Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / gcc / config / mips / mips.c
blobfe52c59897870988ddb31853666b9ccc68b301bf
1 /* Subroutines used for MIPS code generation.
2 Copyright (C) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 Contributed by A. Lichnewsky, lich@inria.inria.fr.
5 Changes by Michael Meissner, meissner@osf.org.
6 64 bit r4000 support by Ian Lance Taylor, ian@cygnus.com, and
7 Brendan Eich, brendan@microunity.com.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
16 GCC is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING. If not, write to
23 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
24 Boston, MA 02110-1301, USA. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include <signal.h>
31 #include "rtl.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "real.h"
35 #include "insn-config.h"
36 #include "conditions.h"
37 #include "insn-attr.h"
38 #include "recog.h"
39 #include "toplev.h"
40 #include "output.h"
41 #include "tree.h"
42 #include "function.h"
43 #include "expr.h"
44 #include "optabs.h"
45 #include "flags.h"
46 #include "reload.h"
47 #include "tm_p.h"
48 #include "ggc.h"
49 #include "gstab.h"
50 #include "hashtab.h"
51 #include "debug.h"
52 #include "target.h"
53 #include "target-def.h"
54 #include "integrate.h"
55 #include "langhooks.h"
56 #include "cfglayout.h"
57 #include "sched-int.h"
58 #include "tree-gimple.h"
60 /* True if X is an unspec wrapper around a SYMBOL_REF or LABEL_REF. */
61 #define UNSPEC_ADDRESS_P(X) \
62 (GET_CODE (X) == UNSPEC \
63 && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST \
64 && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
66 /* Extract the symbol or label from UNSPEC wrapper X. */
67 #define UNSPEC_ADDRESS(X) \
68 XVECEXP (X, 0, 0)
70 /* Extract the symbol type from UNSPEC wrapper X. */
71 #define UNSPEC_ADDRESS_TYPE(X) \
72 ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
74 /* The maximum distance between the top of the stack frame and the
75 value $sp has when we save & restore registers.
77 Use a maximum gap of 0x100 in the mips16 case. We can then use
78 unextended instructions to save and restore registers, and to
79 allocate and deallocate the top part of the frame.
81 The value in the !mips16 case must be a SMALL_OPERAND and must
82 preserve the maximum stack alignment. */
83 #define MIPS_MAX_FIRST_STACK_STEP (TARGET_MIPS16 ? 0x100 : 0x7ff0)
85 /* True if INSN is a mips.md pattern or asm statement. */
86 #define USEFUL_INSN_P(INSN) \
87 (INSN_P (INSN) \
88 && GET_CODE (PATTERN (INSN)) != USE \
89 && GET_CODE (PATTERN (INSN)) != CLOBBER \
90 && GET_CODE (PATTERN (INSN)) != ADDR_VEC \
91 && GET_CODE (PATTERN (INSN)) != ADDR_DIFF_VEC)
93 /* If INSN is a delayed branch sequence, return the first instruction
94 in the sequence, otherwise return INSN itself. */
95 #define SEQ_BEGIN(INSN) \
96 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
97 ? XVECEXP (PATTERN (INSN), 0, 0) \
98 : (INSN))
100 /* Likewise for the last instruction in a delayed branch sequence. */
101 #define SEQ_END(INSN) \
102 (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE \
103 ? XVECEXP (PATTERN (INSN), 0, XVECLEN (PATTERN (INSN), 0) - 1) \
104 : (INSN))
106 /* Execute the following loop body with SUBINSN set to each instruction
107 between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive. */
108 #define FOR_EACH_SUBINSN(SUBINSN, INSN) \
109 for ((SUBINSN) = SEQ_BEGIN (INSN); \
110 (SUBINSN) != NEXT_INSN (SEQ_END (INSN)); \
111 (SUBINSN) = NEXT_INSN (SUBINSN))
113 /* Classifies an address.
115 ADDRESS_REG
116 A natural register + offset address. The register satisfies
117 mips_valid_base_register_p and the offset is a const_arith_operand.
119 ADDRESS_LO_SUM
120 A LO_SUM rtx. The first operand is a valid base register and
121 the second operand is a symbolic address.
123 ADDRESS_CONST_INT
124 A signed 16-bit constant address.
126 ADDRESS_SYMBOLIC:
127 A constant symbolic address (equivalent to CONSTANT_SYMBOLIC). */
128 enum mips_address_type {
129 ADDRESS_REG,
130 ADDRESS_LO_SUM,
131 ADDRESS_CONST_INT,
132 ADDRESS_SYMBOLIC
135 /* Classifies the prototype of a builtin function. */
136 enum mips_function_type
138 MIPS_V2SF_FTYPE_V2SF,
139 MIPS_V2SF_FTYPE_V2SF_V2SF,
140 MIPS_V2SF_FTYPE_V2SF_V2SF_INT,
141 MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF,
142 MIPS_V2SF_FTYPE_SF_SF,
143 MIPS_INT_FTYPE_V2SF_V2SF,
144 MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF,
145 MIPS_INT_FTYPE_SF_SF,
146 MIPS_INT_FTYPE_DF_DF,
147 MIPS_SF_FTYPE_V2SF,
148 MIPS_SF_FTYPE_SF,
149 MIPS_SF_FTYPE_SF_SF,
150 MIPS_DF_FTYPE_DF,
151 MIPS_DF_FTYPE_DF_DF,
153 /* For MIPS DSP ASE */
154 MIPS_DI_FTYPE_DI_SI,
155 MIPS_DI_FTYPE_DI_SI_SI,
156 MIPS_DI_FTYPE_DI_V2HI_V2HI,
157 MIPS_DI_FTYPE_DI_V4QI_V4QI,
158 MIPS_SI_FTYPE_DI_SI,
159 MIPS_SI_FTYPE_PTR_SI,
160 MIPS_SI_FTYPE_SI,
161 MIPS_SI_FTYPE_SI_SI,
162 MIPS_SI_FTYPE_V2HI,
163 MIPS_SI_FTYPE_V2HI_V2HI,
164 MIPS_SI_FTYPE_V4QI,
165 MIPS_SI_FTYPE_V4QI_V4QI,
166 MIPS_SI_FTYPE_VOID,
167 MIPS_V2HI_FTYPE_SI,
168 MIPS_V2HI_FTYPE_SI_SI,
169 MIPS_V2HI_FTYPE_V2HI,
170 MIPS_V2HI_FTYPE_V2HI_SI,
171 MIPS_V2HI_FTYPE_V2HI_V2HI,
172 MIPS_V2HI_FTYPE_V4QI,
173 MIPS_V2HI_FTYPE_V4QI_V2HI,
174 MIPS_V4QI_FTYPE_SI,
175 MIPS_V4QI_FTYPE_V2HI_V2HI,
176 MIPS_V4QI_FTYPE_V4QI_SI,
177 MIPS_V4QI_FTYPE_V4QI_V4QI,
178 MIPS_VOID_FTYPE_SI_SI,
179 MIPS_VOID_FTYPE_V2HI_V2HI,
180 MIPS_VOID_FTYPE_V4QI_V4QI,
182 /* The last type. */
183 MIPS_MAX_FTYPE_MAX
186 /* Specifies how a builtin function should be converted into rtl. */
187 enum mips_builtin_type
189 /* The builtin corresponds directly to an .md pattern. The return
190 value is mapped to operand 0 and the arguments are mapped to
191 operands 1 and above. */
192 MIPS_BUILTIN_DIRECT,
194 /* The builtin corresponds directly to an .md pattern. There is no return
195 value and the arguments are mapped to operands 0 and above. */
196 MIPS_BUILTIN_DIRECT_NO_TARGET,
198 /* The builtin corresponds to a comparison instruction followed by
199 a mips_cond_move_tf_ps pattern. The first two arguments are the
200 values to compare and the second two arguments are the vector
201 operands for the movt.ps or movf.ps instruction (in assembly order). */
202 MIPS_BUILTIN_MOVF,
203 MIPS_BUILTIN_MOVT,
205 /* The builtin corresponds to a V2SF comparison instruction. Operand 0
206 of this instruction is the result of the comparison, which has mode
207 CCV2 or CCV4. The function arguments are mapped to operands 1 and
208 above. The function's return value is an SImode boolean that is
209 true under the following conditions:
211 MIPS_BUILTIN_CMP_ANY: one of the registers is true
212 MIPS_BUILTIN_CMP_ALL: all of the registers are true
213 MIPS_BUILTIN_CMP_LOWER: the first register is true
214 MIPS_BUILTIN_CMP_UPPER: the second register is true. */
215 MIPS_BUILTIN_CMP_ANY,
216 MIPS_BUILTIN_CMP_ALL,
217 MIPS_BUILTIN_CMP_UPPER,
218 MIPS_BUILTIN_CMP_LOWER,
220 /* As above, but the instruction only sets a single $fcc register. */
221 MIPS_BUILTIN_CMP_SINGLE,
223 /* For generating bposge32 branch instructions in MIPS32 DSP ASE. */
224 MIPS_BUILTIN_BPOSGE32
227 /* Invokes MACRO (COND) for each c.cond.fmt condition. */
228 #define MIPS_FP_CONDITIONS(MACRO) \
229 MACRO (f), \
230 MACRO (un), \
231 MACRO (eq), \
232 MACRO (ueq), \
233 MACRO (olt), \
234 MACRO (ult), \
235 MACRO (ole), \
236 MACRO (ule), \
237 MACRO (sf), \
238 MACRO (ngle), \
239 MACRO (seq), \
240 MACRO (ngl), \
241 MACRO (lt), \
242 MACRO (nge), \
243 MACRO (le), \
244 MACRO (ngt)
246 /* Enumerates the codes above as MIPS_FP_COND_<X>. */
247 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
248 enum mips_fp_condition {
249 MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
252 /* Index X provides the string representation of MIPS_FP_COND_<X>. */
253 #define STRINGIFY(X) #X
254 static const char *const mips_fp_conditions[] = {
255 MIPS_FP_CONDITIONS (STRINGIFY)
258 /* A function to save or store a register. The first argument is the
259 register and the second is the stack slot. */
260 typedef void (*mips_save_restore_fn) (rtx, rtx);
262 struct mips16_constant;
263 struct mips_arg_info;
264 struct mips_address_info;
265 struct mips_integer_op;
266 struct mips_sim;
268 static enum mips_symbol_type mips_classify_symbol (rtx);
269 static void mips_split_const (rtx, rtx *, HOST_WIDE_INT *);
270 static bool mips_offset_within_object_p (rtx, HOST_WIDE_INT);
271 static bool mips_valid_base_register_p (rtx, enum machine_mode, int);
272 static bool mips_symbolic_address_p (enum mips_symbol_type, enum machine_mode);
273 static bool mips_classify_address (struct mips_address_info *, rtx,
274 enum machine_mode, int);
275 static bool mips_cannot_force_const_mem (rtx);
276 static int mips_symbol_insns (enum mips_symbol_type);
277 static bool mips16_unextended_reference_p (enum machine_mode mode, rtx, rtx);
278 static rtx mips_force_temporary (rtx, rtx);
279 static rtx mips_split_symbol (rtx, rtx);
280 static rtx mips_unspec_offset_high (rtx, rtx, rtx, enum mips_symbol_type);
281 static rtx mips_add_offset (rtx, rtx, HOST_WIDE_INT);
282 static unsigned int mips_build_shift (struct mips_integer_op *, HOST_WIDE_INT);
283 static unsigned int mips_build_lower (struct mips_integer_op *,
284 unsigned HOST_WIDE_INT);
285 static unsigned int mips_build_integer (struct mips_integer_op *,
286 unsigned HOST_WIDE_INT);
287 static void mips_move_integer (rtx, unsigned HOST_WIDE_INT);
288 static void mips_legitimize_const_move (enum machine_mode, rtx, rtx);
289 static int m16_check_op (rtx, int, int, int);
290 static bool mips_rtx_costs (rtx, int, int, int *);
291 static int mips_address_cost (rtx);
292 static void mips_emit_compare (enum rtx_code *, rtx *, rtx *, bool);
293 static void mips_load_call_address (rtx, rtx, int);
294 static bool mips_function_ok_for_sibcall (tree, tree);
295 static void mips_block_move_straight (rtx, rtx, HOST_WIDE_INT);
296 static void mips_adjust_block_mem (rtx, HOST_WIDE_INT, rtx *, rtx *);
297 static void mips_block_move_loop (rtx, rtx, HOST_WIDE_INT);
298 static void mips_arg_info (const CUMULATIVE_ARGS *, enum machine_mode,
299 tree, int, struct mips_arg_info *);
300 static bool mips_get_unaligned_mem (rtx *, unsigned int, int, rtx *, rtx *);
301 static void mips_set_architecture (const struct mips_cpu_info *);
302 static void mips_set_tune (const struct mips_cpu_info *);
303 static bool mips_handle_option (size_t, const char *, int);
304 static struct machine_function *mips_init_machine_status (void);
305 static void print_operand_reloc (FILE *, rtx, const char **);
306 #if TARGET_IRIX
307 static void irix_output_external_libcall (rtx);
308 #endif
309 static void mips_file_start (void);
310 static void mips_file_end (void);
311 static bool mips_rewrite_small_data_p (rtx);
312 static int mips_small_data_pattern_1 (rtx *, void *);
313 static int mips_rewrite_small_data_1 (rtx *, void *);
314 static bool mips_function_has_gp_insn (void);
315 static unsigned int mips_global_pointer (void);
316 static bool mips_save_reg_p (unsigned int);
317 static void mips_save_restore_reg (enum machine_mode, int, HOST_WIDE_INT,
318 mips_save_restore_fn);
319 static void mips_for_each_saved_reg (HOST_WIDE_INT, mips_save_restore_fn);
320 static void mips_output_cplocal (void);
321 static void mips_emit_loadgp (void);
322 static void mips_output_function_prologue (FILE *, HOST_WIDE_INT);
323 static void mips_set_frame_expr (rtx);
324 static rtx mips_frame_set (rtx, rtx);
325 static void mips_save_reg (rtx, rtx);
326 static void mips_output_function_epilogue (FILE *, HOST_WIDE_INT);
327 static void mips_restore_reg (rtx, rtx);
328 static void mips_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
329 HOST_WIDE_INT, tree);
330 static int symbolic_expression_p (rtx);
331 static void mips_select_rtx_section (enum machine_mode, rtx,
332 unsigned HOST_WIDE_INT);
333 static void mips_function_rodata_section (tree);
334 static bool mips_in_small_data_p (tree);
335 static int mips_fpr_return_fields (tree, tree *);
336 static bool mips_return_in_msb (tree);
337 static rtx mips_return_fpr_pair (enum machine_mode mode,
338 enum machine_mode mode1, HOST_WIDE_INT,
339 enum machine_mode mode2, HOST_WIDE_INT);
340 static rtx mips16_gp_pseudo_reg (void);
341 static void mips16_fp_args (FILE *, int, int);
342 static void build_mips16_function_stub (FILE *);
343 static rtx dump_constants_1 (enum machine_mode, rtx, rtx);
344 static void dump_constants (struct mips16_constant *, rtx);
345 static int mips16_insn_length (rtx);
346 static int mips16_rewrite_pool_refs (rtx *, void *);
347 static void mips16_lay_out_constants (void);
348 static void mips_sim_reset (struct mips_sim *);
349 static void mips_sim_init (struct mips_sim *, state_t);
350 static void mips_sim_next_cycle (struct mips_sim *);
351 static void mips_sim_wait_reg (struct mips_sim *, rtx, rtx);
352 static int mips_sim_wait_regs_2 (rtx *, void *);
353 static void mips_sim_wait_regs_1 (rtx *, void *);
354 static void mips_sim_wait_regs (struct mips_sim *, rtx);
355 static void mips_sim_wait_units (struct mips_sim *, rtx);
356 static void mips_sim_wait_insn (struct mips_sim *, rtx);
357 static void mips_sim_record_set (rtx, rtx, void *);
358 static void mips_sim_issue_insn (struct mips_sim *, rtx);
359 static void mips_sim_issue_nop (struct mips_sim *);
360 static void mips_sim_finish_insn (struct mips_sim *, rtx);
361 static void vr4130_avoid_branch_rt_conflict (rtx);
362 static void vr4130_align_insns (void);
363 static void mips_avoid_hazard (rtx, rtx, int *, rtx *, rtx);
364 static void mips_avoid_hazards (void);
365 static void mips_reorg (void);
366 static bool mips_strict_matching_cpu_name_p (const char *, const char *);
367 static bool mips_matching_cpu_name_p (const char *, const char *);
368 static const struct mips_cpu_info *mips_parse_cpu (const char *);
369 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
370 static bool mips_return_in_memory (tree, tree);
371 static bool mips_strict_argument_naming (CUMULATIVE_ARGS *);
372 static void mips_macc_chains_record (rtx);
373 static void mips_macc_chains_reorder (rtx *, int);
374 static void vr4130_true_reg_dependence_p_1 (rtx, rtx, void *);
375 static bool vr4130_true_reg_dependence_p (rtx);
376 static bool vr4130_swap_insns_p (rtx, rtx);
377 static void vr4130_reorder (rtx *, int);
378 static void mips_promote_ready (rtx *, int, int);
379 static int mips_sched_reorder (FILE *, int, rtx *, int *, int);
380 static int mips_variable_issue (FILE *, int, rtx, int);
381 static int mips_adjust_cost (rtx, rtx, rtx, int);
382 static int mips_issue_rate (void);
383 static int mips_multipass_dfa_lookahead (void);
384 static void mips_init_libfuncs (void);
385 static void mips_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
386 tree, int *, int);
387 static tree mips_build_builtin_va_list (void);
388 static tree mips_gimplify_va_arg_expr (tree, tree, tree *, tree *);
389 static bool mips_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode mode,
390 tree, bool);
391 static bool mips_callee_copies (CUMULATIVE_ARGS *, enum machine_mode mode,
392 tree, bool);
393 static int mips_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode mode,
394 tree, bool);
395 static bool mips_valid_pointer_mode (enum machine_mode);
396 static bool mips_vector_mode_supported_p (enum machine_mode);
397 static rtx mips_prepare_builtin_arg (enum insn_code, unsigned int, tree *);
398 static rtx mips_prepare_builtin_target (enum insn_code, unsigned int, rtx);
399 static rtx mips_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
400 static void mips_init_builtins (void);
401 static rtx mips_expand_builtin_direct (enum insn_code, rtx, tree, bool);
402 static rtx mips_expand_builtin_movtf (enum mips_builtin_type,
403 enum insn_code, enum mips_fp_condition,
404 rtx, tree);
405 static rtx mips_expand_builtin_compare (enum mips_builtin_type,
406 enum insn_code, enum mips_fp_condition,
407 rtx, tree);
408 static rtx mips_expand_builtin_bposge (enum mips_builtin_type, rtx);
409 static void mips_encode_section_info (tree, rtx, int);
411 /* Structure to be filled in by compute_frame_size with register
412 save masks, and offsets for the current function. */
414 struct mips_frame_info GTY(())
416 HOST_WIDE_INT total_size; /* # bytes that the entire frame takes up */
417 HOST_WIDE_INT var_size; /* # bytes that variables take up */
418 HOST_WIDE_INT args_size; /* # bytes that outgoing arguments take up */
419 HOST_WIDE_INT cprestore_size; /* # bytes that the .cprestore slot takes up */
420 HOST_WIDE_INT gp_reg_size; /* # bytes needed to store gp regs */
421 HOST_WIDE_INT fp_reg_size; /* # bytes needed to store fp regs */
422 unsigned int mask; /* mask of saved gp registers */
423 unsigned int fmask; /* mask of saved fp registers */
424 HOST_WIDE_INT gp_save_offset; /* offset from vfp to store gp registers */
425 HOST_WIDE_INT fp_save_offset; /* offset from vfp to store fp registers */
426 HOST_WIDE_INT gp_sp_offset; /* offset from new sp to store gp registers */
427 HOST_WIDE_INT fp_sp_offset; /* offset from new sp to store fp registers */
428 bool initialized; /* true if frame size already calculated */
429 int num_gp; /* number of gp registers saved */
430 int num_fp; /* number of fp registers saved */
433 struct machine_function GTY(()) {
434 /* Pseudo-reg holding the value of $28 in a mips16 function which
435 refers to GP relative global variables. */
436 rtx mips16_gp_pseudo_rtx;
438 /* The number of extra stack bytes taken up by register varargs.
439 This area is allocated by the callee at the very top of the frame. */
440 int varargs_size;
442 /* Current frame information, calculated by compute_frame_size. */
443 struct mips_frame_info frame;
445 /* The register to use as the global pointer within this function. */
446 unsigned int global_pointer;
448 /* True if mips_adjust_insn_length should ignore an instruction's
449 hazard attribute. */
450 bool ignore_hazard_length_p;
452 /* True if the whole function is suitable for .set noreorder and
453 .set nomacro. */
454 bool all_noreorder_p;
456 /* True if the function is known to have an instruction that needs $gp. */
457 bool has_gp_insn_p;
460 /* Information about a single argument. */
461 struct mips_arg_info
463 /* True if the argument is passed in a floating-point register, or
464 would have been if we hadn't run out of registers. */
465 bool fpr_p;
467 /* The number of words passed in registers, rounded up. */
468 unsigned int reg_words;
470 /* For EABI, the offset of the first register from GP_ARG_FIRST or
471 FP_ARG_FIRST. For other ABIs, the offset of the first register from
472 the start of the ABI's argument structure (see the CUMULATIVE_ARGS
473 comment for details).
475 The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
476 on the stack. */
477 unsigned int reg_offset;
479 /* The number of words that must be passed on the stack, rounded up. */
480 unsigned int stack_words;
482 /* The offset from the start of the stack overflow area of the argument's
483 first stack word. Only meaningful when STACK_WORDS is nonzero. */
484 unsigned int stack_offset;
488 /* Information about an address described by mips_address_type.
490 ADDRESS_CONST_INT
491 No fields are used.
493 ADDRESS_REG
494 REG is the base register and OFFSET is the constant offset.
496 ADDRESS_LO_SUM
497 REG is the register that contains the high part of the address,
498 OFFSET is the symbolic address being referenced and SYMBOL_TYPE
499 is the type of OFFSET's symbol.
501 ADDRESS_SYMBOLIC
502 SYMBOL_TYPE is the type of symbol being referenced. */
504 struct mips_address_info
506 enum mips_address_type type;
507 rtx reg;
508 rtx offset;
509 enum mips_symbol_type symbol_type;
513 /* One stage in a constant building sequence. These sequences have
514 the form:
516 A = VALUE[0]
517 A = A CODE[1] VALUE[1]
518 A = A CODE[2] VALUE[2]
521 where A is an accumulator, each CODE[i] is a binary rtl operation
522 and each VALUE[i] is a constant integer. */
523 struct mips_integer_op {
524 enum rtx_code code;
525 unsigned HOST_WIDE_INT value;
529 /* The largest number of operations needed to load an integer constant.
530 The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
531 When the lowest bit is clear, we can try, but reject a sequence with
532 an extra SLL at the end. */
533 #define MIPS_MAX_INTEGER_OPS 7
536 /* Global variables for machine-dependent things. */
538 /* Threshold for data being put into the small data/bss area, instead
539 of the normal data area. */
540 int mips_section_threshold = -1;
542 /* Count the number of .file directives, so that .loc is up to date. */
543 int num_source_filenames = 0;
545 /* Count the number of sdb related labels are generated (to find block
546 start and end boundaries). */
547 int sdb_label_count = 0;
549 /* Next label # for each statement for Silicon Graphics IRIS systems. */
550 int sym_lineno = 0;
552 /* Linked list of all externals that are to be emitted when optimizing
553 for the global pointer if they haven't been declared by the end of
554 the program with an appropriate .comm or initialization. */
556 struct extern_list GTY (())
558 struct extern_list *next; /* next external */
559 const char *name; /* name of the external */
560 int size; /* size in bytes */
563 static GTY (()) struct extern_list *extern_head = 0;
565 /* Name of the file containing the current function. */
566 const char *current_function_file = "";
568 /* Number of nested .set noreorder, noat, nomacro, and volatile requests. */
569 int set_noreorder;
570 int set_noat;
571 int set_nomacro;
572 int set_volatile;
574 /* The next branch instruction is a branch likely, not branch normal. */
575 int mips_branch_likely;
577 /* The operands passed to the last cmpMM expander. */
578 rtx cmp_operands[2];
580 /* The target cpu for code generation. */
581 enum processor_type mips_arch;
582 const struct mips_cpu_info *mips_arch_info;
584 /* The target cpu for optimization and scheduling. */
585 enum processor_type mips_tune;
586 const struct mips_cpu_info *mips_tune_info;
588 /* Which instruction set architecture to use. */
589 int mips_isa;
591 /* Which ABI to use. */
592 int mips_abi = MIPS_ABI_DEFAULT;
594 /* Cost information to use. */
595 const struct mips_rtx_cost_data *mips_cost;
597 /* Whether we are generating mips16 hard float code. In mips16 mode
598 we always set TARGET_SOFT_FLOAT; this variable is nonzero if
599 -msoft-float was not specified by the user, which means that we
600 should arrange to call mips32 hard floating point code. */
601 int mips16_hard_float;
603 /* The architecture selected by -mipsN. */
604 static const struct mips_cpu_info *mips_isa_info;
606 /* If TRUE, we split addresses into their high and low parts in the RTL. */
607 int mips_split_addresses;
609 /* Mode used for saving/restoring general purpose registers. */
610 static enum machine_mode gpr_mode;
612 /* Array giving truth value on whether or not a given hard register
613 can support a given mode. */
614 char mips_hard_regno_mode_ok[(int)MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
616 /* List of all MIPS punctuation characters used by print_operand. */
617 char mips_print_operand_punct[256];
619 /* Map GCC register number to debugger register number. */
620 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
622 /* A copy of the original flag_delayed_branch: see override_options. */
623 static int mips_flag_delayed_branch;
625 static GTY (()) int mips_output_filename_first_time = 1;
627 /* mips_split_p[X] is true if symbols of type X can be split by
628 mips_split_symbol(). */
629 static bool mips_split_p[NUM_SYMBOL_TYPES];
631 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
632 appears in a LO_SUM. It can be null if such LO_SUMs aren't valid or
633 if they are matched by a special .md file pattern. */
634 static const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
636 /* Likewise for HIGHs. */
637 static const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
639 /* Map hard register number to register class */
640 const enum reg_class mips_regno_to_class[] =
642 LEA_REGS, LEA_REGS, M16_NA_REGS, V1_REG,
643 M16_REGS, M16_REGS, M16_REGS, M16_REGS,
644 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
645 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
646 M16_NA_REGS, M16_NA_REGS, LEA_REGS, LEA_REGS,
647 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
648 T_REG, PIC_FN_ADDR_REG, LEA_REGS, LEA_REGS,
649 LEA_REGS, LEA_REGS, LEA_REGS, LEA_REGS,
650 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
651 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
652 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
653 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
654 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
655 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
656 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
657 FP_REGS, FP_REGS, FP_REGS, FP_REGS,
658 HI_REG, LO_REG, NO_REGS, ST_REGS,
659 ST_REGS, ST_REGS, ST_REGS, ST_REGS,
660 ST_REGS, ST_REGS, ST_REGS, NO_REGS,
661 NO_REGS, ALL_REGS, ALL_REGS, NO_REGS,
662 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
663 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
664 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
665 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
666 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
667 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
668 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
669 COP0_REGS, COP0_REGS, COP0_REGS, COP0_REGS,
670 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
671 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
672 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
673 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
674 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
675 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
676 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
677 COP2_REGS, COP2_REGS, COP2_REGS, COP2_REGS,
678 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
679 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
680 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
681 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
682 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
683 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
684 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
685 COP3_REGS, COP3_REGS, COP3_REGS, COP3_REGS,
686 DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS, DSP_ACC_REGS,
687 DSP_ACC_REGS, DSP_ACC_REGS, ALL_REGS, ALL_REGS,
688 ALL_REGS, ALL_REGS, ALL_REGS, ALL_REGS
691 /* Map register constraint character to register class. */
692 enum reg_class mips_char_to_class[256];
694 /* Table of machine dependent attributes. */
695 const struct attribute_spec mips_attribute_table[] =
697 { "long_call", 0, 0, false, true, true, NULL },
698 { NULL, 0, 0, false, false, false, NULL }
701 /* A table describing all the processors gcc knows about. Names are
702 matched in the order listed. The first mention of an ISA level is
703 taken as the canonical name for that ISA.
705 To ease comparison, please keep this table in the same order as
706 gas's mips_cpu_info_table[]. */
707 const struct mips_cpu_info mips_cpu_info_table[] = {
708 /* Entries for generic ISAs */
709 { "mips1", PROCESSOR_R3000, 1 },
710 { "mips2", PROCESSOR_R6000, 2 },
711 { "mips3", PROCESSOR_R4000, 3 },
712 { "mips4", PROCESSOR_R8000, 4 },
713 { "mips32", PROCESSOR_4KC, 32 },
714 { "mips32r2", PROCESSOR_M4K, 33 },
715 { "mips64", PROCESSOR_5KC, 64 },
716 { "mips64r2", PROCESSOR_5KC, 65 },
718 /* MIPS I */
719 { "r3000", PROCESSOR_R3000, 1 },
720 { "r2000", PROCESSOR_R3000, 1 }, /* = r3000 */
721 { "r3900", PROCESSOR_R3900, 1 },
723 /* MIPS II */
724 { "r6000", PROCESSOR_R6000, 2 },
726 /* MIPS III */
727 { "r4000", PROCESSOR_R4000, 3 },
728 { "vr4100", PROCESSOR_R4100, 3 },
729 { "vr4111", PROCESSOR_R4111, 3 },
730 { "vr4120", PROCESSOR_R4120, 3 },
731 { "vr4130", PROCESSOR_R4130, 3 },
732 { "vr4300", PROCESSOR_R4300, 3 },
733 { "r4400", PROCESSOR_R4000, 3 }, /* = r4000 */
734 { "r4600", PROCESSOR_R4600, 3 },
735 { "orion", PROCESSOR_R4600, 3 }, /* = r4600 */
736 { "r4650", PROCESSOR_R4650, 3 },
738 /* MIPS IV */
739 { "r8000", PROCESSOR_R8000, 4 },
740 { "vr5000", PROCESSOR_R5000, 4 },
741 { "vr5400", PROCESSOR_R5400, 4 },
742 { "vr5500", PROCESSOR_R5500, 4 },
743 { "rm7000", PROCESSOR_R7000, 4 },
744 { "rm9000", PROCESSOR_R9000, 4 },
746 /* MIPS32 */
747 { "4kc", PROCESSOR_4KC, 32 },
748 { "4km", PROCESSOR_4KC, 32 }, /* = 4kc */
749 { "4kp", PROCESSOR_4KP, 32 },
751 /* MIPS32 Release 2 */
752 { "m4k", PROCESSOR_M4K, 33 },
753 { "24k", PROCESSOR_24K, 33 },
754 { "24kc", PROCESSOR_24K, 33 }, /* 24K no FPU */
755 { "24kf", PROCESSOR_24K, 33 }, /* 24K 1:2 FPU */
756 { "24kx", PROCESSOR_24KX, 33 }, /* 24K 1:1 FPU */
758 /* MIPS64 */
759 { "5kc", PROCESSOR_5KC, 64 },
760 { "5kf", PROCESSOR_5KF, 64 },
761 { "20kc", PROCESSOR_20KC, 64 },
762 { "sb1", PROCESSOR_SB1, 64 },
763 { "sr71000", PROCESSOR_SR71000, 64 },
765 /* End marker */
766 { 0, 0, 0 }
769 /* Default costs. If these are used for a processor we should look
770 up the actual costs. */
771 #define DEFAULT_COSTS COSTS_N_INSNS (6), /* fp_add */ \
772 COSTS_N_INSNS (7), /* fp_mult_sf */ \
773 COSTS_N_INSNS (8), /* fp_mult_df */ \
774 COSTS_N_INSNS (23), /* fp_div_sf */ \
775 COSTS_N_INSNS (36), /* fp_div_df */ \
776 COSTS_N_INSNS (10), /* int_mult_si */ \
777 COSTS_N_INSNS (10), /* int_mult_di */ \
778 COSTS_N_INSNS (69), /* int_div_si */ \
779 COSTS_N_INSNS (69), /* int_div_di */ \
780 2, /* branch_cost */ \
781 4 /* memory_latency */
783 /* Need to replace these with the costs of calling the appropriate
784 libgcc routine. */
785 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */ \
786 COSTS_N_INSNS (256), /* fp_mult_sf */ \
787 COSTS_N_INSNS (256), /* fp_mult_df */ \
788 COSTS_N_INSNS (256), /* fp_div_sf */ \
789 COSTS_N_INSNS (256) /* fp_div_df */
791 static struct mips_rtx_cost_data const mips_rtx_cost_data[PROCESSOR_MAX] =
793 { /* R3000 */
794 COSTS_N_INSNS (2), /* fp_add */
795 COSTS_N_INSNS (4), /* fp_mult_sf */
796 COSTS_N_INSNS (5), /* fp_mult_df */
797 COSTS_N_INSNS (12), /* fp_div_sf */
798 COSTS_N_INSNS (19), /* fp_div_df */
799 COSTS_N_INSNS (12), /* int_mult_si */
800 COSTS_N_INSNS (12), /* int_mult_di */
801 COSTS_N_INSNS (35), /* int_div_si */
802 COSTS_N_INSNS (35), /* int_div_di */
803 1, /* branch_cost */
804 4 /* memory_latency */
807 { /* 4KC */
808 SOFT_FP_COSTS,
809 COSTS_N_INSNS (6), /* int_mult_si */
810 COSTS_N_INSNS (6), /* int_mult_di */
811 COSTS_N_INSNS (36), /* int_div_si */
812 COSTS_N_INSNS (36), /* int_div_di */
813 1, /* branch_cost */
814 4 /* memory_latency */
816 { /* 4KP */
817 SOFT_FP_COSTS,
818 COSTS_N_INSNS (36), /* int_mult_si */
819 COSTS_N_INSNS (36), /* int_mult_di */
820 COSTS_N_INSNS (37), /* int_div_si */
821 COSTS_N_INSNS (37), /* int_div_di */
822 1, /* branch_cost */
823 4 /* memory_latency */
825 { /* 5KC */
826 SOFT_FP_COSTS,
827 COSTS_N_INSNS (4), /* int_mult_si */
828 COSTS_N_INSNS (11), /* int_mult_di */
829 COSTS_N_INSNS (36), /* int_div_si */
830 COSTS_N_INSNS (68), /* int_div_di */
831 1, /* branch_cost */
832 4 /* memory_latency */
834 { /* 5KF */
835 COSTS_N_INSNS (4), /* fp_add */
836 COSTS_N_INSNS (4), /* fp_mult_sf */
837 COSTS_N_INSNS (5), /* fp_mult_df */
838 COSTS_N_INSNS (17), /* fp_div_sf */
839 COSTS_N_INSNS (32), /* fp_div_df */
840 COSTS_N_INSNS (4), /* int_mult_si */
841 COSTS_N_INSNS (11), /* int_mult_di */
842 COSTS_N_INSNS (36), /* int_div_si */
843 COSTS_N_INSNS (68), /* int_div_di */
844 1, /* branch_cost */
845 4 /* memory_latency */
847 { /* 20KC */
848 DEFAULT_COSTS
850 { /* 24k */
851 COSTS_N_INSNS (8), /* fp_add */
852 COSTS_N_INSNS (8), /* fp_mult_sf */
853 COSTS_N_INSNS (10), /* fp_mult_df */
854 COSTS_N_INSNS (34), /* fp_div_sf */
855 COSTS_N_INSNS (64), /* fp_div_df */
856 COSTS_N_INSNS (5), /* int_mult_si */
857 COSTS_N_INSNS (5), /* int_mult_di */
858 COSTS_N_INSNS (41), /* int_div_si */
859 COSTS_N_INSNS (41), /* int_div_di */
860 1, /* branch_cost */
861 4 /* memory_latency */
863 { /* 24kx */
864 COSTS_N_INSNS (4), /* fp_add */
865 COSTS_N_INSNS (4), /* fp_mult_sf */
866 COSTS_N_INSNS (5), /* fp_mult_df */
867 COSTS_N_INSNS (17), /* fp_div_sf */
868 COSTS_N_INSNS (32), /* fp_div_df */
869 COSTS_N_INSNS (5), /* int_mult_si */
870 COSTS_N_INSNS (5), /* int_mult_di */
871 COSTS_N_INSNS (41), /* int_div_si */
872 COSTS_N_INSNS (41), /* int_div_di */
873 1, /* branch_cost */
874 4 /* memory_latency */
876 { /* M4k */
877 DEFAULT_COSTS
879 { /* R3900 */
880 COSTS_N_INSNS (2), /* fp_add */
881 COSTS_N_INSNS (4), /* fp_mult_sf */
882 COSTS_N_INSNS (5), /* fp_mult_df */
883 COSTS_N_INSNS (12), /* fp_div_sf */
884 COSTS_N_INSNS (19), /* fp_div_df */
885 COSTS_N_INSNS (2), /* int_mult_si */
886 COSTS_N_INSNS (2), /* int_mult_di */
887 COSTS_N_INSNS (35), /* int_div_si */
888 COSTS_N_INSNS (35), /* int_div_di */
889 1, /* branch_cost */
890 4 /* memory_latency */
892 { /* R6000 */
893 COSTS_N_INSNS (3), /* fp_add */
894 COSTS_N_INSNS (5), /* fp_mult_sf */
895 COSTS_N_INSNS (6), /* fp_mult_df */
896 COSTS_N_INSNS (15), /* fp_div_sf */
897 COSTS_N_INSNS (16), /* fp_div_df */
898 COSTS_N_INSNS (17), /* int_mult_si */
899 COSTS_N_INSNS (17), /* int_mult_di */
900 COSTS_N_INSNS (38), /* int_div_si */
901 COSTS_N_INSNS (38), /* int_div_di */
902 2, /* branch_cost */
903 6 /* memory_latency */
905 { /* R4000 */
906 COSTS_N_INSNS (6), /* fp_add */
907 COSTS_N_INSNS (7), /* fp_mult_sf */
908 COSTS_N_INSNS (8), /* fp_mult_df */
909 COSTS_N_INSNS (23), /* fp_div_sf */
910 COSTS_N_INSNS (36), /* fp_div_df */
911 COSTS_N_INSNS (10), /* int_mult_si */
912 COSTS_N_INSNS (10), /* int_mult_di */
913 COSTS_N_INSNS (69), /* int_div_si */
914 COSTS_N_INSNS (69), /* int_div_di */
915 2, /* branch_cost */
916 6 /* memory_latency */
918 { /* R4100 */
919 DEFAULT_COSTS
921 { /* R4111 */
922 DEFAULT_COSTS
924 { /* R4120 */
925 DEFAULT_COSTS
927 { /* R4130 */
928 /* The only costs that appear to be updated here are
929 integer multiplication. */
930 SOFT_FP_COSTS,
931 COSTS_N_INSNS (4), /* int_mult_si */
932 COSTS_N_INSNS (6), /* int_mult_di */
933 COSTS_N_INSNS (69), /* int_div_si */
934 COSTS_N_INSNS (69), /* int_div_di */
935 1, /* branch_cost */
936 4 /* memory_latency */
938 { /* R4300 */
939 DEFAULT_COSTS
941 { /* R4600 */
942 DEFAULT_COSTS
944 { /* R4650 */
945 DEFAULT_COSTS
947 { /* R5000 */
948 COSTS_N_INSNS (6), /* fp_add */
949 COSTS_N_INSNS (4), /* fp_mult_sf */
950 COSTS_N_INSNS (5), /* fp_mult_df */
951 COSTS_N_INSNS (23), /* fp_div_sf */
952 COSTS_N_INSNS (36), /* fp_div_df */
953 COSTS_N_INSNS (5), /* int_mult_si */
954 COSTS_N_INSNS (5), /* int_mult_di */
955 COSTS_N_INSNS (36), /* int_div_si */
956 COSTS_N_INSNS (36), /* int_div_di */
957 1, /* branch_cost */
958 4 /* memory_latency */
960 { /* R5400 */
961 COSTS_N_INSNS (6), /* fp_add */
962 COSTS_N_INSNS (5), /* fp_mult_sf */
963 COSTS_N_INSNS (6), /* fp_mult_df */
964 COSTS_N_INSNS (30), /* fp_div_sf */
965 COSTS_N_INSNS (59), /* fp_div_df */
966 COSTS_N_INSNS (3), /* int_mult_si */
967 COSTS_N_INSNS (4), /* int_mult_di */
968 COSTS_N_INSNS (42), /* int_div_si */
969 COSTS_N_INSNS (74), /* int_div_di */
970 1, /* branch_cost */
971 4 /* memory_latency */
973 { /* R5500 */
974 COSTS_N_INSNS (6), /* fp_add */
975 COSTS_N_INSNS (5), /* fp_mult_sf */
976 COSTS_N_INSNS (6), /* fp_mult_df */
977 COSTS_N_INSNS (30), /* fp_div_sf */
978 COSTS_N_INSNS (59), /* fp_div_df */
979 COSTS_N_INSNS (5), /* int_mult_si */
980 COSTS_N_INSNS (9), /* int_mult_di */
981 COSTS_N_INSNS (42), /* int_div_si */
982 COSTS_N_INSNS (74), /* int_div_di */
983 1, /* branch_cost */
984 4 /* memory_latency */
986 { /* R7000 */
987 /* The only costs that are changed here are
988 integer multiplication. */
989 COSTS_N_INSNS (6), /* fp_add */
990 COSTS_N_INSNS (7), /* fp_mult_sf */
991 COSTS_N_INSNS (8), /* fp_mult_df */
992 COSTS_N_INSNS (23), /* fp_div_sf */
993 COSTS_N_INSNS (36), /* fp_div_df */
994 COSTS_N_INSNS (5), /* int_mult_si */
995 COSTS_N_INSNS (9), /* int_mult_di */
996 COSTS_N_INSNS (69), /* int_div_si */
997 COSTS_N_INSNS (69), /* int_div_di */
998 1, /* branch_cost */
999 4 /* memory_latency */
1001 { /* R8000 */
1002 DEFAULT_COSTS
1004 { /* R9000 */
1005 /* The only costs that are changed here are
1006 integer multiplication. */
1007 COSTS_N_INSNS (6), /* fp_add */
1008 COSTS_N_INSNS (7), /* fp_mult_sf */
1009 COSTS_N_INSNS (8), /* fp_mult_df */
1010 COSTS_N_INSNS (23), /* fp_div_sf */
1011 COSTS_N_INSNS (36), /* fp_div_df */
1012 COSTS_N_INSNS (3), /* int_mult_si */
1013 COSTS_N_INSNS (8), /* int_mult_di */
1014 COSTS_N_INSNS (69), /* int_div_si */
1015 COSTS_N_INSNS (69), /* int_div_di */
1016 1, /* branch_cost */
1017 4 /* memory_latency */
1019 { /* SB1 */
1020 COSTS_N_INSNS (4), /* fp_add */
1021 COSTS_N_INSNS (4), /* fp_mult_sf */
1022 COSTS_N_INSNS (4), /* fp_mult_df */
1023 COSTS_N_INSNS (24), /* fp_div_sf */
1024 COSTS_N_INSNS (32), /* fp_div_df */
1025 COSTS_N_INSNS (3), /* int_mult_si */
1026 COSTS_N_INSNS (4), /* int_mult_di */
1027 COSTS_N_INSNS (36), /* int_div_si */
1028 COSTS_N_INSNS (68), /* int_div_di */
1029 1, /* branch_cost */
1030 4 /* memory_latency */
1032 { /* SR71000 */
1033 DEFAULT_COSTS
1038 /* Nonzero if -march should decide the default value of MASK_SOFT_FLOAT. */
1039 #ifndef MIPS_MARCH_CONTROLS_SOFT_FLOAT
1040 #define MIPS_MARCH_CONTROLS_SOFT_FLOAT 0
1041 #endif
1043 /* Initialize the GCC target structure. */
1044 #undef TARGET_ASM_ALIGNED_HI_OP
1045 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
1046 #undef TARGET_ASM_ALIGNED_SI_OP
1047 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
1048 #undef TARGET_ASM_ALIGNED_DI_OP
1049 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
1051 #undef TARGET_ASM_FUNCTION_PROLOGUE
1052 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
1053 #undef TARGET_ASM_FUNCTION_EPILOGUE
1054 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
1055 #undef TARGET_ASM_SELECT_RTX_SECTION
1056 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
1057 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
1058 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
1060 #undef TARGET_SCHED_REORDER
1061 #define TARGET_SCHED_REORDER mips_sched_reorder
1062 #undef TARGET_SCHED_VARIABLE_ISSUE
1063 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
1064 #undef TARGET_SCHED_ADJUST_COST
1065 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
1066 #undef TARGET_SCHED_ISSUE_RATE
1067 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
1068 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1069 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
1070 mips_multipass_dfa_lookahead
1072 #undef TARGET_DEFAULT_TARGET_FLAGS
1073 #define TARGET_DEFAULT_TARGET_FLAGS \
1074 (TARGET_DEFAULT \
1075 | TARGET_CPU_DEFAULT \
1076 | TARGET_ENDIAN_DEFAULT \
1077 | TARGET_FP_EXCEPTIONS_DEFAULT \
1078 | MASK_CHECK_ZERO_DIV \
1079 | MASK_FUSED_MADD)
1080 #undef TARGET_HANDLE_OPTION
1081 #define TARGET_HANDLE_OPTION mips_handle_option
1083 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1084 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
1086 #undef TARGET_VALID_POINTER_MODE
1087 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
1088 #undef TARGET_RTX_COSTS
1089 #define TARGET_RTX_COSTS mips_rtx_costs
1090 #undef TARGET_ADDRESS_COST
1091 #define TARGET_ADDRESS_COST mips_address_cost
1093 #undef TARGET_IN_SMALL_DATA_P
1094 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
1096 #undef TARGET_MACHINE_DEPENDENT_REORG
1097 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
1099 #undef TARGET_ASM_FILE_START
1100 #undef TARGET_ASM_FILE_END
1101 #define TARGET_ASM_FILE_START mips_file_start
1102 #define TARGET_ASM_FILE_END mips_file_end
1103 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
1104 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
1106 #undef TARGET_INIT_LIBFUNCS
1107 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
1109 #undef TARGET_BUILD_BUILTIN_VA_LIST
1110 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
1111 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1112 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
1114 #undef TARGET_PROMOTE_FUNCTION_ARGS
1115 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
1116 #undef TARGET_PROMOTE_FUNCTION_RETURN
1117 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
1118 #undef TARGET_PROMOTE_PROTOTYPES
1119 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
1121 #undef TARGET_RETURN_IN_MEMORY
1122 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
1123 #undef TARGET_RETURN_IN_MSB
1124 #define TARGET_RETURN_IN_MSB mips_return_in_msb
1126 #undef TARGET_ASM_OUTPUT_MI_THUNK
1127 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
1128 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1129 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
1131 #undef TARGET_SETUP_INCOMING_VARARGS
1132 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
1133 #undef TARGET_STRICT_ARGUMENT_NAMING
1134 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
1135 #undef TARGET_MUST_PASS_IN_STACK
1136 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
1137 #undef TARGET_PASS_BY_REFERENCE
1138 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
1139 #undef TARGET_CALLEE_COPIES
1140 #define TARGET_CALLEE_COPIES mips_callee_copies
1141 #undef TARGET_ARG_PARTIAL_BYTES
1142 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
1144 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1145 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
1147 #undef TARGET_INIT_BUILTINS
1148 #define TARGET_INIT_BUILTINS mips_init_builtins
1149 #undef TARGET_EXPAND_BUILTIN
1150 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
1152 #undef TARGET_HAVE_TLS
1153 #define TARGET_HAVE_TLS HAVE_AS_TLS
1155 #undef TARGET_CANNOT_FORCE_CONST_MEM
1156 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
1158 #undef TARGET_ENCODE_SECTION_INFO
1159 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
1161 #undef TARGET_ATTRIBUTE_TABLE
1162 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
1164 struct gcc_target targetm = TARGET_INITIALIZER;
1166 /* Classify symbol X, which must be a SYMBOL_REF or a LABEL_REF. */
1168 static enum mips_symbol_type
1169 mips_classify_symbol (rtx x)
1171 if (GET_CODE (x) == LABEL_REF)
1173 if (TARGET_MIPS16)
1174 return SYMBOL_CONSTANT_POOL;
1175 if (TARGET_ABICALLS)
1176 return SYMBOL_GOT_LOCAL;
1177 return SYMBOL_GENERAL;
1180 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1182 if (SYMBOL_REF_TLS_MODEL (x))
1183 return SYMBOL_TLS;
1185 if (CONSTANT_POOL_ADDRESS_P (x))
1187 if (TARGET_MIPS16)
1188 return SYMBOL_CONSTANT_POOL;
1190 if (TARGET_ABICALLS)
1191 return SYMBOL_GOT_LOCAL;
1193 if (GET_MODE_SIZE (get_pool_mode (x)) <= mips_section_threshold)
1194 return SYMBOL_SMALL_DATA;
1196 return SYMBOL_GENERAL;
1199 if (SYMBOL_REF_SMALL_P (x))
1200 return SYMBOL_SMALL_DATA;
1202 if (TARGET_ABICALLS)
1204 if (SYMBOL_REF_DECL (x) == 0)
1205 return SYMBOL_REF_LOCAL_P (x) ? SYMBOL_GOT_LOCAL : SYMBOL_GOT_GLOBAL;
1207 /* There are three cases to consider:
1209 - o32 PIC (either with or without explicit relocs)
1210 - n32/n64 PIC without explicit relocs
1211 - n32/n64 PIC with explicit relocs
1213 In the first case, both local and global accesses will use an
1214 R_MIPS_GOT16 relocation. We must correctly predict which of
1215 the two semantics (local or global) the assembler and linker
1216 will apply. The choice doesn't depend on the symbol's
1217 visibility, so we deliberately ignore decl_visibility and
1218 binds_local_p here.
1220 In the second case, the assembler will not use R_MIPS_GOT16
1221 relocations, but it chooses between local and global accesses
1222 in the same way as for o32 PIC.
1224 In the third case we have more freedom since both forms of
1225 access will work for any kind of symbol. However, there seems
1226 little point in doing things differently. */
1227 if (DECL_P (SYMBOL_REF_DECL (x)) && TREE_PUBLIC (SYMBOL_REF_DECL (x)))
1228 return SYMBOL_GOT_GLOBAL;
1230 return SYMBOL_GOT_LOCAL;
1233 return SYMBOL_GENERAL;
1237 /* Split X into a base and a constant offset, storing them in *BASE
1238 and *OFFSET respectively. */
1240 static void
1241 mips_split_const (rtx x, rtx *base, HOST_WIDE_INT *offset)
1243 *offset = 0;
1245 if (GET_CODE (x) == CONST)
1246 x = XEXP (x, 0);
1248 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
1250 *offset += INTVAL (XEXP (x, 1));
1251 x = XEXP (x, 0);
1253 *base = x;
1256 /* Classify symbolic expression X, given that it appears in context
1257 CONTEXT. */
1259 static enum mips_symbol_type
1260 mips_classify_symbolic_expression (rtx x)
1262 HOST_WIDE_INT offset;
1264 mips_split_const (x, &x, &offset);
1265 if (UNSPEC_ADDRESS_P (x))
1266 return UNSPEC_ADDRESS_TYPE (x);
1268 return mips_classify_symbol (x);
1271 /* Return true if SYMBOL is a SYMBOL_REF and OFFSET + SYMBOL points
1272 to the same object as SYMBOL. */
1274 static bool
1275 mips_offset_within_object_p (rtx symbol, HOST_WIDE_INT offset)
1277 if (GET_CODE (symbol) != SYMBOL_REF)
1278 return false;
1280 if (CONSTANT_POOL_ADDRESS_P (symbol)
1281 && offset >= 0
1282 && offset < (int) GET_MODE_SIZE (get_pool_mode (symbol)))
1283 return true;
1285 if (SYMBOL_REF_DECL (symbol) != 0
1286 && offset >= 0
1287 && offset < int_size_in_bytes (TREE_TYPE (SYMBOL_REF_DECL (symbol))))
1288 return true;
1290 return false;
1294 /* Return true if X is a symbolic constant that can be calculated in
1295 the same way as a bare symbol. If it is, store the type of the
1296 symbol in *SYMBOL_TYPE. */
1298 bool
1299 mips_symbolic_constant_p (rtx x, enum mips_symbol_type *symbol_type)
1301 HOST_WIDE_INT offset;
1303 mips_split_const (x, &x, &offset);
1304 if (UNSPEC_ADDRESS_P (x))
1305 *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1306 else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1308 *symbol_type = mips_classify_symbol (x);
1309 if (*symbol_type == SYMBOL_TLS)
1310 return false;
1312 else
1313 return false;
1315 if (offset == 0)
1316 return true;
1318 /* Check whether a nonzero offset is valid for the underlying
1319 relocations. */
1320 switch (*symbol_type)
1322 case SYMBOL_GENERAL:
1323 case SYMBOL_64_HIGH:
1324 case SYMBOL_64_MID:
1325 case SYMBOL_64_LOW:
1326 /* If the target has 64-bit pointers and the object file only
1327 supports 32-bit symbols, the values of those symbols will be
1328 sign-extended. In this case we can't allow an arbitrary offset
1329 in case the 32-bit value X + OFFSET has a different sign from X. */
1330 if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1331 return mips_offset_within_object_p (x, offset);
1333 /* In other cases the relocations can handle any offset. */
1334 return true;
1336 case SYMBOL_CONSTANT_POOL:
1337 /* Allow constant pool references to be converted to LABEL+CONSTANT.
1338 In this case, we no longer have access to the underlying constant,
1339 but the original symbol-based access was known to be valid. */
1340 if (GET_CODE (x) == LABEL_REF)
1341 return true;
1343 /* Fall through. */
1345 case SYMBOL_SMALL_DATA:
1346 /* Make sure that the offset refers to something within the
1347 underlying object. This should guarantee that the final
1348 PC- or GP-relative offset is within the 16-bit limit. */
1349 return mips_offset_within_object_p (x, offset);
1351 case SYMBOL_GOT_LOCAL:
1352 case SYMBOL_GOTOFF_PAGE:
1353 /* The linker should provide enough local GOT entries for a
1354 16-bit offset. Larger offsets may lead to GOT overflow. */
1355 return SMALL_OPERAND (offset);
1357 case SYMBOL_GOT_GLOBAL:
1358 case SYMBOL_GOTOFF_GLOBAL:
1359 case SYMBOL_GOTOFF_CALL:
1360 case SYMBOL_GOTOFF_LOADGP:
1361 case SYMBOL_TLSGD:
1362 case SYMBOL_TLSLDM:
1363 case SYMBOL_DTPREL:
1364 case SYMBOL_TPREL:
1365 case SYMBOL_GOTTPREL:
1366 case SYMBOL_TLS:
1367 return false;
1369 gcc_unreachable ();
1373 /* Return true if X is a symbolic constant whose value is not split
1374 into separate relocations. */
1376 bool
1377 mips_atomic_symbolic_constant_p (rtx x)
1379 enum mips_symbol_type type;
1380 return mips_symbolic_constant_p (x, &type) && !mips_split_p[type];
1384 /* This function is used to implement REG_MODE_OK_FOR_BASE_P. */
1387 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode, int strict)
1389 if (regno >= FIRST_PSEUDO_REGISTER)
1391 if (!strict)
1392 return true;
1393 regno = reg_renumber[regno];
1396 /* These fake registers will be eliminated to either the stack or
1397 hard frame pointer, both of which are usually valid base registers.
1398 Reload deals with the cases where the eliminated form isn't valid. */
1399 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
1400 return true;
1402 /* In mips16 mode, the stack pointer can only address word and doubleword
1403 values, nothing smaller. There are two problems here:
1405 (a) Instantiating virtual registers can introduce new uses of the
1406 stack pointer. If these virtual registers are valid addresses,
1407 the stack pointer should be too.
1409 (b) Most uses of the stack pointer are not made explicit until
1410 FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
1411 We don't know until that stage whether we'll be eliminating to the
1412 stack pointer (which needs the restriction) or the hard frame
1413 pointer (which doesn't).
1415 All in all, it seems more consistent to only enforce this restriction
1416 during and after reload. */
1417 if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
1418 return !strict || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1420 return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
1424 /* Return true if X is a valid base register for the given mode.
1425 Allow only hard registers if STRICT. */
1427 static bool
1428 mips_valid_base_register_p (rtx x, enum machine_mode mode, int strict)
1430 if (!strict && GET_CODE (x) == SUBREG)
1431 x = SUBREG_REG (x);
1433 return (REG_P (x)
1434 && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict));
1438 /* Return true if symbols of type SYMBOL_TYPE can directly address a value
1439 with mode MODE. This is used for both symbolic and LO_SUM addresses. */
1441 static bool
1442 mips_symbolic_address_p (enum mips_symbol_type symbol_type,
1443 enum machine_mode mode)
1445 switch (symbol_type)
1447 case SYMBOL_GENERAL:
1448 return !TARGET_MIPS16;
1450 case SYMBOL_SMALL_DATA:
1451 return true;
1453 case SYMBOL_CONSTANT_POOL:
1454 /* PC-relative addressing is only available for lw and ld. */
1455 return GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1457 case SYMBOL_GOT_LOCAL:
1458 return true;
1460 case SYMBOL_GOT_GLOBAL:
1461 /* The address will have to be loaded from the GOT first. */
1462 return false;
1464 case SYMBOL_GOTOFF_PAGE:
1465 case SYMBOL_GOTOFF_GLOBAL:
1466 case SYMBOL_GOTOFF_CALL:
1467 case SYMBOL_GOTOFF_LOADGP:
1468 case SYMBOL_TLS:
1469 case SYMBOL_TLSGD:
1470 case SYMBOL_TLSLDM:
1471 case SYMBOL_DTPREL:
1472 case SYMBOL_GOTTPREL:
1473 case SYMBOL_TPREL:
1474 case SYMBOL_64_HIGH:
1475 case SYMBOL_64_MID:
1476 case SYMBOL_64_LOW:
1477 return true;
1479 gcc_unreachable ();
1483 /* Return true if X is a valid address for machine mode MODE. If it is,
1484 fill in INFO appropriately. STRICT is true if we should only accept
1485 hard base registers. */
1487 static bool
1488 mips_classify_address (struct mips_address_info *info, rtx x,
1489 enum machine_mode mode, int strict)
1491 switch (GET_CODE (x))
1493 case REG:
1494 case SUBREG:
1495 info->type = ADDRESS_REG;
1496 info->reg = x;
1497 info->offset = const0_rtx;
1498 return mips_valid_base_register_p (info->reg, mode, strict);
1500 case PLUS:
1501 info->type = ADDRESS_REG;
1502 info->reg = XEXP (x, 0);
1503 info->offset = XEXP (x, 1);
1504 return (mips_valid_base_register_p (info->reg, mode, strict)
1505 && const_arith_operand (info->offset, VOIDmode));
1507 case LO_SUM:
1508 info->type = ADDRESS_LO_SUM;
1509 info->reg = XEXP (x, 0);
1510 info->offset = XEXP (x, 1);
1511 /* We have to trust the creator of the LO_SUM to do something vaguely
1512 sane. Target-independent code that creates a LO_SUM should also
1513 create and verify the matching HIGH. Target-independent code that
1514 adds an offset to a LO_SUM must prove that the offset will not
1515 induce a carry. Failure to do either of these things would be
1516 a bug, and we are not required to check for it here. The MIPS
1517 backend itself should only create LO_SUMs for valid symbolic
1518 constants, with the high part being either a HIGH or a copy
1519 of _gp. */
1520 info->symbol_type = mips_classify_symbolic_expression (info->offset);
1521 return (mips_valid_base_register_p (info->reg, mode, strict)
1522 && mips_symbolic_address_p (info->symbol_type, mode)
1523 && mips_lo_relocs[info->symbol_type] != 0);
1525 case CONST_INT:
1526 /* Small-integer addresses don't occur very often, but they
1527 are legitimate if $0 is a valid base register. */
1528 info->type = ADDRESS_CONST_INT;
1529 return !TARGET_MIPS16 && SMALL_INT (x);
1531 case CONST:
1532 case LABEL_REF:
1533 case SYMBOL_REF:
1534 info->type = ADDRESS_SYMBOLIC;
1535 return (mips_symbolic_constant_p (x, &info->symbol_type)
1536 && mips_symbolic_address_p (info->symbol_type, mode)
1537 && !mips_split_p[info->symbol_type]);
1539 default:
1540 return false;
1544 /* Return true if X is a thread-local symbol. */
1546 static bool
1547 mips_tls_operand_p (rtx x)
1549 return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1552 /* Return true if X can not be forced into a constant pool. */
1554 static int
1555 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1557 return mips_tls_operand_p (*x);
1560 /* Return true if X can not be forced into a constant pool. */
1562 static bool
1563 mips_cannot_force_const_mem (rtx x)
1565 if (! TARGET_HAVE_TLS)
1566 return false;
1568 return for_each_rtx (&x, &mips_tls_symbol_ref_1, 0);
1571 /* Return the number of instructions needed to load a symbol of the
1572 given type into a register. If valid in an address, the same number
1573 of instructions are needed for loads and stores. Treat extended
1574 mips16 instructions as two instructions. */
1576 static int
1577 mips_symbol_insns (enum mips_symbol_type type)
1579 switch (type)
1581 case SYMBOL_GENERAL:
1582 /* In mips16 code, general symbols must be fetched from the
1583 constant pool. */
1584 if (TARGET_MIPS16)
1585 return 0;
1587 /* When using 64-bit symbols, we need 5 preparatory instructions,
1588 such as:
1590 lui $at,%highest(symbol)
1591 daddiu $at,$at,%higher(symbol)
1592 dsll $at,$at,16
1593 daddiu $at,$at,%hi(symbol)
1594 dsll $at,$at,16
1596 The final address is then $at + %lo(symbol). With 32-bit
1597 symbols we just need a preparatory lui. */
1598 return (ABI_HAS_64BIT_SYMBOLS ? 6 : 2);
1600 case SYMBOL_SMALL_DATA:
1601 return 1;
1603 case SYMBOL_CONSTANT_POOL:
1604 /* This case is for mips16 only. Assume we'll need an
1605 extended instruction. */
1606 return 2;
1608 case SYMBOL_GOT_LOCAL:
1609 case SYMBOL_GOT_GLOBAL:
1610 /* Unless -funit-at-a-time is in effect, we can't be sure whether
1611 the local/global classification is accurate. See override_options
1612 for details.
1614 The worst cases are:
1616 (1) For local symbols when generating o32 or o64 code. The assembler
1617 will use:
1619 lw $at,%got(symbol)
1622 ...and the final address will be $at + %lo(symbol).
1624 (2) For global symbols when -mxgot. The assembler will use:
1626 lui $at,%got_hi(symbol)
1627 (d)addu $at,$at,$gp
1629 ...and the final address will be $at + %got_lo(symbol). */
1630 return 3;
1632 case SYMBOL_GOTOFF_PAGE:
1633 case SYMBOL_GOTOFF_GLOBAL:
1634 case SYMBOL_GOTOFF_CALL:
1635 case SYMBOL_GOTOFF_LOADGP:
1636 case SYMBOL_64_HIGH:
1637 case SYMBOL_64_MID:
1638 case SYMBOL_64_LOW:
1639 case SYMBOL_TLSGD:
1640 case SYMBOL_TLSLDM:
1641 case SYMBOL_DTPREL:
1642 case SYMBOL_GOTTPREL:
1643 case SYMBOL_TPREL:
1644 /* Check whether the offset is a 16- or 32-bit value. */
1645 return mips_split_p[type] ? 2 : 1;
1647 case SYMBOL_TLS:
1648 /* We don't treat a bare TLS symbol as a constant. */
1649 return 0;
1651 gcc_unreachable ();
1654 /* Return true if X is a legitimate $sp-based address for mode MDOE. */
1656 bool
1657 mips_stack_address_p (rtx x, enum machine_mode mode)
1659 struct mips_address_info addr;
1661 return (mips_classify_address (&addr, x, mode, false)
1662 && addr.type == ADDRESS_REG
1663 && addr.reg == stack_pointer_rtx);
1666 /* Return true if a value at OFFSET bytes from BASE can be accessed
1667 using an unextended mips16 instruction. MODE is the mode of the
1668 value.
1670 Usually the offset in an unextended instruction is a 5-bit field.
1671 The offset is unsigned and shifted left once for HIs, twice
1672 for SIs, and so on. An exception is SImode accesses off the
1673 stack pointer, which have an 8-bit immediate field. */
1675 static bool
1676 mips16_unextended_reference_p (enum machine_mode mode, rtx base, rtx offset)
1678 if (TARGET_MIPS16
1679 && GET_CODE (offset) == CONST_INT
1680 && INTVAL (offset) >= 0
1681 && (INTVAL (offset) & (GET_MODE_SIZE (mode) - 1)) == 0)
1683 if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
1684 return INTVAL (offset) < 256 * GET_MODE_SIZE (mode);
1685 return INTVAL (offset) < 32 * GET_MODE_SIZE (mode);
1687 return false;
1691 /* Return the number of instructions needed to load or store a value
1692 of mode MODE at X. Return 0 if X isn't valid for MODE.
1694 For mips16 code, count extended instructions as two instructions. */
1697 mips_address_insns (rtx x, enum machine_mode mode)
1699 struct mips_address_info addr;
1700 int factor;
1702 if (mode == BLKmode)
1703 /* BLKmode is used for single unaligned loads and stores. */
1704 factor = 1;
1705 else
1706 /* Each word of a multi-word value will be accessed individually. */
1707 factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1709 if (mips_classify_address (&addr, x, mode, false))
1710 switch (addr.type)
1712 case ADDRESS_REG:
1713 if (TARGET_MIPS16
1714 && !mips16_unextended_reference_p (mode, addr.reg, addr.offset))
1715 return factor * 2;
1716 return factor;
1718 case ADDRESS_LO_SUM:
1719 return (TARGET_MIPS16 ? factor * 2 : factor);
1721 case ADDRESS_CONST_INT:
1722 return factor;
1724 case ADDRESS_SYMBOLIC:
1725 return factor * mips_symbol_insns (addr.symbol_type);
1727 return 0;
1731 /* Likewise for constant X. */
1734 mips_const_insns (rtx x)
1736 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
1737 enum mips_symbol_type symbol_type;
1738 HOST_WIDE_INT offset;
1740 switch (GET_CODE (x))
1742 case HIGH:
1743 if (TARGET_MIPS16
1744 || !mips_symbolic_constant_p (XEXP (x, 0), &symbol_type)
1745 || !mips_split_p[symbol_type])
1746 return 0;
1748 return 1;
1750 case CONST_INT:
1751 if (TARGET_MIPS16)
1752 /* Unsigned 8-bit constants can be loaded using an unextended
1753 LI instruction. Unsigned 16-bit constants can be loaded
1754 using an extended LI. Negative constants must be loaded
1755 using LI and then negated. */
1756 return (INTVAL (x) >= 0 && INTVAL (x) < 256 ? 1
1757 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
1758 : INTVAL (x) > -256 && INTVAL (x) < 0 ? 2
1759 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
1760 : 0);
1762 return mips_build_integer (codes, INTVAL (x));
1764 case CONST_DOUBLE:
1765 case CONST_VECTOR:
1766 return (!TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0);
1768 case CONST:
1769 if (CONST_GP_P (x))
1770 return 1;
1772 /* See if we can refer to X directly. */
1773 if (mips_symbolic_constant_p (x, &symbol_type))
1774 return mips_symbol_insns (symbol_type);
1776 /* Otherwise try splitting the constant into a base and offset.
1777 16-bit offsets can be added using an extra addiu. Larger offsets
1778 must be calculated separately and then added to the base. */
1779 mips_split_const (x, &x, &offset);
1780 if (offset != 0)
1782 int n = mips_const_insns (x);
1783 if (n != 0)
1785 if (SMALL_OPERAND (offset))
1786 return n + 1;
1787 else
1788 return n + 1 + mips_build_integer (codes, offset);
1791 return 0;
1793 case SYMBOL_REF:
1794 case LABEL_REF:
1795 return mips_symbol_insns (mips_classify_symbol (x));
1797 default:
1798 return 0;
1803 /* Return the number of instructions needed for memory reference X.
1804 Count extended mips16 instructions as two instructions. */
1807 mips_fetch_insns (rtx x)
1809 gcc_assert (MEM_P (x));
1810 return mips_address_insns (XEXP (x, 0), GET_MODE (x));
1814 /* Return the number of instructions needed for an integer division. */
1817 mips_idiv_insns (void)
1819 int count;
1821 count = 1;
1822 if (TARGET_CHECK_ZERO_DIV)
1824 if (GENERATE_DIVIDE_TRAPS)
1825 count++;
1826 else
1827 count += 2;
1830 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
1831 count++;
1832 return count;
1835 /* This function is used to implement GO_IF_LEGITIMATE_ADDRESS. It
1836 returns a nonzero value if X is a legitimate address for a memory
1837 operand of the indicated MODE. STRICT is nonzero if this function
1838 is called during reload. */
1840 bool
1841 mips_legitimate_address_p (enum machine_mode mode, rtx x, int strict)
1843 struct mips_address_info addr;
1845 return mips_classify_address (&addr, x, mode, strict);
1849 /* Copy VALUE to a register and return that register. If new psuedos
1850 are allowed, copy it into a new register, otherwise use DEST. */
1852 static rtx
1853 mips_force_temporary (rtx dest, rtx value)
1855 if (!no_new_pseudos)
1856 return force_reg (Pmode, value);
1857 else
1859 emit_move_insn (copy_rtx (dest), value);
1860 return dest;
1865 /* Return a LO_SUM expression for ADDR. TEMP is as for mips_force_temporary
1866 and is used to load the high part into a register. */
1868 static rtx
1869 mips_split_symbol (rtx temp, rtx addr)
1871 rtx high;
1873 if (TARGET_MIPS16)
1874 high = mips16_gp_pseudo_reg ();
1875 else
1876 high = mips_force_temporary (temp, gen_rtx_HIGH (Pmode, copy_rtx (addr)));
1877 return gen_rtx_LO_SUM (Pmode, high, addr);
1881 /* Return an UNSPEC address with underlying address ADDRESS and symbol
1882 type SYMBOL_TYPE. */
1885 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
1887 rtx base;
1888 HOST_WIDE_INT offset;
1890 mips_split_const (address, &base, &offset);
1891 base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
1892 UNSPEC_ADDRESS_FIRST + symbol_type);
1893 return plus_constant (gen_rtx_CONST (Pmode, base), offset);
1897 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
1898 high part to BASE and return the result. Just return BASE otherwise.
1899 TEMP is available as a temporary register if needed.
1901 The returned expression can be used as the first operand to a LO_SUM. */
1903 static rtx
1904 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
1905 enum mips_symbol_type symbol_type)
1907 if (mips_split_p[symbol_type])
1909 addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
1910 addr = mips_force_temporary (temp, addr);
1911 return mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
1913 return base;
1917 /* Return a legitimate address for REG + OFFSET. TEMP is as for
1918 mips_force_temporary; it is only needed when OFFSET is not a
1919 SMALL_OPERAND. */
1921 static rtx
1922 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
1924 if (!SMALL_OPERAND (offset))
1926 rtx high;
1927 if (TARGET_MIPS16)
1929 /* Load the full offset into a register so that we can use
1930 an unextended instruction for the address itself. */
1931 high = GEN_INT (offset);
1932 offset = 0;
1934 else
1936 /* Leave OFFSET as a 16-bit offset and put the excess in HIGH. */
1937 high = GEN_INT (CONST_HIGH_PART (offset));
1938 offset = CONST_LOW_PART (offset);
1940 high = mips_force_temporary (temp, high);
1941 reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
1943 return plus_constant (reg, offset);
1946 /* Emit a call to __tls_get_addr. SYM is the TLS symbol we are
1947 referencing, and TYPE is the symbol type to use (either global
1948 dynamic or local dynamic). V0 is an RTX for the return value
1949 location. The entire insn sequence is returned. */
1951 static GTY(()) rtx mips_tls_symbol;
1953 static rtx
1954 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
1956 rtx insn, loc, tga, a0;
1958 a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
1960 if (!mips_tls_symbol)
1961 mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
1963 loc = mips_unspec_address (sym, type);
1965 start_sequence ();
1967 emit_insn (gen_rtx_SET (Pmode, a0,
1968 gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
1969 tga = gen_rtx_MEM (Pmode, mips_tls_symbol);
1970 insn = emit_call_insn (gen_call_value (v0, tga, const0_rtx, const0_rtx));
1971 CONST_OR_PURE_CALL_P (insn) = 1;
1972 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), v0);
1973 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
1974 insn = get_insns ();
1976 end_sequence ();
1978 return insn;
1981 /* Generate the code to access LOC, a thread local SYMBOL_REF. The
1982 return value will be a valid address and move_operand (either a REG
1983 or a LO_SUM). */
1985 static rtx
1986 mips_legitimize_tls_address (rtx loc)
1988 rtx dest, insn, v0, v1, tmp1, tmp2, eqv;
1989 enum tls_model model;
1991 v0 = gen_rtx_REG (Pmode, GP_RETURN);
1992 v1 = gen_rtx_REG (Pmode, GP_RETURN + 1);
1994 model = SYMBOL_REF_TLS_MODEL (loc);
1996 switch (model)
1998 case TLS_MODEL_GLOBAL_DYNAMIC:
1999 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
2000 dest = gen_reg_rtx (Pmode);
2001 emit_libcall_block (insn, dest, v0, loc);
2002 break;
2004 case TLS_MODEL_LOCAL_DYNAMIC:
2005 insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
2006 tmp1 = gen_reg_rtx (Pmode);
2008 /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2009 share the LDM result with other LD model accesses. */
2010 eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2011 UNSPEC_TLS_LDM);
2012 emit_libcall_block (insn, tmp1, v0, eqv);
2014 tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
2015 dest = gen_rtx_LO_SUM (Pmode, tmp2,
2016 mips_unspec_address (loc, SYMBOL_DTPREL));
2017 break;
2019 case TLS_MODEL_INITIAL_EXEC:
2020 tmp1 = gen_reg_rtx (Pmode);
2021 tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
2022 if (Pmode == DImode)
2024 emit_insn (gen_tls_get_tp_di (v1));
2025 emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
2027 else
2029 emit_insn (gen_tls_get_tp_si (v1));
2030 emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
2032 dest = gen_reg_rtx (Pmode);
2033 emit_insn (gen_add3_insn (dest, tmp1, v1));
2034 break;
2036 case TLS_MODEL_LOCAL_EXEC:
2038 if (Pmode == DImode)
2039 emit_insn (gen_tls_get_tp_di (v1));
2040 else
2041 emit_insn (gen_tls_get_tp_si (v1));
2043 tmp1 = mips_unspec_offset_high (NULL, v1, loc, SYMBOL_TPREL);
2044 dest = gen_rtx_LO_SUM (Pmode, tmp1,
2045 mips_unspec_address (loc, SYMBOL_TPREL));
2046 break;
2048 default:
2049 gcc_unreachable ();
2052 return dest;
2055 /* This function is used to implement LEGITIMIZE_ADDRESS. If *XLOC can
2056 be legitimized in a way that the generic machinery might not expect,
2057 put the new address in *XLOC and return true. MODE is the mode of
2058 the memory being accessed. */
2060 bool
2061 mips_legitimize_address (rtx *xloc, enum machine_mode mode)
2063 enum mips_symbol_type symbol_type;
2065 if (mips_tls_operand_p (*xloc))
2067 *xloc = mips_legitimize_tls_address (*xloc);
2068 return true;
2071 /* See if the address can split into a high part and a LO_SUM. */
2072 if (mips_symbolic_constant_p (*xloc, &symbol_type)
2073 && mips_symbolic_address_p (symbol_type, mode)
2074 && mips_split_p[symbol_type])
2076 *xloc = mips_split_symbol (0, *xloc);
2077 return true;
2080 if (GET_CODE (*xloc) == PLUS && GET_CODE (XEXP (*xloc, 1)) == CONST_INT)
2082 /* Handle REG + CONSTANT using mips_add_offset. */
2083 rtx reg;
2085 reg = XEXP (*xloc, 0);
2086 if (!mips_valid_base_register_p (reg, mode, 0))
2087 reg = copy_to_mode_reg (Pmode, reg);
2088 *xloc = mips_add_offset (0, reg, INTVAL (XEXP (*xloc, 1)));
2089 return true;
2092 return false;
2096 /* Subroutine of mips_build_integer (with the same interface).
2097 Assume that the final action in the sequence should be a left shift. */
2099 static unsigned int
2100 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
2102 unsigned int i, shift;
2104 /* Shift VALUE right until its lowest bit is set. Shift arithmetically
2105 since signed numbers are easier to load than unsigned ones. */
2106 shift = 0;
2107 while ((value & 1) == 0)
2108 value /= 2, shift++;
2110 i = mips_build_integer (codes, value);
2111 codes[i].code = ASHIFT;
2112 codes[i].value = shift;
2113 return i + 1;
2117 /* As for mips_build_shift, but assume that the final action will be
2118 an IOR or PLUS operation. */
2120 static unsigned int
2121 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
2123 unsigned HOST_WIDE_INT high;
2124 unsigned int i;
2126 high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
2127 if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
2129 /* The constant is too complex to load with a simple lui/ori pair
2130 so our goal is to clear as many trailing zeros as possible.
2131 In this case, we know bit 16 is set and that the low 16 bits
2132 form a negative number. If we subtract that number from VALUE,
2133 we will clear at least the lowest 17 bits, maybe more. */
2134 i = mips_build_integer (codes, CONST_HIGH_PART (value));
2135 codes[i].code = PLUS;
2136 codes[i].value = CONST_LOW_PART (value);
2138 else
2140 i = mips_build_integer (codes, high);
2141 codes[i].code = IOR;
2142 codes[i].value = value & 0xffff;
2144 return i + 1;
2148 /* Fill CODES with a sequence of rtl operations to load VALUE.
2149 Return the number of operations needed. */
2151 static unsigned int
2152 mips_build_integer (struct mips_integer_op *codes,
2153 unsigned HOST_WIDE_INT value)
2155 if (SMALL_OPERAND (value)
2156 || SMALL_OPERAND_UNSIGNED (value)
2157 || LUI_OPERAND (value))
2159 /* The value can be loaded with a single instruction. */
2160 codes[0].code = UNKNOWN;
2161 codes[0].value = value;
2162 return 1;
2164 else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
2166 /* Either the constant is a simple LUI/ORI combination or its
2167 lowest bit is set. We don't want to shift in this case. */
2168 return mips_build_lower (codes, value);
2170 else if ((value & 0xffff) == 0)
2172 /* The constant will need at least three actions. The lowest
2173 16 bits are clear, so the final action will be a shift. */
2174 return mips_build_shift (codes, value);
2176 else
2178 /* The final action could be a shift, add or inclusive OR.
2179 Rather than use a complex condition to select the best
2180 approach, try both mips_build_shift and mips_build_lower
2181 and pick the one that gives the shortest sequence.
2182 Note that this case is only used once per constant. */
2183 struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
2184 unsigned int cost, alt_cost;
2186 cost = mips_build_shift (codes, value);
2187 alt_cost = mips_build_lower (alt_codes, value);
2188 if (alt_cost < cost)
2190 memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
2191 cost = alt_cost;
2193 return cost;
2198 /* Move VALUE into register DEST. */
2200 static void
2201 mips_move_integer (rtx dest, unsigned HOST_WIDE_INT value)
2203 struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2204 enum machine_mode mode;
2205 unsigned int i, cost;
2206 rtx x;
2208 mode = GET_MODE (dest);
2209 cost = mips_build_integer (codes, value);
2211 /* Apply each binary operation to X. Invariant: X is a legitimate
2212 source operand for a SET pattern. */
2213 x = GEN_INT (codes[0].value);
2214 for (i = 1; i < cost; i++)
2216 if (no_new_pseudos)
2217 emit_move_insn (dest, x), x = dest;
2218 else
2219 x = force_reg (mode, x);
2220 x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
2223 emit_insn (gen_rtx_SET (VOIDmode, dest, x));
2227 /* Subroutine of mips_legitimize_move. Move constant SRC into register
2228 DEST given that SRC satisfies immediate_operand but doesn't satisfy
2229 move_operand. */
2231 static void
2232 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
2234 rtx base;
2235 HOST_WIDE_INT offset;
2236 enum mips_symbol_type symbol_type;
2238 /* Split moves of big integers into smaller pieces. In mips16 code,
2239 it's better to force the constant into memory instead. */
2240 if (GET_CODE (src) == CONST_INT && !TARGET_MIPS16)
2242 mips_move_integer (dest, INTVAL (src));
2243 return;
2246 if (mips_tls_operand_p (src))
2248 emit_move_insn (dest, mips_legitimize_tls_address (src));
2249 return;
2252 /* See if the symbol can be split. For mips16, this is often worse than
2253 forcing it in the constant pool since it needs the single-register form
2254 of addiu or daddiu. */
2255 if (!TARGET_MIPS16
2256 && mips_symbolic_constant_p (src, &symbol_type)
2257 && mips_split_p[symbol_type])
2259 emit_move_insn (dest, mips_split_symbol (dest, src));
2260 return;
2263 /* If we have (const (plus symbol offset)), load the symbol first
2264 and then add in the offset. This is usually better than forcing
2265 the constant into memory, at least in non-mips16 code. */
2266 mips_split_const (src, &base, &offset);
2267 if (!TARGET_MIPS16
2268 && offset != 0
2269 && (!no_new_pseudos || SMALL_OPERAND (offset)))
2271 base = mips_force_temporary (dest, base);
2272 emit_move_insn (dest, mips_add_offset (0, base, offset));
2273 return;
2276 src = force_const_mem (mode, src);
2278 /* When using explicit relocs, constant pool references are sometimes
2279 not legitimate addresses. */
2280 if (!memory_operand (src, VOIDmode))
2281 src = replace_equiv_address (src, mips_split_symbol (dest, XEXP (src, 0)));
2282 emit_move_insn (dest, src);
2286 /* If (set DEST SRC) is not a valid instruction, emit an equivalent
2287 sequence that is valid. */
2289 bool
2290 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
2292 if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
2294 emit_move_insn (dest, force_reg (mode, src));
2295 return true;
2298 /* Check for individual, fully-reloaded mflo and mfhi instructions. */
2299 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
2300 && REG_P (src) && MD_REG_P (REGNO (src))
2301 && REG_P (dest) && GP_REG_P (REGNO (dest)))
2303 int other_regno = REGNO (src) == HI_REGNUM ? LO_REGNUM : HI_REGNUM;
2304 if (GET_MODE_SIZE (mode) <= 4)
2305 emit_insn (gen_mfhilo_si (gen_rtx_REG (SImode, REGNO (dest)),
2306 gen_rtx_REG (SImode, REGNO (src)),
2307 gen_rtx_REG (SImode, other_regno)));
2308 else
2309 emit_insn (gen_mfhilo_di (gen_rtx_REG (DImode, REGNO (dest)),
2310 gen_rtx_REG (DImode, REGNO (src)),
2311 gen_rtx_REG (DImode, other_regno)));
2312 return true;
2315 /* We need to deal with constants that would be legitimate
2316 immediate_operands but not legitimate move_operands. */
2317 if (CONSTANT_P (src) && !move_operand (src, mode))
2319 mips_legitimize_const_move (mode, dest, src);
2320 set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
2321 return true;
2323 return false;
2326 /* We need a lot of little routines to check constant values on the
2327 mips16. These are used to figure out how long the instruction will
2328 be. It would be much better to do this using constraints, but
2329 there aren't nearly enough letters available. */
2331 static int
2332 m16_check_op (rtx op, int low, int high, int mask)
2334 return (GET_CODE (op) == CONST_INT
2335 && INTVAL (op) >= low
2336 && INTVAL (op) <= high
2337 && (INTVAL (op) & mask) == 0);
2341 m16_uimm3_b (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2343 return m16_check_op (op, 0x1, 0x8, 0);
2347 m16_simm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2349 return m16_check_op (op, - 0x8, 0x7, 0);
2353 m16_nsimm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2355 return m16_check_op (op, - 0x7, 0x8, 0);
2359 m16_simm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2361 return m16_check_op (op, - 0x10, 0xf, 0);
2365 m16_nsimm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2367 return m16_check_op (op, - 0xf, 0x10, 0);
2371 m16_uimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2373 return m16_check_op (op, (- 0x10) << 2, 0xf << 2, 3);
2377 m16_nuimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2379 return m16_check_op (op, (- 0xf) << 2, 0x10 << 2, 3);
2383 m16_simm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2385 return m16_check_op (op, - 0x80, 0x7f, 0);
2389 m16_nsimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2391 return m16_check_op (op, - 0x7f, 0x80, 0);
2395 m16_uimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2397 return m16_check_op (op, 0x0, 0xff, 0);
2401 m16_nuimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2403 return m16_check_op (op, - 0xff, 0x0, 0);
2407 m16_uimm8_m1_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2409 return m16_check_op (op, - 0x1, 0xfe, 0);
2413 m16_uimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2415 return m16_check_op (op, 0x0, 0xff << 2, 3);
2419 m16_nuimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2421 return m16_check_op (op, (- 0xff) << 2, 0x0, 3);
2425 m16_simm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2427 return m16_check_op (op, (- 0x80) << 3, 0x7f << 3, 7);
2431 m16_nsimm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2433 return m16_check_op (op, (- 0x7f) << 3, 0x80 << 3, 7);
2436 static bool
2437 mips_rtx_costs (rtx x, int code, int outer_code, int *total)
2439 enum machine_mode mode = GET_MODE (x);
2440 bool float_mode_p = FLOAT_MODE_P (mode);
2442 switch (code)
2444 case CONST_INT:
2445 if (TARGET_MIPS16)
2447 /* A number between 1 and 8 inclusive is efficient for a shift.
2448 Otherwise, we will need an extended instruction. */
2449 if ((outer_code) == ASHIFT || (outer_code) == ASHIFTRT
2450 || (outer_code) == LSHIFTRT)
2452 if (INTVAL (x) >= 1 && INTVAL (x) <= 8)
2453 *total = 0;
2454 else
2455 *total = COSTS_N_INSNS (1);
2456 return true;
2459 /* We can use cmpi for an xor with an unsigned 16 bit value. */
2460 if ((outer_code) == XOR
2461 && INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
2463 *total = 0;
2464 return true;
2467 /* We may be able to use slt or sltu for a comparison with a
2468 signed 16 bit value. (The boundary conditions aren't quite
2469 right, but this is just a heuristic anyhow.) */
2470 if (((outer_code) == LT || (outer_code) == LE
2471 || (outer_code) == GE || (outer_code) == GT
2472 || (outer_code) == LTU || (outer_code) == LEU
2473 || (outer_code) == GEU || (outer_code) == GTU)
2474 && INTVAL (x) >= -0x8000 && INTVAL (x) < 0x8000)
2476 *total = 0;
2477 return true;
2480 /* Equality comparisons with 0 are cheap. */
2481 if (((outer_code) == EQ || (outer_code) == NE)
2482 && INTVAL (x) == 0)
2484 *total = 0;
2485 return true;
2488 /* Constants in the range 0...255 can be loaded with an unextended
2489 instruction. They are therefore as cheap as a register move.
2491 Given the choice between "li R1,0...255" and "move R1,R2"
2492 (where R2 is a known constant), it is usually better to use "li",
2493 since we do not want to unnecessarily extend the lifetime
2494 of R2. */
2495 if (outer_code == SET
2496 && INTVAL (x) >= 0
2497 && INTVAL (x) < 256)
2499 *total = 0;
2500 return true;
2503 else
2505 /* These can be used anywhere. */
2506 *total = 0;
2507 return true;
2510 /* Otherwise fall through to the handling below because
2511 we'll need to construct the constant. */
2513 case CONST:
2514 case SYMBOL_REF:
2515 case LABEL_REF:
2516 case CONST_DOUBLE:
2517 if (LEGITIMATE_CONSTANT_P (x))
2519 *total = COSTS_N_INSNS (1);
2520 return true;
2522 else
2524 /* The value will need to be fetched from the constant pool. */
2525 *total = CONSTANT_POOL_COST;
2526 return true;
2529 case MEM:
2531 /* If the address is legitimate, return the number of
2532 instructions it needs, otherwise use the default handling. */
2533 int n = mips_address_insns (XEXP (x, 0), GET_MODE (x));
2534 if (n > 0)
2536 *total = COSTS_N_INSNS (n + 1);
2537 return true;
2539 return false;
2542 case FFS:
2543 *total = COSTS_N_INSNS (6);
2544 return true;
2546 case NOT:
2547 *total = COSTS_N_INSNS ((mode == DImode && !TARGET_64BIT) ? 2 : 1);
2548 return true;
2550 case AND:
2551 case IOR:
2552 case XOR:
2553 if (mode == DImode && !TARGET_64BIT)
2555 *total = COSTS_N_INSNS (2);
2556 return true;
2558 return false;
2560 case ASHIFT:
2561 case ASHIFTRT:
2562 case LSHIFTRT:
2563 if (mode == DImode && !TARGET_64BIT)
2565 *total = COSTS_N_INSNS ((GET_CODE (XEXP (x, 1)) == CONST_INT)
2566 ? 4 : 12);
2567 return true;
2569 return false;
2571 case ABS:
2572 if (float_mode_p)
2573 *total = COSTS_N_INSNS (1);
2574 else
2575 *total = COSTS_N_INSNS (4);
2576 return true;
2578 case LO_SUM:
2579 *total = COSTS_N_INSNS (1);
2580 return true;
2582 case PLUS:
2583 case MINUS:
2584 if (float_mode_p)
2586 *total = mips_cost->fp_add;
2587 return true;
2590 else if (mode == DImode && !TARGET_64BIT)
2592 *total = COSTS_N_INSNS (4);
2593 return true;
2595 return false;
2597 case NEG:
2598 if (mode == DImode && !TARGET_64BIT)
2600 *total = COSTS_N_INSNS (4);
2601 return true;
2603 return false;
2605 case MULT:
2606 if (mode == SFmode)
2607 *total = mips_cost->fp_mult_sf;
2609 else if (mode == DFmode)
2610 *total = mips_cost->fp_mult_df;
2612 else if (mode == SImode)
2613 *total = mips_cost->int_mult_si;
2615 else
2616 *total = mips_cost->int_mult_di;
2618 return true;
2620 case DIV:
2621 case MOD:
2622 if (float_mode_p)
2624 if (mode == SFmode)
2625 *total = mips_cost->fp_div_sf;
2626 else
2627 *total = mips_cost->fp_div_df;
2629 return true;
2631 /* Fall through. */
2633 case UDIV:
2634 case UMOD:
2635 if (mode == DImode)
2636 *total = mips_cost->int_div_di;
2637 else
2638 *total = mips_cost->int_div_si;
2640 return true;
2642 case SIGN_EXTEND:
2643 /* A sign extend from SImode to DImode in 64 bit mode is often
2644 zero instructions, because the result can often be used
2645 directly by another instruction; we'll call it one. */
2646 if (TARGET_64BIT && mode == DImode
2647 && GET_MODE (XEXP (x, 0)) == SImode)
2648 *total = COSTS_N_INSNS (1);
2649 else
2650 *total = COSTS_N_INSNS (2);
2651 return true;
2653 case ZERO_EXTEND:
2654 if (TARGET_64BIT && mode == DImode
2655 && GET_MODE (XEXP (x, 0)) == SImode)
2656 *total = COSTS_N_INSNS (2);
2657 else
2658 *total = COSTS_N_INSNS (1);
2659 return true;
2661 case FLOAT:
2662 case UNSIGNED_FLOAT:
2663 case FIX:
2664 case FLOAT_EXTEND:
2665 case FLOAT_TRUNCATE:
2666 case SQRT:
2667 *total = mips_cost->fp_add;
2668 return true;
2670 default:
2671 return false;
2675 /* Provide the costs of an addressing mode that contains ADDR.
2676 If ADDR is not a valid address, its cost is irrelevant. */
2678 static int
2679 mips_address_cost (rtx addr)
2681 return mips_address_insns (addr, SImode);
2684 /* Return one word of double-word value OP, taking into account the fixed
2685 endianness of certain registers. HIGH_P is true to select the high part,
2686 false to select the low part. */
2689 mips_subword (rtx op, int high_p)
2691 unsigned int byte;
2692 enum machine_mode mode;
2694 mode = GET_MODE (op);
2695 if (mode == VOIDmode)
2696 mode = DImode;
2698 if (TARGET_BIG_ENDIAN ? !high_p : high_p)
2699 byte = UNITS_PER_WORD;
2700 else
2701 byte = 0;
2703 if (REG_P (op))
2705 if (FP_REG_P (REGNO (op)))
2706 return gen_rtx_REG (word_mode, high_p ? REGNO (op) + 1 : REGNO (op));
2707 if (ACC_HI_REG_P (REGNO (op)))
2708 return gen_rtx_REG (word_mode, high_p ? REGNO (op) : REGNO (op) + 1);
2711 if (MEM_P (op))
2712 return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
2714 return simplify_gen_subreg (word_mode, op, mode, byte);
2718 /* Return true if a 64-bit move from SRC to DEST should be split into two. */
2720 bool
2721 mips_split_64bit_move_p (rtx dest, rtx src)
2723 if (TARGET_64BIT)
2724 return false;
2726 /* FP->FP moves can be done in a single instruction. */
2727 if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
2728 return false;
2730 /* Check for floating-point loads and stores. They can be done using
2731 ldc1 and sdc1 on MIPS II and above. */
2732 if (mips_isa > 1)
2734 if (FP_REG_RTX_P (dest) && MEM_P (src))
2735 return false;
2736 if (FP_REG_RTX_P (src) && MEM_P (dest))
2737 return false;
2739 return true;
2743 /* Split a 64-bit move from SRC to DEST assuming that
2744 mips_split_64bit_move_p holds.
2746 Moves into and out of FPRs cause some difficulty here. Such moves
2747 will always be DFmode, since paired FPRs are not allowed to store
2748 DImode values. The most natural representation would be two separate
2749 32-bit moves, such as:
2751 (set (reg:SI $f0) (mem:SI ...))
2752 (set (reg:SI $f1) (mem:SI ...))
2754 However, the second insn is invalid because odd-numbered FPRs are
2755 not allowed to store independent values. Use the patterns load_df_low,
2756 load_df_high and store_df_high instead. */
2758 void
2759 mips_split_64bit_move (rtx dest, rtx src)
2761 if (FP_REG_RTX_P (dest))
2763 /* Loading an FPR from memory or from GPRs. */
2764 emit_insn (gen_load_df_low (copy_rtx (dest), mips_subword (src, 0)));
2765 emit_insn (gen_load_df_high (dest, mips_subword (src, 1),
2766 copy_rtx (dest)));
2768 else if (FP_REG_RTX_P (src))
2770 /* Storing an FPR into memory or GPRs. */
2771 emit_move_insn (mips_subword (dest, 0), mips_subword (src, 0));
2772 emit_insn (gen_store_df_high (mips_subword (dest, 1), src));
2774 else
2776 /* The operation can be split into two normal moves. Decide in
2777 which order to do them. */
2778 rtx low_dest;
2780 low_dest = mips_subword (dest, 0);
2781 if (REG_P (low_dest)
2782 && reg_overlap_mentioned_p (low_dest, src))
2784 emit_move_insn (mips_subword (dest, 1), mips_subword (src, 1));
2785 emit_move_insn (low_dest, mips_subword (src, 0));
2787 else
2789 emit_move_insn (low_dest, mips_subword (src, 0));
2790 emit_move_insn (mips_subword (dest, 1), mips_subword (src, 1));
2795 /* Return the appropriate instructions to move SRC into DEST. Assume
2796 that SRC is operand 1 and DEST is operand 0. */
2798 const char *
2799 mips_output_move (rtx dest, rtx src)
2801 enum rtx_code dest_code, src_code;
2802 bool dbl_p;
2804 dest_code = GET_CODE (dest);
2805 src_code = GET_CODE (src);
2806 dbl_p = (GET_MODE_SIZE (GET_MODE (dest)) == 8);
2808 if (dbl_p && mips_split_64bit_move_p (dest, src))
2809 return "#";
2811 if ((src_code == REG && GP_REG_P (REGNO (src)))
2812 || (!TARGET_MIPS16 && src == CONST0_RTX (GET_MODE (dest))))
2814 if (dest_code == REG)
2816 if (GP_REG_P (REGNO (dest)))
2817 return "move\t%0,%z1";
2819 if (MD_REG_P (REGNO (dest)))
2820 return "mt%0\t%z1";
2822 if (DSP_ACC_REG_P (REGNO (dest)))
2824 static char retval[] = "mt__\t%z1,%q0";
2825 retval[2] = reg_names[REGNO (dest)][4];
2826 retval[3] = reg_names[REGNO (dest)][5];
2827 return retval;
2830 if (FP_REG_P (REGNO (dest)))
2831 return (dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0");
2833 if (ALL_COP_REG_P (REGNO (dest)))
2835 static char retval[] = "dmtc_\t%z1,%0";
2837 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
2838 return (dbl_p ? retval : retval + 1);
2841 if (dest_code == MEM)
2842 return (dbl_p ? "sd\t%z1,%0" : "sw\t%z1,%0");
2844 if (dest_code == REG && GP_REG_P (REGNO (dest)))
2846 if (src_code == REG)
2848 if (DSP_ACC_REG_P (REGNO (src)))
2850 static char retval[] = "mf__\t%0,%q1";
2851 retval[2] = reg_names[REGNO (src)][4];
2852 retval[3] = reg_names[REGNO (src)][5];
2853 return retval;
2856 if (ST_REG_P (REGNO (src)) && ISA_HAS_8CC)
2857 return "lui\t%0,0x3f80\n\tmovf\t%0,%.,%1";
2859 if (FP_REG_P (REGNO (src)))
2860 return (dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1");
2862 if (ALL_COP_REG_P (REGNO (src)))
2864 static char retval[] = "dmfc_\t%0,%1";
2866 retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
2867 return (dbl_p ? retval : retval + 1);
2871 if (src_code == MEM)
2872 return (dbl_p ? "ld\t%0,%1" : "lw\t%0,%1");
2874 if (src_code == CONST_INT)
2876 /* Don't use the X format, because that will give out of
2877 range numbers for 64 bit hosts and 32 bit targets. */
2878 if (!TARGET_MIPS16)
2879 return "li\t%0,%1\t\t\t# %X1";
2881 if (INTVAL (src) >= 0 && INTVAL (src) <= 0xffff)
2882 return "li\t%0,%1";
2884 if (INTVAL (src) < 0 && INTVAL (src) >= -0xffff)
2885 return "#";
2888 if (src_code == HIGH)
2889 return "lui\t%0,%h1";
2891 if (CONST_GP_P (src))
2892 return "move\t%0,%1";
2894 if (symbolic_operand (src, VOIDmode))
2895 return (dbl_p ? "dla\t%0,%1" : "la\t%0,%1");
2897 if (src_code == REG && FP_REG_P (REGNO (src)))
2899 if (dest_code == REG && FP_REG_P (REGNO (dest)))
2901 if (GET_MODE (dest) == V2SFmode)
2902 return "mov.ps\t%0,%1";
2903 else
2904 return (dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1");
2907 if (dest_code == MEM)
2908 return (dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0");
2910 if (dest_code == REG && FP_REG_P (REGNO (dest)))
2912 if (src_code == MEM)
2913 return (dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1");
2915 if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
2917 static char retval[] = "l_c_\t%0,%1";
2919 retval[1] = (dbl_p ? 'd' : 'w');
2920 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
2921 return retval;
2923 if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
2925 static char retval[] = "s_c_\t%1,%0";
2927 retval[1] = (dbl_p ? 'd' : 'w');
2928 retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
2929 return retval;
2931 gcc_unreachable ();
2934 /* Restore $gp from its save slot. Valid only when using o32 or
2935 o64 abicalls. */
2937 void
2938 mips_restore_gp (void)
2940 rtx address, slot;
2942 gcc_assert (TARGET_ABICALLS && TARGET_OLDABI);
2944 address = mips_add_offset (pic_offset_table_rtx,
2945 frame_pointer_needed
2946 ? hard_frame_pointer_rtx
2947 : stack_pointer_rtx,
2948 current_function_outgoing_args_size);
2949 slot = gen_rtx_MEM (Pmode, address);
2951 emit_move_insn (pic_offset_table_rtx, slot);
2952 if (!TARGET_EXPLICIT_RELOCS)
2953 emit_insn (gen_blockage ());
2956 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)). */
2958 static void
2959 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
2961 emit_insn (gen_rtx_SET (VOIDmode, target,
2962 gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
2965 /* Return true if CMP1 is a suitable second operand for relational
2966 operator CODE. See also the *sCC patterns in mips.md. */
2968 static bool
2969 mips_relational_operand_ok_p (enum rtx_code code, rtx cmp1)
2971 switch (code)
2973 case GT:
2974 case GTU:
2975 return reg_or_0_operand (cmp1, VOIDmode);
2977 case GE:
2978 case GEU:
2979 return !TARGET_MIPS16 && cmp1 == const1_rtx;
2981 case LT:
2982 case LTU:
2983 return arith_operand (cmp1, VOIDmode);
2985 case LE:
2986 return sle_operand (cmp1, VOIDmode);
2988 case LEU:
2989 return sleu_operand (cmp1, VOIDmode);
2991 default:
2992 gcc_unreachable ();
2996 /* Canonicalize LE or LEU comparisons into LT comparisons when
2997 possible to avoid extra instructions or inverting the
2998 comparison. */
3000 static bool
3001 mips_canonicalize_comparison (enum rtx_code *code, rtx *cmp1,
3002 enum machine_mode mode)
3004 HOST_WIDE_INT original, plus_one;
3006 if (GET_CODE (*cmp1) != CONST_INT)
3007 return false;
3009 original = INTVAL (*cmp1);
3010 plus_one = trunc_int_for_mode ((unsigned HOST_WIDE_INT) original + 1, mode);
3012 switch (*code)
3014 case LE:
3015 if (original < plus_one)
3017 *code = LT;
3018 *cmp1 = force_reg (mode, GEN_INT (plus_one));
3019 return true;
3021 break;
3023 case LEU:
3024 if (plus_one != 0)
3026 *code = LTU;
3027 *cmp1 = force_reg (mode, GEN_INT (plus_one));
3028 return true;
3030 break;
3032 default:
3033 return false;
3036 return false;
3040 /* Compare CMP0 and CMP1 using relational operator CODE and store the
3041 result in TARGET. CMP0 and TARGET are register_operands that have
3042 the same integer mode. If INVERT_PTR is nonnull, it's OK to set
3043 TARGET to the inverse of the result and flip *INVERT_PTR instead. */
3045 static void
3046 mips_emit_int_relational (enum rtx_code code, bool *invert_ptr,
3047 rtx target, rtx cmp0, rtx cmp1)
3049 /* First see if there is a MIPS instruction that can do this operation
3050 with CMP1 in its current form. If not, try to canonicalize the
3051 comparison to LT. If that fails, try doing the same for the
3052 inverse operation. If that also fails, force CMP1 into a register
3053 and try again. */
3054 if (mips_relational_operand_ok_p (code, cmp1))
3055 mips_emit_binary (code, target, cmp0, cmp1);
3056 else if (mips_canonicalize_comparison (&code, &cmp1, GET_MODE (target)))
3057 mips_emit_binary (code, target, cmp0, cmp1);
3058 else
3060 enum rtx_code inv_code = reverse_condition (code);
3061 if (!mips_relational_operand_ok_p (inv_code, cmp1))
3063 cmp1 = force_reg (GET_MODE (cmp0), cmp1);
3064 mips_emit_int_relational (code, invert_ptr, target, cmp0, cmp1);
3066 else if (invert_ptr == 0)
3068 rtx inv_target = gen_reg_rtx (GET_MODE (target));
3069 mips_emit_binary (inv_code, inv_target, cmp0, cmp1);
3070 mips_emit_binary (XOR, target, inv_target, const1_rtx);
3072 else
3074 *invert_ptr = !*invert_ptr;
3075 mips_emit_binary (inv_code, target, cmp0, cmp1);
3080 /* Return a register that is zero iff CMP0 and CMP1 are equal.
3081 The register will have the same mode as CMP0. */
3083 static rtx
3084 mips_zero_if_equal (rtx cmp0, rtx cmp1)
3086 if (cmp1 == const0_rtx)
3087 return cmp0;
3089 if (uns_arith_operand (cmp1, VOIDmode))
3090 return expand_binop (GET_MODE (cmp0), xor_optab,
3091 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
3093 return expand_binop (GET_MODE (cmp0), sub_optab,
3094 cmp0, cmp1, 0, 0, OPTAB_DIRECT);
3097 /* Convert a comparison into something that can be used in a branch or
3098 conditional move. cmp_operands[0] and cmp_operands[1] are the values
3099 being compared and *CODE is the code used to compare them.
3101 Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
3102 If NEED_EQ_NE_P, then only EQ/NE comparisons against zero are possible,
3103 otherwise any standard branch condition can be used. The standard branch
3104 conditions are:
3106 - EQ/NE between two registers.
3107 - any comparison between a register and zero. */
3109 static void
3110 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
3112 if (GET_MODE_CLASS (GET_MODE (cmp_operands[0])) == MODE_INT)
3114 if (!need_eq_ne_p && cmp_operands[1] == const0_rtx)
3116 *op0 = cmp_operands[0];
3117 *op1 = cmp_operands[1];
3119 else if (*code == EQ || *code == NE)
3121 if (need_eq_ne_p)
3123 *op0 = mips_zero_if_equal (cmp_operands[0], cmp_operands[1]);
3124 *op1 = const0_rtx;
3126 else
3128 *op0 = cmp_operands[0];
3129 *op1 = force_reg (GET_MODE (*op0), cmp_operands[1]);
3132 else
3134 /* The comparison needs a separate scc instruction. Store the
3135 result of the scc in *OP0 and compare it against zero. */
3136 bool invert = false;
3137 *op0 = gen_reg_rtx (GET_MODE (cmp_operands[0]));
3138 *op1 = const0_rtx;
3139 mips_emit_int_relational (*code, &invert, *op0,
3140 cmp_operands[0], cmp_operands[1]);
3141 *code = (invert ? EQ : NE);
3144 else
3146 enum rtx_code cmp_code;
3148 /* Floating-point tests use a separate c.cond.fmt comparison to
3149 set a condition code register. The branch or conditional move
3150 will then compare that register against zero.
3152 Set CMP_CODE to the code of the comparison instruction and
3153 *CODE to the code that the branch or move should use. */
3154 switch (*code)
3156 case NE:
3157 case LTGT:
3158 case ORDERED:
3159 cmp_code = reverse_condition_maybe_unordered (*code);
3160 *code = EQ;
3161 break;
3163 default:
3164 cmp_code = *code;
3165 *code = NE;
3166 break;
3168 *op0 = (ISA_HAS_8CC
3169 ? gen_reg_rtx (CCmode)
3170 : gen_rtx_REG (CCmode, FPSW_REGNUM));
3171 *op1 = const0_rtx;
3172 mips_emit_binary (cmp_code, *op0, cmp_operands[0], cmp_operands[1]);
3176 /* Try comparing cmp_operands[0] and cmp_operands[1] using rtl code CODE.
3177 Store the result in TARGET and return true if successful.
3179 On 64-bit targets, TARGET may be wider than cmp_operands[0]. */
3181 bool
3182 mips_emit_scc (enum rtx_code code, rtx target)
3184 if (GET_MODE_CLASS (GET_MODE (cmp_operands[0])) != MODE_INT)
3185 return false;
3187 target = gen_lowpart (GET_MODE (cmp_operands[0]), target);
3188 if (code == EQ || code == NE)
3190 rtx zie = mips_zero_if_equal (cmp_operands[0], cmp_operands[1]);
3191 mips_emit_binary (code, target, zie, const0_rtx);
3193 else
3194 mips_emit_int_relational (code, 0, target,
3195 cmp_operands[0], cmp_operands[1]);
3196 return true;
3199 /* Emit the common code for doing conditional branches.
3200 operand[0] is the label to jump to.
3201 The comparison operands are saved away by cmp{si,di,sf,df}. */
3203 void
3204 gen_conditional_branch (rtx *operands, enum rtx_code code)
3206 rtx op0, op1, target;
3208 mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
3209 target = gen_rtx_IF_THEN_ELSE (VOIDmode,
3210 gen_rtx_fmt_ee (code, GET_MODE (op0),
3211 op0, op1),
3212 gen_rtx_LABEL_REF (VOIDmode, operands[0]),
3213 pc_rtx);
3214 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, target));
3217 /* Emit the common code for conditional moves. OPERANDS is the array
3218 of operands passed to the conditional move define_expand. */
3220 void
3221 gen_conditional_move (rtx *operands)
3223 enum rtx_code code;
3224 rtx op0, op1;
3226 code = GET_CODE (operands[1]);
3227 mips_emit_compare (&code, &op0, &op1, true);
3228 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
3229 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
3230 gen_rtx_fmt_ee (code,
3231 GET_MODE (op0),
3232 op0, op1),
3233 operands[2], operands[3])));
3236 /* Emit a conditional trap. OPERANDS is the array of operands passed to
3237 the conditional_trap expander. */
3239 void
3240 mips_gen_conditional_trap (rtx *operands)
3242 rtx op0, op1;
3243 enum rtx_code cmp_code = GET_CODE (operands[0]);
3244 enum machine_mode mode = GET_MODE (cmp_operands[0]);
3246 /* MIPS conditional trap machine instructions don't have GT or LE
3247 flavors, so we must invert the comparison and convert to LT and
3248 GE, respectively. */
3249 switch (cmp_code)
3251 case GT: cmp_code = LT; break;
3252 case LE: cmp_code = GE; break;
3253 case GTU: cmp_code = LTU; break;
3254 case LEU: cmp_code = GEU; break;
3255 default: break;
3257 if (cmp_code == GET_CODE (operands[0]))
3259 op0 = cmp_operands[0];
3260 op1 = cmp_operands[1];
3262 else
3264 op0 = cmp_operands[1];
3265 op1 = cmp_operands[0];
3267 op0 = force_reg (mode, op0);
3268 if (!arith_operand (op1, mode))
3269 op1 = force_reg (mode, op1);
3271 emit_insn (gen_rtx_TRAP_IF (VOIDmode,
3272 gen_rtx_fmt_ee (cmp_code, mode, op0, op1),
3273 operands[1]));
3276 /* Load function address ADDR into register DEST. SIBCALL_P is true
3277 if the address is needed for a sibling call. */
3279 static void
3280 mips_load_call_address (rtx dest, rtx addr, int sibcall_p)
3282 /* If we're generating PIC, and this call is to a global function,
3283 try to allow its address to be resolved lazily. This isn't
3284 possible for NewABI sibcalls since the value of $gp on entry
3285 to the stub would be our caller's gp, not ours. */
3286 if (TARGET_EXPLICIT_RELOCS
3287 && !(sibcall_p && TARGET_NEWABI)
3288 && global_got_operand (addr, VOIDmode))
3290 rtx high, lo_sum_symbol;
3292 high = mips_unspec_offset_high (dest, pic_offset_table_rtx,
3293 addr, SYMBOL_GOTOFF_CALL);
3294 lo_sum_symbol = mips_unspec_address (addr, SYMBOL_GOTOFF_CALL);
3295 if (Pmode == SImode)
3296 emit_insn (gen_load_callsi (dest, high, lo_sum_symbol));
3297 else
3298 emit_insn (gen_load_calldi (dest, high, lo_sum_symbol));
3300 else
3301 emit_move_insn (dest, addr);
3305 /* Expand a call or call_value instruction. RESULT is where the
3306 result will go (null for calls), ADDR is the address of the
3307 function, ARGS_SIZE is the size of the arguments and AUX is
3308 the value passed to us by mips_function_arg. SIBCALL_P is true
3309 if we are expanding a sibling call, false if we're expanding
3310 a normal call. */
3312 void
3313 mips_expand_call (rtx result, rtx addr, rtx args_size, rtx aux, int sibcall_p)
3315 rtx orig_addr, pattern, insn;
3317 orig_addr = addr;
3318 if (!call_insn_operand (addr, VOIDmode))
3320 addr = gen_reg_rtx (Pmode);
3321 mips_load_call_address (addr, orig_addr, sibcall_p);
3324 if (TARGET_MIPS16
3325 && mips16_hard_float
3326 && build_mips16_call_stub (result, addr, args_size,
3327 aux == 0 ? 0 : (int) GET_MODE (aux)))
3328 return;
3330 if (result == 0)
3331 pattern = (sibcall_p
3332 ? gen_sibcall_internal (addr, args_size)
3333 : gen_call_internal (addr, args_size));
3334 else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
3336 rtx reg1, reg2;
3338 reg1 = XEXP (XVECEXP (result, 0, 0), 0);
3339 reg2 = XEXP (XVECEXP (result, 0, 1), 0);
3340 pattern =
3341 (sibcall_p
3342 ? gen_sibcall_value_multiple_internal (reg1, addr, args_size, reg2)
3343 : gen_call_value_multiple_internal (reg1, addr, args_size, reg2));
3345 else
3346 pattern = (sibcall_p
3347 ? gen_sibcall_value_internal (result, addr, args_size)
3348 : gen_call_value_internal (result, addr, args_size));
3350 insn = emit_call_insn (pattern);
3352 /* Lazy-binding stubs require $gp to be valid on entry. */
3353 if (global_got_operand (orig_addr, VOIDmode))
3354 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
3358 /* We can handle any sibcall when TARGET_SIBCALLS is true. */
3360 static bool
3361 mips_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
3362 tree exp ATTRIBUTE_UNUSED)
3364 return TARGET_SIBCALLS;
3367 /* Emit code to move general operand SRC into condition-code
3368 register DEST. SCRATCH is a scratch TFmode float register.
3369 The sequence is:
3371 FP1 = SRC
3372 FP2 = 0.0f
3373 DEST = FP2 < FP1
3375 where FP1 and FP2 are single-precision float registers
3376 taken from SCRATCH. */
3378 void
3379 mips_emit_fcc_reload (rtx dest, rtx src, rtx scratch)
3381 rtx fp1, fp2;
3383 /* Change the source to SFmode. */
3384 if (MEM_P (src))
3385 src = adjust_address (src, SFmode, 0);
3386 else if (REG_P (src) || GET_CODE (src) == SUBREG)
3387 src = gen_rtx_REG (SFmode, true_regnum (src));
3389 fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
3390 fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + FP_INC);
3392 emit_move_insn (copy_rtx (fp1), src);
3393 emit_move_insn (copy_rtx (fp2), CONST0_RTX (SFmode));
3394 emit_insn (gen_slt_sf (dest, fp2, fp1));
3397 /* Emit code to change the current function's return address to
3398 ADDRESS. SCRATCH is available as a scratch register, if needed.
3399 ADDRESS and SCRATCH are both word-mode GPRs. */
3401 void
3402 mips_set_return_address (rtx address, rtx scratch)
3404 rtx slot_address;
3406 compute_frame_size (get_frame_size ());
3407 gcc_assert ((cfun->machine->frame.mask >> 31) & 1);
3408 slot_address = mips_add_offset (scratch, stack_pointer_rtx,
3409 cfun->machine->frame.gp_sp_offset);
3411 emit_move_insn (gen_rtx_MEM (GET_MODE (address), slot_address), address);
3414 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
3415 Assume that the areas do not overlap. */
3417 static void
3418 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
3420 HOST_WIDE_INT offset, delta;
3421 unsigned HOST_WIDE_INT bits;
3422 int i;
3423 enum machine_mode mode;
3424 rtx *regs;
3426 /* Work out how many bits to move at a time. If both operands have
3427 half-word alignment, it is usually better to move in half words.
3428 For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
3429 and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
3430 Otherwise move word-sized chunks. */
3431 if (MEM_ALIGN (src) == BITS_PER_WORD / 2
3432 && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
3433 bits = BITS_PER_WORD / 2;
3434 else
3435 bits = BITS_PER_WORD;
3437 mode = mode_for_size (bits, MODE_INT, 0);
3438 delta = bits / BITS_PER_UNIT;
3440 /* Allocate a buffer for the temporary registers. */
3441 regs = alloca (sizeof (rtx) * length / delta);
3443 /* Load as many BITS-sized chunks as possible. Use a normal load if
3444 the source has enough alignment, otherwise use left/right pairs. */
3445 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
3447 regs[i] = gen_reg_rtx (mode);
3448 if (MEM_ALIGN (src) >= bits)
3449 emit_move_insn (regs[i], adjust_address (src, mode, offset));
3450 else
3452 rtx part = adjust_address (src, BLKmode, offset);
3453 if (!mips_expand_unaligned_load (regs[i], part, bits, 0))
3454 gcc_unreachable ();
3458 /* Copy the chunks to the destination. */
3459 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
3460 if (MEM_ALIGN (dest) >= bits)
3461 emit_move_insn (adjust_address (dest, mode, offset), regs[i]);
3462 else
3464 rtx part = adjust_address (dest, BLKmode, offset);
3465 if (!mips_expand_unaligned_store (part, regs[i], bits, 0))
3466 gcc_unreachable ();
3469 /* Mop up any left-over bytes. */
3470 if (offset < length)
3472 src = adjust_address (src, BLKmode, offset);
3473 dest = adjust_address (dest, BLKmode, offset);
3474 move_by_pieces (dest, src, length - offset,
3475 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
3479 #define MAX_MOVE_REGS 4
3480 #define MAX_MOVE_BYTES (MAX_MOVE_REGS * UNITS_PER_WORD)
3483 /* Helper function for doing a loop-based block operation on memory
3484 reference MEM. Each iteration of the loop will operate on LENGTH
3485 bytes of MEM.
3487 Create a new base register for use within the loop and point it to
3488 the start of MEM. Create a new memory reference that uses this
3489 register. Store them in *LOOP_REG and *LOOP_MEM respectively. */
3491 static void
3492 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
3493 rtx *loop_reg, rtx *loop_mem)
3495 *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
3497 /* Although the new mem does not refer to a known location,
3498 it does keep up to LENGTH bytes of alignment. */
3499 *loop_mem = change_address (mem, BLKmode, *loop_reg);
3500 set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
3504 /* Move LENGTH bytes from SRC to DEST using a loop that moves MAX_MOVE_BYTES
3505 per iteration. LENGTH must be at least MAX_MOVE_BYTES. Assume that the
3506 memory regions do not overlap. */
3508 static void
3509 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length)
3511 rtx label, src_reg, dest_reg, final_src;
3512 HOST_WIDE_INT leftover;
3514 leftover = length % MAX_MOVE_BYTES;
3515 length -= leftover;
3517 /* Create registers and memory references for use within the loop. */
3518 mips_adjust_block_mem (src, MAX_MOVE_BYTES, &src_reg, &src);
3519 mips_adjust_block_mem (dest, MAX_MOVE_BYTES, &dest_reg, &dest);
3521 /* Calculate the value that SRC_REG should have after the last iteration
3522 of the loop. */
3523 final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
3524 0, 0, OPTAB_WIDEN);
3526 /* Emit the start of the loop. */
3527 label = gen_label_rtx ();
3528 emit_label (label);
3530 /* Emit the loop body. */
3531 mips_block_move_straight (dest, src, MAX_MOVE_BYTES);
3533 /* Move on to the next block. */
3534 emit_move_insn (src_reg, plus_constant (src_reg, MAX_MOVE_BYTES));
3535 emit_move_insn (dest_reg, plus_constant (dest_reg, MAX_MOVE_BYTES));
3537 /* Emit the loop condition. */
3538 if (Pmode == DImode)
3539 emit_insn (gen_cmpdi (src_reg, final_src));
3540 else
3541 emit_insn (gen_cmpsi (src_reg, final_src));
3542 emit_jump_insn (gen_bne (label));
3544 /* Mop up any left-over bytes. */
3545 if (leftover)
3546 mips_block_move_straight (dest, src, leftover);
3549 /* Expand a movmemsi instruction. */
3551 bool
3552 mips_expand_block_move (rtx dest, rtx src, rtx length)
3554 if (GET_CODE (length) == CONST_INT)
3556 if (INTVAL (length) <= 2 * MAX_MOVE_BYTES)
3558 mips_block_move_straight (dest, src, INTVAL (length));
3559 return true;
3561 else if (optimize)
3563 mips_block_move_loop (dest, src, INTVAL (length));
3564 return true;
3567 return false;
3570 /* Argument support functions. */
3572 /* Initialize CUMULATIVE_ARGS for a function. */
3574 void
3575 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
3576 rtx libname ATTRIBUTE_UNUSED)
3578 static CUMULATIVE_ARGS zero_cum;
3579 tree param, next_param;
3581 *cum = zero_cum;
3582 cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
3584 /* Determine if this function has variable arguments. This is
3585 indicated by the last argument being 'void_type_mode' if there
3586 are no variable arguments. The standard MIPS calling sequence
3587 passes all arguments in the general purpose registers in this case. */
3589 for (param = fntype ? TYPE_ARG_TYPES (fntype) : 0;
3590 param != 0; param = next_param)
3592 next_param = TREE_CHAIN (param);
3593 if (next_param == 0 && TREE_VALUE (param) != void_type_node)
3594 cum->gp_reg_found = 1;
3599 /* Fill INFO with information about a single argument. CUM is the
3600 cumulative state for earlier arguments. MODE is the mode of this
3601 argument and TYPE is its type (if known). NAMED is true if this
3602 is a named (fixed) argument rather than a variable one. */
3604 static void
3605 mips_arg_info (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
3606 tree type, int named, struct mips_arg_info *info)
3608 bool doubleword_aligned_p;
3609 unsigned int num_bytes, num_words, max_regs;
3611 /* Work out the size of the argument. */
3612 num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
3613 num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3615 /* Decide whether it should go in a floating-point register, assuming
3616 one is free. Later code checks for availability.
3618 The checks against UNITS_PER_FPVALUE handle the soft-float and
3619 single-float cases. */
3620 switch (mips_abi)
3622 case ABI_EABI:
3623 /* The EABI conventions have traditionally been defined in terms
3624 of TYPE_MODE, regardless of the actual type. */
3625 info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
3626 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3627 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
3628 break;
3630 case ABI_32:
3631 case ABI_O64:
3632 /* Only leading floating-point scalars are passed in
3633 floating-point registers. We also handle vector floats the same
3634 say, which is OK because they are not covered by the standard ABI. */
3635 info->fpr_p = (!cum->gp_reg_found
3636 && cum->arg_number < 2
3637 && (type == 0 || SCALAR_FLOAT_TYPE_P (type)
3638 || VECTOR_FLOAT_TYPE_P (type))
3639 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3640 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3641 && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
3642 break;
3644 case ABI_N32:
3645 case ABI_64:
3646 /* Scalar and complex floating-point types are passed in
3647 floating-point registers. */
3648 info->fpr_p = (named
3649 && (type == 0 || FLOAT_TYPE_P (type))
3650 && (GET_MODE_CLASS (mode) == MODE_FLOAT
3651 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3652 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3653 && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
3655 /* ??? According to the ABI documentation, the real and imaginary
3656 parts of complex floats should be passed in individual registers.
3657 The real and imaginary parts of stack arguments are supposed
3658 to be contiguous and there should be an extra word of padding
3659 at the end.
3661 This has two problems. First, it makes it impossible to use a
3662 single "void *" va_list type, since register and stack arguments
3663 are passed differently. (At the time of writing, MIPSpro cannot
3664 handle complex float varargs correctly.) Second, it's unclear
3665 what should happen when there is only one register free.
3667 For now, we assume that named complex floats should go into FPRs
3668 if there are two FPRs free, otherwise they should be passed in the
3669 same way as a struct containing two floats. */
3670 if (info->fpr_p
3671 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3672 && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
3674 if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
3675 info->fpr_p = false;
3676 else
3677 num_words = 2;
3679 break;
3681 default:
3682 gcc_unreachable ();
3685 /* See whether the argument has doubleword alignment. */
3686 doubleword_aligned_p = FUNCTION_ARG_BOUNDARY (mode, type) > BITS_PER_WORD;
3688 /* Set REG_OFFSET to the register count we're interested in.
3689 The EABI allocates the floating-point registers separately,
3690 but the other ABIs allocate them like integer registers. */
3691 info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
3692 ? cum->num_fprs
3693 : cum->num_gprs);
3695 /* Advance to an even register if the argument is doubleword-aligned. */
3696 if (doubleword_aligned_p)
3697 info->reg_offset += info->reg_offset & 1;
3699 /* Work out the offset of a stack argument. */
3700 info->stack_offset = cum->stack_words;
3701 if (doubleword_aligned_p)
3702 info->stack_offset += info->stack_offset & 1;
3704 max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
3706 /* Partition the argument between registers and stack. */
3707 info->reg_words = MIN (num_words, max_regs);
3708 info->stack_words = num_words - info->reg_words;
3712 /* Implement FUNCTION_ARG_ADVANCE. */
3714 void
3715 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3716 tree type, int named)
3718 struct mips_arg_info info;
3720 mips_arg_info (cum, mode, type, named, &info);
3722 if (!info.fpr_p)
3723 cum->gp_reg_found = true;
3725 /* See the comment above the cumulative args structure in mips.h
3726 for an explanation of what this code does. It assumes the O32
3727 ABI, which passes at most 2 arguments in float registers. */
3728 if (cum->arg_number < 2 && info.fpr_p)
3729 cum->fp_code += (mode == SFmode ? 1 : 2) << ((cum->arg_number - 1) * 2);
3731 if (mips_abi != ABI_EABI || !info.fpr_p)
3732 cum->num_gprs = info.reg_offset + info.reg_words;
3733 else if (info.reg_words > 0)
3734 cum->num_fprs += FP_INC;
3736 if (info.stack_words > 0)
3737 cum->stack_words = info.stack_offset + info.stack_words;
3739 cum->arg_number++;
3742 /* Implement FUNCTION_ARG. */
3744 struct rtx_def *
3745 function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
3746 tree type, int named)
3748 struct mips_arg_info info;
3750 /* We will be called with a mode of VOIDmode after the last argument
3751 has been seen. Whatever we return will be passed to the call
3752 insn. If we need a mips16 fp_code, return a REG with the code
3753 stored as the mode. */
3754 if (mode == VOIDmode)
3756 if (TARGET_MIPS16 && cum->fp_code != 0)
3757 return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
3759 else
3760 return 0;
3763 mips_arg_info (cum, mode, type, named, &info);
3765 /* Return straight away if the whole argument is passed on the stack. */
3766 if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
3767 return 0;
3769 if (type != 0
3770 && TREE_CODE (type) == RECORD_TYPE
3771 && TARGET_NEWABI
3772 && TYPE_SIZE_UNIT (type)
3773 && host_integerp (TYPE_SIZE_UNIT (type), 1)
3774 && named)
3776 /* The Irix 6 n32/n64 ABIs say that if any 64 bit chunk of the
3777 structure contains a double in its entirety, then that 64 bit
3778 chunk is passed in a floating point register. */
3779 tree field;
3781 /* First check to see if there is any such field. */
3782 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3783 if (TREE_CODE (field) == FIELD_DECL
3784 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3785 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
3786 && host_integerp (bit_position (field), 0)
3787 && int_bit_position (field) % BITS_PER_WORD == 0)
3788 break;
3790 if (field != 0)
3792 /* Now handle the special case by returning a PARALLEL
3793 indicating where each 64 bit chunk goes. INFO.REG_WORDS
3794 chunks are passed in registers. */
3795 unsigned int i;
3796 HOST_WIDE_INT bitpos;
3797 rtx ret;
3799 /* assign_parms checks the mode of ENTRY_PARM, so we must
3800 use the actual mode here. */
3801 ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
3803 bitpos = 0;
3804 field = TYPE_FIELDS (type);
3805 for (i = 0; i < info.reg_words; i++)
3807 rtx reg;
3809 for (; field; field = TREE_CHAIN (field))
3810 if (TREE_CODE (field) == FIELD_DECL
3811 && int_bit_position (field) >= bitpos)
3812 break;
3814 if (field
3815 && int_bit_position (field) == bitpos
3816 && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3817 && !TARGET_SOFT_FLOAT
3818 && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
3819 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
3820 else
3821 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
3823 XVECEXP (ret, 0, i)
3824 = gen_rtx_EXPR_LIST (VOIDmode, reg,
3825 GEN_INT (bitpos / BITS_PER_UNIT));
3827 bitpos += BITS_PER_WORD;
3829 return ret;
3833 /* Handle the n32/n64 conventions for passing complex floating-point
3834 arguments in FPR pairs. The real part goes in the lower register
3835 and the imaginary part goes in the upper register. */
3836 if (TARGET_NEWABI
3837 && info.fpr_p
3838 && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3840 rtx real, imag;
3841 enum machine_mode inner;
3842 int reg;
3844 inner = GET_MODE_INNER (mode);
3845 reg = FP_ARG_FIRST + info.reg_offset;
3846 real = gen_rtx_EXPR_LIST (VOIDmode,
3847 gen_rtx_REG (inner, reg),
3848 const0_rtx);
3849 imag = gen_rtx_EXPR_LIST (VOIDmode,
3850 gen_rtx_REG (inner, reg + info.reg_words / 2),
3851 GEN_INT (GET_MODE_SIZE (inner)));
3852 return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
3855 if (!info.fpr_p)
3856 return gen_rtx_REG (mode, GP_ARG_FIRST + info.reg_offset);
3857 else if (info.reg_offset == 1)
3858 /* This code handles the special o32 case in which the second word
3859 of the argument structure is passed in floating-point registers. */
3860 return gen_rtx_REG (mode, FP_ARG_FIRST + FP_INC);
3861 else
3862 return gen_rtx_REG (mode, FP_ARG_FIRST + info.reg_offset);
3866 /* Implement TARGET_ARG_PARTIAL_BYTES. */
3868 static int
3869 mips_arg_partial_bytes (CUMULATIVE_ARGS *cum,
3870 enum machine_mode mode, tree type, bool named)
3872 struct mips_arg_info info;
3874 mips_arg_info (cum, mode, type, named, &info);
3875 return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
3879 /* Implement FUNCTION_ARG_BOUNDARY. Every parameter gets at least
3880 PARM_BOUNDARY bits of alignment, but will be given anything up
3881 to STACK_BOUNDARY bits if the type requires it. */
3884 function_arg_boundary (enum machine_mode mode, tree type)
3886 unsigned int alignment;
3888 alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
3889 if (alignment < PARM_BOUNDARY)
3890 alignment = PARM_BOUNDARY;
3891 if (alignment > STACK_BOUNDARY)
3892 alignment = STACK_BOUNDARY;
3893 return alignment;
3896 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
3897 upward rather than downward. In other words, return true if the
3898 first byte of the stack slot has useful data, false if the last
3899 byte does. */
3901 bool
3902 mips_pad_arg_upward (enum machine_mode mode, tree type)
3904 /* On little-endian targets, the first byte of every stack argument
3905 is passed in the first byte of the stack slot. */
3906 if (!BYTES_BIG_ENDIAN)
3907 return true;
3909 /* Otherwise, integral types are padded downward: the last byte of a
3910 stack argument is passed in the last byte of the stack slot. */
3911 if (type != 0
3912 ? INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)
3913 : GET_MODE_CLASS (mode) == MODE_INT)
3914 return false;
3916 /* Big-endian o64 pads floating-point arguments downward. */
3917 if (mips_abi == ABI_O64)
3918 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
3919 return false;
3921 /* Other types are padded upward for o32, o64, n32 and n64. */
3922 if (mips_abi != ABI_EABI)
3923 return true;
3925 /* Arguments smaller than a stack slot are padded downward. */
3926 if (mode != BLKmode)
3927 return (GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY);
3928 else
3929 return (int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT));
3933 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...). Return !BYTES_BIG_ENDIAN
3934 if the least significant byte of the register has useful data. Return
3935 the opposite if the most significant byte does. */
3937 bool
3938 mips_pad_reg_upward (enum machine_mode mode, tree type)
3940 /* No shifting is required for floating-point arguments. */
3941 if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
3942 return !BYTES_BIG_ENDIAN;
3944 /* Otherwise, apply the same padding to register arguments as we do
3945 to stack arguments. */
3946 return mips_pad_arg_upward (mode, type);
3949 static void
3950 mips_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3951 tree type, int *pretend_size ATTRIBUTE_UNUSED,
3952 int no_rtl)
3954 CUMULATIVE_ARGS local_cum;
3955 int gp_saved, fp_saved;
3957 /* The caller has advanced CUM up to, but not beyond, the last named
3958 argument. Advance a local copy of CUM past the last "real" named
3959 argument, to find out how many registers are left over. */
3961 local_cum = *cum;
3962 FUNCTION_ARG_ADVANCE (local_cum, mode, type, 1);
3964 /* Found out how many registers we need to save. */
3965 gp_saved = MAX_ARGS_IN_REGISTERS - local_cum.num_gprs;
3966 fp_saved = (EABI_FLOAT_VARARGS_P
3967 ? MAX_ARGS_IN_REGISTERS - local_cum.num_fprs
3968 : 0);
3970 if (!no_rtl)
3972 if (gp_saved > 0)
3974 rtx ptr, mem;
3976 ptr = plus_constant (virtual_incoming_args_rtx,
3977 REG_PARM_STACK_SPACE (cfun->decl)
3978 - gp_saved * UNITS_PER_WORD);
3979 mem = gen_rtx_MEM (BLKmode, ptr);
3980 set_mem_alias_set (mem, get_varargs_alias_set ());
3982 move_block_from_reg (local_cum.num_gprs + GP_ARG_FIRST,
3983 mem, gp_saved);
3985 if (fp_saved > 0)
3987 /* We can't use move_block_from_reg, because it will use
3988 the wrong mode. */
3989 enum machine_mode mode;
3990 int off, i;
3992 /* Set OFF to the offset from virtual_incoming_args_rtx of
3993 the first float register. The FP save area lies below
3994 the integer one, and is aligned to UNITS_PER_FPVALUE bytes. */
3995 off = -gp_saved * UNITS_PER_WORD;
3996 off &= ~(UNITS_PER_FPVALUE - 1);
3997 off -= fp_saved * UNITS_PER_FPREG;
3999 mode = TARGET_SINGLE_FLOAT ? SFmode : DFmode;
4001 for (i = local_cum.num_fprs; i < MAX_ARGS_IN_REGISTERS; i += FP_INC)
4003 rtx ptr, mem;
4005 ptr = plus_constant (virtual_incoming_args_rtx, off);
4006 mem = gen_rtx_MEM (mode, ptr);
4007 set_mem_alias_set (mem, get_varargs_alias_set ());
4008 emit_move_insn (mem, gen_rtx_REG (mode, FP_ARG_FIRST + i));
4009 off += UNITS_PER_HWFPVALUE;
4013 if (REG_PARM_STACK_SPACE (cfun->decl) == 0)
4014 cfun->machine->varargs_size = (gp_saved * UNITS_PER_WORD
4015 + fp_saved * UNITS_PER_FPREG);
4018 /* Create the va_list data type.
4019 We keep 3 pointers, and two offsets.
4020 Two pointers are to the overflow area, which starts at the CFA.
4021 One of these is constant, for addressing into the GPR save area below it.
4022 The other is advanced up the stack through the overflow region.
4023 The third pointer is to the GPR save area. Since the FPR save area
4024 is just below it, we can address FPR slots off this pointer.
4025 We also keep two one-byte offsets, which are to be subtracted from the
4026 constant pointers to yield addresses in the GPR and FPR save areas.
4027 These are downcounted as float or non-float arguments are used,
4028 and when they get to zero, the argument must be obtained from the
4029 overflow region.
4030 If !EABI_FLOAT_VARARGS_P, then no FPR save area exists, and a single
4031 pointer is enough. It's started at the GPR save area, and is
4032 advanced, period.
4033 Note that the GPR save area is not constant size, due to optimization
4034 in the prologue. Hence, we can't use a design with two pointers
4035 and two offsets, although we could have designed this with two pointers
4036 and three offsets. */
4038 static tree
4039 mips_build_builtin_va_list (void)
4041 if (EABI_FLOAT_VARARGS_P)
4043 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff, f_res, record;
4044 tree array, index;
4046 record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4048 f_ovfl = build_decl (FIELD_DECL, get_identifier ("__overflow_argptr"),
4049 ptr_type_node);
4050 f_gtop = build_decl (FIELD_DECL, get_identifier ("__gpr_top"),
4051 ptr_type_node);
4052 f_ftop = build_decl (FIELD_DECL, get_identifier ("__fpr_top"),
4053 ptr_type_node);
4054 f_goff = build_decl (FIELD_DECL, get_identifier ("__gpr_offset"),
4055 unsigned_char_type_node);
4056 f_foff = build_decl (FIELD_DECL, get_identifier ("__fpr_offset"),
4057 unsigned_char_type_node);
4058 /* Explicitly pad to the size of a pointer, so that -Wpadded won't
4059 warn on every user file. */
4060 index = build_int_cst (NULL_TREE, GET_MODE_SIZE (ptr_mode) - 2 - 1);
4061 array = build_array_type (unsigned_char_type_node,
4062 build_index_type (index));
4063 f_res = build_decl (FIELD_DECL, get_identifier ("__reserved"), array);
4065 DECL_FIELD_CONTEXT (f_ovfl) = record;
4066 DECL_FIELD_CONTEXT (f_gtop) = record;
4067 DECL_FIELD_CONTEXT (f_ftop) = record;
4068 DECL_FIELD_CONTEXT (f_goff) = record;
4069 DECL_FIELD_CONTEXT (f_foff) = record;
4070 DECL_FIELD_CONTEXT (f_res) = record;
4072 TYPE_FIELDS (record) = f_ovfl;
4073 TREE_CHAIN (f_ovfl) = f_gtop;
4074 TREE_CHAIN (f_gtop) = f_ftop;
4075 TREE_CHAIN (f_ftop) = f_goff;
4076 TREE_CHAIN (f_goff) = f_foff;
4077 TREE_CHAIN (f_foff) = f_res;
4079 layout_type (record);
4080 return record;
4082 else if (TARGET_IRIX && TARGET_IRIX6)
4083 /* On IRIX 6, this type is 'char *'. */
4084 return build_pointer_type (char_type_node);
4085 else
4086 /* Otherwise, we use 'void *'. */
4087 return ptr_type_node;
4090 /* Implement va_start. */
4092 void
4093 mips_va_start (tree valist, rtx nextarg)
4095 if (EABI_FLOAT_VARARGS_P)
4097 const CUMULATIVE_ARGS *cum;
4098 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
4099 tree ovfl, gtop, ftop, goff, foff;
4100 tree t;
4101 int gpr_save_area_size;
4102 int fpr_save_area_size;
4103 int fpr_offset;
4105 cum = &current_function_args_info;
4106 gpr_save_area_size
4107 = (MAX_ARGS_IN_REGISTERS - cum->num_gprs) * UNITS_PER_WORD;
4108 fpr_save_area_size
4109 = (MAX_ARGS_IN_REGISTERS - cum->num_fprs) * UNITS_PER_FPREG;
4111 f_ovfl = TYPE_FIELDS (va_list_type_node);
4112 f_gtop = TREE_CHAIN (f_ovfl);
4113 f_ftop = TREE_CHAIN (f_gtop);
4114 f_goff = TREE_CHAIN (f_ftop);
4115 f_foff = TREE_CHAIN (f_goff);
4117 ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
4118 NULL_TREE);
4119 gtop = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
4120 NULL_TREE);
4121 ftop = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
4122 NULL_TREE);
4123 goff = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
4124 NULL_TREE);
4125 foff = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
4126 NULL_TREE);
4128 /* Emit code to initialize OVFL, which points to the next varargs
4129 stack argument. CUM->STACK_WORDS gives the number of stack
4130 words used by named arguments. */
4131 t = make_tree (TREE_TYPE (ovfl), virtual_incoming_args_rtx);
4132 if (cum->stack_words > 0)
4133 t = build (PLUS_EXPR, TREE_TYPE (ovfl), t,
4134 build_int_cst (NULL_TREE,
4135 cum->stack_words * UNITS_PER_WORD));
4136 t = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
4137 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4139 /* Emit code to initialize GTOP, the top of the GPR save area. */
4140 t = make_tree (TREE_TYPE (gtop), virtual_incoming_args_rtx);
4141 t = build (MODIFY_EXPR, TREE_TYPE (gtop), gtop, t);
4142 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4144 /* Emit code to initialize FTOP, the top of the FPR save area.
4145 This address is gpr_save_area_bytes below GTOP, rounded
4146 down to the next fp-aligned boundary. */
4147 t = make_tree (TREE_TYPE (ftop), virtual_incoming_args_rtx);
4148 fpr_offset = gpr_save_area_size + UNITS_PER_FPVALUE - 1;
4149 fpr_offset &= ~(UNITS_PER_FPVALUE - 1);
4150 if (fpr_offset)
4151 t = build (PLUS_EXPR, TREE_TYPE (ftop), t,
4152 build_int_cst (NULL_TREE, -fpr_offset));
4153 t = build (MODIFY_EXPR, TREE_TYPE (ftop), ftop, t);
4154 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4156 /* Emit code to initialize GOFF, the offset from GTOP of the
4157 next GPR argument. */
4158 t = build (MODIFY_EXPR, TREE_TYPE (goff), goff,
4159 build_int_cst (NULL_TREE, gpr_save_area_size));
4160 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4162 /* Likewise emit code to initialize FOFF, the offset from FTOP
4163 of the next FPR argument. */
4164 t = build (MODIFY_EXPR, TREE_TYPE (foff), foff,
4165 build_int_cst (NULL_TREE, fpr_save_area_size));
4166 expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4168 else
4170 nextarg = plus_constant (nextarg, -cfun->machine->varargs_size);
4171 std_expand_builtin_va_start (valist, nextarg);
4175 /* Implement va_arg. */
4177 static tree
4178 mips_gimplify_va_arg_expr (tree valist, tree type, tree *pre_p, tree *post_p)
4180 HOST_WIDE_INT size, rsize;
4181 tree addr;
4182 bool indirect;
4184 indirect = pass_by_reference (NULL, TYPE_MODE (type), type, 0);
4186 if (indirect)
4187 type = build_pointer_type (type);
4189 size = int_size_in_bytes (type);
4190 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
4192 if (mips_abi != ABI_EABI || !EABI_FLOAT_VARARGS_P)
4193 addr = std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
4194 else
4196 /* Not a simple merged stack. */
4198 tree f_ovfl, f_gtop, f_ftop, f_goff, f_foff;
4199 tree ovfl, top, off, align;
4200 HOST_WIDE_INT osize;
4201 tree t, u;
4203 f_ovfl = TYPE_FIELDS (va_list_type_node);
4204 f_gtop = TREE_CHAIN (f_ovfl);
4205 f_ftop = TREE_CHAIN (f_gtop);
4206 f_goff = TREE_CHAIN (f_ftop);
4207 f_foff = TREE_CHAIN (f_goff);
4209 /* We maintain separate pointers and offsets for floating-point
4210 and integer arguments, but we need similar code in both cases.
4211 Let:
4213 TOP be the top of the register save area;
4214 OFF be the offset from TOP of the next register;
4215 ADDR_RTX be the address of the argument;
4216 RSIZE be the number of bytes used to store the argument
4217 when it's in the register save area;
4218 OSIZE be the number of bytes used to store it when it's
4219 in the stack overflow area; and
4220 PADDING be (BYTES_BIG_ENDIAN ? OSIZE - RSIZE : 0)
4222 The code we want is:
4224 1: off &= -rsize; // round down
4225 2: if (off != 0)
4226 3: {
4227 4: addr_rtx = top - off;
4228 5: off -= rsize;
4229 6: }
4230 7: else
4231 8: {
4232 9: ovfl += ((intptr_t) ovfl + osize - 1) & -osize;
4233 10: addr_rtx = ovfl + PADDING;
4234 11: ovfl += osize;
4235 14: }
4237 [1] and [9] can sometimes be optimized away. */
4239 ovfl = build (COMPONENT_REF, TREE_TYPE (f_ovfl), valist, f_ovfl,
4240 NULL_TREE);
4242 if (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_FLOAT
4243 && GET_MODE_SIZE (TYPE_MODE (type)) <= UNITS_PER_FPVALUE)
4245 top = build (COMPONENT_REF, TREE_TYPE (f_ftop), valist, f_ftop,
4246 NULL_TREE);
4247 off = build (COMPONENT_REF, TREE_TYPE (f_foff), valist, f_foff,
4248 NULL_TREE);
4250 /* When floating-point registers are saved to the stack,
4251 each one will take up UNITS_PER_HWFPVALUE bytes, regardless
4252 of the float's precision. */
4253 rsize = UNITS_PER_HWFPVALUE;
4255 /* Overflow arguments are padded to UNITS_PER_WORD bytes
4256 (= PARM_BOUNDARY bits). This can be different from RSIZE
4257 in two cases:
4259 (1) On 32-bit targets when TYPE is a structure such as:
4261 struct s { float f; };
4263 Such structures are passed in paired FPRs, so RSIZE
4264 will be 8 bytes. However, the structure only takes
4265 up 4 bytes of memory, so OSIZE will only be 4.
4267 (2) In combinations such as -mgp64 -msingle-float
4268 -fshort-double. Doubles passed in registers
4269 will then take up 4 (UNITS_PER_HWFPVALUE) bytes,
4270 but those passed on the stack take up
4271 UNITS_PER_WORD bytes. */
4272 osize = MAX (GET_MODE_SIZE (TYPE_MODE (type)), UNITS_PER_WORD);
4274 else
4276 top = build (COMPONENT_REF, TREE_TYPE (f_gtop), valist, f_gtop,
4277 NULL_TREE);
4278 off = build (COMPONENT_REF, TREE_TYPE (f_goff), valist, f_goff,
4279 NULL_TREE);
4280 if (rsize > UNITS_PER_WORD)
4282 /* [1] Emit code for: off &= -rsize. */
4283 t = build (BIT_AND_EXPR, TREE_TYPE (off), off,
4284 build_int_cst (NULL_TREE, -rsize));
4285 t = build (MODIFY_EXPR, TREE_TYPE (off), off, t);
4286 gimplify_and_add (t, pre_p);
4288 osize = rsize;
4291 /* [2] Emit code to branch if off == 0. */
4292 t = build (NE_EXPR, boolean_type_node, off,
4293 build_int_cst (TREE_TYPE (off), 0));
4294 addr = build (COND_EXPR, ptr_type_node, t, NULL, NULL);
4296 /* [5] Emit code for: off -= rsize. We do this as a form of
4297 post-increment not available to C. Also widen for the
4298 coming pointer arithmetic. */
4299 t = fold_convert (TREE_TYPE (off), build_int_cst (NULL_TREE, rsize));
4300 t = build (POSTDECREMENT_EXPR, TREE_TYPE (off), off, t);
4301 t = fold_convert (sizetype, t);
4302 t = fold_convert (TREE_TYPE (top), t);
4304 /* [4] Emit code for: addr_rtx = top - off. On big endian machines,
4305 the argument has RSIZE - SIZE bytes of leading padding. */
4306 t = build (MINUS_EXPR, TREE_TYPE (top), top, t);
4307 if (BYTES_BIG_ENDIAN && rsize > size)
4309 u = fold_convert (TREE_TYPE (t), build_int_cst (NULL_TREE,
4310 rsize - size));
4311 t = build (PLUS_EXPR, TREE_TYPE (t), t, u);
4313 COND_EXPR_THEN (addr) = t;
4315 if (osize > UNITS_PER_WORD)
4317 /* [9] Emit: ovfl += ((intptr_t) ovfl + osize - 1) & -osize. */
4318 u = fold_convert (TREE_TYPE (ovfl),
4319 build_int_cst (NULL_TREE, osize - 1));
4320 t = build (PLUS_EXPR, TREE_TYPE (ovfl), ovfl, u);
4321 u = fold_convert (TREE_TYPE (ovfl),
4322 build_int_cst (NULL_TREE, -osize));
4323 t = build (BIT_AND_EXPR, TREE_TYPE (ovfl), t, u);
4324 align = build (MODIFY_EXPR, TREE_TYPE (ovfl), ovfl, t);
4326 else
4327 align = NULL;
4329 /* [10, 11]. Emit code to store ovfl in addr_rtx, then
4330 post-increment ovfl by osize. On big-endian machines,
4331 the argument has OSIZE - SIZE bytes of leading padding. */
4332 u = fold_convert (TREE_TYPE (ovfl),
4333 build_int_cst (NULL_TREE, osize));
4334 t = build (POSTINCREMENT_EXPR, TREE_TYPE (ovfl), ovfl, u);
4335 if (BYTES_BIG_ENDIAN && osize > size)
4337 u = fold_convert (TREE_TYPE (t),
4338 build_int_cst (NULL_TREE, osize - size));
4339 t = build (PLUS_EXPR, TREE_TYPE (t), t, u);
4342 /* String [9] and [10,11] together. */
4343 if (align)
4344 t = build (COMPOUND_EXPR, TREE_TYPE (t), align, t);
4345 COND_EXPR_ELSE (addr) = t;
4347 addr = fold_convert (build_pointer_type (type), addr);
4348 addr = build_va_arg_indirect_ref (addr);
4351 if (indirect)
4352 addr = build_va_arg_indirect_ref (addr);
4354 return addr;
4357 /* Return true if it is possible to use left/right accesses for a
4358 bitfield of WIDTH bits starting BITPOS bits into *OP. When
4359 returning true, update *OP, *LEFT and *RIGHT as follows:
4361 *OP is a BLKmode reference to the whole field.
4363 *LEFT is a QImode reference to the first byte if big endian or
4364 the last byte if little endian. This address can be used in the
4365 left-side instructions (lwl, swl, ldl, sdl).
4367 *RIGHT is a QImode reference to the opposite end of the field and
4368 can be used in the patterning right-side instruction. */
4370 static bool
4371 mips_get_unaligned_mem (rtx *op, unsigned int width, int bitpos,
4372 rtx *left, rtx *right)
4374 rtx first, last;
4376 /* Check that the operand really is a MEM. Not all the extv and
4377 extzv predicates are checked. */
4378 if (!MEM_P (*op))
4379 return false;
4381 /* Check that the size is valid. */
4382 if (width != 32 && (!TARGET_64BIT || width != 64))
4383 return false;
4385 /* We can only access byte-aligned values. Since we are always passed
4386 a reference to the first byte of the field, it is not necessary to
4387 do anything with BITPOS after this check. */
4388 if (bitpos % BITS_PER_UNIT != 0)
4389 return false;
4391 /* Reject aligned bitfields: we want to use a normal load or store
4392 instead of a left/right pair. */
4393 if (MEM_ALIGN (*op) >= width)
4394 return false;
4396 /* Adjust *OP to refer to the whole field. This also has the effect
4397 of legitimizing *OP's address for BLKmode, possibly simplifying it. */
4398 *op = adjust_address (*op, BLKmode, 0);
4399 set_mem_size (*op, GEN_INT (width / BITS_PER_UNIT));
4401 /* Get references to both ends of the field. We deliberately don't
4402 use the original QImode *OP for FIRST since the new BLKmode one
4403 might have a simpler address. */
4404 first = adjust_address (*op, QImode, 0);
4405 last = adjust_address (*op, QImode, width / BITS_PER_UNIT - 1);
4407 /* Allocate to LEFT and RIGHT according to endianness. LEFT should
4408 be the upper word and RIGHT the lower word. */
4409 if (TARGET_BIG_ENDIAN)
4410 *left = first, *right = last;
4411 else
4412 *left = last, *right = first;
4414 return true;
4418 /* Try to emit the equivalent of (set DEST (zero_extract SRC WIDTH BITPOS)).
4419 Return true on success. We only handle cases where zero_extract is
4420 equivalent to sign_extract. */
4422 bool
4423 mips_expand_unaligned_load (rtx dest, rtx src, unsigned int width, int bitpos)
4425 rtx left, right, temp;
4427 /* If TARGET_64BIT, the destination of a 32-bit load will be a
4428 paradoxical word_mode subreg. This is the only case in which
4429 we allow the destination to be larger than the source. */
4430 if (GET_CODE (dest) == SUBREG
4431 && GET_MODE (dest) == DImode
4432 && SUBREG_BYTE (dest) == 0
4433 && GET_MODE (SUBREG_REG (dest)) == SImode)
4434 dest = SUBREG_REG (dest);
4436 /* After the above adjustment, the destination must be the same
4437 width as the source. */
4438 if (GET_MODE_BITSIZE (GET_MODE (dest)) != width)
4439 return false;
4441 if (!mips_get_unaligned_mem (&src, width, bitpos, &left, &right))
4442 return false;
4444 temp = gen_reg_rtx (GET_MODE (dest));
4445 if (GET_MODE (dest) == DImode)
4447 emit_insn (gen_mov_ldl (temp, src, left));
4448 emit_insn (gen_mov_ldr (dest, copy_rtx (src), right, temp));
4450 else
4452 emit_insn (gen_mov_lwl (temp, src, left));
4453 emit_insn (gen_mov_lwr (dest, copy_rtx (src), right, temp));
4455 return true;
4459 /* Try to expand (set (zero_extract DEST WIDTH BITPOS) SRC). Return
4460 true on success. */
4462 bool
4463 mips_expand_unaligned_store (rtx dest, rtx src, unsigned int width, int bitpos)
4465 rtx left, right;
4466 enum machine_mode mode;
4468 if (!mips_get_unaligned_mem (&dest, width, bitpos, &left, &right))
4469 return false;
4471 mode = mode_for_size (width, MODE_INT, 0);
4472 src = gen_lowpart (mode, src);
4474 if (mode == DImode)
4476 emit_insn (gen_mov_sdl (dest, src, left));
4477 emit_insn (gen_mov_sdr (copy_rtx (dest), copy_rtx (src), right));
4479 else
4481 emit_insn (gen_mov_swl (dest, src, left));
4482 emit_insn (gen_mov_swr (copy_rtx (dest), copy_rtx (src), right));
4484 return true;
4487 /* Return true if X is a MEM with the same size as MODE. */
4489 bool
4490 mips_mem_fits_mode_p (enum machine_mode mode, rtx x)
4492 rtx size;
4494 if (!MEM_P (x))
4495 return false;
4497 size = MEM_SIZE (x);
4498 return size && INTVAL (size) == GET_MODE_SIZE (mode);
4501 /* Return true if (zero_extract OP SIZE POSITION) can be used as the
4502 source of an "ext" instruction or the destination of an "ins"
4503 instruction. OP must be a register operand and the following
4504 conditions must hold:
4506 0 <= POSITION < GET_MODE_BITSIZE (GET_MODE (op))
4507 0 < SIZE <= GET_MODE_BITSIZE (GET_MODE (op))
4508 0 < POSITION + SIZE <= GET_MODE_BITSIZE (GET_MODE (op))
4510 Also reject lengths equal to a word as they are better handled
4511 by the move patterns. */
4513 bool
4514 mips_use_ins_ext_p (rtx op, rtx size, rtx position)
4516 HOST_WIDE_INT len, pos;
4518 if (!ISA_HAS_EXT_INS
4519 || !register_operand (op, VOIDmode)
4520 || GET_MODE_BITSIZE (GET_MODE (op)) > BITS_PER_WORD)
4521 return false;
4523 len = INTVAL (size);
4524 pos = INTVAL (position);
4526 if (len <= 0 || len >= GET_MODE_BITSIZE (GET_MODE (op))
4527 || pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (op)))
4528 return false;
4530 return true;
4533 /* Set up globals to generate code for the ISA or processor
4534 described by INFO. */
4536 static void
4537 mips_set_architecture (const struct mips_cpu_info *info)
4539 if (info != 0)
4541 mips_arch_info = info;
4542 mips_arch = info->cpu;
4543 mips_isa = info->isa;
4548 /* Likewise for tuning. */
4550 static void
4551 mips_set_tune (const struct mips_cpu_info *info)
4553 if (info != 0)
4555 mips_tune_info = info;
4556 mips_tune = info->cpu;
4560 /* Implement TARGET_HANDLE_OPTION. */
4562 static bool
4563 mips_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
4565 switch (code)
4567 case OPT_mabi_:
4568 if (strcmp (arg, "32") == 0)
4569 mips_abi = ABI_32;
4570 else if (strcmp (arg, "o64") == 0)
4571 mips_abi = ABI_O64;
4572 else if (strcmp (arg, "n32") == 0)
4573 mips_abi = ABI_N32;
4574 else if (strcmp (arg, "64") == 0)
4575 mips_abi = ABI_64;
4576 else if (strcmp (arg, "eabi") == 0)
4577 mips_abi = ABI_EABI;
4578 else
4579 return false;
4580 return true;
4582 case OPT_march_:
4583 case OPT_mtune_:
4584 return mips_parse_cpu (arg) != 0;
4586 case OPT_mips:
4587 mips_isa_info = mips_parse_cpu (ACONCAT (("mips", arg, NULL)));
4588 return mips_isa_info != 0;
4590 case OPT_mno_flush_func:
4591 mips_cache_flush_func = NULL;
4592 return true;
4594 default:
4595 return true;
4599 /* Set up the threshold for data to go into the small data area, instead
4600 of the normal data area, and detect any conflicts in the switches. */
4602 void
4603 override_options (void)
4605 int i, start, regno;
4606 enum machine_mode mode;
4608 mips_section_threshold = g_switch_set ? g_switch_value : MIPS_DEFAULT_GVALUE;
4610 /* The following code determines the architecture and register size.
4611 Similar code was added to GAS 2.14 (see tc-mips.c:md_after_parse_args()).
4612 The GAS and GCC code should be kept in sync as much as possible. */
4614 if (mips_arch_string != 0)
4615 mips_set_architecture (mips_parse_cpu (mips_arch_string));
4617 if (mips_isa_info != 0)
4619 if (mips_arch_info == 0)
4620 mips_set_architecture (mips_isa_info);
4621 else if (mips_arch_info->isa != mips_isa_info->isa)
4622 error ("-%s conflicts with the other architecture options, "
4623 "which specify a %s processor",
4624 mips_isa_info->name,
4625 mips_cpu_info_from_isa (mips_arch_info->isa)->name);
4628 if (mips_arch_info == 0)
4630 #ifdef MIPS_CPU_STRING_DEFAULT
4631 mips_set_architecture (mips_parse_cpu (MIPS_CPU_STRING_DEFAULT));
4632 #else
4633 mips_set_architecture (mips_cpu_info_from_isa (MIPS_ISA_DEFAULT));
4634 #endif
4637 if (ABI_NEEDS_64BIT_REGS && !ISA_HAS_64BIT_REGS)
4638 error ("-march=%s is not compatible with the selected ABI",
4639 mips_arch_info->name);
4641 /* Optimize for mips_arch, unless -mtune selects a different processor. */
4642 if (mips_tune_string != 0)
4643 mips_set_tune (mips_parse_cpu (mips_tune_string));
4645 if (mips_tune_info == 0)
4646 mips_set_tune (mips_arch_info);
4648 /* Set cost structure for the processor. */
4649 mips_cost = &mips_rtx_cost_data[mips_tune];
4651 if ((target_flags_explicit & MASK_64BIT) != 0)
4653 /* The user specified the size of the integer registers. Make sure
4654 it agrees with the ABI and ISA. */
4655 if (TARGET_64BIT && !ISA_HAS_64BIT_REGS)
4656 error ("-mgp64 used with a 32-bit processor");
4657 else if (!TARGET_64BIT && ABI_NEEDS_64BIT_REGS)
4658 error ("-mgp32 used with a 64-bit ABI");
4659 else if (TARGET_64BIT && ABI_NEEDS_32BIT_REGS)
4660 error ("-mgp64 used with a 32-bit ABI");
4662 else
4664 /* Infer the integer register size from the ABI and processor.
4665 Restrict ourselves to 32-bit registers if that's all the
4666 processor has, or if the ABI cannot handle 64-bit registers. */
4667 if (ABI_NEEDS_32BIT_REGS || !ISA_HAS_64BIT_REGS)
4668 target_flags &= ~MASK_64BIT;
4669 else
4670 target_flags |= MASK_64BIT;
4673 if ((target_flags_explicit & MASK_FLOAT64) != 0)
4675 /* Really, -mfp32 and -mfp64 are ornamental options. There's
4676 only one right answer here. */
4677 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT && !TARGET_FLOAT64)
4678 error ("unsupported combination: %s", "-mgp64 -mfp32 -mdouble-float");
4679 else if (!TARGET_64BIT && TARGET_FLOAT64)
4680 error ("unsupported combination: %s", "-mgp32 -mfp64");
4681 else if (TARGET_SINGLE_FLOAT && TARGET_FLOAT64)
4682 error ("unsupported combination: %s", "-mfp64 -msingle-float");
4684 else
4686 /* -msingle-float selects 32-bit float registers. Otherwise the
4687 float registers should be the same size as the integer ones. */
4688 if (TARGET_64BIT && TARGET_DOUBLE_FLOAT)
4689 target_flags |= MASK_FLOAT64;
4690 else
4691 target_flags &= ~MASK_FLOAT64;
4694 /* End of code shared with GAS. */
4696 if ((target_flags_explicit & MASK_LONG64) == 0)
4698 if ((mips_abi == ABI_EABI && TARGET_64BIT) || mips_abi == ABI_64)
4699 target_flags |= MASK_LONG64;
4700 else
4701 target_flags &= ~MASK_LONG64;
4704 if (MIPS_MARCH_CONTROLS_SOFT_FLOAT
4705 && (target_flags_explicit & MASK_SOFT_FLOAT) == 0)
4707 /* For some configurations, it is useful to have -march control
4708 the default setting of MASK_SOFT_FLOAT. */
4709 switch ((int) mips_arch)
4711 case PROCESSOR_R4100:
4712 case PROCESSOR_R4111:
4713 case PROCESSOR_R4120:
4714 case PROCESSOR_R4130:
4715 target_flags |= MASK_SOFT_FLOAT;
4716 break;
4718 default:
4719 target_flags &= ~MASK_SOFT_FLOAT;
4720 break;
4724 if (!TARGET_OLDABI)
4725 flag_pcc_struct_return = 0;
4727 if ((target_flags_explicit & MASK_BRANCHLIKELY) == 0)
4729 /* If neither -mbranch-likely nor -mno-branch-likely was given
4730 on the command line, set MASK_BRANCHLIKELY based on the target
4731 architecture.
4733 By default, we enable use of Branch Likely instructions on
4734 all architectures which support them with the following
4735 exceptions: when creating MIPS32 or MIPS64 code, and when
4736 tuning for architectures where their use tends to hurt
4737 performance.
4739 The MIPS32 and MIPS64 architecture specifications say "Software
4740 is strongly encouraged to avoid use of Branch Likely
4741 instructions, as they will be removed from a future revision
4742 of the [MIPS32 and MIPS64] architecture." Therefore, we do not
4743 issue those instructions unless instructed to do so by
4744 -mbranch-likely. */
4745 if (ISA_HAS_BRANCHLIKELY
4746 && !(ISA_MIPS32 || ISA_MIPS32R2 || ISA_MIPS64)
4747 && !(TUNE_MIPS5500 || TUNE_SB1))
4748 target_flags |= MASK_BRANCHLIKELY;
4749 else
4750 target_flags &= ~MASK_BRANCHLIKELY;
4752 if (TARGET_BRANCHLIKELY && !ISA_HAS_BRANCHLIKELY)
4753 warning (0, "generation of Branch Likely instructions enabled, but not supported by architecture");
4755 /* The effect of -mabicalls isn't defined for the EABI. */
4756 if (mips_abi == ABI_EABI && TARGET_ABICALLS)
4758 error ("unsupported combination: %s", "-mabicalls -mabi=eabi");
4759 target_flags &= ~MASK_ABICALLS;
4762 /* -fpic (-KPIC) is the default when TARGET_ABICALLS is defined. We need
4763 to set flag_pic so that the LEGITIMATE_PIC_OPERAND_P macro will work. */
4764 /* ??? -non_shared turns off pic code generation, but this is not
4765 implemented. */
4766 if (TARGET_ABICALLS)
4768 flag_pic = 1;
4769 if (mips_section_threshold > 0)
4770 warning (0, "-G is incompatible with PIC code which is the default");
4773 /* mips_split_addresses is a half-way house between explicit
4774 relocations and the traditional assembler macros. It can
4775 split absolute 32-bit symbolic constants into a high/lo_sum
4776 pair but uses macros for other sorts of access.
4778 Like explicit relocation support for REL targets, it relies
4779 on GNU extensions in the assembler and the linker.
4781 Although this code should work for -O0, it has traditionally
4782 been treated as an optimization. */
4783 if (!TARGET_MIPS16 && TARGET_SPLIT_ADDRESSES
4784 && optimize && !flag_pic
4785 && !ABI_HAS_64BIT_SYMBOLS)
4786 mips_split_addresses = 1;
4787 else
4788 mips_split_addresses = 0;
4790 /* -mvr4130-align is a "speed over size" optimization: it usually produces
4791 faster code, but at the expense of more nops. Enable it at -O3 and
4792 above. */
4793 if (optimize > 2 && (target_flags_explicit & MASK_VR4130_ALIGN) == 0)
4794 target_flags |= MASK_VR4130_ALIGN;
4796 /* When compiling for the mips16, we cannot use floating point. We
4797 record the original hard float value in mips16_hard_float. */
4798 if (TARGET_MIPS16)
4800 if (TARGET_SOFT_FLOAT)
4801 mips16_hard_float = 0;
4802 else
4803 mips16_hard_float = 1;
4804 target_flags |= MASK_SOFT_FLOAT;
4806 /* Don't run the scheduler before reload, since it tends to
4807 increase register pressure. */
4808 flag_schedule_insns = 0;
4810 /* Don't do hot/cold partitioning. The constant layout code expects
4811 the whole function to be in a single section. */
4812 flag_reorder_blocks_and_partition = 0;
4814 /* Silently disable -mexplicit-relocs since it doesn't apply
4815 to mips16 code. Even so, it would overly pedantic to warn
4816 about "-mips16 -mexplicit-relocs", especially given that
4817 we use a %gprel() operator. */
4818 target_flags &= ~MASK_EXPLICIT_RELOCS;
4821 /* When using explicit relocs, we call dbr_schedule from within
4822 mips_reorg. */
4823 if (TARGET_EXPLICIT_RELOCS)
4825 mips_flag_delayed_branch = flag_delayed_branch;
4826 flag_delayed_branch = 0;
4829 #ifdef MIPS_TFMODE_FORMAT
4830 REAL_MODE_FORMAT (TFmode) = &MIPS_TFMODE_FORMAT;
4831 #endif
4833 /* Make sure that the user didn't turn off paired single support when
4834 MIPS-3D support is requested. */
4835 if (TARGET_MIPS3D && (target_flags_explicit & MASK_PAIRED_SINGLE_FLOAT)
4836 && !TARGET_PAIRED_SINGLE_FLOAT)
4837 error ("-mips3d requires -mpaired-single");
4839 /* If TARGET_MIPS3D, enable MASK_PAIRED_SINGLE_FLOAT. */
4840 if (TARGET_MIPS3D)
4841 target_flags |= MASK_PAIRED_SINGLE_FLOAT;
4843 /* Make sure that when TARGET_PAIRED_SINGLE_FLOAT is true, TARGET_FLOAT64
4844 and TARGET_HARD_FLOAT are both true. */
4845 if (TARGET_PAIRED_SINGLE_FLOAT && !(TARGET_FLOAT64 && TARGET_HARD_FLOAT))
4846 error ("-mips3d/-mpaired-single must be used with -mfp64 -mhard-float");
4848 /* Make sure that the ISA supports TARGET_PAIRED_SINGLE_FLOAT when it is
4849 enabled. */
4850 if (TARGET_PAIRED_SINGLE_FLOAT && !ISA_MIPS64)
4851 error ("-mips3d/-mpaired-single must be used with -mips64");
4853 if (TARGET_MIPS16 && TARGET_DSP)
4854 error ("-mips16 and -mdsp cannot be used together");
4856 mips_print_operand_punct['?'] = 1;
4857 mips_print_operand_punct['#'] = 1;
4858 mips_print_operand_punct['/'] = 1;
4859 mips_print_operand_punct['&'] = 1;
4860 mips_print_operand_punct['!'] = 1;
4861 mips_print_operand_punct['*'] = 1;
4862 mips_print_operand_punct['@'] = 1;
4863 mips_print_operand_punct['.'] = 1;
4864 mips_print_operand_punct['('] = 1;
4865 mips_print_operand_punct[')'] = 1;
4866 mips_print_operand_punct['['] = 1;
4867 mips_print_operand_punct[']'] = 1;
4868 mips_print_operand_punct['<'] = 1;
4869 mips_print_operand_punct['>'] = 1;
4870 mips_print_operand_punct['{'] = 1;
4871 mips_print_operand_punct['}'] = 1;
4872 mips_print_operand_punct['^'] = 1;
4873 mips_print_operand_punct['$'] = 1;
4874 mips_print_operand_punct['+'] = 1;
4875 mips_print_operand_punct['~'] = 1;
4877 mips_char_to_class['d'] = TARGET_MIPS16 ? M16_REGS : GR_REGS;
4878 mips_char_to_class['t'] = T_REG;
4879 mips_char_to_class['f'] = (TARGET_HARD_FLOAT ? FP_REGS : NO_REGS);
4880 mips_char_to_class['h'] = HI_REG;
4881 mips_char_to_class['l'] = LO_REG;
4882 mips_char_to_class['x'] = MD_REGS;
4883 mips_char_to_class['b'] = ALL_REGS;
4884 mips_char_to_class['c'] = (TARGET_ABICALLS ? PIC_FN_ADDR_REG :
4885 TARGET_MIPS16 ? M16_NA_REGS :
4886 GR_REGS);
4887 mips_char_to_class['e'] = LEA_REGS;
4888 mips_char_to_class['j'] = PIC_FN_ADDR_REG;
4889 mips_char_to_class['v'] = V1_REG;
4890 mips_char_to_class['y'] = GR_REGS;
4891 mips_char_to_class['z'] = ST_REGS;
4892 mips_char_to_class['B'] = COP0_REGS;
4893 mips_char_to_class['C'] = COP2_REGS;
4894 mips_char_to_class['D'] = COP3_REGS;
4895 mips_char_to_class['A'] = DSP_ACC_REGS;
4896 mips_char_to_class['a'] = ACC_REGS;
4898 /* Set up array to map GCC register number to debug register number.
4899 Ignore the special purpose register numbers. */
4901 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4902 mips_dbx_regno[i] = -1;
4904 start = GP_DBX_FIRST - GP_REG_FIRST;
4905 for (i = GP_REG_FIRST; i <= GP_REG_LAST; i++)
4906 mips_dbx_regno[i] = i + start;
4908 start = FP_DBX_FIRST - FP_REG_FIRST;
4909 for (i = FP_REG_FIRST; i <= FP_REG_LAST; i++)
4910 mips_dbx_regno[i] = i + start;
4912 mips_dbx_regno[HI_REGNUM] = MD_DBX_FIRST + 0;
4913 mips_dbx_regno[LO_REGNUM] = MD_DBX_FIRST + 1;
4915 /* Set up array giving whether a given register can hold a given mode. */
4917 for (mode = VOIDmode;
4918 mode != MAX_MACHINE_MODE;
4919 mode = (enum machine_mode) ((int)mode + 1))
4921 register int size = GET_MODE_SIZE (mode);
4922 register enum mode_class class = GET_MODE_CLASS (mode);
4924 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4926 register int temp;
4928 if (mode == CCV2mode)
4929 temp = (ISA_HAS_8CC
4930 && ST_REG_P (regno)
4931 && (regno - ST_REG_FIRST) % 2 == 0);
4933 else if (mode == CCV4mode)
4934 temp = (ISA_HAS_8CC
4935 && ST_REG_P (regno)
4936 && (regno - ST_REG_FIRST) % 4 == 0);
4938 else if (mode == CCmode)
4940 if (! ISA_HAS_8CC)
4941 temp = (regno == FPSW_REGNUM);
4942 else
4943 temp = (ST_REG_P (regno) || GP_REG_P (regno)
4944 || FP_REG_P (regno));
4947 else if (GP_REG_P (regno))
4948 temp = ((regno & 1) == 0 || size <= UNITS_PER_WORD);
4950 else if (FP_REG_P (regno))
4951 temp = ((regno % FP_INC) == 0)
4952 && (((class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT
4953 || class == MODE_VECTOR_FLOAT)
4954 && size <= UNITS_PER_FPVALUE)
4955 /* Allow integer modes that fit into a single
4956 register. We need to put integers into FPRs
4957 when using instructions like cvt and trunc.
4958 We can't allow sizes smaller than a word,
4959 the FPU has no appropriate load/store
4960 instructions for those. */
4961 || (class == MODE_INT
4962 && size >= MIN_UNITS_PER_WORD
4963 && size <= UNITS_PER_FPREG)
4964 /* Allow TFmode for CCmode reloads. */
4965 || (ISA_HAS_8CC && mode == TFmode));
4967 else if (ACC_REG_P (regno))
4968 temp = (INTEGRAL_MODE_P (mode)
4969 && (size <= UNITS_PER_WORD
4970 || (ACC_HI_REG_P (regno)
4971 && size == 2 * UNITS_PER_WORD)));
4973 else if (ALL_COP_REG_P (regno))
4974 temp = (class == MODE_INT && size <= UNITS_PER_WORD);
4975 else
4976 temp = 0;
4978 mips_hard_regno_mode_ok[(int)mode][regno] = temp;
4982 /* Save GPR registers in word_mode sized hunks. word_mode hasn't been
4983 initialized yet, so we can't use that here. */
4984 gpr_mode = TARGET_64BIT ? DImode : SImode;
4986 /* Provide default values for align_* for 64-bit targets. */
4987 if (TARGET_64BIT && !TARGET_MIPS16)
4989 if (align_loops == 0)
4990 align_loops = 8;
4991 if (align_jumps == 0)
4992 align_jumps = 8;
4993 if (align_functions == 0)
4994 align_functions = 8;
4997 /* Function to allocate machine-dependent function status. */
4998 init_machine_status = &mips_init_machine_status;
5000 if (ABI_HAS_64BIT_SYMBOLS)
5002 if (TARGET_EXPLICIT_RELOCS)
5004 mips_split_p[SYMBOL_64_HIGH] = true;
5005 mips_hi_relocs[SYMBOL_64_HIGH] = "%highest(";
5006 mips_lo_relocs[SYMBOL_64_HIGH] = "%higher(";
5008 mips_split_p[SYMBOL_64_MID] = true;
5009 mips_hi_relocs[SYMBOL_64_MID] = "%higher(";
5010 mips_lo_relocs[SYMBOL_64_MID] = "%hi(";
5012 mips_split_p[SYMBOL_64_LOW] = true;
5013 mips_hi_relocs[SYMBOL_64_LOW] = "%hi(";
5014 mips_lo_relocs[SYMBOL_64_LOW] = "%lo(";
5016 mips_split_p[SYMBOL_GENERAL] = true;
5017 mips_lo_relocs[SYMBOL_GENERAL] = "%lo(";
5020 else
5022 if (TARGET_EXPLICIT_RELOCS || mips_split_addresses)
5024 mips_split_p[SYMBOL_GENERAL] = true;
5025 mips_hi_relocs[SYMBOL_GENERAL] = "%hi(";
5026 mips_lo_relocs[SYMBOL_GENERAL] = "%lo(";
5030 if (TARGET_MIPS16)
5032 /* The high part is provided by a pseudo copy of $gp. */
5033 mips_split_p[SYMBOL_SMALL_DATA] = true;
5034 mips_lo_relocs[SYMBOL_SMALL_DATA] = "%gprel(";
5037 if (TARGET_EXPLICIT_RELOCS)
5039 /* Small data constants are kept whole until after reload,
5040 then lowered by mips_rewrite_small_data. */
5041 mips_lo_relocs[SYMBOL_SMALL_DATA] = "%gp_rel(";
5043 mips_split_p[SYMBOL_GOT_LOCAL] = true;
5044 if (TARGET_NEWABI)
5046 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got_page(";
5047 mips_lo_relocs[SYMBOL_GOT_LOCAL] = "%got_ofst(";
5049 else
5051 mips_lo_relocs[SYMBOL_GOTOFF_PAGE] = "%got(";
5052 mips_lo_relocs[SYMBOL_GOT_LOCAL] = "%lo(";
5055 if (TARGET_XGOT)
5057 /* The HIGH and LO_SUM are matched by special .md patterns. */
5058 mips_split_p[SYMBOL_GOT_GLOBAL] = true;
5060 mips_split_p[SYMBOL_GOTOFF_GLOBAL] = true;
5061 mips_hi_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got_hi(";
5062 mips_lo_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got_lo(";
5064 mips_split_p[SYMBOL_GOTOFF_CALL] = true;
5065 mips_hi_relocs[SYMBOL_GOTOFF_CALL] = "%call_hi(";
5066 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call_lo(";
5068 else
5070 if (TARGET_NEWABI)
5071 mips_lo_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got_disp(";
5072 else
5073 mips_lo_relocs[SYMBOL_GOTOFF_GLOBAL] = "%got(";
5074 mips_lo_relocs[SYMBOL_GOTOFF_CALL] = "%call16(";
5078 if (TARGET_NEWABI)
5080 mips_split_p[SYMBOL_GOTOFF_LOADGP] = true;
5081 mips_hi_relocs[SYMBOL_GOTOFF_LOADGP] = "%hi(%neg(%gp_rel(";
5082 mips_lo_relocs[SYMBOL_GOTOFF_LOADGP] = "%lo(%neg(%gp_rel(";
5085 /* Thread-local relocation operators. */
5086 mips_lo_relocs[SYMBOL_TLSGD] = "%tlsgd(";
5087 mips_lo_relocs[SYMBOL_TLSLDM] = "%tlsldm(";
5088 mips_split_p[SYMBOL_DTPREL] = 1;
5089 mips_hi_relocs[SYMBOL_DTPREL] = "%dtprel_hi(";
5090 mips_lo_relocs[SYMBOL_DTPREL] = "%dtprel_lo(";
5091 mips_lo_relocs[SYMBOL_GOTTPREL] = "%gottprel(";
5092 mips_split_p[SYMBOL_TPREL] = 1;
5093 mips_hi_relocs[SYMBOL_TPREL] = "%tprel_hi(";
5094 mips_lo_relocs[SYMBOL_TPREL] = "%tprel_lo(";
5096 /* We don't have a thread pointer access instruction on MIPS16, or
5097 appropriate TLS relocations. */
5098 if (TARGET_MIPS16)
5099 targetm.have_tls = false;
5101 /* Default to working around R4000 errata only if the processor
5102 was selected explicitly. */
5103 if ((target_flags_explicit & MASK_FIX_R4000) == 0
5104 && mips_matching_cpu_name_p (mips_arch_info->name, "r4000"))
5105 target_flags |= MASK_FIX_R4000;
5107 /* Default to working around R4400 errata only if the processor
5108 was selected explicitly. */
5109 if ((target_flags_explicit & MASK_FIX_R4400) == 0
5110 && mips_matching_cpu_name_p (mips_arch_info->name, "r4400"))
5111 target_flags |= MASK_FIX_R4400;
5114 /* Implement CONDITIONAL_REGISTER_USAGE. */
5116 void
5117 mips_conditional_register_usage (void)
5119 if (!TARGET_DSP)
5121 int regno;
5123 for (regno = DSP_ACC_REG_FIRST; regno <= DSP_ACC_REG_LAST; regno++)
5124 fixed_regs[regno] = call_used_regs[regno] = 1;
5126 if (!TARGET_HARD_FLOAT)
5128 int regno;
5130 for (regno = FP_REG_FIRST; regno <= FP_REG_LAST; regno++)
5131 fixed_regs[regno] = call_used_regs[regno] = 1;
5132 for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
5133 fixed_regs[regno] = call_used_regs[regno] = 1;
5135 else if (! ISA_HAS_8CC)
5137 int regno;
5139 /* We only have a single condition code register. We
5140 implement this by hiding all the condition code registers,
5141 and generating RTL that refers directly to ST_REG_FIRST. */
5142 for (regno = ST_REG_FIRST; regno <= ST_REG_LAST; regno++)
5143 fixed_regs[regno] = call_used_regs[regno] = 1;
5145 /* In mips16 mode, we permit the $t temporary registers to be used
5146 for reload. We prohibit the unused $s registers, since they
5147 are caller saved, and saving them via a mips16 register would
5148 probably waste more time than just reloading the value. */
5149 if (TARGET_MIPS16)
5151 fixed_regs[18] = call_used_regs[18] = 1;
5152 fixed_regs[19] = call_used_regs[19] = 1;
5153 fixed_regs[20] = call_used_regs[20] = 1;
5154 fixed_regs[21] = call_used_regs[21] = 1;
5155 fixed_regs[22] = call_used_regs[22] = 1;
5156 fixed_regs[23] = call_used_regs[23] = 1;
5157 fixed_regs[26] = call_used_regs[26] = 1;
5158 fixed_regs[27] = call_used_regs[27] = 1;
5159 fixed_regs[30] = call_used_regs[30] = 1;
5161 /* fp20-23 are now caller saved. */
5162 if (mips_abi == ABI_64)
5164 int regno;
5165 for (regno = FP_REG_FIRST + 20; regno < FP_REG_FIRST + 24; regno++)
5166 call_really_used_regs[regno] = call_used_regs[regno] = 1;
5168 /* Odd registers from fp21 to fp31 are now caller saved. */
5169 if (mips_abi == ABI_N32)
5171 int regno;
5172 for (regno = FP_REG_FIRST + 21; regno <= FP_REG_FIRST + 31; regno+=2)
5173 call_really_used_regs[regno] = call_used_regs[regno] = 1;
5177 /* Allocate a chunk of memory for per-function machine-dependent data. */
5178 static struct machine_function *
5179 mips_init_machine_status (void)
5181 return ((struct machine_function *)
5182 ggc_alloc_cleared (sizeof (struct machine_function)));
5185 /* On the mips16, we want to allocate $24 (T_REG) before other
5186 registers for instructions for which it is possible. This helps
5187 avoid shuffling registers around in order to set up for an xor,
5188 encouraging the compiler to use a cmp instead. */
5190 void
5191 mips_order_regs_for_local_alloc (void)
5193 register int i;
5195 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5196 reg_alloc_order[i] = i;
5198 if (TARGET_MIPS16)
5200 /* It really doesn't matter where we put register 0, since it is
5201 a fixed register anyhow. */
5202 reg_alloc_order[0] = 24;
5203 reg_alloc_order[24] = 0;
5208 /* The MIPS debug format wants all automatic variables and arguments
5209 to be in terms of the virtual frame pointer (stack pointer before
5210 any adjustment in the function), while the MIPS 3.0 linker wants
5211 the frame pointer to be the stack pointer after the initial
5212 adjustment. So, we do the adjustment here. The arg pointer (which
5213 is eliminated) points to the virtual frame pointer, while the frame
5214 pointer (which may be eliminated) points to the stack pointer after
5215 the initial adjustments. */
5217 HOST_WIDE_INT
5218 mips_debugger_offset (rtx addr, HOST_WIDE_INT offset)
5220 rtx offset2 = const0_rtx;
5221 rtx reg = eliminate_constant_term (addr, &offset2);
5223 if (offset == 0)
5224 offset = INTVAL (offset2);
5226 if (reg == stack_pointer_rtx || reg == frame_pointer_rtx
5227 || reg == hard_frame_pointer_rtx)
5229 HOST_WIDE_INT frame_size = (!cfun->machine->frame.initialized)
5230 ? compute_frame_size (get_frame_size ())
5231 : cfun->machine->frame.total_size;
5233 /* MIPS16 frame is smaller */
5234 if (frame_pointer_needed && TARGET_MIPS16)
5235 frame_size -= cfun->machine->frame.args_size;
5237 offset = offset - frame_size;
5240 /* sdbout_parms does not want this to crash for unrecognized cases. */
5241 #if 0
5242 else if (reg != arg_pointer_rtx)
5243 fatal_insn ("mips_debugger_offset called with non stack/frame/arg pointer",
5244 addr);
5245 #endif
5247 return offset;
5250 /* Implement the PRINT_OPERAND macro. The MIPS-specific operand codes are:
5252 'X' OP is CONST_INT, prints 32 bits in hexadecimal format = "0x%08x",
5253 'x' OP is CONST_INT, prints 16 bits in hexadecimal format = "0x%04x",
5254 'h' OP is HIGH, prints %hi(X),
5255 'd' output integer constant in decimal,
5256 'z' if the operand is 0, use $0 instead of normal operand.
5257 'D' print second part of double-word register or memory operand.
5258 'L' print low-order register of double-word register operand.
5259 'M' print high-order register of double-word register operand.
5260 'C' print part of opcode for a branch condition.
5261 'F' print part of opcode for a floating-point branch condition.
5262 'N' print part of opcode for a branch condition, inverted.
5263 'W' print part of opcode for a floating-point branch condition, inverted.
5264 'T' print 'f' for (eq:CC ...), 't' for (ne:CC ...),
5265 'z' for (eq:?I ...), 'n' for (ne:?I ...).
5266 't' like 'T', but with the EQ/NE cases reversed
5267 'Y' for a CONST_INT X, print mips_fp_conditions[X]
5268 'Z' print the operand and a comma for ISA_HAS_8CC, otherwise print nothing
5269 'R' print the reloc associated with LO_SUM
5270 'q' print DSP accumulator registers
5272 The punctuation characters are:
5274 '(' Turn on .set noreorder
5275 ')' Turn on .set reorder
5276 '[' Turn on .set noat
5277 ']' Turn on .set at
5278 '<' Turn on .set nomacro
5279 '>' Turn on .set macro
5280 '{' Turn on .set volatile (not GAS)
5281 '}' Turn on .set novolatile (not GAS)
5282 '&' Turn on .set noreorder if filling delay slots
5283 '*' Turn on both .set noreorder and .set nomacro if filling delay slots
5284 '!' Turn on .set nomacro if filling delay slots
5285 '#' Print nop if in a .set noreorder section.
5286 '/' Like '#', but does nothing within a delayed branch sequence
5287 '?' Print 'l' if we are to use a branch likely instead of normal branch.
5288 '@' Print the name of the assembler temporary register (at or $1).
5289 '.' Print the name of the register with a hard-wired zero (zero or $0).
5290 '^' Print the name of the pic call-through register (t9 or $25).
5291 '$' Print the name of the stack pointer register (sp or $29).
5292 '+' Print the name of the gp register (usually gp or $28).
5293 '~' Output a branch alignment to LABEL_ALIGN(NULL). */
5295 void
5296 print_operand (FILE *file, rtx op, int letter)
5298 register enum rtx_code code;
5300 if (PRINT_OPERAND_PUNCT_VALID_P (letter))
5302 switch (letter)
5304 case '?':
5305 if (mips_branch_likely)
5306 putc ('l', file);
5307 break;
5309 case '@':
5310 fputs (reg_names [GP_REG_FIRST + 1], file);
5311 break;
5313 case '^':
5314 fputs (reg_names [PIC_FUNCTION_ADDR_REGNUM], file);
5315 break;
5317 case '.':
5318 fputs (reg_names [GP_REG_FIRST + 0], file);
5319 break;
5321 case '$':
5322 fputs (reg_names[STACK_POINTER_REGNUM], file);
5323 break;
5325 case '+':
5326 fputs (reg_names[PIC_OFFSET_TABLE_REGNUM], file);
5327 break;
5329 case '&':
5330 if (final_sequence != 0 && set_noreorder++ == 0)
5331 fputs (".set\tnoreorder\n\t", file);
5332 break;
5334 case '*':
5335 if (final_sequence != 0)
5337 if (set_noreorder++ == 0)
5338 fputs (".set\tnoreorder\n\t", file);
5340 if (set_nomacro++ == 0)
5341 fputs (".set\tnomacro\n\t", file);
5343 break;
5345 case '!':
5346 if (final_sequence != 0 && set_nomacro++ == 0)
5347 fputs ("\n\t.set\tnomacro", file);
5348 break;
5350 case '#':
5351 if (set_noreorder != 0)
5352 fputs ("\n\tnop", file);
5353 break;
5355 case '/':
5356 /* Print an extra newline so that the delayed insn is separated
5357 from the following ones. This looks neater and is consistent
5358 with non-nop delayed sequences. */
5359 if (set_noreorder != 0 && final_sequence == 0)
5360 fputs ("\n\tnop\n", file);
5361 break;
5363 case '(':
5364 if (set_noreorder++ == 0)
5365 fputs (".set\tnoreorder\n\t", file);
5366 break;
5368 case ')':
5369 if (set_noreorder == 0)
5370 error ("internal error: %%) found without a %%( in assembler pattern");
5372 else if (--set_noreorder == 0)
5373 fputs ("\n\t.set\treorder", file);
5375 break;
5377 case '[':
5378 if (set_noat++ == 0)
5379 fputs (".set\tnoat\n\t", file);
5380 break;
5382 case ']':
5383 if (set_noat == 0)
5384 error ("internal error: %%] found without a %%[ in assembler pattern");
5385 else if (--set_noat == 0)
5386 fputs ("\n\t.set\tat", file);
5388 break;
5390 case '<':
5391 if (set_nomacro++ == 0)
5392 fputs (".set\tnomacro\n\t", file);
5393 break;
5395 case '>':
5396 if (set_nomacro == 0)
5397 error ("internal error: %%> found without a %%< in assembler pattern");
5398 else if (--set_nomacro == 0)
5399 fputs ("\n\t.set\tmacro", file);
5401 break;
5403 case '{':
5404 if (set_volatile++ == 0)
5405 fputs ("#.set\tvolatile\n\t", file);
5406 break;
5408 case '}':
5409 if (set_volatile == 0)
5410 error ("internal error: %%} found without a %%{ in assembler pattern");
5411 else if (--set_volatile == 0)
5412 fputs ("\n\t#.set\tnovolatile", file);
5414 break;
5416 case '~':
5418 if (align_labels_log > 0)
5419 ASM_OUTPUT_ALIGN (file, align_labels_log);
5421 break;
5423 default:
5424 error ("PRINT_OPERAND: unknown punctuation '%c'", letter);
5425 break;
5428 return;
5431 if (! op)
5433 error ("PRINT_OPERAND null pointer");
5434 return;
5437 code = GET_CODE (op);
5439 if (letter == 'C')
5440 switch (code)
5442 case EQ: fputs ("eq", file); break;
5443 case NE: fputs ("ne", file); break;
5444 case GT: fputs ("gt", file); break;
5445 case GE: fputs ("ge", file); break;
5446 case LT: fputs ("lt", file); break;
5447 case LE: fputs ("le", file); break;
5448 case GTU: fputs ("gtu", file); break;
5449 case GEU: fputs ("geu", file); break;
5450 case LTU: fputs ("ltu", file); break;
5451 case LEU: fputs ("leu", file); break;
5452 default:
5453 fatal_insn ("PRINT_OPERAND, invalid insn for %%C", op);
5456 else if (letter == 'N')
5457 switch (code)
5459 case EQ: fputs ("ne", file); break;
5460 case NE: fputs ("eq", file); break;
5461 case GT: fputs ("le", file); break;
5462 case GE: fputs ("lt", file); break;
5463 case LT: fputs ("ge", file); break;
5464 case LE: fputs ("gt", file); break;
5465 case GTU: fputs ("leu", file); break;
5466 case GEU: fputs ("ltu", file); break;
5467 case LTU: fputs ("geu", file); break;
5468 case LEU: fputs ("gtu", file); break;
5469 default:
5470 fatal_insn ("PRINT_OPERAND, invalid insn for %%N", op);
5473 else if (letter == 'F')
5474 switch (code)
5476 case EQ: fputs ("c1f", file); break;
5477 case NE: fputs ("c1t", file); break;
5478 default:
5479 fatal_insn ("PRINT_OPERAND, invalid insn for %%F", op);
5482 else if (letter == 'W')
5483 switch (code)
5485 case EQ: fputs ("c1t", file); break;
5486 case NE: fputs ("c1f", file); break;
5487 default:
5488 fatal_insn ("PRINT_OPERAND, invalid insn for %%W", op);
5491 else if (letter == 'h')
5493 if (GET_CODE (op) == HIGH)
5494 op = XEXP (op, 0);
5496 print_operand_reloc (file, op, mips_hi_relocs);
5499 else if (letter == 'R')
5500 print_operand_reloc (file, op, mips_lo_relocs);
5502 else if (letter == 'Y')
5504 if (GET_CODE (op) == CONST_INT
5505 && ((unsigned HOST_WIDE_INT) INTVAL (op)
5506 < ARRAY_SIZE (mips_fp_conditions)))
5507 fputs (mips_fp_conditions[INTVAL (op)], file);
5508 else
5509 output_operand_lossage ("invalid %%Y value");
5512 else if (letter == 'Z')
5514 if (ISA_HAS_8CC)
5516 print_operand (file, op, 0);
5517 fputc (',', file);
5521 else if (letter == 'q')
5523 int regnum;
5525 if (code != REG)
5526 fatal_insn ("PRINT_OPERAND, invalid insn for %%q", op);
5528 regnum = REGNO (op);
5529 if (MD_REG_P (regnum))
5530 fprintf (file, "$ac0");
5531 else if (DSP_ACC_REG_P (regnum))
5532 fprintf (file, "$ac%c", reg_names[regnum][3]);
5533 else
5534 fatal_insn ("PRINT_OPERAND, invalid insn for %%q", op);
5537 else if (code == REG || code == SUBREG)
5539 register int regnum;
5541 if (code == REG)
5542 regnum = REGNO (op);
5543 else
5544 regnum = true_regnum (op);
5546 if ((letter == 'M' && ! WORDS_BIG_ENDIAN)
5547 || (letter == 'L' && WORDS_BIG_ENDIAN)
5548 || letter == 'D')
5549 regnum++;
5551 fprintf (file, "%s", reg_names[regnum]);
5554 else if (code == MEM)
5556 if (letter == 'D')
5557 output_address (plus_constant (XEXP (op, 0), 4));
5558 else
5559 output_address (XEXP (op, 0));
5562 else if (letter == 'x' && GET_CODE (op) == CONST_INT)
5563 fprintf (file, HOST_WIDE_INT_PRINT_HEX, 0xffff & INTVAL(op));
5565 else if (letter == 'X' && GET_CODE(op) == CONST_INT)
5566 fprintf (file, HOST_WIDE_INT_PRINT_HEX, INTVAL (op));
5568 else if (letter == 'd' && GET_CODE(op) == CONST_INT)
5569 fprintf (file, HOST_WIDE_INT_PRINT_DEC, (INTVAL(op)));
5571 else if (letter == 'z' && op == CONST0_RTX (GET_MODE (op)))
5572 fputs (reg_names[GP_REG_FIRST], file);
5574 else if (letter == 'd' || letter == 'x' || letter == 'X')
5575 output_operand_lossage ("invalid use of %%d, %%x, or %%X");
5577 else if (letter == 'T' || letter == 't')
5579 int truth = (code == NE) == (letter == 'T');
5580 fputc ("zfnt"[truth * 2 + (GET_MODE (op) == CCmode)], file);
5583 else if (CONST_GP_P (op))
5584 fputs (reg_names[GLOBAL_POINTER_REGNUM], file);
5586 else
5587 output_addr_const (file, op);
5591 /* Print symbolic operand OP, which is part of a HIGH or LO_SUM.
5592 RELOCS is the array of relocations to use. */
5594 static void
5595 print_operand_reloc (FILE *file, rtx op, const char **relocs)
5597 enum mips_symbol_type symbol_type;
5598 const char *p;
5599 rtx base;
5600 HOST_WIDE_INT offset;
5602 symbol_type = mips_classify_symbolic_expression (op);
5603 if (relocs[symbol_type] == 0)
5604 fatal_insn ("PRINT_OPERAND, invalid operand for relocation", op);
5606 /* If OP uses an UNSPEC address, we want to print the inner symbol. */
5607 mips_split_const (op, &base, &offset);
5608 if (UNSPEC_ADDRESS_P (base))
5609 op = plus_constant (UNSPEC_ADDRESS (base), offset);
5611 fputs (relocs[symbol_type], file);
5612 output_addr_const (file, op);
5613 for (p = relocs[symbol_type]; *p != 0; p++)
5614 if (*p == '(')
5615 fputc (')', file);
5618 /* Output address operand X to FILE. */
5620 void
5621 print_operand_address (FILE *file, rtx x)
5623 struct mips_address_info addr;
5625 if (mips_classify_address (&addr, x, word_mode, true))
5626 switch (addr.type)
5628 case ADDRESS_REG:
5629 print_operand (file, addr.offset, 0);
5630 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
5631 return;
5633 case ADDRESS_LO_SUM:
5634 print_operand (file, addr.offset, 'R');
5635 fprintf (file, "(%s)", reg_names[REGNO (addr.reg)]);
5636 return;
5638 case ADDRESS_CONST_INT:
5639 output_addr_const (file, x);
5640 fprintf (file, "(%s)", reg_names[0]);
5641 return;
5643 case ADDRESS_SYMBOLIC:
5644 output_addr_const (file, x);
5645 return;
5647 gcc_unreachable ();
5650 /* When using assembler macros, keep track of all of small-data externs
5651 so that mips_file_end can emit the appropriate declarations for them.
5653 In most cases it would be safe (though pointless) to emit .externs
5654 for other symbols too. One exception is when an object is within
5655 the -G limit but declared by the user to be in a section other
5656 than .sbss or .sdata. */
5659 mips_output_external (FILE *file ATTRIBUTE_UNUSED, tree decl, const char *name)
5661 register struct extern_list *p;
5663 if (!TARGET_EXPLICIT_RELOCS && mips_in_small_data_p (decl))
5665 p = (struct extern_list *) ggc_alloc (sizeof (struct extern_list));
5666 p->next = extern_head;
5667 p->name = name;
5668 p->size = int_size_in_bytes (TREE_TYPE (decl));
5669 extern_head = p;
5672 if (TARGET_IRIX && mips_abi == ABI_32 && TREE_CODE (decl) == FUNCTION_DECL)
5674 p = (struct extern_list *) ggc_alloc (sizeof (struct extern_list));
5675 p->next = extern_head;
5676 p->name = name;
5677 p->size = -1;
5678 extern_head = p;
5681 return 0;
5684 #if TARGET_IRIX
5685 static void
5686 irix_output_external_libcall (rtx fun)
5688 register struct extern_list *p;
5690 if (mips_abi == ABI_32)
5692 p = (struct extern_list *) ggc_alloc (sizeof (struct extern_list));
5693 p->next = extern_head;
5694 p->name = XSTR (fun, 0);
5695 p->size = -1;
5696 extern_head = p;
5699 #endif
5701 /* Emit a new filename to a stream. If we are smuggling stabs, try to
5702 put out a MIPS ECOFF file and a stab. */
5704 void
5705 mips_output_filename (FILE *stream, const char *name)
5708 /* If we are emitting DWARF-2, let dwarf2out handle the ".file"
5709 directives. */
5710 if (write_symbols == DWARF2_DEBUG)
5711 return;
5712 else if (mips_output_filename_first_time)
5714 mips_output_filename_first_time = 0;
5715 num_source_filenames += 1;
5716 current_function_file = name;
5717 fprintf (stream, "\t.file\t%d ", num_source_filenames);
5718 output_quoted_string (stream, name);
5719 putc ('\n', stream);
5722 /* If we are emitting stabs, let dbxout.c handle this (except for
5723 the mips_output_filename_first_time case). */
5724 else if (write_symbols == DBX_DEBUG)
5725 return;
5727 else if (name != current_function_file
5728 && strcmp (name, current_function_file) != 0)
5730 num_source_filenames += 1;
5731 current_function_file = name;
5732 fprintf (stream, "\t.file\t%d ", num_source_filenames);
5733 output_quoted_string (stream, name);
5734 putc ('\n', stream);
5738 /* Output an ASCII string, in a space-saving way. PREFIX is the string
5739 that should be written before the opening quote, such as "\t.ascii\t"
5740 for real string data or "\t# " for a comment. */
5742 void
5743 mips_output_ascii (FILE *stream, const char *string_param, size_t len,
5744 const char *prefix)
5746 size_t i;
5747 int cur_pos = 17;
5748 register const unsigned char *string =
5749 (const unsigned char *)string_param;
5751 fprintf (stream, "%s\"", prefix);
5752 for (i = 0; i < len; i++)
5754 register int c = string[i];
5756 if (ISPRINT (c))
5758 if (c == '\\' || c == '\"')
5760 putc ('\\', stream);
5761 cur_pos++;
5763 putc (c, stream);
5764 cur_pos++;
5766 else
5768 fprintf (stream, "\\%03o", c);
5769 cur_pos += 4;
5772 if (cur_pos > 72 && i+1 < len)
5774 cur_pos = 17;
5775 fprintf (stream, "\"\n%s\"", prefix);
5778 fprintf (stream, "\"\n");
5781 /* Implement TARGET_ASM_FILE_START. */
5783 static void
5784 mips_file_start (void)
5786 default_file_start ();
5788 if (!TARGET_IRIX)
5790 /* Generate a special section to describe the ABI switches used to
5791 produce the resultant binary. This used to be done by the assembler
5792 setting bits in the ELF header's flags field, but we have run out of
5793 bits. GDB needs this information in order to be able to correctly
5794 debug these binaries. See the function mips_gdbarch_init() in
5795 gdb/mips-tdep.c. This is unnecessary for the IRIX 5/6 ABIs and
5796 causes unnecessary IRIX 6 ld warnings. */
5797 const char * abi_string = NULL;
5799 switch (mips_abi)
5801 case ABI_32: abi_string = "abi32"; break;
5802 case ABI_N32: abi_string = "abiN32"; break;
5803 case ABI_64: abi_string = "abi64"; break;
5804 case ABI_O64: abi_string = "abiO64"; break;
5805 case ABI_EABI: abi_string = TARGET_64BIT ? "eabi64" : "eabi32"; break;
5806 default:
5807 gcc_unreachable ();
5809 /* Note - we use fprintf directly rather than called named_section()
5810 because in this way we can avoid creating an allocated section. We
5811 do not want this section to take up any space in the running
5812 executable. */
5813 fprintf (asm_out_file, "\t.section .mdebug.%s\n", abi_string);
5815 /* There is no ELF header flag to distinguish long32 forms of the
5816 EABI from long64 forms. Emit a special section to help tools
5817 such as GDB. */
5818 if (mips_abi == ABI_EABI)
5819 fprintf (asm_out_file, "\t.section .gcc_compiled_long%d\n",
5820 TARGET_LONG64 ? 64 : 32);
5822 /* Restore the default section. */
5823 fprintf (asm_out_file, "\t.previous\n");
5826 /* Generate the pseudo ops that System V.4 wants. */
5827 if (TARGET_ABICALLS)
5828 /* ??? but do not want this (or want pic0) if -non-shared? */
5829 fprintf (asm_out_file, "\t.abicalls\n");
5831 if (TARGET_MIPS16)
5832 fprintf (asm_out_file, "\t.set\tmips16\n");
5834 if (flag_verbose_asm)
5835 fprintf (asm_out_file, "\n%s -G value = %d, Arch = %s, ISA = %d\n",
5836 ASM_COMMENT_START,
5837 mips_section_threshold, mips_arch_info->name, mips_isa);
5840 #ifdef BSS_SECTION_ASM_OP
5841 /* Implement ASM_OUTPUT_ALIGNED_BSS. This differs from the default only
5842 in the use of sbss. */
5844 void
5845 mips_output_aligned_bss (FILE *stream, tree decl, const char *name,
5846 unsigned HOST_WIDE_INT size, int align)
5848 extern tree last_assemble_variable_decl;
5850 if (mips_in_small_data_p (decl))
5851 named_section (0, ".sbss", 0);
5852 else
5853 bss_section ();
5854 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
5855 last_assemble_variable_decl = decl;
5856 ASM_DECLARE_OBJECT_NAME (stream, name, decl);
5857 ASM_OUTPUT_SKIP (stream, size != 0 ? size : 1);
5859 #endif
5861 /* Implement TARGET_ASM_FILE_END. When using assembler macros, emit
5862 .externs for any small-data variables that turned out to be external. */
5864 static void
5865 mips_file_end (void)
5867 tree name_tree;
5868 struct extern_list *p;
5870 if (extern_head)
5872 fputs ("\n", asm_out_file);
5874 for (p = extern_head; p != 0; p = p->next)
5876 name_tree = get_identifier (p->name);
5878 /* Positively ensure only one .extern for any given symbol. */
5879 if (!TREE_ASM_WRITTEN (name_tree)
5880 && TREE_SYMBOL_REFERENCED (name_tree))
5882 TREE_ASM_WRITTEN (name_tree) = 1;
5883 /* In IRIX 5 or IRIX 6 for the O32 ABI, we must output a
5884 `.global name .text' directive for every used but
5885 undefined function. If we don't, the linker may perform
5886 an optimization (skipping over the insns that set $gp)
5887 when it is unsafe. */
5888 if (TARGET_IRIX && mips_abi == ABI_32 && p->size == -1)
5890 fputs ("\t.globl ", asm_out_file);
5891 assemble_name (asm_out_file, p->name);
5892 fputs (" .text\n", asm_out_file);
5894 else
5896 fputs ("\t.extern\t", asm_out_file);
5897 assemble_name (asm_out_file, p->name);
5898 fprintf (asm_out_file, ", %d\n", p->size);
5905 /* Implement ASM_OUTPUT_ALIGNED_DECL_COMMON. This is usually the same as the
5906 elfos.h version, but we also need to handle -muninit-const-in-rodata. */
5908 void
5909 mips_output_aligned_decl_common (FILE *stream, tree decl, const char *name,
5910 unsigned HOST_WIDE_INT size,
5911 unsigned int align)
5913 /* If the target wants uninitialized const declarations in
5914 .rdata then don't put them in .comm. */
5915 if (TARGET_EMBEDDED_DATA && TARGET_UNINIT_CONST_IN_RODATA
5916 && TREE_CODE (decl) == VAR_DECL && TREE_READONLY (decl)
5917 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5919 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
5920 targetm.asm_out.globalize_label (stream, name);
5922 readonly_data_section ();
5923 ASM_OUTPUT_ALIGN (stream, floor_log2 (align / BITS_PER_UNIT));
5924 mips_declare_object (stream, name, "",
5925 ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED "\n",
5926 size);
5928 else
5929 mips_declare_common_object (stream, name, "\n\t.comm\t",
5930 size, align, true);
5933 /* Declare a common object of SIZE bytes using asm directive INIT_STRING.
5934 NAME is the name of the object and ALIGN is the required alignment
5935 in bytes. TAKES_ALIGNMENT_P is true if the directive takes a third
5936 alignment argument. */
5938 void
5939 mips_declare_common_object (FILE *stream, const char *name,
5940 const char *init_string,
5941 unsigned HOST_WIDE_INT size,
5942 unsigned int align, bool takes_alignment_p)
5944 if (!takes_alignment_p)
5946 size += (align / BITS_PER_UNIT) - 1;
5947 size -= size % (align / BITS_PER_UNIT);
5948 mips_declare_object (stream, name, init_string,
5949 "," HOST_WIDE_INT_PRINT_UNSIGNED "\n", size);
5951 else
5952 mips_declare_object (stream, name, init_string,
5953 "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
5954 size, align / BITS_PER_UNIT);
5957 /* Emit either a label, .comm, or .lcomm directive. When using assembler
5958 macros, mark the symbol as written so that mips_file_end won't emit an
5959 .extern for it. STREAM is the output file, NAME is the name of the
5960 symbol, INIT_STRING is the string that should be written before the
5961 symbol and FINAL_STRING is the string that should be written after it.
5962 FINAL_STRING is a printf() format that consumes the remaining arguments. */
5964 void
5965 mips_declare_object (FILE *stream, const char *name, const char *init_string,
5966 const char *final_string, ...)
5968 va_list ap;
5970 fputs (init_string, stream);
5971 assemble_name (stream, name);
5972 va_start (ap, final_string);
5973 vfprintf (stream, final_string, ap);
5974 va_end (ap);
5976 if (!TARGET_EXPLICIT_RELOCS)
5978 tree name_tree = get_identifier (name);
5979 TREE_ASM_WRITTEN (name_tree) = 1;
5983 #ifdef ASM_OUTPUT_SIZE_DIRECTIVE
5984 extern int size_directive_output;
5986 /* Implement ASM_DECLARE_OBJECT_NAME. This is like most of the standard ELF
5987 definitions except that it uses mips_declare_object() to emit the label. */
5989 void
5990 mips_declare_object_name (FILE *stream, const char *name,
5991 tree decl ATTRIBUTE_UNUSED)
5993 #ifdef ASM_OUTPUT_TYPE_DIRECTIVE
5994 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
5995 #endif
5997 size_directive_output = 0;
5998 if (!flag_inhibit_size_directive && DECL_SIZE (decl))
6000 HOST_WIDE_INT size;
6002 size_directive_output = 1;
6003 size = int_size_in_bytes (TREE_TYPE (decl));
6004 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
6007 mips_declare_object (stream, name, "", ":\n");
6010 /* Implement ASM_FINISH_DECLARE_OBJECT. This is generic ELF stuff. */
6012 void
6013 mips_finish_declare_object (FILE *stream, tree decl, int top_level, int at_end)
6015 const char *name;
6017 name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6018 if (!flag_inhibit_size_directive
6019 && DECL_SIZE (decl) != 0
6020 && !at_end && top_level
6021 && DECL_INITIAL (decl) == error_mark_node
6022 && !size_directive_output)
6024 HOST_WIDE_INT size;
6026 size_directive_output = 1;
6027 size = int_size_in_bytes (TREE_TYPE (decl));
6028 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
6031 #endif
6033 /* Return true if X is a small data address that can be rewritten
6034 as a LO_SUM. */
6036 static bool
6037 mips_rewrite_small_data_p (rtx x)
6039 enum mips_symbol_type symbol_type;
6041 return (TARGET_EXPLICIT_RELOCS
6042 && mips_symbolic_constant_p (x, &symbol_type)
6043 && symbol_type == SYMBOL_SMALL_DATA);
6047 /* A for_each_rtx callback for mips_small_data_pattern_p. */
6049 static int
6050 mips_small_data_pattern_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
6052 if (GET_CODE (*loc) == LO_SUM)
6053 return -1;
6055 return mips_rewrite_small_data_p (*loc);
6058 /* Return true if OP refers to small data symbols directly, not through
6059 a LO_SUM. */
6061 bool
6062 mips_small_data_pattern_p (rtx op)
6064 return for_each_rtx (&op, mips_small_data_pattern_1, 0);
6067 /* A for_each_rtx callback, used by mips_rewrite_small_data. */
6069 static int
6070 mips_rewrite_small_data_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
6072 if (mips_rewrite_small_data_p (*loc))
6073 *loc = gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, *loc);
6075 if (GET_CODE (*loc) == LO_SUM)
6076 return -1;
6078 return 0;
6081 /* If possible, rewrite OP so that it refers to small data using
6082 explicit relocations. */
6085 mips_rewrite_small_data (rtx op)
6087 op = copy_insn (op);
6088 for_each_rtx (&op, mips_rewrite_small_data_1, 0);
6089 return op;
6092 /* Return true if the current function has an insn that implicitly
6093 refers to $gp. */
6095 static bool
6096 mips_function_has_gp_insn (void)
6098 /* Don't bother rechecking if we found one last time. */
6099 if (!cfun->machine->has_gp_insn_p)
6101 rtx insn;
6103 push_topmost_sequence ();
6104 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6105 if (INSN_P (insn)
6106 && GET_CODE (PATTERN (insn)) != USE
6107 && GET_CODE (PATTERN (insn)) != CLOBBER
6108 && (get_attr_got (insn) != GOT_UNSET
6109 || small_data_pattern (PATTERN (insn), VOIDmode)))
6110 break;
6111 pop_topmost_sequence ();
6113 cfun->machine->has_gp_insn_p = (insn != 0);
6115 return cfun->machine->has_gp_insn_p;
6119 /* Return the register that should be used as the global pointer
6120 within this function. Return 0 if the function doesn't need
6121 a global pointer. */
6123 static unsigned int
6124 mips_global_pointer (void)
6126 unsigned int regno;
6128 /* $gp is always available in non-abicalls code. */
6129 if (!TARGET_ABICALLS)
6130 return GLOBAL_POINTER_REGNUM;
6132 /* We must always provide $gp when it is used implicitly. */
6133 if (!TARGET_EXPLICIT_RELOCS)
6134 return GLOBAL_POINTER_REGNUM;
6136 /* FUNCTION_PROFILER includes a jal macro, so we need to give it
6137 a valid gp. */
6138 if (current_function_profile)
6139 return GLOBAL_POINTER_REGNUM;
6141 /* If the function has a nonlocal goto, $gp must hold the correct
6142 global pointer for the target function. */
6143 if (current_function_has_nonlocal_goto)
6144 return GLOBAL_POINTER_REGNUM;
6146 /* If the gp is never referenced, there's no need to initialize it.
6147 Note that reload can sometimes introduce constant pool references
6148 into a function that otherwise didn't need them. For example,
6149 suppose we have an instruction like:
6151 (set (reg:DF R1) (float:DF (reg:SI R2)))
6153 If R2 turns out to be constant such as 1, the instruction may have a
6154 REG_EQUAL note saying that R1 == 1.0. Reload then has the option of
6155 using this constant if R2 doesn't get allocated to a register.
6157 In cases like these, reload will have added the constant to the pool
6158 but no instruction will yet refer to it. */
6159 if (!regs_ever_live[GLOBAL_POINTER_REGNUM]
6160 && !current_function_uses_const_pool
6161 && !mips_function_has_gp_insn ())
6162 return 0;
6164 /* We need a global pointer, but perhaps we can use a call-clobbered
6165 register instead of $gp. */
6166 if (TARGET_NEWABI && current_function_is_leaf)
6167 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
6168 if (!regs_ever_live[regno]
6169 && call_used_regs[regno]
6170 && !fixed_regs[regno]
6171 && regno != PIC_FUNCTION_ADDR_REGNUM)
6172 return regno;
6174 return GLOBAL_POINTER_REGNUM;
6178 /* Return true if the current function must save REGNO. */
6180 static bool
6181 mips_save_reg_p (unsigned int regno)
6183 /* We only need to save $gp for NewABI PIC. */
6184 if (regno == GLOBAL_POINTER_REGNUM)
6185 return (TARGET_ABICALLS && TARGET_NEWABI
6186 && cfun->machine->global_pointer == regno);
6188 /* Check call-saved registers. */
6189 if (regs_ever_live[regno] && !call_used_regs[regno])
6190 return true;
6192 /* We need to save the old frame pointer before setting up a new one. */
6193 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
6194 return true;
6196 /* We need to save the incoming return address if it is ever clobbered
6197 within the function. */
6198 if (regno == GP_REG_FIRST + 31 && regs_ever_live[regno])
6199 return true;
6201 if (TARGET_MIPS16)
6203 tree return_type;
6205 return_type = DECL_RESULT (current_function_decl);
6207 /* $18 is a special case in mips16 code. It may be used to call
6208 a function which returns a floating point value, but it is
6209 marked in call_used_regs. */
6210 if (regno == GP_REG_FIRST + 18 && regs_ever_live[regno])
6211 return true;
6213 /* $31 is also a special case. It will be used to copy a return
6214 value into the floating point registers if the return value is
6215 floating point. */
6216 if (regno == GP_REG_FIRST + 31
6217 && mips16_hard_float
6218 && !aggregate_value_p (return_type, current_function_decl)
6219 && GET_MODE_CLASS (DECL_MODE (return_type)) == MODE_FLOAT
6220 && GET_MODE_SIZE (DECL_MODE (return_type)) <= UNITS_PER_FPVALUE)
6221 return true;
6224 return false;
6228 /* Return the bytes needed to compute the frame pointer from the current
6229 stack pointer. SIZE is the size (in bytes) of the local variables.
6231 MIPS stack frames look like:
6233 Before call After call
6234 +-----------------------+ +-----------------------+
6235 high | | | |
6236 mem. | | | |
6237 | caller's temps. | | caller's temps. |
6238 | | | |
6239 +-----------------------+ +-----------------------+
6240 | | | |
6241 | arguments on stack. | | arguments on stack. |
6242 | | | |
6243 +-----------------------+ +-----------------------+
6244 | 4 words to save | | 4 words to save |
6245 | arguments passed | | arguments passed |
6246 | in registers, even | | in registers, even |
6247 SP->| if not passed. | VFP->| if not passed. |
6248 +-----------------------+ +-----------------------+
6250 | fp register save |
6252 +-----------------------+
6254 | gp register save |
6256 +-----------------------+
6258 | local variables |
6260 +-----------------------+
6262 | alloca allocations |
6264 +-----------------------+
6266 | GP save for V.4 abi |
6268 +-----------------------+
6270 | arguments on stack |
6272 +-----------------------+
6273 | 4 words to save |
6274 | arguments passed |
6275 | in registers, even |
6276 low SP->| if not passed. |
6277 memory +-----------------------+
6281 HOST_WIDE_INT
6282 compute_frame_size (HOST_WIDE_INT size)
6284 unsigned int regno;
6285 HOST_WIDE_INT total_size; /* # bytes that the entire frame takes up */
6286 HOST_WIDE_INT var_size; /* # bytes that variables take up */
6287 HOST_WIDE_INT args_size; /* # bytes that outgoing arguments take up */
6288 HOST_WIDE_INT cprestore_size; /* # bytes that the cprestore slot takes up */
6289 HOST_WIDE_INT gp_reg_rounded; /* # bytes needed to store gp after rounding */
6290 HOST_WIDE_INT gp_reg_size; /* # bytes needed to store gp regs */
6291 HOST_WIDE_INT fp_reg_size; /* # bytes needed to store fp regs */
6292 unsigned int mask; /* mask of saved gp registers */
6293 unsigned int fmask; /* mask of saved fp registers */
6295 cfun->machine->global_pointer = mips_global_pointer ();
6297 gp_reg_size = 0;
6298 fp_reg_size = 0;
6299 mask = 0;
6300 fmask = 0;
6301 var_size = MIPS_STACK_ALIGN (size);
6302 args_size = current_function_outgoing_args_size;
6303 cprestore_size = MIPS_STACK_ALIGN (STARTING_FRAME_OFFSET) - args_size;
6305 /* The space set aside by STARTING_FRAME_OFFSET isn't needed in leaf
6306 functions. If the function has local variables, we're committed
6307 to allocating it anyway. Otherwise reclaim it here. */
6308 if (var_size == 0 && current_function_is_leaf)
6309 cprestore_size = args_size = 0;
6311 /* The MIPS 3.0 linker does not like functions that dynamically
6312 allocate the stack and have 0 for STACK_DYNAMIC_OFFSET, since it
6313 looks like we are trying to create a second frame pointer to the
6314 function, so allocate some stack space to make it happy. */
6316 if (args_size == 0 && current_function_calls_alloca)
6317 args_size = 4 * UNITS_PER_WORD;
6319 total_size = var_size + args_size + cprestore_size;
6321 /* Calculate space needed for gp registers. */
6322 for (regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++)
6323 if (mips_save_reg_p (regno))
6325 gp_reg_size += GET_MODE_SIZE (gpr_mode);
6326 mask |= 1 << (regno - GP_REG_FIRST);
6329 /* We need to restore these for the handler. */
6330 if (current_function_calls_eh_return)
6332 unsigned int i;
6333 for (i = 0; ; ++i)
6335 regno = EH_RETURN_DATA_REGNO (i);
6336 if (regno == INVALID_REGNUM)
6337 break;
6338 gp_reg_size += GET_MODE_SIZE (gpr_mode);
6339 mask |= 1 << (regno - GP_REG_FIRST);
6343 /* This loop must iterate over the same space as its companion in
6344 save_restore_insns. */
6345 for (regno = (FP_REG_LAST - FP_INC + 1);
6346 regno >= FP_REG_FIRST;
6347 regno -= FP_INC)
6349 if (mips_save_reg_p (regno))
6351 fp_reg_size += FP_INC * UNITS_PER_FPREG;
6352 fmask |= ((1L << FP_INC) - 1) << (regno - FP_REG_FIRST);
6356 gp_reg_rounded = MIPS_STACK_ALIGN (gp_reg_size);
6357 total_size += gp_reg_rounded + MIPS_STACK_ALIGN (fp_reg_size);
6359 /* Add in the space required for saving incoming register arguments. */
6360 total_size += current_function_pretend_args_size;
6361 total_size += MIPS_STACK_ALIGN (cfun->machine->varargs_size);
6363 /* Save other computed information. */
6364 cfun->machine->frame.total_size = total_size;
6365 cfun->machine->frame.var_size = var_size;
6366 cfun->machine->frame.args_size = args_size;
6367 cfun->machine->frame.cprestore_size = cprestore_size;
6368 cfun->machine->frame.gp_reg_size = gp_reg_size;
6369 cfun->machine->frame.fp_reg_size = fp_reg_size;
6370 cfun->machine->frame.mask = mask;
6371 cfun->machine->frame.fmask = fmask;
6372 cfun->machine->frame.initialized = reload_completed;
6373 cfun->machine->frame.num_gp = gp_reg_size / UNITS_PER_WORD;
6374 cfun->machine->frame.num_fp = fp_reg_size / (FP_INC * UNITS_PER_FPREG);
6376 if (mask)
6378 HOST_WIDE_INT offset;
6380 offset = (args_size + cprestore_size + var_size
6381 + gp_reg_size - GET_MODE_SIZE (gpr_mode));
6382 cfun->machine->frame.gp_sp_offset = offset;
6383 cfun->machine->frame.gp_save_offset = offset - total_size;
6385 else
6387 cfun->machine->frame.gp_sp_offset = 0;
6388 cfun->machine->frame.gp_save_offset = 0;
6391 if (fmask)
6393 HOST_WIDE_INT offset;
6395 offset = (args_size + cprestore_size + var_size
6396 + gp_reg_rounded + fp_reg_size
6397 - FP_INC * UNITS_PER_FPREG);
6398 cfun->machine->frame.fp_sp_offset = offset;
6399 cfun->machine->frame.fp_save_offset = offset - total_size;
6401 else
6403 cfun->machine->frame.fp_sp_offset = 0;
6404 cfun->machine->frame.fp_save_offset = 0;
6407 /* Ok, we're done. */
6408 return total_size;
6411 /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame
6412 pointer or argument pointer. TO is either the stack pointer or
6413 hard frame pointer. */
6415 HOST_WIDE_INT
6416 mips_initial_elimination_offset (int from, int to)
6418 HOST_WIDE_INT offset;
6420 compute_frame_size (get_frame_size ());
6422 /* Set OFFSET to the offset from the stack pointer. */
6423 switch (from)
6425 case FRAME_POINTER_REGNUM:
6426 offset = 0;
6427 break;
6429 case ARG_POINTER_REGNUM:
6430 offset = (cfun->machine->frame.total_size
6431 - current_function_pretend_args_size);
6432 break;
6434 default:
6435 gcc_unreachable ();
6438 if (TARGET_MIPS16 && to == HARD_FRAME_POINTER_REGNUM)
6439 offset -= cfun->machine->frame.args_size;
6441 return offset;
6444 /* Implement RETURN_ADDR_RTX. Note, we do not support moving
6445 back to a previous frame. */
6447 mips_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
6449 if (count != 0)
6450 return const0_rtx;
6452 return get_hard_reg_initial_val (Pmode, GP_REG_FIRST + 31);
6455 /* Use FN to save or restore register REGNO. MODE is the register's
6456 mode and OFFSET is the offset of its save slot from the current
6457 stack pointer. */
6459 static void
6460 mips_save_restore_reg (enum machine_mode mode, int regno,
6461 HOST_WIDE_INT offset, mips_save_restore_fn fn)
6463 rtx mem;
6465 mem = gen_rtx_MEM (mode, plus_constant (stack_pointer_rtx, offset));
6467 fn (gen_rtx_REG (mode, regno), mem);
6471 /* Call FN for each register that is saved by the current function.
6472 SP_OFFSET is the offset of the current stack pointer from the start
6473 of the frame. */
6475 static void
6476 mips_for_each_saved_reg (HOST_WIDE_INT sp_offset, mips_save_restore_fn fn)
6478 #define BITSET_P(VALUE, BIT) (((VALUE) & (1L << (BIT))) != 0)
6480 enum machine_mode fpr_mode;
6481 HOST_WIDE_INT offset;
6482 int regno;
6484 /* Save registers starting from high to low. The debuggers prefer at least
6485 the return register be stored at func+4, and also it allows us not to
6486 need a nop in the epilog if at least one register is reloaded in
6487 addition to return address. */
6488 offset = cfun->machine->frame.gp_sp_offset - sp_offset;
6489 for (regno = GP_REG_LAST; regno >= GP_REG_FIRST; regno--)
6490 if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST))
6492 mips_save_restore_reg (gpr_mode, regno, offset, fn);
6493 offset -= GET_MODE_SIZE (gpr_mode);
6496 /* This loop must iterate over the same space as its companion in
6497 compute_frame_size. */
6498 offset = cfun->machine->frame.fp_sp_offset - sp_offset;
6499 fpr_mode = (TARGET_SINGLE_FLOAT ? SFmode : DFmode);
6500 for (regno = (FP_REG_LAST - FP_INC + 1);
6501 regno >= FP_REG_FIRST;
6502 regno -= FP_INC)
6503 if (BITSET_P (cfun->machine->frame.fmask, regno - FP_REG_FIRST))
6505 mips_save_restore_reg (fpr_mode, regno, offset, fn);
6506 offset -= GET_MODE_SIZE (fpr_mode);
6508 #undef BITSET_P
6511 /* If we're generating n32 or n64 abicalls, and the current function
6512 does not use $28 as its global pointer, emit a cplocal directive.
6513 Use pic_offset_table_rtx as the argument to the directive. */
6515 static void
6516 mips_output_cplocal (void)
6518 if (!TARGET_EXPLICIT_RELOCS
6519 && cfun->machine->global_pointer > 0
6520 && cfun->machine->global_pointer != GLOBAL_POINTER_REGNUM)
6521 output_asm_insn (".cplocal %+", 0);
6524 /* If we're generating n32 or n64 abicalls, emit instructions
6525 to set up the global pointer. */
6527 static void
6528 mips_emit_loadgp (void)
6530 if (TARGET_ABICALLS && TARGET_NEWABI && cfun->machine->global_pointer > 0)
6532 rtx addr, offset, incoming_address;
6534 addr = XEXP (DECL_RTL (current_function_decl), 0);
6535 offset = mips_unspec_address (addr, SYMBOL_GOTOFF_LOADGP);
6536 incoming_address = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
6537 emit_insn (gen_loadgp (offset, incoming_address));
6538 if (!TARGET_EXPLICIT_RELOCS || current_function_profile)
6539 emit_insn (gen_loadgp_blockage ());
6543 /* Set up the stack and frame (if desired) for the function. */
6545 static void
6546 mips_output_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6548 const char *fnname;
6549 HOST_WIDE_INT tsize = cfun->machine->frame.total_size;
6551 #ifdef SDB_DEBUGGING_INFO
6552 if (debug_info_level != DINFO_LEVEL_TERSE && write_symbols == SDB_DEBUG)
6553 SDB_OUTPUT_SOURCE_LINE (file, DECL_SOURCE_LINE (current_function_decl));
6554 #endif
6556 /* In mips16 mode, we may need to generate a 32 bit to handle
6557 floating point arguments. The linker will arrange for any 32 bit
6558 functions to call this stub, which will then jump to the 16 bit
6559 function proper. */
6560 if (TARGET_MIPS16 && !TARGET_SOFT_FLOAT
6561 && current_function_args_info.fp_code != 0)
6562 build_mips16_function_stub (file);
6564 if (!FUNCTION_NAME_ALREADY_DECLARED)
6566 /* Get the function name the same way that toplev.c does before calling
6567 assemble_start_function. This is needed so that the name used here
6568 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
6569 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
6571 if (!flag_inhibit_size_directive)
6573 fputs ("\t.ent\t", file);
6574 assemble_name (file, fnname);
6575 fputs ("\n", file);
6578 assemble_name (file, fnname);
6579 fputs (":\n", file);
6582 /* Stop mips_file_end from treating this function as external. */
6583 if (TARGET_IRIX && mips_abi == ABI_32)
6584 TREE_ASM_WRITTEN (DECL_NAME (cfun->decl)) = 1;
6586 if (!flag_inhibit_size_directive)
6588 /* .frame FRAMEREG, FRAMESIZE, RETREG */
6589 fprintf (file,
6590 "\t.frame\t%s," HOST_WIDE_INT_PRINT_DEC ",%s\t\t"
6591 "# vars= " HOST_WIDE_INT_PRINT_DEC ", regs= %d/%d"
6592 ", args= " HOST_WIDE_INT_PRINT_DEC
6593 ", gp= " HOST_WIDE_INT_PRINT_DEC "\n",
6594 (reg_names[(frame_pointer_needed)
6595 ? HARD_FRAME_POINTER_REGNUM : STACK_POINTER_REGNUM]),
6596 ((frame_pointer_needed && TARGET_MIPS16)
6597 ? tsize - cfun->machine->frame.args_size
6598 : tsize),
6599 reg_names[GP_REG_FIRST + 31],
6600 cfun->machine->frame.var_size,
6601 cfun->machine->frame.num_gp,
6602 cfun->machine->frame.num_fp,
6603 cfun->machine->frame.args_size,
6604 cfun->machine->frame.cprestore_size);
6606 /* .mask MASK, GPOFFSET; .fmask FPOFFSET */
6607 fprintf (file, "\t.mask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
6608 cfun->machine->frame.mask,
6609 cfun->machine->frame.gp_save_offset);
6610 fprintf (file, "\t.fmask\t0x%08x," HOST_WIDE_INT_PRINT_DEC "\n",
6611 cfun->machine->frame.fmask,
6612 cfun->machine->frame.fp_save_offset);
6614 /* Require:
6615 OLD_SP == *FRAMEREG + FRAMESIZE => can find old_sp from nominated FP reg.
6616 HIGHEST_GP_SAVED == *FRAMEREG + FRAMESIZE + GPOFFSET => can find saved regs. */
6619 if (TARGET_ABICALLS && !TARGET_NEWABI && cfun->machine->global_pointer > 0)
6621 /* Handle the initialization of $gp for SVR4 PIC. */
6622 if (!cfun->machine->all_noreorder_p)
6623 output_asm_insn ("%(.cpload\t%^%)", 0);
6624 else
6625 output_asm_insn ("%(.cpload\t%^\n\t%<", 0);
6627 else if (cfun->machine->all_noreorder_p)
6628 output_asm_insn ("%(%<", 0);
6630 /* Tell the assembler which register we're using as the global
6631 pointer. This is needed for thunks, since they can use either
6632 explicit relocs or assembler macros. */
6633 mips_output_cplocal ();
6636 /* Make the last instruction frame related and note that it performs
6637 the operation described by FRAME_PATTERN. */
6639 static void
6640 mips_set_frame_expr (rtx frame_pattern)
6642 rtx insn;
6644 insn = get_last_insn ();
6645 RTX_FRAME_RELATED_P (insn) = 1;
6646 REG_NOTES (insn) = alloc_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6647 frame_pattern,
6648 REG_NOTES (insn));
6652 /* Return a frame-related rtx that stores REG at MEM.
6653 REG must be a single register. */
6655 static rtx
6656 mips_frame_set (rtx mem, rtx reg)
6658 rtx set;
6660 /* If we're saving the return address register and the dwarf return
6661 address column differs from the hard register number, adjust the
6662 note reg to refer to the former. */
6663 if (REGNO (reg) == GP_REG_FIRST + 31
6664 && DWARF_FRAME_RETURN_COLUMN != GP_REG_FIRST + 31)
6665 reg = gen_rtx_REG (GET_MODE (reg), DWARF_FRAME_RETURN_COLUMN);
6667 set = gen_rtx_SET (VOIDmode, mem, reg);
6668 RTX_FRAME_RELATED_P (set) = 1;
6670 return set;
6674 /* Save register REG to MEM. Make the instruction frame-related. */
6676 static void
6677 mips_save_reg (rtx reg, rtx mem)
6679 if (GET_MODE (reg) == DFmode && !TARGET_FLOAT64)
6681 rtx x1, x2;
6683 if (mips_split_64bit_move_p (mem, reg))
6684 mips_split_64bit_move (mem, reg);
6685 else
6686 emit_move_insn (mem, reg);
6688 x1 = mips_frame_set (mips_subword (mem, 0), mips_subword (reg, 0));
6689 x2 = mips_frame_set (mips_subword (mem, 1), mips_subword (reg, 1));
6690 mips_set_frame_expr (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x1, x2)));
6692 else
6694 if (TARGET_MIPS16
6695 && REGNO (reg) != GP_REG_FIRST + 31
6696 && !M16_REG_P (REGNO (reg)))
6698 /* Save a non-mips16 register by moving it through a temporary.
6699 We don't need to do this for $31 since there's a special
6700 instruction for it. */
6701 emit_move_insn (MIPS_PROLOGUE_TEMP (GET_MODE (reg)), reg);
6702 emit_move_insn (mem, MIPS_PROLOGUE_TEMP (GET_MODE (reg)));
6704 else
6705 emit_move_insn (mem, reg);
6707 mips_set_frame_expr (mips_frame_set (mem, reg));
6712 /* Expand the prologue into a bunch of separate insns. */
6714 void
6715 mips_expand_prologue (void)
6717 HOST_WIDE_INT size;
6719 if (cfun->machine->global_pointer > 0)
6720 REGNO (pic_offset_table_rtx) = cfun->machine->global_pointer;
6722 size = compute_frame_size (get_frame_size ());
6724 /* Save the registers. Allocate up to MIPS_MAX_FIRST_STACK_STEP
6725 bytes beforehand; this is enough to cover the register save area
6726 without going out of range. */
6727 if ((cfun->machine->frame.mask | cfun->machine->frame.fmask) != 0)
6729 HOST_WIDE_INT step1;
6731 step1 = MIN (size, MIPS_MAX_FIRST_STACK_STEP);
6732 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
6733 stack_pointer_rtx,
6734 GEN_INT (-step1)))) = 1;
6735 size -= step1;
6736 mips_for_each_saved_reg (size, mips_save_reg);
6739 /* Allocate the rest of the frame. */
6740 if (size > 0)
6742 if (SMALL_OPERAND (-size))
6743 RTX_FRAME_RELATED_P (emit_insn (gen_add3_insn (stack_pointer_rtx,
6744 stack_pointer_rtx,
6745 GEN_INT (-size)))) = 1;
6746 else
6748 emit_move_insn (MIPS_PROLOGUE_TEMP (Pmode), GEN_INT (size));
6749 if (TARGET_MIPS16)
6751 /* There are no instructions to add or subtract registers
6752 from the stack pointer, so use the frame pointer as a
6753 temporary. We should always be using a frame pointer
6754 in this case anyway. */
6755 gcc_assert (frame_pointer_needed);
6756 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
6757 emit_insn (gen_sub3_insn (hard_frame_pointer_rtx,
6758 hard_frame_pointer_rtx,
6759 MIPS_PROLOGUE_TEMP (Pmode)));
6760 emit_move_insn (stack_pointer_rtx, hard_frame_pointer_rtx);
6762 else
6763 emit_insn (gen_sub3_insn (stack_pointer_rtx,
6764 stack_pointer_rtx,
6765 MIPS_PROLOGUE_TEMP (Pmode)));
6767 /* Describe the combined effect of the previous instructions. */
6768 mips_set_frame_expr
6769 (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
6770 plus_constant (stack_pointer_rtx, -size)));
6774 /* Set up the frame pointer, if we're using one. In mips16 code,
6775 we point the frame pointer ahead of the outgoing argument area.
6776 This should allow more variables & incoming arguments to be
6777 accessed with unextended instructions. */
6778 if (frame_pointer_needed)
6780 if (TARGET_MIPS16 && cfun->machine->frame.args_size != 0)
6782 rtx offset = GEN_INT (cfun->machine->frame.args_size);
6783 if (SMALL_OPERAND (cfun->machine->frame.args_size))
6784 RTX_FRAME_RELATED_P
6785 (emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
6786 stack_pointer_rtx,
6787 offset))) = 1;
6788 else
6790 emit_move_insn (MIPS_PROLOGUE_TEMP (Pmode), offset);
6791 emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
6792 emit_insn (gen_add3_insn (hard_frame_pointer_rtx,
6793 hard_frame_pointer_rtx,
6794 MIPS_PROLOGUE_TEMP (Pmode)));
6795 mips_set_frame_expr
6796 (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
6797 plus_constant (stack_pointer_rtx,
6798 cfun->machine->frame.args_size)));
6801 else
6802 RTX_FRAME_RELATED_P (emit_move_insn (hard_frame_pointer_rtx,
6803 stack_pointer_rtx)) = 1;
6806 /* If generating o32/o64 abicalls, save $gp on the stack. */
6807 if (TARGET_ABICALLS && !TARGET_NEWABI && !current_function_is_leaf)
6808 emit_insn (gen_cprestore (GEN_INT (current_function_outgoing_args_size)));
6810 mips_emit_loadgp ();
6812 /* If we are profiling, make sure no instructions are scheduled before
6813 the call to mcount. */
6815 if (current_function_profile)
6816 emit_insn (gen_blockage ());
6819 /* Do any necessary cleanup after a function to restore stack, frame,
6820 and regs. */
6822 #define RA_MASK BITMASK_HIGH /* 1 << 31 */
6824 static void
6825 mips_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
6826 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6828 /* Reinstate the normal $gp. */
6829 REGNO (pic_offset_table_rtx) = GLOBAL_POINTER_REGNUM;
6830 mips_output_cplocal ();
6832 if (cfun->machine->all_noreorder_p)
6834 /* Avoid using %>%) since it adds excess whitespace. */
6835 output_asm_insn (".set\tmacro", 0);
6836 output_asm_insn (".set\treorder", 0);
6837 set_noreorder = set_nomacro = 0;
6840 if (!FUNCTION_NAME_ALREADY_DECLARED && !flag_inhibit_size_directive)
6842 const char *fnname;
6844 /* Get the function name the same way that toplev.c does before calling
6845 assemble_start_function. This is needed so that the name used here
6846 exactly matches the name used in ASM_DECLARE_FUNCTION_NAME. */
6847 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
6848 fputs ("\t.end\t", file);
6849 assemble_name (file, fnname);
6850 fputs ("\n", file);
6854 /* Emit instructions to restore register REG from slot MEM. */
6856 static void
6857 mips_restore_reg (rtx reg, rtx mem)
6859 /* There's no mips16 instruction to load $31 directly. Load into
6860 $7 instead and adjust the return insn appropriately. */
6861 if (TARGET_MIPS16 && REGNO (reg) == GP_REG_FIRST + 31)
6862 reg = gen_rtx_REG (GET_MODE (reg), 7);
6864 if (TARGET_MIPS16 && !M16_REG_P (REGNO (reg)))
6866 /* Can't restore directly; move through a temporary. */
6867 emit_move_insn (MIPS_EPILOGUE_TEMP (GET_MODE (reg)), mem);
6868 emit_move_insn (reg, MIPS_EPILOGUE_TEMP (GET_MODE (reg)));
6870 else
6871 emit_move_insn (reg, mem);
6875 /* Expand the epilogue into a bunch of separate insns. SIBCALL_P is true
6876 if this epilogue precedes a sibling call, false if it is for a normal
6877 "epilogue" pattern. */
6879 void
6880 mips_expand_epilogue (int sibcall_p)
6882 HOST_WIDE_INT step1, step2;
6883 rtx base, target;
6885 if (!sibcall_p && mips_can_use_return_insn ())
6887 emit_jump_insn (gen_return ());
6888 return;
6891 /* Split the frame into two. STEP1 is the amount of stack we should
6892 deallocate before restoring the registers. STEP2 is the amount we
6893 should deallocate afterwards.
6895 Start off by assuming that no registers need to be restored. */
6896 step1 = cfun->machine->frame.total_size;
6897 step2 = 0;
6899 /* Work out which register holds the frame address. Account for the
6900 frame pointer offset used by mips16 code. */
6901 if (!frame_pointer_needed)
6902 base = stack_pointer_rtx;
6903 else
6905 base = hard_frame_pointer_rtx;
6906 if (TARGET_MIPS16)
6907 step1 -= cfun->machine->frame.args_size;
6910 /* If we need to restore registers, deallocate as much stack as
6911 possible in the second step without going out of range. */
6912 if ((cfun->machine->frame.mask | cfun->machine->frame.fmask) != 0)
6914 step2 = MIN (step1, MIPS_MAX_FIRST_STACK_STEP);
6915 step1 -= step2;
6918 /* Set TARGET to BASE + STEP1. */
6919 target = base;
6920 if (step1 > 0)
6922 rtx adjust;
6924 /* Get an rtx for STEP1 that we can add to BASE. */
6925 adjust = GEN_INT (step1);
6926 if (!SMALL_OPERAND (step1))
6928 emit_move_insn (MIPS_EPILOGUE_TEMP (Pmode), adjust);
6929 adjust = MIPS_EPILOGUE_TEMP (Pmode);
6932 /* Normal mode code can copy the result straight into $sp. */
6933 if (!TARGET_MIPS16)
6934 target = stack_pointer_rtx;
6936 emit_insn (gen_add3_insn (target, base, adjust));
6939 /* Copy TARGET into the stack pointer. */
6940 if (target != stack_pointer_rtx)
6941 emit_move_insn (stack_pointer_rtx, target);
6943 /* If we're using addressing macros for n32/n64 abicalls, $gp is
6944 implicitly used by all SYMBOL_REFs. We must emit a blockage
6945 insn before restoring it. */
6946 if (TARGET_ABICALLS && TARGET_NEWABI && !TARGET_EXPLICIT_RELOCS)
6947 emit_insn (gen_blockage ());
6949 /* Restore the registers. */
6950 mips_for_each_saved_reg (cfun->machine->frame.total_size - step2,
6951 mips_restore_reg);
6953 /* Deallocate the final bit of the frame. */
6954 if (step2 > 0)
6955 emit_insn (gen_add3_insn (stack_pointer_rtx,
6956 stack_pointer_rtx,
6957 GEN_INT (step2)));
6959 /* Add in the __builtin_eh_return stack adjustment. We need to
6960 use a temporary in mips16 code. */
6961 if (current_function_calls_eh_return)
6963 if (TARGET_MIPS16)
6965 emit_move_insn (MIPS_EPILOGUE_TEMP (Pmode), stack_pointer_rtx);
6966 emit_insn (gen_add3_insn (MIPS_EPILOGUE_TEMP (Pmode),
6967 MIPS_EPILOGUE_TEMP (Pmode),
6968 EH_RETURN_STACKADJ_RTX));
6969 emit_move_insn (stack_pointer_rtx, MIPS_EPILOGUE_TEMP (Pmode));
6971 else
6972 emit_insn (gen_add3_insn (stack_pointer_rtx,
6973 stack_pointer_rtx,
6974 EH_RETURN_STACKADJ_RTX));
6977 if (!sibcall_p)
6979 /* The mips16 loads the return address into $7, not $31. */
6980 if (TARGET_MIPS16 && (cfun->machine->frame.mask & RA_MASK) != 0)
6981 emit_jump_insn (gen_return_internal (gen_rtx_REG (Pmode,
6982 GP_REG_FIRST + 7)));
6983 else
6984 emit_jump_insn (gen_return_internal (gen_rtx_REG (Pmode,
6985 GP_REG_FIRST + 31)));
6989 /* Return nonzero if this function is known to have a null epilogue.
6990 This allows the optimizer to omit jumps to jumps if no stack
6991 was created. */
6994 mips_can_use_return_insn (void)
6996 tree return_type;
6998 if (! reload_completed)
6999 return 0;
7001 if (regs_ever_live[31] || current_function_profile)
7002 return 0;
7004 return_type = DECL_RESULT (current_function_decl);
7006 /* In mips16 mode, a function which returns a floating point value
7007 needs to arrange to copy the return value into the floating point
7008 registers. */
7009 if (TARGET_MIPS16
7010 && mips16_hard_float
7011 && ! aggregate_value_p (return_type, current_function_decl)
7012 && GET_MODE_CLASS (DECL_MODE (return_type)) == MODE_FLOAT
7013 && GET_MODE_SIZE (DECL_MODE (return_type)) <= UNITS_PER_FPVALUE)
7014 return 0;
7016 if (cfun->machine->frame.initialized)
7017 return cfun->machine->frame.total_size == 0;
7019 return compute_frame_size (get_frame_size ()) == 0;
7022 /* Implement TARGET_ASM_OUTPUT_MI_THUNK. Generate rtl rather than asm text
7023 in order to avoid duplicating too much logic from elsewhere. */
7025 static void
7026 mips_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
7027 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
7028 tree function)
7030 rtx this, temp1, temp2, insn, fnaddr;
7032 /* Pretend to be a post-reload pass while generating rtl. */
7033 no_new_pseudos = 1;
7034 reload_completed = 1;
7035 reset_block_changes ();
7037 /* Pick a global pointer for -mabicalls. Use $15 rather than $28
7038 for TARGET_NEWABI since the latter is a call-saved register. */
7039 if (TARGET_ABICALLS)
7040 cfun->machine->global_pointer
7041 = REGNO (pic_offset_table_rtx)
7042 = TARGET_NEWABI ? 15 : GLOBAL_POINTER_REGNUM;
7044 /* Set up the global pointer for n32 or n64 abicalls. */
7045 mips_emit_loadgp ();
7047 /* We need two temporary registers in some cases. */
7048 temp1 = gen_rtx_REG (Pmode, 2);
7049 temp2 = gen_rtx_REG (Pmode, 3);
7051 /* Find out which register contains the "this" pointer. */
7052 if (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
7053 this = gen_rtx_REG (Pmode, GP_ARG_FIRST + 1);
7054 else
7055 this = gen_rtx_REG (Pmode, GP_ARG_FIRST);
7057 /* Add DELTA to THIS. */
7058 if (delta != 0)
7060 rtx offset = GEN_INT (delta);
7061 if (!SMALL_OPERAND (delta))
7063 emit_move_insn (temp1, offset);
7064 offset = temp1;
7066 emit_insn (gen_add3_insn (this, this, offset));
7069 /* If needed, add *(*THIS + VCALL_OFFSET) to THIS. */
7070 if (vcall_offset != 0)
7072 rtx addr;
7074 /* Set TEMP1 to *THIS. */
7075 emit_move_insn (temp1, gen_rtx_MEM (Pmode, this));
7077 /* Set ADDR to a legitimate address for *THIS + VCALL_OFFSET. */
7078 addr = mips_add_offset (temp2, temp1, vcall_offset);
7080 /* Load the offset and add it to THIS. */
7081 emit_move_insn (temp1, gen_rtx_MEM (Pmode, addr));
7082 emit_insn (gen_add3_insn (this, this, temp1));
7085 /* Jump to the target function. Use a sibcall if direct jumps are
7086 allowed, otherwise load the address into a register first. */
7087 fnaddr = XEXP (DECL_RTL (function), 0);
7088 if (TARGET_MIPS16 || TARGET_ABICALLS || TARGET_LONG_CALLS)
7090 /* This is messy. gas treats "la $25,foo" as part of a call
7091 sequence and may allow a global "foo" to be lazily bound.
7092 The general move patterns therefore reject this combination.
7094 In this context, lazy binding would actually be OK for o32 and o64,
7095 but it's still wrong for n32 and n64; see mips_load_call_address.
7096 We must therefore load the address via a temporary register if
7097 mips_dangerous_for_la25_p.
7099 If we jump to the temporary register rather than $25, the assembler
7100 can use the move insn to fill the jump's delay slot. */
7101 if (TARGET_ABICALLS && !mips_dangerous_for_la25_p (fnaddr))
7102 temp1 = gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM);
7103 mips_load_call_address (temp1, fnaddr, true);
7105 if (TARGET_ABICALLS && REGNO (temp1) != PIC_FUNCTION_ADDR_REGNUM)
7106 emit_move_insn (gen_rtx_REG (Pmode, PIC_FUNCTION_ADDR_REGNUM), temp1);
7107 emit_jump_insn (gen_indirect_jump (temp1));
7109 else
7111 insn = emit_call_insn (gen_sibcall_internal (fnaddr, const0_rtx));
7112 SIBLING_CALL_P (insn) = 1;
7115 /* Run just enough of rest_of_compilation. This sequence was
7116 "borrowed" from alpha.c. */
7117 insn = get_insns ();
7118 insn_locators_initialize ();
7119 split_all_insns_noflow ();
7120 if (TARGET_MIPS16)
7121 mips16_lay_out_constants ();
7122 shorten_branches (insn);
7123 final_start_function (insn, file, 1);
7124 final (insn, file, 1);
7125 final_end_function ();
7127 /* Clean up the vars set above. Note that final_end_function resets
7128 the global pointer for us. */
7129 reload_completed = 0;
7130 no_new_pseudos = 0;
7133 /* Returns nonzero if X contains a SYMBOL_REF. */
7135 static int
7136 symbolic_expression_p (rtx x)
7138 if (GET_CODE (x) == SYMBOL_REF)
7139 return 1;
7141 if (GET_CODE (x) == CONST)
7142 return symbolic_expression_p (XEXP (x, 0));
7144 if (UNARY_P (x))
7145 return symbolic_expression_p (XEXP (x, 0));
7147 if (ARITHMETIC_P (x))
7148 return (symbolic_expression_p (XEXP (x, 0))
7149 || symbolic_expression_p (XEXP (x, 1)));
7151 return 0;
7154 /* Choose the section to use for the constant rtx expression X that has
7155 mode MODE. */
7157 static void
7158 mips_select_rtx_section (enum machine_mode mode, rtx x,
7159 unsigned HOST_WIDE_INT align)
7161 if (TARGET_MIPS16)
7163 /* In mips16 mode, the constant table always goes in the same section
7164 as the function, so that constants can be loaded using PC relative
7165 addressing. */
7166 function_section (current_function_decl);
7168 else if (TARGET_EMBEDDED_DATA)
7170 /* For embedded applications, always put constants in read-only data,
7171 in order to reduce RAM usage. */
7172 mergeable_constant_section (mode, align, 0);
7174 else
7176 /* For hosted applications, always put constants in small data if
7177 possible, as this gives the best performance. */
7178 /* ??? Consider using mergeable small data sections. */
7180 if (GET_MODE_SIZE (mode) <= (unsigned) mips_section_threshold
7181 && mips_section_threshold > 0)
7182 named_section (0, ".sdata", 0);
7183 else if (flag_pic && symbolic_expression_p (x))
7184 named_section (0, ".data.rel.ro", 3);
7185 else
7186 mergeable_constant_section (mode, align, 0);
7190 /* Implement TARGET_ASM_FUNCTION_RODATA_SECTION.
7192 The complication here is that, with the combination TARGET_ABICALLS
7193 && !TARGET_GPWORD, jump tables will use absolute addresses, and should
7194 therefore not be included in the read-only part of a DSO. Handle such
7195 cases by selecting a normal data section instead of a read-only one.
7196 The logic apes that in default_function_rodata_section. */
7198 static void
7199 mips_function_rodata_section (tree decl)
7201 if (!TARGET_ABICALLS || TARGET_GPWORD)
7202 default_function_rodata_section (decl);
7203 else if (decl && DECL_SECTION_NAME (decl))
7205 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
7206 if (DECL_ONE_ONLY (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
7208 char *rname = ASTRDUP (name);
7209 rname[14] = 'd';
7210 named_section_real (rname, SECTION_LINKONCE | SECTION_WRITE, decl);
7212 else if (flag_function_sections && flag_data_sections
7213 && strncmp (name, ".text.", 6) == 0)
7215 char *rname = ASTRDUP (name);
7216 memcpy (rname + 1, "data", 4);
7217 named_section_flags (rname, SECTION_WRITE);
7219 else
7220 data_section ();
7222 else
7223 data_section ();
7226 /* Implement TARGET_IN_SMALL_DATA_P. Return true if it would be safe to
7227 access DECL using %gp_rel(...)($gp). */
7229 static bool
7230 mips_in_small_data_p (tree decl)
7232 HOST_WIDE_INT size;
7234 if (TREE_CODE (decl) == STRING_CST || TREE_CODE (decl) == FUNCTION_DECL)
7235 return false;
7237 /* We don't yet generate small-data references for -mabicalls. See related
7238 -G handling in override_options. */
7239 if (TARGET_ABICALLS)
7240 return false;
7242 if (TREE_CODE (decl) == VAR_DECL && DECL_SECTION_NAME (decl) != 0)
7244 const char *name;
7246 /* Reject anything that isn't in a known small-data section. */
7247 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
7248 if (strcmp (name, ".sdata") != 0 && strcmp (name, ".sbss") != 0)
7249 return false;
7251 /* If a symbol is defined externally, the assembler will use the
7252 usual -G rules when deciding how to implement macros. */
7253 if (TARGET_EXPLICIT_RELOCS || !DECL_EXTERNAL (decl))
7254 return true;
7256 else if (TARGET_EMBEDDED_DATA)
7258 /* Don't put constants into the small data section: we want them
7259 to be in ROM rather than RAM. */
7260 if (TREE_CODE (decl) != VAR_DECL)
7261 return false;
7263 if (TREE_READONLY (decl)
7264 && !TREE_SIDE_EFFECTS (decl)
7265 && (!DECL_INITIAL (decl) || TREE_CONSTANT (DECL_INITIAL (decl))))
7266 return false;
7269 size = int_size_in_bytes (TREE_TYPE (decl));
7270 return (size > 0 && size <= mips_section_threshold);
7273 /* See whether VALTYPE is a record whose fields should be returned in
7274 floating-point registers. If so, return the number of fields and
7275 list them in FIELDS (which should have two elements). Return 0
7276 otherwise.
7278 For n32 & n64, a structure with one or two fields is returned in
7279 floating-point registers as long as every field has a floating-point
7280 type. */
7282 static int
7283 mips_fpr_return_fields (tree valtype, tree *fields)
7285 tree field;
7286 int i;
7288 if (!TARGET_NEWABI)
7289 return 0;
7291 if (TREE_CODE (valtype) != RECORD_TYPE)
7292 return 0;
7294 i = 0;
7295 for (field = TYPE_FIELDS (valtype); field != 0; field = TREE_CHAIN (field))
7297 if (TREE_CODE (field) != FIELD_DECL)
7298 continue;
7300 if (TREE_CODE (TREE_TYPE (field)) != REAL_TYPE)
7301 return 0;
7303 if (i == 2)
7304 return 0;
7306 fields[i++] = field;
7308 return i;
7312 /* Implement TARGET_RETURN_IN_MSB. For n32 & n64, we should return
7313 a value in the most significant part of $2/$3 if:
7315 - the target is big-endian;
7317 - the value has a structure or union type (we generalize this to
7318 cover aggregates from other languages too); and
7320 - the structure is not returned in floating-point registers. */
7322 static bool
7323 mips_return_in_msb (tree valtype)
7325 tree fields[2];
7327 return (TARGET_NEWABI
7328 && TARGET_BIG_ENDIAN
7329 && AGGREGATE_TYPE_P (valtype)
7330 && mips_fpr_return_fields (valtype, fields) == 0);
7334 /* Return a composite value in a pair of floating-point registers.
7335 MODE1 and OFFSET1 are the mode and byte offset for the first value,
7336 likewise MODE2 and OFFSET2 for the second. MODE is the mode of the
7337 complete value.
7339 For n32 & n64, $f0 always holds the first value and $f2 the second.
7340 Otherwise the values are packed together as closely as possible. */
7342 static rtx
7343 mips_return_fpr_pair (enum machine_mode mode,
7344 enum machine_mode mode1, HOST_WIDE_INT offset1,
7345 enum machine_mode mode2, HOST_WIDE_INT offset2)
7347 int inc;
7349 inc = (TARGET_NEWABI ? 2 : FP_INC);
7350 return gen_rtx_PARALLEL
7351 (mode,
7352 gen_rtvec (2,
7353 gen_rtx_EXPR_LIST (VOIDmode,
7354 gen_rtx_REG (mode1, FP_RETURN),
7355 GEN_INT (offset1)),
7356 gen_rtx_EXPR_LIST (VOIDmode,
7357 gen_rtx_REG (mode2, FP_RETURN + inc),
7358 GEN_INT (offset2))));
7363 /* Implement FUNCTION_VALUE and LIBCALL_VALUE. For normal calls,
7364 VALTYPE is the return type and MODE is VOIDmode. For libcalls,
7365 VALTYPE is null and MODE is the mode of the return value. */
7368 mips_function_value (tree valtype, tree func ATTRIBUTE_UNUSED,
7369 enum machine_mode mode)
7371 if (valtype)
7373 tree fields[2];
7374 int unsignedp;
7376 mode = TYPE_MODE (valtype);
7377 unsignedp = TYPE_UNSIGNED (valtype);
7379 /* Since we define TARGET_PROMOTE_FUNCTION_RETURN that returns
7380 true, we must promote the mode just as PROMOTE_MODE does. */
7381 mode = promote_mode (valtype, mode, &unsignedp, 1);
7383 /* Handle structures whose fields are returned in $f0/$f2. */
7384 switch (mips_fpr_return_fields (valtype, fields))
7386 case 1:
7387 return gen_rtx_REG (mode, FP_RETURN);
7389 case 2:
7390 return mips_return_fpr_pair (mode,
7391 TYPE_MODE (TREE_TYPE (fields[0])),
7392 int_byte_position (fields[0]),
7393 TYPE_MODE (TREE_TYPE (fields[1])),
7394 int_byte_position (fields[1]));
7397 /* If a value is passed in the most significant part of a register, see
7398 whether we have to round the mode up to a whole number of words. */
7399 if (mips_return_in_msb (valtype))
7401 HOST_WIDE_INT size = int_size_in_bytes (valtype);
7402 if (size % UNITS_PER_WORD != 0)
7404 size += UNITS_PER_WORD - size % UNITS_PER_WORD;
7405 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
7409 /* For EABI, the class of return register depends entirely on MODE.
7410 For example, "struct { some_type x; }" and "union { some_type x; }"
7411 are returned in the same way as a bare "some_type" would be.
7412 Other ABIs only use FPRs for scalar, complex or vector types. */
7413 if (mips_abi != ABI_EABI && !FLOAT_TYPE_P (valtype))
7414 return gen_rtx_REG (mode, GP_RETURN);
7417 if ((GET_MODE_CLASS (mode) == MODE_FLOAT
7418 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
7419 && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE)
7420 return gen_rtx_REG (mode, FP_RETURN);
7422 /* Handle long doubles for n32 & n64. */
7423 if (mode == TFmode)
7424 return mips_return_fpr_pair (mode,
7425 DImode, 0,
7426 DImode, GET_MODE_SIZE (mode) / 2);
7428 if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
7429 && GET_MODE_SIZE (mode) <= UNITS_PER_HWFPVALUE * 2)
7430 return mips_return_fpr_pair (mode,
7431 GET_MODE_INNER (mode), 0,
7432 GET_MODE_INNER (mode),
7433 GET_MODE_SIZE (mode) / 2);
7435 return gen_rtx_REG (mode, GP_RETURN);
7438 /* Return nonzero when an argument must be passed by reference. */
7440 static bool
7441 mips_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
7442 enum machine_mode mode, tree type,
7443 bool named ATTRIBUTE_UNUSED)
7445 if (mips_abi == ABI_EABI)
7447 int size;
7449 /* ??? How should SCmode be handled? */
7450 if (type == NULL_TREE || mode == DImode || mode == DFmode)
7451 return 0;
7453 size = int_size_in_bytes (type);
7454 return size == -1 || size > UNITS_PER_WORD;
7456 else
7458 /* If we have a variable-sized parameter, we have no choice. */
7459 return targetm.calls.must_pass_in_stack (mode, type);
7463 static bool
7464 mips_callee_copies (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
7465 enum machine_mode mode ATTRIBUTE_UNUSED,
7466 tree type ATTRIBUTE_UNUSED, bool named)
7468 return mips_abi == ABI_EABI && named;
7471 /* Return true if registers of class CLASS cannot change from mode FROM
7472 to mode TO. */
7474 bool
7475 mips_cannot_change_mode_class (enum machine_mode from,
7476 enum machine_mode to, enum reg_class class)
7478 if (MIN (GET_MODE_SIZE (from), GET_MODE_SIZE (to)) <= UNITS_PER_WORD
7479 && MAX (GET_MODE_SIZE (from), GET_MODE_SIZE (to)) > UNITS_PER_WORD)
7481 if (TARGET_BIG_ENDIAN)
7483 /* When a multi-word value is stored in paired floating-point
7484 registers, the first register always holds the low word.
7485 We therefore can't allow FPRs to change between single-word
7486 and multi-word modes. */
7487 if (FP_INC > 1 && reg_classes_intersect_p (FP_REGS, class))
7488 return true;
7490 else
7492 /* LO_REGNO == HI_REGNO + 1, so if a multi-word value is stored
7493 in LO and HI, the high word always comes first. We therefore
7494 can't allow values stored in HI to change between single-word
7495 and multi-word modes.
7496 This rule applies to both the original HI/LO pair and the new
7497 DSP accumulators. */
7498 if (reg_classes_intersect_p (ACC_REGS, class))
7499 return true;
7502 /* Loading a 32-bit value into a 64-bit floating-point register
7503 will not sign-extend the value, despite what LOAD_EXTEND_OP says.
7504 We can't allow 64-bit float registers to change from SImode to
7505 to a wider mode. */
7506 if (TARGET_FLOAT64
7507 && from == SImode
7508 && GET_MODE_SIZE (to) >= UNITS_PER_WORD
7509 && reg_classes_intersect_p (FP_REGS, class))
7510 return true;
7511 return false;
7514 /* Return true if X should not be moved directly into register $25.
7515 We need this because many versions of GAS will treat "la $25,foo" as
7516 part of a call sequence and so allow a global "foo" to be lazily bound. */
7518 bool
7519 mips_dangerous_for_la25_p (rtx x)
7521 HOST_WIDE_INT offset;
7523 if (TARGET_EXPLICIT_RELOCS)
7524 return false;
7526 mips_split_const (x, &x, &offset);
7527 return global_got_operand (x, VOIDmode);
7530 /* Implement PREFERRED_RELOAD_CLASS. */
7532 enum reg_class
7533 mips_preferred_reload_class (rtx x, enum reg_class class)
7535 if (mips_dangerous_for_la25_p (x) && reg_class_subset_p (LEA_REGS, class))
7536 return LEA_REGS;
7538 if (TARGET_HARD_FLOAT
7539 && FLOAT_MODE_P (GET_MODE (x))
7540 && reg_class_subset_p (FP_REGS, class))
7541 return FP_REGS;
7543 if (reg_class_subset_p (GR_REGS, class))
7544 class = GR_REGS;
7546 if (TARGET_MIPS16 && reg_class_subset_p (M16_REGS, class))
7547 class = M16_REGS;
7549 return class;
7552 /* This function returns the register class required for a secondary
7553 register when copying between one of the registers in CLASS, and X,
7554 using MODE. If IN_P is nonzero, the copy is going from X to the
7555 register, otherwise the register is the source. A return value of
7556 NO_REGS means that no secondary register is required. */
7558 enum reg_class
7559 mips_secondary_reload_class (enum reg_class class,
7560 enum machine_mode mode, rtx x, int in_p)
7562 enum reg_class gr_regs = TARGET_MIPS16 ? M16_REGS : GR_REGS;
7563 int regno = -1;
7564 int gp_reg_p;
7566 if (REG_P (x)|| GET_CODE (x) == SUBREG)
7567 regno = true_regnum (x);
7569 gp_reg_p = TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
7571 if (mips_dangerous_for_la25_p (x))
7573 gr_regs = LEA_REGS;
7574 if (TEST_HARD_REG_BIT (reg_class_contents[(int) class], 25))
7575 return gr_regs;
7578 /* Copying from HI or LO to anywhere other than a general register
7579 requires a general register.
7580 This rule applies to both the original HI/LO pair and the new
7581 DSP accumulators. */
7582 if (reg_class_subset_p (class, ACC_REGS))
7584 if (TARGET_MIPS16 && in_p)
7586 /* We can't really copy to HI or LO at all in mips16 mode. */
7587 return M16_REGS;
7589 return gp_reg_p ? NO_REGS : gr_regs;
7591 if (ACC_REG_P (regno))
7593 if (TARGET_MIPS16 && ! in_p)
7595 /* We can't really copy to HI or LO at all in mips16 mode. */
7596 return M16_REGS;
7598 return class == gr_regs ? NO_REGS : gr_regs;
7601 /* We can only copy a value to a condition code register from a
7602 floating point register, and even then we require a scratch
7603 floating point register. We can only copy a value out of a
7604 condition code register into a general register. */
7605 if (class == ST_REGS)
7607 if (in_p)
7608 return FP_REGS;
7609 return gp_reg_p ? NO_REGS : gr_regs;
7611 if (ST_REG_P (regno))
7613 if (! in_p)
7614 return FP_REGS;
7615 return class == gr_regs ? NO_REGS : gr_regs;
7618 if (class == FP_REGS)
7620 if (MEM_P (x))
7622 /* In this case we can use lwc1, swc1, ldc1 or sdc1. */
7623 return NO_REGS;
7625 else if (CONSTANT_P (x) && GET_MODE_CLASS (mode) == MODE_FLOAT)
7627 /* We can use the l.s and l.d macros to load floating-point
7628 constants. ??? For l.s, we could probably get better
7629 code by returning GR_REGS here. */
7630 return NO_REGS;
7632 else if (gp_reg_p || x == CONST0_RTX (mode))
7634 /* In this case we can use mtc1, mfc1, dmtc1 or dmfc1. */
7635 return NO_REGS;
7637 else if (FP_REG_P (regno))
7639 /* In this case we can use mov.s or mov.d. */
7640 return NO_REGS;
7642 else
7644 /* Otherwise, we need to reload through an integer register. */
7645 return gr_regs;
7649 /* In mips16 mode, going between memory and anything but M16_REGS
7650 requires an M16_REG. */
7651 if (TARGET_MIPS16)
7653 if (class != M16_REGS && class != M16_NA_REGS)
7655 if (gp_reg_p)
7656 return NO_REGS;
7657 return M16_REGS;
7659 if (! gp_reg_p)
7661 if (class == M16_REGS || class == M16_NA_REGS)
7662 return NO_REGS;
7663 return M16_REGS;
7667 return NO_REGS;
7670 /* Implement CLASS_MAX_NREGS.
7672 Usually all registers are word-sized. The only supported exception
7673 is -mgp64 -msingle-float, which has 64-bit words but 32-bit float
7674 registers. A word-based calculation is correct even in that case,
7675 since -msingle-float disallows multi-FPR values.
7677 The FP status registers are an exception to this rule. They are always
7678 4 bytes wide as they only hold condition code modes, and CCmode is always
7679 considered to be 4 bytes wide. */
7682 mips_class_max_nregs (enum reg_class class ATTRIBUTE_UNUSED,
7683 enum machine_mode mode)
7685 if (class == ST_REGS)
7686 return (GET_MODE_SIZE (mode) + 3) / 4;
7687 else
7688 return (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7691 static bool
7692 mips_valid_pointer_mode (enum machine_mode mode)
7694 return (mode == SImode || (TARGET_64BIT && mode == DImode));
7697 /* Target hook for vector_mode_supported_p. */
7699 static bool
7700 mips_vector_mode_supported_p (enum machine_mode mode)
7702 switch (mode)
7704 case V2SFmode:
7705 return TARGET_PAIRED_SINGLE_FLOAT;
7707 case V2HImode:
7708 case V4QImode:
7709 return TARGET_DSP;
7711 default:
7712 return false;
7716 /* If we can access small data directly (using gp-relative relocation
7717 operators) return the small data pointer, otherwise return null.
7719 For each mips16 function which refers to GP relative symbols, we
7720 use a pseudo register, initialized at the start of the function, to
7721 hold the $gp value. */
7723 static rtx
7724 mips16_gp_pseudo_reg (void)
7726 if (cfun->machine->mips16_gp_pseudo_rtx == NULL_RTX)
7728 rtx unspec;
7729 rtx insn, scan;
7731 cfun->machine->mips16_gp_pseudo_rtx = gen_reg_rtx (Pmode);
7733 /* We want to initialize this to a value which gcc will believe
7734 is constant. */
7735 start_sequence ();
7736 unspec = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_GP);
7737 emit_move_insn (cfun->machine->mips16_gp_pseudo_rtx,
7738 gen_rtx_CONST (Pmode, unspec));
7739 insn = get_insns ();
7740 end_sequence ();
7742 push_topmost_sequence ();
7743 /* We need to emit the initialization after the FUNCTION_BEG
7744 note, so that it will be integrated. */
7745 for (scan = get_insns (); scan != NULL_RTX; scan = NEXT_INSN (scan))
7746 if (NOTE_P (scan)
7747 && NOTE_LINE_NUMBER (scan) == NOTE_INSN_FUNCTION_BEG)
7748 break;
7749 if (scan == NULL_RTX)
7750 scan = get_insns ();
7751 insn = emit_insn_after (insn, scan);
7752 pop_topmost_sequence ();
7755 return cfun->machine->mips16_gp_pseudo_rtx;
7758 /* Write out code to move floating point arguments in or out of
7759 general registers. Output the instructions to FILE. FP_CODE is
7760 the code describing which arguments are present (see the comment at
7761 the definition of CUMULATIVE_ARGS in mips.h). FROM_FP_P is nonzero if
7762 we are copying from the floating point registers. */
7764 static void
7765 mips16_fp_args (FILE *file, int fp_code, int from_fp_p)
7767 const char *s;
7768 int gparg, fparg;
7769 unsigned int f;
7771 /* This code only works for the original 32 bit ABI and the O64 ABI. */
7772 gcc_assert (TARGET_OLDABI);
7774 if (from_fp_p)
7775 s = "mfc1";
7776 else
7777 s = "mtc1";
7778 gparg = GP_ARG_FIRST;
7779 fparg = FP_ARG_FIRST;
7780 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
7782 if ((f & 3) == 1)
7784 if ((fparg & 1) != 0)
7785 ++fparg;
7786 fprintf (file, "\t%s\t%s,%s\n", s,
7787 reg_names[gparg], reg_names[fparg]);
7789 else if ((f & 3) == 2)
7791 if (TARGET_64BIT)
7792 fprintf (file, "\td%s\t%s,%s\n", s,
7793 reg_names[gparg], reg_names[fparg]);
7794 else
7796 if ((fparg & 1) != 0)
7797 ++fparg;
7798 if (TARGET_BIG_ENDIAN)
7799 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
7800 reg_names[gparg], reg_names[fparg + 1], s,
7801 reg_names[gparg + 1], reg_names[fparg]);
7802 else
7803 fprintf (file, "\t%s\t%s,%s\n\t%s\t%s,%s\n", s,
7804 reg_names[gparg], reg_names[fparg], s,
7805 reg_names[gparg + 1], reg_names[fparg + 1]);
7806 ++gparg;
7807 ++fparg;
7810 else
7811 gcc_unreachable ();
7813 ++gparg;
7814 ++fparg;
7818 /* Build a mips16 function stub. This is used for functions which
7819 take arguments in the floating point registers. It is 32 bit code
7820 that moves the floating point args into the general registers, and
7821 then jumps to the 16 bit code. */
7823 static void
7824 build_mips16_function_stub (FILE *file)
7826 const char *fnname;
7827 char *secname, *stubname;
7828 tree stubid, stubdecl;
7829 int need_comma;
7830 unsigned int f;
7832 fnname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
7833 secname = (char *) alloca (strlen (fnname) + 20);
7834 sprintf (secname, ".mips16.fn.%s", fnname);
7835 stubname = (char *) alloca (strlen (fnname) + 20);
7836 sprintf (stubname, "__fn_stub_%s", fnname);
7837 stubid = get_identifier (stubname);
7838 stubdecl = build_decl (FUNCTION_DECL, stubid,
7839 build_function_type (void_type_node, NULL_TREE));
7840 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
7842 fprintf (file, "\t# Stub function for %s (", current_function_name ());
7843 need_comma = 0;
7844 for (f = (unsigned int) current_function_args_info.fp_code; f != 0; f >>= 2)
7846 fprintf (file, "%s%s",
7847 need_comma ? ", " : "",
7848 (f & 3) == 1 ? "float" : "double");
7849 need_comma = 1;
7851 fprintf (file, ")\n");
7853 fprintf (file, "\t.set\tnomips16\n");
7854 function_section (stubdecl);
7855 ASM_OUTPUT_ALIGN (file, floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT));
7857 /* ??? If FUNCTION_NAME_ALREADY_DECLARED is defined, then we are
7858 within a .ent, and we cannot emit another .ent. */
7859 if (!FUNCTION_NAME_ALREADY_DECLARED)
7861 fputs ("\t.ent\t", file);
7862 assemble_name (file, stubname);
7863 fputs ("\n", file);
7866 assemble_name (file, stubname);
7867 fputs (":\n", file);
7869 /* We don't want the assembler to insert any nops here. */
7870 fprintf (file, "\t.set\tnoreorder\n");
7872 mips16_fp_args (file, current_function_args_info.fp_code, 1);
7874 fprintf (asm_out_file, "\t.set\tnoat\n");
7875 fprintf (asm_out_file, "\tla\t%s,", reg_names[GP_REG_FIRST + 1]);
7876 assemble_name (file, fnname);
7877 fprintf (file, "\n");
7878 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
7879 fprintf (asm_out_file, "\t.set\tat\n");
7881 /* Unfortunately, we can't fill the jump delay slot. We can't fill
7882 with one of the mfc1 instructions, because the result is not
7883 available for one instruction, so if the very first instruction
7884 in the function refers to the register, it will see the wrong
7885 value. */
7886 fprintf (file, "\tnop\n");
7888 fprintf (file, "\t.set\treorder\n");
7890 if (!FUNCTION_NAME_ALREADY_DECLARED)
7892 fputs ("\t.end\t", file);
7893 assemble_name (file, stubname);
7894 fputs ("\n", file);
7897 fprintf (file, "\t.set\tmips16\n");
7899 function_section (current_function_decl);
7902 /* We keep a list of functions for which we have already built stubs
7903 in build_mips16_call_stub. */
7905 struct mips16_stub
7907 struct mips16_stub *next;
7908 char *name;
7909 int fpret;
7912 static struct mips16_stub *mips16_stubs;
7914 /* Build a call stub for a mips16 call. A stub is needed if we are
7915 passing any floating point values which should go into the floating
7916 point registers. If we are, and the call turns out to be to a 32
7917 bit function, the stub will be used to move the values into the
7918 floating point registers before calling the 32 bit function. The
7919 linker will magically adjust the function call to either the 16 bit
7920 function or the 32 bit stub, depending upon where the function call
7921 is actually defined.
7923 Similarly, we need a stub if the return value might come back in a
7924 floating point register.
7926 RETVAL is the location of the return value, or null if this is
7927 a call rather than a call_value. FN is the address of the
7928 function and ARG_SIZE is the size of the arguments. FP_CODE
7929 is the code built by function_arg. This function returns a nonzero
7930 value if it builds the call instruction itself. */
7933 build_mips16_call_stub (rtx retval, rtx fn, rtx arg_size, int fp_code)
7935 int fpret;
7936 const char *fnname;
7937 char *secname, *stubname;
7938 struct mips16_stub *l;
7939 tree stubid, stubdecl;
7940 int need_comma;
7941 unsigned int f;
7943 /* We don't need to do anything if we aren't in mips16 mode, or if
7944 we were invoked with the -msoft-float option. */
7945 if (! TARGET_MIPS16 || ! mips16_hard_float)
7946 return 0;
7948 /* Figure out whether the value might come back in a floating point
7949 register. */
7950 fpret = (retval != 0
7951 && GET_MODE_CLASS (GET_MODE (retval)) == MODE_FLOAT
7952 && GET_MODE_SIZE (GET_MODE (retval)) <= UNITS_PER_FPVALUE);
7954 /* We don't need to do anything if there were no floating point
7955 arguments and the value will not be returned in a floating point
7956 register. */
7957 if (fp_code == 0 && ! fpret)
7958 return 0;
7960 /* We don't need to do anything if this is a call to a special
7961 mips16 support function. */
7962 if (GET_CODE (fn) == SYMBOL_REF
7963 && strncmp (XSTR (fn, 0), "__mips16_", 9) == 0)
7964 return 0;
7966 /* This code will only work for o32 and o64 abis. The other ABI's
7967 require more sophisticated support. */
7968 gcc_assert (TARGET_OLDABI);
7970 /* We can only handle SFmode and DFmode floating point return
7971 values. */
7972 if (fpret)
7973 gcc_assert (GET_MODE (retval) == SFmode || GET_MODE (retval) == DFmode);
7975 /* If we're calling via a function pointer, then we must always call
7976 via a stub. There are magic stubs provided in libgcc.a for each
7977 of the required cases. Each of them expects the function address
7978 to arrive in register $2. */
7980 if (GET_CODE (fn) != SYMBOL_REF)
7982 char buf[30];
7983 tree id;
7984 rtx stub_fn, insn;
7986 /* ??? If this code is modified to support other ABI's, we need
7987 to handle PARALLEL return values here. */
7989 sprintf (buf, "__mips16_call_stub_%s%d",
7990 (fpret
7991 ? (GET_MODE (retval) == SFmode ? "sf_" : "df_")
7992 : ""),
7993 fp_code);
7994 id = get_identifier (buf);
7995 stub_fn = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (id));
7997 emit_move_insn (gen_rtx_REG (Pmode, 2), fn);
7999 if (retval == NULL_RTX)
8000 insn = gen_call_internal (stub_fn, arg_size);
8001 else
8002 insn = gen_call_value_internal (retval, stub_fn, arg_size);
8003 insn = emit_call_insn (insn);
8005 /* Put the register usage information on the CALL. */
8006 CALL_INSN_FUNCTION_USAGE (insn) =
8007 gen_rtx_EXPR_LIST (VOIDmode,
8008 gen_rtx_USE (VOIDmode, gen_rtx_REG (Pmode, 2)),
8009 CALL_INSN_FUNCTION_USAGE (insn));
8011 /* If we are handling a floating point return value, we need to
8012 save $18 in the function prologue. Putting a note on the
8013 call will mean that regs_ever_live[$18] will be true if the
8014 call is not eliminated, and we can check that in the prologue
8015 code. */
8016 if (fpret)
8017 CALL_INSN_FUNCTION_USAGE (insn) =
8018 gen_rtx_EXPR_LIST (VOIDmode,
8019 gen_rtx_USE (VOIDmode,
8020 gen_rtx_REG (word_mode, 18)),
8021 CALL_INSN_FUNCTION_USAGE (insn));
8023 /* Return 1 to tell the caller that we've generated the call
8024 insn. */
8025 return 1;
8028 /* We know the function we are going to call. If we have already
8029 built a stub, we don't need to do anything further. */
8031 fnname = XSTR (fn, 0);
8032 for (l = mips16_stubs; l != NULL; l = l->next)
8033 if (strcmp (l->name, fnname) == 0)
8034 break;
8036 if (l == NULL)
8038 /* Build a special purpose stub. When the linker sees a
8039 function call in mips16 code, it will check where the target
8040 is defined. If the target is a 32 bit call, the linker will
8041 search for the section defined here. It can tell which
8042 symbol this section is associated with by looking at the
8043 relocation information (the name is unreliable, since this
8044 might be a static function). If such a section is found, the
8045 linker will redirect the call to the start of the magic
8046 section.
8048 If the function does not return a floating point value, the
8049 special stub section is named
8050 .mips16.call.FNNAME
8052 If the function does return a floating point value, the stub
8053 section is named
8054 .mips16.call.fp.FNNAME
8057 secname = (char *) alloca (strlen (fnname) + 40);
8058 sprintf (secname, ".mips16.call.%s%s",
8059 fpret ? "fp." : "",
8060 fnname);
8061 stubname = (char *) alloca (strlen (fnname) + 20);
8062 sprintf (stubname, "__call_stub_%s%s",
8063 fpret ? "fp_" : "",
8064 fnname);
8065 stubid = get_identifier (stubname);
8066 stubdecl = build_decl (FUNCTION_DECL, stubid,
8067 build_function_type (void_type_node, NULL_TREE));
8068 DECL_SECTION_NAME (stubdecl) = build_string (strlen (secname), secname);
8070 fprintf (asm_out_file, "\t# Stub function to call %s%s (",
8071 (fpret
8072 ? (GET_MODE (retval) == SFmode ? "float " : "double ")
8073 : ""),
8074 fnname);
8075 need_comma = 0;
8076 for (f = (unsigned int) fp_code; f != 0; f >>= 2)
8078 fprintf (asm_out_file, "%s%s",
8079 need_comma ? ", " : "",
8080 (f & 3) == 1 ? "float" : "double");
8081 need_comma = 1;
8083 fprintf (asm_out_file, ")\n");
8085 fprintf (asm_out_file, "\t.set\tnomips16\n");
8086 assemble_start_function (stubdecl, stubname);
8088 if (!FUNCTION_NAME_ALREADY_DECLARED)
8090 fputs ("\t.ent\t", asm_out_file);
8091 assemble_name (asm_out_file, stubname);
8092 fputs ("\n", asm_out_file);
8094 assemble_name (asm_out_file, stubname);
8095 fputs (":\n", asm_out_file);
8098 /* We build the stub code by hand. That's the only way we can
8099 do it, since we can't generate 32 bit code during a 16 bit
8100 compilation. */
8102 /* We don't want the assembler to insert any nops here. */
8103 fprintf (asm_out_file, "\t.set\tnoreorder\n");
8105 mips16_fp_args (asm_out_file, fp_code, 0);
8107 if (! fpret)
8109 fprintf (asm_out_file, "\t.set\tnoat\n");
8110 fprintf (asm_out_file, "\tla\t%s,%s\n", reg_names[GP_REG_FIRST + 1],
8111 fnname);
8112 fprintf (asm_out_file, "\tjr\t%s\n", reg_names[GP_REG_FIRST + 1]);
8113 fprintf (asm_out_file, "\t.set\tat\n");
8114 /* Unfortunately, we can't fill the jump delay slot. We
8115 can't fill with one of the mtc1 instructions, because the
8116 result is not available for one instruction, so if the
8117 very first instruction in the function refers to the
8118 register, it will see the wrong value. */
8119 fprintf (asm_out_file, "\tnop\n");
8121 else
8123 fprintf (asm_out_file, "\tmove\t%s,%s\n",
8124 reg_names[GP_REG_FIRST + 18], reg_names[GP_REG_FIRST + 31]);
8125 fprintf (asm_out_file, "\tjal\t%s\n", fnname);
8126 /* As above, we can't fill the delay slot. */
8127 fprintf (asm_out_file, "\tnop\n");
8128 if (GET_MODE (retval) == SFmode)
8129 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8130 reg_names[GP_REG_FIRST + 2], reg_names[FP_REG_FIRST + 0]);
8131 else
8133 if (TARGET_BIG_ENDIAN)
8135 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8136 reg_names[GP_REG_FIRST + 2],
8137 reg_names[FP_REG_FIRST + 1]);
8138 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8139 reg_names[GP_REG_FIRST + 3],
8140 reg_names[FP_REG_FIRST + 0]);
8142 else
8144 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8145 reg_names[GP_REG_FIRST + 2],
8146 reg_names[FP_REG_FIRST + 0]);
8147 fprintf (asm_out_file, "\tmfc1\t%s,%s\n",
8148 reg_names[GP_REG_FIRST + 3],
8149 reg_names[FP_REG_FIRST + 1]);
8152 fprintf (asm_out_file, "\tj\t%s\n", reg_names[GP_REG_FIRST + 18]);
8153 /* As above, we can't fill the delay slot. */
8154 fprintf (asm_out_file, "\tnop\n");
8157 fprintf (asm_out_file, "\t.set\treorder\n");
8159 #ifdef ASM_DECLARE_FUNCTION_SIZE
8160 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, stubname, stubdecl);
8161 #endif
8163 if (!FUNCTION_NAME_ALREADY_DECLARED)
8165 fputs ("\t.end\t", asm_out_file);
8166 assemble_name (asm_out_file, stubname);
8167 fputs ("\n", asm_out_file);
8170 fprintf (asm_out_file, "\t.set\tmips16\n");
8172 /* Record this stub. */
8173 l = (struct mips16_stub *) xmalloc (sizeof *l);
8174 l->name = xstrdup (fnname);
8175 l->fpret = fpret;
8176 l->next = mips16_stubs;
8177 mips16_stubs = l;
8180 /* If we expect a floating point return value, but we've built a
8181 stub which does not expect one, then we're in trouble. We can't
8182 use the existing stub, because it won't handle the floating point
8183 value. We can't build a new stub, because the linker won't know
8184 which stub to use for the various calls in this object file.
8185 Fortunately, this case is illegal, since it means that a function
8186 was declared in two different ways in a single compilation. */
8187 if (fpret && ! l->fpret)
8188 error ("cannot handle inconsistent calls to %qs", fnname);
8190 /* If we are calling a stub which handles a floating point return
8191 value, we need to arrange to save $18 in the prologue. We do
8192 this by marking the function call as using the register. The
8193 prologue will later see that it is used, and emit code to save
8194 it. */
8196 if (l->fpret)
8198 rtx insn;
8200 if (retval == NULL_RTX)
8201 insn = gen_call_internal (fn, arg_size);
8202 else
8203 insn = gen_call_value_internal (retval, fn, arg_size);
8204 insn = emit_call_insn (insn);
8206 CALL_INSN_FUNCTION_USAGE (insn) =
8207 gen_rtx_EXPR_LIST (VOIDmode,
8208 gen_rtx_USE (VOIDmode, gen_rtx_REG (word_mode, 18)),
8209 CALL_INSN_FUNCTION_USAGE (insn));
8211 /* Return 1 to tell the caller that we've generated the call
8212 insn. */
8213 return 1;
8216 /* Return 0 to let the caller generate the call insn. */
8217 return 0;
8220 /* An entry in the mips16 constant pool. VALUE is the pool constant,
8221 MODE is its mode, and LABEL is the CODE_LABEL associated with it. */
8223 struct mips16_constant {
8224 struct mips16_constant *next;
8225 rtx value;
8226 rtx label;
8227 enum machine_mode mode;
8230 /* Information about an incomplete mips16 constant pool. FIRST is the
8231 first constant, HIGHEST_ADDRESS is the highest address that the first
8232 byte of the pool can have, and INSN_ADDRESS is the current instruction
8233 address. */
8235 struct mips16_constant_pool {
8236 struct mips16_constant *first;
8237 int highest_address;
8238 int insn_address;
8241 /* Add constant VALUE to POOL and return its label. MODE is the
8242 value's mode (used for CONST_INTs, etc.). */
8244 static rtx
8245 add_constant (struct mips16_constant_pool *pool,
8246 rtx value, enum machine_mode mode)
8248 struct mips16_constant **p, *c;
8249 bool first_of_size_p;
8251 /* See whether the constant is already in the pool. If so, return the
8252 existing label, otherwise leave P pointing to the place where the
8253 constant should be added.
8255 Keep the pool sorted in increasing order of mode size so that we can
8256 reduce the number of alignments needed. */
8257 first_of_size_p = true;
8258 for (p = &pool->first; *p != 0; p = &(*p)->next)
8260 if (mode == (*p)->mode && rtx_equal_p (value, (*p)->value))
8261 return (*p)->label;
8262 if (GET_MODE_SIZE (mode) < GET_MODE_SIZE ((*p)->mode))
8263 break;
8264 if (GET_MODE_SIZE (mode) == GET_MODE_SIZE ((*p)->mode))
8265 first_of_size_p = false;
8268 /* In the worst case, the constant needed by the earliest instruction
8269 will end up at the end of the pool. The entire pool must then be
8270 accessible from that instruction.
8272 When adding the first constant, set the pool's highest address to
8273 the address of the first out-of-range byte. Adjust this address
8274 downwards each time a new constant is added. */
8275 if (pool->first == 0)
8276 /* For pc-relative lw, addiu and daddiu instructions, the base PC value
8277 is the address of the instruction with the lowest two bits clear.
8278 The base PC value for ld has the lowest three bits clear. Assume
8279 the worst case here. */
8280 pool->highest_address = pool->insn_address - (UNITS_PER_WORD - 2) + 0x8000;
8281 pool->highest_address -= GET_MODE_SIZE (mode);
8282 if (first_of_size_p)
8283 /* Take into account the worst possible padding due to alignment. */
8284 pool->highest_address -= GET_MODE_SIZE (mode) - 1;
8286 /* Create a new entry. */
8287 c = (struct mips16_constant *) xmalloc (sizeof *c);
8288 c->value = value;
8289 c->mode = mode;
8290 c->label = gen_label_rtx ();
8291 c->next = *p;
8292 *p = c;
8294 return c->label;
8297 /* Output constant VALUE after instruction INSN and return the last
8298 instruction emitted. MODE is the mode of the constant. */
8300 static rtx
8301 dump_constants_1 (enum machine_mode mode, rtx value, rtx insn)
8303 switch (GET_MODE_CLASS (mode))
8305 case MODE_INT:
8307 rtx size = GEN_INT (GET_MODE_SIZE (mode));
8308 return emit_insn_after (gen_consttable_int (value, size), insn);
8311 case MODE_FLOAT:
8312 return emit_insn_after (gen_consttable_float (value), insn);
8314 case MODE_VECTOR_FLOAT:
8315 case MODE_VECTOR_INT:
8317 int i;
8318 for (i = 0; i < CONST_VECTOR_NUNITS (value); i++)
8319 insn = dump_constants_1 (GET_MODE_INNER (mode),
8320 CONST_VECTOR_ELT (value, i), insn);
8321 return insn;
8324 default:
8325 gcc_unreachable ();
8330 /* Dump out the constants in CONSTANTS after INSN. */
8332 static void
8333 dump_constants (struct mips16_constant *constants, rtx insn)
8335 struct mips16_constant *c, *next;
8336 int align;
8338 align = 0;
8339 for (c = constants; c != NULL; c = next)
8341 /* If necessary, increase the alignment of PC. */
8342 if (align < GET_MODE_SIZE (c->mode))
8344 int align_log = floor_log2 (GET_MODE_SIZE (c->mode));
8345 insn = emit_insn_after (gen_align (GEN_INT (align_log)), insn);
8347 align = GET_MODE_SIZE (c->mode);
8349 insn = emit_label_after (c->label, insn);
8350 insn = dump_constants_1 (c->mode, c->value, insn);
8352 next = c->next;
8353 free (c);
8356 emit_barrier_after (insn);
8359 /* Return the length of instruction INSN. */
8361 static int
8362 mips16_insn_length (rtx insn)
8364 if (JUMP_P (insn))
8366 rtx body = PATTERN (insn);
8367 if (GET_CODE (body) == ADDR_VEC)
8368 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 0);
8369 if (GET_CODE (body) == ADDR_DIFF_VEC)
8370 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, 1);
8372 return get_attr_length (insn);
8375 /* Rewrite *X so that constant pool references refer to the constant's
8376 label instead. DATA points to the constant pool structure. */
8378 static int
8379 mips16_rewrite_pool_refs (rtx *x, void *data)
8381 struct mips16_constant_pool *pool = data;
8382 if (GET_CODE (*x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (*x))
8383 *x = gen_rtx_LABEL_REF (Pmode, add_constant (pool,
8384 get_pool_constant (*x),
8385 get_pool_mode (*x)));
8386 return 0;
8389 /* Build MIPS16 constant pools. */
8391 static void
8392 mips16_lay_out_constants (void)
8394 struct mips16_constant_pool pool;
8395 rtx insn, barrier;
8397 barrier = 0;
8398 memset (&pool, 0, sizeof (pool));
8399 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8401 /* Rewrite constant pool references in INSN. */
8402 if (INSN_P (insn))
8403 for_each_rtx (&PATTERN (insn), mips16_rewrite_pool_refs, &pool);
8405 pool.insn_address += mips16_insn_length (insn);
8407 if (pool.first != NULL)
8409 /* If there are no natural barriers between the first user of
8410 the pool and the highest acceptable address, we'll need to
8411 create a new instruction to jump around the constant pool.
8412 In the worst case, this instruction will be 4 bytes long.
8414 If it's too late to do this transformation after INSN,
8415 do it immediately before INSN. */
8416 if (barrier == 0 && pool.insn_address + 4 > pool.highest_address)
8418 rtx label, jump;
8420 label = gen_label_rtx ();
8422 jump = emit_jump_insn_before (gen_jump (label), insn);
8423 JUMP_LABEL (jump) = label;
8424 LABEL_NUSES (label) = 1;
8425 barrier = emit_barrier_after (jump);
8427 emit_label_after (label, barrier);
8428 pool.insn_address += 4;
8431 /* See whether the constant pool is now out of range of the first
8432 user. If so, output the constants after the previous barrier.
8433 Note that any instructions between BARRIER and INSN (inclusive)
8434 will use negative offsets to refer to the pool. */
8435 if (pool.insn_address > pool.highest_address)
8437 dump_constants (pool.first, barrier);
8438 pool.first = NULL;
8439 barrier = 0;
8441 else if (BARRIER_P (insn))
8442 barrier = insn;
8445 dump_constants (pool.first, get_last_insn ());
8448 /* A temporary variable used by for_each_rtx callbacks, etc. */
8449 static rtx mips_sim_insn;
8451 /* A structure representing the state of the processor pipeline.
8452 Used by the mips_sim_* family of functions. */
8453 struct mips_sim {
8454 /* The maximum number of instructions that can be issued in a cycle.
8455 (Caches mips_issue_rate.) */
8456 unsigned int issue_rate;
8458 /* The current simulation time. */
8459 unsigned int time;
8461 /* How many more instructions can be issued in the current cycle. */
8462 unsigned int insns_left;
8464 /* LAST_SET[X].INSN is the last instruction to set register X.
8465 LAST_SET[X].TIME is the time at which that instruction was issued.
8466 INSN is null if no instruction has yet set register X. */
8467 struct {
8468 rtx insn;
8469 unsigned int time;
8470 } last_set[FIRST_PSEUDO_REGISTER];
8472 /* The pipeline's current DFA state. */
8473 state_t dfa_state;
8476 /* Reset STATE to the initial simulation state. */
8478 static void
8479 mips_sim_reset (struct mips_sim *state)
8481 state->time = 0;
8482 state->insns_left = state->issue_rate;
8483 memset (&state->last_set, 0, sizeof (state->last_set));
8484 state_reset (state->dfa_state);
8487 /* Initialize STATE before its first use. DFA_STATE points to an
8488 allocated but uninitialized DFA state. */
8490 static void
8491 mips_sim_init (struct mips_sim *state, state_t dfa_state)
8493 state->issue_rate = mips_issue_rate ();
8494 state->dfa_state = dfa_state;
8495 mips_sim_reset (state);
8498 /* Advance STATE by one clock cycle. */
8500 static void
8501 mips_sim_next_cycle (struct mips_sim *state)
8503 state->time++;
8504 state->insns_left = state->issue_rate;
8505 state_transition (state->dfa_state, 0);
8508 /* Advance simulation state STATE until instruction INSN can read
8509 register REG. */
8511 static void
8512 mips_sim_wait_reg (struct mips_sim *state, rtx insn, rtx reg)
8514 unsigned int i;
8516 for (i = 0; i < HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)); i++)
8517 if (state->last_set[REGNO (reg) + i].insn != 0)
8519 unsigned int t;
8521 t = state->last_set[REGNO (reg) + i].time;
8522 t += insn_latency (state->last_set[REGNO (reg) + i].insn, insn);
8523 while (state->time < t)
8524 mips_sim_next_cycle (state);
8528 /* A for_each_rtx callback. If *X is a register, advance simulation state
8529 DATA until mips_sim_insn can read the register's value. */
8531 static int
8532 mips_sim_wait_regs_2 (rtx *x, void *data)
8534 if (REG_P (*x))
8535 mips_sim_wait_reg (data, mips_sim_insn, *x);
8536 return 0;
8539 /* Call mips_sim_wait_regs_2 (R, DATA) for each register R mentioned in *X. */
8541 static void
8542 mips_sim_wait_regs_1 (rtx *x, void *data)
8544 for_each_rtx (x, mips_sim_wait_regs_2, data);
8547 /* Advance simulation state STATE until all of INSN's register
8548 dependencies are satisfied. */
8550 static void
8551 mips_sim_wait_regs (struct mips_sim *state, rtx insn)
8553 mips_sim_insn = insn;
8554 note_uses (&PATTERN (insn), mips_sim_wait_regs_1, state);
8557 /* Advance simulation state STATE until the units required by
8558 instruction INSN are available. */
8560 static void
8561 mips_sim_wait_units (struct mips_sim *state, rtx insn)
8563 state_t tmp_state;
8565 tmp_state = alloca (state_size ());
8566 while (state->insns_left == 0
8567 || (memcpy (tmp_state, state->dfa_state, state_size ()),
8568 state_transition (tmp_state, insn) >= 0))
8569 mips_sim_next_cycle (state);
8572 /* Advance simulation state STATE until INSN is ready to issue. */
8574 static void
8575 mips_sim_wait_insn (struct mips_sim *state, rtx insn)
8577 mips_sim_wait_regs (state, insn);
8578 mips_sim_wait_units (state, insn);
8581 /* mips_sim_insn has just set X. Update the LAST_SET array
8582 in simulation state DATA. */
8584 static void
8585 mips_sim_record_set (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
8587 struct mips_sim *state;
8588 unsigned int i;
8590 state = data;
8591 if (REG_P (x))
8592 for (i = 0; i < HARD_REGNO_NREGS (REGNO (x), GET_MODE (x)); i++)
8594 state->last_set[REGNO (x) + i].insn = mips_sim_insn;
8595 state->last_set[REGNO (x) + i].time = state->time;
8599 /* Issue instruction INSN in scheduler state STATE. Assume that INSN
8600 can issue immediately (i.e., that mips_sim_wait_insn has already
8601 been called). */
8603 static void
8604 mips_sim_issue_insn (struct mips_sim *state, rtx insn)
8606 state_transition (state->dfa_state, insn);
8607 state->insns_left--;
8609 mips_sim_insn = insn;
8610 note_stores (PATTERN (insn), mips_sim_record_set, state);
8613 /* Simulate issuing a NOP in state STATE. */
8615 static void
8616 mips_sim_issue_nop (struct mips_sim *state)
8618 if (state->insns_left == 0)
8619 mips_sim_next_cycle (state);
8620 state->insns_left--;
8623 /* Update simulation state STATE so that it's ready to accept the instruction
8624 after INSN. INSN should be part of the main rtl chain, not a member of a
8625 SEQUENCE. */
8627 static void
8628 mips_sim_finish_insn (struct mips_sim *state, rtx insn)
8630 /* If INSN is a jump with an implicit delay slot, simulate a nop. */
8631 if (JUMP_P (insn))
8632 mips_sim_issue_nop (state);
8634 switch (GET_CODE (SEQ_BEGIN (insn)))
8636 case CODE_LABEL:
8637 case CALL_INSN:
8638 /* We can't predict the processor state after a call or label. */
8639 mips_sim_reset (state);
8640 break;
8642 case JUMP_INSN:
8643 /* The delay slots of branch likely instructions are only executed
8644 when the branch is taken. Therefore, if the caller has simulated
8645 the delay slot instruction, STATE does not really reflect the state
8646 of the pipeline for the instruction after the delay slot. Also,
8647 branch likely instructions tend to incur a penalty when not taken,
8648 so there will probably be an extra delay between the branch and
8649 the instruction after the delay slot. */
8650 if (INSN_ANNULLED_BRANCH_P (SEQ_BEGIN (insn)))
8651 mips_sim_reset (state);
8652 break;
8654 default:
8655 break;
8659 /* The VR4130 pipeline issues aligned pairs of instructions together,
8660 but it stalls the second instruction if it depends on the first.
8661 In order to cut down the amount of logic required, this dependence
8662 check is not based on a full instruction decode. Instead, any non-SPECIAL
8663 instruction is assumed to modify the register specified by bits 20-16
8664 (which is usually the "rt" field).
8666 In beq, beql, bne and bnel instructions, the rt field is actually an
8667 input, so we can end up with a false dependence between the branch
8668 and its delay slot. If this situation occurs in instruction INSN,
8669 try to avoid it by swapping rs and rt. */
8671 static void
8672 vr4130_avoid_branch_rt_conflict (rtx insn)
8674 rtx first, second;
8676 first = SEQ_BEGIN (insn);
8677 second = SEQ_END (insn);
8678 if (JUMP_P (first)
8679 && NONJUMP_INSN_P (second)
8680 && GET_CODE (PATTERN (first)) == SET
8681 && GET_CODE (SET_DEST (PATTERN (first))) == PC
8682 && GET_CODE (SET_SRC (PATTERN (first))) == IF_THEN_ELSE)
8684 /* Check for the right kind of condition. */
8685 rtx cond = XEXP (SET_SRC (PATTERN (first)), 0);
8686 if ((GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
8687 && REG_P (XEXP (cond, 0))
8688 && REG_P (XEXP (cond, 1))
8689 && reg_referenced_p (XEXP (cond, 1), PATTERN (second))
8690 && !reg_referenced_p (XEXP (cond, 0), PATTERN (second)))
8692 /* SECOND mentions the rt register but not the rs register. */
8693 rtx tmp = XEXP (cond, 0);
8694 XEXP (cond, 0) = XEXP (cond, 1);
8695 XEXP (cond, 1) = tmp;
8700 /* Implement -mvr4130-align. Go through each basic block and simulate the
8701 processor pipeline. If we find that a pair of instructions could execute
8702 in parallel, and the first of those instruction is not 8-byte aligned,
8703 insert a nop to make it aligned. */
8705 static void
8706 vr4130_align_insns (void)
8708 struct mips_sim state;
8709 rtx insn, subinsn, last, last2, next;
8710 bool aligned_p;
8712 dfa_start ();
8714 /* LAST is the last instruction before INSN to have a nonzero length.
8715 LAST2 is the last such instruction before LAST. */
8716 last = 0;
8717 last2 = 0;
8719 /* ALIGNED_P is true if INSN is known to be at an aligned address. */
8720 aligned_p = true;
8722 mips_sim_init (&state, alloca (state_size ()));
8723 for (insn = get_insns (); insn != 0; insn = next)
8725 unsigned int length;
8727 next = NEXT_INSN (insn);
8729 /* See the comment above vr4130_avoid_branch_rt_conflict for details.
8730 This isn't really related to the alignment pass, but we do it on
8731 the fly to avoid a separate instruction walk. */
8732 vr4130_avoid_branch_rt_conflict (insn);
8734 if (USEFUL_INSN_P (insn))
8735 FOR_EACH_SUBINSN (subinsn, insn)
8737 mips_sim_wait_insn (&state, subinsn);
8739 /* If we want this instruction to issue in parallel with the
8740 previous one, make sure that the previous instruction is
8741 aligned. There are several reasons why this isn't worthwhile
8742 when the second instruction is a call:
8744 - Calls are less likely to be performance critical,
8745 - There's a good chance that the delay slot can execute
8746 in parallel with the call.
8747 - The return address would then be unaligned.
8749 In general, if we're going to insert a nop between instructions
8750 X and Y, it's better to insert it immediately after X. That
8751 way, if the nop makes Y aligned, it will also align any labels
8752 between X and Y. */
8753 if (state.insns_left != state.issue_rate
8754 && !CALL_P (subinsn))
8756 if (subinsn == SEQ_BEGIN (insn) && aligned_p)
8758 /* SUBINSN is the first instruction in INSN and INSN is
8759 aligned. We want to align the previous instruction
8760 instead, so insert a nop between LAST2 and LAST.
8762 Note that LAST could be either a single instruction
8763 or a branch with a delay slot. In the latter case,
8764 LAST, like INSN, is already aligned, but the delay
8765 slot must have some extra delay that stops it from
8766 issuing at the same time as the branch. We therefore
8767 insert a nop before the branch in order to align its
8768 delay slot. */
8769 emit_insn_after (gen_nop (), last2);
8770 aligned_p = false;
8772 else if (subinsn != SEQ_BEGIN (insn) && !aligned_p)
8774 /* SUBINSN is the delay slot of INSN, but INSN is
8775 currently unaligned. Insert a nop between
8776 LAST and INSN to align it. */
8777 emit_insn_after (gen_nop (), last);
8778 aligned_p = true;
8781 mips_sim_issue_insn (&state, subinsn);
8783 mips_sim_finish_insn (&state, insn);
8785 /* Update LAST, LAST2 and ALIGNED_P for the next instruction. */
8786 length = get_attr_length (insn);
8787 if (length > 0)
8789 /* If the instruction is an asm statement or multi-instruction
8790 mips.md patern, the length is only an estimate. Insert an
8791 8 byte alignment after it so that the following instructions
8792 can be handled correctly. */
8793 if (NONJUMP_INSN_P (SEQ_BEGIN (insn))
8794 && (recog_memoized (insn) < 0 || length >= 8))
8796 next = emit_insn_after (gen_align (GEN_INT (3)), insn);
8797 next = NEXT_INSN (next);
8798 mips_sim_next_cycle (&state);
8799 aligned_p = true;
8801 else if (length & 4)
8802 aligned_p = !aligned_p;
8803 last2 = last;
8804 last = insn;
8807 /* See whether INSN is an aligned label. */
8808 if (LABEL_P (insn) && label_to_alignment (insn) >= 3)
8809 aligned_p = true;
8811 dfa_finish ();
8814 /* Subroutine of mips_reorg. If there is a hazard between INSN
8815 and a previous instruction, avoid it by inserting nops after
8816 instruction AFTER.
8818 *DELAYED_REG and *HILO_DELAY describe the hazards that apply at
8819 this point. If *DELAYED_REG is non-null, INSN must wait a cycle
8820 before using the value of that register. *HILO_DELAY counts the
8821 number of instructions since the last hilo hazard (that is,
8822 the number of instructions since the last mflo or mfhi).
8824 After inserting nops for INSN, update *DELAYED_REG and *HILO_DELAY
8825 for the next instruction.
8827 LO_REG is an rtx for the LO register, used in dependence checking. */
8829 static void
8830 mips_avoid_hazard (rtx after, rtx insn, int *hilo_delay,
8831 rtx *delayed_reg, rtx lo_reg)
8833 rtx pattern, set;
8834 int nops, ninsns;
8836 if (!INSN_P (insn))
8837 return;
8839 pattern = PATTERN (insn);
8841 /* Do not put the whole function in .set noreorder if it contains
8842 an asm statement. We don't know whether there will be hazards
8843 between the asm statement and the gcc-generated code. */
8844 if (GET_CODE (pattern) == ASM_INPUT || asm_noperands (pattern) >= 0)
8845 cfun->machine->all_noreorder_p = false;
8847 /* Ignore zero-length instructions (barriers and the like). */
8848 ninsns = get_attr_length (insn) / 4;
8849 if (ninsns == 0)
8850 return;
8852 /* Work out how many nops are needed. Note that we only care about
8853 registers that are explicitly mentioned in the instruction's pattern.
8854 It doesn't matter that calls use the argument registers or that they
8855 clobber hi and lo. */
8856 if (*hilo_delay < 2 && reg_set_p (lo_reg, pattern))
8857 nops = 2 - *hilo_delay;
8858 else if (*delayed_reg != 0 && reg_referenced_p (*delayed_reg, pattern))
8859 nops = 1;
8860 else
8861 nops = 0;
8863 /* Insert the nops between this instruction and the previous one.
8864 Each new nop takes us further from the last hilo hazard. */
8865 *hilo_delay += nops;
8866 while (nops-- > 0)
8867 emit_insn_after (gen_hazard_nop (), after);
8869 /* Set up the state for the next instruction. */
8870 *hilo_delay += ninsns;
8871 *delayed_reg = 0;
8872 if (INSN_CODE (insn) >= 0)
8873 switch (get_attr_hazard (insn))
8875 case HAZARD_NONE:
8876 break;
8878 case HAZARD_HILO:
8879 *hilo_delay = 0;
8880 break;
8882 case HAZARD_DELAY:
8883 set = single_set (insn);
8884 gcc_assert (set != 0);
8885 *delayed_reg = SET_DEST (set);
8886 break;
8891 /* Go through the instruction stream and insert nops where necessary.
8892 See if the whole function can then be put into .set noreorder &
8893 .set nomacro. */
8895 static void
8896 mips_avoid_hazards (void)
8898 rtx insn, last_insn, lo_reg, delayed_reg;
8899 int hilo_delay, i;
8901 /* Force all instructions to be split into their final form. */
8902 split_all_insns_noflow ();
8904 /* Recalculate instruction lengths without taking nops into account. */
8905 cfun->machine->ignore_hazard_length_p = true;
8906 shorten_branches (get_insns ());
8908 cfun->machine->all_noreorder_p = true;
8910 /* Profiled functions can't be all noreorder because the profiler
8911 support uses assembler macros. */
8912 if (current_function_profile)
8913 cfun->machine->all_noreorder_p = false;
8915 /* Code compiled with -mfix-vr4120 can't be all noreorder because
8916 we rely on the assembler to work around some errata. */
8917 if (TARGET_FIX_VR4120)
8918 cfun->machine->all_noreorder_p = false;
8920 /* The same is true for -mfix-vr4130 if we might generate mflo or
8921 mfhi instructions. Note that we avoid using mflo and mfhi if
8922 the VR4130 macc and dmacc instructions are available instead;
8923 see the *mfhilo_{si,di}_macc patterns. */
8924 if (TARGET_FIX_VR4130 && !ISA_HAS_MACCHI)
8925 cfun->machine->all_noreorder_p = false;
8927 last_insn = 0;
8928 hilo_delay = 2;
8929 delayed_reg = 0;
8930 lo_reg = gen_rtx_REG (SImode, LO_REGNUM);
8932 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
8933 if (INSN_P (insn))
8935 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
8936 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
8937 mips_avoid_hazard (last_insn, XVECEXP (PATTERN (insn), 0, i),
8938 &hilo_delay, &delayed_reg, lo_reg);
8939 else
8940 mips_avoid_hazard (last_insn, insn, &hilo_delay,
8941 &delayed_reg, lo_reg);
8943 last_insn = insn;
8948 /* Implement TARGET_MACHINE_DEPENDENT_REORG. */
8950 static void
8951 mips_reorg (void)
8953 if (TARGET_MIPS16)
8954 mips16_lay_out_constants ();
8955 else if (TARGET_EXPLICIT_RELOCS)
8957 if (mips_flag_delayed_branch)
8958 dbr_schedule (get_insns (), dump_file);
8959 mips_avoid_hazards ();
8960 if (TUNE_MIPS4130 && TARGET_VR4130_ALIGN)
8961 vr4130_align_insns ();
8965 /* This function does three things:
8967 - Register the special divsi3 and modsi3 functions if -mfix-vr4120.
8968 - Register the mips16 hardware floating point stubs.
8969 - Register the gofast functions if selected using --enable-gofast. */
8971 #include "config/gofast.h"
8973 static void
8974 mips_init_libfuncs (void)
8976 if (TARGET_FIX_VR4120)
8978 set_optab_libfunc (sdiv_optab, SImode, "__vr4120_divsi3");
8979 set_optab_libfunc (smod_optab, SImode, "__vr4120_modsi3");
8982 if (TARGET_MIPS16 && mips16_hard_float)
8984 set_optab_libfunc (add_optab, SFmode, "__mips16_addsf3");
8985 set_optab_libfunc (sub_optab, SFmode, "__mips16_subsf3");
8986 set_optab_libfunc (smul_optab, SFmode, "__mips16_mulsf3");
8987 set_optab_libfunc (sdiv_optab, SFmode, "__mips16_divsf3");
8989 set_optab_libfunc (eq_optab, SFmode, "__mips16_eqsf2");
8990 set_optab_libfunc (ne_optab, SFmode, "__mips16_nesf2");
8991 set_optab_libfunc (gt_optab, SFmode, "__mips16_gtsf2");
8992 set_optab_libfunc (ge_optab, SFmode, "__mips16_gesf2");
8993 set_optab_libfunc (lt_optab, SFmode, "__mips16_ltsf2");
8994 set_optab_libfunc (le_optab, SFmode, "__mips16_lesf2");
8996 set_conv_libfunc (sfix_optab, SImode, SFmode, "__mips16_fix_truncsfsi");
8997 set_conv_libfunc (sfloat_optab, SFmode, SImode, "__mips16_floatsisf");
8999 if (TARGET_DOUBLE_FLOAT)
9001 set_optab_libfunc (add_optab, DFmode, "__mips16_adddf3");
9002 set_optab_libfunc (sub_optab, DFmode, "__mips16_subdf3");
9003 set_optab_libfunc (smul_optab, DFmode, "__mips16_muldf3");
9004 set_optab_libfunc (sdiv_optab, DFmode, "__mips16_divdf3");
9006 set_optab_libfunc (eq_optab, DFmode, "__mips16_eqdf2");
9007 set_optab_libfunc (ne_optab, DFmode, "__mips16_nedf2");
9008 set_optab_libfunc (gt_optab, DFmode, "__mips16_gtdf2");
9009 set_optab_libfunc (ge_optab, DFmode, "__mips16_gedf2");
9010 set_optab_libfunc (lt_optab, DFmode, "__mips16_ltdf2");
9011 set_optab_libfunc (le_optab, DFmode, "__mips16_ledf2");
9013 set_conv_libfunc (sext_optab, DFmode, SFmode, "__mips16_extendsfdf2");
9014 set_conv_libfunc (trunc_optab, SFmode, DFmode, "__mips16_truncdfsf2");
9016 set_conv_libfunc (sfix_optab, SImode, DFmode, "__mips16_fix_truncdfsi");
9017 set_conv_libfunc (sfloat_optab, DFmode, SImode, "__mips16_floatsidf");
9020 else
9021 gofast_maybe_init_libfuncs ();
9024 /* Return a number assessing the cost of moving a register in class
9025 FROM to class TO. The classes are expressed using the enumeration
9026 values such as `GENERAL_REGS'. A value of 2 is the default; other
9027 values are interpreted relative to that.
9029 It is not required that the cost always equal 2 when FROM is the
9030 same as TO; on some machines it is expensive to move between
9031 registers if they are not general registers.
9033 If reload sees an insn consisting of a single `set' between two
9034 hard registers, and if `REGISTER_MOVE_COST' applied to their
9035 classes returns a value of 2, reload does not check to ensure that
9036 the constraints of the insn are met. Setting a cost of other than
9037 2 will allow reload to verify that the constraints are met. You
9038 should do this if the `movM' pattern's constraints do not allow
9039 such copying.
9041 ??? We make the cost of moving from HI/LO into general
9042 registers the same as for one of moving general registers to
9043 HI/LO for TARGET_MIPS16 in order to prevent allocating a
9044 pseudo to HI/LO. This might hurt optimizations though, it
9045 isn't clear if it is wise. And it might not work in all cases. We
9046 could solve the DImode LO reg problem by using a multiply, just
9047 like reload_{in,out}si. We could solve the SImode/HImode HI reg
9048 problem by using divide instructions. divu puts the remainder in
9049 the HI reg, so doing a divide by -1 will move the value in the HI
9050 reg for all values except -1. We could handle that case by using a
9051 signed divide, e.g. -1 / 2 (or maybe 1 / -2?). We'd have to emit
9052 a compare/branch to test the input value to see which instruction
9053 we need to use. This gets pretty messy, but it is feasible. */
9056 mips_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
9057 enum reg_class to, enum reg_class from)
9059 if (from == M16_REGS && GR_REG_CLASS_P (to))
9060 return 2;
9061 else if (from == M16_NA_REGS && GR_REG_CLASS_P (to))
9062 return 2;
9063 else if (GR_REG_CLASS_P (from))
9065 if (to == M16_REGS)
9066 return 2;
9067 else if (to == M16_NA_REGS)
9068 return 2;
9069 else if (GR_REG_CLASS_P (to))
9071 if (TARGET_MIPS16)
9072 return 4;
9073 else
9074 return 2;
9076 else if (to == FP_REGS)
9077 return 4;
9078 else if (reg_class_subset_p (to, ACC_REGS))
9080 if (TARGET_MIPS16)
9081 return 12;
9082 else
9083 return 6;
9085 else if (COP_REG_CLASS_P (to))
9087 return 5;
9090 else if (from == FP_REGS)
9092 if (GR_REG_CLASS_P (to))
9093 return 4;
9094 else if (to == FP_REGS)
9095 return 2;
9096 else if (to == ST_REGS)
9097 return 8;
9099 else if (reg_class_subset_p (from, ACC_REGS))
9101 if (GR_REG_CLASS_P (to))
9103 if (TARGET_MIPS16)
9104 return 12;
9105 else
9106 return 6;
9109 else if (from == ST_REGS && GR_REG_CLASS_P (to))
9110 return 4;
9111 else if (COP_REG_CLASS_P (from))
9113 return 5;
9116 /* Fall through.
9117 ??? What cases are these? Shouldn't we return 2 here? */
9119 return 12;
9122 /* Return the length of INSN. LENGTH is the initial length computed by
9123 attributes in the machine-description file. */
9126 mips_adjust_insn_length (rtx insn, int length)
9128 /* A unconditional jump has an unfilled delay slot if it is not part
9129 of a sequence. A conditional jump normally has a delay slot, but
9130 does not on MIPS16. */
9131 if (CALL_P (insn) || (TARGET_MIPS16 ? simplejump_p (insn) : JUMP_P (insn)))
9132 length += 4;
9134 /* See how many nops might be needed to avoid hardware hazards. */
9135 if (!cfun->machine->ignore_hazard_length_p && INSN_CODE (insn) >= 0)
9136 switch (get_attr_hazard (insn))
9138 case HAZARD_NONE:
9139 break;
9141 case HAZARD_DELAY:
9142 length += 4;
9143 break;
9145 case HAZARD_HILO:
9146 length += 8;
9147 break;
9150 /* All MIPS16 instructions are a measly two bytes. */
9151 if (TARGET_MIPS16)
9152 length /= 2;
9154 return length;
9158 /* Return an asm sequence to start a noat block and load the address
9159 of a label into $1. */
9161 const char *
9162 mips_output_load_label (void)
9164 if (TARGET_EXPLICIT_RELOCS)
9165 switch (mips_abi)
9167 case ABI_N32:
9168 return "%[lw\t%@,%%got_page(%0)(%+)\n\taddiu\t%@,%@,%%got_ofst(%0)";
9170 case ABI_64:
9171 return "%[ld\t%@,%%got_page(%0)(%+)\n\tdaddiu\t%@,%@,%%got_ofst(%0)";
9173 default:
9174 if (ISA_HAS_LOAD_DELAY)
9175 return "%[lw\t%@,%%got(%0)(%+)%#\n\taddiu\t%@,%@,%%lo(%0)";
9176 return "%[lw\t%@,%%got(%0)(%+)\n\taddiu\t%@,%@,%%lo(%0)";
9178 else
9180 if (Pmode == DImode)
9181 return "%[dla\t%@,%0";
9182 else
9183 return "%[la\t%@,%0";
9188 /* Output assembly instructions to peform a conditional branch.
9190 INSN is the branch instruction. OPERANDS[0] is the condition.
9191 OPERANDS[1] is the target of the branch. OPERANDS[2] is the target
9192 of the first operand to the condition. If TWO_OPERANDS_P is
9193 nonzero the comparison takes two operands; OPERANDS[3] will be the
9194 second operand.
9196 If INVERTED_P is nonzero we are to branch if the condition does
9197 not hold. If FLOAT_P is nonzero this is a floating-point comparison.
9199 LENGTH is the length (in bytes) of the sequence we are to generate.
9200 That tells us whether to generate a simple conditional branch, or a
9201 reversed conditional branch around a `jr' instruction. */
9202 const char *
9203 mips_output_conditional_branch (rtx insn, rtx *operands, int two_operands_p,
9204 int float_p, int inverted_p, int length)
9206 static char buffer[200];
9207 /* The kind of comparison we are doing. */
9208 enum rtx_code code = GET_CODE (operands[0]);
9209 /* Nonzero if the opcode for the comparison needs a `z' indicating
9210 that it is a comparison against zero. */
9211 int need_z_p;
9212 /* A string to use in the assembly output to represent the first
9213 operand. */
9214 const char *op1 = "%z2";
9215 /* A string to use in the assembly output to represent the second
9216 operand. Use the hard-wired zero register if there's no second
9217 operand. */
9218 const char *op2 = (two_operands_p ? ",%z3" : ",%.");
9219 /* The operand-printing string for the comparison. */
9220 const char *const comp = (float_p ? "%F0" : "%C0");
9221 /* The operand-printing string for the inverted comparison. */
9222 const char *const inverted_comp = (float_p ? "%W0" : "%N0");
9224 /* The MIPS processors (for levels of the ISA at least two), have
9225 "likely" variants of each branch instruction. These instructions
9226 annul the instruction in the delay slot if the branch is not
9227 taken. */
9228 mips_branch_likely = (final_sequence && INSN_ANNULLED_BRANCH_P (insn));
9230 if (!two_operands_p)
9232 /* To compute whether than A > B, for example, we normally
9233 subtract B from A and then look at the sign bit. But, if we
9234 are doing an unsigned comparison, and B is zero, we don't
9235 have to do the subtraction. Instead, we can just check to
9236 see if A is nonzero. Thus, we change the CODE here to
9237 reflect the simpler comparison operation. */
9238 switch (code)
9240 case GTU:
9241 code = NE;
9242 break;
9244 case LEU:
9245 code = EQ;
9246 break;
9248 case GEU:
9249 /* A condition which will always be true. */
9250 code = EQ;
9251 op1 = "%.";
9252 break;
9254 case LTU:
9255 /* A condition which will always be false. */
9256 code = NE;
9257 op1 = "%.";
9258 break;
9260 default:
9261 /* Not a special case. */
9262 break;
9266 /* Relative comparisons are always done against zero. But
9267 equality comparisons are done between two operands, and therefore
9268 do not require a `z' in the assembly language output. */
9269 need_z_p = (!float_p && code != EQ && code != NE);
9270 /* For comparisons against zero, the zero is not provided
9271 explicitly. */
9272 if (need_z_p)
9273 op2 = "";
9275 /* Begin by terminating the buffer. That way we can always use
9276 strcat to add to it. */
9277 buffer[0] = '\0';
9279 switch (length)
9281 case 4:
9282 case 8:
9283 /* Just a simple conditional branch. */
9284 if (float_p)
9285 sprintf (buffer, "%%*b%s%%?\t%%Z2%%1%%/",
9286 inverted_p ? inverted_comp : comp);
9287 else
9288 sprintf (buffer, "%%*b%s%s%%?\t%s%s,%%1%%/",
9289 inverted_p ? inverted_comp : comp,
9290 need_z_p ? "z" : "",
9291 op1,
9292 op2);
9293 return buffer;
9295 case 12:
9296 case 16:
9297 case 24:
9298 case 28:
9300 /* Generate a reversed conditional branch around ` j'
9301 instruction:
9303 .set noreorder
9304 .set nomacro
9305 bc l
9306 delay_slot or #nop
9307 j target
9308 #nop
9310 .set macro
9311 .set reorder
9313 If the original branch was a likely branch, the delay slot
9314 must be executed only if the branch is taken, so generate:
9316 .set noreorder
9317 .set nomacro
9318 bc l
9319 #nop
9320 j target
9321 delay slot or #nop
9323 .set macro
9324 .set reorder
9326 When generating PIC, instead of:
9328 j target
9330 we emit:
9332 .set noat
9333 la $at, target
9334 jr $at
9335 .set at
9338 rtx orig_target;
9339 rtx target = gen_label_rtx ();
9341 orig_target = operands[1];
9342 operands[1] = target;
9343 /* Generate the reversed comparison. This takes four
9344 bytes. */
9345 if (float_p)
9346 sprintf (buffer, "%%*b%s\t%%Z2%%1",
9347 inverted_p ? comp : inverted_comp);
9348 else
9349 sprintf (buffer, "%%*b%s%s\t%s%s,%%1",
9350 inverted_p ? comp : inverted_comp,
9351 need_z_p ? "z" : "",
9352 op1,
9353 op2);
9354 output_asm_insn (buffer, operands);
9356 if (length != 16 && length != 28 && ! mips_branch_likely)
9358 /* Output delay slot instruction. */
9359 rtx insn = final_sequence;
9360 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
9361 optimize, 1, NULL);
9362 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
9364 else
9365 output_asm_insn ("%#", 0);
9367 if (length <= 16)
9368 output_asm_insn ("j\t%0", &orig_target);
9369 else
9371 output_asm_insn (mips_output_load_label (), &orig_target);
9372 output_asm_insn ("jr\t%@%]", 0);
9375 if (length != 16 && length != 28 && mips_branch_likely)
9377 /* Output delay slot instruction. */
9378 rtx insn = final_sequence;
9379 final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file,
9380 optimize, 1, NULL);
9381 INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
9383 else
9384 output_asm_insn ("%#", 0);
9386 (*targetm.asm_out.internal_label) (asm_out_file, "L",
9387 CODE_LABEL_NUMBER (target));
9389 return "";
9392 default:
9393 gcc_unreachable ();
9396 /* NOTREACHED */
9397 return 0;
9400 /* Used to output div or ddiv instruction DIVISION, which has the operands
9401 given by OPERANDS. Add in a divide-by-zero check if needed.
9403 When working around R4000 and R4400 errata, we need to make sure that
9404 the division is not immediately followed by a shift[1][2]. We also
9405 need to stop the division from being put into a branch delay slot[3].
9406 The easiest way to avoid both problems is to add a nop after the
9407 division. When a divide-by-zero check is needed, this nop can be
9408 used to fill the branch delay slot.
9410 [1] If a double-word or a variable shift executes immediately
9411 after starting an integer division, the shift may give an
9412 incorrect result. See quotations of errata #16 and #28 from
9413 "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
9414 in mips.md for details.
9416 [2] A similar bug to [1] exists for all revisions of the
9417 R4000 and the R4400 when run in an MC configuration.
9418 From "MIPS R4000MC Errata, Processor Revision 2.2 and 3.0":
9420 "19. In this following sequence:
9422 ddiv (or ddivu or div or divu)
9423 dsll32 (or dsrl32, dsra32)
9425 if an MPT stall occurs, while the divide is slipping the cpu
9426 pipeline, then the following double shift would end up with an
9427 incorrect result.
9429 Workaround: The compiler needs to avoid generating any
9430 sequence with divide followed by extended double shift."
9432 This erratum is also present in "MIPS R4400MC Errata, Processor
9433 Revision 1.0" and "MIPS R4400MC Errata, Processor Revision 2.0
9434 & 3.0" as errata #10 and #4, respectively.
9436 [3] From "MIPS R4000PC/SC Errata, Processor Revision 2.2 and 3.0"
9437 (also valid for MIPS R4000MC processors):
9439 "52. R4000SC: This bug does not apply for the R4000PC.
9441 There are two flavors of this bug:
9443 1) If the instruction just after divide takes an RF exception
9444 (tlb-refill, tlb-invalid) and gets an instruction cache
9445 miss (both primary and secondary) and the line which is
9446 currently in secondary cache at this index had the first
9447 data word, where the bits 5..2 are set, then R4000 would
9448 get a wrong result for the div.
9452 div r8, r9
9453 ------------------- # end-of page. -tlb-refill
9457 div r8, r9
9458 ------------------- # end-of page. -tlb-invalid
9461 2) If the divide is in the taken branch delay slot, where the
9462 target takes RF exception and gets an I-cache miss for the
9463 exception vector or where I-cache miss occurs for the
9464 target address, under the above mentioned scenarios, the
9465 div would get wrong results.
9468 j r2 # to next page mapped or unmapped
9469 div r8,r9 # this bug would be there as long
9470 # as there is an ICache miss and
9471 nop # the "data pattern" is present
9474 beq r0, r0, NextPage # to Next page
9475 div r8,r9
9478 This bug is present for div, divu, ddiv, and ddivu
9479 instructions.
9481 Workaround: For item 1), OS could make sure that the next page
9482 after the divide instruction is also mapped. For item 2), the
9483 compiler could make sure that the divide instruction is not in
9484 the branch delay slot."
9486 These processors have PRId values of 0x00004220 and 0x00004300 for
9487 the R4000 and 0x00004400, 0x00004500 and 0x00004600 for the R4400. */
9489 const char *
9490 mips_output_division (const char *division, rtx *operands)
9492 const char *s;
9494 s = division;
9495 if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
9497 output_asm_insn (s, operands);
9498 s = "nop";
9500 if (TARGET_CHECK_ZERO_DIV)
9502 if (TARGET_MIPS16)
9504 output_asm_insn (s, operands);
9505 s = "bnez\t%2,1f\n\tbreak\t7\n1:";
9507 else if (GENERATE_DIVIDE_TRAPS)
9509 output_asm_insn (s, operands);
9510 s = "teq\t%2,%.,7";
9512 else
9514 output_asm_insn ("%(bne\t%2,%.,1f", operands);
9515 output_asm_insn (s, operands);
9516 s = "break\t7%)\n1:";
9519 return s;
9522 /* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
9523 with a final "000" replaced by "k". Ignore case.
9525 Note: this function is shared between GCC and GAS. */
9527 static bool
9528 mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
9530 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
9531 given++, canonical++;
9533 return ((*given == 0 && *canonical == 0)
9534 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
9538 /* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
9539 CPU name. We've traditionally allowed a lot of variation here.
9541 Note: this function is shared between GCC and GAS. */
9543 static bool
9544 mips_matching_cpu_name_p (const char *canonical, const char *given)
9546 /* First see if the name matches exactly, or with a final "000"
9547 turned into "k". */
9548 if (mips_strict_matching_cpu_name_p (canonical, given))
9549 return true;
9551 /* If not, try comparing based on numerical designation alone.
9552 See if GIVEN is an unadorned number, or 'r' followed by a number. */
9553 if (TOLOWER (*given) == 'r')
9554 given++;
9555 if (!ISDIGIT (*given))
9556 return false;
9558 /* Skip over some well-known prefixes in the canonical name,
9559 hoping to find a number there too. */
9560 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
9561 canonical += 2;
9562 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
9563 canonical += 2;
9564 else if (TOLOWER (canonical[0]) == 'r')
9565 canonical += 1;
9567 return mips_strict_matching_cpu_name_p (canonical, given);
9571 /* Return the mips_cpu_info entry for the processor or ISA given
9572 by CPU_STRING. Return null if the string isn't recognized.
9574 A similar function exists in GAS. */
9576 static const struct mips_cpu_info *
9577 mips_parse_cpu (const char *cpu_string)
9579 const struct mips_cpu_info *p;
9580 const char *s;
9582 /* In the past, we allowed upper-case CPU names, but it doesn't
9583 work well with the multilib machinery. */
9584 for (s = cpu_string; *s != 0; s++)
9585 if (ISUPPER (*s))
9587 warning (0, "the cpu name must be lower case");
9588 break;
9591 /* 'from-abi' selects the most compatible architecture for the given
9592 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
9593 EABIs, we have to decide whether we're using the 32-bit or 64-bit
9594 version. Look first at the -mgp options, if given, otherwise base
9595 the choice on MASK_64BIT in TARGET_DEFAULT. */
9596 if (strcasecmp (cpu_string, "from-abi") == 0)
9597 return mips_cpu_info_from_isa (ABI_NEEDS_32BIT_REGS ? 1
9598 : ABI_NEEDS_64BIT_REGS ? 3
9599 : (TARGET_64BIT ? 3 : 1));
9601 /* 'default' has traditionally been a no-op. Probably not very useful. */
9602 if (strcasecmp (cpu_string, "default") == 0)
9603 return 0;
9605 for (p = mips_cpu_info_table; p->name != 0; p++)
9606 if (mips_matching_cpu_name_p (p->name, cpu_string))
9607 return p;
9609 return 0;
9613 /* Return the processor associated with the given ISA level, or null
9614 if the ISA isn't valid. */
9616 static const struct mips_cpu_info *
9617 mips_cpu_info_from_isa (int isa)
9619 const struct mips_cpu_info *p;
9621 for (p = mips_cpu_info_table; p->name != 0; p++)
9622 if (p->isa == isa)
9623 return p;
9625 return 0;
9628 /* Implement HARD_REGNO_NREGS. The size of FP registers is controlled
9629 by UNITS_PER_FPREG. The size of FP status registers is always 4, because
9630 they only hold condition code modes, and CCmode is always considered to
9631 be 4 bytes wide. All other registers are word sized. */
9633 unsigned int
9634 mips_hard_regno_nregs (int regno, enum machine_mode mode)
9636 if (ST_REG_P (regno))
9637 return ((GET_MODE_SIZE (mode) + 3) / 4);
9638 else if (! FP_REG_P (regno))
9639 return ((GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
9640 else
9641 return ((GET_MODE_SIZE (mode) + UNITS_PER_FPREG - 1) / UNITS_PER_FPREG);
9644 /* Implement TARGET_RETURN_IN_MEMORY. Under the old (i.e., 32 and O64 ABIs)
9645 all BLKmode objects are returned in memory. Under the new (N32 and
9646 64-bit MIPS ABIs) small structures are returned in a register.
9647 Objects with varying size must still be returned in memory, of
9648 course. */
9650 static bool
9651 mips_return_in_memory (tree type, tree fndecl ATTRIBUTE_UNUSED)
9653 if (TARGET_OLDABI)
9654 return (TYPE_MODE (type) == BLKmode);
9655 else
9656 return ((int_size_in_bytes (type) > (2 * UNITS_PER_WORD))
9657 || (int_size_in_bytes (type) == -1));
9660 static bool
9661 mips_strict_argument_naming (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED)
9663 return !TARGET_OLDABI;
9666 /* Return true if INSN is a multiply-add or multiply-subtract
9667 instruction and PREV assigns to the accumulator operand. */
9669 bool
9670 mips_linked_madd_p (rtx prev, rtx insn)
9672 rtx x;
9674 x = single_set (insn);
9675 if (x == 0)
9676 return false;
9678 x = SET_SRC (x);
9680 if (GET_CODE (x) == PLUS
9681 && GET_CODE (XEXP (x, 0)) == MULT
9682 && reg_set_p (XEXP (x, 1), prev))
9683 return true;
9685 if (GET_CODE (x) == MINUS
9686 && GET_CODE (XEXP (x, 1)) == MULT
9687 && reg_set_p (XEXP (x, 0), prev))
9688 return true;
9690 return false;
9693 /* Used by TUNE_MACC_CHAINS to record the last scheduled instruction
9694 that may clobber hi or lo. */
9696 static rtx mips_macc_chains_last_hilo;
9698 /* A TUNE_MACC_CHAINS helper function. Record that instruction INSN has
9699 been scheduled, updating mips_macc_chains_last_hilo appropriately. */
9701 static void
9702 mips_macc_chains_record (rtx insn)
9704 if (get_attr_may_clobber_hilo (insn))
9705 mips_macc_chains_last_hilo = insn;
9708 /* A TUNE_MACC_CHAINS helper function. Search ready queue READY, which
9709 has NREADY elements, looking for a multiply-add or multiply-subtract
9710 instruction that is cumulative with mips_macc_chains_last_hilo.
9711 If there is one, promote it ahead of anything else that might
9712 clobber hi or lo. */
9714 static void
9715 mips_macc_chains_reorder (rtx *ready, int nready)
9717 int i, j;
9719 if (mips_macc_chains_last_hilo != 0)
9720 for (i = nready - 1; i >= 0; i--)
9721 if (mips_linked_madd_p (mips_macc_chains_last_hilo, ready[i]))
9723 for (j = nready - 1; j > i; j--)
9724 if (recog_memoized (ready[j]) >= 0
9725 && get_attr_may_clobber_hilo (ready[j]))
9727 mips_promote_ready (ready, i, j);
9728 break;
9730 break;
9734 /* The last instruction to be scheduled. */
9736 static rtx vr4130_last_insn;
9738 /* A note_stores callback used by vr4130_true_reg_dependence_p. DATA
9739 points to an rtx that is initially an instruction. Nullify the rtx
9740 if the instruction uses the value of register X. */
9742 static void
9743 vr4130_true_reg_dependence_p_1 (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
9745 rtx *insn_ptr = data;
9746 if (REG_P (x)
9747 && *insn_ptr != 0
9748 && reg_referenced_p (x, PATTERN (*insn_ptr)))
9749 *insn_ptr = 0;
9752 /* Return true if there is true register dependence between vr4130_last_insn
9753 and INSN. */
9755 static bool
9756 vr4130_true_reg_dependence_p (rtx insn)
9758 note_stores (PATTERN (vr4130_last_insn),
9759 vr4130_true_reg_dependence_p_1, &insn);
9760 return insn == 0;
9763 /* A TUNE_MIPS4130 helper function. Given that INSN1 is at the head of
9764 the ready queue and that INSN2 is the instruction after it, return
9765 true if it is worth promoting INSN2 ahead of INSN1. Look for cases
9766 in which INSN1 and INSN2 can probably issue in parallel, but for
9767 which (INSN2, INSN1) should be less sensitive to instruction
9768 alignment than (INSN1, INSN2). See 4130.md for more details. */
9770 static bool
9771 vr4130_swap_insns_p (rtx insn1, rtx insn2)
9773 rtx dep;
9775 /* Check for the following case:
9777 1) there is some other instruction X with an anti dependence on INSN1;
9778 2) X has a higher priority than INSN2; and
9779 3) X is an arithmetic instruction (and thus has no unit restrictions).
9781 If INSN1 is the last instruction blocking X, it would better to
9782 choose (INSN1, X) over (INSN2, INSN1). */
9783 for (dep = INSN_DEPEND (insn1); dep != 0; dep = XEXP (dep, 1))
9784 if (REG_NOTE_KIND (dep) == REG_DEP_ANTI
9785 && INSN_PRIORITY (XEXP (dep, 0)) > INSN_PRIORITY (insn2)
9786 && recog_memoized (XEXP (dep, 0)) >= 0
9787 && get_attr_vr4130_class (XEXP (dep, 0)) == VR4130_CLASS_ALU)
9788 return false;
9790 if (vr4130_last_insn != 0
9791 && recog_memoized (insn1) >= 0
9792 && recog_memoized (insn2) >= 0)
9794 /* See whether INSN1 and INSN2 use different execution units,
9795 or if they are both ALU-type instructions. If so, they can
9796 probably execute in parallel. */
9797 enum attr_vr4130_class class1 = get_attr_vr4130_class (insn1);
9798 enum attr_vr4130_class class2 = get_attr_vr4130_class (insn2);
9799 if (class1 != class2 || class1 == VR4130_CLASS_ALU)
9801 /* If only one of the instructions has a dependence on
9802 vr4130_last_insn, prefer to schedule the other one first. */
9803 bool dep1 = vr4130_true_reg_dependence_p (insn1);
9804 bool dep2 = vr4130_true_reg_dependence_p (insn2);
9805 if (dep1 != dep2)
9806 return dep1;
9808 /* Prefer to schedule INSN2 ahead of INSN1 if vr4130_last_insn
9809 is not an ALU-type instruction and if INSN1 uses the same
9810 execution unit. (Note that if this condition holds, we already
9811 know that INSN2 uses a different execution unit.) */
9812 if (class1 != VR4130_CLASS_ALU
9813 && recog_memoized (vr4130_last_insn) >= 0
9814 && class1 == get_attr_vr4130_class (vr4130_last_insn))
9815 return true;
9818 return false;
9821 /* A TUNE_MIPS4130 helper function. (READY, NREADY) describes a ready
9822 queue with at least two instructions. Swap the first two if
9823 vr4130_swap_insns_p says that it could be worthwhile. */
9825 static void
9826 vr4130_reorder (rtx *ready, int nready)
9828 if (vr4130_swap_insns_p (ready[nready - 1], ready[nready - 2]))
9829 mips_promote_ready (ready, nready - 2, nready - 1);
9832 /* Remove the instruction at index LOWER from ready queue READY and
9833 reinsert it in front of the instruction at index HIGHER. LOWER must
9834 be <= HIGHER. */
9836 static void
9837 mips_promote_ready (rtx *ready, int lower, int higher)
9839 rtx new_head;
9840 int i;
9842 new_head = ready[lower];
9843 for (i = lower; i < higher; i++)
9844 ready[i] = ready[i + 1];
9845 ready[i] = new_head;
9848 /* Implement TARGET_SCHED_REORDER. */
9850 static int
9851 mips_sched_reorder (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
9852 rtx *ready, int *nreadyp, int cycle)
9854 if (!reload_completed && TUNE_MACC_CHAINS)
9856 if (cycle == 0)
9857 mips_macc_chains_last_hilo = 0;
9858 if (*nreadyp > 0)
9859 mips_macc_chains_reorder (ready, *nreadyp);
9861 if (reload_completed && TUNE_MIPS4130 && !TARGET_VR4130_ALIGN)
9863 if (cycle == 0)
9864 vr4130_last_insn = 0;
9865 if (*nreadyp > 1)
9866 vr4130_reorder (ready, *nreadyp);
9868 return mips_issue_rate ();
9871 /* Implement TARGET_SCHED_VARIABLE_ISSUE. */
9873 static int
9874 mips_variable_issue (FILE *file ATTRIBUTE_UNUSED, int verbose ATTRIBUTE_UNUSED,
9875 rtx insn, int more)
9877 switch (GET_CODE (PATTERN (insn)))
9879 case USE:
9880 case CLOBBER:
9881 /* Don't count USEs and CLOBBERs against the issue rate. */
9882 break;
9884 default:
9885 more--;
9886 if (!reload_completed && TUNE_MACC_CHAINS)
9887 mips_macc_chains_record (insn);
9888 vr4130_last_insn = insn;
9889 break;
9891 return more;
9894 /* Implement TARGET_SCHED_ADJUST_COST. We assume that anti and output
9895 dependencies have no cost. */
9897 static int
9898 mips_adjust_cost (rtx insn ATTRIBUTE_UNUSED, rtx link,
9899 rtx dep ATTRIBUTE_UNUSED, int cost)
9901 if (REG_NOTE_KIND (link) != 0)
9902 return 0;
9903 return cost;
9906 /* Return the number of instructions that can be issued per cycle. */
9908 static int
9909 mips_issue_rate (void)
9911 switch (mips_tune)
9913 case PROCESSOR_R4130:
9914 case PROCESSOR_R5400:
9915 case PROCESSOR_R5500:
9916 case PROCESSOR_R7000:
9917 case PROCESSOR_R9000:
9918 return 2;
9920 case PROCESSOR_SB1:
9921 /* This is actually 4, but we get better performance if we claim 3.
9922 This is partly because of unwanted speculative code motion with the
9923 larger number, and partly because in most common cases we can't
9924 reach the theoretical max of 4. */
9925 return 3;
9927 default:
9928 return 1;
9932 /* Implements TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD. This should
9933 be as wide as the scheduling freedom in the DFA. */
9935 static int
9936 mips_multipass_dfa_lookahead (void)
9938 /* Can schedule up to 4 of the 6 function units in any one cycle. */
9939 if (mips_tune == PROCESSOR_SB1)
9940 return 4;
9942 return 0;
9945 /* Given that we have an rtx of the form (prefetch ... WRITE LOCALITY),
9946 return the first operand of the associated "pref" or "prefx" insn. */
9949 mips_prefetch_cookie (rtx write, rtx locality)
9951 /* store_streamed / load_streamed. */
9952 if (INTVAL (locality) <= 0)
9953 return GEN_INT (INTVAL (write) + 4);
9955 /* store / load. */
9956 if (INTVAL (locality) <= 2)
9957 return write;
9959 /* store_retained / load_retained. */
9960 return GEN_INT (INTVAL (write) + 6);
9963 /* MIPS builtin function support. */
9965 struct builtin_description
9967 /* The code of the main .md file instruction. See mips_builtin_type
9968 for more information. */
9969 enum insn_code icode;
9971 /* The floating-point comparison code to use with ICODE, if any. */
9972 enum mips_fp_condition cond;
9974 /* The name of the builtin function. */
9975 const char *name;
9977 /* Specifies how the function should be expanded. */
9978 enum mips_builtin_type builtin_type;
9980 /* The function's prototype. */
9981 enum mips_function_type function_type;
9983 /* The target flags required for this function. */
9984 int target_flags;
9987 /* Define a MIPS_BUILTIN_DIRECT function for instruction CODE_FOR_mips_<INSN>.
9988 FUNCTION_TYPE and TARGET_FLAGS are builtin_description fields. */
9989 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, TARGET_FLAGS) \
9990 { CODE_FOR_mips_ ## INSN, 0, "__builtin_mips_" #INSN, \
9991 MIPS_BUILTIN_DIRECT, FUNCTION_TYPE, TARGET_FLAGS }
9993 /* Define __builtin_mips_<INSN>_<COND>_{s,d}, both of which require
9994 TARGET_FLAGS. */
9995 #define CMP_SCALAR_BUILTINS(INSN, COND, TARGET_FLAGS) \
9996 { CODE_FOR_mips_ ## INSN ## _cond_s, MIPS_FP_COND_ ## COND, \
9997 "__builtin_mips_" #INSN "_" #COND "_s", \
9998 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_SF_SF, TARGET_FLAGS }, \
9999 { CODE_FOR_mips_ ## INSN ## _cond_d, MIPS_FP_COND_ ## COND, \
10000 "__builtin_mips_" #INSN "_" #COND "_d", \
10001 MIPS_BUILTIN_CMP_SINGLE, MIPS_INT_FTYPE_DF_DF, TARGET_FLAGS }
10003 /* Define __builtin_mips_{any,all,upper,lower}_<INSN>_<COND>_ps.
10004 The lower and upper forms require TARGET_FLAGS while the any and all
10005 forms require MASK_MIPS3D. */
10006 #define CMP_PS_BUILTINS(INSN, COND, TARGET_FLAGS) \
10007 { CODE_FOR_mips_ ## INSN ## _cond_ps, MIPS_FP_COND_ ## COND, \
10008 "__builtin_mips_any_" #INSN "_" #COND "_ps", \
10009 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF, MASK_MIPS3D }, \
10010 { CODE_FOR_mips_ ## INSN ## _cond_ps, MIPS_FP_COND_ ## COND, \
10011 "__builtin_mips_all_" #INSN "_" #COND "_ps", \
10012 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF, MASK_MIPS3D }, \
10013 { CODE_FOR_mips_ ## INSN ## _cond_ps, MIPS_FP_COND_ ## COND, \
10014 "__builtin_mips_lower_" #INSN "_" #COND "_ps", \
10015 MIPS_BUILTIN_CMP_LOWER, MIPS_INT_FTYPE_V2SF_V2SF, TARGET_FLAGS }, \
10016 { CODE_FOR_mips_ ## INSN ## _cond_ps, MIPS_FP_COND_ ## COND, \
10017 "__builtin_mips_upper_" #INSN "_" #COND "_ps", \
10018 MIPS_BUILTIN_CMP_UPPER, MIPS_INT_FTYPE_V2SF_V2SF, TARGET_FLAGS }
10020 /* Define __builtin_mips_{any,all}_<INSN>_<COND>_4s. The functions
10021 require MASK_MIPS3D. */
10022 #define CMP_4S_BUILTINS(INSN, COND) \
10023 { CODE_FOR_mips_ ## INSN ## _cond_4s, MIPS_FP_COND_ ## COND, \
10024 "__builtin_mips_any_" #INSN "_" #COND "_4s", \
10025 MIPS_BUILTIN_CMP_ANY, MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, \
10026 MASK_MIPS3D }, \
10027 { CODE_FOR_mips_ ## INSN ## _cond_4s, MIPS_FP_COND_ ## COND, \
10028 "__builtin_mips_all_" #INSN "_" #COND "_4s", \
10029 MIPS_BUILTIN_CMP_ALL, MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF, \
10030 MASK_MIPS3D }
10032 /* Define __builtin_mips_mov{t,f}_<INSN>_<COND>_ps. The comparison
10033 instruction requires TARGET_FLAGS. */
10034 #define MOVTF_BUILTINS(INSN, COND, TARGET_FLAGS) \
10035 { CODE_FOR_mips_ ## INSN ## _cond_ps, MIPS_FP_COND_ ## COND, \
10036 "__builtin_mips_movt_" #INSN "_" #COND "_ps", \
10037 MIPS_BUILTIN_MOVT, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
10038 TARGET_FLAGS }, \
10039 { CODE_FOR_mips_ ## INSN ## _cond_ps, MIPS_FP_COND_ ## COND, \
10040 "__builtin_mips_movf_" #INSN "_" #COND "_ps", \
10041 MIPS_BUILTIN_MOVF, MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF, \
10042 TARGET_FLAGS }
10044 /* Define all the builtins related to c.cond.fmt condition COND. */
10045 #define CMP_BUILTINS(COND) \
10046 MOVTF_BUILTINS (c, COND, MASK_PAIRED_SINGLE_FLOAT), \
10047 MOVTF_BUILTINS (cabs, COND, MASK_MIPS3D), \
10048 CMP_SCALAR_BUILTINS (cabs, COND, MASK_MIPS3D), \
10049 CMP_PS_BUILTINS (c, COND, MASK_PAIRED_SINGLE_FLOAT), \
10050 CMP_PS_BUILTINS (cabs, COND, MASK_MIPS3D), \
10051 CMP_4S_BUILTINS (c, COND), \
10052 CMP_4S_BUILTINS (cabs, COND)
10054 /* __builtin_mips_abs_ps() maps to the standard absM2 pattern. */
10055 #define CODE_FOR_mips_abs_ps CODE_FOR_absv2sf2
10057 static const struct builtin_description mips_bdesc[] =
10059 DIRECT_BUILTIN (pll_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, MASK_PAIRED_SINGLE_FLOAT),
10060 DIRECT_BUILTIN (pul_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, MASK_PAIRED_SINGLE_FLOAT),
10061 DIRECT_BUILTIN (plu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, MASK_PAIRED_SINGLE_FLOAT),
10062 DIRECT_BUILTIN (puu_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, MASK_PAIRED_SINGLE_FLOAT),
10063 DIRECT_BUILTIN (cvt_ps_s, MIPS_V2SF_FTYPE_SF_SF, MASK_PAIRED_SINGLE_FLOAT),
10064 DIRECT_BUILTIN (cvt_s_pl, MIPS_SF_FTYPE_V2SF, MASK_PAIRED_SINGLE_FLOAT),
10065 DIRECT_BUILTIN (cvt_s_pu, MIPS_SF_FTYPE_V2SF, MASK_PAIRED_SINGLE_FLOAT),
10066 DIRECT_BUILTIN (abs_ps, MIPS_V2SF_FTYPE_V2SF, MASK_PAIRED_SINGLE_FLOAT),
10068 DIRECT_BUILTIN (alnv_ps, MIPS_V2SF_FTYPE_V2SF_V2SF_INT,
10069 MASK_PAIRED_SINGLE_FLOAT),
10070 DIRECT_BUILTIN (addr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, MASK_MIPS3D),
10071 DIRECT_BUILTIN (mulr_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, MASK_MIPS3D),
10072 DIRECT_BUILTIN (cvt_pw_ps, MIPS_V2SF_FTYPE_V2SF, MASK_MIPS3D),
10073 DIRECT_BUILTIN (cvt_ps_pw, MIPS_V2SF_FTYPE_V2SF, MASK_MIPS3D),
10075 DIRECT_BUILTIN (recip1_s, MIPS_SF_FTYPE_SF, MASK_MIPS3D),
10076 DIRECT_BUILTIN (recip1_d, MIPS_DF_FTYPE_DF, MASK_MIPS3D),
10077 DIRECT_BUILTIN (recip1_ps, MIPS_V2SF_FTYPE_V2SF, MASK_MIPS3D),
10078 DIRECT_BUILTIN (recip2_s, MIPS_SF_FTYPE_SF_SF, MASK_MIPS3D),
10079 DIRECT_BUILTIN (recip2_d, MIPS_DF_FTYPE_DF_DF, MASK_MIPS3D),
10080 DIRECT_BUILTIN (recip2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, MASK_MIPS3D),
10082 DIRECT_BUILTIN (rsqrt1_s, MIPS_SF_FTYPE_SF, MASK_MIPS3D),
10083 DIRECT_BUILTIN (rsqrt1_d, MIPS_DF_FTYPE_DF, MASK_MIPS3D),
10084 DIRECT_BUILTIN (rsqrt1_ps, MIPS_V2SF_FTYPE_V2SF, MASK_MIPS3D),
10085 DIRECT_BUILTIN (rsqrt2_s, MIPS_SF_FTYPE_SF_SF, MASK_MIPS3D),
10086 DIRECT_BUILTIN (rsqrt2_d, MIPS_DF_FTYPE_DF_DF, MASK_MIPS3D),
10087 DIRECT_BUILTIN (rsqrt2_ps, MIPS_V2SF_FTYPE_V2SF_V2SF, MASK_MIPS3D),
10089 MIPS_FP_CONDITIONS (CMP_BUILTINS)
10092 /* Builtin functions for the SB-1 processor. */
10094 #define CODE_FOR_mips_sqrt_ps CODE_FOR_sqrtv2sf2
10096 static const struct builtin_description sb1_bdesc[] =
10098 DIRECT_BUILTIN (sqrt_ps, MIPS_V2SF_FTYPE_V2SF, MASK_PAIRED_SINGLE_FLOAT)
10101 /* Builtin functions for DSP ASE. */
10103 #define CODE_FOR_mips_addq_ph CODE_FOR_addv2hi3
10104 #define CODE_FOR_mips_addu_qb CODE_FOR_addv4qi3
10105 #define CODE_FOR_mips_subq_ph CODE_FOR_subv2hi3
10106 #define CODE_FOR_mips_subu_qb CODE_FOR_subv4qi3
10108 /* Define a MIPS_BUILTIN_DIRECT_NO_TARGET function for instruction
10109 CODE_FOR_mips_<INSN>. FUNCTION_TYPE and TARGET_FLAGS are
10110 builtin_description fields. */
10111 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, TARGET_FLAGS) \
10112 { CODE_FOR_mips_ ## INSN, 0, "__builtin_mips_" #INSN, \
10113 MIPS_BUILTIN_DIRECT_NO_TARGET, FUNCTION_TYPE, TARGET_FLAGS }
10115 /* Define __builtin_mips_bposge<VALUE>. <VALUE> is 32 for the MIPS32 DSP
10116 branch instruction. TARGET_FLAGS is a builtin_description field. */
10117 #define BPOSGE_BUILTIN(VALUE, TARGET_FLAGS) \
10118 { CODE_FOR_mips_bposge, 0, "__builtin_mips_bposge" #VALUE, \
10119 MIPS_BUILTIN_BPOSGE ## VALUE, MIPS_SI_FTYPE_VOID, TARGET_FLAGS }
10121 static const struct builtin_description dsp_bdesc[] =
10123 DIRECT_BUILTIN (addq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, MASK_DSP),
10124 DIRECT_BUILTIN (addq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, MASK_DSP),
10125 DIRECT_BUILTIN (addq_s_w, MIPS_SI_FTYPE_SI_SI, MASK_DSP),
10126 DIRECT_BUILTIN (addu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, MASK_DSP),
10127 DIRECT_BUILTIN (addu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, MASK_DSP),
10128 DIRECT_BUILTIN (subq_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, MASK_DSP),
10129 DIRECT_BUILTIN (subq_s_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, MASK_DSP),
10130 DIRECT_BUILTIN (subq_s_w, MIPS_SI_FTYPE_SI_SI, MASK_DSP),
10131 DIRECT_BUILTIN (subu_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, MASK_DSP),
10132 DIRECT_BUILTIN (subu_s_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, MASK_DSP),
10133 DIRECT_BUILTIN (addsc, MIPS_SI_FTYPE_SI_SI, MASK_DSP),
10134 DIRECT_BUILTIN (addwc, MIPS_SI_FTYPE_SI_SI, MASK_DSP),
10135 DIRECT_BUILTIN (modsub, MIPS_SI_FTYPE_SI_SI, MASK_DSP),
10136 DIRECT_BUILTIN (raddu_w_qb, MIPS_SI_FTYPE_V4QI, MASK_DSP),
10137 DIRECT_BUILTIN (absq_s_ph, MIPS_V2HI_FTYPE_V2HI, MASK_DSP),
10138 DIRECT_BUILTIN (absq_s_w, MIPS_SI_FTYPE_SI, MASK_DSP),
10139 DIRECT_BUILTIN (precrq_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, MASK_DSP),
10140 DIRECT_BUILTIN (precrq_ph_w, MIPS_V2HI_FTYPE_SI_SI, MASK_DSP),
10141 DIRECT_BUILTIN (precrq_rs_ph_w, MIPS_V2HI_FTYPE_SI_SI, MASK_DSP),
10142 DIRECT_BUILTIN (precrqu_s_qb_ph, MIPS_V4QI_FTYPE_V2HI_V2HI, MASK_DSP),
10143 DIRECT_BUILTIN (preceq_w_phl, MIPS_SI_FTYPE_V2HI, MASK_DSP),
10144 DIRECT_BUILTIN (preceq_w_phr, MIPS_SI_FTYPE_V2HI, MASK_DSP),
10145 DIRECT_BUILTIN (precequ_ph_qbl, MIPS_V2HI_FTYPE_V4QI, MASK_DSP),
10146 DIRECT_BUILTIN (precequ_ph_qbr, MIPS_V2HI_FTYPE_V4QI, MASK_DSP),
10147 DIRECT_BUILTIN (precequ_ph_qbla, MIPS_V2HI_FTYPE_V4QI, MASK_DSP),
10148 DIRECT_BUILTIN (precequ_ph_qbra, MIPS_V2HI_FTYPE_V4QI, MASK_DSP),
10149 DIRECT_BUILTIN (preceu_ph_qbl, MIPS_V2HI_FTYPE_V4QI, MASK_DSP),
10150 DIRECT_BUILTIN (preceu_ph_qbr, MIPS_V2HI_FTYPE_V4QI, MASK_DSP),
10151 DIRECT_BUILTIN (preceu_ph_qbla, MIPS_V2HI_FTYPE_V4QI, MASK_DSP),
10152 DIRECT_BUILTIN (preceu_ph_qbra, MIPS_V2HI_FTYPE_V4QI, MASK_DSP),
10153 DIRECT_BUILTIN (shll_qb, MIPS_V4QI_FTYPE_V4QI_SI, MASK_DSP),
10154 DIRECT_BUILTIN (shll_ph, MIPS_V2HI_FTYPE_V2HI_SI, MASK_DSP),
10155 DIRECT_BUILTIN (shll_s_ph, MIPS_V2HI_FTYPE_V2HI_SI, MASK_DSP),
10156 DIRECT_BUILTIN (shll_s_w, MIPS_SI_FTYPE_SI_SI, MASK_DSP),
10157 DIRECT_BUILTIN (shrl_qb, MIPS_V4QI_FTYPE_V4QI_SI, MASK_DSP),
10158 DIRECT_BUILTIN (shra_ph, MIPS_V2HI_FTYPE_V2HI_SI, MASK_DSP),
10159 DIRECT_BUILTIN (shra_r_ph, MIPS_V2HI_FTYPE_V2HI_SI, MASK_DSP),
10160 DIRECT_BUILTIN (shra_r_w, MIPS_SI_FTYPE_SI_SI, MASK_DSP),
10161 DIRECT_BUILTIN (muleu_s_ph_qbl, MIPS_V2HI_FTYPE_V4QI_V2HI, MASK_DSP),
10162 DIRECT_BUILTIN (muleu_s_ph_qbr, MIPS_V2HI_FTYPE_V4QI_V2HI, MASK_DSP),
10163 DIRECT_BUILTIN (mulq_rs_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, MASK_DSP),
10164 DIRECT_BUILTIN (muleq_s_w_phl, MIPS_SI_FTYPE_V2HI_V2HI, MASK_DSP),
10165 DIRECT_BUILTIN (muleq_s_w_phr, MIPS_SI_FTYPE_V2HI_V2HI, MASK_DSP),
10166 DIRECT_BUILTIN (dpau_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, MASK_DSP),
10167 DIRECT_BUILTIN (dpau_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, MASK_DSP),
10168 DIRECT_BUILTIN (dpsu_h_qbl, MIPS_DI_FTYPE_DI_V4QI_V4QI, MASK_DSP),
10169 DIRECT_BUILTIN (dpsu_h_qbr, MIPS_DI_FTYPE_DI_V4QI_V4QI, MASK_DSP),
10170 DIRECT_BUILTIN (dpaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, MASK_DSP),
10171 DIRECT_BUILTIN (dpsq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, MASK_DSP),
10172 DIRECT_BUILTIN (mulsaq_s_w_ph, MIPS_DI_FTYPE_DI_V2HI_V2HI, MASK_DSP),
10173 DIRECT_BUILTIN (dpaq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, MASK_DSP),
10174 DIRECT_BUILTIN (dpsq_sa_l_w, MIPS_DI_FTYPE_DI_SI_SI, MASK_DSP),
10175 DIRECT_BUILTIN (maq_s_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, MASK_DSP),
10176 DIRECT_BUILTIN (maq_s_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, MASK_DSP),
10177 DIRECT_BUILTIN (maq_sa_w_phl, MIPS_DI_FTYPE_DI_V2HI_V2HI, MASK_DSP),
10178 DIRECT_BUILTIN (maq_sa_w_phr, MIPS_DI_FTYPE_DI_V2HI_V2HI, MASK_DSP),
10179 DIRECT_BUILTIN (bitrev, MIPS_SI_FTYPE_SI, MASK_DSP),
10180 DIRECT_BUILTIN (insv, MIPS_SI_FTYPE_SI_SI, MASK_DSP),
10181 DIRECT_BUILTIN (repl_qb, MIPS_V4QI_FTYPE_SI, MASK_DSP),
10182 DIRECT_BUILTIN (repl_ph, MIPS_V2HI_FTYPE_SI, MASK_DSP),
10183 DIRECT_NO_TARGET_BUILTIN (cmpu_eq_qb, MIPS_VOID_FTYPE_V4QI_V4QI, MASK_DSP),
10184 DIRECT_NO_TARGET_BUILTIN (cmpu_lt_qb, MIPS_VOID_FTYPE_V4QI_V4QI, MASK_DSP),
10185 DIRECT_NO_TARGET_BUILTIN (cmpu_le_qb, MIPS_VOID_FTYPE_V4QI_V4QI, MASK_DSP),
10186 DIRECT_BUILTIN (cmpgu_eq_qb, MIPS_SI_FTYPE_V4QI_V4QI, MASK_DSP),
10187 DIRECT_BUILTIN (cmpgu_lt_qb, MIPS_SI_FTYPE_V4QI_V4QI, MASK_DSP),
10188 DIRECT_BUILTIN (cmpgu_le_qb, MIPS_SI_FTYPE_V4QI_V4QI, MASK_DSP),
10189 DIRECT_NO_TARGET_BUILTIN (cmp_eq_ph, MIPS_VOID_FTYPE_V2HI_V2HI, MASK_DSP),
10190 DIRECT_NO_TARGET_BUILTIN (cmp_lt_ph, MIPS_VOID_FTYPE_V2HI_V2HI, MASK_DSP),
10191 DIRECT_NO_TARGET_BUILTIN (cmp_le_ph, MIPS_VOID_FTYPE_V2HI_V2HI, MASK_DSP),
10192 DIRECT_BUILTIN (pick_qb, MIPS_V4QI_FTYPE_V4QI_V4QI, MASK_DSP),
10193 DIRECT_BUILTIN (pick_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, MASK_DSP),
10194 DIRECT_BUILTIN (packrl_ph, MIPS_V2HI_FTYPE_V2HI_V2HI, MASK_DSP),
10195 DIRECT_BUILTIN (extr_w, MIPS_SI_FTYPE_DI_SI, MASK_DSP),
10196 DIRECT_BUILTIN (extr_r_w, MIPS_SI_FTYPE_DI_SI, MASK_DSP),
10197 DIRECT_BUILTIN (extr_rs_w, MIPS_SI_FTYPE_DI_SI, MASK_DSP),
10198 DIRECT_BUILTIN (extr_s_h, MIPS_SI_FTYPE_DI_SI, MASK_DSP),
10199 DIRECT_BUILTIN (extp, MIPS_SI_FTYPE_DI_SI, MASK_DSP),
10200 DIRECT_BUILTIN (extpdp, MIPS_SI_FTYPE_DI_SI, MASK_DSP),
10201 DIRECT_BUILTIN (shilo, MIPS_DI_FTYPE_DI_SI, MASK_DSP),
10202 DIRECT_BUILTIN (mthlip, MIPS_DI_FTYPE_DI_SI, MASK_DSP),
10203 DIRECT_NO_TARGET_BUILTIN (wrdsp, MIPS_VOID_FTYPE_SI_SI, MASK_DSP),
10204 DIRECT_BUILTIN (rddsp, MIPS_SI_FTYPE_SI, MASK_DSP),
10205 DIRECT_BUILTIN (lbux, MIPS_SI_FTYPE_PTR_SI, MASK_DSP),
10206 DIRECT_BUILTIN (lhx, MIPS_SI_FTYPE_PTR_SI, MASK_DSP),
10207 DIRECT_BUILTIN (lwx, MIPS_SI_FTYPE_PTR_SI, MASK_DSP),
10208 BPOSGE_BUILTIN (32, MASK_DSP)
10211 /* This helps provide a mapping from builtin function codes to bdesc
10212 arrays. */
10214 struct bdesc_map
10216 /* The builtin function table that this entry describes. */
10217 const struct builtin_description *bdesc;
10219 /* The number of entries in the builtin function table. */
10220 unsigned int size;
10222 /* The target processor that supports these builtin functions.
10223 PROCESSOR_MAX means we enable them for all processors. */
10224 enum processor_type proc;
10227 static const struct bdesc_map bdesc_arrays[] =
10229 { mips_bdesc, ARRAY_SIZE (mips_bdesc), PROCESSOR_MAX },
10230 { sb1_bdesc, ARRAY_SIZE (sb1_bdesc), PROCESSOR_SB1 },
10231 { dsp_bdesc, ARRAY_SIZE (dsp_bdesc), PROCESSOR_MAX }
10234 /* Take the head of argument list *ARGLIST and convert it into a form
10235 suitable for input operand OP of instruction ICODE. Return the value
10236 and point *ARGLIST at the next element of the list. */
10238 static rtx
10239 mips_prepare_builtin_arg (enum insn_code icode,
10240 unsigned int op, tree *arglist)
10242 rtx value;
10243 enum machine_mode mode;
10245 value = expand_expr (TREE_VALUE (*arglist), NULL_RTX, VOIDmode, 0);
10246 mode = insn_data[icode].operand[op].mode;
10247 if (!insn_data[icode].operand[op].predicate (value, mode))
10249 value = copy_to_mode_reg (mode, value);
10250 /* Check the predicate again. */
10251 if (!insn_data[icode].operand[op].predicate (value, mode))
10253 error ("invalid argument to builtin function");
10254 return const0_rtx;
10258 *arglist = TREE_CHAIN (*arglist);
10259 return value;
10262 /* Return an rtx suitable for output operand OP of instruction ICODE.
10263 If TARGET is non-null, try to use it where possible. */
10265 static rtx
10266 mips_prepare_builtin_target (enum insn_code icode, unsigned int op, rtx target)
10268 enum machine_mode mode;
10270 mode = insn_data[icode].operand[op].mode;
10271 if (target == 0 || !insn_data[icode].operand[op].predicate (target, mode))
10272 target = gen_reg_rtx (mode);
10274 return target;
10277 /* Expand builtin functions. This is called from TARGET_EXPAND_BUILTIN. */
10280 mips_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
10281 enum machine_mode mode ATTRIBUTE_UNUSED,
10282 int ignore ATTRIBUTE_UNUSED)
10284 enum insn_code icode;
10285 enum mips_builtin_type type;
10286 tree fndecl, arglist;
10287 unsigned int fcode;
10288 const struct builtin_description *bdesc;
10289 const struct bdesc_map *m;
10291 fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
10292 arglist = TREE_OPERAND (exp, 1);
10293 fcode = DECL_FUNCTION_CODE (fndecl);
10295 bdesc = NULL;
10296 for (m = bdesc_arrays; m < &bdesc_arrays[ARRAY_SIZE (bdesc_arrays)]; m++)
10298 if (fcode < m->size)
10300 bdesc = m->bdesc;
10301 icode = bdesc[fcode].icode;
10302 type = bdesc[fcode].builtin_type;
10303 break;
10305 fcode -= m->size;
10307 if (bdesc == NULL)
10308 return 0;
10310 switch (type)
10312 case MIPS_BUILTIN_DIRECT:
10313 return mips_expand_builtin_direct (icode, target, arglist, true);
10315 case MIPS_BUILTIN_DIRECT_NO_TARGET:
10316 return mips_expand_builtin_direct (icode, target, arglist, false);
10318 case MIPS_BUILTIN_MOVT:
10319 case MIPS_BUILTIN_MOVF:
10320 return mips_expand_builtin_movtf (type, icode, bdesc[fcode].cond,
10321 target, arglist);
10323 case MIPS_BUILTIN_CMP_ANY:
10324 case MIPS_BUILTIN_CMP_ALL:
10325 case MIPS_BUILTIN_CMP_UPPER:
10326 case MIPS_BUILTIN_CMP_LOWER:
10327 case MIPS_BUILTIN_CMP_SINGLE:
10328 return mips_expand_builtin_compare (type, icode, bdesc[fcode].cond,
10329 target, arglist);
10331 case MIPS_BUILTIN_BPOSGE32:
10332 return mips_expand_builtin_bposge (type, target);
10334 default:
10335 return 0;
10339 /* Init builtin functions. This is called from TARGET_INIT_BUILTIN. */
10341 void
10342 mips_init_builtins (void)
10344 const struct builtin_description *d;
10345 const struct bdesc_map *m;
10346 tree types[(int) MIPS_MAX_FTYPE_MAX];
10347 tree V2SF_type_node;
10348 tree V2HI_type_node;
10349 tree V4QI_type_node;
10350 unsigned int offset;
10352 /* We have only builtins for -mpaired-single, -mips3d and -mdsp. */
10353 if (!TARGET_PAIRED_SINGLE_FLOAT && !TARGET_DSP)
10354 return;
10356 if (TARGET_PAIRED_SINGLE_FLOAT)
10358 V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
10360 types[MIPS_V2SF_FTYPE_V2SF]
10361 = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
10363 types[MIPS_V2SF_FTYPE_V2SF_V2SF]
10364 = build_function_type_list (V2SF_type_node,
10365 V2SF_type_node, V2SF_type_node, NULL_TREE);
10367 types[MIPS_V2SF_FTYPE_V2SF_V2SF_INT]
10368 = build_function_type_list (V2SF_type_node,
10369 V2SF_type_node, V2SF_type_node,
10370 integer_type_node, NULL_TREE);
10372 types[MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF]
10373 = build_function_type_list (V2SF_type_node,
10374 V2SF_type_node, V2SF_type_node,
10375 V2SF_type_node, V2SF_type_node, NULL_TREE);
10377 types[MIPS_V2SF_FTYPE_SF_SF]
10378 = build_function_type_list (V2SF_type_node,
10379 float_type_node, float_type_node, NULL_TREE);
10381 types[MIPS_INT_FTYPE_V2SF_V2SF]
10382 = build_function_type_list (integer_type_node,
10383 V2SF_type_node, V2SF_type_node, NULL_TREE);
10385 types[MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF]
10386 = build_function_type_list (integer_type_node,
10387 V2SF_type_node, V2SF_type_node,
10388 V2SF_type_node, V2SF_type_node, NULL_TREE);
10390 types[MIPS_INT_FTYPE_SF_SF]
10391 = build_function_type_list (integer_type_node,
10392 float_type_node, float_type_node, NULL_TREE);
10394 types[MIPS_INT_FTYPE_DF_DF]
10395 = build_function_type_list (integer_type_node,
10396 double_type_node, double_type_node, NULL_TREE);
10398 types[MIPS_SF_FTYPE_V2SF]
10399 = build_function_type_list (float_type_node, V2SF_type_node, NULL_TREE);
10401 types[MIPS_SF_FTYPE_SF]
10402 = build_function_type_list (float_type_node,
10403 float_type_node, NULL_TREE);
10405 types[MIPS_SF_FTYPE_SF_SF]
10406 = build_function_type_list (float_type_node,
10407 float_type_node, float_type_node, NULL_TREE);
10409 types[MIPS_DF_FTYPE_DF]
10410 = build_function_type_list (double_type_node,
10411 double_type_node, NULL_TREE);
10413 types[MIPS_DF_FTYPE_DF_DF]
10414 = build_function_type_list (double_type_node,
10415 double_type_node, double_type_node, NULL_TREE);
10418 if (TARGET_DSP)
10420 V2HI_type_node = build_vector_type_for_mode (intHI_type_node, V2HImode);
10421 V4QI_type_node = build_vector_type_for_mode (intQI_type_node, V4QImode);
10423 types[MIPS_V2HI_FTYPE_V2HI_V2HI]
10424 = build_function_type_list (V2HI_type_node,
10425 V2HI_type_node, V2HI_type_node,
10426 NULL_TREE);
10428 types[MIPS_SI_FTYPE_SI_SI]
10429 = build_function_type_list (intSI_type_node,
10430 intSI_type_node, intSI_type_node,
10431 NULL_TREE);
10433 types[MIPS_V4QI_FTYPE_V4QI_V4QI]
10434 = build_function_type_list (V4QI_type_node,
10435 V4QI_type_node, V4QI_type_node,
10436 NULL_TREE);
10438 types[MIPS_SI_FTYPE_V4QI]
10439 = build_function_type_list (intSI_type_node,
10440 V4QI_type_node,
10441 NULL_TREE);
10443 types[MIPS_V2HI_FTYPE_V2HI]
10444 = build_function_type_list (V2HI_type_node,
10445 V2HI_type_node,
10446 NULL_TREE);
10448 types[MIPS_SI_FTYPE_SI]
10449 = build_function_type_list (intSI_type_node,
10450 intSI_type_node,
10451 NULL_TREE);
10453 types[MIPS_V4QI_FTYPE_V2HI_V2HI]
10454 = build_function_type_list (V4QI_type_node,
10455 V2HI_type_node, V2HI_type_node,
10456 NULL_TREE);
10458 types[MIPS_V2HI_FTYPE_SI_SI]
10459 = build_function_type_list (V2HI_type_node,
10460 intSI_type_node, intSI_type_node,
10461 NULL_TREE);
10463 types[MIPS_SI_FTYPE_V2HI]
10464 = build_function_type_list (intSI_type_node,
10465 V2HI_type_node,
10466 NULL_TREE);
10468 types[MIPS_V2HI_FTYPE_V4QI]
10469 = build_function_type_list (V2HI_type_node,
10470 V4QI_type_node,
10471 NULL_TREE);
10473 types[MIPS_V4QI_FTYPE_V4QI_SI]
10474 = build_function_type_list (V4QI_type_node,
10475 V4QI_type_node, intSI_type_node,
10476 NULL_TREE);
10478 types[MIPS_V2HI_FTYPE_V2HI_SI]
10479 = build_function_type_list (V2HI_type_node,
10480 V2HI_type_node, intSI_type_node,
10481 NULL_TREE);
10483 types[MIPS_V2HI_FTYPE_V4QI_V2HI]
10484 = build_function_type_list (V2HI_type_node,
10485 V4QI_type_node, V2HI_type_node,
10486 NULL_TREE);
10488 types[MIPS_SI_FTYPE_V2HI_V2HI]
10489 = build_function_type_list (intSI_type_node,
10490 V2HI_type_node, V2HI_type_node,
10491 NULL_TREE);
10493 types[MIPS_DI_FTYPE_DI_V4QI_V4QI]
10494 = build_function_type_list (intDI_type_node,
10495 intDI_type_node, V4QI_type_node, V4QI_type_node,
10496 NULL_TREE);
10498 types[MIPS_DI_FTYPE_DI_V2HI_V2HI]
10499 = build_function_type_list (intDI_type_node,
10500 intDI_type_node, V2HI_type_node, V2HI_type_node,
10501 NULL_TREE);
10503 types[MIPS_DI_FTYPE_DI_SI_SI]
10504 = build_function_type_list (intDI_type_node,
10505 intDI_type_node, intSI_type_node, intSI_type_node,
10506 NULL_TREE);
10508 types[MIPS_V4QI_FTYPE_SI]
10509 = build_function_type_list (V4QI_type_node,
10510 intSI_type_node,
10511 NULL_TREE);
10513 types[MIPS_V2HI_FTYPE_SI]
10514 = build_function_type_list (V2HI_type_node,
10515 intSI_type_node,
10516 NULL_TREE);
10518 types[MIPS_VOID_FTYPE_V4QI_V4QI]
10519 = build_function_type_list (void_type_node,
10520 V4QI_type_node, V4QI_type_node,
10521 NULL_TREE);
10523 types[MIPS_SI_FTYPE_V4QI_V4QI]
10524 = build_function_type_list (intSI_type_node,
10525 V4QI_type_node, V4QI_type_node,
10526 NULL_TREE);
10528 types[MIPS_VOID_FTYPE_V2HI_V2HI]
10529 = build_function_type_list (void_type_node,
10530 V2HI_type_node, V2HI_type_node,
10531 NULL_TREE);
10533 types[MIPS_SI_FTYPE_DI_SI]
10534 = build_function_type_list (intSI_type_node,
10535 intDI_type_node, intSI_type_node,
10536 NULL_TREE);
10538 types[MIPS_DI_FTYPE_DI_SI]
10539 = build_function_type_list (intDI_type_node,
10540 intDI_type_node, intSI_type_node,
10541 NULL_TREE);
10543 types[MIPS_VOID_FTYPE_SI_SI]
10544 = build_function_type_list (void_type_node,
10545 intSI_type_node, intSI_type_node,
10546 NULL_TREE);
10548 types[MIPS_SI_FTYPE_PTR_SI]
10549 = build_function_type_list (intSI_type_node,
10550 ptr_type_node, intSI_type_node,
10551 NULL_TREE);
10553 types[MIPS_SI_FTYPE_VOID]
10554 = build_function_type (intSI_type_node, void_list_node);
10557 /* Iterate through all of the bdesc arrays, initializing all of the
10558 builtin functions. */
10560 offset = 0;
10561 for (m = bdesc_arrays; m < &bdesc_arrays[ARRAY_SIZE (bdesc_arrays)]; m++)
10563 if (m->proc == PROCESSOR_MAX || (m->proc == mips_arch))
10564 for (d = m->bdesc; d < &m->bdesc[m->size]; d++)
10565 if ((d->target_flags & target_flags) == d->target_flags)
10566 lang_hooks.builtin_function (d->name, types[d->function_type],
10567 d - m->bdesc + offset,
10568 BUILT_IN_MD, NULL, NULL);
10569 offset += m->size;
10573 /* Expand a MIPS_BUILTIN_DIRECT function. ICODE is the code of the
10574 .md pattern and ARGLIST is the list of function arguments. TARGET,
10575 if nonnull, suggests a good place to put the result.
10576 HAS_TARGET indicates the function must return something. */
10578 static rtx
10579 mips_expand_builtin_direct (enum insn_code icode, rtx target, tree arglist,
10580 bool has_target)
10582 rtx ops[MAX_RECOG_OPERANDS];
10583 int i = 0;
10585 if (has_target)
10587 /* We save target to ops[0]. */
10588 ops[0] = mips_prepare_builtin_target (icode, 0, target);
10589 i = 1;
10592 /* We need to test if arglist is not zero. Some instructions have extra
10593 clobber registers. */
10594 for (; i < insn_data[icode].n_operands && arglist != 0; i++)
10595 ops[i] = mips_prepare_builtin_arg (icode, i, &arglist);
10597 switch (i)
10599 case 2:
10600 emit_insn (GEN_FCN (icode) (ops[0], ops[1]));
10601 break;
10603 case 3:
10604 emit_insn (GEN_FCN (icode) (ops[0], ops[1], ops[2]));
10605 break;
10607 case 4:
10608 emit_insn (GEN_FCN (icode) (ops[0], ops[1], ops[2], ops[3]));
10609 break;
10611 default:
10612 gcc_unreachable ();
10614 return target;
10617 /* Expand a __builtin_mips_movt_*_ps() or __builtin_mips_movf_*_ps()
10618 function (TYPE says which). ARGLIST is the list of arguments to the
10619 function, ICODE is the instruction that should be used to compare
10620 the first two arguments, and COND is the condition it should test.
10621 TARGET, if nonnull, suggests a good place to put the result. */
10623 static rtx
10624 mips_expand_builtin_movtf (enum mips_builtin_type type,
10625 enum insn_code icode, enum mips_fp_condition cond,
10626 rtx target, tree arglist)
10628 rtx cmp_result, op0, op1;
10630 cmp_result = mips_prepare_builtin_target (icode, 0, 0);
10631 op0 = mips_prepare_builtin_arg (icode, 1, &arglist);
10632 op1 = mips_prepare_builtin_arg (icode, 2, &arglist);
10633 emit_insn (GEN_FCN (icode) (cmp_result, op0, op1, GEN_INT (cond)));
10635 icode = CODE_FOR_mips_cond_move_tf_ps;
10636 target = mips_prepare_builtin_target (icode, 0, target);
10637 if (type == MIPS_BUILTIN_MOVT)
10639 op1 = mips_prepare_builtin_arg (icode, 2, &arglist);
10640 op0 = mips_prepare_builtin_arg (icode, 1, &arglist);
10642 else
10644 op0 = mips_prepare_builtin_arg (icode, 1, &arglist);
10645 op1 = mips_prepare_builtin_arg (icode, 2, &arglist);
10647 emit_insn (gen_mips_cond_move_tf_ps (target, op0, op1, cmp_result));
10648 return target;
10651 /* Expand a comparison builtin of type BUILTIN_TYPE. ICODE is the code
10652 of the comparison instruction and COND is the condition it should test.
10653 ARGLIST is the list of function arguments and TARGET, if nonnull,
10654 suggests a good place to put the boolean result. */
10656 static rtx
10657 mips_expand_builtin_compare (enum mips_builtin_type builtin_type,
10658 enum insn_code icode, enum mips_fp_condition cond,
10659 rtx target, tree arglist)
10661 rtx label1, label2, if_then_else;
10662 rtx pat, cmp_result, ops[MAX_RECOG_OPERANDS];
10663 rtx target_if_equal, target_if_unequal;
10664 int cmp_value, i;
10666 if (target == 0 || GET_MODE (target) != SImode)
10667 target = gen_reg_rtx (SImode);
10669 /* Prepare the operands to the comparison. */
10670 cmp_result = mips_prepare_builtin_target (icode, 0, 0);
10671 for (i = 1; i < insn_data[icode].n_operands - 1; i++)
10672 ops[i] = mips_prepare_builtin_arg (icode, i, &arglist);
10674 switch (insn_data[icode].n_operands)
10676 case 4:
10677 pat = GEN_FCN (icode) (cmp_result, ops[1], ops[2], GEN_INT (cond));
10678 break;
10680 case 6:
10681 pat = GEN_FCN (icode) (cmp_result, ops[1], ops[2],
10682 ops[3], ops[4], GEN_INT (cond));
10683 break;
10685 default:
10686 gcc_unreachable ();
10689 /* If the comparison sets more than one register, we define the result
10690 to be 0 if all registers are false and -1 if all registers are true.
10691 The value of the complete result is indeterminate otherwise. It is
10692 possible to test individual registers using SUBREGs.
10694 Set up CMP_RESULT, CMP_VALUE, TARGET_IF_EQUAL and TARGET_IF_UNEQUAL so
10695 that the result should be TARGET_IF_EQUAL if (EQ CMP_RESULT CMP_VALUE)
10696 and TARGET_IF_UNEQUAL otherwise. */
10697 if (builtin_type == MIPS_BUILTIN_CMP_ALL)
10699 cmp_value = -1;
10700 target_if_equal = const1_rtx;
10701 target_if_unequal = const0_rtx;
10703 else
10705 cmp_value = 0;
10706 target_if_equal = const0_rtx;
10707 target_if_unequal = const1_rtx;
10708 if (builtin_type == MIPS_BUILTIN_CMP_UPPER)
10709 cmp_result = simplify_gen_subreg (CCmode, cmp_result, CCV2mode, 4);
10710 else if (builtin_type == MIPS_BUILTIN_CMP_LOWER)
10711 cmp_result = simplify_gen_subreg (CCmode, cmp_result, CCV2mode, 0);
10714 /* First assume that CMP_RESULT == CMP_VALUE. */
10715 emit_move_insn (target, target_if_equal);
10717 /* Branch to LABEL1 if CMP_RESULT != CMP_VALUE. */
10718 emit_insn (pat);
10719 label1 = gen_label_rtx ();
10720 label2 = gen_label_rtx ();
10721 if_then_else
10722 = gen_rtx_IF_THEN_ELSE (VOIDmode,
10723 gen_rtx_fmt_ee (NE, GET_MODE (cmp_result),
10724 cmp_result, GEN_INT (cmp_value)),
10725 gen_rtx_LABEL_REF (VOIDmode, label1), pc_rtx);
10726 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, if_then_else));
10727 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
10728 gen_rtx_LABEL_REF (VOIDmode, label2)));
10729 emit_barrier ();
10730 emit_label (label1);
10732 /* Fix TARGET for CMP_RESULT != CMP_VALUE. */
10733 emit_move_insn (target, target_if_unequal);
10734 emit_label (label2);
10736 return target;
10739 /* Expand a bposge builtin of type BUILTIN_TYPE. TARGET, if nonnull,
10740 suggests a good place to put the boolean result.
10742 The sequence we want is
10744 li target, 0
10745 bposge* label1
10746 j label2
10747 label1:
10748 li target, 1
10749 label2: */
10751 static rtx
10752 mips_expand_builtin_bposge (enum mips_builtin_type builtin_type, rtx target)
10754 rtx label1, label2, if_then_else;
10755 rtx cmp_result;
10756 int cmp_value;
10758 if (target == 0 || GET_MODE (target) != SImode)
10759 target = gen_reg_rtx (SImode);
10761 cmp_result = gen_rtx_REG (CCDSPmode, CCDSP_PO_REGNUM);
10763 if (builtin_type == MIPS_BUILTIN_BPOSGE32)
10764 cmp_value = 32;
10765 else
10766 gcc_assert (0);
10768 /* Move 0 to target */
10769 emit_move_insn (target, const0_rtx);
10771 /* Generate two labels */
10772 label1 = gen_label_rtx ();
10773 label2 = gen_label_rtx ();
10775 /* Generate if_then_else */
10776 if_then_else
10777 = gen_rtx_IF_THEN_ELSE (VOIDmode,
10778 gen_rtx_fmt_ee (GE, CCDSPmode,
10779 cmp_result, GEN_INT (cmp_value)),
10780 gen_rtx_LABEL_REF (VOIDmode, label1), pc_rtx);
10782 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, if_then_else));
10783 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx,
10784 gen_rtx_LABEL_REF (VOIDmode, label2)));
10785 emit_barrier ();
10786 emit_label (label1);
10787 emit_move_insn (target, const1_rtx);
10788 emit_label (label2);
10790 return target;
10793 /* Set SYMBOL_REF_FLAGS for the SYMBOL_REF inside RTL, which belongs to DECL.
10794 FIRST is true if this is the first time handling this decl. */
10796 static void
10797 mips_encode_section_info (tree decl, rtx rtl, int first)
10799 default_encode_section_info (decl, rtl, first);
10801 if (TREE_CODE (decl) == FUNCTION_DECL
10802 && lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
10804 rtx symbol = XEXP (rtl, 0);
10805 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LONG_CALL;
10809 #include "gt-mips.h"