invoke.texi (mfix-rm7000, [...]): Document.
[gcc.git] / gcc / config / mips / mips.c
1 /* Subroutines used for MIPS code generation.
2 Copyright (C) 1989-2013 Free Software Foundation, Inc.
3 Contributed by A. Lichnewsky, lich@inria.inria.fr.
4 Changes by Michael Meissner, meissner@osf.org.
5 64-bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
6 Brendan Eich, brendan@microunity.com.
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "insn-attr.h"
34 #include "recog.h"
35 #include "output.h"
36 #include "tree.h"
37 #include "varasm.h"
38 #include "stringpool.h"
39 #include "stor-layout.h"
40 #include "calls.h"
41 #include "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 "hash-table.h"
51 #include "debug.h"
52 #include "target.h"
53 #include "target-def.h"
54 #include "common/common-target.h"
55 #include "langhooks.h"
56 #include "sched-int.h"
57 #include "gimple.h"
58 #include "gimplify.h"
59 #include "bitmap.h"
60 #include "diagnostic.h"
61 #include "target-globals.h"
62 #include "opts.h"
63 #include "tree-pass.h"
64 #include "context.h"
65
66 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
67 #define UNSPEC_ADDRESS_P(X) \
68 (GET_CODE (X) == UNSPEC \
69 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
70 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
71
72 /* Extract the symbol or label from UNSPEC wrapper X. */
73 #define UNSPEC_ADDRESS(X) \
74 XVECEXP (X, 0, 0)
75
76 /* Extract the symbol type from UNSPEC wrapper X. */
77 #define UNSPEC_ADDRESS_TYPE(X) \
78 ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
79
80 /* The maximum distance between the top of the stack frame and the
81 value $sp has when we save and restore registers.
82
83 The value for normal-mode code must be a SMALL_OPERAND and must
84 preserve the maximum stack alignment. We therefore use a value
85 of 0x7ff0 in this case.
86
87 microMIPS LWM and SWM support 12-bit offsets (from -0x800 to 0x7ff),
88 so we use a maximum of 0x7f0 for TARGET_MICROMIPS.
89
90 MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
91 up to 0x7f8 bytes and can usually save or restore all the registers
92 that we need to save or restore. (Note that we can only use these
93 instructions for o32, for which the stack alignment is 8 bytes.)
94
95 We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
96 RESTORE are not available. We can then use unextended instructions
97 to save and restore registers, and to allocate and deallocate the top
98 part of the frame. */
99 #define MIPS_MAX_FIRST_STACK_STEP \
100 (!TARGET_COMPRESSION ? 0x7ff0 \
101 : TARGET_MICROMIPS || GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8 \
102 : TARGET_64BIT ? 0x100 : 0x400)
103
104 /* True if INSN is a mips.md pattern or asm statement. */
105 /* ??? This test exists through the compiler, perhaps it should be
106 moved to rtl.h. */
107 #define USEFUL_INSN_P(INSN) \
108 (NONDEBUG_INSN_P (INSN) \
109 && GET_CODE (PATTERN (INSN)) != USE \
110 && GET_CODE (PATTERN (INSN)) != CLOBBER)
111
112 /* If INSN is a delayed branch sequence, return the first instruction
113 in the sequence, otherwise return INSN itself. */
114 #define SEQ_BEGIN(INSN) \
115 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
116 ? XVECEXP (PATTERN (INSN), 0, 0) \
117 : (INSN))
118
119 /* Likewise for the last instruction in a delayed branch sequence. */
120 #define SEQ_END(INSN) \
121 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
122 ? XVECEXP (PATTERN (INSN), 0, XVECLEN (PATTERN (INSN), 0) - 1) \
123 : (INSN))
124
125 /* Execute the following loop body with SUBINSN set to each instruction
126 between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive. */
127 #define FOR_EACH_SUBINSN(SUBINSN, INSN) \
128 for ((SUBINSN) = SEQ_BEGIN (INSN); \
129 (SUBINSN) != NEXT_INSN (SEQ_END (INSN)); \
130 (SUBINSN) = NEXT_INSN (SUBINSN))
131
132 /* True if bit BIT is set in VALUE. */
133 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
134
135 /* Return the opcode for a ptr_mode load of the form:
136
137 l[wd] DEST, OFFSET(BASE). */
138 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE) \
139 (((ptr_mode == DImode ? 0x37 : 0x23) << 26) \
140 | ((BASE) << 21) \
141 | ((DEST) << 16) \
142 | (OFFSET))
143
144 /* Return the opcode to move register SRC into register DEST. */
145 #define MIPS_MOVE(DEST, SRC) \
146 ((TARGET_64BIT ? 0x2d : 0x21) \
147 | ((DEST) << 11) \
148 | ((SRC) << 21))
149
150 /* Return the opcode for:
151
152 lui DEST, VALUE. */
153 #define MIPS_LUI(DEST, VALUE) \
154 ((0xf << 26) | ((DEST) << 16) | (VALUE))
155
156 /* Return the opcode to jump to register DEST. */
157 #define MIPS_JR(DEST) \
158 (((DEST) << 21) | 0x8)
159
160 /* Return the opcode for:
161
162 bal . + (1 + OFFSET) * 4. */
163 #define MIPS_BAL(OFFSET) \
164 ((0x1 << 26) | (0x11 << 16) | (OFFSET))
165
166 /* Return the usual opcode for a nop. */
167 #define MIPS_NOP 0
168
169 /* Classifies an address.
170
171 ADDRESS_REG
172 A natural register + offset address. The register satisfies
173 mips_valid_base_register_p and the offset is a const_arith_operand.
174
175 ADDRESS_LO_SUM
176 A LO_SUM rtx. The first operand is a valid base register and
177 the second operand is a symbolic address.
178
179 ADDRESS_CONST_INT
180 A signed 16-bit constant address.
181
182 ADDRESS_SYMBOLIC:
183 A constant symbolic address. */
184 enum mips_address_type {
185 ADDRESS_REG,
186 ADDRESS_LO_SUM,
187 ADDRESS_CONST_INT,
188 ADDRESS_SYMBOLIC
189 };
190
191 /* Macros to create an enumeration identifier for a function prototype. */
192 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
193 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
194 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
195 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
196
197 /* Classifies the prototype of a built-in function. */
198 enum mips_function_type {
199 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
200 #include "config/mips/mips-ftypes.def"
201 #undef DEF_MIPS_FTYPE
202 MIPS_MAX_FTYPE_MAX
203 };
204
205 /* Specifies how a built-in function should be converted into rtl. */
206 enum mips_builtin_type {
207 /* The function corresponds directly to an .md pattern. The return
208 value is mapped to operand 0 and the arguments are mapped to
209 operands 1 and above. */
210 MIPS_BUILTIN_DIRECT,
211
212 /* The function corresponds directly to an .md pattern. There is no return
213 value and the arguments are mapped to operands 0 and above. */
214 MIPS_BUILTIN_DIRECT_NO_TARGET,
215
216 /* The function corresponds to a comparison instruction followed by
217 a mips_cond_move_tf_ps pattern. The first two arguments are the
218 values to compare and the second two arguments are the vector
219 operands for the movt.ps or movf.ps instruction (in assembly order). */
220 MIPS_BUILTIN_MOVF,
221 MIPS_BUILTIN_MOVT,
222
223 /* The function corresponds to a V2SF comparison instruction. Operand 0
224 of this instruction is the result of the comparison, which has mode
225 CCV2 or CCV4. The function arguments are mapped to operands 1 and
226 above. The function's return value is an SImode boolean that is
227 true under the following conditions:
228
229 MIPS_BUILTIN_CMP_ANY: one of the registers is true
230 MIPS_BUILTIN_CMP_ALL: all of the registers are true
231 MIPS_BUILTIN_CMP_LOWER: the first register is true
232 MIPS_BUILTIN_CMP_UPPER: the second register is true. */
233 MIPS_BUILTIN_CMP_ANY,
234 MIPS_BUILTIN_CMP_ALL,
235 MIPS_BUILTIN_CMP_UPPER,
236 MIPS_BUILTIN_CMP_LOWER,
237
238 /* As above, but the instruction only sets a single $fcc register. */
239 MIPS_BUILTIN_CMP_SINGLE,
240
241 /* For generating bposge32 branch instructions in MIPS32 DSP ASE. */
242 MIPS_BUILTIN_BPOSGE32
243 };
244
245 /* Invoke MACRO (COND) for each C.cond.fmt condition. */
246 #define MIPS_FP_CONDITIONS(MACRO) \
247 MACRO (f), \
248 MACRO (un), \
249 MACRO (eq), \
250 MACRO (ueq), \
251 MACRO (olt), \
252 MACRO (ult), \
253 MACRO (ole), \
254 MACRO (ule), \
255 MACRO (sf), \
256 MACRO (ngle), \
257 MACRO (seq), \
258 MACRO (ngl), \
259 MACRO (lt), \
260 MACRO (nge), \
261 MACRO (le), \
262 MACRO (ngt)
263
264 /* Enumerates the codes above as MIPS_FP_COND_<X>. */
265 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
266 enum mips_fp_condition {
267 MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
268 };
269
270 /* Index X provides the string representation of MIPS_FP_COND_<X>. */
271 #define STRINGIFY(X) #X
272 static const char *const mips_fp_conditions[] = {
273 MIPS_FP_CONDITIONS (STRINGIFY)
274 };
275
276 /* Tuning information that is automatically derived from other sources
277 (such as the scheduler). */
278 static struct {
279 /* The architecture and tuning settings that this structure describes. */
280 enum processor arch;
281 enum processor tune;
282
283 /* True if this structure describes MIPS16 settings. */
284 bool mips16_p;
285
286 /* True if the structure has been initialized. */
287 bool initialized_p;
288
289 /* True if "MULT $0, $0" is preferable to "MTLO $0; MTHI $0"
290 when optimizing for speed. */
291 bool fast_mult_zero_zero_p;
292 } mips_tuning_info;
293
294 /* Information about a function's frame layout. */
295 struct GTY(()) mips_frame_info {
296 /* The size of the frame in bytes. */
297 HOST_WIDE_INT total_size;
298
299 /* The number of bytes allocated to variables. */
300 HOST_WIDE_INT var_size;
301
302 /* The number of bytes allocated to outgoing function arguments. */
303 HOST_WIDE_INT args_size;
304
305 /* The number of bytes allocated to the .cprestore slot, or 0 if there
306 is no such slot. */
307 HOST_WIDE_INT cprestore_size;
308
309 /* Bit X is set if the function saves or restores GPR X. */
310 unsigned int mask;
311
312 /* Likewise FPR X. */
313 unsigned int fmask;
314
315 /* Likewise doubleword accumulator X ($acX). */
316 unsigned int acc_mask;
317
318 /* The number of GPRs, FPRs, doubleword accumulators and COP0
319 registers saved. */
320 unsigned int num_gp;
321 unsigned int num_fp;
322 unsigned int num_acc;
323 unsigned int num_cop0_regs;
324
325 /* The offset of the topmost GPR, FPR, accumulator and COP0-register
326 save slots from the top of the frame, or zero if no such slots are
327 needed. */
328 HOST_WIDE_INT gp_save_offset;
329 HOST_WIDE_INT fp_save_offset;
330 HOST_WIDE_INT acc_save_offset;
331 HOST_WIDE_INT cop0_save_offset;
332
333 /* Likewise, but giving offsets from the bottom of the frame. */
334 HOST_WIDE_INT gp_sp_offset;
335 HOST_WIDE_INT fp_sp_offset;
336 HOST_WIDE_INT acc_sp_offset;
337 HOST_WIDE_INT cop0_sp_offset;
338
339 /* Similar, but the value passed to _mcount. */
340 HOST_WIDE_INT ra_fp_offset;
341
342 /* The offset of arg_pointer_rtx from the bottom of the frame. */
343 HOST_WIDE_INT arg_pointer_offset;
344
345 /* The offset of hard_frame_pointer_rtx from the bottom of the frame. */
346 HOST_WIDE_INT hard_frame_pointer_offset;
347 };
348
349 struct GTY(()) machine_function {
350 /* The next floating-point condition-code register to allocate
351 for ISA_HAS_8CC targets, relative to ST_REG_FIRST. */
352 unsigned int next_fcc;
353
354 /* The register returned by mips16_gp_pseudo_reg; see there for details. */
355 rtx mips16_gp_pseudo_rtx;
356
357 /* The number of extra stack bytes taken up by register varargs.
358 This area is allocated by the callee at the very top of the frame. */
359 int varargs_size;
360
361 /* The current frame information, calculated by mips_compute_frame_info. */
362 struct mips_frame_info frame;
363
364 /* The register to use as the function's global pointer, or INVALID_REGNUM
365 if the function doesn't need one. */
366 unsigned int global_pointer;
367
368 /* How many instructions it takes to load a label into $AT, or 0 if
369 this property hasn't yet been calculated. */
370 unsigned int load_label_num_insns;
371
372 /* True if mips_adjust_insn_length should ignore an instruction's
373 hazard attribute. */
374 bool ignore_hazard_length_p;
375
376 /* True if the whole function is suitable for .set noreorder and
377 .set nomacro. */
378 bool all_noreorder_p;
379
380 /* True if the function has "inflexible" and "flexible" references
381 to the global pointer. See mips_cfun_has_inflexible_gp_ref_p
382 and mips_cfun_has_flexible_gp_ref_p for details. */
383 bool has_inflexible_gp_insn_p;
384 bool has_flexible_gp_insn_p;
385
386 /* True if the function's prologue must load the global pointer
387 value into pic_offset_table_rtx and store the same value in
388 the function's cprestore slot (if any). Even if this value
389 is currently false, we may decide to set it to true later;
390 see mips_must_initialize_gp_p () for details. */
391 bool must_initialize_gp_p;
392
393 /* True if the current function must restore $gp after any potential
394 clobber. This value is only meaningful during the first post-epilogue
395 split_insns pass; see mips_must_initialize_gp_p () for details. */
396 bool must_restore_gp_when_clobbered_p;
397
398 /* True if this is an interrupt handler. */
399 bool interrupt_handler_p;
400
401 /* True if this is an interrupt handler that uses shadow registers. */
402 bool use_shadow_register_set_p;
403
404 /* True if this is an interrupt handler that should keep interrupts
405 masked. */
406 bool keep_interrupts_masked_p;
407
408 /* True if this is an interrupt handler that should use DERET
409 instead of ERET. */
410 bool use_debug_exception_return_p;
411 };
412
413 /* Information about a single argument. */
414 struct mips_arg_info {
415 /* True if the argument is passed in a floating-point register, or
416 would have been if we hadn't run out of registers. */
417 bool fpr_p;
418
419 /* The number of words passed in registers, rounded up. */
420 unsigned int reg_words;
421
422 /* For EABI, the offset of the first register from GP_ARG_FIRST or
423 FP_ARG_FIRST. For other ABIs, the offset of the first register from
424 the start of the ABI's argument structure (see the CUMULATIVE_ARGS
425 comment for details).
426
427 The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
428 on the stack. */
429 unsigned int reg_offset;
430
431 /* The number of words that must be passed on the stack, rounded up. */
432 unsigned int stack_words;
433
434 /* The offset from the start of the stack overflow area of the argument's
435 first stack word. Only meaningful when STACK_WORDS is nonzero. */
436 unsigned int stack_offset;
437 };
438
439 /* Information about an address described by mips_address_type.
440
441 ADDRESS_CONST_INT
442 No fields are used.
443
444 ADDRESS_REG
445 REG is the base register and OFFSET is the constant offset.
446
447 ADDRESS_LO_SUM
448 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
449 is the type of symbol it references.
450
451 ADDRESS_SYMBOLIC
452 SYMBOL_TYPE is the type of symbol that the address references. */
453 struct mips_address_info {
454 enum mips_address_type type;
455 rtx reg;
456 rtx offset;
457 enum mips_symbol_type symbol_type;
458 };
459
460 /* One stage in a constant building sequence. These sequences have
461 the form:
462
463 A = VALUE[0]
464 A = A CODE[1] VALUE[1]
465 A = A CODE[2] VALUE[2]
466 ...
467
468 where A is an accumulator, each CODE[i] is a binary rtl operation
469 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
470 struct mips_integer_op {
471 enum rtx_code code;
472 unsigned HOST_WIDE_INT value;
473 };
474
475 /* The largest number of operations needed to load an integer constant.
476 The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
477 When the lowest bit is clear, we can try, but reject a sequence with
478 an extra SLL at the end. */
479 #define MIPS_MAX_INTEGER_OPS 7
480
481 /* Information about a MIPS16e SAVE or RESTORE instruction. */
482 struct mips16e_save_restore_info {
483 /* The number of argument registers saved by a SAVE instruction.
484 0 for RESTORE instructions. */
485 unsigned int nargs;
486
487 /* Bit X is set if the instruction saves or restores GPR X. */
488 unsigned int mask;
489
490 /* The total number of bytes to allocate. */
491 HOST_WIDE_INT size;
492 };
493
494 /* Costs of various operations on the different architectures. */
495
496 struct mips_rtx_cost_data
497 {
498 unsigned short fp_add;
499 unsigned short fp_mult_sf;
500 unsigned short fp_mult_df;
501 unsigned short fp_div_sf;
502 unsigned short fp_div_df;
503 unsigned short int_mult_si;
504 unsigned short int_mult_di;
505 unsigned short int_div_si;
506 unsigned short int_div_di;
507 unsigned short branch_cost;
508 unsigned short memory_latency;
509 };
510
511 /* Global variables for machine-dependent things. */
512
513 /* The -G setting, or the configuration's default small-data limit if
514 no -G option is given. */
515 static unsigned int mips_small_data_threshold;
516
517 /* The number of file directives written by mips_output_filename. */
518 int num_source_filenames;
519
520 /* The name that appeared in the last .file directive written by
521 mips_output_filename, or "" if mips_output_filename hasn't
522 written anything yet. */
523 const char *current_function_file = "";
524
525 /* Arrays that map GCC register numbers to debugger register numbers. */
526 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
527 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
528
529 /* Information about the current function's epilogue, used only while
530 expanding it. */
531 static struct {
532 /* A list of queued REG_CFA_RESTORE notes. */
533 rtx cfa_restores;
534
535 /* The CFA is currently defined as CFA_REG + CFA_OFFSET. */
536 rtx cfa_reg;
537 HOST_WIDE_INT cfa_offset;
538
539 /* The offset of the CFA from the stack pointer while restoring
540 registers. */
541 HOST_WIDE_INT cfa_restore_sp_offset;
542 } mips_epilogue;
543
544 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs. */
545 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
546 struct mips_asm_switch mips_nomacro = { "macro", 0 };
547 struct mips_asm_switch mips_noat = { "at", 0 };
548
549 /* True if we're writing out a branch-likely instruction rather than a
550 normal branch. */
551 static bool mips_branch_likely;
552
553 /* The current instruction-set architecture. */
554 enum processor mips_arch;
555 const struct mips_cpu_info *mips_arch_info;
556
557 /* The processor that we should tune the code for. */
558 enum processor mips_tune;
559 const struct mips_cpu_info *mips_tune_info;
560
561 /* The ISA level associated with mips_arch. */
562 int mips_isa;
563
564 /* The architecture selected by -mipsN, or null if -mipsN wasn't used. */
565 static const struct mips_cpu_info *mips_isa_option_info;
566
567 /* Which cost information to use. */
568 static const struct mips_rtx_cost_data *mips_cost;
569
570 /* The ambient target flags, excluding MASK_MIPS16. */
571 static int mips_base_target_flags;
572
573 /* The default compression mode. */
574 unsigned int mips_base_compression_flags;
575
576 /* The ambient values of other global variables. */
577 static int mips_base_schedule_insns; /* flag_schedule_insns */
578 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
579 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
580 static int mips_base_align_loops; /* align_loops */
581 static int mips_base_align_jumps; /* align_jumps */
582 static int mips_base_align_functions; /* align_functions */
583
584 /* Index [M][R] is true if register R is allowed to hold a value of mode M. */
585 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
586
587 /* Index C is true if character C is a valid PRINT_OPERAND punctation
588 character. */
589 static bool mips_print_operand_punct[256];
590
591 static GTY (()) int mips_output_filename_first_time = 1;
592
593 /* mips_split_p[X] is true if symbols of type X can be split by
594 mips_split_symbol. */
595 bool mips_split_p[NUM_SYMBOL_TYPES];
596
597 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
598 can be split by mips_split_symbol. */
599 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
600
601 /* mips_use_pcrel_pool_p[X] is true if symbols of type X should be
602 forced into a PC-relative constant pool. */
603 bool mips_use_pcrel_pool_p[NUM_SYMBOL_TYPES];
604
605 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
606 appears in a LO_SUM. It can be null if such LO_SUMs aren't valid or
607 if they are matched by a special .md file pattern. */
608 const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
609
610 /* Likewise for HIGHs. */
611 const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
612
613 /* Target state for MIPS16. */
614 struct target_globals *mips16_globals;
615
616 /* Cached value of can_issue_more. This is cached in mips_variable_issue hook
617 and returned from mips_sched_reorder2. */
618 static int cached_can_issue_more;
619
620 /* True if the output uses __mips16_rdhwr. */
621 static bool mips_need_mips16_rdhwr_p;
622
623 /* Index R is the smallest register class that contains register R. */
624 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
625 LEA_REGS, LEA_REGS, M16_REGS, V1_REG,
626 M16_REGS, M16_REGS, M16_REGS, M16_REGS,
627 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
628 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
629 M16_REGS, M16_REGS, LEA_REGS, LEA_REGS,
630 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
631 T_REG, PIC_FN_ADDR_REG, LEA_REGS, LEA_REGS,
632 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
633 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
634 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
635 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
636 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
637 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
638 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
639 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
640 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
641 MD0_REG, MD1_REG, NO_REGS, ST_REGS,
642 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
643 ST_REGS, ST_REGS, ST_REGS, NO_REGS,
644 NO_REGS, FRAME_REGS, FRAME_REGS, NO_REGS,
645 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
646 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
647 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
648 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
649 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
650 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
651 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
652 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
653 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
654 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
655 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
656 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
657 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
658 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
659 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
660 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
661 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
662 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
663 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
664 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
665 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
666 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
667 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
668 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
669 DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS,
670 DSP_ACC_REGS, DSP_ACC_REGS, ALL_REGS, ALL_REGS,
671 ALL_REGS, ALL_REGS, ALL_REGS, ALL_REGS
672 };
673
674 /* The value of TARGET_ATTRIBUTE_TABLE. */
675 static const struct attribute_spec mips_attribute_table[] = {
676 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
677 om_diagnostic } */
678 { "long_call", 0, 0, false, true, true, NULL, false },
679 { "far", 0, 0, false, true, true, NULL, false },
680 { "near", 0, 0, false, true, true, NULL, false },
681 /* We would really like to treat "mips16" and "nomips16" as type
682 attributes, but GCC doesn't provide the hooks we need to support
683 the right conversion rules. As declaration attributes, they affect
684 code generation but don't carry other semantics. */
685 { "mips16", 0, 0, true, false, false, NULL, false },
686 { "nomips16", 0, 0, true, false, false, NULL, false },
687 { "micromips", 0, 0, true, false, false, NULL, false },
688 { "nomicromips", 0, 0, true, false, false, NULL, false },
689 { "nocompression", 0, 0, true, false, false, NULL, false },
690 /* Allow functions to be specified as interrupt handlers */
691 { "interrupt", 0, 0, false, true, true, NULL, false },
692 { "use_shadow_register_set", 0, 0, false, true, true, NULL, false },
693 { "keep_interrupts_masked", 0, 0, false, true, true, NULL, false },
694 { "use_debug_exception_return", 0, 0, false, true, true, NULL, false },
695 { NULL, 0, 0, false, false, false, NULL, false }
696 };
697 \f
698 /* A table describing all the processors GCC knows about; see
699 mips-cpus.def for details. */
700 static const struct mips_cpu_info mips_cpu_info_table[] = {
701 #define MIPS_CPU(NAME, CPU, ISA, FLAGS) \
702 { NAME, CPU, ISA, FLAGS },
703 #include "mips-cpus.def"
704 #undef MIPS_CPU
705 };
706
707 /* Default costs. If these are used for a processor we should look
708 up the actual costs. */
709 #define DEFAULT_COSTS COSTS_N_INSNS (6), /* fp_add */ \
710 COSTS_N_INSNS (7), /* fp_mult_sf */ \
711 COSTS_N_INSNS (8), /* fp_mult_df */ \
712 COSTS_N_INSNS (23), /* fp_div_sf */ \
713 COSTS_N_INSNS (36), /* fp_div_df */ \
714 COSTS_N_INSNS (10), /* int_mult_si */ \
715 COSTS_N_INSNS (10), /* int_mult_di */ \
716 COSTS_N_INSNS (69), /* int_div_si */ \
717 COSTS_N_INSNS (69), /* int_div_di */ \
718 2, /* branch_cost */ \
719 4 /* memory_latency */
720
721 /* Floating-point costs for processors without an FPU. Just assume that
722 all floating-point libcalls are very expensive. */
723 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */ \
724 COSTS_N_INSNS (256), /* fp_mult_sf */ \
725 COSTS_N_INSNS (256), /* fp_mult_df */ \
726 COSTS_N_INSNS (256), /* fp_div_sf */ \
727 COSTS_N_INSNS (256) /* fp_div_df */
728
729 /* Costs to use when optimizing for size. */
730 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
731 COSTS_N_INSNS (1), /* fp_add */
732 COSTS_N_INSNS (1), /* fp_mult_sf */
733 COSTS_N_INSNS (1), /* fp_mult_df */
734 COSTS_N_INSNS (1), /* fp_div_sf */
735 COSTS_N_INSNS (1), /* fp_div_df */
736 COSTS_N_INSNS (1), /* int_mult_si */
737 COSTS_N_INSNS (1), /* int_mult_di */
738 COSTS_N_INSNS (1), /* int_div_si */
739 COSTS_N_INSNS (1), /* int_div_di */
740 2, /* branch_cost */
741 4 /* memory_latency */
742 };
743
744 /* Costs to use when optimizing for speed, indexed by processor. */
745 static const struct mips_rtx_cost_data
746 mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
747 { /* R3000 */
748 COSTS_N_INSNS (2), /* fp_add */
749 COSTS_N_INSNS (4), /* fp_mult_sf */
750 COSTS_N_INSNS (5), /* fp_mult_df */
751 COSTS_N_INSNS (12), /* fp_div_sf */
752 COSTS_N_INSNS (19), /* fp_div_df */
753 COSTS_N_INSNS (12), /* int_mult_si */
754 COSTS_N_INSNS (12), /* int_mult_di */
755 COSTS_N_INSNS (35), /* int_div_si */
756 COSTS_N_INSNS (35), /* int_div_di */
757 1, /* branch_cost */
758 4 /* memory_latency */
759 },
760 { /* 4KC */
761 SOFT_FP_COSTS,
762 COSTS_N_INSNS (6), /* int_mult_si */
763 COSTS_N_INSNS (6), /* int_mult_di */
764 COSTS_N_INSNS (36), /* int_div_si */
765 COSTS_N_INSNS (36), /* int_div_di */
766 1, /* branch_cost */
767 4 /* memory_latency */
768 },
769 { /* 4KP */
770 SOFT_FP_COSTS,
771 COSTS_N_INSNS (36), /* int_mult_si */
772 COSTS_N_INSNS (36), /* int_mult_di */
773 COSTS_N_INSNS (37), /* int_div_si */
774 COSTS_N_INSNS (37), /* int_div_di */
775 1, /* branch_cost */
776 4 /* memory_latency */
777 },
778 { /* 5KC */
779 SOFT_FP_COSTS,
780 COSTS_N_INSNS (4), /* int_mult_si */
781 COSTS_N_INSNS (11), /* int_mult_di */
782 COSTS_N_INSNS (36), /* int_div_si */
783 COSTS_N_INSNS (68), /* int_div_di */
784 1, /* branch_cost */
785 4 /* memory_latency */
786 },
787 { /* 5KF */
788 COSTS_N_INSNS (4), /* fp_add */
789 COSTS_N_INSNS (4), /* fp_mult_sf */
790 COSTS_N_INSNS (5), /* fp_mult_df */
791 COSTS_N_INSNS (17), /* fp_div_sf */
792 COSTS_N_INSNS (32), /* fp_div_df */
793 COSTS_N_INSNS (4), /* int_mult_si */
794 COSTS_N_INSNS (11), /* int_mult_di */
795 COSTS_N_INSNS (36), /* int_div_si */
796 COSTS_N_INSNS (68), /* int_div_di */
797 1, /* branch_cost */
798 4 /* memory_latency */
799 },
800 { /* 20KC */
801 COSTS_N_INSNS (4), /* fp_add */
802 COSTS_N_INSNS (4), /* fp_mult_sf */
803 COSTS_N_INSNS (5), /* fp_mult_df */
804 COSTS_N_INSNS (17), /* fp_div_sf */
805 COSTS_N_INSNS (32), /* fp_div_df */
806 COSTS_N_INSNS (4), /* int_mult_si */
807 COSTS_N_INSNS (7), /* int_mult_di */
808 COSTS_N_INSNS (42), /* int_div_si */
809 COSTS_N_INSNS (72), /* int_div_di */
810 1, /* branch_cost */
811 4 /* memory_latency */
812 },
813 { /* 24KC */
814 SOFT_FP_COSTS,
815 COSTS_N_INSNS (5), /* int_mult_si */
816 COSTS_N_INSNS (5), /* int_mult_di */
817 COSTS_N_INSNS (41), /* int_div_si */
818 COSTS_N_INSNS (41), /* int_div_di */
819 1, /* branch_cost */
820 4 /* memory_latency */
821 },
822 { /* 24KF2_1 */
823 COSTS_N_INSNS (8), /* fp_add */
824 COSTS_N_INSNS (8), /* fp_mult_sf */
825 COSTS_N_INSNS (10), /* fp_mult_df */
826 COSTS_N_INSNS (34), /* fp_div_sf */
827 COSTS_N_INSNS (64), /* fp_div_df */
828 COSTS_N_INSNS (5), /* int_mult_si */
829 COSTS_N_INSNS (5), /* int_mult_di */
830 COSTS_N_INSNS (41), /* int_div_si */
831 COSTS_N_INSNS (41), /* int_div_di */
832 1, /* branch_cost */
833 4 /* memory_latency */
834 },
835 { /* 24KF1_1 */
836 COSTS_N_INSNS (4), /* fp_add */
837 COSTS_N_INSNS (4), /* fp_mult_sf */
838 COSTS_N_INSNS (5), /* fp_mult_df */
839 COSTS_N_INSNS (17), /* fp_div_sf */
840 COSTS_N_INSNS (32), /* fp_div_df */
841 COSTS_N_INSNS (5), /* int_mult_si */
842 COSTS_N_INSNS (5), /* int_mult_di */
843 COSTS_N_INSNS (41), /* int_div_si */
844 COSTS_N_INSNS (41), /* int_div_di */
845 1, /* branch_cost */
846 4 /* memory_latency */
847 },
848 { /* 74KC */
849 SOFT_FP_COSTS,
850 COSTS_N_INSNS (5), /* int_mult_si */
851 COSTS_N_INSNS (5), /* int_mult_di */
852 COSTS_N_INSNS (41), /* int_div_si */
853 COSTS_N_INSNS (41), /* int_div_di */
854 1, /* branch_cost */
855 4 /* memory_latency */
856 },
857 { /* 74KF2_1 */
858 COSTS_N_INSNS (8), /* fp_add */
859 COSTS_N_INSNS (8), /* fp_mult_sf */
860 COSTS_N_INSNS (10), /* fp_mult_df */
861 COSTS_N_INSNS (34), /* fp_div_sf */
862 COSTS_N_INSNS (64), /* fp_div_df */
863 COSTS_N_INSNS (5), /* int_mult_si */
864 COSTS_N_INSNS (5), /* int_mult_di */
865 COSTS_N_INSNS (41), /* int_div_si */
866 COSTS_N_INSNS (41), /* int_div_di */
867 1, /* branch_cost */
868 4 /* memory_latency */
869 },
870 { /* 74KF1_1 */
871 COSTS_N_INSNS (4), /* fp_add */
872 COSTS_N_INSNS (4), /* fp_mult_sf */
873 COSTS_N_INSNS (5), /* fp_mult_df */
874 COSTS_N_INSNS (17), /* fp_div_sf */
875 COSTS_N_INSNS (32), /* fp_div_df */
876 COSTS_N_INSNS (5), /* int_mult_si */
877 COSTS_N_INSNS (5), /* int_mult_di */
878 COSTS_N_INSNS (41), /* int_div_si */
879 COSTS_N_INSNS (41), /* int_div_di */
880 1, /* branch_cost */
881 4 /* memory_latency */
882 },
883 { /* 74KF3_2 */
884 COSTS_N_INSNS (6), /* fp_add */
885 COSTS_N_INSNS (6), /* fp_mult_sf */
886 COSTS_N_INSNS (7), /* fp_mult_df */
887 COSTS_N_INSNS (25), /* fp_div_sf */
888 COSTS_N_INSNS (48), /* fp_div_df */
889 COSTS_N_INSNS (5), /* int_mult_si */
890 COSTS_N_INSNS (5), /* int_mult_di */
891 COSTS_N_INSNS (41), /* int_div_si */
892 COSTS_N_INSNS (41), /* int_div_di */
893 1, /* branch_cost */
894 4 /* memory_latency */
895 },
896 { /* Loongson-2E */
897 DEFAULT_COSTS
898 },
899 { /* Loongson-2F */
900 DEFAULT_COSTS
901 },
902 { /* Loongson-3A */
903 DEFAULT_COSTS
904 },
905 { /* M4k */
906 DEFAULT_COSTS
907 },
908 /* Octeon */
909 {
910 SOFT_FP_COSTS,
911 COSTS_N_INSNS (5), /* int_mult_si */
912 COSTS_N_INSNS (5), /* int_mult_di */
913 COSTS_N_INSNS (72), /* int_div_si */
914 COSTS_N_INSNS (72), /* int_div_di */
915 1, /* branch_cost */
916 4 /* memory_latency */
917 },
918 /* Octeon II */
919 {
920 SOFT_FP_COSTS,
921 COSTS_N_INSNS (6), /* int_mult_si */
922 COSTS_N_INSNS (6), /* int_mult_di */
923 COSTS_N_INSNS (18), /* int_div_si */
924 COSTS_N_INSNS (35), /* int_div_di */
925 4, /* branch_cost */
926 4 /* memory_latency */
927 },
928 { /* R3900 */
929 COSTS_N_INSNS (2), /* fp_add */
930 COSTS_N_INSNS (4), /* fp_mult_sf */
931 COSTS_N_INSNS (5), /* fp_mult_df */
932 COSTS_N_INSNS (12), /* fp_div_sf */
933 COSTS_N_INSNS (19), /* fp_div_df */
934 COSTS_N_INSNS (2), /* int_mult_si */
935 COSTS_N_INSNS (2), /* int_mult_di */
936 COSTS_N_INSNS (35), /* int_div_si */
937 COSTS_N_INSNS (35), /* int_div_di */
938 1, /* branch_cost */
939 4 /* memory_latency */
940 },
941 { /* R6000 */
942 COSTS_N_INSNS (3), /* fp_add */
943 COSTS_N_INSNS (5), /* fp_mult_sf */
944 COSTS_N_INSNS (6), /* fp_mult_df */
945 COSTS_N_INSNS (15), /* fp_div_sf */
946 COSTS_N_INSNS (16), /* fp_div_df */
947 COSTS_N_INSNS (17), /* int_mult_si */
948 COSTS_N_INSNS (17), /* int_mult_di */
949 COSTS_N_INSNS (38), /* int_div_si */
950 COSTS_N_INSNS (38), /* int_div_di */
951 2, /* branch_cost */
952 6 /* memory_latency */
953 },
954 { /* R4000 */
955 COSTS_N_INSNS (6), /* fp_add */
956 COSTS_N_INSNS (7), /* fp_mult_sf */
957 COSTS_N_INSNS (8), /* fp_mult_df */
958 COSTS_N_INSNS (23), /* fp_div_sf */
959 COSTS_N_INSNS (36), /* fp_div_df */
960 COSTS_N_INSNS (10), /* int_mult_si */
961 COSTS_N_INSNS (10), /* int_mult_di */
962 COSTS_N_INSNS (69), /* int_div_si */
963 COSTS_N_INSNS (69), /* int_div_di */
964 2, /* branch_cost */
965 6 /* memory_latency */
966 },
967 { /* R4100 */
968 DEFAULT_COSTS
969 },
970 { /* R4111 */
971 DEFAULT_COSTS
972 },
973 { /* R4120 */
974 DEFAULT_COSTS
975 },
976 { /* R4130 */
977 /* The only costs that appear to be updated here are
978 integer multiplication. */
979 SOFT_FP_COSTS,
980 COSTS_N_INSNS (4), /* int_mult_si */
981 COSTS_N_INSNS (6), /* int_mult_di */
982 COSTS_N_INSNS (69), /* int_div_si */
983 COSTS_N_INSNS (69), /* int_div_di */
984 1, /* branch_cost */
985 4 /* memory_latency */
986 },
987 { /* R4300 */
988 DEFAULT_COSTS
989 },
990 { /* R4600 */
991 DEFAULT_COSTS
992 },
993 { /* R4650 */
994 DEFAULT_COSTS
995 },
996 { /* R4700 */
997 DEFAULT_COSTS
998 },
999 { /* R5000 */
1000 COSTS_N_INSNS (6), /* fp_add */
1001 COSTS_N_INSNS (4), /* fp_mult_sf */
1002 COSTS_N_INSNS (5), /* fp_mult_df */
1003 COSTS_N_INSNS (23), /* fp_div_sf */
1004 COSTS_N_INSNS (36), /* fp_div_df */
1005 COSTS_N_INSNS (5), /* int_mult_si */
1006 COSTS_N_INSNS (5), /* int_mult_di */
1007 COSTS_N_INSNS (36), /* int_div_si */
1008 COSTS_N_INSNS (36), /* int_div_di */
1009 1, /* branch_cost */
1010 4 /* memory_latency */
1011 },
1012 { /* R5400 */
1013 COSTS_N_INSNS (6), /* fp_add */
1014 COSTS_N_INSNS (5), /* fp_mult_sf */
1015 COSTS_N_INSNS (6), /* fp_mult_df */
1016 COSTS_N_INSNS (30), /* fp_div_sf */
1017 COSTS_N_INSNS (59), /* fp_div_df */
1018 COSTS_N_INSNS (3), /* int_mult_si */
1019 COSTS_N_INSNS (4), /* int_mult_di */
1020 COSTS_N_INSNS (42), /* int_div_si */
1021 COSTS_N_INSNS (74), /* int_div_di */
1022 1, /* branch_cost */
1023 4 /* memory_latency */
1024 },
1025 { /* R5500 */
1026 COSTS_N_INSNS (6), /* fp_add */
1027 COSTS_N_INSNS (5), /* fp_mult_sf */
1028 COSTS_N_INSNS (6), /* fp_mult_df */
1029 COSTS_N_INSNS (30), /* fp_div_sf */
1030 COSTS_N_INSNS (59), /* fp_div_df */
1031 COSTS_N_INSNS (5), /* int_mult_si */
1032 COSTS_N_INSNS (9), /* int_mult_di */
1033 COSTS_N_INSNS (42), /* int_div_si */
1034 COSTS_N_INSNS (74), /* int_div_di */
1035 1, /* branch_cost */
1036 4 /* memory_latency */
1037 },
1038 { /* R5900 */
1039 COSTS_N_INSNS (4), /* fp_add */
1040 COSTS_N_INSNS (4), /* fp_mult_sf */
1041 COSTS_N_INSNS (256), /* fp_mult_df */
1042 COSTS_N_INSNS (8), /* fp_div_sf */
1043 COSTS_N_INSNS (256), /* fp_div_df */
1044 COSTS_N_INSNS (4), /* int_mult_si */
1045 COSTS_N_INSNS (256), /* int_mult_di */
1046 COSTS_N_INSNS (37), /* int_div_si */
1047 COSTS_N_INSNS (256), /* int_div_di */
1048 1, /* branch_cost */
1049 4 /* memory_latency */
1050 },
1051 { /* R7000 */
1052 /* The only costs that are changed here are
1053 integer multiplication. */
1054 COSTS_N_INSNS (6), /* fp_add */
1055 COSTS_N_INSNS (7), /* fp_mult_sf */
1056 COSTS_N_INSNS (8), /* fp_mult_df */
1057 COSTS_N_INSNS (23), /* fp_div_sf */
1058 COSTS_N_INSNS (36), /* fp_div_df */
1059 COSTS_N_INSNS (5), /* int_mult_si */
1060 COSTS_N_INSNS (9), /* int_mult_di */
1061 COSTS_N_INSNS (69), /* int_div_si */
1062 COSTS_N_INSNS (69), /* int_div_di */
1063 1, /* branch_cost */
1064 4 /* memory_latency */
1065 },
1066 { /* R8000 */
1067 DEFAULT_COSTS
1068 },
1069 { /* R9000 */
1070 /* The only costs that are changed here are
1071 integer multiplication. */
1072 COSTS_N_INSNS (6), /* fp_add */
1073 COSTS_N_INSNS (7), /* fp_mult_sf */
1074 COSTS_N_INSNS (8), /* fp_mult_df */
1075 COSTS_N_INSNS (23), /* fp_div_sf */
1076 COSTS_N_INSNS (36), /* fp_div_df */
1077 COSTS_N_INSNS (3), /* int_mult_si */
1078 COSTS_N_INSNS (8), /* int_mult_di */
1079 COSTS_N_INSNS (69), /* int_div_si */
1080 COSTS_N_INSNS (69), /* int_div_di */
1081 1, /* branch_cost */
1082 4 /* memory_latency */
1083 },
1084 { /* R1x000 */
1085 COSTS_N_INSNS (2), /* fp_add */
1086 COSTS_N_INSNS (2), /* fp_mult_sf */
1087 COSTS_N_INSNS (2), /* fp_mult_df */
1088 COSTS_N_INSNS (12), /* fp_div_sf */
1089 COSTS_N_INSNS (19), /* fp_div_df */
1090 COSTS_N_INSNS (5), /* int_mult_si */
1091 COSTS_N_INSNS (9), /* int_mult_di */
1092 COSTS_N_INSNS (34), /* int_div_si */
1093 COSTS_N_INSNS (66), /* int_div_di */
1094 1, /* branch_cost */
1095 4 /* memory_latency */
1096 },
1097 { /* SB1 */
1098 /* These costs are the same as the SB-1A below. */
1099 COSTS_N_INSNS (4), /* fp_add */
1100 COSTS_N_INSNS (4), /* fp_mult_sf */
1101 COSTS_N_INSNS (4), /* fp_mult_df */
1102 COSTS_N_INSNS (24), /* fp_div_sf */
1103 COSTS_N_INSNS (32), /* fp_div_df */
1104 COSTS_N_INSNS (3), /* int_mult_si */
1105 COSTS_N_INSNS (4), /* int_mult_di */
1106 COSTS_N_INSNS (36), /* int_div_si */
1107 COSTS_N_INSNS (68), /* int_div_di */
1108 1, /* branch_cost */
1109 4 /* memory_latency */
1110 },
1111 { /* SB1-A */
1112 /* These costs are the same as the SB-1 above. */
1113 COSTS_N_INSNS (4), /* fp_add */
1114 COSTS_N_INSNS (4), /* fp_mult_sf */
1115 COSTS_N_INSNS (4), /* fp_mult_df */
1116 COSTS_N_INSNS (24), /* fp_div_sf */
1117 COSTS_N_INSNS (32), /* fp_div_df */
1118 COSTS_N_INSNS (3), /* int_mult_si */
1119 COSTS_N_INSNS (4), /* int_mult_di */
1120 COSTS_N_INSNS (36), /* int_div_si */
1121 COSTS_N_INSNS (68), /* int_div_di */
1122 1, /* branch_cost */
1123 4 /* memory_latency */
1124 },
1125 { /* SR71000 */
1126 DEFAULT_COSTS
1127 },
1128 { /* XLR */
1129 SOFT_FP_COSTS,
1130 COSTS_N_INSNS (8), /* int_mult_si */
1131 COSTS_N_INSNS (8), /* int_mult_di */
1132 COSTS_N_INSNS (72), /* int_div_si */
1133 COSTS_N_INSNS (72), /* int_div_di */
1134 1, /* branch_cost */
1135 4 /* memory_latency */
1136 },
1137 { /* XLP */
1138 /* These costs are the same as 5KF above. */
1139 COSTS_N_INSNS (4), /* fp_add */
1140 COSTS_N_INSNS (4), /* fp_mult_sf */
1141 COSTS_N_INSNS (5), /* fp_mult_df */
1142 COSTS_N_INSNS (17), /* fp_div_sf */
1143 COSTS_N_INSNS (32), /* fp_div_df */
1144 COSTS_N_INSNS (4), /* int_mult_si */
1145 COSTS_N_INSNS (11), /* int_mult_di */
1146 COSTS_N_INSNS (36), /* int_div_si */
1147 COSTS_N_INSNS (68), /* int_div_di */
1148 1, /* branch_cost */
1149 4 /* memory_latency */
1150 }
1151 };
1152 \f
1153 static rtx mips_find_pic_call_symbol (rtx, rtx, bool);
1154 static int mips_register_move_cost (enum machine_mode, reg_class_t,
1155 reg_class_t);
1156 static unsigned int mips_function_arg_boundary (enum machine_mode, const_tree);
1157 \f
1158 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1159 for -mflip_mips16. It maps decl names onto a boolean mode setting. */
1160 struct GTY (()) mflip_mips16_entry {
1161 const char *name;
1162 bool mips16_p;
1163 };
1164 static GTY ((param_is (struct mflip_mips16_entry))) htab_t mflip_mips16_htab;
1165
1166 /* Hash table callbacks for mflip_mips16_htab. */
1167
1168 static hashval_t
1169 mflip_mips16_htab_hash (const void *entry)
1170 {
1171 return htab_hash_string (((const struct mflip_mips16_entry *) entry)->name);
1172 }
1173
1174 static int
1175 mflip_mips16_htab_eq (const void *entry, const void *name)
1176 {
1177 return strcmp (((const struct mflip_mips16_entry *) entry)->name,
1178 (const char *) name) == 0;
1179 }
1180
1181 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1182 mode, false if it should next add an attribute for the opposite mode. */
1183 static GTY(()) bool mips16_flipper;
1184
1185 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1186 for -mflip-mips16. Return true if it should use "mips16" and false if
1187 it should use "nomips16". */
1188
1189 static bool
1190 mflip_mips16_use_mips16_p (tree decl)
1191 {
1192 struct mflip_mips16_entry *entry;
1193 const char *name;
1194 hashval_t hash;
1195 void **slot;
1196 bool base_is_mips16 = (mips_base_compression_flags & MASK_MIPS16) != 0;
1197
1198 /* Use the opposite of the command-line setting for anonymous decls. */
1199 if (!DECL_NAME (decl))
1200 return !base_is_mips16;
1201
1202 if (!mflip_mips16_htab)
1203 mflip_mips16_htab = htab_create_ggc (37, mflip_mips16_htab_hash,
1204 mflip_mips16_htab_eq, NULL);
1205
1206 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1207 hash = htab_hash_string (name);
1208 slot = htab_find_slot_with_hash (mflip_mips16_htab, name, hash, INSERT);
1209 entry = (struct mflip_mips16_entry *) *slot;
1210 if (!entry)
1211 {
1212 mips16_flipper = !mips16_flipper;
1213 entry = ggc_alloc_mflip_mips16_entry ();
1214 entry->name = name;
1215 entry->mips16_p = mips16_flipper ? !base_is_mips16 : base_is_mips16;
1216 *slot = entry;
1217 }
1218 return entry->mips16_p;
1219 }
1220 \f
1221 /* Predicates to test for presence of "near" and "far"/"long_call"
1222 attributes on the given TYPE. */
1223
1224 static bool
1225 mips_near_type_p (const_tree type)
1226 {
1227 return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1228 }
1229
1230 static bool
1231 mips_far_type_p (const_tree type)
1232 {
1233 return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1234 || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1235 }
1236
1237
1238 /* Check if the interrupt attribute is set for a function. */
1239
1240 static bool
1241 mips_interrupt_type_p (tree type)
1242 {
1243 return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1244 }
1245
1246 /* Check if the attribute to use shadow register set is set for a function. */
1247
1248 static bool
1249 mips_use_shadow_register_set_p (tree type)
1250 {
1251 return lookup_attribute ("use_shadow_register_set",
1252 TYPE_ATTRIBUTES (type)) != NULL;
1253 }
1254
1255 /* Check if the attribute to keep interrupts masked is set for a function. */
1256
1257 static bool
1258 mips_keep_interrupts_masked_p (tree type)
1259 {
1260 return lookup_attribute ("keep_interrupts_masked",
1261 TYPE_ATTRIBUTES (type)) != NULL;
1262 }
1263
1264 /* Check if the attribute to use debug exception return is set for
1265 a function. */
1266
1267 static bool
1268 mips_use_debug_exception_return_p (tree type)
1269 {
1270 return lookup_attribute ("use_debug_exception_return",
1271 TYPE_ATTRIBUTES (type)) != NULL;
1272 }
1273
1274 /* Return the set of compression modes that are explicitly required
1275 by the attributes in ATTRIBUTES. */
1276
1277 static unsigned int
1278 mips_get_compress_on_flags (tree attributes)
1279 {
1280 unsigned int flags = 0;
1281
1282 if (lookup_attribute ("mips16", attributes) != NULL)
1283 flags |= MASK_MIPS16;
1284
1285 if (lookup_attribute ("micromips", attributes) != NULL)
1286 flags |= MASK_MICROMIPS;
1287
1288 return flags;
1289 }
1290
1291 /* Return the set of compression modes that are explicitly forbidden
1292 by the attributes in ATTRIBUTES. */
1293
1294 static unsigned int
1295 mips_get_compress_off_flags (tree attributes)
1296 {
1297 unsigned int flags = 0;
1298
1299 if (lookup_attribute ("nocompression", attributes) != NULL)
1300 flags |= MASK_MIPS16 | MASK_MICROMIPS;
1301
1302 if (lookup_attribute ("nomips16", attributes) != NULL)
1303 flags |= MASK_MIPS16;
1304
1305 if (lookup_attribute ("nomicromips", attributes) != NULL)
1306 flags |= MASK_MICROMIPS;
1307
1308 return flags;
1309 }
1310
1311 /* Return the compression mode that should be used for function DECL.
1312 Return the ambient setting if DECL is null. */
1313
1314 static unsigned int
1315 mips_get_compress_mode (tree decl)
1316 {
1317 unsigned int flags, force_on;
1318
1319 flags = mips_base_compression_flags;
1320 if (decl)
1321 {
1322 /* Nested functions must use the same frame pointer as their
1323 parent and must therefore use the same ISA mode. */
1324 tree parent = decl_function_context (decl);
1325 if (parent)
1326 decl = parent;
1327 force_on = mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1328 if (force_on)
1329 return force_on;
1330 flags &= ~mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1331 }
1332 return flags;
1333 }
1334
1335 /* Return the attribute name associated with MASK_MIPS16 and MASK_MICROMIPS
1336 flags FLAGS. */
1337
1338 static const char *
1339 mips_get_compress_on_name (unsigned int flags)
1340 {
1341 if (flags == MASK_MIPS16)
1342 return "mips16";
1343 return "micromips";
1344 }
1345
1346 /* Return the attribute name that forbids MASK_MIPS16 and MASK_MICROMIPS
1347 flags FLAGS. */
1348
1349 static const char *
1350 mips_get_compress_off_name (unsigned int flags)
1351 {
1352 if (flags == MASK_MIPS16)
1353 return "nomips16";
1354 if (flags == MASK_MICROMIPS)
1355 return "nomicromips";
1356 return "nocompression";
1357 }
1358
1359 /* Implement TARGET_COMP_TYPE_ATTRIBUTES. */
1360
1361 static int
1362 mips_comp_type_attributes (const_tree type1, const_tree type2)
1363 {
1364 /* Disallow mixed near/far attributes. */
1365 if (mips_far_type_p (type1) && mips_near_type_p (type2))
1366 return 0;
1367 if (mips_near_type_p (type1) && mips_far_type_p (type2))
1368 return 0;
1369 return 1;
1370 }
1371
1372 /* Implement TARGET_INSERT_ATTRIBUTES. */
1373
1374 static void
1375 mips_insert_attributes (tree decl, tree *attributes)
1376 {
1377 const char *name;
1378 unsigned int compression_flags, nocompression_flags;
1379
1380 /* Check for "mips16" and "nomips16" attributes. */
1381 compression_flags = mips_get_compress_on_flags (*attributes);
1382 nocompression_flags = mips_get_compress_off_flags (*attributes);
1383
1384 if (TREE_CODE (decl) != FUNCTION_DECL)
1385 {
1386 if (nocompression_flags)
1387 error ("%qs attribute only applies to functions",
1388 mips_get_compress_off_name (nocompression_flags));
1389
1390 if (compression_flags)
1391 error ("%qs attribute only applies to functions",
1392 mips_get_compress_on_name (nocompression_flags));
1393 }
1394 else
1395 {
1396 compression_flags |= mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1397 nocompression_flags |=
1398 mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1399
1400 if (compression_flags && nocompression_flags)
1401 error ("%qE cannot have both %qs and %qs attributes",
1402 DECL_NAME (decl), mips_get_compress_on_name (compression_flags),
1403 mips_get_compress_off_name (nocompression_flags));
1404
1405 if (compression_flags & MASK_MIPS16
1406 && compression_flags & MASK_MICROMIPS)
1407 error ("%qE cannot have both %qs and %qs attributes",
1408 DECL_NAME (decl), "mips16", "micromips");
1409
1410 if (TARGET_FLIP_MIPS16
1411 && !DECL_ARTIFICIAL (decl)
1412 && compression_flags == 0
1413 && nocompression_flags == 0)
1414 {
1415 /* Implement -mflip-mips16. If DECL has neither a "nomips16" nor a
1416 "mips16" attribute, arbitrarily pick one. We must pick the same
1417 setting for duplicate declarations of a function. */
1418 name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1419 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1420 name = "nomicromips";
1421 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1422 }
1423 }
1424 }
1425
1426 /* Implement TARGET_MERGE_DECL_ATTRIBUTES. */
1427
1428 static tree
1429 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1430 {
1431 unsigned int diff;
1432
1433 diff = (mips_get_compress_on_flags (DECL_ATTRIBUTES (olddecl))
1434 ^ mips_get_compress_on_flags (DECL_ATTRIBUTES (newdecl)));
1435 if (diff)
1436 error ("%qE redeclared with conflicting %qs attributes",
1437 DECL_NAME (newdecl), mips_get_compress_on_name (diff));
1438
1439 diff = (mips_get_compress_off_flags (DECL_ATTRIBUTES (olddecl))
1440 ^ mips_get_compress_off_flags (DECL_ATTRIBUTES (newdecl)));
1441 if (diff)
1442 error ("%qE redeclared with conflicting %qs attributes",
1443 DECL_NAME (newdecl), mips_get_compress_off_name (diff));
1444
1445 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1446 DECL_ATTRIBUTES (newdecl));
1447 }
1448
1449 /* Implement TARGET_CAN_INLINE_P. */
1450
1451 static bool
1452 mips_can_inline_p (tree caller, tree callee)
1453 {
1454 if (mips_get_compress_mode (callee) != mips_get_compress_mode (caller))
1455 return false;
1456 return default_target_can_inline_p (caller, callee);
1457 }
1458 \f
1459 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1460 and *OFFSET_PTR. Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise. */
1461
1462 static void
1463 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1464 {
1465 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1466 {
1467 *base_ptr = XEXP (x, 0);
1468 *offset_ptr = INTVAL (XEXP (x, 1));
1469 }
1470 else
1471 {
1472 *base_ptr = x;
1473 *offset_ptr = 0;
1474 }
1475 }
1476 \f
1477 static unsigned int mips_build_integer (struct mips_integer_op *,
1478 unsigned HOST_WIDE_INT);
1479
1480 /* A subroutine of mips_build_integer, with the same interface.
1481 Assume that the final action in the sequence should be a left shift. */
1482
1483 static unsigned int
1484 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1485 {
1486 unsigned int i, shift;
1487
1488 /* Shift VALUE right until its lowest bit is set. Shift arithmetically
1489 since signed numbers are easier to load than unsigned ones. */
1490 shift = 0;
1491 while ((value & 1) == 0)
1492 value /= 2, shift++;
1493
1494 i = mips_build_integer (codes, value);
1495 codes[i].code = ASHIFT;
1496 codes[i].value = shift;
1497 return i + 1;
1498 }
1499
1500 /* As for mips_build_shift, but assume that the final action will be
1501 an IOR or PLUS operation. */
1502
1503 static unsigned int
1504 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1505 {
1506 unsigned HOST_WIDE_INT high;
1507 unsigned int i;
1508
1509 high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1510 if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1511 {
1512 /* The constant is too complex to load with a simple LUI/ORI pair,
1513 so we want to give the recursive call as many trailing zeros as
1514 possible. In this case, we know bit 16 is set and that the
1515 low 16 bits form a negative number. If we subtract that number
1516 from VALUE, we will clear at least the lowest 17 bits, maybe more. */
1517 i = mips_build_integer (codes, CONST_HIGH_PART (value));
1518 codes[i].code = PLUS;
1519 codes[i].value = CONST_LOW_PART (value);
1520 }
1521 else
1522 {
1523 /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1524 bits gives a value with at least 17 trailing zeros. */
1525 i = mips_build_integer (codes, high);
1526 codes[i].code = IOR;
1527 codes[i].value = value & 0xffff;
1528 }
1529 return i + 1;
1530 }
1531
1532 /* Fill CODES with a sequence of rtl operations to load VALUE.
1533 Return the number of operations needed. */
1534
1535 static unsigned int
1536 mips_build_integer (struct mips_integer_op *codes,
1537 unsigned HOST_WIDE_INT value)
1538 {
1539 if (SMALL_OPERAND (value)
1540 || SMALL_OPERAND_UNSIGNED (value)
1541 || LUI_OPERAND (value))
1542 {
1543 /* The value can be loaded with a single instruction. */
1544 codes[0].code = UNKNOWN;
1545 codes[0].value = value;
1546 return 1;
1547 }
1548 else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1549 {
1550 /* Either the constant is a simple LUI/ORI combination or its
1551 lowest bit is set. We don't want to shift in this case. */
1552 return mips_build_lower (codes, value);
1553 }
1554 else if ((value & 0xffff) == 0)
1555 {
1556 /* The constant will need at least three actions. The lowest
1557 16 bits are clear, so the final action will be a shift. */
1558 return mips_build_shift (codes, value);
1559 }
1560 else
1561 {
1562 /* The final action could be a shift, add or inclusive OR.
1563 Rather than use a complex condition to select the best
1564 approach, try both mips_build_shift and mips_build_lower
1565 and pick the one that gives the shortest sequence.
1566 Note that this case is only used once per constant. */
1567 struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1568 unsigned int cost, alt_cost;
1569
1570 cost = mips_build_shift (codes, value);
1571 alt_cost = mips_build_lower (alt_codes, value);
1572 if (alt_cost < cost)
1573 {
1574 memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1575 cost = alt_cost;
1576 }
1577 return cost;
1578 }
1579 }
1580 \f
1581 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
1582
1583 static bool
1584 mips_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1585 {
1586 return mips_const_insns (x) > 0;
1587 }
1588 \f
1589 /* Return a SYMBOL_REF for a MIPS16 function called NAME. */
1590
1591 static rtx
1592 mips16_stub_function (const char *name)
1593 {
1594 rtx x;
1595
1596 x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
1597 SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
1598 return x;
1599 }
1600 \f
1601 /* Return true if symbols of type TYPE require a GOT access. */
1602
1603 static bool
1604 mips_got_symbol_type_p (enum mips_symbol_type type)
1605 {
1606 switch (type)
1607 {
1608 case SYMBOL_GOT_PAGE_OFST:
1609 case SYMBOL_GOT_DISP:
1610 return true;
1611
1612 default:
1613 return false;
1614 }
1615 }
1616
1617 /* Return true if X is a thread-local symbol. */
1618
1619 static bool
1620 mips_tls_symbol_p (rtx x)
1621 {
1622 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1623 }
1624
1625 /* Return true if SYMBOL_REF X is associated with a global symbol
1626 (in the STB_GLOBAL sense). */
1627
1628 static bool
1629 mips_global_symbol_p (const_rtx x)
1630 {
1631 const_tree decl = SYMBOL_REF_DECL (x);
1632
1633 if (!decl)
1634 return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1635
1636 /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1637 or weak symbols. Relocations in the object file will be against
1638 the target symbol, so it's that symbol's binding that matters here. */
1639 return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1640 }
1641
1642 /* Return true if function X is a libgcc MIPS16 stub function. */
1643
1644 static bool
1645 mips16_stub_function_p (const_rtx x)
1646 {
1647 return (GET_CODE (x) == SYMBOL_REF
1648 && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1649 }
1650
1651 /* Return true if function X is a locally-defined and locally-binding
1652 MIPS16 function. */
1653
1654 static bool
1655 mips16_local_function_p (const_rtx x)
1656 {
1657 return (GET_CODE (x) == SYMBOL_REF
1658 && SYMBOL_REF_LOCAL_P (x)
1659 && !SYMBOL_REF_EXTERNAL_P (x)
1660 && (mips_get_compress_mode (SYMBOL_REF_DECL (x)) & MASK_MIPS16));
1661 }
1662
1663 /* Return true if SYMBOL_REF X binds locally. */
1664
1665 static bool
1666 mips_symbol_binds_local_p (const_rtx x)
1667 {
1668 return (SYMBOL_REF_DECL (x)
1669 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1670 : SYMBOL_REF_LOCAL_P (x));
1671 }
1672
1673 /* Return true if rtx constants of mode MODE should be put into a small
1674 data section. */
1675
1676 static bool
1677 mips_rtx_constant_in_small_data_p (enum machine_mode mode)
1678 {
1679 return (!TARGET_EMBEDDED_DATA
1680 && TARGET_LOCAL_SDATA
1681 && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1682 }
1683
1684 /* Return true if X should not be moved directly into register $25.
1685 We need this because many versions of GAS will treat "la $25,foo" as
1686 part of a call sequence and so allow a global "foo" to be lazily bound. */
1687
1688 bool
1689 mips_dangerous_for_la25_p (rtx x)
1690 {
1691 return (!TARGET_EXPLICIT_RELOCS
1692 && TARGET_USE_GOT
1693 && GET_CODE (x) == SYMBOL_REF
1694 && mips_global_symbol_p (x));
1695 }
1696
1697 /* Return true if calls to X might need $25 to be valid on entry. */
1698
1699 bool
1700 mips_use_pic_fn_addr_reg_p (const_rtx x)
1701 {
1702 if (!TARGET_USE_PIC_FN_ADDR_REG)
1703 return false;
1704
1705 /* MIPS16 stub functions are guaranteed not to use $25. */
1706 if (mips16_stub_function_p (x))
1707 return false;
1708
1709 if (GET_CODE (x) == SYMBOL_REF)
1710 {
1711 /* If PLTs and copy relocations are available, the static linker
1712 will make sure that $25 is valid on entry to the target function. */
1713 if (TARGET_ABICALLS_PIC0)
1714 return false;
1715
1716 /* Locally-defined functions use absolute accesses to set up
1717 the global pointer. */
1718 if (TARGET_ABSOLUTE_ABICALLS
1719 && mips_symbol_binds_local_p (x)
1720 && !SYMBOL_REF_EXTERNAL_P (x))
1721 return false;
1722 }
1723
1724 return true;
1725 }
1726
1727 /* Return the method that should be used to access SYMBOL_REF or
1728 LABEL_REF X in context CONTEXT. */
1729
1730 static enum mips_symbol_type
1731 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1732 {
1733 if (TARGET_RTP_PIC)
1734 return SYMBOL_GOT_DISP;
1735
1736 if (GET_CODE (x) == LABEL_REF)
1737 {
1738 /* Only return SYMBOL_PC_RELATIVE if we are generating MIPS16
1739 code and if we know that the label is in the current function's
1740 text section. LABEL_REFs are used for jump tables as well as
1741 text labels, so we must check whether jump tables live in the
1742 text section. */
1743 if (TARGET_MIPS16_SHORT_JUMP_TABLES
1744 && !LABEL_REF_NONLOCAL_P (x))
1745 return SYMBOL_PC_RELATIVE;
1746
1747 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1748 return SYMBOL_GOT_PAGE_OFST;
1749
1750 return SYMBOL_ABSOLUTE;
1751 }
1752
1753 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1754
1755 if (SYMBOL_REF_TLS_MODEL (x))
1756 return SYMBOL_TLS;
1757
1758 if (CONSTANT_POOL_ADDRESS_P (x))
1759 {
1760 if (TARGET_MIPS16_TEXT_LOADS)
1761 return SYMBOL_PC_RELATIVE;
1762
1763 if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1764 return SYMBOL_PC_RELATIVE;
1765
1766 if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1767 return SYMBOL_GP_RELATIVE;
1768 }
1769
1770 /* Do not use small-data accesses for weak symbols; they may end up
1771 being zero. */
1772 if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1773 return SYMBOL_GP_RELATIVE;
1774
1775 /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1776 is in effect. */
1777 if (TARGET_ABICALLS_PIC2
1778 && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1779 {
1780 /* There are three cases to consider:
1781
1782 - o32 PIC (either with or without explicit relocs)
1783 - n32/n64 PIC without explicit relocs
1784 - n32/n64 PIC with explicit relocs
1785
1786 In the first case, both local and global accesses will use an
1787 R_MIPS_GOT16 relocation. We must correctly predict which of
1788 the two semantics (local or global) the assembler and linker
1789 will apply. The choice depends on the symbol's binding rather
1790 than its visibility.
1791
1792 In the second case, the assembler will not use R_MIPS_GOT16
1793 relocations, but it chooses between local and global accesses
1794 in the same way as for o32 PIC.
1795
1796 In the third case we have more freedom since both forms of
1797 access will work for any kind of symbol. However, there seems
1798 little point in doing things differently. */
1799 if (mips_global_symbol_p (x))
1800 return SYMBOL_GOT_DISP;
1801
1802 return SYMBOL_GOT_PAGE_OFST;
1803 }
1804
1805 return SYMBOL_ABSOLUTE;
1806 }
1807
1808 /* Classify the base of symbolic expression X, given that X appears in
1809 context CONTEXT. */
1810
1811 static enum mips_symbol_type
1812 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1813 {
1814 rtx offset;
1815
1816 split_const (x, &x, &offset);
1817 if (UNSPEC_ADDRESS_P (x))
1818 return UNSPEC_ADDRESS_TYPE (x);
1819
1820 return mips_classify_symbol (x, context);
1821 }
1822
1823 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1824 is the alignment in bytes of SYMBOL_REF X. */
1825
1826 static bool
1827 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
1828 {
1829 HOST_WIDE_INT align;
1830
1831 align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
1832 return IN_RANGE (offset, 0, align - 1);
1833 }
1834
1835 /* Return true if X is a symbolic constant that can be used in context
1836 CONTEXT. If it is, store the type of the symbol in *SYMBOL_TYPE. */
1837
1838 bool
1839 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
1840 enum mips_symbol_type *symbol_type)
1841 {
1842 rtx offset;
1843
1844 split_const (x, &x, &offset);
1845 if (UNSPEC_ADDRESS_P (x))
1846 {
1847 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1848 x = UNSPEC_ADDRESS (x);
1849 }
1850 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1851 {
1852 *symbol_type = mips_classify_symbol (x, context);
1853 if (*symbol_type == SYMBOL_TLS)
1854 return false;
1855 }
1856 else
1857 return false;
1858
1859 if (offset == const0_rtx)
1860 return true;
1861
1862 /* Check whether a nonzero offset is valid for the underlying
1863 relocations. */
1864 switch (*symbol_type)
1865 {
1866 case SYMBOL_ABSOLUTE:
1867 case SYMBOL_64_HIGH:
1868 case SYMBOL_64_MID:
1869 case SYMBOL_64_LOW:
1870 /* If the target has 64-bit pointers and the object file only
1871 supports 32-bit symbols, the values of those symbols will be
1872 sign-extended. In this case we can't allow an arbitrary offset
1873 in case the 32-bit value X + OFFSET has a different sign from X. */
1874 if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1875 return offset_within_block_p (x, INTVAL (offset));
1876
1877 /* In other cases the relocations can handle any offset. */
1878 return true;
1879
1880 case SYMBOL_PC_RELATIVE:
1881 /* Allow constant pool references to be converted to LABEL+CONSTANT.
1882 In this case, we no longer have access to the underlying constant,
1883 but the original symbol-based access was known to be valid. */
1884 if (GET_CODE (x) == LABEL_REF)
1885 return true;
1886
1887 /* Fall through. */
1888
1889 case SYMBOL_GP_RELATIVE:
1890 /* Make sure that the offset refers to something within the
1891 same object block. This should guarantee that the final
1892 PC- or GP-relative offset is within the 16-bit limit. */
1893 return offset_within_block_p (x, INTVAL (offset));
1894
1895 case SYMBOL_GOT_PAGE_OFST:
1896 case SYMBOL_GOTOFF_PAGE:
1897 /* If the symbol is global, the GOT entry will contain the symbol's
1898 address, and we will apply a 16-bit offset after loading it.
1899 If the symbol is local, the linker should provide enough local
1900 GOT entries for a 16-bit offset, but larger offsets may lead
1901 to GOT overflow. */
1902 return SMALL_INT (offset);
1903
1904 case SYMBOL_TPREL:
1905 case SYMBOL_DTPREL:
1906 /* There is no carry between the HI and LO REL relocations, so the
1907 offset is only valid if we know it won't lead to such a carry. */
1908 return mips_offset_within_alignment_p (x, INTVAL (offset));
1909
1910 case SYMBOL_GOT_DISP:
1911 case SYMBOL_GOTOFF_DISP:
1912 case SYMBOL_GOTOFF_CALL:
1913 case SYMBOL_GOTOFF_LOADGP:
1914 case SYMBOL_TLSGD:
1915 case SYMBOL_TLSLDM:
1916 case SYMBOL_GOTTPREL:
1917 case SYMBOL_TLS:
1918 case SYMBOL_HALF:
1919 return false;
1920 }
1921 gcc_unreachable ();
1922 }
1923 \f
1924 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
1925 single instruction. We rely on the fact that, in the worst case,
1926 all instructions involved in a MIPS16 address calculation are usually
1927 extended ones. */
1928
1929 static int
1930 mips_symbol_insns_1 (enum mips_symbol_type type, enum machine_mode mode)
1931 {
1932 if (mips_use_pcrel_pool_p[(int) type])
1933 {
1934 if (mode == MAX_MACHINE_MODE)
1935 /* LEAs will be converted into constant-pool references by
1936 mips_reorg. */
1937 type = SYMBOL_PC_RELATIVE;
1938 else
1939 /* The constant must be loaded and then dereferenced. */
1940 return 0;
1941 }
1942
1943 switch (type)
1944 {
1945 case SYMBOL_ABSOLUTE:
1946 /* When using 64-bit symbols, we need 5 preparatory instructions,
1947 such as:
1948
1949 lui $at,%highest(symbol)
1950 daddiu $at,$at,%higher(symbol)
1951 dsll $at,$at,16
1952 daddiu $at,$at,%hi(symbol)
1953 dsll $at,$at,16
1954
1955 The final address is then $at + %lo(symbol). With 32-bit
1956 symbols we just need a preparatory LUI for normal mode and
1957 a preparatory LI and SLL for MIPS16. */
1958 return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
1959
1960 case SYMBOL_GP_RELATIVE:
1961 /* Treat GP-relative accesses as taking a single instruction on
1962 MIPS16 too; the copy of $gp can often be shared. */
1963 return 1;
1964
1965 case SYMBOL_PC_RELATIVE:
1966 /* PC-relative constants can be only be used with ADDIUPC,
1967 DADDIUPC, LWPC and LDPC. */
1968 if (mode == MAX_MACHINE_MODE
1969 || GET_MODE_SIZE (mode) == 4
1970 || GET_MODE_SIZE (mode) == 8)
1971 return 1;
1972
1973 /* The constant must be loaded using ADDIUPC or DADDIUPC first. */
1974 return 0;
1975
1976 case SYMBOL_GOT_DISP:
1977 /* The constant will have to be loaded from the GOT before it
1978 is used in an address. */
1979 if (mode != MAX_MACHINE_MODE)
1980 return 0;
1981
1982 /* Fall through. */
1983
1984 case SYMBOL_GOT_PAGE_OFST:
1985 /* Unless -funit-at-a-time is in effect, we can't be sure whether the
1986 local/global classification is accurate. The worst cases are:
1987
1988 (1) For local symbols when generating o32 or o64 code. The assembler
1989 will use:
1990
1991 lw $at,%got(symbol)
1992 nop
1993
1994 ...and the final address will be $at + %lo(symbol).
1995
1996 (2) For global symbols when -mxgot. The assembler will use:
1997
1998 lui $at,%got_hi(symbol)
1999 (d)addu $at,$at,$gp
2000
2001 ...and the final address will be $at + %got_lo(symbol). */
2002 return 3;
2003
2004 case SYMBOL_GOTOFF_PAGE:
2005 case SYMBOL_GOTOFF_DISP:
2006 case SYMBOL_GOTOFF_CALL:
2007 case SYMBOL_GOTOFF_LOADGP:
2008 case SYMBOL_64_HIGH:
2009 case SYMBOL_64_MID:
2010 case SYMBOL_64_LOW:
2011 case SYMBOL_TLSGD:
2012 case SYMBOL_TLSLDM:
2013 case SYMBOL_DTPREL:
2014 case SYMBOL_GOTTPREL:
2015 case SYMBOL_TPREL:
2016 case SYMBOL_HALF:
2017 /* A 16-bit constant formed by a single relocation, or a 32-bit
2018 constant formed from a high 16-bit relocation and a low 16-bit
2019 relocation. Use mips_split_p to determine which. 32-bit
2020 constants need an "lui; addiu" sequence for normal mode and
2021 an "li; sll; addiu" sequence for MIPS16 mode. */
2022 return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
2023
2024 case SYMBOL_TLS:
2025 /* We don't treat a bare TLS symbol as a constant. */
2026 return 0;
2027 }
2028 gcc_unreachable ();
2029 }
2030
2031 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
2032 to load symbols of type TYPE into a register. Return 0 if the given
2033 type of symbol cannot be used as an immediate operand.
2034
2035 Otherwise, return the number of instructions needed to load or store
2036 values of mode MODE to or from addresses of type TYPE. Return 0 if
2037 the given type of symbol is not valid in addresses.
2038
2039 In both cases, instruction counts are based off BASE_INSN_LENGTH. */
2040
2041 static int
2042 mips_symbol_insns (enum mips_symbol_type type, enum machine_mode mode)
2043 {
2044 return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
2045 }
2046 \f
2047 /* A for_each_rtx callback. Stop the search if *X references a
2048 thread-local symbol. */
2049
2050 static int
2051 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
2052 {
2053 return mips_tls_symbol_p (*x);
2054 }
2055
2056 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
2057
2058 static bool
2059 mips_cannot_force_const_mem (enum machine_mode mode, rtx x)
2060 {
2061 enum mips_symbol_type type;
2062 rtx base, offset;
2063
2064 /* There is no assembler syntax for expressing an address-sized
2065 high part. */
2066 if (GET_CODE (x) == HIGH)
2067 return true;
2068
2069 /* As an optimization, reject constants that mips_legitimize_move
2070 can expand inline.
2071
2072 Suppose we have a multi-instruction sequence that loads constant C
2073 into register R. If R does not get allocated a hard register, and
2074 R is used in an operand that allows both registers and memory
2075 references, reload will consider forcing C into memory and using
2076 one of the instruction's memory alternatives. Returning false
2077 here will force it to use an input reload instead. */
2078 if (CONST_INT_P (x) && mips_legitimate_constant_p (mode, x))
2079 return true;
2080
2081 split_const (x, &base, &offset);
2082 if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type))
2083 {
2084 /* See whether we explicitly want these symbols in the pool. */
2085 if (mips_use_pcrel_pool_p[(int) type])
2086 return false;
2087
2088 /* The same optimization as for CONST_INT. */
2089 if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
2090 return true;
2091
2092 /* If MIPS16 constant pools live in the text section, they should
2093 not refer to anything that might need run-time relocation. */
2094 if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
2095 return true;
2096 }
2097
2098 /* TLS symbols must be computed by mips_legitimize_move. */
2099 if (for_each_rtx (&x, &mips_tls_symbol_ref_1, NULL))
2100 return true;
2101
2102 return false;
2103 }
2104
2105 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. We can't use blocks for
2106 constants when we're using a per-function constant pool. */
2107
2108 static bool
2109 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2110 const_rtx x ATTRIBUTE_UNUSED)
2111 {
2112 return !TARGET_MIPS16_PCREL_LOADS;
2113 }
2114 \f
2115 /* Return true if register REGNO is a valid base register for mode MODE.
2116 STRICT_P is true if REG_OK_STRICT is in effect. */
2117
2118 int
2119 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode,
2120 bool strict_p)
2121 {
2122 if (!HARD_REGISTER_NUM_P (regno))
2123 {
2124 if (!strict_p)
2125 return true;
2126 regno = reg_renumber[regno];
2127 }
2128
2129 /* These fake registers will be eliminated to either the stack or
2130 hard frame pointer, both of which are usually valid base registers.
2131 Reload deals with the cases where the eliminated form isn't valid. */
2132 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2133 return true;
2134
2135 /* In MIPS16 mode, the stack pointer can only address word and doubleword
2136 values, nothing smaller. There are two problems here:
2137
2138 (a) Instantiating virtual registers can introduce new uses of the
2139 stack pointer. If these virtual registers are valid addresses,
2140 the stack pointer should be too.
2141
2142 (b) Most uses of the stack pointer are not made explicit until
2143 FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
2144 We don't know until that stage whether we'll be eliminating to the
2145 stack pointer (which needs the restriction) or the hard frame
2146 pointer (which doesn't).
2147
2148 All in all, it seems more consistent to only enforce this restriction
2149 during and after reload. */
2150 if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2151 return !strict_p || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2152
2153 return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2154 }
2155
2156 /* Return true if X is a valid base register for mode MODE.
2157 STRICT_P is true if REG_OK_STRICT is in effect. */
2158
2159 static bool
2160 mips_valid_base_register_p (rtx x, enum machine_mode mode, bool strict_p)
2161 {
2162 if (!strict_p && GET_CODE (x) == SUBREG)
2163 x = SUBREG_REG (x);
2164
2165 return (REG_P (x)
2166 && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2167 }
2168
2169 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2170 can address a value of mode MODE. */
2171
2172 static bool
2173 mips_valid_offset_p (rtx x, enum machine_mode mode)
2174 {
2175 /* Check that X is a signed 16-bit number. */
2176 if (!const_arith_operand (x, Pmode))
2177 return false;
2178
2179 /* We may need to split multiword moves, so make sure that every word
2180 is accessible. */
2181 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2182 && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2183 return false;
2184
2185 return true;
2186 }
2187
2188 /* Return true if a LO_SUM can address a value of mode MODE when the
2189 LO_SUM symbol has type SYMBOL_TYPE. */
2190
2191 static bool
2192 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, enum machine_mode mode)
2193 {
2194 /* Check that symbols of type SYMBOL_TYPE can be used to access values
2195 of mode MODE. */
2196 if (mips_symbol_insns (symbol_type, mode) == 0)
2197 return false;
2198
2199 /* Check that there is a known low-part relocation. */
2200 if (mips_lo_relocs[symbol_type] == NULL)
2201 return false;
2202
2203 /* We may need to split multiword moves, so make sure that each word
2204 can be accessed without inducing a carry. This is mainly needed
2205 for o64, which has historically only guaranteed 64-bit alignment
2206 for 128-bit types. */
2207 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2208 && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2209 return false;
2210
2211 return true;
2212 }
2213
2214 /* Return true if X is a valid address for machine mode MODE. If it is,
2215 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
2216 effect. */
2217
2218 static bool
2219 mips_classify_address (struct mips_address_info *info, rtx x,
2220 enum machine_mode mode, bool strict_p)
2221 {
2222 switch (GET_CODE (x))
2223 {
2224 case REG:
2225 case SUBREG:
2226 info->type = ADDRESS_REG;
2227 info->reg = x;
2228 info->offset = const0_rtx;
2229 return mips_valid_base_register_p (info->reg, mode, strict_p);
2230
2231 case PLUS:
2232 info->type = ADDRESS_REG;
2233 info->reg = XEXP (x, 0);
2234 info->offset = XEXP (x, 1);
2235 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2236 && mips_valid_offset_p (info->offset, mode));
2237
2238 case LO_SUM:
2239 info->type = ADDRESS_LO_SUM;
2240 info->reg = XEXP (x, 0);
2241 info->offset = XEXP (x, 1);
2242 /* We have to trust the creator of the LO_SUM to do something vaguely
2243 sane. Target-independent code that creates a LO_SUM should also
2244 create and verify the matching HIGH. Target-independent code that
2245 adds an offset to a LO_SUM must prove that the offset will not
2246 induce a carry. Failure to do either of these things would be
2247 a bug, and we are not required to check for it here. The MIPS
2248 backend itself should only create LO_SUMs for valid symbolic
2249 constants, with the high part being either a HIGH or a copy
2250 of _gp. */
2251 info->symbol_type
2252 = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2253 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2254 && mips_valid_lo_sum_p (info->symbol_type, mode));
2255
2256 case CONST_INT:
2257 /* Small-integer addresses don't occur very often, but they
2258 are legitimate if $0 is a valid base register. */
2259 info->type = ADDRESS_CONST_INT;
2260 return !TARGET_MIPS16 && SMALL_INT (x);
2261
2262 case CONST:
2263 case LABEL_REF:
2264 case SYMBOL_REF:
2265 info->type = ADDRESS_SYMBOLIC;
2266 return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2267 &info->symbol_type)
2268 && mips_symbol_insns (info->symbol_type, mode) > 0
2269 && !mips_split_p[info->symbol_type]);
2270
2271 default:
2272 return false;
2273 }
2274 }
2275
2276 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
2277
2278 static bool
2279 mips_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
2280 {
2281 struct mips_address_info addr;
2282
2283 return mips_classify_address (&addr, x, mode, strict_p);
2284 }
2285
2286 /* Return true if X is a legitimate $sp-based address for mode MDOE. */
2287
2288 bool
2289 mips_stack_address_p (rtx x, enum machine_mode mode)
2290 {
2291 struct mips_address_info addr;
2292
2293 return (mips_classify_address (&addr, x, mode, false)
2294 && addr.type == ADDRESS_REG
2295 && addr.reg == stack_pointer_rtx);
2296 }
2297
2298 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2299 address instruction. Note that such addresses are not considered
2300 legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2301 is so restricted. */
2302
2303 static bool
2304 mips_lwxs_address_p (rtx addr)
2305 {
2306 if (ISA_HAS_LWXS
2307 && GET_CODE (addr) == PLUS
2308 && REG_P (XEXP (addr, 1)))
2309 {
2310 rtx offset = XEXP (addr, 0);
2311 if (GET_CODE (offset) == MULT
2312 && REG_P (XEXP (offset, 0))
2313 && CONST_INT_P (XEXP (offset, 1))
2314 && INTVAL (XEXP (offset, 1)) == 4)
2315 return true;
2316 }
2317 return false;
2318 }
2319
2320 /* Return true if ADDR matches the pattern for the L{B,H,W,D}{,U}X load
2321 indexed address instruction. Note that such addresses are
2322 not considered legitimate in the TARGET_LEGITIMATE_ADDRESS_P
2323 sense, because their use is so restricted. */
2324
2325 static bool
2326 mips_lx_address_p (rtx addr, enum machine_mode mode)
2327 {
2328 if (GET_CODE (addr) != PLUS
2329 || !REG_P (XEXP (addr, 0))
2330 || !REG_P (XEXP (addr, 1)))
2331 return false;
2332 if (ISA_HAS_LBX && mode == QImode)
2333 return true;
2334 if (ISA_HAS_LHX && mode == HImode)
2335 return true;
2336 if (ISA_HAS_LWX && mode == SImode)
2337 return true;
2338 if (ISA_HAS_LDX && mode == DImode)
2339 return true;
2340 return false;
2341 }
2342 \f
2343 /* Return true if a value at OFFSET bytes from base register BASE can be
2344 accessed using an unextended MIPS16 instruction. MODE is the mode of
2345 the value.
2346
2347 Usually the offset in an unextended instruction is a 5-bit field.
2348 The offset is unsigned and shifted left once for LH and SH, twice
2349 for LW and SW, and so on. An exception is LWSP and SWSP, which have
2350 an 8-bit immediate field that's shifted left twice. */
2351
2352 static bool
2353 mips16_unextended_reference_p (enum machine_mode mode, rtx base,
2354 unsigned HOST_WIDE_INT offset)
2355 {
2356 if (mode != BLKmode && offset % GET_MODE_SIZE (mode) == 0)
2357 {
2358 if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2359 return offset < 256U * GET_MODE_SIZE (mode);
2360 return offset < 32U * GET_MODE_SIZE (mode);
2361 }
2362 return false;
2363 }
2364
2365 /* Return the number of instructions needed to load or store a value
2366 of mode MODE at address X, assuming that BASE_INSN_LENGTH is the
2367 length of one instruction. Return 0 if X isn't valid for MODE.
2368 Assume that multiword moves may need to be split into word moves
2369 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2370 enough. */
2371
2372 int
2373 mips_address_insns (rtx x, enum machine_mode mode, bool might_split_p)
2374 {
2375 struct mips_address_info addr;
2376 int factor;
2377
2378 /* BLKmode is used for single unaligned loads and stores and should
2379 not count as a multiword mode. (GET_MODE_SIZE (BLKmode) is pretty
2380 meaningless, so we have to single it out as a special case one way
2381 or the other.) */
2382 if (mode != BLKmode && might_split_p)
2383 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2384 else
2385 factor = 1;
2386
2387 if (mips_classify_address (&addr, x, mode, false))
2388 switch (addr.type)
2389 {
2390 case ADDRESS_REG:
2391 if (TARGET_MIPS16
2392 && !mips16_unextended_reference_p (mode, addr.reg,
2393 UINTVAL (addr.offset)))
2394 return factor * 2;
2395 return factor;
2396
2397 case ADDRESS_LO_SUM:
2398 return TARGET_MIPS16 ? factor * 2 : factor;
2399
2400 case ADDRESS_CONST_INT:
2401 return factor;
2402
2403 case ADDRESS_SYMBOLIC:
2404 return factor * mips_symbol_insns (addr.symbol_type, mode);
2405 }
2406 return 0;
2407 }
2408
2409 /* Return true if X fits within an unsigned field of BITS bits that is
2410 shifted left SHIFT bits before being used. */
2411
2412 bool
2413 mips_unsigned_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2414 {
2415 return (x & ((1 << shift) - 1)) == 0 && x < ((unsigned) 1 << (shift + bits));
2416 }
2417
2418 /* Return true if X fits within a signed field of BITS bits that is
2419 shifted left SHIFT bits before being used. */
2420
2421 bool
2422 mips_signed_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2423 {
2424 x += 1 << (bits + shift - 1);
2425 return mips_unsigned_immediate_p (x, bits, shift);
2426 }
2427
2428 /* Return true if X is legitimate for accessing values of mode MODE,
2429 if it is based on a MIPS16 register, and if the offset satisfies
2430 OFFSET_PREDICATE. */
2431
2432 bool
2433 m16_based_address_p (rtx x, enum machine_mode mode,
2434 insn_operand_predicate_fn offset_predicate)
2435 {
2436 struct mips_address_info addr;
2437
2438 return (mips_classify_address (&addr, x, mode, false)
2439 && addr.type == ADDRESS_REG
2440 && M16_REG_P (REGNO (addr.reg))
2441 && offset_predicate (addr.offset, mode));
2442 }
2443
2444 /* Return true if X is a legitimate address that conforms to the requirements
2445 for a microMIPS LWSP or SWSP insn. */
2446
2447 bool
2448 lwsp_swsp_address_p (rtx x, enum machine_mode mode)
2449 {
2450 struct mips_address_info addr;
2451
2452 return (mips_classify_address (&addr, x, mode, false)
2453 && addr.type == ADDRESS_REG
2454 && REGNO (addr.reg) == STACK_POINTER_REGNUM
2455 && uw5_operand (addr.offset, mode));
2456 }
2457
2458 /* Return true if X is a legitimate address with a 12-bit offset.
2459 MODE is the mode of the value being accessed. */
2460
2461 bool
2462 umips_12bit_offset_address_p (rtx x, enum machine_mode mode)
2463 {
2464 struct mips_address_info addr;
2465
2466 return (mips_classify_address (&addr, x, mode, false)
2467 && addr.type == ADDRESS_REG
2468 && CONST_INT_P (addr.offset)
2469 && UMIPS_12BIT_OFFSET_P (INTVAL (addr.offset)));
2470 }
2471
2472 /* Return the number of instructions needed to load constant X,
2473 assuming that BASE_INSN_LENGTH is the length of one instruction.
2474 Return 0 if X isn't a valid constant. */
2475
2476 int
2477 mips_const_insns (rtx x)
2478 {
2479 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2480 enum mips_symbol_type symbol_type;
2481 rtx offset;
2482
2483 switch (GET_CODE (x))
2484 {
2485 case HIGH:
2486 if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2487 &symbol_type)
2488 || !mips_split_p[symbol_type])
2489 return 0;
2490
2491 /* This is simply an LUI for normal mode. It is an extended
2492 LI followed by an extended SLL for MIPS16. */
2493 return TARGET_MIPS16 ? 4 : 1;
2494
2495 case CONST_INT:
2496 if (TARGET_MIPS16)
2497 /* Unsigned 8-bit constants can be loaded using an unextended
2498 LI instruction. Unsigned 16-bit constants can be loaded
2499 using an extended LI. Negative constants must be loaded
2500 using LI and then negated. */
2501 return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2502 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2503 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
2504 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2505 : 0);
2506
2507 return mips_build_integer (codes, INTVAL (x));
2508
2509 case CONST_DOUBLE:
2510 case CONST_VECTOR:
2511 /* Allow zeros for normal mode, where we can use $0. */
2512 return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2513
2514 case CONST:
2515 if (CONST_GP_P (x))
2516 return 1;
2517
2518 /* See if we can refer to X directly. */
2519 if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2520 return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2521
2522 /* Otherwise try splitting the constant into a base and offset.
2523 If the offset is a 16-bit value, we can load the base address
2524 into a register and then use (D)ADDIU to add in the offset.
2525 If the offset is larger, we can load the base and offset
2526 into separate registers and add them together with (D)ADDU.
2527 However, the latter is only possible before reload; during
2528 and after reload, we must have the option of forcing the
2529 constant into the pool instead. */
2530 split_const (x, &x, &offset);
2531 if (offset != 0)
2532 {
2533 int n = mips_const_insns (x);
2534 if (n != 0)
2535 {
2536 if (SMALL_INT (offset))
2537 return n + 1;
2538 else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
2539 return n + 1 + mips_build_integer (codes, INTVAL (offset));
2540 }
2541 }
2542 return 0;
2543
2544 case SYMBOL_REF:
2545 case LABEL_REF:
2546 return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2547 MAX_MACHINE_MODE);
2548
2549 default:
2550 return 0;
2551 }
2552 }
2553
2554 /* X is a doubleword constant that can be handled by splitting it into
2555 two words and loading each word separately. Return the number of
2556 instructions required to do this, assuming that BASE_INSN_LENGTH
2557 is the length of one instruction. */
2558
2559 int
2560 mips_split_const_insns (rtx x)
2561 {
2562 unsigned int low, high;
2563
2564 low = mips_const_insns (mips_subword (x, false));
2565 high = mips_const_insns (mips_subword (x, true));
2566 gcc_assert (low > 0 && high > 0);
2567 return low + high;
2568 }
2569
2570 /* Return the number of instructions needed to implement INSN,
2571 given that it loads from or stores to MEM. Assume that
2572 BASE_INSN_LENGTH is the length of one instruction. */
2573
2574 int
2575 mips_load_store_insns (rtx mem, rtx insn)
2576 {
2577 enum machine_mode mode;
2578 bool might_split_p;
2579 rtx set;
2580
2581 gcc_assert (MEM_P (mem));
2582 mode = GET_MODE (mem);
2583
2584 /* Try to prove that INSN does not need to be split. */
2585 might_split_p = GET_MODE_SIZE (mode) > UNITS_PER_WORD;
2586 if (might_split_p)
2587 {
2588 set = single_set (insn);
2589 if (set && !mips_split_move_insn_p (SET_DEST (set), SET_SRC (set), insn))
2590 might_split_p = false;
2591 }
2592
2593 return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2594 }
2595
2596 /* Return the number of instructions needed for an integer division,
2597 assuming that BASE_INSN_LENGTH is the length of one instruction. */
2598
2599 int
2600 mips_idiv_insns (void)
2601 {
2602 int count;
2603
2604 count = 1;
2605 if (TARGET_CHECK_ZERO_DIV)
2606 {
2607 if (GENERATE_DIVIDE_TRAPS)
2608 count++;
2609 else
2610 count += 2;
2611 }
2612
2613 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2614 count++;
2615 return count;
2616 }
2617 \f
2618 /* Emit a move from SRC to DEST. Assume that the move expanders can
2619 handle all moves if !can_create_pseudo_p (). The distinction is
2620 important because, unlike emit_move_insn, the move expanders know
2621 how to force Pmode objects into the constant pool even when the
2622 constant pool address is not itself legitimate. */
2623
2624 rtx
2625 mips_emit_move (rtx dest, rtx src)
2626 {
2627 return (can_create_pseudo_p ()
2628 ? emit_move_insn (dest, src)
2629 : emit_move_insn_1 (dest, src));
2630 }
2631
2632 /* Emit a move from SRC to DEST, splitting compound moves into individual
2633 instructions. SPLIT_TYPE is the type of split to perform. */
2634
2635 static void
2636 mips_emit_move_or_split (rtx dest, rtx src, enum mips_split_type split_type)
2637 {
2638 if (mips_split_move_p (dest, src, split_type))
2639 mips_split_move (dest, src, split_type);
2640 else
2641 mips_emit_move (dest, src);
2642 }
2643
2644 /* Emit an instruction of the form (set TARGET (CODE OP0)). */
2645
2646 static void
2647 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
2648 {
2649 emit_insn (gen_rtx_SET (VOIDmode, target,
2650 gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
2651 }
2652
2653 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
2654 Return that new register. */
2655
2656 static rtx
2657 mips_force_unary (enum machine_mode mode, enum rtx_code code, rtx op0)
2658 {
2659 rtx reg;
2660
2661 reg = gen_reg_rtx (mode);
2662 mips_emit_unary (code, reg, op0);
2663 return reg;
2664 }
2665
2666 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)). */
2667
2668 void
2669 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2670 {
2671 emit_insn (gen_rtx_SET (VOIDmode, target,
2672 gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2673 }
2674
2675 /* Compute (CODE OP0 OP1) and store the result in a new register
2676 of mode MODE. Return that new register. */
2677
2678 static rtx
2679 mips_force_binary (enum machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2680 {
2681 rtx reg;
2682
2683 reg = gen_reg_rtx (mode);
2684 mips_emit_binary (code, reg, op0, op1);
2685 return reg;
2686 }
2687
2688 /* Copy VALUE to a register and return that register. If new pseudos
2689 are allowed, copy it into a new register, otherwise use DEST. */
2690
2691 static rtx
2692 mips_force_temporary (rtx dest, rtx value)
2693 {
2694 if (can_create_pseudo_p ())
2695 return force_reg (Pmode, value);
2696 else
2697 {
2698 mips_emit_move (dest, value);
2699 return dest;
2700 }
2701 }
2702
2703 /* Emit a call sequence with call pattern PATTERN and return the call
2704 instruction itself (which is not necessarily the last instruction
2705 emitted). ORIG_ADDR is the original, unlegitimized address,
2706 ADDR is the legitimized form, and LAZY_P is true if the call
2707 address is lazily-bound. */
2708
2709 static rtx
2710 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
2711 {
2712 rtx insn, reg;
2713
2714 insn = emit_call_insn (pattern);
2715
2716 if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
2717 {
2718 /* MIPS16 JALRs only take MIPS16 registers. If the target
2719 function requires $25 to be valid on entry, we must copy it
2720 there separately. The move instruction can be put in the
2721 call's delay slot. */
2722 reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
2723 emit_insn_before (gen_move_insn (reg, addr), insn);
2724 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
2725 }
2726
2727 if (lazy_p)
2728 /* Lazy-binding stubs require $gp to be valid on entry. */
2729 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2730
2731 if (TARGET_USE_GOT)
2732 {
2733 /* See the comment above load_call<mode> for details. */
2734 use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2735 gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2736 emit_insn (gen_update_got_version ());
2737 }
2738 return insn;
2739 }
2740 \f
2741 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2742 then add CONST_INT OFFSET to the result. */
2743
2744 static rtx
2745 mips_unspec_address_offset (rtx base, rtx offset,
2746 enum mips_symbol_type symbol_type)
2747 {
2748 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2749 UNSPEC_ADDRESS_FIRST + symbol_type);
2750 if (offset != const0_rtx)
2751 base = gen_rtx_PLUS (Pmode, base, offset);
2752 return gen_rtx_CONST (Pmode, base);
2753 }
2754
2755 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2756 type SYMBOL_TYPE. */
2757
2758 rtx
2759 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2760 {
2761 rtx base, offset;
2762
2763 split_const (address, &base, &offset);
2764 return mips_unspec_address_offset (base, offset, symbol_type);
2765 }
2766
2767 /* If OP is an UNSPEC address, return the address to which it refers,
2768 otherwise return OP itself. */
2769
2770 rtx
2771 mips_strip_unspec_address (rtx op)
2772 {
2773 rtx base, offset;
2774
2775 split_const (op, &base, &offset);
2776 if (UNSPEC_ADDRESS_P (base))
2777 op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
2778 return op;
2779 }
2780
2781 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2782 high part to BASE and return the result. Just return BASE otherwise.
2783 TEMP is as for mips_force_temporary.
2784
2785 The returned expression can be used as the first operand to a LO_SUM. */
2786
2787 static rtx
2788 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2789 enum mips_symbol_type symbol_type)
2790 {
2791 if (mips_split_p[symbol_type])
2792 {
2793 addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2794 addr = mips_force_temporary (temp, addr);
2795 base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2796 }
2797 return base;
2798 }
2799 \f
2800 /* Return an instruction that copies $gp into register REG. We want
2801 GCC to treat the register's value as constant, so that its value
2802 can be rematerialized on demand. */
2803
2804 static rtx
2805 gen_load_const_gp (rtx reg)
2806 {
2807 return PMODE_INSN (gen_load_const_gp, (reg));
2808 }
2809
2810 /* Return a pseudo register that contains the value of $gp throughout
2811 the current function. Such registers are needed by MIPS16 functions,
2812 for which $gp itself is not a valid base register or addition operand. */
2813
2814 static rtx
2815 mips16_gp_pseudo_reg (void)
2816 {
2817 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2818 {
2819 rtx insn, scan;
2820
2821 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2822
2823 push_topmost_sequence ();
2824
2825 scan = get_insns ();
2826 while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
2827 scan = NEXT_INSN (scan);
2828
2829 insn = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
2830 insn = emit_insn_after (insn, scan);
2831 INSN_LOCATION (insn) = 0;
2832
2833 pop_topmost_sequence ();
2834 }
2835
2836 return cfun->machine->mips16_gp_pseudo_rtx;
2837 }
2838
2839 /* Return a base register that holds pic_offset_table_rtx.
2840 TEMP, if nonnull, is a scratch Pmode base register. */
2841
2842 rtx
2843 mips_pic_base_register (rtx temp)
2844 {
2845 if (!TARGET_MIPS16)
2846 return pic_offset_table_rtx;
2847
2848 if (currently_expanding_to_rtl)
2849 return mips16_gp_pseudo_reg ();
2850
2851 if (can_create_pseudo_p ())
2852 temp = gen_reg_rtx (Pmode);
2853
2854 if (TARGET_USE_GOT)
2855 /* The first post-reload split exposes all references to $gp
2856 (both uses and definitions). All references must remain
2857 explicit after that point.
2858
2859 It is safe to introduce uses of $gp at any time, so for
2860 simplicity, we do that before the split too. */
2861 mips_emit_move (temp, pic_offset_table_rtx);
2862 else
2863 emit_insn (gen_load_const_gp (temp));
2864 return temp;
2865 }
2866
2867 /* Return the RHS of a load_call<mode> insn. */
2868
2869 static rtx
2870 mips_unspec_call (rtx reg, rtx symbol)
2871 {
2872 rtvec vec;
2873
2874 vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
2875 return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
2876 }
2877
2878 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
2879 reference. Return NULL_RTX otherwise. */
2880
2881 static rtx
2882 mips_strip_unspec_call (rtx src)
2883 {
2884 if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
2885 return mips_strip_unspec_address (XVECEXP (src, 0, 1));
2886 return NULL_RTX;
2887 }
2888
2889 /* Create and return a GOT reference of type TYPE for address ADDR.
2890 TEMP, if nonnull, is a scratch Pmode base register. */
2891
2892 rtx
2893 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
2894 {
2895 rtx base, high, lo_sum_symbol;
2896
2897 base = mips_pic_base_register (temp);
2898
2899 /* If we used the temporary register to load $gp, we can't use
2900 it for the high part as well. */
2901 if (temp != NULL && reg_overlap_mentioned_p (base, temp))
2902 temp = NULL;
2903
2904 high = mips_unspec_offset_high (temp, base, addr, type);
2905 lo_sum_symbol = mips_unspec_address (addr, type);
2906
2907 if (type == SYMBOL_GOTOFF_CALL)
2908 return mips_unspec_call (high, lo_sum_symbol);
2909 else
2910 return PMODE_INSN (gen_unspec_got, (high, lo_sum_symbol));
2911 }
2912
2913 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
2914 it appears in a MEM of that mode. Return true if ADDR is a legitimate
2915 constant in that context and can be split into high and low parts.
2916 If so, and if LOW_OUT is nonnull, emit the high part and store the
2917 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
2918
2919 TEMP is as for mips_force_temporary and is used to load the high
2920 part into a register.
2921
2922 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
2923 a legitimize SET_SRC for an .md pattern, otherwise the low part
2924 is guaranteed to be a legitimate address for mode MODE. */
2925
2926 bool
2927 mips_split_symbol (rtx temp, rtx addr, enum machine_mode mode, rtx *low_out)
2928 {
2929 enum mips_symbol_context context;
2930 enum mips_symbol_type symbol_type;
2931 rtx high;
2932
2933 context = (mode == MAX_MACHINE_MODE
2934 ? SYMBOL_CONTEXT_LEA
2935 : SYMBOL_CONTEXT_MEM);
2936 if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
2937 {
2938 addr = XEXP (addr, 0);
2939 if (mips_symbolic_constant_p (addr, context, &symbol_type)
2940 && mips_symbol_insns (symbol_type, mode) > 0
2941 && mips_split_hi_p[symbol_type])
2942 {
2943 if (low_out)
2944 switch (symbol_type)
2945 {
2946 case SYMBOL_GOT_PAGE_OFST:
2947 /* The high part of a page/ofst pair is loaded from the GOT. */
2948 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
2949 break;
2950
2951 default:
2952 gcc_unreachable ();
2953 }
2954 return true;
2955 }
2956 }
2957 else
2958 {
2959 if (mips_symbolic_constant_p (addr, context, &symbol_type)
2960 && mips_symbol_insns (symbol_type, mode) > 0
2961 && mips_split_p[symbol_type])
2962 {
2963 if (low_out)
2964 switch (symbol_type)
2965 {
2966 case SYMBOL_GOT_DISP:
2967 /* SYMBOL_GOT_DISP symbols are loaded from the GOT. */
2968 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
2969 break;
2970
2971 case SYMBOL_GP_RELATIVE:
2972 high = mips_pic_base_register (temp);
2973 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2974 break;
2975
2976 default:
2977 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
2978 high = mips_force_temporary (temp, high);
2979 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
2980 break;
2981 }
2982 return true;
2983 }
2984 }
2985 return false;
2986 }
2987
2988 /* Return a legitimate address for REG + OFFSET. TEMP is as for
2989 mips_force_temporary; it is only needed when OFFSET is not a
2990 SMALL_OPERAND. */
2991
2992 static rtx
2993 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
2994 {
2995 if (!SMALL_OPERAND (offset))
2996 {
2997 rtx high;
2998
2999 if (TARGET_MIPS16)
3000 {
3001 /* Load the full offset into a register so that we can use
3002 an unextended instruction for the address itself. */
3003 high = GEN_INT (offset);
3004 offset = 0;
3005 }
3006 else
3007 {
3008 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
3009 The addition inside the macro CONST_HIGH_PART may cause an
3010 overflow, so we need to force a sign-extension check. */
3011 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
3012 offset = CONST_LOW_PART (offset);
3013 }
3014 high = mips_force_temporary (temp, high);
3015 reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
3016 }
3017 return plus_constant (Pmode, reg, offset);
3018 }
3019 \f
3020 /* The __tls_get_attr symbol. */
3021 static GTY(()) rtx mips_tls_symbol;
3022
3023 /* Return an instruction sequence that calls __tls_get_addr. SYM is
3024 the TLS symbol we are referencing and TYPE is the symbol type to use
3025 (either global dynamic or local dynamic). V0 is an RTX for the
3026 return value location. */
3027
3028 static rtx
3029 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
3030 {
3031 rtx insn, loc, a0;
3032
3033 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3034
3035 if (!mips_tls_symbol)
3036 mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
3037
3038 loc = mips_unspec_address (sym, type);
3039
3040 start_sequence ();
3041
3042 emit_insn (gen_rtx_SET (Pmode, a0,
3043 gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
3044 insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
3045 const0_rtx, NULL_RTX, false);
3046 RTL_CONST_CALL_P (insn) = 1;
3047 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
3048 insn = get_insns ();
3049
3050 end_sequence ();
3051
3052 return insn;
3053 }
3054
3055 /* Return a pseudo register that contains the current thread pointer. */
3056
3057 rtx
3058 mips_expand_thread_pointer (rtx tp)
3059 {
3060 rtx fn;
3061
3062 if (TARGET_MIPS16)
3063 {
3064 mips_need_mips16_rdhwr_p = true;
3065 fn = mips16_stub_function ("__mips16_rdhwr");
3066 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_LOCAL;
3067 if (!call_insn_operand (fn, VOIDmode))
3068 fn = force_reg (Pmode, fn);
3069 emit_insn (PMODE_INSN (gen_tls_get_tp_mips16, (tp, fn)));
3070 }
3071 else
3072 emit_insn (PMODE_INSN (gen_tls_get_tp, (tp)));
3073 return tp;
3074 }
3075
3076 static rtx
3077 mips_get_tp (void)
3078 {
3079 return mips_expand_thread_pointer (gen_reg_rtx (Pmode));
3080 }
3081
3082 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
3083 its address. The return value will be both a valid address and a valid
3084 SET_SRC (either a REG or a LO_SUM). */
3085
3086 static rtx
3087 mips_legitimize_tls_address (rtx loc)
3088 {
3089 rtx dest, insn, v0, tp, tmp1, tmp2, eqv, offset;
3090 enum tls_model model;
3091
3092 model = SYMBOL_REF_TLS_MODEL (loc);
3093 /* Only TARGET_ABICALLS code can have more than one module; other
3094 code must be be static and should not use a GOT. All TLS models
3095 reduce to local exec in this situation. */
3096 if (!TARGET_ABICALLS)
3097 model = TLS_MODEL_LOCAL_EXEC;
3098
3099 switch (model)
3100 {
3101 case TLS_MODEL_GLOBAL_DYNAMIC:
3102 v0 = gen_rtx_REG (Pmode, GP_RETURN);
3103 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
3104 dest = gen_reg_rtx (Pmode);
3105 emit_libcall_block (insn, dest, v0, loc);
3106 break;
3107
3108 case TLS_MODEL_LOCAL_DYNAMIC:
3109 v0 = gen_rtx_REG (Pmode, GP_RETURN);
3110 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
3111 tmp1 = gen_reg_rtx (Pmode);
3112
3113 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3114 share the LDM result with other LD model accesses. */
3115 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3116 UNSPEC_TLS_LDM);
3117 emit_libcall_block (insn, tmp1, v0, eqv);
3118
3119 offset = mips_unspec_address (loc, SYMBOL_DTPREL);
3120 if (mips_split_p[SYMBOL_DTPREL])
3121 {
3122 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
3123 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3124 }
3125 else
3126 dest = expand_binop (Pmode, add_optab, tmp1, offset,
3127 0, 0, OPTAB_DIRECT);
3128 break;
3129
3130 case TLS_MODEL_INITIAL_EXEC:
3131 tp = mips_get_tp ();
3132 tmp1 = gen_reg_rtx (Pmode);
3133 tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
3134 if (Pmode == DImode)
3135 emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
3136 else
3137 emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
3138 dest = gen_reg_rtx (Pmode);
3139 emit_insn (gen_add3_insn (dest, tmp1, tp));
3140 break;
3141
3142 case TLS_MODEL_LOCAL_EXEC:
3143 tmp1 = mips_get_tp ();
3144 offset = mips_unspec_address (loc, SYMBOL_TPREL);
3145 if (mips_split_p[SYMBOL_TPREL])
3146 {
3147 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_TPREL);
3148 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3149 }
3150 else
3151 dest = expand_binop (Pmode, add_optab, tmp1, offset,
3152 0, 0, OPTAB_DIRECT);
3153 break;
3154
3155 default:
3156 gcc_unreachable ();
3157 }
3158 return dest;
3159 }
3160 \f
3161 /* If X is not a valid address for mode MODE, force it into a register. */
3162
3163 static rtx
3164 mips_force_address (rtx x, enum machine_mode mode)
3165 {
3166 if (!mips_legitimate_address_p (mode, x, false))
3167 x = force_reg (Pmode, x);
3168 return x;
3169 }
3170
3171 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
3172 be legitimized in a way that the generic machinery might not expect,
3173 return a new address, otherwise return NULL. MODE is the mode of
3174 the memory being accessed. */
3175
3176 static rtx
3177 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3178 enum machine_mode mode)
3179 {
3180 rtx base, addr;
3181 HOST_WIDE_INT offset;
3182
3183 if (mips_tls_symbol_p (x))
3184 return mips_legitimize_tls_address (x);
3185
3186 /* See if the address can split into a high part and a LO_SUM. */
3187 if (mips_split_symbol (NULL, x, mode, &addr))
3188 return mips_force_address (addr, mode);
3189
3190 /* Handle BASE + OFFSET using mips_add_offset. */
3191 mips_split_plus (x, &base, &offset);
3192 if (offset != 0)
3193 {
3194 if (!mips_valid_base_register_p (base, mode, false))
3195 base = copy_to_mode_reg (Pmode, base);
3196 addr = mips_add_offset (NULL, base, offset);
3197 return mips_force_address (addr, mode);
3198 }
3199
3200 return x;
3201 }
3202
3203 /* Load VALUE into DEST. TEMP is as for mips_force_temporary. */
3204
3205 void
3206 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3207 {
3208 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3209 enum machine_mode mode;
3210 unsigned int i, num_ops;
3211 rtx x;
3212
3213 mode = GET_MODE (dest);
3214 num_ops = mips_build_integer (codes, value);
3215
3216 /* Apply each binary operation to X. Invariant: X is a legitimate
3217 source operand for a SET pattern. */
3218 x = GEN_INT (codes[0].value);
3219 for (i = 1; i < num_ops; i++)
3220 {
3221 if (!can_create_pseudo_p ())
3222 {
3223 emit_insn (gen_rtx_SET (VOIDmode, temp, x));
3224 x = temp;
3225 }
3226 else
3227 x = force_reg (mode, x);
3228 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3229 }
3230
3231 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
3232 }
3233
3234 /* Subroutine of mips_legitimize_move. Move constant SRC into register
3235 DEST given that SRC satisfies immediate_operand but doesn't satisfy
3236 move_operand. */
3237
3238 static void
3239 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
3240 {
3241 rtx base, offset;
3242
3243 /* Split moves of big integers into smaller pieces. */
3244 if (splittable_const_int_operand (src, mode))
3245 {
3246 mips_move_integer (dest, dest, INTVAL (src));
3247 return;
3248 }
3249
3250 /* Split moves of symbolic constants into high/low pairs. */
3251 if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3252 {
3253 emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3254 return;
3255 }
3256
3257 /* Generate the appropriate access sequences for TLS symbols. */
3258 if (mips_tls_symbol_p (src))
3259 {
3260 mips_emit_move (dest, mips_legitimize_tls_address (src));
3261 return;
3262 }
3263
3264 /* If we have (const (plus symbol offset)), and that expression cannot
3265 be forced into memory, load the symbol first and add in the offset.
3266 In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3267 forced into memory, as it usually produces better code. */
3268 split_const (src, &base, &offset);
3269 if (offset != const0_rtx
3270 && (targetm.cannot_force_const_mem (mode, src)
3271 || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3272 {
3273 base = mips_force_temporary (dest, base);
3274 mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3275 return;
3276 }
3277
3278 src = force_const_mem (mode, src);
3279
3280 /* When using explicit relocs, constant pool references are sometimes
3281 not legitimate addresses. */
3282 mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3283 mips_emit_move (dest, src);
3284 }
3285
3286 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3287 sequence that is valid. */
3288
3289 bool
3290 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
3291 {
3292 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3293 {
3294 mips_emit_move (dest, force_reg (mode, src));
3295 return true;
3296 }
3297
3298 /* We need to deal with constants that would be legitimate
3299 immediate_operands but aren't legitimate move_operands. */
3300 if (CONSTANT_P (src) && !move_operand (src, mode))
3301 {
3302 mips_legitimize_const_move (mode, dest, src);
3303 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3304 return true;
3305 }
3306 return false;
3307 }
3308 \f
3309 /* Return true if value X in context CONTEXT is a small-data address
3310 that can be rewritten as a LO_SUM. */
3311
3312 static bool
3313 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3314 {
3315 enum mips_symbol_type symbol_type;
3316
3317 return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3318 && !mips_split_p[SYMBOL_GP_RELATIVE]
3319 && mips_symbolic_constant_p (x, context, &symbol_type)
3320 && symbol_type == SYMBOL_GP_RELATIVE);
3321 }
3322
3323 /* A for_each_rtx callback for mips_small_data_pattern_p. DATA is the
3324 containing MEM, or null if none. */
3325
3326 static int
3327 mips_small_data_pattern_1 (rtx *loc, void *data)
3328 {
3329 enum mips_symbol_context context;
3330
3331 /* Ignore things like "g" constraints in asms. We make no particular
3332 guarantee about which symbolic constants are acceptable as asm operands
3333 versus which must be forced into a GPR. */
3334 if (GET_CODE (*loc) == LO_SUM || GET_CODE (*loc) == ASM_OPERANDS)
3335 return -1;
3336
3337 if (MEM_P (*loc))
3338 {
3339 if (for_each_rtx (&XEXP (*loc, 0), mips_small_data_pattern_1, *loc))
3340 return 1;
3341 return -1;
3342 }
3343
3344 context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3345 return mips_rewrite_small_data_p (*loc, context);
3346 }
3347
3348 /* Return true if OP refers to small data symbols directly, not through
3349 a LO_SUM. */
3350
3351 bool
3352 mips_small_data_pattern_p (rtx op)
3353 {
3354 return for_each_rtx (&op, mips_small_data_pattern_1, NULL);
3355 }
3356
3357 /* A for_each_rtx callback, used by mips_rewrite_small_data.
3358 DATA is the containing MEM, or null if none. */
3359
3360 static int
3361 mips_rewrite_small_data_1 (rtx *loc, void *data)
3362 {
3363 enum mips_symbol_context context;
3364
3365 if (MEM_P (*loc))
3366 {
3367 for_each_rtx (&XEXP (*loc, 0), mips_rewrite_small_data_1, *loc);
3368 return -1;
3369 }
3370
3371 context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3372 if (mips_rewrite_small_data_p (*loc, context))
3373 *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3374
3375 if (GET_CODE (*loc) == LO_SUM)
3376 return -1;
3377
3378 return 0;
3379 }
3380
3381 /* Rewrite instruction pattern PATTERN so that it refers to small data
3382 using explicit relocations. */
3383
3384 rtx
3385 mips_rewrite_small_data (rtx pattern)
3386 {
3387 pattern = copy_insn (pattern);
3388 for_each_rtx (&pattern, mips_rewrite_small_data_1, NULL);
3389 return pattern;
3390 }
3391 \f
3392 /* The cost of loading values from the constant pool. It should be
3393 larger than the cost of any constant we want to synthesize inline. */
3394 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3395
3396 /* Return the cost of X when used as an operand to the MIPS16 instruction
3397 that implements CODE. Return -1 if there is no such instruction, or if
3398 X is not a valid immediate operand for it. */
3399
3400 static int
3401 mips16_constant_cost (int code, HOST_WIDE_INT x)
3402 {
3403 switch (code)
3404 {
3405 case ASHIFT:
3406 case ASHIFTRT:
3407 case LSHIFTRT:
3408 /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3409 other shifts are extended. The shift patterns truncate the shift
3410 count to the right size, so there are no out-of-range values. */
3411 if (IN_RANGE (x, 1, 8))
3412 return 0;
3413 return COSTS_N_INSNS (1);
3414
3415 case PLUS:
3416 if (IN_RANGE (x, -128, 127))
3417 return 0;
3418 if (SMALL_OPERAND (x))
3419 return COSTS_N_INSNS (1);
3420 return -1;
3421
3422 case LEU:
3423 /* Like LE, but reject the always-true case. */
3424 if (x == -1)
3425 return -1;
3426 case LE:
3427 /* We add 1 to the immediate and use SLT. */
3428 x += 1;
3429 case XOR:
3430 /* We can use CMPI for an xor with an unsigned 16-bit X. */
3431 case LT:
3432 case LTU:
3433 if (IN_RANGE (x, 0, 255))
3434 return 0;
3435 if (SMALL_OPERAND_UNSIGNED (x))
3436 return COSTS_N_INSNS (1);
3437 return -1;
3438
3439 case EQ:
3440 case NE:
3441 /* Equality comparisons with 0 are cheap. */
3442 if (x == 0)
3443 return 0;
3444 return -1;
3445
3446 default:
3447 return -1;
3448 }
3449 }
3450
3451 /* Return true if there is a non-MIPS16 instruction that implements CODE
3452 and if that instruction accepts X as an immediate operand. */
3453
3454 static int
3455 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3456 {
3457 switch (code)
3458 {
3459 case ASHIFT:
3460 case ASHIFTRT:
3461 case LSHIFTRT:
3462 /* All shift counts are truncated to a valid constant. */
3463 return true;
3464
3465 case ROTATE:
3466 case ROTATERT:
3467 /* Likewise rotates, if the target supports rotates at all. */
3468 return ISA_HAS_ROR;
3469
3470 case AND:
3471 case IOR:
3472 case XOR:
3473 /* These instructions take 16-bit unsigned immediates. */
3474 return SMALL_OPERAND_UNSIGNED (x);
3475
3476 case PLUS:
3477 case LT:
3478 case LTU:
3479 /* These instructions take 16-bit signed immediates. */
3480 return SMALL_OPERAND (x);
3481
3482 case EQ:
3483 case NE:
3484 case GT:
3485 case GTU:
3486 /* The "immediate" forms of these instructions are really
3487 implemented as comparisons with register 0. */
3488 return x == 0;
3489
3490 case GE:
3491 case GEU:
3492 /* Likewise, meaning that the only valid immediate operand is 1. */
3493 return x == 1;
3494
3495 case LE:
3496 /* We add 1 to the immediate and use SLT. */
3497 return SMALL_OPERAND (x + 1);
3498
3499 case LEU:
3500 /* Likewise SLTU, but reject the always-true case. */
3501 return SMALL_OPERAND (x + 1) && x + 1 != 0;
3502
3503 case SIGN_EXTRACT:
3504 case ZERO_EXTRACT:
3505 /* The bit position and size are immediate operands. */
3506 return ISA_HAS_EXT_INS;
3507
3508 default:
3509 /* By default assume that $0 can be used for 0. */
3510 return x == 0;
3511 }
3512 }
3513
3514 /* Return the cost of binary operation X, given that the instruction
3515 sequence for a word-sized or smaller operation has cost SINGLE_COST
3516 and that the sequence of a double-word operation has cost DOUBLE_COST.
3517 If SPEED is true, optimize for speed otherwise optimize for size. */
3518
3519 static int
3520 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3521 {
3522 int cost;
3523
3524 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3525 cost = double_cost;
3526 else
3527 cost = single_cost;
3528 return (cost
3529 + set_src_cost (XEXP (x, 0), speed)
3530 + rtx_cost (XEXP (x, 1), GET_CODE (x), 1, speed));
3531 }
3532
3533 /* Return the cost of floating-point multiplications of mode MODE. */
3534
3535 static int
3536 mips_fp_mult_cost (enum machine_mode mode)
3537 {
3538 return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3539 }
3540
3541 /* Return the cost of floating-point divisions of mode MODE. */
3542
3543 static int
3544 mips_fp_div_cost (enum machine_mode mode)
3545 {
3546 return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3547 }
3548
3549 /* Return the cost of sign-extending OP to mode MODE, not including the
3550 cost of OP itself. */
3551
3552 static int
3553 mips_sign_extend_cost (enum machine_mode mode, rtx op)
3554 {
3555 if (MEM_P (op))
3556 /* Extended loads are as cheap as unextended ones. */
3557 return 0;
3558
3559 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3560 /* A sign extension from SImode to DImode in 64-bit mode is free. */
3561 return 0;
3562
3563 if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3564 /* We can use SEB or SEH. */
3565 return COSTS_N_INSNS (1);
3566
3567 /* We need to use a shift left and a shift right. */
3568 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3569 }
3570
3571 /* Return the cost of zero-extending OP to mode MODE, not including the
3572 cost of OP itself. */
3573
3574 static int
3575 mips_zero_extend_cost (enum machine_mode mode, rtx op)
3576 {
3577 if (MEM_P (op))
3578 /* Extended loads are as cheap as unextended ones. */
3579 return 0;
3580
3581 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3582 /* We need a shift left by 32 bits and a shift right by 32 bits. */
3583 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3584
3585 if (GENERATE_MIPS16E)
3586 /* We can use ZEB or ZEH. */
3587 return COSTS_N_INSNS (1);
3588
3589 if (TARGET_MIPS16)
3590 /* We need to load 0xff or 0xffff into a register and use AND. */
3591 return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
3592
3593 /* We can use ANDI. */
3594 return COSTS_N_INSNS (1);
3595 }
3596
3597 /* Return the cost of moving between two registers of mode MODE,
3598 assuming that the move will be in pieces of at most UNITS bytes. */
3599
3600 static int
3601 mips_set_reg_reg_piece_cost (enum machine_mode mode, unsigned int units)
3602 {
3603 return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
3604 }
3605
3606 /* Return the cost of moving between two registers of mode MODE. */
3607
3608 static int
3609 mips_set_reg_reg_cost (enum machine_mode mode)
3610 {
3611 switch (GET_MODE_CLASS (mode))
3612 {
3613 case MODE_CC:
3614 return mips_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
3615
3616 case MODE_FLOAT:
3617 case MODE_COMPLEX_FLOAT:
3618 case MODE_VECTOR_FLOAT:
3619 if (TARGET_HARD_FLOAT)
3620 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
3621 /* Fall through */
3622
3623 default:
3624 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
3625 }
3626 }
3627
3628 /* Return the cost of an operand X that can be trucated for free.
3629 SPEED says whether we're optimizing for size or speed. */
3630
3631 static int
3632 mips_truncated_op_cost (rtx x, bool speed)
3633 {
3634 if (GET_CODE (x) == TRUNCATE)
3635 x = XEXP (x, 0);
3636 return set_src_cost (x, speed);
3637 }
3638
3639 /* Implement TARGET_RTX_COSTS. */
3640
3641 static bool
3642 mips_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3643 int *total, bool speed)
3644 {
3645 enum machine_mode mode = GET_MODE (x);
3646 bool float_mode_p = FLOAT_MODE_P (mode);
3647 int cost;
3648 rtx addr;
3649
3650 /* The cost of a COMPARE is hard to define for MIPS. COMPAREs don't
3651 appear in the instruction stream, and the cost of a comparison is
3652 really the cost of the branch or scc condition. At the time of
3653 writing, GCC only uses an explicit outer COMPARE code when optabs
3654 is testing whether a constant is expensive enough to force into a
3655 register. We want optabs to pass such constants through the MIPS
3656 expanders instead, so make all constants very cheap here. */
3657 if (outer_code == COMPARE)
3658 {
3659 gcc_assert (CONSTANT_P (x));
3660 *total = 0;
3661 return true;
3662 }
3663
3664 switch (code)
3665 {
3666 case CONST_INT:
3667 /* Treat *clear_upper32-style ANDs as having zero cost in the
3668 second operand. The cost is entirely in the first operand.
3669
3670 ??? This is needed because we would otherwise try to CSE
3671 the constant operand. Although that's the right thing for
3672 instructions that continue to be a register operation throughout
3673 compilation, it is disastrous for instructions that could
3674 later be converted into a memory operation. */
3675 if (TARGET_64BIT
3676 && outer_code == AND
3677 && UINTVAL (x) == 0xffffffff)
3678 {
3679 *total = 0;
3680 return true;
3681 }
3682
3683 if (TARGET_MIPS16)
3684 {
3685 cost = mips16_constant_cost (outer_code, INTVAL (x));
3686 if (cost >= 0)
3687 {
3688 *total = cost;
3689 return true;
3690 }
3691 }
3692 else
3693 {
3694 /* When not optimizing for size, we care more about the cost
3695 of hot code, and hot code is often in a loop. If a constant
3696 operand needs to be forced into a register, we will often be
3697 able to hoist the constant load out of the loop, so the load
3698 should not contribute to the cost. */
3699 if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
3700 {
3701 *total = 0;
3702 return true;
3703 }
3704 }
3705 /* Fall through. */
3706
3707 case CONST:
3708 case SYMBOL_REF:
3709 case LABEL_REF:
3710 case CONST_DOUBLE:
3711 if (force_to_mem_operand (x, VOIDmode))
3712 {
3713 *total = COSTS_N_INSNS (1);
3714 return true;
3715 }
3716 cost = mips_const_insns (x);
3717 if (cost > 0)
3718 {
3719 /* If the constant is likely to be stored in a GPR, SETs of
3720 single-insn constants are as cheap as register sets; we
3721 never want to CSE them.
3722
3723 Don't reduce the cost of storing a floating-point zero in
3724 FPRs. If we have a zero in an FPR for other reasons, we
3725 can get better cfg-cleanup and delayed-branch results by
3726 using it consistently, rather than using $0 sometimes and
3727 an FPR at other times. Also, moves between floating-point
3728 registers are sometimes cheaper than (D)MTC1 $0. */
3729 if (cost == 1
3730 && outer_code == SET
3731 && !(float_mode_p && TARGET_HARD_FLOAT))
3732 cost = 0;
3733 /* When non-MIPS16 code loads a constant N>1 times, we rarely
3734 want to CSE the constant itself. It is usually better to
3735 have N copies of the last operation in the sequence and one
3736 shared copy of the other operations. (Note that this is
3737 not true for MIPS16 code, where the final operation in the
3738 sequence is often an extended instruction.)
3739
3740 Also, if we have a CONST_INT, we don't know whether it is
3741 for a word or doubleword operation, so we cannot rely on
3742 the result of mips_build_integer. */
3743 else if (!TARGET_MIPS16
3744 && (outer_code == SET || mode == VOIDmode))
3745 cost = 1;
3746 *total = COSTS_N_INSNS (cost);
3747 return true;
3748 }
3749 /* The value will need to be fetched from the constant pool. */
3750 *total = CONSTANT_POOL_COST;
3751 return true;
3752
3753 case MEM:
3754 /* If the address is legitimate, return the number of
3755 instructions it needs. */
3756 addr = XEXP (x, 0);
3757 cost = mips_address_insns (addr, mode, true);
3758 if (cost > 0)
3759 {
3760 *total = COSTS_N_INSNS (cost + 1);
3761 return true;
3762 }
3763 /* Check for a scaled indexed address. */
3764 if (mips_lwxs_address_p (addr)
3765 || mips_lx_address_p (addr, mode))
3766 {
3767 *total = COSTS_N_INSNS (2);
3768 return true;
3769 }
3770 /* Otherwise use the default handling. */
3771 return false;
3772
3773 case FFS:
3774 *total = COSTS_N_INSNS (6);
3775 return false;
3776
3777 case NOT:
3778 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3779 return false;
3780
3781 case AND:
3782 /* Check for a *clear_upper32 pattern and treat it like a zero
3783 extension. See the pattern's comment for details. */
3784 if (TARGET_64BIT
3785 && mode == DImode
3786 && CONST_INT_P (XEXP (x, 1))
3787 && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3788 {
3789 *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
3790 + set_src_cost (XEXP (x, 0), speed));
3791 return true;
3792 }
3793 if (ISA_HAS_CINS && CONST_INT_P (XEXP (x, 1)))
3794 {
3795 rtx op = XEXP (x, 0);
3796 if (GET_CODE (op) == ASHIFT
3797 && CONST_INT_P (XEXP (op, 1))
3798 && mask_low_and_shift_p (mode, XEXP (x, 1), XEXP (op, 1), 32))
3799 {
3800 *total = COSTS_N_INSNS (1) + set_src_cost (XEXP (op, 0), speed);
3801 return true;
3802 }
3803 }
3804 /* (AND (NOT op0) (NOT op1) is a nor operation that can be done in
3805 a single instruction. */
3806 if (!TARGET_MIPS16
3807 && GET_CODE (XEXP (x, 0)) == NOT
3808 && GET_CODE (XEXP (x, 1)) == NOT)
3809 {
3810 cost = GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1;
3811 *total = (COSTS_N_INSNS (cost)
3812 + set_src_cost (XEXP (XEXP (x, 0), 0), speed)
3813 + set_src_cost (XEXP (XEXP (x, 1), 0), speed));
3814 return true;
3815 }
3816
3817 /* Fall through. */
3818
3819 case IOR:
3820 case XOR:
3821 /* Double-word operations use two single-word operations. */
3822 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
3823 speed);
3824 return true;
3825
3826 case ASHIFT:
3827 case ASHIFTRT:
3828 case LSHIFTRT:
3829 case ROTATE:
3830 case ROTATERT:
3831 if (CONSTANT_P (XEXP (x, 1)))
3832 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3833 speed);
3834 else
3835 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
3836 speed);
3837 return true;
3838
3839 case ABS:
3840 if (float_mode_p)
3841 *total = mips_cost->fp_add;
3842 else
3843 *total = COSTS_N_INSNS (4);
3844 return false;
3845
3846 case LO_SUM:
3847 /* Low-part immediates need an extended MIPS16 instruction. */
3848 *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
3849 + set_src_cost (XEXP (x, 0), speed));
3850 return true;
3851
3852 case LT:
3853 case LTU:
3854 case LE:
3855 case LEU:
3856 case GT:
3857 case GTU:
3858 case GE:
3859 case GEU:
3860 case EQ:
3861 case NE:
3862 case UNORDERED:
3863 case LTGT:
3864 /* Branch comparisons have VOIDmode, so use the first operand's
3865 mode instead. */
3866 mode = GET_MODE (XEXP (x, 0));
3867 if (FLOAT_MODE_P (mode))
3868 {
3869 *total = mips_cost->fp_add;
3870 return false;
3871 }
3872 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3873 speed);
3874 return true;
3875
3876 case MINUS:
3877 if (float_mode_p
3878 && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
3879 && TARGET_FUSED_MADD
3880 && !HONOR_NANS (mode)
3881 && !HONOR_SIGNED_ZEROS (mode))
3882 {
3883 /* See if we can use NMADD or NMSUB. See mips.md for the
3884 associated patterns. */
3885 rtx op0 = XEXP (x, 0);
3886 rtx op1 = XEXP (x, 1);
3887 if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
3888 {
3889 *total = (mips_fp_mult_cost (mode)
3890 + set_src_cost (XEXP (XEXP (op0, 0), 0), speed)
3891 + set_src_cost (XEXP (op0, 1), speed)
3892 + set_src_cost (op1, speed));
3893 return true;
3894 }
3895 if (GET_CODE (op1) == MULT)
3896 {
3897 *total = (mips_fp_mult_cost (mode)
3898 + set_src_cost (op0, speed)
3899 + set_src_cost (XEXP (op1, 0), speed)
3900 + set_src_cost (XEXP (op1, 1), speed));
3901 return true;
3902 }
3903 }
3904 /* Fall through. */
3905
3906 case PLUS:
3907 if (float_mode_p)
3908 {
3909 /* If this is part of a MADD or MSUB, treat the PLUS as
3910 being free. */
3911 if ((ISA_HAS_FP_MADD4_MSUB4 || ISA_HAS_FP_MADD3_MSUB3)
3912 && TARGET_FUSED_MADD
3913 && GET_CODE (XEXP (x, 0)) == MULT)
3914 *total = 0;
3915 else
3916 *total = mips_cost->fp_add;
3917 return false;
3918 }
3919
3920 /* Double-word operations require three single-word operations and
3921 an SLTU. The MIPS16 version then needs to move the result of
3922 the SLTU from $24 to a MIPS16 register. */
3923 *total = mips_binary_cost (x, COSTS_N_INSNS (1),
3924 COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
3925 speed);
3926 return true;
3927
3928 case NEG:
3929 if (float_mode_p
3930 && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
3931 && TARGET_FUSED_MADD
3932 && !HONOR_NANS (mode)
3933 && HONOR_SIGNED_ZEROS (mode))
3934 {
3935 /* See if we can use NMADD or NMSUB. See mips.md for the
3936 associated patterns. */
3937 rtx op = XEXP (x, 0);
3938 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
3939 && GET_CODE (XEXP (op, 0)) == MULT)
3940 {
3941 *total = (mips_fp_mult_cost (mode)
3942 + set_src_cost (XEXP (XEXP (op, 0), 0), speed)
3943 + set_src_cost (XEXP (XEXP (op, 0), 1), speed)
3944 + set_src_cost (XEXP (op, 1), speed));
3945 return true;
3946 }
3947 }
3948
3949 if (float_mode_p)
3950 *total = mips_cost->fp_add;
3951 else
3952 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
3953 return false;
3954
3955 case MULT:
3956 if (float_mode_p)
3957 *total = mips_fp_mult_cost (mode);
3958 else if (mode == DImode && !TARGET_64BIT)
3959 /* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
3960 where the mulsidi3 always includes an MFHI and an MFLO. */
3961 *total = (speed
3962 ? mips_cost->int_mult_si * 3 + 6
3963 : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
3964 else if (!speed)
3965 *total = COSTS_N_INSNS (ISA_HAS_MUL3 ? 1 : 2) + 1;
3966 else if (mode == DImode)
3967 *total = mips_cost->int_mult_di;
3968 else
3969 *total = mips_cost->int_mult_si;
3970 return false;
3971
3972 case DIV:
3973 /* Check for a reciprocal. */
3974 if (float_mode_p
3975 && ISA_HAS_FP4
3976 && flag_unsafe_math_optimizations
3977 && XEXP (x, 0) == CONST1_RTX (mode))
3978 {
3979 if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
3980 /* An rsqrt<mode>a or rsqrt<mode>b pattern. Count the
3981 division as being free. */
3982 *total = set_src_cost (XEXP (x, 1), speed);
3983 else
3984 *total = (mips_fp_div_cost (mode)
3985 + set_src_cost (XEXP (x, 1), speed));
3986 return true;
3987 }
3988 /* Fall through. */
3989
3990 case SQRT:
3991 case MOD:
3992 if (float_mode_p)
3993 {
3994 *total = mips_fp_div_cost (mode);
3995 return false;
3996 }
3997 /* Fall through. */
3998
3999 case UDIV:
4000 case UMOD:
4001 if (!speed)
4002 {
4003 /* It is our responsibility to make division by a power of 2
4004 as cheap as 2 register additions if we want the division
4005 expanders to be used for such operations; see the setting
4006 of sdiv_pow2_cheap in optabs.c. Using (D)DIV for MIPS16
4007 should always produce shorter code than using
4008 expand_sdiv2_pow2. */
4009 if (TARGET_MIPS16
4010 && CONST_INT_P (XEXP (x, 1))
4011 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
4012 {
4013 *total = COSTS_N_INSNS (2) + set_src_cost (XEXP (x, 0), speed);
4014 return true;
4015 }
4016 *total = COSTS_N_INSNS (mips_idiv_insns ());
4017 }
4018 else if (mode == DImode)
4019 *total = mips_cost->int_div_di;
4020 else
4021 *total = mips_cost->int_div_si;
4022 return false;
4023
4024 case SIGN_EXTEND:
4025 *total = mips_sign_extend_cost (mode, XEXP (x, 0));
4026 return false;
4027
4028 case ZERO_EXTEND:
4029 if (outer_code == SET
4030 && ISA_HAS_BADDU
4031 && GET_MODE (XEXP (x, 0)) == QImode
4032 && GET_CODE (XEXP (x, 0)) == PLUS)
4033 {
4034 rtx plus = XEXP (x, 0);
4035 *total = (COSTS_N_INSNS (1)
4036 + mips_truncated_op_cost (XEXP (plus, 0), speed)
4037 + mips_truncated_op_cost (XEXP (plus, 1), speed));
4038 return true;
4039 }
4040 *total = mips_zero_extend_cost (mode, XEXP (x, 0));
4041 return false;
4042
4043 case FLOAT:
4044 case UNSIGNED_FLOAT:
4045 case FIX:
4046 case FLOAT_EXTEND:
4047 case FLOAT_TRUNCATE:
4048 *total = mips_cost->fp_add;
4049 return false;
4050
4051 case SET:
4052 if (register_operand (SET_DEST (x), VOIDmode)
4053 && reg_or_0_operand (SET_SRC (x), VOIDmode))
4054 {
4055 *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
4056 return true;
4057 }
4058 return false;
4059
4060 default:
4061 return false;
4062 }
4063 }
4064
4065 /* Implement TARGET_ADDRESS_COST. */
4066
4067 static int
4068 mips_address_cost (rtx addr, enum machine_mode mode,
4069 addr_space_t as ATTRIBUTE_UNUSED,
4070 bool speed ATTRIBUTE_UNUSED)
4071 {
4072 return mips_address_insns (addr, mode, false);
4073 }
4074 \f
4075 /* Information about a single instruction in a multi-instruction
4076 asm sequence. */
4077 struct mips_multi_member {
4078 /* True if this is a label, false if it is code. */
4079 bool is_label_p;
4080
4081 /* The output_asm_insn format of the instruction. */
4082 const char *format;
4083
4084 /* The operands to the instruction. */
4085 rtx operands[MAX_RECOG_OPERANDS];
4086 };
4087 typedef struct mips_multi_member mips_multi_member;
4088
4089 /* The instructions that make up the current multi-insn sequence. */
4090 static vec<mips_multi_member> mips_multi_members;
4091
4092 /* How many instructions (as opposed to labels) are in the current
4093 multi-insn sequence. */
4094 static unsigned int mips_multi_num_insns;
4095
4096 /* Start a new multi-insn sequence. */
4097
4098 static void
4099 mips_multi_start (void)
4100 {
4101 mips_multi_members.truncate (0);
4102 mips_multi_num_insns = 0;
4103 }
4104
4105 /* Add a new, uninitialized member to the current multi-insn sequence. */
4106
4107 static struct mips_multi_member *
4108 mips_multi_add (void)
4109 {
4110 mips_multi_member empty;
4111 return mips_multi_members.safe_push (empty);
4112 }
4113
4114 /* Add a normal insn with the given asm format to the current multi-insn
4115 sequence. The other arguments are a null-terminated list of operands. */
4116
4117 static void
4118 mips_multi_add_insn (const char *format, ...)
4119 {
4120 struct mips_multi_member *member;
4121 va_list ap;
4122 unsigned int i;
4123 rtx op;
4124
4125 member = mips_multi_add ();
4126 member->is_label_p = false;
4127 member->format = format;
4128 va_start (ap, format);
4129 i = 0;
4130 while ((op = va_arg (ap, rtx)))
4131 member->operands[i++] = op;
4132 va_end (ap);
4133 mips_multi_num_insns++;
4134 }
4135
4136 /* Add the given label definition to the current multi-insn sequence.
4137 The definition should include the colon. */
4138
4139 static void
4140 mips_multi_add_label (const char *label)
4141 {
4142 struct mips_multi_member *member;
4143
4144 member = mips_multi_add ();
4145 member->is_label_p = true;
4146 member->format = label;
4147 }
4148
4149 /* Return the index of the last member of the current multi-insn sequence. */
4150
4151 static unsigned int
4152 mips_multi_last_index (void)
4153 {
4154 return mips_multi_members.length () - 1;
4155 }
4156
4157 /* Add a copy of an existing instruction to the current multi-insn
4158 sequence. I is the index of the instruction that should be copied. */
4159
4160 static void
4161 mips_multi_copy_insn (unsigned int i)
4162 {
4163 struct mips_multi_member *member;
4164
4165 member = mips_multi_add ();
4166 memcpy (member, &mips_multi_members[i], sizeof (*member));
4167 gcc_assert (!member->is_label_p);
4168 }
4169
4170 /* Change the operand of an existing instruction in the current
4171 multi-insn sequence. I is the index of the instruction,
4172 OP is the index of the operand, and X is the new value. */
4173
4174 static void
4175 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
4176 {
4177 mips_multi_members[i].operands[op] = x;
4178 }
4179
4180 /* Write out the asm code for the current multi-insn sequence. */
4181
4182 static void
4183 mips_multi_write (void)
4184 {
4185 struct mips_multi_member *member;
4186 unsigned int i;
4187
4188 FOR_EACH_VEC_ELT (mips_multi_members, i, member)
4189 if (member->is_label_p)
4190 fprintf (asm_out_file, "%s\n", member->format);
4191 else
4192 output_asm_insn (member->format, member->operands);
4193 }
4194 \f
4195 /* Return one word of double-word value OP, taking into account the fixed
4196 endianness of certain registers. HIGH_P is true to select the high part,
4197 false to select the low part. */
4198
4199 rtx
4200 mips_subword (rtx op, bool high_p)
4201 {
4202 unsigned int byte, offset;
4203 enum machine_mode mode;
4204
4205 mode = GET_MODE (op);
4206 if (mode == VOIDmode)
4207 mode = TARGET_64BIT ? TImode : DImode;
4208
4209 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4210 byte = UNITS_PER_WORD;
4211 else
4212 byte = 0;
4213
4214 if (FP_REG_RTX_P (op))
4215 {
4216 /* Paired FPRs are always ordered little-endian. */
4217 offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4218 return gen_rtx_REG (word_mode, REGNO (op) + offset);
4219 }
4220
4221 if (MEM_P (op))
4222 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4223
4224 return simplify_gen_subreg (word_mode, op, mode, byte);
4225 }
4226
4227 /* Return true if SRC should be moved into DEST using "MULT $0, $0".
4228 SPLIT_TYPE is the condition under which moves should be split. */
4229
4230 static bool
4231 mips_mult_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4232 {
4233 return ((split_type != SPLIT_FOR_SPEED
4234 || mips_tuning_info.fast_mult_zero_zero_p)
4235 && src == const0_rtx
4236 && REG_P (dest)
4237 && GET_MODE_SIZE (GET_MODE (dest)) == 2 * UNITS_PER_WORD
4238 && (ISA_HAS_DSP_MULT
4239 ? ACC_REG_P (REGNO (dest))
4240 : MD_REG_P (REGNO (dest))));
4241 }
4242
4243 /* Return true if a move from SRC to DEST should be split into two.
4244 SPLIT_TYPE describes the split condition. */
4245
4246 bool
4247 mips_split_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4248 {
4249 /* Check whether the move can be done using some variant of MULT $0,$0. */
4250 if (mips_mult_move_p (dest, src, split_type))
4251 return false;
4252
4253 /* FPR-to-FPR moves can be done in a single instruction, if they're
4254 allowed at all. */
4255 unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
4256 if (size == 8 && FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4257 return false;
4258
4259 /* Check for floating-point loads and stores. */
4260 if (size == 8 && ISA_HAS_LDC1_SDC1)
4261 {
4262 if (FP_REG_RTX_P (dest) && MEM_P (src))
4263 return false;
4264 if (FP_REG_RTX_P (src) && MEM_P (dest))
4265 return false;
4266 }
4267
4268 /* Otherwise split all multiword moves. */
4269 return size > UNITS_PER_WORD;
4270 }
4271
4272 /* Split a move from SRC to DEST, given that mips_split_move_p holds.
4273 SPLIT_TYPE describes the split condition. */
4274
4275 void
4276 mips_split_move (rtx dest, rtx src, enum mips_split_type split_type)
4277 {
4278 rtx low_dest;
4279
4280 gcc_checking_assert (mips_split_move_p (dest, src, split_type));
4281 if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
4282 {
4283 if (!TARGET_64BIT && GET_MODE (dest) == DImode)
4284 emit_insn (gen_move_doubleword_fprdi (dest, src));
4285 else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
4286 emit_insn (gen_move_doubleword_fprdf (dest, src));
4287 else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
4288 emit_insn (gen_move_doubleword_fprv2sf (dest, src));
4289 else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
4290 emit_insn (gen_move_doubleword_fprv2si (dest, src));
4291 else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
4292 emit_insn (gen_move_doubleword_fprv4hi (dest, src));
4293 else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
4294 emit_insn (gen_move_doubleword_fprv8qi (dest, src));
4295 else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
4296 emit_insn (gen_move_doubleword_fprtf (dest, src));
4297 else
4298 gcc_unreachable ();
4299 }
4300 else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
4301 {
4302 low_dest = mips_subword (dest, false);
4303 mips_emit_move (low_dest, mips_subword (src, false));
4304 if (TARGET_64BIT)
4305 emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
4306 else
4307 emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
4308 }
4309 else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
4310 {
4311 mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
4312 if (TARGET_64BIT)
4313 emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
4314 else
4315 emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
4316 }
4317 else
4318 {
4319 /* The operation can be split into two normal moves. Decide in
4320 which order to do them. */
4321 low_dest = mips_subword (dest, false);
4322 if (REG_P (low_dest)
4323 && reg_overlap_mentioned_p (low_dest, src))
4324 {
4325 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4326 mips_emit_move (low_dest, mips_subword (src, false));
4327 }
4328 else
4329 {
4330 mips_emit_move (low_dest, mips_subword (src, false));
4331 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4332 }
4333 }
4334 }
4335
4336 /* Return the split type for instruction INSN. */
4337
4338 static enum mips_split_type
4339 mips_insn_split_type (rtx insn)
4340 {
4341 basic_block bb = BLOCK_FOR_INSN (insn);
4342 if (bb)
4343 {
4344 if (optimize_bb_for_speed_p (bb))
4345 return SPLIT_FOR_SPEED;
4346 else
4347 return SPLIT_FOR_SIZE;
4348 }
4349 /* Once CFG information has been removed, we should trust the optimization
4350 decisions made by previous passes and only split where necessary. */
4351 return SPLIT_IF_NECESSARY;
4352 }
4353
4354 /* Return true if a move from SRC to DEST in INSN should be split. */
4355
4356 bool
4357 mips_split_move_insn_p (rtx dest, rtx src, rtx insn)
4358 {
4359 return mips_split_move_p (dest, src, mips_insn_split_type (insn));
4360 }
4361
4362 /* Split a move from SRC to DEST in INSN, given that mips_split_move_insn_p
4363 holds. */
4364
4365 void
4366 mips_split_move_insn (rtx dest, rtx src, rtx insn)
4367 {
4368 mips_split_move (dest, src, mips_insn_split_type (insn));
4369 }
4370 \f
4371 /* Return the appropriate instructions to move SRC into DEST. Assume
4372 that SRC is operand 1 and DEST is operand 0. */
4373
4374 const char *
4375 mips_output_move (rtx dest, rtx src)
4376 {
4377 enum rtx_code dest_code, src_code;
4378 enum machine_mode mode;
4379 enum mips_symbol_type symbol_type;
4380 bool dbl_p;
4381
4382 dest_code = GET_CODE (dest);
4383 src_code = GET_CODE (src);
4384 mode = GET_MODE (dest);
4385 dbl_p = (GET_MODE_SIZE (mode) == 8);
4386
4387 if (mips_split_move_p (dest, src, SPLIT_IF_NECESSARY))
4388 return "#";
4389
4390 if ((src_code == REG && GP_REG_P (REGNO (src)))
4391 || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
4392 {
4393 if (dest_code == REG)
4394 {
4395 if (GP_REG_P (REGNO (dest)))
4396 return "move\t%0,%z1";
4397
4398 if (mips_mult_move_p (dest, src, SPLIT_IF_NECESSARY))
4399 {
4400 if (ISA_HAS_DSP_MULT)
4401 return "mult\t%q0,%.,%.";
4402 else
4403 return "mult\t%.,%.";
4404 }
4405
4406 /* Moves to HI are handled by special .md insns. */
4407 if (REGNO (dest) == LO_REGNUM)
4408 return "mtlo\t%z1";
4409
4410 if (DSP_ACC_REG_P (REGNO (dest)))
4411 {
4412 static char retval[] = "mt__\t%z1,%q0";
4413
4414 retval[2] = reg_names[REGNO (dest)][4];
4415 retval[3] = reg_names[REGNO (dest)][5];
4416 return retval;
4417 }
4418
4419 if (FP_REG_P (REGNO (dest)))
4420 return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
4421
4422 if (ALL_COP_REG_P (REGNO (dest)))
4423 {
4424 static char retval[] = "dmtc_\t%z1,%0";
4425
4426 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4427 return dbl_p ? retval : retval + 1;
4428 }
4429 }
4430 if (dest_code == MEM)
4431 switch (GET_MODE_SIZE (mode))
4432 {
4433 case 1: return "sb\t%z1,%0";
4434 case 2: return "sh\t%z1,%0";
4435 case 4: return "sw\t%z1,%0";
4436 case 8: return "sd\t%z1,%0";
4437 }
4438 }
4439 if (dest_code == REG && GP_REG_P (REGNO (dest)))
4440 {
4441 if (src_code == REG)
4442 {
4443 /* Moves from HI are handled by special .md insns. */
4444 if (REGNO (src) == LO_REGNUM)
4445 {
4446 /* When generating VR4120 or VR4130 code, we use MACC and
4447 DMACC instead of MFLO. This avoids both the normal
4448 MIPS III HI/LO hazards and the errata related to
4449 -mfix-vr4130. */
4450 if (ISA_HAS_MACCHI)
4451 return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
4452 return "mflo\t%0";
4453 }
4454
4455 if (DSP_ACC_REG_P (REGNO (src)))
4456 {
4457 static char retval[] = "mf__\t%0,%q1";
4458
4459 retval[2] = reg_names[REGNO (src)][4];
4460 retval[3] = reg_names[REGNO (src)][5];
4461 return retval;
4462 }
4463
4464 if (FP_REG_P (REGNO (src)))
4465 return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
4466
4467 if (ALL_COP_REG_P (REGNO (src)))
4468 {
4469 static char retval[] = "dmfc_\t%0,%1";
4470
4471 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4472 return dbl_p ? retval : retval + 1;
4473 }
4474 }
4475
4476 if (src_code == MEM)
4477 switch (GET_MODE_SIZE (mode))
4478 {
4479 case 1: return "lbu\t%0,%1";
4480 case 2: return "lhu\t%0,%1";
4481 case 4: return "lw\t%0,%1";
4482 case 8: return "ld\t%0,%1";
4483 }
4484
4485 if (src_code == CONST_INT)
4486 {
4487 /* Don't use the X format for the operand itself, because that
4488 will give out-of-range numbers for 64-bit hosts and 32-bit
4489 targets. */
4490 if (!TARGET_MIPS16)
4491 return "li\t%0,%1\t\t\t# %X1";
4492
4493 if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
4494 return "li\t%0,%1";
4495
4496 if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
4497 return "#";
4498 }
4499
4500 if (src_code == HIGH)
4501 return TARGET_MIPS16 ? "#" : "lui\t%0,%h1";
4502
4503 if (CONST_GP_P (src))
4504 return "move\t%0,%1";
4505
4506 if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
4507 && mips_lo_relocs[symbol_type] != 0)
4508 {
4509 /* A signed 16-bit constant formed by applying a relocation
4510 operator to a symbolic address. */
4511 gcc_assert (!mips_split_p[symbol_type]);
4512 return "li\t%0,%R1";
4513 }
4514
4515 if (symbolic_operand (src, VOIDmode))
4516 {
4517 gcc_assert (TARGET_MIPS16
4518 ? TARGET_MIPS16_TEXT_LOADS
4519 : !TARGET_EXPLICIT_RELOCS);
4520 return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
4521 }
4522 }
4523 if (src_code == REG && FP_REG_P (REGNO (src)))
4524 {
4525 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4526 {
4527 if (GET_MODE (dest) == V2SFmode)
4528 return "mov.ps\t%0,%1";
4529 else
4530 return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
4531 }
4532
4533 if (dest_code == MEM)
4534 return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
4535 }
4536 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4537 {
4538 if (src_code == MEM)
4539 return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
4540 }
4541 if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
4542 {
4543 static char retval[] = "l_c_\t%0,%1";
4544
4545 retval[1] = (dbl_p ? 'd' : 'w');
4546 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4547 return retval;
4548 }
4549 if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
4550 {
4551 static char retval[] = "s_c_\t%1,%0";
4552
4553 retval[1] = (dbl_p ? 'd' : 'w');
4554 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4555 return retval;
4556 }
4557 gcc_unreachable ();
4558 }
4559 \f
4560 /* Return true if CMP1 is a suitable second operand for integer ordering
4561 test CODE. See also the *sCC patterns in mips.md. */
4562
4563 static bool
4564 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
4565 {
4566 switch (code)
4567 {
4568 case GT:
4569 case GTU:
4570 return reg_or_0_operand (cmp1, VOIDmode);
4571
4572 case GE:
4573 case GEU:
4574 return !TARGET_MIPS16 && cmp1 == const1_rtx;
4575
4576 case LT:
4577 case LTU:
4578 return arith_operand (cmp1, VOIDmode);
4579
4580 case LE:
4581 return sle_operand (cmp1, VOIDmode);
4582
4583 case LEU:
4584 return sleu_operand (cmp1, VOIDmode);
4585
4586 default:
4587 gcc_unreachable ();
4588 }
4589 }
4590
4591 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
4592 integer ordering test *CODE, or if an equivalent combination can
4593 be formed by adjusting *CODE and *CMP1. When returning true, update
4594 *CODE and *CMP1 with the chosen code and operand, otherwise leave
4595 them alone. */
4596
4597 static bool
4598 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
4599 enum machine_mode mode)
4600 {
4601 HOST_WIDE_INT plus_one;
4602
4603 if (mips_int_order_operand_ok_p (*code, *cmp1))
4604 return true;
4605
4606 if (CONST_INT_P (*cmp1))
4607 switch (*code)
4608 {
4609 case LE:
4610 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4611 if (INTVAL (*cmp1) < plus_one)
4612 {
4613 *code = LT;
4614 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4615 return true;
4616 }
4617 break;
4618
4619 case LEU:
4620 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4621 if (plus_one != 0)
4622 {
4623 *code = LTU;
4624 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4625 return true;
4626 }
4627 break;
4628
4629 default:
4630 break;
4631 }
4632 return false;
4633 }
4634
4635 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
4636 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
4637 is nonnull, it's OK to set TARGET to the inverse of the result and
4638 flip *INVERT_PTR instead. */
4639
4640 static void
4641 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
4642 rtx target, rtx cmp0, rtx cmp1)
4643 {
4644 enum machine_mode mode;
4645
4646 /* First see if there is a MIPS instruction that can do this operation.
4647 If not, try doing the same for the inverse operation. If that also
4648 fails, force CMP1 into a register and try again. */
4649 mode = GET_MODE (cmp0);
4650 if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
4651 mips_emit_binary (code, target, cmp0, cmp1);
4652 else
4653 {
4654 enum rtx_code inv_code = reverse_condition (code);
4655 if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
4656 {
4657 cmp1 = force_reg (mode, cmp1);
4658 mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
4659 }
4660 else if (invert_ptr == 0)
4661 {
4662 rtx inv_target;
4663
4664 inv_target = mips_force_binary (GET_MODE (target),
4665 inv_code, cmp0, cmp1);
4666 mips_emit_binary (XOR, target, inv_target, const1_rtx);
4667 }
4668 else
4669 {
4670 *invert_ptr = !*invert_ptr;
4671 mips_emit_binary (inv_code, target, cmp0, cmp1);
4672 }
4673 }
4674 }
4675
4676 /* Return a register that is zero iff CMP0 and CMP1 are equal.
4677 The register will have the same mode as CMP0. */
4678
4679 static rtx
4680 mips_zero_if_equal (rtx cmp0, rtx cmp1)
4681 {
4682 if (cmp1 == const0_rtx)
4683 return cmp0;
4684
4685 if (uns_arith_operand (cmp1, VOIDmode))
4686 return expand_binop (GET_MODE (cmp0), xor_optab,
4687 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4688
4689 return expand_binop (GET_MODE (cmp0), sub_optab,
4690 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4691 }
4692
4693 /* Convert *CODE into a code that can be used in a floating-point
4694 scc instruction (C.cond.fmt). Return true if the values of
4695 the condition code registers will be inverted, with 0 indicating
4696 that the condition holds. */
4697
4698 static bool
4699 mips_reversed_fp_cond (enum rtx_code *code)
4700 {
4701 switch (*code)
4702 {
4703 case NE:
4704 case LTGT:
4705 case ORDERED:
4706 *code = reverse_condition_maybe_unordered (*code);
4707 return true;
4708
4709 default:
4710 return false;
4711 }
4712 }
4713
4714 /* Allocate a floating-point condition-code register of mode MODE.
4715
4716 These condition code registers are used for certain kinds
4717 of compound operation, such as compare and branches, vconds,
4718 and built-in functions. At expand time, their use is entirely
4719 controlled by MIPS-specific code and is entirely internal
4720 to these compound operations.
4721
4722 We could (and did in the past) expose condition-code values
4723 as pseudo registers and leave the register allocator to pick
4724 appropriate registers. The problem is that it is not practically
4725 possible for the rtl optimizers to guarantee that no spills will
4726 be needed, even when AVOID_CCMODE_COPIES is defined. We would
4727 therefore need spill and reload sequences to handle the worst case.
4728
4729 Although such sequences do exist, they are very expensive and are
4730 not something we'd want to use. This is especially true of CCV2 and
4731 CCV4, where all the shuffling would greatly outweigh whatever benefit
4732 the vectorization itself provides.
4733
4734 The main benefit of having more than one condition-code register
4735 is to allow the pipelining of operations, especially those involving
4736 comparisons and conditional moves. We don't really expect the
4737 registers to be live for long periods, and certainly never want
4738 them to be live across calls.
4739
4740 Also, there should be no penalty attached to using all the available
4741 registers. They are simply bits in the same underlying FPU control
4742 register.
4743
4744 We therefore expose the hardware registers from the outset and use
4745 a simple round-robin allocation scheme. */
4746
4747 static rtx
4748 mips_allocate_fcc (enum machine_mode mode)
4749 {
4750 unsigned int regno, count;
4751
4752 gcc_assert (TARGET_HARD_FLOAT && ISA_HAS_8CC);
4753
4754 if (mode == CCmode)
4755 count = 1;
4756 else if (mode == CCV2mode)
4757 count = 2;
4758 else if (mode == CCV4mode)
4759 count = 4;
4760 else
4761 gcc_unreachable ();
4762
4763 cfun->machine->next_fcc += -cfun->machine->next_fcc & (count - 1);
4764 if (cfun->machine->next_fcc > ST_REG_LAST - ST_REG_FIRST)
4765 cfun->machine->next_fcc = 0;
4766 regno = ST_REG_FIRST + cfun->machine->next_fcc;
4767 cfun->machine->next_fcc += count;
4768 return gen_rtx_REG (mode, regno);
4769 }
4770
4771 /* Convert a comparison into something that can be used in a branch or
4772 conditional move. On entry, *OP0 and *OP1 are the values being
4773 compared and *CODE is the code used to compare them.
4774
4775 Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
4776 If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
4777 otherwise any standard branch condition can be used. The standard branch
4778 conditions are:
4779
4780 - EQ or NE between two registers.
4781 - any comparison between a register and zero. */
4782
4783 static void
4784 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
4785 {
4786 rtx cmp_op0 = *op0;
4787 rtx cmp_op1 = *op1;
4788
4789 if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
4790 {
4791 if (!need_eq_ne_p && *op1 == const0_rtx)
4792 ;
4793 else if (*code == EQ || *code == NE)
4794 {
4795 if (need_eq_ne_p)
4796 {
4797 *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
4798 *op1 = const0_rtx;
4799 }
4800 else
4801 *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
4802 }
4803 else
4804 {
4805 /* The comparison needs a separate scc instruction. Store the
4806 result of the scc in *OP0 and compare it against zero. */
4807 bool invert = false;
4808 *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
4809 mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
4810 *code = (invert ? EQ : NE);
4811 *op1 = const0_rtx;
4812 }
4813 }
4814 else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
4815 {
4816 *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
4817 mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
4818 *code = NE;
4819 *op1 = const0_rtx;
4820 }
4821 else
4822 {
4823 enum rtx_code cmp_code;
4824
4825 /* Floating-point tests use a separate C.cond.fmt comparison to
4826 set a condition code register. The branch or conditional move
4827 will then compare that register against zero.
4828
4829 Set CMP_CODE to the code of the comparison instruction and
4830 *CODE to the code that the branch or move should use. */
4831 cmp_code = *code;
4832 *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
4833 *op0 = (ISA_HAS_8CC
4834 ? mips_allocate_fcc (CCmode)
4835 : gen_rtx_REG (CCmode, FPSW_REGNUM));
4836 *op1 = const0_rtx;
4837 mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
4838 }
4839 }
4840 \f
4841 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
4842 and OPERAND[3]. Store the result in OPERANDS[0].
4843
4844 On 64-bit targets, the mode of the comparison and target will always be
4845 SImode, thus possibly narrower than that of the comparison's operands. */
4846
4847 void
4848 mips_expand_scc (rtx operands[])
4849 {
4850 rtx target = operands[0];
4851 enum rtx_code code = GET_CODE (operands[1]);
4852 rtx op0 = operands[2];
4853 rtx op1 = operands[3];
4854
4855 gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
4856
4857 if (code == EQ || code == NE)
4858 {
4859 if (ISA_HAS_SEQ_SNE
4860 && reg_imm10_operand (op1, GET_MODE (op1)))
4861 mips_emit_binary (code, target, op0, op1);
4862 else
4863 {
4864 rtx zie = mips_zero_if_equal (op0, op1);
4865 mips_emit_binary (code, target, zie, const0_rtx);
4866 }
4867 }
4868 else
4869 mips_emit_int_order_test (code, 0, target, op0, op1);
4870 }
4871
4872 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
4873 CODE and jump to OPERANDS[3] if the condition holds. */
4874
4875 void
4876 mips_expand_conditional_branch (rtx *operands)
4877 {
4878 enum rtx_code code = GET_CODE (operands[0]);
4879 rtx op0 = operands[1];
4880 rtx op1 = operands[2];
4881 rtx condition;
4882
4883 mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
4884 condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
4885 emit_jump_insn (gen_condjump (condition, operands[3]));
4886 }
4887
4888 /* Implement:
4889
4890 (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
4891 (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS)) */
4892
4893 void
4894 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
4895 enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
4896 {
4897 rtx cmp_result;
4898 bool reversed_p;
4899
4900 reversed_p = mips_reversed_fp_cond (&cond);
4901 cmp_result = mips_allocate_fcc (CCV2mode);
4902 emit_insn (gen_scc_ps (cmp_result,
4903 gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
4904 if (reversed_p)
4905 emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
4906 cmp_result));
4907 else
4908 emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
4909 cmp_result));
4910 }
4911
4912 /* Perform the comparison in OPERANDS[1]. Move OPERANDS[2] into OPERANDS[0]
4913 if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0]. */
4914
4915 void
4916 mips_expand_conditional_move (rtx *operands)
4917 {
4918 rtx cond;
4919 enum rtx_code code = GET_CODE (operands[1]);
4920 rtx op0 = XEXP (operands[1], 0);
4921 rtx op1 = XEXP (operands[1], 1);
4922
4923 mips_emit_compare (&code, &op0, &op1, true);
4924 cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
4925 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
4926 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
4927 operands[2], operands[3])));
4928 }
4929
4930 /* Perform the comparison in COMPARISON, then trap if the condition holds. */
4931
4932 void
4933 mips_expand_conditional_trap (rtx comparison)
4934 {
4935 rtx op0, op1;
4936 enum machine_mode mode;
4937 enum rtx_code code;
4938
4939 /* MIPS conditional trap instructions don't have GT or LE flavors,
4940 so we must swap the operands and convert to LT and GE respectively. */
4941 code = GET_CODE (comparison);
4942 switch (code)
4943 {
4944 case GT:
4945 case LE:
4946 case GTU:
4947 case LEU:
4948 code = swap_condition (code);
4949 op0 = XEXP (comparison, 1);
4950 op1 = XEXP (comparison, 0);
4951 break;
4952
4953 default:
4954 op0 = XEXP (comparison, 0);
4955 op1 = XEXP (comparison, 1);
4956 break;
4957 }
4958
4959 mode = GET_MODE (XEXP (comparison, 0));
4960 op0 = force_reg (mode, op0);
4961 if (!arith_operand (op1, mode))
4962 op1 = force_reg (mode, op1);
4963
4964 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
4965 gen_rtx_fmt_ee (code, mode, op0, op1),
4966 const0_rtx));
4967 }
4968 \f
4969 /* Initialize *CUM for a call to a function of type FNTYPE. */
4970
4971 void
4972 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
4973 {
4974 memset (cum, 0, sizeof (*cum));
4975 cum->prototype = (fntype && prototype_p (fntype));
4976 cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
4977 }
4978
4979 /* Fill INFO with information about a single argument. CUM is the
4980 cumulative state for earlier arguments. MODE is the mode of this
4981 argument and TYPE is its type (if known). NAMED is true if this
4982 is a named (fixed) argument rather than a variable one. */
4983
4984 static void
4985 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
4986 enum machine_mode mode, const_tree type, bool named)
4987 {
4988 bool doubleword_aligned_p;
4989 unsigned int num_bytes, num_words, max_regs;
4990
4991 /* Work out the size of the argument. */
4992 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
4993 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4994
4995 /* Decide whether it should go in a floating-point register, assuming
4996 one is free. Later code checks for availability.
4997
4998 The checks against UNITS_PER_FPVALUE handle the soft-float and
4999 single-float cases. */
5000 switch (mips_abi)
5001 {
5002 case ABI_EABI:
5003 /* The EABI conventions have traditionally been defined in terms
5004 of TYPE_MODE, regardless of the actual type. */
5005 info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
5006 || mode == V2SFmode)
5007 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5008 break;
5009
5010 case ABI_32:
5011 case ABI_O64:
5012 /* Only leading floating-point scalars are passed in
5013 floating-point registers. We also handle vector floats the same
5014 say, which is OK because they are not covered by the standard ABI. */
5015 info->fpr_p = (!cum->gp_reg_found
5016 && cum->arg_number < 2
5017 && (type == 0
5018 || SCALAR_FLOAT_TYPE_P (type)
5019 || VECTOR_FLOAT_TYPE_P (type))
5020 && (GET_MODE_CLASS (mode) == MODE_FLOAT
5021 || mode == V2SFmode)
5022 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5023 break;
5024
5025 case ABI_N32:
5026 case ABI_64:
5027 /* Scalar, complex and vector floating-point types are passed in
5028 floating-point registers, as long as this is a named rather
5029 than a variable argument. */
5030 info->fpr_p = (named
5031 && (type == 0 || FLOAT_TYPE_P (type))
5032 && (GET_MODE_CLASS (mode) == MODE_FLOAT
5033 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5034 || mode == V2SFmode)
5035 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
5036
5037 /* ??? According to the ABI documentation, the real and imaginary
5038 parts of complex floats should be passed in individual registers.
5039 The real and imaginary parts of stack arguments are supposed
5040 to be contiguous and there should be an extra word of padding
5041 at the end.
5042
5043 This has two problems. First, it makes it impossible to use a
5044 single "void *" va_list type, since register and stack arguments
5045 are passed differently. (At the time of writing, MIPSpro cannot
5046 handle complex float varargs correctly.) Second, it's unclear
5047 what should happen when there is only one register free.
5048
5049 For now, we assume that named complex floats should go into FPRs
5050 if there are two FPRs free, otherwise they should be passed in the
5051 same way as a struct containing two floats. */
5052 if (info->fpr_p
5053 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5054 && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
5055 {
5056 if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
5057 info->fpr_p = false;
5058 else
5059 num_words = 2;
5060 }
5061 break;
5062
5063 default:
5064 gcc_unreachable ();
5065 }
5066
5067 /* See whether the argument has doubleword alignment. */
5068 doubleword_aligned_p = (mips_function_arg_boundary (mode, type)
5069 > BITS_PER_WORD);
5070
5071 /* Set REG_OFFSET to the register count we're interested in.
5072 The EABI allocates the floating-point registers separately,
5073 but the other ABIs allocate them like integer registers. */
5074 info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
5075 ? cum->num_fprs
5076 : cum->num_gprs);
5077
5078 /* Advance to an even register if the argument is doubleword-aligned. */
5079 if (doubleword_aligned_p)
5080 info->reg_offset += info->reg_offset & 1;
5081
5082 /* Work out the offset of a stack argument. */
5083 info->stack_offset = cum->stack_words;
5084 if (doubleword_aligned_p)
5085 info->stack_offset += info->stack_offset & 1;
5086
5087 max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
5088
5089 /* Partition the argument between registers and stack. */
5090 info->reg_words = MIN (num_words, max_regs);
5091 info->stack_words = num_words - info->reg_words;
5092 }
5093
5094 /* INFO describes a register argument that has the normal format for the
5095 argument's mode. Return the register it uses, assuming that FPRs are
5096 available if HARD_FLOAT_P. */
5097
5098 static unsigned int
5099 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
5100 {
5101 if (!info->fpr_p || !hard_float_p)
5102 return GP_ARG_FIRST + info->reg_offset;
5103 else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
5104 /* In o32, the second argument is always passed in $f14
5105 for TARGET_DOUBLE_FLOAT, regardless of whether the
5106 first argument was a word or doubleword. */
5107 return FP_ARG_FIRST + 2;
5108 else
5109 return FP_ARG_FIRST + info->reg_offset;
5110 }
5111
5112 /* Implement TARGET_STRICT_ARGUMENT_NAMING. */
5113
5114 static bool
5115 mips_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
5116 {
5117 return !TARGET_OLDABI;
5118 }
5119
5120 /* Implement TARGET_FUNCTION_ARG. */
5121
5122 static rtx
5123 mips_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
5124 const_tree type, bool named)
5125 {
5126 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5127 struct mips_arg_info info;
5128
5129 /* We will be called with a mode of VOIDmode after the last argument
5130 has been seen. Whatever we return will be passed to the call expander.
5131 If we need a MIPS16 fp_code, return a REG with the code stored as
5132 the mode. */
5133 if (mode == VOIDmode)
5134 {
5135 if (TARGET_MIPS16 && cum->fp_code != 0)
5136 return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
5137 else
5138 return NULL;
5139 }
5140
5141 mips_get_arg_info (&info, cum, mode, type, named);
5142
5143 /* Return straight away if the whole argument is passed on the stack. */
5144 if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
5145 return NULL;
5146
5147 /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
5148 contains a double in its entirety, then that 64-bit chunk is passed
5149 in a floating-point register. */
5150 if (TARGET_NEWABI
5151 && TARGET_HARD_FLOAT
5152 && named
5153 && type != 0
5154 && TREE_CODE (type) == RECORD_TYPE
5155 && TYPE_SIZE_UNIT (type)
5156 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
5157 {
5158 tree field;
5159
5160 /* First check to see if there is any such field. */
5161 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5162 if (TREE_CODE (field) == FIELD_DECL
5163 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5164 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
5165 && tree_fits_shwi_p (bit_position (field))
5166 && int_bit_position (field) % BITS_PER_WORD == 0)
5167 break;
5168
5169 if (field != 0)
5170 {
5171 /* Now handle the special case by returning a PARALLEL
5172 indicating where each 64-bit chunk goes. INFO.REG_WORDS
5173 chunks are passed in registers. */
5174 unsigned int i;
5175 HOST_WIDE_INT bitpos;
5176 rtx ret;
5177
5178 /* assign_parms checks the mode of ENTRY_PARM, so we must
5179 use the actual mode here. */
5180 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
5181
5182 bitpos = 0;
5183 field = TYPE_FIELDS (type);
5184 for (i = 0; i < info.reg_words; i++)
5185 {
5186 rtx reg;
5187
5188 for (; field; field = DECL_CHAIN (field))
5189 if (TREE_CODE (field) == FIELD_DECL
5190 && int_bit_position (field) >= bitpos)
5191 break;
5192
5193 if (field
5194 && int_bit_position (field) == bitpos
5195 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5196 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
5197 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
5198 else
5199 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
5200
5201 XVECEXP (ret, 0, i)
5202 = gen_rtx_EXPR_LIST (VOIDmode, reg,
5203 GEN_INT (bitpos / BITS_PER_UNIT));
5204
5205 bitpos += BITS_PER_WORD;
5206 }
5207 return ret;
5208 }
5209 }
5210
5211 /* Handle the n32/n64 conventions for passing complex floating-point
5212 arguments in FPR pairs. The real part goes in the lower register
5213 and the imaginary part goes in the upper register. */
5214 if (TARGET_NEWABI
5215 && info.fpr_p
5216 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5217 {
5218 rtx real, imag;
5219 enum machine_mode inner;
5220 unsigned int regno;
5221
5222 inner = GET_MODE_INNER (mode);
5223 regno = FP_ARG_FIRST + info.reg_offset;
5224 if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
5225 {
5226 /* Real part in registers, imaginary part on stack. */
5227 gcc_assert (info.stack_words == info.reg_words);
5228 return gen_rtx_REG (inner, regno);
5229 }
5230 else
5231 {
5232 gcc_assert (info.stack_words == 0);
5233 real = gen_rtx_EXPR_LIST (VOIDmode,
5234 gen_rtx_REG (inner, regno),
5235 const0_rtx);
5236 imag = gen_rtx_EXPR_LIST (VOIDmode,
5237 gen_rtx_REG (inner,
5238 regno + info.reg_words / 2),
5239 GEN_INT (GET_MODE_SIZE (inner)));
5240 return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
5241 }
5242 }
5243
5244 return gen_rtx_REG (mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
5245 }
5246
5247 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
5248
5249 static void
5250 mips_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
5251 const_tree type, bool named)
5252 {
5253 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5254 struct mips_arg_info info;
5255
5256 mips_get_arg_info (&info, cum, mode, type, named);
5257
5258 if (!info.fpr_p)
5259 cum->gp_reg_found = true;
5260
5261 /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
5262 an explanation of what this code does. It assumes that we're using
5263 either the o32 or the o64 ABI, both of which pass at most 2 arguments
5264 in FPRs. */
5265 if (cum->arg_number < 2 && info.fpr_p)
5266 cum->fp_code += (mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
5267
5268 /* Advance the register count. This has the effect of setting
5269 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
5270 argument required us to skip the final GPR and pass the whole
5271 argument on the stack. */
5272 if (mips_abi != ABI_EABI || !info.fpr_p)
5273 cum->num_gprs = info.reg_offset + info.reg_words;
5274 else if (info.reg_words > 0)
5275 cum->num_fprs += MAX_FPRS_PER_FMT;
5276
5277 /* Advance the stack word count. */
5278 if (info.stack_words > 0)
5279 cum->stack_words = info.stack_offset + info.stack_words;
5280
5281 cum->arg_number++;
5282 }
5283
5284 /* Implement TARGET_ARG_PARTIAL_BYTES. */
5285
5286 static int
5287 mips_arg_partial_bytes (cumulative_args_t cum,
5288 enum machine_mode mode, tree type, bool named)
5289 {
5290 struct mips_arg_info info;
5291
5292 mips_get_arg_info (&info, get_cumulative_args (cum), mode, type, named);
5293 return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
5294 }
5295
5296 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Every parameter gets at
5297 least PARM_BOUNDARY bits of alignment, but will be given anything up
5298 to STACK_BOUNDARY bits if the type requires it. */
5299
5300 static unsigned int
5301 mips_function_arg_boundary (enum machine_mode mode, const_tree type)
5302 {
5303 unsigned int alignment;
5304
5305 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
5306 if (alignment < PARM_BOUNDARY)
5307 alignment = PARM_BOUNDARY;
5308 if (alignment > STACK_BOUNDARY)
5309 alignment = STACK_BOUNDARY;
5310 return alignment;
5311 }
5312
5313 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
5314 upward rather than downward. In other words, return true if the
5315 first byte of the stack slot has useful data, false if the last
5316 byte does. */
5317
5318 bool
5319 mips_pad_arg_upward (enum machine_mode mode, const_tree type)
5320 {
5321 /* On little-endian targets, the first byte of every stack argument
5322 is passed in the first byte of the stack slot. */
5323 if (!BYTES_BIG_ENDIAN)
5324 return true;
5325
5326 /* Otherwise, integral types are padded downward: the last byte of a
5327 stack argument is passed in the last byte of the stack slot. */
5328 if (type != 0
5329 ? (INTEGRAL_TYPE_P (type)
5330 || POINTER_TYPE_P (type)
5331 || FIXED_POINT_TYPE_P (type))
5332 : (SCALAR_INT_MODE_P (mode)
5333 || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
5334 return false;
5335
5336 /* Big-endian o64 pads floating-point arguments downward. */
5337 if (mips_abi == ABI_O64)
5338 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5339 return false;
5340
5341 /* Other types are padded upward for o32, o64, n32 and n64. */
5342 if (mips_abi != ABI_EABI)
5343 return true;
5344
5345 /* Arguments smaller than a stack slot are padded downward. */
5346 if (mode != BLKmode)
5347 return GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY;
5348 else
5349 return int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT);
5350 }
5351
5352 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...). Return !BYTES_BIG_ENDIAN
5353 if the least significant byte of the register has useful data. Return
5354 the opposite if the most significant byte does. */
5355
5356 bool
5357 mips_pad_reg_upward (enum machine_mode mode, tree type)
5358 {
5359 /* No shifting is required for floating-point arguments. */
5360 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5361 return !BYTES_BIG_ENDIAN;
5362
5363 /* Otherwise, apply the same padding to register arguments as we do
5364 to stack arguments. */
5365 return mips_pad_arg_upward (mode, type);
5366 }
5367
5368 /* Return nonzero when an argument must be passed by reference. */
5369
5370 static bool
5371 mips_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
5372 enum machine_mode mode, const_tree type,
5373 bool named ATTRIBUTE_UNUSED)
5374 {
5375 if (mips_abi == ABI_EABI)
5376 {
5377 int size;
5378
5379 /* ??? How should SCmode be handled? */
5380 if (mode == DImode || mode == DFmode
5381 || mode == DQmode || mode == UDQmode
5382 || mode == DAmode || mode == UDAmode)
5383 return 0;
5384
5385 size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5386 return size == -1 || size > UNITS_PER_WORD;
5387 }
5388 else
5389 {
5390 /* If we have a variable-sized parameter, we have no choice. */
5391 return targetm.calls.must_pass_in_stack (mode, type);
5392 }
5393 }
5394
5395 /* Implement TARGET_CALLEE_COPIES. */
5396
5397 static bool
5398 mips_callee_copies (cumulative_args_t cum ATTRIBUTE_UNUSED,
5399 enum machine_mode mode ATTRIBUTE_UNUSED,
5400 const_tree type ATTRIBUTE_UNUSED, bool named)
5401 {
5402 return mips_abi == ABI_EABI && named;
5403 }
5404 \f
5405 /* See whether VALTYPE is a record whose fields should be returned in
5406 floating-point registers. If so, return the number of fields and
5407 list them in FIELDS (which should have two elements). Return 0
5408 otherwise.
5409
5410 For n32 & n64, a structure with one or two fields is returned in
5411 floating-point registers as long as every field has a floating-point
5412 type. */
5413
5414 static int
5415 mips_fpr_return_fields (const_tree valtype, tree *fields)
5416 {
5417 tree field;
5418 int i;
5419
5420 if (!TARGET_NEWABI)
5421 return 0;
5422
5423 if (TREE_CODE (valtype) != RECORD_TYPE)
5424 return 0;
5425
5426 i = 0;
5427 for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
5428 {
5429 if (TREE_CODE (field) != FIELD_DECL)
5430 continue;
5431
5432 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
5433 return 0;
5434
5435 if (i == 2)
5436 return 0;
5437
5438 fields[i++] = field;
5439 }
5440 return i;
5441 }
5442
5443 /* Implement TARGET_RETURN_IN_MSB. For n32 & n64, we should return
5444 a value in the most significant part of $2/$3 if:
5445
5446 - the target is big-endian;
5447
5448 - the value has a structure or union type (we generalize this to
5449 cover aggregates from other languages too); and
5450
5451 - the structure is not returned in floating-point registers. */
5452
5453 static bool
5454 mips_return_in_msb (const_tree valtype)
5455 {
5456 tree fields[2];
5457
5458 return (TARGET_NEWABI
5459 && TARGET_BIG_ENDIAN
5460 && AGGREGATE_TYPE_P (valtype)
5461 && mips_fpr_return_fields (valtype, fields) == 0);
5462 }
5463
5464 /* Return true if the function return value MODE will get returned in a
5465 floating-point register. */
5466
5467 static bool
5468 mips_return_mode_in_fpr_p (enum machine_mode mode)
5469 {
5470 return ((GET_MODE_CLASS (mode) == MODE_FLOAT
5471 || mode == V2SFmode
5472 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5473 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
5474 }
5475
5476 /* Return the representation of an FPR return register when the
5477 value being returned in FP_RETURN has mode VALUE_MODE and the
5478 return type itself has mode TYPE_MODE. On NewABI targets,
5479 the two modes may be different for structures like:
5480
5481 struct __attribute__((packed)) foo { float f; }
5482
5483 where we return the SFmode value of "f" in FP_RETURN, but where
5484 the structure itself has mode BLKmode. */
5485
5486 static rtx
5487 mips_return_fpr_single (enum machine_mode type_mode,
5488 enum machine_mode value_mode)
5489 {
5490 rtx x;
5491
5492 x = gen_rtx_REG (value_mode, FP_RETURN);
5493 if (type_mode != value_mode)
5494 {
5495 x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
5496 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
5497 }
5498 return x;
5499 }
5500
5501 /* Return a composite value in a pair of floating-point registers.
5502 MODE1 and OFFSET1 are the mode and byte offset for the first value,
5503 likewise MODE2 and OFFSET2 for the second. MODE is the mode of the
5504 complete value.
5505
5506 For n32 & n64, $f0 always holds the first value and $f2 the second.
5507 Otherwise the values are packed together as closely as possible. */
5508
5509 static rtx
5510 mips_return_fpr_pair (enum machine_mode mode,
5511 enum machine_mode mode1, HOST_WIDE_INT offset1,
5512 enum machine_mode mode2, HOST_WIDE_INT offset2)
5513 {
5514 int inc;
5515
5516 inc = (TARGET_NEWABI ? 2 : MAX_FPRS_PER_FMT);
5517 return gen_rtx_PARALLEL
5518 (mode,
5519 gen_rtvec (2,
5520 gen_rtx_EXPR_LIST (VOIDmode,
5521 gen_rtx_REG (mode1, FP_RETURN),
5522 GEN_INT (offset1)),
5523 gen_rtx_EXPR_LIST (VOIDmode,
5524 gen_rtx_REG (mode2, FP_RETURN + inc),
5525 GEN_INT (offset2))));
5526
5527 }
5528
5529 /* Implement TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE.
5530 For normal calls, VALTYPE is the return type and MODE is VOIDmode.
5531 For libcalls, VALTYPE is null and MODE is the mode of the return value. */
5532
5533 static rtx
5534 mips_function_value_1 (const_tree valtype, const_tree fn_decl_or_type,
5535 enum machine_mode mode)
5536 {
5537 if (valtype)
5538 {
5539 tree fields[2];
5540 int unsigned_p;
5541 const_tree func;
5542
5543 if (fn_decl_or_type && DECL_P (fn_decl_or_type))
5544 func = fn_decl_or_type;
5545 else
5546 func = NULL;
5547
5548 mode = TYPE_MODE (valtype);
5549 unsigned_p = TYPE_UNSIGNED (valtype);
5550
5551 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
5552 return values, promote the mode here too. */
5553 mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
5554
5555 /* Handle structures whose fields are returned in $f0/$f2. */
5556 switch (mips_fpr_return_fields (valtype, fields))
5557 {
5558 case 1:
5559 return mips_return_fpr_single (mode,
5560 TYPE_MODE (TREE_TYPE (fields[0])));
5561
5562 case 2:
5563 return mips_return_fpr_pair (mode,
5564 TYPE_MODE (TREE_TYPE (fields[0])),
5565 int_byte_position (fields[0]),
5566 TYPE_MODE (TREE_TYPE (fields[1])),
5567 int_byte_position (fields[1]));
5568 }
5569
5570 /* If a value is passed in the most significant part of a register, see
5571 whether we have to round the mode up to a whole number of words. */
5572 if (mips_return_in_msb (valtype))
5573 {
5574 HOST_WIDE_INT size = int_size_in_bytes (valtype);
5575 if (size % UNITS_PER_WORD != 0)
5576 {
5577 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
5578 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
5579 }
5580 }
5581
5582 /* For EABI, the class of return register depends entirely on MODE.
5583 For example, "struct { some_type x; }" and "union { some_type x; }"
5584 are returned in the same way as a bare "some_type" would be.
5585 Other ABIs only use FPRs for scalar, complex or vector types. */
5586 if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
5587 return gen_rtx_REG (mode, GP_RETURN);
5588 }
5589
5590 if (!TARGET_MIPS16)
5591 {
5592 /* Handle long doubles for n32 & n64. */
5593 if (mode == TFmode)
5594 return mips_return_fpr_pair (mode,
5595 DImode, 0,
5596 DImode, GET_MODE_SIZE (mode) / 2);
5597
5598 if (mips_return_mode_in_fpr_p (mode))
5599 {
5600 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5601 return mips_return_fpr_pair (mode,
5602 GET_MODE_INNER (mode), 0,
5603 GET_MODE_INNER (mode),
5604 GET_MODE_SIZE (mode) / 2);
5605 else
5606 return gen_rtx_REG (mode, FP_RETURN);
5607 }
5608 }
5609
5610 return gen_rtx_REG (mode, GP_RETURN);
5611 }
5612
5613 /* Implement TARGET_FUNCTION_VALUE. */
5614
5615 static rtx
5616 mips_function_value (const_tree valtype, const_tree fn_decl_or_type,
5617 bool outgoing ATTRIBUTE_UNUSED)
5618 {
5619 return mips_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
5620 }
5621
5622 /* Implement TARGET_LIBCALL_VALUE. */
5623
5624 static rtx
5625 mips_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
5626 {
5627 return mips_function_value_1 (NULL_TREE, NULL_TREE, mode);
5628 }
5629
5630 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
5631
5632 On the MIPS, R2 R3 and F0 F2 are the only register thus used.
5633 Currently, R2 and F0 are only implemented here (C has no complex type). */
5634
5635 static bool
5636 mips_function_value_regno_p (const unsigned int regno)
5637 {
5638 if (regno == GP_RETURN
5639 || regno == FP_RETURN
5640 || (LONG_DOUBLE_TYPE_SIZE == 128
5641 && FP_RETURN != GP_RETURN
5642 && regno == FP_RETURN + 2))
5643 return true;
5644
5645 return false;
5646 }
5647
5648 /* Implement TARGET_RETURN_IN_MEMORY. Under the o32 and o64 ABIs,
5649 all BLKmode objects are returned in memory. Under the n32, n64
5650 and embedded ABIs, small structures are returned in a register.
5651 Objects with varying size must still be returned in memory, of
5652 course. */
5653
5654 static bool
5655 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
5656 {
5657 return (TARGET_OLDABI
5658 ? TYPE_MODE (type) == BLKmode
5659 : !IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
5660 }
5661 \f
5662 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
5663
5664 static void
5665 mips_setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
5666 tree type, int *pretend_size ATTRIBUTE_UNUSED,
5667 int no_rtl)
5668 {
5669 CUMULATIVE_ARGS local_cum;
5670 int gp_saved, fp_saved;
5671
5672 /* The caller has advanced CUM up to, but not beyond, the last named
5673 argument. Advance a local copy of CUM past the last "real" named
5674 argument, to find out how many registers are left over. */
5675 local_cum = *get_cumulative_args (cum);
5676 mips_function_arg_advance (pack_cumulative_args (&local_cum), mode, type,
5677 true);
5678
5679 /* Found out how many registers we need to save. */
5680 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
5681 fp_saved = (EABI_FLOAT_VARARGS_P
5682 ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
5683 : 0);
5684
5685 if (!no_rtl)
5686 {
5687 if (gp_saved > 0)
5688 {
5689 rtx ptr, mem;
5690
5691 ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
5692 REG_PARM_STACK_SPACE (cfun->decl)
5693 - gp_saved * UNITS_PER_WORD);
5694 mem = gen_frame_mem (BLKmode, ptr);
5695 set_mem_alias_set (mem, get_varargs_alias_set ());
5696
5697 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
5698 mem, gp_saved);
5699 }
5700 if (fp_saved > 0)
5701 {
5702 /* We can't use move_block_from_reg, because it will use
5703 the wrong mode. */
5704 enum machine_mode mode;
5705 int off, i;
5706
5707 /* Set OFF to the offset from virtual_incoming_args_rtx of
5708 the first float register. The FP save area lies below
5709 the integer one, and is aligned to UNITS_PER_FPVALUE bytes. */
5710 off = (-gp_saved * UNITS_PER_WORD) & -UNITS_PER_FPVALUE;
5711 off -= fp_saved * UNITS_PER_FPREG;
5712
5713 mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
5714
5715 for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
5716 i += MAX_FPRS_PER_FMT)
5717 {
5718 rtx ptr, mem;
5719
5720 ptr = plus_constant (Pmode, virtual_incoming_args_rtx, off);
5721 mem = gen_frame_mem (mode, ptr);
5722 set_mem_alias_set (mem, get_varargs_alias_set ());
5723 mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
5724 off += UNITS_PER_HWFPVALUE;
5725 }
5726 }
5727 }
5728 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
5729 cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
5730 + fp_saved * UNITS_PER_FPREG);
5731 }
5732
5733 /* Implement TARGET_BUILTIN_VA_LIST. */
5734
5735 static tree
5736 mips_build_builtin_va_list (void)
5737 {
5738 if (EABI_FLOAT_VARARGS_P)
5739 {
5740 /* We keep 3 pointers, and two offsets.
5741
5742 Two pointers are to the overflow area, which starts at the CFA.
5743 One of these is constant, for addressing into the GPR save area
5744 below it. The other is advanced up the stack through the
5745 overflow region.
5746
5747 The third pointer is to the bottom of the GPR save area.
5748 Since the FPR save area is just below it, we can address
5749 FPR slots off this pointer.
5750
5751 We also keep two one-byte offsets, which are to be subtracted
5752 from the constant pointers to yield addresses in the GPR and
5753 FPR save areas. These are downcounted as float or non-float
5754 arguments are used, and when they get to zero, the argument
5755 must be obtained from the overflow region. */
5756 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
5757 tree array, index;
5758
5759 record = lang_hooks.types.make_type (RECORD_TYPE);
5760
5761 f_ovfl = build_decl (BUILTINS_LOCATION,
5762 FIELD_DECL, get_identifier ("__overflow_argptr"),
5763 ptr_type_node);
5764 f_gtop = build_decl (BUILTINS_LOCATION,
5765 FIELD_DECL, get_identifier ("__gpr_top"),
5766 ptr_type_node);
5767 f_ftop = build_decl (BUILTINS_LOCATION,
5768 FIELD_DECL, get_identifier ("__fpr_top"),
5769 ptr_type_node);
5770 f_goff = build_decl (BUILTINS_LOCATION,
5771 FIELD_DECL, get_identifier ("__gpr_offset"),
5772 unsigned_char_type_node);
5773 f_foff = build_decl (BUILTINS_LOCATION,
5774 FIELD_DECL, get_identifier ("__fpr_offset"),
5775 unsigned_char_type_node);
5776 /* Explicitly pad to the size of a pointer, so that -Wpadded won't
5777 warn on every user file. */
5778 index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
5779 array = build_array_type (unsigned_char_type_node,
5780 build_index_type (index));
5781 f_res = build_decl (BUILTINS_LOCATION,
5782 FIELD_DECL, get_identifier ("__reserved"), array);
5783
5784 DECL_FIELD_CONTEXT (f_ovfl) = record;
5785 DECL_FIELD_CONTEXT (f_gtop) = record;
5786 DECL_FIELD_CONTEXT (f_ftop) = record;
5787 DECL_FIELD_CONTEXT (f_goff) = record;
5788 DECL_FIELD_CONTEXT (f_foff) = record;
5789 DECL_FIELD_CONTEXT (f_res) = record;
5790
5791 TYPE_FIELDS (record) = f_ovfl;
5792 DECL_CHAIN (f_ovfl) = f_gtop;
5793 DECL_CHAIN (f_gtop) = f_ftop;
5794 DECL_CHAIN (f_ftop) = f_goff;
5795 DECL_CHAIN (f_goff) = f_foff;
5796 DECL_CHAIN (f_foff) = f_res;
5797
5798 layout_type (record);
5799 return record;
5800 }
5801 else
5802 /* Otherwise, we use 'void *'. */
5803 return ptr_type_node;
5804 }
5805
5806 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
5807
5808 static void
5809 mips_va_start (tree valist, rtx nextarg)
5810 {
5811 if (EABI_FLOAT_VARARGS_P)
5812 {
5813 const CUMULATIVE_ARGS *cum;
5814 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5815 tree ovfl, gtop, ftop, goff, foff;
5816 tree t;
5817 int gpr_save_area_size;
5818 int fpr_save_area_size;
5819 int fpr_offset;
5820
5821 cum = &crtl->args.info;
5822 gpr_save_area_size
5823 = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
5824 fpr_save_area_size
5825 = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
5826
5827 f_ovfl = TYPE_FIELDS (va_list_type_node);
5828 f_gtop = DECL_CHAIN (f_ovfl);
5829 f_ftop = DECL_CHAIN (f_gtop);
5830 f_goff = DECL_CHAIN (f_ftop);
5831 f_foff = DECL_CHAIN (f_goff);
5832
5833 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5834 NULL_TREE);
5835 gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
5836 NULL_TREE);
5837 ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
5838 NULL_TREE);
5839 goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
5840 NULL_TREE);
5841 foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
5842 NULL_TREE);
5843
5844 /* Emit code to initialize OVFL, which points to the next varargs
5845 stack argument. CUM->STACK_WORDS gives the number of stack
5846 words used by named arguments. */
5847 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
5848 if (cum->stack_words > 0)
5849 t = fold_build_pointer_plus_hwi (t, cum->stack_words * UNITS_PER_WORD);
5850 t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5851 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5852
5853 /* Emit code to initialize GTOP, the top of the GPR save area. */
5854 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
5855 t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
5856 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5857
5858 /* Emit code to initialize FTOP, the top of the FPR save area.
5859 This address is gpr_save_area_bytes below GTOP, rounded
5860 down to the next fp-aligned boundary. */
5861 t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
5862 fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
5863 fpr_offset &= -UNITS_PER_FPVALUE;
5864 if (fpr_offset)
5865 t = fold_build_pointer_plus_hwi (t, -fpr_offset);
5866 t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
5867 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5868
5869 /* Emit code to initialize GOFF, the offset from GTOP of the
5870 next GPR argument. */
5871 t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
5872 build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
5873 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5874
5875 /* Likewise emit code to initialize FOFF, the offset from FTOP
5876 of the next FPR argument. */
5877 t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
5878 build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
5879 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5880 }
5881 else
5882 {
5883 nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
5884 std_expand_builtin_va_start (valist, nextarg);
5885 }
5886 }
5887
5888 /* Like std_gimplify_va_arg_expr, but apply alignment to zero-sized
5889 types as well. */
5890
5891 static tree
5892 mips_std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
5893 gimple_seq *post_p)
5894 {
5895 tree addr, t, type_size, rounded_size, valist_tmp;
5896 unsigned HOST_WIDE_INT align, boundary;
5897 bool indirect;
5898
5899 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
5900 if (indirect)
5901 type = build_pointer_type (type);
5902
5903 align = PARM_BOUNDARY / BITS_PER_UNIT;
5904 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
5905
5906 /* When we align parameter on stack for caller, if the parameter
5907 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
5908 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
5909 here with caller. */
5910 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
5911 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
5912
5913 boundary /= BITS_PER_UNIT;
5914
5915 /* Hoist the valist value into a temporary for the moment. */
5916 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
5917
5918 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
5919 requires greater alignment, we must perform dynamic alignment. */
5920 if (boundary > align)
5921 {
5922 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
5923 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
5924 gimplify_and_add (t, pre_p);
5925
5926 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
5927 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
5928 valist_tmp,
5929 build_int_cst (TREE_TYPE (valist), -boundary)));
5930 gimplify_and_add (t, pre_p);
5931 }
5932 else
5933 boundary = align;
5934
5935 /* If the actual alignment is less than the alignment of the type,
5936 adjust the type accordingly so that we don't assume strict alignment
5937 when dereferencing the pointer. */
5938 boundary *= BITS_PER_UNIT;
5939 if (boundary < TYPE_ALIGN (type))
5940 {
5941 type = build_variant_type_copy (type);
5942 TYPE_ALIGN (type) = boundary;
5943 }
5944
5945 /* Compute the rounded size of the type. */
5946 type_size = size_in_bytes (type);
5947 rounded_size = round_up (type_size, align);
5948
5949 /* Reduce rounded_size so it's sharable with the postqueue. */
5950 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
5951
5952 /* Get AP. */
5953 addr = valist_tmp;
5954 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
5955 {
5956 /* Small args are padded downward. */
5957 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
5958 rounded_size, size_int (align));
5959 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
5960 size_binop (MINUS_EXPR, rounded_size, type_size));
5961 addr = fold_build_pointer_plus (addr, t);
5962 }
5963
5964 /* Compute new value for AP. */
5965 t = fold_build_pointer_plus (valist_tmp, rounded_size);
5966 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
5967 gimplify_and_add (t, pre_p);
5968
5969 addr = fold_convert (build_pointer_type (type), addr);
5970
5971 if (indirect)
5972 addr = build_va_arg_indirect_ref (addr);
5973
5974 return build_va_arg_indirect_ref (addr);
5975 }
5976
5977 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR. */
5978
5979 static tree
5980 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
5981 gimple_seq *post_p)
5982 {
5983 tree addr;
5984 bool indirect_p;
5985
5986 indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
5987 if (indirect_p)
5988 type = build_pointer_type (type);
5989
5990 if (!EABI_FLOAT_VARARGS_P)
5991 addr = mips_std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
5992 else
5993 {
5994 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5995 tree ovfl, top, off, align;
5996 HOST_WIDE_INT size, rsize, osize;
5997 tree t, u;
5998
5999 f_ovfl = TYPE_FIELDS (va_list_type_node);
6000 f_gtop = DECL_CHAIN (f_ovfl);
6001 f_ftop = DECL_CHAIN (f_gtop);
6002 f_goff = DECL_CHAIN (f_ftop);
6003 f_foff = DECL_CHAIN (f_goff);
6004
6005 /* Let:
6006
6007 TOP be the top of the GPR or FPR save area;
6008 OFF be the offset from TOP of the next register;
6009 ADDR_RTX be the address of the argument;
6010 SIZE be the number of bytes in the argument type;
6011 RSIZE be the number of bytes used to store the argument
6012 when it's in the register save area; and
6013 OSIZE be the number of bytes used to store it when it's
6014 in the stack overflow area.
6015
6016 The code we want is:
6017
6018 1: off &= -rsize; // round down
6019 2: if (off != 0)
6020 3: {
6021 4: addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
6022 5: off -= rsize;
6023 6: }
6024 7: else
6025 8: {
6026 9: ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
6027 10: addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
6028 11: ovfl += osize;
6029 14: }
6030
6031 [1] and [9] can sometimes be optimized away. */
6032
6033 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
6034 NULL_TREE);
6035 size = int_size_in_bytes (type);
6036
6037 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
6038 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
6039 {
6040 top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
6041 unshare_expr (valist), f_ftop, NULL_TREE);
6042 off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
6043 unshare_expr (valist), f_foff, NULL_TREE);
6044
6045 /* When va_start saves FPR arguments to the stack, each slot
6046 takes up UNITS_PER_HWFPVALUE bytes, regardless of the
6047 argument's precision. */
6048 rsize = UNITS_PER_HWFPVALUE;
6049
6050 /* Overflow arguments are padded to UNITS_PER_WORD bytes
6051 (= PARM_BOUNDARY bits). This can be different from RSIZE
6052 in two cases:
6053
6054 (1) On 32-bit targets when TYPE is a structure such as:
6055
6056 struct s { float f; };
6057
6058 Such structures are passed in paired FPRs, so RSIZE
6059 will be 8 bytes. However, the structure only takes
6060 up 4 bytes of memory, so OSIZE will only be 4.
6061
6062 (2) In combinations such as -mgp64 -msingle-float
6063 -fshort-double. Doubles passed in registers will then take
6064 up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
6065 stack take up UNITS_PER_WORD bytes. */
6066 osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
6067 }
6068 else
6069 {
6070 top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
6071 unshare_expr (valist), f_gtop, NULL_TREE);
6072 off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
6073 unshare_expr (valist), f_goff, NULL_TREE);
6074 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
6075 if (rsize > UNITS_PER_WORD)
6076 {
6077 /* [1] Emit code for: off &= -rsize. */
6078 t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
6079 build_int_cst (TREE_TYPE (off), -rsize));
6080 gimplify_assign (unshare_expr (off), t, pre_p);
6081 }
6082 osize = rsize;
6083 }
6084
6085 /* [2] Emit code to branch if off == 0. */
6086 t = build2 (NE_EXPR, boolean_type_node, unshare_expr (off),
6087 build_int_cst (TREE_TYPE (off), 0));
6088 addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
6089
6090 /* [5] Emit code for: off -= rsize. We do this as a form of
6091 post-decrement not available to C. */
6092 t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
6093 t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
6094
6095 /* [4] Emit code for:
6096 addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0). */
6097 t = fold_convert (sizetype, t);
6098 t = fold_build1 (NEGATE_EXPR, sizetype, t);
6099 t = fold_build_pointer_plus (top, t);
6100 if (BYTES_BIG_ENDIAN && rsize > size)
6101 t = fold_build_pointer_plus_hwi (t, rsize - size);
6102 COND_EXPR_THEN (addr) = t;
6103
6104 if (osize > UNITS_PER_WORD)
6105 {
6106 /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize. */
6107 t = fold_build_pointer_plus_hwi (unshare_expr (ovfl), osize - 1);
6108 u = build_int_cst (TREE_TYPE (t), -osize);
6109 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
6110 align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
6111 unshare_expr (ovfl), t);
6112 }
6113 else
6114 align = NULL;
6115
6116 /* [10, 11] Emit code for:
6117 addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
6118 ovfl += osize. */
6119 u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
6120 t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
6121 if (BYTES_BIG_ENDIAN && osize > size)
6122 t = fold_build_pointer_plus_hwi (t, osize - size);
6123
6124 /* String [9] and [10, 11] together. */
6125 if (align)
6126 t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
6127 COND_EXPR_ELSE (addr) = t;
6128
6129 addr = fold_convert (build_pointer_type (type), addr);
6130 addr = build_va_arg_indirect_ref (addr);
6131 }
6132
6133 if (indirect_p)
6134 addr = build_va_arg_indirect_ref (addr);
6135
6136 return addr;
6137 }
6138 \f
6139 /* Declare a unique, locally-binding function called NAME, then start
6140 its definition. */
6141
6142 static void
6143 mips_start_unique_function (const char *name)
6144 {
6145 tree decl;
6146
6147 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
6148 get_identifier (name),
6149 build_function_type_list (void_type_node, NULL_TREE));
6150 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
6151 NULL_TREE, void_type_node);
6152 TREE_PUBLIC (decl) = 1;
6153 TREE_STATIC (decl) = 1;
6154
6155 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
6156
6157 targetm.asm_out.unique_section (decl, 0);
6158 switch_to_section (get_named_section (decl, NULL, 0));
6159
6160 targetm.asm_out.globalize_label (asm_out_file, name);
6161 fputs ("\t.hidden\t", asm_out_file);
6162 assemble_name (asm_out_file, name);
6163 putc ('\n', asm_out_file);
6164 }
6165
6166 /* Start a definition of function NAME. MIPS16_P indicates whether the
6167 function contains MIPS16 code. */
6168
6169 static void
6170 mips_start_function_definition (const char *name, bool mips16_p)
6171 {
6172 if (mips16_p)
6173 fprintf (asm_out_file, "\t.set\tmips16\n");
6174 else
6175 fprintf (asm_out_file, "\t.set\tnomips16\n");
6176
6177 if (TARGET_MICROMIPS)
6178 fprintf (asm_out_file, "\t.set\tmicromips\n");
6179 #ifdef HAVE_GAS_MICROMIPS
6180 else
6181 fprintf (asm_out_file, "\t.set\tnomicromips\n");
6182 #endif
6183
6184 if (!flag_inhibit_size_directive)
6185 {
6186 fputs ("\t.ent\t", asm_out_file);
6187 assemble_name (asm_out_file, name);
6188 fputs ("\n", asm_out_file);
6189 }
6190
6191 ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
6192
6193 /* Start the definition proper. */
6194 assemble_name (asm_out_file, name);
6195 fputs (":\n", asm_out_file);
6196 }
6197
6198 /* End a function definition started by mips_start_function_definition. */
6199
6200 static void
6201 mips_end_function_definition (const char *name)
6202 {
6203 if (!flag_inhibit_size_directive)
6204 {
6205 fputs ("\t.end\t", asm_out_file);
6206 assemble_name (asm_out_file, name);
6207 fputs ("\n", asm_out_file);
6208 }
6209 }
6210 \f
6211 /* Output a definition of the __mips16_rdhwr function. */
6212
6213 static void
6214 mips_output_mips16_rdhwr (void)
6215 {
6216 const char *name;
6217
6218 name = "__mips16_rdhwr";
6219 mips_start_unique_function (name);
6220 mips_start_function_definition (name, false);
6221 fprintf (asm_out_file,
6222 "\t.set\tpush\n"
6223 "\t.set\tmips32r2\n"
6224 "\t.set\tnoreorder\n"
6225 "\trdhwr\t$3,$29\n"
6226 "\t.set\tpop\n"
6227 "\tj\t$31\n");
6228 mips_end_function_definition (name);
6229 }
6230 \f
6231 /* Return true if calls to X can use R_MIPS_CALL* relocations. */
6232
6233 static bool
6234 mips_ok_for_lazy_binding_p (rtx x)
6235 {
6236 return (TARGET_USE_GOT
6237 && GET_CODE (x) == SYMBOL_REF
6238 && !SYMBOL_REF_BIND_NOW_P (x)
6239 && !mips_symbol_binds_local_p (x));
6240 }
6241
6242 /* Load function address ADDR into register DEST. TYPE is as for
6243 mips_expand_call. Return true if we used an explicit lazy-binding
6244 sequence. */
6245
6246 static bool
6247 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
6248 {
6249 /* If we're generating PIC, and this call is to a global function,
6250 try to allow its address to be resolved lazily. This isn't
6251 possible for sibcalls when $gp is call-saved because the value
6252 of $gp on entry to the stub would be our caller's gp, not ours. */
6253 if (TARGET_EXPLICIT_RELOCS
6254 && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
6255 && mips_ok_for_lazy_binding_p (addr))
6256 {
6257 addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
6258 emit_insn (gen_rtx_SET (VOIDmode, dest, addr));
6259 return true;
6260 }
6261 else
6262 {
6263 mips_emit_move (dest, addr);
6264 return false;
6265 }
6266 }
6267 \f
6268 /* Each locally-defined hard-float MIPS16 function has a local symbol
6269 associated with it. This hash table maps the function symbol (FUNC)
6270 to the local symbol (LOCAL). */
6271 struct GTY(()) mips16_local_alias {
6272 rtx func;
6273 rtx local;
6274 };
6275 static GTY ((param_is (struct mips16_local_alias))) htab_t mips16_local_aliases;
6276
6277 /* Hash table callbacks for mips16_local_aliases. */
6278
6279 static hashval_t
6280 mips16_local_aliases_hash (const void *entry)
6281 {
6282 const struct mips16_local_alias *alias;
6283
6284 alias = (const struct mips16_local_alias *) entry;
6285 return htab_hash_string (XSTR (alias->func, 0));
6286 }
6287
6288 static int
6289 mips16_local_aliases_eq (const void *entry1, const void *entry2)
6290 {
6291 const struct mips16_local_alias *alias1, *alias2;
6292
6293 alias1 = (const struct mips16_local_alias *) entry1;
6294 alias2 = (const struct mips16_local_alias *) entry2;
6295 return rtx_equal_p (alias1->func, alias2->func);
6296 }
6297
6298 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
6299 Return a local alias for it, creating a new one if necessary. */
6300
6301 static rtx
6302 mips16_local_alias (rtx func)
6303 {
6304 struct mips16_local_alias *alias, tmp_alias;
6305 void **slot;
6306
6307 /* Create the hash table if this is the first call. */
6308 if (mips16_local_aliases == NULL)
6309 mips16_local_aliases = htab_create_ggc (37, mips16_local_aliases_hash,
6310 mips16_local_aliases_eq, NULL);
6311
6312 /* Look up the function symbol, creating a new entry if need be. */
6313 tmp_alias.func = func;
6314 slot = htab_find_slot (mips16_local_aliases, &tmp_alias, INSERT);
6315 gcc_assert (slot != NULL);
6316
6317 alias = (struct mips16_local_alias *) *slot;
6318 if (alias == NULL)
6319 {
6320 const char *func_name, *local_name;
6321 rtx local;
6322
6323 /* Create a new SYMBOL_REF for the local symbol. The choice of
6324 __fn_local_* is based on the __fn_stub_* names that we've
6325 traditionally used for the non-MIPS16 stub. */
6326 func_name = targetm.strip_name_encoding (XSTR (func, 0));
6327 local_name = ACONCAT (("__fn_local_", func_name, NULL));
6328 local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
6329 SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
6330
6331 /* Create a new structure to represent the mapping. */
6332 alias = ggc_alloc_mips16_local_alias ();
6333 alias->func = func;
6334 alias->local = local;
6335 *slot = alias;
6336 }
6337 return alias->local;
6338 }
6339 \f
6340 /* A chained list of functions for which mips16_build_call_stub has already
6341 generated a stub. NAME is the name of the function and FP_RET_P is true
6342 if the function returns a value in floating-point registers. */
6343 struct mips16_stub {
6344 struct mips16_stub *next;
6345 char *name;
6346 bool fp_ret_p;
6347 };
6348 static struct mips16_stub *mips16_stubs;
6349
6350 /* Return the two-character string that identifies floating-point
6351 return mode MODE in the name of a MIPS16 function stub. */
6352
6353 static const char *
6354 mips16_call_stub_mode_suffix (enum machine_mode mode)
6355 {
6356 if (mode == SFmode)
6357 return "sf";
6358 else if (mode == DFmode)
6359 return "df";
6360 else if (mode == SCmode)
6361 return "sc";
6362 else if (mode == DCmode)
6363 return "dc";
6364 else if (mode == V2SFmode)
6365 return "df";
6366 else
6367 gcc_unreachable ();
6368 }
6369
6370 /* Write instructions to move a 32-bit value between general register
6371 GPREG and floating-point register FPREG. DIRECTION is 't' to move
6372 from GPREG to FPREG and 'f' to move in the opposite direction. */
6373
6374 static void
6375 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6376 {
6377 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6378 reg_names[gpreg], reg_names[fpreg]);
6379 }
6380
6381 /* Likewise for 64-bit values. */
6382
6383 static void
6384 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6385 {
6386 if (TARGET_64BIT)
6387 fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
6388 reg_names[gpreg], reg_names[fpreg]);
6389 else if (TARGET_FLOAT64)
6390 {
6391 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6392 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6393 fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
6394 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
6395 }
6396 else
6397 {
6398 /* Move the least-significant word. */
6399 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6400 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6401 /* ...then the most significant word. */
6402 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6403 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
6404 }
6405 }
6406
6407 /* Write out code to move floating-point arguments into or out of
6408 general registers. FP_CODE is the code describing which arguments
6409 are present (see the comment above the definition of CUMULATIVE_ARGS
6410 in mips.h). DIRECTION is as for mips_output_32bit_xfer. */
6411
6412 static void
6413 mips_output_args_xfer (int fp_code, char direction)
6414 {
6415 unsigned int gparg, fparg, f;
6416 CUMULATIVE_ARGS cum;
6417
6418 /* This code only works for o32 and o64. */
6419 gcc_assert (TARGET_OLDABI);
6420
6421 mips_init_cumulative_args (&cum, NULL);
6422
6423 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6424 {
6425 enum machine_mode mode;
6426 struct mips_arg_info info;
6427
6428 if ((f & 3) == 1)
6429 mode = SFmode;
6430 else if ((f & 3) == 2)
6431 mode = DFmode;
6432 else
6433 gcc_unreachable ();
6434
6435 mips_get_arg_info (&info, &cum, mode, NULL, true);
6436 gparg = mips_arg_regno (&info, false);
6437 fparg = mips_arg_regno (&info, true);
6438
6439 if (mode == SFmode)
6440 mips_output_32bit_xfer (direction, gparg, fparg);
6441 else
6442 mips_output_64bit_xfer (direction, gparg, fparg);
6443
6444 mips_function_arg_advance (pack_cumulative_args (&cum), mode, NULL, true);
6445 }
6446 }
6447
6448 /* Write a MIPS16 stub for the current function. This stub is used
6449 for functions which take arguments in the floating-point registers.
6450 It is normal-mode code that moves the floating-point arguments
6451 into the general registers and then jumps to the MIPS16 code. */
6452
6453 static void
6454 mips16_build_function_stub (void)
6455 {
6456 const char *fnname, *alias_name, *separator;
6457 char *secname, *stubname;
6458 tree stubdecl;
6459 unsigned int f;
6460 rtx symbol, alias;
6461
6462 /* Create the name of the stub, and its unique section. */
6463 symbol = XEXP (DECL_RTL (current_function_decl), 0);
6464 alias = mips16_local_alias (symbol);
6465
6466 fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
6467 alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
6468 secname = ACONCAT ((".mips16.fn.", fnname, NULL));
6469 stubname = ACONCAT (("__fn_stub_", fnname, NULL));
6470
6471 /* Build a decl for the stub. */
6472 stubdecl = build_decl (BUILTINS_LOCATION,
6473 FUNCTION_DECL, get_identifier (stubname),
6474 build_function_type_list (void_type_node, NULL_TREE));
6475 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6476 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6477 RESULT_DECL, NULL_TREE, void_type_node);
6478
6479 /* Output a comment. */
6480 fprintf (asm_out_file, "\t# Stub function for %s (",
6481 current_function_name ());
6482 separator = "";
6483 for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
6484 {
6485 fprintf (asm_out_file, "%s%s", separator,
6486 (f & 3) == 1 ? "float" : "double");
6487 separator = ", ";
6488 }
6489 fprintf (asm_out_file, ")\n");
6490
6491 /* Start the function definition. */
6492 assemble_start_function (stubdecl, stubname);
6493 mips_start_function_definition (stubname, false);
6494
6495 /* If generating pic2 code, either set up the global pointer or
6496 switch to pic0. */
6497 if (TARGET_ABICALLS_PIC2)
6498 {
6499 if (TARGET_ABSOLUTE_ABICALLS)
6500 fprintf (asm_out_file, "\t.option\tpic0\n");
6501 else
6502 {
6503 output_asm_insn ("%(.cpload\t%^%)", NULL);
6504 /* Emit an R_MIPS_NONE relocation to tell the linker what the
6505 target function is. Use a local GOT access when loading the
6506 symbol, to cut down on the number of unnecessary GOT entries
6507 for stubs that aren't needed. */
6508 output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
6509 symbol = alias;
6510 }
6511 }
6512
6513 /* Load the address of the MIPS16 function into $25. Do this first so
6514 that targets with coprocessor interlocks can use an MFC1 to fill the
6515 delay slot. */
6516 output_asm_insn ("la\t%^,%0", &symbol);
6517
6518 /* Move the arguments from floating-point registers to general registers. */
6519 mips_output_args_xfer (crtl->args.info.fp_code, 'f');
6520
6521 /* Jump to the MIPS16 function. */
6522 output_asm_insn ("jr\t%^", NULL);
6523
6524 if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
6525 fprintf (asm_out_file, "\t.option\tpic2\n");
6526
6527 mips_end_function_definition (stubname);
6528
6529 /* If the linker needs to create a dynamic symbol for the target
6530 function, it will associate the symbol with the stub (which,
6531 unlike the target function, follows the proper calling conventions).
6532 It is therefore useful to have a local alias for the target function,
6533 so that it can still be identified as MIPS16 code. As an optimization,
6534 this symbol can also be used for indirect MIPS16 references from
6535 within this file. */
6536 ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
6537
6538 switch_to_section (function_section (current_function_decl));
6539 }
6540
6541 /* The current function is a MIPS16 function that returns a value in an FPR.
6542 Copy the return value from its soft-float to its hard-float location.
6543 libgcc2 has special non-MIPS16 helper functions for each case. */
6544
6545 static void
6546 mips16_copy_fpr_return_value (void)
6547 {
6548 rtx fn, insn, retval;
6549 tree return_type;
6550 enum machine_mode return_mode;
6551 const char *name;
6552
6553 return_type = DECL_RESULT (current_function_decl);
6554 return_mode = DECL_MODE (return_type);
6555
6556 name = ACONCAT (("__mips16_ret_",
6557 mips16_call_stub_mode_suffix (return_mode),
6558 NULL));
6559 fn = mips16_stub_function (name);
6560
6561 /* The function takes arguments in $2 (and possibly $3), so calls
6562 to it cannot be lazily bound. */
6563 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
6564
6565 /* Model the call as something that takes the GPR return value as
6566 argument and returns an "updated" value. */
6567 retval = gen_rtx_REG (return_mode, GP_RETURN);
6568 insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
6569 const0_rtx, NULL_RTX, false);
6570 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
6571 }
6572
6573 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
6574 RETVAL is the location of the return value, or null if this is
6575 a "call" rather than a "call_value". ARGS_SIZE is the size of the
6576 arguments and FP_CODE is the code built by mips_function_arg;
6577 see the comment before the fp_code field in CUMULATIVE_ARGS for details.
6578
6579 There are three alternatives:
6580
6581 - If a stub was needed, emit the call and return the call insn itself.
6582
6583 - If we can avoid using a stub by redirecting the call, set *FN_PTR
6584 to the new target and return null.
6585
6586 - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
6587 unmodified.
6588
6589 A stub is needed for calls to functions that, in normal mode,
6590 receive arguments in FPRs or return values in FPRs. The stub
6591 copies the arguments from their soft-float positions to their
6592 hard-float positions, calls the real function, then copies the
6593 return value from its hard-float position to its soft-float
6594 position.
6595
6596 We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
6597 If *FN_PTR turns out to be to a non-MIPS16 function, the linker
6598 automatically redirects the JAL to the stub, otherwise the JAL
6599 continues to call FN directly. */
6600
6601 static rtx
6602 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
6603 {
6604 const char *fnname;
6605 bool fp_ret_p;
6606 struct mips16_stub *l;
6607 rtx insn, fn;
6608
6609 /* We don't need to do anything if we aren't in MIPS16 mode, or if
6610 we were invoked with the -msoft-float option. */
6611 if (!TARGET_MIPS16 || TARGET_SOFT_FLOAT_ABI)
6612 return NULL_RTX;
6613
6614 /* Figure out whether the value might come back in a floating-point
6615 register. */
6616 fp_ret_p = retval && mips_return_mode_in_fpr_p (GET_MODE (retval));
6617
6618 /* We don't need to do anything if there were no floating-point
6619 arguments and the value will not be returned in a floating-point
6620 register. */
6621 if (fp_code == 0 && !fp_ret_p)
6622 return NULL_RTX;
6623
6624 /* We don't need to do anything if this is a call to a special
6625 MIPS16 support function. */
6626 fn = *fn_ptr;
6627 if (mips16_stub_function_p (fn))
6628 return NULL_RTX;
6629
6630 /* If we're calling a locally-defined MIPS16 function, we know that
6631 it will return values in both the "soft-float" and "hard-float"
6632 registers. There is no need to use a stub to move the latter
6633 to the former. */
6634 if (fp_code == 0 && mips16_local_function_p (fn))
6635 return NULL_RTX;
6636
6637 /* This code will only work for o32 and o64 abis. The other ABI's
6638 require more sophisticated support. */
6639 gcc_assert (TARGET_OLDABI);
6640
6641 /* If we're calling via a function pointer, use one of the magic
6642 libgcc.a stubs provided for each (FP_CODE, FP_RET_P) combination.
6643 Each stub expects the function address to arrive in register $2. */
6644 if (GET_CODE (fn) != SYMBOL_REF
6645 || !call_insn_operand (fn, VOIDmode))
6646 {
6647 char buf[30];
6648 rtx stub_fn, insn, addr;
6649 bool lazy_p;
6650
6651 /* If this is a locally-defined and locally-binding function,
6652 avoid the stub by calling the local alias directly. */
6653 if (mips16_local_function_p (fn))
6654 {
6655 *fn_ptr = mips16_local_alias (fn);
6656 return NULL_RTX;
6657 }
6658
6659 /* Create a SYMBOL_REF for the libgcc.a function. */
6660 if (fp_ret_p)
6661 sprintf (buf, "__mips16_call_stub_%s_%d",
6662 mips16_call_stub_mode_suffix (GET_MODE (retval)),
6663 fp_code);
6664 else
6665 sprintf (buf, "__mips16_call_stub_%d", fp_code);
6666 stub_fn = mips16_stub_function (buf);
6667
6668 /* The function uses $2 as an argument, so calls to it
6669 cannot be lazily bound. */
6670 SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
6671
6672 /* Load the target function into $2. */
6673 addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
6674 lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
6675
6676 /* Emit the call. */
6677 insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
6678 args_size, NULL_RTX, lazy_p);
6679
6680 /* Tell GCC that this call does indeed use the value of $2. */
6681 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
6682
6683 /* If we are handling a floating-point return value, we need to
6684 save $18 in the function prologue. Putting a note on the
6685 call will mean that df_regs_ever_live_p ($18) will be true if the
6686 call is not eliminated, and we can check that in the prologue
6687 code. */
6688 if (fp_ret_p)
6689 CALL_INSN_FUNCTION_USAGE (insn) =
6690 gen_rtx_EXPR_LIST (VOIDmode,
6691 gen_rtx_CLOBBER (VOIDmode,
6692 gen_rtx_REG (word_mode, 18)),
6693 CALL_INSN_FUNCTION_USAGE (insn));
6694
6695 return insn;
6696 }
6697
6698 /* We know the function we are going to call. If we have already
6699 built a stub, we don't need to do anything further. */
6700 fnname = targetm.strip_name_encoding (XSTR (fn, 0));
6701 for (l = mips16_stubs; l != NULL; l = l->next)
6702 if (strcmp (l->name, fnname) == 0)
6703 break;
6704
6705 if (l == NULL)
6706 {
6707 const char *separator;
6708 char *secname, *stubname;
6709 tree stubid, stubdecl;
6710 unsigned int f;
6711
6712 /* If the function does not return in FPRs, the special stub
6713 section is named
6714 .mips16.call.FNNAME
6715
6716 If the function does return in FPRs, the stub section is named
6717 .mips16.call.fp.FNNAME
6718
6719 Build a decl for the stub. */
6720 secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
6721 fnname, NULL));
6722 stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
6723 fnname, NULL));
6724 stubid = get_identifier (stubname);
6725 stubdecl = build_decl (BUILTINS_LOCATION,
6726 FUNCTION_DECL, stubid,
6727 build_function_type_list (void_type_node,
6728 NULL_TREE));
6729 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6730 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6731 RESULT_DECL, NULL_TREE,
6732 void_type_node);
6733
6734 /* Output a comment. */
6735 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
6736 (fp_ret_p
6737 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
6738 : ""),
6739 fnname);
6740 separator = "";
6741 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6742 {
6743 fprintf (asm_out_file, "%s%s", separator,
6744 (f & 3) == 1 ? "float" : "double");
6745 separator = ", ";
6746 }
6747 fprintf (asm_out_file, ")\n");
6748
6749 /* Start the function definition. */
6750 assemble_start_function (stubdecl, stubname);
6751 mips_start_function_definition (stubname, false);
6752
6753 if (fp_ret_p)
6754 {
6755 fprintf (asm_out_file, "\t.cfi_startproc\n");
6756
6757 /* Create a fake CFA 4 bytes below the stack pointer.
6758 This works around unwinders (like libgcc's) that expect
6759 the CFA for non-signal frames to be unique. */
6760 fprintf (asm_out_file, "\t.cfi_def_cfa 29,-4\n");
6761
6762 /* "Save" $sp in itself so we don't use the fake CFA.
6763 This is: DW_CFA_val_expression r29, { DW_OP_reg29 }. */
6764 fprintf (asm_out_file, "\t.cfi_escape 0x16,29,1,0x6d\n");
6765 }
6766 else
6767 {
6768 /* Load the address of the MIPS16 function into $25. Do this
6769 first so that targets with coprocessor interlocks can use
6770 an MFC1 to fill the delay slot. */
6771 if (TARGET_EXPLICIT_RELOCS)
6772 {
6773 output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
6774 output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
6775 }
6776 else
6777 output_asm_insn ("la\t%^,%0", &fn);
6778 }
6779
6780 /* Move the arguments from general registers to floating-point
6781 registers. */
6782 mips_output_args_xfer (fp_code, 't');
6783
6784 if (fp_ret_p)
6785 {
6786 /* Save the return address in $18 and call the non-MIPS16 function.
6787 The stub's caller knows that $18 might be clobbered, even though
6788 $18 is usually a call-saved register. */
6789 fprintf (asm_out_file, "\tmove\t%s,%s\n",
6790 reg_names[GP_REG_FIRST + 18], reg_names[RETURN_ADDR_REGNUM]);
6791 output_asm_insn (MIPS_CALL ("jal", &fn, 0, -1), &fn);
6792 fprintf (asm_out_file, "\t.cfi_register 31,18\n");
6793
6794 /* Move the result from floating-point registers to
6795 general registers. */
6796 switch (GET_MODE (retval))
6797 {
6798 case SCmode:
6799 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
6800 TARGET_BIG_ENDIAN
6801 ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6802 : FP_REG_FIRST);
6803 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
6804 TARGET_LITTLE_ENDIAN
6805 ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6806 : FP_REG_FIRST);
6807 if (GET_MODE (retval) == SCmode && TARGET_64BIT)
6808 {
6809 /* On 64-bit targets, complex floats are returned in
6810 a single GPR, such that "sd" on a suitably-aligned
6811 target would store the value correctly. */
6812 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6813 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6814 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6815 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6816 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
6817 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
6818 fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
6819 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6820 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6821 fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
6822 reg_names[GP_RETURN],
6823 reg_names[GP_RETURN],
6824 reg_names[GP_RETURN + 1]);
6825 }
6826 break;
6827
6828 case SFmode:
6829 mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6830 break;
6831
6832 case DCmode:
6833 mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
6834 FP_REG_FIRST + MAX_FPRS_PER_FMT);
6835 /* Fall though. */
6836 case DFmode:
6837 case V2SFmode:
6838 mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6839 break;
6840
6841 default:
6842 gcc_unreachable ();
6843 }
6844 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
6845 fprintf (asm_out_file, "\t.cfi_endproc\n");
6846 }
6847 else
6848 {
6849 /* Jump to the previously-loaded address. */
6850 output_asm_insn ("jr\t%^", NULL);
6851 }
6852
6853 #ifdef ASM_DECLARE_FUNCTION_SIZE
6854 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
6855 #endif
6856
6857 mips_end_function_definition (stubname);
6858
6859 /* Record this stub. */
6860 l = XNEW (struct mips16_stub);
6861 l->name = xstrdup (fnname);
6862 l->fp_ret_p = fp_ret_p;
6863 l->next = mips16_stubs;
6864 mips16_stubs = l;
6865 }
6866
6867 /* If we expect a floating-point return value, but we've built a
6868 stub which does not expect one, then we're in trouble. We can't
6869 use the existing stub, because it won't handle the floating-point
6870 value. We can't build a new stub, because the linker won't know
6871 which stub to use for the various calls in this object file.
6872 Fortunately, this case is illegal, since it means that a function
6873 was declared in two different ways in a single compilation. */
6874 if (fp_ret_p && !l->fp_ret_p)
6875 error ("cannot handle inconsistent calls to %qs", fnname);
6876
6877 if (retval == NULL_RTX)
6878 insn = gen_call_internal_direct (fn, args_size);
6879 else
6880 insn = gen_call_value_internal_direct (retval, fn, args_size);
6881 insn = mips_emit_call_insn (insn, fn, fn, false);
6882
6883 /* If we are calling a stub which handles a floating-point return
6884 value, we need to arrange to save $18 in the prologue. We do this
6885 by marking the function call as using the register. The prologue
6886 will later see that it is used, and emit code to save it. */
6887 if (fp_ret_p)
6888 CALL_INSN_FUNCTION_USAGE (insn) =
6889 gen_rtx_EXPR_LIST (VOIDmode,
6890 gen_rtx_CLOBBER (VOIDmode,
6891 gen_rtx_REG (word_mode, 18)),
6892 CALL_INSN_FUNCTION_USAGE (insn));
6893
6894 return insn;
6895 }
6896 \f
6897 /* Expand a call of type TYPE. RESULT is where the result will go (null
6898 for "call"s and "sibcall"s), ADDR is the address of the function,
6899 ARGS_SIZE is the size of the arguments and AUX is the value passed
6900 to us by mips_function_arg. LAZY_P is true if this call already
6901 involves a lazily-bound function address (such as when calling
6902 functions through a MIPS16 hard-float stub).
6903
6904 Return the call itself. */
6905
6906 rtx
6907 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
6908 rtx args_size, rtx aux, bool lazy_p)
6909 {
6910 rtx orig_addr, pattern, insn;
6911 int fp_code;
6912
6913 fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
6914 insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
6915 if (insn)
6916 {
6917 gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
6918 return insn;
6919 }
6920
6921 orig_addr = addr;
6922 if (!call_insn_operand (addr, VOIDmode))
6923 {
6924 if (type == MIPS_CALL_EPILOGUE)
6925 addr = MIPS_EPILOGUE_TEMP (Pmode);
6926 else
6927 addr = gen_reg_rtx (Pmode);
6928 lazy_p |= mips_load_call_address (type, addr, orig_addr);
6929 }
6930
6931 if (result == 0)
6932 {
6933 rtx (*fn) (rtx, rtx);
6934
6935 if (type == MIPS_CALL_SIBCALL)
6936 fn = gen_sibcall_internal;
6937 else
6938 fn = gen_call_internal;
6939
6940 pattern = fn (addr, args_size);
6941 }
6942 else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
6943 {
6944 /* Handle return values created by mips_return_fpr_pair. */
6945 rtx (*fn) (rtx, rtx, rtx, rtx);
6946 rtx reg1, reg2;
6947
6948 if (type == MIPS_CALL_SIBCALL)
6949 fn = gen_sibcall_value_multiple_internal;
6950 else
6951 fn = gen_call_value_multiple_internal;
6952
6953 reg1 = XEXP (XVECEXP (result, 0, 0), 0);
6954 reg2 = XEXP (XVECEXP (result, 0, 1), 0);
6955 pattern = fn (reg1, addr, args_size, reg2);
6956 }
6957 else
6958 {
6959 rtx (*fn) (rtx, rtx, rtx);
6960
6961 if (type == MIPS_CALL_SIBCALL)
6962 fn = gen_sibcall_value_internal;
6963 else
6964 fn = gen_call_value_internal;
6965
6966 /* Handle return values created by mips_return_fpr_single. */
6967 if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
6968 result = XEXP (XVECEXP (result, 0, 0), 0);
6969 pattern = fn (result, addr, args_size);
6970 }
6971
6972 return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
6973 }
6974
6975 /* Split call instruction INSN into a $gp-clobbering call and
6976 (where necessary) an instruction to restore $gp from its save slot.
6977 CALL_PATTERN is the pattern of the new call. */
6978
6979 void
6980 mips_split_call (rtx insn, rtx call_pattern)
6981 {
6982 emit_call_insn (call_pattern);
6983 if (!find_reg_note (insn, REG_NORETURN, 0))
6984 /* Pick a temporary register that is suitable for both MIPS16 and
6985 non-MIPS16 code. $4 and $5 are used for returning complex double
6986 values in soft-float code, so $6 is the first suitable candidate. */
6987 mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode, GP_ARG_FIRST + 2));
6988 }
6989
6990 /* Return true if a call to DECL may need to use JALX. */
6991
6992 static bool
6993 mips_call_may_need_jalx_p (tree decl)
6994 {
6995 /* If the current translation unit would use a different mode for DECL,
6996 assume that the call needs JALX. */
6997 if (mips_get_compress_mode (decl) != TARGET_COMPRESSION)
6998 return true;
6999
7000 /* mips_get_compress_mode is always accurate for locally-binding
7001 functions in the current translation unit. */
7002 if (!DECL_EXTERNAL (decl) && targetm.binds_local_p (decl))
7003 return false;
7004
7005 /* When -minterlink-compressed is in effect, assume that functions
7006 could use a different encoding mode unless an attribute explicitly
7007 tells us otherwise. */
7008 if (TARGET_INTERLINK_COMPRESSED)
7009 {
7010 if (!TARGET_COMPRESSION
7011 && mips_get_compress_off_flags (DECL_ATTRIBUTES (decl)) ==0)
7012 return true;
7013 if (TARGET_COMPRESSION
7014 && mips_get_compress_on_flags (DECL_ATTRIBUTES (decl)) == 0)
7015 return true;
7016 }
7017
7018 return false;
7019 }
7020
7021 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
7022
7023 static bool
7024 mips_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
7025 {
7026 if (!TARGET_SIBCALLS)
7027 return false;
7028
7029 /* Interrupt handlers need special epilogue code and therefore can't
7030 use sibcalls. */
7031 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
7032 return false;
7033
7034 /* Direct Js are only possible to functions that use the same ISA encoding.
7035 There is no JX counterpoart of JALX. */
7036 if (decl
7037 && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode)
7038 && mips_call_may_need_jalx_p (decl))
7039 return false;
7040
7041 /* Sibling calls should not prevent lazy binding. Lazy-binding stubs
7042 require $gp to be valid on entry, so sibcalls can only use stubs
7043 if $gp is call-clobbered. */
7044 if (decl
7045 && TARGET_CALL_SAVED_GP
7046 && !TARGET_ABICALLS_PIC0
7047 && !targetm.binds_local_p (decl))
7048 return false;
7049
7050 /* Otherwise OK. */
7051 return true;
7052 }
7053 \f
7054 /* Emit code to move general operand SRC into condition-code
7055 register DEST given that SCRATCH is a scratch TFmode FPR.
7056 The sequence is:
7057
7058 FP1 = SRC
7059 FP2 = 0.0f
7060 DEST = FP2 < FP1
7061
7062 where FP1 and FP2 are single-precision FPRs taken from SCRATCH. */
7063
7064 void
7065 mips_expand_fcc_reload (rtx dest, rtx src, rtx scratch)
7066 {
7067 rtx fp1, fp2;
7068
7069 /* Change the source to SFmode. */
7070 if (MEM_P (src))
7071 src = adjust_address (src, SFmode, 0);
7072 else if (REG_P (src) || GET_CODE (src) == SUBREG)
7073 src = gen_rtx_REG (SFmode, true_regnum (src));
7074
7075 fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
7076 fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + MAX_FPRS_PER_FMT);
7077
7078 mips_emit_move (copy_rtx (fp1), src);
7079 mips_emit_move (copy_rtx (fp2), CONST0_RTX (SFmode));
7080 emit_insn (gen_slt_sf (dest, fp2, fp1));
7081 }
7082 \f
7083 /* Implement MOVE_BY_PIECES_P. */
7084
7085 bool
7086 mips_move_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7087 {
7088 if (HAVE_movmemsi)
7089 {
7090 /* movmemsi is meant to generate code that is at least as good as
7091 move_by_pieces. However, movmemsi effectively uses a by-pieces
7092 implementation both for moves smaller than a word and for
7093 word-aligned moves of no more than MIPS_MAX_MOVE_BYTES_STRAIGHT
7094 bytes. We should allow the tree-level optimisers to do such
7095 moves by pieces, as it often exposes other optimization
7096 opportunities. We might as well continue to use movmemsi at
7097 the rtl level though, as it produces better code when
7098 scheduling is disabled (such as at -O). */
7099 if (currently_expanding_to_rtl)
7100 return false;
7101 if (align < BITS_PER_WORD)
7102 return size < UNITS_PER_WORD;
7103 return size <= MIPS_MAX_MOVE_BYTES_STRAIGHT;
7104 }
7105 /* The default value. If this becomes a target hook, we should
7106 call the default definition instead. */
7107 return (move_by_pieces_ninsns (size, align, MOVE_MAX_PIECES + 1)
7108 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()));
7109 }
7110
7111 /* Implement STORE_BY_PIECES_P. */
7112
7113 bool
7114 mips_store_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7115 {
7116 /* Storing by pieces involves moving constants into registers
7117 of size MIN (ALIGN, BITS_PER_WORD), then storing them.
7118 We need to decide whether it is cheaper to load the address of
7119 constant data into a register and use a block move instead. */
7120
7121 /* If the data is only byte aligned, then:
7122
7123 (a1) A block move of less than 4 bytes would involve three 3 LBs and
7124 3 SBs. We might as well use 3 single-instruction LIs and 3 SBs
7125 instead.
7126
7127 (a2) A block move of 4 bytes from aligned source data can use an
7128 LW/SWL/SWR sequence. This is often better than the 4 LIs and
7129 4 SBs that we would generate when storing by pieces. */
7130 if (align <= BITS_PER_UNIT)
7131 return size < 4;
7132
7133 /* If the data is 2-byte aligned, then:
7134
7135 (b1) A block move of less than 4 bytes would use a combination of LBs,
7136 LHs, SBs and SHs. We get better code by using single-instruction
7137 LIs, SBs and SHs instead.
7138
7139 (b2) A block move of 4 bytes from aligned source data would again use
7140 an LW/SWL/SWR sequence. In most cases, loading the address of
7141 the source data would require at least one extra instruction.
7142 It is often more efficient to use 2 single-instruction LIs and
7143 2 SHs instead.
7144
7145 (b3) A block move of up to 3 additional bytes would be like (b1).
7146
7147 (b4) A block move of 8 bytes from aligned source data can use two
7148 LW/SWL/SWR sequences or a single LD/SDL/SDR sequence. Both
7149 sequences are better than the 4 LIs and 4 SHs that we'd generate
7150 when storing by pieces.
7151
7152 The reasoning for higher alignments is similar:
7153
7154 (c1) A block move of less than 4 bytes would be the same as (b1).
7155
7156 (c2) A block move of 4 bytes would use an LW/SW sequence. Again,
7157 loading the address of the source data would typically require
7158 at least one extra instruction. It is generally better to use
7159 LUI/ORI/SW instead.
7160
7161 (c3) A block move of up to 3 additional bytes would be like (b1).
7162
7163 (c4) A block move of 8 bytes can use two LW/SW sequences or a single
7164 LD/SD sequence, and in these cases we've traditionally preferred
7165 the memory copy over the more bulky constant moves. */
7166 return size < 8;
7167 }
7168
7169 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
7170 Assume that the areas do not overlap. */
7171
7172 static void
7173 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
7174 {
7175 HOST_WIDE_INT offset, delta;
7176 unsigned HOST_WIDE_INT bits;
7177 int i;
7178 enum machine_mode mode;
7179 rtx *regs;
7180
7181 /* Work out how many bits to move at a time. If both operands have
7182 half-word alignment, it is usually better to move in half words.
7183 For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
7184 and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
7185 Otherwise move word-sized chunks. */
7186 if (MEM_ALIGN (src) == BITS_PER_WORD / 2
7187 && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
7188 bits = BITS_PER_WORD / 2;
7189 else
7190 bits = BITS_PER_WORD;
7191
7192 mode = mode_for_size (bits, MODE_INT, 0);
7193 delta = bits / BITS_PER_UNIT;
7194
7195 /* Allocate a buffer for the temporary registers. */
7196 regs = XALLOCAVEC (rtx, length / delta);
7197
7198 /* Load as many BITS-sized chunks as possible. Use a normal load if
7199 the source has enough alignment, otherwise use left/right pairs. */
7200 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7201 {
7202 regs[i] = gen_reg_rtx (mode);
7203 if (MEM_ALIGN (src) >= bits)
7204 mips_emit_move (regs[i], adjust_address (src, mode, offset));
7205 else
7206 {
7207 rtx part = adjust_address (src, BLKmode, offset);
7208 set_mem_size (part, delta);
7209 if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0, 0))
7210 gcc_unreachable ();
7211 }
7212 }
7213
7214 /* Copy the chunks to the destination. */
7215 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7216 if (MEM_ALIGN (dest) >= bits)
7217 mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
7218 else
7219 {
7220 rtx part = adjust_address (dest, BLKmode, offset);
7221 set_mem_size (part, delta);
7222 if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
7223 gcc_unreachable ();
7224 }
7225
7226 /* Mop up any left-over bytes. */
7227 if (offset < length)
7228 {
7229 src = adjust_address (src, BLKmode, offset);
7230 dest = adjust_address (dest, BLKmode, offset);
7231 move_by_pieces (dest, src, length - offset,
7232 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
7233 }
7234 }
7235
7236 /* Helper function for doing a loop-based block operation on memory
7237 reference MEM. Each iteration of the loop will operate on LENGTH
7238 bytes of MEM.
7239
7240 Create a new base register for use within the loop and point it to
7241 the start of MEM. Create a new memory reference that uses this
7242 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
7243
7244 static void
7245 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
7246 rtx *loop_reg, rtx *loop_mem)
7247 {
7248 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
7249
7250 /* Although the new mem does not refer to a known location,
7251 it does keep up to LENGTH bytes of alignment. */
7252 *loop_mem = change_address (mem, BLKmode, *loop_reg);
7253 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
7254 }
7255
7256 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
7257 bytes at a time. LENGTH must be at least BYTES_PER_ITER. Assume that
7258 the memory regions do not overlap. */
7259
7260 static void
7261 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
7262 HOST_WIDE_INT bytes_per_iter)
7263 {
7264 rtx label, src_reg, dest_reg, final_src, test;
7265 HOST_WIDE_INT leftover;
7266
7267 leftover = length % bytes_per_iter;
7268 length -= leftover;
7269
7270 /* Create registers and memory references for use within the loop. */
7271 mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
7272 mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
7273
7274 /* Calculate the value that SRC_REG should have after the last iteration
7275 of the loop. */
7276 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
7277 0, 0, OPTAB_WIDEN);
7278
7279 /* Emit the start of the loop. */
7280 label = gen_label_rtx ();
7281 emit_label (label);
7282
7283 /* Emit the loop body. */
7284 mips_block_move_straight (dest, src, bytes_per_iter);
7285
7286 /* Move on to the next block. */
7287 mips_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
7288 mips_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
7289
7290 /* Emit the loop condition. */
7291 test = gen_rtx_NE (VOIDmode, src_reg, final_src);
7292 if (Pmode == DImode)
7293 emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
7294 else
7295 emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
7296
7297 /* Mop up any left-over bytes. */
7298 if (leftover)
7299 mips_block_move_straight (dest, src, leftover);
7300 }
7301
7302 /* Expand a movmemsi instruction, which copies LENGTH bytes from
7303 memory reference SRC to memory reference DEST. */
7304
7305 bool
7306 mips_expand_block_move (rtx dest, rtx src, rtx length)
7307 {
7308 if (CONST_INT_P (length))
7309 {
7310 if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_STRAIGHT)
7311 {
7312 mips_block_move_straight (dest, src, INTVAL (length));
7313 return true;
7314 }
7315 else if (optimize)
7316 {
7317 mips_block_move_loop (dest, src, INTVAL (length),
7318 MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
7319 return true;
7320 }
7321 }
7322 return false;
7323 }
7324 \f
7325 /* Expand a loop of synci insns for the address range [BEGIN, END). */
7326
7327 void
7328 mips_expand_synci_loop (rtx begin, rtx end)
7329 {
7330 rtx inc, label, end_label, cmp_result, mask, length;
7331
7332 /* Create end_label. */
7333 end_label = gen_label_rtx ();
7334
7335 /* Check if begin equals end. */
7336 cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
7337 emit_jump_insn (gen_condjump (cmp_result, end_label));
7338
7339 /* Load INC with the cache line size (rdhwr INC,$1). */
7340 inc = gen_reg_rtx (Pmode);
7341 emit_insn (PMODE_INSN (gen_rdhwr_synci_step, (inc)));
7342
7343 /* Check if inc is 0. */
7344 cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
7345 emit_jump_insn (gen_condjump (cmp_result, end_label));
7346
7347 /* Calculate mask. */
7348 mask = mips_force_unary (Pmode, NEG, inc);
7349
7350 /* Mask out begin by mask. */
7351 begin = mips_force_binary (Pmode, AND, begin, mask);
7352
7353 /* Calculate length. */
7354 length = mips_force_binary (Pmode, MINUS, end, begin);
7355
7356 /* Loop back to here. */
7357 label = gen_label_rtx ();
7358 emit_label (label);
7359
7360 emit_insn (gen_synci (begin));
7361
7362 /* Update length. */
7363 mips_emit_binary (MINUS, length, length, inc);
7364
7365 /* Update begin. */
7366 mips_emit_binary (PLUS, begin, begin, inc);
7367
7368 /* Check if length is greater than 0. */
7369 cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
7370 emit_jump_insn (gen_condjump (cmp_result, label));
7371
7372 emit_label (end_label);
7373 }
7374 \f
7375 /* Expand a QI or HI mode atomic memory operation.
7376
7377 GENERATOR contains a pointer to the gen_* function that generates
7378 the SI mode underlying atomic operation using masks that we
7379 calculate.
7380
7381 RESULT is the return register for the operation. Its value is NULL
7382 if unused.
7383
7384 MEM is the location of the atomic access.
7385
7386 OLDVAL is the first operand for the operation.
7387
7388 NEWVAL is the optional second operand for the operation. Its value
7389 is NULL if unused. */
7390
7391 void
7392 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
7393 rtx result, rtx mem, rtx oldval, rtx newval)
7394 {
7395 rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
7396 rtx unshifted_mask_reg, mask, inverted_mask, si_op;
7397 rtx res = NULL;
7398 enum machine_mode mode;
7399
7400 mode = GET_MODE (mem);
7401
7402 /* Compute the address of the containing SImode value. */
7403 orig_addr = force_reg (Pmode, XEXP (mem, 0));
7404 memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
7405 force_reg (Pmode, GEN_INT (-4)));
7406
7407 /* Create a memory reference for it. */
7408 memsi = gen_rtx_MEM (SImode, memsi_addr);
7409 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
7410 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
7411
7412 /* Work out the byte offset of the QImode or HImode value,
7413 counting from the least significant byte. */
7414 shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
7415 if (TARGET_BIG_ENDIAN)
7416 mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
7417
7418 /* Multiply by eight to convert the shift value from bytes to bits. */
7419 mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
7420
7421 /* Make the final shift an SImode value, so that it can be used in
7422 SImode operations. */
7423 shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
7424
7425 /* Set MASK to an inclusive mask of the QImode or HImode value. */
7426 unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
7427 unshifted_mask_reg = force_reg (SImode, unshifted_mask);
7428 mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
7429
7430 /* Compute the equivalent exclusive mask. */
7431 inverted_mask = gen_reg_rtx (SImode);
7432 emit_insn (gen_rtx_SET (VOIDmode, inverted_mask,
7433 gen_rtx_NOT (SImode, mask)));
7434
7435 /* Shift the old value into place. */
7436 if (oldval != const0_rtx)
7437 {
7438 oldval = convert_modes (SImode, mode, oldval, true);
7439 oldval = force_reg (SImode, oldval);
7440 oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
7441 }
7442
7443 /* Do the same for the new value. */
7444 if (newval && newval != const0_rtx)
7445 {
7446 newval = convert_modes (SImode, mode, newval, true);
7447 newval = force_reg (SImode, newval);
7448 newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
7449 }
7450
7451 /* Do the SImode atomic access. */
7452 if (result)
7453 res = gen_reg_rtx (SImode);
7454 if (newval)
7455 si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
7456 else if (result)
7457 si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
7458 else
7459 si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
7460
7461 emit_insn (si_op);
7462
7463 if (result)
7464 {
7465 /* Shift and convert the result. */
7466 mips_emit_binary (AND, res, res, mask);
7467 mips_emit_binary (LSHIFTRT, res, res, shiftsi);
7468 mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
7469 }
7470 }
7471
7472 /* Return true if it is possible to use left/right accesses for a
7473 bitfield of WIDTH bits starting BITPOS bits into BLKmode memory OP.
7474 When returning true, update *LEFT and *RIGHT as follows:
7475
7476 *LEFT is a QImode reference to the first byte if big endian or
7477 the last byte if little endian. This address can be used in the
7478 left-side instructions (LWL, SWL, LDL, SDL).
7479
7480 *RIGHT is a QImode reference to the opposite end of the field and
7481 can be used in the patterning right-side instruction. */
7482
7483 static bool
7484 mips_get_unaligned_mem (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
7485 rtx *left, rtx *right)
7486 {
7487 rtx first, last;
7488
7489 /* Check that the size is valid. */
7490 if (width != 32 && (!TARGET_64BIT || width != 64))
7491 return false;
7492
7493 /* We can only access byte-aligned values. Since we are always passed
7494 a reference to the first byte of the field, it is not necessary to
7495 do anything with BITPOS after this check. */
7496 if (bitpos % BITS_PER_UNIT != 0)
7497 return false;
7498
7499 /* Reject aligned bitfields: we want to use a normal load or store
7500 instead of a left/right pair. */
7501 if (MEM_ALIGN (op) >= width)
7502 return false;
7503
7504 /* Get references to both ends of the field. */
7505 first = adjust_address (op, QImode, 0);
7506 last = adjust_address (op, QImode, width / BITS_PER_UNIT - 1);
7507
7508 /* Allocate to LEFT and RIGHT according to endianness. LEFT should
7509 correspond to the MSB and RIGHT to the LSB. */
7510 if (TARGET_BIG_ENDIAN)
7511 *left = first, *right = last;
7512 else
7513 *left = last, *right = first;
7514
7515 return true;
7516 }
7517
7518 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
7519 DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
7520 the operation is the equivalent of:
7521
7522 (set DEST (*_extract SRC WIDTH BITPOS))
7523
7524 Return true on success. */
7525
7526 bool
7527 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
7528 HOST_WIDE_INT bitpos, bool unsigned_p)
7529 {
7530 rtx left, right, temp;
7531 rtx dest1 = NULL_RTX;
7532
7533 /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
7534 be a DImode, create a new temp and emit a zero extend at the end. */
7535 if (GET_MODE (dest) == DImode
7536 && REG_P (dest)
7537 && GET_MODE_BITSIZE (SImode) == width)
7538 {
7539 dest1 = dest;
7540 dest = gen_reg_rtx (SImode);
7541 }
7542
7543 if (!mips_get_unaligned_mem (src, width, bitpos, &left, &right))
7544 return false;
7545
7546 temp = gen_reg_rtx (GET_MODE (dest));
7547 if (GET_MODE (dest) == DImode)
7548 {
7549 emit_insn (gen_mov_ldl (temp, src, left));
7550 emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
7551 }
7552 else
7553 {
7554 emit_insn (gen_mov_lwl (temp, src, left));
7555 emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
7556 }
7557
7558 /* If we were loading 32bits and the original register was DI then
7559 sign/zero extend into the orignal dest. */
7560 if (dest1)
7561 {
7562 if (unsigned_p)
7563 emit_insn (gen_zero_extendsidi2 (dest1, dest));
7564 else
7565 emit_insn (gen_extendsidi2 (dest1, dest));
7566 }
7567 return true;
7568 }
7569
7570 /* Try to use left/right stores to expand an "ins" pattern. DEST, WIDTH,
7571 BITPOS and SRC are the operands passed to the expander; the operation
7572 is the equivalent of:
7573
7574 (set (zero_extract DEST WIDTH BITPOS) SRC)
7575
7576 Return true on success. */
7577
7578 bool
7579 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
7580 HOST_WIDE_INT bitpos)
7581 {
7582 rtx left, right;
7583 enum machine_mode mode;
7584
7585 if (!mips_get_unaligned_mem (dest, width, bitpos, &left, &right))
7586 return false;
7587
7588 mode = mode_for_size (width, MODE_INT, 0);
7589 src = gen_lowpart (mode, src);
7590 if (mode == DImode)
7591 {
7592 emit_insn (gen_mov_sdl (dest, src, left));
7593 emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
7594 }
7595 else
7596 {
7597 emit_insn (gen_mov_swl (dest, src, left));
7598 emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
7599 }
7600 return true;
7601 }
7602
7603 /* Return true if X is a MEM with the same size as MODE. */
7604
7605 bool
7606 mips_mem_fits_mode_p (enum machine_mode mode, rtx x)
7607 {
7608 return (MEM_P (x)
7609 && MEM_SIZE_KNOWN_P (x)
7610 && MEM_SIZE (x) == GET_MODE_SIZE (mode));
7611 }
7612
7613 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
7614 source of an "ext" instruction or the destination of an "ins"
7615 instruction. OP must be a register operand and the following
7616 conditions must hold:
7617
7618 0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
7619 0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7620 0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7621
7622 Also reject lengths equal to a word as they are better handled
7623 by the move patterns. */
7624
7625 bool
7626 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
7627 {
7628 if (!ISA_HAS_EXT_INS
7629 || !register_operand (op, VOIDmode)
7630 || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
7631 return false;
7632
7633 if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
7634 return false;
7635
7636 if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
7637 return false;
7638
7639 return true;
7640 }
7641
7642 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
7643 operation if MAXLEN is the maxium length of consecutive bits that
7644 can make up MASK. MODE is the mode of the operation. See
7645 mask_low_and_shift_len for the actual definition. */
7646
7647 bool
7648 mask_low_and_shift_p (enum machine_mode mode, rtx mask, rtx shift, int maxlen)
7649 {
7650 return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
7651 }
7652
7653 /* Return true iff OP1 and OP2 are valid operands together for the
7654 *and<MODE>3 and *and<MODE>3_mips16 patterns. For the cases to consider,
7655 see the table in the comment before the pattern. */
7656
7657 bool
7658 and_operands_ok (enum machine_mode mode, rtx op1, rtx op2)
7659 {
7660 return (memory_operand (op1, mode)
7661 ? and_load_operand (op2, mode)
7662 : and_reg_operand (op2, mode));
7663 }
7664
7665 /* The canonical form of a mask-low-and-shift-left operation is
7666 (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
7667 cleared. Thus we need to shift MASK to the right before checking if it
7668 is a valid mask value. MODE is the mode of the operation. If true
7669 return the length of the mask, otherwise return -1. */
7670
7671 int
7672 mask_low_and_shift_len (enum machine_mode mode, rtx mask, rtx shift)
7673 {
7674 HOST_WIDE_INT shval;
7675
7676 shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
7677 return exact_log2 ((UINTVAL (mask) >> shval) + 1);
7678 }
7679 \f
7680 /* Return true if -msplit-addresses is selected and should be honored.
7681
7682 -msplit-addresses is a half-way house between explicit relocations
7683 and the traditional assembler macros. It can split absolute 32-bit
7684 symbolic constants into a high/lo_sum pair but uses macros for other
7685 sorts of access.
7686
7687 Like explicit relocation support for REL targets, it relies
7688 on GNU extensions in the assembler and the linker.
7689
7690 Although this code should work for -O0, it has traditionally
7691 been treated as an optimization. */
7692
7693 static bool
7694 mips_split_addresses_p (void)
7695 {
7696 return (TARGET_SPLIT_ADDRESSES
7697 && optimize
7698 && !TARGET_MIPS16
7699 && !flag_pic
7700 && !ABI_HAS_64BIT_SYMBOLS);
7701 }
7702
7703 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs. */
7704
7705 static void
7706 mips_init_relocs (void)
7707 {
7708 memset (mips_split_p, '\0', sizeof (mips_split_p));
7709 memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
7710 memset (mips_use_pcrel_pool_p, '\0', sizeof (mips_use_pcrel_pool_p));
7711 memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
7712 memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
7713
7714 if (TARGET_MIPS16_PCREL_LOADS)
7715 mips_use_pcrel_pool_p[SYMBOL_ABSOLUTE] = true;
7716 else
7717 {
7718 if (ABI_HAS_64BIT_SYMBOLS)
7719 {
7720 if (TARGET_EXPLICIT_RELOCS)
7721 {
7722 mips_split_p[SYMBOL_64_HIGH] = true;
7723 mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
7724 mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
7725
7726 mips_split_p[SYMBOL_64_MID] = true;
7727 mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
7728 mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
7729
7730 mips_split_p[SYMBOL_64_LOW] = true;
7731 mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
7732 mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
7733
7734 mips_split_p[SYMBOL_ABSOLUTE] = true;
7735 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7736 }
7737 }
7738 else
7739 {
7740 if (TARGET_EXPLICIT_RELOCS
7741 || mips_split_addresses_p ()
7742 || TARGET_MIPS16)
7743 {
7744 mips_split_p[SYMBOL_ABSOLUTE] = true;
7745 mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
7746 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7747 }
7748 }
7749 }
7750
7751 if (TARGET_MIPS16)
7752 {
7753 /* The high part is provided by a pseudo copy of $gp. */
7754 mips_split_p[SYMBOL_GP_RELATIVE] = true;
7755 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
7756 }
7757 else if (TARGET_EXPLICIT_RELOCS)
7758 /* Small data constants are kept whole until after reload,
7759 then lowered by mips_rewrite_small_data. */
7760 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
7761
7762 if (TARGET_EXPLICIT_RELOCS)
7763 {
7764 mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
7765 if (TARGET_NEWABI)
7766 {
7767 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
7768 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
7769 }
7770 else
7771 {
7772 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
7773 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
7774 }
7775 if (TARGET_MIPS16)
7776 /* Expose the use of $28 as soon as possible. */
7777 mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
7778
7779 if (TARGET_XGOT)
7780 {
7781 /* The HIGH and LO_SUM are matched by special .md patterns. */
7782 mips_split_p[SYMBOL_GOT_DISP] = true;
7783
7784 mips_split_p[SYMBOL_GOTOFF_DISP] = true;
7785 mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
7786 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
7787
7788 mips_split_p[SYMBOL_GOTOFF_CALL] = true;
7789 mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
7790 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
7791 }
7792 else
7793 {
7794 if (TARGET_NEWABI)
7795 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
7796 else
7797 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
7798 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
7799 if (TARGET_MIPS16)
7800 /* Expose the use of $28 as soon as possible. */
7801 mips_split_p[SYMBOL_GOT_DISP] = true;
7802 }
7803 }
7804
7805 if (TARGET_NEWABI)
7806 {
7807 mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
7808 mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
7809 mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
7810 }
7811
7812 mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
7813 mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
7814
7815 if (TARGET_MIPS16_PCREL_LOADS)
7816 {
7817 mips_use_pcrel_pool_p[SYMBOL_DTPREL] = true;
7818 mips_use_pcrel_pool_p[SYMBOL_TPREL] = true;
7819 }
7820 else
7821 {
7822 mips_split_p[SYMBOL_DTPREL] = true;
7823 mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
7824 mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
7825
7826 mips_split_p[SYMBOL_TPREL] = true;
7827 mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
7828 mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
7829 }
7830
7831 mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
7832 mips_lo_relocs[SYMBOL_HALF] = "%half(";
7833 }
7834
7835 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
7836 in context CONTEXT. RELOCS is the array of relocations to use. */
7837
7838 static void
7839 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
7840 const char **relocs)
7841 {
7842 enum mips_symbol_type symbol_type;
7843 const char *p;
7844
7845 symbol_type = mips_classify_symbolic_expression (op, context);
7846 gcc_assert (relocs[symbol_type]);
7847
7848 fputs (relocs[symbol_type], file);
7849 output_addr_const (file, mips_strip_unspec_address (op));
7850 for (p = relocs[symbol_type]; *p != 0; p++)
7851 if (*p == '(')
7852 fputc (')', file);
7853 }
7854
7855 /* Start a new block with the given asm switch enabled. If we need
7856 to print a directive, emit PREFIX before it and SUFFIX after it. */
7857
7858 static void
7859 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
7860 const char *prefix, const char *suffix)
7861 {
7862 if (asm_switch->nesting_level == 0)
7863 fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
7864 asm_switch->nesting_level++;
7865 }
7866
7867 /* Likewise, but end a block. */
7868
7869 static void
7870 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
7871 const char *prefix, const char *suffix)
7872 {
7873 gcc_assert (asm_switch->nesting_level);
7874 asm_switch->nesting_level--;
7875 if (asm_switch->nesting_level == 0)
7876 fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
7877 }
7878
7879 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
7880 that either print a complete line or print nothing. */
7881
7882 void
7883 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
7884 {
7885 mips_push_asm_switch_1 (asm_switch, "\t", "\n");
7886 }
7887
7888 void
7889 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
7890 {
7891 mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
7892 }
7893
7894 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
7895 The punctuation characters are:
7896
7897 '(' Start a nested ".set noreorder" block.
7898 ')' End a nested ".set noreorder" block.
7899 '[' Start a nested ".set noat" block.
7900 ']' End a nested ".set noat" block.
7901 '<' Start a nested ".set nomacro" block.
7902 '>' End a nested ".set nomacro" block.
7903 '*' Behave like %(%< if generating a delayed-branch sequence.
7904 '#' Print a nop if in a ".set noreorder" block.
7905 '/' Like '#', but do nothing within a delayed-branch sequence.
7906 '?' Print "l" if mips_branch_likely is true
7907 '~' Print a nop if mips_branch_likely is true
7908 '.' Print the name of the register with a hard-wired zero (zero or $0).
7909 '@' Print the name of the assembler temporary register (at or $1).
7910 '^' Print the name of the pic call-through register (t9 or $25).
7911 '+' Print the name of the gp register (usually gp or $28).
7912 '$' Print the name of the stack pointer register (sp or $29).
7913 ':' Print "c" to use the compact version if the delay slot is a nop.
7914 '!' Print "s" to use the short version if the delay slot contains a
7915 16-bit instruction.
7916
7917 See also mips_init_print_operand_pucnt. */
7918
7919 static void
7920 mips_print_operand_punctuation (FILE *file, int ch)
7921 {
7922 switch (ch)
7923 {
7924 case '(':
7925 mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
7926 break;
7927
7928 case ')':
7929 mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
7930 break;
7931
7932 case '[':
7933 mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
7934 break;
7935
7936 case ']':
7937 mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
7938 break;
7939
7940 case '<':
7941 mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
7942 break;
7943
7944 case '>':
7945 mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
7946 break;
7947
7948 case '*':
7949 if (final_sequence != 0)
7950 {
7951 mips_print_operand_punctuation (file, '(');
7952 mips_print_operand_punctuation (file, '<');
7953 }
7954 break;
7955
7956 case '#':
7957 if (mips_noreorder.nesting_level > 0)
7958 fputs ("\n\tnop", file);
7959 break;
7960
7961 case '/':
7962 /* Print an extra newline so that the delayed insn is separated
7963 from the following ones. This looks neater and is consistent
7964 with non-nop delayed sequences. */
7965 if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
7966 fputs ("\n\tnop\n", file);
7967 break;
7968
7969 case '?':
7970 if (mips_branch_likely)
7971 putc ('l', file);
7972 break;
7973
7974 case '~':
7975 if (mips_branch_likely)
7976 fputs ("\n\tnop", file);
7977 break;
7978
7979 case '.':
7980 fputs (reg_names[GP_REG_FIRST + 0], file);
7981 break;
7982
7983 case '@':
7984 fputs (reg_names[AT_REGNUM], file);
7985 break;
7986
7987 case '^':
7988 fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
7989 break;
7990
7991 case '+':
7992 fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
7993 break;
7994
7995 case '$':
7996 fputs (reg_names[STACK_POINTER_REGNUM], file);
7997 break;
7998
7999 case ':':
8000 /* When final_sequence is 0, the delay slot will be a nop. We can
8001 use the compact version for microMIPS. */
8002 if (final_sequence == 0)
8003 putc ('c', file);
8004 break;
8005
8006 case '!':
8007 /* If the delay slot instruction is short, then use the
8008 compact version. */
8009 if (final_sequence == 0
8010 || get_attr_length (XVECEXP (final_sequence, 0, 1)) == 2)
8011 putc ('s', file);
8012 break;
8013
8014 default:
8015 gcc_unreachable ();
8016 break;
8017 }
8018 }
8019
8020 /* Initialize mips_print_operand_punct. */
8021
8022 static void
8023 mips_init_print_operand_punct (void)
8024 {
8025 const char *p;
8026
8027 for (p = "()[]<>*#/?~.@^+$:!"; *p; p++)
8028 mips_print_operand_punct[(unsigned char) *p] = true;
8029 }
8030
8031 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
8032 associated with condition CODE. Print the condition part of the
8033 opcode to FILE. */
8034
8035 static void
8036 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
8037 {
8038 switch (code)
8039 {
8040 case EQ:
8041 case NE:
8042 case GT:
8043 case GE:
8044 case LT:
8045 case LE:
8046 case GTU:
8047 case GEU:
8048 case LTU:
8049 case LEU:
8050 /* Conveniently, the MIPS names for these conditions are the same
8051 as their RTL equivalents. */
8052 fputs (GET_RTX_NAME (code), file);
8053 break;
8054
8055 default:
8056 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8057 break;
8058 }
8059 }
8060
8061 /* Likewise floating-point branches. */
8062
8063 static void
8064 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
8065 {
8066 switch (code)
8067 {
8068 case EQ:
8069 fputs ("c1f", file);
8070 break;
8071
8072 case NE:
8073 fputs ("c1t", file);
8074 break;
8075
8076 default:
8077 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8078 break;
8079 }
8080 }
8081
8082 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
8083
8084 static bool
8085 mips_print_operand_punct_valid_p (unsigned char code)
8086 {
8087 return mips_print_operand_punct[code];
8088 }
8089
8090 /* Implement TARGET_PRINT_OPERAND. The MIPS-specific operand codes are:
8091
8092 'X' Print CONST_INT OP in hexadecimal format.
8093 'x' Print the low 16 bits of CONST_INT OP in hexadecimal format.
8094 'd' Print CONST_INT OP in decimal.
8095 'm' Print one less than CONST_INT OP in decimal.
8096 'h' Print the high-part relocation associated with OP, after stripping
8097 any outermost HIGH.
8098 'R' Print the low-part relocation associated with OP.
8099 'C' Print the integer branch condition for comparison OP.
8100 'N' Print the inverse of the integer branch condition for comparison OP.
8101 'F' Print the FPU branch condition for comparison OP.
8102 'W' Print the inverse of the FPU branch condition for comparison OP.
8103 'T' Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
8104 'z' for (eq:?I ...), 'n' for (ne:?I ...).
8105 't' Like 'T', but with the EQ/NE cases reversed
8106 'Y' Print mips_fp_conditions[INTVAL (OP)]
8107 'Z' Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
8108 'q' Print a DSP accumulator register.
8109 'D' Print the second part of a double-word register or memory operand.
8110 'L' Print the low-order register in a double-word register operand.
8111 'M' Print high-order register in a double-word register operand.
8112 'z' Print $0 if OP is zero, otherwise print OP normally.
8113 'b' Print the address of a memory operand, without offset. */
8114
8115 static void
8116 mips_print_operand (FILE *file, rtx op, int letter)
8117 {
8118 enum rtx_code code;
8119
8120 if (mips_print_operand_punct_valid_p (letter))
8121 {
8122 mips_print_operand_punctuation (file, letter);
8123 return;
8124 }
8125
8126 gcc_assert (op);
8127 code = GET_CODE (op);
8128
8129 switch (letter)
8130 {
8131 case 'X':
8132 if (CONST_INT_P (op))
8133 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
8134 else
8135 output_operand_lossage ("invalid use of '%%%c'", letter);
8136 break;
8137
8138 case 'x':
8139 if (CONST_INT_P (op))
8140 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
8141 else
8142 output_operand_lossage ("invalid use of '%%%c'", letter);
8143 break;
8144
8145 case 'd':
8146 if (CONST_INT_P (op))
8147 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
8148 else
8149 output_operand_lossage ("invalid use of '%%%c'", letter);
8150 break;
8151
8152 case 'm':
8153 if (CONST_INT_P (op))
8154 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
8155 else
8156 output_operand_lossage ("invalid use of '%%%c'", letter);
8157 break;
8158
8159 case 'h':
8160 if (code == HIGH)
8161 op = XEXP (op, 0);
8162 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
8163 break;
8164
8165 case 'R':
8166 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
8167 break;
8168
8169 case 'C':
8170 mips_print_int_branch_condition (file, code, letter);
8171 break;
8172
8173 case 'N':
8174 mips_print_int_branch_condition (file, reverse_condition (code), letter);
8175 break;
8176
8177 case 'F':
8178 mips_print_float_branch_condition (file, code, letter);
8179 break;
8180
8181 case 'W':
8182 mips_print_float_branch_condition (file, reverse_condition (code),
8183 letter);
8184 break;
8185
8186 case 'T':
8187 case 't':
8188 {
8189 int truth = (code == NE) == (letter == 'T');
8190 fputc ("zfnt"[truth * 2 + (GET_MODE (op) == CCmode)], file);
8191 }
8192 break;
8193
8194 case 'Y':
8195 if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
8196 fputs (mips_fp_conditions[UINTVAL (op)], file);
8197 else
8198 output_operand_lossage ("'%%%c' is not a valid operand prefix",
8199 letter);
8200 break;
8201
8202 case 'Z':
8203 if (ISA_HAS_8CC)
8204 {
8205 mips_print_operand (file, op, 0);
8206 fputc (',', file);
8207 }
8208 break;
8209
8210 case 'q':
8211 if (code == REG && MD_REG_P (REGNO (op)))
8212 fprintf (file, "$ac0");
8213 else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
8214 fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
8215 else
8216 output_operand_lossage ("invalid use of '%%%c'", letter);
8217 break;
8218
8219 default:
8220 switch (code)
8221 {
8222 case REG:
8223 {
8224 unsigned int regno = REGNO (op);
8225 if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
8226 || (letter == 'L' && TARGET_BIG_ENDIAN)
8227 || letter == 'D')
8228 regno++;
8229 else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
8230 output_operand_lossage ("invalid use of '%%%c'", letter);
8231 /* We need to print $0 .. $31 for COP0 registers. */
8232 if (COP0_REG_P (regno))
8233 fprintf (file, "$%s", &reg_names[regno][4]);
8234 else
8235 fprintf (file, "%s", reg_names[regno]);
8236 }
8237 break;
8238
8239 case MEM:
8240 if (letter == 'D')
8241 output_address (plus_constant (Pmode, XEXP (op, 0), 4));
8242 else if (letter == 'b')
8243 {
8244 gcc_assert (REG_P (XEXP (op, 0)));
8245 mips_print_operand (file, XEXP (op, 0), 0);
8246 }
8247 else if (letter && letter != 'z')
8248 output_operand_lossage ("invalid use of '%%%c'", letter);
8249 else
8250 output_address (XEXP (op, 0));
8251 break;
8252
8253 default:
8254 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
8255 fputs (reg_names[GP_REG_FIRST], file);
8256 else if (letter && letter != 'z')
8257 output_operand_lossage ("invalid use of '%%%c'", letter);
8258 else if (CONST_GP_P (op))
8259 fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
8260 else
8261 output_addr_const (file, mips_strip_unspec_address (op));
8262 break;
8263 }
8264 }
8265 }
8266
8267 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
8268
8269 static void
8270 mips_print_operand_address (FILE *file, rtx x)
8271 {
8272 struct mips_address_info addr;
8273
8274 if (mips_classify_address (&addr, x, word_mode, true))
8275 switch (addr.type)
8276 {
8277 case ADDRESS_REG:
8278 mips_print_operand (file, addr.offset, 0);
8279 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8280 return;
8281
8282 case ADDRESS_LO_SUM:
8283 mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
8284 mips_lo_relocs);
8285 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8286 return;
8287
8288 case ADDRESS_CONST_INT:
8289 output_addr_const (file, x);
8290 fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
8291 return;
8292
8293 case ADDRESS_SYMBOLIC:
8294 output_addr_const (file, mips_strip_unspec_address (x));
8295 return;
8296 }
8297 gcc_unreachable ();
8298 }
8299 \f
8300 /* Implement TARGET_ENCODE_SECTION_INFO. */
8301
8302 static void
8303 mips_encode_section_info (tree decl, rtx rtl, int first)
8304 {
8305 default_encode_section_info (decl, rtl, first);
8306
8307 if (TREE_CODE (decl) == FUNCTION_DECL)
8308 {
8309 rtx symbol = XEXP (rtl, 0);
8310 tree type = TREE_TYPE (decl);
8311
8312 /* Encode whether the symbol is short or long. */
8313 if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
8314 || mips_far_type_p (type))
8315 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
8316 }
8317 }
8318
8319 /* Implement TARGET_SELECT_RTX_SECTION. */
8320
8321 static section *
8322 mips_select_rtx_section (enum machine_mode mode, rtx x,
8323 unsigned HOST_WIDE_INT align)
8324 {
8325 /* ??? Consider using mergeable small data sections. */
8326 if (mips_rtx_constant_in_small_data_p (mode))
8327 return get_named_section (NULL, ".sdata", 0);
8328
8329 return default_elf_select_rtx_section (mode, x, align);
8330 }
8331
8332 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
8333
8334 The complication here is that, with the combination TARGET_ABICALLS
8335 && !TARGET_ABSOLUTE_ABICALLS && !TARGET_GPWORD, jump tables will use
8336 absolute addresses, and should therefore not be included in the
8337 read-only part of a DSO. Handle such cases by selecting a normal
8338 data section instead of a read-only one. The logic apes that in
8339 default_function_rodata_section. */
8340
8341 static section *
8342 mips_function_rodata_section (tree decl)
8343 {
8344 if (!TARGET_ABICALLS || TARGET_ABSOLUTE_ABICALLS || TARGET_GPWORD)
8345 return default_function_rodata_section (decl);
8346
8347 if (decl && DECL_SECTION_NAME (decl))
8348 {
8349 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
8350 if (DECL_ONE_ONLY (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
8351 {
8352 char *rname = ASTRDUP (name);
8353 rname[14] = 'd';
8354 return get_section (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
8355 }
8356 else if (flag_function_sections
8357 && flag_data_sections
8358 && strncmp (name, ".text.", 6) == 0)
8359 {
8360 char *rname = ASTRDUP (name);
8361 memcpy (rname + 1, "data", 4);
8362 return get_section (rname, SECTION_WRITE, decl);
8363 }
8364 }
8365 return data_section;
8366 }
8367
8368 /* Implement TARGET_IN_SMALL_DATA_P. */
8369
8370 static bool
8371 mips_in_small_data_p (const_tree decl)
8372 {
8373 unsigned HOST_WIDE_INT size;
8374
8375 if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
8376 return false;
8377
8378 /* We don't yet generate small-data references for -mabicalls
8379 or VxWorks RTP code. See the related -G handling in
8380 mips_option_override. */
8381 if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
8382 return false;
8383
8384 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
8385 {
8386 const char *name;
8387
8388 /* Reject anything that isn't in a known small-data section. */
8389 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
8390 if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
8391 return false;
8392
8393 /* If a symbol is defined externally, the assembler will use the
8394 usual -G rules when deciding how to implement macros. */
8395 if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
8396 return true;
8397 }
8398 else if (TARGET_EMBEDDED_DATA)
8399 {
8400 /* Don't put constants into the small data section: we want them
8401 to be in ROM rather than RAM. */
8402 if (TREE_CODE (decl) != VAR_DECL)
8403 return false;
8404
8405 if (TREE_READONLY (decl)
8406 && !TREE_SIDE_EFFECTS (decl)
8407 && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
8408 return false;
8409 }
8410
8411 /* Enforce -mlocal-sdata. */
8412 if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
8413 return false;
8414
8415 /* Enforce -mextern-sdata. */
8416 if (!TARGET_EXTERN_SDATA && DECL_P (decl))
8417 {
8418 if (DECL_EXTERNAL (decl))
8419 return false;
8420 if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
8421 return false;
8422 }
8423
8424 /* We have traditionally not treated zero-sized objects as small data,
8425 so this is now effectively part of the ABI. */
8426 size = int_size_in_bytes (TREE_TYPE (decl));
8427 return size > 0 && size <= mips_small_data_threshold;
8428 }
8429
8430 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P. We don't want to use
8431 anchors for small data: the GP register acts as an anchor in that
8432 case. We also don't want to use them for PC-relative accesses,
8433 where the PC acts as an anchor. */
8434
8435 static bool
8436 mips_use_anchors_for_symbol_p (const_rtx symbol)
8437 {
8438 switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
8439 {
8440 case SYMBOL_PC_RELATIVE:
8441 case SYMBOL_GP_RELATIVE:
8442 return false;
8443
8444 default:
8445 return default_use_anchors_for_symbol_p (symbol);
8446 }
8447 }
8448 \f
8449 /* The MIPS debug format wants all automatic variables and arguments
8450 to be in terms of the virtual frame pointer (stack pointer before
8451 any adjustment in the function), while the MIPS 3.0 linker wants
8452 the frame pointer to be the stack pointer after the initial
8453 adjustment. So, we do the adjustment here. The arg pointer (which
8454 is eliminated) points to the virtual frame pointer, while the frame
8455 pointer (which may be eliminated) points to the stack pointer after
8456 the initial adjustments. */
8457
8458 HOST_WIDE_INT
8459 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
8460 {
8461 rtx offset2 = const0_rtx;
8462 rtx reg = eliminate_constant_term (addr, &offset2);
8463
8464 if (offset == 0)
8465 offset = INTVAL (offset2);
8466
8467 if (reg == stack_pointer_rtx
8468 || reg == frame_pointer_rtx
8469 || reg == hard_frame_pointer_rtx)
8470 {
8471 offset -= cfun->machine->frame.total_size;
8472 if (reg == hard_frame_pointer_rtx)
8473 offset += cfun->machine->frame.hard_frame_pointer_offset;
8474 }
8475
8476 return offset;
8477 }
8478 \f
8479 /* Implement ASM_OUTPUT_EXTERNAL. */
8480
8481 void
8482 mips_output_external (FILE *file, tree decl, const char *name)
8483 {
8484 default_elf_asm_output_external (file, decl, name);
8485
8486 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8487 set in order to avoid putting out names that are never really
8488 used. */
8489 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
8490 {
8491 if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
8492 {
8493 /* When using assembler macros, emit .extern directives for
8494 all small-data externs so that the assembler knows how
8495 big they are.
8496
8497 In most cases it would be safe (though pointless) to emit
8498 .externs for other symbols too. One exception is when an
8499 object is within the -G limit but declared by the user to
8500 be in a section other than .sbss or .sdata. */
8501 fputs ("\t.extern\t", file);
8502 assemble_name (file, name);
8503 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
8504 int_size_in_bytes (TREE_TYPE (decl)));
8505 }
8506 }
8507 }
8508
8509 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8510
8511 static void
8512 mips_output_filename (FILE *stream, const char *name)
8513 {
8514 /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
8515 directives. */
8516 if (write_symbols == DWARF2_DEBUG)
8517 return;
8518 else if (mips_output_filename_first_time)
8519 {
8520 mips_output_filename_first_time = 0;
8521 num_source_filenames += 1;
8522 current_function_file = name;
8523 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8524 output_quoted_string (stream, name);
8525 putc ('\n', stream);
8526 }
8527 /* If we are emitting stabs, let dbxout.c handle this (except for
8528 the mips_output_filename_first_time case). */
8529 else if (write_symbols == DBX_DEBUG)
8530 return;
8531 else if (name != current_function_file
8532 && strcmp (name, current_function_file) != 0)
8533 {
8534 num_source_filenames += 1;
8535 current_function_file = name;
8536 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8537 output_quoted_string (stream, name);
8538 putc ('\n', stream);
8539 }
8540 }
8541
8542 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL. */
8543
8544 static void ATTRIBUTE_UNUSED
8545 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
8546 {
8547 switch (size)
8548 {
8549 case 4:
8550 fputs ("\t.dtprelword\t", file);
8551 break;
8552
8553 case 8:
8554 fputs ("\t.dtpreldword\t", file);
8555 break;
8556
8557 default:
8558 gcc_unreachable ();
8559 }
8560 output_addr_const (file, x);
8561 fputs ("+0x8000", file);
8562 }
8563
8564 /* Implement TARGET_DWARF_REGISTER_SPAN. */
8565
8566 static rtx
8567 mips_dwarf_register_span (rtx reg)
8568 {
8569 rtx high, low;
8570 enum machine_mode mode;
8571
8572 /* By default, GCC maps increasing register numbers to increasing
8573 memory locations, but paired FPRs are always little-endian,
8574 regardless of the prevailing endianness. */
8575 mode = GET_MODE (reg);
8576 if (FP_REG_P (REGNO (reg))
8577 && TARGET_BIG_ENDIAN
8578 && MAX_FPRS_PER_FMT > 1
8579 && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
8580 {
8581 gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
8582 high = mips_subword (reg, true);
8583 low = mips_subword (reg, false);
8584 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
8585 }
8586
8587 return NULL_RTX;
8588 }
8589
8590 /* DSP ALU can bypass data with no delays for the following pairs. */
8591 enum insn_code dspalu_bypass_table[][2] =
8592 {
8593 {CODE_FOR_mips_addsc, CODE_FOR_mips_addwc},
8594 {CODE_FOR_mips_cmpu_eq_qb, CODE_FOR_mips_pick_qb},
8595 {CODE_FOR_mips_cmpu_lt_qb, CODE_FOR_mips_pick_qb},
8596 {CODE_FOR_mips_cmpu_le_qb, CODE_FOR_mips_pick_qb},
8597 {CODE_FOR_mips_cmp_eq_ph, CODE_FOR_mips_pick_ph},
8598 {CODE_FOR_mips_cmp_lt_ph, CODE_FOR_mips_pick_ph},
8599 {CODE_FOR_mips_cmp_le_ph, CODE_FOR_mips_pick_ph},
8600 {CODE_FOR_mips_wrdsp, CODE_FOR_mips_insv}
8601 };
8602
8603 int
8604 mips_dspalu_bypass_p (rtx out_insn, rtx in_insn)
8605 {
8606 int i;
8607 int num_bypass = ARRAY_SIZE (dspalu_bypass_table);
8608 enum insn_code out_icode = (enum insn_code) INSN_CODE (out_insn);
8609 enum insn_code in_icode = (enum insn_code) INSN_CODE (in_insn);
8610
8611 for (i = 0; i < num_bypass; i++)
8612 {
8613 if (out_icode == dspalu_bypass_table[i][0]
8614 && in_icode == dspalu_bypass_table[i][1])
8615 return true;
8616 }
8617
8618 return false;
8619 }
8620 /* Implement ASM_OUTPUT_ASCII. */
8621
8622 void
8623 mips_output_ascii (FILE *stream, const char *string, size_t len)
8624 {
8625 size_t i;
8626 int cur_pos;
8627
8628 cur_pos = 17;
8629 fprintf (stream, "\t.ascii\t\"");
8630 for (i = 0; i < len; i++)
8631 {
8632 int c;
8633
8634 c = (unsigned char) string[i];
8635 if (ISPRINT (c))
8636 {
8637 if (c == '\\' || c == '\"')
8638 {
8639 putc ('\\', stream);
8640 cur_pos++;
8641 }
8642 putc (c, stream);
8643 cur_pos++;
8644 }
8645 else
8646 {
8647 fprintf (stream, "\\%03o", c);
8648 cur_pos += 4;
8649 }
8650
8651 if (cur_pos > 72 && i+1 < len)
8652 {
8653 cur_pos = 17;
8654 fprintf (stream, "\"\n\t.ascii\t\"");
8655 }
8656 }
8657 fprintf (stream, "\"\n");
8658 }
8659
8660 /* Return the pseudo-op for full SYMBOL_(D)TPREL address *ADDR.
8661 Update *ADDR with the operand that should be printed. */
8662
8663 const char *
8664 mips_output_tls_reloc_directive (rtx *addr)
8665 {
8666 enum mips_symbol_type type;
8667
8668 type = mips_classify_symbolic_expression (*addr, SYMBOL_CONTEXT_LEA);
8669 *addr = mips_strip_unspec_address (*addr);
8670 switch (type)
8671 {
8672 case SYMBOL_DTPREL:
8673 return Pmode == SImode ? ".dtprelword\t%0" : ".dtpreldword\t%0";
8674
8675 case SYMBOL_TPREL:
8676 return Pmode == SImode ? ".tprelword\t%0" : ".tpreldword\t%0";
8677
8678 default:
8679 gcc_unreachable ();
8680 }
8681 }
8682
8683 /* Emit either a label, .comm, or .lcomm directive. When using assembler
8684 macros, mark the symbol as written so that mips_asm_output_external
8685 won't emit an .extern for it. STREAM is the output file, NAME is the
8686 name of the symbol, INIT_STRING is the string that should be written
8687 before the symbol and FINAL_STRING is the string that should be
8688 written after it. FINAL_STRING is a printf format that consumes the
8689 remaining arguments. */
8690
8691 void
8692 mips_declare_object (FILE *stream, const char *name, const char *init_string,
8693 const char *final_string, ...)
8694 {
8695 va_list ap;
8696
8697 fputs (init_string, stream);
8698 assemble_name (stream, name);
8699 va_start (ap, final_string);
8700 vfprintf (stream, final_string, ap);
8701 va_end (ap);
8702
8703 if (!TARGET_EXPLICIT_RELOCS)
8704 {
8705 tree name_tree = get_identifier (name);
8706 TREE_ASM_WRITTEN (name_tree) = 1;
8707 }
8708 }
8709
8710 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
8711 NAME is the name of the object and ALIGN is the required alignment
8712 in bytes. TAKES_ALIGNMENT_P is true if the directive takes a third
8713 alignment argument. */
8714
8715 void
8716 mips_declare_common_object (FILE *stream, const char *name,
8717 const char *init_string,
8718 unsigned HOST_WIDE_INT size,
8719 unsigned int align, bool takes_alignment_p)
8720 {
8721 if (!takes_alignment_p)
8722 {
8723 size += (align / BITS_PER_UNIT) - 1;
8724 size -= size % (align / BITS_PER_UNIT);
8725 mips_declare_object (stream, name, init_string,
8726 "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
8727 }
8728 else
8729 mips_declare_object (stream, name, init_string,
8730 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
8731 size, align / BITS_PER_UNIT);
8732 }
8733
8734 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON. This is usually the same as the
8735 elfos.h version, but we also need to handle -muninit-const-in-rodata. */
8736
8737 void
8738 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
8739 unsigned HOST_WIDE_INT size,
8740 unsigned int align)
8741 {
8742 /* If the target wants uninitialized const declarations in
8743 .rdata then don't put them in .comm. */
8744 if (TARGET_EMBEDDED_DATA
8745 && TARGET_UNINIT_CONST_IN_RODATA
8746 && TREE_CODE (decl) == VAR_DECL
8747 && TREE_READONLY (decl)
8748 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
8749 {
8750 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
8751 targetm.asm_out.globalize_label (stream, name);
8752
8753 switch_to_section (readonly_data_section);
8754 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
8755 mips_declare_object (stream, name, "",
8756 ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
8757 size);
8758 }
8759 else
8760 mips_declare_common_object (stream, name, "\n\t.comm\t",
8761 size, align, true);
8762 }
8763
8764 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
8765 extern int size_directive_output;
8766
8767 /* Implement ASM_DECLARE_OBJECT_NAME. This is like most of the standard ELF
8768 definitions except that it uses mips_declare_object to emit the label. */
8769
8770 void
8771 mips_declare_object_name (FILE *stream, const char *name,
8772 tree decl ATTRIBUTE_UNUSED)
8773 {
8774 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
8775 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
8776 #endif
8777
8778 size_directive_output = 0;
8779 if (!flag_inhibit_size_directive && DECL_SIZE (decl))
8780 {
8781 HOST_WIDE_INT size;
8782
8783 size_directive_output = 1;
8784 size = int_size_in_bytes (TREE_TYPE (decl));
8785 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8786 }
8787
8788 mips_declare_object (stream, name, "", ":\n");
8789 }
8790
8791 /* Implement ASM_FINISH_DECLARE_OBJECT. This is generic ELF stuff. */
8792
8793 void
8794 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
8795 {
8796 const char *name;
8797
8798 name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8799 if (!flag_inhibit_size_directive
8800 && DECL_SIZE (decl) != 0
8801 && !at_end
8802 && top_level
8803 && DECL_INITIAL (decl) == error_mark_node
8804 && !size_directive_output)
8805 {
8806 HOST_WIDE_INT size;
8807
8808 size_directive_output = 1;
8809 size = int_size_in_bytes (TREE_TYPE (decl));
8810 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8811 }
8812 }
8813 #endif
8814 \f
8815 /* Return the FOO in the name of the ".mdebug.FOO" section associated
8816 with the current ABI. */
8817
8818 static const char *
8819 mips_mdebug_abi_name (void)
8820 {
8821 switch (mips_abi)
8822 {
8823 case ABI_32:
8824 return "abi32";
8825 case ABI_O64:
8826 return "abiO64";
8827 case ABI_N32:
8828 return "abiN32";
8829 case ABI_64:
8830 return "abi64";
8831 case ABI_EABI:
8832 return TARGET_64BIT ? "eabi64" : "eabi32";
8833 default:
8834 gcc_unreachable ();
8835 }
8836 }
8837
8838 /* Implement TARGET_ASM_FILE_START. */
8839
8840 static void
8841 mips_file_start (void)
8842 {
8843 default_file_start ();
8844
8845 /* Generate a special section to describe the ABI switches used to
8846 produce the resultant binary. */
8847
8848 /* Record the ABI itself. Modern versions of binutils encode
8849 this information in the ELF header flags, but GDB needs the
8850 information in order to correctly debug binaries produced by
8851 older binutils. See the function mips_gdbarch_init in
8852 gdb/mips-tdep.c. */
8853 fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
8854 mips_mdebug_abi_name ());
8855
8856 /* There is no ELF header flag to distinguish long32 forms of the
8857 EABI from long64 forms. Emit a special section to help tools
8858 such as GDB. Do the same for o64, which is sometimes used with
8859 -mlong64. */
8860 if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
8861 fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
8862 "\t.previous\n", TARGET_LONG64 ? 64 : 32);
8863
8864 /* Record the NaN encoding. */
8865 if (HAVE_AS_NAN || mips_nan != MIPS_IEEE_754_DEFAULT)
8866 fprintf (asm_out_file, "\t.nan\t%s\n",
8867 mips_nan == MIPS_IEEE_754_2008 ? "2008" : "legacy");
8868
8869 #ifdef HAVE_AS_GNU_ATTRIBUTE
8870 {
8871 int attr;
8872
8873 /* No floating-point operations, -mno-float. */
8874 if (TARGET_NO_FLOAT)
8875 attr = 0;
8876 /* Soft-float code, -msoft-float. */
8877 else if (!TARGET_HARD_FLOAT_ABI)
8878 attr = 3;
8879 /* Single-float code, -msingle-float. */
8880 else if (!TARGET_DOUBLE_FLOAT)
8881 attr = 2;
8882 /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64. */
8883 else if (!TARGET_64BIT && TARGET_FLOAT64)
8884 attr = 4;
8885 /* Regular FP code, FP regs same size as GP regs, -mdouble-float. */
8886 else
8887 attr = 1;
8888
8889 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
8890 }
8891 #endif
8892
8893 /* If TARGET_ABICALLS, tell GAS to generate -KPIC code. */
8894 if (TARGET_ABICALLS)
8895 {
8896 fprintf (asm_out_file, "\t.abicalls\n");
8897 if (TARGET_ABICALLS_PIC0)
8898 fprintf (asm_out_file, "\t.option\tpic0\n");
8899 }
8900
8901 if (flag_verbose_asm)
8902 fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
8903 ASM_COMMENT_START,
8904 mips_small_data_threshold, mips_arch_info->name, mips_isa);
8905 }
8906
8907 /* Implement TARGET_ASM_CODE_END. */
8908
8909 static void
8910 mips_code_end (void)
8911 {
8912 if (mips_need_mips16_rdhwr_p)
8913 mips_output_mips16_rdhwr ();
8914 }
8915 \f
8916 /* Make the last instruction frame-related and note that it performs
8917 the operation described by FRAME_PATTERN. */
8918
8919 static void
8920 mips_set_frame_expr (rtx frame_pattern)
8921 {
8922 rtx insn;
8923
8924 insn = get_last_insn ();
8925 RTX_FRAME_RELATED_P (insn) = 1;
8926 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
8927 frame_pattern,
8928 REG_NOTES (insn));
8929 }
8930
8931 /* Return a frame-related rtx that stores REG at MEM.
8932 REG must be a single register. */
8933
8934 static rtx
8935 mips_frame_set (rtx mem, rtx reg)
8936 {
8937 rtx set;
8938
8939 set = gen_rtx_SET (VOIDmode, mem, reg);
8940 RTX_FRAME_RELATED_P (set) = 1;
8941
8942 return set;
8943 }
8944
8945 /* Record that the epilogue has restored call-saved register REG. */
8946
8947 static void
8948 mips_add_cfa_restore (rtx reg)
8949 {
8950 mips_epilogue.cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
8951 mips_epilogue.cfa_restores);
8952 }
8953 \f
8954 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
8955 mips16e_s2_s8_regs[X], it must also save the registers in indexes
8956 X + 1 onwards. Likewise mips16e_a0_a3_regs. */
8957 static const unsigned char mips16e_s2_s8_regs[] = {
8958 30, 23, 22, 21, 20, 19, 18
8959 };
8960 static const unsigned char mips16e_a0_a3_regs[] = {
8961 4, 5, 6, 7
8962 };
8963
8964 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
8965 ordered from the uppermost in memory to the lowest in memory. */
8966 static const unsigned char mips16e_save_restore_regs[] = {
8967 31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
8968 };
8969
8970 /* Return the index of the lowest X in the range [0, SIZE) for which
8971 bit REGS[X] is set in MASK. Return SIZE if there is no such X. */
8972
8973 static unsigned int
8974 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
8975 unsigned int size)
8976 {
8977 unsigned int i;
8978
8979 for (i = 0; i < size; i++)
8980 if (BITSET_P (mask, regs[i]))
8981 break;
8982
8983 return i;
8984 }
8985
8986 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
8987 is the number of set bits. If *MASK_PTR contains REGS[X] for some X
8988 in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
8989 is true for all indexes (X, SIZE). */
8990
8991 static void
8992 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
8993 unsigned int size, unsigned int *num_regs_ptr)
8994 {
8995 unsigned int i;
8996
8997 i = mips16e_find_first_register (*mask_ptr, regs, size);
8998 for (i++; i < size; i++)
8999 if (!BITSET_P (*mask_ptr, regs[i]))
9000 {
9001 *num_regs_ptr += 1;
9002 *mask_ptr |= 1 << regs[i];
9003 }
9004 }
9005
9006 /* Return a simplified form of X using the register values in REG_VALUES.
9007 REG_VALUES[R] is the last value assigned to hard register R, or null
9008 if R has not been modified.
9009
9010 This function is rather limited, but is good enough for our purposes. */
9011
9012 static rtx
9013 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
9014 {
9015 x = avoid_constant_pool_reference (x);
9016
9017 if (UNARY_P (x))
9018 {
9019 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9020 return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
9021 x0, GET_MODE (XEXP (x, 0)));
9022 }
9023
9024 if (ARITHMETIC_P (x))
9025 {
9026 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9027 rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
9028 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
9029 }
9030
9031 if (REG_P (x)
9032 && reg_values[REGNO (x)]
9033 && !rtx_unstable_p (reg_values[REGNO (x)]))
9034 return reg_values[REGNO (x)];
9035
9036 return x;
9037 }
9038
9039 /* Return true if (set DEST SRC) stores an argument register into its
9040 caller-allocated save slot, storing the number of that argument
9041 register in *REGNO_PTR if so. REG_VALUES is as for
9042 mips16e_collect_propagate_value. */
9043
9044 static bool
9045 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
9046 unsigned int *regno_ptr)
9047 {
9048 unsigned int argno, regno;
9049 HOST_WIDE_INT offset, required_offset;
9050 rtx addr, base;
9051
9052 /* Check that this is a word-mode store. */
9053 if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
9054 return false;
9055
9056 /* Check that the register being saved is an unmodified argument
9057 register. */
9058 regno = REGNO (src);
9059 if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
9060 return false;
9061 argno = regno - GP_ARG_FIRST;
9062
9063 /* Check whether the address is an appropriate stack-pointer or
9064 frame-pointer access. */
9065 addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
9066 mips_split_plus (addr, &base, &offset);
9067 required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
9068 if (base == hard_frame_pointer_rtx)
9069 required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
9070 else if (base != stack_pointer_rtx)
9071 return false;
9072 if (offset != required_offset)
9073 return false;
9074
9075 *regno_ptr = regno;
9076 return true;
9077 }
9078
9079 /* A subroutine of mips_expand_prologue, called only when generating
9080 MIPS16e SAVE instructions. Search the start of the function for any
9081 instructions that save argument registers into their caller-allocated
9082 save slots. Delete such instructions and return a value N such that
9083 saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
9084 instructions redundant. */
9085
9086 static unsigned int
9087 mips16e_collect_argument_saves (void)
9088 {
9089 rtx reg_values[FIRST_PSEUDO_REGISTER];
9090 rtx insn, next, set, dest, src;
9091 unsigned int nargs, regno;
9092
9093 push_topmost_sequence ();
9094 nargs = 0;
9095 memset (reg_values, 0, sizeof (reg_values));
9096 for (insn = get_insns (); insn; insn = next)
9097 {
9098 next = NEXT_INSN (insn);
9099 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
9100 continue;
9101
9102 if (!INSN_P (insn))
9103 break;
9104
9105 set = PATTERN (insn);
9106 if (GET_CODE (set) != SET)
9107 break;
9108
9109 dest = SET_DEST (set);
9110 src = SET_SRC (set);
9111 if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
9112 {
9113 if (!BITSET_P (cfun->machine->frame.mask, regno))
9114 {
9115 delete_insn (insn);
9116 nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
9117 }
9118 }
9119 else if (REG_P (dest) && GET_MODE (dest) == word_mode)
9120 reg_values[REGNO (dest)]
9121 = mips16e_collect_propagate_value (src, reg_values);
9122 else
9123 break;
9124 }
9125 pop_topmost_sequence ();
9126
9127 return nargs;
9128 }
9129
9130 /* Return a move between register REGNO and memory location SP + OFFSET.
9131 REG_PARM_P is true if SP + OFFSET belongs to REG_PARM_STACK_SPACE.
9132 Make the move a load if RESTORE_P, otherwise make it a store. */
9133
9134 static rtx
9135 mips16e_save_restore_reg (bool restore_p, bool reg_parm_p,
9136 HOST_WIDE_INT offset, unsigned int regno)
9137 {
9138 rtx reg, mem;
9139
9140 mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx,
9141 offset));
9142 reg = gen_rtx_REG (SImode, regno);
9143 if (restore_p)
9144 {
9145 mips_add_cfa_restore (reg);
9146 return gen_rtx_SET (VOIDmode, reg, mem);
9147 }
9148 if (reg_parm_p)
9149 return gen_rtx_SET (VOIDmode, mem, reg);
9150 return mips_frame_set (mem, reg);
9151 }
9152
9153 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
9154 The instruction must:
9155
9156 - Allocate or deallocate SIZE bytes in total; SIZE is known
9157 to be nonzero.
9158
9159 - Save or restore as many registers in *MASK_PTR as possible.
9160 The instruction saves the first registers at the top of the
9161 allocated area, with the other registers below it.
9162
9163 - Save NARGS argument registers above the allocated area.
9164
9165 (NARGS is always zero if RESTORE_P.)
9166
9167 The SAVE and RESTORE instructions cannot save and restore all general
9168 registers, so there may be some registers left over for the caller to
9169 handle. Destructively modify *MASK_PTR so that it contains the registers
9170 that still need to be saved or restored. The caller can save these
9171 registers in the memory immediately below *OFFSET_PTR, which is a
9172 byte offset from the bottom of the allocated stack area. */
9173
9174 static rtx
9175 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
9176 HOST_WIDE_INT *offset_ptr, unsigned int nargs,
9177 HOST_WIDE_INT size)
9178 {
9179 rtx pattern, set;
9180 HOST_WIDE_INT offset, top_offset;
9181 unsigned int i, regno;
9182 int n;
9183
9184 gcc_assert (cfun->machine->frame.num_fp == 0);
9185
9186 /* Calculate the number of elements in the PARALLEL. We need one element
9187 for the stack adjustment, one for each argument register save, and one
9188 for each additional register move. */
9189 n = 1 + nargs;
9190 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9191 if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
9192 n++;
9193
9194 /* Create the final PARALLEL. */
9195 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
9196 n = 0;
9197
9198 /* Add the stack pointer adjustment. */
9199 set = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9200 plus_constant (Pmode, stack_pointer_rtx,
9201 restore_p ? size : -size));
9202 RTX_FRAME_RELATED_P (set) = 1;
9203 XVECEXP (pattern, 0, n++) = set;
9204
9205 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
9206 top_offset = restore_p ? size : 0;
9207
9208 /* Save the arguments. */
9209 for (i = 0; i < nargs; i++)
9210 {
9211 offset = top_offset + i * UNITS_PER_WORD;
9212 set = mips16e_save_restore_reg (restore_p, true, offset,
9213 GP_ARG_FIRST + i);
9214 XVECEXP (pattern, 0, n++) = set;
9215 }
9216
9217 /* Then fill in the other register moves. */
9218 offset = top_offset;
9219 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9220 {
9221 regno = mips16e_save_restore_regs[i];
9222 if (BITSET_P (*mask_ptr, regno))
9223 {
9224 offset -= UNITS_PER_WORD;
9225 set = mips16e_save_restore_reg (restore_p, false, offset, regno);
9226 XVECEXP (pattern, 0, n++) = set;
9227 *mask_ptr &= ~(1 << regno);
9228 }
9229 }
9230
9231 /* Tell the caller what offset it should use for the remaining registers. */
9232 *offset_ptr = size + (offset - top_offset);
9233
9234 gcc_assert (n == XVECLEN (pattern, 0));
9235
9236 return pattern;
9237 }
9238
9239 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
9240 pointer. Return true if PATTERN matches the kind of instruction
9241 generated by mips16e_build_save_restore. If INFO is nonnull,
9242 initialize it when returning true. */
9243
9244 bool
9245 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
9246 struct mips16e_save_restore_info *info)
9247 {
9248 unsigned int i, nargs, mask, extra;
9249 HOST_WIDE_INT top_offset, save_offset, offset;
9250 rtx set, reg, mem, base;
9251 int n;
9252
9253 if (!GENERATE_MIPS16E_SAVE_RESTORE)
9254 return false;
9255
9256 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
9257 top_offset = adjust > 0 ? adjust : 0;
9258
9259 /* Interpret all other members of the PARALLEL. */
9260 save_offset = top_offset - UNITS_PER_WORD;
9261 mask = 0;
9262 nargs = 0;
9263 i = 0;
9264 for (n = 1; n < XVECLEN (pattern, 0); n++)
9265 {
9266 /* Check that we have a SET. */
9267 set = XVECEXP (pattern, 0, n);
9268 if (GET_CODE (set) != SET)
9269 return false;
9270
9271 /* Check that the SET is a load (if restoring) or a store
9272 (if saving). */
9273 mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
9274 if (!MEM_P (mem))
9275 return false;
9276
9277 /* Check that the address is the sum of the stack pointer and a
9278 possibly-zero constant offset. */
9279 mips_split_plus (XEXP (mem, 0), &base, &offset);
9280 if (base != stack_pointer_rtx)
9281 return false;
9282
9283 /* Check that SET's other operand is a register. */
9284 reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
9285 if (!REG_P (reg))
9286 return false;
9287
9288 /* Check for argument saves. */
9289 if (offset == top_offset + nargs * UNITS_PER_WORD
9290 && REGNO (reg) == GP_ARG_FIRST + nargs)
9291 nargs++;
9292 else if (offset == save_offset)
9293 {
9294 while (mips16e_save_restore_regs[i++] != REGNO (reg))
9295 if (i == ARRAY_SIZE (mips16e_save_restore_regs))
9296 return false;
9297
9298 mask |= 1 << REGNO (reg);
9299 save_offset -= UNITS_PER_WORD;
9300 }
9301 else
9302 return false;
9303 }
9304
9305 /* Check that the restrictions on register ranges are met. */
9306 extra = 0;
9307 mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
9308 ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
9309 mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
9310 ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
9311 if (extra != 0)
9312 return false;
9313
9314 /* Make sure that the topmost argument register is not saved twice.
9315 The checks above ensure that the same is then true for the other
9316 argument registers. */
9317 if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
9318 return false;
9319
9320 /* Pass back information, if requested. */
9321 if (info)
9322 {
9323 info->nargs = nargs;
9324 info->mask = mask;
9325 info->size = (adjust > 0 ? adjust : -adjust);
9326 }
9327
9328 return true;
9329 }
9330
9331 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
9332 for the register range [MIN_REG, MAX_REG]. Return a pointer to
9333 the null terminator. */
9334
9335 static char *
9336 mips16e_add_register_range (char *s, unsigned int min_reg,
9337 unsigned int max_reg)
9338 {
9339 if (min_reg != max_reg)
9340 s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
9341 else
9342 s += sprintf (s, ",%s", reg_names[min_reg]);
9343 return s;
9344 }
9345
9346 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
9347 PATTERN and ADJUST are as for mips16e_save_restore_pattern_p. */
9348
9349 const char *
9350 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
9351 {
9352 static char buffer[300];
9353
9354 struct mips16e_save_restore_info info;
9355 unsigned int i, end;
9356 char *s;
9357
9358 /* Parse the pattern. */
9359 if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
9360 gcc_unreachable ();
9361
9362 /* Add the mnemonic. */
9363 s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
9364 s += strlen (s);
9365
9366 /* Save the arguments. */
9367 if (info.nargs > 1)
9368 s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
9369 reg_names[GP_ARG_FIRST + info.nargs - 1]);
9370 else if (info.nargs == 1)
9371 s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
9372
9373 /* Emit the amount of stack space to allocate or deallocate. */
9374 s += sprintf (s, "%d", (int) info.size);
9375
9376 /* Save or restore $16. */
9377 if (BITSET_P (info.mask, 16))
9378 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
9379
9380 /* Save or restore $17. */
9381 if (BITSET_P (info.mask, 17))
9382 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
9383
9384 /* Save or restore registers in the range $s2...$s8, which
9385 mips16e_s2_s8_regs lists in decreasing order. Note that this
9386 is a software register range; the hardware registers are not
9387 numbered consecutively. */
9388 end = ARRAY_SIZE (mips16e_s2_s8_regs);
9389 i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
9390 if (i < end)
9391 s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
9392 mips16e_s2_s8_regs[i]);
9393
9394 /* Save or restore registers in the range $a0...$a3. */
9395 end = ARRAY_SIZE (mips16e_a0_a3_regs);
9396 i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
9397 if (i < end)
9398 s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
9399 mips16e_a0_a3_regs[end - 1]);
9400
9401 /* Save or restore $31. */
9402 if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
9403 s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
9404
9405 return buffer;
9406 }
9407 \f
9408 /* Return true if the current function returns its value in a floating-point
9409 register in MIPS16 mode. */
9410
9411 static bool
9412 mips16_cfun_returns_in_fpr_p (void)
9413 {
9414 tree return_type = DECL_RESULT (current_function_decl);
9415 return (TARGET_MIPS16
9416 && TARGET_HARD_FLOAT_ABI
9417 && !aggregate_value_p (return_type, current_function_decl)
9418 && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
9419 }
9420
9421 /* Return true if predicate PRED is true for at least one instruction.
9422 Cache the result in *CACHE, and assume that the result is true
9423 if *CACHE is already true. */
9424
9425 static bool
9426 mips_find_gp_ref (bool *cache, bool (*pred) (rtx))
9427 {
9428 rtx insn;
9429
9430 if (!*cache)
9431 {
9432 push_topmost_sequence ();
9433 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9434 if (USEFUL_INSN_P (insn) && pred (insn))
9435 {
9436 *cache = true;
9437 break;
9438 }
9439 pop_topmost_sequence ();
9440 }
9441 return *cache;
9442 }
9443
9444 /* Return true if INSN refers to the global pointer in an "inflexible" way.
9445 See mips_cfun_has_inflexible_gp_ref_p for details. */
9446
9447 static bool
9448 mips_insn_has_inflexible_gp_ref_p (rtx insn)
9449 {
9450 /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
9451 indicate that the target could be a traditional MIPS
9452 lazily-binding stub. */
9453 return find_reg_fusage (insn, USE, pic_offset_table_rtx);
9454 }
9455
9456 /* Return true if the current function refers to the global pointer
9457 in a way that forces $28 to be valid. This means that we can't
9458 change the choice of global pointer, even for NewABI code.
9459
9460 One example of this (and one which needs several checks) is that
9461 $28 must be valid when calling traditional MIPS lazy-binding stubs.
9462 (This restriction does not apply to PLTs.) */
9463
9464 static bool
9465 mips_cfun_has_inflexible_gp_ref_p (void)
9466 {
9467 /* If the function has a nonlocal goto, $28 must hold the correct
9468 global pointer for the target function. That is, the target
9469 of the goto implicitly uses $28. */
9470 if (crtl->has_nonlocal_goto)
9471 return true;
9472
9473 if (TARGET_ABICALLS_PIC2)
9474 {
9475 /* Symbolic accesses implicitly use the global pointer unless
9476 -mexplicit-relocs is in effect. JAL macros to symbolic addresses
9477 might go to traditional MIPS lazy-binding stubs. */
9478 if (!TARGET_EXPLICIT_RELOCS)
9479 return true;
9480
9481 /* FUNCTION_PROFILER includes a JAL to _mcount, which again
9482 can be lazily-bound. */
9483 if (crtl->profile)
9484 return true;
9485
9486 /* MIPS16 functions that return in FPRs need to call an
9487 external libgcc routine. This call is only made explict
9488 during mips_expand_epilogue, and it too might be lazily bound. */
9489 if (mips16_cfun_returns_in_fpr_p ())
9490 return true;
9491 }
9492
9493 return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
9494 mips_insn_has_inflexible_gp_ref_p);
9495 }
9496
9497 /* Return true if INSN refers to the global pointer in a "flexible" way.
9498 See mips_cfun_has_flexible_gp_ref_p for details. */
9499
9500 static bool
9501 mips_insn_has_flexible_gp_ref_p (rtx insn)
9502 {
9503 return (get_attr_got (insn) != GOT_UNSET
9504 || mips_small_data_pattern_p (PATTERN (insn))
9505 || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
9506 }
9507
9508 /* Return true if the current function references the global pointer,
9509 but if those references do not inherently require the global pointer
9510 to be $28. Assume !mips_cfun_has_inflexible_gp_ref_p (). */
9511
9512 static bool
9513 mips_cfun_has_flexible_gp_ref_p (void)
9514 {
9515 /* Reload can sometimes introduce constant pool references
9516 into a function that otherwise didn't need them. For example,
9517 suppose we have an instruction like:
9518
9519 (set (reg:DF R1) (float:DF (reg:SI R2)))
9520
9521 If R2 turns out to be a constant such as 1, the instruction may
9522 have a REG_EQUAL note saying that R1 == 1.0. Reload then has
9523 the option of using this constant if R2 doesn't get allocated
9524 to a register.
9525
9526 In cases like these, reload will have added the constant to the
9527 pool but no instruction will yet refer to it. */
9528 if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
9529 return true;
9530
9531 return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
9532 mips_insn_has_flexible_gp_ref_p);
9533 }
9534
9535 /* Return the register that should be used as the global pointer
9536 within this function. Return INVALID_REGNUM if the function
9537 doesn't need a global pointer. */
9538
9539 static unsigned int
9540 mips_global_pointer (void)
9541 {
9542 unsigned int regno;
9543
9544 /* $gp is always available unless we're using a GOT. */
9545 if (!TARGET_USE_GOT)
9546 return GLOBAL_POINTER_REGNUM;
9547
9548 /* If there are inflexible references to $gp, we must use the
9549 standard register. */
9550 if (mips_cfun_has_inflexible_gp_ref_p ())
9551 return GLOBAL_POINTER_REGNUM;
9552
9553 /* If there are no current references to $gp, then the only uses
9554 we can introduce later are those involved in long branches. */
9555 if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
9556 return INVALID_REGNUM;
9557
9558 /* If the global pointer is call-saved, try to use a call-clobbered
9559 alternative. */
9560 if (TARGET_CALL_SAVED_GP && crtl->is_leaf)
9561 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9562 if (!df_regs_ever_live_p (regno)
9563 && call_really_used_regs[regno]
9564 && !fixed_regs[regno]
9565 && regno != PIC_FUNCTION_ADDR_REGNUM)
9566 return regno;
9567
9568 return GLOBAL_POINTER_REGNUM;
9569 }
9570
9571 /* Return true if the current function's prologue must load the global
9572 pointer value into pic_offset_table_rtx and store the same value in
9573 the function's cprestore slot (if any).
9574
9575 One problem we have to deal with is that, when emitting GOT-based
9576 position independent code, long-branch sequences will need to load
9577 the address of the branch target from the GOT. We don't know until
9578 the very end of compilation whether (and where) the function needs
9579 long branches, so we must ensure that _any_ branch can access the
9580 global pointer in some form. However, we do not want to pessimize
9581 the usual case in which all branches are short.
9582
9583 We handle this as follows:
9584
9585 (1) During reload, we set cfun->machine->global_pointer to
9586 INVALID_REGNUM if we _know_ that the current function
9587 doesn't need a global pointer. This is only valid if
9588 long branches don't need the GOT.
9589
9590 Otherwise, we assume that we might need a global pointer
9591 and pick an appropriate register.
9592
9593 (2) If cfun->machine->global_pointer != INVALID_REGNUM,
9594 we ensure that the global pointer is available at every
9595 block boundary bar entry and exit. We do this in one of two ways:
9596
9597 - If the function has a cprestore slot, we ensure that this
9598 slot is valid at every branch. However, as explained in
9599 point (6) below, there is no guarantee that pic_offset_table_rtx
9600 itself is valid if new uses of the global pointer are introduced
9601 after the first post-epilogue split.
9602
9603 We guarantee that the cprestore slot is valid by loading it
9604 into a fake register, CPRESTORE_SLOT_REGNUM. We then make
9605 this register live at every block boundary bar function entry
9606 and exit. It is then invalid to move the load (and thus the
9607 preceding store) across a block boundary.
9608
9609 - If the function has no cprestore slot, we guarantee that
9610 pic_offset_table_rtx itself is valid at every branch.
9611
9612 See mips_eh_uses for the handling of the register liveness.
9613
9614 (3) During prologue and epilogue generation, we emit "ghost"
9615 placeholder instructions to manipulate the global pointer.
9616
9617 (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
9618 and cfun->machine->must_restore_gp_when_clobbered_p if we already know
9619 that the function needs a global pointer. (There is no need to set
9620 them earlier than this, and doing it as late as possible leads to
9621 fewer false positives.)
9622
9623 (5) If cfun->machine->must_initialize_gp_p is true during a
9624 split_insns pass, we split the ghost instructions into real
9625 instructions. These split instructions can then be optimized in
9626 the usual way. Otherwise, we keep the ghost instructions intact,
9627 and optimize for the case where they aren't needed. We still
9628 have the option of splitting them later, if we need to introduce
9629 new uses of the global pointer.
9630
9631 For example, the scheduler ignores a ghost instruction that
9632 stores $28 to the stack, but it handles the split form of
9633 the ghost instruction as an ordinary store.
9634
9635 (6) [OldABI only.] If cfun->machine->must_restore_gp_when_clobbered_p
9636 is true during the first post-epilogue split_insns pass, we split
9637 calls and restore_gp patterns into instructions that explicitly
9638 load pic_offset_table_rtx from the cprestore slot. Otherwise,
9639 we split these patterns into instructions that _don't_ load from
9640 the cprestore slot.
9641
9642 If cfun->machine->must_restore_gp_when_clobbered_p is true at the
9643 time of the split, then any instructions that exist at that time
9644 can make free use of pic_offset_table_rtx. However, if we want
9645 to introduce new uses of the global pointer after the split,
9646 we must explicitly load the value from the cprestore slot, since
9647 pic_offset_table_rtx itself might not be valid at a given point
9648 in the function.
9649
9650 The idea is that we want to be able to delete redundant
9651 loads from the cprestore slot in the usual case where no
9652 long branches are needed.
9653
9654 (7) If cfun->machine->must_initialize_gp_p is still false at the end
9655 of md_reorg, we decide whether the global pointer is needed for
9656 long branches. If so, we set cfun->machine->must_initialize_gp_p
9657 to true and split the ghost instructions into real instructions
9658 at that stage.
9659
9660 Note that the ghost instructions must have a zero length for three reasons:
9661
9662 - Giving the length of the underlying $gp sequence might cause
9663 us to use long branches in cases where they aren't really needed.
9664
9665 - They would perturb things like alignment calculations.
9666
9667 - More importantly, the hazard detection in md_reorg relies on
9668 empty instructions having a zero length.
9669
9670 If we find a long branch and split the ghost instructions at the
9671 end of md_reorg, the split could introduce more long branches.
9672 That isn't a problem though, because we still do the split before
9673 the final shorten_branches pass.
9674
9675 This is extremely ugly, but it seems like the best compromise between
9676 correctness and efficiency. */
9677
9678 bool
9679 mips_must_initialize_gp_p (void)
9680 {
9681 return cfun->machine->must_initialize_gp_p;
9682 }
9683
9684 /* Return true if REGNO is a register that is ordinarily call-clobbered
9685 but must nevertheless be preserved by an interrupt handler. */
9686
9687 static bool
9688 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
9689 {
9690 if (MD_REG_P (regno))
9691 return true;
9692
9693 if (TARGET_DSP && DSP_ACC_REG_P (regno))
9694 return true;
9695
9696 if (GP_REG_P (regno) && !cfun->machine->use_shadow_register_set_p)
9697 {
9698 /* $0 is hard-wired. */
9699 if (regno == GP_REG_FIRST)
9700 return false;
9701
9702 /* The interrupt handler can treat kernel registers as
9703 scratch registers. */
9704 if (KERNEL_REG_P (regno))
9705 return false;
9706
9707 /* The function will return the stack pointer to its original value
9708 anyway. */
9709 if (regno == STACK_POINTER_REGNUM)
9710 return false;
9711
9712 /* Otherwise, return true for registers that aren't ordinarily
9713 call-clobbered. */
9714 return call_really_used_regs[regno];
9715 }
9716
9717 return false;
9718 }
9719
9720 /* Return true if the current function should treat register REGNO
9721 as call-saved. */
9722
9723 static bool
9724 mips_cfun_call_saved_reg_p (unsigned int regno)
9725 {
9726 /* If the user makes an ordinarily-call-saved register global,
9727 that register is no longer call-saved. */
9728 if (global_regs[regno])
9729 return false;
9730
9731 /* Interrupt handlers need to save extra registers. */
9732 if (cfun->machine->interrupt_handler_p
9733 && mips_interrupt_extra_call_saved_reg_p (regno))
9734 return true;
9735
9736 /* call_insns preserve $28 unless they explicitly say otherwise,
9737 so call_really_used_regs[] treats $28 as call-saved. However,
9738 we want the ABI property rather than the default call_insn
9739 property here. */
9740 return (regno == GLOBAL_POINTER_REGNUM
9741 ? TARGET_CALL_SAVED_GP
9742 : !call_really_used_regs[regno]);
9743 }
9744
9745 /* Return true if the function body might clobber register REGNO.
9746 We know that REGNO is call-saved. */
9747
9748 static bool
9749 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
9750 {
9751 /* Some functions should be treated as clobbering all call-saved
9752 registers. */
9753 if (crtl->saves_all_registers)
9754 return true;
9755
9756 /* DF handles cases where a register is explicitly referenced in
9757 the rtl. Incoming values are passed in call-clobbered registers,
9758 so we can assume that any live call-saved register is set within
9759 the function. */
9760 if (df_regs_ever_live_p (regno))
9761 return true;
9762
9763 /* Check for registers that are clobbered by FUNCTION_PROFILER.
9764 These clobbers are not explicit in the rtl. */
9765 if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
9766 return true;
9767
9768 /* If we're using a call-saved global pointer, the function's
9769 prologue will need to set it up. */
9770 if (cfun->machine->global_pointer == regno)
9771 return true;
9772
9773 /* The function's prologue will need to set the frame pointer if
9774 frame_pointer_needed. */
9775 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
9776 return true;
9777
9778 /* If a MIPS16 function returns a value in FPRs, its epilogue
9779 will need to call an external libgcc routine. This yet-to-be
9780 generated call_insn will clobber $31. */
9781 if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
9782 return true;
9783
9784 /* If REGNO is ordinarily call-clobbered, we must assume that any
9785 called function could modify it. */
9786 if (cfun->machine->interrupt_handler_p
9787 && !crtl->is_leaf
9788 && mips_interrupt_extra_call_saved_reg_p (regno))
9789 return true;
9790
9791 return false;
9792 }
9793
9794 /* Return true if the current function must save register REGNO. */
9795
9796 static bool
9797 mips_save_reg_p (unsigned int regno)
9798 {
9799 if (mips_cfun_call_saved_reg_p (regno))
9800 {
9801 if (mips_cfun_might_clobber_call_saved_reg_p (regno))
9802 return true;
9803
9804 /* Save both registers in an FPR pair if either one is used. This is
9805 needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
9806 register to be used without the even register. */
9807 if (FP_REG_P (regno)
9808 && MAX_FPRS_PER_FMT == 2
9809 && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
9810 return true;
9811 }
9812
9813 /* We need to save the incoming return address if __builtin_eh_return
9814 is being used to set a different return address. */
9815 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
9816 return true;
9817
9818 return false;
9819 }
9820
9821 /* Populate the current function's mips_frame_info structure.
9822
9823 MIPS stack frames look like:
9824
9825 +-------------------------------+
9826 | |
9827 | incoming stack arguments |
9828 | |
9829 +-------------------------------+
9830 | |
9831 | caller-allocated save area |
9832 A | for register arguments |
9833 | |
9834 +-------------------------------+ <-- incoming stack pointer
9835 | |
9836 | callee-allocated save area |
9837 B | for arguments that are |
9838 | split between registers and |
9839 | the stack |
9840 | |
9841 +-------------------------------+ <-- arg_pointer_rtx
9842 | |
9843 C | callee-allocated save area |
9844 | for register varargs |
9845 | |
9846 +-------------------------------+ <-- frame_pointer_rtx
9847 | | + cop0_sp_offset
9848 | COP0 reg save area | + UNITS_PER_WORD
9849 | |
9850 +-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
9851 | | + UNITS_PER_WORD
9852 | accumulator save area |
9853 | |
9854 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
9855 | | + UNITS_PER_HWFPVALUE
9856 | FPR save area |
9857 | |
9858 +-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
9859 | | + UNITS_PER_WORD
9860 | GPR save area |
9861 | |
9862 +-------------------------------+ <-- frame_pointer_rtx with
9863 | | \ -fstack-protector
9864 | local variables | | var_size
9865 | | /
9866 +-------------------------------+
9867 | | \
9868 | $gp save area | | cprestore_size
9869 | | /
9870 P +-------------------------------+ <-- hard_frame_pointer_rtx for
9871 | | \ MIPS16 code
9872 | outgoing stack arguments | |
9873 | | |
9874 +-------------------------------+ | args_size
9875 | | |
9876 | caller-allocated save area | |
9877 | for register arguments | |
9878 | | /
9879 +-------------------------------+ <-- stack_pointer_rtx
9880 frame_pointer_rtx without
9881 -fstack-protector
9882 hard_frame_pointer_rtx for
9883 non-MIPS16 code.
9884
9885 At least two of A, B and C will be empty.
9886
9887 Dynamic stack allocations such as alloca insert data at point P.
9888 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
9889 hard_frame_pointer_rtx unchanged. */
9890
9891 static void
9892 mips_compute_frame_info (void)
9893 {
9894 struct mips_frame_info *frame;
9895 HOST_WIDE_INT offset, size;
9896 unsigned int regno, i;
9897
9898 /* Set this function's interrupt properties. */
9899 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
9900 {
9901 if (!ISA_MIPS32R2)
9902 error ("the %<interrupt%> attribute requires a MIPS32r2 processor");
9903 else if (TARGET_HARD_FLOAT)
9904 error ("the %<interrupt%> attribute requires %<-msoft-float%>");
9905 else if (TARGET_MIPS16)
9906 error ("interrupt handlers cannot be MIPS16 functions");
9907 else
9908 {
9909 cfun->machine->interrupt_handler_p = true;
9910 cfun->machine->use_shadow_register_set_p =
9911 mips_use_shadow_register_set_p (TREE_TYPE (current_function_decl));
9912 cfun->machine->keep_interrupts_masked_p =
9913 mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
9914 cfun->machine->use_debug_exception_return_p =
9915 mips_use_debug_exception_return_p (TREE_TYPE
9916 (current_function_decl));
9917 }
9918 }
9919
9920 frame = &cfun->machine->frame;
9921 memset (frame, 0, sizeof (*frame));
9922 size = get_frame_size ();
9923
9924 cfun->machine->global_pointer = mips_global_pointer ();
9925
9926 /* The first two blocks contain the outgoing argument area and the $gp save
9927 slot. This area isn't needed in leaf functions, but if the
9928 target-independent frame size is nonzero, we have already committed to
9929 allocating these in STARTING_FRAME_OFFSET for !FRAME_GROWS_DOWNWARD. */
9930 if ((size == 0 || FRAME_GROWS_DOWNWARD) && crtl->is_leaf)
9931 {
9932 /* The MIPS 3.0 linker does not like functions that dynamically
9933 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
9934 looks like we are trying to create a second frame pointer to the
9935 function, so allocate some stack space to make it happy. */
9936 if (cfun->calls_alloca)
9937 frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
9938 else
9939 frame->args_size = 0;
9940 frame->cprestore_size = 0;
9941 }
9942 else
9943 {
9944 frame->args_size = crtl->outgoing_args_size;
9945 frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
9946 }
9947 offset = frame->args_size + frame->cprestore_size;
9948
9949 /* Move above the local variables. */
9950 frame->var_size = MIPS_STACK_ALIGN (size);
9951 offset += frame->var_size;
9952
9953 /* Find out which GPRs we need to save. */
9954 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9955 if (mips_save_reg_p (regno))
9956 {
9957 frame->num_gp++;
9958 frame->mask |= 1 << (regno - GP_REG_FIRST);
9959 }
9960
9961 /* If this function calls eh_return, we must also save and restore the
9962 EH data registers. */
9963 if (crtl->calls_eh_return)
9964 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
9965 {
9966 frame->num_gp++;
9967 frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
9968 }
9969
9970 /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
9971 $a3-$a0 and $s2-$s8. If we save one register in the range, we must
9972 save all later registers too. */
9973 if (GENERATE_MIPS16E_SAVE_RESTORE)
9974 {
9975 mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
9976 ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
9977 mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
9978 ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
9979 }
9980
9981 /* Move above the GPR save area. */
9982 if (frame->num_gp > 0)
9983 {
9984 offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
9985 frame->gp_sp_offset = offset - UNITS_PER_WORD;
9986 }
9987
9988 /* Find out which FPRs we need to save. This loop must iterate over
9989 the same space as its companion in mips_for_each_saved_gpr_and_fpr. */
9990 if (TARGET_HARD_FLOAT)
9991 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
9992 if (mips_save_reg_p (regno))
9993 {
9994 frame->num_fp += MAX_FPRS_PER_FMT;
9995 frame->fmask |= ~(~0 << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
9996 }
9997
9998 /* Move above the FPR save area. */
9999 if (frame->num_fp > 0)
10000 {
10001 offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
10002 frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
10003 }
10004
10005 /* Add in space for the interrupt context information. */
10006 if (cfun->machine->interrupt_handler_p)
10007 {
10008 /* Check HI/LO. */
10009 if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
10010 {
10011 frame->num_acc++;
10012 frame->acc_mask |= (1 << 0);
10013 }
10014
10015 /* Check accumulators 1, 2, 3. */
10016 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
10017 if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
10018 {
10019 frame->num_acc++;
10020 frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
10021 }
10022
10023 /* All interrupt context functions need space to preserve STATUS. */
10024 frame->num_cop0_regs++;
10025
10026 /* If we don't keep interrupts masked, we need to save EPC. */
10027 if (!cfun->machine->keep_interrupts_masked_p)
10028 frame->num_cop0_regs++;
10029 }
10030
10031 /* Move above the accumulator save area. */
10032 if (frame->num_acc > 0)
10033 {
10034 /* Each accumulator needs 2 words. */
10035 offset += frame->num_acc * 2 * UNITS_PER_WORD;
10036 frame->acc_sp_offset = offset - UNITS_PER_WORD;
10037 }
10038
10039 /* Move above the COP0 register save area. */
10040 if (frame->num_cop0_regs > 0)
10041 {
10042 offset += frame->num_cop0_regs * UNITS_PER_WORD;
10043 frame->cop0_sp_offset = offset - UNITS_PER_WORD;
10044 }
10045
10046 /* Move above the callee-allocated varargs save area. */
10047 offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
10048 frame->arg_pointer_offset = offset;
10049
10050 /* Move above the callee-allocated area for pretend stack arguments. */
10051 offset += crtl->args.pretend_args_size;
10052 frame->total_size = offset;
10053
10054 /* Work out the offsets of the save areas from the top of the frame. */
10055 if (frame->gp_sp_offset > 0)
10056 frame->gp_save_offset = frame->gp_sp_offset - offset;
10057 if (frame->fp_sp_offset > 0)
10058 frame->fp_save_offset = frame->fp_sp_offset - offset;
10059 if (frame->acc_sp_offset > 0)
10060 frame->acc_save_offset = frame->acc_sp_offset - offset;
10061 if (frame->num_cop0_regs > 0)
10062 frame->cop0_save_offset = frame->cop0_sp_offset - offset;
10063
10064 /* MIPS16 code offsets the frame pointer by the size of the outgoing
10065 arguments. This tends to increase the chances of using unextended
10066 instructions for local variables and incoming arguments. */
10067 if (TARGET_MIPS16)
10068 frame->hard_frame_pointer_offset = frame->args_size;
10069 }
10070
10071 /* Return the style of GP load sequence that is being used for the
10072 current function. */
10073
10074 enum mips_loadgp_style
10075 mips_current_loadgp_style (void)
10076 {
10077 if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
10078 return LOADGP_NONE;
10079
10080 if (TARGET_RTP_PIC)
10081 return LOADGP_RTP;
10082
10083 if (TARGET_ABSOLUTE_ABICALLS)
10084 return LOADGP_ABSOLUTE;
10085
10086 return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
10087 }
10088
10089 /* Implement TARGET_FRAME_POINTER_REQUIRED. */
10090
10091 static bool
10092 mips_frame_pointer_required (void)
10093 {
10094 /* If the function contains dynamic stack allocations, we need to
10095 use the frame pointer to access the static parts of the frame. */
10096 if (cfun->calls_alloca)
10097 return true;
10098
10099 /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
10100 reload may be unable to compute the address of a local variable,
10101 since there is no way to add a large constant to the stack pointer
10102 without using a second temporary register. */
10103 if (TARGET_MIPS16)
10104 {
10105 mips_compute_frame_info ();
10106 if (!SMALL_OPERAND (cfun->machine->frame.total_size))
10107 return true;
10108 }
10109
10110 return false;
10111 }
10112
10113 /* Make sure that we're not trying to eliminate to the wrong hard frame
10114 pointer. */
10115
10116 static bool
10117 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
10118 {
10119 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
10120 }
10121
10122 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
10123 or argument pointer. TO is either the stack pointer or hard frame
10124 pointer. */
10125
10126 HOST_WIDE_INT
10127 mips_initial_elimination_offset (int from, int to)
10128 {
10129 HOST_WIDE_INT offset;
10130
10131 mips_compute_frame_info ();
10132
10133 /* Set OFFSET to the offset from the end-of-prologue stack pointer. */
10134 switch (from)
10135 {
10136 case FRAME_POINTER_REGNUM:
10137 if (FRAME_GROWS_DOWNWARD)
10138 offset = (cfun->machine->frame.args_size
10139 + cfun->machine->frame.cprestore_size
10140 + cfun->machine->frame.var_size);
10141 else
10142 offset = 0;
10143 break;
10144
10145 case ARG_POINTER_REGNUM:
10146 offset = cfun->machine->frame.arg_pointer_offset;
10147 break;
10148
10149 default:
10150 gcc_unreachable ();
10151 }
10152
10153 if (to == HARD_FRAME_POINTER_REGNUM)
10154 offset -= cfun->machine->frame.hard_frame_pointer_offset;
10155
10156 return offset;
10157 }
10158 \f
10159 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY. */
10160
10161 static void
10162 mips_extra_live_on_entry (bitmap regs)
10163 {
10164 if (TARGET_USE_GOT)
10165 {
10166 /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
10167 the global pointer. */
10168 if (!TARGET_ABSOLUTE_ABICALLS)
10169 bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
10170
10171 /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
10172 the global pointer. */
10173 if (TARGET_MIPS16)
10174 bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
10175
10176 /* See the comment above load_call<mode> for details. */
10177 bitmap_set_bit (regs, GOT_VERSION_REGNUM);
10178 }
10179 }
10180
10181 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
10182 previous frame. */
10183
10184 rtx
10185 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
10186 {
10187 if (count != 0)
10188 return const0_rtx;
10189
10190 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
10191 }
10192
10193 /* Emit code to change the current function's return address to
10194 ADDRESS. SCRATCH is available as a scratch register, if needed.
10195 ADDRESS and SCRATCH are both word-mode GPRs. */
10196
10197 void
10198 mips_set_return_address (rtx address, rtx scratch)
10199 {
10200 rtx slot_address;
10201
10202 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
10203 slot_address = mips_add_offset (scratch, stack_pointer_rtx,
10204 cfun->machine->frame.gp_sp_offset);
10205 mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
10206 }
10207
10208 /* Return true if the current function has a cprestore slot. */
10209
10210 bool
10211 mips_cfun_has_cprestore_slot_p (void)
10212 {
10213 return (cfun->machine->global_pointer != INVALID_REGNUM
10214 && cfun->machine->frame.cprestore_size > 0);
10215 }
10216
10217 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
10218 cprestore slot. LOAD_P is true if the caller wants to load from
10219 the cprestore slot; it is false if the caller wants to store to
10220 the slot. */
10221
10222 static void
10223 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
10224 bool load_p)
10225 {
10226 const struct mips_frame_info *frame;
10227
10228 frame = &cfun->machine->frame;
10229 /* .cprestore always uses the stack pointer instead of the frame pointer.
10230 We have a free choice for direct stores for non-MIPS16 functions,
10231 and for MIPS16 functions whose cprestore slot is in range of the
10232 stack pointer. Using the stack pointer would sometimes give more
10233 (early) scheduling freedom, but using the frame pointer would
10234 sometimes give more (late) scheduling freedom. It's hard to
10235 predict which applies to a given function, so let's keep things
10236 simple.
10237
10238 Loads must always use the frame pointer in functions that call
10239 alloca, and there's little benefit to using the stack pointer
10240 otherwise. */
10241 if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
10242 {
10243 *base = hard_frame_pointer_rtx;
10244 *offset = frame->args_size - frame->hard_frame_pointer_offset;
10245 }
10246 else
10247 {
10248 *base = stack_pointer_rtx;
10249 *offset = frame->args_size;
10250 }
10251 }
10252
10253 /* Return true if X is the load or store address of the cprestore slot;
10254 LOAD_P says which. */
10255
10256 bool
10257 mips_cprestore_address_p (rtx x, bool load_p)
10258 {
10259 rtx given_base, required_base;
10260 HOST_WIDE_INT given_offset, required_offset;
10261
10262 mips_split_plus (x, &given_base, &given_offset);
10263 mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
10264 return given_base == required_base && given_offset == required_offset;
10265 }
10266
10267 /* Return a MEM rtx for the cprestore slot. LOAD_P is true if we are
10268 going to load from it, false if we are going to store to it.
10269 Use TEMP as a temporary register if need be. */
10270
10271 static rtx
10272 mips_cprestore_slot (rtx temp, bool load_p)
10273 {
10274 rtx base;
10275 HOST_WIDE_INT offset;
10276
10277 mips_get_cprestore_base_and_offset (&base, &offset, load_p);
10278 return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
10279 }
10280
10281 /* Emit instructions to save global pointer value GP into cprestore
10282 slot MEM. OFFSET is the offset that MEM applies to the base register.
10283
10284 MEM may not be a legitimate address. If it isn't, TEMP is a
10285 temporary register that can be used, otherwise it is a SCRATCH. */
10286
10287 void
10288 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
10289 {
10290 if (TARGET_CPRESTORE_DIRECTIVE)
10291 {
10292 gcc_assert (gp == pic_offset_table_rtx);
10293 emit_insn (PMODE_INSN (gen_cprestore, (mem, offset)));
10294 }
10295 else
10296 mips_emit_move (mips_cprestore_slot (temp, false), gp);
10297 }
10298
10299 /* Restore $gp from its save slot, using TEMP as a temporary base register
10300 if need be. This function is for o32 and o64 abicalls only.
10301
10302 See mips_must_initialize_gp_p for details about how we manage the
10303 global pointer. */
10304
10305 void
10306 mips_restore_gp_from_cprestore_slot (rtx temp)
10307 {
10308 gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
10309
10310 if (!cfun->machine->must_restore_gp_when_clobbered_p)
10311 {
10312 emit_note (NOTE_INSN_DELETED);
10313 return;
10314 }
10315
10316 if (TARGET_MIPS16)
10317 {
10318 mips_emit_move (temp, mips_cprestore_slot (temp, true));
10319 mips_emit_move (pic_offset_table_rtx, temp);
10320 }
10321 else
10322 mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
10323 if (!TARGET_EXPLICIT_RELOCS)
10324 emit_insn (gen_blockage ());
10325 }
10326 \f
10327 /* A function to save or store a register. The first argument is the
10328 register and the second is the stack slot. */
10329 typedef void (*mips_save_restore_fn) (rtx, rtx);
10330
10331 /* Use FN to save or restore register REGNO. MODE is the register's
10332 mode and OFFSET is the offset of its save slot from the current
10333 stack pointer. */
10334
10335 static void
10336 mips_save_restore_reg (enum machine_mode mode, int regno,
10337 HOST_WIDE_INT offset, mips_save_restore_fn fn)
10338 {
10339 rtx mem;
10340
10341 mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx,
10342 offset));
10343 fn (gen_rtx_REG (mode, regno), mem);
10344 }
10345
10346 /* Call FN for each accumlator that is saved by the current function.
10347 SP_OFFSET is the offset of the current stack pointer from the start
10348 of the frame. */
10349
10350 static void
10351 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
10352 {
10353 HOST_WIDE_INT offset;
10354 int regno;
10355
10356 offset = cfun->machine->frame.acc_sp_offset - sp_offset;
10357 if (BITSET_P (cfun->machine->frame.acc_mask, 0))
10358 {
10359 mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
10360 offset -= UNITS_PER_WORD;
10361 mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
10362 offset -= UNITS_PER_WORD;
10363 }
10364
10365 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
10366 if (BITSET_P (cfun->machine->frame.acc_mask,
10367 ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
10368 {
10369 mips_save_restore_reg (word_mode, regno, offset, fn);
10370 offset -= UNITS_PER_WORD;
10371 }
10372 }
10373
10374 /* Save register REG to MEM. Make the instruction frame-related. */
10375
10376 static void
10377 mips_save_reg (rtx reg, rtx mem)
10378 {
10379 if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
10380 {
10381 rtx x1, x2;
10382
10383 mips_emit_move_or_split (mem, reg, SPLIT_IF_NECESSARY);
10384
10385 x1 = mips_frame_set (mips_subword (mem, false),
10386 mips_subword (reg, false));
10387 x2 = mips_frame_set (mips_subword (mem, true),
10388 mips_subword (reg, true));
10389 mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
10390 }
10391 else
10392 mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
10393 }
10394
10395 /* Capture the register combinations that are allowed in a SWM or LWM
10396 instruction. The entries are ordered by number of registers set in
10397 the mask. We also ignore the single register encodings because a
10398 normal SW/LW is preferred. */
10399
10400 static const unsigned int umips_swm_mask[17] = {
10401 0xc0ff0000, 0x80ff0000, 0x40ff0000, 0x807f0000,
10402 0x00ff0000, 0x803f0000, 0x007f0000, 0x801f0000,
10403 0x003f0000, 0x800f0000, 0x001f0000, 0x80070000,
10404 0x000f0000, 0x80030000, 0x00070000, 0x80010000,
10405 0x00030000
10406 };
10407
10408 static const unsigned int umips_swm_encoding[17] = {
10409 25, 24, 9, 23, 8, 22, 7, 21, 6, 20, 5, 19, 4, 18, 3, 17, 2
10410 };
10411
10412 /* Try to use a microMIPS LWM or SWM instruction to save or restore
10413 as many GPRs in *MASK as possible. *OFFSET is the offset from the
10414 stack pointer of the topmost save slot.
10415
10416 Remove from *MASK all registers that were handled using LWM and SWM.
10417 Update *OFFSET so that it points to the first unused save slot. */
10418
10419 static bool
10420 umips_build_save_restore (mips_save_restore_fn fn,
10421 unsigned *mask, HOST_WIDE_INT *offset)
10422 {
10423 int nregs;
10424 unsigned int i, j;
10425 rtx pattern, set, reg, mem;
10426 HOST_WIDE_INT this_offset;
10427 rtx this_base;
10428
10429 /* Try matching $16 to $31 (s0 to ra). */
10430 for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
10431 if ((*mask & 0xffff0000) == umips_swm_mask[i])
10432 break;
10433
10434 if (i == ARRAY_SIZE (umips_swm_mask))
10435 return false;
10436
10437 /* Get the offset of the lowest save slot. */
10438 nregs = (umips_swm_encoding[i] & 0xf) + (umips_swm_encoding[i] >> 4);
10439 this_offset = *offset - UNITS_PER_WORD * (nregs - 1);
10440
10441 /* LWM/SWM can only support offsets from -2048 to 2047. */
10442 if (!UMIPS_12BIT_OFFSET_P (this_offset))
10443 return false;
10444
10445 /* Create the final PARALLEL. */
10446 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
10447 this_base = stack_pointer_rtx;
10448
10449 /* For registers $16-$23 and $30. */
10450 for (j = 0; j < (umips_swm_encoding[i] & 0xf); j++)
10451 {
10452 HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10453 mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10454 unsigned int regno = (j != 8) ? 16 + j : 30;
10455 *mask &= ~(1 << regno);
10456 reg = gen_rtx_REG (SImode, regno);
10457 if (fn == mips_save_reg)
10458 set = mips_frame_set (mem, reg);
10459 else
10460 {
10461 set = gen_rtx_SET (VOIDmode, reg, mem);
10462 mips_add_cfa_restore (reg);
10463 }
10464 XVECEXP (pattern, 0, j) = set;
10465 }
10466
10467 /* For register $31. */
10468 if (umips_swm_encoding[i] >> 4)
10469 {
10470 HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10471 *mask &= ~(1 << 31);
10472 mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10473 reg = gen_rtx_REG (SImode, 31);
10474 if (fn == mips_save_reg)
10475 set = mips_frame_set (mem, reg);
10476 else
10477 {
10478 set = gen_rtx_SET (VOIDmode, reg, mem);
10479 mips_add_cfa_restore (reg);
10480 }
10481 XVECEXP (pattern, 0, j) = set;
10482 }
10483
10484 pattern = emit_insn (pattern);
10485 if (fn == mips_save_reg)
10486 RTX_FRAME_RELATED_P (pattern) = 1;
10487
10488 /* Adjust the last offset. */
10489 *offset -= UNITS_PER_WORD * nregs;
10490
10491 return true;
10492 }
10493
10494 /* Call FN for each register that is saved by the current function.
10495 SP_OFFSET is the offset of the current stack pointer from the start
10496 of the frame. */
10497
10498 static void
10499 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
10500 mips_save_restore_fn fn)
10501 {
10502 enum machine_mode fpr_mode;
10503 int regno;
10504 const struct mips_frame_info *frame = &cfun->machine->frame;
10505 HOST_WIDE_INT offset;
10506 unsigned int mask;
10507
10508 /* Save registers starting from high to low. The debuggers prefer at least
10509 the return register be stored at func+4, and also it allows us not to
10510 need a nop in the epilogue if at least one register is reloaded in
10511 addition to return address. */
10512 offset = frame->gp_sp_offset - sp_offset;
10513 mask = frame->mask;
10514
10515 if (TARGET_MICROMIPS)
10516 umips_build_save_restore (fn, &mask, &offset);
10517
10518 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
10519 if (BITSET_P (mask, regno - GP_REG_FIRST))
10520 {
10521 /* Record the ra offset for use by mips_function_profiler. */
10522 if (regno == RETURN_ADDR_REGNUM)
10523 cfun->machine->frame.ra_fp_offset = offset + sp_offset;
10524 mips_save_restore_reg (word_mode, regno, offset, fn);
10525 offset -= UNITS_PER_WORD;
10526 }
10527
10528 /* This loop must iterate over the same space as its companion in
10529 mips_compute_frame_info. */
10530 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
10531 fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
10532 for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
10533 regno >= FP_REG_FIRST;
10534 regno -= MAX_FPRS_PER_FMT)
10535 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
10536 {
10537 mips_save_restore_reg (fpr_mode, regno, offset, fn);
10538 offset -= GET_MODE_SIZE (fpr_mode);
10539 }
10540 }
10541
10542 /* Return true if a move between register REGNO and its save slot (MEM)
10543 can be done in a single move. LOAD_P is true if we are loading
10544 from the slot, false if we are storing to it. */
10545
10546 static bool
10547 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
10548 {
10549 /* There is a specific MIPS16 instruction for saving $31 to the stack. */
10550 if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
10551 return false;
10552
10553 return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
10554 GET_MODE (mem), mem, load_p) == NO_REGS;
10555 }
10556
10557 /* Emit a move from SRC to DEST, given that one of them is a register
10558 save slot and that the other is a register. TEMP is a temporary
10559 GPR of the same mode that is available if need be. */
10560
10561 void
10562 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
10563 {
10564 unsigned int regno;
10565 rtx mem;
10566
10567 if (REG_P (src))
10568 {
10569 regno = REGNO (src);
10570 mem = dest;
10571 }
10572 else
10573 {
10574 regno = REGNO (dest);
10575 mem = src;
10576 }
10577
10578 if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
10579 {
10580 /* We don't yet know whether we'll need this instruction or not.
10581 Postpone the decision by emitting a ghost move. This move
10582 is specifically not frame-related; only the split version is. */
10583 if (TARGET_64BIT)
10584 emit_insn (gen_move_gpdi (dest, src));
10585 else
10586 emit_insn (gen_move_gpsi (dest, src));
10587 return;
10588 }
10589
10590 if (regno == HI_REGNUM)
10591 {
10592 if (REG_P (dest))
10593 {
10594 mips_emit_move (temp, src);
10595 if (TARGET_64BIT)
10596 emit_insn (gen_mthisi_di (gen_rtx_REG (TImode, MD_REG_FIRST),
10597 temp, gen_rtx_REG (DImode, LO_REGNUM)));
10598 else
10599 emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
10600 temp, gen_rtx_REG (SImode, LO_REGNUM)));
10601 }
10602 else
10603 {
10604 if (TARGET_64BIT)
10605 emit_insn (gen_mfhidi_ti (temp,
10606 gen_rtx_REG (TImode, MD_REG_FIRST)));
10607 else
10608 emit_insn (gen_mfhisi_di (temp,
10609 gen_rtx_REG (DImode, MD_REG_FIRST)));
10610 mips_emit_move (dest, temp);
10611 }
10612 }
10613 else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
10614 mips_emit_move (dest, src);
10615 else
10616 {
10617 gcc_assert (!reg_overlap_mentioned_p (dest, temp));
10618 mips_emit_move (temp, src);
10619 mips_emit_move (dest, temp);
10620 }
10621 if (MEM_P (dest))
10622 mips_set_frame_expr (mips_frame_set (dest, src));
10623 }
10624 \f
10625 /* If we're generating n32 or n64 abicalls, and the current function
10626 does not use $28 as its global pointer, emit a cplocal directive.
10627 Use pic_offset_table_rtx as the argument to the directive. */
10628
10629 static void
10630 mips_output_cplocal (void)
10631 {
10632 if (!TARGET_EXPLICIT_RELOCS
10633 && mips_must_initialize_gp_p ()
10634 && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
10635 output_asm_insn (".cplocal %+", 0);
10636 }
10637
10638 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE. */
10639
10640 static void
10641 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10642 {
10643 const char *fnname;
10644
10645 /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
10646 floating-point arguments. */
10647 if (TARGET_MIPS16
10648 && TARGET_HARD_FLOAT_ABI
10649 && crtl->args.info.fp_code != 0)
10650 mips16_build_function_stub ();
10651
10652 /* Get the function name the same way that toplev.c does before calling
10653 assemble_start_function. This is needed so that the name used here
10654 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
10655 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10656 mips_start_function_definition (fnname, TARGET_MIPS16);
10657
10658 /* Output MIPS-specific frame information. */
10659 if (!flag_inhibit_size_directive)
10660 {
10661 const struct mips_frame_info *frame;
10662
10663 frame = &cfun->machine->frame;
10664
10665 /* .frame FRAMEREG, FRAMESIZE, RETREG. */
10666 fprintf (file,
10667 "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
10668 "# vars= " HOST_WIDE_INT_PRINT_DEC
10669 ", regs= %d/%d"
10670 ", args= " HOST_WIDE_INT_PRINT_DEC
10671 ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
10672 reg_names[frame_pointer_needed
10673 ? HARD_FRAME_POINTER_REGNUM
10674 : STACK_POINTER_REGNUM],
10675 (frame_pointer_needed
10676 ? frame->total_size - frame->hard_frame_pointer_offset
10677 : frame->total_size),
10678 reg_names[RETURN_ADDR_REGNUM],
10679 frame->var_size,
10680 frame->num_gp, frame->num_fp,
10681 frame->args_size,
10682 frame->cprestore_size);
10683
10684 /* .mask MASK, OFFSET. */
10685 fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10686 frame->mask, frame->gp_save_offset);
10687
10688 /* .fmask MASK, OFFSET. */
10689 fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10690 frame->fmask, frame->fp_save_offset);
10691 }
10692
10693 /* Handle the initialization of $gp for SVR4 PIC, if applicable.
10694 Also emit the ".set noreorder; .set nomacro" sequence for functions
10695 that need it. */
10696 if (mips_must_initialize_gp_p ()
10697 && mips_current_loadgp_style () == LOADGP_OLDABI)
10698 {
10699 if (TARGET_MIPS16)
10700 {
10701 /* This is a fixed-form sequence. The position of the
10702 first two instructions is important because of the
10703 way _gp_disp is defined. */
10704 output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
10705 output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
10706 output_asm_insn ("sll\t$2,16", 0);
10707 output_asm_insn ("addu\t$2,$3", 0);
10708 }
10709 else
10710 {
10711 /* .cpload must be in a .set noreorder but not a
10712 .set nomacro block. */
10713 mips_push_asm_switch (&mips_noreorder);
10714 output_asm_insn (".cpload\t%^", 0);
10715 if (!cfun->machine->all_noreorder_p)
10716 mips_pop_asm_switch (&mips_noreorder);
10717 else
10718 mips_push_asm_switch (&mips_nomacro);
10719 }
10720 }
10721 else if (cfun->machine->all_noreorder_p)
10722 {
10723 mips_push_asm_switch (&mips_noreorder);
10724 mips_push_asm_switch (&mips_nomacro);
10725 }
10726
10727 /* Tell the assembler which register we're using as the global
10728 pointer. This is needed for thunks, since they can use either
10729 explicit relocs or assembler macros. */
10730 mips_output_cplocal ();
10731 }
10732
10733 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE. */
10734
10735 static void
10736 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10737 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10738 {
10739 const char *fnname;
10740
10741 /* Reinstate the normal $gp. */
10742 SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
10743 mips_output_cplocal ();
10744
10745 if (cfun->machine->all_noreorder_p)
10746 {
10747 mips_pop_asm_switch (&mips_nomacro);
10748 mips_pop_asm_switch (&mips_noreorder);
10749 }
10750
10751 /* Get the function name the same way that toplev.c does before calling
10752 assemble_start_function. This is needed so that the name used here
10753 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
10754 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10755 mips_end_function_definition (fnname);
10756 }
10757 \f
10758 /* Emit an optimisation barrier for accesses to the current frame. */
10759
10760 static void
10761 mips_frame_barrier (void)
10762 {
10763 emit_clobber (gen_frame_mem (BLKmode, stack_pointer_rtx));
10764 }
10765
10766
10767 /* The __gnu_local_gp symbol. */
10768
10769 static GTY(()) rtx mips_gnu_local_gp;
10770
10771 /* If we're generating n32 or n64 abicalls, emit instructions
10772 to set up the global pointer. */
10773
10774 static void
10775 mips_emit_loadgp (void)
10776 {
10777 rtx addr, offset, incoming_address, base, index, pic_reg;
10778
10779 pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
10780 switch (mips_current_loadgp_style ())
10781 {
10782 case LOADGP_ABSOLUTE:
10783 if (mips_gnu_local_gp == NULL)
10784 {
10785 mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
10786 SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
10787 }
10788 emit_insn (PMODE_INSN (gen_loadgp_absolute,
10789 (pic_reg, mips_gnu_local_gp)));
10790 break;
10791
10792 case LOADGP_OLDABI:
10793 /* Added by mips_output_function_prologue. */
10794 break;
10795
10796 case LOADGP_NEWABI:
10797 addr = XEXP (DECL_RTL (current_function_decl), 0);
10798 offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
10799 incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
10800 emit_insn (PMODE_INSN (gen_loadgp_newabi,
10801 (pic_reg, offset, incoming_address)));
10802 break;
10803
10804 case LOADGP_RTP:
10805 base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
10806 index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
10807 emit_insn (PMODE_INSN (gen_loadgp_rtp, (pic_reg, base, index)));
10808 break;
10809
10810 default:
10811 return;
10812 }
10813
10814 if (TARGET_MIPS16)
10815 emit_insn (PMODE_INSN (gen_copygp_mips16,
10816 (pic_offset_table_rtx, pic_reg)));
10817
10818 /* Emit a blockage if there are implicit uses of the GP register.
10819 This includes profiled functions, because FUNCTION_PROFILE uses
10820 a jal macro. */
10821 if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
10822 emit_insn (gen_loadgp_blockage ());
10823 }
10824
10825 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
10826
10827 #if PROBE_INTERVAL > 32768
10828 #error Cannot use indexed addressing mode for stack probing
10829 #endif
10830
10831 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
10832 inclusive. These are offsets from the current stack pointer. */
10833
10834 static void
10835 mips_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
10836 {
10837 if (TARGET_MIPS16)
10838 sorry ("-fstack-check=specific not implemented for MIPS16");
10839
10840 /* See if we have a constant small number of probes to generate. If so,
10841 that's the easy case. */
10842 if (first + size <= 32768)
10843 {
10844 HOST_WIDE_INT i;
10845
10846 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
10847 it exceeds SIZE. If only one probe is needed, this will not
10848 generate any code. Then probe at FIRST + SIZE. */
10849 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
10850 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10851 -(first + i)));
10852
10853 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10854 -(first + size)));
10855 }
10856
10857 /* Otherwise, do the same as above, but in a loop. Note that we must be
10858 extra careful with variables wrapping around because we might be at
10859 the very top (or the very bottom) of the address space and we have
10860 to be able to handle this case properly; in particular, we use an
10861 equality test for the loop condition. */
10862 else
10863 {
10864 HOST_WIDE_INT rounded_size;
10865 rtx r3 = MIPS_PROLOGUE_TEMP (Pmode);
10866 rtx r12 = MIPS_PROLOGUE_TEMP2 (Pmode);
10867
10868 /* Sanity check for the addressing mode we're going to use. */
10869 gcc_assert (first <= 32768);
10870
10871
10872 /* Step 1: round SIZE to the previous multiple of the interval. */
10873
10874 rounded_size = size & -PROBE_INTERVAL;
10875
10876
10877 /* Step 2: compute initial and final value of the loop counter. */
10878
10879 /* TEST_ADDR = SP + FIRST. */
10880 emit_insn (gen_rtx_SET (VOIDmode, r3,
10881 plus_constant (Pmode, stack_pointer_rtx,
10882 -first)));
10883
10884 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
10885 if (rounded_size > 32768)
10886 {
10887 emit_move_insn (r12, GEN_INT (rounded_size));
10888 emit_insn (gen_rtx_SET (VOIDmode, r12,
10889 gen_rtx_MINUS (Pmode, r3, r12)));
10890 }
10891 else
10892 emit_insn (gen_rtx_SET (VOIDmode, r12,
10893 plus_constant (Pmode, r3, -rounded_size)));
10894
10895
10896 /* Step 3: the loop
10897
10898 while (TEST_ADDR != LAST_ADDR)
10899 {
10900 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
10901 probe at TEST_ADDR
10902 }
10903
10904 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
10905 until it is equal to ROUNDED_SIZE. */
10906
10907 emit_insn (PMODE_INSN (gen_probe_stack_range, (r3, r3, r12)));
10908
10909
10910 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
10911 that SIZE is equal to ROUNDED_SIZE. */
10912
10913 if (size != rounded_size)
10914 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
10915 }
10916
10917 /* Make sure nothing is scheduled before we are done. */
10918 emit_insn (gen_blockage ());
10919 }
10920
10921 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
10922 absolute addresses. */
10923
10924 const char *
10925 mips_output_probe_stack_range (rtx reg1, rtx reg2)
10926 {
10927 static int labelno = 0;
10928 char loop_lab[32], end_lab[32], tmp[64];
10929 rtx xops[2];
10930
10931 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
10932 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
10933
10934 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
10935
10936 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
10937 xops[0] = reg1;
10938 xops[1] = reg2;
10939 strcpy (tmp, "%(%<beq\t%0,%1,");
10940 output_asm_insn (strcat (tmp, &end_lab[1]), xops);
10941
10942 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
10943 xops[1] = GEN_INT (-PROBE_INTERVAL);
10944 if (TARGET_64BIT && TARGET_LONG64)
10945 output_asm_insn ("daddiu\t%0,%0,%1", xops);
10946 else
10947 output_asm_insn ("addiu\t%0,%0,%1", xops);
10948
10949 /* Probe at TEST_ADDR and branch. */
10950 fprintf (asm_out_file, "\tb\t");
10951 assemble_name_raw (asm_out_file, loop_lab);
10952 fputc ('\n', asm_out_file);
10953 if (TARGET_64BIT)
10954 output_asm_insn ("sd\t$0,0(%0)%)", xops);
10955 else
10956 output_asm_insn ("sw\t$0,0(%0)%)", xops);
10957
10958 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
10959
10960 return "";
10961 }
10962
10963 /* A for_each_rtx callback. Stop the search if *X is a kernel register. */
10964
10965 static int
10966 mips_kernel_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
10967 {
10968 return REG_P (*x) && KERNEL_REG_P (REGNO (*x));
10969 }
10970
10971 /* Expand the "prologue" pattern. */
10972
10973 void
10974 mips_expand_prologue (void)
10975 {
10976 const struct mips_frame_info *frame;
10977 HOST_WIDE_INT size;
10978 unsigned int nargs;
10979 rtx insn;
10980
10981 if (cfun->machine->global_pointer != INVALID_REGNUM)
10982 {
10983 /* Check whether an insn uses pic_offset_table_rtx, either explicitly
10984 or implicitly. If so, we can commit to using a global pointer
10985 straight away, otherwise we need to defer the decision. */
10986 if (mips_cfun_has_inflexible_gp_ref_p ()
10987 || mips_cfun_has_flexible_gp_ref_p ())
10988 {
10989 cfun->machine->must_initialize_gp_p = true;
10990 cfun->machine->must_restore_gp_when_clobbered_p = true;
10991 }
10992
10993 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
10994 }
10995
10996 frame = &cfun->machine->frame;
10997 size = frame->total_size;
10998
10999 if (flag_stack_usage_info)
11000 current_function_static_stack_size = size;
11001
11002 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
11003 {
11004 if (crtl->is_leaf && !cfun->calls_alloca)
11005 {
11006 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
11007 mips_emit_probe_stack_range (STACK_CHECK_PROTECT,
11008 size - STACK_CHECK_PROTECT);
11009 }
11010 else if (size > 0)
11011 mips_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
11012 }
11013
11014 /* Save the registers. Allocate up to MIPS_MAX_FIRST_STACK_STEP
11015 bytes beforehand; this is enough to cover the register save area
11016 without going out of range. */
11017 if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
11018 || frame->num_cop0_regs > 0)
11019 {
11020 HOST_WIDE_INT step1;
11021
11022 step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
11023 if (GENERATE_MIPS16E_SAVE_RESTORE)
11024 {
11025 HOST_WIDE_INT offset;
11026 unsigned int mask, regno;
11027
11028 /* Try to merge argument stores into the save instruction. */
11029 nargs = mips16e_collect_argument_saves ();
11030
11031 /* Build the save instruction. */
11032 mask = frame->mask;
11033 insn = mips16e_build_save_restore (false, &mask, &offset,
11034 nargs, step1);
11035 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11036 mips_frame_barrier ();
11037 size -= step1;
11038
11039 /* Check if we need to save other registers. */
11040 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11041 if (BITSET_P (mask, regno - GP_REG_FIRST))
11042 {
11043 offset -= UNITS_PER_WORD;
11044 mips_save_restore_reg (word_mode, regno,
11045 offset, mips_save_reg);
11046 }
11047 }
11048 else
11049 {
11050 if (cfun->machine->interrupt_handler_p)
11051 {
11052 HOST_WIDE_INT offset;
11053 rtx mem;
11054
11055 /* If this interrupt is using a shadow register set, we need to
11056 get the stack pointer from the previous register set. */
11057 if (cfun->machine->use_shadow_register_set_p)
11058 emit_insn (gen_mips_rdpgpr (stack_pointer_rtx,
11059 stack_pointer_rtx));
11060
11061 if (!cfun->machine->keep_interrupts_masked_p)
11062 {
11063 /* Move from COP0 Cause to K0. */
11064 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
11065 gen_rtx_REG (SImode,
11066 COP0_CAUSE_REG_NUM)));
11067 /* Move from COP0 EPC to K1. */
11068 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11069 gen_rtx_REG (SImode,
11070 COP0_EPC_REG_NUM)));
11071 }
11072
11073 /* Allocate the first part of the frame. */
11074 insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
11075 GEN_INT (-step1));
11076 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11077 mips_frame_barrier ();
11078 size -= step1;
11079
11080 /* Start at the uppermost location for saving. */
11081 offset = frame->cop0_sp_offset - size;
11082 if (!cfun->machine->keep_interrupts_masked_p)
11083 {
11084 /* Push EPC into its stack slot. */
11085 mem = gen_frame_mem (word_mode,
11086 plus_constant (Pmode, stack_pointer_rtx,
11087 offset));
11088 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11089 offset -= UNITS_PER_WORD;
11090 }
11091
11092 /* Move from COP0 Status to K1. */
11093 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11094 gen_rtx_REG (SImode,
11095 COP0_STATUS_REG_NUM)));
11096
11097 /* Right justify the RIPL in k0. */
11098 if (!cfun->machine->keep_interrupts_masked_p)
11099 emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
11100 gen_rtx_REG (SImode, K0_REG_NUM),
11101 GEN_INT (CAUSE_IPL)));
11102
11103 /* Push Status into its stack slot. */
11104 mem = gen_frame_mem (word_mode,
11105 plus_constant (Pmode, stack_pointer_rtx,
11106 offset));
11107 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11108 offset -= UNITS_PER_WORD;
11109
11110 /* Insert the RIPL into our copy of SR (k1) as the new IPL. */
11111 if (!cfun->machine->keep_interrupts_masked_p)
11112 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11113 GEN_INT (6),
11114 GEN_INT (SR_IPL),
11115 gen_rtx_REG (SImode, K0_REG_NUM)));
11116
11117 if (!cfun->machine->keep_interrupts_masked_p)
11118 /* Enable interrupts by clearing the KSU ERL and EXL bits.
11119 IE is already the correct value, so we don't have to do
11120 anything explicit. */
11121 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11122 GEN_INT (4),
11123 GEN_INT (SR_EXL),
11124 gen_rtx_REG (SImode, GP_REG_FIRST)));
11125 else
11126 /* Disable interrupts by clearing the KSU, ERL, EXL,
11127 and IE bits. */
11128 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11129 GEN_INT (5),
11130 GEN_INT (SR_IE),
11131 gen_rtx_REG (SImode, GP_REG_FIRST)));
11132 }
11133 else
11134 {
11135 insn = gen_add3_insn (stack_pointer_rtx,
11136 stack_pointer_rtx,
11137 GEN_INT (-step1));
11138 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11139 mips_frame_barrier ();
11140 size -= step1;
11141 }
11142 mips_for_each_saved_acc (size, mips_save_reg);
11143 mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
11144 }
11145 }
11146
11147 /* Allocate the rest of the frame. */
11148 if (size > 0)
11149 {
11150 if (SMALL_OPERAND (-size))
11151 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
11152 stack_pointer_rtx,
11153 GEN_INT (-size)))) = 1;
11154 else
11155 {
11156 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
11157 if (TARGET_MIPS16)
11158 {
11159 /* There are no instructions to add or subtract registers
11160 from the stack pointer, so use the frame pointer as a
11161 temporary. We should always be using a frame pointer
11162 in this case anyway. */
11163 gcc_assert (frame_pointer_needed);
11164 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11165 emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
11166 hard_frame_pointer_rtx,
11167 MIPS_PROLOGUE_TEMP (Pmode)));
11168 mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
11169 }
11170 else
11171 emit_insn (gen_sub3_insn (stack_pointer_rtx,
11172 stack_pointer_rtx,
11173 MIPS_PROLOGUE_TEMP (Pmode)));
11174
11175 /* Describe the combined effect of the previous instructions. */
11176 mips_set_frame_expr
11177 (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
11178 plus_constant (Pmode, stack_pointer_rtx, -size)));
11179 }
11180 mips_frame_barrier ();
11181 }
11182
11183 /* Set up the frame pointer, if we're using one. */
11184 if (frame_pointer_needed)
11185 {
11186 HOST_WIDE_INT offset;
11187
11188 offset = frame->hard_frame_pointer_offset;
11189 if (offset == 0)
11190 {
11191 insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11192 RTX_FRAME_RELATED_P (insn) = 1;
11193 }
11194 else if (SMALL_OPERAND (offset))
11195 {
11196 insn = gen_add3_insn (hard_frame_pointer_rtx,
11197 stack_pointer_rtx, GEN_INT (offset));
11198 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11199 }
11200 else
11201 {
11202 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
11203 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11204 emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
11205 hard_frame_pointer_rtx,
11206 MIPS_PROLOGUE_TEMP (Pmode)));
11207 mips_set_frame_expr
11208 (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
11209 plus_constant (Pmode, stack_pointer_rtx, offset)));
11210 }
11211 }
11212
11213 mips_emit_loadgp ();
11214
11215 /* Initialize the $gp save slot. */
11216 if (mips_cfun_has_cprestore_slot_p ())
11217 {
11218 rtx base, mem, gp, temp;
11219 HOST_WIDE_INT offset;
11220
11221 mips_get_cprestore_base_and_offset (&base, &offset, false);
11222 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11223 gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
11224 temp = (SMALL_OPERAND (offset)
11225 ? gen_rtx_SCRATCH (Pmode)
11226 : MIPS_PROLOGUE_TEMP (Pmode));
11227 emit_insn (PMODE_INSN (gen_potential_cprestore,
11228 (mem, GEN_INT (offset), gp, temp)));
11229
11230 mips_get_cprestore_base_and_offset (&base, &offset, true);
11231 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11232 emit_insn (PMODE_INSN (gen_use_cprestore, (mem)));
11233 }
11234
11235 /* We need to search back to the last use of K0 or K1. */
11236 if (cfun->machine->interrupt_handler_p)
11237 {
11238 for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
11239 if (INSN_P (insn)
11240 && for_each_rtx (&PATTERN (insn), mips_kernel_reg_p, NULL))
11241 break;
11242 /* Emit a move from K1 to COP0 Status after insn. */
11243 gcc_assert (insn != NULL_RTX);
11244 emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11245 gen_rtx_REG (SImode, K1_REG_NUM)),
11246 insn);
11247 }
11248
11249 /* If we are profiling, make sure no instructions are scheduled before
11250 the call to mcount. */
11251 if (crtl->profile)
11252 emit_insn (gen_blockage ());
11253 }
11254 \f
11255 /* Attach all pending register saves to the previous instruction.
11256 Return that instruction. */
11257
11258 static rtx
11259 mips_epilogue_emit_cfa_restores (void)
11260 {
11261 rtx insn;
11262
11263 insn = get_last_insn ();
11264 gcc_assert (insn && !REG_NOTES (insn));
11265 if (mips_epilogue.cfa_restores)
11266 {
11267 RTX_FRAME_RELATED_P (insn) = 1;
11268 REG_NOTES (insn) = mips_epilogue.cfa_restores;
11269 mips_epilogue.cfa_restores = 0;
11270 }
11271 return insn;
11272 }
11273
11274 /* Like mips_epilogue_emit_cfa_restores, but also record that the CFA is
11275 now at REG + OFFSET. */
11276
11277 static void
11278 mips_epilogue_set_cfa (rtx reg, HOST_WIDE_INT offset)
11279 {
11280 rtx insn;
11281
11282 insn = mips_epilogue_emit_cfa_restores ();
11283 if (reg != mips_epilogue.cfa_reg || offset != mips_epilogue.cfa_offset)
11284 {
11285 RTX_FRAME_RELATED_P (insn) = 1;
11286 REG_NOTES (insn) = alloc_reg_note (REG_CFA_DEF_CFA,
11287 plus_constant (Pmode, reg, offset),
11288 REG_NOTES (insn));
11289 mips_epilogue.cfa_reg = reg;
11290 mips_epilogue.cfa_offset = offset;
11291 }
11292 }
11293
11294 /* Emit instructions to restore register REG from slot MEM. Also update
11295 the cfa_restores list. */
11296
11297 static void
11298 mips_restore_reg (rtx reg, rtx mem)
11299 {
11300 /* There's no MIPS16 instruction to load $31 directly. Load into
11301 $7 instead and adjust the return insn appropriately. */
11302 if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
11303 reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
11304 else if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
11305 {
11306 mips_add_cfa_restore (mips_subword (reg, true));
11307 mips_add_cfa_restore (mips_subword (reg, false));
11308 }
11309 else
11310 mips_add_cfa_restore (reg);
11311
11312 mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
11313 if (REGNO (reg) == REGNO (mips_epilogue.cfa_reg))
11314 /* The CFA is currently defined in terms of the register whose
11315 value we have just restored. Redefine the CFA in terms of
11316 the stack pointer. */
11317 mips_epilogue_set_cfa (stack_pointer_rtx,
11318 mips_epilogue.cfa_restore_sp_offset);
11319 }
11320
11321 /* Emit code to set the stack pointer to BASE + OFFSET, given that
11322 BASE + OFFSET is NEW_FRAME_SIZE bytes below the top of the frame.
11323 BASE, if not the stack pointer, is available as a temporary. */
11324
11325 static void
11326 mips_deallocate_stack (rtx base, rtx offset, HOST_WIDE_INT new_frame_size)
11327 {
11328 if (base == stack_pointer_rtx && offset == const0_rtx)
11329 return;
11330
11331 mips_frame_barrier ();
11332 if (offset == const0_rtx)
11333 {
11334 emit_move_insn (stack_pointer_rtx, base);
11335 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11336 }
11337 else if (TARGET_MIPS16 && base != stack_pointer_rtx)
11338 {
11339 emit_insn (gen_add3_insn (base, base, offset));
11340 mips_epilogue_set_cfa (base, new_frame_size);
11341 emit_move_insn (stack_pointer_rtx, base);
11342 }
11343 else
11344 {
11345 emit_insn (gen_add3_insn (stack_pointer_rtx, base, offset));
11346 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11347 }
11348 }
11349
11350 /* Emit any instructions needed before a return. */
11351
11352 void
11353 mips_expand_before_return (void)
11354 {
11355 /* When using a call-clobbered gp, we start out with unified call
11356 insns that include instructions to restore the gp. We then split
11357 these unified calls after reload. These split calls explicitly
11358 clobber gp, so there is no need to define
11359 PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
11360
11361 For consistency, we should also insert an explicit clobber of $28
11362 before return insns, so that the post-reload optimizers know that
11363 the register is not live on exit. */
11364 if (TARGET_CALL_CLOBBERED_GP)
11365 emit_clobber (pic_offset_table_rtx);
11366 }
11367
11368 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
11369 says which. */
11370
11371 void
11372 mips_expand_epilogue (bool sibcall_p)
11373 {
11374 const struct mips_frame_info *frame;
11375 HOST_WIDE_INT step1, step2;
11376 rtx base, adjust, insn;
11377 bool use_jraddiusp_p = false;
11378
11379 if (!sibcall_p && mips_can_use_return_insn ())
11380 {
11381 emit_jump_insn (gen_return ());
11382 return;
11383 }
11384
11385 /* In MIPS16 mode, if the return value should go into a floating-point
11386 register, we need to call a helper routine to copy it over. */
11387 if (mips16_cfun_returns_in_fpr_p ())
11388 mips16_copy_fpr_return_value ();
11389
11390 /* Split the frame into two. STEP1 is the amount of stack we should
11391 deallocate before restoring the registers. STEP2 is the amount we
11392 should deallocate afterwards.
11393
11394 Start off by assuming that no registers need to be restored. */
11395 frame = &cfun->machine->frame;
11396 step1 = frame->total_size;
11397 step2 = 0;
11398
11399 /* Work out which register holds the frame address. */
11400 if (!frame_pointer_needed)
11401 base = stack_pointer_rtx;
11402 else
11403 {
11404 base = hard_frame_pointer_rtx;
11405 step1 -= frame->hard_frame_pointer_offset;
11406 }
11407 mips_epilogue.cfa_reg = base;
11408 mips_epilogue.cfa_offset = step1;
11409 mips_epilogue.cfa_restores = NULL_RTX;
11410
11411 /* If we need to restore registers, deallocate as much stack as
11412 possible in the second step without going out of range. */
11413 if ((frame->mask | frame->fmask | frame->acc_mask) != 0
11414 || frame->num_cop0_regs > 0)
11415 {
11416 step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
11417 step1 -= step2;
11418 }
11419
11420 /* Get an rtx for STEP1 that we can add to BASE. */
11421 adjust = GEN_INT (step1);
11422 if (!SMALL_OPERAND (step1))
11423 {
11424 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
11425 adjust = MIPS_EPILOGUE_TEMP (Pmode);
11426 }
11427 mips_deallocate_stack (base, adjust, step2);
11428
11429 /* If we're using addressing macros, $gp is implicitly used by all
11430 SYMBOL_REFs. We must emit a blockage insn before restoring $gp
11431 from the stack. */
11432 if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
11433 emit_insn (gen_blockage ());
11434
11435 mips_epilogue.cfa_restore_sp_offset = step2;
11436 if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
11437 {
11438 unsigned int regno, mask;
11439 HOST_WIDE_INT offset;
11440 rtx restore;
11441
11442 /* Generate the restore instruction. */
11443 mask = frame->mask;
11444 restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
11445
11446 /* Restore any other registers manually. */
11447 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11448 if (BITSET_P (mask, regno - GP_REG_FIRST))
11449 {
11450 offset -= UNITS_PER_WORD;
11451 mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
11452 }
11453
11454 /* Restore the remaining registers and deallocate the final bit
11455 of the frame. */
11456 mips_frame_barrier ();
11457 emit_insn (restore);
11458 mips_epilogue_set_cfa (stack_pointer_rtx, 0);
11459 }
11460 else
11461 {
11462 /* Restore the registers. */
11463 mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
11464 mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
11465 mips_restore_reg);
11466
11467 if (cfun->machine->interrupt_handler_p)
11468 {
11469 HOST_WIDE_INT offset;
11470 rtx mem;
11471
11472 offset = frame->cop0_sp_offset - (frame->total_size - step2);
11473 if (!cfun->machine->keep_interrupts_masked_p)
11474 {
11475 /* Restore the original EPC. */
11476 mem = gen_frame_mem (word_mode,
11477 plus_constant (Pmode, stack_pointer_rtx,
11478 offset));
11479 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11480 offset -= UNITS_PER_WORD;
11481
11482 /* Move to COP0 EPC. */
11483 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
11484 gen_rtx_REG (SImode, K0_REG_NUM)));
11485 }
11486
11487 /* Restore the original Status. */
11488 mem = gen_frame_mem (word_mode,
11489 plus_constant (Pmode, stack_pointer_rtx,
11490 offset));
11491 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11492 offset -= UNITS_PER_WORD;
11493
11494 /* If we don't use shadow register set, we need to update SP. */
11495 if (!cfun->machine->use_shadow_register_set_p)
11496 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11497 else
11498 /* The choice of position is somewhat arbitrary in this case. */
11499 mips_epilogue_emit_cfa_restores ();
11500
11501 /* Move to COP0 Status. */
11502 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11503 gen_rtx_REG (SImode, K0_REG_NUM)));
11504 }
11505 else if (TARGET_MICROMIPS
11506 && !crtl->calls_eh_return
11507 && !sibcall_p
11508 && step2 > 0
11509 && mips_unsigned_immediate_p (step2, 5, 2))
11510 use_jraddiusp_p = true;
11511 else
11512 /* Deallocate the final bit of the frame. */
11513 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11514 }
11515
11516 if (!use_jraddiusp_p)
11517 gcc_assert (!mips_epilogue.cfa_restores);
11518
11519 /* Add in the __builtin_eh_return stack adjustment. We need to
11520 use a temporary in MIPS16 code. */
11521 if (crtl->calls_eh_return)
11522 {
11523 if (TARGET_MIPS16)
11524 {
11525 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
11526 emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
11527 MIPS_EPILOGUE_TEMP (Pmode),
11528 EH_RETURN_STACKADJ_RTX));
11529 mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
11530 }
11531 else
11532 emit_insn (gen_add3_insn (stack_pointer_rtx,
11533 stack_pointer_rtx,
11534 EH_RETURN_STACKADJ_RTX));
11535 }
11536
11537 if (!sibcall_p)
11538 {
11539 mips_expand_before_return ();
11540 if (cfun->machine->interrupt_handler_p)
11541 {
11542 /* Interrupt handlers generate eret or deret. */
11543 if (cfun->machine->use_debug_exception_return_p)
11544 emit_jump_insn (gen_mips_deret ());
11545 else
11546 emit_jump_insn (gen_mips_eret ());
11547 }
11548 else
11549 {
11550 rtx pat;
11551
11552 /* When generating MIPS16 code, the normal
11553 mips_for_each_saved_gpr_and_fpr path will restore the return
11554 address into $7 rather than $31. */
11555 if (TARGET_MIPS16
11556 && !GENERATE_MIPS16E_SAVE_RESTORE
11557 && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
11558 {
11559 /* simple_returns cannot rely on values that are only available
11560 on paths through the epilogue (because return paths that do
11561 not pass through the epilogue may nevertheless reuse a
11562 simple_return that occurs at the end of the epilogue).
11563 Use a normal return here instead. */
11564 rtx reg = gen_rtx_REG (Pmode, GP_REG_FIRST + 7);
11565 pat = gen_return_internal (reg);
11566 }
11567 else if (use_jraddiusp_p)
11568 pat = gen_jraddiusp (GEN_INT (step2));
11569 else
11570 {
11571 rtx reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
11572 pat = gen_simple_return_internal (reg);
11573 }
11574 emit_jump_insn (pat);
11575 if (use_jraddiusp_p)
11576 mips_epilogue_set_cfa (stack_pointer_rtx, step2);
11577 }
11578 }
11579
11580 /* Search from the beginning to the first use of K0 or K1. */
11581 if (cfun->machine->interrupt_handler_p
11582 && !cfun->machine->keep_interrupts_masked_p)
11583 {
11584 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
11585 if (INSN_P (insn)
11586 && for_each_rtx (&PATTERN(insn), mips_kernel_reg_p, NULL))
11587 break;
11588 gcc_assert (insn != NULL_RTX);
11589 /* Insert disable interrupts before the first use of K0 or K1. */
11590 emit_insn_before (gen_mips_di (), insn);
11591 emit_insn_before (gen_mips_ehb (), insn);
11592 }
11593 }
11594 \f
11595 /* Return nonzero if this function is known to have a null epilogue.
11596 This allows the optimizer to omit jumps to jumps if no stack
11597 was created. */
11598
11599 bool
11600 mips_can_use_return_insn (void)
11601 {
11602 /* Interrupt handlers need to go through the epilogue. */
11603 if (cfun->machine->interrupt_handler_p)
11604 return false;
11605
11606 if (!reload_completed)
11607 return false;
11608
11609 if (crtl->profile)
11610 return false;
11611
11612 /* In MIPS16 mode, a function that returns a floating-point value
11613 needs to arrange to copy the return value into the floating-point
11614 registers. */
11615 if (mips16_cfun_returns_in_fpr_p ())
11616 return false;
11617
11618 return cfun->machine->frame.total_size == 0;
11619 }
11620 \f
11621 /* Return true if register REGNO can store a value of mode MODE.
11622 The result of this function is cached in mips_hard_regno_mode_ok. */
11623
11624 static bool
11625 mips_hard_regno_mode_ok_p (unsigned int regno, enum machine_mode mode)
11626 {
11627 unsigned int size;
11628 enum mode_class mclass;
11629
11630 if (mode == CCV2mode)
11631 return (ISA_HAS_8CC
11632 && ST_REG_P (regno)
11633 && (regno - ST_REG_FIRST) % 2 == 0);
11634
11635 if (mode == CCV4mode)
11636 return (ISA_HAS_8CC
11637 && ST_REG_P (regno)
11638 && (regno - ST_REG_FIRST) % 4 == 0);
11639
11640 if (mode == CCmode)
11641 return ISA_HAS_8CC ? ST_REG_P (regno) : regno == FPSW_REGNUM;
11642
11643 size = GET_MODE_SIZE (mode);
11644 mclass = GET_MODE_CLASS (mode);
11645
11646 if (GP_REG_P (regno))
11647 return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
11648
11649 if (FP_REG_P (regno)
11650 && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
11651 || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
11652 {
11653 /* Allow 64-bit vector modes for Loongson-2E/2F. */
11654 if (TARGET_LOONGSON_VECTORS
11655 && (mode == V2SImode
11656 || mode == V4HImode
11657 || mode == V8QImode
11658 || mode == DImode))
11659 return true;
11660
11661 if (mclass == MODE_FLOAT
11662 || mclass == MODE_COMPLEX_FLOAT
11663 || mclass == MODE_VECTOR_FLOAT)
11664 return size <= UNITS_PER_FPVALUE;
11665
11666 /* Allow integer modes that fit into a single register. We need
11667 to put integers into FPRs when using instructions like CVT
11668 and TRUNC. There's no point allowing sizes smaller than a word,
11669 because the FPU has no appropriate load/store instructions. */
11670 if (mclass == MODE_INT)
11671 return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
11672 }
11673
11674 if (ACC_REG_P (regno)
11675 && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
11676 {
11677 if (MD_REG_P (regno))
11678 {
11679 /* After a multiplication or division, clobbering HI makes
11680 the value of LO unpredictable, and vice versa. This means
11681 that, for all interesting cases, HI and LO are effectively
11682 a single register.
11683
11684 We model this by requiring that any value that uses HI
11685 also uses LO. */
11686 if (size <= UNITS_PER_WORD * 2)
11687 return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
11688 }
11689 else
11690 {
11691 /* DSP accumulators do not have the same restrictions as
11692 HI and LO, so we can treat them as normal doubleword
11693 registers. */
11694 if (size <= UNITS_PER_WORD)
11695 return true;
11696
11697 if (size <= UNITS_PER_WORD * 2
11698 && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
11699 return true;
11700 }
11701 }
11702
11703 if (ALL_COP_REG_P (regno))
11704 return mclass == MODE_INT && size <= UNITS_PER_WORD;
11705
11706 if (regno == GOT_VERSION_REGNUM)
11707 return mode == SImode;
11708
11709 return false;
11710 }
11711
11712 /* Implement HARD_REGNO_NREGS. */
11713
11714 unsigned int
11715 mips_hard_regno_nregs (int regno, enum machine_mode mode)
11716 {
11717 if (ST_REG_P (regno))
11718 /* The size of FP status registers is always 4, because they only hold
11719 CCmode values, and CCmode is always considered to be 4 bytes wide. */
11720 return (GET_MODE_SIZE (mode) + 3) / 4;
11721
11722 if (FP_REG_P (regno))
11723 return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
11724
11725 /* All other registers are word-sized. */
11726 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
11727 }
11728
11729 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
11730 in mips_hard_regno_nregs. */
11731
11732 int
11733 mips_class_max_nregs (enum reg_class rclass, enum machine_mode mode)
11734 {
11735 int size;
11736 HARD_REG_SET left;
11737
11738 size = 0x8000;
11739 COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
11740 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
11741 {
11742 if (HARD_REGNO_MODE_OK (ST_REG_FIRST, mode))
11743 size = MIN (size, 4);
11744 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) ST_REGS]);
11745 }
11746 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
11747 {
11748 if (HARD_REGNO_MODE_OK (FP_REG_FIRST, mode))
11749 size = MIN (size, UNITS_PER_FPREG);
11750 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
11751 }
11752 if (!hard_reg_set_empty_p (left))
11753 size = MIN (size, UNITS_PER_WORD);
11754 return (GET_MODE_SIZE (mode) + size - 1) / size;
11755 }
11756
11757 /* Implement CANNOT_CHANGE_MODE_CLASS. */
11758
11759 bool
11760 mips_cannot_change_mode_class (enum machine_mode from,
11761 enum machine_mode to,
11762 enum reg_class rclass)
11763 {
11764 /* Allow conversions between different Loongson integer vectors,
11765 and between those vectors and DImode. */
11766 if (GET_MODE_SIZE (from) == 8 && GET_MODE_SIZE (to) == 8
11767 && INTEGRAL_MODE_P (from) && INTEGRAL_MODE_P (to))
11768 return false;
11769
11770 /* Otherwise, there are several problems with changing the modes of
11771 values in floating-point registers:
11772
11773 - When a multi-word value is stored in paired floating-point
11774 registers, the first register always holds the low word. We
11775 therefore can't allow FPRs to change between single-word and
11776 multi-word modes on big-endian targets.
11777
11778 - GCC assumes that each word of a multiword register can be
11779 accessed individually using SUBREGs. This is not true for
11780 floating-point registers if they are bigger than a word.
11781
11782 - Loading a 32-bit value into a 64-bit floating-point register
11783 will not sign-extend the value, despite what LOAD_EXTEND_OP
11784 says. We can't allow FPRs to change from SImode to a wider
11785 mode on 64-bit targets.
11786
11787 - If the FPU has already interpreted a value in one format, we
11788 must not ask it to treat the value as having a different
11789 format.
11790
11791 We therefore disallow all mode changes involving FPRs. */
11792
11793 return reg_classes_intersect_p (FP_REGS, rclass);
11794 }
11795
11796 /* Implement target hook small_register_classes_for_mode_p. */
11797
11798 static bool
11799 mips_small_register_classes_for_mode_p (enum machine_mode mode
11800 ATTRIBUTE_UNUSED)
11801 {
11802 return TARGET_MIPS16;
11803 }
11804
11805 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction. */
11806
11807 static bool
11808 mips_mode_ok_for_mov_fmt_p (enum machine_mode mode)
11809 {
11810 switch (mode)
11811 {
11812 case SFmode:
11813 return TARGET_HARD_FLOAT;
11814
11815 case DFmode:
11816 return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
11817
11818 case V2SFmode:
11819 return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
11820
11821 default:
11822 return false;
11823 }
11824 }
11825
11826 /* Implement MODES_TIEABLE_P. */
11827
11828 bool
11829 mips_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
11830 {
11831 /* FPRs allow no mode punning, so it's not worth tying modes if we'd
11832 prefer to put one of them in FPRs. */
11833 return (mode1 == mode2
11834 || (!mips_mode_ok_for_mov_fmt_p (mode1)
11835 && !mips_mode_ok_for_mov_fmt_p (mode2)));
11836 }
11837
11838 /* Implement TARGET_PREFERRED_RELOAD_CLASS. */
11839
11840 static reg_class_t
11841 mips_preferred_reload_class (rtx x, reg_class_t rclass)
11842 {
11843 if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
11844 return LEA_REGS;
11845
11846 if (reg_class_subset_p (FP_REGS, rclass)
11847 && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
11848 return FP_REGS;
11849
11850 if (reg_class_subset_p (GR_REGS, rclass))
11851 rclass = GR_REGS;
11852
11853 if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
11854 rclass = M16_REGS;
11855
11856 return rclass;
11857 }
11858
11859 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
11860 Return a "canonical" class to represent it in later calculations. */
11861
11862 static reg_class_t
11863 mips_canonicalize_move_class (reg_class_t rclass)
11864 {
11865 /* All moves involving accumulator registers have the same cost. */
11866 if (reg_class_subset_p (rclass, ACC_REGS))
11867 rclass = ACC_REGS;
11868
11869 /* Likewise promote subclasses of general registers to the most
11870 interesting containing class. */
11871 if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
11872 rclass = M16_REGS;
11873 else if (reg_class_subset_p (rclass, GENERAL_REGS))
11874 rclass = GENERAL_REGS;
11875
11876 return rclass;
11877 }
11878
11879 /* Return the cost of moving a value of mode MODE from a register of
11880 class FROM to a GPR. Return 0 for classes that are unions of other
11881 classes handled by this function. */
11882
11883 static int
11884 mips_move_to_gpr_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
11885 reg_class_t from)
11886 {
11887 switch (from)
11888 {
11889 case GENERAL_REGS:
11890 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
11891 return 2;
11892
11893 case ACC_REGS:
11894 /* MFLO and MFHI. */
11895 return 6;
11896
11897 case FP_REGS:
11898 /* MFC1, etc. */
11899 return 4;
11900
11901 case ST_REGS:
11902 /* LUI followed by MOVF. */
11903 return 4;
11904
11905 case COP0_REGS:
11906 case COP2_REGS:
11907 case COP3_REGS:
11908 /* This choice of value is historical. */
11909 return 5;
11910
11911 default:
11912 return 0;
11913 }
11914 }
11915
11916 /* Return the cost of moving a value of mode MODE from a GPR to a
11917 register of class TO. Return 0 for classes that are unions of
11918 other classes handled by this function. */
11919
11920 static int
11921 mips_move_from_gpr_cost (enum machine_mode mode, reg_class_t to)
11922 {
11923 switch (to)
11924 {
11925 case GENERAL_REGS:
11926 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
11927 return 2;
11928
11929 case ACC_REGS:
11930 /* MTLO and MTHI. */
11931 return 6;
11932
11933 case FP_REGS:
11934 /* MTC1, etc. */
11935 return 4;
11936
11937 case ST_REGS:
11938 /* A secondary reload through an FPR scratch. */
11939 return (mips_register_move_cost (mode, GENERAL_REGS, FP_REGS)
11940 + mips_register_move_cost (mode, FP_REGS, ST_REGS));
11941
11942 case COP0_REGS:
11943 case COP2_REGS:
11944 case COP3_REGS:
11945 /* This choice of value is historical. */
11946 return 5;
11947
11948 default:
11949 return 0;
11950 }
11951 }
11952
11953 /* Implement TARGET_REGISTER_MOVE_COST. Return 0 for classes that are the
11954 maximum of the move costs for subclasses; regclass will work out
11955 the maximum for us. */
11956
11957 static int
11958 mips_register_move_cost (enum machine_mode mode,
11959 reg_class_t from, reg_class_t to)
11960 {
11961 reg_class_t dregs;
11962 int cost1, cost2;
11963
11964 from = mips_canonicalize_move_class (from);
11965 to = mips_canonicalize_move_class (to);
11966
11967 /* Handle moves that can be done without using general-purpose registers. */
11968 if (from == FP_REGS)
11969 {
11970 if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
11971 /* MOV.FMT. */
11972 return 4;
11973 if (to == ST_REGS)
11974 /* The sequence generated by mips_expand_fcc_reload. */
11975 return 8;
11976 }
11977
11978 /* Handle cases in which only one class deviates from the ideal. */
11979 dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
11980 if (from == dregs)
11981 return mips_move_from_gpr_cost (mode, to);
11982 if (to == dregs)
11983 return mips_move_to_gpr_cost (mode, from);
11984
11985 /* Handles cases that require a GPR temporary. */
11986 cost1 = mips_move_to_gpr_cost (mode, from);
11987 if (cost1 != 0)
11988 {
11989 cost2 = mips_move_from_gpr_cost (mode, to);
11990 if (cost2 != 0)
11991 return cost1 + cost2;
11992 }
11993
11994 return 0;
11995 }
11996
11997 /* Implement TARGET_MEMORY_MOVE_COST. */
11998
11999 static int
12000 mips_memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
12001 {
12002 return (mips_cost->memory_latency
12003 + memory_move_secondary_cost (mode, rclass, in));
12004 }
12005
12006 /* Return the register class required for a secondary register when
12007 copying between one of the registers in RCLASS and value X, which
12008 has mode MODE. X is the source of the move if IN_P, otherwise it
12009 is the destination. Return NO_REGS if no secondary register is
12010 needed. */
12011
12012 enum reg_class
12013 mips_secondary_reload_class (enum reg_class rclass,
12014 enum machine_mode mode, rtx x, bool in_p)
12015 {
12016 int regno;
12017
12018 /* If X is a constant that cannot be loaded into $25, it must be loaded
12019 into some other GPR. No other register class allows a direct move. */
12020 if (mips_dangerous_for_la25_p (x))
12021 return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
12022
12023 regno = true_regnum (x);
12024 if (TARGET_MIPS16)
12025 {
12026 /* In MIPS16 mode, every move must involve a member of M16_REGS. */
12027 if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
12028 return M16_REGS;
12029
12030 return NO_REGS;
12031 }
12032
12033 /* Copying from accumulator registers to anywhere other than a general
12034 register requires a temporary general register. */
12035 if (reg_class_subset_p (rclass, ACC_REGS))
12036 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
12037 if (ACC_REG_P (regno))
12038 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12039
12040 /* We can only copy a value to a condition code register from a
12041 floating-point register, and even then we require a scratch
12042 floating-point register. We can only copy a value out of a
12043 condition-code register into a general register. */
12044 if (reg_class_subset_p (rclass, ST_REGS))
12045 {
12046 if (in_p)
12047 return FP_REGS;
12048 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
12049 }
12050 if (ST_REG_P (regno))
12051 {
12052 if (!in_p)
12053 return FP_REGS;
12054 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12055 }
12056
12057 if (reg_class_subset_p (rclass, FP_REGS))
12058 {
12059 if (MEM_P (x)
12060 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8))
12061 /* In this case we can use lwc1, swc1, ldc1 or sdc1. We'll use
12062 pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported. */
12063 return NO_REGS;
12064
12065 if (GP_REG_P (regno) || x == CONST0_RTX (mode))
12066 /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1. */
12067 return NO_REGS;
12068
12069 if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
12070 /* We can force the constant to memory and use lwc1
12071 and ldc1. As above, we will use pairs of lwc1s if
12072 ldc1 is not supported. */
12073 return NO_REGS;
12074
12075 if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
12076 /* In this case we can use mov.fmt. */
12077 return NO_REGS;
12078
12079 /* Otherwise, we need to reload through an integer register. */
12080 return GR_REGS;
12081 }
12082 if (FP_REG_P (regno))
12083 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12084
12085 return NO_REGS;
12086 }
12087
12088 /* Implement TARGET_MODE_REP_EXTENDED. */
12089
12090 static int
12091 mips_mode_rep_extended (enum machine_mode mode, enum machine_mode mode_rep)
12092 {
12093 /* On 64-bit targets, SImode register values are sign-extended to DImode. */
12094 if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
12095 return SIGN_EXTEND;
12096
12097 return UNKNOWN;
12098 }
12099 \f
12100 /* Implement TARGET_VALID_POINTER_MODE. */
12101
12102 static bool
12103 mips_valid_pointer_mode (enum machine_mode mode)
12104 {
12105 return mode == SImode || (TARGET_64BIT && mode == DImode);
12106 }
12107
12108 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P. */
12109
12110 static bool
12111 mips_vector_mode_supported_p (enum machine_mode mode)
12112 {
12113 switch (mode)
12114 {
12115 case V2SFmode:
12116 return TARGET_PAIRED_SINGLE_FLOAT;
12117
12118 case V2HImode:
12119 case V4QImode:
12120 case V2HQmode:
12121 case V2UHQmode:
12122 case V2HAmode:
12123 case V2UHAmode:
12124 case V4QQmode:
12125 case V4UQQmode:
12126 return TARGET_DSP;
12127
12128 case V2SImode:
12129 case V4HImode:
12130 case V8QImode:
12131 return TARGET_LOONGSON_VECTORS;
12132
12133 default:
12134 return false;
12135 }
12136 }
12137
12138 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P. */
12139
12140 static bool
12141 mips_scalar_mode_supported_p (enum machine_mode mode)
12142 {
12143 if (ALL_FIXED_POINT_MODE_P (mode)
12144 && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
12145 return true;
12146
12147 return default_scalar_mode_supported_p (mode);
12148 }
12149 \f
12150 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE. */
12151
12152 static enum machine_mode
12153 mips_preferred_simd_mode (enum machine_mode mode ATTRIBUTE_UNUSED)
12154 {
12155 if (TARGET_PAIRED_SINGLE_FLOAT
12156 && mode == SFmode)
12157 return V2SFmode;
12158 return word_mode;
12159 }
12160
12161 /* Implement TARGET_INIT_LIBFUNCS. */
12162
12163 static void
12164 mips_init_libfuncs (void)
12165 {
12166 if (TARGET_FIX_VR4120)
12167 {
12168 /* Register the special divsi3 and modsi3 functions needed to work
12169 around VR4120 division errata. */
12170 set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
12171 set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
12172 }
12173
12174 if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
12175 {
12176 /* Register the MIPS16 -mhard-float stubs. */
12177 set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
12178 set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
12179 set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
12180 set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
12181
12182 set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
12183 set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
12184 set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
12185 set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
12186 set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
12187 set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
12188 set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
12189
12190 set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
12191 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
12192 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
12193
12194 if (TARGET_DOUBLE_FLOAT)
12195 {
12196 set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
12197 set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
12198 set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
12199 set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
12200
12201 set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
12202 set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
12203 set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
12204 set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
12205 set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
12206 set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
12207 set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
12208
12209 set_conv_libfunc (sext_optab, DFmode, SFmode,
12210 "__mips16_extendsfdf2");
12211 set_conv_libfunc (trunc_optab, SFmode, DFmode,
12212 "__mips16_truncdfsf2");
12213 set_conv_libfunc (sfix_optab, SImode, DFmode,
12214 "__mips16_fix_truncdfsi");
12215 set_conv_libfunc (sfloat_optab, DFmode, SImode,
12216 "__mips16_floatsidf");
12217 set_conv_libfunc (ufloat_optab, DFmode, SImode,
12218 "__mips16_floatunsidf");
12219 }
12220 }
12221
12222 /* The MIPS16 ISA does not have an encoding for "sync", so we rely
12223 on an external non-MIPS16 routine to implement __sync_synchronize.
12224 Similarly for the rest of the ll/sc libfuncs. */
12225 if (TARGET_MIPS16)
12226 {
12227 synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
12228 init_sync_libfuncs (UNITS_PER_WORD);
12229 }
12230 }
12231
12232 /* Build up a multi-insn sequence that loads label TARGET into $AT. */
12233
12234 static void
12235 mips_process_load_label (rtx target)
12236 {
12237 rtx base, gp, intop;
12238 HOST_WIDE_INT offset;
12239
12240 mips_multi_start ();
12241 switch (mips_abi)
12242 {
12243 case ABI_N32:
12244 mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
12245 mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
12246 break;
12247
12248 case ABI_64:
12249 mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
12250 mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
12251 break;
12252
12253 default:
12254 gp = pic_offset_table_rtx;
12255 if (mips_cfun_has_cprestore_slot_p ())
12256 {
12257 gp = gen_rtx_REG (Pmode, AT_REGNUM);
12258 mips_get_cprestore_base_and_offset (&base, &offset, true);
12259 if (!SMALL_OPERAND (offset))
12260 {
12261 intop = GEN_INT (CONST_HIGH_PART (offset));
12262 mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
12263 mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
12264
12265 base = gp;
12266 offset = CONST_LOW_PART (offset);
12267 }
12268 intop = GEN_INT (offset);
12269 if (ISA_HAS_LOAD_DELAY)
12270 mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
12271 else
12272 mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
12273 }
12274 if (ISA_HAS_LOAD_DELAY)
12275 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
12276 else
12277 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
12278 mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
12279 break;
12280 }
12281 }
12282
12283 /* Return the number of instructions needed to load a label into $AT. */
12284
12285 static unsigned int
12286 mips_load_label_num_insns (void)
12287 {
12288 if (cfun->machine->load_label_num_insns == 0)
12289 {
12290 mips_process_load_label (pc_rtx);
12291 cfun->machine->load_label_num_insns = mips_multi_num_insns;
12292 }
12293 return cfun->machine->load_label_num_insns;
12294 }
12295
12296 /* Emit an asm sequence to start a noat block and load the address
12297 of a label into $1. */
12298
12299 void
12300 mips_output_load_label (rtx target)
12301 {
12302 mips_push_asm_switch (&mips_noat);
12303 if (TARGET_EXPLICIT_RELOCS)
12304 {
12305 mips_process_load_label (target);
12306 mips_multi_write ();
12307 }
12308 else
12309 {
12310 if (Pmode == DImode)
12311 output_asm_insn ("dla\t%@,%0", &target);
12312 else
12313 output_asm_insn ("la\t%@,%0", &target);
12314 }
12315 }
12316
12317 /* Return the length of INSN. LENGTH is the initial length computed by
12318 attributes in the machine-description file. */
12319
12320 int
12321 mips_adjust_insn_length (rtx insn, int length)
12322 {
12323 /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
12324 of a PIC long-branch sequence. Substitute the correct value. */
12325 if (length == MAX_PIC_BRANCH_LENGTH
12326 && JUMP_P (insn)
12327 && INSN_CODE (insn) >= 0
12328 && get_attr_type (insn) == TYPE_BRANCH)
12329 {
12330 /* Add the branch-over instruction and its delay slot, if this
12331 is a conditional branch. */
12332 length = simplejump_p (insn) ? 0 : 8;
12333
12334 /* Add the size of a load into $AT. */
12335 length += BASE_INSN_LENGTH * mips_load_label_num_insns ();
12336
12337 /* Add the length of an indirect jump, ignoring the delay slot. */
12338 length += TARGET_COMPRESSION ? 2 : 4;
12339 }
12340
12341 /* A unconditional jump has an unfilled delay slot if it is not part
12342 of a sequence. A conditional jump normally has a delay slot, but
12343 does not on MIPS16. */
12344 if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
12345 length += TARGET_MIPS16 ? 2 : 4;
12346
12347 /* See how many nops might be needed to avoid hardware hazards. */
12348 if (!cfun->machine->ignore_hazard_length_p
12349 && INSN_P (insn)
12350 && INSN_CODE (insn) >= 0)
12351 switch (get_attr_hazard (insn))
12352 {
12353 case HAZARD_NONE:
12354 break;
12355
12356 case HAZARD_DELAY:
12357 length += NOP_INSN_LENGTH;
12358 break;
12359
12360 case HAZARD_HILO:
12361 length += NOP_INSN_LENGTH * 2;
12362 break;
12363 }
12364
12365 return length;
12366 }
12367
12368 /* Return the assembly code for INSN, which has the operands given by
12369 OPERANDS, and which branches to OPERANDS[0] if some condition is true.
12370 BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
12371 is in range of a direct branch. BRANCH_IF_FALSE is an inverted
12372 version of BRANCH_IF_TRUE. */
12373
12374 const char *
12375 mips_output_conditional_branch (rtx insn, rtx *operands,
12376 const char *branch_if_true,
12377 const char *branch_if_false)
12378 {
12379 unsigned int length;
12380 rtx taken, not_taken;
12381
12382 gcc_assert (LABEL_P (operands[0]));
12383
12384 length = get_attr_length (insn);
12385 if (length <= 8)
12386 {
12387 /* Just a simple conditional branch. */
12388 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
12389 return branch_if_true;
12390 }
12391
12392 /* Generate a reversed branch around a direct jump. This fallback does
12393 not use branch-likely instructions. */
12394 mips_branch_likely = false;
12395 not_taken = gen_label_rtx ();
12396 taken = operands[0];
12397
12398 /* Generate the reversed branch to NOT_TAKEN. */
12399 operands[0] = not_taken;
12400 output_asm_insn (branch_if_false, operands);
12401
12402 /* If INSN has a delay slot, we must provide delay slots for both the
12403 branch to NOT_TAKEN and the conditional jump. We must also ensure
12404 that INSN's delay slot is executed in the appropriate cases. */
12405 if (final_sequence)
12406 {
12407 /* This first delay slot will always be executed, so use INSN's
12408 delay slot if is not annulled. */
12409 if (!INSN_ANNULLED_BRANCH_P (insn))
12410 {
12411 final_scan_insn (XVECEXP (final_sequence, 0, 1),
12412 asm_out_file, optimize, 1, NULL);
12413 INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
12414 }
12415 else
12416 output_asm_insn ("nop", 0);
12417 fprintf (asm_out_file, "\n");
12418 }
12419
12420 /* Output the unconditional branch to TAKEN. */
12421 if (TARGET_ABSOLUTE_JUMPS)
12422 output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
12423 else
12424 {
12425 mips_output_load_label (taken);
12426 output_asm_insn ("jr\t%@%]%/", 0);
12427 }
12428
12429 /* Now deal with its delay slot; see above. */
12430 if (final_sequence)
12431 {
12432 /* This delay slot will only be executed if the branch is taken.
12433 Use INSN's delay slot if is annulled. */
12434 if (INSN_ANNULLED_BRANCH_P (insn))
12435 {
12436 final_scan_insn (XVECEXP (final_sequence, 0, 1),
12437 asm_out_file, optimize, 1, NULL);
12438 INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
12439 }
12440 else
12441 output_asm_insn ("nop", 0);
12442 fprintf (asm_out_file, "\n");
12443 }
12444
12445 /* Output NOT_TAKEN. */
12446 targetm.asm_out.internal_label (asm_out_file, "L",
12447 CODE_LABEL_NUMBER (not_taken));
12448 return "";
12449 }
12450
12451 /* Return the assembly code for INSN, which branches to OPERANDS[0]
12452 if some ordering condition is true. The condition is given by
12453 OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
12454 OPERANDS[1]. OPERANDS[2] is the comparison's first operand;
12455 its second is always zero. */
12456
12457 const char *
12458 mips_output_order_conditional_branch (rtx insn, rtx *operands, bool inverted_p)
12459 {
12460 const char *branch[2];
12461
12462 /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
12463 Make BRANCH[0] branch on the inverse condition. */
12464 switch (GET_CODE (operands[1]))
12465 {
12466 /* These cases are equivalent to comparisons against zero. */
12467 case LEU:
12468 inverted_p = !inverted_p;
12469 /* Fall through. */
12470 case GTU:
12471 branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
12472 branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
12473 break;
12474
12475 /* These cases are always true or always false. */
12476 case LTU:
12477 inverted_p = !inverted_p;
12478 /* Fall through. */
12479 case GEU:
12480 branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
12481 branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
12482 break;
12483
12484 default:
12485 branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
12486 branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
12487 break;
12488 }
12489 return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
12490 }
12491 \f
12492 /* Start a block of code that needs access to the LL, SC and SYNC
12493 instructions. */
12494
12495 static void
12496 mips_start_ll_sc_sync_block (void)
12497 {
12498 if (!ISA_HAS_LL_SC)
12499 {
12500 output_asm_insn (".set\tpush", 0);
12501 if (TARGET_64BIT)
12502 output_asm_insn (".set\tmips3", 0);
12503 else
12504 output_asm_insn (".set\tmips2", 0);
12505 }
12506 }
12507
12508 /* End a block started by mips_start_ll_sc_sync_block. */
12509
12510 static void
12511 mips_end_ll_sc_sync_block (void)
12512 {
12513 if (!ISA_HAS_LL_SC)
12514 output_asm_insn (".set\tpop", 0);
12515 }
12516
12517 /* Output and/or return the asm template for a sync instruction. */
12518
12519 const char *
12520 mips_output_sync (void)
12521 {
12522 mips_start_ll_sc_sync_block ();
12523 output_asm_insn ("sync", 0);
12524 mips_end_ll_sc_sync_block ();
12525 return "";
12526 }
12527
12528 /* Return the asm template associated with sync_insn1 value TYPE.
12529 IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation. */
12530
12531 static const char *
12532 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
12533 {
12534 switch (type)
12535 {
12536 case SYNC_INSN1_MOVE:
12537 return "move\t%0,%z2";
12538 case SYNC_INSN1_LI:
12539 return "li\t%0,%2";
12540 case SYNC_INSN1_ADDU:
12541 return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
12542 case SYNC_INSN1_ADDIU:
12543 return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
12544 case SYNC_INSN1_SUBU:
12545 return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
12546 case SYNC_INSN1_AND:
12547 return "and\t%0,%1,%z2";
12548 case SYNC_INSN1_ANDI:
12549 return "andi\t%0,%1,%2";
12550 case SYNC_INSN1_OR:
12551 return "or\t%0,%1,%z2";
12552 case SYNC_INSN1_ORI:
12553 return "ori\t%0,%1,%2";
12554 case SYNC_INSN1_XOR:
12555 return "xor\t%0,%1,%z2";
12556 case SYNC_INSN1_XORI:
12557 return "xori\t%0,%1,%2";
12558 }
12559 gcc_unreachable ();
12560 }
12561
12562 /* Return the asm template associated with sync_insn2 value TYPE. */
12563
12564 static const char *
12565 mips_sync_insn2_template (enum attr_sync_insn2 type)
12566 {
12567 switch (type)
12568 {
12569 case SYNC_INSN2_NOP:
12570 gcc_unreachable ();
12571 case SYNC_INSN2_AND:
12572 return "and\t%0,%1,%z2";
12573 case SYNC_INSN2_XOR:
12574 return "xor\t%0,%1,%z2";
12575 case SYNC_INSN2_NOT:
12576 return "nor\t%0,%1,%.";
12577 }
12578 gcc_unreachable ();
12579 }
12580
12581 /* OPERANDS are the operands to a sync loop instruction and INDEX is
12582 the value of the one of the sync_* attributes. Return the operand
12583 referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
12584 have the associated attribute. */
12585
12586 static rtx
12587 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
12588 {
12589 if (index > 0)
12590 default_value = operands[index - 1];
12591 return default_value;
12592 }
12593
12594 /* INSN is a sync loop with operands OPERANDS. Build up a multi-insn
12595 sequence for it. */
12596
12597 static void
12598 mips_process_sync_loop (rtx insn, rtx *operands)
12599 {
12600 rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
12601 rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3, cmp;
12602 unsigned int tmp3_insn;
12603 enum attr_sync_insn1 insn1;
12604 enum attr_sync_insn2 insn2;
12605 bool is_64bit_p;
12606 int memmodel_attr;
12607 enum memmodel model;
12608
12609 /* Read an operand from the sync_WHAT attribute and store it in
12610 variable WHAT. DEFAULT is the default value if no attribute
12611 is specified. */
12612 #define READ_OPERAND(WHAT, DEFAULT) \
12613 WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
12614 DEFAULT)
12615
12616 /* Read the memory. */
12617 READ_OPERAND (mem, 0);
12618 gcc_assert (mem);
12619 is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
12620
12621 /* Read the other attributes. */
12622 at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
12623 READ_OPERAND (oldval, at);
12624 READ_OPERAND (cmp, 0);
12625 READ_OPERAND (newval, at);
12626 READ_OPERAND (inclusive_mask, 0);
12627 READ_OPERAND (exclusive_mask, 0);
12628 READ_OPERAND (required_oldval, 0);
12629 READ_OPERAND (insn1_op2, 0);
12630 insn1 = get_attr_sync_insn1 (insn);
12631 insn2 = get_attr_sync_insn2 (insn);
12632
12633 /* Don't bother setting CMP result that is never used. */
12634 if (cmp && find_reg_note (insn, REG_UNUSED, cmp))
12635 cmp = 0;
12636
12637 memmodel_attr = get_attr_sync_memmodel (insn);
12638 switch (memmodel_attr)
12639 {
12640 case 10:
12641 model = MEMMODEL_ACQ_REL;
12642 break;
12643 case 11:
12644 model = MEMMODEL_ACQUIRE;
12645 break;
12646 default:
12647 model = (enum memmodel) INTVAL (operands[memmodel_attr]);
12648 }
12649
12650 mips_multi_start ();
12651
12652 /* Output the release side of the memory barrier. */
12653 if (need_atomic_barrier_p (model, true))
12654 {
12655 if (required_oldval == 0 && TARGET_OCTEON)
12656 {
12657 /* Octeon doesn't reorder reads, so a full barrier can be
12658 created by using SYNCW to order writes combined with the
12659 write from the following SC. When the SC successfully
12660 completes, we know that all preceding writes are also
12661 committed to the coherent memory system. It is possible
12662 for a single SYNCW to fail, but a pair of them will never
12663 fail, so we use two. */
12664 mips_multi_add_insn ("syncw", NULL);
12665 mips_multi_add_insn ("syncw", NULL);
12666 }
12667 else
12668 mips_multi_add_insn ("sync", NULL);
12669 }
12670
12671 /* Output the branch-back label. */
12672 mips_multi_add_label ("1:");
12673
12674 /* OLDVAL = *MEM. */
12675 mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
12676 oldval, mem, NULL);
12677
12678 /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2. */
12679 if (required_oldval)
12680 {
12681 if (inclusive_mask == 0)
12682 tmp1 = oldval;
12683 else
12684 {
12685 gcc_assert (oldval != at);
12686 mips_multi_add_insn ("and\t%0,%1,%2",
12687 at, oldval, inclusive_mask, NULL);
12688 tmp1 = at;
12689 }
12690 mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
12691
12692 /* CMP = 0 [delay slot]. */
12693 if (cmp)
12694 mips_multi_add_insn ("li\t%0,0", cmp, NULL);
12695 }
12696
12697 /* $TMP1 = OLDVAL & EXCLUSIVE_MASK. */
12698 if (exclusive_mask == 0)
12699 tmp1 = const0_rtx;
12700 else
12701 {
12702 gcc_assert (oldval != at);
12703 mips_multi_add_insn ("and\t%0,%1,%z2",
12704 at, oldval, exclusive_mask, NULL);
12705 tmp1 = at;
12706 }
12707
12708 /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
12709
12710 We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
12711 at least one instruction in that case. */
12712 if (insn1 == SYNC_INSN1_MOVE
12713 && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
12714 tmp2 = insn1_op2;
12715 else
12716 {
12717 mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
12718 newval, oldval, insn1_op2, NULL);
12719 tmp2 = newval;
12720 }
12721
12722 /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK). */
12723 if (insn2 == SYNC_INSN2_NOP)
12724 tmp3 = tmp2;
12725 else
12726 {
12727 mips_multi_add_insn (mips_sync_insn2_template (insn2),
12728 newval, tmp2, inclusive_mask, NULL);
12729 tmp3 = newval;
12730 }
12731 tmp3_insn = mips_multi_last_index ();
12732
12733 /* $AT = $TMP1 | $TMP3. */
12734 if (tmp1 == const0_rtx || tmp3 == const0_rtx)
12735 {
12736 mips_multi_set_operand (tmp3_insn, 0, at);
12737 tmp3 = at;
12738 }
12739 else
12740 {
12741 gcc_assert (tmp1 != tmp3);
12742 mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
12743 }
12744
12745 /* if (!commit (*MEM = $AT)) goto 1.
12746
12747 This will sometimes be a delayed branch; see the write code below
12748 for details. */
12749 mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
12750 mips_multi_add_insn ("beq%?\t%0,%.,1b", at, NULL);
12751
12752 /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot]. */
12753 if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
12754 {
12755 mips_multi_copy_insn (tmp3_insn);
12756 mips_multi_set_operand (mips_multi_last_index (), 0, newval);
12757 }
12758 else if (!(required_oldval && cmp))
12759 mips_multi_add_insn ("nop", NULL);
12760
12761 /* CMP = 1 -- either standalone or in a delay slot. */
12762 if (required_oldval && cmp)
12763 mips_multi_add_insn ("li\t%0,1", cmp, NULL);
12764
12765 /* Output the acquire side of the memory barrier. */
12766 if (TARGET_SYNC_AFTER_SC && need_atomic_barrier_p (model, false))
12767 mips_multi_add_insn ("sync", NULL);
12768
12769 /* Output the exit label, if needed. */
12770 if (required_oldval)
12771 mips_multi_add_label ("2:");
12772
12773 #undef READ_OPERAND
12774 }
12775
12776 /* Output and/or return the asm template for sync loop INSN, which has
12777 the operands given by OPERANDS. */
12778
12779 const char *
12780 mips_output_sync_loop (rtx insn, rtx *operands)
12781 {
12782 mips_process_sync_loop (insn, operands);
12783
12784 /* Use branch-likely instructions to work around the LL/SC R10000
12785 errata. */
12786 mips_branch_likely = TARGET_FIX_R10000;
12787
12788 mips_push_asm_switch (&mips_noreorder);
12789 mips_push_asm_switch (&mips_nomacro);
12790 mips_push_asm_switch (&mips_noat);
12791 mips_start_ll_sc_sync_block ();
12792
12793 mips_multi_write ();
12794
12795 mips_end_ll_sc_sync_block ();
12796 mips_pop_asm_switch (&mips_noat);
12797 mips_pop_asm_switch (&mips_nomacro);
12798 mips_pop_asm_switch (&mips_noreorder);
12799
12800 return "";
12801 }
12802
12803 /* Return the number of individual instructions in sync loop INSN,
12804 which has the operands given by OPERANDS. */
12805
12806 unsigned int
12807 mips_sync_loop_insns (rtx insn, rtx *operands)
12808 {
12809 mips_process_sync_loop (insn, operands);
12810 return mips_multi_num_insns;
12811 }
12812 \f
12813 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
12814 the operands given by OPERANDS. Add in a divide-by-zero check if needed.
12815
12816 When working around R4000 and R4400 errata, we need to make sure that
12817 the division is not immediately followed by a shift[1][2]. We also
12818 need to stop the division from being put into a branch delay slot[3].
12819 The easiest way to avoid both problems is to add a nop after the
12820 division. When a divide-by-zero check is needed, this nop can be
12821 used to fill the branch delay slot.
12822
12823 [1] If a double-word or a variable shift executes immediately
12824 after starting an integer division, the shift may give an
12825 incorrect result. See quotations of errata #16 and #28 from
12826 "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12827 in mips.md for details.
12828
12829 [2] A similar bug to [1] exists for all revisions of the
12830 R4000 and the R4400 when run in an MC configuration.
12831 From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
12832
12833 "19. In this following sequence:
12834
12835 ddiv (or ddivu or div or divu)
12836 dsll32 (or dsrl32, dsra32)
12837
12838 if an MPT stall occurs, while the divide is slipping the cpu
12839 pipeline, then the following double shift would end up with an
12840 incorrect result.
12841
12842 Workaround: The compiler needs to avoid generating any
12843 sequence with divide followed by extended double shift."
12844
12845 This erratum is also present in "MIPS R4400MC Errata, Processor
12846 Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
12847 & 3.0" as errata #10 and #4, respectively.
12848
12849 [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12850 (also valid for MIPS R4000MC processors):
12851
12852 "52. R4000SC: This bug does not apply for the R4000PC.
12853
12854 There are two flavors of this bug:
12855
12856 1) If the instruction just after divide takes an RF exception
12857 (tlb-refill, tlb-invalid) and gets an instruction cache
12858 miss (both primary and secondary) and the line which is
12859 currently in secondary cache at this index had the first
12860 data word, where the bits 5..2 are set, then R4000 would
12861 get a wrong result for the div.
12862
12863 ##1
12864 nop
12865 div r8, r9
12866 ------------------- # end-of page. -tlb-refill
12867 nop
12868 ##2
12869 nop
12870 div r8, r9
12871 ------------------- # end-of page. -tlb-invalid
12872 nop
12873
12874 2) If the divide is in the taken branch delay slot, where the
12875 target takes RF exception and gets an I-cache miss for the
12876 exception vector or where I-cache miss occurs for the
12877 target address, under the above mentioned scenarios, the
12878 div would get wrong results.
12879
12880 ##1
12881 j r2 # to next page mapped or unmapped
12882 div r8,r9 # this bug would be there as long
12883 # as there is an ICache miss and
12884 nop # the "data pattern" is present
12885
12886 ##2
12887 beq r0, r0, NextPage # to Next page
12888 div r8,r9
12889 nop
12890
12891 This bug is present for div, divu, ddiv, and ddivu
12892 instructions.
12893
12894 Workaround: For item 1), OS could make sure that the next page
12895 after the divide instruction is also mapped. For item 2), the
12896 compiler could make sure that the divide instruction is not in
12897 the branch delay slot."
12898
12899 These processors have PRId values of 0x00004220 and 0x00004300 for
12900 the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400. */
12901
12902 const char *
12903 mips_output_division (const char *division, rtx *operands)
12904 {
12905 const char *s;
12906
12907 s = division;
12908 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
12909 {
12910 output_asm_insn (s, operands);
12911 s = "nop";
12912 }
12913 if (TARGET_CHECK_ZERO_DIV)
12914 {
12915 if (TARGET_MIPS16)
12916 {
12917 output_asm_insn (s, operands);
12918 s = "bnez\t%2,1f\n\tbreak\t7\n1:";
12919 }
12920 else if (GENERATE_DIVIDE_TRAPS)
12921 {
12922 /* Avoid long replay penalty on load miss by putting the trap before
12923 the divide. */
12924 if (TUNE_74K)
12925 output_asm_insn ("teq\t%2,%.,7", operands);
12926 else
12927 {
12928 output_asm_insn (s, operands);
12929 s = "teq\t%2,%.,7";
12930 }
12931 }
12932 else
12933 {
12934 output_asm_insn ("%(bne\t%2,%.,1f", operands);
12935 output_asm_insn (s, operands);
12936 s = "break\t7%)\n1:";
12937 }
12938 }
12939 return s;
12940 }
12941 \f
12942 /* Return true if IN_INSN is a multiply-add or multiply-subtract
12943 instruction and if OUT_INSN assigns to the accumulator operand. */
12944
12945 bool
12946 mips_linked_madd_p (rtx out_insn, rtx in_insn)
12947 {
12948 enum attr_accum_in accum_in;
12949 int accum_in_opnum;
12950 rtx accum_in_op;
12951
12952 if (recog_memoized (in_insn) < 0)
12953 return false;
12954
12955 accum_in = get_attr_accum_in (in_insn);
12956 if (accum_in == ACCUM_IN_NONE)
12957 return false;
12958
12959 accum_in_opnum = accum_in - ACCUM_IN_0;
12960
12961 extract_insn (in_insn);
12962 gcc_assert (accum_in_opnum < recog_data.n_operands);
12963 accum_in_op = recog_data.operand[accum_in_opnum];
12964
12965 return reg_set_p (accum_in_op, out_insn);
12966 }
12967
12968 /* True if the dependency between OUT_INSN and IN_INSN is on the store
12969 data rather than the address. We need this because the cprestore
12970 pattern is type "store", but is defined using an UNSPEC_VOLATILE,
12971 which causes the default routine to abort. We just return false
12972 for that case. */
12973
12974 bool
12975 mips_store_data_bypass_p (rtx out_insn, rtx in_insn)
12976 {
12977 if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
12978 return false;
12979
12980 return !store_data_bypass_p (out_insn, in_insn);
12981 }
12982 \f
12983
12984 /* Variables and flags used in scheduler hooks when tuning for
12985 Loongson 2E/2F. */
12986 static struct
12987 {
12988 /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
12989 strategy. */
12990
12991 /* If true, then next ALU1/2 instruction will go to ALU1. */
12992 bool alu1_turn_p;
12993
12994 /* If true, then next FALU1/2 unstruction will go to FALU1. */
12995 bool falu1_turn_p;
12996
12997 /* Codes to query if [f]alu{1,2}_core units are subscribed or not. */
12998 int alu1_core_unit_code;
12999 int alu2_core_unit_code;
13000 int falu1_core_unit_code;
13001 int falu2_core_unit_code;
13002
13003 /* True if current cycle has a multi instruction.
13004 This flag is used in mips_ls2_dfa_post_advance_cycle. */
13005 bool cycle_has_multi_p;
13006
13007 /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
13008 These are used in mips_ls2_dfa_post_advance_cycle to initialize
13009 DFA state.
13010 E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
13011 instruction to go ALU1. */
13012 rtx alu1_turn_enabled_insn;
13013 rtx alu2_turn_enabled_insn;
13014 rtx falu1_turn_enabled_insn;
13015 rtx falu2_turn_enabled_insn;
13016 } mips_ls2;
13017
13018 /* Implement TARGET_SCHED_ADJUST_COST. We assume that anti and output
13019 dependencies have no cost, except on the 20Kc where output-dependence
13020 is treated like input-dependence. */
13021
13022 static int
13023 mips_adjust_cost (rtx insn ATTRIBUTE_UNUSED, rtx link,
13024 rtx dep ATTRIBUTE_UNUSED, int cost)
13025 {
13026 if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
13027 && TUNE_20KC)
13028 return cost;
13029 if (REG_NOTE_KIND (link) != 0)
13030 return 0;
13031 return cost;
13032 }
13033
13034 /* Return the number of instructions that can be issued per cycle. */
13035
13036 static int
13037 mips_issue_rate (void)
13038 {
13039 switch (mips_tune)
13040 {
13041 case PROCESSOR_74KC:
13042 case PROCESSOR_74KF2_1:
13043 case PROCESSOR_74KF1_1:
13044 case PROCESSOR_74KF3_2:
13045 /* The 74k is not strictly quad-issue cpu, but can be seen as one
13046 by the scheduler. It can issue 1 ALU, 1 AGEN and 2 FPU insns,
13047 but in reality only a maximum of 3 insns can be issued as
13048 floating-point loads and stores also require a slot in the
13049 AGEN pipe. */
13050 case PROCESSOR_R10000:
13051 /* All R10K Processors are quad-issue (being the first MIPS
13052 processors to support this feature). */
13053 return 4;
13054
13055 case PROCESSOR_20KC:
13056 case PROCESSOR_R4130:
13057 case PROCESSOR_R5400:
13058 case PROCESSOR_R5500:
13059 case PROCESSOR_R5900:
13060 case PROCESSOR_R7000:
13061 case PROCESSOR_R9000:
13062 case PROCESSOR_OCTEON:
13063 case PROCESSOR_OCTEON2:
13064 return 2;
13065
13066 case PROCESSOR_SB1:
13067 case PROCESSOR_SB1A:
13068 /* This is actually 4, but we get better performance if we claim 3.
13069 This is partly because of unwanted speculative code motion with the
13070 larger number, and partly because in most common cases we can't
13071 reach the theoretical max of 4. */
13072 return 3;
13073
13074 case PROCESSOR_LOONGSON_2E:
13075 case PROCESSOR_LOONGSON_2F:
13076 case PROCESSOR_LOONGSON_3A:
13077 return 4;
13078
13079 case PROCESSOR_XLP:
13080 return (reload_completed ? 4 : 3);
13081
13082 default:
13083 return 1;
13084 }
13085 }
13086
13087 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2. */
13088
13089 static void
13090 mips_ls2_init_dfa_post_cycle_insn (void)
13091 {
13092 start_sequence ();
13093 emit_insn (gen_ls2_alu1_turn_enabled_insn ());
13094 mips_ls2.alu1_turn_enabled_insn = get_insns ();
13095 end_sequence ();
13096
13097 start_sequence ();
13098 emit_insn (gen_ls2_alu2_turn_enabled_insn ());
13099 mips_ls2.alu2_turn_enabled_insn = get_insns ();
13100 end_sequence ();
13101
13102 start_sequence ();
13103 emit_insn (gen_ls2_falu1_turn_enabled_insn ());
13104 mips_ls2.falu1_turn_enabled_insn = get_insns ();
13105 end_sequence ();
13106
13107 start_sequence ();
13108 emit_insn (gen_ls2_falu2_turn_enabled_insn ());
13109 mips_ls2.falu2_turn_enabled_insn = get_insns ();
13110 end_sequence ();
13111
13112 mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
13113 mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
13114 mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
13115 mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
13116 }
13117
13118 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
13119 Init data used in mips_dfa_post_advance_cycle. */
13120
13121 static void
13122 mips_init_dfa_post_cycle_insn (void)
13123 {
13124 if (TUNE_LOONGSON_2EF)
13125 mips_ls2_init_dfa_post_cycle_insn ();
13126 }
13127
13128 /* Initialize STATE when scheduling for Loongson 2E/2F.
13129 Support round-robin dispatch scheme by enabling only one of
13130 ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
13131 respectively. */
13132
13133 static void
13134 mips_ls2_dfa_post_advance_cycle (state_t state)
13135 {
13136 if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
13137 {
13138 /* Though there are no non-pipelined ALU1 insns,
13139 we can get an instruction of type 'multi' before reload. */
13140 gcc_assert (mips_ls2.cycle_has_multi_p);
13141 mips_ls2.alu1_turn_p = false;
13142 }
13143
13144 mips_ls2.cycle_has_multi_p = false;
13145
13146 if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
13147 /* We have a non-pipelined alu instruction in the core,
13148 adjust round-robin counter. */
13149 mips_ls2.alu1_turn_p = true;
13150
13151 if (mips_ls2.alu1_turn_p)
13152 {
13153 if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
13154 gcc_unreachable ();
13155 }
13156 else
13157 {
13158 if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
13159 gcc_unreachable ();
13160 }
13161
13162 if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
13163 {
13164 /* There are no non-pipelined FALU1 insns. */
13165 gcc_unreachable ();
13166 mips_ls2.falu1_turn_p = false;
13167 }
13168
13169 if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
13170 /* We have a non-pipelined falu instruction in the core,
13171 adjust round-robin counter. */
13172 mips_ls2.falu1_turn_p = true;
13173
13174 if (mips_ls2.falu1_turn_p)
13175 {
13176 if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
13177 gcc_unreachable ();
13178 }
13179 else
13180 {
13181 if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
13182 gcc_unreachable ();
13183 }
13184 }
13185
13186 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
13187 This hook is being called at the start of each cycle. */
13188
13189 static void
13190 mips_dfa_post_advance_cycle (void)
13191 {
13192 if (TUNE_LOONGSON_2EF)
13193 mips_ls2_dfa_post_advance_cycle (curr_state);
13194 }
13195
13196 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD. This should
13197 be as wide as the scheduling freedom in the DFA. */
13198
13199 static int
13200 mips_multipass_dfa_lookahead (void)
13201 {
13202 /* Can schedule up to 4 of the 6 function units in any one cycle. */
13203 if (TUNE_SB1)
13204 return 4;
13205
13206 if (TUNE_LOONGSON_2EF || TUNE_LOONGSON_3A)
13207 return 4;
13208
13209 if (TUNE_OCTEON)
13210 return 2;
13211
13212 return 0;
13213 }
13214 \f
13215 /* Remove the instruction at index LOWER from ready queue READY and
13216 reinsert it in front of the instruction at index HIGHER. LOWER must
13217 be <= HIGHER. */
13218
13219 static void
13220 mips_promote_ready (rtx *ready, int lower, int higher)
13221 {
13222 rtx new_head;
13223 int i;
13224
13225 new_head = ready[lower];
13226 for (i = lower; i < higher; i++)
13227 ready[i] = ready[i + 1];
13228 ready[i] = new_head;
13229 }
13230
13231 /* If the priority of the instruction at POS2 in the ready queue READY
13232 is within LIMIT units of that of the instruction at POS1, swap the
13233 instructions if POS2 is not already less than POS1. */
13234
13235 static void
13236 mips_maybe_swap_ready (rtx *ready, int pos1, int pos2, int limit)
13237 {
13238 if (pos1 < pos2
13239 && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
13240 {
13241 rtx temp;
13242
13243 temp = ready[pos1];
13244 ready[pos1] = ready[pos2];
13245 ready[pos2] = temp;
13246 }
13247 }
13248 \f
13249 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
13250 that may clobber hi or lo. */
13251 static rtx mips_macc_chains_last_hilo;
13252
13253 /* A TUNE_MACC_CHAINS helper function. Record that instruction INSN has
13254 been scheduled, updating mips_macc_chains_last_hilo appropriately. */
13255
13256 static void
13257 mips_macc_chains_record (rtx insn)
13258 {
13259 if (get_attr_may_clobber_hilo (insn))
13260 mips_macc_chains_last_hilo = insn;
13261 }
13262
13263 /* A TUNE_MACC_CHAINS helper function. Search ready queue READY, which
13264 has NREADY elements, looking for a multiply-add or multiply-subtract
13265 instruction that is cumulative with mips_macc_chains_last_hilo.
13266 If there is one, promote it ahead of anything else that might
13267 clobber hi or lo. */
13268
13269 static void
13270 mips_macc_chains_reorder (rtx *ready, int nready)
13271 {
13272 int i, j;
13273
13274 if (mips_macc_chains_last_hilo != 0)
13275 for (i = nready - 1; i >= 0; i--)
13276 if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
13277 {
13278 for (j = nready - 1; j > i; j--)
13279 if (recog_memoized (ready[j]) >= 0
13280 && get_attr_may_clobber_hilo (ready[j]))
13281 {
13282 mips_promote_ready (ready, i, j);
13283 break;
13284 }
13285 break;
13286 }
13287 }
13288 \f
13289 /* The last instruction to be scheduled. */
13290 static rtx vr4130_last_insn;
13291
13292 /* A note_stores callback used by vr4130_true_reg_dependence_p. DATA
13293 points to an rtx that is initially an instruction. Nullify the rtx
13294 if the instruction uses the value of register X. */
13295
13296 static void
13297 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
13298 void *data)
13299 {
13300 rtx *insn_ptr;
13301
13302 insn_ptr = (rtx *) data;
13303 if (REG_P (x)
13304 && *insn_ptr != 0
13305 && reg_referenced_p (x, PATTERN (*insn_ptr)))
13306 *insn_ptr = 0;
13307 }
13308
13309 /* Return true if there is true register dependence between vr4130_last_insn
13310 and INSN. */
13311
13312 static bool
13313 vr4130_true_reg_dependence_p (rtx insn)
13314 {
13315 note_stores (PATTERN (vr4130_last_insn),
13316 vr4130_true_reg_dependence_p_1, &insn);
13317 return insn == 0;
13318 }
13319
13320 /* A TUNE_MIPS4130 helper function. Given that INSN1 is at the head of
13321 the ready queue and that INSN2 is the instruction after it, return
13322 true if it is worth promoting INSN2 ahead of INSN1. Look for cases
13323 in which INSN1 and INSN2 can probably issue in parallel, but for
13324 which (INSN2, INSN1) should be less sensitive to instruction
13325 alignment than (INSN1, INSN2). See 4130.md for more details. */
13326
13327 static bool
13328 vr4130_swap_insns_p (rtx insn1, rtx insn2)
13329 {
13330 sd_iterator_def sd_it;
13331 dep_t dep;
13332
13333 /* Check for the following case:
13334
13335 1) there is some other instruction X with an anti dependence on INSN1;
13336 2) X has a higher priority than INSN2; and
13337 3) X is an arithmetic instruction (and thus has no unit restrictions).
13338
13339 If INSN1 is the last instruction blocking X, it would better to
13340 choose (INSN1, X) over (INSN2, INSN1). */
13341 FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
13342 if (DEP_TYPE (dep) == REG_DEP_ANTI
13343 && INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
13344 && recog_memoized (DEP_CON (dep)) >= 0
13345 && get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
13346 return false;
13347
13348 if (vr4130_last_insn != 0
13349 && recog_memoized (insn1) >= 0
13350 && recog_memoized (insn2) >= 0)
13351 {
13352 /* See whether INSN1 and INSN2 use different execution units,
13353 or if they are both ALU-type instructions. If so, they can
13354 probably execute in parallel. */
13355 enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
13356 enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
13357 if (class1 != class2 || class1 == VR4130_CLASS_ALU)
13358 {
13359 /* If only one of the instructions has a dependence on
13360 vr4130_last_insn, prefer to schedule the other one first. */
13361 bool dep1_p = vr4130_true_reg_dependence_p (insn1);
13362 bool dep2_p = vr4130_true_reg_dependence_p (insn2);
13363 if (dep1_p != dep2_p)
13364 return dep1_p;
13365
13366 /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
13367 is not an ALU-type instruction and if INSN1 uses the same
13368 execution unit. (Note that if this condition holds, we already
13369 know that INSN2 uses a different execution unit.) */
13370 if (class1 != VR4130_CLASS_ALU
13371 && recog_memoized (vr4130_last_insn) >= 0
13372 && class1 == get_attr_vr4130_class (vr4130_last_insn))
13373 return true;
13374 }
13375 }
13376 return false;
13377 }
13378
13379 /* A TUNE_MIPS4130 helper function. (READY, NREADY) describes a ready
13380 queue with at least two instructions. Swap the first two if
13381 vr4130_swap_insns_p says that it could be worthwhile. */
13382
13383 static void
13384 vr4130_reorder (rtx *ready, int nready)
13385 {
13386 if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
13387 mips_promote_ready (ready, nready - 2, nready - 1);
13388 }
13389 \f
13390 /* Record whether last 74k AGEN instruction was a load or store. */
13391 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
13392
13393 /* Initialize mips_last_74k_agen_insn from INSN. A null argument
13394 resets to TYPE_UNKNOWN state. */
13395
13396 static void
13397 mips_74k_agen_init (rtx insn)
13398 {
13399 if (!insn || CALL_P (insn) || JUMP_P (insn))
13400 mips_last_74k_agen_insn = TYPE_UNKNOWN;
13401 else
13402 {
13403 enum attr_type type = get_attr_type (insn);
13404 if (type == TYPE_LOAD || type == TYPE_STORE)
13405 mips_last_74k_agen_insn = type;
13406 }
13407 }
13408
13409 /* A TUNE_74K helper function. The 74K AGEN pipeline likes multiple
13410 loads to be grouped together, and multiple stores to be grouped
13411 together. Swap things around in the ready queue to make this happen. */
13412
13413 static void
13414 mips_74k_agen_reorder (rtx *ready, int nready)
13415 {
13416 int i;
13417 int store_pos, load_pos;
13418
13419 store_pos = -1;
13420 load_pos = -1;
13421
13422 for (i = nready - 1; i >= 0; i--)
13423 {
13424 rtx insn = ready[i];
13425 if (USEFUL_INSN_P (insn))
13426 switch (get_attr_type (insn))
13427 {
13428 case TYPE_STORE:
13429 if (store_pos == -1)
13430 store_pos = i;
13431 break;
13432
13433 case TYPE_LOAD:
13434 if (load_pos == -1)
13435 load_pos = i;
13436 break;
13437
13438 default:
13439 break;
13440 }
13441 }
13442
13443 if (load_pos == -1 || store_pos == -1)
13444 return;
13445
13446 switch (mips_last_74k_agen_insn)
13447 {
13448 case TYPE_UNKNOWN:
13449 /* Prefer to schedule loads since they have a higher latency. */
13450 case TYPE_LOAD:
13451 /* Swap loads to the front of the queue. */
13452 mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
13453 break;
13454 case TYPE_STORE:
13455 /* Swap stores to the front of the queue. */
13456 mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
13457 break;
13458 default:
13459 break;
13460 }
13461 }
13462 \f
13463 /* Implement TARGET_SCHED_INIT. */
13464
13465 static void
13466 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13467 int max_ready ATTRIBUTE_UNUSED)
13468 {
13469 mips_macc_chains_last_hilo = 0;
13470 vr4130_last_insn = 0;
13471 mips_74k_agen_init (NULL_RTX);
13472
13473 /* When scheduling for Loongson2, branch instructions go to ALU1,
13474 therefore basic block is most likely to start with round-robin counter
13475 pointed to ALU2. */
13476 mips_ls2.alu1_turn_p = false;
13477 mips_ls2.falu1_turn_p = true;
13478 }
13479
13480 /* Subroutine used by TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2. */
13481
13482 static void
13483 mips_sched_reorder_1 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13484 rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13485 {
13486 if (!reload_completed
13487 && TUNE_MACC_CHAINS
13488 && *nreadyp > 0)
13489 mips_macc_chains_reorder (ready, *nreadyp);
13490
13491 if (reload_completed
13492 && TUNE_MIPS4130
13493 && !TARGET_VR4130_ALIGN
13494 && *nreadyp > 1)
13495 vr4130_reorder (ready, *nreadyp);
13496
13497 if (TUNE_74K)
13498 mips_74k_agen_reorder (ready, *nreadyp);
13499 }
13500
13501 /* Implement TARGET_SCHED_REORDER. */
13502
13503 static int
13504 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13505 rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13506 {
13507 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13508 return mips_issue_rate ();
13509 }
13510
13511 /* Implement TARGET_SCHED_REORDER2. */
13512
13513 static int
13514 mips_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13515 rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13516 {
13517 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13518 return cached_can_issue_more;
13519 }
13520
13521 /* Update round-robin counters for ALU1/2 and FALU1/2. */
13522
13523 static void
13524 mips_ls2_variable_issue (rtx insn)
13525 {
13526 if (mips_ls2.alu1_turn_p)
13527 {
13528 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
13529 mips_ls2.alu1_turn_p = false;
13530 }
13531 else
13532 {
13533 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
13534 mips_ls2.alu1_turn_p = true;
13535 }
13536
13537 if (mips_ls2.falu1_turn_p)
13538 {
13539 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
13540 mips_ls2.falu1_turn_p = false;
13541 }
13542 else
13543 {
13544 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
13545 mips_ls2.falu1_turn_p = true;
13546 }
13547
13548 if (recog_memoized (insn) >= 0)
13549 mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
13550 }
13551
13552 /* Implement TARGET_SCHED_VARIABLE_ISSUE. */
13553
13554 static int
13555 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13556 rtx insn, int more)
13557 {
13558 /* Ignore USEs and CLOBBERs; don't count them against the issue rate. */
13559 if (USEFUL_INSN_P (insn))
13560 {
13561 if (get_attr_type (insn) != TYPE_GHOST)
13562 more--;
13563 if (!reload_completed && TUNE_MACC_CHAINS)
13564 mips_macc_chains_record (insn);
13565 vr4130_last_insn = insn;
13566 if (TUNE_74K)
13567 mips_74k_agen_init (insn);
13568 else if (TUNE_LOONGSON_2EF)
13569 mips_ls2_variable_issue (insn);
13570 }
13571
13572 /* Instructions of type 'multi' should all be split before
13573 the second scheduling pass. */
13574 gcc_assert (!reload_completed
13575 || recog_memoized (insn) < 0
13576 || get_attr_type (insn) != TYPE_MULTI);
13577
13578 cached_can_issue_more = more;
13579 return more;
13580 }
13581 \f
13582 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
13583 return the first operand of the associated PREF or PREFX insn. */
13584
13585 rtx
13586 mips_prefetch_cookie (rtx write, rtx locality)
13587 {
13588 /* store_streamed / load_streamed. */
13589 if (INTVAL (locality) <= 0)
13590 return GEN_INT (INTVAL (write) + 4);
13591
13592 /* store / load. */
13593 if (INTVAL (locality) <= 2)
13594 return write;
13595
13596 /* store_retained / load_retained. */
13597 return GEN_INT (INTVAL (write) + 6);
13598 }
13599 \f
13600 /* Flags that indicate when a built-in function is available.
13601
13602 BUILTIN_AVAIL_NON_MIPS16
13603 The function is available on the current target, but only
13604 in non-MIPS16 mode. */
13605 #define BUILTIN_AVAIL_NON_MIPS16 1
13606
13607 /* Declare an availability predicate for built-in functions that
13608 require non-MIPS16 mode and also require COND to be true.
13609 NAME is the main part of the predicate's name. */
13610 #define AVAIL_NON_MIPS16(NAME, COND) \
13611 static unsigned int \
13612 mips_builtin_avail_##NAME (void) \
13613 { \
13614 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0; \
13615 }
13616
13617 /* This structure describes a single built-in function. */
13618 struct mips_builtin_description {
13619 /* The code of the main .md file instruction. See mips_builtin_type
13620 for more information. */
13621 enum insn_code icode;
13622
13623 /* The floating-point comparison code to use with ICODE, if any. */
13624 enum mips_fp_condition cond;
13625
13626 /* The name of the built-in function. */
13627 const char *name;
13628
13629 /* Specifies how the function should be expanded. */
13630 enum mips_builtin_type builtin_type;
13631
13632 /* The function's prototype. */
13633 enum mips_function_type function_type;
13634
13635 /* Whether the function is available. */
13636 unsigned int (*avail) (void);
13637 };
13638
13639 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
13640 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
13641 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
13642 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
13643 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
13644 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
13645 AVAIL_NON_MIPS16 (dsp_64, TARGET_64BIT && TARGET_DSP)
13646 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
13647 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_VECTORS)
13648 AVAIL_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
13649
13650 /* Construct a mips_builtin_description from the given arguments.
13651
13652 INSN is the name of the associated instruction pattern, without the
13653 leading CODE_FOR_mips_.
13654
13655 CODE is the floating-point condition code associated with the
13656 function. It can be 'f' if the field is not applicable.
13657
13658 NAME is the name of the function itself, without the leading
13659 "__builtin_mips_".
13660
13661 BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
13662
13663 AVAIL is the name of the availability predicate, without the leading
13664 mips_builtin_avail_. */
13665 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE, \
13666 FUNCTION_TYPE, AVAIL) \
13667 { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND, \
13668 "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE, \
13669 mips_builtin_avail_ ## AVAIL }
13670
13671 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
13672 mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE and AVAIL
13673 are as for MIPS_BUILTIN. */
13674 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
13675 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
13676
13677 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
13678 are subject to mips_builtin_avail_<AVAIL>. */
13679 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL) \
13680 MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s", \
13681 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL), \
13682 MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d", \
13683 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL)
13684
13685 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
13686 The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
13687 while the any and all forms are subject to mips_builtin_avail_mips3d. */
13688 #define CMP_PS_BUILTINS(INSN, COND, AVAIL) \
13689 MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps", \
13690 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF, \
13691 mips3d), \
13692 MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps", \
13693 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF, \
13694 mips3d), \
13695 MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps", \
13696 MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF, \
13697 AVAIL), \
13698 MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps", \
13699 MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF, \
13700 AVAIL)
13701
13702 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s. The functions
13703 are subject to mips_builtin_avail_mips3d. */
13704 #define CMP_4S_BUILTINS(INSN, COND) \
13705 MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s", \
13706 MIPS_BUILTIN_CMP_ANY, \
13707 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d), \
13708 MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s", \
13709 MIPS_BUILTIN_CMP_ALL, \
13710 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d)
13711
13712 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps. The comparison
13713 instruction requires mips_builtin_avail_<AVAIL>. */
13714 #define MOVTF_BUILTINS(INSN, COND, AVAIL) \
13715 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps", \
13716 MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13717 AVAIL), \
13718 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps", \
13719 MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13720 AVAIL)
13721
13722 /* Define all the built-in functions related to C.cond.fmt condition COND. */
13723 #define CMP_BUILTINS(COND) \
13724 MOVTF_BUILTINS (c, COND, paired_single), \
13725 MOVTF_BUILTINS (cabs, COND, mips3d), \
13726 CMP_SCALAR_BUILTINS (cabs, COND, mips3d), \
13727 CMP_PS_BUILTINS (c, COND, paired_single), \
13728 CMP_PS_BUILTINS (cabs, COND, mips3d), \
13729 CMP_4S_BUILTINS (c, COND), \
13730 CMP_4S_BUILTINS (cabs, COND)
13731
13732 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
13733 function mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE
13734 and AVAIL are as for MIPS_BUILTIN. */
13735 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
13736 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT_NO_TARGET, \
13737 FUNCTION_TYPE, AVAIL)
13738
13739 /* Define __builtin_mips_bposge<VALUE>. <VALUE> is 32 for the MIPS32 DSP
13740 branch instruction. AVAIL is as for MIPS_BUILTIN. */
13741 #define BPOSGE_BUILTIN(VALUE, AVAIL) \
13742 MIPS_BUILTIN (bposge, f, "bposge" #VALUE, \
13743 MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL)
13744
13745 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
13746 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
13747 builtin_description field. */
13748 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE) \
13749 { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f, \
13750 "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT, \
13751 FUNCTION_TYPE, mips_builtin_avail_loongson }
13752
13753 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
13754 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
13755 builtin_description field. */
13756 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE) \
13757 LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
13758
13759 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
13760 We use functions of this form when the same insn can be usefully applied
13761 to more than one datatype. */
13762 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE) \
13763 LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
13764
13765 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
13766 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
13767 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
13768 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
13769 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
13770 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
13771 #define CODE_FOR_mips_mult CODE_FOR_mulsidi3_32bit
13772 #define CODE_FOR_mips_multu CODE_FOR_umulsidi3_32bit
13773
13774 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
13775 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
13776 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
13777 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
13778 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
13779 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
13780 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
13781 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
13782 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
13783 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
13784 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
13785 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
13786 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
13787 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
13788 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
13789 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
13790 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
13791 #define CODE_FOR_loongson_psllh CODE_FOR_ashlv4hi3
13792 #define CODE_FOR_loongson_psllw CODE_FOR_ashlv2si3
13793 #define CODE_FOR_loongson_psrlh CODE_FOR_lshrv4hi3
13794 #define CODE_FOR_loongson_psrlw CODE_FOR_lshrv2si3
13795 #define CODE_FOR_loongson_psrah CODE_FOR_ashrv4hi3
13796 #define CODE_FOR_loongson_psraw CODE_FOR_ashrv2si3
13797 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
13798 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
13799 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
13800 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
13801 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
13802 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
13803 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
13804
13805 static const struct mips_builtin_description mips_builtins[] = {
13806 DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13807 DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13808 DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13809 DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13810 DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
13811 DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
13812 DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
13813 DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
13814
13815 DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
13816 DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13817 DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13818 DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13819 DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
13820
13821 DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
13822 DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
13823 DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13824 DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13825 DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13826 DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13827
13828 DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
13829 DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
13830 DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13831 DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13832 DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13833 DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13834
13835 MIPS_FP_CONDITIONS (CMP_BUILTINS),
13836
13837 /* Built-in functions for the SB-1 processor. */
13838 DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
13839
13840 /* Built-in functions for the DSP ASE (32-bit and 64-bit). */
13841 DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13842 DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13843 DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13844 DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13845 DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13846 DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13847 DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13848 DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13849 DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13850 DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13851 DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
13852 DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
13853 DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
13854 DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
13855 DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
13856 DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
13857 DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13858 DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13859 DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13860 DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13861 DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
13862 DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
13863 DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
13864 DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
13865 DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
13866 DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
13867 DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
13868 DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
13869 DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
13870 DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
13871 DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
13872 DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13873 DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13874 DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13875 DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
13876 DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13877 DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
13878 DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
13879 DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
13880 DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
13881 DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13882 DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
13883 DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
13884 DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, dsp),
13885 DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, dsp),
13886 DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
13887 DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
13888 DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13889 DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13890 DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
13891 DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13892 DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13893 DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
13894 DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13895 DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13896 DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
13897 DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13898 DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13899 DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13900 DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
13901 DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, dsp),
13902 DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
13903 DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
13904 DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
13905 BPOSGE_BUILTIN (32, dsp),
13906
13907 /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit). */
13908 DIRECT_BUILTIN (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
13909 DIRECT_BUILTIN (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13910 DIRECT_BUILTIN (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13911 DIRECT_BUILTIN (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13912 DIRECT_BUILTIN (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13913 DIRECT_BUILTIN (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13914 DIRECT_BUILTIN (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13915 DIRECT_BUILTIN (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13916 DIRECT_BUILTIN (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13917 DIRECT_BUILTIN (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
13918 DIRECT_BUILTIN (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13919 DIRECT_BUILTIN (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13920 DIRECT_BUILTIN (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13921 DIRECT_BUILTIN (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13922 DIRECT_BUILTIN (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13923 DIRECT_BUILTIN (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
13924 DIRECT_BUILTIN (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
13925 DIRECT_BUILTIN (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
13926 DIRECT_BUILTIN (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
13927 DIRECT_BUILTIN (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
13928 DIRECT_BUILTIN (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
13929 DIRECT_BUILTIN (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
13930 DIRECT_BUILTIN (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13931 DIRECT_BUILTIN (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13932 DIRECT_BUILTIN (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13933 DIRECT_BUILTIN (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
13934 DIRECT_BUILTIN (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13935 DIRECT_BUILTIN (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13936 DIRECT_BUILTIN (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13937 DIRECT_BUILTIN (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13938 DIRECT_BUILTIN (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13939 DIRECT_BUILTIN (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
13940 DIRECT_BUILTIN (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13941 DIRECT_BUILTIN (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
13942
13943 /* Built-in functions for the DSP ASE (32-bit only). */
13944 DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13945 DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13946 DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13947 DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
13948 DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13949 DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13950 DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13951 DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13952 DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13953 DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13954 DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13955 DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13956 DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
13957 DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13958 DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13959 DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
13960 DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
13961 DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
13962 DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
13963 DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
13964 DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
13965 DIRECT_BUILTIN (madd, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13966 DIRECT_BUILTIN (maddu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
13967 DIRECT_BUILTIN (msub, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
13968 DIRECT_BUILTIN (msubu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
13969 DIRECT_BUILTIN (mult, MIPS_DI_FTYPE_SI_SI, dsp_32),
13970 DIRECT_BUILTIN (multu, MIPS_DI_FTYPE_USI_USI, dsp_32),
13971
13972 /* Built-in functions for the DSP ASE (64-bit only). */
13973 DIRECT_BUILTIN (ldx, MIPS_DI_FTYPE_POINTER_SI, dsp_64),
13974
13975 /* The following are for the MIPS DSP ASE REV 2 (32-bit only). */
13976 DIRECT_BUILTIN (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13977 DIRECT_BUILTIN (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13978 DIRECT_BUILTIN (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13979 DIRECT_BUILTIN (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13980 DIRECT_BUILTIN (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13981 DIRECT_BUILTIN (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13982 DIRECT_BUILTIN (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13983 DIRECT_BUILTIN (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13984 DIRECT_BUILTIN (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
13985
13986 /* Builtin functions for ST Microelectronics Loongson-2E/2F cores. */
13987 LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
13988 LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
13989 LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
13990 LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
13991 LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
13992 LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
13993 LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
13994 LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
13995 LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
13996 LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
13997 LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
13998 LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
13999 LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14000 LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14001 LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14002 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
14003 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14004 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14005 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14006 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
14007 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
14008 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14009 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14010 LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14011 LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14012 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14013 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14014 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14015 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14016 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14017 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14018 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14019 LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14020 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14021 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14022 LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14023 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14024 LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
14025 LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
14026 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14027 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14028 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14029 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14030 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14031 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14032 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14033 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14034 LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
14035 LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14036 LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14037 LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14038 LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14039 LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
14040 LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
14041 LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14042 LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14043 LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14044 LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
14045 LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14046 LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
14047 LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
14048 LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14049 LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14050 LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14051 LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14052 LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14053 LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14054 LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14055 LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14056 LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14057 LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14058 LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14059 LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14060 LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14061 LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14062 LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14063 LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14064 LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14065 LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14066 LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14067 LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14068 LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
14069 LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
14070 LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14071 LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14072 LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14073 LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14074 LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14075 LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14076 LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14077 LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14078 LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14079 LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14080 LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14081 LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14082 LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14083 LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14084 LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14085 LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14086
14087 /* Sundry other built-in functions. */
14088 DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache)
14089 };
14090
14091 /* Index I is the function declaration for mips_builtins[I], or null if the
14092 function isn't defined on this target. */
14093 static GTY(()) tree mips_builtin_decls[ARRAY_SIZE (mips_builtins)];
14094
14095 /* MODE is a vector mode whose elements have type TYPE. Return the type
14096 of the vector itself. */
14097
14098 static tree
14099 mips_builtin_vector_type (tree type, enum machine_mode mode)
14100 {
14101 static tree types[2 * (int) MAX_MACHINE_MODE];
14102 int mode_index;
14103
14104 mode_index = (int) mode;
14105
14106 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
14107 mode_index += MAX_MACHINE_MODE;
14108
14109 if (types[mode_index] == NULL_TREE)
14110 types[mode_index] = build_vector_type_for_mode (type, mode);
14111 return types[mode_index];
14112 }
14113
14114 /* Return a type for 'const volatile void *'. */
14115
14116 static tree
14117 mips_build_cvpointer_type (void)
14118 {
14119 static tree cache;
14120
14121 if (cache == NULL_TREE)
14122 cache = build_pointer_type (build_qualified_type
14123 (void_type_node,
14124 TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
14125 return cache;
14126 }
14127
14128 /* Source-level argument types. */
14129 #define MIPS_ATYPE_VOID void_type_node
14130 #define MIPS_ATYPE_INT integer_type_node
14131 #define MIPS_ATYPE_POINTER ptr_type_node
14132 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
14133
14134 /* Standard mode-based argument types. */
14135 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
14136 #define MIPS_ATYPE_SI intSI_type_node
14137 #define MIPS_ATYPE_USI unsigned_intSI_type_node
14138 #define MIPS_ATYPE_DI intDI_type_node
14139 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
14140 #define MIPS_ATYPE_SF float_type_node
14141 #define MIPS_ATYPE_DF double_type_node
14142
14143 /* Vector argument types. */
14144 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
14145 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
14146 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
14147 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
14148 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
14149 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
14150 #define MIPS_ATYPE_UV2SI \
14151 mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
14152 #define MIPS_ATYPE_UV4HI \
14153 mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
14154 #define MIPS_ATYPE_UV8QI \
14155 mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
14156
14157 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
14158 their associated MIPS_ATYPEs. */
14159 #define MIPS_FTYPE_ATYPES1(A, B) \
14160 MIPS_ATYPE_##A, MIPS_ATYPE_##B
14161
14162 #define MIPS_FTYPE_ATYPES2(A, B, C) \
14163 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
14164
14165 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
14166 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
14167
14168 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
14169 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
14170 MIPS_ATYPE_##E
14171
14172 /* Return the function type associated with function prototype TYPE. */
14173
14174 static tree
14175 mips_build_function_type (enum mips_function_type type)
14176 {
14177 static tree types[(int) MIPS_MAX_FTYPE_MAX];
14178
14179 if (types[(int) type] == NULL_TREE)
14180 switch (type)
14181 {
14182 #define DEF_MIPS_FTYPE(NUM, ARGS) \
14183 case MIPS_FTYPE_NAME##NUM ARGS: \
14184 types[(int) type] \
14185 = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS, \
14186 NULL_TREE); \
14187 break;
14188 #include "config/mips/mips-ftypes.def"
14189 #undef DEF_MIPS_FTYPE
14190 default:
14191 gcc_unreachable ();
14192 }
14193
14194 return types[(int) type];
14195 }
14196
14197 /* Implement TARGET_INIT_BUILTINS. */
14198
14199 static void
14200 mips_init_builtins (void)
14201 {
14202 const struct mips_builtin_description *d;
14203 unsigned int i;
14204
14205 /* Iterate through all of the bdesc arrays, initializing all of the
14206 builtin functions. */
14207 for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
14208 {
14209 d = &mips_builtins[i];
14210 if (d->avail ())
14211 mips_builtin_decls[i]
14212 = add_builtin_function (d->name,
14213 mips_build_function_type (d->function_type),
14214 i, BUILT_IN_MD, NULL, NULL);
14215 }
14216 }
14217
14218 /* Implement TARGET_BUILTIN_DECL. */
14219
14220 static tree
14221 mips_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
14222 {
14223 if (code >= ARRAY_SIZE (mips_builtins))
14224 return error_mark_node;
14225 return mips_builtin_decls[code];
14226 }
14227
14228 /* Take argument ARGNO from EXP's argument list and convert it into
14229 an expand operand. Store the operand in *OP. */
14230
14231 static void
14232 mips_prepare_builtin_arg (struct expand_operand *op, tree exp,
14233 unsigned int argno)
14234 {
14235 tree arg;
14236 rtx value;
14237
14238 arg = CALL_EXPR_ARG (exp, argno);
14239 value = expand_normal (arg);
14240 create_input_operand (op, value, TYPE_MODE (TREE_TYPE (arg)));
14241 }
14242
14243 /* Expand instruction ICODE as part of a built-in function sequence.
14244 Use the first NOPS elements of OPS as the instruction's operands.
14245 HAS_TARGET_P is true if operand 0 is a target; it is false if the
14246 instruction has no target.
14247
14248 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */
14249
14250 static rtx
14251 mips_expand_builtin_insn (enum insn_code icode, unsigned int nops,
14252 struct expand_operand *ops, bool has_target_p)
14253 {
14254 if (!maybe_expand_insn (icode, nops, ops))
14255 {
14256 error ("invalid argument to built-in function");
14257 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
14258 }
14259 return has_target_p ? ops[0].value : const0_rtx;
14260 }
14261
14262 /* Expand a floating-point comparison for built-in function call EXP.
14263 The first NARGS arguments are the values to be compared. ICODE is
14264 the .md pattern that does the comparison and COND is the condition
14265 that is being tested. Return an rtx for the result. */
14266
14267 static rtx
14268 mips_expand_builtin_compare_1 (enum insn_code icode,
14269 enum mips_fp_condition cond,
14270 tree exp, int nargs)
14271 {
14272 struct expand_operand ops[MAX_RECOG_OPERANDS];
14273 rtx output;
14274 int opno, argno;
14275
14276 /* The instruction should have a target operand, an operand for each
14277 argument, and an operand for COND. */
14278 gcc_assert (nargs + 2 == insn_data[(int) icode].n_generator_args);
14279
14280 output = mips_allocate_fcc (insn_data[(int) icode].operand[0].mode);
14281 opno = 0;
14282 create_fixed_operand (&ops[opno++], output);
14283 for (argno = 0; argno < nargs; argno++)
14284 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14285 create_integer_operand (&ops[opno++], (int) cond);
14286 return mips_expand_builtin_insn (icode, opno, ops, true);
14287 }
14288
14289 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
14290 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
14291 and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
14292 suggests a good place to put the result. */
14293
14294 static rtx
14295 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
14296 bool has_target_p)
14297 {
14298 struct expand_operand ops[MAX_RECOG_OPERANDS];
14299 int opno, argno;
14300
14301 /* Map any target to operand 0. */
14302 opno = 0;
14303 if (has_target_p)
14304 create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
14305
14306 /* Map the arguments to the other operands. */
14307 gcc_assert (opno + call_expr_nargs (exp)
14308 == insn_data[icode].n_generator_args);
14309 for (argno = 0; argno < call_expr_nargs (exp); argno++)
14310 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14311
14312 return mips_expand_builtin_insn (icode, opno, ops, has_target_p);
14313 }
14314
14315 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
14316 function; TYPE says which. EXP is the CALL_EXPR that calls the
14317 function, ICODE is the instruction that should be used to compare
14318 the first two arguments, and COND is the condition it should test.
14319 TARGET, if nonnull, suggests a good place to put the result. */
14320
14321 static rtx
14322 mips_expand_builtin_movtf (enum mips_builtin_type type,
14323 enum insn_code icode, enum mips_fp_condition cond,
14324 rtx target, tree exp)
14325 {
14326 struct expand_operand ops[4];
14327 rtx cmp_result;
14328
14329 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp, 2);
14330 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
14331 if (type == MIPS_BUILTIN_MOVT)
14332 {
14333 mips_prepare_builtin_arg (&ops[2], exp, 2);
14334 mips_prepare_builtin_arg (&ops[1], exp, 3);
14335 }
14336 else
14337 {
14338 mips_prepare_builtin_arg (&ops[1], exp, 2);
14339 mips_prepare_builtin_arg (&ops[2], exp, 3);
14340 }
14341 create_fixed_operand (&ops[3], cmp_result);
14342 return mips_expand_builtin_insn (CODE_FOR_mips_cond_move_tf_ps,
14343 4, ops, true);
14344 }
14345
14346 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
14347 into TARGET otherwise. Return TARGET. */
14348
14349 static rtx
14350 mips_builtin_branch_and_move (rtx condition, rtx target,
14351 rtx value_if_true, rtx value_if_false)
14352 {
14353 rtx true_label, done_label;
14354
14355 true_label = gen_label_rtx ();
14356 done_label = gen_label_rtx ();
14357
14358 /* First assume that CONDITION is false. */
14359 mips_emit_move (target, value_if_false);
14360
14361 /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise. */
14362 emit_jump_insn (gen_condjump (condition, true_label));
14363 emit_jump_insn (gen_jump (done_label));
14364 emit_barrier ();
14365
14366 /* Fix TARGET if CONDITION is true. */
14367 emit_label (true_label);
14368 mips_emit_move (target, value_if_true);
14369
14370 emit_label (done_label);
14371 return target;
14372 }
14373
14374 /* Expand a comparison built-in function of type BUILTIN_TYPE. EXP is
14375 the CALL_EXPR that calls the function, ICODE is the code of the
14376 comparison instruction, and COND is the condition it should test.
14377 TARGET, if nonnull, suggests a good place to put the boolean result. */
14378
14379 static rtx
14380 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
14381 enum insn_code icode, enum mips_fp_condition cond,
14382 rtx target, tree exp)
14383 {
14384 rtx offset, condition, cmp_result;
14385
14386 if (target == 0 || GET_MODE (target) != SImode)
14387 target = gen_reg_rtx (SImode);
14388 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp,
14389 call_expr_nargs (exp));
14390
14391 /* If the comparison sets more than one register, we define the result
14392 to be 0 if all registers are false and -1 if all registers are true.
14393 The value of the complete result is indeterminate otherwise. */
14394 switch (builtin_type)
14395 {
14396 case MIPS_BUILTIN_CMP_ALL:
14397 condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
14398 return mips_builtin_branch_and_move (condition, target,
14399 const0_rtx, const1_rtx);
14400
14401 case MIPS_BUILTIN_CMP_UPPER:
14402 case MIPS_BUILTIN_CMP_LOWER:
14403 offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
14404 condition = gen_single_cc (cmp_result, offset);
14405 return mips_builtin_branch_and_move (condition, target,
14406 const1_rtx, const0_rtx);
14407
14408 default:
14409 condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
14410 return mips_builtin_branch_and_move (condition, target,
14411 const1_rtx, const0_rtx);
14412 }
14413 }
14414
14415 /* Expand a bposge built-in function of type BUILTIN_TYPE. TARGET,
14416 if nonnull, suggests a good place to put the boolean result. */
14417
14418 static rtx
14419 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
14420 {
14421 rtx condition, cmp_result;
14422 int cmp_value;
14423
14424 if (target == 0 || GET_MODE (target) != SImode)
14425 target = gen_reg_rtx (SImode);
14426
14427 cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
14428
14429 if (builtin_type == MIPS_BUILTIN_BPOSGE32)
14430 cmp_value = 32;
14431 else
14432 gcc_assert (0);
14433
14434 condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
14435 return mips_builtin_branch_and_move (condition, target,
14436 const1_rtx, const0_rtx);
14437 }
14438
14439 /* Implement TARGET_EXPAND_BUILTIN. */
14440
14441 static rtx
14442 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
14443 enum machine_mode mode, int ignore)
14444 {
14445 tree fndecl;
14446 unsigned int fcode, avail;
14447 const struct mips_builtin_description *d;
14448
14449 fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
14450 fcode = DECL_FUNCTION_CODE (fndecl);
14451 gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
14452 d = &mips_builtins[fcode];
14453 avail = d->avail ();
14454 gcc_assert (avail != 0);
14455 if (TARGET_MIPS16)
14456 {
14457 error ("built-in function %qE not supported for MIPS16",
14458 DECL_NAME (fndecl));
14459 return ignore ? const0_rtx : CONST0_RTX (mode);
14460 }
14461 switch (d->builtin_type)
14462 {
14463 case MIPS_BUILTIN_DIRECT:
14464 return mips_expand_builtin_direct (d->icode, target, exp, true);
14465
14466 case MIPS_BUILTIN_DIRECT_NO_TARGET:
14467 return mips_expand_builtin_direct (d->icode, target, exp, false);
14468
14469 case MIPS_BUILTIN_MOVT:
14470 case MIPS_BUILTIN_MOVF:
14471 return mips_expand_builtin_movtf (d->builtin_type, d->icode,
14472 d->cond, target, exp);
14473
14474 case MIPS_BUILTIN_CMP_ANY:
14475 case MIPS_BUILTIN_CMP_ALL:
14476 case MIPS_BUILTIN_CMP_UPPER:
14477 case MIPS_BUILTIN_CMP_LOWER:
14478 case MIPS_BUILTIN_CMP_SINGLE:
14479 return mips_expand_builtin_compare (d->builtin_type, d->icode,
14480 d->cond, target, exp);
14481
14482 case MIPS_BUILTIN_BPOSGE32:
14483 return mips_expand_builtin_bposge (d->builtin_type, target);
14484 }
14485 gcc_unreachable ();
14486 }
14487 \f
14488 /* An entry in the MIPS16 constant pool. VALUE is the pool constant,
14489 MODE is its mode, and LABEL is the CODE_LABEL associated with it. */
14490 struct mips16_constant {
14491 struct mips16_constant *next;
14492 rtx value;
14493 rtx label;
14494 enum machine_mode mode;
14495 };
14496
14497 /* Information about an incomplete MIPS16 constant pool. FIRST is the
14498 first constant, HIGHEST_ADDRESS is the highest address that the first
14499 byte of the pool can have, and INSN_ADDRESS is the current instruction
14500 address. */
14501 struct mips16_constant_pool {
14502 struct mips16_constant *first;
14503 int highest_address;
14504 int insn_address;
14505 };
14506
14507 /* Add constant VALUE to POOL and return its label. MODE is the
14508 value's mode (used for CONST_INTs, etc.). */
14509
14510 static rtx
14511 mips16_add_constant (struct mips16_constant_pool *pool,
14512 rtx value, enum machine_mode mode)
14513 {
14514 struct mips16_constant **p, *c;
14515 bool first_of_size_p;
14516
14517 /* See whether the constant is already in the pool. If so, return the
14518 existing label, otherwise leave P pointing to the place where the
14519 constant should be added.
14520
14521 Keep the pool sorted in increasing order of mode size so that we can
14522 reduce the number of alignments needed. */
14523 first_of_size_p = true;
14524 for (p = &pool->first; *p != 0; p = &(*p)->next)
14525 {
14526 if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
14527 return (*p)->label;
14528 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
14529 break;
14530 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
14531 first_of_size_p = false;
14532 }
14533
14534 /* In the worst case, the constant needed by the earliest instruction
14535 will end up at the end of the pool. The entire pool must then be
14536 accessible from that instruction.
14537
14538 When adding the first constant, set the pool's highest address to
14539 the address of the first out-of-range byte. Adjust this address
14540 downwards each time a new constant is added. */
14541 if (pool->first == 0)
14542 /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
14543 of the instruction with the lowest two bits clear. The base PC
14544 value for LDPC has the lowest three bits clear. Assume the worst
14545 case here; namely that the PC-relative instruction occupies the
14546 last 2 bytes in an aligned word. */
14547 pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
14548 pool->highest_address -= GET_MODE_SIZE (mode);
14549 if (first_of_size_p)
14550 /* Take into account the worst possible padding due to alignment. */
14551 pool->highest_address -= GET_MODE_SIZE (mode) - 1;
14552
14553 /* Create a new entry. */
14554 c = XNEW (struct mips16_constant);
14555 c->value = value;
14556 c->mode = mode;
14557 c->label = gen_label_rtx ();
14558 c->next = *p;
14559 *p = c;
14560
14561 return c->label;
14562 }
14563
14564 /* Output constant VALUE after instruction INSN and return the last
14565 instruction emitted. MODE is the mode of the constant. */
14566
14567 static rtx
14568 mips16_emit_constants_1 (enum machine_mode mode, rtx value, rtx insn)
14569 {
14570 if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
14571 {
14572 rtx size = GEN_INT (GET_MODE_SIZE (mode));
14573 return emit_insn_after (gen_consttable_int (value, size), insn);
14574 }
14575
14576 if (SCALAR_FLOAT_MODE_P (mode))
14577 return emit_insn_after (gen_consttable_float (value), insn);
14578
14579 if (VECTOR_MODE_P (mode))
14580 {
14581 int i;
14582
14583 for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
14584 insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
14585 CONST_VECTOR_ELT (value, i), insn);
14586 return insn;
14587 }
14588
14589 gcc_unreachable ();
14590 }
14591
14592 /* Dump out the constants in CONSTANTS after INSN. */
14593
14594 static void
14595 mips16_emit_constants (struct mips16_constant *constants, rtx insn)
14596 {
14597 struct mips16_constant *c, *next;
14598 int align;
14599
14600 align = 0;
14601 for (c = constants; c != NULL; c = next)
14602 {
14603 /* If necessary, increase the alignment of PC. */
14604 if (align < GET_MODE_SIZE (c->mode))
14605 {
14606 int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
14607 insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
14608 }
14609 align = GET_MODE_SIZE (c->mode);
14610
14611 insn = emit_label_after (c->label, insn);
14612 insn = mips16_emit_constants_1 (c->mode, c->value, insn);
14613
14614 next = c->next;
14615 free (c);
14616 }
14617
14618 emit_barrier_after (insn);
14619 }
14620
14621 /* Return the length of instruction INSN. */
14622
14623 static int
14624 mips16_insn_length (rtx insn)
14625 {
14626 if (JUMP_TABLE_DATA_P (insn))
14627 {
14628 rtx body = PATTERN (insn);
14629 if (GET_CODE (body) == ADDR_VEC)
14630 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
14631 else if (GET_CODE (body) == ADDR_DIFF_VEC)
14632 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
14633 else
14634 gcc_unreachable ();
14635 }
14636 return get_attr_length (insn);
14637 }
14638
14639 /* If *X is a symbolic constant that refers to the constant pool, add
14640 the constant to POOL and rewrite *X to use the constant's label. */
14641
14642 static void
14643 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
14644 {
14645 rtx base, offset, label;
14646
14647 split_const (*x, &base, &offset);
14648 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
14649 {
14650 label = mips16_add_constant (pool, copy_rtx (get_pool_constant (base)),
14651 get_pool_mode (base));
14652 base = gen_rtx_LABEL_REF (Pmode, label);
14653 *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
14654 }
14655 }
14656
14657 /* This structure is used to communicate with mips16_rewrite_pool_refs.
14658 INSN is the instruction we're rewriting and POOL points to the current
14659 constant pool. */
14660 struct mips16_rewrite_pool_refs_info {
14661 rtx insn;
14662 struct mips16_constant_pool *pool;
14663 };
14664
14665 /* Rewrite *X so that constant pool references refer to the constant's
14666 label instead. DATA points to a mips16_rewrite_pool_refs_info
14667 structure. */
14668
14669 static int
14670 mips16_rewrite_pool_refs (rtx *x, void *data)
14671 {
14672 struct mips16_rewrite_pool_refs_info *info =
14673 (struct mips16_rewrite_pool_refs_info *) data;
14674
14675 if (force_to_mem_operand (*x, Pmode))
14676 {
14677 rtx mem = force_const_mem (GET_MODE (*x), *x);
14678 validate_change (info->insn, x, mem, false);
14679 }
14680
14681 if (MEM_P (*x))
14682 {
14683 mips16_rewrite_pool_constant (info->pool, &XEXP (*x, 0));
14684 return -1;
14685 }
14686
14687 /* Don't rewrite the __mips16_rdwr symbol. */
14688 if (GET_CODE (*x) == UNSPEC && XINT (*x, 1) == UNSPEC_TLS_GET_TP)
14689 return -1;
14690
14691 if (TARGET_MIPS16_TEXT_LOADS)
14692 mips16_rewrite_pool_constant (info->pool, x);
14693
14694 return GET_CODE (*x) == CONST ? -1 : 0;
14695 }
14696
14697 /* Return whether CFG is used in mips_reorg. */
14698
14699 static bool
14700 mips_cfg_in_reorg (void)
14701 {
14702 return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
14703 || TARGET_RELAX_PIC_CALLS);
14704 }
14705
14706 /* Build MIPS16 constant pools. Split the instructions if SPLIT_P,
14707 otherwise assume that they are already split. */
14708
14709 static void
14710 mips16_lay_out_constants (bool split_p)
14711 {
14712 struct mips16_constant_pool pool;
14713 struct mips16_rewrite_pool_refs_info info;
14714 rtx insn, barrier;
14715
14716 if (!TARGET_MIPS16_PCREL_LOADS)
14717 return;
14718
14719 if (split_p)
14720 {
14721 if (mips_cfg_in_reorg ())
14722 split_all_insns ();
14723 else
14724 split_all_insns_noflow ();
14725 }
14726 barrier = 0;
14727 memset (&pool, 0, sizeof (pool));
14728 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14729 {
14730 /* Rewrite constant pool references in INSN. */
14731 if (USEFUL_INSN_P (insn))
14732 {
14733 info.insn = insn;
14734 info.pool = &pool;
14735 for_each_rtx (&PATTERN (insn), mips16_rewrite_pool_refs, &info);
14736 }
14737
14738 pool.insn_address += mips16_insn_length (insn);
14739
14740 if (pool.first != NULL)
14741 {
14742 /* If there are no natural barriers between the first user of
14743 the pool and the highest acceptable address, we'll need to
14744 create a new instruction to jump around the constant pool.
14745 In the worst case, this instruction will be 4 bytes long.
14746
14747 If it's too late to do this transformation after INSN,
14748 do it immediately before INSN. */
14749 if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
14750 {
14751 rtx label, jump;
14752
14753 label = gen_label_rtx ();
14754
14755 jump = emit_jump_insn_before (gen_jump (label), insn);
14756 JUMP_LABEL (jump) = label;
14757 LABEL_NUSES (label) = 1;
14758 barrier = emit_barrier_after (jump);
14759
14760 emit_label_after (label, barrier);
14761 pool.insn_address += 4;
14762 }
14763
14764 /* See whether the constant pool is now out of range of the first
14765 user. If so, output the constants after the previous barrier.
14766 Note that any instructions between BARRIER and INSN (inclusive)
14767 will use negative offsets to refer to the pool. */
14768 if (pool.insn_address > pool.highest_address)
14769 {
14770 mips16_emit_constants (pool.first, barrier);
14771 pool.first = NULL;
14772 barrier = 0;
14773 }
14774 else if (BARRIER_P (insn))
14775 barrier = insn;
14776 }
14777 }
14778 mips16_emit_constants (pool.first, get_last_insn ());
14779 }
14780 \f
14781 /* Return true if it is worth r10k_simplify_address's while replacing
14782 an address with X. We are looking for constants, and for addresses
14783 at a known offset from the incoming stack pointer. */
14784
14785 static bool
14786 r10k_simplified_address_p (rtx x)
14787 {
14788 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
14789 x = XEXP (x, 0);
14790 return x == virtual_incoming_args_rtx || CONSTANT_P (x);
14791 }
14792
14793 /* X is an expression that appears in INSN. Try to use the UD chains
14794 to simplify it, returning the simplified form on success and the
14795 original form otherwise. Replace the incoming value of $sp with
14796 virtual_incoming_args_rtx (which should never occur in X otherwise). */
14797
14798 static rtx
14799 r10k_simplify_address (rtx x, rtx insn)
14800 {
14801 rtx newx, op0, op1, set, def_insn, note;
14802 df_ref use, def;
14803 struct df_link *defs;
14804
14805 newx = NULL_RTX;
14806 if (UNARY_P (x))
14807 {
14808 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14809 if (op0 != XEXP (x, 0))
14810 newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
14811 op0, GET_MODE (XEXP (x, 0)));
14812 }
14813 else if (BINARY_P (x))
14814 {
14815 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14816 op1 = r10k_simplify_address (XEXP (x, 1), insn);
14817 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
14818 newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
14819 }
14820 else if (GET_CODE (x) == LO_SUM)
14821 {
14822 /* LO_SUMs can be offset from HIGHs, if we know they won't
14823 overflow. See mips_classify_address for the rationale behind
14824 the lax check. */
14825 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14826 if (GET_CODE (op0) == HIGH)
14827 newx = XEXP (x, 1);
14828 }
14829 else if (REG_P (x))
14830 {
14831 /* Uses are recorded by regno_reg_rtx, not X itself. */
14832 use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
14833 gcc_assert (use);
14834 defs = DF_REF_CHAIN (use);
14835
14836 /* Require a single definition. */
14837 if (defs && defs->next == NULL)
14838 {
14839 def = defs->ref;
14840 if (DF_REF_IS_ARTIFICIAL (def))
14841 {
14842 /* Replace the incoming value of $sp with
14843 virtual_incoming_args_rtx. */
14844 if (x == stack_pointer_rtx
14845 && DF_REF_BB (def) == ENTRY_BLOCK_PTR)
14846 newx = virtual_incoming_args_rtx;
14847 }
14848 else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
14849 DF_REF_BB (def)))
14850 {
14851 /* Make sure that DEF_INSN is a single set of REG. */
14852 def_insn = DF_REF_INSN (def);
14853 if (NONJUMP_INSN_P (def_insn))
14854 {
14855 set = single_set (def_insn);
14856 if (set && rtx_equal_p (SET_DEST (set), x))
14857 {
14858 /* Prefer to use notes, since the def-use chains
14859 are often shorter. */
14860 note = find_reg_equal_equiv_note (def_insn);
14861 if (note)
14862 newx = XEXP (note, 0);
14863 else
14864 newx = SET_SRC (set);
14865 newx = r10k_simplify_address (newx, def_insn);
14866 }
14867 }
14868 }
14869 }
14870 }
14871 if (newx && r10k_simplified_address_p (newx))
14872 return newx;
14873 return x;
14874 }
14875
14876 /* Return true if ADDRESS is known to be an uncached address
14877 on R10K systems. */
14878
14879 static bool
14880 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
14881 {
14882 unsigned HOST_WIDE_INT upper;
14883
14884 /* Check for KSEG1. */
14885 if (address + 0x60000000 < 0x20000000)
14886 return true;
14887
14888 /* Check for uncached XKPHYS addresses. */
14889 if (Pmode == DImode)
14890 {
14891 upper = (address >> 40) & 0xf9ffff;
14892 if (upper == 0x900000 || upper == 0xb80000)
14893 return true;
14894 }
14895 return false;
14896 }
14897
14898 /* Return true if we can prove that an access to address X in instruction
14899 INSN would be safe from R10K speculation. This X is a general
14900 expression; it might not be a legitimate address. */
14901
14902 static bool
14903 r10k_safe_address_p (rtx x, rtx insn)
14904 {
14905 rtx base, offset;
14906 HOST_WIDE_INT offset_val;
14907
14908 x = r10k_simplify_address (x, insn);
14909
14910 /* Check for references to the stack frame. It doesn't really matter
14911 how much of the frame has been allocated at INSN; -mr10k-cache-barrier
14912 allows us to assume that accesses to any part of the eventual frame
14913 is safe from speculation at any point in the function. */
14914 mips_split_plus (x, &base, &offset_val);
14915 if (base == virtual_incoming_args_rtx
14916 && offset_val >= -cfun->machine->frame.total_size
14917 && offset_val < cfun->machine->frame.args_size)
14918 return true;
14919
14920 /* Check for uncached addresses. */
14921 if (CONST_INT_P (x))
14922 return r10k_uncached_address_p (INTVAL (x));
14923
14924 /* Check for accesses to a static object. */
14925 split_const (x, &base, &offset);
14926 return offset_within_block_p (base, INTVAL (offset));
14927 }
14928
14929 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
14930 an in-range access to an automatic variable, or to an object with
14931 a link-time-constant address. */
14932
14933 static bool
14934 r10k_safe_mem_expr_p (tree expr, unsigned HOST_WIDE_INT offset)
14935 {
14936 HOST_WIDE_INT bitoffset, bitsize;
14937 tree inner, var_offset;
14938 enum machine_mode mode;
14939 int unsigned_p, volatile_p;
14940
14941 inner = get_inner_reference (expr, &bitsize, &bitoffset, &var_offset, &mode,
14942 &unsigned_p, &volatile_p, false);
14943 if (!DECL_P (inner) || !DECL_SIZE_UNIT (inner) || var_offset)
14944 return false;
14945
14946 offset += bitoffset / BITS_PER_UNIT;
14947 return offset < tree_to_uhwi (DECL_SIZE_UNIT (inner));
14948 }
14949
14950 /* A for_each_rtx callback for which DATA points to the instruction
14951 containing *X. Stop the search if we find a MEM that is not safe
14952 from R10K speculation. */
14953
14954 static int
14955 r10k_needs_protection_p_1 (rtx *loc, void *data)
14956 {
14957 rtx mem;
14958
14959 mem = *loc;
14960 if (!MEM_P (mem))
14961 return 0;
14962
14963 if (MEM_EXPR (mem)
14964 && MEM_OFFSET_KNOWN_P (mem)
14965 && r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
14966 return -1;
14967
14968 if (r10k_safe_address_p (XEXP (mem, 0), (rtx) data))
14969 return -1;
14970
14971 return 1;
14972 }
14973
14974 /* A note_stores callback for which DATA points to an instruction pointer.
14975 If *DATA is nonnull, make it null if it X contains a MEM that is not
14976 safe from R10K speculation. */
14977
14978 static void
14979 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
14980 void *data)
14981 {
14982 rtx *insn_ptr;
14983
14984 insn_ptr = (rtx *) data;
14985 if (*insn_ptr && for_each_rtx (&x, r10k_needs_protection_p_1, *insn_ptr))
14986 *insn_ptr = NULL_RTX;
14987 }
14988
14989 /* A for_each_rtx callback that iterates over the pattern of a CALL_INSN.
14990 Return nonzero if the call is not to a declared function. */
14991
14992 static int
14993 r10k_needs_protection_p_call (rtx *loc, void *data ATTRIBUTE_UNUSED)
14994 {
14995 rtx x;
14996
14997 x = *loc;
14998 if (!MEM_P (x))
14999 return 0;
15000
15001 x = XEXP (x, 0);
15002 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DECL (x))
15003 return -1;
15004
15005 return 1;
15006 }
15007
15008 /* Return true if instruction INSN needs to be protected by an R10K
15009 cache barrier. */
15010
15011 static bool
15012 r10k_needs_protection_p (rtx insn)
15013 {
15014 if (CALL_P (insn))
15015 return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_call, NULL);
15016
15017 if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
15018 {
15019 note_stores (PATTERN (insn), r10k_needs_protection_p_store, &insn);
15020 return insn == NULL_RTX;
15021 }
15022
15023 return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_1, insn);
15024 }
15025
15026 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
15027 edge is unconditional. */
15028
15029 static bool
15030 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
15031 {
15032 edge_iterator ei;
15033 edge e;
15034
15035 FOR_EACH_EDGE (e, ei, bb->preds)
15036 if (!single_succ_p (e->src)
15037 || !bitmap_bit_p (protected_bbs, e->src->index)
15038 || (e->flags & EDGE_COMPLEX) != 0)
15039 return false;
15040 return true;
15041 }
15042
15043 /* Implement -mr10k-cache-barrier= for the current function. */
15044
15045 static void
15046 r10k_insert_cache_barriers (void)
15047 {
15048 int *rev_post_order;
15049 unsigned int i, n;
15050 basic_block bb;
15051 sbitmap protected_bbs;
15052 rtx insn, end, unprotected_region;
15053
15054 if (TARGET_MIPS16)
15055 {
15056 sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
15057 return;
15058 }
15059
15060 /* Calculate dominators. */
15061 calculate_dominance_info (CDI_DOMINATORS);
15062
15063 /* Bit X of PROTECTED_BBS is set if the last operation in basic block
15064 X is protected by a cache barrier. */
15065 protected_bbs = sbitmap_alloc (last_basic_block);
15066 bitmap_clear (protected_bbs);
15067
15068 /* Iterate over the basic blocks in reverse post-order. */
15069 rev_post_order = XNEWVEC (int, last_basic_block);
15070 n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
15071 for (i = 0; i < n; i++)
15072 {
15073 bb = BASIC_BLOCK (rev_post_order[i]);
15074
15075 /* If this block is only reached by unconditional edges, and if the
15076 source of every edge is protected, the beginning of the block is
15077 also protected. */
15078 if (r10k_protected_bb_p (bb, protected_bbs))
15079 unprotected_region = NULL_RTX;
15080 else
15081 unprotected_region = pc_rtx;
15082 end = NEXT_INSN (BB_END (bb));
15083
15084 /* UNPROTECTED_REGION is:
15085
15086 - null if we are processing a protected region,
15087 - pc_rtx if we are processing an unprotected region but have
15088 not yet found the first instruction in it
15089 - the first instruction in an unprotected region otherwise. */
15090 for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
15091 {
15092 if (unprotected_region && USEFUL_INSN_P (insn))
15093 {
15094 if (recog_memoized (insn) == CODE_FOR_mips_cache)
15095 /* This CACHE instruction protects the following code. */
15096 unprotected_region = NULL_RTX;
15097 else
15098 {
15099 /* See if INSN is the first instruction in this
15100 unprotected region. */
15101 if (unprotected_region == pc_rtx)
15102 unprotected_region = insn;
15103
15104 /* See if INSN needs to be protected. If so,
15105 we must insert a cache barrier somewhere between
15106 PREV_INSN (UNPROTECTED_REGION) and INSN. It isn't
15107 clear which position is better performance-wise,
15108 but as a tie-breaker, we assume that it is better
15109 to allow delay slots to be back-filled where
15110 possible, and that it is better not to insert
15111 barriers in the middle of already-scheduled code.
15112 We therefore insert the barrier at the beginning
15113 of the region. */
15114 if (r10k_needs_protection_p (insn))
15115 {
15116 emit_insn_before (gen_r10k_cache_barrier (),
15117 unprotected_region);
15118 unprotected_region = NULL_RTX;
15119 }
15120 }
15121 }
15122
15123 if (CALL_P (insn))
15124 /* The called function is not required to protect the exit path.
15125 The code that follows a call is therefore unprotected. */
15126 unprotected_region = pc_rtx;
15127 }
15128
15129 /* Record whether the end of this block is protected. */
15130 if (unprotected_region == NULL_RTX)
15131 bitmap_set_bit (protected_bbs, bb->index);
15132 }
15133 XDELETEVEC (rev_post_order);
15134
15135 sbitmap_free (protected_bbs);
15136
15137 free_dominance_info (CDI_DOMINATORS);
15138 }
15139 \f
15140 /* If INSN is a call, return the underlying CALL expr. Return NULL_RTX
15141 otherwise. If INSN has two call rtx, then store the second one in
15142 SECOND_CALL. */
15143
15144 static rtx
15145 mips_call_expr_from_insn (rtx insn, rtx *second_call)
15146 {
15147 rtx x;
15148 rtx x2;
15149
15150 if (!CALL_P (insn))
15151 return NULL_RTX;
15152
15153 x = PATTERN (insn);
15154 if (GET_CODE (x) == PARALLEL)
15155 {
15156 /* Calls returning complex values have two CALL rtx. Look for the second
15157 one here, and return it via the SECOND_CALL arg. */
15158 x2 = XVECEXP (x, 0, 1);
15159 if (GET_CODE (x2) == SET)
15160 x2 = XEXP (x2, 1);
15161 if (GET_CODE (x2) == CALL)
15162 *second_call = x2;
15163
15164 x = XVECEXP (x, 0, 0);
15165 }
15166 if (GET_CODE (x) == SET)
15167 x = XEXP (x, 1);
15168 gcc_assert (GET_CODE (x) == CALL);
15169
15170 return x;
15171 }
15172
15173 /* REG is set in DEF. See if the definition is one of the ways we load a
15174 register with a symbol address for a mips_use_pic_fn_addr_reg_p call.
15175 If it is, return the symbol reference of the function, otherwise return
15176 NULL_RTX.
15177
15178 If RECURSE_P is true, use mips_find_pic_call_symbol to interpret
15179 the values of source registers, otherwise treat such registers as
15180 having an unknown value. */
15181
15182 static rtx
15183 mips_pic_call_symbol_from_set (df_ref def, rtx reg, bool recurse_p)
15184 {
15185 rtx def_insn, set;
15186
15187 if (DF_REF_IS_ARTIFICIAL (def))
15188 return NULL_RTX;
15189
15190 def_insn = DF_REF_INSN (def);
15191 set = single_set (def_insn);
15192 if (set && rtx_equal_p (SET_DEST (set), reg))
15193 {
15194 rtx note, src, symbol;
15195
15196 /* First see whether the source is a plain symbol. This is used
15197 when calling symbols that are not lazily bound. */
15198 src = SET_SRC (set);
15199 if (GET_CODE (src) == SYMBOL_REF)
15200 return src;
15201
15202 /* Handle %call16 references. */
15203 symbol = mips_strip_unspec_call (src);
15204 if (symbol)
15205 {
15206 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15207 return symbol;
15208 }
15209
15210 /* If we have something more complicated, look for a
15211 REG_EQUAL or REG_EQUIV note. */
15212 note = find_reg_equal_equiv_note (def_insn);
15213 if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
15214 return XEXP (note, 0);
15215
15216 /* Follow at most one simple register copy. Such copies are
15217 interesting in cases like:
15218
15219 for (...)
15220 {
15221 locally_binding_fn (...);
15222 }
15223
15224 and:
15225
15226 locally_binding_fn (...);
15227 ...
15228 locally_binding_fn (...);
15229
15230 where the load of locally_binding_fn can legitimately be
15231 hoisted or shared. However, we do not expect to see complex
15232 chains of copies, so a full worklist solution to the problem
15233 would probably be overkill. */
15234 if (recurse_p && REG_P (src))
15235 return mips_find_pic_call_symbol (def_insn, src, false);
15236 }
15237
15238 return NULL_RTX;
15239 }
15240
15241 /* Find the definition of the use of REG in INSN. See if the definition
15242 is one of the ways we load a register with a symbol address for a
15243 mips_use_pic_fn_addr_reg_p call. If it is return the symbol reference
15244 of the function, otherwise return NULL_RTX. RECURSE_P is as for
15245 mips_pic_call_symbol_from_set. */
15246
15247 static rtx
15248 mips_find_pic_call_symbol (rtx insn, rtx reg, bool recurse_p)
15249 {
15250 df_ref use;
15251 struct df_link *defs;
15252 rtx symbol;
15253
15254 use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
15255 if (!use)
15256 return NULL_RTX;
15257 defs = DF_REF_CHAIN (use);
15258 if (!defs)
15259 return NULL_RTX;
15260 symbol = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15261 if (!symbol)
15262 return NULL_RTX;
15263
15264 /* If we have more than one definition, they need to be identical. */
15265 for (defs = defs->next; defs; defs = defs->next)
15266 {
15267 rtx other;
15268
15269 other = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15270 if (!rtx_equal_p (symbol, other))
15271 return NULL_RTX;
15272 }
15273
15274 return symbol;
15275 }
15276
15277 /* Replace the args_size operand of the call expression CALL with the
15278 call-attribute UNSPEC and fill in SYMBOL as the function symbol. */
15279
15280 static void
15281 mips_annotate_pic_call_expr (rtx call, rtx symbol)
15282 {
15283 rtx args_size;
15284
15285 args_size = XEXP (call, 1);
15286 XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
15287 gen_rtvec (2, args_size, symbol),
15288 UNSPEC_CALL_ATTR);
15289 }
15290
15291 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression. See
15292 if instead of the arg_size argument it contains the call attributes. If
15293 yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
15294 symbol from the call attributes. Also return false if ARGS_SIZE_OPNO is
15295 -1. */
15296
15297 bool
15298 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
15299 {
15300 rtx args_size, symbol;
15301
15302 if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
15303 return false;
15304
15305 args_size = operands[args_size_opno];
15306 if (GET_CODE (args_size) != UNSPEC)
15307 return false;
15308 gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
15309
15310 symbol = XVECEXP (args_size, 0, 1);
15311 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15312
15313 operands[args_size_opno] = symbol;
15314 return true;
15315 }
15316
15317 /* Use DF to annotate PIC indirect calls with the function symbol they
15318 dispatch to. */
15319
15320 static void
15321 mips_annotate_pic_calls (void)
15322 {
15323 basic_block bb;
15324 rtx insn;
15325
15326 FOR_EACH_BB (bb)
15327 FOR_BB_INSNS (bb, insn)
15328 {
15329 rtx call, reg, symbol, second_call;
15330
15331 second_call = 0;
15332 call = mips_call_expr_from_insn (insn, &second_call);
15333 if (!call)
15334 continue;
15335 gcc_assert (MEM_P (XEXP (call, 0)));
15336 reg = XEXP (XEXP (call, 0), 0);
15337 if (!REG_P (reg))
15338 continue;
15339
15340 symbol = mips_find_pic_call_symbol (insn, reg, true);
15341 if (symbol)
15342 {
15343 mips_annotate_pic_call_expr (call, symbol);
15344 if (second_call)
15345 mips_annotate_pic_call_expr (second_call, symbol);
15346 }
15347 }
15348 }
15349 \f
15350 /* A temporary variable used by for_each_rtx callbacks, etc. */
15351 static rtx mips_sim_insn;
15352
15353 /* A structure representing the state of the processor pipeline.
15354 Used by the mips_sim_* family of functions. */
15355 struct mips_sim {
15356 /* The maximum number of instructions that can be issued in a cycle.
15357 (Caches mips_issue_rate.) */
15358 unsigned int issue_rate;
15359
15360 /* The current simulation time. */
15361 unsigned int time;
15362
15363 /* How many more instructions can be issued in the current cycle. */
15364 unsigned int insns_left;
15365
15366 /* LAST_SET[X].INSN is the last instruction to set register X.
15367 LAST_SET[X].TIME is the time at which that instruction was issued.
15368 INSN is null if no instruction has yet set register X. */
15369 struct {
15370 rtx insn;
15371 unsigned int time;
15372 } last_set[FIRST_PSEUDO_REGISTER];
15373
15374 /* The pipeline's current DFA state. */
15375 state_t dfa_state;
15376 };
15377
15378 /* Reset STATE to the initial simulation state. */
15379
15380 static void
15381 mips_sim_reset (struct mips_sim *state)
15382 {
15383 curr_state = state->dfa_state;
15384
15385 state->time = 0;
15386 state->insns_left = state->issue_rate;
15387 memset (&state->last_set, 0, sizeof (state->last_set));
15388 state_reset (curr_state);
15389
15390 targetm.sched.init (0, false, 0);
15391 advance_state (curr_state);
15392 }
15393
15394 /* Initialize STATE before its first use. DFA_STATE points to an
15395 allocated but uninitialized DFA state. */
15396
15397 static void
15398 mips_sim_init (struct mips_sim *state, state_t dfa_state)
15399 {
15400 if (targetm.sched.init_dfa_pre_cycle_insn)
15401 targetm.sched.init_dfa_pre_cycle_insn ();
15402
15403 if (targetm.sched.init_dfa_post_cycle_insn)
15404 targetm.sched.init_dfa_post_cycle_insn ();
15405
15406 state->issue_rate = mips_issue_rate ();
15407 state->dfa_state = dfa_state;
15408 mips_sim_reset (state);
15409 }
15410
15411 /* Advance STATE by one clock cycle. */
15412
15413 static void
15414 mips_sim_next_cycle (struct mips_sim *state)
15415 {
15416 curr_state = state->dfa_state;
15417
15418 state->time++;
15419 state->insns_left = state->issue_rate;
15420 advance_state (curr_state);
15421 }
15422
15423 /* Advance simulation state STATE until instruction INSN can read
15424 register REG. */
15425
15426 static void
15427 mips_sim_wait_reg (struct mips_sim *state, rtx insn, rtx reg)
15428 {
15429 unsigned int regno, end_regno;
15430
15431 end_regno = END_REGNO (reg);
15432 for (regno = REGNO (reg); regno < end_regno; regno++)
15433 if (state->last_set[regno].insn != 0)
15434 {
15435 unsigned int t;
15436
15437 t = (state->last_set[regno].time
15438 + insn_latency (state->last_set[regno].insn, insn));
15439 while (state->time < t)
15440 mips_sim_next_cycle (state);
15441 }
15442 }
15443
15444 /* A for_each_rtx callback. If *X is a register, advance simulation state
15445 DATA until mips_sim_insn can read the register's value. */
15446
15447 static int
15448 mips_sim_wait_regs_2 (rtx *x, void *data)
15449 {
15450 if (REG_P (*x))
15451 mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *x);
15452 return 0;
15453 }
15454
15455 /* Call mips_sim_wait_regs_2 (R, DATA) for each register R mentioned in *X. */
15456
15457 static void
15458 mips_sim_wait_regs_1 (rtx *x, void *data)
15459 {
15460 for_each_rtx (x, mips_sim_wait_regs_2, data);
15461 }
15462
15463 /* Advance simulation state STATE until all of INSN's register
15464 dependencies are satisfied. */
15465
15466 static void
15467 mips_sim_wait_regs (struct mips_sim *state, rtx insn)
15468 {
15469 mips_sim_insn = insn;
15470 note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
15471 }
15472
15473 /* Advance simulation state STATE until the units required by
15474 instruction INSN are available. */
15475
15476 static void
15477 mips_sim_wait_units (struct mips_sim *state, rtx insn)
15478 {
15479 state_t tmp_state;
15480
15481 tmp_state = alloca (state_size ());
15482 while (state->insns_left == 0
15483 || (memcpy (tmp_state, state->dfa_state, state_size ()),
15484 state_transition (tmp_state, insn) >= 0))
15485 mips_sim_next_cycle (state);
15486 }
15487
15488 /* Advance simulation state STATE until INSN is ready to issue. */
15489
15490 static void
15491 mips_sim_wait_insn (struct mips_sim *state, rtx insn)
15492 {
15493 mips_sim_wait_regs (state, insn);
15494 mips_sim_wait_units (state, insn);
15495 }
15496
15497 /* mips_sim_insn has just set X. Update the LAST_SET array
15498 in simulation state DATA. */
15499
15500 static void
15501 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
15502 {
15503 struct mips_sim *state;
15504
15505 state = (struct mips_sim *) data;
15506 if (REG_P (x))
15507 {
15508 unsigned int regno, end_regno;
15509
15510 end_regno = END_REGNO (x);
15511 for (regno = REGNO (x); regno < end_regno; regno++)
15512 {
15513 state->last_set[regno].insn = mips_sim_insn;
15514 state->last_set[regno].time = state->time;
15515 }
15516 }
15517 }
15518
15519 /* Issue instruction INSN in scheduler state STATE. Assume that INSN
15520 can issue immediately (i.e., that mips_sim_wait_insn has already
15521 been called). */
15522
15523 static void
15524 mips_sim_issue_insn (struct mips_sim *state, rtx insn)
15525 {
15526 curr_state = state->dfa_state;
15527
15528 state_transition (curr_state, insn);
15529 state->insns_left = targetm.sched.variable_issue (0, false, insn,
15530 state->insns_left);
15531
15532 mips_sim_insn = insn;
15533 note_stores (PATTERN (insn), mips_sim_record_set, state);
15534 }
15535
15536 /* Simulate issuing a NOP in state STATE. */
15537
15538 static void
15539 mips_sim_issue_nop (struct mips_sim *state)
15540 {
15541 if (state->insns_left == 0)
15542 mips_sim_next_cycle (state);
15543 state->insns_left--;
15544 }
15545
15546 /* Update simulation state STATE so that it's ready to accept the instruction
15547 after INSN. INSN should be part of the main rtl chain, not a member of a
15548 SEQUENCE. */
15549
15550 static void
15551 mips_sim_finish_insn (struct mips_sim *state, rtx insn)
15552 {
15553 /* If INSN is a jump with an implicit delay slot, simulate a nop. */
15554 if (JUMP_P (insn))
15555 mips_sim_issue_nop (state);
15556
15557 switch (GET_CODE (SEQ_BEGIN (insn)))
15558 {
15559 case CODE_LABEL:
15560 case CALL_INSN:
15561 /* We can't predict the processor state after a call or label. */
15562 mips_sim_reset (state);
15563 break;
15564
15565 case JUMP_INSN:
15566 /* The delay slots of branch likely instructions are only executed
15567 when the branch is taken. Therefore, if the caller has simulated
15568 the delay slot instruction, STATE does not really reflect the state
15569 of the pipeline for the instruction after the delay slot. Also,
15570 branch likely instructions tend to incur a penalty when not taken,
15571 so there will probably be an extra delay between the branch and
15572 the instruction after the delay slot. */
15573 if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
15574 mips_sim_reset (state);
15575 break;
15576
15577 default:
15578 break;
15579 }
15580 }
15581
15582 /* Use simulator state STATE to calculate the execution time of
15583 instruction sequence SEQ. */
15584
15585 static unsigned int
15586 mips_seq_time (struct mips_sim *state, rtx seq)
15587 {
15588 mips_sim_reset (state);
15589 for (rtx insn = seq; insn; insn = NEXT_INSN (insn))
15590 {
15591 mips_sim_wait_insn (state, insn);
15592 mips_sim_issue_insn (state, insn);
15593 }
15594 return state->time;
15595 }
15596 \f
15597 /* Return the execution-time cost of mips_tuning_info.fast_mult_zero_zero_p
15598 setting SETTING, using STATE to simulate instruction sequences. */
15599
15600 static unsigned int
15601 mips_mult_zero_zero_cost (struct mips_sim *state, bool setting)
15602 {
15603 mips_tuning_info.fast_mult_zero_zero_p = setting;
15604 start_sequence ();
15605
15606 enum machine_mode dword_mode = TARGET_64BIT ? TImode : DImode;
15607 rtx hilo = gen_rtx_REG (dword_mode, MD_REG_FIRST);
15608 mips_emit_move_or_split (hilo, const0_rtx, SPLIT_FOR_SPEED);
15609
15610 /* If the target provides mulsidi3_32bit then that's the most likely
15611 consumer of the result. Test for bypasses. */
15612 if (dword_mode == DImode && HAVE_maddsidi4)
15613 {
15614 rtx gpr = gen_rtx_REG (SImode, GP_REG_FIRST + 4);
15615 emit_insn (gen_maddsidi4 (hilo, gpr, gpr, hilo));
15616 }
15617
15618 unsigned int time = mips_seq_time (state, get_insns ());
15619 end_sequence ();
15620 return time;
15621 }
15622
15623 /* Check the relative speeds of "MULT $0,$0" and "MTLO $0; MTHI $0"
15624 and set up mips_tuning_info.fast_mult_zero_zero_p accordingly.
15625 Prefer MULT -- which is shorter -- in the event of a tie. */
15626
15627 static void
15628 mips_set_fast_mult_zero_zero_p (struct mips_sim *state)
15629 {
15630 if (TARGET_MIPS16)
15631 /* No MTLO or MTHI available. */
15632 mips_tuning_info.fast_mult_zero_zero_p = true;
15633 else
15634 {
15635 unsigned int true_time = mips_mult_zero_zero_cost (state, true);
15636 unsigned int false_time = mips_mult_zero_zero_cost (state, false);
15637 mips_tuning_info.fast_mult_zero_zero_p = (true_time <= false_time);
15638 }
15639 }
15640
15641 /* Set up costs based on the current architecture and tuning settings. */
15642
15643 static void
15644 mips_set_tuning_info (void)
15645 {
15646 if (mips_tuning_info.initialized_p
15647 && mips_tuning_info.arch == mips_arch
15648 && mips_tuning_info.tune == mips_tune
15649 && mips_tuning_info.mips16_p == TARGET_MIPS16)
15650 return;
15651
15652 mips_tuning_info.arch = mips_arch;
15653 mips_tuning_info.tune = mips_tune;
15654 mips_tuning_info.mips16_p = TARGET_MIPS16;
15655 mips_tuning_info.initialized_p = true;
15656
15657 dfa_start ();
15658
15659 struct mips_sim state;
15660 mips_sim_init (&state, alloca (state_size ()));
15661
15662 mips_set_fast_mult_zero_zero_p (&state);
15663
15664 dfa_finish ();
15665 }
15666
15667 /* Implement TARGET_EXPAND_TO_RTL_HOOK. */
15668
15669 static void
15670 mips_expand_to_rtl_hook (void)
15671 {
15672 /* We need to call this at a point where we can safely create sequences
15673 of instructions, so TARGET_OVERRIDE_OPTIONS is too early. We also
15674 need to call it at a point where the DFA infrastructure is not
15675 already in use, so we can't just call it lazily on demand.
15676
15677 At present, mips_tuning_info is only needed during post-expand
15678 RTL passes such as split_insns, so this hook should be early enough.
15679 We may need to move the call elsewhere if mips_tuning_info starts
15680 to be used for other things (such as rtx_costs, or expanders that
15681 could be called during gimple optimization). */
15682 mips_set_tuning_info ();
15683 }
15684 \f
15685 /* The VR4130 pipeline issues aligned pairs of instructions together,
15686 but it stalls the second instruction if it depends on the first.
15687 In order to cut down the amount of logic required, this dependence
15688 check is not based on a full instruction decode. Instead, any non-SPECIAL
15689 instruction is assumed to modify the register specified by bits 20-16
15690 (which is usually the "rt" field).
15691
15692 In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
15693 input, so we can end up with a false dependence between the branch
15694 and its delay slot. If this situation occurs in instruction INSN,
15695 try to avoid it by swapping rs and rt. */
15696
15697 static void
15698 vr4130_avoid_branch_rt_conflict (rtx insn)
15699 {
15700 rtx first, second;
15701
15702 first = SEQ_BEGIN (insn);
15703 second = SEQ_END (insn);
15704 if (JUMP_P (first)
15705 && NONJUMP_INSN_P (second)
15706 && GET_CODE (PATTERN (first)) == SET
15707 && GET_CODE (SET_DEST (PATTERN (first))) == PC
15708 && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
15709 {
15710 /* Check for the right kind of condition. */
15711 rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
15712 if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
15713 && REG_P (XEXP (cond, 0))
15714 && REG_P (XEXP (cond, 1))
15715 && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
15716 && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
15717 {
15718 /* SECOND mentions the rt register but not the rs register. */
15719 rtx tmp = XEXP (cond, 0);
15720 XEXP (cond, 0) = XEXP (cond, 1);
15721 XEXP (cond, 1) = tmp;
15722 }
15723 }
15724 }
15725
15726 /* Implement -mvr4130-align. Go through each basic block and simulate the
15727 processor pipeline. If we find that a pair of instructions could execute
15728 in parallel, and the first of those instructions is not 8-byte aligned,
15729 insert a nop to make it aligned. */
15730
15731 static void
15732 vr4130_align_insns (void)
15733 {
15734 struct mips_sim state;
15735 rtx insn, subinsn, last, last2, next;
15736 bool aligned_p;
15737
15738 dfa_start ();
15739
15740 /* LAST is the last instruction before INSN to have a nonzero length.
15741 LAST2 is the last such instruction before LAST. */
15742 last = 0;
15743 last2 = 0;
15744
15745 /* ALIGNED_P is true if INSN is known to be at an aligned address. */
15746 aligned_p = true;
15747
15748 mips_sim_init (&state, alloca (state_size ()));
15749 for (insn = get_insns (); insn != 0; insn = next)
15750 {
15751 unsigned int length;
15752
15753 next = NEXT_INSN (insn);
15754
15755 /* See the comment above vr4130_avoid_branch_rt_conflict for details.
15756 This isn't really related to the alignment pass, but we do it on
15757 the fly to avoid a separate instruction walk. */
15758 vr4130_avoid_branch_rt_conflict (insn);
15759
15760 length = get_attr_length (insn);
15761 if (length > 0 && USEFUL_INSN_P (insn))
15762 FOR_EACH_SUBINSN (subinsn, insn)
15763 {
15764 mips_sim_wait_insn (&state, subinsn);
15765
15766 /* If we want this instruction to issue in parallel with the
15767 previous one, make sure that the previous instruction is
15768 aligned. There are several reasons why this isn't worthwhile
15769 when the second instruction is a call:
15770
15771 - Calls are less likely to be performance critical,
15772 - There's a good chance that the delay slot can execute
15773 in parallel with the call.
15774 - The return address would then be unaligned.
15775
15776 In general, if we're going to insert a nop between instructions
15777 X and Y, it's better to insert it immediately after X. That
15778 way, if the nop makes Y aligned, it will also align any labels
15779 between X and Y. */
15780 if (state.insns_left != state.issue_rate
15781 && !CALL_P (subinsn))
15782 {
15783 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
15784 {
15785 /* SUBINSN is the first instruction in INSN and INSN is
15786 aligned. We want to align the previous instruction
15787 instead, so insert a nop between LAST2 and LAST.
15788
15789 Note that LAST could be either a single instruction
15790 or a branch with a delay slot. In the latter case,
15791 LAST, like INSN, is already aligned, but the delay
15792 slot must have some extra delay that stops it from
15793 issuing at the same time as the branch. We therefore
15794 insert a nop before the branch in order to align its
15795 delay slot. */
15796 gcc_assert (last2);
15797 emit_insn_after (gen_nop (), last2);
15798 aligned_p = false;
15799 }
15800 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
15801 {
15802 /* SUBINSN is the delay slot of INSN, but INSN is
15803 currently unaligned. Insert a nop between
15804 LAST and INSN to align it. */
15805 gcc_assert (last);
15806 emit_insn_after (gen_nop (), last);
15807 aligned_p = true;
15808 }
15809 }
15810 mips_sim_issue_insn (&state, subinsn);
15811 }
15812 mips_sim_finish_insn (&state, insn);
15813
15814 /* Update LAST, LAST2 and ALIGNED_P for the next instruction. */
15815 length = get_attr_length (insn);
15816 if (length > 0)
15817 {
15818 /* If the instruction is an asm statement or multi-instruction
15819 mips.md patern, the length is only an estimate. Insert an
15820 8 byte alignment after it so that the following instructions
15821 can be handled correctly. */
15822 if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
15823 && (recog_memoized (insn) < 0 || length >= 8))
15824 {
15825 next = emit_insn_after (gen_align (GEN_INT (3)), insn);
15826 next = NEXT_INSN (next);
15827 mips_sim_next_cycle (&state);
15828 aligned_p = true;
15829 }
15830 else if (length & 4)
15831 aligned_p = !aligned_p;
15832 last2 = last;
15833 last = insn;
15834 }
15835
15836 /* See whether INSN is an aligned label. */
15837 if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
15838 aligned_p = true;
15839 }
15840 dfa_finish ();
15841 }
15842 \f
15843 /* This structure records that the current function has a LO_SUM
15844 involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
15845 the largest offset applied to BASE by all such LO_SUMs. */
15846 struct mips_lo_sum_offset {
15847 rtx base;
15848 HOST_WIDE_INT offset;
15849 };
15850
15851 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE. */
15852
15853 static hashval_t
15854 mips_hash_base (rtx base)
15855 {
15856 int do_not_record_p;
15857
15858 return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
15859 }
15860
15861 /* Hashtable helpers. */
15862
15863 struct mips_lo_sum_offset_hasher : typed_free_remove <mips_lo_sum_offset>
15864 {
15865 typedef mips_lo_sum_offset value_type;
15866 typedef rtx_def compare_type;
15867 static inline hashval_t hash (const value_type *);
15868 static inline bool equal (const value_type *, const compare_type *);
15869 };
15870
15871 /* Hash-table callbacks for mips_lo_sum_offsets. */
15872
15873 inline hashval_t
15874 mips_lo_sum_offset_hasher::hash (const value_type *entry)
15875 {
15876 return mips_hash_base (entry->base);
15877 }
15878
15879 inline bool
15880 mips_lo_sum_offset_hasher::equal (const value_type *entry,
15881 const compare_type *value)
15882 {
15883 return rtx_equal_p (entry->base, value);
15884 }
15885
15886 typedef hash_table <mips_lo_sum_offset_hasher> mips_offset_table;
15887
15888 /* Look up symbolic constant X in HTAB, which is a hash table of
15889 mips_lo_sum_offsets. If OPTION is NO_INSERT, return true if X can be
15890 paired with a recorded LO_SUM, otherwise record X in the table. */
15891
15892 static bool
15893 mips_lo_sum_offset_lookup (mips_offset_table htab, rtx x,
15894 enum insert_option option)
15895 {
15896 rtx base, offset;
15897 mips_lo_sum_offset **slot;
15898 struct mips_lo_sum_offset *entry;
15899
15900 /* Split X into a base and offset. */
15901 split_const (x, &base, &offset);
15902 if (UNSPEC_ADDRESS_P (base))
15903 base = UNSPEC_ADDRESS (base);
15904
15905 /* Look up the base in the hash table. */
15906 slot = htab.find_slot_with_hash (base, mips_hash_base (base), option);
15907 if (slot == NULL)
15908 return false;
15909
15910 entry = (struct mips_lo_sum_offset *) *slot;
15911 if (option == INSERT)
15912 {
15913 if (entry == NULL)
15914 {
15915 entry = XNEW (struct mips_lo_sum_offset);
15916 entry->base = base;
15917 entry->offset = INTVAL (offset);
15918 *slot = entry;
15919 }
15920 else
15921 {
15922 if (INTVAL (offset) > entry->offset)
15923 entry->offset = INTVAL (offset);
15924 }
15925 }
15926 return INTVAL (offset) <= entry->offset;
15927 }
15928
15929 /* A for_each_rtx callback for which DATA is a mips_lo_sum_offset hash table.
15930 Record every LO_SUM in *LOC. */
15931
15932 static int
15933 mips_record_lo_sum (rtx *loc, void *data)
15934 {
15935 if (GET_CODE (*loc) == LO_SUM)
15936 mips_lo_sum_offset_lookup (*(mips_offset_table*) data,
15937 XEXP (*loc, 1), INSERT);
15938 return 0;
15939 }
15940
15941 /* Return true if INSN is a SET of an orphaned high-part relocation.
15942 HTAB is a hash table of mips_lo_sum_offsets that describes all the
15943 LO_SUMs in the current function. */
15944
15945 static bool
15946 mips_orphaned_high_part_p (mips_offset_table htab, rtx insn)
15947 {
15948 enum mips_symbol_type type;
15949 rtx x, set;
15950
15951 set = single_set (insn);
15952 if (set)
15953 {
15954 /* Check for %his. */
15955 x = SET_SRC (set);
15956 if (GET_CODE (x) == HIGH
15957 && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
15958 return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
15959
15960 /* Check for local %gots (and %got_pages, which is redundant but OK). */
15961 if (GET_CODE (x) == UNSPEC
15962 && XINT (x, 1) == UNSPEC_LOAD_GOT
15963 && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
15964 SYMBOL_CONTEXT_LEA, &type)
15965 && type == SYMBOL_GOTOFF_PAGE)
15966 return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
15967 }
15968 return false;
15969 }
15970
15971 /* Subroutine of mips_reorg_process_insns. If there is a hazard between
15972 INSN and a previous instruction, avoid it by inserting nops after
15973 instruction AFTER.
15974
15975 *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
15976 this point. If *DELAYED_REG is non-null, INSN must wait a cycle
15977 before using the value of that register. *HILO_DELAY counts the
15978 number of instructions since the last hilo hazard (that is,
15979 the number of instructions since the last MFLO or MFHI).
15980
15981 After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
15982 for the next instruction.
15983
15984 LO_REG is an rtx for the LO register, used in dependence checking. */
15985
15986 static void
15987 mips_avoid_hazard (rtx after, rtx insn, int *hilo_delay,
15988 rtx *delayed_reg, rtx lo_reg)
15989 {
15990 rtx pattern, set;
15991 int nops, ninsns;
15992
15993 pattern = PATTERN (insn);
15994
15995 /* Do not put the whole function in .set noreorder if it contains
15996 an asm statement. We don't know whether there will be hazards
15997 between the asm statement and the gcc-generated code. */
15998 if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
15999 cfun->machine->all_noreorder_p = false;
16000
16001 /* Ignore zero-length instructions (barriers and the like). */
16002 ninsns = get_attr_length (insn) / 4;
16003 if (ninsns == 0)
16004 return;
16005
16006 /* Work out how many nops are needed. Note that we only care about
16007 registers that are explicitly mentioned in the instruction's pattern.
16008 It doesn't matter that calls use the argument registers or that they
16009 clobber hi and lo. */
16010 if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
16011 nops = 2 - *hilo_delay;
16012 else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
16013 nops = 1;
16014 else
16015 nops = 0;
16016
16017 /* Insert the nops between this instruction and the previous one.
16018 Each new nop takes us further from the last hilo hazard. */
16019 *hilo_delay += nops;
16020 while (nops-- > 0)
16021 emit_insn_after (gen_hazard_nop (), after);
16022
16023 /* Set up the state for the next instruction. */
16024 *hilo_delay += ninsns;
16025 *delayed_reg = 0;
16026 if (INSN_CODE (insn) >= 0)
16027 switch (get_attr_hazard (insn))
16028 {
16029 case HAZARD_NONE:
16030 break;
16031
16032 case HAZARD_HILO:
16033 *hilo_delay = 0;
16034 break;
16035
16036 case HAZARD_DELAY:
16037 set = single_set (insn);
16038 gcc_assert (set);
16039 *delayed_reg = SET_DEST (set);
16040 break;
16041 }
16042 }
16043
16044 /* Go through the instruction stream and insert nops where necessary.
16045 Also delete any high-part relocations whose partnering low parts
16046 are now all dead. See if the whole function can then be put into
16047 .set noreorder and .set nomacro. */
16048
16049 static void
16050 mips_reorg_process_insns (void)
16051 {
16052 rtx insn, last_insn, subinsn, next_insn, lo_reg, delayed_reg;
16053 int hilo_delay;
16054 mips_offset_table htab;
16055
16056 /* Force all instructions to be split into their final form. */
16057 split_all_insns_noflow ();
16058
16059 /* Recalculate instruction lengths without taking nops into account. */
16060 cfun->machine->ignore_hazard_length_p = true;
16061 shorten_branches (get_insns ());
16062
16063 cfun->machine->all_noreorder_p = true;
16064
16065 /* We don't track MIPS16 PC-relative offsets closely enough to make
16066 a good job of "set .noreorder" code in MIPS16 mode. */
16067 if (TARGET_MIPS16)
16068 cfun->machine->all_noreorder_p = false;
16069
16070 /* Code that doesn't use explicit relocs can't be ".set nomacro". */
16071 if (!TARGET_EXPLICIT_RELOCS)
16072 cfun->machine->all_noreorder_p = false;
16073
16074 /* Profiled functions can't be all noreorder because the profiler
16075 support uses assembler macros. */
16076 if (crtl->profile)
16077 cfun->machine->all_noreorder_p = false;
16078
16079 /* Code compiled with -mfix-vr4120, -mfix-rm7000 or -mfix-24k can't be
16080 all noreorder because we rely on the assembler to work around some
16081 errata. The R5900 too has several bugs. */
16082 if (TARGET_FIX_VR4120
16083 || TARGET_FIX_RM7000
16084 || TARGET_FIX_24K
16085 || TARGET_MIPS5900)
16086 cfun->machine->all_noreorder_p = false;
16087
16088 /* The same is true for -mfix-vr4130 if we might generate MFLO or
16089 MFHI instructions. Note that we avoid using MFLO and MFHI if
16090 the VR4130 MACC and DMACC instructions are available instead;
16091 see the *mfhilo_{si,di}_macc patterns. */
16092 if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
16093 cfun->machine->all_noreorder_p = false;
16094
16095 htab.create (37);
16096
16097 /* Make a first pass over the instructions, recording all the LO_SUMs. */
16098 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
16099 FOR_EACH_SUBINSN (subinsn, insn)
16100 if (USEFUL_INSN_P (subinsn))
16101 for_each_rtx (&PATTERN (subinsn), mips_record_lo_sum, &htab);
16102
16103 last_insn = 0;
16104 hilo_delay = 2;
16105 delayed_reg = 0;
16106 lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
16107
16108 /* Make a second pass over the instructions. Delete orphaned
16109 high-part relocations or turn them into NOPs. Avoid hazards
16110 by inserting NOPs. */
16111 for (insn = get_insns (); insn != 0; insn = next_insn)
16112 {
16113 next_insn = NEXT_INSN (insn);
16114 if (USEFUL_INSN_P (insn))
16115 {
16116 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
16117 {
16118 /* If we find an orphaned high-part relocation in a delay
16119 slot, it's easier to turn that instruction into a NOP than
16120 to delete it. The delay slot will be a NOP either way. */
16121 FOR_EACH_SUBINSN (subinsn, insn)
16122 if (INSN_P (subinsn))
16123 {
16124 if (mips_orphaned_high_part_p (htab, subinsn))
16125 {
16126 PATTERN (subinsn) = gen_nop ();
16127 INSN_CODE (subinsn) = CODE_FOR_nop;
16128 }
16129 mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
16130 &delayed_reg, lo_reg);
16131 }
16132 last_insn = insn;
16133 }
16134 else
16135 {
16136 /* INSN is a single instruction. Delete it if it's an
16137 orphaned high-part relocation. */
16138 if (mips_orphaned_high_part_p (htab, insn))
16139 delete_insn (insn);
16140 /* Also delete cache barriers if the last instruction
16141 was an annulled branch. INSN will not be speculatively
16142 executed. */
16143 else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
16144 && last_insn
16145 && JUMP_P (SEQ_BEGIN (last_insn))
16146 && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
16147 delete_insn (insn);
16148 else
16149 {
16150 mips_avoid_hazard (last_insn, insn, &hilo_delay,
16151 &delayed_reg, lo_reg);
16152 last_insn = insn;
16153 }
16154 }
16155 }
16156 }
16157
16158 htab.dispose ();
16159 }
16160
16161 /* Return true if the function has a long branch instruction. */
16162
16163 static bool
16164 mips_has_long_branch_p (void)
16165 {
16166 rtx insn, subinsn;
16167 int normal_length;
16168
16169 /* We need up-to-date instruction lengths. */
16170 shorten_branches (get_insns ());
16171
16172 /* Look for a branch that is longer than normal. The normal length for
16173 non-MIPS16 branches is 8, because the length includes the delay slot.
16174 It is 4 for MIPS16, because MIPS16 branches are extended instructions,
16175 but they have no delay slot. */
16176 normal_length = (TARGET_MIPS16 ? 4 : 8);
16177 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16178 FOR_EACH_SUBINSN (subinsn, insn)
16179 if (JUMP_P (subinsn)
16180 && get_attr_length (subinsn) > normal_length
16181 && (any_condjump_p (subinsn) || any_uncondjump_p (subinsn)))
16182 return true;
16183
16184 return false;
16185 }
16186
16187 /* If we are using a GOT, but have not decided to use a global pointer yet,
16188 see whether we need one to implement long branches. Convert the ghost
16189 global-pointer instructions into real ones if so. */
16190
16191 static bool
16192 mips_expand_ghost_gp_insns (void)
16193 {
16194 /* Quick exit if we already know that we will or won't need a
16195 global pointer. */
16196 if (!TARGET_USE_GOT
16197 || cfun->machine->global_pointer == INVALID_REGNUM
16198 || mips_must_initialize_gp_p ())
16199 return false;
16200
16201 /* Run a full check for long branches. */
16202 if (!mips_has_long_branch_p ())
16203 return false;
16204
16205 /* We've now established that we need $gp. */
16206 cfun->machine->must_initialize_gp_p = true;
16207 split_all_insns_noflow ();
16208
16209 return true;
16210 }
16211
16212 /* Subroutine of mips_reorg to manage passes that require DF. */
16213
16214 static void
16215 mips_df_reorg (void)
16216 {
16217 /* Create def-use chains. */
16218 df_set_flags (DF_EQ_NOTES);
16219 df_chain_add_problem (DF_UD_CHAIN);
16220 df_analyze ();
16221
16222 if (TARGET_RELAX_PIC_CALLS)
16223 mips_annotate_pic_calls ();
16224
16225 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
16226 r10k_insert_cache_barriers ();
16227
16228 df_finish_pass (false);
16229 }
16230
16231 /* Emit code to load LABEL_REF SRC into MIPS16 register DEST. This is
16232 called very late in mips_reorg, but the caller is required to run
16233 mips16_lay_out_constants on the result. */
16234
16235 static void
16236 mips16_load_branch_target (rtx dest, rtx src)
16237 {
16238 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
16239 {
16240 rtx page, low;
16241
16242 if (mips_cfun_has_cprestore_slot_p ())
16243 mips_emit_move (dest, mips_cprestore_slot (dest, true));
16244 else
16245 mips_emit_move (dest, pic_offset_table_rtx);
16246 page = mips_unspec_address (src, SYMBOL_GOTOFF_PAGE);
16247 low = mips_unspec_address (src, SYMBOL_GOT_PAGE_OFST);
16248 emit_insn (gen_rtx_SET (VOIDmode, dest,
16249 PMODE_INSN (gen_unspec_got, (dest, page))));
16250 emit_insn (gen_rtx_SET (VOIDmode, dest,
16251 gen_rtx_LO_SUM (Pmode, dest, low)));
16252 }
16253 else
16254 {
16255 src = mips_unspec_address (src, SYMBOL_ABSOLUTE);
16256 mips_emit_move (dest, src);
16257 }
16258 }
16259
16260 /* If we're compiling a MIPS16 function, look for and split any long branches.
16261 This must be called after all other instruction modifications in
16262 mips_reorg. */
16263
16264 static void
16265 mips16_split_long_branches (void)
16266 {
16267 bool something_changed;
16268
16269 if (!TARGET_MIPS16)
16270 return;
16271
16272 /* Loop until the alignments for all targets are sufficient. */
16273 do
16274 {
16275 rtx insn;
16276
16277 shorten_branches (get_insns ());
16278 something_changed = false;
16279 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16280 if (JUMP_P (insn)
16281 && get_attr_length (insn) > 4
16282 && (any_condjump_p (insn) || any_uncondjump_p (insn)))
16283 {
16284 rtx old_label, new_label, temp, saved_temp;
16285 rtx target, jump, jump_sequence;
16286
16287 start_sequence ();
16288
16289 /* Free up a MIPS16 register by saving it in $1. */
16290 saved_temp = gen_rtx_REG (Pmode, AT_REGNUM);
16291 temp = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
16292 emit_move_insn (saved_temp, temp);
16293
16294 /* Load the branch target into TEMP. */
16295 old_label = JUMP_LABEL (insn);
16296 target = gen_rtx_LABEL_REF (Pmode, old_label);
16297 mips16_load_branch_target (temp, target);
16298
16299 /* Jump to the target and restore the register's
16300 original value. */
16301 jump = emit_jump_insn (PMODE_INSN (gen_indirect_jump_and_restore,
16302 (temp, temp, saved_temp)));
16303 JUMP_LABEL (jump) = old_label;
16304 LABEL_NUSES (old_label)++;
16305
16306 /* Rewrite any symbolic references that are supposed to use
16307 a PC-relative constant pool. */
16308 mips16_lay_out_constants (false);
16309
16310 if (simplejump_p (insn))
16311 /* We're going to replace INSN with a longer form. */
16312 new_label = NULL_RTX;
16313 else
16314 {
16315 /* Create a branch-around label for the original
16316 instruction. */
16317 new_label = gen_label_rtx ();
16318 emit_label (new_label);
16319 }
16320
16321 jump_sequence = get_insns ();
16322 end_sequence ();
16323
16324 emit_insn_after (jump_sequence, insn);
16325 if (new_label)
16326 invert_jump (insn, new_label, false);
16327 else
16328 delete_insn (insn);
16329 something_changed = true;
16330 }
16331 }
16332 while (something_changed);
16333 }
16334
16335 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
16336
16337 static void
16338 mips_reorg (void)
16339 {
16340 /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF. Also during
16341 insn splitting in mips16_lay_out_constants, DF insn info is only kept up
16342 to date if the CFG is available. */
16343 if (mips_cfg_in_reorg ())
16344 compute_bb_for_insn ();
16345 mips16_lay_out_constants (true);
16346 if (mips_cfg_in_reorg ())
16347 {
16348 mips_df_reorg ();
16349 free_bb_for_insn ();
16350 }
16351 }
16352
16353 /* We use a machine specific pass to do a second machine dependent reorg
16354 pass after delay branch scheduling. */
16355
16356 static unsigned int
16357 mips_machine_reorg2 (void)
16358 {
16359 mips_reorg_process_insns ();
16360 if (!TARGET_MIPS16
16361 && TARGET_EXPLICIT_RELOCS
16362 && TUNE_MIPS4130
16363 && TARGET_VR4130_ALIGN)
16364 vr4130_align_insns ();
16365 if (mips_expand_ghost_gp_insns ())
16366 /* The expansion could invalidate some of the VR4130 alignment
16367 optimizations, but this should be an extremely rare case anyhow. */
16368 mips_reorg_process_insns ();
16369 mips16_split_long_branches ();
16370 return 0;
16371 }
16372
16373 namespace {
16374
16375 const pass_data pass_data_mips_machine_reorg2 =
16376 {
16377 RTL_PASS, /* type */
16378 "mach2", /* name */
16379 OPTGROUP_NONE, /* optinfo_flags */
16380 false, /* has_gate */
16381 true, /* has_execute */
16382 TV_MACH_DEP, /* tv_id */
16383 0, /* properties_required */
16384 0, /* properties_provided */
16385 0, /* properties_destroyed */
16386 0, /* todo_flags_start */
16387 TODO_verify_rtl_sharing, /* todo_flags_finish */
16388 };
16389
16390 class pass_mips_machine_reorg2 : public rtl_opt_pass
16391 {
16392 public:
16393 pass_mips_machine_reorg2(gcc::context *ctxt)
16394 : rtl_opt_pass(pass_data_mips_machine_reorg2, ctxt)
16395 {}
16396
16397 /* opt_pass methods: */
16398 unsigned int execute () { return mips_machine_reorg2 (); }
16399
16400 }; // class pass_mips_machine_reorg2
16401
16402 } // anon namespace
16403
16404 rtl_opt_pass *
16405 make_pass_mips_machine_reorg2 (gcc::context *ctxt)
16406 {
16407 return new pass_mips_machine_reorg2 (ctxt);
16408 }
16409
16410 \f
16411 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
16412 in order to avoid duplicating too much logic from elsewhere. */
16413
16414 static void
16415 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
16416 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
16417 tree function)
16418 {
16419 rtx this_rtx, temp1, temp2, insn, fnaddr;
16420 bool use_sibcall_p;
16421
16422 /* Pretend to be a post-reload pass while generating rtl. */
16423 reload_completed = 1;
16424
16425 /* Mark the end of the (empty) prologue. */
16426 emit_note (NOTE_INSN_PROLOGUE_END);
16427
16428 /* Determine if we can use a sibcall to call FUNCTION directly. */
16429 fnaddr = XEXP (DECL_RTL (function), 0);
16430 use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
16431 && const_call_insn_operand (fnaddr, Pmode));
16432
16433 /* Determine if we need to load FNADDR from the GOT. */
16434 if (!use_sibcall_p
16435 && (mips_got_symbol_type_p
16436 (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
16437 {
16438 /* Pick a global pointer. Use a call-clobbered register if
16439 TARGET_CALL_SAVED_GP. */
16440 cfun->machine->global_pointer
16441 = TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
16442 cfun->machine->must_initialize_gp_p = true;
16443 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
16444
16445 /* Set up the global pointer for n32 or n64 abicalls. */
16446 mips_emit_loadgp ();
16447 }
16448
16449 /* We need two temporary registers in some cases. */
16450 temp1 = gen_rtx_REG (Pmode, 2);
16451 temp2 = gen_rtx_REG (Pmode, 3);
16452
16453 /* Find out which register contains the "this" pointer. */
16454 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
16455 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
16456 else
16457 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
16458
16459 /* Add DELTA to THIS_RTX. */
16460 if (delta != 0)
16461 {
16462 rtx offset = GEN_INT (delta);
16463 if (!SMALL_OPERAND (delta))
16464 {
16465 mips_emit_move (temp1, offset);
16466 offset = temp1;
16467 }
16468 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
16469 }
16470
16471 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
16472 if (vcall_offset != 0)
16473 {
16474 rtx addr;
16475
16476 /* Set TEMP1 to *THIS_RTX. */
16477 mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
16478
16479 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
16480 addr = mips_add_offset (temp2, temp1, vcall_offset);
16481
16482 /* Load the offset and add it to THIS_RTX. */
16483 mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
16484 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
16485 }
16486
16487 /* Jump to the target function. Use a sibcall if direct jumps are
16488 allowed, otherwise load the address into a register first. */
16489 if (use_sibcall_p)
16490 {
16491 insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
16492 SIBLING_CALL_P (insn) = 1;
16493 }
16494 else
16495 {
16496 /* This is messy. GAS treats "la $25,foo" as part of a call
16497 sequence and may allow a global "foo" to be lazily bound.
16498 The general move patterns therefore reject this combination.
16499
16500 In this context, lazy binding would actually be OK
16501 for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
16502 TARGET_CALL_SAVED_GP; see mips_load_call_address.
16503 We must therefore load the address via a temporary
16504 register if mips_dangerous_for_la25_p.
16505
16506 If we jump to the temporary register rather than $25,
16507 the assembler can use the move insn to fill the jump's
16508 delay slot.
16509
16510 We can use the same technique for MIPS16 code, where $25
16511 is not a valid JR register. */
16512 if (TARGET_USE_PIC_FN_ADDR_REG
16513 && !TARGET_MIPS16
16514 && !mips_dangerous_for_la25_p (fnaddr))
16515 temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
16516 mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
16517
16518 if (TARGET_USE_PIC_FN_ADDR_REG
16519 && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
16520 mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
16521 emit_jump_insn (gen_indirect_jump (temp1));
16522 }
16523
16524 /* Run just enough of rest_of_compilation. This sequence was
16525 "borrowed" from alpha.c. */
16526 insn = get_insns ();
16527 split_all_insns_noflow ();
16528 mips16_lay_out_constants (true);
16529 shorten_branches (insn);
16530 final_start_function (insn, file, 1);
16531 final (insn, file, 1);
16532 final_end_function ();
16533
16534 /* Clean up the vars set above. Note that final_end_function resets
16535 the global pointer for us. */
16536 reload_completed = 0;
16537 }
16538 \f
16539
16540 /* The last argument passed to mips_set_compression_mode,
16541 or negative if the function hasn't been called yet. */
16542 static unsigned int old_compression_mode = -1;
16543
16544 /* Set up the target-dependent global state for ISA mode COMPRESSION_MODE,
16545 which is either MASK_MIPS16 or MASK_MICROMIPS. */
16546
16547 static void
16548 mips_set_compression_mode (unsigned int compression_mode)
16549 {
16550
16551 if (compression_mode == old_compression_mode)
16552 return;
16553
16554 /* Restore base settings of various flags. */
16555 target_flags = mips_base_target_flags;
16556 flag_schedule_insns = mips_base_schedule_insns;
16557 flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
16558 flag_move_loop_invariants = mips_base_move_loop_invariants;
16559 align_loops = mips_base_align_loops;
16560 align_jumps = mips_base_align_jumps;
16561 align_functions = mips_base_align_functions;
16562 target_flags &= ~(MASK_MIPS16 | MASK_MICROMIPS);
16563 target_flags |= compression_mode;
16564
16565 if (compression_mode & MASK_MIPS16)
16566 {
16567 /* Switch to MIPS16 mode. */
16568 target_flags |= MASK_MIPS16;
16569
16570 /* Turn off SYNCI if it was on, MIPS16 doesn't support it. */
16571 target_flags &= ~MASK_SYNCI;
16572
16573 /* Don't run the scheduler before reload, since it tends to
16574 increase register pressure. */
16575 flag_schedule_insns = 0;
16576
16577 /* Don't do hot/cold partitioning. mips16_lay_out_constants expects
16578 the whole function to be in a single section. */
16579 flag_reorder_blocks_and_partition = 0;
16580
16581 /* Don't move loop invariants, because it tends to increase
16582 register pressure. It also introduces an extra move in cases
16583 where the constant is the first operand in a two-operand binary
16584 instruction, or when it forms a register argument to a functon
16585 call. */
16586 flag_move_loop_invariants = 0;
16587
16588 target_flags |= MASK_EXPLICIT_RELOCS;
16589
16590 /* Experiments suggest we get the best overall section-anchor
16591 results from using the range of an unextended LW or SW. Code
16592 that makes heavy use of byte or short accesses can do better
16593 with ranges of 0...31 and 0...63 respectively, but most code is
16594 sensitive to the range of LW and SW instead. */
16595 targetm.min_anchor_offset = 0;
16596 targetm.max_anchor_offset = 127;
16597
16598 targetm.const_anchor = 0;
16599
16600 /* MIPS16 has no BAL instruction. */
16601 target_flags &= ~MASK_RELAX_PIC_CALLS;
16602
16603 /* The R4000 errata don't apply to any known MIPS16 cores.
16604 It's simpler to make the R4000 fixes and MIPS16 mode
16605 mutually exclusive. */
16606 target_flags &= ~MASK_FIX_R4000;
16607
16608 if (flag_pic && !TARGET_OLDABI)
16609 sorry ("MIPS16 PIC for ABIs other than o32 and o64");
16610
16611 if (TARGET_XGOT)
16612 sorry ("MIPS16 -mxgot code");
16613
16614 if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
16615 sorry ("hard-float MIPS16 code for ABIs other than o32 and o64");
16616 }
16617 else
16618 {
16619 /* Switch to microMIPS or the standard encoding. */
16620
16621 if (TARGET_MICROMIPS)
16622 /* Avoid branch likely. */
16623 target_flags &= ~MASK_BRANCHLIKELY;
16624
16625 /* Provide default values for align_* for 64-bit targets. */
16626 if (TARGET_64BIT)
16627 {
16628 if (align_loops == 0)
16629 align_loops = 8;
16630 if (align_jumps == 0)
16631 align_jumps = 8;
16632 if (align_functions == 0)
16633 align_functions = 8;
16634 }
16635
16636 targetm.min_anchor_offset = -32768;
16637 targetm.max_anchor_offset = 32767;
16638
16639 targetm.const_anchor = 0x8000;
16640 }
16641
16642 /* (Re)initialize MIPS target internals for new ISA. */
16643 mips_init_relocs ();
16644
16645 if (compression_mode & MASK_MIPS16)
16646 {
16647 if (!mips16_globals)
16648 mips16_globals = save_target_globals_default_opts ();
16649 else
16650 restore_target_globals (mips16_globals);
16651 }
16652 else
16653 restore_target_globals (&default_target_globals);
16654
16655 old_compression_mode = compression_mode;
16656 }
16657
16658 /* Implement TARGET_SET_CURRENT_FUNCTION. Decide whether the current
16659 function should use the MIPS16 or microMIPS ISA and switch modes
16660 accordingly. */
16661
16662 static void
16663 mips_set_current_function (tree fndecl)
16664 {
16665 mips_set_compression_mode (mips_get_compress_mode (fndecl));
16666 }
16667 \f
16668 /* Allocate a chunk of memory for per-function machine-dependent data. */
16669
16670 static struct machine_function *
16671 mips_init_machine_status (void)
16672 {
16673 return ggc_alloc_cleared_machine_function ();
16674 }
16675
16676 /* Return the processor associated with the given ISA level, or null
16677 if the ISA isn't valid. */
16678
16679 static const struct mips_cpu_info *
16680 mips_cpu_info_from_isa (int isa)
16681 {
16682 unsigned int i;
16683
16684 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16685 if (mips_cpu_info_table[i].isa == isa)
16686 return mips_cpu_info_table + i;
16687
16688 return NULL;
16689 }
16690
16691 /* Return a mips_cpu_info entry determined by an option valued
16692 OPT. */
16693
16694 static const struct mips_cpu_info *
16695 mips_cpu_info_from_opt (int opt)
16696 {
16697 switch (opt)
16698 {
16699 case MIPS_ARCH_OPTION_FROM_ABI:
16700 /* 'from-abi' selects the most compatible architecture for the
16701 given ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit
16702 ABIs. For the EABIs, we have to decide whether we're using
16703 the 32-bit or 64-bit version. */
16704 return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
16705 : ABI_NEEDS_64BIT_REGS ? 3
16706 : (TARGET_64BIT ? 3 : 1));
16707
16708 case MIPS_ARCH_OPTION_NATIVE:
16709 gcc_unreachable ();
16710
16711 default:
16712 return &mips_cpu_info_table[opt];
16713 }
16714 }
16715
16716 /* Return a default mips_cpu_info entry, given that no -march= option
16717 was explicitly specified. */
16718
16719 static const struct mips_cpu_info *
16720 mips_default_arch (void)
16721 {
16722 #if defined (MIPS_CPU_STRING_DEFAULT)
16723 unsigned int i;
16724 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16725 if (strcmp (mips_cpu_info_table[i].name, MIPS_CPU_STRING_DEFAULT) == 0)
16726 return mips_cpu_info_table + i;
16727 gcc_unreachable ();
16728 #elif defined (MIPS_ISA_DEFAULT)
16729 return mips_cpu_info_from_isa (MIPS_ISA_DEFAULT);
16730 #else
16731 /* 'from-abi' makes a good default: you get whatever the ABI
16732 requires. */
16733 return mips_cpu_info_from_opt (MIPS_ARCH_OPTION_FROM_ABI);
16734 #endif
16735 }
16736
16737 /* Set up globals to generate code for the ISA or processor
16738 described by INFO. */
16739
16740 static void
16741 mips_set_architecture (const struct mips_cpu_info *info)
16742 {
16743 if (info != 0)
16744 {
16745 mips_arch_info = info;
16746 mips_arch = info->cpu;
16747 mips_isa = info->isa;
16748 }
16749 }
16750
16751 /* Likewise for tuning. */
16752
16753 static void
16754 mips_set_tune (const struct mips_cpu_info *info)
16755 {
16756 if (info != 0)
16757 {
16758 mips_tune_info = info;
16759 mips_tune = info->cpu;
16760 }
16761 }
16762
16763 /* Implement TARGET_OPTION_OVERRIDE. */
16764
16765 static void
16766 mips_option_override (void)
16767 {
16768 int i, start, regno, mode;
16769
16770 if (global_options_set.x_mips_isa_option)
16771 mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
16772
16773 #ifdef SUBTARGET_OVERRIDE_OPTIONS
16774 SUBTARGET_OVERRIDE_OPTIONS;
16775 #endif
16776
16777 /* MIPS16 and microMIPS cannot coexist. */
16778 if (TARGET_MICROMIPS && TARGET_MIPS16)
16779 error ("unsupported combination: %s", "-mips16 -mmicromips");
16780
16781 /* Save the base compression state and process flags as though we
16782 were generating uncompressed code. */
16783 mips_base_compression_flags = TARGET_COMPRESSION;
16784 target_flags &= ~TARGET_COMPRESSION;
16785
16786 /* -mno-float overrides -mhard-float and -msoft-float. */
16787 if (TARGET_NO_FLOAT)
16788 {
16789 target_flags |= MASK_SOFT_FLOAT_ABI;
16790 target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
16791 }
16792
16793 if (TARGET_FLIP_MIPS16)
16794 TARGET_INTERLINK_COMPRESSED = 1;
16795
16796 /* Set the small data limit. */
16797 mips_small_data_threshold = (global_options_set.x_g_switch_value
16798 ? g_switch_value
16799 : MIPS_DEFAULT_GVALUE);
16800
16801 /* The following code determines the architecture and register size.
16802 Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
16803 The GAS and GCC code should be kept in sync as much as possible. */
16804
16805 if (global_options_set.x_mips_arch_option)
16806 mips_set_architecture (mips_cpu_info_from_opt (mips_arch_option));
16807
16808 if (mips_isa_option_info != 0)
16809 {
16810 if (mips_arch_info == 0)
16811 mips_set_architecture (mips_isa_option_info);
16812 else if (mips_arch_info->isa != mips_isa_option_info->isa)
16813 error ("%<-%s%> conflicts with the other architecture options, "
16814 "which specify a %s processor",
16815 mips_isa_option_info->name,
16816 mips_cpu_info_from_isa (mips_arch_info->isa)->name);
16817 }
16818
16819 if (mips_arch_info == 0)
16820 mips_set_architecture (mips_default_arch ());
16821
16822 if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
16823 error ("%<-march=%s%> is not compatible with the selected ABI",
16824 mips_arch_info->name);
16825
16826 /* Optimize for mips_arch, unless -mtune selects a different processor. */
16827 if (global_options_set.x_mips_tune_option)
16828 mips_set_tune (mips_cpu_info_from_opt (mips_tune_option));
16829
16830 if (mips_tune_info == 0)
16831 mips_set_tune (mips_arch_info);
16832
16833 if ((target_flags_explicit & MASK_64BIT) != 0)
16834 {
16835 /* The user specified the size of the integer registers. Make sure
16836 it agrees with the ABI and ISA. */
16837 if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
16838 error ("%<-mgp64%> used with a 32-bit processor");
16839 else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
16840 error ("%<-mgp32%> used with a 64-bit ABI");
16841 else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
16842 error ("%<-mgp64%> used with a 32-bit ABI");
16843 }
16844 else
16845 {
16846 /* Infer the integer register size from the ABI and processor.
16847 Restrict ourselves to 32-bit registers if that's all the
16848 processor has, or if the ABI cannot handle 64-bit registers. */
16849 if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
16850 target_flags &= ~MASK_64BIT;
16851 else
16852 target_flags |= MASK_64BIT;
16853 }
16854
16855 if ((target_flags_explicit & MASK_FLOAT64) != 0)
16856 {
16857 if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
16858 error ("unsupported combination: %s", "-mfp64 -msingle-float");
16859 else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
16860 error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
16861 else if (!TARGET_64BIT && TARGET_FLOAT64)
16862 {
16863 if (!ISA_HAS_MXHC1)
16864 error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
16865 " the target supports the mfhc1 and mthc1 instructions");
16866 else if (mips_abi != ABI_32)
16867 error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
16868 " the o32 ABI");
16869 }
16870 }
16871 else
16872 {
16873 /* -msingle-float selects 32-bit float registers. Otherwise the
16874 float registers should be the same size as the integer ones. */
16875 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
16876 target_flags |= MASK_FLOAT64;
16877 else
16878 target_flags &= ~MASK_FLOAT64;
16879 }
16880
16881 /* End of code shared with GAS. */
16882
16883 /* The R5900 FPU only supports single precision. */
16884 if (TARGET_MIPS5900 && TARGET_HARD_FLOAT_ABI && TARGET_DOUBLE_FLOAT)
16885 error ("unsupported combination: %s",
16886 "-march=r5900 -mhard-float -mdouble-float");
16887
16888 /* If a -mlong* option was given, check that it matches the ABI,
16889 otherwise infer the -mlong* setting from the other options. */
16890 if ((target_flags_explicit & MASK_LONG64) != 0)
16891 {
16892 if (TARGET_LONG64)
16893 {
16894 if (mips_abi == ABI_N32)
16895 error ("%qs is incompatible with %qs", "-mabi=n32", "-mlong64");
16896 else if (mips_abi == ABI_32)
16897 error ("%qs is incompatible with %qs", "-mabi=32", "-mlong64");
16898 else if (mips_abi == ABI_O64 && TARGET_ABICALLS)
16899 /* We have traditionally allowed non-abicalls code to use
16900 an LP64 form of o64. However, it would take a bit more
16901 effort to support the combination of 32-bit GOT entries
16902 and 64-bit pointers, so we treat the abicalls case as
16903 an error. */
16904 error ("the combination of %qs and %qs is incompatible with %qs",
16905 "-mabi=o64", "-mabicalls", "-mlong64");
16906 }
16907 else
16908 {
16909 if (mips_abi == ABI_64)
16910 error ("%qs is incompatible with %qs", "-mabi=64", "-mlong32");
16911 }
16912 }
16913 else
16914 {
16915 if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
16916 target_flags |= MASK_LONG64;
16917 else
16918 target_flags &= ~MASK_LONG64;
16919 }
16920
16921 if (!TARGET_OLDABI)
16922 flag_pcc_struct_return = 0;
16923
16924 /* Decide which rtx_costs structure to use. */
16925 if (optimize_size)
16926 mips_cost = &mips_rtx_cost_optimize_size;
16927 else
16928 mips_cost = &mips_rtx_cost_data[mips_tune];
16929
16930 /* If the user hasn't specified a branch cost, use the processor's
16931 default. */
16932 if (mips_branch_cost == 0)
16933 mips_branch_cost = mips_cost->branch_cost;
16934
16935 /* If neither -mbranch-likely nor -mno-branch-likely was given
16936 on the command line, set MASK_BRANCHLIKELY based on the target
16937 architecture and tuning flags. Annulled delay slots are a
16938 size win, so we only consider the processor-specific tuning
16939 for !optimize_size. */
16940 if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
16941 {
16942 if (ISA_HAS_BRANCHLIKELY
16943 && (optimize_size
16944 || (mips_tune_info->tune_flags & PTF_AVOID_BRANCHLIKELY) == 0))
16945 target_flags |= MASK_BRANCHLIKELY;
16946 else
16947 target_flags &= ~MASK_BRANCHLIKELY;
16948 }
16949 else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
16950 warning (0, "the %qs architecture does not support branch-likely"
16951 " instructions", mips_arch_info->name);
16952
16953 /* If the user hasn't specified -mimadd or -mno-imadd set
16954 MASK_IMADD based on the target architecture and tuning
16955 flags. */
16956 if ((target_flags_explicit & MASK_IMADD) == 0)
16957 {
16958 if (ISA_HAS_MADD_MSUB &&
16959 (mips_tune_info->tune_flags & PTF_AVOID_IMADD) == 0)
16960 target_flags |= MASK_IMADD;
16961 else
16962 target_flags &= ~MASK_IMADD;
16963 }
16964 else if (TARGET_IMADD && !ISA_HAS_MADD_MSUB)
16965 warning (0, "the %qs architecture does not support madd or msub"
16966 " instructions", mips_arch_info->name);
16967
16968 /* The effect of -mabicalls isn't defined for the EABI. */
16969 if (mips_abi == ABI_EABI && TARGET_ABICALLS)
16970 {
16971 error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
16972 target_flags &= ~MASK_ABICALLS;
16973 }
16974
16975 /* PIC requires -mabicalls. */
16976 if (flag_pic)
16977 {
16978 if (mips_abi == ABI_EABI)
16979 error ("cannot generate position-independent code for %qs",
16980 "-mabi=eabi");
16981 else if (!TARGET_ABICALLS)
16982 error ("position-independent code requires %qs", "-mabicalls");
16983 }
16984
16985 if (TARGET_ABICALLS_PIC2)
16986 /* We need to set flag_pic for executables as well as DSOs
16987 because we may reference symbols that are not defined in
16988 the final executable. (MIPS does not use things like
16989 copy relocs, for example.)
16990
16991 There is a body of code that uses __PIC__ to distinguish
16992 between -mabicalls and -mno-abicalls code. The non-__PIC__
16993 variant is usually appropriate for TARGET_ABICALLS_PIC0, as
16994 long as any indirect jumps use $25. */
16995 flag_pic = 1;
16996
16997 /* -mvr4130-align is a "speed over size" optimization: it usually produces
16998 faster code, but at the expense of more nops. Enable it at -O3 and
16999 above. */
17000 if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
17001 target_flags |= MASK_VR4130_ALIGN;
17002
17003 /* Prefer a call to memcpy over inline code when optimizing for size,
17004 though see MOVE_RATIO in mips.h. */
17005 if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
17006 target_flags |= MASK_MEMCPY;
17007
17008 /* If we have a nonzero small-data limit, check that the -mgpopt
17009 setting is consistent with the other target flags. */
17010 if (mips_small_data_threshold > 0)
17011 {
17012 if (!TARGET_GPOPT)
17013 {
17014 if (!TARGET_EXPLICIT_RELOCS)
17015 error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
17016
17017 TARGET_LOCAL_SDATA = false;
17018 TARGET_EXTERN_SDATA = false;
17019 }
17020 else
17021 {
17022 if (TARGET_VXWORKS_RTP)
17023 warning (0, "cannot use small-data accesses for %qs", "-mrtp");
17024
17025 if (TARGET_ABICALLS)
17026 warning (0, "cannot use small-data accesses for %qs",
17027 "-mabicalls");
17028 }
17029 }
17030
17031 /* Pre-IEEE 754-2008 MIPS hardware has a quirky almost-IEEE format
17032 for all its floating point. */
17033 if (mips_nan != MIPS_IEEE_754_2008)
17034 {
17035 REAL_MODE_FORMAT (SFmode) = &mips_single_format;
17036 REAL_MODE_FORMAT (DFmode) = &mips_double_format;
17037 REAL_MODE_FORMAT (TFmode) = &mips_quad_format;
17038 }
17039
17040 /* Make sure that the user didn't turn off paired single support when
17041 MIPS-3D support is requested. */
17042 if (TARGET_MIPS3D
17043 && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
17044 && !TARGET_PAIRED_SINGLE_FLOAT)
17045 error ("%<-mips3d%> requires %<-mpaired-single%>");
17046
17047 /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT. */
17048 if (TARGET_MIPS3D)
17049 target_flags |= MASK_PAIRED_SINGLE_FLOAT;
17050
17051 /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
17052 and TARGET_HARD_FLOAT_ABI are both true. */
17053 if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
17054 error ("%qs must be used with %qs",
17055 TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
17056 TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
17057
17058 /* Make sure that the ISA supports TARGET_PAIRED_SINGLE_FLOAT when it is
17059 enabled. */
17060 if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
17061 warning (0, "the %qs architecture does not support paired-single"
17062 " instructions", mips_arch_info->name);
17063
17064 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
17065 && !TARGET_CACHE_BUILTIN)
17066 {
17067 error ("%qs requires a target that provides the %qs instruction",
17068 "-mr10k-cache-barrier", "cache");
17069 mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
17070 }
17071
17072 /* If TARGET_DSPR2, enable MASK_DSP. */
17073 if (TARGET_DSPR2)
17074 target_flags |= MASK_DSP;
17075
17076 /* .eh_frame addresses should be the same width as a C pointer.
17077 Most MIPS ABIs support only one pointer size, so the assembler
17078 will usually know exactly how big an .eh_frame address is.
17079
17080 Unfortunately, this is not true of the 64-bit EABI. The ABI was
17081 originally defined to use 64-bit pointers (i.e. it is LP64), and
17082 this is still the default mode. However, we also support an n32-like
17083 ILP32 mode, which is selected by -mlong32. The problem is that the
17084 assembler has traditionally not had an -mlong option, so it has
17085 traditionally not known whether we're using the ILP32 or LP64 form.
17086
17087 As it happens, gas versions up to and including 2.19 use _32-bit_
17088 addresses for EABI64 .cfi_* directives. This is wrong for the
17089 default LP64 mode, so we can't use the directives by default.
17090 Moreover, since gas's current behavior is at odds with gcc's
17091 default behavior, it seems unwise to rely on future versions
17092 of gas behaving the same way. We therefore avoid using .cfi
17093 directives for -mlong32 as well. */
17094 if (mips_abi == ABI_EABI && TARGET_64BIT)
17095 flag_dwarf2_cfi_asm = 0;
17096
17097 /* .cfi_* directives generate a read-only section, so fall back on
17098 manual .eh_frame creation if we need the section to be writable. */
17099 if (TARGET_WRITABLE_EH_FRAME)
17100 flag_dwarf2_cfi_asm = 0;
17101
17102 mips_init_print_operand_punct ();
17103
17104 /* Set up array to map GCC register number to debug register number.
17105 Ignore the special purpose register numbers. */
17106
17107 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17108 {
17109 mips_dbx_regno[i] = IGNORED_DWARF_REGNUM;
17110 if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
17111 mips_dwarf_regno[i] = i;
17112 else
17113 mips_dwarf_regno[i] = INVALID_REGNUM;
17114 }
17115
17116 start = GP_DBX_FIRST - GP_REG_FIRST;
17117 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
17118 mips_dbx_regno[i] = i + start;
17119
17120 start = FP_DBX_FIRST - FP_REG_FIRST;
17121 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
17122 mips_dbx_regno[i] = i + start;
17123
17124 /* Accumulator debug registers use big-endian ordering. */
17125 mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
17126 mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
17127 mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
17128 mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
17129 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
17130 {
17131 mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
17132 mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
17133 }
17134
17135 /* Set up mips_hard_regno_mode_ok. */
17136 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
17137 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
17138 mips_hard_regno_mode_ok[mode][regno]
17139 = mips_hard_regno_mode_ok_p (regno, (enum machine_mode) mode);
17140
17141 /* Function to allocate machine-dependent function status. */
17142 init_machine_status = &mips_init_machine_status;
17143
17144 /* Default to working around R4000 errata only if the processor
17145 was selected explicitly. */
17146 if ((target_flags_explicit & MASK_FIX_R4000) == 0
17147 && strcmp (mips_arch_info->name, "r4000") == 0)
17148 target_flags |= MASK_FIX_R4000;
17149
17150 /* Default to working around R4400 errata only if the processor
17151 was selected explicitly. */
17152 if ((target_flags_explicit & MASK_FIX_R4400) == 0
17153 && strcmp (mips_arch_info->name, "r4400") == 0)
17154 target_flags |= MASK_FIX_R4400;
17155
17156 /* Default to working around R10000 errata only if the processor
17157 was selected explicitly. */
17158 if ((target_flags_explicit & MASK_FIX_R10000) == 0
17159 && strcmp (mips_arch_info->name, "r10000") == 0)
17160 target_flags |= MASK_FIX_R10000;
17161
17162 /* Make sure that branch-likely instructions available when using
17163 -mfix-r10000. The instructions are not available if either:
17164
17165 1. -mno-branch-likely was passed.
17166 2. The selected ISA does not support branch-likely and
17167 the command line does not include -mbranch-likely. */
17168 if (TARGET_FIX_R10000
17169 && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
17170 ? !ISA_HAS_BRANCHLIKELY
17171 : !TARGET_BRANCHLIKELY))
17172 sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
17173
17174 if (TARGET_SYNCI && !ISA_HAS_SYNCI)
17175 {
17176 warning (0, "the %qs architecture does not support the synci "
17177 "instruction", mips_arch_info->name);
17178 target_flags &= ~MASK_SYNCI;
17179 }
17180
17181 /* Only optimize PIC indirect calls if they are actually required. */
17182 if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
17183 target_flags &= ~MASK_RELAX_PIC_CALLS;
17184
17185 /* Save base state of options. */
17186 mips_base_target_flags = target_flags;
17187 mips_base_schedule_insns = flag_schedule_insns;
17188 mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
17189 mips_base_move_loop_invariants = flag_move_loop_invariants;
17190 mips_base_align_loops = align_loops;
17191 mips_base_align_jumps = align_jumps;
17192 mips_base_align_functions = align_functions;
17193
17194 /* Now select the ISA mode.
17195
17196 Do all CPP-sensitive stuff in uncompressed mode; we'll switch modes
17197 later if required. */
17198 mips_set_compression_mode (0);
17199
17200 /* We register a second machine specific reorg pass after delay slot
17201 filling. Registering the pass must be done at start up. It's
17202 convenient to do it here. */
17203 opt_pass *new_pass = make_pass_mips_machine_reorg2 (g);
17204 struct register_pass_info insert_pass_mips_machine_reorg2 =
17205 {
17206 new_pass, /* pass */
17207 "dbr", /* reference_pass_name */
17208 1, /* ref_pass_instance_number */
17209 PASS_POS_INSERT_AFTER /* po_op */
17210 };
17211 register_pass (&insert_pass_mips_machine_reorg2);
17212
17213 if (TARGET_HARD_FLOAT_ABI && TARGET_MIPS5900)
17214 REAL_MODE_FORMAT (SFmode) = &spu_single_format;
17215 }
17216
17217 /* Swap the register information for registers I and I + 1, which
17218 currently have the wrong endianness. Note that the registers'
17219 fixedness and call-clobberedness might have been set on the
17220 command line. */
17221
17222 static void
17223 mips_swap_registers (unsigned int i)
17224 {
17225 int tmpi;
17226 const char *tmps;
17227
17228 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
17229 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
17230
17231 SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
17232 SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
17233 SWAP_INT (call_really_used_regs[i], call_really_used_regs[i + 1]);
17234 SWAP_STRING (reg_names[i], reg_names[i + 1]);
17235
17236 #undef SWAP_STRING
17237 #undef SWAP_INT
17238 }
17239
17240 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
17241
17242 static void
17243 mips_conditional_register_usage (void)
17244 {
17245
17246 if (ISA_HAS_DSP)
17247 {
17248 /* These DSP control register fields are global. */
17249 global_regs[CCDSP_PO_REGNUM] = 1;
17250 global_regs[CCDSP_SC_REGNUM] = 1;
17251 }
17252 else
17253 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17254 reg_class_contents[(int) DSP_ACC_REGS]);
17255
17256 if (!TARGET_HARD_FLOAT)
17257 {
17258 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17259 reg_class_contents[(int) FP_REGS]);
17260 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17261 reg_class_contents[(int) ST_REGS]);
17262 }
17263 else if (!ISA_HAS_8CC)
17264 {
17265 /* We only have a single condition-code register. We implement
17266 this by fixing all the condition-code registers and generating
17267 RTL that refers directly to ST_REG_FIRST. */
17268 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17269 reg_class_contents[(int) ST_REGS]);
17270 SET_HARD_REG_BIT (accessible_reg_set, FPSW_REGNUM);
17271 fixed_regs[FPSW_REGNUM] = call_used_regs[FPSW_REGNUM] = 1;
17272 }
17273 if (TARGET_MIPS16)
17274 {
17275 /* In MIPS16 mode, we prohibit the unused $s registers, since they
17276 are call-saved, and saving them via a MIPS16 register would
17277 probably waste more time than just reloading the value.
17278
17279 We permit the $t temporary registers when optimizing for speed
17280 but not when optimizing for space because using them results in
17281 code that is larger (but faster) then not using them. We do
17282 allow $24 (t8) because it is used in CMP and CMPI instructions
17283 and $25 (t9) because it is used as the function call address in
17284 SVR4 PIC code. */
17285
17286 fixed_regs[18] = call_used_regs[18] = 1;
17287 fixed_regs[19] = call_used_regs[19] = 1;
17288 fixed_regs[20] = call_used_regs[20] = 1;
17289 fixed_regs[21] = call_used_regs[21] = 1;
17290 fixed_regs[22] = call_used_regs[22] = 1;
17291 fixed_regs[23] = call_used_regs[23] = 1;
17292 fixed_regs[26] = call_used_regs[26] = 1;
17293 fixed_regs[27] = call_used_regs[27] = 1;
17294 fixed_regs[30] = call_used_regs[30] = 1;
17295 if (optimize_size)
17296 {
17297 fixed_regs[8] = call_used_regs[8] = 1;
17298 fixed_regs[9] = call_used_regs[9] = 1;
17299 fixed_regs[10] = call_used_regs[10] = 1;
17300 fixed_regs[11] = call_used_regs[11] = 1;
17301 fixed_regs[12] = call_used_regs[12] = 1;
17302 fixed_regs[13] = call_used_regs[13] = 1;
17303 fixed_regs[14] = call_used_regs[14] = 1;
17304 fixed_regs[15] = call_used_regs[15] = 1;
17305 }
17306
17307 /* Do not allow HI and LO to be treated as register operands.
17308 There are no MTHI or MTLO instructions (or any real need
17309 for them) and one-way registers cannot easily be reloaded. */
17310 AND_COMPL_HARD_REG_SET (operand_reg_set,
17311 reg_class_contents[(int) MD_REGS]);
17312 }
17313 /* $f20-$f23 are call-clobbered for n64. */
17314 if (mips_abi == ABI_64)
17315 {
17316 int regno;
17317 for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
17318 call_really_used_regs[regno] = call_used_regs[regno] = 1;
17319 }
17320 /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
17321 for n32. */
17322 if (mips_abi == ABI_N32)
17323 {
17324 int regno;
17325 for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
17326 call_really_used_regs[regno] = call_used_regs[regno] = 1;
17327 }
17328 /* Make sure that double-register accumulator values are correctly
17329 ordered for the current endianness. */
17330 if (TARGET_LITTLE_ENDIAN)
17331 {
17332 unsigned int regno;
17333
17334 mips_swap_registers (MD_REG_FIRST);
17335 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
17336 mips_swap_registers (regno);
17337 }
17338 }
17339
17340 /* When generating MIPS16 code, we want to allocate $24 (T_REG) before
17341 other registers for instructions for which it is possible. This
17342 encourages the compiler to use CMP in cases where an XOR would
17343 require some register shuffling. */
17344
17345 void
17346 mips_order_regs_for_local_alloc (void)
17347 {
17348 int i;
17349
17350 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17351 reg_alloc_order[i] = i;
17352
17353 if (TARGET_MIPS16)
17354 {
17355 /* It really doesn't matter where we put register 0, since it is
17356 a fixed register anyhow. */
17357 reg_alloc_order[0] = 24;
17358 reg_alloc_order[24] = 0;
17359 }
17360 }
17361
17362 /* Implement EH_USES. */
17363
17364 bool
17365 mips_eh_uses (unsigned int regno)
17366 {
17367 if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
17368 {
17369 /* We need to force certain registers to be live in order to handle
17370 PIC long branches correctly. See mips_must_initialize_gp_p for
17371 details. */
17372 if (mips_cfun_has_cprestore_slot_p ())
17373 {
17374 if (regno == CPRESTORE_SLOT_REGNUM)
17375 return true;
17376 }
17377 else
17378 {
17379 if (cfun->machine->global_pointer == regno)
17380 return true;
17381 }
17382 }
17383
17384 return false;
17385 }
17386
17387 /* Implement EPILOGUE_USES. */
17388
17389 bool
17390 mips_epilogue_uses (unsigned int regno)
17391 {
17392 /* Say that the epilogue uses the return address register. Note that
17393 in the case of sibcalls, the values "used by the epilogue" are
17394 considered live at the start of the called function. */
17395 if (regno == RETURN_ADDR_REGNUM)
17396 return true;
17397
17398 /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
17399 See the comment above load_call<mode> for details. */
17400 if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
17401 return true;
17402
17403 /* An interrupt handler must preserve some registers that are
17404 ordinarily call-clobbered. */
17405 if (cfun->machine->interrupt_handler_p
17406 && mips_interrupt_extra_call_saved_reg_p (regno))
17407 return true;
17408
17409 return false;
17410 }
17411
17412 /* A for_each_rtx callback. Stop the search if *X is an AT register. */
17413
17414 static int
17415 mips_at_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
17416 {
17417 return REG_P (*x) && REGNO (*x) == AT_REGNUM;
17418 }
17419
17420 /* Return true if INSN needs to be wrapped in ".set noat".
17421 INSN has NOPERANDS operands, stored in OPVEC. */
17422
17423 static bool
17424 mips_need_noat_wrapper_p (rtx insn, rtx *opvec, int noperands)
17425 {
17426 int i;
17427
17428 if (recog_memoized (insn) >= 0)
17429 for (i = 0; i < noperands; i++)
17430 if (for_each_rtx (&opvec[i], mips_at_reg_p, NULL))
17431 return true;
17432 return false;
17433 }
17434
17435 /* Implement FINAL_PRESCAN_INSN. */
17436
17437 void
17438 mips_final_prescan_insn (rtx insn, rtx *opvec, int noperands)
17439 {
17440 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17441 mips_push_asm_switch (&mips_noat);
17442 }
17443
17444 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN. */
17445
17446 static void
17447 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx insn,
17448 rtx *opvec, int noperands)
17449 {
17450 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17451 mips_pop_asm_switch (&mips_noat);
17452 }
17453
17454 /* Return the function that is used to expand the <u>mulsidi3 pattern.
17455 EXT_CODE is the code of the extension used. Return NULL if widening
17456 multiplication shouldn't be used. */
17457
17458 mulsidi3_gen_fn
17459 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
17460 {
17461 bool signed_p;
17462
17463 signed_p = ext_code == SIGN_EXTEND;
17464 if (TARGET_64BIT)
17465 {
17466 /* Don't use widening multiplication with MULT when we have DMUL. Even
17467 with the extension of its input operands DMUL is faster. Note that
17468 the extension is not needed for signed multiplication. In order to
17469 ensure that we always remove the redundant sign-extension in this
17470 case we still expand mulsidi3 for DMUL. */
17471 if (ISA_HAS_DMUL3)
17472 return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
17473 if (TARGET_MIPS16)
17474 return (signed_p
17475 ? gen_mulsidi3_64bit_mips16
17476 : gen_umulsidi3_64bit_mips16);
17477 if (TARGET_FIX_R4000)
17478 return NULL;
17479 return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
17480 }
17481 else
17482 {
17483 if (TARGET_MIPS16)
17484 return (signed_p
17485 ? gen_mulsidi3_32bit_mips16
17486 : gen_umulsidi3_32bit_mips16);
17487 if (TARGET_FIX_R4000 && !ISA_HAS_DSP)
17488 return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
17489 return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
17490 }
17491 }
17492
17493 /* Return true if PATTERN matches the kind of instruction generated by
17494 umips_build_save_restore. SAVE_P is true for store. */
17495
17496 bool
17497 umips_save_restore_pattern_p (bool save_p, rtx pattern)
17498 {
17499 int n;
17500 unsigned int i;
17501 HOST_WIDE_INT first_offset = 0;
17502 rtx first_base = 0;
17503 unsigned int regmask = 0;
17504
17505 for (n = 0; n < XVECLEN (pattern, 0); n++)
17506 {
17507 rtx set, reg, mem, this_base;
17508 HOST_WIDE_INT this_offset;
17509
17510 /* Check that we have a SET. */
17511 set = XVECEXP (pattern, 0, n);
17512 if (GET_CODE (set) != SET)
17513 return false;
17514
17515 /* Check that the SET is a load (if restoring) or a store
17516 (if saving). */
17517 mem = save_p ? SET_DEST (set) : SET_SRC (set);
17518 if (!MEM_P (mem) || MEM_VOLATILE_P (mem))
17519 return false;
17520
17521 /* Check that the address is the sum of base and a possibly-zero
17522 constant offset. Determine if the offset is in range. */
17523 mips_split_plus (XEXP (mem, 0), &this_base, &this_offset);
17524 if (!REG_P (this_base))
17525 return false;
17526
17527 if (n == 0)
17528 {
17529 if (!UMIPS_12BIT_OFFSET_P (this_offset))
17530 return false;
17531 first_base = this_base;
17532 first_offset = this_offset;
17533 }
17534 else
17535 {
17536 /* Check that the save slots are consecutive. */
17537 if (REGNO (this_base) != REGNO (first_base)
17538 || this_offset != first_offset + UNITS_PER_WORD * n)
17539 return false;
17540 }
17541
17542 /* Check that SET's other operand is a register. */
17543 reg = save_p ? SET_SRC (set) : SET_DEST (set);
17544 if (!REG_P (reg))
17545 return false;
17546
17547 regmask |= 1 << REGNO (reg);
17548 }
17549
17550 for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
17551 if (regmask == umips_swm_mask[i])
17552 return true;
17553
17554 return false;
17555 }
17556
17557 /* Return the assembly instruction for microMIPS LWM or SWM.
17558 SAVE_P and PATTERN are as for umips_save_restore_pattern_p. */
17559
17560 const char *
17561 umips_output_save_restore (bool save_p, rtx pattern)
17562 {
17563 static char buffer[300];
17564 char *s;
17565 int n;
17566 HOST_WIDE_INT offset;
17567 rtx base, mem, set, last_set, last_reg;
17568
17569 /* Parse the pattern. */
17570 gcc_assert (umips_save_restore_pattern_p (save_p, pattern));
17571
17572 s = strcpy (buffer, save_p ? "swm\t" : "lwm\t");
17573 s += strlen (s);
17574 n = XVECLEN (pattern, 0);
17575
17576 set = XVECEXP (pattern, 0, 0);
17577 mem = save_p ? SET_DEST (set) : SET_SRC (set);
17578 mips_split_plus (XEXP (mem, 0), &base, &offset);
17579
17580 last_set = XVECEXP (pattern, 0, n - 1);
17581 last_reg = save_p ? SET_SRC (last_set) : SET_DEST (last_set);
17582
17583 if (REGNO (last_reg) == 31)
17584 n--;
17585
17586 gcc_assert (n <= 9);
17587 if (n == 0)
17588 ;
17589 else if (n == 1)
17590 s += sprintf (s, "%s,", reg_names[16]);
17591 else if (n < 9)
17592 s += sprintf (s, "%s-%s,", reg_names[16], reg_names[15 + n]);
17593 else if (n == 9)
17594 s += sprintf (s, "%s-%s,%s,", reg_names[16], reg_names[23],
17595 reg_names[30]);
17596
17597 if (REGNO (last_reg) == 31)
17598 s += sprintf (s, "%s,", reg_names[31]);
17599
17600 s += sprintf (s, "%d(%s)", (int)offset, reg_names[REGNO (base)]);
17601 return buffer;
17602 }
17603
17604 /* Return true if MEM1 and MEM2 use the same base register, and the
17605 offset of MEM2 equals the offset of MEM1 plus 4. FIRST_REG is the
17606 register into (from) which the contents of MEM1 will be loaded
17607 (stored), depending on the value of LOAD_P.
17608 SWAP_P is true when the 1st and 2nd instructions are swapped. */
17609
17610 static bool
17611 umips_load_store_pair_p_1 (bool load_p, bool swap_p,
17612 rtx first_reg, rtx mem1, rtx mem2)
17613 {
17614 rtx base1, base2;
17615 HOST_WIDE_INT offset1, offset2;
17616
17617 if (!MEM_P (mem1) || !MEM_P (mem2))
17618 return false;
17619
17620 mips_split_plus (XEXP (mem1, 0), &base1, &offset1);
17621 mips_split_plus (XEXP (mem2, 0), &base2, &offset2);
17622
17623 if (!REG_P (base1) || !rtx_equal_p (base1, base2))
17624 return false;
17625
17626 /* Avoid invalid load pair instructions. */
17627 if (load_p && REGNO (first_reg) == REGNO (base1))
17628 return false;
17629
17630 /* We must avoid this case for anti-dependence.
17631 Ex: lw $3, 4($3)
17632 lw $2, 0($3)
17633 first_reg is $2, but the base is $3. */
17634 if (load_p
17635 && swap_p
17636 && REGNO (first_reg) + 1 == REGNO (base1))
17637 return false;
17638
17639 if (offset2 != offset1 + 4)
17640 return false;
17641
17642 if (!UMIPS_12BIT_OFFSET_P (offset1))
17643 return false;
17644
17645 return true;
17646 }
17647
17648 /* OPERANDS describes the operands to a pair of SETs, in the order
17649 dest1, src1, dest2, src2. Return true if the operands can be used
17650 in an LWP or SWP instruction; LOAD_P says which. */
17651
17652 bool
17653 umips_load_store_pair_p (bool load_p, rtx *operands)
17654 {
17655 rtx reg1, reg2, mem1, mem2;
17656
17657 if (load_p)
17658 {
17659 reg1 = operands[0];
17660 reg2 = operands[2];
17661 mem1 = operands[1];
17662 mem2 = operands[3];
17663 }
17664 else
17665 {
17666 reg1 = operands[1];
17667 reg2 = operands[3];
17668 mem1 = operands[0];
17669 mem2 = operands[2];
17670 }
17671
17672 if (REGNO (reg2) == REGNO (reg1) + 1)
17673 return umips_load_store_pair_p_1 (load_p, false, reg1, mem1, mem2);
17674
17675 if (REGNO (reg1) == REGNO (reg2) + 1)
17676 return umips_load_store_pair_p_1 (load_p, true, reg2, mem2, mem1);
17677
17678 return false;
17679 }
17680
17681 /* Return the assembly instruction for a microMIPS LWP or SWP in which
17682 the first register is REG and the first memory slot is MEM.
17683 LOAD_P is true for LWP. */
17684
17685 static void
17686 umips_output_load_store_pair_1 (bool load_p, rtx reg, rtx mem)
17687 {
17688 rtx ops[] = {reg, mem};
17689
17690 if (load_p)
17691 output_asm_insn ("lwp\t%0,%1", ops);
17692 else
17693 output_asm_insn ("swp\t%0,%1", ops);
17694 }
17695
17696 /* Output the assembly instruction for a microMIPS LWP or SWP instruction.
17697 LOAD_P and OPERANDS are as for umips_load_store_pair_p. */
17698
17699 void
17700 umips_output_load_store_pair (bool load_p, rtx *operands)
17701 {
17702 rtx reg1, reg2, mem1, mem2;
17703 if (load_p)
17704 {
17705 reg1 = operands[0];
17706 reg2 = operands[2];
17707 mem1 = operands[1];
17708 mem2 = operands[3];
17709 }
17710 else
17711 {
17712 reg1 = operands[1];
17713 reg2 = operands[3];
17714 mem1 = operands[0];
17715 mem2 = operands[2];
17716 }
17717
17718 if (REGNO (reg2) == REGNO (reg1) + 1)
17719 {
17720 umips_output_load_store_pair_1 (load_p, reg1, mem1);
17721 return;
17722 }
17723
17724 gcc_assert (REGNO (reg1) == REGNO (reg2) + 1);
17725 umips_output_load_store_pair_1 (load_p, reg2, mem2);
17726 }
17727
17728 /* Return true if REG1 and REG2 match the criteria for a movep insn. */
17729
17730 bool
17731 umips_movep_target_p (rtx reg1, rtx reg2)
17732 {
17733 int regno1, regno2, pair;
17734 unsigned int i;
17735 static const int match[8] = {
17736 0x00000060, /* 5, 6 */
17737 0x000000a0, /* 5, 7 */
17738 0x000000c0, /* 6, 7 */
17739 0x00200010, /* 4, 21 */
17740 0x00400010, /* 4, 22 */
17741 0x00000030, /* 4, 5 */
17742 0x00000050, /* 4, 6 */
17743 0x00000090 /* 4, 7 */
17744 };
17745
17746 if (!REG_P (reg1) || !REG_P (reg2))
17747 return false;
17748
17749 regno1 = REGNO (reg1);
17750 regno2 = REGNO (reg2);
17751
17752 if (!GP_REG_P (regno1) || !GP_REG_P (regno2))
17753 return false;
17754
17755 pair = (1 << regno1) | (1 << regno2);
17756
17757 for (i = 0; i < ARRAY_SIZE (match); i++)
17758 if (pair == match[i])
17759 return true;
17760
17761 return false;
17762 }
17763 \f
17764 /* Return the size in bytes of the trampoline code, padded to
17765 TRAMPOLINE_ALIGNMENT bits. The static chain pointer and target
17766 function address immediately follow. */
17767
17768 int
17769 mips_trampoline_code_size (void)
17770 {
17771 if (TARGET_USE_PIC_FN_ADDR_REG)
17772 return 4 * 4;
17773 else if (ptr_mode == DImode)
17774 return 8 * 4;
17775 else if (ISA_HAS_LOAD_DELAY)
17776 return 6 * 4;
17777 else
17778 return 4 * 4;
17779 }
17780
17781 /* Implement TARGET_TRAMPOLINE_INIT. */
17782
17783 static void
17784 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
17785 {
17786 rtx addr, end_addr, high, low, opcode, mem;
17787 rtx trampoline[8];
17788 unsigned int i, j;
17789 HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
17790
17791 /* Work out the offsets of the pointers from the start of the
17792 trampoline code. */
17793 end_addr_offset = mips_trampoline_code_size ();
17794 static_chain_offset = end_addr_offset;
17795 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
17796
17797 /* Get pointers to the beginning and end of the code block. */
17798 addr = force_reg (Pmode, XEXP (m_tramp, 0));
17799 end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
17800
17801 #define OP(X) gen_int_mode (X, SImode)
17802
17803 /* Build up the code in TRAMPOLINE. */
17804 i = 0;
17805 if (TARGET_USE_PIC_FN_ADDR_REG)
17806 {
17807 /* $25 contains the address of the trampoline. Emit code of the form:
17808
17809 l[wd] $1, target_function_offset($25)
17810 l[wd] $static_chain, static_chain_offset($25)
17811 jr $1
17812 move $25,$1. */
17813 trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
17814 target_function_offset,
17815 PIC_FUNCTION_ADDR_REGNUM));
17816 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17817 static_chain_offset,
17818 PIC_FUNCTION_ADDR_REGNUM));
17819 trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
17820 trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
17821 }
17822 else if (ptr_mode == DImode)
17823 {
17824 /* It's too cumbersome to create the full 64-bit address, so let's
17825 instead use:
17826
17827 move $1, $31
17828 bal 1f
17829 nop
17830 1: l[wd] $25, target_function_offset - 12($31)
17831 l[wd] $static_chain, static_chain_offset - 12($31)
17832 jr $25
17833 move $31, $1
17834
17835 where 12 is the offset of "1:" from the start of the code block. */
17836 trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
17837 trampoline[i++] = OP (MIPS_BAL (1));
17838 trampoline[i++] = OP (MIPS_NOP);
17839 trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
17840 target_function_offset - 12,
17841 RETURN_ADDR_REGNUM));
17842 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17843 static_chain_offset - 12,
17844 RETURN_ADDR_REGNUM));
17845 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17846 trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
17847 }
17848 else
17849 {
17850 /* If the target has load delays, emit:
17851
17852 lui $1, %hi(end_addr)
17853 lw $25, %lo(end_addr + ...)($1)
17854 lw $static_chain, %lo(end_addr + ...)($1)
17855 jr $25
17856 nop
17857
17858 Otherwise emit:
17859
17860 lui $1, %hi(end_addr)
17861 lw $25, %lo(end_addr + ...)($1)
17862 jr $25
17863 lw $static_chain, %lo(end_addr + ...)($1). */
17864
17865 /* Split END_ADDR into %hi and %lo values. Trampolines are aligned
17866 to 64 bits, so the %lo value will have the bottom 3 bits clear. */
17867 high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
17868 NULL, false, OPTAB_WIDEN);
17869 high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
17870 NULL, false, OPTAB_WIDEN);
17871 low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
17872
17873 /* Emit the LUI. */
17874 opcode = OP (MIPS_LUI (AT_REGNUM, 0));
17875 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
17876 NULL, false, OPTAB_WIDEN);
17877
17878 /* Emit the load of the target function. */
17879 opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
17880 target_function_offset - end_addr_offset,
17881 AT_REGNUM));
17882 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
17883 NULL, false, OPTAB_WIDEN);
17884
17885 /* Emit the JR here, if we can. */
17886 if (!ISA_HAS_LOAD_DELAY)
17887 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17888
17889 /* Emit the load of the static chain register. */
17890 opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17891 static_chain_offset - end_addr_offset,
17892 AT_REGNUM));
17893 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
17894 NULL, false, OPTAB_WIDEN);
17895
17896 /* Emit the JR, if we couldn't above. */
17897 if (ISA_HAS_LOAD_DELAY)
17898 {
17899 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17900 trampoline[i++] = OP (MIPS_NOP);
17901 }
17902 }
17903
17904 #undef OP
17905
17906 /* Copy the trampoline code. Leave any padding uninitialized. */
17907 for (j = 0; j < i; j++)
17908 {
17909 mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
17910 mips_emit_move (mem, trampoline[j]);
17911 }
17912
17913 /* Set up the static chain pointer field. */
17914 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
17915 mips_emit_move (mem, chain_value);
17916
17917 /* Set up the target function field. */
17918 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
17919 mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
17920
17921 /* Flush the code part of the trampoline. */
17922 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
17923 emit_insn (gen_clear_cache (addr, end_addr));
17924 }
17925
17926 /* Implement FUNCTION_PROFILER. */
17927
17928 void mips_function_profiler (FILE *file)
17929 {
17930 if (TARGET_MIPS16)
17931 sorry ("mips16 function profiling");
17932 if (TARGET_LONG_CALLS)
17933 {
17934 /* For TARGET_LONG_CALLS use $3 for the address of _mcount. */
17935 if (Pmode == DImode)
17936 fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
17937 else
17938 fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
17939 }
17940 mips_push_asm_switch (&mips_noat);
17941 fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
17942 reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
17943 /* _mcount treats $2 as the static chain register. */
17944 if (cfun->static_chain_decl != NULL)
17945 fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
17946 reg_names[STATIC_CHAIN_REGNUM]);
17947 if (TARGET_MCOUNT_RA_ADDRESS)
17948 {
17949 /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
17950 ra save location. */
17951 if (cfun->machine->frame.ra_fp_offset == 0)
17952 /* ra not saved, pass zero. */
17953 fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
17954 else
17955 fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
17956 Pmode == DImode ? "dla" : "la", reg_names[12],
17957 cfun->machine->frame.ra_fp_offset,
17958 reg_names[STACK_POINTER_REGNUM]);
17959 }
17960 if (!TARGET_NEWABI)
17961 fprintf (file,
17962 "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from stack\n",
17963 TARGET_64BIT ? "dsubu" : "subu",
17964 reg_names[STACK_POINTER_REGNUM],
17965 reg_names[STACK_POINTER_REGNUM],
17966 Pmode == DImode ? 16 : 8);
17967
17968 if (TARGET_LONG_CALLS)
17969 fprintf (file, "\tjalr\t%s\n", reg_names[3]);
17970 else
17971 fprintf (file, "\tjal\t_mcount\n");
17972 mips_pop_asm_switch (&mips_noat);
17973 /* _mcount treats $2 as the static chain register. */
17974 if (cfun->static_chain_decl != NULL)
17975 fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
17976 reg_names[2]);
17977 }
17978
17979 /* Implement TARGET_SHIFT_TRUNCATION_MASK. We want to keep the default
17980 behaviour of TARGET_SHIFT_TRUNCATION_MASK for non-vector modes even
17981 when TARGET_LOONGSON_VECTORS is true. */
17982
17983 static unsigned HOST_WIDE_INT
17984 mips_shift_truncation_mask (enum machine_mode mode)
17985 {
17986 if (TARGET_LOONGSON_VECTORS && VECTOR_MODE_P (mode))
17987 return 0;
17988
17989 return GET_MODE_BITSIZE (mode) - 1;
17990 }
17991
17992 /* Implement TARGET_PREPARE_PCH_SAVE. */
17993
17994 static void
17995 mips_prepare_pch_save (void)
17996 {
17997 /* We are called in a context where the current MIPS16 vs. non-MIPS16
17998 setting should be irrelevant. The question then is: which setting
17999 makes most sense at load time?
18000
18001 The PCH is loaded before the first token is read. We should never
18002 have switched into MIPS16 mode by that point, and thus should not
18003 have populated mips16_globals. Nor can we load the entire contents
18004 of mips16_globals from the PCH file, because mips16_globals contains
18005 a combination of GGC and non-GGC data.
18006
18007 There is therefore no point in trying save the GGC part of
18008 mips16_globals to the PCH file, or to preserve MIPS16ness across
18009 the PCH save and load. The loading compiler would not have access
18010 to the non-GGC parts of mips16_globals (either from the PCH file,
18011 or from a copy that the loading compiler generated itself) and would
18012 have to call target_reinit anyway.
18013
18014 It therefore seems best to switch back to non-MIPS16 mode at
18015 save time, and to ensure that mips16_globals remains null after
18016 a PCH load. */
18017 mips_set_compression_mode (0);
18018 mips16_globals = 0;
18019 }
18020 \f
18021 /* Generate or test for an insn that supports a constant permutation. */
18022
18023 #define MAX_VECT_LEN 8
18024
18025 struct expand_vec_perm_d
18026 {
18027 rtx target, op0, op1;
18028 unsigned char perm[MAX_VECT_LEN];
18029 enum machine_mode vmode;
18030 unsigned char nelt;
18031 bool one_vector_p;
18032 bool testing_p;
18033 };
18034
18035 /* Construct (set target (vec_select op0 (parallel perm))) and
18036 return true if that's a valid instruction in the active ISA. */
18037
18038 static bool
18039 mips_expand_vselect (rtx target, rtx op0,
18040 const unsigned char *perm, unsigned nelt)
18041 {
18042 rtx rperm[MAX_VECT_LEN], x;
18043 unsigned i;
18044
18045 for (i = 0; i < nelt; ++i)
18046 rperm[i] = GEN_INT (perm[i]);
18047
18048 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
18049 x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
18050 x = gen_rtx_SET (VOIDmode, target, x);
18051
18052 x = emit_insn (x);
18053 if (recog_memoized (x) < 0)
18054 {
18055 remove_insn (x);
18056 return false;
18057 }
18058 return true;
18059 }
18060
18061 /* Similar, but generate a vec_concat from op0 and op1 as well. */
18062
18063 static bool
18064 mips_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
18065 const unsigned char *perm, unsigned nelt)
18066 {
18067 enum machine_mode v2mode;
18068 rtx x;
18069
18070 v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
18071 x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
18072 return mips_expand_vselect (target, x, perm, nelt);
18073 }
18074
18075 /* Recognize patterns for even-odd extraction. */
18076
18077 static bool
18078 mips_expand_vpc_loongson_even_odd (struct expand_vec_perm_d *d)
18079 {
18080 unsigned i, odd, nelt = d->nelt;
18081 rtx t0, t1, t2, t3;
18082
18083 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18084 return false;
18085 /* Even-odd for V2SI/V2SFmode is matched by interleave directly. */
18086 if (nelt < 4)
18087 return false;
18088
18089 odd = d->perm[0];
18090 if (odd > 1)
18091 return false;
18092 for (i = 1; i < nelt; ++i)
18093 if (d->perm[i] != i * 2 + odd)
18094 return false;
18095
18096 if (d->testing_p)
18097 return true;
18098
18099 /* We need 2*log2(N)-1 operations to achieve odd/even with interleave. */
18100 t0 = gen_reg_rtx (d->vmode);
18101 t1 = gen_reg_rtx (d->vmode);
18102 switch (d->vmode)
18103 {
18104 case V4HImode:
18105 emit_insn (gen_loongson_punpckhhw (t0, d->op0, d->op1));
18106 emit_insn (gen_loongson_punpcklhw (t1, d->op0, d->op1));
18107 if (odd)
18108 emit_insn (gen_loongson_punpckhhw (d->target, t1, t0));
18109 else
18110 emit_insn (gen_loongson_punpcklhw (d->target, t1, t0));
18111 break;
18112
18113 case V8QImode:
18114 t2 = gen_reg_rtx (d->vmode);
18115 t3 = gen_reg_rtx (d->vmode);
18116 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op1));
18117 emit_insn (gen_loongson_punpcklbh (t1, d->op0, d->op1));
18118 emit_insn (gen_loongson_punpckhbh (t2, t1, t0));
18119 emit_insn (gen_loongson_punpcklbh (t3, t1, t0));
18120 if (odd)
18121 emit_insn (gen_loongson_punpckhbh (d->target, t3, t2));
18122 else
18123 emit_insn (gen_loongson_punpcklbh (d->target, t3, t2));
18124 break;
18125
18126 default:
18127 gcc_unreachable ();
18128 }
18129 return true;
18130 }
18131
18132 /* Recognize patterns for the Loongson PSHUFH instruction. */
18133
18134 static bool
18135 mips_expand_vpc_loongson_pshufh (struct expand_vec_perm_d *d)
18136 {
18137 unsigned i, mask;
18138 rtx rmask;
18139
18140 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18141 return false;
18142 if (d->vmode != V4HImode)
18143 return false;
18144 if (d->testing_p)
18145 return true;
18146
18147 /* Convert the selector into the packed 8-bit form for pshufh. */
18148 /* Recall that loongson is little-endian only. No big-endian
18149 adjustment required. */
18150 for (i = mask = 0; i < 4; i++)
18151 mask |= (d->perm[i] & 3) << (i * 2);
18152 rmask = force_reg (SImode, GEN_INT (mask));
18153
18154 if (d->one_vector_p)
18155 emit_insn (gen_loongson_pshufh (d->target, d->op0, rmask));
18156 else
18157 {
18158 rtx t0, t1, x, merge, rmerge[4];
18159
18160 t0 = gen_reg_rtx (V4HImode);
18161 t1 = gen_reg_rtx (V4HImode);
18162 emit_insn (gen_loongson_pshufh (t1, d->op1, rmask));
18163 emit_insn (gen_loongson_pshufh (t0, d->op0, rmask));
18164
18165 for (i = 0; i < 4; ++i)
18166 rmerge[i] = (d->perm[i] & 4 ? constm1_rtx : const0_rtx);
18167 merge = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmerge));
18168 merge = force_reg (V4HImode, merge);
18169
18170 x = gen_rtx_AND (V4HImode, merge, t1);
18171 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18172
18173 x = gen_rtx_NOT (V4HImode, merge);
18174 x = gen_rtx_AND (V4HImode, x, t0);
18175 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18176
18177 x = gen_rtx_IOR (V4HImode, t0, t1);
18178 emit_insn (gen_rtx_SET (VOIDmode, d->target, x));
18179 }
18180
18181 return true;
18182 }
18183
18184 /* Recognize broadcast patterns for the Loongson. */
18185
18186 static bool
18187 mips_expand_vpc_loongson_bcast (struct expand_vec_perm_d *d)
18188 {
18189 unsigned i, elt;
18190 rtx t0, t1;
18191
18192 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18193 return false;
18194 /* Note that we've already matched V2SI via punpck and V4HI via pshufh. */
18195 if (d->vmode != V8QImode)
18196 return false;
18197 if (!d->one_vector_p)
18198 return false;
18199
18200 elt = d->perm[0];
18201 for (i = 1; i < 8; ++i)
18202 if (d->perm[i] != elt)
18203 return false;
18204
18205 if (d->testing_p)
18206 return true;
18207
18208 /* With one interleave we put two of the desired element adjacent. */
18209 t0 = gen_reg_rtx (V8QImode);
18210 if (elt < 4)
18211 emit_insn (gen_loongson_punpcklbh (t0, d->op0, d->op0));
18212 else
18213 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op0));
18214
18215 /* Shuffle that one HImode element into all locations. */
18216 elt &= 3;
18217 elt *= 0x55;
18218 t1 = gen_reg_rtx (V4HImode);
18219 emit_insn (gen_loongson_pshufh (t1, gen_lowpart (V4HImode, t0),
18220 force_reg (SImode, GEN_INT (elt))));
18221
18222 emit_move_insn (d->target, gen_lowpart (V8QImode, t1));
18223 return true;
18224 }
18225
18226 static bool
18227 mips_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
18228 {
18229 unsigned int i, nelt = d->nelt;
18230 unsigned char perm2[MAX_VECT_LEN];
18231
18232 if (d->one_vector_p)
18233 {
18234 /* Try interleave with alternating operands. */
18235 memcpy (perm2, d->perm, sizeof(perm2));
18236 for (i = 1; i < nelt; i += 2)
18237 perm2[i] += nelt;
18238 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1, perm2, nelt))
18239 return true;
18240 }
18241 else
18242 {
18243 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1,
18244 d->perm, nelt))
18245 return true;
18246
18247 /* Try again with swapped operands. */
18248 for (i = 0; i < nelt; ++i)
18249 perm2[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
18250 if (mips_expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
18251 return true;
18252 }
18253
18254 if (mips_expand_vpc_loongson_even_odd (d))
18255 return true;
18256 if (mips_expand_vpc_loongson_pshufh (d))
18257 return true;
18258 if (mips_expand_vpc_loongson_bcast (d))
18259 return true;
18260 return false;
18261 }
18262
18263 /* Expand a vec_perm_const pattern. */
18264
18265 bool
18266 mips_expand_vec_perm_const (rtx operands[4])
18267 {
18268 struct expand_vec_perm_d d;
18269 int i, nelt, which;
18270 unsigned char orig_perm[MAX_VECT_LEN];
18271 rtx sel;
18272 bool ok;
18273
18274 d.target = operands[0];
18275 d.op0 = operands[1];
18276 d.op1 = operands[2];
18277 sel = operands[3];
18278
18279 d.vmode = GET_MODE (d.target);
18280 gcc_assert (VECTOR_MODE_P (d.vmode));
18281 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18282 d.testing_p = false;
18283
18284 for (i = which = 0; i < nelt; ++i)
18285 {
18286 rtx e = XVECEXP (sel, 0, i);
18287 int ei = INTVAL (e) & (2 * nelt - 1);
18288 which |= (ei < nelt ? 1 : 2);
18289 orig_perm[i] = ei;
18290 }
18291 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18292
18293 switch (which)
18294 {
18295 default:
18296 gcc_unreachable();
18297
18298 case 3:
18299 d.one_vector_p = false;
18300 if (!rtx_equal_p (d.op0, d.op1))
18301 break;
18302 /* FALLTHRU */
18303
18304 case 2:
18305 for (i = 0; i < nelt; ++i)
18306 d.perm[i] &= nelt - 1;
18307 d.op0 = d.op1;
18308 d.one_vector_p = true;
18309 break;
18310
18311 case 1:
18312 d.op1 = d.op0;
18313 d.one_vector_p = true;
18314 break;
18315 }
18316
18317 ok = mips_expand_vec_perm_const_1 (&d);
18318
18319 /* If we were given a two-vector permutation which just happened to
18320 have both input vectors equal, we folded this into a one-vector
18321 permutation. There are several loongson patterns that are matched
18322 via direct vec_select+vec_concat expansion, but we do not have
18323 support in mips_expand_vec_perm_const_1 to guess the adjustment
18324 that should be made for a single operand. Just try again with
18325 the original permutation. */
18326 if (!ok && which == 3)
18327 {
18328 d.op0 = operands[1];
18329 d.op1 = operands[2];
18330 d.one_vector_p = false;
18331 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18332 ok = mips_expand_vec_perm_const_1 (&d);
18333 }
18334
18335 return ok;
18336 }
18337
18338 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST_OK. */
18339
18340 static bool
18341 mips_vectorize_vec_perm_const_ok (enum machine_mode vmode,
18342 const unsigned char *sel)
18343 {
18344 struct expand_vec_perm_d d;
18345 unsigned int i, nelt, which;
18346 bool ret;
18347
18348 d.vmode = vmode;
18349 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18350 d.testing_p = true;
18351 memcpy (d.perm, sel, nelt);
18352
18353 /* Categorize the set of elements in the selector. */
18354 for (i = which = 0; i < nelt; ++i)
18355 {
18356 unsigned char e = d.perm[i];
18357 gcc_assert (e < 2 * nelt);
18358 which |= (e < nelt ? 1 : 2);
18359 }
18360
18361 /* For all elements from second vector, fold the elements to first. */
18362 if (which == 2)
18363 for (i = 0; i < nelt; ++i)
18364 d.perm[i] -= nelt;
18365
18366 /* Check whether the mask can be applied to the vector type. */
18367 d.one_vector_p = (which != 3);
18368
18369 d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
18370 d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
18371 if (!d.one_vector_p)
18372 d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
18373
18374 start_sequence ();
18375 ret = mips_expand_vec_perm_const_1 (&d);
18376 end_sequence ();
18377
18378 return ret;
18379 }
18380
18381 /* Expand an integral vector unpack operation. */
18382
18383 void
18384 mips_expand_vec_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18385 {
18386 enum machine_mode imode = GET_MODE (operands[1]);
18387 rtx (*unpack) (rtx, rtx, rtx);
18388 rtx (*cmpgt) (rtx, rtx, rtx);
18389 rtx tmp, dest, zero;
18390
18391 switch (imode)
18392 {
18393 case V8QImode:
18394 if (high_p)
18395 unpack = gen_loongson_punpckhbh;
18396 else
18397 unpack = gen_loongson_punpcklbh;
18398 cmpgt = gen_loongson_pcmpgtb;
18399 break;
18400 case V4HImode:
18401 if (high_p)
18402 unpack = gen_loongson_punpckhhw;
18403 else
18404 unpack = gen_loongson_punpcklhw;
18405 cmpgt = gen_loongson_pcmpgth;
18406 break;
18407 default:
18408 gcc_unreachable ();
18409 }
18410
18411 zero = force_reg (imode, CONST0_RTX (imode));
18412 if (unsigned_p)
18413 tmp = zero;
18414 else
18415 {
18416 tmp = gen_reg_rtx (imode);
18417 emit_insn (cmpgt (tmp, zero, operands[1]));
18418 }
18419
18420 dest = gen_reg_rtx (imode);
18421 emit_insn (unpack (dest, operands[1], tmp));
18422
18423 emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
18424 }
18425
18426 /* A subroutine of mips_expand_vec_init, match constant vector elements. */
18427
18428 static inline bool
18429 mips_constant_elt_p (rtx x)
18430 {
18431 return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
18432 }
18433
18434 /* A subroutine of mips_expand_vec_init, expand via broadcast. */
18435
18436 static void
18437 mips_expand_vi_broadcast (enum machine_mode vmode, rtx target, rtx elt)
18438 {
18439 struct expand_vec_perm_d d;
18440 rtx t1;
18441 bool ok;
18442
18443 if (elt != const0_rtx)
18444 elt = force_reg (GET_MODE_INNER (vmode), elt);
18445 if (REG_P (elt))
18446 elt = gen_lowpart (DImode, elt);
18447
18448 t1 = gen_reg_rtx (vmode);
18449 switch (vmode)
18450 {
18451 case V8QImode:
18452 emit_insn (gen_loongson_vec_init1_v8qi (t1, elt));
18453 break;
18454 case V4HImode:
18455 emit_insn (gen_loongson_vec_init1_v4hi (t1, elt));
18456 break;
18457 default:
18458 gcc_unreachable ();
18459 }
18460
18461 memset (&d, 0, sizeof (d));
18462 d.target = target;
18463 d.op0 = t1;
18464 d.op1 = t1;
18465 d.vmode = vmode;
18466 d.nelt = GET_MODE_NUNITS (vmode);
18467 d.one_vector_p = true;
18468
18469 ok = mips_expand_vec_perm_const_1 (&d);
18470 gcc_assert (ok);
18471 }
18472
18473 /* A subroutine of mips_expand_vec_init, replacing all of the non-constant
18474 elements of VALS with zeros, copy the constant vector to TARGET. */
18475
18476 static void
18477 mips_expand_vi_constant (enum machine_mode vmode, unsigned nelt,
18478 rtx target, rtx vals)
18479 {
18480 rtvec vec = shallow_copy_rtvec (XVEC (vals, 0));
18481 unsigned i;
18482
18483 for (i = 0; i < nelt; ++i)
18484 {
18485 if (!mips_constant_elt_p (RTVEC_ELT (vec, i)))
18486 RTVEC_ELT (vec, i) = const0_rtx;
18487 }
18488
18489 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, vec));
18490 }
18491
18492
18493 /* A subroutine of mips_expand_vec_init, expand via pinsrh. */
18494
18495 static void
18496 mips_expand_vi_loongson_one_pinsrh (rtx target, rtx vals, unsigned one_var)
18497 {
18498 mips_expand_vi_constant (V4HImode, 4, target, vals);
18499
18500 emit_insn (gen_vec_setv4hi (target, target, XVECEXP (vals, 0, one_var),
18501 GEN_INT (one_var)));
18502 }
18503
18504 /* A subroutine of mips_expand_vec_init, expand anything via memory. */
18505
18506 static void
18507 mips_expand_vi_general (enum machine_mode vmode, enum machine_mode imode,
18508 unsigned nelt, unsigned nvar, rtx target, rtx vals)
18509 {
18510 rtx mem = assign_stack_temp (vmode, GET_MODE_SIZE (vmode));
18511 unsigned int i, isize = GET_MODE_SIZE (imode);
18512
18513 if (nvar < nelt)
18514 mips_expand_vi_constant (vmode, nelt, mem, vals);
18515
18516 for (i = 0; i < nelt; ++i)
18517 {
18518 rtx x = XVECEXP (vals, 0, i);
18519 if (!mips_constant_elt_p (x))
18520 emit_move_insn (adjust_address (mem, imode, i * isize), x);
18521 }
18522
18523 emit_move_insn (target, mem);
18524 }
18525
18526 /* Expand a vector initialization. */
18527
18528 void
18529 mips_expand_vector_init (rtx target, rtx vals)
18530 {
18531 enum machine_mode vmode = GET_MODE (target);
18532 enum machine_mode imode = GET_MODE_INNER (vmode);
18533 unsigned i, nelt = GET_MODE_NUNITS (vmode);
18534 unsigned nvar = 0, one_var = -1u;
18535 bool all_same = true;
18536 rtx x;
18537
18538 for (i = 0; i < nelt; ++i)
18539 {
18540 x = XVECEXP (vals, 0, i);
18541 if (!mips_constant_elt_p (x))
18542 nvar++, one_var = i;
18543 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
18544 all_same = false;
18545 }
18546
18547 /* Load constants from the pool, or whatever's handy. */
18548 if (nvar == 0)
18549 {
18550 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
18551 return;
18552 }
18553
18554 /* For two-part initialization, always use CONCAT. */
18555 if (nelt == 2)
18556 {
18557 rtx op0 = force_reg (imode, XVECEXP (vals, 0, 0));
18558 rtx op1 = force_reg (imode, XVECEXP (vals, 0, 1));
18559 x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
18560 emit_insn (gen_rtx_SET (VOIDmode, target, x));
18561 return;
18562 }
18563
18564 /* Loongson is the only cpu with vectors with more elements. */
18565 gcc_assert (TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS);
18566
18567 /* If all values are identical, broadcast the value. */
18568 if (all_same)
18569 {
18570 mips_expand_vi_broadcast (vmode, target, XVECEXP (vals, 0, 0));
18571 return;
18572 }
18573
18574 /* If we've only got one non-variable V4HImode, use PINSRH. */
18575 if (nvar == 1 && vmode == V4HImode)
18576 {
18577 mips_expand_vi_loongson_one_pinsrh (target, vals, one_var);
18578 return;
18579 }
18580
18581 mips_expand_vi_general (vmode, imode, nelt, nvar, target, vals);
18582 }
18583
18584 /* Expand a vector reduction. */
18585
18586 void
18587 mips_expand_vec_reduc (rtx target, rtx in, rtx (*gen)(rtx, rtx, rtx))
18588 {
18589 enum machine_mode vmode = GET_MODE (in);
18590 unsigned char perm2[2];
18591 rtx last, next, fold, x;
18592 bool ok;
18593
18594 last = in;
18595 fold = gen_reg_rtx (vmode);
18596 switch (vmode)
18597 {
18598 case V2SFmode:
18599 /* Use PUL/PLU to produce { L, H } op { H, L }.
18600 By reversing the pair order, rather than a pure interleave high,
18601 we avoid erroneous exceptional conditions that we might otherwise
18602 produce from the computation of H op H. */
18603 perm2[0] = 1;
18604 perm2[1] = 2;
18605 ok = mips_expand_vselect_vconcat (fold, last, last, perm2, 2);
18606 gcc_assert (ok);
18607 break;
18608
18609 case V2SImode:
18610 /* Use interleave to produce { H, L } op { H, H }. */
18611 emit_insn (gen_loongson_punpckhwd (fold, last, last));
18612 break;
18613
18614 case V4HImode:
18615 /* Perform the first reduction with interleave,
18616 and subsequent reductions with shifts. */
18617 emit_insn (gen_loongson_punpckhwd_hi (fold, last, last));
18618
18619 next = gen_reg_rtx (vmode);
18620 emit_insn (gen (next, last, fold));
18621 last = next;
18622
18623 fold = gen_reg_rtx (vmode);
18624 x = force_reg (SImode, GEN_INT (16));
18625 emit_insn (gen_vec_shr_v4hi (fold, last, x));
18626 break;
18627
18628 case V8QImode:
18629 emit_insn (gen_loongson_punpckhwd_qi (fold, last, last));
18630
18631 next = gen_reg_rtx (vmode);
18632 emit_insn (gen (next, last, fold));
18633 last = next;
18634
18635 fold = gen_reg_rtx (vmode);
18636 x = force_reg (SImode, GEN_INT (16));
18637 emit_insn (gen_vec_shr_v8qi (fold, last, x));
18638
18639 next = gen_reg_rtx (vmode);
18640 emit_insn (gen (next, last, fold));
18641 last = next;
18642
18643 fold = gen_reg_rtx (vmode);
18644 x = force_reg (SImode, GEN_INT (8));
18645 emit_insn (gen_vec_shr_v8qi (fold, last, x));
18646 break;
18647
18648 default:
18649 gcc_unreachable ();
18650 }
18651
18652 emit_insn (gen (target, last, fold));
18653 }
18654
18655 /* Expand a vector minimum/maximum. */
18656
18657 void
18658 mips_expand_vec_minmax (rtx target, rtx op0, rtx op1,
18659 rtx (*cmp) (rtx, rtx, rtx), bool min_p)
18660 {
18661 enum machine_mode vmode = GET_MODE (target);
18662 rtx tc, t0, t1, x;
18663
18664 tc = gen_reg_rtx (vmode);
18665 t0 = gen_reg_rtx (vmode);
18666 t1 = gen_reg_rtx (vmode);
18667
18668 /* op0 > op1 */
18669 emit_insn (cmp (tc, op0, op1));
18670
18671 x = gen_rtx_AND (vmode, tc, (min_p ? op1 : op0));
18672 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18673
18674 x = gen_rtx_NOT (vmode, tc);
18675 x = gen_rtx_AND (vmode, x, (min_p ? op0 : op1));
18676 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18677
18678 x = gen_rtx_IOR (vmode, t0, t1);
18679 emit_insn (gen_rtx_SET (VOIDmode, target, x));
18680 }
18681
18682 /* Implement TARGET_CASE_VALUES_THRESHOLD. */
18683
18684 unsigned int
18685 mips_case_values_threshold (void)
18686 {
18687 /* In MIPS16 mode using a larger case threshold generates smaller code. */
18688 if (TARGET_MIPS16 && optimize_size)
18689 return 10;
18690 else
18691 return default_case_values_threshold ();
18692 }
18693 \f
18694 /* Initialize the GCC target structure. */
18695 #undef TARGET_ASM_ALIGNED_HI_OP
18696 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
18697 #undef TARGET_ASM_ALIGNED_SI_OP
18698 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
18699 #undef TARGET_ASM_ALIGNED_DI_OP
18700 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
18701
18702 #undef TARGET_OPTION_OVERRIDE
18703 #define TARGET_OPTION_OVERRIDE mips_option_override
18704
18705 #undef TARGET_LEGITIMIZE_ADDRESS
18706 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
18707
18708 #undef TARGET_ASM_FUNCTION_PROLOGUE
18709 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
18710 #undef TARGET_ASM_FUNCTION_EPILOGUE
18711 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
18712 #undef TARGET_ASM_SELECT_RTX_SECTION
18713 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
18714 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
18715 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
18716
18717 #undef TARGET_SCHED_INIT
18718 #define TARGET_SCHED_INIT mips_sched_init
18719 #undef TARGET_SCHED_REORDER
18720 #define TARGET_SCHED_REORDER mips_sched_reorder
18721 #undef TARGET_SCHED_REORDER2
18722 #define TARGET_SCHED_REORDER2 mips_sched_reorder2
18723 #undef TARGET_SCHED_VARIABLE_ISSUE
18724 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
18725 #undef TARGET_SCHED_ADJUST_COST
18726 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
18727 #undef TARGET_SCHED_ISSUE_RATE
18728 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
18729 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
18730 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
18731 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
18732 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
18733 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
18734 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
18735 mips_multipass_dfa_lookahead
18736 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
18737 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
18738 mips_small_register_classes_for_mode_p
18739
18740 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
18741 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
18742
18743 #undef TARGET_INSERT_ATTRIBUTES
18744 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
18745 #undef TARGET_MERGE_DECL_ATTRIBUTES
18746 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
18747 #undef TARGET_CAN_INLINE_P
18748 #define TARGET_CAN_INLINE_P mips_can_inline_p
18749 #undef TARGET_SET_CURRENT_FUNCTION
18750 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
18751
18752 #undef TARGET_VALID_POINTER_MODE
18753 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
18754 #undef TARGET_REGISTER_MOVE_COST
18755 #define TARGET_REGISTER_MOVE_COST mips_register_move_cost
18756 #undef TARGET_MEMORY_MOVE_COST
18757 #define TARGET_MEMORY_MOVE_COST mips_memory_move_cost
18758 #undef TARGET_RTX_COSTS
18759 #define TARGET_RTX_COSTS mips_rtx_costs
18760 #undef TARGET_ADDRESS_COST
18761 #define TARGET_ADDRESS_COST mips_address_cost
18762
18763 #undef TARGET_IN_SMALL_DATA_P
18764 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
18765
18766 #undef TARGET_MACHINE_DEPENDENT_REORG
18767 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
18768
18769 #undef TARGET_PREFERRED_RELOAD_CLASS
18770 #define TARGET_PREFERRED_RELOAD_CLASS mips_preferred_reload_class
18771
18772 #undef TARGET_EXPAND_TO_RTL_HOOK
18773 #define TARGET_EXPAND_TO_RTL_HOOK mips_expand_to_rtl_hook
18774 #undef TARGET_ASM_FILE_START
18775 #define TARGET_ASM_FILE_START mips_file_start
18776 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
18777 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
18778 #undef TARGET_ASM_CODE_END
18779 #define TARGET_ASM_CODE_END mips_code_end
18780
18781 #undef TARGET_INIT_LIBFUNCS
18782 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
18783
18784 #undef TARGET_BUILD_BUILTIN_VA_LIST
18785 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
18786 #undef TARGET_EXPAND_BUILTIN_VA_START
18787 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
18788 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
18789 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
18790
18791 #undef TARGET_PROMOTE_FUNCTION_MODE
18792 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
18793 #undef TARGET_PROMOTE_PROTOTYPES
18794 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
18795
18796 #undef TARGET_FUNCTION_VALUE
18797 #define TARGET_FUNCTION_VALUE mips_function_value
18798 #undef TARGET_LIBCALL_VALUE
18799 #define TARGET_LIBCALL_VALUE mips_libcall_value
18800 #undef TARGET_FUNCTION_VALUE_REGNO_P
18801 #define TARGET_FUNCTION_VALUE_REGNO_P mips_function_value_regno_p
18802 #undef TARGET_RETURN_IN_MEMORY
18803 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
18804 #undef TARGET_RETURN_IN_MSB
18805 #define TARGET_RETURN_IN_MSB mips_return_in_msb
18806
18807 #undef TARGET_ASM_OUTPUT_MI_THUNK
18808 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
18809 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
18810 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
18811
18812 #undef TARGET_PRINT_OPERAND
18813 #define TARGET_PRINT_OPERAND mips_print_operand
18814 #undef TARGET_PRINT_OPERAND_ADDRESS
18815 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
18816 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
18817 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
18818
18819 #undef TARGET_SETUP_INCOMING_VARARGS
18820 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
18821 #undef TARGET_STRICT_ARGUMENT_NAMING
18822 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
18823 #undef TARGET_MUST_PASS_IN_STACK
18824 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
18825 #undef TARGET_PASS_BY_REFERENCE
18826 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
18827 #undef TARGET_CALLEE_COPIES
18828 #define TARGET_CALLEE_COPIES mips_callee_copies
18829 #undef TARGET_ARG_PARTIAL_BYTES
18830 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
18831 #undef TARGET_FUNCTION_ARG
18832 #define TARGET_FUNCTION_ARG mips_function_arg
18833 #undef TARGET_FUNCTION_ARG_ADVANCE
18834 #define TARGET_FUNCTION_ARG_ADVANCE mips_function_arg_advance
18835 #undef TARGET_FUNCTION_ARG_BOUNDARY
18836 #define TARGET_FUNCTION_ARG_BOUNDARY mips_function_arg_boundary
18837
18838 #undef TARGET_MODE_REP_EXTENDED
18839 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
18840
18841 #undef TARGET_VECTOR_MODE_SUPPORTED_P
18842 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
18843
18844 #undef TARGET_SCALAR_MODE_SUPPORTED_P
18845 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
18846
18847 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
18848 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
18849
18850 #undef TARGET_INIT_BUILTINS
18851 #define TARGET_INIT_BUILTINS mips_init_builtins
18852 #undef TARGET_BUILTIN_DECL
18853 #define TARGET_BUILTIN_DECL mips_builtin_decl
18854 #undef TARGET_EXPAND_BUILTIN
18855 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
18856
18857 #undef TARGET_HAVE_TLS
18858 #define TARGET_HAVE_TLS HAVE_AS_TLS
18859
18860 #undef TARGET_CANNOT_FORCE_CONST_MEM
18861 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
18862
18863 #undef TARGET_LEGITIMATE_CONSTANT_P
18864 #define TARGET_LEGITIMATE_CONSTANT_P mips_legitimate_constant_p
18865
18866 #undef TARGET_ENCODE_SECTION_INFO
18867 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
18868
18869 #undef TARGET_ATTRIBUTE_TABLE
18870 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
18871 /* All our function attributes are related to how out-of-line copies should
18872 be compiled or called. They don't in themselves prevent inlining. */
18873 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
18874 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
18875
18876 #undef TARGET_EXTRA_LIVE_ON_ENTRY
18877 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
18878
18879 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
18880 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
18881 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
18882 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
18883
18884 #undef TARGET_COMP_TYPE_ATTRIBUTES
18885 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
18886
18887 #ifdef HAVE_AS_DTPRELWORD
18888 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
18889 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
18890 #endif
18891 #undef TARGET_DWARF_REGISTER_SPAN
18892 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
18893
18894 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
18895 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
18896
18897 #undef TARGET_LEGITIMATE_ADDRESS_P
18898 #define TARGET_LEGITIMATE_ADDRESS_P mips_legitimate_address_p
18899
18900 #undef TARGET_FRAME_POINTER_REQUIRED
18901 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
18902
18903 #undef TARGET_CAN_ELIMINATE
18904 #define TARGET_CAN_ELIMINATE mips_can_eliminate
18905
18906 #undef TARGET_CONDITIONAL_REGISTER_USAGE
18907 #define TARGET_CONDITIONAL_REGISTER_USAGE mips_conditional_register_usage
18908
18909 #undef TARGET_TRAMPOLINE_INIT
18910 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
18911
18912 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
18913 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
18914
18915 #undef TARGET_SHIFT_TRUNCATION_MASK
18916 #define TARGET_SHIFT_TRUNCATION_MASK mips_shift_truncation_mask
18917
18918 #undef TARGET_PREPARE_PCH_SAVE
18919 #define TARGET_PREPARE_PCH_SAVE mips_prepare_pch_save
18920
18921 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
18922 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK mips_vectorize_vec_perm_const_ok
18923
18924 #undef TARGET_CASE_VALUES_THRESHOLD
18925 #define TARGET_CASE_VALUES_THRESHOLD mips_case_values_threshold
18926
18927 struct gcc_target targetm = TARGET_INITIALIZER;
18928 \f
18929 #include "gt-mips.h"