OSDN Git Service

* config/mips/mips.c (mips_rtx_cost_optimize_size): New table of
[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_optimize_size =
800   {
801       COSTS_N_INSNS (1),            /* fp_add */
802       COSTS_N_INSNS (1),            /* fp_mult_sf */
803       COSTS_N_INSNS (1),            /* fp_mult_df */
804       COSTS_N_INSNS (1),            /* fp_div_sf */
805       COSTS_N_INSNS (1),            /* fp_div_df */
806       COSTS_N_INSNS (1),            /* int_mult_si */
807       COSTS_N_INSNS (1),            /* int_mult_di */
808       COSTS_N_INSNS (1),            /* int_div_si */
809       COSTS_N_INSNS (1),            /* int_div_di */
810                        2,           /* branch_cost */
811                        4            /* memory_latency */
812   };
813
814 static struct mips_rtx_cost_data const mips_rtx_cost_data[PROCESSOR_MAX] =
815   {
816     { /* R3000 */
817       COSTS_N_INSNS (2),            /* fp_add */
818       COSTS_N_INSNS (4),            /* fp_mult_sf */
819       COSTS_N_INSNS (5),            /* fp_mult_df */
820       COSTS_N_INSNS (12),           /* fp_div_sf */
821       COSTS_N_INSNS (19),           /* fp_div_df */
822       COSTS_N_INSNS (12),           /* int_mult_si */
823       COSTS_N_INSNS (12),           /* int_mult_di */
824       COSTS_N_INSNS (35),           /* int_div_si */
825       COSTS_N_INSNS (35),           /* int_div_di */
826                        1,           /* branch_cost */
827                        4            /* memory_latency */
828
829     },
830     { /* 4KC */
831       SOFT_FP_COSTS,
832       COSTS_N_INSNS (6),            /* int_mult_si */
833       COSTS_N_INSNS (6),            /* int_mult_di */
834       COSTS_N_INSNS (36),           /* int_div_si */
835       COSTS_N_INSNS (36),           /* int_div_di */
836                        1,           /* branch_cost */
837                        4            /* memory_latency */
838     },
839     { /* 4KP */
840       SOFT_FP_COSTS,
841       COSTS_N_INSNS (36),           /* int_mult_si */
842       COSTS_N_INSNS (36),           /* int_mult_di */
843       COSTS_N_INSNS (37),           /* int_div_si */
844       COSTS_N_INSNS (37),           /* int_div_di */
845                        1,           /* branch_cost */
846                        4            /* memory_latency */
847     },
848     { /* 5KC */
849       SOFT_FP_COSTS,
850       COSTS_N_INSNS (4),            /* int_mult_si */
851       COSTS_N_INSNS (11),           /* int_mult_di */
852       COSTS_N_INSNS (36),           /* int_div_si */
853       COSTS_N_INSNS (68),           /* int_div_di */
854                        1,           /* branch_cost */
855                        4            /* memory_latency */
856     },
857     { /* 5KF */
858       COSTS_N_INSNS (4),            /* fp_add */
859       COSTS_N_INSNS (4),            /* fp_mult_sf */
860       COSTS_N_INSNS (5),            /* fp_mult_df */
861       COSTS_N_INSNS (17),           /* fp_div_sf */
862       COSTS_N_INSNS (32),           /* fp_div_df */
863       COSTS_N_INSNS (4),            /* int_mult_si */
864       COSTS_N_INSNS (11),           /* int_mult_di */
865       COSTS_N_INSNS (36),           /* int_div_si */
866       COSTS_N_INSNS (68),           /* int_div_di */
867                        1,           /* branch_cost */
868                        4            /* memory_latency */
869     },
870     { /* 20KC */
871       DEFAULT_COSTS
872     },
873     { /* 24KC */
874       SOFT_FP_COSTS,
875       COSTS_N_INSNS (5),            /* int_mult_si */
876       COSTS_N_INSNS (5),            /* int_mult_di */
877       COSTS_N_INSNS (41),           /* int_div_si */
878       COSTS_N_INSNS (41),           /* int_div_di */
879                        1,           /* branch_cost */
880                        4            /* memory_latency */
881     },
882     { /* 24KF */
883       COSTS_N_INSNS (8),            /* fp_add */
884       COSTS_N_INSNS (8),            /* fp_mult_sf */
885       COSTS_N_INSNS (10),           /* fp_mult_df */
886       COSTS_N_INSNS (34),           /* fp_div_sf */
887       COSTS_N_INSNS (64),           /* fp_div_df */
888       COSTS_N_INSNS (5),            /* int_mult_si */
889       COSTS_N_INSNS (5),            /* int_mult_di */
890       COSTS_N_INSNS (41),           /* int_div_si */
891       COSTS_N_INSNS (41),           /* int_div_di */
892                        1,           /* branch_cost */
893                        4            /* memory_latency */
894     },
895     { /* 24KX */
896       COSTS_N_INSNS (4),            /* fp_add */
897       COSTS_N_INSNS (4),            /* fp_mult_sf */
898       COSTS_N_INSNS (5),            /* fp_mult_df */
899       COSTS_N_INSNS (17),           /* fp_div_sf */
900       COSTS_N_INSNS (32),           /* fp_div_df */
901       COSTS_N_INSNS (5),            /* int_mult_si */
902       COSTS_N_INSNS (5),            /* int_mult_di */
903       COSTS_N_INSNS (41),           /* int_div_si */
904       COSTS_N_INSNS (41),           /* int_div_di */
905                        1,           /* branch_cost */
906                        4            /* memory_latency */
907     },
908     { /* M4k */
909       DEFAULT_COSTS
910     },
911     { /* R3900 */
912       COSTS_N_INSNS (2),            /* fp_add */
913       COSTS_N_INSNS (4),            /* fp_mult_sf */
914       COSTS_N_INSNS (5),            /* fp_mult_df */
915       COSTS_N_INSNS (12),           /* fp_div_sf */
916       COSTS_N_INSNS (19),           /* fp_div_df */
917       COSTS_N_INSNS (2),            /* int_mult_si */
918       COSTS_N_INSNS (2),            /* int_mult_di */
919       COSTS_N_INSNS (35),           /* int_div_si */
920       COSTS_N_INSNS (35),           /* int_div_di */
921                        1,           /* branch_cost */
922                        4            /* memory_latency */
923     },
924     { /* R6000 */
925       COSTS_N_INSNS (3),            /* fp_add */
926       COSTS_N_INSNS (5),            /* fp_mult_sf */
927       COSTS_N_INSNS (6),            /* fp_mult_df */
928       COSTS_N_INSNS (15),           /* fp_div_sf */
929       COSTS_N_INSNS (16),           /* fp_div_df */
930       COSTS_N_INSNS (17),           /* int_mult_si */
931       COSTS_N_INSNS (17),           /* int_mult_di */
932       COSTS_N_INSNS (38),           /* int_div_si */
933       COSTS_N_INSNS (38),           /* int_div_di */
934                        2,           /* branch_cost */
935                        6            /* memory_latency */
936     },
937     { /* R4000 */
938        COSTS_N_INSNS (6),           /* fp_add */
939        COSTS_N_INSNS (7),           /* fp_mult_sf */
940        COSTS_N_INSNS (8),           /* fp_mult_df */
941        COSTS_N_INSNS (23),          /* fp_div_sf */
942        COSTS_N_INSNS (36),          /* fp_div_df */
943        COSTS_N_INSNS (10),          /* int_mult_si */
944        COSTS_N_INSNS (10),          /* int_mult_di */
945        COSTS_N_INSNS (69),          /* int_div_si */
946        COSTS_N_INSNS (69),          /* int_div_di */
947                         2,          /* branch_cost */
948                         6           /* memory_latency */
949     },
950     { /* R4100 */
951       DEFAULT_COSTS
952     },
953     { /* R4111 */
954       DEFAULT_COSTS
955     },
956     { /* R4120 */
957       DEFAULT_COSTS
958     },
959     { /* R4130 */
960       /* The only costs that appear to be updated here are
961          integer multiplication.  */
962       SOFT_FP_COSTS,
963       COSTS_N_INSNS (4),            /* int_mult_si */
964       COSTS_N_INSNS (6),            /* int_mult_di */
965       COSTS_N_INSNS (69),           /* int_div_si */
966       COSTS_N_INSNS (69),           /* int_div_di */
967                        1,           /* branch_cost */
968                        4            /* memory_latency */
969     },
970     { /* R4300 */
971       DEFAULT_COSTS
972     },
973     { /* R4600 */
974       DEFAULT_COSTS
975     },
976     { /* R4650 */
977       DEFAULT_COSTS
978     },
979     { /* R5000 */
980       COSTS_N_INSNS (6),            /* fp_add */
981       COSTS_N_INSNS (4),            /* fp_mult_sf */
982       COSTS_N_INSNS (5),            /* fp_mult_df */
983       COSTS_N_INSNS (23),           /* fp_div_sf */
984       COSTS_N_INSNS (36),           /* fp_div_df */
985       COSTS_N_INSNS (5),            /* int_mult_si */
986       COSTS_N_INSNS (5),            /* int_mult_di */
987       COSTS_N_INSNS (36),           /* int_div_si */
988       COSTS_N_INSNS (36),           /* int_div_di */
989                        1,           /* branch_cost */
990                        4            /* memory_latency */
991     },
992     { /* R5400 */
993       COSTS_N_INSNS (6),            /* fp_add */
994       COSTS_N_INSNS (5),            /* fp_mult_sf */
995       COSTS_N_INSNS (6),            /* fp_mult_df */
996       COSTS_N_INSNS (30),           /* fp_div_sf */
997       COSTS_N_INSNS (59),           /* fp_div_df */
998       COSTS_N_INSNS (3),            /* int_mult_si */
999       COSTS_N_INSNS (4),            /* int_mult_di */
1000       COSTS_N_INSNS (42),           /* int_div_si */
1001       COSTS_N_INSNS (74),           /* int_div_di */
1002                        1,           /* branch_cost */
1003                        4            /* memory_latency */
1004     },
1005     { /* R5500 */
1006       COSTS_N_INSNS (6),            /* fp_add */
1007       COSTS_N_INSNS (5),            /* fp_mult_sf */
1008       COSTS_N_INSNS (6),            /* fp_mult_df */
1009       COSTS_N_INSNS (30),           /* fp_div_sf */
1010       COSTS_N_INSNS (59),           /* fp_div_df */
1011       COSTS_N_INSNS (5),            /* int_mult_si */
1012       COSTS_N_INSNS (9),            /* int_mult_di */
1013       COSTS_N_INSNS (42),           /* int_div_si */
1014       COSTS_N_INSNS (74),           /* int_div_di */
1015                        1,           /* branch_cost */
1016                        4            /* memory_latency */
1017     },
1018     { /* R7000 */
1019       /* The only costs that are changed here are
1020          integer multiplication.  */
1021       COSTS_N_INSNS (6),            /* fp_add */
1022       COSTS_N_INSNS (7),            /* fp_mult_sf */
1023       COSTS_N_INSNS (8),            /* fp_mult_df */
1024       COSTS_N_INSNS (23),           /* fp_div_sf */
1025       COSTS_N_INSNS (36),           /* fp_div_df */
1026       COSTS_N_INSNS (5),            /* int_mult_si */
1027       COSTS_N_INSNS (9),            /* int_mult_di */
1028       COSTS_N_INSNS (69),           /* int_div_si */
1029       COSTS_N_INSNS (69),           /* int_div_di */
1030                        1,           /* branch_cost */
1031                        4            /* memory_latency */
1032     },
1033     { /* R8000 */
1034       DEFAULT_COSTS
1035     },
1036     { /* R9000 */
1037       /* The only costs that are changed here are
1038          integer multiplication.  */
1039       COSTS_N_INSNS (6),            /* fp_add */
1040       COSTS_N_INSNS (7),            /* fp_mult_sf */
1041       COSTS_N_INSNS (8),            /* fp_mult_df */
1042       COSTS_N_INSNS (23),           /* fp_div_sf */
1043       COSTS_N_INSNS (36),           /* fp_div_df */
1044       COSTS_N_INSNS (3),            /* int_mult_si */
1045       COSTS_N_INSNS (8),            /* int_mult_di */
1046       COSTS_N_INSNS (69),           /* int_div_si */
1047       COSTS_N_INSNS (69),           /* int_div_di */
1048                        1,           /* branch_cost */
1049                        4            /* memory_latency */
1050     },
1051     { /* SB1 */
1052       /* These costs are the same as the SB-1A below.  */
1053       COSTS_N_INSNS (4),            /* fp_add */
1054       COSTS_N_INSNS (4),            /* fp_mult_sf */
1055       COSTS_N_INSNS (4),            /* fp_mult_df */
1056       COSTS_N_INSNS (24),           /* fp_div_sf */
1057       COSTS_N_INSNS (32),           /* fp_div_df */
1058       COSTS_N_INSNS (3),            /* int_mult_si */
1059       COSTS_N_INSNS (4),            /* int_mult_di */
1060       COSTS_N_INSNS (36),           /* int_div_si */
1061       COSTS_N_INSNS (68),           /* int_div_di */
1062                        1,           /* branch_cost */
1063                        4            /* memory_latency */
1064     },
1065     { /* SB1-A */
1066       /* These costs are the same as the SB-1 above.  */
1067       COSTS_N_INSNS (4),            /* fp_add */
1068       COSTS_N_INSNS (4),            /* fp_mult_sf */
1069       COSTS_N_INSNS (4),            /* fp_mult_df */
1070       COSTS_N_INSNS (24),           /* fp_div_sf */
1071       COSTS_N_INSNS (32),           /* fp_div_df */
1072       COSTS_N_INSNS (3),            /* int_mult_si */
1073       COSTS_N_INSNS (4),            /* int_mult_di */
1074       COSTS_N_INSNS (36),           /* int_div_si */
1075       COSTS_N_INSNS (68),           /* int_div_di */
1076                        1,           /* branch_cost */
1077                        4            /* memory_latency */
1078     },
1079     { /* SR71000 */
1080       DEFAULT_COSTS
1081     },
1082   };
1083
1084 \f
1085 /* Nonzero if -march should decide the default value of MASK_SOFT_FLOAT.  */
1086 #ifndef MIPS_MARCH_CONTROLS_SOFT_FLOAT
1087 #define MIPS_MARCH_CONTROLS_SOFT_FLOAT 0
1088 #endif
1089 \f
1090 /* Initialize the GCC target structure.  */
1091 #undef TARGET_ASM_ALIGNED_HI_OP
1092 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
1093 #undef TARGET_ASM_ALIGNED_SI_OP
1094 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
1095 #undef TARGET_ASM_ALIGNED_DI_OP
1096 #define TARGET_ASM_ALIGNED_DI_OP "\t.dword\t"
1097
1098 #undef TARGET_ASM_FUNCTION_PROLOGUE
1099 #define TARGET_ASM_FUNCTION_PROLOGUE mips_output_function_prologue
1100 #undef TARGET_ASM_FUNCTION_EPILOGUE
1101 #define TARGET_ASM_FUNCTION_EPILOGUE mips_output_function_epilogue
1102 #undef TARGET_ASM_SELECT_RTX_SECTION
1103 #define TARGET_ASM_SELECT_RTX_SECTION mips_select_rtx_section
1104 #undef TARGET_ASM_FUNCTION_RODATA_SECTION
1105 #define TARGET_ASM_FUNCTION_RODATA_SECTION mips_function_rodata_section
1106
1107 #undef TARGET_SCHED_REORDER
1108 #define TARGET_SCHED_REORDER mips_sched_reorder
1109 #undef TARGET_SCHED_VARIABLE_ISSUE
1110 #define TARGET_SCHED_VARIABLE_ISSUE mips_variable_issue
1111 #undef TARGET_SCHED_ADJUST_COST
1112 #define TARGET_SCHED_ADJUST_COST mips_adjust_cost
1113 #undef TARGET_SCHED_ISSUE_RATE
1114 #define TARGET_SCHED_ISSUE_RATE mips_issue_rate
1115 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1116 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
1117   mips_multipass_dfa_lookahead
1118
1119 #undef TARGET_DEFAULT_TARGET_FLAGS
1120 #define TARGET_DEFAULT_TARGET_FLAGS             \
1121   (TARGET_DEFAULT                               \
1122    | TARGET_CPU_DEFAULT                         \
1123    | TARGET_ENDIAN_DEFAULT                      \
1124    | TARGET_FP_EXCEPTIONS_DEFAULT               \
1125    | MASK_CHECK_ZERO_DIV                        \
1126    | MASK_FUSED_MADD)
1127 #undef TARGET_HANDLE_OPTION
1128 #define TARGET_HANDLE_OPTION mips_handle_option
1129
1130 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1131 #define TARGET_FUNCTION_OK_FOR_SIBCALL mips_function_ok_for_sibcall
1132
1133 #undef TARGET_VALID_POINTER_MODE
1134 #define TARGET_VALID_POINTER_MODE mips_valid_pointer_mode
1135 #undef TARGET_RTX_COSTS
1136 #define TARGET_RTX_COSTS mips_rtx_costs
1137 #undef TARGET_ADDRESS_COST
1138 #define TARGET_ADDRESS_COST mips_address_cost
1139
1140 #undef TARGET_IN_SMALL_DATA_P
1141 #define TARGET_IN_SMALL_DATA_P mips_in_small_data_p
1142
1143 #undef TARGET_MACHINE_DEPENDENT_REORG
1144 #define TARGET_MACHINE_DEPENDENT_REORG mips_reorg
1145
1146 #undef TARGET_ASM_FILE_START
1147 #undef TARGET_ASM_FILE_END
1148 #define TARGET_ASM_FILE_START mips_file_start
1149 #define TARGET_ASM_FILE_END mips_file_end
1150 #undef TARGET_ASM_FILE_START_FILE_DIRECTIVE
1151 #define TARGET_ASM_FILE_START_FILE_DIRECTIVE true
1152
1153 #undef TARGET_INIT_LIBFUNCS
1154 #define TARGET_INIT_LIBFUNCS mips_init_libfuncs
1155
1156 #undef TARGET_BUILD_BUILTIN_VA_LIST
1157 #define TARGET_BUILD_BUILTIN_VA_LIST mips_build_builtin_va_list
1158 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1159 #define TARGET_GIMPLIFY_VA_ARG_EXPR mips_gimplify_va_arg_expr
1160
1161 #undef TARGET_PROMOTE_FUNCTION_ARGS
1162 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
1163 #undef TARGET_PROMOTE_FUNCTION_RETURN
1164 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
1165 #undef TARGET_PROMOTE_PROTOTYPES
1166 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
1167
1168 #undef TARGET_RETURN_IN_MEMORY
1169 #define TARGET_RETURN_IN_MEMORY mips_return_in_memory
1170 #undef TARGET_RETURN_IN_MSB
1171 #define TARGET_RETURN_IN_MSB mips_return_in_msb
1172
1173 #undef TARGET_ASM_OUTPUT_MI_THUNK
1174 #define TARGET_ASM_OUTPUT_MI_THUNK mips_output_mi_thunk
1175 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1176 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_tree_hwi_hwi_tree_true
1177
1178 #undef TARGET_SETUP_INCOMING_VARARGS
1179 #define TARGET_SETUP_INCOMING_VARARGS mips_setup_incoming_varargs
1180 #undef TARGET_STRICT_ARGUMENT_NAMING
1181 #define TARGET_STRICT_ARGUMENT_NAMING mips_strict_argument_naming
1182 #undef TARGET_MUST_PASS_IN_STACK
1183 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
1184 #undef TARGET_PASS_BY_REFERENCE
1185 #define TARGET_PASS_BY_REFERENCE mips_pass_by_reference
1186 #undef TARGET_CALLEE_COPIES
1187 #define TARGET_CALLEE_COPIES mips_callee_copies
1188 #undef TARGET_ARG_PARTIAL_BYTES
1189 #define TARGET_ARG_PARTIAL_BYTES mips_arg_partial_bytes
1190
1191 #undef TARGET_MODE_REP_EXTENDED
1192 #define TARGET_MODE_REP_EXTENDED mips_mode_rep_extended
1193
1194 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1195 #define TARGET_VECTOR_MODE_SUPPORTED_P mips_vector_mode_supported_p
1196
1197 #undef TARGET_INIT_BUILTINS
1198 #define TARGET_INIT_BUILTINS mips_init_builtins
1199 #undef TARGET_EXPAND_BUILTIN
1200 #define TARGET_EXPAND_BUILTIN mips_expand_builtin
1201
1202 #undef TARGET_HAVE_TLS
1203 #define TARGET_HAVE_TLS HAVE_AS_TLS
1204
1205 #undef TARGET_CANNOT_FORCE_CONST_MEM
1206 #define TARGET_CANNOT_FORCE_CONST_MEM mips_cannot_force_const_mem
1207
1208 #undef TARGET_ENCODE_SECTION_INFO
1209 #define TARGET_ENCODE_SECTION_INFO mips_encode_section_info
1210
1211 #undef TARGET_ATTRIBUTE_TABLE
1212 #define TARGET_ATTRIBUTE_TABLE mips_attribute_table
1213
1214 #undef TARGET_EXTRA_LIVE_ON_ENTRY
1215 #define TARGET_EXTRA_LIVE_ON_ENTRY mips_extra_live_on_entry
1216
1217 #undef TARGET_MIN_ANCHOR_OFFSET
1218 #define TARGET_MIN_ANCHOR_OFFSET -32768
1219 #undef TARGET_MAX_ANCHOR_OFFSET
1220 #define TARGET_MAX_ANCHOR_OFFSET 32767
1221 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1222 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P mips_use_blocks_for_constant_p
1223 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
1224 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P mips_use_anchors_for_symbol_p
1225
1226 struct gcc_target targetm = TARGET_INITIALIZER;
1227 \f
1228 /* Classify symbol X, which must be a SYMBOL_REF or a LABEL_REF.  */
1229
1230 static enum mips_symbol_type
1231 mips_classify_symbol (rtx x)
1232 {
1233   tree decl;
1234
1235   if (GET_CODE (x) == LABEL_REF)
1236     {
1237       if (TARGET_MIPS16)
1238         return SYMBOL_CONSTANT_POOL;
1239       if (TARGET_ABICALLS && !TARGET_ABSOLUTE_ABICALLS)
1240         return SYMBOL_GOT_LOCAL;
1241       return SYMBOL_GENERAL;
1242     }
1243
1244   gcc_assert (GET_CODE (x) == SYMBOL_REF);
1245
1246   if (SYMBOL_REF_TLS_MODEL (x))
1247     return SYMBOL_TLS;
1248
1249   if (CONSTANT_POOL_ADDRESS_P (x))
1250     {
1251       if (TARGET_MIPS16)
1252         return SYMBOL_CONSTANT_POOL;
1253
1254       if (GET_MODE_SIZE (get_pool_mode (x)) <= mips_section_threshold)
1255         return SYMBOL_SMALL_DATA;
1256     }
1257
1258   /* Do not use small-data accesses for weak symbols; they may end up
1259      being zero.  */
1260   if (SYMBOL_REF_SMALL_P (x)
1261       && !SYMBOL_REF_WEAK (x))
1262     return SYMBOL_SMALL_DATA;
1263
1264   if (TARGET_ABICALLS)
1265     {
1266       decl = SYMBOL_REF_DECL (x);
1267       if (decl == 0)
1268         {
1269           if (!SYMBOL_REF_LOCAL_P (x))
1270             return SYMBOL_GOT_GLOBAL;
1271         }
1272       else
1273         {
1274           /* Don't use GOT accesses for locally-binding symbols if
1275              TARGET_ABSOLUTE_ABICALLS.  Otherwise, there are three
1276              cases to consider:
1277
1278                 - o32 PIC (either with or without explicit relocs)
1279                 - n32/n64 PIC without explicit relocs
1280                 - n32/n64 PIC with explicit relocs
1281
1282              In the first case, both local and global accesses will use an
1283              R_MIPS_GOT16 relocation.  We must correctly predict which of
1284              the two semantics (local or global) the assembler and linker
1285              will apply.  The choice doesn't depend on the symbol's
1286              visibility, so we deliberately ignore decl_visibility and
1287              binds_local_p here.
1288
1289              In the second case, the assembler will not use R_MIPS_GOT16
1290              relocations, but it chooses between local and global accesses
1291              in the same way as for o32 PIC.
1292
1293              In the third case we have more freedom since both forms of
1294              access will work for any kind of symbol.  However, there seems
1295              little point in doing things differently.
1296
1297              Note that weakref symbols are not TREE_PUBLIC, but their
1298              targets are global or weak symbols.  Relocations in the
1299              object file will be against the target symbol, so it's
1300              that symbol's binding that matters here.  */
1301           if (DECL_P (decl)
1302               && (TREE_PUBLIC (decl) || DECL_WEAK (decl))
1303               && !(TARGET_ABSOLUTE_ABICALLS && targetm.binds_local_p (decl)))
1304             return SYMBOL_GOT_GLOBAL;
1305         }
1306
1307       if (!TARGET_ABSOLUTE_ABICALLS)
1308         return SYMBOL_GOT_LOCAL;
1309     }
1310
1311   return SYMBOL_GENERAL;
1312 }
1313
1314
1315 /* Split X into a base and a constant offset, storing them in *BASE
1316    and *OFFSET respectively.  */
1317
1318 static void
1319 mips_split_const (rtx x, rtx *base, HOST_WIDE_INT *offset)
1320 {
1321   *offset = 0;
1322
1323   if (GET_CODE (x) == CONST)
1324     {
1325       x = XEXP (x, 0);
1326       if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT)
1327         {
1328           *offset += INTVAL (XEXP (x, 1));
1329           x = XEXP (x, 0);
1330         }
1331     }
1332   *base = x;
1333 }
1334
1335
1336 /* Return true if SYMBOL is a SYMBOL_REF and OFFSET + SYMBOL points
1337    to the same object as SYMBOL, or to the same object_block.  */
1338
1339 static bool
1340 mips_offset_within_object_p (rtx symbol, HOST_WIDE_INT offset)
1341 {
1342   if (GET_CODE (symbol) != SYMBOL_REF)
1343     return false;
1344
1345   if (CONSTANT_POOL_ADDRESS_P (symbol)
1346       && offset >= 0
1347       && offset < (int) GET_MODE_SIZE (get_pool_mode (symbol)))
1348     return true;
1349
1350   if (SYMBOL_REF_DECL (symbol) != 0
1351       && offset >= 0
1352       && offset < int_size_in_bytes (TREE_TYPE (SYMBOL_REF_DECL (symbol))))
1353     return true;
1354
1355   if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol)
1356       && SYMBOL_REF_BLOCK (symbol)
1357       && SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0
1358       && ((unsigned HOST_WIDE_INT) offset + SYMBOL_REF_BLOCK_OFFSET (symbol)
1359           < (unsigned HOST_WIDE_INT) SYMBOL_REF_BLOCK (symbol)->size))
1360     return true;
1361
1362   return false;
1363 }
1364
1365
1366 /* Return true if X is a symbolic constant that can be calculated in
1367    the same way as a bare symbol.  If it is, store the type of the
1368    symbol in *SYMBOL_TYPE.  */
1369
1370 bool
1371 mips_symbolic_constant_p (rtx x, enum mips_symbol_type *symbol_type)
1372 {
1373   HOST_WIDE_INT offset;
1374
1375   mips_split_const (x, &x, &offset);
1376   if (UNSPEC_ADDRESS_P (x))
1377     *symbol_type = UNSPEC_ADDRESS_TYPE (x);
1378   else if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
1379     {
1380       *symbol_type = mips_classify_symbol (x);
1381       if (*symbol_type == SYMBOL_TLS)
1382         return false;
1383     }
1384   else
1385     return false;
1386
1387   if (offset == 0)
1388     return true;
1389
1390   /* Check whether a nonzero offset is valid for the underlying
1391      relocations.  */
1392   switch (*symbol_type)
1393     {
1394     case SYMBOL_GENERAL:
1395     case SYMBOL_64_HIGH:
1396     case SYMBOL_64_MID:
1397     case SYMBOL_64_LOW:
1398       /* If the target has 64-bit pointers and the object file only
1399          supports 32-bit symbols, the values of those symbols will be
1400          sign-extended.  In this case we can't allow an arbitrary offset
1401          in case the 32-bit value X + OFFSET has a different sign from X.  */
1402       if (Pmode == DImode && !ABI_HAS_64BIT_SYMBOLS)
1403         return mips_offset_within_object_p (x, offset);
1404
1405       /* In other cases the relocations can handle any offset.  */
1406       return true;
1407
1408     case SYMBOL_CONSTANT_POOL:
1409       /* Allow constant pool references to be converted to LABEL+CONSTANT.
1410          In this case, we no longer have access to the underlying constant,
1411          but the original symbol-based access was known to be valid.  */
1412       if (GET_CODE (x) == LABEL_REF)
1413         return true;
1414
1415       /* Fall through.  */
1416
1417     case SYMBOL_SMALL_DATA:
1418       /* Make sure that the offset refers to something within the
1419          underlying object.  This should guarantee that the final
1420          PC- or GP-relative offset is within the 16-bit limit.  */
1421       return mips_offset_within_object_p (x, offset);
1422
1423     case SYMBOL_GOT_LOCAL:
1424     case SYMBOL_GOTOFF_PAGE:
1425       /* The linker should provide enough local GOT entries for a
1426          16-bit offset.  Larger offsets may lead to GOT overflow.  */
1427       return SMALL_OPERAND (offset);
1428
1429     case SYMBOL_GOT_GLOBAL:
1430     case SYMBOL_GOTOFF_GLOBAL:
1431     case SYMBOL_GOTOFF_CALL:
1432     case SYMBOL_GOTOFF_LOADGP:
1433     case SYMBOL_TLSGD:
1434     case SYMBOL_TLSLDM:
1435     case SYMBOL_DTPREL:
1436     case SYMBOL_TPREL:
1437     case SYMBOL_GOTTPREL:
1438     case SYMBOL_TLS:
1439       return false;
1440     }
1441   gcc_unreachable ();
1442 }
1443
1444
1445 /* This function is used to implement REG_MODE_OK_FOR_BASE_P.  */
1446
1447 int
1448 mips_regno_mode_ok_for_base_p (int regno, enum machine_mode mode, int strict)
1449 {
1450   if (regno >= FIRST_PSEUDO_REGISTER)
1451     {
1452       if (!strict)
1453         return true;
1454       regno = reg_renumber[regno];
1455     }
1456
1457   /* These fake registers will be eliminated to either the stack or
1458      hard frame pointer, both of which are usually valid base registers.
1459      Reload deals with the cases where the eliminated form isn't valid.  */
1460   if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
1461     return true;
1462
1463   /* In mips16 mode, the stack pointer can only address word and doubleword
1464      values, nothing smaller.  There are two problems here:
1465
1466        (a) Instantiating virtual registers can introduce new uses of the
1467            stack pointer.  If these virtual registers are valid addresses,
1468            the stack pointer should be too.
1469
1470        (b) Most uses of the stack pointer are not made explicit until
1471            FRAME_POINTER_REGNUM and ARG_POINTER_REGNUM have been eliminated.
1472            We don't know until that stage whether we'll be eliminating to the
1473            stack pointer (which needs the restriction) or the hard frame
1474            pointer (which doesn't).
1475
1476      All in all, it seems more consistent to only enforce this restriction
1477      during and after reload.  */
1478   if (TARGET_MIPS16 && regno == STACK_POINTER_REGNUM)
1479     return !strict || GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1480
1481   return TARGET_MIPS16 ? M16_REG_P (regno) : GP_REG_P (regno);
1482 }
1483
1484
1485 /* Return true if X is a valid base register for the given mode.
1486    Allow only hard registers if STRICT.  */
1487
1488 static bool
1489 mips_valid_base_register_p (rtx x, enum machine_mode mode, int strict)
1490 {
1491   if (!strict && GET_CODE (x) == SUBREG)
1492     x = SUBREG_REG (x);
1493
1494   return (REG_P (x)
1495           && mips_regno_mode_ok_for_base_p (REGNO (x), mode, strict));
1496 }
1497
1498
1499 /* Return true if symbols of type SYMBOL_TYPE can directly address a value
1500    with mode MODE.  This is used for both symbolic and LO_SUM addresses.  */
1501
1502 static bool
1503 mips_symbolic_address_p (enum mips_symbol_type symbol_type,
1504                          enum machine_mode mode)
1505 {
1506   switch (symbol_type)
1507     {
1508     case SYMBOL_GENERAL:
1509       return !TARGET_MIPS16;
1510
1511     case SYMBOL_SMALL_DATA:
1512       return true;
1513
1514     case SYMBOL_CONSTANT_POOL:
1515       /* PC-relative addressing is only available for lw and ld.  */
1516       return GET_MODE_SIZE (mode) == 4 || GET_MODE_SIZE (mode) == 8;
1517
1518     case SYMBOL_GOT_LOCAL:
1519       return true;
1520
1521     case SYMBOL_GOT_GLOBAL:
1522       /* The address will have to be loaded from the GOT first.  */
1523       return false;
1524
1525     case SYMBOL_GOTOFF_PAGE:
1526     case SYMBOL_GOTOFF_GLOBAL:
1527     case SYMBOL_GOTOFF_CALL:
1528     case SYMBOL_GOTOFF_LOADGP:
1529     case SYMBOL_TLS:
1530     case SYMBOL_TLSGD:
1531     case SYMBOL_TLSLDM:
1532     case SYMBOL_DTPREL:
1533     case SYMBOL_GOTTPREL:
1534     case SYMBOL_TPREL:
1535     case SYMBOL_64_HIGH:
1536     case SYMBOL_64_MID:
1537     case SYMBOL_64_LOW:
1538       return true;
1539     }
1540   gcc_unreachable ();
1541 }
1542
1543
1544 /* Return true if X is a valid address for machine mode MODE.  If it is,
1545    fill in INFO appropriately.  STRICT is true if we should only accept
1546    hard base registers.  */
1547
1548 static bool
1549 mips_classify_address (struct mips_address_info *info, rtx x,
1550                        enum machine_mode mode, int strict)
1551 {
1552   switch (GET_CODE (x))
1553     {
1554     case REG:
1555     case SUBREG:
1556       info->type = ADDRESS_REG;
1557       info->reg = x;
1558       info->offset = const0_rtx;
1559       return mips_valid_base_register_p (info->reg, mode, strict);
1560
1561     case PLUS:
1562       info->type = ADDRESS_REG;
1563       info->reg = XEXP (x, 0);
1564       info->offset = XEXP (x, 1);
1565       return (mips_valid_base_register_p (info->reg, mode, strict)
1566               && const_arith_operand (info->offset, VOIDmode));
1567
1568     case LO_SUM:
1569       info->type = ADDRESS_LO_SUM;
1570       info->reg = XEXP (x, 0);
1571       info->offset = XEXP (x, 1);
1572       return (mips_valid_base_register_p (info->reg, mode, strict)
1573               && mips_symbolic_constant_p (info->offset, &info->symbol_type)
1574               && mips_symbolic_address_p (info->symbol_type, mode)
1575               && mips_lo_relocs[info->symbol_type] != 0);
1576
1577     case CONST_INT:
1578       /* Small-integer addresses don't occur very often, but they
1579          are legitimate if $0 is a valid base register.  */
1580       info->type = ADDRESS_CONST_INT;
1581       return !TARGET_MIPS16 && SMALL_INT (x);
1582
1583     case CONST:
1584     case LABEL_REF:
1585     case SYMBOL_REF:
1586       info->type = ADDRESS_SYMBOLIC;
1587       return (mips_symbolic_constant_p (x, &info->symbol_type)
1588               && mips_symbolic_address_p (info->symbol_type, mode)
1589               && !mips_split_p[info->symbol_type]);
1590
1591     default:
1592       return false;
1593     }
1594 }
1595
1596 /* Return true if X is a thread-local symbol.  */
1597
1598 static bool
1599 mips_tls_operand_p (rtx x)
1600 {
1601   return GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x) != 0;
1602 }
1603
1604 /* Return true if X can not be forced into a constant pool.  */
1605
1606 static int
1607 mips_tls_symbol_ref_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
1608 {
1609   return mips_tls_operand_p (*x);
1610 }
1611
1612 /* Return true if X can not be forced into a constant pool.  */
1613
1614 static bool
1615 mips_cannot_force_const_mem (rtx x)
1616 {
1617   rtx base;
1618   HOST_WIDE_INT offset;
1619
1620   if (!TARGET_MIPS16)
1621     {
1622       /* As an optimization, reject constants that mips_legitimize_move
1623          can expand inline.
1624
1625          Suppose we have a multi-instruction sequence that loads constant C
1626          into register R.  If R does not get allocated a hard register, and
1627          R is used in an operand that allows both registers and memory
1628          references, reload will consider forcing C into memory and using
1629          one of the instruction's memory alternatives.  Returning false
1630          here will force it to use an input reload instead.  */
1631       if (GET_CODE (x) == CONST_INT)
1632         return true;
1633
1634       mips_split_const (x, &base, &offset);
1635       if (symbolic_operand (base, VOIDmode) && SMALL_OPERAND (offset))
1636         return true;
1637     }
1638
1639   if (TARGET_HAVE_TLS && for_each_rtx (&x, &mips_tls_symbol_ref_1, 0))
1640     return true;
1641
1642   return false;
1643 }
1644
1645 /* Implement TARGET_USE_BLOCKS_FOR_CONSTANT_P.  MIPS16 uses per-function
1646    constant pools, but normal-mode code doesn't need to.  */
1647
1648 static bool
1649 mips_use_blocks_for_constant_p (enum machine_mode mode ATTRIBUTE_UNUSED,
1650                                 rtx x ATTRIBUTE_UNUSED)
1651 {
1652   return !TARGET_MIPS16;
1653 }
1654 \f
1655 /* Return the number of instructions needed to load a symbol of the
1656    given type into a register.  If valid in an address, the same number
1657    of instructions are needed for loads and stores.  Treat extended
1658    mips16 instructions as two instructions.  */
1659
1660 static int
1661 mips_symbol_insns (enum mips_symbol_type type)
1662 {
1663   switch (type)
1664     {
1665     case SYMBOL_GENERAL:
1666       /* In mips16 code, general symbols must be fetched from the
1667          constant pool.  */
1668       if (TARGET_MIPS16)
1669         return 0;
1670
1671       /* When using 64-bit symbols, we need 5 preparatory instructions,
1672          such as:
1673
1674              lui     $at,%highest(symbol)
1675              daddiu  $at,$at,%higher(symbol)
1676              dsll    $at,$at,16
1677              daddiu  $at,$at,%hi(symbol)
1678              dsll    $at,$at,16
1679
1680          The final address is then $at + %lo(symbol).  With 32-bit
1681          symbols we just need a preparatory lui.  */
1682       return (ABI_HAS_64BIT_SYMBOLS ? 6 : 2);
1683
1684     case SYMBOL_SMALL_DATA:
1685       return 1;
1686
1687     case SYMBOL_CONSTANT_POOL:
1688       /* This case is for mips16 only.  Assume we'll need an
1689          extended instruction.  */
1690       return 2;
1691
1692     case SYMBOL_GOT_LOCAL:
1693     case SYMBOL_GOT_GLOBAL:
1694       /* Unless -funit-at-a-time is in effect, we can't be sure whether
1695          the local/global classification is accurate.  See override_options
1696          for details.
1697
1698          The worst cases are:
1699
1700          (1) For local symbols when generating o32 or o64 code.  The assembler
1701              will use:
1702
1703                  lw           $at,%got(symbol)
1704                  nop
1705
1706              ...and the final address will be $at + %lo(symbol).
1707
1708          (2) For global symbols when -mxgot.  The assembler will use:
1709
1710                  lui     $at,%got_hi(symbol)
1711                  (d)addu $at,$at,$gp
1712
1713              ...and the final address will be $at + %got_lo(symbol).  */
1714       return 3;
1715
1716     case SYMBOL_GOTOFF_PAGE:
1717     case SYMBOL_GOTOFF_GLOBAL:
1718     case SYMBOL_GOTOFF_CALL:
1719     case SYMBOL_GOTOFF_LOADGP:
1720     case SYMBOL_64_HIGH:
1721     case SYMBOL_64_MID:
1722     case SYMBOL_64_LOW:
1723     case SYMBOL_TLSGD:
1724     case SYMBOL_TLSLDM:
1725     case SYMBOL_DTPREL:
1726     case SYMBOL_GOTTPREL:
1727     case SYMBOL_TPREL:
1728       /* Check whether the offset is a 16- or 32-bit value.  */
1729       return mips_split_p[type] ? 2 : 1;
1730
1731     case SYMBOL_TLS:
1732       /* We don't treat a bare TLS symbol as a constant.  */
1733       return 0;
1734     }
1735   gcc_unreachable ();
1736 }
1737
1738 /* Return true if X is a legitimate $sp-based address for mode MDOE.  */
1739
1740 bool
1741 mips_stack_address_p (rtx x, enum machine_mode mode)
1742 {
1743   struct mips_address_info addr;
1744
1745   return (mips_classify_address (&addr, x, mode, false)
1746           && addr.type == ADDRESS_REG
1747           && addr.reg == stack_pointer_rtx);
1748 }
1749
1750 /* Return true if a value at OFFSET bytes from BASE can be accessed
1751    using an unextended mips16 instruction.  MODE is the mode of the
1752    value.
1753
1754    Usually the offset in an unextended instruction is a 5-bit field.
1755    The offset is unsigned and shifted left once for HIs, twice
1756    for SIs, and so on.  An exception is SImode accesses off the
1757    stack pointer, which have an 8-bit immediate field.  */
1758
1759 static bool
1760 mips16_unextended_reference_p (enum machine_mode mode, rtx base, rtx offset)
1761 {
1762   if (TARGET_MIPS16
1763       && GET_CODE (offset) == CONST_INT
1764       && INTVAL (offset) >= 0
1765       && (INTVAL (offset) & (GET_MODE_SIZE (mode) - 1)) == 0)
1766     {
1767       if (GET_MODE_SIZE (mode) == 4 && base == stack_pointer_rtx)
1768         return INTVAL (offset) < 256 * GET_MODE_SIZE (mode);
1769       return INTVAL (offset) < 32 * GET_MODE_SIZE (mode);
1770     }
1771   return false;
1772 }
1773
1774
1775 /* Return the number of instructions needed to load or store a value
1776    of mode MODE at X.  Return 0 if X isn't valid for MODE.
1777
1778    For mips16 code, count extended instructions as two instructions.  */
1779
1780 int
1781 mips_address_insns (rtx x, enum machine_mode mode)
1782 {
1783   struct mips_address_info addr;
1784   int factor;
1785
1786   if (mode == BLKmode)
1787     /* BLKmode is used for single unaligned loads and stores.  */
1788     factor = 1;
1789   else
1790     /* Each word of a multi-word value will be accessed individually.  */
1791     factor = (GET_MODE_SIZE (mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1792
1793   if (mips_classify_address (&addr, x, mode, false))
1794     switch (addr.type)
1795       {
1796       case ADDRESS_REG:
1797         if (TARGET_MIPS16
1798             && !mips16_unextended_reference_p (mode, addr.reg, addr.offset))
1799           return factor * 2;
1800         return factor;
1801
1802       case ADDRESS_LO_SUM:
1803         return (TARGET_MIPS16 ? factor * 2 : factor);
1804
1805       case ADDRESS_CONST_INT:
1806         return factor;
1807
1808       case ADDRESS_SYMBOLIC:
1809         return factor * mips_symbol_insns (addr.symbol_type);
1810       }
1811   return 0;
1812 }
1813
1814
1815 /* Likewise for constant X.  */
1816
1817 int
1818 mips_const_insns (rtx x)
1819 {
1820   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
1821   enum mips_symbol_type symbol_type;
1822   HOST_WIDE_INT offset;
1823
1824   switch (GET_CODE (x))
1825     {
1826     case HIGH:
1827       if (TARGET_MIPS16
1828           || !mips_symbolic_constant_p (XEXP (x, 0), &symbol_type)
1829           || !mips_split_p[symbol_type])
1830         return 0;
1831
1832       return 1;
1833
1834     case CONST_INT:
1835       if (TARGET_MIPS16)
1836         /* Unsigned 8-bit constants can be loaded using an unextended
1837            LI instruction.  Unsigned 16-bit constants can be loaded
1838            using an extended LI.  Negative constants must be loaded
1839            using LI and then negated.  */
1840         return (INTVAL (x) >= 0 && INTVAL (x) < 256 ? 1
1841                 : SMALL_OPERAND_UNSIGNED (INTVAL (x)) ? 2
1842                 : INTVAL (x) > -256 && INTVAL (x) < 0 ? 2
1843                 : SMALL_OPERAND_UNSIGNED (-INTVAL (x)) ? 3
1844                 : 0);
1845
1846       return mips_build_integer (codes, INTVAL (x));
1847
1848     case CONST_DOUBLE:
1849     case CONST_VECTOR:
1850       return (!TARGET_MIPS16 && x == CONST0_RTX (GET_MODE (x)) ? 1 : 0);
1851
1852     case CONST:
1853       if (CONST_GP_P (x))
1854         return 1;
1855
1856       /* See if we can refer to X directly.  */
1857       if (mips_symbolic_constant_p (x, &symbol_type))
1858         return mips_symbol_insns (symbol_type);
1859
1860       /* Otherwise try splitting the constant into a base and offset.
1861          16-bit offsets can be added using an extra addiu.  Larger offsets
1862          must be calculated separately and then added to the base.  */
1863       mips_split_const (x, &x, &offset);
1864       if (offset != 0)
1865         {
1866           int n = mips_const_insns (x);
1867           if (n != 0)
1868             {
1869               if (SMALL_OPERAND (offset))
1870                 return n + 1;
1871               else
1872                 return n + 1 + mips_build_integer (codes, offset);
1873             }
1874         }
1875       return 0;
1876
1877     case SYMBOL_REF:
1878     case LABEL_REF:
1879       return mips_symbol_insns (mips_classify_symbol (x));
1880
1881     default:
1882       return 0;
1883     }
1884 }
1885
1886
1887 /* Return the number of instructions needed for memory reference X.
1888    Count extended mips16 instructions as two instructions.  */
1889
1890 int
1891 mips_fetch_insns (rtx x)
1892 {
1893   gcc_assert (MEM_P (x));
1894   return mips_address_insns (XEXP (x, 0), GET_MODE (x));
1895 }
1896
1897
1898 /* Return the number of instructions needed for an integer division.  */
1899
1900 int
1901 mips_idiv_insns (void)
1902 {
1903   int count;
1904
1905   count = 1;
1906   if (TARGET_CHECK_ZERO_DIV)
1907     {
1908       if (GENERATE_DIVIDE_TRAPS)
1909         count++;
1910       else
1911         count += 2;
1912     }
1913
1914   if (TARGET_FIX_R4000 || TARGET_FIX_R4400)
1915     count++;
1916   return count;
1917 }
1918 \f
1919 /* This function is used to implement GO_IF_LEGITIMATE_ADDRESS.  It
1920    returns a nonzero value if X is a legitimate address for a memory
1921    operand of the indicated MODE.  STRICT is nonzero if this function
1922    is called during reload.  */
1923
1924 bool
1925 mips_legitimate_address_p (enum machine_mode mode, rtx x, int strict)
1926 {
1927   struct mips_address_info addr;
1928
1929   return mips_classify_address (&addr, x, mode, strict);
1930 }
1931
1932
1933 /* Copy VALUE to a register and return that register.  If new psuedos
1934    are allowed, copy it into a new register, otherwise use DEST.  */
1935
1936 static rtx
1937 mips_force_temporary (rtx dest, rtx value)
1938 {
1939   if (!no_new_pseudos)
1940     return force_reg (Pmode, value);
1941   else
1942     {
1943       emit_move_insn (copy_rtx (dest), value);
1944       return dest;
1945     }
1946 }
1947
1948
1949 /* Return a LO_SUM expression for ADDR.  TEMP is as for mips_force_temporary
1950    and is used to load the high part into a register.  */
1951
1952 rtx
1953 mips_split_symbol (rtx temp, rtx addr)
1954 {
1955   rtx high;
1956
1957   if (TARGET_MIPS16)
1958     high = mips16_gp_pseudo_reg ();
1959   else
1960     high = mips_force_temporary (temp, gen_rtx_HIGH (Pmode, copy_rtx (addr)));
1961   return gen_rtx_LO_SUM (Pmode, high, addr);
1962 }
1963
1964
1965 /* Return an UNSPEC address with underlying address ADDRESS and symbol
1966    type SYMBOL_TYPE.  */
1967
1968 rtx
1969 mips_unspec_address (rtx address, enum mips_symbol_type symbol_type)
1970 {
1971   rtx base;
1972   HOST_WIDE_INT offset;
1973
1974   mips_split_const (address, &base, &offset);
1975   base = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, base),
1976                          UNSPEC_ADDRESS_FIRST + symbol_type);
1977   return plus_constant (gen_rtx_CONST (Pmode, base), offset);
1978 }
1979
1980
1981 /* If mips_unspec_address (ADDR, SYMBOL_TYPE) is a 32-bit value, add the
1982    high part to BASE and return the result.  Just return BASE otherwise.
1983    TEMP is available as a temporary register if needed.
1984
1985    The returned expression can be used as the first operand to a LO_SUM.  */
1986
1987 static rtx
1988 mips_unspec_offset_high (rtx temp, rtx base, rtx addr,
1989                          enum mips_symbol_type symbol_type)
1990 {
1991   if (mips_split_p[symbol_type])
1992     {
1993       addr = gen_rtx_HIGH (Pmode, mips_unspec_address (addr, symbol_type));
1994       addr = mips_force_temporary (temp, addr);
1995       return mips_force_temporary (temp, gen_rtx_PLUS (Pmode, addr, base));
1996     }
1997   return base;
1998 }
1999
2000
2001 /* Return a legitimate address for REG + OFFSET.  TEMP is as for
2002    mips_force_temporary; it is only needed when OFFSET is not a
2003    SMALL_OPERAND.  */
2004
2005 static rtx
2006 mips_add_offset (rtx temp, rtx reg, HOST_WIDE_INT offset)
2007 {
2008   if (!SMALL_OPERAND (offset))
2009     {
2010       rtx high;
2011       if (TARGET_MIPS16)
2012         {
2013           /* Load the full offset into a register so that we can use
2014              an unextended instruction for the address itself.  */
2015           high = GEN_INT (offset);
2016           offset = 0;
2017         }
2018       else
2019         {
2020           /* Leave OFFSET as a 16-bit offset and put the excess in HIGH.  */
2021           high = GEN_INT (CONST_HIGH_PART (offset));
2022           offset = CONST_LOW_PART (offset);
2023         }
2024       high = mips_force_temporary (temp, high);
2025       reg = mips_force_temporary (temp, gen_rtx_PLUS (Pmode, high, reg));
2026     }
2027   return plus_constant (reg, offset);
2028 }
2029
2030 /* Emit a call to __tls_get_addr.  SYM is the TLS symbol we are
2031    referencing, and TYPE is the symbol type to use (either global
2032    dynamic or local dynamic).  V0 is an RTX for the return value
2033    location.  The entire insn sequence is returned.  */
2034
2035 static GTY(()) rtx mips_tls_symbol;
2036
2037 static rtx
2038 mips_call_tls_get_addr (rtx sym, enum mips_symbol_type type, rtx v0)
2039 {
2040   rtx insn, loc, tga, a0;
2041
2042   a0 = gen_rtx_REG (Pmode, GP_ARG_FIRST);
2043
2044   if (!mips_tls_symbol)
2045     mips_tls_symbol = init_one_libfunc ("__tls_get_addr");
2046
2047   loc = mips_unspec_address (sym, type);
2048
2049   start_sequence ();
2050
2051   emit_insn (gen_rtx_SET (Pmode, a0,
2052                           gen_rtx_LO_SUM (Pmode, pic_offset_table_rtx, loc)));
2053   tga = gen_rtx_MEM (Pmode, mips_tls_symbol);
2054   insn = emit_call_insn (gen_call_value (v0, tga, const0_rtx, const0_rtx));
2055   CONST_OR_PURE_CALL_P (insn) = 1;
2056   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), v0);
2057   use_reg (&CALL_INSN_FUNCTION_USAGE (insn), a0);
2058   insn = get_insns ();
2059
2060   end_sequence ();
2061
2062   return insn;
2063 }
2064
2065 /* Generate the code to access LOC, a thread local SYMBOL_REF.  The
2066    return value will be a valid address and move_operand (either a REG
2067    or a LO_SUM).  */
2068
2069 static rtx
2070 mips_legitimize_tls_address (rtx loc)
2071 {
2072   rtx dest, insn, v0, v1, tmp1, tmp2, eqv;
2073   enum tls_model model;
2074
2075   v0 = gen_rtx_REG (Pmode, GP_RETURN);
2076   v1 = gen_rtx_REG (Pmode, GP_RETURN + 1);
2077
2078   model = SYMBOL_REF_TLS_MODEL (loc);
2079   /* Only TARGET_ABICALLS code can have more than one module; other
2080      code must be be static and should not use a GOT.  All TLS models
2081      reduce to local exec in this situation.  */
2082   if (!TARGET_ABICALLS)
2083     model = TLS_MODEL_LOCAL_EXEC;
2084
2085   switch (model)
2086     {
2087     case TLS_MODEL_GLOBAL_DYNAMIC:
2088       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSGD, v0);
2089       dest = gen_reg_rtx (Pmode);
2090       emit_libcall_block (insn, dest, v0, loc);
2091       break;
2092
2093     case TLS_MODEL_LOCAL_DYNAMIC:
2094       insn = mips_call_tls_get_addr (loc, SYMBOL_TLSLDM, v0);
2095       tmp1 = gen_reg_rtx (Pmode);
2096
2097       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
2098          share the LDM result with other LD model accesses.  */
2099       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
2100                             UNSPEC_TLS_LDM);
2101       emit_libcall_block (insn, tmp1, v0, eqv);
2102
2103       tmp2 = mips_unspec_offset_high (NULL, tmp1, loc, SYMBOL_DTPREL);
2104       dest = gen_rtx_LO_SUM (Pmode, tmp2,
2105                              mips_unspec_address (loc, SYMBOL_DTPREL));
2106       break;
2107
2108     case TLS_MODEL_INITIAL_EXEC:
2109       tmp1 = gen_reg_rtx (Pmode);
2110       tmp2 = mips_unspec_address (loc, SYMBOL_GOTTPREL);
2111       if (Pmode == DImode)
2112         {
2113           emit_insn (gen_tls_get_tp_di (v1));
2114           emit_insn (gen_load_gotdi (tmp1, pic_offset_table_rtx, tmp2));
2115         }
2116       else
2117         {
2118           emit_insn (gen_tls_get_tp_si (v1));
2119           emit_insn (gen_load_gotsi (tmp1, pic_offset_table_rtx, tmp2));
2120         }
2121       dest = gen_reg_rtx (Pmode);
2122       emit_insn (gen_add3_insn (dest, tmp1, v1));
2123       break;
2124
2125     case TLS_MODEL_LOCAL_EXEC:
2126       if (Pmode == DImode)
2127         emit_insn (gen_tls_get_tp_di (v1));
2128       else
2129         emit_insn (gen_tls_get_tp_si (v1));
2130
2131       tmp1 = mips_unspec_offset_high (NULL, v1, loc, SYMBOL_TPREL);
2132       dest = gen_rtx_LO_SUM (Pmode, tmp1,
2133                              mips_unspec_address (loc, SYMBOL_TPREL));
2134       break;
2135
2136     default:
2137       gcc_unreachable ();
2138     }
2139
2140   return dest;
2141 }
2142
2143 /* This function is used to implement LEGITIMIZE_ADDRESS.  If *XLOC can
2144    be legitimized in a way that the generic machinery might not expect,
2145    put the new address in *XLOC and return true.  MODE is the mode of
2146    the memory being accessed.  */
2147
2148 bool
2149 mips_legitimize_address (rtx *xloc, enum machine_mode mode)
2150 {
2151   enum mips_symbol_type symbol_type;
2152
2153   if (mips_tls_operand_p (*xloc))
2154     {
2155       *xloc = mips_legitimize_tls_address (*xloc);
2156       return true;
2157     }
2158
2159   /* See if the address can split into a high part and a LO_SUM.  */
2160   if (mips_symbolic_constant_p (*xloc, &symbol_type)
2161       && mips_symbolic_address_p (symbol_type, mode)
2162       && mips_split_p[symbol_type])
2163     {
2164       *xloc = mips_split_symbol (0, *xloc);
2165       return true;
2166     }
2167
2168   if (GET_CODE (*xloc) == PLUS && GET_CODE (XEXP (*xloc, 1)) == CONST_INT)
2169     {
2170       /* Handle REG + CONSTANT using mips_add_offset.  */
2171       rtx reg;
2172
2173       reg = XEXP (*xloc, 0);
2174       if (!mips_valid_base_register_p (reg, mode, 0))
2175         reg = copy_to_mode_reg (Pmode, reg);
2176       *xloc = mips_add_offset (0, reg, INTVAL (XEXP (*xloc, 1)));
2177       return true;
2178     }
2179
2180   return false;
2181 }
2182
2183
2184 /* Subroutine of mips_build_integer (with the same interface).
2185    Assume that the final action in the sequence should be a left shift.  */
2186
2187 static unsigned int
2188 mips_build_shift (struct mips_integer_op *codes, HOST_WIDE_INT value)
2189 {
2190   unsigned int i, shift;
2191
2192   /* Shift VALUE right until its lowest bit is set.  Shift arithmetically
2193      since signed numbers are easier to load than unsigned ones.  */
2194   shift = 0;
2195   while ((value & 1) == 0)
2196     value /= 2, shift++;
2197
2198   i = mips_build_integer (codes, value);
2199   codes[i].code = ASHIFT;
2200   codes[i].value = shift;
2201   return i + 1;
2202 }
2203
2204
2205 /* As for mips_build_shift, but assume that the final action will be
2206    an IOR or PLUS operation.  */
2207
2208 static unsigned int
2209 mips_build_lower (struct mips_integer_op *codes, unsigned HOST_WIDE_INT value)
2210 {
2211   unsigned HOST_WIDE_INT high;
2212   unsigned int i;
2213
2214   high = value & ~(unsigned HOST_WIDE_INT) 0xffff;
2215   if (!LUI_OPERAND (high) && (value & 0x18000) == 0x18000)
2216     {
2217       /* The constant is too complex to load with a simple lui/ori pair
2218          so our goal is to clear as many trailing zeros as possible.
2219          In this case, we know bit 16 is set and that the low 16 bits
2220          form a negative number.  If we subtract that number from VALUE,
2221          we will clear at least the lowest 17 bits, maybe more.  */
2222       i = mips_build_integer (codes, CONST_HIGH_PART (value));
2223       codes[i].code = PLUS;
2224       codes[i].value = CONST_LOW_PART (value);
2225     }
2226   else
2227     {
2228       i = mips_build_integer (codes, high);
2229       codes[i].code = IOR;
2230       codes[i].value = value & 0xffff;
2231     }
2232   return i + 1;
2233 }
2234
2235
2236 /* Fill CODES with a sequence of rtl operations to load VALUE.
2237    Return the number of operations needed.  */
2238
2239 static unsigned int
2240 mips_build_integer (struct mips_integer_op *codes,
2241                     unsigned HOST_WIDE_INT value)
2242 {
2243   if (SMALL_OPERAND (value)
2244       || SMALL_OPERAND_UNSIGNED (value)
2245       || LUI_OPERAND (value))
2246     {
2247       /* The value can be loaded with a single instruction.  */
2248       codes[0].code = UNKNOWN;
2249       codes[0].value = value;
2250       return 1;
2251     }
2252   else if ((value & 1) != 0 || LUI_OPERAND (CONST_HIGH_PART (value)))
2253     {
2254       /* Either the constant is a simple LUI/ORI combination or its
2255          lowest bit is set.  We don't want to shift in this case.  */
2256       return mips_build_lower (codes, value);
2257     }
2258   else if ((value & 0xffff) == 0)
2259     {
2260       /* The constant will need at least three actions.  The lowest
2261          16 bits are clear, so the final action will be a shift.  */
2262       return mips_build_shift (codes, value);
2263     }
2264   else
2265     {
2266       /* The final action could be a shift, add or inclusive OR.
2267          Rather than use a complex condition to select the best
2268          approach, try both mips_build_shift and mips_build_lower
2269          and pick the one that gives the shortest sequence.
2270          Note that this case is only used once per constant.  */
2271       struct mips_integer_op alt_codes[MIPS_MAX_INTEGER_OPS];
2272       unsigned int cost, alt_cost;
2273
2274       cost = mips_build_shift (codes, value);
2275       alt_cost = mips_build_lower (alt_codes, value);
2276       if (alt_cost < cost)
2277         {
2278           memcpy (codes, alt_codes, alt_cost * sizeof (codes[0]));
2279           cost = alt_cost;
2280         }
2281       return cost;
2282     }
2283 }
2284
2285
2286 /* Load VALUE into DEST, using TEMP as a temporary register if need be.  */
2287
2288 void
2289 mips_move_integer (rtx dest, rtx temp, unsigned HOST_WIDE_INT value)
2290 {
2291   struct mips_integer_op codes[MIPS_MAX_INTEGER_OPS];
2292   enum machine_mode mode;
2293   unsigned int i, cost;
2294   rtx x;
2295
2296   mode = GET_MODE (dest);
2297   cost = mips_build_integer (codes, value);
2298
2299   /* Apply each binary operation to X.  Invariant: X is a legitimate
2300      source operand for a SET pattern.  */
2301   x = GEN_INT (codes[0].value);
2302   for (i = 1; i < cost; i++)
2303     {
2304       if (no_new_pseudos)
2305         {
2306           emit_insn (gen_rtx_SET (VOIDmode, temp, x));
2307           x = temp;
2308         }
2309       else
2310         x = force_reg (mode, x);
2311       x = gen_rtx_fmt_ee (codes[i].code, mode, x, GEN_INT (codes[i].value));
2312     }
2313
2314   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
2315 }
2316
2317
2318 /* Subroutine of mips_legitimize_move.  Move constant SRC into register
2319    DEST given that SRC satisfies immediate_operand but doesn't satisfy
2320    move_operand.  */
2321
2322 static void
2323 mips_legitimize_const_move (enum machine_mode mode, rtx dest, rtx src)
2324 {
2325   rtx base;
2326   HOST_WIDE_INT offset;
2327
2328   /* Split moves of big integers into smaller pieces.  */
2329   if (splittable_const_int_operand (src, mode))
2330     {
2331       mips_move_integer (dest, dest, INTVAL (src));
2332       return;
2333     }
2334
2335   /* Split moves of symbolic constants into high/low pairs.  */
2336   if (splittable_symbolic_operand (src, mode))
2337     {
2338       emit_insn (gen_rtx_SET (VOIDmode, dest, mips_split_symbol (dest, src)));
2339       return;
2340     }
2341
2342   if (mips_tls_operand_p (src))
2343     {
2344       emit_move_insn (dest, mips_legitimize_tls_address (src));
2345       return;
2346     }
2347
2348   /* If we have (const (plus symbol offset)), load the symbol first
2349      and then add in the offset.  This is usually better than forcing
2350      the constant into memory, at least in non-mips16 code.  */
2351   mips_split_const (src, &base, &offset);
2352   if (!TARGET_MIPS16
2353       && offset != 0
2354       && (!no_new_pseudos || SMALL_OPERAND (offset)))
2355     {
2356       base = mips_force_temporary (dest, base);
2357       emit_move_insn (dest, mips_add_offset (0, base, offset));
2358       return;
2359     }
2360
2361   src = force_const_mem (mode, src);
2362
2363   /* When using explicit relocs, constant pool references are sometimes
2364      not legitimate addresses.  */
2365   if (!memory_operand (src, VOIDmode))
2366     src = replace_equiv_address (src, mips_split_symbol (dest, XEXP (src, 0)));
2367   emit_move_insn (dest, src);
2368 }
2369
2370
2371 /* If (set DEST SRC) is not a valid instruction, emit an equivalent
2372    sequence that is valid.  */
2373
2374 bool
2375 mips_legitimize_move (enum machine_mode mode, rtx dest, rtx src)
2376 {
2377   if (!register_operand (dest, mode) && !reg_or_0_operand (src, mode))
2378     {
2379       emit_move_insn (dest, force_reg (mode, src));
2380       return true;
2381     }
2382
2383   /* Check for individual, fully-reloaded mflo and mfhi instructions.  */
2384   if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
2385       && REG_P (src) && MD_REG_P (REGNO (src))
2386       && REG_P (dest) && GP_REG_P (REGNO (dest)))
2387     {
2388       int other_regno = REGNO (src) == HI_REGNUM ? LO_REGNUM : HI_REGNUM;
2389       if (GET_MODE_SIZE (mode) <= 4)
2390         emit_insn (gen_mfhilo_si (gen_rtx_REG (SImode, REGNO (dest)),
2391                                   gen_rtx_REG (SImode, REGNO (src)),
2392                                   gen_rtx_REG (SImode, other_regno)));
2393       else
2394         emit_insn (gen_mfhilo_di (gen_rtx_REG (DImode, REGNO (dest)),
2395                                   gen_rtx_REG (DImode, REGNO (src)),
2396                                   gen_rtx_REG (DImode, other_regno)));
2397       return true;
2398     }
2399
2400   /* We need to deal with constants that would be legitimate
2401      immediate_operands but not legitimate move_operands.  */
2402   if (CONSTANT_P (src) && !move_operand (src, mode))
2403     {
2404       mips_legitimize_const_move (mode, dest, src);
2405       set_unique_reg_note (get_last_insn (), REG_EQUAL, copy_rtx (src));
2406       return true;
2407     }
2408   return false;
2409 }
2410 \f
2411 /* We need a lot of little routines to check constant values on the
2412    mips16.  These are used to figure out how long the instruction will
2413    be.  It would be much better to do this using constraints, but
2414    there aren't nearly enough letters available.  */
2415
2416 static int
2417 m16_check_op (rtx op, int low, int high, int mask)
2418 {
2419   return (GET_CODE (op) == CONST_INT
2420           && INTVAL (op) >= low
2421           && INTVAL (op) <= high
2422           && (INTVAL (op) & mask) == 0);
2423 }
2424
2425 int
2426 m16_uimm3_b (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2427 {
2428   return m16_check_op (op, 0x1, 0x8, 0);
2429 }
2430
2431 int
2432 m16_simm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2433 {
2434   return m16_check_op (op, - 0x8, 0x7, 0);
2435 }
2436
2437 int
2438 m16_nsimm4_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2439 {
2440   return m16_check_op (op, - 0x7, 0x8, 0);
2441 }
2442
2443 int
2444 m16_simm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2445 {
2446   return m16_check_op (op, - 0x10, 0xf, 0);
2447 }
2448
2449 int
2450 m16_nsimm5_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2451 {
2452   return m16_check_op (op, - 0xf, 0x10, 0);
2453 }
2454
2455 int
2456 m16_uimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2457 {
2458   return m16_check_op (op, (- 0x10) << 2, 0xf << 2, 3);
2459 }
2460
2461 int
2462 m16_nuimm5_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2463 {
2464   return m16_check_op (op, (- 0xf) << 2, 0x10 << 2, 3);
2465 }
2466
2467 int
2468 m16_simm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2469 {
2470   return m16_check_op (op, - 0x80, 0x7f, 0);
2471 }
2472
2473 int
2474 m16_nsimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2475 {
2476   return m16_check_op (op, - 0x7f, 0x80, 0);
2477 }
2478
2479 int
2480 m16_uimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2481 {
2482   return m16_check_op (op, 0x0, 0xff, 0);
2483 }
2484
2485 int
2486 m16_nuimm8_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2487 {
2488   return m16_check_op (op, - 0xff, 0x0, 0);
2489 }
2490
2491 int
2492 m16_uimm8_m1_1 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2493 {
2494   return m16_check_op (op, - 0x1, 0xfe, 0);
2495 }
2496
2497 int
2498 m16_uimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2499 {
2500   return m16_check_op (op, 0x0, 0xff << 2, 3);
2501 }
2502
2503 int
2504 m16_nuimm8_4 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2505 {
2506   return m16_check_op (op, (- 0xff) << 2, 0x0, 3);
2507 }
2508
2509 int
2510 m16_simm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2511 {
2512   return m16_check_op (op, (- 0x80) << 3, 0x7f << 3, 7);
2513 }
2514
2515 int
2516 m16_nsimm8_8 (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
2517 {
2518   return m16_check_op (op, (- 0x7f) << 3, 0x80 << 3, 7);
2519 }
2520 \f
2521 static bool
2522 mips_rtx_costs (rtx x, int code, int outer_code, int *total)
2523 {
2524   enum machine_mode mode = GET_MODE (x);
2525   bool float_mode_p = FLOAT_MODE_P (mode);
2526
2527   switch (code)
2528     {
2529     case CONST_INT:
2530       if (TARGET_MIPS16)
2531         {
2532           /* A number between 1 and 8 inclusive is efficient for a shift.
2533              Otherwise, we will need an extended instruction.  */
2534           if ((outer_code) == ASHIFT || (outer_code) == ASHIFTRT
2535               || (outer_code) == LSHIFTRT)
2536             {
2537               if (INTVAL (x) >= 1 && INTVAL (x) <= 8)
2538                 *total = 0;
2539               else
2540                 *total = COSTS_N_INSNS (1);
2541               return true;
2542             }
2543
2544           /* We can use cmpi for an xor with an unsigned 16 bit value.  */
2545           if ((outer_code) == XOR
2546               && INTVAL (x) >= 0 && INTVAL (x) < 0x10000)
2547             {
2548               *total = 0;
2549               return true;
2550             }
2551
2552           /* We may be able to use slt or sltu for a comparison with a
2553              signed 16 bit value.  (The boundary conditions aren't quite
2554              right, but this is just a heuristic anyhow.)  */
2555           if (((outer_code) == LT || (outer_code) == LE
2556                || (outer_code) == GE || (outer_code) == GT
2557                || (outer_code) == LTU || (outer_code) == LEU
2558                || (outer_code) == GEU || (outer_code) == GTU)
2559               && INTVAL (x) >= -0x8000 && INTVAL (x) < 0x8000)
2560             {
2561               *total = 0;
2562               return true;
2563             }
2564
2565           /* Equality comparisons with 0 are cheap.  */
2566           if (((outer_code) == EQ || (outer_code) == NE)
2567               && INTVAL (x) == 0)
2568             {
2569               *total = 0;
2570               return true;
2571             }
2572
2573           /* Constants in the range 0...255 can be loaded with an unextended
2574              instruction.  They are therefore as cheap as a register move.
2575
2576              Given the choice between "li R1,0...255" and "move R1,R2"
2577              (where R2 is a known constant), it is usually better to use "li",
2578              since we do not want to unnecessarily extend the lifetime
2579              of R2.  */
2580           if (outer_code == SET
2581               && INTVAL (x) >= 0
2582               && INTVAL (x) < 256)
2583             {
2584               *total = 0;
2585               return true;
2586             }
2587         }
2588       else
2589         {
2590           /* These can be used anywhere. */
2591           *total = 0;
2592           return true;
2593         }
2594
2595       /* Otherwise fall through to the handling below because
2596          we'll need to construct the constant.  */
2597
2598     case CONST:
2599     case SYMBOL_REF:
2600     case LABEL_REF:
2601     case CONST_DOUBLE:
2602       if (LEGITIMATE_CONSTANT_P (x))
2603         {
2604           *total = COSTS_N_INSNS (1);
2605           return true;
2606         }
2607       else
2608         {
2609           /* The value will need to be fetched from the constant pool.  */
2610           *total = CONSTANT_POOL_COST;
2611           return true;
2612         }
2613
2614     case MEM:
2615       {
2616         /* If the address is legitimate, return the number of
2617            instructions it needs, otherwise use the default handling.  */
2618         int n = mips_address_insns (XEXP (x, 0), GET_MODE (x));
2619         if (n > 0)
2620           {
2621             *total = COSTS_N_INSNS (n + 1);
2622             return true;
2623           }
2624         return false;
2625       }
2626
2627     case FFS:
2628       *total = COSTS_N_INSNS (6);
2629       return true;
2630
2631     case NOT:
2632       *total = COSTS_N_INSNS ((mode == DImode && !TARGET_64BIT) ? 2 : 1);
2633       return true;
2634
2635     case AND:
2636     case IOR:
2637     case XOR:
2638       if (mode == DImode && !TARGET_64BIT)
2639         {
2640           *total = COSTS_N_INSNS (2);
2641           return true;
2642         }
2643       return false;
2644
2645     case ASHIFT:
2646     case ASHIFTRT:
2647     case LSHIFTRT:
2648       if (mode == DImode && !TARGET_64BIT)
2649         {
2650           *total = COSTS_N_INSNS ((GET_CODE (XEXP (x, 1)) == CONST_INT)
2651                                   ? 4 : 12);
2652           return true;
2653         }
2654       return false;
2655
2656     case ABS:
2657       if (float_mode_p)
2658         *total = COSTS_N_INSNS (1);
2659       else
2660         *total = COSTS_N_INSNS (4);
2661       return true;
2662
2663     case LO_SUM:
2664       *total = COSTS_N_INSNS (1);
2665       return true;
2666
2667     case PLUS:
2668     case MINUS:
2669       if (float_mode_p)
2670         {
2671           *total = mips_cost->fp_add;
2672           return true;
2673         }
2674
2675       else if (mode == DImode && !TARGET_64BIT)
2676         {
2677           *total = COSTS_N_INSNS (4);
2678           return true;
2679         }
2680       return false;
2681
2682     case NEG:
2683       if (mode == DImode && !TARGET_64BIT)
2684         {
2685           *total = COSTS_N_INSNS (4);
2686           return true;
2687         }
2688       return false;
2689
2690     case MULT:
2691       if (mode == SFmode)
2692         *total = mips_cost->fp_mult_sf;
2693
2694       else if (mode == DFmode)
2695         *total = mips_cost->fp_mult_df;
2696
2697       else if (mode == SImode)
2698         *total = mips_cost->int_mult_si;
2699
2700       else
2701         *total = mips_cost->int_mult_di;
2702
2703       return true;
2704
2705     case DIV:
2706     case MOD:
2707       if (float_mode_p)
2708         {
2709           if (mode == SFmode)
2710             *total = mips_cost->fp_div_sf;
2711           else
2712             *total = mips_cost->fp_div_df;
2713
2714           return true;
2715         }
2716       /* Fall through.  */
2717
2718     case UDIV:
2719     case UMOD:
2720       if (mode == DImode)
2721         *total = mips_cost->int_div_di;
2722       else
2723         *total = mips_cost->int_div_si;
2724
2725       return true;
2726
2727     case SIGN_EXTEND:
2728       /* A sign extend from SImode to DImode in 64 bit mode is often
2729          zero instructions, because the result can often be used
2730          directly by another instruction; we'll call it one.  */
2731       if (TARGET_64BIT && mode == DImode
2732           && GET_MODE (XEXP (x, 0)) == SImode)
2733         *total = COSTS_N_INSNS (1);
2734       else
2735         *total = COSTS_N_INSNS (2);
2736       return true;
2737
2738     case ZERO_EXTEND:
2739       if (TARGET_64BIT && mode == DImode
2740           && GET_MODE (XEXP (x, 0)) == SImode)
2741         *total = COSTS_N_INSNS (2);
2742       else
2743         *total = COSTS_N_INSNS (1);
2744       return true;
2745
2746     case FLOAT:
2747     case UNSIGNED_FLOAT:
2748     case FIX:
2749     case FLOAT_EXTEND:
2750     case FLOAT_TRUNCATE:
2751     case SQRT:
2752       *total = mips_cost->fp_add;
2753       return true;
2754
2755     default:
2756       return false;
2757     }
2758 }
2759
2760 /* Provide the costs of an addressing mode that contains ADDR.
2761    If ADDR is not a valid address, its cost is irrelevant.  */
2762
2763 static int
2764 mips_address_cost (rtx addr)
2765 {
2766   return mips_address_insns (addr, SImode);
2767 }
2768 \f
2769 /* Return one word of double-word value OP, taking into account the fixed
2770    endianness of certain registers.  HIGH_P is true to select the high part,
2771    false to select the low part.  */
2772
2773 rtx
2774 mips_subword (rtx op, int high_p)
2775 {
2776   unsigned int byte;
2777   enum machine_mode mode;
2778
2779   mode = GET_MODE (op);
2780   if (mode == VOIDmode)
2781     mode = DImode;
2782
2783   if (TARGET_BIG_ENDIAN ? !high_p : high_p)
2784     byte = UNITS_PER_WORD;
2785   else
2786     byte = 0;
2787
2788   if (REG_P (op))
2789     {
2790       if (FP_REG_P (REGNO (op)))
2791         return gen_rtx_REG (word_mode, high_p ? REGNO (op) + 1 : REGNO (op));
2792       if (ACC_HI_REG_P (REGNO (op)))
2793         return gen_rtx_REG (word_mode, high_p ? REGNO (op) : REGNO (op) + 1);
2794     }
2795
2796   if (MEM_P (op))
2797     return mips_rewrite_small_data (adjust_address (op, word_mode, byte));
2798
2799   return simplify_gen_subreg (word_mode, op, mode, byte);
2800 }
2801
2802
2803 /* Return true if a 64-bit move from SRC to DEST should be split into two.  */
2804
2805 bool
2806 mips_split_64bit_move_p (rtx dest, rtx src)
2807 {
2808   if (TARGET_64BIT)
2809     return false;
2810
2811   /* FP->FP moves can be done in a single instruction.  */
2812   if (FP_REG_RTX_P (src) && FP_REG_RTX_P (dest))
2813     return false;
2814
2815   /* Check for floating-point loads and stores.  They can be done using
2816      ldc1 and sdc1 on MIPS II and above.  */
2817   if (mips_isa > 1)
2818     {
2819       if (FP_REG_RTX_P (dest) && MEM_P (src))
2820         return false;
2821       if (FP_REG_RTX_P (src) && MEM_P (dest))
2822         return false;
2823     }
2824   return true;
2825 }
2826
2827
2828 /* Split a 64-bit move from SRC to DEST assuming that
2829    mips_split_64bit_move_p holds.
2830
2831    Moves into and out of FPRs cause some difficulty here.  Such moves
2832    will always be DFmode, since paired FPRs are not allowed to store
2833    DImode values.  The most natural representation would be two separate
2834    32-bit moves, such as:
2835
2836         (set (reg:SI $f0) (mem:SI ...))
2837         (set (reg:SI $f1) (mem:SI ...))
2838
2839    However, the second insn is invalid because odd-numbered FPRs are
2840    not allowed to store independent values.  Use the patterns load_df_low,
2841    load_df_high and store_df_high instead.  */
2842
2843 void
2844 mips_split_64bit_move (rtx dest, rtx src)
2845 {
2846   if (FP_REG_RTX_P (dest))
2847     {
2848       /* Loading an FPR from memory or from GPRs.  */
2849       emit_insn (gen_load_df_low (copy_rtx (dest), mips_subword (src, 0)));
2850       emit_insn (gen_load_df_high (dest, mips_subword (src, 1),
2851                                    copy_rtx (dest)));
2852     }
2853   else if (FP_REG_RTX_P (src))
2854     {
2855       /* Storing an FPR into memory or GPRs.  */
2856       emit_move_insn (mips_subword (dest, 0), mips_subword (src, 0));
2857       emit_insn (gen_store_df_high (mips_subword (dest, 1), src));
2858     }
2859   else
2860     {
2861       /* The operation can be split into two normal moves.  Decide in
2862          which order to do them.  */
2863       rtx low_dest;
2864
2865       low_dest = mips_subword (dest, 0);
2866       if (REG_P (low_dest)
2867           && reg_overlap_mentioned_p (low_dest, src))
2868         {
2869           emit_move_insn (mips_subword (dest, 1), mips_subword (src, 1));
2870           emit_move_insn (low_dest, mips_subword (src, 0));
2871         }
2872       else
2873         {
2874           emit_move_insn (low_dest, mips_subword (src, 0));
2875           emit_move_insn (mips_subword (dest, 1), mips_subword (src, 1));
2876         }
2877     }
2878 }
2879 \f
2880 /* Return the appropriate instructions to move SRC into DEST.  Assume
2881    that SRC is operand 1 and DEST is operand 0.  */
2882
2883 const char *
2884 mips_output_move (rtx dest, rtx src)
2885 {
2886   enum rtx_code dest_code, src_code;
2887   bool dbl_p;
2888
2889   dest_code = GET_CODE (dest);
2890   src_code = GET_CODE (src);
2891   dbl_p = (GET_MODE_SIZE (GET_MODE (dest)) == 8);
2892
2893   if (dbl_p && mips_split_64bit_move_p (dest, src))
2894     return "#";
2895
2896   if ((src_code == REG && GP_REG_P (REGNO (src)))
2897       || (!TARGET_MIPS16 && src == CONST0_RTX (GET_MODE (dest))))
2898     {
2899       if (dest_code == REG)
2900         {
2901           if (GP_REG_P (REGNO (dest)))
2902             return "move\t%0,%z1";
2903
2904           if (MD_REG_P (REGNO (dest)))
2905             return "mt%0\t%z1";
2906
2907           if (DSP_ACC_REG_P (REGNO (dest)))
2908             {
2909               static char retval[] = "mt__\t%z1,%q0";
2910               retval[2] = reg_names[REGNO (dest)][4];
2911               retval[3] = reg_names[REGNO (dest)][5];
2912               return retval;
2913             }
2914
2915           if (FP_REG_P (REGNO (dest)))
2916             return (dbl_p ? "dmtc1\t%z1,%0" : "mtc1\t%z1,%0");
2917
2918           if (ALL_COP_REG_P (REGNO (dest)))
2919             {
2920               static char retval[] = "dmtc_\t%z1,%0";
2921
2922               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
2923               return (dbl_p ? retval : retval + 1);
2924             }
2925         }
2926       if (dest_code == MEM)
2927         return (dbl_p ? "sd\t%z1,%0" : "sw\t%z1,%0");
2928     }
2929   if (dest_code == REG && GP_REG_P (REGNO (dest)))
2930     {
2931       if (src_code == REG)
2932         {
2933           if (DSP_ACC_REG_P (REGNO (src)))
2934             {
2935               static char retval[] = "mf__\t%0,%q1";
2936               retval[2] = reg_names[REGNO (src)][4];
2937               retval[3] = reg_names[REGNO (src)][5];
2938               return retval;
2939             }
2940
2941           if (ST_REG_P (REGNO (src)) && ISA_HAS_8CC)
2942             return "lui\t%0,0x3f80\n\tmovf\t%0,%.,%1";
2943
2944           if (FP_REG_P (REGNO (src)))
2945             return (dbl_p ? "dmfc1\t%0,%1" : "mfc1\t%0,%1");
2946
2947           if (ALL_COP_REG_P (REGNO (src)))
2948             {
2949               static char retval[] = "dmfc_\t%0,%1";
2950
2951               retval[4] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
2952               return (dbl_p ? retval : retval + 1);
2953             }
2954         }
2955
2956       if (src_code == MEM)
2957         return (dbl_p ? "ld\t%0,%1" : "lw\t%0,%1");
2958
2959       if (src_code == CONST_INT)
2960         {
2961           /* Don't use the X format, because that will give out of
2962              range numbers for 64 bit hosts and 32 bit targets.  */
2963           if (!TARGET_MIPS16)
2964             return "li\t%0,%1\t\t\t# %X1";
2965
2966           if (INTVAL (src) >= 0 && INTVAL (src) <= 0xffff)
2967             return "li\t%0,%1";
2968
2969           if (INTVAL (src) < 0 && INTVAL (src) >= -0xffff)
2970             return "#";
2971         }
2972
2973       if (src_code == HIGH)
2974         return "lui\t%0,%h1";
2975
2976       if (CONST_GP_P (src))
2977         return "move\t%0,%1";
2978
2979       if (symbolic_operand (src, VOIDmode))
2980         return (dbl_p ? "dla\t%0,%1" : "la\t%0,%1");
2981     }
2982   if (src_code == REG && FP_REG_P (REGNO (src)))
2983     {
2984       if (dest_code == REG && FP_REG_P (REGNO (dest)))
2985         {
2986           if (GET_MODE (dest) == V2SFmode)
2987             return "mov.ps\t%0,%1";
2988           else
2989             return (dbl_p ? "mov.d\t%0,%1" : "mov.s\t%0,%1");
2990         }
2991
2992       if (dest_code == MEM)
2993         return (dbl_p ? "sdc1\t%1,%0" : "swc1\t%1,%0");
2994     }
2995   if (dest_code == REG && FP_REG_P (REGNO (dest)))
2996     {
2997       if (src_code == MEM)
2998         return (dbl_p ? "ldc1\t%0,%1" : "lwc1\t%0,%1");
2999     }
3000   if (dest_code == REG && ALL_COP_REG_P (REGNO (dest)) && src_code == MEM)
3001     {
3002       static char retval[] = "l_c_\t%0,%1";
3003
3004       retval[1] = (dbl_p ? 'd' : 'w');
3005       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (dest));
3006       return retval;
3007     }
3008   if (dest_code == MEM && src_code == REG && ALL_COP_REG_P (REGNO (src)))
3009     {
3010       static char retval[] = "s_c_\t%1,%0";
3011
3012       retval[1] = (dbl_p ? 'd' : 'w');
3013       retval[3] = COPNUM_AS_CHAR_FROM_REGNUM (REGNO (src));
3014       return retval;
3015     }
3016   gcc_unreachable ();
3017 }
3018 \f
3019 /* Restore $gp from its save slot.  Valid only when using o32 or
3020    o64 abicalls.  */
3021
3022 void
3023 mips_restore_gp (void)
3024 {
3025   rtx address, slot;
3026
3027   gcc_assert (TARGET_ABICALLS && TARGET_OLDABI);
3028
3029   address = mips_add_offset (pic_offset_table_rtx,
3030                              frame_pointer_needed
3031                              ? hard_frame_pointer_rtx
3032                              : stack_pointer_rtx,
3033                              current_function_outgoing_args_size);
3034   slot = gen_rtx_MEM (Pmode, address);
3035
3036   emit_move_insn (pic_offset_table_rtx, slot);
3037   if (!TARGET_EXPLICIT_RELOCS)
3038     emit_insn (gen_blockage ());
3039 }
3040 \f
3041 /* Emit an instruction of the form (set TARGET (CODE OP0 OP1)).  */
3042
3043 static void
3044 mips_emit_binary (enum rtx_code code, rtx target, rtx op0, rtx op1)
3045 {
3046   emit_insn (gen_rtx_SET (VOIDmode, target,
3047                           gen_rtx_fmt_ee (code, GET_MODE (target), op0, op1)));
3048 }
3049
3050 /* Return true if CMP1 is a suitable second operand for relational
3051    operator CODE.  See also the *sCC patterns in mips.md.  */
3052
3053 static bool
3054 mips_relational_operand_ok_p (enum rtx_code code, rtx cmp1)
3055 {
3056   switch (code)
3057     {
3058     case GT:
3059     case GTU:
3060       return reg_or_0_operand (cmp1, VOIDmode);
3061
3062     case GE:
3063     case GEU:
3064       return !TARGET_MIPS16 && cmp1 == const1_rtx;
3065
3066     case LT:
3067     case LTU:
3068       return arith_operand (cmp1, VOIDmode);
3069
3070     case LE:
3071       return sle_operand (cmp1, VOIDmode);
3072
3073     case LEU:
3074       return sleu_operand (cmp1, VOIDmode);
3075
3076     default:
3077       gcc_unreachable ();
3078     }
3079 }
3080
3081 /* Canonicalize LE or LEU comparisons into LT comparisons when
3082    possible to avoid extra instructions or inverting the
3083    comparison.  */
3084
3085 static bool
3086 mips_canonicalize_comparison (enum rtx_code *code, rtx *cmp1, 
3087                               enum machine_mode mode)
3088 {
3089   HOST_WIDE_INT original, plus_one;
3090
3091   if (GET_CODE (*cmp1) != CONST_INT)
3092     return false;
3093   
3094   original = INTVAL (*cmp1);
3095   plus_one = trunc_int_for_mode ((unsigned HOST_WIDE_INT) original + 1, mode);
3096   
3097   switch (*code)
3098     {
3099     case LE:
3100       if (original < plus_one)
3101         {
3102           *code = LT;
3103           *cmp1 = force_reg (mode, GEN_INT (plus_one));
3104           return true;
3105         }
3106       break;
3107       
3108     case LEU:
3109       if (plus_one != 0)
3110         {
3111           *code = LTU;
3112           *cmp1 = force_reg (mode, GEN_INT (plus_one));
3113           return true;
3114         }
3115       break;
3116       
3117     default:
3118       return false;
3119    }
3120   
3121   return false;
3122
3123 }
3124
3125 /* Compare CMP0 and CMP1 using relational operator CODE and store the
3126    result in TARGET.  CMP0 and TARGET are register_operands that have
3127    the same integer mode.  If INVERT_PTR is nonnull, it's OK to set
3128    TARGET to the inverse of the result and flip *INVERT_PTR instead.  */
3129
3130 static void
3131 mips_emit_int_relational (enum rtx_code code, bool *invert_ptr,
3132                           rtx target, rtx cmp0, rtx cmp1)
3133 {
3134   /* First see if there is a MIPS instruction that can do this operation
3135      with CMP1 in its current form. If not, try to canonicalize the
3136      comparison to LT. If that fails, try doing the same for the
3137      inverse operation.  If that also fails, force CMP1 into a register
3138      and try again.  */
3139   if (mips_relational_operand_ok_p (code, cmp1))
3140     mips_emit_binary (code, target, cmp0, cmp1);
3141   else if (mips_canonicalize_comparison (&code, &cmp1, GET_MODE (target)))
3142     mips_emit_binary (code, target, cmp0, cmp1);
3143   else
3144     {
3145       enum rtx_code inv_code = reverse_condition (code);
3146       if (!mips_relational_operand_ok_p (inv_code, cmp1))
3147         {
3148           cmp1 = force_reg (GET_MODE (cmp0), cmp1);
3149           mips_emit_int_relational (code, invert_ptr, target, cmp0, cmp1);
3150         }
3151       else if (invert_ptr == 0)
3152         {
3153           rtx inv_target = gen_reg_rtx (GET_MODE (target));
3154           mips_emit_binary (inv_code, inv_target, cmp0, cmp1);
3155           mips_emit_binary (XOR, target, inv_target, const1_rtx);
3156         }
3157       else
3158         {
3159           *invert_ptr = !*invert_ptr;
3160           mips_emit_binary (inv_code, target, cmp0, cmp1);
3161         }
3162     }
3163 }
3164
3165 /* Return a register that is zero iff CMP0 and CMP1 are equal.
3166    The register will have the same mode as CMP0.  */
3167
3168 static rtx
3169 mips_zero_if_equal (rtx cmp0, rtx cmp1)
3170 {
3171   if (cmp1 == const0_rtx)
3172     return cmp0;
3173
3174   if (uns_arith_operand (cmp1, VOIDmode))
3175     return expand_binop (GET_MODE (cmp0), xor_optab,
3176                          cmp0, cmp1, 0, 0, OPTAB_DIRECT);
3177
3178   return expand_binop (GET_MODE (cmp0), sub_optab,
3179                        cmp0, cmp1, 0, 0, OPTAB_DIRECT);
3180 }
3181
3182 /* Convert *CODE into a code that can be used in a floating-point
3183    scc instruction (c.<cond>.<fmt>).  Return true if the values of
3184    the condition code registers will be inverted, with 0 indicating
3185    that the condition holds.  */
3186
3187 static bool
3188 mips_reverse_fp_cond_p (enum rtx_code *code)
3189 {
3190   switch (*code)
3191     {
3192     case NE:
3193     case LTGT:
3194     case ORDERED:
3195       *code = reverse_condition_maybe_unordered (*code);
3196       return true;
3197
3198     default:
3199       return false;
3200     }
3201 }
3202
3203 /* Convert a comparison into something that can be used in a branch or
3204    conditional move.  cmp_operands[0] and cmp_operands[1] are the values
3205    being compared and *CODE is the code used to compare them.
3206
3207    Update *CODE, *OP0 and *OP1 so that they describe the final comparison.
3208    If NEED_EQ_NE_P, then only EQ/NE comparisons against zero are possible,
3209    otherwise any standard branch condition can be used.  The standard branch
3210    conditions are:
3211
3212       - EQ/NE between two registers.
3213       - any comparison between a register and zero.  */
3214
3215 static void
3216 mips_emit_compare (enum rtx_code *code, rtx *op0, rtx *op1, bool need_eq_ne_p)
3217 {
3218   if (GET_MODE_CLASS (GET_MODE (cmp_operands[0])) == MODE_INT)
3219     {
3220       if (!need_eq_ne_p && cmp_operands[1] == const0_rtx)
3221         {
3222           *op0 = cmp_operands[0];
3223           *op1 = cmp_operands[1];
3224         }
3225       else if (*code == EQ || *code == NE)
3226         {
3227           if (need_eq_ne_p)
3228             {
3229               *op0 = mips_zero_if_equal (cmp_operands[0], cmp_operands[1]);
3230               *op1 = const0_rtx;
3231             }
3232           else
3233             {
3234               *op0 = cmp_operands[0];
3235               *op1 = force_reg (GET_MODE (*op0), cmp_operands[1]);
3236             }
3237         }
3238       else
3239         {
3240           /* The comparison needs a separate scc instruction.  Store the
3241              result of the scc in *OP0 and compare it against zero.  */
3242           bool invert = false;
3243           *op0 = gen_reg_rtx (GET_MODE (cmp_operands[0]));
3244           *op1 = const0_rtx;
3245           mips_emit_int_relational (*code, &invert, *op0,
3246                                     cmp_operands[0], cmp_operands[1]);
3247           *code = (invert ? EQ : NE);
3248         }
3249     }
3250   else
3251     {
3252       enum rtx_code cmp_code;
3253
3254       /* Floating-point tests use a separate c.cond.fmt comparison to
3255          set a condition code register.  The branch or conditional move
3256          will then compare that register against zero.
3257
3258          Set CMP_CODE to the code of the comparison instruction and
3259          *CODE to the code that the branch or move should use.  */
3260       cmp_code = *code;
3261       *code = mips_reverse_fp_cond_p (&cmp_code) ? EQ : NE;
3262       *op0 = (ISA_HAS_8CC
3263               ? gen_reg_rtx (CCmode)
3264               : gen_rtx_REG (CCmode, FPSW_REGNUM));
3265       *op1 = const0_rtx;
3266       mips_emit_binary (cmp_code, *op0, cmp_operands[0], cmp_operands[1]);
3267     }
3268 }
3269 \f
3270 /* Try comparing cmp_operands[0] and cmp_operands[1] using rtl code CODE.
3271    Store the result in TARGET and return true if successful.
3272
3273    On 64-bit targets, TARGET may be wider than cmp_operands[0].  */
3274
3275 bool
3276 mips_emit_scc (enum rtx_code code, rtx target)
3277 {
3278   if (GET_MODE_CLASS (GET_MODE (cmp_operands[0])) != MODE_INT)
3279     return false;
3280
3281   target = gen_lowpart (GET_MODE (cmp_operands[0]), target);
3282   if (code == EQ || code == NE)
3283     {
3284       rtx zie = mips_zero_if_equal (cmp_operands[0], cmp_operands[1]);
3285       mips_emit_binary (code, target, zie, const0_rtx);
3286     }
3287   else
3288     mips_emit_int_relational (code, 0, target,
3289                               cmp_operands[0], cmp_operands[1]);
3290   return true;
3291 }
3292
3293 /* Emit the common code for doing conditional branches.
3294    operand[0] is the label to jump to.
3295    The comparison operands are saved away by cmp{si,di,sf,df}.  */
3296
3297 void
3298 gen_conditional_branch (rtx *operands, enum rtx_code code)
3299 {
3300   rtx op0, op1, condition;
3301
3302   mips_emit_compare (&code, &op0, &op1, TARGET_MIPS16);
3303   condition = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
3304   emit_jump_insn (gen_condjump (condition, operands[0]));
3305 }
3306
3307 /* Implement:
3308
3309    (set temp (COND:CCV2 CMP_OP0 CMP_OP1))
3310    (set DEST (unspec [TRUE_SRC FALSE_SRC temp] UNSPEC_MOVE_TF_PS))  */
3311
3312 void
3313 mips_expand_vcondv2sf (rtx dest, rtx true_src, rtx false_src,
3314                        enum rtx_code cond, rtx cmp_op0, rtx cmp_op1)
3315 {
3316   rtx cmp_result;
3317   bool reversed_p;
3318
3319   reversed_p = mips_reverse_fp_cond_p (&cond);
3320   cmp_result = gen_reg_rtx (CCV2mode);
3321   emit_insn (gen_scc_ps (cmp_result,
3322                          gen_rtx_fmt_ee (cond, VOIDmode, cmp_op0, cmp_op1)));
3323   if (reversed_p)
3324     emit_insn (gen_mips_cond_move_tf_ps (dest, false_src, true_src,
3325                                          cmp_result));
3326   else
3327     emit_insn (gen_mips_cond_move_tf_ps (dest, true_src, false_src,
3328                                          cmp_result));
3329 }
3330
3331 /* Emit the common code for conditional moves.  OPERANDS is the array
3332    of operands passed to the conditional move define_expand.  */
3333
3334 void
3335 gen_conditional_move (rtx *operands)
3336 {
3337   enum rtx_code code;
3338   rtx op0, op1;
3339
3340   code = GET_CODE (operands[1]);
3341   mips_emit_compare (&code, &op0, &op1, true);
3342   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
3343                           gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
3344                                                 gen_rtx_fmt_ee (code,
3345                                                                 GET_MODE (op0),
3346                                                                 op0, op1),
3347                                                 operands[2], operands[3])));
3348 }
3349
3350 /* Emit a conditional trap.  OPERANDS is the array of operands passed to
3351    the conditional_trap expander.  */
3352
3353 void
3354 mips_gen_conditional_trap (rtx *operands)
3355 {
3356   rtx op0, op1;
3357   enum rtx_code cmp_code = GET_CODE (operands[0]);
3358   enum machine_mode mode = GET_MODE (cmp_operands[0]);
3359
3360   /* MIPS conditional trap machine instructions don't have GT or LE
3361      flavors, so we must invert the comparison and convert to LT and
3362      GE, respectively.  */
3363   switch (cmp_code)
3364     {
3365     case GT: cmp_code = LT; break;
3366     case LE: cmp_code = GE; break;
3367     case GTU: cmp_code = LTU; break;
3368     case LEU: cmp_code = GEU; break;
3369     default: break;
3370     }
3371   if (cmp_code == GET_CODE (operands[0]))
3372     {
3373       op0 = cmp_operands[0];
3374       op1 = cmp_operands[1];
3375     }
3376   else
3377     {
3378       op0 = cmp_operands[1];
3379       op1 = cmp_operands[0];
3380     }
3381   op0 = force_reg (mode, op0);
3382   if (!arith_operand (op1, mode))
3383     op1 = force_reg (mode, op1);
3384
3385   emit_insn (gen_rtx_TRAP_IF (VOIDmode,
3386                               gen_rtx_fmt_ee (cmp_code, mode, op0, op1),
3387                               operands[1]));
3388 }
3389 \f
3390 /* Load function address ADDR into register DEST.  SIBCALL_P is true
3391    if the address is needed for a sibling call.  */
3392
3393 static void
3394 mips_load_call_address (rtx dest, rtx addr, int sibcall_p)
3395 {
3396   /* If we're generating PIC, and this call is to a global function,
3397      try to allow its address to be resolved lazily.  This isn't
3398      possible for NewABI sibcalls since the value of $gp on entry
3399      to the stub would be our caller's gp, not ours.  */
3400   if (TARGET_EXPLICIT_RELOCS
3401       && !(sibcall_p && TARGET_NEWABI)
3402       && global_got_operand (addr, VOIDmode))
3403     {
3404       rtx high, lo_sum_symbol;
3405
3406       high = mips_unspec_offset_high (dest, pic_offset_table_rtx,
3407                                       addr, SYMBOL_GOTOFF_CALL);
3408       lo_sum_symbol = mips_unspec_address (addr, SYMBOL_GOTOFF_CALL);
3409       if (Pmode == SImode)
3410         emit_insn (gen_load_callsi (dest, high, lo_sum_symbol));
3411       else
3412         emit_insn (gen_load_calldi (dest, high, lo_sum_symbol));
3413     }
3414   else
3415     emit_move_insn (dest, addr);
3416 }
3417
3418
3419 /* Expand a call or call_value instruction.  RESULT is where the
3420    result will go (null for calls), ADDR is the address of the
3421    function, ARGS_SIZE is the size of the arguments and AUX is
3422    the value passed to us by mips_function_arg.  SIBCALL_P is true
3423    if we are expanding a sibling call, false if we're expanding
3424    a normal call.  */
3425
3426 void
3427 mips_expand_call (rtx result, rtx addr, rtx args_size, rtx aux, int sibcall_p)
3428 {
3429   rtx orig_addr, pattern, insn;
3430
3431   orig_addr = addr;
3432   if (!call_insn_operand (addr, VOIDmode))
3433     {
3434       addr = gen_reg_rtx (Pmode);
3435       mips_load_call_address (addr, orig_addr, sibcall_p);
3436     }
3437
3438   if (TARGET_MIPS16
3439       && mips16_hard_float
3440       && build_mips16_call_stub (result, addr, args_size,
3441                                  aux == 0 ? 0 : (int) GET_MODE (aux)))
3442     return;
3443
3444   if (result == 0)
3445     pattern = (sibcall_p
3446                ? gen_sibcall_internal (addr, args_size)
3447                : gen_call_internal (addr, args_size));
3448   else if (GET_CODE (result) == PARALLEL && XVECLEN (result, 0) == 2)
3449     {
3450       rtx reg1, reg2;
3451
3452       reg1 = XEXP (XVECEXP (result, 0, 0), 0);
3453       reg2 = XEXP (XVECEXP (result, 0, 1), 0);
3454       pattern =
3455         (sibcall_p
3456          ? gen_sibcall_value_multiple_internal (reg1, addr, args_size, reg2)
3457          : gen_call_value_multiple_internal (reg1, addr, args_size, reg2));
3458     }
3459   else
3460     pattern = (sibcall_p
3461                ? gen_sibcall_value_internal (result, addr, args_size)
3462                : gen_call_value_internal (result, addr, args_size));
3463
3464   insn&