OSDN Git Service

* doc/tm.texi.in (US_SOFTWARE_GOFAST): Don't document.
[pf3gnuchains/gcc-fork.git] / gcc / config / sparc / sparc.c
1 /* Subroutines for insn-output.c for SPARC.
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by Michael Tiemann (tiemann@cygnus.com)
6    64-bit SPARC-V9 support by Michael Tiemann, Jim Wilson, and Doug Evans,
7    at Cygnus Support.
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 3, 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 COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "rtl.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "insn-config.h"
34 #include "insn-codes.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "except.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "recog.h"
44 #include "diagnostic-core.h"
45 #include "ggc.h"
46 #include "tm_p.h"
47 #include "debug.h"
48 #include "target.h"
49 #include "target-def.h"
50 #include "cfglayout.h"
51 #include "gimple.h"
52 #include "langhooks.h"
53 #include "reload.h"
54 #include "params.h"
55 #include "df.h"
56 #include "dwarf2out.h"
57
58 /* Processor costs */
59 static const
60 struct processor_costs cypress_costs = {
61   COSTS_N_INSNS (2), /* int load */
62   COSTS_N_INSNS (2), /* int signed load */
63   COSTS_N_INSNS (2), /* int zeroed load */
64   COSTS_N_INSNS (2), /* float load */
65   COSTS_N_INSNS (5), /* fmov, fneg, fabs */
66   COSTS_N_INSNS (5), /* fadd, fsub */
67   COSTS_N_INSNS (1), /* fcmp */
68   COSTS_N_INSNS (1), /* fmov, fmovr */
69   COSTS_N_INSNS (7), /* fmul */
70   COSTS_N_INSNS (37), /* fdivs */
71   COSTS_N_INSNS (37), /* fdivd */
72   COSTS_N_INSNS (63), /* fsqrts */
73   COSTS_N_INSNS (63), /* fsqrtd */
74   COSTS_N_INSNS (1), /* imul */
75   COSTS_N_INSNS (1), /* imulX */
76   0, /* imul bit factor */
77   COSTS_N_INSNS (1), /* idiv */
78   COSTS_N_INSNS (1), /* idivX */
79   COSTS_N_INSNS (1), /* movcc/movr */
80   0, /* shift penalty */
81 };
82
83 static const
84 struct processor_costs supersparc_costs = {
85   COSTS_N_INSNS (1), /* int load */
86   COSTS_N_INSNS (1), /* int signed load */
87   COSTS_N_INSNS (1), /* int zeroed load */
88   COSTS_N_INSNS (0), /* float load */
89   COSTS_N_INSNS (3), /* fmov, fneg, fabs */
90   COSTS_N_INSNS (3), /* fadd, fsub */
91   COSTS_N_INSNS (3), /* fcmp */
92   COSTS_N_INSNS (1), /* fmov, fmovr */
93   COSTS_N_INSNS (3), /* fmul */
94   COSTS_N_INSNS (6), /* fdivs */
95   COSTS_N_INSNS (9), /* fdivd */
96   COSTS_N_INSNS (12), /* fsqrts */
97   COSTS_N_INSNS (12), /* fsqrtd */
98   COSTS_N_INSNS (4), /* imul */
99   COSTS_N_INSNS (4), /* imulX */
100   0, /* imul bit factor */
101   COSTS_N_INSNS (4), /* idiv */
102   COSTS_N_INSNS (4), /* idivX */
103   COSTS_N_INSNS (1), /* movcc/movr */
104   1, /* shift penalty */
105 };
106
107 static const
108 struct processor_costs hypersparc_costs = {
109   COSTS_N_INSNS (1), /* int load */
110   COSTS_N_INSNS (1), /* int signed load */
111   COSTS_N_INSNS (1), /* int zeroed load */
112   COSTS_N_INSNS (1), /* float load */
113   COSTS_N_INSNS (1), /* fmov, fneg, fabs */
114   COSTS_N_INSNS (1), /* fadd, fsub */
115   COSTS_N_INSNS (1), /* fcmp */
116   COSTS_N_INSNS (1), /* fmov, fmovr */
117   COSTS_N_INSNS (1), /* fmul */
118   COSTS_N_INSNS (8), /* fdivs */
119   COSTS_N_INSNS (12), /* fdivd */
120   COSTS_N_INSNS (17), /* fsqrts */
121   COSTS_N_INSNS (17), /* fsqrtd */
122   COSTS_N_INSNS (17), /* imul */
123   COSTS_N_INSNS (17), /* imulX */
124   0, /* imul bit factor */
125   COSTS_N_INSNS (17), /* idiv */
126   COSTS_N_INSNS (17), /* idivX */
127   COSTS_N_INSNS (1), /* movcc/movr */
128   0, /* shift penalty */
129 };
130
131 static const
132 struct processor_costs sparclet_costs = {
133   COSTS_N_INSNS (3), /* int load */
134   COSTS_N_INSNS (3), /* int signed load */
135   COSTS_N_INSNS (1), /* int zeroed load */
136   COSTS_N_INSNS (1), /* float load */
137   COSTS_N_INSNS (1), /* fmov, fneg, fabs */
138   COSTS_N_INSNS (1), /* fadd, fsub */
139   COSTS_N_INSNS (1), /* fcmp */
140   COSTS_N_INSNS (1), /* fmov, fmovr */
141   COSTS_N_INSNS (1), /* fmul */
142   COSTS_N_INSNS (1), /* fdivs */
143   COSTS_N_INSNS (1), /* fdivd */
144   COSTS_N_INSNS (1), /* fsqrts */
145   COSTS_N_INSNS (1), /* fsqrtd */
146   COSTS_N_INSNS (5), /* imul */
147   COSTS_N_INSNS (5), /* imulX */
148   0, /* imul bit factor */
149   COSTS_N_INSNS (5), /* idiv */
150   COSTS_N_INSNS (5), /* idivX */
151   COSTS_N_INSNS (1), /* movcc/movr */
152   0, /* shift penalty */
153 };
154
155 static const
156 struct processor_costs ultrasparc_costs = {
157   COSTS_N_INSNS (2), /* int load */
158   COSTS_N_INSNS (3), /* int signed load */
159   COSTS_N_INSNS (2), /* int zeroed load */
160   COSTS_N_INSNS (2), /* float load */
161   COSTS_N_INSNS (1), /* fmov, fneg, fabs */
162   COSTS_N_INSNS (4), /* fadd, fsub */
163   COSTS_N_INSNS (1), /* fcmp */
164   COSTS_N_INSNS (2), /* fmov, fmovr */
165   COSTS_N_INSNS (4), /* fmul */
166   COSTS_N_INSNS (13), /* fdivs */
167   COSTS_N_INSNS (23), /* fdivd */
168   COSTS_N_INSNS (13), /* fsqrts */
169   COSTS_N_INSNS (23), /* fsqrtd */
170   COSTS_N_INSNS (4), /* imul */
171   COSTS_N_INSNS (4), /* imulX */
172   2, /* imul bit factor */
173   COSTS_N_INSNS (37), /* idiv */
174   COSTS_N_INSNS (68), /* idivX */
175   COSTS_N_INSNS (2), /* movcc/movr */
176   2, /* shift penalty */
177 };
178
179 static const
180 struct processor_costs ultrasparc3_costs = {
181   COSTS_N_INSNS (2), /* int load */
182   COSTS_N_INSNS (3), /* int signed load */
183   COSTS_N_INSNS (3), /* int zeroed load */
184   COSTS_N_INSNS (2), /* float load */
185   COSTS_N_INSNS (3), /* fmov, fneg, fabs */
186   COSTS_N_INSNS (4), /* fadd, fsub */
187   COSTS_N_INSNS (5), /* fcmp */
188   COSTS_N_INSNS (3), /* fmov, fmovr */
189   COSTS_N_INSNS (4), /* fmul */
190   COSTS_N_INSNS (17), /* fdivs */
191   COSTS_N_INSNS (20), /* fdivd */
192   COSTS_N_INSNS (20), /* fsqrts */
193   COSTS_N_INSNS (29), /* fsqrtd */
194   COSTS_N_INSNS (6), /* imul */
195   COSTS_N_INSNS (6), /* imulX */
196   0, /* imul bit factor */
197   COSTS_N_INSNS (40), /* idiv */
198   COSTS_N_INSNS (71), /* idivX */
199   COSTS_N_INSNS (2), /* movcc/movr */
200   0, /* shift penalty */
201 };
202
203 static const
204 struct processor_costs niagara_costs = {
205   COSTS_N_INSNS (3), /* int load */
206   COSTS_N_INSNS (3), /* int signed load */
207   COSTS_N_INSNS (3), /* int zeroed load */
208   COSTS_N_INSNS (9), /* float load */
209   COSTS_N_INSNS (8), /* fmov, fneg, fabs */
210   COSTS_N_INSNS (8), /* fadd, fsub */
211   COSTS_N_INSNS (26), /* fcmp */
212   COSTS_N_INSNS (8), /* fmov, fmovr */
213   COSTS_N_INSNS (29), /* fmul */
214   COSTS_N_INSNS (54), /* fdivs */
215   COSTS_N_INSNS (83), /* fdivd */
216   COSTS_N_INSNS (100), /* fsqrts - not implemented in hardware */
217   COSTS_N_INSNS (100), /* fsqrtd - not implemented in hardware */
218   COSTS_N_INSNS (11), /* imul */
219   COSTS_N_INSNS (11), /* imulX */
220   0, /* imul bit factor */
221   COSTS_N_INSNS (72), /* idiv */
222   COSTS_N_INSNS (72), /* idivX */
223   COSTS_N_INSNS (1), /* movcc/movr */
224   0, /* shift penalty */
225 };
226
227 static const
228 struct processor_costs niagara2_costs = {
229   COSTS_N_INSNS (3), /* int load */
230   COSTS_N_INSNS (3), /* int signed load */
231   COSTS_N_INSNS (3), /* int zeroed load */
232   COSTS_N_INSNS (3), /* float load */
233   COSTS_N_INSNS (6), /* fmov, fneg, fabs */
234   COSTS_N_INSNS (6), /* fadd, fsub */
235   COSTS_N_INSNS (6), /* fcmp */
236   COSTS_N_INSNS (6), /* fmov, fmovr */
237   COSTS_N_INSNS (6), /* fmul */
238   COSTS_N_INSNS (19), /* fdivs */
239   COSTS_N_INSNS (33), /* fdivd */
240   COSTS_N_INSNS (19), /* fsqrts */
241   COSTS_N_INSNS (33), /* fsqrtd */
242   COSTS_N_INSNS (5), /* imul */
243   COSTS_N_INSNS (5), /* imulX */
244   0, /* imul bit factor */
245   COSTS_N_INSNS (31), /* idiv, average of 12 - 41 cycle range */
246   COSTS_N_INSNS (31), /* idivX, average of 12 - 41 cycle range */
247   COSTS_N_INSNS (1), /* movcc/movr */
248   0, /* shift penalty */
249 };
250
251 const struct processor_costs *sparc_costs = &cypress_costs;
252
253 #ifdef HAVE_AS_RELAX_OPTION
254 /* If 'as' and 'ld' are relaxing tail call insns into branch always, use
255    "or %o7,%g0,X; call Y; or X,%g0,%o7" always, so that it can be optimized.
256    With sethi/jmp, neither 'as' nor 'ld' has an easy way how to find out if
257    somebody does not branch between the sethi and jmp.  */
258 #define LEAF_SIBCALL_SLOT_RESERVED_P 1
259 #else
260 #define LEAF_SIBCALL_SLOT_RESERVED_P \
261   ((TARGET_ARCH64 && !TARGET_CM_MEDLOW) || flag_pic)
262 #endif
263
264 /* Global variables for machine-dependent things.  */
265
266 /* Size of frame.  Need to know this to emit return insns from leaf procedures.
267    ACTUAL_FSIZE is set by sparc_compute_frame_size() which is called during the
268    reload pass.  This is important as the value is later used for scheduling
269    (to see what can go in a delay slot).
270    APPARENT_FSIZE is the size of the stack less the register save area and less
271    the outgoing argument area.  It is used when saving call preserved regs.  */
272 static HOST_WIDE_INT apparent_fsize;
273 static HOST_WIDE_INT actual_fsize;
274
275 /* Number of live general or floating point registers needed to be
276    saved (as 4-byte quantities).  */
277 static int num_gfregs;
278
279 /* The alias set for prologue/epilogue register save/restore.  */
280 static GTY(()) alias_set_type sparc_sr_alias_set;
281
282 /* The alias set for the structure return value.  */
283 static GTY(()) alias_set_type struct_value_alias_set;
284
285 /* Vector to say how input registers are mapped to output registers.
286    HARD_FRAME_POINTER_REGNUM cannot be remapped by this function to
287    eliminate it.  You must use -fomit-frame-pointer to get that.  */
288 char leaf_reg_remap[] =
289 { 0, 1, 2, 3, 4, 5, 6, 7,
290   -1, -1, -1, -1, -1, -1, 14, -1,
291   -1, -1, -1, -1, -1, -1, -1, -1,
292   8, 9, 10, 11, 12, 13, -1, 15,
293
294   32, 33, 34, 35, 36, 37, 38, 39,
295   40, 41, 42, 43, 44, 45, 46, 47,
296   48, 49, 50, 51, 52, 53, 54, 55,
297   56, 57, 58, 59, 60, 61, 62, 63,
298   64, 65, 66, 67, 68, 69, 70, 71,
299   72, 73, 74, 75, 76, 77, 78, 79,
300   80, 81, 82, 83, 84, 85, 86, 87,
301   88, 89, 90, 91, 92, 93, 94, 95,
302   96, 97, 98, 99, 100};
303
304 /* Vector, indexed by hard register number, which contains 1
305    for a register that is allowable in a candidate for leaf
306    function treatment.  */
307 char sparc_leaf_regs[] =
308 { 1, 1, 1, 1, 1, 1, 1, 1,
309   0, 0, 0, 0, 0, 0, 1, 0,
310   0, 0, 0, 0, 0, 0, 0, 0,
311   1, 1, 1, 1, 1, 1, 0, 1,
312   1, 1, 1, 1, 1, 1, 1, 1,
313   1, 1, 1, 1, 1, 1, 1, 1,
314   1, 1, 1, 1, 1, 1, 1, 1,
315   1, 1, 1, 1, 1, 1, 1, 1,
316   1, 1, 1, 1, 1, 1, 1, 1,
317   1, 1, 1, 1, 1, 1, 1, 1,
318   1, 1, 1, 1, 1, 1, 1, 1,
319   1, 1, 1, 1, 1, 1, 1, 1,
320   1, 1, 1, 1, 1};
321
322 struct GTY(()) machine_function
323 {
324   /* Some local-dynamic TLS symbol name.  */
325   const char *some_ld_name;
326
327   /* True if the current function is leaf and uses only leaf regs,
328      so that the SPARC leaf function optimization can be applied.
329      Private version of current_function_uses_only_leaf_regs, see
330      sparc_expand_prologue for the rationale.  */
331   int leaf_function_p;
332
333   /* True if the data calculated by sparc_expand_prologue are valid.  */
334   bool prologue_data_valid_p;
335 };
336
337 #define sparc_leaf_function_p  cfun->machine->leaf_function_p
338 #define sparc_prologue_data_valid_p  cfun->machine->prologue_data_valid_p
339
340 /* Register we pretend to think the frame pointer is allocated to.
341    Normally, this is %fp, but if we are in a leaf procedure, this
342    is %sp+"something".  We record "something" separately as it may
343    be too big for reg+constant addressing.  */
344 static rtx frame_base_reg;
345 static HOST_WIDE_INT frame_base_offset;
346
347 /* 1 if the next opcode is to be specially indented.  */
348 int sparc_indent_opcode = 0;
349
350 static bool sparc_handle_option (size_t, const char *, int);
351 static void sparc_option_override (void);
352 static void sparc_init_modes (void);
353 static void scan_record_type (const_tree, int *, int *, int *);
354 static int function_arg_slotno (const CUMULATIVE_ARGS *, enum machine_mode,
355                                 const_tree, bool, bool, int *, int *);
356
357 static int supersparc_adjust_cost (rtx, rtx, rtx, int);
358 static int hypersparc_adjust_cost (rtx, rtx, rtx, int);
359
360 static void sparc_emit_set_const32 (rtx, rtx);
361 static void sparc_emit_set_const64 (rtx, rtx);
362 static void sparc_output_addr_vec (rtx);
363 static void sparc_output_addr_diff_vec (rtx);
364 static void sparc_output_deferred_case_vectors (void);
365 static bool sparc_legitimate_address_p (enum machine_mode, rtx, bool);
366 static rtx sparc_builtin_saveregs (void);
367 static int epilogue_renumber (rtx *, int);
368 static bool sparc_assemble_integer (rtx, unsigned int, int);
369 static int set_extends (rtx);
370 static void load_pic_register (void);
371 static int save_or_restore_regs (int, int, rtx, int, int);
372 static void emit_save_or_restore_regs (int);
373 static void sparc_asm_function_prologue (FILE *, HOST_WIDE_INT);
374 static void sparc_asm_function_epilogue (FILE *, HOST_WIDE_INT);
375 static void sparc_solaris_elf_asm_named_section (const char *, unsigned int,
376                                                  tree) ATTRIBUTE_UNUSED;
377 static int sparc_adjust_cost (rtx, rtx, rtx, int);
378 static int sparc_issue_rate (void);
379 static void sparc_sched_init (FILE *, int, int);
380 static int sparc_use_sched_lookahead (void);
381
382 static void emit_soft_tfmode_libcall (const char *, int, rtx *);
383 static void emit_soft_tfmode_binop (enum rtx_code, rtx *);
384 static void emit_soft_tfmode_unop (enum rtx_code, rtx *);
385 static void emit_soft_tfmode_cvt (enum rtx_code, rtx *);
386 static void emit_hard_tfmode_operation (enum rtx_code, rtx *);
387
388 static bool sparc_function_ok_for_sibcall (tree, tree);
389 static void sparc_init_libfuncs (void);
390 static void sparc_init_builtins (void);
391 static void sparc_vis_init_builtins (void);
392 static rtx sparc_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
393 static tree sparc_fold_builtin (tree, int, tree *, bool);
394 static int sparc_vis_mul8x16 (int, int);
395 static tree sparc_handle_vis_mul8x16 (int, tree, tree, tree);
396 static void sparc_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
397                                    HOST_WIDE_INT, tree);
398 static bool sparc_can_output_mi_thunk (const_tree, HOST_WIDE_INT,
399                                        HOST_WIDE_INT, const_tree);
400 static struct machine_function * sparc_init_machine_status (void);
401 static bool sparc_cannot_force_const_mem (rtx);
402 static rtx sparc_tls_get_addr (void);
403 static rtx sparc_tls_got (void);
404 static const char *get_some_local_dynamic_name (void);
405 static int get_some_local_dynamic_name_1 (rtx *, void *);
406 static bool sparc_rtx_costs (rtx, int, int, int *, bool);
407 static rtx sparc_function_value (const_tree, const_tree, bool);
408 static rtx sparc_libcall_value (enum machine_mode, const_rtx);
409 static bool sparc_function_value_regno_p (const unsigned int);
410 static rtx sparc_struct_value_rtx (tree, int);
411 static enum machine_mode sparc_promote_function_mode (const_tree, enum machine_mode,
412                                                       int *, const_tree, int);
413 static bool sparc_return_in_memory (const_tree, const_tree);
414 static bool sparc_strict_argument_naming (CUMULATIVE_ARGS *);
415 static void sparc_va_start (tree, rtx);
416 static tree sparc_gimplify_va_arg (tree, tree, gimple_seq *, gimple_seq *);
417 static bool sparc_vector_mode_supported_p (enum machine_mode);
418 static bool sparc_tls_referenced_p (rtx);
419 static rtx sparc_legitimize_tls_address (rtx);
420 static rtx sparc_legitimize_pic_address (rtx, rtx);
421 static rtx sparc_legitimize_address (rtx, rtx, enum machine_mode);
422 static rtx sparc_delegitimize_address (rtx);
423 static bool sparc_mode_dependent_address_p (const_rtx);
424 static bool sparc_pass_by_reference (CUMULATIVE_ARGS *,
425                                      enum machine_mode, const_tree, bool);
426 static void sparc_function_arg_advance (CUMULATIVE_ARGS *,
427                                         enum machine_mode, const_tree, bool);
428 static rtx sparc_function_arg_1 (const CUMULATIVE_ARGS *,
429                                  enum machine_mode, const_tree, bool, bool);
430 static rtx sparc_function_arg (CUMULATIVE_ARGS *,
431                                enum machine_mode, const_tree, bool);
432 static rtx sparc_function_incoming_arg (CUMULATIVE_ARGS *,
433                                         enum machine_mode, const_tree, bool);
434 static unsigned int sparc_function_arg_boundary (enum machine_mode,
435                                                  const_tree);
436 static int sparc_arg_partial_bytes (CUMULATIVE_ARGS *,
437                                     enum machine_mode, tree, bool);
438 static void sparc_dwarf_handle_frame_unspec (const char *, rtx, int);
439 static void sparc_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
440 static void sparc_file_end (void);
441 static bool sparc_frame_pointer_required (void);
442 static bool sparc_can_eliminate (const int, const int);
443 static void sparc_conditional_register_usage (void);
444 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
445 static const char *sparc_mangle_type (const_tree);
446 #endif
447 static void sparc_trampoline_init (rtx, tree, rtx);
448 static enum machine_mode sparc_preferred_simd_mode (enum machine_mode);
449 \f
450 #ifdef SUBTARGET_ATTRIBUTE_TABLE
451 /* Table of valid machine attributes.  */
452 static const struct attribute_spec sparc_attribute_table[] =
453 {
454   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
455   SUBTARGET_ATTRIBUTE_TABLE,
456   { NULL,        0, 0, false, false, false, NULL }
457 };
458 #endif
459 \f
460 /* Option handling.  */
461
462 /* Parsed value.  */
463 enum cmodel sparc_cmodel;
464
465 char sparc_hard_reg_printed[8];
466
467 struct sparc_cpu_select sparc_select[] =
468 {
469   /* switch     name,           tune    arch */
470   { (char *)0,  "default",      1,      1 },
471   { (char *)0,  "-mcpu=",       1,      1 },
472   { (char *)0,  "-mtune=",      1,      0 },
473   { 0, 0, 0, 0 }
474 };
475
476 /* CPU type.  This is set from TARGET_CPU_DEFAULT and -m{cpu,tune}=xxx.  */
477 enum processor_type sparc_cpu;
478
479 /* Whether\fan FPU option was specified.  */
480 static bool fpu_option_set = false;
481
482 /* Implement TARGET_OPTION_OPTIMIZATION_TABLE.  */
483 static const struct default_options sparc_option_optimization_table[] =
484   {
485     { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
486     { OPT_LEVELS_NONE, 0, NULL, 0 }
487   };
488
489 /* Initialize the GCC target structure.  */
490
491 /* The default is to use .half rather than .short for aligned HI objects.  */
492 #undef TARGET_ASM_ALIGNED_HI_OP
493 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
494
495 #undef TARGET_ASM_UNALIGNED_HI_OP
496 #define TARGET_ASM_UNALIGNED_HI_OP "\t.uahalf\t"
497 #undef TARGET_ASM_UNALIGNED_SI_OP
498 #define TARGET_ASM_UNALIGNED_SI_OP "\t.uaword\t"
499 #undef TARGET_ASM_UNALIGNED_DI_OP
500 #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaxword\t"
501
502 /* The target hook has to handle DI-mode values.  */
503 #undef TARGET_ASM_INTEGER
504 #define TARGET_ASM_INTEGER sparc_assemble_integer
505
506 #undef TARGET_ASM_FUNCTION_PROLOGUE
507 #define TARGET_ASM_FUNCTION_PROLOGUE sparc_asm_function_prologue
508 #undef TARGET_ASM_FUNCTION_EPILOGUE
509 #define TARGET_ASM_FUNCTION_EPILOGUE sparc_asm_function_epilogue
510
511 #undef TARGET_SCHED_ADJUST_COST
512 #define TARGET_SCHED_ADJUST_COST sparc_adjust_cost
513 #undef TARGET_SCHED_ISSUE_RATE
514 #define TARGET_SCHED_ISSUE_RATE sparc_issue_rate
515 #undef TARGET_SCHED_INIT
516 #define TARGET_SCHED_INIT sparc_sched_init
517 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
518 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD sparc_use_sched_lookahead
519
520 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
521 #define TARGET_FUNCTION_OK_FOR_SIBCALL sparc_function_ok_for_sibcall
522
523 #undef TARGET_INIT_LIBFUNCS
524 #define TARGET_INIT_LIBFUNCS sparc_init_libfuncs
525 #undef TARGET_INIT_BUILTINS
526 #define TARGET_INIT_BUILTINS sparc_init_builtins
527
528 #undef TARGET_LEGITIMIZE_ADDRESS
529 #define TARGET_LEGITIMIZE_ADDRESS sparc_legitimize_address
530 #undef TARGET_DELEGITIMIZE_ADDRESS
531 #define TARGET_DELEGITIMIZE_ADDRESS sparc_delegitimize_address
532 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
533 #define TARGET_MODE_DEPENDENT_ADDRESS_P sparc_mode_dependent_address_p
534
535 #undef TARGET_EXPAND_BUILTIN
536 #define TARGET_EXPAND_BUILTIN sparc_expand_builtin
537 #undef TARGET_FOLD_BUILTIN
538 #define TARGET_FOLD_BUILTIN sparc_fold_builtin
539
540 #if TARGET_TLS
541 #undef TARGET_HAVE_TLS
542 #define TARGET_HAVE_TLS true
543 #endif
544
545 #undef TARGET_CANNOT_FORCE_CONST_MEM
546 #define TARGET_CANNOT_FORCE_CONST_MEM sparc_cannot_force_const_mem
547
548 #undef TARGET_ASM_OUTPUT_MI_THUNK
549 #define TARGET_ASM_OUTPUT_MI_THUNK sparc_output_mi_thunk
550 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
551 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK sparc_can_output_mi_thunk
552
553 #undef TARGET_RTX_COSTS
554 #define TARGET_RTX_COSTS sparc_rtx_costs
555 #undef TARGET_ADDRESS_COST
556 #define TARGET_ADDRESS_COST hook_int_rtx_bool_0
557
558 #undef TARGET_PROMOTE_FUNCTION_MODE
559 #define TARGET_PROMOTE_FUNCTION_MODE sparc_promote_function_mode
560
561 #undef TARGET_FUNCTION_VALUE
562 #define TARGET_FUNCTION_VALUE sparc_function_value
563 #undef TARGET_LIBCALL_VALUE
564 #define TARGET_LIBCALL_VALUE sparc_libcall_value
565 #undef TARGET_FUNCTION_VALUE_REGNO_P
566 #define TARGET_FUNCTION_VALUE_REGNO_P sparc_function_value_regno_p
567
568 #undef TARGET_STRUCT_VALUE_RTX
569 #define TARGET_STRUCT_VALUE_RTX sparc_struct_value_rtx
570 #undef TARGET_RETURN_IN_MEMORY
571 #define TARGET_RETURN_IN_MEMORY sparc_return_in_memory
572 #undef TARGET_MUST_PASS_IN_STACK
573 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
574 #undef TARGET_PASS_BY_REFERENCE
575 #define TARGET_PASS_BY_REFERENCE sparc_pass_by_reference
576 #undef TARGET_ARG_PARTIAL_BYTES
577 #define TARGET_ARG_PARTIAL_BYTES sparc_arg_partial_bytes
578 #undef TARGET_FUNCTION_ARG_ADVANCE
579 #define TARGET_FUNCTION_ARG_ADVANCE sparc_function_arg_advance
580 #undef TARGET_FUNCTION_ARG
581 #define TARGET_FUNCTION_ARG sparc_function_arg
582 #undef TARGET_FUNCTION_INCOMING_ARG
583 #define TARGET_FUNCTION_INCOMING_ARG sparc_function_incoming_arg
584 #undef TARGET_FUNCTION_ARG_BOUNDARY
585 #define TARGET_FUNCTION_ARG_BOUNDARY sparc_function_arg_boundary
586
587 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
588 #define TARGET_EXPAND_BUILTIN_SAVEREGS sparc_builtin_saveregs
589 #undef TARGET_STRICT_ARGUMENT_NAMING
590 #define TARGET_STRICT_ARGUMENT_NAMING sparc_strict_argument_naming
591
592 #undef TARGET_EXPAND_BUILTIN_VA_START
593 #define TARGET_EXPAND_BUILTIN_VA_START sparc_va_start
594 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
595 #define TARGET_GIMPLIFY_VA_ARG_EXPR sparc_gimplify_va_arg
596
597 #undef TARGET_VECTOR_MODE_SUPPORTED_P
598 #define TARGET_VECTOR_MODE_SUPPORTED_P sparc_vector_mode_supported_p
599
600 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
601 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE sparc_preferred_simd_mode
602
603 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
604 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC sparc_dwarf_handle_frame_unspec
605
606 #ifdef SUBTARGET_INSERT_ATTRIBUTES
607 #undef TARGET_INSERT_ATTRIBUTES
608 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
609 #endif
610
611 #ifdef SUBTARGET_ATTRIBUTE_TABLE
612 #undef TARGET_ATTRIBUTE_TABLE
613 #define TARGET_ATTRIBUTE_TABLE sparc_attribute_table
614 #endif
615
616 #undef TARGET_RELAXED_ORDERING
617 #define TARGET_RELAXED_ORDERING SPARC_RELAXED_ORDERING
618
619 #undef TARGET_DEFAULT_TARGET_FLAGS
620 #define TARGET_DEFAULT_TARGET_FLAGS TARGET_DEFAULT
621 #undef TARGET_HANDLE_OPTION
622 #define TARGET_HANDLE_OPTION sparc_handle_option
623 #undef TARGET_OPTION_OVERRIDE
624 #define TARGET_OPTION_OVERRIDE sparc_option_override
625 #undef TARGET_OPTION_OPTIMIZATION_TABLE
626 #define TARGET_OPTION_OPTIMIZATION_TABLE sparc_option_optimization_table
627
628 #if TARGET_GNU_TLS && defined(HAVE_AS_SPARC_UA_PCREL)
629 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
630 #define TARGET_ASM_OUTPUT_DWARF_DTPREL sparc_output_dwarf_dtprel
631 #endif
632
633 #undef TARGET_ASM_FILE_END
634 #define TARGET_ASM_FILE_END sparc_file_end
635
636 #undef TARGET_FRAME_POINTER_REQUIRED
637 #define TARGET_FRAME_POINTER_REQUIRED sparc_frame_pointer_required
638
639 #undef TARGET_CAN_ELIMINATE
640 #define TARGET_CAN_ELIMINATE sparc_can_eliminate
641
642 #undef TARGET_CONDITIONAL_REGISTER_USAGE
643 #define TARGET_CONDITIONAL_REGISTER_USAGE sparc_conditional_register_usage
644
645 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
646 #undef TARGET_MANGLE_TYPE
647 #define TARGET_MANGLE_TYPE sparc_mangle_type
648 #endif
649
650 #undef TARGET_LEGITIMATE_ADDRESS_P
651 #define TARGET_LEGITIMATE_ADDRESS_P sparc_legitimate_address_p
652
653 #undef TARGET_TRAMPOLINE_INIT
654 #define TARGET_TRAMPOLINE_INIT sparc_trampoline_init
655
656 struct gcc_target targetm = TARGET_INITIALIZER;
657
658 /* Implement TARGET_HANDLE_OPTION.  */
659
660 static bool
661 sparc_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
662 {
663   switch (code)
664     {
665     case OPT_mfpu:
666     case OPT_mhard_float:
667     case OPT_msoft_float:
668       fpu_option_set = true;
669       break;
670
671     case OPT_mcpu_:
672       sparc_select[1].string = arg;
673       break;
674
675     case OPT_mtune_:
676       sparc_select[2].string = arg;
677       break;
678     }
679
680   return true;
681 }
682
683 /* Validate and override various options, and do some machine dependent
684    initialization.  */
685
686 static void
687 sparc_option_override (void)
688 {
689   static struct code_model {
690     const char *const name;
691     const enum cmodel value;
692   } const cmodels[] = {
693     { "32", CM_32 },
694     { "medlow", CM_MEDLOW },
695     { "medmid", CM_MEDMID },
696     { "medany", CM_MEDANY },
697     { "embmedany", CM_EMBMEDANY },
698     { NULL, (enum cmodel) 0 }
699   };
700   const struct code_model *cmodel;
701   /* Map TARGET_CPU_DEFAULT to value for -m{arch,tune}=.  */
702   static struct cpu_default {
703     const int cpu;
704     const char *const name;
705   } const cpu_default[] = {
706     /* There must be one entry here for each TARGET_CPU value.  */
707     { TARGET_CPU_sparc, "cypress" },
708     { TARGET_CPU_sparclet, "tsc701" },
709     { TARGET_CPU_sparclite, "f930" },
710     { TARGET_CPU_v8, "v8" },
711     { TARGET_CPU_hypersparc, "hypersparc" },
712     { TARGET_CPU_sparclite86x, "sparclite86x" },
713     { TARGET_CPU_supersparc, "supersparc" },
714     { TARGET_CPU_v9, "v9" },
715     { TARGET_CPU_ultrasparc, "ultrasparc" },
716     { TARGET_CPU_ultrasparc3, "ultrasparc3" },
717     { TARGET_CPU_niagara, "niagara" },
718     { TARGET_CPU_niagara2, "niagara2" },
719     { 0, 0 }
720   };
721   const struct cpu_default *def;
722   /* Table of values for -m{cpu,tune}=.  */
723   static struct cpu_table {
724     const char *const name;
725     const enum processor_type processor;
726     const int disable;
727     const int enable;
728   } const cpu_table[] = {
729     { "v7",         PROCESSOR_V7, MASK_ISA, 0 },
730     { "cypress",    PROCESSOR_CYPRESS, MASK_ISA, 0 },
731     { "v8",         PROCESSOR_V8, MASK_ISA, MASK_V8 },
732     /* TI TMS390Z55 supersparc */
733     { "supersparc", PROCESSOR_SUPERSPARC, MASK_ISA, MASK_V8 },
734     { "sparclite",  PROCESSOR_SPARCLITE, MASK_ISA, MASK_SPARCLITE },
735     /* The Fujitsu MB86930 is the original sparclite chip, with no fpu.
736        The Fujitsu MB86934 is the recent sparclite chip, with an fpu.  */
737     { "f930",       PROCESSOR_F930, MASK_ISA|MASK_FPU, MASK_SPARCLITE },
738     { "f934",       PROCESSOR_F934, MASK_ISA, MASK_SPARCLITE|MASK_FPU },
739     { "hypersparc", PROCESSOR_HYPERSPARC, MASK_ISA, MASK_V8|MASK_FPU },
740     { "sparclite86x",  PROCESSOR_SPARCLITE86X, MASK_ISA|MASK_FPU,
741       MASK_SPARCLITE },
742     { "sparclet",   PROCESSOR_SPARCLET, MASK_ISA, MASK_SPARCLET },
743     /* TEMIC sparclet */
744     { "tsc701",     PROCESSOR_TSC701, MASK_ISA, MASK_SPARCLET },
745     { "v9",         PROCESSOR_V9, MASK_ISA, MASK_V9 },
746     /* TI ultrasparc I, II, IIi */
747     { "ultrasparc", PROCESSOR_ULTRASPARC, MASK_ISA, MASK_V9
748     /* Although insns using %y are deprecated, it is a clear win on current
749        ultrasparcs.  */
750                                                     |MASK_DEPRECATED_V8_INSNS},
751     /* TI ultrasparc III */
752     /* ??? Check if %y issue still holds true in ultra3.  */
753     { "ultrasparc3", PROCESSOR_ULTRASPARC3, MASK_ISA, MASK_V9|MASK_DEPRECATED_V8_INSNS},
754     /* UltraSPARC T1 */
755     { "niagara", PROCESSOR_NIAGARA, MASK_ISA, MASK_V9|MASK_DEPRECATED_V8_INSNS},
756     { "niagara2", PROCESSOR_NIAGARA, MASK_ISA, MASK_V9},
757     { 0, (enum processor_type) 0, 0, 0 }
758   };
759   const struct cpu_table *cpu;
760   const struct sparc_cpu_select *sel;
761   int fpu;
762
763 #ifdef SUBTARGET_OVERRIDE_OPTIONS
764   SUBTARGET_OVERRIDE_OPTIONS;
765 #endif
766
767 #ifndef SPARC_BI_ARCH
768   /* Check for unsupported architecture size.  */
769   if (! TARGET_64BIT != DEFAULT_ARCH32_P)
770     error ("%s is not supported by this configuration",
771            DEFAULT_ARCH32_P ? "-m64" : "-m32");
772 #endif
773
774   /* We force all 64bit archs to use 128 bit long double */
775   if (TARGET_64BIT && ! TARGET_LONG_DOUBLE_128)
776     {
777       error ("-mlong-double-64 not allowed with -m64");
778       target_flags |= MASK_LONG_DOUBLE_128;
779     }
780
781   /* Code model selection.  */
782   sparc_cmodel = SPARC_DEFAULT_CMODEL;
783
784 #ifdef SPARC_BI_ARCH
785   if (TARGET_ARCH32)
786     sparc_cmodel = CM_32;
787 #endif
788
789   if (sparc_cmodel_string != NULL)
790     {
791       if (TARGET_ARCH64)
792         {
793           for (cmodel = &cmodels[0]; cmodel->name; cmodel++)
794             if (strcmp (sparc_cmodel_string, cmodel->name) == 0)
795               break;
796           if (cmodel->name == NULL)
797             error ("bad value (%s) for -mcmodel= switch", sparc_cmodel_string);
798           else
799             sparc_cmodel = cmodel->value;
800         }
801       else
802         error ("-mcmodel= is not supported on 32 bit systems");
803     }
804
805   fpu = target_flags & MASK_FPU; /* save current -mfpu status */
806
807   /* Set the default CPU.  */
808   for (def = &cpu_default[0]; def->name; ++def)
809     if (def->cpu == TARGET_CPU_DEFAULT)
810       break;
811   gcc_assert (def->name);
812   sparc_select[0].string = def->name;
813
814   for (sel = &sparc_select[0]; sel->name; ++sel)
815     {
816       if (sel->string)
817         {
818           for (cpu = &cpu_table[0]; cpu->name; ++cpu)
819             if (! strcmp (sel->string, cpu->name))
820               {
821                 if (sel->set_tune_p)
822                   sparc_cpu = cpu->processor;
823
824                 if (sel->set_arch_p)
825                   {
826                     target_flags &= ~cpu->disable;
827                     target_flags |= cpu->enable;
828                   }
829                 break;
830               }
831
832           if (! cpu->name)
833             error ("bad value (%s) for %s switch", sel->string, sel->name);
834         }
835     }
836
837   /* If -mfpu or -mno-fpu was explicitly used, don't override with
838      the processor default.  */
839   if (fpu_option_set)
840     target_flags = (target_flags & ~MASK_FPU) | fpu;
841
842   /* Don't allow -mvis if FPU is disabled.  */
843   if (! TARGET_FPU)
844     target_flags &= ~MASK_VIS;
845
846   /* -mvis assumes UltraSPARC+, so we are sure v9 instructions
847      are available.
848      -m64 also implies v9.  */
849   if (TARGET_VIS || TARGET_ARCH64)
850     {
851       target_flags |= MASK_V9;
852       target_flags &= ~(MASK_V8 | MASK_SPARCLET | MASK_SPARCLITE);
853     }
854
855   /* Use the deprecated v8 insns for sparc64 in 32 bit mode.  */
856   if (TARGET_V9 && TARGET_ARCH32)
857     target_flags |= MASK_DEPRECATED_V8_INSNS;
858
859   /* V8PLUS requires V9, makes no sense in 64 bit mode.  */
860   if (! TARGET_V9 || TARGET_ARCH64)
861     target_flags &= ~MASK_V8PLUS;
862
863   /* Don't use stack biasing in 32 bit mode.  */
864   if (TARGET_ARCH32)
865     target_flags &= ~MASK_STACK_BIAS;
866
867   /* Supply a default value for align_functions.  */
868   if (align_functions == 0
869       && (sparc_cpu == PROCESSOR_ULTRASPARC
870           || sparc_cpu == PROCESSOR_ULTRASPARC3
871           || sparc_cpu == PROCESSOR_NIAGARA
872           || sparc_cpu == PROCESSOR_NIAGARA2))
873     align_functions = 32;
874
875   /* Validate PCC_STRUCT_RETURN.  */
876   if (flag_pcc_struct_return == DEFAULT_PCC_STRUCT_RETURN)
877     flag_pcc_struct_return = (TARGET_ARCH64 ? 0 : 1);
878
879   /* Only use .uaxword when compiling for a 64-bit target.  */
880   if (!TARGET_ARCH64)
881     targetm.asm_out.unaligned_op.di = NULL;
882
883   /* Do various machine dependent initializations.  */
884   sparc_init_modes ();
885
886   /* Acquire unique alias sets for our private stuff.  */
887   sparc_sr_alias_set = new_alias_set ();
888   struct_value_alias_set = new_alias_set ();
889
890   /* Set up function hooks.  */
891   init_machine_status = sparc_init_machine_status;
892
893   switch (sparc_cpu)
894     {
895     case PROCESSOR_V7:
896     case PROCESSOR_CYPRESS:
897       sparc_costs = &cypress_costs;
898       break;
899     case PROCESSOR_V8:
900     case PROCESSOR_SPARCLITE:
901     case PROCESSOR_SUPERSPARC:
902       sparc_costs = &supersparc_costs;
903       break;
904     case PROCESSOR_F930:
905     case PROCESSOR_F934:
906     case PROCESSOR_HYPERSPARC:
907     case PROCESSOR_SPARCLITE86X:
908       sparc_costs = &hypersparc_costs;
909       break;
910     case PROCESSOR_SPARCLET:
911     case PROCESSOR_TSC701:
912       sparc_costs = &sparclet_costs;
913       break;
914     case PROCESSOR_V9:
915     case PROCESSOR_ULTRASPARC:
916       sparc_costs = &ultrasparc_costs;
917       break;
918     case PROCESSOR_ULTRASPARC3:
919       sparc_costs = &ultrasparc3_costs;
920       break;
921     case PROCESSOR_NIAGARA:
922       sparc_costs = &niagara_costs;
923       break;
924     case PROCESSOR_NIAGARA2:
925       sparc_costs = &niagara2_costs;
926       break;
927     };
928
929 #ifdef TARGET_DEFAULT_LONG_DOUBLE_128
930   if (!(target_flags_explicit & MASK_LONG_DOUBLE_128))
931     target_flags |= MASK_LONG_DOUBLE_128;
932 #endif
933
934   maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
935                          ((sparc_cpu == PROCESSOR_ULTRASPARC
936                            || sparc_cpu == PROCESSOR_NIAGARA
937                            || sparc_cpu == PROCESSOR_NIAGARA2)
938                           ? 2
939                           : (sparc_cpu == PROCESSOR_ULTRASPARC3
940                              ? 8 : 3)),
941                          global_options.x_param_values,
942                          global_options_set.x_param_values);
943   maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
944                          ((sparc_cpu == PROCESSOR_ULTRASPARC
945                            || sparc_cpu == PROCESSOR_ULTRASPARC3
946                            || sparc_cpu == PROCESSOR_NIAGARA
947                            || sparc_cpu == PROCESSOR_NIAGARA2)
948                           ? 64 : 32),
949                          global_options.x_param_values,
950                          global_options_set.x_param_values);
951 }
952 \f
953 /* Miscellaneous utilities.  */
954
955 /* Nonzero if CODE, a comparison, is suitable for use in v9 conditional move
956    or branch on register contents instructions.  */
957
958 int
959 v9_regcmp_p (enum rtx_code code)
960 {
961   return (code == EQ || code == NE || code == GE || code == LT
962           || code == LE || code == GT);
963 }
964
965 /* Nonzero if OP is a floating point constant which can
966    be loaded into an integer register using a single
967    sethi instruction.  */
968
969 int
970 fp_sethi_p (rtx op)
971 {
972   if (GET_CODE (op) == CONST_DOUBLE)
973     {
974       REAL_VALUE_TYPE r;
975       long i;
976
977       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
978       REAL_VALUE_TO_TARGET_SINGLE (r, i);
979       return !SPARC_SIMM13_P (i) && SPARC_SETHI_P (i);
980     }
981
982   return 0;
983 }
984
985 /* Nonzero if OP is a floating point constant which can
986    be loaded into an integer register using a single
987    mov instruction.  */
988
989 int
990 fp_mov_p (rtx op)
991 {
992   if (GET_CODE (op) == CONST_DOUBLE)
993     {
994       REAL_VALUE_TYPE r;
995       long i;
996
997       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
998       REAL_VALUE_TO_TARGET_SINGLE (r, i);
999       return SPARC_SIMM13_P (i);
1000     }
1001
1002   return 0;
1003 }
1004
1005 /* Nonzero if OP is a floating point constant which can
1006    be loaded into an integer register using a high/losum
1007    instruction sequence.  */
1008
1009 int
1010 fp_high_losum_p (rtx op)
1011 {
1012   /* The constraints calling this should only be in
1013      SFmode move insns, so any constant which cannot
1014      be moved using a single insn will do.  */
1015   if (GET_CODE (op) == CONST_DOUBLE)
1016     {
1017       REAL_VALUE_TYPE r;
1018       long i;
1019
1020       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
1021       REAL_VALUE_TO_TARGET_SINGLE (r, i);
1022       return !SPARC_SIMM13_P (i) && !SPARC_SETHI_P (i);
1023     }
1024
1025   return 0;
1026 }
1027
1028 /* Return true if the address of LABEL can be loaded by means of the
1029    mov{si,di}_pic_label_ref patterns in PIC mode.  */
1030
1031 static bool
1032 can_use_mov_pic_label_ref (rtx label)
1033 {
1034   /* VxWorks does not impose a fixed gap between segments; the run-time
1035      gap can be different from the object-file gap.  We therefore can't
1036      assume X - _GLOBAL_OFFSET_TABLE_ is a link-time constant unless we
1037      are absolutely sure that X is in the same segment as the GOT.
1038      Unfortunately, the flexibility of linker scripts means that we
1039      can't be sure of that in general, so assume that GOT-relative
1040      accesses are never valid on VxWorks.  */
1041   if (TARGET_VXWORKS_RTP)
1042     return false;
1043
1044   /* Similarly, if the label is non-local, it might end up being placed
1045      in a different section than the current one; now mov_pic_label_ref
1046      requires the label and the code to be in the same section.  */
1047   if (LABEL_REF_NONLOCAL_P (label))
1048     return false;
1049
1050   /* Finally, if we are reordering basic blocks and partition into hot
1051      and cold sections, this might happen for any label.  */
1052   if (flag_reorder_blocks_and_partition)
1053     return false;
1054
1055   return true;
1056 }
1057
1058 /* Expand a move instruction.  Return true if all work is done.  */
1059
1060 bool
1061 sparc_expand_move (enum machine_mode mode, rtx *operands)
1062 {
1063   /* Handle sets of MEM first.  */
1064   if (GET_CODE (operands[0]) == MEM)
1065     {
1066       /* 0 is a register (or a pair of registers) on SPARC.  */
1067       if (register_or_zero_operand (operands[1], mode))
1068         return false;
1069
1070       if (!reload_in_progress)
1071         {
1072           operands[0] = validize_mem (operands[0]);
1073           operands[1] = force_reg (mode, operands[1]);
1074         }
1075     }
1076
1077   /* Fixup TLS cases.  */
1078   if (TARGET_HAVE_TLS
1079       && CONSTANT_P (operands[1])
1080       && sparc_tls_referenced_p (operands [1]))
1081     {
1082       operands[1] = sparc_legitimize_tls_address (operands[1]);
1083       return false;
1084     }
1085
1086   /* Fixup PIC cases.  */
1087   if (flag_pic && CONSTANT_P (operands[1]))
1088     {
1089       if (pic_address_needs_scratch (operands[1]))
1090         operands[1] = sparc_legitimize_pic_address (operands[1], NULL_RTX);
1091
1092       /* We cannot use the mov{si,di}_pic_label_ref patterns in all cases.  */
1093       if (GET_CODE (operands[1]) == LABEL_REF
1094           && can_use_mov_pic_label_ref (operands[1]))
1095         {
1096           if (mode == SImode)
1097             {
1098               emit_insn (gen_movsi_pic_label_ref (operands[0], operands[1]));
1099               return true;
1100             }
1101
1102           if (mode == DImode)
1103             {
1104               gcc_assert (TARGET_ARCH64);
1105               emit_insn (gen_movdi_pic_label_ref (operands[0], operands[1]));
1106               return true;
1107             }
1108         }
1109
1110       if (symbolic_operand (operands[1], mode))
1111         {
1112           operands[1]
1113             = sparc_legitimize_pic_address (operands[1],
1114                                             reload_in_progress
1115                                             ? operands[0] : NULL_RTX);
1116           return false;
1117         }
1118     }
1119
1120   /* If we are trying to toss an integer constant into FP registers,
1121      or loading a FP or vector constant, force it into memory.  */
1122   if (CONSTANT_P (operands[1])
1123       && REG_P (operands[0])
1124       && (SPARC_FP_REG_P (REGNO (operands[0]))
1125           || SCALAR_FLOAT_MODE_P (mode)
1126           || VECTOR_MODE_P (mode)))
1127     {
1128       /* emit_group_store will send such bogosity to us when it is
1129          not storing directly into memory.  So fix this up to avoid
1130          crashes in output_constant_pool.  */
1131       if (operands [1] == const0_rtx)
1132         operands[1] = CONST0_RTX (mode);
1133
1134       /* We can clear FP registers if TARGET_VIS, and always other regs.  */
1135       if ((TARGET_VIS || REGNO (operands[0]) < SPARC_FIRST_FP_REG)
1136           && const_zero_operand (operands[1], mode))
1137         return false;
1138
1139       if (REGNO (operands[0]) < SPARC_FIRST_FP_REG
1140           /* We are able to build any SF constant in integer registers
1141              with at most 2 instructions.  */
1142           && (mode == SFmode
1143               /* And any DF constant in integer registers.  */
1144               || (mode == DFmode
1145                   && (reload_completed || reload_in_progress))))
1146         return false;
1147
1148       operands[1] = force_const_mem (mode, operands[1]);
1149       if (!reload_in_progress)
1150         operands[1] = validize_mem (operands[1]);
1151       return false;
1152     }
1153
1154   /* Accept non-constants and valid constants unmodified.  */
1155   if (!CONSTANT_P (operands[1])
1156       || GET_CODE (operands[1]) == HIGH
1157       || input_operand (operands[1], mode))
1158     return false;
1159
1160   switch (mode)
1161     {
1162     case QImode:
1163       /* All QImode constants require only one insn, so proceed.  */
1164       break;
1165
1166     case HImode:
1167     case SImode:
1168       sparc_emit_set_const32 (operands[0], operands[1]);
1169       return true;
1170
1171     case DImode:
1172       /* input_operand should have filtered out 32-bit mode.  */
1173       sparc_emit_set_const64 (operands[0], operands[1]);
1174       return true;
1175
1176     default:
1177       gcc_unreachable ();
1178     }
1179
1180   return false;
1181 }
1182
1183 /* Load OP1, a 32-bit constant, into OP0, a register.
1184    We know it can't be done in one insn when we get
1185    here, the move expander guarantees this.  */
1186
1187 static void
1188 sparc_emit_set_const32 (rtx op0, rtx op1)
1189 {
1190   enum machine_mode mode = GET_MODE (op0);
1191   rtx temp;
1192
1193   if (reload_in_progress || reload_completed)
1194     temp = op0;
1195   else
1196     temp = gen_reg_rtx (mode);
1197
1198   if (GET_CODE (op1) == CONST_INT)
1199     {
1200       gcc_assert (!small_int_operand (op1, mode)
1201                   && !const_high_operand (op1, mode));
1202
1203       /* Emit them as real moves instead of a HIGH/LO_SUM,
1204          this way CSE can see everything and reuse intermediate
1205          values if it wants.  */
1206       emit_insn (gen_rtx_SET (VOIDmode, temp,
1207                               GEN_INT (INTVAL (op1)
1208                                 & ~(HOST_WIDE_INT)0x3ff)));
1209
1210       emit_insn (gen_rtx_SET (VOIDmode,
1211                               op0,
1212                               gen_rtx_IOR (mode, temp,
1213                                            GEN_INT (INTVAL (op1) & 0x3ff))));
1214     }
1215   else
1216     {
1217       /* A symbol, emit in the traditional way.  */
1218       emit_insn (gen_rtx_SET (VOIDmode, temp,
1219                               gen_rtx_HIGH (mode, op1)));
1220       emit_insn (gen_rtx_SET (VOIDmode,
1221                               op0, gen_rtx_LO_SUM (mode, temp, op1)));
1222     }
1223 }
1224
1225 /* Load OP1, a symbolic 64-bit constant, into OP0, a DImode register.
1226    If TEMP is nonzero, we are forbidden to use any other scratch
1227    registers.  Otherwise, we are allowed to generate them as needed.
1228
1229    Note that TEMP may have TImode if the code model is TARGET_CM_MEDANY
1230    or TARGET_CM_EMBMEDANY (see the reload_indi and reload_outdi patterns).  */
1231
1232 void
1233 sparc_emit_set_symbolic_const64 (rtx op0, rtx op1, rtx temp)
1234 {
1235   rtx temp1, temp2, temp3, temp4, temp5;
1236   rtx ti_temp = 0;
1237
1238   if (temp && GET_MODE (temp) == TImode)
1239     {
1240       ti_temp = temp;
1241       temp = gen_rtx_REG (DImode, REGNO (temp));
1242     }
1243
1244   /* SPARC-V9 code-model support.  */
1245   switch (sparc_cmodel)
1246     {
1247     case CM_MEDLOW:
1248       /* The range spanned by all instructions in the object is less
1249          than 2^31 bytes (2GB) and the distance from any instruction
1250          to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1251          than 2^31 bytes (2GB).
1252
1253          The executable must be in the low 4TB of the virtual address
1254          space.
1255
1256          sethi  %hi(symbol), %temp1
1257          or     %temp1, %lo(symbol), %reg  */
1258       if (temp)
1259         temp1 = temp;  /* op0 is allowed.  */
1260       else
1261         temp1 = gen_reg_rtx (DImode);
1262
1263       emit_insn (gen_rtx_SET (VOIDmode, temp1, gen_rtx_HIGH (DImode, op1)));
1264       emit_insn (gen_rtx_SET (VOIDmode, op0, gen_rtx_LO_SUM (DImode, temp1, op1)));
1265       break;
1266
1267     case CM_MEDMID:
1268       /* The range spanned by all instructions in the object is less
1269          than 2^31 bytes (2GB) and the distance from any instruction
1270          to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1271          than 2^31 bytes (2GB).
1272
1273          The executable must be in the low 16TB of the virtual address
1274          space.
1275
1276          sethi  %h44(symbol), %temp1
1277          or     %temp1, %m44(symbol), %temp2
1278          sllx   %temp2, 12, %temp3
1279          or     %temp3, %l44(symbol), %reg  */
1280       if (temp)
1281         {
1282           temp1 = op0;
1283           temp2 = op0;
1284           temp3 = temp;  /* op0 is allowed.  */
1285         }
1286       else
1287         {
1288           temp1 = gen_reg_rtx (DImode);
1289           temp2 = gen_reg_rtx (DImode);
1290           temp3 = gen_reg_rtx (DImode);
1291         }
1292
1293       emit_insn (gen_seth44 (temp1, op1));
1294       emit_insn (gen_setm44 (temp2, temp1, op1));
1295       emit_insn (gen_rtx_SET (VOIDmode, temp3,
1296                               gen_rtx_ASHIFT (DImode, temp2, GEN_INT (12))));
1297       emit_insn (gen_setl44 (op0, temp3, op1));
1298       break;
1299
1300     case CM_MEDANY:
1301       /* The range spanned by all instructions in the object is less
1302          than 2^31 bytes (2GB) and the distance from any instruction
1303          to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1304          than 2^31 bytes (2GB).
1305
1306          The executable can be placed anywhere in the virtual address
1307          space.
1308
1309          sethi  %hh(symbol), %temp1
1310          sethi  %lm(symbol), %temp2
1311          or     %temp1, %hm(symbol), %temp3
1312          sllx   %temp3, 32, %temp4
1313          or     %temp4, %temp2, %temp5
1314          or     %temp5, %lo(symbol), %reg  */
1315       if (temp)
1316         {
1317           /* It is possible that one of the registers we got for operands[2]
1318              might coincide with that of operands[0] (which is why we made
1319              it TImode).  Pick the other one to use as our scratch.  */
1320           if (rtx_equal_p (temp, op0))
1321             {
1322               gcc_assert (ti_temp);
1323               temp = gen_rtx_REG (DImode, REGNO (temp) + 1);
1324             }
1325           temp1 = op0;
1326           temp2 = temp;  /* op0 is _not_ allowed, see above.  */
1327           temp3 = op0;
1328           temp4 = op0;
1329           temp5 = op0;
1330         }
1331       else
1332         {
1333           temp1 = gen_reg_rtx (DImode);
1334           temp2 = gen_reg_rtx (DImode);
1335           temp3 = gen_reg_rtx (DImode);
1336           temp4 = gen_reg_rtx (DImode);
1337           temp5 = gen_reg_rtx (DImode);
1338         }
1339
1340       emit_insn (gen_sethh (temp1, op1));
1341       emit_insn (gen_setlm (temp2, op1));
1342       emit_insn (gen_sethm (temp3, temp1, op1));
1343       emit_insn (gen_rtx_SET (VOIDmode, temp4,
1344                               gen_rtx_ASHIFT (DImode, temp3, GEN_INT (32))));
1345       emit_insn (gen_rtx_SET (VOIDmode, temp5,
1346                               gen_rtx_PLUS (DImode, temp4, temp2)));
1347       emit_insn (gen_setlo (op0, temp5, op1));
1348       break;
1349
1350     case CM_EMBMEDANY:
1351       /* Old old old backwards compatibility kruft here.
1352          Essentially it is MEDLOW with a fixed 64-bit
1353          virtual base added to all data segment addresses.
1354          Text-segment stuff is computed like MEDANY, we can't
1355          reuse the code above because the relocation knobs
1356          look different.
1357
1358          Data segment:  sethi   %hi(symbol), %temp1
1359                         add     %temp1, EMBMEDANY_BASE_REG, %temp2
1360                         or      %temp2, %lo(symbol), %reg  */
1361       if (data_segment_operand (op1, GET_MODE (op1)))
1362         {
1363           if (temp)
1364             {
1365               temp1 = temp;  /* op0 is allowed.  */
1366               temp2 = op0;
1367             }
1368           else
1369             {
1370               temp1 = gen_reg_rtx (DImode);
1371               temp2 = gen_reg_rtx (DImode);
1372             }
1373
1374           emit_insn (gen_embmedany_sethi (temp1, op1));
1375           emit_insn (gen_embmedany_brsum (temp2, temp1));
1376           emit_insn (gen_embmedany_losum (op0, temp2, op1));
1377         }
1378
1379       /* Text segment:  sethi   %uhi(symbol), %temp1
1380                         sethi   %hi(symbol), %temp2
1381                         or      %temp1, %ulo(symbol), %temp3
1382                         sllx    %temp3, 32, %temp4
1383                         or      %temp4, %temp2, %temp5
1384                         or      %temp5, %lo(symbol), %reg  */
1385       else
1386         {
1387           if (temp)
1388             {
1389               /* It is possible that one of the registers we got for operands[2]
1390                  might coincide with that of operands[0] (which is why we made
1391                  it TImode).  Pick the other one to use as our scratch.  */
1392               if (rtx_equal_p (temp, op0))
1393                 {
1394                   gcc_assert (ti_temp);
1395                   temp = gen_rtx_REG (DImode, REGNO (temp) + 1);
1396                 }
1397               temp1 = op0;
1398               temp2 = temp;  /* op0 is _not_ allowed, see above.  */
1399               temp3 = op0;
1400               temp4 = op0;
1401               temp5 = op0;
1402             }
1403           else
1404             {
1405               temp1 = gen_reg_rtx (DImode);
1406               temp2 = gen_reg_rtx (DImode);
1407               temp3 = gen_reg_rtx (DImode);
1408               temp4 = gen_reg_rtx (DImode);
1409               temp5 = gen_reg_rtx (DImode);
1410             }
1411
1412           emit_insn (gen_embmedany_textuhi (temp1, op1));
1413           emit_insn (gen_embmedany_texthi  (temp2, op1));
1414           emit_insn (gen_embmedany_textulo (temp3, temp1, op1));
1415           emit_insn (gen_rtx_SET (VOIDmode, temp4,
1416                                   gen_rtx_ASHIFT (DImode, temp3, GEN_INT (32))));
1417           emit_insn (gen_rtx_SET (VOIDmode, temp5,
1418                                   gen_rtx_PLUS (DImode, temp4, temp2)));
1419           emit_insn (gen_embmedany_textlo  (op0, temp5, op1));
1420         }
1421       break;
1422
1423     default:
1424       gcc_unreachable ();
1425     }
1426 }
1427
1428 #if HOST_BITS_PER_WIDE_INT == 32
1429 static void
1430 sparc_emit_set_const64 (rtx op0 ATTRIBUTE_UNUSED, rtx op1 ATTRIBUTE_UNUSED)
1431 {
1432   gcc_unreachable ();
1433 }
1434 #else
1435 /* These avoid problems when cross compiling.  If we do not
1436    go through all this hair then the optimizer will see
1437    invalid REG_EQUAL notes or in some cases none at all.  */
1438 static rtx gen_safe_HIGH64 (rtx, HOST_WIDE_INT);
1439 static rtx gen_safe_SET64 (rtx, HOST_WIDE_INT);
1440 static rtx gen_safe_OR64 (rtx, HOST_WIDE_INT);
1441 static rtx gen_safe_XOR64 (rtx, HOST_WIDE_INT);
1442
1443 /* The optimizer is not to assume anything about exactly
1444    which bits are set for a HIGH, they are unspecified.
1445    Unfortunately this leads to many missed optimizations
1446    during CSE.  We mask out the non-HIGH bits, and matches
1447    a plain movdi, to alleviate this problem.  */
1448 static rtx
1449 gen_safe_HIGH64 (rtx dest, HOST_WIDE_INT val)
1450 {
1451   return gen_rtx_SET (VOIDmode, dest, GEN_INT (val & ~(HOST_WIDE_INT)0x3ff));
1452 }
1453
1454 static rtx
1455 gen_safe_SET64 (rtx dest, HOST_WIDE_INT val)
1456 {
1457   return gen_rtx_SET (VOIDmode, dest, GEN_INT (val));
1458 }
1459
1460 static rtx
1461 gen_safe_OR64 (rtx src, HOST_WIDE_INT val)
1462 {
1463   return gen_rtx_IOR (DImode, src, GEN_INT (val));
1464 }
1465
1466 static rtx
1467 gen_safe_XOR64 (rtx src, HOST_WIDE_INT val)
1468 {
1469   return gen_rtx_XOR (DImode, src, GEN_INT (val));
1470 }
1471
1472 /* Worker routines for 64-bit constant formation on arch64.
1473    One of the key things to be doing in these emissions is
1474    to create as many temp REGs as possible.  This makes it
1475    possible for half-built constants to be used later when
1476    such values are similar to something required later on.
1477    Without doing this, the optimizer cannot see such
1478    opportunities.  */
1479
1480 static void sparc_emit_set_const64_quick1 (rtx, rtx,
1481                                            unsigned HOST_WIDE_INT, int);
1482
1483 static void
1484 sparc_emit_set_const64_quick1 (rtx op0, rtx temp,
1485                                unsigned HOST_WIDE_INT low_bits, int is_neg)
1486 {
1487   unsigned HOST_WIDE_INT high_bits;
1488
1489   if (is_neg)
1490     high_bits = (~low_bits) & 0xffffffff;
1491   else
1492     high_bits = low_bits;
1493
1494   emit_insn (gen_safe_HIGH64 (temp, high_bits));
1495   if (!is_neg)
1496     {
1497       emit_insn (gen_rtx_SET (VOIDmode, op0,
1498                               gen_safe_OR64 (temp, (high_bits & 0x3ff))));
1499     }
1500   else
1501     {
1502       /* If we are XOR'ing with -1, then we should emit a one's complement
1503          instead.  This way the combiner will notice logical operations
1504          such as ANDN later on and substitute.  */
1505       if ((low_bits & 0x3ff) == 0x3ff)
1506         {
1507           emit_insn (gen_rtx_SET (VOIDmode, op0,
1508                                   gen_rtx_NOT (DImode, temp)));
1509         }
1510       else
1511         {
1512           emit_insn (gen_rtx_SET (VOIDmode, op0,
1513                                   gen_safe_XOR64 (temp,
1514                                                   (-(HOST_WIDE_INT)0x400
1515                                                    | (low_bits & 0x3ff)))));
1516         }
1517     }
1518 }
1519
1520 static void sparc_emit_set_const64_quick2 (rtx, rtx, unsigned HOST_WIDE_INT,
1521                                            unsigned HOST_WIDE_INT, int);
1522
1523 static void
1524 sparc_emit_set_const64_quick2 (rtx op0, rtx temp,
1525                                unsigned HOST_WIDE_INT high_bits,
1526                                unsigned HOST_WIDE_INT low_immediate,
1527                                int shift_count)
1528 {
1529   rtx temp2 = op0;
1530
1531   if ((high_bits & 0xfffffc00) != 0)
1532     {
1533       emit_insn (gen_safe_HIGH64 (temp, high_bits));
1534       if ((high_bits & ~0xfffffc00) != 0)
1535         emit_insn (gen_rtx_SET (VOIDmode, op0,
1536                                 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
1537       else
1538         temp2 = temp;
1539     }
1540   else
1541     {
1542       emit_insn (gen_safe_SET64 (temp, high_bits));
1543       temp2 = temp;
1544     }
1545
1546   /* Now shift it up into place.  */
1547   emit_insn (gen_rtx_SET (VOIDmode, op0,
1548                           gen_rtx_ASHIFT (DImode, temp2,
1549                                           GEN_INT (shift_count))));
1550
1551   /* If there is a low immediate part piece, finish up by
1552      putting that in as well.  */
1553   if (low_immediate != 0)
1554     emit_insn (gen_rtx_SET (VOIDmode, op0,
1555                             gen_safe_OR64 (op0, low_immediate)));
1556 }
1557
1558 static void sparc_emit_set_const64_longway (rtx, rtx, unsigned HOST_WIDE_INT,
1559                                             unsigned HOST_WIDE_INT);
1560
1561 /* Full 64-bit constant decomposition.  Even though this is the
1562    'worst' case, we still optimize a few things away.  */
1563 static void
1564 sparc_emit_set_const64_longway (rtx op0, rtx temp,
1565                                 unsigned HOST_WIDE_INT high_bits,
1566                                 unsigned HOST_WIDE_INT low_bits)
1567 {
1568   rtx sub_temp;
1569
1570   if (reload_in_progress || reload_completed)
1571     sub_temp = op0;
1572   else
1573     sub_temp = gen_reg_rtx (DImode);
1574
1575   if ((high_bits & 0xfffffc00) != 0)
1576     {
1577       emit_insn (gen_safe_HIGH64 (temp, high_bits));
1578       if ((high_bits & ~0xfffffc00) != 0)
1579         emit_insn (gen_rtx_SET (VOIDmode,
1580                                 sub_temp,
1581                                 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
1582       else
1583         sub_temp = temp;
1584     }
1585   else
1586     {
1587       emit_insn (gen_safe_SET64 (temp, high_bits));
1588       sub_temp = temp;
1589     }
1590
1591   if (!reload_in_progress && !reload_completed)
1592     {
1593       rtx temp2 = gen_reg_rtx (DImode);
1594       rtx temp3 = gen_reg_rtx (DImode);
1595       rtx temp4 = gen_reg_rtx (DImode);
1596
1597       emit_insn (gen_rtx_SET (VOIDmode, temp4,
1598                               gen_rtx_ASHIFT (DImode, sub_temp,
1599                                               GEN_INT (32))));
1600
1601       emit_insn (gen_safe_HIGH64 (temp2, low_bits));
1602       if ((low_bits & ~0xfffffc00) != 0)
1603         {
1604           emit_insn (gen_rtx_SET (VOIDmode, temp3,
1605                                   gen_safe_OR64 (temp2, (low_bits & 0x3ff))));
1606           emit_insn (gen_rtx_SET (VOIDmode, op0,
1607                                   gen_rtx_PLUS (DImode, temp4, temp3)));
1608         }
1609       else
1610         {
1611           emit_insn (gen_rtx_SET (VOIDmode, op0,
1612                                   gen_rtx_PLUS (DImode, temp4, temp2)));
1613         }
1614     }
1615   else
1616     {
1617       rtx low1 = GEN_INT ((low_bits >> (32 - 12))          & 0xfff);
1618       rtx low2 = GEN_INT ((low_bits >> (32 - 12 - 12))     & 0xfff);
1619       rtx low3 = GEN_INT ((low_bits >> (32 - 12 - 12 - 8)) & 0x0ff);
1620       int to_shift = 12;
1621
1622       /* We are in the middle of reload, so this is really
1623          painful.  However we do still make an attempt to
1624          avoid emitting truly stupid code.  */
1625       if (low1 != const0_rtx)
1626         {
1627           emit_insn (gen_rtx_SET (VOIDmode, op0,
1628                                   gen_rtx_ASHIFT (DImode, sub_temp,
1629                                                   GEN_INT (to_shift))));
1630           emit_insn (gen_rtx_SET (VOIDmode, op0,
1631                                   gen_rtx_IOR (DImode, op0, low1)));
1632           sub_temp = op0;
1633           to_shift = 12;
1634         }
1635       else
1636         {
1637           to_shift += 12;
1638         }
1639       if (low2 != const0_rtx)
1640         {
1641           emit_insn (gen_rtx_SET (VOIDmode, op0,
1642                                   gen_rtx_ASHIFT (DImode, sub_temp,
1643                                                   GEN_INT (to_shift))));
1644           emit_insn (gen_rtx_SET (VOIDmode, op0,
1645                                   gen_rtx_IOR (DImode, op0, low2)));
1646           sub_temp = op0;
1647           to_shift = 8;
1648         }
1649       else
1650         {
1651           to_shift += 8;
1652         }
1653       emit_insn (gen_rtx_SET (VOIDmode, op0,
1654                               gen_rtx_ASHIFT (DImode, sub_temp,
1655                                               GEN_INT (to_shift))));
1656       if (low3 != const0_rtx)
1657         emit_insn (gen_rtx_SET (VOIDmode, op0,
1658                                 gen_rtx_IOR (DImode, op0, low3)));
1659       /* phew...  */
1660     }
1661 }
1662
1663 /* Analyze a 64-bit constant for certain properties.  */
1664 static void analyze_64bit_constant (unsigned HOST_WIDE_INT,
1665                                     unsigned HOST_WIDE_INT,
1666                                     int *, int *, int *);
1667
1668 static void
1669 analyze_64bit_constant (unsigned HOST_WIDE_INT high_bits,
1670                         unsigned HOST_WIDE_INT low_bits,
1671                         int *hbsp, int *lbsp, int *abbasp)
1672 {
1673   int lowest_bit_set, highest_bit_set, all_bits_between_are_set;
1674   int i;
1675
1676   lowest_bit_set = highest_bit_set = -1;
1677   i = 0;
1678   do
1679     {
1680       if ((lowest_bit_set == -1)
1681           && ((low_bits >> i) & 1))
1682         lowest_bit_set = i;
1683       if ((highest_bit_set == -1)
1684           && ((high_bits >> (32 - i - 1)) & 1))
1685         highest_bit_set = (64 - i - 1);
1686     }
1687   while (++i < 32
1688          && ((highest_bit_set == -1)
1689              || (lowest_bit_set == -1)));
1690   if (i == 32)
1691     {
1692       i = 0;
1693       do
1694         {
1695           if ((lowest_bit_set == -1)
1696               && ((high_bits >> i) & 1))
1697             lowest_bit_set = i + 32;
1698           if ((highest_bit_set == -1)
1699               && ((low_bits >> (32 - i - 1)) & 1))
1700             highest_bit_set = 32 - i - 1;
1701         }
1702       while (++i < 32
1703              && ((highest_bit_set == -1)
1704                  || (lowest_bit_set == -1)));
1705     }
1706   /* If there are no bits set this should have gone out
1707      as one instruction!  */
1708   gcc_assert (lowest_bit_set != -1 && highest_bit_set != -1);
1709   all_bits_between_are_set = 1;
1710   for (i = lowest_bit_set; i <= highest_bit_set; i++)
1711     {
1712       if (i < 32)
1713         {
1714           if ((low_bits & (1 << i)) != 0)
1715             continue;
1716         }
1717       else
1718         {
1719           if ((high_bits & (1 << (i - 32))) != 0)
1720             continue;
1721         }
1722       all_bits_between_are_set = 0;
1723       break;
1724     }
1725   *hbsp = highest_bit_set;
1726   *lbsp = lowest_bit_set;
1727   *abbasp = all_bits_between_are_set;
1728 }
1729
1730 static int const64_is_2insns (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
1731
1732 static int
1733 const64_is_2insns (unsigned HOST_WIDE_INT high_bits,
1734                    unsigned HOST_WIDE_INT low_bits)
1735 {
1736   int highest_bit_set, lowest_bit_set, all_bits_between_are_set;
1737
1738   if (high_bits == 0
1739       || high_bits == 0xffffffff)
1740     return 1;
1741
1742   analyze_64bit_constant (high_bits, low_bits,
1743                           &highest_bit_set, &lowest_bit_set,
1744                           &all_bits_between_are_set);
1745
1746   if ((highest_bit_set == 63
1747        || lowest_bit_set == 0)
1748       && all_bits_between_are_set != 0)
1749     return 1;
1750
1751   if ((highest_bit_set - lowest_bit_set) < 21)
1752     return 1;
1753
1754   return 0;
1755 }
1756
1757 static unsigned HOST_WIDE_INT create_simple_focus_bits (unsigned HOST_WIDE_INT,
1758                                                         unsigned HOST_WIDE_INT,
1759                                                         int, int);
1760
1761 static unsigned HOST_WIDE_INT
1762 create_simple_focus_bits (unsigned HOST_WIDE_INT high_bits,
1763                           unsigned HOST_WIDE_INT low_bits,
1764                           int lowest_bit_set, int shift)
1765 {
1766   HOST_WIDE_INT hi, lo;
1767
1768   if (lowest_bit_set < 32)
1769     {
1770       lo = (low_bits >> lowest_bit_set) << shift;
1771       hi = ((high_bits << (32 - lowest_bit_set)) << shift);
1772     }
1773   else
1774     {
1775       lo = 0;
1776       hi = ((high_bits >> (lowest_bit_set - 32)) << shift);
1777     }
1778   gcc_assert (! (hi & lo));
1779   return (hi | lo);
1780 }
1781
1782 /* Here we are sure to be arch64 and this is an integer constant
1783    being loaded into a register.  Emit the most efficient
1784    insn sequence possible.  Detection of all the 1-insn cases
1785    has been done already.  */
1786 static void
1787 sparc_emit_set_const64 (rtx op0, rtx op1)
1788 {
1789   unsigned HOST_WIDE_INT high_bits, low_bits;
1790   int lowest_bit_set, highest_bit_set;
1791   int all_bits_between_are_set;
1792   rtx temp = 0;
1793
1794   /* Sanity check that we know what we are working with.  */
1795   gcc_assert (TARGET_ARCH64
1796               && (GET_CODE (op0) == SUBREG
1797                   || (REG_P (op0) && ! SPARC_FP_REG_P (REGNO (op0)))));
1798
1799   if (reload_in_progress || reload_completed)
1800     temp = op0;
1801
1802   if (GET_CODE (op1) != CONST_INT)
1803     {
1804       sparc_emit_set_symbolic_const64 (op0, op1, temp);
1805       return;
1806     }
1807
1808   if (! temp)
1809     temp = gen_reg_rtx (DImode);
1810
1811   high_bits = ((INTVAL (op1) >> 32) & 0xffffffff);
1812   low_bits = (INTVAL (op1) & 0xffffffff);
1813
1814   /* low_bits   bits 0  --> 31
1815      high_bits  bits 32 --> 63  */
1816
1817   analyze_64bit_constant (high_bits, low_bits,
1818                           &highest_bit_set, &lowest_bit_set,
1819                           &all_bits_between_are_set);
1820
1821   /* First try for a 2-insn sequence.  */
1822
1823   /* These situations are preferred because the optimizer can
1824    * do more things with them:
1825    * 1) mov     -1, %reg
1826    *    sllx    %reg, shift, %reg
1827    * 2) mov     -1, %reg
1828    *    srlx    %reg, shift, %reg
1829    * 3) mov     some_small_const, %reg
1830    *    sllx    %reg, shift, %reg
1831    */
1832   if (((highest_bit_set == 63
1833         || lowest_bit_set == 0)
1834        && all_bits_between_are_set != 0)
1835       || ((highest_bit_set - lowest_bit_set) < 12))
1836     {
1837       HOST_WIDE_INT the_const = -1;
1838       int shift = lowest_bit_set;
1839
1840       if ((highest_bit_set != 63
1841            && lowest_bit_set != 0)
1842           || all_bits_between_are_set == 0)
1843         {
1844           the_const =
1845             create_simple_focus_bits (high_bits, low_bits,
1846                                       lowest_bit_set, 0);
1847         }
1848       else if (lowest_bit_set == 0)
1849         shift = -(63 - highest_bit_set);
1850
1851       gcc_assert (SPARC_SIMM13_P (the_const));
1852       gcc_assert (shift != 0);
1853
1854       emit_insn (gen_safe_SET64 (temp, the_const));
1855       if (shift > 0)
1856         emit_insn (gen_rtx_SET (VOIDmode,
1857                                 op0,
1858                                 gen_rtx_ASHIFT (DImode,
1859                                                 temp,
1860                                                 GEN_INT (shift))));
1861       else if (shift < 0)
1862         emit_insn (gen_rtx_SET (VOIDmode,
1863                                 op0,
1864                                 gen_rtx_LSHIFTRT (DImode,
1865                                                   temp,
1866                                                   GEN_INT (-shift))));
1867       return;
1868     }
1869
1870   /* Now a range of 22 or less bits set somewhere.
1871    * 1) sethi   %hi(focus_bits), %reg
1872    *    sllx    %reg, shift, %reg
1873    * 2) sethi   %hi(focus_bits), %reg
1874    *    srlx    %reg, shift, %reg
1875    */
1876   if ((highest_bit_set - lowest_bit_set) < 21)
1877     {
1878       unsigned HOST_WIDE_INT focus_bits =
1879         create_simple_focus_bits (high_bits, low_bits,
1880                                   lowest_bit_set, 10);
1881
1882       gcc_assert (SPARC_SETHI_P (focus_bits));
1883       gcc_assert (lowest_bit_set != 10);
1884
1885       emit_insn (gen_safe_HIGH64 (temp, focus_bits));
1886
1887       /* If lowest_bit_set == 10 then a sethi alone could have done it.  */
1888       if (lowest_bit_set < 10)
1889         emit_insn (gen_rtx_SET (VOIDmode,
1890                                 op0,
1891                                 gen_rtx_LSHIFTRT (DImode, temp,
1892                                                   GEN_INT (10 - lowest_bit_set))));
1893       else if (lowest_bit_set > 10)
1894         emit_insn (gen_rtx_SET (VOIDmode,
1895                                 op0,
1896                                 gen_rtx_ASHIFT (DImode, temp,
1897                                                 GEN_INT (lowest_bit_set - 10))));
1898       return;
1899     }
1900
1901   /* 1) sethi   %hi(low_bits), %reg
1902    *    or      %reg, %lo(low_bits), %reg
1903    * 2) sethi   %hi(~low_bits), %reg
1904    *    xor     %reg, %lo(-0x400 | (low_bits & 0x3ff)), %reg
1905    */
1906   if (high_bits == 0
1907       || high_bits == 0xffffffff)
1908     {
1909       sparc_emit_set_const64_quick1 (op0, temp, low_bits,
1910                                      (high_bits == 0xffffffff));
1911       return;
1912     }
1913
1914   /* Now, try 3-insn sequences.  */
1915
1916   /* 1) sethi   %hi(high_bits), %reg
1917    *    or      %reg, %lo(high_bits), %reg
1918    *    sllx    %reg, 32, %reg
1919    */
1920   if (low_bits == 0)
1921     {
1922       sparc_emit_set_const64_quick2 (op0, temp, high_bits, 0, 32);
1923       return;
1924     }
1925
1926   /* We may be able to do something quick
1927      when the constant is negated, so try that.  */
1928   if (const64_is_2insns ((~high_bits) & 0xffffffff,
1929                          (~low_bits) & 0xfffffc00))
1930     {
1931       /* NOTE: The trailing bits get XOR'd so we need the
1932          non-negated bits, not the negated ones.  */
1933       unsigned HOST_WIDE_INT trailing_bits = low_bits & 0x3ff;
1934
1935       if ((((~high_bits) & 0xffffffff) == 0
1936            && ((~low_bits) & 0x80000000) == 0)
1937           || (((~high_bits) & 0xffffffff) == 0xffffffff
1938               && ((~low_bits) & 0x80000000) != 0))
1939         {
1940           unsigned HOST_WIDE_INT fast_int = (~low_bits & 0xffffffff);
1941
1942           if ((SPARC_SETHI_P (fast_int)
1943                && (~high_bits & 0xffffffff) == 0)
1944               || SPARC_SIMM13_P (fast_int))
1945             emit_insn (gen_safe_SET64 (temp, fast_int));
1946           else
1947             sparc_emit_set_const64 (temp, GEN_INT (fast_int));
1948         }
1949       else
1950         {
1951           rtx negated_const;
1952           negated_const = GEN_INT (((~low_bits) & 0xfffffc00) |
1953                                    (((HOST_WIDE_INT)((~high_bits) & 0xffffffff))<<32));
1954           sparc_emit_set_const64 (temp, negated_const);
1955         }
1956
1957       /* If we are XOR'ing with -1, then we should emit a one's complement
1958          instead.  This way the combiner will notice logical operations
1959          such as ANDN later on and substitute.  */
1960       if (trailing_bits == 0x3ff)
1961         {
1962           emit_insn (gen_rtx_SET (VOIDmode, op0,
1963                                   gen_rtx_NOT (DImode, temp)));
1964         }
1965       else
1966         {
1967           emit_insn (gen_rtx_SET (VOIDmode,
1968                                   op0,
1969                                   gen_safe_XOR64 (temp,
1970                                                   (-0x400 | trailing_bits))));
1971         }
1972       return;
1973     }
1974
1975   /* 1) sethi   %hi(xxx), %reg
1976    *    or      %reg, %lo(xxx), %reg
1977    *    sllx    %reg, yyy, %reg
1978    *
1979    * ??? This is just a generalized version of the low_bits==0
1980    * thing above, FIXME...
1981    */
1982   if ((highest_bit_set - lowest_bit_set) < 32)
1983     {
1984       unsigned HOST_WIDE_INT focus_bits =
1985         create_simple_focus_bits (high_bits, low_bits,
1986                                   lowest_bit_set, 0);
1987
1988       /* We can't get here in this state.  */
1989       gcc_assert (highest_bit_set >= 32 && lowest_bit_set < 32);
1990
1991       /* So what we know is that the set bits straddle the
1992          middle of the 64-bit word.  */
1993       sparc_emit_set_const64_quick2 (op0, temp,
1994                                      focus_bits, 0,
1995                                      lowest_bit_set);
1996       return;
1997     }
1998
1999   /* 1) sethi   %hi(high_bits), %reg
2000    *    or      %reg, %lo(high_bits), %reg
2001    *    sllx    %reg, 32, %reg
2002    *    or      %reg, low_bits, %reg
2003    */
2004   if (SPARC_SIMM13_P(low_bits)
2005       && ((int)low_bits > 0))
2006     {
2007       sparc_emit_set_const64_quick2 (op0, temp, high_bits, low_bits, 32);
2008       return;
2009     }
2010
2011   /* The easiest way when all else fails, is full decomposition.  */
2012   sparc_emit_set_const64_longway (op0, temp, high_bits, low_bits);
2013 }
2014 #endif /* HOST_BITS_PER_WIDE_INT == 32 */
2015
2016 /* Given a comparison code (EQ, NE, etc.) and the first operand of a COMPARE,
2017    return the mode to be used for the comparison.  For floating-point,
2018    CCFP[E]mode is used.  CC_NOOVmode should be used when the first operand
2019    is a PLUS, MINUS, NEG, or ASHIFT.  CCmode should be used when no special
2020    processing is needed.  */
2021
2022 enum machine_mode
2023 select_cc_mode (enum rtx_code op, rtx x, rtx y ATTRIBUTE_UNUSED)
2024 {
2025   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2026     {
2027       switch (op)
2028         {
2029         case EQ:
2030         case NE:
2031         case UNORDERED:
2032         case ORDERED:
2033         case UNLT:
2034         case UNLE:
2035         case UNGT:
2036         case UNGE:
2037         case UNEQ:
2038         case LTGT:
2039           return CCFPmode;
2040
2041         case LT:
2042         case LE:
2043         case GT:
2044         case GE:
2045           return CCFPEmode;
2046
2047         default:
2048           gcc_unreachable ();
2049         }
2050     }
2051   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
2052            || GET_CODE (x) == NEG || GET_CODE (x) == ASHIFT)
2053     {
2054       if (TARGET_ARCH64 && GET_MODE (x) == DImode)
2055         return CCX_NOOVmode;
2056       else
2057         return CC_NOOVmode;
2058     }
2059   else
2060     {
2061       if (TARGET_ARCH64 && GET_MODE (x) == DImode)
2062         return CCXmode;
2063       else
2064         return CCmode;
2065     }
2066 }
2067
2068 /* Emit the compare insn and return the CC reg for a CODE comparison
2069    with operands X and Y.  */
2070
2071 static rtx
2072 gen_compare_reg_1 (enum rtx_code code, rtx x, rtx y)
2073 {
2074   enum machine_mode mode;
2075   rtx cc_reg;
2076
2077   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
2078     return x;
2079
2080   mode = SELECT_CC_MODE (code, x, y);
2081
2082   /* ??? We don't have movcc patterns so we cannot generate pseudo regs for the
2083      fcc regs (cse can't tell they're really call clobbered regs and will
2084      remove a duplicate comparison even if there is an intervening function
2085      call - it will then try to reload the cc reg via an int reg which is why
2086      we need the movcc patterns).  It is possible to provide the movcc
2087      patterns by using the ldxfsr/stxfsr v9 insns.  I tried it: you need two
2088      registers (say %g1,%g5) and it takes about 6 insns.  A better fix would be
2089      to tell cse that CCFPE mode registers (even pseudos) are call
2090      clobbered.  */
2091
2092   /* ??? This is an experiment.  Rather than making changes to cse which may
2093      or may not be easy/clean, we do our own cse.  This is possible because
2094      we will generate hard registers.  Cse knows they're call clobbered (it
2095      doesn't know the same thing about pseudos). If we guess wrong, no big
2096      deal, but if we win, great!  */
2097
2098   if (TARGET_V9 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2099 #if 1 /* experiment */
2100     {
2101       int reg;
2102       /* We cycle through the registers to ensure they're all exercised.  */
2103       static int next_fcc_reg = 0;
2104       /* Previous x,y for each fcc reg.  */
2105       static rtx prev_args[4][2];
2106
2107       /* Scan prev_args for x,y.  */
2108       for (reg = 0; reg < 4; reg++)
2109         if (prev_args[reg][0] == x && prev_args[reg][1] == y)
2110           break;
2111       if (reg == 4)
2112         {
2113           reg = next_fcc_reg;
2114           prev_args[reg][0] = x;
2115           prev_args[reg][1] = y;
2116           next_fcc_reg = (next_fcc_reg + 1) & 3;
2117         }
2118       cc_reg = gen_rtx_REG (mode, reg + SPARC_FIRST_V9_FCC_REG);
2119     }
2120 #else
2121     cc_reg = gen_reg_rtx (mode);
2122 #endif /* ! experiment */
2123   else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2124     cc_reg = gen_rtx_REG (mode, SPARC_FCC_REG);
2125   else
2126     cc_reg = gen_rtx_REG (mode, SPARC_ICC_REG);
2127
2128   /* We shouldn't get there for TFmode if !TARGET_HARD_QUAD.  If we do, this
2129      will only result in an unrecognizable insn so no point in asserting.  */
2130   emit_insn (gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y)));
2131
2132   return cc_reg;
2133 }
2134
2135
2136 /* Emit the compare insn and return the CC reg for the comparison in CMP.  */
2137
2138 rtx
2139 gen_compare_reg (rtx cmp)
2140 {
2141   return gen_compare_reg_1 (GET_CODE (cmp), XEXP (cmp, 0), XEXP (cmp, 1));
2142 }
2143
2144 /* This function is used for v9 only.
2145    DEST is the target of the Scc insn.
2146    CODE is the code for an Scc's comparison.
2147    X and Y are the values we compare.
2148
2149    This function is needed to turn
2150
2151            (set (reg:SI 110)
2152                (gt (reg:CCX 100 %icc)
2153                    (const_int 0)))
2154    into
2155            (set (reg:SI 110)
2156                (gt:DI (reg:CCX 100 %icc)
2157                    (const_int 0)))
2158
2159    IE: The instruction recognizer needs to see the mode of the comparison to
2160    find the right instruction. We could use "gt:DI" right in the
2161    define_expand, but leaving it out allows us to handle DI, SI, etc.  */
2162
2163 static int
2164 gen_v9_scc (rtx dest, enum rtx_code compare_code, rtx x, rtx y)
2165 {
2166   if (! TARGET_ARCH64
2167       && (GET_MODE (x) == DImode
2168           || GET_MODE (dest) == DImode))
2169     return 0;
2170
2171   /* Try to use the movrCC insns.  */
2172   if (TARGET_ARCH64
2173       && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT
2174       && y == const0_rtx
2175       && v9_regcmp_p (compare_code))
2176     {
2177       rtx op0 = x;
2178       rtx temp;
2179
2180       /* Special case for op0 != 0.  This can be done with one instruction if
2181          dest == x.  */
2182
2183       if (compare_code == NE
2184           && GET_MODE (dest) == DImode
2185           && rtx_equal_p (op0, dest))
2186         {
2187           emit_insn (gen_rtx_SET (VOIDmode, dest,
2188                               gen_rtx_IF_THEN_ELSE (DImode,
2189                                        gen_rtx_fmt_ee (compare_code, DImode,
2190                                                        op0, const0_rtx),
2191                                        const1_rtx,
2192                                        dest)));
2193           return 1;
2194         }
2195
2196       if (reg_overlap_mentioned_p (dest, op0))
2197         {
2198           /* Handle the case where dest == x.
2199              We "early clobber" the result.  */
2200           op0 = gen_reg_rtx (GET_MODE (x));
2201           emit_move_insn (op0, x);
2202         }
2203
2204       emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
2205       if (GET_MODE (op0) != DImode)
2206         {
2207           temp = gen_reg_rtx (DImode);
2208           convert_move (temp, op0, 0);
2209         }
2210       else
2211         temp = op0;
2212       emit_insn (gen_rtx_SET (VOIDmode, dest,
2213                           gen_rtx_IF_THEN_ELSE (GET_MODE (dest),
2214                                    gen_rtx_fmt_ee (compare_code, DImode,
2215                                                    temp, const0_rtx),
2216                                    const1_rtx,
2217                                    dest)));
2218       return 1;
2219     }
2220   else
2221     {
2222       x = gen_compare_reg_1 (compare_code, x, y);
2223       y = const0_rtx;
2224
2225       gcc_assert (GET_MODE (x) != CC_NOOVmode
2226                   && GET_MODE (x) != CCX_NOOVmode);
2227
2228       emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
2229       emit_insn (gen_rtx_SET (VOIDmode, dest,
2230                           gen_rtx_IF_THEN_ELSE (GET_MODE (dest),
2231                                    gen_rtx_fmt_ee (compare_code,
2232                                                    GET_MODE (x), x, y),
2233                                     const1_rtx, dest)));
2234       return 1;
2235     }
2236 }
2237
2238
2239 /* Emit an scc insn.  For seq, sne, sgeu, and sltu, we can do this
2240    without jumps using the addx/subx instructions.  */
2241
2242 bool
2243 emit_scc_insn (rtx operands[])
2244 {
2245   rtx tem;
2246   rtx x;
2247   rtx y;
2248   enum rtx_code code;
2249
2250   /* The quad-word fp compare library routines all return nonzero to indicate
2251      true, which is different from the equivalent libgcc routines, so we must
2252      handle them specially here.  */
2253   if (GET_MODE (operands[2]) == TFmode && ! TARGET_HARD_QUAD)
2254     {
2255       operands[1] = sparc_emit_float_lib_cmp (operands[2], operands[3],
2256                                               GET_CODE (operands[1]));
2257       operands[2] = XEXP (operands[1], 0);
2258       operands[3] = XEXP (operands[1], 1);
2259     }
2260
2261   code = GET_CODE (operands[1]);
2262   x = operands[2];
2263   y = operands[3];
2264
2265   /* For seq/sne on v9 we use the same code as v8 (the addx/subx method has
2266      more applications).  The exception to this is "reg != 0" which can
2267      be done in one instruction on v9 (so we do it).  */
2268   if (code == EQ)
2269     {
2270       if (GET_MODE (x) == SImode)
2271         {
2272           rtx pat = gen_seqsi_special (operands[0], x, y);
2273           emit_insn (pat);
2274           return true;
2275         }
2276       else if (GET_MODE (x) == DImode)
2277         {
2278           rtx pat = gen_seqdi_special (operands[0], x, y);
2279           emit_insn (pat);
2280           return true;
2281         }
2282     }
2283
2284   if (code == NE)
2285     {
2286       if (GET_MODE (x) == SImode)
2287         {
2288           rtx pat = gen_snesi_special (operands[0], x, y);
2289           emit_insn (pat);
2290           return true;
2291         }
2292       else if (GET_MODE (x) == DImode)
2293         {
2294           rtx pat = gen_snedi_special (operands[0], x, y);
2295           emit_insn (pat);
2296           return true;
2297         }
2298     }
2299
2300   /* For the rest, on v9 we can use conditional moves.  */
2301
2302   if (TARGET_V9)
2303     {
2304       if (gen_v9_scc (operands[0], code, x, y))
2305         return true;
2306     }
2307
2308   /* We can do LTU and GEU using the addx/subx instructions too.  And
2309      for GTU/LEU, if both operands are registers swap them and fall
2310      back to the easy case.  */
2311   if (code == GTU || code == LEU)
2312     {
2313       if ((GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2314           && (GET_CODE (y) == REG || GET_CODE (y) == SUBREG))
2315         {
2316           tem = x;
2317           x = y;
2318           y = tem;
2319           code = swap_condition (code);
2320         }
2321     }
2322
2323   if (code == LTU || code == GEU)
2324     {
2325       emit_insn (gen_rtx_SET (VOIDmode, operands[0],
2326                               gen_rtx_fmt_ee (code, SImode,
2327                                               gen_compare_reg_1 (code, x, y),
2328                                               const0_rtx)));
2329       return true;
2330     }
2331
2332   /* Nope, do branches.  */
2333   return false;
2334 }
2335
2336 /* Emit a conditional jump insn for the v9 architecture using comparison code
2337    CODE and jump target LABEL.
2338    This function exists to take advantage of the v9 brxx insns.  */
2339
2340 static void
2341 emit_v9_brxx_insn (enum rtx_code code, rtx op0, rtx label)
2342 {
2343   emit_jump_insn (gen_rtx_SET (VOIDmode,
2344                            pc_rtx,
2345                            gen_rtx_IF_THEN_ELSE (VOIDmode,
2346                                     gen_rtx_fmt_ee (code, GET_MODE (op0),
2347                                                     op0, const0_rtx),
2348                                     gen_rtx_LABEL_REF (VOIDmode, label),
2349                                     pc_rtx)));
2350 }
2351
2352 void
2353 emit_conditional_branch_insn (rtx operands[])
2354 {
2355   /* The quad-word fp compare library routines all return nonzero to indicate
2356      true, which is different from the equivalent libgcc routines, so we must
2357      handle them specially here.  */
2358   if (GET_MODE (operands[1]) == TFmode && ! TARGET_HARD_QUAD)
2359     {
2360       operands[0] = sparc_emit_float_lib_cmp (operands[1], operands[2],
2361                                               GET_CODE (operands[0]));
2362       operands[1] = XEXP (operands[0], 0);
2363       operands[2] = XEXP (operands[0], 1);
2364     }
2365
2366   if (TARGET_ARCH64 && operands[2] == const0_rtx
2367       && GET_CODE (operands[1]) == REG
2368       && GET_MODE (operands[1]) == DImode)
2369     {
2370       emit_v9_brxx_insn (GET_CODE (operands[0]), operands[1], operands[3]);
2371       return;
2372     }
2373
2374   operands[1] = gen_compare_reg (operands[0]);
2375   operands[2] = const0_rtx;
2376   operands[0] = gen_rtx_fmt_ee (GET_CODE (operands[0]), VOIDmode,
2377                                 operands[1], operands[2]);
2378   emit_jump_insn (gen_cbranchcc4 (operands[0], operands[1], operands[2],
2379                                   operands[3]));
2380 }
2381
2382
2383 /* Generate a DFmode part of a hard TFmode register.
2384    REG is the TFmode hard register, LOW is 1 for the
2385    low 64bit of the register and 0 otherwise.
2386  */
2387 rtx
2388 gen_df_reg (rtx reg, int low)
2389 {
2390   int regno = REGNO (reg);
2391
2392   if ((WORDS_BIG_ENDIAN == 0) ^ (low != 0))
2393     regno += (TARGET_ARCH64 && regno < 32) ? 1 : 2;
2394   return gen_rtx_REG (DFmode, regno);
2395 }
2396 \f
2397 /* Generate a call to FUNC with OPERANDS.  Operand 0 is the return value.
2398    Unlike normal calls, TFmode operands are passed by reference.  It is
2399    assumed that no more than 3 operands are required.  */
2400
2401 static void
2402 emit_soft_tfmode_libcall (const char *func_name, int nargs, rtx *operands)
2403 {
2404   rtx ret_slot = NULL, arg[3], func_sym;
2405   int i;
2406
2407   /* We only expect to be called for conversions, unary, and binary ops.  */
2408   gcc_assert (nargs == 2 || nargs == 3);
2409
2410   for (i = 0; i < nargs; ++i)
2411     {
2412       rtx this_arg = operands[i];
2413       rtx this_slot;
2414
2415       /* TFmode arguments and return values are passed by reference.  */
2416       if (GET_MODE (this_arg) == TFmode)
2417         {
2418           int force_stack_temp;
2419
2420           force_stack_temp = 0;
2421           if (TARGET_BUGGY_QP_LIB && i == 0)
2422             force_stack_temp = 1;
2423
2424           if (GET_CODE (this_arg) == MEM
2425               && ! force_stack_temp)
2426             this_arg = XEXP (this_arg, 0);
2427           else if (CONSTANT_P (this_arg)
2428                    && ! force_stack_temp)
2429             {
2430               this_slot = force_const_mem (TFmode, this_arg);
2431               this_arg = XEXP (this_slot, 0);
2432             }
2433           else
2434             {
2435               this_slot = assign_stack_temp (TFmode, GET_MODE_SIZE (TFmode), 0);
2436
2437               /* Operand 0 is the return value.  We'll copy it out later.  */
2438               if (i > 0)
2439                 emit_move_insn (this_slot, this_arg);
2440               else
2441                 ret_slot = this_slot;
2442
2443               this_arg = XEXP (this_slot, 0);
2444             }
2445         }
2446
2447       arg[i] = this_arg;
2448     }
2449
2450   func_sym = gen_rtx_SYMBOL_REF (Pmode, func_name);
2451
2452   if (GET_MODE (operands[0]) == TFmode)
2453     {
2454       if (nargs == 2)
2455         emit_library_call (func_sym, LCT_NORMAL, VOIDmode, 2,
2456                            arg[0], GET_MODE (arg[0]),
2457                            arg[1], GET_MODE (arg[1]));
2458       else
2459         emit_library_call (func_sym, LCT_NORMAL, VOIDmode, 3,
2460                            arg[0], GET_MODE (arg[0]),
2461                            arg[1], GET_MODE (arg[1]),
2462                            arg[2], GET_MODE (arg[2]));
2463
2464       if (ret_slot)
2465         emit_move_insn (operands[0], ret_slot);
2466     }
2467   else
2468     {
2469       rtx ret;
2470
2471       gcc_assert (nargs == 2);
2472
2473       ret = emit_library_call_value (func_sym, operands[0], LCT_NORMAL,
2474                                      GET_MODE (operands[0]), 1,
2475                                      arg[1], GET_MODE (arg[1]));
2476
2477       if (ret != operands[0])
2478         emit_move_insn (operands[0], ret);
2479     }
2480 }
2481
2482 /* Expand soft-float TFmode calls to sparc abi routines.  */
2483
2484 static void
2485 emit_soft_tfmode_binop (enum rtx_code code, rtx *operands)
2486 {
2487   const char *func;
2488
2489   switch (code)
2490     {
2491     case PLUS:
2492       func = "_Qp_add";
2493       break;
2494     case MINUS:
2495       func = "_Qp_sub";
2496       break;
2497     case MULT:
2498       func = "_Qp_mul";
2499       break;
2500     case DIV:
2501       func = "_Qp_div";
2502       break;
2503     default:
2504       gcc_unreachable ();
2505     }
2506
2507   emit_soft_tfmode_libcall (func, 3, operands);
2508 }
2509
2510 static void
2511 emit_soft_tfmode_unop (enum rtx_code code, rtx *operands)
2512 {
2513   const char *func;
2514
2515   gcc_assert (code == SQRT);
2516   func = "_Qp_sqrt";
2517
2518   emit_soft_tfmode_libcall (func, 2, operands);
2519 }
2520
2521 static void
2522 emit_soft_tfmode_cvt (enum rtx_code code, rtx *operands)
2523 {
2524   const char *func;
2525
2526   switch (code)
2527     {
2528     case FLOAT_EXTEND:
2529       switch (GET_MODE (operands[1]))
2530         {
2531         case SFmode:
2532           func = "_Qp_stoq";
2533           break;
2534         case DFmode:
2535           func = "_Qp_dtoq";
2536           break;
2537         default:
2538           gcc_unreachable ();
2539         }
2540       break;
2541
2542     case FLOAT_TRUNCATE:
2543       switch (GET_MODE (operands[0]))
2544         {
2545         case SFmode:
2546           func = "_Qp_qtos";
2547           break;
2548         case DFmode:
2549           func = "_Qp_qtod";
2550           break;
2551         default:
2552           gcc_unreachable ();
2553         }
2554       break;
2555
2556     case FLOAT:
2557       switch (GET_MODE (operands[1]))
2558         {
2559         case SImode:
2560           func = "_Qp_itoq";
2561           if (TARGET_ARCH64)
2562             operands[1] = gen_rtx_SIGN_EXTEND (DImode, operands[1]);
2563           break;
2564         case DImode:
2565           func = "_Qp_xtoq";
2566           break;
2567         default:
2568           gcc_unreachable ();
2569         }
2570       break;
2571
2572     case UNSIGNED_FLOAT:
2573       switch (GET_MODE (operands[1]))
2574         {
2575         case SImode:
2576           func = "_Qp_uitoq";
2577           if (TARGET_ARCH64)
2578             operands[1] = gen_rtx_ZERO_EXTEND (DImode, operands[1]);
2579           break;
2580         case DImode:
2581           func = "_Qp_uxtoq";
2582           break;
2583         default:
2584           gcc_unreachable ();
2585         }
2586       break;
2587
2588     case FIX:
2589       switch (GET_MODE (operands[0]))
2590         {
2591         case SImode:
2592           func = "_Qp_qtoi";
2593           break;
2594         case DImode:
2595           func = "_Qp_qtox";
2596           break;
2597         default:
2598           gcc_unreachable ();
2599         }
2600       break;
2601
2602     case UNSIGNED_FIX:
2603       switch (GET_MODE (operands[0]))
2604         {
2605         case SImode:
2606           func = "_Qp_qtoui";
2607           break;
2608         case DImode:
2609           func = "_Qp_qtoux";
2610           break;
2611         default:
2612           gcc_unreachable ();
2613         }
2614       break;
2615
2616     default:
2617       gcc_unreachable ();
2618     }
2619
2620   emit_soft_tfmode_libcall (func, 2, operands);
2621 }
2622
2623 /* Expand a hard-float tfmode operation.  All arguments must be in
2624    registers.  */
2625
2626 static void
2627 emit_hard_tfmode_operation (enum rtx_code code, rtx *operands)
2628 {
2629   rtx op, dest;
2630
2631   if (GET_RTX_CLASS (code) == RTX_UNARY)
2632     {
2633       operands[1] = force_reg (GET_MODE (operands[1]), operands[1]);
2634       op = gen_rtx_fmt_e (code, GET_MODE (operands[0]), operands[1]);
2635     }
2636   else
2637     {
2638       operands[1] = force_reg (GET_MODE (operands[1]), operands[1]);
2639       operands[2] = force_reg (GET_MODE (operands[2]), operands[2]);
2640       op = gen_rtx_fmt_ee (code, GET_MODE (operands[0]),
2641                            operands[1], operands[2]);
2642     }
2643
2644   if (register_operand (operands[0], VOIDmode))
2645     dest = operands[0];
2646   else
2647     dest = gen_reg_rtx (GET_MODE (operands[0]));
2648
2649   emit_insn (gen_rtx_SET (VOIDmode, dest, op));
2650
2651   if (dest != operands[0])
2652     emit_move_insn (operands[0], dest);
2653 }
2654
2655 void
2656 emit_tfmode_binop (enum rtx_code code, rtx *operands)
2657 {
2658   if (TARGET_HARD_QUAD)
2659     emit_hard_tfmode_operation (code, operands);
2660   else
2661     emit_soft_tfmode_binop (code, operands);
2662 }
2663
2664 void
2665 emit_tfmode_unop (enum rtx_code code, rtx *operands)
2666 {
2667   if (TARGET_HARD_QUAD)
2668     emit_hard_tfmode_operation (code, operands);
2669   else
2670     emit_soft_tfmode_unop (code, operands);
2671 }
2672
2673 void
2674 emit_tfmode_cvt (enum rtx_code code, rtx *operands)
2675 {
2676   if (TARGET_HARD_QUAD)
2677     emit_hard_tfmode_operation (code, operands);
2678   else
2679     emit_soft_tfmode_cvt (code, operands);
2680 }
2681 \f
2682 /* Return nonzero if a branch/jump/call instruction will be emitting
2683    nop into its delay slot.  */
2684
2685 int
2686 empty_delay_slot (rtx insn)
2687 {
2688   rtx seq;
2689
2690   /* If no previous instruction (should not happen), return true.  */
2691   if (PREV_INSN (insn) == NULL)
2692     return 1;
2693
2694   seq = NEXT_INSN (PREV_INSN (insn));
2695   if (GET_CODE (PATTERN (seq)) == SEQUENCE)
2696     return 0;
2697
2698   return 1;
2699 }
2700
2701 /* Return nonzero if TRIAL can go into the call delay slot.  */
2702
2703 int
2704 tls_call_delay (rtx trial)
2705 {
2706   rtx pat;
2707
2708   /* Binutils allows
2709        call __tls_get_addr, %tgd_call (foo)
2710         add %l7, %o0, %o0, %tgd_add (foo)
2711      while Sun as/ld does not.  */
2712   if (TARGET_GNU_TLS || !TARGET_TLS)
2713     return 1;
2714
2715   pat = PATTERN (trial);
2716
2717   /* We must reject tgd_add{32|64}, i.e.
2718        (set (reg) (plus (reg) (unspec [(reg) (symbol_ref)] UNSPEC_TLSGD)))
2719      and tldm_add{32|64}, i.e.
2720        (set (reg) (plus (reg) (unspec [(reg) (symbol_ref)] UNSPEC_TLSLDM)))
2721      for Sun as/ld.  */
2722   if (GET_CODE (pat) == SET
2723       && GET_CODE (SET_SRC (pat)) == PLUS)
2724     {
2725       rtx unspec = XEXP (SET_SRC (pat), 1);
2726
2727       if (GET_CODE (unspec) == UNSPEC
2728           && (XINT (unspec, 1) == UNSPEC_TLSGD
2729               || XINT (unspec, 1) == UNSPEC_TLSLDM))
2730         return 0;
2731     }
2732
2733   return 1;
2734 }
2735
2736 /* Return nonzero if TRIAL, an insn, can be combined with a 'restore'
2737    instruction.  RETURN_P is true if the v9 variant 'return' is to be
2738    considered in the test too.
2739
2740    TRIAL must be a SET whose destination is a REG appropriate for the
2741    'restore' instruction or, if RETURN_P is true, for the 'return'
2742    instruction.  */
2743
2744 static int
2745 eligible_for_restore_insn (rtx trial, bool return_p)
2746 {
2747   rtx pat = PATTERN (trial);
2748   rtx src = SET_SRC (pat);
2749
2750   /* The 'restore src,%g0,dest' pattern for word mode and below.  */
2751   if (GET_MODE_CLASS (GET_MODE (src)) != MODE_FLOAT
2752       && arith_operand (src, GET_MODE (src)))
2753     {
2754       if (TARGET_ARCH64)
2755         return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
2756       else
2757         return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (SImode);
2758     }
2759
2760   /* The 'restore src,%g0,dest' pattern for double-word mode.  */
2761   else if (GET_MODE_CLASS (GET_MODE (src)) != MODE_FLOAT
2762            && arith_double_operand (src, GET_MODE (src)))
2763     return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
2764
2765   /* The 'restore src,%g0,dest' pattern for float if no FPU.  */
2766   else if (! TARGET_FPU && register_operand (src, SFmode))
2767     return 1;
2768
2769   /* The 'restore src,%g0,dest' pattern for double if no FPU.  */
2770   else if (! TARGET_FPU && TARGET_ARCH64 && register_operand (src, DFmode))
2771     return 1;
2772
2773   /* If we have the 'return' instruction, anything that does not use
2774      local or output registers and can go into a delay slot wins.  */
2775   else if (return_p && TARGET_V9 && ! epilogue_renumber (&pat, 1)
2776            && (get_attr_in_uncond_branch_delay (trial)
2777                == IN_UNCOND_BRANCH_DELAY_TRUE))
2778     return 1;
2779
2780   /* The 'restore src1,src2,dest' pattern for SImode.  */
2781   else if (GET_CODE (src) == PLUS
2782            && register_operand (XEXP (src, 0), SImode)
2783            && arith_operand (XEXP (src, 1), SImode))
2784     return 1;
2785
2786   /* The 'restore src1,src2,dest' pattern for DImode.  */
2787   else if (GET_CODE (src) == PLUS
2788            && register_operand (XEXP (src, 0), DImode)
2789            && arith_double_operand (XEXP (src, 1), DImode))
2790     return 1;
2791
2792   /* The 'restore src1,%lo(src2),dest' pattern.  */
2793   else if (GET_CODE (src) == LO_SUM
2794            && ! TARGET_CM_MEDMID
2795            && ((register_operand (XEXP (src, 0), SImode)
2796                 && immediate_operand (XEXP (src, 1), SImode))
2797                || (TARGET_ARCH64
2798                    && register_operand (XEXP (src, 0), DImode)
2799                    && immediate_operand (XEXP (src, 1), DImode))))
2800     return 1;
2801
2802   /* The 'restore src,src,dest' pattern.  */
2803   else if (GET_CODE (src) == ASHIFT
2804            && (register_operand (XEXP (src, 0), SImode)
2805                || register_operand (XEXP (src, 0), DImode))
2806            && XEXP (src, 1) == const1_rtx)
2807     return 1;
2808
2809   return 0;
2810 }
2811
2812 /* Return nonzero if TRIAL can go into the function return's
2813    delay slot.  */
2814
2815 int
2816 eligible_for_return_delay (rtx trial)
2817 {
2818   rtx pat;
2819
2820   if (GET_CODE (trial) != INSN || GET_CODE (PATTERN (trial)) != SET)
2821     return 0;
2822
2823   if (get_attr_length (trial) != 1)
2824     return 0;
2825
2826   /* If there are any call-saved registers, we should scan TRIAL if it
2827      does not reference them.  For now just make it easy.  */
2828   if (num_gfregs)
2829     return 0;
2830
2831   /* If the function uses __builtin_eh_return, the eh_return machinery
2832      occupies the delay slot.  */
2833   if (crtl->calls_eh_return)
2834     return 0;
2835
2836   /* In the case of a true leaf function, anything can go into the slot.  */
2837   if (sparc_leaf_function_p)
2838     return get_attr_in_uncond_branch_delay (trial)
2839            == IN_UNCOND_BRANCH_DELAY_TRUE;
2840
2841   pat = PATTERN (trial);
2842
2843   /* Otherwise, only operations which can be done in tandem with
2844      a `restore' or `return' insn can go into the delay slot.  */
2845   if (GET_CODE (SET_DEST (pat)) != REG
2846       || (REGNO (SET_DEST (pat)) >= 8 && REGNO (SET_DEST (pat)) < 24))
2847     return 0;
2848
2849   /* If this instruction sets up floating point register and we have a return
2850      instruction, it can probably go in.  But restore will not work
2851      with FP_REGS.  */
2852   if (REGNO (SET_DEST (pat)) >= 32)
2853     return (TARGET_V9
2854             && ! epilogue_renumber (&pat, 1)
2855             && (get_attr_in_uncond_branch_delay (trial)
2856                 == IN_UNCOND_BRANCH_DELAY_TRUE));
2857
2858   return eligible_for_restore_insn (trial, true);
2859 }
2860
2861 /* Return nonzero if TRIAL can go into the sibling call's
2862    delay slot.  */
2863
2864 int
2865 eligible_for_sibcall_delay (rtx trial)
2866 {
2867   rtx pat;
2868
2869   if (GET_CODE (trial) != INSN || GET_CODE (PATTERN (trial)) != SET)
2870     return 0;
2871
2872   if (get_attr_length (trial) != 1)
2873     return 0;
2874
2875   pat = PATTERN (trial);
2876
2877   if (sparc_leaf_function_p)
2878     {
2879       /* If the tail call is done using the call instruction,
2880          we have to restore %o7 in the delay slot.  */
2881       if (LEAF_SIBCALL_SLOT_RESERVED_P)
2882         return 0;
2883
2884       /* %g1 is used to build the function address */
2885       if (reg_mentioned_p (gen_rtx_REG (Pmode, 1), pat))
2886         return 0;
2887
2888       return 1;
2889     }
2890
2891   /* Otherwise, only operations which can be done in tandem with
2892      a `restore' insn can go into the delay slot.  */
2893   if (GET_CODE (SET_DEST (pat)) != REG
2894       || (REGNO (SET_DEST (pat)) >= 8 && REGNO (SET_DEST (pat)) < 24)
2895       || REGNO (SET_DEST (pat)) >= 32)
2896     return 0;
2897
2898   /* If it mentions %o7, it can't go in, because sibcall will clobber it
2899      in most cases.  */
2900   if (reg_mentioned_p (gen_rtx_REG (Pmode, 15), pat))
2901     return 0;
2902
2903   return eligible_for_restore_insn (trial, false);
2904 }
2905
2906 int
2907 short_branch (int uid1, int uid2)
2908 {
2909   int delta = INSN_ADDRESSES (uid1) - INSN_ADDRESSES (uid2);
2910
2911   /* Leave a few words of "slop".  */
2912   if (delta >= -1023 && delta <= 1022)
2913     return 1;
2914
2915   return 0;
2916 }
2917
2918 /* Return nonzero if REG is not used after INSN.
2919    We assume REG is a reload reg, and therefore does
2920    not live past labels or calls or jumps.  */
2921 int
2922 reg_unused_after (rtx reg, rtx insn)
2923 {
2924   enum rtx_code code, prev_code = UNKNOWN;
2925
2926   while ((insn = NEXT_INSN (insn)))
2927     {
2928       if (prev_code == CALL_INSN && call_used_regs[REGNO (reg)])
2929         return 1;
2930
2931       code = GET_CODE (insn);
2932       if (GET_CODE (insn) == CODE_LABEL)
2933         return 1;
2934
2935       if (INSN_P (insn))
2936         {
2937           rtx set = single_set (insn);
2938           int in_src = set && reg_overlap_mentioned_p (reg, SET_SRC (set));
2939           if (set && in_src)
2940             return 0;
2941           if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2942             return 1;
2943           if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
2944             return 0;
2945         }
2946       prev_code = code;
2947     }
2948   return 1;
2949 }
2950 \f
2951 /* Determine if it's legal to put X into the constant pool.  This
2952    is not possible if X contains the address of a symbol that is
2953    not constant (TLS) or not known at final link time (PIC).  */
2954
2955 static bool
2956 sparc_cannot_force_const_mem (rtx x)
2957 {
2958   switch (GET_CODE (x))
2959     {
2960     case CONST_INT:
2961     case CONST_DOUBLE:
2962     case CONST_VECTOR:
2963       /* Accept all non-symbolic constants.  */
2964       return false;
2965
2966     case LABEL_REF:
2967       /* Labels are OK iff we are non-PIC.  */
2968       return flag_pic != 0;
2969
2970     case SYMBOL_REF:
2971       /* 'Naked' TLS symbol references are never OK,
2972          non-TLS symbols are OK iff we are non-PIC.  */
2973       if (SYMBOL_REF_TLS_MODEL (x))
2974         return true;
2975       else
2976         return flag_pic != 0;
2977
2978     case CONST:
2979       return sparc_cannot_force_const_mem (XEXP (x, 0));
2980     case PLUS:
2981     case MINUS:
2982       return sparc_cannot_force_const_mem (XEXP (x, 0))
2983          || sparc_cannot_force_const_mem (XEXP (x, 1));
2984     case UNSPEC:
2985       return true;
2986     default:
2987       gcc_unreachable ();
2988     }
2989 }
2990 \f
2991 /* PIC support.  */
2992 static GTY(()) bool pic_helper_needed = false;
2993 static GTY(()) rtx pic_helper_symbol;
2994 static GTY(()) rtx global_offset_table;
2995
2996 /* Ensure that we are not using patterns that are not OK with PIC.  */
2997
2998 int
2999 check_pic (int i)
3000 {
3001   switch (flag_pic)
3002     {
3003     case 1:
3004       gcc_assert (GET_CODE (recog_data.operand[i]) != SYMBOL_REF
3005                   && (GET_CODE (recog_data.operand[i]) != CONST
3006                   || (GET_CODE (XEXP (recog_data.operand[i], 0)) == MINUS
3007                       && (XEXP (XEXP (recog_data.operand[i], 0), 0)
3008                           == global_offset_table)
3009                       && (GET_CODE (XEXP (XEXP (recog_data.operand[i], 0), 1))
3010                           == CONST))));
3011     case 2:
3012     default:
3013       return 1;
3014     }
3015 }
3016
3017 /* Return true if X is an address which needs a temporary register when
3018    reloaded while generating PIC code.  */
3019
3020 int
3021 pic_address_needs_scratch (rtx x)
3022 {
3023   /* An address which is a symbolic plus a non SMALL_INT needs a temp reg.  */
3024   if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
3025       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
3026       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3027       && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
3028     return 1;
3029
3030   return 0;
3031 }
3032
3033 /* Determine if a given RTX is a valid constant.  We already know this
3034    satisfies CONSTANT_P.  */
3035
3036 bool
3037 legitimate_constant_p (rtx x)
3038 {
3039   switch (GET_CODE (x))
3040     {
3041     case CONST:
3042     case SYMBOL_REF:
3043       if (sparc_tls_referenced_p (x))
3044         return false;
3045       break;
3046
3047     case CONST_DOUBLE:
3048       if (GET_MODE (x) == VOIDmode)
3049         return true;
3050
3051       /* Floating point constants are generally not ok.
3052          The only exception is 0.0 in VIS.  */
3053       if (TARGET_VIS
3054           && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3055           && const_zero_operand (x, GET_MODE (x)))
3056         return true;
3057
3058       return false;
3059
3060     case CONST_VECTOR:
3061       /* Vector constants are generally not ok.
3062          The only exception is 0 in VIS.  */
3063       if (TARGET_VIS
3064           && const_zero_operand (x, GET_MODE (x)))
3065         return true;
3066
3067       return false;
3068
3069     default:
3070       break;
3071     }
3072
3073   return true;
3074 }
3075
3076 /* Determine if a given RTX is a valid constant address.  */
3077
3078 bool
3079 constant_address_p (rtx x)
3080 {
3081   switch (GET_CODE (x))
3082     {
3083     case LABEL_REF:
3084     case CONST_INT:
3085     case HIGH:
3086       return true;
3087
3088     case CONST:
3089       if (flag_pic && pic_address_needs_scratch (x))
3090         return false;
3091       return legitimate_constant_p (x);
3092
3093     case SYMBOL_REF:
3094       return !flag_pic && legitimate_constant_p (x);
3095
3096     default:
3097       return false;
3098     }
3099 }
3100
3101 /* Nonzero if the constant value X is a legitimate general operand
3102    when generating PIC code.  It is given that flag_pic is on and
3103    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
3104
3105 bool
3106 legitimate_pic_operand_p (rtx x)
3107 {
3108   if (pic_address_needs_scratch (x))
3109     return false;
3110   if (sparc_tls_referenced_p (x))
3111     return false;
3112   return true;
3113 }
3114
3115 /* Return nonzero if ADDR is a valid memory address.
3116    STRICT specifies whether strict register checking applies.  */
3117
3118 static bool
3119 sparc_legitimate_address_p (enum machine_mode mode, rtx addr, bool strict)
3120 {
3121   rtx rs1 = NULL, rs2 = NULL, imm1 = NULL;
3122
3123   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
3124     rs1 = addr;
3125   else if (GET_CODE (addr) == PLUS)
3126     {
3127       rs1 = XEXP (addr, 0);
3128       rs2 = XEXP (addr, 1);
3129
3130       /* Canonicalize.  REG comes first, if there are no regs,
3131          LO_SUM comes first.  */
3132       if (!REG_P (rs1)
3133           && GET_CODE (rs1) != SUBREG
3134           && (REG_P (rs2)
3135               || GET_CODE (rs2) == SUBREG
3136               || (GET_CODE (rs2) == LO_SUM && GET_CODE (rs1) != LO_SUM)))
3137         {
3138           rs1 = XEXP (addr, 1);
3139           rs2 = XEXP (addr, 0);
3140         }
3141
3142       if ((flag_pic == 1
3143            && rs1 == pic_offset_table_rtx
3144            && !REG_P (rs2)
3145            && GET_CODE (rs2) != SUBREG
3146            && GET_CODE (rs2) != LO_SUM
3147            && GET_CODE (rs2) != MEM
3148            && !(GET_CODE (rs2) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs2))
3149            && (! symbolic_operand (rs2, VOIDmode) || mode == Pmode)
3150            && (GET_CODE (rs2) != CONST_INT || SMALL_INT (rs2)))
3151           || ((REG_P (rs1)
3152                || GET_CODE (rs1) == SUBREG)
3153               && RTX_OK_FOR_OFFSET_P (rs2)))
3154         {
3155           imm1 = rs2;
3156           rs2 = NULL;
3157         }
3158       else if ((REG_P (rs1) || GET_CODE (rs1) == SUBREG)
3159                && (REG_P (rs2) || GET_CODE (rs2) == SUBREG))
3160         {
3161           /* We prohibit REG + REG for TFmode when there are no quad move insns
3162              and we consequently need to split.  We do this because REG+REG
3163              is not an offsettable address.  If we get the situation in reload
3164              where source and destination of a movtf pattern are both MEMs with
3165              REG+REG address, then only one of them gets converted to an
3166              offsettable address.  */
3167           if (mode == TFmode
3168               && ! (TARGET_FPU && TARGET_ARCH64 && TARGET_HARD_QUAD))
3169             return 0;
3170
3171           /* We prohibit REG + REG on ARCH32 if not optimizing for
3172              DFmode/DImode because then mem_min_alignment is likely to be zero
3173              after reload and the  forced split would lack a matching splitter
3174              pattern.  */
3175           if (TARGET_ARCH32 && !optimize
3176               && (mode == DFmode || mode == DImode))
3177             return 0;
3178         }
3179       else if (USE_AS_OFFSETABLE_LO10
3180                && GET_CODE (rs1) == LO_SUM
3181                && TARGET_ARCH64
3182                && ! TARGET_CM_MEDMID
3183                && RTX_OK_FOR_OLO10_P (rs2))
3184         {
3185           rs2 = NULL;
3186           imm1 = XEXP (rs1, 1);
3187           rs1 = XEXP (rs1, 0);
3188           if (!CONSTANT_P (imm1)
3189               || (GET_CODE (rs1) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs1)))
3190             return 0;
3191         }
3192     }
3193   else if (GET_CODE (addr) == LO_SUM)
3194     {
3195       rs1 = XEXP (addr, 0);
3196       imm1 = XEXP (addr, 1);
3197
3198       if (!CONSTANT_P (imm1)
3199           || (GET_CODE (rs1) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs1)))
3200         return 0;
3201
3202       /* We can't allow TFmode in 32-bit mode, because an offset greater
3203          than the alignment (8) may cause the LO_SUM to overflow.  */
3204       if (mode == TFmode && TARGET_ARCH32)
3205         return 0;
3206     }
3207   else if (GET_CODE (addr) == CONST_INT && SMALL_INT (addr))
3208     return 1;
3209   else
3210     return 0;
3211
3212   if (GET_CODE (rs1) == SUBREG)
3213     rs1 = SUBREG_REG (rs1);
3214   if (!REG_P (rs1))
3215     return 0;
3216
3217   if (rs2)
3218     {
3219       if (GET_CODE (rs2) == SUBREG)
3220         rs2 = SUBREG_REG (rs2);
3221       if (!REG_P (rs2))
3222         return 0;
3223     }
3224
3225   if (strict)
3226     {
3227       if (!REGNO_OK_FOR_BASE_P (REGNO (rs1))
3228           || (rs2 && !REGNO_OK_FOR_BASE_P (REGNO (rs2))))
3229         return 0;
3230     }
3231   else
3232     {
3233       if ((REGNO (rs1) >= 32
3234            && REGNO (rs1) != FRAME_POINTER_REGNUM
3235            && REGNO (rs1) < FIRST_PSEUDO_REGISTER)
3236           || (rs2
3237               && (REGNO (rs2) >= 32
3238                   && REGNO (rs2) != FRAME_POINTER_REGNUM
3239                   && REGNO (rs2) < FIRST_PSEUDO_REGISTER)))
3240         return 0;
3241     }
3242   return 1;
3243 }
3244
3245 /* Construct the SYMBOL_REF for the tls_get_offset function.  */
3246
3247 static GTY(()) rtx sparc_tls_symbol;
3248
3249 static rtx
3250 sparc_tls_get_addr (void)
3251 {
3252   if (!sparc_tls_symbol)
3253     sparc_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_addr");
3254
3255   return sparc_tls_symbol;
3256 }
3257
3258 static rtx
3259 sparc_tls_got (void)
3260 {
3261   rtx temp;
3262   if (flag_pic)
3263     {
3264       crtl->uses_pic_offset_table = 1;
3265       return pic_offset_table_rtx;
3266     }
3267
3268   if (!global_offset_table)
3269     global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3270   temp = gen_reg_rtx (Pmode);
3271   emit_move_insn (temp, global_offset_table);
3272   return temp;
3273 }
3274
3275 /* Return true if X contains a thread-local symbol.  */
3276
3277 static bool
3278 sparc_tls_referenced_p (rtx x)
3279 {
3280   if (!TARGET_HAVE_TLS)
3281     return false;
3282
3283   if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS)
3284     x = XEXP (XEXP (x, 0), 0);
3285
3286   if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x))
3287     return true;
3288
3289   /* That's all we handle in sparc_legitimize_tls_address for now.  */
3290   return false;
3291 }
3292
3293 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
3294    this (thread-local) address.  */
3295
3296 static rtx
3297 sparc_legitimize_tls_address (rtx addr)
3298 {
3299   rtx temp1, temp2, temp3, ret, o0, got, insn;
3300
3301   gcc_assert (can_create_pseudo_p ());
3302
3303   if (GET_CODE (addr) == SYMBOL_REF)
3304     switch (SYMBOL_REF_TLS_MODEL (addr))
3305       {
3306       case TLS_MODEL_GLOBAL_DYNAMIC:
3307         start_sequence ();
3308         temp1 = gen_reg_rtx (SImode);
3309         temp2 = gen_reg_rtx (SImode);
3310         ret = gen_reg_rtx (Pmode);
3311         o0 = gen_rtx_REG (Pmode, 8);
3312         got = sparc_tls_got ();
3313         emit_insn (gen_tgd_hi22 (temp1, addr));
3314         emit_insn (gen_tgd_lo10 (temp2, temp1, addr));
3315         if (TARGET_ARCH32)
3316           {
3317             emit_insn (gen_tgd_add32 (o0, got, temp2, addr));
3318             insn = emit_call_insn (gen_tgd_call32 (o0, sparc_tls_get_addr (),
3319                                                    addr, const1_rtx));
3320           }
3321         else
3322           {
3323             emit_insn (gen_tgd_add64 (o0, got, temp2, addr));
3324             insn = emit_call_insn (gen_tgd_call64 (o0, sparc_tls_get_addr (),
3325                                                    addr, const1_rtx));
3326           }
3327         CALL_INSN_FUNCTION_USAGE (insn)
3328           = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_USE (VOIDmode, o0),
3329                                CALL_INSN_FUNCTION_USAGE (insn));
3330         insn = get_insns ();
3331         end_sequence ();
3332         emit_libcall_block (insn, ret, o0, addr);
3333         break;
3334
3335       case TLS_MODEL_LOCAL_DYNAMIC:
3336         start_sequence ();
3337         temp1 = gen_reg_rtx (SImode);
3338         temp2 = gen_reg_rtx (SImode);
3339         temp3 = gen_reg_rtx (Pmode);
3340         ret = gen_reg_rtx (Pmode);
3341         o0 = gen_rtx_REG (Pmode, 8);
3342         got = sparc_tls_got ();
3343         emit_insn (gen_tldm_hi22 (temp1));
3344         emit_insn (gen_tldm_lo10 (temp2, temp1));
3345         if (TARGET_ARCH32)
3346           {
3347             emit_insn (gen_tldm_add32 (o0, got, temp2));
3348             insn = emit_call_insn (gen_tldm_call32 (o0, sparc_tls_get_addr (),
3349                                                     const1_rtx));
3350           }
3351         else
3352           {
3353             emit_insn (gen_tldm_add64 (o0, got, temp2));
3354             insn = emit_call_insn (gen_tldm_call64 (o0, sparc_tls_get_addr (),
3355                                                     const1_rtx));
3356           }
3357         CALL_INSN_FUNCTION_USAGE (insn)
3358           = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_USE (VOIDmode, o0),
3359                                CALL_INSN_FUNCTION_USAGE (insn));
3360         insn = get_insns ();
3361         end_sequence ();
3362         emit_libcall_block (insn, temp3, o0,
3363                             gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3364                                             UNSPEC_TLSLD_BASE));
3365         temp1 = gen_reg_rtx (SImode);
3366         temp2 = gen_reg_rtx (SImode);
3367         emit_insn (gen_tldo_hix22 (temp1, addr));
3368         emit_insn (gen_tldo_lox10 (temp2, temp1, addr));
3369         if (TARGET_ARCH32)
3370           emit_insn (gen_tldo_add32 (ret, temp3, temp2, addr));
3371         else
3372           emit_insn (gen_tldo_add64 (ret, temp3, temp2, addr));
3373         break;
3374
3375       case TLS_MODEL_INITIAL_EXEC:
3376         temp1 = gen_reg_rtx (SImode);
3377         temp2 = gen_reg_rtx (SImode);
3378         temp3 = gen_reg_rtx (Pmode);
3379         got = sparc_tls_got ();
3380         emit_insn (gen_tie_hi22 (temp1, addr));
3381         emit_insn (gen_tie_lo10 (temp2, temp1, addr));
3382         if (TARGET_ARCH32)
3383           emit_insn (gen_tie_ld32 (temp3, got, temp2, addr));
3384         else
3385           emit_insn (gen_tie_ld64 (temp3, got, temp2, addr));
3386         if (TARGET_SUN_TLS)
3387           {
3388             ret = gen_reg_rtx (Pmode);
3389             if (TARGET_ARCH32)
3390               emit_insn (gen_tie_add32 (ret, gen_rtx_REG (Pmode, 7),
3391                                         temp3, addr));
3392             else
3393               emit_insn (gen_tie_add64 (ret, gen_rtx_REG (Pmode, 7),
3394                                         temp3, addr));
3395           }
3396         else
3397           ret = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, 7), temp3);
3398         break;
3399
3400       case TLS_MODEL_LOCAL_EXEC:
3401         temp1 = gen_reg_rtx (Pmode);
3402         temp2 = gen_reg_rtx (Pmode);
3403         if (TARGET_ARCH32)
3404           {
3405             emit_insn (gen_tle_hix22_sp32 (temp1, addr));
3406             emit_insn (gen_tle_lox10_sp32 (temp2, temp1, addr));
3407           }
3408         else
3409           {
3410             emit_insn (gen_tle_hix22_sp64 (temp1, addr));
3411             emit_insn (gen_tle_lox10_sp64 (temp2, temp1, addr));
3412           }
3413         ret = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, 7), temp2);
3414         break;
3415
3416       default:
3417         gcc_unreachable ();
3418       }
3419
3420   else if (GET_CODE (addr) == CONST)
3421     {
3422       rtx base, offset;
3423
3424       gcc_assert (GET_CODE (XEXP (addr, 0)) == PLUS);
3425
3426       base = sparc_legitimize_tls_address (XEXP (XEXP (addr, 0), 0));
3427       offset = XEXP (XEXP (addr, 0), 1);
3428
3429       base = force_operand (base, NULL_RTX);
3430       if (!(GET_CODE (offset) == CONST_INT && SMALL_INT (offset)))
3431         offset = force_reg (Pmode, offset);
3432       ret = gen_rtx_PLUS (Pmode, base, offset);
3433     }
3434
3435   else
3436     gcc_unreachable ();  /* for now ... */
3437
3438   return ret;
3439 }
3440
3441 /* Legitimize PIC addresses.  If the address is already position-independent,
3442    we return ORIG.  Newly generated position-independent addresses go into a
3443    reg.  This is REG if nonzero, otherwise we allocate register(s) as
3444    necessary.  */
3445
3446 static rtx
3447 sparc_legitimize_pic_address (rtx orig, rtx reg)
3448 {
3449   bool gotdata_op = false;
3450
3451   if (GET_CODE (orig) == SYMBOL_REF
3452       /* See the comment&nbs