OSDN Git Service

6706b83e99b3ea9626311a711423377ee9361484
[pf3gnuchains/gcc-fork.git] / gcc / config / mips / mips.c
1 /* Subroutines used for MIPS code generation.
2    Copyright (C) 1989, 1990, 1991, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005 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.
8
9 This file is part of GCC.
10
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.
15
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.
20
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.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include <signal.h>
31 #include "rtl.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "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"
59 #include "bitmap.h"
60
61 /* True if X is an unspec wrapper around a SYMBOL_REF or LABEL_REF.  */
62 #define UNSPEC_ADDRESS_P(X)                                     \
63   (GET_CODE (X) == UNSPEC                                       \
64    && XINT (X, 1) >= UNSPEC_ADDRESS_FIRST                       \
65    && XINT (X, 1) < UNSPEC_ADDRESS_FIRST + NUM_SYMBOL_TYPES)
66
67 /* Extract the symbol or label from UNSPEC wrapper X.  */
68 #define UNSPEC_ADDRESS(X) \
69   XVECEXP (X, 0, 0)
70
71 /* Extract the symbol type from UNSPEC wrapper X.  */
72 #define UNSPEC_ADDRESS_TYPE(X) \
73   ((enum mips_symbol_type) (XINT (X, 1) - UNSPEC_ADDRESS_FIRST))
74
75 /* The maximum distance between the top of the stack frame and the
76    value $sp has when we save & restore registers.
77
78    Use a maximum gap of 0x100 in the mips16 case.  We can then use
79    unextended instructions to save and restore registers, and to
80    allocate and deallocate the top part of the frame.
81
82    The value in the !mips16 case must be a SMALL_OPERAND and must
83    preserve the maximum stack alignment.  */
84 #define MIPS_MAX_FIRST_STACK_STEP (TARGET_MIPS16 ? 0x100 : 0x7ff0)
85
86 /* True if INSN is a mips.md pattern or asm statement.  */
87 #define USEFUL_INSN_P(INSN)                                             \
88   (INSN_P (INSN)                                                        \
89    && GET_CODE (PATTERN (INSN)) != USE                                  \
90    && GET_CODE (PATTERN (INSN)) != CLOBBER                              \
91    && GET_CODE (PATTERN (INSN)) != ADDR_VEC                             \
92    && GET_CODE (PATTERN (INSN)) != ADDR_DIFF_VEC)
93
94 /* If INSN is a delayed branch sequence, return the first instruction
95    in the sequence, otherwise return INSN itself.  */
96 #define SEQ_BEGIN(INSN)                                                 \
97   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE               \
98    ? XVECEXP (PATTERN (INSN), 0, 0)                                     \
99    : (INSN))
100
101 /* Likewise for the last instruction in a delayed branch sequence.  */
102 #define SEQ_END(INSN)                                                   \
103   (INSN_P (INSN) && GET_CODE (PATTERN (INSN)) == SEQUENCE               \
104    ? XVECEXP (PATTERN (INSN), 0, XVECLEN (PATTERN (INSN), 0) - 1)       \
105    : (INSN))
106
107 /* Execute the following loop body with SUBINSN set to each instruction
108    between SEQ_BEGIN (INSN) and SEQ_END (INSN) inclusive.  */
109 #define FOR_EACH_SUBINSN(SUBINSN, INSN)                                 \
110   for ((SUBINSN) = SEQ_BEGIN (INSN);                                    \
111        (SUBINSN) != NEXT_INSN (SEQ_END (INSN));                         \
112        (SUBINSN) = NEXT_INSN (SUBINSN))
113
114 /* Classifies an address.
115
116    ADDRESS_REG
117        A natural register + offset address.  The register satisfies
118        mips_valid_base_register_p and the offset is a const_arith_operand.
119
120    ADDRESS_LO_SUM
121        A LO_SUM rtx.  The first operand is a valid base register and
122        the second operand is a symbolic address.
123
124    ADDRESS_CONST_INT
125        A signed 16-bit constant address.
126
127    ADDRESS_SYMBOLIC:
128        A constant symbolic address (equivalent to CONSTANT_SYMBOLIC).  */
129 enum mips_address_type {
130   ADDRESS_REG,
131   ADDRESS_LO_SUM,
132   ADDRESS_CONST_INT,
133   ADDRESS_SYMBOLIC
134 };
135
136 /* Classifies the prototype of a builtin function.  */
137 enum mips_function_type
138 {
139   MIPS_V2SF_FTYPE_V2SF,
140   MIPS_V2SF_FTYPE_V2SF_V2SF,
141   MIPS_V2SF_FTYPE_V2SF_V2SF_INT,
142   MIPS_V2SF_FTYPE_V2SF_V2SF_V2SF_V2SF,
143   MIPS_V2SF_FTYPE_SF_SF,
144   MIPS_INT_FTYPE_V2SF_V2SF,
145   MIPS_INT_FTYPE_V2SF_V2SF_V2SF_V2SF,
146   MIPS_INT_FTYPE_SF_SF,
147   MIPS_INT_FTYPE_DF_DF,
148   MIPS_SF_FTYPE_V2SF,
149   MIPS_SF_FTYPE_SF,
150   MIPS_SF_FTYPE_SF_SF,
151   MIPS_DF_FTYPE_DF,
152   MIPS_DF_FTYPE_DF_DF,
153
154   /* For MIPS DSP ASE  */
155   MIPS_DI_FTYPE_DI_SI,
156   MIPS_DI_FTYPE_DI_SI_SI,
157   MIPS_DI_FTYPE_DI_V2HI_V2HI,
158   MIPS_DI_FTYPE_DI_V4QI_V4QI,
159   MIPS_SI_FTYPE_DI_SI,
160   MIPS_SI_FTYPE_PTR_SI,
161   MIPS_SI_FTYPE_SI,
162   MIPS_SI_FTYPE_SI_SI,
163   MIPS_SI_FTYPE_V2HI,
164   MIPS_SI_FTYPE_V2HI_V2HI,
165   MIPS_SI_FTYPE_V4QI,
166   MIPS_SI_FTYPE_V4QI_V4QI,
167   MIPS_SI_FTYPE_VOID,
168   MIPS_V2HI_FTYPE_SI,
169   MIPS_V2HI_FTYPE_SI_SI,
170   MIPS_V2HI_FTYPE_V2HI,
171   MIPS_V2HI_FTYPE_V2HI_SI,
172   MIPS_V2HI_FTYPE_V2HI_V2HI,
173   MIPS_V2HI_FTYPE_V4QI,
174   MIPS_V2HI_FTYPE_V4QI_V2HI,
175   MIPS_V4QI_FTYPE_SI,
176   MIPS_V4QI_FTYPE_V2HI_V2HI,
177   MIPS_V4QI_FTYPE_V4QI_SI,
178   MIPS_V4QI_FTYPE_V4QI_V4QI,
179   MIPS_VOID_FTYPE_SI_SI,
180   MIPS_VOID_FTYPE_V2HI_V2HI,
181   MIPS_VOID_FTYPE_V4QI_V4QI,
182
183   /* The last type.  */
184   MIPS_MAX_FTYPE_MAX
185 };
186
187 /* Specifies how a builtin function should be converted into rtl.  */
188 enum mips_builtin_type
189 {
190   /* The builtin corresponds directly to an .md pattern.  The return
191      value is mapped to operand 0 and the arguments are mapped to
192      operands 1 and above.  */
193   MIPS_BUILTIN_DIRECT,
194
195   /* The builtin corresponds directly to an .md pattern.  There is no return
196      value and the arguments are mapped to operands 0 and above.  */
197   MIPS_BUILTIN_DIRECT_NO_TARGET,
198
199   /* The builtin corresponds to a comparison instruction followed by
200      a mips_cond_move_tf_ps pattern.  The first two arguments are the
201      values to compare and the second two arguments are the vector
202      operands for the movt.ps or movf.ps instruction (in assembly order).  */
203   MIPS_BUILTIN_MOVF,
204   MIPS_BUILTIN_MOVT,
205
206   /* The builtin corresponds to a V2SF comparison instruction.  Operand 0
207      of this instruction is the result of the comparison, which has mode
208      CCV2 or CCV4.  The function arguments are mapped to operands 1 and
209      above.  The function's return value is an SImode boolean that is
210      true under the following conditions:
211
212      MIPS_BUILTIN_CMP_ANY: one of the registers is true
213      MIPS_BUILTIN_CMP_ALL: all of the registers are true
214      MIPS_BUILTIN_CMP_LOWER: the first register is true
215      MIPS_BUILTIN_CMP_UPPER: the second register is true.  */
216   MIPS_BUILTIN_CMP_ANY,
217   MIPS_BUILTIN_CMP_ALL,
218   MIPS_BUILTIN_CMP_UPPER,
219   MIPS_BUILTIN_CMP_LOWER,
220
221   /* As above, but the instruction only sets a single $fcc register.  */
222   MIPS_BUILTIN_CMP_SINGLE,
223
224   /* For generating bposge32 branch instructions in MIPS32 DSP ASE.  */
225   MIPS_BUILTIN_BPOSGE32
226 };
227
228 /* Invokes MACRO (COND) for each c.cond.fmt condition.  */
229 #define MIPS_FP_CONDITIONS(MACRO) \
230   MACRO (f),    \
231   MACRO (un),   \
232   MACRO (eq),   \
233   MACRO (ueq),  \
234   MACRO (olt),  \
235   MACRO (ult),  \
236   MACRO (ole),  \
237   MACRO (ule),  \
238   MACRO (sf),   \
239   MACRO (ngle), \
240   MACRO (seq),  \
241   MACRO (ngl),  \
242   MACRO (lt),   \
243   MACRO (nge),  \
244   MACRO (le),   \
245   MACRO (ngt)
246
247 /* Enumerates the codes above as MIPS_FP_COND_<X>.  */
248 #define DECLARE_MIPS_COND(X) MIPS_FP_COND_ ## X
249 enum mips_fp_condition {
250   MIPS_FP_CONDITIONS (DECLARE_MIPS_COND)
251 };
252
253 /* Index X provides the string representation of MIPS_FP_COND_<X>.  */
254 #define STRINGIFY(X) #X
255 static const char *const mips_fp_conditions[] = {
256   MIPS_FP_CONDITIONS (STRINGIFY)
257 };
258
259 /* A function to save or store a register.  The first argument is the
260    register and the second is the stack slot.  */
261 typedef void (*mips_save_restore_fn) (rtx, rtx);
262
263 struct mips16_constant;
264 struct mips_arg_info;
265 struct mips_address_info;
266 struct mips_integer_op;
267 struct mips_sim;
268
269 static enum mips_symbol_type mips_classify_symbol (rtx);
270 static void mips_split_const (rtx, rtx *, HOST_WIDE_INT *);
271 static bool mips_offset_within_object_p (rtx, HOST_WIDE_INT);
272 static bool mips_valid_base_register_p (rtx, enum machine_mode, int);
273 static bool mips_symbolic_address_p (enum mips_symbol_type, enum machine_mode);
274 static bool mips_classify_address (struct mips_address_info *, rtx,
275                                    enum machine_mode, int);
276 static bool mips_cannot_force_const_mem (rtx);
277 static bool mips_use_blocks_for_constant_p (enum machine_mode, rtx);
278 static int mips_symbol_insns (enum mips_symbol_type);
279 static bool mips16_unextended_reference_p (enum machine_mode mode, rtx, rtx);
280 static rtx mips_force_temporary (rtx, rtx);
281 static rtx mips_unspec_offset_high (rtx, rtx, rtx, enum mips_symbol_type);
282 static rtx mips_add_offset (rtx, rtx, HOST_WIDE_INT);
283 static unsigned int mips_build_shift (struct mips_integer_op *, HOST_WIDE_INT);
284 static unsigned int mips_build_lower (struct mips_integer_op *,
285                                       unsigned HOST_WIDE_INT);
286 static unsigned int mips_build_integer (struct mips_integer_op *,
287                                         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 section *mips_select_rtx_section (enum machine_mode, rtx,
332                                          unsigned HOST_WIDE_INT);
333 static section *mips_function_rodata_section (tree);
334 static bool mips_in_small_data_p (tree);
335 static bool mips_use_anchors_for_symbol_p (rtx);
336 static int mips_fpr_return_fields (tree, tree *);
337 static bool mips_return_in_msb (tree);
338 static rtx mips_return_fpr_pair (enum machine_mode mode,
339                                  enum machine_mode mode1, HOST_WIDE_INT,
340                                  enum machine_mode mode2, HOST_WIDE_INT);
341 static rtx mips16_gp_pseudo_reg (void);
342 static void mips16_fp_args (FILE *, int, int);
343 static void build_mips16_function_stub (FILE *);
344 static rtx dump_constants_1 (enum machine_mode, rtx, rtx);
345 static void dump_constants (struct mips16_constant *, rtx);
346 static int mips16_insn_length (rtx);
347 static int mips16_rewrite_pool_refs (rtx *, void *);
348 static void mips16_lay_out_constants (void);
349 static void mips_sim_reset (struct mips_sim *);
350 static void mips_sim_init (struct mips_sim *, state_t);
351 static void mips_sim_next_cycle (struct mips_sim *);
352 static void mips_sim_wait_reg (struct mips_sim *, rtx, rtx);
353 static int mips_sim_wait_regs_2 (rtx *, void *);
354 static void mips_sim_wait_regs_1 (rtx *, void *);
355 static void mips_sim_wait_regs (struct mips_sim *, rtx);
356 static void mips_sim_wait_units (struct mips_sim *, rtx);
357 static void mips_sim_wait_insn (struct mips_sim *, rtx);
358 static void mips_sim_record_set (rtx, rtx, void *);
359 static void mips_sim_issue_insn (struct mips_sim *, rtx);
360 static void mips_sim_issue_nop (struct mips_sim *);
361 static void mips_sim_finish_insn (struct mips_sim *, rtx);
362 static void vr4130_avoid_branch_rt_conflict (rtx);
363 static void vr4130_align_insns (void);
364 static void mips_avoid_hazard (rtx, rtx, int *, rtx *, rtx);
365 static void mips_avoid_hazards (void);
366 static void mips_reorg (void);
367 static bool mips_strict_matching_cpu_name_p (const char *, const char *);
368 static bool mips_matching_cpu_name_p (const char *, const char *);
369 static const struct mips_cpu_info *mips_parse_cpu (const char *);
370 static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
371 static bool mips_return_in_memory (tree, tree);
372 static bool mips_strict_argument_naming (CUMULATIVE_ARGS *);
373 static void mips_macc_chains_record (rtx);
374 static void mips_macc_chains_reorder (rtx *, int);
375 static void vr4130_true_reg_dependence_p_1 (rtx, rtx, void *);
376 static bool vr4130_true_reg_dependence_p (rtx);
377 static bool vr4130_swap_insns_p (rtx, rtx);
378 static void vr4130_reorder (rtx *, int);
379 static void mips_promote_ready (rtx *, int, int);
380 static int mips_sched_reorder (FILE *, int, rtx *, int *, int);
381 static int mips_variable_issue (FILE *, int, rtx, int);
382 static int mips_adjust_cost (rtx, rtx, rtx, int);
383 static int mips_issue_rate (void);
384 static int mips_multipass_dfa_lookahead (void);
385 static void mips_init_libfuncs (void);
386 static void mips_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
387                                          tree, int *, int);
388 static tree mips_build_builtin_va_list (void);
389 static tree mips_gimplify_va_arg_expr (tree, tree, tree *, tree *);
390 static bool mips_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode mode,
391                                     tree, bool);
392 static bool mips_callee_copies (CUMULATIVE_ARGS *, enum machine_mode mode,
393                                 tree, bool);
394 static int mips_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode mode,
395                                    tree, bool);
396 static bool mips_valid_pointer_mode (enum machine_mode);
397 static bool mips_vector_mode_supported_p (enum machine_mode);
398 static rtx mips_prepare_builtin_arg (enum insn_code, unsigned int, tree *);
399 static rtx mips_prepare_builtin_target (enum insn_code, unsigned int, rtx);
400 static rtx mips_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
401 static void mips_init_builtins (void);
402 static rtx mips_expand_builtin_direct (enum insn_code, rtx, tree, bool);
403 static rtx mips_expand_builtin_movtf (enum mips_builtin_type,
404                                       enum insn_code, enum mips_fp_condition,
405                                       rtx, tree);
406 static rtx mips_expand_builtin_compare (enum mips_builtin_type,
407                                         enum insn_code, enum mips_fp_condition,
408                                         rtx, tree);
409 static rtx mips_expand_builtin_bposge (enum mips_builtin_type, rtx);
410 static void mips_encode_section_info (tree, rtx, int);
411 static void mips_extra_live_on_entry (bitmap);
412 static int mips_mode_rep_extended (enum machine_mode, enum machine_mode);
413
414 /* Structure to be filled in by compute_frame_size with register
415    save masks, and offsets for the current function.  */
416
417 struct mips_frame_info GTY(())
418 {
419   HOST_WIDE_INT total_size;     /* # bytes that the entire frame takes up */
420   HOST_WIDE_INT var_size;       /* # bytes that variables take up */
421   HOST_WIDE_INT args_size;      /* # bytes that outgoing arguments take up */
422   HOST_WIDE_INT cprestore_size; /* # bytes that the .cprestore slot takes up */
423   HOST_WIDE_INT gp_reg_size;    /* # bytes needed to store gp regs */
424   HOST_WIDE_INT fp_reg_size;    /* # bytes needed to store fp regs */
425   unsigned int mask;            /* mask of saved gp registers */
426   unsigned int fmask;           /* mask of saved fp registers */
427   HOST_WIDE_INT gp_save_offset; /* offset from vfp to store gp registers */
428   HOST_WIDE_INT fp_save_offset; /* offset from vfp to store fp registers */
429   HOST_WIDE_INT gp_sp_offset;   /* offset from new sp to store gp registers */
430   HOST_WIDE_INT fp_sp_offset;   /* offset from new sp to store fp registers */
431   bool initialized;             /* true if frame size already calculated */
432   int num_gp;                   /* number of gp registers saved */
433   int num_fp;                   /* number of fp registers saved */
434 };
435
436 struct machine_function GTY(()) {
437   /* Pseudo-reg holding the value of $28 in a mips16 function which
438      refers to GP relative global variables.  */
439   rtx mips16_gp_pseudo_rtx;
440
441   /* The number of extra stack bytes taken up by register varargs.
442      This area is allocated by the callee at the very top of the frame.  */
443   int varargs_size;
444
445   /* Current frame information, calculated by compute_frame_size.  */
446   struct mips_frame_info frame;
447
448   /* The register to use as the global pointer within this function.  */
449   unsigned int global_pointer;
450
451   /* True if mips_adjust_insn_length should ignore an instruction's
452      hazard attribute.  */
453   bool ignore_hazard_length_p;
454
455   /* True if the whole function is suitable for .set noreorder and
456      .set nomacro.  */
457   bool all_noreorder_p;
458
459   /* True if the function is known to have an instruction that needs $gp.  */
460   bool has_gp_insn_p;
461 };
462
463 /* Information about a single argument.  */
464 struct mips_arg_info
465 {
466   /* True if the argument is passed in a floating-point register, or
467      would have been if we hadn't run out of registers.  */
468   bool fpr_p;
469
470   /* The number of words passed in registers, rounded up.  */
471   unsigned int reg_words;
472
473   /* For EABI, the offset of the first register from GP_ARG_FIRST or
474      FP_ARG_FIRST.  For other ABIs, the offset of the first register from
475      the start of the ABI's argument structure (see the CUMULATIVE_ARGS
476      comment for details).
477
478      The value is MAX_ARGS_IN_REGISTERS if the argument is passed entirely
479      on the stack.  */
480   unsigned int reg_offset;
481
482   /* The number of words that must be passed on the stack, rounded up.  */
483   unsigned int stack_words;
484
485   /* The offset from the start of the stack overflow area of the argument's
486      first stack word.  Only meaningful when STACK_WORDS is nonzero.  */
487   unsigned int stack_offset;
488 };
489
490
491 /* Information about an address described by mips_address_type.
492
493    ADDRESS_CONST_INT
494        No fields are used.
495
496    ADDRESS_REG
497        REG is the base register and OFFSET is the constant offset.
498
499    ADDRESS_LO_SUM
500        REG is the register that contains the high part of the address,
501        OFFSET is the symbolic address being referenced and SYMBOL_TYPE
502        is the type of OFFSET's symbol.
503
504    ADDRESS_SYMBOLIC
505        SYMBOL_TYPE is the type of symbol being referenced.  */
506
507 struct mips_address_info
508 {
509   enum mips_address_type type;
510   rtx reg;
511   rtx offset;
512   enum mips_symbol_type symbol_type;
513 };
514
515
516 /* One stage in a constant building sequence.  These sequences have
517    the form:
518
519         A = VALUE[0]
520         A = A CODE[1] VALUE[1]
521         A = A CODE[2] VALUE[2]
522         ...
523
524    where A is an accumulator, each CODE[i] is a binary rtl operation
525    and each VALUE[i] is a constant integer.  */
526 struct mips_integer_op {
527   enum rtx_code code;
528   unsigned HOST_WIDE_INT value;
529 };
530
531
532 /* The largest number of operations needed to load an integer constant.
533    The worst accepted case for 64-bit constants is LUI,ORI,SLL,ORI,SLL,ORI.
534    When the lowest bit is clear, we can try, but reject a sequence with
535    an extra SLL at the end.  */
536 #define MIPS_MAX_INTEGER_OPS 7
537
538
539 /* Global variables for machine-dependent things.  */
540
541 /* Threshold for data being put into the small data/bss area, instead
542    of the normal data area.  */
543 int mips_section_threshold = -1;
544
545 /* Count the number of .file directives, so that .loc is up to date.  */
546 int num_source_filenames = 0;
547
548 /* Count the number of sdb related labels are generated (to find block
549    start and end boundaries).  */
550 int sdb_label_count = 0;
551
552 /* Next label # for each statement for Silicon Graphics IRIS systems.  */
553 int sym_lineno = 0;
554
555 /* Linked list of all externals that are to be emitted when optimizing
556    for the global pointer if they haven't been declared by the end of
557    the program with an appropriate .comm or initialization.  */
558
559 struct extern_list GTY (())
560 {
561   struct extern_list *next;     /* next external */
562   const char *name;             /* name of the external */
563   int size;                     /* size in bytes */
564 };
565
566 static GTY (()) struct extern_list *extern_head = 0;
567
568 /* Name of the file containing the current function.  */
569 const char *current_function_file = "";
570
571 /* Number of nested .set noreorder, noat, nomacro, and volatile requests.  */
572 int set_noreorder;
573 int set_noat;
574 int set_nomacro;
575 int set_volatile;
576
577 /* The next branch instruction is a branch likely, not branch normal.  */
578 int mips_branch_likely;
579
580 /* The operands passed to the last cmpMM expander.  */
581 rtx cmp_operands[2];
582
583 /* The target cpu for code generation.  */
584 enum processor_type mips_arch;
585 const struct mips_cpu_info *mips_arch_info;
586
587 /* The target cpu for optimization and scheduling.  */
588 enum processor_type mips_tune;
589 const struct mips_cpu_info *mips_tune_info;
590
591 /* Which instruction set architecture to use.  */
592 int mips_isa;
593
594 /* Which ABI to use.  */
595 int mips_abi = MIPS_ABI_DEFAULT;
596
597 /* Cost information to use.  */
598 const struct mips_rtx_cost_data *mips_cost;
599
600 /* Whether we are generating mips16 hard float code.  In mips16 mode
601    we always set TARGET_SOFT_FLOAT; this variable is nonzero if
602    -msoft-float was not specified by the user, which means that we
603    should arrange to call mips32 hard floating point code.  */
604 int mips16_hard_float;
605
606 /* The architecture selected by -mipsN.  */
607 static const struct mips_cpu_info *mips_isa_info;
608
609 /* If TRUE, we split addresses into their high and low parts in the RTL.  */
610 int mips_split_addresses;
611
612 /* Mode used for saving/restoring general purpose registers.  */
613 static enum machine_mode gpr_mode;
614
615 /* Array giving truth value on whether or not a given hard register
616    can support a given mode.  */
617 char mips_hard_regno_mode_ok[(int)MAX_MACHINE_MODE][FIRST_PSEUDO_REGISTER];
618
619 /* List of all MIPS punctuation characters used by print_operand.  */
620 char mips_print_operand_punct[256];
621
622 /* Map GCC register number to debugger register number.  */
623 int mips_dbx_regno[FIRST_PSEUDO_REGISTER];
624
625 /* A copy of the original flag_delayed_branch: see override_options.  */
626 static int mips_flag_delayed_branch;
627
628 static GTY (()) int mips_output_filename_first_time = 1;
629
630 /* mips_split_p[X] is true if symbols of type X can be split by
631    mips_split_symbol().  */
632 bool mips_split_p[NUM_SYMBOL_TYPES];
633
634 /* mips_lo_relocs[X] is the relocation to use when a symbol of type X
635    appears in a LO_SUM.  It can be null if such LO_SUMs aren't valid or
636    if they are matched by a special .md file pattern.  */
637 static const char *mips_lo_relocs[NUM_SYMBOL_TYPES];
638
639 /* Likewise for HIGHs.  */
640 static const char *mips_hi_relocs[NUM_SYMBOL_TYPES];
641
642 /* Map hard register number to register class */
643 const enum reg_class mips_regno_to_class[] =
644 {
645   LEA_REGS,     LEA_REGS,       M16_NA_REGS,    V1_REG,
646   M16_REGS,     M16_REGS,       M16_REGS,       M16_REGS,
647   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
648   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
649   M16_NA_REGS,  M16_NA_REGS,    LEA_REGS,       LEA_REGS,
650   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_REGS,
651   T_REG,        PIC_FN_ADDR_REG, LEA_REGS,      LEA_REGS,
652   LEA_REGS,     LEA_REGS,       LEA_REGS,       LEA_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   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
659   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
660   FP_REGS,      FP_REGS,        FP_REGS,        FP_REGS,
661   HI_REG,       LO_REG,         NO_REGS,        ST_REGS,
662   ST_REGS,      ST_REGS,        ST_REGS,        ST_REGS,
663   ST_REGS,      ST_REGS,        ST_REGS,        NO_REGS,
664   NO_REGS,      ALL_REGS,       ALL_REGS,       NO_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   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
671   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_REGS,
672   COP0_REGS,    COP0_REGS,      COP0_REGS,      COP0_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   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
679   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_REGS,
680   COP2_REGS,    COP2_REGS,      COP2_REGS,      COP2_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   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
687   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
688   COP3_REGS,    COP3_REGS,      COP3_REGS,      COP3_REGS,
689   DSP_ACC_REGS, DSP_ACC_REGS,   DSP_ACC_REGS,   DSP_ACC_REGS,
690   DSP_ACC_REGS, DSP_ACC_REGS,   ALL_REGS,       ALL_REGS,
691   ALL_REGS,     ALL_REGS,       ALL_REGS,       ALL_REGS
692 };
693
694 /* Table of machine dependent attributes.  */
695 const struct attribute_spec mips_attribute_table[] =
696 {
697   { "long_call",   0, 0, false, true,  true,  NULL },
698   { NULL,          0, 0, false, false, false, NULL }
699 };
700 \f
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.
704
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
717   /* MIPS I */
718   { "r3000", PROCESSOR_R3000, 1 },
719   { "r2000", PROCESSOR_R3000, 1 }, /* = r3000 */
720   { "r3900", PROCESSOR_R3900, 1 },
721
722   /* MIPS II */
723   { "r6000", PROCESSOR_R6000, 2 },
724
725   /* MIPS III */
726   { "r4000", PROCESSOR_R4000, 3 },
727   { "vr4100", PROCESSOR_R4100, 3 },
728   { "vr4111", PROCESSOR_R4111, 3 },
729   { "vr4120", PROCESSOR_R4120, 3 },
730   { "vr4130", PROCESSOR_R4130, 3 },
731   { "vr4300", PROCESSOR_R4300, 3 },
732   { "r4400", PROCESSOR_R4000, 3 }, /* = r4000 */
733   { "r4600", PROCESSOR_R4600, 3 },
734   { "orion", PROCESSOR_R4600, 3 }, /* = r4600 */
735   { "r4650", PROCESSOR_R4650, 3 },
736
737   /* MIPS IV */
738   { "r8000", PROCESSOR_R8000, 4 },
739   { "vr5000", PROCESSOR_R5000, 4 },
740   { "vr5400", PROCESSOR_R5400, 4 },
741   { "vr5500", PROCESSOR_R5500, 4 },
742   { "rm7000", PROCESSOR_R7000, 4 },
743   { "rm9000", PROCESSOR_R9000, 4 },
744
745   /* MIPS32 */
746   { "4kc", PROCESSOR_4KC, 32 },
747   { "4km", PROCESSOR_4KC, 32 }, /* = 4kc */
748   { "4kp", PROCESSOR_4KP, 32 },
749
750   /* MIPS32 Release 2 */
751   { "m4k", PROCESSOR_M4K, 33 },
752   { "4kec", PROCESSOR_4KC, 33 },
753   { "4kem", PROCESSOR_4KC, 33 },
754   { "4kep", PROCESSOR_4KP, 33 },
755   { "24kc", PROCESSOR_24KC, 33 },  /* 24K  no FPU */
756   { "24kf", PROCESSOR_24KF, 33 },  /* 24K 1:2 FPU */
757   { "24kx", PROCESSOR_24KX, 33 },  /* 24K 1:1 FPU */
758   { "24kec", PROCESSOR_24KC, 33 }, /* 24K with DSP */
759   { "24kef", PROCESSOR_24KF, 33 },
760   { "24kex", PROCESSOR_24KX, 33 },
761   { "34kc", PROCESSOR_24KC, 33 },  /* 34K with MT/DSP */
762   { "34kf", PROCESSOR_24KF, 33 },
763   { "34kx", PROCESSOR_24KX, 33 },
764
765   /* MIPS64 */
766   { "5kc", PROCESSOR_5KC, 64 },
767   { "5kf", PROCESSOR_5KF, 64 },
768   { "20kc", PROCESSOR_20KC, 64 },
769   { "sb1", PROCESSOR_SB1, 64 },
770   { "sb1a", PROCESSOR_SB1A, 64 },
771   { "sr71000", PROCESSOR_SR71000, 64 },
772
773   /* End marker */
774   { 0, 0, 0 }
775 };
776
777 /* Default costs. If these are used for a processor we should look
778    up the actual costs.  */
779 #define DEFAULT_COSTS COSTS_N_INSNS (6),  /* fp_add */       \
780                       COSTS_N_INSNS (7),  /* fp_mult_sf */   \
781                       COSTS_N_INSNS (8),  /* fp_mult_df */   \
782                       COSTS_N_INSNS (23), /* fp_div_sf */    \
783                       COSTS_N_INSNS (36), /* fp_div_df */    \
784                       COSTS_N_INSNS (10), /* int_mult_si */  \
785                       COSTS_N_INSNS (10), /* int_mult_di */  \
786                       COSTS_N_INSNS (69), /* int_div_si */   \
787                       COSTS_N_INSNS (69), /* int_div_di */   \
788                                        2, /* branch_cost */  \
789                                        4  /* memory_latency */
790
791 /* Need to replace these with the costs of calling the appropriate
792    libgcc routine.  */
793 #define SOFT_FP_COSTS COSTS_N_INSNS (256), /* fp_add */       \
794                       COSTS_N_INSNS (256), /* fp_mult_sf */   \
795                       COSTS_N_INSNS (256), /* fp_mult_df */   \
796                       COSTS_N_INSNS (256), /* fp_div_sf */    \
797                       COSTS_N_INSNS (256)  /* fp_div_df */
798
799 static struct mips_rtx_cost_data const mips_rtx_cost_data[PROCESSOR_MAX] =
800   {
801     { /* R3000 */
802       COSTS_N_INSNS (2),            /* fp_add */
803       COSTS_N_INSNS (4),            /* fp_mult_sf */
804       COSTS_N_INSNS (5),            /* fp_mult_df */
805       COSTS_N_INSNS (12),           /* fp_div_sf */
806       COSTS_N_INSNS (19),           /* fp_div_df */
807       COSTS_N_INSNS (12),           /* int_mult_si */
808       COSTS_N_INSNS (12),           /* int_mult_di */
809       COSTS_N_INSNS (35),           /* int_div_si */
810       COSTS_N_INSNS (35),           /* int_div_di */
811                        1,           /* branch_cost */
812                        4            /* memory_latency */
813
814     },
815     { /* 4KC */
816       SOFT_FP_COSTS,
817       COSTS_N_INSNS (6),            /* int_mult_si */
818       COSTS_N_INSNS (6),            /* int_mult_di */
819       COSTS_N_INSNS (36),           /* int_div_si */
820       COSTS_N_INSNS (36),           /* int_div_di */
821                        1,           /* branch_cost */
822                        4            /* memory_latency */
823     },
824     { /* 4KP */
825       SOFT_FP_COSTS,
826       COSTS_N_INSNS (36),           /* int_mult_si */
827       COSTS_N_INSNS (36),           /* int_mult_di */
828       COSTS_N_INSNS (37),           /* int_div_si */
829       COSTS_N_INSNS (37),           /* int_div_di */
830                        1,           /* branch_cost */
831                        4            /* memory_latency */
832     },
833     { /* 5KC */
834       SOFT_FP_COSTS,
835       COSTS_N_INSNS (4),            /* int_mult_si */
836       COSTS_N_INSNS (11),           /* int_mult_di */
837       COSTS_N_INSNS (36),           /* int_div_si */
838       COSTS_N_INSNS (68),           /* int_div_di */
839                        1,           /* branch_cost */
840                        4            /* memory_latency */
841     },
842     { /* 5KF */
843       COSTS_N_INSNS (4),            /* fp_add */
844       COSTS_N_INSNS (4),            /* fp_mult_sf */
845       COSTS_N_INSNS (5),            /* fp_mult_df */
846       COSTS_N_INSNS (17),           /* fp_div_sf */
847       COSTS_N_INSNS (32),           /* fp_div_df */
848       COSTS_N_INSNS (4),            /* int_mult_si */
849       COSTS_N_INSNS (11),           /* int_mult_di */
850       COSTS_N_INSNS (36),           /* int_div_si */
851       COSTS_N_INSNS (68),           /* int_div_di */
852                        1,           /* branch_cost */
853                        4            /* memory_latency */
854     },
855     { /* 20KC */
856       DEFAULT_COSTS
857     },
858     { /* 24KC */
859       SOFT_FP_COSTS,
860       COSTS_N_INSNS (5),            /* int_mult_si */
861       COSTS_N_INSNS (5),            /* int_mult_di */
862       COSTS_N_INSNS (41),           /* int_div_si */
863       COSTS_N_INSNS (41),           /* int_div_di */
864                        1,           /* branch_cost */
865                        4            /* memory_latency */
866     },
867     { /* 24KF */
868       COSTS_N_INSNS (8),            /* fp_add */
869       COSTS_N_INSNS (8),            /* fp_mult_sf */
870       COSTS_N_INSNS (10),           /* fp_mult_df */
871       COSTS_N_INSNS (34),           /* fp_div_sf */
872       COSTS_N_INSNS (64),           /* fp_div_df */
873       COSTS_N_INSNS (5),            /* int_mult_si */
874       COSTS_N_INSNS (5),            /* int_mult_di */
875       COSTS_N_INSNS (41),           /* int_div_si */
876       COSTS_N_INSNS (41),           /* int_div_di */
877                        1,           /* branch_cost */
878                        4            /* memory_latency */
879     },
880     { /* 24KX */
881       COSTS_N_INSNS (4),            /* fp_add */
882       COSTS_N_INSNS (4),            /* fp_mult_sf */
883       COSTS_N_INSNS (5),            /* fp_mult_df */
884       COSTS_N_INSNS (17),           /* fp_div_sf */
885       COSTS_N_INSNS (32),           /* fp_div_df */
886       COSTS_N_INSNS (5),            /* int_mult_si */
887       COSTS_N_INSNS (5),            /* int_mult_di */
888       COSTS_N_INSNS (41),           /* int_div_si */
889       COSTS_N_INSNS (41),           /* int_div_di */
890                        1,           /* branch_cost */
891                        4            /* memory_latency */
892     },
893     { /* M4k */
894       DEFAULT_COSTS
895     },
896     { /* R3900 */
897       COSTS_N_INSNS (2),            /* fp_add */
898       COSTS_N_INSNS (4),            /* fp_mult_sf */
899       COSTS_N_INSNS (5),            /* fp_mult_df */
900       COSTS_N_INSNS (12),           /* fp_div_sf */
901       COSTS_N_INSNS (19),           /* fp_div_df */
902       COSTS_N_INSNS (2),            /* int_mult_si */
903       COSTS_N_INSNS (2),            /* int_mult_di */
904       COSTS_N_INSNS (35),           /* int_div_si */
905       COSTS_N_INSNS (35),           /* int_div_di */
906                        1,           /* branch_cost */
907                        4            /* memory_latency */
908     },
909     { /* R6000 */
910       COSTS_N_INSNS (3),            /* fp_add */
911       COSTS_N_INSNS (5),            /* fp_mult_sf */
912       COSTS_N_INSNS (6),            /* fp_mult_df */
913       COSTS_N_INSNS (15),           /* fp_div_sf */
914       COSTS_N_INSNS (16),           /* fp_div_df */
915       COSTS_N_INSNS (17),           /* int_mult_si */
916       COSTS_N_INSNS (17),           /* int_mult_di */
917       COSTS_N_INSNS (38),           /* int_div_si */
918       COSTS_N_INSNS (38),           /* int_div_di */
919                        2,           /* branch_cost */
920                        6            /* memory_latency */
921     },
922     { /* R4000 */
923        COSTS_N_INSNS (6),           /* fp_add */
924        COSTS_N_INSNS (7),           /* fp_mult_sf */
925        COSTS_N_INSNS (8),           /* fp_mult_df */
926        COSTS_N_INSNS (23),          /* fp_div_sf */
927        COSTS_N_INSNS (36),          /* fp_div_df */
928        COSTS_N_INSNS (10),          /* int_mult_si */
929        COSTS_N_INSNS (10),          /* int_mult_di */
930        COSTS_N_INSNS (69),          /* int_div_si */
931        COSTS_N_INSNS (69),          /* int_div_di */
932                         2,          /* branch_cost */
933                         6           /* memory_latency */
934     },
935     { /* R4100 */
936       DEFAULT_COSTS
937     },
938     { /* R4111 */
939       DEFAULT_COSTS
940     },
941     { /* R4120 */
942       DEFAULT_COSTS
943     },
944     { /* R4130 */
945       /* The only costs that appear to be updated here are
946          integer multiplication.  */
947       SOFT_FP_COSTS,
948       COSTS_N_INSNS (4),            /* int_mult_si */
949       COSTS_N_INSNS (6),            /* int_mult_di */
950       COSTS_N_INSNS (69),           /* int_div_si */
951       COSTS_N_INSNS (69),           /* int_div_di */
952                        1,           /* branch_cost */
953                        4            /* memory_latency */
954     },
955     { /* R4300 */
956       DEFAULT_COSTS
957     },
958     { /* R4600 */
959       DEFAULT_COSTS
960     },
961     { /* R4650 */
962       DEFAULT_COSTS
963     },
964     { /* R5000 */
965       COSTS_N_INSNS (6),            /* fp_add */
966       COSTS_N_INSNS (4),            /* fp_mult_sf */
967       COSTS_N_INSNS (5),            /* fp_mult_df */
968       COSTS_N_INSNS (23),           /* fp_div_sf */
969       COSTS_N_INSNS (36),           /* fp_div_df */
970       COSTS_N_INSNS (5),            /* int_mult_si */
971       COSTS_N_INSNS (5),            /* int_mult_di */
972       COSTS_N_INSNS (36),           /* int_div_si */
973       COSTS_N_INSNS (36),           /* int_div_di */
974                        1,           /* branch_cost */
975                        4            /* memory_latency */
976     },
977     { /* R5400 */
978       COSTS_N_INSNS (6),            /* fp_add */
979       COSTS_N_INSNS (5),            /* fp_mult_sf */
980       COSTS_N_INSNS (6),            /* fp_mult_df */
981       COSTS_N_INSNS (30),           /* fp_div_sf */
982       COSTS_N_INSNS (59),           /* fp_div_df */
983       COSTS_N_INSNS (3),            /* int_mult_si */
984       COSTS_N_INSNS (4),            /* int_mult_di */
985       COSTS_N_INSNS (42),           /* int_div_si */
986       COSTS_N_INSNS (74),           /* int_div_di */
987                        1,           /* branch_cost */
988                        4            /* memory_latency */
989     },
990     { /* R5500 */
991       COSTS_N_INSNS (6),            /* fp_add */
992       COSTS_N_INSNS (5),            /* fp_mult_sf */
993       COSTS_N_INSNS (6),            /* fp_mult_df */
994       COSTS_N_INSNS (30),           /* fp_div_sf */
995       COSTS_N_INSNS (59),           /* fp_div_df */
996       COSTS_N_INSNS (5),            /* int_mult_si */
997       COSTS_N_INSNS (9),            /* int_mult_di */
998       COSTS_N_INSNS (42),           /* int_div_si */
999       COSTS_N_INSNS (74),           /* int_div_di */
1000                        1,           /* branch_cost */
1001                        4            /* memory_latency */
1002     },
1003     { /* R7000 */
1004       /* The only costs that are changed here are
1005          integer multiplication.  */
1006       COSTS_N_INSNS (6),            /* fp_add */
1007       COSTS_N_INSNS (7),            /* fp_mult_sf */
1008       COSTS_N_INSNS (8),            /* fp_mult_df */
1009       COSTS_N_INSNS (23),           /* fp_div_sf */
1010       COSTS_N_INSNS (36),           /* fp_div_df */
1011       COSTS_N_INSNS (5),            /* int_mult_si */
1012       COSTS_N_INSNS (9),            /* int_mult_di */
1013       COSTS_N_INSNS (69),           /* int_div_si */
1014       COSTS_N_INSNS (69),           /* int_div_di */
1015                        1,           /* branch_cost */
1016                        4            /* memory_latency */
1017     },
1018     { /* R8000 */
1019       DEFAULT_COSTS
1020     },
1021     { /* R9000 */
1022       /* The only costs that are changed here are
1023          integer multiplication.  */
1024       COSTS_N_INSNS (6),            /* fp_add */
1025       COSTS_N_INSNS (7),            /* fp_mult_sf */
1026       COSTS_N_INSNS (8),            /* fp_mult_df */
1027       COSTS_N_INSNS (23),           /* fp_div_sf */
1028       COSTS_N_INSNS (36),           /* fp_div_df */
1029       COSTS_N_INSNS (3),            /* int_mult_si */
1030       COSTS_N_INSNS (8),            /* int_mult_di */
1031       COSTS_N_INSNS (69),           /* int_div_si */
1032       COSTS_N_INSNS (69),           /* int_div_di */
1033                        1,           /* branch_cost */
1034                        4            /* memory_latency */
1035     },
1036     { /* SB1 */
1037       /* These costs are the same as the SB-1A below.  */
1038       COSTS_N_INSNS (4),            /* fp_add */
1039       COSTS_N_INSNS (4),            /* fp_mult_sf */
1040       COSTS_N_INSNS (4),            /* fp_mult_df */
1041       COSTS_N_INSNS (24),           /* fp_div_sf */
1042       COSTS_N_INSNS (32),           /* fp_div_df */
1043       COSTS_N_INSNS (3),            /* int_mult_si */
1044       COSTS_N_INSNS (4),            /* int_mult_di */
1045       COSTS_N_INSNS (36),           /* int_div_si */
1046       COSTS_N_INSNS (68),           /* int_div_di */
1047                        1,           /* branch_cost */
1048                        4            /* memory_latency */
1049     },
1050     { /* SB1-A */
1051       /* These costs are the same as the SB-1 above.  */
1052       COSTS_N_INSNS (4),            /* fp_add */
1053       COSTS_N_INSNS (4),            /* fp_mult_sf */
1054       COSTS_N_INSNS (4),            /* fp_mult_df */
1055       COSTS_N_INSNS (24),           /* fp_div_sf */
1056       COSTS_N_INSNS (32),           /* fp_div_df */
1057       COSTS_N_INSNS (3),            /* int_mult_si */
1058       COSTS_N_INSNS (4),            /* int_mult_di */
1059       COSTS_N_INSNS (36),           /* int_div_si */
1060       COSTS_N_INSNS (68),           /* int_div_di */
1061                        1,           /* branch_cost */
1062                        4            /* memory_latency */
1063     },
1064     { /* SR71000 */
1065       DEFAULT_COSTS
1066     },
1067   };
1068
1069 \f
1070 /* Nonzero if -march should decide the default value of MASK_SOFT_FLOAT.  */
1071 #ifndef MIPS_MARCH_CONTROLS_SOFT_FLOAT
1072 #define MIPS_MARCH_CONTROLS_SOFT_FLOAT 0
1073 #endif
1074 \f
1075 /* Initialize the GCC target structure.  */
1076 #undef TARGET_ASM_ALIGNED_HI_OP
1077 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
1078 #undef TARGET_ASM_ALIGNED_SI_OP
1079 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
1080 #undef TARGET_ASM_ALIGNED_DI_OP
1081 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
1082
1083 #undef TARGET_ASM_FUNCTION_PROLOGUE
1084 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
1085 #undef TARGET_ASM_FUNCTION_EPILOGUE
1086 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
1087 #undef TARGET_ASM_SELECT_RTX_SECTION
1088 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
1089 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
1090 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
1091
1092 #undef TARGET_SCHED_REORDER
1093 #define TARGET_SCHED_REORDER mips_sched_reorder
1094 #undef TARGET_SCHED_VARIABLE_ISSUE
1095 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
1096 #undef TARGET_SCHED_ADJUST_COST
1097 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
1098 #undef TARGET_SCHED_ISSUE_RATE
1099 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
1100 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1101 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
1102   mips_multipass_dfa_lookahead
1103
1104 #undef TARGET_DEFAULT_TARGET_FLAGS
1105 #define TARGET_DEFAULT_TARGET_FLAGS             \
1106   (TARGET_DEFAULT                               \
1107    | TARGET_CPU_DEFAULT                         \
1108    | TARGET_ENDIAN_DEFAULT                      \
1109    | TARGET_FP_EXCEPTIONS_DEFAULT               \
1110    | MASK_CHECK_ZERO_DIV                        \
1111    | MASK_FUSED_MADD)
1112 #undef TARGET_HANDLE_OPTION
1113 #define TARGET_HANDLE_OPTION mips_handle_option
1114
1115 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1116 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
1117
1118 #undef TARGET_VALID_POINTER_MODE
1119 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
1120 #undef TARGET_RTX_COSTS
1121 #define TARGET_RTX_COSTS mips_rtx_costs
1122 #undef TARGET_ADDRESS_COST
1123 #define TARGET_ADDRESS_COST mips_address_cost
1124
1125 #undef TARGET_IN_SMALL_DATA_P
1126 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
1127
1128 #undef TARGET_MACHINE_DEPENDENT_REORG
1129 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
1130
1131 #undef TARGET_ASM_FILE_START
1132 #undef TARGET_ASM_FILE_END
1133 #define TARGET_ASM_FILE_START mips_file_start
1134 #define TARGET_ASM_FILE_END mips_file_end
1135 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
1136 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
1137
1138 #undef TARGET_INIT_LIBFUNCS
1139 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
1140
1141 #undef TARGET_BUILD_BUILTIN_VA_LIST
1142 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
1143 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1144 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
1145
1146 #undef TARGET_PROMOTE_FUNCTION_ARGS
1147 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
1148 #undef TARGET_PROMOTE_FUNCTION_RETURN
1149 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
1150 #undef TARGET_PROMOTE_PROTOTYPES
1151 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
1152
1153 #undef TARGET_RETURN_IN_MEMORY
1154 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
1155 #undef TARGET_RETURN_IN_MSB
1156 #define TARGET_RETURN_IN_MSB mips_return_in_msb
1157
1158 #undef TARGET_ASM_OUTPUT_MI_THUNK
1159 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
1160 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1161 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
1162
1163 #undef TARGET_SETUP_INCOMING_VARARGS
1164 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
1165 #undef TARGET_STRICT_ARGUMENT_NAMING
1166 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
1167 #undef TARGET_MUST_PASS_IN_STACK
1168 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
1169 #undef TARGET_PASS_BY_REFERENCE
1170 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
1171 #undef TARGET_CALLEE_COPIES
1172 #define TARGET_CALLEE_COPIES mips_callee_copies
1173 #undef TARGET_ARG_PARTIAL_BYTES
1174 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
1175
1176 #undef TARGET_MODE_REP_EXTENDED
1177 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
1178
1179 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1180 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
1181
1182 #undef TARGET_INIT_BUILTINS
1183 #define TARGET_INIT_BUILTINS mips_init_builtins
1184 #undef TARGET_EXPAND_BUILTIN
1185 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
1186
1187 #undef TARGET_HAVE_TLS
1188 #define TARGET_HAVE_TLS HAVE_AS_TLS
1189
1190 #undef TARGET_CANNOT_FORCE_CONST_MEM
1191 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
1192
1193 #undef TARGET_ENCODE_SECTION_INFO
1194 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
1195
1196 #undef TARGET_ATTRIBUTE_TABLE
1197 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
1198
1199 #undef TARGET_EXTRA_LIVE_ON_ENTRY
1200 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
1201
1202 #undef TARGET_MIN_ANCHOR_OFFSET
1203 #define TARGET_MIN_ANCHOR_OFFSET -32768
1204 #undef TARGET_MAX_ANCHOR_OFFSET
1205 #define TARGET_MAX_ANCHOR_OFFSET 32767
1206 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1207 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
1208 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
1209 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
1210
1211 struct gcc_target targetm = TARGET_INITIALIZER;
1212 \f
1213 /* Classify symbol X, which must be a SYMBOL_REF or a LABEL_REF.  */
1214
1215 static enum mips_symbol_type
1216 mips_classify_symbol (rtx x)
1217 {
1218   tree decl;
1219
1220   if (GET_CODE (x) == LABEL_REF)
1221     {
1222       if (TARGET_MIPS16)
1223         return SYMBOL_CONSTANT_POOL;
1224       if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1225         return SYMBOL_GOT_LOCAL;
1226       return SYMBOL_GENERAL;
1227     }
1228
1229   gcc_assert (GET_CODE (x) == SYMBOL_REF);
1230
1231   if (SYMBOL_REF_TLS_MODEL (x))
1232     return SYMBOL_TLS;
1233
1234   if (CONSTANT_POOL_ADDRESS_P (x))
1235     {
1236       if (TARGET_MIPS16)
1237         return SYMBOL_CONSTANT_POOL;
1238
1239       if (GET_MODE_SIZE (get_pool_mode (x)) <= mips_section_threshold)
1240         return SYMBOL_SMALL_DATA;
1241     }
1242
1243   /* Do not use small-data accesses for weak symbols; they may end up
1244      being zero.  */
1245   if (SYMBOL_REF_SMALL_P (x)
1246       && !SYMBOL_REF_WEAK (x))
1247     return SYMBOL_SMALL_DATA;
1248
1249   if (TARGET_ABICALLS)
1250     {
1251       decl = SYMBOL_REF_DECL (x);
1252       if (decl == 0)
1253         {
1254           if (!SYMBOL_REF_LOCAL_P (x))
1255             return SYMBOL_GOT_GLOBAL;
1256         }
1257       else
1258         {
1259           /* Don't use GOT accesses for locally-binding symbols if
1260              TARGET_ABSOLUTE_ABICALLS.  Otherwise, there are three
1261              cases to consider:
1262
1263                 - o32 PIC (either with or without explicit relocs)
1264                 - n32/n64 PIC without explicit relocs
1265                 - n32/n64 PIC with explicit relocs
1266
1267              In the first case, both local and global accesses will use an
1268              R_MIPS_GOT16 relocation.  We must correctly predict which of
1269              the two semantics (local or global) the assembler and linker
1270              will apply.  The choice doesn't depend on the symbol's
1271              visibility, so we deliberately ignore decl_visibility and
1272              binds_local_p here.
1273
1274              In the second case, the assembler will not use R_MIPS_GOT16
1275              relocations, but it chooses between local and global accesses
1276              in the same way as for o32 PIC.
1277
1278              In the third case we have more freedom since both forms of
1279              access will work for any kind of symbol.  However, there seems
1280              little point in doing things differently.
1281
1282              Note that weakref symbols are not TREE_PUBLIC, but their
1283              targets are global or weak symbols.  Relocations in the
1284              object file will be against the target symbol, so it's
1285              that symbol's binding that matters here.  */
1286           if (DECL_P (decl)
1287               && (TREE_PUBLIC (decl) || DECL_WEAK (decl))
1288               && !(TARGET_ABSOLUTE_ABICALLS && targetm.binds_local_p (decl)))
1289             return SYMBOL_GOT_GLOBAL;
1290         }
1291
1292       if (!TARGET_ABSOLUTE_ABICALLS)
1293         return SYMBOL_GOT_LOCAL;
1294     }
1295
1296   return SYMBOL_GENERAL;
1297 }
1298
1299
1300 /* Split X into a base and a constant offset, storing them in *BASE
1301    and *OFFSET respectively.  */
1302
1303 static void
1304 mips_split_const (rtx x, rtx *base, HOST_WIDE_INT *offset)
1305 {
1306   *offset = 0;
1307
1308   if (GET_CODE (x) == CONST)
1309     {
1310       x = XEXP (x, 0);
1311       if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
1312         {
1313           *offset += INTVAL (XEXP (x, 1));
1314           x = XEXP (x, 0);
1315         }
1316     }
1317   *base = x;
1318 }
1319
1320
1321 /* Return true if SYMBOL is a SYMBOL_REF and OFFSET + SYMBOL points
1322    to the same object as SYMBOL, or to the same object_block.  */
1323
1324 static bool
1325 mips_offset_within_object_p (rtx symbol, HOST_WIDE_INT offset)
1326 {
1327   if (GET_CODE (symbol) != SYMBOL_REF)
1328     return false;
1329
1330   if (CONSTANT_POOL_ADDRESS_P (symbol)
1331       && offset >= 0
1332       && offset < (int) GET_MODE_SIZE (get_pool_mode (symbol)))
1333     return true;
1334
1335   if (SYMBOL_REF_DECL (symbol) != 0
1336       && offset >= 0
1337       && offset < int_size_in_bytes (TREE_TYPE (SYMBOL_REF_DECL (symbol))))
1338     return true;
1339
1340   if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol)
1341       && SYMBOL_REF_BLOCK (symbol)
1342       && SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0
1343       && ((unsigned HOST_WIDE_INT) offset + SYMBOL_REF_BLOCK_OFFSET (symbol)
1344           < (unsigned HOST_WIDE_INT) SYMBOL_REF_BLOCK (symbol)->size))
1345     return true;
1346
1347   return false;
1348 }
1349
1350
1351 /* Return true if X is a symbolic constant that can be calculated in
1352    the same way as a bare symbol.  If it is, store the type of the
1353    symbol in *SYMBOL_TYPE.  */
1354
1355 bool
1356 mips_symbolic_constant_p (rtx x, enum mips_symbol_type *symbol_type)
1357 {
1358   HOST_WIDE_INT offset;
1359
1360   mips_split_const (x, &x, &offset);
1361   if (UNSPEC_ADDRESS_P (x))
1362     *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1363   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1364     {
1365       *symbol_type = mips_classify_symbol (x);
1366       if (*symbol_type == SYMBOL_TLS)
1367         return false;
1368     }
1369   else
1370     return false;
1371
1372   if (offset == 0)
1373     return true;
1374
1375   /* Check whether a nonzero offset is valid for the underlying
1376      relocations.  */
1377   switch (*symbol_type)
1378     {
1379     case SYMBOL_GENERAL:
1380     case SYMBOL_64_HIGH:
1381     case SYMBOL_64_MID:
1382     case SYMBOL_64_LOW:
1383       /* If the target has 64-bit pointers and the object file only
1384          supports 32-bit symbols, the values of those symbols will be
1385          sign-extended.  In this case we can't allow an arbitrary offset
1386          in case the 32-bit value X + OFFSET has a different sign from X.  */
1387       if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1388         return mips_offset_within_object_p (x, offset);
1389
1390       /* In other cases the relocations can handle any offset.  */
1391       return true;
1392
1393     case SYMBOL_CONSTANT_POOL:
1394       /* Allow constant pool references to be converted to LABEL+CONSTANT.
1395          In this case, we no longer have access to the underlying constant,
1396          but the original symbol-based access was known to be valid.  */
1397       if (GET_CODE (x) == LABEL_REF)
1398         return true;
1399
1400       /* Fall through.  */
1401
1402     case SYMBOL_SMALL_DATA:
1403       /* Make sure that the offset refers to something within the
1404          underlying object.  This should guarantee that the final
1405          PC- or GP-relative offset is within the 16-bit limit.  */
1406       return mips_offset_within_object_p (x, offset);
1407
1408     case SYMBOL_GOT_LOCAL:
1409     case SYMBOL_GOTOFF_PAGE:
1410       /* The linker should provide enough local GOT entries for a
1411          16-bit offset.  Larger offsets may lead to GOT overflow.  */
1412       return SMALL_OPERAND (offset);
1413
1414     case SYMBOL_GOT_GLOBAL:
1415     case SYMBOL_GOTOFF_GLOBAL:
1416     case SYMBOL_GOTOFF_CALL:
1417     case SYMBOL_GOTOFF_LOADGP:
1418     case SYMBOL_TLSGD:
1419     case SYMBOL_TLSLDM:
1420     case SYMBOL_DTPREL:
1421     case SYMBOL_TPREL:
1422     case SYMBOL_GOTTPREL:
1423     case SYMBOL_TLS:
1424       return false;
1425     }
1426   gcc_unreachable ();
1427 }
1428
1429
1430 /* This function is used to implement REG_MODE_OK_FOR_BASE_P.  */
1431
1432 int
1433 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode, int strict)
1434 {
1435   if (regno >= FIRST_PSEUDO_REGISTER)
1436     {
1437       if (!strict)
1438         return true;
1439       regno = reg_renumber[regno];
1440     }
1441
1442   /* These fake registers will be eliminated to either the stack or
1443      hard frame pointer, both of which are usually valid base registers.
1444      Reload deals with the cases where the eliminated form isn't valid.  */
1445   if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
1446     return true;
1447
1448   /* In mips16 mode, the stack pointer can only address word and doubleword
1449      values, nothing smaller.  There are two problems here:
1450
1451        (a) Instantiating virtual registers can introduce new uses of the
1452            stack pointer.  If these virtual registers are valid addresses,
1453            the stack pointer should be too.
1454
1455        (b) Most uses of the stack pointer are not made explicit until
1456            FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
1457            We don't know until that stage whether we'll be eliminating to the
1458            stack pointer (which needs the restriction) or the hard frame
1459            pointer (which doesn't).
1460
1461      All in all, it seems more consistent to only enforce this restriction
1462      during and after reload.  */
1463   if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
1464     return !strict || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1465
1466   return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
1467 }
1468
1469
1470 /* Return true if X is a valid base register for the given mode.
1471    Allow only hard registers if STRICT.  */
1472
1473 static bool
1474 mips_valid_base_register_p (rtx x, enum machine_mode mode, int strict)
1475 {
1476   if (!strict && GET_CODE (x) == SUBREG)
1477     x = SUBREG_REG (x);
1478
1479   return (REG_P (x)
1480           && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict));
1481 }
1482
1483
1484 /* Return true if symbols of type SYMBOL_TYPE can directly address a value
1485    with mode MODE.  This is used for both symbolic and LO_SUM addresses.  */
1486
1487 static bool
1488 mips_symbolic_address_p (enum mips_symbol_type symbol_type,
1489                          enum machine_mode mode)
1490 {
1491   switch (symbol_type)
1492     {
1493     case SYMBOL_GENERAL:
1494       return !TARGET_MIPS16;
1495
1496     case SYMBOL_SMALL_DATA:
1497       return true;
1498
1499     case SYMBOL_CONSTANT_POOL:
1500       /* PC-relative addressing is only available for lw and ld.  */
1501       return GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1502
1503     case SYMBOL_GOT_LOCAL:
1504       return true;
1505
1506     case SYMBOL_GOT_GLOBAL:
1507       /* The address will have to be loaded from the GOT first.  */
1508       return false;
1509
1510     case SYMBOL_GOTOFF_PAGE:
1511     case SYMBOL_GOTOFF_GLOBAL:
1512     case SYMBOL_GOTOFF_CALL:
1513     case SYMBOL_GOTOFF_LOADGP:
1514     case SYMBOL_TLS:
1515     case SYMBOL_TLSGD:
1516     case SYMBOL_TLSLDM:
1517     case SYMBOL_DTPREL:
1518     case SYMBOL_GOTTPREL:
1519     case SYMBOL_TPREL:
1520     case SYMBOL_64_HIGH:
1521     case SYMBOL_64_MID:
1522     case SYMBOL_64_LOW:
1523       return true;
1524     }
1525   gcc_unreachable ();
1526 }
1527
1528
1529 /* Return true if X is a valid address for machine mode MODE.  If it is,
1530    fill in INFO appropriately.  STRICT is true if we should only accept
1531    hard base registers.  */
1532
1533 static bool
1534 mips_classify_address (struct mips_address_info *info, rtx x,
1535                        enum machine_mode mode, int strict)
1536 {
1537   switch (GET_CODE (x))
1538     {
1539     case REG:
1540     case SUBREG:
1541       info->type = ADDRESS_REG;
1542       info->reg = x;
1543       info->offset = const0_rtx;
1544       return mips_valid_base_register_p (info->reg, mode, strict);
1545
1546     case PLUS:
1547       info->type = ADDRESS_REG;
1548       info->reg = XEXP (x, 0);
1549       info->offset = XEXP (x, 1);
1550       return (mips_valid_base_register_p (info->reg, mode, strict)
1551               && const_arith_operand (info->offset, VOIDmode));
1552
1553     case LO_SUM:
1554       info->type = ADDRESS_LO_SUM;
1555       info->reg = XEXP (x, 0);
1556       info->offset = XEXP (x, 1);
1557       return (mips_valid_base_register_p (info->reg, mode, strict)
1558               && mips_symbolic_constant_p (info->offset, &info->symbol_type)
1559               && mips_symbolic_address_p (info->symbol_type, mode)
1560               && mips_lo_relocs[info->symbol_type] != 0);
1561
1562     case CONST_INT:
1563       /* Small-integer addresses don't occur very often, but they
1564          are legitimate if $0 is a valid base register.  */
1565       info->type = ADDRESS_CONST_INT;
1566       return !TARGET_MIPS16 && SMALL_INT (x);
1567
1568     case CONST:
1569     case LABEL_REF:
1570     case SYMBOL_REF:
1571       info->type = ADDRESS_SYMBOLIC;
1572       return (mips_symbolic_constant_p (x, &info->symbol_type)
1573               && mips_symbolic_address_p (info->symbol_type, mode)
1574               && !mips_split_p[info->symbol_type]);
1575
1576     default:
1577       return false;
1578     }
1579 }
1580
1581 /* Return true if X is a thread-local symbol.  */
1582
1583 static bool
1584 mips_tls_operand_p (rtx x)
1585 {
1586   return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1587 }
1588
1589 /* Return true if X can not be forced into a constant pool.  */
1590
1591 static int
1592 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1593 {
1594   return mips_tls_operand_p (*x);
1595 }
1596
1597 /* Return true if X can not be forced into a constant pool.  */
1598
1599 static bool
1600 mips_cannot_force_const_mem (rtx x)
1601 {
1602   rtx base;
1603   HOST_WIDE_INT offset;
1604
1605   if (!TARGET_MIPS16)
1606     {
1607       /* As an optimization, reject constants that mips_legitimize_move
1608          can expand inline.
1609
1610          Suppose we have a multi-instruction sequence that loads constant C
1611          into register R.  If R does not get allocated a hard register, and
1612          R is used in an operand that allows both registers and memory
1613          references, reload will consider forcing C into memory and using
1614          one of the instruction's memory alternatives.  Returning false
1615          here will force it to use an input reload instead.  */
1616       if (GET_CODE (x) == CONST_INT)
1617         return true;
1618
1619       mips_split_const (x, &base, &offset);
1620       if (symbolic_operand (base, VOIDmode) && SMALL_OPERAND (offset))
1621         return true;
1622     }
1623
1624   if (TARGET_HAVE_TLS && for_each_rtx (&x, &mips_tls_symbol_ref_1, 0))
1625     return true;
1626
1627   return false;
1628 }
1629
1630 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  MIPS16 uses per-function
1631    constant pools, but normal-mode code doesn't need to.  */
1632
1633 static bool
1634 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
1635                                 rtx x ATTRIBUTE_UNUSED)
1636 {
1637   return !TARGET_MIPS16;
1638 }
1639 \f
1640 /* Return the number of instructions needed to load a symbol of the
1641    given type into a register.  If valid in an address, the same number
1642    of instructions are needed for loads and stores.  Treat extended
1643    mips16 instructions as two instructions.  */
1644
1645 static int
1646 mips_symbol_insns (enum mips_symbol_type type)
1647 {
1648   switch (type)
1649     {
1650     case SYMBOL_GENERAL:
1651       /* In mips16 code, general symbols must be fetched from the
1652          constant pool.  */
1653       if (TARGET_MIPS16)
1654         return 0;
1655
1656       /* When using 64-bit symbols, we need 5 preparatory instructions,
1657          such as:
1658
1659              lui     $at,%highest(symbol)
1660              daddiu  $at,$at,%higher(symbol)
1661              dsll    $at,$at,16
1662              daddiu  $at,$at,%hi(symbol)
1663              dsll    $at,$at,16
1664
1665          The final address is then $at + %lo(symbol).  With 32-bit
1666          symbols we just need a preparatory lui.  */
1667       return (ABI_HAS_64BIT_SYMBOLS ? 6 : 2);
1668
1669     case SYMBOL_SMALL_DATA:
1670       return 1;
1671
1672     case SYMBOL_CONSTANT_POOL:
1673       /* This case is for mips16 only.  Assume we'll need an
1674          extended instruction.  */
1675       return 2;
1676
1677     case SYMBOL_GOT_LOCAL:
1678     case SYMBOL_GOT_GLOBAL:
1679       /* Unless -funit-at-a-time is in effect, we can't be sure whether
1680          the local/global classification is accurate.  See override_options
1681          for details.
1682
1683          The worst cases are:
1684
1685          (1) For local symbols when generating o32 or o64 code.  The assembler
1686              will use:
1687
1688                  lw           $at,%got(symbol)
1689                  nop
1690
1691              ...and the final address will be $at + %lo(symbol).
1692
1693          (2) For global symbols when -mxgot.  The assembler will use:
1694
1695                  lui     $at,%got_hi(symbol)
1696                  (d)addu $at,$at,$gp
1697
1698              ...and the final address will be $at + %got_lo(symbol).  */
1699       return 3;
1700
1701     case SYMBOL_GOTOFF_PAGE:
1702     case SYMBOL_GOTOFF_GLOBAL:
1703     case SYMBOL_GOTOFF_CALL:
1704     case SYMBOL_GOTOFF_LOADGP:
1705     case SYMBOL_64_HIGH:
1706     case SYMBOL_64_MID:
1707     case SYMBOL_64_LOW:
1708     case SYMBOL_TLSGD:
1709     case SYMBOL_TLSLDM:
1710     case SYMBOL_DTPREL:
1711     case SYMBOL_GOTTPREL:
1712     case SYMBOL_TPREL:
1713       /* Check whether the offset is a 16- or 32-bit value.  */
1714       return mips_split_p[type] ? 2 : 1;
1715
1716     case SYMBOL_TLS:
1717       /* We don't treat a bare TLS symbol as a constant.  */
1718       return 0;
1719     }
1720   gcc_unreachable ();
1721 }
1722
1723 /* Return true if X is a legitimate $sp-based address for mode MDOE.  */
1724
1725 bool
1726 mips_stack_address_p (rtx x, enum machine_mode mode)
1727 {
1728   struct mips_address_info addr;
1729
1730   return (mips_classify_address (&addr, x, mode, false)
1731           && addr.type == ADDRESS_REG
1732           && addr.reg == stack_pointer_rtx);
1733 }
1734
1735 /* Return true if a value at OFFSET bytes from BASE can be accessed
1736    using an unextended mips16 instruction.  MODE is the mode of the
1737    value.
1738
1739    Usually the offset in an unextended instruction is a 5-bit field.
1740    The offset is unsigned and shifted left once for HIs, twice
1741    for SIs, and so on.  An exception is SImode accesses off the
1742    stack pointer, which have an 8-bit immediate field.  */
1743
1744 static bool
1745 mips16_unextended_reference_p (enum machine_mode mode, rtx base, rtx offset)
1746 {
1747   if (TARGET_MIPS16
1748       && GET_CODE (offset) == CONST_INT
1749       && INTVAL (offset) >= 0
1750       && (INTVAL (offset) & (GET_MODE_SIZE (mode) - 1)) == 0)
1751     {
1752       if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
1753         return INTVAL (offset) < 256 * GET_MODE_SIZE (mode);
1754       return INTVAL (offset) < 32 * GET_MODE_SIZE (mode);
1755     }
1756   return false;
1757 }
1758
1759
1760 /* Return the number of instructions needed to load or store a value
1761    of mode MODE at X.  Return 0 if X isn't valid for MODE.
1762
1763    For mips16 code, count extended instructions as two instructions.  */
1764
1765 int
1766 mips_address_insns (rtx x, enum machine_mode mode)
1767 {
1768   struct mips_address_info addr;
1769   int factor;
1770
1771   if (mode == BLKmode)
1772     /* BLKmode is used for single unaligned loads and stores.  */
1773     factor = 1;
1774   else
1775     /* Each word of a multi-word value will be accessed individually.  */
1776     factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1777
1778   if (mips_classify_address (&addr, x, mode, false))
1779     switch (addr.type)
1780       {
1781       case ADDRESS_REG:
1782         if (TARGET_MIPS16
1783             && !mips16_unextended_reference_p (mode, addr.reg, addr.offset))
1784           return factor * 2;
1785         return factor;
1786
1787       case ADDRESS_LO_SUM:
1788         return (TARGET_MIPS16 ? factor * 2 : factor);
1789
1790       case ADDRESS_CONST_INT:
1791         return factor;
1792
1793       case ADDRESS_SYMBOLIC:
1794         return factor * mips_symbol_insns (addr.symbol_type);
1795       }
1796   return 0;
1797 }
1798
1799
1800 /* Likewise for constant X.  */
1801
1802 int
1803 mips_const_insns (rtx x)
1804 {
1805   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
1806   enum mips_symbol_type symbol_type;
1807   HOST_WIDE_INT offset;
1808
1809   switch (GET_CODE (x))
1810     {
1811     case HIGH:
1812       if (TARGET_MIPS16
1813           || !mips_symbolic_constant_p (XEXP (x, 0), &symbol_type)
1814           || !mips_split_p[symbol_type])
1815         return 0;
1816
1817       return 1;
1818
1819     case CONST_INT:
1820       if (TARGET_MIPS16)
1821         /* Unsigned 8-bit constants can be loaded using an unextended
1822            LI instruction.  Unsigned 16-bit constants can be loaded
1823            using an extended LI.  Negative constants must be loaded
1824            using LI and then negated.  */
1825         return (INTVAL (x) >= 0 && INTVAL (x) < 256 ? 1
1826                 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
1827                 : INTVAL (x) > -256 && INTVAL (x) < 0 ? 2
1828                 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
1829                 : 0);
1830
1831       return mips_build_integer (codes, INTVAL (x));
1832
1833     case CONST_DOUBLE:
1834     case CONST_VECTOR:
1835       return (!TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0);
1836
1837     case CONST:
1838       if (CONST_GP_P (x))
1839         return 1;
1840
1841       /* See if we can refer to X directly.  */
1842       if (mips_symbolic_constant_p (x, &symbol_type))
1843         return mips_symbol_insns (symbol_type);
1844
1845       /* Otherwise try splitting the constant into a base and offset.
1846          16-bit offsets can be added using an extra addiu.  Larger offsets
1847          must be calculated separately and then added to the base.  */
1848       mips_split_const (x, &x, &offset);
1849       if (offset != 0)
1850         {
1851           int n = mips_const_insns (x);
1852           if (n != 0)
1853             {
1854               if (SMALL_OPERAND (offset))
1855                 return n + 1;
1856               else
1857                 return n + 1 + mips_build_integer (codes, offset);
1858             }
1859         }
1860       return 0;
1861
1862     case SYMBOL_REF:
1863     case LABEL_REF:
1864       return mips_symbol_insns (mips_classify_symbol (x));
1865
1866     default:
1867       return 0;
1868     }
1869 }
1870
1871
1872 /* Return the number of instructions needed for memory reference X.
1873    Count extended mips16 instructions as two instructions.  */
1874
1875 int
1876 mips_fetch_insns (rtx x)
1877 {
1878   gcc_assert (MEM_P (x));
1879   return mips_address_insns (XEXP (x, 0), GET_MODE (x));
1880 }
1881
1882
1883 /* Return the number of instructions needed for an integer division.  */
1884
1885 int
1886 mips_idiv_insns (void)
1887 {
1888   int count;
1889
1890   count = 1;
1891   if (TARGET_CHECK_ZERO_DIV)
1892     {
1893       if (GENERATE_DIVIDE_TRAPS)
1894         count++;
1895       else
1896         count += 2;
1897     }
1898
1899   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
1900     count++;
1901   return count;
1902 }
1903 \f
1904 /* This function is used to implement GO_IF_LEGITIMATE_ADDRESS.  It
1905    returns a nonzero value if X is a legitimate address for a memory
1906    operand of the indicated MODE.  STRICT is nonzero if this function
1907    is called during reload.  */
1908
1909 bool
1910 mips_legitimate_address_p (enum machine_mode mode, rtx x, int strict)
1911 {
1912   struct mips_address_info addr;
1913
1914   return mips_classify_address (&addr, x, mode, strict);
1915 }
1916
1917
1918 /* Copy VALUE to a register and return that register.  If new psuedos
1919    are allowed, copy it into a new register, otherwise use DEST.  */
1920
1921 static rtx
1922 mips_force_temporary (rtx dest, rtx value)
1923 {
1924   if (!no_new_pseudos)
1925     return force_reg (Pmode, value);
1926   else
1927     {
1928       emit_move_insn (copy_rtx (dest), value);
1929       return dest;
1930     }
1931 }
1932
1933
1934 /* Return a LO_SUM expression for ADDR.  TEMP is as for mips_force_temporary
1935    and is used to load the high part into a register.  */
1936
1937 rtx
1938 mips_split_symbol (rtx temp, rtx addr)
1939 {
1940   rtx high;
1941
1942   if (TARGET_MIPS16)
1943     high = mips16_gp_pseudo_reg ();
1944   else
1945     high = mips_force_temporary (temp, gen_rtx_HIGH (Pmode, copy_rtx (addr)));
1946   return gen_rtx_LO_SUM (Pmode, high, addr);
1947 }
1948
1949
1950 /* Return an UNSPEC address with underlying address ADDRESS and symbol
1951    type SYMBOL_TYPE.  */
1952
1953 rtx
1954 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
1955 {
1956   rtx base;
1957   HOST_WIDE_INT offset;
1958
1959   mips_split_const (address, &base, &offset);
1960   base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
1961                          UNSPEC_ADDRESS_FIRST + symbol_type);
1962   return plus_constant (gen_rtx_CONST (Pmode, base), offset);
1963 }
1964
1965
1966 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
1967    high part to BASE and return the result.  Just return BASE otherwise.
1968    TEMP is available as a temporary register if needed.
1969
1970    The returned expression can be used as the first operand to a LO_SUM.  */
1971
1972 static rtx
1973 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
1974                          enum mips_symbol_type symbol_type)
1975 {
1976   if (mips_split_p[symbol_type])
1977     {
1978       addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
1979       addr = mips_force_temporary (temp, addr);
1980       return mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
1981     }
1982   return base;
1983 }
1984
1985
1986 /* Return a legitimate address for REG + OFFSET.  TEMP is as for
1987    mips_force_temporary; it is only needed when OFFSET is not a
1988    SMALL_OPERAND.  */
1989
1990 static rtx
1991 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
1992 {
1993   if (!SMALL_OPERAND (offset))
1994     {
1995       rtx high;
1996       if (TARGET_MIPS16)
1997         {
1998           /* Load the full offset into a register so that we can use
1999              an unextended instruction for the address itself.  */
2000           high = GEN_INT (offset);
2001           offset = 0;
2002         }
2003       else
2004         {
2005           /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.  */
2006           high = GEN_INT (CONST_HIGH_PART (offset));
2007           offset = CONST_LOW_PART (offset);
2008         }
2009       high = mips_force_temporary (temp, high);
2010       reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
2011     }
2012   return plus_constant (reg, offset);
2013 }
2014
2015 /* Emit a call to __tls_get_addr.  SYM is the TLS symbol we are
2016    referencing, and TYPE is the symbol type to use (either global
2017    dynamic or local dynamic).  V0 is an RTX for the return value
2018    location.  The entire insn sequence is returned.  */
2019
2020 static GTY(()) rtx mips_tls_symbol;
2021
2022 static rtx
2023 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
2024 {
2025   rtx insn, loc, tga, a0;
2026
2027   a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
2028
2029   if (!mips_tls_symbol)
2030     mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
2031
2032   loc = mips_unspec_address (sym, type);
2033
2034   start_sequence ();
2035
2036   emit_insn (gen_rtx_SET (Pmode, a0,
2037                           gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
2038   tga = gen_rtx_MEM (Pmode, mips_tls_symbol);
2039   insn = emit_call_insn (gen_call_value (v0, tga, const0_rtx, const0_rtx));
2040   CONST_OR_PURE_CALL_P (insn) = 1;
2041   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), v0);
2042   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
2043   insn = get_insns ();
2044
2045   end_sequence ();
2046
2047   return insn;
2048 }
2049
2050 /* Generate the code to access LOC, a thread local SYMBOL_REF.  The
2051    return value will be a valid address and move_operand (either a REG
2052    or a LO_SUM).  */
2053
2054 static rtx
2055 mips_legitimize_tls_address (rtx loc)
2056 {
2057   rtx dest, insn, v0, v1, tmp1, tmp2, eqv;
2058   enum tls_model model;
2059
2060   v0 = gen_rtx_REG (Pmode, GP_RETURN);
2061   v1 = gen_rtx_REG (Pmode, GP_RETURN + 1);
2062
2063   model = SYMBOL_REF_TLS_MODEL (loc);
2064   /* Only TARGET_ABICALLS code can have more than one module; other
2065      code must be be static and should not use a GOT.  All TLS models
2066      reduce to local exec in this situation.  */
2067   if (!TARGET_ABICALLS)
2068     model = TLS_MODEL_LOCAL_EXEC;
2069
2070   switch (model)
2071     {
2072     case TLS_MODEL_GLOBAL_DYNAMIC:
2073       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
2074       dest = gen_reg_rtx (Pmode);
2075       emit_libcall_block (insn, dest, v0, loc);
2076       break;
2077
2078     case TLS_MODEL_LOCAL_DYNAMIC:
2079       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
2080       tmp1 = gen_reg_rtx (Pmode);
2081
2082       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2083          share the LDM result with other LD model accesses.  */
2084       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2085                             UNSPEC_TLS_LDM);
2086       emit_libcall_block (insn, tmp1, v0, eqv);
2087
2088       tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
2089       dest = gen_rtx_LO_SUM (Pmode, tmp2,
2090                              mips_unspec_address (loc, SYMBOL_DTPREL));
2091       break;
2092
2093     case TLS_MODEL_INITIAL_EXEC:
2094       tmp1 = gen_reg_rtx (Pmode);
2095       tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
2096       if (Pmode == DImode)
2097         {
2098           emit_insn (gen_tls_get_tp_di (v1));
2099           emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
2100         }
2101       else
2102         {
2103           emit_insn (gen_tls_get_tp_si (v1));
2104           emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
2105         }
2106       dest = gen_reg_rtx (Pmode);
2107       emit_insn (gen_add3_insn (dest, tmp1, v1));
2108       break;
2109
2110     case TLS_MODEL_LOCAL_EXEC:
2111       if (Pmode == DImode)
2112         emit_insn (gen_tls_get_tp_di (v1));
2113       else
2114         emit_insn (gen_tls_get_tp_si (v1));
2115
2116       tmp1 = mips_unspec_offset_high (NULL, v1, loc, SYMBOL_TPREL);
2117       dest = gen_rtx_LO_SUM (Pmode, tmp1,
2118                              mips_unspec_address (loc, SYMBOL_TPREL));
2119       break;
2120
2121     default:
2122       gcc_unreachable ();
2123     }
2124
2125   return dest;
2126 }
2127
2128 /* This function is used to implement LEGITIMIZE_ADDRESS.  If *XLOC can
2129    be legitimized in a way that the generic machinery might not expect,
2130    put the new address in *XLOC and return true.  MODE is the mode of
2131    the memory being accessed.  */
2132
2133 bool
2134 mips_legitimize_address (rtx *xloc, enum machine_mode mode)
2135 {
2136   enum mips_symbol_type symbol_type;
2137
2138   if (mips_tls_operand_p (*xloc))
2139     {
2140       *xloc = mips_legitimize_tls_address (*xloc);
2141       return true;
2142     }
2143
2144   /* See if the address can split into a high part and a LO_SUM.  */
2145   if (mips_symbolic_constant_p (*xloc, &symbol_type)
2146       && mips_symbolic_address_p (symbol_type, mode)
2147       && mips_split_p[symbol_type])
2148     {
2149       *xloc = mips_split_symbol (0, *xloc);
2150       return true;
2151     }
2152
2153   if (GET_CODE (*xloc) == PLUS && GET_CODE (XEXP (*xloc, 1)) == CONST_INT)
2154     {
2155       /* Handle REG + CONSTANT using mips_add_offset.  */
2156       rtx reg;
2157
2158       reg = XEXP (*xloc, 0);
2159       if (!mips_valid_base_register_p (reg, mode, 0))
2160         reg = copy_to_mode_reg (Pmode, reg);
2161       *xloc = mips_add_offset (0, reg, INTVAL (XEXP (*xloc, 1)));
2162       return true;
2163     }
2164
2165   return false;
2166 }
2167
2168
2169 /* Subroutine of mips_build_integer (with the same interface).
2170    Assume that the final action in the sequence should be a left shift.  */
2171
2172 static unsigned int
2173 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
2174 {
2175   unsigned int i, shift;
2176
2177   /* Shift VALUE right until its lowest bit is set.  Shift arithmetically
2178      since signed numbers are easier to load than unsigned ones.  */
2179   shift = 0;
2180   while ((value & 1) == 0)
2181     value /= 2, shift++;
2182
2183   i = mips_build_integer (codes, value);
2184   codes[i].code = ASHIFT;
2185   codes[i].value = shift;
2186   return i + 1;
2187 }
2188
2189
2190 /* As for mips_build_shift, but assume that the final action will be
2191    an IOR or PLUS operation.  */
2192
2193 static unsigned int
2194 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
2195 {
2196   unsigned HOST_WIDE_INT high;
2197   unsigned int i;
2198
2199   high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
2200   if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
2201     {
2202       /* The constant is too complex to load with a simple lui/ori pair
2203          so our goal is to clear as many trailing zeros as possible.
2204          In this case, we know bit 16 is set and that the low 16 bits
2205          form a negative number.  If we subtract that number from VALUE,
2206          we will clear at least the lowest 17 bits, maybe more.  */
2207       i = mips_build_integer (codes, CONST_HIGH_PART (value));
2208       codes[i].code = PLUS;
2209       codes[i].value = CONST_LOW_PART (value);
2210     }
2211   else
2212     {
2213       i = mips_build_integer (codes, high);
2214       codes[i].code = IOR;
2215       codes[i].value = value & 0xffff;
2216     }
2217   return i + 1;
2218 }
2219
2220
2221 /* Fill CODES with a sequence of rtl operations to load VALUE.
2222    Return the number of operations needed.  */
2223
2224 static unsigned int
2225 mips_build_integer (struct mips_integer_op *codes,
2226                     unsigned HOST_WIDE_INT value)
2227 {
2228   if (SMALL_OPERAND (value)
2229       || SMALL_OPERAND_UNSIGNED (value)
2230       || LUI_OPERAND (value))
2231     {
2232       /* The value can be loaded with a single instruction.  */
2233       codes[0].code = UNKNOWN;
2234       codes[0].value = value;
2235       return 1;
2236     }
2237   else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
2238     {
2239       /* Either the constant is a simple LUI/ORI combination or its
2240          lowest bit is set.  We don't want to shift in this case.  */
2241       return mips_build_lower (codes, value);
2242     }
2243   else if ((value & 0xffff) == 0)
2244     {
2245       /* The constant will need at least three actions.  The lowest
2246          16 bits are clear, so the final action will be a shift.  */
2247       return mips_build_shift (codes, value);
2248     }
2249   else
2250     {
2251       /* The final action could be a shift, add or inclusive OR.
2252          Rather than use a complex condition to select the best
2253          approach, try both mips_build_shift and mips_build_lower
2254          and pick the one that gives the shortest sequence.
2255          Note that this case is only used once per constant.  */
2256       struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
2257       unsigned int cost, alt_cost;
2258
2259       cost = mips_build_shift (codes, value);
2260       alt_cost = mips_build_lower (alt_codes, value);
2261       if (alt_cost < cost)
2262         {
2263           memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
2264           cost = alt_cost;
2265         }
2266       return cost;
2267     }
2268 }
2269
2270
2271 /* Load VALUE into DEST, using TEMP as a temporary register if need be.  */
2272
2273 void
2274 mips_move_integer (rtx dest, rtx temp, unsigned HOST_WIDE_INT value)
2275 {
2276   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2277   enum machine_mode mode;
2278   unsigned int i, cost;
2279   rtx x;
2280
2281   mode = GET_MODE (dest);
2282   cost = mips_build_integer (codes, value);
2283
2284   /* Apply each binary operation to X.  Invariant: X is a legitimate
2285      source operand for a SET pattern.  */
2286   x = GEN_INT (codes[0].value);
2287   for (i = 1; i < cost; i++)
2288     {
2289       if (no_new_pseudos)
2290         {
2291           emit_insn (gen_rtx_SET (VOIDmode, temp, x));
2292           x = temp;
2293         }
2294       else
2295         x = force_reg (mode, x);
2296       x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
2297     }
2298
2299   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
2300 }
2301
2302
2303 /* Subroutine of mips_legitimize_move.  Move constant SRC into register
2304    DEST given that SRC satisfies immediate_operand but doesn't satisfy
2305    move_operand.  */
2306
2307 static void
2308 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
2309 {
2310   rtx base;
2311   HOST_WIDE_INT offset;
2312
2313   /* Split moves of big integers into smaller pieces.  */
2314   if (splittable_const_int_operand (src, mode))
2315     {
2316       mips_move_integer (dest, dest, INTVAL (src));
2317       return;
2318     }
2319
2320   /* Split moves of symbolic constants into high/low pairs.  */
2321   if (splittable_symbolic_operand (src, mode))
2322     {
2323       emit_insn (gen_rtx_SET (VOIDmode, dest, mips_split_symbol (dest, src)));
2324       return;
2325     }
2326
2327   if (mips_tls_operand_p (src))
2328     {
2329       emit_move_insn (dest, mips_legitimize_tls_address (src));
2330       return;
2331     }
2332
2333   /* If we have (const (plus symbol offset)), load the symbol first
2334      and then add in the offset.  This is usually better than forcing
2335      the constant into memory, at least in non-mips16 code.  */
2336   mips_split_const (src, &base, &offset);
2337   if (!TARGET_MIPS16
2338       && offset != 0
2339       && (!no_new_pseudos || SMALL_OPERAND (offset)))
2340     {
2341       base = mips_force_temporary (dest, base);
2342       emit_move_insn (dest, mips_add_offset (0, base, offset));
2343       return;
2344     }
2345
2346   src = force_const_mem (mode, src);
2347
2348   /* When using explicit relocs, constant pool references are sometimes
2349      not legitimate addresses.  */
2350   if (!memory_operand (src, VOIDmode))
2351     src = replace_equiv_address (src, mips_split_symbol (dest, XEXP (src, 0)));
2352   emit_move_insn (dest, src);
2353 }
2354
2355
2356 /* If (set DEST SRC) is not a valid instruction, emit an equivalent
2357    sequence that is valid.  */
2358
2359 bool
2360 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
2361 {
2362   if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
2363     {
2364       emit_move_insn (dest, force_reg (mode, src));
2365       return true;
2366     }
2367
2368   /* Check for individual, fully-reloaded mflo and mfhi instructions.  */
2369   if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
2370       && REG_P (src) && MD_REG_P (REGNO (src))
2371       && REG_P (dest) && GP_REG_P (REGNO (dest)))
2372     {
2373       int other_regno = REGNO (src) == HI_REGNUM ? LO_REGNUM : HI_REGNUM;
2374       if (GET_MODE_SIZE (mode) <= 4)
2375         emit_insn (gen_mfhilo_si (gen_rtx_REG (SImode, REGNO (dest)),
2376                                   gen_rtx_REG (SImode, REGNO (src)),
2377                                   gen_rtx_REG (SImode, other_regno)));
2378       else
2379         emit_insn (gen_mfhilo_di (gen_rtx_REG (DImode, REGNO (dest)),
2380                                   gen_rtx_REG (DImode, REGNO (src)),
2381                                   gen_rtx_REG (DImode, other_regno)));
2382       return true;
2383     }
2384
2385   /* We need to deal with constants that would be legitimate
2386      immediate_operands but not legitimate move_operands.  */
2387   if (CONSTANT_P (src) && !move_operand (src, mode))
2388     {
2389       mips_legitimize_const_move (mode, dest, src);
2390       set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
2391       return true;
2392     }
2393   return false;
2394 }
2395 \f
2396 /* We need a lot of little routines to check constant values on the
2397    mips16.  These are used to figure out how long the instruction will
2398    be.  It would be much better to do this using constraints, but
2399    there aren't nearly enough letters available.  */
2400
2401 static int
2402 m16_check_op (rtx op, int low, int high, int mask)
2403 {
2404   return (GET_CODE (op) == CONST_INT
2405           && INTVAL (op) >= low
2406           && INTVAL (op) <= high
2407           && (INTVAL (op) & mask) == 0);
2408 }
2409
2410 int
2411 m16_uimm3_b (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2412 {
2413   return m16_check_op (op, 0x1, 0x8, 0);
2414 }
2415
2416 int
2417 m16_simm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2418 {
2419   return m16_check_op (op, - 0x8, 0x7, 0);
2420 }
2421
2422 int
2423 m16_nsimm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2424 {
2425   return m16_check_op (op, - 0x7, 0x8, 0);
2426 }
2427
2428 int
2429 m16_simm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2430 {
2431   return m16_check_op (op, - 0x10, 0xf, 0);
2432 }
2433
2434 int
2435 m16_nsimm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2436 {
2437   return m16_check_op (op, - 0xf, 0x10, 0);
2438 }
2439
2440 int
2441 m16_uimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2442 {
2443   return m16_check_op (op, (- 0x10) << 2, 0xf << 2, 3);
2444 }
2445
2446 int
2447 m16_nuimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2448 {
2449   return m16_check_op (op, (- 0xf) << 2, 0x10 << 2, 3);
2450 }
2451
2452 int
2453 m16_simm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2454 {
2455   return m16_check_op (op, - 0x80, 0x7f, 0);
2456 }
2457
2458 int
2459 m16_nsimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2460 {
2461   return m16_check_op (op, - 0x7f, 0x80, 0);
2462 }
2463
2464 int
2465 m16_uimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2466 {
2467   return m16_check_op (op, 0x0, 0xff, 0);
2468 }
2469
2470 int
2471 m16_nuimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2472 {
2473   return m16_check_op (op, - 0xff, 0x0, 0);
2474 }
2475
2476 int
2477 m16_uimm8_m1_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2478 {
2479   return m16_check_op (op, - 0x1, 0xfe, 0);
2480 }
2481
2482 int
2483 m16_uimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2484 {
2485   return m16_check_op (op, 0x0, 0xff << 2, 3);
2486 }
2487
2488 int
2489 m16_nuimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2490 {
2491   return m16_check_op (op, (- 0xff) << 2, 0x0, 3);
2492 }
2493
2494 int
2495 m16_simm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2496 {
2497   return m16_check_op (op, (- 0x80) << 3, 0x7f << 3, 7);
2498 }
2499
2500 int
2501 m16_nsimm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2502 {
2503   return m16_check_op (op, (- 0x7f) << 3, 0x80 << 3, 7);
2504 }
2505 \f
2506 static bool
2507 mips_rtx_costs (rtx x, int code, int outer_code, int *total)
2508 {
2509   enum machine_mode mode = GET_MODE (x);
2510   bool float_mode_p = FLOAT_MODE_P (mode);
2511
2512   switch (code)
2513     {
2514     case CONST_INT:
2515       if (TARGET_MIPS16)
2516         {
2517           /* A number between 1 and 8 inclusive is efficient for a shift.
2518              Otherwise, we will need an extended instruction.  */
2519           if ((outer_code) == ASHIFT || (outer_code) == ASHIFTRT
2520               || (outer_code) == LSHIFTRT)
2521             {
2522               if (INTVAL (x) >= 1 && INTVAL (x) <= 8)
2523                 *total = 0;
2524               else
2525                 *total = COSTS_N_INSNS (1);
2526               return true;
2527             }
2528
2529           /* We can use cmpi for an xor with an unsigned 16 bit value.  */
2530           if ((outer_code) == XOR
2531               && INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
2532             {
2533               *total = 0;
2534               return true;
2535             }
2536
2537           /* We may be able to use slt or sltu for a comparison with a
2538              signed 16 bit value.  (The boundary conditions aren't quite
2539              right, but this is just a heuristic anyhow.)  */
2540           if (((outer_code) == LT || (outer_code) == LE
2541                || (outer_code) == GE || (outer_code) == GT
2542                || (outer_code) == LTU || (outer_code) == LEU
2543                || (outer_code) == GEU || (outer_code) == GTU)
2544               && INTVAL (x) >= -0x8000 && INTVAL (x) < 0x8000)
2545             {
2546               *total = 0;
2547               return true;
2548             }
2549
2550           /* Equality comparisons with 0 are cheap.  */
2551           if (((outer_code) == EQ || (outer_code) == NE)
2552               && INTVAL (x) == 0)
2553             {
2554               *total = 0;
2555               return true;
2556             }
2557
2558           /* Constants in the range 0...255 can be loaded with an unextended
2559              instruction.  They are therefore as cheap as a register move.
2560
2561              Given the choice between "li R1,0...255" and "move R1,R2"
2562              (where R2 is a known constant), it is usually better to use "li",
2563              since we do not want to unnecessarily extend the lifetime
2564              of R2.  */
2565           if (outer_code == SET
2566               && INTVAL (x) >= 0
2567               && INTVAL (x) < 256)
2568             {
2569               *total = 0;
2570               return true;
2571             }
2572         }
2573       else
2574         {
2575           /* These can be used anywhere. */
2576           *total = 0;
2577           return true;
2578         }
2579
2580       /* Otherwise fall through to the handling below because
2581          we'll need to construct the constant.  */
2582
2583     case CONST:
2584     case SYMBOL_REF:
2585     case LABEL_REF:
2586     case CONST_DOUBLE:
2587       if (LEGITIMATE_CONSTANT_P (x))
2588         {
2589           *total = COSTS_N_INSNS (1);
2590           return true;
2591         }
2592       else
2593         {
2594           /* The value will need to be fetched from the constant pool.  */
2595           *total = CONSTANT_POOL_COST;
2596           return true;
2597         }
2598
2599     case MEM:
2600       {
2601         /* If the address is legitimate, return the number of
2602            instructions it needs, otherwise use the default handling.  */
2603         int n = mips_address_insns (XEXP (x, 0), GET_MODE (x));
2604         if (n > 0)
2605           {
2606             *total = COSTS_N_INSNS (n + 1);
2607             return true;
2608           }
2609         return false;
2610       }
2611
2612     case FFS:
2613       *total = COSTS_N_INSNS (6);
2614       return true;
2615
2616     case NOT:
2617       *total = COSTS_N_INSNS ((mode == DImode && !TARGET_64BIT) ? 2 : 1);
2618       return true;
2619
2620     case AND:
2621     case IOR:
2622     case XOR:
2623       if (mode == DImode && !TARGET_64BIT)
2624         {
2625           *total = COSTS_N_INSNS (2);
2626           return true;
2627         }
2628       return false;
2629
2630     case ASHIFT:
2631     case ASHIFTRT:
2632     case LSHIFTRT:
2633       if (mode == DImode && !TARGET_64BIT)
2634         {
2635           *total = COSTS_N_INSNS ((GET_CODE (XEXP (x, 1)) == CONST_INT)
2636                                   ? 4 : 12);
2637           return true;
2638         }
2639       return false;
2640
2641     case ABS:
2642       if (float_mode_p)
2643         *total = COSTS_N_INSNS (1);
2644       else
2645         *total = COSTS_N_INSNS (4);
2646       return true;
2647
2648     case LO_SUM:
2649       *total = COSTS_N_INSNS (1);
2650       return true;
2651
2652     case PLUS:
2653     case MINUS:
2654       if (float_mode_p)
2655         {
2656           *total = mips_cost->fp_add;
2657           return true;
2658         }
2659
2660       else if (mode == DImode && !TARGET_64BIT)
2661         {
2662           *total = COSTS_N_INSNS (4);
2663           return true;
2664         }
2665       return false;
2666
2667     case NEG:
2668       if (mode == DImode && !TARGET_64BIT)
2669         {
2670           *total = COSTS_N_INSNS (4);
2671           return true;
2672         }
2673       return false;
2674
2675     case MULT:
2676       if (mode == SFmode)
2677         *total = mips_cost->fp_mult_sf;
2678
2679       else if (mode == DFmode)
2680         *total = mips_cost->fp_mult_df;
2681
2682       else if (mode == SImode)
2683         *total = mips_cost->int_mult_si;
2684
2685       else
2686         *total = mips_cost->int_mult_di;
2687
2688       return true;
2689
2690     case DIV:
2691     case MOD:
2692       if (float_mode_p)
2693         {
2694           if (mode == SFmode)
2695             *total = mips_cost->fp_div_sf;
2696           else
2697             *total = mips_cost->fp_div_df;
2698
2699           return true;
2700         }
2701       /* Fall through.  */
2702
2703     case UDIV:
2704     case UMOD:
2705       if (mode == DImode)
2706         *total = mips_cost->int_div_di;
2707       else
2708         *total = mips_cost->int_div_si;
2709
2710       return true;
2711
2712     case SIGN_EXTEND:
2713       /* A sign extend from SImode to DImode in 64 bit mode is often
2714          zero instructions, because the result can often be used
2715          directly by another instruction; we'll call it one.  */
2716       if (TARGET_64BIT && mode == DImode
2717           && GET_MODE (XEXP (x, 0)) == SImode)
2718         *total = COSTS_N_INSNS (1);
2719       else
2720         *total = COSTS_N_INSNS (2);
2721       return true;
2722
2723     case ZERO_EXTEND:
2724       if (TARGET_64BIT && mode == DImode
2725           && GET_MODE (XEXP (x, 0)) == SImode)
2726         *total = COSTS_N_INSNS (2);
2727       else
2728         *total = COSTS_N_INSNS (1);
2729       return true;
2730
2731     case FLOAT:
2732     case UNSIGNED_FLOAT:
2733     case FIX:
2734     case FLOAT_EXTEND:
2735     case FLOAT_TRUNCATE:
2736     case SQRT:
2737       *total = mips_cost->fp_add;
2738       return true;
2739
2740     default:
2741       return false;
2742     }
2743 }
2744
2745 /* Provide the costs of an addressing mode that contains ADDR.
2746    If ADDR is not a valid address, its cost is irrelevant.  */
2747
2748 static int
2749 mips_address_cost (rtx addr)
2750 {
2751   return mips_address_insns (addr, SImode);
2752 }
2753 \f
2754 /* Return one word of double-word value OP, taking into account the fixed
2755    endianness of certain registers.  HIGH_P is true to select the high part,
2756    false to select the low part.  */
2757
2758 rtx
2759 mips_subword (rtx op, int high_p)
2760 {
2761   unsigned int byte;
2762   enum machine_mode mode;
2763
2764   mode = GET_MODE (op);
2765   if (mode == VOIDmode)
2766     mode = DImode;
2767
2768   if (TARGET_BIG_ENDIAN ? !high_p : high_p)
2769     byte = UNITS_PER_WORD;
2770   else
2771     byte = 0;
2772
2773   if (REG_P (op))
2774     {
2775       if (FP_REG_P (REGNO (op)))
2776         return gen_rtx_REG (word_mode, high_p ? REGNO (op) + 1 : REGNO (op));
2777       if (ACC_HI_REG_P (REGNO (op)))
2778         return gen_rtx_REG (word_mode, high_p ? REGNO (op) : REGNO (op) + 1);
2779     }
2780
2781   if (MEM_P (op))
2782     return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
2783
2784   return simplify_gen_subreg (word_mode, op, mode, byte);
2785 }
2786
2787
2788 /* Return true if a 64-bit move from SRC to DEST should be split into two.  */
2789
2790 bool
2791 mips_split_64bit_move_p (rtx dest, rtx src)
2792 {
2793   if (TARGET_64BIT)
2794     return false;
2795
2796   /* FP->FP moves can be done in a single instruction.  */
2797   if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
2798     return false;
2799
2800   /* Check for floating-point loads and stores.  They can be done using
2801      ldc1 and sdc1 on MIPS II and above.  */
2802   if (mips_isa > 1)
2803     {
2804       if (FP_REG_RTX_P (dest) && MEM_P (src))
2805         return false;
2806       if (FP_REG_RTX_P (src) && MEM_P (dest))
2807         return false;
2808     }
2809   return true;
2810 }
2811
2812
2813 /* Split a 64-bit move from SRC to DEST assuming that
2814    mips_split_64bit_move_p holds.
2815
2816    Moves into and out of FPRs cause some difficulty here.  Such moves
2817    will always be DFmode, since paired FPRs are not allowed to store
2818    DImode values.  The most natural representation would be two separate
2819    32-bit moves, such as:
2820
2821         (set (reg:SI $f0) (mem:SI ...))
2822         (set (reg:SI $f1) (mem:SI ...))
2823
2824    However, the second insn is invalid because odd-numbered FPRs are
2825    not allowed to store independent values.  Use the patterns load_df_low,
2826    load_df_high and store_df_high instead.  */
2827
2828 void
2829 mips_split_64bit_move (rtx dest, rtx src)
2830 {
2831   if (FP_REG_RTX_P (dest))
2832     {
2833       /* Loading an FPR from memory or from GPRs.  */
2834       emit_insn (gen_load_df_low (copy_rtx (dest), mips_subword (src, 0)));
2835       emit_insn (gen_load_df_high (dest, mips_subword (src, 1),
2836                                    copy_rtx (dest)));
2837     }
2838   else if (FP_REG_RTX_P (src))
2839     {
2840       /* Storing an FPR into memory or GPRs.  */
2841       emit_move_insn (mips_subword (dest, 0), mips_subword (src, 0));
2842       emit_insn (gen_store_df_high (mips_subword (dest, 1), src));
2843     }
2844   else
2845     {
2846       /* The operation can be split into two normal moves.  Decide in
2847          which order to do them.  */
2848       rtx low_dest;
2849
2850       low_dest = mips_subword (dest, 0);
2851       if (REG_P (low_dest)
2852           && reg_overlap_mentioned_p (low_dest, src))
2853         {
2854           emit_move_insn (mips_subword (dest, 1), mips_subword (src, 1));
2855           emit_move_insn (low_dest, mips_subword (src, 0));
2856         }
2857       else
2858         {
2859           emit_move_insn (low_dest, mips_subword (src, 0));
2860           emit_move_insn (mips_subword (dest, 1), mips_subword (src, 1));
2861         }
2862     }
2863 }
2864 \f
2865 /* Return the appropriate instructions to move SRC into DEST.  Assume
2866    that SRC is operand 1 and DEST is operand 0.  */
2867
2868 const char *
2869 mips_output_move (rtx dest, rtx src)
2870 {
2871   enum rtx_code dest_code, src_code;
2872   bool dbl_p;
2873
2874   dest_code = GET_CODE (dest);
2875   src_code = GET_CODE (src);
2876   dbl_p = (GET_MODE_SIZE (GET_MODE (dest)) == 8);
2877
2878   if (dbl_p && mips_split_64bit_move_p (dest, src))
2879     return "#";
2880
2881   if ((src_code == REG && GP_REG_P (REGNO (src)))
2882       || (!TARGET_MIPS16 && src == CONST0_RTX (GET_MODE (dest))))
2883     {
2884       if (dest_code == REG)
2885         {
2886           if (GP_REG_P (REGNO (dest)))
2887             return "move\t%0,%z1";
2888
2889           if (MD_REG_P (REGNO (dest)))
2890             return "mt%0\t%z1";
2891
2892           if (DSP_ACC_REG_P (REGNO (dest)))
2893             {
2894               static char retval[] = "mt__\t%z1,%q0";
2895               retval[2] = reg_names[REGNO (dest)][4];
2896               retval[3] = reg_names[REGNO (dest)][5];
2897               return retval;
2898             }
2899
2900           if (FP_REG_P (REGNO (dest)))
2901             return (dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0");
2902
2903           if (ALL_COP_REG_P (REGNO (dest)))
2904             {
2905               static char retval[] = "dmtc_\t%z1,%0";
2906
2907               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
2908               return (dbl_p ? retval : retval + 1);
2909             }
2910         }
2911       if (dest_code == MEM)
2912         return (dbl_p ? "sd\t%z1,%0" : "sw\t%z1,%0");
2913     }
2914   if (dest_code == REG && GP_REG_P (REGNO (dest)))
2915     {
2916       if (src_code == REG)
2917         {
2918           if (DSP_ACC_REG_P (REGNO (src)))
2919             {
2920               static char retval[] = "mf__\t%0,%q1";
2921               retval[2] = reg_names[REGNO (src)][4];
2922               retval[3] = reg_names[REGNO (src)][5];
2923               return retval;
2924             }
2925
2926           if (ST_REG_P (REGNO (src)) && ISA_HAS_8CC)
2927             return "lui\t%0,0x3f80\n\tmovf\t%0,%.,%1";
2928
2929           if (FP_REG_P (REGNO (src)))
2930             return (dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1");
2931
2932           if (ALL_COP_REG_P (REGNO (src)))
2933             {
2934               static char retval[] = "dmfc_\t%0,%1";
2935
2936               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
2937               return (dbl_p ? retval : retval + 1);
2938             }
2939         }
2940
2941       if (src_code == MEM)
2942         return (dbl_p ? "ld\t%0,%1" : "lw\t%0,%1");
2943
2944       if (src_code == CONST_INT)
2945         {
2946           /* Don't use the X format, because that will give out of
2947              range numbers for 64 bit hosts and 32 bit targets.  */
2948           if (!TARGET_MIPS16)
2949             return "li\t%0,%1\t\t\t# %X1";
2950
2951           if (INTVAL (src) >= 0 && INTVAL (src) <= 0xffff)
2952             return "li\t%0,%1";
2953
2954           if (INTVAL (src) < 0 && INTVAL (src) >= -0xffff)
2955             return "#";
2956         }
2957
2958       if (src_code == HIGH)
2959         return "lui\t%0,%h1";
2960
2961       if (CONST_GP_P (src))
2962         return "move\t%0,%1";
2963
2964       if (symbolic_operand (src, VOIDmode))
2965         return (dbl_p ? "dla\t%0,%1" : "la\t%0,%1");
2966     }
2967   if (src_code == REG && FP_REG_P (REGNO (src)))
2968     {
2969       if (dest_code == REG && FP_REG_P (REGNO (dest)))
2970         {
2971           if (GET_MODE (dest) == V2SFmode)
2972             return "mov.ps\t%0,%1";
2973           else
2974             return (dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1");
2975         }
2976
2977       if (dest_code == MEM)
2978         return (dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0");
2979     }
2980   if (dest_code == REG && FP_REG_P (REGNO (dest)))
2981     {
2982       if (src_code == MEM)
2983         return (dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1");
2984     }
2985   if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
2986     {
2987       static char retval[] = "l_c_\t%0,%1";
2988
2989       retval[1] = (dbl_p ? 'd' : 'w');
2990       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
2991       return retval;
2992     }
2993   if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
2994     {
2995       static char retval[] = "s_c_\t%1,%0";
2996
2997       retval[1] = (dbl_p ? 'd' : 'w');
2998       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
2999       return retval;
3000     }
3001   gcc_unreachable ();
3002 }
3003 \f
3004 /* Restore $gp from its save slot.  Valid only when using o32 or
3005    o64 abicalls.  */
3006
3007 void
3008 mips_restore_gp (void)
3009 {
3010   rtx address, slot;
3011
3012   gcc_assert (TARGET_ABICALLS && TARGET_OLDABI);
3013
3014   address = mips_add_offset (pic_offset_table_rtx,
3015                              frame_pointer_needed
3016                              ? hard_frame_pointer_rtx
3017                              : stack_pointer_rtx,
3018                              current_function_outgoing_args_size);
3019   slot = gen_rtx_MEM (Pmode, address);
3020
3021   emit_move_insn (pic_offset_table_rtx, slot);
3022   if (!TARGET_EXPLICIT_RELOCS)
3023     emit_insn (gen_blockage ());
3024 }
3025 \f
3026 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)).  */
3027
3028 static void
3029 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
3030 {
3031   emit_insn (gen_rtx_SET (VOIDmode, target,
3032                           gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
3033 }
3034
3035 /* Return true if CMP1 is a suitable second operand for relational
3036    operator CODE.  See also the *sCC patterns in mips.md.  */
3037
3038 static bool
3039 mips_relational_operand_ok_p (enum rtx_code code, rtx cmp1)
3040 {
3041   switch (code)
3042     {
3043     case GT:
3044     case GTU:
3045       return reg_or_0_operand (cmp1, VOIDmode);
3046
3047     case GE:
3048     case GEU:
3049       return !TARGET_MIPS16 && cmp1 == const1_rtx;
3050
3051     case LT:
3052     case LTU:
3053       return arith_operand (cmp1, VOIDmode);
3054
3055     case LE:
3056       return sle_operand (cmp1, VOIDmode);
3057
3058     case LEU:
3059       return sleu_operand (cmp1, VOIDmode);
3060
3061     default:
3062       gcc_unreachable ();
3063     }
3064 }
3065
3066 /* Canonicalize LE or LEU comparisons into LT comparisons when
3067    possible to avoid extra instructions or inverting the
3068    comparison.  */
3069
3070 static bool
3071 mips_canonicalize_comparison (enum rtx_code *code, rtx *cmp1, 
3072                               enum machine_mode mode)
3073 {
3074   HOST_WIDE_INT original, plus_one;
3075
3076   if (GET_CODE (*cmp1) != CONST_INT)
3077     return false;
3078   
3079   original = INTVAL (*cmp1);
3080   plus_one = trunc_int_for_mode ((unsigned HOST_WIDE_INT) original + 1, mode);
3081   
3082   switch (*code)
3083     {
3084     case LE:
3085       if (original < plus_one)
3086         {
3087           *code = LT;
3088           *cmp1 = force_reg (mode, GEN_INT (plus_one));
3089           return true;
3090         }
3091       break;
3092       
3093     case LEU:
3094       if (plus_one != 0)
3095         {
3096           *code = LTU;
3097           *cmp1 = force_reg (mode, GEN_INT (plus_one));
3098           return true;
3099         }
3100       break;
3101       
3102     default:
3103       return false;
3104    }
3105   
3106   return false;
3107
3108 }
3109
3110 /* Compare CMP0 and CMP1 using relational operator CODE and store the
3111    result in TARGET.  CMP0 and TARGET are register_operands that have
3112    the same integer mode.  If INVERT_PTR is nonnull, it's OK to set
3113    TARGET to the inverse of the result and flip *INVERT_PTR instead.  */
3114
3115 static void
3116 mips_emit_int_relational (enum rtx_code code, bool *invert_ptr,
3117                           rtx target, rtx cmp0, rtx cmp1)
3118 {
3119   /* First see if there is a MIPS instruction that can do this operation
3120      with CMP1 in its current form. If not, try to canonicalize the
3121      comparison to LT. If that fails, try doing the same for the
3122      inverse operation.  If that also fails, force CMP1 into a register
3123      and try again.  */
3124   if (mips_relational_operand_ok_p (code, cmp1))
3125     mips_emit_binary (code, target, cmp0, cmp1);
3126   else if (mips_canonicalize_comparison (&code, &cmp1, GET_MODE (target)))
3127     mips_emit_binary (code, target, cmp0, cmp1);
3128   else
3129     {
3130       enum rtx_code inv_code = reverse_condition (code);
3131       if (!mips_relational_operand_ok_p (inv_code, cmp1))
3132         {
3133           cmp1 = force_reg (GET_MODE (cmp0), cmp1);
3134           mips_emit_int_relational (code, invert_ptr, target, cmp0, cmp1);
3135         }
3136       else if (invert_ptr == 0)
3137         {
3138           rtx inv_target = gen_reg_rtx (GET_MODE (target));
3139           mips_emit_binary (inv_code, inv_target, cmp0, cmp1);
3140           mips_emit_binary (XOR, target, inv_target, const1_rtx);
3141         }
3142       else
3143         {
3144           *invert_ptr = !*invert_ptr;
3145           mips_emit_binary (inv_code, target, cmp0, cmp1);
3146         }
3147     }
3148 }
3149
3150 /* Return a register that is zero iff CMP0 and CMP1 are equal.
3151    The register will have the same mode as CMP0.  */
3152
3153 static rtx
3154 mips_zero_if_equal (rtx cmp0, rtx cmp1)
3155 {
3156   if (cmp1 == const0_rtx)
3157     return cmp0;
3158
3159   if (uns_arith_operand (cmp1, VOIDmode))
3160     return expand_binop (GET_MODE (cmp0), xor_optab,
3161                          cmp0, cmp1, 0, 0, OPTAB_DIRECT);
3162
3163   return expand_binop (GET_MODE (cmp0), sub_optab,
3164                        cmp0, cmp1, 0, 0, OPTAB_DIRECT);
3165 }
3166
3167 /* Convert *CODE into a code that can be used in a floating-point
3168    scc instruction (c.<cond>.<fmt>).  Return true if the values of
3169    the condition code registers will be inverted, with 0 indicating
3170    that the condition holds.  */
3171
3172 static bool
3173 mips_reverse_fp_cond_p (enum rtx_code *code)
3174 {
3175   switch (*code)
3176     {
3177     case NE:
3178     case LTGT:
3179     case ORDERED:
3180       *code = reverse_condition_maybe_unordered (*code);
3181       return true;
3182
3183     default:
3184       return false;
3185     }
3186 }
3187
3188 /* Convert a comparison into something that can be used in a branch or
3189    conditional move.  cmp_operands[0] and cmp_operands[1] are the values
3190    being compared and *CODE is the code used to compare them.
3191
3192    Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
3193    If NEED_EQ_NE_P, then only EQ/NE comparisons against zero are possible,
3194    otherwise any standard branch condition can be used.  The standard branch
3195    conditions are:
3196
3197       - EQ/NE between two registers.
3198       - any comparison between a register and zero.  */
3199
3200 static void
3201 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
3202 {
3203   if (GET_MODE_CLASS (GET_MODE (cmp_operands[0])) == MODE_INT)
3204     {
3205       if (!need_eq_ne_p && cmp_operands[1] == const0_rtx)
3206         {
3207           *op0 = cmp_operands[0];
3208           *op1 = cmp_operands[1];
3209         }
3210       else if (*code == EQ || *code == NE)
3211         {
3212           if (need_eq_ne_p)
3213             {
3214               *op0 = mips_zero_if_equal (cmp_operands[0], cmp_operands[1]);
3215               *op1 = const0_rtx;
3216             }
3217           else
3218             {
3219               *op0 = cmp_operands[0];
3220               *op1 = force_reg (GET_MODE (*op0), cmp_operands[1]);
3221             }
3222         }
3223       else
3224         {
3225           /* The comparison needs a separate scc instruction.  Store the
3226              result of the scc in *OP0 and compare it against zero.  */
3227           bool invert = false;
3228           *op0 = gen_reg_rtx (GET_MODE (cmp_operands[0]));
3229           *op1 = const0_rtx;
3230           mips_emit_int_relational (*code, &invert, *op0,
3231                                     cmp_operands[0], cmp_operands[1]);
3232           *code = (invert ? EQ : NE);
3233         }
3234     }
3235   else
3236     {
3237       enum rtx_code cmp_code;
3238
3239       /* Floating-point tests use a separate c.cond.fmt comparison to
3240          set a condition code register.  The branch or conditional move
3241          will then compare that register against zero.
3242
3243          Set CMP_CODE to the code of the comparison instruction and
3244          *CODE to the code that the branch or move should use.  */
3245       cmp_code = *code;
3246       *code = mips_reverse_fp_cond_p (&cmp_code) ? EQ : NE;
3247       *op0 = (ISA_HAS_8CC
3248               ? gen_reg_rtx (CCmode)
3249               : gen_rtx_REG (CCmode, FPSW_REGNUM));
3250       *op1 = const0_rtx;
3251       mips_emit_binary (cmp_code, *op0, cmp_operands[0], cmp_operands[1]);
3252     }
3253 }
3254 \f
3255 /* Try comparing cmp_operands[0] and cmp_operands[1] using rtl code CODE.
3256    Store the result in TARGET and return true if successful.
3257
3258    On 64-bit targets, TARGET may be wider than cmp_operands[0].  */
3259
3260 bool
3261 mips_emit_scc (enum rtx_code code, rtx target)
3262 {
3263   if (GET_MODE_CLASS (GET_MODE (cmp_operands[0])) != MODE_INT)
3264     return false;
3265
3266   target = gen_lowpart (GET_MODE (cmp_operands[0]), target);
3267   if (code == EQ || code == NE)
3268     {
3269       rtx zie = mips_zero_if_equal (cmp_operands[0], cmp_operands[1]);
3270       mips_emit_binary (code, target, zie, const0_rtx);
3271     }
3272   else
3273     mips_emit_int_relational (code, 0, target,
3274                               cmp_operands[0], cmp_operands[1]);
3275   return true;
3276 }
3277
3278 /* Emit the common code for doing conditional branches.
3279    operand[0] is the label to jump to.
3280    The comparison operands are saved away by cmp{si,di,sf,df}.  */
3281
3282 void
3283 gen_conditional_branch (rtx *operands, enum rtx_code code)
3284 {
3285   rtx op0, op1, condition;
3286
3287   mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
3288   condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
3289   emit_jump_insn (gen_condjump (condition, operands[0]));
3290 }
3291
3292 /* Implement:
3293
3294    (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
3295    (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS))  */
3296
3297 void
3298 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
3299                        enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
3300 {
3301   rtx cmp_result;
3302   bool reversed_p;
3303
3304   reversed_p = mips_reverse_fp_cond_p (&cond);
3305   cmp_result = gen_reg_rtx (CCV2mode);
3306   emit_insn (gen_scc_ps (cmp_result,
3307                          gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
3308   if (reversed_p)
3309     emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
3310                                          cmp_result));
3311   else
3312     emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
3313                                          cmp_result));
3314 }
3315
3316 /* Emit the common code for conditional moves.  OPERANDS is the array
3317    of operands passed to the conditional move define_expand.  */
3318
3319 void
3320 gen_conditional_move (rtx *operands)
3321 {
3322   enum rtx_code code;
3323   rtx op0, op1;
3324
3325   code = GET_CODE (operands[1]);
3326   mips_emit_compare (&code, &op0, &op1, true);
3327   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
3328                           gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
3329                                                 gen_rtx_fmt_ee (code,
3330                                                                 GET_MODE (op0),
3331                                                                 op0, op1),
3332                                                 operands[2], operands[3])));
3333 }
3334
3335 /* Emit a conditional trap.  OPERANDS is the array of operands passed to
3336    the conditional_trap expander.  */
3337
3338 void
3339 mips_gen_conditional_trap (rtx *operands)
3340 {
3341   rtx op0, op1;
3342   enum rtx_code cmp_code = GET_CODE (operands[0]);
3343   enum machine_mode mode = GET_MODE (cmp_operands[0]);
3344
3345   /* MIPS conditional trap machine instructions don't have GT or LE
3346      flavors, so we must invert the comparison and convert to LT and
3347      GE, respectively.  */
3348   switch (cmp_code)
3349     {
3350     case GT: cmp_code = LT; break;
3351     case LE: cmp_code = GE; break;
3352     case GTU: cmp_code = LTU; break;
3353     case LEU: cmp_code = GEU; break;
3354     default: break;
3355     }
3356   if (cmp_code == GET_CODE (operands[0]))
3357     {
3358       op0 = cmp_operands[0];
3359       op1 = cmp_operands[1];
3360     }
3361   else
3362     {
3363       op0 = cmp_operands[1];
3364       op1 = cmp_operands[0];
3365     }
3366   op0 = force_reg (mode, op0);
3367   if (!arith_operand (op1, mode))
3368     op1 = force_reg (mode, op1);
3369
3370   emit_insn (gen_rtx_TRAP_IF (VOIDmode,
3371                               gen_rtx_fmt_ee (cmp_code, mode, op0, op1),
3372                               operands[1]));
3373 }
3374 \f
3375 /* Load function address ADDR into register DEST.  SIBCALL_P is true
3376    if the address is needed for a sibling call.  */
3377
3378 static void
3379 mips_load_call_address (rtx dest, rtx addr, int sibcall_p)
3380 {
3381   /* If we're generating PIC, and this call is to a global function,
3382      try to allow its address to be resolved lazily.  This isn't
3383      possible for NewABI sibcalls since the value of $gp on entry
3384      to the stub would be our caller's gp, not ours.  */
3385   if (TARGET_EXPLICIT_RELOCS
3386       && !(sibcall_p && TARGET_NEWABI)
3387       && global_got_operand (addr, VOIDmode))
3388     {
3389       rtx high, lo_sum_symbol;
3390
3391       high = mips_unspec_offset_high (dest, pic_offset_table_rtx,
3392                                       addr, SYMBOL_GOTOFF_CALL);
3393       lo_sum_symbol = mips_unspec_address (addr, SYMBOL_GOTOFF_CALL);
3394       if (Pmode == SImode)
3395         emit_insn (gen_load_callsi (dest, high, lo_sum_symbol));
3396       else
3397         emit_insn (gen_load_calldi (dest, high, lo_sum_symbol));
3398     }
3399   else
3400     emit_move_insn (dest, addr);
3401 }
3402
3403
3404 /* Expand a call or call_value instruction.  RESULT is where the
3405    result will go (null for calls), ADDR is the address of the
3406    function, ARGS_SIZE is the size of the arguments and AUX is
3407    the value passed to us by mips_function_arg.  SIBCALL_P is true
3408    if we are expanding a sibling call, false if we're expanding
3409    a normal call.  */
3410
3411 void
3412 mips_expand_call (rtx result, rtx addr, rtx args_size, rtx aux, int sibcall_p)
3413 {
3414   rtx orig_addr, pattern, insn;
3415
3416   orig_addr = addr;
3417   if (!call_insn_operand (addr, VOIDmode))
3418     {
3419       addr = gen_reg_rtx (Pmode);
3420       mips_load_call_address (addr, orig_addr, sibcall_p);
3421     }
3422
3423   if (TARGET_MIPS16
3424       && mips16_hard_float
3425       && build_mips16_call_stub (result, addr, args_size,
3426                                  aux == 0 ? 0 : (int) GET_MODE (aux)))
3427     return;
3428
3429   if (result == 0)
3430     pattern = (sibcall_p
3431                ? gen_sibcall_internal (addr, args_size)
3432                : gen_call_internal (addr, args_size));
3433   else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
3434     {
3435       rtx reg1, reg2;
3436
3437       reg1 = XEXP (XVECEXP (result, 0, 0), 0);
3438       reg2 = XEXP (XVECEXP (result, 0, 1), 0);
3439       pattern =
3440         (sibcall_p
3441          ? gen_sibcall_value_multiple_internal (reg1, addr, args_size, reg2)
3442          : gen_call_value_multiple_internal (reg1, addr, args_size, reg2));
3443     }
3444   else
3445     pattern = (sibcall_p
3446                ? gen_sibcall_value_internal (result, addr, args_size)
3447                : gen_call_value_internal (result, addr, args_size));
3448
3449   insn = emit_call_insn (pattern);
3450
3451   /* Lazy-binding stubs require $gp to be valid on entry.  */
3452   if (global_got_operand (orig_addr, VOIDmode))
3453     use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
3454 }
3455
3456
3457 /* We can handle any sibcall when TARGET_SIBCALLS is true.  */
3458
3459 static bool
3460 mips_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
3461                               tree exp ATTRIBUTE_UNUSED)
3462 {
3463   return TARGET_SIBCALLS;
3464 }
3465 \f
3466 /* Emit code to move general operand SRC into condition-code
3467    register DEST.  SCRATCH is a scratch TFmode float register.
3468    The sequence is:
3469
3470         FP1 = SRC
3471         FP2 = 0.0f
3472         DEST = FP2 < FP1
3473
3474    where FP1 and FP2 are single-precision float registers
3475    taken from SCRATCH.  */
3476
3477 void
3478 mips_emit_fcc_reload (rtx dest, rtx src, rtx scratch)
3479 {
3480   rtx fp1, fp2;
3481
3482   /* Change the source to SFmode.  */
3483   if (MEM_P (src))
3484     src = adjust_address (src, SFmode, 0);
3485   else if (REG_P (src) || GET_CODE (src) == SUBREG)
3486     src = gen_rtx_REG (SFmode, true_regnum (src));
3487
3488   fp1 = gen_rtx_REG (SFmode, REGNO (scratch));
3489   fp2 = gen_rtx_REG (SFmode, REGNO (scratch) + FP_INC);
3490
3491   emit_move_insn (copy_rtx (fp1), src);
3492   emit_move_insn (copy_rtx (fp2), CONST0_RTX (SFmode));
3493   emit_insn (gen_slt_sf (dest, fp2, fp1));
3494 }
3495 \f
3496 /* Emit code to change the current function's return address to
3497    ADDRESS.  SCRATCH is available as a scratch register, if needed.
3498    ADDRESS and SCRATCH are both word-mode GPRs.  */
3499
3500 void
3501 mips_set_return_address (rtx address, rtx scratch)
3502 {
3503   rtx slot_address;
3504
3505   compute_frame_size (get_frame_size ());
3506   gcc_assert ((cfun->machine->frame.mask >> 31) & 1);
3507   slot_address = mips_add_offset (scratch, stack_pointer_rtx,
3508                                   cfun->machine->frame.gp_sp_offset);
3509
3510   emit_move_insn (gen_rtx_MEM (GET_MODE (address), slot_address), address);
3511 }
3512 \f
3513 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
3514    Assume that the areas do not overlap.  */
3515
3516 static void
3517 mips_block_move_straight (rtx dest, rtx src, HOST_WIDE_INT length)
3518 {
3519   HOST_WIDE_INT offset, delta;
3520   unsigned HOST_WIDE_INT bits;
3521   int i;
3522   enum machine_mode mode;
3523   rtx *regs;
3524
3525   /* Work out how many bits to move at a time.  If both operands have
3526      half-word alignment, it is usually better to move in half words.
3527      For instance, lh/lh/sh/sh is usually better than lwl/lwr/swl/swr
3528      and lw/lw/sw/sw is usually better than ldl/ldr/sdl/sdr.
3529      Otherwise move word-sized chunks.  */
3530   if (MEM_ALIGN (src) == BITS_PER_WORD / 2
3531       && MEM_ALIGN (dest) == BITS_PER_WORD / 2)
3532     bits = BITS_PER_WORD / 2;
3533   else
3534     bits = BITS_PER_WORD;
3535
3536   mode = mode_for_size (bits, MODE_INT, 0);
3537   delta = bits / BITS_PER_UNIT;
3538
3539   /* Allocate a buffer for the temporary registers.  */
3540   regs = alloca (sizeof (rtx) * length / delta);
3541
3542   /* Load as many BITS-sized chunks as possible.  Use a normal load if
3543      the source has enough alignment, otherwise use left/right pairs.  */
3544   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
3545     {
3546       regs[i] = gen_reg_rtx (mode);
3547       if (MEM_ALIGN (src) >= bits)
3548         emit_move_insn (regs[i], adjust_address (src, mode, offset));
3549       else
3550         {
3551           rtx part = adjust_address (src, BLKmode, offset);
3552           if (!mips_expand_unaligned_load (regs[i], part, bits, 0))
3553             gcc_unreachable ();
3554         }
3555     }
3556
3557   /* Copy the chunks to the destination.  */
3558   for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
3559     if (MEM_ALIGN (dest) >= bits)
3560       emit_move_insn (adjust_address (dest, mode, offset), regs[i]);
3561     else
3562       {
3563         rtx part = adjust_address (dest, BLKmode, offset);
3564         if (!mips_expand_unaligned_store (part, regs[i], bits, 0))
3565           gcc_unreachable ();
3566       }
3567
3568   /* Mop up any left-over bytes.  */
3569   if (offset < length)
3570     {
3571       src = adjust_address (src, BLKmode, offset);
3572       dest = adjust_address (dest, BLKmode, offset);
3573       move_by_pieces (dest, src, length - offset,
3574                       MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), 0);
3575     }
3576 }
3577 \f
3578 #define MAX_MOVE_REGS 4
3579 #define MAX_MOVE_BYTES (MAX_MOVE_REGS * UNITS_PER_WORD)
3580
3581
3582 /* Helper function for doing a loop-based block operation on memory
3583    reference MEM.  Each iteration of the loop will operate on LENGTH
3584    bytes of MEM.
3585
3586    Create a new base register for use within the loop and point it to
3587    the start of MEM.  Create a new memory reference that uses this
3588    register.  Store them in *LOOP_REG and *LOOP_MEM respectively.  */
3589
3590 static void
3591 mips_adjust_block_mem (rtx mem, HOST_WIDE_INT length,
3592                        rtx *loop_reg, rtx *loop_mem)
3593 {
3594   *loop_reg = copy_addr_to_reg (XEXP (mem, 0));
3595
3596   /* Although the new mem does not refer to a known location,
3597      it does keep up to LENGTH bytes of alignment.  */
3598   *loop_mem = change_address (mem, BLKmode, *loop_reg);
3599   set_mem_align (*loop_mem, MIN (MEM_ALIGN (mem), length * BITS_PER_UNIT));
3600 }
3601
3602
3603 /* Move LENGTH bytes from SRC to DEST using a loop that moves MAX_MOVE_BYTES
3604    per iteration.  LENGTH must be at least MAX_MOVE_BYTES.  Assume that the
3605    memory regions do not overlap.  */
3606
3607 static void
3608 mips_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT length)
3609 {
3610   rtx label, src_reg, dest_reg, final_src;
3611   HOST_WIDE_INT leftover;
3612
3613   leftover = length % MAX_MOVE_BYTES;
3614   length -= leftover;
3615
3616   /* Create registers and memory references for use within the loop.  */
3617   mips_adjust_block_mem (src, MAX_MOVE_BYTES, &src_reg, &src);
3618   mips_adjust_block_mem (dest, MAX_MOVE_BYTES, &dest_reg, &dest);
3619
3620   /* Calculate the value that SRC_REG should have after the last iteration
3621      of the loop.  */
3622   final_src = expand_simple_binop (Pmode, PLUS, src_reg, GEN_INT (length),
3623                                    0, 0, OPTAB_WIDEN);
3624
3625   /* Emit the start of the loop.  */
3626   label = gen_label_rtx ();
3627   emit_label (label);
3628
3629   /* Emit the loop body.  */
3630   mips_block_move_straight (dest, src, MAX_MOVE_BYTES);
3631
3632   /* Move on to the next block.  */
3633   emit_move_insn (src_reg, plus_constant (src_reg, MAX_MOVE_BYTES));
3634   emit_move_insn (dest_reg, plus_constant (dest_reg, MAX_MOVE_BYTES));
3635
3636   /* Emit the loop condition.  */
3637   if (Pmode == DImode)
3638     emit_insn (gen_cmpdi (src_reg, final_src));
3639   else
3640     emit_insn (gen_cmpsi (src_reg, final_src));
3641   emit_jump_insn (gen_bne (label));
3642
3643   /* Mop up any left-over bytes.  */
3644   if (leftover)
3645     mips_block_move_straight (dest, src, leftover);
3646 }
3647 \f
3648 /* Expand a movmemsi instruction.  */
3649
3650 bool
3651 mips_expand_block_move (rtx dest, rtx src, rtx length)
3652 {
3653   if (GET_CODE (length) == CONST_INT)
3654     {
3655       if (INTVAL (length) <= 2 * MAX_MOVE_BYTES)
3656         {
3657           mips_block_move_straight (dest, src, INTVAL (length));
3658           return true;
3659         }
3660       else if (optimize)
3661         {
3662           mips_block_move_loop (dest, src, INTVAL (length));
3663           return true;
3664         }
3665     }
3666   return false;
3667 }
3668 \f
3669 /* Argument support functions.  */
3670
3671 /* Initialize CUMULATIVE_ARGS for a function.  */
3672
3673 void
3674 init_cumulative_args (CUMULATIVE_ARGS *cum, tree fntype,
3675                       rtx libname ATTRIBUTE_UNUSED)
3676 {
3677   static CUMULATIVE_ARGS zero_cum;
3678   tree param, next_param;
3679
3680   *cum = zero_cum;
3681   cum->prototype = (fntype && TYPE_ARG_TYPES (fntype));
3682
3683   /* Determine if this function has variable arguments.  This is
3684      indicated by the last argument being 'void_type_mode' if there
3685      are no variable arguments.  The standard MIPS calling sequence
3686      passes all arguments in the general purpose registers in this case.  */
3687
3688   for (param = fntype ? TYPE_ARG_TYPES (fntype) : 0;
3689        param != 0; param = next_param)
3690     {
3691       next_param = TREE_CHAIN (param);
3692       if (next_param == 0 && TREE_VALUE (param) != void_type_node)
3693         cum->gp_reg_found = 1;
3694     }
3695 }
3696
3697
3698 /* Fill INFO with information about a single argument.  CUM is the
3699    cumulative state for earlier arguments.  MODE is the mode of this
3700    argument and TYPE is its type (if known).  NAMED is true if this
3701    is a named (fixed) argument rather than a variable one.  */
3702
3703 static void
3704 mips_arg_info (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
3705                tree type, int named, struct mips_arg_info *info)
3706 {
3707   bool doubleword_aligned_p;
3708   unsigned int num_bytes, num_words, max_regs;
3709
3710   /* Work out the size of the argument.  */
3711   num_bytes = type ? int_size_in_bytes (type) : GET_MODE_SIZE (mode);
3712   num_words = (num_bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3713
3714   /* Decide whether it should go in a floating-point register, assuming
3715      one is free.  Later code checks for availability.
3716
3717      The checks against UNITS_PER_FPVALUE handle the soft-float and
3718      single-float cases.  */
3719   switch (mips_abi)
3720     {
3721     case ABI_EABI:
3722       /* The EABI conventions have traditionally been defined in terms
3723          of TYPE_MODE, regardless of the actual type.  */
3724       info->fpr_p = ((GET_MODE_CLASS (mode) == MODE_FLOAT
3725                       || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3726                      && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
3727       break;
3728
3729     case ABI_32:
3730     case ABI_O64:
3731       /* Only leading floating-point scalars are passed in
3732          floating-point registers.  We also handle vector floats the same
3733          say, which is OK because they are not covered by the standard ABI.  */
3734       info->fpr_p = (!cum->gp_reg_found
3735                      && cum->arg_number < 2
3736                      && (type == 0 || SCALAR_FLOAT_TYPE_P (type)
3737                          || VECTOR_FLOAT_TYPE_P (type))
3738                      && (GET_MODE_CLASS (mode) == MODE_FLOAT
3739                          || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3740                      && GET_MODE_SIZE (mode) <= UNITS_PER_FPVALUE);
3741       break;
3742
3743     case ABI_N32:
3744     case ABI_64:
3745       /* Scalar and complex floating-point types are passed in
3746          floating-point registers.  */
3747       info->fpr_p = (named
3748                      && (type == 0 || FLOAT_TYPE_P (type))
3749                      && (GET_MODE_CLASS (mode) == MODE_FLOAT
3750                          || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3751                          || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
3752                      && GET_MODE_UNIT_SIZE (mode) <= UNITS_PER_FPVALUE);
3753
3754       /* ??? According to the ABI documentation, the real and imaginary
3755          parts of complex floats should be passed in individual registers.
3756          The real and imaginary parts of stack arguments are supposed
3757          to be contiguous and there should be an extra word of padding
3758          at the end.
3759
3760          This has two problems.  First, it makes it impossible to use a
3761          single "void *" va_list type, since register and stack arguments
3762          are passed differently.  (At the time of writing, MIPSpro cannot
3763          handle complex float varargs correctly.)  Second, it's unclear
3764          what should happen when there is only one register free.
3765
3766          For now, we assume that named complex floats should go into FPRs
3767          if there are two FPRs free, otherwise they should be passed in the
3768          same way as a struct containing two floats.  */
3769       if (info->fpr_p
3770           && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3771           && GET_MODE_UNIT_SIZE (mode) < UNITS_PER_FPVALUE)
3772         {
3773           if (cum->num_gprs >= MAX_ARGS_IN_REGISTERS - 1)
3774             info->fpr_p = false;
3775           else
3776             num_words = 2;
3777         }
3778       break;
3779
3780     default:
3781       gcc_unreachable ();
3782     }
3783
3784   /* See whether the argument has doubleword alignment.  */
3785   doubleword_aligned_p = FUNCTION_ARG_BOUNDARY (mode, type) > BITS_PER_WORD;
3786
3787   /* Set REG_OFFSET to the register count we're interested in.
3788      The EABI allocates the floating-point registers separately,
3789      but the other ABIs allocate them like integer registers.  */
3790   info->reg_offset = (mips_abi == ABI_EABI && info->fpr_p
3791                       ? cum->num_fprs
3792                       : cum->num_gprs);
3793
3794   /* Advance to an even register if the argument is doubleword-aligned.  */
3795   if (doubleword_aligned_p)
3796     info->reg_offset += info->reg_offset & 1;
3797
3798   /* Work out the offset of a stack argument.  */
3799   info->stack_offset = cum->stack_words;
3800   if (doubleword_aligned_p)
3801     info->stack_offset += info->stack_offset & 1;
3802
3803   max_regs = MAX_ARGS_IN_REGISTERS - info->reg_offset;
3804
3805   /* Partition the argument between registers and stack.  */
3806   info->reg_words = MIN (num_words, max_regs);
3807   info->stack_words = num_words - info->reg_words;
3808 }
3809
3810
3811 /* Implement FUNCTION_ARG_ADVANCE.  */
3812
3813 void
3814 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3815                       tree type, int named)
3816 {
3817   struct mips_arg_info info;
3818
3819   mips_arg_info (cum, mode, type, named, &info);
3820
3821   if (!info.fpr_p)
3822     cum->gp_reg_found = true;
3823
3824   /* See the comment above the cumulative args structure in mips.h
3825      for an explanation of what this code does.  It assumes the O32
3826      ABI, which passes at most 2 arguments in float registers.  */
3827   if (cum->arg_number < 2 && info.fpr_p)
3828     cum->fp_code += (mode == SFmode ? 1 : 2) << ((cum->arg_number - 1) * 2);
3829
3830   if (mips_abi != ABI_EABI || !info.fpr_p)
3831     cum->num_gprs = info.reg_offset + info.reg_words;
3832   else if (info.reg_words > 0)
3833     cum->num_fprs += FP_INC;
3834
3835   if (info.stack_words > 0)
3836     cum->stack_words = info.stack_offset + info.stack_words;
3837
3838   cum->arg_number++;
3839 }
3840
3841 /* Implement FUNCTION_ARG.  */
3842
3843 struct rtx_def *
3844 function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
3845               tree type, int named)
3846 {
3847   struct mips_arg_info info;
3848
3849   /* We will be called with a mode of VOIDmode after the last argument
3850      has been seen.  Whatever we return will be passed to the call
3851      insn.  If we need a mips16 fp_code, return a REG with the code
3852      stored as the mode.  */
3853   if (mode == VOIDmode)
3854     {
3855       if (TARGET_MIPS16 && cum->fp_code != 0)
3856         return gen_rtx_REG ((enum machine_mode) cum->fp_code, 0);
3857
3858       else
3859         return 0;
3860     }
3861
3862   mips_arg_info (cum, mode, type, named, &info);
3863
3864   /* Return straight away if the whole argument is passed on the stack.  */
3865   if (info.reg_offset == MAX_ARGS_IN_REGISTERS)
3866     return 0;
3867
3868   if (type != 0
3869       && TREE_CODE (type) == RECORD_TYPE
3870       && TARGET_NEWABI
3871       && TYPE_SIZE_UNIT (type)
3872       && host_integerp (TYPE_SIZE_UNIT (type), 1)
3873       && named)
3874     {
3875       /* The Irix 6 n32/n64 ABIs say that if any 64 bit chunk of the
3876          structure contains a double in its entirety, then that 64 bit
3877          chunk is passed in a floating point register.  */
3878       tree field;
3879
3880       /* First check to see if there is any such field.  */
3881       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3882         if (TREE_CODE (field) == FIELD_DECL
3883             && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3884             && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD
3885             && host_integerp (bit_position (field), 0)
3886             && int_bit_position (field) % BITS_PER_WORD == 0)
3887           break;
3888
3889       if (field != 0)
3890         {
3891           /* Now handle the special case by returning a PARALLEL
3892              indicating where each 64 bit chunk goes.  INFO.REG_WORDS
3893              chunks are passed in registers.  */
3894           unsigned int i;
3895           HOST_WIDE_INT bitpos;
3896           rtx ret;
3897
3898           /* assign_parms checks the mode of ENTRY_PARM, so we must
3899              use the actual mode here.  */
3900           ret = gen_rtx_PARALLEL (mode, rtvec_alloc (info.reg_words));
3901
3902           bitpos = 0;
3903           field = TYPE_FIELDS (type);
3904           for (i = 0; i < info.reg_words; i++)
3905             {
3906               rtx reg;
3907
3908               for (; field; field = TREE_CHAIN (field))
3909                 if (TREE_CODE (field) == FIELD_DECL
3910                     && int_bit_position (field) >= bitpos)
3911                   break;
3912
3913               if (field
3914                   && int_bit_position (field) == bitpos
3915                   && TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
3916                   && !TARGET_SOFT_FLOAT
3917                   && TYPE_PRECISION (TREE_TYPE (field)) == BITS_PER_WORD)
3918                 reg = gen_rtx_REG (DFmode, FP_ARG_FIRST + info.reg_offset + i);
3919               else
3920                 reg = gen_rtx_REG (DImode, GP_ARG_FIRST + info.reg_offset + i);
3921
3922               XVECEXP (ret, 0, i)
3923                 = gen_rtx_EXPR_LIST (VOIDmode, reg,
3924                                      GEN_INT (bitpos / BITS_PER_UNIT));
3925
3926               bitpos += BITS_PER_WORD;
3927             }
3928           return ret;
3929         }
3930     }
3931
3932   /* Handle the n32/n64 conventions for passing complex floating-point
3933      arguments in FPR pairs.  The real part goes in the lower register
3934      and the imaginary part goes in the upper register.  */
3935   if (TARGET_NEWABI
3936       && info.fpr_p
3937       && GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
3938     {
3939       rtx real, imag;
3940       enum machine_mode inner;
3941       int reg;
3942
3943       inner = GET_MODE_INNER (mode);
3944       reg = FP_ARG_FIRST + info.reg_offset;
3945       if (info.reg_words * UNITS_PER_WORD == GET_MODE_SIZE (inner))
3946         {
3947           /* Real part in registers, imaginary part on stack.  */
3948           gcc_assert (info.stack_words == info.reg_words);
3949           return gen_rtx_REG (inner, reg);
3950         }
3951       else
3952         {
3953           gcc_assert (info.stack_words == 0);
3954           real = gen_rtx_EXPR_LIST (VOIDmode,
3955                                     gen_rtx_REG (inner, reg),
3956                                     const0_rtx);
3957           imag = gen_rtx_EXPR_LIST (VOIDmode,
3958                                     gen_rtx_REG (inner,
3959                                                  reg + info.reg_words / 2),
3960                                     GEN_INT (GET_MODE_SIZE (inner)));
3961           return gen_rtx_PARALLEL (mode, gen_rtvec (2, real, imag));
3962         }
3963     }
3964
3965   if (!info.fpr_p)
3966     return gen_rtx_REG (mode, GP_ARG_FIRST + info.reg_offset);
3967   else if (info.reg_offset == 1)
3968     /* This code handles the special o32 case in which the second word
3969        of the argument structure is passed in floating-point registers.  */
3970     return gen_rtx_REG (mode, FP_ARG_FIRST + FP_INC);
3971   else
3972     return gen_rtx_REG (mode, FP_ARG_FIRST + info.reg_offset);
3973 }
3974
3975
3976 /* Implement TARGET_ARG_PARTIAL_BYTES.  */
3977
3978 static int
3979 mips_arg_partial_bytes (CUMULATIVE_ARGS *cum,
3980                         enum machine_mode mode, tree type, bool named)
3981 {
3982   struct mips_arg_info info;
3983
3984   mips_arg_info (cum, mode, type, named, &info);
3985   return info.stack_words > 0 ? info.reg_words * UNITS_PER_WORD : 0;
3986 }
3987
3988
3989 /* Implement FUNCTION_ARG_BOUNDARY.  Every parameter gets at least
3990    PARM_BOUNDARY bits of alignment, but will be given anything up
3991    to STACK_BOUNDARY bits if the type requires it.  */
3992
3993 int
3994 function_arg_boundary (enum machine_mode mode, tree type)
3995 {
3996   unsigned int alignment;
3997
3998   alignment = type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode);
3999   if (alignment < PARM_BOUNDARY)
4000     alignment = PARM_BOUNDARY;
4001   if (alignment > STACK_BOUNDARY)
4002     alignment = STACK_BOUNDARY;
4003   return alignment;
4004 }
4005
4006 /* Return true if FUNCTION_ARG_PADDING (MODE, TYPE) should return
4007    upward rather than downward.  In other words, return true if the
4008    first byte of the stack slot has useful data, false if the last
4009    byte does.  */
4010
4011 bool
4012 mips_pad_arg_upward (enum machine_mode mode, tree type)
4013 {
4014   /* On little-endian targets, the first byte of every stack argument
4015      is passed in the first byte of the stack slot.  */
4016   if (!BYTES_BIG_ENDIAN)
4017     return true;
4018
4019   /* Otherwise, integral types are padded downward: the last byte of a
4020      stack argument is passed in the last byte of the stack slot.  */
4021   if (type != 0
4022       ? INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)
4023       : GET_MODE_CLASS (mode) == MODE_INT)
4024     return false;
4025
4026   /* Big-endian o64 pads floating-point arguments downward.  */
4027   if (mips_abi == ABI_O64)
4028     if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
4029       return false;
4030
4031   /* Other types are padded upward for o32, o64, n32 and n64.  */
4032   if (mips_abi != ABI_EABI)
4033     return true;
4034
4035   /* Arguments smaller than a stack slot are padded downward.  */
4036   if (mode != BLKmode)
4037     return (GET_MODE_BITSIZE (mode) >= PARM_BOUNDARY);
4038   else
4039     return (int_size_in_bytes (type) >= (PARM_BOUNDARY / BITS_PER_UNIT));
4040 }
4041
4042
4043 /* Likewise BLOCK_REG_PADDING (MODE, TYPE, ...).  Return !BYTES_BIG_ENDIAN
4044    if the least significant byte of the register has useful data.  Return
4045    the opposite if the most significant byte does.  */
4046
4047 bool
4048 mips_pad_reg_upward (enum machine_mode mode, tree type)
4049 {
4050   /* No shifting is required for floating-point arguments.  */
4051   if (type != 0 ? FLOAT_TYPE_P (type) : GET_MODE_CLASS (mode) == MODE_FLOAT)
4052     return !BYTES_BIG_ENDIAN;
4053
4054   /* Otherwise, apply the same padding to register arguments as we do
4055      to stack arguments.  */
4056   return mips_pad_arg_upward (mode, type);
4057 }
4058 \f
4059 static void
4060 mips_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,<