]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/mips/mips.c
mips.c (mips_isa_rev): New variable.
[thirdparty/gcc.git] / gcc / config / mips / mips.c
1 /* Subroutines used for MIPS code generation.
2 Copyright (C) 1989-2014 Free Software Foundation, Inc.
3 Contributed by A. Lichnewsky, lich@inria.inria.fr.
4 Changes by Michael Meissner, meissner@osf.org.
5 64-bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
6 Brendan Eich, brendan@microunity.com.
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 "pointer-set.h"
58 #include "vec.h"
59 #include "basic-block.h"
60 #include "tree-ssa-alias.h"
61 #include "internal-fn.h"
62 #include "gimple-fold.h"
63 #include "tree-eh.h"
64 #include "gimple-expr.h"
65 #include "is-a.h"
66 #include "gimple.h"
67 #include "gimplify.h"
68 #include "bitmap.h"
69 #include "diagnostic.h"
70 #include "target-globals.h"
71 #include "opts.h"
72 #include "tree-pass.h"
73 #include "context.h"
74
75 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
76 #define UNSPEC_ADDRESS_P(X) \
77 (GET_CODE (X) == UNSPEC \
78 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
79 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
80
81 /* Extract the symbol or label from UNSPEC wrapper X. */
82 #define UNSPEC_ADDRESS(X) \
83 XVECEXP (X, 0, 0)
84
85 /* Extract the symbol type from UNSPEC wrapper X. */
86 #define UNSPEC_ADDRESS_TYPE(X) \
87 ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
88
89 /* The maximum distance between the top of the stack frame and the
90 value $sp has when we save and restore registers.
91
92 The value for normal-mode code must be a SMALL_OPERAND and must
93 preserve the maximum stack alignment. We therefore use a value
94 of 0x7ff0 in this case.
95
96 microMIPS LWM and SWM support 12-bit offsets (from -0x800 to 0x7ff),
97 so we use a maximum of 0x7f0 for TARGET_MICROMIPS.
98
99 MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
100 up to 0x7f8 bytes and can usually save or restore all the registers
101 that we need to save or restore. (Note that we can only use these
102 instructions for o32, for which the stack alignment is 8 bytes.)
103
104 We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
105 RESTORE are not available. We can then use unextended instructions
106 to save and restore registers, and to allocate and deallocate the top
107 part of the frame. */
108 #define MIPS_MAX_FIRST_STACK_STEP \
109 (!TARGET_COMPRESSION ? 0x7ff0 \
110 : TARGET_MICROMIPS || GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8 \
111 : TARGET_64BIT ? 0x100 : 0x400)
112
113 /* True if INSN is a mips.md pattern or asm statement. */
114 /* ??? This test exists through the compiler, perhaps it should be
115 moved to rtl.h. */
116 #define USEFUL_INSN_P(INSN) \
117 (NONDEBUG_INSN_P (INSN) \
118 && GET_CODE (PATTERN (INSN)) != USE \
119 && GET_CODE (PATTERN (INSN)) != CLOBBER)
120
121 /* If INSN is a delayed branch sequence, return the first instruction
122 in the sequence, otherwise return INSN itself. */
123 #define SEQ_BEGIN(INSN) \
124 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
125 ? XVECEXP (PATTERN (INSN), 0, 0) \
126 : (INSN))
127
128 /* Likewise for the last instruction in a delayed branch sequence. */
129 #define SEQ_END(INSN) \
130 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
131 ? XVECEXP (PATTERN (INSN), 0, XVECLEN (PATTERN (INSN), 0) - 1) \
132 : (INSN))
133
134 /* Execute the following loop body with SUBINSN set to each instruction
135 between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive. */
136 #define FOR_EACH_SUBINSN(SUBINSN, INSN) \
137 for ((SUBINSN) = SEQ_BEGIN (INSN); \
138 (SUBINSN) != NEXT_INSN (SEQ_END (INSN)); \
139 (SUBINSN) = NEXT_INSN (SUBINSN))
140
141 /* True if bit BIT is set in VALUE. */
142 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
143
144 /* Return the opcode for a ptr_mode load of the form:
145
146 l[wd] DEST, OFFSET(BASE). */
147 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE) \
148 (((ptr_mode == DImode ? 0x37 : 0x23) << 26) \
149 | ((BASE) << 21) \
150 | ((DEST) << 16) \
151 | (OFFSET))
152
153 /* Return the opcode to move register SRC into register DEST. */
154 #define MIPS_MOVE(DEST, SRC) \
155 ((TARGET_64BIT ? 0x2d : 0x21) \
156 | ((DEST) << 11) \
157 | ((SRC) << 21))
158
159 /* Return the opcode for:
160
161 lui DEST, VALUE. */
162 #define MIPS_LUI(DEST, VALUE) \
163 ((0xf << 26) | ((DEST) << 16) | (VALUE))
164
165 /* Return the opcode to jump to register DEST. */
166 #define MIPS_JR(DEST) \
167 (((DEST) << 21) | 0x8)
168
169 /* Return the opcode for:
170
171 bal . + (1 + OFFSET) * 4. */
172 #define MIPS_BAL(OFFSET) \
173 ((0x1 << 26) | (0x11 << 16) | (OFFSET))
174
175 /* Return the usual opcode for a nop. */
176 #define MIPS_NOP 0
177
178 /* Classifies an address.
179
180 ADDRESS_REG
181 A natural register + offset address. The register satisfies
182 mips_valid_base_register_p and the offset is a const_arith_operand.
183
184 ADDRESS_LO_SUM
185 A LO_SUM rtx. The first operand is a valid base register and
186 the second operand is a symbolic address.
187
188 ADDRESS_CONST_INT
189 A signed 16-bit constant address.
190
191 ADDRESS_SYMBOLIC:
192 A constant symbolic address. */
193 enum mips_address_type {
194 ADDRESS_REG,
195 ADDRESS_LO_SUM,
196 ADDRESS_CONST_INT,
197 ADDRESS_SYMBOLIC
198 };
199
200 /* Macros to create an enumeration identifier for a function prototype. */
201 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
202 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
203 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
204 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
205
206 /* Classifies the prototype of a built-in function. */
207 enum mips_function_type {
208 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
209 #include "config/mips/mips-ftypes.def"
210 #undef DEF_MIPS_FTYPE
211 MIPS_MAX_FTYPE_MAX
212 };
213
214 /* Specifies how a built-in function should be converted into rtl. */
215 enum mips_builtin_type {
216 /* The function corresponds directly to an .md pattern. The return
217 value is mapped to operand 0 and the arguments are mapped to
218 operands 1 and above. */
219 MIPS_BUILTIN_DIRECT,
220
221 /* The function corresponds directly to an .md pattern. There is no return
222 value and the arguments are mapped to operands 0 and above. */
223 MIPS_BUILTIN_DIRECT_NO_TARGET,
224
225 /* The function corresponds to a comparison instruction followed by
226 a mips_cond_move_tf_ps pattern. The first two arguments are the
227 values to compare and the second two arguments are the vector
228 operands for the movt.ps or movf.ps instruction (in assembly order). */
229 MIPS_BUILTIN_MOVF,
230 MIPS_BUILTIN_MOVT,
231
232 /* The function corresponds to a V2SF comparison instruction. Operand 0
233 of this instruction is the result of the comparison, which has mode
234 CCV2 or CCV4. The function arguments are mapped to operands 1 and
235 above. The function's return value is an SImode boolean that is
236 true under the following conditions:
237
238 MIPS_BUILTIN_CMP_ANY: one of the registers is true
239 MIPS_BUILTIN_CMP_ALL: all of the registers are true
240 MIPS_BUILTIN_CMP_LOWER: the first register is true
241 MIPS_BUILTIN_CMP_UPPER: the second register is true. */
242 MIPS_BUILTIN_CMP_ANY,
243 MIPS_BUILTIN_CMP_ALL,
244 MIPS_BUILTIN_CMP_UPPER,
245 MIPS_BUILTIN_CMP_LOWER,
246
247 /* As above, but the instruction only sets a single $fcc register. */
248 MIPS_BUILTIN_CMP_SINGLE,
249
250 /* For generating bposge32 branch instructions in MIPS32 DSP ASE. */
251 MIPS_BUILTIN_BPOSGE32
252 };
253
254 /* Invoke MACRO (COND) for each C.cond.fmt condition. */
255 #define MIPS_FP_CONDITIONS(MACRO) \
256 MACRO (f), \
257 MACRO (un), \
258 MACRO (eq), \
259 MACRO (ueq), \
260 MACRO (olt), \
261 MACRO (ult), \
262 MACRO (ole), \
263 MACRO (ule), \
264 MACRO (sf), \
265 MACRO (ngle), \
266 MACRO (seq), \
267 MACRO (ngl), \
268 MACRO (lt), \
269 MACRO (nge), \
270 MACRO (le), \
271 MACRO (ngt)
272
273 /* Enumerates the codes above as MIPS_FP_COND_<X>. */
274 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
275 enum mips_fp_condition {
276 MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
277 };
278 #undef DECLARE_MIPS_COND
279
280 /* Index X provides the string representation of MIPS_FP_COND_<X>. */
281 #define STRINGIFY(X) #X
282 static const char *const mips_fp_conditions[] = {
283 MIPS_FP_CONDITIONS (STRINGIFY)
284 };
285 #undef STRINGIFY
286
287 /* A class used to control a comdat-style stub that we output in each
288 translation unit that needs it. */
289 class mips_one_only_stub {
290 public:
291 virtual ~mips_one_only_stub () {}
292
293 /* Return the name of the stub. */
294 virtual const char *get_name () = 0;
295
296 /* Output the body of the function to asm_out_file. */
297 virtual void output_body () = 0;
298 };
299
300 /* Tuning information that is automatically derived from other sources
301 (such as the scheduler). */
302 static struct {
303 /* The architecture and tuning settings that this structure describes. */
304 enum processor arch;
305 enum processor tune;
306
307 /* True if this structure describes MIPS16 settings. */
308 bool mips16_p;
309
310 /* True if the structure has been initialized. */
311 bool initialized_p;
312
313 /* True if "MULT $0, $0" is preferable to "MTLO $0; MTHI $0"
314 when optimizing for speed. */
315 bool fast_mult_zero_zero_p;
316 } mips_tuning_info;
317
318 /* Information about a function's frame layout. */
319 struct GTY(()) mips_frame_info {
320 /* The size of the frame in bytes. */
321 HOST_WIDE_INT total_size;
322
323 /* The number of bytes allocated to variables. */
324 HOST_WIDE_INT var_size;
325
326 /* The number of bytes allocated to outgoing function arguments. */
327 HOST_WIDE_INT args_size;
328
329 /* The number of bytes allocated to the .cprestore slot, or 0 if there
330 is no such slot. */
331 HOST_WIDE_INT cprestore_size;
332
333 /* Bit X is set if the function saves or restores GPR X. */
334 unsigned int mask;
335
336 /* Likewise FPR X. */
337 unsigned int fmask;
338
339 /* Likewise doubleword accumulator X ($acX). */
340 unsigned int acc_mask;
341
342 /* The number of GPRs, FPRs, doubleword accumulators and COP0
343 registers saved. */
344 unsigned int num_gp;
345 unsigned int num_fp;
346 unsigned int num_acc;
347 unsigned int num_cop0_regs;
348
349 /* The offset of the topmost GPR, FPR, accumulator and COP0-register
350 save slots from the top of the frame, or zero if no such slots are
351 needed. */
352 HOST_WIDE_INT gp_save_offset;
353 HOST_WIDE_INT fp_save_offset;
354 HOST_WIDE_INT acc_save_offset;
355 HOST_WIDE_INT cop0_save_offset;
356
357 /* Likewise, but giving offsets from the bottom of the frame. */
358 HOST_WIDE_INT gp_sp_offset;
359 HOST_WIDE_INT fp_sp_offset;
360 HOST_WIDE_INT acc_sp_offset;
361 HOST_WIDE_INT cop0_sp_offset;
362
363 /* Similar, but the value passed to _mcount. */
364 HOST_WIDE_INT ra_fp_offset;
365
366 /* The offset of arg_pointer_rtx from the bottom of the frame. */
367 HOST_WIDE_INT arg_pointer_offset;
368
369 /* The offset of hard_frame_pointer_rtx from the bottom of the frame. */
370 HOST_WIDE_INT hard_frame_pointer_offset;
371 };
372
373 struct GTY(()) machine_function {
374 /* The next floating-point condition-code register to allocate
375 for ISA_HAS_8CC targets, relative to ST_REG_FIRST. */
376 unsigned int next_fcc;
377
378 /* The register returned by mips16_gp_pseudo_reg; see there for details. */
379 rtx mips16_gp_pseudo_rtx;
380
381 /* The number of extra stack bytes taken up by register varargs.
382 This area is allocated by the callee at the very top of the frame. */
383 int varargs_size;
384
385 /* The current frame information, calculated by mips_compute_frame_info. */
386 struct mips_frame_info frame;
387
388 /* The register to use as the function's global pointer, or INVALID_REGNUM
389 if the function doesn't need one. */
390 unsigned int global_pointer;
391
392 /* How many instructions it takes to load a label into $AT, or 0 if
393 this property hasn't yet been calculated. */
394 unsigned int load_label_num_insns;
395
396 /* True if mips_adjust_insn_length should ignore an instruction's
397 hazard attribute. */
398 bool ignore_hazard_length_p;
399
400 /* True if the whole function is suitable for .set noreorder and
401 .set nomacro. */
402 bool all_noreorder_p;
403
404 /* True if the function has "inflexible" and "flexible" references
405 to the global pointer. See mips_cfun_has_inflexible_gp_ref_p
406 and mips_cfun_has_flexible_gp_ref_p for details. */
407 bool has_inflexible_gp_insn_p;
408 bool has_flexible_gp_insn_p;
409
410 /* True if the function's prologue must load the global pointer
411 value into pic_offset_table_rtx and store the same value in
412 the function's cprestore slot (if any). Even if this value
413 is currently false, we may decide to set it to true later;
414 see mips_must_initialize_gp_p () for details. */
415 bool must_initialize_gp_p;
416
417 /* True if the current function must restore $gp after any potential
418 clobber. This value is only meaningful during the first post-epilogue
419 split_insns pass; see mips_must_initialize_gp_p () for details. */
420 bool must_restore_gp_when_clobbered_p;
421
422 /* True if this is an interrupt handler. */
423 bool interrupt_handler_p;
424
425 /* True if this is an interrupt handler that uses shadow registers. */
426 bool use_shadow_register_set_p;
427
428 /* True if this is an interrupt handler that should keep interrupts
429 masked. */
430 bool keep_interrupts_masked_p;
431
432 /* True if this is an interrupt handler that should use DERET
433 instead of ERET. */
434 bool use_debug_exception_return_p;
435 };
436
437 /* Information about a single argument. */
438 struct mips_arg_info {
439 /* True if the argument is passed in a floating-point register, or
440 would have been if we hadn't run out of registers. */
441 bool fpr_p;
442
443 /* The number of words passed in registers, rounded up. */
444 unsigned int reg_words;
445
446 /* For EABI, the offset of the first register from GP_ARG_FIRST or
447 FP_ARG_FIRST. For other ABIs, the offset of the first register from
448 the start of the ABI's argument structure (see the CUMULATIVE_ARGS
449 comment for details).
450
451 The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
452 on the stack. */
453 unsigned int reg_offset;
454
455 /* The number of words that must be passed on the stack, rounded up. */
456 unsigned int stack_words;
457
458 /* The offset from the start of the stack overflow area of the argument's
459 first stack word. Only meaningful when STACK_WORDS is nonzero. */
460 unsigned int stack_offset;
461 };
462
463 /* Information about an address described by mips_address_type.
464
465 ADDRESS_CONST_INT
466 No fields are used.
467
468 ADDRESS_REG
469 REG is the base register and OFFSET is the constant offset.
470
471 ADDRESS_LO_SUM
472 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
473 is the type of symbol it references.
474
475 ADDRESS_SYMBOLIC
476 SYMBOL_TYPE is the type of symbol that the address references. */
477 struct mips_address_info {
478 enum mips_address_type type;
479 rtx reg;
480 rtx offset;
481 enum mips_symbol_type symbol_type;
482 };
483
484 /* One stage in a constant building sequence. These sequences have
485 the form:
486
487 A = VALUE[0]
488 A = A CODE[1] VALUE[1]
489 A = A CODE[2] VALUE[2]
490 ...
491
492 where A is an accumulator, each CODE[i] is a binary rtl operation
493 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
494 struct mips_integer_op {
495 enum rtx_code code;
496 unsigned HOST_WIDE_INT value;
497 };
498
499 /* The largest number of operations needed to load an integer constant.
500 The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
501 When the lowest bit is clear, we can try, but reject a sequence with
502 an extra SLL at the end. */
503 #define MIPS_MAX_INTEGER_OPS 7
504
505 /* Information about a MIPS16e SAVE or RESTORE instruction. */
506 struct mips16e_save_restore_info {
507 /* The number of argument registers saved by a SAVE instruction.
508 0 for RESTORE instructions. */
509 unsigned int nargs;
510
511 /* Bit X is set if the instruction saves or restores GPR X. */
512 unsigned int mask;
513
514 /* The total number of bytes to allocate. */
515 HOST_WIDE_INT size;
516 };
517
518 /* Costs of various operations on the different architectures. */
519
520 struct mips_rtx_cost_data
521 {
522 unsigned short fp_add;
523 unsigned short fp_mult_sf;
524 unsigned short fp_mult_df;
525 unsigned short fp_div_sf;
526 unsigned short fp_div_df;
527 unsigned short int_mult_si;
528 unsigned short int_mult_di;
529 unsigned short int_div_si;
530 unsigned short int_div_di;
531 unsigned short branch_cost;
532 unsigned short memory_latency;
533 };
534
535 /* Global variables for machine-dependent things. */
536
537 /* The -G setting, or the configuration's default small-data limit if
538 no -G option is given. */
539 static unsigned int mips_small_data_threshold;
540
541 /* The number of file directives written by mips_output_filename. */
542 int num_source_filenames;
543
544 /* The name that appeared in the last .file directive written by
545 mips_output_filename, or "" if mips_output_filename hasn't
546 written anything yet. */
547 const char *current_function_file = "";
548
549 /* Arrays that map GCC register numbers to debugger register numbers. */
550 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
551 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
552
553 /* Information about the current function's epilogue, used only while
554 expanding it. */
555 static struct {
556 /* A list of queued REG_CFA_RESTORE notes. */
557 rtx cfa_restores;
558
559 /* The CFA is currently defined as CFA_REG + CFA_OFFSET. */
560 rtx cfa_reg;
561 HOST_WIDE_INT cfa_offset;
562
563 /* The offset of the CFA from the stack pointer while restoring
564 registers. */
565 HOST_WIDE_INT cfa_restore_sp_offset;
566 } mips_epilogue;
567
568 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs. */
569 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
570 struct mips_asm_switch mips_nomacro = { "macro", 0 };
571 struct mips_asm_switch mips_noat = { "at", 0 };
572
573 /* True if we're writing out a branch-likely instruction rather than a
574 normal branch. */
575 static bool mips_branch_likely;
576
577 /* The current instruction-set architecture. */
578 enum processor mips_arch;
579 const struct mips_cpu_info *mips_arch_info;
580
581 /* The processor that we should tune the code for. */
582 enum processor mips_tune;
583 const struct mips_cpu_info *mips_tune_info;
584
585 /* The ISA level associated with mips_arch. */
586 int mips_isa;
587
588 /* The ISA revision level. This is 0 for MIPS I to V and N for
589 MIPS{32,64}rN. */
590 int mips_isa_rev;
591
592 /* The architecture selected by -mipsN, or null if -mipsN wasn't used. */
593 static const struct mips_cpu_info *mips_isa_option_info;
594
595 /* Which cost information to use. */
596 static const struct mips_rtx_cost_data *mips_cost;
597
598 /* The ambient target flags, excluding MASK_MIPS16. */
599 static int mips_base_target_flags;
600
601 /* The default compression mode. */
602 unsigned int mips_base_compression_flags;
603
604 /* The ambient values of other global variables. */
605 static int mips_base_schedule_insns; /* flag_schedule_insns */
606 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
607 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
608 static int mips_base_align_loops; /* align_loops */
609 static int mips_base_align_jumps; /* align_jumps */
610 static int mips_base_align_functions; /* align_functions */
611
612 /* Index [M][R] is true if register R is allowed to hold a value of mode M. */
613 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
614
615 /* Index C is true if character C is a valid PRINT_OPERAND punctation
616 character. */
617 static bool mips_print_operand_punct[256];
618
619 static GTY (()) int mips_output_filename_first_time = 1;
620
621 /* mips_split_p[X] is true if symbols of type X can be split by
622 mips_split_symbol. */
623 bool mips_split_p[NUM_SYMBOL_TYPES];
624
625 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
626 can be split by mips_split_symbol. */
627 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
628
629 /* mips_use_pcrel_pool_p[X] is true if symbols of type X should be
630 forced into a PC-relative constant pool. */
631 bool mips_use_pcrel_pool_p[NUM_SYMBOL_TYPES];
632
633 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
634 appears in a LO_SUM. It can be null if such LO_SUMs aren't valid or
635 if they are matched by a special .md file pattern. */
636 const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
637
638 /* Likewise for HIGHs. */
639 const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
640
641 /* Target state for MIPS16. */
642 struct target_globals *mips16_globals;
643
644 /* Cached value of can_issue_more. This is cached in mips_variable_issue hook
645 and returned from mips_sched_reorder2. */
646 static int cached_can_issue_more;
647
648 /* The stubs for various MIPS16 support functions, if used. */
649 static mips_one_only_stub *mips16_rdhwr_stub;
650 static mips_one_only_stub *mips16_get_fcsr_stub;
651 static mips_one_only_stub *mips16_set_fcsr_stub;
652
653 /* Index R is the smallest register class that contains register R. */
654 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
655 LEA_REGS, LEA_REGS, M16_STORE_REGS, V1_REG,
656 M16_STORE_REGS, M16_STORE_REGS, M16_STORE_REGS, M16_STORE_REGS,
657 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
658 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
659 M16_REGS, M16_STORE_REGS, LEA_REGS, LEA_REGS,
660 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
661 T_REG, PIC_FN_ADDR_REG, LEA_REGS, LEA_REGS,
662 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
663
664 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
665 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
666 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
667 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
668 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
669 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
670 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
671 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
672 MD0_REG, MD1_REG, NO_REGS, ST_REGS,
673 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
674 ST_REGS, ST_REGS, ST_REGS, NO_REGS,
675 NO_REGS, FRAME_REGS, FRAME_REGS, NO_REGS,
676 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
677 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
678 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
679 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
680 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
681 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
682 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
683 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
684 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
685 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
686 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
687 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
688 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
689 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
690 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
691 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
692 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
693 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
694 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
695 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
696 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
697 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
698 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
699 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
700 DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS,
701 DSP_ACC_REGS, DSP_ACC_REGS, ALL_REGS, ALL_REGS,
702 ALL_REGS, ALL_REGS, ALL_REGS, ALL_REGS
703 };
704
705 /* The value of TARGET_ATTRIBUTE_TABLE. */
706 static const struct attribute_spec mips_attribute_table[] = {
707 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
708 om_diagnostic } */
709 { "long_call", 0, 0, false, true, true, NULL, false },
710 { "far", 0, 0, false, true, true, NULL, false },
711 { "near", 0, 0, false, true, true, NULL, false },
712 /* We would really like to treat "mips16" and "nomips16" as type
713 attributes, but GCC doesn't provide the hooks we need to support
714 the right conversion rules. As declaration attributes, they affect
715 code generation but don't carry other semantics. */
716 { "mips16", 0, 0, true, false, false, NULL, false },
717 { "nomips16", 0, 0, true, false, false, NULL, false },
718 { "micromips", 0, 0, true, false, false, NULL, false },
719 { "nomicromips", 0, 0, true, false, false, NULL, false },
720 { "nocompression", 0, 0, true, false, false, NULL, false },
721 /* Allow functions to be specified as interrupt handlers */
722 { "interrupt", 0, 0, false, true, true, NULL, false },
723 { "use_shadow_register_set", 0, 0, false, true, true, NULL, false },
724 { "keep_interrupts_masked", 0, 0, false, true, true, NULL, false },
725 { "use_debug_exception_return", 0, 0, false, true, true, NULL, false },
726 { NULL, 0, 0, false, false, false, NULL, false }
727 };
728 \f
729 /* A table describing all the processors GCC knows about; see
730 mips-cpus.def for details. */
731 static const struct mips_cpu_info mips_cpu_info_table[] = {
732 #define MIPS_CPU(NAME, CPU, ISA, FLAGS) \
733 { NAME, CPU, ISA, FLAGS },
734 #include "mips-cpus.def"
735 #undef MIPS_CPU
736 };
737
738 /* Default costs. If these are used for a processor we should look
739 up the actual costs. */
740 #define DEFAULT_COSTS COSTS_N_INSNS (6), /* fp_add */ \
741 COSTS_N_INSNS (7), /* fp_mult_sf */ \
742 COSTS_N_INSNS (8), /* fp_mult_df */ \
743 COSTS_N_INSNS (23), /* fp_div_sf */ \
744 COSTS_N_INSNS (36), /* fp_div_df */ \
745 COSTS_N_INSNS (10), /* int_mult_si */ \
746 COSTS_N_INSNS (10), /* int_mult_di */ \
747 COSTS_N_INSNS (69), /* int_div_si */ \
748 COSTS_N_INSNS (69), /* int_div_di */ \
749 2, /* branch_cost */ \
750 4 /* memory_latency */
751
752 /* Floating-point costs for processors without an FPU. Just assume that
753 all floating-point libcalls are very expensive. */
754 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */ \
755 COSTS_N_INSNS (256), /* fp_mult_sf */ \
756 COSTS_N_INSNS (256), /* fp_mult_df */ \
757 COSTS_N_INSNS (256), /* fp_div_sf */ \
758 COSTS_N_INSNS (256) /* fp_div_df */
759
760 /* Costs to use when optimizing for size. */
761 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
762 COSTS_N_INSNS (1), /* fp_add */
763 COSTS_N_INSNS (1), /* fp_mult_sf */
764 COSTS_N_INSNS (1), /* fp_mult_df */
765 COSTS_N_INSNS (1), /* fp_div_sf */
766 COSTS_N_INSNS (1), /* fp_div_df */
767 COSTS_N_INSNS (1), /* int_mult_si */
768 COSTS_N_INSNS (1), /* int_mult_di */
769 COSTS_N_INSNS (1), /* int_div_si */
770 COSTS_N_INSNS (1), /* int_div_di */
771 2, /* branch_cost */
772 4 /* memory_latency */
773 };
774
775 /* Costs to use when optimizing for speed, indexed by processor. */
776 static const struct mips_rtx_cost_data
777 mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
778 { /* R3000 */
779 COSTS_N_INSNS (2), /* fp_add */
780 COSTS_N_INSNS (4), /* fp_mult_sf */
781 COSTS_N_INSNS (5), /* fp_mult_df */
782 COSTS_N_INSNS (12), /* fp_div_sf */
783 COSTS_N_INSNS (19), /* fp_div_df */
784 COSTS_N_INSNS (12), /* int_mult_si */
785 COSTS_N_INSNS (12), /* int_mult_di */
786 COSTS_N_INSNS (35), /* int_div_si */
787 COSTS_N_INSNS (35), /* int_div_di */
788 1, /* branch_cost */
789 4 /* memory_latency */
790 },
791 { /* 4KC */
792 SOFT_FP_COSTS,
793 COSTS_N_INSNS (6), /* int_mult_si */
794 COSTS_N_INSNS (6), /* int_mult_di */
795 COSTS_N_INSNS (36), /* int_div_si */
796 COSTS_N_INSNS (36), /* int_div_di */
797 1, /* branch_cost */
798 4 /* memory_latency */
799 },
800 { /* 4KP */
801 SOFT_FP_COSTS,
802 COSTS_N_INSNS (36), /* int_mult_si */
803 COSTS_N_INSNS (36), /* int_mult_di */
804 COSTS_N_INSNS (37), /* int_div_si */
805 COSTS_N_INSNS (37), /* int_div_di */
806 1, /* branch_cost */
807 4 /* memory_latency */
808 },
809 { /* 5KC */
810 SOFT_FP_COSTS,
811 COSTS_N_INSNS (4), /* int_mult_si */
812 COSTS_N_INSNS (11), /* int_mult_di */
813 COSTS_N_INSNS (36), /* int_div_si */
814 COSTS_N_INSNS (68), /* int_div_di */
815 1, /* branch_cost */
816 4 /* memory_latency */
817 },
818 { /* 5KF */
819 COSTS_N_INSNS (4), /* fp_add */
820 COSTS_N_INSNS (4), /* fp_mult_sf */
821 COSTS_N_INSNS (5), /* fp_mult_df */
822 COSTS_N_INSNS (17), /* fp_div_sf */
823 COSTS_N_INSNS (32), /* fp_div_df */
824 COSTS_N_INSNS (4), /* int_mult_si */
825 COSTS_N_INSNS (11), /* int_mult_di */
826 COSTS_N_INSNS (36), /* int_div_si */
827 COSTS_N_INSNS (68), /* int_div_di */
828 1, /* branch_cost */
829 4 /* memory_latency */
830 },
831 { /* 20KC */
832 COSTS_N_INSNS (4), /* fp_add */
833 COSTS_N_INSNS (4), /* fp_mult_sf */
834 COSTS_N_INSNS (5), /* fp_mult_df */
835 COSTS_N_INSNS (17), /* fp_div_sf */
836 COSTS_N_INSNS (32), /* fp_div_df */
837 COSTS_N_INSNS (4), /* int_mult_si */
838 COSTS_N_INSNS (7), /* int_mult_di */
839 COSTS_N_INSNS (42), /* int_div_si */
840 COSTS_N_INSNS (72), /* int_div_di */
841 1, /* branch_cost */
842 4 /* memory_latency */
843 },
844 { /* 24KC */
845 SOFT_FP_COSTS,
846 COSTS_N_INSNS (5), /* int_mult_si */
847 COSTS_N_INSNS (5), /* int_mult_di */
848 COSTS_N_INSNS (41), /* int_div_si */
849 COSTS_N_INSNS (41), /* int_div_di */
850 1, /* branch_cost */
851 4 /* memory_latency */
852 },
853 { /* 24KF2_1 */
854 COSTS_N_INSNS (8), /* fp_add */
855 COSTS_N_INSNS (8), /* fp_mult_sf */
856 COSTS_N_INSNS (10), /* fp_mult_df */
857 COSTS_N_INSNS (34), /* fp_div_sf */
858 COSTS_N_INSNS (64), /* fp_div_df */
859 COSTS_N_INSNS (5), /* int_mult_si */
860 COSTS_N_INSNS (5), /* int_mult_di */
861 COSTS_N_INSNS (41), /* int_div_si */
862 COSTS_N_INSNS (41), /* int_div_di */
863 1, /* branch_cost */
864 4 /* memory_latency */
865 },
866 { /* 24KF1_1 */
867 COSTS_N_INSNS (4), /* fp_add */
868 COSTS_N_INSNS (4), /* fp_mult_sf */
869 COSTS_N_INSNS (5), /* fp_mult_df */
870 COSTS_N_INSNS (17), /* fp_div_sf */
871 COSTS_N_INSNS (32), /* fp_div_df */
872 COSTS_N_INSNS (5), /* int_mult_si */
873 COSTS_N_INSNS (5), /* int_mult_di */
874 COSTS_N_INSNS (41), /* int_div_si */
875 COSTS_N_INSNS (41), /* int_div_di */
876 1, /* branch_cost */
877 4 /* memory_latency */
878 },
879 { /* 74KC */
880 SOFT_FP_COSTS,
881 COSTS_N_INSNS (5), /* int_mult_si */
882 COSTS_N_INSNS (5), /* int_mult_di */
883 COSTS_N_INSNS (41), /* int_div_si */
884 COSTS_N_INSNS (41), /* int_div_di */
885 1, /* branch_cost */
886 4 /* memory_latency */
887 },
888 { /* 74KF2_1 */
889 COSTS_N_INSNS (8), /* fp_add */
890 COSTS_N_INSNS (8), /* fp_mult_sf */
891 COSTS_N_INSNS (10), /* fp_mult_df */
892 COSTS_N_INSNS (34), /* fp_div_sf */
893 COSTS_N_INSNS (64), /* fp_div_df */
894 COSTS_N_INSNS (5), /* int_mult_si */
895 COSTS_N_INSNS (5), /* int_mult_di */
896 COSTS_N_INSNS (41), /* int_div_si */
897 COSTS_N_INSNS (41), /* int_div_di */
898 1, /* branch_cost */
899 4 /* memory_latency */
900 },
901 { /* 74KF1_1 */
902 COSTS_N_INSNS (4), /* fp_add */
903 COSTS_N_INSNS (4), /* fp_mult_sf */
904 COSTS_N_INSNS (5), /* fp_mult_df */
905 COSTS_N_INSNS (17), /* fp_div_sf */
906 COSTS_N_INSNS (32), /* fp_div_df */
907 COSTS_N_INSNS (5), /* int_mult_si */
908 COSTS_N_INSNS (5), /* int_mult_di */
909 COSTS_N_INSNS (41), /* int_div_si */
910 COSTS_N_INSNS (41), /* int_div_di */
911 1, /* branch_cost */
912 4 /* memory_latency */
913 },
914 { /* 74KF3_2 */
915 COSTS_N_INSNS (6), /* fp_add */
916 COSTS_N_INSNS (6), /* fp_mult_sf */
917 COSTS_N_INSNS (7), /* fp_mult_df */
918 COSTS_N_INSNS (25), /* fp_div_sf */
919 COSTS_N_INSNS (48), /* fp_div_df */
920 COSTS_N_INSNS (5), /* int_mult_si */
921 COSTS_N_INSNS (5), /* int_mult_di */
922 COSTS_N_INSNS (41), /* int_div_si */
923 COSTS_N_INSNS (41), /* int_div_di */
924 1, /* branch_cost */
925 4 /* memory_latency */
926 },
927 { /* Loongson-2E */
928 DEFAULT_COSTS
929 },
930 { /* Loongson-2F */
931 DEFAULT_COSTS
932 },
933 { /* Loongson-3A */
934 DEFAULT_COSTS
935 },
936 { /* M4k */
937 DEFAULT_COSTS
938 },
939 /* Octeon */
940 {
941 SOFT_FP_COSTS,
942 COSTS_N_INSNS (5), /* int_mult_si */
943 COSTS_N_INSNS (5), /* int_mult_di */
944 COSTS_N_INSNS (72), /* int_div_si */
945 COSTS_N_INSNS (72), /* int_div_di */
946 1, /* branch_cost */
947 4 /* memory_latency */
948 },
949 /* Octeon II */
950 {
951 SOFT_FP_COSTS,
952 COSTS_N_INSNS (6), /* int_mult_si */
953 COSTS_N_INSNS (6), /* int_mult_di */
954 COSTS_N_INSNS (18), /* int_div_si */
955 COSTS_N_INSNS (35), /* int_div_di */
956 4, /* branch_cost */
957 4 /* memory_latency */
958 },
959 { /* R3900 */
960 COSTS_N_INSNS (2), /* fp_add */
961 COSTS_N_INSNS (4), /* fp_mult_sf */
962 COSTS_N_INSNS (5), /* fp_mult_df */
963 COSTS_N_INSNS (12), /* fp_div_sf */
964 COSTS_N_INSNS (19), /* fp_div_df */
965 COSTS_N_INSNS (2), /* int_mult_si */
966 COSTS_N_INSNS (2), /* int_mult_di */
967 COSTS_N_INSNS (35), /* int_div_si */
968 COSTS_N_INSNS (35), /* int_div_di */
969 1, /* branch_cost */
970 4 /* memory_latency */
971 },
972 { /* R6000 */
973 COSTS_N_INSNS (3), /* fp_add */
974 COSTS_N_INSNS (5), /* fp_mult_sf */
975 COSTS_N_INSNS (6), /* fp_mult_df */
976 COSTS_N_INSNS (15), /* fp_div_sf */
977 COSTS_N_INSNS (16), /* fp_div_df */
978 COSTS_N_INSNS (17), /* int_mult_si */
979 COSTS_N_INSNS (17), /* int_mult_di */
980 COSTS_N_INSNS (38), /* int_div_si */
981 COSTS_N_INSNS (38), /* int_div_di */
982 2, /* branch_cost */
983 6 /* memory_latency */
984 },
985 { /* R4000 */
986 COSTS_N_INSNS (6), /* fp_add */
987 COSTS_N_INSNS (7), /* fp_mult_sf */
988 COSTS_N_INSNS (8), /* fp_mult_df */
989 COSTS_N_INSNS (23), /* fp_div_sf */
990 COSTS_N_INSNS (36), /* fp_div_df */
991 COSTS_N_INSNS (10), /* int_mult_si */
992 COSTS_N_INSNS (10), /* int_mult_di */
993 COSTS_N_INSNS (69), /* int_div_si */
994 COSTS_N_INSNS (69), /* int_div_di */
995 2, /* branch_cost */
996 6 /* memory_latency */
997 },
998 { /* R4100 */
999 DEFAULT_COSTS
1000 },
1001 { /* R4111 */
1002 DEFAULT_COSTS
1003 },
1004 { /* R4120 */
1005 DEFAULT_COSTS
1006 },
1007 { /* R4130 */
1008 /* The only costs that appear to be updated here are
1009 integer multiplication. */
1010 SOFT_FP_COSTS,
1011 COSTS_N_INSNS (4), /* int_mult_si */
1012 COSTS_N_INSNS (6), /* int_mult_di */
1013 COSTS_N_INSNS (69), /* int_div_si */
1014 COSTS_N_INSNS (69), /* int_div_di */
1015 1, /* branch_cost */
1016 4 /* memory_latency */
1017 },
1018 { /* R4300 */
1019 DEFAULT_COSTS
1020 },
1021 { /* R4600 */
1022 DEFAULT_COSTS
1023 },
1024 { /* R4650 */
1025 DEFAULT_COSTS
1026 },
1027 { /* R4700 */
1028 DEFAULT_COSTS
1029 },
1030 { /* R5000 */
1031 COSTS_N_INSNS (6), /* fp_add */
1032 COSTS_N_INSNS (4), /* fp_mult_sf */
1033 COSTS_N_INSNS (5), /* fp_mult_df */
1034 COSTS_N_INSNS (23), /* fp_div_sf */
1035 COSTS_N_INSNS (36), /* fp_div_df */
1036 COSTS_N_INSNS (5), /* int_mult_si */
1037 COSTS_N_INSNS (5), /* int_mult_di */
1038 COSTS_N_INSNS (36), /* int_div_si */
1039 COSTS_N_INSNS (36), /* int_div_di */
1040 1, /* branch_cost */
1041 4 /* memory_latency */
1042 },
1043 { /* R5400 */
1044 COSTS_N_INSNS (6), /* fp_add */
1045 COSTS_N_INSNS (5), /* fp_mult_sf */
1046 COSTS_N_INSNS (6), /* fp_mult_df */
1047 COSTS_N_INSNS (30), /* fp_div_sf */
1048 COSTS_N_INSNS (59), /* fp_div_df */
1049 COSTS_N_INSNS (3), /* int_mult_si */
1050 COSTS_N_INSNS (4), /* int_mult_di */
1051 COSTS_N_INSNS (42), /* int_div_si */
1052 COSTS_N_INSNS (74), /* int_div_di */
1053 1, /* branch_cost */
1054 4 /* memory_latency */
1055 },
1056 { /* R5500 */
1057 COSTS_N_INSNS (6), /* fp_add */
1058 COSTS_N_INSNS (5), /* fp_mult_sf */
1059 COSTS_N_INSNS (6), /* fp_mult_df */
1060 COSTS_N_INSNS (30), /* fp_div_sf */
1061 COSTS_N_INSNS (59), /* fp_div_df */
1062 COSTS_N_INSNS (5), /* int_mult_si */
1063 COSTS_N_INSNS (9), /* int_mult_di */
1064 COSTS_N_INSNS (42), /* int_div_si */
1065 COSTS_N_INSNS (74), /* int_div_di */
1066 1, /* branch_cost */
1067 4 /* memory_latency */
1068 },
1069 { /* R5900 */
1070 COSTS_N_INSNS (4), /* fp_add */
1071 COSTS_N_INSNS (4), /* fp_mult_sf */
1072 COSTS_N_INSNS (256), /* fp_mult_df */
1073 COSTS_N_INSNS (8), /* fp_div_sf */
1074 COSTS_N_INSNS (256), /* fp_div_df */
1075 COSTS_N_INSNS (4), /* int_mult_si */
1076 COSTS_N_INSNS (256), /* int_mult_di */
1077 COSTS_N_INSNS (37), /* int_div_si */
1078 COSTS_N_INSNS (256), /* int_div_di */
1079 1, /* branch_cost */
1080 4 /* memory_latency */
1081 },
1082 { /* R7000 */
1083 /* The only costs that are changed here are
1084 integer multiplication. */
1085 COSTS_N_INSNS (6), /* fp_add */
1086 COSTS_N_INSNS (7), /* fp_mult_sf */
1087 COSTS_N_INSNS (8), /* fp_mult_df */
1088 COSTS_N_INSNS (23), /* fp_div_sf */
1089 COSTS_N_INSNS (36), /* fp_div_df */
1090 COSTS_N_INSNS (5), /* int_mult_si */
1091 COSTS_N_INSNS (9), /* int_mult_di */
1092 COSTS_N_INSNS (69), /* int_div_si */
1093 COSTS_N_INSNS (69), /* int_div_di */
1094 1, /* branch_cost */
1095 4 /* memory_latency */
1096 },
1097 { /* R8000 */
1098 DEFAULT_COSTS
1099 },
1100 { /* R9000 */
1101 /* The only costs that are changed here are
1102 integer multiplication. */
1103 COSTS_N_INSNS (6), /* fp_add */
1104 COSTS_N_INSNS (7), /* fp_mult_sf */
1105 COSTS_N_INSNS (8), /* fp_mult_df */
1106 COSTS_N_INSNS (23), /* fp_div_sf */
1107 COSTS_N_INSNS (36), /* fp_div_df */
1108 COSTS_N_INSNS (3), /* int_mult_si */
1109 COSTS_N_INSNS (8), /* int_mult_di */
1110 COSTS_N_INSNS (69), /* int_div_si */
1111 COSTS_N_INSNS (69), /* int_div_di */
1112 1, /* branch_cost */
1113 4 /* memory_latency */
1114 },
1115 { /* R1x000 */
1116 COSTS_N_INSNS (2), /* fp_add */
1117 COSTS_N_INSNS (2), /* fp_mult_sf */
1118 COSTS_N_INSNS (2), /* fp_mult_df */
1119 COSTS_N_INSNS (12), /* fp_div_sf */
1120 COSTS_N_INSNS (19), /* fp_div_df */
1121 COSTS_N_INSNS (5), /* int_mult_si */
1122 COSTS_N_INSNS (9), /* int_mult_di */
1123 COSTS_N_INSNS (34), /* int_div_si */
1124 COSTS_N_INSNS (66), /* int_div_di */
1125 1, /* branch_cost */
1126 4 /* memory_latency */
1127 },
1128 { /* SB1 */
1129 /* These costs are the same as the SB-1A below. */
1130 COSTS_N_INSNS (4), /* fp_add */
1131 COSTS_N_INSNS (4), /* fp_mult_sf */
1132 COSTS_N_INSNS (4), /* fp_mult_df */
1133 COSTS_N_INSNS (24), /* fp_div_sf */
1134 COSTS_N_INSNS (32), /* fp_div_df */
1135 COSTS_N_INSNS (3), /* int_mult_si */
1136 COSTS_N_INSNS (4), /* int_mult_di */
1137 COSTS_N_INSNS (36), /* int_div_si */
1138 COSTS_N_INSNS (68), /* int_div_di */
1139 1, /* branch_cost */
1140 4 /* memory_latency */
1141 },
1142 { /* SB1-A */
1143 /* These costs are the same as the SB-1 above. */
1144 COSTS_N_INSNS (4), /* fp_add */
1145 COSTS_N_INSNS (4), /* fp_mult_sf */
1146 COSTS_N_INSNS (4), /* fp_mult_df */
1147 COSTS_N_INSNS (24), /* fp_div_sf */
1148 COSTS_N_INSNS (32), /* fp_div_df */
1149 COSTS_N_INSNS (3), /* int_mult_si */
1150 COSTS_N_INSNS (4), /* int_mult_di */
1151 COSTS_N_INSNS (36), /* int_div_si */
1152 COSTS_N_INSNS (68), /* int_div_di */
1153 1, /* branch_cost */
1154 4 /* memory_latency */
1155 },
1156 { /* SR71000 */
1157 DEFAULT_COSTS
1158 },
1159 { /* XLR */
1160 SOFT_FP_COSTS,
1161 COSTS_N_INSNS (8), /* int_mult_si */
1162 COSTS_N_INSNS (8), /* int_mult_di */
1163 COSTS_N_INSNS (72), /* int_div_si */
1164 COSTS_N_INSNS (72), /* int_div_di */
1165 1, /* branch_cost */
1166 4 /* memory_latency */
1167 },
1168 { /* XLP */
1169 /* These costs are the same as 5KF above. */
1170 COSTS_N_INSNS (4), /* fp_add */
1171 COSTS_N_INSNS (4), /* fp_mult_sf */
1172 COSTS_N_INSNS (5), /* fp_mult_df */
1173 COSTS_N_INSNS (17), /* fp_div_sf */
1174 COSTS_N_INSNS (32), /* fp_div_df */
1175 COSTS_N_INSNS (4), /* int_mult_si */
1176 COSTS_N_INSNS (11), /* int_mult_di */
1177 COSTS_N_INSNS (36), /* int_div_si */
1178 COSTS_N_INSNS (68), /* int_div_di */
1179 1, /* branch_cost */
1180 4 /* memory_latency */
1181 }
1182 };
1183 \f
1184 static rtx mips_find_pic_call_symbol (rtx, rtx, bool);
1185 static int mips_register_move_cost (enum machine_mode, reg_class_t,
1186 reg_class_t);
1187 static unsigned int mips_function_arg_boundary (enum machine_mode, const_tree);
1188 \f
1189 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1190 for -mflip_mips16. It maps decl names onto a boolean mode setting. */
1191 struct GTY (()) mflip_mips16_entry {
1192 const char *name;
1193 bool mips16_p;
1194 };
1195 static GTY ((param_is (struct mflip_mips16_entry))) htab_t mflip_mips16_htab;
1196
1197 /* Hash table callbacks for mflip_mips16_htab. */
1198
1199 static hashval_t
1200 mflip_mips16_htab_hash (const void *entry)
1201 {
1202 return htab_hash_string (((const struct mflip_mips16_entry *) entry)->name);
1203 }
1204
1205 static int
1206 mflip_mips16_htab_eq (const void *entry, const void *name)
1207 {
1208 return strcmp (((const struct mflip_mips16_entry *) entry)->name,
1209 (const char *) name) == 0;
1210 }
1211
1212 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1213 mode, false if it should next add an attribute for the opposite mode. */
1214 static GTY(()) bool mips16_flipper;
1215
1216 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1217 for -mflip-mips16. Return true if it should use "mips16" and false if
1218 it should use "nomips16". */
1219
1220 static bool
1221 mflip_mips16_use_mips16_p (tree decl)
1222 {
1223 struct mflip_mips16_entry *entry;
1224 const char *name;
1225 hashval_t hash;
1226 void **slot;
1227 bool base_is_mips16 = (mips_base_compression_flags & MASK_MIPS16) != 0;
1228
1229 /* Use the opposite of the command-line setting for anonymous decls. */
1230 if (!DECL_NAME (decl))
1231 return !base_is_mips16;
1232
1233 if (!mflip_mips16_htab)
1234 mflip_mips16_htab = htab_create_ggc (37, mflip_mips16_htab_hash,
1235 mflip_mips16_htab_eq, NULL);
1236
1237 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1238 hash = htab_hash_string (name);
1239 slot = htab_find_slot_with_hash (mflip_mips16_htab, name, hash, INSERT);
1240 entry = (struct mflip_mips16_entry *) *slot;
1241 if (!entry)
1242 {
1243 mips16_flipper = !mips16_flipper;
1244 entry = ggc_alloc_mflip_mips16_entry ();
1245 entry->name = name;
1246 entry->mips16_p = mips16_flipper ? !base_is_mips16 : base_is_mips16;
1247 *slot = entry;
1248 }
1249 return entry->mips16_p;
1250 }
1251 \f
1252 /* Predicates to test for presence of "near" and "far"/"long_call"
1253 attributes on the given TYPE. */
1254
1255 static bool
1256 mips_near_type_p (const_tree type)
1257 {
1258 return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1259 }
1260
1261 static bool
1262 mips_far_type_p (const_tree type)
1263 {
1264 return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1265 || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1266 }
1267
1268
1269 /* Check if the interrupt attribute is set for a function. */
1270
1271 static bool
1272 mips_interrupt_type_p (tree type)
1273 {
1274 return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1275 }
1276
1277 /* Check if the attribute to use shadow register set is set for a function. */
1278
1279 static bool
1280 mips_use_shadow_register_set_p (tree type)
1281 {
1282 return lookup_attribute ("use_shadow_register_set",
1283 TYPE_ATTRIBUTES (type)) != NULL;
1284 }
1285
1286 /* Check if the attribute to keep interrupts masked is set for a function. */
1287
1288 static bool
1289 mips_keep_interrupts_masked_p (tree type)
1290 {
1291 return lookup_attribute ("keep_interrupts_masked",
1292 TYPE_ATTRIBUTES (type)) != NULL;
1293 }
1294
1295 /* Check if the attribute to use debug exception return is set for
1296 a function. */
1297
1298 static bool
1299 mips_use_debug_exception_return_p (tree type)
1300 {
1301 return lookup_attribute ("use_debug_exception_return",
1302 TYPE_ATTRIBUTES (type)) != NULL;
1303 }
1304
1305 /* Return the set of compression modes that are explicitly required
1306 by the attributes in ATTRIBUTES. */
1307
1308 static unsigned int
1309 mips_get_compress_on_flags (tree attributes)
1310 {
1311 unsigned int flags = 0;
1312
1313 if (lookup_attribute ("mips16", attributes) != NULL)
1314 flags |= MASK_MIPS16;
1315
1316 if (lookup_attribute ("micromips", attributes) != NULL)
1317 flags |= MASK_MICROMIPS;
1318
1319 return flags;
1320 }
1321
1322 /* Return the set of compression modes that are explicitly forbidden
1323 by the attributes in ATTRIBUTES. */
1324
1325 static unsigned int
1326 mips_get_compress_off_flags (tree attributes)
1327 {
1328 unsigned int flags = 0;
1329
1330 if (lookup_attribute ("nocompression", attributes) != NULL)
1331 flags |= MASK_MIPS16 | MASK_MICROMIPS;
1332
1333 if (lookup_attribute ("nomips16", attributes) != NULL)
1334 flags |= MASK_MIPS16;
1335
1336 if (lookup_attribute ("nomicromips", attributes) != NULL)
1337 flags |= MASK_MICROMIPS;
1338
1339 return flags;
1340 }
1341
1342 /* Return the compression mode that should be used for function DECL.
1343 Return the ambient setting if DECL is null. */
1344
1345 static unsigned int
1346 mips_get_compress_mode (tree decl)
1347 {
1348 unsigned int flags, force_on;
1349
1350 flags = mips_base_compression_flags;
1351 if (decl)
1352 {
1353 /* Nested functions must use the same frame pointer as their
1354 parent and must therefore use the same ISA mode. */
1355 tree parent = decl_function_context (decl);
1356 if (parent)
1357 decl = parent;
1358 force_on = mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1359 if (force_on)
1360 return force_on;
1361 flags &= ~mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1362 }
1363 return flags;
1364 }
1365
1366 /* Return the attribute name associated with MASK_MIPS16 and MASK_MICROMIPS
1367 flags FLAGS. */
1368
1369 static const char *
1370 mips_get_compress_on_name (unsigned int flags)
1371 {
1372 if (flags == MASK_MIPS16)
1373 return "mips16";
1374 return "micromips";
1375 }
1376
1377 /* Return the attribute name that forbids MASK_MIPS16 and MASK_MICROMIPS
1378 flags FLAGS. */
1379
1380 static const char *
1381 mips_get_compress_off_name (unsigned int flags)
1382 {
1383 if (flags == MASK_MIPS16)
1384 return "nomips16";
1385 if (flags == MASK_MICROMIPS)
1386 return "nomicromips";
1387 return "nocompression";
1388 }
1389
1390 /* Implement TARGET_COMP_TYPE_ATTRIBUTES. */
1391
1392 static int
1393 mips_comp_type_attributes (const_tree type1, const_tree type2)
1394 {
1395 /* Disallow mixed near/far attributes. */
1396 if (mips_far_type_p (type1) && mips_near_type_p (type2))
1397 return 0;
1398 if (mips_near_type_p (type1) && mips_far_type_p (type2))
1399 return 0;
1400 return 1;
1401 }
1402
1403 /* Implement TARGET_INSERT_ATTRIBUTES. */
1404
1405 static void
1406 mips_insert_attributes (tree decl, tree *attributes)
1407 {
1408 const char *name;
1409 unsigned int compression_flags, nocompression_flags;
1410
1411 /* Check for "mips16" and "nomips16" attributes. */
1412 compression_flags = mips_get_compress_on_flags (*attributes);
1413 nocompression_flags = mips_get_compress_off_flags (*attributes);
1414
1415 if (TREE_CODE (decl) != FUNCTION_DECL)
1416 {
1417 if (nocompression_flags)
1418 error ("%qs attribute only applies to functions",
1419 mips_get_compress_off_name (nocompression_flags));
1420
1421 if (compression_flags)
1422 error ("%qs attribute only applies to functions",
1423 mips_get_compress_on_name (nocompression_flags));
1424 }
1425 else
1426 {
1427 compression_flags |= mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1428 nocompression_flags |=
1429 mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1430
1431 if (compression_flags && nocompression_flags)
1432 error ("%qE cannot have both %qs and %qs attributes",
1433 DECL_NAME (decl), mips_get_compress_on_name (compression_flags),
1434 mips_get_compress_off_name (nocompression_flags));
1435
1436 if (compression_flags & MASK_MIPS16
1437 && compression_flags & MASK_MICROMIPS)
1438 error ("%qE cannot have both %qs and %qs attributes",
1439 DECL_NAME (decl), "mips16", "micromips");
1440
1441 if (TARGET_FLIP_MIPS16
1442 && !DECL_ARTIFICIAL (decl)
1443 && compression_flags == 0
1444 && nocompression_flags == 0)
1445 {
1446 /* Implement -mflip-mips16. If DECL has neither a "nomips16" nor a
1447 "mips16" attribute, arbitrarily pick one. We must pick the same
1448 setting for duplicate declarations of a function. */
1449 name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1450 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1451 name = "nomicromips";
1452 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1453 }
1454 }
1455 }
1456
1457 /* Implement TARGET_MERGE_DECL_ATTRIBUTES. */
1458
1459 static tree
1460 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1461 {
1462 unsigned int diff;
1463
1464 diff = (mips_get_compress_on_flags (DECL_ATTRIBUTES (olddecl))
1465 ^ mips_get_compress_on_flags (DECL_ATTRIBUTES (newdecl)));
1466 if (diff)
1467 error ("%qE redeclared with conflicting %qs attributes",
1468 DECL_NAME (newdecl), mips_get_compress_on_name (diff));
1469
1470 diff = (mips_get_compress_off_flags (DECL_ATTRIBUTES (olddecl))
1471 ^ mips_get_compress_off_flags (DECL_ATTRIBUTES (newdecl)));
1472 if (diff)
1473 error ("%qE redeclared with conflicting %qs attributes",
1474 DECL_NAME (newdecl), mips_get_compress_off_name (diff));
1475
1476 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1477 DECL_ATTRIBUTES (newdecl));
1478 }
1479
1480 /* Implement TARGET_CAN_INLINE_P. */
1481
1482 static bool
1483 mips_can_inline_p (tree caller, tree callee)
1484 {
1485 if (mips_get_compress_mode (callee) != mips_get_compress_mode (caller))
1486 return false;
1487 return default_target_can_inline_p (caller, callee);
1488 }
1489 \f
1490 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1491 and *OFFSET_PTR. Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise. */
1492
1493 static void
1494 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1495 {
1496 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1497 {
1498 *base_ptr = XEXP (x, 0);
1499 *offset_ptr = INTVAL (XEXP (x, 1));
1500 }
1501 else
1502 {
1503 *base_ptr = x;
1504 *offset_ptr = 0;
1505 }
1506 }
1507 \f
1508 static unsigned int mips_build_integer (struct mips_integer_op *,
1509 unsigned HOST_WIDE_INT);
1510
1511 /* A subroutine of mips_build_integer, with the same interface.
1512 Assume that the final action in the sequence should be a left shift. */
1513
1514 static unsigned int
1515 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1516 {
1517 unsigned int i, shift;
1518
1519 /* Shift VALUE right until its lowest bit is set. Shift arithmetically
1520 since signed numbers are easier to load than unsigned ones. */
1521 shift = 0;
1522 while ((value & 1) == 0)
1523 value /= 2, shift++;
1524
1525 i = mips_build_integer (codes, value);
1526 codes[i].code = ASHIFT;
1527 codes[i].value = shift;
1528 return i + 1;
1529 }
1530
1531 /* As for mips_build_shift, but assume that the final action will be
1532 an IOR or PLUS operation. */
1533
1534 static unsigned int
1535 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1536 {
1537 unsigned HOST_WIDE_INT high;
1538 unsigned int i;
1539
1540 high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1541 if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1542 {
1543 /* The constant is too complex to load with a simple LUI/ORI pair,
1544 so we want to give the recursive call as many trailing zeros as
1545 possible. In this case, we know bit 16 is set and that the
1546 low 16 bits form a negative number. If we subtract that number
1547 from VALUE, we will clear at least the lowest 17 bits, maybe more. */
1548 i = mips_build_integer (codes, CONST_HIGH_PART (value));
1549 codes[i].code = PLUS;
1550 codes[i].value = CONST_LOW_PART (value);
1551 }
1552 else
1553 {
1554 /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1555 bits gives a value with at least 17 trailing zeros. */
1556 i = mips_build_integer (codes, high);
1557 codes[i].code = IOR;
1558 codes[i].value = value & 0xffff;
1559 }
1560 return i + 1;
1561 }
1562
1563 /* Fill CODES with a sequence of rtl operations to load VALUE.
1564 Return the number of operations needed. */
1565
1566 static unsigned int
1567 mips_build_integer (struct mips_integer_op *codes,
1568 unsigned HOST_WIDE_INT value)
1569 {
1570 if (SMALL_OPERAND (value)
1571 || SMALL_OPERAND_UNSIGNED (value)
1572 || LUI_OPERAND (value))
1573 {
1574 /* The value can be loaded with a single instruction. */
1575 codes[0].code = UNKNOWN;
1576 codes[0].value = value;
1577 return 1;
1578 }
1579 else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1580 {
1581 /* Either the constant is a simple LUI/ORI combination or its
1582 lowest bit is set. We don't want to shift in this case. */
1583 return mips_build_lower (codes, value);
1584 }
1585 else if ((value & 0xffff) == 0)
1586 {
1587 /* The constant will need at least three actions. The lowest
1588 16 bits are clear, so the final action will be a shift. */
1589 return mips_build_shift (codes, value);
1590 }
1591 else
1592 {
1593 /* The final action could be a shift, add or inclusive OR.
1594 Rather than use a complex condition to select the best
1595 approach, try both mips_build_shift and mips_build_lower
1596 and pick the one that gives the shortest sequence.
1597 Note that this case is only used once per constant. */
1598 struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1599 unsigned int cost, alt_cost;
1600
1601 cost = mips_build_shift (codes, value);
1602 alt_cost = mips_build_lower (alt_codes, value);
1603 if (alt_cost < cost)
1604 {
1605 memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1606 cost = alt_cost;
1607 }
1608 return cost;
1609 }
1610 }
1611 \f
1612 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
1613
1614 static bool
1615 mips_legitimate_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1616 {
1617 return mips_const_insns (x) > 0;
1618 }
1619 \f
1620 /* Return a SYMBOL_REF for a MIPS16 function called NAME. */
1621
1622 static rtx
1623 mips16_stub_function (const char *name)
1624 {
1625 rtx x;
1626
1627 x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
1628 SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
1629 return x;
1630 }
1631
1632 /* Return a legitimate call address for STUB, given that STUB is a MIPS16
1633 support function. */
1634
1635 static rtx
1636 mips16_stub_call_address (mips_one_only_stub *stub)
1637 {
1638 rtx fn = mips16_stub_function (stub->get_name ());
1639 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_LOCAL;
1640 if (!call_insn_operand (fn, VOIDmode))
1641 fn = force_reg (Pmode, fn);
1642 return fn;
1643 }
1644 \f
1645 /* A stub for moving the thread pointer into TLS_GET_TP_REGNUM. */
1646
1647 class mips16_rdhwr_one_only_stub : public mips_one_only_stub
1648 {
1649 virtual const char *get_name ();
1650 virtual void output_body ();
1651 };
1652
1653 const char *
1654 mips16_rdhwr_one_only_stub::get_name ()
1655 {
1656 return "__mips16_rdhwr";
1657 }
1658
1659 void
1660 mips16_rdhwr_one_only_stub::output_body ()
1661 {
1662 fprintf (asm_out_file,
1663 "\t.set\tpush\n"
1664 "\t.set\tmips32r2\n"
1665 "\t.set\tnoreorder\n"
1666 "\trdhwr\t$3,$29\n"
1667 "\t.set\tpop\n"
1668 "\tj\t$31\n");
1669 }
1670
1671 /* A stub for moving the FCSR into GET_FCSR_REGNUM. */
1672 class mips16_get_fcsr_one_only_stub : public mips_one_only_stub
1673 {
1674 virtual const char *get_name ();
1675 virtual void output_body ();
1676 };
1677
1678 const char *
1679 mips16_get_fcsr_one_only_stub::get_name ()
1680 {
1681 return "__mips16_get_fcsr";
1682 }
1683
1684 void
1685 mips16_get_fcsr_one_only_stub::output_body ()
1686 {
1687 fprintf (asm_out_file,
1688 "\tcfc1\t%s,$31\n"
1689 "\tj\t$31\n", reg_names[GET_FCSR_REGNUM]);
1690 }
1691
1692 /* A stub for moving SET_FCSR_REGNUM into the FCSR. */
1693 class mips16_set_fcsr_one_only_stub : public mips_one_only_stub
1694 {
1695 virtual const char *get_name ();
1696 virtual void output_body ();
1697 };
1698
1699 const char *
1700 mips16_set_fcsr_one_only_stub::get_name ()
1701 {
1702 return "__mips16_set_fcsr";
1703 }
1704
1705 void
1706 mips16_set_fcsr_one_only_stub::output_body ()
1707 {
1708 fprintf (asm_out_file,
1709 "\tctc1\t%s,$31\n"
1710 "\tj\t$31\n", reg_names[SET_FCSR_REGNUM]);
1711 }
1712 \f
1713 /* Return true if symbols of type TYPE require a GOT access. */
1714
1715 static bool
1716 mips_got_symbol_type_p (enum mips_symbol_type type)
1717 {
1718 switch (type)
1719 {
1720 case SYMBOL_GOT_PAGE_OFST:
1721 case SYMBOL_GOT_DISP:
1722 return true;
1723
1724 default:
1725 return false;
1726 }
1727 }
1728
1729 /* Return true if X is a thread-local symbol. */
1730
1731 static bool
1732 mips_tls_symbol_p (rtx x)
1733 {
1734 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1735 }
1736
1737 /* Return true if SYMBOL_REF X is associated with a global symbol
1738 (in the STB_GLOBAL sense). */
1739
1740 static bool
1741 mips_global_symbol_p (const_rtx x)
1742 {
1743 const_tree decl = SYMBOL_REF_DECL (x);
1744
1745 if (!decl)
1746 return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1747
1748 /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1749 or weak symbols. Relocations in the object file will be against
1750 the target symbol, so it's that symbol's binding that matters here. */
1751 return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1752 }
1753
1754 /* Return true if function X is a libgcc MIPS16 stub function. */
1755
1756 static bool
1757 mips16_stub_function_p (const_rtx x)
1758 {
1759 return (GET_CODE (x) == SYMBOL_REF
1760 && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1761 }
1762
1763 /* Return true if function X is a locally-defined and locally-binding
1764 MIPS16 function. */
1765
1766 static bool
1767 mips16_local_function_p (const_rtx x)
1768 {
1769 return (GET_CODE (x) == SYMBOL_REF
1770 && SYMBOL_REF_LOCAL_P (x)
1771 && !SYMBOL_REF_EXTERNAL_P (x)
1772 && (mips_get_compress_mode (SYMBOL_REF_DECL (x)) & MASK_MIPS16));
1773 }
1774
1775 /* Return true if SYMBOL_REF X binds locally. */
1776
1777 static bool
1778 mips_symbol_binds_local_p (const_rtx x)
1779 {
1780 return (SYMBOL_REF_DECL (x)
1781 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1782 : SYMBOL_REF_LOCAL_P (x));
1783 }
1784
1785 /* Return true if rtx constants of mode MODE should be put into a small
1786 data section. */
1787
1788 static bool
1789 mips_rtx_constant_in_small_data_p (enum machine_mode mode)
1790 {
1791 return (!TARGET_EMBEDDED_DATA
1792 && TARGET_LOCAL_SDATA
1793 && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1794 }
1795
1796 /* Return true if X should not be moved directly into register $25.
1797 We need this because many versions of GAS will treat "la $25,foo" as
1798 part of a call sequence and so allow a global "foo" to be lazily bound. */
1799
1800 bool
1801 mips_dangerous_for_la25_p (rtx x)
1802 {
1803 return (!TARGET_EXPLICIT_RELOCS
1804 && TARGET_USE_GOT
1805 && GET_CODE (x) == SYMBOL_REF
1806 && mips_global_symbol_p (x));
1807 }
1808
1809 /* Return true if calls to X might need $25 to be valid on entry. */
1810
1811 bool
1812 mips_use_pic_fn_addr_reg_p (const_rtx x)
1813 {
1814 if (!TARGET_USE_PIC_FN_ADDR_REG)
1815 return false;
1816
1817 /* MIPS16 stub functions are guaranteed not to use $25. */
1818 if (mips16_stub_function_p (x))
1819 return false;
1820
1821 if (GET_CODE (x) == SYMBOL_REF)
1822 {
1823 /* If PLTs and copy relocations are available, the static linker
1824 will make sure that $25 is valid on entry to the target function. */
1825 if (TARGET_ABICALLS_PIC0)
1826 return false;
1827
1828 /* Locally-defined functions use absolute accesses to set up
1829 the global pointer. */
1830 if (TARGET_ABSOLUTE_ABICALLS
1831 && mips_symbol_binds_local_p (x)
1832 && !SYMBOL_REF_EXTERNAL_P (x))
1833 return false;
1834 }
1835
1836 return true;
1837 }
1838
1839 /* Return the method that should be used to access SYMBOL_REF or
1840 LABEL_REF X in context CONTEXT. */
1841
1842 static enum mips_symbol_type
1843 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1844 {
1845 if (TARGET_RTP_PIC)
1846 return SYMBOL_GOT_DISP;
1847
1848 if (GET_CODE (x) == LABEL_REF)
1849 {
1850 /* Only return SYMBOL_PC_RELATIVE if we are generating MIPS16
1851 code and if we know that the label is in the current function's
1852 text section. LABEL_REFs are used for jump tables as well as
1853 text labels, so we must check whether jump tables live in the
1854 text section. */
1855 if (TARGET_MIPS16_SHORT_JUMP_TABLES
1856 && !LABEL_REF_NONLOCAL_P (x))
1857 return SYMBOL_PC_RELATIVE;
1858
1859 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1860 return SYMBOL_GOT_PAGE_OFST;
1861
1862 return SYMBOL_ABSOLUTE;
1863 }
1864
1865 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1866
1867 if (SYMBOL_REF_TLS_MODEL (x))
1868 return SYMBOL_TLS;
1869
1870 if (CONSTANT_POOL_ADDRESS_P (x))
1871 {
1872 if (TARGET_MIPS16_TEXT_LOADS)
1873 return SYMBOL_PC_RELATIVE;
1874
1875 if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1876 return SYMBOL_PC_RELATIVE;
1877
1878 if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1879 return SYMBOL_GP_RELATIVE;
1880 }
1881
1882 /* Do not use small-data accesses for weak symbols; they may end up
1883 being zero. */
1884 if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1885 return SYMBOL_GP_RELATIVE;
1886
1887 /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1888 is in effect. */
1889 if (TARGET_ABICALLS_PIC2
1890 && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1891 {
1892 /* There are three cases to consider:
1893
1894 - o32 PIC (either with or without explicit relocs)
1895 - n32/n64 PIC without explicit relocs
1896 - n32/n64 PIC with explicit relocs
1897
1898 In the first case, both local and global accesses will use an
1899 R_MIPS_GOT16 relocation. We must correctly predict which of
1900 the two semantics (local or global) the assembler and linker
1901 will apply. The choice depends on the symbol's binding rather
1902 than its visibility.
1903
1904 In the second case, the assembler will not use R_MIPS_GOT16
1905 relocations, but it chooses between local and global accesses
1906 in the same way as for o32 PIC.
1907
1908 In the third case we have more freedom since both forms of
1909 access will work for any kind of symbol. However, there seems
1910 little point in doing things differently. */
1911 if (mips_global_symbol_p (x))
1912 return SYMBOL_GOT_DISP;
1913
1914 return SYMBOL_GOT_PAGE_OFST;
1915 }
1916
1917 return SYMBOL_ABSOLUTE;
1918 }
1919
1920 /* Classify the base of symbolic expression X, given that X appears in
1921 context CONTEXT. */
1922
1923 static enum mips_symbol_type
1924 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1925 {
1926 rtx offset;
1927
1928 split_const (x, &x, &offset);
1929 if (UNSPEC_ADDRESS_P (x))
1930 return UNSPEC_ADDRESS_TYPE (x);
1931
1932 return mips_classify_symbol (x, context);
1933 }
1934
1935 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1936 is the alignment in bytes of SYMBOL_REF X. */
1937
1938 static bool
1939 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
1940 {
1941 HOST_WIDE_INT align;
1942
1943 align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
1944 return IN_RANGE (offset, 0, align - 1);
1945 }
1946
1947 /* Return true if X is a symbolic constant that can be used in context
1948 CONTEXT. If it is, store the type of the symbol in *SYMBOL_TYPE. */
1949
1950 bool
1951 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
1952 enum mips_symbol_type *symbol_type)
1953 {
1954 rtx offset;
1955
1956 split_const (x, &x, &offset);
1957 if (UNSPEC_ADDRESS_P (x))
1958 {
1959 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1960 x = UNSPEC_ADDRESS (x);
1961 }
1962 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1963 {
1964 *symbol_type = mips_classify_symbol (x, context);
1965 if (*symbol_type == SYMBOL_TLS)
1966 return false;
1967 }
1968 else
1969 return false;
1970
1971 if (offset == const0_rtx)
1972 return true;
1973
1974 /* Check whether a nonzero offset is valid for the underlying
1975 relocations. */
1976 switch (*symbol_type)
1977 {
1978 case SYMBOL_ABSOLUTE:
1979 case SYMBOL_64_HIGH:
1980 case SYMBOL_64_MID:
1981 case SYMBOL_64_LOW:
1982 /* If the target has 64-bit pointers and the object file only
1983 supports 32-bit symbols, the values of those symbols will be
1984 sign-extended. In this case we can't allow an arbitrary offset
1985 in case the 32-bit value X + OFFSET has a different sign from X. */
1986 if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1987 return offset_within_block_p (x, INTVAL (offset));
1988
1989 /* In other cases the relocations can handle any offset. */
1990 return true;
1991
1992 case SYMBOL_PC_RELATIVE:
1993 /* Allow constant pool references to be converted to LABEL+CONSTANT.
1994 In this case, we no longer have access to the underlying constant,
1995 but the original symbol-based access was known to be valid. */
1996 if (GET_CODE (x) == LABEL_REF)
1997 return true;
1998
1999 /* Fall through. */
2000
2001 case SYMBOL_GP_RELATIVE:
2002 /* Make sure that the offset refers to something within the
2003 same object block. This should guarantee that the final
2004 PC- or GP-relative offset is within the 16-bit limit. */
2005 return offset_within_block_p (x, INTVAL (offset));
2006
2007 case SYMBOL_GOT_PAGE_OFST:
2008 case SYMBOL_GOTOFF_PAGE:
2009 /* If the symbol is global, the GOT entry will contain the symbol's
2010 address, and we will apply a 16-bit offset after loading it.
2011 If the symbol is local, the linker should provide enough local
2012 GOT entries for a 16-bit offset, but larger offsets may lead
2013 to GOT overflow. */
2014 return SMALL_INT (offset);
2015
2016 case SYMBOL_TPREL:
2017 case SYMBOL_DTPREL:
2018 /* There is no carry between the HI and LO REL relocations, so the
2019 offset is only valid if we know it won't lead to such a carry. */
2020 return mips_offset_within_alignment_p (x, INTVAL (offset));
2021
2022 case SYMBOL_GOT_DISP:
2023 case SYMBOL_GOTOFF_DISP:
2024 case SYMBOL_GOTOFF_CALL:
2025 case SYMBOL_GOTOFF_LOADGP:
2026 case SYMBOL_TLSGD:
2027 case SYMBOL_TLSLDM:
2028 case SYMBOL_GOTTPREL:
2029 case SYMBOL_TLS:
2030 case SYMBOL_HALF:
2031 return false;
2032 }
2033 gcc_unreachable ();
2034 }
2035 \f
2036 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
2037 single instruction. We rely on the fact that, in the worst case,
2038 all instructions involved in a MIPS16 address calculation are usually
2039 extended ones. */
2040
2041 static int
2042 mips_symbol_insns_1 (enum mips_symbol_type type, enum machine_mode mode)
2043 {
2044 if (mips_use_pcrel_pool_p[(int) type])
2045 {
2046 if (mode == MAX_MACHINE_MODE)
2047 /* LEAs will be converted into constant-pool references by
2048 mips_reorg. */
2049 type = SYMBOL_PC_RELATIVE;
2050 else
2051 /* The constant must be loaded and then dereferenced. */
2052 return 0;
2053 }
2054
2055 switch (type)
2056 {
2057 case SYMBOL_ABSOLUTE:
2058 /* When using 64-bit symbols, we need 5 preparatory instructions,
2059 such as:
2060
2061 lui $at,%highest(symbol)
2062 daddiu $at,$at,%higher(symbol)
2063 dsll $at,$at,16
2064 daddiu $at,$at,%hi(symbol)
2065 dsll $at,$at,16
2066
2067 The final address is then $at + %lo(symbol). With 32-bit
2068 symbols we just need a preparatory LUI for normal mode and
2069 a preparatory LI and SLL for MIPS16. */
2070 return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
2071
2072 case SYMBOL_GP_RELATIVE:
2073 /* Treat GP-relative accesses as taking a single instruction on
2074 MIPS16 too; the copy of $gp can often be shared. */
2075 return 1;
2076
2077 case SYMBOL_PC_RELATIVE:
2078 /* PC-relative constants can be only be used with ADDIUPC,
2079 DADDIUPC, LWPC and LDPC. */
2080 if (mode == MAX_MACHINE_MODE
2081 || GET_MODE_SIZE (mode) == 4
2082 || GET_MODE_SIZE (mode) == 8)
2083 return 1;
2084
2085 /* The constant must be loaded using ADDIUPC or DADDIUPC first. */
2086 return 0;
2087
2088 case SYMBOL_GOT_DISP:
2089 /* The constant will have to be loaded from the GOT before it
2090 is used in an address. */
2091 if (mode != MAX_MACHINE_MODE)
2092 return 0;
2093
2094 /* Fall through. */
2095
2096 case SYMBOL_GOT_PAGE_OFST:
2097 /* Unless -funit-at-a-time is in effect, we can't be sure whether the
2098 local/global classification is accurate. The worst cases are:
2099
2100 (1) For local symbols when generating o32 or o64 code. The assembler
2101 will use:
2102
2103 lw $at,%got(symbol)
2104 nop
2105
2106 ...and the final address will be $at + %lo(symbol).
2107
2108 (2) For global symbols when -mxgot. The assembler will use:
2109
2110 lui $at,%got_hi(symbol)
2111 (d)addu $at,$at,$gp
2112
2113 ...and the final address will be $at + %got_lo(symbol). */
2114 return 3;
2115
2116 case SYMBOL_GOTOFF_PAGE:
2117 case SYMBOL_GOTOFF_DISP:
2118 case SYMBOL_GOTOFF_CALL:
2119 case SYMBOL_GOTOFF_LOADGP:
2120 case SYMBOL_64_HIGH:
2121 case SYMBOL_64_MID:
2122 case SYMBOL_64_LOW:
2123 case SYMBOL_TLSGD:
2124 case SYMBOL_TLSLDM:
2125 case SYMBOL_DTPREL:
2126 case SYMBOL_GOTTPREL:
2127 case SYMBOL_TPREL:
2128 case SYMBOL_HALF:
2129 /* A 16-bit constant formed by a single relocation, or a 32-bit
2130 constant formed from a high 16-bit relocation and a low 16-bit
2131 relocation. Use mips_split_p to determine which. 32-bit
2132 constants need an "lui; addiu" sequence for normal mode and
2133 an "li; sll; addiu" sequence for MIPS16 mode. */
2134 return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
2135
2136 case SYMBOL_TLS:
2137 /* We don't treat a bare TLS symbol as a constant. */
2138 return 0;
2139 }
2140 gcc_unreachable ();
2141 }
2142
2143 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
2144 to load symbols of type TYPE into a register. Return 0 if the given
2145 type of symbol cannot be used as an immediate operand.
2146
2147 Otherwise, return the number of instructions needed to load or store
2148 values of mode MODE to or from addresses of type TYPE. Return 0 if
2149 the given type of symbol is not valid in addresses.
2150
2151 In both cases, instruction counts are based off BASE_INSN_LENGTH. */
2152
2153 static int
2154 mips_symbol_insns (enum mips_symbol_type type, enum machine_mode mode)
2155 {
2156 return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
2157 }
2158 \f
2159 /* A for_each_rtx callback. Stop the search if *X references a
2160 thread-local symbol. */
2161
2162 static int
2163 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
2164 {
2165 return mips_tls_symbol_p (*x);
2166 }
2167
2168 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
2169
2170 static bool
2171 mips_cannot_force_const_mem (enum machine_mode mode, rtx x)
2172 {
2173 enum mips_symbol_type type;
2174 rtx base, offset;
2175
2176 /* There is no assembler syntax for expressing an address-sized
2177 high part. */
2178 if (GET_CODE (x) == HIGH)
2179 return true;
2180
2181 /* As an optimization, reject constants that mips_legitimize_move
2182 can expand inline.
2183
2184 Suppose we have a multi-instruction sequence that loads constant C
2185 into register R. If R does not get allocated a hard register, and
2186 R is used in an operand that allows both registers and memory
2187 references, reload will consider forcing C into memory and using
2188 one of the instruction's memory alternatives. Returning false
2189 here will force it to use an input reload instead. */
2190 if (CONST_INT_P (x) && mips_legitimate_constant_p (mode, x))
2191 return true;
2192
2193 split_const (x, &base, &offset);
2194 if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type))
2195 {
2196 /* See whether we explicitly want these symbols in the pool. */
2197 if (mips_use_pcrel_pool_p[(int) type])
2198 return false;
2199
2200 /* The same optimization as for CONST_INT. */
2201 if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
2202 return true;
2203
2204 /* If MIPS16 constant pools live in the text section, they should
2205 not refer to anything that might need run-time relocation. */
2206 if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
2207 return true;
2208 }
2209
2210 /* TLS symbols must be computed by mips_legitimize_move. */
2211 if (for_each_rtx (&x, &mips_tls_symbol_ref_1, NULL))
2212 return true;
2213
2214 return false;
2215 }
2216
2217 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. We can't use blocks for
2218 constants when we're using a per-function constant pool. */
2219
2220 static bool
2221 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
2222 const_rtx x ATTRIBUTE_UNUSED)
2223 {
2224 return !TARGET_MIPS16_PCREL_LOADS;
2225 }
2226 \f
2227 /* Return true if register REGNO is a valid base register for mode MODE.
2228 STRICT_P is true if REG_OK_STRICT is in effect. */
2229
2230 int
2231 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode,
2232 bool strict_p)
2233 {
2234 if (!HARD_REGISTER_NUM_P (regno))
2235 {
2236 if (!strict_p)
2237 return true;
2238 regno = reg_renumber[regno];
2239 }
2240
2241 /* These fake registers will be eliminated to either the stack or
2242 hard frame pointer, both of which are usually valid base registers.
2243 Reload deals with the cases where the eliminated form isn't valid. */
2244 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2245 return true;
2246
2247 /* In MIPS16 mode, the stack pointer can only address word and doubleword
2248 values, nothing smaller. There are two problems here:
2249
2250 (a) Instantiating virtual registers can introduce new uses of the
2251 stack pointer. If these virtual registers are valid addresses,
2252 the stack pointer should be too.
2253
2254 (b) Most uses of the stack pointer are not made explicit until
2255 FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
2256 We don't know until that stage whether we'll be eliminating to the
2257 stack pointer (which needs the restriction) or the hard frame
2258 pointer (which doesn't).
2259
2260 All in all, it seems more consistent to only enforce this restriction
2261 during and after reload. */
2262 if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2263 return !strict_p || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2264
2265 return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2266 }
2267
2268 /* Return true if X is a valid base register for mode MODE.
2269 STRICT_P is true if REG_OK_STRICT is in effect. */
2270
2271 static bool
2272 mips_valid_base_register_p (rtx x, enum machine_mode mode, bool strict_p)
2273 {
2274 if (!strict_p && GET_CODE (x) == SUBREG)
2275 x = SUBREG_REG (x);
2276
2277 return (REG_P (x)
2278 && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2279 }
2280
2281 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2282 can address a value of mode MODE. */
2283
2284 static bool
2285 mips_valid_offset_p (rtx x, enum machine_mode mode)
2286 {
2287 /* Check that X is a signed 16-bit number. */
2288 if (!const_arith_operand (x, Pmode))
2289 return false;
2290
2291 /* We may need to split multiword moves, so make sure that every word
2292 is accessible. */
2293 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2294 && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2295 return false;
2296
2297 return true;
2298 }
2299
2300 /* Return true if a LO_SUM can address a value of mode MODE when the
2301 LO_SUM symbol has type SYMBOL_TYPE. */
2302
2303 static bool
2304 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, enum machine_mode mode)
2305 {
2306 /* Check that symbols of type SYMBOL_TYPE can be used to access values
2307 of mode MODE. */
2308 if (mips_symbol_insns (symbol_type, mode) == 0)
2309 return false;
2310
2311 /* Check that there is a known low-part relocation. */
2312 if (mips_lo_relocs[symbol_type] == NULL)
2313 return false;
2314
2315 /* We may need to split multiword moves, so make sure that each word
2316 can be accessed without inducing a carry. This is mainly needed
2317 for o64, which has historically only guaranteed 64-bit alignment
2318 for 128-bit types. */
2319 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2320 && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2321 return false;
2322
2323 return true;
2324 }
2325
2326 /* Return true if X is a valid address for machine mode MODE. If it is,
2327 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
2328 effect. */
2329
2330 static bool
2331 mips_classify_address (struct mips_address_info *info, rtx x,
2332 enum machine_mode mode, bool strict_p)
2333 {
2334 switch (GET_CODE (x))
2335 {
2336 case REG:
2337 case SUBREG:
2338 info->type = ADDRESS_REG;
2339 info->reg = x;
2340 info->offset = const0_rtx;
2341 return mips_valid_base_register_p (info->reg, mode, strict_p);
2342
2343 case PLUS:
2344 info->type = ADDRESS_REG;
2345 info->reg = XEXP (x, 0);
2346 info->offset = XEXP (x, 1);
2347 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2348 && mips_valid_offset_p (info->offset, mode));
2349
2350 case LO_SUM:
2351 info->type = ADDRESS_LO_SUM;
2352 info->reg = XEXP (x, 0);
2353 info->offset = XEXP (x, 1);
2354 /* We have to trust the creator of the LO_SUM to do something vaguely
2355 sane. Target-independent code that creates a LO_SUM should also
2356 create and verify the matching HIGH. Target-independent code that
2357 adds an offset to a LO_SUM must prove that the offset will not
2358 induce a carry. Failure to do either of these things would be
2359 a bug, and we are not required to check for it here. The MIPS
2360 backend itself should only create LO_SUMs for valid symbolic
2361 constants, with the high part being either a HIGH or a copy
2362 of _gp. */
2363 info->symbol_type
2364 = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2365 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2366 && mips_valid_lo_sum_p (info->symbol_type, mode));
2367
2368 case CONST_INT:
2369 /* Small-integer addresses don't occur very often, but they
2370 are legitimate if $0 is a valid base register. */
2371 info->type = ADDRESS_CONST_INT;
2372 return !TARGET_MIPS16 && SMALL_INT (x);
2373
2374 case CONST:
2375 case LABEL_REF:
2376 case SYMBOL_REF:
2377 info->type = ADDRESS_SYMBOLIC;
2378 return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2379 &info->symbol_type)
2380 && mips_symbol_insns (info->symbol_type, mode) > 0
2381 && !mips_split_p[info->symbol_type]);
2382
2383 default:
2384 return false;
2385 }
2386 }
2387
2388 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
2389
2390 static bool
2391 mips_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
2392 {
2393 struct mips_address_info addr;
2394
2395 return mips_classify_address (&addr, x, mode, strict_p);
2396 }
2397
2398 /* Return true if X is a legitimate $sp-based address for mode MDOE. */
2399
2400 bool
2401 mips_stack_address_p (rtx x, enum machine_mode mode)
2402 {
2403 struct mips_address_info addr;
2404
2405 return (mips_classify_address (&addr, x, mode, false)
2406 && addr.type == ADDRESS_REG
2407 && addr.reg == stack_pointer_rtx);
2408 }
2409
2410 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2411 address instruction. Note that such addresses are not considered
2412 legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2413 is so restricted. */
2414
2415 static bool
2416 mips_lwxs_address_p (rtx addr)
2417 {
2418 if (ISA_HAS_LWXS
2419 && GET_CODE (addr) == PLUS
2420 && REG_P (XEXP (addr, 1)))
2421 {
2422 rtx offset = XEXP (addr, 0);
2423 if (GET_CODE (offset) == MULT
2424 && REG_P (XEXP (offset, 0))
2425 && CONST_INT_P (XEXP (offset, 1))
2426 && INTVAL (XEXP (offset, 1)) == 4)
2427 return true;
2428 }
2429 return false;
2430 }
2431
2432 /* Return true if ADDR matches the pattern for the L{B,H,W,D}{,U}X load
2433 indexed address instruction. Note that such addresses are
2434 not considered legitimate in the TARGET_LEGITIMATE_ADDRESS_P
2435 sense, because their use is so restricted. */
2436
2437 static bool
2438 mips_lx_address_p (rtx addr, enum machine_mode mode)
2439 {
2440 if (GET_CODE (addr) != PLUS
2441 || !REG_P (XEXP (addr, 0))
2442 || !REG_P (XEXP (addr, 1)))
2443 return false;
2444 if (ISA_HAS_LBX && mode == QImode)
2445 return true;
2446 if (ISA_HAS_LHX && mode == HImode)
2447 return true;
2448 if (ISA_HAS_LWX && mode == SImode)
2449 return true;
2450 if (ISA_HAS_LDX && mode == DImode)
2451 return true;
2452 return false;
2453 }
2454 \f
2455 /* Return true if a value at OFFSET bytes from base register BASE can be
2456 accessed using an unextended MIPS16 instruction. MODE is the mode of
2457 the value.
2458
2459 Usually the offset in an unextended instruction is a 5-bit field.
2460 The offset is unsigned and shifted left once for LH and SH, twice
2461 for LW and SW, and so on. An exception is LWSP and SWSP, which have
2462 an 8-bit immediate field that's shifted left twice. */
2463
2464 static bool
2465 mips16_unextended_reference_p (enum machine_mode mode, rtx base,
2466 unsigned HOST_WIDE_INT offset)
2467 {
2468 if (mode != BLKmode && offset % GET_MODE_SIZE (mode) == 0)
2469 {
2470 if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2471 return offset < 256U * GET_MODE_SIZE (mode);
2472 return offset < 32U * GET_MODE_SIZE (mode);
2473 }
2474 return false;
2475 }
2476
2477 /* Return the number of instructions needed to load or store a value
2478 of mode MODE at address X, assuming that BASE_INSN_LENGTH is the
2479 length of one instruction. Return 0 if X isn't valid for MODE.
2480 Assume that multiword moves may need to be split into word moves
2481 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2482 enough. */
2483
2484 int
2485 mips_address_insns (rtx x, enum machine_mode mode, bool might_split_p)
2486 {
2487 struct mips_address_info addr;
2488 int factor;
2489
2490 /* BLKmode is used for single unaligned loads and stores and should
2491 not count as a multiword mode. (GET_MODE_SIZE (BLKmode) is pretty
2492 meaningless, so we have to single it out as a special case one way
2493 or the other.) */
2494 if (mode != BLKmode && might_split_p)
2495 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2496 else
2497 factor = 1;
2498
2499 if (mips_classify_address (&addr, x, mode, false))
2500 switch (addr.type)
2501 {
2502 case ADDRESS_REG:
2503 if (TARGET_MIPS16
2504 && !mips16_unextended_reference_p (mode, addr.reg,
2505 UINTVAL (addr.offset)))
2506 return factor * 2;
2507 return factor;
2508
2509 case ADDRESS_LO_SUM:
2510 return TARGET_MIPS16 ? factor * 2 : factor;
2511
2512 case ADDRESS_CONST_INT:
2513 return factor;
2514
2515 case ADDRESS_SYMBOLIC:
2516 return factor * mips_symbol_insns (addr.symbol_type, mode);
2517 }
2518 return 0;
2519 }
2520
2521 /* Return true if X fits within an unsigned field of BITS bits that is
2522 shifted left SHIFT bits before being used. */
2523
2524 bool
2525 mips_unsigned_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2526 {
2527 return (x & ((1 << shift) - 1)) == 0 && x < ((unsigned) 1 << (shift + bits));
2528 }
2529
2530 /* Return true if X fits within a signed field of BITS bits that is
2531 shifted left SHIFT bits before being used. */
2532
2533 bool
2534 mips_signed_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2535 {
2536 x += 1 << (bits + shift - 1);
2537 return mips_unsigned_immediate_p (x, bits, shift);
2538 }
2539
2540 /* Return true if X is legitimate for accessing values of mode MODE,
2541 if it is based on a MIPS16 register, and if the offset satisfies
2542 OFFSET_PREDICATE. */
2543
2544 bool
2545 m16_based_address_p (rtx x, enum machine_mode mode,
2546 insn_operand_predicate_fn offset_predicate)
2547 {
2548 struct mips_address_info addr;
2549
2550 return (mips_classify_address (&addr, x, mode, false)
2551 && addr.type == ADDRESS_REG
2552 && M16_REG_P (REGNO (addr.reg))
2553 && offset_predicate (addr.offset, mode));
2554 }
2555
2556 /* Return true if X is a legitimate address that conforms to the requirements
2557 for a microMIPS LWSP or SWSP insn. */
2558
2559 bool
2560 lwsp_swsp_address_p (rtx x, enum machine_mode mode)
2561 {
2562 struct mips_address_info addr;
2563
2564 return (mips_classify_address (&addr, x, mode, false)
2565 && addr.type == ADDRESS_REG
2566 && REGNO (addr.reg) == STACK_POINTER_REGNUM
2567 && uw5_operand (addr.offset, mode));
2568 }
2569
2570 /* Return true if X is a legitimate address with a 12-bit offset.
2571 MODE is the mode of the value being accessed. */
2572
2573 bool
2574 umips_12bit_offset_address_p (rtx x, enum machine_mode mode)
2575 {
2576 struct mips_address_info addr;
2577
2578 return (mips_classify_address (&addr, x, mode, false)
2579 && addr.type == ADDRESS_REG
2580 && CONST_INT_P (addr.offset)
2581 && UMIPS_12BIT_OFFSET_P (INTVAL (addr.offset)));
2582 }
2583
2584 /* Return the number of instructions needed to load constant X,
2585 assuming that BASE_INSN_LENGTH is the length of one instruction.
2586 Return 0 if X isn't a valid constant. */
2587
2588 int
2589 mips_const_insns (rtx x)
2590 {
2591 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2592 enum mips_symbol_type symbol_type;
2593 rtx offset;
2594
2595 switch (GET_CODE (x))
2596 {
2597 case HIGH:
2598 if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2599 &symbol_type)
2600 || !mips_split_p[symbol_type])
2601 return 0;
2602
2603 /* This is simply an LUI for normal mode. It is an extended
2604 LI followed by an extended SLL for MIPS16. */
2605 return TARGET_MIPS16 ? 4 : 1;
2606
2607 case CONST_INT:
2608 if (TARGET_MIPS16)
2609 /* Unsigned 8-bit constants can be loaded using an unextended
2610 LI instruction. Unsigned 16-bit constants can be loaded
2611 using an extended LI. Negative constants must be loaded
2612 using LI and then negated. */
2613 return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2614 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2615 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
2616 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2617 : 0);
2618
2619 return mips_build_integer (codes, INTVAL (x));
2620
2621 case CONST_DOUBLE:
2622 case CONST_VECTOR:
2623 /* Allow zeros for normal mode, where we can use $0. */
2624 return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2625
2626 case CONST:
2627 if (CONST_GP_P (x))
2628 return 1;
2629
2630 /* See if we can refer to X directly. */
2631 if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2632 return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2633
2634 /* Otherwise try splitting the constant into a base and offset.
2635 If the offset is a 16-bit value, we can load the base address
2636 into a register and then use (D)ADDIU to add in the offset.
2637 If the offset is larger, we can load the base and offset
2638 into separate registers and add them together with (D)ADDU.
2639 However, the latter is only possible before reload; during
2640 and after reload, we must have the option of forcing the
2641 constant into the pool instead. */
2642 split_const (x, &x, &offset);
2643 if (offset != 0)
2644 {
2645 int n = mips_const_insns (x);
2646 if (n != 0)
2647 {
2648 if (SMALL_INT (offset))
2649 return n + 1;
2650 else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
2651 return n + 1 + mips_build_integer (codes, INTVAL (offset));
2652 }
2653 }
2654 return 0;
2655
2656 case SYMBOL_REF:
2657 case LABEL_REF:
2658 return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2659 MAX_MACHINE_MODE);
2660
2661 default:
2662 return 0;
2663 }
2664 }
2665
2666 /* X is a doubleword constant that can be handled by splitting it into
2667 two words and loading each word separately. Return the number of
2668 instructions required to do this, assuming that BASE_INSN_LENGTH
2669 is the length of one instruction. */
2670
2671 int
2672 mips_split_const_insns (rtx x)
2673 {
2674 unsigned int low, high;
2675
2676 low = mips_const_insns (mips_subword (x, false));
2677 high = mips_const_insns (mips_subword (x, true));
2678 gcc_assert (low > 0 && high > 0);
2679 return low + high;
2680 }
2681
2682 /* Return the number of instructions needed to implement INSN,
2683 given that it loads from or stores to MEM. Assume that
2684 BASE_INSN_LENGTH is the length of one instruction. */
2685
2686 int
2687 mips_load_store_insns (rtx mem, rtx insn)
2688 {
2689 enum machine_mode mode;
2690 bool might_split_p;
2691 rtx set;
2692
2693 gcc_assert (MEM_P (mem));
2694 mode = GET_MODE (mem);
2695
2696 /* Try to prove that INSN does not need to be split. */
2697 might_split_p = GET_MODE_SIZE (mode) > UNITS_PER_WORD;
2698 if (might_split_p)
2699 {
2700 set = single_set (insn);
2701 if (set && !mips_split_move_insn_p (SET_DEST (set), SET_SRC (set), insn))
2702 might_split_p = false;
2703 }
2704
2705 return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2706 }
2707
2708 /* Return the number of instructions needed for an integer division,
2709 assuming that BASE_INSN_LENGTH is the length of one instruction. */
2710
2711 int
2712 mips_idiv_insns (void)
2713 {
2714 int count;
2715
2716 count = 1;
2717 if (TARGET_CHECK_ZERO_DIV)
2718 {
2719 if (GENERATE_DIVIDE_TRAPS)
2720 count++;
2721 else
2722 count += 2;
2723 }
2724
2725 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2726 count++;
2727 return count;
2728 }
2729 \f
2730 /* Emit a move from SRC to DEST. Assume that the move expanders can
2731 handle all moves if !can_create_pseudo_p (). The distinction is
2732 important because, unlike emit_move_insn, the move expanders know
2733 how to force Pmode objects into the constant pool even when the
2734 constant pool address is not itself legitimate. */
2735
2736 rtx
2737 mips_emit_move (rtx dest, rtx src)
2738 {
2739 return (can_create_pseudo_p ()
2740 ? emit_move_insn (dest, src)
2741 : emit_move_insn_1 (dest, src));
2742 }
2743
2744 /* Emit a move from SRC to DEST, splitting compound moves into individual
2745 instructions. SPLIT_TYPE is the type of split to perform. */
2746
2747 static void
2748 mips_emit_move_or_split (rtx dest, rtx src, enum mips_split_type split_type)
2749 {
2750 if (mips_split_move_p (dest, src, split_type))
2751 mips_split_move (dest, src, split_type);
2752 else
2753 mips_emit_move (dest, src);
2754 }
2755
2756 /* Emit an instruction of the form (set TARGET (CODE OP0)). */
2757
2758 static void
2759 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
2760 {
2761 emit_insn (gen_rtx_SET (VOIDmode, target,
2762 gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
2763 }
2764
2765 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
2766 Return that new register. */
2767
2768 static rtx
2769 mips_force_unary (enum machine_mode mode, enum rtx_code code, rtx op0)
2770 {
2771 rtx reg;
2772
2773 reg = gen_reg_rtx (mode);
2774 mips_emit_unary (code, reg, op0);
2775 return reg;
2776 }
2777
2778 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)). */
2779
2780 void
2781 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2782 {
2783 emit_insn (gen_rtx_SET (VOIDmode, target,
2784 gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2785 }
2786
2787 /* Compute (CODE OP0 OP1) and store the result in a new register
2788 of mode MODE. Return that new register. */
2789
2790 static rtx
2791 mips_force_binary (enum machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2792 {
2793 rtx reg;
2794
2795 reg = gen_reg_rtx (mode);
2796 mips_emit_binary (code, reg, op0, op1);
2797 return reg;
2798 }
2799
2800 /* Copy VALUE to a register and return that register. If new pseudos
2801 are allowed, copy it into a new register, otherwise use DEST. */
2802
2803 static rtx
2804 mips_force_temporary (rtx dest, rtx value)
2805 {
2806 if (can_create_pseudo_p ())
2807 return force_reg (Pmode, value);
2808 else
2809 {
2810 mips_emit_move (dest, value);
2811 return dest;
2812 }
2813 }
2814
2815 /* Emit a call sequence with call pattern PATTERN and return the call
2816 instruction itself (which is not necessarily the last instruction
2817 emitted). ORIG_ADDR is the original, unlegitimized address,
2818 ADDR is the legitimized form, and LAZY_P is true if the call
2819 address is lazily-bound. */
2820
2821 static rtx
2822 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
2823 {
2824 rtx insn, reg;
2825
2826 insn = emit_call_insn (pattern);
2827
2828 if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
2829 {
2830 /* MIPS16 JALRs only take MIPS16 registers. If the target
2831 function requires $25 to be valid on entry, we must copy it
2832 there separately. The move instruction can be put in the
2833 call's delay slot. */
2834 reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
2835 emit_insn_before (gen_move_insn (reg, addr), insn);
2836 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
2837 }
2838
2839 if (lazy_p)
2840 /* Lazy-binding stubs require $gp to be valid on entry. */
2841 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2842
2843 if (TARGET_USE_GOT)
2844 {
2845 /* See the comment above load_call<mode> for details. */
2846 use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2847 gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2848 emit_insn (gen_update_got_version ());
2849 }
2850 return insn;
2851 }
2852 \f
2853 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2854 then add CONST_INT OFFSET to the result. */
2855
2856 static rtx
2857 mips_unspec_address_offset (rtx base, rtx offset,
2858 enum mips_symbol_type symbol_type)
2859 {
2860 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2861 UNSPEC_ADDRESS_FIRST + symbol_type);
2862 if (offset != const0_rtx)
2863 base = gen_rtx_PLUS (Pmode, base, offset);
2864 return gen_rtx_CONST (Pmode, base);
2865 }
2866
2867 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2868 type SYMBOL_TYPE. */
2869
2870 rtx
2871 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2872 {
2873 rtx base, offset;
2874
2875 split_const (address, &base, &offset);
2876 return mips_unspec_address_offset (base, offset, symbol_type);
2877 }
2878
2879 /* If OP is an UNSPEC address, return the address to which it refers,
2880 otherwise return OP itself. */
2881
2882 rtx
2883 mips_strip_unspec_address (rtx op)
2884 {
2885 rtx base, offset;
2886
2887 split_const (op, &base, &offset);
2888 if (UNSPEC_ADDRESS_P (base))
2889 op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
2890 return op;
2891 }
2892
2893 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2894 high part to BASE and return the result. Just return BASE otherwise.
2895 TEMP is as for mips_force_temporary.
2896
2897 The returned expression can be used as the first operand to a LO_SUM. */
2898
2899 static rtx
2900 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2901 enum mips_symbol_type symbol_type)
2902 {
2903 if (mips_split_p[symbol_type])
2904 {
2905 addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2906 addr = mips_force_temporary (temp, addr);
2907 base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2908 }
2909 return base;
2910 }
2911 \f
2912 /* Return an instruction that copies $gp into register REG. We want
2913 GCC to treat the register's value as constant, so that its value
2914 can be rematerialized on demand. */
2915
2916 static rtx
2917 gen_load_const_gp (rtx reg)
2918 {
2919 return PMODE_INSN (gen_load_const_gp, (reg));
2920 }
2921
2922 /* Return a pseudo register that contains the value of $gp throughout
2923 the current function. Such registers are needed by MIPS16 functions,
2924 for which $gp itself is not a valid base register or addition operand. */
2925
2926 static rtx
2927 mips16_gp_pseudo_reg (void)
2928 {
2929 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2930 {
2931 rtx insn, scan;
2932
2933 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2934
2935 push_topmost_sequence ();
2936
2937 scan = get_insns ();
2938 while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
2939 scan = NEXT_INSN (scan);
2940
2941 insn = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
2942 insn = emit_insn_after (insn, scan);
2943 INSN_LOCATION (insn) = 0;
2944
2945 pop_topmost_sequence ();
2946 }
2947
2948 return cfun->machine->mips16_gp_pseudo_rtx;
2949 }
2950
2951 /* Return a base register that holds pic_offset_table_rtx.
2952 TEMP, if nonnull, is a scratch Pmode base register. */
2953
2954 rtx
2955 mips_pic_base_register (rtx temp)
2956 {
2957 if (!TARGET_MIPS16)
2958 return pic_offset_table_rtx;
2959
2960 if (currently_expanding_to_rtl)
2961 return mips16_gp_pseudo_reg ();
2962
2963 if (can_create_pseudo_p ())
2964 temp = gen_reg_rtx (Pmode);
2965
2966 if (TARGET_USE_GOT)
2967 /* The first post-reload split exposes all references to $gp
2968 (both uses and definitions). All references must remain
2969 explicit after that point.
2970
2971 It is safe to introduce uses of $gp at any time, so for
2972 simplicity, we do that before the split too. */
2973 mips_emit_move (temp, pic_offset_table_rtx);
2974 else
2975 emit_insn (gen_load_const_gp (temp));
2976 return temp;
2977 }
2978
2979 /* Return the RHS of a load_call<mode> insn. */
2980
2981 static rtx
2982 mips_unspec_call (rtx reg, rtx symbol)
2983 {
2984 rtvec vec;
2985
2986 vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
2987 return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
2988 }
2989
2990 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
2991 reference. Return NULL_RTX otherwise. */
2992
2993 static rtx
2994 mips_strip_unspec_call (rtx src)
2995 {
2996 if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
2997 return mips_strip_unspec_address (XVECEXP (src, 0, 1));
2998 return NULL_RTX;
2999 }
3000
3001 /* Create and return a GOT reference of type TYPE for address ADDR.
3002 TEMP, if nonnull, is a scratch Pmode base register. */
3003
3004 rtx
3005 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
3006 {
3007 rtx base, high, lo_sum_symbol;
3008
3009 base = mips_pic_base_register (temp);
3010
3011 /* If we used the temporary register to load $gp, we can't use
3012 it for the high part as well. */
3013 if (temp != NULL && reg_overlap_mentioned_p (base, temp))
3014 temp = NULL;
3015
3016 high = mips_unspec_offset_high (temp, base, addr, type);
3017 lo_sum_symbol = mips_unspec_address (addr, type);
3018
3019 if (type == SYMBOL_GOTOFF_CALL)
3020 return mips_unspec_call (high, lo_sum_symbol);
3021 else
3022 return PMODE_INSN (gen_unspec_got, (high, lo_sum_symbol));
3023 }
3024
3025 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
3026 it appears in a MEM of that mode. Return true if ADDR is a legitimate
3027 constant in that context and can be split into high and low parts.
3028 If so, and if LOW_OUT is nonnull, emit the high part and store the
3029 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
3030
3031 TEMP is as for mips_force_temporary and is used to load the high
3032 part into a register.
3033
3034 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
3035 a legitimize SET_SRC for an .md pattern, otherwise the low part
3036 is guaranteed to be a legitimate address for mode MODE. */
3037
3038 bool
3039 mips_split_symbol (rtx temp, rtx addr, enum machine_mode mode, rtx *low_out)
3040 {
3041 enum mips_symbol_context context;
3042 enum mips_symbol_type symbol_type;
3043 rtx high;
3044
3045 context = (mode == MAX_MACHINE_MODE
3046 ? SYMBOL_CONTEXT_LEA
3047 : SYMBOL_CONTEXT_MEM);
3048 if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
3049 {
3050 addr = XEXP (addr, 0);
3051 if (mips_symbolic_constant_p (addr, context, &symbol_type)
3052 && mips_symbol_insns (symbol_type, mode) > 0
3053 && mips_split_hi_p[symbol_type])
3054 {
3055 if (low_out)
3056 switch (symbol_type)
3057 {
3058 case SYMBOL_GOT_PAGE_OFST:
3059 /* The high part of a page/ofst pair is loaded from the GOT. */
3060 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
3061 break;
3062
3063 default:
3064 gcc_unreachable ();
3065 }
3066 return true;
3067 }
3068 }
3069 else
3070 {
3071 if (mips_symbolic_constant_p (addr, context, &symbol_type)
3072 && mips_symbol_insns (symbol_type, mode) > 0
3073 && mips_split_p[symbol_type])
3074 {
3075 if (low_out)
3076 switch (symbol_type)
3077 {
3078 case SYMBOL_GOT_DISP:
3079 /* SYMBOL_GOT_DISP symbols are loaded from the GOT. */
3080 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
3081 break;
3082
3083 case SYMBOL_GP_RELATIVE:
3084 high = mips_pic_base_register (temp);
3085 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3086 break;
3087
3088 default:
3089 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
3090 high = mips_force_temporary (temp, high);
3091 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3092 break;
3093 }
3094 return true;
3095 }
3096 }
3097 return false;
3098 }
3099
3100 /* Return a legitimate address for REG + OFFSET. TEMP is as for
3101 mips_force_temporary; it is only needed when OFFSET is not a
3102 SMALL_OPERAND. */
3103
3104 static rtx
3105 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
3106 {
3107 if (!SMALL_OPERAND (offset))
3108 {
3109 rtx high;
3110
3111 if (TARGET_MIPS16)
3112 {
3113 /* Load the full offset into a register so that we can use
3114 an unextended instruction for the address itself. */
3115 high = GEN_INT (offset);
3116 offset = 0;
3117 }
3118 else
3119 {
3120 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
3121 The addition inside the macro CONST_HIGH_PART may cause an
3122 overflow, so we need to force a sign-extension check. */
3123 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
3124 offset = CONST_LOW_PART (offset);
3125 }
3126 high = mips_force_temporary (temp, high);
3127 reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
3128 }
3129 return plus_constant (Pmode, reg, offset);
3130 }
3131 \f
3132 /* The __tls_get_attr symbol. */
3133 static GTY(()) rtx mips_tls_symbol;
3134
3135 /* Return an instruction sequence that calls __tls_get_addr. SYM is
3136 the TLS symbol we are referencing and TYPE is the symbol type to use
3137 (either global dynamic or local dynamic). V0 is an RTX for the
3138 return value location. */
3139
3140 static rtx
3141 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
3142 {
3143 rtx insn, loc, a0;
3144
3145 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3146
3147 if (!mips_tls_symbol)
3148 mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
3149
3150 loc = mips_unspec_address (sym, type);
3151
3152 start_sequence ();
3153
3154 emit_insn (gen_rtx_SET (Pmode, a0,
3155 gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
3156 insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
3157 const0_rtx, NULL_RTX, false);
3158 RTL_CONST_CALL_P (insn) = 1;
3159 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
3160 insn = get_insns ();
3161
3162 end_sequence ();
3163
3164 return insn;
3165 }
3166
3167 /* Return a pseudo register that contains the current thread pointer. */
3168
3169 rtx
3170 mips_expand_thread_pointer (rtx tp)
3171 {
3172 rtx fn;
3173
3174 if (TARGET_MIPS16)
3175 {
3176 if (!mips16_rdhwr_stub)
3177 mips16_rdhwr_stub = new mips16_rdhwr_one_only_stub ();
3178 fn = mips16_stub_call_address (mips16_rdhwr_stub);
3179 emit_insn (PMODE_INSN (gen_tls_get_tp_mips16, (tp, fn)));
3180 }
3181 else
3182 emit_insn (PMODE_INSN (gen_tls_get_tp, (tp)));
3183 return tp;
3184 }
3185
3186 static rtx
3187 mips_get_tp (void)
3188 {
3189 return mips_expand_thread_pointer (gen_reg_rtx (Pmode));
3190 }
3191
3192 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
3193 its address. The return value will be both a valid address and a valid
3194 SET_SRC (either a REG or a LO_SUM). */
3195
3196 static rtx
3197 mips_legitimize_tls_address (rtx loc)
3198 {
3199 rtx dest, insn, v0, tp, tmp1, tmp2, eqv, offset;
3200 enum tls_model model;
3201
3202 model = SYMBOL_REF_TLS_MODEL (loc);
3203 /* Only TARGET_ABICALLS code can have more than one module; other
3204 code must be be static and should not use a GOT. All TLS models
3205 reduce to local exec in this situation. */
3206 if (!TARGET_ABICALLS)
3207 model = TLS_MODEL_LOCAL_EXEC;
3208
3209 switch (model)
3210 {
3211 case TLS_MODEL_GLOBAL_DYNAMIC:
3212 v0 = gen_rtx_REG (Pmode, GP_RETURN);
3213 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
3214 dest = gen_reg_rtx (Pmode);
3215 emit_libcall_block (insn, dest, v0, loc);
3216 break;
3217
3218 case TLS_MODEL_LOCAL_DYNAMIC:
3219 v0 = gen_rtx_REG (Pmode, GP_RETURN);
3220 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
3221 tmp1 = gen_reg_rtx (Pmode);
3222
3223 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3224 share the LDM result with other LD model accesses. */
3225 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3226 UNSPEC_TLS_LDM);
3227 emit_libcall_block (insn, tmp1, v0, eqv);
3228
3229 offset = mips_unspec_address (loc, SYMBOL_DTPREL);
3230 if (mips_split_p[SYMBOL_DTPREL])
3231 {
3232 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
3233 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3234 }
3235 else
3236 dest = expand_binop (Pmode, add_optab, tmp1, offset,
3237 0, 0, OPTAB_DIRECT);
3238 break;
3239
3240 case TLS_MODEL_INITIAL_EXEC:
3241 tp = mips_get_tp ();
3242 tmp1 = gen_reg_rtx (Pmode);
3243 tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
3244 if (Pmode == DImode)
3245 emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
3246 else
3247 emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
3248 dest = gen_reg_rtx (Pmode);
3249 emit_insn (gen_add3_insn (dest, tmp1, tp));
3250 break;
3251
3252 case TLS_MODEL_LOCAL_EXEC:
3253 tmp1 = mips_get_tp ();
3254 offset = mips_unspec_address (loc, SYMBOL_TPREL);
3255 if (mips_split_p[SYMBOL_TPREL])
3256 {
3257 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_TPREL);
3258 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3259 }
3260 else
3261 dest = expand_binop (Pmode, add_optab, tmp1, offset,
3262 0, 0, OPTAB_DIRECT);
3263 break;
3264
3265 default:
3266 gcc_unreachable ();
3267 }
3268 return dest;
3269 }
3270 \f
3271 /* Implement "TARGET = __builtin_mips_get_fcsr ()" for MIPS16,
3272 using a stub. */
3273
3274 void
3275 mips16_expand_get_fcsr (rtx target)
3276 {
3277 if (!mips16_get_fcsr_stub)
3278 mips16_get_fcsr_stub = new mips16_get_fcsr_one_only_stub ();
3279 rtx fn = mips16_stub_call_address (mips16_get_fcsr_stub);
3280 emit_insn (PMODE_INSN (gen_mips_get_fcsr_mips16, (fn)));
3281 emit_move_insn (target, gen_rtx_REG (SImode, GET_FCSR_REGNUM));
3282 }
3283
3284 /* Implement __builtin_mips_set_fcsr (TARGET) for MIPS16, using a stub. */
3285
3286 void
3287 mips16_expand_set_fcsr (rtx newval)
3288 {
3289 if (!mips16_set_fcsr_stub)
3290 mips16_set_fcsr_stub = new mips16_set_fcsr_one_only_stub ();
3291 rtx fn = mips16_stub_call_address (mips16_set_fcsr_stub);
3292 emit_move_insn (gen_rtx_REG (SImode, SET_FCSR_REGNUM), newval);
3293 emit_insn (PMODE_INSN (gen_mips_set_fcsr_mips16, (fn)));
3294 }
3295 \f
3296 /* If X is not a valid address for mode MODE, force it into a register. */
3297
3298 static rtx
3299 mips_force_address (rtx x, enum machine_mode mode)
3300 {
3301 if (!mips_legitimate_address_p (mode, x, false))
3302 x = force_reg (Pmode, x);
3303 return x;
3304 }
3305
3306 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
3307 be legitimized in a way that the generic machinery might not expect,
3308 return a new address, otherwise return NULL. MODE is the mode of
3309 the memory being accessed. */
3310
3311 static rtx
3312 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3313 enum machine_mode mode)
3314 {
3315 rtx base, addr;
3316 HOST_WIDE_INT offset;
3317
3318 if (mips_tls_symbol_p (x))
3319 return mips_legitimize_tls_address (x);
3320
3321 /* See if the address can split into a high part and a LO_SUM. */
3322 if (mips_split_symbol (NULL, x, mode, &addr))
3323 return mips_force_address (addr, mode);
3324
3325 /* Handle BASE + OFFSET using mips_add_offset. */
3326 mips_split_plus (x, &base, &offset);
3327 if (offset != 0)
3328 {
3329 if (!mips_valid_base_register_p (base, mode, false))
3330 base = copy_to_mode_reg (Pmode, base);
3331 addr = mips_add_offset (NULL, base, offset);
3332 return mips_force_address (addr, mode);
3333 }
3334
3335 return x;
3336 }
3337
3338 /* Load VALUE into DEST. TEMP is as for mips_force_temporary. */
3339
3340 void
3341 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3342 {
3343 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3344 enum machine_mode mode;
3345 unsigned int i, num_ops;
3346 rtx x;
3347
3348 mode = GET_MODE (dest);
3349 num_ops = mips_build_integer (codes, value);
3350
3351 /* Apply each binary operation to X. Invariant: X is a legitimate
3352 source operand for a SET pattern. */
3353 x = GEN_INT (codes[0].value);
3354 for (i = 1; i < num_ops; i++)
3355 {
3356 if (!can_create_pseudo_p ())
3357 {
3358 emit_insn (gen_rtx_SET (VOIDmode, temp, x));
3359 x = temp;
3360 }
3361 else
3362 x = force_reg (mode, x);
3363 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3364 }
3365
3366 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
3367 }
3368
3369 /* Subroutine of mips_legitimize_move. Move constant SRC into register
3370 DEST given that SRC satisfies immediate_operand but doesn't satisfy
3371 move_operand. */
3372
3373 static void
3374 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
3375 {
3376 rtx base, offset;
3377
3378 /* Split moves of big integers into smaller pieces. */
3379 if (splittable_const_int_operand (src, mode))
3380 {
3381 mips_move_integer (dest, dest, INTVAL (src));
3382 return;
3383 }
3384
3385 /* Split moves of symbolic constants into high/low pairs. */
3386 if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3387 {
3388 emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3389 return;
3390 }
3391
3392 /* Generate the appropriate access sequences for TLS symbols. */
3393 if (mips_tls_symbol_p (src))
3394 {
3395 mips_emit_move (dest, mips_legitimize_tls_address (src));
3396 return;
3397 }
3398
3399 /* If we have (const (plus symbol offset)), and that expression cannot
3400 be forced into memory, load the symbol first and add in the offset.
3401 In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3402 forced into memory, as it usually produces better code. */
3403 split_const (src, &base, &offset);
3404 if (offset != const0_rtx
3405 && (targetm.cannot_force_const_mem (mode, src)
3406 || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3407 {
3408 base = mips_force_temporary (dest, base);
3409 mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3410 return;
3411 }
3412
3413 src = force_const_mem (mode, src);
3414
3415 /* When using explicit relocs, constant pool references are sometimes
3416 not legitimate addresses. */
3417 mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3418 mips_emit_move (dest, src);
3419 }
3420
3421 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3422 sequence that is valid. */
3423
3424 bool
3425 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
3426 {
3427 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3428 {
3429 mips_emit_move (dest, force_reg (mode, src));
3430 return true;
3431 }
3432
3433 /* We need to deal with constants that would be legitimate
3434 immediate_operands but aren't legitimate move_operands. */
3435 if (CONSTANT_P (src) && !move_operand (src, mode))
3436 {
3437 mips_legitimize_const_move (mode, dest, src);
3438 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3439 return true;
3440 }
3441 return false;
3442 }
3443 \f
3444 /* Return true if value X in context CONTEXT is a small-data address
3445 that can be rewritten as a LO_SUM. */
3446
3447 static bool
3448 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3449 {
3450 enum mips_symbol_type symbol_type;
3451
3452 return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3453 && !mips_split_p[SYMBOL_GP_RELATIVE]
3454 && mips_symbolic_constant_p (x, context, &symbol_type)
3455 && symbol_type == SYMBOL_GP_RELATIVE);
3456 }
3457
3458 /* A for_each_rtx callback for mips_small_data_pattern_p. DATA is the
3459 containing MEM, or null if none. */
3460
3461 static int
3462 mips_small_data_pattern_1 (rtx *loc, void *data)
3463 {
3464 enum mips_symbol_context context;
3465
3466 /* Ignore things like "g" constraints in asms. We make no particular
3467 guarantee about which symbolic constants are acceptable as asm operands
3468 versus which must be forced into a GPR. */
3469 if (GET_CODE (*loc) == LO_SUM || GET_CODE (*loc) == ASM_OPERANDS)
3470 return -1;
3471
3472 if (MEM_P (*loc))
3473 {
3474 if (for_each_rtx (&XEXP (*loc, 0), mips_small_data_pattern_1, *loc))
3475 return 1;
3476 return -1;
3477 }
3478
3479 context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3480 return mips_rewrite_small_data_p (*loc, context);
3481 }
3482
3483 /* Return true if OP refers to small data symbols directly, not through
3484 a LO_SUM. */
3485
3486 bool
3487 mips_small_data_pattern_p (rtx op)
3488 {
3489 return for_each_rtx (&op, mips_small_data_pattern_1, NULL);
3490 }
3491
3492 /* A for_each_rtx callback, used by mips_rewrite_small_data.
3493 DATA is the containing MEM, or null if none. */
3494
3495 static int
3496 mips_rewrite_small_data_1 (rtx *loc, void *data)
3497 {
3498 enum mips_symbol_context context;
3499
3500 if (MEM_P (*loc))
3501 {
3502 for_each_rtx (&XEXP (*loc, 0), mips_rewrite_small_data_1, *loc);
3503 return -1;
3504 }
3505
3506 context = data ? SYMBOL_CONTEXT_MEM : SYMBOL_CONTEXT_LEA;
3507 if (mips_rewrite_small_data_p (*loc, context))
3508 *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3509
3510 if (GET_CODE (*loc) == LO_SUM)
3511 return -1;
3512
3513 return 0;
3514 }
3515
3516 /* Rewrite instruction pattern PATTERN so that it refers to small data
3517 using explicit relocations. */
3518
3519 rtx
3520 mips_rewrite_small_data (rtx pattern)
3521 {
3522 pattern = copy_insn (pattern);
3523 for_each_rtx (&pattern, mips_rewrite_small_data_1, NULL);
3524 return pattern;
3525 }
3526 \f
3527 /* The cost of loading values from the constant pool. It should be
3528 larger than the cost of any constant we want to synthesize inline. */
3529 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3530
3531 /* Return the cost of X when used as an operand to the MIPS16 instruction
3532 that implements CODE. Return -1 if there is no such instruction, or if
3533 X is not a valid immediate operand for it. */
3534
3535 static int
3536 mips16_constant_cost (int code, HOST_WIDE_INT x)
3537 {
3538 switch (code)
3539 {
3540 case ASHIFT:
3541 case ASHIFTRT:
3542 case LSHIFTRT:
3543 /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3544 other shifts are extended. The shift patterns truncate the shift
3545 count to the right size, so there are no out-of-range values. */
3546 if (IN_RANGE (x, 1, 8))
3547 return 0;
3548 return COSTS_N_INSNS (1);
3549
3550 case PLUS:
3551 if (IN_RANGE (x, -128, 127))
3552 return 0;
3553 if (SMALL_OPERAND (x))
3554 return COSTS_N_INSNS (1);
3555 return -1;
3556
3557 case LEU:
3558 /* Like LE, but reject the always-true case. */
3559 if (x == -1)
3560 return -1;
3561 case LE:
3562 /* We add 1 to the immediate and use SLT. */
3563 x += 1;
3564 case XOR:
3565 /* We can use CMPI for an xor with an unsigned 16-bit X. */
3566 case LT:
3567 case LTU:
3568 if (IN_RANGE (x, 0, 255))
3569 return 0;
3570 if (SMALL_OPERAND_UNSIGNED (x))
3571 return COSTS_N_INSNS (1);
3572 return -1;
3573
3574 case EQ:
3575 case NE:
3576 /* Equality comparisons with 0 are cheap. */
3577 if (x == 0)
3578 return 0;
3579 return -1;
3580
3581 default:
3582 return -1;
3583 }
3584 }
3585
3586 /* Return true if there is a non-MIPS16 instruction that implements CODE
3587 and if that instruction accepts X as an immediate operand. */
3588
3589 static int
3590 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3591 {
3592 switch (code)
3593 {
3594 case ASHIFT:
3595 case ASHIFTRT:
3596 case LSHIFTRT:
3597 /* All shift counts are truncated to a valid constant. */
3598 return true;
3599
3600 case ROTATE:
3601 case ROTATERT:
3602 /* Likewise rotates, if the target supports rotates at all. */
3603 return ISA_HAS_ROR;
3604
3605 case AND:
3606 case IOR:
3607 case XOR:
3608 /* These instructions take 16-bit unsigned immediates. */
3609 return SMALL_OPERAND_UNSIGNED (x);
3610
3611 case PLUS:
3612 case LT:
3613 case LTU:
3614 /* These instructions take 16-bit signed immediates. */
3615 return SMALL_OPERAND (x);
3616
3617 case EQ:
3618 case NE:
3619 case GT:
3620 case GTU:
3621 /* The "immediate" forms of these instructions are really
3622 implemented as comparisons with register 0. */
3623 return x == 0;
3624
3625 case GE:
3626 case GEU:
3627 /* Likewise, meaning that the only valid immediate operand is 1. */
3628 return x == 1;
3629
3630 case LE:
3631 /* We add 1 to the immediate and use SLT. */
3632 return SMALL_OPERAND (x + 1);
3633
3634 case LEU:
3635 /* Likewise SLTU, but reject the always-true case. */
3636 return SMALL_OPERAND (x + 1) && x + 1 != 0;
3637
3638 case SIGN_EXTRACT:
3639 case ZERO_EXTRACT:
3640 /* The bit position and size are immediate operands. */
3641 return ISA_HAS_EXT_INS;
3642
3643 default:
3644 /* By default assume that $0 can be used for 0. */
3645 return x == 0;
3646 }
3647 }
3648
3649 /* Return the cost of binary operation X, given that the instruction
3650 sequence for a word-sized or smaller operation has cost SINGLE_COST
3651 and that the sequence of a double-word operation has cost DOUBLE_COST.
3652 If SPEED is true, optimize for speed otherwise optimize for size. */
3653
3654 static int
3655 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3656 {
3657 int cost;
3658
3659 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3660 cost = double_cost;
3661 else
3662 cost = single_cost;
3663 return (cost
3664 + set_src_cost (XEXP (x, 0), speed)
3665 + rtx_cost (XEXP (x, 1), GET_CODE (x), 1, speed));
3666 }
3667
3668 /* Return the cost of floating-point multiplications of mode MODE. */
3669
3670 static int
3671 mips_fp_mult_cost (enum machine_mode mode)
3672 {
3673 return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3674 }
3675
3676 /* Return the cost of floating-point divisions of mode MODE. */
3677
3678 static int
3679 mips_fp_div_cost (enum machine_mode mode)
3680 {
3681 return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3682 }
3683
3684 /* Return the cost of sign-extending OP to mode MODE, not including the
3685 cost of OP itself. */
3686
3687 static int
3688 mips_sign_extend_cost (enum machine_mode mode, rtx op)
3689 {
3690 if (MEM_P (op))
3691 /* Extended loads are as cheap as unextended ones. */
3692 return 0;
3693
3694 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3695 /* A sign extension from SImode to DImode in 64-bit mode is free. */
3696 return 0;
3697
3698 if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3699 /* We can use SEB or SEH. */
3700 return COSTS_N_INSNS (1);
3701
3702 /* We need to use a shift left and a shift right. */
3703 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3704 }
3705
3706 /* Return the cost of zero-extending OP to mode MODE, not including the
3707 cost of OP itself. */
3708
3709 static int
3710 mips_zero_extend_cost (enum machine_mode mode, rtx op)
3711 {
3712 if (MEM_P (op))
3713 /* Extended loads are as cheap as unextended ones. */
3714 return 0;
3715
3716 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3717 /* We need a shift left by 32 bits and a shift right by 32 bits. */
3718 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3719
3720 if (GENERATE_MIPS16E)
3721 /* We can use ZEB or ZEH. */
3722 return COSTS_N_INSNS (1);
3723
3724 if (TARGET_MIPS16)
3725 /* We need to load 0xff or 0xffff into a register and use AND. */
3726 return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
3727
3728 /* We can use ANDI. */
3729 return COSTS_N_INSNS (1);
3730 }
3731
3732 /* Return the cost of moving between two registers of mode MODE,
3733 assuming that the move will be in pieces of at most UNITS bytes. */
3734
3735 static int
3736 mips_set_reg_reg_piece_cost (enum machine_mode mode, unsigned int units)
3737 {
3738 return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
3739 }
3740
3741 /* Return the cost of moving between two registers of mode MODE. */
3742
3743 static int
3744 mips_set_reg_reg_cost (enum machine_mode mode)
3745 {
3746 switch (GET_MODE_CLASS (mode))
3747 {
3748 case MODE_CC:
3749 return mips_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
3750
3751 case MODE_FLOAT:
3752 case MODE_COMPLEX_FLOAT:
3753 case MODE_VECTOR_FLOAT:
3754 if (TARGET_HARD_FLOAT)
3755 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
3756 /* Fall through */
3757
3758 default:
3759 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
3760 }
3761 }
3762
3763 /* Implement TARGET_RTX_COSTS. */
3764
3765 static bool
3766 mips_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3767 int *total, bool speed)
3768 {
3769 enum machine_mode mode = GET_MODE (x);
3770 bool float_mode_p = FLOAT_MODE_P (mode);
3771 int cost;
3772 rtx addr;
3773
3774 /* The cost of a COMPARE is hard to define for MIPS. COMPAREs don't
3775 appear in the instruction stream, and the cost of a comparison is
3776 really the cost of the branch or scc condition. At the time of
3777 writing, GCC only uses an explicit outer COMPARE code when optabs
3778 is testing whether a constant is expensive enough to force into a
3779 register. We want optabs to pass such constants through the MIPS
3780 expanders instead, so make all constants very cheap here. */
3781 if (outer_code == COMPARE)
3782 {
3783 gcc_assert (CONSTANT_P (x));
3784 *total = 0;
3785 return true;
3786 }
3787
3788 switch (code)
3789 {
3790 case CONST_INT:
3791 /* Treat *clear_upper32-style ANDs as having zero cost in the
3792 second operand. The cost is entirely in the first operand.
3793
3794 ??? This is needed because we would otherwise try to CSE
3795 the constant operand. Although that's the right thing for
3796 instructions that continue to be a register operation throughout
3797 compilation, it is disastrous for instructions that could
3798 later be converted into a memory operation. */
3799 if (TARGET_64BIT
3800 && outer_code == AND
3801 && UINTVAL (x) == 0xffffffff)
3802 {
3803 *total = 0;
3804 return true;
3805 }
3806
3807 if (TARGET_MIPS16)
3808 {
3809 cost = mips16_constant_cost (outer_code, INTVAL (x));
3810 if (cost >= 0)
3811 {
3812 *total = cost;
3813 return true;
3814 }
3815 }
3816 else
3817 {
3818 /* When not optimizing for size, we care more about the cost
3819 of hot code, and hot code is often in a loop. If a constant
3820 operand needs to be forced into a register, we will often be
3821 able to hoist the constant load out of the loop, so the load
3822 should not contribute to the cost. */
3823 if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
3824 {
3825 *total = 0;
3826 return true;
3827 }
3828 }
3829 /* Fall through. */
3830
3831 case CONST:
3832 case SYMBOL_REF:
3833 case LABEL_REF:
3834 case CONST_DOUBLE:
3835 if (force_to_mem_operand (x, VOIDmode))
3836 {
3837 *total = COSTS_N_INSNS (1);
3838 return true;
3839 }
3840 cost = mips_const_insns (x);
3841 if (cost > 0)
3842 {
3843 /* If the constant is likely to be stored in a GPR, SETs of
3844 single-insn constants are as cheap as register sets; we
3845 never want to CSE them.
3846
3847 Don't reduce the cost of storing a floating-point zero in
3848 FPRs. If we have a zero in an FPR for other reasons, we
3849 can get better cfg-cleanup and delayed-branch results by
3850 using it consistently, rather than using $0 sometimes and
3851 an FPR at other times. Also, moves between floating-point
3852 registers are sometimes cheaper than (D)MTC1 $0. */
3853 if (cost == 1
3854 && outer_code == SET
3855 && !(float_mode_p && TARGET_HARD_FLOAT))
3856 cost = 0;
3857 /* When non-MIPS16 code loads a constant N>1 times, we rarely
3858 want to CSE the constant itself. It is usually better to
3859 have N copies of the last operation in the sequence and one
3860 shared copy of the other operations. (Note that this is
3861 not true for MIPS16 code, where the final operation in the
3862 sequence is often an extended instruction.)
3863
3864 Also, if we have a CONST_INT, we don't know whether it is
3865 for a word or doubleword operation, so we cannot rely on
3866 the result of mips_build_integer. */
3867 else if (!TARGET_MIPS16
3868 && (outer_code == SET || mode == VOIDmode))
3869 cost = 1;
3870 *total = COSTS_N_INSNS (cost);
3871 return true;
3872 }
3873 /* The value will need to be fetched from the constant pool. */
3874 *total = CONSTANT_POOL_COST;
3875 return true;
3876
3877 case MEM:
3878 /* If the address is legitimate, return the number of
3879 instructions it needs. */
3880 addr = XEXP (x, 0);
3881 cost = mips_address_insns (addr, mode, true);
3882 if (cost > 0)
3883 {
3884 *total = COSTS_N_INSNS (cost + 1);
3885 return true;
3886 }
3887 /* Check for a scaled indexed address. */
3888 if (mips_lwxs_address_p (addr)
3889 || mips_lx_address_p (addr, mode))
3890 {
3891 *total = COSTS_N_INSNS (2);
3892 return true;
3893 }
3894 /* Otherwise use the default handling. */
3895 return false;
3896
3897 case FFS:
3898 *total = COSTS_N_INSNS (6);
3899 return false;
3900
3901 case NOT:
3902 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3903 return false;
3904
3905 case AND:
3906 /* Check for a *clear_upper32 pattern and treat it like a zero
3907 extension. See the pattern's comment for details. */
3908 if (TARGET_64BIT
3909 && mode == DImode
3910 && CONST_INT_P (XEXP (x, 1))
3911 && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3912 {
3913 *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
3914 + set_src_cost (XEXP (x, 0), speed));
3915 return true;
3916 }
3917 if (ISA_HAS_CINS && CONST_INT_P (XEXP (x, 1)))
3918 {
3919 rtx op = XEXP (x, 0);
3920 if (GET_CODE (op) == ASHIFT
3921 && CONST_INT_P (XEXP (op, 1))
3922 && mask_low_and_shift_p (mode, XEXP (x, 1), XEXP (op, 1), 32))
3923 {
3924 *total = COSTS_N_INSNS (1) + set_src_cost (XEXP (op, 0), speed);
3925 return true;
3926 }
3927 }
3928 /* (AND (NOT op0) (NOT op1) is a nor operation that can be done in
3929 a single instruction. */
3930 if (!TARGET_MIPS16
3931 && GET_CODE (XEXP (x, 0)) == NOT
3932 && GET_CODE (XEXP (x, 1)) == NOT)
3933 {
3934 cost = GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1;
3935 *total = (COSTS_N_INSNS (cost)
3936 + set_src_cost (XEXP (XEXP (x, 0), 0), speed)
3937 + set_src_cost (XEXP (XEXP (x, 1), 0), speed));
3938 return true;
3939 }
3940
3941 /* Fall through. */
3942
3943 case IOR:
3944 case XOR:
3945 /* Double-word operations use two single-word operations. */
3946 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
3947 speed);
3948 return true;
3949
3950 case ASHIFT:
3951 case ASHIFTRT:
3952 case LSHIFTRT:
3953 case ROTATE:
3954 case ROTATERT:
3955 if (CONSTANT_P (XEXP (x, 1)))
3956 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3957 speed);
3958 else
3959 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
3960 speed);
3961 return true;
3962
3963 case ABS:
3964 if (float_mode_p)
3965 *total = mips_cost->fp_add;
3966 else
3967 *total = COSTS_N_INSNS (4);
3968 return false;
3969
3970 case LO_SUM:
3971 /* Low-part immediates need an extended MIPS16 instruction. */
3972 *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
3973 + set_src_cost (XEXP (x, 0), speed));
3974 return true;
3975
3976 case LT:
3977 case LTU:
3978 case LE:
3979 case LEU:
3980 case GT:
3981 case GTU:
3982 case GE:
3983 case GEU:
3984 case EQ:
3985 case NE:
3986 case UNORDERED:
3987 case LTGT:
3988 /* Branch comparisons have VOIDmode, so use the first operand's
3989 mode instead. */
3990 mode = GET_MODE (XEXP (x, 0));
3991 if (FLOAT_MODE_P (mode))
3992 {
3993 *total = mips_cost->fp_add;
3994 return false;
3995 }
3996 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3997 speed);
3998 return true;
3999
4000 case MINUS:
4001 if (float_mode_p
4002 && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
4003 && TARGET_FUSED_MADD
4004 && !HONOR_NANS (mode)
4005 && !HONOR_SIGNED_ZEROS (mode))
4006 {
4007 /* See if we can use NMADD or NMSUB. See mips.md for the
4008 associated patterns. */
4009 rtx op0 = XEXP (x, 0);
4010 rtx op1 = XEXP (x, 1);
4011 if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
4012 {
4013 *total = (mips_fp_mult_cost (mode)
4014 + set_src_cost (XEXP (XEXP (op0, 0), 0), speed)
4015 + set_src_cost (XEXP (op0, 1), speed)
4016 + set_src_cost (op1, speed));
4017 return true;
4018 }
4019 if (GET_CODE (op1) == MULT)
4020 {
4021 *total = (mips_fp_mult_cost (mode)
4022 + set_src_cost (op0, speed)
4023 + set_src_cost (XEXP (op1, 0), speed)
4024 + set_src_cost (XEXP (op1, 1), speed));
4025 return true;
4026 }
4027 }
4028 /* Fall through. */
4029
4030 case PLUS:
4031 if (float_mode_p)
4032 {
4033 /* If this is part of a MADD or MSUB, treat the PLUS as
4034 being free. */
4035 if ((ISA_HAS_FP_MADD4_MSUB4 || ISA_HAS_FP_MADD3_MSUB3)
4036 && TARGET_FUSED_MADD
4037 && GET_CODE (XEXP (x, 0)) == MULT)
4038 *total = 0;
4039 else
4040 *total = mips_cost->fp_add;
4041 return false;
4042 }
4043
4044 /* Double-word operations require three single-word operations and
4045 an SLTU. The MIPS16 version then needs to move the result of
4046 the SLTU from $24 to a MIPS16 register. */
4047 *total = mips_binary_cost (x, COSTS_N_INSNS (1),
4048 COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
4049 speed);
4050 return true;
4051
4052 case NEG:
4053 if (float_mode_p
4054 && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
4055 && TARGET_FUSED_MADD
4056 && !HONOR_NANS (mode)
4057 && HONOR_SIGNED_ZEROS (mode))
4058 {
4059 /* See if we can use NMADD or NMSUB. See mips.md for the
4060 associated patterns. */
4061 rtx op = XEXP (x, 0);
4062 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
4063 && GET_CODE (XEXP (op, 0)) == MULT)
4064 {
4065 *total = (mips_fp_mult_cost (mode)
4066 + set_src_cost (XEXP (XEXP (op, 0), 0), speed)
4067 + set_src_cost (XEXP (XEXP (op, 0), 1), speed)
4068 + set_src_cost (XEXP (op, 1), speed));
4069 return true;
4070 }
4071 }
4072
4073 if (float_mode_p)
4074 *total = mips_cost->fp_add;
4075 else
4076 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
4077 return false;
4078
4079 case MULT:
4080 if (float_mode_p)
4081 *total = mips_fp_mult_cost (mode);
4082 else if (mode == DImode && !TARGET_64BIT)
4083 /* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
4084 where the mulsidi3 always includes an MFHI and an MFLO. */
4085 *total = (speed
4086 ? mips_cost->int_mult_si * 3 + 6
4087 : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
4088 else if (!speed)
4089 *total = COSTS_N_INSNS (ISA_HAS_MUL3 ? 1 : 2) + 1;
4090 else if (mode == DImode)
4091 *total = mips_cost->int_mult_di;
4092 else
4093 *total = mips_cost->int_mult_si;
4094 return false;
4095
4096 case DIV:
4097 /* Check for a reciprocal. */
4098 if (float_mode_p
4099 && ISA_HAS_FP_RECIP_RSQRT (mode)
4100 && flag_unsafe_math_optimizations
4101 && XEXP (x, 0) == CONST1_RTX (mode))
4102 {
4103 if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
4104 /* An rsqrt<mode>a or rsqrt<mode>b pattern. Count the
4105 division as being free. */
4106 *total = set_src_cost (XEXP (x, 1), speed);
4107 else
4108 *total = (mips_fp_div_cost (mode)
4109 + set_src_cost (XEXP (x, 1), speed));
4110 return true;
4111 }
4112 /* Fall through. */
4113
4114 case SQRT:
4115 case MOD:
4116 if (float_mode_p)
4117 {
4118 *total = mips_fp_div_cost (mode);
4119 return false;
4120 }
4121 /* Fall through. */
4122
4123 case UDIV:
4124 case UMOD:
4125 if (!speed)
4126 {
4127 /* It is our responsibility to make division by a power of 2
4128 as cheap as 2 register additions if we want the division
4129 expanders to be used for such operations; see the setting
4130 of sdiv_pow2_cheap in optabs.c. Using (D)DIV for MIPS16
4131 should always produce shorter code than using
4132 expand_sdiv2_pow2. */
4133 if (TARGET_MIPS16
4134 && CONST_INT_P (XEXP (x, 1))
4135 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
4136 {
4137 *total = COSTS_N_INSNS (2) + set_src_cost (XEXP (x, 0), speed);
4138 return true;
4139 }
4140 *total = COSTS_N_INSNS (mips_idiv_insns ());
4141 }
4142 else if (mode == DImode)
4143 *total = mips_cost->int_div_di;
4144 else
4145 *total = mips_cost->int_div_si;
4146 return false;
4147
4148 case SIGN_EXTEND:
4149 *total = mips_sign_extend_cost (mode, XEXP (x, 0));
4150 return false;
4151
4152 case ZERO_EXTEND:
4153 if (outer_code == SET
4154 && ISA_HAS_BADDU
4155 && (GET_CODE (XEXP (x, 0)) == TRUNCATE
4156 || GET_CODE (XEXP (x, 0)) == SUBREG)
4157 && GET_MODE (XEXP (x, 0)) == QImode
4158 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
4159 {
4160 *total = set_src_cost (XEXP (XEXP (x, 0), 0), speed);
4161 return true;
4162 }
4163 *total = mips_zero_extend_cost (mode, XEXP (x, 0));
4164 return false;
4165
4166 case FLOAT:
4167 case UNSIGNED_FLOAT:
4168 case FIX:
4169 case FLOAT_EXTEND:
4170 case FLOAT_TRUNCATE:
4171 *total = mips_cost->fp_add;
4172 return false;
4173
4174 case SET:
4175 if (register_operand (SET_DEST (x), VOIDmode)
4176 && reg_or_0_operand (SET_SRC (x), VOIDmode))
4177 {
4178 *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
4179 return true;
4180 }
4181 return false;
4182
4183 default:
4184 return false;
4185 }
4186 }
4187
4188 /* Implement TARGET_ADDRESS_COST. */
4189
4190 static int
4191 mips_address_cost (rtx addr, enum machine_mode mode,
4192 addr_space_t as ATTRIBUTE_UNUSED,
4193 bool speed ATTRIBUTE_UNUSED)
4194 {
4195 return mips_address_insns (addr, mode, false);
4196 }
4197 \f
4198 /* Information about a single instruction in a multi-instruction
4199 asm sequence. */
4200 struct mips_multi_member {
4201 /* True if this is a label, false if it is code. */
4202 bool is_label_p;
4203
4204 /* The output_asm_insn format of the instruction. */
4205 const char *format;
4206
4207 /* The operands to the instruction. */
4208 rtx operands[MAX_RECOG_OPERANDS];
4209 };
4210 typedef struct mips_multi_member mips_multi_member;
4211
4212 /* The instructions that make up the current multi-insn sequence. */
4213 static vec<mips_multi_member> mips_multi_members;
4214
4215 /* How many instructions (as opposed to labels) are in the current
4216 multi-insn sequence. */
4217 static unsigned int mips_multi_num_insns;
4218
4219 /* Start a new multi-insn sequence. */
4220
4221 static void
4222 mips_multi_start (void)
4223 {
4224 mips_multi_members.truncate (0);
4225 mips_multi_num_insns = 0;
4226 }
4227
4228 /* Add a new, uninitialized member to the current multi-insn sequence. */
4229
4230 static struct mips_multi_member *
4231 mips_multi_add (void)
4232 {
4233 mips_multi_member empty;
4234 return mips_multi_members.safe_push (empty);
4235 }
4236
4237 /* Add a normal insn with the given asm format to the current multi-insn
4238 sequence. The other arguments are a null-terminated list of operands. */
4239
4240 static void
4241 mips_multi_add_insn (const char *format, ...)
4242 {
4243 struct mips_multi_member *member;
4244 va_list ap;
4245 unsigned int i;
4246 rtx op;
4247
4248 member = mips_multi_add ();
4249 member->is_label_p = false;
4250 member->format = format;
4251 va_start (ap, format);
4252 i = 0;
4253 while ((op = va_arg (ap, rtx)))
4254 member->operands[i++] = op;
4255 va_end (ap);
4256 mips_multi_num_insns++;
4257 }
4258
4259 /* Add the given label definition to the current multi-insn sequence.
4260 The definition should include the colon. */
4261
4262 static void
4263 mips_multi_add_label (const char *label)
4264 {
4265 struct mips_multi_member *member;
4266
4267 member = mips_multi_add ();
4268 member->is_label_p = true;
4269 member->format = label;
4270 }
4271
4272 /* Return the index of the last member of the current multi-insn sequence. */
4273
4274 static unsigned int
4275 mips_multi_last_index (void)
4276 {
4277 return mips_multi_members.length () - 1;
4278 }
4279
4280 /* Add a copy of an existing instruction to the current multi-insn
4281 sequence. I is the index of the instruction that should be copied. */
4282
4283 static void
4284 mips_multi_copy_insn (unsigned int i)
4285 {
4286 struct mips_multi_member *member;
4287
4288 member = mips_multi_add ();
4289 memcpy (member, &mips_multi_members[i], sizeof (*member));
4290 gcc_assert (!member->is_label_p);
4291 }
4292
4293 /* Change the operand of an existing instruction in the current
4294 multi-insn sequence. I is the index of the instruction,
4295 OP is the index of the operand, and X is the new value. */
4296
4297 static void
4298 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
4299 {
4300 mips_multi_members[i].operands[op] = x;
4301 }
4302
4303 /* Write out the asm code for the current multi-insn sequence. */
4304
4305 static void
4306 mips_multi_write (void)
4307 {
4308 struct mips_multi_member *member;
4309 unsigned int i;
4310
4311 FOR_EACH_VEC_ELT (mips_multi_members, i, member)
4312 if (member->is_label_p)
4313 fprintf (asm_out_file, "%s\n", member->format);
4314 else
4315 output_asm_insn (member->format, member->operands);
4316 }
4317 \f
4318 /* Return one word of double-word value OP, taking into account the fixed
4319 endianness of certain registers. HIGH_P is true to select the high part,
4320 false to select the low part. */
4321
4322 rtx
4323 mips_subword (rtx op, bool high_p)
4324 {
4325 unsigned int byte, offset;
4326 enum machine_mode mode;
4327
4328 mode = GET_MODE (op);
4329 if (mode == VOIDmode)
4330 mode = TARGET_64BIT ? TImode : DImode;
4331
4332 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4333 byte = UNITS_PER_WORD;
4334 else
4335 byte = 0;
4336
4337 if (FP_REG_RTX_P (op))
4338 {
4339 /* Paired FPRs are always ordered little-endian. */
4340 offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4341 return gen_rtx_REG (word_mode, REGNO (op) + offset);
4342 }
4343
4344 if (MEM_P (op))
4345 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4346
4347 return simplify_gen_subreg (word_mode, op, mode, byte);
4348 }
4349
4350 /* Return true if SRC should be moved into DEST using "MULT $0, $0".
4351 SPLIT_TYPE is the condition under which moves should be split. */
4352
4353 static bool
4354 mips_mult_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4355 {
4356 return ((split_type != SPLIT_FOR_SPEED
4357 || mips_tuning_info.fast_mult_zero_zero_p)
4358 && src == const0_rtx
4359 && REG_P (dest)
4360 && GET_MODE_SIZE (GET_MODE (dest)) == 2 * UNITS_PER_WORD
4361 && (ISA_HAS_DSP_MULT
4362 ? ACC_REG_P (REGNO (dest))
4363 : MD_REG_P (REGNO (dest))));
4364 }
4365
4366 /* Return true if a move from SRC to DEST should be split into two.
4367 SPLIT_TYPE describes the split condition. */
4368
4369 bool
4370 mips_split_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4371 {
4372 /* Check whether the move can be done using some variant of MULT $0,$0. */
4373 if (mips_mult_move_p (dest, src, split_type))
4374 return false;
4375
4376 /* FPR-to-FPR moves can be done in a single instruction, if they're
4377 allowed at all. */
4378 unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
4379 if (size == 8 && FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4380 return false;
4381
4382 /* Check for floating-point loads and stores. */
4383 if (size == 8 && ISA_HAS_LDC1_SDC1)
4384 {
4385 if (FP_REG_RTX_P (dest) && MEM_P (src))
4386 return false;
4387 if (FP_REG_RTX_P (src) && MEM_P (dest))
4388 return false;
4389 }
4390
4391 /* Otherwise split all multiword moves. */
4392 return size > UNITS_PER_WORD;
4393 }
4394
4395 /* Split a move from SRC to DEST, given that mips_split_move_p holds.
4396 SPLIT_TYPE describes the split condition. */
4397
4398 void
4399 mips_split_move (rtx dest, rtx src, enum mips_split_type split_type)
4400 {
4401 rtx low_dest;
4402
4403 gcc_checking_assert (mips_split_move_p (dest, src, split_type));
4404 if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
4405 {
4406 if (!TARGET_64BIT && GET_MODE (dest) == DImode)
4407 emit_insn (gen_move_doubleword_fprdi (dest, src));
4408 else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
4409 emit_insn (gen_move_doubleword_fprdf (dest, src));
4410 else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
4411 emit_insn (gen_move_doubleword_fprv2sf (dest, src));
4412 else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
4413 emit_insn (gen_move_doubleword_fprv2si (dest, src));
4414 else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
4415 emit_insn (gen_move_doubleword_fprv4hi (dest, src));
4416 else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
4417 emit_insn (gen_move_doubleword_fprv8qi (dest, src));
4418 else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
4419 emit_insn (gen_move_doubleword_fprtf (dest, src));
4420 else
4421 gcc_unreachable ();
4422 }
4423 else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
4424 {
4425 low_dest = mips_subword (dest, false);
4426 mips_emit_move (low_dest, mips_subword (src, false));
4427 if (TARGET_64BIT)
4428 emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
4429 else
4430 emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
4431 }
4432 else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
4433 {
4434 mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
4435 if (TARGET_64BIT)
4436 emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
4437 else
4438 emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
4439 }
4440 else
4441 {
4442 /* The operation can be split into two normal moves. Decide in
4443 which order to do them. */
4444 low_dest = mips_subword (dest, false);
4445 if (REG_P (low_dest)
4446 && reg_overlap_mentioned_p (low_dest, src))
4447 {
4448 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4449 mips_emit_move (low_dest, mips_subword (src, false));
4450 }
4451 else
4452 {
4453 mips_emit_move (low_dest, mips_subword (src, false));
4454 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4455 }
4456 }
4457 }
4458
4459 /* Return the split type for instruction INSN. */
4460
4461 static enum mips_split_type
4462 mips_insn_split_type (rtx insn)
4463 {
4464 basic_block bb = BLOCK_FOR_INSN (insn);
4465 if (bb)
4466 {
4467 if (optimize_bb_for_speed_p (bb))
4468 return SPLIT_FOR_SPEED;
4469 else
4470 return SPLIT_FOR_SIZE;
4471 }
4472 /* Once CFG information has been removed, we should trust the optimization
4473 decisions made by previous passes and only split where necessary. */
4474 return SPLIT_IF_NECESSARY;
4475 }
4476
4477 /* Return true if a move from SRC to DEST in INSN should be split. */
4478
4479 bool
4480 mips_split_move_insn_p (rtx dest, rtx src, rtx insn)
4481 {
4482 return mips_split_move_p (dest, src, mips_insn_split_type (insn));
4483 }
4484
4485 /* Split a move from SRC to DEST in INSN, given that mips_split_move_insn_p
4486 holds. */
4487
4488 void
4489 mips_split_move_insn (rtx dest, rtx src, rtx insn)
4490 {
4491 mips_split_move (dest, src, mips_insn_split_type (insn));
4492 }
4493 \f
4494 /* Return the appropriate instructions to move SRC into DEST. Assume
4495 that SRC is operand 1 and DEST is operand 0. */
4496
4497 const char *
4498 mips_output_move (rtx dest, rtx src)
4499 {
4500 enum rtx_code dest_code, src_code;
4501 enum machine_mode mode;
4502 enum mips_symbol_type symbol_type;
4503 bool dbl_p;
4504
4505 dest_code = GET_CODE (dest);
4506 src_code = GET_CODE (src);
4507 mode = GET_MODE (dest);
4508 dbl_p = (GET_MODE_SIZE (mode) == 8);
4509
4510 if (mips_split_move_p (dest, src, SPLIT_IF_NECESSARY))
4511 return "#";
4512
4513 if ((src_code == REG && GP_REG_P (REGNO (src)))
4514 || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
4515 {
4516 if (dest_code == REG)
4517 {
4518 if (GP_REG_P (REGNO (dest)))
4519 return "move\t%0,%z1";
4520
4521 if (mips_mult_move_p (dest, src, SPLIT_IF_NECESSARY))
4522 {
4523 if (ISA_HAS_DSP_MULT)
4524 return "mult\t%q0,%.,%.";
4525 else
4526 return "mult\t%.,%.";
4527 }
4528
4529 /* Moves to HI are handled by special .md insns. */
4530 if (REGNO (dest) == LO_REGNUM)
4531 return "mtlo\t%z1";
4532
4533 if (DSP_ACC_REG_P (REGNO (dest)))
4534 {
4535 static char retval[] = "mt__\t%z1,%q0";
4536
4537 retval[2] = reg_names[REGNO (dest)][4];
4538 retval[3] = reg_names[REGNO (dest)][5];
4539 return retval;
4540 }
4541
4542 if (FP_REG_P (REGNO (dest)))
4543 return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
4544
4545 if (ALL_COP_REG_P (REGNO (dest)))
4546 {
4547 static char retval[] = "dmtc_\t%z1,%0";
4548
4549 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4550 return dbl_p ? retval : retval + 1;
4551 }
4552 }
4553 if (dest_code == MEM)
4554 switch (GET_MODE_SIZE (mode))
4555 {
4556 case 1: return "sb\t%z1,%0";
4557 case 2: return "sh\t%z1,%0";
4558 case 4: return "sw\t%z1,%0";
4559 case 8: return "sd\t%z1,%0";
4560 }
4561 }
4562 if (dest_code == REG && GP_REG_P (REGNO (dest)))
4563 {
4564 if (src_code == REG)
4565 {
4566 /* Moves from HI are handled by special .md insns. */
4567 if (REGNO (src) == LO_REGNUM)
4568 {
4569 /* When generating VR4120 or VR4130 code, we use MACC and
4570 DMACC instead of MFLO. This avoids both the normal
4571 MIPS III HI/LO hazards and the errata related to
4572 -mfix-vr4130. */
4573 if (ISA_HAS_MACCHI)
4574 return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
4575 return "mflo\t%0";
4576 }
4577
4578 if (DSP_ACC_REG_P (REGNO (src)))
4579 {
4580 static char retval[] = "mf__\t%0,%q1";
4581
4582 retval[2] = reg_names[REGNO (src)][4];
4583 retval[3] = reg_names[REGNO (src)][5];
4584 return retval;
4585 }
4586
4587 if (FP_REG_P (REGNO (src)))
4588 return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
4589
4590 if (ALL_COP_REG_P (REGNO (src)))
4591 {
4592 static char retval[] = "dmfc_\t%0,%1";
4593
4594 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4595 return dbl_p ? retval : retval + 1;
4596 }
4597 }
4598
4599 if (src_code == MEM)
4600 switch (GET_MODE_SIZE (mode))
4601 {
4602 case 1: return "lbu\t%0,%1";
4603 case 2: return "lhu\t%0,%1";
4604 case 4: return "lw\t%0,%1";
4605 case 8: return "ld\t%0,%1";
4606 }
4607
4608 if (src_code == CONST_INT)
4609 {
4610 /* Don't use the X format for the operand itself, because that
4611 will give out-of-range numbers for 64-bit hosts and 32-bit
4612 targets. */
4613 if (!TARGET_MIPS16)
4614 return "li\t%0,%1\t\t\t# %X1";
4615
4616 if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
4617 return "li\t%0,%1";
4618
4619 if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
4620 return "#";
4621 }
4622
4623 if (src_code == HIGH)
4624 return TARGET_MIPS16 ? "#" : "lui\t%0,%h1";
4625
4626 if (CONST_GP_P (src))
4627 return "move\t%0,%1";
4628
4629 if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
4630 && mips_lo_relocs[symbol_type] != 0)
4631 {
4632 /* A signed 16-bit constant formed by applying a relocation
4633 operator to a symbolic address. */
4634 gcc_assert (!mips_split_p[symbol_type]);
4635 return "li\t%0,%R1";
4636 }
4637
4638 if (symbolic_operand (src, VOIDmode))
4639 {
4640 gcc_assert (TARGET_MIPS16
4641 ? TARGET_MIPS16_TEXT_LOADS
4642 : !TARGET_EXPLICIT_RELOCS);
4643 return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
4644 }
4645 }
4646 if (src_code == REG && FP_REG_P (REGNO (src)))
4647 {
4648 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4649 {
4650 if (GET_MODE (dest) == V2SFmode)
4651 return "mov.ps\t%0,%1";
4652 else
4653 return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
4654 }
4655
4656 if (dest_code == MEM)
4657 return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
4658 }
4659 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4660 {
4661 if (src_code == MEM)
4662 return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
4663 }
4664 if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
4665 {
4666 static char retval[] = "l_c_\t%0,%1";
4667
4668 retval[1] = (dbl_p ? 'd' : 'w');
4669 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4670 return retval;
4671 }
4672 if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
4673 {
4674 static char retval[] = "s_c_\t%1,%0";
4675
4676 retval[1] = (dbl_p ? 'd' : 'w');
4677 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4678 return retval;
4679 }
4680 gcc_unreachable ();
4681 }
4682 \f
4683 /* Return true if CMP1 is a suitable second operand for integer ordering
4684 test CODE. See also the *sCC patterns in mips.md. */
4685
4686 static bool
4687 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
4688 {
4689 switch (code)
4690 {
4691 case GT:
4692 case GTU:
4693 return reg_or_0_operand (cmp1, VOIDmode);
4694
4695 case GE:
4696 case GEU:
4697 return !TARGET_MIPS16 && cmp1 == const1_rtx;
4698
4699 case LT:
4700 case LTU:
4701 return arith_operand (cmp1, VOIDmode);
4702
4703 case LE:
4704 return sle_operand (cmp1, VOIDmode);
4705
4706 case LEU:
4707 return sleu_operand (cmp1, VOIDmode);
4708
4709 default:
4710 gcc_unreachable ();
4711 }
4712 }
4713
4714 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
4715 integer ordering test *CODE, or if an equivalent combination can
4716 be formed by adjusting *CODE and *CMP1. When returning true, update
4717 *CODE and *CMP1 with the chosen code and operand, otherwise leave
4718 them alone. */
4719
4720 static bool
4721 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
4722 enum machine_mode mode)
4723 {
4724 HOST_WIDE_INT plus_one;
4725
4726 if (mips_int_order_operand_ok_p (*code, *cmp1))
4727 return true;
4728
4729 if (CONST_INT_P (*cmp1))
4730 switch (*code)
4731 {
4732 case LE:
4733 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4734 if (INTVAL (*cmp1) < plus_one)
4735 {
4736 *code = LT;
4737 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4738 return true;
4739 }
4740 break;
4741
4742 case LEU:
4743 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4744 if (plus_one != 0)
4745 {
4746 *code = LTU;
4747 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4748 return true;
4749 }
4750 break;
4751
4752 default:
4753 break;
4754 }
4755 return false;
4756 }
4757
4758 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
4759 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
4760 is nonnull, it's OK to set TARGET to the inverse of the result and
4761 flip *INVERT_PTR instead. */
4762
4763 static void
4764 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
4765 rtx target, rtx cmp0, rtx cmp1)
4766 {
4767 enum machine_mode mode;
4768
4769 /* First see if there is a MIPS instruction that can do this operation.
4770 If not, try doing the same for the inverse operation. If that also
4771 fails, force CMP1 into a register and try again. */
4772 mode = GET_MODE (cmp0);
4773 if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
4774 mips_emit_binary (code, target, cmp0, cmp1);
4775 else
4776 {
4777 enum rtx_code inv_code = reverse_condition (code);
4778 if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
4779 {
4780 cmp1 = force_reg (mode, cmp1);
4781 mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
4782 }
4783 else if (invert_ptr == 0)
4784 {
4785 rtx inv_target;
4786
4787 inv_target = mips_force_binary (GET_MODE (target),
4788 inv_code, cmp0, cmp1);
4789 mips_emit_binary (XOR, target, inv_target, const1_rtx);
4790 }
4791 else
4792 {
4793 *invert_ptr = !*invert_ptr;
4794 mips_emit_binary (inv_code, target, cmp0, cmp1);
4795 }
4796 }
4797 }
4798
4799 /* Return a register that is zero iff CMP0 and CMP1 are equal.
4800 The register will have the same mode as CMP0. */
4801
4802 static rtx
4803 mips_zero_if_equal (rtx cmp0, rtx cmp1)
4804 {
4805 if (cmp1 == const0_rtx)
4806 return cmp0;
4807
4808 if (uns_arith_operand (cmp1, VOIDmode))
4809 return expand_binop (GET_MODE (cmp0), xor_optab,
4810 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4811
4812 return expand_binop (GET_MODE (cmp0), sub_optab,
4813 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4814 }
4815
4816 /* Convert *CODE into a code that can be used in a floating-point
4817 scc instruction (C.cond.fmt). Return true if the values of
4818 the condition code registers will be inverted, with 0 indicating
4819 that the condition holds. */
4820
4821 static bool
4822 mips_reversed_fp_cond (enum rtx_code *code)
4823 {
4824 switch (*code)
4825 {
4826 case NE:
4827 case LTGT:
4828 case ORDERED:
4829 *code = reverse_condition_maybe_unordered (*code);
4830 return true;
4831
4832 default:
4833 return false;
4834 }
4835 }
4836
4837 /* Allocate a floating-point condition-code register of mode MODE.
4838
4839 These condition code registers are used for certain kinds
4840 of compound operation, such as compare and branches, vconds,
4841 and built-in functions. At expand time, their use is entirely
4842 controlled by MIPS-specific code and is entirely internal
4843 to these compound operations.
4844
4845 We could (and did in the past) expose condition-code values
4846 as pseudo registers and leave the register allocator to pick
4847 appropriate registers. The problem is that it is not practically
4848 possible for the rtl optimizers to guarantee that no spills will
4849 be needed, even when AVOID_CCMODE_COPIES is defined. We would
4850 therefore need spill and reload sequences to handle the worst case.
4851
4852 Although such sequences do exist, they are very expensive and are
4853 not something we'd want to use. This is especially true of CCV2 and
4854 CCV4, where all the shuffling would greatly outweigh whatever benefit
4855 the vectorization itself provides.
4856
4857 The main benefit of having more than one condition-code register
4858 is to allow the pipelining of operations, especially those involving
4859 comparisons and conditional moves. We don't really expect the
4860 registers to be live for long periods, and certainly never want
4861 them to be live across calls.
4862
4863 Also, there should be no penalty attached to using all the available
4864 registers. They are simply bits in the same underlying FPU control
4865 register.
4866
4867 We therefore expose the hardware registers from the outset and use
4868 a simple round-robin allocation scheme. */
4869
4870 static rtx
4871 mips_allocate_fcc (enum machine_mode mode)
4872 {
4873 unsigned int regno, count;
4874
4875 gcc_assert (TARGET_HARD_FLOAT && ISA_HAS_8CC);
4876
4877 if (mode == CCmode)
4878 count = 1;
4879 else if (mode == CCV2mode)
4880 count = 2;
4881 else if (mode == CCV4mode)
4882 count = 4;
4883 else
4884 gcc_unreachable ();
4885
4886 cfun->machine->next_fcc += -cfun->machine->next_fcc & (count - 1);
4887 if (cfun->machine->next_fcc > ST_REG_LAST - ST_REG_FIRST)
4888 cfun->machine->next_fcc = 0;
4889 regno = ST_REG_FIRST + cfun->machine->next_fcc;
4890 cfun->machine->next_fcc += count;
4891 return gen_rtx_REG (mode, regno);
4892 }
4893
4894 /* Convert a comparison into something that can be used in a branch or
4895 conditional move. On entry, *OP0 and *OP1 are the values being
4896 compared and *CODE is the code used to compare them.
4897
4898 Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
4899 If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
4900 otherwise any standard branch condition can be used. The standard branch
4901 conditions are:
4902
4903 - EQ or NE between two registers.
4904 - any comparison between a register and zero. */
4905
4906 static void
4907 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
4908 {
4909 rtx cmp_op0 = *op0;
4910 rtx cmp_op1 = *op1;
4911
4912 if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
4913 {
4914 if (!need_eq_ne_p && *op1 == const0_rtx)
4915 ;
4916 else if (*code == EQ || *code == NE)
4917 {
4918 if (need_eq_ne_p)
4919 {
4920 *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
4921 *op1 = const0_rtx;
4922 }
4923 else
4924 *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
4925 }
4926 else
4927 {
4928 /* The comparison needs a separate scc instruction. Store the
4929 result of the scc in *OP0 and compare it against zero. */
4930 bool invert = false;
4931 *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
4932 mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
4933 *code = (invert ? EQ : NE);
4934 *op1 = const0_rtx;
4935 }
4936 }
4937 else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
4938 {
4939 *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
4940 mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
4941 *code = NE;
4942 *op1 = const0_rtx;
4943 }
4944 else
4945 {
4946 enum rtx_code cmp_code;
4947
4948 /* Floating-point tests use a separate C.cond.fmt comparison to
4949 set a condition code register. The branch or conditional move
4950 will then compare that register against zero.
4951
4952 Set CMP_CODE to the code of the comparison instruction and
4953 *CODE to the code that the branch or move should use. */
4954 cmp_code = *code;
4955 *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
4956 *op0 = (ISA_HAS_8CC
4957 ? mips_allocate_fcc (CCmode)
4958 : gen_rtx_REG (CCmode, FPSW_REGNUM));
4959 *op1 = const0_rtx;
4960 mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
4961 }
4962 }
4963 \f
4964 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
4965 and OPERAND[3]. Store the result in OPERANDS[0].
4966
4967 On 64-bit targets, the mode of the comparison and target will always be
4968 SImode, thus possibly narrower than that of the comparison's operands. */
4969
4970 void
4971 mips_expand_scc (rtx operands[])
4972 {
4973 rtx target = operands[0];
4974 enum rtx_code code = GET_CODE (operands[1]);
4975 rtx op0 = operands[2];
4976 rtx op1 = operands[3];
4977
4978 gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
4979
4980 if (code == EQ || code == NE)
4981 {
4982 if (ISA_HAS_SEQ_SNE
4983 && reg_imm10_operand (op1, GET_MODE (op1)))
4984 mips_emit_binary (code, target, op0, op1);
4985 else
4986 {
4987 rtx zie = mips_zero_if_equal (op0, op1);
4988 mips_emit_binary (code, target, zie, const0_rtx);
4989 }
4990 }
4991 else
4992 mips_emit_int_order_test (code, 0, target, op0, op1);
4993 }
4994
4995 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
4996 CODE and jump to OPERANDS[3] if the condition holds. */
4997
4998 void
4999 mips_expand_conditional_branch (rtx *operands)
5000 {
5001 enum rtx_code code = GET_CODE (operands[0]);
5002 rtx op0 = operands[1];
5003 rtx op1 = operands[2];
5004 rtx condition;
5005
5006 mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
5007 condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
5008 emit_jump_insn (gen_condjump (condition, operands[3]));
5009 }
5010
5011 /* Implement:
5012
5013 (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
5014 (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS)) */
5015
5016 void
5017 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
5018 enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
5019 {
5020 rtx cmp_result;
5021 bool reversed_p;
5022
5023 reversed_p = mips_reversed_fp_cond (&cond);
5024 cmp_result = mips_allocate_fcc (CCV2mode);
5025 emit_insn (gen_scc_ps (cmp_result,
5026 gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
5027 if (reversed_p)
5028 emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
5029 cmp_result));
5030 else
5031 emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
5032 cmp_result));
5033 }
5034
5035 /* Perform the comparison in OPERANDS[1]. Move OPERANDS[2] into OPERANDS[0]
5036 if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0]. */
5037
5038 void
5039 mips_expand_conditional_move (rtx *operands)
5040 {
5041 rtx cond;
5042 enum rtx_code code = GET_CODE (operands[1]);
5043 rtx op0 = XEXP (operands[1], 0);
5044 rtx op1 = XEXP (operands[1], 1);
5045
5046 mips_emit_compare (&code, &op0, &op1, true);
5047 cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
5048 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
5049 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
5050 operands[2], operands[3])));
5051 }
5052
5053 /* Perform the comparison in COMPARISON, then trap if the condition holds. */
5054
5055 void
5056 mips_expand_conditional_trap (rtx comparison)
5057 {
5058 rtx op0, op1;
5059 enum machine_mode mode;
5060 enum rtx_code code;
5061
5062 /* MIPS conditional trap instructions don't have GT or LE flavors,
5063 so we must swap the operands and convert to LT and GE respectively. */
5064 code = GET_CODE (comparison);
5065 switch (code)
5066 {
5067 case GT:
5068 case LE:
5069 case GTU:
5070 case LEU:
5071 code = swap_condition (code);
5072 op0 = XEXP (comparison, 1);
5073 op1 = XEXP (comparison, 0);
5074 break;
5075
5076 default:
5077 op0 = XEXP (comparison, 0);
5078 op1 = XEXP (comparison, 1);
5079 break;
5080 }
5081
5082 mode = GET_MODE (XEXP (comparison, 0));
5083 op0 = force_reg (mode, op0);
5084 if (!arith_operand (op1, mode))
5085 op1 = force_reg (mode, op1);
5086
5087 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
5088 gen_rtx_fmt_ee (code, mode, op0, op1),
5089 const0_rtx));
5090 }
5091 \f
5092 /* Initialize *CUM for a call to a function of type FNTYPE. */
5093
5094 void
5095 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
5096 {
5097 memset (cum, 0, sizeof (*cum));
5098 cum->prototype = (fntype && prototype_p (fntype));
5099 cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
5100 }
5101
5102 /* Fill INFO with information about a single argument. CUM is the
5103 cumulative state for earlier arguments. MODE is the mode of this
5104 argument and TYPE is its type (if known). NAMED is true if this
5105 is a named (fixed) argument rather than a variable one. */
5106
5107 static void
5108 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
5109 enum machine_mode mode, const_tree type, bool named)
5110 {
5111 bool doubleword_aligned_p;
5112 unsigned int num_bytes, num_words, max_regs;
5113
5114 /* Work out the size of the argument. */
5115 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5116 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5117
5118 /* Decide whether it should go in a floating-point register, assuming
5119 one is free. Later code checks for availability.
5120
5121 The checks against UNITS_PER_FPVALUE handle the soft-float and
5122 single-float cases. */
5123 switch (mips_abi)
5124 {
5125 case ABI_EABI:
5126 /* The EABI conventions have traditionally been defined in terms
5127 of TYPE_MODE, regardless of the actual type. */
5128 info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
5129 || mode == V2SFmode)
5130 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5131 break;
5132
5133 case ABI_32:
5134 case ABI_O64:
5135 /* Only leading floating-point scalars are passed in
5136 floating-point registers. We also handle vector floats the same
5137 say, which is OK because they are not covered by the standard ABI. */
5138 info->fpr_p = (!cum->gp_reg_found
5139 && cum->arg_number < 2
5140 && (type == 0
5141 || SCALAR_FLOAT_TYPE_P (type)
5142 || VECTOR_FLOAT_TYPE_P (type))
5143 && (GET_MODE_CLASS (mode) == MODE_FLOAT
5144 || mode == V2SFmode)
5145 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5146 break;
5147
5148 case ABI_N32:
5149 case ABI_64:
5150 /* Scalar, complex and vector floating-point types are passed in
5151 floating-point registers, as long as this is a named rather
5152 than a variable argument. */
5153 info->fpr_p = (named
5154 && (type == 0 || FLOAT_TYPE_P (type))
5155 && (GET_MODE_CLASS (mode) == MODE_FLOAT
5156 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5157 || mode == V2SFmode)
5158 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
5159
5160 /* ??? According to the ABI documentation, the real and imaginary
5161 parts of complex floats should be passed in individual registers.
5162 The real and imaginary parts of stack arguments are supposed
5163 to be contiguous and there should be an extra word of padding
5164 at the end.
5165
5166 This has two problems. First, it makes it impossible to use a
5167 single "void *" va_list type, since register and stack arguments
5168 are passed differently. (At the time of writing, MIPSpro cannot
5169 handle complex float varargs correctly.) Second, it's unclear
5170 what should happen when there is only one register free.
5171
5172 For now, we assume that named complex floats should go into FPRs
5173 if there are two FPRs free, otherwise they should be passed in the
5174 same way as a struct containing two floats. */
5175 if (info->fpr_p
5176 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5177 && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
5178 {
5179 if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
5180 info->fpr_p = false;
5181 else
5182 num_words = 2;
5183 }
5184 break;
5185
5186 default:
5187 gcc_unreachable ();
5188 }
5189
5190 /* See whether the argument has doubleword alignment. */
5191 doubleword_aligned_p = (mips_function_arg_boundary (mode, type)
5192 > BITS_PER_WORD);
5193
5194 /* Set REG_OFFSET to the register count we're interested in.
5195 The EABI allocates the floating-point registers separately,
5196 but the other ABIs allocate them like integer registers. */
5197 info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
5198 ? cum->num_fprs
5199 : cum->num_gprs);
5200
5201 /* Advance to an even register if the argument is doubleword-aligned. */
5202 if (doubleword_aligned_p)
5203 info->reg_offset += info->reg_offset & 1;
5204
5205 /* Work out the offset of a stack argument. */
5206 info->stack_offset = cum->stack_words;
5207 if (doubleword_aligned_p)
5208 info->stack_offset += info->stack_offset & 1;
5209
5210 max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
5211
5212 /* Partition the argument between registers and stack. */
5213 info->reg_words = MIN (num_words, max_regs);
5214 info->stack_words = num_words - info->reg_words;
5215 }
5216
5217 /* INFO describes a register argument that has the normal format for the
5218 argument's mode. Return the register it uses, assuming that FPRs are
5219 available if HARD_FLOAT_P. */
5220
5221 static unsigned int
5222 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
5223 {
5224 if (!info->fpr_p || !hard_float_p)
5225 return GP_ARG_FIRST + info->reg_offset;
5226 else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
5227 /* In o32, the second argument is always passed in $f14
5228 for TARGET_DOUBLE_FLOAT, regardless of whether the
5229 first argument was a word or doubleword. */
5230 return FP_ARG_FIRST + 2;
5231 else
5232 return FP_ARG_FIRST + info->reg_offset;
5233 }
5234
5235 /* Implement TARGET_STRICT_ARGUMENT_NAMING. */
5236
5237 static bool
5238 mips_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
5239 {
5240 return !TARGET_OLDABI;
5241 }
5242
5243 /* Implement TARGET_FUNCTION_ARG. */
5244
5245 static rtx
5246 mips_function_arg (cumulative_args_t cum_v, enum machine_mode mode,
5247 const_tree type, bool named)
5248 {
5249 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5250 struct mips_arg_info info;
5251
5252 /* We will be called with a mode of VOIDmode after the last argument
5253 has been seen. Whatever we return will be passed to the call expander.
5254 If we need a MIPS16 fp_code, return a REG with the code stored as
5255 the mode. */
5256 if (mode == VOIDmode)
5257 {
5258 if (TARGET_MIPS16 && cum->fp_code != 0)
5259 return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
5260 else
5261 return NULL;
5262 }
5263
5264 mips_get_arg_info (&info, cum, mode, type, named);
5265
5266 /* Return straight away if the whole argument is passed on the stack. */
5267 if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
5268 return NULL;
5269
5270 /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
5271 contains a double in its entirety, then that 64-bit chunk is passed
5272 in a floating-point register. */
5273 if (TARGET_NEWABI
5274 && TARGET_HARD_FLOAT
5275 && named
5276 && type != 0
5277 && TREE_CODE (type) == RECORD_TYPE
5278 && TYPE_SIZE_UNIT (type)
5279 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
5280 {
5281 tree field;
5282
5283 /* First check to see if there is any such field. */
5284 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5285 if (TREE_CODE (field) == FIELD_DECL
5286 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5287 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
5288 && tree_fits_shwi_p (bit_position (field))
5289 && int_bit_position (field) % BITS_PER_WORD == 0)
5290 break;
5291
5292 if (field != 0)
5293 {
5294 /* Now handle the special case by returning a PARALLEL
5295 indicating where each 64-bit chunk goes. INFO.REG_WORDS
5296 chunks are passed in registers. */
5297 unsigned int i;
5298 HOST_WIDE_INT bitpos;
5299 rtx ret;
5300
5301 /* assign_parms checks the mode of ENTRY_PARM, so we must
5302 use the actual mode here. */
5303 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
5304
5305 bitpos = 0;
5306 field = TYPE_FIELDS (type);
5307 for (i = 0; i < info.reg_words; i++)
5308 {
5309 rtx reg;
5310
5311 for (; field; field = DECL_CHAIN (field))
5312 if (TREE_CODE (field) == FIELD_DECL
5313 && int_bit_position (field) >= bitpos)
5314 break;
5315
5316 if (field
5317 && int_bit_position (field) == bitpos
5318 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5319 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
5320 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
5321 else
5322 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
5323
5324 XVECEXP (ret, 0, i)
5325 = gen_rtx_EXPR_LIST (VOIDmode, reg,
5326 GEN_INT (bitpos / BITS_PER_UNIT));
5327
5328 bitpos += BITS_PER_WORD;
5329 }
5330 return ret;
5331 }
5332 }
5333
5334 /* Handle the n32/n64 conventions for passing complex floating-point
5335 arguments in FPR pairs. The real part goes in the lower register
5336 and the imaginary part goes in the upper register. */
5337 if (TARGET_NEWABI
5338 && info.fpr_p
5339 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5340 {
5341 rtx real, imag;
5342 enum machine_mode inner;
5343 unsigned int regno;
5344
5345 inner = GET_MODE_INNER (mode);
5346 regno = FP_ARG_FIRST + info.reg_offset;
5347 if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
5348 {
5349 /* Real part in registers, imaginary part on stack. */
5350 gcc_assert (info.stack_words == info.reg_words);
5351 return gen_rtx_REG (inner, regno);
5352 }
5353 else
5354 {
5355 gcc_assert (info.stack_words == 0);
5356 real = gen_rtx_EXPR_LIST (VOIDmode,
5357 gen_rtx_REG (inner, regno),
5358 const0_rtx);
5359 imag = gen_rtx_EXPR_LIST (VOIDmode,
5360 gen_rtx_REG (inner,
5361 regno + info.reg_words / 2),
5362 GEN_INT (GET_MODE_SIZE (inner)));
5363 return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
5364 }
5365 }
5366
5367 return gen_rtx_REG (mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
5368 }
5369
5370 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
5371
5372 static void
5373 mips_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
5374 const_tree type, bool named)
5375 {
5376 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5377 struct mips_arg_info info;
5378
5379 mips_get_arg_info (&info, cum, mode, type, named);
5380
5381 if (!info.fpr_p)
5382 cum->gp_reg_found = true;
5383
5384 /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
5385 an explanation of what this code does. It assumes that we're using
5386 either the o32 or the o64 ABI, both of which pass at most 2 arguments
5387 in FPRs. */
5388 if (cum->arg_number < 2 && info.fpr_p)
5389 cum->fp_code += (mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
5390
5391 /* Advance the register count. This has the effect of setting
5392 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
5393 argument required us to skip the final GPR and pass the whole
5394 argument on the stack. */
5395 if (mips_abi != ABI_EABI || !info.fpr_p)
5396 cum->num_gprs = info.reg_offset + info.reg_words;
5397 else if (info.reg_words > 0)
5398 cum->num_fprs += MAX_FPRS_PER_FMT;
5399
5400 /* Advance the stack word count. */
5401 if (info.stack_words > 0)
5402 cum->stack_words = info.stack_offset + info.stack_words;
5403
5404 cum->arg_number++;
5405 }
5406
5407 /* Implement TARGET_ARG_PARTIAL_BYTES. */
5408
5409 static int
5410 mips_arg_partial_bytes (cumulative_args_t cum,
5411 enum machine_mode mode, tree type, bool named)
5412 {
5413 struct mips_arg_info info;
5414
5415 mips_get_arg_info (&info, get_cumulative_args (cum), mode, type, named);
5416 return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
5417 }
5418
5419 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Every parameter gets at
5420 least PARM_BOUNDARY bits of alignment, but will be given anything up
5421 to STACK_BOUNDARY bits if the type requires it. */
5422
5423 static unsigned int
5424 mips_function_arg_boundary (enum machine_mode mode, const_tree type)
5425 {
5426 unsigned int alignment;
5427
5428 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
5429 if (alignment < PARM_BOUNDARY)
5430 alignment = PARM_BOUNDARY;
5431 if (alignment > STACK_BOUNDARY)
5432 alignment = STACK_BOUNDARY;
5433 return alignment;
5434 }
5435
5436 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
5437 upward rather than downward. In other words, return true if the
5438 first byte of the stack slot has useful data, false if the last
5439 byte does. */
5440
5441 bool
5442 mips_pad_arg_upward (enum machine_mode mode, const_tree type)
5443 {
5444 /* On little-endian targets, the first byte of every stack argument
5445 is passed in the first byte of the stack slot. */
5446 if (!BYTES_BIG_ENDIAN)
5447 return true;
5448
5449 /* Otherwise, integral types are padded downward: the last byte of a
5450 stack argument is passed in the last byte of the stack slot. */
5451 if (type != 0
5452 ? (INTEGRAL_TYPE_P (type)
5453 || POINTER_TYPE_P (type)
5454 || FIXED_POINT_TYPE_P (type))
5455 : (SCALAR_INT_MODE_P (mode)
5456 || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
5457 return false;
5458
5459 /* Big-endian o64 pads floating-point arguments downward. */
5460 if (mips_abi == ABI_O64)
5461 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5462 return false;
5463
5464 /* Other types are padded upward for o32, o64, n32 and n64. */
5465 if (mips_abi != ABI_EABI)
5466 return true;
5467
5468 /* Arguments smaller than a stack slot are padded downward. */
5469 if (mode != BLKmode)
5470 return GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY;
5471 else
5472 return int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT);
5473 }
5474
5475 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...). Return !BYTES_BIG_ENDIAN
5476 if the least significant byte of the register has useful data. Return
5477 the opposite if the most significant byte does. */
5478
5479 bool
5480 mips_pad_reg_upward (enum machine_mode mode, tree type)
5481 {
5482 /* No shifting is required for floating-point arguments. */
5483 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5484 return !BYTES_BIG_ENDIAN;
5485
5486 /* Otherwise, apply the same padding to register arguments as we do
5487 to stack arguments. */
5488 return mips_pad_arg_upward (mode, type);
5489 }
5490
5491 /* Return nonzero when an argument must be passed by reference. */
5492
5493 static bool
5494 mips_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
5495 enum machine_mode mode, const_tree type,
5496 bool named ATTRIBUTE_UNUSED)
5497 {
5498 if (mips_abi == ABI_EABI)
5499 {
5500 int size;
5501
5502 /* ??? How should SCmode be handled? */
5503 if (mode == DImode || mode == DFmode
5504 || mode == DQmode || mode == UDQmode
5505 || mode == DAmode || mode == UDAmode)
5506 return 0;
5507
5508 size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5509 return size == -1 || size > UNITS_PER_WORD;
5510 }
5511 else
5512 {
5513 /* If we have a variable-sized parameter, we have no choice. */
5514 return targetm.calls.must_pass_in_stack (mode, type);
5515 }
5516 }
5517
5518 /* Implement TARGET_CALLEE_COPIES. */
5519
5520 static bool
5521 mips_callee_copies (cumulative_args_t cum ATTRIBUTE_UNUSED,
5522 enum machine_mode mode ATTRIBUTE_UNUSED,
5523 const_tree type ATTRIBUTE_UNUSED, bool named)
5524 {
5525 return mips_abi == ABI_EABI && named;
5526 }
5527 \f
5528 /* See whether VALTYPE is a record whose fields should be returned in
5529 floating-point registers. If so, return the number of fields and
5530 list them in FIELDS (which should have two elements). Return 0
5531 otherwise.
5532
5533 For n32 & n64, a structure with one or two fields is returned in
5534 floating-point registers as long as every field has a floating-point
5535 type. */
5536
5537 static int
5538 mips_fpr_return_fields (const_tree valtype, tree *fields)
5539 {
5540 tree field;
5541 int i;
5542
5543 if (!TARGET_NEWABI)
5544 return 0;
5545
5546 if (TREE_CODE (valtype) != RECORD_TYPE)
5547 return 0;
5548
5549 i = 0;
5550 for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
5551 {
5552 if (TREE_CODE (field) != FIELD_DECL)
5553 continue;
5554
5555 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
5556 return 0;
5557
5558 if (i == 2)
5559 return 0;
5560
5561 fields[i++] = field;
5562 }
5563 return i;
5564 }
5565
5566 /* Implement TARGET_RETURN_IN_MSB. For n32 & n64, we should return
5567 a value in the most significant part of $2/$3 if:
5568
5569 - the target is big-endian;
5570
5571 - the value has a structure or union type (we generalize this to
5572 cover aggregates from other languages too); and
5573
5574 - the structure is not returned in floating-point registers. */
5575
5576 static bool
5577 mips_return_in_msb (const_tree valtype)
5578 {
5579 tree fields[2];
5580
5581 return (TARGET_NEWABI
5582 && TARGET_BIG_ENDIAN
5583 && AGGREGATE_TYPE_P (valtype)
5584 && mips_fpr_return_fields (valtype, fields) == 0);
5585 }
5586
5587 /* Return true if the function return value MODE will get returned in a
5588 floating-point register. */
5589
5590 static bool
5591 mips_return_mode_in_fpr_p (enum machine_mode mode)
5592 {
5593 return ((GET_MODE_CLASS (mode) == MODE_FLOAT
5594 || mode == V2SFmode
5595 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5596 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
5597 }
5598
5599 /* Return the representation of an FPR return register when the
5600 value being returned in FP_RETURN has mode VALUE_MODE and the
5601 return type itself has mode TYPE_MODE. On NewABI targets,
5602 the two modes may be different for structures like:
5603
5604 struct __attribute__((packed)) foo { float f; }
5605
5606 where we return the SFmode value of "f" in FP_RETURN, but where
5607 the structure itself has mode BLKmode. */
5608
5609 static rtx
5610 mips_return_fpr_single (enum machine_mode type_mode,
5611 enum machine_mode value_mode)
5612 {
5613 rtx x;
5614
5615 x = gen_rtx_REG (value_mode, FP_RETURN);
5616 if (type_mode != value_mode)
5617 {
5618 x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
5619 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
5620 }
5621 return x;
5622 }
5623
5624 /* Return a composite value in a pair of floating-point registers.
5625 MODE1 and OFFSET1 are the mode and byte offset for the first value,
5626 likewise MODE2 and OFFSET2 for the second. MODE is the mode of the
5627 complete value.
5628
5629 For n32 & n64, $f0 always holds the first value and $f2 the second.
5630 Otherwise the values are packed together as closely as possible. */
5631
5632 static rtx
5633 mips_return_fpr_pair (enum machine_mode mode,
5634 enum machine_mode mode1, HOST_WIDE_INT offset1,
5635 enum machine_mode mode2, HOST_WIDE_INT offset2)
5636 {
5637 int inc;
5638
5639 inc = (TARGET_NEWABI ? 2 : MAX_FPRS_PER_FMT);
5640 return gen_rtx_PARALLEL
5641 (mode,
5642 gen_rtvec (2,
5643 gen_rtx_EXPR_LIST (VOIDmode,
5644 gen_rtx_REG (mode1, FP_RETURN),
5645 GEN_INT (offset1)),
5646 gen_rtx_EXPR_LIST (VOIDmode,
5647 gen_rtx_REG (mode2, FP_RETURN + inc),
5648 GEN_INT (offset2))));
5649
5650 }
5651
5652 /* Implement TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE.
5653 For normal calls, VALTYPE is the return type and MODE is VOIDmode.
5654 For libcalls, VALTYPE is null and MODE is the mode of the return value. */
5655
5656 static rtx
5657 mips_function_value_1 (const_tree valtype, const_tree fn_decl_or_type,
5658 enum machine_mode mode)
5659 {
5660 if (valtype)
5661 {
5662 tree fields[2];
5663 int unsigned_p;
5664 const_tree func;
5665
5666 if (fn_decl_or_type && DECL_P (fn_decl_or_type))
5667 func = fn_decl_or_type;
5668 else
5669 func = NULL;
5670
5671 mode = TYPE_MODE (valtype);
5672 unsigned_p = TYPE_UNSIGNED (valtype);
5673
5674 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
5675 return values, promote the mode here too. */
5676 mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
5677
5678 /* Handle structures whose fields are returned in $f0/$f2. */
5679 switch (mips_fpr_return_fields (valtype, fields))
5680 {
5681 case 1:
5682 return mips_return_fpr_single (mode,
5683 TYPE_MODE (TREE_TYPE (fields[0])));
5684
5685 case 2:
5686 return mips_return_fpr_pair (mode,
5687 TYPE_MODE (TREE_TYPE (fields[0])),
5688 int_byte_position (fields[0]),
5689 TYPE_MODE (TREE_TYPE (fields[1])),
5690 int_byte_position (fields[1]));
5691 }
5692
5693 /* If a value is passed in the most significant part of a register, see
5694 whether we have to round the mode up to a whole number of words. */
5695 if (mips_return_in_msb (valtype))
5696 {
5697 HOST_WIDE_INT size = int_size_in_bytes (valtype);
5698 if (size % UNITS_PER_WORD != 0)
5699 {
5700 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
5701 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
5702 }
5703 }
5704
5705 /* For EABI, the class of return register depends entirely on MODE.
5706 For example, "struct { some_type x; }" and "union { some_type x; }"
5707 are returned in the same way as a bare "some_type" would be.
5708 Other ABIs only use FPRs for scalar, complex or vector types. */
5709 if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
5710 return gen_rtx_REG (mode, GP_RETURN);
5711 }
5712
5713 if (!TARGET_MIPS16)
5714 {
5715 /* Handle long doubles for n32 & n64. */
5716 if (mode == TFmode)
5717 return mips_return_fpr_pair (mode,
5718 DImode, 0,
5719 DImode, GET_MODE_SIZE (mode) / 2);
5720
5721 if (mips_return_mode_in_fpr_p (mode))
5722 {
5723 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5724 return mips_return_fpr_pair (mode,
5725 GET_MODE_INNER (mode), 0,
5726 GET_MODE_INNER (mode),
5727 GET_MODE_SIZE (mode) / 2);
5728 else
5729 return gen_rtx_REG (mode, FP_RETURN);
5730 }
5731 }
5732
5733 return gen_rtx_REG (mode, GP_RETURN);
5734 }
5735
5736 /* Implement TARGET_FUNCTION_VALUE. */
5737
5738 static rtx
5739 mips_function_value (const_tree valtype, const_tree fn_decl_or_type,
5740 bool outgoing ATTRIBUTE_UNUSED)
5741 {
5742 return mips_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
5743 }
5744
5745 /* Implement TARGET_LIBCALL_VALUE. */
5746
5747 static rtx
5748 mips_libcall_value (enum machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
5749 {
5750 return mips_function_value_1 (NULL_TREE, NULL_TREE, mode);
5751 }
5752
5753 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
5754
5755 On the MIPS, R2 R3 and F0 F2 are the only register thus used.
5756 Currently, R2 and F0 are only implemented here (C has no complex type). */
5757
5758 static bool
5759 mips_function_value_regno_p (const unsigned int regno)
5760 {
5761 if (regno == GP_RETURN
5762 || regno == FP_RETURN
5763 || (LONG_DOUBLE_TYPE_SIZE == 128
5764 && FP_RETURN != GP_RETURN
5765 && regno == FP_RETURN + 2))
5766 return true;
5767
5768 return false;
5769 }
5770
5771 /* Implement TARGET_RETURN_IN_MEMORY. Under the o32 and o64 ABIs,
5772 all BLKmode objects are returned in memory. Under the n32, n64
5773 and embedded ABIs, small structures are returned in a register.
5774 Objects with varying size must still be returned in memory, of
5775 course. */
5776
5777 static bool
5778 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
5779 {
5780 return (TARGET_OLDABI
5781 ? TYPE_MODE (type) == BLKmode
5782 : !IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
5783 }
5784 \f
5785 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
5786
5787 static void
5788 mips_setup_incoming_varargs (cumulative_args_t cum, enum machine_mode mode,
5789 tree type, int *pretend_size ATTRIBUTE_UNUSED,
5790 int no_rtl)
5791 {
5792 CUMULATIVE_ARGS local_cum;
5793 int gp_saved, fp_saved;
5794
5795 /* The caller has advanced CUM up to, but not beyond, the last named
5796 argument. Advance a local copy of CUM past the last "real" named
5797 argument, to find out how many registers are left over. */
5798 local_cum = *get_cumulative_args (cum);
5799 mips_function_arg_advance (pack_cumulative_args (&local_cum), mode, type,
5800 true);
5801
5802 /* Found out how many registers we need to save. */
5803 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
5804 fp_saved = (EABI_FLOAT_VARARGS_P
5805 ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
5806 : 0);
5807
5808 if (!no_rtl)
5809 {
5810 if (gp_saved > 0)
5811 {
5812 rtx ptr, mem;
5813
5814 ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
5815 REG_PARM_STACK_SPACE (cfun->decl)
5816 - gp_saved * UNITS_PER_WORD);
5817 mem = gen_frame_mem (BLKmode, ptr);
5818 set_mem_alias_set (mem, get_varargs_alias_set ());
5819
5820 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
5821 mem, gp_saved);
5822 }
5823 if (fp_saved > 0)
5824 {
5825 /* We can't use move_block_from_reg, because it will use
5826 the wrong mode. */
5827 enum machine_mode mode;
5828 int off, i;
5829
5830 /* Set OFF to the offset from virtual_incoming_args_rtx of
5831 the first float register. The FP save area lies below
5832 the integer one, and is aligned to UNITS_PER_FPVALUE bytes. */
5833 off = (-gp_saved * UNITS_PER_WORD) & -UNITS_PER_FPVALUE;
5834 off -= fp_saved * UNITS_PER_FPREG;
5835
5836 mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
5837
5838 for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
5839 i += MAX_FPRS_PER_FMT)
5840 {
5841 rtx ptr, mem;
5842
5843 ptr = plus_constant (Pmode, virtual_incoming_args_rtx, off);
5844 mem = gen_frame_mem (mode, ptr);
5845 set_mem_alias_set (mem, get_varargs_alias_set ());
5846 mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
5847 off += UNITS_PER_HWFPVALUE;
5848 }
5849 }
5850 }
5851 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
5852 cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
5853 + fp_saved * UNITS_PER_FPREG);
5854 }
5855
5856 /* Implement TARGET_BUILTIN_VA_LIST. */
5857
5858 static tree
5859 mips_build_builtin_va_list (void)
5860 {
5861 if (EABI_FLOAT_VARARGS_P)
5862 {
5863 /* We keep 3 pointers, and two offsets.
5864
5865 Two pointers are to the overflow area, which starts at the CFA.
5866 One of these is constant, for addressing into the GPR save area
5867 below it. The other is advanced up the stack through the
5868 overflow region.
5869
5870 The third pointer is to the bottom of the GPR save area.
5871 Since the FPR save area is just below it, we can address
5872 FPR slots off this pointer.
5873
5874 We also keep two one-byte offsets, which are to be subtracted
5875 from the constant pointers to yield addresses in the GPR and
5876 FPR save areas. These are downcounted as float or non-float
5877 arguments are used, and when they get to zero, the argument
5878 must be obtained from the overflow region. */
5879 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
5880 tree array, index;
5881
5882 record = lang_hooks.types.make_type (RECORD_TYPE);
5883
5884 f_ovfl = build_decl (BUILTINS_LOCATION,
5885 FIELD_DECL, get_identifier ("__overflow_argptr"),
5886 ptr_type_node);
5887 f_gtop = build_decl (BUILTINS_LOCATION,
5888 FIELD_DECL, get_identifier ("__gpr_top"),
5889 ptr_type_node);
5890 f_ftop = build_decl (BUILTINS_LOCATION,
5891 FIELD_DECL, get_identifier ("__fpr_top"),
5892 ptr_type_node);
5893 f_goff = build_decl (BUILTINS_LOCATION,
5894 FIELD_DECL, get_identifier ("__gpr_offset"),
5895 unsigned_char_type_node);
5896 f_foff = build_decl (BUILTINS_LOCATION,
5897 FIELD_DECL, get_identifier ("__fpr_offset"),
5898 unsigned_char_type_node);
5899 /* Explicitly pad to the size of a pointer, so that -Wpadded won't
5900 warn on every user file. */
5901 index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
5902 array = build_array_type (unsigned_char_type_node,
5903 build_index_type (index));
5904 f_res = build_decl (BUILTINS_LOCATION,
5905 FIELD_DECL, get_identifier ("__reserved"), array);
5906
5907 DECL_FIELD_CONTEXT (f_ovfl) = record;
5908 DECL_FIELD_CONTEXT (f_gtop) = record;
5909 DECL_FIELD_CONTEXT (f_ftop) = record;
5910 DECL_FIELD_CONTEXT (f_goff) = record;
5911 DECL_FIELD_CONTEXT (f_foff) = record;
5912 DECL_FIELD_CONTEXT (f_res) = record;
5913
5914 TYPE_FIELDS (record) = f_ovfl;
5915 DECL_CHAIN (f_ovfl) = f_gtop;
5916 DECL_CHAIN (f_gtop) = f_ftop;
5917 DECL_CHAIN (f_ftop) = f_goff;
5918 DECL_CHAIN (f_goff) = f_foff;
5919 DECL_CHAIN (f_foff) = f_res;
5920
5921 layout_type (record);
5922 return record;
5923 }
5924 else
5925 /* Otherwise, we use 'void *'. */
5926 return ptr_type_node;
5927 }
5928
5929 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
5930
5931 static void
5932 mips_va_start (tree valist, rtx nextarg)
5933 {
5934 if (EABI_FLOAT_VARARGS_P)
5935 {
5936 const CUMULATIVE_ARGS *cum;
5937 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5938 tree ovfl, gtop, ftop, goff, foff;
5939 tree t;
5940 int gpr_save_area_size;
5941 int fpr_save_area_size;
5942 int fpr_offset;
5943
5944 cum = &crtl->args.info;
5945 gpr_save_area_size
5946 = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
5947 fpr_save_area_size
5948 = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
5949
5950 f_ovfl = TYPE_FIELDS (va_list_type_node);
5951 f_gtop = DECL_CHAIN (f_ovfl);
5952 f_ftop = DECL_CHAIN (f_gtop);
5953 f_goff = DECL_CHAIN (f_ftop);
5954 f_foff = DECL_CHAIN (f_goff);
5955
5956 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5957 NULL_TREE);
5958 gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
5959 NULL_TREE);
5960 ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
5961 NULL_TREE);
5962 goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
5963 NULL_TREE);
5964 foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
5965 NULL_TREE);
5966
5967 /* Emit code to initialize OVFL, which points to the next varargs
5968 stack argument. CUM->STACK_WORDS gives the number of stack
5969 words used by named arguments. */
5970 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
5971 if (cum->stack_words > 0)
5972 t = fold_build_pointer_plus_hwi (t, cum->stack_words * UNITS_PER_WORD);
5973 t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5974 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5975
5976 /* Emit code to initialize GTOP, the top of the GPR save area. */
5977 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
5978 t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
5979 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5980
5981 /* Emit code to initialize FTOP, the top of the FPR save area.
5982 This address is gpr_save_area_bytes below GTOP, rounded
5983 down to the next fp-aligned boundary. */
5984 t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
5985 fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
5986 fpr_offset &= -UNITS_PER_FPVALUE;
5987 if (fpr_offset)
5988 t = fold_build_pointer_plus_hwi (t, -fpr_offset);
5989 t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
5990 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5991
5992 /* Emit code to initialize GOFF, the offset from GTOP of the
5993 next GPR argument. */
5994 t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
5995 build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
5996 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5997
5998 /* Likewise emit code to initialize FOFF, the offset from FTOP
5999 of the next FPR argument. */
6000 t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
6001 build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
6002 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6003 }
6004 else
6005 {
6006 nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
6007 std_expand_builtin_va_start (valist, nextarg);
6008 }
6009 }
6010
6011 /* Like std_gimplify_va_arg_expr, but apply alignment to zero-sized
6012 types as well. */
6013
6014 static tree
6015 mips_std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
6016 gimple_seq *post_p)
6017 {
6018 tree addr, t, type_size, rounded_size, valist_tmp;
6019 unsigned HOST_WIDE_INT align, boundary;
6020 bool indirect;
6021
6022 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
6023 if (indirect)
6024 type = build_pointer_type (type);
6025
6026 align = PARM_BOUNDARY / BITS_PER_UNIT;
6027 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
6028
6029 /* When we align parameter on stack for caller, if the parameter
6030 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
6031 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
6032 here with caller. */
6033 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
6034 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
6035
6036 boundary /= BITS_PER_UNIT;
6037
6038 /* Hoist the valist value into a temporary for the moment. */
6039 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
6040
6041 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
6042 requires greater alignment, we must perform dynamic alignment. */
6043 if (boundary > align)
6044 {
6045 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
6046 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
6047 gimplify_and_add (t, pre_p);
6048
6049 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
6050 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
6051 valist_tmp,
6052 build_int_cst (TREE_TYPE (valist), -boundary)));
6053 gimplify_and_add (t, pre_p);
6054 }
6055 else
6056 boundary = align;
6057
6058 /* If the actual alignment is less than the alignment of the type,
6059 adjust the type accordingly so that we don't assume strict alignment
6060 when dereferencing the pointer. */
6061 boundary *= BITS_PER_UNIT;
6062 if (boundary < TYPE_ALIGN (type))
6063 {
6064 type = build_variant_type_copy (type);
6065 TYPE_ALIGN (type) = boundary;
6066 }
6067
6068 /* Compute the rounded size of the type. */
6069 type_size = size_in_bytes (type);
6070 rounded_size = round_up (type_size, align);
6071
6072 /* Reduce rounded_size so it's sharable with the postqueue. */
6073 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
6074
6075 /* Get AP. */
6076 addr = valist_tmp;
6077 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
6078 {
6079 /* Small args are padded downward. */
6080 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
6081 rounded_size, size_int (align));
6082 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
6083 size_binop (MINUS_EXPR, rounded_size, type_size));
6084 addr = fold_build_pointer_plus (addr, t);
6085 }
6086
6087 /* Compute new value for AP. */
6088 t = fold_build_pointer_plus (valist_tmp, rounded_size);
6089 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
6090 gimplify_and_add (t, pre_p);
6091
6092 addr = fold_convert (build_pointer_type (type), addr);
6093
6094 if (indirect)
6095 addr = build_va_arg_indirect_ref (addr);
6096
6097 return build_va_arg_indirect_ref (addr);
6098 }
6099
6100 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR. */
6101
6102 static tree
6103 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
6104 gimple_seq *post_p)
6105 {
6106 tree addr;
6107 bool indirect_p;
6108
6109 indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
6110 if (indirect_p)
6111 type = build_pointer_type (type);
6112
6113 if (!EABI_FLOAT_VARARGS_P)
6114 addr = mips_std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
6115 else
6116 {
6117 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
6118 tree ovfl, top, off, align;
6119 HOST_WIDE_INT size, rsize, osize;
6120 tree t, u;
6121
6122 f_ovfl = TYPE_FIELDS (va_list_type_node);
6123 f_gtop = DECL_CHAIN (f_ovfl);
6124 f_ftop = DECL_CHAIN (f_gtop);
6125 f_goff = DECL_CHAIN (f_ftop);
6126 f_foff = DECL_CHAIN (f_goff);
6127
6128 /* Let:
6129
6130 TOP be the top of the GPR or FPR save area;
6131 OFF be the offset from TOP of the next register;
6132 ADDR_RTX be the address of the argument;
6133 SIZE be the number of bytes in the argument type;
6134 RSIZE be the number of bytes used to store the argument
6135 when it's in the register save area; and
6136 OSIZE be the number of bytes used to store it when it's
6137 in the stack overflow area.
6138
6139 The code we want is:
6140
6141 1: off &= -rsize; // round down
6142 2: if (off != 0)
6143 3: {
6144 4: addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
6145 5: off -= rsize;
6146 6: }
6147 7: else
6148 8: {
6149 9: ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
6150 10: addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
6151 11: ovfl += osize;
6152 14: }
6153
6154 [1] and [9] can sometimes be optimized away. */
6155
6156 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
6157 NULL_TREE);
6158 size = int_size_in_bytes (type);
6159
6160 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
6161 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
6162 {
6163 top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
6164 unshare_expr (valist), f_ftop, NULL_TREE);
6165 off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
6166 unshare_expr (valist), f_foff, NULL_TREE);
6167
6168 /* When va_start saves FPR arguments to the stack, each slot
6169 takes up UNITS_PER_HWFPVALUE bytes, regardless of the
6170 argument's precision. */
6171 rsize = UNITS_PER_HWFPVALUE;
6172
6173 /* Overflow arguments are padded to UNITS_PER_WORD bytes
6174 (= PARM_BOUNDARY bits). This can be different from RSIZE
6175 in two cases:
6176
6177 (1) On 32-bit targets when TYPE is a structure such as:
6178
6179 struct s { float f; };
6180
6181 Such structures are passed in paired FPRs, so RSIZE
6182 will be 8 bytes. However, the structure only takes
6183 up 4 bytes of memory, so OSIZE will only be 4.
6184
6185 (2) In combinations such as -mgp64 -msingle-float
6186 -fshort-double. Doubles passed in registers will then take
6187 up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
6188 stack take up UNITS_PER_WORD bytes. */
6189 osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
6190 }
6191 else
6192 {
6193 top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
6194 unshare_expr (valist), f_gtop, NULL_TREE);
6195 off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
6196 unshare_expr (valist), f_goff, NULL_TREE);
6197 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
6198 if (rsize > UNITS_PER_WORD)
6199 {
6200 /* [1] Emit code for: off &= -rsize. */
6201 t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
6202 build_int_cst (TREE_TYPE (off), -rsize));
6203 gimplify_assign (unshare_expr (off), t, pre_p);
6204 }
6205 osize = rsize;
6206 }
6207
6208 /* [2] Emit code to branch if off == 0. */
6209 t = build2 (NE_EXPR, boolean_type_node, unshare_expr (off),
6210 build_int_cst (TREE_TYPE (off), 0));
6211 addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
6212
6213 /* [5] Emit code for: off -= rsize. We do this as a form of
6214 post-decrement not available to C. */
6215 t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
6216 t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
6217
6218 /* [4] Emit code for:
6219 addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0). */
6220 t = fold_convert (sizetype, t);
6221 t = fold_build1 (NEGATE_EXPR, sizetype, t);
6222 t = fold_build_pointer_plus (top, t);
6223 if (BYTES_BIG_ENDIAN && rsize > size)
6224 t = fold_build_pointer_plus_hwi (t, rsize - size);
6225 COND_EXPR_THEN (addr) = t;
6226
6227 if (osize > UNITS_PER_WORD)
6228 {
6229 /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize. */
6230 t = fold_build_pointer_plus_hwi (unshare_expr (ovfl), osize - 1);
6231 u = build_int_cst (TREE_TYPE (t), -osize);
6232 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
6233 align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
6234 unshare_expr (ovfl), t);
6235 }
6236 else
6237 align = NULL;
6238
6239 /* [10, 11] Emit code for:
6240 addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
6241 ovfl += osize. */
6242 u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
6243 t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
6244 if (BYTES_BIG_ENDIAN && osize > size)
6245 t = fold_build_pointer_plus_hwi (t, osize - size);
6246
6247 /* String [9] and [10, 11] together. */
6248 if (align)
6249 t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
6250 COND_EXPR_ELSE (addr) = t;
6251
6252 addr = fold_convert (build_pointer_type (type), addr);
6253 addr = build_va_arg_indirect_ref (addr);
6254 }
6255
6256 if (indirect_p)
6257 addr = build_va_arg_indirect_ref (addr);
6258
6259 return addr;
6260 }
6261 \f
6262 /* Declare a unique, locally-binding function called NAME, then start
6263 its definition. */
6264
6265 static void
6266 mips_start_unique_function (const char *name)
6267 {
6268 tree decl;
6269
6270 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
6271 get_identifier (name),
6272 build_function_type_list (void_type_node, NULL_TREE));
6273 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
6274 NULL_TREE, void_type_node);
6275 TREE_PUBLIC (decl) = 1;
6276 TREE_STATIC (decl) = 1;
6277
6278 DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
6279
6280 targetm.asm_out.unique_section (decl, 0);
6281 switch_to_section (get_named_section (decl, NULL, 0));
6282
6283 targetm.asm_out.globalize_label (asm_out_file, name);
6284 fputs ("\t.hidden\t", asm_out_file);
6285 assemble_name (asm_out_file, name);
6286 putc ('\n', asm_out_file);
6287 }
6288
6289 /* Start a definition of function NAME. MIPS16_P indicates whether the
6290 function contains MIPS16 code. */
6291
6292 static void
6293 mips_start_function_definition (const char *name, bool mips16_p)
6294 {
6295 if (mips16_p)
6296 fprintf (asm_out_file, "\t.set\tmips16\n");
6297 else
6298 fprintf (asm_out_file, "\t.set\tnomips16\n");
6299
6300 if (TARGET_MICROMIPS)
6301 fprintf (asm_out_file, "\t.set\tmicromips\n");
6302 #ifdef HAVE_GAS_MICROMIPS
6303 else
6304 fprintf (asm_out_file, "\t.set\tnomicromips\n");
6305 #endif
6306
6307 if (!flag_inhibit_size_directive)
6308 {
6309 fputs ("\t.ent\t", asm_out_file);
6310 assemble_name (asm_out_file, name);
6311 fputs ("\n", asm_out_file);
6312 }
6313
6314 ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
6315
6316 /* Start the definition proper. */
6317 assemble_name (asm_out_file, name);
6318 fputs (":\n", asm_out_file);
6319 }
6320
6321 /* End a function definition started by mips_start_function_definition. */
6322
6323 static void
6324 mips_end_function_definition (const char *name)
6325 {
6326 if (!flag_inhibit_size_directive)
6327 {
6328 fputs ("\t.end\t", asm_out_file);
6329 assemble_name (asm_out_file, name);
6330 fputs ("\n", asm_out_file);
6331 }
6332 }
6333
6334 /* If *STUB_PTR points to a stub, output a comdat-style definition for it,
6335 then free *STUB_PTR. */
6336
6337 static void
6338 mips_finish_stub (mips_one_only_stub **stub_ptr)
6339 {
6340 mips_one_only_stub *stub = *stub_ptr;
6341 if (!stub)
6342 return;
6343
6344 const char *name = stub->get_name ();
6345 mips_start_unique_function (name);
6346 mips_start_function_definition (name, false);
6347 stub->output_body ();
6348 mips_end_function_definition (name);
6349 delete stub;
6350 *stub_ptr = 0;
6351 }
6352 \f
6353 /* Return true if calls to X can use R_MIPS_CALL* relocations. */
6354
6355 static bool
6356 mips_ok_for_lazy_binding_p (rtx x)
6357 {
6358 return (TARGET_USE_GOT
6359 && GET_CODE (x) == SYMBOL_REF
6360 && !SYMBOL_REF_BIND_NOW_P (x)
6361 && !mips_symbol_binds_local_p (x));
6362 }
6363
6364 /* Load function address ADDR into register DEST. TYPE is as for
6365 mips_expand_call. Return true if we used an explicit lazy-binding
6366 sequence. */
6367
6368 static bool
6369 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
6370 {
6371 /* If we're generating PIC, and this call is to a global function,
6372 try to allow its address to be resolved lazily. This isn't
6373 possible for sibcalls when $gp is call-saved because the value
6374 of $gp on entry to the stub would be our caller's gp, not ours. */
6375 if (TARGET_EXPLICIT_RELOCS
6376 && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
6377 && mips_ok_for_lazy_binding_p (addr))
6378 {
6379 addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
6380 emit_insn (gen_rtx_SET (VOIDmode, dest, addr));
6381 return true;
6382 }
6383 else
6384 {
6385 mips_emit_move (dest, addr);
6386 return false;
6387 }
6388 }
6389 \f
6390 /* Each locally-defined hard-float MIPS16 function has a local symbol
6391 associated with it. This hash table maps the function symbol (FUNC)
6392 to the local symbol (LOCAL). */
6393 struct GTY(()) mips16_local_alias {
6394 rtx func;
6395 rtx local;
6396 };
6397 static GTY ((param_is (struct mips16_local_alias))) htab_t mips16_local_aliases;
6398
6399 /* Hash table callbacks for mips16_local_aliases. */
6400
6401 static hashval_t
6402 mips16_local_aliases_hash (const void *entry)
6403 {
6404 const struct mips16_local_alias *alias;
6405
6406 alias = (const struct mips16_local_alias *) entry;
6407 return htab_hash_string (XSTR (alias->func, 0));
6408 }
6409
6410 static int
6411 mips16_local_aliases_eq (const void *entry1, const void *entry2)
6412 {
6413 const struct mips16_local_alias *alias1, *alias2;
6414
6415 alias1 = (const struct mips16_local_alias *) entry1;
6416 alias2 = (const struct mips16_local_alias *) entry2;
6417 return rtx_equal_p (alias1->func, alias2->func);
6418 }
6419
6420 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
6421 Return a local alias for it, creating a new one if necessary. */
6422
6423 static rtx
6424 mips16_local_alias (rtx func)
6425 {
6426 struct mips16_local_alias *alias, tmp_alias;
6427 void **slot;
6428
6429 /* Create the hash table if this is the first call. */
6430 if (mips16_local_aliases == NULL)
6431 mips16_local_aliases = htab_create_ggc (37, mips16_local_aliases_hash,
6432 mips16_local_aliases_eq, NULL);
6433
6434 /* Look up the function symbol, creating a new entry if need be. */
6435 tmp_alias.func = func;
6436 slot = htab_find_slot (mips16_local_aliases, &tmp_alias, INSERT);
6437 gcc_assert (slot != NULL);
6438
6439 alias = (struct mips16_local_alias *) *slot;
6440 if (alias == NULL)
6441 {
6442 const char *func_name, *local_name;
6443 rtx local;
6444
6445 /* Create a new SYMBOL_REF for the local symbol. The choice of
6446 __fn_local_* is based on the __fn_stub_* names that we've
6447 traditionally used for the non-MIPS16 stub. */
6448 func_name = targetm.strip_name_encoding (XSTR (func, 0));
6449 local_name = ACONCAT (("__fn_local_", func_name, NULL));
6450 local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
6451 SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
6452
6453 /* Create a new structure to represent the mapping. */
6454 alias = ggc_alloc_mips16_local_alias ();
6455 alias->func = func;
6456 alias->local = local;
6457 *slot = alias;
6458 }
6459 return alias->local;
6460 }
6461 \f
6462 /* A chained list of functions for which mips16_build_call_stub has already
6463 generated a stub. NAME is the name of the function and FP_RET_P is true
6464 if the function returns a value in floating-point registers. */
6465 struct mips16_stub {
6466 struct mips16_stub *next;
6467 char *name;
6468 bool fp_ret_p;
6469 };
6470 static struct mips16_stub *mips16_stubs;
6471
6472 /* Return the two-character string that identifies floating-point
6473 return mode MODE in the name of a MIPS16 function stub. */
6474
6475 static const char *
6476 mips16_call_stub_mode_suffix (enum machine_mode mode)
6477 {
6478 if (mode == SFmode)
6479 return "sf";
6480 else if (mode == DFmode)
6481 return "df";
6482 else if (mode == SCmode)
6483 return "sc";
6484 else if (mode == DCmode)
6485 return "dc";
6486 else if (mode == V2SFmode)
6487 return "df";
6488 else
6489 gcc_unreachable ();
6490 }
6491
6492 /* Write instructions to move a 32-bit value between general register
6493 GPREG and floating-point register FPREG. DIRECTION is 't' to move
6494 from GPREG to FPREG and 'f' to move in the opposite direction. */
6495
6496 static void
6497 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6498 {
6499 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6500 reg_names[gpreg], reg_names[fpreg]);
6501 }
6502
6503 /* Likewise for 64-bit values. */
6504
6505 static void
6506 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6507 {
6508 if (TARGET_64BIT)
6509 fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
6510 reg_names[gpreg], reg_names[fpreg]);
6511 else if (TARGET_FLOAT64)
6512 {
6513 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6514 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6515 fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
6516 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
6517 }
6518 else
6519 {
6520 /* Move the least-significant word. */
6521 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6522 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6523 /* ...then the most significant word. */
6524 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6525 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
6526 }
6527 }
6528
6529 /* Write out code to move floating-point arguments into or out of
6530 general registers. FP_CODE is the code describing which arguments
6531 are present (see the comment above the definition of CUMULATIVE_ARGS
6532 in mips.h). DIRECTION is as for mips_output_32bit_xfer. */
6533
6534 static void
6535 mips_output_args_xfer (int fp_code, char direction)
6536 {
6537 unsigned int gparg, fparg, f;
6538 CUMULATIVE_ARGS cum;
6539
6540 /* This code only works for o32 and o64. */
6541 gcc_assert (TARGET_OLDABI);
6542
6543 mips_init_cumulative_args (&cum, NULL);
6544
6545 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6546 {
6547 enum machine_mode mode;
6548 struct mips_arg_info info;
6549
6550 if ((f & 3) == 1)
6551 mode = SFmode;
6552 else if ((f & 3) == 2)
6553 mode = DFmode;
6554 else
6555 gcc_unreachable ();
6556
6557 mips_get_arg_info (&info, &cum, mode, NULL, true);
6558 gparg = mips_arg_regno (&info, false);
6559 fparg = mips_arg_regno (&info, true);
6560
6561 if (mode == SFmode)
6562 mips_output_32bit_xfer (direction, gparg, fparg);
6563 else
6564 mips_output_64bit_xfer (direction, gparg, fparg);
6565
6566 mips_function_arg_advance (pack_cumulative_args (&cum), mode, NULL, true);
6567 }
6568 }
6569
6570 /* Write a MIPS16 stub for the current function. This stub is used
6571 for functions which take arguments in the floating-point registers.
6572 It is normal-mode code that moves the floating-point arguments
6573 into the general registers and then jumps to the MIPS16 code. */
6574
6575 static void
6576 mips16_build_function_stub (void)
6577 {
6578 const char *fnname, *alias_name, *separator;
6579 char *secname, *stubname;
6580 tree stubdecl;
6581 unsigned int f;
6582 rtx symbol, alias;
6583
6584 /* Create the name of the stub, and its unique section. */
6585 symbol = XEXP (DECL_RTL (current_function_decl), 0);
6586 alias = mips16_local_alias (symbol);
6587
6588 fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
6589 alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
6590 secname = ACONCAT ((".mips16.fn.", fnname, NULL));
6591 stubname = ACONCAT (("__fn_stub_", fnname, NULL));
6592
6593 /* Build a decl for the stub. */
6594 stubdecl = build_decl (BUILTINS_LOCATION,
6595 FUNCTION_DECL, get_identifier (stubname),
6596 build_function_type_list (void_type_node, NULL_TREE));
6597 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6598 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6599 RESULT_DECL, NULL_TREE, void_type_node);
6600
6601 /* Output a comment. */
6602 fprintf (asm_out_file, "\t# Stub function for %s (",
6603 current_function_name ());
6604 separator = "";
6605 for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
6606 {
6607 fprintf (asm_out_file, "%s%s", separator,
6608 (f & 3) == 1 ? "float" : "double");
6609 separator = ", ";
6610 }
6611 fprintf (asm_out_file, ")\n");
6612
6613 /* Start the function definition. */
6614 assemble_start_function (stubdecl, stubname);
6615 mips_start_function_definition (stubname, false);
6616
6617 /* If generating pic2 code, either set up the global pointer or
6618 switch to pic0. */
6619 if (TARGET_ABICALLS_PIC2)
6620 {
6621 if (TARGET_ABSOLUTE_ABICALLS)
6622 fprintf (asm_out_file, "\t.option\tpic0\n");
6623 else
6624 {
6625 output_asm_insn ("%(.cpload\t%^%)", NULL);
6626 /* Emit an R_MIPS_NONE relocation to tell the linker what the
6627 target function is. Use a local GOT access when loading the
6628 symbol, to cut down on the number of unnecessary GOT entries
6629 for stubs that aren't needed. */
6630 output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
6631 symbol = alias;
6632 }
6633 }
6634
6635 /* Load the address of the MIPS16 function into $25. Do this first so
6636 that targets with coprocessor interlocks can use an MFC1 to fill the
6637 delay slot. */
6638 output_asm_insn ("la\t%^,%0", &symbol);
6639
6640 /* Move the arguments from floating-point registers to general registers. */
6641 mips_output_args_xfer (crtl->args.info.fp_code, 'f');
6642
6643 /* Jump to the MIPS16 function. */
6644 output_asm_insn ("jr\t%^", NULL);
6645
6646 if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
6647 fprintf (asm_out_file, "\t.option\tpic2\n");
6648
6649 mips_end_function_definition (stubname);
6650
6651 /* If the linker needs to create a dynamic symbol for the target
6652 function, it will associate the symbol with the stub (which,
6653 unlike the target function, follows the proper calling conventions).
6654 It is therefore useful to have a local alias for the target function,
6655 so that it can still be identified as MIPS16 code. As an optimization,
6656 this symbol can also be used for indirect MIPS16 references from
6657 within this file. */
6658 ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
6659
6660 switch_to_section (function_section (current_function_decl));
6661 }
6662
6663 /* The current function is a MIPS16 function that returns a value in an FPR.
6664 Copy the return value from its soft-float to its hard-float location.
6665 libgcc2 has special non-MIPS16 helper functions for each case. */
6666
6667 static void
6668 mips16_copy_fpr_return_value (void)
6669 {
6670 rtx fn, insn, retval;
6671 tree return_type;
6672 enum machine_mode return_mode;
6673 const char *name;
6674
6675 return_type = DECL_RESULT (current_function_decl);
6676 return_mode = DECL_MODE (return_type);
6677
6678 name = ACONCAT (("__mips16_ret_",
6679 mips16_call_stub_mode_suffix (return_mode),
6680 NULL));
6681 fn = mips16_stub_function (name);
6682
6683 /* The function takes arguments in $2 (and possibly $3), so calls
6684 to it cannot be lazily bound. */
6685 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
6686
6687 /* Model the call as something that takes the GPR return value as
6688 argument and returns an "updated" value. */
6689 retval = gen_rtx_REG (return_mode, GP_RETURN);
6690 insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
6691 const0_rtx, NULL_RTX, false);
6692 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
6693 }
6694
6695 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
6696 RETVAL is the location of the return value, or null if this is
6697 a "call" rather than a "call_value". ARGS_SIZE is the size of the
6698 arguments and FP_CODE is the code built by mips_function_arg;
6699 see the comment before the fp_code field in CUMULATIVE_ARGS for details.
6700
6701 There are three alternatives:
6702
6703 - If a stub was needed, emit the call and return the call insn itself.
6704
6705 - If we can avoid using a stub by redirecting the call, set *FN_PTR
6706 to the new target and return null.
6707
6708 - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
6709 unmodified.
6710
6711 A stub is needed for calls to functions that, in normal mode,
6712 receive arguments in FPRs or return values in FPRs. The stub
6713 copies the arguments from their soft-float positions to their
6714 hard-float positions, calls the real function, then copies the
6715 return value from its hard-float position to its soft-float
6716 position.
6717
6718 We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
6719 If *FN_PTR turns out to be to a non-MIPS16 function, the linker
6720 automatically redirects the JAL to the stub, otherwise the JAL
6721 continues to call FN directly. */
6722
6723 static rtx
6724 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
6725 {
6726 const char *fnname;
6727 bool fp_ret_p;
6728 struct mips16_stub *l;
6729 rtx insn, fn;
6730
6731 /* We don't need to do anything if we aren't in MIPS16 mode, or if
6732 we were invoked with the -msoft-float option. */
6733 if (!TARGET_MIPS16 || TARGET_SOFT_FLOAT_ABI)
6734 return NULL_RTX;
6735
6736 /* Figure out whether the value might come back in a floating-point
6737 register. */
6738 fp_ret_p = retval && mips_return_mode_in_fpr_p (GET_MODE (retval));
6739
6740 /* We don't need to do anything if there were no floating-point
6741 arguments and the value will not be returned in a floating-point
6742 register. */
6743 if (fp_code == 0 && !fp_ret_p)
6744 return NULL_RTX;
6745
6746 /* We don't need to do anything if this is a call to a special
6747 MIPS16 support function. */
6748 fn = *fn_ptr;
6749 if (mips16_stub_function_p (fn))
6750 return NULL_RTX;
6751
6752 /* If we're calling a locally-defined MIPS16 function, we know that
6753 it will return values in both the "soft-float" and "hard-float"
6754 registers. There is no need to use a stub to move the latter
6755 to the former. */
6756 if (fp_code == 0 && mips16_local_function_p (fn))
6757 return NULL_RTX;
6758
6759 /* This code will only work for o32 and o64 abis. The other ABI's
6760 require more sophisticated support. */
6761 gcc_assert (TARGET_OLDABI);
6762
6763 /* If we're calling via a function pointer, use one of the magic
6764 libgcc.a stubs provided for each (FP_CODE, FP_RET_P) combination.
6765 Each stub expects the function address to arrive in register $2. */
6766 if (GET_CODE (fn) != SYMBOL_REF
6767 || !call_insn_operand (fn, VOIDmode))
6768 {
6769 char buf[30];
6770 rtx stub_fn, insn, addr;
6771 bool lazy_p;
6772
6773 /* If this is a locally-defined and locally-binding function,
6774 avoid the stub by calling the local alias directly. */
6775 if (mips16_local_function_p (fn))
6776 {
6777 *fn_ptr = mips16_local_alias (fn);
6778 return NULL_RTX;
6779 }
6780
6781 /* Create a SYMBOL_REF for the libgcc.a function. */
6782 if (fp_ret_p)
6783 sprintf (buf, "__mips16_call_stub_%s_%d",
6784 mips16_call_stub_mode_suffix (GET_MODE (retval)),
6785 fp_code);
6786 else
6787 sprintf (buf, "__mips16_call_stub_%d", fp_code);
6788 stub_fn = mips16_stub_function (buf);
6789
6790 /* The function uses $2 as an argument, so calls to it
6791 cannot be lazily bound. */
6792 SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
6793
6794 /* Load the target function into $2. */
6795 addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
6796 lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
6797
6798 /* Emit the call. */
6799 insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
6800 args_size, NULL_RTX, lazy_p);
6801
6802 /* Tell GCC that this call does indeed use the value of $2. */
6803 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
6804
6805 /* If we are handling a floating-point return value, we need to
6806 save $18 in the function prologue. Putting a note on the
6807 call will mean that df_regs_ever_live_p ($18) will be true if the
6808 call is not eliminated, and we can check that in the prologue
6809 code. */
6810 if (fp_ret_p)
6811 CALL_INSN_FUNCTION_USAGE (insn) =
6812 gen_rtx_EXPR_LIST (VOIDmode,
6813 gen_rtx_CLOBBER (VOIDmode,
6814 gen_rtx_REG (word_mode, 18)),
6815 CALL_INSN_FUNCTION_USAGE (insn));
6816
6817 return insn;
6818 }
6819
6820 /* We know the function we are going to call. If we have already
6821 built a stub, we don't need to do anything further. */
6822 fnname = targetm.strip_name_encoding (XSTR (fn, 0));
6823 for (l = mips16_stubs; l != NULL; l = l->next)
6824 if (strcmp (l->name, fnname) == 0)
6825 break;
6826
6827 if (l == NULL)
6828 {
6829 const char *separator;
6830 char *secname, *stubname;
6831 tree stubid, stubdecl;
6832 unsigned int f;
6833
6834 /* If the function does not return in FPRs, the special stub
6835 section is named
6836 .mips16.call.FNNAME
6837
6838 If the function does return in FPRs, the stub section is named
6839 .mips16.call.fp.FNNAME
6840
6841 Build a decl for the stub. */
6842 secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
6843 fnname, NULL));
6844 stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
6845 fnname, NULL));
6846 stubid = get_identifier (stubname);
6847 stubdecl = build_decl (BUILTINS_LOCATION,
6848 FUNCTION_DECL, stubid,
6849 build_function_type_list (void_type_node,
6850 NULL_TREE));
6851 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
6852 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6853 RESULT_DECL, NULL_TREE,
6854 void_type_node);
6855
6856 /* Output a comment. */
6857 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
6858 (fp_ret_p
6859 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
6860 : ""),
6861 fnname);
6862 separator = "";
6863 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6864 {
6865 fprintf (asm_out_file, "%s%s", separator,
6866 (f & 3) == 1 ? "float" : "double");
6867 separator = ", ";
6868 }
6869 fprintf (asm_out_file, ")\n");
6870
6871 /* Start the function definition. */
6872 assemble_start_function (stubdecl, stubname);
6873 mips_start_function_definition (stubname, false);
6874
6875 if (fp_ret_p)
6876 {
6877 fprintf (asm_out_file, "\t.cfi_startproc\n");
6878
6879 /* Create a fake CFA 4 bytes below the stack pointer.
6880 This works around unwinders (like libgcc's) that expect
6881 the CFA for non-signal frames to be unique. */
6882 fprintf (asm_out_file, "\t.cfi_def_cfa 29,-4\n");
6883
6884 /* "Save" $sp in itself so we don't use the fake CFA.
6885 This is: DW_CFA_val_expression r29, { DW_OP_reg29 }. */
6886 fprintf (asm_out_file, "\t.cfi_escape 0x16,29,1,0x6d\n");
6887 }
6888 else
6889 {
6890 /* Load the address of the MIPS16 function into $25. Do this
6891 first so that targets with coprocessor interlocks can use
6892 an MFC1 to fill the delay slot. */
6893 if (TARGET_EXPLICIT_RELOCS)
6894 {
6895 output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
6896 output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
6897 }
6898 else
6899 output_asm_insn ("la\t%^,%0", &fn);
6900 }
6901
6902 /* Move the arguments from general registers to floating-point
6903 registers. */
6904 mips_output_args_xfer (fp_code, 't');
6905
6906 if (fp_ret_p)
6907 {
6908 /* Save the return address in $18 and call the non-MIPS16 function.
6909 The stub's caller knows that $18 might be clobbered, even though
6910 $18 is usually a call-saved register. */
6911 fprintf (asm_out_file, "\tmove\t%s,%s\n",
6912 reg_names[GP_REG_FIRST + 18], reg_names[RETURN_ADDR_REGNUM]);
6913 output_asm_insn (MIPS_CALL ("jal", &fn, 0, -1), &fn);
6914 fprintf (asm_out_file, "\t.cfi_register 31,18\n");
6915
6916 /* Move the result from floating-point registers to
6917 general registers. */
6918 switch (GET_MODE (retval))
6919 {
6920 case SCmode:
6921 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
6922 TARGET_BIG_ENDIAN
6923 ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6924 : FP_REG_FIRST);
6925 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
6926 TARGET_LITTLE_ENDIAN
6927 ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6928 : FP_REG_FIRST);
6929 if (GET_MODE (retval) == SCmode && TARGET_64BIT)
6930 {
6931 /* On 64-bit targets, complex floats are returned in
6932 a single GPR, such that "sd" on a suitably-aligned
6933 target would store the value correctly. */
6934 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6935 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6936 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6937 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6938 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
6939 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
6940 fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
6941 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6942 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6943 fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
6944 reg_names[GP_RETURN],
6945 reg_names[GP_RETURN],
6946 reg_names[GP_RETURN + 1]);
6947 }
6948 break;
6949
6950 case SFmode:
6951 mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6952 break;
6953
6954 case DCmode:
6955 mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
6956 FP_REG_FIRST + MAX_FPRS_PER_FMT);
6957 /* Fall though. */
6958 case DFmode:
6959 case V2SFmode:
6960 mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6961 break;
6962
6963 default:
6964 gcc_unreachable ();
6965 }
6966 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
6967 fprintf (asm_out_file, "\t.cfi_endproc\n");
6968 }
6969 else
6970 {
6971 /* Jump to the previously-loaded address. */
6972 output_asm_insn ("jr\t%^", NULL);
6973 }
6974
6975 #ifdef ASM_DECLARE_FUNCTION_SIZE
6976 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
6977 #endif
6978
6979 mips_end_function_definition (stubname);
6980
6981 /* Record this stub. */
6982 l = XNEW (struct mips16_stub);
6983 l->name = xstrdup (fnname);
6984 l->fp_ret_p = fp_ret_p;
6985 l->next = mips16_stubs;
6986 mips16_stubs = l;
6987 }
6988
6989 /* If we expect a floating-point return value, but we've built a
6990 stub which does not expect one, then we're in trouble. We can't
6991 use the existing stub, because it won't handle the floating-point
6992 value. We can't build a new stub, because the linker won't know
6993 which stub to use for the various calls in this object file.
6994 Fortunately, this case is illegal, since it means that a function
6995 was declared in two different ways in a single compilation. */
6996 if (fp_ret_p && !l->fp_ret_p)
6997 error ("cannot handle inconsistent calls to %qs", fnname);
6998
6999 if (retval == NULL_RTX)
7000 insn = gen_call_internal_direct (fn, args_size);
7001 else
7002 insn = gen_call_value_internal_direct (retval, fn, args_size);
7003 insn = mips_emit_call_insn (insn, fn, fn, false);
7004
7005 /* If we are calling a stub which handles a floating-point return
7006 value, we need to arrange to save $18 in the prologue. We do this
7007 by marking the function call as using the register. The prologue
7008 will later see that it is used, and emit code to save it. */
7009 if (fp_ret_p)
7010 CALL_INSN_FUNCTION_USAGE (insn) =
7011 gen_rtx_EXPR_LIST (VOIDmode,
7012 gen_rtx_CLOBBER (VOIDmode,
7013 gen_rtx_REG (word_mode, 18)),
7014 CALL_INSN_FUNCTION_USAGE (insn));
7015
7016 return insn;
7017 }
7018 \f
7019 /* Expand a call of type TYPE. RESULT is where the result will go (null
7020 for "call"s and "sibcall"s), ADDR is the address of the function,
7021 ARGS_SIZE is the size of the arguments and AUX is the value passed
7022 to us by mips_function_arg. LAZY_P is true if this call already
7023 involves a lazily-bound function address (such as when calling
7024 functions through a MIPS16 hard-float stub).
7025
7026 Return the call itself. */
7027
7028 rtx
7029 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
7030 rtx args_size, rtx aux, bool lazy_p)
7031 {
7032 rtx orig_addr, pattern, insn;
7033 int fp_code;
7034
7035 fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
7036 insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
7037 if (insn)
7038 {
7039 gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
7040 return insn;
7041 }
7042
7043 orig_addr = addr;
7044 if (!call_insn_operand (addr, VOIDmode))
7045 {
7046 if (type == MIPS_CALL_EPILOGUE)
7047 addr = MIPS_EPILOGUE_TEMP (Pmode);
7048 else
7049 addr = gen_reg_rtx (Pmode);
7050 lazy_p |= mips_load_call_address (type, addr, orig_addr);
7051 }
7052
7053 if (result == 0)
7054 {
7055 rtx (*fn) (rtx, rtx);
7056
7057 if (type == MIPS_CALL_SIBCALL)
7058 fn = gen_sibcall_internal;
7059 else
7060 fn = gen_call_internal;
7061
7062 pattern = fn (addr, args_size);
7063 }
7064 else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
7065 {
7066 /* Handle return values created by mips_return_fpr_pair. */
7067 rtx (*fn) (rtx, rtx, rtx, rtx);
7068 rtx reg1, reg2;
7069
7070 if (type == MIPS_CALL_SIBCALL)
7071 fn = gen_sibcall_value_multiple_internal;
7072 else
7073 fn = gen_call_value_multiple_internal;
7074
7075 reg1 = XEXP (XVECEXP (result, 0, 0), 0);
7076 reg2 = XEXP (XVECEXP (result, 0, 1), 0);
7077 pattern = fn (reg1, addr, args_size, reg2);
7078 }
7079 else
7080 {
7081 rtx (*fn) (rtx, rtx, rtx);
7082
7083 if (type == MIPS_CALL_SIBCALL)
7084 fn = gen_sibcall_value_internal;
7085 else
7086 fn = gen_call_value_internal;
7087
7088 /* Handle return values created by mips_return_fpr_single. */
7089 if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
7090 result = XEXP (XVECEXP (result, 0, 0), 0);
7091 pattern = fn (result, addr, args_size);
7092 }
7093
7094 return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
7095 }
7096
7097 /* Split call instruction INSN into a $gp-clobbering call and
7098 (where necessary) an instruction to restore $gp from its save slot.
7099 CALL_PATTERN is the pattern of the new call. */
7100
7101 void
7102 mips_split_call (rtx insn, rtx call_pattern)
7103 {
7104 emit_call_insn (call_pattern);
7105 if (!find_reg_note (insn, REG_NORETURN, 0))
7106 /* Pick a temporary register that is suitable for both MIPS16 and
7107 non-MIPS16 code. $4 and $5 are used for returning complex double
7108 values in soft-float code, so $6 is the first suitable candidate. */
7109 mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode, GP_ARG_FIRST + 2));
7110 }
7111
7112 /* Return true if a call to DECL may need to use JALX. */
7113
7114 static bool
7115 mips_call_may_need_jalx_p (tree decl)
7116 {
7117 /* If the current translation unit would use a different mode for DECL,
7118 assume that the call needs JALX. */
7119 if (mips_get_compress_mode (decl) != TARGET_COMPRESSION)
7120 return true;
7121
7122 /* mips_get_compress_mode is always accurate for locally-binding
7123 functions in the current translation unit. */
7124 if (!DECL_EXTERNAL (decl) && targetm.binds_local_p (decl))
7125 return false;
7126
7127 /* When -minterlink-compressed is in effect, assume that functions
7128 could use a different encoding mode unless an attribute explicitly
7129 tells us otherwise. */
7130 if (TARGET_INTERLINK_COMPRESSED)
7131 {
7132 if (!TARGET_COMPRESSION
7133 && mips_get_compress_off_flags (DECL_ATTRIBUTES (decl)) ==0)
7134 return true;
7135 if (TARGET_COMPRESSION
7136 && mips_get_compress_on_flags (DECL_ATTRIBUTES (decl)) == 0)
7137 return true;
7138 }
7139
7140 return false;
7141 }
7142
7143 /* Implement TARGET_FUNCTION_OK_FOR_SIBCALL. */
7144
7145 static bool
7146 mips_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
7147 {
7148 if (!TARGET_SIBCALLS)
7149 return false;
7150
7151 /* Interrupt handlers need special epilogue code and therefore can't
7152 use sibcalls. */
7153 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
7154 return false;
7155
7156 /* Direct Js are only possible to functions that use the same ISA encoding.
7157 There is no JX counterpoart of JALX. */
7158 if (decl
7159 && const_call_insn_operand (XEXP (DECL_RTL (decl), 0), VOIDmode)
7160 && mips_call_may_need_jalx_p (decl))
7161 return false;
7162
7163 /* Sibling calls should not prevent lazy binding. Lazy-binding stubs
7164 require $gp to be valid on entry, so sibcalls can only use stubs
7165 if $gp is call-clobbered. */
7166 if (decl
7167 && TARGET_CALL_SAVED_GP
7168 && !TARGET_ABICALLS_PIC0
7169 && !targetm.binds_local_p (decl))
7170 return false;
7171
7172 /* Otherwise OK. */
7173 return true;
7174 }
7175 \f
7176 /* Emit code to move general operand SRC into condition-code
7177 register DEST given that SCRATCH is a scratch TFmode FPR.
7178 The sequence is:
7179
7180 FP1 = SRC
7181 FP2 = 0.0f
7182 DEST = FP2 < FP1
7183
7184 where FP1 and FP2 are single-precision FPRs taken from SCRATCH. */
7185
7186 void
7187 mips_expand_fcc_reload (rtx dest, rtx src, rtx scratch)
7188 {
7189 rtx fp1, fp2;
7190
7191 /* Change the source to SFmode. */
7192 if (MEM_P (src))
7193 src = adjust_address (src, SFmode, 0);
7194 else if (REG_P (src) || GET_CODE (src) == SUBREG)
7195 src = gen_rtx_REG (SFmode, true_regnum (src));
7196
7197 fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
7198 fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + MAX_FPRS_PER_FMT);
7199
7200 mips_emit_move (copy_rtx (fp1), src);
7201 mips_emit_move (copy_rtx (fp2), CONST0_RTX (SFmode));
7202 emit_insn (gen_slt_sf (dest, fp2, fp1));
7203 }
7204 \f
7205 /* Implement MOVE_BY_PIECES_P. */
7206
7207 bool
7208 mips_move_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7209 {
7210 if (HAVE_movmemsi)
7211 {
7212 /* movmemsi is meant to generate code that is at least as good as
7213 move_by_pieces. However, movmemsi effectively uses a by-pieces
7214 implementation both for moves smaller than a word and for
7215 word-aligned moves of no more than MIPS_MAX_MOVE_BYTES_STRAIGHT
7216 bytes. We should allow the tree-level optimisers to do such
7217 moves by pieces, as it often exposes other optimization
7218 opportunities. We might as well continue to use movmemsi at
7219 the rtl level though, as it produces better code when
7220 scheduling is disabled (such as at -O). */
7221 if (currently_expanding_to_rtl)
7222 return false;
7223 if (align < BITS_PER_WORD)
7224 return size < UNITS_PER_WORD;
7225 return size <= MIPS_MAX_MOVE_BYTES_STRAIGHT;
7226 }
7227 /* The default value. If this becomes a target hook, we should
7228 call the default definition instead. */
7229 return (move_by_pieces_ninsns (size, align, MOVE_MAX_PIECES + 1)
7230 < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()));
7231 }
7232
7233 /* Implement STORE_BY_PIECES_P. */
7234
7235 bool
7236 mips_store_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7237 {
7238 /* Storing by pieces involves moving constants into registers
7239 of size MIN (ALIGN, BITS_PER_WORD), then storing them.
7240 We need to decide whether it is cheaper to load the address of
7241 constant data into a register and use a block move instead. */
7242
7243 /* If the data is only byte aligned, then:
7244
7245 (a1) A block move of less than 4 bytes would involve three 3 LBs and
7246 3 SBs. We might as well use 3 single-instruction LIs and 3 SBs
7247 instead.
7248
7249 (a2) A block move of 4 bytes from aligned source data can use an
7250 LW/SWL/SWR sequence. This is often better than the 4 LIs and
7251 4 SBs that we would generate when storing by pieces. */
7252 if (align <= BITS_PER_UNIT)
7253 return size < 4;
7254
7255 /* If the data is 2-byte aligned, then:
7256
7257 (b1) A block move of less than 4 bytes would use a combination of LBs,
7258 LHs, SBs and SHs. We get better code by using single-instruction
7259 LIs, SBs and SHs instead.
7260
7261 (b2) A block move of 4 bytes from aligned source data would again use
7262 an LW/SWL/SWR sequence. In most cases, loading the address of
7263 the source data would require at least one extra instruction.
7264 It is often more efficient to use 2 single-instruction LIs and
7265 2 SHs instead.
7266
7267 (b3) A block move of up to 3 additional bytes would be like (b1).
7268
7269 (b4) A block move of 8 bytes from aligned source data can use two
7270 LW/SWL/SWR sequences or a single LD/SDL/SDR sequence. Both
7271 sequences are better than the 4 LIs and 4 SHs that we'd generate
7272 when storing by pieces.
7273
7274 The reasoning for higher alignments is similar:
7275
7276 (c1) A block move of less than 4 bytes would be the same as (b1).
7277
7278 (c2) A block move of 4 bytes would use an LW/SW sequence. Again,
7279 loading the address of the source data would typically require
7280 at least one extra instruction. It is generally better to use
7281 LUI/ORI/SW instead.
7282
7283 (c3) A block move of up to 3 additional bytes would be like (b1).
7284
7285 (c4) A block move of 8 bytes can use two LW/SW sequences or a single
7286 LD/SD sequence, and in these cases we've traditionally preferred
7287 the memory copy over the more bulky constant moves. */
7288 return size < 8;
7289 }
7290
7291 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
7292 Assume that the areas do not overlap. */
7293
7294 static void
7295 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
7296 {
7297 HOST_WIDE_INT offset, delta;
7298 unsigned HOST_WIDE_INT bits;
7299 int i;
7300 enum machine_mode mode;
7301 rtx *regs;
7302
7303 /* Work out how many bits to move at a time. If both operands have
7304 half-word alignment, it is usually better to move in half words.
7305 For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
7306 and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
7307 Otherwise move word-sized chunks. */
7308 if (MEM_ALIGN (src) == BITS_PER_WORD / 2
7309 && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
7310 bits = BITS_PER_WORD / 2;
7311 else
7312 bits = BITS_PER_WORD;
7313
7314 mode = mode_for_size (bits, MODE_INT, 0);
7315 delta = bits / BITS_PER_UNIT;
7316
7317 /* Allocate a buffer for the temporary registers. */
7318 regs = XALLOCAVEC (rtx, length / delta);
7319
7320 /* Load as many BITS-sized chunks as possible. Use a normal load if
7321 the source has enough alignment, otherwise use left/right pairs. */
7322 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7323 {
7324 regs[i] = gen_reg_rtx (mode);
7325 if (MEM_ALIGN (src) >= bits)
7326 mips_emit_move (regs[i], adjust_address (src, mode, offset));
7327 else
7328 {
7329 rtx part = adjust_address (src, BLKmode, offset);
7330 set_mem_size (part, delta);
7331 if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0, 0))
7332 gcc_unreachable ();
7333 }
7334 }
7335
7336 /* Copy the chunks to the destination. */
7337 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7338 if (MEM_ALIGN (dest) >= bits)
7339 mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
7340 else
7341 {
7342 rtx part = adjust_address (dest, BLKmode, offset);
7343 set_mem_size (part, delta);
7344 if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
7345 gcc_unreachable ();
7346 }
7347
7348 /* Mop up any left-over bytes. */
7349 if (offset < length)
7350 {
7351 src = adjust_address (src, BLKmode, offset);
7352 dest = adjust_address (dest, BLKmode, offset);
7353 move_by_pieces (dest, src, length - offset,
7354 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
7355 }
7356 }
7357
7358 /* Helper function for doing a loop-based block operation on memory
7359 reference MEM. Each iteration of the loop will operate on LENGTH
7360 bytes of MEM.
7361
7362 Create a new base register for use within the loop and point it to
7363 the start of MEM. Create a new memory reference that uses this
7364 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
7365
7366 static void
7367 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
7368 rtx *loop_reg, rtx *loop_mem)
7369 {
7370 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
7371
7372 /* Although the new mem does not refer to a known location,
7373 it does keep up to LENGTH bytes of alignment. */
7374 *loop_mem = change_address (mem, BLKmode, *loop_reg);
7375 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
7376 }
7377
7378 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
7379 bytes at a time. LENGTH must be at least BYTES_PER_ITER. Assume that
7380 the memory regions do not overlap. */
7381
7382 static void
7383 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
7384 HOST_WIDE_INT bytes_per_iter)
7385 {
7386 rtx label, src_reg, dest_reg, final_src, test;
7387 HOST_WIDE_INT leftover;
7388
7389 leftover = length % bytes_per_iter;
7390 length -= leftover;
7391
7392 /* Create registers and memory references for use within the loop. */
7393 mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
7394 mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
7395
7396 /* Calculate the value that SRC_REG should have after the last iteration
7397 of the loop. */
7398 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
7399 0, 0, OPTAB_WIDEN);
7400
7401 /* Emit the start of the loop. */
7402 label = gen_label_rtx ();
7403 emit_label (label);
7404
7405 /* Emit the loop body. */
7406 mips_block_move_straight (dest, src, bytes_per_iter);
7407
7408 /* Move on to the next block. */
7409 mips_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
7410 mips_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
7411
7412 /* Emit the loop condition. */
7413 test = gen_rtx_NE (VOIDmode, src_reg, final_src);
7414 if (Pmode == DImode)
7415 emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
7416 else
7417 emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
7418
7419 /* Mop up any left-over bytes. */
7420 if (leftover)
7421 mips_block_move_straight (dest, src, leftover);
7422 }
7423
7424 /* Expand a movmemsi instruction, which copies LENGTH bytes from
7425 memory reference SRC to memory reference DEST. */
7426
7427 bool
7428 mips_expand_block_move (rtx dest, rtx src, rtx length)
7429 {
7430 if (CONST_INT_P (length))
7431 {
7432 if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_STRAIGHT)
7433 {
7434 mips_block_move_straight (dest, src, INTVAL (length));
7435 return true;
7436 }
7437 else if (optimize)
7438 {
7439 mips_block_move_loop (dest, src, INTVAL (length),
7440 MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
7441 return true;
7442 }
7443 }
7444 return false;
7445 }
7446 \f
7447 /* Expand a loop of synci insns for the address range [BEGIN, END). */
7448
7449 void
7450 mips_expand_synci_loop (rtx begin, rtx end)
7451 {
7452 rtx inc, label, end_label, cmp_result, mask, length;
7453
7454 /* Create end_label. */
7455 end_label = gen_label_rtx ();
7456
7457 /* Check if begin equals end. */
7458 cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
7459 emit_jump_insn (gen_condjump (cmp_result, end_label));
7460
7461 /* Load INC with the cache line size (rdhwr INC,$1). */
7462 inc = gen_reg_rtx (Pmode);
7463 emit_insn (PMODE_INSN (gen_rdhwr_synci_step, (inc)));
7464
7465 /* Check if inc is 0. */
7466 cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
7467 emit_jump_insn (gen_condjump (cmp_result, end_label));
7468
7469 /* Calculate mask. */
7470 mask = mips_force_unary (Pmode, NEG, inc);
7471
7472 /* Mask out begin by mask. */
7473 begin = mips_force_binary (Pmode, AND, begin, mask);
7474
7475 /* Calculate length. */
7476 length = mips_force_binary (Pmode, MINUS, end, begin);
7477
7478 /* Loop back to here. */
7479 label = gen_label_rtx ();
7480 emit_label (label);
7481
7482 emit_insn (gen_synci (begin));
7483
7484 /* Update length. */
7485 mips_emit_binary (MINUS, length, length, inc);
7486
7487 /* Update begin. */
7488 mips_emit_binary (PLUS, begin, begin, inc);
7489
7490 /* Check if length is greater than 0. */
7491 cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
7492 emit_jump_insn (gen_condjump (cmp_result, label));
7493
7494 emit_label (end_label);
7495 }
7496 \f
7497 /* Expand a QI or HI mode atomic memory operation.
7498
7499 GENERATOR contains a pointer to the gen_* function that generates
7500 the SI mode underlying atomic operation using masks that we
7501 calculate.
7502
7503 RESULT is the return register for the operation. Its value is NULL
7504 if unused.
7505
7506 MEM is the location of the atomic access.
7507
7508 OLDVAL is the first operand for the operation.
7509
7510 NEWVAL is the optional second operand for the operation. Its value
7511 is NULL if unused. */
7512
7513 void
7514 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
7515 rtx result, rtx mem, rtx oldval, rtx newval)
7516 {
7517 rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
7518 rtx unshifted_mask_reg, mask, inverted_mask, si_op;
7519 rtx res = NULL;
7520 enum machine_mode mode;
7521
7522 mode = GET_MODE (mem);
7523
7524 /* Compute the address of the containing SImode value. */
7525 orig_addr = force_reg (Pmode, XEXP (mem, 0));
7526 memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
7527 force_reg (Pmode, GEN_INT (-4)));
7528
7529 /* Create a memory reference for it. */
7530 memsi = gen_rtx_MEM (SImode, memsi_addr);
7531 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
7532 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
7533
7534 /* Work out the byte offset of the QImode or HImode value,
7535 counting from the least significant byte. */
7536 shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
7537 if (TARGET_BIG_ENDIAN)
7538 mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
7539
7540 /* Multiply by eight to convert the shift value from bytes to bits. */
7541 mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
7542
7543 /* Make the final shift an SImode value, so that it can be used in
7544 SImode operations. */
7545 shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
7546
7547 /* Set MASK to an inclusive mask of the QImode or HImode value. */
7548 unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
7549 unshifted_mask_reg = force_reg (SImode, unshifted_mask);
7550 mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
7551
7552 /* Compute the equivalent exclusive mask. */
7553 inverted_mask = gen_reg_rtx (SImode);
7554 emit_insn (gen_rtx_SET (VOIDmode, inverted_mask,
7555 gen_rtx_NOT (SImode, mask)));
7556
7557 /* Shift the old value into place. */
7558 if (oldval != const0_rtx)
7559 {
7560 oldval = convert_modes (SImode, mode, oldval, true);
7561 oldval = force_reg (SImode, oldval);
7562 oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
7563 }
7564
7565 /* Do the same for the new value. */
7566 if (newval && newval != const0_rtx)
7567 {
7568 newval = convert_modes (SImode, mode, newval, true);
7569 newval = force_reg (SImode, newval);
7570 newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
7571 }
7572
7573 /* Do the SImode atomic access. */
7574 if (result)
7575 res = gen_reg_rtx (SImode);
7576 if (newval)
7577 si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
7578 else if (result)
7579 si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
7580 else
7581 si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
7582
7583 emit_insn (si_op);
7584
7585 if (result)
7586 {
7587 /* Shift and convert the result. */
7588 mips_emit_binary (AND, res, res, mask);
7589 mips_emit_binary (LSHIFTRT, res, res, shiftsi);
7590 mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
7591 }
7592 }
7593
7594 /* Return true if it is possible to use left/right accesses for a
7595 bitfield of WIDTH bits starting BITPOS bits into BLKmode memory OP.
7596 When returning true, update *LEFT and *RIGHT as follows:
7597
7598 *LEFT is a QImode reference to the first byte if big endian or
7599 the last byte if little endian. This address can be used in the
7600 left-side instructions (LWL, SWL, LDL, SDL).
7601
7602 *RIGHT is a QImode reference to the opposite end of the field and
7603 can be used in the patterning right-side instruction. */
7604
7605 static bool
7606 mips_get_unaligned_mem (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
7607 rtx *left, rtx *right)
7608 {
7609 rtx first, last;
7610
7611 /* Check that the size is valid. */
7612 if (width != 32 && (!TARGET_64BIT || width != 64))
7613 return false;
7614
7615 /* We can only access byte-aligned values. Since we are always passed
7616 a reference to the first byte of the field, it is not necessary to
7617 do anything with BITPOS after this check. */
7618 if (bitpos % BITS_PER_UNIT != 0)
7619 return false;
7620
7621 /* Reject aligned bitfields: we want to use a normal load or store
7622 instead of a left/right pair. */
7623 if (MEM_ALIGN (op) >= width)
7624 return false;
7625
7626 /* Get references to both ends of the field. */
7627 first = adjust_address (op, QImode, 0);
7628 last = adjust_address (op, QImode, width / BITS_PER_UNIT - 1);
7629
7630 /* Allocate to LEFT and RIGHT according to endianness. LEFT should
7631 correspond to the MSB and RIGHT to the LSB. */
7632 if (TARGET_BIG_ENDIAN)
7633 *left = first, *right = last;
7634 else
7635 *left = last, *right = first;
7636
7637 return true;
7638 }
7639
7640 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
7641 DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
7642 the operation is the equivalent of:
7643
7644 (set DEST (*_extract SRC WIDTH BITPOS))
7645
7646 Return true on success. */
7647
7648 bool
7649 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
7650 HOST_WIDE_INT bitpos, bool unsigned_p)
7651 {
7652 rtx left, right, temp;
7653 rtx dest1 = NULL_RTX;
7654
7655 /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
7656 be a DImode, create a new temp and emit a zero extend at the end. */
7657 if (GET_MODE (dest) == DImode
7658 && REG_P (dest)
7659 && GET_MODE_BITSIZE (SImode) == width)
7660 {
7661 dest1 = dest;
7662 dest = gen_reg_rtx (SImode);
7663 }
7664
7665 if (!mips_get_unaligned_mem (src, width, bitpos, &left, &right))
7666 return false;
7667
7668 temp = gen_reg_rtx (GET_MODE (dest));
7669 if (GET_MODE (dest) == DImode)
7670 {
7671 emit_insn (gen_mov_ldl (temp, src, left));
7672 emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
7673 }
7674 else
7675 {
7676 emit_insn (gen_mov_lwl (temp, src, left));
7677 emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
7678 }
7679
7680 /* If we were loading 32bits and the original register was DI then
7681 sign/zero extend into the orignal dest. */
7682 if (dest1)
7683 {
7684 if (unsigned_p)
7685 emit_insn (gen_zero_extendsidi2 (dest1, dest));
7686 else
7687 emit_insn (gen_extendsidi2 (dest1, dest));
7688 }
7689 return true;
7690 }
7691
7692 /* Try to use left/right stores to expand an "ins" pattern. DEST, WIDTH,
7693 BITPOS and SRC are the operands passed to the expander; the operation
7694 is the equivalent of:
7695
7696 (set (zero_extract DEST WIDTH BITPOS) SRC)
7697
7698 Return true on success. */
7699
7700 bool
7701 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
7702 HOST_WIDE_INT bitpos)
7703 {
7704 rtx left, right;
7705 enum machine_mode mode;
7706
7707 if (!mips_get_unaligned_mem (dest, width, bitpos, &left, &right))
7708 return false;
7709
7710 mode = mode_for_size (width, MODE_INT, 0);
7711 src = gen_lowpart (mode, src);
7712 if (mode == DImode)
7713 {
7714 emit_insn (gen_mov_sdl (dest, src, left));
7715 emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
7716 }
7717 else
7718 {
7719 emit_insn (gen_mov_swl (dest, src, left));
7720 emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
7721 }
7722 return true;
7723 }
7724
7725 /* Return true if X is a MEM with the same size as MODE. */
7726
7727 bool
7728 mips_mem_fits_mode_p (enum machine_mode mode, rtx x)
7729 {
7730 return (MEM_P (x)
7731 && MEM_SIZE_KNOWN_P (x)
7732 && MEM_SIZE (x) == GET_MODE_SIZE (mode));
7733 }
7734
7735 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
7736 source of an "ext" instruction or the destination of an "ins"
7737 instruction. OP must be a register operand and the following
7738 conditions must hold:
7739
7740 0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
7741 0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7742 0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7743
7744 Also reject lengths equal to a word as they are better handled
7745 by the move patterns. */
7746
7747 bool
7748 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
7749 {
7750 if (!ISA_HAS_EXT_INS
7751 || !register_operand (op, VOIDmode)
7752 || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
7753 return false;
7754
7755 if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
7756 return false;
7757
7758 if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
7759 return false;
7760
7761 return true;
7762 }
7763
7764 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
7765 operation if MAXLEN is the maxium length of consecutive bits that
7766 can make up MASK. MODE is the mode of the operation. See
7767 mask_low_and_shift_len for the actual definition. */
7768
7769 bool
7770 mask_low_and_shift_p (enum machine_mode mode, rtx mask, rtx shift, int maxlen)
7771 {
7772 return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
7773 }
7774
7775 /* Return true iff OP1 and OP2 are valid operands together for the
7776 *and<MODE>3 and *and<MODE>3_mips16 patterns. For the cases to consider,
7777 see the table in the comment before the pattern. */
7778
7779 bool
7780 and_operands_ok (enum machine_mode mode, rtx op1, rtx op2)
7781 {
7782 return (memory_operand (op1, mode)
7783 ? and_load_operand (op2, mode)
7784 : and_reg_operand (op2, mode));
7785 }
7786
7787 /* The canonical form of a mask-low-and-shift-left operation is
7788 (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
7789 cleared. Thus we need to shift MASK to the right before checking if it
7790 is a valid mask value. MODE is the mode of the operation. If true
7791 return the length of the mask, otherwise return -1. */
7792
7793 int
7794 mask_low_and_shift_len (enum machine_mode mode, rtx mask, rtx shift)
7795 {
7796 HOST_WIDE_INT shval;
7797
7798 shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
7799 return exact_log2 ((UINTVAL (mask) >> shval) + 1);
7800 }
7801 \f
7802 /* Return true if -msplit-addresses is selected and should be honored.
7803
7804 -msplit-addresses is a half-way house between explicit relocations
7805 and the traditional assembler macros. It can split absolute 32-bit
7806 symbolic constants into a high/lo_sum pair but uses macros for other
7807 sorts of access.
7808
7809 Like explicit relocation support for REL targets, it relies
7810 on GNU extensions in the assembler and the linker.
7811
7812 Although this code should work for -O0, it has traditionally
7813 been treated as an optimization. */
7814
7815 static bool
7816 mips_split_addresses_p (void)
7817 {
7818 return (TARGET_SPLIT_ADDRESSES
7819 && optimize
7820 && !TARGET_MIPS16
7821 && !flag_pic
7822 && !ABI_HAS_64BIT_SYMBOLS);
7823 }
7824
7825 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs. */
7826
7827 static void
7828 mips_init_relocs (void)
7829 {
7830 memset (mips_split_p, '\0', sizeof (mips_split_p));
7831 memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
7832 memset (mips_use_pcrel_pool_p, '\0', sizeof (mips_use_pcrel_pool_p));
7833 memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
7834 memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
7835
7836 if (TARGET_MIPS16_PCREL_LOADS)
7837 mips_use_pcrel_pool_p[SYMBOL_ABSOLUTE] = true;
7838 else
7839 {
7840 if (ABI_HAS_64BIT_SYMBOLS)
7841 {
7842 if (TARGET_EXPLICIT_RELOCS)
7843 {
7844 mips_split_p[SYMBOL_64_HIGH] = true;
7845 mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
7846 mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
7847
7848 mips_split_p[SYMBOL_64_MID] = true;
7849 mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
7850 mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
7851
7852 mips_split_p[SYMBOL_64_LOW] = true;
7853 mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
7854 mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
7855
7856 mips_split_p[SYMBOL_ABSOLUTE] = true;
7857 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7858 }
7859 }
7860 else
7861 {
7862 if (TARGET_EXPLICIT_RELOCS
7863 || mips_split_addresses_p ()
7864 || TARGET_MIPS16)
7865 {
7866 mips_split_p[SYMBOL_ABSOLUTE] = true;
7867 mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
7868 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7869 }
7870 }
7871 }
7872
7873 if (TARGET_MIPS16)
7874 {
7875 /* The high part is provided by a pseudo copy of $gp. */
7876 mips_split_p[SYMBOL_GP_RELATIVE] = true;
7877 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
7878 }
7879 else if (TARGET_EXPLICIT_RELOCS)
7880 /* Small data constants are kept whole until after reload,
7881 then lowered by mips_rewrite_small_data. */
7882 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
7883
7884 if (TARGET_EXPLICIT_RELOCS)
7885 {
7886 mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
7887 if (TARGET_NEWABI)
7888 {
7889 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
7890 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
7891 }
7892 else
7893 {
7894 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
7895 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
7896 }
7897 if (TARGET_MIPS16)
7898 /* Expose the use of $28 as soon as possible. */
7899 mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
7900
7901 if (TARGET_XGOT)
7902 {
7903 /* The HIGH and LO_SUM are matched by special .md patterns. */
7904 mips_split_p[SYMBOL_GOT_DISP] = true;
7905
7906 mips_split_p[SYMBOL_GOTOFF_DISP] = true;
7907 mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
7908 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
7909
7910 mips_split_p[SYMBOL_GOTOFF_CALL] = true;
7911 mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
7912 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
7913 }
7914 else
7915 {
7916 if (TARGET_NEWABI)
7917 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
7918 else
7919 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
7920 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
7921 if (TARGET_MIPS16)
7922 /* Expose the use of $28 as soon as possible. */
7923 mips_split_p[SYMBOL_GOT_DISP] = true;
7924 }
7925 }
7926
7927 if (TARGET_NEWABI)
7928 {
7929 mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
7930 mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
7931 mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
7932 }
7933
7934 mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
7935 mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
7936
7937 if (TARGET_MIPS16_PCREL_LOADS)
7938 {
7939 mips_use_pcrel_pool_p[SYMBOL_DTPREL] = true;
7940 mips_use_pcrel_pool_p[SYMBOL_TPREL] = true;
7941 }
7942 else
7943 {
7944 mips_split_p[SYMBOL_DTPREL] = true;
7945 mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
7946 mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
7947
7948 mips_split_p[SYMBOL_TPREL] = true;
7949 mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
7950 mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
7951 }
7952
7953 mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
7954 mips_lo_relocs[SYMBOL_HALF] = "%half(";
7955 }
7956
7957 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
7958 in context CONTEXT. RELOCS is the array of relocations to use. */
7959
7960 static void
7961 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
7962 const char **relocs)
7963 {
7964 enum mips_symbol_type symbol_type;
7965 const char *p;
7966
7967 symbol_type = mips_classify_symbolic_expression (op, context);
7968 gcc_assert (relocs[symbol_type]);
7969
7970 fputs (relocs[symbol_type], file);
7971 output_addr_const (file, mips_strip_unspec_address (op));
7972 for (p = relocs[symbol_type]; *p != 0; p++)
7973 if (*p == '(')
7974 fputc (')', file);
7975 }
7976
7977 /* Start a new block with the given asm switch enabled. If we need
7978 to print a directive, emit PREFIX before it and SUFFIX after it. */
7979
7980 static void
7981 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
7982 const char *prefix, const char *suffix)
7983 {
7984 if (asm_switch->nesting_level == 0)
7985 fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
7986 asm_switch->nesting_level++;
7987 }
7988
7989 /* Likewise, but end a block. */
7990
7991 static void
7992 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
7993 const char *prefix, const char *suffix)
7994 {
7995 gcc_assert (asm_switch->nesting_level);
7996 asm_switch->nesting_level--;
7997 if (asm_switch->nesting_level == 0)
7998 fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
7999 }
8000
8001 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
8002 that either print a complete line or print nothing. */
8003
8004 void
8005 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
8006 {
8007 mips_push_asm_switch_1 (asm_switch, "\t", "\n");
8008 }
8009
8010 void
8011 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
8012 {
8013 mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
8014 }
8015
8016 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
8017 The punctuation characters are:
8018
8019 '(' Start a nested ".set noreorder" block.
8020 ')' End a nested ".set noreorder" block.
8021 '[' Start a nested ".set noat" block.
8022 ']' End a nested ".set noat" block.
8023 '<' Start a nested ".set nomacro" block.
8024 '>' End a nested ".set nomacro" block.
8025 '*' Behave like %(%< if generating a delayed-branch sequence.
8026 '#' Print a nop if in a ".set noreorder" block.
8027 '/' Like '#', but do nothing within a delayed-branch sequence.
8028 '?' Print "l" if mips_branch_likely is true
8029 '~' Print a nop if mips_branch_likely is true
8030 '.' Print the name of the register with a hard-wired zero (zero or $0).
8031 '@' Print the name of the assembler temporary register (at or $1).
8032 '^' Print the name of the pic call-through register (t9 or $25).
8033 '+' Print the name of the gp register (usually gp or $28).
8034 '$' Print the name of the stack pointer register (sp or $29).
8035 ':' Print "c" to use the compact version if the delay slot is a nop.
8036 '!' Print "s" to use the short version if the delay slot contains a
8037 16-bit instruction.
8038
8039 See also mips_init_print_operand_pucnt. */
8040
8041 static void
8042 mips_print_operand_punctuation (FILE *file, int ch)
8043 {
8044 switch (ch)
8045 {
8046 case '(':
8047 mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
8048 break;
8049
8050 case ')':
8051 mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
8052 break;
8053
8054 case '[':
8055 mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
8056 break;
8057
8058 case ']':
8059 mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
8060 break;
8061
8062 case '<':
8063 mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
8064 break;
8065
8066 case '>':
8067 mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
8068 break;
8069
8070 case '*':
8071 if (final_sequence != 0)
8072 {
8073 mips_print_operand_punctuation (file, '(');
8074 mips_print_operand_punctuation (file, '<');
8075 }
8076 break;
8077
8078 case '#':
8079 if (mips_noreorder.nesting_level > 0)
8080 fputs ("\n\tnop", file);
8081 break;
8082
8083 case '/':
8084 /* Print an extra newline so that the delayed insn is separated
8085 from the following ones. This looks neater and is consistent
8086 with non-nop delayed sequences. */
8087 if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
8088 fputs ("\n\tnop\n", file);
8089 break;
8090
8091 case '?':
8092 if (mips_branch_likely)
8093 putc ('l', file);
8094 break;
8095
8096 case '~':
8097 if (mips_branch_likely)
8098 fputs ("\n\tnop", file);
8099 break;
8100
8101 case '.':
8102 fputs (reg_names[GP_REG_FIRST + 0], file);
8103 break;
8104
8105 case '@':
8106 fputs (reg_names[AT_REGNUM], file);
8107 break;
8108
8109 case '^':
8110 fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
8111 break;
8112
8113 case '+':
8114 fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
8115 break;
8116
8117 case '$':
8118 fputs (reg_names[STACK_POINTER_REGNUM], file);
8119 break;
8120
8121 case ':':
8122 /* When final_sequence is 0, the delay slot will be a nop. We can
8123 use the compact version for microMIPS. */
8124 if (final_sequence == 0)
8125 putc ('c', file);
8126 break;
8127
8128 case '!':
8129 /* If the delay slot instruction is short, then use the
8130 compact version. */
8131 if (final_sequence == 0
8132 || get_attr_length (XVECEXP (final_sequence, 0, 1)) == 2)
8133 putc ('s', file);
8134 break;
8135
8136 default:
8137 gcc_unreachable ();
8138 break;
8139 }
8140 }
8141
8142 /* Initialize mips_print_operand_punct. */
8143
8144 static void
8145 mips_init_print_operand_punct (void)
8146 {
8147 const char *p;
8148
8149 for (p = "()[]<>*#/?~.@^+$:!"; *p; p++)
8150 mips_print_operand_punct[(unsigned char) *p] = true;
8151 }
8152
8153 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
8154 associated with condition CODE. Print the condition part of the
8155 opcode to FILE. */
8156
8157 static void
8158 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
8159 {
8160 switch (code)
8161 {
8162 case EQ:
8163 case NE:
8164 case GT:
8165 case GE:
8166 case LT:
8167 case LE:
8168 case GTU:
8169 case GEU:
8170 case LTU:
8171 case LEU:
8172 /* Conveniently, the MIPS names for these conditions are the same
8173 as their RTL equivalents. */
8174 fputs (GET_RTX_NAME (code), file);
8175 break;
8176
8177 default:
8178 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8179 break;
8180 }
8181 }
8182
8183 /* Likewise floating-point branches. */
8184
8185 static void
8186 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
8187 {
8188 switch (code)
8189 {
8190 case EQ:
8191 fputs ("c1f", file);
8192 break;
8193
8194 case NE:
8195 fputs ("c1t", file);
8196 break;
8197
8198 default:
8199 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8200 break;
8201 }
8202 }
8203
8204 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
8205
8206 static bool
8207 mips_print_operand_punct_valid_p (unsigned char code)
8208 {
8209 return mips_print_operand_punct[code];
8210 }
8211
8212 /* Implement TARGET_PRINT_OPERAND. The MIPS-specific operand codes are:
8213
8214 'X' Print CONST_INT OP in hexadecimal format.
8215 'x' Print the low 16 bits of CONST_INT OP in hexadecimal format.
8216 'd' Print CONST_INT OP in decimal.
8217 'm' Print one less than CONST_INT OP in decimal.
8218 'h' Print the high-part relocation associated with OP, after stripping
8219 any outermost HIGH.
8220 'R' Print the low-part relocation associated with OP.
8221 'C' Print the integer branch condition for comparison OP.
8222 'N' Print the inverse of the integer branch condition for comparison OP.
8223 'F' Print the FPU branch condition for comparison OP.
8224 'W' Print the inverse of the FPU branch condition for comparison OP.
8225 'T' Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
8226 'z' for (eq:?I ...), 'n' for (ne:?I ...).
8227 't' Like 'T', but with the EQ/NE cases reversed
8228 'Y' Print mips_fp_conditions[INTVAL (OP)]
8229 'Z' Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
8230 'q' Print a DSP accumulator register.
8231 'D' Print the second part of a double-word register or memory operand.
8232 'L' Print the low-order register in a double-word register operand.
8233 'M' Print high-order register in a double-word register operand.
8234 'z' Print $0 if OP is zero, otherwise print OP normally.
8235 'b' Print the address of a memory operand, without offset. */
8236
8237 static void
8238 mips_print_operand (FILE *file, rtx op, int letter)
8239 {
8240 enum rtx_code code;
8241
8242 if (mips_print_operand_punct_valid_p (letter))
8243 {
8244 mips_print_operand_punctuation (file, letter);
8245 return;
8246 }
8247
8248 gcc_assert (op);
8249 code = GET_CODE (op);
8250
8251 switch (letter)
8252 {
8253 case 'X':
8254 if (CONST_INT_P (op))
8255 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
8256 else
8257 output_operand_lossage ("invalid use of '%%%c'", letter);
8258 break;
8259
8260 case 'x':
8261 if (CONST_INT_P (op))
8262 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
8263 else
8264 output_operand_lossage ("invalid use of '%%%c'", letter);
8265 break;
8266
8267 case 'd':
8268 if (CONST_INT_P (op))
8269 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
8270 else
8271 output_operand_lossage ("invalid use of '%%%c'", letter);
8272 break;
8273
8274 case 'm':
8275 if (CONST_INT_P (op))
8276 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
8277 else
8278 output_operand_lossage ("invalid use of '%%%c'", letter);
8279 break;
8280
8281 case 'h':
8282 if (code == HIGH)
8283 op = XEXP (op, 0);
8284 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
8285 break;
8286
8287 case 'R':
8288 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
8289 break;
8290
8291 case 'C':
8292 mips_print_int_branch_condition (file, code, letter);
8293 break;
8294
8295 case 'N':
8296 mips_print_int_branch_condition (file, reverse_condition (code), letter);
8297 break;
8298
8299 case 'F':
8300 mips_print_float_branch_condition (file, code, letter);
8301 break;
8302
8303 case 'W':
8304 mips_print_float_branch_condition (file, reverse_condition (code),
8305 letter);
8306 break;
8307
8308 case 'T':
8309 case 't':
8310 {
8311 int truth = (code == NE) == (letter == 'T');
8312 fputc ("zfnt"[truth * 2 + ST_REG_P (REGNO (XEXP (op, 0)))], file);
8313 }
8314 break;
8315
8316 case 'Y':
8317 if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
8318 fputs (mips_fp_conditions[UINTVAL (op)], file);
8319 else
8320 output_operand_lossage ("'%%%c' is not a valid operand prefix",
8321 letter);
8322 break;
8323
8324 case 'Z':
8325 if (ISA_HAS_8CC)
8326 {
8327 mips_print_operand (file, op, 0);
8328 fputc (',', file);
8329 }
8330 break;
8331
8332 case 'q':
8333 if (code == REG && MD_REG_P (REGNO (op)))
8334 fprintf (file, "$ac0");
8335 else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
8336 fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
8337 else
8338 output_operand_lossage ("invalid use of '%%%c'", letter);
8339 break;
8340
8341 default:
8342 switch (code)
8343 {
8344 case REG:
8345 {
8346 unsigned int regno = REGNO (op);
8347 if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
8348 || (letter == 'L' && TARGET_BIG_ENDIAN)
8349 || letter == 'D')
8350 regno++;
8351 else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
8352 output_operand_lossage ("invalid use of '%%%c'", letter);
8353 /* We need to print $0 .. $31 for COP0 registers. */
8354 if (COP0_REG_P (regno))
8355 fprintf (file, "$%s", &reg_names[regno][4]);
8356 else
8357 fprintf (file, "%s", reg_names[regno]);
8358 }
8359 break;
8360
8361 case MEM:
8362 if (letter == 'D')
8363 output_address (plus_constant (Pmode, XEXP (op, 0), 4));
8364 else if (letter == 'b')
8365 {
8366 gcc_assert (REG_P (XEXP (op, 0)));
8367 mips_print_operand (file, XEXP (op, 0), 0);
8368 }
8369 else if (letter && letter != 'z')
8370 output_operand_lossage ("invalid use of '%%%c'", letter);
8371 else
8372 output_address (XEXP (op, 0));
8373 break;
8374
8375 default:
8376 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
8377 fputs (reg_names[GP_REG_FIRST], file);
8378 else if (letter && letter != 'z')
8379 output_operand_lossage ("invalid use of '%%%c'", letter);
8380 else if (CONST_GP_P (op))
8381 fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
8382 else
8383 output_addr_const (file, mips_strip_unspec_address (op));
8384 break;
8385 }
8386 }
8387 }
8388
8389 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
8390
8391 static void
8392 mips_print_operand_address (FILE *file, rtx x)
8393 {
8394 struct mips_address_info addr;
8395
8396 if (mips_classify_address (&addr, x, word_mode, true))
8397 switch (addr.type)
8398 {
8399 case ADDRESS_REG:
8400 mips_print_operand (file, addr.offset, 0);
8401 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8402 return;
8403
8404 case ADDRESS_LO_SUM:
8405 mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
8406 mips_lo_relocs);
8407 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8408 return;
8409
8410 case ADDRESS_CONST_INT:
8411 output_addr_const (file, x);
8412 fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
8413 return;
8414
8415 case ADDRESS_SYMBOLIC:
8416 output_addr_const (file, mips_strip_unspec_address (x));
8417 return;
8418 }
8419 gcc_unreachable ();
8420 }
8421 \f
8422 /* Implement TARGET_ENCODE_SECTION_INFO. */
8423
8424 static void
8425 mips_encode_section_info (tree decl, rtx rtl, int first)
8426 {
8427 default_encode_section_info (decl, rtl, first);
8428
8429 if (TREE_CODE (decl) == FUNCTION_DECL)
8430 {
8431 rtx symbol = XEXP (rtl, 0);
8432 tree type = TREE_TYPE (decl);
8433
8434 /* Encode whether the symbol is short or long. */
8435 if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
8436 || mips_far_type_p (type))
8437 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
8438 }
8439 }
8440
8441 /* Implement TARGET_SELECT_RTX_SECTION. */
8442
8443 static section *
8444 mips_select_rtx_section (enum machine_mode mode, rtx x,
8445 unsigned HOST_WIDE_INT align)
8446 {
8447 /* ??? Consider using mergeable small data sections. */
8448 if (mips_rtx_constant_in_small_data_p (mode))
8449 return get_named_section (NULL, ".sdata", 0);
8450
8451 return default_elf_select_rtx_section (mode, x, align);
8452 }
8453
8454 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
8455
8456 The complication here is that, with the combination TARGET_ABICALLS
8457 && !TARGET_ABSOLUTE_ABICALLS && !TARGET_GPWORD, jump tables will use
8458 absolute addresses, and should therefore not be included in the
8459 read-only part of a DSO. Handle such cases by selecting a normal
8460 data section instead of a read-only one. The logic apes that in
8461 default_function_rodata_section. */
8462
8463 static section *
8464 mips_function_rodata_section (tree decl)
8465 {
8466 if (!TARGET_ABICALLS || TARGET_ABSOLUTE_ABICALLS || TARGET_GPWORD)
8467 return default_function_rodata_section (decl);
8468
8469 if (decl && DECL_SECTION_NAME (decl))
8470 {
8471 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
8472 if (DECL_ONE_ONLY (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
8473 {
8474 char *rname = ASTRDUP (name);
8475 rname[14] = 'd';
8476 return get_section (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
8477 }
8478 else if (flag_function_sections
8479 && flag_data_sections
8480 && strncmp (name, ".text.", 6) == 0)
8481 {
8482 char *rname = ASTRDUP (name);
8483 memcpy (rname + 1, "data", 4);
8484 return get_section (rname, SECTION_WRITE, decl);
8485 }
8486 }
8487 return data_section;
8488 }
8489
8490 /* Implement TARGET_IN_SMALL_DATA_P. */
8491
8492 static bool
8493 mips_in_small_data_p (const_tree decl)
8494 {
8495 unsigned HOST_WIDE_INT size;
8496
8497 if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
8498 return false;
8499
8500 /* We don't yet generate small-data references for -mabicalls
8501 or VxWorks RTP code. See the related -G handling in
8502 mips_option_override. */
8503 if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
8504 return false;
8505
8506 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
8507 {
8508 const char *name;
8509
8510 /* Reject anything that isn't in a known small-data section. */
8511 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
8512 if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
8513 return false;
8514
8515 /* If a symbol is defined externally, the assembler will use the
8516 usual -G rules when deciding how to implement macros. */
8517 if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
8518 return true;
8519 }
8520 else if (TARGET_EMBEDDED_DATA)
8521 {
8522 /* Don't put constants into the small data section: we want them
8523 to be in ROM rather than RAM. */
8524 if (TREE_CODE (decl) != VAR_DECL)
8525 return false;
8526
8527 if (TREE_READONLY (decl)
8528 && !TREE_SIDE_EFFECTS (decl)
8529 && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
8530 return false;
8531 }
8532
8533 /* Enforce -mlocal-sdata. */
8534 if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
8535 return false;
8536
8537 /* Enforce -mextern-sdata. */
8538 if (!TARGET_EXTERN_SDATA && DECL_P (decl))
8539 {
8540 if (DECL_EXTERNAL (decl))
8541 return false;
8542 if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
8543 return false;
8544 }
8545
8546 /* We have traditionally not treated zero-sized objects as small data,
8547 so this is now effectively part of the ABI. */
8548 size = int_size_in_bytes (TREE_TYPE (decl));
8549 return size > 0 && size <= mips_small_data_threshold;
8550 }
8551
8552 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P. We don't want to use
8553 anchors for small data: the GP register acts as an anchor in that
8554 case. We also don't want to use them for PC-relative accesses,
8555 where the PC acts as an anchor. */
8556
8557 static bool
8558 mips_use_anchors_for_symbol_p (const_rtx symbol)
8559 {
8560 switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
8561 {
8562 case SYMBOL_PC_RELATIVE:
8563 case SYMBOL_GP_RELATIVE:
8564 return false;
8565
8566 default:
8567 return default_use_anchors_for_symbol_p (symbol);
8568 }
8569 }
8570 \f
8571 /* The MIPS debug format wants all automatic variables and arguments
8572 to be in terms of the virtual frame pointer (stack pointer before
8573 any adjustment in the function), while the MIPS 3.0 linker wants
8574 the frame pointer to be the stack pointer after the initial
8575 adjustment. So, we do the adjustment here. The arg pointer (which
8576 is eliminated) points to the virtual frame pointer, while the frame
8577 pointer (which may be eliminated) points to the stack pointer after
8578 the initial adjustments. */
8579
8580 HOST_WIDE_INT
8581 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
8582 {
8583 rtx offset2 = const0_rtx;
8584 rtx reg = eliminate_constant_term (addr, &offset2);
8585
8586 if (offset == 0)
8587 offset = INTVAL (offset2);
8588
8589 if (reg == stack_pointer_rtx
8590 || reg == frame_pointer_rtx
8591 || reg == hard_frame_pointer_rtx)
8592 {
8593 offset -= cfun->machine->frame.total_size;
8594 if (reg == hard_frame_pointer_rtx)
8595 offset += cfun->machine->frame.hard_frame_pointer_offset;
8596 }
8597
8598 return offset;
8599 }
8600 \f
8601 /* Implement ASM_OUTPUT_EXTERNAL. */
8602
8603 void
8604 mips_output_external (FILE *file, tree decl, const char *name)
8605 {
8606 default_elf_asm_output_external (file, decl, name);
8607
8608 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8609 set in order to avoid putting out names that are never really
8610 used. */
8611 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
8612 {
8613 if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
8614 {
8615 /* When using assembler macros, emit .extern directives for
8616 all small-data externs so that the assembler knows how
8617 big they are.
8618
8619 In most cases it would be safe (though pointless) to emit
8620 .externs for other symbols too. One exception is when an
8621 object is within the -G limit but declared by the user to
8622 be in a section other than .sbss or .sdata. */
8623 fputs ("\t.extern\t", file);
8624 assemble_name (file, name);
8625 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
8626 int_size_in_bytes (TREE_TYPE (decl)));
8627 }
8628 }
8629 }
8630
8631 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8632
8633 static void
8634 mips_output_filename (FILE *stream, const char *name)
8635 {
8636 /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
8637 directives. */
8638 if (write_symbols == DWARF2_DEBUG)
8639 return;
8640 else if (mips_output_filename_first_time)
8641 {
8642 mips_output_filename_first_time = 0;
8643 num_source_filenames += 1;
8644 current_function_file = name;
8645 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8646 output_quoted_string (stream, name);
8647 putc ('\n', stream);
8648 }
8649 /* If we are emitting stabs, let dbxout.c handle this (except for
8650 the mips_output_filename_first_time case). */
8651 else if (write_symbols == DBX_DEBUG)
8652 return;
8653 else if (name != current_function_file
8654 && strcmp (name, current_function_file) != 0)
8655 {
8656 num_source_filenames += 1;
8657 current_function_file = name;
8658 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8659 output_quoted_string (stream, name);
8660 putc ('\n', stream);
8661 }
8662 }
8663
8664 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL. */
8665
8666 static void ATTRIBUTE_UNUSED
8667 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
8668 {
8669 switch (size)
8670 {
8671 case 4:
8672 fputs ("\t.dtprelword\t", file);
8673 break;
8674
8675 case 8:
8676 fputs ("\t.dtpreldword\t", file);
8677 break;
8678
8679 default:
8680 gcc_unreachable ();
8681 }
8682 output_addr_const (file, x);
8683 fputs ("+0x8000", file);
8684 }
8685
8686 /* Implement TARGET_DWARF_REGISTER_SPAN. */
8687
8688 static rtx
8689 mips_dwarf_register_span (rtx reg)
8690 {
8691 rtx high, low;
8692 enum machine_mode mode;
8693
8694 /* By default, GCC maps increasing register numbers to increasing
8695 memory locations, but paired FPRs are always little-endian,
8696 regardless of the prevailing endianness. */
8697 mode = GET_MODE (reg);
8698 if (FP_REG_P (REGNO (reg))
8699 && TARGET_BIG_ENDIAN
8700 && MAX_FPRS_PER_FMT > 1
8701 && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
8702 {
8703 gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
8704 high = mips_subword (reg, true);
8705 low = mips_subword (reg, false);
8706 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
8707 }
8708
8709 return NULL_RTX;
8710 }
8711
8712 /* DSP ALU can bypass data with no delays for the following pairs. */
8713 enum insn_code dspalu_bypass_table[][2] =
8714 {
8715 {CODE_FOR_mips_addsc, CODE_FOR_mips_addwc},
8716 {CODE_FOR_mips_cmpu_eq_qb, CODE_FOR_mips_pick_qb},
8717 {CODE_FOR_mips_cmpu_lt_qb, CODE_FOR_mips_pick_qb},
8718 {CODE_FOR_mips_cmpu_le_qb, CODE_FOR_mips_pick_qb},
8719 {CODE_FOR_mips_cmp_eq_ph, CODE_FOR_mips_pick_ph},
8720 {CODE_FOR_mips_cmp_lt_ph, CODE_FOR_mips_pick_ph},
8721 {CODE_FOR_mips_cmp_le_ph, CODE_FOR_mips_pick_ph},
8722 {CODE_FOR_mips_wrdsp, CODE_FOR_mips_insv}
8723 };
8724
8725 int
8726 mips_dspalu_bypass_p (rtx out_insn, rtx in_insn)
8727 {
8728 int i;
8729 int num_bypass = ARRAY_SIZE (dspalu_bypass_table);
8730 enum insn_code out_icode = (enum insn_code) INSN_CODE (out_insn);
8731 enum insn_code in_icode = (enum insn_code) INSN_CODE (in_insn);
8732
8733 for (i = 0; i < num_bypass; i++)
8734 {
8735 if (out_icode == dspalu_bypass_table[i][0]
8736 && in_icode == dspalu_bypass_table[i][1])
8737 return true;
8738 }
8739
8740 return false;
8741 }
8742 /* Implement ASM_OUTPUT_ASCII. */
8743
8744 void
8745 mips_output_ascii (FILE *stream, const char *string, size_t len)
8746 {
8747 size_t i;
8748 int cur_pos;
8749
8750 cur_pos = 17;
8751 fprintf (stream, "\t.ascii\t\"");
8752 for (i = 0; i < len; i++)
8753 {
8754 int c;
8755
8756 c = (unsigned char) string[i];
8757 if (ISPRINT (c))
8758 {
8759 if (c == '\\' || c == '\"')
8760 {
8761 putc ('\\', stream);
8762 cur_pos++;
8763 }
8764 putc (c, stream);
8765 cur_pos++;
8766 }
8767 else
8768 {
8769 fprintf (stream, "\\%03o", c);
8770 cur_pos += 4;
8771 }
8772
8773 if (cur_pos > 72 && i+1 < len)
8774 {
8775 cur_pos = 17;
8776 fprintf (stream, "\"\n\t.ascii\t\"");
8777 }
8778 }
8779 fprintf (stream, "\"\n");
8780 }
8781
8782 /* Return the pseudo-op for full SYMBOL_(D)TPREL address *ADDR.
8783 Update *ADDR with the operand that should be printed. */
8784
8785 const char *
8786 mips_output_tls_reloc_directive (rtx *addr)
8787 {
8788 enum mips_symbol_type type;
8789
8790 type = mips_classify_symbolic_expression (*addr, SYMBOL_CONTEXT_LEA);
8791 *addr = mips_strip_unspec_address (*addr);
8792 switch (type)
8793 {
8794 case SYMBOL_DTPREL:
8795 return Pmode == SImode ? ".dtprelword\t%0" : ".dtpreldword\t%0";
8796
8797 case SYMBOL_TPREL:
8798 return Pmode == SImode ? ".tprelword\t%0" : ".tpreldword\t%0";
8799
8800 default:
8801 gcc_unreachable ();
8802 }
8803 }
8804
8805 /* Emit either a label, .comm, or .lcomm directive. When using assembler
8806 macros, mark the symbol as written so that mips_asm_output_external
8807 won't emit an .extern for it. STREAM is the output file, NAME is the
8808 name of the symbol, INIT_STRING is the string that should be written
8809 before the symbol and FINAL_STRING is the string that should be
8810 written after it. FINAL_STRING is a printf format that consumes the
8811 remaining arguments. */
8812
8813 void
8814 mips_declare_object (FILE *stream, const char *name, const char *init_string,
8815 const char *final_string, ...)
8816 {
8817 va_list ap;
8818
8819 fputs (init_string, stream);
8820 assemble_name (stream, name);
8821 va_start (ap, final_string);
8822 vfprintf (stream, final_string, ap);
8823 va_end (ap);
8824
8825 if (!TARGET_EXPLICIT_RELOCS)
8826 {
8827 tree name_tree = get_identifier (name);
8828 TREE_ASM_WRITTEN (name_tree) = 1;
8829 }
8830 }
8831
8832 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
8833 NAME is the name of the object and ALIGN is the required alignment
8834 in bytes. TAKES_ALIGNMENT_P is true if the directive takes a third
8835 alignment argument. */
8836
8837 void
8838 mips_declare_common_object (FILE *stream, const char *name,
8839 const char *init_string,
8840 unsigned HOST_WIDE_INT size,
8841 unsigned int align, bool takes_alignment_p)
8842 {
8843 if (!takes_alignment_p)
8844 {
8845 size += (align / BITS_PER_UNIT) - 1;
8846 size -= size % (align / BITS_PER_UNIT);
8847 mips_declare_object (stream, name, init_string,
8848 "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
8849 }
8850 else
8851 mips_declare_object (stream, name, init_string,
8852 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
8853 size, align / BITS_PER_UNIT);
8854 }
8855
8856 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON. This is usually the same as the
8857 elfos.h version, but we also need to handle -muninit-const-in-rodata. */
8858
8859 void
8860 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
8861 unsigned HOST_WIDE_INT size,
8862 unsigned int align)
8863 {
8864 /* If the target wants uninitialized const declarations in
8865 .rdata then don't put them in .comm. */
8866 if (TARGET_EMBEDDED_DATA
8867 && TARGET_UNINIT_CONST_IN_RODATA
8868 && TREE_CODE (decl) == VAR_DECL
8869 && TREE_READONLY (decl)
8870 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
8871 {
8872 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
8873 targetm.asm_out.globalize_label (stream, name);
8874
8875 switch_to_section (readonly_data_section);
8876 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
8877 mips_declare_object (stream, name, "",
8878 ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
8879 size);
8880 }
8881 else
8882 mips_declare_common_object (stream, name, "\n\t.comm\t",
8883 size, align, true);
8884 }
8885
8886 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
8887 extern int size_directive_output;
8888
8889 /* Implement ASM_DECLARE_OBJECT_NAME. This is like most of the standard ELF
8890 definitions except that it uses mips_declare_object to emit the label. */
8891
8892 void
8893 mips_declare_object_name (FILE *stream, const char *name,
8894 tree decl ATTRIBUTE_UNUSED)
8895 {
8896 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
8897 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
8898 #endif
8899
8900 size_directive_output = 0;
8901 if (!flag_inhibit_size_directive && DECL_SIZE (decl))
8902 {
8903 HOST_WIDE_INT size;
8904
8905 size_directive_output = 1;
8906 size = int_size_in_bytes (TREE_TYPE (decl));
8907 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8908 }
8909
8910 mips_declare_object (stream, name, "", ":\n");
8911 }
8912
8913 /* Implement ASM_FINISH_DECLARE_OBJECT. This is generic ELF stuff. */
8914
8915 void
8916 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
8917 {
8918 const char *name;
8919
8920 name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8921 if (!flag_inhibit_size_directive
8922 && DECL_SIZE (decl) != 0
8923 && !at_end
8924 && top_level
8925 && DECL_INITIAL (decl) == error_mark_node
8926 && !size_directive_output)
8927 {
8928 HOST_WIDE_INT size;
8929
8930 size_directive_output = 1;
8931 size = int_size_in_bytes (TREE_TYPE (decl));
8932 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8933 }
8934 }
8935 #endif
8936 \f
8937 /* Return the FOO in the name of the ".mdebug.FOO" section associated
8938 with the current ABI. */
8939
8940 static const char *
8941 mips_mdebug_abi_name (void)
8942 {
8943 switch (mips_abi)
8944 {
8945 case ABI_32:
8946 return "abi32";
8947 case ABI_O64:
8948 return "abiO64";
8949 case ABI_N32:
8950 return "abiN32";
8951 case ABI_64:
8952 return "abi64";
8953 case ABI_EABI:
8954 return TARGET_64BIT ? "eabi64" : "eabi32";
8955 default:
8956 gcc_unreachable ();
8957 }
8958 }
8959
8960 /* Implement TARGET_ASM_FILE_START. */
8961
8962 static void
8963 mips_file_start (void)
8964 {
8965 default_file_start ();
8966
8967 /* Generate a special section to describe the ABI switches used to
8968 produce the resultant binary. */
8969
8970 /* Record the ABI itself. Modern versions of binutils encode
8971 this information in the ELF header flags, but GDB needs the
8972 information in order to correctly debug binaries produced by
8973 older binutils. See the function mips_gdbarch_init in
8974 gdb/mips-tdep.c. */
8975 fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
8976 mips_mdebug_abi_name ());
8977
8978 /* There is no ELF header flag to distinguish long32 forms of the
8979 EABI from long64 forms. Emit a special section to help tools
8980 such as GDB. Do the same for o64, which is sometimes used with
8981 -mlong64. */
8982 if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
8983 fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
8984 "\t.previous\n", TARGET_LONG64 ? 64 : 32);
8985
8986 /* Record the NaN encoding. */
8987 if (HAVE_AS_NAN || mips_nan != MIPS_IEEE_754_DEFAULT)
8988 fprintf (asm_out_file, "\t.nan\t%s\n",
8989 mips_nan == MIPS_IEEE_754_2008 ? "2008" : "legacy");
8990
8991 #ifdef HAVE_AS_GNU_ATTRIBUTE
8992 {
8993 int attr;
8994
8995 /* No floating-point operations, -mno-float. */
8996 if (TARGET_NO_FLOAT)
8997 attr = 0;
8998 /* Soft-float code, -msoft-float. */
8999 else if (!TARGET_HARD_FLOAT_ABI)
9000 attr = 3;
9001 /* Single-float code, -msingle-float. */
9002 else if (!TARGET_DOUBLE_FLOAT)
9003 attr = 2;
9004 /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64. */
9005 else if (!TARGET_64BIT && TARGET_FLOAT64)
9006 attr = 4;
9007 /* Regular FP code, FP regs same size as GP regs, -mdouble-float. */
9008 else
9009 attr = 1;
9010
9011 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
9012 }
9013 #endif
9014
9015 /* If TARGET_ABICALLS, tell GAS to generate -KPIC code. */
9016 if (TARGET_ABICALLS)
9017 {
9018 fprintf (asm_out_file, "\t.abicalls\n");
9019 if (TARGET_ABICALLS_PIC0)
9020 fprintf (asm_out_file, "\t.option\tpic0\n");
9021 }
9022
9023 if (flag_verbose_asm)
9024 fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
9025 ASM_COMMENT_START,
9026 mips_small_data_threshold, mips_arch_info->name, mips_isa);
9027 }
9028
9029 /* Implement TARGET_ASM_CODE_END. */
9030
9031 static void
9032 mips_code_end (void)
9033 {
9034 mips_finish_stub (&mips16_rdhwr_stub);
9035 mips_finish_stub (&mips16_get_fcsr_stub);
9036 mips_finish_stub (&mips16_set_fcsr_stub);
9037 }
9038 \f
9039 /* Make the last instruction frame-related and note that it performs
9040 the operation described by FRAME_PATTERN. */
9041
9042 static void
9043 mips_set_frame_expr (rtx frame_pattern)
9044 {
9045 rtx insn;
9046
9047 insn = get_last_insn ();
9048 RTX_FRAME_RELATED_P (insn) = 1;
9049 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
9050 frame_pattern,
9051 REG_NOTES (insn));
9052 }
9053
9054 /* Return a frame-related rtx that stores REG at MEM.
9055 REG must be a single register. */
9056
9057 static rtx
9058 mips_frame_set (rtx mem, rtx reg)
9059 {
9060 rtx set;
9061
9062 set = gen_rtx_SET (VOIDmode, mem, reg);
9063 RTX_FRAME_RELATED_P (set) = 1;
9064
9065 return set;
9066 }
9067
9068 /* Record that the epilogue has restored call-saved register REG. */
9069
9070 static void
9071 mips_add_cfa_restore (rtx reg)
9072 {
9073 mips_epilogue.cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
9074 mips_epilogue.cfa_restores);
9075 }
9076 \f
9077 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
9078 mips16e_s2_s8_regs[X], it must also save the registers in indexes
9079 X + 1 onwards. Likewise mips16e_a0_a3_regs. */
9080 static const unsigned char mips16e_s2_s8_regs[] = {
9081 30, 23, 22, 21, 20, 19, 18
9082 };
9083 static const unsigned char mips16e_a0_a3_regs[] = {
9084 4, 5, 6, 7
9085 };
9086
9087 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
9088 ordered from the uppermost in memory to the lowest in memory. */
9089 static const unsigned char mips16e_save_restore_regs[] = {
9090 31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
9091 };
9092
9093 /* Return the index of the lowest X in the range [0, SIZE) for which
9094 bit REGS[X] is set in MASK. Return SIZE if there is no such X. */
9095
9096 static unsigned int
9097 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
9098 unsigned int size)
9099 {
9100 unsigned int i;
9101
9102 for (i = 0; i < size; i++)
9103 if (BITSET_P (mask, regs[i]))
9104 break;
9105
9106 return i;
9107 }
9108
9109 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
9110 is the number of set bits. If *MASK_PTR contains REGS[X] for some X
9111 in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
9112 is true for all indexes (X, SIZE). */
9113
9114 static void
9115 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
9116 unsigned int size, unsigned int *num_regs_ptr)
9117 {
9118 unsigned int i;
9119
9120 i = mips16e_find_first_register (*mask_ptr, regs, size);
9121 for (i++; i < size; i++)
9122 if (!BITSET_P (*mask_ptr, regs[i]))
9123 {
9124 *num_regs_ptr += 1;
9125 *mask_ptr |= 1 << regs[i];
9126 }
9127 }
9128
9129 /* Return a simplified form of X using the register values in REG_VALUES.
9130 REG_VALUES[R] is the last value assigned to hard register R, or null
9131 if R has not been modified.
9132
9133 This function is rather limited, but is good enough for our purposes. */
9134
9135 static rtx
9136 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
9137 {
9138 x = avoid_constant_pool_reference (x);
9139
9140 if (UNARY_P (x))
9141 {
9142 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9143 return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
9144 x0, GET_MODE (XEXP (x, 0)));
9145 }
9146
9147 if (ARITHMETIC_P (x))
9148 {
9149 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9150 rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
9151 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
9152 }
9153
9154 if (REG_P (x)
9155 && reg_values[REGNO (x)]
9156 && !rtx_unstable_p (reg_values[REGNO (x)]))
9157 return reg_values[REGNO (x)];
9158
9159 return x;
9160 }
9161
9162 /* Return true if (set DEST SRC) stores an argument register into its
9163 caller-allocated save slot, storing the number of that argument
9164 register in *REGNO_PTR if so. REG_VALUES is as for
9165 mips16e_collect_propagate_value. */
9166
9167 static bool
9168 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
9169 unsigned int *regno_ptr)
9170 {
9171 unsigned int argno, regno;
9172 HOST_WIDE_INT offset, required_offset;
9173 rtx addr, base;
9174
9175 /* Check that this is a word-mode store. */
9176 if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
9177 return false;
9178
9179 /* Check that the register being saved is an unmodified argument
9180 register. */
9181 regno = REGNO (src);
9182 if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
9183 return false;
9184 argno = regno - GP_ARG_FIRST;
9185
9186 /* Check whether the address is an appropriate stack-pointer or
9187 frame-pointer access. */
9188 addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
9189 mips_split_plus (addr, &base, &offset);
9190 required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
9191 if (base == hard_frame_pointer_rtx)
9192 required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
9193 else if (base != stack_pointer_rtx)
9194 return false;
9195 if (offset != required_offset)
9196 return false;
9197
9198 *regno_ptr = regno;
9199 return true;
9200 }
9201
9202 /* A subroutine of mips_expand_prologue, called only when generating
9203 MIPS16e SAVE instructions. Search the start of the function for any
9204 instructions that save argument registers into their caller-allocated
9205 save slots. Delete such instructions and return a value N such that
9206 saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
9207 instructions redundant. */
9208
9209 static unsigned int
9210 mips16e_collect_argument_saves (void)
9211 {
9212 rtx reg_values[FIRST_PSEUDO_REGISTER];
9213 rtx insn, next, set, dest, src;
9214 unsigned int nargs, regno;
9215
9216 push_topmost_sequence ();
9217 nargs = 0;
9218 memset (reg_values, 0, sizeof (reg_values));
9219 for (insn = get_insns (); insn; insn = next)
9220 {
9221 next = NEXT_INSN (insn);
9222 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
9223 continue;
9224
9225 if (!INSN_P (insn))
9226 break;
9227
9228 set = PATTERN (insn);
9229 if (GET_CODE (set) != SET)
9230 break;
9231
9232 dest = SET_DEST (set);
9233 src = SET_SRC (set);
9234 if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
9235 {
9236 if (!BITSET_P (cfun->machine->frame.mask, regno))
9237 {
9238 delete_insn (insn);
9239 nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
9240 }
9241 }
9242 else if (REG_P (dest) && GET_MODE (dest) == word_mode)
9243 reg_values[REGNO (dest)]
9244 = mips16e_collect_propagate_value (src, reg_values);
9245 else
9246 break;
9247 }
9248 pop_topmost_sequence ();
9249
9250 return nargs;
9251 }
9252
9253 /* Return a move between register REGNO and memory location SP + OFFSET.
9254 REG_PARM_P is true if SP + OFFSET belongs to REG_PARM_STACK_SPACE.
9255 Make the move a load if RESTORE_P, otherwise make it a store. */
9256
9257 static rtx
9258 mips16e_save_restore_reg (bool restore_p, bool reg_parm_p,
9259 HOST_WIDE_INT offset, unsigned int regno)
9260 {
9261 rtx reg, mem;
9262
9263 mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx,
9264 offset));
9265 reg = gen_rtx_REG (SImode, regno);
9266 if (restore_p)
9267 {
9268 mips_add_cfa_restore (reg);
9269 return gen_rtx_SET (VOIDmode, reg, mem);
9270 }
9271 if (reg_parm_p)
9272 return gen_rtx_SET (VOIDmode, mem, reg);
9273 return mips_frame_set (mem, reg);
9274 }
9275
9276 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
9277 The instruction must:
9278
9279 - Allocate or deallocate SIZE bytes in total; SIZE is known
9280 to be nonzero.
9281
9282 - Save or restore as many registers in *MASK_PTR as possible.
9283 The instruction saves the first registers at the top of the
9284 allocated area, with the other registers below it.
9285
9286 - Save NARGS argument registers above the allocated area.
9287
9288 (NARGS is always zero if RESTORE_P.)
9289
9290 The SAVE and RESTORE instructions cannot save and restore all general
9291 registers, so there may be some registers left over for the caller to
9292 handle. Destructively modify *MASK_PTR so that it contains the registers
9293 that still need to be saved or restored. The caller can save these
9294 registers in the memory immediately below *OFFSET_PTR, which is a
9295 byte offset from the bottom of the allocated stack area. */
9296
9297 static rtx
9298 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
9299 HOST_WIDE_INT *offset_ptr, unsigned int nargs,
9300 HOST_WIDE_INT size)
9301 {
9302 rtx pattern, set;
9303 HOST_WIDE_INT offset, top_offset;
9304 unsigned int i, regno;
9305 int n;
9306
9307 gcc_assert (cfun->machine->frame.num_fp == 0);
9308
9309 /* Calculate the number of elements in the PARALLEL. We need one element
9310 for the stack adjustment, one for each argument register save, and one
9311 for each additional register move. */
9312 n = 1 + nargs;
9313 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9314 if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
9315 n++;
9316
9317 /* Create the final PARALLEL. */
9318 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
9319 n = 0;
9320
9321 /* Add the stack pointer adjustment. */
9322 set = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9323 plus_constant (Pmode, stack_pointer_rtx,
9324 restore_p ? size : -size));
9325 RTX_FRAME_RELATED_P (set) = 1;
9326 XVECEXP (pattern, 0, n++) = set;
9327
9328 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
9329 top_offset = restore_p ? size : 0;
9330
9331 /* Save the arguments. */
9332 for (i = 0; i < nargs; i++)
9333 {
9334 offset = top_offset + i * UNITS_PER_WORD;
9335 set = mips16e_save_restore_reg (restore_p, true, offset,
9336 GP_ARG_FIRST + i);
9337 XVECEXP (pattern, 0, n++) = set;
9338 }
9339
9340 /* Then fill in the other register moves. */
9341 offset = top_offset;
9342 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9343 {
9344 regno = mips16e_save_restore_regs[i];
9345 if (BITSET_P (*mask_ptr, regno))
9346 {
9347 offset -= UNITS_PER_WORD;
9348 set = mips16e_save_restore_reg (restore_p, false, offset, regno);
9349 XVECEXP (pattern, 0, n++) = set;
9350 *mask_ptr &= ~(1 << regno);
9351 }
9352 }
9353
9354 /* Tell the caller what offset it should use for the remaining registers. */
9355 *offset_ptr = size + (offset - top_offset);
9356
9357 gcc_assert (n == XVECLEN (pattern, 0));
9358
9359 return pattern;
9360 }
9361
9362 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
9363 pointer. Return true if PATTERN matches the kind of instruction
9364 generated by mips16e_build_save_restore. If INFO is nonnull,
9365 initialize it when returning true. */
9366
9367 bool
9368 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
9369 struct mips16e_save_restore_info *info)
9370 {
9371 unsigned int i, nargs, mask, extra;
9372 HOST_WIDE_INT top_offset, save_offset, offset;
9373 rtx set, reg, mem, base;
9374 int n;
9375
9376 if (!GENERATE_MIPS16E_SAVE_RESTORE)
9377 return false;
9378
9379 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
9380 top_offset = adjust > 0 ? adjust : 0;
9381
9382 /* Interpret all other members of the PARALLEL. */
9383 save_offset = top_offset - UNITS_PER_WORD;
9384 mask = 0;
9385 nargs = 0;
9386 i = 0;
9387 for (n = 1; n < XVECLEN (pattern, 0); n++)
9388 {
9389 /* Check that we have a SET. */
9390 set = XVECEXP (pattern, 0, n);
9391 if (GET_CODE (set) != SET)
9392 return false;
9393
9394 /* Check that the SET is a load (if restoring) or a store
9395 (if saving). */
9396 mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
9397 if (!MEM_P (mem))
9398 return false;
9399
9400 /* Check that the address is the sum of the stack pointer and a
9401 possibly-zero constant offset. */
9402 mips_split_plus (XEXP (mem, 0), &base, &offset);
9403 if (base != stack_pointer_rtx)
9404 return false;
9405
9406 /* Check that SET's other operand is a register. */
9407 reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
9408 if (!REG_P (reg))
9409 return false;
9410
9411 /* Check for argument saves. */
9412 if (offset == top_offset + nargs * UNITS_PER_WORD
9413 && REGNO (reg) == GP_ARG_FIRST + nargs)
9414 nargs++;
9415 else if (offset == save_offset)
9416 {
9417 while (mips16e_save_restore_regs[i++] != REGNO (reg))
9418 if (i == ARRAY_SIZE (mips16e_save_restore_regs))
9419 return false;
9420
9421 mask |= 1 << REGNO (reg);
9422 save_offset -= UNITS_PER_WORD;
9423 }
9424 else
9425 return false;
9426 }
9427
9428 /* Check that the restrictions on register ranges are met. */
9429 extra = 0;
9430 mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
9431 ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
9432 mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
9433 ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
9434 if (extra != 0)
9435 return false;
9436
9437 /* Make sure that the topmost argument register is not saved twice.
9438 The checks above ensure that the same is then true for the other
9439 argument registers. */
9440 if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
9441 return false;
9442
9443 /* Pass back information, if requested. */
9444 if (info)
9445 {
9446 info->nargs = nargs;
9447 info->mask = mask;
9448 info->size = (adjust > 0 ? adjust : -adjust);
9449 }
9450
9451 return true;
9452 }
9453
9454 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
9455 for the register range [MIN_REG, MAX_REG]. Return a pointer to
9456 the null terminator. */
9457
9458 static char *
9459 mips16e_add_register_range (char *s, unsigned int min_reg,
9460 unsigned int max_reg)
9461 {
9462 if (min_reg != max_reg)
9463 s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
9464 else
9465 s += sprintf (s, ",%s", reg_names[min_reg]);
9466 return s;
9467 }
9468
9469 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
9470 PATTERN and ADJUST are as for mips16e_save_restore_pattern_p. */
9471
9472 const char *
9473 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
9474 {
9475 static char buffer[300];
9476
9477 struct mips16e_save_restore_info info;
9478 unsigned int i, end;
9479 char *s;
9480
9481 /* Parse the pattern. */
9482 if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
9483 gcc_unreachable ();
9484
9485 /* Add the mnemonic. */
9486 s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
9487 s += strlen (s);
9488
9489 /* Save the arguments. */
9490 if (info.nargs > 1)
9491 s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
9492 reg_names[GP_ARG_FIRST + info.nargs - 1]);
9493 else if (info.nargs == 1)
9494 s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
9495
9496 /* Emit the amount of stack space to allocate or deallocate. */
9497 s += sprintf (s, "%d", (int) info.size);
9498
9499 /* Save or restore $16. */
9500 if (BITSET_P (info.mask, 16))
9501 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
9502
9503 /* Save or restore $17. */
9504 if (BITSET_P (info.mask, 17))
9505 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
9506
9507 /* Save or restore registers in the range $s2...$s8, which
9508 mips16e_s2_s8_regs lists in decreasing order. Note that this
9509 is a software register range; the hardware registers are not
9510 numbered consecutively. */
9511 end = ARRAY_SIZE (mips16e_s2_s8_regs);
9512 i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
9513 if (i < end)
9514 s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
9515 mips16e_s2_s8_regs[i]);
9516
9517 /* Save or restore registers in the range $a0...$a3. */
9518 end = ARRAY_SIZE (mips16e_a0_a3_regs);
9519 i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
9520 if (i < end)
9521 s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
9522 mips16e_a0_a3_regs[end - 1]);
9523
9524 /* Save or restore $31. */
9525 if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
9526 s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
9527
9528 return buffer;
9529 }
9530 \f
9531 /* Return true if the current function returns its value in a floating-point
9532 register in MIPS16 mode. */
9533
9534 static bool
9535 mips16_cfun_returns_in_fpr_p (void)
9536 {
9537 tree return_type = DECL_RESULT (current_function_decl);
9538 return (TARGET_MIPS16
9539 && TARGET_HARD_FLOAT_ABI
9540 && !aggregate_value_p (return_type, current_function_decl)
9541 && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
9542 }
9543
9544 /* Return true if predicate PRED is true for at least one instruction.
9545 Cache the result in *CACHE, and assume that the result is true
9546 if *CACHE is already true. */
9547
9548 static bool
9549 mips_find_gp_ref (bool *cache, bool (*pred) (rtx))
9550 {
9551 rtx insn;
9552
9553 if (!*cache)
9554 {
9555 push_topmost_sequence ();
9556 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9557 if (USEFUL_INSN_P (insn) && pred (insn))
9558 {
9559 *cache = true;
9560 break;
9561 }
9562 pop_topmost_sequence ();
9563 }
9564 return *cache;
9565 }
9566
9567 /* Return true if INSN refers to the global pointer in an "inflexible" way.
9568 See mips_cfun_has_inflexible_gp_ref_p for details. */
9569
9570 static bool
9571 mips_insn_has_inflexible_gp_ref_p (rtx insn)
9572 {
9573 /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
9574 indicate that the target could be a traditional MIPS
9575 lazily-binding stub. */
9576 return find_reg_fusage (insn, USE, pic_offset_table_rtx);
9577 }
9578
9579 /* Return true if the current function refers to the global pointer
9580 in a way that forces $28 to be valid. This means that we can't
9581 change the choice of global pointer, even for NewABI code.
9582
9583 One example of this (and one which needs several checks) is that
9584 $28 must be valid when calling traditional MIPS lazy-binding stubs.
9585 (This restriction does not apply to PLTs.) */
9586
9587 static bool
9588 mips_cfun_has_inflexible_gp_ref_p (void)
9589 {
9590 /* If the function has a nonlocal goto, $28 must hold the correct
9591 global pointer for the target function. That is, the target
9592 of the goto implicitly uses $28. */
9593 if (crtl->has_nonlocal_goto)
9594 return true;
9595
9596 if (TARGET_ABICALLS_PIC2)
9597 {
9598 /* Symbolic accesses implicitly use the global pointer unless
9599 -mexplicit-relocs is in effect. JAL macros to symbolic addresses
9600 might go to traditional MIPS lazy-binding stubs. */
9601 if (!TARGET_EXPLICIT_RELOCS)
9602 return true;
9603
9604 /* FUNCTION_PROFILER includes a JAL to _mcount, which again
9605 can be lazily-bound. */
9606 if (crtl->profile)
9607 return true;
9608
9609 /* MIPS16 functions that return in FPRs need to call an
9610 external libgcc routine. This call is only made explict
9611 during mips_expand_epilogue, and it too might be lazily bound. */
9612 if (mips16_cfun_returns_in_fpr_p ())
9613 return true;
9614 }
9615
9616 return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
9617 mips_insn_has_inflexible_gp_ref_p);
9618 }
9619
9620 /* Return true if INSN refers to the global pointer in a "flexible" way.
9621 See mips_cfun_has_flexible_gp_ref_p for details. */
9622
9623 static bool
9624 mips_insn_has_flexible_gp_ref_p (rtx insn)
9625 {
9626 return (get_attr_got (insn) != GOT_UNSET
9627 || mips_small_data_pattern_p (PATTERN (insn))
9628 || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
9629 }
9630
9631 /* Return true if the current function references the global pointer,
9632 but if those references do not inherently require the global pointer
9633 to be $28. Assume !mips_cfun_has_inflexible_gp_ref_p (). */
9634
9635 static bool
9636 mips_cfun_has_flexible_gp_ref_p (void)
9637 {
9638 /* Reload can sometimes introduce constant pool references
9639 into a function that otherwise didn't need them. For example,
9640 suppose we have an instruction like:
9641
9642 (set (reg:DF R1) (float:DF (reg:SI R2)))
9643
9644 If R2 turns out to be a constant such as 1, the instruction may
9645 have a REG_EQUAL note saying that R1 == 1.0. Reload then has
9646 the option of using this constant if R2 doesn't get allocated
9647 to a register.
9648
9649 In cases like these, reload will have added the constant to the
9650 pool but no instruction will yet refer to it. */
9651 if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
9652 return true;
9653
9654 return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
9655 mips_insn_has_flexible_gp_ref_p);
9656 }
9657
9658 /* Return the register that should be used as the global pointer
9659 within this function. Return INVALID_REGNUM if the function
9660 doesn't need a global pointer. */
9661
9662 static unsigned int
9663 mips_global_pointer (void)
9664 {
9665 unsigned int regno;
9666
9667 /* $gp is always available unless we're using a GOT. */
9668 if (!TARGET_USE_GOT)
9669 return GLOBAL_POINTER_REGNUM;
9670
9671 /* If there are inflexible references to $gp, we must use the
9672 standard register. */
9673 if (mips_cfun_has_inflexible_gp_ref_p ())
9674 return GLOBAL_POINTER_REGNUM;
9675
9676 /* If there are no current references to $gp, then the only uses
9677 we can introduce later are those involved in long branches. */
9678 if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
9679 return INVALID_REGNUM;
9680
9681 /* If the global pointer is call-saved, try to use a call-clobbered
9682 alternative. */
9683 if (TARGET_CALL_SAVED_GP && crtl->is_leaf)
9684 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9685 if (!df_regs_ever_live_p (regno)
9686 && call_really_used_regs[regno]
9687 && !fixed_regs[regno]
9688 && regno != PIC_FUNCTION_ADDR_REGNUM)
9689 return regno;
9690
9691 return GLOBAL_POINTER_REGNUM;
9692 }
9693
9694 /* Return true if the current function's prologue must load the global
9695 pointer value into pic_offset_table_rtx and store the same value in
9696 the function's cprestore slot (if any).
9697
9698 One problem we have to deal with is that, when emitting GOT-based
9699 position independent code, long-branch sequences will need to load
9700 the address of the branch target from the GOT. We don't know until
9701 the very end of compilation whether (and where) the function needs
9702 long branches, so we must ensure that _any_ branch can access the
9703 global pointer in some form. However, we do not want to pessimize
9704 the usual case in which all branches are short.
9705
9706 We handle this as follows:
9707
9708 (1) During reload, we set cfun->machine->global_pointer to
9709 INVALID_REGNUM if we _know_ that the current function
9710 doesn't need a global pointer. This is only valid if
9711 long branches don't need the GOT.
9712
9713 Otherwise, we assume that we might need a global pointer
9714 and pick an appropriate register.
9715
9716 (2) If cfun->machine->global_pointer != INVALID_REGNUM,
9717 we ensure that the global pointer is available at every
9718 block boundary bar entry and exit. We do this in one of two ways:
9719
9720 - If the function has a cprestore slot, we ensure that this
9721 slot is valid at every branch. However, as explained in
9722 point (6) below, there is no guarantee that pic_offset_table_rtx
9723 itself is valid if new uses of the global pointer are introduced
9724 after the first post-epilogue split.
9725
9726 We guarantee that the cprestore slot is valid by loading it
9727 into a fake register, CPRESTORE_SLOT_REGNUM. We then make
9728 this register live at every block boundary bar function entry
9729 and exit. It is then invalid to move the load (and thus the
9730 preceding store) across a block boundary.
9731
9732 - If the function has no cprestore slot, we guarantee that
9733 pic_offset_table_rtx itself is valid at every branch.
9734
9735 See mips_eh_uses for the handling of the register liveness.
9736
9737 (3) During prologue and epilogue generation, we emit "ghost"
9738 placeholder instructions to manipulate the global pointer.
9739
9740 (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
9741 and cfun->machine->must_restore_gp_when_clobbered_p if we already know
9742 that the function needs a global pointer. (There is no need to set
9743 them earlier than this, and doing it as late as possible leads to
9744 fewer false positives.)
9745
9746 (5) If cfun->machine->must_initialize_gp_p is true during a
9747 split_insns pass, we split the ghost instructions into real
9748 instructions. These split instructions can then be optimized in
9749 the usual way. Otherwise, we keep the ghost instructions intact,
9750 and optimize for the case where they aren't needed. We still
9751 have the option of splitting them later, if we need to introduce
9752 new uses of the global pointer.
9753
9754 For example, the scheduler ignores a ghost instruction that
9755 stores $28 to the stack, but it handles the split form of
9756 the ghost instruction as an ordinary store.
9757
9758 (6) [OldABI only.] If cfun->machine->must_restore_gp_when_clobbered_p
9759 is true during the first post-epilogue split_insns pass, we split
9760 calls and restore_gp patterns into instructions that explicitly
9761 load pic_offset_table_rtx from the cprestore slot. Otherwise,
9762 we split these patterns into instructions that _don't_ load from
9763 the cprestore slot.
9764
9765 If cfun->machine->must_restore_gp_when_clobbered_p is true at the
9766 time of the split, then any instructions that exist at that time
9767 can make free use of pic_offset_table_rtx. However, if we want
9768 to introduce new uses of the global pointer after the split,
9769 we must explicitly load the value from the cprestore slot, since
9770 pic_offset_table_rtx itself might not be valid at a given point
9771 in the function.
9772
9773 The idea is that we want to be able to delete redundant
9774 loads from the cprestore slot in the usual case where no
9775 long branches are needed.
9776
9777 (7) If cfun->machine->must_initialize_gp_p is still false at the end
9778 of md_reorg, we decide whether the global pointer is needed for
9779 long branches. If so, we set cfun->machine->must_initialize_gp_p
9780 to true and split the ghost instructions into real instructions
9781 at that stage.
9782
9783 Note that the ghost instructions must have a zero length for three reasons:
9784
9785 - Giving the length of the underlying $gp sequence might cause
9786 us to use long branches in cases where they aren't really needed.
9787
9788 - They would perturb things like alignment calculations.
9789
9790 - More importantly, the hazard detection in md_reorg relies on
9791 empty instructions having a zero length.
9792
9793 If we find a long branch and split the ghost instructions at the
9794 end of md_reorg, the split could introduce more long branches.
9795 That isn't a problem though, because we still do the split before
9796 the final shorten_branches pass.
9797
9798 This is extremely ugly, but it seems like the best compromise between
9799 correctness and efficiency. */
9800
9801 bool
9802 mips_must_initialize_gp_p (void)
9803 {
9804 return cfun->machine->must_initialize_gp_p;
9805 }
9806
9807 /* Return true if REGNO is a register that is ordinarily call-clobbered
9808 but must nevertheless be preserved by an interrupt handler. */
9809
9810 static bool
9811 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
9812 {
9813 if (MD_REG_P (regno))
9814 return true;
9815
9816 if (TARGET_DSP && DSP_ACC_REG_P (regno))
9817 return true;
9818
9819 if (GP_REG_P (regno) && !cfun->machine->use_shadow_register_set_p)
9820 {
9821 /* $0 is hard-wired. */
9822 if (regno == GP_REG_FIRST)
9823 return false;
9824
9825 /* The interrupt handler can treat kernel registers as
9826 scratch registers. */
9827 if (KERNEL_REG_P (regno))
9828 return false;
9829
9830 /* The function will return the stack pointer to its original value
9831 anyway. */
9832 if (regno == STACK_POINTER_REGNUM)
9833 return false;
9834
9835 /* Otherwise, return true for registers that aren't ordinarily
9836 call-clobbered. */
9837 return call_really_used_regs[regno];
9838 }
9839
9840 return false;
9841 }
9842
9843 /* Return true if the current function should treat register REGNO
9844 as call-saved. */
9845
9846 static bool
9847 mips_cfun_call_saved_reg_p (unsigned int regno)
9848 {
9849 /* If the user makes an ordinarily-call-saved register global,
9850 that register is no longer call-saved. */
9851 if (global_regs[regno])
9852 return false;
9853
9854 /* Interrupt handlers need to save extra registers. */
9855 if (cfun->machine->interrupt_handler_p
9856 && mips_interrupt_extra_call_saved_reg_p (regno))
9857 return true;
9858
9859 /* call_insns preserve $28 unless they explicitly say otherwise,
9860 so call_really_used_regs[] treats $28 as call-saved. However,
9861 we want the ABI property rather than the default call_insn
9862 property here. */
9863 return (regno == GLOBAL_POINTER_REGNUM
9864 ? TARGET_CALL_SAVED_GP
9865 : !call_really_used_regs[regno]);
9866 }
9867
9868 /* Return true if the function body might clobber register REGNO.
9869 We know that REGNO is call-saved. */
9870
9871 static bool
9872 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
9873 {
9874 /* Some functions should be treated as clobbering all call-saved
9875 registers. */
9876 if (crtl->saves_all_registers)
9877 return true;
9878
9879 /* DF handles cases where a register is explicitly referenced in
9880 the rtl. Incoming values are passed in call-clobbered registers,
9881 so we can assume that any live call-saved register is set within
9882 the function. */
9883 if (df_regs_ever_live_p (regno))
9884 return true;
9885
9886 /* Check for registers that are clobbered by FUNCTION_PROFILER.
9887 These clobbers are not explicit in the rtl. */
9888 if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
9889 return true;
9890
9891 /* If we're using a call-saved global pointer, the function's
9892 prologue will need to set it up. */
9893 if (cfun->machine->global_pointer == regno)
9894 return true;
9895
9896 /* The function's prologue will need to set the frame pointer if
9897 frame_pointer_needed. */
9898 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
9899 return true;
9900
9901 /* If a MIPS16 function returns a value in FPRs, its epilogue
9902 will need to call an external libgcc routine. This yet-to-be
9903 generated call_insn will clobber $31. */
9904 if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
9905 return true;
9906
9907 /* If REGNO is ordinarily call-clobbered, we must assume that any
9908 called function could modify it. */
9909 if (cfun->machine->interrupt_handler_p
9910 && !crtl->is_leaf
9911 && mips_interrupt_extra_call_saved_reg_p (regno))
9912 return true;
9913
9914 return false;
9915 }
9916
9917 /* Return true if the current function must save register REGNO. */
9918
9919 static bool
9920 mips_save_reg_p (unsigned int regno)
9921 {
9922 if (mips_cfun_call_saved_reg_p (regno))
9923 {
9924 if (mips_cfun_might_clobber_call_saved_reg_p (regno))
9925 return true;
9926
9927 /* Save both registers in an FPR pair if either one is used. This is
9928 needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
9929 register to be used without the even register. */
9930 if (FP_REG_P (regno)
9931 && MAX_FPRS_PER_FMT == 2
9932 && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
9933 return true;
9934 }
9935
9936 /* We need to save the incoming return address if __builtin_eh_return
9937 is being used to set a different return address. */
9938 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
9939 return true;
9940
9941 return false;
9942 }
9943
9944 /* Populate the current function's mips_frame_info structure.
9945
9946 MIPS stack frames look like:
9947
9948 +-------------------------------+
9949 | |
9950 | incoming stack arguments |
9951 | |
9952 +-------------------------------+
9953 | |
9954 | caller-allocated save area |
9955 A | for register arguments |
9956 | |
9957 +-------------------------------+ <-- incoming stack pointer
9958 | |
9959 | callee-allocated save area |
9960 B | for arguments that are |
9961 | split between registers and |
9962 | the stack |
9963 | |
9964 +-------------------------------+ <-- arg_pointer_rtx
9965 | |
9966 C | callee-allocated save area |
9967 | for register varargs |
9968 | |
9969 +-------------------------------+ <-- frame_pointer_rtx
9970 | | + cop0_sp_offset
9971 | COP0 reg save area | + UNITS_PER_WORD
9972 | |
9973 +-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
9974 | | + UNITS_PER_WORD
9975 | accumulator save area |
9976 | |
9977 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
9978 | | + UNITS_PER_HWFPVALUE
9979 | FPR save area |
9980 | |
9981 +-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
9982 | | + UNITS_PER_WORD
9983 | GPR save area |
9984 | |
9985 +-------------------------------+ <-- frame_pointer_rtx with
9986 | | \ -fstack-protector
9987 | local variables | | var_size
9988 | | /
9989 +-------------------------------+
9990 | | \
9991 | $gp save area | | cprestore_size
9992 | | /
9993 P +-------------------------------+ <-- hard_frame_pointer_rtx for
9994 | | \ MIPS16 code
9995 | outgoing stack arguments | |
9996 | | |
9997 +-------------------------------+ | args_size
9998 | | |
9999 | caller-allocated save area | |
10000 | for register arguments | |
10001 | | /
10002 +-------------------------------+ <-- stack_pointer_rtx
10003 frame_pointer_rtx without
10004 -fstack-protector
10005 hard_frame_pointer_rtx for
10006 non-MIPS16 code.
10007
10008 At least two of A, B and C will be empty.
10009
10010 Dynamic stack allocations such as alloca insert data at point P.
10011 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
10012 hard_frame_pointer_rtx unchanged. */
10013
10014 static void
10015 mips_compute_frame_info (void)
10016 {
10017 struct mips_frame_info *frame;
10018 HOST_WIDE_INT offset, size;
10019 unsigned int regno, i;
10020
10021 /* Set this function's interrupt properties. */
10022 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
10023 {
10024 if (!ISA_MIPS32R2)
10025 error ("the %<interrupt%> attribute requires a MIPS32r2 processor");
10026 else if (TARGET_HARD_FLOAT)
10027 error ("the %<interrupt%> attribute requires %<-msoft-float%>");
10028 else if (TARGET_MIPS16)
10029 error ("interrupt handlers cannot be MIPS16 functions");
10030 else
10031 {
10032 cfun->machine->interrupt_handler_p = true;
10033 cfun->machine->use_shadow_register_set_p =
10034 mips_use_shadow_register_set_p (TREE_TYPE (current_function_decl));
10035 cfun->machine->keep_interrupts_masked_p =
10036 mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
10037 cfun->machine->use_debug_exception_return_p =
10038 mips_use_debug_exception_return_p (TREE_TYPE
10039 (current_function_decl));
10040 }
10041 }
10042
10043 frame = &cfun->machine->frame;
10044 memset (frame, 0, sizeof (*frame));
10045 size = get_frame_size ();
10046
10047 cfun->machine->global_pointer = mips_global_pointer ();
10048
10049 /* The first two blocks contain the outgoing argument area and the $gp save
10050 slot. This area isn't needed in leaf functions, but if the
10051 target-independent frame size is nonzero, we have already committed to
10052 allocating these in STARTING_FRAME_OFFSET for !FRAME_GROWS_DOWNWARD. */
10053 if ((size == 0 || FRAME_GROWS_DOWNWARD) && crtl->is_leaf)
10054 {
10055 /* The MIPS 3.0 linker does not like functions that dynamically
10056 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
10057 looks like we are trying to create a second frame pointer to the
10058 function, so allocate some stack space to make it happy. */
10059 if (cfun->calls_alloca)
10060 frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
10061 else
10062 frame->args_size = 0;
10063 frame->cprestore_size = 0;
10064 }
10065 else
10066 {
10067 frame->args_size = crtl->outgoing_args_size;
10068 frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
10069 }
10070 offset = frame->args_size + frame->cprestore_size;
10071
10072 /* Move above the local variables. */
10073 frame->var_size = MIPS_STACK_ALIGN (size);
10074 offset += frame->var_size;
10075
10076 /* Find out which GPRs we need to save. */
10077 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
10078 if (mips_save_reg_p (regno))
10079 {
10080 frame->num_gp++;
10081 frame->mask |= 1 << (regno - GP_REG_FIRST);
10082 }
10083
10084 /* If this function calls eh_return, we must also save and restore the
10085 EH data registers. */
10086 if (crtl->calls_eh_return)
10087 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
10088 {
10089 frame->num_gp++;
10090 frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
10091 }
10092
10093 /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
10094 $a3-$a0 and $s2-$s8. If we save one register in the range, we must
10095 save all later registers too. */
10096 if (GENERATE_MIPS16E_SAVE_RESTORE)
10097 {
10098 mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
10099 ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
10100 mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
10101 ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
10102 }
10103
10104 /* Move above the GPR save area. */
10105 if (frame->num_gp > 0)
10106 {
10107 offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
10108 frame->gp_sp_offset = offset - UNITS_PER_WORD;
10109 }
10110
10111 /* Find out which FPRs we need to save. This loop must iterate over
10112 the same space as its companion in mips_for_each_saved_gpr_and_fpr. */
10113 if (TARGET_HARD_FLOAT)
10114 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
10115 if (mips_save_reg_p (regno))
10116 {
10117 frame->num_fp += MAX_FPRS_PER_FMT;
10118 frame->fmask |= ~(~0 << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
10119 }
10120
10121 /* Move above the FPR save area. */
10122 if (frame->num_fp > 0)
10123 {
10124 offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
10125 frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
10126 }
10127
10128 /* Add in space for the interrupt context information. */
10129 if (cfun->machine->interrupt_handler_p)
10130 {
10131 /* Check HI/LO. */
10132 if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
10133 {
10134 frame->num_acc++;
10135 frame->acc_mask |= (1 << 0);
10136 }
10137
10138 /* Check accumulators 1, 2, 3. */
10139 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
10140 if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
10141 {
10142 frame->num_acc++;
10143 frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
10144 }
10145
10146 /* All interrupt context functions need space to preserve STATUS. */
10147 frame->num_cop0_regs++;
10148
10149 /* If we don't keep interrupts masked, we need to save EPC. */
10150 if (!cfun->machine->keep_interrupts_masked_p)
10151 frame->num_cop0_regs++;
10152 }
10153
10154 /* Move above the accumulator save area. */
10155 if (frame->num_acc > 0)
10156 {
10157 /* Each accumulator needs 2 words. */
10158 offset += frame->num_acc * 2 * UNITS_PER_WORD;
10159 frame->acc_sp_offset = offset - UNITS_PER_WORD;
10160 }
10161
10162 /* Move above the COP0 register save area. */
10163 if (frame->num_cop0_regs > 0)
10164 {
10165 offset += frame->num_cop0_regs * UNITS_PER_WORD;
10166 frame->cop0_sp_offset = offset - UNITS_PER_WORD;
10167 }
10168
10169 /* Move above the callee-allocated varargs save area. */
10170 offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
10171 frame->arg_pointer_offset = offset;
10172
10173 /* Move above the callee-allocated area for pretend stack arguments. */
10174 offset += crtl->args.pretend_args_size;
10175 frame->total_size = offset;
10176
10177 /* Work out the offsets of the save areas from the top of the frame. */
10178 if (frame->gp_sp_offset > 0)
10179 frame->gp_save_offset = frame->gp_sp_offset - offset;
10180 if (frame->fp_sp_offset > 0)
10181 frame->fp_save_offset = frame->fp_sp_offset - offset;
10182 if (frame->acc_sp_offset > 0)
10183 frame->acc_save_offset = frame->acc_sp_offset - offset;
10184 if (frame->num_cop0_regs > 0)
10185 frame->cop0_save_offset = frame->cop0_sp_offset - offset;
10186
10187 /* MIPS16 code offsets the frame pointer by the size of the outgoing
10188 arguments. This tends to increase the chances of using unextended
10189 instructions for local variables and incoming arguments. */
10190 if (TARGET_MIPS16)
10191 frame->hard_frame_pointer_offset = frame->args_size;
10192 }
10193
10194 /* Return the style of GP load sequence that is being used for the
10195 current function. */
10196
10197 enum mips_loadgp_style
10198 mips_current_loadgp_style (void)
10199 {
10200 if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
10201 return LOADGP_NONE;
10202
10203 if (TARGET_RTP_PIC)
10204 return LOADGP_RTP;
10205
10206 if (TARGET_ABSOLUTE_ABICALLS)
10207 return LOADGP_ABSOLUTE;
10208
10209 return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
10210 }
10211
10212 /* Implement TARGET_FRAME_POINTER_REQUIRED. */
10213
10214 static bool
10215 mips_frame_pointer_required (void)
10216 {
10217 /* If the function contains dynamic stack allocations, we need to
10218 use the frame pointer to access the static parts of the frame. */
10219 if (cfun->calls_alloca)
10220 return true;
10221
10222 /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
10223 reload may be unable to compute the address of a local variable,
10224 since there is no way to add a large constant to the stack pointer
10225 without using a second temporary register. */
10226 if (TARGET_MIPS16)
10227 {
10228 mips_compute_frame_info ();
10229 if (!SMALL_OPERAND (cfun->machine->frame.total_size))
10230 return true;
10231 }
10232
10233 return false;
10234 }
10235
10236 /* Make sure that we're not trying to eliminate to the wrong hard frame
10237 pointer. */
10238
10239 static bool
10240 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
10241 {
10242 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
10243 }
10244
10245 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
10246 or argument pointer. TO is either the stack pointer or hard frame
10247 pointer. */
10248
10249 HOST_WIDE_INT
10250 mips_initial_elimination_offset (int from, int to)
10251 {
10252 HOST_WIDE_INT offset;
10253
10254 mips_compute_frame_info ();
10255
10256 /* Set OFFSET to the offset from the end-of-prologue stack pointer. */
10257 switch (from)
10258 {
10259 case FRAME_POINTER_REGNUM:
10260 if (FRAME_GROWS_DOWNWARD)
10261 offset = (cfun->machine->frame.args_size
10262 + cfun->machine->frame.cprestore_size
10263 + cfun->machine->frame.var_size);
10264 else
10265 offset = 0;
10266 break;
10267
10268 case ARG_POINTER_REGNUM:
10269 offset = cfun->machine->frame.arg_pointer_offset;
10270 break;
10271
10272 default:
10273 gcc_unreachable ();
10274 }
10275
10276 if (to == HARD_FRAME_POINTER_REGNUM)
10277 offset -= cfun->machine->frame.hard_frame_pointer_offset;
10278
10279 return offset;
10280 }
10281 \f
10282 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY. */
10283
10284 static void
10285 mips_extra_live_on_entry (bitmap regs)
10286 {
10287 if (TARGET_USE_GOT)
10288 {
10289 /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
10290 the global pointer. */
10291 if (!TARGET_ABSOLUTE_ABICALLS)
10292 bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
10293
10294 /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
10295 the global pointer. */
10296 if (TARGET_MIPS16)
10297 bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
10298
10299 /* See the comment above load_call<mode> for details. */
10300 bitmap_set_bit (regs, GOT_VERSION_REGNUM);
10301 }
10302 }
10303
10304 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
10305 previous frame. */
10306
10307 rtx
10308 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
10309 {
10310 if (count != 0)
10311 return const0_rtx;
10312
10313 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
10314 }
10315
10316 /* Emit code to change the current function's return address to
10317 ADDRESS. SCRATCH is available as a scratch register, if needed.
10318 ADDRESS and SCRATCH are both word-mode GPRs. */
10319
10320 void
10321 mips_set_return_address (rtx address, rtx scratch)
10322 {
10323 rtx slot_address;
10324
10325 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
10326 slot_address = mips_add_offset (scratch, stack_pointer_rtx,
10327 cfun->machine->frame.gp_sp_offset);
10328 mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
10329 }
10330
10331 /* Return true if the current function has a cprestore slot. */
10332
10333 bool
10334 mips_cfun_has_cprestore_slot_p (void)
10335 {
10336 return (cfun->machine->global_pointer != INVALID_REGNUM
10337 && cfun->machine->frame.cprestore_size > 0);
10338 }
10339
10340 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
10341 cprestore slot. LOAD_P is true if the caller wants to load from
10342 the cprestore slot; it is false if the caller wants to store to
10343 the slot. */
10344
10345 static void
10346 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
10347 bool load_p)
10348 {
10349 const struct mips_frame_info *frame;
10350
10351 frame = &cfun->machine->frame;
10352 /* .cprestore always uses the stack pointer instead of the frame pointer.
10353 We have a free choice for direct stores for non-MIPS16 functions,
10354 and for MIPS16 functions whose cprestore slot is in range of the
10355 stack pointer. Using the stack pointer would sometimes give more
10356 (early) scheduling freedom, but using the frame pointer would
10357 sometimes give more (late) scheduling freedom. It's hard to
10358 predict which applies to a given function, so let's keep things
10359 simple.
10360
10361 Loads must always use the frame pointer in functions that call
10362 alloca, and there's little benefit to using the stack pointer
10363 otherwise. */
10364 if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
10365 {
10366 *base = hard_frame_pointer_rtx;
10367 *offset = frame->args_size - frame->hard_frame_pointer_offset;
10368 }
10369 else
10370 {
10371 *base = stack_pointer_rtx;
10372 *offset = frame->args_size;
10373 }
10374 }
10375
10376 /* Return true if X is the load or store address of the cprestore slot;
10377 LOAD_P says which. */
10378
10379 bool
10380 mips_cprestore_address_p (rtx x, bool load_p)
10381 {
10382 rtx given_base, required_base;
10383 HOST_WIDE_INT given_offset, required_offset;
10384
10385 mips_split_plus (x, &given_base, &given_offset);
10386 mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
10387 return given_base == required_base && given_offset == required_offset;
10388 }
10389
10390 /* Return a MEM rtx for the cprestore slot. LOAD_P is true if we are
10391 going to load from it, false if we are going to store to it.
10392 Use TEMP as a temporary register if need be. */
10393
10394 static rtx
10395 mips_cprestore_slot (rtx temp, bool load_p)
10396 {
10397 rtx base;
10398 HOST_WIDE_INT offset;
10399
10400 mips_get_cprestore_base_and_offset (&base, &offset, load_p);
10401 return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
10402 }
10403
10404 /* Emit instructions to save global pointer value GP into cprestore
10405 slot MEM. OFFSET is the offset that MEM applies to the base register.
10406
10407 MEM may not be a legitimate address. If it isn't, TEMP is a
10408 temporary register that can be used, otherwise it is a SCRATCH. */
10409
10410 void
10411 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
10412 {
10413 if (TARGET_CPRESTORE_DIRECTIVE)
10414 {
10415 gcc_assert (gp == pic_offset_table_rtx);
10416 emit_insn (PMODE_INSN (gen_cprestore, (mem, offset)));
10417 }
10418 else
10419 mips_emit_move (mips_cprestore_slot (temp, false), gp);
10420 }
10421
10422 /* Restore $gp from its save slot, using TEMP as a temporary base register
10423 if need be. This function is for o32 and o64 abicalls only.
10424
10425 See mips_must_initialize_gp_p for details about how we manage the
10426 global pointer. */
10427
10428 void
10429 mips_restore_gp_from_cprestore_slot (rtx temp)
10430 {
10431 gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
10432
10433 if (!cfun->machine->must_restore_gp_when_clobbered_p)
10434 {
10435 emit_note (NOTE_INSN_DELETED);
10436 return;
10437 }
10438
10439 if (TARGET_MIPS16)
10440 {
10441 mips_emit_move (temp, mips_cprestore_slot (temp, true));
10442 mips_emit_move (pic_offset_table_rtx, temp);
10443 }
10444 else
10445 mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
10446 if (!TARGET_EXPLICIT_RELOCS)
10447 emit_insn (gen_blockage ());
10448 }
10449 \f
10450 /* A function to save or store a register. The first argument is the
10451 register and the second is the stack slot. */
10452 typedef void (*mips_save_restore_fn) (rtx, rtx);
10453
10454 /* Use FN to save or restore register REGNO. MODE is the register's
10455 mode and OFFSET is the offset of its save slot from the current
10456 stack pointer. */
10457
10458 static void
10459 mips_save_restore_reg (enum machine_mode mode, int regno,
10460 HOST_WIDE_INT offset, mips_save_restore_fn fn)
10461 {
10462 rtx mem;
10463
10464 mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx,
10465 offset));
10466 fn (gen_rtx_REG (mode, regno), mem);
10467 }
10468
10469 /* Call FN for each accumlator that is saved by the current function.
10470 SP_OFFSET is the offset of the current stack pointer from the start
10471 of the frame. */
10472
10473 static void
10474 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
10475 {
10476 HOST_WIDE_INT offset;
10477 int regno;
10478
10479 offset = cfun->machine->frame.acc_sp_offset - sp_offset;
10480 if (BITSET_P (cfun->machine->frame.acc_mask, 0))
10481 {
10482 mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
10483 offset -= UNITS_PER_WORD;
10484 mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
10485 offset -= UNITS_PER_WORD;
10486 }
10487
10488 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
10489 if (BITSET_P (cfun->machine->frame.acc_mask,
10490 ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
10491 {
10492 mips_save_restore_reg (word_mode, regno, offset, fn);
10493 offset -= UNITS_PER_WORD;
10494 }
10495 }
10496
10497 /* Save register REG to MEM. Make the instruction frame-related. */
10498
10499 static void
10500 mips_save_reg (rtx reg, rtx mem)
10501 {
10502 if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
10503 {
10504 rtx x1, x2;
10505
10506 mips_emit_move_or_split (mem, reg, SPLIT_IF_NECESSARY);
10507
10508 x1 = mips_frame_set (mips_subword (mem, false),
10509 mips_subword (reg, false));
10510 x2 = mips_frame_set (mips_subword (mem, true),
10511 mips_subword (reg, true));
10512 mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
10513 }
10514 else
10515 mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
10516 }
10517
10518 /* Capture the register combinations that are allowed in a SWM or LWM
10519 instruction. The entries are ordered by number of registers set in
10520 the mask. We also ignore the single register encodings because a
10521 normal SW/LW is preferred. */
10522
10523 static const unsigned int umips_swm_mask[17] = {
10524 0xc0ff0000, 0x80ff0000, 0x40ff0000, 0x807f0000,
10525 0x00ff0000, 0x803f0000, 0x007f0000, 0x801f0000,
10526 0x003f0000, 0x800f0000, 0x001f0000, 0x80070000,
10527 0x000f0000, 0x80030000, 0x00070000, 0x80010000,
10528 0x00030000
10529 };
10530
10531 static const unsigned int umips_swm_encoding[17] = {
10532 25, 24, 9, 23, 8, 22, 7, 21, 6, 20, 5, 19, 4, 18, 3, 17, 2
10533 };
10534
10535 /* Try to use a microMIPS LWM or SWM instruction to save or restore
10536 as many GPRs in *MASK as possible. *OFFSET is the offset from the
10537 stack pointer of the topmost save slot.
10538
10539 Remove from *MASK all registers that were handled using LWM and SWM.
10540 Update *OFFSET so that it points to the first unused save slot. */
10541
10542 static bool
10543 umips_build_save_restore (mips_save_restore_fn fn,
10544 unsigned *mask, HOST_WIDE_INT *offset)
10545 {
10546 int nregs;
10547 unsigned int i, j;
10548 rtx pattern, set, reg, mem;
10549 HOST_WIDE_INT this_offset;
10550 rtx this_base;
10551
10552 /* Try matching $16 to $31 (s0 to ra). */
10553 for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
10554 if ((*mask & 0xffff0000) == umips_swm_mask[i])
10555 break;
10556
10557 if (i == ARRAY_SIZE (umips_swm_mask))
10558 return false;
10559
10560 /* Get the offset of the lowest save slot. */
10561 nregs = (umips_swm_encoding[i] & 0xf) + (umips_swm_encoding[i] >> 4);
10562 this_offset = *offset - UNITS_PER_WORD * (nregs - 1);
10563
10564 /* LWM/SWM can only support offsets from -2048 to 2047. */
10565 if (!UMIPS_12BIT_OFFSET_P (this_offset))
10566 return false;
10567
10568 /* Create the final PARALLEL. */
10569 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
10570 this_base = stack_pointer_rtx;
10571
10572 /* For registers $16-$23 and $30. */
10573 for (j = 0; j < (umips_swm_encoding[i] & 0xf); j++)
10574 {
10575 HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10576 mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10577 unsigned int regno = (j != 8) ? 16 + j : 30;
10578 *mask &= ~(1 << regno);
10579 reg = gen_rtx_REG (SImode, regno);
10580 if (fn == mips_save_reg)
10581 set = mips_frame_set (mem, reg);
10582 else
10583 {
10584 set = gen_rtx_SET (VOIDmode, reg, mem);
10585 mips_add_cfa_restore (reg);
10586 }
10587 XVECEXP (pattern, 0, j) = set;
10588 }
10589
10590 /* For register $31. */
10591 if (umips_swm_encoding[i] >> 4)
10592 {
10593 HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10594 *mask &= ~(1 << 31);
10595 mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10596 reg = gen_rtx_REG (SImode, 31);
10597 if (fn == mips_save_reg)
10598 set = mips_frame_set (mem, reg);
10599 else
10600 {
10601 set = gen_rtx_SET (VOIDmode, reg, mem);
10602 mips_add_cfa_restore (reg);
10603 }
10604 XVECEXP (pattern, 0, j) = set;
10605 }
10606
10607 pattern = emit_insn (pattern);
10608 if (fn == mips_save_reg)
10609 RTX_FRAME_RELATED_P (pattern) = 1;
10610
10611 /* Adjust the last offset. */
10612 *offset -= UNITS_PER_WORD * nregs;
10613
10614 return true;
10615 }
10616
10617 /* Call FN for each register that is saved by the current function.
10618 SP_OFFSET is the offset of the current stack pointer from the start
10619 of the frame. */
10620
10621 static void
10622 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
10623 mips_save_restore_fn fn)
10624 {
10625 enum machine_mode fpr_mode;
10626 int regno;
10627 const struct mips_frame_info *frame = &cfun->machine->frame;
10628 HOST_WIDE_INT offset;
10629 unsigned int mask;
10630
10631 /* Save registers starting from high to low. The debuggers prefer at least
10632 the return register be stored at func+4, and also it allows us not to
10633 need a nop in the epilogue if at least one register is reloaded in
10634 addition to return address. */
10635 offset = frame->gp_sp_offset - sp_offset;
10636 mask = frame->mask;
10637
10638 if (TARGET_MICROMIPS)
10639 umips_build_save_restore (fn, &mask, &offset);
10640
10641 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
10642 if (BITSET_P (mask, regno - GP_REG_FIRST))
10643 {
10644 /* Record the ra offset for use by mips_function_profiler. */
10645 if (regno == RETURN_ADDR_REGNUM)
10646 cfun->machine->frame.ra_fp_offset = offset + sp_offset;
10647 mips_save_restore_reg (word_mode, regno, offset, fn);
10648 offset -= UNITS_PER_WORD;
10649 }
10650
10651 /* This loop must iterate over the same space as its companion in
10652 mips_compute_frame_info. */
10653 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
10654 fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
10655 for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
10656 regno >= FP_REG_FIRST;
10657 regno -= MAX_FPRS_PER_FMT)
10658 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
10659 {
10660 mips_save_restore_reg (fpr_mode, regno, offset, fn);
10661 offset -= GET_MODE_SIZE (fpr_mode);
10662 }
10663 }
10664
10665 /* Return true if a move between register REGNO and its save slot (MEM)
10666 can be done in a single move. LOAD_P is true if we are loading
10667 from the slot, false if we are storing to it. */
10668
10669 static bool
10670 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
10671 {
10672 /* There is a specific MIPS16 instruction for saving $31 to the stack. */
10673 if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
10674 return false;
10675
10676 return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
10677 GET_MODE (mem), mem, load_p) == NO_REGS;
10678 }
10679
10680 /* Emit a move from SRC to DEST, given that one of them is a register
10681 save slot and that the other is a register. TEMP is a temporary
10682 GPR of the same mode that is available if need be. */
10683
10684 void
10685 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
10686 {
10687 unsigned int regno;
10688 rtx mem;
10689
10690 if (REG_P (src))
10691 {
10692 regno = REGNO (src);
10693 mem = dest;
10694 }
10695 else
10696 {
10697 regno = REGNO (dest);
10698 mem = src;
10699 }
10700
10701 if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
10702 {
10703 /* We don't yet know whether we'll need this instruction or not.
10704 Postpone the decision by emitting a ghost move. This move
10705 is specifically not frame-related; only the split version is. */
10706 if (TARGET_64BIT)
10707 emit_insn (gen_move_gpdi (dest, src));
10708 else
10709 emit_insn (gen_move_gpsi (dest, src));
10710 return;
10711 }
10712
10713 if (regno == HI_REGNUM)
10714 {
10715 if (REG_P (dest))
10716 {
10717 mips_emit_move (temp, src);
10718 if (TARGET_64BIT)
10719 emit_insn (gen_mthisi_di (gen_rtx_REG (TImode, MD_REG_FIRST),
10720 temp, gen_rtx_REG (DImode, LO_REGNUM)));
10721 else
10722 emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
10723 temp, gen_rtx_REG (SImode, LO_REGNUM)));
10724 }
10725 else
10726 {
10727 if (TARGET_64BIT)
10728 emit_insn (gen_mfhidi_ti (temp,
10729 gen_rtx_REG (TImode, MD_REG_FIRST)));
10730 else
10731 emit_insn (gen_mfhisi_di (temp,
10732 gen_rtx_REG (DImode, MD_REG_FIRST)));
10733 mips_emit_move (dest, temp);
10734 }
10735 }
10736 else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
10737 mips_emit_move (dest, src);
10738 else
10739 {
10740 gcc_assert (!reg_overlap_mentioned_p (dest, temp));
10741 mips_emit_move (temp, src);
10742 mips_emit_move (dest, temp);
10743 }
10744 if (MEM_P (dest))
10745 mips_set_frame_expr (mips_frame_set (dest, src));
10746 }
10747 \f
10748 /* If we're generating n32 or n64 abicalls, and the current function
10749 does not use $28 as its global pointer, emit a cplocal directive.
10750 Use pic_offset_table_rtx as the argument to the directive. */
10751
10752 static void
10753 mips_output_cplocal (void)
10754 {
10755 if (!TARGET_EXPLICIT_RELOCS
10756 && mips_must_initialize_gp_p ()
10757 && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
10758 output_asm_insn (".cplocal %+", 0);
10759 }
10760
10761 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE. */
10762
10763 static void
10764 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10765 {
10766 const char *fnname;
10767
10768 /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
10769 floating-point arguments. */
10770 if (TARGET_MIPS16
10771 && TARGET_HARD_FLOAT_ABI
10772 && crtl->args.info.fp_code != 0)
10773 mips16_build_function_stub ();
10774
10775 /* Get the function name the same way that toplev.c does before calling
10776 assemble_start_function. This is needed so that the name used here
10777 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
10778 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10779 mips_start_function_definition (fnname, TARGET_MIPS16);
10780
10781 /* Output MIPS-specific frame information. */
10782 if (!flag_inhibit_size_directive)
10783 {
10784 const struct mips_frame_info *frame;
10785
10786 frame = &cfun->machine->frame;
10787
10788 /* .frame FRAMEREG, FRAMESIZE, RETREG. */
10789 fprintf (file,
10790 "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
10791 "# vars= " HOST_WIDE_INT_PRINT_DEC
10792 ", regs= %d/%d"
10793 ", args= " HOST_WIDE_INT_PRINT_DEC
10794 ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
10795 reg_names[frame_pointer_needed
10796 ? HARD_FRAME_POINTER_REGNUM
10797 : STACK_POINTER_REGNUM],
10798 (frame_pointer_needed
10799 ? frame->total_size - frame->hard_frame_pointer_offset
10800 : frame->total_size),
10801 reg_names[RETURN_ADDR_REGNUM],
10802 frame->var_size,
10803 frame->num_gp, frame->num_fp,
10804 frame->args_size,
10805 frame->cprestore_size);
10806
10807 /* .mask MASK, OFFSET. */
10808 fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10809 frame->mask, frame->gp_save_offset);
10810
10811 /* .fmask MASK, OFFSET. */
10812 fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10813 frame->fmask, frame->fp_save_offset);
10814 }
10815
10816 /* Handle the initialization of $gp for SVR4 PIC, if applicable.
10817 Also emit the ".set noreorder; .set nomacro" sequence for functions
10818 that need it. */
10819 if (mips_must_initialize_gp_p ()
10820 && mips_current_loadgp_style () == LOADGP_OLDABI)
10821 {
10822 if (TARGET_MIPS16)
10823 {
10824 /* This is a fixed-form sequence. The position of the
10825 first two instructions is important because of the
10826 way _gp_disp is defined. */
10827 output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
10828 output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
10829 output_asm_insn ("sll\t$2,16", 0);
10830 output_asm_insn ("addu\t$2,$3", 0);
10831 }
10832 else
10833 {
10834 /* .cpload must be in a .set noreorder but not a
10835 .set nomacro block. */
10836 mips_push_asm_switch (&mips_noreorder);
10837 output_asm_insn (".cpload\t%^", 0);
10838 if (!cfun->machine->all_noreorder_p)
10839 mips_pop_asm_switch (&mips_noreorder);
10840 else
10841 mips_push_asm_switch (&mips_nomacro);
10842 }
10843 }
10844 else if (cfun->machine->all_noreorder_p)
10845 {
10846 mips_push_asm_switch (&mips_noreorder);
10847 mips_push_asm_switch (&mips_nomacro);
10848 }
10849
10850 /* Tell the assembler which register we're using as the global
10851 pointer. This is needed for thunks, since they can use either
10852 explicit relocs or assembler macros. */
10853 mips_output_cplocal ();
10854 }
10855
10856 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE. */
10857
10858 static void
10859 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10860 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10861 {
10862 const char *fnname;
10863
10864 /* Reinstate the normal $gp. */
10865 SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
10866 mips_output_cplocal ();
10867
10868 if (cfun->machine->all_noreorder_p)
10869 {
10870 mips_pop_asm_switch (&mips_nomacro);
10871 mips_pop_asm_switch (&mips_noreorder);
10872 }
10873
10874 /* Get the function name the same way that toplev.c does before calling
10875 assemble_start_function. This is needed so that the name used here
10876 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
10877 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10878 mips_end_function_definition (fnname);
10879 }
10880 \f
10881 /* Emit an optimisation barrier for accesses to the current frame. */
10882
10883 static void
10884 mips_frame_barrier (void)
10885 {
10886 emit_clobber (gen_frame_mem (BLKmode, stack_pointer_rtx));
10887 }
10888
10889
10890 /* The __gnu_local_gp symbol. */
10891
10892 static GTY(()) rtx mips_gnu_local_gp;
10893
10894 /* If we're generating n32 or n64 abicalls, emit instructions
10895 to set up the global pointer. */
10896
10897 static void
10898 mips_emit_loadgp (void)
10899 {
10900 rtx addr, offset, incoming_address, base, index, pic_reg;
10901
10902 pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
10903 switch (mips_current_loadgp_style ())
10904 {
10905 case LOADGP_ABSOLUTE:
10906 if (mips_gnu_local_gp == NULL)
10907 {
10908 mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
10909 SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
10910 }
10911 emit_insn (PMODE_INSN (gen_loadgp_absolute,
10912 (pic_reg, mips_gnu_local_gp)));
10913 break;
10914
10915 case LOADGP_OLDABI:
10916 /* Added by mips_output_function_prologue. */
10917 break;
10918
10919 case LOADGP_NEWABI:
10920 addr = XEXP (DECL_RTL (current_function_decl), 0);
10921 offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
10922 incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
10923 emit_insn (PMODE_INSN (gen_loadgp_newabi,
10924 (pic_reg, offset, incoming_address)));
10925 break;
10926
10927 case LOADGP_RTP:
10928 base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
10929 index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
10930 emit_insn (PMODE_INSN (gen_loadgp_rtp, (pic_reg, base, index)));
10931 break;
10932
10933 default:
10934 return;
10935 }
10936
10937 if (TARGET_MIPS16)
10938 emit_insn (PMODE_INSN (gen_copygp_mips16,
10939 (pic_offset_table_rtx, pic_reg)));
10940
10941 /* Emit a blockage if there are implicit uses of the GP register.
10942 This includes profiled functions, because FUNCTION_PROFILE uses
10943 a jal macro. */
10944 if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
10945 emit_insn (gen_loadgp_blockage ());
10946 }
10947
10948 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
10949
10950 #if PROBE_INTERVAL > 32768
10951 #error Cannot use indexed addressing mode for stack probing
10952 #endif
10953
10954 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
10955 inclusive. These are offsets from the current stack pointer. */
10956
10957 static void
10958 mips_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
10959 {
10960 if (TARGET_MIPS16)
10961 sorry ("-fstack-check=specific not implemented for MIPS16");
10962
10963 /* See if we have a constant small number of probes to generate. If so,
10964 that's the easy case. */
10965 if (first + size <= 32768)
10966 {
10967 HOST_WIDE_INT i;
10968
10969 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
10970 it exceeds SIZE. If only one probe is needed, this will not
10971 generate any code. Then probe at FIRST + SIZE. */
10972 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
10973 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10974 -(first + i)));
10975
10976 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10977 -(first + size)));
10978 }
10979
10980 /* Otherwise, do the same as above, but in a loop. Note that we must be
10981 extra careful with variables wrapping around because we might be at
10982 the very top (or the very bottom) of the address space and we have
10983 to be able to handle this case properly; in particular, we use an
10984 equality test for the loop condition. */
10985 else
10986 {
10987 HOST_WIDE_INT rounded_size;
10988 rtx r3 = MIPS_PROLOGUE_TEMP (Pmode);
10989 rtx r12 = MIPS_PROLOGUE_TEMP2 (Pmode);
10990
10991 /* Sanity check for the addressing mode we're going to use. */
10992 gcc_assert (first <= 32768);
10993
10994
10995 /* Step 1: round SIZE to the previous multiple of the interval. */
10996
10997 rounded_size = size & -PROBE_INTERVAL;
10998
10999
11000 /* Step 2: compute initial and final value of the loop counter. */
11001
11002 /* TEST_ADDR = SP + FIRST. */
11003 emit_insn (gen_rtx_SET (VOIDmode, r3,
11004 plus_constant (Pmode, stack_pointer_rtx,
11005 -first)));
11006
11007 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
11008 if (rounded_size > 32768)
11009 {
11010 emit_move_insn (r12, GEN_INT (rounded_size));
11011 emit_insn (gen_rtx_SET (VOIDmode, r12,
11012 gen_rtx_MINUS (Pmode, r3, r12)));
11013 }
11014 else
11015 emit_insn (gen_rtx_SET (VOIDmode, r12,
11016 plus_constant (Pmode, r3, -rounded_size)));
11017
11018
11019 /* Step 3: the loop
11020
11021 while (TEST_ADDR != LAST_ADDR)
11022 {
11023 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
11024 probe at TEST_ADDR
11025 }
11026
11027 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
11028 until it is equal to ROUNDED_SIZE. */
11029
11030 emit_insn (PMODE_INSN (gen_probe_stack_range, (r3, r3, r12)));
11031
11032
11033 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
11034 that SIZE is equal to ROUNDED_SIZE. */
11035
11036 if (size != rounded_size)
11037 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
11038 }
11039
11040 /* Make sure nothing is scheduled before we are done. */
11041 emit_insn (gen_blockage ());
11042 }
11043
11044 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
11045 absolute addresses. */
11046
11047 const char *
11048 mips_output_probe_stack_range (rtx reg1, rtx reg2)
11049 {
11050 static int labelno = 0;
11051 char loop_lab[32], end_lab[32], tmp[64];
11052 rtx xops[2];
11053
11054 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
11055 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
11056
11057 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
11058
11059 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
11060 xops[0] = reg1;
11061 xops[1] = reg2;
11062 strcpy (tmp, "%(%<beq\t%0,%1,");
11063 output_asm_insn (strcat (tmp, &end_lab[1]), xops);
11064
11065 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
11066 xops[1] = GEN_INT (-PROBE_INTERVAL);
11067 if (TARGET_64BIT && TARGET_LONG64)
11068 output_asm_insn ("daddiu\t%0,%0,%1", xops);
11069 else
11070 output_asm_insn ("addiu\t%0,%0,%1", xops);
11071
11072 /* Probe at TEST_ADDR and branch. */
11073 fprintf (asm_out_file, "\tb\t");
11074 assemble_name_raw (asm_out_file, loop_lab);
11075 fputc ('\n', asm_out_file);
11076 if (TARGET_64BIT)
11077 output_asm_insn ("sd\t$0,0(%0)%)", xops);
11078 else
11079 output_asm_insn ("sw\t$0,0(%0)%)", xops);
11080
11081 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
11082
11083 return "";
11084 }
11085
11086 /* A for_each_rtx callback. Stop the search if *X is a kernel register. */
11087
11088 static int
11089 mips_kernel_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
11090 {
11091 return REG_P (*x) && KERNEL_REG_P (REGNO (*x));
11092 }
11093
11094 /* Expand the "prologue" pattern. */
11095
11096 void
11097 mips_expand_prologue (void)
11098 {
11099 const struct mips_frame_info *frame;
11100 HOST_WIDE_INT size;
11101 unsigned int nargs;
11102 rtx insn;
11103
11104 if (cfun->machine->global_pointer != INVALID_REGNUM)
11105 {
11106 /* Check whether an insn uses pic_offset_table_rtx, either explicitly
11107 or implicitly. If so, we can commit to using a global pointer
11108 straight away, otherwise we need to defer the decision. */
11109 if (mips_cfun_has_inflexible_gp_ref_p ()
11110 || mips_cfun_has_flexible_gp_ref_p ())
11111 {
11112 cfun->machine->must_initialize_gp_p = true;
11113 cfun->machine->must_restore_gp_when_clobbered_p = true;
11114 }
11115
11116 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
11117 }
11118
11119 frame = &cfun->machine->frame;
11120 size = frame->total_size;
11121
11122 if (flag_stack_usage_info)
11123 current_function_static_stack_size = size;
11124
11125 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
11126 {
11127 if (crtl->is_leaf && !cfun->calls_alloca)
11128 {
11129 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
11130 mips_emit_probe_stack_range (STACK_CHECK_PROTECT,
11131 size - STACK_CHECK_PROTECT);
11132 }
11133 else if (size > 0)
11134 mips_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
11135 }
11136
11137 /* Save the registers. Allocate up to MIPS_MAX_FIRST_STACK_STEP
11138 bytes beforehand; this is enough to cover the register save area
11139 without going out of range. */
11140 if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
11141 || frame->num_cop0_regs > 0)
11142 {
11143 HOST_WIDE_INT step1;
11144
11145 step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
11146 if (GENERATE_MIPS16E_SAVE_RESTORE)
11147 {
11148 HOST_WIDE_INT offset;
11149 unsigned int mask, regno;
11150
11151 /* Try to merge argument stores into the save instruction. */
11152 nargs = mips16e_collect_argument_saves ();
11153
11154 /* Build the save instruction. */
11155 mask = frame->mask;
11156 insn = mips16e_build_save_restore (false, &mask, &offset,
11157 nargs, step1);
11158 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11159 mips_frame_barrier ();
11160 size -= step1;
11161
11162 /* Check if we need to save other registers. */
11163 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11164 if (BITSET_P (mask, regno - GP_REG_FIRST))
11165 {
11166 offset -= UNITS_PER_WORD;
11167 mips_save_restore_reg (word_mode, regno,
11168 offset, mips_save_reg);
11169 }
11170 }
11171 else
11172 {
11173 if (cfun->machine->interrupt_handler_p)
11174 {
11175 HOST_WIDE_INT offset;
11176 rtx mem;
11177
11178 /* If this interrupt is using a shadow register set, we need to
11179 get the stack pointer from the previous register set. */
11180 if (cfun->machine->use_shadow_register_set_p)
11181 emit_insn (gen_mips_rdpgpr (stack_pointer_rtx,
11182 stack_pointer_rtx));
11183
11184 if (!cfun->machine->keep_interrupts_masked_p)
11185 {
11186 /* Move from COP0 Cause to K0. */
11187 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
11188 gen_rtx_REG (SImode,
11189 COP0_CAUSE_REG_NUM)));
11190 /* Move from COP0 EPC to K1. */
11191 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11192 gen_rtx_REG (SImode,
11193 COP0_EPC_REG_NUM)));
11194 }
11195
11196 /* Allocate the first part of the frame. */
11197 insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
11198 GEN_INT (-step1));
11199 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11200 mips_frame_barrier ();
11201 size -= step1;
11202
11203 /* Start at the uppermost location for saving. */
11204 offset = frame->cop0_sp_offset - size;
11205 if (!cfun->machine->keep_interrupts_masked_p)
11206 {
11207 /* Push EPC into its stack slot. */
11208 mem = gen_frame_mem (word_mode,
11209 plus_constant (Pmode, stack_pointer_rtx,
11210 offset));
11211 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11212 offset -= UNITS_PER_WORD;
11213 }
11214
11215 /* Move from COP0 Status to K1. */
11216 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11217 gen_rtx_REG (SImode,
11218 COP0_STATUS_REG_NUM)));
11219
11220 /* Right justify the RIPL in k0. */
11221 if (!cfun->machine->keep_interrupts_masked_p)
11222 emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
11223 gen_rtx_REG (SImode, K0_REG_NUM),
11224 GEN_INT (CAUSE_IPL)));
11225
11226 /* Push Status into its stack slot. */
11227 mem = gen_frame_mem (word_mode,
11228 plus_constant (Pmode, stack_pointer_rtx,
11229 offset));
11230 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11231 offset -= UNITS_PER_WORD;
11232
11233 /* Insert the RIPL into our copy of SR (k1) as the new IPL. */
11234 if (!cfun->machine->keep_interrupts_masked_p)
11235 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11236 GEN_INT (6),
11237 GEN_INT (SR_IPL),
11238 gen_rtx_REG (SImode, K0_REG_NUM)));
11239
11240 if (!cfun->machine->keep_interrupts_masked_p)
11241 /* Enable interrupts by clearing the KSU ERL and EXL bits.
11242 IE is already the correct value, so we don't have to do
11243 anything explicit. */
11244 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11245 GEN_INT (4),
11246 GEN_INT (SR_EXL),
11247 gen_rtx_REG (SImode, GP_REG_FIRST)));
11248 else
11249 /* Disable interrupts by clearing the KSU, ERL, EXL,
11250 and IE bits. */
11251 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11252 GEN_INT (5),
11253 GEN_INT (SR_IE),
11254 gen_rtx_REG (SImode, GP_REG_FIRST)));
11255 }
11256 else
11257 {
11258 insn = gen_add3_insn (stack_pointer_rtx,
11259 stack_pointer_rtx,
11260 GEN_INT (-step1));
11261 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11262 mips_frame_barrier ();
11263 size -= step1;
11264 }
11265 mips_for_each_saved_acc (size, mips_save_reg);
11266 mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
11267 }
11268 }
11269
11270 /* Allocate the rest of the frame. */
11271 if (size > 0)
11272 {
11273 if (SMALL_OPERAND (-size))
11274 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
11275 stack_pointer_rtx,
11276 GEN_INT (-size)))) = 1;
11277 else
11278 {
11279 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
11280 if (TARGET_MIPS16)
11281 {
11282 /* There are no instructions to add or subtract registers
11283 from the stack pointer, so use the frame pointer as a
11284 temporary. We should always be using a frame pointer
11285 in this case anyway. */
11286 gcc_assert (frame_pointer_needed);
11287 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11288 emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
11289 hard_frame_pointer_rtx,
11290 MIPS_PROLOGUE_TEMP (Pmode)));
11291 mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
11292 }
11293 else
11294 emit_insn (gen_sub3_insn (stack_pointer_rtx,
11295 stack_pointer_rtx,
11296 MIPS_PROLOGUE_TEMP (Pmode)));
11297
11298 /* Describe the combined effect of the previous instructions. */
11299 mips_set_frame_expr
11300 (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
11301 plus_constant (Pmode, stack_pointer_rtx, -size)));
11302 }
11303 mips_frame_barrier ();
11304 }
11305
11306 /* Set up the frame pointer, if we're using one. */
11307 if (frame_pointer_needed)
11308 {
11309 HOST_WIDE_INT offset;
11310
11311 offset = frame->hard_frame_pointer_offset;
11312 if (offset == 0)
11313 {
11314 insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11315 RTX_FRAME_RELATED_P (insn) = 1;
11316 }
11317 else if (SMALL_OPERAND (offset))
11318 {
11319 insn = gen_add3_insn (hard_frame_pointer_rtx,
11320 stack_pointer_rtx, GEN_INT (offset));
11321 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11322 }
11323 else
11324 {
11325 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
11326 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11327 emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
11328 hard_frame_pointer_rtx,
11329 MIPS_PROLOGUE_TEMP (Pmode)));
11330 mips_set_frame_expr
11331 (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
11332 plus_constant (Pmode, stack_pointer_rtx, offset)));
11333 }
11334 }
11335
11336 mips_emit_loadgp ();
11337
11338 /* Initialize the $gp save slot. */
11339 if (mips_cfun_has_cprestore_slot_p ())
11340 {
11341 rtx base, mem, gp, temp;
11342 HOST_WIDE_INT offset;
11343
11344 mips_get_cprestore_base_and_offset (&base, &offset, false);
11345 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11346 gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
11347 temp = (SMALL_OPERAND (offset)
11348 ? gen_rtx_SCRATCH (Pmode)
11349 : MIPS_PROLOGUE_TEMP (Pmode));
11350 emit_insn (PMODE_INSN (gen_potential_cprestore,
11351 (mem, GEN_INT (offset), gp, temp)));
11352
11353 mips_get_cprestore_base_and_offset (&base, &offset, true);
11354 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11355 emit_insn (PMODE_INSN (gen_use_cprestore, (mem)));
11356 }
11357
11358 /* We need to search back to the last use of K0 or K1. */
11359 if (cfun->machine->interrupt_handler_p)
11360 {
11361 for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
11362 if (INSN_P (insn)
11363 && for_each_rtx (&PATTERN (insn), mips_kernel_reg_p, NULL))
11364 break;
11365 /* Emit a move from K1 to COP0 Status after insn. */
11366 gcc_assert (insn != NULL_RTX);
11367 emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11368 gen_rtx_REG (SImode, K1_REG_NUM)),
11369 insn);
11370 }
11371
11372 /* If we are profiling, make sure no instructions are scheduled before
11373 the call to mcount. */
11374 if (crtl->profile)
11375 emit_insn (gen_blockage ());
11376 }
11377 \f
11378 /* Attach all pending register saves to the previous instruction.
11379 Return that instruction. */
11380
11381 static rtx
11382 mips_epilogue_emit_cfa_restores (void)
11383 {
11384 rtx insn;
11385
11386 insn = get_last_insn ();
11387 gcc_assert (insn && !REG_NOTES (insn));
11388 if (mips_epilogue.cfa_restores)
11389 {
11390 RTX_FRAME_RELATED_P (insn) = 1;
11391 REG_NOTES (insn) = mips_epilogue.cfa_restores;
11392 mips_epilogue.cfa_restores = 0;
11393 }
11394 return insn;
11395 }
11396
11397 /* Like mips_epilogue_emit_cfa_restores, but also record that the CFA is
11398 now at REG + OFFSET. */
11399
11400 static void
11401 mips_epilogue_set_cfa (rtx reg, HOST_WIDE_INT offset)
11402 {
11403 rtx insn;
11404
11405 insn = mips_epilogue_emit_cfa_restores ();
11406 if (reg != mips_epilogue.cfa_reg || offset != mips_epilogue.cfa_offset)
11407 {
11408 RTX_FRAME_RELATED_P (insn) = 1;
11409 REG_NOTES (insn) = alloc_reg_note (REG_CFA_DEF_CFA,
11410 plus_constant (Pmode, reg, offset),
11411 REG_NOTES (insn));
11412 mips_epilogue.cfa_reg = reg;
11413 mips_epilogue.cfa_offset = offset;
11414 }
11415 }
11416
11417 /* Emit instructions to restore register REG from slot MEM. Also update
11418 the cfa_restores list. */
11419
11420 static void
11421 mips_restore_reg (rtx reg, rtx mem)
11422 {
11423 /* There's no MIPS16 instruction to load $31 directly. Load into
11424 $7 instead and adjust the return insn appropriately. */
11425 if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
11426 reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
11427 else if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
11428 {
11429 mips_add_cfa_restore (mips_subword (reg, true));
11430 mips_add_cfa_restore (mips_subword (reg, false));
11431 }
11432 else
11433 mips_add_cfa_restore (reg);
11434
11435 mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
11436 if (REGNO (reg) == REGNO (mips_epilogue.cfa_reg))
11437 /* The CFA is currently defined in terms of the register whose
11438 value we have just restored. Redefine the CFA in terms of
11439 the stack pointer. */
11440 mips_epilogue_set_cfa (stack_pointer_rtx,
11441 mips_epilogue.cfa_restore_sp_offset);
11442 }
11443
11444 /* Emit code to set the stack pointer to BASE + OFFSET, given that
11445 BASE + OFFSET is NEW_FRAME_SIZE bytes below the top of the frame.
11446 BASE, if not the stack pointer, is available as a temporary. */
11447
11448 static void
11449 mips_deallocate_stack (rtx base, rtx offset, HOST_WIDE_INT new_frame_size)
11450 {
11451 if (base == stack_pointer_rtx && offset == const0_rtx)
11452 return;
11453
11454 mips_frame_barrier ();
11455 if (offset == const0_rtx)
11456 {
11457 emit_move_insn (stack_pointer_rtx, base);
11458 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11459 }
11460 else if (TARGET_MIPS16 && base != stack_pointer_rtx)
11461 {
11462 emit_insn (gen_add3_insn (base, base, offset));
11463 mips_epilogue_set_cfa (base, new_frame_size);
11464 emit_move_insn (stack_pointer_rtx, base);
11465 }
11466 else
11467 {
11468 emit_insn (gen_add3_insn (stack_pointer_rtx, base, offset));
11469 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11470 }
11471 }
11472
11473 /* Emit any instructions needed before a return. */
11474
11475 void
11476 mips_expand_before_return (void)
11477 {
11478 /* When using a call-clobbered gp, we start out with unified call
11479 insns that include instructions to restore the gp. We then split
11480 these unified calls after reload. These split calls explicitly
11481 clobber gp, so there is no need to define
11482 PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
11483
11484 For consistency, we should also insert an explicit clobber of $28
11485 before return insns, so that the post-reload optimizers know that
11486 the register is not live on exit. */
11487 if (TARGET_CALL_CLOBBERED_GP)
11488 emit_clobber (pic_offset_table_rtx);
11489 }
11490
11491 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
11492 says which. */
11493
11494 void
11495 mips_expand_epilogue (bool sibcall_p)
11496 {
11497 const struct mips_frame_info *frame;
11498 HOST_WIDE_INT step1, step2;
11499 rtx base, adjust, insn;
11500 bool use_jraddiusp_p = false;
11501
11502 if (!sibcall_p && mips_can_use_return_insn ())
11503 {
11504 emit_jump_insn (gen_return ());
11505 return;
11506 }
11507
11508 /* In MIPS16 mode, if the return value should go into a floating-point
11509 register, we need to call a helper routine to copy it over. */
11510 if (mips16_cfun_returns_in_fpr_p ())
11511 mips16_copy_fpr_return_value ();
11512
11513 /* Split the frame into two. STEP1 is the amount of stack we should
11514 deallocate before restoring the registers. STEP2 is the amount we
11515 should deallocate afterwards.
11516
11517 Start off by assuming that no registers need to be restored. */
11518 frame = &cfun->machine->frame;
11519 step1 = frame->total_size;
11520 step2 = 0;
11521
11522 /* Work out which register holds the frame address. */
11523 if (!frame_pointer_needed)
11524 base = stack_pointer_rtx;
11525 else
11526 {
11527 base = hard_frame_pointer_rtx;
11528 step1 -= frame->hard_frame_pointer_offset;
11529 }
11530 mips_epilogue.cfa_reg = base;
11531 mips_epilogue.cfa_offset = step1;
11532 mips_epilogue.cfa_restores = NULL_RTX;
11533
11534 /* If we need to restore registers, deallocate as much stack as
11535 possible in the second step without going out of range. */
11536 if ((frame->mask | frame->fmask | frame->acc_mask) != 0
11537 || frame->num_cop0_regs > 0)
11538 {
11539 step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
11540 step1 -= step2;
11541 }
11542
11543 /* Get an rtx for STEP1 that we can add to BASE. */
11544 adjust = GEN_INT (step1);
11545 if (!SMALL_OPERAND (step1))
11546 {
11547 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
11548 adjust = MIPS_EPILOGUE_TEMP (Pmode);
11549 }
11550 mips_deallocate_stack (base, adjust, step2);
11551
11552 /* If we're using addressing macros, $gp is implicitly used by all
11553 SYMBOL_REFs. We must emit a blockage insn before restoring $gp
11554 from the stack. */
11555 if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
11556 emit_insn (gen_blockage ());
11557
11558 mips_epilogue.cfa_restore_sp_offset = step2;
11559 if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
11560 {
11561 unsigned int regno, mask;
11562 HOST_WIDE_INT offset;
11563 rtx restore;
11564
11565 /* Generate the restore instruction. */
11566 mask = frame->mask;
11567 restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
11568
11569 /* Restore any other registers manually. */
11570 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11571 if (BITSET_P (mask, regno - GP_REG_FIRST))
11572 {
11573 offset -= UNITS_PER_WORD;
11574 mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
11575 }
11576
11577 /* Restore the remaining registers and deallocate the final bit
11578 of the frame. */
11579 mips_frame_barrier ();
11580 emit_insn (restore);
11581 mips_epilogue_set_cfa (stack_pointer_rtx, 0);
11582 }
11583 else
11584 {
11585 /* Restore the registers. */
11586 mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
11587 mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
11588 mips_restore_reg);
11589
11590 if (cfun->machine->interrupt_handler_p)
11591 {
11592 HOST_WIDE_INT offset;
11593 rtx mem;
11594
11595 offset = frame->cop0_sp_offset - (frame->total_size - step2);
11596 if (!cfun->machine->keep_interrupts_masked_p)
11597 {
11598 /* Restore the original EPC. */
11599 mem = gen_frame_mem (word_mode,
11600 plus_constant (Pmode, stack_pointer_rtx,
11601 offset));
11602 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11603 offset -= UNITS_PER_WORD;
11604
11605 /* Move to COP0 EPC. */
11606 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
11607 gen_rtx_REG (SImode, K0_REG_NUM)));
11608 }
11609
11610 /* Restore the original Status. */
11611 mem = gen_frame_mem (word_mode,
11612 plus_constant (Pmode, stack_pointer_rtx,
11613 offset));
11614 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11615 offset -= UNITS_PER_WORD;
11616
11617 /* If we don't use shadow register set, we need to update SP. */
11618 if (!cfun->machine->use_shadow_register_set_p)
11619 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11620 else
11621 /* The choice of position is somewhat arbitrary in this case. */
11622 mips_epilogue_emit_cfa_restores ();
11623
11624 /* Move to COP0 Status. */
11625 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11626 gen_rtx_REG (SImode, K0_REG_NUM)));
11627 }
11628 else if (TARGET_MICROMIPS
11629 && !crtl->calls_eh_return
11630 && !sibcall_p
11631 && step2 > 0
11632 && mips_unsigned_immediate_p (step2, 5, 2))
11633 use_jraddiusp_p = true;
11634 else
11635 /* Deallocate the final bit of the frame. */
11636 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11637 }
11638
11639 if (!use_jraddiusp_p)
11640 gcc_assert (!mips_epilogue.cfa_restores);
11641
11642 /* Add in the __builtin_eh_return stack adjustment. We need to
11643 use a temporary in MIPS16 code. */
11644 if (crtl->calls_eh_return)
11645 {
11646 if (TARGET_MIPS16)
11647 {
11648 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
11649 emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
11650 MIPS_EPILOGUE_TEMP (Pmode),
11651 EH_RETURN_STACKADJ_RTX));
11652 mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
11653 }
11654 else
11655 emit_insn (gen_add3_insn (stack_pointer_rtx,
11656 stack_pointer_rtx,
11657 EH_RETURN_STACKADJ_RTX));
11658 }
11659
11660 if (!sibcall_p)
11661 {
11662 mips_expand_before_return ();
11663 if (cfun->machine->interrupt_handler_p)
11664 {
11665 /* Interrupt handlers generate eret or deret. */
11666 if (cfun->machine->use_debug_exception_return_p)
11667 emit_jump_insn (gen_mips_deret ());
11668 else
11669 emit_jump_insn (gen_mips_eret ());
11670 }
11671 else
11672 {
11673 rtx pat;
11674
11675 /* When generating MIPS16 code, the normal
11676 mips_for_each_saved_gpr_and_fpr path will restore the return
11677 address into $7 rather than $31. */
11678 if (TARGET_MIPS16
11679 && !GENERATE_MIPS16E_SAVE_RESTORE
11680 && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
11681 {
11682 /* simple_returns cannot rely on values that are only available
11683 on paths through the epilogue (because return paths that do
11684 not pass through the epilogue may nevertheless reuse a
11685 simple_return that occurs at the end of the epilogue).
11686 Use a normal return here instead. */
11687 rtx reg = gen_rtx_REG (Pmode, GP_REG_FIRST + 7);
11688 pat = gen_return_internal (reg);
11689 }
11690 else if (use_jraddiusp_p)
11691 pat = gen_jraddiusp (GEN_INT (step2));
11692 else
11693 {
11694 rtx reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
11695 pat = gen_simple_return_internal (reg);
11696 }
11697 emit_jump_insn (pat);
11698 if (use_jraddiusp_p)
11699 mips_epilogue_set_cfa (stack_pointer_rtx, step2);
11700 }
11701 }
11702
11703 /* Search from the beginning to the first use of K0 or K1. */
11704 if (cfun->machine->interrupt_handler_p
11705 && !cfun->machine->keep_interrupts_masked_p)
11706 {
11707 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
11708 if (INSN_P (insn)
11709 && for_each_rtx (&PATTERN(insn), mips_kernel_reg_p, NULL))
11710 break;
11711 gcc_assert (insn != NULL_RTX);
11712 /* Insert disable interrupts before the first use of K0 or K1. */
11713 emit_insn_before (gen_mips_di (), insn);
11714 emit_insn_before (gen_mips_ehb (), insn);
11715 }
11716 }
11717 \f
11718 /* Return nonzero if this function is known to have a null epilogue.
11719 This allows the optimizer to omit jumps to jumps if no stack
11720 was created. */
11721
11722 bool
11723 mips_can_use_return_insn (void)
11724 {
11725 /* Interrupt handlers need to go through the epilogue. */
11726 if (cfun->machine->interrupt_handler_p)
11727 return false;
11728
11729 if (!reload_completed)
11730 return false;
11731
11732 if (crtl->profile)
11733 return false;
11734
11735 /* In MIPS16 mode, a function that returns a floating-point value
11736 needs to arrange to copy the return value into the floating-point
11737 registers. */
11738 if (mips16_cfun_returns_in_fpr_p ())
11739 return false;
11740
11741 return cfun->machine->frame.total_size == 0;
11742 }
11743 \f
11744 /* Return true if register REGNO can store a value of mode MODE.
11745 The result of this function is cached in mips_hard_regno_mode_ok. */
11746
11747 static bool
11748 mips_hard_regno_mode_ok_p (unsigned int regno, enum machine_mode mode)
11749 {
11750 unsigned int size;
11751 enum mode_class mclass;
11752
11753 if (mode == CCV2mode)
11754 return (ISA_HAS_8CC
11755 && ST_REG_P (regno)
11756 && (regno - ST_REG_FIRST) % 2 == 0);
11757
11758 if (mode == CCV4mode)
11759 return (ISA_HAS_8CC
11760 && ST_REG_P (regno)
11761 && (regno - ST_REG_FIRST) % 4 == 0);
11762
11763 if (mode == CCmode)
11764 return ISA_HAS_8CC ? ST_REG_P (regno) : regno == FPSW_REGNUM;
11765
11766 size = GET_MODE_SIZE (mode);
11767 mclass = GET_MODE_CLASS (mode);
11768
11769 if (GP_REG_P (regno))
11770 return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
11771
11772 if (FP_REG_P (regno)
11773 && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
11774 || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
11775 {
11776 /* Allow 64-bit vector modes for Loongson-2E/2F. */
11777 if (TARGET_LOONGSON_VECTORS
11778 && (mode == V2SImode
11779 || mode == V4HImode
11780 || mode == V8QImode
11781 || mode == DImode))
11782 return true;
11783
11784 if (mclass == MODE_FLOAT
11785 || mclass == MODE_COMPLEX_FLOAT
11786 || mclass == MODE_VECTOR_FLOAT)
11787 return size <= UNITS_PER_FPVALUE;
11788
11789 /* Allow integer modes that fit into a single register. We need
11790 to put integers into FPRs when using instructions like CVT
11791 and TRUNC. There's no point allowing sizes smaller than a word,
11792 because the FPU has no appropriate load/store instructions. */
11793 if (mclass == MODE_INT)
11794 return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
11795 }
11796
11797 if (ACC_REG_P (regno)
11798 && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
11799 {
11800 if (MD_REG_P (regno))
11801 {
11802 /* After a multiplication or division, clobbering HI makes
11803 the value of LO unpredictable, and vice versa. This means
11804 that, for all interesting cases, HI and LO are effectively
11805 a single register.
11806
11807 We model this by requiring that any value that uses HI
11808 also uses LO. */
11809 if (size <= UNITS_PER_WORD * 2)
11810 return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
11811 }
11812 else
11813 {
11814 /* DSP accumulators do not have the same restrictions as
11815 HI and LO, so we can treat them as normal doubleword
11816 registers. */
11817 if (size <= UNITS_PER_WORD)
11818 return true;
11819
11820 if (size <= UNITS_PER_WORD * 2
11821 && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
11822 return true;
11823 }
11824 }
11825
11826 if (ALL_COP_REG_P (regno))
11827 return mclass == MODE_INT && size <= UNITS_PER_WORD;
11828
11829 if (regno == GOT_VERSION_REGNUM)
11830 return mode == SImode;
11831
11832 return false;
11833 }
11834
11835 /* Implement HARD_REGNO_NREGS. */
11836
11837 unsigned int
11838 mips_hard_regno_nregs (int regno, enum machine_mode mode)
11839 {
11840 if (ST_REG_P (regno))
11841 /* The size of FP status registers is always 4, because they only hold
11842 CCmode values, and CCmode is always considered to be 4 bytes wide. */
11843 return (GET_MODE_SIZE (mode) + 3) / 4;
11844
11845 if (FP_REG_P (regno))
11846 return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
11847
11848 /* All other registers are word-sized. */
11849 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
11850 }
11851
11852 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
11853 in mips_hard_regno_nregs. */
11854
11855 int
11856 mips_class_max_nregs (enum reg_class rclass, enum machine_mode mode)
11857 {
11858 int size;
11859 HARD_REG_SET left;
11860
11861 size = 0x8000;
11862 COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
11863 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
11864 {
11865 if (HARD_REGNO_MODE_OK (ST_REG_FIRST, mode))
11866 size = MIN (size, 4);
11867 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) ST_REGS]);
11868 }
11869 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
11870 {
11871 if (HARD_REGNO_MODE_OK (FP_REG_FIRST, mode))
11872 size = MIN (size, UNITS_PER_FPREG);
11873 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
11874 }
11875 if (!hard_reg_set_empty_p (left))
11876 size = MIN (size, UNITS_PER_WORD);
11877 return (GET_MODE_SIZE (mode) + size - 1) / size;
11878 }
11879
11880 /* Implement CANNOT_CHANGE_MODE_CLASS. */
11881
11882 bool
11883 mips_cannot_change_mode_class (enum machine_mode from,
11884 enum machine_mode to,
11885 enum reg_class rclass)
11886 {
11887 /* Allow conversions between different Loongson integer vectors,
11888 and between those vectors and DImode. */
11889 if (GET_MODE_SIZE (from) == 8 && GET_MODE_SIZE (to) == 8
11890 && INTEGRAL_MODE_P (from) && INTEGRAL_MODE_P (to))
11891 return false;
11892
11893 /* Otherwise, there are several problems with changing the modes of
11894 values in floating-point registers:
11895
11896 - When a multi-word value is stored in paired floating-point
11897 registers, the first register always holds the low word. We
11898 therefore can't allow FPRs to change between single-word and
11899 multi-word modes on big-endian targets.
11900
11901 - GCC assumes that each word of a multiword register can be
11902 accessed individually using SUBREGs. This is not true for
11903 floating-point registers if they are bigger than a word.
11904
11905 - Loading a 32-bit value into a 64-bit floating-point register
11906 will not sign-extend the value, despite what LOAD_EXTEND_OP
11907 says. We can't allow FPRs to change from SImode to a wider
11908 mode on 64-bit targets.
11909
11910 - If the FPU has already interpreted a value in one format, we
11911 must not ask it to treat the value as having a different
11912 format.
11913
11914 We therefore disallow all mode changes involving FPRs. */
11915
11916 return reg_classes_intersect_p (FP_REGS, rclass);
11917 }
11918
11919 /* Implement target hook small_register_classes_for_mode_p. */
11920
11921 static bool
11922 mips_small_register_classes_for_mode_p (enum machine_mode mode
11923 ATTRIBUTE_UNUSED)
11924 {
11925 return TARGET_MIPS16;
11926 }
11927
11928 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction. */
11929
11930 static bool
11931 mips_mode_ok_for_mov_fmt_p (enum machine_mode mode)
11932 {
11933 switch (mode)
11934 {
11935 case SFmode:
11936 return TARGET_HARD_FLOAT;
11937
11938 case DFmode:
11939 return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
11940
11941 case V2SFmode:
11942 return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
11943
11944 default:
11945 return false;
11946 }
11947 }
11948
11949 /* Implement MODES_TIEABLE_P. */
11950
11951 bool
11952 mips_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
11953 {
11954 /* FPRs allow no mode punning, so it's not worth tying modes if we'd
11955 prefer to put one of them in FPRs. */
11956 return (mode1 == mode2
11957 || (!mips_mode_ok_for_mov_fmt_p (mode1)
11958 && !mips_mode_ok_for_mov_fmt_p (mode2)));
11959 }
11960
11961 /* Implement TARGET_PREFERRED_RELOAD_CLASS. */
11962
11963 static reg_class_t
11964 mips_preferred_reload_class (rtx x, reg_class_t rclass)
11965 {
11966 if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
11967 return LEA_REGS;
11968
11969 if (reg_class_subset_p (FP_REGS, rclass)
11970 && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
11971 return FP_REGS;
11972
11973 if (reg_class_subset_p (GR_REGS, rclass))
11974 rclass = GR_REGS;
11975
11976 if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
11977 rclass = M16_REGS;
11978
11979 return rclass;
11980 }
11981
11982 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
11983 Return a "canonical" class to represent it in later calculations. */
11984
11985 static reg_class_t
11986 mips_canonicalize_move_class (reg_class_t rclass)
11987 {
11988 /* All moves involving accumulator registers have the same cost. */
11989 if (reg_class_subset_p (rclass, ACC_REGS))
11990 rclass = ACC_REGS;
11991
11992 /* Likewise promote subclasses of general registers to the most
11993 interesting containing class. */
11994 if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
11995 rclass = M16_REGS;
11996 else if (reg_class_subset_p (rclass, GENERAL_REGS))
11997 rclass = GENERAL_REGS;
11998
11999 return rclass;
12000 }
12001
12002 /* Return the cost of moving a value of mode MODE from a register of
12003 class FROM to a GPR. Return 0 for classes that are unions of other
12004 classes handled by this function. */
12005
12006 static int
12007 mips_move_to_gpr_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
12008 reg_class_t from)
12009 {
12010 switch (from)
12011 {
12012 case M16_REGS:
12013 case GENERAL_REGS:
12014 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
12015 return 2;
12016
12017 case ACC_REGS:
12018 /* MFLO and MFHI. */
12019 return 6;
12020
12021 case FP_REGS:
12022 /* MFC1, etc. */
12023 return 4;
12024
12025 case ST_REGS:
12026 /* LUI followed by MOVF. */
12027 return 4;
12028
12029 case COP0_REGS:
12030 case COP2_REGS:
12031 case COP3_REGS:
12032 /* This choice of value is historical. */
12033 return 5;
12034
12035 default:
12036 return 0;
12037 }
12038 }
12039
12040 /* Return the cost of moving a value of mode MODE from a GPR to a
12041 register of class TO. Return 0 for classes that are unions of
12042 other classes handled by this function. */
12043
12044 static int
12045 mips_move_from_gpr_cost (enum machine_mode mode, reg_class_t to)
12046 {
12047 switch (to)
12048 {
12049 case M16_REGS:
12050 case GENERAL_REGS:
12051 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
12052 return 2;
12053
12054 case ACC_REGS:
12055 /* MTLO and MTHI. */
12056 return 6;
12057
12058 case FP_REGS:
12059 /* MTC1, etc. */
12060 return 4;
12061
12062 case ST_REGS:
12063 /* A secondary reload through an FPR scratch. */
12064 return (mips_register_move_cost (mode, GENERAL_REGS, FP_REGS)
12065 + mips_register_move_cost (mode, FP_REGS, ST_REGS));
12066
12067 case COP0_REGS:
12068 case COP2_REGS:
12069 case COP3_REGS:
12070 /* This choice of value is historical. */
12071 return 5;
12072
12073 default:
12074 return 0;
12075 }
12076 }
12077
12078 /* Implement TARGET_REGISTER_MOVE_COST. Return 0 for classes that are the
12079 maximum of the move costs for subclasses; regclass will work out
12080 the maximum for us. */
12081
12082 static int
12083 mips_register_move_cost (enum machine_mode mode,
12084 reg_class_t from, reg_class_t to)
12085 {
12086 reg_class_t dregs;
12087 int cost1, cost2;
12088
12089 from = mips_canonicalize_move_class (from);
12090 to = mips_canonicalize_move_class (to);
12091
12092 /* Handle moves that can be done without using general-purpose registers. */
12093 if (from == FP_REGS)
12094 {
12095 if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
12096 /* MOV.FMT. */
12097 return 4;
12098 if (to == ST_REGS)
12099 /* The sequence generated by mips_expand_fcc_reload. */
12100 return 8;
12101 }
12102
12103 /* Handle cases in which only one class deviates from the ideal. */
12104 dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
12105 if (from == dregs)
12106 return mips_move_from_gpr_cost (mode, to);
12107 if (to == dregs)
12108 return mips_move_to_gpr_cost (mode, from);
12109
12110 /* Handles cases that require a GPR temporary. */
12111 cost1 = mips_move_to_gpr_cost (mode, from);
12112 if (cost1 != 0)
12113 {
12114 cost2 = mips_move_from_gpr_cost (mode, to);
12115 if (cost2 != 0)
12116 return cost1 + cost2;
12117 }
12118
12119 return 0;
12120 }
12121
12122 /* Implement TARGET_MEMORY_MOVE_COST. */
12123
12124 static int
12125 mips_memory_move_cost (enum machine_mode mode, reg_class_t rclass, bool in)
12126 {
12127 return (mips_cost->memory_latency
12128 + memory_move_secondary_cost (mode, rclass, in));
12129 }
12130
12131 /* Return the register class required for a secondary register when
12132 copying between one of the registers in RCLASS and value X, which
12133 has mode MODE. X is the source of the move if IN_P, otherwise it
12134 is the destination. Return NO_REGS if no secondary register is
12135 needed. */
12136
12137 enum reg_class
12138 mips_secondary_reload_class (enum reg_class rclass,
12139 enum machine_mode mode, rtx x, bool in_p)
12140 {
12141 int regno;
12142
12143 /* If X is a constant that cannot be loaded into $25, it must be loaded
12144 into some other GPR. No other register class allows a direct move. */
12145 if (mips_dangerous_for_la25_p (x))
12146 return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
12147
12148 regno = true_regnum (x);
12149 if (TARGET_MIPS16)
12150 {
12151 /* In MIPS16 mode, every move must involve a member of M16_REGS. */
12152 if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
12153 return M16_REGS;
12154
12155 return NO_REGS;
12156 }
12157
12158 /* Copying from accumulator registers to anywhere other than a general
12159 register requires a temporary general register. */
12160 if (reg_class_subset_p (rclass, ACC_REGS))
12161 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
12162 if (ACC_REG_P (regno))
12163 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12164
12165 /* We can only copy a value to a condition code register from a
12166 floating-point register, and even then we require a scratch
12167 floating-point register. We can only copy a value out of a
12168 condition-code register into a general register. */
12169 if (reg_class_subset_p (rclass, ST_REGS))
12170 {
12171 if (in_p)
12172 return FP_REGS;
12173 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
12174 }
12175 if (ST_REG_P (regno))
12176 {
12177 if (!in_p)
12178 return FP_REGS;
12179 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12180 }
12181
12182 if (reg_class_subset_p (rclass, FP_REGS))
12183 {
12184 if (MEM_P (x)
12185 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8))
12186 /* In this case we can use lwc1, swc1, ldc1 or sdc1. We'll use
12187 pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported. */
12188 return NO_REGS;
12189
12190 if (GP_REG_P (regno) || x == CONST0_RTX (mode))
12191 /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1. */
12192 return NO_REGS;
12193
12194 if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
12195 /* We can force the constant to memory and use lwc1
12196 and ldc1. As above, we will use pairs of lwc1s if
12197 ldc1 is not supported. */
12198 return NO_REGS;
12199
12200 if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
12201 /* In this case we can use mov.fmt. */
12202 return NO_REGS;
12203
12204 /* Otherwise, we need to reload through an integer register. */
12205 return GR_REGS;
12206 }
12207 if (FP_REG_P (regno))
12208 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12209
12210 return NO_REGS;
12211 }
12212
12213 /* Implement TARGET_MODE_REP_EXTENDED. */
12214
12215 static int
12216 mips_mode_rep_extended (enum machine_mode mode, enum machine_mode mode_rep)
12217 {
12218 /* On 64-bit targets, SImode register values are sign-extended to DImode. */
12219 if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
12220 return SIGN_EXTEND;
12221
12222 return UNKNOWN;
12223 }
12224 \f
12225 /* Implement TARGET_VALID_POINTER_MODE. */
12226
12227 static bool
12228 mips_valid_pointer_mode (enum machine_mode mode)
12229 {
12230 return mode == SImode || (TARGET_64BIT && mode == DImode);
12231 }
12232
12233 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P. */
12234
12235 static bool
12236 mips_vector_mode_supported_p (enum machine_mode mode)
12237 {
12238 switch (mode)
12239 {
12240 case V2SFmode:
12241 return TARGET_PAIRED_SINGLE_FLOAT;
12242
12243 case V2HImode:
12244 case V4QImode:
12245 case V2HQmode:
12246 case V2UHQmode:
12247 case V2HAmode:
12248 case V2UHAmode:
12249 case V4QQmode:
12250 case V4UQQmode:
12251 return TARGET_DSP;
12252
12253 case V2SImode:
12254 case V4HImode:
12255 case V8QImode:
12256 return TARGET_LOONGSON_VECTORS;
12257
12258 default:
12259 return false;
12260 }
12261 }
12262
12263 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P. */
12264
12265 static bool
12266 mips_scalar_mode_supported_p (enum machine_mode mode)
12267 {
12268 if (ALL_FIXED_POINT_MODE_P (mode)
12269 && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
12270 return true;
12271
12272 return default_scalar_mode_supported_p (mode);
12273 }
12274 \f
12275 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE. */
12276
12277 static enum machine_mode
12278 mips_preferred_simd_mode (enum machine_mode mode ATTRIBUTE_UNUSED)
12279 {
12280 if (TARGET_PAIRED_SINGLE_FLOAT
12281 && mode == SFmode)
12282 return V2SFmode;
12283 return word_mode;
12284 }
12285
12286 /* Implement TARGET_INIT_LIBFUNCS. */
12287
12288 static void
12289 mips_init_libfuncs (void)
12290 {
12291 if (TARGET_FIX_VR4120)
12292 {
12293 /* Register the special divsi3 and modsi3 functions needed to work
12294 around VR4120 division errata. */
12295 set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
12296 set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
12297 }
12298
12299 if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
12300 {
12301 /* Register the MIPS16 -mhard-float stubs. */
12302 set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
12303 set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
12304 set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
12305 set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
12306
12307 set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
12308 set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
12309 set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
12310 set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
12311 set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
12312 set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
12313 set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
12314
12315 set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
12316 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
12317 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
12318
12319 if (TARGET_DOUBLE_FLOAT)
12320 {
12321 set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
12322 set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
12323 set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
12324 set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
12325
12326 set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
12327 set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
12328 set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
12329 set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
12330 set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
12331 set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
12332 set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
12333
12334 set_conv_libfunc (sext_optab, DFmode, SFmode,
12335 "__mips16_extendsfdf2");
12336 set_conv_libfunc (trunc_optab, SFmode, DFmode,
12337 "__mips16_truncdfsf2");
12338 set_conv_libfunc (sfix_optab, SImode, DFmode,
12339 "__mips16_fix_truncdfsi");
12340 set_conv_libfunc (sfloat_optab, DFmode, SImode,
12341 "__mips16_floatsidf");
12342 set_conv_libfunc (ufloat_optab, DFmode, SImode,
12343 "__mips16_floatunsidf");
12344 }
12345 }
12346
12347 /* The MIPS16 ISA does not have an encoding for "sync", so we rely
12348 on an external non-MIPS16 routine to implement __sync_synchronize.
12349 Similarly for the rest of the ll/sc libfuncs. */
12350 if (TARGET_MIPS16)
12351 {
12352 synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
12353 init_sync_libfuncs (UNITS_PER_WORD);
12354 }
12355 }
12356
12357 /* Build up a multi-insn sequence that loads label TARGET into $AT. */
12358
12359 static void
12360 mips_process_load_label (rtx target)
12361 {
12362 rtx base, gp, intop;
12363 HOST_WIDE_INT offset;
12364
12365 mips_multi_start ();
12366 switch (mips_abi)
12367 {
12368 case ABI_N32:
12369 mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
12370 mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
12371 break;
12372
12373 case ABI_64:
12374 mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
12375 mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
12376 break;
12377
12378 default:
12379 gp = pic_offset_table_rtx;
12380 if (mips_cfun_has_cprestore_slot_p ())
12381 {
12382 gp = gen_rtx_REG (Pmode, AT_REGNUM);
12383 mips_get_cprestore_base_and_offset (&base, &offset, true);
12384 if (!SMALL_OPERAND (offset))
12385 {
12386 intop = GEN_INT (CONST_HIGH_PART (offset));
12387 mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
12388 mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
12389
12390 base = gp;
12391 offset = CONST_LOW_PART (offset);
12392 }
12393 intop = GEN_INT (offset);
12394 if (ISA_HAS_LOAD_DELAY)
12395 mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
12396 else
12397 mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
12398 }
12399 if (ISA_HAS_LOAD_DELAY)
12400 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
12401 else
12402 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
12403 mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
12404 break;
12405 }
12406 }
12407
12408 /* Return the number of instructions needed to load a label into $AT. */
12409
12410 static unsigned int
12411 mips_load_label_num_insns (void)
12412 {
12413 if (cfun->machine->load_label_num_insns == 0)
12414 {
12415 mips_process_load_label (pc_rtx);
12416 cfun->machine->load_label_num_insns = mips_multi_num_insns;
12417 }
12418 return cfun->machine->load_label_num_insns;
12419 }
12420
12421 /* Emit an asm sequence to start a noat block and load the address
12422 of a label into $1. */
12423
12424 void
12425 mips_output_load_label (rtx target)
12426 {
12427 mips_push_asm_switch (&mips_noat);
12428 if (TARGET_EXPLICIT_RELOCS)
12429 {
12430 mips_process_load_label (target);
12431 mips_multi_write ();
12432 }
12433 else
12434 {
12435 if (Pmode == DImode)
12436 output_asm_insn ("dla\t%@,%0", &target);
12437 else
12438 output_asm_insn ("la\t%@,%0", &target);
12439 }
12440 }
12441
12442 /* Return the length of INSN. LENGTH is the initial length computed by
12443 attributes in the machine-description file. */
12444
12445 int
12446 mips_adjust_insn_length (rtx insn, int length)
12447 {
12448 /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
12449 of a PIC long-branch sequence. Substitute the correct value. */
12450 if (length == MAX_PIC_BRANCH_LENGTH
12451 && JUMP_P (insn)
12452 && INSN_CODE (insn) >= 0
12453 && get_attr_type (insn) == TYPE_BRANCH)
12454 {
12455 /* Add the branch-over instruction and its delay slot, if this
12456 is a conditional branch. */
12457 length = simplejump_p (insn) ? 0 : 8;
12458
12459 /* Add the size of a load into $AT. */
12460 length += BASE_INSN_LENGTH * mips_load_label_num_insns ();
12461
12462 /* Add the length of an indirect jump, ignoring the delay slot. */
12463 length += TARGET_COMPRESSION ? 2 : 4;
12464 }
12465
12466 /* A unconditional jump has an unfilled delay slot if it is not part
12467 of a sequence. A conditional jump normally has a delay slot, but
12468 does not on MIPS16. */
12469 if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
12470 length += TARGET_MIPS16 ? 2 : 4;
12471
12472 /* See how many nops might be needed to avoid hardware hazards. */
12473 if (!cfun->machine->ignore_hazard_length_p
12474 && INSN_P (insn)
12475 && INSN_CODE (insn) >= 0)
12476 switch (get_attr_hazard (insn))
12477 {
12478 case HAZARD_NONE:
12479 break;
12480
12481 case HAZARD_DELAY:
12482 length += NOP_INSN_LENGTH;
12483 break;
12484
12485 case HAZARD_HILO:
12486 length += NOP_INSN_LENGTH * 2;
12487 break;
12488 }
12489
12490 return length;
12491 }
12492
12493 /* Return the assembly code for INSN, which has the operands given by
12494 OPERANDS, and which branches to OPERANDS[0] if some condition is true.
12495 BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
12496 is in range of a direct branch. BRANCH_IF_FALSE is an inverted
12497 version of BRANCH_IF_TRUE. */
12498
12499 const char *
12500 mips_output_conditional_branch (rtx insn, rtx *operands,
12501 const char *branch_if_true,
12502 const char *branch_if_false)
12503 {
12504 unsigned int length;
12505 rtx taken, not_taken;
12506
12507 gcc_assert (LABEL_P (operands[0]));
12508
12509 length = get_attr_length (insn);
12510 if (length <= 8)
12511 {
12512 /* Just a simple conditional branch. */
12513 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
12514 return branch_if_true;
12515 }
12516
12517 /* Generate a reversed branch around a direct jump. This fallback does
12518 not use branch-likely instructions. */
12519 mips_branch_likely = false;
12520 not_taken = gen_label_rtx ();
12521 taken = operands[0];
12522
12523 /* Generate the reversed branch to NOT_TAKEN. */
12524 operands[0] = not_taken;
12525 output_asm_insn (branch_if_false, operands);
12526
12527 /* If INSN has a delay slot, we must provide delay slots for both the
12528 branch to NOT_TAKEN and the conditional jump. We must also ensure
12529 that INSN's delay slot is executed in the appropriate cases. */
12530 if (final_sequence)
12531 {
12532 /* This first delay slot will always be executed, so use INSN's
12533 delay slot if is not annulled. */
12534 if (!INSN_ANNULLED_BRANCH_P (insn))
12535 {
12536 final_scan_insn (XVECEXP (final_sequence, 0, 1),
12537 asm_out_file, optimize, 1, NULL);
12538 INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
12539 }
12540 else
12541 output_asm_insn ("nop", 0);
12542 fprintf (asm_out_file, "\n");
12543 }
12544
12545 /* Output the unconditional branch to TAKEN. */
12546 if (TARGET_ABSOLUTE_JUMPS)
12547 output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
12548 else
12549 {
12550 mips_output_load_label (taken);
12551 output_asm_insn ("jr\t%@%]%/", 0);
12552 }
12553
12554 /* Now deal with its delay slot; see above. */
12555 if (final_sequence)
12556 {
12557 /* This delay slot will only be executed if the branch is taken.
12558 Use INSN's delay slot if is annulled. */
12559 if (INSN_ANNULLED_BRANCH_P (insn))
12560 {
12561 final_scan_insn (XVECEXP (final_sequence, 0, 1),
12562 asm_out_file, optimize, 1, NULL);
12563 INSN_DELETED_P (XVECEXP (final_sequence, 0, 1)) = 1;
12564 }
12565 else
12566 output_asm_insn ("nop", 0);
12567 fprintf (asm_out_file, "\n");
12568 }
12569
12570 /* Output NOT_TAKEN. */
12571 targetm.asm_out.internal_label (asm_out_file, "L",
12572 CODE_LABEL_NUMBER (not_taken));
12573 return "";
12574 }
12575
12576 /* Return the assembly code for INSN, which branches to OPERANDS[0]
12577 if some ordering condition is true. The condition is given by
12578 OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
12579 OPERANDS[1]. OPERANDS[2] is the comparison's first operand;
12580 its second is always zero. */
12581
12582 const char *
12583 mips_output_order_conditional_branch (rtx insn, rtx *operands, bool inverted_p)
12584 {
12585 const char *branch[2];
12586
12587 /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
12588 Make BRANCH[0] branch on the inverse condition. */
12589 switch (GET_CODE (operands[1]))
12590 {
12591 /* These cases are equivalent to comparisons against zero. */
12592 case LEU:
12593 inverted_p = !inverted_p;
12594 /* Fall through. */
12595 case GTU:
12596 branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
12597 branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
12598 break;
12599
12600 /* These cases are always true or always false. */
12601 case LTU:
12602 inverted_p = !inverted_p;
12603 /* Fall through. */
12604 case GEU:
12605 branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
12606 branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
12607 break;
12608
12609 default:
12610 branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
12611 branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
12612 break;
12613 }
12614 return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
12615 }
12616 \f
12617 /* Start a block of code that needs access to the LL, SC and SYNC
12618 instructions. */
12619
12620 static void
12621 mips_start_ll_sc_sync_block (void)
12622 {
12623 if (!ISA_HAS_LL_SC)
12624 {
12625 output_asm_insn (".set\tpush", 0);
12626 if (TARGET_64BIT)
12627 output_asm_insn (".set\tmips3", 0);
12628 else
12629 output_asm_insn (".set\tmips2", 0);
12630 }
12631 }
12632
12633 /* End a block started by mips_start_ll_sc_sync_block. */
12634
12635 static void
12636 mips_end_ll_sc_sync_block (void)
12637 {
12638 if (!ISA_HAS_LL_SC)
12639 output_asm_insn (".set\tpop", 0);
12640 }
12641
12642 /* Output and/or return the asm template for a sync instruction. */
12643
12644 const char *
12645 mips_output_sync (void)
12646 {
12647 mips_start_ll_sc_sync_block ();
12648 output_asm_insn ("sync", 0);
12649 mips_end_ll_sc_sync_block ();
12650 return "";
12651 }
12652
12653 /* Return the asm template associated with sync_insn1 value TYPE.
12654 IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation. */
12655
12656 static const char *
12657 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
12658 {
12659 switch (type)
12660 {
12661 case SYNC_INSN1_MOVE:
12662 return "move\t%0,%z2";
12663 case SYNC_INSN1_LI:
12664 return "li\t%0,%2";
12665 case SYNC_INSN1_ADDU:
12666 return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
12667 case SYNC_INSN1_ADDIU:
12668 return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
12669 case SYNC_INSN1_SUBU:
12670 return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
12671 case SYNC_INSN1_AND:
12672 return "and\t%0,%1,%z2";
12673 case SYNC_INSN1_ANDI:
12674 return "andi\t%0,%1,%2";
12675 case SYNC_INSN1_OR:
12676 return "or\t%0,%1,%z2";
12677 case SYNC_INSN1_ORI:
12678 return "ori\t%0,%1,%2";
12679 case SYNC_INSN1_XOR:
12680 return "xor\t%0,%1,%z2";
12681 case SYNC_INSN1_XORI:
12682 return "xori\t%0,%1,%2";
12683 }
12684 gcc_unreachable ();
12685 }
12686
12687 /* Return the asm template associated with sync_insn2 value TYPE. */
12688
12689 static const char *
12690 mips_sync_insn2_template (enum attr_sync_insn2 type)
12691 {
12692 switch (type)
12693 {
12694 case SYNC_INSN2_NOP:
12695 gcc_unreachable ();
12696 case SYNC_INSN2_AND:
12697 return "and\t%0,%1,%z2";
12698 case SYNC_INSN2_XOR:
12699 return "xor\t%0,%1,%z2";
12700 case SYNC_INSN2_NOT:
12701 return "nor\t%0,%1,%.";
12702 }
12703 gcc_unreachable ();
12704 }
12705
12706 /* OPERANDS are the operands to a sync loop instruction and INDEX is
12707 the value of the one of the sync_* attributes. Return the operand
12708 referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
12709 have the associated attribute. */
12710
12711 static rtx
12712 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
12713 {
12714 if (index > 0)
12715 default_value = operands[index - 1];
12716 return default_value;
12717 }
12718
12719 /* INSN is a sync loop with operands OPERANDS. Build up a multi-insn
12720 sequence for it. */
12721
12722 static void
12723 mips_process_sync_loop (rtx insn, rtx *operands)
12724 {
12725 rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
12726 rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3, cmp;
12727 unsigned int tmp3_insn;
12728 enum attr_sync_insn1 insn1;
12729 enum attr_sync_insn2 insn2;
12730 bool is_64bit_p;
12731 int memmodel_attr;
12732 enum memmodel model;
12733
12734 /* Read an operand from the sync_WHAT attribute and store it in
12735 variable WHAT. DEFAULT is the default value if no attribute
12736 is specified. */
12737 #define READ_OPERAND(WHAT, DEFAULT) \
12738 WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
12739 DEFAULT)
12740
12741 /* Read the memory. */
12742 READ_OPERAND (mem, 0);
12743 gcc_assert (mem);
12744 is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
12745
12746 /* Read the other attributes. */
12747 at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
12748 READ_OPERAND (oldval, at);
12749 READ_OPERAND (cmp, 0);
12750 READ_OPERAND (newval, at);
12751 READ_OPERAND (inclusive_mask, 0);
12752 READ_OPERAND (exclusive_mask, 0);
12753 READ_OPERAND (required_oldval, 0);
12754 READ_OPERAND (insn1_op2, 0);
12755 insn1 = get_attr_sync_insn1 (insn);
12756 insn2 = get_attr_sync_insn2 (insn);
12757
12758 /* Don't bother setting CMP result that is never used. */
12759 if (cmp && find_reg_note (insn, REG_UNUSED, cmp))
12760 cmp = 0;
12761
12762 memmodel_attr = get_attr_sync_memmodel (insn);
12763 switch (memmodel_attr)
12764 {
12765 case 10:
12766 model = MEMMODEL_ACQ_REL;
12767 break;
12768 case 11:
12769 model = MEMMODEL_ACQUIRE;
12770 break;
12771 default:
12772 model = (enum memmodel) INTVAL (operands[memmodel_attr]);
12773 }
12774
12775 mips_multi_start ();
12776
12777 /* Output the release side of the memory barrier. */
12778 if (need_atomic_barrier_p (model, true))
12779 {
12780 if (required_oldval == 0 && TARGET_OCTEON)
12781 {
12782 /* Octeon doesn't reorder reads, so a full barrier can be
12783 created by using SYNCW to order writes combined with the
12784 write from the following SC. When the SC successfully
12785 completes, we know that all preceding writes are also
12786 committed to the coherent memory system. It is possible
12787 for a single SYNCW to fail, but a pair of them will never
12788 fail, so we use two. */
12789 mips_multi_add_insn ("syncw", NULL);
12790 mips_multi_add_insn ("syncw", NULL);
12791 }
12792 else
12793 mips_multi_add_insn ("sync", NULL);
12794 }
12795
12796 /* Output the branch-back label. */
12797 mips_multi_add_label ("1:");
12798
12799 /* OLDVAL = *MEM. */
12800 mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
12801 oldval, mem, NULL);
12802
12803 /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2. */
12804 if (required_oldval)
12805 {
12806 if (inclusive_mask == 0)
12807 tmp1 = oldval;
12808 else
12809 {
12810 gcc_assert (oldval != at);
12811 mips_multi_add_insn ("and\t%0,%1,%2",
12812 at, oldval, inclusive_mask, NULL);
12813 tmp1 = at;
12814 }
12815 mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
12816
12817 /* CMP = 0 [delay slot]. */
12818 if (cmp)
12819 mips_multi_add_insn ("li\t%0,0", cmp, NULL);
12820 }
12821
12822 /* $TMP1 = OLDVAL & EXCLUSIVE_MASK. */
12823 if (exclusive_mask == 0)
12824 tmp1 = const0_rtx;
12825 else
12826 {
12827 gcc_assert (oldval != at);
12828 mips_multi_add_insn ("and\t%0,%1,%z2",
12829 at, oldval, exclusive_mask, NULL);
12830 tmp1 = at;
12831 }
12832
12833 /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
12834
12835 We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
12836 at least one instruction in that case. */
12837 if (insn1 == SYNC_INSN1_MOVE
12838 && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
12839 tmp2 = insn1_op2;
12840 else
12841 {
12842 mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
12843 newval, oldval, insn1_op2, NULL);
12844 tmp2 = newval;
12845 }
12846
12847 /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK). */
12848 if (insn2 == SYNC_INSN2_NOP)
12849 tmp3 = tmp2;
12850 else
12851 {
12852 mips_multi_add_insn (mips_sync_insn2_template (insn2),
12853 newval, tmp2, inclusive_mask, NULL);
12854 tmp3 = newval;
12855 }
12856 tmp3_insn = mips_multi_last_index ();
12857
12858 /* $AT = $TMP1 | $TMP3. */
12859 if (tmp1 == const0_rtx || tmp3 == const0_rtx)
12860 {
12861 mips_multi_set_operand (tmp3_insn, 0, at);
12862 tmp3 = at;
12863 }
12864 else
12865 {
12866 gcc_assert (tmp1 != tmp3);
12867 mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
12868 }
12869
12870 /* if (!commit (*MEM = $AT)) goto 1.
12871
12872 This will sometimes be a delayed branch; see the write code below
12873 for details. */
12874 mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
12875 mips_multi_add_insn ("beq%?\t%0,%.,1b", at, NULL);
12876
12877 /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot]. */
12878 if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
12879 {
12880 mips_multi_copy_insn (tmp3_insn);
12881 mips_multi_set_operand (mips_multi_last_index (), 0, newval);
12882 }
12883 else if (!(required_oldval && cmp))
12884 mips_multi_add_insn ("nop", NULL);
12885
12886 /* CMP = 1 -- either standalone or in a delay slot. */
12887 if (required_oldval && cmp)
12888 mips_multi_add_insn ("li\t%0,1", cmp, NULL);
12889
12890 /* Output the acquire side of the memory barrier. */
12891 if (TARGET_SYNC_AFTER_SC && need_atomic_barrier_p (model, false))
12892 mips_multi_add_insn ("sync", NULL);
12893
12894 /* Output the exit label, if needed. */
12895 if (required_oldval)
12896 mips_multi_add_label ("2:");
12897
12898 #undef READ_OPERAND
12899 }
12900
12901 /* Output and/or return the asm template for sync loop INSN, which has
12902 the operands given by OPERANDS. */
12903
12904 const char *
12905 mips_output_sync_loop (rtx insn, rtx *operands)
12906 {
12907 mips_process_sync_loop (insn, operands);
12908
12909 /* Use branch-likely instructions to work around the LL/SC R10000
12910 errata. */
12911 mips_branch_likely = TARGET_FIX_R10000;
12912
12913 mips_push_asm_switch (&mips_noreorder);
12914 mips_push_asm_switch (&mips_nomacro);
12915 mips_push_asm_switch (&mips_noat);
12916 mips_start_ll_sc_sync_block ();
12917
12918 mips_multi_write ();
12919
12920 mips_end_ll_sc_sync_block ();
12921 mips_pop_asm_switch (&mips_noat);
12922 mips_pop_asm_switch (&mips_nomacro);
12923 mips_pop_asm_switch (&mips_noreorder);
12924
12925 return "";
12926 }
12927
12928 /* Return the number of individual instructions in sync loop INSN,
12929 which has the operands given by OPERANDS. */
12930
12931 unsigned int
12932 mips_sync_loop_insns (rtx insn, rtx *operands)
12933 {
12934 mips_process_sync_loop (insn, operands);
12935 return mips_multi_num_insns;
12936 }
12937 \f
12938 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
12939 the operands given by OPERANDS. Add in a divide-by-zero check if needed.
12940
12941 When working around R4000 and R4400 errata, we need to make sure that
12942 the division is not immediately followed by a shift[1][2]. We also
12943 need to stop the division from being put into a branch delay slot[3].
12944 The easiest way to avoid both problems is to add a nop after the
12945 division. When a divide-by-zero check is needed, this nop can be
12946 used to fill the branch delay slot.
12947
12948 [1] If a double-word or a variable shift executes immediately
12949 after starting an integer division, the shift may give an
12950 incorrect result. See quotations of errata #16 and #28 from
12951 "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12952 in mips.md for details.
12953
12954 [2] A similar bug to [1] exists for all revisions of the
12955 R4000 and the R4400 when run in an MC configuration.
12956 From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
12957
12958 "19. In this following sequence:
12959
12960 ddiv (or ddivu or div or divu)
12961 dsll32 (or dsrl32, dsra32)
12962
12963 if an MPT stall occurs, while the divide is slipping the cpu
12964 pipeline, then the following double shift would end up with an
12965 incorrect result.
12966
12967 Workaround: The compiler needs to avoid generating any
12968 sequence with divide followed by extended double shift."
12969
12970 This erratum is also present in "MIPS R4400MC Errata, Processor
12971 Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
12972 & 3.0" as errata #10 and #4, respectively.
12973
12974 [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12975 (also valid for MIPS R4000MC processors):
12976
12977 "52. R4000SC: This bug does not apply for the R4000PC.
12978
12979 There are two flavors of this bug:
12980
12981 1) If the instruction just after divide takes an RF exception
12982 (tlb-refill, tlb-invalid) and gets an instruction cache
12983 miss (both primary and secondary) and the line which is
12984 currently in secondary cache at this index had the first
12985 data word, where the bits 5..2 are set, then R4000 would
12986 get a wrong result for the div.
12987
12988 ##1
12989 nop
12990 div r8, r9
12991 ------------------- # end-of page. -tlb-refill
12992 nop
12993 ##2
12994 nop
12995 div r8, r9
12996 ------------------- # end-of page. -tlb-invalid
12997 nop
12998
12999 2) If the divide is in the taken branch delay slot, where the
13000 target takes RF exception and gets an I-cache miss for the
13001 exception vector or where I-cache miss occurs for the
13002 target address, under the above mentioned scenarios, the
13003 div would get wrong results.
13004
13005 ##1
13006 j r2 # to next page mapped or unmapped
13007 div r8,r9 # this bug would be there as long
13008 # as there is an ICache miss and
13009 nop # the "data pattern" is present
13010
13011 ##2
13012 beq r0, r0, NextPage # to Next page
13013 div r8,r9
13014 nop
13015
13016 This bug is present for div, divu, ddiv, and ddivu
13017 instructions.
13018
13019 Workaround: For item 1), OS could make sure that the next page
13020 after the divide instruction is also mapped. For item 2), the
13021 compiler could make sure that the divide instruction is not in
13022 the branch delay slot."
13023
13024 These processors have PRId values of 0x00004220 and 0x00004300 for
13025 the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400. */
13026
13027 const char *
13028 mips_output_division (const char *division, rtx *operands)
13029 {
13030 const char *s;
13031
13032 s = division;
13033 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
13034 {
13035 output_asm_insn (s, operands);
13036 s = "nop";
13037 }
13038 if (TARGET_CHECK_ZERO_DIV)
13039 {
13040 if (TARGET_MIPS16)
13041 {
13042 output_asm_insn (s, operands);
13043 s = "bnez\t%2,1f\n\tbreak\t7\n1:";
13044 }
13045 else if (GENERATE_DIVIDE_TRAPS)
13046 {
13047 /* Avoid long replay penalty on load miss by putting the trap before
13048 the divide. */
13049 if (TUNE_74K)
13050 output_asm_insn ("teq\t%2,%.,7", operands);
13051 else
13052 {
13053 output_asm_insn (s, operands);
13054 s = "teq\t%2,%.,7";
13055 }
13056 }
13057 else
13058 {
13059 output_asm_insn ("%(bne\t%2,%.,1f", operands);
13060 output_asm_insn (s, operands);
13061 s = "break\t7%)\n1:";
13062 }
13063 }
13064 return s;
13065 }
13066 \f
13067 /* Return true if IN_INSN is a multiply-add or multiply-subtract
13068 instruction and if OUT_INSN assigns to the accumulator operand. */
13069
13070 bool
13071 mips_linked_madd_p (rtx out_insn, rtx in_insn)
13072 {
13073 enum attr_accum_in accum_in;
13074 int accum_in_opnum;
13075 rtx accum_in_op;
13076
13077 if (recog_memoized (in_insn) < 0)
13078 return false;
13079
13080 accum_in = get_attr_accum_in (in_insn);
13081 if (accum_in == ACCUM_IN_NONE)
13082 return false;
13083
13084 accum_in_opnum = accum_in - ACCUM_IN_0;
13085
13086 extract_insn (in_insn);
13087 gcc_assert (accum_in_opnum < recog_data.n_operands);
13088 accum_in_op = recog_data.operand[accum_in_opnum];
13089
13090 return reg_set_p (accum_in_op, out_insn);
13091 }
13092
13093 /* True if the dependency between OUT_INSN and IN_INSN is on the store
13094 data rather than the address. We need this because the cprestore
13095 pattern is type "store", but is defined using an UNSPEC_VOLATILE,
13096 which causes the default routine to abort. We just return false
13097 for that case. */
13098
13099 bool
13100 mips_store_data_bypass_p (rtx out_insn, rtx in_insn)
13101 {
13102 if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
13103 return false;
13104
13105 return !store_data_bypass_p (out_insn, in_insn);
13106 }
13107 \f
13108
13109 /* Variables and flags used in scheduler hooks when tuning for
13110 Loongson 2E/2F. */
13111 static struct
13112 {
13113 /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
13114 strategy. */
13115
13116 /* If true, then next ALU1/2 instruction will go to ALU1. */
13117 bool alu1_turn_p;
13118
13119 /* If true, then next FALU1/2 unstruction will go to FALU1. */
13120 bool falu1_turn_p;
13121
13122 /* Codes to query if [f]alu{1,2}_core units are subscribed or not. */
13123 int alu1_core_unit_code;
13124 int alu2_core_unit_code;
13125 int falu1_core_unit_code;
13126 int falu2_core_unit_code;
13127
13128 /* True if current cycle has a multi instruction.
13129 This flag is used in mips_ls2_dfa_post_advance_cycle. */
13130 bool cycle_has_multi_p;
13131
13132 /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
13133 These are used in mips_ls2_dfa_post_advance_cycle to initialize
13134 DFA state.
13135 E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
13136 instruction to go ALU1. */
13137 rtx alu1_turn_enabled_insn;
13138 rtx alu2_turn_enabled_insn;
13139 rtx falu1_turn_enabled_insn;
13140 rtx falu2_turn_enabled_insn;
13141 } mips_ls2;
13142
13143 /* Implement TARGET_SCHED_ADJUST_COST. We assume that anti and output
13144 dependencies have no cost, except on the 20Kc where output-dependence
13145 is treated like input-dependence. */
13146
13147 static int
13148 mips_adjust_cost (rtx insn ATTRIBUTE_UNUSED, rtx link,
13149 rtx dep ATTRIBUTE_UNUSED, int cost)
13150 {
13151 if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
13152 && TUNE_20KC)
13153 return cost;
13154 if (REG_NOTE_KIND (link) != 0)
13155 return 0;
13156 return cost;
13157 }
13158
13159 /* Return the number of instructions that can be issued per cycle. */
13160
13161 static int
13162 mips_issue_rate (void)
13163 {
13164 switch (mips_tune)
13165 {
13166 case PROCESSOR_74KC:
13167 case PROCESSOR_74KF2_1:
13168 case PROCESSOR_74KF1_1:
13169 case PROCESSOR_74KF3_2:
13170 /* The 74k is not strictly quad-issue cpu, but can be seen as one
13171 by the scheduler. It can issue 1 ALU, 1 AGEN and 2 FPU insns,
13172 but in reality only a maximum of 3 insns can be issued as
13173 floating-point loads and stores also require a slot in the
13174 AGEN pipe. */
13175 case PROCESSOR_R10000:
13176 /* All R10K Processors are quad-issue (being the first MIPS
13177 processors to support this feature). */
13178 return 4;
13179
13180 case PROCESSOR_20KC:
13181 case PROCESSOR_R4130:
13182 case PROCESSOR_R5400:
13183 case PROCESSOR_R5500:
13184 case PROCESSOR_R5900:
13185 case PROCESSOR_R7000:
13186 case PROCESSOR_R9000:
13187 case PROCESSOR_OCTEON:
13188 case PROCESSOR_OCTEON2:
13189 return 2;
13190
13191 case PROCESSOR_SB1:
13192 case PROCESSOR_SB1A:
13193 /* This is actually 4, but we get better performance if we claim 3.
13194 This is partly because of unwanted speculative code motion with the
13195 larger number, and partly because in most common cases we can't
13196 reach the theoretical max of 4. */
13197 return 3;
13198
13199 case PROCESSOR_LOONGSON_2E:
13200 case PROCESSOR_LOONGSON_2F:
13201 case PROCESSOR_LOONGSON_3A:
13202 return 4;
13203
13204 case PROCESSOR_XLP:
13205 return (reload_completed ? 4 : 3);
13206
13207 default:
13208 return 1;
13209 }
13210 }
13211
13212 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2. */
13213
13214 static void
13215 mips_ls2_init_dfa_post_cycle_insn (void)
13216 {
13217 start_sequence ();
13218 emit_insn (gen_ls2_alu1_turn_enabled_insn ());
13219 mips_ls2.alu1_turn_enabled_insn = get_insns ();
13220 end_sequence ();
13221
13222 start_sequence ();
13223 emit_insn (gen_ls2_alu2_turn_enabled_insn ());
13224 mips_ls2.alu2_turn_enabled_insn = get_insns ();
13225 end_sequence ();
13226
13227 start_sequence ();
13228 emit_insn (gen_ls2_falu1_turn_enabled_insn ());
13229 mips_ls2.falu1_turn_enabled_insn = get_insns ();
13230 end_sequence ();
13231
13232 start_sequence ();
13233 emit_insn (gen_ls2_falu2_turn_enabled_insn ());
13234 mips_ls2.falu2_turn_enabled_insn = get_insns ();
13235 end_sequence ();
13236
13237 mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
13238 mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
13239 mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
13240 mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
13241 }
13242
13243 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
13244 Init data used in mips_dfa_post_advance_cycle. */
13245
13246 static void
13247 mips_init_dfa_post_cycle_insn (void)
13248 {
13249 if (TUNE_LOONGSON_2EF)
13250 mips_ls2_init_dfa_post_cycle_insn ();
13251 }
13252
13253 /* Initialize STATE when scheduling for Loongson 2E/2F.
13254 Support round-robin dispatch scheme by enabling only one of
13255 ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
13256 respectively. */
13257
13258 static void
13259 mips_ls2_dfa_post_advance_cycle (state_t state)
13260 {
13261 if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
13262 {
13263 /* Though there are no non-pipelined ALU1 insns,
13264 we can get an instruction of type 'multi' before reload. */
13265 gcc_assert (mips_ls2.cycle_has_multi_p);
13266 mips_ls2.alu1_turn_p = false;
13267 }
13268
13269 mips_ls2.cycle_has_multi_p = false;
13270
13271 if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
13272 /* We have a non-pipelined alu instruction in the core,
13273 adjust round-robin counter. */
13274 mips_ls2.alu1_turn_p = true;
13275
13276 if (mips_ls2.alu1_turn_p)
13277 {
13278 if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
13279 gcc_unreachable ();
13280 }
13281 else
13282 {
13283 if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
13284 gcc_unreachable ();
13285 }
13286
13287 if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
13288 {
13289 /* There are no non-pipelined FALU1 insns. */
13290 gcc_unreachable ();
13291 mips_ls2.falu1_turn_p = false;
13292 }
13293
13294 if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
13295 /* We have a non-pipelined falu instruction in the core,
13296 adjust round-robin counter. */
13297 mips_ls2.falu1_turn_p = true;
13298
13299 if (mips_ls2.falu1_turn_p)
13300 {
13301 if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
13302 gcc_unreachable ();
13303 }
13304 else
13305 {
13306 if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
13307 gcc_unreachable ();
13308 }
13309 }
13310
13311 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
13312 This hook is being called at the start of each cycle. */
13313
13314 static void
13315 mips_dfa_post_advance_cycle (void)
13316 {
13317 if (TUNE_LOONGSON_2EF)
13318 mips_ls2_dfa_post_advance_cycle (curr_state);
13319 }
13320
13321 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD. This should
13322 be as wide as the scheduling freedom in the DFA. */
13323
13324 static int
13325 mips_multipass_dfa_lookahead (void)
13326 {
13327 /* Can schedule up to 4 of the 6 function units in any one cycle. */
13328 if (TUNE_SB1)
13329 return 4;
13330
13331 if (TUNE_LOONGSON_2EF || TUNE_LOONGSON_3A)
13332 return 4;
13333
13334 if (TUNE_OCTEON)
13335 return 2;
13336
13337 return 0;
13338 }
13339 \f
13340 /* Remove the instruction at index LOWER from ready queue READY and
13341 reinsert it in front of the instruction at index HIGHER. LOWER must
13342 be <= HIGHER. */
13343
13344 static void
13345 mips_promote_ready (rtx *ready, int lower, int higher)
13346 {
13347 rtx new_head;
13348 int i;
13349
13350 new_head = ready[lower];
13351 for (i = lower; i < higher; i++)
13352 ready[i] = ready[i + 1];
13353 ready[i] = new_head;
13354 }
13355
13356 /* If the priority of the instruction at POS2 in the ready queue READY
13357 is within LIMIT units of that of the instruction at POS1, swap the
13358 instructions if POS2 is not already less than POS1. */
13359
13360 static void
13361 mips_maybe_swap_ready (rtx *ready, int pos1, int pos2, int limit)
13362 {
13363 if (pos1 < pos2
13364 && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
13365 {
13366 rtx temp;
13367
13368 temp = ready[pos1];
13369 ready[pos1] = ready[pos2];
13370 ready[pos2] = temp;
13371 }
13372 }
13373 \f
13374 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
13375 that may clobber hi or lo. */
13376 static rtx mips_macc_chains_last_hilo;
13377
13378 /* A TUNE_MACC_CHAINS helper function. Record that instruction INSN has
13379 been scheduled, updating mips_macc_chains_last_hilo appropriately. */
13380
13381 static void
13382 mips_macc_chains_record (rtx insn)
13383 {
13384 if (get_attr_may_clobber_hilo (insn))
13385 mips_macc_chains_last_hilo = insn;
13386 }
13387
13388 /* A TUNE_MACC_CHAINS helper function. Search ready queue READY, which
13389 has NREADY elements, looking for a multiply-add or multiply-subtract
13390 instruction that is cumulative with mips_macc_chains_last_hilo.
13391 If there is one, promote it ahead of anything else that might
13392 clobber hi or lo. */
13393
13394 static void
13395 mips_macc_chains_reorder (rtx *ready, int nready)
13396 {
13397 int i, j;
13398
13399 if (mips_macc_chains_last_hilo != 0)
13400 for (i = nready - 1; i >= 0; i--)
13401 if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
13402 {
13403 for (j = nready - 1; j > i; j--)
13404 if (recog_memoized (ready[j]) >= 0
13405 && get_attr_may_clobber_hilo (ready[j]))
13406 {
13407 mips_promote_ready (ready, i, j);
13408 break;
13409 }
13410 break;
13411 }
13412 }
13413 \f
13414 /* The last instruction to be scheduled. */
13415 static rtx vr4130_last_insn;
13416
13417 /* A note_stores callback used by vr4130_true_reg_dependence_p. DATA
13418 points to an rtx that is initially an instruction. Nullify the rtx
13419 if the instruction uses the value of register X. */
13420
13421 static void
13422 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
13423 void *data)
13424 {
13425 rtx *insn_ptr;
13426
13427 insn_ptr = (rtx *) data;
13428 if (REG_P (x)
13429 && *insn_ptr != 0
13430 && reg_referenced_p (x, PATTERN (*insn_ptr)))
13431 *insn_ptr = 0;
13432 }
13433
13434 /* Return true if there is true register dependence between vr4130_last_insn
13435 and INSN. */
13436
13437 static bool
13438 vr4130_true_reg_dependence_p (rtx insn)
13439 {
13440 note_stores (PATTERN (vr4130_last_insn),
13441 vr4130_true_reg_dependence_p_1, &insn);
13442 return insn == 0;
13443 }
13444
13445 /* A TUNE_MIPS4130 helper function. Given that INSN1 is at the head of
13446 the ready queue and that INSN2 is the instruction after it, return
13447 true if it is worth promoting INSN2 ahead of INSN1. Look for cases
13448 in which INSN1 and INSN2 can probably issue in parallel, but for
13449 which (INSN2, INSN1) should be less sensitive to instruction
13450 alignment than (INSN1, INSN2). See 4130.md for more details. */
13451
13452 static bool
13453 vr4130_swap_insns_p (rtx insn1, rtx insn2)
13454 {
13455 sd_iterator_def sd_it;
13456 dep_t dep;
13457
13458 /* Check for the following case:
13459
13460 1) there is some other instruction X with an anti dependence on INSN1;
13461 2) X has a higher priority than INSN2; and
13462 3) X is an arithmetic instruction (and thus has no unit restrictions).
13463
13464 If INSN1 is the last instruction blocking X, it would better to
13465 choose (INSN1, X) over (INSN2, INSN1). */
13466 FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
13467 if (DEP_TYPE (dep) == REG_DEP_ANTI
13468 && INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
13469 && recog_memoized (DEP_CON (dep)) >= 0
13470 && get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
13471 return false;
13472
13473 if (vr4130_last_insn != 0
13474 && recog_memoized (insn1) >= 0
13475 && recog_memoized (insn2) >= 0)
13476 {
13477 /* See whether INSN1 and INSN2 use different execution units,
13478 or if they are both ALU-type instructions. If so, they can
13479 probably execute in parallel. */
13480 enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
13481 enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
13482 if (class1 != class2 || class1 == VR4130_CLASS_ALU)
13483 {
13484 /* If only one of the instructions has a dependence on
13485 vr4130_last_insn, prefer to schedule the other one first. */
13486 bool dep1_p = vr4130_true_reg_dependence_p (insn1);
13487 bool dep2_p = vr4130_true_reg_dependence_p (insn2);
13488 if (dep1_p != dep2_p)
13489 return dep1_p;
13490
13491 /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
13492 is not an ALU-type instruction and if INSN1 uses the same
13493 execution unit. (Note that if this condition holds, we already
13494 know that INSN2 uses a different execution unit.) */
13495 if (class1 != VR4130_CLASS_ALU
13496 && recog_memoized (vr4130_last_insn) >= 0
13497 && class1 == get_attr_vr4130_class (vr4130_last_insn))
13498 return true;
13499 }
13500 }
13501 return false;
13502 }
13503
13504 /* A TUNE_MIPS4130 helper function. (READY, NREADY) describes a ready
13505 queue with at least two instructions. Swap the first two if
13506 vr4130_swap_insns_p says that it could be worthwhile. */
13507
13508 static void
13509 vr4130_reorder (rtx *ready, int nready)
13510 {
13511 if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
13512 mips_promote_ready (ready, nready - 2, nready - 1);
13513 }
13514 \f
13515 /* Record whether last 74k AGEN instruction was a load or store. */
13516 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
13517
13518 /* Initialize mips_last_74k_agen_insn from INSN. A null argument
13519 resets to TYPE_UNKNOWN state. */
13520
13521 static void
13522 mips_74k_agen_init (rtx insn)
13523 {
13524 if (!insn || CALL_P (insn) || JUMP_P (insn))
13525 mips_last_74k_agen_insn = TYPE_UNKNOWN;
13526 else
13527 {
13528 enum attr_type type = get_attr_type (insn);
13529 if (type == TYPE_LOAD || type == TYPE_STORE)
13530 mips_last_74k_agen_insn = type;
13531 }
13532 }
13533
13534 /* A TUNE_74K helper function. The 74K AGEN pipeline likes multiple
13535 loads to be grouped together, and multiple stores to be grouped
13536 together. Swap things around in the ready queue to make this happen. */
13537
13538 static void
13539 mips_74k_agen_reorder (rtx *ready, int nready)
13540 {
13541 int i;
13542 int store_pos, load_pos;
13543
13544 store_pos = -1;
13545 load_pos = -1;
13546
13547 for (i = nready - 1; i >= 0; i--)
13548 {
13549 rtx insn = ready[i];
13550 if (USEFUL_INSN_P (insn))
13551 switch (get_attr_type (insn))
13552 {
13553 case TYPE_STORE:
13554 if (store_pos == -1)
13555 store_pos = i;
13556 break;
13557
13558 case TYPE_LOAD:
13559 if (load_pos == -1)
13560 load_pos = i;
13561 break;
13562
13563 default:
13564 break;
13565 }
13566 }
13567
13568 if (load_pos == -1 || store_pos == -1)
13569 return;
13570
13571 switch (mips_last_74k_agen_insn)
13572 {
13573 case TYPE_UNKNOWN:
13574 /* Prefer to schedule loads since they have a higher latency. */
13575 case TYPE_LOAD:
13576 /* Swap loads to the front of the queue. */
13577 mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
13578 break;
13579 case TYPE_STORE:
13580 /* Swap stores to the front of the queue. */
13581 mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
13582 break;
13583 default:
13584 break;
13585 }
13586 }
13587 \f
13588 /* Implement TARGET_SCHED_INIT. */
13589
13590 static void
13591 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13592 int max_ready ATTRIBUTE_UNUSED)
13593 {
13594 mips_macc_chains_last_hilo = 0;
13595 vr4130_last_insn = 0;
13596 mips_74k_agen_init (NULL_RTX);
13597
13598 /* When scheduling for Loongson2, branch instructions go to ALU1,
13599 therefore basic block is most likely to start with round-robin counter
13600 pointed to ALU2. */
13601 mips_ls2.alu1_turn_p = false;
13602 mips_ls2.falu1_turn_p = true;
13603 }
13604
13605 /* Subroutine used by TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2. */
13606
13607 static void
13608 mips_sched_reorder_1 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13609 rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13610 {
13611 if (!reload_completed
13612 && TUNE_MACC_CHAINS
13613 && *nreadyp > 0)
13614 mips_macc_chains_reorder (ready, *nreadyp);
13615
13616 if (reload_completed
13617 && TUNE_MIPS4130
13618 && !TARGET_VR4130_ALIGN
13619 && *nreadyp > 1)
13620 vr4130_reorder (ready, *nreadyp);
13621
13622 if (TUNE_74K)
13623 mips_74k_agen_reorder (ready, *nreadyp);
13624 }
13625
13626 /* Implement TARGET_SCHED_REORDER. */
13627
13628 static int
13629 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13630 rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13631 {
13632 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13633 return mips_issue_rate ();
13634 }
13635
13636 /* Implement TARGET_SCHED_REORDER2. */
13637
13638 static int
13639 mips_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13640 rtx *ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13641 {
13642 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13643 return cached_can_issue_more;
13644 }
13645
13646 /* Update round-robin counters for ALU1/2 and FALU1/2. */
13647
13648 static void
13649 mips_ls2_variable_issue (rtx insn)
13650 {
13651 if (mips_ls2.alu1_turn_p)
13652 {
13653 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
13654 mips_ls2.alu1_turn_p = false;
13655 }
13656 else
13657 {
13658 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
13659 mips_ls2.alu1_turn_p = true;
13660 }
13661
13662 if (mips_ls2.falu1_turn_p)
13663 {
13664 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
13665 mips_ls2.falu1_turn_p = false;
13666 }
13667 else
13668 {
13669 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
13670 mips_ls2.falu1_turn_p = true;
13671 }
13672
13673 if (recog_memoized (insn) >= 0)
13674 mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
13675 }
13676
13677 /* Implement TARGET_SCHED_VARIABLE_ISSUE. */
13678
13679 static int
13680 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13681 rtx insn, int more)
13682 {
13683 /* Ignore USEs and CLOBBERs; don't count them against the issue rate. */
13684 if (USEFUL_INSN_P (insn))
13685 {
13686 if (get_attr_type (insn) != TYPE_GHOST)
13687 more--;
13688 if (!reload_completed && TUNE_MACC_CHAINS)
13689 mips_macc_chains_record (insn);
13690 vr4130_last_insn = insn;
13691 if (TUNE_74K)
13692 mips_74k_agen_init (insn);
13693 else if (TUNE_LOONGSON_2EF)
13694 mips_ls2_variable_issue (insn);
13695 }
13696
13697 /* Instructions of type 'multi' should all be split before
13698 the second scheduling pass. */
13699 gcc_assert (!reload_completed
13700 || recog_memoized (insn) < 0
13701 || get_attr_type (insn) != TYPE_MULTI);
13702
13703 cached_can_issue_more = more;
13704 return more;
13705 }
13706 \f
13707 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
13708 return the first operand of the associated PREF or PREFX insn. */
13709
13710 rtx
13711 mips_prefetch_cookie (rtx write, rtx locality)
13712 {
13713 /* store_streamed / load_streamed. */
13714 if (INTVAL (locality) <= 0)
13715 return GEN_INT (INTVAL (write) + 4);
13716
13717 /* store / load. */
13718 if (INTVAL (locality) <= 2)
13719 return write;
13720
13721 /* store_retained / load_retained. */
13722 return GEN_INT (INTVAL (write) + 6);
13723 }
13724 \f
13725 /* Flags that indicate when a built-in function is available.
13726
13727 BUILTIN_AVAIL_NON_MIPS16
13728 The function is available on the current target if !TARGET_MIPS16.
13729
13730 BUILTIN_AVAIL_MIPS16
13731 The function is available on the current target if TARGET_MIPS16. */
13732 #define BUILTIN_AVAIL_NON_MIPS16 1
13733 #define BUILTIN_AVAIL_MIPS16 2
13734
13735 /* Declare an availability predicate for built-in functions that
13736 require non-MIPS16 mode and also require COND to be true.
13737 NAME is the main part of the predicate's name. */
13738 #define AVAIL_NON_MIPS16(NAME, COND) \
13739 static unsigned int \
13740 mips_builtin_avail_##NAME (void) \
13741 { \
13742 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0; \
13743 }
13744
13745 /* Declare an availability predicate for built-in functions that
13746 support both MIPS16 and non-MIPS16 code and also require COND
13747 to be true. NAME is the main part of the predicate's name. */
13748 #define AVAIL_ALL(NAME, COND) \
13749 static unsigned int \
13750 mips_builtin_avail_##NAME (void) \
13751 { \
13752 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 | BUILTIN_AVAIL_MIPS16 : 0; \
13753 }
13754
13755 /* This structure describes a single built-in function. */
13756 struct mips_builtin_description {
13757 /* The code of the main .md file instruction. See mips_builtin_type
13758 for more information. */
13759 enum insn_code icode;
13760
13761 /* The floating-point comparison code to use with ICODE, if any. */
13762 enum mips_fp_condition cond;
13763
13764 /* The name of the built-in function. */
13765 const char *name;
13766
13767 /* Specifies how the function should be expanded. */
13768 enum mips_builtin_type builtin_type;
13769
13770 /* The function's prototype. */
13771 enum mips_function_type function_type;
13772
13773 /* Whether the function is available. */
13774 unsigned int (*avail) (void);
13775 };
13776
13777 AVAIL_ALL (hard_float, TARGET_HARD_FLOAT_ABI)
13778 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
13779 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
13780 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
13781 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
13782 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
13783 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
13784 AVAIL_NON_MIPS16 (dsp_64, TARGET_64BIT && TARGET_DSP)
13785 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
13786 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_VECTORS)
13787 AVAIL_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
13788
13789 /* Construct a mips_builtin_description from the given arguments.
13790
13791 INSN is the name of the associated instruction pattern, without the
13792 leading CODE_FOR_mips_.
13793
13794 CODE is the floating-point condition code associated with the
13795 function. It can be 'f' if the field is not applicable.
13796
13797 NAME is the name of the function itself, without the leading
13798 "__builtin_mips_".
13799
13800 BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
13801
13802 AVAIL is the name of the availability predicate, without the leading
13803 mips_builtin_avail_. */
13804 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE, \
13805 FUNCTION_TYPE, AVAIL) \
13806 { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND, \
13807 "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE, \
13808 mips_builtin_avail_ ## AVAIL }
13809
13810 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
13811 mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE and AVAIL
13812 are as for MIPS_BUILTIN. */
13813 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
13814 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
13815
13816 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
13817 are subject to mips_builtin_avail_<AVAIL>. */
13818 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL) \
13819 MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s", \
13820 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL), \
13821 MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d", \
13822 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL)
13823
13824 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
13825 The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
13826 while the any and all forms are subject to mips_builtin_avail_mips3d. */
13827 #define CMP_PS_BUILTINS(INSN, COND, AVAIL) \
13828 MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps", \
13829 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF, \
13830 mips3d), \
13831 MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps", \
13832 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF, \
13833 mips3d), \
13834 MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps", \
13835 MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF, \
13836 AVAIL), \
13837 MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps", \
13838 MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF, \
13839 AVAIL)
13840
13841 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s. The functions
13842 are subject to mips_builtin_avail_mips3d. */
13843 #define CMP_4S_BUILTINS(INSN, COND) \
13844 MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s", \
13845 MIPS_BUILTIN_CMP_ANY, \
13846 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d), \
13847 MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s", \
13848 MIPS_BUILTIN_CMP_ALL, \
13849 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d)
13850
13851 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps. The comparison
13852 instruction requires mips_builtin_avail_<AVAIL>. */
13853 #define MOVTF_BUILTINS(INSN, COND, AVAIL) \
13854 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps", \
13855 MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13856 AVAIL), \
13857 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps", \
13858 MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13859 AVAIL)
13860
13861 /* Define all the built-in functions related to C.cond.fmt condition COND. */
13862 #define CMP_BUILTINS(COND) \
13863 MOVTF_BUILTINS (c, COND, paired_single), \
13864 MOVTF_BUILTINS (cabs, COND, mips3d), \
13865 CMP_SCALAR_BUILTINS (cabs, COND, mips3d), \
13866 CMP_PS_BUILTINS (c, COND, paired_single), \
13867 CMP_PS_BUILTINS (cabs, COND, mips3d), \
13868 CMP_4S_BUILTINS (c, COND), \
13869 CMP_4S_BUILTINS (cabs, COND)
13870
13871 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
13872 function mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE
13873 and AVAIL are as for MIPS_BUILTIN. */
13874 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
13875 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT_NO_TARGET, \
13876 FUNCTION_TYPE, AVAIL)
13877
13878 /* Define __builtin_mips_bposge<VALUE>. <VALUE> is 32 for the MIPS32 DSP
13879 branch instruction. AVAIL is as for MIPS_BUILTIN. */
13880 #define BPOSGE_BUILTIN(VALUE, AVAIL) \
13881 MIPS_BUILTIN (bposge, f, "bposge" #VALUE, \
13882 MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL)
13883
13884 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
13885 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
13886 builtin_description field. */
13887 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE) \
13888 { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f, \
13889 "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT, \
13890 FUNCTION_TYPE, mips_builtin_avail_loongson }
13891
13892 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
13893 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
13894 builtin_description field. */
13895 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE) \
13896 LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
13897
13898 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
13899 We use functions of this form when the same insn can be usefully applied
13900 to more than one datatype. */
13901 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE) \
13902 LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
13903
13904 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
13905 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
13906 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
13907 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
13908 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
13909 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
13910 #define CODE_FOR_mips_mult CODE_FOR_mulsidi3_32bit
13911 #define CODE_FOR_mips_multu CODE_FOR_umulsidi3_32bit
13912
13913 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
13914 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
13915 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
13916 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
13917 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
13918 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
13919 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
13920 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
13921 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
13922 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
13923 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
13924 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
13925 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
13926 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
13927 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
13928 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
13929 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
13930 #define CODE_FOR_loongson_psllh CODE_FOR_ashlv4hi3
13931 #define CODE_FOR_loongson_psllw CODE_FOR_ashlv2si3
13932 #define CODE_FOR_loongson_psrlh CODE_FOR_lshrv4hi3
13933 #define CODE_FOR_loongson_psrlw CODE_FOR_lshrv2si3
13934 #define CODE_FOR_loongson_psrah CODE_FOR_ashrv4hi3
13935 #define CODE_FOR_loongson_psraw CODE_FOR_ashrv2si3
13936 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
13937 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
13938 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
13939 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
13940 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
13941 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
13942 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
13943
13944 static const struct mips_builtin_description mips_builtins[] = {
13945 #define MIPS_GET_FCSR 0
13946 DIRECT_BUILTIN (get_fcsr, MIPS_USI_FTYPE_VOID, hard_float),
13947 #define MIPS_SET_FCSR 1
13948 DIRECT_NO_TARGET_BUILTIN (set_fcsr, MIPS_VOID_FTYPE_USI, hard_float),
13949
13950 DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13951 DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13952 DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13953 DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13954 DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
13955 DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
13956 DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
13957 DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
13958
13959 DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
13960 DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13961 DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13962 DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13963 DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
13964
13965 DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
13966 DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
13967 DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13968 DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13969 DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13970 DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13971
13972 DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
13973 DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
13974 DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13975 DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13976 DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13977 DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13978
13979 MIPS_FP_CONDITIONS (CMP_BUILTINS),
13980
13981 /* Built-in functions for the SB-1 processor. */
13982 DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
13983
13984 /* Built-in functions for the DSP ASE (32-bit and 64-bit). */
13985 DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13986 DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13987 DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13988 DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13989 DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13990 DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13991 DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13992 DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13993 DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13994 DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13995 DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
13996 DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
13997 DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
13998 DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
13999 DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
14000 DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
14001 DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
14002 DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
14003 DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
14004 DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
14005 DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
14006 DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
14007 DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
14008 DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
14009 DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
14010 DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
14011 DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
14012 DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
14013 DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
14014 DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
14015 DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
14016 DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14017 DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14018 DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
14019 DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
14020 DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14021 DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14022 DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
14023 DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
14024 DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
14025 DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14026 DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
14027 DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
14028 DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, dsp),
14029 DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, dsp),
14030 DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
14031 DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
14032 DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14033 DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14034 DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14035 DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14036 DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14037 DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14038 DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14039 DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14040 DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14041 DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
14042 DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14043 DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14044 DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
14045 DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, dsp),
14046 DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
14047 DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
14048 DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
14049 BPOSGE_BUILTIN (32, dsp),
14050
14051 /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit). */
14052 DIRECT_BUILTIN (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
14053 DIRECT_BUILTIN (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14054 DIRECT_BUILTIN (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14055 DIRECT_BUILTIN (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14056 DIRECT_BUILTIN (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14057 DIRECT_BUILTIN (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14058 DIRECT_BUILTIN (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14059 DIRECT_BUILTIN (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14060 DIRECT_BUILTIN (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14061 DIRECT_BUILTIN (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14062 DIRECT_BUILTIN (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14063 DIRECT_BUILTIN (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14064 DIRECT_BUILTIN (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14065 DIRECT_BUILTIN (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14066 DIRECT_BUILTIN (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14067 DIRECT_BUILTIN (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
14068 DIRECT_BUILTIN (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
14069 DIRECT_BUILTIN (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
14070 DIRECT_BUILTIN (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14071 DIRECT_BUILTIN (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
14072 DIRECT_BUILTIN (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
14073 DIRECT_BUILTIN (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
14074 DIRECT_BUILTIN (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14075 DIRECT_BUILTIN (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14076 DIRECT_BUILTIN (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14077 DIRECT_BUILTIN (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14078 DIRECT_BUILTIN (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14079 DIRECT_BUILTIN (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14080 DIRECT_BUILTIN (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14081 DIRECT_BUILTIN (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14082 DIRECT_BUILTIN (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14083 DIRECT_BUILTIN (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14084 DIRECT_BUILTIN (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14085 DIRECT_BUILTIN (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14086
14087 /* Built-in functions for the DSP ASE (32-bit only). */
14088 DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14089 DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14090 DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14091 DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14092 DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14093 DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14094 DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14095 DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14096 DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14097 DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14098 DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14099 DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14100 DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14101 DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14102 DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14103 DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14104 DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
14105 DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
14106 DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
14107 DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
14108 DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
14109 DIRECT_BUILTIN (madd, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14110 DIRECT_BUILTIN (maddu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
14111 DIRECT_BUILTIN (msub, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14112 DIRECT_BUILTIN (msubu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
14113 DIRECT_BUILTIN (mult, MIPS_DI_FTYPE_SI_SI, dsp_32),
14114 DIRECT_BUILTIN (multu, MIPS_DI_FTYPE_USI_USI, dsp_32),
14115
14116 /* Built-in functions for the DSP ASE (64-bit only). */
14117 DIRECT_BUILTIN (ldx, MIPS_DI_FTYPE_POINTER_SI, dsp_64),
14118
14119 /* The following are for the MIPS DSP ASE REV 2 (32-bit only). */
14120 DIRECT_BUILTIN (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14121 DIRECT_BUILTIN (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14122 DIRECT_BUILTIN (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14123 DIRECT_BUILTIN (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14124 DIRECT_BUILTIN (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14125 DIRECT_BUILTIN (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14126 DIRECT_BUILTIN (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14127 DIRECT_BUILTIN (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14128 DIRECT_BUILTIN (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14129
14130 /* Builtin functions for ST Microelectronics Loongson-2E/2F cores. */
14131 LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
14132 LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
14133 LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
14134 LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14135 LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14136 LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14137 LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14138 LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14139 LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14140 LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
14141 LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
14142 LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14143 LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14144 LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14145 LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14146 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
14147 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14148 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14149 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14150 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
14151 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
14152 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14153 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14154 LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14155 LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14156 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14157 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14158 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14159 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14160 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14161 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14162 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14163 LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14164 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14165 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14166 LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14167 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14168 LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
14169 LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
14170 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14171 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14172 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14173 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14174 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14175 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14176 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14177 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14178 LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
14179 LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14180 LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14181 LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14182 LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14183 LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
14184 LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
14185 LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14186 LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14187 LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14188 LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
14189 LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14190 LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
14191 LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
14192 LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14193 LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14194 LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14195 LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14196 LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14197 LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14198 LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14199 LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14200 LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14201 LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14202 LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14203 LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14204 LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14205 LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14206 LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14207 LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14208 LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14209 LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14210 LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14211 LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14212 LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
14213 LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
14214 LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14215 LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14216 LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14217 LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14218 LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14219 LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14220 LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14221 LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14222 LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14223 LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14224 LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14225 LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14226 LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14227 LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14228 LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14229 LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14230
14231 /* Sundry other built-in functions. */
14232 DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache)
14233 };
14234
14235 /* Index I is the function declaration for mips_builtins[I], or null if the
14236 function isn't defined on this target. */
14237 static GTY(()) tree mips_builtin_decls[ARRAY_SIZE (mips_builtins)];
14238
14239 /* MODE is a vector mode whose elements have type TYPE. Return the type
14240 of the vector itself. */
14241
14242 static tree
14243 mips_builtin_vector_type (tree type, enum machine_mode mode)
14244 {
14245 static tree types[2 * (int) MAX_MACHINE_MODE];
14246 int mode_index;
14247
14248 mode_index = (int) mode;
14249
14250 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
14251 mode_index += MAX_MACHINE_MODE;
14252
14253 if (types[mode_index] == NULL_TREE)
14254 types[mode_index] = build_vector_type_for_mode (type, mode);
14255 return types[mode_index];
14256 }
14257
14258 /* Return a type for 'const volatile void *'. */
14259
14260 static tree
14261 mips_build_cvpointer_type (void)
14262 {
14263 static tree cache;
14264
14265 if (cache == NULL_TREE)
14266 cache = build_pointer_type (build_qualified_type
14267 (void_type_node,
14268 TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
14269 return cache;
14270 }
14271
14272 /* Source-level argument types. */
14273 #define MIPS_ATYPE_VOID void_type_node
14274 #define MIPS_ATYPE_INT integer_type_node
14275 #define MIPS_ATYPE_POINTER ptr_type_node
14276 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
14277
14278 /* Standard mode-based argument types. */
14279 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
14280 #define MIPS_ATYPE_SI intSI_type_node
14281 #define MIPS_ATYPE_USI unsigned_intSI_type_node
14282 #define MIPS_ATYPE_DI intDI_type_node
14283 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
14284 #define MIPS_ATYPE_SF float_type_node
14285 #define MIPS_ATYPE_DF double_type_node
14286
14287 /* Vector argument types. */
14288 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
14289 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
14290 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
14291 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
14292 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
14293 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
14294 #define MIPS_ATYPE_UV2SI \
14295 mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
14296 #define MIPS_ATYPE_UV4HI \
14297 mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
14298 #define MIPS_ATYPE_UV8QI \
14299 mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
14300
14301 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
14302 their associated MIPS_ATYPEs. */
14303 #define MIPS_FTYPE_ATYPES1(A, B) \
14304 MIPS_ATYPE_##A, MIPS_ATYPE_##B
14305
14306 #define MIPS_FTYPE_ATYPES2(A, B, C) \
14307 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
14308
14309 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
14310 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
14311
14312 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
14313 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
14314 MIPS_ATYPE_##E
14315
14316 /* Return the function type associated with function prototype TYPE. */
14317
14318 static tree
14319 mips_build_function_type (enum mips_function_type type)
14320 {
14321 static tree types[(int) MIPS_MAX_FTYPE_MAX];
14322
14323 if (types[(int) type] == NULL_TREE)
14324 switch (type)
14325 {
14326 #define DEF_MIPS_FTYPE(NUM, ARGS) \
14327 case MIPS_FTYPE_NAME##NUM ARGS: \
14328 types[(int) type] \
14329 = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS, \
14330 NULL_TREE); \
14331 break;
14332 #include "config/mips/mips-ftypes.def"
14333 #undef DEF_MIPS_FTYPE
14334 default:
14335 gcc_unreachable ();
14336 }
14337
14338 return types[(int) type];
14339 }
14340
14341 /* Implement TARGET_INIT_BUILTINS. */
14342
14343 static void
14344 mips_init_builtins (void)
14345 {
14346 const struct mips_builtin_description *d;
14347 unsigned int i;
14348
14349 /* Iterate through all of the bdesc arrays, initializing all of the
14350 builtin functions. */
14351 for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
14352 {
14353 d = &mips_builtins[i];
14354 if (d->avail ())
14355 mips_builtin_decls[i]
14356 = add_builtin_function (d->name,
14357 mips_build_function_type (d->function_type),
14358 i, BUILT_IN_MD, NULL, NULL);
14359 }
14360 }
14361
14362 /* Implement TARGET_BUILTIN_DECL. */
14363
14364 static tree
14365 mips_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
14366 {
14367 if (code >= ARRAY_SIZE (mips_builtins))
14368 return error_mark_node;
14369 return mips_builtin_decls[code];
14370 }
14371
14372 /* Take argument ARGNO from EXP's argument list and convert it into
14373 an expand operand. Store the operand in *OP. */
14374
14375 static void
14376 mips_prepare_builtin_arg (struct expand_operand *op, tree exp,
14377 unsigned int argno)
14378 {
14379 tree arg;
14380 rtx value;
14381
14382 arg = CALL_EXPR_ARG (exp, argno);
14383 value = expand_normal (arg);
14384 create_input_operand (op, value, TYPE_MODE (TREE_TYPE (arg)));
14385 }
14386
14387 /* Expand instruction ICODE as part of a built-in function sequence.
14388 Use the first NOPS elements of OPS as the instruction's operands.
14389 HAS_TARGET_P is true if operand 0 is a target; it is false if the
14390 instruction has no target.
14391
14392 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */
14393
14394 static rtx
14395 mips_expand_builtin_insn (enum insn_code icode, unsigned int nops,
14396 struct expand_operand *ops, bool has_target_p)
14397 {
14398 if (!maybe_expand_insn (icode, nops, ops))
14399 {
14400 error ("invalid argument to built-in function");
14401 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
14402 }
14403 return has_target_p ? ops[0].value : const0_rtx;
14404 }
14405
14406 /* Expand a floating-point comparison for built-in function call EXP.
14407 The first NARGS arguments are the values to be compared. ICODE is
14408 the .md pattern that does the comparison and COND is the condition
14409 that is being tested. Return an rtx for the result. */
14410
14411 static rtx
14412 mips_expand_builtin_compare_1 (enum insn_code icode,
14413 enum mips_fp_condition cond,
14414 tree exp, int nargs)
14415 {
14416 struct expand_operand ops[MAX_RECOG_OPERANDS];
14417 rtx output;
14418 int opno, argno;
14419
14420 /* The instruction should have a target operand, an operand for each
14421 argument, and an operand for COND. */
14422 gcc_assert (nargs + 2 == insn_data[(int) icode].n_generator_args);
14423
14424 output = mips_allocate_fcc (insn_data[(int) icode].operand[0].mode);
14425 opno = 0;
14426 create_fixed_operand (&ops[opno++], output);
14427 for (argno = 0; argno < nargs; argno++)
14428 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14429 create_integer_operand (&ops[opno++], (int) cond);
14430 return mips_expand_builtin_insn (icode, opno, ops, true);
14431 }
14432
14433 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
14434 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
14435 and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
14436 suggests a good place to put the result. */
14437
14438 static rtx
14439 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
14440 bool has_target_p)
14441 {
14442 struct expand_operand ops[MAX_RECOG_OPERANDS];
14443 int opno, argno;
14444
14445 /* Map any target to operand 0. */
14446 opno = 0;
14447 if (has_target_p)
14448 create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
14449
14450 /* Map the arguments to the other operands. */
14451 gcc_assert (opno + call_expr_nargs (exp)
14452 == insn_data[icode].n_generator_args);
14453 for (argno = 0; argno < call_expr_nargs (exp); argno++)
14454 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14455
14456 return mips_expand_builtin_insn (icode, opno, ops, has_target_p);
14457 }
14458
14459 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
14460 function; TYPE says which. EXP is the CALL_EXPR that calls the
14461 function, ICODE is the instruction that should be used to compare
14462 the first two arguments, and COND is the condition it should test.
14463 TARGET, if nonnull, suggests a good place to put the result. */
14464
14465 static rtx
14466 mips_expand_builtin_movtf (enum mips_builtin_type type,
14467 enum insn_code icode, enum mips_fp_condition cond,
14468 rtx target, tree exp)
14469 {
14470 struct expand_operand ops[4];
14471 rtx cmp_result;
14472
14473 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp, 2);
14474 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
14475 if (type == MIPS_BUILTIN_MOVT)
14476 {
14477 mips_prepare_builtin_arg (&ops[2], exp, 2);
14478 mips_prepare_builtin_arg (&ops[1], exp, 3);
14479 }
14480 else
14481 {
14482 mips_prepare_builtin_arg (&ops[1], exp, 2);
14483 mips_prepare_builtin_arg (&ops[2], exp, 3);
14484 }
14485 create_fixed_operand (&ops[3], cmp_result);
14486 return mips_expand_builtin_insn (CODE_FOR_mips_cond_move_tf_ps,
14487 4, ops, true);
14488 }
14489
14490 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
14491 into TARGET otherwise. Return TARGET. */
14492
14493 static rtx
14494 mips_builtin_branch_and_move (rtx condition, rtx target,
14495 rtx value_if_true, rtx value_if_false)
14496 {
14497 rtx true_label, done_label;
14498
14499 true_label = gen_label_rtx ();
14500 done_label = gen_label_rtx ();
14501
14502 /* First assume that CONDITION is false. */
14503 mips_emit_move (target, value_if_false);
14504
14505 /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise. */
14506 emit_jump_insn (gen_condjump (condition, true_label));
14507 emit_jump_insn (gen_jump (done_label));
14508 emit_barrier ();
14509
14510 /* Fix TARGET if CONDITION is true. */
14511 emit_label (true_label);
14512 mips_emit_move (target, value_if_true);
14513
14514 emit_label (done_label);
14515 return target;
14516 }
14517
14518 /* Expand a comparison built-in function of type BUILTIN_TYPE. EXP is
14519 the CALL_EXPR that calls the function, ICODE is the code of the
14520 comparison instruction, and COND is the condition it should test.
14521 TARGET, if nonnull, suggests a good place to put the boolean result. */
14522
14523 static rtx
14524 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
14525 enum insn_code icode, enum mips_fp_condition cond,
14526 rtx target, tree exp)
14527 {
14528 rtx offset, condition, cmp_result;
14529
14530 if (target == 0 || GET_MODE (target) != SImode)
14531 target = gen_reg_rtx (SImode);
14532 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp,
14533 call_expr_nargs (exp));
14534
14535 /* If the comparison sets more than one register, we define the result
14536 to be 0 if all registers are false and -1 if all registers are true.
14537 The value of the complete result is indeterminate otherwise. */
14538 switch (builtin_type)
14539 {
14540 case MIPS_BUILTIN_CMP_ALL:
14541 condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
14542 return mips_builtin_branch_and_move (condition, target,
14543 const0_rtx, const1_rtx);
14544
14545 case MIPS_BUILTIN_CMP_UPPER:
14546 case MIPS_BUILTIN_CMP_LOWER:
14547 offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
14548 condition = gen_single_cc (cmp_result, offset);
14549 return mips_builtin_branch_and_move (condition, target,
14550 const1_rtx, const0_rtx);
14551
14552 default:
14553 condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
14554 return mips_builtin_branch_and_move (condition, target,
14555 const1_rtx, const0_rtx);
14556 }
14557 }
14558
14559 /* Expand a bposge built-in function of type BUILTIN_TYPE. TARGET,
14560 if nonnull, suggests a good place to put the boolean result. */
14561
14562 static rtx
14563 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
14564 {
14565 rtx condition, cmp_result;
14566 int cmp_value;
14567
14568 if (target == 0 || GET_MODE (target) != SImode)
14569 target = gen_reg_rtx (SImode);
14570
14571 cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
14572
14573 if (builtin_type == MIPS_BUILTIN_BPOSGE32)
14574 cmp_value = 32;
14575 else
14576 gcc_assert (0);
14577
14578 condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
14579 return mips_builtin_branch_and_move (condition, target,
14580 const1_rtx, const0_rtx);
14581 }
14582
14583 /* Implement TARGET_EXPAND_BUILTIN. */
14584
14585 static rtx
14586 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
14587 enum machine_mode mode, int ignore)
14588 {
14589 tree fndecl;
14590 unsigned int fcode, avail;
14591 const struct mips_builtin_description *d;
14592
14593 fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
14594 fcode = DECL_FUNCTION_CODE (fndecl);
14595 gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
14596 d = &mips_builtins[fcode];
14597 avail = d->avail ();
14598 gcc_assert (avail != 0);
14599 if (TARGET_MIPS16 && !(avail & BUILTIN_AVAIL_MIPS16))
14600 {
14601 error ("built-in function %qE not supported for MIPS16",
14602 DECL_NAME (fndecl));
14603 return ignore ? const0_rtx : CONST0_RTX (mode);
14604 }
14605 switch (d->builtin_type)
14606 {
14607 case MIPS_BUILTIN_DIRECT:
14608 return mips_expand_builtin_direct (d->icode, target, exp, true);
14609
14610 case MIPS_BUILTIN_DIRECT_NO_TARGET:
14611 return mips_expand_builtin_direct (d->icode, target, exp, false);
14612
14613 case MIPS_BUILTIN_MOVT:
14614 case MIPS_BUILTIN_MOVF:
14615 return mips_expand_builtin_movtf (d->builtin_type, d->icode,
14616 d->cond, target, exp);
14617
14618 case MIPS_BUILTIN_CMP_ANY:
14619 case MIPS_BUILTIN_CMP_ALL:
14620 case MIPS_BUILTIN_CMP_UPPER:
14621 case MIPS_BUILTIN_CMP_LOWER:
14622 case MIPS_BUILTIN_CMP_SINGLE:
14623 return mips_expand_builtin_compare (d->builtin_type, d->icode,
14624 d->cond, target, exp);
14625
14626 case MIPS_BUILTIN_BPOSGE32:
14627 return mips_expand_builtin_bposge (d->builtin_type, target);
14628 }
14629 gcc_unreachable ();
14630 }
14631 \f
14632 /* An entry in the MIPS16 constant pool. VALUE is the pool constant,
14633 MODE is its mode, and LABEL is the CODE_LABEL associated with it. */
14634 struct mips16_constant {
14635 struct mips16_constant *next;
14636 rtx value;
14637 rtx label;
14638 enum machine_mode mode;
14639 };
14640
14641 /* Information about an incomplete MIPS16 constant pool. FIRST is the
14642 first constant, HIGHEST_ADDRESS is the highest address that the first
14643 byte of the pool can have, and INSN_ADDRESS is the current instruction
14644 address. */
14645 struct mips16_constant_pool {
14646 struct mips16_constant *first;
14647 int highest_address;
14648 int insn_address;
14649 };
14650
14651 /* Add constant VALUE to POOL and return its label. MODE is the
14652 value's mode (used for CONST_INTs, etc.). */
14653
14654 static rtx
14655 mips16_add_constant (struct mips16_constant_pool *pool,
14656 rtx value, enum machine_mode mode)
14657 {
14658 struct mips16_constant **p, *c;
14659 bool first_of_size_p;
14660
14661 /* See whether the constant is already in the pool. If so, return the
14662 existing label, otherwise leave P pointing to the place where the
14663 constant should be added.
14664
14665 Keep the pool sorted in increasing order of mode size so that we can
14666 reduce the number of alignments needed. */
14667 first_of_size_p = true;
14668 for (p = &pool->first; *p != 0; p = &(*p)->next)
14669 {
14670 if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
14671 return (*p)->label;
14672 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
14673 break;
14674 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
14675 first_of_size_p = false;
14676 }
14677
14678 /* In the worst case, the constant needed by the earliest instruction
14679 will end up at the end of the pool. The entire pool must then be
14680 accessible from that instruction.
14681
14682 When adding the first constant, set the pool's highest address to
14683 the address of the first out-of-range byte. Adjust this address
14684 downwards each time a new constant is added. */
14685 if (pool->first == 0)
14686 /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
14687 of the instruction with the lowest two bits clear. The base PC
14688 value for LDPC has the lowest three bits clear. Assume the worst
14689 case here; namely that the PC-relative instruction occupies the
14690 last 2 bytes in an aligned word. */
14691 pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
14692 pool->highest_address -= GET_MODE_SIZE (mode);
14693 if (first_of_size_p)
14694 /* Take into account the worst possible padding due to alignment. */
14695 pool->highest_address -= GET_MODE_SIZE (mode) - 1;
14696
14697 /* Create a new entry. */
14698 c = XNEW (struct mips16_constant);
14699 c->value = value;
14700 c->mode = mode;
14701 c->label = gen_label_rtx ();
14702 c->next = *p;
14703 *p = c;
14704
14705 return c->label;
14706 }
14707
14708 /* Output constant VALUE after instruction INSN and return the last
14709 instruction emitted. MODE is the mode of the constant. */
14710
14711 static rtx
14712 mips16_emit_constants_1 (enum machine_mode mode, rtx value, rtx insn)
14713 {
14714 if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
14715 {
14716 rtx size = GEN_INT (GET_MODE_SIZE (mode));
14717 return emit_insn_after (gen_consttable_int (value, size), insn);
14718 }
14719
14720 if (SCALAR_FLOAT_MODE_P (mode))
14721 return emit_insn_after (gen_consttable_float (value), insn);
14722
14723 if (VECTOR_MODE_P (mode))
14724 {
14725 int i;
14726
14727 for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
14728 insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
14729 CONST_VECTOR_ELT (value, i), insn);
14730 return insn;
14731 }
14732
14733 gcc_unreachable ();
14734 }
14735
14736 /* Dump out the constants in CONSTANTS after INSN. */
14737
14738 static void
14739 mips16_emit_constants (struct mips16_constant *constants, rtx insn)
14740 {
14741 struct mips16_constant *c, *next;
14742 int align;
14743
14744 align = 0;
14745 for (c = constants; c != NULL; c = next)
14746 {
14747 /* If necessary, increase the alignment of PC. */
14748 if (align < GET_MODE_SIZE (c->mode))
14749 {
14750 int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
14751 insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
14752 }
14753 align = GET_MODE_SIZE (c->mode);
14754
14755 insn = emit_label_after (c->label, insn);
14756 insn = mips16_emit_constants_1 (c->mode, c->value, insn);
14757
14758 next = c->next;
14759 free (c);
14760 }
14761
14762 emit_barrier_after (insn);
14763 }
14764
14765 /* Return the length of instruction INSN. */
14766
14767 static int
14768 mips16_insn_length (rtx insn)
14769 {
14770 if (JUMP_TABLE_DATA_P (insn))
14771 {
14772 rtx body = PATTERN (insn);
14773 if (GET_CODE (body) == ADDR_VEC)
14774 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
14775 else if (GET_CODE (body) == ADDR_DIFF_VEC)
14776 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
14777 else
14778 gcc_unreachable ();
14779 }
14780 return get_attr_length (insn);
14781 }
14782
14783 /* If *X is a symbolic constant that refers to the constant pool, add
14784 the constant to POOL and rewrite *X to use the constant's label. */
14785
14786 static void
14787 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
14788 {
14789 rtx base, offset, label;
14790
14791 split_const (*x, &base, &offset);
14792 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
14793 {
14794 label = mips16_add_constant (pool, copy_rtx (get_pool_constant (base)),
14795 get_pool_mode (base));
14796 base = gen_rtx_LABEL_REF (Pmode, label);
14797 *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
14798 }
14799 }
14800
14801 /* This structure is used to communicate with mips16_rewrite_pool_refs.
14802 INSN is the instruction we're rewriting and POOL points to the current
14803 constant pool. */
14804 struct mips16_rewrite_pool_refs_info {
14805 rtx insn;
14806 struct mips16_constant_pool *pool;
14807 };
14808
14809 /* Rewrite *X so that constant pool references refer to the constant's
14810 label instead. DATA points to a mips16_rewrite_pool_refs_info
14811 structure. */
14812
14813 static int
14814 mips16_rewrite_pool_refs (rtx *x, void *data)
14815 {
14816 struct mips16_rewrite_pool_refs_info *info =
14817 (struct mips16_rewrite_pool_refs_info *) data;
14818
14819 if (force_to_mem_operand (*x, Pmode))
14820 {
14821 rtx mem = force_const_mem (GET_MODE (*x), *x);
14822 validate_change (info->insn, x, mem, false);
14823 }
14824
14825 if (MEM_P (*x))
14826 {
14827 mips16_rewrite_pool_constant (info->pool, &XEXP (*x, 0));
14828 return -1;
14829 }
14830
14831 /* Don't rewrite the __mips16_rdwr symbol. */
14832 if (GET_CODE (*x) == UNSPEC && XINT (*x, 1) == UNSPEC_TLS_GET_TP)
14833 return -1;
14834
14835 if (TARGET_MIPS16_TEXT_LOADS)
14836 mips16_rewrite_pool_constant (info->pool, x);
14837
14838 return GET_CODE (*x) == CONST ? -1 : 0;
14839 }
14840
14841 /* Return whether CFG is used in mips_reorg. */
14842
14843 static bool
14844 mips_cfg_in_reorg (void)
14845 {
14846 return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
14847 || TARGET_RELAX_PIC_CALLS);
14848 }
14849
14850 /* Build MIPS16 constant pools. Split the instructions if SPLIT_P,
14851 otherwise assume that they are already split. */
14852
14853 static void
14854 mips16_lay_out_constants (bool split_p)
14855 {
14856 struct mips16_constant_pool pool;
14857 struct mips16_rewrite_pool_refs_info info;
14858 rtx insn, barrier;
14859
14860 if (!TARGET_MIPS16_PCREL_LOADS)
14861 return;
14862
14863 if (split_p)
14864 {
14865 if (mips_cfg_in_reorg ())
14866 split_all_insns ();
14867 else
14868 split_all_insns_noflow ();
14869 }
14870 barrier = 0;
14871 memset (&pool, 0, sizeof (pool));
14872 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14873 {
14874 /* Rewrite constant pool references in INSN. */
14875 if (USEFUL_INSN_P (insn))
14876 {
14877 info.insn = insn;
14878 info.pool = &pool;
14879 for_each_rtx (&PATTERN (insn), mips16_rewrite_pool_refs, &info);
14880 }
14881
14882 pool.insn_address += mips16_insn_length (insn);
14883
14884 if (pool.first != NULL)
14885 {
14886 /* If there are no natural barriers between the first user of
14887 the pool and the highest acceptable address, we'll need to
14888 create a new instruction to jump around the constant pool.
14889 In the worst case, this instruction will be 4 bytes long.
14890
14891 If it's too late to do this transformation after INSN,
14892 do it immediately before INSN. */
14893 if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
14894 {
14895 rtx label, jump;
14896
14897 label = gen_label_rtx ();
14898
14899 jump = emit_jump_insn_before (gen_jump (label), insn);
14900 JUMP_LABEL (jump) = label;
14901 LABEL_NUSES (label) = 1;
14902 barrier = emit_barrier_after (jump);
14903
14904 emit_label_after (label, barrier);
14905 pool.insn_address += 4;
14906 }
14907
14908 /* See whether the constant pool is now out of range of the first
14909 user. If so, output the constants after the previous barrier.
14910 Note that any instructions between BARRIER and INSN (inclusive)
14911 will use negative offsets to refer to the pool. */
14912 if (pool.insn_address > pool.highest_address)
14913 {
14914 mips16_emit_constants (pool.first, barrier);
14915 pool.first = NULL;
14916 barrier = 0;
14917 }
14918 else if (BARRIER_P (insn))
14919 barrier = insn;
14920 }
14921 }
14922 mips16_emit_constants (pool.first, get_last_insn ());
14923 }
14924 \f
14925 /* Return true if it is worth r10k_simplify_address's while replacing
14926 an address with X. We are looking for constants, and for addresses
14927 at a known offset from the incoming stack pointer. */
14928
14929 static bool
14930 r10k_simplified_address_p (rtx x)
14931 {
14932 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
14933 x = XEXP (x, 0);
14934 return x == virtual_incoming_args_rtx || CONSTANT_P (x);
14935 }
14936
14937 /* X is an expression that appears in INSN. Try to use the UD chains
14938 to simplify it, returning the simplified form on success and the
14939 original form otherwise. Replace the incoming value of $sp with
14940 virtual_incoming_args_rtx (which should never occur in X otherwise). */
14941
14942 static rtx
14943 r10k_simplify_address (rtx x, rtx insn)
14944 {
14945 rtx newx, op0, op1, set, def_insn, note;
14946 df_ref use, def;
14947 struct df_link *defs;
14948
14949 newx = NULL_RTX;
14950 if (UNARY_P (x))
14951 {
14952 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14953 if (op0 != XEXP (x, 0))
14954 newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
14955 op0, GET_MODE (XEXP (x, 0)));
14956 }
14957 else if (BINARY_P (x))
14958 {
14959 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14960 op1 = r10k_simplify_address (XEXP (x, 1), insn);
14961 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
14962 newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
14963 }
14964 else if (GET_CODE (x) == LO_SUM)
14965 {
14966 /* LO_SUMs can be offset from HIGHs, if we know they won't
14967 overflow. See mips_classify_address for the rationale behind
14968 the lax check. */
14969 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14970 if (GET_CODE (op0) == HIGH)
14971 newx = XEXP (x, 1);
14972 }
14973 else if (REG_P (x))
14974 {
14975 /* Uses are recorded by regno_reg_rtx, not X itself. */
14976 use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
14977 gcc_assert (use);
14978 defs = DF_REF_CHAIN (use);
14979
14980 /* Require a single definition. */
14981 if (defs && defs->next == NULL)
14982 {
14983 def = defs->ref;
14984 if (DF_REF_IS_ARTIFICIAL (def))
14985 {
14986 /* Replace the incoming value of $sp with
14987 virtual_incoming_args_rtx. */
14988 if (x == stack_pointer_rtx
14989 && DF_REF_BB (def) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
14990 newx = virtual_incoming_args_rtx;
14991 }
14992 else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
14993 DF_REF_BB (def)))
14994 {
14995 /* Make sure that DEF_INSN is a single set of REG. */
14996 def_insn = DF_REF_INSN (def);
14997 if (NONJUMP_INSN_P (def_insn))
14998 {
14999 set = single_set (def_insn);
15000 if (set && rtx_equal_p (SET_DEST (set), x))
15001 {
15002 /* Prefer to use notes, since the def-use chains
15003 are often shorter. */
15004 note = find_reg_equal_equiv_note (def_insn);
15005 if (note)
15006 newx = XEXP (note, 0);
15007 else
15008 newx = SET_SRC (set);
15009 newx = r10k_simplify_address (newx, def_insn);
15010 }
15011 }
15012 }
15013 }
15014 }
15015 if (newx && r10k_simplified_address_p (newx))
15016 return newx;
15017 return x;
15018 }
15019
15020 /* Return true if ADDRESS is known to be an uncached address
15021 on R10K systems. */
15022
15023 static bool
15024 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
15025 {
15026 unsigned HOST_WIDE_INT upper;
15027
15028 /* Check for KSEG1. */
15029 if (address + 0x60000000 < 0x20000000)
15030 return true;
15031
15032 /* Check for uncached XKPHYS addresses. */
15033 if (Pmode == DImode)
15034 {
15035 upper = (address >> 40) & 0xf9ffff;
15036 if (upper == 0x900000 || upper == 0xb80000)
15037 return true;
15038 }
15039 return false;
15040 }
15041
15042 /* Return true if we can prove that an access to address X in instruction
15043 INSN would be safe from R10K speculation. This X is a general
15044 expression; it might not be a legitimate address. */
15045
15046 static bool
15047 r10k_safe_address_p (rtx x, rtx insn)
15048 {
15049 rtx base, offset;
15050 HOST_WIDE_INT offset_val;
15051
15052 x = r10k_simplify_address (x, insn);
15053
15054 /* Check for references to the stack frame. It doesn't really matter
15055 how much of the frame has been allocated at INSN; -mr10k-cache-barrier
15056 allows us to assume that accesses to any part of the eventual frame
15057 is safe from speculation at any point in the function. */
15058 mips_split_plus (x, &base, &offset_val);
15059 if (base == virtual_incoming_args_rtx
15060 && offset_val >= -cfun->machine->frame.total_size
15061 && offset_val < cfun->machine->frame.args_size)
15062 return true;
15063
15064 /* Check for uncached addresses. */
15065 if (CONST_INT_P (x))
15066 return r10k_uncached_address_p (INTVAL (x));
15067
15068 /* Check for accesses to a static object. */
15069 split_const (x, &base, &offset);
15070 return offset_within_block_p (base, INTVAL (offset));
15071 }
15072
15073 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
15074 an in-range access to an automatic variable, or to an object with
15075 a link-time-constant address. */
15076
15077 static bool
15078 r10k_safe_mem_expr_p (tree expr, unsigned HOST_WIDE_INT offset)
15079 {
15080 HOST_WIDE_INT bitoffset, bitsize;
15081 tree inner, var_offset;
15082 enum machine_mode mode;
15083 int unsigned_p, volatile_p;
15084
15085 inner = get_inner_reference (expr, &bitsize, &bitoffset, &var_offset, &mode,
15086 &unsigned_p, &volatile_p, false);
15087 if (!DECL_P (inner) || !DECL_SIZE_UNIT (inner) || var_offset)
15088 return false;
15089
15090 offset += bitoffset / BITS_PER_UNIT;
15091 return offset < tree_to_uhwi (DECL_SIZE_UNIT (inner));
15092 }
15093
15094 /* A for_each_rtx callback for which DATA points to the instruction
15095 containing *X. Stop the search if we find a MEM that is not safe
15096 from R10K speculation. */
15097
15098 static int
15099 r10k_needs_protection_p_1 (rtx *loc, void *data)
15100 {
15101 rtx mem;
15102
15103 mem = *loc;
15104 if (!MEM_P (mem))
15105 return 0;
15106
15107 if (MEM_EXPR (mem)
15108 && MEM_OFFSET_KNOWN_P (mem)
15109 && r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
15110 return -1;
15111
15112 if (r10k_safe_address_p (XEXP (mem, 0), (rtx) data))
15113 return -1;
15114
15115 return 1;
15116 }
15117
15118 /* A note_stores callback for which DATA points to an instruction pointer.
15119 If *DATA is nonnull, make it null if it X contains a MEM that is not
15120 safe from R10K speculation. */
15121
15122 static void
15123 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
15124 void *data)
15125 {
15126 rtx *insn_ptr;
15127
15128 insn_ptr = (rtx *) data;
15129 if (*insn_ptr && for_each_rtx (&x, r10k_needs_protection_p_1, *insn_ptr))
15130 *insn_ptr = NULL_RTX;
15131 }
15132
15133 /* A for_each_rtx callback that iterates over the pattern of a CALL_INSN.
15134 Return nonzero if the call is not to a declared function. */
15135
15136 static int
15137 r10k_needs_protection_p_call (rtx *loc, void *data ATTRIBUTE_UNUSED)
15138 {
15139 rtx x;
15140
15141 x = *loc;
15142 if (!MEM_P (x))
15143 return 0;
15144
15145 x = XEXP (x, 0);
15146 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DECL (x))
15147 return -1;
15148
15149 return 1;
15150 }
15151
15152 /* Return true if instruction INSN needs to be protected by an R10K
15153 cache barrier. */
15154
15155 static bool
15156 r10k_needs_protection_p (rtx insn)
15157 {
15158 if (CALL_P (insn))
15159 return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_call, NULL);
15160
15161 if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
15162 {
15163 note_stores (PATTERN (insn), r10k_needs_protection_p_store, &insn);
15164 return insn == NULL_RTX;
15165 }
15166
15167 return for_each_rtx (&PATTERN (insn), r10k_needs_protection_p_1, insn);
15168 }
15169
15170 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
15171 edge is unconditional. */
15172
15173 static bool
15174 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
15175 {
15176 edge_iterator ei;
15177 edge e;
15178
15179 FOR_EACH_EDGE (e, ei, bb->preds)
15180 if (!single_succ_p (e->src)
15181 || !bitmap_bit_p (protected_bbs, e->src->index)
15182 || (e->flags & EDGE_COMPLEX) != 0)
15183 return false;
15184 return true;
15185 }
15186
15187 /* Implement -mr10k-cache-barrier= for the current function. */
15188
15189 static void
15190 r10k_insert_cache_barriers (void)
15191 {
15192 int *rev_post_order;
15193 unsigned int i, n;
15194 basic_block bb;
15195 sbitmap protected_bbs;
15196 rtx insn, end, unprotected_region;
15197
15198 if (TARGET_MIPS16)
15199 {
15200 sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
15201 return;
15202 }
15203
15204 /* Calculate dominators. */
15205 calculate_dominance_info (CDI_DOMINATORS);
15206
15207 /* Bit X of PROTECTED_BBS is set if the last operation in basic block
15208 X is protected by a cache barrier. */
15209 protected_bbs = sbitmap_alloc (last_basic_block_for_fn (cfun));
15210 bitmap_clear (protected_bbs);
15211
15212 /* Iterate over the basic blocks in reverse post-order. */
15213 rev_post_order = XNEWVEC (int, last_basic_block_for_fn (cfun));
15214 n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
15215 for (i = 0; i < n; i++)
15216 {
15217 bb = BASIC_BLOCK_FOR_FN (cfun, rev_post_order[i]);
15218
15219 /* If this block is only reached by unconditional edges, and if the
15220 source of every edge is protected, the beginning of the block is
15221 also protected. */
15222 if (r10k_protected_bb_p (bb, protected_bbs))
15223 unprotected_region = NULL_RTX;
15224 else
15225 unprotected_region = pc_rtx;
15226 end = NEXT_INSN (BB_END (bb));
15227
15228 /* UNPROTECTED_REGION is:
15229
15230 - null if we are processing a protected region,
15231 - pc_rtx if we are processing an unprotected region but have
15232 not yet found the first instruction in it
15233 - the first instruction in an unprotected region otherwise. */
15234 for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
15235 {
15236 if (unprotected_region && USEFUL_INSN_P (insn))
15237 {
15238 if (recog_memoized (insn) == CODE_FOR_mips_cache)
15239 /* This CACHE instruction protects the following code. */
15240 unprotected_region = NULL_RTX;
15241 else
15242 {
15243 /* See if INSN is the first instruction in this
15244 unprotected region. */
15245 if (unprotected_region == pc_rtx)
15246 unprotected_region = insn;
15247
15248 /* See if INSN needs to be protected. If so,
15249 we must insert a cache barrier somewhere between
15250 PREV_INSN (UNPROTECTED_REGION) and INSN. It isn't
15251 clear which position is better performance-wise,
15252 but as a tie-breaker, we assume that it is better
15253 to allow delay slots to be back-filled where
15254 possible, and that it is better not to insert
15255 barriers in the middle of already-scheduled code.
15256 We therefore insert the barrier at the beginning
15257 of the region. */
15258 if (r10k_needs_protection_p (insn))
15259 {
15260 emit_insn_before (gen_r10k_cache_barrier (),
15261 unprotected_region);
15262 unprotected_region = NULL_RTX;
15263 }
15264 }
15265 }
15266
15267 if (CALL_P (insn))
15268 /* The called function is not required to protect the exit path.
15269 The code that follows a call is therefore unprotected. */
15270 unprotected_region = pc_rtx;
15271 }
15272
15273 /* Record whether the end of this block is protected. */
15274 if (unprotected_region == NULL_RTX)
15275 bitmap_set_bit (protected_bbs, bb->index);
15276 }
15277 XDELETEVEC (rev_post_order);
15278
15279 sbitmap_free (protected_bbs);
15280
15281 free_dominance_info (CDI_DOMINATORS);
15282 }
15283 \f
15284 /* If INSN is a call, return the underlying CALL expr. Return NULL_RTX
15285 otherwise. If INSN has two call rtx, then store the second one in
15286 SECOND_CALL. */
15287
15288 static rtx
15289 mips_call_expr_from_insn (rtx insn, rtx *second_call)
15290 {
15291 rtx x;
15292 rtx x2;
15293
15294 if (!CALL_P (insn))
15295 return NULL_RTX;
15296
15297 x = PATTERN (insn);
15298 if (GET_CODE (x) == PARALLEL)
15299 {
15300 /* Calls returning complex values have two CALL rtx. Look for the second
15301 one here, and return it via the SECOND_CALL arg. */
15302 x2 = XVECEXP (x, 0, 1);
15303 if (GET_CODE (x2) == SET)
15304 x2 = XEXP (x2, 1);
15305 if (GET_CODE (x2) == CALL)
15306 *second_call = x2;
15307
15308 x = XVECEXP (x, 0, 0);
15309 }
15310 if (GET_CODE (x) == SET)
15311 x = XEXP (x, 1);
15312 gcc_assert (GET_CODE (x) == CALL);
15313
15314 return x;
15315 }
15316
15317 /* REG is set in DEF. See if the definition is one of the ways we load a
15318 register with a symbol address for a mips_use_pic_fn_addr_reg_p call.
15319 If it is, return the symbol reference of the function, otherwise return
15320 NULL_RTX.
15321
15322 If RECURSE_P is true, use mips_find_pic_call_symbol to interpret
15323 the values of source registers, otherwise treat such registers as
15324 having an unknown value. */
15325
15326 static rtx
15327 mips_pic_call_symbol_from_set (df_ref def, rtx reg, bool recurse_p)
15328 {
15329 rtx def_insn, set;
15330
15331 if (DF_REF_IS_ARTIFICIAL (def))
15332 return NULL_RTX;
15333
15334 def_insn = DF_REF_INSN (def);
15335 set = single_set (def_insn);
15336 if (set && rtx_equal_p (SET_DEST (set), reg))
15337 {
15338 rtx note, src, symbol;
15339
15340 /* First see whether the source is a plain symbol. This is used
15341 when calling symbols that are not lazily bound. */
15342 src = SET_SRC (set);
15343 if (GET_CODE (src) == SYMBOL_REF)
15344 return src;
15345
15346 /* Handle %call16 references. */
15347 symbol = mips_strip_unspec_call (src);
15348 if (symbol)
15349 {
15350 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15351 return symbol;
15352 }
15353
15354 /* If we have something more complicated, look for a
15355 REG_EQUAL or REG_EQUIV note. */
15356 note = find_reg_equal_equiv_note (def_insn);
15357 if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
15358 return XEXP (note, 0);
15359
15360 /* Follow at most one simple register copy. Such copies are
15361 interesting in cases like:
15362
15363 for (...)
15364 {
15365 locally_binding_fn (...);
15366 }
15367
15368 and:
15369
15370 locally_binding_fn (...);
15371 ...
15372 locally_binding_fn (...);
15373
15374 where the load of locally_binding_fn can legitimately be
15375 hoisted or shared. However, we do not expect to see complex
15376 chains of copies, so a full worklist solution to the problem
15377 would probably be overkill. */
15378 if (recurse_p && REG_P (src))
15379 return mips_find_pic_call_symbol (def_insn, src, false);
15380 }
15381
15382 return NULL_RTX;
15383 }
15384
15385 /* Find the definition of the use of REG in INSN. See if the definition
15386 is one of the ways we load a register with a symbol address for a
15387 mips_use_pic_fn_addr_reg_p call. If it is return the symbol reference
15388 of the function, otherwise return NULL_RTX. RECURSE_P is as for
15389 mips_pic_call_symbol_from_set. */
15390
15391 static rtx
15392 mips_find_pic_call_symbol (rtx insn, rtx reg, bool recurse_p)
15393 {
15394 df_ref use;
15395 struct df_link *defs;
15396 rtx symbol;
15397
15398 use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
15399 if (!use)
15400 return NULL_RTX;
15401 defs = DF_REF_CHAIN (use);
15402 if (!defs)
15403 return NULL_RTX;
15404 symbol = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15405 if (!symbol)
15406 return NULL_RTX;
15407
15408 /* If we have more than one definition, they need to be identical. */
15409 for (defs = defs->next; defs; defs = defs->next)
15410 {
15411 rtx other;
15412
15413 other = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15414 if (!rtx_equal_p (symbol, other))
15415 return NULL_RTX;
15416 }
15417
15418 return symbol;
15419 }
15420
15421 /* Replace the args_size operand of the call expression CALL with the
15422 call-attribute UNSPEC and fill in SYMBOL as the function symbol. */
15423
15424 static void
15425 mips_annotate_pic_call_expr (rtx call, rtx symbol)
15426 {
15427 rtx args_size;
15428
15429 args_size = XEXP (call, 1);
15430 XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
15431 gen_rtvec (2, args_size, symbol),
15432 UNSPEC_CALL_ATTR);
15433 }
15434
15435 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression. See
15436 if instead of the arg_size argument it contains the call attributes. If
15437 yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
15438 symbol from the call attributes. Also return false if ARGS_SIZE_OPNO is
15439 -1. */
15440
15441 bool
15442 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
15443 {
15444 rtx args_size, symbol;
15445
15446 if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
15447 return false;
15448
15449 args_size = operands[args_size_opno];
15450 if (GET_CODE (args_size) != UNSPEC)
15451 return false;
15452 gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
15453
15454 symbol = XVECEXP (args_size, 0, 1);
15455 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15456
15457 operands[args_size_opno] = symbol;
15458 return true;
15459 }
15460
15461 /* Use DF to annotate PIC indirect calls with the function symbol they
15462 dispatch to. */
15463
15464 static void
15465 mips_annotate_pic_calls (void)
15466 {
15467 basic_block bb;
15468 rtx insn;
15469
15470 FOR_EACH_BB_FN (bb, cfun)
15471 FOR_BB_INSNS (bb, insn)
15472 {
15473 rtx call, reg, symbol, second_call;
15474
15475 second_call = 0;
15476 call = mips_call_expr_from_insn (insn, &second_call);
15477 if (!call)
15478 continue;
15479 gcc_assert (MEM_P (XEXP (call, 0)));
15480 reg = XEXP (XEXP (call, 0), 0);
15481 if (!REG_P (reg))
15482 continue;
15483
15484 symbol = mips_find_pic_call_symbol (insn, reg, true);
15485 if (symbol)
15486 {
15487 mips_annotate_pic_call_expr (call, symbol);
15488 if (second_call)
15489 mips_annotate_pic_call_expr (second_call, symbol);
15490 }
15491 }
15492 }
15493 \f
15494 /* A temporary variable used by for_each_rtx callbacks, etc. */
15495 static rtx mips_sim_insn;
15496
15497 /* A structure representing the state of the processor pipeline.
15498 Used by the mips_sim_* family of functions. */
15499 struct mips_sim {
15500 /* The maximum number of instructions that can be issued in a cycle.
15501 (Caches mips_issue_rate.) */
15502 unsigned int issue_rate;
15503
15504 /* The current simulation time. */
15505 unsigned int time;
15506
15507 /* How many more instructions can be issued in the current cycle. */
15508 unsigned int insns_left;
15509
15510 /* LAST_SET[X].INSN is the last instruction to set register X.
15511 LAST_SET[X].TIME is the time at which that instruction was issued.
15512 INSN is null if no instruction has yet set register X. */
15513 struct {
15514 rtx insn;
15515 unsigned int time;
15516 } last_set[FIRST_PSEUDO_REGISTER];
15517
15518 /* The pipeline's current DFA state. */
15519 state_t dfa_state;
15520 };
15521
15522 /* Reset STATE to the initial simulation state. */
15523
15524 static void
15525 mips_sim_reset (struct mips_sim *state)
15526 {
15527 curr_state = state->dfa_state;
15528
15529 state->time = 0;
15530 state->insns_left = state->issue_rate;
15531 memset (&state->last_set, 0, sizeof (state->last_set));
15532 state_reset (curr_state);
15533
15534 targetm.sched.init (0, false, 0);
15535 advance_state (curr_state);
15536 }
15537
15538 /* Initialize STATE before its first use. DFA_STATE points to an
15539 allocated but uninitialized DFA state. */
15540
15541 static void
15542 mips_sim_init (struct mips_sim *state, state_t dfa_state)
15543 {
15544 if (targetm.sched.init_dfa_pre_cycle_insn)
15545 targetm.sched.init_dfa_pre_cycle_insn ();
15546
15547 if (targetm.sched.init_dfa_post_cycle_insn)
15548 targetm.sched.init_dfa_post_cycle_insn ();
15549
15550 state->issue_rate = mips_issue_rate ();
15551 state->dfa_state = dfa_state;
15552 mips_sim_reset (state);
15553 }
15554
15555 /* Advance STATE by one clock cycle. */
15556
15557 static void
15558 mips_sim_next_cycle (struct mips_sim *state)
15559 {
15560 curr_state = state->dfa_state;
15561
15562 state->time++;
15563 state->insns_left = state->issue_rate;
15564 advance_state (curr_state);
15565 }
15566
15567 /* Advance simulation state STATE until instruction INSN can read
15568 register REG. */
15569
15570 static void
15571 mips_sim_wait_reg (struct mips_sim *state, rtx insn, rtx reg)
15572 {
15573 unsigned int regno, end_regno;
15574
15575 end_regno = END_REGNO (reg);
15576 for (regno = REGNO (reg); regno < end_regno; regno++)
15577 if (state->last_set[regno].insn != 0)
15578 {
15579 unsigned int t;
15580
15581 t = (state->last_set[regno].time
15582 + insn_latency (state->last_set[regno].insn, insn));
15583 while (state->time < t)
15584 mips_sim_next_cycle (state);
15585 }
15586 }
15587
15588 /* A for_each_rtx callback. If *X is a register, advance simulation state
15589 DATA until mips_sim_insn can read the register's value. */
15590
15591 static int
15592 mips_sim_wait_regs_2 (rtx *x, void *data)
15593 {
15594 if (REG_P (*x))
15595 mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *x);
15596 return 0;
15597 }
15598
15599 /* Call mips_sim_wait_regs_2 (R, DATA) for each register R mentioned in *X. */
15600
15601 static void
15602 mips_sim_wait_regs_1 (rtx *x, void *data)
15603 {
15604 for_each_rtx (x, mips_sim_wait_regs_2, data);
15605 }
15606
15607 /* Advance simulation state STATE until all of INSN's register
15608 dependencies are satisfied. */
15609
15610 static void
15611 mips_sim_wait_regs (struct mips_sim *state, rtx insn)
15612 {
15613 mips_sim_insn = insn;
15614 note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
15615 }
15616
15617 /* Advance simulation state STATE until the units required by
15618 instruction INSN are available. */
15619
15620 static void
15621 mips_sim_wait_units (struct mips_sim *state, rtx insn)
15622 {
15623 state_t tmp_state;
15624
15625 tmp_state = alloca (state_size ());
15626 while (state->insns_left == 0
15627 || (memcpy (tmp_state, state->dfa_state, state_size ()),
15628 state_transition (tmp_state, insn) >= 0))
15629 mips_sim_next_cycle (state);
15630 }
15631
15632 /* Advance simulation state STATE until INSN is ready to issue. */
15633
15634 static void
15635 mips_sim_wait_insn (struct mips_sim *state, rtx insn)
15636 {
15637 mips_sim_wait_regs (state, insn);
15638 mips_sim_wait_units (state, insn);
15639 }
15640
15641 /* mips_sim_insn has just set X. Update the LAST_SET array
15642 in simulation state DATA. */
15643
15644 static void
15645 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
15646 {
15647 struct mips_sim *state;
15648
15649 state = (struct mips_sim *) data;
15650 if (REG_P (x))
15651 {
15652 unsigned int regno, end_regno;
15653
15654 end_regno = END_REGNO (x);
15655 for (regno = REGNO (x); regno < end_regno; regno++)
15656 {
15657 state->last_set[regno].insn = mips_sim_insn;
15658 state->last_set[regno].time = state->time;
15659 }
15660 }
15661 }
15662
15663 /* Issue instruction INSN in scheduler state STATE. Assume that INSN
15664 can issue immediately (i.e., that mips_sim_wait_insn has already
15665 been called). */
15666
15667 static void
15668 mips_sim_issue_insn (struct mips_sim *state, rtx insn)
15669 {
15670 curr_state = state->dfa_state;
15671
15672 state_transition (curr_state, insn);
15673 state->insns_left = targetm.sched.variable_issue (0, false, insn,
15674 state->insns_left);
15675
15676 mips_sim_insn = insn;
15677 note_stores (PATTERN (insn), mips_sim_record_set, state);
15678 }
15679
15680 /* Simulate issuing a NOP in state STATE. */
15681
15682 static void
15683 mips_sim_issue_nop (struct mips_sim *state)
15684 {
15685 if (state->insns_left == 0)
15686 mips_sim_next_cycle (state);
15687 state->insns_left--;
15688 }
15689
15690 /* Update simulation state STATE so that it's ready to accept the instruction
15691 after INSN. INSN should be part of the main rtl chain, not a member of a
15692 SEQUENCE. */
15693
15694 static void
15695 mips_sim_finish_insn (struct mips_sim *state, rtx insn)
15696 {
15697 /* If INSN is a jump with an implicit delay slot, simulate a nop. */
15698 if (JUMP_P (insn))
15699 mips_sim_issue_nop (state);
15700
15701 switch (GET_CODE (SEQ_BEGIN (insn)))
15702 {
15703 case CODE_LABEL:
15704 case CALL_INSN:
15705 /* We can't predict the processor state after a call or label. */
15706 mips_sim_reset (state);
15707 break;
15708
15709 case JUMP_INSN:
15710 /* The delay slots of branch likely instructions are only executed
15711 when the branch is taken. Therefore, if the caller has simulated
15712 the delay slot instruction, STATE does not really reflect the state
15713 of the pipeline for the instruction after the delay slot. Also,
15714 branch likely instructions tend to incur a penalty when not taken,
15715 so there will probably be an extra delay between the branch and
15716 the instruction after the delay slot. */
15717 if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
15718 mips_sim_reset (state);
15719 break;
15720
15721 default:
15722 break;
15723 }
15724 }
15725
15726 /* Use simulator state STATE to calculate the execution time of
15727 instruction sequence SEQ. */
15728
15729 static unsigned int
15730 mips_seq_time (struct mips_sim *state, rtx seq)
15731 {
15732 mips_sim_reset (state);
15733 for (rtx insn = seq; insn; insn = NEXT_INSN (insn))
15734 {
15735 mips_sim_wait_insn (state, insn);
15736 mips_sim_issue_insn (state, insn);
15737 }
15738 return state->time;
15739 }
15740 \f
15741 /* Return the execution-time cost of mips_tuning_info.fast_mult_zero_zero_p
15742 setting SETTING, using STATE to simulate instruction sequences. */
15743
15744 static unsigned int
15745 mips_mult_zero_zero_cost (struct mips_sim *state, bool setting)
15746 {
15747 mips_tuning_info.fast_mult_zero_zero_p = setting;
15748 start_sequence ();
15749
15750 enum machine_mode dword_mode = TARGET_64BIT ? TImode : DImode;
15751 rtx hilo = gen_rtx_REG (dword_mode, MD_REG_FIRST);
15752 mips_emit_move_or_split (hilo, const0_rtx, SPLIT_FOR_SPEED);
15753
15754 /* If the target provides mulsidi3_32bit then that's the most likely
15755 consumer of the result. Test for bypasses. */
15756 if (dword_mode == DImode && HAVE_maddsidi4)
15757 {
15758 rtx gpr = gen_rtx_REG (SImode, GP_REG_FIRST + 4);
15759 emit_insn (gen_maddsidi4 (hilo, gpr, gpr, hilo));
15760 }
15761
15762 unsigned int time = mips_seq_time (state, get_insns ());
15763 end_sequence ();
15764 return time;
15765 }
15766
15767 /* Check the relative speeds of "MULT $0,$0" and "MTLO $0; MTHI $0"
15768 and set up mips_tuning_info.fast_mult_zero_zero_p accordingly.
15769 Prefer MULT -- which is shorter -- in the event of a tie. */
15770
15771 static void
15772 mips_set_fast_mult_zero_zero_p (struct mips_sim *state)
15773 {
15774 if (TARGET_MIPS16)
15775 /* No MTLO or MTHI available. */
15776 mips_tuning_info.fast_mult_zero_zero_p = true;
15777 else
15778 {
15779 unsigned int true_time = mips_mult_zero_zero_cost (state, true);
15780 unsigned int false_time = mips_mult_zero_zero_cost (state, false);
15781 mips_tuning_info.fast_mult_zero_zero_p = (true_time <= false_time);
15782 }
15783 }
15784
15785 /* Set up costs based on the current architecture and tuning settings. */
15786
15787 static void
15788 mips_set_tuning_info (void)
15789 {
15790 if (mips_tuning_info.initialized_p
15791 && mips_tuning_info.arch == mips_arch
15792 && mips_tuning_info.tune == mips_tune
15793 && mips_tuning_info.mips16_p == TARGET_MIPS16)
15794 return;
15795
15796 mips_tuning_info.arch = mips_arch;
15797 mips_tuning_info.tune = mips_tune;
15798 mips_tuning_info.mips16_p = TARGET_MIPS16;
15799 mips_tuning_info.initialized_p = true;
15800
15801 dfa_start ();
15802
15803 struct mips_sim state;
15804 mips_sim_init (&state, alloca (state_size ()));
15805
15806 mips_set_fast_mult_zero_zero_p (&state);
15807
15808 dfa_finish ();
15809 }
15810
15811 /* Implement TARGET_EXPAND_TO_RTL_HOOK. */
15812
15813 static void
15814 mips_expand_to_rtl_hook (void)
15815 {
15816 /* We need to call this at a point where we can safely create sequences
15817 of instructions, so TARGET_OVERRIDE_OPTIONS is too early. We also
15818 need to call it at a point where the DFA infrastructure is not
15819 already in use, so we can't just call it lazily on demand.
15820
15821 At present, mips_tuning_info is only needed during post-expand
15822 RTL passes such as split_insns, so this hook should be early enough.
15823 We may need to move the call elsewhere if mips_tuning_info starts
15824 to be used for other things (such as rtx_costs, or expanders that
15825 could be called during gimple optimization). */
15826 mips_set_tuning_info ();
15827 }
15828 \f
15829 /* The VR4130 pipeline issues aligned pairs of instructions together,
15830 but it stalls the second instruction if it depends on the first.
15831 In order to cut down the amount of logic required, this dependence
15832 check is not based on a full instruction decode. Instead, any non-SPECIAL
15833 instruction is assumed to modify the register specified by bits 20-16
15834 (which is usually the "rt" field).
15835
15836 In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
15837 input, so we can end up with a false dependence between the branch
15838 and its delay slot. If this situation occurs in instruction INSN,
15839 try to avoid it by swapping rs and rt. */
15840
15841 static void
15842 vr4130_avoid_branch_rt_conflict (rtx insn)
15843 {
15844 rtx first, second;
15845
15846 first = SEQ_BEGIN (insn);
15847 second = SEQ_END (insn);
15848 if (JUMP_P (first)
15849 && NONJUMP_INSN_P (second)
15850 && GET_CODE (PATTERN (first)) == SET
15851 && GET_CODE (SET_DEST (PATTERN (first))) == PC
15852 && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
15853 {
15854 /* Check for the right kind of condition. */
15855 rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
15856 if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
15857 && REG_P (XEXP (cond, 0))
15858 && REG_P (XEXP (cond, 1))
15859 && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
15860 && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
15861 {
15862 /* SECOND mentions the rt register but not the rs register. */
15863 rtx tmp = XEXP (cond, 0);
15864 XEXP (cond, 0) = XEXP (cond, 1);
15865 XEXP (cond, 1) = tmp;
15866 }
15867 }
15868 }
15869
15870 /* Implement -mvr4130-align. Go through each basic block and simulate the
15871 processor pipeline. If we find that a pair of instructions could execute
15872 in parallel, and the first of those instructions is not 8-byte aligned,
15873 insert a nop to make it aligned. */
15874
15875 static void
15876 vr4130_align_insns (void)
15877 {
15878 struct mips_sim state;
15879 rtx insn, subinsn, last, last2, next;
15880 bool aligned_p;
15881
15882 dfa_start ();
15883
15884 /* LAST is the last instruction before INSN to have a nonzero length.
15885 LAST2 is the last such instruction before LAST. */
15886 last = 0;
15887 last2 = 0;
15888
15889 /* ALIGNED_P is true if INSN is known to be at an aligned address. */
15890 aligned_p = true;
15891
15892 mips_sim_init (&state, alloca (state_size ()));
15893 for (insn = get_insns (); insn != 0; insn = next)
15894 {
15895 unsigned int length;
15896
15897 next = NEXT_INSN (insn);
15898
15899 /* See the comment above vr4130_avoid_branch_rt_conflict for details.
15900 This isn't really related to the alignment pass, but we do it on
15901 the fly to avoid a separate instruction walk. */
15902 vr4130_avoid_branch_rt_conflict (insn);
15903
15904 length = get_attr_length (insn);
15905 if (length > 0 && USEFUL_INSN_P (insn))
15906 FOR_EACH_SUBINSN (subinsn, insn)
15907 {
15908 mips_sim_wait_insn (&state, subinsn);
15909
15910 /* If we want this instruction to issue in parallel with the
15911 previous one, make sure that the previous instruction is
15912 aligned. There are several reasons why this isn't worthwhile
15913 when the second instruction is a call:
15914
15915 - Calls are less likely to be performance critical,
15916 - There's a good chance that the delay slot can execute
15917 in parallel with the call.
15918 - The return address would then be unaligned.
15919
15920 In general, if we're going to insert a nop between instructions
15921 X and Y, it's better to insert it immediately after X. That
15922 way, if the nop makes Y aligned, it will also align any labels
15923 between X and Y. */
15924 if (state.insns_left != state.issue_rate
15925 && !CALL_P (subinsn))
15926 {
15927 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
15928 {
15929 /* SUBINSN is the first instruction in INSN and INSN is
15930 aligned. We want to align the previous instruction
15931 instead, so insert a nop between LAST2 and LAST.
15932
15933 Note that LAST could be either a single instruction
15934 or a branch with a delay slot. In the latter case,
15935 LAST, like INSN, is already aligned, but the delay
15936 slot must have some extra delay that stops it from
15937 issuing at the same time as the branch. We therefore
15938 insert a nop before the branch in order to align its
15939 delay slot. */
15940 gcc_assert (last2);
15941 emit_insn_after (gen_nop (), last2);
15942 aligned_p = false;
15943 }
15944 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
15945 {
15946 /* SUBINSN is the delay slot of INSN, but INSN is
15947 currently unaligned. Insert a nop between
15948 LAST and INSN to align it. */
15949 gcc_assert (last);
15950 emit_insn_after (gen_nop (), last);
15951 aligned_p = true;
15952 }
15953 }
15954 mips_sim_issue_insn (&state, subinsn);
15955 }
15956 mips_sim_finish_insn (&state, insn);
15957
15958 /* Update LAST, LAST2 and ALIGNED_P for the next instruction. */
15959 length = get_attr_length (insn);
15960 if (length > 0)
15961 {
15962 /* If the instruction is an asm statement or multi-instruction
15963 mips.md patern, the length is only an estimate. Insert an
15964 8 byte alignment after it so that the following instructions
15965 can be handled correctly. */
15966 if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
15967 && (recog_memoized (insn) < 0 || length >= 8))
15968 {
15969 next = emit_insn_after (gen_align (GEN_INT (3)), insn);
15970 next = NEXT_INSN (next);
15971 mips_sim_next_cycle (&state);
15972 aligned_p = true;
15973 }
15974 else if (length & 4)
15975 aligned_p = !aligned_p;
15976 last2 = last;
15977 last = insn;
15978 }
15979
15980 /* See whether INSN is an aligned label. */
15981 if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
15982 aligned_p = true;
15983 }
15984 dfa_finish ();
15985 }
15986 \f
15987 /* This structure records that the current function has a LO_SUM
15988 involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
15989 the largest offset applied to BASE by all such LO_SUMs. */
15990 struct mips_lo_sum_offset {
15991 rtx base;
15992 HOST_WIDE_INT offset;
15993 };
15994
15995 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE. */
15996
15997 static hashval_t
15998 mips_hash_base (rtx base)
15999 {
16000 int do_not_record_p;
16001
16002 return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
16003 }
16004
16005 /* Hashtable helpers. */
16006
16007 struct mips_lo_sum_offset_hasher : typed_free_remove <mips_lo_sum_offset>
16008 {
16009 typedef mips_lo_sum_offset value_type;
16010 typedef rtx_def compare_type;
16011 static inline hashval_t hash (const value_type *);
16012 static inline bool equal (const value_type *, const compare_type *);
16013 };
16014
16015 /* Hash-table callbacks for mips_lo_sum_offsets. */
16016
16017 inline hashval_t
16018 mips_lo_sum_offset_hasher::hash (const value_type *entry)
16019 {
16020 return mips_hash_base (entry->base);
16021 }
16022
16023 inline bool
16024 mips_lo_sum_offset_hasher::equal (const value_type *entry,
16025 const compare_type *value)
16026 {
16027 return rtx_equal_p (entry->base, value);
16028 }
16029
16030 typedef hash_table <mips_lo_sum_offset_hasher> mips_offset_table;
16031
16032 /* Look up symbolic constant X in HTAB, which is a hash table of
16033 mips_lo_sum_offsets. If OPTION is NO_INSERT, return true if X can be
16034 paired with a recorded LO_SUM, otherwise record X in the table. */
16035
16036 static bool
16037 mips_lo_sum_offset_lookup (mips_offset_table htab, rtx x,
16038 enum insert_option option)
16039 {
16040 rtx base, offset;
16041 mips_lo_sum_offset **slot;
16042 struct mips_lo_sum_offset *entry;
16043
16044 /* Split X into a base and offset. */
16045 split_const (x, &base, &offset);
16046 if (UNSPEC_ADDRESS_P (base))
16047 base = UNSPEC_ADDRESS (base);
16048
16049 /* Look up the base in the hash table. */
16050 slot = htab.find_slot_with_hash (base, mips_hash_base (base), option);
16051 if (slot == NULL)
16052 return false;
16053
16054 entry = (struct mips_lo_sum_offset *) *slot;
16055 if (option == INSERT)
16056 {
16057 if (entry == NULL)
16058 {
16059 entry = XNEW (struct mips_lo_sum_offset);
16060 entry->base = base;
16061 entry->offset = INTVAL (offset);
16062 *slot = entry;
16063 }
16064 else
16065 {
16066 if (INTVAL (offset) > entry->offset)
16067 entry->offset = INTVAL (offset);
16068 }
16069 }
16070 return INTVAL (offset) <= entry->offset;
16071 }
16072
16073 /* A for_each_rtx callback for which DATA is a mips_lo_sum_offset hash table.
16074 Record every LO_SUM in *LOC. */
16075
16076 static int
16077 mips_record_lo_sum (rtx *loc, void *data)
16078 {
16079 if (GET_CODE (*loc) == LO_SUM)
16080 mips_lo_sum_offset_lookup (*(mips_offset_table*) data,
16081 XEXP (*loc, 1), INSERT);
16082 return 0;
16083 }
16084
16085 /* Return true if INSN is a SET of an orphaned high-part relocation.
16086 HTAB is a hash table of mips_lo_sum_offsets that describes all the
16087 LO_SUMs in the current function. */
16088
16089 static bool
16090 mips_orphaned_high_part_p (mips_offset_table htab, rtx insn)
16091 {
16092 enum mips_symbol_type type;
16093 rtx x, set;
16094
16095 set = single_set (insn);
16096 if (set)
16097 {
16098 /* Check for %his. */
16099 x = SET_SRC (set);
16100 if (GET_CODE (x) == HIGH
16101 && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
16102 return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
16103
16104 /* Check for local %gots (and %got_pages, which is redundant but OK). */
16105 if (GET_CODE (x) == UNSPEC
16106 && XINT (x, 1) == UNSPEC_LOAD_GOT
16107 && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
16108 SYMBOL_CONTEXT_LEA, &type)
16109 && type == SYMBOL_GOTOFF_PAGE)
16110 return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
16111 }
16112 return false;
16113 }
16114
16115 /* Subroutine of mips_reorg_process_insns. If there is a hazard between
16116 INSN and a previous instruction, avoid it by inserting nops after
16117 instruction AFTER.
16118
16119 *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
16120 this point. If *DELAYED_REG is non-null, INSN must wait a cycle
16121 before using the value of that register. *HILO_DELAY counts the
16122 number of instructions since the last hilo hazard (that is,
16123 the number of instructions since the last MFLO or MFHI).
16124
16125 After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
16126 for the next instruction.
16127
16128 LO_REG is an rtx for the LO register, used in dependence checking. */
16129
16130 static void
16131 mips_avoid_hazard (rtx after, rtx insn, int *hilo_delay,
16132 rtx *delayed_reg, rtx lo_reg)
16133 {
16134 rtx pattern, set;
16135 int nops, ninsns;
16136
16137 pattern = PATTERN (insn);
16138
16139 /* Do not put the whole function in .set noreorder if it contains
16140 an asm statement. We don't know whether there will be hazards
16141 between the asm statement and the gcc-generated code. */
16142 if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
16143 cfun->machine->all_noreorder_p = false;
16144
16145 /* Ignore zero-length instructions (barriers and the like). */
16146 ninsns = get_attr_length (insn) / 4;
16147 if (ninsns == 0)
16148 return;
16149
16150 /* Work out how many nops are needed. Note that we only care about
16151 registers that are explicitly mentioned in the instruction's pattern.
16152 It doesn't matter that calls use the argument registers or that they
16153 clobber hi and lo. */
16154 if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
16155 nops = 2 - *hilo_delay;
16156 else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
16157 nops = 1;
16158 else
16159 nops = 0;
16160
16161 /* Insert the nops between this instruction and the previous one.
16162 Each new nop takes us further from the last hilo hazard. */
16163 *hilo_delay += nops;
16164 while (nops-- > 0)
16165 emit_insn_after (gen_hazard_nop (), after);
16166
16167 /* Set up the state for the next instruction. */
16168 *hilo_delay += ninsns;
16169 *delayed_reg = 0;
16170 if (INSN_CODE (insn) >= 0)
16171 switch (get_attr_hazard (insn))
16172 {
16173 case HAZARD_NONE:
16174 break;
16175
16176 case HAZARD_HILO:
16177 *hilo_delay = 0;
16178 break;
16179
16180 case HAZARD_DELAY:
16181 set = single_set (insn);
16182 gcc_assert (set);
16183 *delayed_reg = SET_DEST (set);
16184 break;
16185 }
16186 }
16187
16188 /* Go through the instruction stream and insert nops where necessary.
16189 Also delete any high-part relocations whose partnering low parts
16190 are now all dead. See if the whole function can then be put into
16191 .set noreorder and .set nomacro. */
16192
16193 static void
16194 mips_reorg_process_insns (void)
16195 {
16196 rtx insn, last_insn, subinsn, next_insn, lo_reg, delayed_reg;
16197 int hilo_delay;
16198 mips_offset_table htab;
16199
16200 /* Force all instructions to be split into their final form. */
16201 split_all_insns_noflow ();
16202
16203 /* Recalculate instruction lengths without taking nops into account. */
16204 cfun->machine->ignore_hazard_length_p = true;
16205 shorten_branches (get_insns ());
16206
16207 cfun->machine->all_noreorder_p = true;
16208
16209 /* We don't track MIPS16 PC-relative offsets closely enough to make
16210 a good job of "set .noreorder" code in MIPS16 mode. */
16211 if (TARGET_MIPS16)
16212 cfun->machine->all_noreorder_p = false;
16213
16214 /* Code that doesn't use explicit relocs can't be ".set nomacro". */
16215 if (!TARGET_EXPLICIT_RELOCS)
16216 cfun->machine->all_noreorder_p = false;
16217
16218 /* Profiled functions can't be all noreorder because the profiler
16219 support uses assembler macros. */
16220 if (crtl->profile)
16221 cfun->machine->all_noreorder_p = false;
16222
16223 /* Code compiled with -mfix-vr4120, -mfix-rm7000 or -mfix-24k can't be
16224 all noreorder because we rely on the assembler to work around some
16225 errata. The R5900 too has several bugs. */
16226 if (TARGET_FIX_VR4120
16227 || TARGET_FIX_RM7000
16228 || TARGET_FIX_24K
16229 || TARGET_MIPS5900)
16230 cfun->machine->all_noreorder_p = false;
16231
16232 /* The same is true for -mfix-vr4130 if we might generate MFLO or
16233 MFHI instructions. Note that we avoid using MFLO and MFHI if
16234 the VR4130 MACC and DMACC instructions are available instead;
16235 see the *mfhilo_{si,di}_macc patterns. */
16236 if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
16237 cfun->machine->all_noreorder_p = false;
16238
16239 htab.create (37);
16240
16241 /* Make a first pass over the instructions, recording all the LO_SUMs. */
16242 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
16243 FOR_EACH_SUBINSN (subinsn, insn)
16244 if (USEFUL_INSN_P (subinsn))
16245 {
16246 rtx body = PATTERN (insn);
16247 int noperands = asm_noperands (body);
16248 if (noperands >= 0)
16249 {
16250 rtx *ops = XALLOCAVEC (rtx, noperands);
16251 bool *used = XALLOCAVEC (bool, noperands);
16252 const char *string = decode_asm_operands (body, ops, NULL, NULL,
16253 NULL, NULL);
16254 get_referenced_operands (string, used, noperands);
16255 for (int i = 0; i < noperands; ++i)
16256 if (used[i])
16257 for_each_rtx (&ops[i], mips_record_lo_sum, &htab);
16258 }
16259 else
16260 for_each_rtx (&PATTERN (subinsn), mips_record_lo_sum, &htab);
16261 }
16262
16263 last_insn = 0;
16264 hilo_delay = 2;
16265 delayed_reg = 0;
16266 lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
16267
16268 /* Make a second pass over the instructions. Delete orphaned
16269 high-part relocations or turn them into NOPs. Avoid hazards
16270 by inserting NOPs. */
16271 for (insn = get_insns (); insn != 0; insn = next_insn)
16272 {
16273 next_insn = NEXT_INSN (insn);
16274 if (USEFUL_INSN_P (insn))
16275 {
16276 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
16277 {
16278 /* If we find an orphaned high-part relocation in a delay
16279 slot, it's easier to turn that instruction into a NOP than
16280 to delete it. The delay slot will be a NOP either way. */
16281 FOR_EACH_SUBINSN (subinsn, insn)
16282 if (INSN_P (subinsn))
16283 {
16284 if (mips_orphaned_high_part_p (htab, subinsn))
16285 {
16286 PATTERN (subinsn) = gen_nop ();
16287 INSN_CODE (subinsn) = CODE_FOR_nop;
16288 }
16289 mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
16290 &delayed_reg, lo_reg);
16291 }
16292 last_insn = insn;
16293 }
16294 else
16295 {
16296 /* INSN is a single instruction. Delete it if it's an
16297 orphaned high-part relocation. */
16298 if (mips_orphaned_high_part_p (htab, insn))
16299 delete_insn (insn);
16300 /* Also delete cache barriers if the last instruction
16301 was an annulled branch. INSN will not be speculatively
16302 executed. */
16303 else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
16304 && last_insn
16305 && JUMP_P (SEQ_BEGIN (last_insn))
16306 && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
16307 delete_insn (insn);
16308 else
16309 {
16310 mips_avoid_hazard (last_insn, insn, &hilo_delay,
16311 &delayed_reg, lo_reg);
16312 last_insn = insn;
16313 }
16314 }
16315 }
16316 }
16317
16318 htab.dispose ();
16319 }
16320
16321 /* Return true if the function has a long branch instruction. */
16322
16323 static bool
16324 mips_has_long_branch_p (void)
16325 {
16326 rtx insn, subinsn;
16327 int normal_length;
16328
16329 /* We need up-to-date instruction lengths. */
16330 shorten_branches (get_insns ());
16331
16332 /* Look for a branch that is longer than normal. The normal length for
16333 non-MIPS16 branches is 8, because the length includes the delay slot.
16334 It is 4 for MIPS16, because MIPS16 branches are extended instructions,
16335 but they have no delay slot. */
16336 normal_length = (TARGET_MIPS16 ? 4 : 8);
16337 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16338 FOR_EACH_SUBINSN (subinsn, insn)
16339 if (JUMP_P (subinsn)
16340 && get_attr_length (subinsn) > normal_length
16341 && (any_condjump_p (subinsn) || any_uncondjump_p (subinsn)))
16342 return true;
16343
16344 return false;
16345 }
16346
16347 /* If we are using a GOT, but have not decided to use a global pointer yet,
16348 see whether we need one to implement long branches. Convert the ghost
16349 global-pointer instructions into real ones if so. */
16350
16351 static bool
16352 mips_expand_ghost_gp_insns (void)
16353 {
16354 /* Quick exit if we already know that we will or won't need a
16355 global pointer. */
16356 if (!TARGET_USE_GOT
16357 || cfun->machine->global_pointer == INVALID_REGNUM
16358 || mips_must_initialize_gp_p ())
16359 return false;
16360
16361 /* Run a full check for long branches. */
16362 if (!mips_has_long_branch_p ())
16363 return false;
16364
16365 /* We've now established that we need $gp. */
16366 cfun->machine->must_initialize_gp_p = true;
16367 split_all_insns_noflow ();
16368
16369 return true;
16370 }
16371
16372 /* Subroutine of mips_reorg to manage passes that require DF. */
16373
16374 static void
16375 mips_df_reorg (void)
16376 {
16377 /* Create def-use chains. */
16378 df_set_flags (DF_EQ_NOTES);
16379 df_chain_add_problem (DF_UD_CHAIN);
16380 df_analyze ();
16381
16382 if (TARGET_RELAX_PIC_CALLS)
16383 mips_annotate_pic_calls ();
16384
16385 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
16386 r10k_insert_cache_barriers ();
16387
16388 df_finish_pass (false);
16389 }
16390
16391 /* Emit code to load LABEL_REF SRC into MIPS16 register DEST. This is
16392 called very late in mips_reorg, but the caller is required to run
16393 mips16_lay_out_constants on the result. */
16394
16395 static void
16396 mips16_load_branch_target (rtx dest, rtx src)
16397 {
16398 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
16399 {
16400 rtx page, low;
16401
16402 if (mips_cfun_has_cprestore_slot_p ())
16403 mips_emit_move (dest, mips_cprestore_slot (dest, true));
16404 else
16405 mips_emit_move (dest, pic_offset_table_rtx);
16406 page = mips_unspec_address (src, SYMBOL_GOTOFF_PAGE);
16407 low = mips_unspec_address (src, SYMBOL_GOT_PAGE_OFST);
16408 emit_insn (gen_rtx_SET (VOIDmode, dest,
16409 PMODE_INSN (gen_unspec_got, (dest, page))));
16410 emit_insn (gen_rtx_SET (VOIDmode, dest,
16411 gen_rtx_LO_SUM (Pmode, dest, low)));
16412 }
16413 else
16414 {
16415 src = mips_unspec_address (src, SYMBOL_ABSOLUTE);
16416 mips_emit_move (dest, src);
16417 }
16418 }
16419
16420 /* If we're compiling a MIPS16 function, look for and split any long branches.
16421 This must be called after all other instruction modifications in
16422 mips_reorg. */
16423
16424 static void
16425 mips16_split_long_branches (void)
16426 {
16427 bool something_changed;
16428
16429 if (!TARGET_MIPS16)
16430 return;
16431
16432 /* Loop until the alignments for all targets are sufficient. */
16433 do
16434 {
16435 rtx insn;
16436
16437 shorten_branches (get_insns ());
16438 something_changed = false;
16439 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16440 if (JUMP_P (insn)
16441 && get_attr_length (insn) > 4
16442 && (any_condjump_p (insn) || any_uncondjump_p (insn)))
16443 {
16444 rtx old_label, new_label, temp, saved_temp;
16445 rtx target, jump, jump_sequence;
16446
16447 start_sequence ();
16448
16449 /* Free up a MIPS16 register by saving it in $1. */
16450 saved_temp = gen_rtx_REG (Pmode, AT_REGNUM);
16451 temp = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
16452 emit_move_insn (saved_temp, temp);
16453
16454 /* Load the branch target into TEMP. */
16455 old_label = JUMP_LABEL (insn);
16456 target = gen_rtx_LABEL_REF (Pmode, old_label);
16457 mips16_load_branch_target (temp, target);
16458
16459 /* Jump to the target and restore the register's
16460 original value. */
16461 jump = emit_jump_insn (PMODE_INSN (gen_indirect_jump_and_restore,
16462 (temp, temp, saved_temp)));
16463 JUMP_LABEL (jump) = old_label;
16464 LABEL_NUSES (old_label)++;
16465
16466 /* Rewrite any symbolic references that are supposed to use
16467 a PC-relative constant pool. */
16468 mips16_lay_out_constants (false);
16469
16470 if (simplejump_p (insn))
16471 /* We're going to replace INSN with a longer form. */
16472 new_label = NULL_RTX;
16473 else
16474 {
16475 /* Create a branch-around label for the original
16476 instruction. */
16477 new_label = gen_label_rtx ();
16478 emit_label (new_label);
16479 }
16480
16481 jump_sequence = get_insns ();
16482 end_sequence ();
16483
16484 emit_insn_after (jump_sequence, insn);
16485 if (new_label)
16486 invert_jump (insn, new_label, false);
16487 else
16488 delete_insn (insn);
16489 something_changed = true;
16490 }
16491 }
16492 while (something_changed);
16493 }
16494
16495 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
16496
16497 static void
16498 mips_reorg (void)
16499 {
16500 /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF. Also during
16501 insn splitting in mips16_lay_out_constants, DF insn info is only kept up
16502 to date if the CFG is available. */
16503 if (mips_cfg_in_reorg ())
16504 compute_bb_for_insn ();
16505 mips16_lay_out_constants (true);
16506 if (mips_cfg_in_reorg ())
16507 {
16508 mips_df_reorg ();
16509 free_bb_for_insn ();
16510 }
16511 }
16512
16513 /* We use a machine specific pass to do a second machine dependent reorg
16514 pass after delay branch scheduling. */
16515
16516 static unsigned int
16517 mips_machine_reorg2 (void)
16518 {
16519 mips_reorg_process_insns ();
16520 if (!TARGET_MIPS16
16521 && TARGET_EXPLICIT_RELOCS
16522 && TUNE_MIPS4130
16523 && TARGET_VR4130_ALIGN)
16524 vr4130_align_insns ();
16525 if (mips_expand_ghost_gp_insns ())
16526 /* The expansion could invalidate some of the VR4130 alignment
16527 optimizations, but this should be an extremely rare case anyhow. */
16528 mips_reorg_process_insns ();
16529 mips16_split_long_branches ();
16530 return 0;
16531 }
16532
16533 namespace {
16534
16535 const pass_data pass_data_mips_machine_reorg2 =
16536 {
16537 RTL_PASS, /* type */
16538 "mach2", /* name */
16539 OPTGROUP_NONE, /* optinfo_flags */
16540 true, /* has_execute */
16541 TV_MACH_DEP, /* tv_id */
16542 0, /* properties_required */
16543 0, /* properties_provided */
16544 0, /* properties_destroyed */
16545 0, /* todo_flags_start */
16546 TODO_verify_rtl_sharing, /* todo_flags_finish */
16547 };
16548
16549 class pass_mips_machine_reorg2 : public rtl_opt_pass
16550 {
16551 public:
16552 pass_mips_machine_reorg2(gcc::context *ctxt)
16553 : rtl_opt_pass(pass_data_mips_machine_reorg2, ctxt)
16554 {}
16555
16556 /* opt_pass methods: */
16557 virtual unsigned int execute (function *) { return mips_machine_reorg2 (); }
16558
16559 }; // class pass_mips_machine_reorg2
16560
16561 } // anon namespace
16562
16563 rtl_opt_pass *
16564 make_pass_mips_machine_reorg2 (gcc::context *ctxt)
16565 {
16566 return new pass_mips_machine_reorg2 (ctxt);
16567 }
16568
16569 \f
16570 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
16571 in order to avoid duplicating too much logic from elsewhere. */
16572
16573 static void
16574 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
16575 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
16576 tree function)
16577 {
16578 rtx this_rtx, temp1, temp2, insn, fnaddr;
16579 bool use_sibcall_p;
16580
16581 /* Pretend to be a post-reload pass while generating rtl. */
16582 reload_completed = 1;
16583
16584 /* Mark the end of the (empty) prologue. */
16585 emit_note (NOTE_INSN_PROLOGUE_END);
16586
16587 /* Determine if we can use a sibcall to call FUNCTION directly. */
16588 fnaddr = XEXP (DECL_RTL (function), 0);
16589 use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
16590 && const_call_insn_operand (fnaddr, Pmode));
16591
16592 /* Determine if we need to load FNADDR from the GOT. */
16593 if (!use_sibcall_p
16594 && (mips_got_symbol_type_p
16595 (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
16596 {
16597 /* Pick a global pointer. Use a call-clobbered register if
16598 TARGET_CALL_SAVED_GP. */
16599 cfun->machine->global_pointer
16600 = TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
16601 cfun->machine->must_initialize_gp_p = true;
16602 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
16603
16604 /* Set up the global pointer for n32 or n64 abicalls. */
16605 mips_emit_loadgp ();
16606 }
16607
16608 /* We need two temporary registers in some cases. */
16609 temp1 = gen_rtx_REG (Pmode, 2);
16610 temp2 = gen_rtx_REG (Pmode, 3);
16611
16612 /* Find out which register contains the "this" pointer. */
16613 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
16614 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
16615 else
16616 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
16617
16618 /* Add DELTA to THIS_RTX. */
16619 if (delta != 0)
16620 {
16621 rtx offset = GEN_INT (delta);
16622 if (!SMALL_OPERAND (delta))
16623 {
16624 mips_emit_move (temp1, offset);
16625 offset = temp1;
16626 }
16627 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
16628 }
16629
16630 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
16631 if (vcall_offset != 0)
16632 {
16633 rtx addr;
16634
16635 /* Set TEMP1 to *THIS_RTX. */
16636 mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
16637
16638 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
16639 addr = mips_add_offset (temp2, temp1, vcall_offset);
16640
16641 /* Load the offset and add it to THIS_RTX. */
16642 mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
16643 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
16644 }
16645
16646 /* Jump to the target function. Use a sibcall if direct jumps are
16647 allowed, otherwise load the address into a register first. */
16648 if (use_sibcall_p)
16649 {
16650 insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
16651 SIBLING_CALL_P (insn) = 1;
16652 }
16653 else
16654 {
16655 /* This is messy. GAS treats "la $25,foo" as part of a call
16656 sequence and may allow a global "foo" to be lazily bound.
16657 The general move patterns therefore reject this combination.
16658
16659 In this context, lazy binding would actually be OK
16660 for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
16661 TARGET_CALL_SAVED_GP; see mips_load_call_address.
16662 We must therefore load the address via a temporary
16663 register if mips_dangerous_for_la25_p.
16664
16665 If we jump to the temporary register rather than $25,
16666 the assembler can use the move insn to fill the jump's
16667 delay slot.
16668
16669 We can use the same technique for MIPS16 code, where $25
16670 is not a valid JR register. */
16671 if (TARGET_USE_PIC_FN_ADDR_REG
16672 && !TARGET_MIPS16
16673 && !mips_dangerous_for_la25_p (fnaddr))
16674 temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
16675 mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
16676
16677 if (TARGET_USE_PIC_FN_ADDR_REG
16678 && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
16679 mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
16680 emit_jump_insn (gen_indirect_jump (temp1));
16681 }
16682
16683 /* Run just enough of rest_of_compilation. This sequence was
16684 "borrowed" from alpha.c. */
16685 insn = get_insns ();
16686 split_all_insns_noflow ();
16687 mips16_lay_out_constants (true);
16688 shorten_branches (insn);
16689 final_start_function (insn, file, 1);
16690 final (insn, file, 1);
16691 final_end_function ();
16692
16693 /* Clean up the vars set above. Note that final_end_function resets
16694 the global pointer for us. */
16695 reload_completed = 0;
16696 }
16697 \f
16698
16699 /* The last argument passed to mips_set_compression_mode,
16700 or negative if the function hasn't been called yet. */
16701 static unsigned int old_compression_mode = -1;
16702
16703 /* Set up the target-dependent global state for ISA mode COMPRESSION_MODE,
16704 which is either MASK_MIPS16 or MASK_MICROMIPS. */
16705
16706 static void
16707 mips_set_compression_mode (unsigned int compression_mode)
16708 {
16709
16710 if (compression_mode == old_compression_mode)
16711 return;
16712
16713 /* Restore base settings of various flags. */
16714 target_flags = mips_base_target_flags;
16715 flag_schedule_insns = mips_base_schedule_insns;
16716 flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
16717 flag_move_loop_invariants = mips_base_move_loop_invariants;
16718 align_loops = mips_base_align_loops;
16719 align_jumps = mips_base_align_jumps;
16720 align_functions = mips_base_align_functions;
16721 target_flags &= ~(MASK_MIPS16 | MASK_MICROMIPS);
16722 target_flags |= compression_mode;
16723
16724 if (compression_mode & MASK_MIPS16)
16725 {
16726 /* Switch to MIPS16 mode. */
16727 target_flags |= MASK_MIPS16;
16728
16729 /* Turn off SYNCI if it was on, MIPS16 doesn't support it. */
16730 target_flags &= ~MASK_SYNCI;
16731
16732 /* Don't run the scheduler before reload, since it tends to
16733 increase register pressure. */
16734 flag_schedule_insns = 0;
16735
16736 /* Don't do hot/cold partitioning. mips16_lay_out_constants expects
16737 the whole function to be in a single section. */
16738 flag_reorder_blocks_and_partition = 0;
16739
16740 /* Don't move loop invariants, because it tends to increase
16741 register pressure. It also introduces an extra move in cases
16742 where the constant is the first operand in a two-operand binary
16743 instruction, or when it forms a register argument to a functon
16744 call. */
16745 flag_move_loop_invariants = 0;
16746
16747 target_flags |= MASK_EXPLICIT_RELOCS;
16748
16749 /* Experiments suggest we get the best overall section-anchor
16750 results from using the range of an unextended LW or SW. Code
16751 that makes heavy use of byte or short accesses can do better
16752 with ranges of 0...31 and 0...63 respectively, but most code is
16753 sensitive to the range of LW and SW instead. */
16754 targetm.min_anchor_offset = 0;
16755 targetm.max_anchor_offset = 127;
16756
16757 targetm.const_anchor = 0;
16758
16759 /* MIPS16 has no BAL instruction. */
16760 target_flags &= ~MASK_RELAX_PIC_CALLS;
16761
16762 /* The R4000 errata don't apply to any known MIPS16 cores.
16763 It's simpler to make the R4000 fixes and MIPS16 mode
16764 mutually exclusive. */
16765 target_flags &= ~MASK_FIX_R4000;
16766
16767 if (flag_pic && !TARGET_OLDABI)
16768 sorry ("MIPS16 PIC for ABIs other than o32 and o64");
16769
16770 if (TARGET_XGOT)
16771 sorry ("MIPS16 -mxgot code");
16772
16773 if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
16774 sorry ("hard-float MIPS16 code for ABIs other than o32 and o64");
16775 }
16776 else
16777 {
16778 /* Switch to microMIPS or the standard encoding. */
16779
16780 if (TARGET_MICROMIPS)
16781 /* Avoid branch likely. */
16782 target_flags &= ~MASK_BRANCHLIKELY;
16783
16784 /* Provide default values for align_* for 64-bit targets. */
16785 if (TARGET_64BIT)
16786 {
16787 if (align_loops == 0)
16788 align_loops = 8;
16789 if (align_jumps == 0)
16790 align_jumps = 8;
16791 if (align_functions == 0)
16792 align_functions = 8;
16793 }
16794
16795 targetm.min_anchor_offset = -32768;
16796 targetm.max_anchor_offset = 32767;
16797
16798 targetm.const_anchor = 0x8000;
16799 }
16800
16801 /* (Re)initialize MIPS target internals for new ISA. */
16802 mips_init_relocs ();
16803
16804 if (compression_mode & MASK_MIPS16)
16805 {
16806 if (!mips16_globals)
16807 mips16_globals = save_target_globals_default_opts ();
16808 else
16809 restore_target_globals (mips16_globals);
16810 }
16811 else
16812 restore_target_globals (&default_target_globals);
16813
16814 old_compression_mode = compression_mode;
16815 }
16816
16817 /* Implement TARGET_SET_CURRENT_FUNCTION. Decide whether the current
16818 function should use the MIPS16 or microMIPS ISA and switch modes
16819 accordingly. */
16820
16821 static void
16822 mips_set_current_function (tree fndecl)
16823 {
16824 mips_set_compression_mode (mips_get_compress_mode (fndecl));
16825 }
16826 \f
16827 /* Allocate a chunk of memory for per-function machine-dependent data. */
16828
16829 static struct machine_function *
16830 mips_init_machine_status (void)
16831 {
16832 return ggc_alloc_cleared_machine_function ();
16833 }
16834
16835 /* Return the processor associated with the given ISA level, or null
16836 if the ISA isn't valid. */
16837
16838 static const struct mips_cpu_info *
16839 mips_cpu_info_from_isa (int isa)
16840 {
16841 unsigned int i;
16842
16843 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16844 if (mips_cpu_info_table[i].isa == isa)
16845 return mips_cpu_info_table + i;
16846
16847 return NULL;
16848 }
16849
16850 /* Return a mips_cpu_info entry determined by an option valued
16851 OPT. */
16852
16853 static const struct mips_cpu_info *
16854 mips_cpu_info_from_opt (int opt)
16855 {
16856 switch (opt)
16857 {
16858 case MIPS_ARCH_OPTION_FROM_ABI:
16859 /* 'from-abi' selects the most compatible architecture for the
16860 given ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit
16861 ABIs. For the EABIs, we have to decide whether we're using
16862 the 32-bit or 64-bit version. */
16863 return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
16864 : ABI_NEEDS_64BIT_REGS ? 3
16865 : (TARGET_64BIT ? 3 : 1));
16866
16867 case MIPS_ARCH_OPTION_NATIVE:
16868 gcc_unreachable ();
16869
16870 default:
16871 return &mips_cpu_info_table[opt];
16872 }
16873 }
16874
16875 /* Return a default mips_cpu_info entry, given that no -march= option
16876 was explicitly specified. */
16877
16878 static const struct mips_cpu_info *
16879 mips_default_arch (void)
16880 {
16881 #if defined (MIPS_CPU_STRING_DEFAULT)
16882 unsigned int i;
16883 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16884 if (strcmp (mips_cpu_info_table[i].name, MIPS_CPU_STRING_DEFAULT) == 0)
16885 return mips_cpu_info_table + i;
16886 gcc_unreachable ();
16887 #elif defined (MIPS_ISA_DEFAULT)
16888 return mips_cpu_info_from_isa (MIPS_ISA_DEFAULT);
16889 #else
16890 /* 'from-abi' makes a good default: you get whatever the ABI
16891 requires. */
16892 return mips_cpu_info_from_opt (MIPS_ARCH_OPTION_FROM_ABI);
16893 #endif
16894 }
16895
16896 /* Set up globals to generate code for the ISA or processor
16897 described by INFO. */
16898
16899 static void
16900 mips_set_architecture (const struct mips_cpu_info *info)
16901 {
16902 if (info != 0)
16903 {
16904 mips_arch_info = info;
16905 mips_arch = info->cpu;
16906 mips_isa = info->isa;
16907 if (mips_isa < 32)
16908 mips_isa_rev = 0;
16909 else
16910 mips_isa_rev = (mips_isa & 31) + 1;
16911 }
16912 }
16913
16914 /* Likewise for tuning. */
16915
16916 static void
16917 mips_set_tune (const struct mips_cpu_info *info)
16918 {
16919 if (info != 0)
16920 {
16921 mips_tune_info = info;
16922 mips_tune = info->cpu;
16923 }
16924 }
16925
16926 /* Implement TARGET_OPTION_OVERRIDE. */
16927
16928 static void
16929 mips_option_override (void)
16930 {
16931 int i, start, regno, mode;
16932
16933 if (global_options_set.x_mips_isa_option)
16934 mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
16935
16936 #ifdef SUBTARGET_OVERRIDE_OPTIONS
16937 SUBTARGET_OVERRIDE_OPTIONS;
16938 #endif
16939
16940 /* MIPS16 and microMIPS cannot coexist. */
16941 if (TARGET_MICROMIPS && TARGET_MIPS16)
16942 error ("unsupported combination: %s", "-mips16 -mmicromips");
16943
16944 /* Save the base compression state and process flags as though we
16945 were generating uncompressed code. */
16946 mips_base_compression_flags = TARGET_COMPRESSION;
16947 target_flags &= ~TARGET_COMPRESSION;
16948
16949 /* -mno-float overrides -mhard-float and -msoft-float. */
16950 if (TARGET_NO_FLOAT)
16951 {
16952 target_flags |= MASK_SOFT_FLOAT_ABI;
16953 target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
16954 }
16955
16956 if (TARGET_FLIP_MIPS16)
16957 TARGET_INTERLINK_COMPRESSED = 1;
16958
16959 /* Set the small data limit. */
16960 mips_small_data_threshold = (global_options_set.x_g_switch_value
16961 ? g_switch_value
16962 : MIPS_DEFAULT_GVALUE);
16963
16964 /* The following code determines the architecture and register size.
16965 Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
16966 The GAS and GCC code should be kept in sync as much as possible. */
16967
16968 if (global_options_set.x_mips_arch_option)
16969 mips_set_architecture (mips_cpu_info_from_opt (mips_arch_option));
16970
16971 if (mips_isa_option_info != 0)
16972 {
16973 if (mips_arch_info == 0)
16974 mips_set_architecture (mips_isa_option_info);
16975 else if (mips_arch_info->isa != mips_isa_option_info->isa)
16976 error ("%<-%s%> conflicts with the other architecture options, "
16977 "which specify a %s processor",
16978 mips_isa_option_info->name,
16979 mips_cpu_info_from_isa (mips_arch_info->isa)->name);
16980 }
16981
16982 if (mips_arch_info == 0)
16983 mips_set_architecture (mips_default_arch ());
16984
16985 if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
16986 error ("%<-march=%s%> is not compatible with the selected ABI",
16987 mips_arch_info->name);
16988
16989 /* Optimize for mips_arch, unless -mtune selects a different processor. */
16990 if (global_options_set.x_mips_tune_option)
16991 mips_set_tune (mips_cpu_info_from_opt (mips_tune_option));
16992
16993 if (mips_tune_info == 0)
16994 mips_set_tune (mips_arch_info);
16995
16996 if ((target_flags_explicit & MASK_64BIT) != 0)
16997 {
16998 /* The user specified the size of the integer registers. Make sure
16999 it agrees with the ABI and ISA. */
17000 if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
17001 error ("%<-mgp64%> used with a 32-bit processor");
17002 else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
17003 error ("%<-mgp32%> used with a 64-bit ABI");
17004 else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
17005 error ("%<-mgp64%> used with a 32-bit ABI");
17006 }
17007 else
17008 {
17009 /* Infer the integer register size from the ABI and processor.
17010 Restrict ourselves to 32-bit registers if that's all the
17011 processor has, or if the ABI cannot handle 64-bit registers. */
17012 if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
17013 target_flags &= ~MASK_64BIT;
17014 else
17015 target_flags |= MASK_64BIT;
17016 }
17017
17018 if ((target_flags_explicit & MASK_FLOAT64) != 0)
17019 {
17020 if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
17021 error ("unsupported combination: %s", "-mfp64 -msingle-float");
17022 else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
17023 error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
17024 else if (!TARGET_64BIT && TARGET_FLOAT64)
17025 {
17026 if (!ISA_HAS_MXHC1)
17027 error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
17028 " the target supports the mfhc1 and mthc1 instructions");
17029 else if (mips_abi != ABI_32)
17030 error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
17031 " the o32 ABI");
17032 }
17033 }
17034 else
17035 {
17036 /* -msingle-float selects 32-bit float registers. Otherwise the
17037 float registers should be the same size as the integer ones. */
17038 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
17039 target_flags |= MASK_FLOAT64;
17040 else
17041 target_flags &= ~MASK_FLOAT64;
17042 }
17043
17044 /* End of code shared with GAS. */
17045
17046 /* The R5900 FPU only supports single precision. */
17047 if (TARGET_MIPS5900 && TARGET_HARD_FLOAT_ABI && TARGET_DOUBLE_FLOAT)
17048 error ("unsupported combination: %s",
17049 "-march=r5900 -mhard-float -mdouble-float");
17050
17051 /* If a -mlong* option was given, check that it matches the ABI,
17052 otherwise infer the -mlong* setting from the other options. */
17053 if ((target_flags_explicit & MASK_LONG64) != 0)
17054 {
17055 if (TARGET_LONG64)
17056 {
17057 if (mips_abi == ABI_N32)
17058 error ("%qs is incompatible with %qs", "-mabi=n32", "-mlong64");
17059 else if (mips_abi == ABI_32)
17060 error ("%qs is incompatible with %qs", "-mabi=32", "-mlong64");
17061 else if (mips_abi == ABI_O64 && TARGET_ABICALLS)
17062 /* We have traditionally allowed non-abicalls code to use
17063 an LP64 form of o64. However, it would take a bit more
17064 effort to support the combination of 32-bit GOT entries
17065 and 64-bit pointers, so we treat the abicalls case as
17066 an error. */
17067 error ("the combination of %qs and %qs is incompatible with %qs",
17068 "-mabi=o64", "-mabicalls", "-mlong64");
17069 }
17070 else
17071 {
17072 if (mips_abi == ABI_64)
17073 error ("%qs is incompatible with %qs", "-mabi=64", "-mlong32");
17074 }
17075 }
17076 else
17077 {
17078 if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
17079 target_flags |= MASK_LONG64;
17080 else
17081 target_flags &= ~MASK_LONG64;
17082 }
17083
17084 if (!TARGET_OLDABI)
17085 flag_pcc_struct_return = 0;
17086
17087 /* Decide which rtx_costs structure to use. */
17088 if (optimize_size)
17089 mips_cost = &mips_rtx_cost_optimize_size;
17090 else
17091 mips_cost = &mips_rtx_cost_data[mips_tune];
17092
17093 /* If the user hasn't specified a branch cost, use the processor's
17094 default. */
17095 if (mips_branch_cost == 0)
17096 mips_branch_cost = mips_cost->branch_cost;
17097
17098 /* If neither -mbranch-likely nor -mno-branch-likely was given
17099 on the command line, set MASK_BRANCHLIKELY based on the target
17100 architecture and tuning flags. Annulled delay slots are a
17101 size win, so we only consider the processor-specific tuning
17102 for !optimize_size. */
17103 if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
17104 {
17105 if (ISA_HAS_BRANCHLIKELY
17106 && (optimize_size
17107 || (mips_tune_info->tune_flags & PTF_AVOID_BRANCHLIKELY) == 0))
17108 target_flags |= MASK_BRANCHLIKELY;
17109 else
17110 target_flags &= ~MASK_BRANCHLIKELY;
17111 }
17112 else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
17113 warning (0, "the %qs architecture does not support branch-likely"
17114 " instructions", mips_arch_info->name);
17115
17116 /* If the user hasn't specified -mimadd or -mno-imadd set
17117 MASK_IMADD based on the target architecture and tuning
17118 flags. */
17119 if ((target_flags_explicit & MASK_IMADD) == 0)
17120 {
17121 if (ISA_HAS_MADD_MSUB &&
17122 (mips_tune_info->tune_flags & PTF_AVOID_IMADD) == 0)
17123 target_flags |= MASK_IMADD;
17124 else
17125 target_flags &= ~MASK_IMADD;
17126 }
17127 else if (TARGET_IMADD && !ISA_HAS_MADD_MSUB)
17128 warning (0, "the %qs architecture does not support madd or msub"
17129 " instructions", mips_arch_info->name);
17130
17131 /* The effect of -mabicalls isn't defined for the EABI. */
17132 if (mips_abi == ABI_EABI && TARGET_ABICALLS)
17133 {
17134 error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
17135 target_flags &= ~MASK_ABICALLS;
17136 }
17137
17138 /* PIC requires -mabicalls. */
17139 if (flag_pic)
17140 {
17141 if (mips_abi == ABI_EABI)
17142 error ("cannot generate position-independent code for %qs",
17143 "-mabi=eabi");
17144 else if (!TARGET_ABICALLS)
17145 error ("position-independent code requires %qs", "-mabicalls");
17146 }
17147
17148 if (TARGET_ABICALLS_PIC2)
17149 /* We need to set flag_pic for executables as well as DSOs
17150 because we may reference symbols that are not defined in
17151 the final executable. (MIPS does not use things like
17152 copy relocs, for example.)
17153
17154 There is a body of code that uses __PIC__ to distinguish
17155 between -mabicalls and -mno-abicalls code. The non-__PIC__
17156 variant is usually appropriate for TARGET_ABICALLS_PIC0, as
17157 long as any indirect jumps use $25. */
17158 flag_pic = 1;
17159
17160 /* -mvr4130-align is a "speed over size" optimization: it usually produces
17161 faster code, but at the expense of more nops. Enable it at -O3 and
17162 above. */
17163 if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
17164 target_flags |= MASK_VR4130_ALIGN;
17165
17166 /* Prefer a call to memcpy over inline code when optimizing for size,
17167 though see MOVE_RATIO in mips.h. */
17168 if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
17169 target_flags |= MASK_MEMCPY;
17170
17171 /* If we have a nonzero small-data limit, check that the -mgpopt
17172 setting is consistent with the other target flags. */
17173 if (mips_small_data_threshold > 0)
17174 {
17175 if (!TARGET_GPOPT)
17176 {
17177 if (!TARGET_EXPLICIT_RELOCS)
17178 error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
17179
17180 TARGET_LOCAL_SDATA = false;
17181 TARGET_EXTERN_SDATA = false;
17182 }
17183 else
17184 {
17185 if (TARGET_VXWORKS_RTP)
17186 warning (0, "cannot use small-data accesses for %qs", "-mrtp");
17187
17188 if (TARGET_ABICALLS)
17189 warning (0, "cannot use small-data accesses for %qs",
17190 "-mabicalls");
17191 }
17192 }
17193
17194 /* Pre-IEEE 754-2008 MIPS hardware has a quirky almost-IEEE format
17195 for all its floating point. */
17196 if (mips_nan != MIPS_IEEE_754_2008)
17197 {
17198 REAL_MODE_FORMAT (SFmode) = &mips_single_format;
17199 REAL_MODE_FORMAT (DFmode) = &mips_double_format;
17200 REAL_MODE_FORMAT (TFmode) = &mips_quad_format;
17201 }
17202
17203 /* Make sure that the user didn't turn off paired single support when
17204 MIPS-3D support is requested. */
17205 if (TARGET_MIPS3D
17206 && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
17207 && !TARGET_PAIRED_SINGLE_FLOAT)
17208 error ("%<-mips3d%> requires %<-mpaired-single%>");
17209
17210 /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT. */
17211 if (TARGET_MIPS3D)
17212 target_flags |= MASK_PAIRED_SINGLE_FLOAT;
17213
17214 /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
17215 and TARGET_HARD_FLOAT_ABI are both true. */
17216 if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
17217 {
17218 error ("%qs must be used with %qs",
17219 TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
17220 TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
17221 target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
17222 TARGET_MIPS3D = 0;
17223 }
17224
17225 /* Make sure that -mpaired-single is only used on ISAs that support it.
17226 We must disable it otherwise since it relies on other ISA properties
17227 like ISA_HAS_8CC having their normal values. */
17228 if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
17229 {
17230 error ("the %qs architecture does not support paired-single"
17231 " instructions", mips_arch_info->name);
17232 target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
17233 TARGET_MIPS3D = 0;
17234 }
17235
17236 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
17237 && !TARGET_CACHE_BUILTIN)
17238 {
17239 error ("%qs requires a target that provides the %qs instruction",
17240 "-mr10k-cache-barrier", "cache");
17241 mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
17242 }
17243
17244 /* If TARGET_DSPR2, enable TARGET_DSP. */
17245 if (TARGET_DSPR2)
17246 TARGET_DSP = true;
17247
17248 /* .eh_frame addresses should be the same width as a C pointer.
17249 Most MIPS ABIs support only one pointer size, so the assembler
17250 will usually know exactly how big an .eh_frame address is.
17251
17252 Unfortunately, this is not true of the 64-bit EABI. The ABI was
17253 originally defined to use 64-bit pointers (i.e. it is LP64), and
17254 this is still the default mode. However, we also support an n32-like
17255 ILP32 mode, which is selected by -mlong32. The problem is that the
17256 assembler has traditionally not had an -mlong option, so it has
17257 traditionally not known whether we're using the ILP32 or LP64 form.
17258
17259 As it happens, gas versions up to and including 2.19 use _32-bit_
17260 addresses for EABI64 .cfi_* directives. This is wrong for the
17261 default LP64 mode, so we can't use the directives by default.
17262 Moreover, since gas's current behavior is at odds with gcc's
17263 default behavior, it seems unwise to rely on future versions
17264 of gas behaving the same way. We therefore avoid using .cfi
17265 directives for -mlong32 as well. */
17266 if (mips_abi == ABI_EABI && TARGET_64BIT)
17267 flag_dwarf2_cfi_asm = 0;
17268
17269 /* .cfi_* directives generate a read-only section, so fall back on
17270 manual .eh_frame creation if we need the section to be writable. */
17271 if (TARGET_WRITABLE_EH_FRAME)
17272 flag_dwarf2_cfi_asm = 0;
17273
17274 mips_init_print_operand_punct ();
17275
17276 /* Set up array to map GCC register number to debug register number.
17277 Ignore the special purpose register numbers. */
17278
17279 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17280 {
17281 mips_dbx_regno[i] = IGNORED_DWARF_REGNUM;
17282 if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
17283 mips_dwarf_regno[i] = i;
17284 else
17285 mips_dwarf_regno[i] = INVALID_REGNUM;
17286 }
17287
17288 start = GP_DBX_FIRST - GP_REG_FIRST;
17289 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
17290 mips_dbx_regno[i] = i + start;
17291
17292 start = FP_DBX_FIRST - FP_REG_FIRST;
17293 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
17294 mips_dbx_regno[i] = i + start;
17295
17296 /* Accumulator debug registers use big-endian ordering. */
17297 mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
17298 mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
17299 mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
17300 mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
17301 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
17302 {
17303 mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
17304 mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
17305 }
17306
17307 /* Set up mips_hard_regno_mode_ok. */
17308 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
17309 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
17310 mips_hard_regno_mode_ok[mode][regno]
17311 = mips_hard_regno_mode_ok_p (regno, (enum machine_mode) mode);
17312
17313 /* Function to allocate machine-dependent function status. */
17314 init_machine_status = &mips_init_machine_status;
17315
17316 /* Default to working around R4000 errata only if the processor
17317 was selected explicitly. */
17318 if ((target_flags_explicit & MASK_FIX_R4000) == 0
17319 && strcmp (mips_arch_info->name, "r4000") == 0)
17320 target_flags |= MASK_FIX_R4000;
17321
17322 /* Default to working around R4400 errata only if the processor
17323 was selected explicitly. */
17324 if ((target_flags_explicit & MASK_FIX_R4400) == 0
17325 && strcmp (mips_arch_info->name, "r4400") == 0)
17326 target_flags |= MASK_FIX_R4400;
17327
17328 /* Default to working around R10000 errata only if the processor
17329 was selected explicitly. */
17330 if ((target_flags_explicit & MASK_FIX_R10000) == 0
17331 && strcmp (mips_arch_info->name, "r10000") == 0)
17332 target_flags |= MASK_FIX_R10000;
17333
17334 /* Make sure that branch-likely instructions available when using
17335 -mfix-r10000. The instructions are not available if either:
17336
17337 1. -mno-branch-likely was passed.
17338 2. The selected ISA does not support branch-likely and
17339 the command line does not include -mbranch-likely. */
17340 if (TARGET_FIX_R10000
17341 && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
17342 ? !ISA_HAS_BRANCHLIKELY
17343 : !TARGET_BRANCHLIKELY))
17344 sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
17345
17346 if (TARGET_SYNCI && !ISA_HAS_SYNCI)
17347 {
17348 warning (0, "the %qs architecture does not support the synci "
17349 "instruction", mips_arch_info->name);
17350 target_flags &= ~MASK_SYNCI;
17351 }
17352
17353 /* Only optimize PIC indirect calls if they are actually required. */
17354 if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
17355 target_flags &= ~MASK_RELAX_PIC_CALLS;
17356
17357 /* Save base state of options. */
17358 mips_base_target_flags = target_flags;
17359 mips_base_schedule_insns = flag_schedule_insns;
17360 mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
17361 mips_base_move_loop_invariants = flag_move_loop_invariants;
17362 mips_base_align_loops = align_loops;
17363 mips_base_align_jumps = align_jumps;
17364 mips_base_align_functions = align_functions;
17365
17366 /* Now select the ISA mode.
17367
17368 Do all CPP-sensitive stuff in uncompressed mode; we'll switch modes
17369 later if required. */
17370 mips_set_compression_mode (0);
17371
17372 /* We register a second machine specific reorg pass after delay slot
17373 filling. Registering the pass must be done at start up. It's
17374 convenient to do it here. */
17375 opt_pass *new_pass = make_pass_mips_machine_reorg2 (g);
17376 struct register_pass_info insert_pass_mips_machine_reorg2 =
17377 {
17378 new_pass, /* pass */
17379 "dbr", /* reference_pass_name */
17380 1, /* ref_pass_instance_number */
17381 PASS_POS_INSERT_AFTER /* po_op */
17382 };
17383 register_pass (&insert_pass_mips_machine_reorg2);
17384
17385 if (TARGET_HARD_FLOAT_ABI && TARGET_MIPS5900)
17386 REAL_MODE_FORMAT (SFmode) = &spu_single_format;
17387 }
17388
17389 /* Swap the register information for registers I and I + 1, which
17390 currently have the wrong endianness. Note that the registers'
17391 fixedness and call-clobberedness might have been set on the
17392 command line. */
17393
17394 static void
17395 mips_swap_registers (unsigned int i)
17396 {
17397 int tmpi;
17398 const char *tmps;
17399
17400 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
17401 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
17402
17403 SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
17404 SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
17405 SWAP_INT (call_really_used_regs[i], call_really_used_regs[i + 1]);
17406 SWAP_STRING (reg_names[i], reg_names[i + 1]);
17407
17408 #undef SWAP_STRING
17409 #undef SWAP_INT
17410 }
17411
17412 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
17413
17414 static void
17415 mips_conditional_register_usage (void)
17416 {
17417
17418 if (ISA_HAS_DSP)
17419 {
17420 /* These DSP control register fields are global. */
17421 global_regs[CCDSP_PO_REGNUM] = 1;
17422 global_regs[CCDSP_SC_REGNUM] = 1;
17423 }
17424 else
17425 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17426 reg_class_contents[(int) DSP_ACC_REGS]);
17427
17428 if (!TARGET_HARD_FLOAT)
17429 {
17430 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17431 reg_class_contents[(int) FP_REGS]);
17432 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17433 reg_class_contents[(int) ST_REGS]);
17434 }
17435 else if (!ISA_HAS_8CC)
17436 {
17437 /* We only have a single condition-code register. We implement
17438 this by fixing all the condition-code registers and generating
17439 RTL that refers directly to ST_REG_FIRST. */
17440 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17441 reg_class_contents[(int) ST_REGS]);
17442 SET_HARD_REG_BIT (accessible_reg_set, FPSW_REGNUM);
17443 fixed_regs[FPSW_REGNUM] = call_used_regs[FPSW_REGNUM] = 1;
17444 }
17445 if (TARGET_MIPS16)
17446 {
17447 /* In MIPS16 mode, we prohibit the unused $s registers, since they
17448 are call-saved, and saving them via a MIPS16 register would
17449 probably waste more time than just reloading the value.
17450
17451 We permit the $t temporary registers when optimizing for speed
17452 but not when optimizing for space because using them results in
17453 code that is larger (but faster) then not using them. We do
17454 allow $24 (t8) because it is used in CMP and CMPI instructions
17455 and $25 (t9) because it is used as the function call address in
17456 SVR4 PIC code. */
17457
17458 fixed_regs[18] = call_used_regs[18] = 1;
17459 fixed_regs[19] = call_used_regs[19] = 1;
17460 fixed_regs[20] = call_used_regs[20] = 1;
17461 fixed_regs[21] = call_used_regs[21] = 1;
17462 fixed_regs[22] = call_used_regs[22] = 1;
17463 fixed_regs[23] = call_used_regs[23] = 1;
17464 fixed_regs[26] = call_used_regs[26] = 1;
17465 fixed_regs[27] = call_used_regs[27] = 1;
17466 fixed_regs[30] = call_used_regs[30] = 1;
17467 if (optimize_size)
17468 {
17469 fixed_regs[8] = call_used_regs[8] = 1;
17470 fixed_regs[9] = call_used_regs[9] = 1;
17471 fixed_regs[10] = call_used_regs[10] = 1;
17472 fixed_regs[11] = call_used_regs[11] = 1;
17473 fixed_regs[12] = call_used_regs[12] = 1;
17474 fixed_regs[13] = call_used_regs[13] = 1;
17475 fixed_regs[14] = call_used_regs[14] = 1;
17476 fixed_regs[15] = call_used_regs[15] = 1;
17477 }
17478
17479 /* Do not allow HI and LO to be treated as register operands.
17480 There are no MTHI or MTLO instructions (or any real need
17481 for them) and one-way registers cannot easily be reloaded. */
17482 AND_COMPL_HARD_REG_SET (operand_reg_set,
17483 reg_class_contents[(int) MD_REGS]);
17484 }
17485 /* $f20-$f23 are call-clobbered for n64. */
17486 if (mips_abi == ABI_64)
17487 {
17488 int regno;
17489 for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
17490 call_really_used_regs[regno] = call_used_regs[regno] = 1;
17491 }
17492 /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
17493 for n32. */
17494 if (mips_abi == ABI_N32)
17495 {
17496 int regno;
17497 for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
17498 call_really_used_regs[regno] = call_used_regs[regno] = 1;
17499 }
17500 /* Make sure that double-register accumulator values are correctly
17501 ordered for the current endianness. */
17502 if (TARGET_LITTLE_ENDIAN)
17503 {
17504 unsigned int regno;
17505
17506 mips_swap_registers (MD_REG_FIRST);
17507 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
17508 mips_swap_registers (regno);
17509 }
17510 }
17511
17512 /* When generating MIPS16 code, we want to allocate $24 (T_REG) before
17513 other registers for instructions for which it is possible. This
17514 encourages the compiler to use CMP in cases where an XOR would
17515 require some register shuffling. */
17516
17517 void
17518 mips_order_regs_for_local_alloc (void)
17519 {
17520 int i;
17521
17522 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17523 reg_alloc_order[i] = i;
17524
17525 if (TARGET_MIPS16)
17526 {
17527 /* It really doesn't matter where we put register 0, since it is
17528 a fixed register anyhow. */
17529 reg_alloc_order[0] = 24;
17530 reg_alloc_order[24] = 0;
17531 }
17532 }
17533
17534 /* Implement EH_USES. */
17535
17536 bool
17537 mips_eh_uses (unsigned int regno)
17538 {
17539 if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
17540 {
17541 /* We need to force certain registers to be live in order to handle
17542 PIC long branches correctly. See mips_must_initialize_gp_p for
17543 details. */
17544 if (mips_cfun_has_cprestore_slot_p ())
17545 {
17546 if (regno == CPRESTORE_SLOT_REGNUM)
17547 return true;
17548 }
17549 else
17550 {
17551 if (cfun->machine->global_pointer == regno)
17552 return true;
17553 }
17554 }
17555
17556 return false;
17557 }
17558
17559 /* Implement EPILOGUE_USES. */
17560
17561 bool
17562 mips_epilogue_uses (unsigned int regno)
17563 {
17564 /* Say that the epilogue uses the return address register. Note that
17565 in the case of sibcalls, the values "used by the epilogue" are
17566 considered live at the start of the called function. */
17567 if (regno == RETURN_ADDR_REGNUM)
17568 return true;
17569
17570 /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
17571 See the comment above load_call<mode> for details. */
17572 if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
17573 return true;
17574
17575 /* An interrupt handler must preserve some registers that are
17576 ordinarily call-clobbered. */
17577 if (cfun->machine->interrupt_handler_p
17578 && mips_interrupt_extra_call_saved_reg_p (regno))
17579 return true;
17580
17581 return false;
17582 }
17583
17584 /* A for_each_rtx callback. Stop the search if *X is an AT register. */
17585
17586 static int
17587 mips_at_reg_p (rtx *x, void *data ATTRIBUTE_UNUSED)
17588 {
17589 return REG_P (*x) && REGNO (*x) == AT_REGNUM;
17590 }
17591
17592 /* Return true if INSN needs to be wrapped in ".set noat".
17593 INSN has NOPERANDS operands, stored in OPVEC. */
17594
17595 static bool
17596 mips_need_noat_wrapper_p (rtx insn, rtx *opvec, int noperands)
17597 {
17598 int i;
17599
17600 if (recog_memoized (insn) >= 0)
17601 for (i = 0; i < noperands; i++)
17602 if (for_each_rtx (&opvec[i], mips_at_reg_p, NULL))
17603 return true;
17604 return false;
17605 }
17606
17607 /* Implement FINAL_PRESCAN_INSN. */
17608
17609 void
17610 mips_final_prescan_insn (rtx insn, rtx *opvec, int noperands)
17611 {
17612 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17613 mips_push_asm_switch (&mips_noat);
17614 }
17615
17616 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN. */
17617
17618 static void
17619 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx insn,
17620 rtx *opvec, int noperands)
17621 {
17622 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17623 mips_pop_asm_switch (&mips_noat);
17624 }
17625
17626 /* Return the function that is used to expand the <u>mulsidi3 pattern.
17627 EXT_CODE is the code of the extension used. Return NULL if widening
17628 multiplication shouldn't be used. */
17629
17630 mulsidi3_gen_fn
17631 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
17632 {
17633 bool signed_p;
17634
17635 signed_p = ext_code == SIGN_EXTEND;
17636 if (TARGET_64BIT)
17637 {
17638 /* Don't use widening multiplication with MULT when we have DMUL. Even
17639 with the extension of its input operands DMUL is faster. Note that
17640 the extension is not needed for signed multiplication. In order to
17641 ensure that we always remove the redundant sign-extension in this
17642 case we still expand mulsidi3 for DMUL. */
17643 if (ISA_HAS_DMUL3)
17644 return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
17645 if (TARGET_MIPS16)
17646 return (signed_p
17647 ? gen_mulsidi3_64bit_mips16
17648 : gen_umulsidi3_64bit_mips16);
17649 if (TARGET_FIX_R4000)
17650 return NULL;
17651 return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
17652 }
17653 else
17654 {
17655 if (TARGET_MIPS16)
17656 return (signed_p
17657 ? gen_mulsidi3_32bit_mips16
17658 : gen_umulsidi3_32bit_mips16);
17659 if (TARGET_FIX_R4000 && !ISA_HAS_DSP)
17660 return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
17661 return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
17662 }
17663 }
17664
17665 /* Return true if PATTERN matches the kind of instruction generated by
17666 umips_build_save_restore. SAVE_P is true for store. */
17667
17668 bool
17669 umips_save_restore_pattern_p (bool save_p, rtx pattern)
17670 {
17671 int n;
17672 unsigned int i;
17673 HOST_WIDE_INT first_offset = 0;
17674 rtx first_base = 0;
17675 unsigned int regmask = 0;
17676
17677 for (n = 0; n < XVECLEN (pattern, 0); n++)
17678 {
17679 rtx set, reg, mem, this_base;
17680 HOST_WIDE_INT this_offset;
17681
17682 /* Check that we have a SET. */
17683 set = XVECEXP (pattern, 0, n);
17684 if (GET_CODE (set) != SET)
17685 return false;
17686
17687 /* Check that the SET is a load (if restoring) or a store
17688 (if saving). */
17689 mem = save_p ? SET_DEST (set) : SET_SRC (set);
17690 if (!MEM_P (mem) || MEM_VOLATILE_P (mem))
17691 return false;
17692
17693 /* Check that the address is the sum of base and a possibly-zero
17694 constant offset. Determine if the offset is in range. */
17695 mips_split_plus (XEXP (mem, 0), &this_base, &this_offset);
17696 if (!REG_P (this_base))
17697 return false;
17698
17699 if (n == 0)
17700 {
17701 if (!UMIPS_12BIT_OFFSET_P (this_offset))
17702 return false;
17703 first_base = this_base;
17704 first_offset = this_offset;
17705 }
17706 else
17707 {
17708 /* Check that the save slots are consecutive. */
17709 if (REGNO (this_base) != REGNO (first_base)
17710 || this_offset != first_offset + UNITS_PER_WORD * n)
17711 return false;
17712 }
17713
17714 /* Check that SET's other operand is a register. */
17715 reg = save_p ? SET_SRC (set) : SET_DEST (set);
17716 if (!REG_P (reg))
17717 return false;
17718
17719 regmask |= 1 << REGNO (reg);
17720 }
17721
17722 for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
17723 if (regmask == umips_swm_mask[i])
17724 return true;
17725
17726 return false;
17727 }
17728
17729 /* Return the assembly instruction for microMIPS LWM or SWM.
17730 SAVE_P and PATTERN are as for umips_save_restore_pattern_p. */
17731
17732 const char *
17733 umips_output_save_restore (bool save_p, rtx pattern)
17734 {
17735 static char buffer[300];
17736 char *s;
17737 int n;
17738 HOST_WIDE_INT offset;
17739 rtx base, mem, set, last_set, last_reg;
17740
17741 /* Parse the pattern. */
17742 gcc_assert (umips_save_restore_pattern_p (save_p, pattern));
17743
17744 s = strcpy (buffer, save_p ? "swm\t" : "lwm\t");
17745 s += strlen (s);
17746 n = XVECLEN (pattern, 0);
17747
17748 set = XVECEXP (pattern, 0, 0);
17749 mem = save_p ? SET_DEST (set) : SET_SRC (set);
17750 mips_split_plus (XEXP (mem, 0), &base, &offset);
17751
17752 last_set = XVECEXP (pattern, 0, n - 1);
17753 last_reg = save_p ? SET_SRC (last_set) : SET_DEST (last_set);
17754
17755 if (REGNO (last_reg) == 31)
17756 n--;
17757
17758 gcc_assert (n <= 9);
17759 if (n == 0)
17760 ;
17761 else if (n == 1)
17762 s += sprintf (s, "%s,", reg_names[16]);
17763 else if (n < 9)
17764 s += sprintf (s, "%s-%s,", reg_names[16], reg_names[15 + n]);
17765 else if (n == 9)
17766 s += sprintf (s, "%s-%s,%s,", reg_names[16], reg_names[23],
17767 reg_names[30]);
17768
17769 if (REGNO (last_reg) == 31)
17770 s += sprintf (s, "%s,", reg_names[31]);
17771
17772 s += sprintf (s, "%d(%s)", (int)offset, reg_names[REGNO (base)]);
17773 return buffer;
17774 }
17775
17776 /* Return true if MEM1 and MEM2 use the same base register, and the
17777 offset of MEM2 equals the offset of MEM1 plus 4. FIRST_REG is the
17778 register into (from) which the contents of MEM1 will be loaded
17779 (stored), depending on the value of LOAD_P.
17780 SWAP_P is true when the 1st and 2nd instructions are swapped. */
17781
17782 static bool
17783 umips_load_store_pair_p_1 (bool load_p, bool swap_p,
17784 rtx first_reg, rtx mem1, rtx mem2)
17785 {
17786 rtx base1, base2;
17787 HOST_WIDE_INT offset1, offset2;
17788
17789 if (!MEM_P (mem1) || !MEM_P (mem2))
17790 return false;
17791
17792 mips_split_plus (XEXP (mem1, 0), &base1, &offset1);
17793 mips_split_plus (XEXP (mem2, 0), &base2, &offset2);
17794
17795 if (!REG_P (base1) || !rtx_equal_p (base1, base2))
17796 return false;
17797
17798 /* Avoid invalid load pair instructions. */
17799 if (load_p && REGNO (first_reg) == REGNO (base1))
17800 return false;
17801
17802 /* We must avoid this case for anti-dependence.
17803 Ex: lw $3, 4($3)
17804 lw $2, 0($3)
17805 first_reg is $2, but the base is $3. */
17806 if (load_p
17807 && swap_p
17808 && REGNO (first_reg) + 1 == REGNO (base1))
17809 return false;
17810
17811 if (offset2 != offset1 + 4)
17812 return false;
17813
17814 if (!UMIPS_12BIT_OFFSET_P (offset1))
17815 return false;
17816
17817 return true;
17818 }
17819
17820 /* OPERANDS describes the operands to a pair of SETs, in the order
17821 dest1, src1, dest2, src2. Return true if the operands can be used
17822 in an LWP or SWP instruction; LOAD_P says which. */
17823
17824 bool
17825 umips_load_store_pair_p (bool load_p, rtx *operands)
17826 {
17827 rtx reg1, reg2, mem1, mem2;
17828
17829 if (load_p)
17830 {
17831 reg1 = operands[0];
17832 reg2 = operands[2];
17833 mem1 = operands[1];
17834 mem2 = operands[3];
17835 }
17836 else
17837 {
17838 reg1 = operands[1];
17839 reg2 = operands[3];
17840 mem1 = operands[0];
17841 mem2 = operands[2];
17842 }
17843
17844 if (REGNO (reg2) == REGNO (reg1) + 1)
17845 return umips_load_store_pair_p_1 (load_p, false, reg1, mem1, mem2);
17846
17847 if (REGNO (reg1) == REGNO (reg2) + 1)
17848 return umips_load_store_pair_p_1 (load_p, true, reg2, mem2, mem1);
17849
17850 return false;
17851 }
17852
17853 /* Return the assembly instruction for a microMIPS LWP or SWP in which
17854 the first register is REG and the first memory slot is MEM.
17855 LOAD_P is true for LWP. */
17856
17857 static void
17858 umips_output_load_store_pair_1 (bool load_p, rtx reg, rtx mem)
17859 {
17860 rtx ops[] = {reg, mem};
17861
17862 if (load_p)
17863 output_asm_insn ("lwp\t%0,%1", ops);
17864 else
17865 output_asm_insn ("swp\t%0,%1", ops);
17866 }
17867
17868 /* Output the assembly instruction for a microMIPS LWP or SWP instruction.
17869 LOAD_P and OPERANDS are as for umips_load_store_pair_p. */
17870
17871 void
17872 umips_output_load_store_pair (bool load_p, rtx *operands)
17873 {
17874 rtx reg1, reg2, mem1, mem2;
17875 if (load_p)
17876 {
17877 reg1 = operands[0];
17878 reg2 = operands[2];
17879 mem1 = operands[1];
17880 mem2 = operands[3];
17881 }
17882 else
17883 {
17884 reg1 = operands[1];
17885 reg2 = operands[3];
17886 mem1 = operands[0];
17887 mem2 = operands[2];
17888 }
17889
17890 if (REGNO (reg2) == REGNO (reg1) + 1)
17891 {
17892 umips_output_load_store_pair_1 (load_p, reg1, mem1);
17893 return;
17894 }
17895
17896 gcc_assert (REGNO (reg1) == REGNO (reg2) + 1);
17897 umips_output_load_store_pair_1 (load_p, reg2, mem2);
17898 }
17899
17900 /* Return true if REG1 and REG2 match the criteria for a movep insn. */
17901
17902 bool
17903 umips_movep_target_p (rtx reg1, rtx reg2)
17904 {
17905 int regno1, regno2, pair;
17906 unsigned int i;
17907 static const int match[8] = {
17908 0x00000060, /* 5, 6 */
17909 0x000000a0, /* 5, 7 */
17910 0x000000c0, /* 6, 7 */
17911 0x00200010, /* 4, 21 */
17912 0x00400010, /* 4, 22 */
17913 0x00000030, /* 4, 5 */
17914 0x00000050, /* 4, 6 */
17915 0x00000090 /* 4, 7 */
17916 };
17917
17918 if (!REG_P (reg1) || !REG_P (reg2))
17919 return false;
17920
17921 regno1 = REGNO (reg1);
17922 regno2 = REGNO (reg2);
17923
17924 if (!GP_REG_P (regno1) || !GP_REG_P (regno2))
17925 return false;
17926
17927 pair = (1 << regno1) | (1 << regno2);
17928
17929 for (i = 0; i < ARRAY_SIZE (match); i++)
17930 if (pair == match[i])
17931 return true;
17932
17933 return false;
17934 }
17935 \f
17936 /* Return the size in bytes of the trampoline code, padded to
17937 TRAMPOLINE_ALIGNMENT bits. The static chain pointer and target
17938 function address immediately follow. */
17939
17940 int
17941 mips_trampoline_code_size (void)
17942 {
17943 if (TARGET_USE_PIC_FN_ADDR_REG)
17944 return 4 * 4;
17945 else if (ptr_mode == DImode)
17946 return 8 * 4;
17947 else if (ISA_HAS_LOAD_DELAY)
17948 return 6 * 4;
17949 else
17950 return 4 * 4;
17951 }
17952
17953 /* Implement TARGET_TRAMPOLINE_INIT. */
17954
17955 static void
17956 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
17957 {
17958 rtx addr, end_addr, high, low, opcode, mem;
17959 rtx trampoline[8];
17960 unsigned int i, j;
17961 HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
17962
17963 /* Work out the offsets of the pointers from the start of the
17964 trampoline code. */
17965 end_addr_offset = mips_trampoline_code_size ();
17966 static_chain_offset = end_addr_offset;
17967 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
17968
17969 /* Get pointers to the beginning and end of the code block. */
17970 addr = force_reg (Pmode, XEXP (m_tramp, 0));
17971 end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
17972
17973 #define OP(X) gen_int_mode (X, SImode)
17974
17975 /* Build up the code in TRAMPOLINE. */
17976 i = 0;
17977 if (TARGET_USE_PIC_FN_ADDR_REG)
17978 {
17979 /* $25 contains the address of the trampoline. Emit code of the form:
17980
17981 l[wd] $1, target_function_offset($25)
17982 l[wd] $static_chain, static_chain_offset($25)
17983 jr $1
17984 move $25,$1. */
17985 trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
17986 target_function_offset,
17987 PIC_FUNCTION_ADDR_REGNUM));
17988 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17989 static_chain_offset,
17990 PIC_FUNCTION_ADDR_REGNUM));
17991 trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
17992 trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
17993 }
17994 else if (ptr_mode == DImode)
17995 {
17996 /* It's too cumbersome to create the full 64-bit address, so let's
17997 instead use:
17998
17999 move $1, $31
18000 bal 1f
18001 nop
18002 1: l[wd] $25, target_function_offset - 12($31)
18003 l[wd] $static_chain, static_chain_offset - 12($31)
18004 jr $25
18005 move $31, $1
18006
18007 where 12 is the offset of "1:" from the start of the code block. */
18008 trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
18009 trampoline[i++] = OP (MIPS_BAL (1));
18010 trampoline[i++] = OP (MIPS_NOP);
18011 trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
18012 target_function_offset - 12,
18013 RETURN_ADDR_REGNUM));
18014 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
18015 static_chain_offset - 12,
18016 RETURN_ADDR_REGNUM));
18017 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
18018 trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
18019 }
18020 else
18021 {
18022 /* If the target has load delays, emit:
18023
18024 lui $1, %hi(end_addr)
18025 lw $25, %lo(end_addr + ...)($1)
18026 lw $static_chain, %lo(end_addr + ...)($1)
18027 jr $25
18028 nop
18029
18030 Otherwise emit:
18031
18032 lui $1, %hi(end_addr)
18033 lw $25, %lo(end_addr + ...)($1)
18034 jr $25
18035 lw $static_chain, %lo(end_addr + ...)($1). */
18036
18037 /* Split END_ADDR into %hi and %lo values. Trampolines are aligned
18038 to 64 bits, so the %lo value will have the bottom 3 bits clear. */
18039 high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
18040 NULL, false, OPTAB_WIDEN);
18041 high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
18042 NULL, false, OPTAB_WIDEN);
18043 low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
18044
18045 /* Emit the LUI. */
18046 opcode = OP (MIPS_LUI (AT_REGNUM, 0));
18047 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
18048 NULL, false, OPTAB_WIDEN);
18049
18050 /* Emit the load of the target function. */
18051 opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
18052 target_function_offset - end_addr_offset,
18053 AT_REGNUM));
18054 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
18055 NULL, false, OPTAB_WIDEN);
18056
18057 /* Emit the JR here, if we can. */
18058 if (!ISA_HAS_LOAD_DELAY)
18059 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
18060
18061 /* Emit the load of the static chain register. */
18062 opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
18063 static_chain_offset - end_addr_offset,
18064 AT_REGNUM));
18065 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
18066 NULL, false, OPTAB_WIDEN);
18067
18068 /* Emit the JR, if we couldn't above. */
18069 if (ISA_HAS_LOAD_DELAY)
18070 {
18071 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
18072 trampoline[i++] = OP (MIPS_NOP);
18073 }
18074 }
18075
18076 #undef OP
18077
18078 /* Copy the trampoline code. Leave any padding uninitialized. */
18079 for (j = 0; j < i; j++)
18080 {
18081 mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
18082 mips_emit_move (mem, trampoline[j]);
18083 }
18084
18085 /* Set up the static chain pointer field. */
18086 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
18087 mips_emit_move (mem, chain_value);
18088
18089 /* Set up the target function field. */
18090 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
18091 mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
18092
18093 /* Flush the code part of the trampoline. */
18094 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
18095 emit_insn (gen_clear_cache (addr, end_addr));
18096 }
18097
18098 /* Implement FUNCTION_PROFILER. */
18099
18100 void mips_function_profiler (FILE *file)
18101 {
18102 if (TARGET_MIPS16)
18103 sorry ("mips16 function profiling");
18104 if (TARGET_LONG_CALLS)
18105 {
18106 /* For TARGET_LONG_CALLS use $3 for the address of _mcount. */
18107 if (Pmode == DImode)
18108 fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
18109 else
18110 fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
18111 }
18112 mips_push_asm_switch (&mips_noat);
18113 fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
18114 reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
18115 /* _mcount treats $2 as the static chain register. */
18116 if (cfun->static_chain_decl != NULL)
18117 fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
18118 reg_names[STATIC_CHAIN_REGNUM]);
18119 if (TARGET_MCOUNT_RA_ADDRESS)
18120 {
18121 /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
18122 ra save location. */
18123 if (cfun->machine->frame.ra_fp_offset == 0)
18124 /* ra not saved, pass zero. */
18125 fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
18126 else
18127 fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
18128 Pmode == DImode ? "dla" : "la", reg_names[12],
18129 cfun->machine->frame.ra_fp_offset,
18130 reg_names[STACK_POINTER_REGNUM]);
18131 }
18132 if (!TARGET_NEWABI)
18133 fprintf (file,
18134 "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from stack\n",
18135 TARGET_64BIT ? "dsubu" : "subu",
18136 reg_names[STACK_POINTER_REGNUM],
18137 reg_names[STACK_POINTER_REGNUM],
18138 Pmode == DImode ? 16 : 8);
18139
18140 if (TARGET_LONG_CALLS)
18141 fprintf (file, "\tjalr\t%s\n", reg_names[3]);
18142 else
18143 fprintf (file, "\tjal\t_mcount\n");
18144 mips_pop_asm_switch (&mips_noat);
18145 /* _mcount treats $2 as the static chain register. */
18146 if (cfun->static_chain_decl != NULL)
18147 fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
18148 reg_names[2]);
18149 }
18150
18151 /* Implement TARGET_SHIFT_TRUNCATION_MASK. We want to keep the default
18152 behaviour of TARGET_SHIFT_TRUNCATION_MASK for non-vector modes even
18153 when TARGET_LOONGSON_VECTORS is true. */
18154
18155 static unsigned HOST_WIDE_INT
18156 mips_shift_truncation_mask (enum machine_mode mode)
18157 {
18158 if (TARGET_LOONGSON_VECTORS && VECTOR_MODE_P (mode))
18159 return 0;
18160
18161 return GET_MODE_BITSIZE (mode) - 1;
18162 }
18163
18164 /* Implement TARGET_PREPARE_PCH_SAVE. */
18165
18166 static void
18167 mips_prepare_pch_save (void)
18168 {
18169 /* We are called in a context where the current MIPS16 vs. non-MIPS16
18170 setting should be irrelevant. The question then is: which setting
18171 makes most sense at load time?
18172
18173 The PCH is loaded before the first token is read. We should never
18174 have switched into MIPS16 mode by that point, and thus should not
18175 have populated mips16_globals. Nor can we load the entire contents
18176 of mips16_globals from the PCH file, because mips16_globals contains
18177 a combination of GGC and non-GGC data.
18178
18179 There is therefore no point in trying save the GGC part of
18180 mips16_globals to the PCH file, or to preserve MIPS16ness across
18181 the PCH save and load. The loading compiler would not have access
18182 to the non-GGC parts of mips16_globals (either from the PCH file,
18183 or from a copy that the loading compiler generated itself) and would
18184 have to call target_reinit anyway.
18185
18186 It therefore seems best to switch back to non-MIPS16 mode at
18187 save time, and to ensure that mips16_globals remains null after
18188 a PCH load. */
18189 mips_set_compression_mode (0);
18190 mips16_globals = 0;
18191 }
18192 \f
18193 /* Generate or test for an insn that supports a constant permutation. */
18194
18195 #define MAX_VECT_LEN 8
18196
18197 struct expand_vec_perm_d
18198 {
18199 rtx target, op0, op1;
18200 unsigned char perm[MAX_VECT_LEN];
18201 enum machine_mode vmode;
18202 unsigned char nelt;
18203 bool one_vector_p;
18204 bool testing_p;
18205 };
18206
18207 /* Construct (set target (vec_select op0 (parallel perm))) and
18208 return true if that's a valid instruction in the active ISA. */
18209
18210 static bool
18211 mips_expand_vselect (rtx target, rtx op0,
18212 const unsigned char *perm, unsigned nelt)
18213 {
18214 rtx rperm[MAX_VECT_LEN], x;
18215 unsigned i;
18216
18217 for (i = 0; i < nelt; ++i)
18218 rperm[i] = GEN_INT (perm[i]);
18219
18220 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
18221 x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
18222 x = gen_rtx_SET (VOIDmode, target, x);
18223
18224 x = emit_insn (x);
18225 if (recog_memoized (x) < 0)
18226 {
18227 remove_insn (x);
18228 return false;
18229 }
18230 return true;
18231 }
18232
18233 /* Similar, but generate a vec_concat from op0 and op1 as well. */
18234
18235 static bool
18236 mips_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
18237 const unsigned char *perm, unsigned nelt)
18238 {
18239 enum machine_mode v2mode;
18240 rtx x;
18241
18242 v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
18243 x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
18244 return mips_expand_vselect (target, x, perm, nelt);
18245 }
18246
18247 /* Recognize patterns for even-odd extraction. */
18248
18249 static bool
18250 mips_expand_vpc_loongson_even_odd (struct expand_vec_perm_d *d)
18251 {
18252 unsigned i, odd, nelt = d->nelt;
18253 rtx t0, t1, t2, t3;
18254
18255 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18256 return false;
18257 /* Even-odd for V2SI/V2SFmode is matched by interleave directly. */
18258 if (nelt < 4)
18259 return false;
18260
18261 odd = d->perm[0];
18262 if (odd > 1)
18263 return false;
18264 for (i = 1; i < nelt; ++i)
18265 if (d->perm[i] != i * 2 + odd)
18266 return false;
18267
18268 if (d->testing_p)
18269 return true;
18270
18271 /* We need 2*log2(N)-1 operations to achieve odd/even with interleave. */
18272 t0 = gen_reg_rtx (d->vmode);
18273 t1 = gen_reg_rtx (d->vmode);
18274 switch (d->vmode)
18275 {
18276 case V4HImode:
18277 emit_insn (gen_loongson_punpckhhw (t0, d->op0, d->op1));
18278 emit_insn (gen_loongson_punpcklhw (t1, d->op0, d->op1));
18279 if (odd)
18280 emit_insn (gen_loongson_punpckhhw (d->target, t1, t0));
18281 else
18282 emit_insn (gen_loongson_punpcklhw (d->target, t1, t0));
18283 break;
18284
18285 case V8QImode:
18286 t2 = gen_reg_rtx (d->vmode);
18287 t3 = gen_reg_rtx (d->vmode);
18288 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op1));
18289 emit_insn (gen_loongson_punpcklbh (t1, d->op0, d->op1));
18290 emit_insn (gen_loongson_punpckhbh (t2, t1, t0));
18291 emit_insn (gen_loongson_punpcklbh (t3, t1, t0));
18292 if (odd)
18293 emit_insn (gen_loongson_punpckhbh (d->target, t3, t2));
18294 else
18295 emit_insn (gen_loongson_punpcklbh (d->target, t3, t2));
18296 break;
18297
18298 default:
18299 gcc_unreachable ();
18300 }
18301 return true;
18302 }
18303
18304 /* Recognize patterns for the Loongson PSHUFH instruction. */
18305
18306 static bool
18307 mips_expand_vpc_loongson_pshufh (struct expand_vec_perm_d *d)
18308 {
18309 unsigned i, mask;
18310 rtx rmask;
18311
18312 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18313 return false;
18314 if (d->vmode != V4HImode)
18315 return false;
18316 if (d->testing_p)
18317 return true;
18318
18319 /* Convert the selector into the packed 8-bit form for pshufh. */
18320 /* Recall that loongson is little-endian only. No big-endian
18321 adjustment required. */
18322 for (i = mask = 0; i < 4; i++)
18323 mask |= (d->perm[i] & 3) << (i * 2);
18324 rmask = force_reg (SImode, GEN_INT (mask));
18325
18326 if (d->one_vector_p)
18327 emit_insn (gen_loongson_pshufh (d->target, d->op0, rmask));
18328 else
18329 {
18330 rtx t0, t1, x, merge, rmerge[4];
18331
18332 t0 = gen_reg_rtx (V4HImode);
18333 t1 = gen_reg_rtx (V4HImode);
18334 emit_insn (gen_loongson_pshufh (t1, d->op1, rmask));
18335 emit_insn (gen_loongson_pshufh (t0, d->op0, rmask));
18336
18337 for (i = 0; i < 4; ++i)
18338 rmerge[i] = (d->perm[i] & 4 ? constm1_rtx : const0_rtx);
18339 merge = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmerge));
18340 merge = force_reg (V4HImode, merge);
18341
18342 x = gen_rtx_AND (V4HImode, merge, t1);
18343 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18344
18345 x = gen_rtx_NOT (V4HImode, merge);
18346 x = gen_rtx_AND (V4HImode, x, t0);
18347 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18348
18349 x = gen_rtx_IOR (V4HImode, t0, t1);
18350 emit_insn (gen_rtx_SET (VOIDmode, d->target, x));
18351 }
18352
18353 return true;
18354 }
18355
18356 /* Recognize broadcast patterns for the Loongson. */
18357
18358 static bool
18359 mips_expand_vpc_loongson_bcast (struct expand_vec_perm_d *d)
18360 {
18361 unsigned i, elt;
18362 rtx t0, t1;
18363
18364 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18365 return false;
18366 /* Note that we've already matched V2SI via punpck and V4HI via pshufh. */
18367 if (d->vmode != V8QImode)
18368 return false;
18369 if (!d->one_vector_p)
18370 return false;
18371
18372 elt = d->perm[0];
18373 for (i = 1; i < 8; ++i)
18374 if (d->perm[i] != elt)
18375 return false;
18376
18377 if (d->testing_p)
18378 return true;
18379
18380 /* With one interleave we put two of the desired element adjacent. */
18381 t0 = gen_reg_rtx (V8QImode);
18382 if (elt < 4)
18383 emit_insn (gen_loongson_punpcklbh (t0, d->op0, d->op0));
18384 else
18385 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op0));
18386
18387 /* Shuffle that one HImode element into all locations. */
18388 elt &= 3;
18389 elt *= 0x55;
18390 t1 = gen_reg_rtx (V4HImode);
18391 emit_insn (gen_loongson_pshufh (t1, gen_lowpart (V4HImode, t0),
18392 force_reg (SImode, GEN_INT (elt))));
18393
18394 emit_move_insn (d->target, gen_lowpart (V8QImode, t1));
18395 return true;
18396 }
18397
18398 static bool
18399 mips_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
18400 {
18401 unsigned int i, nelt = d->nelt;
18402 unsigned char perm2[MAX_VECT_LEN];
18403
18404 if (d->one_vector_p)
18405 {
18406 /* Try interleave with alternating operands. */
18407 memcpy (perm2, d->perm, sizeof(perm2));
18408 for (i = 1; i < nelt; i += 2)
18409 perm2[i] += nelt;
18410 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1, perm2, nelt))
18411 return true;
18412 }
18413 else
18414 {
18415 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1,
18416 d->perm, nelt))
18417 return true;
18418
18419 /* Try again with swapped operands. */
18420 for (i = 0; i < nelt; ++i)
18421 perm2[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
18422 if (mips_expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
18423 return true;
18424 }
18425
18426 if (mips_expand_vpc_loongson_even_odd (d))
18427 return true;
18428 if (mips_expand_vpc_loongson_pshufh (d))
18429 return true;
18430 if (mips_expand_vpc_loongson_bcast (d))
18431 return true;
18432 return false;
18433 }
18434
18435 /* Expand a vec_perm_const pattern. */
18436
18437 bool
18438 mips_expand_vec_perm_const (rtx operands[4])
18439 {
18440 struct expand_vec_perm_d d;
18441 int i, nelt, which;
18442 unsigned char orig_perm[MAX_VECT_LEN];
18443 rtx sel;
18444 bool ok;
18445
18446 d.target = operands[0];
18447 d.op0 = operands[1];
18448 d.op1 = operands[2];
18449 sel = operands[3];
18450
18451 d.vmode = GET_MODE (d.target);
18452 gcc_assert (VECTOR_MODE_P (d.vmode));
18453 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18454 d.testing_p = false;
18455
18456 for (i = which = 0; i < nelt; ++i)
18457 {
18458 rtx e = XVECEXP (sel, 0, i);
18459 int ei = INTVAL (e) & (2 * nelt - 1);
18460 which |= (ei < nelt ? 1 : 2);
18461 orig_perm[i] = ei;
18462 }
18463 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18464
18465 switch (which)
18466 {
18467 default:
18468 gcc_unreachable();
18469
18470 case 3:
18471 d.one_vector_p = false;
18472 if (!rtx_equal_p (d.op0, d.op1))
18473 break;
18474 /* FALLTHRU */
18475
18476 case 2:
18477 for (i = 0; i < nelt; ++i)
18478 d.perm[i] &= nelt - 1;
18479 d.op0 = d.op1;
18480 d.one_vector_p = true;
18481 break;
18482
18483 case 1:
18484 d.op1 = d.op0;
18485 d.one_vector_p = true;
18486 break;
18487 }
18488
18489 ok = mips_expand_vec_perm_const_1 (&d);
18490
18491 /* If we were given a two-vector permutation which just happened to
18492 have both input vectors equal, we folded this into a one-vector
18493 permutation. There are several loongson patterns that are matched
18494 via direct vec_select+vec_concat expansion, but we do not have
18495 support in mips_expand_vec_perm_const_1 to guess the adjustment
18496 that should be made for a single operand. Just try again with
18497 the original permutation. */
18498 if (!ok && which == 3)
18499 {
18500 d.op0 = operands[1];
18501 d.op1 = operands[2];
18502 d.one_vector_p = false;
18503 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18504 ok = mips_expand_vec_perm_const_1 (&d);
18505 }
18506
18507 return ok;
18508 }
18509
18510 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST_OK. */
18511
18512 static bool
18513 mips_vectorize_vec_perm_const_ok (enum machine_mode vmode,
18514 const unsigned char *sel)
18515 {
18516 struct expand_vec_perm_d d;
18517 unsigned int i, nelt, which;
18518 bool ret;
18519
18520 d.vmode = vmode;
18521 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18522 d.testing_p = true;
18523 memcpy (d.perm, sel, nelt);
18524
18525 /* Categorize the set of elements in the selector. */
18526 for (i = which = 0; i < nelt; ++i)
18527 {
18528 unsigned char e = d.perm[i];
18529 gcc_assert (e < 2 * nelt);
18530 which |= (e < nelt ? 1 : 2);
18531 }
18532
18533 /* For all elements from second vector, fold the elements to first. */
18534 if (which == 2)
18535 for (i = 0; i < nelt; ++i)
18536 d.perm[i] -= nelt;
18537
18538 /* Check whether the mask can be applied to the vector type. */
18539 d.one_vector_p = (which != 3);
18540
18541 d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
18542 d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
18543 if (!d.one_vector_p)
18544 d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
18545
18546 start_sequence ();
18547 ret = mips_expand_vec_perm_const_1 (&d);
18548 end_sequence ();
18549
18550 return ret;
18551 }
18552
18553 /* Expand an integral vector unpack operation. */
18554
18555 void
18556 mips_expand_vec_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18557 {
18558 enum machine_mode imode = GET_MODE (operands[1]);
18559 rtx (*unpack) (rtx, rtx, rtx);
18560 rtx (*cmpgt) (rtx, rtx, rtx);
18561 rtx tmp, dest, zero;
18562
18563 switch (imode)
18564 {
18565 case V8QImode:
18566 if (high_p)
18567 unpack = gen_loongson_punpckhbh;
18568 else
18569 unpack = gen_loongson_punpcklbh;
18570 cmpgt = gen_loongson_pcmpgtb;
18571 break;
18572 case V4HImode:
18573 if (high_p)
18574 unpack = gen_loongson_punpckhhw;
18575 else
18576 unpack = gen_loongson_punpcklhw;
18577 cmpgt = gen_loongson_pcmpgth;
18578 break;
18579 default:
18580 gcc_unreachable ();
18581 }
18582
18583 zero = force_reg (imode, CONST0_RTX (imode));
18584 if (unsigned_p)
18585 tmp = zero;
18586 else
18587 {
18588 tmp = gen_reg_rtx (imode);
18589 emit_insn (cmpgt (tmp, zero, operands[1]));
18590 }
18591
18592 dest = gen_reg_rtx (imode);
18593 emit_insn (unpack (dest, operands[1], tmp));
18594
18595 emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
18596 }
18597
18598 /* A subroutine of mips_expand_vec_init, match constant vector elements. */
18599
18600 static inline bool
18601 mips_constant_elt_p (rtx x)
18602 {
18603 return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
18604 }
18605
18606 /* A subroutine of mips_expand_vec_init, expand via broadcast. */
18607
18608 static void
18609 mips_expand_vi_broadcast (enum machine_mode vmode, rtx target, rtx elt)
18610 {
18611 struct expand_vec_perm_d d;
18612 rtx t1;
18613 bool ok;
18614
18615 if (elt != const0_rtx)
18616 elt = force_reg (GET_MODE_INNER (vmode), elt);
18617 if (REG_P (elt))
18618 elt = gen_lowpart (DImode, elt);
18619
18620 t1 = gen_reg_rtx (vmode);
18621 switch (vmode)
18622 {
18623 case V8QImode:
18624 emit_insn (gen_loongson_vec_init1_v8qi (t1, elt));
18625 break;
18626 case V4HImode:
18627 emit_insn (gen_loongson_vec_init1_v4hi (t1, elt));
18628 break;
18629 default:
18630 gcc_unreachable ();
18631 }
18632
18633 memset (&d, 0, sizeof (d));
18634 d.target = target;
18635 d.op0 = t1;
18636 d.op1 = t1;
18637 d.vmode = vmode;
18638 d.nelt = GET_MODE_NUNITS (vmode);
18639 d.one_vector_p = true;
18640
18641 ok = mips_expand_vec_perm_const_1 (&d);
18642 gcc_assert (ok);
18643 }
18644
18645 /* A subroutine of mips_expand_vec_init, replacing all of the non-constant
18646 elements of VALS with zeros, copy the constant vector to TARGET. */
18647
18648 static void
18649 mips_expand_vi_constant (enum machine_mode vmode, unsigned nelt,
18650 rtx target, rtx vals)
18651 {
18652 rtvec vec = shallow_copy_rtvec (XVEC (vals, 0));
18653 unsigned i;
18654
18655 for (i = 0; i < nelt; ++i)
18656 {
18657 if (!mips_constant_elt_p (RTVEC_ELT (vec, i)))
18658 RTVEC_ELT (vec, i) = const0_rtx;
18659 }
18660
18661 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, vec));
18662 }
18663
18664
18665 /* A subroutine of mips_expand_vec_init, expand via pinsrh. */
18666
18667 static void
18668 mips_expand_vi_loongson_one_pinsrh (rtx target, rtx vals, unsigned one_var)
18669 {
18670 mips_expand_vi_constant (V4HImode, 4, target, vals);
18671
18672 emit_insn (gen_vec_setv4hi (target, target, XVECEXP (vals, 0, one_var),
18673 GEN_INT (one_var)));
18674 }
18675
18676 /* A subroutine of mips_expand_vec_init, expand anything via memory. */
18677
18678 static void
18679 mips_expand_vi_general (enum machine_mode vmode, enum machine_mode imode,
18680 unsigned nelt, unsigned nvar, rtx target, rtx vals)
18681 {
18682 rtx mem = assign_stack_temp (vmode, GET_MODE_SIZE (vmode));
18683 unsigned int i, isize = GET_MODE_SIZE (imode);
18684
18685 if (nvar < nelt)
18686 mips_expand_vi_constant (vmode, nelt, mem, vals);
18687
18688 for (i = 0; i < nelt; ++i)
18689 {
18690 rtx x = XVECEXP (vals, 0, i);
18691 if (!mips_constant_elt_p (x))
18692 emit_move_insn (adjust_address (mem, imode, i * isize), x);
18693 }
18694
18695 emit_move_insn (target, mem);
18696 }
18697
18698 /* Expand a vector initialization. */
18699
18700 void
18701 mips_expand_vector_init (rtx target, rtx vals)
18702 {
18703 enum machine_mode vmode = GET_MODE (target);
18704 enum machine_mode imode = GET_MODE_INNER (vmode);
18705 unsigned i, nelt = GET_MODE_NUNITS (vmode);
18706 unsigned nvar = 0, one_var = -1u;
18707 bool all_same = true;
18708 rtx x;
18709
18710 for (i = 0; i < nelt; ++i)
18711 {
18712 x = XVECEXP (vals, 0, i);
18713 if (!mips_constant_elt_p (x))
18714 nvar++, one_var = i;
18715 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
18716 all_same = false;
18717 }
18718
18719 /* Load constants from the pool, or whatever's handy. */
18720 if (nvar == 0)
18721 {
18722 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
18723 return;
18724 }
18725
18726 /* For two-part initialization, always use CONCAT. */
18727 if (nelt == 2)
18728 {
18729 rtx op0 = force_reg (imode, XVECEXP (vals, 0, 0));
18730 rtx op1 = force_reg (imode, XVECEXP (vals, 0, 1));
18731 x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
18732 emit_insn (gen_rtx_SET (VOIDmode, target, x));
18733 return;
18734 }
18735
18736 /* Loongson is the only cpu with vectors with more elements. */
18737 gcc_assert (TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS);
18738
18739 /* If all values are identical, broadcast the value. */
18740 if (all_same)
18741 {
18742 mips_expand_vi_broadcast (vmode, target, XVECEXP (vals, 0, 0));
18743 return;
18744 }
18745
18746 /* If we've only got one non-variable V4HImode, use PINSRH. */
18747 if (nvar == 1 && vmode == V4HImode)
18748 {
18749 mips_expand_vi_loongson_one_pinsrh (target, vals, one_var);
18750 return;
18751 }
18752
18753 mips_expand_vi_general (vmode, imode, nelt, nvar, target, vals);
18754 }
18755
18756 /* Expand a vector reduction. */
18757
18758 void
18759 mips_expand_vec_reduc (rtx target, rtx in, rtx (*gen)(rtx, rtx, rtx))
18760 {
18761 enum machine_mode vmode = GET_MODE (in);
18762 unsigned char perm2[2];
18763 rtx last, next, fold, x;
18764 bool ok;
18765
18766 last = in;
18767 fold = gen_reg_rtx (vmode);
18768 switch (vmode)
18769 {
18770 case V2SFmode:
18771 /* Use PUL/PLU to produce { L, H } op { H, L }.
18772 By reversing the pair order, rather than a pure interleave high,
18773 we avoid erroneous exceptional conditions that we might otherwise
18774 produce from the computation of H op H. */
18775 perm2[0] = 1;
18776 perm2[1] = 2;
18777 ok = mips_expand_vselect_vconcat (fold, last, last, perm2, 2);
18778 gcc_assert (ok);
18779 break;
18780
18781 case V2SImode:
18782 /* Use interleave to produce { H, L } op { H, H }. */
18783 emit_insn (gen_loongson_punpckhwd (fold, last, last));
18784 break;
18785
18786 case V4HImode:
18787 /* Perform the first reduction with interleave,
18788 and subsequent reductions with shifts. */
18789 emit_insn (gen_loongson_punpckhwd_hi (fold, last, last));
18790
18791 next = gen_reg_rtx (vmode);
18792 emit_insn (gen (next, last, fold));
18793 last = next;
18794
18795 fold = gen_reg_rtx (vmode);
18796 x = force_reg (SImode, GEN_INT (16));
18797 emit_insn (gen_vec_shr_v4hi (fold, last, x));
18798 break;
18799
18800 case V8QImode:
18801 emit_insn (gen_loongson_punpckhwd_qi (fold, last, last));
18802
18803 next = gen_reg_rtx (vmode);
18804 emit_insn (gen (next, last, fold));
18805 last = next;
18806
18807 fold = gen_reg_rtx (vmode);
18808 x = force_reg (SImode, GEN_INT (16));
18809 emit_insn (gen_vec_shr_v8qi (fold, last, x));
18810
18811 next = gen_reg_rtx (vmode);
18812 emit_insn (gen (next, last, fold));
18813 last = next;
18814
18815 fold = gen_reg_rtx (vmode);
18816 x = force_reg (SImode, GEN_INT (8));
18817 emit_insn (gen_vec_shr_v8qi (fold, last, x));
18818 break;
18819
18820 default:
18821 gcc_unreachable ();
18822 }
18823
18824 emit_insn (gen (target, last, fold));
18825 }
18826
18827 /* Expand a vector minimum/maximum. */
18828
18829 void
18830 mips_expand_vec_minmax (rtx target, rtx op0, rtx op1,
18831 rtx (*cmp) (rtx, rtx, rtx), bool min_p)
18832 {
18833 enum machine_mode vmode = GET_MODE (target);
18834 rtx tc, t0, t1, x;
18835
18836 tc = gen_reg_rtx (vmode);
18837 t0 = gen_reg_rtx (vmode);
18838 t1 = gen_reg_rtx (vmode);
18839
18840 /* op0 > op1 */
18841 emit_insn (cmp (tc, op0, op1));
18842
18843 x = gen_rtx_AND (vmode, tc, (min_p ? op1 : op0));
18844 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18845
18846 x = gen_rtx_NOT (vmode, tc);
18847 x = gen_rtx_AND (vmode, x, (min_p ? op0 : op1));
18848 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18849
18850 x = gen_rtx_IOR (vmode, t0, t1);
18851 emit_insn (gen_rtx_SET (VOIDmode, target, x));
18852 }
18853
18854 /* Implement TARGET_CASE_VALUES_THRESHOLD. */
18855
18856 unsigned int
18857 mips_case_values_threshold (void)
18858 {
18859 /* In MIPS16 mode using a larger case threshold generates smaller code. */
18860 if (TARGET_MIPS16 && optimize_size)
18861 return 10;
18862 else
18863 return default_case_values_threshold ();
18864 }
18865
18866 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */
18867
18868 static void
18869 mips_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
18870 {
18871 if (!TARGET_HARD_FLOAT_ABI)
18872 return;
18873 tree exceptions_var = create_tmp_var (MIPS_ATYPE_USI, NULL);
18874 tree fcsr_orig_var = create_tmp_var (MIPS_ATYPE_USI, NULL);
18875 tree fcsr_mod_var = create_tmp_var (MIPS_ATYPE_USI, NULL);
18876 tree get_fcsr = mips_builtin_decls[MIPS_GET_FCSR];
18877 tree set_fcsr = mips_builtin_decls[MIPS_SET_FCSR];
18878 tree get_fcsr_hold_call = build_call_expr (get_fcsr, 0);
18879 tree hold_assign_orig = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
18880 fcsr_orig_var, get_fcsr_hold_call);
18881 tree hold_mod_val = build2 (BIT_AND_EXPR, MIPS_ATYPE_USI, fcsr_orig_var,
18882 build_int_cst (MIPS_ATYPE_USI, 0xfffff003));
18883 tree hold_assign_mod = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
18884 fcsr_mod_var, hold_mod_val);
18885 tree set_fcsr_hold_call = build_call_expr (set_fcsr, 1, fcsr_mod_var);
18886 tree hold_all = build2 (COMPOUND_EXPR, MIPS_ATYPE_USI,
18887 hold_assign_orig, hold_assign_mod);
18888 *hold = build2 (COMPOUND_EXPR, void_type_node, hold_all,
18889 set_fcsr_hold_call);
18890
18891 *clear = build_call_expr (set_fcsr, 1, fcsr_mod_var);
18892
18893 tree get_fcsr_update_call = build_call_expr (get_fcsr, 0);
18894 *update = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
18895 exceptions_var, get_fcsr_update_call);
18896 tree set_fcsr_update_call = build_call_expr (set_fcsr, 1, fcsr_orig_var);
18897 *update = build2 (COMPOUND_EXPR, void_type_node, *update,
18898 set_fcsr_update_call);
18899 tree atomic_feraiseexcept
18900 = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
18901 tree int_exceptions_var = fold_convert (integer_type_node,
18902 exceptions_var);
18903 tree atomic_feraiseexcept_call = build_call_expr (atomic_feraiseexcept,
18904 1, int_exceptions_var);
18905 *update = build2 (COMPOUND_EXPR, void_type_node, *update,
18906 atomic_feraiseexcept_call);
18907 }
18908 \f
18909 /* Initialize the GCC target structure. */
18910 #undef TARGET_ASM_ALIGNED_HI_OP
18911 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
18912 #undef TARGET_ASM_ALIGNED_SI_OP
18913 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
18914 #undef TARGET_ASM_ALIGNED_DI_OP
18915 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
18916
18917 #undef TARGET_OPTION_OVERRIDE
18918 #define TARGET_OPTION_OVERRIDE mips_option_override
18919
18920 #undef TARGET_LEGITIMIZE_ADDRESS
18921 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
18922
18923 #undef TARGET_ASM_FUNCTION_PROLOGUE
18924 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
18925 #undef TARGET_ASM_FUNCTION_EPILOGUE
18926 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
18927 #undef TARGET_ASM_SELECT_RTX_SECTION
18928 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
18929 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
18930 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
18931
18932 #undef TARGET_SCHED_INIT
18933 #define TARGET_SCHED_INIT mips_sched_init
18934 #undef TARGET_SCHED_REORDER
18935 #define TARGET_SCHED_REORDER mips_sched_reorder
18936 #undef TARGET_SCHED_REORDER2
18937 #define TARGET_SCHED_REORDER2 mips_sched_reorder2
18938 #undef TARGET_SCHED_VARIABLE_ISSUE
18939 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
18940 #undef TARGET_SCHED_ADJUST_COST
18941 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
18942 #undef TARGET_SCHED_ISSUE_RATE
18943 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
18944 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
18945 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
18946 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
18947 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
18948 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
18949 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
18950 mips_multipass_dfa_lookahead
18951 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
18952 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
18953 mips_small_register_classes_for_mode_p
18954
18955 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
18956 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
18957
18958 #undef TARGET_INSERT_ATTRIBUTES
18959 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
18960 #undef TARGET_MERGE_DECL_ATTRIBUTES
18961 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
18962 #undef TARGET_CAN_INLINE_P
18963 #define TARGET_CAN_INLINE_P mips_can_inline_p
18964 #undef TARGET_SET_CURRENT_FUNCTION
18965 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
18966
18967 #undef TARGET_VALID_POINTER_MODE
18968 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
18969 #undef TARGET_REGISTER_MOVE_COST
18970 #define TARGET_REGISTER_MOVE_COST mips_register_move_cost
18971 #undef TARGET_MEMORY_MOVE_COST
18972 #define TARGET_MEMORY_MOVE_COST mips_memory_move_cost
18973 #undef TARGET_RTX_COSTS
18974 #define TARGET_RTX_COSTS mips_rtx_costs
18975 #undef TARGET_ADDRESS_COST
18976 #define TARGET_ADDRESS_COST mips_address_cost
18977
18978 #undef TARGET_IN_SMALL_DATA_P
18979 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
18980
18981 #undef TARGET_MACHINE_DEPENDENT_REORG
18982 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
18983
18984 #undef TARGET_PREFERRED_RELOAD_CLASS
18985 #define TARGET_PREFERRED_RELOAD_CLASS mips_preferred_reload_class
18986
18987 #undef TARGET_EXPAND_TO_RTL_HOOK
18988 #define TARGET_EXPAND_TO_RTL_HOOK mips_expand_to_rtl_hook
18989 #undef TARGET_ASM_FILE_START
18990 #define TARGET_ASM_FILE_START mips_file_start
18991 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
18992 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
18993 #undef TARGET_ASM_CODE_END
18994 #define TARGET_ASM_CODE_END mips_code_end
18995
18996 #undef TARGET_INIT_LIBFUNCS
18997 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
18998
18999 #undef TARGET_BUILD_BUILTIN_VA_LIST
19000 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
19001 #undef TARGET_EXPAND_BUILTIN_VA_START
19002 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
19003 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
19004 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
19005
19006 #undef TARGET_PROMOTE_FUNCTION_MODE
19007 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
19008 #undef TARGET_PROMOTE_PROTOTYPES
19009 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
19010
19011 #undef TARGET_FUNCTION_VALUE
19012 #define TARGET_FUNCTION_VALUE mips_function_value
19013 #undef TARGET_LIBCALL_VALUE
19014 #define TARGET_LIBCALL_VALUE mips_libcall_value
19015 #undef TARGET_FUNCTION_VALUE_REGNO_P
19016 #define TARGET_FUNCTION_VALUE_REGNO_P mips_function_value_regno_p
19017 #undef TARGET_RETURN_IN_MEMORY
19018 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
19019 #undef TARGET_RETURN_IN_MSB
19020 #define TARGET_RETURN_IN_MSB mips_return_in_msb
19021
19022 #undef TARGET_ASM_OUTPUT_MI_THUNK
19023 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
19024 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
19025 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
19026
19027 #undef TARGET_PRINT_OPERAND
19028 #define TARGET_PRINT_OPERAND mips_print_operand
19029 #undef TARGET_PRINT_OPERAND_ADDRESS
19030 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
19031 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
19032 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
19033
19034 #undef TARGET_SETUP_INCOMING_VARARGS
19035 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
19036 #undef TARGET_STRICT_ARGUMENT_NAMING
19037 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
19038 #undef TARGET_MUST_PASS_IN_STACK
19039 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
19040 #undef TARGET_PASS_BY_REFERENCE
19041 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
19042 #undef TARGET_CALLEE_COPIES
19043 #define TARGET_CALLEE_COPIES mips_callee_copies
19044 #undef TARGET_ARG_PARTIAL_BYTES
19045 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
19046 #undef TARGET_FUNCTION_ARG
19047 #define TARGET_FUNCTION_ARG mips_function_arg
19048 #undef TARGET_FUNCTION_ARG_ADVANCE
19049 #define TARGET_FUNCTION_ARG_ADVANCE mips_function_arg_advance
19050 #undef TARGET_FUNCTION_ARG_BOUNDARY
19051 #define TARGET_FUNCTION_ARG_BOUNDARY mips_function_arg_boundary
19052
19053 #undef TARGET_MODE_REP_EXTENDED
19054 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
19055
19056 #undef TARGET_VECTOR_MODE_SUPPORTED_P
19057 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
19058
19059 #undef TARGET_SCALAR_MODE_SUPPORTED_P
19060 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
19061
19062 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
19063 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
19064
19065 #undef TARGET_INIT_BUILTINS
19066 #define TARGET_INIT_BUILTINS mips_init_builtins
19067 #undef TARGET_BUILTIN_DECL
19068 #define TARGET_BUILTIN_DECL mips_builtin_decl
19069 #undef TARGET_EXPAND_BUILTIN
19070 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
19071
19072 #undef TARGET_HAVE_TLS
19073 #define TARGET_HAVE_TLS HAVE_AS_TLS
19074
19075 #undef TARGET_CANNOT_FORCE_CONST_MEM
19076 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
19077
19078 #undef TARGET_LEGITIMATE_CONSTANT_P
19079 #define TARGET_LEGITIMATE_CONSTANT_P mips_legitimate_constant_p
19080
19081 #undef TARGET_ENCODE_SECTION_INFO
19082 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
19083
19084 #undef TARGET_ATTRIBUTE_TABLE
19085 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
19086 /* All our function attributes are related to how out-of-line copies should
19087 be compiled or called. They don't in themselves prevent inlining. */
19088 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
19089 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
19090
19091 #undef TARGET_EXTRA_LIVE_ON_ENTRY
19092 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
19093
19094 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
19095 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
19096 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
19097 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
19098
19099 #undef TARGET_COMP_TYPE_ATTRIBUTES
19100 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
19101
19102 #ifdef HAVE_AS_DTPRELWORD
19103 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
19104 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
19105 #endif
19106 #undef TARGET_DWARF_REGISTER_SPAN
19107 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
19108
19109 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
19110 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
19111
19112 #undef TARGET_LEGITIMATE_ADDRESS_P
19113 #define TARGET_LEGITIMATE_ADDRESS_P mips_legitimate_address_p
19114
19115 #undef TARGET_FRAME_POINTER_REQUIRED
19116 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
19117
19118 #undef TARGET_CAN_ELIMINATE
19119 #define TARGET_CAN_ELIMINATE mips_can_eliminate
19120
19121 #undef TARGET_CONDITIONAL_REGISTER_USAGE
19122 #define TARGET_CONDITIONAL_REGISTER_USAGE mips_conditional_register_usage
19123
19124 #undef TARGET_TRAMPOLINE_INIT
19125 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
19126
19127 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
19128 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
19129
19130 #undef TARGET_SHIFT_TRUNCATION_MASK
19131 #define TARGET_SHIFT_TRUNCATION_MASK mips_shift_truncation_mask
19132
19133 #undef TARGET_PREPARE_PCH_SAVE
19134 #define TARGET_PREPARE_PCH_SAVE mips_prepare_pch_save
19135
19136 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
19137 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK mips_vectorize_vec_perm_const_ok
19138
19139 #undef TARGET_CASE_VALUES_THRESHOLD
19140 #define TARGET_CASE_VALUES_THRESHOLD mips_case_values_threshold
19141
19142 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
19143 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV mips_atomic_assign_expand_fenv
19144
19145 struct gcc_target targetm = TARGET_INITIALIZER;
19146 \f
19147 #include "gt-mips.h"