]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/mips/mips.c
2014-11-01 Andrew MacLeod <amacleod@redhat,com>
[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 "hashtab.h"
42 #include "hash-set.h"
43 #include "vec.h"
44 #include "machmode.h"
45 #include "input.h"
46 #include "function.h"
47 #include "expr.h"
48 #include "insn-codes.h"
49 #include "optabs.h"
50 #include "libfuncs.h"
51 #include "flags.h"
52 #include "reload.h"
53 #include "tm_p.h"
54 #include "ggc.h"
55 #include "gstab.h"
56 #include "hash-table.h"
57 #include "debug.h"
58 #include "target.h"
59 #include "target-def.h"
60 #include "common/common-target.h"
61 #include "langhooks.h"
62 #include "dominance.h"
63 #include "cfg.h"
64 #include "cfgrtl.h"
65 #include "cfganal.h"
66 #include "lcm.h"
67 #include "cfgbuild.h"
68 #include "cfgcleanup.h"
69 #include "predict.h"
70 #include "basic-block.h"
71 #include "sched-int.h"
72 #include "tree-ssa-alias.h"
73 #include "internal-fn.h"
74 #include "gimple-fold.h"
75 #include "tree-eh.h"
76 #include "gimple-expr.h"
77 #include "is-a.h"
78 #include "gimple.h"
79 #include "gimplify.h"
80 #include "bitmap.h"
81 #include "diagnostic.h"
82 #include "target-globals.h"
83 #include "opts.h"
84 #include "tree-pass.h"
85 #include "context.h"
86 #include "hash-map.h"
87 #include "plugin-api.h"
88 #include "ipa-ref.h"
89 #include "cgraph.h"
90 #include "builtins.h"
91 #include "rtl-iter.h"
92
93 /* True if X is an UNSPEC wrapper around a SYMBOL_REF or LABEL_REF. */
94 #define UNSPEC_ADDRESS_P(X) \
95 (GET_CODE (X) == UNSPEC \
96 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
97 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
98
99 /* Extract the symbol or label from UNSPEC wrapper X. */
100 #define UNSPEC_ADDRESS(X) \
101 XVECEXP (X, 0, 0)
102
103 /* Extract the symbol type from UNSPEC wrapper X. */
104 #define UNSPEC_ADDRESS_TYPE(X) \
105 ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
106
107 /* The maximum distance between the top of the stack frame and the
108 value $sp has when we save and restore registers.
109
110 The value for normal-mode code must be a SMALL_OPERAND and must
111 preserve the maximum stack alignment. We therefore use a value
112 of 0x7ff0 in this case.
113
114 microMIPS LWM and SWM support 12-bit offsets (from -0x800 to 0x7ff),
115 so we use a maximum of 0x7f0 for TARGET_MICROMIPS.
116
117 MIPS16e SAVE and RESTORE instructions can adjust the stack pointer by
118 up to 0x7f8 bytes and can usually save or restore all the registers
119 that we need to save or restore. (Note that we can only use these
120 instructions for o32, for which the stack alignment is 8 bytes.)
121
122 We use a maximum gap of 0x100 or 0x400 for MIPS16 code when SAVE and
123 RESTORE are not available. We can then use unextended instructions
124 to save and restore registers, and to allocate and deallocate the top
125 part of the frame. */
126 #define MIPS_MAX_FIRST_STACK_STEP \
127 (!TARGET_COMPRESSION ? 0x7ff0 \
128 : TARGET_MICROMIPS || GENERATE_MIPS16E_SAVE_RESTORE ? 0x7f8 \
129 : TARGET_64BIT ? 0x100 : 0x400)
130
131 /* True if INSN is a mips.md pattern or asm statement. */
132 /* ??? This test exists through the compiler, perhaps it should be
133 moved to rtl.h. */
134 #define USEFUL_INSN_P(INSN) \
135 (NONDEBUG_INSN_P (INSN) \
136 && GET_CODE (PATTERN (INSN)) != USE \
137 && GET_CODE (PATTERN (INSN)) != CLOBBER)
138
139 /* If INSN is a delayed branch sequence, return the first instruction
140 in the sequence, otherwise return INSN itself. */
141 #define SEQ_BEGIN(INSN) \
142 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
143 ? as_a <rtx_insn *> (XVECEXP (PATTERN (INSN), 0, 0)) \
144 : (INSN))
145
146 /* Likewise for the last instruction in a delayed branch sequence. */
147 #define SEQ_END(INSN) \
148 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
149 ? as_a <rtx_insn *> (XVECEXP (PATTERN (INSN), \
150 0, \
151 XVECLEN (PATTERN (INSN), 0) - 1)) \
152 : (INSN))
153
154 /* Execute the following loop body with SUBINSN set to each instruction
155 between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive. */
156 #define FOR_EACH_SUBINSN(SUBINSN, INSN) \
157 for ((SUBINSN) = SEQ_BEGIN (INSN); \
158 (SUBINSN) != NEXT_INSN (SEQ_END (INSN)); \
159 (SUBINSN) = NEXT_INSN (SUBINSN))
160
161 /* True if bit BIT is set in VALUE. */
162 #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0)
163
164 /* Return the opcode for a ptr_mode load of the form:
165
166 l[wd] DEST, OFFSET(BASE). */
167 #define MIPS_LOAD_PTR(DEST, OFFSET, BASE) \
168 (((ptr_mode == DImode ? 0x37 : 0x23) << 26) \
169 | ((BASE) << 21) \
170 | ((DEST) << 16) \
171 | (OFFSET))
172
173 /* Return the opcode to move register SRC into register DEST. */
174 #define MIPS_MOVE(DEST, SRC) \
175 ((TARGET_64BIT ? 0x2d : 0x21) \
176 | ((DEST) << 11) \
177 | ((SRC) << 21))
178
179 /* Return the opcode for:
180
181 lui DEST, VALUE. */
182 #define MIPS_LUI(DEST, VALUE) \
183 ((0xf << 26) | ((DEST) << 16) | (VALUE))
184
185 /* Return the opcode to jump to register DEST. */
186 #define MIPS_JR(DEST) \
187 (((DEST) << 21) | 0x8)
188
189 /* Return the opcode for:
190
191 bal . + (1 + OFFSET) * 4. */
192 #define MIPS_BAL(OFFSET) \
193 ((0x1 << 26) | (0x11 << 16) | (OFFSET))
194
195 /* Return the usual opcode for a nop. */
196 #define MIPS_NOP 0
197
198 /* Classifies an address.
199
200 ADDRESS_REG
201 A natural register + offset address. The register satisfies
202 mips_valid_base_register_p and the offset is a const_arith_operand.
203
204 ADDRESS_LO_SUM
205 A LO_SUM rtx. The first operand is a valid base register and
206 the second operand is a symbolic address.
207
208 ADDRESS_CONST_INT
209 A signed 16-bit constant address.
210
211 ADDRESS_SYMBOLIC:
212 A constant symbolic address. */
213 enum mips_address_type {
214 ADDRESS_REG,
215 ADDRESS_LO_SUM,
216 ADDRESS_CONST_INT,
217 ADDRESS_SYMBOLIC
218 };
219
220 /* Macros to create an enumeration identifier for a function prototype. */
221 #define MIPS_FTYPE_NAME1(A, B) MIPS_##A##_FTYPE_##B
222 #define MIPS_FTYPE_NAME2(A, B, C) MIPS_##A##_FTYPE_##B##_##C
223 #define MIPS_FTYPE_NAME3(A, B, C, D) MIPS_##A##_FTYPE_##B##_##C##_##D
224 #define MIPS_FTYPE_NAME4(A, B, C, D, E) MIPS_##A##_FTYPE_##B##_##C##_##D##_##E
225
226 /* Classifies the prototype of a built-in function. */
227 enum mips_function_type {
228 #define DEF_MIPS_FTYPE(NARGS, LIST) MIPS_FTYPE_NAME##NARGS LIST,
229 #include "config/mips/mips-ftypes.def"
230 #undef DEF_MIPS_FTYPE
231 MIPS_MAX_FTYPE_MAX
232 };
233
234 /* Specifies how a built-in function should be converted into rtl. */
235 enum mips_builtin_type {
236 /* The function corresponds directly to an .md pattern. The return
237 value is mapped to operand 0 and the arguments are mapped to
238 operands 1 and above. */
239 MIPS_BUILTIN_DIRECT,
240
241 /* The function corresponds directly to an .md pattern. There is no return
242 value and the arguments are mapped to operands 0 and above. */
243 MIPS_BUILTIN_DIRECT_NO_TARGET,
244
245 /* The function corresponds to a comparison instruction followed by
246 a mips_cond_move_tf_ps pattern. The first two arguments are the
247 values to compare and the second two arguments are the vector
248 operands for the movt.ps or movf.ps instruction (in assembly order). */
249 MIPS_BUILTIN_MOVF,
250 MIPS_BUILTIN_MOVT,
251
252 /* The function corresponds to a V2SF comparison instruction. Operand 0
253 of this instruction is the result of the comparison, which has mode
254 CCV2 or CCV4. The function arguments are mapped to operands 1 and
255 above. The function's return value is an SImode boolean that is
256 true under the following conditions:
257
258 MIPS_BUILTIN_CMP_ANY: one of the registers is true
259 MIPS_BUILTIN_CMP_ALL: all of the registers are true
260 MIPS_BUILTIN_CMP_LOWER: the first register is true
261 MIPS_BUILTIN_CMP_UPPER: the second register is true. */
262 MIPS_BUILTIN_CMP_ANY,
263 MIPS_BUILTIN_CMP_ALL,
264 MIPS_BUILTIN_CMP_UPPER,
265 MIPS_BUILTIN_CMP_LOWER,
266
267 /* As above, but the instruction only sets a single $fcc register. */
268 MIPS_BUILTIN_CMP_SINGLE,
269
270 /* For generating bposge32 branch instructions in MIPS32 DSP ASE. */
271 MIPS_BUILTIN_BPOSGE32
272 };
273
274 /* Invoke MACRO (COND) for each C.cond.fmt condition. */
275 #define MIPS_FP_CONDITIONS(MACRO) \
276 MACRO (f), \
277 MACRO (un), \
278 MACRO (eq), \
279 MACRO (ueq), \
280 MACRO (olt), \
281 MACRO (ult), \
282 MACRO (ole), \
283 MACRO (ule), \
284 MACRO (sf), \
285 MACRO (ngle), \
286 MACRO (seq), \
287 MACRO (ngl), \
288 MACRO (lt), \
289 MACRO (nge), \
290 MACRO (le), \
291 MACRO (ngt)
292
293 /* Enumerates the codes above as MIPS_FP_COND_<X>. */
294 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
295 enum mips_fp_condition {
296 MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
297 };
298 #undef DECLARE_MIPS_COND
299
300 /* Index X provides the string representation of MIPS_FP_COND_<X>. */
301 #define STRINGIFY(X) #X
302 static const char *const mips_fp_conditions[] = {
303 MIPS_FP_CONDITIONS (STRINGIFY)
304 };
305 #undef STRINGIFY
306
307 /* A class used to control a comdat-style stub that we output in each
308 translation unit that needs it. */
309 class mips_one_only_stub {
310 public:
311 virtual ~mips_one_only_stub () {}
312
313 /* Return the name of the stub. */
314 virtual const char *get_name () = 0;
315
316 /* Output the body of the function to asm_out_file. */
317 virtual void output_body () = 0;
318 };
319
320 /* Tuning information that is automatically derived from other sources
321 (such as the scheduler). */
322 static struct {
323 /* The architecture and tuning settings that this structure describes. */
324 enum processor arch;
325 enum processor tune;
326
327 /* True if this structure describes MIPS16 settings. */
328 bool mips16_p;
329
330 /* True if the structure has been initialized. */
331 bool initialized_p;
332
333 /* True if "MULT $0, $0" is preferable to "MTLO $0; MTHI $0"
334 when optimizing for speed. */
335 bool fast_mult_zero_zero_p;
336 } mips_tuning_info;
337
338 /* Information about a function's frame layout. */
339 struct GTY(()) mips_frame_info {
340 /* The size of the frame in bytes. */
341 HOST_WIDE_INT total_size;
342
343 /* The number of bytes allocated to variables. */
344 HOST_WIDE_INT var_size;
345
346 /* The number of bytes allocated to outgoing function arguments. */
347 HOST_WIDE_INT args_size;
348
349 /* The number of bytes allocated to the .cprestore slot, or 0 if there
350 is no such slot. */
351 HOST_WIDE_INT cprestore_size;
352
353 /* Bit X is set if the function saves or restores GPR X. */
354 unsigned int mask;
355
356 /* Likewise FPR X. */
357 unsigned int fmask;
358
359 /* Likewise doubleword accumulator X ($acX). */
360 unsigned int acc_mask;
361
362 /* The number of GPRs, FPRs, doubleword accumulators and COP0
363 registers saved. */
364 unsigned int num_gp;
365 unsigned int num_fp;
366 unsigned int num_acc;
367 unsigned int num_cop0_regs;
368
369 /* The offset of the topmost GPR, FPR, accumulator and COP0-register
370 save slots from the top of the frame, or zero if no such slots are
371 needed. */
372 HOST_WIDE_INT gp_save_offset;
373 HOST_WIDE_INT fp_save_offset;
374 HOST_WIDE_INT acc_save_offset;
375 HOST_WIDE_INT cop0_save_offset;
376
377 /* Likewise, but giving offsets from the bottom of the frame. */
378 HOST_WIDE_INT gp_sp_offset;
379 HOST_WIDE_INT fp_sp_offset;
380 HOST_WIDE_INT acc_sp_offset;
381 HOST_WIDE_INT cop0_sp_offset;
382
383 /* Similar, but the value passed to _mcount. */
384 HOST_WIDE_INT ra_fp_offset;
385
386 /* The offset of arg_pointer_rtx from the bottom of the frame. */
387 HOST_WIDE_INT arg_pointer_offset;
388
389 /* The offset of hard_frame_pointer_rtx from the bottom of the frame. */
390 HOST_WIDE_INT hard_frame_pointer_offset;
391 };
392
393 struct GTY(()) machine_function {
394 /* The next floating-point condition-code register to allocate
395 for ISA_HAS_8CC targets, relative to ST_REG_FIRST. */
396 unsigned int next_fcc;
397
398 /* The register returned by mips16_gp_pseudo_reg; see there for details. */
399 rtx mips16_gp_pseudo_rtx;
400
401 /* The number of extra stack bytes taken up by register varargs.
402 This area is allocated by the callee at the very top of the frame. */
403 int varargs_size;
404
405 /* The current frame information, calculated by mips_compute_frame_info. */
406 struct mips_frame_info frame;
407
408 /* The register to use as the function's global pointer, or INVALID_REGNUM
409 if the function doesn't need one. */
410 unsigned int global_pointer;
411
412 /* How many instructions it takes to load a label into $AT, or 0 if
413 this property hasn't yet been calculated. */
414 unsigned int load_label_num_insns;
415
416 /* True if mips_adjust_insn_length should ignore an instruction's
417 hazard attribute. */
418 bool ignore_hazard_length_p;
419
420 /* True if the whole function is suitable for .set noreorder and
421 .set nomacro. */
422 bool all_noreorder_p;
423
424 /* True if the function has "inflexible" and "flexible" references
425 to the global pointer. See mips_cfun_has_inflexible_gp_ref_p
426 and mips_cfun_has_flexible_gp_ref_p for details. */
427 bool has_inflexible_gp_insn_p;
428 bool has_flexible_gp_insn_p;
429
430 /* True if the function's prologue must load the global pointer
431 value into pic_offset_table_rtx and store the same value in
432 the function's cprestore slot (if any). Even if this value
433 is currently false, we may decide to set it to true later;
434 see mips_must_initialize_gp_p () for details. */
435 bool must_initialize_gp_p;
436
437 /* True if the current function must restore $gp after any potential
438 clobber. This value is only meaningful during the first post-epilogue
439 split_insns pass; see mips_must_initialize_gp_p () for details. */
440 bool must_restore_gp_when_clobbered_p;
441
442 /* True if this is an interrupt handler. */
443 bool interrupt_handler_p;
444
445 /* True if this is an interrupt handler that uses shadow registers. */
446 bool use_shadow_register_set_p;
447
448 /* True if this is an interrupt handler that should keep interrupts
449 masked. */
450 bool keep_interrupts_masked_p;
451
452 /* True if this is an interrupt handler that should use DERET
453 instead of ERET. */
454 bool use_debug_exception_return_p;
455 };
456
457 /* Information about a single argument. */
458 struct mips_arg_info {
459 /* True if the argument is passed in a floating-point register, or
460 would have been if we hadn't run out of registers. */
461 bool fpr_p;
462
463 /* The number of words passed in registers, rounded up. */
464 unsigned int reg_words;
465
466 /* For EABI, the offset of the first register from GP_ARG_FIRST or
467 FP_ARG_FIRST. For other ABIs, the offset of the first register from
468 the start of the ABI's argument structure (see the CUMULATIVE_ARGS
469 comment for details).
470
471 The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
472 on the stack. */
473 unsigned int reg_offset;
474
475 /* The number of words that must be passed on the stack, rounded up. */
476 unsigned int stack_words;
477
478 /* The offset from the start of the stack overflow area of the argument's
479 first stack word. Only meaningful when STACK_WORDS is nonzero. */
480 unsigned int stack_offset;
481 };
482
483 /* Information about an address described by mips_address_type.
484
485 ADDRESS_CONST_INT
486 No fields are used.
487
488 ADDRESS_REG
489 REG is the base register and OFFSET is the constant offset.
490
491 ADDRESS_LO_SUM
492 REG and OFFSET are the operands to the LO_SUM and SYMBOL_TYPE
493 is the type of symbol it references.
494
495 ADDRESS_SYMBOLIC
496 SYMBOL_TYPE is the type of symbol that the address references. */
497 struct mips_address_info {
498 enum mips_address_type type;
499 rtx reg;
500 rtx offset;
501 enum mips_symbol_type symbol_type;
502 };
503
504 /* One stage in a constant building sequence. These sequences have
505 the form:
506
507 A = VALUE[0]
508 A = A CODE[1] VALUE[1]
509 A = A CODE[2] VALUE[2]
510 ...
511
512 where A is an accumulator, each CODE[i] is a binary rtl operation
513 and each VALUE[i] is a constant integer. CODE[0] is undefined. */
514 struct mips_integer_op {
515 enum rtx_code code;
516 unsigned HOST_WIDE_INT value;
517 };
518
519 /* The largest number of operations needed to load an integer constant.
520 The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
521 When the lowest bit is clear, we can try, but reject a sequence with
522 an extra SLL at the end. */
523 #define MIPS_MAX_INTEGER_OPS 7
524
525 /* Information about a MIPS16e SAVE or RESTORE instruction. */
526 struct mips16e_save_restore_info {
527 /* The number of argument registers saved by a SAVE instruction.
528 0 for RESTORE instructions. */
529 unsigned int nargs;
530
531 /* Bit X is set if the instruction saves or restores GPR X. */
532 unsigned int mask;
533
534 /* The total number of bytes to allocate. */
535 HOST_WIDE_INT size;
536 };
537
538 /* Costs of various operations on the different architectures. */
539
540 struct mips_rtx_cost_data
541 {
542 unsigned short fp_add;
543 unsigned short fp_mult_sf;
544 unsigned short fp_mult_df;
545 unsigned short fp_div_sf;
546 unsigned short fp_div_df;
547 unsigned short int_mult_si;
548 unsigned short int_mult_di;
549 unsigned short int_div_si;
550 unsigned short int_div_di;
551 unsigned short branch_cost;
552 unsigned short memory_latency;
553 };
554
555 /* Global variables for machine-dependent things. */
556
557 /* The -G setting, or the configuration's default small-data limit if
558 no -G option is given. */
559 static unsigned int mips_small_data_threshold;
560
561 /* The number of file directives written by mips_output_filename. */
562 int num_source_filenames;
563
564 /* The name that appeared in the last .file directive written by
565 mips_output_filename, or "" if mips_output_filename hasn't
566 written anything yet. */
567 const char *current_function_file = "";
568
569 /* Arrays that map GCC register numbers to debugger register numbers. */
570 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
571 int mips_dwarf_regno[FIRST_PSEUDO_REGISTER];
572
573 /* Information about the current function's epilogue, used only while
574 expanding it. */
575 static struct {
576 /* A list of queued REG_CFA_RESTORE notes. */
577 rtx cfa_restores;
578
579 /* The CFA is currently defined as CFA_REG + CFA_OFFSET. */
580 rtx cfa_reg;
581 HOST_WIDE_INT cfa_offset;
582
583 /* The offset of the CFA from the stack pointer while restoring
584 registers. */
585 HOST_WIDE_INT cfa_restore_sp_offset;
586 } mips_epilogue;
587
588 /* The nesting depth of the PRINT_OPERAND '%(', '%<' and '%[' constructs. */
589 struct mips_asm_switch mips_noreorder = { "reorder", 0 };
590 struct mips_asm_switch mips_nomacro = { "macro", 0 };
591 struct mips_asm_switch mips_noat = { "at", 0 };
592
593 /* True if we're writing out a branch-likely instruction rather than a
594 normal branch. */
595 static bool mips_branch_likely;
596
597 /* The current instruction-set architecture. */
598 enum processor mips_arch;
599 const struct mips_cpu_info *mips_arch_info;
600
601 /* The processor that we should tune the code for. */
602 enum processor mips_tune;
603 const struct mips_cpu_info *mips_tune_info;
604
605 /* The ISA level associated with mips_arch. */
606 int mips_isa;
607
608 /* The ISA revision level. This is 0 for MIPS I to V and N for
609 MIPS{32,64}rN. */
610 int mips_isa_rev;
611
612 /* The architecture selected by -mipsN, or null if -mipsN wasn't used. */
613 static const struct mips_cpu_info *mips_isa_option_info;
614
615 /* Which cost information to use. */
616 static const struct mips_rtx_cost_data *mips_cost;
617
618 /* The ambient target flags, excluding MASK_MIPS16. */
619 static int mips_base_target_flags;
620
621 /* The default compression mode. */
622 unsigned int mips_base_compression_flags;
623
624 /* The ambient values of other global variables. */
625 static int mips_base_schedule_insns; /* flag_schedule_insns */
626 static int mips_base_reorder_blocks_and_partition; /* flag_reorder... */
627 static int mips_base_move_loop_invariants; /* flag_move_loop_invariants */
628 static int mips_base_align_loops; /* align_loops */
629 static int mips_base_align_jumps; /* align_jumps */
630 static int mips_base_align_functions; /* align_functions */
631
632 /* Index [M][R] is true if register R is allowed to hold a value of mode M. */
633 bool mips_hard_regno_mode_ok[(int) MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
634
635 /* Index C is true if character C is a valid PRINT_OPERAND punctation
636 character. */
637 static bool mips_print_operand_punct[256];
638
639 static GTY (()) int mips_output_filename_first_time = 1;
640
641 /* mips_split_p[X] is true if symbols of type X can be split by
642 mips_split_symbol. */
643 bool mips_split_p[NUM_SYMBOL_TYPES];
644
645 /* mips_split_hi_p[X] is true if the high parts of symbols of type X
646 can be split by mips_split_symbol. */
647 bool mips_split_hi_p[NUM_SYMBOL_TYPES];
648
649 /* mips_use_pcrel_pool_p[X] is true if symbols of type X should be
650 forced into a PC-relative constant pool. */
651 bool mips_use_pcrel_pool_p[NUM_SYMBOL_TYPES];
652
653 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
654 appears in a LO_SUM. It can be null if such LO_SUMs aren't valid or
655 if they are matched by a special .md file pattern. */
656 const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
657
658 /* Likewise for HIGHs. */
659 const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
660
661 /* Target state for MIPS16. */
662 struct target_globals *mips16_globals;
663
664 /* Cached value of can_issue_more. This is cached in mips_variable_issue hook
665 and returned from mips_sched_reorder2. */
666 static int cached_can_issue_more;
667
668 /* The stubs for various MIPS16 support functions, if used. */
669 static mips_one_only_stub *mips16_rdhwr_stub;
670 static mips_one_only_stub *mips16_get_fcsr_stub;
671 static mips_one_only_stub *mips16_set_fcsr_stub;
672
673 /* Index R is the smallest register class that contains register R. */
674 const enum reg_class mips_regno_to_class[FIRST_PSEUDO_REGISTER] = {
675 LEA_REGS, LEA_REGS, M16_STORE_REGS, V1_REG,
676 M16_STORE_REGS, M16_STORE_REGS, M16_STORE_REGS, M16_STORE_REGS,
677 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
678 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
679 M16_REGS, M16_STORE_REGS, LEA_REGS, LEA_REGS,
680 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
681 T_REG, PIC_FN_ADDR_REG, LEA_REGS, LEA_REGS,
682 LEA_REGS, M16_SP_REGS, LEA_REGS, LEA_REGS,
683
684 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
685 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
686 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
687 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
688 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
689 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
690 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
691 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
692 MD0_REG, MD1_REG, NO_REGS, ST_REGS,
693 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
694 ST_REGS, ST_REGS, ST_REGS, NO_REGS,
695 NO_REGS, FRAME_REGS, FRAME_REGS, NO_REGS,
696 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
697 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
698 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
699 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
700 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
701 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
702 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
703 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
704 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
705 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
706 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
707 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
708 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
709 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
710 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
711 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
712 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
713 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
714 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
715 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
716 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
717 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
718 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
719 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
720 DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS,
721 DSP_ACC_REGS, DSP_ACC_REGS, ALL_REGS, ALL_REGS,
722 ALL_REGS, ALL_REGS, ALL_REGS, ALL_REGS
723 };
724
725 /* The value of TARGET_ATTRIBUTE_TABLE. */
726 static const struct attribute_spec mips_attribute_table[] = {
727 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
728 om_diagnostic } */
729 { "long_call", 0, 0, false, true, true, NULL, false },
730 { "far", 0, 0, false, true, true, NULL, false },
731 { "near", 0, 0, false, true, true, NULL, false },
732 /* We would really like to treat "mips16" and "nomips16" as type
733 attributes, but GCC doesn't provide the hooks we need to support
734 the right conversion rules. As declaration attributes, they affect
735 code generation but don't carry other semantics. */
736 { "mips16", 0, 0, true, false, false, NULL, false },
737 { "nomips16", 0, 0, true, false, false, NULL, false },
738 { "micromips", 0, 0, true, false, false, NULL, false },
739 { "nomicromips", 0, 0, true, false, false, NULL, false },
740 { "nocompression", 0, 0, true, false, false, NULL, false },
741 /* Allow functions to be specified as interrupt handlers */
742 { "interrupt", 0, 0, false, true, true, NULL, false },
743 { "use_shadow_register_set", 0, 0, false, true, true, NULL, false },
744 { "keep_interrupts_masked", 0, 0, false, true, true, NULL, false },
745 { "use_debug_exception_return", 0, 0, false, true, true, NULL, false },
746 { NULL, 0, 0, false, false, false, NULL, false }
747 };
748 \f
749 /* A table describing all the processors GCC knows about; see
750 mips-cpus.def for details. */
751 static const struct mips_cpu_info mips_cpu_info_table[] = {
752 #define MIPS_CPU(NAME, CPU, ISA, FLAGS) \
753 { NAME, CPU, ISA, FLAGS },
754 #include "mips-cpus.def"
755 #undef MIPS_CPU
756 };
757
758 /* Default costs. If these are used for a processor we should look
759 up the actual costs. */
760 #define DEFAULT_COSTS COSTS_N_INSNS (6), /* fp_add */ \
761 COSTS_N_INSNS (7), /* fp_mult_sf */ \
762 COSTS_N_INSNS (8), /* fp_mult_df */ \
763 COSTS_N_INSNS (23), /* fp_div_sf */ \
764 COSTS_N_INSNS (36), /* fp_div_df */ \
765 COSTS_N_INSNS (10), /* int_mult_si */ \
766 COSTS_N_INSNS (10), /* int_mult_di */ \
767 COSTS_N_INSNS (69), /* int_div_si */ \
768 COSTS_N_INSNS (69), /* int_div_di */ \
769 2, /* branch_cost */ \
770 4 /* memory_latency */
771
772 /* Floating-point costs for processors without an FPU. Just assume that
773 all floating-point libcalls are very expensive. */
774 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */ \
775 COSTS_N_INSNS (256), /* fp_mult_sf */ \
776 COSTS_N_INSNS (256), /* fp_mult_df */ \
777 COSTS_N_INSNS (256), /* fp_div_sf */ \
778 COSTS_N_INSNS (256) /* fp_div_df */
779
780 /* Costs to use when optimizing for size. */
781 static const struct mips_rtx_cost_data mips_rtx_cost_optimize_size = {
782 COSTS_N_INSNS (1), /* fp_add */
783 COSTS_N_INSNS (1), /* fp_mult_sf */
784 COSTS_N_INSNS (1), /* fp_mult_df */
785 COSTS_N_INSNS (1), /* fp_div_sf */
786 COSTS_N_INSNS (1), /* fp_div_df */
787 COSTS_N_INSNS (1), /* int_mult_si */
788 COSTS_N_INSNS (1), /* int_mult_di */
789 COSTS_N_INSNS (1), /* int_div_si */
790 COSTS_N_INSNS (1), /* int_div_di */
791 2, /* branch_cost */
792 4 /* memory_latency */
793 };
794
795 /* Costs to use when optimizing for speed, indexed by processor. */
796 static const struct mips_rtx_cost_data
797 mips_rtx_cost_data[NUM_PROCESSOR_VALUES] = {
798 { /* R3000 */
799 COSTS_N_INSNS (2), /* fp_add */
800 COSTS_N_INSNS (4), /* fp_mult_sf */
801 COSTS_N_INSNS (5), /* fp_mult_df */
802 COSTS_N_INSNS (12), /* fp_div_sf */
803 COSTS_N_INSNS (19), /* fp_div_df */
804 COSTS_N_INSNS (12), /* int_mult_si */
805 COSTS_N_INSNS (12), /* int_mult_di */
806 COSTS_N_INSNS (35), /* int_div_si */
807 COSTS_N_INSNS (35), /* int_div_di */
808 1, /* branch_cost */
809 4 /* memory_latency */
810 },
811 { /* 4KC */
812 SOFT_FP_COSTS,
813 COSTS_N_INSNS (6), /* int_mult_si */
814 COSTS_N_INSNS (6), /* int_mult_di */
815 COSTS_N_INSNS (36), /* int_div_si */
816 COSTS_N_INSNS (36), /* int_div_di */
817 1, /* branch_cost */
818 4 /* memory_latency */
819 },
820 { /* 4KP */
821 SOFT_FP_COSTS,
822 COSTS_N_INSNS (36), /* int_mult_si */
823 COSTS_N_INSNS (36), /* int_mult_di */
824 COSTS_N_INSNS (37), /* int_div_si */
825 COSTS_N_INSNS (37), /* int_div_di */
826 1, /* branch_cost */
827 4 /* memory_latency */
828 },
829 { /* 5KC */
830 SOFT_FP_COSTS,
831 COSTS_N_INSNS (4), /* int_mult_si */
832 COSTS_N_INSNS (11), /* int_mult_di */
833 COSTS_N_INSNS (36), /* int_div_si */
834 COSTS_N_INSNS (68), /* int_div_di */
835 1, /* branch_cost */
836 4 /* memory_latency */
837 },
838 { /* 5KF */
839 COSTS_N_INSNS (4), /* fp_add */
840 COSTS_N_INSNS (4), /* fp_mult_sf */
841 COSTS_N_INSNS (5), /* fp_mult_df */
842 COSTS_N_INSNS (17), /* fp_div_sf */
843 COSTS_N_INSNS (32), /* fp_div_df */
844 COSTS_N_INSNS (4), /* int_mult_si */
845 COSTS_N_INSNS (11), /* int_mult_di */
846 COSTS_N_INSNS (36), /* int_div_si */
847 COSTS_N_INSNS (68), /* int_div_di */
848 1, /* branch_cost */
849 4 /* memory_latency */
850 },
851 { /* 20KC */
852 COSTS_N_INSNS (4), /* fp_add */
853 COSTS_N_INSNS (4), /* fp_mult_sf */
854 COSTS_N_INSNS (5), /* fp_mult_df */
855 COSTS_N_INSNS (17), /* fp_div_sf */
856 COSTS_N_INSNS (32), /* fp_div_df */
857 COSTS_N_INSNS (4), /* int_mult_si */
858 COSTS_N_INSNS (7), /* int_mult_di */
859 COSTS_N_INSNS (42), /* int_div_si */
860 COSTS_N_INSNS (72), /* int_div_di */
861 1, /* branch_cost */
862 4 /* memory_latency */
863 },
864 { /* 24KC */
865 SOFT_FP_COSTS,
866 COSTS_N_INSNS (5), /* int_mult_si */
867 COSTS_N_INSNS (5), /* int_mult_di */
868 COSTS_N_INSNS (41), /* int_div_si */
869 COSTS_N_INSNS (41), /* int_div_di */
870 1, /* branch_cost */
871 4 /* memory_latency */
872 },
873 { /* 24KF2_1 */
874 COSTS_N_INSNS (8), /* fp_add */
875 COSTS_N_INSNS (8), /* fp_mult_sf */
876 COSTS_N_INSNS (10), /* fp_mult_df */
877 COSTS_N_INSNS (34), /* fp_div_sf */
878 COSTS_N_INSNS (64), /* fp_div_df */
879 COSTS_N_INSNS (5), /* int_mult_si */
880 COSTS_N_INSNS (5), /* int_mult_di */
881 COSTS_N_INSNS (41), /* int_div_si */
882 COSTS_N_INSNS (41), /* int_div_di */
883 1, /* branch_cost */
884 4 /* memory_latency */
885 },
886 { /* 24KF1_1 */
887 COSTS_N_INSNS (4), /* fp_add */
888 COSTS_N_INSNS (4), /* fp_mult_sf */
889 COSTS_N_INSNS (5), /* fp_mult_df */
890 COSTS_N_INSNS (17), /* fp_div_sf */
891 COSTS_N_INSNS (32), /* fp_div_df */
892 COSTS_N_INSNS (5), /* int_mult_si */
893 COSTS_N_INSNS (5), /* int_mult_di */
894 COSTS_N_INSNS (41), /* int_div_si */
895 COSTS_N_INSNS (41), /* int_div_di */
896 1, /* branch_cost */
897 4 /* memory_latency */
898 },
899 { /* 74KC */
900 SOFT_FP_COSTS,
901 COSTS_N_INSNS (5), /* int_mult_si */
902 COSTS_N_INSNS (5), /* int_mult_di */
903 COSTS_N_INSNS (41), /* int_div_si */
904 COSTS_N_INSNS (41), /* int_div_di */
905 1, /* branch_cost */
906 4 /* memory_latency */
907 },
908 { /* 74KF2_1 */
909 COSTS_N_INSNS (8), /* fp_add */
910 COSTS_N_INSNS (8), /* fp_mult_sf */
911 COSTS_N_INSNS (10), /* fp_mult_df */
912 COSTS_N_INSNS (34), /* fp_div_sf */
913 COSTS_N_INSNS (64), /* fp_div_df */
914 COSTS_N_INSNS (5), /* int_mult_si */
915 COSTS_N_INSNS (5), /* int_mult_di */
916 COSTS_N_INSNS (41), /* int_div_si */
917 COSTS_N_INSNS (41), /* int_div_di */
918 1, /* branch_cost */
919 4 /* memory_latency */
920 },
921 { /* 74KF1_1 */
922 COSTS_N_INSNS (4), /* fp_add */
923 COSTS_N_INSNS (4), /* fp_mult_sf */
924 COSTS_N_INSNS (5), /* fp_mult_df */
925 COSTS_N_INSNS (17), /* fp_div_sf */
926 COSTS_N_INSNS (32), /* fp_div_df */
927 COSTS_N_INSNS (5), /* int_mult_si */
928 COSTS_N_INSNS (5), /* int_mult_di */
929 COSTS_N_INSNS (41), /* int_div_si */
930 COSTS_N_INSNS (41), /* int_div_di */
931 1, /* branch_cost */
932 4 /* memory_latency */
933 },
934 { /* 74KF3_2 */
935 COSTS_N_INSNS (6), /* fp_add */
936 COSTS_N_INSNS (6), /* fp_mult_sf */
937 COSTS_N_INSNS (7), /* fp_mult_df */
938 COSTS_N_INSNS (25), /* fp_div_sf */
939 COSTS_N_INSNS (48), /* fp_div_df */
940 COSTS_N_INSNS (5), /* int_mult_si */
941 COSTS_N_INSNS (5), /* int_mult_di */
942 COSTS_N_INSNS (41), /* int_div_si */
943 COSTS_N_INSNS (41), /* int_div_di */
944 1, /* branch_cost */
945 4 /* memory_latency */
946 },
947 { /* Loongson-2E */
948 DEFAULT_COSTS
949 },
950 { /* Loongson-2F */
951 DEFAULT_COSTS
952 },
953 { /* Loongson-3A */
954 DEFAULT_COSTS
955 },
956 { /* M4k */
957 DEFAULT_COSTS
958 },
959 /* Octeon */
960 {
961 SOFT_FP_COSTS,
962 COSTS_N_INSNS (5), /* int_mult_si */
963 COSTS_N_INSNS (5), /* int_mult_di */
964 COSTS_N_INSNS (72), /* int_div_si */
965 COSTS_N_INSNS (72), /* int_div_di */
966 1, /* branch_cost */
967 4 /* memory_latency */
968 },
969 /* Octeon II */
970 {
971 SOFT_FP_COSTS,
972 COSTS_N_INSNS (6), /* int_mult_si */
973 COSTS_N_INSNS (6), /* int_mult_di */
974 COSTS_N_INSNS (18), /* int_div_si */
975 COSTS_N_INSNS (35), /* int_div_di */
976 4, /* branch_cost */
977 4 /* memory_latency */
978 },
979 { /* R3900 */
980 COSTS_N_INSNS (2), /* fp_add */
981 COSTS_N_INSNS (4), /* fp_mult_sf */
982 COSTS_N_INSNS (5), /* fp_mult_df */
983 COSTS_N_INSNS (12), /* fp_div_sf */
984 COSTS_N_INSNS (19), /* fp_div_df */
985 COSTS_N_INSNS (2), /* int_mult_si */
986 COSTS_N_INSNS (2), /* int_mult_di */
987 COSTS_N_INSNS (35), /* int_div_si */
988 COSTS_N_INSNS (35), /* int_div_di */
989 1, /* branch_cost */
990 4 /* memory_latency */
991 },
992 { /* R6000 */
993 COSTS_N_INSNS (3), /* fp_add */
994 COSTS_N_INSNS (5), /* fp_mult_sf */
995 COSTS_N_INSNS (6), /* fp_mult_df */
996 COSTS_N_INSNS (15), /* fp_div_sf */
997 COSTS_N_INSNS (16), /* fp_div_df */
998 COSTS_N_INSNS (17), /* int_mult_si */
999 COSTS_N_INSNS (17), /* int_mult_di */
1000 COSTS_N_INSNS (38), /* int_div_si */
1001 COSTS_N_INSNS (38), /* int_div_di */
1002 2, /* branch_cost */
1003 6 /* memory_latency */
1004 },
1005 { /* R4000 */
1006 COSTS_N_INSNS (6), /* fp_add */
1007 COSTS_N_INSNS (7), /* fp_mult_sf */
1008 COSTS_N_INSNS (8), /* fp_mult_df */
1009 COSTS_N_INSNS (23), /* fp_div_sf */
1010 COSTS_N_INSNS (36), /* fp_div_df */
1011 COSTS_N_INSNS (10), /* int_mult_si */
1012 COSTS_N_INSNS (10), /* int_mult_di */
1013 COSTS_N_INSNS (69), /* int_div_si */
1014 COSTS_N_INSNS (69), /* int_div_di */
1015 2, /* branch_cost */
1016 6 /* memory_latency */
1017 },
1018 { /* R4100 */
1019 DEFAULT_COSTS
1020 },
1021 { /* R4111 */
1022 DEFAULT_COSTS
1023 },
1024 { /* R4120 */
1025 DEFAULT_COSTS
1026 },
1027 { /* R4130 */
1028 /* The only costs that appear to be updated here are
1029 integer multiplication. */
1030 SOFT_FP_COSTS,
1031 COSTS_N_INSNS (4), /* int_mult_si */
1032 COSTS_N_INSNS (6), /* int_mult_di */
1033 COSTS_N_INSNS (69), /* int_div_si */
1034 COSTS_N_INSNS (69), /* int_div_di */
1035 1, /* branch_cost */
1036 4 /* memory_latency */
1037 },
1038 { /* R4300 */
1039 DEFAULT_COSTS
1040 },
1041 { /* R4600 */
1042 DEFAULT_COSTS
1043 },
1044 { /* R4650 */
1045 DEFAULT_COSTS
1046 },
1047 { /* R4700 */
1048 DEFAULT_COSTS
1049 },
1050 { /* R5000 */
1051 COSTS_N_INSNS (6), /* fp_add */
1052 COSTS_N_INSNS (4), /* fp_mult_sf */
1053 COSTS_N_INSNS (5), /* fp_mult_df */
1054 COSTS_N_INSNS (23), /* fp_div_sf */
1055 COSTS_N_INSNS (36), /* fp_div_df */
1056 COSTS_N_INSNS (5), /* int_mult_si */
1057 COSTS_N_INSNS (5), /* int_mult_di */
1058 COSTS_N_INSNS (36), /* int_div_si */
1059 COSTS_N_INSNS (36), /* int_div_di */
1060 1, /* branch_cost */
1061 4 /* memory_latency */
1062 },
1063 { /* R5400 */
1064 COSTS_N_INSNS (6), /* fp_add */
1065 COSTS_N_INSNS (5), /* fp_mult_sf */
1066 COSTS_N_INSNS (6), /* fp_mult_df */
1067 COSTS_N_INSNS (30), /* fp_div_sf */
1068 COSTS_N_INSNS (59), /* fp_div_df */
1069 COSTS_N_INSNS (3), /* int_mult_si */
1070 COSTS_N_INSNS (4), /* int_mult_di */
1071 COSTS_N_INSNS (42), /* int_div_si */
1072 COSTS_N_INSNS (74), /* int_div_di */
1073 1, /* branch_cost */
1074 4 /* memory_latency */
1075 },
1076 { /* R5500 */
1077 COSTS_N_INSNS (6), /* fp_add */
1078 COSTS_N_INSNS (5), /* fp_mult_sf */
1079 COSTS_N_INSNS (6), /* fp_mult_df */
1080 COSTS_N_INSNS (30), /* fp_div_sf */
1081 COSTS_N_INSNS (59), /* fp_div_df */
1082 COSTS_N_INSNS (5), /* int_mult_si */
1083 COSTS_N_INSNS (9), /* int_mult_di */
1084 COSTS_N_INSNS (42), /* int_div_si */
1085 COSTS_N_INSNS (74), /* int_div_di */
1086 1, /* branch_cost */
1087 4 /* memory_latency */
1088 },
1089 { /* R5900 */
1090 COSTS_N_INSNS (4), /* fp_add */
1091 COSTS_N_INSNS (4), /* fp_mult_sf */
1092 COSTS_N_INSNS (256), /* fp_mult_df */
1093 COSTS_N_INSNS (8), /* fp_div_sf */
1094 COSTS_N_INSNS (256), /* fp_div_df */
1095 COSTS_N_INSNS (4), /* int_mult_si */
1096 COSTS_N_INSNS (256), /* int_mult_di */
1097 COSTS_N_INSNS (37), /* int_div_si */
1098 COSTS_N_INSNS (256), /* int_div_di */
1099 1, /* branch_cost */
1100 4 /* memory_latency */
1101 },
1102 { /* R7000 */
1103 /* The only costs that are changed here are
1104 integer multiplication. */
1105 COSTS_N_INSNS (6), /* fp_add */
1106 COSTS_N_INSNS (7), /* fp_mult_sf */
1107 COSTS_N_INSNS (8), /* fp_mult_df */
1108 COSTS_N_INSNS (23), /* fp_div_sf */
1109 COSTS_N_INSNS (36), /* fp_div_df */
1110 COSTS_N_INSNS (5), /* int_mult_si */
1111 COSTS_N_INSNS (9), /* int_mult_di */
1112 COSTS_N_INSNS (69), /* int_div_si */
1113 COSTS_N_INSNS (69), /* int_div_di */
1114 1, /* branch_cost */
1115 4 /* memory_latency */
1116 },
1117 { /* R8000 */
1118 DEFAULT_COSTS
1119 },
1120 { /* R9000 */
1121 /* The only costs that are changed here are
1122 integer multiplication. */
1123 COSTS_N_INSNS (6), /* fp_add */
1124 COSTS_N_INSNS (7), /* fp_mult_sf */
1125 COSTS_N_INSNS (8), /* fp_mult_df */
1126 COSTS_N_INSNS (23), /* fp_div_sf */
1127 COSTS_N_INSNS (36), /* fp_div_df */
1128 COSTS_N_INSNS (3), /* int_mult_si */
1129 COSTS_N_INSNS (8), /* int_mult_di */
1130 COSTS_N_INSNS (69), /* int_div_si */
1131 COSTS_N_INSNS (69), /* int_div_di */
1132 1, /* branch_cost */
1133 4 /* memory_latency */
1134 },
1135 { /* R1x000 */
1136 COSTS_N_INSNS (2), /* fp_add */
1137 COSTS_N_INSNS (2), /* fp_mult_sf */
1138 COSTS_N_INSNS (2), /* fp_mult_df */
1139 COSTS_N_INSNS (12), /* fp_div_sf */
1140 COSTS_N_INSNS (19), /* fp_div_df */
1141 COSTS_N_INSNS (5), /* int_mult_si */
1142 COSTS_N_INSNS (9), /* int_mult_di */
1143 COSTS_N_INSNS (34), /* int_div_si */
1144 COSTS_N_INSNS (66), /* int_div_di */
1145 1, /* branch_cost */
1146 4 /* memory_latency */
1147 },
1148 { /* SB1 */
1149 /* These costs are the same as the SB-1A below. */
1150 COSTS_N_INSNS (4), /* fp_add */
1151 COSTS_N_INSNS (4), /* fp_mult_sf */
1152 COSTS_N_INSNS (4), /* fp_mult_df */
1153 COSTS_N_INSNS (24), /* fp_div_sf */
1154 COSTS_N_INSNS (32), /* fp_div_df */
1155 COSTS_N_INSNS (3), /* int_mult_si */
1156 COSTS_N_INSNS (4), /* int_mult_di */
1157 COSTS_N_INSNS (36), /* int_div_si */
1158 COSTS_N_INSNS (68), /* int_div_di */
1159 1, /* branch_cost */
1160 4 /* memory_latency */
1161 },
1162 { /* SB1-A */
1163 /* These costs are the same as the SB-1 above. */
1164 COSTS_N_INSNS (4), /* fp_add */
1165 COSTS_N_INSNS (4), /* fp_mult_sf */
1166 COSTS_N_INSNS (4), /* fp_mult_df */
1167 COSTS_N_INSNS (24), /* fp_div_sf */
1168 COSTS_N_INSNS (32), /* fp_div_df */
1169 COSTS_N_INSNS (3), /* int_mult_si */
1170 COSTS_N_INSNS (4), /* int_mult_di */
1171 COSTS_N_INSNS (36), /* int_div_si */
1172 COSTS_N_INSNS (68), /* int_div_di */
1173 1, /* branch_cost */
1174 4 /* memory_latency */
1175 },
1176 { /* SR71000 */
1177 DEFAULT_COSTS
1178 },
1179 { /* XLR */
1180 SOFT_FP_COSTS,
1181 COSTS_N_INSNS (8), /* int_mult_si */
1182 COSTS_N_INSNS (8), /* int_mult_di */
1183 COSTS_N_INSNS (72), /* int_div_si */
1184 COSTS_N_INSNS (72), /* int_div_di */
1185 1, /* branch_cost */
1186 4 /* memory_latency */
1187 },
1188 { /* XLP */
1189 /* These costs are the same as 5KF above. */
1190 COSTS_N_INSNS (4), /* fp_add */
1191 COSTS_N_INSNS (4), /* fp_mult_sf */
1192 COSTS_N_INSNS (5), /* fp_mult_df */
1193 COSTS_N_INSNS (17), /* fp_div_sf */
1194 COSTS_N_INSNS (32), /* fp_div_df */
1195 COSTS_N_INSNS (4), /* int_mult_si */
1196 COSTS_N_INSNS (11), /* int_mult_di */
1197 COSTS_N_INSNS (36), /* int_div_si */
1198 COSTS_N_INSNS (68), /* int_div_di */
1199 1, /* branch_cost */
1200 4 /* memory_latency */
1201 },
1202 { /* P5600 */
1203 COSTS_N_INSNS (4), /* fp_add */
1204 COSTS_N_INSNS (5), /* fp_mult_sf */
1205 COSTS_N_INSNS (5), /* fp_mult_df */
1206 COSTS_N_INSNS (17), /* fp_div_sf */
1207 COSTS_N_INSNS (17), /* fp_div_df */
1208 COSTS_N_INSNS (5), /* int_mult_si */
1209 COSTS_N_INSNS (5), /* int_mult_di */
1210 COSTS_N_INSNS (8), /* int_div_si */
1211 COSTS_N_INSNS (8), /* int_div_di */
1212 2, /* branch_cost */
1213 10 /* memory_latency */
1214 }
1215 };
1216 \f
1217 static rtx mips_find_pic_call_symbol (rtx_insn *, rtx, bool);
1218 static int mips_register_move_cost (machine_mode, reg_class_t,
1219 reg_class_t);
1220 static unsigned int mips_function_arg_boundary (machine_mode, const_tree);
1221 \f
1222 struct mips16_flip_traits : default_hashmap_traits
1223 {
1224 static hashval_t hash (const char *s) { return htab_hash_string (s); }
1225 static bool
1226 equal_keys (const char *a, const char *b)
1227 {
1228 return !strcmp (a, b);
1229 }
1230 };
1231
1232 /* This hash table keeps track of implicit "mips16" and "nomips16" attributes
1233 for -mflip_mips16. It maps decl names onto a boolean mode setting. */
1234 static GTY (()) hash_map<const char *, bool, mips16_flip_traits> *
1235 mflip_mips16_htab;
1236
1237 /* True if -mflip-mips16 should next add an attribute for the default MIPS16
1238 mode, false if it should next add an attribute for the opposite mode. */
1239 static GTY(()) bool mips16_flipper;
1240
1241 /* DECL is a function that needs a default "mips16" or "nomips16" attribute
1242 for -mflip-mips16. Return true if it should use "mips16" and false if
1243 it should use "nomips16". */
1244
1245 static bool
1246 mflip_mips16_use_mips16_p (tree decl)
1247 {
1248 const char *name;
1249 bool base_is_mips16 = (mips_base_compression_flags & MASK_MIPS16) != 0;
1250
1251 /* Use the opposite of the command-line setting for anonymous decls. */
1252 if (!DECL_NAME (decl))
1253 return !base_is_mips16;
1254
1255 if (!mflip_mips16_htab)
1256 mflip_mips16_htab
1257 = hash_map<const char *, bool, mips16_flip_traits>::create_ggc (37);
1258
1259 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1260
1261 bool existed;
1262 bool *slot = &mflip_mips16_htab->get_or_insert (name, &existed);
1263 if (!existed)
1264 {
1265 mips16_flipper = !mips16_flipper;
1266 *slot = mips16_flipper ? !base_is_mips16 : base_is_mips16;
1267 }
1268 return *slot;
1269 }
1270 \f
1271 /* Predicates to test for presence of "near" and "far"/"long_call"
1272 attributes on the given TYPE. */
1273
1274 static bool
1275 mips_near_type_p (const_tree type)
1276 {
1277 return lookup_attribute ("near", TYPE_ATTRIBUTES (type)) != NULL;
1278 }
1279
1280 static bool
1281 mips_far_type_p (const_tree type)
1282 {
1283 return (lookup_attribute ("long_call", TYPE_ATTRIBUTES (type)) != NULL
1284 || lookup_attribute ("far", TYPE_ATTRIBUTES (type)) != NULL);
1285 }
1286
1287
1288 /* Check if the interrupt attribute is set for a function. */
1289
1290 static bool
1291 mips_interrupt_type_p (tree type)
1292 {
1293 return lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type)) != NULL;
1294 }
1295
1296 /* Check if the attribute to use shadow register set is set for a function. */
1297
1298 static bool
1299 mips_use_shadow_register_set_p (tree type)
1300 {
1301 return lookup_attribute ("use_shadow_register_set",
1302 TYPE_ATTRIBUTES (type)) != NULL;
1303 }
1304
1305 /* Check if the attribute to keep interrupts masked is set for a function. */
1306
1307 static bool
1308 mips_keep_interrupts_masked_p (tree type)
1309 {
1310 return lookup_attribute ("keep_interrupts_masked",
1311 TYPE_ATTRIBUTES (type)) != NULL;
1312 }
1313
1314 /* Check if the attribute to use debug exception return is set for
1315 a function. */
1316
1317 static bool
1318 mips_use_debug_exception_return_p (tree type)
1319 {
1320 return lookup_attribute ("use_debug_exception_return",
1321 TYPE_ATTRIBUTES (type)) != NULL;
1322 }
1323
1324 /* Return the set of compression modes that are explicitly required
1325 by the attributes in ATTRIBUTES. */
1326
1327 static unsigned int
1328 mips_get_compress_on_flags (tree attributes)
1329 {
1330 unsigned int flags = 0;
1331
1332 if (lookup_attribute ("mips16", attributes) != NULL)
1333 flags |= MASK_MIPS16;
1334
1335 if (lookup_attribute ("micromips", attributes) != NULL)
1336 flags |= MASK_MICROMIPS;
1337
1338 return flags;
1339 }
1340
1341 /* Return the set of compression modes that are explicitly forbidden
1342 by the attributes in ATTRIBUTES. */
1343
1344 static unsigned int
1345 mips_get_compress_off_flags (tree attributes)
1346 {
1347 unsigned int flags = 0;
1348
1349 if (lookup_attribute ("nocompression", attributes) != NULL)
1350 flags |= MASK_MIPS16 | MASK_MICROMIPS;
1351
1352 if (lookup_attribute ("nomips16", attributes) != NULL)
1353 flags |= MASK_MIPS16;
1354
1355 if (lookup_attribute ("nomicromips", attributes) != NULL)
1356 flags |= MASK_MICROMIPS;
1357
1358 return flags;
1359 }
1360
1361 /* Return the compression mode that should be used for function DECL.
1362 Return the ambient setting if DECL is null. */
1363
1364 static unsigned int
1365 mips_get_compress_mode (tree decl)
1366 {
1367 unsigned int flags, force_on;
1368
1369 flags = mips_base_compression_flags;
1370 if (decl)
1371 {
1372 /* Nested functions must use the same frame pointer as their
1373 parent and must therefore use the same ISA mode. */
1374 tree parent = decl_function_context (decl);
1375 if (parent)
1376 decl = parent;
1377 force_on = mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1378 if (force_on)
1379 return force_on;
1380 flags &= ~mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1381 }
1382 return flags;
1383 }
1384
1385 /* Return the attribute name associated with MASK_MIPS16 and MASK_MICROMIPS
1386 flags FLAGS. */
1387
1388 static const char *
1389 mips_get_compress_on_name (unsigned int flags)
1390 {
1391 if (flags == MASK_MIPS16)
1392 return "mips16";
1393 return "micromips";
1394 }
1395
1396 /* Return the attribute name that forbids MASK_MIPS16 and MASK_MICROMIPS
1397 flags FLAGS. */
1398
1399 static const char *
1400 mips_get_compress_off_name (unsigned int flags)
1401 {
1402 if (flags == MASK_MIPS16)
1403 return "nomips16";
1404 if (flags == MASK_MICROMIPS)
1405 return "nomicromips";
1406 return "nocompression";
1407 }
1408
1409 /* Implement TARGET_COMP_TYPE_ATTRIBUTES. */
1410
1411 static int
1412 mips_comp_type_attributes (const_tree type1, const_tree type2)
1413 {
1414 /* Disallow mixed near/far attributes. */
1415 if (mips_far_type_p (type1) && mips_near_type_p (type2))
1416 return 0;
1417 if (mips_near_type_p (type1) && mips_far_type_p (type2))
1418 return 0;
1419 return 1;
1420 }
1421
1422 /* Implement TARGET_INSERT_ATTRIBUTES. */
1423
1424 static void
1425 mips_insert_attributes (tree decl, tree *attributes)
1426 {
1427 const char *name;
1428 unsigned int compression_flags, nocompression_flags;
1429
1430 /* Check for "mips16" and "nomips16" attributes. */
1431 compression_flags = mips_get_compress_on_flags (*attributes);
1432 nocompression_flags = mips_get_compress_off_flags (*attributes);
1433
1434 if (TREE_CODE (decl) != FUNCTION_DECL)
1435 {
1436 if (nocompression_flags)
1437 error ("%qs attribute only applies to functions",
1438 mips_get_compress_off_name (nocompression_flags));
1439
1440 if (compression_flags)
1441 error ("%qs attribute only applies to functions",
1442 mips_get_compress_on_name (nocompression_flags));
1443 }
1444 else
1445 {
1446 compression_flags |= mips_get_compress_on_flags (DECL_ATTRIBUTES (decl));
1447 nocompression_flags |=
1448 mips_get_compress_off_flags (DECL_ATTRIBUTES (decl));
1449
1450 if (compression_flags && nocompression_flags)
1451 error ("%qE cannot have both %qs and %qs attributes",
1452 DECL_NAME (decl), mips_get_compress_on_name (compression_flags),
1453 mips_get_compress_off_name (nocompression_flags));
1454
1455 if (compression_flags & MASK_MIPS16
1456 && compression_flags & MASK_MICROMIPS)
1457 error ("%qE cannot have both %qs and %qs attributes",
1458 DECL_NAME (decl), "mips16", "micromips");
1459
1460 if (TARGET_FLIP_MIPS16
1461 && !DECL_ARTIFICIAL (decl)
1462 && compression_flags == 0
1463 && nocompression_flags == 0)
1464 {
1465 /* Implement -mflip-mips16. If DECL has neither a "nomips16" nor a
1466 "mips16" attribute, arbitrarily pick one. We must pick the same
1467 setting for duplicate declarations of a function. */
1468 name = mflip_mips16_use_mips16_p (decl) ? "mips16" : "nomips16";
1469 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1470 name = "nomicromips";
1471 *attributes = tree_cons (get_identifier (name), NULL, *attributes);
1472 }
1473 }
1474 }
1475
1476 /* Implement TARGET_MERGE_DECL_ATTRIBUTES. */
1477
1478 static tree
1479 mips_merge_decl_attributes (tree olddecl, tree newdecl)
1480 {
1481 unsigned int diff;
1482
1483 diff = (mips_get_compress_on_flags (DECL_ATTRIBUTES (olddecl))
1484 ^ mips_get_compress_on_flags (DECL_ATTRIBUTES (newdecl)));
1485 if (diff)
1486 error ("%qE redeclared with conflicting %qs attributes",
1487 DECL_NAME (newdecl), mips_get_compress_on_name (diff));
1488
1489 diff = (mips_get_compress_off_flags (DECL_ATTRIBUTES (olddecl))
1490 ^ mips_get_compress_off_flags (DECL_ATTRIBUTES (newdecl)));
1491 if (diff)
1492 error ("%qE redeclared with conflicting %qs attributes",
1493 DECL_NAME (newdecl), mips_get_compress_off_name (diff));
1494
1495 return merge_attributes (DECL_ATTRIBUTES (olddecl),
1496 DECL_ATTRIBUTES (newdecl));
1497 }
1498
1499 /* Implement TARGET_CAN_INLINE_P. */
1500
1501 static bool
1502 mips_can_inline_p (tree caller, tree callee)
1503 {
1504 if (mips_get_compress_mode (callee) != mips_get_compress_mode (caller))
1505 return false;
1506 return default_target_can_inline_p (caller, callee);
1507 }
1508 \f
1509 /* If X is a PLUS of a CONST_INT, return the two terms in *BASE_PTR
1510 and *OFFSET_PTR. Return X in *BASE_PTR and 0 in *OFFSET_PTR otherwise. */
1511
1512 static void
1513 mips_split_plus (rtx x, rtx *base_ptr, HOST_WIDE_INT *offset_ptr)
1514 {
1515 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
1516 {
1517 *base_ptr = XEXP (x, 0);
1518 *offset_ptr = INTVAL (XEXP (x, 1));
1519 }
1520 else
1521 {
1522 *base_ptr = x;
1523 *offset_ptr = 0;
1524 }
1525 }
1526 \f
1527 static unsigned int mips_build_integer (struct mips_integer_op *,
1528 unsigned HOST_WIDE_INT);
1529
1530 /* A subroutine of mips_build_integer, with the same interface.
1531 Assume that the final action in the sequence should be a left shift. */
1532
1533 static unsigned int
1534 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
1535 {
1536 unsigned int i, shift;
1537
1538 /* Shift VALUE right until its lowest bit is set. Shift arithmetically
1539 since signed numbers are easier to load than unsigned ones. */
1540 shift = 0;
1541 while ((value & 1) == 0)
1542 value /= 2, shift++;
1543
1544 i = mips_build_integer (codes, value);
1545 codes[i].code = ASHIFT;
1546 codes[i].value = shift;
1547 return i + 1;
1548 }
1549
1550 /* As for mips_build_shift, but assume that the final action will be
1551 an IOR or PLUS operation. */
1552
1553 static unsigned int
1554 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
1555 {
1556 unsigned HOST_WIDE_INT high;
1557 unsigned int i;
1558
1559 high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
1560 if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
1561 {
1562 /* The constant is too complex to load with a simple LUI/ORI pair,
1563 so we want to give the recursive call as many trailing zeros as
1564 possible. In this case, we know bit 16 is set and that the
1565 low 16 bits form a negative number. If we subtract that number
1566 from VALUE, we will clear at least the lowest 17 bits, maybe more. */
1567 i = mips_build_integer (codes, CONST_HIGH_PART (value));
1568 codes[i].code = PLUS;
1569 codes[i].value = CONST_LOW_PART (value);
1570 }
1571 else
1572 {
1573 /* Either this is a simple LUI/ORI pair, or clearing the lowest 16
1574 bits gives a value with at least 17 trailing zeros. */
1575 i = mips_build_integer (codes, high);
1576 codes[i].code = IOR;
1577 codes[i].value = value & 0xffff;
1578 }
1579 return i + 1;
1580 }
1581
1582 /* Fill CODES with a sequence of rtl operations to load VALUE.
1583 Return the number of operations needed. */
1584
1585 static unsigned int
1586 mips_build_integer (struct mips_integer_op *codes,
1587 unsigned HOST_WIDE_INT value)
1588 {
1589 if (SMALL_OPERAND (value)
1590 || SMALL_OPERAND_UNSIGNED (value)
1591 || LUI_OPERAND (value))
1592 {
1593 /* The value can be loaded with a single instruction. */
1594 codes[0].code = UNKNOWN;
1595 codes[0].value = value;
1596 return 1;
1597 }
1598 else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
1599 {
1600 /* Either the constant is a simple LUI/ORI combination or its
1601 lowest bit is set. We don't want to shift in this case. */
1602 return mips_build_lower (codes, value);
1603 }
1604 else if ((value & 0xffff) == 0)
1605 {
1606 /* The constant will need at least three actions. The lowest
1607 16 bits are clear, so the final action will be a shift. */
1608 return mips_build_shift (codes, value);
1609 }
1610 else
1611 {
1612 /* The final action could be a shift, add or inclusive OR.
1613 Rather than use a complex condition to select the best
1614 approach, try both mips_build_shift and mips_build_lower
1615 and pick the one that gives the shortest sequence.
1616 Note that this case is only used once per constant. */
1617 struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
1618 unsigned int cost, alt_cost;
1619
1620 cost = mips_build_shift (codes, value);
1621 alt_cost = mips_build_lower (alt_codes, value);
1622 if (alt_cost < cost)
1623 {
1624 memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
1625 cost = alt_cost;
1626 }
1627 return cost;
1628 }
1629 }
1630 \f
1631 /* Implement TARGET_LEGITIMATE_CONSTANT_P. */
1632
1633 static bool
1634 mips_legitimate_constant_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x)
1635 {
1636 return mips_const_insns (x) > 0;
1637 }
1638 \f
1639 /* Return a SYMBOL_REF for a MIPS16 function called NAME. */
1640
1641 static rtx
1642 mips16_stub_function (const char *name)
1643 {
1644 rtx x;
1645
1646 x = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
1647 SYMBOL_REF_FLAGS (x) |= (SYMBOL_FLAG_EXTERNAL | SYMBOL_FLAG_FUNCTION);
1648 return x;
1649 }
1650
1651 /* Return a legitimate call address for STUB, given that STUB is a MIPS16
1652 support function. */
1653
1654 static rtx
1655 mips16_stub_call_address (mips_one_only_stub *stub)
1656 {
1657 rtx fn = mips16_stub_function (stub->get_name ());
1658 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_LOCAL;
1659 if (!call_insn_operand (fn, VOIDmode))
1660 fn = force_reg (Pmode, fn);
1661 return fn;
1662 }
1663 \f
1664 /* A stub for moving the thread pointer into TLS_GET_TP_REGNUM. */
1665
1666 class mips16_rdhwr_one_only_stub : public mips_one_only_stub
1667 {
1668 virtual const char *get_name ();
1669 virtual void output_body ();
1670 };
1671
1672 const char *
1673 mips16_rdhwr_one_only_stub::get_name ()
1674 {
1675 return "__mips16_rdhwr";
1676 }
1677
1678 void
1679 mips16_rdhwr_one_only_stub::output_body ()
1680 {
1681 fprintf (asm_out_file,
1682 "\t.set\tpush\n"
1683 "\t.set\tmips32r2\n"
1684 "\t.set\tnoreorder\n"
1685 "\trdhwr\t$3,$29\n"
1686 "\t.set\tpop\n"
1687 "\tj\t$31\n");
1688 }
1689
1690 /* A stub for moving the FCSR into GET_FCSR_REGNUM. */
1691 class mips16_get_fcsr_one_only_stub : public mips_one_only_stub
1692 {
1693 virtual const char *get_name ();
1694 virtual void output_body ();
1695 };
1696
1697 const char *
1698 mips16_get_fcsr_one_only_stub::get_name ()
1699 {
1700 return "__mips16_get_fcsr";
1701 }
1702
1703 void
1704 mips16_get_fcsr_one_only_stub::output_body ()
1705 {
1706 fprintf (asm_out_file,
1707 "\tcfc1\t%s,$31\n"
1708 "\tj\t$31\n", reg_names[GET_FCSR_REGNUM]);
1709 }
1710
1711 /* A stub for moving SET_FCSR_REGNUM into the FCSR. */
1712 class mips16_set_fcsr_one_only_stub : public mips_one_only_stub
1713 {
1714 virtual const char *get_name ();
1715 virtual void output_body ();
1716 };
1717
1718 const char *
1719 mips16_set_fcsr_one_only_stub::get_name ()
1720 {
1721 return "__mips16_set_fcsr";
1722 }
1723
1724 void
1725 mips16_set_fcsr_one_only_stub::output_body ()
1726 {
1727 fprintf (asm_out_file,
1728 "\tctc1\t%s,$31\n"
1729 "\tj\t$31\n", reg_names[SET_FCSR_REGNUM]);
1730 }
1731 \f
1732 /* Return true if symbols of type TYPE require a GOT access. */
1733
1734 static bool
1735 mips_got_symbol_type_p (enum mips_symbol_type type)
1736 {
1737 switch (type)
1738 {
1739 case SYMBOL_GOT_PAGE_OFST:
1740 case SYMBOL_GOT_DISP:
1741 return true;
1742
1743 default:
1744 return false;
1745 }
1746 }
1747
1748 /* Return true if X is a thread-local symbol. */
1749
1750 static bool
1751 mips_tls_symbol_p (rtx x)
1752 {
1753 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1754 }
1755
1756 /* Return true if SYMBOL_REF X is associated with a global symbol
1757 (in the STB_GLOBAL sense). */
1758
1759 static bool
1760 mips_global_symbol_p (const_rtx x)
1761 {
1762 const_tree decl = SYMBOL_REF_DECL (x);
1763
1764 if (!decl)
1765 return !SYMBOL_REF_LOCAL_P (x) || SYMBOL_REF_EXTERNAL_P (x);
1766
1767 /* Weakref symbols are not TREE_PUBLIC, but their targets are global
1768 or weak symbols. Relocations in the object file will be against
1769 the target symbol, so it's that symbol's binding that matters here. */
1770 return DECL_P (decl) && (TREE_PUBLIC (decl) || DECL_WEAK (decl));
1771 }
1772
1773 /* Return true if function X is a libgcc MIPS16 stub function. */
1774
1775 static bool
1776 mips16_stub_function_p (const_rtx x)
1777 {
1778 return (GET_CODE (x) == SYMBOL_REF
1779 && strncmp (XSTR (x, 0), "__mips16_", 9) == 0);
1780 }
1781
1782 /* Return true if function X is a locally-defined and locally-binding
1783 MIPS16 function. */
1784
1785 static bool
1786 mips16_local_function_p (const_rtx x)
1787 {
1788 return (GET_CODE (x) == SYMBOL_REF
1789 && SYMBOL_REF_LOCAL_P (x)
1790 && !SYMBOL_REF_EXTERNAL_P (x)
1791 && (mips_get_compress_mode (SYMBOL_REF_DECL (x)) & MASK_MIPS16));
1792 }
1793
1794 /* Return true if SYMBOL_REF X binds locally. */
1795
1796 static bool
1797 mips_symbol_binds_local_p (const_rtx x)
1798 {
1799 return (SYMBOL_REF_DECL (x)
1800 ? targetm.binds_local_p (SYMBOL_REF_DECL (x))
1801 : SYMBOL_REF_LOCAL_P (x));
1802 }
1803
1804 /* Return true if rtx constants of mode MODE should be put into a small
1805 data section. */
1806
1807 static bool
1808 mips_rtx_constant_in_small_data_p (machine_mode mode)
1809 {
1810 return (!TARGET_EMBEDDED_DATA
1811 && TARGET_LOCAL_SDATA
1812 && GET_MODE_SIZE (mode) <= mips_small_data_threshold);
1813 }
1814
1815 /* Return true if X should not be moved directly into register $25.
1816 We need this because many versions of GAS will treat "la $25,foo" as
1817 part of a call sequence and so allow a global "foo" to be lazily bound. */
1818
1819 bool
1820 mips_dangerous_for_la25_p (rtx x)
1821 {
1822 return (!TARGET_EXPLICIT_RELOCS
1823 && TARGET_USE_GOT
1824 && GET_CODE (x) == SYMBOL_REF
1825 && mips_global_symbol_p (x));
1826 }
1827
1828 /* Return true if calls to X might need $25 to be valid on entry. */
1829
1830 bool
1831 mips_use_pic_fn_addr_reg_p (const_rtx x)
1832 {
1833 if (!TARGET_USE_PIC_FN_ADDR_REG)
1834 return false;
1835
1836 /* MIPS16 stub functions are guaranteed not to use $25. */
1837 if (mips16_stub_function_p (x))
1838 return false;
1839
1840 if (GET_CODE (x) == SYMBOL_REF)
1841 {
1842 /* If PLTs and copy relocations are available, the static linker
1843 will make sure that $25 is valid on entry to the target function. */
1844 if (TARGET_ABICALLS_PIC0)
1845 return false;
1846
1847 /* Locally-defined functions use absolute accesses to set up
1848 the global pointer. */
1849 if (TARGET_ABSOLUTE_ABICALLS
1850 && mips_symbol_binds_local_p (x)
1851 && !SYMBOL_REF_EXTERNAL_P (x))
1852 return false;
1853 }
1854
1855 return true;
1856 }
1857
1858 /* Return the method that should be used to access SYMBOL_REF or
1859 LABEL_REF X in context CONTEXT. */
1860
1861 static enum mips_symbol_type
1862 mips_classify_symbol (const_rtx x, enum mips_symbol_context context)
1863 {
1864 if (TARGET_RTP_PIC)
1865 return SYMBOL_GOT_DISP;
1866
1867 if (GET_CODE (x) == LABEL_REF)
1868 {
1869 /* Only return SYMBOL_PC_RELATIVE if we are generating MIPS16
1870 code and if we know that the label is in the current function's
1871 text section. LABEL_REFs are used for jump tables as well as
1872 text labels, so we must check whether jump tables live in the
1873 text section. */
1874 if (TARGET_MIPS16_SHORT_JUMP_TABLES
1875 && !LABEL_REF_NONLOCAL_P (x))
1876 return SYMBOL_PC_RELATIVE;
1877
1878 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1879 return SYMBOL_GOT_PAGE_OFST;
1880
1881 return SYMBOL_ABSOLUTE;
1882 }
1883
1884 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1885
1886 if (SYMBOL_REF_TLS_MODEL (x))
1887 return SYMBOL_TLS;
1888
1889 if (CONSTANT_POOL_ADDRESS_P (x))
1890 {
1891 if (TARGET_MIPS16_TEXT_LOADS)
1892 return SYMBOL_PC_RELATIVE;
1893
1894 if (TARGET_MIPS16_PCREL_LOADS && context == SYMBOL_CONTEXT_MEM)
1895 return SYMBOL_PC_RELATIVE;
1896
1897 if (mips_rtx_constant_in_small_data_p (get_pool_mode (x)))
1898 return SYMBOL_GP_RELATIVE;
1899 }
1900
1901 /* Do not use small-data accesses for weak symbols; they may end up
1902 being zero. */
1903 if (TARGET_GPOPT && SYMBOL_REF_SMALL_P (x) && !SYMBOL_REF_WEAK (x))
1904 return SYMBOL_GP_RELATIVE;
1905
1906 /* Don't use GOT accesses for locally-binding symbols when -mno-shared
1907 is in effect. */
1908 if (TARGET_ABICALLS_PIC2
1909 && !(TARGET_ABSOLUTE_ABICALLS && mips_symbol_binds_local_p (x)))
1910 {
1911 /* There are three cases to consider:
1912
1913 - o32 PIC (either with or without explicit relocs)
1914 - n32/n64 PIC without explicit relocs
1915 - n32/n64 PIC with explicit relocs
1916
1917 In the first case, both local and global accesses will use an
1918 R_MIPS_GOT16 relocation. We must correctly predict which of
1919 the two semantics (local or global) the assembler and linker
1920 will apply. The choice depends on the symbol's binding rather
1921 than its visibility.
1922
1923 In the second case, the assembler will not use R_MIPS_GOT16
1924 relocations, but it chooses between local and global accesses
1925 in the same way as for o32 PIC.
1926
1927 In the third case we have more freedom since both forms of
1928 access will work for any kind of symbol. However, there seems
1929 little point in doing things differently. */
1930 if (mips_global_symbol_p (x))
1931 return SYMBOL_GOT_DISP;
1932
1933 return SYMBOL_GOT_PAGE_OFST;
1934 }
1935
1936 return SYMBOL_ABSOLUTE;
1937 }
1938
1939 /* Classify the base of symbolic expression X, given that X appears in
1940 context CONTEXT. */
1941
1942 static enum mips_symbol_type
1943 mips_classify_symbolic_expression (rtx x, enum mips_symbol_context context)
1944 {
1945 rtx offset;
1946
1947 split_const (x, &x, &offset);
1948 if (UNSPEC_ADDRESS_P (x))
1949 return UNSPEC_ADDRESS_TYPE (x);
1950
1951 return mips_classify_symbol (x, context);
1952 }
1953
1954 /* Return true if OFFSET is within the range [0, ALIGN), where ALIGN
1955 is the alignment in bytes of SYMBOL_REF X. */
1956
1957 static bool
1958 mips_offset_within_alignment_p (rtx x, HOST_WIDE_INT offset)
1959 {
1960 HOST_WIDE_INT align;
1961
1962 align = SYMBOL_REF_DECL (x) ? DECL_ALIGN_UNIT (SYMBOL_REF_DECL (x)) : 1;
1963 return IN_RANGE (offset, 0, align - 1);
1964 }
1965
1966 /* Return true if X is a symbolic constant that can be used in context
1967 CONTEXT. If it is, store the type of the symbol in *SYMBOL_TYPE. */
1968
1969 bool
1970 mips_symbolic_constant_p (rtx x, enum mips_symbol_context context,
1971 enum mips_symbol_type *symbol_type)
1972 {
1973 rtx offset;
1974
1975 split_const (x, &x, &offset);
1976 if (UNSPEC_ADDRESS_P (x))
1977 {
1978 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1979 x = UNSPEC_ADDRESS (x);
1980 }
1981 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1982 {
1983 *symbol_type = mips_classify_symbol (x, context);
1984 if (*symbol_type == SYMBOL_TLS)
1985 return false;
1986 }
1987 else
1988 return false;
1989
1990 if (offset == const0_rtx)
1991 return true;
1992
1993 /* Check whether a nonzero offset is valid for the underlying
1994 relocations. */
1995 switch (*symbol_type)
1996 {
1997 case SYMBOL_ABSOLUTE:
1998 case SYMBOL_64_HIGH:
1999 case SYMBOL_64_MID:
2000 case SYMBOL_64_LOW:
2001 /* If the target has 64-bit pointers and the object file only
2002 supports 32-bit symbols, the values of those symbols will be
2003 sign-extended. In this case we can't allow an arbitrary offset
2004 in case the 32-bit value X + OFFSET has a different sign from X. */
2005 if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
2006 return offset_within_block_p (x, INTVAL (offset));
2007
2008 /* In other cases the relocations can handle any offset. */
2009 return true;
2010
2011 case SYMBOL_PC_RELATIVE:
2012 /* Allow constant pool references to be converted to LABEL+CONSTANT.
2013 In this case, we no longer have access to the underlying constant,
2014 but the original symbol-based access was known to be valid. */
2015 if (GET_CODE (x) == LABEL_REF)
2016 return true;
2017
2018 /* Fall through. */
2019
2020 case SYMBOL_GP_RELATIVE:
2021 /* Make sure that the offset refers to something within the
2022 same object block. This should guarantee that the final
2023 PC- or GP-relative offset is within the 16-bit limit. */
2024 return offset_within_block_p (x, INTVAL (offset));
2025
2026 case SYMBOL_GOT_PAGE_OFST:
2027 case SYMBOL_GOTOFF_PAGE:
2028 /* If the symbol is global, the GOT entry will contain the symbol's
2029 address, and we will apply a 16-bit offset after loading it.
2030 If the symbol is local, the linker should provide enough local
2031 GOT entries for a 16-bit offset, but larger offsets may lead
2032 to GOT overflow. */
2033 return SMALL_INT (offset);
2034
2035 case SYMBOL_TPREL:
2036 case SYMBOL_DTPREL:
2037 /* There is no carry between the HI and LO REL relocations, so the
2038 offset is only valid if we know it won't lead to such a carry. */
2039 return mips_offset_within_alignment_p (x, INTVAL (offset));
2040
2041 case SYMBOL_GOT_DISP:
2042 case SYMBOL_GOTOFF_DISP:
2043 case SYMBOL_GOTOFF_CALL:
2044 case SYMBOL_GOTOFF_LOADGP:
2045 case SYMBOL_TLSGD:
2046 case SYMBOL_TLSLDM:
2047 case SYMBOL_GOTTPREL:
2048 case SYMBOL_TLS:
2049 case SYMBOL_HALF:
2050 return false;
2051 }
2052 gcc_unreachable ();
2053 }
2054 \f
2055 /* Like mips_symbol_insns, but treat extended MIPS16 instructions as a
2056 single instruction. We rely on the fact that, in the worst case,
2057 all instructions involved in a MIPS16 address calculation are usually
2058 extended ones. */
2059
2060 static int
2061 mips_symbol_insns_1 (enum mips_symbol_type type, machine_mode mode)
2062 {
2063 if (mips_use_pcrel_pool_p[(int) type])
2064 {
2065 if (mode == MAX_MACHINE_MODE)
2066 /* LEAs will be converted into constant-pool references by
2067 mips_reorg. */
2068 type = SYMBOL_PC_RELATIVE;
2069 else
2070 /* The constant must be loaded and then dereferenced. */
2071 return 0;
2072 }
2073
2074 switch (type)
2075 {
2076 case SYMBOL_ABSOLUTE:
2077 /* When using 64-bit symbols, we need 5 preparatory instructions,
2078 such as:
2079
2080 lui $at,%highest(symbol)
2081 daddiu $at,$at,%higher(symbol)
2082 dsll $at,$at,16
2083 daddiu $at,$at,%hi(symbol)
2084 dsll $at,$at,16
2085
2086 The final address is then $at + %lo(symbol). With 32-bit
2087 symbols we just need a preparatory LUI for normal mode and
2088 a preparatory LI and SLL for MIPS16. */
2089 return ABI_HAS_64BIT_SYMBOLS ? 6 : TARGET_MIPS16 ? 3 : 2;
2090
2091 case SYMBOL_GP_RELATIVE:
2092 /* Treat GP-relative accesses as taking a single instruction on
2093 MIPS16 too; the copy of $gp can often be shared. */
2094 return 1;
2095
2096 case SYMBOL_PC_RELATIVE:
2097 /* PC-relative constants can be only be used with ADDIUPC,
2098 DADDIUPC, LWPC and LDPC. */
2099 if (mode == MAX_MACHINE_MODE
2100 || GET_MODE_SIZE (mode) == 4
2101 || GET_MODE_SIZE (mode) == 8)
2102 return 1;
2103
2104 /* The constant must be loaded using ADDIUPC or DADDIUPC first. */
2105 return 0;
2106
2107 case SYMBOL_GOT_DISP:
2108 /* The constant will have to be loaded from the GOT before it
2109 is used in an address. */
2110 if (mode != MAX_MACHINE_MODE)
2111 return 0;
2112
2113 /* Fall through. */
2114
2115 case SYMBOL_GOT_PAGE_OFST:
2116 /* Unless -funit-at-a-time is in effect, we can't be sure whether the
2117 local/global classification is accurate. The worst cases are:
2118
2119 (1) For local symbols when generating o32 or o64 code. The assembler
2120 will use:
2121
2122 lw $at,%got(symbol)
2123 nop
2124
2125 ...and the final address will be $at + %lo(symbol).
2126
2127 (2) For global symbols when -mxgot. The assembler will use:
2128
2129 lui $at,%got_hi(symbol)
2130 (d)addu $at,$at,$gp
2131
2132 ...and the final address will be $at + %got_lo(symbol). */
2133 return 3;
2134
2135 case SYMBOL_GOTOFF_PAGE:
2136 case SYMBOL_GOTOFF_DISP:
2137 case SYMBOL_GOTOFF_CALL:
2138 case SYMBOL_GOTOFF_LOADGP:
2139 case SYMBOL_64_HIGH:
2140 case SYMBOL_64_MID:
2141 case SYMBOL_64_LOW:
2142 case SYMBOL_TLSGD:
2143 case SYMBOL_TLSLDM:
2144 case SYMBOL_DTPREL:
2145 case SYMBOL_GOTTPREL:
2146 case SYMBOL_TPREL:
2147 case SYMBOL_HALF:
2148 /* A 16-bit constant formed by a single relocation, or a 32-bit
2149 constant formed from a high 16-bit relocation and a low 16-bit
2150 relocation. Use mips_split_p to determine which. 32-bit
2151 constants need an "lui; addiu" sequence for normal mode and
2152 an "li; sll; addiu" sequence for MIPS16 mode. */
2153 return !mips_split_p[type] ? 1 : TARGET_MIPS16 ? 3 : 2;
2154
2155 case SYMBOL_TLS:
2156 /* We don't treat a bare TLS symbol as a constant. */
2157 return 0;
2158 }
2159 gcc_unreachable ();
2160 }
2161
2162 /* If MODE is MAX_MACHINE_MODE, return the number of instructions needed
2163 to load symbols of type TYPE into a register. Return 0 if the given
2164 type of symbol cannot be used as an immediate operand.
2165
2166 Otherwise, return the number of instructions needed to load or store
2167 values of mode MODE to or from addresses of type TYPE. Return 0 if
2168 the given type of symbol is not valid in addresses.
2169
2170 In both cases, instruction counts are based off BASE_INSN_LENGTH. */
2171
2172 static int
2173 mips_symbol_insns (enum mips_symbol_type type, machine_mode mode)
2174 {
2175 return mips_symbol_insns_1 (type, mode) * (TARGET_MIPS16 ? 2 : 1);
2176 }
2177 \f
2178 /* Implement TARGET_CANNOT_FORCE_CONST_MEM. */
2179
2180 static bool
2181 mips_cannot_force_const_mem (machine_mode mode, rtx x)
2182 {
2183 enum mips_symbol_type type;
2184 rtx base, offset;
2185
2186 /* There is no assembler syntax for expressing an address-sized
2187 high part. */
2188 if (GET_CODE (x) == HIGH)
2189 return true;
2190
2191 /* As an optimization, reject constants that mips_legitimize_move
2192 can expand inline.
2193
2194 Suppose we have a multi-instruction sequence that loads constant C
2195 into register R. If R does not get allocated a hard register, and
2196 R is used in an operand that allows both registers and memory
2197 references, reload will consider forcing C into memory and using
2198 one of the instruction's memory alternatives. Returning false
2199 here will force it to use an input reload instead. */
2200 if (CONST_INT_P (x) && mips_legitimate_constant_p (mode, x))
2201 return true;
2202
2203 split_const (x, &base, &offset);
2204 if (mips_symbolic_constant_p (base, SYMBOL_CONTEXT_LEA, &type))
2205 {
2206 /* See whether we explicitly want these symbols in the pool. */
2207 if (mips_use_pcrel_pool_p[(int) type])
2208 return false;
2209
2210 /* The same optimization as for CONST_INT. */
2211 if (SMALL_INT (offset) && mips_symbol_insns (type, MAX_MACHINE_MODE) > 0)
2212 return true;
2213
2214 /* If MIPS16 constant pools live in the text section, they should
2215 not refer to anything that might need run-time relocation. */
2216 if (TARGET_MIPS16_PCREL_LOADS && mips_got_symbol_type_p (type))
2217 return true;
2218 }
2219
2220 /* TLS symbols must be computed by mips_legitimize_move. */
2221 if (tls_referenced_p (x))
2222 return true;
2223
2224 return false;
2225 }
2226
2227 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P. We can't use blocks for
2228 constants when we're using a per-function constant pool. */
2229
2230 static bool
2231 mips_use_blocks_for_constant_p (machine_mode mode ATTRIBUTE_UNUSED,
2232 const_rtx x ATTRIBUTE_UNUSED)
2233 {
2234 return !TARGET_MIPS16_PCREL_LOADS;
2235 }
2236 \f
2237 /* Return true if register REGNO is a valid base register for mode MODE.
2238 STRICT_P is true if REG_OK_STRICT is in effect. */
2239
2240 int
2241 mips_regno_mode_ok_for_base_p (int regno, machine_mode mode,
2242 bool strict_p)
2243 {
2244 if (!HARD_REGISTER_NUM_P (regno))
2245 {
2246 if (!strict_p)
2247 return true;
2248 regno = reg_renumber[regno];
2249 }
2250
2251 /* These fake registers will be eliminated to either the stack or
2252 hard frame pointer, both of which are usually valid base registers.
2253 Reload deals with the cases where the eliminated form isn't valid. */
2254 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
2255 return true;
2256
2257 /* In MIPS16 mode, the stack pointer can only address word and doubleword
2258 values, nothing smaller. */
2259 if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
2260 return GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
2261
2262 return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
2263 }
2264
2265 /* Return true if X is a valid base register for mode MODE.
2266 STRICT_P is true if REG_OK_STRICT is in effect. */
2267
2268 static bool
2269 mips_valid_base_register_p (rtx x, machine_mode mode, bool strict_p)
2270 {
2271 if (!strict_p && GET_CODE (x) == SUBREG)
2272 x = SUBREG_REG (x);
2273
2274 return (REG_P (x)
2275 && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict_p));
2276 }
2277
2278 /* Return true if, for every base register BASE_REG, (plus BASE_REG X)
2279 can address a value of mode MODE. */
2280
2281 static bool
2282 mips_valid_offset_p (rtx x, machine_mode mode)
2283 {
2284 /* Check that X is a signed 16-bit number. */
2285 if (!const_arith_operand (x, Pmode))
2286 return false;
2287
2288 /* We may need to split multiword moves, so make sure that every word
2289 is accessible. */
2290 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2291 && !SMALL_OPERAND (INTVAL (x) + GET_MODE_SIZE (mode) - UNITS_PER_WORD))
2292 return false;
2293
2294 return true;
2295 }
2296
2297 /* Return true if a LO_SUM can address a value of mode MODE when the
2298 LO_SUM symbol has type SYMBOL_TYPE. */
2299
2300 static bool
2301 mips_valid_lo_sum_p (enum mips_symbol_type symbol_type, machine_mode mode)
2302 {
2303 /* Check that symbols of type SYMBOL_TYPE can be used to access values
2304 of mode MODE. */
2305 if (mips_symbol_insns (symbol_type, mode) == 0)
2306 return false;
2307
2308 /* Check that there is a known low-part relocation. */
2309 if (mips_lo_relocs[symbol_type] == NULL)
2310 return false;
2311
2312 /* We may need to split multiword moves, so make sure that each word
2313 can be accessed without inducing a carry. This is mainly needed
2314 for o64, which has historically only guaranteed 64-bit alignment
2315 for 128-bit types. */
2316 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
2317 && GET_MODE_BITSIZE (mode) > GET_MODE_ALIGNMENT (mode))
2318 return false;
2319
2320 return true;
2321 }
2322
2323 /* Return true if X is a valid address for machine mode MODE. If it is,
2324 fill in INFO appropriately. STRICT_P is true if REG_OK_STRICT is in
2325 effect. */
2326
2327 static bool
2328 mips_classify_address (struct mips_address_info *info, rtx x,
2329 machine_mode mode, bool strict_p)
2330 {
2331 switch (GET_CODE (x))
2332 {
2333 case REG:
2334 case SUBREG:
2335 info->type = ADDRESS_REG;
2336 info->reg = x;
2337 info->offset = const0_rtx;
2338 return mips_valid_base_register_p (info->reg, mode, strict_p);
2339
2340 case PLUS:
2341 info->type = ADDRESS_REG;
2342 info->reg = XEXP (x, 0);
2343 info->offset = XEXP (x, 1);
2344 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2345 && mips_valid_offset_p (info->offset, mode));
2346
2347 case LO_SUM:
2348 info->type = ADDRESS_LO_SUM;
2349 info->reg = XEXP (x, 0);
2350 info->offset = XEXP (x, 1);
2351 /* We have to trust the creator of the LO_SUM to do something vaguely
2352 sane. Target-independent code that creates a LO_SUM should also
2353 create and verify the matching HIGH. Target-independent code that
2354 adds an offset to a LO_SUM must prove that the offset will not
2355 induce a carry. Failure to do either of these things would be
2356 a bug, and we are not required to check for it here. The MIPS
2357 backend itself should only create LO_SUMs for valid symbolic
2358 constants, with the high part being either a HIGH or a copy
2359 of _gp. */
2360 info->symbol_type
2361 = mips_classify_symbolic_expression (info->offset, SYMBOL_CONTEXT_MEM);
2362 return (mips_valid_base_register_p (info->reg, mode, strict_p)
2363 && mips_valid_lo_sum_p (info->symbol_type, mode));
2364
2365 case CONST_INT:
2366 /* Small-integer addresses don't occur very often, but they
2367 are legitimate if $0 is a valid base register. */
2368 info->type = ADDRESS_CONST_INT;
2369 return !TARGET_MIPS16 && SMALL_INT (x);
2370
2371 case CONST:
2372 case LABEL_REF:
2373 case SYMBOL_REF:
2374 info->type = ADDRESS_SYMBOLIC;
2375 return (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_MEM,
2376 &info->symbol_type)
2377 && mips_symbol_insns (info->symbol_type, mode) > 0
2378 && !mips_split_p[info->symbol_type]);
2379
2380 default:
2381 return false;
2382 }
2383 }
2384
2385 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
2386
2387 static bool
2388 mips_legitimate_address_p (machine_mode mode, rtx x, bool strict_p)
2389 {
2390 struct mips_address_info addr;
2391
2392 return mips_classify_address (&addr, x, mode, strict_p);
2393 }
2394
2395 /* Return true if X is a legitimate $sp-based address for mode MDOE. */
2396
2397 bool
2398 mips_stack_address_p (rtx x, machine_mode mode)
2399 {
2400 struct mips_address_info addr;
2401
2402 return (mips_classify_address (&addr, x, mode, false)
2403 && addr.type == ADDRESS_REG
2404 && addr.reg == stack_pointer_rtx);
2405 }
2406
2407 /* Return true if ADDR matches the pattern for the LWXS load scaled indexed
2408 address instruction. Note that such addresses are not considered
2409 legitimate in the TARGET_LEGITIMATE_ADDRESS_P sense, because their use
2410 is so restricted. */
2411
2412 static bool
2413 mips_lwxs_address_p (rtx addr)
2414 {
2415 if (ISA_HAS_LWXS
2416 && GET_CODE (addr) == PLUS
2417 && REG_P (XEXP (addr, 1)))
2418 {
2419 rtx offset = XEXP (addr, 0);
2420 if (GET_CODE (offset) == MULT
2421 && REG_P (XEXP (offset, 0))
2422 && CONST_INT_P (XEXP (offset, 1))
2423 && INTVAL (XEXP (offset, 1)) == 4)
2424 return true;
2425 }
2426 return false;
2427 }
2428
2429 /* Return true if ADDR matches the pattern for the L{B,H,W,D}{,U}X load
2430 indexed address instruction. Note that such addresses are
2431 not considered legitimate in the TARGET_LEGITIMATE_ADDRESS_P
2432 sense, because their use is so restricted. */
2433
2434 static bool
2435 mips_lx_address_p (rtx addr, machine_mode mode)
2436 {
2437 if (GET_CODE (addr) != PLUS
2438 || !REG_P (XEXP (addr, 0))
2439 || !REG_P (XEXP (addr, 1)))
2440 return false;
2441 if (ISA_HAS_LBX && mode == QImode)
2442 return true;
2443 if (ISA_HAS_LHX && mode == HImode)
2444 return true;
2445 if (ISA_HAS_LWX && mode == SImode)
2446 return true;
2447 if (ISA_HAS_LDX && mode == DImode)
2448 return true;
2449 return false;
2450 }
2451 \f
2452 /* Return true if a value at OFFSET bytes from base register BASE can be
2453 accessed using an unextended MIPS16 instruction. MODE is the mode of
2454 the value.
2455
2456 Usually the offset in an unextended instruction is a 5-bit field.
2457 The offset is unsigned and shifted left once for LH and SH, twice
2458 for LW and SW, and so on. An exception is LWSP and SWSP, which have
2459 an 8-bit immediate field that's shifted left twice. */
2460
2461 static bool
2462 mips16_unextended_reference_p (machine_mode mode, rtx base,
2463 unsigned HOST_WIDE_INT offset)
2464 {
2465 if (mode != BLKmode && offset % GET_MODE_SIZE (mode) == 0)
2466 {
2467 if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
2468 return offset < 256U * GET_MODE_SIZE (mode);
2469 return offset < 32U * GET_MODE_SIZE (mode);
2470 }
2471 return false;
2472 }
2473
2474 /* Return the number of instructions needed to load or store a value
2475 of mode MODE at address X, assuming that BASE_INSN_LENGTH is the
2476 length of one instruction. Return 0 if X isn't valid for MODE.
2477 Assume that multiword moves may need to be split into word moves
2478 if MIGHT_SPLIT_P, otherwise assume that a single load or store is
2479 enough. */
2480
2481 int
2482 mips_address_insns (rtx x, machine_mode mode, bool might_split_p)
2483 {
2484 struct mips_address_info addr;
2485 int factor;
2486
2487 /* BLKmode is used for single unaligned loads and stores and should
2488 not count as a multiword mode. (GET_MODE_SIZE (BLKmode) is pretty
2489 meaningless, so we have to single it out as a special case one way
2490 or the other.) */
2491 if (mode != BLKmode && might_split_p)
2492 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2493 else
2494 factor = 1;
2495
2496 if (mips_classify_address (&addr, x, mode, false))
2497 switch (addr.type)
2498 {
2499 case ADDRESS_REG:
2500 if (TARGET_MIPS16
2501 && !mips16_unextended_reference_p (mode, addr.reg,
2502 UINTVAL (addr.offset)))
2503 return factor * 2;
2504 return factor;
2505
2506 case ADDRESS_LO_SUM:
2507 return TARGET_MIPS16 ? factor * 2 : factor;
2508
2509 case ADDRESS_CONST_INT:
2510 return factor;
2511
2512 case ADDRESS_SYMBOLIC:
2513 return factor * mips_symbol_insns (addr.symbol_type, mode);
2514 }
2515 return 0;
2516 }
2517
2518 /* Return true if X fits within an unsigned field of BITS bits that is
2519 shifted left SHIFT bits before being used. */
2520
2521 bool
2522 mips_unsigned_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2523 {
2524 return (x & ((1 << shift) - 1)) == 0 && x < ((unsigned) 1 << (shift + bits));
2525 }
2526
2527 /* Return true if X fits within a signed field of BITS bits that is
2528 shifted left SHIFT bits before being used. */
2529
2530 bool
2531 mips_signed_immediate_p (unsigned HOST_WIDE_INT x, int bits, int shift = 0)
2532 {
2533 x += 1 << (bits + shift - 1);
2534 return mips_unsigned_immediate_p (x, bits, shift);
2535 }
2536
2537 /* Return true if X is legitimate for accessing values of mode MODE,
2538 if it is based on a MIPS16 register, and if the offset satisfies
2539 OFFSET_PREDICATE. */
2540
2541 bool
2542 m16_based_address_p (rtx x, machine_mode mode,
2543 insn_operand_predicate_fn offset_predicate)
2544 {
2545 struct mips_address_info addr;
2546
2547 return (mips_classify_address (&addr, x, mode, false)
2548 && addr.type == ADDRESS_REG
2549 && M16_REG_P (REGNO (addr.reg))
2550 && offset_predicate (addr.offset, mode));
2551 }
2552
2553 /* Return true if X is a legitimate address that conforms to the requirements
2554 for a microMIPS LWSP or SWSP insn. */
2555
2556 bool
2557 lwsp_swsp_address_p (rtx x, machine_mode mode)
2558 {
2559 struct mips_address_info addr;
2560
2561 return (mips_classify_address (&addr, x, mode, false)
2562 && addr.type == ADDRESS_REG
2563 && REGNO (addr.reg) == STACK_POINTER_REGNUM
2564 && uw5_operand (addr.offset, mode));
2565 }
2566
2567 /* Return true if X is a legitimate address with a 12-bit offset.
2568 MODE is the mode of the value being accessed. */
2569
2570 bool
2571 umips_12bit_offset_address_p (rtx x, machine_mode mode)
2572 {
2573 struct mips_address_info addr;
2574
2575 return (mips_classify_address (&addr, x, mode, false)
2576 && addr.type == ADDRESS_REG
2577 && CONST_INT_P (addr.offset)
2578 && UMIPS_12BIT_OFFSET_P (INTVAL (addr.offset)));
2579 }
2580
2581 /* Return the number of instructions needed to load constant X,
2582 assuming that BASE_INSN_LENGTH is the length of one instruction.
2583 Return 0 if X isn't a valid constant. */
2584
2585 int
2586 mips_const_insns (rtx x)
2587 {
2588 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2589 enum mips_symbol_type symbol_type;
2590 rtx offset;
2591
2592 switch (GET_CODE (x))
2593 {
2594 case HIGH:
2595 if (!mips_symbolic_constant_p (XEXP (x, 0), SYMBOL_CONTEXT_LEA,
2596 &symbol_type)
2597 || !mips_split_p[symbol_type])
2598 return 0;
2599
2600 /* This is simply an LUI for normal mode. It is an extended
2601 LI followed by an extended SLL for MIPS16. */
2602 return TARGET_MIPS16 ? 4 : 1;
2603
2604 case CONST_INT:
2605 if (TARGET_MIPS16)
2606 /* Unsigned 8-bit constants can be loaded using an unextended
2607 LI instruction. Unsigned 16-bit constants can be loaded
2608 using an extended LI. Negative constants must be loaded
2609 using LI and then negated. */
2610 return (IN_RANGE (INTVAL (x), 0, 255) ? 1
2611 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
2612 : IN_RANGE (-INTVAL (x), 0, 255) ? 2
2613 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
2614 : 0);
2615
2616 return mips_build_integer (codes, INTVAL (x));
2617
2618 case CONST_DOUBLE:
2619 case CONST_VECTOR:
2620 /* Allow zeros for normal mode, where we can use $0. */
2621 return !TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0;
2622
2623 case CONST:
2624 if (CONST_GP_P (x))
2625 return 1;
2626
2627 /* See if we can refer to X directly. */
2628 if (mips_symbolic_constant_p (x, SYMBOL_CONTEXT_LEA, &symbol_type))
2629 return mips_symbol_insns (symbol_type, MAX_MACHINE_MODE);
2630
2631 /* Otherwise try splitting the constant into a base and offset.
2632 If the offset is a 16-bit value, we can load the base address
2633 into a register and then use (D)ADDIU to add in the offset.
2634 If the offset is larger, we can load the base and offset
2635 into separate registers and add them together with (D)ADDU.
2636 However, the latter is only possible before reload; during
2637 and after reload, we must have the option of forcing the
2638 constant into the pool instead. */
2639 split_const (x, &x, &offset);
2640 if (offset != 0)
2641 {
2642 int n = mips_const_insns (x);
2643 if (n != 0)
2644 {
2645 if (SMALL_INT (offset))
2646 return n + 1;
2647 else if (!targetm.cannot_force_const_mem (GET_MODE (x), x))
2648 return n + 1 + mips_build_integer (codes, INTVAL (offset));
2649 }
2650 }
2651 return 0;
2652
2653 case SYMBOL_REF:
2654 case LABEL_REF:
2655 return mips_symbol_insns (mips_classify_symbol (x, SYMBOL_CONTEXT_LEA),
2656 MAX_MACHINE_MODE);
2657
2658 default:
2659 return 0;
2660 }
2661 }
2662
2663 /* X is a doubleword constant that can be handled by splitting it into
2664 two words and loading each word separately. Return the number of
2665 instructions required to do this, assuming that BASE_INSN_LENGTH
2666 is the length of one instruction. */
2667
2668 int
2669 mips_split_const_insns (rtx x)
2670 {
2671 unsigned int low, high;
2672
2673 low = mips_const_insns (mips_subword (x, false));
2674 high = mips_const_insns (mips_subword (x, true));
2675 gcc_assert (low > 0 && high > 0);
2676 return low + high;
2677 }
2678
2679 /* Return the number of instructions needed to implement INSN,
2680 given that it loads from or stores to MEM. Assume that
2681 BASE_INSN_LENGTH is the length of one instruction. */
2682
2683 int
2684 mips_load_store_insns (rtx mem, rtx_insn *insn)
2685 {
2686 machine_mode mode;
2687 bool might_split_p;
2688 rtx set;
2689
2690 gcc_assert (MEM_P (mem));
2691 mode = GET_MODE (mem);
2692
2693 /* Try to prove that INSN does not need to be split. */
2694 might_split_p = GET_MODE_SIZE (mode) > UNITS_PER_WORD;
2695 if (might_split_p)
2696 {
2697 set = single_set (insn);
2698 if (set && !mips_split_move_insn_p (SET_DEST (set), SET_SRC (set), insn))
2699 might_split_p = false;
2700 }
2701
2702 return mips_address_insns (XEXP (mem, 0), mode, might_split_p);
2703 }
2704
2705 /* Return the number of instructions needed for an integer division,
2706 assuming that BASE_INSN_LENGTH is the length of one instruction. */
2707
2708 int
2709 mips_idiv_insns (void)
2710 {
2711 int count;
2712
2713 count = 1;
2714 if (TARGET_CHECK_ZERO_DIV)
2715 {
2716 if (GENERATE_DIVIDE_TRAPS)
2717 count++;
2718 else
2719 count += 2;
2720 }
2721
2722 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
2723 count++;
2724 return count;
2725 }
2726 \f
2727 /* Emit a move from SRC to DEST. Assume that the move expanders can
2728 handle all moves if !can_create_pseudo_p (). The distinction is
2729 important because, unlike emit_move_insn, the move expanders know
2730 how to force Pmode objects into the constant pool even when the
2731 constant pool address is not itself legitimate. */
2732
2733 rtx_insn *
2734 mips_emit_move (rtx dest, rtx src)
2735 {
2736 return (can_create_pseudo_p ()
2737 ? emit_move_insn (dest, src)
2738 : emit_move_insn_1 (dest, src));
2739 }
2740
2741 /* Emit a move from SRC to DEST, splitting compound moves into individual
2742 instructions. SPLIT_TYPE is the type of split to perform. */
2743
2744 static void
2745 mips_emit_move_or_split (rtx dest, rtx src, enum mips_split_type split_type)
2746 {
2747 if (mips_split_move_p (dest, src, split_type))
2748 mips_split_move (dest, src, split_type);
2749 else
2750 mips_emit_move (dest, src);
2751 }
2752
2753 /* Emit an instruction of the form (set TARGET (CODE OP0)). */
2754
2755 static void
2756 mips_emit_unary (enum rtx_code code, rtx target, rtx op0)
2757 {
2758 emit_insn (gen_rtx_SET (VOIDmode, target,
2759 gen_rtx_fmt_e (code, GET_MODE (op0), op0)));
2760 }
2761
2762 /* Compute (CODE OP0) and store the result in a new register of mode MODE.
2763 Return that new register. */
2764
2765 static rtx
2766 mips_force_unary (machine_mode mode, enum rtx_code code, rtx op0)
2767 {
2768 rtx reg;
2769
2770 reg = gen_reg_rtx (mode);
2771 mips_emit_unary (code, reg, op0);
2772 return reg;
2773 }
2774
2775 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)). */
2776
2777 void
2778 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2779 {
2780 emit_insn (gen_rtx_SET (VOIDmode, target,
2781 gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2782 }
2783
2784 /* Compute (CODE OP0 OP1) and store the result in a new register
2785 of mode MODE. Return that new register. */
2786
2787 static rtx
2788 mips_force_binary (machine_mode mode, enum rtx_code code, rtx op0, rtx op1)
2789 {
2790 rtx reg;
2791
2792 reg = gen_reg_rtx (mode);
2793 mips_emit_binary (code, reg, op0, op1);
2794 return reg;
2795 }
2796
2797 /* Copy VALUE to a register and return that register. If new pseudos
2798 are allowed, copy it into a new register, otherwise use DEST. */
2799
2800 static rtx
2801 mips_force_temporary (rtx dest, rtx value)
2802 {
2803 if (can_create_pseudo_p ())
2804 return force_reg (Pmode, value);
2805 else
2806 {
2807 mips_emit_move (dest, value);
2808 return dest;
2809 }
2810 }
2811
2812 /* Emit a call sequence with call pattern PATTERN and return the call
2813 instruction itself (which is not necessarily the last instruction
2814 emitted). ORIG_ADDR is the original, unlegitimized address,
2815 ADDR is the legitimized form, and LAZY_P is true if the call
2816 address is lazily-bound. */
2817
2818 static rtx_insn *
2819 mips_emit_call_insn (rtx pattern, rtx orig_addr, rtx addr, bool lazy_p)
2820 {
2821 rtx_insn *insn;
2822 rtx reg;
2823
2824 insn = emit_call_insn (pattern);
2825
2826 if (TARGET_MIPS16 && mips_use_pic_fn_addr_reg_p (orig_addr))
2827 {
2828 /* MIPS16 JALRs only take MIPS16 registers. If the target
2829 function requires $25 to be valid on entry, we must copy it
2830 there separately. The move instruction can be put in the
2831 call's delay slot. */
2832 reg = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
2833 emit_insn_before (gen_move_insn (reg, addr), insn);
2834 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), reg);
2835 }
2836
2837 if (lazy_p)
2838 /* Lazy-binding stubs require $gp to be valid on entry. */
2839 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
2840
2841 if (TARGET_USE_GOT)
2842 {
2843 /* See the comment above load_call<mode> for details. */
2844 use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
2845 gen_rtx_REG (Pmode, GOT_VERSION_REGNUM));
2846 emit_insn (gen_update_got_version ());
2847 }
2848
2849 if (TARGET_MIPS16
2850 && TARGET_EXPLICIT_RELOCS
2851 && TARGET_CALL_CLOBBERED_GP)
2852 {
2853 rtx post_call_tmp_reg = gen_rtx_REG (word_mode, POST_CALL_TMP_REG);
2854 clobber_reg (&CALL_INSN_FUNCTION_USAGE (insn), post_call_tmp_reg);
2855 }
2856
2857 return insn;
2858 }
2859 \f
2860 /* Wrap symbol or label BASE in an UNSPEC address of type SYMBOL_TYPE,
2861 then add CONST_INT OFFSET to the result. */
2862
2863 static rtx
2864 mips_unspec_address_offset (rtx base, rtx offset,
2865 enum mips_symbol_type symbol_type)
2866 {
2867 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
2868 UNSPEC_ADDRESS_FIRST + symbol_type);
2869 if (offset != const0_rtx)
2870 base = gen_rtx_PLUS (Pmode, base, offset);
2871 return gen_rtx_CONST (Pmode, base);
2872 }
2873
2874 /* Return an UNSPEC address with underlying address ADDRESS and symbol
2875 type SYMBOL_TYPE. */
2876
2877 rtx
2878 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
2879 {
2880 rtx base, offset;
2881
2882 split_const (address, &base, &offset);
2883 return mips_unspec_address_offset (base, offset, symbol_type);
2884 }
2885
2886 /* If OP is an UNSPEC address, return the address to which it refers,
2887 otherwise return OP itself. */
2888
2889 rtx
2890 mips_strip_unspec_address (rtx op)
2891 {
2892 rtx base, offset;
2893
2894 split_const (op, &base, &offset);
2895 if (UNSPEC_ADDRESS_P (base))
2896 op = plus_constant (Pmode, UNSPEC_ADDRESS (base), INTVAL (offset));
2897 return op;
2898 }
2899
2900 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
2901 high part to BASE and return the result. Just return BASE otherwise.
2902 TEMP is as for mips_force_temporary.
2903
2904 The returned expression can be used as the first operand to a LO_SUM. */
2905
2906 static rtx
2907 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
2908 enum mips_symbol_type symbol_type)
2909 {
2910 if (mips_split_p[symbol_type])
2911 {
2912 addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
2913 addr = mips_force_temporary (temp, addr);
2914 base = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
2915 }
2916 return base;
2917 }
2918 \f
2919 /* Return an instruction that copies $gp into register REG. We want
2920 GCC to treat the register's value as constant, so that its value
2921 can be rematerialized on demand. */
2922
2923 static rtx
2924 gen_load_const_gp (rtx reg)
2925 {
2926 return PMODE_INSN (gen_load_const_gp, (reg));
2927 }
2928
2929 /* Return a pseudo register that contains the value of $gp throughout
2930 the current function. Such registers are needed by MIPS16 functions,
2931 for which $gp itself is not a valid base register or addition operand. */
2932
2933 static rtx
2934 mips16_gp_pseudo_reg (void)
2935 {
2936 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
2937 {
2938 rtx_insn *scan;
2939
2940 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
2941
2942 push_topmost_sequence ();
2943
2944 scan = get_insns ();
2945 while (NEXT_INSN (scan) && !INSN_P (NEXT_INSN (scan)))
2946 scan = NEXT_INSN (scan);
2947
2948 rtx set = gen_load_const_gp (cfun->machine->mips16_gp_pseudo_rtx);
2949 rtx_insn *insn = emit_insn_after (set, scan);
2950 INSN_LOCATION (insn) = 0;
2951
2952 pop_topmost_sequence ();
2953 }
2954
2955 return cfun->machine->mips16_gp_pseudo_rtx;
2956 }
2957
2958 /* Return a base register that holds pic_offset_table_rtx.
2959 TEMP, if nonnull, is a scratch Pmode base register. */
2960
2961 rtx
2962 mips_pic_base_register (rtx temp)
2963 {
2964 if (!TARGET_MIPS16)
2965 return pic_offset_table_rtx;
2966
2967 if (currently_expanding_to_rtl)
2968 return mips16_gp_pseudo_reg ();
2969
2970 if (can_create_pseudo_p ())
2971 temp = gen_reg_rtx (Pmode);
2972
2973 if (TARGET_USE_GOT)
2974 /* The first post-reload split exposes all references to $gp
2975 (both uses and definitions). All references must remain
2976 explicit after that point.
2977
2978 It is safe to introduce uses of $gp at any time, so for
2979 simplicity, we do that before the split too. */
2980 mips_emit_move (temp, pic_offset_table_rtx);
2981 else
2982 emit_insn (gen_load_const_gp (temp));
2983 return temp;
2984 }
2985
2986 /* Return the RHS of a load_call<mode> insn. */
2987
2988 static rtx
2989 mips_unspec_call (rtx reg, rtx symbol)
2990 {
2991 rtvec vec;
2992
2993 vec = gen_rtvec (3, reg, symbol, gen_rtx_REG (SImode, GOT_VERSION_REGNUM));
2994 return gen_rtx_UNSPEC (Pmode, vec, UNSPEC_LOAD_CALL);
2995 }
2996
2997 /* If SRC is the RHS of a load_call<mode> insn, return the underlying symbol
2998 reference. Return NULL_RTX otherwise. */
2999
3000 static rtx
3001 mips_strip_unspec_call (rtx src)
3002 {
3003 if (GET_CODE (src) == UNSPEC && XINT (src, 1) == UNSPEC_LOAD_CALL)
3004 return mips_strip_unspec_address (XVECEXP (src, 0, 1));
3005 return NULL_RTX;
3006 }
3007
3008 /* Create and return a GOT reference of type TYPE for address ADDR.
3009 TEMP, if nonnull, is a scratch Pmode base register. */
3010
3011 rtx
3012 mips_got_load (rtx temp, rtx addr, enum mips_symbol_type type)
3013 {
3014 rtx base, high, lo_sum_symbol;
3015
3016 base = mips_pic_base_register (temp);
3017
3018 /* If we used the temporary register to load $gp, we can't use
3019 it for the high part as well. */
3020 if (temp != NULL && reg_overlap_mentioned_p (base, temp))
3021 temp = NULL;
3022
3023 high = mips_unspec_offset_high (temp, base, addr, type);
3024 lo_sum_symbol = mips_unspec_address (addr, type);
3025
3026 if (type == SYMBOL_GOTOFF_CALL)
3027 return mips_unspec_call (high, lo_sum_symbol);
3028 else
3029 return PMODE_INSN (gen_unspec_got, (high, lo_sum_symbol));
3030 }
3031
3032 /* If MODE is MAX_MACHINE_MODE, ADDR appears as a move operand, otherwise
3033 it appears in a MEM of that mode. Return true if ADDR is a legitimate
3034 constant in that context and can be split into high and low parts.
3035 If so, and if LOW_OUT is nonnull, emit the high part and store the
3036 low part in *LOW_OUT. Leave *LOW_OUT unchanged otherwise.
3037
3038 TEMP is as for mips_force_temporary and is used to load the high
3039 part into a register.
3040
3041 When MODE is MAX_MACHINE_MODE, the low part is guaranteed to be
3042 a legitimize SET_SRC for an .md pattern, otherwise the low part
3043 is guaranteed to be a legitimate address for mode MODE. */
3044
3045 bool
3046 mips_split_symbol (rtx temp, rtx addr, machine_mode mode, rtx *low_out)
3047 {
3048 enum mips_symbol_context context;
3049 enum mips_symbol_type symbol_type;
3050 rtx high;
3051
3052 context = (mode == MAX_MACHINE_MODE
3053 ? SYMBOL_CONTEXT_LEA
3054 : SYMBOL_CONTEXT_MEM);
3055 if (GET_CODE (addr) == HIGH && context == SYMBOL_CONTEXT_LEA)
3056 {
3057 addr = XEXP (addr, 0);
3058 if (mips_symbolic_constant_p (addr, context, &symbol_type)
3059 && mips_symbol_insns (symbol_type, mode) > 0
3060 && mips_split_hi_p[symbol_type])
3061 {
3062 if (low_out)
3063 switch (symbol_type)
3064 {
3065 case SYMBOL_GOT_PAGE_OFST:
3066 /* The high part of a page/ofst pair is loaded from the GOT. */
3067 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_PAGE);
3068 break;
3069
3070 default:
3071 gcc_unreachable ();
3072 }
3073 return true;
3074 }
3075 }
3076 else
3077 {
3078 if (mips_symbolic_constant_p (addr, context, &symbol_type)
3079 && mips_symbol_insns (symbol_type, mode) > 0
3080 && mips_split_p[symbol_type])
3081 {
3082 if (low_out)
3083 switch (symbol_type)
3084 {
3085 case SYMBOL_GOT_DISP:
3086 /* SYMBOL_GOT_DISP symbols are loaded from the GOT. */
3087 *low_out = mips_got_load (temp, addr, SYMBOL_GOTOFF_DISP);
3088 break;
3089
3090 case SYMBOL_GP_RELATIVE:
3091 high = mips_pic_base_register (temp);
3092 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3093 break;
3094
3095 default:
3096 high = gen_rtx_HIGH (Pmode, copy_rtx (addr));
3097 high = mips_force_temporary (temp, high);
3098 *low_out = gen_rtx_LO_SUM (Pmode, high, addr);
3099 break;
3100 }
3101 return true;
3102 }
3103 }
3104 return false;
3105 }
3106
3107 /* Return a legitimate address for REG + OFFSET. TEMP is as for
3108 mips_force_temporary; it is only needed when OFFSET is not a
3109 SMALL_OPERAND. */
3110
3111 static rtx
3112 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
3113 {
3114 if (!SMALL_OPERAND (offset))
3115 {
3116 rtx high;
3117
3118 if (TARGET_MIPS16)
3119 {
3120 /* Load the full offset into a register so that we can use
3121 an unextended instruction for the address itself. */
3122 high = GEN_INT (offset);
3123 offset = 0;
3124 }
3125 else
3126 {
3127 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.
3128 The addition inside the macro CONST_HIGH_PART may cause an
3129 overflow, so we need to force a sign-extension check. */
3130 high = gen_int_mode (CONST_HIGH_PART (offset), Pmode);
3131 offset = CONST_LOW_PART (offset);
3132 }
3133 high = mips_force_temporary (temp, high);
3134 reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
3135 }
3136 return plus_constant (Pmode, reg, offset);
3137 }
3138 \f
3139 /* The __tls_get_attr symbol. */
3140 static GTY(()) rtx mips_tls_symbol;
3141
3142 /* Return an instruction sequence that calls __tls_get_addr. SYM is
3143 the TLS symbol we are referencing and TYPE is the symbol type to use
3144 (either global dynamic or local dynamic). V0 is an RTX for the
3145 return value location. */
3146
3147 static rtx
3148 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
3149 {
3150 rtx insn, loc, a0;
3151
3152 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
3153
3154 if (!mips_tls_symbol)
3155 mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
3156
3157 loc = mips_unspec_address (sym, type);
3158
3159 start_sequence ();
3160
3161 emit_insn (gen_rtx_SET (Pmode, a0,
3162 gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
3163 insn = mips_expand_call (MIPS_CALL_NORMAL, v0, mips_tls_symbol,
3164 const0_rtx, NULL_RTX, false);
3165 RTL_CONST_CALL_P (insn) = 1;
3166 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
3167 insn = get_insns ();
3168
3169 end_sequence ();
3170
3171 return insn;
3172 }
3173
3174 /* Return a pseudo register that contains the current thread pointer. */
3175
3176 rtx
3177 mips_expand_thread_pointer (rtx tp)
3178 {
3179 rtx fn;
3180
3181 if (TARGET_MIPS16)
3182 {
3183 if (!mips16_rdhwr_stub)
3184 mips16_rdhwr_stub = new mips16_rdhwr_one_only_stub ();
3185 fn = mips16_stub_call_address (mips16_rdhwr_stub);
3186 emit_insn (PMODE_INSN (gen_tls_get_tp_mips16, (tp, fn)));
3187 }
3188 else
3189 emit_insn (PMODE_INSN (gen_tls_get_tp, (tp)));
3190 return tp;
3191 }
3192
3193 static rtx
3194 mips_get_tp (void)
3195 {
3196 return mips_expand_thread_pointer (gen_reg_rtx (Pmode));
3197 }
3198
3199 /* Generate the code to access LOC, a thread-local SYMBOL_REF, and return
3200 its address. The return value will be both a valid address and a valid
3201 SET_SRC (either a REG or a LO_SUM). */
3202
3203 static rtx
3204 mips_legitimize_tls_address (rtx loc)
3205 {
3206 rtx dest, insn, v0, tp, tmp1, tmp2, eqv, offset;
3207 enum tls_model model;
3208
3209 model = SYMBOL_REF_TLS_MODEL (loc);
3210 /* Only TARGET_ABICALLS code can have more than one module; other
3211 code must be be static and should not use a GOT. All TLS models
3212 reduce to local exec in this situation. */
3213 if (!TARGET_ABICALLS)
3214 model = TLS_MODEL_LOCAL_EXEC;
3215
3216 switch (model)
3217 {
3218 case TLS_MODEL_GLOBAL_DYNAMIC:
3219 v0 = gen_rtx_REG (Pmode, GP_RETURN);
3220 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
3221 dest = gen_reg_rtx (Pmode);
3222 emit_libcall_block (insn, dest, v0, loc);
3223 break;
3224
3225 case TLS_MODEL_LOCAL_DYNAMIC:
3226 v0 = gen_rtx_REG (Pmode, GP_RETURN);
3227 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
3228 tmp1 = gen_reg_rtx (Pmode);
3229
3230 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3231 share the LDM result with other LD model accesses. */
3232 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3233 UNSPEC_TLS_LDM);
3234 emit_libcall_block (insn, tmp1, v0, eqv);
3235
3236 offset = mips_unspec_address (loc, SYMBOL_DTPREL);
3237 if (mips_split_p[SYMBOL_DTPREL])
3238 {
3239 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
3240 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3241 }
3242 else
3243 dest = expand_binop (Pmode, add_optab, tmp1, offset,
3244 0, 0, OPTAB_DIRECT);
3245 break;
3246
3247 case TLS_MODEL_INITIAL_EXEC:
3248 tp = mips_get_tp ();
3249 tmp1 = gen_reg_rtx (Pmode);
3250 tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
3251 if (Pmode == DImode)
3252 emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
3253 else
3254 emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
3255 dest = gen_reg_rtx (Pmode);
3256 emit_insn (gen_add3_insn (dest, tmp1, tp));
3257 break;
3258
3259 case TLS_MODEL_LOCAL_EXEC:
3260 tmp1 = mips_get_tp ();
3261 offset = mips_unspec_address (loc, SYMBOL_TPREL);
3262 if (mips_split_p[SYMBOL_TPREL])
3263 {
3264 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_TPREL);
3265 dest = gen_rtx_LO_SUM (Pmode, tmp2, offset);
3266 }
3267 else
3268 dest = expand_binop (Pmode, add_optab, tmp1, offset,
3269 0, 0, OPTAB_DIRECT);
3270 break;
3271
3272 default:
3273 gcc_unreachable ();
3274 }
3275 return dest;
3276 }
3277 \f
3278 /* Implement "TARGET = __builtin_mips_get_fcsr ()" for MIPS16,
3279 using a stub. */
3280
3281 void
3282 mips16_expand_get_fcsr (rtx target)
3283 {
3284 if (!mips16_get_fcsr_stub)
3285 mips16_get_fcsr_stub = new mips16_get_fcsr_one_only_stub ();
3286 rtx fn = mips16_stub_call_address (mips16_get_fcsr_stub);
3287 emit_insn (PMODE_INSN (gen_mips_get_fcsr_mips16, (fn)));
3288 emit_move_insn (target, gen_rtx_REG (SImode, GET_FCSR_REGNUM));
3289 }
3290
3291 /* Implement __builtin_mips_set_fcsr (TARGET) for MIPS16, using a stub. */
3292
3293 void
3294 mips16_expand_set_fcsr (rtx newval)
3295 {
3296 if (!mips16_set_fcsr_stub)
3297 mips16_set_fcsr_stub = new mips16_set_fcsr_one_only_stub ();
3298 rtx fn = mips16_stub_call_address (mips16_set_fcsr_stub);
3299 emit_move_insn (gen_rtx_REG (SImode, SET_FCSR_REGNUM), newval);
3300 emit_insn (PMODE_INSN (gen_mips_set_fcsr_mips16, (fn)));
3301 }
3302 \f
3303 /* If X is not a valid address for mode MODE, force it into a register. */
3304
3305 static rtx
3306 mips_force_address (rtx x, machine_mode mode)
3307 {
3308 if (!mips_legitimate_address_p (mode, x, false))
3309 x = force_reg (Pmode, x);
3310 return x;
3311 }
3312
3313 /* This function is used to implement LEGITIMIZE_ADDRESS. If X can
3314 be legitimized in a way that the generic machinery might not expect,
3315 return a new address, otherwise return NULL. MODE is the mode of
3316 the memory being accessed. */
3317
3318 static rtx
3319 mips_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3320 machine_mode mode)
3321 {
3322 rtx base, addr;
3323 HOST_WIDE_INT offset;
3324
3325 if (mips_tls_symbol_p (x))
3326 return mips_legitimize_tls_address (x);
3327
3328 /* See if the address can split into a high part and a LO_SUM. */
3329 if (mips_split_symbol (NULL, x, mode, &addr))
3330 return mips_force_address (addr, mode);
3331
3332 /* Handle BASE + OFFSET using mips_add_offset. */
3333 mips_split_plus (x, &base, &offset);
3334 if (offset != 0)
3335 {
3336 if (!mips_valid_base_register_p (base, mode, false))
3337 base = copy_to_mode_reg (Pmode, base);
3338 addr = mips_add_offset (NULL, base, offset);
3339 return mips_force_address (addr, mode);
3340 }
3341
3342 return x;
3343 }
3344
3345 /* Load VALUE into DEST. TEMP is as for mips_force_temporary. */
3346
3347 void
3348 mips_move_integer (rtx temp, rtx dest, unsigned HOST_WIDE_INT value)
3349 {
3350 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
3351 machine_mode mode;
3352 unsigned int i, num_ops;
3353 rtx x;
3354
3355 mode = GET_MODE (dest);
3356 num_ops = mips_build_integer (codes, value);
3357
3358 /* Apply each binary operation to X. Invariant: X is a legitimate
3359 source operand for a SET pattern. */
3360 x = GEN_INT (codes[0].value);
3361 for (i = 1; i < num_ops; i++)
3362 {
3363 if (!can_create_pseudo_p ())
3364 {
3365 emit_insn (gen_rtx_SET (VOIDmode, temp, x));
3366 x = temp;
3367 }
3368 else
3369 x = force_reg (mode, x);
3370 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
3371 }
3372
3373 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
3374 }
3375
3376 /* Subroutine of mips_legitimize_move. Move constant SRC into register
3377 DEST given that SRC satisfies immediate_operand but doesn't satisfy
3378 move_operand. */
3379
3380 static void
3381 mips_legitimize_const_move (machine_mode mode, rtx dest, rtx src)
3382 {
3383 rtx base, offset;
3384
3385 /* Split moves of big integers into smaller pieces. */
3386 if (splittable_const_int_operand (src, mode))
3387 {
3388 mips_move_integer (dest, dest, INTVAL (src));
3389 return;
3390 }
3391
3392 /* Split moves of symbolic constants into high/low pairs. */
3393 if (mips_split_symbol (dest, src, MAX_MACHINE_MODE, &src))
3394 {
3395 emit_insn (gen_rtx_SET (VOIDmode, dest, src));
3396 return;
3397 }
3398
3399 /* Generate the appropriate access sequences for TLS symbols. */
3400 if (mips_tls_symbol_p (src))
3401 {
3402 mips_emit_move (dest, mips_legitimize_tls_address (src));
3403 return;
3404 }
3405
3406 /* If we have (const (plus symbol offset)), and that expression cannot
3407 be forced into memory, load the symbol first and add in the offset.
3408 In non-MIPS16 mode, prefer to do this even if the constant _can_ be
3409 forced into memory, as it usually produces better code. */
3410 split_const (src, &base, &offset);
3411 if (offset != const0_rtx
3412 && (targetm.cannot_force_const_mem (mode, src)
3413 || (!TARGET_MIPS16 && can_create_pseudo_p ())))
3414 {
3415 base = mips_force_temporary (dest, base);
3416 mips_emit_move (dest, mips_add_offset (NULL, base, INTVAL (offset)));
3417 return;
3418 }
3419
3420 src = force_const_mem (mode, src);
3421
3422 /* When using explicit relocs, constant pool references are sometimes
3423 not legitimate addresses. */
3424 mips_split_symbol (dest, XEXP (src, 0), mode, &XEXP (src, 0));
3425 mips_emit_move (dest, src);
3426 }
3427
3428 /* If (set DEST SRC) is not a valid move instruction, emit an equivalent
3429 sequence that is valid. */
3430
3431 bool
3432 mips_legitimize_move (machine_mode mode, rtx dest, rtx src)
3433 {
3434 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
3435 {
3436 mips_emit_move (dest, force_reg (mode, src));
3437 return true;
3438 }
3439
3440 /* We need to deal with constants that would be legitimate
3441 immediate_operands but aren't legitimate move_operands. */
3442 if (CONSTANT_P (src) && !move_operand (src, mode))
3443 {
3444 mips_legitimize_const_move (mode, dest, src);
3445 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
3446 return true;
3447 }
3448 return false;
3449 }
3450 \f
3451 /* Return true if value X in context CONTEXT is a small-data address
3452 that can be rewritten as a LO_SUM. */
3453
3454 static bool
3455 mips_rewrite_small_data_p (rtx x, enum mips_symbol_context context)
3456 {
3457 enum mips_symbol_type symbol_type;
3458
3459 return (mips_lo_relocs[SYMBOL_GP_RELATIVE]
3460 && !mips_split_p[SYMBOL_GP_RELATIVE]
3461 && mips_symbolic_constant_p (x, context, &symbol_type)
3462 && symbol_type == SYMBOL_GP_RELATIVE);
3463 }
3464
3465 /* Return true if OP refers to small data symbols directly, not through
3466 a LO_SUM. CONTEXT is the context in which X appears. */
3467
3468 static int
3469 mips_small_data_pattern_1 (rtx x, enum mips_symbol_context context)
3470 {
3471 subrtx_var_iterator::array_type array;
3472 FOR_EACH_SUBRTX_VAR (iter, array, x, ALL)
3473 {
3474 rtx x = *iter;
3475
3476 /* Ignore things like "g" constraints in asms. We make no particular
3477 guarantee about which symbolic constants are acceptable as asm operands
3478 versus which must be forced into a GPR. */
3479 if (GET_CODE (x) == LO_SUM || GET_CODE (x) == ASM_OPERANDS)
3480 iter.skip_subrtxes ();
3481 else if (MEM_P (x))
3482 {
3483 if (mips_small_data_pattern_1 (XEXP (x, 0), SYMBOL_CONTEXT_MEM))
3484 return true;
3485 iter.skip_subrtxes ();
3486 }
3487 else if (mips_rewrite_small_data_p (x, context))
3488 return true;
3489 }
3490 return false;
3491 }
3492
3493 /* Return true if OP refers to small data symbols directly, not through
3494 a LO_SUM. */
3495
3496 bool
3497 mips_small_data_pattern_p (rtx op)
3498 {
3499 return mips_small_data_pattern_1 (op, SYMBOL_CONTEXT_LEA);
3500 }
3501
3502 /* Rewrite *LOC so that it refers to small data using explicit
3503 relocations. CONTEXT is the context in which *LOC appears. */
3504
3505 static void
3506 mips_rewrite_small_data_1 (rtx *loc, enum mips_symbol_context context)
3507 {
3508 subrtx_ptr_iterator::array_type array;
3509 FOR_EACH_SUBRTX_PTR (iter, array, loc, ALL)
3510 {
3511 rtx *loc = *iter;
3512 if (MEM_P (*loc))
3513 {
3514 mips_rewrite_small_data_1 (&XEXP (*loc, 0), SYMBOL_CONTEXT_MEM);
3515 iter.skip_subrtxes ();
3516 }
3517 else if (mips_rewrite_small_data_p (*loc, context))
3518 {
3519 *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
3520 iter.skip_subrtxes ();
3521 }
3522 else if (GET_CODE (*loc) == LO_SUM)
3523 iter.skip_subrtxes ();
3524 }
3525 }
3526
3527 /* Rewrite instruction pattern PATTERN so that it refers to small data
3528 using explicit relocations. */
3529
3530 rtx
3531 mips_rewrite_small_data (rtx pattern)
3532 {
3533 pattern = copy_insn (pattern);
3534 mips_rewrite_small_data_1 (&pattern, SYMBOL_CONTEXT_LEA);
3535 return pattern;
3536 }
3537 \f
3538 /* The cost of loading values from the constant pool. It should be
3539 larger than the cost of any constant we want to synthesize inline. */
3540 #define CONSTANT_POOL_COST COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 8)
3541
3542 /* Return the cost of X when used as an operand to the MIPS16 instruction
3543 that implements CODE. Return -1 if there is no such instruction, or if
3544 X is not a valid immediate operand for it. */
3545
3546 static int
3547 mips16_constant_cost (int code, HOST_WIDE_INT x)
3548 {
3549 switch (code)
3550 {
3551 case ASHIFT:
3552 case ASHIFTRT:
3553 case LSHIFTRT:
3554 /* Shifts by between 1 and 8 bits (inclusive) are unextended,
3555 other shifts are extended. The shift patterns truncate the shift
3556 count to the right size, so there are no out-of-range values. */
3557 if (IN_RANGE (x, 1, 8))
3558 return 0;
3559 return COSTS_N_INSNS (1);
3560
3561 case PLUS:
3562 if (IN_RANGE (x, -128, 127))
3563 return 0;
3564 if (SMALL_OPERAND (x))
3565 return COSTS_N_INSNS (1);
3566 return -1;
3567
3568 case LEU:
3569 /* Like LE, but reject the always-true case. */
3570 if (x == -1)
3571 return -1;
3572 case LE:
3573 /* We add 1 to the immediate and use SLT. */
3574 x += 1;
3575 case XOR:
3576 /* We can use CMPI for an xor with an unsigned 16-bit X. */
3577 case LT:
3578 case LTU:
3579 if (IN_RANGE (x, 0, 255))
3580 return 0;
3581 if (SMALL_OPERAND_UNSIGNED (x))
3582 return COSTS_N_INSNS (1);
3583 return -1;
3584
3585 case EQ:
3586 case NE:
3587 /* Equality comparisons with 0 are cheap. */
3588 if (x == 0)
3589 return 0;
3590 return -1;
3591
3592 default:
3593 return -1;
3594 }
3595 }
3596
3597 /* Return true if there is a non-MIPS16 instruction that implements CODE
3598 and if that instruction accepts X as an immediate operand. */
3599
3600 static int
3601 mips_immediate_operand_p (int code, HOST_WIDE_INT x)
3602 {
3603 switch (code)
3604 {
3605 case ASHIFT:
3606 case ASHIFTRT:
3607 case LSHIFTRT:
3608 /* All shift counts are truncated to a valid constant. */
3609 return true;
3610
3611 case ROTATE:
3612 case ROTATERT:
3613 /* Likewise rotates, if the target supports rotates at all. */
3614 return ISA_HAS_ROR;
3615
3616 case AND:
3617 case IOR:
3618 case XOR:
3619 /* These instructions take 16-bit unsigned immediates. */
3620 return SMALL_OPERAND_UNSIGNED (x);
3621
3622 case PLUS:
3623 case LT:
3624 case LTU:
3625 /* These instructions take 16-bit signed immediates. */
3626 return SMALL_OPERAND (x);
3627
3628 case EQ:
3629 case NE:
3630 case GT:
3631 case GTU:
3632 /* The "immediate" forms of these instructions are really
3633 implemented as comparisons with register 0. */
3634 return x == 0;
3635
3636 case GE:
3637 case GEU:
3638 /* Likewise, meaning that the only valid immediate operand is 1. */
3639 return x == 1;
3640
3641 case LE:
3642 /* We add 1 to the immediate and use SLT. */
3643 return SMALL_OPERAND (x + 1);
3644
3645 case LEU:
3646 /* Likewise SLTU, but reject the always-true case. */
3647 return SMALL_OPERAND (x + 1) && x + 1 != 0;
3648
3649 case SIGN_EXTRACT:
3650 case ZERO_EXTRACT:
3651 /* The bit position and size are immediate operands. */
3652 return ISA_HAS_EXT_INS;
3653
3654 default:
3655 /* By default assume that $0 can be used for 0. */
3656 return x == 0;
3657 }
3658 }
3659
3660 /* Return the cost of binary operation X, given that the instruction
3661 sequence for a word-sized or smaller operation has cost SINGLE_COST
3662 and that the sequence of a double-word operation has cost DOUBLE_COST.
3663 If SPEED is true, optimize for speed otherwise optimize for size. */
3664
3665 static int
3666 mips_binary_cost (rtx x, int single_cost, int double_cost, bool speed)
3667 {
3668 int cost;
3669
3670 if (GET_MODE_SIZE (GET_MODE (x)) == UNITS_PER_WORD * 2)
3671 cost = double_cost;
3672 else
3673 cost = single_cost;
3674 return (cost
3675 + set_src_cost (XEXP (x, 0), speed)
3676 + rtx_cost (XEXP (x, 1), GET_CODE (x), 1, speed));
3677 }
3678
3679 /* Return the cost of floating-point multiplications of mode MODE. */
3680
3681 static int
3682 mips_fp_mult_cost (machine_mode mode)
3683 {
3684 return mode == DFmode ? mips_cost->fp_mult_df : mips_cost->fp_mult_sf;
3685 }
3686
3687 /* Return the cost of floating-point divisions of mode MODE. */
3688
3689 static int
3690 mips_fp_div_cost (machine_mode mode)
3691 {
3692 return mode == DFmode ? mips_cost->fp_div_df : mips_cost->fp_div_sf;
3693 }
3694
3695 /* Return the cost of sign-extending OP to mode MODE, not including the
3696 cost of OP itself. */
3697
3698 static int
3699 mips_sign_extend_cost (machine_mode mode, rtx op)
3700 {
3701 if (MEM_P (op))
3702 /* Extended loads are as cheap as unextended ones. */
3703 return 0;
3704
3705 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3706 /* A sign extension from SImode to DImode in 64-bit mode is free. */
3707 return 0;
3708
3709 if (ISA_HAS_SEB_SEH || GENERATE_MIPS16E)
3710 /* We can use SEB or SEH. */
3711 return COSTS_N_INSNS (1);
3712
3713 /* We need to use a shift left and a shift right. */
3714 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3715 }
3716
3717 /* Return the cost of zero-extending OP to mode MODE, not including the
3718 cost of OP itself. */
3719
3720 static int
3721 mips_zero_extend_cost (machine_mode mode, rtx op)
3722 {
3723 if (MEM_P (op))
3724 /* Extended loads are as cheap as unextended ones. */
3725 return 0;
3726
3727 if (TARGET_64BIT && mode == DImode && GET_MODE (op) == SImode)
3728 /* We need a shift left by 32 bits and a shift right by 32 bits. */
3729 return COSTS_N_INSNS (TARGET_MIPS16 ? 4 : 2);
3730
3731 if (GENERATE_MIPS16E)
3732 /* We can use ZEB or ZEH. */
3733 return COSTS_N_INSNS (1);
3734
3735 if (TARGET_MIPS16)
3736 /* We need to load 0xff or 0xffff into a register and use AND. */
3737 return COSTS_N_INSNS (GET_MODE (op) == QImode ? 2 : 3);
3738
3739 /* We can use ANDI. */
3740 return COSTS_N_INSNS (1);
3741 }
3742
3743 /* Return the cost of moving between two registers of mode MODE,
3744 assuming that the move will be in pieces of at most UNITS bytes. */
3745
3746 static int
3747 mips_set_reg_reg_piece_cost (machine_mode mode, unsigned int units)
3748 {
3749 return COSTS_N_INSNS ((GET_MODE_SIZE (mode) + units - 1) / units);
3750 }
3751
3752 /* Return the cost of moving between two registers of mode MODE. */
3753
3754 static int
3755 mips_set_reg_reg_cost (machine_mode mode)
3756 {
3757 switch (GET_MODE_CLASS (mode))
3758 {
3759 case MODE_CC:
3760 return mips_set_reg_reg_piece_cost (mode, GET_MODE_SIZE (CCmode));
3761
3762 case MODE_FLOAT:
3763 case MODE_COMPLEX_FLOAT:
3764 case MODE_VECTOR_FLOAT:
3765 if (TARGET_HARD_FLOAT)
3766 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_HWFPVALUE);
3767 /* Fall through */
3768
3769 default:
3770 return mips_set_reg_reg_piece_cost (mode, UNITS_PER_WORD);
3771 }
3772 }
3773
3774 /* Implement TARGET_RTX_COSTS. */
3775
3776 static bool
3777 mips_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
3778 int *total, bool speed)
3779 {
3780 machine_mode mode = GET_MODE (x);
3781 bool float_mode_p = FLOAT_MODE_P (mode);
3782 int cost;
3783 rtx addr;
3784
3785 /* The cost of a COMPARE is hard to define for MIPS. COMPAREs don't
3786 appear in the instruction stream, and the cost of a comparison is
3787 really the cost of the branch or scc condition. At the time of
3788 writing, GCC only uses an explicit outer COMPARE code when optabs
3789 is testing whether a constant is expensive enough to force into a
3790 register. We want optabs to pass such constants through the MIPS
3791 expanders instead, so make all constants very cheap here. */
3792 if (outer_code == COMPARE)
3793 {
3794 gcc_assert (CONSTANT_P (x));
3795 *total = 0;
3796 return true;
3797 }
3798
3799 switch (code)
3800 {
3801 case CONST_INT:
3802 /* Treat *clear_upper32-style ANDs as having zero cost in the
3803 second operand. The cost is entirely in the first operand.
3804
3805 ??? This is needed because we would otherwise try to CSE
3806 the constant operand. Although that's the right thing for
3807 instructions that continue to be a register operation throughout
3808 compilation, it is disastrous for instructions that could
3809 later be converted into a memory operation. */
3810 if (TARGET_64BIT
3811 && outer_code == AND
3812 && UINTVAL (x) == 0xffffffff)
3813 {
3814 *total = 0;
3815 return true;
3816 }
3817
3818 if (TARGET_MIPS16)
3819 {
3820 cost = mips16_constant_cost (outer_code, INTVAL (x));
3821 if (cost >= 0)
3822 {
3823 *total = cost;
3824 return true;
3825 }
3826 }
3827 else
3828 {
3829 /* When not optimizing for size, we care more about the cost
3830 of hot code, and hot code is often in a loop. If a constant
3831 operand needs to be forced into a register, we will often be
3832 able to hoist the constant load out of the loop, so the load
3833 should not contribute to the cost. */
3834 if (speed || mips_immediate_operand_p (outer_code, INTVAL (x)))
3835 {
3836 *total = 0;
3837 return true;
3838 }
3839 }
3840 /* Fall through. */
3841
3842 case CONST:
3843 case SYMBOL_REF:
3844 case LABEL_REF:
3845 case CONST_DOUBLE:
3846 if (force_to_mem_operand (x, VOIDmode))
3847 {
3848 *total = COSTS_N_INSNS (1);
3849 return true;
3850 }
3851 cost = mips_const_insns (x);
3852 if (cost > 0)
3853 {
3854 /* If the constant is likely to be stored in a GPR, SETs of
3855 single-insn constants are as cheap as register sets; we
3856 never want to CSE them.
3857
3858 Don't reduce the cost of storing a floating-point zero in
3859 FPRs. If we have a zero in an FPR for other reasons, we
3860 can get better cfg-cleanup and delayed-branch results by
3861 using it consistently, rather than using $0 sometimes and
3862 an FPR at other times. Also, moves between floating-point
3863 registers are sometimes cheaper than (D)MTC1 $0. */
3864 if (cost == 1
3865 && outer_code == SET
3866 && !(float_mode_p && TARGET_HARD_FLOAT))
3867 cost = 0;
3868 /* When non-MIPS16 code loads a constant N>1 times, we rarely
3869 want to CSE the constant itself. It is usually better to
3870 have N copies of the last operation in the sequence and one
3871 shared copy of the other operations. (Note that this is
3872 not true for MIPS16 code, where the final operation in the
3873 sequence is often an extended instruction.)
3874
3875 Also, if we have a CONST_INT, we don't know whether it is
3876 for a word or doubleword operation, so we cannot rely on
3877 the result of mips_build_integer. */
3878 else if (!TARGET_MIPS16
3879 && (outer_code == SET || mode == VOIDmode))
3880 cost = 1;
3881 *total = COSTS_N_INSNS (cost);
3882 return true;
3883 }
3884 /* The value will need to be fetched from the constant pool. */
3885 *total = CONSTANT_POOL_COST;
3886 return true;
3887
3888 case MEM:
3889 /* If the address is legitimate, return the number of
3890 instructions it needs. */
3891 addr = XEXP (x, 0);
3892 cost = mips_address_insns (addr, mode, true);
3893 if (cost > 0)
3894 {
3895 *total = COSTS_N_INSNS (cost + 1);
3896 return true;
3897 }
3898 /* Check for a scaled indexed address. */
3899 if (mips_lwxs_address_p (addr)
3900 || mips_lx_address_p (addr, mode))
3901 {
3902 *total = COSTS_N_INSNS (2);
3903 return true;
3904 }
3905 /* Otherwise use the default handling. */
3906 return false;
3907
3908 case FFS:
3909 *total = COSTS_N_INSNS (6);
3910 return false;
3911
3912 case NOT:
3913 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1);
3914 return false;
3915
3916 case AND:
3917 /* Check for a *clear_upper32 pattern and treat it like a zero
3918 extension. See the pattern's comment for details. */
3919 if (TARGET_64BIT
3920 && mode == DImode
3921 && CONST_INT_P (XEXP (x, 1))
3922 && UINTVAL (XEXP (x, 1)) == 0xffffffff)
3923 {
3924 *total = (mips_zero_extend_cost (mode, XEXP (x, 0))
3925 + set_src_cost (XEXP (x, 0), speed));
3926 return true;
3927 }
3928 if (ISA_HAS_CINS && CONST_INT_P (XEXP (x, 1)))
3929 {
3930 rtx op = XEXP (x, 0);
3931 if (GET_CODE (op) == ASHIFT
3932 && CONST_INT_P (XEXP (op, 1))
3933 && mask_low_and_shift_p (mode, XEXP (x, 1), XEXP (op, 1), 32))
3934 {
3935 *total = COSTS_N_INSNS (1) + set_src_cost (XEXP (op, 0), speed);
3936 return true;
3937 }
3938 }
3939 /* (AND (NOT op0) (NOT op1) is a nor operation that can be done in
3940 a single instruction. */
3941 if (!TARGET_MIPS16
3942 && GET_CODE (XEXP (x, 0)) == NOT
3943 && GET_CODE (XEXP (x, 1)) == NOT)
3944 {
3945 cost = GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 2 : 1;
3946 *total = (COSTS_N_INSNS (cost)
3947 + set_src_cost (XEXP (XEXP (x, 0), 0), speed)
3948 + set_src_cost (XEXP (XEXP (x, 1), 0), speed));
3949 return true;
3950 }
3951
3952 /* Fall through. */
3953
3954 case IOR:
3955 case XOR:
3956 /* Double-word operations use two single-word operations. */
3957 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (2),
3958 speed);
3959 return true;
3960
3961 case ASHIFT:
3962 case ASHIFTRT:
3963 case LSHIFTRT:
3964 case ROTATE:
3965 case ROTATERT:
3966 if (CONSTANT_P (XEXP (x, 1)))
3967 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
3968 speed);
3969 else
3970 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (12),
3971 speed);
3972 return true;
3973
3974 case ABS:
3975 if (float_mode_p)
3976 *total = mips_cost->fp_add;
3977 else
3978 *total = COSTS_N_INSNS (4);
3979 return false;
3980
3981 case LO_SUM:
3982 /* Low-part immediates need an extended MIPS16 instruction. */
3983 *total = (COSTS_N_INSNS (TARGET_MIPS16 ? 2 : 1)
3984 + set_src_cost (XEXP (x, 0), speed));
3985 return true;
3986
3987 case LT:
3988 case LTU:
3989 case LE:
3990 case LEU:
3991 case GT:
3992 case GTU:
3993 case GE:
3994 case GEU:
3995 case EQ:
3996 case NE:
3997 case UNORDERED:
3998 case LTGT:
3999 /* Branch comparisons have VOIDmode, so use the first operand's
4000 mode instead. */
4001 mode = GET_MODE (XEXP (x, 0));
4002 if (FLOAT_MODE_P (mode))
4003 {
4004 *total = mips_cost->fp_add;
4005 return false;
4006 }
4007 *total = mips_binary_cost (x, COSTS_N_INSNS (1), COSTS_N_INSNS (4),
4008 speed);
4009 return true;
4010
4011 case MINUS:
4012 if (float_mode_p
4013 && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
4014 && TARGET_FUSED_MADD
4015 && !HONOR_NANS (mode)
4016 && !HONOR_SIGNED_ZEROS (mode))
4017 {
4018 /* See if we can use NMADD or NMSUB. See mips.md for the
4019 associated patterns. */
4020 rtx op0 = XEXP (x, 0);
4021 rtx op1 = XEXP (x, 1);
4022 if (GET_CODE (op0) == MULT && GET_CODE (XEXP (op0, 0)) == NEG)
4023 {
4024 *total = (mips_fp_mult_cost (mode)
4025 + set_src_cost (XEXP (XEXP (op0, 0), 0), speed)
4026 + set_src_cost (XEXP (op0, 1), speed)
4027 + set_src_cost (op1, speed));
4028 return true;
4029 }
4030 if (GET_CODE (op1) == MULT)
4031 {
4032 *total = (mips_fp_mult_cost (mode)
4033 + set_src_cost (op0, speed)
4034 + set_src_cost (XEXP (op1, 0), speed)
4035 + set_src_cost (XEXP (op1, 1), speed));
4036 return true;
4037 }
4038 }
4039 /* Fall through. */
4040
4041 case PLUS:
4042 if (float_mode_p)
4043 {
4044 /* If this is part of a MADD or MSUB, treat the PLUS as
4045 being free. */
4046 if ((ISA_HAS_FP_MADD4_MSUB4 || ISA_HAS_FP_MADD3_MSUB3)
4047 && TARGET_FUSED_MADD
4048 && GET_CODE (XEXP (x, 0)) == MULT)
4049 *total = 0;
4050 else
4051 *total = mips_cost->fp_add;
4052 return false;
4053 }
4054
4055 /* Double-word operations require three single-word operations and
4056 an SLTU. The MIPS16 version then needs to move the result of
4057 the SLTU from $24 to a MIPS16 register. */
4058 *total = mips_binary_cost (x, COSTS_N_INSNS (1),
4059 COSTS_N_INSNS (TARGET_MIPS16 ? 5 : 4),
4060 speed);
4061 return true;
4062
4063 case NEG:
4064 if (float_mode_p
4065 && (ISA_HAS_NMADD4_NMSUB4 || ISA_HAS_NMADD3_NMSUB3)
4066 && TARGET_FUSED_MADD
4067 && !HONOR_NANS (mode)
4068 && HONOR_SIGNED_ZEROS (mode))
4069 {
4070 /* See if we can use NMADD or NMSUB. See mips.md for the
4071 associated patterns. */
4072 rtx op = XEXP (x, 0);
4073 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
4074 && GET_CODE (XEXP (op, 0)) == MULT)
4075 {
4076 *total = (mips_fp_mult_cost (mode)
4077 + set_src_cost (XEXP (XEXP (op, 0), 0), speed)
4078 + set_src_cost (XEXP (XEXP (op, 0), 1), speed)
4079 + set_src_cost (XEXP (op, 1), speed));
4080 return true;
4081 }
4082 }
4083
4084 if (float_mode_p)
4085 *total = mips_cost->fp_add;
4086 else
4087 *total = COSTS_N_INSNS (GET_MODE_SIZE (mode) > UNITS_PER_WORD ? 4 : 1);
4088 return false;
4089
4090 case MULT:
4091 if (float_mode_p)
4092 *total = mips_fp_mult_cost (mode);
4093 else if (mode == DImode && !TARGET_64BIT)
4094 /* Synthesized from 2 mulsi3s, 1 mulsidi3 and two additions,
4095 where the mulsidi3 always includes an MFHI and an MFLO. */
4096 *total = (speed
4097 ? mips_cost->int_mult_si * 3 + 6
4098 : COSTS_N_INSNS (ISA_HAS_MUL3 ? 7 : 9));
4099 else if (!speed)
4100 *total = COSTS_N_INSNS (ISA_HAS_MUL3 ? 1 : 2) + 1;
4101 else if (mode == DImode)
4102 *total = mips_cost->int_mult_di;
4103 else
4104 *total = mips_cost->int_mult_si;
4105 return false;
4106
4107 case DIV:
4108 /* Check for a reciprocal. */
4109 if (float_mode_p
4110 && ISA_HAS_FP_RECIP_RSQRT (mode)
4111 && flag_unsafe_math_optimizations
4112 && XEXP (x, 0) == CONST1_RTX (mode))
4113 {
4114 if (outer_code == SQRT || GET_CODE (XEXP (x, 1)) == SQRT)
4115 /* An rsqrt<mode>a or rsqrt<mode>b pattern. Count the
4116 division as being free. */
4117 *total = set_src_cost (XEXP (x, 1), speed);
4118 else
4119 *total = (mips_fp_div_cost (mode)
4120 + set_src_cost (XEXP (x, 1), speed));
4121 return true;
4122 }
4123 /* Fall through. */
4124
4125 case SQRT:
4126 case MOD:
4127 if (float_mode_p)
4128 {
4129 *total = mips_fp_div_cost (mode);
4130 return false;
4131 }
4132 /* Fall through. */
4133
4134 case UDIV:
4135 case UMOD:
4136 if (!speed)
4137 {
4138 /* It is our responsibility to make division by a power of 2
4139 as cheap as 2 register additions if we want the division
4140 expanders to be used for such operations; see the setting
4141 of sdiv_pow2_cheap in optabs.c. Using (D)DIV for MIPS16
4142 should always produce shorter code than using
4143 expand_sdiv2_pow2. */
4144 if (TARGET_MIPS16
4145 && CONST_INT_P (XEXP (x, 1))
4146 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0)
4147 {
4148 *total = COSTS_N_INSNS (2) + set_src_cost (XEXP (x, 0), speed);
4149 return true;
4150 }
4151 *total = COSTS_N_INSNS (mips_idiv_insns ());
4152 }
4153 else if (mode == DImode)
4154 *total = mips_cost->int_div_di;
4155 else
4156 *total = mips_cost->int_div_si;
4157 return false;
4158
4159 case SIGN_EXTEND:
4160 *total = mips_sign_extend_cost (mode, XEXP (x, 0));
4161 return false;
4162
4163 case ZERO_EXTEND:
4164 if (outer_code == SET
4165 && ISA_HAS_BADDU
4166 && (GET_CODE (XEXP (x, 0)) == TRUNCATE
4167 || GET_CODE (XEXP (x, 0)) == SUBREG)
4168 && GET_MODE (XEXP (x, 0)) == QImode
4169 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS)
4170 {
4171 *total = set_src_cost (XEXP (XEXP (x, 0), 0), speed);
4172 return true;
4173 }
4174 *total = mips_zero_extend_cost (mode, XEXP (x, 0));
4175 return false;
4176
4177 case FLOAT:
4178 case UNSIGNED_FLOAT:
4179 case FIX:
4180 case FLOAT_EXTEND:
4181 case FLOAT_TRUNCATE:
4182 *total = mips_cost->fp_add;
4183 return false;
4184
4185 case SET:
4186 if (register_operand (SET_DEST (x), VOIDmode)
4187 && reg_or_0_operand (SET_SRC (x), VOIDmode))
4188 {
4189 *total = mips_set_reg_reg_cost (GET_MODE (SET_DEST (x)));
4190 return true;
4191 }
4192 return false;
4193
4194 default:
4195 return false;
4196 }
4197 }
4198
4199 /* Implement TARGET_ADDRESS_COST. */
4200
4201 static int
4202 mips_address_cost (rtx addr, machine_mode mode,
4203 addr_space_t as ATTRIBUTE_UNUSED,
4204 bool speed ATTRIBUTE_UNUSED)
4205 {
4206 return mips_address_insns (addr, mode, false);
4207 }
4208 \f
4209 /* Information about a single instruction in a multi-instruction
4210 asm sequence. */
4211 struct mips_multi_member {
4212 /* True if this is a label, false if it is code. */
4213 bool is_label_p;
4214
4215 /* The output_asm_insn format of the instruction. */
4216 const char *format;
4217
4218 /* The operands to the instruction. */
4219 rtx operands[MAX_RECOG_OPERANDS];
4220 };
4221 typedef struct mips_multi_member mips_multi_member;
4222
4223 /* The instructions that make up the current multi-insn sequence. */
4224 static vec<mips_multi_member> mips_multi_members;
4225
4226 /* How many instructions (as opposed to labels) are in the current
4227 multi-insn sequence. */
4228 static unsigned int mips_multi_num_insns;
4229
4230 /* Start a new multi-insn sequence. */
4231
4232 static void
4233 mips_multi_start (void)
4234 {
4235 mips_multi_members.truncate (0);
4236 mips_multi_num_insns = 0;
4237 }
4238
4239 /* Add a new, uninitialized member to the current multi-insn sequence. */
4240
4241 static struct mips_multi_member *
4242 mips_multi_add (void)
4243 {
4244 mips_multi_member empty;
4245 return mips_multi_members.safe_push (empty);
4246 }
4247
4248 /* Add a normal insn with the given asm format to the current multi-insn
4249 sequence. The other arguments are a null-terminated list of operands. */
4250
4251 static void
4252 mips_multi_add_insn (const char *format, ...)
4253 {
4254 struct mips_multi_member *member;
4255 va_list ap;
4256 unsigned int i;
4257 rtx op;
4258
4259 member = mips_multi_add ();
4260 member->is_label_p = false;
4261 member->format = format;
4262 va_start (ap, format);
4263 i = 0;
4264 while ((op = va_arg (ap, rtx)))
4265 member->operands[i++] = op;
4266 va_end (ap);
4267 mips_multi_num_insns++;
4268 }
4269
4270 /* Add the given label definition to the current multi-insn sequence.
4271 The definition should include the colon. */
4272
4273 static void
4274 mips_multi_add_label (const char *label)
4275 {
4276 struct mips_multi_member *member;
4277
4278 member = mips_multi_add ();
4279 member->is_label_p = true;
4280 member->format = label;
4281 }
4282
4283 /* Return the index of the last member of the current multi-insn sequence. */
4284
4285 static unsigned int
4286 mips_multi_last_index (void)
4287 {
4288 return mips_multi_members.length () - 1;
4289 }
4290
4291 /* Add a copy of an existing instruction to the current multi-insn
4292 sequence. I is the index of the instruction that should be copied. */
4293
4294 static void
4295 mips_multi_copy_insn (unsigned int i)
4296 {
4297 struct mips_multi_member *member;
4298
4299 member = mips_multi_add ();
4300 memcpy (member, &mips_multi_members[i], sizeof (*member));
4301 gcc_assert (!member->is_label_p);
4302 }
4303
4304 /* Change the operand of an existing instruction in the current
4305 multi-insn sequence. I is the index of the instruction,
4306 OP is the index of the operand, and X is the new value. */
4307
4308 static void
4309 mips_multi_set_operand (unsigned int i, unsigned int op, rtx x)
4310 {
4311 mips_multi_members[i].operands[op] = x;
4312 }
4313
4314 /* Write out the asm code for the current multi-insn sequence. */
4315
4316 static void
4317 mips_multi_write (void)
4318 {
4319 struct mips_multi_member *member;
4320 unsigned int i;
4321
4322 FOR_EACH_VEC_ELT (mips_multi_members, i, member)
4323 if (member->is_label_p)
4324 fprintf (asm_out_file, "%s\n", member->format);
4325 else
4326 output_asm_insn (member->format, member->operands);
4327 }
4328 \f
4329 /* Return one word of double-word value OP, taking into account the fixed
4330 endianness of certain registers. HIGH_P is true to select the high part,
4331 false to select the low part. */
4332
4333 rtx
4334 mips_subword (rtx op, bool high_p)
4335 {
4336 unsigned int byte, offset;
4337 machine_mode mode;
4338
4339 mode = GET_MODE (op);
4340 if (mode == VOIDmode)
4341 mode = TARGET_64BIT ? TImode : DImode;
4342
4343 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
4344 byte = UNITS_PER_WORD;
4345 else
4346 byte = 0;
4347
4348 if (FP_REG_RTX_P (op))
4349 {
4350 /* Paired FPRs are always ordered little-endian. */
4351 offset = (UNITS_PER_WORD < UNITS_PER_HWFPVALUE ? high_p : byte != 0);
4352 return gen_rtx_REG (word_mode, REGNO (op) + offset);
4353 }
4354
4355 if (MEM_P (op))
4356 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
4357
4358 return simplify_gen_subreg (word_mode, op, mode, byte);
4359 }
4360
4361 /* Return true if SRC should be moved into DEST using "MULT $0, $0".
4362 SPLIT_TYPE is the condition under which moves should be split. */
4363
4364 static bool
4365 mips_mult_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4366 {
4367 return ((split_type != SPLIT_FOR_SPEED
4368 || mips_tuning_info.fast_mult_zero_zero_p)
4369 && src == const0_rtx
4370 && REG_P (dest)
4371 && GET_MODE_SIZE (GET_MODE (dest)) == 2 * UNITS_PER_WORD
4372 && (ISA_HAS_DSP_MULT
4373 ? ACC_REG_P (REGNO (dest))
4374 : MD_REG_P (REGNO (dest))));
4375 }
4376
4377 /* Return true if a move from SRC to DEST should be split into two.
4378 SPLIT_TYPE describes the split condition. */
4379
4380 bool
4381 mips_split_move_p (rtx dest, rtx src, enum mips_split_type split_type)
4382 {
4383 /* Check whether the move can be done using some variant of MULT $0,$0. */
4384 if (mips_mult_move_p (dest, src, split_type))
4385 return false;
4386
4387 /* FPR-to-FPR moves can be done in a single instruction, if they're
4388 allowed at all. */
4389 unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
4390 if (size == 8 && FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
4391 return false;
4392
4393 /* Check for floating-point loads and stores. */
4394 if (size == 8 && ISA_HAS_LDC1_SDC1)
4395 {
4396 if (FP_REG_RTX_P (dest) && MEM_P (src))
4397 return false;
4398 if (FP_REG_RTX_P (src) && MEM_P (dest))
4399 return false;
4400 }
4401
4402 /* Otherwise split all multiword moves. */
4403 return size > UNITS_PER_WORD;
4404 }
4405
4406 /* Split a move from SRC to DEST, given that mips_split_move_p holds.
4407 SPLIT_TYPE describes the split condition. */
4408
4409 void
4410 mips_split_move (rtx dest, rtx src, enum mips_split_type split_type)
4411 {
4412 rtx low_dest;
4413
4414 gcc_checking_assert (mips_split_move_p (dest, src, split_type));
4415 if (FP_REG_RTX_P (dest) || FP_REG_RTX_P (src))
4416 {
4417 if (!TARGET_64BIT && GET_MODE (dest) == DImode)
4418 emit_insn (gen_move_doubleword_fprdi (dest, src));
4419 else if (!TARGET_64BIT && GET_MODE (dest) == DFmode)
4420 emit_insn (gen_move_doubleword_fprdf (dest, src));
4421 else if (!TARGET_64BIT && GET_MODE (dest) == V2SFmode)
4422 emit_insn (gen_move_doubleword_fprv2sf (dest, src));
4423 else if (!TARGET_64BIT && GET_MODE (dest) == V2SImode)
4424 emit_insn (gen_move_doubleword_fprv2si (dest, src));
4425 else if (!TARGET_64BIT && GET_MODE (dest) == V4HImode)
4426 emit_insn (gen_move_doubleword_fprv4hi (dest, src));
4427 else if (!TARGET_64BIT && GET_MODE (dest) == V8QImode)
4428 emit_insn (gen_move_doubleword_fprv8qi (dest, src));
4429 else if (TARGET_64BIT && GET_MODE (dest) == TFmode)
4430 emit_insn (gen_move_doubleword_fprtf (dest, src));
4431 else
4432 gcc_unreachable ();
4433 }
4434 else if (REG_P (dest) && REGNO (dest) == MD_REG_FIRST)
4435 {
4436 low_dest = mips_subword (dest, false);
4437 mips_emit_move (low_dest, mips_subword (src, false));
4438 if (TARGET_64BIT)
4439 emit_insn (gen_mthidi_ti (dest, mips_subword (src, true), low_dest));
4440 else
4441 emit_insn (gen_mthisi_di (dest, mips_subword (src, true), low_dest));
4442 }
4443 else if (REG_P (src) && REGNO (src) == MD_REG_FIRST)
4444 {
4445 mips_emit_move (mips_subword (dest, false), mips_subword (src, false));
4446 if (TARGET_64BIT)
4447 emit_insn (gen_mfhidi_ti (mips_subword (dest, true), src));
4448 else
4449 emit_insn (gen_mfhisi_di (mips_subword (dest, true), src));
4450 }
4451 else
4452 {
4453 /* The operation can be split into two normal moves. Decide in
4454 which order to do them. */
4455 low_dest = mips_subword (dest, false);
4456 if (REG_P (low_dest)
4457 && reg_overlap_mentioned_p (low_dest, src))
4458 {
4459 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4460 mips_emit_move (low_dest, mips_subword (src, false));
4461 }
4462 else
4463 {
4464 mips_emit_move (low_dest, mips_subword (src, false));
4465 mips_emit_move (mips_subword (dest, true), mips_subword (src, true));
4466 }
4467 }
4468 }
4469
4470 /* Return the split type for instruction INSN. */
4471
4472 static enum mips_split_type
4473 mips_insn_split_type (rtx insn)
4474 {
4475 basic_block bb = BLOCK_FOR_INSN (insn);
4476 if (bb)
4477 {
4478 if (optimize_bb_for_speed_p (bb))
4479 return SPLIT_FOR_SPEED;
4480 else
4481 return SPLIT_FOR_SIZE;
4482 }
4483 /* Once CFG information has been removed, we should trust the optimization
4484 decisions made by previous passes and only split where necessary. */
4485 return SPLIT_IF_NECESSARY;
4486 }
4487
4488 /* Return true if a move from SRC to DEST in INSN should be split. */
4489
4490 bool
4491 mips_split_move_insn_p (rtx dest, rtx src, rtx insn)
4492 {
4493 return mips_split_move_p (dest, src, mips_insn_split_type (insn));
4494 }
4495
4496 /* Split a move from SRC to DEST in INSN, given that mips_split_move_insn_p
4497 holds. */
4498
4499 void
4500 mips_split_move_insn (rtx dest, rtx src, rtx insn)
4501 {
4502 mips_split_move (dest, src, mips_insn_split_type (insn));
4503 }
4504 \f
4505 /* Return the appropriate instructions to move SRC into DEST. Assume
4506 that SRC is operand 1 and DEST is operand 0. */
4507
4508 const char *
4509 mips_output_move (rtx dest, rtx src)
4510 {
4511 enum rtx_code dest_code, src_code;
4512 machine_mode mode;
4513 enum mips_symbol_type symbol_type;
4514 bool dbl_p;
4515
4516 dest_code = GET_CODE (dest);
4517 src_code = GET_CODE (src);
4518 mode = GET_MODE (dest);
4519 dbl_p = (GET_MODE_SIZE (mode) == 8);
4520
4521 if (mips_split_move_p (dest, src, SPLIT_IF_NECESSARY))
4522 return "#";
4523
4524 if ((src_code == REG && GP_REG_P (REGNO (src)))
4525 || (!TARGET_MIPS16 && src == CONST0_RTX (mode)))
4526 {
4527 if (dest_code == REG)
4528 {
4529 if (GP_REG_P (REGNO (dest)))
4530 return "move\t%0,%z1";
4531
4532 if (mips_mult_move_p (dest, src, SPLIT_IF_NECESSARY))
4533 {
4534 if (ISA_HAS_DSP_MULT)
4535 return "mult\t%q0,%.,%.";
4536 else
4537 return "mult\t%.,%.";
4538 }
4539
4540 /* Moves to HI are handled by special .md insns. */
4541 if (REGNO (dest) == LO_REGNUM)
4542 return "mtlo\t%z1";
4543
4544 if (DSP_ACC_REG_P (REGNO (dest)))
4545 {
4546 static char retval[] = "mt__\t%z1,%q0";
4547
4548 retval[2] = reg_names[REGNO (dest)][4];
4549 retval[3] = reg_names[REGNO (dest)][5];
4550 return retval;
4551 }
4552
4553 if (FP_REG_P (REGNO (dest)))
4554 return dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0";
4555
4556 if (ALL_COP_REG_P (REGNO (dest)))
4557 {
4558 static char retval[] = "dmtc_\t%z1,%0";
4559
4560 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4561 return dbl_p ? retval : retval + 1;
4562 }
4563 }
4564 if (dest_code == MEM)
4565 switch (GET_MODE_SIZE (mode))
4566 {
4567 case 1: return "sb\t%z1,%0";
4568 case 2: return "sh\t%z1,%0";
4569 case 4: return "sw\t%z1,%0";
4570 case 8: return "sd\t%z1,%0";
4571 }
4572 }
4573 if (dest_code == REG && GP_REG_P (REGNO (dest)))
4574 {
4575 if (src_code == REG)
4576 {
4577 /* Moves from HI are handled by special .md insns. */
4578 if (REGNO (src) == LO_REGNUM)
4579 {
4580 /* When generating VR4120 or VR4130 code, we use MACC and
4581 DMACC instead of MFLO. This avoids both the normal
4582 MIPS III HI/LO hazards and the errata related to
4583 -mfix-vr4130. */
4584 if (ISA_HAS_MACCHI)
4585 return dbl_p ? "dmacc\t%0,%.,%." : "macc\t%0,%.,%.";
4586 return "mflo\t%0";
4587 }
4588
4589 if (DSP_ACC_REG_P (REGNO (src)))
4590 {
4591 static char retval[] = "mf__\t%0,%q1";
4592
4593 retval[2] = reg_names[REGNO (src)][4];
4594 retval[3] = reg_names[REGNO (src)][5];
4595 return retval;
4596 }
4597
4598 if (FP_REG_P (REGNO (src)))
4599 return dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1";
4600
4601 if (ALL_COP_REG_P (REGNO (src)))
4602 {
4603 static char retval[] = "dmfc_\t%0,%1";
4604
4605 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4606 return dbl_p ? retval : retval + 1;
4607 }
4608 }
4609
4610 if (src_code == MEM)
4611 switch (GET_MODE_SIZE (mode))
4612 {
4613 case 1: return "lbu\t%0,%1";
4614 case 2: return "lhu\t%0,%1";
4615 case 4: return "lw\t%0,%1";
4616 case 8: return "ld\t%0,%1";
4617 }
4618
4619 if (src_code == CONST_INT)
4620 {
4621 /* Don't use the X format for the operand itself, because that
4622 will give out-of-range numbers for 64-bit hosts and 32-bit
4623 targets. */
4624 if (!TARGET_MIPS16)
4625 return "li\t%0,%1\t\t\t# %X1";
4626
4627 if (SMALL_OPERAND_UNSIGNED (INTVAL (src)))
4628 return "li\t%0,%1";
4629
4630 if (SMALL_OPERAND_UNSIGNED (-INTVAL (src)))
4631 return "#";
4632 }
4633
4634 if (src_code == HIGH)
4635 return TARGET_MIPS16 ? "#" : "lui\t%0,%h1";
4636
4637 if (CONST_GP_P (src))
4638 return "move\t%0,%1";
4639
4640 if (mips_symbolic_constant_p (src, SYMBOL_CONTEXT_LEA, &symbol_type)
4641 && mips_lo_relocs[symbol_type] != 0)
4642 {
4643 /* A signed 16-bit constant formed by applying a relocation
4644 operator to a symbolic address. */
4645 gcc_assert (!mips_split_p[symbol_type]);
4646 return "li\t%0,%R1";
4647 }
4648
4649 if (symbolic_operand (src, VOIDmode))
4650 {
4651 gcc_assert (TARGET_MIPS16
4652 ? TARGET_MIPS16_TEXT_LOADS
4653 : !TARGET_EXPLICIT_RELOCS);
4654 return dbl_p ? "dla\t%0,%1" : "la\t%0,%1";
4655 }
4656 }
4657 if (src_code == REG && FP_REG_P (REGNO (src)))
4658 {
4659 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4660 {
4661 if (GET_MODE (dest) == V2SFmode)
4662 return "mov.ps\t%0,%1";
4663 else
4664 return dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1";
4665 }
4666
4667 if (dest_code == MEM)
4668 return dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0";
4669 }
4670 if (dest_code == REG && FP_REG_P (REGNO (dest)))
4671 {
4672 if (src_code == MEM)
4673 return dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1";
4674 }
4675 if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
4676 {
4677 static char retval[] = "l_c_\t%0,%1";
4678
4679 retval[1] = (dbl_p ? 'd' : 'w');
4680 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
4681 return retval;
4682 }
4683 if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
4684 {
4685 static char retval[] = "s_c_\t%1,%0";
4686
4687 retval[1] = (dbl_p ? 'd' : 'w');
4688 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
4689 return retval;
4690 }
4691 gcc_unreachable ();
4692 }
4693 \f
4694 /* Return true if CMP1 is a suitable second operand for integer ordering
4695 test CODE. See also the *sCC patterns in mips.md. */
4696
4697 static bool
4698 mips_int_order_operand_ok_p (enum rtx_code code, rtx cmp1)
4699 {
4700 switch (code)
4701 {
4702 case GT:
4703 case GTU:
4704 return reg_or_0_operand (cmp1, VOIDmode);
4705
4706 case GE:
4707 case GEU:
4708 return !TARGET_MIPS16 && cmp1 == const1_rtx;
4709
4710 case LT:
4711 case LTU:
4712 return arith_operand (cmp1, VOIDmode);
4713
4714 case LE:
4715 return sle_operand (cmp1, VOIDmode);
4716
4717 case LEU:
4718 return sleu_operand (cmp1, VOIDmode);
4719
4720 default:
4721 gcc_unreachable ();
4722 }
4723 }
4724
4725 /* Return true if *CMP1 (of mode MODE) is a valid second operand for
4726 integer ordering test *CODE, or if an equivalent combination can
4727 be formed by adjusting *CODE and *CMP1. When returning true, update
4728 *CODE and *CMP1 with the chosen code and operand, otherwise leave
4729 them alone. */
4730
4731 static bool
4732 mips_canonicalize_int_order_test (enum rtx_code *code, rtx *cmp1,
4733 machine_mode mode)
4734 {
4735 HOST_WIDE_INT plus_one;
4736
4737 if (mips_int_order_operand_ok_p (*code, *cmp1))
4738 return true;
4739
4740 if (CONST_INT_P (*cmp1))
4741 switch (*code)
4742 {
4743 case LE:
4744 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4745 if (INTVAL (*cmp1) < plus_one)
4746 {
4747 *code = LT;
4748 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4749 return true;
4750 }
4751 break;
4752
4753 case LEU:
4754 plus_one = trunc_int_for_mode (UINTVAL (*cmp1) + 1, mode);
4755 if (plus_one != 0)
4756 {
4757 *code = LTU;
4758 *cmp1 = force_reg (mode, GEN_INT (plus_one));
4759 return true;
4760 }
4761 break;
4762
4763 default:
4764 break;
4765 }
4766 return false;
4767 }
4768
4769 /* Compare CMP0 and CMP1 using ordering test CODE and store the result
4770 in TARGET. CMP0 and TARGET are register_operands. If INVERT_PTR
4771 is nonnull, it's OK to set TARGET to the inverse of the result and
4772 flip *INVERT_PTR instead. */
4773
4774 static void
4775 mips_emit_int_order_test (enum rtx_code code, bool *invert_ptr,
4776 rtx target, rtx cmp0, rtx cmp1)
4777 {
4778 machine_mode mode;
4779
4780 /* First see if there is a MIPS instruction that can do this operation.
4781 If not, try doing the same for the inverse operation. If that also
4782 fails, force CMP1 into a register and try again. */
4783 mode = GET_MODE (cmp0);
4784 if (mips_canonicalize_int_order_test (&code, &cmp1, mode))
4785 mips_emit_binary (code, target, cmp0, cmp1);
4786 else
4787 {
4788 enum rtx_code inv_code = reverse_condition (code);
4789 if (!mips_canonicalize_int_order_test (&inv_code, &cmp1, mode))
4790 {
4791 cmp1 = force_reg (mode, cmp1);
4792 mips_emit_int_order_test (code, invert_ptr, target, cmp0, cmp1);
4793 }
4794 else if (invert_ptr == 0)
4795 {
4796 rtx inv_target;
4797
4798 inv_target = mips_force_binary (GET_MODE (target),
4799 inv_code, cmp0, cmp1);
4800 mips_emit_binary (XOR, target, inv_target, const1_rtx);
4801 }
4802 else
4803 {
4804 *invert_ptr = !*invert_ptr;
4805 mips_emit_binary (inv_code, target, cmp0, cmp1);
4806 }
4807 }
4808 }
4809
4810 /* Return a register that is zero iff CMP0 and CMP1 are equal.
4811 The register will have the same mode as CMP0. */
4812
4813 static rtx
4814 mips_zero_if_equal (rtx cmp0, rtx cmp1)
4815 {
4816 if (cmp1 == const0_rtx)
4817 return cmp0;
4818
4819 if (uns_arith_operand (cmp1, VOIDmode))
4820 return expand_binop (GET_MODE (cmp0), xor_optab,
4821 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4822
4823 return expand_binop (GET_MODE (cmp0), sub_optab,
4824 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
4825 }
4826
4827 /* Convert *CODE into a code that can be used in a floating-point
4828 scc instruction (C.cond.fmt). Return true if the values of
4829 the condition code registers will be inverted, with 0 indicating
4830 that the condition holds. */
4831
4832 static bool
4833 mips_reversed_fp_cond (enum rtx_code *code)
4834 {
4835 switch (*code)
4836 {
4837 case NE:
4838 case LTGT:
4839 case ORDERED:
4840 *code = reverse_condition_maybe_unordered (*code);
4841 return true;
4842
4843 default:
4844 return false;
4845 }
4846 }
4847
4848 /* Allocate a floating-point condition-code register of mode MODE.
4849
4850 These condition code registers are used for certain kinds
4851 of compound operation, such as compare and branches, vconds,
4852 and built-in functions. At expand time, their use is entirely
4853 controlled by MIPS-specific code and is entirely internal
4854 to these compound operations.
4855
4856 We could (and did in the past) expose condition-code values
4857 as pseudo registers and leave the register allocator to pick
4858 appropriate registers. The problem is that it is not practically
4859 possible for the rtl optimizers to guarantee that no spills will
4860 be needed, even when AVOID_CCMODE_COPIES is defined. We would
4861 therefore need spill and reload sequences to handle the worst case.
4862
4863 Although such sequences do exist, they are very expensive and are
4864 not something we'd want to use. This is especially true of CCV2 and
4865 CCV4, where all the shuffling would greatly outweigh whatever benefit
4866 the vectorization itself provides.
4867
4868 The main benefit of having more than one condition-code register
4869 is to allow the pipelining of operations, especially those involving
4870 comparisons and conditional moves. We don't really expect the
4871 registers to be live for long periods, and certainly never want
4872 them to be live across calls.
4873
4874 Also, there should be no penalty attached to using all the available
4875 registers. They are simply bits in the same underlying FPU control
4876 register.
4877
4878 We therefore expose the hardware registers from the outset and use
4879 a simple round-robin allocation scheme. */
4880
4881 static rtx
4882 mips_allocate_fcc (machine_mode mode)
4883 {
4884 unsigned int regno, count;
4885
4886 gcc_assert (TARGET_HARD_FLOAT && ISA_HAS_8CC);
4887
4888 if (mode == CCmode)
4889 count = 1;
4890 else if (mode == CCV2mode)
4891 count = 2;
4892 else if (mode == CCV4mode)
4893 count = 4;
4894 else
4895 gcc_unreachable ();
4896
4897 cfun->machine->next_fcc += -cfun->machine->next_fcc & (count - 1);
4898 if (cfun->machine->next_fcc > ST_REG_LAST - ST_REG_FIRST)
4899 cfun->machine->next_fcc = 0;
4900 regno = ST_REG_FIRST + cfun->machine->next_fcc;
4901 cfun->machine->next_fcc += count;
4902 return gen_rtx_REG (mode, regno);
4903 }
4904
4905 /* Convert a comparison into something that can be used in a branch or
4906 conditional move. On entry, *OP0 and *OP1 are the values being
4907 compared and *CODE is the code used to compare them.
4908
4909 Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
4910 If NEED_EQ_NE_P, then only EQ or NE comparisons against zero are possible,
4911 otherwise any standard branch condition can be used. The standard branch
4912 conditions are:
4913
4914 - EQ or NE between two registers.
4915 - any comparison between a register and zero. */
4916
4917 static void
4918 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
4919 {
4920 rtx cmp_op0 = *op0;
4921 rtx cmp_op1 = *op1;
4922
4923 if (GET_MODE_CLASS (GET_MODE (*op0)) == MODE_INT)
4924 {
4925 if (!need_eq_ne_p && *op1 == const0_rtx)
4926 ;
4927 else if (*code == EQ || *code == NE)
4928 {
4929 if (need_eq_ne_p)
4930 {
4931 *op0 = mips_zero_if_equal (cmp_op0, cmp_op1);
4932 *op1 = const0_rtx;
4933 }
4934 else
4935 *op1 = force_reg (GET_MODE (cmp_op0), cmp_op1);
4936 }
4937 else
4938 {
4939 /* The comparison needs a separate scc instruction. Store the
4940 result of the scc in *OP0 and compare it against zero. */
4941 bool invert = false;
4942 *op0 = gen_reg_rtx (GET_MODE (cmp_op0));
4943 mips_emit_int_order_test (*code, &invert, *op0, cmp_op0, cmp_op1);
4944 *code = (invert ? EQ : NE);
4945 *op1 = const0_rtx;
4946 }
4947 }
4948 else if (ALL_FIXED_POINT_MODE_P (GET_MODE (cmp_op0)))
4949 {
4950 *op0 = gen_rtx_REG (CCDSPmode, CCDSP_CC_REGNUM);
4951 mips_emit_binary (*code, *op0, cmp_op0, cmp_op1);
4952 *code = NE;
4953 *op1 = const0_rtx;
4954 }
4955 else
4956 {
4957 enum rtx_code cmp_code;
4958
4959 /* Floating-point tests use a separate C.cond.fmt comparison to
4960 set a condition code register. The branch or conditional move
4961 will then compare that register against zero.
4962
4963 Set CMP_CODE to the code of the comparison instruction and
4964 *CODE to the code that the branch or move should use. */
4965 cmp_code = *code;
4966 *code = mips_reversed_fp_cond (&cmp_code) ? EQ : NE;
4967 *op0 = (ISA_HAS_8CC
4968 ? mips_allocate_fcc (CCmode)
4969 : gen_rtx_REG (CCmode, FPSW_REGNUM));
4970 *op1 = const0_rtx;
4971 mips_emit_binary (cmp_code, *op0, cmp_op0, cmp_op1);
4972 }
4973 }
4974 \f
4975 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
4976 and OPERAND[3]. Store the result in OPERANDS[0].
4977
4978 On 64-bit targets, the mode of the comparison and target will always be
4979 SImode, thus possibly narrower than that of the comparison's operands. */
4980
4981 void
4982 mips_expand_scc (rtx operands[])
4983 {
4984 rtx target = operands[0];
4985 enum rtx_code code = GET_CODE (operands[1]);
4986 rtx op0 = operands[2];
4987 rtx op1 = operands[3];
4988
4989 gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT);
4990
4991 if (code == EQ || code == NE)
4992 {
4993 if (ISA_HAS_SEQ_SNE
4994 && reg_imm10_operand (op1, GET_MODE (op1)))
4995 mips_emit_binary (code, target, op0, op1);
4996 else
4997 {
4998 rtx zie = mips_zero_if_equal (op0, op1);
4999 mips_emit_binary (code, target, zie, const0_rtx);
5000 }
5001 }
5002 else
5003 mips_emit_int_order_test (code, 0, target, op0, op1);
5004 }
5005
5006 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
5007 CODE and jump to OPERANDS[3] if the condition holds. */
5008
5009 void
5010 mips_expand_conditional_branch (rtx *operands)
5011 {
5012 enum rtx_code code = GET_CODE (operands[0]);
5013 rtx op0 = operands[1];
5014 rtx op1 = operands[2];
5015 rtx condition;
5016
5017 mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
5018 condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
5019 emit_jump_insn (gen_condjump (condition, operands[3]));
5020 }
5021
5022 /* Implement:
5023
5024 (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
5025 (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS)) */
5026
5027 void
5028 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
5029 enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
5030 {
5031 rtx cmp_result;
5032 bool reversed_p;
5033
5034 reversed_p = mips_reversed_fp_cond (&cond);
5035 cmp_result = mips_allocate_fcc (CCV2mode);
5036 emit_insn (gen_scc_ps (cmp_result,
5037 gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
5038 if (reversed_p)
5039 emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
5040 cmp_result));
5041 else
5042 emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
5043 cmp_result));
5044 }
5045
5046 /* Perform the comparison in OPERANDS[1]. Move OPERANDS[2] into OPERANDS[0]
5047 if the condition holds, otherwise move OPERANDS[3] into OPERANDS[0]. */
5048
5049 void
5050 mips_expand_conditional_move (rtx *operands)
5051 {
5052 rtx cond;
5053 enum rtx_code code = GET_CODE (operands[1]);
5054 rtx op0 = XEXP (operands[1], 0);
5055 rtx op1 = XEXP (operands[1], 1);
5056
5057 mips_emit_compare (&code, &op0, &op1, true);
5058 cond = gen_rtx_fmt_ee (code, GET_MODE (op0), op0, op1);
5059 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
5060 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]), cond,
5061 operands[2], operands[3])));
5062 }
5063
5064 /* Perform the comparison in COMPARISON, then trap if the condition holds. */
5065
5066 void
5067 mips_expand_conditional_trap (rtx comparison)
5068 {
5069 rtx op0, op1;
5070 machine_mode mode;
5071 enum rtx_code code;
5072
5073 /* MIPS conditional trap instructions don't have GT or LE flavors,
5074 so we must swap the operands and convert to LT and GE respectively. */
5075 code = GET_CODE (comparison);
5076 switch (code)
5077 {
5078 case GT:
5079 case LE:
5080 case GTU:
5081 case LEU:
5082 code = swap_condition (code);
5083 op0 = XEXP (comparison, 1);
5084 op1 = XEXP (comparison, 0);
5085 break;
5086
5087 default:
5088 op0 = XEXP (comparison, 0);
5089 op1 = XEXP (comparison, 1);
5090 break;
5091 }
5092
5093 mode = GET_MODE (XEXP (comparison, 0));
5094 op0 = force_reg (mode, op0);
5095 if (!arith_operand (op1, mode))
5096 op1 = force_reg (mode, op1);
5097
5098 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
5099 gen_rtx_fmt_ee (code, mode, op0, op1),
5100 const0_rtx));
5101 }
5102 \f
5103 /* Initialize *CUM for a call to a function of type FNTYPE. */
5104
5105 void
5106 mips_init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype)
5107 {
5108 memset (cum, 0, sizeof (*cum));
5109 cum->prototype = (fntype && prototype_p (fntype));
5110 cum->gp_reg_found = (cum->prototype && stdarg_p (fntype));
5111 }
5112
5113 /* Fill INFO with information about a single argument. CUM is the
5114 cumulative state for earlier arguments. MODE is the mode of this
5115 argument and TYPE is its type (if known). NAMED is true if this
5116 is a named (fixed) argument rather than a variable one. */
5117
5118 static void
5119 mips_get_arg_info (struct mips_arg_info *info, const CUMULATIVE_ARGS *cum,
5120 machine_mode mode, const_tree type, bool named)
5121 {
5122 bool doubleword_aligned_p;
5123 unsigned int num_bytes, num_words, max_regs;
5124
5125 /* Work out the size of the argument. */
5126 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5127 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5128
5129 /* Decide whether it should go in a floating-point register, assuming
5130 one is free. Later code checks for availability.
5131
5132 The checks against UNITS_PER_FPVALUE handle the soft-float and
5133 single-float cases. */
5134 switch (mips_abi)
5135 {
5136 case ABI_EABI:
5137 /* The EABI conventions have traditionally been defined in terms
5138 of TYPE_MODE, regardless of the actual type. */
5139 info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
5140 || mode == V2SFmode)
5141 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5142 break;
5143
5144 case ABI_32:
5145 case ABI_O64:
5146 /* Only leading floating-point scalars are passed in
5147 floating-point registers. We also handle vector floats the same
5148 say, which is OK because they are not covered by the standard ABI. */
5149 info->fpr_p = (!cum->gp_reg_found
5150 && cum->arg_number < 2
5151 && (type == 0
5152 || SCALAR_FLOAT_TYPE_P (type)
5153 || VECTOR_FLOAT_TYPE_P (type))
5154 && (GET_MODE_CLASS (mode) == MODE_FLOAT
5155 || mode == V2SFmode)
5156 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
5157 break;
5158
5159 case ABI_N32:
5160 case ABI_64:
5161 /* Scalar, complex and vector floating-point types are passed in
5162 floating-point registers, as long as this is a named rather
5163 than a variable argument. */
5164 info->fpr_p = (named
5165 && (type == 0 || FLOAT_TYPE_P (type))
5166 && (GET_MODE_CLASS (mode) == MODE_FLOAT
5167 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5168 || mode == V2SFmode)
5169 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
5170
5171 /* ??? According to the ABI documentation, the real and imaginary
5172 parts of complex floats should be passed in individual registers.
5173 The real and imaginary parts of stack arguments are supposed
5174 to be contiguous and there should be an extra word of padding
5175 at the end.
5176
5177 This has two problems. First, it makes it impossible to use a
5178 single "void *" va_list type, since register and stack arguments
5179 are passed differently. (At the time of writing, MIPSpro cannot
5180 handle complex float varargs correctly.) Second, it's unclear
5181 what should happen when there is only one register free.
5182
5183 For now, we assume that named complex floats should go into FPRs
5184 if there are two FPRs free, otherwise they should be passed in the
5185 same way as a struct containing two floats. */
5186 if (info->fpr_p
5187 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5188 && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
5189 {
5190 if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
5191 info->fpr_p = false;
5192 else
5193 num_words = 2;
5194 }
5195 break;
5196
5197 default:
5198 gcc_unreachable ();
5199 }
5200
5201 /* See whether the argument has doubleword alignment. */
5202 doubleword_aligned_p = (mips_function_arg_boundary (mode, type)
5203 > BITS_PER_WORD);
5204
5205 /* Set REG_OFFSET to the register count we're interested in.
5206 The EABI allocates the floating-point registers separately,
5207 but the other ABIs allocate them like integer registers. */
5208 info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
5209 ? cum->num_fprs
5210 : cum->num_gprs);
5211
5212 /* Advance to an even register if the argument is doubleword-aligned. */
5213 if (doubleword_aligned_p)
5214 info->reg_offset += info->reg_offset & 1;
5215
5216 /* Work out the offset of a stack argument. */
5217 info->stack_offset = cum->stack_words;
5218 if (doubleword_aligned_p)
5219 info->stack_offset += info->stack_offset & 1;
5220
5221 max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
5222
5223 /* Partition the argument between registers and stack. */
5224 info->reg_words = MIN (num_words, max_regs);
5225 info->stack_words = num_words - info->reg_words;
5226 }
5227
5228 /* INFO describes a register argument that has the normal format for the
5229 argument's mode. Return the register it uses, assuming that FPRs are
5230 available if HARD_FLOAT_P. */
5231
5232 static unsigned int
5233 mips_arg_regno (const struct mips_arg_info *info, bool hard_float_p)
5234 {
5235 if (!info->fpr_p || !hard_float_p)
5236 return GP_ARG_FIRST + info->reg_offset;
5237 else if (mips_abi == ABI_32 && TARGET_DOUBLE_FLOAT && info->reg_offset > 0)
5238 /* In o32, the second argument is always passed in $f14
5239 for TARGET_DOUBLE_FLOAT, regardless of whether the
5240 first argument was a word or doubleword. */
5241 return FP_ARG_FIRST + 2;
5242 else
5243 return FP_ARG_FIRST + info->reg_offset;
5244 }
5245
5246 /* Implement TARGET_STRICT_ARGUMENT_NAMING. */
5247
5248 static bool
5249 mips_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
5250 {
5251 return !TARGET_OLDABI;
5252 }
5253
5254 /* Implement TARGET_FUNCTION_ARG. */
5255
5256 static rtx
5257 mips_function_arg (cumulative_args_t cum_v, machine_mode mode,
5258 const_tree type, bool named)
5259 {
5260 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5261 struct mips_arg_info info;
5262
5263 /* We will be called with a mode of VOIDmode after the last argument
5264 has been seen. Whatever we return will be passed to the call expander.
5265 If we need a MIPS16 fp_code, return a REG with the code stored as
5266 the mode. */
5267 if (mode == VOIDmode)
5268 {
5269 if (TARGET_MIPS16 && cum->fp_code != 0)
5270 return gen_rtx_REG ((machine_mode) cum->fp_code, 0);
5271 else
5272 return NULL;
5273 }
5274
5275 mips_get_arg_info (&info, cum, mode, type, named);
5276
5277 /* Return straight away if the whole argument is passed on the stack. */
5278 if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
5279 return NULL;
5280
5281 /* The n32 and n64 ABIs say that if any 64-bit chunk of the structure
5282 contains a double in its entirety, then that 64-bit chunk is passed
5283 in a floating-point register. */
5284 if (TARGET_NEWABI
5285 && TARGET_HARD_FLOAT
5286 && named
5287 && type != 0
5288 && TREE_CODE (type) == RECORD_TYPE
5289 && TYPE_SIZE_UNIT (type)
5290 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
5291 {
5292 tree field;
5293
5294 /* First check to see if there is any such field. */
5295 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5296 if (TREE_CODE (field) == FIELD_DECL
5297 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5298 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
5299 && tree_fits_shwi_p (bit_position (field))
5300 && int_bit_position (field) % BITS_PER_WORD == 0)
5301 break;
5302
5303 if (field != 0)
5304 {
5305 /* Now handle the special case by returning a PARALLEL
5306 indicating where each 64-bit chunk goes. INFO.REG_WORDS
5307 chunks are passed in registers. */
5308 unsigned int i;
5309 HOST_WIDE_INT bitpos;
5310 rtx ret;
5311
5312 /* assign_parms checks the mode of ENTRY_PARM, so we must
5313 use the actual mode here. */
5314 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
5315
5316 bitpos = 0;
5317 field = TYPE_FIELDS (type);
5318 for (i = 0; i < info.reg_words; i++)
5319 {
5320 rtx reg;
5321
5322 for (; field; field = DECL_CHAIN (field))
5323 if (TREE_CODE (field) == FIELD_DECL
5324 && int_bit_position (field) >= bitpos)
5325 break;
5326
5327 if (field
5328 && int_bit_position (field) == bitpos
5329 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (field))
5330 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
5331 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
5332 else
5333 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
5334
5335 XVECEXP (ret, 0, i)
5336 = gen_rtx_EXPR_LIST (VOIDmode, reg,
5337 GEN_INT (bitpos / BITS_PER_UNIT));
5338
5339 bitpos += BITS_PER_WORD;
5340 }
5341 return ret;
5342 }
5343 }
5344
5345 /* Handle the n32/n64 conventions for passing complex floating-point
5346 arguments in FPR pairs. The real part goes in the lower register
5347 and the imaginary part goes in the upper register. */
5348 if (TARGET_NEWABI
5349 && info.fpr_p
5350 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5351 {
5352 rtx real, imag;
5353 machine_mode inner;
5354 unsigned int regno;
5355
5356 inner = GET_MODE_INNER (mode);
5357 regno = FP_ARG_FIRST + info.reg_offset;
5358 if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
5359 {
5360 /* Real part in registers, imaginary part on stack. */
5361 gcc_assert (info.stack_words == info.reg_words);
5362 return gen_rtx_REG (inner, regno);
5363 }
5364 else
5365 {
5366 gcc_assert (info.stack_words == 0);
5367 real = gen_rtx_EXPR_LIST (VOIDmode,
5368 gen_rtx_REG (inner, regno),
5369 const0_rtx);
5370 imag = gen_rtx_EXPR_LIST (VOIDmode,
5371 gen_rtx_REG (inner,
5372 regno + info.reg_words / 2),
5373 GEN_INT (GET_MODE_SIZE (inner)));
5374 return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
5375 }
5376 }
5377
5378 return gen_rtx_REG (mode, mips_arg_regno (&info, TARGET_HARD_FLOAT));
5379 }
5380
5381 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
5382
5383 static void
5384 mips_function_arg_advance (cumulative_args_t cum_v, machine_mode mode,
5385 const_tree type, bool named)
5386 {
5387 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5388 struct mips_arg_info info;
5389
5390 mips_get_arg_info (&info, cum, mode, type, named);
5391
5392 if (!info.fpr_p)
5393 cum->gp_reg_found = true;
5394
5395 /* See the comment above the CUMULATIVE_ARGS structure in mips.h for
5396 an explanation of what this code does. It assumes that we're using
5397 either the o32 or the o64 ABI, both of which pass at most 2 arguments
5398 in FPRs. */
5399 if (cum->arg_number < 2 && info.fpr_p)
5400 cum->fp_code += (mode == SFmode ? 1 : 2) << (cum->arg_number * 2);
5401
5402 /* Advance the register count. This has the effect of setting
5403 num_gprs to MAX_ARGS_IN_REGISTERS if a doubleword-aligned
5404 argument required us to skip the final GPR and pass the whole
5405 argument on the stack. */
5406 if (mips_abi != ABI_EABI || !info.fpr_p)
5407 cum->num_gprs = info.reg_offset + info.reg_words;
5408 else if (info.reg_words > 0)
5409 cum->num_fprs += MAX_FPRS_PER_FMT;
5410
5411 /* Advance the stack word count. */
5412 if (info.stack_words > 0)
5413 cum->stack_words = info.stack_offset + info.stack_words;
5414
5415 cum->arg_number++;
5416 }
5417
5418 /* Implement TARGET_ARG_PARTIAL_BYTES. */
5419
5420 static int
5421 mips_arg_partial_bytes (cumulative_args_t cum,
5422 machine_mode mode, tree type, bool named)
5423 {
5424 struct mips_arg_info info;
5425
5426 mips_get_arg_info (&info, get_cumulative_args (cum), mode, type, named);
5427 return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
5428 }
5429
5430 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. Every parameter gets at
5431 least PARM_BOUNDARY bits of alignment, but will be given anything up
5432 to STACK_BOUNDARY bits if the type requires it. */
5433
5434 static unsigned int
5435 mips_function_arg_boundary (machine_mode mode, const_tree type)
5436 {
5437 unsigned int alignment;
5438
5439 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
5440 if (alignment < PARM_BOUNDARY)
5441 alignment = PARM_BOUNDARY;
5442 if (alignment > STACK_BOUNDARY)
5443 alignment = STACK_BOUNDARY;
5444 return alignment;
5445 }
5446
5447 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
5448 upward rather than downward. In other words, return true if the
5449 first byte of the stack slot has useful data, false if the last
5450 byte does. */
5451
5452 bool
5453 mips_pad_arg_upward (machine_mode mode, const_tree type)
5454 {
5455 /* On little-endian targets, the first byte of every stack argument
5456 is passed in the first byte of the stack slot. */
5457 if (!BYTES_BIG_ENDIAN)
5458 return true;
5459
5460 /* Otherwise, integral types are padded downward: the last byte of a
5461 stack argument is passed in the last byte of the stack slot. */
5462 if (type != 0
5463 ? (INTEGRAL_TYPE_P (type)
5464 || POINTER_TYPE_P (type)
5465 || FIXED_POINT_TYPE_P (type))
5466 : (SCALAR_INT_MODE_P (mode)
5467 || ALL_SCALAR_FIXED_POINT_MODE_P (mode)))
5468 return false;
5469
5470 /* Big-endian o64 pads floating-point arguments downward. */
5471 if (mips_abi == ABI_O64)
5472 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5473 return false;
5474
5475 /* Other types are padded upward for o32, o64, n32 and n64. */
5476 if (mips_abi != ABI_EABI)
5477 return true;
5478
5479 /* Arguments smaller than a stack slot are padded downward. */
5480 if (mode != BLKmode)
5481 return GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY;
5482 else
5483 return int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT);
5484 }
5485
5486 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...). Return !BYTES_BIG_ENDIAN
5487 if the least significant byte of the register has useful data. Return
5488 the opposite if the most significant byte does. */
5489
5490 bool
5491 mips_pad_reg_upward (machine_mode mode, tree type)
5492 {
5493 /* No shifting is required for floating-point arguments. */
5494 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
5495 return !BYTES_BIG_ENDIAN;
5496
5497 /* Otherwise, apply the same padding to register arguments as we do
5498 to stack arguments. */
5499 return mips_pad_arg_upward (mode, type);
5500 }
5501
5502 /* Return nonzero when an argument must be passed by reference. */
5503
5504 static bool
5505 mips_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
5506 machine_mode mode, const_tree type,
5507 bool named ATTRIBUTE_UNUSED)
5508 {
5509 if (mips_abi == ABI_EABI)
5510 {
5511 int size;
5512
5513 /* ??? How should SCmode be handled? */
5514 if (mode == DImode || mode == DFmode
5515 || mode == DQmode || mode == UDQmode
5516 || mode == DAmode || mode == UDAmode)
5517 return 0;
5518
5519 size = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
5520 return size == -1 || size > UNITS_PER_WORD;
5521 }
5522 else
5523 {
5524 /* If we have a variable-sized parameter, we have no choice. */
5525 return targetm.calls.must_pass_in_stack (mode, type);
5526 }
5527 }
5528
5529 /* Implement TARGET_CALLEE_COPIES. */
5530
5531 static bool
5532 mips_callee_copies (cumulative_args_t cum ATTRIBUTE_UNUSED,
5533 machine_mode mode ATTRIBUTE_UNUSED,
5534 const_tree type ATTRIBUTE_UNUSED, bool named)
5535 {
5536 return mips_abi == ABI_EABI && named;
5537 }
5538 \f
5539 /* See whether VALTYPE is a record whose fields should be returned in
5540 floating-point registers. If so, return the number of fields and
5541 list them in FIELDS (which should have two elements). Return 0
5542 otherwise.
5543
5544 For n32 & n64, a structure with one or two fields is returned in
5545 floating-point registers as long as every field has a floating-point
5546 type. */
5547
5548 static int
5549 mips_fpr_return_fields (const_tree valtype, tree *fields)
5550 {
5551 tree field;
5552 int i;
5553
5554 if (!TARGET_NEWABI)
5555 return 0;
5556
5557 if (TREE_CODE (valtype) != RECORD_TYPE)
5558 return 0;
5559
5560 i = 0;
5561 for (field = TYPE_FIELDS (valtype); field != 0; field = DECL_CHAIN (field))
5562 {
5563 if (TREE_CODE (field) != FIELD_DECL)
5564 continue;
5565
5566 if (!SCALAR_FLOAT_TYPE_P (TREE_TYPE (field)))
5567 return 0;
5568
5569 if (i == 2)
5570 return 0;
5571
5572 fields[i++] = field;
5573 }
5574 return i;
5575 }
5576
5577 /* Implement TARGET_RETURN_IN_MSB. For n32 & n64, we should return
5578 a value in the most significant part of $2/$3 if:
5579
5580 - the target is big-endian;
5581
5582 - the value has a structure or union type (we generalize this to
5583 cover aggregates from other languages too); and
5584
5585 - the structure is not returned in floating-point registers. */
5586
5587 static bool
5588 mips_return_in_msb (const_tree valtype)
5589 {
5590 tree fields[2];
5591
5592 return (TARGET_NEWABI
5593 && TARGET_BIG_ENDIAN
5594 && AGGREGATE_TYPE_P (valtype)
5595 && mips_fpr_return_fields (valtype, fields) == 0);
5596 }
5597
5598 /* Return true if the function return value MODE will get returned in a
5599 floating-point register. */
5600
5601 static bool
5602 mips_return_mode_in_fpr_p (machine_mode mode)
5603 {
5604 return ((GET_MODE_CLASS (mode) == MODE_FLOAT
5605 || mode == V2SFmode
5606 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5607 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_HWFPVALUE);
5608 }
5609
5610 /* Return the representation of an FPR return register when the
5611 value being returned in FP_RETURN has mode VALUE_MODE and the
5612 return type itself has mode TYPE_MODE. On NewABI targets,
5613 the two modes may be different for structures like:
5614
5615 struct __attribute__((packed)) foo { float f; }
5616
5617 where we return the SFmode value of "f" in FP_RETURN, but where
5618 the structure itself has mode BLKmode. */
5619
5620 static rtx
5621 mips_return_fpr_single (machine_mode type_mode,
5622 machine_mode value_mode)
5623 {
5624 rtx x;
5625
5626 x = gen_rtx_REG (value_mode, FP_RETURN);
5627 if (type_mode != value_mode)
5628 {
5629 x = gen_rtx_EXPR_LIST (VOIDmode, x, const0_rtx);
5630 x = gen_rtx_PARALLEL (type_mode, gen_rtvec (1, x));
5631 }
5632 return x;
5633 }
5634
5635 /* Return a composite value in a pair of floating-point registers.
5636 MODE1 and OFFSET1 are the mode and byte offset for the first value,
5637 likewise MODE2 and OFFSET2 for the second. MODE is the mode of the
5638 complete value.
5639
5640 For n32 & n64, $f0 always holds the first value and $f2 the second.
5641 Otherwise the values are packed together as closely as possible. */
5642
5643 static rtx
5644 mips_return_fpr_pair (machine_mode mode,
5645 machine_mode mode1, HOST_WIDE_INT offset1,
5646 machine_mode mode2, HOST_WIDE_INT offset2)
5647 {
5648 int inc;
5649
5650 inc = (TARGET_NEWABI ? 2 : MAX_FPRS_PER_FMT);
5651 return gen_rtx_PARALLEL
5652 (mode,
5653 gen_rtvec (2,
5654 gen_rtx_EXPR_LIST (VOIDmode,
5655 gen_rtx_REG (mode1, FP_RETURN),
5656 GEN_INT (offset1)),
5657 gen_rtx_EXPR_LIST (VOIDmode,
5658 gen_rtx_REG (mode2, FP_RETURN + inc),
5659 GEN_INT (offset2))));
5660
5661 }
5662
5663 /* Implement TARGET_FUNCTION_VALUE and TARGET_LIBCALL_VALUE.
5664 For normal calls, VALTYPE is the return type and MODE is VOIDmode.
5665 For libcalls, VALTYPE is null and MODE is the mode of the return value. */
5666
5667 static rtx
5668 mips_function_value_1 (const_tree valtype, const_tree fn_decl_or_type,
5669 machine_mode mode)
5670 {
5671 if (valtype)
5672 {
5673 tree fields[2];
5674 int unsigned_p;
5675 const_tree func;
5676
5677 if (fn_decl_or_type && DECL_P (fn_decl_or_type))
5678 func = fn_decl_or_type;
5679 else
5680 func = NULL;
5681
5682 mode = TYPE_MODE (valtype);
5683 unsigned_p = TYPE_UNSIGNED (valtype);
5684
5685 /* Since TARGET_PROMOTE_FUNCTION_MODE unconditionally promotes,
5686 return values, promote the mode here too. */
5687 mode = promote_function_mode (valtype, mode, &unsigned_p, func, 1);
5688
5689 /* Handle structures whose fields are returned in $f0/$f2. */
5690 switch (mips_fpr_return_fields (valtype, fields))
5691 {
5692 case 1:
5693 return mips_return_fpr_single (mode,
5694 TYPE_MODE (TREE_TYPE (fields[0])));
5695
5696 case 2:
5697 return mips_return_fpr_pair (mode,
5698 TYPE_MODE (TREE_TYPE (fields[0])),
5699 int_byte_position (fields[0]),
5700 TYPE_MODE (TREE_TYPE (fields[1])),
5701 int_byte_position (fields[1]));
5702 }
5703
5704 /* If a value is passed in the most significant part of a register, see
5705 whether we have to round the mode up to a whole number of words. */
5706 if (mips_return_in_msb (valtype))
5707 {
5708 HOST_WIDE_INT size = int_size_in_bytes (valtype);
5709 if (size % UNITS_PER_WORD != 0)
5710 {
5711 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
5712 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
5713 }
5714 }
5715
5716 /* For EABI, the class of return register depends entirely on MODE.
5717 For example, "struct { some_type x; }" and "union { some_type x; }"
5718 are returned in the same way as a bare "some_type" would be.
5719 Other ABIs only use FPRs for scalar, complex or vector types. */
5720 if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
5721 return gen_rtx_REG (mode, GP_RETURN);
5722 }
5723
5724 if (!TARGET_MIPS16)
5725 {
5726 /* Handle long doubles for n32 & n64. */
5727 if (mode == TFmode)
5728 return mips_return_fpr_pair (mode,
5729 DImode, 0,
5730 DImode, GET_MODE_SIZE (mode) / 2);
5731
5732 if (mips_return_mode_in_fpr_p (mode))
5733 {
5734 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5735 return mips_return_fpr_pair (mode,
5736 GET_MODE_INNER (mode), 0,
5737 GET_MODE_INNER (mode),
5738 GET_MODE_SIZE (mode) / 2);
5739 else
5740 return gen_rtx_REG (mode, FP_RETURN);
5741 }
5742 }
5743
5744 return gen_rtx_REG (mode, GP_RETURN);
5745 }
5746
5747 /* Implement TARGET_FUNCTION_VALUE. */
5748
5749 static rtx
5750 mips_function_value (const_tree valtype, const_tree fn_decl_or_type,
5751 bool outgoing ATTRIBUTE_UNUSED)
5752 {
5753 return mips_function_value_1 (valtype, fn_decl_or_type, VOIDmode);
5754 }
5755
5756 /* Implement TARGET_LIBCALL_VALUE. */
5757
5758 static rtx
5759 mips_libcall_value (machine_mode mode, const_rtx fun ATTRIBUTE_UNUSED)
5760 {
5761 return mips_function_value_1 (NULL_TREE, NULL_TREE, mode);
5762 }
5763
5764 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
5765
5766 On the MIPS, R2 R3 and F0 F2 are the only register thus used.
5767 Currently, R2 and F0 are only implemented here (C has no complex type). */
5768
5769 static bool
5770 mips_function_value_regno_p (const unsigned int regno)
5771 {
5772 if (regno == GP_RETURN
5773 || regno == FP_RETURN
5774 || (LONG_DOUBLE_TYPE_SIZE == 128
5775 && FP_RETURN != GP_RETURN
5776 && regno == FP_RETURN + 2))
5777 return true;
5778
5779 return false;
5780 }
5781
5782 /* Implement TARGET_RETURN_IN_MEMORY. Under the o32 and o64 ABIs,
5783 all BLKmode objects are returned in memory. Under the n32, n64
5784 and embedded ABIs, small structures are returned in a register.
5785 Objects with varying size must still be returned in memory, of
5786 course. */
5787
5788 static bool
5789 mips_return_in_memory (const_tree type, const_tree fndecl ATTRIBUTE_UNUSED)
5790 {
5791 return (TARGET_OLDABI
5792 ? TYPE_MODE (type) == BLKmode
5793 : !IN_RANGE (int_size_in_bytes (type), 0, 2 * UNITS_PER_WORD));
5794 }
5795 \f
5796 /* Implement TARGET_SETUP_INCOMING_VARARGS. */
5797
5798 static void
5799 mips_setup_incoming_varargs (cumulative_args_t cum, machine_mode mode,
5800 tree type, int *pretend_size ATTRIBUTE_UNUSED,
5801 int no_rtl)
5802 {
5803 CUMULATIVE_ARGS local_cum;
5804 int gp_saved, fp_saved;
5805
5806 /* The caller has advanced CUM up to, but not beyond, the last named
5807 argument. Advance a local copy of CUM past the last "real" named
5808 argument, to find out how many registers are left over. */
5809 local_cum = *get_cumulative_args (cum);
5810 mips_function_arg_advance (pack_cumulative_args (&local_cum), mode, type,
5811 true);
5812
5813 /* Found out how many registers we need to save. */
5814 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
5815 fp_saved = (EABI_FLOAT_VARARGS_P
5816 ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
5817 : 0);
5818
5819 if (!no_rtl)
5820 {
5821 if (gp_saved > 0)
5822 {
5823 rtx ptr, mem;
5824
5825 ptr = plus_constant (Pmode, virtual_incoming_args_rtx,
5826 REG_PARM_STACK_SPACE (cfun->decl)
5827 - gp_saved * UNITS_PER_WORD);
5828 mem = gen_frame_mem (BLKmode, ptr);
5829 set_mem_alias_set (mem, get_varargs_alias_set ());
5830
5831 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
5832 mem, gp_saved);
5833 }
5834 if (fp_saved > 0)
5835 {
5836 /* We can't use move_block_from_reg, because it will use
5837 the wrong mode. */
5838 machine_mode mode;
5839 int off, i;
5840
5841 /* Set OFF to the offset from virtual_incoming_args_rtx of
5842 the first float register. The FP save area lies below
5843 the integer one, and is aligned to UNITS_PER_FPVALUE bytes. */
5844 off = (-gp_saved * UNITS_PER_WORD) & -UNITS_PER_FPVALUE;
5845 off -= fp_saved * UNITS_PER_FPREG;
5846
5847 mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
5848
5849 for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS;
5850 i += MAX_FPRS_PER_FMT)
5851 {
5852 rtx ptr, mem;
5853
5854 ptr = plus_constant (Pmode, virtual_incoming_args_rtx, off);
5855 mem = gen_frame_mem (mode, ptr);
5856 set_mem_alias_set (mem, get_varargs_alias_set ());
5857 mips_emit_move (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
5858 off += UNITS_PER_HWFPVALUE;
5859 }
5860 }
5861 }
5862 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
5863 cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
5864 + fp_saved * UNITS_PER_FPREG);
5865 }
5866
5867 /* Implement TARGET_BUILTIN_VA_LIST. */
5868
5869 static tree
5870 mips_build_builtin_va_list (void)
5871 {
5872 if (EABI_FLOAT_VARARGS_P)
5873 {
5874 /* We keep 3 pointers, and two offsets.
5875
5876 Two pointers are to the overflow area, which starts at the CFA.
5877 One of these is constant, for addressing into the GPR save area
5878 below it. The other is advanced up the stack through the
5879 overflow region.
5880
5881 The third pointer is to the bottom of the GPR save area.
5882 Since the FPR save area is just below it, we can address
5883 FPR slots off this pointer.
5884
5885 We also keep two one-byte offsets, which are to be subtracted
5886 from the constant pointers to yield addresses in the GPR and
5887 FPR save areas. These are downcounted as float or non-float
5888 arguments are used, and when they get to zero, the argument
5889 must be obtained from the overflow region. */
5890 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
5891 tree array, index;
5892
5893 record = lang_hooks.types.make_type (RECORD_TYPE);
5894
5895 f_ovfl = build_decl (BUILTINS_LOCATION,
5896 FIELD_DECL, get_identifier ("__overflow_argptr"),
5897 ptr_type_node);
5898 f_gtop = build_decl (BUILTINS_LOCATION,
5899 FIELD_DECL, get_identifier ("__gpr_top"),
5900 ptr_type_node);
5901 f_ftop = build_decl (BUILTINS_LOCATION,
5902 FIELD_DECL, get_identifier ("__fpr_top"),
5903 ptr_type_node);
5904 f_goff = build_decl (BUILTINS_LOCATION,
5905 FIELD_DECL, get_identifier ("__gpr_offset"),
5906 unsigned_char_type_node);
5907 f_foff = build_decl (BUILTINS_LOCATION,
5908 FIELD_DECL, get_identifier ("__fpr_offset"),
5909 unsigned_char_type_node);
5910 /* Explicitly pad to the size of a pointer, so that -Wpadded won't
5911 warn on every user file. */
5912 index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
5913 array = build_array_type (unsigned_char_type_node,
5914 build_index_type (index));
5915 f_res = build_decl (BUILTINS_LOCATION,
5916 FIELD_DECL, get_identifier ("__reserved"), array);
5917
5918 DECL_FIELD_CONTEXT (f_ovfl) = record;
5919 DECL_FIELD_CONTEXT (f_gtop) = record;
5920 DECL_FIELD_CONTEXT (f_ftop) = record;
5921 DECL_FIELD_CONTEXT (f_goff) = record;
5922 DECL_FIELD_CONTEXT (f_foff) = record;
5923 DECL_FIELD_CONTEXT (f_res) = record;
5924
5925 TYPE_FIELDS (record) = f_ovfl;
5926 DECL_CHAIN (f_ovfl) = f_gtop;
5927 DECL_CHAIN (f_gtop) = f_ftop;
5928 DECL_CHAIN (f_ftop) = f_goff;
5929 DECL_CHAIN (f_goff) = f_foff;
5930 DECL_CHAIN (f_foff) = f_res;
5931
5932 layout_type (record);
5933 return record;
5934 }
5935 else
5936 /* Otherwise, we use 'void *'. */
5937 return ptr_type_node;
5938 }
5939
5940 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
5941
5942 static void
5943 mips_va_start (tree valist, rtx nextarg)
5944 {
5945 if (EABI_FLOAT_VARARGS_P)
5946 {
5947 const CUMULATIVE_ARGS *cum;
5948 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
5949 tree ovfl, gtop, ftop, goff, foff;
5950 tree t;
5951 int gpr_save_area_size;
5952 int fpr_save_area_size;
5953 int fpr_offset;
5954
5955 cum = &crtl->args.info;
5956 gpr_save_area_size
5957 = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
5958 fpr_save_area_size
5959 = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
5960
5961 f_ovfl = TYPE_FIELDS (va_list_type_node);
5962 f_gtop = DECL_CHAIN (f_ovfl);
5963 f_ftop = DECL_CHAIN (f_gtop);
5964 f_goff = DECL_CHAIN (f_ftop);
5965 f_foff = DECL_CHAIN (f_goff);
5966
5967 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
5968 NULL_TREE);
5969 gtop = build3 (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
5970 NULL_TREE);
5971 ftop = build3 (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
5972 NULL_TREE);
5973 goff = build3 (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
5974 NULL_TREE);
5975 foff = build3 (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
5976 NULL_TREE);
5977
5978 /* Emit code to initialize OVFL, which points to the next varargs
5979 stack argument. CUM->STACK_WORDS gives the number of stack
5980 words used by named arguments. */
5981 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
5982 if (cum->stack_words > 0)
5983 t = fold_build_pointer_plus_hwi (t, cum->stack_words * UNITS_PER_WORD);
5984 t = build2 (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
5985 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5986
5987 /* Emit code to initialize GTOP, the top of the GPR save area. */
5988 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
5989 t = build2 (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
5990 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5991
5992 /* Emit code to initialize FTOP, the top of the FPR save area.
5993 This address is gpr_save_area_bytes below GTOP, rounded
5994 down to the next fp-aligned boundary. */
5995 t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
5996 fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
5997 fpr_offset &= -UNITS_PER_FPVALUE;
5998 if (fpr_offset)
5999 t = fold_build_pointer_plus_hwi (t, -fpr_offset);
6000 t = build2 (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
6001 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6002
6003 /* Emit code to initialize GOFF, the offset from GTOP of the
6004 next GPR argument. */
6005 t = build2 (MODIFY_EXPR, TREE_TYPE (goff), goff,
6006 build_int_cst (TREE_TYPE (goff), gpr_save_area_size));
6007 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6008
6009 /* Likewise emit code to initialize FOFF, the offset from FTOP
6010 of the next FPR argument. */
6011 t = build2 (MODIFY_EXPR, TREE_TYPE (foff), foff,
6012 build_int_cst (TREE_TYPE (foff), fpr_save_area_size));
6013 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
6014 }
6015 else
6016 {
6017 nextarg = plus_constant (Pmode, nextarg, -cfun->machine->varargs_size);
6018 std_expand_builtin_va_start (valist, nextarg);
6019 }
6020 }
6021
6022 /* Like std_gimplify_va_arg_expr, but apply alignment to zero-sized
6023 types as well. */
6024
6025 static tree
6026 mips_std_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
6027 gimple_seq *post_p)
6028 {
6029 tree addr, t, type_size, rounded_size, valist_tmp;
6030 unsigned HOST_WIDE_INT align, boundary;
6031 bool indirect;
6032
6033 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, false);
6034 if (indirect)
6035 type = build_pointer_type (type);
6036
6037 align = PARM_BOUNDARY / BITS_PER_UNIT;
6038 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
6039
6040 /* When we align parameter on stack for caller, if the parameter
6041 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
6042 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
6043 here with caller. */
6044 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
6045 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
6046
6047 boundary /= BITS_PER_UNIT;
6048
6049 /* Hoist the valist value into a temporary for the moment. */
6050 valist_tmp = get_initialized_tmp_var (valist, pre_p, NULL);
6051
6052 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
6053 requires greater alignment, we must perform dynamic alignment. */
6054 if (boundary > align)
6055 {
6056 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
6057 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
6058 gimplify_and_add (t, pre_p);
6059
6060 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
6061 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
6062 valist_tmp,
6063 build_int_cst (TREE_TYPE (valist), -boundary)));
6064 gimplify_and_add (t, pre_p);
6065 }
6066 else
6067 boundary = align;
6068
6069 /* If the actual alignment is less than the alignment of the type,
6070 adjust the type accordingly so that we don't assume strict alignment
6071 when dereferencing the pointer. */
6072 boundary *= BITS_PER_UNIT;
6073 if (boundary < TYPE_ALIGN (type))
6074 {
6075 type = build_variant_type_copy (type);
6076 TYPE_ALIGN (type) = boundary;
6077 }
6078
6079 /* Compute the rounded size of the type. */
6080 type_size = size_in_bytes (type);
6081 rounded_size = round_up (type_size, align);
6082
6083 /* Reduce rounded_size so it's sharable with the postqueue. */
6084 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
6085
6086 /* Get AP. */
6087 addr = valist_tmp;
6088 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
6089 {
6090 /* Small args are padded downward. */
6091 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
6092 rounded_size, size_int (align));
6093 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
6094 size_binop (MINUS_EXPR, rounded_size, type_size));
6095 addr = fold_build_pointer_plus (addr, t);
6096 }
6097
6098 /* Compute new value for AP. */
6099 t = fold_build_pointer_plus (valist_tmp, rounded_size);
6100 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
6101 gimplify_and_add (t, pre_p);
6102
6103 addr = fold_convert (build_pointer_type (type), addr);
6104
6105 if (indirect)
6106 addr = build_va_arg_indirect_ref (addr);
6107
6108 return build_va_arg_indirect_ref (addr);
6109 }
6110
6111 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR. */
6112
6113 static tree
6114 mips_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
6115 gimple_seq *post_p)
6116 {
6117 tree addr;
6118 bool indirect_p;
6119
6120 indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
6121 if (indirect_p)
6122 type = build_pointer_type (type);
6123
6124 if (!EABI_FLOAT_VARARGS_P)
6125 addr = mips_std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
6126 else
6127 {
6128 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
6129 tree ovfl, top, off, align;
6130 HOST_WIDE_INT size, rsize, osize;
6131 tree t, u;
6132
6133 f_ovfl = TYPE_FIELDS (va_list_type_node);
6134 f_gtop = DECL_CHAIN (f_ovfl);
6135 f_ftop = DECL_CHAIN (f_gtop);
6136 f_goff = DECL_CHAIN (f_ftop);
6137 f_foff = DECL_CHAIN (f_goff);
6138
6139 /* Let:
6140
6141 TOP be the top of the GPR or FPR save area;
6142 OFF be the offset from TOP of the next register;
6143 ADDR_RTX be the address of the argument;
6144 SIZE be the number of bytes in the argument type;
6145 RSIZE be the number of bytes used to store the argument
6146 when it's in the register save area; and
6147 OSIZE be the number of bytes used to store it when it's
6148 in the stack overflow area.
6149
6150 The code we want is:
6151
6152 1: off &= -rsize; // round down
6153 2: if (off != 0)
6154 3: {
6155 4: addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0);
6156 5: off -= rsize;
6157 6: }
6158 7: else
6159 8: {
6160 9: ovfl = ((intptr_t) ovfl + osize - 1) & -osize;
6161 10: addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0);
6162 11: ovfl += osize;
6163 14: }
6164
6165 [1] and [9] can sometimes be optimized away. */
6166
6167 ovfl = build3 (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
6168 NULL_TREE);
6169 size = int_size_in_bytes (type);
6170
6171 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
6172 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
6173 {
6174 top = build3 (COMPONENT_REF, TREE_TYPE (f_ftop),
6175 unshare_expr (valist), f_ftop, NULL_TREE);
6176 off = build3 (COMPONENT_REF, TREE_TYPE (f_foff),
6177 unshare_expr (valist), f_foff, NULL_TREE);
6178
6179 /* When va_start saves FPR arguments to the stack, each slot
6180 takes up UNITS_PER_HWFPVALUE bytes, regardless of the
6181 argument's precision. */
6182 rsize = UNITS_PER_HWFPVALUE;
6183
6184 /* Overflow arguments are padded to UNITS_PER_WORD bytes
6185 (= PARM_BOUNDARY bits). This can be different from RSIZE
6186 in two cases:
6187
6188 (1) On 32-bit targets when TYPE is a structure such as:
6189
6190 struct s { float f; };
6191
6192 Such structures are passed in paired FPRs, so RSIZE
6193 will be 8 bytes. However, the structure only takes
6194 up 4 bytes of memory, so OSIZE will only be 4.
6195
6196 (2) In combinations such as -mgp64 -msingle-float
6197 -fshort-double. Doubles passed in registers will then take
6198 up 4 (UNITS_PER_HWFPVALUE) bytes, but those passed on the
6199 stack take up UNITS_PER_WORD bytes. */
6200 osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
6201 }
6202 else
6203 {
6204 top = build3 (COMPONENT_REF, TREE_TYPE (f_gtop),
6205 unshare_expr (valist), f_gtop, NULL_TREE);
6206 off = build3 (COMPONENT_REF, TREE_TYPE (f_goff),
6207 unshare_expr (valist), f_goff, NULL_TREE);
6208 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
6209 if (rsize > UNITS_PER_WORD)
6210 {
6211 /* [1] Emit code for: off &= -rsize. */
6212 t = build2 (BIT_AND_EXPR, TREE_TYPE (off), unshare_expr (off),
6213 build_int_cst (TREE_TYPE (off), -rsize));
6214 gimplify_assign (unshare_expr (off), t, pre_p);
6215 }
6216 osize = rsize;
6217 }
6218
6219 /* [2] Emit code to branch if off == 0. */
6220 t = build2 (NE_EXPR, boolean_type_node, unshare_expr (off),
6221 build_int_cst (TREE_TYPE (off), 0));
6222 addr = build3 (COND_EXPR, ptr_type_node, t, NULL_TREE, NULL_TREE);
6223
6224 /* [5] Emit code for: off -= rsize. We do this as a form of
6225 post-decrement not available to C. */
6226 t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
6227 t = build2 (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
6228
6229 /* [4] Emit code for:
6230 addr_rtx = top - off + (BYTES_BIG_ENDIAN ? RSIZE - SIZE : 0). */
6231 t = fold_convert (sizetype, t);
6232 t = fold_build1 (NEGATE_EXPR, sizetype, t);
6233 t = fold_build_pointer_plus (top, t);
6234 if (BYTES_BIG_ENDIAN && rsize > size)
6235 t = fold_build_pointer_plus_hwi (t, rsize - size);
6236 COND_EXPR_THEN (addr) = t;
6237
6238 if (osize > UNITS_PER_WORD)
6239 {
6240 /* [9] Emit: ovfl = ((intptr_t) ovfl + osize - 1) & -osize. */
6241 t = fold_build_pointer_plus_hwi (unshare_expr (ovfl), osize - 1);
6242 u = build_int_cst (TREE_TYPE (t), -osize);
6243 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
6244 align = build2 (MODIFY_EXPR, TREE_TYPE (ovfl),
6245 unshare_expr (ovfl), t);
6246 }
6247 else
6248 align = NULL;
6249
6250 /* [10, 11] Emit code for:
6251 addr_rtx = ovfl + (BYTES_BIG_ENDIAN ? OSIZE - SIZE : 0)
6252 ovfl += osize. */
6253 u = fold_convert (TREE_TYPE (ovfl), build_int_cst (NULL_TREE, osize));
6254 t = build2 (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
6255 if (BYTES_BIG_ENDIAN && osize > size)
6256 t = fold_build_pointer_plus_hwi (t, osize - size);
6257
6258 /* String [9] and [10, 11] together. */
6259 if (align)
6260 t = build2 (COMPOUND_EXPR, TREE_TYPE (t), align, t);
6261 COND_EXPR_ELSE (addr) = t;
6262
6263 addr = fold_convert (build_pointer_type (type), addr);
6264 addr = build_va_arg_indirect_ref (addr);
6265 }
6266
6267 if (indirect_p)
6268 addr = build_va_arg_indirect_ref (addr);
6269
6270 return addr;
6271 }
6272 \f
6273 /* Declare a unique, locally-binding function called NAME, then start
6274 its definition. */
6275
6276 static void
6277 mips_start_unique_function (const char *name)
6278 {
6279 tree decl;
6280
6281 decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
6282 get_identifier (name),
6283 build_function_type_list (void_type_node, NULL_TREE));
6284 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
6285 NULL_TREE, void_type_node);
6286 TREE_PUBLIC (decl) = 1;
6287 TREE_STATIC (decl) = 1;
6288
6289 cgraph_node::create (decl)->set_comdat_group (DECL_ASSEMBLER_NAME (decl));
6290
6291 targetm.asm_out.unique_section (decl, 0);
6292 switch_to_section (get_named_section (decl, NULL, 0));
6293
6294 targetm.asm_out.globalize_label (asm_out_file, name);
6295 fputs ("\t.hidden\t", asm_out_file);
6296 assemble_name (asm_out_file, name);
6297 putc ('\n', asm_out_file);
6298 }
6299
6300 /* Start a definition of function NAME. MIPS16_P indicates whether the
6301 function contains MIPS16 code. */
6302
6303 static void
6304 mips_start_function_definition (const char *name, bool mips16_p)
6305 {
6306 if (mips16_p)
6307 fprintf (asm_out_file, "\t.set\tmips16\n");
6308 else
6309 fprintf (asm_out_file, "\t.set\tnomips16\n");
6310
6311 if (TARGET_MICROMIPS)
6312 fprintf (asm_out_file, "\t.set\tmicromips\n");
6313 #ifdef HAVE_GAS_MICROMIPS
6314 else
6315 fprintf (asm_out_file, "\t.set\tnomicromips\n");
6316 #endif
6317
6318 if (!flag_inhibit_size_directive)
6319 {
6320 fputs ("\t.ent\t", asm_out_file);
6321 assemble_name (asm_out_file, name);
6322 fputs ("\n", asm_out_file);
6323 }
6324
6325 ASM_OUTPUT_TYPE_DIRECTIVE (asm_out_file, name, "function");
6326
6327 /* Start the definition proper. */
6328 assemble_name (asm_out_file, name);
6329 fputs (":\n", asm_out_file);
6330 }
6331
6332 /* End a function definition started by mips_start_function_definition. */
6333
6334 static void
6335 mips_end_function_definition (const char *name)
6336 {
6337 if (!flag_inhibit_size_directive)
6338 {
6339 fputs ("\t.end\t", asm_out_file);
6340 assemble_name (asm_out_file, name);
6341 fputs ("\n", asm_out_file);
6342 }
6343 }
6344
6345 /* If *STUB_PTR points to a stub, output a comdat-style definition for it,
6346 then free *STUB_PTR. */
6347
6348 static void
6349 mips_finish_stub (mips_one_only_stub **stub_ptr)
6350 {
6351 mips_one_only_stub *stub = *stub_ptr;
6352 if (!stub)
6353 return;
6354
6355 const char *name = stub->get_name ();
6356 mips_start_unique_function (name);
6357 mips_start_function_definition (name, false);
6358 stub->output_body ();
6359 mips_end_function_definition (name);
6360 delete stub;
6361 *stub_ptr = 0;
6362 }
6363 \f
6364 /* Return true if calls to X can use R_MIPS_CALL* relocations. */
6365
6366 static bool
6367 mips_ok_for_lazy_binding_p (rtx x)
6368 {
6369 return (TARGET_USE_GOT
6370 && GET_CODE (x) == SYMBOL_REF
6371 && !SYMBOL_REF_BIND_NOW_P (x)
6372 && !mips_symbol_binds_local_p (x));
6373 }
6374
6375 /* Load function address ADDR into register DEST. TYPE is as for
6376 mips_expand_call. Return true if we used an explicit lazy-binding
6377 sequence. */
6378
6379 static bool
6380 mips_load_call_address (enum mips_call_type type, rtx dest, rtx addr)
6381 {
6382 /* If we're generating PIC, and this call is to a global function,
6383 try to allow its address to be resolved lazily. This isn't
6384 possible for sibcalls when $gp is call-saved because the value
6385 of $gp on entry to the stub would be our caller's gp, not ours. */
6386 if (TARGET_EXPLICIT_RELOCS
6387 && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
6388 && mips_ok_for_lazy_binding_p (addr))
6389 {
6390 addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
6391 emit_insn (gen_rtx_SET (VOIDmode, dest, addr));
6392 return true;
6393 }
6394 else
6395 {
6396 mips_emit_move (dest, addr);
6397 return false;
6398 }
6399 }
6400 \f
6401 struct local_alias_traits : default_hashmap_traits
6402 {
6403 static hashval_t hash (rtx);
6404 static bool equal_keys (rtx, rtx);
6405 };
6406
6407 /* Each locally-defined hard-float MIPS16 function has a local symbol
6408 associated with it. This hash table maps the function symbol (FUNC)
6409 to the local symbol (LOCAL). */
6410 static GTY (()) hash_map<rtx, rtx, local_alias_traits> *mips16_local_aliases;
6411
6412 /* Hash table callbacks for mips16_local_aliases. */
6413
6414 hashval_t
6415 local_alias_traits::hash (rtx func)
6416 {
6417 return htab_hash_string (XSTR (func, 0));
6418 }
6419
6420 bool
6421 local_alias_traits::equal_keys (rtx func1, rtx func2)
6422 {
6423 return rtx_equal_p (func1, func2);
6424 }
6425
6426 /* FUNC is the symbol for a locally-defined hard-float MIPS16 function.
6427 Return a local alias for it, creating a new one if necessary. */
6428
6429 static rtx
6430 mips16_local_alias (rtx func)
6431 {
6432 /* Create the hash table if this is the first call. */
6433 if (mips16_local_aliases == NULL)
6434 mips16_local_aliases
6435 = hash_map<rtx, rtx, local_alias_traits>::create_ggc (37);
6436
6437 /* Look up the function symbol, creating a new entry if need be. */
6438 bool existed;
6439 rtx *slot = &mips16_local_aliases->get_or_insert (func, &existed);
6440 gcc_assert (slot != NULL);
6441
6442 if (!existed)
6443 {
6444 const char *func_name, *local_name;
6445 rtx local;
6446
6447 /* Create a new SYMBOL_REF for the local symbol. The choice of
6448 __fn_local_* is based on the __fn_stub_* names that we've
6449 traditionally used for the non-MIPS16 stub. */
6450 func_name = targetm.strip_name_encoding (XSTR (func, 0));
6451 local_name = ACONCAT (("__fn_local_", func_name, NULL));
6452 local = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (local_name));
6453 SYMBOL_REF_FLAGS (local) = SYMBOL_REF_FLAGS (func) | SYMBOL_FLAG_LOCAL;
6454
6455 /* Create a new structure to represent the mapping. */
6456 *slot = local;
6457 }
6458 return *slot;
6459 }
6460 \f
6461 /* A chained list of functions for which mips16_build_call_stub has already
6462 generated a stub. NAME is the name of the function and FP_RET_P is true
6463 if the function returns a value in floating-point registers. */
6464 struct mips16_stub {
6465 struct mips16_stub *next;
6466 char *name;
6467 bool fp_ret_p;
6468 };
6469 static struct mips16_stub *mips16_stubs;
6470
6471 /* Return the two-character string that identifies floating-point
6472 return mode MODE in the name of a MIPS16 function stub. */
6473
6474 static const char *
6475 mips16_call_stub_mode_suffix (machine_mode mode)
6476 {
6477 if (mode == SFmode)
6478 return "sf";
6479 else if (mode == DFmode)
6480 return "df";
6481 else if (mode == SCmode)
6482 return "sc";
6483 else if (mode == DCmode)
6484 return "dc";
6485 else if (mode == V2SFmode)
6486 return "df";
6487 else
6488 gcc_unreachable ();
6489 }
6490
6491 /* Write instructions to move a 32-bit value between general register
6492 GPREG and floating-point register FPREG. DIRECTION is 't' to move
6493 from GPREG to FPREG and 'f' to move in the opposite direction. */
6494
6495 static void
6496 mips_output_32bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6497 {
6498 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6499 reg_names[gpreg], reg_names[fpreg]);
6500 }
6501
6502 /* Likewise for 64-bit values. */
6503
6504 static void
6505 mips_output_64bit_xfer (char direction, unsigned int gpreg, unsigned int fpreg)
6506 {
6507 if (TARGET_64BIT)
6508 fprintf (asm_out_file, "\tdm%cc1\t%s,%s\n", direction,
6509 reg_names[gpreg], reg_names[fpreg]);
6510 else if (TARGET_FLOAT64)
6511 {
6512 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6513 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6514 fprintf (asm_out_file, "\tm%chc1\t%s,%s\n", direction,
6515 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg]);
6516 }
6517 else
6518 {
6519 /* Move the least-significant word. */
6520 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6521 reg_names[gpreg + TARGET_BIG_ENDIAN], reg_names[fpreg]);
6522 /* ...then the most significant word. */
6523 fprintf (asm_out_file, "\tm%cc1\t%s,%s\n", direction,
6524 reg_names[gpreg + TARGET_LITTLE_ENDIAN], reg_names[fpreg + 1]);
6525 }
6526 }
6527
6528 /* Write out code to move floating-point arguments into or out of
6529 general registers. FP_CODE is the code describing which arguments
6530 are present (see the comment above the definition of CUMULATIVE_ARGS
6531 in mips.h). DIRECTION is as for mips_output_32bit_xfer. */
6532
6533 static void
6534 mips_output_args_xfer (int fp_code, char direction)
6535 {
6536 unsigned int gparg, fparg, f;
6537 CUMULATIVE_ARGS cum;
6538
6539 /* This code only works for o32 and o64. */
6540 gcc_assert (TARGET_OLDABI);
6541
6542 mips_init_cumulative_args (&cum, NULL);
6543
6544 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6545 {
6546 machine_mode mode;
6547 struct mips_arg_info info;
6548
6549 if ((f & 3) == 1)
6550 mode = SFmode;
6551 else if ((f & 3) == 2)
6552 mode = DFmode;
6553 else
6554 gcc_unreachable ();
6555
6556 mips_get_arg_info (&info, &cum, mode, NULL, true);
6557 gparg = mips_arg_regno (&info, false);
6558 fparg = mips_arg_regno (&info, true);
6559
6560 if (mode == SFmode)
6561 mips_output_32bit_xfer (direction, gparg, fparg);
6562 else
6563 mips_output_64bit_xfer (direction, gparg, fparg);
6564
6565 mips_function_arg_advance (pack_cumulative_args (&cum), mode, NULL, true);
6566 }
6567 }
6568
6569 /* Write a MIPS16 stub for the current function. This stub is used
6570 for functions which take arguments in the floating-point registers.
6571 It is normal-mode code that moves the floating-point arguments
6572 into the general registers and then jumps to the MIPS16 code. */
6573
6574 static void
6575 mips16_build_function_stub (void)
6576 {
6577 const char *fnname, *alias_name, *separator;
6578 char *secname, *stubname;
6579 tree stubdecl;
6580 unsigned int f;
6581 rtx symbol, alias;
6582
6583 /* Create the name of the stub, and its unique section. */
6584 symbol = XEXP (DECL_RTL (current_function_decl), 0);
6585 alias = mips16_local_alias (symbol);
6586
6587 fnname = targetm.strip_name_encoding (XSTR (symbol, 0));
6588 alias_name = targetm.strip_name_encoding (XSTR (alias, 0));
6589 secname = ACONCAT ((".mips16.fn.", fnname, NULL));
6590 stubname = ACONCAT (("__fn_stub_", fnname, NULL));
6591
6592 /* Build a decl for the stub. */
6593 stubdecl = build_decl (BUILTINS_LOCATION,
6594 FUNCTION_DECL, get_identifier (stubname),
6595 build_function_type_list (void_type_node, NULL_TREE));
6596 set_decl_section_name (stubdecl, secname);
6597 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6598 RESULT_DECL, NULL_TREE, void_type_node);
6599
6600 /* Output a comment. */
6601 fprintf (asm_out_file, "\t# Stub function for %s (",
6602 current_function_name ());
6603 separator = "";
6604 for (f = (unsigned int) crtl->args.info.fp_code; f != 0; f >>= 2)
6605 {
6606 fprintf (asm_out_file, "%s%s", separator,
6607 (f & 3) == 1 ? "float" : "double");
6608 separator = ", ";
6609 }
6610 fprintf (asm_out_file, ")\n");
6611
6612 /* Start the function definition. */
6613 assemble_start_function (stubdecl, stubname);
6614 mips_start_function_definition (stubname, false);
6615
6616 /* If generating pic2 code, either set up the global pointer or
6617 switch to pic0. */
6618 if (TARGET_ABICALLS_PIC2)
6619 {
6620 if (TARGET_ABSOLUTE_ABICALLS)
6621 fprintf (asm_out_file, "\t.option\tpic0\n");
6622 else
6623 {
6624 output_asm_insn ("%(.cpload\t%^%)", NULL);
6625 /* Emit an R_MIPS_NONE relocation to tell the linker what the
6626 target function is. Use a local GOT access when loading the
6627 symbol, to cut down on the number of unnecessary GOT entries
6628 for stubs that aren't needed. */
6629 output_asm_insn (".reloc\t0,R_MIPS_NONE,%0", &symbol);
6630 symbol = alias;
6631 }
6632 }
6633
6634 /* Load the address of the MIPS16 function into $25. Do this first so
6635 that targets with coprocessor interlocks can use an MFC1 to fill the
6636 delay slot. */
6637 output_asm_insn ("la\t%^,%0", &symbol);
6638
6639 /* Move the arguments from floating-point registers to general registers. */
6640 mips_output_args_xfer (crtl->args.info.fp_code, 'f');
6641
6642 /* Jump to the MIPS16 function. */
6643 output_asm_insn ("jr\t%^", NULL);
6644
6645 if (TARGET_ABICALLS_PIC2 && TARGET_ABSOLUTE_ABICALLS)
6646 fprintf (asm_out_file, "\t.option\tpic2\n");
6647
6648 mips_end_function_definition (stubname);
6649
6650 /* If the linker needs to create a dynamic symbol for the target
6651 function, it will associate the symbol with the stub (which,
6652 unlike the target function, follows the proper calling conventions).
6653 It is therefore useful to have a local alias for the target function,
6654 so that it can still be identified as MIPS16 code. As an optimization,
6655 this symbol can also be used for indirect MIPS16 references from
6656 within this file. */
6657 ASM_OUTPUT_DEF (asm_out_file, alias_name, fnname);
6658
6659 switch_to_section (function_section (current_function_decl));
6660 }
6661
6662 /* The current function is a MIPS16 function that returns a value in an FPR.
6663 Copy the return value from its soft-float to its hard-float location.
6664 libgcc2 has special non-MIPS16 helper functions for each case. */
6665
6666 static void
6667 mips16_copy_fpr_return_value (void)
6668 {
6669 rtx fn, insn, retval;
6670 tree return_type;
6671 machine_mode return_mode;
6672 const char *name;
6673
6674 return_type = DECL_RESULT (current_function_decl);
6675 return_mode = DECL_MODE (return_type);
6676
6677 name = ACONCAT (("__mips16_ret_",
6678 mips16_call_stub_mode_suffix (return_mode),
6679 NULL));
6680 fn = mips16_stub_function (name);
6681
6682 /* The function takes arguments in $2 (and possibly $3), so calls
6683 to it cannot be lazily bound. */
6684 SYMBOL_REF_FLAGS (fn) |= SYMBOL_FLAG_BIND_NOW;
6685
6686 /* Model the call as something that takes the GPR return value as
6687 argument and returns an "updated" value. */
6688 retval = gen_rtx_REG (return_mode, GP_RETURN);
6689 insn = mips_expand_call (MIPS_CALL_EPILOGUE, retval, fn,
6690 const0_rtx, NULL_RTX, false);
6691 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), retval);
6692 }
6693
6694 /* Consider building a stub for a MIPS16 call to function *FN_PTR.
6695 RETVAL is the location of the return value, or null if this is
6696 a "call" rather than a "call_value". ARGS_SIZE is the size of the
6697 arguments and FP_CODE is the code built by mips_function_arg;
6698 see the comment before the fp_code field in CUMULATIVE_ARGS for details.
6699
6700 There are three alternatives:
6701
6702 - If a stub was needed, emit the call and return the call insn itself.
6703
6704 - If we can avoid using a stub by redirecting the call, set *FN_PTR
6705 to the new target and return null.
6706
6707 - If *FN_PTR doesn't need a stub, return null and leave *FN_PTR
6708 unmodified.
6709
6710 A stub is needed for calls to functions that, in normal mode,
6711 receive arguments in FPRs or return values in FPRs. The stub
6712 copies the arguments from their soft-float positions to their
6713 hard-float positions, calls the real function, then copies the
6714 return value from its hard-float position to its soft-float
6715 position.
6716
6717 We can emit a JAL to *FN_PTR even when *FN_PTR might need a stub.
6718 If *FN_PTR turns out to be to a non-MIPS16 function, the linker
6719 automatically redirects the JAL to the stub, otherwise the JAL
6720 continues to call FN directly. */
6721
6722 static rtx_insn *
6723 mips16_build_call_stub (rtx retval, rtx *fn_ptr, rtx args_size, int fp_code)
6724 {
6725 const char *fnname;
6726 bool fp_ret_p;
6727 struct mips16_stub *l;
6728 rtx_insn *insn;
6729 rtx pattern, 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;
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;
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;
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;
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, addr;
6771 rtx_insn *insn;
6772 bool lazy_p;
6773
6774 /* If this is a locally-defined and locally-binding function,
6775 avoid the stub by calling the local alias directly. */
6776 if (mips16_local_function_p (fn))
6777 {
6778 *fn_ptr = mips16_local_alias (fn);
6779 return NULL;
6780 }
6781
6782 /* Create a SYMBOL_REF for the libgcc.a function. */
6783 if (fp_ret_p)
6784 sprintf (buf, "__mips16_call_stub_%s_%d",
6785 mips16_call_stub_mode_suffix (GET_MODE (retval)),
6786 fp_code);
6787 else
6788 sprintf (buf, "__mips16_call_stub_%d", fp_code);
6789 stub_fn = mips16_stub_function (buf);
6790
6791 /* The function uses $2 as an argument, so calls to it
6792 cannot be lazily bound. */
6793 SYMBOL_REF_FLAGS (stub_fn) |= SYMBOL_FLAG_BIND_NOW;
6794
6795 /* Load the target function into $2. */
6796 addr = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
6797 lazy_p = mips_load_call_address (MIPS_CALL_NORMAL, addr, fn);
6798
6799 /* Emit the call. */
6800 insn = mips_expand_call (MIPS_CALL_NORMAL, retval, stub_fn,
6801 args_size, NULL_RTX, lazy_p);
6802
6803 /* Tell GCC that this call does indeed use the value of $2. */
6804 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), addr);
6805
6806 /* If we are handling a floating-point return value, we need to
6807 save $18 in the function prologue. Putting a note on the
6808 call will mean that df_regs_ever_live_p ($18) will be true if the
6809 call is not eliminated, and we can check that in the prologue
6810 code. */
6811 if (fp_ret_p)
6812 CALL_INSN_FUNCTION_USAGE (insn) =
6813 gen_rtx_EXPR_LIST (VOIDmode,
6814 gen_rtx_CLOBBER (VOIDmode,
6815 gen_rtx_REG (word_mode, 18)),
6816 CALL_INSN_FUNCTION_USAGE (insn));
6817
6818 return insn;
6819 }
6820
6821 /* We know the function we are going to call. If we have already
6822 built a stub, we don't need to do anything further. */
6823 fnname = targetm.strip_name_encoding (XSTR (fn, 0));
6824 for (l = mips16_stubs; l != NULL; l = l->next)
6825 if (strcmp (l->name, fnname) == 0)
6826 break;
6827
6828 if (l == NULL)
6829 {
6830 const char *separator;
6831 char *secname, *stubname;
6832 tree stubid, stubdecl;
6833 unsigned int f;
6834
6835 /* If the function does not return in FPRs, the special stub
6836 section is named
6837 .mips16.call.FNNAME
6838
6839 If the function does return in FPRs, the stub section is named
6840 .mips16.call.fp.FNNAME
6841
6842 Build a decl for the stub. */
6843 secname = ACONCAT ((".mips16.call.", fp_ret_p ? "fp." : "",
6844 fnname, NULL));
6845 stubname = ACONCAT (("__call_stub_", fp_ret_p ? "fp_" : "",
6846 fnname, NULL));
6847 stubid = get_identifier (stubname);
6848 stubdecl = build_decl (BUILTINS_LOCATION,
6849 FUNCTION_DECL, stubid,
6850 build_function_type_list (void_type_node,
6851 NULL_TREE));
6852 set_decl_section_name (stubdecl, secname);
6853 DECL_RESULT (stubdecl) = build_decl (BUILTINS_LOCATION,
6854 RESULT_DECL, NULL_TREE,
6855 void_type_node);
6856
6857 /* Output a comment. */
6858 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
6859 (fp_ret_p
6860 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
6861 : ""),
6862 fnname);
6863 separator = "";
6864 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
6865 {
6866 fprintf (asm_out_file, "%s%s", separator,
6867 (f & 3) == 1 ? "float" : "double");
6868 separator = ", ";
6869 }
6870 fprintf (asm_out_file, ")\n");
6871
6872 /* Start the function definition. */
6873 assemble_start_function (stubdecl, stubname);
6874 mips_start_function_definition (stubname, false);
6875
6876 if (fp_ret_p)
6877 {
6878 fprintf (asm_out_file, "\t.cfi_startproc\n");
6879
6880 /* Create a fake CFA 4 bytes below the stack pointer.
6881 This works around unwinders (like libgcc's) that expect
6882 the CFA for non-signal frames to be unique. */
6883 fprintf (asm_out_file, "\t.cfi_def_cfa 29,-4\n");
6884
6885 /* "Save" $sp in itself so we don't use the fake CFA.
6886 This is: DW_CFA_val_expression r29, { DW_OP_reg29 }. */
6887 fprintf (asm_out_file, "\t.cfi_escape 0x16,29,1,0x6d\n");
6888 }
6889 else
6890 {
6891 /* Load the address of the MIPS16 function into $25. Do this
6892 first so that targets with coprocessor interlocks can use
6893 an MFC1 to fill the delay slot. */
6894 if (TARGET_EXPLICIT_RELOCS)
6895 {
6896 output_asm_insn ("lui\t%^,%%hi(%0)", &fn);
6897 output_asm_insn ("addiu\t%^,%^,%%lo(%0)", &fn);
6898 }
6899 else
6900 output_asm_insn ("la\t%^,%0", &fn);
6901 }
6902
6903 /* Move the arguments from general registers to floating-point
6904 registers. */
6905 mips_output_args_xfer (fp_code, 't');
6906
6907 if (fp_ret_p)
6908 {
6909 /* Save the return address in $18 and call the non-MIPS16 function.
6910 The stub's caller knows that $18 might be clobbered, even though
6911 $18 is usually a call-saved register. */
6912 fprintf (asm_out_file, "\tmove\t%s,%s\n",
6913 reg_names[GP_REG_FIRST + 18], reg_names[RETURN_ADDR_REGNUM]);
6914 output_asm_insn (MIPS_CALL ("jal", &fn, 0, -1), &fn);
6915 fprintf (asm_out_file, "\t.cfi_register 31,18\n");
6916
6917 /* Move the result from floating-point registers to
6918 general registers. */
6919 switch (GET_MODE (retval))
6920 {
6921 case SCmode:
6922 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_BIG_ENDIAN,
6923 TARGET_BIG_ENDIAN
6924 ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6925 : FP_REG_FIRST);
6926 mips_output_32bit_xfer ('f', GP_RETURN + TARGET_LITTLE_ENDIAN,
6927 TARGET_LITTLE_ENDIAN
6928 ? FP_REG_FIRST + MAX_FPRS_PER_FMT
6929 : FP_REG_FIRST);
6930 if (GET_MODE (retval) == SCmode && TARGET_64BIT)
6931 {
6932 /* On 64-bit targets, complex floats are returned in
6933 a single GPR, such that "sd" on a suitably-aligned
6934 target would store the value correctly. */
6935 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6936 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6937 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6938 fprintf (asm_out_file, "\tdsll\t%s,%s,32\n",
6939 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN],
6940 reg_names[GP_RETURN + TARGET_LITTLE_ENDIAN]);
6941 fprintf (asm_out_file, "\tdsrl\t%s,%s,32\n",
6942 reg_names[GP_RETURN + TARGET_BIG_ENDIAN],
6943 reg_names[GP_RETURN + TARGET_BIG_ENDIAN]);
6944 fprintf (asm_out_file, "\tor\t%s,%s,%s\n",
6945 reg_names[GP_RETURN],
6946 reg_names[GP_RETURN],
6947 reg_names[GP_RETURN + 1]);
6948 }
6949 break;
6950
6951 case SFmode:
6952 mips_output_32bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6953 break;
6954
6955 case DCmode:
6956 mips_output_64bit_xfer ('f', GP_RETURN + (8 / UNITS_PER_WORD),
6957 FP_REG_FIRST + MAX_FPRS_PER_FMT);
6958 /* Fall though. */
6959 case DFmode:
6960 case V2SFmode:
6961 mips_output_64bit_xfer ('f', GP_RETURN, FP_REG_FIRST);
6962 break;
6963
6964 default:
6965 gcc_unreachable ();
6966 }
6967 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 18]);
6968 fprintf (asm_out_file, "\t.cfi_endproc\n");
6969 }
6970 else
6971 {
6972 /* Jump to the previously-loaded address. */
6973 output_asm_insn ("jr\t%^", NULL);
6974 }
6975
6976 #ifdef ASM_DECLARE_FUNCTION_SIZE
6977 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
6978 #endif
6979
6980 mips_end_function_definition (stubname);
6981
6982 /* Record this stub. */
6983 l = XNEW (struct mips16_stub);
6984 l->name = xstrdup (fnname);
6985 l->fp_ret_p = fp_ret_p;
6986 l->next = mips16_stubs;
6987 mips16_stubs = l;
6988 }
6989
6990 /* If we expect a floating-point return value, but we've built a
6991 stub which does not expect one, then we're in trouble. We can't
6992 use the existing stub, because it won't handle the floating-point
6993 value. We can't build a new stub, because the linker won't know
6994 which stub to use for the various calls in this object file.
6995 Fortunately, this case is illegal, since it means that a function
6996 was declared in two different ways in a single compilation. */
6997 if (fp_ret_p && !l->fp_ret_p)
6998 error ("cannot handle inconsistent calls to %qs", fnname);
6999
7000 if (retval == NULL_RTX)
7001 pattern = gen_call_internal_direct (fn, args_size);
7002 else
7003 pattern = gen_call_value_internal_direct (retval, fn, args_size);
7004 insn = mips_emit_call_insn (pattern, fn, fn, false);
7005
7006 /* If we are calling a stub which handles a floating-point return
7007 value, we need to arrange to save $18 in the prologue. We do this
7008 by marking the function call as using the register. The prologue
7009 will later see that it is used, and emit code to save it. */
7010 if (fp_ret_p)
7011 CALL_INSN_FUNCTION_USAGE (insn) =
7012 gen_rtx_EXPR_LIST (VOIDmode,
7013 gen_rtx_CLOBBER (VOIDmode,
7014 gen_rtx_REG (word_mode, 18)),
7015 CALL_INSN_FUNCTION_USAGE (insn));
7016
7017 return insn;
7018 }
7019 \f
7020 /* Expand a call of type TYPE. RESULT is where the result will go (null
7021 for "call"s and "sibcall"s), ADDR is the address of the function,
7022 ARGS_SIZE is the size of the arguments and AUX is the value passed
7023 to us by mips_function_arg. LAZY_P is true if this call already
7024 involves a lazily-bound function address (such as when calling
7025 functions through a MIPS16 hard-float stub).
7026
7027 Return the call itself. */
7028
7029 rtx_insn *
7030 mips_expand_call (enum mips_call_type type, rtx result, rtx addr,
7031 rtx args_size, rtx aux, bool lazy_p)
7032 {
7033 rtx orig_addr, pattern;
7034 rtx_insn *insn;
7035 int fp_code;
7036
7037 fp_code = aux == 0 ? 0 : (int) GET_MODE (aux);
7038 insn = mips16_build_call_stub (result, &addr, args_size, fp_code);
7039 if (insn)
7040 {
7041 gcc_assert (!lazy_p && type == MIPS_CALL_NORMAL);
7042 return insn;
7043 }
7044
7045 orig_addr = addr;
7046 if (!call_insn_operand (addr, VOIDmode))
7047 {
7048 if (type == MIPS_CALL_EPILOGUE)
7049 addr = MIPS_EPILOGUE_TEMP (Pmode);
7050 else
7051 addr = gen_reg_rtx (Pmode);
7052 lazy_p |= mips_load_call_address (type, addr, orig_addr);
7053 }
7054
7055 if (result == 0)
7056 {
7057 rtx (*fn) (rtx, rtx);
7058
7059 if (type == MIPS_CALL_SIBCALL)
7060 fn = gen_sibcall_internal;
7061 else
7062 fn = gen_call_internal;
7063
7064 pattern = fn (addr, args_size);
7065 }
7066 else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
7067 {
7068 /* Handle return values created by mips_return_fpr_pair. */
7069 rtx (*fn) (rtx, rtx, rtx, rtx);
7070 rtx reg1, reg2;
7071
7072 if (type == MIPS_CALL_SIBCALL)
7073 fn = gen_sibcall_value_multiple_internal;
7074 else
7075 fn = gen_call_value_multiple_internal;
7076
7077 reg1 = XEXP (XVECEXP (result, 0, 0), 0);
7078 reg2 = XEXP (XVECEXP (result, 0, 1), 0);
7079 pattern = fn (reg1, addr, args_size, reg2);
7080 }
7081 else
7082 {
7083 rtx (*fn) (rtx, rtx, rtx);
7084
7085 if (type == MIPS_CALL_SIBCALL)
7086 fn = gen_sibcall_value_internal;
7087 else
7088 fn = gen_call_value_internal;
7089
7090 /* Handle return values created by mips_return_fpr_single. */
7091 if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 1)
7092 result = XEXP (XVECEXP (result, 0, 0), 0);
7093 pattern = fn (result, addr, args_size);
7094 }
7095
7096 return mips_emit_call_insn (pattern, orig_addr, addr, lazy_p);
7097 }
7098
7099 /* Split call instruction INSN into a $gp-clobbering call and
7100 (where necessary) an instruction to restore $gp from its save slot.
7101 CALL_PATTERN is the pattern of the new call. */
7102
7103 void
7104 mips_split_call (rtx insn, rtx call_pattern)
7105 {
7106 emit_call_insn (call_pattern);
7107 if (!find_reg_note (insn, REG_NORETURN, 0))
7108 mips_restore_gp_from_cprestore_slot (gen_rtx_REG (Pmode,
7109 POST_CALL_TMP_REG));
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 /* Implement TARGET_USE_MOVE_BY_PIECES_INFRASTRUCTURE_P. */
7177
7178 bool
7179 mips_use_by_pieces_infrastructure_p (unsigned int size,
7180 unsigned int align,
7181 enum by_pieces_operation op,
7182 bool speed_p)
7183 {
7184 if (op == STORE_BY_PIECES)
7185 return mips_store_by_pieces_p (size, align);
7186 if (op == MOVE_BY_PIECES && HAVE_movmemsi)
7187 {
7188 /* movmemsi is meant to generate code that is at least as good as
7189 move_by_pieces. However, movmemsi effectively uses a by-pieces
7190 implementation both for moves smaller than a word and for
7191 word-aligned moves of no more than MIPS_MAX_MOVE_BYTES_STRAIGHT
7192 bytes. We should allow the tree-level optimisers to do such
7193 moves by pieces, as it often exposes other optimization
7194 opportunities. We might as well continue to use movmemsi at
7195 the rtl level though, as it produces better code when
7196 scheduling is disabled (such as at -O). */
7197 if (currently_expanding_to_rtl)
7198 return false;
7199 if (align < BITS_PER_WORD)
7200 return size < UNITS_PER_WORD;
7201 return size <= MIPS_MAX_MOVE_BYTES_STRAIGHT;
7202 }
7203
7204 return default_use_by_pieces_infrastructure_p (size, align, op, speed_p);
7205 }
7206
7207 /* Implement a handler for STORE_BY_PIECES operations
7208 for TARGET_USE_MOVE_BY_PIECES_INFRASTRUCTURE_P. */
7209
7210 bool
7211 mips_store_by_pieces_p (unsigned HOST_WIDE_INT size, unsigned int align)
7212 {
7213 /* Storing by pieces involves moving constants into registers
7214 of size MIN (ALIGN, BITS_PER_WORD), then storing them.
7215 We need to decide whether it is cheaper to load the address of
7216 constant data into a register and use a block move instead. */
7217
7218 /* If the data is only byte aligned, then:
7219
7220 (a1) A block move of less than 4 bytes would involve three 3 LBs and
7221 3 SBs. We might as well use 3 single-instruction LIs and 3 SBs
7222 instead.
7223
7224 (a2) A block move of 4 bytes from aligned source data can use an
7225 LW/SWL/SWR sequence. This is often better than the 4 LIs and
7226 4 SBs that we would generate when storing by pieces. */
7227 if (align <= BITS_PER_UNIT)
7228 return size < 4;
7229
7230 /* If the data is 2-byte aligned, then:
7231
7232 (b1) A block move of less than 4 bytes would use a combination of LBs,
7233 LHs, SBs and SHs. We get better code by using single-instruction
7234 LIs, SBs and SHs instead.
7235
7236 (b2) A block move of 4 bytes from aligned source data would again use
7237 an LW/SWL/SWR sequence. In most cases, loading the address of
7238 the source data would require at least one extra instruction.
7239 It is often more efficient to use 2 single-instruction LIs and
7240 2 SHs instead.
7241
7242 (b3) A block move of up to 3 additional bytes would be like (b1).
7243
7244 (b4) A block move of 8 bytes from aligned source data can use two
7245 LW/SWL/SWR sequences or a single LD/SDL/SDR sequence. Both
7246 sequences are better than the 4 LIs and 4 SHs that we'd generate
7247 when storing by pieces.
7248
7249 The reasoning for higher alignments is similar:
7250
7251 (c1) A block move of less than 4 bytes would be the same as (b1).
7252
7253 (c2) A block move of 4 bytes would use an LW/SW sequence. Again,
7254 loading the address of the source data would typically require
7255 at least one extra instruction. It is generally better to use
7256 LUI/ORI/SW instead.
7257
7258 (c3) A block move of up to 3 additional bytes would be like (b1).
7259
7260 (c4) A block move of 8 bytes can use two LW/SW sequences or a single
7261 LD/SD sequence, and in these cases we've traditionally preferred
7262 the memory copy over the more bulky constant moves. */
7263 return size < 8;
7264 }
7265
7266 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
7267 Assume that the areas do not overlap. */
7268
7269 static void
7270 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
7271 {
7272 HOST_WIDE_INT offset, delta;
7273 unsigned HOST_WIDE_INT bits;
7274 int i;
7275 machine_mode mode;
7276 rtx *regs;
7277
7278 /* Work out how many bits to move at a time. If both operands have
7279 half-word alignment, it is usually better to move in half words.
7280 For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
7281 and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
7282 Otherwise move word-sized chunks. */
7283 if (MEM_ALIGN (src) == BITS_PER_WORD / 2
7284 && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
7285 bits = BITS_PER_WORD / 2;
7286 else
7287 bits = BITS_PER_WORD;
7288
7289 mode = mode_for_size (bits, MODE_INT, 0);
7290 delta = bits / BITS_PER_UNIT;
7291
7292 /* Allocate a buffer for the temporary registers. */
7293 regs = XALLOCAVEC (rtx, length / delta);
7294
7295 /* Load as many BITS-sized chunks as possible. Use a normal load if
7296 the source has enough alignment, otherwise use left/right pairs. */
7297 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7298 {
7299 regs[i] = gen_reg_rtx (mode);
7300 if (MEM_ALIGN (src) >= bits)
7301 mips_emit_move (regs[i], adjust_address (src, mode, offset));
7302 else
7303 {
7304 rtx part = adjust_address (src, BLKmode, offset);
7305 set_mem_size (part, delta);
7306 if (!mips_expand_ext_as_unaligned_load (regs[i], part, bits, 0, 0))
7307 gcc_unreachable ();
7308 }
7309 }
7310
7311 /* Copy the chunks to the destination. */
7312 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
7313 if (MEM_ALIGN (dest) >= bits)
7314 mips_emit_move (adjust_address (dest, mode, offset), regs[i]);
7315 else
7316 {
7317 rtx part = adjust_address (dest, BLKmode, offset);
7318 set_mem_size (part, delta);
7319 if (!mips_expand_ins_as_unaligned_store (part, regs[i], bits, 0))
7320 gcc_unreachable ();
7321 }
7322
7323 /* Mop up any left-over bytes. */
7324 if (offset < length)
7325 {
7326 src = adjust_address (src, BLKmode, offset);
7327 dest = adjust_address (dest, BLKmode, offset);
7328 move_by_pieces (dest, src, length - offset,
7329 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
7330 }
7331 }
7332
7333 /* Helper function for doing a loop-based block operation on memory
7334 reference MEM. Each iteration of the loop will operate on LENGTH
7335 bytes of MEM.
7336
7337 Create a new base register for use within the loop and point it to
7338 the start of MEM. Create a new memory reference that uses this
7339 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
7340
7341 static void
7342 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
7343 rtx *loop_reg, rtx *loop_mem)
7344 {
7345 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
7346
7347 /* Although the new mem does not refer to a known location,
7348 it does keep up to LENGTH bytes of alignment. */
7349 *loop_mem = change_address (mem, BLKmode, *loop_reg);
7350 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
7351 }
7352
7353 /* Move LENGTH bytes from SRC to DEST using a loop that moves BYTES_PER_ITER
7354 bytes at a time. LENGTH must be at least BYTES_PER_ITER. Assume that
7355 the memory regions do not overlap. */
7356
7357 static void
7358 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length,
7359 HOST_WIDE_INT bytes_per_iter)
7360 {
7361 rtx_code_label *label;
7362 rtx src_reg, dest_reg, final_src, test;
7363 HOST_WIDE_INT leftover;
7364
7365 leftover = length % bytes_per_iter;
7366 length -= leftover;
7367
7368 /* Create registers and memory references for use within the loop. */
7369 mips_adjust_block_mem (src, bytes_per_iter, &src_reg, &src);
7370 mips_adjust_block_mem (dest, bytes_per_iter, &dest_reg, &dest);
7371
7372 /* Calculate the value that SRC_REG should have after the last iteration
7373 of the loop. */
7374 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
7375 0, 0, OPTAB_WIDEN);
7376
7377 /* Emit the start of the loop. */
7378 label = gen_label_rtx ();
7379 emit_label (label);
7380
7381 /* Emit the loop body. */
7382 mips_block_move_straight (dest, src, bytes_per_iter);
7383
7384 /* Move on to the next block. */
7385 mips_emit_move (src_reg, plus_constant (Pmode, src_reg, bytes_per_iter));
7386 mips_emit_move (dest_reg, plus_constant (Pmode, dest_reg, bytes_per_iter));
7387
7388 /* Emit the loop condition. */
7389 test = gen_rtx_NE (VOIDmode, src_reg, final_src);
7390 if (Pmode == DImode)
7391 emit_jump_insn (gen_cbranchdi4 (test, src_reg, final_src, label));
7392 else
7393 emit_jump_insn (gen_cbranchsi4 (test, src_reg, final_src, label));
7394
7395 /* Mop up any left-over bytes. */
7396 if (leftover)
7397 mips_block_move_straight (dest, src, leftover);
7398 }
7399
7400 /* Expand a movmemsi instruction, which copies LENGTH bytes from
7401 memory reference SRC to memory reference DEST. */
7402
7403 bool
7404 mips_expand_block_move (rtx dest, rtx src, rtx length)
7405 {
7406 if (CONST_INT_P (length))
7407 {
7408 if (INTVAL (length) <= MIPS_MAX_MOVE_BYTES_STRAIGHT)
7409 {
7410 mips_block_move_straight (dest, src, INTVAL (length));
7411 return true;
7412 }
7413 else if (optimize)
7414 {
7415 mips_block_move_loop (dest, src, INTVAL (length),
7416 MIPS_MAX_MOVE_BYTES_PER_LOOP_ITER);
7417 return true;
7418 }
7419 }
7420 return false;
7421 }
7422 \f
7423 /* Expand a loop of synci insns for the address range [BEGIN, END). */
7424
7425 void
7426 mips_expand_synci_loop (rtx begin, rtx end)
7427 {
7428 rtx inc, cmp_result, mask, length;
7429 rtx_code_label *label, *end_label;
7430
7431 /* Create end_label. */
7432 end_label = gen_label_rtx ();
7433
7434 /* Check if begin equals end. */
7435 cmp_result = gen_rtx_EQ (VOIDmode, begin, end);
7436 emit_jump_insn (gen_condjump (cmp_result, end_label));
7437
7438 /* Load INC with the cache line size (rdhwr INC,$1). */
7439 inc = gen_reg_rtx (Pmode);
7440 emit_insn (PMODE_INSN (gen_rdhwr_synci_step, (inc)));
7441
7442 /* Check if inc is 0. */
7443 cmp_result = gen_rtx_EQ (VOIDmode, inc, const0_rtx);
7444 emit_jump_insn (gen_condjump (cmp_result, end_label));
7445
7446 /* Calculate mask. */
7447 mask = mips_force_unary (Pmode, NEG, inc);
7448
7449 /* Mask out begin by mask. */
7450 begin = mips_force_binary (Pmode, AND, begin, mask);
7451
7452 /* Calculate length. */
7453 length = mips_force_binary (Pmode, MINUS, end, begin);
7454
7455 /* Loop back to here. */
7456 label = gen_label_rtx ();
7457 emit_label (label);
7458
7459 emit_insn (gen_synci (begin));
7460
7461 /* Update length. */
7462 mips_emit_binary (MINUS, length, length, inc);
7463
7464 /* Update begin. */
7465 mips_emit_binary (PLUS, begin, begin, inc);
7466
7467 /* Check if length is greater than 0. */
7468 cmp_result = gen_rtx_GT (VOIDmode, length, const0_rtx);
7469 emit_jump_insn (gen_condjump (cmp_result, label));
7470
7471 emit_label (end_label);
7472 }
7473 \f
7474 /* Expand a QI or HI mode atomic memory operation.
7475
7476 GENERATOR contains a pointer to the gen_* function that generates
7477 the SI mode underlying atomic operation using masks that we
7478 calculate.
7479
7480 RESULT is the return register for the operation. Its value is NULL
7481 if unused.
7482
7483 MEM is the location of the atomic access.
7484
7485 OLDVAL is the first operand for the operation.
7486
7487 NEWVAL is the optional second operand for the operation. Its value
7488 is NULL if unused. */
7489
7490 void
7491 mips_expand_atomic_qihi (union mips_gen_fn_ptrs generator,
7492 rtx result, rtx mem, rtx oldval, rtx newval)
7493 {
7494 rtx orig_addr, memsi_addr, memsi, shift, shiftsi, unshifted_mask;
7495 rtx unshifted_mask_reg, mask, inverted_mask, si_op;
7496 rtx res = NULL;
7497 machine_mode mode;
7498
7499 mode = GET_MODE (mem);
7500
7501 /* Compute the address of the containing SImode value. */
7502 orig_addr = force_reg (Pmode, XEXP (mem, 0));
7503 memsi_addr = mips_force_binary (Pmode, AND, orig_addr,
7504 force_reg (Pmode, GEN_INT (-4)));
7505
7506 /* Create a memory reference for it. */
7507 memsi = gen_rtx_MEM (SImode, memsi_addr);
7508 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
7509 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
7510
7511 /* Work out the byte offset of the QImode or HImode value,
7512 counting from the least significant byte. */
7513 shift = mips_force_binary (Pmode, AND, orig_addr, GEN_INT (3));
7514 if (TARGET_BIG_ENDIAN)
7515 mips_emit_binary (XOR, shift, shift, GEN_INT (mode == QImode ? 3 : 2));
7516
7517 /* Multiply by eight to convert the shift value from bytes to bits. */
7518 mips_emit_binary (ASHIFT, shift, shift, GEN_INT (3));
7519
7520 /* Make the final shift an SImode value, so that it can be used in
7521 SImode operations. */
7522 shiftsi = force_reg (SImode, gen_lowpart (SImode, shift));
7523
7524 /* Set MASK to an inclusive mask of the QImode or HImode value. */
7525 unshifted_mask = GEN_INT (GET_MODE_MASK (mode));
7526 unshifted_mask_reg = force_reg (SImode, unshifted_mask);
7527 mask = mips_force_binary (SImode, ASHIFT, unshifted_mask_reg, shiftsi);
7528
7529 /* Compute the equivalent exclusive mask. */
7530 inverted_mask = gen_reg_rtx (SImode);
7531 emit_insn (gen_rtx_SET (VOIDmode, inverted_mask,
7532 gen_rtx_NOT (SImode, mask)));
7533
7534 /* Shift the old value into place. */
7535 if (oldval != const0_rtx)
7536 {
7537 oldval = convert_modes (SImode, mode, oldval, true);
7538 oldval = force_reg (SImode, oldval);
7539 oldval = mips_force_binary (SImode, ASHIFT, oldval, shiftsi);
7540 }
7541
7542 /* Do the same for the new value. */
7543 if (newval && newval != const0_rtx)
7544 {
7545 newval = convert_modes (SImode, mode, newval, true);
7546 newval = force_reg (SImode, newval);
7547 newval = mips_force_binary (SImode, ASHIFT, newval, shiftsi);
7548 }
7549
7550 /* Do the SImode atomic access. */
7551 if (result)
7552 res = gen_reg_rtx (SImode);
7553 if (newval)
7554 si_op = generator.fn_6 (res, memsi, mask, inverted_mask, oldval, newval);
7555 else if (result)
7556 si_op = generator.fn_5 (res, memsi, mask, inverted_mask, oldval);
7557 else
7558 si_op = generator.fn_4 (memsi, mask, inverted_mask, oldval);
7559
7560 emit_insn (si_op);
7561
7562 if (result)
7563 {
7564 /* Shift and convert the result. */
7565 mips_emit_binary (AND, res, res, mask);
7566 mips_emit_binary (LSHIFTRT, res, res, shiftsi);
7567 mips_emit_move (result, gen_lowpart (GET_MODE (result), res));
7568 }
7569 }
7570
7571 /* Return true if it is possible to use left/right accesses for a
7572 bitfield of WIDTH bits starting BITPOS bits into BLKmode memory OP.
7573 When returning true, update *LEFT and *RIGHT as follows:
7574
7575 *LEFT is a QImode reference to the first byte if big endian or
7576 the last byte if little endian. This address can be used in the
7577 left-side instructions (LWL, SWL, LDL, SDL).
7578
7579 *RIGHT is a QImode reference to the opposite end of the field and
7580 can be used in the patterning right-side instruction. */
7581
7582 static bool
7583 mips_get_unaligned_mem (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos,
7584 rtx *left, rtx *right)
7585 {
7586 rtx first, last;
7587
7588 /* Check that the size is valid. */
7589 if (width != 32 && (!TARGET_64BIT || width != 64))
7590 return false;
7591
7592 /* We can only access byte-aligned values. Since we are always passed
7593 a reference to the first byte of the field, it is not necessary to
7594 do anything with BITPOS after this check. */
7595 if (bitpos % BITS_PER_UNIT != 0)
7596 return false;
7597
7598 /* Reject aligned bitfields: we want to use a normal load or store
7599 instead of a left/right pair. */
7600 if (MEM_ALIGN (op) >= width)
7601 return false;
7602
7603 /* Get references to both ends of the field. */
7604 first = adjust_address (op, QImode, 0);
7605 last = adjust_address (op, QImode, width / BITS_PER_UNIT - 1);
7606
7607 /* Allocate to LEFT and RIGHT according to endianness. LEFT should
7608 correspond to the MSB and RIGHT to the LSB. */
7609 if (TARGET_BIG_ENDIAN)
7610 *left = first, *right = last;
7611 else
7612 *left = last, *right = first;
7613
7614 return true;
7615 }
7616
7617 /* Try to use left/right loads to expand an "extv" or "extzv" pattern.
7618 DEST, SRC, WIDTH and BITPOS are the operands passed to the expander;
7619 the operation is the equivalent of:
7620
7621 (set DEST (*_extract SRC WIDTH BITPOS))
7622
7623 Return true on success. */
7624
7625 bool
7626 mips_expand_ext_as_unaligned_load (rtx dest, rtx src, HOST_WIDE_INT width,
7627 HOST_WIDE_INT bitpos, bool unsigned_p)
7628 {
7629 rtx left, right, temp;
7630 rtx dest1 = NULL_RTX;
7631
7632 /* If TARGET_64BIT, the destination of a 32-bit "extz" or "extzv" will
7633 be a DImode, create a new temp and emit a zero extend at the end. */
7634 if (GET_MODE (dest) == DImode
7635 && REG_P (dest)
7636 && GET_MODE_BITSIZE (SImode) == width)
7637 {
7638 dest1 = dest;
7639 dest = gen_reg_rtx (SImode);
7640 }
7641
7642 if (!mips_get_unaligned_mem (src, width, bitpos, &left, &right))
7643 return false;
7644
7645 temp = gen_reg_rtx (GET_MODE (dest));
7646 if (GET_MODE (dest) == DImode)
7647 {
7648 emit_insn (gen_mov_ldl (temp, src, left));
7649 emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
7650 }
7651 else
7652 {
7653 emit_insn (gen_mov_lwl (temp, src, left));
7654 emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
7655 }
7656
7657 /* If we were loading 32bits and the original register was DI then
7658 sign/zero extend into the orignal dest. */
7659 if (dest1)
7660 {
7661 if (unsigned_p)
7662 emit_insn (gen_zero_extendsidi2 (dest1, dest));
7663 else
7664 emit_insn (gen_extendsidi2 (dest1, dest));
7665 }
7666 return true;
7667 }
7668
7669 /* Try to use left/right stores to expand an "ins" pattern. DEST, WIDTH,
7670 BITPOS and SRC are the operands passed to the expander; the operation
7671 is the equivalent of:
7672
7673 (set (zero_extract DEST WIDTH BITPOS) SRC)
7674
7675 Return true on success. */
7676
7677 bool
7678 mips_expand_ins_as_unaligned_store (rtx dest, rtx src, HOST_WIDE_INT width,
7679 HOST_WIDE_INT bitpos)
7680 {
7681 rtx left, right;
7682 machine_mode mode;
7683
7684 if (!mips_get_unaligned_mem (dest, width, bitpos, &left, &right))
7685 return false;
7686
7687 mode = mode_for_size (width, MODE_INT, 0);
7688 src = gen_lowpart (mode, src);
7689 if (mode == DImode)
7690 {
7691 emit_insn (gen_mov_sdl (dest, src, left));
7692 emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
7693 }
7694 else
7695 {
7696 emit_insn (gen_mov_swl (dest, src, left));
7697 emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
7698 }
7699 return true;
7700 }
7701
7702 /* Return true if X is a MEM with the same size as MODE. */
7703
7704 bool
7705 mips_mem_fits_mode_p (machine_mode mode, rtx x)
7706 {
7707 return (MEM_P (x)
7708 && MEM_SIZE_KNOWN_P (x)
7709 && MEM_SIZE (x) == GET_MODE_SIZE (mode));
7710 }
7711
7712 /* Return true if (zero_extract OP WIDTH BITPOS) can be used as the
7713 source of an "ext" instruction or the destination of an "ins"
7714 instruction. OP must be a register operand and the following
7715 conditions must hold:
7716
7717 0 <= BITPOS < GET_MODE_BITSIZE (GET_MODE (op))
7718 0 < WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7719 0 < BITPOS + WIDTH <= GET_MODE_BITSIZE (GET_MODE (op))
7720
7721 Also reject lengths equal to a word as they are better handled
7722 by the move patterns. */
7723
7724 bool
7725 mips_use_ins_ext_p (rtx op, HOST_WIDE_INT width, HOST_WIDE_INT bitpos)
7726 {
7727 if (!ISA_HAS_EXT_INS
7728 || !register_operand (op, VOIDmode)
7729 || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
7730 return false;
7731
7732 if (!IN_RANGE (width, 1, GET_MODE_BITSIZE (GET_MODE (op)) - 1))
7733 return false;
7734
7735 if (bitpos < 0 || bitpos + width > GET_MODE_BITSIZE (GET_MODE (op)))
7736 return false;
7737
7738 return true;
7739 }
7740
7741 /* Check if MASK and SHIFT are valid in mask-low-and-shift-left
7742 operation if MAXLEN is the maxium length of consecutive bits that
7743 can make up MASK. MODE is the mode of the operation. See
7744 mask_low_and_shift_len for the actual definition. */
7745
7746 bool
7747 mask_low_and_shift_p (machine_mode mode, rtx mask, rtx shift, int maxlen)
7748 {
7749 return IN_RANGE (mask_low_and_shift_len (mode, mask, shift), 1, maxlen);
7750 }
7751
7752 /* Return true iff OP1 and OP2 are valid operands together for the
7753 *and<MODE>3 and *and<MODE>3_mips16 patterns. For the cases to consider,
7754 see the table in the comment before the pattern. */
7755
7756 bool
7757 and_operands_ok (machine_mode mode, rtx op1, rtx op2)
7758 {
7759 return (memory_operand (op1, mode)
7760 ? and_load_operand (op2, mode)
7761 : and_reg_operand (op2, mode));
7762 }
7763
7764 /* The canonical form of a mask-low-and-shift-left operation is
7765 (and (ashift X SHIFT) MASK) where MASK has the lower SHIFT number of bits
7766 cleared. Thus we need to shift MASK to the right before checking if it
7767 is a valid mask value. MODE is the mode of the operation. If true
7768 return the length of the mask, otherwise return -1. */
7769
7770 int
7771 mask_low_and_shift_len (machine_mode mode, rtx mask, rtx shift)
7772 {
7773 HOST_WIDE_INT shval;
7774
7775 shval = INTVAL (shift) & (GET_MODE_BITSIZE (mode) - 1);
7776 return exact_log2 ((UINTVAL (mask) >> shval) + 1);
7777 }
7778 \f
7779 /* Return true if -msplit-addresses is selected and should be honored.
7780
7781 -msplit-addresses is a half-way house between explicit relocations
7782 and the traditional assembler macros. It can split absolute 32-bit
7783 symbolic constants into a high/lo_sum pair but uses macros for other
7784 sorts of access.
7785
7786 Like explicit relocation support for REL targets, it relies
7787 on GNU extensions in the assembler and the linker.
7788
7789 Although this code should work for -O0, it has traditionally
7790 been treated as an optimization. */
7791
7792 static bool
7793 mips_split_addresses_p (void)
7794 {
7795 return (TARGET_SPLIT_ADDRESSES
7796 && optimize
7797 && !TARGET_MIPS16
7798 && !flag_pic
7799 && !ABI_HAS_64BIT_SYMBOLS);
7800 }
7801
7802 /* (Re-)Initialize mips_split_p, mips_lo_relocs and mips_hi_relocs. */
7803
7804 static void
7805 mips_init_relocs (void)
7806 {
7807 memset (mips_split_p, '\0', sizeof (mips_split_p));
7808 memset (mips_split_hi_p, '\0', sizeof (mips_split_hi_p));
7809 memset (mips_use_pcrel_pool_p, '\0', sizeof (mips_use_pcrel_pool_p));
7810 memset (mips_hi_relocs, '\0', sizeof (mips_hi_relocs));
7811 memset (mips_lo_relocs, '\0', sizeof (mips_lo_relocs));
7812
7813 if (TARGET_MIPS16_PCREL_LOADS)
7814 mips_use_pcrel_pool_p[SYMBOL_ABSOLUTE] = true;
7815 else
7816 {
7817 if (ABI_HAS_64BIT_SYMBOLS)
7818 {
7819 if (TARGET_EXPLICIT_RELOCS)
7820 {
7821 mips_split_p[SYMBOL_64_HIGH] = true;
7822 mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
7823 mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
7824
7825 mips_split_p[SYMBOL_64_MID] = true;
7826 mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
7827 mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
7828
7829 mips_split_p[SYMBOL_64_LOW] = true;
7830 mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
7831 mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
7832
7833 mips_split_p[SYMBOL_ABSOLUTE] = true;
7834 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7835 }
7836 }
7837 else
7838 {
7839 if (TARGET_EXPLICIT_RELOCS
7840 || mips_split_addresses_p ()
7841 || TARGET_MIPS16)
7842 {
7843 mips_split_p[SYMBOL_ABSOLUTE] = true;
7844 mips_hi_relocs[SYMBOL_ABSOLUTE] = "%hi(";
7845 mips_lo_relocs[SYMBOL_ABSOLUTE] = "%lo(";
7846 }
7847 }
7848 }
7849
7850 if (TARGET_MIPS16)
7851 {
7852 /* The high part is provided by a pseudo copy of $gp. */
7853 mips_split_p[SYMBOL_GP_RELATIVE] = true;
7854 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gprel(";
7855 }
7856 else if (TARGET_EXPLICIT_RELOCS)
7857 /* Small data constants are kept whole until after reload,
7858 then lowered by mips_rewrite_small_data. */
7859 mips_lo_relocs[SYMBOL_GP_RELATIVE] = "%gp_rel(";
7860
7861 if (TARGET_EXPLICIT_RELOCS)
7862 {
7863 mips_split_p[SYMBOL_GOT_PAGE_OFST] = true;
7864 if (TARGET_NEWABI)
7865 {
7866 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
7867 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%got_ofst(";
7868 }
7869 else
7870 {
7871 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
7872 mips_lo_relocs[SYMBOL_GOT_PAGE_OFST] = "%lo(";
7873 }
7874 if (TARGET_MIPS16)
7875 /* Expose the use of $28 as soon as possible. */
7876 mips_split_hi_p[SYMBOL_GOT_PAGE_OFST] = true;
7877
7878 if (TARGET_XGOT)
7879 {
7880 /* The HIGH and LO_SUM are matched by special .md patterns. */
7881 mips_split_p[SYMBOL_GOT_DISP] = true;
7882
7883 mips_split_p[SYMBOL_GOTOFF_DISP] = true;
7884 mips_hi_relocs[SYMBOL_GOTOFF_DISP] = "%got_hi(";
7885 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_lo(";
7886
7887 mips_split_p[SYMBOL_GOTOFF_CALL] = true;
7888 mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
7889 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
7890 }
7891 else
7892 {
7893 if (TARGET_NEWABI)
7894 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got_disp(";
7895 else
7896 mips_lo_relocs[SYMBOL_GOTOFF_DISP] = "%got(";
7897 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
7898 if (TARGET_MIPS16)
7899 /* Expose the use of $28 as soon as possible. */
7900 mips_split_p[SYMBOL_GOT_DISP] = true;
7901 }
7902 }
7903
7904 if (TARGET_NEWABI)
7905 {
7906 mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
7907 mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
7908 mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
7909 }
7910
7911 mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
7912 mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
7913
7914 if (TARGET_MIPS16_PCREL_LOADS)
7915 {
7916 mips_use_pcrel_pool_p[SYMBOL_DTPREL] = true;
7917 mips_use_pcrel_pool_p[SYMBOL_TPREL] = true;
7918 }
7919 else
7920 {
7921 mips_split_p[SYMBOL_DTPREL] = true;
7922 mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
7923 mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
7924
7925 mips_split_p[SYMBOL_TPREL] = true;
7926 mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
7927 mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
7928 }
7929
7930 mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
7931 mips_lo_relocs[SYMBOL_HALF] = "%half(";
7932 }
7933
7934 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM
7935 in context CONTEXT. RELOCS is the array of relocations to use. */
7936
7937 static void
7938 mips_print_operand_reloc (FILE *file, rtx op, enum mips_symbol_context context,
7939 const char **relocs)
7940 {
7941 enum mips_symbol_type symbol_type;
7942 const char *p;
7943
7944 symbol_type = mips_classify_symbolic_expression (op, context);
7945 gcc_assert (relocs[symbol_type]);
7946
7947 fputs (relocs[symbol_type], file);
7948 output_addr_const (file, mips_strip_unspec_address (op));
7949 for (p = relocs[symbol_type]; *p != 0; p++)
7950 if (*p == '(')
7951 fputc (')', file);
7952 }
7953
7954 /* Start a new block with the given asm switch enabled. If we need
7955 to print a directive, emit PREFIX before it and SUFFIX after it. */
7956
7957 static void
7958 mips_push_asm_switch_1 (struct mips_asm_switch *asm_switch,
7959 const char *prefix, const char *suffix)
7960 {
7961 if (asm_switch->nesting_level == 0)
7962 fprintf (asm_out_file, "%s.set\tno%s%s", prefix, asm_switch->name, suffix);
7963 asm_switch->nesting_level++;
7964 }
7965
7966 /* Likewise, but end a block. */
7967
7968 static void
7969 mips_pop_asm_switch_1 (struct mips_asm_switch *asm_switch,
7970 const char *prefix, const char *suffix)
7971 {
7972 gcc_assert (asm_switch->nesting_level);
7973 asm_switch->nesting_level--;
7974 if (asm_switch->nesting_level == 0)
7975 fprintf (asm_out_file, "%s.set\t%s%s", prefix, asm_switch->name, suffix);
7976 }
7977
7978 /* Wrappers around mips_push_asm_switch_1 and mips_pop_asm_switch_1
7979 that either print a complete line or print nothing. */
7980
7981 void
7982 mips_push_asm_switch (struct mips_asm_switch *asm_switch)
7983 {
7984 mips_push_asm_switch_1 (asm_switch, "\t", "\n");
7985 }
7986
7987 void
7988 mips_pop_asm_switch (struct mips_asm_switch *asm_switch)
7989 {
7990 mips_pop_asm_switch_1 (asm_switch, "\t", "\n");
7991 }
7992
7993 /* Print the text for PRINT_OPERAND punctation character CH to FILE.
7994 The punctuation characters are:
7995
7996 '(' Start a nested ".set noreorder" block.
7997 ')' End a nested ".set noreorder" block.
7998 '[' Start a nested ".set noat" block.
7999 ']' End a nested ".set noat" block.
8000 '<' Start a nested ".set nomacro" block.
8001 '>' End a nested ".set nomacro" block.
8002 '*' Behave like %(%< if generating a delayed-branch sequence.
8003 '#' Print a nop if in a ".set noreorder" block.
8004 '/' Like '#', but do nothing within a delayed-branch sequence.
8005 '?' Print "l" if mips_branch_likely is true
8006 '~' Print a nop if mips_branch_likely is true
8007 '.' Print the name of the register with a hard-wired zero (zero or $0).
8008 '@' Print the name of the assembler temporary register (at or $1).
8009 '^' Print the name of the pic call-through register (t9 or $25).
8010 '+' Print the name of the gp register (usually gp or $28).
8011 '$' Print the name of the stack pointer register (sp or $29).
8012 ':' Print "c" to use the compact version if the delay slot is a nop.
8013 '!' Print "s" to use the short version if the delay slot contains a
8014 16-bit instruction.
8015
8016 See also mips_init_print_operand_pucnt. */
8017
8018 static void
8019 mips_print_operand_punctuation (FILE *file, int ch)
8020 {
8021 switch (ch)
8022 {
8023 case '(':
8024 mips_push_asm_switch_1 (&mips_noreorder, "", "\n\t");
8025 break;
8026
8027 case ')':
8028 mips_pop_asm_switch_1 (&mips_noreorder, "\n\t", "");
8029 break;
8030
8031 case '[':
8032 mips_push_asm_switch_1 (&mips_noat, "", "\n\t");
8033 break;
8034
8035 case ']':
8036 mips_pop_asm_switch_1 (&mips_noat, "\n\t", "");
8037 break;
8038
8039 case '<':
8040 mips_push_asm_switch_1 (&mips_nomacro, "", "\n\t");
8041 break;
8042
8043 case '>':
8044 mips_pop_asm_switch_1 (&mips_nomacro, "\n\t", "");
8045 break;
8046
8047 case '*':
8048 if (final_sequence != 0)
8049 {
8050 mips_print_operand_punctuation (file, '(');
8051 mips_print_operand_punctuation (file, '<');
8052 }
8053 break;
8054
8055 case '#':
8056 if (mips_noreorder.nesting_level > 0)
8057 fputs ("\n\tnop", file);
8058 break;
8059
8060 case '/':
8061 /* Print an extra newline so that the delayed insn is separated
8062 from the following ones. This looks neater and is consistent
8063 with non-nop delayed sequences. */
8064 if (mips_noreorder.nesting_level > 0 && final_sequence == 0)
8065 fputs ("\n\tnop\n", file);
8066 break;
8067
8068 case '?':
8069 if (mips_branch_likely)
8070 putc ('l', file);
8071 break;
8072
8073 case '~':
8074 if (mips_branch_likely)
8075 fputs ("\n\tnop", file);
8076 break;
8077
8078 case '.':
8079 fputs (reg_names[GP_REG_FIRST + 0], file);
8080 break;
8081
8082 case '@':
8083 fputs (reg_names[AT_REGNUM], file);
8084 break;
8085
8086 case '^':
8087 fputs (reg_names[PIC_FUNCTION_ADDR_REGNUM], file);
8088 break;
8089
8090 case '+':
8091 fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
8092 break;
8093
8094 case '$':
8095 fputs (reg_names[STACK_POINTER_REGNUM], file);
8096 break;
8097
8098 case ':':
8099 /* When final_sequence is 0, the delay slot will be a nop. We can
8100 use the compact version for microMIPS. */
8101 if (final_sequence == 0)
8102 putc ('c', file);
8103 break;
8104
8105 case '!':
8106 /* If the delay slot instruction is short, then use the
8107 compact version. */
8108 if (final_sequence == 0
8109 || get_attr_length (final_sequence->insn (1)) == 2)
8110 putc ('s', file);
8111 break;
8112
8113 default:
8114 gcc_unreachable ();
8115 break;
8116 }
8117 }
8118
8119 /* Initialize mips_print_operand_punct. */
8120
8121 static void
8122 mips_init_print_operand_punct (void)
8123 {
8124 const char *p;
8125
8126 for (p = "()[]<>*#/?~.@^+$:!"; *p; p++)
8127 mips_print_operand_punct[(unsigned char) *p] = true;
8128 }
8129
8130 /* PRINT_OPERAND prefix LETTER refers to the integer branch instruction
8131 associated with condition CODE. Print the condition part of the
8132 opcode to FILE. */
8133
8134 static void
8135 mips_print_int_branch_condition (FILE *file, enum rtx_code code, int letter)
8136 {
8137 switch (code)
8138 {
8139 case EQ:
8140 case NE:
8141 case GT:
8142 case GE:
8143 case LT:
8144 case LE:
8145 case GTU:
8146 case GEU:
8147 case LTU:
8148 case LEU:
8149 /* Conveniently, the MIPS names for these conditions are the same
8150 as their RTL equivalents. */
8151 fputs (GET_RTX_NAME (code), file);
8152 break;
8153
8154 default:
8155 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8156 break;
8157 }
8158 }
8159
8160 /* Likewise floating-point branches. */
8161
8162 static void
8163 mips_print_float_branch_condition (FILE *file, enum rtx_code code, int letter)
8164 {
8165 switch (code)
8166 {
8167 case EQ:
8168 fputs ("c1f", file);
8169 break;
8170
8171 case NE:
8172 fputs ("c1t", file);
8173 break;
8174
8175 default:
8176 output_operand_lossage ("'%%%c' is not a valid operand prefix", letter);
8177 break;
8178 }
8179 }
8180
8181 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
8182
8183 static bool
8184 mips_print_operand_punct_valid_p (unsigned char code)
8185 {
8186 return mips_print_operand_punct[code];
8187 }
8188
8189 /* Implement TARGET_PRINT_OPERAND. The MIPS-specific operand codes are:
8190
8191 'X' Print CONST_INT OP in hexadecimal format.
8192 'x' Print the low 16 bits of CONST_INT OP in hexadecimal format.
8193 'd' Print CONST_INT OP in decimal.
8194 'm' Print one less than CONST_INT OP in decimal.
8195 'h' Print the high-part relocation associated with OP, after stripping
8196 any outermost HIGH.
8197 'R' Print the low-part relocation associated with OP.
8198 'C' Print the integer branch condition for comparison OP.
8199 'N' Print the inverse of the integer branch condition for comparison OP.
8200 'F' Print the FPU branch condition for comparison OP.
8201 'W' Print the inverse of the FPU branch condition for comparison OP.
8202 'T' Print 'f' for (eq:CC ...), 't' for (ne:CC ...),
8203 'z' for (eq:?I ...), 'n' for (ne:?I ...).
8204 't' Like 'T', but with the EQ/NE cases reversed
8205 'Y' Print mips_fp_conditions[INTVAL (OP)]
8206 'Z' Print OP and a comma for ISA_HAS_8CC, otherwise print nothing.
8207 'q' Print a DSP accumulator register.
8208 'D' Print the second part of a double-word register or memory operand.
8209 'L' Print the low-order register in a double-word register operand.
8210 'M' Print high-order register in a double-word register operand.
8211 'z' Print $0 if OP is zero, otherwise print OP normally.
8212 'b' Print the address of a memory operand, without offset. */
8213
8214 static void
8215 mips_print_operand (FILE *file, rtx op, int letter)
8216 {
8217 enum rtx_code code;
8218
8219 if (mips_print_operand_punct_valid_p (letter))
8220 {
8221 mips_print_operand_punctuation (file, letter);
8222 return;
8223 }
8224
8225 gcc_assert (op);
8226 code = GET_CODE (op);
8227
8228 switch (letter)
8229 {
8230 case 'X':
8231 if (CONST_INT_P (op))
8232 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
8233 else
8234 output_operand_lossage ("invalid use of '%%%c'", letter);
8235 break;
8236
8237 case 'x':
8238 if (CONST_INT_P (op))
8239 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op) & 0xffff);
8240 else
8241 output_operand_lossage ("invalid use of '%%%c'", letter);
8242 break;
8243
8244 case 'd':
8245 if (CONST_INT_P (op))
8246 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op));
8247 else
8248 output_operand_lossage ("invalid use of '%%%c'", letter);
8249 break;
8250
8251 case 'm':
8252 if (CONST_INT_P (op))
8253 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (op) - 1);
8254 else
8255 output_operand_lossage ("invalid use of '%%%c'", letter);
8256 break;
8257
8258 case 'h':
8259 if (code == HIGH)
8260 op = XEXP (op, 0);
8261 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_hi_relocs);
8262 break;
8263
8264 case 'R':
8265 mips_print_operand_reloc (file, op, SYMBOL_CONTEXT_LEA, mips_lo_relocs);
8266 break;
8267
8268 case 'C':
8269 mips_print_int_branch_condition (file, code, letter);
8270 break;
8271
8272 case 'N':
8273 mips_print_int_branch_condition (file, reverse_condition (code), letter);
8274 break;
8275
8276 case 'F':
8277 mips_print_float_branch_condition (file, code, letter);
8278 break;
8279
8280 case 'W':
8281 mips_print_float_branch_condition (file, reverse_condition (code),
8282 letter);
8283 break;
8284
8285 case 'T':
8286 case 't':
8287 {
8288 int truth = (code == NE) == (letter == 'T');
8289 fputc ("zfnt"[truth * 2 + ST_REG_P (REGNO (XEXP (op, 0)))], file);
8290 }
8291 break;
8292
8293 case 'Y':
8294 if (code == CONST_INT && UINTVAL (op) < ARRAY_SIZE (mips_fp_conditions))
8295 fputs (mips_fp_conditions[UINTVAL (op)], file);
8296 else
8297 output_operand_lossage ("'%%%c' is not a valid operand prefix",
8298 letter);
8299 break;
8300
8301 case 'Z':
8302 if (ISA_HAS_8CC)
8303 {
8304 mips_print_operand (file, op, 0);
8305 fputc (',', file);
8306 }
8307 break;
8308
8309 case 'q':
8310 if (code == REG && MD_REG_P (REGNO (op)))
8311 fprintf (file, "$ac0");
8312 else if (code == REG && DSP_ACC_REG_P (REGNO (op)))
8313 fprintf (file, "$ac%c", reg_names[REGNO (op)][3]);
8314 else
8315 output_operand_lossage ("invalid use of '%%%c'", letter);
8316 break;
8317
8318 default:
8319 switch (code)
8320 {
8321 case REG:
8322 {
8323 unsigned int regno = REGNO (op);
8324 if ((letter == 'M' && TARGET_LITTLE_ENDIAN)
8325 || (letter == 'L' && TARGET_BIG_ENDIAN)
8326 || letter == 'D')
8327 regno++;
8328 else if (letter && letter != 'z' && letter != 'M' && letter != 'L')
8329 output_operand_lossage ("invalid use of '%%%c'", letter);
8330 /* We need to print $0 .. $31 for COP0 registers. */
8331 if (COP0_REG_P (regno))
8332 fprintf (file, "$%s", &reg_names[regno][4]);
8333 else
8334 fprintf (file, "%s", reg_names[regno]);
8335 }
8336 break;
8337
8338 case MEM:
8339 if (letter == 'D')
8340 output_address (plus_constant (Pmode, XEXP (op, 0), 4));
8341 else if (letter == 'b')
8342 {
8343 gcc_assert (REG_P (XEXP (op, 0)));
8344 mips_print_operand (file, XEXP (op, 0), 0);
8345 }
8346 else if (letter && letter != 'z')
8347 output_operand_lossage ("invalid use of '%%%c'", letter);
8348 else
8349 output_address (XEXP (op, 0));
8350 break;
8351
8352 default:
8353 if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
8354 fputs (reg_names[GP_REG_FIRST], file);
8355 else if (letter && letter != 'z')
8356 output_operand_lossage ("invalid use of '%%%c'", letter);
8357 else if (CONST_GP_P (op))
8358 fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
8359 else
8360 output_addr_const (file, mips_strip_unspec_address (op));
8361 break;
8362 }
8363 }
8364 }
8365
8366 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
8367
8368 static void
8369 mips_print_operand_address (FILE *file, rtx x)
8370 {
8371 struct mips_address_info addr;
8372
8373 if (mips_classify_address (&addr, x, word_mode, true))
8374 switch (addr.type)
8375 {
8376 case ADDRESS_REG:
8377 mips_print_operand (file, addr.offset, 0);
8378 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8379 return;
8380
8381 case ADDRESS_LO_SUM:
8382 mips_print_operand_reloc (file, addr.offset, SYMBOL_CONTEXT_MEM,
8383 mips_lo_relocs);
8384 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
8385 return;
8386
8387 case ADDRESS_CONST_INT:
8388 output_addr_const (file, x);
8389 fprintf (file, "(%s)", reg_names[GP_REG_FIRST]);
8390 return;
8391
8392 case ADDRESS_SYMBOLIC:
8393 output_addr_const (file, mips_strip_unspec_address (x));
8394 return;
8395 }
8396 gcc_unreachable ();
8397 }
8398 \f
8399 /* Implement TARGET_ENCODE_SECTION_INFO. */
8400
8401 static void
8402 mips_encode_section_info (tree decl, rtx rtl, int first)
8403 {
8404 default_encode_section_info (decl, rtl, first);
8405
8406 if (TREE_CODE (decl) == FUNCTION_DECL)
8407 {
8408 rtx symbol = XEXP (rtl, 0);
8409 tree type = TREE_TYPE (decl);
8410
8411 /* Encode whether the symbol is short or long. */
8412 if ((TARGET_LONG_CALLS && !mips_near_type_p (type))
8413 || mips_far_type_p (type))
8414 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
8415 }
8416 }
8417
8418 /* Implement TARGET_SELECT_RTX_SECTION. */
8419
8420 static section *
8421 mips_select_rtx_section (machine_mode mode, rtx x,
8422 unsigned HOST_WIDE_INT align)
8423 {
8424 /* ??? Consider using mergeable small data sections. */
8425 if (mips_rtx_constant_in_small_data_p (mode))
8426 return get_named_section (NULL, ".sdata", 0);
8427
8428 return default_elf_select_rtx_section (mode, x, align);
8429 }
8430
8431 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
8432
8433 The complication here is that, with the combination TARGET_ABICALLS
8434 && !TARGET_ABSOLUTE_ABICALLS && !TARGET_GPWORD, jump tables will use
8435 absolute addresses, and should therefore not be included in the
8436 read-only part of a DSO. Handle such cases by selecting a normal
8437 data section instead of a read-only one. The logic apes that in
8438 default_function_rodata_section. */
8439
8440 static section *
8441 mips_function_rodata_section (tree decl)
8442 {
8443 if (!TARGET_ABICALLS || TARGET_ABSOLUTE_ABICALLS || TARGET_GPWORD)
8444 return default_function_rodata_section (decl);
8445
8446 if (decl && DECL_SECTION_NAME (decl))
8447 {
8448 const char *name = DECL_SECTION_NAME (decl);
8449 if (DECL_COMDAT_GROUP (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
8450 {
8451 char *rname = ASTRDUP (name);
8452 rname[14] = 'd';
8453 return get_section (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
8454 }
8455 else if (flag_function_sections
8456 && flag_data_sections
8457 && strncmp (name, ".text.", 6) == 0)
8458 {
8459 char *rname = ASTRDUP (name);
8460 memcpy (rname + 1, "data", 4);
8461 return get_section (rname, SECTION_WRITE, decl);
8462 }
8463 }
8464 return data_section;
8465 }
8466
8467 /* Implement TARGET_IN_SMALL_DATA_P. */
8468
8469 static bool
8470 mips_in_small_data_p (const_tree decl)
8471 {
8472 unsigned HOST_WIDE_INT size;
8473
8474 if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
8475 return false;
8476
8477 /* We don't yet generate small-data references for -mabicalls
8478 or VxWorks RTP code. See the related -G handling in
8479 mips_option_override. */
8480 if (TARGET_ABICALLS || TARGET_VXWORKS_RTP)
8481 return false;
8482
8483 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
8484 {
8485 const char *name;
8486
8487 /* Reject anything that isn't in a known small-data section. */
8488 name = DECL_SECTION_NAME (decl);
8489 if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
8490 return false;
8491
8492 /* If a symbol is defined externally, the assembler will use the
8493 usual -G rules when deciding how to implement macros. */
8494 if (mips_lo_relocs[SYMBOL_GP_RELATIVE] || !DECL_EXTERNAL (decl))
8495 return true;
8496 }
8497 else if (TARGET_EMBEDDED_DATA)
8498 {
8499 /* Don't put constants into the small data section: we want them
8500 to be in ROM rather than RAM. */
8501 if (TREE_CODE (decl) != VAR_DECL)
8502 return false;
8503
8504 if (TREE_READONLY (decl)
8505 && !TREE_SIDE_EFFECTS (decl)
8506 && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
8507 return false;
8508 }
8509
8510 /* Enforce -mlocal-sdata. */
8511 if (!TARGET_LOCAL_SDATA && !TREE_PUBLIC (decl))
8512 return false;
8513
8514 /* Enforce -mextern-sdata. */
8515 if (!TARGET_EXTERN_SDATA && DECL_P (decl))
8516 {
8517 if (DECL_EXTERNAL (decl))
8518 return false;
8519 if (DECL_COMMON (decl) && DECL_INITIAL (decl) == NULL)
8520 return false;
8521 }
8522
8523 /* We have traditionally not treated zero-sized objects as small data,
8524 so this is now effectively part of the ABI. */
8525 size = int_size_in_bytes (TREE_TYPE (decl));
8526 return size > 0 && size <= mips_small_data_threshold;
8527 }
8528
8529 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P. We don't want to use
8530 anchors for small data: the GP register acts as an anchor in that
8531 case. We also don't want to use them for PC-relative accesses,
8532 where the PC acts as an anchor. */
8533
8534 static bool
8535 mips_use_anchors_for_symbol_p (const_rtx symbol)
8536 {
8537 switch (mips_classify_symbol (symbol, SYMBOL_CONTEXT_MEM))
8538 {
8539 case SYMBOL_PC_RELATIVE:
8540 case SYMBOL_GP_RELATIVE:
8541 return false;
8542
8543 default:
8544 return default_use_anchors_for_symbol_p (symbol);
8545 }
8546 }
8547 \f
8548 /* The MIPS debug format wants all automatic variables and arguments
8549 to be in terms of the virtual frame pointer (stack pointer before
8550 any adjustment in the function), while the MIPS 3.0 linker wants
8551 the frame pointer to be the stack pointer after the initial
8552 adjustment. So, we do the adjustment here. The arg pointer (which
8553 is eliminated) points to the virtual frame pointer, while the frame
8554 pointer (which may be eliminated) points to the stack pointer after
8555 the initial adjustments. */
8556
8557 HOST_WIDE_INT
8558 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
8559 {
8560 rtx offset2 = const0_rtx;
8561 rtx reg = eliminate_constant_term (addr, &offset2);
8562
8563 if (offset == 0)
8564 offset = INTVAL (offset2);
8565
8566 if (reg == stack_pointer_rtx
8567 || reg == frame_pointer_rtx
8568 || reg == hard_frame_pointer_rtx)
8569 {
8570 offset -= cfun->machine->frame.total_size;
8571 if (reg == hard_frame_pointer_rtx)
8572 offset += cfun->machine->frame.hard_frame_pointer_offset;
8573 }
8574
8575 return offset;
8576 }
8577 \f
8578 /* Implement ASM_OUTPUT_EXTERNAL. */
8579
8580 void
8581 mips_output_external (FILE *file, tree decl, const char *name)
8582 {
8583 default_elf_asm_output_external (file, decl, name);
8584
8585 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8586 set in order to avoid putting out names that are never really
8587 used. */
8588 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
8589 {
8590 if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
8591 {
8592 /* When using assembler macros, emit .extern directives for
8593 all small-data externs so that the assembler knows how
8594 big they are.
8595
8596 In most cases it would be safe (though pointless) to emit
8597 .externs for other symbols too. One exception is when an
8598 object is within the -G limit but declared by the user to
8599 be in a section other than .sbss or .sdata. */
8600 fputs ("\t.extern\t", file);
8601 assemble_name (file, name);
8602 fprintf (file, ", " HOST_WIDE_INT_PRINT_DEC "\n",
8603 int_size_in_bytes (TREE_TYPE (decl)));
8604 }
8605 }
8606 }
8607
8608 /* Implement TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8609
8610 static void
8611 mips_output_filename (FILE *stream, const char *name)
8612 {
8613 /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
8614 directives. */
8615 if (write_symbols == DWARF2_DEBUG)
8616 return;
8617 else if (mips_output_filename_first_time)
8618 {
8619 mips_output_filename_first_time = 0;
8620 num_source_filenames += 1;
8621 current_function_file = name;
8622 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8623 output_quoted_string (stream, name);
8624 putc ('\n', stream);
8625 }
8626 /* If we are emitting stabs, let dbxout.c handle this (except for
8627 the mips_output_filename_first_time case). */
8628 else if (write_symbols == DBX_DEBUG)
8629 return;
8630 else if (name != current_function_file
8631 && strcmp (name, current_function_file) != 0)
8632 {
8633 num_source_filenames += 1;
8634 current_function_file = name;
8635 fprintf (stream, "\t.file\t%d ", num_source_filenames);
8636 output_quoted_string (stream, name);
8637 putc ('\n', stream);
8638 }
8639 }
8640
8641 /* Implement TARGET_ASM_OUTPUT_DWARF_DTPREL. */
8642
8643 static void ATTRIBUTE_UNUSED
8644 mips_output_dwarf_dtprel (FILE *file, int size, rtx x)
8645 {
8646 switch (size)
8647 {
8648 case 4:
8649 fputs ("\t.dtprelword\t", file);
8650 break;
8651
8652 case 8:
8653 fputs ("\t.dtpreldword\t", file);
8654 break;
8655
8656 default:
8657 gcc_unreachable ();
8658 }
8659 output_addr_const (file, x);
8660 fputs ("+0x8000", file);
8661 }
8662
8663 /* Implement TARGET_DWARF_REGISTER_SPAN. */
8664
8665 static rtx
8666 mips_dwarf_register_span (rtx reg)
8667 {
8668 rtx high, low;
8669 machine_mode mode;
8670
8671 /* By default, GCC maps increasing register numbers to increasing
8672 memory locations, but paired FPRs are always little-endian,
8673 regardless of the prevailing endianness. */
8674 mode = GET_MODE (reg);
8675 if (FP_REG_P (REGNO (reg))
8676 && TARGET_BIG_ENDIAN
8677 && MAX_FPRS_PER_FMT > 1
8678 && GET_MODE_SIZE (mode) > UNITS_PER_FPREG)
8679 {
8680 gcc_assert (GET_MODE_SIZE (mode) == UNITS_PER_HWFPVALUE);
8681 high = mips_subword (reg, true);
8682 low = mips_subword (reg, false);
8683 return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, high, low));
8684 }
8685
8686 return NULL_RTX;
8687 }
8688
8689 /* DSP ALU can bypass data with no delays for the following pairs. */
8690 enum insn_code dspalu_bypass_table[][2] =
8691 {
8692 {CODE_FOR_mips_addsc, CODE_FOR_mips_addwc},
8693 {CODE_FOR_mips_cmpu_eq_qb, CODE_FOR_mips_pick_qb},
8694 {CODE_FOR_mips_cmpu_lt_qb, CODE_FOR_mips_pick_qb},
8695 {CODE_FOR_mips_cmpu_le_qb, CODE_FOR_mips_pick_qb},
8696 {CODE_FOR_mips_cmp_eq_ph, CODE_FOR_mips_pick_ph},
8697 {CODE_FOR_mips_cmp_lt_ph, CODE_FOR_mips_pick_ph},
8698 {CODE_FOR_mips_cmp_le_ph, CODE_FOR_mips_pick_ph},
8699 {CODE_FOR_mips_wrdsp, CODE_FOR_mips_insv}
8700 };
8701
8702 int
8703 mips_dspalu_bypass_p (rtx out_insn, rtx in_insn)
8704 {
8705 int i;
8706 int num_bypass = ARRAY_SIZE (dspalu_bypass_table);
8707 enum insn_code out_icode = (enum insn_code) INSN_CODE (out_insn);
8708 enum insn_code in_icode = (enum insn_code) INSN_CODE (in_insn);
8709
8710 for (i = 0; i < num_bypass; i++)
8711 {
8712 if (out_icode == dspalu_bypass_table[i][0]
8713 && in_icode == dspalu_bypass_table[i][1])
8714 return true;
8715 }
8716
8717 return false;
8718 }
8719 /* Implement ASM_OUTPUT_ASCII. */
8720
8721 void
8722 mips_output_ascii (FILE *stream, const char *string, size_t len)
8723 {
8724 size_t i;
8725 int cur_pos;
8726
8727 cur_pos = 17;
8728 fprintf (stream, "\t.ascii\t\"");
8729 for (i = 0; i < len; i++)
8730 {
8731 int c;
8732
8733 c = (unsigned char) string[i];
8734 if (ISPRINT (c))
8735 {
8736 if (c == '\\' || c == '\"')
8737 {
8738 putc ('\\', stream);
8739 cur_pos++;
8740 }
8741 putc (c, stream);
8742 cur_pos++;
8743 }
8744 else
8745 {
8746 fprintf (stream, "\\%03o", c);
8747 cur_pos += 4;
8748 }
8749
8750 if (cur_pos > 72 && i+1 < len)
8751 {
8752 cur_pos = 17;
8753 fprintf (stream, "\"\n\t.ascii\t\"");
8754 }
8755 }
8756 fprintf (stream, "\"\n");
8757 }
8758
8759 /* Return the pseudo-op for full SYMBOL_(D)TPREL address *ADDR.
8760 Update *ADDR with the operand that should be printed. */
8761
8762 const char *
8763 mips_output_tls_reloc_directive (rtx *addr)
8764 {
8765 enum mips_symbol_type type;
8766
8767 type = mips_classify_symbolic_expression (*addr, SYMBOL_CONTEXT_LEA);
8768 *addr = mips_strip_unspec_address (*addr);
8769 switch (type)
8770 {
8771 case SYMBOL_DTPREL:
8772 return Pmode == SImode ? ".dtprelword\t%0" : ".dtpreldword\t%0";
8773
8774 case SYMBOL_TPREL:
8775 return Pmode == SImode ? ".tprelword\t%0" : ".tpreldword\t%0";
8776
8777 default:
8778 gcc_unreachable ();
8779 }
8780 }
8781
8782 /* Emit either a label, .comm, or .lcomm directive. When using assembler
8783 macros, mark the symbol as written so that mips_asm_output_external
8784 won't emit an .extern for it. STREAM is the output file, NAME is the
8785 name of the symbol, INIT_STRING is the string that should be written
8786 before the symbol and FINAL_STRING is the string that should be
8787 written after it. FINAL_STRING is a printf format that consumes the
8788 remaining arguments. */
8789
8790 void
8791 mips_declare_object (FILE *stream, const char *name, const char *init_string,
8792 const char *final_string, ...)
8793 {
8794 va_list ap;
8795
8796 fputs (init_string, stream);
8797 assemble_name (stream, name);
8798 va_start (ap, final_string);
8799 vfprintf (stream, final_string, ap);
8800 va_end (ap);
8801
8802 if (!TARGET_EXPLICIT_RELOCS)
8803 {
8804 tree name_tree = get_identifier (name);
8805 TREE_ASM_WRITTEN (name_tree) = 1;
8806 }
8807 }
8808
8809 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
8810 NAME is the name of the object and ALIGN is the required alignment
8811 in bytes. TAKES_ALIGNMENT_P is true if the directive takes a third
8812 alignment argument. */
8813
8814 void
8815 mips_declare_common_object (FILE *stream, const char *name,
8816 const char *init_string,
8817 unsigned HOST_WIDE_INT size,
8818 unsigned int align, bool takes_alignment_p)
8819 {
8820 if (!takes_alignment_p)
8821 {
8822 size += (align / BITS_PER_UNIT) - 1;
8823 size -= size % (align / BITS_PER_UNIT);
8824 mips_declare_object (stream, name, init_string,
8825 "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
8826 }
8827 else
8828 mips_declare_object (stream, name, init_string,
8829 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
8830 size, align / BITS_PER_UNIT);
8831 }
8832
8833 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON. This is usually the same as the
8834 elfos.h version, but we also need to handle -muninit-const-in-rodata. */
8835
8836 void
8837 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
8838 unsigned HOST_WIDE_INT size,
8839 unsigned int align)
8840 {
8841 /* If the target wants uninitialized const declarations in
8842 .rdata then don't put them in .comm. */
8843 if (TARGET_EMBEDDED_DATA
8844 && TARGET_UNINIT_CONST_IN_RODATA
8845 && TREE_CODE (decl) == VAR_DECL
8846 && TREE_READONLY (decl)
8847 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
8848 {
8849 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
8850 targetm.asm_out.globalize_label (stream, name);
8851
8852 switch_to_section (readonly_data_section);
8853 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
8854 mips_declare_object (stream, name, "",
8855 ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
8856 size);
8857 }
8858 else
8859 mips_declare_common_object (stream, name, "\n\t.comm\t",
8860 size, align, true);
8861 }
8862
8863 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
8864 extern int size_directive_output;
8865
8866 /* Implement ASM_DECLARE_OBJECT_NAME. This is like most of the standard ELF
8867 definitions except that it uses mips_declare_object to emit the label. */
8868
8869 void
8870 mips_declare_object_name (FILE *stream, const char *name,
8871 tree decl ATTRIBUTE_UNUSED)
8872 {
8873 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
8874 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
8875 #endif
8876
8877 size_directive_output = 0;
8878 if (!flag_inhibit_size_directive && DECL_SIZE (decl))
8879 {
8880 HOST_WIDE_INT size;
8881
8882 size_directive_output = 1;
8883 size = int_size_in_bytes (TREE_TYPE (decl));
8884 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8885 }
8886
8887 mips_declare_object (stream, name, "", ":\n");
8888 }
8889
8890 /* Implement ASM_FINISH_DECLARE_OBJECT. This is generic ELF stuff. */
8891
8892 void
8893 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
8894 {
8895 const char *name;
8896
8897 name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8898 if (!flag_inhibit_size_directive
8899 && DECL_SIZE (decl) != 0
8900 && !at_end
8901 && top_level
8902 && DECL_INITIAL (decl) == error_mark_node
8903 && !size_directive_output)
8904 {
8905 HOST_WIDE_INT size;
8906
8907 size_directive_output = 1;
8908 size = int_size_in_bytes (TREE_TYPE (decl));
8909 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
8910 }
8911 }
8912 #endif
8913 \f
8914 /* Return the FOO in the name of the ".mdebug.FOO" section associated
8915 with the current ABI. */
8916
8917 static const char *
8918 mips_mdebug_abi_name (void)
8919 {
8920 switch (mips_abi)
8921 {
8922 case ABI_32:
8923 return "abi32";
8924 case ABI_O64:
8925 return "abiO64";
8926 case ABI_N32:
8927 return "abiN32";
8928 case ABI_64:
8929 return "abi64";
8930 case ABI_EABI:
8931 return TARGET_64BIT ? "eabi64" : "eabi32";
8932 default:
8933 gcc_unreachable ();
8934 }
8935 }
8936
8937 /* Implement TARGET_ASM_FILE_START. */
8938
8939 static void
8940 mips_file_start (void)
8941 {
8942 default_file_start ();
8943
8944 /* Generate a special section to describe the ABI switches used to
8945 produce the resultant binary. */
8946
8947 /* Record the ABI itself. Modern versions of binutils encode
8948 this information in the ELF header flags, but GDB needs the
8949 information in order to correctly debug binaries produced by
8950 older binutils. See the function mips_gdbarch_init in
8951 gdb/mips-tdep.c. */
8952 fprintf (asm_out_file, "\t.section .mdebug.%s\n\t.previous\n",
8953 mips_mdebug_abi_name ());
8954
8955 /* There is no ELF header flag to distinguish long32 forms of the
8956 EABI from long64 forms. Emit a special section to help tools
8957 such as GDB. Do the same for o64, which is sometimes used with
8958 -mlong64. */
8959 if (mips_abi == ABI_EABI || mips_abi == ABI_O64)
8960 fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n"
8961 "\t.previous\n", TARGET_LONG64 ? 64 : 32);
8962
8963 /* Record the NaN encoding. */
8964 if (HAVE_AS_NAN || mips_nan != MIPS_IEEE_754_DEFAULT)
8965 fprintf (asm_out_file, "\t.nan\t%s\n",
8966 mips_nan == MIPS_IEEE_754_2008 ? "2008" : "legacy");
8967
8968 #ifdef HAVE_AS_GNU_ATTRIBUTE
8969 {
8970 int attr;
8971
8972 /* No floating-point operations, -mno-float. */
8973 if (TARGET_NO_FLOAT)
8974 attr = 0;
8975 /* Soft-float code, -msoft-float. */
8976 else if (!TARGET_HARD_FLOAT_ABI)
8977 attr = 3;
8978 /* Single-float code, -msingle-float. */
8979 else if (!TARGET_DOUBLE_FLOAT)
8980 attr = 2;
8981 /* 64-bit FP registers on a 32-bit target, -mips32r2 -mfp64. */
8982 else if (!TARGET_64BIT && TARGET_FLOAT64)
8983 attr = 4;
8984 /* Regular FP code, FP regs same size as GP regs, -mdouble-float. */
8985 else
8986 attr = 1;
8987
8988 fprintf (asm_out_file, "\t.gnu_attribute 4, %d\n", attr);
8989 }
8990 #endif
8991
8992 /* If TARGET_ABICALLS, tell GAS to generate -KPIC code. */
8993 if (TARGET_ABICALLS)
8994 {
8995 fprintf (asm_out_file, "\t.abicalls\n");
8996 if (TARGET_ABICALLS_PIC0)
8997 fprintf (asm_out_file, "\t.option\tpic0\n");
8998 }
8999
9000 if (flag_verbose_asm)
9001 fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
9002 ASM_COMMENT_START,
9003 mips_small_data_threshold, mips_arch_info->name, mips_isa);
9004 }
9005
9006 /* Implement TARGET_ASM_CODE_END. */
9007
9008 static void
9009 mips_code_end (void)
9010 {
9011 mips_finish_stub (&mips16_rdhwr_stub);
9012 mips_finish_stub (&mips16_get_fcsr_stub);
9013 mips_finish_stub (&mips16_set_fcsr_stub);
9014 }
9015 \f
9016 /* Make the last instruction frame-related and note that it performs
9017 the operation described by FRAME_PATTERN. */
9018
9019 static void
9020 mips_set_frame_expr (rtx frame_pattern)
9021 {
9022 rtx_insn *insn;
9023
9024 insn = get_last_insn ();
9025 RTX_FRAME_RELATED_P (insn) = 1;
9026 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
9027 frame_pattern,
9028 REG_NOTES (insn));
9029 }
9030
9031 /* Return a frame-related rtx that stores REG at MEM.
9032 REG must be a single register. */
9033
9034 static rtx
9035 mips_frame_set (rtx mem, rtx reg)
9036 {
9037 rtx set;
9038
9039 set = gen_rtx_SET (VOIDmode, mem, reg);
9040 RTX_FRAME_RELATED_P (set) = 1;
9041
9042 return set;
9043 }
9044
9045 /* Record that the epilogue has restored call-saved register REG. */
9046
9047 static void
9048 mips_add_cfa_restore (rtx reg)
9049 {
9050 mips_epilogue.cfa_restores = alloc_reg_note (REG_CFA_RESTORE, reg,
9051 mips_epilogue.cfa_restores);
9052 }
9053 \f
9054 /* If a MIPS16e SAVE or RESTORE instruction saves or restores register
9055 mips16e_s2_s8_regs[X], it must also save the registers in indexes
9056 X + 1 onwards. Likewise mips16e_a0_a3_regs. */
9057 static const unsigned char mips16e_s2_s8_regs[] = {
9058 30, 23, 22, 21, 20, 19, 18
9059 };
9060 static const unsigned char mips16e_a0_a3_regs[] = {
9061 4, 5, 6, 7
9062 };
9063
9064 /* A list of the registers that can be saved by the MIPS16e SAVE instruction,
9065 ordered from the uppermost in memory to the lowest in memory. */
9066 static const unsigned char mips16e_save_restore_regs[] = {
9067 31, 30, 23, 22, 21, 20, 19, 18, 17, 16, 7, 6, 5, 4
9068 };
9069
9070 /* Return the index of the lowest X in the range [0, SIZE) for which
9071 bit REGS[X] is set in MASK. Return SIZE if there is no such X. */
9072
9073 static unsigned int
9074 mips16e_find_first_register (unsigned int mask, const unsigned char *regs,
9075 unsigned int size)
9076 {
9077 unsigned int i;
9078
9079 for (i = 0; i < size; i++)
9080 if (BITSET_P (mask, regs[i]))
9081 break;
9082
9083 return i;
9084 }
9085
9086 /* *MASK_PTR is a mask of general-purpose registers and *NUM_REGS_PTR
9087 is the number of set bits. If *MASK_PTR contains REGS[X] for some X
9088 in [0, SIZE), adjust *MASK_PTR and *NUM_REGS_PTR so that the same
9089 is true for all indexes (X, SIZE). */
9090
9091 static void
9092 mips16e_mask_registers (unsigned int *mask_ptr, const unsigned char *regs,
9093 unsigned int size, unsigned int *num_regs_ptr)
9094 {
9095 unsigned int i;
9096
9097 i = mips16e_find_first_register (*mask_ptr, regs, size);
9098 for (i++; i < size; i++)
9099 if (!BITSET_P (*mask_ptr, regs[i]))
9100 {
9101 *num_regs_ptr += 1;
9102 *mask_ptr |= 1 << regs[i];
9103 }
9104 }
9105
9106 /* Return a simplified form of X using the register values in REG_VALUES.
9107 REG_VALUES[R] is the last value assigned to hard register R, or null
9108 if R has not been modified.
9109
9110 This function is rather limited, but is good enough for our purposes. */
9111
9112 static rtx
9113 mips16e_collect_propagate_value (rtx x, rtx *reg_values)
9114 {
9115 x = avoid_constant_pool_reference (x);
9116
9117 if (UNARY_P (x))
9118 {
9119 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9120 return simplify_gen_unary (GET_CODE (x), GET_MODE (x),
9121 x0, GET_MODE (XEXP (x, 0)));
9122 }
9123
9124 if (ARITHMETIC_P (x))
9125 {
9126 rtx x0 = mips16e_collect_propagate_value (XEXP (x, 0), reg_values);
9127 rtx x1 = mips16e_collect_propagate_value (XEXP (x, 1), reg_values);
9128 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), x0, x1);
9129 }
9130
9131 if (REG_P (x)
9132 && reg_values[REGNO (x)]
9133 && !rtx_unstable_p (reg_values[REGNO (x)]))
9134 return reg_values[REGNO (x)];
9135
9136 return x;
9137 }
9138
9139 /* Return true if (set DEST SRC) stores an argument register into its
9140 caller-allocated save slot, storing the number of that argument
9141 register in *REGNO_PTR if so. REG_VALUES is as for
9142 mips16e_collect_propagate_value. */
9143
9144 static bool
9145 mips16e_collect_argument_save_p (rtx dest, rtx src, rtx *reg_values,
9146 unsigned int *regno_ptr)
9147 {
9148 unsigned int argno, regno;
9149 HOST_WIDE_INT offset, required_offset;
9150 rtx addr, base;
9151
9152 /* Check that this is a word-mode store. */
9153 if (!MEM_P (dest) || !REG_P (src) || GET_MODE (dest) != word_mode)
9154 return false;
9155
9156 /* Check that the register being saved is an unmodified argument
9157 register. */
9158 regno = REGNO (src);
9159 if (!IN_RANGE (regno, GP_ARG_FIRST, GP_ARG_LAST) || reg_values[regno])
9160 return false;
9161 argno = regno - GP_ARG_FIRST;
9162
9163 /* Check whether the address is an appropriate stack-pointer or
9164 frame-pointer access. */
9165 addr = mips16e_collect_propagate_value (XEXP (dest, 0), reg_values);
9166 mips_split_plus (addr, &base, &offset);
9167 required_offset = cfun->machine->frame.total_size + argno * UNITS_PER_WORD;
9168 if (base == hard_frame_pointer_rtx)
9169 required_offset -= cfun->machine->frame.hard_frame_pointer_offset;
9170 else if (base != stack_pointer_rtx)
9171 return false;
9172 if (offset != required_offset)
9173 return false;
9174
9175 *regno_ptr = regno;
9176 return true;
9177 }
9178
9179 /* A subroutine of mips_expand_prologue, called only when generating
9180 MIPS16e SAVE instructions. Search the start of the function for any
9181 instructions that save argument registers into their caller-allocated
9182 save slots. Delete such instructions and return a value N such that
9183 saving [GP_ARG_FIRST, GP_ARG_FIRST + N) would make all the deleted
9184 instructions redundant. */
9185
9186 static unsigned int
9187 mips16e_collect_argument_saves (void)
9188 {
9189 rtx reg_values[FIRST_PSEUDO_REGISTER];
9190 rtx_insn *insn, *next;
9191 rtx set, dest, src;
9192 unsigned int nargs, regno;
9193
9194 push_topmost_sequence ();
9195 nargs = 0;
9196 memset (reg_values, 0, sizeof (reg_values));
9197 for (insn = get_insns (); insn; insn = next)
9198 {
9199 next = NEXT_INSN (insn);
9200 if (NOTE_P (insn) || DEBUG_INSN_P (insn))
9201 continue;
9202
9203 if (!INSN_P (insn))
9204 break;
9205
9206 set = PATTERN (insn);
9207 if (GET_CODE (set) != SET)
9208 break;
9209
9210 dest = SET_DEST (set);
9211 src = SET_SRC (set);
9212 if (mips16e_collect_argument_save_p (dest, src, reg_values, &regno))
9213 {
9214 if (!BITSET_P (cfun->machine->frame.mask, regno))
9215 {
9216 delete_insn (insn);
9217 nargs = MAX (nargs, (regno - GP_ARG_FIRST) + 1);
9218 }
9219 }
9220 else if (REG_P (dest) && GET_MODE (dest) == word_mode)
9221 reg_values[REGNO (dest)]
9222 = mips16e_collect_propagate_value (src, reg_values);
9223 else
9224 break;
9225 }
9226 pop_topmost_sequence ();
9227
9228 return nargs;
9229 }
9230
9231 /* Return a move between register REGNO and memory location SP + OFFSET.
9232 REG_PARM_P is true if SP + OFFSET belongs to REG_PARM_STACK_SPACE.
9233 Make the move a load if RESTORE_P, otherwise make it a store. */
9234
9235 static rtx
9236 mips16e_save_restore_reg (bool restore_p, bool reg_parm_p,
9237 HOST_WIDE_INT offset, unsigned int regno)
9238 {
9239 rtx reg, mem;
9240
9241 mem = gen_frame_mem (SImode, plus_constant (Pmode, stack_pointer_rtx,
9242 offset));
9243 reg = gen_rtx_REG (SImode, regno);
9244 if (restore_p)
9245 {
9246 mips_add_cfa_restore (reg);
9247 return gen_rtx_SET (VOIDmode, reg, mem);
9248 }
9249 if (reg_parm_p)
9250 return gen_rtx_SET (VOIDmode, mem, reg);
9251 return mips_frame_set (mem, reg);
9252 }
9253
9254 /* Return RTL for a MIPS16e SAVE or RESTORE instruction; RESTORE_P says which.
9255 The instruction must:
9256
9257 - Allocate or deallocate SIZE bytes in total; SIZE is known
9258 to be nonzero.
9259
9260 - Save or restore as many registers in *MASK_PTR as possible.
9261 The instruction saves the first registers at the top of the
9262 allocated area, with the other registers below it.
9263
9264 - Save NARGS argument registers above the allocated area.
9265
9266 (NARGS is always zero if RESTORE_P.)
9267
9268 The SAVE and RESTORE instructions cannot save and restore all general
9269 registers, so there may be some registers left over for the caller to
9270 handle. Destructively modify *MASK_PTR so that it contains the registers
9271 that still need to be saved or restored. The caller can save these
9272 registers in the memory immediately below *OFFSET_PTR, which is a
9273 byte offset from the bottom of the allocated stack area. */
9274
9275 static rtx
9276 mips16e_build_save_restore (bool restore_p, unsigned int *mask_ptr,
9277 HOST_WIDE_INT *offset_ptr, unsigned int nargs,
9278 HOST_WIDE_INT size)
9279 {
9280 rtx pattern, set;
9281 HOST_WIDE_INT offset, top_offset;
9282 unsigned int i, regno;
9283 int n;
9284
9285 gcc_assert (cfun->machine->frame.num_fp == 0);
9286
9287 /* Calculate the number of elements in the PARALLEL. We need one element
9288 for the stack adjustment, one for each argument register save, and one
9289 for each additional register move. */
9290 n = 1 + nargs;
9291 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9292 if (BITSET_P (*mask_ptr, mips16e_save_restore_regs[i]))
9293 n++;
9294
9295 /* Create the final PARALLEL. */
9296 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (n));
9297 n = 0;
9298
9299 /* Add the stack pointer adjustment. */
9300 set = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9301 plus_constant (Pmode, stack_pointer_rtx,
9302 restore_p ? size : -size));
9303 RTX_FRAME_RELATED_P (set) = 1;
9304 XVECEXP (pattern, 0, n++) = set;
9305
9306 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
9307 top_offset = restore_p ? size : 0;
9308
9309 /* Save the arguments. */
9310 for (i = 0; i < nargs; i++)
9311 {
9312 offset = top_offset + i * UNITS_PER_WORD;
9313 set = mips16e_save_restore_reg (restore_p, true, offset,
9314 GP_ARG_FIRST + i);
9315 XVECEXP (pattern, 0, n++) = set;
9316 }
9317
9318 /* Then fill in the other register moves. */
9319 offset = top_offset;
9320 for (i = 0; i < ARRAY_SIZE (mips16e_save_restore_regs); i++)
9321 {
9322 regno = mips16e_save_restore_regs[i];
9323 if (BITSET_P (*mask_ptr, regno))
9324 {
9325 offset -= UNITS_PER_WORD;
9326 set = mips16e_save_restore_reg (restore_p, false, offset, regno);
9327 XVECEXP (pattern, 0, n++) = set;
9328 *mask_ptr &= ~(1 << regno);
9329 }
9330 }
9331
9332 /* Tell the caller what offset it should use for the remaining registers. */
9333 *offset_ptr = size + (offset - top_offset);
9334
9335 gcc_assert (n == XVECLEN (pattern, 0));
9336
9337 return pattern;
9338 }
9339
9340 /* PATTERN is a PARALLEL whose first element adds ADJUST to the stack
9341 pointer. Return true if PATTERN matches the kind of instruction
9342 generated by mips16e_build_save_restore. If INFO is nonnull,
9343 initialize it when returning true. */
9344
9345 bool
9346 mips16e_save_restore_pattern_p (rtx pattern, HOST_WIDE_INT adjust,
9347 struct mips16e_save_restore_info *info)
9348 {
9349 unsigned int i, nargs, mask, extra;
9350 HOST_WIDE_INT top_offset, save_offset, offset;
9351 rtx set, reg, mem, base;
9352 int n;
9353
9354 if (!GENERATE_MIPS16E_SAVE_RESTORE)
9355 return false;
9356
9357 /* Stack offsets in the PARALLEL are relative to the old stack pointer. */
9358 top_offset = adjust > 0 ? adjust : 0;
9359
9360 /* Interpret all other members of the PARALLEL. */
9361 save_offset = top_offset - UNITS_PER_WORD;
9362 mask = 0;
9363 nargs = 0;
9364 i = 0;
9365 for (n = 1; n < XVECLEN (pattern, 0); n++)
9366 {
9367 /* Check that we have a SET. */
9368 set = XVECEXP (pattern, 0, n);
9369 if (GET_CODE (set) != SET)
9370 return false;
9371
9372 /* Check that the SET is a load (if restoring) or a store
9373 (if saving). */
9374 mem = adjust > 0 ? SET_SRC (set) : SET_DEST (set);
9375 if (!MEM_P (mem))
9376 return false;
9377
9378 /* Check that the address is the sum of the stack pointer and a
9379 possibly-zero constant offset. */
9380 mips_split_plus (XEXP (mem, 0), &base, &offset);
9381 if (base != stack_pointer_rtx)
9382 return false;
9383
9384 /* Check that SET's other operand is a register. */
9385 reg = adjust > 0 ? SET_DEST (set) : SET_SRC (set);
9386 if (!REG_P (reg))
9387 return false;
9388
9389 /* Check for argument saves. */
9390 if (offset == top_offset + nargs * UNITS_PER_WORD
9391 && REGNO (reg) == GP_ARG_FIRST + nargs)
9392 nargs++;
9393 else if (offset == save_offset)
9394 {
9395 while (mips16e_save_restore_regs[i++] != REGNO (reg))
9396 if (i == ARRAY_SIZE (mips16e_save_restore_regs))
9397 return false;
9398
9399 mask |= 1 << REGNO (reg);
9400 save_offset -= UNITS_PER_WORD;
9401 }
9402 else
9403 return false;
9404 }
9405
9406 /* Check that the restrictions on register ranges are met. */
9407 extra = 0;
9408 mips16e_mask_registers (&mask, mips16e_s2_s8_regs,
9409 ARRAY_SIZE (mips16e_s2_s8_regs), &extra);
9410 mips16e_mask_registers (&mask, mips16e_a0_a3_regs,
9411 ARRAY_SIZE (mips16e_a0_a3_regs), &extra);
9412 if (extra != 0)
9413 return false;
9414
9415 /* Make sure that the topmost argument register is not saved twice.
9416 The checks above ensure that the same is then true for the other
9417 argument registers. */
9418 if (nargs > 0 && BITSET_P (mask, GP_ARG_FIRST + nargs - 1))
9419 return false;
9420
9421 /* Pass back information, if requested. */
9422 if (info)
9423 {
9424 info->nargs = nargs;
9425 info->mask = mask;
9426 info->size = (adjust > 0 ? adjust : -adjust);
9427 }
9428
9429 return true;
9430 }
9431
9432 /* Add a MIPS16e SAVE or RESTORE register-range argument to string S
9433 for the register range [MIN_REG, MAX_REG]. Return a pointer to
9434 the null terminator. */
9435
9436 static char *
9437 mips16e_add_register_range (char *s, unsigned int min_reg,
9438 unsigned int max_reg)
9439 {
9440 if (min_reg != max_reg)
9441 s += sprintf (s, ",%s-%s", reg_names[min_reg], reg_names[max_reg]);
9442 else
9443 s += sprintf (s, ",%s", reg_names[min_reg]);
9444 return s;
9445 }
9446
9447 /* Return the assembly instruction for a MIPS16e SAVE or RESTORE instruction.
9448 PATTERN and ADJUST are as for mips16e_save_restore_pattern_p. */
9449
9450 const char *
9451 mips16e_output_save_restore (rtx pattern, HOST_WIDE_INT adjust)
9452 {
9453 static char buffer[300];
9454
9455 struct mips16e_save_restore_info info;
9456 unsigned int i, end;
9457 char *s;
9458
9459 /* Parse the pattern. */
9460 if (!mips16e_save_restore_pattern_p (pattern, adjust, &info))
9461 gcc_unreachable ();
9462
9463 /* Add the mnemonic. */
9464 s = strcpy (buffer, adjust > 0 ? "restore\t" : "save\t");
9465 s += strlen (s);
9466
9467 /* Save the arguments. */
9468 if (info.nargs > 1)
9469 s += sprintf (s, "%s-%s,", reg_names[GP_ARG_FIRST],
9470 reg_names[GP_ARG_FIRST + info.nargs - 1]);
9471 else if (info.nargs == 1)
9472 s += sprintf (s, "%s,", reg_names[GP_ARG_FIRST]);
9473
9474 /* Emit the amount of stack space to allocate or deallocate. */
9475 s += sprintf (s, "%d", (int) info.size);
9476
9477 /* Save or restore $16. */
9478 if (BITSET_P (info.mask, 16))
9479 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 16]);
9480
9481 /* Save or restore $17. */
9482 if (BITSET_P (info.mask, 17))
9483 s += sprintf (s, ",%s", reg_names[GP_REG_FIRST + 17]);
9484
9485 /* Save or restore registers in the range $s2...$s8, which
9486 mips16e_s2_s8_regs lists in decreasing order. Note that this
9487 is a software register range; the hardware registers are not
9488 numbered consecutively. */
9489 end = ARRAY_SIZE (mips16e_s2_s8_regs);
9490 i = mips16e_find_first_register (info.mask, mips16e_s2_s8_regs, end);
9491 if (i < end)
9492 s = mips16e_add_register_range (s, mips16e_s2_s8_regs[end - 1],
9493 mips16e_s2_s8_regs[i]);
9494
9495 /* Save or restore registers in the range $a0...$a3. */
9496 end = ARRAY_SIZE (mips16e_a0_a3_regs);
9497 i = mips16e_find_first_register (info.mask, mips16e_a0_a3_regs, end);
9498 if (i < end)
9499 s = mips16e_add_register_range (s, mips16e_a0_a3_regs[i],
9500 mips16e_a0_a3_regs[end - 1]);
9501
9502 /* Save or restore $31. */
9503 if (BITSET_P (info.mask, RETURN_ADDR_REGNUM))
9504 s += sprintf (s, ",%s", reg_names[RETURN_ADDR_REGNUM]);
9505
9506 return buffer;
9507 }
9508 \f
9509 /* Return true if the current function returns its value in a floating-point
9510 register in MIPS16 mode. */
9511
9512 static bool
9513 mips16_cfun_returns_in_fpr_p (void)
9514 {
9515 tree return_type = DECL_RESULT (current_function_decl);
9516 return (TARGET_MIPS16
9517 && TARGET_HARD_FLOAT_ABI
9518 && !aggregate_value_p (return_type, current_function_decl)
9519 && mips_return_mode_in_fpr_p (DECL_MODE (return_type)));
9520 }
9521
9522 /* Return true if predicate PRED is true for at least one instruction.
9523 Cache the result in *CACHE, and assume that the result is true
9524 if *CACHE is already true. */
9525
9526 static bool
9527 mips_find_gp_ref (bool *cache, bool (*pred) (rtx_insn *))
9528 {
9529 rtx_insn *insn;
9530
9531 if (!*cache)
9532 {
9533 push_topmost_sequence ();
9534 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9535 if (USEFUL_INSN_P (insn) && pred (insn))
9536 {
9537 *cache = true;
9538 break;
9539 }
9540 pop_topmost_sequence ();
9541 }
9542 return *cache;
9543 }
9544
9545 /* Return true if INSN refers to the global pointer in an "inflexible" way.
9546 See mips_cfun_has_inflexible_gp_ref_p for details. */
9547
9548 static bool
9549 mips_insn_has_inflexible_gp_ref_p (rtx_insn *insn)
9550 {
9551 /* Uses of pic_offset_table_rtx in CALL_INSN_FUNCTION_USAGE
9552 indicate that the target could be a traditional MIPS
9553 lazily-binding stub. */
9554 return find_reg_fusage (insn, USE, pic_offset_table_rtx);
9555 }
9556
9557 /* Return true if the current function refers to the global pointer
9558 in a way that forces $28 to be valid. This means that we can't
9559 change the choice of global pointer, even for NewABI code.
9560
9561 One example of this (and one which needs several checks) is that
9562 $28 must be valid when calling traditional MIPS lazy-binding stubs.
9563 (This restriction does not apply to PLTs.) */
9564
9565 static bool
9566 mips_cfun_has_inflexible_gp_ref_p (void)
9567 {
9568 /* If the function has a nonlocal goto, $28 must hold the correct
9569 global pointer for the target function. That is, the target
9570 of the goto implicitly uses $28. */
9571 if (crtl->has_nonlocal_goto)
9572 return true;
9573
9574 if (TARGET_ABICALLS_PIC2)
9575 {
9576 /* Symbolic accesses implicitly use the global pointer unless
9577 -mexplicit-relocs is in effect. JAL macros to symbolic addresses
9578 might go to traditional MIPS lazy-binding stubs. */
9579 if (!TARGET_EXPLICIT_RELOCS)
9580 return true;
9581
9582 /* FUNCTION_PROFILER includes a JAL to _mcount, which again
9583 can be lazily-bound. */
9584 if (crtl->profile)
9585 return true;
9586
9587 /* MIPS16 functions that return in FPRs need to call an
9588 external libgcc routine. This call is only made explict
9589 during mips_expand_epilogue, and it too might be lazily bound. */
9590 if (mips16_cfun_returns_in_fpr_p ())
9591 return true;
9592 }
9593
9594 return mips_find_gp_ref (&cfun->machine->has_inflexible_gp_insn_p,
9595 mips_insn_has_inflexible_gp_ref_p);
9596 }
9597
9598 /* Return true if INSN refers to the global pointer in a "flexible" way.
9599 See mips_cfun_has_flexible_gp_ref_p for details. */
9600
9601 static bool
9602 mips_insn_has_flexible_gp_ref_p (rtx_insn *insn)
9603 {
9604 return (get_attr_got (insn) != GOT_UNSET
9605 || mips_small_data_pattern_p (PATTERN (insn))
9606 || reg_overlap_mentioned_p (pic_offset_table_rtx, PATTERN (insn)));
9607 }
9608
9609 /* Return true if the current function references the global pointer,
9610 but if those references do not inherently require the global pointer
9611 to be $28. Assume !mips_cfun_has_inflexible_gp_ref_p (). */
9612
9613 static bool
9614 mips_cfun_has_flexible_gp_ref_p (void)
9615 {
9616 /* Reload can sometimes introduce constant pool references
9617 into a function that otherwise didn't need them. For example,
9618 suppose we have an instruction like:
9619
9620 (set (reg:DF R1) (float:DF (reg:SI R2)))
9621
9622 If R2 turns out to be a constant such as 1, the instruction may
9623 have a REG_EQUAL note saying that R1 == 1.0. Reload then has
9624 the option of using this constant if R2 doesn't get allocated
9625 to a register.
9626
9627 In cases like these, reload will have added the constant to the
9628 pool but no instruction will yet refer to it. */
9629 if (TARGET_ABICALLS_PIC2 && !reload_completed && crtl->uses_const_pool)
9630 return true;
9631
9632 return mips_find_gp_ref (&cfun->machine->has_flexible_gp_insn_p,
9633 mips_insn_has_flexible_gp_ref_p);
9634 }
9635
9636 /* Return the register that should be used as the global pointer
9637 within this function. Return INVALID_REGNUM if the function
9638 doesn't need a global pointer. */
9639
9640 static unsigned int
9641 mips_global_pointer (void)
9642 {
9643 unsigned int regno;
9644
9645 /* $gp is always available unless we're using a GOT. */
9646 if (!TARGET_USE_GOT)
9647 return GLOBAL_POINTER_REGNUM;
9648
9649 /* If there are inflexible references to $gp, we must use the
9650 standard register. */
9651 if (mips_cfun_has_inflexible_gp_ref_p ())
9652 return GLOBAL_POINTER_REGNUM;
9653
9654 /* If there are no current references to $gp, then the only uses
9655 we can introduce later are those involved in long branches. */
9656 if (TARGET_ABSOLUTE_JUMPS && !mips_cfun_has_flexible_gp_ref_p ())
9657 return INVALID_REGNUM;
9658
9659 /* If the global pointer is call-saved, try to use a call-clobbered
9660 alternative. */
9661 if (TARGET_CALL_SAVED_GP && crtl->is_leaf)
9662 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
9663 if (!df_regs_ever_live_p (regno)
9664 && call_really_used_regs[regno]
9665 && !fixed_regs[regno]
9666 && regno != PIC_FUNCTION_ADDR_REGNUM)
9667 return regno;
9668
9669 return GLOBAL_POINTER_REGNUM;
9670 }
9671
9672 /* Return true if the current function's prologue must load the global
9673 pointer value into pic_offset_table_rtx and store the same value in
9674 the function's cprestore slot (if any).
9675
9676 One problem we have to deal with is that, when emitting GOT-based
9677 position independent code, long-branch sequences will need to load
9678 the address of the branch target from the GOT. We don't know until
9679 the very end of compilation whether (and where) the function needs
9680 long branches, so we must ensure that _any_ branch can access the
9681 global pointer in some form. However, we do not want to pessimize
9682 the usual case in which all branches are short.
9683
9684 We handle this as follows:
9685
9686 (1) During reload, we set cfun->machine->global_pointer to
9687 INVALID_REGNUM if we _know_ that the current function
9688 doesn't need a global pointer. This is only valid if
9689 long branches don't need the GOT.
9690
9691 Otherwise, we assume that we might need a global pointer
9692 and pick an appropriate register.
9693
9694 (2) If cfun->machine->global_pointer != INVALID_REGNUM,
9695 we ensure that the global pointer is available at every
9696 block boundary bar entry and exit. We do this in one of two ways:
9697
9698 - If the function has a cprestore slot, we ensure that this
9699 slot is valid at every branch. However, as explained in
9700 point (6) below, there is no guarantee that pic_offset_table_rtx
9701 itself is valid if new uses of the global pointer are introduced
9702 after the first post-epilogue split.
9703
9704 We guarantee that the cprestore slot is valid by loading it
9705 into a fake register, CPRESTORE_SLOT_REGNUM. We then make
9706 this register live at every block boundary bar function entry
9707 and exit. It is then invalid to move the load (and thus the
9708 preceding store) across a block boundary.
9709
9710 - If the function has no cprestore slot, we guarantee that
9711 pic_offset_table_rtx itself is valid at every branch.
9712
9713 See mips_eh_uses for the handling of the register liveness.
9714
9715 (3) During prologue and epilogue generation, we emit "ghost"
9716 placeholder instructions to manipulate the global pointer.
9717
9718 (4) During prologue generation, we set cfun->machine->must_initialize_gp_p
9719 and cfun->machine->must_restore_gp_when_clobbered_p if we already know
9720 that the function needs a global pointer. (There is no need to set
9721 them earlier than this, and doing it as late as possible leads to
9722 fewer false positives.)
9723
9724 (5) If cfun->machine->must_initialize_gp_p is true during a
9725 split_insns pass, we split the ghost instructions into real
9726 instructions. These split instructions can then be optimized in
9727 the usual way. Otherwise, we keep the ghost instructions intact,
9728 and optimize for the case where they aren't needed. We still
9729 have the option of splitting them later, if we need to introduce
9730 new uses of the global pointer.
9731
9732 For example, the scheduler ignores a ghost instruction that
9733 stores $28 to the stack, but it handles the split form of
9734 the ghost instruction as an ordinary store.
9735
9736 (6) [OldABI only.] If cfun->machine->must_restore_gp_when_clobbered_p
9737 is true during the first post-epilogue split_insns pass, we split
9738 calls and restore_gp patterns into instructions that explicitly
9739 load pic_offset_table_rtx from the cprestore slot. Otherwise,
9740 we split these patterns into instructions that _don't_ load from
9741 the cprestore slot.
9742
9743 If cfun->machine->must_restore_gp_when_clobbered_p is true at the
9744 time of the split, then any instructions that exist at that time
9745 can make free use of pic_offset_table_rtx. However, if we want
9746 to introduce new uses of the global pointer after the split,
9747 we must explicitly load the value from the cprestore slot, since
9748 pic_offset_table_rtx itself might not be valid at a given point
9749 in the function.
9750
9751 The idea is that we want to be able to delete redundant
9752 loads from the cprestore slot in the usual case where no
9753 long branches are needed.
9754
9755 (7) If cfun->machine->must_initialize_gp_p is still false at the end
9756 of md_reorg, we decide whether the global pointer is needed for
9757 long branches. If so, we set cfun->machine->must_initialize_gp_p
9758 to true and split the ghost instructions into real instructions
9759 at that stage.
9760
9761 Note that the ghost instructions must have a zero length for three reasons:
9762
9763 - Giving the length of the underlying $gp sequence might cause
9764 us to use long branches in cases where they aren't really needed.
9765
9766 - They would perturb things like alignment calculations.
9767
9768 - More importantly, the hazard detection in md_reorg relies on
9769 empty instructions having a zero length.
9770
9771 If we find a long branch and split the ghost instructions at the
9772 end of md_reorg, the split could introduce more long branches.
9773 That isn't a problem though, because we still do the split before
9774 the final shorten_branches pass.
9775
9776 This is extremely ugly, but it seems like the best compromise between
9777 correctness and efficiency. */
9778
9779 bool
9780 mips_must_initialize_gp_p (void)
9781 {
9782 return cfun->machine->must_initialize_gp_p;
9783 }
9784
9785 /* Return true if REGNO is a register that is ordinarily call-clobbered
9786 but must nevertheless be preserved by an interrupt handler. */
9787
9788 static bool
9789 mips_interrupt_extra_call_saved_reg_p (unsigned int regno)
9790 {
9791 if (MD_REG_P (regno))
9792 return true;
9793
9794 if (TARGET_DSP && DSP_ACC_REG_P (regno))
9795 return true;
9796
9797 if (GP_REG_P (regno) && !cfun->machine->use_shadow_register_set_p)
9798 {
9799 /* $0 is hard-wired. */
9800 if (regno == GP_REG_FIRST)
9801 return false;
9802
9803 /* The interrupt handler can treat kernel registers as
9804 scratch registers. */
9805 if (KERNEL_REG_P (regno))
9806 return false;
9807
9808 /* The function will return the stack pointer to its original value
9809 anyway. */
9810 if (regno == STACK_POINTER_REGNUM)
9811 return false;
9812
9813 /* Otherwise, return true for registers that aren't ordinarily
9814 call-clobbered. */
9815 return call_really_used_regs[regno];
9816 }
9817
9818 return false;
9819 }
9820
9821 /* Return true if the current function should treat register REGNO
9822 as call-saved. */
9823
9824 static bool
9825 mips_cfun_call_saved_reg_p (unsigned int regno)
9826 {
9827 /* If the user makes an ordinarily-call-saved register global,
9828 that register is no longer call-saved. */
9829 if (global_regs[regno])
9830 return false;
9831
9832 /* Interrupt handlers need to save extra registers. */
9833 if (cfun->machine->interrupt_handler_p
9834 && mips_interrupt_extra_call_saved_reg_p (regno))
9835 return true;
9836
9837 /* call_insns preserve $28 unless they explicitly say otherwise,
9838 so call_really_used_regs[] treats $28 as call-saved. However,
9839 we want the ABI property rather than the default call_insn
9840 property here. */
9841 return (regno == GLOBAL_POINTER_REGNUM
9842 ? TARGET_CALL_SAVED_GP
9843 : !call_really_used_regs[regno]);
9844 }
9845
9846 /* Return true if the function body might clobber register REGNO.
9847 We know that REGNO is call-saved. */
9848
9849 static bool
9850 mips_cfun_might_clobber_call_saved_reg_p (unsigned int regno)
9851 {
9852 /* Some functions should be treated as clobbering all call-saved
9853 registers. */
9854 if (crtl->saves_all_registers)
9855 return true;
9856
9857 /* DF handles cases where a register is explicitly referenced in
9858 the rtl. Incoming values are passed in call-clobbered registers,
9859 so we can assume that any live call-saved register is set within
9860 the function. */
9861 if (df_regs_ever_live_p (regno))
9862 return true;
9863
9864 /* Check for registers that are clobbered by FUNCTION_PROFILER.
9865 These clobbers are not explicit in the rtl. */
9866 if (crtl->profile && MIPS_SAVE_REG_FOR_PROFILING_P (regno))
9867 return true;
9868
9869 /* If we're using a call-saved global pointer, the function's
9870 prologue will need to set it up. */
9871 if (cfun->machine->global_pointer == regno)
9872 return true;
9873
9874 /* The function's prologue will need to set the frame pointer if
9875 frame_pointer_needed. */
9876 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
9877 return true;
9878
9879 /* If a MIPS16 function returns a value in FPRs, its epilogue
9880 will need to call an external libgcc routine. This yet-to-be
9881 generated call_insn will clobber $31. */
9882 if (regno == RETURN_ADDR_REGNUM && mips16_cfun_returns_in_fpr_p ())
9883 return true;
9884
9885 /* If REGNO is ordinarily call-clobbered, we must assume that any
9886 called function could modify it. */
9887 if (cfun->machine->interrupt_handler_p
9888 && !crtl->is_leaf
9889 && mips_interrupt_extra_call_saved_reg_p (regno))
9890 return true;
9891
9892 return false;
9893 }
9894
9895 /* Return true if the current function must save register REGNO. */
9896
9897 static bool
9898 mips_save_reg_p (unsigned int regno)
9899 {
9900 if (mips_cfun_call_saved_reg_p (regno))
9901 {
9902 if (mips_cfun_might_clobber_call_saved_reg_p (regno))
9903 return true;
9904
9905 /* Save both registers in an FPR pair if either one is used. This is
9906 needed for the case when MIN_FPRS_PER_FMT == 1, which allows the odd
9907 register to be used without the even register. */
9908 if (FP_REG_P (regno)
9909 && MAX_FPRS_PER_FMT == 2
9910 && mips_cfun_might_clobber_call_saved_reg_p (regno + 1))
9911 return true;
9912 }
9913
9914 /* We need to save the incoming return address if __builtin_eh_return
9915 is being used to set a different return address. */
9916 if (regno == RETURN_ADDR_REGNUM && crtl->calls_eh_return)
9917 return true;
9918
9919 return false;
9920 }
9921
9922 /* Populate the current function's mips_frame_info structure.
9923
9924 MIPS stack frames look like:
9925
9926 +-------------------------------+
9927 | |
9928 | incoming stack arguments |
9929 | |
9930 +-------------------------------+
9931 | |
9932 | caller-allocated save area |
9933 A | for register arguments |
9934 | |
9935 +-------------------------------+ <-- incoming stack pointer
9936 | |
9937 | callee-allocated save area |
9938 B | for arguments that are |
9939 | split between registers and |
9940 | the stack |
9941 | |
9942 +-------------------------------+ <-- arg_pointer_rtx
9943 | |
9944 C | callee-allocated save area |
9945 | for register varargs |
9946 | |
9947 +-------------------------------+ <-- frame_pointer_rtx
9948 | | + cop0_sp_offset
9949 | COP0 reg save area | + UNITS_PER_WORD
9950 | |
9951 +-------------------------------+ <-- frame_pointer_rtx + acc_sp_offset
9952 | | + UNITS_PER_WORD
9953 | accumulator save area |
9954 | |
9955 +-------------------------------+ <-- stack_pointer_rtx + fp_sp_offset
9956 | | + UNITS_PER_HWFPVALUE
9957 | FPR save area |
9958 | |
9959 +-------------------------------+ <-- stack_pointer_rtx + gp_sp_offset
9960 | | + UNITS_PER_WORD
9961 | GPR save area |
9962 | |
9963 +-------------------------------+ <-- frame_pointer_rtx with
9964 | | \ -fstack-protector
9965 | local variables | | var_size
9966 | | /
9967 +-------------------------------+
9968 | | \
9969 | $gp save area | | cprestore_size
9970 | | /
9971 P +-------------------------------+ <-- hard_frame_pointer_rtx for
9972 | | \ MIPS16 code
9973 | outgoing stack arguments | |
9974 | | |
9975 +-------------------------------+ | args_size
9976 | | |
9977 | caller-allocated save area | |
9978 | for register arguments | |
9979 | | /
9980 +-------------------------------+ <-- stack_pointer_rtx
9981 frame_pointer_rtx without
9982 -fstack-protector
9983 hard_frame_pointer_rtx for
9984 non-MIPS16 code.
9985
9986 At least two of A, B and C will be empty.
9987
9988 Dynamic stack allocations such as alloca insert data at point P.
9989 They decrease stack_pointer_rtx but leave frame_pointer_rtx and
9990 hard_frame_pointer_rtx unchanged. */
9991
9992 static void
9993 mips_compute_frame_info (void)
9994 {
9995 struct mips_frame_info *frame;
9996 HOST_WIDE_INT offset, size;
9997 unsigned int regno, i;
9998
9999 /* Set this function's interrupt properties. */
10000 if (mips_interrupt_type_p (TREE_TYPE (current_function_decl)))
10001 {
10002 if (mips_isa_rev < 2)
10003 error ("the %<interrupt%> attribute requires a MIPS32r2 processor or greater");
10004 else if (TARGET_HARD_FLOAT)
10005 error ("the %<interrupt%> attribute requires %<-msoft-float%>");
10006 else if (TARGET_MIPS16)
10007 error ("interrupt handlers cannot be MIPS16 functions");
10008 else
10009 {
10010 cfun->machine->interrupt_handler_p = true;
10011 cfun->machine->use_shadow_register_set_p =
10012 mips_use_shadow_register_set_p (TREE_TYPE (current_function_decl));
10013 cfun->machine->keep_interrupts_masked_p =
10014 mips_keep_interrupts_masked_p (TREE_TYPE (current_function_decl));
10015 cfun->machine->use_debug_exception_return_p =
10016 mips_use_debug_exception_return_p (TREE_TYPE
10017 (current_function_decl));
10018 }
10019 }
10020
10021 frame = &cfun->machine->frame;
10022 memset (frame, 0, sizeof (*frame));
10023 size = get_frame_size ();
10024
10025 cfun->machine->global_pointer = mips_global_pointer ();
10026
10027 /* The first two blocks contain the outgoing argument area and the $gp save
10028 slot. This area isn't needed in leaf functions, but if the
10029 target-independent frame size is nonzero, we have already committed to
10030 allocating these in STARTING_FRAME_OFFSET for !FRAME_GROWS_DOWNWARD. */
10031 if ((size == 0 || FRAME_GROWS_DOWNWARD) && crtl->is_leaf)
10032 {
10033 /* The MIPS 3.0 linker does not like functions that dynamically
10034 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
10035 looks like we are trying to create a second frame pointer to the
10036 function, so allocate some stack space to make it happy. */
10037 if (cfun->calls_alloca)
10038 frame->args_size = REG_PARM_STACK_SPACE (cfun->decl);
10039 else
10040 frame->args_size = 0;
10041 frame->cprestore_size = 0;
10042 }
10043 else
10044 {
10045 frame->args_size = crtl->outgoing_args_size;
10046 frame->cprestore_size = MIPS_GP_SAVE_AREA_SIZE;
10047 }
10048 offset = frame->args_size + frame->cprestore_size;
10049
10050 /* Move above the local variables. */
10051 frame->var_size = MIPS_STACK_ALIGN (size);
10052 offset += frame->var_size;
10053
10054 /* Find out which GPRs we need to save. */
10055 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
10056 if (mips_save_reg_p (regno))
10057 {
10058 frame->num_gp++;
10059 frame->mask |= 1 << (regno - GP_REG_FIRST);
10060 }
10061
10062 /* If this function calls eh_return, we must also save and restore the
10063 EH data registers. */
10064 if (crtl->calls_eh_return)
10065 for (i = 0; EH_RETURN_DATA_REGNO (i) != INVALID_REGNUM; i++)
10066 {
10067 frame->num_gp++;
10068 frame->mask |= 1 << (EH_RETURN_DATA_REGNO (i) - GP_REG_FIRST);
10069 }
10070
10071 /* The MIPS16e SAVE and RESTORE instructions have two ranges of registers:
10072 $a3-$a0 and $s2-$s8. If we save one register in the range, we must
10073 save all later registers too. */
10074 if (GENERATE_MIPS16E_SAVE_RESTORE)
10075 {
10076 mips16e_mask_registers (&frame->mask, mips16e_s2_s8_regs,
10077 ARRAY_SIZE (mips16e_s2_s8_regs), &frame->num_gp);
10078 mips16e_mask_registers (&frame->mask, mips16e_a0_a3_regs,
10079 ARRAY_SIZE (mips16e_a0_a3_regs), &frame->num_gp);
10080 }
10081
10082 /* Move above the GPR save area. */
10083 if (frame->num_gp > 0)
10084 {
10085 offset += MIPS_STACK_ALIGN (frame->num_gp * UNITS_PER_WORD);
10086 frame->gp_sp_offset = offset - UNITS_PER_WORD;
10087 }
10088
10089 /* Find out which FPRs we need to save. This loop must iterate over
10090 the same space as its companion in mips_for_each_saved_gpr_and_fpr. */
10091 if (TARGET_HARD_FLOAT)
10092 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno += MAX_FPRS_PER_FMT)
10093 if (mips_save_reg_p (regno))
10094 {
10095 frame->num_fp += MAX_FPRS_PER_FMT;
10096 frame->fmask |= ~(~0 << MAX_FPRS_PER_FMT) << (regno - FP_REG_FIRST);
10097 }
10098
10099 /* Move above the FPR save area. */
10100 if (frame->num_fp > 0)
10101 {
10102 offset += MIPS_STACK_ALIGN (frame->num_fp * UNITS_PER_FPREG);
10103 frame->fp_sp_offset = offset - UNITS_PER_HWFPVALUE;
10104 }
10105
10106 /* Add in space for the interrupt context information. */
10107 if (cfun->machine->interrupt_handler_p)
10108 {
10109 /* Check HI/LO. */
10110 if (mips_save_reg_p (LO_REGNUM) || mips_save_reg_p (HI_REGNUM))
10111 {
10112 frame->num_acc++;
10113 frame->acc_mask |= (1 << 0);
10114 }
10115
10116 /* Check accumulators 1, 2, 3. */
10117 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
10118 if (mips_save_reg_p (i) || mips_save_reg_p (i + 1))
10119 {
10120 frame->num_acc++;
10121 frame->acc_mask |= 1 << (((i - DSP_ACC_REG_FIRST) / 2) + 1);
10122 }
10123
10124 /* All interrupt context functions need space to preserve STATUS. */
10125 frame->num_cop0_regs++;
10126
10127 /* If we don't keep interrupts masked, we need to save EPC. */
10128 if (!cfun->machine->keep_interrupts_masked_p)
10129 frame->num_cop0_regs++;
10130 }
10131
10132 /* Move above the accumulator save area. */
10133 if (frame->num_acc > 0)
10134 {
10135 /* Each accumulator needs 2 words. */
10136 offset += frame->num_acc * 2 * UNITS_PER_WORD;
10137 frame->acc_sp_offset = offset - UNITS_PER_WORD;
10138 }
10139
10140 /* Move above the COP0 register save area. */
10141 if (frame->num_cop0_regs > 0)
10142 {
10143 offset += frame->num_cop0_regs * UNITS_PER_WORD;
10144 frame->cop0_sp_offset = offset - UNITS_PER_WORD;
10145 }
10146
10147 /* Move above the callee-allocated varargs save area. */
10148 offset += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
10149 frame->arg_pointer_offset = offset;
10150
10151 /* Move above the callee-allocated area for pretend stack arguments. */
10152 offset += crtl->args.pretend_args_size;
10153 frame->total_size = offset;
10154
10155 /* Work out the offsets of the save areas from the top of the frame. */
10156 if (frame->gp_sp_offset > 0)
10157 frame->gp_save_offset = frame->gp_sp_offset - offset;
10158 if (frame->fp_sp_offset > 0)
10159 frame->fp_save_offset = frame->fp_sp_offset - offset;
10160 if (frame->acc_sp_offset > 0)
10161 frame->acc_save_offset = frame->acc_sp_offset - offset;
10162 if (frame->num_cop0_regs > 0)
10163 frame->cop0_save_offset = frame->cop0_sp_offset - offset;
10164
10165 /* MIPS16 code offsets the frame pointer by the size of the outgoing
10166 arguments. This tends to increase the chances of using unextended
10167 instructions for local variables and incoming arguments. */
10168 if (TARGET_MIPS16)
10169 frame->hard_frame_pointer_offset = frame->args_size;
10170 }
10171
10172 /* Return the style of GP load sequence that is being used for the
10173 current function. */
10174
10175 enum mips_loadgp_style
10176 mips_current_loadgp_style (void)
10177 {
10178 if (!TARGET_USE_GOT || cfun->machine->global_pointer == INVALID_REGNUM)
10179 return LOADGP_NONE;
10180
10181 if (TARGET_RTP_PIC)
10182 return LOADGP_RTP;
10183
10184 if (TARGET_ABSOLUTE_ABICALLS)
10185 return LOADGP_ABSOLUTE;
10186
10187 return TARGET_NEWABI ? LOADGP_NEWABI : LOADGP_OLDABI;
10188 }
10189
10190 /* Implement TARGET_FRAME_POINTER_REQUIRED. */
10191
10192 static bool
10193 mips_frame_pointer_required (void)
10194 {
10195 /* If the function contains dynamic stack allocations, we need to
10196 use the frame pointer to access the static parts of the frame. */
10197 if (cfun->calls_alloca)
10198 return true;
10199
10200 /* In MIPS16 mode, we need a frame pointer for a large frame; otherwise,
10201 reload may be unable to compute the address of a local variable,
10202 since there is no way to add a large constant to the stack pointer
10203 without using a second temporary register. */
10204 if (TARGET_MIPS16)
10205 {
10206 mips_compute_frame_info ();
10207 if (!SMALL_OPERAND (cfun->machine->frame.total_size))
10208 return true;
10209 }
10210
10211 return false;
10212 }
10213
10214 /* Make sure that we're not trying to eliminate to the wrong hard frame
10215 pointer. */
10216
10217 static bool
10218 mips_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
10219 {
10220 return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM);
10221 }
10222
10223 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer
10224 or argument pointer. TO is either the stack pointer or hard frame
10225 pointer. */
10226
10227 HOST_WIDE_INT
10228 mips_initial_elimination_offset (int from, int to)
10229 {
10230 HOST_WIDE_INT offset;
10231
10232 mips_compute_frame_info ();
10233
10234 /* Set OFFSET to the offset from the end-of-prologue stack pointer. */
10235 switch (from)
10236 {
10237 case FRAME_POINTER_REGNUM:
10238 if (FRAME_GROWS_DOWNWARD)
10239 offset = (cfun->machine->frame.args_size
10240 + cfun->machine->frame.cprestore_size
10241 + cfun->machine->frame.var_size);
10242 else
10243 offset = 0;
10244 break;
10245
10246 case ARG_POINTER_REGNUM:
10247 offset = cfun->machine->frame.arg_pointer_offset;
10248 break;
10249
10250 default:
10251 gcc_unreachable ();
10252 }
10253
10254 if (to == HARD_FRAME_POINTER_REGNUM)
10255 offset -= cfun->machine->frame.hard_frame_pointer_offset;
10256
10257 return offset;
10258 }
10259 \f
10260 /* Implement TARGET_EXTRA_LIVE_ON_ENTRY. */
10261
10262 static void
10263 mips_extra_live_on_entry (bitmap regs)
10264 {
10265 if (TARGET_USE_GOT)
10266 {
10267 /* PIC_FUNCTION_ADDR_REGNUM is live if we need it to set up
10268 the global pointer. */
10269 if (!TARGET_ABSOLUTE_ABICALLS)
10270 bitmap_set_bit (regs, PIC_FUNCTION_ADDR_REGNUM);
10271
10272 /* The prologue may set MIPS16_PIC_TEMP_REGNUM to the value of
10273 the global pointer. */
10274 if (TARGET_MIPS16)
10275 bitmap_set_bit (regs, MIPS16_PIC_TEMP_REGNUM);
10276
10277 /* See the comment above load_call<mode> for details. */
10278 bitmap_set_bit (regs, GOT_VERSION_REGNUM);
10279 }
10280 }
10281
10282 /* Implement RETURN_ADDR_RTX. We do not support moving back to a
10283 previous frame. */
10284
10285 rtx
10286 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
10287 {
10288 if (count != 0)
10289 return const0_rtx;
10290
10291 return get_hard_reg_initial_val (Pmode, RETURN_ADDR_REGNUM);
10292 }
10293
10294 /* Emit code to change the current function's return address to
10295 ADDRESS. SCRATCH is available as a scratch register, if needed.
10296 ADDRESS and SCRATCH are both word-mode GPRs. */
10297
10298 void
10299 mips_set_return_address (rtx address, rtx scratch)
10300 {
10301 rtx slot_address;
10302
10303 gcc_assert (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM));
10304 slot_address = mips_add_offset (scratch, stack_pointer_rtx,
10305 cfun->machine->frame.gp_sp_offset);
10306 mips_emit_move (gen_frame_mem (GET_MODE (address), slot_address), address);
10307 }
10308
10309 /* Return true if the current function has a cprestore slot. */
10310
10311 bool
10312 mips_cfun_has_cprestore_slot_p (void)
10313 {
10314 return (cfun->machine->global_pointer != INVALID_REGNUM
10315 && cfun->machine->frame.cprestore_size > 0);
10316 }
10317
10318 /* Fill *BASE and *OFFSET such that *BASE + *OFFSET refers to the
10319 cprestore slot. LOAD_P is true if the caller wants to load from
10320 the cprestore slot; it is false if the caller wants to store to
10321 the slot. */
10322
10323 static void
10324 mips_get_cprestore_base_and_offset (rtx *base, HOST_WIDE_INT *offset,
10325 bool load_p)
10326 {
10327 const struct mips_frame_info *frame;
10328
10329 frame = &cfun->machine->frame;
10330 /* .cprestore always uses the stack pointer instead of the frame pointer.
10331 We have a free choice for direct stores for non-MIPS16 functions,
10332 and for MIPS16 functions whose cprestore slot is in range of the
10333 stack pointer. Using the stack pointer would sometimes give more
10334 (early) scheduling freedom, but using the frame pointer would
10335 sometimes give more (late) scheduling freedom. It's hard to
10336 predict which applies to a given function, so let's keep things
10337 simple.
10338
10339 Loads must always use the frame pointer in functions that call
10340 alloca, and there's little benefit to using the stack pointer
10341 otherwise. */
10342 if (frame_pointer_needed && !(TARGET_CPRESTORE_DIRECTIVE && !load_p))
10343 {
10344 *base = hard_frame_pointer_rtx;
10345 *offset = frame->args_size - frame->hard_frame_pointer_offset;
10346 }
10347 else
10348 {
10349 *base = stack_pointer_rtx;
10350 *offset = frame->args_size;
10351 }
10352 }
10353
10354 /* Return true if X is the load or store address of the cprestore slot;
10355 LOAD_P says which. */
10356
10357 bool
10358 mips_cprestore_address_p (rtx x, bool load_p)
10359 {
10360 rtx given_base, required_base;
10361 HOST_WIDE_INT given_offset, required_offset;
10362
10363 mips_split_plus (x, &given_base, &given_offset);
10364 mips_get_cprestore_base_and_offset (&required_base, &required_offset, load_p);
10365 return given_base == required_base && given_offset == required_offset;
10366 }
10367
10368 /* Return a MEM rtx for the cprestore slot. LOAD_P is true if we are
10369 going to load from it, false if we are going to store to it.
10370 Use TEMP as a temporary register if need be. */
10371
10372 static rtx
10373 mips_cprestore_slot (rtx temp, bool load_p)
10374 {
10375 rtx base;
10376 HOST_WIDE_INT offset;
10377
10378 mips_get_cprestore_base_and_offset (&base, &offset, load_p);
10379 return gen_frame_mem (Pmode, mips_add_offset (temp, base, offset));
10380 }
10381
10382 /* Emit instructions to save global pointer value GP into cprestore
10383 slot MEM. OFFSET is the offset that MEM applies to the base register.
10384
10385 MEM may not be a legitimate address. If it isn't, TEMP is a
10386 temporary register that can be used, otherwise it is a SCRATCH. */
10387
10388 void
10389 mips_save_gp_to_cprestore_slot (rtx mem, rtx offset, rtx gp, rtx temp)
10390 {
10391 if (TARGET_CPRESTORE_DIRECTIVE)
10392 {
10393 gcc_assert (gp == pic_offset_table_rtx);
10394 emit_insn (PMODE_INSN (gen_cprestore, (mem, offset)));
10395 }
10396 else
10397 mips_emit_move (mips_cprestore_slot (temp, false), gp);
10398 }
10399
10400 /* Restore $gp from its save slot, using TEMP as a temporary base register
10401 if need be. This function is for o32 and o64 abicalls only.
10402
10403 See mips_must_initialize_gp_p for details about how we manage the
10404 global pointer. */
10405
10406 void
10407 mips_restore_gp_from_cprestore_slot (rtx temp)
10408 {
10409 gcc_assert (TARGET_ABICALLS && TARGET_OLDABI && epilogue_completed);
10410
10411 if (!cfun->machine->must_restore_gp_when_clobbered_p)
10412 {
10413 emit_note (NOTE_INSN_DELETED);
10414 return;
10415 }
10416
10417 if (TARGET_MIPS16)
10418 {
10419 mips_emit_move (temp, mips_cprestore_slot (temp, true));
10420 mips_emit_move (pic_offset_table_rtx, temp);
10421 }
10422 else
10423 mips_emit_move (pic_offset_table_rtx, mips_cprestore_slot (temp, true));
10424 if (!TARGET_EXPLICIT_RELOCS)
10425 emit_insn (gen_blockage ());
10426 }
10427 \f
10428 /* A function to save or store a register. The first argument is the
10429 register and the second is the stack slot. */
10430 typedef void (*mips_save_restore_fn) (rtx, rtx);
10431
10432 /* Use FN to save or restore register REGNO. MODE is the register's
10433 mode and OFFSET is the offset of its save slot from the current
10434 stack pointer. */
10435
10436 static void
10437 mips_save_restore_reg (machine_mode mode, int regno,
10438 HOST_WIDE_INT offset, mips_save_restore_fn fn)
10439 {
10440 rtx mem;
10441
10442 mem = gen_frame_mem (mode, plus_constant (Pmode, stack_pointer_rtx,
10443 offset));
10444 fn (gen_rtx_REG (mode, regno), mem);
10445 }
10446
10447 /* Call FN for each accumlator that is saved by the current function.
10448 SP_OFFSET is the offset of the current stack pointer from the start
10449 of the frame. */
10450
10451 static void
10452 mips_for_each_saved_acc (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
10453 {
10454 HOST_WIDE_INT offset;
10455 int regno;
10456
10457 offset = cfun->machine->frame.acc_sp_offset - sp_offset;
10458 if (BITSET_P (cfun->machine->frame.acc_mask, 0))
10459 {
10460 mips_save_restore_reg (word_mode, LO_REGNUM, offset, fn);
10461 offset -= UNITS_PER_WORD;
10462 mips_save_restore_reg (word_mode, HI_REGNUM, offset, fn);
10463 offset -= UNITS_PER_WORD;
10464 }
10465
10466 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
10467 if (BITSET_P (cfun->machine->frame.acc_mask,
10468 ((regno - DSP_ACC_REG_FIRST) / 2) + 1))
10469 {
10470 mips_save_restore_reg (word_mode, regno, offset, fn);
10471 offset -= UNITS_PER_WORD;
10472 }
10473 }
10474
10475 /* Save register REG to MEM. Make the instruction frame-related. */
10476
10477 static void
10478 mips_save_reg (rtx reg, rtx mem)
10479 {
10480 if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
10481 {
10482 rtx x1, x2;
10483
10484 mips_emit_move_or_split (mem, reg, SPLIT_IF_NECESSARY);
10485
10486 x1 = mips_frame_set (mips_subword (mem, false),
10487 mips_subword (reg, false));
10488 x2 = mips_frame_set (mips_subword (mem, true),
10489 mips_subword (reg, true));
10490 mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
10491 }
10492 else
10493 mips_emit_save_slot_move (mem, reg, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
10494 }
10495
10496 /* Capture the register combinations that are allowed in a SWM or LWM
10497 instruction. The entries are ordered by number of registers set in
10498 the mask. We also ignore the single register encodings because a
10499 normal SW/LW is preferred. */
10500
10501 static const unsigned int umips_swm_mask[17] = {
10502 0xc0ff0000, 0x80ff0000, 0x40ff0000, 0x807f0000,
10503 0x00ff0000, 0x803f0000, 0x007f0000, 0x801f0000,
10504 0x003f0000, 0x800f0000, 0x001f0000, 0x80070000,
10505 0x000f0000, 0x80030000, 0x00070000, 0x80010000,
10506 0x00030000
10507 };
10508
10509 static const unsigned int umips_swm_encoding[17] = {
10510 25, 24, 9, 23, 8, 22, 7, 21, 6, 20, 5, 19, 4, 18, 3, 17, 2
10511 };
10512
10513 /* Try to use a microMIPS LWM or SWM instruction to save or restore
10514 as many GPRs in *MASK as possible. *OFFSET is the offset from the
10515 stack pointer of the topmost save slot.
10516
10517 Remove from *MASK all registers that were handled using LWM and SWM.
10518 Update *OFFSET so that it points to the first unused save slot. */
10519
10520 static bool
10521 umips_build_save_restore (mips_save_restore_fn fn,
10522 unsigned *mask, HOST_WIDE_INT *offset)
10523 {
10524 int nregs;
10525 unsigned int i, j;
10526 rtx pattern, set, reg, mem;
10527 HOST_WIDE_INT this_offset;
10528 rtx this_base;
10529
10530 /* Try matching $16 to $31 (s0 to ra). */
10531 for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
10532 if ((*mask & 0xffff0000) == umips_swm_mask[i])
10533 break;
10534
10535 if (i == ARRAY_SIZE (umips_swm_mask))
10536 return false;
10537
10538 /* Get the offset of the lowest save slot. */
10539 nregs = (umips_swm_encoding[i] & 0xf) + (umips_swm_encoding[i] >> 4);
10540 this_offset = *offset - UNITS_PER_WORD * (nregs - 1);
10541
10542 /* LWM/SWM can only support offsets from -2048 to 2047. */
10543 if (!UMIPS_12BIT_OFFSET_P (this_offset))
10544 return false;
10545
10546 /* Create the final PARALLEL. */
10547 pattern = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (nregs));
10548 this_base = stack_pointer_rtx;
10549
10550 /* For registers $16-$23 and $30. */
10551 for (j = 0; j < (umips_swm_encoding[i] & 0xf); j++)
10552 {
10553 HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10554 mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10555 unsigned int regno = (j != 8) ? 16 + j : 30;
10556 *mask &= ~(1 << regno);
10557 reg = gen_rtx_REG (SImode, regno);
10558 if (fn == mips_save_reg)
10559 set = mips_frame_set (mem, reg);
10560 else
10561 {
10562 set = gen_rtx_SET (VOIDmode, reg, mem);
10563 mips_add_cfa_restore (reg);
10564 }
10565 XVECEXP (pattern, 0, j) = set;
10566 }
10567
10568 /* For register $31. */
10569 if (umips_swm_encoding[i] >> 4)
10570 {
10571 HOST_WIDE_INT offset = this_offset + j * UNITS_PER_WORD;
10572 *mask &= ~(1 << 31);
10573 mem = gen_frame_mem (SImode, plus_constant (Pmode, this_base, offset));
10574 reg = gen_rtx_REG (SImode, 31);
10575 if (fn == mips_save_reg)
10576 set = mips_frame_set (mem, reg);
10577 else
10578 {
10579 set = gen_rtx_SET (VOIDmode, reg, mem);
10580 mips_add_cfa_restore (reg);
10581 }
10582 XVECEXP (pattern, 0, j) = set;
10583 }
10584
10585 pattern = emit_insn (pattern);
10586 if (fn == mips_save_reg)
10587 RTX_FRAME_RELATED_P (pattern) = 1;
10588
10589 /* Adjust the last offset. */
10590 *offset -= UNITS_PER_WORD * nregs;
10591
10592 return true;
10593 }
10594
10595 /* Call FN for each register that is saved by the current function.
10596 SP_OFFSET is the offset of the current stack pointer from the start
10597 of the frame. */
10598
10599 static void
10600 mips_for_each_saved_gpr_and_fpr (HOST_WIDE_INT sp_offset,
10601 mips_save_restore_fn fn)
10602 {
10603 machine_mode fpr_mode;
10604 int regno;
10605 const struct mips_frame_info *frame = &cfun->machine->frame;
10606 HOST_WIDE_INT offset;
10607 unsigned int mask;
10608
10609 /* Save registers starting from high to low. The debuggers prefer at least
10610 the return register be stored at func+4, and also it allows us not to
10611 need a nop in the epilogue if at least one register is reloaded in
10612 addition to return address. */
10613 offset = frame->gp_sp_offset - sp_offset;
10614 mask = frame->mask;
10615
10616 if (TARGET_MICROMIPS)
10617 umips_build_save_restore (fn, &mask, &offset);
10618
10619 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
10620 if (BITSET_P (mask, regno - GP_REG_FIRST))
10621 {
10622 /* Record the ra offset for use by mips_function_profiler. */
10623 if (regno == RETURN_ADDR_REGNUM)
10624 cfun->machine->frame.ra_fp_offset = offset + sp_offset;
10625 mips_save_restore_reg (word_mode, regno, offset, fn);
10626 offset -= UNITS_PER_WORD;
10627 }
10628
10629 /* This loop must iterate over the same space as its companion in
10630 mips_compute_frame_info. */
10631 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
10632 fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
10633 for (regno = FP_REG_LAST - MAX_FPRS_PER_FMT + 1;
10634 regno >= FP_REG_FIRST;
10635 regno -= MAX_FPRS_PER_FMT)
10636 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
10637 {
10638 mips_save_restore_reg (fpr_mode, regno, offset, fn);
10639 offset -= GET_MODE_SIZE (fpr_mode);
10640 }
10641 }
10642
10643 /* Return true if a move between register REGNO and its save slot (MEM)
10644 can be done in a single move. LOAD_P is true if we are loading
10645 from the slot, false if we are storing to it. */
10646
10647 static bool
10648 mips_direct_save_slot_move_p (unsigned int regno, rtx mem, bool load_p)
10649 {
10650 /* There is a specific MIPS16 instruction for saving $31 to the stack. */
10651 if (TARGET_MIPS16 && !load_p && regno == RETURN_ADDR_REGNUM)
10652 return false;
10653
10654 return mips_secondary_reload_class (REGNO_REG_CLASS (regno),
10655 GET_MODE (mem), mem, load_p) == NO_REGS;
10656 }
10657
10658 /* Emit a move from SRC to DEST, given that one of them is a register
10659 save slot and that the other is a register. TEMP is a temporary
10660 GPR of the same mode that is available if need be. */
10661
10662 void
10663 mips_emit_save_slot_move (rtx dest, rtx src, rtx temp)
10664 {
10665 unsigned int regno;
10666 rtx mem;
10667
10668 if (REG_P (src))
10669 {
10670 regno = REGNO (src);
10671 mem = dest;
10672 }
10673 else
10674 {
10675 regno = REGNO (dest);
10676 mem = src;
10677 }
10678
10679 if (regno == cfun->machine->global_pointer && !mips_must_initialize_gp_p ())
10680 {
10681 /* We don't yet know whether we'll need this instruction or not.
10682 Postpone the decision by emitting a ghost move. This move
10683 is specifically not frame-related; only the split version is. */
10684 if (TARGET_64BIT)
10685 emit_insn (gen_move_gpdi (dest, src));
10686 else
10687 emit_insn (gen_move_gpsi (dest, src));
10688 return;
10689 }
10690
10691 if (regno == HI_REGNUM)
10692 {
10693 if (REG_P (dest))
10694 {
10695 mips_emit_move (temp, src);
10696 if (TARGET_64BIT)
10697 emit_insn (gen_mthisi_di (gen_rtx_REG (TImode, MD_REG_FIRST),
10698 temp, gen_rtx_REG (DImode, LO_REGNUM)));
10699 else
10700 emit_insn (gen_mthisi_di (gen_rtx_REG (DImode, MD_REG_FIRST),
10701 temp, gen_rtx_REG (SImode, LO_REGNUM)));
10702 }
10703 else
10704 {
10705 if (TARGET_64BIT)
10706 emit_insn (gen_mfhidi_ti (temp,
10707 gen_rtx_REG (TImode, MD_REG_FIRST)));
10708 else
10709 emit_insn (gen_mfhisi_di (temp,
10710 gen_rtx_REG (DImode, MD_REG_FIRST)));
10711 mips_emit_move (dest, temp);
10712 }
10713 }
10714 else if (mips_direct_save_slot_move_p (regno, mem, mem == src))
10715 mips_emit_move (dest, src);
10716 else
10717 {
10718 gcc_assert (!reg_overlap_mentioned_p (dest, temp));
10719 mips_emit_move (temp, src);
10720 mips_emit_move (dest, temp);
10721 }
10722 if (MEM_P (dest))
10723 mips_set_frame_expr (mips_frame_set (dest, src));
10724 }
10725 \f
10726 /* If we're generating n32 or n64 abicalls, and the current function
10727 does not use $28 as its global pointer, emit a cplocal directive.
10728 Use pic_offset_table_rtx as the argument to the directive. */
10729
10730 static void
10731 mips_output_cplocal (void)
10732 {
10733 if (!TARGET_EXPLICIT_RELOCS
10734 && mips_must_initialize_gp_p ()
10735 && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
10736 output_asm_insn (".cplocal %+", 0);
10737 }
10738
10739 /* Implement TARGET_OUTPUT_FUNCTION_PROLOGUE. */
10740
10741 static void
10742 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10743 {
10744 const char *fnname;
10745
10746 /* In MIPS16 mode, we may need to generate a non-MIPS16 stub to handle
10747 floating-point arguments. */
10748 if (TARGET_MIPS16
10749 && TARGET_HARD_FLOAT_ABI
10750 && crtl->args.info.fp_code != 0)
10751 mips16_build_function_stub ();
10752
10753 /* Get the function name the same way that toplev.c does before calling
10754 assemble_start_function. This is needed so that the name used here
10755 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
10756 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10757 mips_start_function_definition (fnname, TARGET_MIPS16);
10758
10759 /* Output MIPS-specific frame information. */
10760 if (!flag_inhibit_size_directive)
10761 {
10762 const struct mips_frame_info *frame;
10763
10764 frame = &cfun->machine->frame;
10765
10766 /* .frame FRAMEREG, FRAMESIZE, RETREG. */
10767 fprintf (file,
10768 "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
10769 "# vars= " HOST_WIDE_INT_PRINT_DEC
10770 ", regs= %d/%d"
10771 ", args= " HOST_WIDE_INT_PRINT_DEC
10772 ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
10773 reg_names[frame_pointer_needed
10774 ? HARD_FRAME_POINTER_REGNUM
10775 : STACK_POINTER_REGNUM],
10776 (frame_pointer_needed
10777 ? frame->total_size - frame->hard_frame_pointer_offset
10778 : frame->total_size),
10779 reg_names[RETURN_ADDR_REGNUM],
10780 frame->var_size,
10781 frame->num_gp, frame->num_fp,
10782 frame->args_size,
10783 frame->cprestore_size);
10784
10785 /* .mask MASK, OFFSET. */
10786 fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10787 frame->mask, frame->gp_save_offset);
10788
10789 /* .fmask MASK, OFFSET. */
10790 fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
10791 frame->fmask, frame->fp_save_offset);
10792 }
10793
10794 /* Handle the initialization of $gp for SVR4 PIC, if applicable.
10795 Also emit the ".set noreorder; .set nomacro" sequence for functions
10796 that need it. */
10797 if (mips_must_initialize_gp_p ()
10798 && mips_current_loadgp_style () == LOADGP_OLDABI)
10799 {
10800 if (TARGET_MIPS16)
10801 {
10802 /* This is a fixed-form sequence. The position of the
10803 first two instructions is important because of the
10804 way _gp_disp is defined. */
10805 output_asm_insn ("li\t$2,%%hi(_gp_disp)", 0);
10806 output_asm_insn ("addiu\t$3,$pc,%%lo(_gp_disp)", 0);
10807 output_asm_insn ("sll\t$2,16", 0);
10808 output_asm_insn ("addu\t$2,$3", 0);
10809 }
10810 else
10811 {
10812 /* .cpload must be in a .set noreorder but not a
10813 .set nomacro block. */
10814 mips_push_asm_switch (&mips_noreorder);
10815 output_asm_insn (".cpload\t%^", 0);
10816 if (!cfun->machine->all_noreorder_p)
10817 mips_pop_asm_switch (&mips_noreorder);
10818 else
10819 mips_push_asm_switch (&mips_nomacro);
10820 }
10821 }
10822 else if (cfun->machine->all_noreorder_p)
10823 {
10824 mips_push_asm_switch (&mips_noreorder);
10825 mips_push_asm_switch (&mips_nomacro);
10826 }
10827
10828 /* Tell the assembler which register we're using as the global
10829 pointer. This is needed for thunks, since they can use either
10830 explicit relocs or assembler macros. */
10831 mips_output_cplocal ();
10832 }
10833
10834 /* Implement TARGET_OUTPUT_FUNCTION_EPILOGUE. */
10835
10836 static void
10837 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10838 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10839 {
10840 const char *fnname;
10841
10842 /* Reinstate the normal $gp. */
10843 SET_REGNO (pic_offset_table_rtx, GLOBAL_POINTER_REGNUM);
10844 mips_output_cplocal ();
10845
10846 if (cfun->machine->all_noreorder_p)
10847 {
10848 mips_pop_asm_switch (&mips_nomacro);
10849 mips_pop_asm_switch (&mips_noreorder);
10850 }
10851
10852 /* Get the function name the same way that toplev.c does before calling
10853 assemble_start_function. This is needed so that the name used here
10854 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
10855 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
10856 mips_end_function_definition (fnname);
10857 }
10858 \f
10859 /* Emit an optimisation barrier for accesses to the current frame. */
10860
10861 static void
10862 mips_frame_barrier (void)
10863 {
10864 emit_clobber (gen_frame_mem (BLKmode, stack_pointer_rtx));
10865 }
10866
10867
10868 /* The __gnu_local_gp symbol. */
10869
10870 static GTY(()) rtx mips_gnu_local_gp;
10871
10872 /* If we're generating n32 or n64 abicalls, emit instructions
10873 to set up the global pointer. */
10874
10875 static void
10876 mips_emit_loadgp (void)
10877 {
10878 rtx addr, offset, incoming_address, base, index, pic_reg;
10879
10880 pic_reg = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
10881 switch (mips_current_loadgp_style ())
10882 {
10883 case LOADGP_ABSOLUTE:
10884 if (mips_gnu_local_gp == NULL)
10885 {
10886 mips_gnu_local_gp = gen_rtx_SYMBOL_REF (Pmode, "__gnu_local_gp");
10887 SYMBOL_REF_FLAGS (mips_gnu_local_gp) |= SYMBOL_FLAG_LOCAL;
10888 }
10889 emit_insn (PMODE_INSN (gen_loadgp_absolute,
10890 (pic_reg, mips_gnu_local_gp)));
10891 break;
10892
10893 case LOADGP_OLDABI:
10894 /* Added by mips_output_function_prologue. */
10895 break;
10896
10897 case LOADGP_NEWABI:
10898 addr = XEXP (DECL_RTL (current_function_decl), 0);
10899 offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
10900 incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
10901 emit_insn (PMODE_INSN (gen_loadgp_newabi,
10902 (pic_reg, offset, incoming_address)));
10903 break;
10904
10905 case LOADGP_RTP:
10906 base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_BASE));
10907 index = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (VXWORKS_GOTT_INDEX));
10908 emit_insn (PMODE_INSN (gen_loadgp_rtp, (pic_reg, base, index)));
10909 break;
10910
10911 default:
10912 return;
10913 }
10914
10915 if (TARGET_MIPS16)
10916 emit_insn (PMODE_INSN (gen_copygp_mips16,
10917 (pic_offset_table_rtx, pic_reg)));
10918
10919 /* Emit a blockage if there are implicit uses of the GP register.
10920 This includes profiled functions, because FUNCTION_PROFILE uses
10921 a jal macro. */
10922 if (!TARGET_EXPLICIT_RELOCS || crtl->profile)
10923 emit_insn (gen_loadgp_blockage ());
10924 }
10925
10926 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
10927
10928 #if PROBE_INTERVAL > 32768
10929 #error Cannot use indexed addressing mode for stack probing
10930 #endif
10931
10932 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
10933 inclusive. These are offsets from the current stack pointer. */
10934
10935 static void
10936 mips_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
10937 {
10938 if (TARGET_MIPS16)
10939 sorry ("-fstack-check=specific not implemented for MIPS16");
10940
10941 /* See if we have a constant small number of probes to generate. If so,
10942 that's the easy case. */
10943 if (first + size <= 32768)
10944 {
10945 HOST_WIDE_INT i;
10946
10947 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
10948 it exceeds SIZE. If only one probe is needed, this will not
10949 generate any code. Then probe at FIRST + SIZE. */
10950 for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
10951 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10952 -(first + i)));
10953
10954 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
10955 -(first + size)));
10956 }
10957
10958 /* Otherwise, do the same as above, but in a loop. Note that we must be
10959 extra careful with variables wrapping around because we might be at
10960 the very top (or the very bottom) of the address space and we have
10961 to be able to handle this case properly; in particular, we use an
10962 equality test for the loop condition. */
10963 else
10964 {
10965 HOST_WIDE_INT rounded_size;
10966 rtx r3 = MIPS_PROLOGUE_TEMP (Pmode);
10967 rtx r12 = MIPS_PROLOGUE_TEMP2 (Pmode);
10968
10969 /* Sanity check for the addressing mode we're going to use. */
10970 gcc_assert (first <= 32768);
10971
10972
10973 /* Step 1: round SIZE to the previous multiple of the interval. */
10974
10975 rounded_size = size & -PROBE_INTERVAL;
10976
10977
10978 /* Step 2: compute initial and final value of the loop counter. */
10979
10980 /* TEST_ADDR = SP + FIRST. */
10981 emit_insn (gen_rtx_SET (VOIDmode, r3,
10982 plus_constant (Pmode, stack_pointer_rtx,
10983 -first)));
10984
10985 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
10986 if (rounded_size > 32768)
10987 {
10988 emit_move_insn (r12, GEN_INT (rounded_size));
10989 emit_insn (gen_rtx_SET (VOIDmode, r12,
10990 gen_rtx_MINUS (Pmode, r3, r12)));
10991 }
10992 else
10993 emit_insn (gen_rtx_SET (VOIDmode, r12,
10994 plus_constant (Pmode, r3, -rounded_size)));
10995
10996
10997 /* Step 3: the loop
10998
10999 while (TEST_ADDR != LAST_ADDR)
11000 {
11001 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
11002 probe at TEST_ADDR
11003 }
11004
11005 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
11006 until it is equal to ROUNDED_SIZE. */
11007
11008 emit_insn (PMODE_INSN (gen_probe_stack_range, (r3, r3, r12)));
11009
11010
11011 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
11012 that SIZE is equal to ROUNDED_SIZE. */
11013
11014 if (size != rounded_size)
11015 emit_stack_probe (plus_constant (Pmode, r12, rounded_size - size));
11016 }
11017
11018 /* Make sure nothing is scheduled before we are done. */
11019 emit_insn (gen_blockage ());
11020 }
11021
11022 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
11023 absolute addresses. */
11024
11025 const char *
11026 mips_output_probe_stack_range (rtx reg1, rtx reg2)
11027 {
11028 static int labelno = 0;
11029 char loop_lab[32], end_lab[32], tmp[64];
11030 rtx xops[2];
11031
11032 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
11033 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
11034
11035 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
11036
11037 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
11038 xops[0] = reg1;
11039 xops[1] = reg2;
11040 strcpy (tmp, "%(%<beq\t%0,%1,");
11041 output_asm_insn (strcat (tmp, &end_lab[1]), xops);
11042
11043 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
11044 xops[1] = GEN_INT (-PROBE_INTERVAL);
11045 if (TARGET_64BIT && TARGET_LONG64)
11046 output_asm_insn ("daddiu\t%0,%0,%1", xops);
11047 else
11048 output_asm_insn ("addiu\t%0,%0,%1", xops);
11049
11050 /* Probe at TEST_ADDR and branch. */
11051 fprintf (asm_out_file, "\tb\t");
11052 assemble_name_raw (asm_out_file, loop_lab);
11053 fputc ('\n', asm_out_file);
11054 if (TARGET_64BIT)
11055 output_asm_insn ("sd\t$0,0(%0)%)", xops);
11056 else
11057 output_asm_insn ("sw\t$0,0(%0)%)", xops);
11058
11059 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
11060
11061 return "";
11062 }
11063
11064 /* Return true if X contains a kernel register. */
11065
11066 static bool
11067 mips_refers_to_kernel_reg_p (const_rtx x)
11068 {
11069 subrtx_iterator::array_type array;
11070 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
11071 if (REG_P (*iter) && KERNEL_REG_P (REGNO (*iter)))
11072 return true;
11073 return false;
11074 }
11075
11076 /* Expand the "prologue" pattern. */
11077
11078 void
11079 mips_expand_prologue (void)
11080 {
11081 const struct mips_frame_info *frame;
11082 HOST_WIDE_INT size;
11083 unsigned int nargs;
11084
11085 if (cfun->machine->global_pointer != INVALID_REGNUM)
11086 {
11087 /* Check whether an insn uses pic_offset_table_rtx, either explicitly
11088 or implicitly. If so, we can commit to using a global pointer
11089 straight away, otherwise we need to defer the decision. */
11090 if (mips_cfun_has_inflexible_gp_ref_p ()
11091 || mips_cfun_has_flexible_gp_ref_p ())
11092 {
11093 cfun->machine->must_initialize_gp_p = true;
11094 cfun->machine->must_restore_gp_when_clobbered_p = true;
11095 }
11096
11097 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
11098 }
11099
11100 frame = &cfun->machine->frame;
11101 size = frame->total_size;
11102
11103 if (flag_stack_usage_info)
11104 current_function_static_stack_size = size;
11105
11106 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
11107 {
11108 if (crtl->is_leaf && !cfun->calls_alloca)
11109 {
11110 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
11111 mips_emit_probe_stack_range (STACK_CHECK_PROTECT,
11112 size - STACK_CHECK_PROTECT);
11113 }
11114 else if (size > 0)
11115 mips_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
11116 }
11117
11118 /* Save the registers. Allocate up to MIPS_MAX_FIRST_STACK_STEP
11119 bytes beforehand; this is enough to cover the register save area
11120 without going out of range. */
11121 if (((frame->mask | frame->fmask | frame->acc_mask) != 0)
11122 || frame->num_cop0_regs > 0)
11123 {
11124 HOST_WIDE_INT step1;
11125
11126 step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
11127 if (GENERATE_MIPS16E_SAVE_RESTORE)
11128 {
11129 HOST_WIDE_INT offset;
11130 unsigned int mask, regno;
11131
11132 /* Try to merge argument stores into the save instruction. */
11133 nargs = mips16e_collect_argument_saves ();
11134
11135 /* Build the save instruction. */
11136 mask = frame->mask;
11137 rtx insn = mips16e_build_save_restore (false, &mask, &offset,
11138 nargs, step1);
11139 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11140 mips_frame_barrier ();
11141 size -= step1;
11142
11143 /* Check if we need to save other registers. */
11144 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11145 if (BITSET_P (mask, regno - GP_REG_FIRST))
11146 {
11147 offset -= UNITS_PER_WORD;
11148 mips_save_restore_reg (word_mode, regno,
11149 offset, mips_save_reg);
11150 }
11151 }
11152 else
11153 {
11154 if (cfun->machine->interrupt_handler_p)
11155 {
11156 HOST_WIDE_INT offset;
11157 rtx mem;
11158
11159 /* If this interrupt is using a shadow register set, we need to
11160 get the stack pointer from the previous register set. */
11161 if (cfun->machine->use_shadow_register_set_p)
11162 emit_insn (gen_mips_rdpgpr (stack_pointer_rtx,
11163 stack_pointer_rtx));
11164
11165 if (!cfun->machine->keep_interrupts_masked_p)
11166 {
11167 /* Move from COP0 Cause to K0. */
11168 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K0_REG_NUM),
11169 gen_rtx_REG (SImode,
11170 COP0_CAUSE_REG_NUM)));
11171 /* Move from COP0 EPC to K1. */
11172 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11173 gen_rtx_REG (SImode,
11174 COP0_EPC_REG_NUM)));
11175 }
11176
11177 /* Allocate the first part of the frame. */
11178 rtx insn = gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
11179 GEN_INT (-step1));
11180 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11181 mips_frame_barrier ();
11182 size -= step1;
11183
11184 /* Start at the uppermost location for saving. */
11185 offset = frame->cop0_sp_offset - size;
11186 if (!cfun->machine->keep_interrupts_masked_p)
11187 {
11188 /* Push EPC into its stack slot. */
11189 mem = gen_frame_mem (word_mode,
11190 plus_constant (Pmode, stack_pointer_rtx,
11191 offset));
11192 mips_emit_move (mem, gen_rtx_REG (word_mode, K1_REG_NUM));
11193 offset -= UNITS_PER_WORD;
11194 }
11195
11196 /* Move from COP0 Status to K1. */
11197 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, K1_REG_NUM),
11198 gen_rtx_REG (SImode,
11199 COP0_STATUS_REG_NUM)));
11200
11201 /* Right justify the RIPL in k0. */
11202 if (!cfun->machine->keep_interrupts_masked_p)
11203 emit_insn (gen_lshrsi3 (gen_rtx_REG (SImode, K0_REG_NUM),
11204 gen_rtx_REG (SImode, K0_REG_NUM),
11205 GEN_INT (CAUSE_IPL)));
11206
11207 /* Push Status 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 /* Insert the RIPL into our copy of SR (k1) as the new IPL. */
11215 if (!cfun->machine->keep_interrupts_masked_p)
11216 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11217 GEN_INT (6),
11218 GEN_INT (SR_IPL),
11219 gen_rtx_REG (SImode, K0_REG_NUM)));
11220
11221 if (!cfun->machine->keep_interrupts_masked_p)
11222 /* Enable interrupts by clearing the KSU ERL and EXL bits.
11223 IE is already the correct value, so we don't have to do
11224 anything explicit. */
11225 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11226 GEN_INT (4),
11227 GEN_INT (SR_EXL),
11228 gen_rtx_REG (SImode, GP_REG_FIRST)));
11229 else
11230 /* Disable interrupts by clearing the KSU, ERL, EXL,
11231 and IE bits. */
11232 emit_insn (gen_insvsi (gen_rtx_REG (SImode, K1_REG_NUM),
11233 GEN_INT (5),
11234 GEN_INT (SR_IE),
11235 gen_rtx_REG (SImode, GP_REG_FIRST)));
11236 }
11237 else
11238 {
11239 rtx insn = gen_add3_insn (stack_pointer_rtx,
11240 stack_pointer_rtx,
11241 GEN_INT (-step1));
11242 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11243 mips_frame_barrier ();
11244 size -= step1;
11245 }
11246 mips_for_each_saved_acc (size, mips_save_reg);
11247 mips_for_each_saved_gpr_and_fpr (size, mips_save_reg);
11248 }
11249 }
11250
11251 /* Allocate the rest of the frame. */
11252 if (size > 0)
11253 {
11254 if (SMALL_OPERAND (-size))
11255 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
11256 stack_pointer_rtx,
11257 GEN_INT (-size)))) = 1;
11258 else
11259 {
11260 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
11261 if (TARGET_MIPS16)
11262 {
11263 /* There are no instructions to add or subtract registers
11264 from the stack pointer, so use the frame pointer as a
11265 temporary. We should always be using a frame pointer
11266 in this case anyway. */
11267 gcc_assert (frame_pointer_needed);
11268 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11269 emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
11270 hard_frame_pointer_rtx,
11271 MIPS_PROLOGUE_TEMP (Pmode)));
11272 mips_emit_move (stack_pointer_rtx, hard_frame_pointer_rtx);
11273 }
11274 else
11275 emit_insn (gen_sub3_insn (stack_pointer_rtx,
11276 stack_pointer_rtx,
11277 MIPS_PROLOGUE_TEMP (Pmode)));
11278
11279 /* Describe the combined effect of the previous instructions. */
11280 mips_set_frame_expr
11281 (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
11282 plus_constant (Pmode, stack_pointer_rtx, -size)));
11283 }
11284 mips_frame_barrier ();
11285 }
11286
11287 /* Set up the frame pointer, if we're using one. */
11288 if (frame_pointer_needed)
11289 {
11290 HOST_WIDE_INT offset;
11291
11292 offset = frame->hard_frame_pointer_offset;
11293 if (offset == 0)
11294 {
11295 rtx insn = mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11296 RTX_FRAME_RELATED_P (insn) = 1;
11297 }
11298 else if (SMALL_OPERAND (offset))
11299 {
11300 rtx insn = gen_add3_insn (hard_frame_pointer_rtx,
11301 stack_pointer_rtx, GEN_INT (offset));
11302 RTX_FRAME_RELATED_P (emit_insn (insn)) = 1;
11303 }
11304 else
11305 {
11306 mips_emit_move (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (offset));
11307 mips_emit_move (hard_frame_pointer_rtx, stack_pointer_rtx);
11308 emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
11309 hard_frame_pointer_rtx,
11310 MIPS_PROLOGUE_TEMP (Pmode)));
11311 mips_set_frame_expr
11312 (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
11313 plus_constant (Pmode, stack_pointer_rtx, offset)));
11314 }
11315 }
11316
11317 mips_emit_loadgp ();
11318
11319 /* Initialize the $gp save slot. */
11320 if (mips_cfun_has_cprestore_slot_p ())
11321 {
11322 rtx base, mem, gp, temp;
11323 HOST_WIDE_INT offset;
11324
11325 mips_get_cprestore_base_and_offset (&base, &offset, false);
11326 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11327 gp = TARGET_MIPS16 ? MIPS16_PIC_TEMP : pic_offset_table_rtx;
11328 temp = (SMALL_OPERAND (offset)
11329 ? gen_rtx_SCRATCH (Pmode)
11330 : MIPS_PROLOGUE_TEMP (Pmode));
11331 emit_insn (PMODE_INSN (gen_potential_cprestore,
11332 (mem, GEN_INT (offset), gp, temp)));
11333
11334 mips_get_cprestore_base_and_offset (&base, &offset, true);
11335 mem = gen_frame_mem (Pmode, plus_constant (Pmode, base, offset));
11336 emit_insn (PMODE_INSN (gen_use_cprestore, (mem)));
11337 }
11338
11339 /* We need to search back to the last use of K0 or K1. */
11340 if (cfun->machine->interrupt_handler_p)
11341 {
11342 rtx_insn *insn;
11343 for (insn = get_last_insn (); insn != NULL_RTX; insn = PREV_INSN (insn))
11344 if (INSN_P (insn)
11345 && mips_refers_to_kernel_reg_p (PATTERN (insn)))
11346 break;
11347 /* Emit a move from K1 to COP0 Status after insn. */
11348 gcc_assert (insn != NULL_RTX);
11349 emit_insn_after (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11350 gen_rtx_REG (SImode, K1_REG_NUM)),
11351 insn);
11352 }
11353
11354 /* If we are profiling, make sure no instructions are scheduled before
11355 the call to mcount. */
11356 if (crtl->profile)
11357 emit_insn (gen_blockage ());
11358 }
11359 \f
11360 /* Attach all pending register saves to the previous instruction.
11361 Return that instruction. */
11362
11363 static rtx_insn *
11364 mips_epilogue_emit_cfa_restores (void)
11365 {
11366 rtx_insn *insn;
11367
11368 insn = get_last_insn ();
11369 gcc_assert (insn && !REG_NOTES (insn));
11370 if (mips_epilogue.cfa_restores)
11371 {
11372 RTX_FRAME_RELATED_P (insn) = 1;
11373 REG_NOTES (insn) = mips_epilogue.cfa_restores;
11374 mips_epilogue.cfa_restores = 0;
11375 }
11376 return insn;
11377 }
11378
11379 /* Like mips_epilogue_emit_cfa_restores, but also record that the CFA is
11380 now at REG + OFFSET. */
11381
11382 static void
11383 mips_epilogue_set_cfa (rtx reg, HOST_WIDE_INT offset)
11384 {
11385 rtx_insn *insn;
11386
11387 insn = mips_epilogue_emit_cfa_restores ();
11388 if (reg != mips_epilogue.cfa_reg || offset != mips_epilogue.cfa_offset)
11389 {
11390 RTX_FRAME_RELATED_P (insn) = 1;
11391 REG_NOTES (insn) = alloc_reg_note (REG_CFA_DEF_CFA,
11392 plus_constant (Pmode, reg, offset),
11393 REG_NOTES (insn));
11394 mips_epilogue.cfa_reg = reg;
11395 mips_epilogue.cfa_offset = offset;
11396 }
11397 }
11398
11399 /* Emit instructions to restore register REG from slot MEM. Also update
11400 the cfa_restores list. */
11401
11402 static void
11403 mips_restore_reg (rtx reg, rtx mem)
11404 {
11405 /* There's no MIPS16 instruction to load $31 directly. Load into
11406 $7 instead and adjust the return insn appropriately. */
11407 if (TARGET_MIPS16 && REGNO (reg) == RETURN_ADDR_REGNUM)
11408 reg = gen_rtx_REG (GET_MODE (reg), GP_REG_FIRST + 7);
11409 else if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
11410 {
11411 mips_add_cfa_restore (mips_subword (reg, true));
11412 mips_add_cfa_restore (mips_subword (reg, false));
11413 }
11414 else
11415 mips_add_cfa_restore (reg);
11416
11417 mips_emit_save_slot_move (reg, mem, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
11418 if (REGNO (reg) == REGNO (mips_epilogue.cfa_reg))
11419 /* The CFA is currently defined in terms of the register whose
11420 value we have just restored. Redefine the CFA in terms of
11421 the stack pointer. */
11422 mips_epilogue_set_cfa (stack_pointer_rtx,
11423 mips_epilogue.cfa_restore_sp_offset);
11424 }
11425
11426 /* Emit code to set the stack pointer to BASE + OFFSET, given that
11427 BASE + OFFSET is NEW_FRAME_SIZE bytes below the top of the frame.
11428 BASE, if not the stack pointer, is available as a temporary. */
11429
11430 static void
11431 mips_deallocate_stack (rtx base, rtx offset, HOST_WIDE_INT new_frame_size)
11432 {
11433 if (base == stack_pointer_rtx && offset == const0_rtx)
11434 return;
11435
11436 mips_frame_barrier ();
11437 if (offset == const0_rtx)
11438 {
11439 emit_move_insn (stack_pointer_rtx, base);
11440 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11441 }
11442 else if (TARGET_MIPS16 && base != stack_pointer_rtx)
11443 {
11444 emit_insn (gen_add3_insn (base, base, offset));
11445 mips_epilogue_set_cfa (base, new_frame_size);
11446 emit_move_insn (stack_pointer_rtx, base);
11447 }
11448 else
11449 {
11450 emit_insn (gen_add3_insn (stack_pointer_rtx, base, offset));
11451 mips_epilogue_set_cfa (stack_pointer_rtx, new_frame_size);
11452 }
11453 }
11454
11455 /* Emit any instructions needed before a return. */
11456
11457 void
11458 mips_expand_before_return (void)
11459 {
11460 /* When using a call-clobbered gp, we start out with unified call
11461 insns that include instructions to restore the gp. We then split
11462 these unified calls after reload. These split calls explicitly
11463 clobber gp, so there is no need to define
11464 PIC_OFFSET_TABLE_REG_CALL_CLOBBERED.
11465
11466 For consistency, we should also insert an explicit clobber of $28
11467 before return insns, so that the post-reload optimizers know that
11468 the register is not live on exit. */
11469 if (TARGET_CALL_CLOBBERED_GP)
11470 emit_clobber (pic_offset_table_rtx);
11471 }
11472
11473 /* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P
11474 says which. */
11475
11476 void
11477 mips_expand_epilogue (bool sibcall_p)
11478 {
11479 const struct mips_frame_info *frame;
11480 HOST_WIDE_INT step1, step2;
11481 rtx base, adjust;
11482 rtx_insn *insn;
11483 bool use_jraddiusp_p = false;
11484
11485 if (!sibcall_p && mips_can_use_return_insn ())
11486 {
11487 emit_jump_insn (gen_return ());
11488 return;
11489 }
11490
11491 /* In MIPS16 mode, if the return value should go into a floating-point
11492 register, we need to call a helper routine to copy it over. */
11493 if (mips16_cfun_returns_in_fpr_p ())
11494 mips16_copy_fpr_return_value ();
11495
11496 /* Split the frame into two. STEP1 is the amount of stack we should
11497 deallocate before restoring the registers. STEP2 is the amount we
11498 should deallocate afterwards.
11499
11500 Start off by assuming that no registers need to be restored. */
11501 frame = &cfun->machine->frame;
11502 step1 = frame->total_size;
11503 step2 = 0;
11504
11505 /* Work out which register holds the frame address. */
11506 if (!frame_pointer_needed)
11507 base = stack_pointer_rtx;
11508 else
11509 {
11510 base = hard_frame_pointer_rtx;
11511 step1 -= frame->hard_frame_pointer_offset;
11512 }
11513 mips_epilogue.cfa_reg = base;
11514 mips_epilogue.cfa_offset = step1;
11515 mips_epilogue.cfa_restores = NULL_RTX;
11516
11517 /* If we need to restore registers, deallocate as much stack as
11518 possible in the second step without going out of range. */
11519 if ((frame->mask | frame->fmask | frame->acc_mask) != 0
11520 || frame->num_cop0_regs > 0)
11521 {
11522 step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
11523 step1 -= step2;
11524 }
11525
11526 /* Get an rtx for STEP1 that we can add to BASE. */
11527 adjust = GEN_INT (step1);
11528 if (!SMALL_OPERAND (step1))
11529 {
11530 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), adjust);
11531 adjust = MIPS_EPILOGUE_TEMP (Pmode);
11532 }
11533 mips_deallocate_stack (base, adjust, step2);
11534
11535 /* If we're using addressing macros, $gp is implicitly used by all
11536 SYMBOL_REFs. We must emit a blockage insn before restoring $gp
11537 from the stack. */
11538 if (TARGET_CALL_SAVED_GP && !TARGET_EXPLICIT_RELOCS)
11539 emit_insn (gen_blockage ());
11540
11541 mips_epilogue.cfa_restore_sp_offset = step2;
11542 if (GENERATE_MIPS16E_SAVE_RESTORE && frame->mask != 0)
11543 {
11544 unsigned int regno, mask;
11545 HOST_WIDE_INT offset;
11546 rtx restore;
11547
11548 /* Generate the restore instruction. */
11549 mask = frame->mask;
11550 restore = mips16e_build_save_restore (true, &mask, &offset, 0, step2);
11551
11552 /* Restore any other registers manually. */
11553 for (regno = GP_REG_FIRST; regno < GP_REG_LAST; regno++)
11554 if (BITSET_P (mask, regno - GP_REG_FIRST))
11555 {
11556 offset -= UNITS_PER_WORD;
11557 mips_save_restore_reg (word_mode, regno, offset, mips_restore_reg);
11558 }
11559
11560 /* Restore the remaining registers and deallocate the final bit
11561 of the frame. */
11562 mips_frame_barrier ();
11563 emit_insn (restore);
11564 mips_epilogue_set_cfa (stack_pointer_rtx, 0);
11565 }
11566 else
11567 {
11568 /* Restore the registers. */
11569 mips_for_each_saved_acc (frame->total_size - step2, mips_restore_reg);
11570 mips_for_each_saved_gpr_and_fpr (frame->total_size - step2,
11571 mips_restore_reg);
11572
11573 if (cfun->machine->interrupt_handler_p)
11574 {
11575 HOST_WIDE_INT offset;
11576 rtx mem;
11577
11578 offset = frame->cop0_sp_offset - (frame->total_size - step2);
11579 if (!cfun->machine->keep_interrupts_masked_p)
11580 {
11581 /* Restore the original EPC. */
11582 mem = gen_frame_mem (word_mode,
11583 plus_constant (Pmode, stack_pointer_rtx,
11584 offset));
11585 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11586 offset -= UNITS_PER_WORD;
11587
11588 /* Move to COP0 EPC. */
11589 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_EPC_REG_NUM),
11590 gen_rtx_REG (SImode, K0_REG_NUM)));
11591 }
11592
11593 /* Restore the original Status. */
11594 mem = gen_frame_mem (word_mode,
11595 plus_constant (Pmode, stack_pointer_rtx,
11596 offset));
11597 mips_emit_move (gen_rtx_REG (word_mode, K0_REG_NUM), mem);
11598 offset -= UNITS_PER_WORD;
11599
11600 /* If we don't use shadow register set, we need to update SP. */
11601 if (!cfun->machine->use_shadow_register_set_p)
11602 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11603 else
11604 /* The choice of position is somewhat arbitrary in this case. */
11605 mips_epilogue_emit_cfa_restores ();
11606
11607 /* Move to COP0 Status. */
11608 emit_insn (gen_cop0_move (gen_rtx_REG (SImode, COP0_STATUS_REG_NUM),
11609 gen_rtx_REG (SImode, K0_REG_NUM)));
11610 }
11611 else if (TARGET_MICROMIPS
11612 && !crtl->calls_eh_return
11613 && !sibcall_p
11614 && step2 > 0
11615 && mips_unsigned_immediate_p (step2, 5, 2))
11616 use_jraddiusp_p = true;
11617 else
11618 /* Deallocate the final bit of the frame. */
11619 mips_deallocate_stack (stack_pointer_rtx, GEN_INT (step2), 0);
11620 }
11621
11622 if (!use_jraddiusp_p)
11623 gcc_assert (!mips_epilogue.cfa_restores);
11624
11625 /* Add in the __builtin_eh_return stack adjustment. We need to
11626 use a temporary in MIPS16 code. */
11627 if (crtl->calls_eh_return)
11628 {
11629 if (TARGET_MIPS16)
11630 {
11631 mips_emit_move (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
11632 emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
11633 MIPS_EPILOGUE_TEMP (Pmode),
11634 EH_RETURN_STACKADJ_RTX));
11635 mips_emit_move (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
11636 }
11637 else
11638 emit_insn (gen_add3_insn (stack_pointer_rtx,
11639 stack_pointer_rtx,
11640 EH_RETURN_STACKADJ_RTX));
11641 }
11642
11643 if (!sibcall_p)
11644 {
11645 mips_expand_before_return ();
11646 if (cfun->machine->interrupt_handler_p)
11647 {
11648 /* Interrupt handlers generate eret or deret. */
11649 if (cfun->machine->use_debug_exception_return_p)
11650 emit_jump_insn (gen_mips_deret ());
11651 else
11652 emit_jump_insn (gen_mips_eret ());
11653 }
11654 else
11655 {
11656 rtx pat;
11657
11658 /* When generating MIPS16 code, the normal
11659 mips_for_each_saved_gpr_and_fpr path will restore the return
11660 address into $7 rather than $31. */
11661 if (TARGET_MIPS16
11662 && !GENERATE_MIPS16E_SAVE_RESTORE
11663 && BITSET_P (frame->mask, RETURN_ADDR_REGNUM))
11664 {
11665 /* simple_returns cannot rely on values that are only available
11666 on paths through the epilogue (because return paths that do
11667 not pass through the epilogue may nevertheless reuse a
11668 simple_return that occurs at the end of the epilogue).
11669 Use a normal return here instead. */
11670 rtx reg = gen_rtx_REG (Pmode, GP_REG_FIRST + 7);
11671 pat = gen_return_internal (reg);
11672 }
11673 else if (use_jraddiusp_p)
11674 pat = gen_jraddiusp (GEN_INT (step2));
11675 else
11676 {
11677 rtx reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
11678 pat = gen_simple_return_internal (reg);
11679 }
11680 emit_jump_insn (pat);
11681 if (use_jraddiusp_p)
11682 mips_epilogue_set_cfa (stack_pointer_rtx, step2);
11683 }
11684 }
11685
11686 /* Search from the beginning to the first use of K0 or K1. */
11687 if (cfun->machine->interrupt_handler_p
11688 && !cfun->machine->keep_interrupts_masked_p)
11689 {
11690 for (insn = get_insns (); insn != NULL_RTX; insn = NEXT_INSN (insn))
11691 if (INSN_P (insn)
11692 && mips_refers_to_kernel_reg_p (PATTERN (insn)))
11693 break;
11694 gcc_assert (insn != NULL_RTX);
11695 /* Insert disable interrupts before the first use of K0 or K1. */
11696 emit_insn_before (gen_mips_di (), insn);
11697 emit_insn_before (gen_mips_ehb (), insn);
11698 }
11699 }
11700 \f
11701 /* Return nonzero if this function is known to have a null epilogue.
11702 This allows the optimizer to omit jumps to jumps if no stack
11703 was created. */
11704
11705 bool
11706 mips_can_use_return_insn (void)
11707 {
11708 /* Interrupt handlers need to go through the epilogue. */
11709 if (cfun->machine->interrupt_handler_p)
11710 return false;
11711
11712 if (!reload_completed)
11713 return false;
11714
11715 if (crtl->profile)
11716 return false;
11717
11718 /* In MIPS16 mode, a function that returns a floating-point value
11719 needs to arrange to copy the return value into the floating-point
11720 registers. */
11721 if (mips16_cfun_returns_in_fpr_p ())
11722 return false;
11723
11724 return cfun->machine->frame.total_size == 0;
11725 }
11726 \f
11727 /* Return true if register REGNO can store a value of mode MODE.
11728 The result of this function is cached in mips_hard_regno_mode_ok. */
11729
11730 static bool
11731 mips_hard_regno_mode_ok_p (unsigned int regno, machine_mode mode)
11732 {
11733 unsigned int size;
11734 enum mode_class mclass;
11735
11736 if (mode == CCV2mode)
11737 return (ISA_HAS_8CC
11738 && ST_REG_P (regno)
11739 && (regno - ST_REG_FIRST) % 2 == 0);
11740
11741 if (mode == CCV4mode)
11742 return (ISA_HAS_8CC
11743 && ST_REG_P (regno)
11744 && (regno - ST_REG_FIRST) % 4 == 0);
11745
11746 if (mode == CCmode)
11747 return ISA_HAS_8CC ? ST_REG_P (regno) : regno == FPSW_REGNUM;
11748
11749 size = GET_MODE_SIZE (mode);
11750 mclass = GET_MODE_CLASS (mode);
11751
11752 if (GP_REG_P (regno))
11753 return ((regno - GP_REG_FIRST) & 1) == 0 || size <= UNITS_PER_WORD;
11754
11755 if (FP_REG_P (regno)
11756 && (((regno - FP_REG_FIRST) % MAX_FPRS_PER_FMT) == 0
11757 || (MIN_FPRS_PER_FMT == 1 && size <= UNITS_PER_FPREG)))
11758 {
11759 /* Allow 64-bit vector modes for Loongson-2E/2F. */
11760 if (TARGET_LOONGSON_VECTORS
11761 && (mode == V2SImode
11762 || mode == V4HImode
11763 || mode == V8QImode
11764 || mode == DImode))
11765 return true;
11766
11767 if (mclass == MODE_FLOAT
11768 || mclass == MODE_COMPLEX_FLOAT
11769 || mclass == MODE_VECTOR_FLOAT)
11770 return size <= UNITS_PER_FPVALUE;
11771
11772 /* Allow integer modes that fit into a single register. We need
11773 to put integers into FPRs when using instructions like CVT
11774 and TRUNC. There's no point allowing sizes smaller than a word,
11775 because the FPU has no appropriate load/store instructions. */
11776 if (mclass == MODE_INT)
11777 return size >= MIN_UNITS_PER_WORD && size <= UNITS_PER_FPREG;
11778 }
11779
11780 if (ACC_REG_P (regno)
11781 && (INTEGRAL_MODE_P (mode) || ALL_FIXED_POINT_MODE_P (mode)))
11782 {
11783 if (MD_REG_P (regno))
11784 {
11785 /* After a multiplication or division, clobbering HI makes
11786 the value of LO unpredictable, and vice versa. This means
11787 that, for all interesting cases, HI and LO are effectively
11788 a single register.
11789
11790 We model this by requiring that any value that uses HI
11791 also uses LO. */
11792 if (size <= UNITS_PER_WORD * 2)
11793 return regno == (size <= UNITS_PER_WORD ? LO_REGNUM : MD_REG_FIRST);
11794 }
11795 else
11796 {
11797 /* DSP accumulators do not have the same restrictions as
11798 HI and LO, so we can treat them as normal doubleword
11799 registers. */
11800 if (size <= UNITS_PER_WORD)
11801 return true;
11802
11803 if (size <= UNITS_PER_WORD * 2
11804 && ((regno - DSP_ACC_REG_FIRST) & 1) == 0)
11805 return true;
11806 }
11807 }
11808
11809 if (ALL_COP_REG_P (regno))
11810 return mclass == MODE_INT && size <= UNITS_PER_WORD;
11811
11812 if (regno == GOT_VERSION_REGNUM)
11813 return mode == SImode;
11814
11815 return false;
11816 }
11817
11818 /* Implement HARD_REGNO_NREGS. */
11819
11820 unsigned int
11821 mips_hard_regno_nregs (int regno, machine_mode mode)
11822 {
11823 if (ST_REG_P (regno))
11824 /* The size of FP status registers is always 4, because they only hold
11825 CCmode values, and CCmode is always considered to be 4 bytes wide. */
11826 return (GET_MODE_SIZE (mode) + 3) / 4;
11827
11828 if (FP_REG_P (regno))
11829 return (GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG;
11830
11831 /* All other registers are word-sized. */
11832 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
11833 }
11834
11835 /* Implement CLASS_MAX_NREGS, taking the maximum of the cases
11836 in mips_hard_regno_nregs. */
11837
11838 int
11839 mips_class_max_nregs (enum reg_class rclass, machine_mode mode)
11840 {
11841 int size;
11842 HARD_REG_SET left;
11843
11844 size = 0x8000;
11845 COPY_HARD_REG_SET (left, reg_class_contents[(int) rclass]);
11846 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) ST_REGS]))
11847 {
11848 if (HARD_REGNO_MODE_OK (ST_REG_FIRST, mode))
11849 size = MIN (size, 4);
11850 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) ST_REGS]);
11851 }
11852 if (hard_reg_set_intersect_p (left, reg_class_contents[(int) FP_REGS]))
11853 {
11854 if (HARD_REGNO_MODE_OK (FP_REG_FIRST, mode))
11855 size = MIN (size, UNITS_PER_FPREG);
11856 AND_COMPL_HARD_REG_SET (left, reg_class_contents[(int) FP_REGS]);
11857 }
11858 if (!hard_reg_set_empty_p (left))
11859 size = MIN (size, UNITS_PER_WORD);
11860 return (GET_MODE_SIZE (mode) + size - 1) / size;
11861 }
11862
11863 /* Implement CANNOT_CHANGE_MODE_CLASS. */
11864
11865 bool
11866 mips_cannot_change_mode_class (machine_mode from,
11867 machine_mode to,
11868 enum reg_class rclass)
11869 {
11870 /* Allow conversions between different Loongson integer vectors,
11871 and between those vectors and DImode. */
11872 if (GET_MODE_SIZE (from) == 8 && GET_MODE_SIZE (to) == 8
11873 && INTEGRAL_MODE_P (from) && INTEGRAL_MODE_P (to))
11874 return false;
11875
11876 /* Otherwise, there are several problems with changing the modes of
11877 values in floating-point registers:
11878
11879 - When a multi-word value is stored in paired floating-point
11880 registers, the first register always holds the low word. We
11881 therefore can't allow FPRs to change between single-word and
11882 multi-word modes on big-endian targets.
11883
11884 - GCC assumes that each word of a multiword register can be
11885 accessed individually using SUBREGs. This is not true for
11886 floating-point registers if they are bigger than a word.
11887
11888 - Loading a 32-bit value into a 64-bit floating-point register
11889 will not sign-extend the value, despite what LOAD_EXTEND_OP
11890 says. We can't allow FPRs to change from SImode to a wider
11891 mode on 64-bit targets.
11892
11893 - If the FPU has already interpreted a value in one format, we
11894 must not ask it to treat the value as having a different
11895 format.
11896
11897 We therefore disallow all mode changes involving FPRs. */
11898
11899 return reg_classes_intersect_p (FP_REGS, rclass);
11900 }
11901
11902 /* Implement target hook small_register_classes_for_mode_p. */
11903
11904 static bool
11905 mips_small_register_classes_for_mode_p (machine_mode mode
11906 ATTRIBUTE_UNUSED)
11907 {
11908 return TARGET_MIPS16;
11909 }
11910
11911 /* Return true if moves in mode MODE can use the FPU's mov.fmt instruction. */
11912
11913 static bool
11914 mips_mode_ok_for_mov_fmt_p (machine_mode mode)
11915 {
11916 switch (mode)
11917 {
11918 case SFmode:
11919 return TARGET_HARD_FLOAT;
11920
11921 case DFmode:
11922 return TARGET_HARD_FLOAT && TARGET_DOUBLE_FLOAT;
11923
11924 case V2SFmode:
11925 return TARGET_HARD_FLOAT && TARGET_PAIRED_SINGLE_FLOAT;
11926
11927 default:
11928 return false;
11929 }
11930 }
11931
11932 /* Implement MODES_TIEABLE_P. */
11933
11934 bool
11935 mips_modes_tieable_p (machine_mode mode1, machine_mode mode2)
11936 {
11937 /* FPRs allow no mode punning, so it's not worth tying modes if we'd
11938 prefer to put one of them in FPRs. */
11939 return (mode1 == mode2
11940 || (!mips_mode_ok_for_mov_fmt_p (mode1)
11941 && !mips_mode_ok_for_mov_fmt_p (mode2)));
11942 }
11943
11944 /* Implement TARGET_PREFERRED_RELOAD_CLASS. */
11945
11946 static reg_class_t
11947 mips_preferred_reload_class (rtx x, reg_class_t rclass)
11948 {
11949 if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, rclass))
11950 return LEA_REGS;
11951
11952 if (reg_class_subset_p (FP_REGS, rclass)
11953 && mips_mode_ok_for_mov_fmt_p (GET_MODE (x)))
11954 return FP_REGS;
11955
11956 if (reg_class_subset_p (GR_REGS, rclass))
11957 rclass = GR_REGS;
11958
11959 if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, rclass))
11960 rclass = M16_REGS;
11961
11962 return rclass;
11963 }
11964
11965 /* RCLASS is a class involved in a REGISTER_MOVE_COST calculation.
11966 Return a "canonical" class to represent it in later calculations. */
11967
11968 static reg_class_t
11969 mips_canonicalize_move_class (reg_class_t rclass)
11970 {
11971 /* All moves involving accumulator registers have the same cost. */
11972 if (reg_class_subset_p (rclass, ACC_REGS))
11973 rclass = ACC_REGS;
11974
11975 /* Likewise promote subclasses of general registers to the most
11976 interesting containing class. */
11977 if (TARGET_MIPS16 && reg_class_subset_p (rclass, M16_REGS))
11978 rclass = M16_REGS;
11979 else if (reg_class_subset_p (rclass, GENERAL_REGS))
11980 rclass = GENERAL_REGS;
11981
11982 return rclass;
11983 }
11984
11985 /* Return the cost of moving a value from a register of class FROM to a GPR.
11986 Return 0 for classes that are unions of other classes handled by this
11987 function. */
11988
11989 static int
11990 mips_move_to_gpr_cost (reg_class_t from)
11991 {
11992 switch (from)
11993 {
11994 case M16_REGS:
11995 case GENERAL_REGS:
11996 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
11997 return 2;
11998
11999 case ACC_REGS:
12000 /* MFLO and MFHI. */
12001 return 6;
12002
12003 case FP_REGS:
12004 /* MFC1, etc. */
12005 return 4;
12006
12007 case COP0_REGS:
12008 case COP2_REGS:
12009 case COP3_REGS:
12010 /* This choice of value is historical. */
12011 return 5;
12012
12013 default:
12014 return 0;
12015 }
12016 }
12017
12018 /* Return the cost of moving a value from a GPR to a register of class TO.
12019 Return 0 for classes that are unions of other classes handled by this
12020 function. */
12021
12022 static int
12023 mips_move_from_gpr_cost (reg_class_t to)
12024 {
12025 switch (to)
12026 {
12027 case M16_REGS:
12028 case GENERAL_REGS:
12029 /* A MIPS16 MOVE instruction, or a non-MIPS16 MOVE macro. */
12030 return 2;
12031
12032 case ACC_REGS:
12033 /* MTLO and MTHI. */
12034 return 6;
12035
12036 case FP_REGS:
12037 /* MTC1, etc. */
12038 return 4;
12039
12040 case COP0_REGS:
12041 case COP2_REGS:
12042 case COP3_REGS:
12043 /* This choice of value is historical. */
12044 return 5;
12045
12046 default:
12047 return 0;
12048 }
12049 }
12050
12051 /* Implement TARGET_REGISTER_MOVE_COST. Return 0 for classes that are the
12052 maximum of the move costs for subclasses; regclass will work out
12053 the maximum for us. */
12054
12055 static int
12056 mips_register_move_cost (machine_mode mode,
12057 reg_class_t from, reg_class_t to)
12058 {
12059 reg_class_t dregs;
12060 int cost1, cost2;
12061
12062 from = mips_canonicalize_move_class (from);
12063 to = mips_canonicalize_move_class (to);
12064
12065 /* Handle moves that can be done without using general-purpose registers. */
12066 if (from == FP_REGS)
12067 {
12068 if (to == FP_REGS && mips_mode_ok_for_mov_fmt_p (mode))
12069 /* MOV.FMT. */
12070 return 4;
12071 }
12072
12073 /* Handle cases in which only one class deviates from the ideal. */
12074 dregs = TARGET_MIPS16 ? M16_REGS : GENERAL_REGS;
12075 if (from == dregs)
12076 return mips_move_from_gpr_cost (to);
12077 if (to == dregs)
12078 return mips_move_to_gpr_cost (from);
12079
12080 /* Handles cases that require a GPR temporary. */
12081 cost1 = mips_move_to_gpr_cost (from);
12082 if (cost1 != 0)
12083 {
12084 cost2 = mips_move_from_gpr_cost (to);
12085 if (cost2 != 0)
12086 return cost1 + cost2;
12087 }
12088
12089 return 0;
12090 }
12091
12092 /* Implement TARGET_REGISTER_PRIORITY. */
12093
12094 static int
12095 mips_register_priority (int hard_regno)
12096 {
12097 /* Treat MIPS16 registers with higher priority than other regs. */
12098 if (TARGET_MIPS16
12099 && TEST_HARD_REG_BIT (reg_class_contents[M16_REGS], hard_regno))
12100 return 1;
12101 return 0;
12102 }
12103
12104 /* Implement TARGET_MEMORY_MOVE_COST. */
12105
12106 static int
12107 mips_memory_move_cost (machine_mode mode, reg_class_t rclass, bool in)
12108 {
12109 return (mips_cost->memory_latency
12110 + memory_move_secondary_cost (mode, rclass, in));
12111 }
12112
12113 /* Return the register class required for a secondary register when
12114 copying between one of the registers in RCLASS and value X, which
12115 has mode MODE. X is the source of the move if IN_P, otherwise it
12116 is the destination. Return NO_REGS if no secondary register is
12117 needed. */
12118
12119 enum reg_class
12120 mips_secondary_reload_class (enum reg_class rclass,
12121 machine_mode mode, rtx x, bool)
12122 {
12123 int regno;
12124
12125 /* If X is a constant that cannot be loaded into $25, it must be loaded
12126 into some other GPR. No other register class allows a direct move. */
12127 if (mips_dangerous_for_la25_p (x))
12128 return reg_class_subset_p (rclass, LEA_REGS) ? NO_REGS : LEA_REGS;
12129
12130 regno = true_regnum (x);
12131 if (TARGET_MIPS16)
12132 {
12133 /* In MIPS16 mode, every move must involve a member of M16_REGS. */
12134 if (!reg_class_subset_p (rclass, M16_REGS) && !M16_REG_P (regno))
12135 return M16_REGS;
12136
12137 return NO_REGS;
12138 }
12139
12140 /* Copying from accumulator registers to anywhere other than a general
12141 register requires a temporary general register. */
12142 if (reg_class_subset_p (rclass, ACC_REGS))
12143 return GP_REG_P (regno) ? NO_REGS : GR_REGS;
12144 if (ACC_REG_P (regno))
12145 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12146
12147 if (reg_class_subset_p (rclass, FP_REGS))
12148 {
12149 if (regno < 0
12150 || (MEM_P (x)
12151 && (GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8)))
12152 /* In this case we can use lwc1, swc1, ldc1 or sdc1. We'll use
12153 pairs of lwc1s and swc1s if ldc1 and sdc1 are not supported. */
12154 return NO_REGS;
12155
12156 if (GP_REG_P (regno) || x == CONST0_RTX (mode))
12157 /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1. */
12158 return NO_REGS;
12159
12160 if (CONSTANT_P (x) && !targetm.cannot_force_const_mem (mode, x))
12161 /* We can force the constant to memory and use lwc1
12162 and ldc1. As above, we will use pairs of lwc1s if
12163 ldc1 is not supported. */
12164 return NO_REGS;
12165
12166 if (FP_REG_P (regno) && mips_mode_ok_for_mov_fmt_p (mode))
12167 /* In this case we can use mov.fmt. */
12168 return NO_REGS;
12169
12170 /* Otherwise, we need to reload through an integer register. */
12171 return GR_REGS;
12172 }
12173 if (FP_REG_P (regno))
12174 return reg_class_subset_p (rclass, GR_REGS) ? NO_REGS : GR_REGS;
12175
12176 return NO_REGS;
12177 }
12178
12179 /* Implement TARGET_MODE_REP_EXTENDED. */
12180
12181 static int
12182 mips_mode_rep_extended (machine_mode mode, machine_mode mode_rep)
12183 {
12184 /* On 64-bit targets, SImode register values are sign-extended to DImode. */
12185 if (TARGET_64BIT && mode == SImode && mode_rep == DImode)
12186 return SIGN_EXTEND;
12187
12188 return UNKNOWN;
12189 }
12190 \f
12191 /* Implement TARGET_VALID_POINTER_MODE. */
12192
12193 static bool
12194 mips_valid_pointer_mode (machine_mode mode)
12195 {
12196 return mode == SImode || (TARGET_64BIT && mode == DImode);
12197 }
12198
12199 /* Implement TARGET_VECTOR_MODE_SUPPORTED_P. */
12200
12201 static bool
12202 mips_vector_mode_supported_p (machine_mode mode)
12203 {
12204 switch (mode)
12205 {
12206 case V2SFmode:
12207 return TARGET_PAIRED_SINGLE_FLOAT;
12208
12209 case V2HImode:
12210 case V4QImode:
12211 case V2HQmode:
12212 case V2UHQmode:
12213 case V2HAmode:
12214 case V2UHAmode:
12215 case V4QQmode:
12216 case V4UQQmode:
12217 return TARGET_DSP;
12218
12219 case V2SImode:
12220 case V4HImode:
12221 case V8QImode:
12222 return TARGET_LOONGSON_VECTORS;
12223
12224 default:
12225 return false;
12226 }
12227 }
12228
12229 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P. */
12230
12231 static bool
12232 mips_scalar_mode_supported_p (machine_mode mode)
12233 {
12234 if (ALL_FIXED_POINT_MODE_P (mode)
12235 && GET_MODE_PRECISION (mode) <= 2 * BITS_PER_WORD)
12236 return true;
12237
12238 return default_scalar_mode_supported_p (mode);
12239 }
12240 \f
12241 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE. */
12242
12243 static machine_mode
12244 mips_preferred_simd_mode (machine_mode mode ATTRIBUTE_UNUSED)
12245 {
12246 if (TARGET_PAIRED_SINGLE_FLOAT
12247 && mode == SFmode)
12248 return V2SFmode;
12249 return word_mode;
12250 }
12251
12252 /* Implement TARGET_INIT_LIBFUNCS. */
12253
12254 static void
12255 mips_init_libfuncs (void)
12256 {
12257 if (TARGET_FIX_VR4120)
12258 {
12259 /* Register the special divsi3 and modsi3 functions needed to work
12260 around VR4120 division errata. */
12261 set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
12262 set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
12263 }
12264
12265 if (TARGET_MIPS16 && TARGET_HARD_FLOAT_ABI)
12266 {
12267 /* Register the MIPS16 -mhard-float stubs. */
12268 set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
12269 set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
12270 set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
12271 set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
12272
12273 set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
12274 set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
12275 set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
12276 set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
12277 set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
12278 set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
12279 set_optab_libfunc (unord_optab, SFmode, "__mips16_unordsf2");
12280
12281 set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
12282 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
12283 set_conv_libfunc (ufloat_optab, SFmode, SImode, "__mips16_floatunsisf");
12284
12285 if (TARGET_DOUBLE_FLOAT)
12286 {
12287 set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
12288 set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
12289 set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
12290 set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
12291
12292 set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
12293 set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
12294 set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
12295 set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
12296 set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
12297 set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
12298 set_optab_libfunc (unord_optab, DFmode, "__mips16_unorddf2");
12299
12300 set_conv_libfunc (sext_optab, DFmode, SFmode,
12301 "__mips16_extendsfdf2");
12302 set_conv_libfunc (trunc_optab, SFmode, DFmode,
12303 "__mips16_truncdfsf2");
12304 set_conv_libfunc (sfix_optab, SImode, DFmode,
12305 "__mips16_fix_truncdfsi");
12306 set_conv_libfunc (sfloat_optab, DFmode, SImode,
12307 "__mips16_floatsidf");
12308 set_conv_libfunc (ufloat_optab, DFmode, SImode,
12309 "__mips16_floatunsidf");
12310 }
12311 }
12312
12313 /* The MIPS16 ISA does not have an encoding for "sync", so we rely
12314 on an external non-MIPS16 routine to implement __sync_synchronize.
12315 Similarly for the rest of the ll/sc libfuncs. */
12316 if (TARGET_MIPS16)
12317 {
12318 synchronize_libfunc = init_one_libfunc ("__sync_synchronize");
12319 init_sync_libfuncs (UNITS_PER_WORD);
12320 }
12321 }
12322
12323 /* Build up a multi-insn sequence that loads label TARGET into $AT. */
12324
12325 static void
12326 mips_process_load_label (rtx target)
12327 {
12328 rtx base, gp, intop;
12329 HOST_WIDE_INT offset;
12330
12331 mips_multi_start ();
12332 switch (mips_abi)
12333 {
12334 case ABI_N32:
12335 mips_multi_add_insn ("lw\t%@,%%got_page(%0)(%+)", target, 0);
12336 mips_multi_add_insn ("addiu\t%@,%@,%%got_ofst(%0)", target, 0);
12337 break;
12338
12339 case ABI_64:
12340 mips_multi_add_insn ("ld\t%@,%%got_page(%0)(%+)", target, 0);
12341 mips_multi_add_insn ("daddiu\t%@,%@,%%got_ofst(%0)", target, 0);
12342 break;
12343
12344 default:
12345 gp = pic_offset_table_rtx;
12346 if (mips_cfun_has_cprestore_slot_p ())
12347 {
12348 gp = gen_rtx_REG (Pmode, AT_REGNUM);
12349 mips_get_cprestore_base_and_offset (&base, &offset, true);
12350 if (!SMALL_OPERAND (offset))
12351 {
12352 intop = GEN_INT (CONST_HIGH_PART (offset));
12353 mips_multi_add_insn ("lui\t%0,%1", gp, intop, 0);
12354 mips_multi_add_insn ("addu\t%0,%0,%1", gp, base, 0);
12355
12356 base = gp;
12357 offset = CONST_LOW_PART (offset);
12358 }
12359 intop = GEN_INT (offset);
12360 if (ISA_HAS_LOAD_DELAY)
12361 mips_multi_add_insn ("lw\t%0,%1(%2)%#", gp, intop, base, 0);
12362 else
12363 mips_multi_add_insn ("lw\t%0,%1(%2)", gp, intop, base, 0);
12364 }
12365 if (ISA_HAS_LOAD_DELAY)
12366 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)%#", target, gp, 0);
12367 else
12368 mips_multi_add_insn ("lw\t%@,%%got(%0)(%1)", target, gp, 0);
12369 mips_multi_add_insn ("addiu\t%@,%@,%%lo(%0)", target, 0);
12370 break;
12371 }
12372 }
12373
12374 /* Return the number of instructions needed to load a label into $AT. */
12375
12376 static unsigned int
12377 mips_load_label_num_insns (void)
12378 {
12379 if (cfun->machine->load_label_num_insns == 0)
12380 {
12381 mips_process_load_label (pc_rtx);
12382 cfun->machine->load_label_num_insns = mips_multi_num_insns;
12383 }
12384 return cfun->machine->load_label_num_insns;
12385 }
12386
12387 /* Emit an asm sequence to start a noat block and load the address
12388 of a label into $1. */
12389
12390 void
12391 mips_output_load_label (rtx target)
12392 {
12393 mips_push_asm_switch (&mips_noat);
12394 if (TARGET_EXPLICIT_RELOCS)
12395 {
12396 mips_process_load_label (target);
12397 mips_multi_write ();
12398 }
12399 else
12400 {
12401 if (Pmode == DImode)
12402 output_asm_insn ("dla\t%@,%0", &target);
12403 else
12404 output_asm_insn ("la\t%@,%0", &target);
12405 }
12406 }
12407
12408 /* Return the length of INSN. LENGTH is the initial length computed by
12409 attributes in the machine-description file. */
12410
12411 int
12412 mips_adjust_insn_length (rtx_insn *insn, int length)
12413 {
12414 /* mips.md uses MAX_PIC_BRANCH_LENGTH as a placeholder for the length
12415 of a PIC long-branch sequence. Substitute the correct value. */
12416 if (length == MAX_PIC_BRANCH_LENGTH
12417 && JUMP_P (insn)
12418 && INSN_CODE (insn) >= 0
12419 && get_attr_type (insn) == TYPE_BRANCH)
12420 {
12421 /* Add the branch-over instruction and its delay slot, if this
12422 is a conditional branch. */
12423 length = simplejump_p (insn) ? 0 : 8;
12424
12425 /* Add the size of a load into $AT. */
12426 length += BASE_INSN_LENGTH * mips_load_label_num_insns ();
12427
12428 /* Add the length of an indirect jump, ignoring the delay slot. */
12429 length += TARGET_COMPRESSION ? 2 : 4;
12430 }
12431
12432 /* A unconditional jump has an unfilled delay slot if it is not part
12433 of a sequence. A conditional jump normally has a delay slot, but
12434 does not on MIPS16. */
12435 if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
12436 length += TARGET_MIPS16 ? 2 : 4;
12437
12438 /* See how many nops might be needed to avoid hardware hazards. */
12439 if (!cfun->machine->ignore_hazard_length_p
12440 && INSN_P (insn)
12441 && INSN_CODE (insn) >= 0)
12442 switch (get_attr_hazard (insn))
12443 {
12444 case HAZARD_NONE:
12445 break;
12446
12447 case HAZARD_DELAY:
12448 length += NOP_INSN_LENGTH;
12449 break;
12450
12451 case HAZARD_HILO:
12452 length += NOP_INSN_LENGTH * 2;
12453 break;
12454 }
12455
12456 return length;
12457 }
12458
12459 /* Return the assembly code for INSN, which has the operands given by
12460 OPERANDS, and which branches to OPERANDS[0] if some condition is true.
12461 BRANCH_IF_TRUE is the asm template that should be used if OPERANDS[0]
12462 is in range of a direct branch. BRANCH_IF_FALSE is an inverted
12463 version of BRANCH_IF_TRUE. */
12464
12465 const char *
12466 mips_output_conditional_branch (rtx_insn *insn, rtx *operands,
12467 const char *branch_if_true,
12468 const char *branch_if_false)
12469 {
12470 unsigned int length;
12471 rtx taken;
12472
12473 gcc_assert (LABEL_P (operands[0]));
12474
12475 length = get_attr_length (insn);
12476 if (length <= 8)
12477 {
12478 /* Just a simple conditional branch. */
12479 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
12480 return branch_if_true;
12481 }
12482
12483 /* Generate a reversed branch around a direct jump. This fallback does
12484 not use branch-likely instructions. */
12485 mips_branch_likely = false;
12486 rtx_code_label *not_taken = gen_label_rtx ();
12487 taken = operands[0];
12488
12489 /* Generate the reversed branch to NOT_TAKEN. */
12490 operands[0] = not_taken;
12491 output_asm_insn (branch_if_false, operands);
12492
12493 /* If INSN has a delay slot, we must provide delay slots for both the
12494 branch to NOT_TAKEN and the conditional jump. We must also ensure
12495 that INSN's delay slot is executed in the appropriate cases. */
12496 if (final_sequence)
12497 {
12498 /* This first delay slot will always be executed, so use INSN's
12499 delay slot if is not annulled. */
12500 if (!INSN_ANNULLED_BRANCH_P (insn))
12501 {
12502 final_scan_insn (final_sequence->insn (1),
12503 asm_out_file, optimize, 1, NULL);
12504 final_sequence->insn (1)->set_deleted ();
12505 }
12506 else
12507 output_asm_insn ("nop", 0);
12508 fprintf (asm_out_file, "\n");
12509 }
12510
12511 /* Output the unconditional branch to TAKEN. */
12512 if (TARGET_ABSOLUTE_JUMPS)
12513 output_asm_insn (MIPS_ABSOLUTE_JUMP ("j\t%0%/"), &taken);
12514 else
12515 {
12516 mips_output_load_label (taken);
12517 output_asm_insn ("jr\t%@%]%/", 0);
12518 }
12519
12520 /* Now deal with its delay slot; see above. */
12521 if (final_sequence)
12522 {
12523 /* This delay slot will only be executed if the branch is taken.
12524 Use INSN's delay slot if is annulled. */
12525 if (INSN_ANNULLED_BRANCH_P (insn))
12526 {
12527 final_scan_insn (final_sequence->insn (1),
12528 asm_out_file, optimize, 1, NULL);
12529 final_sequence->insn (1)->set_deleted ();
12530 }
12531 else
12532 output_asm_insn ("nop", 0);
12533 fprintf (asm_out_file, "\n");
12534 }
12535
12536 /* Output NOT_TAKEN. */
12537 targetm.asm_out.internal_label (asm_out_file, "L",
12538 CODE_LABEL_NUMBER (not_taken));
12539 return "";
12540 }
12541
12542 /* Return the assembly code for INSN, which branches to OPERANDS[0]
12543 if some ordering condition is true. The condition is given by
12544 OPERANDS[1] if !INVERTED_P, otherwise it is the inverse of
12545 OPERANDS[1]. OPERANDS[2] is the comparison's first operand;
12546 its second is always zero. */
12547
12548 const char *
12549 mips_output_order_conditional_branch (rtx_insn *insn, rtx *operands, bool inverted_p)
12550 {
12551 const char *branch[2];
12552
12553 /* Make BRANCH[1] branch to OPERANDS[0] when the condition is true.
12554 Make BRANCH[0] branch on the inverse condition. */
12555 switch (GET_CODE (operands[1]))
12556 {
12557 /* These cases are equivalent to comparisons against zero. */
12558 case LEU:
12559 inverted_p = !inverted_p;
12560 /* Fall through. */
12561 case GTU:
12562 branch[!inverted_p] = MIPS_BRANCH ("bne", "%2,%.,%0");
12563 branch[inverted_p] = MIPS_BRANCH ("beq", "%2,%.,%0");
12564 break;
12565
12566 /* These cases are always true or always false. */
12567 case LTU:
12568 inverted_p = !inverted_p;
12569 /* Fall through. */
12570 case GEU:
12571 branch[!inverted_p] = MIPS_BRANCH ("beq", "%.,%.,%0");
12572 branch[inverted_p] = MIPS_BRANCH ("bne", "%.,%.,%0");
12573 break;
12574
12575 default:
12576 branch[!inverted_p] = MIPS_BRANCH ("b%C1z", "%2,%0");
12577 branch[inverted_p] = MIPS_BRANCH ("b%N1z", "%2,%0");
12578 break;
12579 }
12580 return mips_output_conditional_branch (insn, operands, branch[1], branch[0]);
12581 }
12582 \f
12583 /* Start a block of code that needs access to the LL, SC and SYNC
12584 instructions. */
12585
12586 static void
12587 mips_start_ll_sc_sync_block (void)
12588 {
12589 if (!ISA_HAS_LL_SC)
12590 {
12591 output_asm_insn (".set\tpush", 0);
12592 if (TARGET_64BIT)
12593 output_asm_insn (".set\tmips3", 0);
12594 else
12595 output_asm_insn (".set\tmips2", 0);
12596 }
12597 }
12598
12599 /* End a block started by mips_start_ll_sc_sync_block. */
12600
12601 static void
12602 mips_end_ll_sc_sync_block (void)
12603 {
12604 if (!ISA_HAS_LL_SC)
12605 output_asm_insn (".set\tpop", 0);
12606 }
12607
12608 /* Output and/or return the asm template for a sync instruction. */
12609
12610 const char *
12611 mips_output_sync (void)
12612 {
12613 mips_start_ll_sc_sync_block ();
12614 output_asm_insn ("sync", 0);
12615 mips_end_ll_sc_sync_block ();
12616 return "";
12617 }
12618
12619 /* Return the asm template associated with sync_insn1 value TYPE.
12620 IS_64BIT_P is true if we want a 64-bit rather than 32-bit operation. */
12621
12622 static const char *
12623 mips_sync_insn1_template (enum attr_sync_insn1 type, bool is_64bit_p)
12624 {
12625 switch (type)
12626 {
12627 case SYNC_INSN1_MOVE:
12628 return "move\t%0,%z2";
12629 case SYNC_INSN1_LI:
12630 return "li\t%0,%2";
12631 case SYNC_INSN1_ADDU:
12632 return is_64bit_p ? "daddu\t%0,%1,%z2" : "addu\t%0,%1,%z2";
12633 case SYNC_INSN1_ADDIU:
12634 return is_64bit_p ? "daddiu\t%0,%1,%2" : "addiu\t%0,%1,%2";
12635 case SYNC_INSN1_SUBU:
12636 return is_64bit_p ? "dsubu\t%0,%1,%z2" : "subu\t%0,%1,%z2";
12637 case SYNC_INSN1_AND:
12638 return "and\t%0,%1,%z2";
12639 case SYNC_INSN1_ANDI:
12640 return "andi\t%0,%1,%2";
12641 case SYNC_INSN1_OR:
12642 return "or\t%0,%1,%z2";
12643 case SYNC_INSN1_ORI:
12644 return "ori\t%0,%1,%2";
12645 case SYNC_INSN1_XOR:
12646 return "xor\t%0,%1,%z2";
12647 case SYNC_INSN1_XORI:
12648 return "xori\t%0,%1,%2";
12649 }
12650 gcc_unreachable ();
12651 }
12652
12653 /* Return the asm template associated with sync_insn2 value TYPE. */
12654
12655 static const char *
12656 mips_sync_insn2_template (enum attr_sync_insn2 type)
12657 {
12658 switch (type)
12659 {
12660 case SYNC_INSN2_NOP:
12661 gcc_unreachable ();
12662 case SYNC_INSN2_AND:
12663 return "and\t%0,%1,%z2";
12664 case SYNC_INSN2_XOR:
12665 return "xor\t%0,%1,%z2";
12666 case SYNC_INSN2_NOT:
12667 return "nor\t%0,%1,%.";
12668 }
12669 gcc_unreachable ();
12670 }
12671
12672 /* OPERANDS are the operands to a sync loop instruction and INDEX is
12673 the value of the one of the sync_* attributes. Return the operand
12674 referred to by the attribute, or DEFAULT_VALUE if the insn doesn't
12675 have the associated attribute. */
12676
12677 static rtx
12678 mips_get_sync_operand (rtx *operands, int index, rtx default_value)
12679 {
12680 if (index > 0)
12681 default_value = operands[index - 1];
12682 return default_value;
12683 }
12684
12685 /* INSN is a sync loop with operands OPERANDS. Build up a multi-insn
12686 sequence for it. */
12687
12688 static void
12689 mips_process_sync_loop (rtx_insn *insn, rtx *operands)
12690 {
12691 rtx at, mem, oldval, newval, inclusive_mask, exclusive_mask;
12692 rtx required_oldval, insn1_op2, tmp1, tmp2, tmp3, cmp;
12693 unsigned int tmp3_insn;
12694 enum attr_sync_insn1 insn1;
12695 enum attr_sync_insn2 insn2;
12696 bool is_64bit_p;
12697 int memmodel_attr;
12698 enum memmodel model;
12699
12700 /* Read an operand from the sync_WHAT attribute and store it in
12701 variable WHAT. DEFAULT is the default value if no attribute
12702 is specified. */
12703 #define READ_OPERAND(WHAT, DEFAULT) \
12704 WHAT = mips_get_sync_operand (operands, (int) get_attr_sync_##WHAT (insn), \
12705 DEFAULT)
12706
12707 /* Read the memory. */
12708 READ_OPERAND (mem, 0);
12709 gcc_assert (mem);
12710 is_64bit_p = (GET_MODE_BITSIZE (GET_MODE (mem)) == 64);
12711
12712 /* Read the other attributes. */
12713 at = gen_rtx_REG (GET_MODE (mem), AT_REGNUM);
12714 READ_OPERAND (oldval, at);
12715 READ_OPERAND (cmp, 0);
12716 READ_OPERAND (newval, at);
12717 READ_OPERAND (inclusive_mask, 0);
12718 READ_OPERAND (exclusive_mask, 0);
12719 READ_OPERAND (required_oldval, 0);
12720 READ_OPERAND (insn1_op2, 0);
12721 insn1 = get_attr_sync_insn1 (insn);
12722 insn2 = get_attr_sync_insn2 (insn);
12723
12724 /* Don't bother setting CMP result that is never used. */
12725 if (cmp && find_reg_note (insn, REG_UNUSED, cmp))
12726 cmp = 0;
12727
12728 memmodel_attr = get_attr_sync_memmodel (insn);
12729 switch (memmodel_attr)
12730 {
12731 case 10:
12732 model = MEMMODEL_ACQ_REL;
12733 break;
12734 case 11:
12735 model = MEMMODEL_ACQUIRE;
12736 break;
12737 default:
12738 model = (enum memmodel) INTVAL (operands[memmodel_attr]);
12739 }
12740
12741 mips_multi_start ();
12742
12743 /* Output the release side of the memory barrier. */
12744 if (need_atomic_barrier_p (model, true))
12745 {
12746 if (required_oldval == 0 && TARGET_OCTEON)
12747 {
12748 /* Octeon doesn't reorder reads, so a full barrier can be
12749 created by using SYNCW to order writes combined with the
12750 write from the following SC. When the SC successfully
12751 completes, we know that all preceding writes are also
12752 committed to the coherent memory system. It is possible
12753 for a single SYNCW to fail, but a pair of them will never
12754 fail, so we use two. */
12755 mips_multi_add_insn ("syncw", NULL);
12756 mips_multi_add_insn ("syncw", NULL);
12757 }
12758 else
12759 mips_multi_add_insn ("sync", NULL);
12760 }
12761
12762 /* Output the branch-back label. */
12763 mips_multi_add_label ("1:");
12764
12765 /* OLDVAL = *MEM. */
12766 mips_multi_add_insn (is_64bit_p ? "lld\t%0,%1" : "ll\t%0,%1",
12767 oldval, mem, NULL);
12768
12769 /* if ((OLDVAL & INCLUSIVE_MASK) != REQUIRED_OLDVAL) goto 2. */
12770 if (required_oldval)
12771 {
12772 if (inclusive_mask == 0)
12773 tmp1 = oldval;
12774 else
12775 {
12776 gcc_assert (oldval != at);
12777 mips_multi_add_insn ("and\t%0,%1,%2",
12778 at, oldval, inclusive_mask, NULL);
12779 tmp1 = at;
12780 }
12781 mips_multi_add_insn ("bne\t%0,%z1,2f", tmp1, required_oldval, NULL);
12782
12783 /* CMP = 0 [delay slot]. */
12784 if (cmp)
12785 mips_multi_add_insn ("li\t%0,0", cmp, NULL);
12786 }
12787
12788 /* $TMP1 = OLDVAL & EXCLUSIVE_MASK. */
12789 if (exclusive_mask == 0)
12790 tmp1 = const0_rtx;
12791 else
12792 {
12793 gcc_assert (oldval != at);
12794 mips_multi_add_insn ("and\t%0,%1,%z2",
12795 at, oldval, exclusive_mask, NULL);
12796 tmp1 = at;
12797 }
12798
12799 /* $TMP2 = INSN1 (OLDVAL, INSN1_OP2).
12800
12801 We can ignore moves if $TMP4 != INSN1_OP2, since we'll still emit
12802 at least one instruction in that case. */
12803 if (insn1 == SYNC_INSN1_MOVE
12804 && (tmp1 != const0_rtx || insn2 != SYNC_INSN2_NOP))
12805 tmp2 = insn1_op2;
12806 else
12807 {
12808 mips_multi_add_insn (mips_sync_insn1_template (insn1, is_64bit_p),
12809 newval, oldval, insn1_op2, NULL);
12810 tmp2 = newval;
12811 }
12812
12813 /* $TMP3 = INSN2 ($TMP2, INCLUSIVE_MASK). */
12814 if (insn2 == SYNC_INSN2_NOP)
12815 tmp3 = tmp2;
12816 else
12817 {
12818 mips_multi_add_insn (mips_sync_insn2_template (insn2),
12819 newval, tmp2, inclusive_mask, NULL);
12820 tmp3 = newval;
12821 }
12822 tmp3_insn = mips_multi_last_index ();
12823
12824 /* $AT = $TMP1 | $TMP3. */
12825 if (tmp1 == const0_rtx || tmp3 == const0_rtx)
12826 {
12827 mips_multi_set_operand (tmp3_insn, 0, at);
12828 tmp3 = at;
12829 }
12830 else
12831 {
12832 gcc_assert (tmp1 != tmp3);
12833 mips_multi_add_insn ("or\t%0,%1,%2", at, tmp1, tmp3, NULL);
12834 }
12835
12836 /* if (!commit (*MEM = $AT)) goto 1.
12837
12838 This will sometimes be a delayed branch; see the write code below
12839 for details. */
12840 mips_multi_add_insn (is_64bit_p ? "scd\t%0,%1" : "sc\t%0,%1", at, mem, NULL);
12841 mips_multi_add_insn ("beq%?\t%0,%.,1b", at, NULL);
12842
12843 /* if (INSN1 != MOVE && INSN1 != LI) NEWVAL = $TMP3 [delay slot]. */
12844 if (insn1 != SYNC_INSN1_MOVE && insn1 != SYNC_INSN1_LI && tmp3 != newval)
12845 {
12846 mips_multi_copy_insn (tmp3_insn);
12847 mips_multi_set_operand (mips_multi_last_index (), 0, newval);
12848 }
12849 else if (!(required_oldval && cmp))
12850 mips_multi_add_insn ("nop", NULL);
12851
12852 /* CMP = 1 -- either standalone or in a delay slot. */
12853 if (required_oldval && cmp)
12854 mips_multi_add_insn ("li\t%0,1", cmp, NULL);
12855
12856 /* Output the acquire side of the memory barrier. */
12857 if (TARGET_SYNC_AFTER_SC && need_atomic_barrier_p (model, false))
12858 mips_multi_add_insn ("sync", NULL);
12859
12860 /* Output the exit label, if needed. */
12861 if (required_oldval)
12862 mips_multi_add_label ("2:");
12863
12864 #undef READ_OPERAND
12865 }
12866
12867 /* Output and/or return the asm template for sync loop INSN, which has
12868 the operands given by OPERANDS. */
12869
12870 const char *
12871 mips_output_sync_loop (rtx_insn *insn, rtx *operands)
12872 {
12873 mips_process_sync_loop (insn, operands);
12874
12875 /* Use branch-likely instructions to work around the LL/SC R10000
12876 errata. */
12877 mips_branch_likely = TARGET_FIX_R10000;
12878
12879 mips_push_asm_switch (&mips_noreorder);
12880 mips_push_asm_switch (&mips_nomacro);
12881 mips_push_asm_switch (&mips_noat);
12882 mips_start_ll_sc_sync_block ();
12883
12884 mips_multi_write ();
12885
12886 mips_end_ll_sc_sync_block ();
12887 mips_pop_asm_switch (&mips_noat);
12888 mips_pop_asm_switch (&mips_nomacro);
12889 mips_pop_asm_switch (&mips_noreorder);
12890
12891 return "";
12892 }
12893
12894 /* Return the number of individual instructions in sync loop INSN,
12895 which has the operands given by OPERANDS. */
12896
12897 unsigned int
12898 mips_sync_loop_insns (rtx_insn *insn, rtx *operands)
12899 {
12900 mips_process_sync_loop (insn, operands);
12901 return mips_multi_num_insns;
12902 }
12903 \f
12904 /* Return the assembly code for DIV or DDIV instruction DIVISION, which has
12905 the operands given by OPERANDS. Add in a divide-by-zero check if needed.
12906
12907 When working around R4000 and R4400 errata, we need to make sure that
12908 the division is not immediately followed by a shift[1][2]. We also
12909 need to stop the division from being put into a branch delay slot[3].
12910 The easiest way to avoid both problems is to add a nop after the
12911 division. When a divide-by-zero check is needed, this nop can be
12912 used to fill the branch delay slot.
12913
12914 [1] If a double-word or a variable shift executes immediately
12915 after starting an integer division, the shift may give an
12916 incorrect result. See quotations of errata #16 and #28 from
12917 "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12918 in mips.md for details.
12919
12920 [2] A similar bug to [1] exists for all revisions of the
12921 R4000 and the R4400 when run in an MC configuration.
12922 From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
12923
12924 "19. In this following sequence:
12925
12926 ddiv (or ddivu or div or divu)
12927 dsll32 (or dsrl32, dsra32)
12928
12929 if an MPT stall occurs, while the divide is slipping the cpu
12930 pipeline, then the following double shift would end up with an
12931 incorrect result.
12932
12933 Workaround: The compiler needs to avoid generating any
12934 sequence with divide followed by extended double shift."
12935
12936 This erratum is also present in "MIPS R4400MC Errata, Processor
12937 Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
12938 & 3.0" as errata #10 and #4, respectively.
12939
12940 [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
12941 (also valid for MIPS R4000MC processors):
12942
12943 "52. R4000SC: This bug does not apply for the R4000PC.
12944
12945 There are two flavors of this bug:
12946
12947 1) If the instruction just after divide takes an RF exception
12948 (tlb-refill, tlb-invalid) and gets an instruction cache
12949 miss (both primary and secondary) and the line which is
12950 currently in secondary cache at this index had the first
12951 data word, where the bits 5..2 are set, then R4000 would
12952 get a wrong result for the div.
12953
12954 ##1
12955 nop
12956 div r8, r9
12957 ------------------- # end-of page. -tlb-refill
12958 nop
12959 ##2
12960 nop
12961 div r8, r9
12962 ------------------- # end-of page. -tlb-invalid
12963 nop
12964
12965 2) If the divide is in the taken branch delay slot, where the
12966 target takes RF exception and gets an I-cache miss for the
12967 exception vector or where I-cache miss occurs for the
12968 target address, under the above mentioned scenarios, the
12969 div would get wrong results.
12970
12971 ##1
12972 j r2 # to next page mapped or unmapped
12973 div r8,r9 # this bug would be there as long
12974 # as there is an ICache miss and
12975 nop # the "data pattern" is present
12976
12977 ##2
12978 beq r0, r0, NextPage # to Next page
12979 div r8,r9
12980 nop
12981
12982 This bug is present for div, divu, ddiv, and ddivu
12983 instructions.
12984
12985 Workaround: For item 1), OS could make sure that the next page
12986 after the divide instruction is also mapped. For item 2), the
12987 compiler could make sure that the divide instruction is not in
12988 the branch delay slot."
12989
12990 These processors have PRId values of 0x00004220 and 0x00004300 for
12991 the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400. */
12992
12993 const char *
12994 mips_output_division (const char *division, rtx *operands)
12995 {
12996 const char *s;
12997
12998 s = division;
12999 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
13000 {
13001 output_asm_insn (s, operands);
13002 s = "nop";
13003 }
13004 if (TARGET_CHECK_ZERO_DIV)
13005 {
13006 if (TARGET_MIPS16)
13007 {
13008 output_asm_insn (s, operands);
13009 s = "bnez\t%2,1f\n\tbreak\t7\n1:";
13010 }
13011 else if (GENERATE_DIVIDE_TRAPS)
13012 {
13013 /* Avoid long replay penalty on load miss by putting the trap before
13014 the divide. */
13015 if (TUNE_74K)
13016 output_asm_insn ("teq\t%2,%.,7", operands);
13017 else
13018 {
13019 output_asm_insn (s, operands);
13020 s = "teq\t%2,%.,7";
13021 }
13022 }
13023 else
13024 {
13025 output_asm_insn ("%(bne\t%2,%.,1f", operands);
13026 output_asm_insn (s, operands);
13027 s = "break\t7%)\n1:";
13028 }
13029 }
13030 return s;
13031 }
13032 \f
13033 /* Return true if destination of IN_INSN is used as add source in
13034 OUT_INSN. Both IN_INSN and OUT_INSN are of type fmadd. Example:
13035 madd.s dst, x, y, z
13036 madd.s a, dst, b, c */
13037
13038 bool
13039 mips_fmadd_bypass (rtx_insn *out_insn, rtx_insn *in_insn)
13040 {
13041 int dst_reg, src_reg;
13042
13043 gcc_assert (get_attr_type (in_insn) == TYPE_FMADD);
13044 gcc_assert (get_attr_type (out_insn) == TYPE_FMADD);
13045
13046 extract_insn (in_insn);
13047 dst_reg = REG_P (recog_data.operand[0]);
13048
13049 extract_insn (out_insn);
13050 src_reg = REG_P (recog_data.operand[1]);
13051
13052 if (dst_reg == src_reg)
13053 return true;
13054
13055 return false;
13056 }
13057
13058 /* Return true if IN_INSN is a multiply-add or multiply-subtract
13059 instruction and if OUT_INSN assigns to the accumulator operand. */
13060
13061 bool
13062 mips_linked_madd_p (rtx_insn *out_insn, rtx_insn *in_insn)
13063 {
13064 enum attr_accum_in accum_in;
13065 int accum_in_opnum;
13066 rtx accum_in_op;
13067
13068 if (recog_memoized (in_insn) < 0)
13069 return false;
13070
13071 accum_in = get_attr_accum_in (in_insn);
13072 if (accum_in == ACCUM_IN_NONE)
13073 return false;
13074
13075 accum_in_opnum = accum_in - ACCUM_IN_0;
13076
13077 extract_insn (in_insn);
13078 gcc_assert (accum_in_opnum < recog_data.n_operands);
13079 accum_in_op = recog_data.operand[accum_in_opnum];
13080
13081 return reg_set_p (accum_in_op, out_insn);
13082 }
13083
13084 /* True if the dependency between OUT_INSN and IN_INSN is on the store
13085 data rather than the address. We need this because the cprestore
13086 pattern is type "store", but is defined using an UNSPEC_VOLATILE,
13087 which causes the default routine to abort. We just return false
13088 for that case. */
13089
13090 bool
13091 mips_store_data_bypass_p (rtx_insn *out_insn, rtx_insn *in_insn)
13092 {
13093 if (GET_CODE (PATTERN (in_insn)) == UNSPEC_VOLATILE)
13094 return false;
13095
13096 return !store_data_bypass_p (out_insn, in_insn);
13097 }
13098 \f
13099
13100 /* Variables and flags used in scheduler hooks when tuning for
13101 Loongson 2E/2F. */
13102 static struct
13103 {
13104 /* Variables to support Loongson 2E/2F round-robin [F]ALU1/2 dispatch
13105 strategy. */
13106
13107 /* If true, then next ALU1/2 instruction will go to ALU1. */
13108 bool alu1_turn_p;
13109
13110 /* If true, then next FALU1/2 unstruction will go to FALU1. */
13111 bool falu1_turn_p;
13112
13113 /* Codes to query if [f]alu{1,2}_core units are subscribed or not. */
13114 int alu1_core_unit_code;
13115 int alu2_core_unit_code;
13116 int falu1_core_unit_code;
13117 int falu2_core_unit_code;
13118
13119 /* True if current cycle has a multi instruction.
13120 This flag is used in mips_ls2_dfa_post_advance_cycle. */
13121 bool cycle_has_multi_p;
13122
13123 /* Instructions to subscribe ls2_[f]alu{1,2}_turn_enabled units.
13124 These are used in mips_ls2_dfa_post_advance_cycle to initialize
13125 DFA state.
13126 E.g., when alu1_turn_enabled_insn is issued it makes next ALU1/2
13127 instruction to go ALU1. */
13128 rtx_insn *alu1_turn_enabled_insn;
13129 rtx_insn *alu2_turn_enabled_insn;
13130 rtx_insn *falu1_turn_enabled_insn;
13131 rtx_insn *falu2_turn_enabled_insn;
13132 } mips_ls2;
13133
13134 /* Implement TARGET_SCHED_ADJUST_COST. We assume that anti and output
13135 dependencies have no cost, except on the 20Kc where output-dependence
13136 is treated like input-dependence. */
13137
13138 static int
13139 mips_adjust_cost (rtx_insn *insn ATTRIBUTE_UNUSED, rtx link,
13140 rtx_insn *dep ATTRIBUTE_UNUSED, int cost)
13141 {
13142 if (REG_NOTE_KIND (link) == REG_DEP_OUTPUT
13143 && TUNE_20KC)
13144 return cost;
13145 if (REG_NOTE_KIND (link) != 0)
13146 return 0;
13147 return cost;
13148 }
13149
13150 /* Return the number of instructions that can be issued per cycle. */
13151
13152 static int
13153 mips_issue_rate (void)
13154 {
13155 switch (mips_tune)
13156 {
13157 case PROCESSOR_74KC:
13158 case PROCESSOR_74KF2_1:
13159 case PROCESSOR_74KF1_1:
13160 case PROCESSOR_74KF3_2:
13161 /* The 74k is not strictly quad-issue cpu, but can be seen as one
13162 by the scheduler. It can issue 1 ALU, 1 AGEN and 2 FPU insns,
13163 but in reality only a maximum of 3 insns can be issued as
13164 floating-point loads and stores also require a slot in the
13165 AGEN pipe. */
13166 case PROCESSOR_R10000:
13167 /* All R10K Processors are quad-issue (being the first MIPS
13168 processors to support this feature). */
13169 return 4;
13170
13171 case PROCESSOR_20KC:
13172 case PROCESSOR_R4130:
13173 case PROCESSOR_R5400:
13174 case PROCESSOR_R5500:
13175 case PROCESSOR_R5900:
13176 case PROCESSOR_R7000:
13177 case PROCESSOR_R9000:
13178 case PROCESSOR_OCTEON:
13179 case PROCESSOR_OCTEON2:
13180 return 2;
13181
13182 case PROCESSOR_SB1:
13183 case PROCESSOR_SB1A:
13184 /* This is actually 4, but we get better performance if we claim 3.
13185 This is partly because of unwanted speculative code motion with the
13186 larger number, and partly because in most common cases we can't
13187 reach the theoretical max of 4. */
13188 return 3;
13189
13190 case PROCESSOR_LOONGSON_2E:
13191 case PROCESSOR_LOONGSON_2F:
13192 case PROCESSOR_LOONGSON_3A:
13193 case PROCESSOR_P5600:
13194 return 4;
13195
13196 case PROCESSOR_XLP:
13197 return (reload_completed ? 4 : 3);
13198
13199 default:
13200 return 1;
13201 }
13202 }
13203
13204 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook for Loongson2. */
13205
13206 static void
13207 mips_ls2_init_dfa_post_cycle_insn (void)
13208 {
13209 start_sequence ();
13210 emit_insn (gen_ls2_alu1_turn_enabled_insn ());
13211 mips_ls2.alu1_turn_enabled_insn = get_insns ();
13212 end_sequence ();
13213
13214 start_sequence ();
13215 emit_insn (gen_ls2_alu2_turn_enabled_insn ());
13216 mips_ls2.alu2_turn_enabled_insn = get_insns ();
13217 end_sequence ();
13218
13219 start_sequence ();
13220 emit_insn (gen_ls2_falu1_turn_enabled_insn ());
13221 mips_ls2.falu1_turn_enabled_insn = get_insns ();
13222 end_sequence ();
13223
13224 start_sequence ();
13225 emit_insn (gen_ls2_falu2_turn_enabled_insn ());
13226 mips_ls2.falu2_turn_enabled_insn = get_insns ();
13227 end_sequence ();
13228
13229 mips_ls2.alu1_core_unit_code = get_cpu_unit_code ("ls2_alu1_core");
13230 mips_ls2.alu2_core_unit_code = get_cpu_unit_code ("ls2_alu2_core");
13231 mips_ls2.falu1_core_unit_code = get_cpu_unit_code ("ls2_falu1_core");
13232 mips_ls2.falu2_core_unit_code = get_cpu_unit_code ("ls2_falu2_core");
13233 }
13234
13235 /* Implement TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN hook.
13236 Init data used in mips_dfa_post_advance_cycle. */
13237
13238 static void
13239 mips_init_dfa_post_cycle_insn (void)
13240 {
13241 if (TUNE_LOONGSON_2EF)
13242 mips_ls2_init_dfa_post_cycle_insn ();
13243 }
13244
13245 /* Initialize STATE when scheduling for Loongson 2E/2F.
13246 Support round-robin dispatch scheme by enabling only one of
13247 ALU1/ALU2 and one of FALU1/FALU2 units for ALU1/2 and FALU1/2 instructions
13248 respectively. */
13249
13250 static void
13251 mips_ls2_dfa_post_advance_cycle (state_t state)
13252 {
13253 if (cpu_unit_reservation_p (state, mips_ls2.alu1_core_unit_code))
13254 {
13255 /* Though there are no non-pipelined ALU1 insns,
13256 we can get an instruction of type 'multi' before reload. */
13257 gcc_assert (mips_ls2.cycle_has_multi_p);
13258 mips_ls2.alu1_turn_p = false;
13259 }
13260
13261 mips_ls2.cycle_has_multi_p = false;
13262
13263 if (cpu_unit_reservation_p (state, mips_ls2.alu2_core_unit_code))
13264 /* We have a non-pipelined alu instruction in the core,
13265 adjust round-robin counter. */
13266 mips_ls2.alu1_turn_p = true;
13267
13268 if (mips_ls2.alu1_turn_p)
13269 {
13270 if (state_transition (state, mips_ls2.alu1_turn_enabled_insn) >= 0)
13271 gcc_unreachable ();
13272 }
13273 else
13274 {
13275 if (state_transition (state, mips_ls2.alu2_turn_enabled_insn) >= 0)
13276 gcc_unreachable ();
13277 }
13278
13279 if (cpu_unit_reservation_p (state, mips_ls2.falu1_core_unit_code))
13280 {
13281 /* There are no non-pipelined FALU1 insns. */
13282 gcc_unreachable ();
13283 mips_ls2.falu1_turn_p = false;
13284 }
13285
13286 if (cpu_unit_reservation_p (state, mips_ls2.falu2_core_unit_code))
13287 /* We have a non-pipelined falu instruction in the core,
13288 adjust round-robin counter. */
13289 mips_ls2.falu1_turn_p = true;
13290
13291 if (mips_ls2.falu1_turn_p)
13292 {
13293 if (state_transition (state, mips_ls2.falu1_turn_enabled_insn) >= 0)
13294 gcc_unreachable ();
13295 }
13296 else
13297 {
13298 if (state_transition (state, mips_ls2.falu2_turn_enabled_insn) >= 0)
13299 gcc_unreachable ();
13300 }
13301 }
13302
13303 /* Implement TARGET_SCHED_DFA_POST_ADVANCE_CYCLE.
13304 This hook is being called at the start of each cycle. */
13305
13306 static void
13307 mips_dfa_post_advance_cycle (void)
13308 {
13309 if (TUNE_LOONGSON_2EF)
13310 mips_ls2_dfa_post_advance_cycle (curr_state);
13311 }
13312
13313 /* Implement TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD. This should
13314 be as wide as the scheduling freedom in the DFA. */
13315
13316 static int
13317 mips_multipass_dfa_lookahead (void)
13318 {
13319 /* Can schedule up to 4 of the 6 function units in any one cycle. */
13320 if (TUNE_SB1)
13321 return 4;
13322
13323 if (TUNE_LOONGSON_2EF || TUNE_LOONGSON_3A)
13324 return 4;
13325
13326 if (TUNE_OCTEON)
13327 return 2;
13328
13329 if (TUNE_P5600)
13330 return 4;
13331
13332 return 0;
13333 }
13334 \f
13335 /* Remove the instruction at index LOWER from ready queue READY and
13336 reinsert it in front of the instruction at index HIGHER. LOWER must
13337 be <= HIGHER. */
13338
13339 static void
13340 mips_promote_ready (rtx_insn **ready, int lower, int higher)
13341 {
13342 rtx_insn *new_head;
13343 int i;
13344
13345 new_head = ready[lower];
13346 for (i = lower; i < higher; i++)
13347 ready[i] = ready[i + 1];
13348 ready[i] = new_head;
13349 }
13350
13351 /* If the priority of the instruction at POS2 in the ready queue READY
13352 is within LIMIT units of that of the instruction at POS1, swap the
13353 instructions if POS2 is not already less than POS1. */
13354
13355 static void
13356 mips_maybe_swap_ready (rtx_insn **ready, int pos1, int pos2, int limit)
13357 {
13358 if (pos1 < pos2
13359 && INSN_PRIORITY (ready[pos1]) + limit >= INSN_PRIORITY (ready[pos2]))
13360 {
13361 rtx_insn *temp;
13362
13363 temp = ready[pos1];
13364 ready[pos1] = ready[pos2];
13365 ready[pos2] = temp;
13366 }
13367 }
13368 \f
13369 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
13370 that may clobber hi or lo. */
13371 static rtx_insn *mips_macc_chains_last_hilo;
13372
13373 /* A TUNE_MACC_CHAINS helper function. Record that instruction INSN has
13374 been scheduled, updating mips_macc_chains_last_hilo appropriately. */
13375
13376 static void
13377 mips_macc_chains_record (rtx_insn *insn)
13378 {
13379 if (get_attr_may_clobber_hilo (insn))
13380 mips_macc_chains_last_hilo = insn;
13381 }
13382
13383 /* A TUNE_MACC_CHAINS helper function. Search ready queue READY, which
13384 has NREADY elements, looking for a multiply-add or multiply-subtract
13385 instruction that is cumulative with mips_macc_chains_last_hilo.
13386 If there is one, promote it ahead of anything else that might
13387 clobber hi or lo. */
13388
13389 static void
13390 mips_macc_chains_reorder (rtx_insn **ready, int nready)
13391 {
13392 int i, j;
13393
13394 if (mips_macc_chains_last_hilo != 0)
13395 for (i = nready - 1; i >= 0; i--)
13396 if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
13397 {
13398 for (j = nready - 1; j > i; j--)
13399 if (recog_memoized (ready[j]) >= 0
13400 && get_attr_may_clobber_hilo (ready[j]))
13401 {
13402 mips_promote_ready (ready, i, j);
13403 break;
13404 }
13405 break;
13406 }
13407 }
13408 \f
13409 /* The last instruction to be scheduled. */
13410 static rtx_insn *vr4130_last_insn;
13411
13412 /* A note_stores callback used by vr4130_true_reg_dependence_p. DATA
13413 points to an rtx that is initially an instruction. Nullify the rtx
13414 if the instruction uses the value of register X. */
13415
13416 static void
13417 vr4130_true_reg_dependence_p_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
13418 void *data)
13419 {
13420 rtx *insn_ptr;
13421
13422 insn_ptr = (rtx *) data;
13423 if (REG_P (x)
13424 && *insn_ptr != 0
13425 && reg_referenced_p (x, PATTERN (*insn_ptr)))
13426 *insn_ptr = 0;
13427 }
13428
13429 /* Return true if there is true register dependence between vr4130_last_insn
13430 and INSN. */
13431
13432 static bool
13433 vr4130_true_reg_dependence_p (rtx insn)
13434 {
13435 note_stores (PATTERN (vr4130_last_insn),
13436 vr4130_true_reg_dependence_p_1, &insn);
13437 return insn == 0;
13438 }
13439
13440 /* A TUNE_MIPS4130 helper function. Given that INSN1 is at the head of
13441 the ready queue and that INSN2 is the instruction after it, return
13442 true if it is worth promoting INSN2 ahead of INSN1. Look for cases
13443 in which INSN1 and INSN2 can probably issue in parallel, but for
13444 which (INSN2, INSN1) should be less sensitive to instruction
13445 alignment than (INSN1, INSN2). See 4130.md for more details. */
13446
13447 static bool
13448 vr4130_swap_insns_p (rtx_insn *insn1, rtx_insn *insn2)
13449 {
13450 sd_iterator_def sd_it;
13451 dep_t dep;
13452
13453 /* Check for the following case:
13454
13455 1) there is some other instruction X with an anti dependence on INSN1;
13456 2) X has a higher priority than INSN2; and
13457 3) X is an arithmetic instruction (and thus has no unit restrictions).
13458
13459 If INSN1 is the last instruction blocking X, it would better to
13460 choose (INSN1, X) over (INSN2, INSN1). */
13461 FOR_EACH_DEP (insn1, SD_LIST_FORW, sd_it, dep)
13462 if (DEP_TYPE (dep) == REG_DEP_ANTI
13463 && INSN_PRIORITY (DEP_CON (dep)) > INSN_PRIORITY (insn2)
13464 && recog_memoized (DEP_CON (dep)) >= 0
13465 && get_attr_vr4130_class (DEP_CON (dep)) == VR4130_CLASS_ALU)
13466 return false;
13467
13468 if (vr4130_last_insn != 0
13469 && recog_memoized (insn1) >= 0
13470 && recog_memoized (insn2) >= 0)
13471 {
13472 /* See whether INSN1 and INSN2 use different execution units,
13473 or if they are both ALU-type instructions. If so, they can
13474 probably execute in parallel. */
13475 enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
13476 enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
13477 if (class1 != class2 || class1 == VR4130_CLASS_ALU)
13478 {
13479 /* If only one of the instructions has a dependence on
13480 vr4130_last_insn, prefer to schedule the other one first. */
13481 bool dep1_p = vr4130_true_reg_dependence_p (insn1);
13482 bool dep2_p = vr4130_true_reg_dependence_p (insn2);
13483 if (dep1_p != dep2_p)
13484 return dep1_p;
13485
13486 /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
13487 is not an ALU-type instruction and if INSN1 uses the same
13488 execution unit. (Note that if this condition holds, we already
13489 know that INSN2 uses a different execution unit.) */
13490 if (class1 != VR4130_CLASS_ALU
13491 && recog_memoized (vr4130_last_insn) >= 0
13492 && class1 == get_attr_vr4130_class (vr4130_last_insn))
13493 return true;
13494 }
13495 }
13496 return false;
13497 }
13498
13499 /* A TUNE_MIPS4130 helper function. (READY, NREADY) describes a ready
13500 queue with at least two instructions. Swap the first two if
13501 vr4130_swap_insns_p says that it could be worthwhile. */
13502
13503 static void
13504 vr4130_reorder (rtx_insn **ready, int nready)
13505 {
13506 if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
13507 mips_promote_ready (ready, nready - 2, nready - 1);
13508 }
13509 \f
13510 /* Record whether last 74k AGEN instruction was a load or store. */
13511 static enum attr_type mips_last_74k_agen_insn = TYPE_UNKNOWN;
13512
13513 /* Initialize mips_last_74k_agen_insn from INSN. A null argument
13514 resets to TYPE_UNKNOWN state. */
13515
13516 static void
13517 mips_74k_agen_init (rtx_insn *insn)
13518 {
13519 if (!insn || CALL_P (insn) || JUMP_P (insn))
13520 mips_last_74k_agen_insn = TYPE_UNKNOWN;
13521 else
13522 {
13523 enum attr_type type = get_attr_type (insn);
13524 if (type == TYPE_LOAD || type == TYPE_STORE)
13525 mips_last_74k_agen_insn = type;
13526 }
13527 }
13528
13529 /* A TUNE_74K helper function. The 74K AGEN pipeline likes multiple
13530 loads to be grouped together, and multiple stores to be grouped
13531 together. Swap things around in the ready queue to make this happen. */
13532
13533 static void
13534 mips_74k_agen_reorder (rtx_insn **ready, int nready)
13535 {
13536 int i;
13537 int store_pos, load_pos;
13538
13539 store_pos = -1;
13540 load_pos = -1;
13541
13542 for (i = nready - 1; i >= 0; i--)
13543 {
13544 rtx_insn *insn = ready[i];
13545 if (USEFUL_INSN_P (insn))
13546 switch (get_attr_type (insn))
13547 {
13548 case TYPE_STORE:
13549 if (store_pos == -1)
13550 store_pos = i;
13551 break;
13552
13553 case TYPE_LOAD:
13554 if (load_pos == -1)
13555 load_pos = i;
13556 break;
13557
13558 default:
13559 break;
13560 }
13561 }
13562
13563 if (load_pos == -1 || store_pos == -1)
13564 return;
13565
13566 switch (mips_last_74k_agen_insn)
13567 {
13568 case TYPE_UNKNOWN:
13569 /* Prefer to schedule loads since they have a higher latency. */
13570 case TYPE_LOAD:
13571 /* Swap loads to the front of the queue. */
13572 mips_maybe_swap_ready (ready, load_pos, store_pos, 4);
13573 break;
13574 case TYPE_STORE:
13575 /* Swap stores to the front of the queue. */
13576 mips_maybe_swap_ready (ready, store_pos, load_pos, 4);
13577 break;
13578 default:
13579 break;
13580 }
13581 }
13582 \f
13583 /* Implement TARGET_SCHED_INIT. */
13584
13585 static void
13586 mips_sched_init (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13587 int max_ready ATTRIBUTE_UNUSED)
13588 {
13589 mips_macc_chains_last_hilo = 0;
13590 vr4130_last_insn = 0;
13591 mips_74k_agen_init (NULL);
13592
13593 /* When scheduling for Loongson2, branch instructions go to ALU1,
13594 therefore basic block is most likely to start with round-robin counter
13595 pointed to ALU2. */
13596 mips_ls2.alu1_turn_p = false;
13597 mips_ls2.falu1_turn_p = true;
13598 }
13599
13600 /* Subroutine used by TARGET_SCHED_REORDER and TARGET_SCHED_REORDER2. */
13601
13602 static void
13603 mips_sched_reorder_1 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13604 rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13605 {
13606 if (!reload_completed
13607 && TUNE_MACC_CHAINS
13608 && *nreadyp > 0)
13609 mips_macc_chains_reorder (ready, *nreadyp);
13610
13611 if (reload_completed
13612 && TUNE_MIPS4130
13613 && !TARGET_VR4130_ALIGN
13614 && *nreadyp > 1)
13615 vr4130_reorder (ready, *nreadyp);
13616
13617 if (TUNE_74K)
13618 mips_74k_agen_reorder (ready, *nreadyp);
13619 }
13620
13621 /* Implement TARGET_SCHED_REORDER. */
13622
13623 static int
13624 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13625 rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13626 {
13627 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13628 return mips_issue_rate ();
13629 }
13630
13631 /* Implement TARGET_SCHED_REORDER2. */
13632
13633 static int
13634 mips_sched_reorder2 (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13635 rtx_insn **ready, int *nreadyp, int cycle ATTRIBUTE_UNUSED)
13636 {
13637 mips_sched_reorder_1 (file, verbose, ready, nreadyp, cycle);
13638 return cached_can_issue_more;
13639 }
13640
13641 /* Update round-robin counters for ALU1/2 and FALU1/2. */
13642
13643 static void
13644 mips_ls2_variable_issue (rtx_insn *insn)
13645 {
13646 if (mips_ls2.alu1_turn_p)
13647 {
13648 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu1_core_unit_code))
13649 mips_ls2.alu1_turn_p = false;
13650 }
13651 else
13652 {
13653 if (cpu_unit_reservation_p (curr_state, mips_ls2.alu2_core_unit_code))
13654 mips_ls2.alu1_turn_p = true;
13655 }
13656
13657 if (mips_ls2.falu1_turn_p)
13658 {
13659 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu1_core_unit_code))
13660 mips_ls2.falu1_turn_p = false;
13661 }
13662 else
13663 {
13664 if (cpu_unit_reservation_p (curr_state, mips_ls2.falu2_core_unit_code))
13665 mips_ls2.falu1_turn_p = true;
13666 }
13667
13668 if (recog_memoized (insn) >= 0)
13669 mips_ls2.cycle_has_multi_p |= (get_attr_type (insn) == TYPE_MULTI);
13670 }
13671
13672 /* Implement TARGET_SCHED_VARIABLE_ISSUE. */
13673
13674 static int
13675 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
13676 rtx_insn *insn, int more)
13677 {
13678 /* Ignore USEs and CLOBBERs; don't count them against the issue rate. */
13679 if (USEFUL_INSN_P (insn))
13680 {
13681 if (get_attr_type (insn) != TYPE_GHOST)
13682 more--;
13683 if (!reload_completed && TUNE_MACC_CHAINS)
13684 mips_macc_chains_record (insn);
13685 vr4130_last_insn = insn;
13686 if (TUNE_74K)
13687 mips_74k_agen_init (insn);
13688 else if (TUNE_LOONGSON_2EF)
13689 mips_ls2_variable_issue (insn);
13690 }
13691
13692 /* Instructions of type 'multi' should all be split before
13693 the second scheduling pass. */
13694 gcc_assert (!reload_completed
13695 || recog_memoized (insn) < 0
13696 || get_attr_type (insn) != TYPE_MULTI);
13697
13698 cached_can_issue_more = more;
13699 return more;
13700 }
13701 \f
13702 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
13703 return the first operand of the associated PREF or PREFX insn. */
13704
13705 rtx
13706 mips_prefetch_cookie (rtx write, rtx locality)
13707 {
13708 /* store_streamed / load_streamed. */
13709 if (INTVAL (locality) <= 0)
13710 return GEN_INT (INTVAL (write) + 4);
13711
13712 /* store / load. */
13713 if (INTVAL (locality) <= 2)
13714 return write;
13715
13716 /* store_retained / load_retained. */
13717 return GEN_INT (INTVAL (write) + 6);
13718 }
13719 \f
13720 /* Flags that indicate when a built-in function is available.
13721
13722 BUILTIN_AVAIL_NON_MIPS16
13723 The function is available on the current target if !TARGET_MIPS16.
13724
13725 BUILTIN_AVAIL_MIPS16
13726 The function is available on the current target if TARGET_MIPS16. */
13727 #define BUILTIN_AVAIL_NON_MIPS16 1
13728 #define BUILTIN_AVAIL_MIPS16 2
13729
13730 /* Declare an availability predicate for built-in functions that
13731 require non-MIPS16 mode and also require COND to be true.
13732 NAME is the main part of the predicate's name. */
13733 #define AVAIL_NON_MIPS16(NAME, COND) \
13734 static unsigned int \
13735 mips_builtin_avail_##NAME (void) \
13736 { \
13737 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 : 0; \
13738 }
13739
13740 /* Declare an availability predicate for built-in functions that
13741 support both MIPS16 and non-MIPS16 code and also require COND
13742 to be true. NAME is the main part of the predicate's name. */
13743 #define AVAIL_ALL(NAME, COND) \
13744 static unsigned int \
13745 mips_builtin_avail_##NAME (void) \
13746 { \
13747 return (COND) ? BUILTIN_AVAIL_NON_MIPS16 | BUILTIN_AVAIL_MIPS16 : 0; \
13748 }
13749
13750 /* This structure describes a single built-in function. */
13751 struct mips_builtin_description {
13752 /* The code of the main .md file instruction. See mips_builtin_type
13753 for more information. */
13754 enum insn_code icode;
13755
13756 /* The floating-point comparison code to use with ICODE, if any. */
13757 enum mips_fp_condition cond;
13758
13759 /* The name of the built-in function. */
13760 const char *name;
13761
13762 /* Specifies how the function should be expanded. */
13763 enum mips_builtin_type builtin_type;
13764
13765 /* The function's prototype. */
13766 enum mips_function_type function_type;
13767
13768 /* Whether the function is available. */
13769 unsigned int (*avail) (void);
13770 };
13771
13772 AVAIL_ALL (hard_float, TARGET_HARD_FLOAT_ABI)
13773 AVAIL_NON_MIPS16 (paired_single, TARGET_PAIRED_SINGLE_FLOAT)
13774 AVAIL_NON_MIPS16 (sb1_paired_single, TARGET_SB1 && TARGET_PAIRED_SINGLE_FLOAT)
13775 AVAIL_NON_MIPS16 (mips3d, TARGET_MIPS3D)
13776 AVAIL_NON_MIPS16 (dsp, TARGET_DSP)
13777 AVAIL_NON_MIPS16 (dspr2, TARGET_DSPR2)
13778 AVAIL_NON_MIPS16 (dsp_32, !TARGET_64BIT && TARGET_DSP)
13779 AVAIL_NON_MIPS16 (dsp_64, TARGET_64BIT && TARGET_DSP)
13780 AVAIL_NON_MIPS16 (dspr2_32, !TARGET_64BIT && TARGET_DSPR2)
13781 AVAIL_NON_MIPS16 (loongson, TARGET_LOONGSON_VECTORS)
13782 AVAIL_NON_MIPS16 (cache, TARGET_CACHE_BUILTIN)
13783
13784 /* Construct a mips_builtin_description from the given arguments.
13785
13786 INSN is the name of the associated instruction pattern, without the
13787 leading CODE_FOR_mips_.
13788
13789 CODE is the floating-point condition code associated with the
13790 function. It can be 'f' if the field is not applicable.
13791
13792 NAME is the name of the function itself, without the leading
13793 "__builtin_mips_".
13794
13795 BUILTIN_TYPE and FUNCTION_TYPE are mips_builtin_description fields.
13796
13797 AVAIL is the name of the availability predicate, without the leading
13798 mips_builtin_avail_. */
13799 #define MIPS_BUILTIN(INSN, COND, NAME, BUILTIN_TYPE, \
13800 FUNCTION_TYPE, AVAIL) \
13801 { CODE_FOR_mips_ ## INSN, MIPS_FP_COND_ ## COND, \
13802 "__builtin_mips_" NAME, BUILTIN_TYPE, FUNCTION_TYPE, \
13803 mips_builtin_avail_ ## AVAIL }
13804
13805 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT function
13806 mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE and AVAIL
13807 are as for MIPS_BUILTIN. */
13808 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
13809 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL)
13810
13811 /* Define __builtin_mips_<INSN>_<COND>_{s,d} functions, both of which
13812 are subject to mips_builtin_avail_<AVAIL>. */
13813 #define CMP_SCALAR_BUILTINS(INSN, COND, AVAIL) \
13814 MIPS_BUILTIN (INSN ## _cond_s, COND, #INSN "_" #COND "_s", \
13815 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, AVAIL), \
13816 MIPS_BUILTIN (INSN ## _cond_d, COND, #INSN "_" #COND "_d", \
13817 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, AVAIL)
13818
13819 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
13820 The lower and upper forms are subject to mips_builtin_avail_<AVAIL>
13821 while the any and all forms are subject to mips_builtin_avail_mips3d. */
13822 #define CMP_PS_BUILTINS(INSN, COND, AVAIL) \
13823 MIPS_BUILTIN (INSN ## _cond_ps, COND, "any_" #INSN "_" #COND "_ps", \
13824 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF, \
13825 mips3d), \
13826 MIPS_BUILTIN (INSN ## _cond_ps, COND, "all_" #INSN "_" #COND "_ps", \
13827 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF, \
13828 mips3d), \
13829 MIPS_BUILTIN (INSN ## _cond_ps, COND, "lower_" #INSN "_" #COND "_ps", \
13830 MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF, \
13831 AVAIL), \
13832 MIPS_BUILTIN (INSN ## _cond_ps, COND, "upper_" #INSN "_" #COND "_ps", \
13833 MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF, \
13834 AVAIL)
13835
13836 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s. The functions
13837 are subject to mips_builtin_avail_mips3d. */
13838 #define CMP_4S_BUILTINS(INSN, COND) \
13839 MIPS_BUILTIN (INSN ## _cond_4s, COND, "any_" #INSN "_" #COND "_4s", \
13840 MIPS_BUILTIN_CMP_ANY, \
13841 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d), \
13842 MIPS_BUILTIN (INSN ## _cond_4s, COND, "all_" #INSN "_" #COND "_4s", \
13843 MIPS_BUILTIN_CMP_ALL, \
13844 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, mips3d)
13845
13846 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps. The comparison
13847 instruction requires mips_builtin_avail_<AVAIL>. */
13848 #define MOVTF_BUILTINS(INSN, COND, AVAIL) \
13849 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movt_" #INSN "_" #COND "_ps", \
13850 MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13851 AVAIL), \
13852 MIPS_BUILTIN (INSN ## _cond_ps, COND, "movf_" #INSN "_" #COND "_ps", \
13853 MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
13854 AVAIL)
13855
13856 /* Define all the built-in functions related to C.cond.fmt condition COND. */
13857 #define CMP_BUILTINS(COND) \
13858 MOVTF_BUILTINS (c, COND, paired_single), \
13859 MOVTF_BUILTINS (cabs, COND, mips3d), \
13860 CMP_SCALAR_BUILTINS (cabs, COND, mips3d), \
13861 CMP_PS_BUILTINS (c, COND, paired_single), \
13862 CMP_PS_BUILTINS (cabs, COND, mips3d), \
13863 CMP_4S_BUILTINS (c, COND), \
13864 CMP_4S_BUILTINS (cabs, COND)
13865
13866 /* Define __builtin_mips_<INSN>, which is a MIPS_BUILTIN_DIRECT_NO_TARGET
13867 function mapped to instruction CODE_FOR_mips_<INSN>, FUNCTION_TYPE
13868 and AVAIL are as for MIPS_BUILTIN. */
13869 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \
13870 MIPS_BUILTIN (INSN, f, #INSN, MIPS_BUILTIN_DIRECT_NO_TARGET, \
13871 FUNCTION_TYPE, AVAIL)
13872
13873 /* Define __builtin_mips_bposge<VALUE>. <VALUE> is 32 for the MIPS32 DSP
13874 branch instruction. AVAIL is as for MIPS_BUILTIN. */
13875 #define BPOSGE_BUILTIN(VALUE, AVAIL) \
13876 MIPS_BUILTIN (bposge, f, "bposge" #VALUE, \
13877 MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, AVAIL)
13878
13879 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<FN_NAME>
13880 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
13881 builtin_description field. */
13882 #define LOONGSON_BUILTIN_ALIAS(INSN, FN_NAME, FUNCTION_TYPE) \
13883 { CODE_FOR_loongson_ ## INSN, MIPS_FP_COND_f, \
13884 "__builtin_loongson_" #FN_NAME, MIPS_BUILTIN_DIRECT, \
13885 FUNCTION_TYPE, mips_builtin_avail_loongson }
13886
13887 /* Define a Loongson MIPS_BUILTIN_DIRECT function __builtin_loongson_<INSN>
13888 for instruction CODE_FOR_loongson_<INSN>. FUNCTION_TYPE is a
13889 builtin_description field. */
13890 #define LOONGSON_BUILTIN(INSN, FUNCTION_TYPE) \
13891 LOONGSON_BUILTIN_ALIAS (INSN, INSN, FUNCTION_TYPE)
13892
13893 /* Like LOONGSON_BUILTIN, but add _<SUFFIX> to the end of the function name.
13894 We use functions of this form when the same insn can be usefully applied
13895 to more than one datatype. */
13896 #define LOONGSON_BUILTIN_SUFFIX(INSN, SUFFIX, FUNCTION_TYPE) \
13897 LOONGSON_BUILTIN_ALIAS (INSN, INSN ## _ ## SUFFIX, FUNCTION_TYPE)
13898
13899 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
13900 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
13901 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
13902 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
13903 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
13904 #define CODE_FOR_mips_mul_ph CODE_FOR_mulv2hi3
13905 #define CODE_FOR_mips_mult CODE_FOR_mulsidi3_32bit
13906 #define CODE_FOR_mips_multu CODE_FOR_umulsidi3_32bit
13907
13908 #define CODE_FOR_loongson_packsswh CODE_FOR_vec_pack_ssat_v2si
13909 #define CODE_FOR_loongson_packsshb CODE_FOR_vec_pack_ssat_v4hi
13910 #define CODE_FOR_loongson_packushb CODE_FOR_vec_pack_usat_v4hi
13911 #define CODE_FOR_loongson_paddw CODE_FOR_addv2si3
13912 #define CODE_FOR_loongson_paddh CODE_FOR_addv4hi3
13913 #define CODE_FOR_loongson_paddb CODE_FOR_addv8qi3
13914 #define CODE_FOR_loongson_paddsh CODE_FOR_ssaddv4hi3
13915 #define CODE_FOR_loongson_paddsb CODE_FOR_ssaddv8qi3
13916 #define CODE_FOR_loongson_paddush CODE_FOR_usaddv4hi3
13917 #define CODE_FOR_loongson_paddusb CODE_FOR_usaddv8qi3
13918 #define CODE_FOR_loongson_pmaxsh CODE_FOR_smaxv4hi3
13919 #define CODE_FOR_loongson_pmaxub CODE_FOR_umaxv8qi3
13920 #define CODE_FOR_loongson_pminsh CODE_FOR_sminv4hi3
13921 #define CODE_FOR_loongson_pminub CODE_FOR_uminv8qi3
13922 #define CODE_FOR_loongson_pmulhuh CODE_FOR_umulv4hi3_highpart
13923 #define CODE_FOR_loongson_pmulhh CODE_FOR_smulv4hi3_highpart
13924 #define CODE_FOR_loongson_pmullh CODE_FOR_mulv4hi3
13925 #define CODE_FOR_loongson_psllh CODE_FOR_ashlv4hi3
13926 #define CODE_FOR_loongson_psllw CODE_FOR_ashlv2si3
13927 #define CODE_FOR_loongson_psrlh CODE_FOR_lshrv4hi3
13928 #define CODE_FOR_loongson_psrlw CODE_FOR_lshrv2si3
13929 #define CODE_FOR_loongson_psrah CODE_FOR_ashrv4hi3
13930 #define CODE_FOR_loongson_psraw CODE_FOR_ashrv2si3
13931 #define CODE_FOR_loongson_psubw CODE_FOR_subv2si3
13932 #define CODE_FOR_loongson_psubh CODE_FOR_subv4hi3
13933 #define CODE_FOR_loongson_psubb CODE_FOR_subv8qi3
13934 #define CODE_FOR_loongson_psubsh CODE_FOR_sssubv4hi3
13935 #define CODE_FOR_loongson_psubsb CODE_FOR_sssubv8qi3
13936 #define CODE_FOR_loongson_psubush CODE_FOR_ussubv4hi3
13937 #define CODE_FOR_loongson_psubusb CODE_FOR_ussubv8qi3
13938
13939 static const struct mips_builtin_description mips_builtins[] = {
13940 #define MIPS_GET_FCSR 0
13941 DIRECT_BUILTIN (get_fcsr, MIPS_USI_FTYPE_VOID, hard_float),
13942 #define MIPS_SET_FCSR 1
13943 DIRECT_NO_TARGET_BUILTIN (set_fcsr, MIPS_VOID_FTYPE_USI, hard_float),
13944
13945 DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13946 DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13947 DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13948 DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, paired_single),
13949 DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, paired_single),
13950 DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, paired_single),
13951 DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, paired_single),
13952 DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, paired_single),
13953
13954 DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT, paired_single),
13955 DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13956 DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13957 DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13958 DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, mips3d),
13959
13960 DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, mips3d),
13961 DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, mips3d),
13962 DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13963 DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13964 DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13965 DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13966
13967 DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, mips3d),
13968 DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, mips3d),
13969 DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, mips3d),
13970 DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, mips3d),
13971 DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, mips3d),
13972 DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, mips3d),
13973
13974 MIPS_FP_CONDITIONS (CMP_BUILTINS),
13975
13976 /* Built-in functions for the SB-1 processor. */
13977 DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, sb1_paired_single),
13978
13979 /* Built-in functions for the DSP ASE (32-bit and 64-bit). */
13980 DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13981 DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13982 DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13983 DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13984 DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13985 DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13986 DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
13987 DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
13988 DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13989 DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
13990 DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, dsp),
13991 DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, dsp),
13992 DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, dsp),
13993 DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, dsp),
13994 DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, dsp),
13995 DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, dsp),
13996 DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
13997 DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13998 DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, dsp),
13999 DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dsp),
14000 DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, dsp),
14001 DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, dsp),
14002 DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
14003 DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
14004 DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
14005 DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
14006 DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, dsp),
14007 DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, dsp),
14008 DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, dsp),
14009 DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, dsp),
14010 DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
14011 DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14012 DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14013 DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, dsp),
14014 DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, dsp),
14015 DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14016 DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, dsp),
14017 DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, dsp),
14018 DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
14019 DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, dsp),
14020 DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14021 DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
14022 DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, dsp),
14023 DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, dsp),
14024 DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, dsp),
14025 DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, dsp),
14026 DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, dsp),
14027 DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14028 DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14029 DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, dsp),
14030 DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14031 DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14032 DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dsp),
14033 DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14034 DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14035 DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, dsp),
14036 DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dsp),
14037 DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14038 DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dsp),
14039 DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, dsp),
14040 DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, dsp),
14041 DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_POINTER_SI, dsp),
14042 DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_POINTER_SI, dsp),
14043 DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_POINTER_SI, dsp),
14044 BPOSGE_BUILTIN (32, dsp),
14045
14046 /* The following are for the MIPS DSP ASE REV 2 (32-bit and 64-bit). */
14047 DIRECT_BUILTIN (absq_s_qb, MIPS_V4QI_FTYPE_V4QI, dspr2),
14048 DIRECT_BUILTIN (addu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14049 DIRECT_BUILTIN (addu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14050 DIRECT_BUILTIN (adduh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14051 DIRECT_BUILTIN (adduh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14052 DIRECT_BUILTIN (append, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14053 DIRECT_BUILTIN (balign, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14054 DIRECT_BUILTIN (cmpgdu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14055 DIRECT_BUILTIN (cmpgdu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14056 DIRECT_BUILTIN (cmpgdu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, dspr2),
14057 DIRECT_BUILTIN (mul_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14058 DIRECT_BUILTIN (mul_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14059 DIRECT_BUILTIN (mulq_rs_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14060 DIRECT_BUILTIN (mulq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14061 DIRECT_BUILTIN (mulq_s_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14062 DIRECT_BUILTIN (precr_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, dspr2),
14063 DIRECT_BUILTIN (precr_sra_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
14064 DIRECT_BUILTIN (precr_sra_r_ph_w, MIPS_V2HI_FTYPE_SI_SI_SI, dspr2),
14065 DIRECT_BUILTIN (prepend, MIPS_SI_FTYPE_SI_SI_SI, dspr2),
14066 DIRECT_BUILTIN (shra_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
14067 DIRECT_BUILTIN (shra_r_qb, MIPS_V4QI_FTYPE_V4QI_SI, dspr2),
14068 DIRECT_BUILTIN (shrl_ph, MIPS_V2HI_FTYPE_V2HI_SI, dspr2),
14069 DIRECT_BUILTIN (subu_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14070 DIRECT_BUILTIN (subu_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14071 DIRECT_BUILTIN (subuh_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14072 DIRECT_BUILTIN (subuh_r_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, dspr2),
14073 DIRECT_BUILTIN (addqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14074 DIRECT_BUILTIN (addqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14075 DIRECT_BUILTIN (addqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14076 DIRECT_BUILTIN (addqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14077 DIRECT_BUILTIN (subqh_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14078 DIRECT_BUILTIN (subqh_r_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, dspr2),
14079 DIRECT_BUILTIN (subqh_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14080 DIRECT_BUILTIN (subqh_r_w, MIPS_SI_FTYPE_SI_SI, dspr2),
14081
14082 /* Built-in functions for the DSP ASE (32-bit only). */
14083 DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14084 DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14085 DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14086 DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, dsp_32),
14087 DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14088 DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14089 DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14090 DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14091 DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14092 DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14093 DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14094 DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14095 DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, dsp_32),
14096 DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14097 DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14098 DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, dsp_32),
14099 DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, dsp_32),
14100 DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, dsp_32),
14101 DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, dsp_32),
14102 DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, dsp_32),
14103 DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, dsp_32),
14104 DIRECT_BUILTIN (madd, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14105 DIRECT_BUILTIN (maddu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
14106 DIRECT_BUILTIN (msub, MIPS_DI_FTYPE_DI_SI_SI, dsp_32),
14107 DIRECT_BUILTIN (msubu, MIPS_DI_FTYPE_DI_USI_USI, dsp_32),
14108 DIRECT_BUILTIN (mult, MIPS_DI_FTYPE_SI_SI, dsp_32),
14109 DIRECT_BUILTIN (multu, MIPS_DI_FTYPE_USI_USI, dsp_32),
14110
14111 /* Built-in functions for the DSP ASE (64-bit only). */
14112 DIRECT_BUILTIN (ldx, MIPS_DI_FTYPE_POINTER_SI, dsp_64),
14113
14114 /* The following are for the MIPS DSP ASE REV 2 (32-bit only). */
14115 DIRECT_BUILTIN (dpa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14116 DIRECT_BUILTIN (dps_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14117 DIRECT_BUILTIN (mulsa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14118 DIRECT_BUILTIN (dpax_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14119 DIRECT_BUILTIN (dpsx_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14120 DIRECT_BUILTIN (dpaqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14121 DIRECT_BUILTIN (dpaqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14122 DIRECT_BUILTIN (dpsqx_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14123 DIRECT_BUILTIN (dpsqx_sa_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, dspr2_32),
14124
14125 /* Builtin functions for ST Microelectronics Loongson-2E/2F cores. */
14126 LOONGSON_BUILTIN (packsswh, MIPS_V4HI_FTYPE_V2SI_V2SI),
14127 LOONGSON_BUILTIN (packsshb, MIPS_V8QI_FTYPE_V4HI_V4HI),
14128 LOONGSON_BUILTIN (packushb, MIPS_UV8QI_FTYPE_UV4HI_UV4HI),
14129 LOONGSON_BUILTIN_SUFFIX (paddw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14130 LOONGSON_BUILTIN_SUFFIX (paddh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14131 LOONGSON_BUILTIN_SUFFIX (paddb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14132 LOONGSON_BUILTIN_SUFFIX (paddw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14133 LOONGSON_BUILTIN_SUFFIX (paddh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14134 LOONGSON_BUILTIN_SUFFIX (paddb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14135 LOONGSON_BUILTIN_SUFFIX (paddd, u, MIPS_UDI_FTYPE_UDI_UDI),
14136 LOONGSON_BUILTIN_SUFFIX (paddd, s, MIPS_DI_FTYPE_DI_DI),
14137 LOONGSON_BUILTIN (paddsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14138 LOONGSON_BUILTIN (paddsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14139 LOONGSON_BUILTIN (paddush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14140 LOONGSON_BUILTIN (paddusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14141 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_ud, MIPS_UDI_FTYPE_UDI_UDI),
14142 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_uw, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14143 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_uh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14144 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_ub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14145 LOONGSON_BUILTIN_ALIAS (pandn_d, pandn_sd, MIPS_DI_FTYPE_DI_DI),
14146 LOONGSON_BUILTIN_ALIAS (pandn_w, pandn_sw, MIPS_V2SI_FTYPE_V2SI_V2SI),
14147 LOONGSON_BUILTIN_ALIAS (pandn_h, pandn_sh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14148 LOONGSON_BUILTIN_ALIAS (pandn_b, pandn_sb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14149 LOONGSON_BUILTIN (pavgh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14150 LOONGSON_BUILTIN (pavgb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14151 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14152 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14153 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14154 LOONGSON_BUILTIN_SUFFIX (pcmpeqw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14155 LOONGSON_BUILTIN_SUFFIX (pcmpeqh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14156 LOONGSON_BUILTIN_SUFFIX (pcmpeqb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14157 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14158 LOONGSON_BUILTIN_SUFFIX (pcmpgth, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14159 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14160 LOONGSON_BUILTIN_SUFFIX (pcmpgtw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14161 LOONGSON_BUILTIN_SUFFIX (pcmpgth, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14162 LOONGSON_BUILTIN_SUFFIX (pcmpgtb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14163 LOONGSON_BUILTIN_SUFFIX (pextrh, u, MIPS_UV4HI_FTYPE_UV4HI_USI),
14164 LOONGSON_BUILTIN_SUFFIX (pextrh, s, MIPS_V4HI_FTYPE_V4HI_USI),
14165 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14166 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14167 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14168 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14169 LOONGSON_BUILTIN_SUFFIX (pinsrh_0, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14170 LOONGSON_BUILTIN_SUFFIX (pinsrh_1, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14171 LOONGSON_BUILTIN_SUFFIX (pinsrh_2, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14172 LOONGSON_BUILTIN_SUFFIX (pinsrh_3, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14173 LOONGSON_BUILTIN (pmaddhw, MIPS_V2SI_FTYPE_V4HI_V4HI),
14174 LOONGSON_BUILTIN (pmaxsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14175 LOONGSON_BUILTIN (pmaxub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14176 LOONGSON_BUILTIN (pminsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14177 LOONGSON_BUILTIN (pminub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14178 LOONGSON_BUILTIN_SUFFIX (pmovmskb, u, MIPS_UV8QI_FTYPE_UV8QI),
14179 LOONGSON_BUILTIN_SUFFIX (pmovmskb, s, MIPS_V8QI_FTYPE_V8QI),
14180 LOONGSON_BUILTIN (pmulhuh, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14181 LOONGSON_BUILTIN (pmulhh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14182 LOONGSON_BUILTIN (pmullh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14183 LOONGSON_BUILTIN (pmuluw, MIPS_UDI_FTYPE_UV2SI_UV2SI),
14184 LOONGSON_BUILTIN (pasubub, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14185 LOONGSON_BUILTIN (biadd, MIPS_UV4HI_FTYPE_UV8QI),
14186 LOONGSON_BUILTIN (psadbh, MIPS_UV4HI_FTYPE_UV8QI_UV8QI),
14187 LOONGSON_BUILTIN_SUFFIX (pshufh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14188 LOONGSON_BUILTIN_SUFFIX (pshufh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14189 LOONGSON_BUILTIN_SUFFIX (psllh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14190 LOONGSON_BUILTIN_SUFFIX (psllh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14191 LOONGSON_BUILTIN_SUFFIX (psllw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14192 LOONGSON_BUILTIN_SUFFIX (psllw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14193 LOONGSON_BUILTIN_SUFFIX (psrah, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14194 LOONGSON_BUILTIN_SUFFIX (psrah, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14195 LOONGSON_BUILTIN_SUFFIX (psraw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14196 LOONGSON_BUILTIN_SUFFIX (psraw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14197 LOONGSON_BUILTIN_SUFFIX (psrlh, u, MIPS_UV4HI_FTYPE_UV4HI_UQI),
14198 LOONGSON_BUILTIN_SUFFIX (psrlh, s, MIPS_V4HI_FTYPE_V4HI_UQI),
14199 LOONGSON_BUILTIN_SUFFIX (psrlw, u, MIPS_UV2SI_FTYPE_UV2SI_UQI),
14200 LOONGSON_BUILTIN_SUFFIX (psrlw, s, MIPS_V2SI_FTYPE_V2SI_UQI),
14201 LOONGSON_BUILTIN_SUFFIX (psubw, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14202 LOONGSON_BUILTIN_SUFFIX (psubh, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14203 LOONGSON_BUILTIN_SUFFIX (psubb, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14204 LOONGSON_BUILTIN_SUFFIX (psubw, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14205 LOONGSON_BUILTIN_SUFFIX (psubh, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14206 LOONGSON_BUILTIN_SUFFIX (psubb, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14207 LOONGSON_BUILTIN_SUFFIX (psubd, u, MIPS_UDI_FTYPE_UDI_UDI),
14208 LOONGSON_BUILTIN_SUFFIX (psubd, s, MIPS_DI_FTYPE_DI_DI),
14209 LOONGSON_BUILTIN (psubsh, MIPS_V4HI_FTYPE_V4HI_V4HI),
14210 LOONGSON_BUILTIN (psubsb, MIPS_V8QI_FTYPE_V8QI_V8QI),
14211 LOONGSON_BUILTIN (psubush, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14212 LOONGSON_BUILTIN (psubusb, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14213 LOONGSON_BUILTIN_SUFFIX (punpckhbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14214 LOONGSON_BUILTIN_SUFFIX (punpckhhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14215 LOONGSON_BUILTIN_SUFFIX (punpckhwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14216 LOONGSON_BUILTIN_SUFFIX (punpckhbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14217 LOONGSON_BUILTIN_SUFFIX (punpckhhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14218 LOONGSON_BUILTIN_SUFFIX (punpckhwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14219 LOONGSON_BUILTIN_SUFFIX (punpcklbh, u, MIPS_UV8QI_FTYPE_UV8QI_UV8QI),
14220 LOONGSON_BUILTIN_SUFFIX (punpcklhw, u, MIPS_UV4HI_FTYPE_UV4HI_UV4HI),
14221 LOONGSON_BUILTIN_SUFFIX (punpcklwd, u, MIPS_UV2SI_FTYPE_UV2SI_UV2SI),
14222 LOONGSON_BUILTIN_SUFFIX (punpcklbh, s, MIPS_V8QI_FTYPE_V8QI_V8QI),
14223 LOONGSON_BUILTIN_SUFFIX (punpcklhw, s, MIPS_V4HI_FTYPE_V4HI_V4HI),
14224 LOONGSON_BUILTIN_SUFFIX (punpcklwd, s, MIPS_V2SI_FTYPE_V2SI_V2SI),
14225
14226 /* Sundry other built-in functions. */
14227 DIRECT_NO_TARGET_BUILTIN (cache, MIPS_VOID_FTYPE_SI_CVPOINTER, cache)
14228 };
14229
14230 /* Index I is the function declaration for mips_builtins[I], or null if the
14231 function isn't defined on this target. */
14232 static GTY(()) tree mips_builtin_decls[ARRAY_SIZE (mips_builtins)];
14233
14234 /* MODE is a vector mode whose elements have type TYPE. Return the type
14235 of the vector itself. */
14236
14237 static tree
14238 mips_builtin_vector_type (tree type, machine_mode mode)
14239 {
14240 static tree types[2 * (int) MAX_MACHINE_MODE];
14241 int mode_index;
14242
14243 mode_index = (int) mode;
14244
14245 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type))
14246 mode_index += MAX_MACHINE_MODE;
14247
14248 if (types[mode_index] == NULL_TREE)
14249 types[mode_index] = build_vector_type_for_mode (type, mode);
14250 return types[mode_index];
14251 }
14252
14253 /* Return a type for 'const volatile void *'. */
14254
14255 static tree
14256 mips_build_cvpointer_type (void)
14257 {
14258 static tree cache;
14259
14260 if (cache == NULL_TREE)
14261 cache = build_pointer_type (build_qualified_type
14262 (void_type_node,
14263 TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE));
14264 return cache;
14265 }
14266
14267 /* Source-level argument types. */
14268 #define MIPS_ATYPE_VOID void_type_node
14269 #define MIPS_ATYPE_INT integer_type_node
14270 #define MIPS_ATYPE_POINTER ptr_type_node
14271 #define MIPS_ATYPE_CVPOINTER mips_build_cvpointer_type ()
14272
14273 /* Standard mode-based argument types. */
14274 #define MIPS_ATYPE_UQI unsigned_intQI_type_node
14275 #define MIPS_ATYPE_SI intSI_type_node
14276 #define MIPS_ATYPE_USI unsigned_intSI_type_node
14277 #define MIPS_ATYPE_DI intDI_type_node
14278 #define MIPS_ATYPE_UDI unsigned_intDI_type_node
14279 #define MIPS_ATYPE_SF float_type_node
14280 #define MIPS_ATYPE_DF double_type_node
14281
14282 /* Vector argument types. */
14283 #define MIPS_ATYPE_V2SF mips_builtin_vector_type (float_type_node, V2SFmode)
14284 #define MIPS_ATYPE_V2HI mips_builtin_vector_type (intHI_type_node, V2HImode)
14285 #define MIPS_ATYPE_V2SI mips_builtin_vector_type (intSI_type_node, V2SImode)
14286 #define MIPS_ATYPE_V4QI mips_builtin_vector_type (intQI_type_node, V4QImode)
14287 #define MIPS_ATYPE_V4HI mips_builtin_vector_type (intHI_type_node, V4HImode)
14288 #define MIPS_ATYPE_V8QI mips_builtin_vector_type (intQI_type_node, V8QImode)
14289 #define MIPS_ATYPE_UV2SI \
14290 mips_builtin_vector_type (unsigned_intSI_type_node, V2SImode)
14291 #define MIPS_ATYPE_UV4HI \
14292 mips_builtin_vector_type (unsigned_intHI_type_node, V4HImode)
14293 #define MIPS_ATYPE_UV8QI \
14294 mips_builtin_vector_type (unsigned_intQI_type_node, V8QImode)
14295
14296 /* MIPS_FTYPE_ATYPESN takes N MIPS_FTYPES-like type codes and lists
14297 their associated MIPS_ATYPEs. */
14298 #define MIPS_FTYPE_ATYPES1(A, B) \
14299 MIPS_ATYPE_##A, MIPS_ATYPE_##B
14300
14301 #define MIPS_FTYPE_ATYPES2(A, B, C) \
14302 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C
14303
14304 #define MIPS_FTYPE_ATYPES3(A, B, C, D) \
14305 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D
14306
14307 #define MIPS_FTYPE_ATYPES4(A, B, C, D, E) \
14308 MIPS_ATYPE_##A, MIPS_ATYPE_##B, MIPS_ATYPE_##C, MIPS_ATYPE_##D, \
14309 MIPS_ATYPE_##E
14310
14311 /* Return the function type associated with function prototype TYPE. */
14312
14313 static tree
14314 mips_build_function_type (enum mips_function_type type)
14315 {
14316 static tree types[(int) MIPS_MAX_FTYPE_MAX];
14317
14318 if (types[(int) type] == NULL_TREE)
14319 switch (type)
14320 {
14321 #define DEF_MIPS_FTYPE(NUM, ARGS) \
14322 case MIPS_FTYPE_NAME##NUM ARGS: \
14323 types[(int) type] \
14324 = build_function_type_list (MIPS_FTYPE_ATYPES##NUM ARGS, \
14325 NULL_TREE); \
14326 break;
14327 #include "config/mips/mips-ftypes.def"
14328 #undef DEF_MIPS_FTYPE
14329 default:
14330 gcc_unreachable ();
14331 }
14332
14333 return types[(int) type];
14334 }
14335
14336 /* Implement TARGET_INIT_BUILTINS. */
14337
14338 static void
14339 mips_init_builtins (void)
14340 {
14341 const struct mips_builtin_description *d;
14342 unsigned int i;
14343
14344 /* Iterate through all of the bdesc arrays, initializing all of the
14345 builtin functions. */
14346 for (i = 0; i < ARRAY_SIZE (mips_builtins); i++)
14347 {
14348 d = &mips_builtins[i];
14349 if (d->avail ())
14350 mips_builtin_decls[i]
14351 = add_builtin_function (d->name,
14352 mips_build_function_type (d->function_type),
14353 i, BUILT_IN_MD, NULL, NULL);
14354 }
14355 }
14356
14357 /* Implement TARGET_BUILTIN_DECL. */
14358
14359 static tree
14360 mips_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED)
14361 {
14362 if (code >= ARRAY_SIZE (mips_builtins))
14363 return error_mark_node;
14364 return mips_builtin_decls[code];
14365 }
14366
14367 /* Take argument ARGNO from EXP's argument list and convert it into
14368 an expand operand. Store the operand in *OP. */
14369
14370 static void
14371 mips_prepare_builtin_arg (struct expand_operand *op, tree exp,
14372 unsigned int argno)
14373 {
14374 tree arg;
14375 rtx value;
14376
14377 arg = CALL_EXPR_ARG (exp, argno);
14378 value = expand_normal (arg);
14379 create_input_operand (op, value, TYPE_MODE (TREE_TYPE (arg)));
14380 }
14381
14382 /* Expand instruction ICODE as part of a built-in function sequence.
14383 Use the first NOPS elements of OPS as the instruction's operands.
14384 HAS_TARGET_P is true if operand 0 is a target; it is false if the
14385 instruction has no target.
14386
14387 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */
14388
14389 static rtx
14390 mips_expand_builtin_insn (enum insn_code icode, unsigned int nops,
14391 struct expand_operand *ops, bool has_target_p)
14392 {
14393 if (!maybe_expand_insn (icode, nops, ops))
14394 {
14395 error ("invalid argument to built-in function");
14396 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx;
14397 }
14398 return has_target_p ? ops[0].value : const0_rtx;
14399 }
14400
14401 /* Expand a floating-point comparison for built-in function call EXP.
14402 The first NARGS arguments are the values to be compared. ICODE is
14403 the .md pattern that does the comparison and COND is the condition
14404 that is being tested. Return an rtx for the result. */
14405
14406 static rtx
14407 mips_expand_builtin_compare_1 (enum insn_code icode,
14408 enum mips_fp_condition cond,
14409 tree exp, int nargs)
14410 {
14411 struct expand_operand ops[MAX_RECOG_OPERANDS];
14412 rtx output;
14413 int opno, argno;
14414
14415 /* The instruction should have a target operand, an operand for each
14416 argument, and an operand for COND. */
14417 gcc_assert (nargs + 2 == insn_data[(int) icode].n_generator_args);
14418
14419 output = mips_allocate_fcc (insn_data[(int) icode].operand[0].mode);
14420 opno = 0;
14421 create_fixed_operand (&ops[opno++], output);
14422 for (argno = 0; argno < nargs; argno++)
14423 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14424 create_integer_operand (&ops[opno++], (int) cond);
14425 return mips_expand_builtin_insn (icode, opno, ops, true);
14426 }
14427
14428 /* Expand a MIPS_BUILTIN_DIRECT or MIPS_BUILTIN_DIRECT_NO_TARGET function;
14429 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function
14430 and ICODE is the code of the associated .md pattern. TARGET, if nonnull,
14431 suggests a good place to put the result. */
14432
14433 static rtx
14434 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree exp,
14435 bool has_target_p)
14436 {
14437 struct expand_operand ops[MAX_RECOG_OPERANDS];
14438 int opno, argno;
14439
14440 /* Map any target to operand 0. */
14441 opno = 0;
14442 if (has_target_p)
14443 create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp)));
14444
14445 /* Map the arguments to the other operands. */
14446 gcc_assert (opno + call_expr_nargs (exp)
14447 == insn_data[icode].n_generator_args);
14448 for (argno = 0; argno < call_expr_nargs (exp); argno++)
14449 mips_prepare_builtin_arg (&ops[opno++], exp, argno);
14450
14451 return mips_expand_builtin_insn (icode, opno, ops, has_target_p);
14452 }
14453
14454 /* Expand a __builtin_mips_movt_*_ps or __builtin_mips_movf_*_ps
14455 function; TYPE says which. EXP is the CALL_EXPR that calls the
14456 function, ICODE is the instruction that should be used to compare
14457 the first two arguments, and COND is the condition it should test.
14458 TARGET, if nonnull, suggests a good place to put the result. */
14459
14460 static rtx
14461 mips_expand_builtin_movtf (enum mips_builtin_type type,
14462 enum insn_code icode, enum mips_fp_condition cond,
14463 rtx target, tree exp)
14464 {
14465 struct expand_operand ops[4];
14466 rtx cmp_result;
14467
14468 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp, 2);
14469 create_output_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (exp)));
14470 if (type == MIPS_BUILTIN_MOVT)
14471 {
14472 mips_prepare_builtin_arg (&ops[2], exp, 2);
14473 mips_prepare_builtin_arg (&ops[1], exp, 3);
14474 }
14475 else
14476 {
14477 mips_prepare_builtin_arg (&ops[1], exp, 2);
14478 mips_prepare_builtin_arg (&ops[2], exp, 3);
14479 }
14480 create_fixed_operand (&ops[3], cmp_result);
14481 return mips_expand_builtin_insn (CODE_FOR_mips_cond_move_tf_ps,
14482 4, ops, true);
14483 }
14484
14485 /* Move VALUE_IF_TRUE into TARGET if CONDITION is true; move VALUE_IF_FALSE
14486 into TARGET otherwise. Return TARGET. */
14487
14488 static rtx
14489 mips_builtin_branch_and_move (rtx condition, rtx target,
14490 rtx value_if_true, rtx value_if_false)
14491 {
14492 rtx_code_label *true_label, *done_label;
14493
14494 true_label = gen_label_rtx ();
14495 done_label = gen_label_rtx ();
14496
14497 /* First assume that CONDITION is false. */
14498 mips_emit_move (target, value_if_false);
14499
14500 /* Branch to TRUE_LABEL if CONDITION is true and DONE_LABEL otherwise. */
14501 emit_jump_insn (gen_condjump (condition, true_label));
14502 emit_jump_insn (gen_jump (done_label));
14503 emit_barrier ();
14504
14505 /* Fix TARGET if CONDITION is true. */
14506 emit_label (true_label);
14507 mips_emit_move (target, value_if_true);
14508
14509 emit_label (done_label);
14510 return target;
14511 }
14512
14513 /* Expand a comparison built-in function of type BUILTIN_TYPE. EXP is
14514 the CALL_EXPR that calls the function, ICODE is the code of the
14515 comparison instruction, and COND is the condition it should test.
14516 TARGET, if nonnull, suggests a good place to put the boolean result. */
14517
14518 static rtx
14519 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
14520 enum insn_code icode, enum mips_fp_condition cond,
14521 rtx target, tree exp)
14522 {
14523 rtx offset, condition, cmp_result;
14524
14525 if (target == 0 || GET_MODE (target) != SImode)
14526 target = gen_reg_rtx (SImode);
14527 cmp_result = mips_expand_builtin_compare_1 (icode, cond, exp,
14528 call_expr_nargs (exp));
14529
14530 /* If the comparison sets more than one register, we define the result
14531 to be 0 if all registers are false and -1 if all registers are true.
14532 The value of the complete result is indeterminate otherwise. */
14533 switch (builtin_type)
14534 {
14535 case MIPS_BUILTIN_CMP_ALL:
14536 condition = gen_rtx_NE (VOIDmode, cmp_result, constm1_rtx);
14537 return mips_builtin_branch_and_move (condition, target,
14538 const0_rtx, const1_rtx);
14539
14540 case MIPS_BUILTIN_CMP_UPPER:
14541 case MIPS_BUILTIN_CMP_LOWER:
14542 offset = GEN_INT (builtin_type == MIPS_BUILTIN_CMP_UPPER);
14543 condition = gen_single_cc (cmp_result, offset);
14544 return mips_builtin_branch_and_move (condition, target,
14545 const1_rtx, const0_rtx);
14546
14547 default:
14548 condition = gen_rtx_NE (VOIDmode, cmp_result, const0_rtx);
14549 return mips_builtin_branch_and_move (condition, target,
14550 const1_rtx, const0_rtx);
14551 }
14552 }
14553
14554 /* Expand a bposge built-in function of type BUILTIN_TYPE. TARGET,
14555 if nonnull, suggests a good place to put the boolean result. */
14556
14557 static rtx
14558 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
14559 {
14560 rtx condition, cmp_result;
14561 int cmp_value;
14562
14563 if (target == 0 || GET_MODE (target) != SImode)
14564 target = gen_reg_rtx (SImode);
14565
14566 cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
14567
14568 if (builtin_type == MIPS_BUILTIN_BPOSGE32)
14569 cmp_value = 32;
14570 else
14571 gcc_assert (0);
14572
14573 condition = gen_rtx_GE (VOIDmode, cmp_result, GEN_INT (cmp_value));
14574 return mips_builtin_branch_and_move (condition, target,
14575 const1_rtx, const0_rtx);
14576 }
14577
14578 /* Implement TARGET_EXPAND_BUILTIN. */
14579
14580 static rtx
14581 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
14582 machine_mode mode, int ignore)
14583 {
14584 tree fndecl;
14585 unsigned int fcode, avail;
14586 const struct mips_builtin_description *d;
14587
14588 fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
14589 fcode = DECL_FUNCTION_CODE (fndecl);
14590 gcc_assert (fcode < ARRAY_SIZE (mips_builtins));
14591 d = &mips_builtins[fcode];
14592 avail = d->avail ();
14593 gcc_assert (avail != 0);
14594 if (TARGET_MIPS16 && !(avail & BUILTIN_AVAIL_MIPS16))
14595 {
14596 error ("built-in function %qE not supported for MIPS16",
14597 DECL_NAME (fndecl));
14598 return ignore ? const0_rtx : CONST0_RTX (mode);
14599 }
14600 switch (d->builtin_type)
14601 {
14602 case MIPS_BUILTIN_DIRECT:
14603 return mips_expand_builtin_direct (d->icode, target, exp, true);
14604
14605 case MIPS_BUILTIN_DIRECT_NO_TARGET:
14606 return mips_expand_builtin_direct (d->icode, target, exp, false);
14607
14608 case MIPS_BUILTIN_MOVT:
14609 case MIPS_BUILTIN_MOVF:
14610 return mips_expand_builtin_movtf (d->builtin_type, d->icode,
14611 d->cond, target, exp);
14612
14613 case MIPS_BUILTIN_CMP_ANY:
14614 case MIPS_BUILTIN_CMP_ALL:
14615 case MIPS_BUILTIN_CMP_UPPER:
14616 case MIPS_BUILTIN_CMP_LOWER:
14617 case MIPS_BUILTIN_CMP_SINGLE:
14618 return mips_expand_builtin_compare (d->builtin_type, d->icode,
14619 d->cond, target, exp);
14620
14621 case MIPS_BUILTIN_BPOSGE32:
14622 return mips_expand_builtin_bposge (d->builtin_type, target);
14623 }
14624 gcc_unreachable ();
14625 }
14626 \f
14627 /* An entry in the MIPS16 constant pool. VALUE is the pool constant,
14628 MODE is its mode, and LABEL is the CODE_LABEL associated with it. */
14629 struct mips16_constant {
14630 struct mips16_constant *next;
14631 rtx value;
14632 rtx_code_label *label;
14633 machine_mode mode;
14634 };
14635
14636 /* Information about an incomplete MIPS16 constant pool. FIRST is the
14637 first constant, HIGHEST_ADDRESS is the highest address that the first
14638 byte of the pool can have, and INSN_ADDRESS is the current instruction
14639 address. */
14640 struct mips16_constant_pool {
14641 struct mips16_constant *first;
14642 int highest_address;
14643 int insn_address;
14644 };
14645
14646 /* Add constant VALUE to POOL and return its label. MODE is the
14647 value's mode (used for CONST_INTs, etc.). */
14648
14649 static rtx_code_label *
14650 mips16_add_constant (struct mips16_constant_pool *pool,
14651 rtx value, machine_mode mode)
14652 {
14653 struct mips16_constant **p, *c;
14654 bool first_of_size_p;
14655
14656 /* See whether the constant is already in the pool. If so, return the
14657 existing label, otherwise leave P pointing to the place where the
14658 constant should be added.
14659
14660 Keep the pool sorted in increasing order of mode size so that we can
14661 reduce the number of alignments needed. */
14662 first_of_size_p = true;
14663 for (p = &pool->first; *p != 0; p = &(*p)->next)
14664 {
14665 if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
14666 return (*p)->label;
14667 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
14668 break;
14669 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
14670 first_of_size_p = false;
14671 }
14672
14673 /* In the worst case, the constant needed by the earliest instruction
14674 will end up at the end of the pool. The entire pool must then be
14675 accessible from that instruction.
14676
14677 When adding the first constant, set the pool's highest address to
14678 the address of the first out-of-range byte. Adjust this address
14679 downwards each time a new constant is added. */
14680 if (pool->first == 0)
14681 /* For LWPC, ADDIUPC and DADDIUPC, the base PC value is the address
14682 of the instruction with the lowest two bits clear. The base PC
14683 value for LDPC has the lowest three bits clear. Assume the worst
14684 case here; namely that the PC-relative instruction occupies the
14685 last 2 bytes in an aligned word. */
14686 pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
14687 pool->highest_address -= GET_MODE_SIZE (mode);
14688 if (first_of_size_p)
14689 /* Take into account the worst possible padding due to alignment. */
14690 pool->highest_address -= GET_MODE_SIZE (mode) - 1;
14691
14692 /* Create a new entry. */
14693 c = XNEW (struct mips16_constant);
14694 c->value = value;
14695 c->mode = mode;
14696 c->label = gen_label_rtx ();
14697 c->next = *p;
14698 *p = c;
14699
14700 return c->label;
14701 }
14702
14703 /* Output constant VALUE after instruction INSN and return the last
14704 instruction emitted. MODE is the mode of the constant. */
14705
14706 static rtx_insn *
14707 mips16_emit_constants_1 (machine_mode mode, rtx value, rtx_insn *insn)
14708 {
14709 if (SCALAR_INT_MODE_P (mode) || ALL_SCALAR_FIXED_POINT_MODE_P (mode))
14710 {
14711 rtx size = GEN_INT (GET_MODE_SIZE (mode));
14712 return emit_insn_after (gen_consttable_int (value, size), insn);
14713 }
14714
14715 if (SCALAR_FLOAT_MODE_P (mode))
14716 return emit_insn_after (gen_consttable_float (value), insn);
14717
14718 if (VECTOR_MODE_P (mode))
14719 {
14720 int i;
14721
14722 for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
14723 insn = mips16_emit_constants_1 (GET_MODE_INNER (mode),
14724 CONST_VECTOR_ELT (value, i), insn);
14725 return insn;
14726 }
14727
14728 gcc_unreachable ();
14729 }
14730
14731 /* Dump out the constants in CONSTANTS after INSN. */
14732
14733 static void
14734 mips16_emit_constants (struct mips16_constant *constants, rtx_insn *insn)
14735 {
14736 struct mips16_constant *c, *next;
14737 int align;
14738
14739 align = 0;
14740 for (c = constants; c != NULL; c = next)
14741 {
14742 /* If necessary, increase the alignment of PC. */
14743 if (align < GET_MODE_SIZE (c->mode))
14744 {
14745 int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
14746 insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
14747 }
14748 align = GET_MODE_SIZE (c->mode);
14749
14750 insn = emit_label_after (c->label, insn);
14751 insn = mips16_emit_constants_1 (c->mode, c->value, insn);
14752
14753 next = c->next;
14754 free (c);
14755 }
14756
14757 emit_barrier_after (insn);
14758 }
14759
14760 /* Return the length of instruction INSN. */
14761
14762 static int
14763 mips16_insn_length (rtx_insn *insn)
14764 {
14765 if (JUMP_TABLE_DATA_P (insn))
14766 {
14767 rtx body = PATTERN (insn);
14768 if (GET_CODE (body) == ADDR_VEC)
14769 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
14770 else if (GET_CODE (body) == ADDR_DIFF_VEC)
14771 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
14772 else
14773 gcc_unreachable ();
14774 }
14775 return get_attr_length (insn);
14776 }
14777
14778 /* If *X is a symbolic constant that refers to the constant pool, add
14779 the constant to POOL and rewrite *X to use the constant's label. */
14780
14781 static void
14782 mips16_rewrite_pool_constant (struct mips16_constant_pool *pool, rtx *x)
14783 {
14784 rtx base, offset;
14785 rtx_code_label *label;
14786
14787 split_const (*x, &base, &offset);
14788 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
14789 {
14790 label = mips16_add_constant (pool, copy_rtx (get_pool_constant (base)),
14791 get_pool_mode (base));
14792 base = gen_rtx_LABEL_REF (Pmode, label);
14793 *x = mips_unspec_address_offset (base, offset, SYMBOL_PC_RELATIVE);
14794 }
14795 }
14796
14797 /* Rewrite INSN so that constant pool references refer to the constant's
14798 label instead. */
14799
14800 static void
14801 mips16_rewrite_pool_refs (rtx_insn *insn, struct mips16_constant_pool *pool)
14802 {
14803 subrtx_ptr_iterator::array_type array;
14804 FOR_EACH_SUBRTX_PTR (iter, array, &PATTERN (insn), ALL)
14805 {
14806 rtx *loc = *iter;
14807
14808 if (force_to_mem_operand (*loc, Pmode))
14809 {
14810 rtx mem = force_const_mem (GET_MODE (*loc), *loc);
14811 validate_change (insn, loc, mem, false);
14812 }
14813
14814 if (MEM_P (*loc))
14815 {
14816 mips16_rewrite_pool_constant (pool, &XEXP (*loc, 0));
14817 iter.skip_subrtxes ();
14818 }
14819 else
14820 {
14821 if (TARGET_MIPS16_TEXT_LOADS)
14822 mips16_rewrite_pool_constant (pool, loc);
14823 if (GET_CODE (*loc) == CONST
14824 /* Don't rewrite the __mips16_rdwr symbol. */
14825 || (GET_CODE (*loc) == UNSPEC
14826 && XINT (*loc, 1) == UNSPEC_TLS_GET_TP))
14827 iter.skip_subrtxes ();
14828 }
14829 }
14830 }
14831
14832 /* Return whether CFG is used in mips_reorg. */
14833
14834 static bool
14835 mips_cfg_in_reorg (void)
14836 {
14837 return (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
14838 || TARGET_RELAX_PIC_CALLS);
14839 }
14840
14841 /* Build MIPS16 constant pools. Split the instructions if SPLIT_P,
14842 otherwise assume that they are already split. */
14843
14844 static void
14845 mips16_lay_out_constants (bool split_p)
14846 {
14847 struct mips16_constant_pool pool;
14848 rtx_insn *insn, *barrier;
14849
14850 if (!TARGET_MIPS16_PCREL_LOADS)
14851 return;
14852
14853 if (split_p)
14854 {
14855 if (mips_cfg_in_reorg ())
14856 split_all_insns ();
14857 else
14858 split_all_insns_noflow ();
14859 }
14860 barrier = 0;
14861 memset (&pool, 0, sizeof (pool));
14862 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14863 {
14864 /* Rewrite constant pool references in INSN. */
14865 if (USEFUL_INSN_P (insn))
14866 mips16_rewrite_pool_refs (insn, &pool);
14867
14868 pool.insn_address += mips16_insn_length (insn);
14869
14870 if (pool.first != NULL)
14871 {
14872 /* If there are no natural barriers between the first user of
14873 the pool and the highest acceptable address, we'll need to
14874 create a new instruction to jump around the constant pool.
14875 In the worst case, this instruction will be 4 bytes long.
14876
14877 If it's too late to do this transformation after INSN,
14878 do it immediately before INSN. */
14879 if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
14880 {
14881 rtx_code_label *label;
14882 rtx_insn *jump;
14883
14884 label = gen_label_rtx ();
14885
14886 jump = emit_jump_insn_before (gen_jump (label), insn);
14887 JUMP_LABEL (jump) = label;
14888 LABEL_NUSES (label) = 1;
14889 barrier = emit_barrier_after (jump);
14890
14891 emit_label_after (label, barrier);
14892 pool.insn_address += 4;
14893 }
14894
14895 /* See whether the constant pool is now out of range of the first
14896 user. If so, output the constants after the previous barrier.
14897 Note that any instructions between BARRIER and INSN (inclusive)
14898 will use negative offsets to refer to the pool. */
14899 if (pool.insn_address > pool.highest_address)
14900 {
14901 mips16_emit_constants (pool.first, barrier);
14902 pool.first = NULL;
14903 barrier = 0;
14904 }
14905 else if (BARRIER_P (insn))
14906 barrier = insn;
14907 }
14908 }
14909 mips16_emit_constants (pool.first, get_last_insn ());
14910 }
14911 \f
14912 /* Return true if it is worth r10k_simplify_address's while replacing
14913 an address with X. We are looking for constants, and for addresses
14914 at a known offset from the incoming stack pointer. */
14915
14916 static bool
14917 r10k_simplified_address_p (rtx x)
14918 {
14919 if (GET_CODE (x) == PLUS && CONST_INT_P (XEXP (x, 1)))
14920 x = XEXP (x, 0);
14921 return x == virtual_incoming_args_rtx || CONSTANT_P (x);
14922 }
14923
14924 /* X is an expression that appears in INSN. Try to use the UD chains
14925 to simplify it, returning the simplified form on success and the
14926 original form otherwise. Replace the incoming value of $sp with
14927 virtual_incoming_args_rtx (which should never occur in X otherwise). */
14928
14929 static rtx
14930 r10k_simplify_address (rtx x, rtx_insn *insn)
14931 {
14932 rtx newx, op0, op1, set, note;
14933 rtx_insn *def_insn;
14934 df_ref use, def;
14935 struct df_link *defs;
14936
14937 newx = NULL_RTX;
14938 if (UNARY_P (x))
14939 {
14940 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14941 if (op0 != XEXP (x, 0))
14942 newx = simplify_gen_unary (GET_CODE (x), GET_MODE (x),
14943 op0, GET_MODE (XEXP (x, 0)));
14944 }
14945 else if (BINARY_P (x))
14946 {
14947 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14948 op1 = r10k_simplify_address (XEXP (x, 1), insn);
14949 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
14950 newx = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
14951 }
14952 else if (GET_CODE (x) == LO_SUM)
14953 {
14954 /* LO_SUMs can be offset from HIGHs, if we know they won't
14955 overflow. See mips_classify_address for the rationale behind
14956 the lax check. */
14957 op0 = r10k_simplify_address (XEXP (x, 0), insn);
14958 if (GET_CODE (op0) == HIGH)
14959 newx = XEXP (x, 1);
14960 }
14961 else if (REG_P (x))
14962 {
14963 /* Uses are recorded by regno_reg_rtx, not X itself. */
14964 use = df_find_use (insn, regno_reg_rtx[REGNO (x)]);
14965 gcc_assert (use);
14966 defs = DF_REF_CHAIN (use);
14967
14968 /* Require a single definition. */
14969 if (defs && defs->next == NULL)
14970 {
14971 def = defs->ref;
14972 if (DF_REF_IS_ARTIFICIAL (def))
14973 {
14974 /* Replace the incoming value of $sp with
14975 virtual_incoming_args_rtx. */
14976 if (x == stack_pointer_rtx
14977 && DF_REF_BB (def) == ENTRY_BLOCK_PTR_FOR_FN (cfun))
14978 newx = virtual_incoming_args_rtx;
14979 }
14980 else if (dominated_by_p (CDI_DOMINATORS, DF_REF_BB (use),
14981 DF_REF_BB (def)))
14982 {
14983 /* Make sure that DEF_INSN is a single set of REG. */
14984 def_insn = DF_REF_INSN (def);
14985 if (NONJUMP_INSN_P (def_insn))
14986 {
14987 set = single_set (def_insn);
14988 if (set && rtx_equal_p (SET_DEST (set), x))
14989 {
14990 /* Prefer to use notes, since the def-use chains
14991 are often shorter. */
14992 note = find_reg_equal_equiv_note (def_insn);
14993 if (note)
14994 newx = XEXP (note, 0);
14995 else
14996 newx = SET_SRC (set);
14997 newx = r10k_simplify_address (newx, def_insn);
14998 }
14999 }
15000 }
15001 }
15002 }
15003 if (newx && r10k_simplified_address_p (newx))
15004 return newx;
15005 return x;
15006 }
15007
15008 /* Return true if ADDRESS is known to be an uncached address
15009 on R10K systems. */
15010
15011 static bool
15012 r10k_uncached_address_p (unsigned HOST_WIDE_INT address)
15013 {
15014 unsigned HOST_WIDE_INT upper;
15015
15016 /* Check for KSEG1. */
15017 if (address + 0x60000000 < 0x20000000)
15018 return true;
15019
15020 /* Check for uncached XKPHYS addresses. */
15021 if (Pmode == DImode)
15022 {
15023 upper = (address >> 40) & 0xf9ffff;
15024 if (upper == 0x900000 || upper == 0xb80000)
15025 return true;
15026 }
15027 return false;
15028 }
15029
15030 /* Return true if we can prove that an access to address X in instruction
15031 INSN would be safe from R10K speculation. This X is a general
15032 expression; it might not be a legitimate address. */
15033
15034 static bool
15035 r10k_safe_address_p (rtx x, rtx_insn *insn)
15036 {
15037 rtx base, offset;
15038 HOST_WIDE_INT offset_val;
15039
15040 x = r10k_simplify_address (x, insn);
15041
15042 /* Check for references to the stack frame. It doesn't really matter
15043 how much of the frame has been allocated at INSN; -mr10k-cache-barrier
15044 allows us to assume that accesses to any part of the eventual frame
15045 is safe from speculation at any point in the function. */
15046 mips_split_plus (x, &base, &offset_val);
15047 if (base == virtual_incoming_args_rtx
15048 && offset_val >= -cfun->machine->frame.total_size
15049 && offset_val < cfun->machine->frame.args_size)
15050 return true;
15051
15052 /* Check for uncached addresses. */
15053 if (CONST_INT_P (x))
15054 return r10k_uncached_address_p (INTVAL (x));
15055
15056 /* Check for accesses to a static object. */
15057 split_const (x, &base, &offset);
15058 return offset_within_block_p (base, INTVAL (offset));
15059 }
15060
15061 /* Return true if a MEM with MEM_EXPR EXPR and MEM_OFFSET OFFSET is
15062 an in-range access to an automatic variable, or to an object with
15063 a link-time-constant address. */
15064
15065 static bool
15066 r10k_safe_mem_expr_p (tree expr, unsigned HOST_WIDE_INT offset)
15067 {
15068 HOST_WIDE_INT bitoffset, bitsize;
15069 tree inner, var_offset;
15070 machine_mode mode;
15071 int unsigned_p, volatile_p;
15072
15073 inner = get_inner_reference (expr, &bitsize, &bitoffset, &var_offset, &mode,
15074 &unsigned_p, &volatile_p, false);
15075 if (!DECL_P (inner) || !DECL_SIZE_UNIT (inner) || var_offset)
15076 return false;
15077
15078 offset += bitoffset / BITS_PER_UNIT;
15079 return offset < tree_to_uhwi (DECL_SIZE_UNIT (inner));
15080 }
15081
15082 /* Return true if X contains a MEM that is not safe from R10K speculation.
15083 INSN is the instruction that contains X. */
15084
15085 static bool
15086 r10k_needs_protection_p_1 (rtx x, rtx_insn *insn)
15087 {
15088 subrtx_var_iterator::array_type array;
15089 FOR_EACH_SUBRTX_VAR (iter, array, x, NONCONST)
15090 {
15091 rtx mem = *iter;
15092 if (MEM_P (mem))
15093 {
15094 if ((MEM_EXPR (mem)
15095 && MEM_OFFSET_KNOWN_P (mem)
15096 && r10k_safe_mem_expr_p (MEM_EXPR (mem), MEM_OFFSET (mem)))
15097 || r10k_safe_address_p (XEXP (mem, 0), insn))
15098 iter.skip_subrtxes ();
15099 else
15100 return true;
15101 }
15102 }
15103 return false;
15104 }
15105
15106 /* A note_stores callback for which DATA points to an instruction pointer.
15107 If *DATA is nonnull, make it null if it X contains a MEM that is not
15108 safe from R10K speculation. */
15109
15110 static void
15111 r10k_needs_protection_p_store (rtx x, const_rtx pat ATTRIBUTE_UNUSED,
15112 void *data)
15113 {
15114 rtx_insn **insn_ptr;
15115
15116 insn_ptr = (rtx_insn **) data;
15117 if (*insn_ptr && r10k_needs_protection_p_1 (x, *insn_ptr))
15118 *insn_ptr = NULL;
15119 }
15120
15121 /* X is the pattern of a call instruction. Return true if the call is
15122 not to a declared function. */
15123
15124 static bool
15125 r10k_needs_protection_p_call (const_rtx x)
15126 {
15127 subrtx_iterator::array_type array;
15128 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
15129 {
15130 const_rtx mem = *iter;
15131 if (MEM_P (mem))
15132 {
15133 const_rtx addr = XEXP (mem, 0);
15134 if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DECL (addr))
15135 iter.skip_subrtxes ();
15136 else
15137 return true;
15138 }
15139 }
15140 return false;
15141 }
15142
15143 /* Return true if instruction INSN needs to be protected by an R10K
15144 cache barrier. */
15145
15146 static bool
15147 r10k_needs_protection_p (rtx_insn *insn)
15148 {
15149 if (CALL_P (insn))
15150 return r10k_needs_protection_p_call (PATTERN (insn));
15151
15152 if (mips_r10k_cache_barrier == R10K_CACHE_BARRIER_STORE)
15153 {
15154 note_stores (PATTERN (insn), r10k_needs_protection_p_store, &insn);
15155 return insn == NULL_RTX;
15156 }
15157
15158 return r10k_needs_protection_p_1 (PATTERN (insn), insn);
15159 }
15160
15161 /* Return true if BB is only reached by blocks in PROTECTED_BBS and if every
15162 edge is unconditional. */
15163
15164 static bool
15165 r10k_protected_bb_p (basic_block bb, sbitmap protected_bbs)
15166 {
15167 edge_iterator ei;
15168 edge e;
15169
15170 FOR_EACH_EDGE (e, ei, bb->preds)
15171 if (!single_succ_p (e->src)
15172 || !bitmap_bit_p (protected_bbs, e->src->index)
15173 || (e->flags & EDGE_COMPLEX) != 0)
15174 return false;
15175 return true;
15176 }
15177
15178 /* Implement -mr10k-cache-barrier= for the current function. */
15179
15180 static void
15181 r10k_insert_cache_barriers (void)
15182 {
15183 int *rev_post_order;
15184 unsigned int i, n;
15185 basic_block bb;
15186 sbitmap protected_bbs;
15187 rtx_insn *insn, *end;
15188 rtx unprotected_region;
15189
15190 if (TARGET_MIPS16)
15191 {
15192 sorry ("%qs does not support MIPS16 code", "-mr10k-cache-barrier");
15193 return;
15194 }
15195
15196 /* Calculate dominators. */
15197 calculate_dominance_info (CDI_DOMINATORS);
15198
15199 /* Bit X of PROTECTED_BBS is set if the last operation in basic block
15200 X is protected by a cache barrier. */
15201 protected_bbs = sbitmap_alloc (last_basic_block_for_fn (cfun));
15202 bitmap_clear (protected_bbs);
15203
15204 /* Iterate over the basic blocks in reverse post-order. */
15205 rev_post_order = XNEWVEC (int, last_basic_block_for_fn (cfun));
15206 n = pre_and_rev_post_order_compute (NULL, rev_post_order, false);
15207 for (i = 0; i < n; i++)
15208 {
15209 bb = BASIC_BLOCK_FOR_FN (cfun, rev_post_order[i]);
15210
15211 /* If this block is only reached by unconditional edges, and if the
15212 source of every edge is protected, the beginning of the block is
15213 also protected. */
15214 if (r10k_protected_bb_p (bb, protected_bbs))
15215 unprotected_region = NULL_RTX;
15216 else
15217 unprotected_region = pc_rtx;
15218 end = NEXT_INSN (BB_END (bb));
15219
15220 /* UNPROTECTED_REGION is:
15221
15222 - null if we are processing a protected region,
15223 - pc_rtx if we are processing an unprotected region but have
15224 not yet found the first instruction in it
15225 - the first instruction in an unprotected region otherwise. */
15226 for (insn = BB_HEAD (bb); insn != end; insn = NEXT_INSN (insn))
15227 {
15228 if (unprotected_region && USEFUL_INSN_P (insn))
15229 {
15230 if (recog_memoized (insn) == CODE_FOR_mips_cache)
15231 /* This CACHE instruction protects the following code. */
15232 unprotected_region = NULL_RTX;
15233 else
15234 {
15235 /* See if INSN is the first instruction in this
15236 unprotected region. */
15237 if (unprotected_region == pc_rtx)
15238 unprotected_region = insn;
15239
15240 /* See if INSN needs to be protected. If so,
15241 we must insert a cache barrier somewhere between
15242 PREV_INSN (UNPROTECTED_REGION) and INSN. It isn't
15243 clear which position is better performance-wise,
15244 but as a tie-breaker, we assume that it is better
15245 to allow delay slots to be back-filled where
15246 possible, and that it is better not to insert
15247 barriers in the middle of already-scheduled code.
15248 We therefore insert the barrier at the beginning
15249 of the region. */
15250 if (r10k_needs_protection_p (insn))
15251 {
15252 emit_insn_before (gen_r10k_cache_barrier (),
15253 unprotected_region);
15254 unprotected_region = NULL_RTX;
15255 }
15256 }
15257 }
15258
15259 if (CALL_P (insn))
15260 /* The called function is not required to protect the exit path.
15261 The code that follows a call is therefore unprotected. */
15262 unprotected_region = pc_rtx;
15263 }
15264
15265 /* Record whether the end of this block is protected. */
15266 if (unprotected_region == NULL_RTX)
15267 bitmap_set_bit (protected_bbs, bb->index);
15268 }
15269 XDELETEVEC (rev_post_order);
15270
15271 sbitmap_free (protected_bbs);
15272
15273 free_dominance_info (CDI_DOMINATORS);
15274 }
15275 \f
15276 /* If INSN is a call, return the underlying CALL expr. Return NULL_RTX
15277 otherwise. If INSN has two call rtx, then store the second one in
15278 SECOND_CALL. */
15279
15280 static rtx
15281 mips_call_expr_from_insn (rtx_insn *insn, rtx *second_call)
15282 {
15283 rtx x;
15284 rtx x2;
15285
15286 if (!CALL_P (insn))
15287 return NULL_RTX;
15288
15289 x = PATTERN (insn);
15290 if (GET_CODE (x) == PARALLEL)
15291 {
15292 /* Calls returning complex values have two CALL rtx. Look for the second
15293 one here, and return it via the SECOND_CALL arg. */
15294 x2 = XVECEXP (x, 0, 1);
15295 if (GET_CODE (x2) == SET)
15296 x2 = XEXP (x2, 1);
15297 if (GET_CODE (x2) == CALL)
15298 *second_call = x2;
15299
15300 x = XVECEXP (x, 0, 0);
15301 }
15302 if (GET_CODE (x) == SET)
15303 x = XEXP (x, 1);
15304 gcc_assert (GET_CODE (x) == CALL);
15305
15306 return x;
15307 }
15308
15309 /* REG is set in DEF. See if the definition is one of the ways we load a
15310 register with a symbol address for a mips_use_pic_fn_addr_reg_p call.
15311 If it is, return the symbol reference of the function, otherwise return
15312 NULL_RTX.
15313
15314 If RECURSE_P is true, use mips_find_pic_call_symbol to interpret
15315 the values of source registers, otherwise treat such registers as
15316 having an unknown value. */
15317
15318 static rtx
15319 mips_pic_call_symbol_from_set (df_ref def, rtx reg, bool recurse_p)
15320 {
15321 rtx_insn *def_insn;
15322 rtx set;
15323
15324 if (DF_REF_IS_ARTIFICIAL (def))
15325 return NULL_RTX;
15326
15327 def_insn = DF_REF_INSN (def);
15328 set = single_set (def_insn);
15329 if (set && rtx_equal_p (SET_DEST (set), reg))
15330 {
15331 rtx note, src, symbol;
15332
15333 /* First see whether the source is a plain symbol. This is used
15334 when calling symbols that are not lazily bound. */
15335 src = SET_SRC (set);
15336 if (GET_CODE (src) == SYMBOL_REF)
15337 return src;
15338
15339 /* Handle %call16 references. */
15340 symbol = mips_strip_unspec_call (src);
15341 if (symbol)
15342 {
15343 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15344 return symbol;
15345 }
15346
15347 /* If we have something more complicated, look for a
15348 REG_EQUAL or REG_EQUIV note. */
15349 note = find_reg_equal_equiv_note (def_insn);
15350 if (note && GET_CODE (XEXP (note, 0)) == SYMBOL_REF)
15351 return XEXP (note, 0);
15352
15353 /* Follow at most one simple register copy. Such copies are
15354 interesting in cases like:
15355
15356 for (...)
15357 {
15358 locally_binding_fn (...);
15359 }
15360
15361 and:
15362
15363 locally_binding_fn (...);
15364 ...
15365 locally_binding_fn (...);
15366
15367 where the load of locally_binding_fn can legitimately be
15368 hoisted or shared. However, we do not expect to see complex
15369 chains of copies, so a full worklist solution to the problem
15370 would probably be overkill. */
15371 if (recurse_p && REG_P (src))
15372 return mips_find_pic_call_symbol (def_insn, src, false);
15373 }
15374
15375 return NULL_RTX;
15376 }
15377
15378 /* Find the definition of the use of REG in INSN. See if the definition
15379 is one of the ways we load a register with a symbol address for a
15380 mips_use_pic_fn_addr_reg_p call. If it is return the symbol reference
15381 of the function, otherwise return NULL_RTX. RECURSE_P is as for
15382 mips_pic_call_symbol_from_set. */
15383
15384 static rtx
15385 mips_find_pic_call_symbol (rtx_insn *insn, rtx reg, bool recurse_p)
15386 {
15387 df_ref use;
15388 struct df_link *defs;
15389 rtx symbol;
15390
15391 use = df_find_use (insn, regno_reg_rtx[REGNO (reg)]);
15392 if (!use)
15393 return NULL_RTX;
15394 defs = DF_REF_CHAIN (use);
15395 if (!defs)
15396 return NULL_RTX;
15397 symbol = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15398 if (!symbol)
15399 return NULL_RTX;
15400
15401 /* If we have more than one definition, they need to be identical. */
15402 for (defs = defs->next; defs; defs = defs->next)
15403 {
15404 rtx other;
15405
15406 other = mips_pic_call_symbol_from_set (defs->ref, reg, recurse_p);
15407 if (!rtx_equal_p (symbol, other))
15408 return NULL_RTX;
15409 }
15410
15411 return symbol;
15412 }
15413
15414 /* Replace the args_size operand of the call expression CALL with the
15415 call-attribute UNSPEC and fill in SYMBOL as the function symbol. */
15416
15417 static void
15418 mips_annotate_pic_call_expr (rtx call, rtx symbol)
15419 {
15420 rtx args_size;
15421
15422 args_size = XEXP (call, 1);
15423 XEXP (call, 1) = gen_rtx_UNSPEC (GET_MODE (args_size),
15424 gen_rtvec (2, args_size, symbol),
15425 UNSPEC_CALL_ATTR);
15426 }
15427
15428 /* OPERANDS[ARGS_SIZE_OPNO] is the arg_size operand of a CALL expression. See
15429 if instead of the arg_size argument it contains the call attributes. If
15430 yes return true along with setting OPERANDS[ARGS_SIZE_OPNO] to the function
15431 symbol from the call attributes. Also return false if ARGS_SIZE_OPNO is
15432 -1. */
15433
15434 bool
15435 mips_get_pic_call_symbol (rtx *operands, int args_size_opno)
15436 {
15437 rtx args_size, symbol;
15438
15439 if (!TARGET_RELAX_PIC_CALLS || args_size_opno == -1)
15440 return false;
15441
15442 args_size = operands[args_size_opno];
15443 if (GET_CODE (args_size) != UNSPEC)
15444 return false;
15445 gcc_assert (XINT (args_size, 1) == UNSPEC_CALL_ATTR);
15446
15447 symbol = XVECEXP (args_size, 0, 1);
15448 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15449
15450 operands[args_size_opno] = symbol;
15451 return true;
15452 }
15453
15454 /* Use DF to annotate PIC indirect calls with the function symbol they
15455 dispatch to. */
15456
15457 static void
15458 mips_annotate_pic_calls (void)
15459 {
15460 basic_block bb;
15461 rtx_insn *insn;
15462
15463 FOR_EACH_BB_FN (bb, cfun)
15464 FOR_BB_INSNS (bb, insn)
15465 {
15466 rtx call, reg, symbol, second_call;
15467
15468 second_call = 0;
15469 call = mips_call_expr_from_insn (insn, &second_call);
15470 if (!call)
15471 continue;
15472 gcc_assert (MEM_P (XEXP (call, 0)));
15473 reg = XEXP (XEXP (call, 0), 0);
15474 if (!REG_P (reg))
15475 continue;
15476
15477 symbol = mips_find_pic_call_symbol (insn, reg, true);
15478 if (symbol)
15479 {
15480 mips_annotate_pic_call_expr (call, symbol);
15481 if (second_call)
15482 mips_annotate_pic_call_expr (second_call, symbol);
15483 }
15484 }
15485 }
15486 \f
15487 /* A temporary variable used by note_uses callbacks, etc. */
15488 static rtx_insn *mips_sim_insn;
15489
15490 /* A structure representing the state of the processor pipeline.
15491 Used by the mips_sim_* family of functions. */
15492 struct mips_sim {
15493 /* The maximum number of instructions that can be issued in a cycle.
15494 (Caches mips_issue_rate.) */
15495 unsigned int issue_rate;
15496
15497 /* The current simulation time. */
15498 unsigned int time;
15499
15500 /* How many more instructions can be issued in the current cycle. */
15501 unsigned int insns_left;
15502
15503 /* LAST_SET[X].INSN is the last instruction to set register X.
15504 LAST_SET[X].TIME is the time at which that instruction was issued.
15505 INSN is null if no instruction has yet set register X. */
15506 struct {
15507 rtx_insn *insn;
15508 unsigned int time;
15509 } last_set[FIRST_PSEUDO_REGISTER];
15510
15511 /* The pipeline's current DFA state. */
15512 state_t dfa_state;
15513 };
15514
15515 /* Reset STATE to the initial simulation state. */
15516
15517 static void
15518 mips_sim_reset (struct mips_sim *state)
15519 {
15520 curr_state = state->dfa_state;
15521
15522 state->time = 0;
15523 state->insns_left = state->issue_rate;
15524 memset (&state->last_set, 0, sizeof (state->last_set));
15525 state_reset (curr_state);
15526
15527 targetm.sched.init (0, false, 0);
15528 advance_state (curr_state);
15529 }
15530
15531 /* Initialize STATE before its first use. DFA_STATE points to an
15532 allocated but uninitialized DFA state. */
15533
15534 static void
15535 mips_sim_init (struct mips_sim *state, state_t dfa_state)
15536 {
15537 if (targetm.sched.init_dfa_pre_cycle_insn)
15538 targetm.sched.init_dfa_pre_cycle_insn ();
15539
15540 if (targetm.sched.init_dfa_post_cycle_insn)
15541 targetm.sched.init_dfa_post_cycle_insn ();
15542
15543 state->issue_rate = mips_issue_rate ();
15544 state->dfa_state = dfa_state;
15545 mips_sim_reset (state);
15546 }
15547
15548 /* Advance STATE by one clock cycle. */
15549
15550 static void
15551 mips_sim_next_cycle (struct mips_sim *state)
15552 {
15553 curr_state = state->dfa_state;
15554
15555 state->time++;
15556 state->insns_left = state->issue_rate;
15557 advance_state (curr_state);
15558 }
15559
15560 /* Advance simulation state STATE until instruction INSN can read
15561 register REG. */
15562
15563 static void
15564 mips_sim_wait_reg (struct mips_sim *state, rtx_insn *insn, rtx reg)
15565 {
15566 unsigned int regno, end_regno;
15567
15568 end_regno = END_REGNO (reg);
15569 for (regno = REGNO (reg); regno < end_regno; regno++)
15570 if (state->last_set[regno].insn != 0)
15571 {
15572 unsigned int t;
15573
15574 t = (state->last_set[regno].time
15575 + insn_latency (state->last_set[regno].insn, insn));
15576 while (state->time < t)
15577 mips_sim_next_cycle (state);
15578 }
15579 }
15580
15581 /* A note_uses callback. For each register in *X, advance simulation
15582 state DATA until mips_sim_insn can read the register's value. */
15583
15584 static void
15585 mips_sim_wait_regs_1 (rtx *x, void *data)
15586 {
15587 subrtx_var_iterator::array_type array;
15588 FOR_EACH_SUBRTX_VAR (iter, array, *x, NONCONST)
15589 if (REG_P (*iter))
15590 mips_sim_wait_reg ((struct mips_sim *) data, mips_sim_insn, *iter);
15591 }
15592
15593 /* Advance simulation state STATE until all of INSN's register
15594 dependencies are satisfied. */
15595
15596 static void
15597 mips_sim_wait_regs (struct mips_sim *state, rtx_insn *insn)
15598 {
15599 mips_sim_insn = insn;
15600 note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
15601 }
15602
15603 /* Advance simulation state STATE until the units required by
15604 instruction INSN are available. */
15605
15606 static void
15607 mips_sim_wait_units (struct mips_sim *state, rtx_insn *insn)
15608 {
15609 state_t tmp_state;
15610
15611 tmp_state = alloca (state_size ());
15612 while (state->insns_left == 0
15613 || (memcpy (tmp_state, state->dfa_state, state_size ()),
15614 state_transition (tmp_state, insn) >= 0))
15615 mips_sim_next_cycle (state);
15616 }
15617
15618 /* Advance simulation state STATE until INSN is ready to issue. */
15619
15620 static void
15621 mips_sim_wait_insn (struct mips_sim *state, rtx_insn *insn)
15622 {
15623 mips_sim_wait_regs (state, insn);
15624 mips_sim_wait_units (state, insn);
15625 }
15626
15627 /* mips_sim_insn has just set X. Update the LAST_SET array
15628 in simulation state DATA. */
15629
15630 static void
15631 mips_sim_record_set (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
15632 {
15633 struct mips_sim *state;
15634
15635 state = (struct mips_sim *) data;
15636 if (REG_P (x))
15637 {
15638 unsigned int regno, end_regno;
15639
15640 end_regno = END_REGNO (x);
15641 for (regno = REGNO (x); regno < end_regno; regno++)
15642 {
15643 state->last_set[regno].insn = mips_sim_insn;
15644 state->last_set[regno].time = state->time;
15645 }
15646 }
15647 }
15648
15649 /* Issue instruction INSN in scheduler state STATE. Assume that INSN
15650 can issue immediately (i.e., that mips_sim_wait_insn has already
15651 been called). */
15652
15653 static void
15654 mips_sim_issue_insn (struct mips_sim *state, rtx_insn *insn)
15655 {
15656 curr_state = state->dfa_state;
15657
15658 state_transition (curr_state, insn);
15659 state->insns_left = targetm.sched.variable_issue (0, false, insn,
15660 state->insns_left);
15661
15662 mips_sim_insn = insn;
15663 note_stores (PATTERN (insn), mips_sim_record_set, state);
15664 }
15665
15666 /* Simulate issuing a NOP in state STATE. */
15667
15668 static void
15669 mips_sim_issue_nop (struct mips_sim *state)
15670 {
15671 if (state->insns_left == 0)
15672 mips_sim_next_cycle (state);
15673 state->insns_left--;
15674 }
15675
15676 /* Update simulation state STATE so that it's ready to accept the instruction
15677 after INSN. INSN should be part of the main rtl chain, not a member of a
15678 SEQUENCE. */
15679
15680 static void
15681 mips_sim_finish_insn (struct mips_sim *state, rtx_insn *insn)
15682 {
15683 /* If INSN is a jump with an implicit delay slot, simulate a nop. */
15684 if (JUMP_P (insn))
15685 mips_sim_issue_nop (state);
15686
15687 switch (GET_CODE (SEQ_BEGIN (insn)))
15688 {
15689 case CODE_LABEL:
15690 case CALL_INSN:
15691 /* We can't predict the processor state after a call or label. */
15692 mips_sim_reset (state);
15693 break;
15694
15695 case JUMP_INSN:
15696 /* The delay slots of branch likely instructions are only executed
15697 when the branch is taken. Therefore, if the caller has simulated
15698 the delay slot instruction, STATE does not really reflect the state
15699 of the pipeline for the instruction after the delay slot. Also,
15700 branch likely instructions tend to incur a penalty when not taken,
15701 so there will probably be an extra delay between the branch and
15702 the instruction after the delay slot. */
15703 if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
15704 mips_sim_reset (state);
15705 break;
15706
15707 default:
15708 break;
15709 }
15710 }
15711
15712 /* Use simulator state STATE to calculate the execution time of
15713 instruction sequence SEQ. */
15714
15715 static unsigned int
15716 mips_seq_time (struct mips_sim *state, rtx_insn *seq)
15717 {
15718 mips_sim_reset (state);
15719 for (rtx_insn *insn = seq; insn; insn = NEXT_INSN (insn))
15720 {
15721 mips_sim_wait_insn (state, insn);
15722 mips_sim_issue_insn (state, insn);
15723 }
15724 return state->time;
15725 }
15726 \f
15727 /* Return the execution-time cost of mips_tuning_info.fast_mult_zero_zero_p
15728 setting SETTING, using STATE to simulate instruction sequences. */
15729
15730 static unsigned int
15731 mips_mult_zero_zero_cost (struct mips_sim *state, bool setting)
15732 {
15733 mips_tuning_info.fast_mult_zero_zero_p = setting;
15734 start_sequence ();
15735
15736 machine_mode dword_mode = TARGET_64BIT ? TImode : DImode;
15737 rtx hilo = gen_rtx_REG (dword_mode, MD_REG_FIRST);
15738 mips_emit_move_or_split (hilo, const0_rtx, SPLIT_FOR_SPEED);
15739
15740 /* If the target provides mulsidi3_32bit then that's the most likely
15741 consumer of the result. Test for bypasses. */
15742 if (dword_mode == DImode && HAVE_maddsidi4)
15743 {
15744 rtx gpr = gen_rtx_REG (SImode, GP_REG_FIRST + 4);
15745 emit_insn (gen_maddsidi4 (hilo, gpr, gpr, hilo));
15746 }
15747
15748 unsigned int time = mips_seq_time (state, get_insns ());
15749 end_sequence ();
15750 return time;
15751 }
15752
15753 /* Check the relative speeds of "MULT $0,$0" and "MTLO $0; MTHI $0"
15754 and set up mips_tuning_info.fast_mult_zero_zero_p accordingly.
15755 Prefer MULT -- which is shorter -- in the event of a tie. */
15756
15757 static void
15758 mips_set_fast_mult_zero_zero_p (struct mips_sim *state)
15759 {
15760 if (TARGET_MIPS16)
15761 /* No MTLO or MTHI available. */
15762 mips_tuning_info.fast_mult_zero_zero_p = true;
15763 else
15764 {
15765 unsigned int true_time = mips_mult_zero_zero_cost (state, true);
15766 unsigned int false_time = mips_mult_zero_zero_cost (state, false);
15767 mips_tuning_info.fast_mult_zero_zero_p = (true_time <= false_time);
15768 }
15769 }
15770
15771 /* Set up costs based on the current architecture and tuning settings. */
15772
15773 static void
15774 mips_set_tuning_info (void)
15775 {
15776 if (mips_tuning_info.initialized_p
15777 && mips_tuning_info.arch == mips_arch
15778 && mips_tuning_info.tune == mips_tune
15779 && mips_tuning_info.mips16_p == TARGET_MIPS16)
15780 return;
15781
15782 mips_tuning_info.arch = mips_arch;
15783 mips_tuning_info.tune = mips_tune;
15784 mips_tuning_info.mips16_p = TARGET_MIPS16;
15785 mips_tuning_info.initialized_p = true;
15786
15787 dfa_start ();
15788
15789 struct mips_sim state;
15790 mips_sim_init (&state, alloca (state_size ()));
15791
15792 mips_set_fast_mult_zero_zero_p (&state);
15793
15794 dfa_finish ();
15795 }
15796
15797 /* Implement TARGET_EXPAND_TO_RTL_HOOK. */
15798
15799 static void
15800 mips_expand_to_rtl_hook (void)
15801 {
15802 /* We need to call this at a point where we can safely create sequences
15803 of instructions, so TARGET_OVERRIDE_OPTIONS is too early. We also
15804 need to call it at a point where the DFA infrastructure is not
15805 already in use, so we can't just call it lazily on demand.
15806
15807 At present, mips_tuning_info is only needed during post-expand
15808 RTL passes such as split_insns, so this hook should be early enough.
15809 We may need to move the call elsewhere if mips_tuning_info starts
15810 to be used for other things (such as rtx_costs, or expanders that
15811 could be called during gimple optimization). */
15812 mips_set_tuning_info ();
15813 }
15814 \f
15815 /* The VR4130 pipeline issues aligned pairs of instructions together,
15816 but it stalls the second instruction if it depends on the first.
15817 In order to cut down the amount of logic required, this dependence
15818 check is not based on a full instruction decode. Instead, any non-SPECIAL
15819 instruction is assumed to modify the register specified by bits 20-16
15820 (which is usually the "rt" field).
15821
15822 In BEQ, BEQL, BNE and BNEL instructions, the rt field is actually an
15823 input, so we can end up with a false dependence between the branch
15824 and its delay slot. If this situation occurs in instruction INSN,
15825 try to avoid it by swapping rs and rt. */
15826
15827 static void
15828 vr4130_avoid_branch_rt_conflict (rtx_insn *insn)
15829 {
15830 rtx_insn *first, *second;
15831
15832 first = SEQ_BEGIN (insn);
15833 second = SEQ_END (insn);
15834 if (JUMP_P (first)
15835 && NONJUMP_INSN_P (second)
15836 && GET_CODE (PATTERN (first)) == SET
15837 && GET_CODE (SET_DEST (PATTERN (first))) == PC
15838 && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
15839 {
15840 /* Check for the right kind of condition. */
15841 rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
15842 if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
15843 && REG_P (XEXP (cond, 0))
15844 && REG_P (XEXP (cond, 1))
15845 && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
15846 && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
15847 {
15848 /* SECOND mentions the rt register but not the rs register. */
15849 rtx tmp = XEXP (cond, 0);
15850 XEXP (cond, 0) = XEXP (cond, 1);
15851 XEXP (cond, 1) = tmp;
15852 }
15853 }
15854 }
15855
15856 /* Implement -mvr4130-align. Go through each basic block and simulate the
15857 processor pipeline. If we find that a pair of instructions could execute
15858 in parallel, and the first of those instructions is not 8-byte aligned,
15859 insert a nop to make it aligned. */
15860
15861 static void
15862 vr4130_align_insns (void)
15863 {
15864 struct mips_sim state;
15865 rtx_insn *insn, *subinsn, *last, *last2, *next;
15866 bool aligned_p;
15867
15868 dfa_start ();
15869
15870 /* LAST is the last instruction before INSN to have a nonzero length.
15871 LAST2 is the last such instruction before LAST. */
15872 last = 0;
15873 last2 = 0;
15874
15875 /* ALIGNED_P is true if INSN is known to be at an aligned address. */
15876 aligned_p = true;
15877
15878 mips_sim_init (&state, alloca (state_size ()));
15879 for (insn = get_insns (); insn != 0; insn = next)
15880 {
15881 unsigned int length;
15882
15883 next = NEXT_INSN (insn);
15884
15885 /* See the comment above vr4130_avoid_branch_rt_conflict for details.
15886 This isn't really related to the alignment pass, but we do it on
15887 the fly to avoid a separate instruction walk. */
15888 vr4130_avoid_branch_rt_conflict (insn);
15889
15890 length = get_attr_length (insn);
15891 if (length > 0 && USEFUL_INSN_P (insn))
15892 FOR_EACH_SUBINSN (subinsn, insn)
15893 {
15894 mips_sim_wait_insn (&state, subinsn);
15895
15896 /* If we want this instruction to issue in parallel with the
15897 previous one, make sure that the previous instruction is
15898 aligned. There are several reasons why this isn't worthwhile
15899 when the second instruction is a call:
15900
15901 - Calls are less likely to be performance critical,
15902 - There's a good chance that the delay slot can execute
15903 in parallel with the call.
15904 - The return address would then be unaligned.
15905
15906 In general, if we're going to insert a nop between instructions
15907 X and Y, it's better to insert it immediately after X. That
15908 way, if the nop makes Y aligned, it will also align any labels
15909 between X and Y. */
15910 if (state.insns_left != state.issue_rate
15911 && !CALL_P (subinsn))
15912 {
15913 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
15914 {
15915 /* SUBINSN is the first instruction in INSN and INSN is
15916 aligned. We want to align the previous instruction
15917 instead, so insert a nop between LAST2 and LAST.
15918
15919 Note that LAST could be either a single instruction
15920 or a branch with a delay slot. In the latter case,
15921 LAST, like INSN, is already aligned, but the delay
15922 slot must have some extra delay that stops it from
15923 issuing at the same time as the branch. We therefore
15924 insert a nop before the branch in order to align its
15925 delay slot. */
15926 gcc_assert (last2);
15927 emit_insn_after (gen_nop (), last2);
15928 aligned_p = false;
15929 }
15930 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
15931 {
15932 /* SUBINSN is the delay slot of INSN, but INSN is
15933 currently unaligned. Insert a nop between
15934 LAST and INSN to align it. */
15935 gcc_assert (last);
15936 emit_insn_after (gen_nop (), last);
15937 aligned_p = true;
15938 }
15939 }
15940 mips_sim_issue_insn (&state, subinsn);
15941 }
15942 mips_sim_finish_insn (&state, insn);
15943
15944 /* Update LAST, LAST2 and ALIGNED_P for the next instruction. */
15945 length = get_attr_length (insn);
15946 if (length > 0)
15947 {
15948 /* If the instruction is an asm statement or multi-instruction
15949 mips.md patern, the length is only an estimate. Insert an
15950 8 byte alignment after it so that the following instructions
15951 can be handled correctly. */
15952 if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
15953 && (recog_memoized (insn) < 0 || length >= 8))
15954 {
15955 next = emit_insn_after (gen_align (GEN_INT (3)), insn);
15956 next = NEXT_INSN (next);
15957 mips_sim_next_cycle (&state);
15958 aligned_p = true;
15959 }
15960 else if (length & 4)
15961 aligned_p = !aligned_p;
15962 last2 = last;
15963 last = insn;
15964 }
15965
15966 /* See whether INSN is an aligned label. */
15967 if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
15968 aligned_p = true;
15969 }
15970 dfa_finish ();
15971 }
15972 \f
15973 /* This structure records that the current function has a LO_SUM
15974 involving SYMBOL_REF or LABEL_REF BASE and that MAX_OFFSET is
15975 the largest offset applied to BASE by all such LO_SUMs. */
15976 struct mips_lo_sum_offset {
15977 rtx base;
15978 HOST_WIDE_INT offset;
15979 };
15980
15981 /* Return a hash value for SYMBOL_REF or LABEL_REF BASE. */
15982
15983 static hashval_t
15984 mips_hash_base (rtx base)
15985 {
15986 int do_not_record_p;
15987
15988 return hash_rtx (base, GET_MODE (base), &do_not_record_p, NULL, false);
15989 }
15990
15991 /* Hashtable helpers. */
15992
15993 struct mips_lo_sum_offset_hasher : typed_free_remove <mips_lo_sum_offset>
15994 {
15995 typedef mips_lo_sum_offset value_type;
15996 typedef rtx_def compare_type;
15997 static inline hashval_t hash (const value_type *);
15998 static inline bool equal (const value_type *, const compare_type *);
15999 };
16000
16001 /* Hash-table callbacks for mips_lo_sum_offsets. */
16002
16003 inline hashval_t
16004 mips_lo_sum_offset_hasher::hash (const value_type *entry)
16005 {
16006 return mips_hash_base (entry->base);
16007 }
16008
16009 inline bool
16010 mips_lo_sum_offset_hasher::equal (const value_type *entry,
16011 const compare_type *value)
16012 {
16013 return rtx_equal_p (entry->base, value);
16014 }
16015
16016 typedef hash_table<mips_lo_sum_offset_hasher> mips_offset_table;
16017
16018 /* Look up symbolic constant X in HTAB, which is a hash table of
16019 mips_lo_sum_offsets. If OPTION is NO_INSERT, return true if X can be
16020 paired with a recorded LO_SUM, otherwise record X in the table. */
16021
16022 static bool
16023 mips_lo_sum_offset_lookup (mips_offset_table *htab, rtx x,
16024 enum insert_option option)
16025 {
16026 rtx base, offset;
16027 mips_lo_sum_offset **slot;
16028 struct mips_lo_sum_offset *entry;
16029
16030 /* Split X into a base and offset. */
16031 split_const (x, &base, &offset);
16032 if (UNSPEC_ADDRESS_P (base))
16033 base = UNSPEC_ADDRESS (base);
16034
16035 /* Look up the base in the hash table. */
16036 slot = htab->find_slot_with_hash (base, mips_hash_base (base), option);
16037 if (slot == NULL)
16038 return false;
16039
16040 entry = (struct mips_lo_sum_offset *) *slot;
16041 if (option == INSERT)
16042 {
16043 if (entry == NULL)
16044 {
16045 entry = XNEW (struct mips_lo_sum_offset);
16046 entry->base = base;
16047 entry->offset = INTVAL (offset);
16048 *slot = entry;
16049 }
16050 else
16051 {
16052 if (INTVAL (offset) > entry->offset)
16053 entry->offset = INTVAL (offset);
16054 }
16055 }
16056 return INTVAL (offset) <= entry->offset;
16057 }
16058
16059 /* Search X for LO_SUMs and record them in HTAB. */
16060
16061 static void
16062 mips_record_lo_sums (const_rtx x, mips_offset_table *htab)
16063 {
16064 subrtx_iterator::array_type array;
16065 FOR_EACH_SUBRTX (iter, array, x, NONCONST)
16066 if (GET_CODE (*iter) == LO_SUM)
16067 mips_lo_sum_offset_lookup (htab, XEXP (*iter, 1), INSERT);
16068 }
16069
16070 /* Return true if INSN is a SET of an orphaned high-part relocation.
16071 HTAB is a hash table of mips_lo_sum_offsets that describes all the
16072 LO_SUMs in the current function. */
16073
16074 static bool
16075 mips_orphaned_high_part_p (mips_offset_table *htab, rtx_insn *insn)
16076 {
16077 enum mips_symbol_type type;
16078 rtx x, set;
16079
16080 set = single_set (insn);
16081 if (set)
16082 {
16083 /* Check for %his. */
16084 x = SET_SRC (set);
16085 if (GET_CODE (x) == HIGH
16086 && absolute_symbolic_operand (XEXP (x, 0), VOIDmode))
16087 return !mips_lo_sum_offset_lookup (htab, XEXP (x, 0), NO_INSERT);
16088
16089 /* Check for local %gots (and %got_pages, which is redundant but OK). */
16090 if (GET_CODE (x) == UNSPEC
16091 && XINT (x, 1) == UNSPEC_LOAD_GOT
16092 && mips_symbolic_constant_p (XVECEXP (x, 0, 1),
16093 SYMBOL_CONTEXT_LEA, &type)
16094 && type == SYMBOL_GOTOFF_PAGE)
16095 return !mips_lo_sum_offset_lookup (htab, XVECEXP (x, 0, 1), NO_INSERT);
16096 }
16097 return false;
16098 }
16099
16100 /* Subroutine of mips_reorg_process_insns. If there is a hazard between
16101 INSN and a previous instruction, avoid it by inserting nops after
16102 instruction AFTER.
16103
16104 *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
16105 this point. If *DELAYED_REG is non-null, INSN must wait a cycle
16106 before using the value of that register. *HILO_DELAY counts the
16107 number of instructions since the last hilo hazard (that is,
16108 the number of instructions since the last MFLO or MFHI).
16109
16110 After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
16111 for the next instruction.
16112
16113 LO_REG is an rtx for the LO register, used in dependence checking. */
16114
16115 static void
16116 mips_avoid_hazard (rtx_insn *after, rtx_insn *insn, int *hilo_delay,
16117 rtx *delayed_reg, rtx lo_reg)
16118 {
16119 rtx pattern, set;
16120 int nops, ninsns;
16121
16122 pattern = PATTERN (insn);
16123
16124 /* Do not put the whole function in .set noreorder if it contains
16125 an asm statement. We don't know whether there will be hazards
16126 between the asm statement and the gcc-generated code. */
16127 if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
16128 cfun->machine->all_noreorder_p = false;
16129
16130 /* Ignore zero-length instructions (barriers and the like). */
16131 ninsns = get_attr_length (insn) / 4;
16132 if (ninsns == 0)
16133 return;
16134
16135 /* Work out how many nops are needed. Note that we only care about
16136 registers that are explicitly mentioned in the instruction's pattern.
16137 It doesn't matter that calls use the argument registers or that they
16138 clobber hi and lo. */
16139 if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
16140 nops = 2 - *hilo_delay;
16141 else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
16142 nops = 1;
16143 else
16144 nops = 0;
16145
16146 /* Insert the nops between this instruction and the previous one.
16147 Each new nop takes us further from the last hilo hazard. */
16148 *hilo_delay += nops;
16149 while (nops-- > 0)
16150 emit_insn_after (gen_hazard_nop (), after);
16151
16152 /* Set up the state for the next instruction. */
16153 *hilo_delay += ninsns;
16154 *delayed_reg = 0;
16155 if (INSN_CODE (insn) >= 0)
16156 switch (get_attr_hazard (insn))
16157 {
16158 case HAZARD_NONE:
16159 break;
16160
16161 case HAZARD_HILO:
16162 *hilo_delay = 0;
16163 break;
16164
16165 case HAZARD_DELAY:
16166 set = single_set (insn);
16167 gcc_assert (set);
16168 *delayed_reg = SET_DEST (set);
16169 break;
16170 }
16171 }
16172
16173 /* Go through the instruction stream and insert nops where necessary.
16174 Also delete any high-part relocations whose partnering low parts
16175 are now all dead. See if the whole function can then be put into
16176 .set noreorder and .set nomacro. */
16177
16178 static void
16179 mips_reorg_process_insns (void)
16180 {
16181 rtx_insn *insn, *last_insn, *subinsn, *next_insn;
16182 rtx lo_reg, delayed_reg;
16183 int hilo_delay;
16184
16185 /* Force all instructions to be split into their final form. */
16186 split_all_insns_noflow ();
16187
16188 /* Recalculate instruction lengths without taking nops into account. */
16189 cfun->machine->ignore_hazard_length_p = true;
16190 shorten_branches (get_insns ());
16191
16192 cfun->machine->all_noreorder_p = true;
16193
16194 /* We don't track MIPS16 PC-relative offsets closely enough to make
16195 a good job of "set .noreorder" code in MIPS16 mode. */
16196 if (TARGET_MIPS16)
16197 cfun->machine->all_noreorder_p = false;
16198
16199 /* Code that doesn't use explicit relocs can't be ".set nomacro". */
16200 if (!TARGET_EXPLICIT_RELOCS)
16201 cfun->machine->all_noreorder_p = false;
16202
16203 /* Profiled functions can't be all noreorder because the profiler
16204 support uses assembler macros. */
16205 if (crtl->profile)
16206 cfun->machine->all_noreorder_p = false;
16207
16208 /* Code compiled with -mfix-vr4120, -mfix-rm7000 or -mfix-24k can't be
16209 all noreorder because we rely on the assembler to work around some
16210 errata. The R5900 too has several bugs. */
16211 if (TARGET_FIX_VR4120
16212 || TARGET_FIX_RM7000
16213 || TARGET_FIX_24K
16214 || TARGET_MIPS5900)
16215 cfun->machine->all_noreorder_p = false;
16216
16217 /* The same is true for -mfix-vr4130 if we might generate MFLO or
16218 MFHI instructions. Note that we avoid using MFLO and MFHI if
16219 the VR4130 MACC and DMACC instructions are available instead;
16220 see the *mfhilo_{si,di}_macc patterns. */
16221 if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
16222 cfun->machine->all_noreorder_p = false;
16223
16224 mips_offset_table htab (37);
16225
16226 /* Make a first pass over the instructions, recording all the LO_SUMs. */
16227 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
16228 FOR_EACH_SUBINSN (subinsn, insn)
16229 if (USEFUL_INSN_P (subinsn))
16230 {
16231 rtx body = PATTERN (insn);
16232 int noperands = asm_noperands (body);
16233 if (noperands >= 0)
16234 {
16235 rtx *ops = XALLOCAVEC (rtx, noperands);
16236 bool *used = XALLOCAVEC (bool, noperands);
16237 const char *string = decode_asm_operands (body, ops, NULL, NULL,
16238 NULL, NULL);
16239 get_referenced_operands (string, used, noperands);
16240 for (int i = 0; i < noperands; ++i)
16241 if (used[i])
16242 mips_record_lo_sums (ops[i], &htab);
16243 }
16244 else
16245 mips_record_lo_sums (PATTERN (subinsn), &htab);
16246 }
16247
16248 last_insn = 0;
16249 hilo_delay = 2;
16250 delayed_reg = 0;
16251 lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
16252
16253 /* Make a second pass over the instructions. Delete orphaned
16254 high-part relocations or turn them into NOPs. Avoid hazards
16255 by inserting NOPs. */
16256 for (insn = get_insns (); insn != 0; insn = next_insn)
16257 {
16258 next_insn = NEXT_INSN (insn);
16259 if (USEFUL_INSN_P (insn))
16260 {
16261 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
16262 {
16263 /* If we find an orphaned high-part relocation in a delay
16264 slot, it's easier to turn that instruction into a NOP than
16265 to delete it. The delay slot will be a NOP either way. */
16266 FOR_EACH_SUBINSN (subinsn, insn)
16267 if (INSN_P (subinsn))
16268 {
16269 if (mips_orphaned_high_part_p (&htab, subinsn))
16270 {
16271 PATTERN (subinsn) = gen_nop ();
16272 INSN_CODE (subinsn) = CODE_FOR_nop;
16273 }
16274 mips_avoid_hazard (last_insn, subinsn, &hilo_delay,
16275 &delayed_reg, lo_reg);
16276 }
16277 last_insn = insn;
16278 }
16279 else
16280 {
16281 /* INSN is a single instruction. Delete it if it's an
16282 orphaned high-part relocation. */
16283 if (mips_orphaned_high_part_p (&htab, insn))
16284 delete_insn (insn);
16285 /* Also delete cache barriers if the last instruction
16286 was an annulled branch. INSN will not be speculatively
16287 executed. */
16288 else if (recog_memoized (insn) == CODE_FOR_r10k_cache_barrier
16289 && last_insn
16290 && JUMP_P (SEQ_BEGIN (last_insn))
16291 && INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (last_insn)))
16292 delete_insn (insn);
16293 else
16294 {
16295 mips_avoid_hazard (last_insn, insn, &hilo_delay,
16296 &delayed_reg, lo_reg);
16297 last_insn = insn;
16298 }
16299 }
16300 }
16301 }
16302 }
16303
16304 /* Return true if the function has a long branch instruction. */
16305
16306 static bool
16307 mips_has_long_branch_p (void)
16308 {
16309 rtx_insn *insn, *subinsn;
16310 int normal_length;
16311
16312 /* We need up-to-date instruction lengths. */
16313 shorten_branches (get_insns ());
16314
16315 /* Look for a branch that is longer than normal. The normal length for
16316 non-MIPS16 branches is 8, because the length includes the delay slot.
16317 It is 4 for MIPS16, because MIPS16 branches are extended instructions,
16318 but they have no delay slot. */
16319 normal_length = (TARGET_MIPS16 ? 4 : 8);
16320 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16321 FOR_EACH_SUBINSN (subinsn, insn)
16322 if (JUMP_P (subinsn)
16323 && get_attr_length (subinsn) > normal_length
16324 && (any_condjump_p (subinsn) || any_uncondjump_p (subinsn)))
16325 return true;
16326
16327 return false;
16328 }
16329
16330 /* If we are using a GOT, but have not decided to use a global pointer yet,
16331 see whether we need one to implement long branches. Convert the ghost
16332 global-pointer instructions into real ones if so. */
16333
16334 static bool
16335 mips_expand_ghost_gp_insns (void)
16336 {
16337 /* Quick exit if we already know that we will or won't need a
16338 global pointer. */
16339 if (!TARGET_USE_GOT
16340 || cfun->machine->global_pointer == INVALID_REGNUM
16341 || mips_must_initialize_gp_p ())
16342 return false;
16343
16344 /* Run a full check for long branches. */
16345 if (!mips_has_long_branch_p ())
16346 return false;
16347
16348 /* We've now established that we need $gp. */
16349 cfun->machine->must_initialize_gp_p = true;
16350 split_all_insns_noflow ();
16351
16352 return true;
16353 }
16354
16355 /* Subroutine of mips_reorg to manage passes that require DF. */
16356
16357 static void
16358 mips_df_reorg (void)
16359 {
16360 /* Create def-use chains. */
16361 df_set_flags (DF_EQ_NOTES);
16362 df_chain_add_problem (DF_UD_CHAIN);
16363 df_analyze ();
16364
16365 if (TARGET_RELAX_PIC_CALLS)
16366 mips_annotate_pic_calls ();
16367
16368 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE)
16369 r10k_insert_cache_barriers ();
16370
16371 df_finish_pass (false);
16372 }
16373
16374 /* Emit code to load LABEL_REF SRC into MIPS16 register DEST. This is
16375 called very late in mips_reorg, but the caller is required to run
16376 mips16_lay_out_constants on the result. */
16377
16378 static void
16379 mips16_load_branch_target (rtx dest, rtx src)
16380 {
16381 if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
16382 {
16383 rtx page, low;
16384
16385 if (mips_cfun_has_cprestore_slot_p ())
16386 mips_emit_move (dest, mips_cprestore_slot (dest, true));
16387 else
16388 mips_emit_move (dest, pic_offset_table_rtx);
16389 page = mips_unspec_address (src, SYMBOL_GOTOFF_PAGE);
16390 low = mips_unspec_address (src, SYMBOL_GOT_PAGE_OFST);
16391 emit_insn (gen_rtx_SET (VOIDmode, dest,
16392 PMODE_INSN (gen_unspec_got, (dest, page))));
16393 emit_insn (gen_rtx_SET (VOIDmode, dest,
16394 gen_rtx_LO_SUM (Pmode, dest, low)));
16395 }
16396 else
16397 {
16398 src = mips_unspec_address (src, SYMBOL_ABSOLUTE);
16399 mips_emit_move (dest, src);
16400 }
16401 }
16402
16403 /* If we're compiling a MIPS16 function, look for and split any long branches.
16404 This must be called after all other instruction modifications in
16405 mips_reorg. */
16406
16407 static void
16408 mips16_split_long_branches (void)
16409 {
16410 bool something_changed;
16411
16412 if (!TARGET_MIPS16)
16413 return;
16414
16415 /* Loop until the alignments for all targets are sufficient. */
16416 do
16417 {
16418 rtx_insn *insn;
16419
16420 shorten_branches (get_insns ());
16421 something_changed = false;
16422 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16423 if (JUMP_P (insn)
16424 && get_attr_length (insn) > 4
16425 && (any_condjump_p (insn) || any_uncondjump_p (insn)))
16426 {
16427 rtx old_label, temp, saved_temp;
16428 rtx_code_label *new_label;
16429 rtx target;
16430 rtx_insn *jump, *jump_sequence;
16431
16432 start_sequence ();
16433
16434 /* Free up a MIPS16 register by saving it in $1. */
16435 saved_temp = gen_rtx_REG (Pmode, AT_REGNUM);
16436 temp = gen_rtx_REG (Pmode, GP_REG_FIRST + 2);
16437 emit_move_insn (saved_temp, temp);
16438
16439 /* Load the branch target into TEMP. */
16440 old_label = JUMP_LABEL (insn);
16441 target = gen_rtx_LABEL_REF (Pmode, old_label);
16442 mips16_load_branch_target (temp, target);
16443
16444 /* Jump to the target and restore the register's
16445 original value. */
16446 jump = emit_jump_insn (PMODE_INSN (gen_indirect_jump_and_restore,
16447 (temp, temp, saved_temp)));
16448 JUMP_LABEL (jump) = old_label;
16449 LABEL_NUSES (old_label)++;
16450
16451 /* Rewrite any symbolic references that are supposed to use
16452 a PC-relative constant pool. */
16453 mips16_lay_out_constants (false);
16454
16455 if (simplejump_p (insn))
16456 /* We're going to replace INSN with a longer form. */
16457 new_label = NULL;
16458 else
16459 {
16460 /* Create a branch-around label for the original
16461 instruction. */
16462 new_label = gen_label_rtx ();
16463 emit_label (new_label);
16464 }
16465
16466 jump_sequence = get_insns ();
16467 end_sequence ();
16468
16469 emit_insn_after (jump_sequence, insn);
16470 if (new_label)
16471 invert_jump (insn, new_label, false);
16472 else
16473 delete_insn (insn);
16474 something_changed = true;
16475 }
16476 }
16477 while (something_changed);
16478 }
16479
16480 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
16481
16482 static void
16483 mips_reorg (void)
16484 {
16485 /* Restore the BLOCK_FOR_INSN pointers, which are needed by DF. Also during
16486 insn splitting in mips16_lay_out_constants, DF insn info is only kept up
16487 to date if the CFG is available. */
16488 if (mips_cfg_in_reorg ())
16489 compute_bb_for_insn ();
16490 mips16_lay_out_constants (true);
16491 if (mips_cfg_in_reorg ())
16492 {
16493 mips_df_reorg ();
16494 free_bb_for_insn ();
16495 }
16496 }
16497
16498 /* We use a machine specific pass to do a second machine dependent reorg
16499 pass after delay branch scheduling. */
16500
16501 static unsigned int
16502 mips_machine_reorg2 (void)
16503 {
16504 mips_reorg_process_insns ();
16505 if (!TARGET_MIPS16
16506 && TARGET_EXPLICIT_RELOCS
16507 && TUNE_MIPS4130
16508 && TARGET_VR4130_ALIGN)
16509 vr4130_align_insns ();
16510 if (mips_expand_ghost_gp_insns ())
16511 /* The expansion could invalidate some of the VR4130 alignment
16512 optimizations, but this should be an extremely rare case anyhow. */
16513 mips_reorg_process_insns ();
16514 mips16_split_long_branches ();
16515 return 0;
16516 }
16517
16518 namespace {
16519
16520 const pass_data pass_data_mips_machine_reorg2 =
16521 {
16522 RTL_PASS, /* type */
16523 "mach2", /* name */
16524 OPTGROUP_NONE, /* optinfo_flags */
16525 TV_MACH_DEP, /* tv_id */
16526 0, /* properties_required */
16527 0, /* properties_provided */
16528 0, /* properties_destroyed */
16529 0, /* todo_flags_start */
16530 0, /* todo_flags_finish */
16531 };
16532
16533 class pass_mips_machine_reorg2 : public rtl_opt_pass
16534 {
16535 public:
16536 pass_mips_machine_reorg2(gcc::context *ctxt)
16537 : rtl_opt_pass(pass_data_mips_machine_reorg2, ctxt)
16538 {}
16539
16540 /* opt_pass methods: */
16541 virtual unsigned int execute (function *) { return mips_machine_reorg2 (); }
16542
16543 }; // class pass_mips_machine_reorg2
16544
16545 } // anon namespace
16546
16547 rtl_opt_pass *
16548 make_pass_mips_machine_reorg2 (gcc::context *ctxt)
16549 {
16550 return new pass_mips_machine_reorg2 (ctxt);
16551 }
16552
16553 \f
16554 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
16555 in order to avoid duplicating too much logic from elsewhere. */
16556
16557 static void
16558 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
16559 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
16560 tree function)
16561 {
16562 rtx this_rtx, temp1, temp2, fnaddr;
16563 rtx_insn *insn;
16564 bool use_sibcall_p;
16565
16566 /* Pretend to be a post-reload pass while generating rtl. */
16567 reload_completed = 1;
16568
16569 /* Mark the end of the (empty) prologue. */
16570 emit_note (NOTE_INSN_PROLOGUE_END);
16571
16572 /* Determine if we can use a sibcall to call FUNCTION directly. */
16573 fnaddr = XEXP (DECL_RTL (function), 0);
16574 use_sibcall_p = (mips_function_ok_for_sibcall (function, NULL)
16575 && const_call_insn_operand (fnaddr, Pmode));
16576
16577 /* Determine if we need to load FNADDR from the GOT. */
16578 if (!use_sibcall_p
16579 && (mips_got_symbol_type_p
16580 (mips_classify_symbol (fnaddr, SYMBOL_CONTEXT_LEA))))
16581 {
16582 /* Pick a global pointer. Use a call-clobbered register if
16583 TARGET_CALL_SAVED_GP. */
16584 cfun->machine->global_pointer
16585 = TARGET_CALL_SAVED_GP ? 15 : GLOBAL_POINTER_REGNUM;
16586 cfun->machine->must_initialize_gp_p = true;
16587 SET_REGNO (pic_offset_table_rtx, cfun->machine->global_pointer);
16588
16589 /* Set up the global pointer for n32 or n64 abicalls. */
16590 mips_emit_loadgp ();
16591 }
16592
16593 /* We need two temporary registers in some cases. */
16594 temp1 = gen_rtx_REG (Pmode, 2);
16595 temp2 = gen_rtx_REG (Pmode, 3);
16596
16597 /* Find out which register contains the "this" pointer. */
16598 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
16599 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
16600 else
16601 this_rtx = gen_rtx_REG (Pmode, GP_ARG_FIRST);
16602
16603 /* Add DELTA to THIS_RTX. */
16604 if (delta != 0)
16605 {
16606 rtx offset = GEN_INT (delta);
16607 if (!SMALL_OPERAND (delta))
16608 {
16609 mips_emit_move (temp1, offset);
16610 offset = temp1;
16611 }
16612 emit_insn (gen_add3_insn (this_rtx, this_rtx, offset));
16613 }
16614
16615 /* If needed, add *(*THIS_RTX + VCALL_OFFSET) to THIS_RTX. */
16616 if (vcall_offset != 0)
16617 {
16618 rtx addr;
16619
16620 /* Set TEMP1 to *THIS_RTX. */
16621 mips_emit_move (temp1, gen_rtx_MEM (Pmode, this_rtx));
16622
16623 /* Set ADDR to a legitimate address for *THIS_RTX + VCALL_OFFSET. */
16624 addr = mips_add_offset (temp2, temp1, vcall_offset);
16625
16626 /* Load the offset and add it to THIS_RTX. */
16627 mips_emit_move (temp1, gen_rtx_MEM (Pmode, addr));
16628 emit_insn (gen_add3_insn (this_rtx, this_rtx, temp1));
16629 }
16630
16631 /* Jump to the target function. Use a sibcall if direct jumps are
16632 allowed, otherwise load the address into a register first. */
16633 if (use_sibcall_p)
16634 {
16635 insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
16636 SIBLING_CALL_P (insn) = 1;
16637 }
16638 else
16639 {
16640 /* This is messy. GAS treats "la $25,foo" as part of a call
16641 sequence and may allow a global "foo" to be lazily bound.
16642 The general move patterns therefore reject this combination.
16643
16644 In this context, lazy binding would actually be OK
16645 for TARGET_CALL_CLOBBERED_GP, but it's still wrong for
16646 TARGET_CALL_SAVED_GP; see mips_load_call_address.
16647 We must therefore load the address via a temporary
16648 register if mips_dangerous_for_la25_p.
16649
16650 If we jump to the temporary register rather than $25,
16651 the assembler can use the move insn to fill the jump's
16652 delay slot.
16653
16654 We can use the same technique for MIPS16 code, where $25
16655 is not a valid JR register. */
16656 if (TARGET_USE_PIC_FN_ADDR_REG
16657 && !TARGET_MIPS16
16658 && !mips_dangerous_for_la25_p (fnaddr))
16659 temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
16660 mips_load_call_address (MIPS_CALL_SIBCALL, temp1, fnaddr);
16661
16662 if (TARGET_USE_PIC_FN_ADDR_REG
16663 && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
16664 mips_emit_move (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
16665 emit_jump_insn (gen_indirect_jump (temp1));
16666 }
16667
16668 /* Run just enough of rest_of_compilation. This sequence was
16669 "borrowed" from alpha.c. */
16670 insn = get_insns ();
16671 split_all_insns_noflow ();
16672 mips16_lay_out_constants (true);
16673 shorten_branches (insn);
16674 final_start_function (insn, file, 1);
16675 final (insn, file, 1);
16676 final_end_function ();
16677
16678 /* Clean up the vars set above. Note that final_end_function resets
16679 the global pointer for us. */
16680 reload_completed = 0;
16681 }
16682 \f
16683
16684 /* The last argument passed to mips_set_compression_mode,
16685 or negative if the function hasn't been called yet. */
16686 static unsigned int old_compression_mode = -1;
16687
16688 /* Set up the target-dependent global state for ISA mode COMPRESSION_MODE,
16689 which is either MASK_MIPS16 or MASK_MICROMIPS. */
16690
16691 static void
16692 mips_set_compression_mode (unsigned int compression_mode)
16693 {
16694
16695 if (compression_mode == old_compression_mode)
16696 return;
16697
16698 /* Restore base settings of various flags. */
16699 target_flags = mips_base_target_flags;
16700 flag_schedule_insns = mips_base_schedule_insns;
16701 flag_reorder_blocks_and_partition = mips_base_reorder_blocks_and_partition;
16702 flag_move_loop_invariants = mips_base_move_loop_invariants;
16703 align_loops = mips_base_align_loops;
16704 align_jumps = mips_base_align_jumps;
16705 align_functions = mips_base_align_functions;
16706 target_flags &= ~(MASK_MIPS16 | MASK_MICROMIPS);
16707 target_flags |= compression_mode;
16708
16709 if (compression_mode & MASK_MIPS16)
16710 {
16711 /* Switch to MIPS16 mode. */
16712 target_flags |= MASK_MIPS16;
16713
16714 /* Turn off SYNCI if it was on, MIPS16 doesn't support it. */
16715 target_flags &= ~MASK_SYNCI;
16716
16717 /* Don't run the scheduler before reload, since it tends to
16718 increase register pressure. */
16719 flag_schedule_insns = 0;
16720
16721 /* Don't do hot/cold partitioning. mips16_lay_out_constants expects
16722 the whole function to be in a single section. */
16723 flag_reorder_blocks_and_partition = 0;
16724
16725 /* Don't move loop invariants, because it tends to increase
16726 register pressure. It also introduces an extra move in cases
16727 where the constant is the first operand in a two-operand binary
16728 instruction, or when it forms a register argument to a functon
16729 call. */
16730 flag_move_loop_invariants = 0;
16731
16732 target_flags |= MASK_EXPLICIT_RELOCS;
16733
16734 /* Experiments suggest we get the best overall section-anchor
16735 results from using the range of an unextended LW or SW. Code
16736 that makes heavy use of byte or short accesses can do better
16737 with ranges of 0...31 and 0...63 respectively, but most code is
16738 sensitive to the range of LW and SW instead. */
16739 targetm.min_anchor_offset = 0;
16740 targetm.max_anchor_offset = 127;
16741
16742 targetm.const_anchor = 0;
16743
16744 /* MIPS16 has no BAL instruction. */
16745 target_flags &= ~MASK_RELAX_PIC_CALLS;
16746
16747 /* The R4000 errata don't apply to any known MIPS16 cores.
16748 It's simpler to make the R4000 fixes and MIPS16 mode
16749 mutually exclusive. */
16750 target_flags &= ~MASK_FIX_R4000;
16751
16752 if (flag_pic && !TARGET_OLDABI)
16753 sorry ("MIPS16 PIC for ABIs other than o32 and o64");
16754
16755 if (TARGET_XGOT)
16756 sorry ("MIPS16 -mxgot code");
16757
16758 if (TARGET_HARD_FLOAT_ABI && !TARGET_OLDABI)
16759 sorry ("hard-float MIPS16 code for ABIs other than o32 and o64");
16760 }
16761 else
16762 {
16763 /* Switch to microMIPS or the standard encoding. */
16764
16765 if (TARGET_MICROMIPS)
16766 /* Avoid branch likely. */
16767 target_flags &= ~MASK_BRANCHLIKELY;
16768
16769 /* Provide default values for align_* for 64-bit targets. */
16770 if (TARGET_64BIT)
16771 {
16772 if (align_loops == 0)
16773 align_loops = 8;
16774 if (align_jumps == 0)
16775 align_jumps = 8;
16776 if (align_functions == 0)
16777 align_functions = 8;
16778 }
16779
16780 targetm.min_anchor_offset = -32768;
16781 targetm.max_anchor_offset = 32767;
16782
16783 targetm.const_anchor = 0x8000;
16784 }
16785
16786 /* (Re)initialize MIPS target internals for new ISA. */
16787 mips_init_relocs ();
16788
16789 if (compression_mode & MASK_MIPS16)
16790 {
16791 if (!mips16_globals)
16792 mips16_globals = save_target_globals_default_opts ();
16793 else
16794 restore_target_globals (mips16_globals);
16795 }
16796 else
16797 restore_target_globals (&default_target_globals);
16798
16799 old_compression_mode = compression_mode;
16800 }
16801
16802 /* Implement TARGET_SET_CURRENT_FUNCTION. Decide whether the current
16803 function should use the MIPS16 or microMIPS ISA and switch modes
16804 accordingly. */
16805
16806 static void
16807 mips_set_current_function (tree fndecl)
16808 {
16809 mips_set_compression_mode (mips_get_compress_mode (fndecl));
16810 }
16811 \f
16812 /* Allocate a chunk of memory for per-function machine-dependent data. */
16813
16814 static struct machine_function *
16815 mips_init_machine_status (void)
16816 {
16817 return ggc_cleared_alloc<machine_function> ();
16818 }
16819
16820 /* Return the processor associated with the given ISA level, or null
16821 if the ISA isn't valid. */
16822
16823 static const struct mips_cpu_info *
16824 mips_cpu_info_from_isa (int isa)
16825 {
16826 unsigned int i;
16827
16828 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16829 if (mips_cpu_info_table[i].isa == isa)
16830 return mips_cpu_info_table + i;
16831
16832 return NULL;
16833 }
16834
16835 /* Return a mips_cpu_info entry determined by an option valued
16836 OPT. */
16837
16838 static const struct mips_cpu_info *
16839 mips_cpu_info_from_opt (int opt)
16840 {
16841 switch (opt)
16842 {
16843 case MIPS_ARCH_OPTION_FROM_ABI:
16844 /* 'from-abi' selects the most compatible architecture for the
16845 given ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit
16846 ABIs. For the EABIs, we have to decide whether we're using
16847 the 32-bit or 64-bit version. */
16848 return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
16849 : ABI_NEEDS_64BIT_REGS ? 3
16850 : (TARGET_64BIT ? 3 : 1));
16851
16852 case MIPS_ARCH_OPTION_NATIVE:
16853 gcc_unreachable ();
16854
16855 default:
16856 return &mips_cpu_info_table[opt];
16857 }
16858 }
16859
16860 /* Return a default mips_cpu_info entry, given that no -march= option
16861 was explicitly specified. */
16862
16863 static const struct mips_cpu_info *
16864 mips_default_arch (void)
16865 {
16866 #if defined (MIPS_CPU_STRING_DEFAULT)
16867 unsigned int i;
16868 for (i = 0; i < ARRAY_SIZE (mips_cpu_info_table); i++)
16869 if (strcmp (mips_cpu_info_table[i].name, MIPS_CPU_STRING_DEFAULT) == 0)
16870 return mips_cpu_info_table + i;
16871 gcc_unreachable ();
16872 #elif defined (MIPS_ISA_DEFAULT)
16873 return mips_cpu_info_from_isa (MIPS_ISA_DEFAULT);
16874 #else
16875 /* 'from-abi' makes a good default: you get whatever the ABI
16876 requires. */
16877 return mips_cpu_info_from_opt (MIPS_ARCH_OPTION_FROM_ABI);
16878 #endif
16879 }
16880
16881 /* Set up globals to generate code for the ISA or processor
16882 described by INFO. */
16883
16884 static void
16885 mips_set_architecture (const struct mips_cpu_info *info)
16886 {
16887 if (info != 0)
16888 {
16889 mips_arch_info = info;
16890 mips_arch = info->cpu;
16891 mips_isa = info->isa;
16892 if (mips_isa < 32)
16893 mips_isa_rev = 0;
16894 else
16895 mips_isa_rev = (mips_isa & 31) + 1;
16896 }
16897 }
16898
16899 /* Likewise for tuning. */
16900
16901 static void
16902 mips_set_tune (const struct mips_cpu_info *info)
16903 {
16904 if (info != 0)
16905 {
16906 mips_tune_info = info;
16907 mips_tune = info->cpu;
16908 }
16909 }
16910
16911 /* Implement TARGET_OPTION_OVERRIDE. */
16912
16913 static void
16914 mips_option_override (void)
16915 {
16916 int i, start, regno, mode;
16917
16918 if (global_options_set.x_mips_isa_option)
16919 mips_isa_option_info = &mips_cpu_info_table[mips_isa_option];
16920
16921 #ifdef SUBTARGET_OVERRIDE_OPTIONS
16922 SUBTARGET_OVERRIDE_OPTIONS;
16923 #endif
16924
16925 /* MIPS16 and microMIPS cannot coexist. */
16926 if (TARGET_MICROMIPS && TARGET_MIPS16)
16927 error ("unsupported combination: %s", "-mips16 -mmicromips");
16928
16929 /* Save the base compression state and process flags as though we
16930 were generating uncompressed code. */
16931 mips_base_compression_flags = TARGET_COMPRESSION;
16932 target_flags &= ~TARGET_COMPRESSION;
16933
16934 /* -mno-float overrides -mhard-float and -msoft-float. */
16935 if (TARGET_NO_FLOAT)
16936 {
16937 target_flags |= MASK_SOFT_FLOAT_ABI;
16938 target_flags_explicit |= MASK_SOFT_FLOAT_ABI;
16939 }
16940
16941 if (TARGET_FLIP_MIPS16)
16942 TARGET_INTERLINK_COMPRESSED = 1;
16943
16944 /* Set the small data limit. */
16945 mips_small_data_threshold = (global_options_set.x_g_switch_value
16946 ? g_switch_value
16947 : MIPS_DEFAULT_GVALUE);
16948
16949 /* The following code determines the architecture and register size.
16950 Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
16951 The GAS and GCC code should be kept in sync as much as possible. */
16952
16953 if (global_options_set.x_mips_arch_option)
16954 mips_set_architecture (mips_cpu_info_from_opt (mips_arch_option));
16955
16956 if (mips_isa_option_info != 0)
16957 {
16958 if (mips_arch_info == 0)
16959 mips_set_architecture (mips_isa_option_info);
16960 else if (mips_arch_info->isa != mips_isa_option_info->isa)
16961 error ("%<-%s%> conflicts with the other architecture options, "
16962 "which specify a %s processor",
16963 mips_isa_option_info->name,
16964 mips_cpu_info_from_isa (mips_arch_info->isa)->name);
16965 }
16966
16967 if (mips_arch_info == 0)
16968 mips_set_architecture (mips_default_arch ());
16969
16970 if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
16971 error ("%<-march=%s%> is not compatible with the selected ABI",
16972 mips_arch_info->name);
16973
16974 /* Optimize for mips_arch, unless -mtune selects a different processor. */
16975 if (global_options_set.x_mips_tune_option)
16976 mips_set_tune (mips_cpu_info_from_opt (mips_tune_option));
16977
16978 if (mips_tune_info == 0)
16979 mips_set_tune (mips_arch_info);
16980
16981 if ((target_flags_explicit & MASK_64BIT) != 0)
16982 {
16983 /* The user specified the size of the integer registers. Make sure
16984 it agrees with the ABI and ISA. */
16985 if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
16986 error ("%<-mgp64%> used with a 32-bit processor");
16987 else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
16988 error ("%<-mgp32%> used with a 64-bit ABI");
16989 else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
16990 error ("%<-mgp64%> used with a 32-bit ABI");
16991 }
16992 else
16993 {
16994 /* Infer the integer register size from the ABI and processor.
16995 Restrict ourselves to 32-bit registers if that's all the
16996 processor has, or if the ABI cannot handle 64-bit registers. */
16997 if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
16998 target_flags &= ~MASK_64BIT;
16999 else
17000 target_flags |= MASK_64BIT;
17001 }
17002
17003 if ((target_flags_explicit & MASK_FLOAT64) != 0)
17004 {
17005 if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
17006 error ("unsupported combination: %s", "-mfp64 -msingle-float");
17007 else if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
17008 error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
17009 else if (!TARGET_64BIT && TARGET_FLOAT64)
17010 {
17011 if (!ISA_HAS_MXHC1)
17012 error ("%<-mgp32%> and %<-mfp64%> can only be combined if"
17013 " the target supports the mfhc1 and mthc1 instructions");
17014 else if (mips_abi != ABI_32)
17015 error ("%<-mgp32%> and %<-mfp64%> can only be combined when using"
17016 " the o32 ABI");
17017 }
17018 }
17019 else
17020 {
17021 /* -msingle-float selects 32-bit float registers. Otherwise the
17022 float registers should be the same size as the integer ones. */
17023 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
17024 target_flags |= MASK_FLOAT64;
17025 else
17026 target_flags &= ~MASK_FLOAT64;
17027 }
17028
17029 /* End of code shared with GAS. */
17030
17031 /* The R5900 FPU only supports single precision. */
17032 if (TARGET_MIPS5900 && TARGET_HARD_FLOAT_ABI && TARGET_DOUBLE_FLOAT)
17033 error ("unsupported combination: %s",
17034 "-march=r5900 -mhard-float -mdouble-float");
17035
17036 /* If a -mlong* option was given, check that it matches the ABI,
17037 otherwise infer the -mlong* setting from the other options. */
17038 if ((target_flags_explicit & MASK_LONG64) != 0)
17039 {
17040 if (TARGET_LONG64)
17041 {
17042 if (mips_abi == ABI_N32)
17043 error ("%qs is incompatible with %qs", "-mabi=n32", "-mlong64");
17044 else if (mips_abi == ABI_32)
17045 error ("%qs is incompatible with %qs", "-mabi=32", "-mlong64");
17046 else if (mips_abi == ABI_O64 && TARGET_ABICALLS)
17047 /* We have traditionally allowed non-abicalls code to use
17048 an LP64 form of o64. However, it would take a bit more
17049 effort to support the combination of 32-bit GOT entries
17050 and 64-bit pointers, so we treat the abicalls case as
17051 an error. */
17052 error ("the combination of %qs and %qs is incompatible with %qs",
17053 "-mabi=o64", "-mabicalls", "-mlong64");
17054 }
17055 else
17056 {
17057 if (mips_abi == ABI_64)
17058 error ("%qs is incompatible with %qs", "-mabi=64", "-mlong32");
17059 }
17060 }
17061 else
17062 {
17063 if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
17064 target_flags |= MASK_LONG64;
17065 else
17066 target_flags &= ~MASK_LONG64;
17067 }
17068
17069 if (!TARGET_OLDABI)
17070 flag_pcc_struct_return = 0;
17071
17072 /* Decide which rtx_costs structure to use. */
17073 if (optimize_size)
17074 mips_cost = &mips_rtx_cost_optimize_size;
17075 else
17076 mips_cost = &mips_rtx_cost_data[mips_tune];
17077
17078 /* If the user hasn't specified a branch cost, use the processor's
17079 default. */
17080 if (mips_branch_cost == 0)
17081 mips_branch_cost = mips_cost->branch_cost;
17082
17083 /* If neither -mbranch-likely nor -mno-branch-likely was given
17084 on the command line, set MASK_BRANCHLIKELY based on the target
17085 architecture and tuning flags. Annulled delay slots are a
17086 size win, so we only consider the processor-specific tuning
17087 for !optimize_size. */
17088 if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
17089 {
17090 if (ISA_HAS_BRANCHLIKELY
17091 && (optimize_size
17092 || (mips_tune_info->tune_flags & PTF_AVOID_BRANCHLIKELY) == 0))
17093 target_flags |= MASK_BRANCHLIKELY;
17094 else
17095 target_flags &= ~MASK_BRANCHLIKELY;
17096 }
17097 else if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
17098 warning (0, "the %qs architecture does not support branch-likely"
17099 " instructions", mips_arch_info->name);
17100
17101 /* If the user hasn't specified -mimadd or -mno-imadd set
17102 MASK_IMADD based on the target architecture and tuning
17103 flags. */
17104 if ((target_flags_explicit & MASK_IMADD) == 0)
17105 {
17106 if (ISA_HAS_MADD_MSUB &&
17107 (mips_tune_info->tune_flags & PTF_AVOID_IMADD) == 0)
17108 target_flags |= MASK_IMADD;
17109 else
17110 target_flags &= ~MASK_IMADD;
17111 }
17112 else if (TARGET_IMADD && !ISA_HAS_MADD_MSUB)
17113 warning (0, "the %qs architecture does not support madd or msub"
17114 " instructions", mips_arch_info->name);
17115
17116 /* The effect of -mabicalls isn't defined for the EABI. */
17117 if (mips_abi == ABI_EABI && TARGET_ABICALLS)
17118 {
17119 error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
17120 target_flags &= ~MASK_ABICALLS;
17121 }
17122
17123 /* PIC requires -mabicalls. */
17124 if (flag_pic)
17125 {
17126 if (mips_abi == ABI_EABI)
17127 error ("cannot generate position-independent code for %qs",
17128 "-mabi=eabi");
17129 else if (!TARGET_ABICALLS)
17130 error ("position-independent code requires %qs", "-mabicalls");
17131 }
17132
17133 if (TARGET_ABICALLS_PIC2)
17134 /* We need to set flag_pic for executables as well as DSOs
17135 because we may reference symbols that are not defined in
17136 the final executable. (MIPS does not use things like
17137 copy relocs, for example.)
17138
17139 There is a body of code that uses __PIC__ to distinguish
17140 between -mabicalls and -mno-abicalls code. The non-__PIC__
17141 variant is usually appropriate for TARGET_ABICALLS_PIC0, as
17142 long as any indirect jumps use $25. */
17143 flag_pic = 1;
17144
17145 /* -mvr4130-align is a "speed over size" optimization: it usually produces
17146 faster code, but at the expense of more nops. Enable it at -O3 and
17147 above. */
17148 if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
17149 target_flags |= MASK_VR4130_ALIGN;
17150
17151 /* Prefer a call to memcpy over inline code when optimizing for size,
17152 though see MOVE_RATIO in mips.h. */
17153 if (optimize_size && (target_flags_explicit & MASK_MEMCPY) == 0)
17154 target_flags |= MASK_MEMCPY;
17155
17156 /* If we have a nonzero small-data limit, check that the -mgpopt
17157 setting is consistent with the other target flags. */
17158 if (mips_small_data_threshold > 0)
17159 {
17160 if (!TARGET_GPOPT)
17161 {
17162 if (!TARGET_EXPLICIT_RELOCS)
17163 error ("%<-mno-gpopt%> needs %<-mexplicit-relocs%>");
17164
17165 TARGET_LOCAL_SDATA = false;
17166 TARGET_EXTERN_SDATA = false;
17167 }
17168 else
17169 {
17170 if (TARGET_VXWORKS_RTP)
17171 warning (0, "cannot use small-data accesses for %qs", "-mrtp");
17172
17173 if (TARGET_ABICALLS)
17174 warning (0, "cannot use small-data accesses for %qs",
17175 "-mabicalls");
17176 }
17177 }
17178
17179 /* Pre-IEEE 754-2008 MIPS hardware has a quirky almost-IEEE format
17180 for all its floating point. */
17181 if (mips_nan != MIPS_IEEE_754_2008)
17182 {
17183 REAL_MODE_FORMAT (SFmode) = &mips_single_format;
17184 REAL_MODE_FORMAT (DFmode) = &mips_double_format;
17185 REAL_MODE_FORMAT (TFmode) = &mips_quad_format;
17186 }
17187
17188 /* Make sure that the user didn't turn off paired single support when
17189 MIPS-3D support is requested. */
17190 if (TARGET_MIPS3D
17191 && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
17192 && !TARGET_PAIRED_SINGLE_FLOAT)
17193 error ("%<-mips3d%> requires %<-mpaired-single%>");
17194
17195 /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT. */
17196 if (TARGET_MIPS3D)
17197 target_flags |= MASK_PAIRED_SINGLE_FLOAT;
17198
17199 /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
17200 and TARGET_HARD_FLOAT_ABI are both true. */
17201 if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT_ABI))
17202 {
17203 error ("%qs must be used with %qs",
17204 TARGET_MIPS3D ? "-mips3d" : "-mpaired-single",
17205 TARGET_HARD_FLOAT_ABI ? "-mfp64" : "-mhard-float");
17206 target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
17207 TARGET_MIPS3D = 0;
17208 }
17209
17210 /* Make sure that -mpaired-single is only used on ISAs that support it.
17211 We must disable it otherwise since it relies on other ISA properties
17212 like ISA_HAS_8CC having their normal values. */
17213 if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_HAS_PAIRED_SINGLE)
17214 {
17215 error ("the %qs architecture does not support paired-single"
17216 " instructions", mips_arch_info->name);
17217 target_flags &= ~MASK_PAIRED_SINGLE_FLOAT;
17218 TARGET_MIPS3D = 0;
17219 }
17220
17221 if (mips_r10k_cache_barrier != R10K_CACHE_BARRIER_NONE
17222 && !TARGET_CACHE_BUILTIN)
17223 {
17224 error ("%qs requires a target that provides the %qs instruction",
17225 "-mr10k-cache-barrier", "cache");
17226 mips_r10k_cache_barrier = R10K_CACHE_BARRIER_NONE;
17227 }
17228
17229 /* If TARGET_DSPR2, enable TARGET_DSP. */
17230 if (TARGET_DSPR2)
17231 TARGET_DSP = true;
17232
17233 /* .eh_frame addresses should be the same width as a C pointer.
17234 Most MIPS ABIs support only one pointer size, so the assembler
17235 will usually know exactly how big an .eh_frame address is.
17236
17237 Unfortunately, this is not true of the 64-bit EABI. The ABI was
17238 originally defined to use 64-bit pointers (i.e. it is LP64), and
17239 this is still the default mode. However, we also support an n32-like
17240 ILP32 mode, which is selected by -mlong32. The problem is that the
17241 assembler has traditionally not had an -mlong option, so it has
17242 traditionally not known whether we're using the ILP32 or LP64 form.
17243
17244 As it happens, gas versions up to and including 2.19 use _32-bit_
17245 addresses for EABI64 .cfi_* directives. This is wrong for the
17246 default LP64 mode, so we can't use the directives by default.
17247 Moreover, since gas's current behavior is at odds with gcc's
17248 default behavior, it seems unwise to rely on future versions
17249 of gas behaving the same way. We therefore avoid using .cfi
17250 directives for -mlong32 as well. */
17251 if (mips_abi == ABI_EABI && TARGET_64BIT)
17252 flag_dwarf2_cfi_asm = 0;
17253
17254 /* .cfi_* directives generate a read-only section, so fall back on
17255 manual .eh_frame creation if we need the section to be writable. */
17256 if (TARGET_WRITABLE_EH_FRAME)
17257 flag_dwarf2_cfi_asm = 0;
17258
17259 mips_init_print_operand_punct ();
17260
17261 /* Set up array to map GCC register number to debug register number.
17262 Ignore the special purpose register numbers. */
17263
17264 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17265 {
17266 mips_dbx_regno[i] = IGNORED_DWARF_REGNUM;
17267 if (GP_REG_P (i) || FP_REG_P (i) || ALL_COP_REG_P (i))
17268 mips_dwarf_regno[i] = i;
17269 else
17270 mips_dwarf_regno[i] = INVALID_REGNUM;
17271 }
17272
17273 start = GP_DBX_FIRST - GP_REG_FIRST;
17274 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
17275 mips_dbx_regno[i] = i + start;
17276
17277 start = FP_DBX_FIRST - FP_REG_FIRST;
17278 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
17279 mips_dbx_regno[i] = i + start;
17280
17281 /* Accumulator debug registers use big-endian ordering. */
17282 mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
17283 mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
17284 mips_dwarf_regno[HI_REGNUM] = MD_REG_FIRST + 0;
17285 mips_dwarf_regno[LO_REGNUM] = MD_REG_FIRST + 1;
17286 for (i = DSP_ACC_REG_FIRST; i <= DSP_ACC_REG_LAST; i += 2)
17287 {
17288 mips_dwarf_regno[i + TARGET_LITTLE_ENDIAN] = i;
17289 mips_dwarf_regno[i + TARGET_BIG_ENDIAN] = i + 1;
17290 }
17291
17292 /* Set up mips_hard_regno_mode_ok. */
17293 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
17294 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
17295 mips_hard_regno_mode_ok[mode][regno]
17296 = mips_hard_regno_mode_ok_p (regno, (machine_mode) mode);
17297
17298 /* Function to allocate machine-dependent function status. */
17299 init_machine_status = &mips_init_machine_status;
17300
17301 /* Default to working around R4000 errata only if the processor
17302 was selected explicitly. */
17303 if ((target_flags_explicit & MASK_FIX_R4000) == 0
17304 && strcmp (mips_arch_info->name, "r4000") == 0)
17305 target_flags |= MASK_FIX_R4000;
17306
17307 /* Default to working around R4400 errata only if the processor
17308 was selected explicitly. */
17309 if ((target_flags_explicit & MASK_FIX_R4400) == 0
17310 && strcmp (mips_arch_info->name, "r4400") == 0)
17311 target_flags |= MASK_FIX_R4400;
17312
17313 /* Default to working around R10000 errata only if the processor
17314 was selected explicitly. */
17315 if ((target_flags_explicit & MASK_FIX_R10000) == 0
17316 && strcmp (mips_arch_info->name, "r10000") == 0)
17317 target_flags |= MASK_FIX_R10000;
17318
17319 /* Make sure that branch-likely instructions available when using
17320 -mfix-r10000. The instructions are not available if either:
17321
17322 1. -mno-branch-likely was passed.
17323 2. The selected ISA does not support branch-likely and
17324 the command line does not include -mbranch-likely. */
17325 if (TARGET_FIX_R10000
17326 && ((target_flags_explicit & MASK_BRANCHLIKELY) == 0
17327 ? !ISA_HAS_BRANCHLIKELY
17328 : !TARGET_BRANCHLIKELY))
17329 sorry ("%qs requires branch-likely instructions", "-mfix-r10000");
17330
17331 if (TARGET_SYNCI && !ISA_HAS_SYNCI)
17332 {
17333 warning (0, "the %qs architecture does not support the synci "
17334 "instruction", mips_arch_info->name);
17335 target_flags &= ~MASK_SYNCI;
17336 }
17337
17338 /* Only optimize PIC indirect calls if they are actually required. */
17339 if (!TARGET_USE_GOT || !TARGET_EXPLICIT_RELOCS)
17340 target_flags &= ~MASK_RELAX_PIC_CALLS;
17341
17342 /* Save base state of options. */
17343 mips_base_target_flags = target_flags;
17344 mips_base_schedule_insns = flag_schedule_insns;
17345 mips_base_reorder_blocks_and_partition = flag_reorder_blocks_and_partition;
17346 mips_base_move_loop_invariants = flag_move_loop_invariants;
17347 mips_base_align_loops = align_loops;
17348 mips_base_align_jumps = align_jumps;
17349 mips_base_align_functions = align_functions;
17350
17351 /* Now select the ISA mode.
17352
17353 Do all CPP-sensitive stuff in uncompressed mode; we'll switch modes
17354 later if required. */
17355 mips_set_compression_mode (0);
17356
17357 /* We register a second machine specific reorg pass after delay slot
17358 filling. Registering the pass must be done at start up. It's
17359 convenient to do it here. */
17360 opt_pass *new_pass = make_pass_mips_machine_reorg2 (g);
17361 struct register_pass_info insert_pass_mips_machine_reorg2 =
17362 {
17363 new_pass, /* pass */
17364 "dbr", /* reference_pass_name */
17365 1, /* ref_pass_instance_number */
17366 PASS_POS_INSERT_AFTER /* po_op */
17367 };
17368 register_pass (&insert_pass_mips_machine_reorg2);
17369
17370 if (TARGET_HARD_FLOAT_ABI && TARGET_MIPS5900)
17371 REAL_MODE_FORMAT (SFmode) = &spu_single_format;
17372 }
17373
17374 /* Swap the register information for registers I and I + 1, which
17375 currently have the wrong endianness. Note that the registers'
17376 fixedness and call-clobberedness might have been set on the
17377 command line. */
17378
17379 static void
17380 mips_swap_registers (unsigned int i)
17381 {
17382 int tmpi;
17383 const char *tmps;
17384
17385 #define SWAP_INT(X, Y) (tmpi = (X), (X) = (Y), (Y) = tmpi)
17386 #define SWAP_STRING(X, Y) (tmps = (X), (X) = (Y), (Y) = tmps)
17387
17388 SWAP_INT (fixed_regs[i], fixed_regs[i + 1]);
17389 SWAP_INT (call_used_regs[i], call_used_regs[i + 1]);
17390 SWAP_INT (call_really_used_regs[i], call_really_used_regs[i + 1]);
17391 SWAP_STRING (reg_names[i], reg_names[i + 1]);
17392
17393 #undef SWAP_STRING
17394 #undef SWAP_INT
17395 }
17396
17397 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE. */
17398
17399 static void
17400 mips_conditional_register_usage (void)
17401 {
17402
17403 if (ISA_HAS_DSP)
17404 {
17405 /* These DSP control register fields are global. */
17406 global_regs[CCDSP_PO_REGNUM] = 1;
17407 global_regs[CCDSP_SC_REGNUM] = 1;
17408 }
17409 else
17410 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17411 reg_class_contents[(int) DSP_ACC_REGS]);
17412
17413 if (!TARGET_HARD_FLOAT)
17414 {
17415 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17416 reg_class_contents[(int) FP_REGS]);
17417 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17418 reg_class_contents[(int) ST_REGS]);
17419 }
17420 else if (!ISA_HAS_8CC)
17421 {
17422 /* We only have a single condition-code register. We implement
17423 this by fixing all the condition-code registers and generating
17424 RTL that refers directly to ST_REG_FIRST. */
17425 AND_COMPL_HARD_REG_SET (accessible_reg_set,
17426 reg_class_contents[(int) ST_REGS]);
17427 SET_HARD_REG_BIT (accessible_reg_set, FPSW_REGNUM);
17428 fixed_regs[FPSW_REGNUM] = call_used_regs[FPSW_REGNUM] = 1;
17429 }
17430 if (TARGET_MIPS16)
17431 {
17432 /* In MIPS16 mode, we prohibit the unused $s registers, since they
17433 are call-saved, and saving them via a MIPS16 register would
17434 probably waste more time than just reloading the value.
17435
17436 We permit the $t temporary registers when optimizing for speed
17437 but not when optimizing for space because using them results in
17438 code that is larger (but faster) then not using them. We do
17439 allow $24 (t8) because it is used in CMP and CMPI instructions
17440 and $25 (t9) because it is used as the function call address in
17441 SVR4 PIC code. */
17442
17443 fixed_regs[18] = call_used_regs[18] = 1;
17444 fixed_regs[19] = call_used_regs[19] = 1;
17445 fixed_regs[20] = call_used_regs[20] = 1;
17446 fixed_regs[21] = call_used_regs[21] = 1;
17447 fixed_regs[22] = call_used_regs[22] = 1;
17448 fixed_regs[23] = call_used_regs[23] = 1;
17449 fixed_regs[26] = call_used_regs[26] = 1;
17450 fixed_regs[27] = call_used_regs[27] = 1;
17451 fixed_regs[30] = call_used_regs[30] = 1;
17452 if (optimize_size)
17453 {
17454 fixed_regs[8] = call_used_regs[8] = 1;
17455 fixed_regs[9] = call_used_regs[9] = 1;
17456 fixed_regs[10] = call_used_regs[10] = 1;
17457 fixed_regs[11] = call_used_regs[11] = 1;
17458 fixed_regs[12] = call_used_regs[12] = 1;
17459 fixed_regs[13] = call_used_regs[13] = 1;
17460 fixed_regs[14] = call_used_regs[14] = 1;
17461 fixed_regs[15] = call_used_regs[15] = 1;
17462 }
17463
17464 /* Do not allow HI and LO to be treated as register operands.
17465 There are no MTHI or MTLO instructions (or any real need
17466 for them) and one-way registers cannot easily be reloaded. */
17467 AND_COMPL_HARD_REG_SET (operand_reg_set,
17468 reg_class_contents[(int) MD_REGS]);
17469 }
17470 /* $f20-$f23 are call-clobbered for n64. */
17471 if (mips_abi == ABI_64)
17472 {
17473 int regno;
17474 for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
17475 call_really_used_regs[regno] = call_used_regs[regno] = 1;
17476 }
17477 /* Odd registers in the range $f21-$f31 (inclusive) are call-clobbered
17478 for n32. */
17479 if (mips_abi == ABI_N32)
17480 {
17481 int regno;
17482 for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
17483 call_really_used_regs[regno] = call_used_regs[regno] = 1;
17484 }
17485 /* Make sure that double-register accumulator values are correctly
17486 ordered for the current endianness. */
17487 if (TARGET_LITTLE_ENDIAN)
17488 {
17489 unsigned int regno;
17490
17491 mips_swap_registers (MD_REG_FIRST);
17492 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno += 2)
17493 mips_swap_registers (regno);
17494 }
17495 }
17496
17497 /* Implement EH_USES. */
17498
17499 bool
17500 mips_eh_uses (unsigned int regno)
17501 {
17502 if (reload_completed && !TARGET_ABSOLUTE_JUMPS)
17503 {
17504 /* We need to force certain registers to be live in order to handle
17505 PIC long branches correctly. See mips_must_initialize_gp_p for
17506 details. */
17507 if (mips_cfun_has_cprestore_slot_p ())
17508 {
17509 if (regno == CPRESTORE_SLOT_REGNUM)
17510 return true;
17511 }
17512 else
17513 {
17514 if (cfun->machine->global_pointer == regno)
17515 return true;
17516 }
17517 }
17518
17519 return false;
17520 }
17521
17522 /* Implement EPILOGUE_USES. */
17523
17524 bool
17525 mips_epilogue_uses (unsigned int regno)
17526 {
17527 /* Say that the epilogue uses the return address register. Note that
17528 in the case of sibcalls, the values "used by the epilogue" are
17529 considered live at the start of the called function. */
17530 if (regno == RETURN_ADDR_REGNUM)
17531 return true;
17532
17533 /* If using a GOT, say that the epilogue also uses GOT_VERSION_REGNUM.
17534 See the comment above load_call<mode> for details. */
17535 if (TARGET_USE_GOT && (regno) == GOT_VERSION_REGNUM)
17536 return true;
17537
17538 /* An interrupt handler must preserve some registers that are
17539 ordinarily call-clobbered. */
17540 if (cfun->machine->interrupt_handler_p
17541 && mips_interrupt_extra_call_saved_reg_p (regno))
17542 return true;
17543
17544 return false;
17545 }
17546
17547 /* Return true if INSN needs to be wrapped in ".set noat".
17548 INSN has NOPERANDS operands, stored in OPVEC. */
17549
17550 static bool
17551 mips_need_noat_wrapper_p (rtx_insn *insn, rtx *opvec, int noperands)
17552 {
17553 if (recog_memoized (insn) >= 0)
17554 {
17555 subrtx_iterator::array_type array;
17556 for (int i = 0; i < noperands; i++)
17557 FOR_EACH_SUBRTX (iter, array, opvec[i], NONCONST)
17558 if (REG_P (*iter) && REGNO (*iter) == AT_REGNUM)
17559 return true;
17560 }
17561 return false;
17562 }
17563
17564 /* Implement FINAL_PRESCAN_INSN. */
17565
17566 void
17567 mips_final_prescan_insn (rtx_insn *insn, rtx *opvec, int noperands)
17568 {
17569 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17570 mips_push_asm_switch (&mips_noat);
17571 }
17572
17573 /* Implement TARGET_ASM_FINAL_POSTSCAN_INSN. */
17574
17575 static void
17576 mips_final_postscan_insn (FILE *file ATTRIBUTE_UNUSED, rtx_insn *insn,
17577 rtx *opvec, int noperands)
17578 {
17579 if (mips_need_noat_wrapper_p (insn, opvec, noperands))
17580 mips_pop_asm_switch (&mips_noat);
17581 }
17582
17583 /* Return the function that is used to expand the <u>mulsidi3 pattern.
17584 EXT_CODE is the code of the extension used. Return NULL if widening
17585 multiplication shouldn't be used. */
17586
17587 mulsidi3_gen_fn
17588 mips_mulsidi3_gen_fn (enum rtx_code ext_code)
17589 {
17590 bool signed_p;
17591
17592 signed_p = ext_code == SIGN_EXTEND;
17593 if (TARGET_64BIT)
17594 {
17595 /* Don't use widening multiplication with MULT when we have DMUL. Even
17596 with the extension of its input operands DMUL is faster. Note that
17597 the extension is not needed for signed multiplication. In order to
17598 ensure that we always remove the redundant sign-extension in this
17599 case we still expand mulsidi3 for DMUL. */
17600 if (ISA_HAS_DMUL3)
17601 return signed_p ? gen_mulsidi3_64bit_dmul : NULL;
17602 if (TARGET_MIPS16)
17603 return (signed_p
17604 ? gen_mulsidi3_64bit_mips16
17605 : gen_umulsidi3_64bit_mips16);
17606 if (TARGET_FIX_R4000)
17607 return NULL;
17608 return signed_p ? gen_mulsidi3_64bit : gen_umulsidi3_64bit;
17609 }
17610 else
17611 {
17612 if (TARGET_MIPS16)
17613 return (signed_p
17614 ? gen_mulsidi3_32bit_mips16
17615 : gen_umulsidi3_32bit_mips16);
17616 if (TARGET_FIX_R4000 && !ISA_HAS_DSP)
17617 return signed_p ? gen_mulsidi3_32bit_r4000 : gen_umulsidi3_32bit_r4000;
17618 return signed_p ? gen_mulsidi3_32bit : gen_umulsidi3_32bit;
17619 }
17620 }
17621
17622 /* Return true if PATTERN matches the kind of instruction generated by
17623 umips_build_save_restore. SAVE_P is true for store. */
17624
17625 bool
17626 umips_save_restore_pattern_p (bool save_p, rtx pattern)
17627 {
17628 int n;
17629 unsigned int i;
17630 HOST_WIDE_INT first_offset = 0;
17631 rtx first_base = 0;
17632 unsigned int regmask = 0;
17633
17634 for (n = 0; n < XVECLEN (pattern, 0); n++)
17635 {
17636 rtx set, reg, mem, this_base;
17637 HOST_WIDE_INT this_offset;
17638
17639 /* Check that we have a SET. */
17640 set = XVECEXP (pattern, 0, n);
17641 if (GET_CODE (set) != SET)
17642 return false;
17643
17644 /* Check that the SET is a load (if restoring) or a store
17645 (if saving). */
17646 mem = save_p ? SET_DEST (set) : SET_SRC (set);
17647 if (!MEM_P (mem) || MEM_VOLATILE_P (mem))
17648 return false;
17649
17650 /* Check that the address is the sum of base and a possibly-zero
17651 constant offset. Determine if the offset is in range. */
17652 mips_split_plus (XEXP (mem, 0), &this_base, &this_offset);
17653 if (!REG_P (this_base))
17654 return false;
17655
17656 if (n == 0)
17657 {
17658 if (!UMIPS_12BIT_OFFSET_P (this_offset))
17659 return false;
17660 first_base = this_base;
17661 first_offset = this_offset;
17662 }
17663 else
17664 {
17665 /* Check that the save slots are consecutive. */
17666 if (REGNO (this_base) != REGNO (first_base)
17667 || this_offset != first_offset + UNITS_PER_WORD * n)
17668 return false;
17669 }
17670
17671 /* Check that SET's other operand is a register. */
17672 reg = save_p ? SET_SRC (set) : SET_DEST (set);
17673 if (!REG_P (reg))
17674 return false;
17675
17676 regmask |= 1 << REGNO (reg);
17677 }
17678
17679 for (i = 0; i < ARRAY_SIZE (umips_swm_mask); i++)
17680 if (regmask == umips_swm_mask[i])
17681 return true;
17682
17683 return false;
17684 }
17685
17686 /* Return the assembly instruction for microMIPS LWM or SWM.
17687 SAVE_P and PATTERN are as for umips_save_restore_pattern_p. */
17688
17689 const char *
17690 umips_output_save_restore (bool save_p, rtx pattern)
17691 {
17692 static char buffer[300];
17693 char *s;
17694 int n;
17695 HOST_WIDE_INT offset;
17696 rtx base, mem, set, last_set, last_reg;
17697
17698 /* Parse the pattern. */
17699 gcc_assert (umips_save_restore_pattern_p (save_p, pattern));
17700
17701 s = strcpy (buffer, save_p ? "swm\t" : "lwm\t");
17702 s += strlen (s);
17703 n = XVECLEN (pattern, 0);
17704
17705 set = XVECEXP (pattern, 0, 0);
17706 mem = save_p ? SET_DEST (set) : SET_SRC (set);
17707 mips_split_plus (XEXP (mem, 0), &base, &offset);
17708
17709 last_set = XVECEXP (pattern, 0, n - 1);
17710 last_reg = save_p ? SET_SRC (last_set) : SET_DEST (last_set);
17711
17712 if (REGNO (last_reg) == 31)
17713 n--;
17714
17715 gcc_assert (n <= 9);
17716 if (n == 0)
17717 ;
17718 else if (n == 1)
17719 s += sprintf (s, "%s,", reg_names[16]);
17720 else if (n < 9)
17721 s += sprintf (s, "%s-%s,", reg_names[16], reg_names[15 + n]);
17722 else if (n == 9)
17723 s += sprintf (s, "%s-%s,%s,", reg_names[16], reg_names[23],
17724 reg_names[30]);
17725
17726 if (REGNO (last_reg) == 31)
17727 s += sprintf (s, "%s,", reg_names[31]);
17728
17729 s += sprintf (s, "%d(%s)", (int)offset, reg_names[REGNO (base)]);
17730 return buffer;
17731 }
17732
17733 /* Return true if MEM1 and MEM2 use the same base register, and the
17734 offset of MEM2 equals the offset of MEM1 plus 4. FIRST_REG is the
17735 register into (from) which the contents of MEM1 will be loaded
17736 (stored), depending on the value of LOAD_P.
17737 SWAP_P is true when the 1st and 2nd instructions are swapped. */
17738
17739 static bool
17740 umips_load_store_pair_p_1 (bool load_p, bool swap_p,
17741 rtx first_reg, rtx mem1, rtx mem2)
17742 {
17743 rtx base1, base2;
17744 HOST_WIDE_INT offset1, offset2;
17745
17746 if (!MEM_P (mem1) || !MEM_P (mem2))
17747 return false;
17748
17749 mips_split_plus (XEXP (mem1, 0), &base1, &offset1);
17750 mips_split_plus (XEXP (mem2, 0), &base2, &offset2);
17751
17752 if (!REG_P (base1) || !rtx_equal_p (base1, base2))
17753 return false;
17754
17755 /* Avoid invalid load pair instructions. */
17756 if (load_p && REGNO (first_reg) == REGNO (base1))
17757 return false;
17758
17759 /* We must avoid this case for anti-dependence.
17760 Ex: lw $3, 4($3)
17761 lw $2, 0($3)
17762 first_reg is $2, but the base is $3. */
17763 if (load_p
17764 && swap_p
17765 && REGNO (first_reg) + 1 == REGNO (base1))
17766 return false;
17767
17768 if (offset2 != offset1 + 4)
17769 return false;
17770
17771 if (!UMIPS_12BIT_OFFSET_P (offset1))
17772 return false;
17773
17774 return true;
17775 }
17776
17777 /* OPERANDS describes the operands to a pair of SETs, in the order
17778 dest1, src1, dest2, src2. Return true if the operands can be used
17779 in an LWP or SWP instruction; LOAD_P says which. */
17780
17781 bool
17782 umips_load_store_pair_p (bool load_p, rtx *operands)
17783 {
17784 rtx reg1, reg2, mem1, mem2;
17785
17786 if (load_p)
17787 {
17788 reg1 = operands[0];
17789 reg2 = operands[2];
17790 mem1 = operands[1];
17791 mem2 = operands[3];
17792 }
17793 else
17794 {
17795 reg1 = operands[1];
17796 reg2 = operands[3];
17797 mem1 = operands[0];
17798 mem2 = operands[2];
17799 }
17800
17801 if (REGNO (reg2) == REGNO (reg1) + 1)
17802 return umips_load_store_pair_p_1 (load_p, false, reg1, mem1, mem2);
17803
17804 if (REGNO (reg1) == REGNO (reg2) + 1)
17805 return umips_load_store_pair_p_1 (load_p, true, reg2, mem2, mem1);
17806
17807 return false;
17808 }
17809
17810 /* Return the assembly instruction for a microMIPS LWP or SWP in which
17811 the first register is REG and the first memory slot is MEM.
17812 LOAD_P is true for LWP. */
17813
17814 static void
17815 umips_output_load_store_pair_1 (bool load_p, rtx reg, rtx mem)
17816 {
17817 rtx ops[] = {reg, mem};
17818
17819 if (load_p)
17820 output_asm_insn ("lwp\t%0,%1", ops);
17821 else
17822 output_asm_insn ("swp\t%0,%1", ops);
17823 }
17824
17825 /* Output the assembly instruction for a microMIPS LWP or SWP instruction.
17826 LOAD_P and OPERANDS are as for umips_load_store_pair_p. */
17827
17828 void
17829 umips_output_load_store_pair (bool load_p, rtx *operands)
17830 {
17831 rtx reg1, reg2, mem1, mem2;
17832 if (load_p)
17833 {
17834 reg1 = operands[0];
17835 reg2 = operands[2];
17836 mem1 = operands[1];
17837 mem2 = operands[3];
17838 }
17839 else
17840 {
17841 reg1 = operands[1];
17842 reg2 = operands[3];
17843 mem1 = operands[0];
17844 mem2 = operands[2];
17845 }
17846
17847 if (REGNO (reg2) == REGNO (reg1) + 1)
17848 {
17849 umips_output_load_store_pair_1 (load_p, reg1, mem1);
17850 return;
17851 }
17852
17853 gcc_assert (REGNO (reg1) == REGNO (reg2) + 1);
17854 umips_output_load_store_pair_1 (load_p, reg2, mem2);
17855 }
17856
17857 /* Return true if REG1 and REG2 match the criteria for a movep insn. */
17858
17859 bool
17860 umips_movep_target_p (rtx reg1, rtx reg2)
17861 {
17862 int regno1, regno2, pair;
17863 unsigned int i;
17864 static const int match[8] = {
17865 0x00000060, /* 5, 6 */
17866 0x000000a0, /* 5, 7 */
17867 0x000000c0, /* 6, 7 */
17868 0x00200010, /* 4, 21 */
17869 0x00400010, /* 4, 22 */
17870 0x00000030, /* 4, 5 */
17871 0x00000050, /* 4, 6 */
17872 0x00000090 /* 4, 7 */
17873 };
17874
17875 if (!REG_P (reg1) || !REG_P (reg2))
17876 return false;
17877
17878 regno1 = REGNO (reg1);
17879 regno2 = REGNO (reg2);
17880
17881 if (!GP_REG_P (regno1) || !GP_REG_P (regno2))
17882 return false;
17883
17884 pair = (1 << regno1) | (1 << regno2);
17885
17886 for (i = 0; i < ARRAY_SIZE (match); i++)
17887 if (pair == match[i])
17888 return true;
17889
17890 return false;
17891 }
17892 \f
17893 /* Return the size in bytes of the trampoline code, padded to
17894 TRAMPOLINE_ALIGNMENT bits. The static chain pointer and target
17895 function address immediately follow. */
17896
17897 int
17898 mips_trampoline_code_size (void)
17899 {
17900 if (TARGET_USE_PIC_FN_ADDR_REG)
17901 return 4 * 4;
17902 else if (ptr_mode == DImode)
17903 return 8 * 4;
17904 else if (ISA_HAS_LOAD_DELAY)
17905 return 6 * 4;
17906 else
17907 return 4 * 4;
17908 }
17909
17910 /* Implement TARGET_TRAMPOLINE_INIT. */
17911
17912 static void
17913 mips_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
17914 {
17915 rtx addr, end_addr, high, low, opcode, mem;
17916 rtx trampoline[8];
17917 unsigned int i, j;
17918 HOST_WIDE_INT end_addr_offset, static_chain_offset, target_function_offset;
17919
17920 /* Work out the offsets of the pointers from the start of the
17921 trampoline code. */
17922 end_addr_offset = mips_trampoline_code_size ();
17923 static_chain_offset = end_addr_offset;
17924 target_function_offset = static_chain_offset + GET_MODE_SIZE (ptr_mode);
17925
17926 /* Get pointers to the beginning and end of the code block. */
17927 addr = force_reg (Pmode, XEXP (m_tramp, 0));
17928 end_addr = mips_force_binary (Pmode, PLUS, addr, GEN_INT (end_addr_offset));
17929
17930 #define OP(X) gen_int_mode (X, SImode)
17931
17932 /* Build up the code in TRAMPOLINE. */
17933 i = 0;
17934 if (TARGET_USE_PIC_FN_ADDR_REG)
17935 {
17936 /* $25 contains the address of the trampoline. Emit code of the form:
17937
17938 l[wd] $1, target_function_offset($25)
17939 l[wd] $static_chain, static_chain_offset($25)
17940 jr $1
17941 move $25,$1. */
17942 trampoline[i++] = OP (MIPS_LOAD_PTR (AT_REGNUM,
17943 target_function_offset,
17944 PIC_FUNCTION_ADDR_REGNUM));
17945 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17946 static_chain_offset,
17947 PIC_FUNCTION_ADDR_REGNUM));
17948 trampoline[i++] = OP (MIPS_JR (AT_REGNUM));
17949 trampoline[i++] = OP (MIPS_MOVE (PIC_FUNCTION_ADDR_REGNUM, AT_REGNUM));
17950 }
17951 else if (ptr_mode == DImode)
17952 {
17953 /* It's too cumbersome to create the full 64-bit address, so let's
17954 instead use:
17955
17956 move $1, $31
17957 bal 1f
17958 nop
17959 1: l[wd] $25, target_function_offset - 12($31)
17960 l[wd] $static_chain, static_chain_offset - 12($31)
17961 jr $25
17962 move $31, $1
17963
17964 where 12 is the offset of "1:" from the start of the code block. */
17965 trampoline[i++] = OP (MIPS_MOVE (AT_REGNUM, RETURN_ADDR_REGNUM));
17966 trampoline[i++] = OP (MIPS_BAL (1));
17967 trampoline[i++] = OP (MIPS_NOP);
17968 trampoline[i++] = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
17969 target_function_offset - 12,
17970 RETURN_ADDR_REGNUM));
17971 trampoline[i++] = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
17972 static_chain_offset - 12,
17973 RETURN_ADDR_REGNUM));
17974 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
17975 trampoline[i++] = OP (MIPS_MOVE (RETURN_ADDR_REGNUM, AT_REGNUM));
17976 }
17977 else
17978 {
17979 /* If the target has load delays, emit:
17980
17981 lui $1, %hi(end_addr)
17982 lw $25, %lo(end_addr + ...)($1)
17983 lw $static_chain, %lo(end_addr + ...)($1)
17984 jr $25
17985 nop
17986
17987 Otherwise emit:
17988
17989 lui $1, %hi(end_addr)
17990 lw $25, %lo(end_addr + ...)($1)
17991 jr $25
17992 lw $static_chain, %lo(end_addr + ...)($1). */
17993
17994 /* Split END_ADDR into %hi and %lo values. Trampolines are aligned
17995 to 64 bits, so the %lo value will have the bottom 3 bits clear. */
17996 high = expand_simple_binop (SImode, PLUS, end_addr, GEN_INT (0x8000),
17997 NULL, false, OPTAB_WIDEN);
17998 high = expand_simple_binop (SImode, LSHIFTRT, high, GEN_INT (16),
17999 NULL, false, OPTAB_WIDEN);
18000 low = convert_to_mode (SImode, gen_lowpart (HImode, end_addr), true);
18001
18002 /* Emit the LUI. */
18003 opcode = OP (MIPS_LUI (AT_REGNUM, 0));
18004 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, high,
18005 NULL, false, OPTAB_WIDEN);
18006
18007 /* Emit the load of the target function. */
18008 opcode = OP (MIPS_LOAD_PTR (PIC_FUNCTION_ADDR_REGNUM,
18009 target_function_offset - end_addr_offset,
18010 AT_REGNUM));
18011 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
18012 NULL, false, OPTAB_WIDEN);
18013
18014 /* Emit the JR here, if we can. */
18015 if (!ISA_HAS_LOAD_DELAY)
18016 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
18017
18018 /* Emit the load of the static chain register. */
18019 opcode = OP (MIPS_LOAD_PTR (STATIC_CHAIN_REGNUM,
18020 static_chain_offset - end_addr_offset,
18021 AT_REGNUM));
18022 trampoline[i++] = expand_simple_binop (SImode, IOR, opcode, low,
18023 NULL, false, OPTAB_WIDEN);
18024
18025 /* Emit the JR, if we couldn't above. */
18026 if (ISA_HAS_LOAD_DELAY)
18027 {
18028 trampoline[i++] = OP (MIPS_JR (PIC_FUNCTION_ADDR_REGNUM));
18029 trampoline[i++] = OP (MIPS_NOP);
18030 }
18031 }
18032
18033 #undef OP
18034
18035 /* Copy the trampoline code. Leave any padding uninitialized. */
18036 for (j = 0; j < i; j++)
18037 {
18038 mem = adjust_address (m_tramp, SImode, j * GET_MODE_SIZE (SImode));
18039 mips_emit_move (mem, trampoline[j]);
18040 }
18041
18042 /* Set up the static chain pointer field. */
18043 mem = adjust_address (m_tramp, ptr_mode, static_chain_offset);
18044 mips_emit_move (mem, chain_value);
18045
18046 /* Set up the target function field. */
18047 mem = adjust_address (m_tramp, ptr_mode, target_function_offset);
18048 mips_emit_move (mem, XEXP (DECL_RTL (fndecl), 0));
18049
18050 /* Flush the code part of the trampoline. */
18051 emit_insn (gen_add3_insn (end_addr, addr, GEN_INT (TRAMPOLINE_SIZE)));
18052 emit_insn (gen_clear_cache (addr, end_addr));
18053 }
18054
18055 /* Implement FUNCTION_PROFILER. */
18056
18057 void mips_function_profiler (FILE *file)
18058 {
18059 if (TARGET_MIPS16)
18060 sorry ("mips16 function profiling");
18061 if (TARGET_LONG_CALLS)
18062 {
18063 /* For TARGET_LONG_CALLS use $3 for the address of _mcount. */
18064 if (Pmode == DImode)
18065 fprintf (file, "\tdla\t%s,_mcount\n", reg_names[3]);
18066 else
18067 fprintf (file, "\tla\t%s,_mcount\n", reg_names[3]);
18068 }
18069 mips_push_asm_switch (&mips_noat);
18070 fprintf (file, "\tmove\t%s,%s\t\t# save current return address\n",
18071 reg_names[AT_REGNUM], reg_names[RETURN_ADDR_REGNUM]);
18072 /* _mcount treats $2 as the static chain register. */
18073 if (cfun->static_chain_decl != NULL)
18074 fprintf (file, "\tmove\t%s,%s\n", reg_names[2],
18075 reg_names[STATIC_CHAIN_REGNUM]);
18076 if (TARGET_MCOUNT_RA_ADDRESS)
18077 {
18078 /* If TARGET_MCOUNT_RA_ADDRESS load $12 with the address of the
18079 ra save location. */
18080 if (cfun->machine->frame.ra_fp_offset == 0)
18081 /* ra not saved, pass zero. */
18082 fprintf (file, "\tmove\t%s,%s\n", reg_names[12], reg_names[0]);
18083 else
18084 fprintf (file, "\t%s\t%s," HOST_WIDE_INT_PRINT_DEC "(%s)\n",
18085 Pmode == DImode ? "dla" : "la", reg_names[12],
18086 cfun->machine->frame.ra_fp_offset,
18087 reg_names[STACK_POINTER_REGNUM]);
18088 }
18089 if (!TARGET_NEWABI)
18090 fprintf (file,
18091 "\t%s\t%s,%s,%d\t\t# _mcount pops 2 words from stack\n",
18092 TARGET_64BIT ? "dsubu" : "subu",
18093 reg_names[STACK_POINTER_REGNUM],
18094 reg_names[STACK_POINTER_REGNUM],
18095 Pmode == DImode ? 16 : 8);
18096
18097 if (TARGET_LONG_CALLS)
18098 fprintf (file, "\tjalr\t%s\n", reg_names[3]);
18099 else
18100 fprintf (file, "\tjal\t_mcount\n");
18101 mips_pop_asm_switch (&mips_noat);
18102 /* _mcount treats $2 as the static chain register. */
18103 if (cfun->static_chain_decl != NULL)
18104 fprintf (file, "\tmove\t%s,%s\n", reg_names[STATIC_CHAIN_REGNUM],
18105 reg_names[2]);
18106 }
18107
18108 /* Implement TARGET_SHIFT_TRUNCATION_MASK. We want to keep the default
18109 behaviour of TARGET_SHIFT_TRUNCATION_MASK for non-vector modes even
18110 when TARGET_LOONGSON_VECTORS is true. */
18111
18112 static unsigned HOST_WIDE_INT
18113 mips_shift_truncation_mask (machine_mode mode)
18114 {
18115 if (TARGET_LOONGSON_VECTORS && VECTOR_MODE_P (mode))
18116 return 0;
18117
18118 return GET_MODE_BITSIZE (mode) - 1;
18119 }
18120
18121 /* Implement TARGET_PREPARE_PCH_SAVE. */
18122
18123 static void
18124 mips_prepare_pch_save (void)
18125 {
18126 /* We are called in a context where the current MIPS16 vs. non-MIPS16
18127 setting should be irrelevant. The question then is: which setting
18128 makes most sense at load time?
18129
18130 The PCH is loaded before the first token is read. We should never
18131 have switched into MIPS16 mode by that point, and thus should not
18132 have populated mips16_globals. Nor can we load the entire contents
18133 of mips16_globals from the PCH file, because mips16_globals contains
18134 a combination of GGC and non-GGC data.
18135
18136 There is therefore no point in trying save the GGC part of
18137 mips16_globals to the PCH file, or to preserve MIPS16ness across
18138 the PCH save and load. The loading compiler would not have access
18139 to the non-GGC parts of mips16_globals (either from the PCH file,
18140 or from a copy that the loading compiler generated itself) and would
18141 have to call target_reinit anyway.
18142
18143 It therefore seems best to switch back to non-MIPS16 mode at
18144 save time, and to ensure that mips16_globals remains null after
18145 a PCH load. */
18146 mips_set_compression_mode (0);
18147 mips16_globals = 0;
18148 }
18149 \f
18150 /* Generate or test for an insn that supports a constant permutation. */
18151
18152 #define MAX_VECT_LEN 8
18153
18154 struct expand_vec_perm_d
18155 {
18156 rtx target, op0, op1;
18157 unsigned char perm[MAX_VECT_LEN];
18158 machine_mode vmode;
18159 unsigned char nelt;
18160 bool one_vector_p;
18161 bool testing_p;
18162 };
18163
18164 /* Construct (set target (vec_select op0 (parallel perm))) and
18165 return true if that's a valid instruction in the active ISA. */
18166
18167 static bool
18168 mips_expand_vselect (rtx target, rtx op0,
18169 const unsigned char *perm, unsigned nelt)
18170 {
18171 rtx rperm[MAX_VECT_LEN], x;
18172 rtx_insn *insn;
18173 unsigned i;
18174
18175 for (i = 0; i < nelt; ++i)
18176 rperm[i] = GEN_INT (perm[i]);
18177
18178 x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
18179 x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
18180 x = gen_rtx_SET (VOIDmode, target, x);
18181
18182 insn = emit_insn (x);
18183 if (recog_memoized (insn) < 0)
18184 {
18185 remove_insn (insn);
18186 return false;
18187 }
18188 return true;
18189 }
18190
18191 /* Similar, but generate a vec_concat from op0 and op1 as well. */
18192
18193 static bool
18194 mips_expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
18195 const unsigned char *perm, unsigned nelt)
18196 {
18197 machine_mode v2mode;
18198 rtx x;
18199
18200 v2mode = GET_MODE_2XWIDER_MODE (GET_MODE (op0));
18201 x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
18202 return mips_expand_vselect (target, x, perm, nelt);
18203 }
18204
18205 /* Recognize patterns for even-odd extraction. */
18206
18207 static bool
18208 mips_expand_vpc_loongson_even_odd (struct expand_vec_perm_d *d)
18209 {
18210 unsigned i, odd, nelt = d->nelt;
18211 rtx t0, t1, t2, t3;
18212
18213 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18214 return false;
18215 /* Even-odd for V2SI/V2SFmode is matched by interleave directly. */
18216 if (nelt < 4)
18217 return false;
18218
18219 odd = d->perm[0];
18220 if (odd > 1)
18221 return false;
18222 for (i = 1; i < nelt; ++i)
18223 if (d->perm[i] != i * 2 + odd)
18224 return false;
18225
18226 if (d->testing_p)
18227 return true;
18228
18229 /* We need 2*log2(N)-1 operations to achieve odd/even with interleave. */
18230 t0 = gen_reg_rtx (d->vmode);
18231 t1 = gen_reg_rtx (d->vmode);
18232 switch (d->vmode)
18233 {
18234 case V4HImode:
18235 emit_insn (gen_loongson_punpckhhw (t0, d->op0, d->op1));
18236 emit_insn (gen_loongson_punpcklhw (t1, d->op0, d->op1));
18237 if (odd)
18238 emit_insn (gen_loongson_punpckhhw (d->target, t1, t0));
18239 else
18240 emit_insn (gen_loongson_punpcklhw (d->target, t1, t0));
18241 break;
18242
18243 case V8QImode:
18244 t2 = gen_reg_rtx (d->vmode);
18245 t3 = gen_reg_rtx (d->vmode);
18246 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op1));
18247 emit_insn (gen_loongson_punpcklbh (t1, d->op0, d->op1));
18248 emit_insn (gen_loongson_punpckhbh (t2, t1, t0));
18249 emit_insn (gen_loongson_punpcklbh (t3, t1, t0));
18250 if (odd)
18251 emit_insn (gen_loongson_punpckhbh (d->target, t3, t2));
18252 else
18253 emit_insn (gen_loongson_punpcklbh (d->target, t3, t2));
18254 break;
18255
18256 default:
18257 gcc_unreachable ();
18258 }
18259 return true;
18260 }
18261
18262 /* Recognize patterns for the Loongson PSHUFH instruction. */
18263
18264 static bool
18265 mips_expand_vpc_loongson_pshufh (struct expand_vec_perm_d *d)
18266 {
18267 unsigned i, mask;
18268 rtx rmask;
18269
18270 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18271 return false;
18272 if (d->vmode != V4HImode)
18273 return false;
18274 if (d->testing_p)
18275 return true;
18276
18277 /* Convert the selector into the packed 8-bit form for pshufh. */
18278 /* Recall that loongson is little-endian only. No big-endian
18279 adjustment required. */
18280 for (i = mask = 0; i < 4; i++)
18281 mask |= (d->perm[i] & 3) << (i * 2);
18282 rmask = force_reg (SImode, GEN_INT (mask));
18283
18284 if (d->one_vector_p)
18285 emit_insn (gen_loongson_pshufh (d->target, d->op0, rmask));
18286 else
18287 {
18288 rtx t0, t1, x, merge, rmerge[4];
18289
18290 t0 = gen_reg_rtx (V4HImode);
18291 t1 = gen_reg_rtx (V4HImode);
18292 emit_insn (gen_loongson_pshufh (t1, d->op1, rmask));
18293 emit_insn (gen_loongson_pshufh (t0, d->op0, rmask));
18294
18295 for (i = 0; i < 4; ++i)
18296 rmerge[i] = (d->perm[i] & 4 ? constm1_rtx : const0_rtx);
18297 merge = gen_rtx_CONST_VECTOR (V4HImode, gen_rtvec_v (4, rmerge));
18298 merge = force_reg (V4HImode, merge);
18299
18300 x = gen_rtx_AND (V4HImode, merge, t1);
18301 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18302
18303 x = gen_rtx_NOT (V4HImode, merge);
18304 x = gen_rtx_AND (V4HImode, x, t0);
18305 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18306
18307 x = gen_rtx_IOR (V4HImode, t0, t1);
18308 emit_insn (gen_rtx_SET (VOIDmode, d->target, x));
18309 }
18310
18311 return true;
18312 }
18313
18314 /* Recognize broadcast patterns for the Loongson. */
18315
18316 static bool
18317 mips_expand_vpc_loongson_bcast (struct expand_vec_perm_d *d)
18318 {
18319 unsigned i, elt;
18320 rtx t0, t1;
18321
18322 if (!(TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS))
18323 return false;
18324 /* Note that we've already matched V2SI via punpck and V4HI via pshufh. */
18325 if (d->vmode != V8QImode)
18326 return false;
18327 if (!d->one_vector_p)
18328 return false;
18329
18330 elt = d->perm[0];
18331 for (i = 1; i < 8; ++i)
18332 if (d->perm[i] != elt)
18333 return false;
18334
18335 if (d->testing_p)
18336 return true;
18337
18338 /* With one interleave we put two of the desired element adjacent. */
18339 t0 = gen_reg_rtx (V8QImode);
18340 if (elt < 4)
18341 emit_insn (gen_loongson_punpcklbh (t0, d->op0, d->op0));
18342 else
18343 emit_insn (gen_loongson_punpckhbh (t0, d->op0, d->op0));
18344
18345 /* Shuffle that one HImode element into all locations. */
18346 elt &= 3;
18347 elt *= 0x55;
18348 t1 = gen_reg_rtx (V4HImode);
18349 emit_insn (gen_loongson_pshufh (t1, gen_lowpart (V4HImode, t0),
18350 force_reg (SImode, GEN_INT (elt))));
18351
18352 emit_move_insn (d->target, gen_lowpart (V8QImode, t1));
18353 return true;
18354 }
18355
18356 static bool
18357 mips_expand_vec_perm_const_1 (struct expand_vec_perm_d *d)
18358 {
18359 unsigned int i, nelt = d->nelt;
18360 unsigned char perm2[MAX_VECT_LEN];
18361
18362 if (d->one_vector_p)
18363 {
18364 /* Try interleave with alternating operands. */
18365 memcpy (perm2, d->perm, sizeof(perm2));
18366 for (i = 1; i < nelt; i += 2)
18367 perm2[i] += nelt;
18368 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1, perm2, nelt))
18369 return true;
18370 }
18371 else
18372 {
18373 if (mips_expand_vselect_vconcat (d->target, d->op0, d->op1,
18374 d->perm, nelt))
18375 return true;
18376
18377 /* Try again with swapped operands. */
18378 for (i = 0; i < nelt; ++i)
18379 perm2[i] = (d->perm[i] + nelt) & (2 * nelt - 1);
18380 if (mips_expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
18381 return true;
18382 }
18383
18384 if (mips_expand_vpc_loongson_even_odd (d))
18385 return true;
18386 if (mips_expand_vpc_loongson_pshufh (d))
18387 return true;
18388 if (mips_expand_vpc_loongson_bcast (d))
18389 return true;
18390 return false;
18391 }
18392
18393 /* Expand a vec_perm_const pattern. */
18394
18395 bool
18396 mips_expand_vec_perm_const (rtx operands[4])
18397 {
18398 struct expand_vec_perm_d d;
18399 int i, nelt, which;
18400 unsigned char orig_perm[MAX_VECT_LEN];
18401 rtx sel;
18402 bool ok;
18403
18404 d.target = operands[0];
18405 d.op0 = operands[1];
18406 d.op1 = operands[2];
18407 sel = operands[3];
18408
18409 d.vmode = GET_MODE (d.target);
18410 gcc_assert (VECTOR_MODE_P (d.vmode));
18411 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18412 d.testing_p = false;
18413
18414 for (i = which = 0; i < nelt; ++i)
18415 {
18416 rtx e = XVECEXP (sel, 0, i);
18417 int ei = INTVAL (e) & (2 * nelt - 1);
18418 which |= (ei < nelt ? 1 : 2);
18419 orig_perm[i] = ei;
18420 }
18421 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18422
18423 switch (which)
18424 {
18425 default:
18426 gcc_unreachable();
18427
18428 case 3:
18429 d.one_vector_p = false;
18430 if (!rtx_equal_p (d.op0, d.op1))
18431 break;
18432 /* FALLTHRU */
18433
18434 case 2:
18435 for (i = 0; i < nelt; ++i)
18436 d.perm[i] &= nelt - 1;
18437 d.op0 = d.op1;
18438 d.one_vector_p = true;
18439 break;
18440
18441 case 1:
18442 d.op1 = d.op0;
18443 d.one_vector_p = true;
18444 break;
18445 }
18446
18447 ok = mips_expand_vec_perm_const_1 (&d);
18448
18449 /* If we were given a two-vector permutation which just happened to
18450 have both input vectors equal, we folded this into a one-vector
18451 permutation. There are several loongson patterns that are matched
18452 via direct vec_select+vec_concat expansion, but we do not have
18453 support in mips_expand_vec_perm_const_1 to guess the adjustment
18454 that should be made for a single operand. Just try again with
18455 the original permutation. */
18456 if (!ok && which == 3)
18457 {
18458 d.op0 = operands[1];
18459 d.op1 = operands[2];
18460 d.one_vector_p = false;
18461 memcpy (d.perm, orig_perm, MAX_VECT_LEN);
18462 ok = mips_expand_vec_perm_const_1 (&d);
18463 }
18464
18465 return ok;
18466 }
18467
18468 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST_OK. */
18469
18470 static bool
18471 mips_vectorize_vec_perm_const_ok (machine_mode vmode,
18472 const unsigned char *sel)
18473 {
18474 struct expand_vec_perm_d d;
18475 unsigned int i, nelt, which;
18476 bool ret;
18477
18478 d.vmode = vmode;
18479 d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
18480 d.testing_p = true;
18481 memcpy (d.perm, sel, nelt);
18482
18483 /* Categorize the set of elements in the selector. */
18484 for (i = which = 0; i < nelt; ++i)
18485 {
18486 unsigned char e = d.perm[i];
18487 gcc_assert (e < 2 * nelt);
18488 which |= (e < nelt ? 1 : 2);
18489 }
18490
18491 /* For all elements from second vector, fold the elements to first. */
18492 if (which == 2)
18493 for (i = 0; i < nelt; ++i)
18494 d.perm[i] -= nelt;
18495
18496 /* Check whether the mask can be applied to the vector type. */
18497 d.one_vector_p = (which != 3);
18498
18499 d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
18500 d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
18501 if (!d.one_vector_p)
18502 d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
18503
18504 start_sequence ();
18505 ret = mips_expand_vec_perm_const_1 (&d);
18506 end_sequence ();
18507
18508 return ret;
18509 }
18510
18511 /* Expand an integral vector unpack operation. */
18512
18513 void
18514 mips_expand_vec_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18515 {
18516 machine_mode imode = GET_MODE (operands[1]);
18517 rtx (*unpack) (rtx, rtx, rtx);
18518 rtx (*cmpgt) (rtx, rtx, rtx);
18519 rtx tmp, dest, zero;
18520
18521 switch (imode)
18522 {
18523 case V8QImode:
18524 if (high_p)
18525 unpack = gen_loongson_punpckhbh;
18526 else
18527 unpack = gen_loongson_punpcklbh;
18528 cmpgt = gen_loongson_pcmpgtb;
18529 break;
18530 case V4HImode:
18531 if (high_p)
18532 unpack = gen_loongson_punpckhhw;
18533 else
18534 unpack = gen_loongson_punpcklhw;
18535 cmpgt = gen_loongson_pcmpgth;
18536 break;
18537 default:
18538 gcc_unreachable ();
18539 }
18540
18541 zero = force_reg (imode, CONST0_RTX (imode));
18542 if (unsigned_p)
18543 tmp = zero;
18544 else
18545 {
18546 tmp = gen_reg_rtx (imode);
18547 emit_insn (cmpgt (tmp, zero, operands[1]));
18548 }
18549
18550 dest = gen_reg_rtx (imode);
18551 emit_insn (unpack (dest, operands[1], tmp));
18552
18553 emit_move_insn (operands[0], gen_lowpart (GET_MODE (operands[0]), dest));
18554 }
18555
18556 /* A subroutine of mips_expand_vec_init, match constant vector elements. */
18557
18558 static inline bool
18559 mips_constant_elt_p (rtx x)
18560 {
18561 return CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE;
18562 }
18563
18564 /* A subroutine of mips_expand_vec_init, expand via broadcast. */
18565
18566 static void
18567 mips_expand_vi_broadcast (machine_mode vmode, rtx target, rtx elt)
18568 {
18569 struct expand_vec_perm_d d;
18570 rtx t1;
18571 bool ok;
18572
18573 if (elt != const0_rtx)
18574 elt = force_reg (GET_MODE_INNER (vmode), elt);
18575 if (REG_P (elt))
18576 elt = gen_lowpart (DImode, elt);
18577
18578 t1 = gen_reg_rtx (vmode);
18579 switch (vmode)
18580 {
18581 case V8QImode:
18582 emit_insn (gen_loongson_vec_init1_v8qi (t1, elt));
18583 break;
18584 case V4HImode:
18585 emit_insn (gen_loongson_vec_init1_v4hi (t1, elt));
18586 break;
18587 default:
18588 gcc_unreachable ();
18589 }
18590
18591 memset (&d, 0, sizeof (d));
18592 d.target = target;
18593 d.op0 = t1;
18594 d.op1 = t1;
18595 d.vmode = vmode;
18596 d.nelt = GET_MODE_NUNITS (vmode);
18597 d.one_vector_p = true;
18598
18599 ok = mips_expand_vec_perm_const_1 (&d);
18600 gcc_assert (ok);
18601 }
18602
18603 /* A subroutine of mips_expand_vec_init, replacing all of the non-constant
18604 elements of VALS with zeros, copy the constant vector to TARGET. */
18605
18606 static void
18607 mips_expand_vi_constant (machine_mode vmode, unsigned nelt,
18608 rtx target, rtx vals)
18609 {
18610 rtvec vec = shallow_copy_rtvec (XVEC (vals, 0));
18611 unsigned i;
18612
18613 for (i = 0; i < nelt; ++i)
18614 {
18615 if (!mips_constant_elt_p (RTVEC_ELT (vec, i)))
18616 RTVEC_ELT (vec, i) = const0_rtx;
18617 }
18618
18619 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, vec));
18620 }
18621
18622
18623 /* A subroutine of mips_expand_vec_init, expand via pinsrh. */
18624
18625 static void
18626 mips_expand_vi_loongson_one_pinsrh (rtx target, rtx vals, unsigned one_var)
18627 {
18628 mips_expand_vi_constant (V4HImode, 4, target, vals);
18629
18630 emit_insn (gen_vec_setv4hi (target, target, XVECEXP (vals, 0, one_var),
18631 GEN_INT (one_var)));
18632 }
18633
18634 /* A subroutine of mips_expand_vec_init, expand anything via memory. */
18635
18636 static void
18637 mips_expand_vi_general (machine_mode vmode, machine_mode imode,
18638 unsigned nelt, unsigned nvar, rtx target, rtx vals)
18639 {
18640 rtx mem = assign_stack_temp (vmode, GET_MODE_SIZE (vmode));
18641 unsigned int i, isize = GET_MODE_SIZE (imode);
18642
18643 if (nvar < nelt)
18644 mips_expand_vi_constant (vmode, nelt, mem, vals);
18645
18646 for (i = 0; i < nelt; ++i)
18647 {
18648 rtx x = XVECEXP (vals, 0, i);
18649 if (!mips_constant_elt_p (x))
18650 emit_move_insn (adjust_address (mem, imode, i * isize), x);
18651 }
18652
18653 emit_move_insn (target, mem);
18654 }
18655
18656 /* Expand a vector initialization. */
18657
18658 void
18659 mips_expand_vector_init (rtx target, rtx vals)
18660 {
18661 machine_mode vmode = GET_MODE (target);
18662 machine_mode imode = GET_MODE_INNER (vmode);
18663 unsigned i, nelt = GET_MODE_NUNITS (vmode);
18664 unsigned nvar = 0, one_var = -1u;
18665 bool all_same = true;
18666 rtx x;
18667
18668 for (i = 0; i < nelt; ++i)
18669 {
18670 x = XVECEXP (vals, 0, i);
18671 if (!mips_constant_elt_p (x))
18672 nvar++, one_var = i;
18673 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
18674 all_same = false;
18675 }
18676
18677 /* Load constants from the pool, or whatever's handy. */
18678 if (nvar == 0)
18679 {
18680 emit_move_insn (target, gen_rtx_CONST_VECTOR (vmode, XVEC (vals, 0)));
18681 return;
18682 }
18683
18684 /* For two-part initialization, always use CONCAT. */
18685 if (nelt == 2)
18686 {
18687 rtx op0 = force_reg (imode, XVECEXP (vals, 0, 0));
18688 rtx op1 = force_reg (imode, XVECEXP (vals, 0, 1));
18689 x = gen_rtx_VEC_CONCAT (vmode, op0, op1);
18690 emit_insn (gen_rtx_SET (VOIDmode, target, x));
18691 return;
18692 }
18693
18694 /* Loongson is the only cpu with vectors with more elements. */
18695 gcc_assert (TARGET_HARD_FLOAT && TARGET_LOONGSON_VECTORS);
18696
18697 /* If all values are identical, broadcast the value. */
18698 if (all_same)
18699 {
18700 mips_expand_vi_broadcast (vmode, target, XVECEXP (vals, 0, 0));
18701 return;
18702 }
18703
18704 /* If we've only got one non-variable V4HImode, use PINSRH. */
18705 if (nvar == 1 && vmode == V4HImode)
18706 {
18707 mips_expand_vi_loongson_one_pinsrh (target, vals, one_var);
18708 return;
18709 }
18710
18711 mips_expand_vi_general (vmode, imode, nelt, nvar, target, vals);
18712 }
18713
18714 /* Expand a vector reduction. */
18715
18716 void
18717 mips_expand_vec_reduc (rtx target, rtx in, rtx (*gen)(rtx, rtx, rtx))
18718 {
18719 machine_mode vmode = GET_MODE (in);
18720 unsigned char perm2[2];
18721 rtx last, next, fold, x;
18722 bool ok;
18723
18724 last = in;
18725 fold = gen_reg_rtx (vmode);
18726 switch (vmode)
18727 {
18728 case V2SFmode:
18729 /* Use PUL/PLU to produce { L, H } op { H, L }.
18730 By reversing the pair order, rather than a pure interleave high,
18731 we avoid erroneous exceptional conditions that we might otherwise
18732 produce from the computation of H op H. */
18733 perm2[0] = 1;
18734 perm2[1] = 2;
18735 ok = mips_expand_vselect_vconcat (fold, last, last, perm2, 2);
18736 gcc_assert (ok);
18737 break;
18738
18739 case V2SImode:
18740 /* Use interleave to produce { H, L } op { H, H }. */
18741 emit_insn (gen_loongson_punpckhwd (fold, last, last));
18742 break;
18743
18744 case V4HImode:
18745 /* Perform the first reduction with interleave,
18746 and subsequent reductions with shifts. */
18747 emit_insn (gen_loongson_punpckhwd_hi (fold, last, last));
18748
18749 next = gen_reg_rtx (vmode);
18750 emit_insn (gen (next, last, fold));
18751 last = next;
18752
18753 fold = gen_reg_rtx (vmode);
18754 x = force_reg (SImode, GEN_INT (16));
18755 emit_insn (gen_vec_shr_v4hi (fold, last, x));
18756 break;
18757
18758 case V8QImode:
18759 emit_insn (gen_loongson_punpckhwd_qi (fold, last, last));
18760
18761 next = gen_reg_rtx (vmode);
18762 emit_insn (gen (next, last, fold));
18763 last = next;
18764
18765 fold = gen_reg_rtx (vmode);
18766 x = force_reg (SImode, GEN_INT (16));
18767 emit_insn (gen_vec_shr_v8qi (fold, last, x));
18768
18769 next = gen_reg_rtx (vmode);
18770 emit_insn (gen (next, last, fold));
18771 last = next;
18772
18773 fold = gen_reg_rtx (vmode);
18774 x = force_reg (SImode, GEN_INT (8));
18775 emit_insn (gen_vec_shr_v8qi (fold, last, x));
18776 break;
18777
18778 default:
18779 gcc_unreachable ();
18780 }
18781
18782 emit_insn (gen (target, last, fold));
18783 }
18784
18785 /* Expand a vector minimum/maximum. */
18786
18787 void
18788 mips_expand_vec_minmax (rtx target, rtx op0, rtx op1,
18789 rtx (*cmp) (rtx, rtx, rtx), bool min_p)
18790 {
18791 machine_mode vmode = GET_MODE (target);
18792 rtx tc, t0, t1, x;
18793
18794 tc = gen_reg_rtx (vmode);
18795 t0 = gen_reg_rtx (vmode);
18796 t1 = gen_reg_rtx (vmode);
18797
18798 /* op0 > op1 */
18799 emit_insn (cmp (tc, op0, op1));
18800
18801 x = gen_rtx_AND (vmode, tc, (min_p ? op1 : op0));
18802 emit_insn (gen_rtx_SET (VOIDmode, t0, x));
18803
18804 x = gen_rtx_NOT (vmode, tc);
18805 x = gen_rtx_AND (vmode, x, (min_p ? op0 : op1));
18806 emit_insn (gen_rtx_SET (VOIDmode, t1, x));
18807
18808 x = gen_rtx_IOR (vmode, t0, t1);
18809 emit_insn (gen_rtx_SET (VOIDmode, target, x));
18810 }
18811
18812 /* Implement TARGET_CASE_VALUES_THRESHOLD. */
18813
18814 unsigned int
18815 mips_case_values_threshold (void)
18816 {
18817 /* In MIPS16 mode using a larger case threshold generates smaller code. */
18818 if (TARGET_MIPS16 && optimize_size)
18819 return 10;
18820 else
18821 return default_case_values_threshold ();
18822 }
18823
18824 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */
18825
18826 static void
18827 mips_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
18828 {
18829 if (!TARGET_HARD_FLOAT_ABI)
18830 return;
18831 tree exceptions_var = create_tmp_var (MIPS_ATYPE_USI, NULL);
18832 tree fcsr_orig_var = create_tmp_var (MIPS_ATYPE_USI, NULL);
18833 tree fcsr_mod_var = create_tmp_var (MIPS_ATYPE_USI, NULL);
18834 tree get_fcsr = mips_builtin_decls[MIPS_GET_FCSR];
18835 tree set_fcsr = mips_builtin_decls[MIPS_SET_FCSR];
18836 tree get_fcsr_hold_call = build_call_expr (get_fcsr, 0);
18837 tree hold_assign_orig = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
18838 fcsr_orig_var, get_fcsr_hold_call);
18839 tree hold_mod_val = build2 (BIT_AND_EXPR, MIPS_ATYPE_USI, fcsr_orig_var,
18840 build_int_cst (MIPS_ATYPE_USI, 0xfffff003));
18841 tree hold_assign_mod = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
18842 fcsr_mod_var, hold_mod_val);
18843 tree set_fcsr_hold_call = build_call_expr (set_fcsr, 1, fcsr_mod_var);
18844 tree hold_all = build2 (COMPOUND_EXPR, MIPS_ATYPE_USI,
18845 hold_assign_orig, hold_assign_mod);
18846 *hold = build2 (COMPOUND_EXPR, void_type_node, hold_all,
18847 set_fcsr_hold_call);
18848
18849 *clear = build_call_expr (set_fcsr, 1, fcsr_mod_var);
18850
18851 tree get_fcsr_update_call = build_call_expr (get_fcsr, 0);
18852 *update = build2 (MODIFY_EXPR, MIPS_ATYPE_USI,
18853 exceptions_var, get_fcsr_update_call);
18854 tree set_fcsr_update_call = build_call_expr (set_fcsr, 1, fcsr_orig_var);
18855 *update = build2 (COMPOUND_EXPR, void_type_node, *update,
18856 set_fcsr_update_call);
18857 tree atomic_feraiseexcept
18858 = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
18859 tree int_exceptions_var = fold_convert (integer_type_node,
18860 exceptions_var);
18861 tree atomic_feraiseexcept_call = build_call_expr (atomic_feraiseexcept,
18862 1, int_exceptions_var);
18863 *update = build2 (COMPOUND_EXPR, void_type_node, *update,
18864 atomic_feraiseexcept_call);
18865 }
18866
18867 /* Implement TARGET_SPILL_CLASS. */
18868
18869 static reg_class_t
18870 mips_spill_class (reg_class_t rclass ATTRIBUTE_UNUSED,
18871 machine_mode mode ATTRIBUTE_UNUSED)
18872 {
18873 if (TARGET_MIPS16)
18874 return SPILL_REGS;
18875 return NO_REGS;
18876 }
18877
18878 /* Implement TARGET_LRA_P. */
18879
18880 static bool
18881 mips_lra_p (void)
18882 {
18883 return mips_lra_flag;
18884 }
18885 \f
18886 /* Initialize the GCC target structure. */
18887 #undef TARGET_ASM_ALIGNED_HI_OP
18888 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
18889 #undef TARGET_ASM_ALIGNED_SI_OP
18890 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
18891 #undef TARGET_ASM_ALIGNED_DI_OP
18892 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
18893
18894 #undef TARGET_OPTION_OVERRIDE
18895 #define TARGET_OPTION_OVERRIDE mips_option_override
18896
18897 #undef TARGET_LEGITIMIZE_ADDRESS
18898 #define TARGET_LEGITIMIZE_ADDRESS mips_legitimize_address
18899
18900 #undef TARGET_ASM_FUNCTION_PROLOGUE
18901 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
18902 #undef TARGET_ASM_FUNCTION_EPILOGUE
18903 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
18904 #undef TARGET_ASM_SELECT_RTX_SECTION
18905 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
18906 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
18907 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
18908
18909 #undef TARGET_SCHED_INIT
18910 #define TARGET_SCHED_INIT mips_sched_init
18911 #undef TARGET_SCHED_REORDER
18912 #define TARGET_SCHED_REORDER mips_sched_reorder
18913 #undef TARGET_SCHED_REORDER2
18914 #define TARGET_SCHED_REORDER2 mips_sched_reorder2
18915 #undef TARGET_SCHED_VARIABLE_ISSUE
18916 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
18917 #undef TARGET_SCHED_ADJUST_COST
18918 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
18919 #undef TARGET_SCHED_ISSUE_RATE
18920 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
18921 #undef TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN
18922 #define TARGET_SCHED_INIT_DFA_POST_CYCLE_INSN mips_init_dfa_post_cycle_insn
18923 #undef TARGET_SCHED_DFA_POST_ADVANCE_CYCLE
18924 #define TARGET_SCHED_DFA_POST_ADVANCE_CYCLE mips_dfa_post_advance_cycle
18925 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
18926 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
18927 mips_multipass_dfa_lookahead
18928 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
18929 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
18930 mips_small_register_classes_for_mode_p
18931
18932 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
18933 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
18934
18935 #undef TARGET_INSERT_ATTRIBUTES
18936 #define TARGET_INSERT_ATTRIBUTES mips_insert_attributes
18937 #undef TARGET_MERGE_DECL_ATTRIBUTES
18938 #define TARGET_MERGE_DECL_ATTRIBUTES mips_merge_decl_attributes
18939 #undef TARGET_CAN_INLINE_P
18940 #define TARGET_CAN_INLINE_P mips_can_inline_p
18941 #undef TARGET_SET_CURRENT_FUNCTION
18942 #define TARGET_SET_CURRENT_FUNCTION mips_set_current_function
18943
18944 #undef TARGET_VALID_POINTER_MODE
18945 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
18946 #undef TARGET_REGISTER_MOVE_COST
18947 #define TARGET_REGISTER_MOVE_COST mips_register_move_cost
18948 #undef TARGET_REGISTER_PRIORITY
18949 #define TARGET_REGISTER_PRIORITY mips_register_priority
18950 #undef TARGET_MEMORY_MOVE_COST
18951 #define TARGET_MEMORY_MOVE_COST mips_memory_move_cost
18952 #undef TARGET_RTX_COSTS
18953 #define TARGET_RTX_COSTS mips_rtx_costs
18954 #undef TARGET_ADDRESS_COST
18955 #define TARGET_ADDRESS_COST mips_address_cost
18956
18957 #undef TARGET_IN_SMALL_DATA_P
18958 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
18959
18960 #undef TARGET_MACHINE_DEPENDENT_REORG
18961 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
18962
18963 #undef TARGET_PREFERRED_RELOAD_CLASS
18964 #define TARGET_PREFERRED_RELOAD_CLASS mips_preferred_reload_class
18965
18966 #undef TARGET_EXPAND_TO_RTL_HOOK
18967 #define TARGET_EXPAND_TO_RTL_HOOK mips_expand_to_rtl_hook
18968 #undef TARGET_ASM_FILE_START
18969 #define TARGET_ASM_FILE_START mips_file_start
18970 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
18971 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
18972 #undef TARGET_ASM_CODE_END
18973 #define TARGET_ASM_CODE_END mips_code_end
18974
18975 #undef TARGET_INIT_LIBFUNCS
18976 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
18977
18978 #undef TARGET_BUILD_BUILTIN_VA_LIST
18979 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
18980 #undef TARGET_EXPAND_BUILTIN_VA_START
18981 #define TARGET_EXPAND_BUILTIN_VA_START mips_va_start
18982 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
18983 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
18984
18985 #undef TARGET_PROMOTE_FUNCTION_MODE
18986 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
18987 #undef TARGET_PROMOTE_PROTOTYPES
18988 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
18989
18990 #undef TARGET_FUNCTION_VALUE
18991 #define TARGET_FUNCTION_VALUE mips_function_value
18992 #undef TARGET_LIBCALL_VALUE
18993 #define TARGET_LIBCALL_VALUE mips_libcall_value
18994 #undef TARGET_FUNCTION_VALUE_REGNO_P
18995 #define TARGET_FUNCTION_VALUE_REGNO_P mips_function_value_regno_p
18996 #undef TARGET_RETURN_IN_MEMORY
18997 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
18998 #undef TARGET_RETURN_IN_MSB
18999 #define TARGET_RETURN_IN_MSB mips_return_in_msb
19000
19001 #undef TARGET_ASM_OUTPUT_MI_THUNK
19002 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
19003 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
19004 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
19005
19006 #undef TARGET_PRINT_OPERAND
19007 #define TARGET_PRINT_OPERAND mips_print_operand
19008 #undef TARGET_PRINT_OPERAND_ADDRESS
19009 #define TARGET_PRINT_OPERAND_ADDRESS mips_print_operand_address
19010 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
19011 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P mips_print_operand_punct_valid_p
19012
19013 #undef TARGET_SETUP_INCOMING_VARARGS
19014 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
19015 #undef TARGET_STRICT_ARGUMENT_NAMING
19016 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
19017 #undef TARGET_MUST_PASS_IN_STACK
19018 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
19019 #undef TARGET_PASS_BY_REFERENCE
19020 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
19021 #undef TARGET_CALLEE_COPIES
19022 #define TARGET_CALLEE_COPIES mips_callee_copies
19023 #undef TARGET_ARG_PARTIAL_BYTES
19024 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
19025 #undef TARGET_FUNCTION_ARG
19026 #define TARGET_FUNCTION_ARG mips_function_arg
19027 #undef TARGET_FUNCTION_ARG_ADVANCE
19028 #define TARGET_FUNCTION_ARG_ADVANCE mips_function_arg_advance
19029 #undef TARGET_FUNCTION_ARG_BOUNDARY
19030 #define TARGET_FUNCTION_ARG_BOUNDARY mips_function_arg_boundary
19031
19032 #undef TARGET_MODE_REP_EXTENDED
19033 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
19034
19035 #undef TARGET_VECTOR_MODE_SUPPORTED_P
19036 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
19037
19038 #undef TARGET_SCALAR_MODE_SUPPORTED_P
19039 #define TARGET_SCALAR_MODE_SUPPORTED_P mips_scalar_mode_supported_p
19040
19041 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
19042 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE mips_preferred_simd_mode
19043
19044 #undef TARGET_INIT_BUILTINS
19045 #define TARGET_INIT_BUILTINS mips_init_builtins
19046 #undef TARGET_BUILTIN_DECL
19047 #define TARGET_BUILTIN_DECL mips_builtin_decl
19048 #undef TARGET_EXPAND_BUILTIN
19049 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
19050
19051 #undef TARGET_HAVE_TLS
19052 #define TARGET_HAVE_TLS HAVE_AS_TLS
19053
19054 #undef TARGET_CANNOT_FORCE_CONST_MEM
19055 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
19056
19057 #undef TARGET_LEGITIMATE_CONSTANT_P
19058 #define TARGET_LEGITIMATE_CONSTANT_P mips_legitimate_constant_p
19059
19060 #undef TARGET_ENCODE_SECTION_INFO
19061 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
19062
19063 #undef TARGET_ATTRIBUTE_TABLE
19064 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
19065 /* All our function attributes are related to how out-of-line copies should
19066 be compiled or called. They don't in themselves prevent inlining. */
19067 #undef TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P
19068 #define TARGET_FUNCTION_ATTRIBUTE_INLINABLE_P hook_bool_const_tree_true
19069
19070 #undef TARGET_EXTRA_LIVE_ON_ENTRY
19071 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
19072
19073 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
19074 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
19075 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
19076 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
19077
19078 #undef TARGET_COMP_TYPE_ATTRIBUTES
19079 #define TARGET_COMP_TYPE_ATTRIBUTES mips_comp_type_attributes
19080
19081 #ifdef HAVE_AS_DTPRELWORD
19082 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
19083 #define TARGET_ASM_OUTPUT_DWARF_DTPREL mips_output_dwarf_dtprel
19084 #endif
19085 #undef TARGET_DWARF_REGISTER_SPAN
19086 #define TARGET_DWARF_REGISTER_SPAN mips_dwarf_register_span
19087
19088 #undef TARGET_ASM_FINAL_POSTSCAN_INSN
19089 #define TARGET_ASM_FINAL_POSTSCAN_INSN mips_final_postscan_insn
19090
19091 #undef TARGET_LEGITIMATE_ADDRESS_P
19092 #define TARGET_LEGITIMATE_ADDRESS_P mips_legitimate_address_p
19093
19094 #undef TARGET_FRAME_POINTER_REQUIRED
19095 #define TARGET_FRAME_POINTER_REQUIRED mips_frame_pointer_required
19096
19097 #undef TARGET_CAN_ELIMINATE
19098 #define TARGET_CAN_ELIMINATE mips_can_eliminate
19099
19100 #undef TARGET_CONDITIONAL_REGISTER_USAGE
19101 #define TARGET_CONDITIONAL_REGISTER_USAGE mips_conditional_register_usage
19102
19103 #undef TARGET_TRAMPOLINE_INIT
19104 #define TARGET_TRAMPOLINE_INIT mips_trampoline_init
19105
19106 #undef TARGET_ASM_OUTPUT_SOURCE_FILENAME
19107 #define TARGET_ASM_OUTPUT_SOURCE_FILENAME mips_output_filename
19108
19109 #undef TARGET_SHIFT_TRUNCATION_MASK
19110 #define TARGET_SHIFT_TRUNCATION_MASK mips_shift_truncation_mask
19111
19112 #undef TARGET_PREPARE_PCH_SAVE
19113 #define TARGET_PREPARE_PCH_SAVE mips_prepare_pch_save
19114
19115 #undef TARGET_VECTORIZE_VEC_PERM_CONST_OK
19116 #define TARGET_VECTORIZE_VEC_PERM_CONST_OK mips_vectorize_vec_perm_const_ok
19117
19118 #undef TARGET_CASE_VALUES_THRESHOLD
19119 #define TARGET_CASE_VALUES_THRESHOLD mips_case_values_threshold
19120
19121 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
19122 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV mips_atomic_assign_expand_fenv
19123
19124 #undef TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS
19125 #define TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS true
19126
19127 #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
19128 #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
19129 mips_use_by_pieces_infrastructure_p
19130
19131 #undef TARGET_SPILL_CLASS
19132 #define TARGET_SPILL_CLASS mips_spill_class
19133 #undef TARGET_LRA_P
19134 #define TARGET_LRA_P mips_lra_p
19135
19136 struct gcc_target targetm = TARGET_INITIALIZER;
19137 \f
19138 #include "gt-mips.h"