OSDN Git Service

Turn UNITS_PER_SIMD_WORD into a target hook.
[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 "toplev.h"
46 #include "ggc.h"
47 #include "tm_p.h"
48 #include "debug.h"
49 #include "target.h"
50 #include "target-def.h"
51 #include "cfglayout.h"
52 #include "gimple.h"
53 #include "langhooks.h"
54 #include "reload.h"
55 #include "params.h"
56 #include "df.h"
57 #include "dwarf2out.h"
58
59 /* Processor costs */
60 static const
61 struct processor_costs cypress_costs = {
62   COSTS_N_INSNS (2), /* int load */
63   COSTS_N_INSNS (2), /* int signed load */
64   COSTS_N_INSNS (2), /* int zeroed load */
65   COSTS_N_INSNS (2), /* float load */
66   COSTS_N_INSNS (5), /* fmov, fneg, fabs */
67   COSTS_N_INSNS (5), /* fadd, fsub */
68   COSTS_N_INSNS (1), /* fcmp */
69   COSTS_N_INSNS (1), /* fmov, fmovr */
70   COSTS_N_INSNS (7), /* fmul */
71   COSTS_N_INSNS (37), /* fdivs */
72   COSTS_N_INSNS (37), /* fdivd */
73   COSTS_N_INSNS (63), /* fsqrts */
74   COSTS_N_INSNS (63), /* fsqrtd */
75   COSTS_N_INSNS (1), /* imul */
76   COSTS_N_INSNS (1), /* imulX */
77   0, /* imul bit factor */
78   COSTS_N_INSNS (1), /* idiv */
79   COSTS_N_INSNS (1), /* idivX */
80   COSTS_N_INSNS (1), /* movcc/movr */
81   0, /* shift penalty */
82 };
83
84 static const
85 struct processor_costs supersparc_costs = {
86   COSTS_N_INSNS (1), /* int load */
87   COSTS_N_INSNS (1), /* int signed load */
88   COSTS_N_INSNS (1), /* int zeroed load */
89   COSTS_N_INSNS (0), /* float load */
90   COSTS_N_INSNS (3), /* fmov, fneg, fabs */
91   COSTS_N_INSNS (3), /* fadd, fsub */
92   COSTS_N_INSNS (3), /* fcmp */
93   COSTS_N_INSNS (1), /* fmov, fmovr */
94   COSTS_N_INSNS (3), /* fmul */
95   COSTS_N_INSNS (6), /* fdivs */
96   COSTS_N_INSNS (9), /* fdivd */
97   COSTS_N_INSNS (12), /* fsqrts */
98   COSTS_N_INSNS (12), /* fsqrtd */
99   COSTS_N_INSNS (4), /* imul */
100   COSTS_N_INSNS (4), /* imulX */
101   0, /* imul bit factor */
102   COSTS_N_INSNS (4), /* idiv */
103   COSTS_N_INSNS (4), /* idivX */
104   COSTS_N_INSNS (1), /* movcc/movr */
105   1, /* shift penalty */
106 };
107
108 static const
109 struct processor_costs hypersparc_costs = {
110   COSTS_N_INSNS (1), /* int load */
111   COSTS_N_INSNS (1), /* int signed load */
112   COSTS_N_INSNS (1), /* int zeroed load */
113   COSTS_N_INSNS (1), /* float load */
114   COSTS_N_INSNS (1), /* fmov, fneg, fabs */
115   COSTS_N_INSNS (1), /* fadd, fsub */
116   COSTS_N_INSNS (1), /* fcmp */
117   COSTS_N_INSNS (1), /* fmov, fmovr */
118   COSTS_N_INSNS (1), /* fmul */
119   COSTS_N_INSNS (8), /* fdivs */
120   COSTS_N_INSNS (12), /* fdivd */
121   COSTS_N_INSNS (17), /* fsqrts */
122   COSTS_N_INSNS (17), /* fsqrtd */
123   COSTS_N_INSNS (17), /* imul */
124   COSTS_N_INSNS (17), /* imulX */
125   0, /* imul bit factor */
126   COSTS_N_INSNS (17), /* idiv */
127   COSTS_N_INSNS (17), /* idivX */
128   COSTS_N_INSNS (1), /* movcc/movr */
129   0, /* shift penalty */
130 };
131
132 static const
133 struct processor_costs sparclet_costs = {
134   COSTS_N_INSNS (3), /* int load */
135   COSTS_N_INSNS (3), /* int signed load */
136   COSTS_N_INSNS (1), /* int zeroed load */
137   COSTS_N_INSNS (1), /* float load */
138   COSTS_N_INSNS (1), /* fmov, fneg, fabs */
139   COSTS_N_INSNS (1), /* fadd, fsub */
140   COSTS_N_INSNS (1), /* fcmp */
141   COSTS_N_INSNS (1), /* fmov, fmovr */
142   COSTS_N_INSNS (1), /* fmul */
143   COSTS_N_INSNS (1), /* fdivs */
144   COSTS_N_INSNS (1), /* fdivd */
145   COSTS_N_INSNS (1), /* fsqrts */
146   COSTS_N_INSNS (1), /* fsqrtd */
147   COSTS_N_INSNS (5), /* imul */
148   COSTS_N_INSNS (5), /* imulX */
149   0, /* imul bit factor */
150   COSTS_N_INSNS (5), /* idiv */
151   COSTS_N_INSNS (5), /* idivX */
152   COSTS_N_INSNS (1), /* movcc/movr */
153   0, /* shift penalty */
154 };
155
156 static const
157 struct processor_costs ultrasparc_costs = {
158   COSTS_N_INSNS (2), /* int load */
159   COSTS_N_INSNS (3), /* int signed load */
160   COSTS_N_INSNS (2), /* int zeroed load */
161   COSTS_N_INSNS (2), /* float load */
162   COSTS_N_INSNS (1), /* fmov, fneg, fabs */
163   COSTS_N_INSNS (4), /* fadd, fsub */
164   COSTS_N_INSNS (1), /* fcmp */
165   COSTS_N_INSNS (2), /* fmov, fmovr */
166   COSTS_N_INSNS (4), /* fmul */
167   COSTS_N_INSNS (13), /* fdivs */
168   COSTS_N_INSNS (23), /* fdivd */
169   COSTS_N_INSNS (13), /* fsqrts */
170   COSTS_N_INSNS (23), /* fsqrtd */
171   COSTS_N_INSNS (4), /* imul */
172   COSTS_N_INSNS (4), /* imulX */
173   2, /* imul bit factor */
174   COSTS_N_INSNS (37), /* idiv */
175   COSTS_N_INSNS (68), /* idivX */
176   COSTS_N_INSNS (2), /* movcc/movr */
177   2, /* shift penalty */
178 };
179
180 static const
181 struct processor_costs ultrasparc3_costs = {
182   COSTS_N_INSNS (2), /* int load */
183   COSTS_N_INSNS (3), /* int signed load */
184   COSTS_N_INSNS (3), /* int zeroed load */
185   COSTS_N_INSNS (2), /* float load */
186   COSTS_N_INSNS (3), /* fmov, fneg, fabs */
187   COSTS_N_INSNS (4), /* fadd, fsub */
188   COSTS_N_INSNS (5), /* fcmp */
189   COSTS_N_INSNS (3), /* fmov, fmovr */
190   COSTS_N_INSNS (4), /* fmul */
191   COSTS_N_INSNS (17), /* fdivs */
192   COSTS_N_INSNS (20), /* fdivd */
193   COSTS_N_INSNS (20), /* fsqrts */
194   COSTS_N_INSNS (29), /* fsqrtd */
195   COSTS_N_INSNS (6), /* imul */
196   COSTS_N_INSNS (6), /* imulX */
197   0, /* imul bit factor */
198   COSTS_N_INSNS (40), /* idiv */
199   COSTS_N_INSNS (71), /* idivX */
200   COSTS_N_INSNS (2), /* movcc/movr */
201   0, /* shift penalty */
202 };
203
204 static const
205 struct processor_costs niagara_costs = {
206   COSTS_N_INSNS (3), /* int load */
207   COSTS_N_INSNS (3), /* int signed load */
208   COSTS_N_INSNS (3), /* int zeroed load */
209   COSTS_N_INSNS (9), /* float load */
210   COSTS_N_INSNS (8), /* fmov, fneg, fabs */
211   COSTS_N_INSNS (8), /* fadd, fsub */
212   COSTS_N_INSNS (26), /* fcmp */
213   COSTS_N_INSNS (8), /* fmov, fmovr */
214   COSTS_N_INSNS (29), /* fmul */
215   COSTS_N_INSNS (54), /* fdivs */
216   COSTS_N_INSNS (83), /* fdivd */
217   COSTS_N_INSNS (100), /* fsqrts - not implemented in hardware */
218   COSTS_N_INSNS (100), /* fsqrtd - not implemented in hardware */
219   COSTS_N_INSNS (11), /* imul */
220   COSTS_N_INSNS (11), /* imulX */
221   0, /* imul bit factor */
222   COSTS_N_INSNS (72), /* idiv */
223   COSTS_N_INSNS (72), /* idivX */
224   COSTS_N_INSNS (1), /* movcc/movr */
225   0, /* shift penalty */
226 };
227
228 static const
229 struct processor_costs niagara2_costs = {
230   COSTS_N_INSNS (3), /* int load */
231   COSTS_N_INSNS (3), /* int signed load */
232   COSTS_N_INSNS (3), /* int zeroed load */
233   COSTS_N_INSNS (3), /* float load */
234   COSTS_N_INSNS (6), /* fmov, fneg, fabs */
235   COSTS_N_INSNS (6), /* fadd, fsub */
236   COSTS_N_INSNS (6), /* fcmp */
237   COSTS_N_INSNS (6), /* fmov, fmovr */
238   COSTS_N_INSNS (6), /* fmul */
239   COSTS_N_INSNS (19), /* fdivs */
240   COSTS_N_INSNS (33), /* fdivd */
241   COSTS_N_INSNS (19), /* fsqrts */
242   COSTS_N_INSNS (33), /* fsqrtd */
243   COSTS_N_INSNS (5), /* imul */
244   COSTS_N_INSNS (5), /* imulX */
245   0, /* imul bit factor */
246   COSTS_N_INSNS (31), /* idiv, average of 12 - 41 cycle range */
247   COSTS_N_INSNS (31), /* idivX, average of 12 - 41 cycle range */
248   COSTS_N_INSNS (1), /* movcc/movr */
249   0, /* shift penalty */
250 };
251
252 const struct processor_costs *sparc_costs = &cypress_costs;
253
254 #ifdef HAVE_AS_RELAX_OPTION
255 /* If 'as' and 'ld' are relaxing tail call insns into branch always, use
256    "or %o7,%g0,X; call Y; or X,%g0,%o7" always, so that it can be optimized.
257    With sethi/jmp, neither 'as' nor 'ld' has an easy way how to find out if
258    somebody does not branch between the sethi and jmp.  */
259 #define LEAF_SIBCALL_SLOT_RESERVED_P 1
260 #else
261 #define LEAF_SIBCALL_SLOT_RESERVED_P \
262   ((TARGET_ARCH64 && !TARGET_CM_MEDLOW) || flag_pic)
263 #endif
264
265 /* Global variables for machine-dependent things.  */
266
267 /* Size of frame.  Need to know this to emit return insns from leaf procedures.
268    ACTUAL_FSIZE is set by sparc_compute_frame_size() which is called during the
269    reload pass.  This is important as the value is later used for scheduling
270    (to see what can go in a delay slot).
271    APPARENT_FSIZE is the size of the stack less the register save area and less
272    the outgoing argument area.  It is used when saving call preserved regs.  */
273 static HOST_WIDE_INT apparent_fsize;
274 static HOST_WIDE_INT actual_fsize;
275
276 /* Number of live general or floating point registers needed to be
277    saved (as 4-byte quantities).  */
278 static int num_gfregs;
279
280 /* The alias set for prologue/epilogue register save/restore.  */
281 static GTY(()) alias_set_type sparc_sr_alias_set;
282
283 /* The alias set for the structure return value.  */
284 static GTY(()) alias_set_type struct_value_alias_set;
285
286 /* Vector to say how input registers are mapped to output registers.
287    HARD_FRAME_POINTER_REGNUM cannot be remapped by this function to
288    eliminate it.  You must use -fomit-frame-pointer to get that.  */
289 char leaf_reg_remap[] =
290 { 0, 1, 2, 3, 4, 5, 6, 7,
291   -1, -1, -1, -1, -1, -1, 14, -1,
292   -1, -1, -1, -1, -1, -1, -1, -1,
293   8, 9, 10, 11, 12, 13, -1, 15,
294
295   32, 33, 34, 35, 36, 37, 38, 39,
296   40, 41, 42, 43, 44, 45, 46, 47,
297   48, 49, 50, 51, 52, 53, 54, 55,
298   56, 57, 58, 59, 60, 61, 62, 63,
299   64, 65, 66, 67, 68, 69, 70, 71,
300   72, 73, 74, 75, 76, 77, 78, 79,
301   80, 81, 82, 83, 84, 85, 86, 87,
302   88, 89, 90, 91, 92, 93, 94, 95,
303   96, 97, 98, 99, 100};
304
305 /* Vector, indexed by hard register number, which contains 1
306    for a register that is allowable in a candidate for leaf
307    function treatment.  */
308 char sparc_leaf_regs[] =
309 { 1, 1, 1, 1, 1, 1, 1, 1,
310   0, 0, 0, 0, 0, 0, 1, 0,
311   0, 0, 0, 0, 0, 0, 0, 0,
312   1, 1, 1, 1, 1, 1, 0, 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, 1, 1, 1,
321   1, 1, 1, 1, 1};
322
323 struct GTY(()) machine_function
324 {
325   /* Some local-dynamic TLS symbol name.  */
326   const char *some_ld_name;
327
328   /* True if the current function is leaf and uses only leaf regs,
329      so that the SPARC leaf function optimization can be applied.
330      Private version of current_function_uses_only_leaf_regs, see
331      sparc_expand_prologue for the rationale.  */
332   int leaf_function_p;
333
334   /* True if the data calculated by sparc_expand_prologue are valid.  */
335   bool prologue_data_valid_p;
336 };
337
338 #define sparc_leaf_function_p  cfun->machine->leaf_function_p
339 #define sparc_prologue_data_valid_p  cfun->machine->prologue_data_valid_p
340
341 /* Register we pretend to think the frame pointer is allocated to.
342    Normally, this is %fp, but if we are in a leaf procedure, this
343    is %sp+"something".  We record "something" separately as it may
344    be too big for reg+constant addressing.  */
345 static rtx frame_base_reg;
346 static HOST_WIDE_INT frame_base_offset;
347
348 /* 1 if the next opcode is to be specially indented.  */
349 int sparc_indent_opcode = 0;
350
351 static bool sparc_handle_option (size_t, const char *, int);
352 static void sparc_init_modes (void);
353 static void scan_record_type (tree, int *, int *, int *);
354 static int function_arg_slotno (const CUMULATIVE_ARGS *, enum machine_mode,
355                                 tree, int, int, 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 bool sparc_promote_prototypes (const_tree);
408 static rtx sparc_function_value (const_tree, const_tree, bool);
409 static rtx sparc_libcall_value (enum machine_mode, const_rtx);
410 static bool sparc_function_value_regno_p (const unsigned int);
411 static rtx sparc_struct_value_rtx (tree, int);
412 static enum machine_mode sparc_promote_function_mode (const_tree, enum machine_mode,
413                                                       int *, const_tree, int);
414 static bool sparc_return_in_memory (const_tree, const_tree);
415 static bool sparc_strict_argument_naming (CUMULATIVE_ARGS *);
416 static void sparc_va_start (tree, rtx);
417 static tree sparc_gimplify_va_arg (tree, tree, gimple_seq *, gimple_seq *);
418 static bool sparc_vector_mode_supported_p (enum machine_mode);
419 static bool sparc_tls_referenced_p (rtx);
420 static rtx sparc_legitimize_tls_address (rtx);
421 static rtx sparc_legitimize_pic_address (rtx, rtx);
422 static rtx sparc_legitimize_address (rtx, rtx, enum machine_mode);
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 int sparc_arg_partial_bytes (CUMULATIVE_ARGS *,
427                                     enum machine_mode, tree, bool);
428 static void sparc_dwarf_handle_frame_unspec (const char *, rtx, int);
429 static void sparc_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
430 static void sparc_file_end (void);
431 static bool sparc_frame_pointer_required (void);
432 static bool sparc_can_eliminate (const int, const int);
433 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
434 static const char *sparc_mangle_type (const_tree);
435 #endif
436 static void sparc_trampoline_init (rtx, tree, rtx);
437 static bool sparc_units_per_simd_word (enum machine_mode);
438 \f
439 #ifdef SUBTARGET_ATTRIBUTE_TABLE
440 /* Table of valid machine attributes.  */
441 static const struct attribute_spec sparc_attribute_table[] =
442 {
443   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
444   SUBTARGET_ATTRIBUTE_TABLE,
445   { NULL,        0, 0, false, false, false, NULL }
446 };
447 #endif
448 \f
449 /* Option handling.  */
450
451 /* Parsed value.  */
452 enum cmodel sparc_cmodel;
453
454 char sparc_hard_reg_printed[8];
455
456 struct sparc_cpu_select sparc_select[] =
457 {
458   /* switch     name,           tune    arch */
459   { (char *)0,  "default",      1,      1 },
460   { (char *)0,  "-mcpu=",       1,      1 },
461   { (char *)0,  "-mtune=",      1,      0 },
462   { 0, 0, 0, 0 }
463 };
464
465 /* CPU type.  This is set from TARGET_CPU_DEFAULT and -m{cpu,tune}=xxx.  */
466 enum processor_type sparc_cpu;
467
468 /* Whether\fan FPU option was specified.  */
469 static bool fpu_option_set = false;
470
471 /* Initialize the GCC target structure.  */
472
473 /* The default is to use .half rather than .short for aligned HI objects.  */
474 #undef TARGET_ASM_ALIGNED_HI_OP
475 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
476
477 #undef TARGET_ASM_UNALIGNED_HI_OP
478 #define TARGET_ASM_UNALIGNED_HI_OP "\t.uahalf\t"
479 #undef TARGET_ASM_UNALIGNED_SI_OP
480 #define TARGET_ASM_UNALIGNED_SI_OP "\t.uaword\t"
481 #undef TARGET_ASM_UNALIGNED_DI_OP
482 #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaxword\t"
483
484 /* The target hook has to handle DI-mode values.  */
485 #undef TARGET_ASM_INTEGER
486 #define TARGET_ASM_INTEGER sparc_assemble_integer
487
488 #undef TARGET_ASM_FUNCTION_PROLOGUE
489 #define TARGET_ASM_FUNCTION_PROLOGUE sparc_asm_function_prologue
490 #undef TARGET_ASM_FUNCTION_EPILOGUE
491 #define TARGET_ASM_FUNCTION_EPILOGUE sparc_asm_function_epilogue
492
493 #undef TARGET_SCHED_ADJUST_COST
494 #define TARGET_SCHED_ADJUST_COST sparc_adjust_cost
495 #undef TARGET_SCHED_ISSUE_RATE
496 #define TARGET_SCHED_ISSUE_RATE sparc_issue_rate
497 #undef TARGET_SCHED_INIT
498 #define TARGET_SCHED_INIT sparc_sched_init
499 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
500 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD sparc_use_sched_lookahead
501
502 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
503 #define TARGET_FUNCTION_OK_FOR_SIBCALL sparc_function_ok_for_sibcall
504
505 #undef TARGET_INIT_LIBFUNCS
506 #define TARGET_INIT_LIBFUNCS sparc_init_libfuncs
507 #undef TARGET_INIT_BUILTINS
508 #define TARGET_INIT_BUILTINS sparc_init_builtins
509
510 #undef TARGET_LEGITIMIZE_ADDRESS
511 #define TARGET_LEGITIMIZE_ADDRESS sparc_legitimize_address
512 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
513 #define TARGET_MODE_DEPENDENT_ADDRESS_P sparc_mode_dependent_address_p
514
515 #undef TARGET_EXPAND_BUILTIN
516 #define TARGET_EXPAND_BUILTIN sparc_expand_builtin
517 #undef TARGET_FOLD_BUILTIN
518 #define TARGET_FOLD_BUILTIN sparc_fold_builtin
519
520 #if TARGET_TLS
521 #undef TARGET_HAVE_TLS
522 #define TARGET_HAVE_TLS true
523 #endif
524
525 #undef TARGET_CANNOT_FORCE_CONST_MEM
526 #define TARGET_CANNOT_FORCE_CONST_MEM sparc_cannot_force_const_mem
527
528 #undef TARGET_ASM_OUTPUT_MI_THUNK
529 #define TARGET_ASM_OUTPUT_MI_THUNK sparc_output_mi_thunk
530 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
531 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK sparc_can_output_mi_thunk
532
533 #undef TARGET_RTX_COSTS
534 #define TARGET_RTX_COSTS sparc_rtx_costs
535 #undef TARGET_ADDRESS_COST
536 #define TARGET_ADDRESS_COST hook_int_rtx_bool_0
537
538 #undef TARGET_PROMOTE_FUNCTION_MODE
539 #define TARGET_PROMOTE_FUNCTION_MODE sparc_promote_function_mode
540
541 #undef TARGET_PROMOTE_PROTOTYPES
542 #define TARGET_PROMOTE_PROTOTYPES sparc_promote_prototypes
543
544 #undef TARGET_FUNCTION_VALUE
545 #define TARGET_FUNCTION_VALUE sparc_function_value
546 #undef TARGET_LIBCALL_VALUE
547 #define TARGET_LIBCALL_VALUE sparc_libcall_value
548 #undef TARGET_FUNCTION_VALUE_REGNO_P
549 #define TARGET_FUNCTION_VALUE_REGNO_P sparc_function_value_regno_p
550
551 #undef TARGET_STRUCT_VALUE_RTX
552 #define TARGET_STRUCT_VALUE_RTX sparc_struct_value_rtx
553 #undef TARGET_RETURN_IN_MEMORY
554 #define TARGET_RETURN_IN_MEMORY sparc_return_in_memory
555 #undef TARGET_MUST_PASS_IN_STACK
556 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
557 #undef TARGET_PASS_BY_REFERENCE
558 #define TARGET_PASS_BY_REFERENCE sparc_pass_by_reference
559 #undef TARGET_ARG_PARTIAL_BYTES
560 #define TARGET_ARG_PARTIAL_BYTES sparc_arg_partial_bytes
561
562 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
563 #define TARGET_EXPAND_BUILTIN_SAVEREGS sparc_builtin_saveregs
564 #undef TARGET_STRICT_ARGUMENT_NAMING
565 #define TARGET_STRICT_ARGUMENT_NAMING sparc_strict_argument_naming
566
567 #undef TARGET_EXPAND_BUILTIN_VA_START
568 #define TARGET_EXPAND_BUILTIN_VA_START sparc_va_start
569 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
570 #define TARGET_GIMPLIFY_VA_ARG_EXPR sparc_gimplify_va_arg
571
572 #undef TARGET_VECTOR_MODE_SUPPORTED_P
573 #define TARGET_VECTOR_MODE_SUPPORTED_P sparc_vector_mode_supported_p
574
575 #undef TARGET_VECTORIZE_UNITS_PER_SIMD_WORD
576 #define TARGET_VECTORIZE_UNITS_PER_SIMD_WORD sparc_units_per_simd_word
577
578 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
579 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC sparc_dwarf_handle_frame_unspec
580
581 #ifdef SUBTARGET_INSERT_ATTRIBUTES
582 #undef TARGET_INSERT_ATTRIBUTES
583 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
584 #endif
585
586 #ifdef SUBTARGET_ATTRIBUTE_TABLE
587 #undef TARGET_ATTRIBUTE_TABLE
588 #define TARGET_ATTRIBUTE_TABLE sparc_attribute_table
589 #endif
590
591 #undef TARGET_RELAXED_ORDERING
592 #define TARGET_RELAXED_ORDERING SPARC_RELAXED_ORDERING
593
594 #undef TARGET_DEFAULT_TARGET_FLAGS
595 #define TARGET_DEFAULT_TARGET_FLAGS TARGET_DEFAULT
596 #undef TARGET_HANDLE_OPTION
597 #define TARGET_HANDLE_OPTION sparc_handle_option
598
599 #if TARGET_GNU_TLS && defined(HAVE_AS_SPARC_UA_PCREL)
600 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
601 #define TARGET_ASM_OUTPUT_DWARF_DTPREL sparc_output_dwarf_dtprel
602 #endif
603
604 #undef TARGET_ASM_FILE_END
605 #define TARGET_ASM_FILE_END sparc_file_end
606
607 #undef TARGET_FRAME_POINTER_REQUIRED
608 #define TARGET_FRAME_POINTER_REQUIRED sparc_frame_pointer_required
609
610 #undef TARGET_CAN_ELIMINATE
611 #define TARGET_CAN_ELIMINATE sparc_can_eliminate
612
613 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
614 #undef TARGET_MANGLE_TYPE
615 #define TARGET_MANGLE_TYPE sparc_mangle_type
616 #endif
617
618 #undef TARGET_LEGITIMATE_ADDRESS_P
619 #define TARGET_LEGITIMATE_ADDRESS_P sparc_legitimate_address_p
620
621 #undef TARGET_TRAMPOLINE_INIT
622 #define TARGET_TRAMPOLINE_INIT sparc_trampoline_init
623
624 struct gcc_target targetm = TARGET_INITIALIZER;
625
626 /* Implement TARGET_HANDLE_OPTION.  */
627
628 static bool
629 sparc_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
630 {
631   switch (code)
632     {
633     case OPT_mfpu:
634     case OPT_mhard_float:
635     case OPT_msoft_float:
636       fpu_option_set = true;
637       break;
638
639     case OPT_mcpu_:
640       sparc_select[1].string = arg;
641       break;
642
643     case OPT_mtune_:
644       sparc_select[2].string = arg;
645       break;
646     }
647
648   return true;
649 }
650
651 /* Validate and override various options, and do some machine dependent
652    initialization.  */
653
654 void
655 sparc_override_options (void)
656 {
657   static struct code_model {
658     const char *const name;
659     const enum cmodel value;
660   } const cmodels[] = {
661     { "32", CM_32 },
662     { "medlow", CM_MEDLOW },
663     { "medmid", CM_MEDMID },
664     { "medany", CM_MEDANY },
665     { "embmedany", CM_EMBMEDANY },
666     { NULL, (enum cmodel) 0 }
667   };
668   const struct code_model *cmodel;
669   /* Map TARGET_CPU_DEFAULT to value for -m{arch,tune}=.  */
670   static struct cpu_default {
671     const int cpu;
672     const char *const name;
673   } const cpu_default[] = {
674     /* There must be one entry here for each TARGET_CPU value.  */
675     { TARGET_CPU_sparc, "cypress" },
676     { TARGET_CPU_sparclet, "tsc701" },
677     { TARGET_CPU_sparclite, "f930" },
678     { TARGET_CPU_v8, "v8" },
679     { TARGET_CPU_hypersparc, "hypersparc" },
680     { TARGET_CPU_sparclite86x, "sparclite86x" },
681     { TARGET_CPU_supersparc, "supersparc" },
682     { TARGET_CPU_v9, "v9" },
683     { TARGET_CPU_ultrasparc, "ultrasparc" },
684     { TARGET_CPU_ultrasparc3, "ultrasparc3" },
685     { TARGET_CPU_niagara, "niagara" },
686     { TARGET_CPU_niagara2, "niagara2" },
687     { 0, 0 }
688   };
689   const struct cpu_default *def;
690   /* Table of values for -m{cpu,tune}=.  */
691   static struct cpu_table {
692     const char *const name;
693     const enum processor_type processor;
694     const int disable;
695     const int enable;
696   } const cpu_table[] = {
697     { "v7",         PROCESSOR_V7, MASK_ISA, 0 },
698     { "cypress",    PROCESSOR_CYPRESS, MASK_ISA, 0 },
699     { "v8",         PROCESSOR_V8, MASK_ISA, MASK_V8 },
700     /* TI TMS390Z55 supersparc */
701     { "supersparc", PROCESSOR_SUPERSPARC, MASK_ISA, MASK_V8 },
702     { "sparclite",  PROCESSOR_SPARCLITE, MASK_ISA, MASK_SPARCLITE },
703     /* The Fujitsu MB86930 is the original sparclite chip, with no fpu.
704        The Fujitsu MB86934 is the recent sparclite chip, with an fpu.  */
705     { "f930",       PROCESSOR_F930, MASK_ISA|MASK_FPU, MASK_SPARCLITE },
706     { "f934",       PROCESSOR_F934, MASK_ISA, MASK_SPARCLITE|MASK_FPU },
707     { "hypersparc", PROCESSOR_HYPERSPARC, MASK_ISA, MASK_V8|MASK_FPU },
708     { "sparclite86x",  PROCESSOR_SPARCLITE86X, MASK_ISA|MASK_FPU,
709       MASK_SPARCLITE },
710     { "sparclet",   PROCESSOR_SPARCLET, MASK_ISA, MASK_SPARCLET },
711     /* TEMIC sparclet */
712     { "tsc701",     PROCESSOR_TSC701, MASK_ISA, MASK_SPARCLET },
713     { "v9",         PROCESSOR_V9, MASK_ISA, MASK_V9 },
714     /* TI ultrasparc I, II, IIi */
715     { "ultrasparc", PROCESSOR_ULTRASPARC, MASK_ISA, MASK_V9
716     /* Although insns using %y are deprecated, it is a clear win on current
717        ultrasparcs.  */
718                                                     |MASK_DEPRECATED_V8_INSNS},
719     /* TI ultrasparc III */
720     /* ??? Check if %y issue still holds true in ultra3.  */
721     { "ultrasparc3", PROCESSOR_ULTRASPARC3, MASK_ISA, MASK_V9|MASK_DEPRECATED_V8_INSNS},
722     /* UltraSPARC T1 */
723     { "niagara", PROCESSOR_NIAGARA, MASK_ISA, MASK_V9|MASK_DEPRECATED_V8_INSNS},
724     { "niagara2", PROCESSOR_NIAGARA, MASK_ISA, MASK_V9},
725     { 0, (enum processor_type) 0, 0, 0 }
726   };
727   const struct cpu_table *cpu;
728   const struct sparc_cpu_select *sel;
729   int fpu;
730
731 #ifndef SPARC_BI_ARCH
732   /* Check for unsupported architecture size.  */
733   if (! TARGET_64BIT != DEFAULT_ARCH32_P)
734     error ("%s is not supported by this configuration",
735            DEFAULT_ARCH32_P ? "-m64" : "-m32");
736 #endif
737
738   /* We force all 64bit archs to use 128 bit long double */
739   if (TARGET_64BIT && ! TARGET_LONG_DOUBLE_128)
740     {
741       error ("-mlong-double-64 not allowed with -m64");
742       target_flags |= MASK_LONG_DOUBLE_128;
743     }
744
745   /* Code model selection.  */
746   sparc_cmodel = SPARC_DEFAULT_CMODEL;
747
748 #ifdef SPARC_BI_ARCH
749   if (TARGET_ARCH32)
750     sparc_cmodel = CM_32;
751 #endif
752
753   if (sparc_cmodel_string != NULL)
754     {
755       if (TARGET_ARCH64)
756         {
757           for (cmodel = &cmodels[0]; cmodel->name; cmodel++)
758             if (strcmp (sparc_cmodel_string, cmodel->name) == 0)
759               break;
760           if (cmodel->name == NULL)
761             error ("bad value (%s) for -mcmodel= switch", sparc_cmodel_string);
762           else
763             sparc_cmodel = cmodel->value;
764         }
765       else
766         error ("-mcmodel= is not supported on 32 bit systems");
767     }
768
769   fpu = target_flags & MASK_FPU; /* save current -mfpu status */
770
771   /* Set the default CPU.  */
772   for (def = &cpu_default[0]; def->name; ++def)
773     if (def->cpu == TARGET_CPU_DEFAULT)
774       break;
775   gcc_assert (def->name);
776   sparc_select[0].string = def->name;
777
778   for (sel = &sparc_select[0]; sel->name; ++sel)
779     {
780       if (sel->string)
781         {
782           for (cpu = &cpu_table[0]; cpu->name; ++cpu)
783             if (! strcmp (sel->string, cpu->name))
784               {
785                 if (sel->set_tune_p)
786                   sparc_cpu = cpu->processor;
787
788                 if (sel->set_arch_p)
789                   {
790                     target_flags &= ~cpu->disable;
791                     target_flags |= cpu->enable;
792                   }
793                 break;
794               }
795
796           if (! cpu->name)
797             error ("bad value (%s) for %s switch", sel->string, sel->name);
798         }
799     }
800
801   /* If -mfpu or -mno-fpu was explicitly used, don't override with
802      the processor default.  */
803   if (fpu_option_set)
804     target_flags = (target_flags & ~MASK_FPU) | fpu;
805
806   /* Don't allow -mvis if FPU is disabled.  */
807   if (! TARGET_FPU)
808     target_flags &= ~MASK_VIS;
809
810   /* -mvis assumes UltraSPARC+, so we are sure v9 instructions
811      are available.
812      -m64 also implies v9.  */
813   if (TARGET_VIS || TARGET_ARCH64)
814     {
815       target_flags |= MASK_V9;
816       target_flags &= ~(MASK_V8 | MASK_SPARCLET | MASK_SPARCLITE);
817     }
818
819   /* Use the deprecated v8 insns for sparc64 in 32 bit mode.  */
820   if (TARGET_V9 && TARGET_ARCH32)
821     target_flags |= MASK_DEPRECATED_V8_INSNS;
822
823   /* V8PLUS requires V9, makes no sense in 64 bit mode.  */
824   if (! TARGET_V9 || TARGET_ARCH64)
825     target_flags &= ~MASK_V8PLUS;
826
827   /* Don't use stack biasing in 32 bit mode.  */
828   if (TARGET_ARCH32)
829     target_flags &= ~MASK_STACK_BIAS;
830
831   /* Supply a default value for align_functions.  */
832   if (align_functions == 0
833       && (sparc_cpu == PROCESSOR_ULTRASPARC
834           || sparc_cpu == PROCESSOR_ULTRASPARC3
835           || sparc_cpu == PROCESSOR_NIAGARA
836           || sparc_cpu == PROCESSOR_NIAGARA2))
837     align_functions = 32;
838
839   /* Validate PCC_STRUCT_RETURN.  */
840   if (flag_pcc_struct_return == DEFAULT_PCC_STRUCT_RETURN)
841     flag_pcc_struct_return = (TARGET_ARCH64 ? 0 : 1);
842
843   /* Only use .uaxword when compiling for a 64-bit target.  */
844   if (!TARGET_ARCH64)
845     targetm.asm_out.unaligned_op.di = NULL;
846
847   /* Do various machine dependent initializations.  */
848   sparc_init_modes ();
849
850   /* Acquire unique alias sets for our private stuff.  */
851   sparc_sr_alias_set = new_alias_set ();
852   struct_value_alias_set = new_alias_set ();
853
854   /* Set up function hooks.  */
855   init_machine_status = sparc_init_machine_status;
856
857   switch (sparc_cpu)
858     {
859     case PROCESSOR_V7:
860     case PROCESSOR_CYPRESS:
861       sparc_costs = &cypress_costs;
862       break;
863     case PROCESSOR_V8:
864     case PROCESSOR_SPARCLITE:
865     case PROCESSOR_SUPERSPARC:
866       sparc_costs = &supersparc_costs;
867       break;
868     case PROCESSOR_F930:
869     case PROCESSOR_F934:
870     case PROCESSOR_HYPERSPARC:
871     case PROCESSOR_SPARCLITE86X:
872       sparc_costs = &hypersparc_costs;
873       break;
874     case PROCESSOR_SPARCLET:
875     case PROCESSOR_TSC701:
876       sparc_costs = &sparclet_costs;
877       break;
878     case PROCESSOR_V9:
879     case PROCESSOR_ULTRASPARC:
880       sparc_costs = &ultrasparc_costs;
881       break;
882     case PROCESSOR_ULTRASPARC3:
883       sparc_costs = &ultrasparc3_costs;
884       break;
885     case PROCESSOR_NIAGARA:
886       sparc_costs = &niagara_costs;
887       break;
888     case PROCESSOR_NIAGARA2:
889       sparc_costs = &niagara2_costs;
890       break;
891     };
892
893 #ifdef TARGET_DEFAULT_LONG_DOUBLE_128
894   if (!(target_flags_explicit & MASK_LONG_DOUBLE_128))
895     target_flags |= MASK_LONG_DOUBLE_128;
896 #endif
897
898   if (!PARAM_SET_P (PARAM_SIMULTANEOUS_PREFETCHES))
899     set_param_value ("simultaneous-prefetches",
900                      ((sparc_cpu == PROCESSOR_ULTRASPARC
901                        || sparc_cpu == PROCESSOR_NIAGARA
902                        || sparc_cpu == PROCESSOR_NIAGARA2)
903                       ? 2
904                       : (sparc_cpu == PROCESSOR_ULTRASPARC3
905                          ? 8 : 3)));
906   if (!PARAM_SET_P (PARAM_L1_CACHE_LINE_SIZE))
907     set_param_value ("l1-cache-line-size",
908                      ((sparc_cpu == PROCESSOR_ULTRASPARC
909                        || sparc_cpu == PROCESSOR_ULTRASPARC3
910                        || sparc_cpu == PROCESSOR_NIAGARA
911                        || sparc_cpu == PROCESSOR_NIAGARA2)
912                       ? 64 : 32));
913 }
914 \f
915 /* Miscellaneous utilities.  */
916
917 /* Nonzero if CODE, a comparison, is suitable for use in v9 conditional move
918    or branch on register contents instructions.  */
919
920 int
921 v9_regcmp_p (enum rtx_code code)
922 {
923   return (code == EQ || code == NE || code == GE || code == LT
924           || code == LE || code == GT);
925 }
926
927 /* Nonzero if OP is a floating point constant which can
928    be loaded into an integer register using a single
929    sethi instruction.  */
930
931 int
932 fp_sethi_p (rtx op)
933 {
934   if (GET_CODE (op) == CONST_DOUBLE)
935     {
936       REAL_VALUE_TYPE r;
937       long i;
938
939       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
940       REAL_VALUE_TO_TARGET_SINGLE (r, i);
941       return !SPARC_SIMM13_P (i) && SPARC_SETHI_P (i);
942     }
943
944   return 0;
945 }
946
947 /* Nonzero if OP is a floating point constant which can
948    be loaded into an integer register using a single
949    mov instruction.  */
950
951 int
952 fp_mov_p (rtx op)
953 {
954   if (GET_CODE (op) == CONST_DOUBLE)
955     {
956       REAL_VALUE_TYPE r;
957       long i;
958
959       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
960       REAL_VALUE_TO_TARGET_SINGLE (r, i);
961       return SPARC_SIMM13_P (i);
962     }
963
964   return 0;
965 }
966
967 /* Nonzero if OP is a floating point constant which can
968    be loaded into an integer register using a high/losum
969    instruction sequence.  */
970
971 int
972 fp_high_losum_p (rtx op)
973 {
974   /* The constraints calling this should only be in
975      SFmode move insns, so any constant which cannot
976      be moved using a single insn will do.  */
977   if (GET_CODE (op) == CONST_DOUBLE)
978     {
979       REAL_VALUE_TYPE r;
980       long i;
981
982       REAL_VALUE_FROM_CONST_DOUBLE (r, op);
983       REAL_VALUE_TO_TARGET_SINGLE (r, i);
984       return !SPARC_SIMM13_P (i) && !SPARC_SETHI_P (i);
985     }
986
987   return 0;
988 }
989
990 /* Expand a move instruction.  Return true if all work is done.  */
991
992 bool
993 sparc_expand_move (enum machine_mode mode, rtx *operands)
994 {
995   /* Handle sets of MEM first.  */
996   if (GET_CODE (operands[0]) == MEM)
997     {
998       /* 0 is a register (or a pair of registers) on SPARC.  */
999       if (register_or_zero_operand (operands[1], mode))
1000         return false;
1001
1002       if (!reload_in_progress)
1003         {
1004           operands[0] = validize_mem (operands[0]);
1005           operands[1] = force_reg (mode, operands[1]);
1006         }
1007     }
1008
1009   /* Fixup TLS cases.  */
1010   if (TARGET_HAVE_TLS
1011       && CONSTANT_P (operands[1])
1012       && sparc_tls_referenced_p (operands [1]))
1013     {
1014       operands[1] = sparc_legitimize_tls_address (operands[1]);
1015       return false;
1016     }
1017
1018   /* Fixup PIC cases.  */
1019   if (flag_pic && CONSTANT_P (operands[1]))
1020     {
1021       if (pic_address_needs_scratch (operands[1]))
1022         operands[1] = sparc_legitimize_pic_address (operands[1], NULL_RTX);
1023
1024       /* VxWorks does not impose a fixed gap between segments; the run-time
1025          gap can be different from the object-file gap.  We therefore can't
1026          assume X - _GLOBAL_OFFSET_TABLE_ is a link-time constant unless we
1027          are absolutely sure that X is in the same segment as the GOT.
1028          Unfortunately, the flexibility of linker scripts means that we
1029          can't be sure of that in general, so assume that _G_O_T_-relative
1030          accesses are never valid on VxWorks.  */
1031       if (GET_CODE (operands[1]) == LABEL_REF && !TARGET_VXWORKS_RTP)
1032         {
1033           if (mode == SImode)
1034             {
1035               emit_insn (gen_movsi_pic_label_ref (operands[0], operands[1]));
1036               return true;
1037             }
1038
1039           if (mode == DImode)
1040             {
1041               gcc_assert (TARGET_ARCH64);
1042               emit_insn (gen_movdi_pic_label_ref (operands[0], operands[1]));
1043               return true;
1044             }
1045         }
1046
1047       if (symbolic_operand (operands[1], mode))
1048         {
1049           operands[1]
1050             = sparc_legitimize_pic_address (operands[1],
1051                                             reload_in_progress
1052                                             ? operands[0] : NULL_RTX);
1053           return false;
1054         }
1055     }
1056
1057   /* If we are trying to toss an integer constant into FP registers,
1058      or loading a FP or vector constant, force it into memory.  */
1059   if (CONSTANT_P (operands[1])
1060       && REG_P (operands[0])
1061       && (SPARC_FP_REG_P (REGNO (operands[0]))
1062           || SCALAR_FLOAT_MODE_P (mode)
1063           || VECTOR_MODE_P (mode)))
1064     {
1065       /* emit_group_store will send such bogosity to us when it is
1066          not storing directly into memory.  So fix this up to avoid
1067          crashes in output_constant_pool.  */
1068       if (operands [1] == const0_rtx)
1069         operands[1] = CONST0_RTX (mode);
1070
1071       /* We can clear FP registers if TARGET_VIS, and always other regs.  */
1072       if ((TARGET_VIS || REGNO (operands[0]) < SPARC_FIRST_FP_REG)
1073           && const_zero_operand (operands[1], mode))
1074         return false;
1075
1076       if (REGNO (operands[0]) < SPARC_FIRST_FP_REG
1077           /* We are able to build any SF constant in integer registers
1078              with at most 2 instructions.  */
1079           && (mode == SFmode
1080               /* And any DF constant in integer registers.  */
1081               || (mode == DFmode
1082                   && (reload_completed || reload_in_progress))))
1083         return false;
1084
1085       operands[1] = force_const_mem (mode, operands[1]);
1086       if (!reload_in_progress)
1087         operands[1] = validize_mem (operands[1]);
1088       return false;
1089     }
1090
1091   /* Accept non-constants and valid constants unmodified.  */
1092   if (!CONSTANT_P (operands[1])
1093       || GET_CODE (operands[1]) == HIGH
1094       || input_operand (operands[1], mode))
1095     return false;
1096
1097   switch (mode)
1098     {
1099     case QImode:
1100       /* All QImode constants require only one insn, so proceed.  */
1101       break;
1102
1103     case HImode:
1104     case SImode:
1105       sparc_emit_set_const32 (operands[0], operands[1]);
1106       return true;
1107
1108     case DImode:
1109       /* input_operand should have filtered out 32-bit mode.  */
1110       sparc_emit_set_const64 (operands[0], operands[1]);
1111       return true;
1112
1113     default:
1114       gcc_unreachable ();
1115     }
1116
1117   return false;
1118 }
1119
1120 /* Load OP1, a 32-bit constant, into OP0, a register.
1121    We know it can't be done in one insn when we get
1122    here, the move expander guarantees this.  */
1123
1124 static void
1125 sparc_emit_set_const32 (rtx op0, rtx op1)
1126 {
1127   enum machine_mode mode = GET_MODE (op0);
1128   rtx temp;
1129
1130   if (reload_in_progress || reload_completed)
1131     temp = op0;
1132   else
1133     temp = gen_reg_rtx (mode);
1134
1135   if (GET_CODE (op1) == CONST_INT)
1136     {
1137       gcc_assert (!small_int_operand (op1, mode)
1138                   && !const_high_operand (op1, mode));
1139
1140       /* Emit them as real moves instead of a HIGH/LO_SUM,
1141          this way CSE can see everything and reuse intermediate
1142          values if it wants.  */
1143       emit_insn (gen_rtx_SET (VOIDmode, temp,
1144                               GEN_INT (INTVAL (op1)
1145                                 & ~(HOST_WIDE_INT)0x3ff)));
1146
1147       emit_insn (gen_rtx_SET (VOIDmode,
1148                               op0,
1149                               gen_rtx_IOR (mode, temp,
1150                                            GEN_INT (INTVAL (op1) & 0x3ff))));
1151     }
1152   else
1153     {
1154       /* A symbol, emit in the traditional way.  */
1155       emit_insn (gen_rtx_SET (VOIDmode, temp,
1156                               gen_rtx_HIGH (mode, op1)));
1157       emit_insn (gen_rtx_SET (VOIDmode,
1158                               op0, gen_rtx_LO_SUM (mode, temp, op1)));
1159     }
1160 }
1161
1162 /* Load OP1, a symbolic 64-bit constant, into OP0, a DImode register.
1163    If TEMP is nonzero, we are forbidden to use any other scratch
1164    registers.  Otherwise, we are allowed to generate them as needed.
1165
1166    Note that TEMP may have TImode if the code model is TARGET_CM_MEDANY
1167    or TARGET_CM_EMBMEDANY (see the reload_indi and reload_outdi patterns).  */
1168
1169 void
1170 sparc_emit_set_symbolic_const64 (rtx op0, rtx op1, rtx temp)
1171 {
1172   rtx temp1, temp2, temp3, temp4, temp5;
1173   rtx ti_temp = 0;
1174
1175   if (temp && GET_MODE (temp) == TImode)
1176     {
1177       ti_temp = temp;
1178       temp = gen_rtx_REG (DImode, REGNO (temp));
1179     }
1180
1181   /* SPARC-V9 code-model support.  */
1182   switch (sparc_cmodel)
1183     {
1184     case CM_MEDLOW:
1185       /* The range spanned by all instructions in the object is less
1186          than 2^31 bytes (2GB) and the distance from any instruction
1187          to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1188          than 2^31 bytes (2GB).
1189
1190          The executable must be in the low 4TB of the virtual address
1191          space.
1192
1193          sethi  %hi(symbol), %temp1
1194          or     %temp1, %lo(symbol), %reg  */
1195       if (temp)
1196         temp1 = temp;  /* op0 is allowed.  */
1197       else
1198         temp1 = gen_reg_rtx (DImode);
1199
1200       emit_insn (gen_rtx_SET (VOIDmode, temp1, gen_rtx_HIGH (DImode, op1)));
1201       emit_insn (gen_rtx_SET (VOIDmode, op0, gen_rtx_LO_SUM (DImode, temp1, op1)));
1202       break;
1203
1204     case CM_MEDMID:
1205       /* The range spanned by all instructions in the object is less
1206          than 2^31 bytes (2GB) and the distance from any instruction
1207          to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1208          than 2^31 bytes (2GB).
1209
1210          The executable must be in the low 16TB of the virtual address
1211          space.
1212
1213          sethi  %h44(symbol), %temp1
1214          or     %temp1, %m44(symbol), %temp2
1215          sllx   %temp2, 12, %temp3
1216          or     %temp3, %l44(symbol), %reg  */
1217       if (temp)
1218         {
1219           temp1 = op0;
1220           temp2 = op0;
1221           temp3 = temp;  /* op0 is allowed.  */
1222         }
1223       else
1224         {
1225           temp1 = gen_reg_rtx (DImode);
1226           temp2 = gen_reg_rtx (DImode);
1227           temp3 = gen_reg_rtx (DImode);
1228         }
1229
1230       emit_insn (gen_seth44 (temp1, op1));
1231       emit_insn (gen_setm44 (temp2, temp1, op1));
1232       emit_insn (gen_rtx_SET (VOIDmode, temp3,
1233                               gen_rtx_ASHIFT (DImode, temp2, GEN_INT (12))));
1234       emit_insn (gen_setl44 (op0, temp3, op1));
1235       break;
1236
1237     case CM_MEDANY:
1238       /* The range spanned by all instructions in the object is less
1239          than 2^31 bytes (2GB) and the distance from any instruction
1240          to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1241          than 2^31 bytes (2GB).
1242
1243          The executable can be placed anywhere in the virtual address
1244          space.
1245
1246          sethi  %hh(symbol), %temp1
1247          sethi  %lm(symbol), %temp2
1248          or     %temp1, %hm(symbol), %temp3
1249          sllx   %temp3, 32, %temp4
1250          or     %temp4, %temp2, %temp5
1251          or     %temp5, %lo(symbol), %reg  */
1252       if (temp)
1253         {
1254           /* It is possible that one of the registers we got for operands[2]
1255              might coincide with that of operands[0] (which is why we made
1256              it TImode).  Pick the other one to use as our scratch.  */
1257           if (rtx_equal_p (temp, op0))
1258             {
1259               gcc_assert (ti_temp);
1260               temp = gen_rtx_REG (DImode, REGNO (temp) + 1);
1261             }
1262           temp1 = op0;
1263           temp2 = temp;  /* op0 is _not_ allowed, see above.  */
1264           temp3 = op0;
1265           temp4 = op0;
1266           temp5 = op0;
1267         }
1268       else
1269         {
1270           temp1 = gen_reg_rtx (DImode);
1271           temp2 = gen_reg_rtx (DImode);
1272           temp3 = gen_reg_rtx (DImode);
1273           temp4 = gen_reg_rtx (DImode);
1274           temp5 = gen_reg_rtx (DImode);
1275         }
1276
1277       emit_insn (gen_sethh (temp1, op1));
1278       emit_insn (gen_setlm (temp2, op1));
1279       emit_insn (gen_sethm (temp3, temp1, op1));
1280       emit_insn (gen_rtx_SET (VOIDmode, temp4,
1281                               gen_rtx_ASHIFT (DImode, temp3, GEN_INT (32))));
1282       emit_insn (gen_rtx_SET (VOIDmode, temp5,
1283                               gen_rtx_PLUS (DImode, temp4, temp2)));
1284       emit_insn (gen_setlo (op0, temp5, op1));
1285       break;
1286
1287     case CM_EMBMEDANY:
1288       /* Old old old backwards compatibility kruft here.
1289          Essentially it is MEDLOW with a fixed 64-bit
1290          virtual base added to all data segment addresses.
1291          Text-segment stuff is computed like MEDANY, we can't
1292          reuse the code above because the relocation knobs
1293          look different.
1294
1295          Data segment:  sethi   %hi(symbol), %temp1
1296                         add     %temp1, EMBMEDANY_BASE_REG, %temp2
1297                         or      %temp2, %lo(symbol), %reg  */
1298       if (data_segment_operand (op1, GET_MODE (op1)))
1299         {
1300           if (temp)
1301             {
1302               temp1 = temp;  /* op0 is allowed.  */
1303               temp2 = op0;
1304             }
1305           else
1306             {
1307               temp1 = gen_reg_rtx (DImode);
1308               temp2 = gen_reg_rtx (DImode);
1309             }
1310
1311           emit_insn (gen_embmedany_sethi (temp1, op1));
1312           emit_insn (gen_embmedany_brsum (temp2, temp1));
1313           emit_insn (gen_embmedany_losum (op0, temp2, op1));
1314         }
1315
1316       /* Text segment:  sethi   %uhi(symbol), %temp1
1317                         sethi   %hi(symbol), %temp2
1318                         or      %temp1, %ulo(symbol), %temp3
1319                         sllx    %temp3, 32, %temp4
1320                         or      %temp4, %temp2, %temp5
1321                         or      %temp5, %lo(symbol), %reg  */
1322       else
1323         {
1324           if (temp)
1325             {
1326               /* It is possible that one of the registers we got for operands[2]
1327                  might coincide with that of operands[0] (which is why we made
1328                  it TImode).  Pick the other one to use as our scratch.  */
1329               if (rtx_equal_p (temp, op0))
1330                 {
1331                   gcc_assert (ti_temp);
1332                   temp = gen_rtx_REG (DImode, REGNO (temp) + 1);
1333                 }
1334               temp1 = op0;
1335               temp2 = temp;  /* op0 is _not_ allowed, see above.  */
1336               temp3 = op0;
1337               temp4 = op0;
1338               temp5 = op0;
1339             }
1340           else
1341             {
1342               temp1 = gen_reg_rtx (DImode);
1343               temp2 = gen_reg_rtx (DImode);
1344               temp3 = gen_reg_rtx (DImode);
1345               temp4 = gen_reg_rtx (DImode);
1346               temp5 = gen_reg_rtx (DImode);
1347             }
1348
1349           emit_insn (gen_embmedany_textuhi (temp1, op1));
1350           emit_insn (gen_embmedany_texthi  (temp2, op1));
1351           emit_insn (gen_embmedany_textulo (temp3, temp1, op1));
1352           emit_insn (gen_rtx_SET (VOIDmode, temp4,
1353                                   gen_rtx_ASHIFT (DImode, temp3, GEN_INT (32))));
1354           emit_insn (gen_rtx_SET (VOIDmode, temp5,
1355                                   gen_rtx_PLUS (DImode, temp4, temp2)));
1356           emit_insn (gen_embmedany_textlo  (op0, temp5, op1));
1357         }
1358       break;
1359
1360     default:
1361       gcc_unreachable ();
1362     }
1363 }
1364
1365 #if HOST_BITS_PER_WIDE_INT == 32
1366 static void
1367 sparc_emit_set_const64 (rtx op0 ATTRIBUTE_UNUSED, rtx op1 ATTRIBUTE_UNUSED)
1368 {
1369   gcc_unreachable ();
1370 }
1371 #else
1372 /* These avoid problems when cross compiling.  If we do not
1373    go through all this hair then the optimizer will see
1374    invalid REG_EQUAL notes or in some cases none at all.  */
1375 static rtx gen_safe_HIGH64 (rtx, HOST_WIDE_INT);
1376 static rtx gen_safe_SET64 (rtx, HOST_WIDE_INT);
1377 static rtx gen_safe_OR64 (rtx, HOST_WIDE_INT);
1378 static rtx gen_safe_XOR64 (rtx, HOST_WIDE_INT);
1379
1380 /* The optimizer is not to assume anything about exactly
1381    which bits are set for a HIGH, they are unspecified.
1382    Unfortunately this leads to many missed optimizations
1383    during CSE.  We mask out the non-HIGH bits, and matches
1384    a plain movdi, to alleviate this problem.  */
1385 static rtx
1386 gen_safe_HIGH64 (rtx dest, HOST_WIDE_INT val)
1387 {
1388   return gen_rtx_SET (VOIDmode, dest, GEN_INT (val & ~(HOST_WIDE_INT)0x3ff));
1389 }
1390
1391 static rtx
1392 gen_safe_SET64 (rtx dest, HOST_WIDE_INT val)
1393 {
1394   return gen_rtx_SET (VOIDmode, dest, GEN_INT (val));
1395 }
1396
1397 static rtx
1398 gen_safe_OR64 (rtx src, HOST_WIDE_INT val)
1399 {
1400   return gen_rtx_IOR (DImode, src, GEN_INT (val));
1401 }
1402
1403 static rtx
1404 gen_safe_XOR64 (rtx src, HOST_WIDE_INT val)
1405 {
1406   return gen_rtx_XOR (DImode, src, GEN_INT (val));
1407 }
1408
1409 /* Worker routines for 64-bit constant formation on arch64.
1410    One of the key things to be doing in these emissions is
1411    to create as many temp REGs as possible.  This makes it
1412    possible for half-built constants to be used later when
1413    such values are similar to something required later on.
1414    Without doing this, the optimizer cannot see such
1415    opportunities.  */
1416
1417 static void sparc_emit_set_const64_quick1 (rtx, rtx,
1418                                            unsigned HOST_WIDE_INT, int);
1419
1420 static void
1421 sparc_emit_set_const64_quick1 (rtx op0, rtx temp,
1422                                unsigned HOST_WIDE_INT low_bits, int is_neg)
1423 {
1424   unsigned HOST_WIDE_INT high_bits;
1425
1426   if (is_neg)
1427     high_bits = (~low_bits) & 0xffffffff;
1428   else
1429     high_bits = low_bits;
1430
1431   emit_insn (gen_safe_HIGH64 (temp, high_bits));
1432   if (!is_neg)
1433     {
1434       emit_insn (gen_rtx_SET (VOIDmode, op0,
1435                               gen_safe_OR64 (temp, (high_bits & 0x3ff))));
1436     }
1437   else
1438     {
1439       /* If we are XOR'ing with -1, then we should emit a one's complement
1440          instead.  This way the combiner will notice logical operations
1441          such as ANDN later on and substitute.  */
1442       if ((low_bits & 0x3ff) == 0x3ff)
1443         {
1444           emit_insn (gen_rtx_SET (VOIDmode, op0,
1445                                   gen_rtx_NOT (DImode, temp)));
1446         }
1447       else
1448         {
1449           emit_insn (gen_rtx_SET (VOIDmode, op0,
1450                                   gen_safe_XOR64 (temp,
1451                                                   (-(HOST_WIDE_INT)0x400
1452                                                    | (low_bits & 0x3ff)))));
1453         }
1454     }
1455 }
1456
1457 static void sparc_emit_set_const64_quick2 (rtx, rtx, unsigned HOST_WIDE_INT,
1458                                            unsigned HOST_WIDE_INT, int);
1459
1460 static void
1461 sparc_emit_set_const64_quick2 (rtx op0, rtx temp,
1462                                unsigned HOST_WIDE_INT high_bits,
1463                                unsigned HOST_WIDE_INT low_immediate,
1464                                int shift_count)
1465 {
1466   rtx temp2 = op0;
1467
1468   if ((high_bits & 0xfffffc00) != 0)
1469     {
1470       emit_insn (gen_safe_HIGH64 (temp, high_bits));
1471       if ((high_bits & ~0xfffffc00) != 0)
1472         emit_insn (gen_rtx_SET (VOIDmode, op0,
1473                                 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
1474       else
1475         temp2 = temp;
1476     }
1477   else
1478     {
1479       emit_insn (gen_safe_SET64 (temp, high_bits));
1480       temp2 = temp;
1481     }
1482
1483   /* Now shift it up into place.  */
1484   emit_insn (gen_rtx_SET (VOIDmode, op0,
1485                           gen_rtx_ASHIFT (DImode, temp2,
1486                                           GEN_INT (shift_count))));
1487
1488   /* If there is a low immediate part piece, finish up by
1489      putting that in as well.  */
1490   if (low_immediate != 0)
1491     emit_insn (gen_rtx_SET (VOIDmode, op0,
1492                             gen_safe_OR64 (op0, low_immediate)));
1493 }
1494
1495 static void sparc_emit_set_const64_longway (rtx, rtx, unsigned HOST_WIDE_INT,
1496                                             unsigned HOST_WIDE_INT);
1497
1498 /* Full 64-bit constant decomposition.  Even though this is the
1499    'worst' case, we still optimize a few things away.  */
1500 static void
1501 sparc_emit_set_const64_longway (rtx op0, rtx temp,
1502                                 unsigned HOST_WIDE_INT high_bits,
1503                                 unsigned HOST_WIDE_INT low_bits)
1504 {
1505   rtx sub_temp;
1506
1507   if (reload_in_progress || reload_completed)
1508     sub_temp = op0;
1509   else
1510     sub_temp = gen_reg_rtx (DImode);
1511
1512   if ((high_bits & 0xfffffc00) != 0)
1513     {
1514       emit_insn (gen_safe_HIGH64 (temp, high_bits));
1515       if ((high_bits & ~0xfffffc00) != 0)
1516         emit_insn (gen_rtx_SET (VOIDmode,
1517                                 sub_temp,
1518                                 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
1519       else
1520         sub_temp = temp;
1521     }
1522   else
1523     {
1524       emit_insn (gen_safe_SET64 (temp, high_bits));
1525       sub_temp = temp;
1526     }
1527
1528   if (!reload_in_progress && !reload_completed)
1529     {
1530       rtx temp2 = gen_reg_rtx (DImode);
1531       rtx temp3 = gen_reg_rtx (DImode);
1532       rtx temp4 = gen_reg_rtx (DImode);
1533
1534       emit_insn (gen_rtx_SET (VOIDmode, temp4,
1535                               gen_rtx_ASHIFT (DImode, sub_temp,
1536                                               GEN_INT (32))));
1537
1538       emit_insn (gen_safe_HIGH64 (temp2, low_bits));
1539       if ((low_bits & ~0xfffffc00) != 0)
1540         {
1541           emit_insn (gen_rtx_SET (VOIDmode, temp3,
1542                                   gen_safe_OR64 (temp2, (low_bits & 0x3ff))));
1543           emit_insn (gen_rtx_SET (VOIDmode, op0,
1544                                   gen_rtx_PLUS (DImode, temp4, temp3)));
1545         }
1546       else
1547         {
1548           emit_insn (gen_rtx_SET (VOIDmode, op0,
1549                                   gen_rtx_PLUS (DImode, temp4, temp2)));
1550         }
1551     }
1552   else
1553     {
1554       rtx low1 = GEN_INT ((low_bits >> (32 - 12))          & 0xfff);
1555       rtx low2 = GEN_INT ((low_bits >> (32 - 12 - 12))     & 0xfff);
1556       rtx low3 = GEN_INT ((low_bits >> (32 - 12 - 12 - 8)) & 0x0ff);
1557       int to_shift = 12;
1558
1559       /* We are in the middle of reload, so this is really
1560          painful.  However we do still make an attempt to
1561          avoid emitting truly stupid code.  */
1562       if (low1 != const0_rtx)
1563         {
1564           emit_insn (gen_rtx_SET (VOIDmode, op0,
1565                                   gen_rtx_ASHIFT (DImode, sub_temp,
1566                                                   GEN_INT (to_shift))));
1567           emit_insn (gen_rtx_SET (VOIDmode, op0,
1568                                   gen_rtx_IOR (DImode, op0, low1)));
1569           sub_temp = op0;
1570           to_shift = 12;
1571         }
1572       else
1573         {
1574           to_shift += 12;
1575         }
1576       if (low2 != const0_rtx)
1577         {
1578           emit_insn (gen_rtx_SET (VOIDmode, op0,
1579                                   gen_rtx_ASHIFT (DImode, sub_temp,
1580                                                   GEN_INT (to_shift))));
1581           emit_insn (gen_rtx_SET (VOIDmode, op0,
1582                                   gen_rtx_IOR (DImode, op0, low2)));
1583           sub_temp = op0;
1584           to_shift = 8;
1585         }
1586       else
1587         {
1588           to_shift += 8;
1589         }
1590       emit_insn (gen_rtx_SET (VOIDmode, op0,
1591                               gen_rtx_ASHIFT (DImode, sub_temp,
1592                                               GEN_INT (to_shift))));
1593       if (low3 != const0_rtx)
1594         emit_insn (gen_rtx_SET (VOIDmode, op0,
1595                                 gen_rtx_IOR (DImode, op0, low3)));
1596       /* phew...  */
1597     }
1598 }
1599
1600 /* Analyze a 64-bit constant for certain properties.  */
1601 static void analyze_64bit_constant (unsigned HOST_WIDE_INT,
1602                                     unsigned HOST_WIDE_INT,
1603                                     int *, int *, int *);
1604
1605 static void
1606 analyze_64bit_constant (unsigned HOST_WIDE_INT high_bits,
1607                         unsigned HOST_WIDE_INT low_bits,
1608                         int *hbsp, int *lbsp, int *abbasp)
1609 {
1610   int lowest_bit_set, highest_bit_set, all_bits_between_are_set;
1611   int i;
1612
1613   lowest_bit_set = highest_bit_set = -1;
1614   i = 0;
1615   do
1616     {
1617       if ((lowest_bit_set == -1)
1618           && ((low_bits >> i) & 1))
1619         lowest_bit_set = i;
1620       if ((highest_bit_set == -1)
1621           && ((high_bits >> (32 - i - 1)) & 1))
1622         highest_bit_set = (64 - i - 1);
1623     }
1624   while (++i < 32
1625          && ((highest_bit_set == -1)
1626              || (lowest_bit_set == -1)));
1627   if (i == 32)
1628     {
1629       i = 0;
1630       do
1631         {
1632           if ((lowest_bit_set == -1)
1633               && ((high_bits >> i) & 1))
1634             lowest_bit_set = i + 32;
1635           if ((highest_bit_set == -1)
1636               && ((low_bits >> (32 - i - 1)) & 1))
1637             highest_bit_set = 32 - i - 1;
1638         }
1639       while (++i < 32
1640              && ((highest_bit_set == -1)
1641                  || (lowest_bit_set == -1)));
1642     }
1643   /* If there are no bits set this should have gone out
1644      as one instruction!  */
1645   gcc_assert (lowest_bit_set != -1 && highest_bit_set != -1);
1646   all_bits_between_are_set = 1;
1647   for (i = lowest_bit_set; i <= highest_bit_set; i++)
1648     {
1649       if (i < 32)
1650         {
1651           if ((low_bits & (1 << i)) != 0)
1652             continue;
1653         }
1654       else
1655         {
1656           if ((high_bits & (1 << (i - 32))) != 0)
1657             continue;
1658         }
1659       all_bits_between_are_set = 0;
1660       break;
1661     }
1662   *hbsp = highest_bit_set;
1663   *lbsp = lowest_bit_set;
1664   *abbasp = all_bits_between_are_set;
1665 }
1666
1667 static int const64_is_2insns (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
1668
1669 static int
1670 const64_is_2insns (unsigned HOST_WIDE_INT high_bits,
1671                    unsigned HOST_WIDE_INT low_bits)
1672 {
1673   int highest_bit_set, lowest_bit_set, all_bits_between_are_set;
1674
1675   if (high_bits == 0
1676       || high_bits == 0xffffffff)
1677     return 1;
1678
1679   analyze_64bit_constant (high_bits, low_bits,
1680                           &highest_bit_set, &lowest_bit_set,
1681                           &all_bits_between_are_set);
1682
1683   if ((highest_bit_set == 63
1684        || lowest_bit_set == 0)
1685       && all_bits_between_are_set != 0)
1686     return 1;
1687
1688   if ((highest_bit_set - lowest_bit_set) < 21)
1689     return 1;
1690
1691   return 0;
1692 }
1693
1694 static unsigned HOST_WIDE_INT create_simple_focus_bits (unsigned HOST_WIDE_INT,
1695                                                         unsigned HOST_WIDE_INT,
1696                                                         int, int);
1697
1698 static unsigned HOST_WIDE_INT
1699 create_simple_focus_bits (unsigned HOST_WIDE_INT high_bits,
1700                           unsigned HOST_WIDE_INT low_bits,
1701                           int lowest_bit_set, int shift)
1702 {
1703   HOST_WIDE_INT hi, lo;
1704
1705   if (lowest_bit_set < 32)
1706     {
1707       lo = (low_bits >> lowest_bit_set) << shift;
1708       hi = ((high_bits << (32 - lowest_bit_set)) << shift);
1709     }
1710   else
1711     {
1712       lo = 0;
1713       hi = ((high_bits >> (lowest_bit_set - 32)) << shift);
1714     }
1715   gcc_assert (! (hi & lo));
1716   return (hi | lo);
1717 }
1718
1719 /* Here we are sure to be arch64 and this is an integer constant
1720    being loaded into a register.  Emit the most efficient
1721    insn sequence possible.  Detection of all the 1-insn cases
1722    has been done already.  */
1723 static void
1724 sparc_emit_set_const64 (rtx op0, rtx op1)
1725 {
1726   unsigned HOST_WIDE_INT high_bits, low_bits;
1727   int lowest_bit_set, highest_bit_set;
1728   int all_bits_between_are_set;
1729   rtx temp = 0;
1730
1731   /* Sanity check that we know what we are working with.  */
1732   gcc_assert (TARGET_ARCH64
1733               && (GET_CODE (op0) == SUBREG
1734                   || (REG_P (op0) && ! SPARC_FP_REG_P (REGNO (op0)))));
1735
1736   if (reload_in_progress || reload_completed)
1737     temp = op0;
1738
1739   if (GET_CODE (op1) != CONST_INT)
1740     {
1741       sparc_emit_set_symbolic_const64 (op0, op1, temp);
1742       return;
1743     }
1744
1745   if (! temp)
1746     temp = gen_reg_rtx (DImode);
1747
1748   high_bits = ((INTVAL (op1) >> 32) & 0xffffffff);
1749   low_bits = (INTVAL (op1) & 0xffffffff);
1750
1751   /* low_bits   bits 0  --> 31
1752      high_bits  bits 32 --> 63  */
1753
1754   analyze_64bit_constant (high_bits, low_bits,
1755                           &highest_bit_set, &lowest_bit_set,
1756                           &all_bits_between_are_set);
1757
1758   /* First try for a 2-insn sequence.  */
1759
1760   /* These situations are preferred because the optimizer can
1761    * do more things with them:
1762    * 1) mov     -1, %reg
1763    *    sllx    %reg, shift, %reg
1764    * 2) mov     -1, %reg
1765    *    srlx    %reg, shift, %reg
1766    * 3) mov     some_small_const, %reg
1767    *    sllx    %reg, shift, %reg
1768    */
1769   if (((highest_bit_set == 63
1770         || lowest_bit_set == 0)
1771        && all_bits_between_are_set != 0)
1772       || ((highest_bit_set - lowest_bit_set) < 12))
1773     {
1774       HOST_WIDE_INT the_const = -1;
1775       int shift = lowest_bit_set;
1776
1777       if ((highest_bit_set != 63
1778            && lowest_bit_set != 0)
1779           || all_bits_between_are_set == 0)
1780         {
1781           the_const =
1782             create_simple_focus_bits (high_bits, low_bits,
1783                                       lowest_bit_set, 0);
1784         }
1785       else if (lowest_bit_set == 0)
1786         shift = -(63 - highest_bit_set);
1787
1788       gcc_assert (SPARC_SIMM13_P (the_const));
1789       gcc_assert (shift != 0);
1790
1791       emit_insn (gen_safe_SET64 (temp, the_const));
1792       if (shift > 0)
1793         emit_insn (gen_rtx_SET (VOIDmode,
1794                                 op0,
1795                                 gen_rtx_ASHIFT (DImode,
1796                                                 temp,
1797                                                 GEN_INT (shift))));
1798       else if (shift < 0)
1799         emit_insn (gen_rtx_SET (VOIDmode,
1800                                 op0,
1801                                 gen_rtx_LSHIFTRT (DImode,
1802                                                   temp,
1803                                                   GEN_INT (-shift))));
1804       return;
1805     }
1806
1807   /* Now a range of 22 or less bits set somewhere.
1808    * 1) sethi   %hi(focus_bits), %reg
1809    *    sllx    %reg, shift, %reg
1810    * 2) sethi   %hi(focus_bits), %reg
1811    *    srlx    %reg, shift, %reg
1812    */
1813   if ((highest_bit_set - lowest_bit_set) < 21)
1814     {
1815       unsigned HOST_WIDE_INT focus_bits =
1816         create_simple_focus_bits (high_bits, low_bits,
1817                                   lowest_bit_set, 10);
1818
1819       gcc_assert (SPARC_SETHI_P (focus_bits));
1820       gcc_assert (lowest_bit_set != 10);
1821
1822       emit_insn (gen_safe_HIGH64 (temp, focus_bits));
1823
1824       /* If lowest_bit_set == 10 then a sethi alone could have done it.  */
1825       if (lowest_bit_set < 10)
1826         emit_insn (gen_rtx_SET (VOIDmode,
1827                                 op0,
1828                                 gen_rtx_LSHIFTRT (DImode, temp,
1829                                                   GEN_INT (10 - lowest_bit_set))));
1830       else if (lowest_bit_set > 10)
1831         emit_insn (gen_rtx_SET (VOIDmode,
1832                                 op0,
1833                                 gen_rtx_ASHIFT (DImode, temp,
1834                                                 GEN_INT (lowest_bit_set - 10))));
1835       return;
1836     }
1837
1838   /* 1) sethi   %hi(low_bits), %reg
1839    *    or      %reg, %lo(low_bits), %reg
1840    * 2) sethi   %hi(~low_bits), %reg
1841    *    xor     %reg, %lo(-0x400 | (low_bits & 0x3ff)), %reg
1842    */
1843   if (high_bits == 0
1844       || high_bits == 0xffffffff)
1845     {
1846       sparc_emit_set_const64_quick1 (op0, temp, low_bits,
1847                                      (high_bits == 0xffffffff));
1848       return;
1849     }
1850
1851   /* Now, try 3-insn sequences.  */
1852
1853   /* 1) sethi   %hi(high_bits), %reg
1854    *    or      %reg, %lo(high_bits), %reg
1855    *    sllx    %reg, 32, %reg
1856    */
1857   if (low_bits == 0)
1858     {
1859       sparc_emit_set_const64_quick2 (op0, temp, high_bits, 0, 32);
1860       return;
1861     }
1862
1863   /* We may be able to do something quick
1864      when the constant is negated, so try that.  */
1865   if (const64_is_2insns ((~high_bits) & 0xffffffff,
1866                          (~low_bits) & 0xfffffc00))
1867     {
1868       /* NOTE: The trailing bits get XOR'd so we need the
1869          non-negated bits, not the negated ones.  */
1870       unsigned HOST_WIDE_INT trailing_bits = low_bits & 0x3ff;
1871
1872       if ((((~high_bits) & 0xffffffff) == 0
1873            && ((~low_bits) & 0x80000000) == 0)
1874           || (((~high_bits) & 0xffffffff) == 0xffffffff
1875               && ((~low_bits) & 0x80000000) != 0))
1876         {
1877           unsigned HOST_WIDE_INT fast_int = (~low_bits & 0xffffffff);
1878
1879           if ((SPARC_SETHI_P (fast_int)
1880                && (~high_bits & 0xffffffff) == 0)
1881               || SPARC_SIMM13_P (fast_int))
1882             emit_insn (gen_safe_SET64 (temp, fast_int));
1883           else
1884             sparc_emit_set_const64 (temp, GEN_INT (fast_int));
1885         }
1886       else
1887         {
1888           rtx negated_const;
1889           negated_const = GEN_INT (((~low_bits) & 0xfffffc00) |
1890                                    (((HOST_WIDE_INT)((~high_bits) & 0xffffffff))<<32));
1891           sparc_emit_set_const64 (temp, negated_const);
1892         }
1893
1894       /* If we are XOR'ing with -1, then we should emit a one's complement
1895          instead.  This way the combiner will notice logical operations
1896          such as ANDN later on and substitute.  */
1897       if (trailing_bits == 0x3ff)
1898         {
1899           emit_insn (gen_rtx_SET (VOIDmode, op0,
1900                                   gen_rtx_NOT (DImode, temp)));
1901         }
1902       else
1903         {
1904           emit_insn (gen_rtx_SET (VOIDmode,
1905                                   op0,
1906                                   gen_safe_XOR64 (temp,
1907                                                   (-0x400 | trailing_bits))));
1908         }
1909       return;
1910     }
1911
1912   /* 1) sethi   %hi(xxx), %reg
1913    *    or      %reg, %lo(xxx), %reg
1914    *    sllx    %reg, yyy, %reg
1915    *
1916    * ??? This is just a generalized version of the low_bits==0
1917    * thing above, FIXME...
1918    */
1919   if ((highest_bit_set - lowest_bit_set) < 32)
1920     {
1921       unsigned HOST_WIDE_INT focus_bits =
1922         create_simple_focus_bits (high_bits, low_bits,
1923                                   lowest_bit_set, 0);
1924
1925       /* We can't get here in this state.  */
1926       gcc_assert (highest_bit_set >= 32 && lowest_bit_set < 32);
1927
1928       /* So what we know is that the set bits straddle the
1929          middle of the 64-bit word.  */
1930       sparc_emit_set_const64_quick2 (op0, temp,
1931                                      focus_bits, 0,
1932                                      lowest_bit_set);
1933       return;
1934     }
1935
1936   /* 1) sethi   %hi(high_bits), %reg
1937    *    or      %reg, %lo(high_bits), %reg
1938    *    sllx    %reg, 32, %reg
1939    *    or      %reg, low_bits, %reg
1940    */
1941   if (SPARC_SIMM13_P(low_bits)
1942       && ((int)low_bits > 0))
1943     {
1944       sparc_emit_set_const64_quick2 (op0, temp, high_bits, low_bits, 32);
1945       return;
1946     }
1947
1948   /* The easiest way when all else fails, is full decomposition.  */
1949   sparc_emit_set_const64_longway (op0, temp, high_bits, low_bits);
1950 }
1951 #endif /* HOST_BITS_PER_WIDE_INT == 32 */
1952
1953 /* Given a comparison code (EQ, NE, etc.) and the first operand of a COMPARE,
1954    return the mode to be used for the comparison.  For floating-point,
1955    CCFP[E]mode is used.  CC_NOOVmode should be used when the first operand
1956    is a PLUS, MINUS, NEG, or ASHIFT.  CCmode should be used when no special
1957    processing is needed.  */
1958
1959 enum machine_mode
1960 select_cc_mode (enum rtx_code op, rtx x, rtx y ATTRIBUTE_UNUSED)
1961 {
1962   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
1963     {
1964       switch (op)
1965         {
1966         case EQ:
1967         case NE:
1968         case UNORDERED:
1969         case ORDERED:
1970         case UNLT:
1971         case UNLE:
1972         case UNGT:
1973         case UNGE:
1974         case UNEQ:
1975         case LTGT:
1976           return CCFPmode;
1977
1978         case LT:
1979         case LE:
1980         case GT:
1981         case GE:
1982           return CCFPEmode;
1983
1984         default:
1985           gcc_unreachable ();
1986         }
1987     }
1988   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
1989            || GET_CODE (x) == NEG || GET_CODE (x) == ASHIFT)
1990     {
1991       if (TARGET_ARCH64 && GET_MODE (x) == DImode)
1992         return CCX_NOOVmode;
1993       else
1994         return CC_NOOVmode;
1995     }
1996   else
1997     {
1998       if (TARGET_ARCH64 && GET_MODE (x) == DImode)
1999         return CCXmode;
2000       else
2001         return CCmode;
2002     }
2003 }
2004
2005 /* Emit the compare insn and return the CC reg for a CODE comparison
2006    with operands X and Y.  */
2007
2008 static rtx
2009 gen_compare_reg_1 (enum rtx_code code, rtx x, rtx y)
2010 {
2011   enum machine_mode mode;
2012   rtx cc_reg;
2013
2014   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
2015     return x;
2016
2017   mode = SELECT_CC_MODE (code, x, y);
2018
2019   /* ??? We don't have movcc patterns so we cannot generate pseudo regs for the
2020      fcc regs (cse can't tell they're really call clobbered regs and will
2021      remove a duplicate comparison even if there is an intervening function
2022      call - it will then try to reload the cc reg via an int reg which is why
2023      we need the movcc patterns).  It is possible to provide the movcc
2024      patterns by using the ldxfsr/stxfsr v9 insns.  I tried it: you need two
2025      registers (say %g1,%g5) and it takes about 6 insns.  A better fix would be
2026      to tell cse that CCFPE mode registers (even pseudos) are call
2027      clobbered.  */
2028
2029   /* ??? This is an experiment.  Rather than making changes to cse which may
2030      or may not be easy/clean, we do our own cse.  This is possible because
2031      we will generate hard registers.  Cse knows they're call clobbered (it
2032      doesn't know the same thing about pseudos). If we guess wrong, no big
2033      deal, but if we win, great!  */
2034
2035   if (TARGET_V9 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2036 #if 1 /* experiment */
2037     {
2038       int reg;
2039       /* We cycle through the registers to ensure they're all exercised.  */
2040       static int next_fcc_reg = 0;
2041       /* Previous x,y for each fcc reg.  */
2042       static rtx prev_args[4][2];
2043
2044       /* Scan prev_args for x,y.  */
2045       for (reg = 0; reg < 4; reg++)
2046         if (prev_args[reg][0] == x && prev_args[reg][1] == y)
2047           break;
2048       if (reg == 4)
2049         {
2050           reg = next_fcc_reg;
2051           prev_args[reg][0] = x;
2052           prev_args[reg][1] = y;
2053           next_fcc_reg = (next_fcc_reg + 1) & 3;
2054         }
2055       cc_reg = gen_rtx_REG (mode, reg + SPARC_FIRST_V9_FCC_REG);
2056     }
2057 #else
2058     cc_reg = gen_reg_rtx (mode);
2059 #endif /* ! experiment */
2060   else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2061     cc_reg = gen_rtx_REG (mode, SPARC_FCC_REG);
2062   else
2063     cc_reg = gen_rtx_REG (mode, SPARC_ICC_REG);
2064
2065   /* We shouldn't get there for TFmode if !TARGET_HARD_QUAD.  If we do, this
2066      will only result in an unrecognizable insn so no point in asserting.  */
2067   emit_insn (gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y)));
2068
2069   return cc_reg;
2070 }
2071
2072
2073 /* Emit the compare insn and return the CC reg for the comparison in CMP.  */
2074
2075 rtx
2076 gen_compare_reg (rtx cmp)
2077 {
2078   return gen_compare_reg_1 (GET_CODE (cmp), XEXP (cmp, 0), XEXP (cmp, 1));
2079 }
2080
2081 /* This function is used for v9 only.
2082    DEST is the target of the Scc insn.
2083    CODE is the code for an Scc's comparison.
2084    X and Y are the values we compare.
2085
2086    This function is needed to turn
2087
2088            (set (reg:SI 110)
2089                (gt (reg:CCX 100 %icc)
2090                    (const_int 0)))
2091    into
2092            (set (reg:SI 110)
2093                (gt:DI (reg:CCX 100 %icc)
2094                    (const_int 0)))
2095
2096    IE: The instruction recognizer needs to see the mode of the comparison to
2097    find the right instruction. We could use "gt:DI" right in the
2098    define_expand, but leaving it out allows us to handle DI, SI, etc.  */
2099
2100 static int
2101 gen_v9_scc (rtx dest, enum rtx_code compare_code, rtx x, rtx y)
2102 {
2103   if (! TARGET_ARCH64
2104       && (GET_MODE (x) == DImode
2105           || GET_MODE (dest) == DImode))
2106     return 0;
2107
2108   /* Try to use the movrCC insns.  */
2109   if (TARGET_ARCH64
2110       && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT
2111       && y == const0_rtx
2112       && v9_regcmp_p (compare_code))
2113     {
2114       rtx op0 = x;
2115       rtx temp;
2116
2117       /* Special case for op0 != 0.  This can be done with one instruction if
2118          dest == x.  */
2119
2120       if (compare_code == NE
2121           && GET_MODE (dest) == DImode
2122           && rtx_equal_p (op0, dest))
2123         {
2124           emit_insn (gen_rtx_SET (VOIDmode, dest,
2125                               gen_rtx_IF_THEN_ELSE (DImode,
2126                                        gen_rtx_fmt_ee (compare_code, DImode,
2127                                                        op0, const0_rtx),
2128                                        const1_rtx,
2129                                        dest)));
2130           return 1;
2131         }
2132
2133       if (reg_overlap_mentioned_p (dest, op0))
2134         {
2135           /* Handle the case where dest == x.
2136              We "early clobber" the result.  */
2137           op0 = gen_reg_rtx (GET_MODE (x));
2138           emit_move_insn (op0, x);
2139         }
2140
2141       emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
2142       if (GET_MODE (op0) != DImode)
2143         {
2144           temp = gen_reg_rtx (DImode);
2145           convert_move (temp, op0, 0);
2146         }
2147       else
2148         temp = op0;
2149       emit_insn (gen_rtx_SET (VOIDmode, dest,
2150                           gen_rtx_IF_THEN_ELSE (GET_MODE (dest),
2151                                    gen_rtx_fmt_ee (compare_code, DImode,
2152                                                    temp, const0_rtx),
2153                                    const1_rtx,
2154                                    dest)));
2155       return 1;
2156     }
2157   else
2158     {
2159       x = gen_compare_reg_1 (compare_code, x, y);
2160       y = const0_rtx;
2161
2162       gcc_assert (GET_MODE (x) != CC_NOOVmode
2163                   && GET_MODE (x) != CCX_NOOVmode);
2164
2165       emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
2166       emit_insn (gen_rtx_SET (VOIDmode, dest,
2167                           gen_rtx_IF_THEN_ELSE (GET_MODE (dest),
2168                                    gen_rtx_fmt_ee (compare_code,
2169                                                    GET_MODE (x), x, y),
2170                                     const1_rtx, dest)));
2171       return 1;
2172     }
2173 }
2174
2175
2176 /* Emit an scc insn.  For seq, sne, sgeu, and sltu, we can do this
2177    without jumps using the addx/subx instructions.  */
2178
2179 bool
2180 emit_scc_insn (rtx operands[])
2181 {
2182   rtx tem;
2183   rtx x;
2184   rtx y;
2185   enum rtx_code code;
2186
2187   /* The quad-word fp compare library routines all return nonzero to indicate
2188      true, which is different from the equivalent libgcc routines, so we must
2189      handle them specially here.  */
2190   if (GET_MODE (operands[2]) == TFmode && ! TARGET_HARD_QUAD)
2191     {
2192       operands[1] = sparc_emit_float_lib_cmp (operands[2], operands[3],
2193                                               GET_CODE (operands[1]));
2194       operands[2] = XEXP (operands[1], 0);
2195       operands[3] = XEXP (operands[1], 1);
2196     }
2197
2198   code = GET_CODE (operands[1]);
2199   x = operands[2];
2200   y = operands[3];
2201
2202   /* For seq/sne on v9 we use the same code as v8 (the addx/subx method has
2203      more applications).  The exception to this is "reg != 0" which can
2204      be done in one instruction on v9 (so we do it).  */
2205   if (code == EQ)
2206     {
2207       if (GET_MODE (x) == SImode)
2208         {
2209           rtx pat = gen_seqsi_special (operands[0], x, y);
2210           emit_insn (pat);
2211           return true;
2212         }
2213       else if (GET_MODE (x) == DImode)
2214         {
2215           rtx pat = gen_seqdi_special (operands[0], x, y);
2216           emit_insn (pat);
2217           return true;
2218         }
2219     }
2220
2221   if (code == NE)
2222     {
2223       if (GET_MODE (x) == SImode)
2224         {
2225           rtx pat = gen_snesi_special (operands[0], x, y);
2226           emit_insn (pat);
2227           return true;
2228         }
2229       else if (GET_MODE (x) == DImode)
2230         {
2231           rtx pat = gen_snedi_special (operands[0], x, y);
2232           emit_insn (pat);
2233           return true;
2234         }
2235     }
2236
2237   /* For the rest, on v9 we can use conditional moves.  */
2238
2239   if (TARGET_V9)
2240     {
2241       if (gen_v9_scc (operands[0], code, x, y))
2242         return true;
2243     }
2244
2245   /* We can do LTU and GEU using the addx/subx instructions too.  And
2246      for GTU/LEU, if both operands are registers swap them and fall
2247      back to the easy case.  */
2248   if (code == GTU || code == LEU)
2249     {
2250       if ((GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
2251           && (GET_CODE (y) == REG || GET_CODE (y) == SUBREG))
2252         {
2253           tem = x;
2254           x = y;
2255           y = tem;
2256           code = swap_condition (code);
2257         }
2258     }
2259
2260   if (code == LTU || code == GEU)
2261     {
2262       emit_insn (gen_rtx_SET (VOIDmode, operands[0],
2263                               gen_rtx_fmt_ee (code, SImode,
2264                                               gen_compare_reg_1 (code, x, y),
2265                                               const0_rtx)));
2266       return true;
2267     }
2268
2269   /* Nope, do branches.  */
2270   return false;
2271 }
2272
2273 /* Emit a conditional jump insn for the v9 architecture using comparison code
2274    CODE and jump target LABEL.
2275    This function exists to take advantage of the v9 brxx insns.  */
2276
2277 static void
2278 emit_v9_brxx_insn (enum rtx_code code, rtx op0, rtx label)
2279 {
2280   emit_jump_insn (gen_rtx_SET (VOIDmode,
2281                            pc_rtx,
2282                            gen_rtx_IF_THEN_ELSE (VOIDmode,
2283                                     gen_rtx_fmt_ee (code, GET_MODE (op0),
2284                                                     op0, const0_rtx),
2285                                     gen_rtx_LABEL_REF (VOIDmode, label),
2286                                     pc_rtx)));
2287 }
2288
2289 void
2290 emit_conditional_branch_insn (rtx operands[])
2291 {
2292   /* The quad-word fp compare library routines all return nonzero to indicate
2293      true, which is different from the equivalent libgcc routines, so we must
2294      handle them specially here.  */
2295   if (GET_MODE (operands[1]) == TFmode && ! TARGET_HARD_QUAD)
2296     {
2297       operands[0] = sparc_emit_float_lib_cmp (operands[1], operands[2],
2298                                               GET_CODE (operands[0]));
2299       operands[1] = XEXP (operands[0], 0);
2300       operands[2] = XEXP (operands[0], 1);
2301     }
2302
2303   if (TARGET_ARCH64 && operands[2] == const0_rtx
2304       && GET_CODE (operands[1]) == REG
2305       && GET_MODE (operands[1]) == DImode)
2306     {
2307       emit_v9_brxx_insn (GET_CODE (operands[0]), operands[1], operands[3]);
2308       return;
2309     }
2310
2311   operands[1] = gen_compare_reg (operands[0]);
2312   operands[2] = const0_rtx;
2313   operands[0] = gen_rtx_fmt_ee (GET_CODE (operands[0]), VOIDmode,
2314                                 operands[1], operands[2]);
2315   emit_jump_insn (gen_cbranchcc4 (operands[0], operands[1], operands[2],
2316                                   operands[3]));
2317 }
2318
2319
2320 /* Generate a DFmode part of a hard TFmode register.
2321    REG is the TFmode hard register, LOW is 1 for the
2322    low 64bit of the register and 0 otherwise.
2323  */
2324 rtx
2325 gen_df_reg (rtx reg, int low)
2326 {
2327   int regno = REGNO (reg);
2328
2329   if ((WORDS_BIG_ENDIAN == 0) ^ (low != 0))
2330     regno += (TARGET_ARCH64 && regno < 32) ? 1 : 2;
2331   return gen_rtx_REG (DFmode, regno);
2332 }
2333 \f
2334 /* Generate a call to FUNC with OPERANDS.  Operand 0 is the return value.
2335    Unlike normal calls, TFmode operands are passed by reference.  It is
2336    assumed that no more than 3 operands are required.  */
2337
2338 static void
2339 emit_soft_tfmode_libcall (const char *func_name, int nargs, rtx *operands)
2340 {
2341   rtx ret_slot = NULL, arg[3], func_sym;
2342   int i;
2343
2344   /* We only expect to be called for conversions, unary, and binary ops.  */
2345   gcc_assert (nargs == 2 || nargs == 3);
2346
2347   for (i = 0; i < nargs; ++i)
2348     {
2349       rtx this_arg = operands[i];
2350       rtx this_slot;
2351
2352       /* TFmode arguments and return values are passed by reference.  */
2353       if (GET_MODE (this_arg) == TFmode)
2354         {
2355           int force_stack_temp;
2356
2357           force_stack_temp = 0;
2358           if (TARGET_BUGGY_QP_LIB && i == 0)
2359             force_stack_temp = 1;
2360
2361           if (GET_CODE (this_arg) == MEM
2362               && ! force_stack_temp)
2363             this_arg = XEXP (this_arg, 0);
2364           else if (CONSTANT_P (this_arg)
2365                    && ! force_stack_temp)
2366             {
2367               this_slot = force_const_mem (TFmode, this_arg);
2368               this_arg = XEXP (this_slot, 0);
2369             }
2370           else
2371             {
2372               this_slot = assign_stack_temp (TFmode, GET_MODE_SIZE (TFmode), 0);
2373
2374               /* Operand 0 is the return value.  We'll copy it out later.  */
2375               if (i > 0)
2376                 emit_move_insn (this_slot, this_arg);
2377               else
2378                 ret_slot = this_slot;
2379
2380               this_arg = XEXP (this_slot, 0);
2381             }
2382         }
2383
2384       arg[i] = this_arg;
2385     }
2386
2387   func_sym = gen_rtx_SYMBOL_REF (Pmode, func_name);
2388
2389   if (GET_MODE (operands[0]) == TFmode)
2390     {
2391       if (nargs == 2)
2392         emit_library_call (func_sym, LCT_NORMAL, VOIDmode, 2,
2393                            arg[0], GET_MODE (arg[0]),
2394                            arg[1], GET_MODE (arg[1]));
2395       else
2396         emit_library_call (func_sym, LCT_NORMAL, VOIDmode, 3,
2397                            arg[0], GET_MODE (arg[0]),
2398                            arg[1], GET_MODE (arg[1]),
2399                            arg[2], GET_MODE (arg[2]));
2400
2401       if (ret_slot)
2402         emit_move_insn (operands[0], ret_slot);
2403     }
2404   else
2405     {
2406       rtx ret;
2407
2408       gcc_assert (nargs == 2);
2409
2410       ret = emit_library_call_value (func_sym, operands[0], LCT_NORMAL,
2411                                      GET_MODE (operands[0]), 1,
2412                                      arg[1], GET_MODE (arg[1]));
2413
2414       if (ret != operands[0])
2415         emit_move_insn (operands[0], ret);
2416     }
2417 }
2418
2419 /* Expand soft-float TFmode calls to sparc abi routines.  */
2420
2421 static void
2422 emit_soft_tfmode_binop (enum rtx_code code, rtx *operands)
2423 {
2424   const char *func;
2425
2426   switch (code)
2427     {
2428     case PLUS:
2429       func = "_Qp_add";
2430       break;
2431     case MINUS:
2432       func = "_Qp_sub";
2433       break;
2434     case MULT:
2435       func = "_Qp_mul";
2436       break;
2437     case DIV:
2438       func = "_Qp_div";
2439       break;
2440     default:
2441       gcc_unreachable ();
2442     }
2443
2444   emit_soft_tfmode_libcall (func, 3, operands);
2445 }
2446
2447 static void
2448 emit_soft_tfmode_unop (enum rtx_code code, rtx *operands)
2449 {
2450   const char *func;
2451
2452   gcc_assert (code == SQRT);
2453   func = "_Qp_sqrt";
2454
2455   emit_soft_tfmode_libcall (func, 2, operands);
2456 }
2457
2458 static void
2459 emit_soft_tfmode_cvt (enum rtx_code code, rtx *operands)
2460 {
2461   const char *func;
2462
2463   switch (code)
2464     {
2465     case FLOAT_EXTEND:
2466       switch (GET_MODE (operands[1]))
2467         {
2468         case SFmode:
2469           func = "_Qp_stoq";
2470           break;
2471         case DFmode:
2472           func = "_Qp_dtoq";
2473           break;
2474         default:
2475           gcc_unreachable ();
2476         }
2477       break;
2478
2479     case FLOAT_TRUNCATE:
2480       switch (GET_MODE (operands[0]))
2481         {
2482         case SFmode:
2483           func = "_Qp_qtos";
2484           break;
2485         case DFmode:
2486           func = "_Qp_qtod";
2487           break;
2488         default:
2489           gcc_unreachable ();
2490         }
2491       break;
2492
2493     case FLOAT:
2494       switch (GET_MODE (operands[1]))
2495         {
2496         case SImode:
2497           func = "_Qp_itoq";
2498           if (TARGET_ARCH64)
2499             operands[1] = gen_rtx_SIGN_EXTEND (DImode, operands[1]);
2500           break;
2501         case DImode:
2502           func = "_Qp_xtoq";
2503           break;
2504         default:
2505           gcc_unreachable ();
2506         }
2507       break;
2508
2509     case UNSIGNED_FLOAT:
2510       switch (GET_MODE (operands[1]))
2511         {
2512         case SImode:
2513           func = "_Qp_uitoq";
2514           if (TARGET_ARCH64)
2515             operands[1] = gen_rtx_ZERO_EXTEND (DImode, operands[1]);
2516           break;
2517         case DImode:
2518           func = "_Qp_uxtoq";
2519           break;
2520         default:
2521           gcc_unreachable ();
2522         }
2523       break;
2524
2525     case FIX:
2526       switch (GET_MODE (operands[0]))
2527         {
2528         case SImode:
2529           func = "_Qp_qtoi";
2530           break;
2531         case DImode:
2532           func = "_Qp_qtox";
2533           break;
2534         default:
2535           gcc_unreachable ();
2536         }
2537       break;
2538
2539     case UNSIGNED_FIX:
2540       switch (GET_MODE (operands[0]))
2541         {
2542         case SImode:
2543           func = "_Qp_qtoui";
2544           break;
2545         case DImode:
2546           func = "_Qp_qtoux";
2547           break;
2548         default:
2549           gcc_unreachable ();
2550         }
2551       break;
2552
2553     default:
2554       gcc_unreachable ();
2555     }
2556
2557   emit_soft_tfmode_libcall (func, 2, operands);
2558 }
2559
2560 /* Expand a hard-float tfmode operation.  All arguments must be in
2561    registers.  */
2562
2563 static void
2564 emit_hard_tfmode_operation (enum rtx_code code, rtx *operands)
2565 {
2566   rtx op, dest;
2567
2568   if (GET_RTX_CLASS (code) == RTX_UNARY)
2569     {
2570       operands[1] = force_reg (GET_MODE (operands[1]), operands[1]);
2571       op = gen_rtx_fmt_e (code, GET_MODE (operands[0]), operands[1]);
2572     }
2573   else
2574     {
2575       operands[1] = force_reg (GET_MODE (operands[1]), operands[1]);
2576       operands[2] = force_reg (GET_MODE (operands[2]), operands[2]);
2577       op = gen_rtx_fmt_ee (code, GET_MODE (operands[0]),
2578                            operands[1], operands[2]);
2579     }
2580
2581   if (register_operand (operands[0], VOIDmode))
2582     dest = operands[0];
2583   else
2584     dest = gen_reg_rtx (GET_MODE (operands[0]));
2585
2586   emit_insn (gen_rtx_SET (VOIDmode, dest, op));
2587
2588   if (dest != operands[0])
2589     emit_move_insn (operands[0], dest);
2590 }
2591
2592 void
2593 emit_tfmode_binop (enum rtx_code code, rtx *operands)
2594 {
2595   if (TARGET_HARD_QUAD)
2596     emit_hard_tfmode_operation (code, operands);
2597   else
2598     emit_soft_tfmode_binop (code, operands);
2599 }
2600
2601 void
2602 emit_tfmode_unop (enum rtx_code code, rtx *operands)
2603 {
2604   if (TARGET_HARD_QUAD)
2605     emit_hard_tfmode_operation (code, operands);
2606   else
2607     emit_soft_tfmode_unop (code, operands);
2608 }
2609
2610 void
2611 emit_tfmode_cvt (enum rtx_code code, rtx *operands)
2612 {
2613   if (TARGET_HARD_QUAD)
2614     emit_hard_tfmode_operation (code, operands);
2615   else
2616     emit_soft_tfmode_cvt (code, operands);
2617 }
2618 \f
2619 /* Return nonzero if a branch/jump/call instruction will be emitting
2620    nop into its delay slot.  */
2621
2622 int
2623 empty_delay_slot (rtx insn)
2624 {
2625   rtx seq;
2626
2627   /* If no previous instruction (should not happen), return true.  */
2628   if (PREV_INSN (insn) == NULL)
2629     return 1;
2630
2631   seq = NEXT_INSN (PREV_INSN (insn));
2632   if (GET_CODE (PATTERN (seq)) == SEQUENCE)
2633     return 0;
2634
2635   return 1;
2636 }
2637
2638 /* Return nonzero if TRIAL can go into the call delay slot.  */
2639
2640 int
2641 tls_call_delay (rtx trial)
2642 {
2643   rtx pat;
2644
2645   /* Binutils allows
2646        call __tls_get_addr, %tgd_call (foo)
2647         add %l7, %o0, %o0, %tgd_add (foo)
2648      while Sun as/ld does not.  */
2649   if (TARGET_GNU_TLS || !TARGET_TLS)
2650     return 1;
2651
2652   pat = PATTERN (trial);
2653
2654   /* We must reject tgd_add{32|64}, i.e.
2655        (set (reg) (plus (reg) (unspec [(reg) (symbol_ref)] UNSPEC_TLSGD)))
2656      and tldm_add{32|64}, i.e.
2657        (set (reg) (plus (reg) (unspec [(reg) (symbol_ref)] UNSPEC_TLSLDM)))
2658      for Sun as/ld.  */
2659   if (GET_CODE (pat) == SET
2660       && GET_CODE (SET_SRC (pat)) == PLUS)
2661     {
2662       rtx unspec = XEXP (SET_SRC (pat), 1);
2663
2664       if (GET_CODE (unspec) == UNSPEC
2665           && (XINT (unspec, 1) == UNSPEC_TLSGD
2666               || XINT (unspec, 1) == UNSPEC_TLSLDM))
2667         return 0;
2668     }
2669
2670   return 1;
2671 }
2672
2673 /* Return nonzero if TRIAL, an insn, can be combined with a 'restore'
2674    instruction.  RETURN_P is true if the v9 variant 'return' is to be
2675    considered in the test too.
2676
2677    TRIAL must be a SET whose destination is a REG appropriate for the
2678    'restore' instruction or, if RETURN_P is true, for the 'return'
2679    instruction.  */
2680
2681 static int
2682 eligible_for_restore_insn (rtx trial, bool return_p)
2683 {
2684   rtx pat = PATTERN (trial);
2685   rtx src = SET_SRC (pat);
2686
2687   /* The 'restore src,%g0,dest' pattern for word mode and below.  */
2688   if (GET_MODE_CLASS (GET_MODE (src)) != MODE_FLOAT
2689       && arith_operand (src, GET_MODE (src)))
2690     {
2691       if (TARGET_ARCH64)
2692         return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
2693       else
2694         return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (SImode);
2695     }
2696
2697   /* The 'restore src,%g0,dest' pattern for double-word mode.  */
2698   else if (GET_MODE_CLASS (GET_MODE (src)) != MODE_FLOAT
2699            && arith_double_operand (src, GET_MODE (src)))
2700     return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
2701
2702   /* The 'restore src,%g0,dest' pattern for float if no FPU.  */
2703   else if (! TARGET_FPU && register_operand (src, SFmode))
2704     return 1;
2705
2706   /* The 'restore src,%g0,dest' pattern for double if no FPU.  */
2707   else if (! TARGET_FPU && TARGET_ARCH64 && register_operand (src, DFmode))
2708     return 1;
2709
2710   /* If we have the 'return' instruction, anything that does not use
2711      local or output registers and can go into a delay slot wins.  */
2712   else if (return_p && TARGET_V9 && ! epilogue_renumber (&pat, 1)
2713            && (get_attr_in_uncond_branch_delay (trial)
2714                == IN_UNCOND_BRANCH_DELAY_TRUE))
2715     return 1;
2716
2717   /* The 'restore src1,src2,dest' pattern for SImode.  */
2718   else if (GET_CODE (src) == PLUS
2719            && register_operand (XEXP (src, 0), SImode)
2720            && arith_operand (XEXP (src, 1), SImode))
2721     return 1;
2722
2723   /* The 'restore src1,src2,dest' pattern for DImode.  */
2724   else if (GET_CODE (src) == PLUS
2725            && register_operand (XEXP (src, 0), DImode)
2726            && arith_double_operand (XEXP (src, 1), DImode))
2727     return 1;
2728
2729   /* The 'restore src1,%lo(src2),dest' pattern.  */
2730   else if (GET_CODE (src) == LO_SUM
2731            && ! TARGET_CM_MEDMID
2732            && ((register_operand (XEXP (src, 0), SImode)
2733                 && immediate_operand (XEXP (src, 1), SImode))
2734                || (TARGET_ARCH64
2735                    && register_operand (XEXP (src, 0), DImode)
2736                    && immediate_operand (XEXP (src, 1), DImode))))
2737     return 1;
2738
2739   /* The 'restore src,src,dest' pattern.  */
2740   else if (GET_CODE (src) == ASHIFT
2741            && (register_operand (XEXP (src, 0), SImode)
2742                || register_operand (XEXP (src, 0), DImode))
2743            && XEXP (src, 1) == const1_rtx)
2744     return 1;
2745
2746   return 0;
2747 }
2748
2749 /* Return nonzero if TRIAL can go into the function return's
2750    delay slot.  */
2751
2752 int
2753 eligible_for_return_delay (rtx trial)
2754 {
2755   rtx pat;
2756
2757   if (GET_CODE (trial) != INSN || GET_CODE (PATTERN (trial)) != SET)
2758     return 0;
2759
2760   if (get_attr_length (trial) != 1)
2761     return 0;
2762
2763   /* If there are any call-saved registers, we should scan TRIAL if it
2764      does not reference them.  For now just make it easy.  */
2765   if (num_gfregs)
2766     return 0;
2767
2768   /* If the function uses __builtin_eh_return, the eh_return machinery
2769      occupies the delay slot.  */
2770   if (crtl->calls_eh_return)
2771     return 0;
2772
2773   /* In the case of a true leaf function, anything can go into the slot.  */
2774   if (sparc_leaf_function_p)
2775     return get_attr_in_uncond_branch_delay (trial)
2776            == IN_UNCOND_BRANCH_DELAY_TRUE;
2777
2778   pat = PATTERN (trial);
2779
2780   /* Otherwise, only operations which can be done in tandem with
2781      a `restore' or `return' insn can go into the delay slot.  */
2782   if (GET_CODE (SET_DEST (pat)) != REG
2783       || (REGNO (SET_DEST (pat)) >= 8 && REGNO (SET_DEST (pat)) < 24))
2784     return 0;
2785
2786   /* If this instruction sets up floating point register and we have a return
2787      instruction, it can probably go in.  But restore will not work
2788      with FP_REGS.  */
2789   if (REGNO (SET_DEST (pat)) >= 32)
2790     return (TARGET_V9
2791             && ! epilogue_renumber (&pat, 1)
2792             && (get_attr_in_uncond_branch_delay (trial)
2793                 == IN_UNCOND_BRANCH_DELAY_TRUE));
2794
2795   return eligible_for_restore_insn (trial, true);
2796 }
2797
2798 /* Return nonzero if TRIAL can go into the sibling call's
2799    delay slot.  */
2800
2801 int
2802 eligible_for_sibcall_delay (rtx trial)
2803 {
2804   rtx pat;
2805
2806   if (GET_CODE (trial) != INSN || GET_CODE (PATTERN (trial)) != SET)
2807     return 0;
2808
2809   if (get_attr_length (trial) != 1)
2810     return 0;
2811
2812   pat = PATTERN (trial);
2813
2814   if (sparc_leaf_function_p)
2815     {
2816       /* If the tail call is done using the call instruction,
2817          we have to restore %o7 in the delay slot.  */
2818       if (LEAF_SIBCALL_SLOT_RESERVED_P)
2819         return 0;
2820
2821       /* %g1 is used to build the function address */
2822       if (reg_mentioned_p (gen_rtx_REG (Pmode, 1), pat))
2823         return 0;
2824
2825       return 1;
2826     }
2827
2828   /* Otherwise, only operations which can be done in tandem with
2829      a `restore' insn can go into the delay slot.  */
2830   if (GET_CODE (SET_DEST (pat)) != REG
2831       || (REGNO (SET_DEST (pat)) >= 8 && REGNO (SET_DEST (pat)) < 24)
2832       || REGNO (SET_DEST (pat)) >= 32)
2833     return 0;
2834
2835   /* If it mentions %o7, it can't go in, because sibcall will clobber it
2836      in most cases.  */
2837   if (reg_mentioned_p (gen_rtx_REG (Pmode, 15), pat))
2838     return 0;
2839
2840   return eligible_for_restore_insn (trial, false);
2841 }
2842
2843 int
2844 short_branch (int uid1, int uid2)
2845 {
2846   int delta = INSN_ADDRESSES (uid1) - INSN_ADDRESSES (uid2);
2847
2848   /* Leave a few words of "slop".  */
2849   if (delta >= -1023 && delta <= 1022)
2850     return 1;
2851
2852   return 0;
2853 }
2854
2855 /* Return nonzero if REG is not used after INSN.
2856    We assume REG is a reload reg, and therefore does
2857    not live past labels or calls or jumps.  */
2858 int
2859 reg_unused_after (rtx reg, rtx insn)
2860 {
2861   enum rtx_code code, prev_code = UNKNOWN;
2862
2863   while ((insn = NEXT_INSN (insn)))
2864     {
2865       if (prev_code == CALL_INSN && call_used_regs[REGNO (reg)])
2866         return 1;
2867
2868       code = GET_CODE (insn);
2869       if (GET_CODE (insn) == CODE_LABEL)
2870         return 1;
2871
2872       if (INSN_P (insn))
2873         {
2874           rtx set = single_set (insn);
2875           int in_src = set && reg_overlap_mentioned_p (reg, SET_SRC (set));
2876           if (set && in_src)
2877             return 0;
2878           if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2879             return 1;
2880           if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
2881             return 0;
2882         }
2883       prev_code = code;
2884     }
2885   return 1;
2886 }
2887 \f
2888 /* Determine if it's legal to put X into the constant pool.  This
2889    is not possible if X contains the address of a symbol that is
2890    not constant (TLS) or not known at final link time (PIC).  */
2891
2892 static bool
2893 sparc_cannot_force_const_mem (rtx x)
2894 {
2895   switch (GET_CODE (x))
2896     {
2897     case CONST_INT:
2898     case CONST_DOUBLE:
2899     case CONST_VECTOR:
2900       /* Accept all non-symbolic constants.  */
2901       return false;
2902
2903     case LABEL_REF:
2904       /* Labels are OK iff we are non-PIC.  */
2905       return flag_pic != 0;
2906
2907     case SYMBOL_REF:
2908       /* 'Naked' TLS symbol references are never OK,
2909          non-TLS symbols are OK iff we are non-PIC.  */
2910       if (SYMBOL_REF_TLS_MODEL (x))
2911         return true;
2912       else
2913         return flag_pic != 0;
2914
2915     case CONST:
2916       return sparc_cannot_force_const_mem (XEXP (x, 0));
2917     case PLUS:
2918     case MINUS:
2919       return sparc_cannot_force_const_mem (XEXP (x, 0))
2920          || sparc_cannot_force_const_mem (XEXP (x, 1));
2921     case UNSPEC:
2922       return true;
2923     default:
2924       gcc_unreachable ();
2925     }
2926 }
2927 \f
2928 /* PIC support.  */
2929 static GTY(()) bool pic_helper_needed = false;
2930 static GTY(()) rtx pic_helper_symbol;
2931 static GTY(()) rtx global_offset_table;
2932
2933 /* Ensure that we are not using patterns that are not OK with PIC.  */
2934
2935 int
2936 check_pic (int i)
2937 {
2938   switch (flag_pic)
2939     {
2940     case 1:
2941       gcc_assert (GET_CODE (recog_data.operand[i]) != SYMBOL_REF
2942                   && (GET_CODE (recog_data.operand[i]) != CONST
2943                   || (GET_CODE (XEXP (recog_data.operand[i], 0)) == MINUS
2944                       && (XEXP (XEXP (recog_data.operand[i], 0), 0)
2945                           == global_offset_table)
2946                       && (GET_CODE (XEXP (XEXP (recog_data.operand[i], 0), 1))
2947                           == CONST))));
2948     case 2:
2949     default:
2950       return 1;
2951     }
2952 }
2953
2954 /* Return true if X is an address which needs a temporary register when
2955    reloaded while generating PIC code.  */
2956
2957 int
2958 pic_address_needs_scratch (rtx x)
2959 {
2960   /* An address which is a symbolic plus a non SMALL_INT needs a temp reg.  */
2961   if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
2962       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
2963       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2964       && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
2965     return 1;
2966
2967   return 0;
2968 }
2969
2970 /* Determine if a given RTX is a valid constant.  We already know this
2971    satisfies CONSTANT_P.  */
2972
2973 bool
2974 legitimate_constant_p (rtx x)
2975 {
2976   switch (GET_CODE (x))
2977     {
2978     case CONST:
2979     case SYMBOL_REF:
2980       if (sparc_tls_referenced_p (x))
2981         return false;
2982       break;
2983
2984     case CONST_DOUBLE:
2985       if (GET_MODE (x) == VOIDmode)
2986         return true;
2987
2988       /* Floating point constants are generally not ok.
2989          The only exception is 0.0 in VIS.  */
2990       if (TARGET_VIS
2991           && SCALAR_FLOAT_MODE_P (GET_MODE (x))
2992           && const_zero_operand (x, GET_MODE (x)))
2993         return true;
2994
2995       return false;
2996
2997     case CONST_VECTOR:
2998       /* Vector constants are generally not ok.
2999          The only exception is 0 in VIS.  */
3000       if (TARGET_VIS
3001           && const_zero_operand (x, GET_MODE (x)))
3002         return true;
3003
3004       return false;
3005
3006     default:
3007       break;
3008     }
3009
3010   return true;
3011 }
3012
3013 /* Determine if a given RTX is a valid constant address.  */
3014
3015 bool
3016 constant_address_p (rtx x)
3017 {
3018   switch (GET_CODE (x))
3019     {
3020     case LABEL_REF:
3021     case CONST_INT:
3022     case HIGH:
3023       return true;
3024
3025     case CONST:
3026       if (flag_pic && pic_address_needs_scratch (x))
3027         return false;
3028       return legitimate_constant_p (x);
3029
3030     case SYMBOL_REF:
3031       return !flag_pic && legitimate_constant_p (x);
3032
3033     default:
3034       return false;
3035     }
3036 }
3037
3038 /* Nonzero if the constant value X is a legitimate general operand
3039    when generating PIC code.  It is given that flag_pic is on and
3040    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
3041
3042 bool
3043 legitimate_pic_operand_p (rtx x)
3044 {
3045   if (pic_address_needs_scratch (x))
3046     return false;
3047   if (sparc_tls_referenced_p (x))
3048     return false;
3049   return true;
3050 }
3051
3052 /* Return nonzero if ADDR is a valid memory address.
3053    STRICT specifies whether strict register checking applies.  */
3054
3055 static bool
3056 sparc_legitimate_address_p (enum machine_mode mode, rtx addr, bool strict)
3057 {
3058   rtx rs1 = NULL, rs2 = NULL, imm1 = NULL;
3059
3060   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
3061     rs1 = addr;
3062   else if (GET_CODE (addr) == PLUS)
3063     {
3064       rs1 = XEXP (addr, 0);
3065       rs2 = XEXP (addr, 1);
3066
3067       /* Canonicalize.  REG comes first, if there are no regs,
3068          LO_SUM comes first.  */
3069       if (!REG_P (rs1)
3070           && GET_CODE (rs1) != SUBREG
3071           && (REG_P (rs2)
3072               || GET_CODE (rs2) == SUBREG
3073               || (GET_CODE (rs2) == LO_SUM && GET_CODE (rs1) != LO_SUM)))
3074         {
3075           rs1 = XEXP (addr, 1);
3076           rs2 = XEXP (addr, 0);
3077         }
3078
3079       if ((flag_pic == 1
3080            && rs1 == pic_offset_table_rtx
3081            && !REG_P (rs2)
3082            && GET_CODE (rs2) != SUBREG
3083            && GET_CODE (rs2) != LO_SUM
3084            && GET_CODE (rs2) != MEM
3085            && !(GET_CODE (rs2) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs2))
3086            && (! symbolic_operand (rs2, VOIDmode) || mode == Pmode)
3087            && (GET_CODE (rs2) != CONST_INT || SMALL_INT (rs2)))
3088           || ((REG_P (rs1)
3089                || GET_CODE (rs1) == SUBREG)
3090               && RTX_OK_FOR_OFFSET_P (rs2)))
3091         {
3092           imm1 = rs2;
3093           rs2 = NULL;
3094         }
3095       else if ((REG_P (rs1) || GET_CODE (rs1) == SUBREG)
3096                && (REG_P (rs2) || GET_CODE (rs2) == SUBREG))
3097         {
3098           /* We prohibit REG + REG for TFmode when there are no quad move insns
3099              and we consequently need to split.  We do this because REG+REG
3100              is not an offsettable address.  If we get the situation in reload
3101              where source and destination of a movtf pattern are both MEMs with
3102              REG+REG address, then only one of them gets converted to an
3103              offsettable address.  */
3104           if (mode == TFmode
3105               && ! (TARGET_FPU && TARGET_ARCH64 && TARGET_HARD_QUAD))
3106             return 0;
3107
3108           /* We prohibit REG + REG on ARCH32 if not optimizing for
3109              DFmode/DImode because then mem_min_alignment is likely to be zero
3110              after reload and the  forced split would lack a matching splitter
3111              pattern.  */
3112           if (TARGET_ARCH32 && !optimize
3113               && (mode == DFmode || mode == DImode))
3114             return 0;
3115         }
3116       else if (USE_AS_OFFSETABLE_LO10
3117                && GET_CODE (rs1) == LO_SUM
3118                && TARGET_ARCH64
3119                && ! TARGET_CM_MEDMID
3120                && RTX_OK_FOR_OLO10_P (rs2))
3121         {
3122           rs2 = NULL;
3123           imm1 = XEXP (rs1, 1);
3124           rs1 = XEXP (rs1, 0);
3125           if (!CONSTANT_P (imm1)
3126               || (GET_CODE (rs1) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs1)))
3127             return 0;
3128         }
3129     }
3130   else if (GET_CODE (addr) == LO_SUM)
3131     {
3132       rs1 = XEXP (addr, 0);
3133       imm1 = XEXP (addr, 1);
3134
3135       if (!CONSTANT_P (imm1)
3136           || (GET_CODE (rs1) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs1)))
3137         return 0;
3138
3139       /* We can't allow TFmode in 32-bit mode, because an offset greater
3140          than the alignment (8) may cause the LO_SUM to overflow.  */
3141       if (mode == TFmode && TARGET_ARCH32)
3142         return 0;
3143     }
3144   else if (GET_CODE (addr) == CONST_INT && SMALL_INT (addr))
3145     return 1;
3146   else
3147     return 0;
3148
3149   if (GET_CODE (rs1) == SUBREG)
3150     rs1 = SUBREG_REG (rs1);
3151   if (!REG_P (rs1))
3152     return 0;
3153
3154   if (rs2)
3155     {
3156       if (GET_CODE (rs2) == SUBREG)
3157         rs2 = SUBREG_REG (rs2);
3158       if (!REG_P (rs2))
3159         return 0;
3160     }
3161
3162   if (strict)
3163     {
3164       if (!REGNO_OK_FOR_BASE_P (REGNO (rs1))
3165           || (rs2 && !REGNO_OK_FOR_BASE_P (REGNO (rs2))))
3166         return 0;
3167     }
3168   else
3169     {
3170       if ((REGNO (rs1) >= 32
3171            && REGNO (rs1) != FRAME_POINTER_REGNUM
3172            && REGNO (rs1) < FIRST_PSEUDO_REGISTER)
3173           || (rs2
3174               && (REGNO (rs2) >= 32
3175                   && REGNO (rs2) != FRAME_POINTER_REGNUM
3176                   && REGNO (rs2) < FIRST_PSEUDO_REGISTER)))
3177         return 0;
3178     }
3179   return 1;
3180 }
3181
3182 /* Construct the SYMBOL_REF for the tls_get_offset function.  */
3183
3184 static GTY(()) rtx sparc_tls_symbol;
3185
3186 static rtx
3187 sparc_tls_get_addr (void)
3188 {
3189   if (!sparc_tls_symbol)
3190     sparc_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_addr");
3191
3192   return sparc_tls_symbol;
3193 }
3194
3195 static rtx
3196 sparc_tls_got (void)
3197 {
3198   rtx temp;
3199   if (flag_pic)
3200     {
3201       crtl->uses_pic_offset_table = 1;
3202       return pic_offset_table_rtx;
3203     }
3204
3205   if (!global_offset_table)
3206     global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3207   temp = gen_reg_rtx (Pmode);
3208   emit_move_insn (temp, global_offset_table);
3209   return temp;
3210 }
3211
3212 /* Return true if X contains a thread-local symbol.  */
3213
3214 static bool
3215 sparc_tls_referenced_p (rtx x)
3216 {
3217   if (!TARGET_HAVE_TLS)
3218     return false;
3219
3220   if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS)
3221     x = XEXP (XEXP (x, 0), 0);
3222
3223   if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x))
3224     return true;
3225
3226   /* That's all we handle in sparc_legitimize_tls_address for now.  */
3227   return false;
3228 }
3229
3230 /* ADDR contains a thread-local SYMBOL_REF.  Generate code to compute
3231    this (thread-local) address.  */
3232
3233 static rtx
3234 sparc_legitimize_tls_address (rtx addr)
3235 {
3236   rtx temp1, temp2, temp3, ret, o0, got, insn;
3237
3238   gcc_assert (can_create_pseudo_p ());
3239
3240   if (GET_CODE (addr) == SYMBOL_REF)
3241     switch (SYMBOL_REF_TLS_MODEL (addr))
3242       {
3243       case TLS_MODEL_GLOBAL_DYNAMIC:
3244         start_sequence ();
3245         temp1 = gen_reg_rtx (SImode);
3246         temp2 = gen_reg_rtx (SImode);
3247         ret = gen_reg_rtx (Pmode);
3248         o0 = gen_rtx_REG (Pmode, 8);
3249         got = sparc_tls_got ();
3250         emit_insn (gen_tgd_hi22 (temp1, addr));
3251         emit_insn (gen_tgd_lo10 (temp2, temp1, addr));
3252         if (TARGET_ARCH32)
3253           {
3254             emit_insn (gen_tgd_add32 (o0, got, temp2, addr));
3255             insn = emit_call_insn (gen_tgd_call32 (o0, sparc_tls_get_addr (),
3256                                                    addr, const1_rtx));
3257           }
3258         else
3259           {
3260             emit_insn (gen_tgd_add64 (o0, got, temp2, addr));
3261             insn = emit_call_insn (gen_tgd_call64 (o0, sparc_tls_get_addr (),
3262                                                    addr, const1_rtx));
3263           }
3264         CALL_INSN_FUNCTION_USAGE (insn)
3265           = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_USE (VOIDmode, o0),
3266                                CALL_INSN_FUNCTION_USAGE (insn));
3267         insn = get_insns ();
3268         end_sequence ();
3269         emit_libcall_block (insn, ret, o0, addr);
3270         break;
3271
3272       case TLS_MODEL_LOCAL_DYNAMIC:
3273         start_sequence ();
3274         temp1 = gen_reg_rtx (SImode);
3275         temp2 = gen_reg_rtx (SImode);
3276         temp3 = gen_reg_rtx (Pmode);
3277         ret = gen_reg_rtx (Pmode);
3278         o0 = gen_rtx_REG (Pmode, 8);
3279         got = sparc_tls_got ();
3280         emit_insn (gen_tldm_hi22 (temp1));
3281         emit_insn (gen_tldm_lo10 (temp2, temp1));
3282         if (TARGET_ARCH32)
3283           {
3284             emit_insn (gen_tldm_add32 (o0, got, temp2));
3285             insn = emit_call_insn (gen_tldm_call32 (o0, sparc_tls_get_addr (),
3286                                                     const1_rtx));
3287           }
3288         else
3289           {
3290             emit_insn (gen_tldm_add64 (o0, got, temp2));
3291             insn = emit_call_insn (gen_tldm_call64 (o0, sparc_tls_get_addr (),
3292                                                     const1_rtx));
3293           }
3294         CALL_INSN_FUNCTION_USAGE (insn)
3295           = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_USE (VOIDmode, o0),
3296                                CALL_INSN_FUNCTION_USAGE (insn));
3297         insn = get_insns ();
3298         end_sequence ();
3299         emit_libcall_block (insn, temp3, o0,
3300                             gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3301                                             UNSPEC_TLSLD_BASE));
3302         temp1 = gen_reg_rtx (SImode);
3303         temp2 = gen_reg_rtx (SImode);
3304         emit_insn (gen_tldo_hix22 (temp1, addr));
3305         emit_insn (gen_tldo_lox10 (temp2, temp1, addr));
3306         if (TARGET_ARCH32)
3307           emit_insn (gen_tldo_add32 (ret, temp3, temp2, addr));
3308         else
3309           emit_insn (gen_tldo_add64 (ret, temp3, temp2, addr));
3310         break;
3311
3312       case TLS_MODEL_INITIAL_EXEC:
3313         temp1 = gen_reg_rtx (SImode);
3314         temp2 = gen_reg_rtx (SImode);
3315         temp3 = gen_reg_rtx (Pmode);
3316         got = sparc_tls_got ();
3317         emit_insn (gen_tie_hi22 (temp1, addr));
3318         emit_insn (gen_tie_lo10 (temp2, temp1, addr));
3319         if (TARGET_ARCH32)
3320           emit_insn (gen_tie_ld32 (temp3, got, temp2, addr));
3321         else
3322           emit_insn (gen_tie_ld64 (temp3, got, temp2, addr));
3323         if (TARGET_SUN_TLS)
3324           {
3325             ret = gen_reg_rtx (Pmode);
3326             if (TARGET_ARCH32)
3327               emit_insn (gen_tie_add32 (ret, gen_rtx_REG (Pmode, 7),
3328                                         temp3, addr));
3329             else
3330               emit_insn (gen_tie_add64 (ret, gen_rtx_REG (Pmode, 7),
3331                                         temp3, addr));
3332           }
3333         else
3334           ret = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, 7), temp3);
3335         break;
3336
3337       case TLS_MODEL_LOCAL_EXEC:
3338         temp1 = gen_reg_rtx (Pmode);
3339         temp2 = gen_reg_rtx (Pmode);
3340         if (TARGET_ARCH32)
3341           {
3342             emit_insn (gen_tle_hix22_sp32 (temp1, addr));
3343             emit_insn (gen_tle_lox10_sp32 (temp2, temp1, addr));
3344           }
3345         else
3346           {
3347             emit_insn (gen_tle_hix22_sp64 (temp1, addr));
3348             emit_insn (gen_tle_lox10_sp64 (temp2, temp1, addr));
3349           }
3350         ret = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, 7), temp2);
3351         break;
3352
3353       default:
3354         gcc_unreachable ();
3355       }
3356
3357   else if (GET_CODE (addr) == CONST)
3358     {
3359       rtx base, offset;
3360
3361       gcc_assert (GET_CODE (XEXP (addr, 0)) == PLUS);
3362
3363       base = sparc_legitimize_tls_address (XEXP (XEXP (addr, 0), 0));
3364       offset = XEXP (XEXP (addr, 0), 1);
3365
3366       base = force_operand (base, NULL_RTX);
3367       if (!(GET_CODE (offset) == CONST_INT && SMALL_INT (offset)))
3368         offset = force_reg (Pmode, offset);
3369       ret = gen_rtx_PLUS (Pmode, base, offset);
3370     }
3371
3372   else
3373     gcc_unreachable ();  /* for now ... */
3374
3375   return ret;
3376 }
3377
3378 /* Legitimize PIC addresses.  If the address is already position-independent,
3379    we return ORIG.  Newly generated position-independent addresses go into a
3380    reg.  This is REG if nonzero, otherwise we allocate register(s) as
3381    necessary.  */
3382
3383 static rtx
3384 sparc_legitimize_pic_address (rtx orig, rtx reg)
3385 {
3386   bool gotdata_op = false;
3387
3388   if (GET_CODE (orig) == SYMBOL_REF
3389       /* See the comment in sparc_expand_move.  */
3390       || (TARGET_VXWORKS_RTP && GET_CODE (orig) == LABEL_REF))
3391     {
3392       rtx pic_ref, address;
3393       rtx insn;
3394
3395       if (reg == 0)
3396         {
3397           gcc_assert (! reload_in_progress && ! reload_completed);
3398           reg = gen_reg_rtx (Pmode);
3399         }
3400
3401       if (flag_pic == 2)
3402         {
3403           /* If not during reload, allocate another temp reg here for loading
3404              in the address, so that these instructions can be optimized
3405              properly.  */
3406           rtx temp_reg = ((reload_in_progress || reload_completed)
3407                           ? reg : gen_reg_rtx (Pmode));
3408
3409           /* Must put the SYMBOL_REF inside an UNSPEC here so that cse
3410              won't get confused into thinking that these two instructions
3411              are loading in the true address of the symbol.  If in the
3412              future a PIC rtx exists, that should be used instead.  */
3413           if (TARGET_ARCH64)
3414             {
3415               emit_insn (gen_movdi_high_pic (temp_reg, orig));
3416               emit_insn (gen_movdi_lo_sum_pic (temp_reg, temp_reg, orig));
3417             }
3418           else
3419             {
3420               emit_insn (gen_movsi_high_pic (temp_reg, orig));
3421               emit_insn (gen_movsi_lo_sum_pic (temp_reg, temp_reg, orig));
3422             }
3423           address = temp_reg;
3424           gotdata_op = true;
3425         }
3426       else
3427         address = orig;
3428
3429       crtl->uses_pic_offset_table = 1;
3430       if (gotdata_op)
3431         {
3432           if (TARGET_ARCH64)
3433             insn = emit_insn (gen_movdi_pic_gotdata_op (reg,
3434                                                         pic_offset_table_rtx,
3435                                                         address, orig));
3436           else
3437             insn = emit_insn (gen_movsi_pic_gotdata_op (reg,
3438                                                         pic_offset_table_rtx,
3439                                                         address, orig));
3440         }
3441       else
3442         {
3443           pic_ref = gen_const_mem (Pmode,
3444                                    gen_rtx_PLUS (Pmode,
3445                                                  pic_offset_table_rtx, address));
3446           insn = emit_move_insn (reg, pic_ref);
3447         }
3448       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3449          by loop.  */
3450       set_unique_reg_note (insn, REG_EQUAL, orig);
3451       return reg;
3452     }
3453   else if (GET_CODE (orig) == CONST)
3454     {
3455       rtx base, offset;
3456
3457       if (GET_CODE (XEXP (orig, 0)) == PLUS
3458           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3459         return orig;
3460
3461       if (reg == 0)
3462         {
3463           gcc_assert (! reload_in_progress && ! reload_completed);
3464           reg = gen_reg_rtx (Pmode);
3465         }
3466
3467       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3468       base = sparc_legitimize_pic_address (XEXP (XEXP (orig, 0), 0), reg);
3469       offset = sparc_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
3470                                              base == reg ? NULL_RTX : reg);
3471
3472       if (GET_CODE (offset) == CONST_INT)
3473         {
3474           if (SMALL_INT (offset))
3475             return plus_constant (base, INTVAL (offset));
3476           else if (! reload_in_progress && ! reload_completed)
3477             offset = force_reg (Pmode, offset);
3478           else
3479             /* If we reach here, then something is seriously wrong.  */
3480             gcc_unreachable ();
3481         }
3482       return gen_rtx_PLUS (Pmode, base, offset);
3483     }
3484   else if (GET_CODE (orig) == LABEL_REF)
3485     /* ??? Why do we do this?  */
3486     /* Now movsi_pic_label_ref uses it, but we ought to be checking that
3487        the register is live instead, in case it is eliminated.  */
3488     crtl->uses_pic_offset_table = 1;
3489
3490   return orig;
3491 }
3492
3493 /* Try machine-dependent ways of modifying an illegitimate address X
3494    to be legitimate.  If we find one, return the new, valid address.
3495
3496    OLDX is the address as it was before break_out_memory_refs was called.
3497    In some cases it is useful to look at this to decide what needs to be done.
3498
3499    MODE is the mode of the operand pointed to by X.
3500
3501    On SPARC, change REG+N into REG+REG, and REG+(X*Y) into REG+REG.  */
3502
3503 static rtx
3504 sparc_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
3505                           enum machine_mode mode)
3506 {
3507   rtx orig_x = x;
3508
3509   if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == MULT)
3510     x = gen_rtx_PLUS (Pmode, XEXP (x, 1),
3511                       force_operand (XEXP (x, 0), NULL_RTX));
3512   if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == MULT)
3513     x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
3514                       force_operand (XEXP (x, 1), NULL_RTX));
3515   if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS)
3516     x = gen_rtx_PLUS (Pmode, force_operand (XEXP (x, 0), NULL_RTX),
3517                       XEXP (x, 1));
3518   if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == PLUS)
3519     x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
3520                       force_operand (XEXP (x, 1), NULL_RTX));
3521
3522   if (x != orig_x && sparc_legitimate_address_p (mode, x, FALSE))
3523     return x;
3524
3525   if (sparc_tls_referenced_p (x))
3526     x = sparc_legitimize_tls_address (x);
3527   else if (flag_pic)
3528     x = sparc_legitimize_pic_address (x, NULL_RTX);
3529   else if (GET_CODE (x) == PLUS && CONSTANT_ADDRESS_P (XEXP (x, 1)))
3530     x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
3531                       copy_to_mode_reg (Pmode, XEXP (x, 1)));
3532   else if (GET_CODE (x) == PLUS && CONSTANT_ADDRESS_P (XEXP (x, 0)))
3533     x = gen_rtx_PLUS (Pmode, XEXP (x, 1),
3534                       copy_to_mode_reg (Pmode, XEXP (x, 0)));
3535   else if (GET_CODE (x) == SYMBOL_REF
3536            || GET_CODE (x) == CONST
3537            || GET_CODE (x) == LABEL_REF)
3538     x = copy_to_suggested_reg (x, NULL_RTX, Pmode);
3539
3540   return x;
3541 }
3542
3543 /* SPARC implementation of LEGITIMIZE_RELOAD_ADDRESS.  Returns a value to
3544    replace the input X, or the original X if no replacement is called for.
3545    The output parameter *WIN is 1 if the calling macro should goto WIN,
3546    0 if it should not.
3547
3548    For SPARC, we wish to handle addresses by splitting them into
3549    HIGH+LO_SUM pairs, retaining the LO_SUM in the memory reference.
3550    This cuts the number of extra insns by one.
3551
3552    Do nothing when generating PIC code and the address is a symbolic
3553    operand or requires a scratch register.  */
3554
3555 rtx
3556 sparc_legitimize_reload_address (rtx x, enum machine_mode mode,
3557                                  int opnum, int type,
3558                                  int ind_levels ATTRIBUTE_UNUSED, int *win)
3559 {
3560   /* Decompose SImode constants into HIGH+LO_SUM.  */
3561   if (CONSTANT_P (x)
3562       && (mode != TFmode || TARGET_ARCH64)
3563       && GET_MODE (x) == SImode
3564       && GET_CODE (x) != LO_SUM
3565       && GET_CODE (x) != HIGH
3566       && sparc_cmodel <= CM_MEDLOW
3567       && !(flag_pic
3568            && (symbolic_operand (x, Pmode) || pic_address_needs_scratch (x))))
3569     {
3570       x = gen_rtx_LO_SUM (GET_MODE (x), gen_rtx_HIGH (GET_MODE (x), x), x);
3571       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3572                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
3573                    opnum, (enum reload_type)type);
3574       *win = 1;
3575       return x;
3576     }
3577
3578   /* We have to recognize what we have already generated above.  */
3579   if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == HIGH)
3580     {
3581       push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
3582                    BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
3583                    opnum, (enum reload_type)type);
3584       *win = 1;
3585       return x;
3586     }
3587
3588   *win = 0;
3589   return x;
3590 }
3591
3592 /* Return true if ADDR (a legitimate address expression)
3593    has an effect that depends on the machine mode it is used for.
3594
3595    In PIC mode,
3596
3597       (mem:HI [%l7+a])
3598
3599    is not equivalent to
3600
3601       (mem:QI [%l7+a]) (mem:QI [%l7+a+1])
3602
3603    because [%l7+a+1] is interpreted as the address of (a+1).  */
3604
3605
3606 static bool
3607 sparc_mode_dependent_address_p (const_rtx addr)
3608 {
3609   if (flag_pic && GET_CODE (addr) == PLUS)
3610     {
3611       rtx op0 = XEXP (addr, 0);
3612       rtx op1 = XEXP (addr, 1);
3613       if (op0 == pic_offset_table_rtx
3614           && SYMBOLIC_CONST (op1))
3615         return true;
3616     }
3617
3618   return false;
3619 }
3620
3621 #ifdef HAVE_GAS_HIDDEN
3622 # define USE_HIDDEN_LINKONCE 1
3623 #else
3624 # define USE_HIDDEN_LINKONCE 0
3625 #endif
3626
3627 static void
3628 get_pc_thunk_name (char name[32], unsigned int regno)
3629 {
3630   const char *pic_name = reg_names[regno];
3631
3632   /* Skip the leading '%' as that cannot be used in a
3633      symbol name.  */
3634   pic_name += 1;
3635
3636   if (USE_HIDDEN_LINKONCE)
3637     sprintf (name, "__sparc_get_pc_thunk.%s", pic_name);
3638   else
3639     ASM_GENERATE_INTERNAL_LABEL (name, "LADDPC", regno);
3640 }
3641
3642 /* Emit code to load the PIC register.  */
3643
3644 static void
3645 load_pic_register (void)
3646 {
3647   int orig_flag_pic = flag_pic;
3648
3649   if (TARGET_VXWORKS_RTP)
3650     {
3651       emit_insn (gen_vxworks_load_got ());
3652       emit_use (pic_offset_table_rtx);
3653       return;
3654     }
3655
3656   /* If we haven't initialized the special PIC symbols, do so now.  */
3657   if (!pic_helper_needed)
3658     {
3659       char name[32];
3660
3661       pic_helper_needed = true;
3662
3663       get_pc_thunk_name (name, REGNO (pic_offset_table_rtx));
3664       pic_helper_symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
3665
3666       global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3667     }
3668
3669   flag_pic = 0;
3670   if (TARGET_ARCH64)
3671     emit_insn (gen_load_pcrel_symdi (pic_offset_table_rtx, global_offset_table,
3672                                      pic_helper_symbol));
3673   else
3674     emit_insn (gen_load_pcrel_symsi (pic_offset_table_rtx, global_offset_table,
3675                                      pic_helper_symbol));
3676   flag_pic = orig_flag_pic;
3677
3678   /* Need to emit this whether or not we obey regdecls,
3679      since setjmp/longjmp can cause life info to screw up.
3680      ??? In the case where we don't obey regdecls, this is not sufficient
3681      since we may not fall out the bottom.  */
3682   emit_use (pic_offset_table_rtx);
3683 }
3684
3685 /* Emit a call instruction with the pattern given by PAT.  ADDR is the
3686    address of the call target.  */
3687
3688 void
3689 sparc_emit_call_insn (rtx pat, rtx addr)
3690 {
3691   rtx insn;
3692
3693   insn = emit_call_insn (pat);
3694
3695   /* The PIC register is live on entry to VxWorks PIC PLT entries.  */
3696   if (TARGET_VXWORKS_RTP
3697       && flag_pic
3698       && GET_CODE (addr) == SYMBOL_REF
3699       && (SYMBOL_REF_DECL (addr)
3700           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
3701           : !SYMBOL_REF_LOCAL_P (addr)))
3702     {
3703       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
3704       crtl->uses_pic_offset_table = 1;
3705     }
3706 }
3707 \f
3708 /* Return 1 if RTX is a MEM which is known to be aligned to at
3709    least a DESIRED byte boundary.  */
3710
3711 int
3712 mem_min_alignment (rtx mem, int desired)
3713 {
3714   rtx addr, base, offset;
3715
3716   /* If it's not a MEM we can't accept it.  */
3717   if (GET_CODE (mem) != MEM)
3718     return 0;
3719
3720   /* Obviously...  */
3721   if (!TARGET_UNALIGNED_DOUBLES
3722       && MEM_ALIGN (mem) / BITS_PER_UNIT >= (unsigned)desired)
3723     return 1;
3724
3725   /* ??? The rest of the function predates MEM_ALIGN so
3726      there is probably a bit of redundancy.  */
3727   addr = XEXP (mem, 0);
3728   base = offset = NULL_RTX;
3729   if (GET_CODE (addr) == PLUS)
3730     {
3731       if (GET_CODE (XEXP (addr, 0)) == REG)
3732         {
3733           base = XEXP (addr, 0);
3734
3735           /* What we are saying here is that if the base
3736              REG is aligned properly, the compiler will make
3737              sure any REG based index upon it will be so
3738              as well.  */
3739           if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
3740             offset = XEXP (addr, 1);
3741           else
3742             offset = const0_rtx;
3743         }
3744     }
3745   else if (GET_CODE (addr) == REG)
3746     {
3747       base = addr;
3748       offset = const0_rtx;
3749     }
3750
3751   if (base != NULL_RTX)
3752     {
3753       int regno = REGNO (base);
3754
3755       if (regno != HARD_FRAME_POINTER_REGNUM && regno != STACK_POINTER_REGNUM)
3756         {
3757           /* Check if the compiler has recorded some information
3758              about the alignment of the base REG.  If reload has
3759              completed, we already matched with proper alignments.
3760              If not running global_alloc, reload might give us
3761              unaligned pointer to local stack though.  */
3762           if (((cfun != 0
3763                 && REGNO_POINTER_ALIGN (regno) >= desired * BITS_PER_UNIT)
3764                || (optimize && reload_completed))
3765               && (INTVAL (offset) & (desired - 1)) == 0)
3766             return 1;
3767         }
3768       else
3769         {
3770           if (((INTVAL (offset) - SPARC_STACK_BIAS) & (desired - 1)) == 0)
3771             return 1;
3772         }
3773     }
3774   else if (! TARGET_UNALIGNED_DOUBLES
3775            || CONSTANT_P (addr)
3776            || GET_CODE (addr) == LO_SUM)
3777     {
3778       /* Anything else we know is properly aligned unless TARGET_UNALIGNED_DOUBLES
3779          is true, in which case we can only assume that an access is aligned if
3780          it is to a constant address, or the address involves a LO_SUM.  */
3781       return 1;
3782     }
3783
3784   /* An obviously unaligned address.  */
3785   return 0;
3786 }
3787
3788 \f
3789 /* Vectors to keep interesting information about registers where it can easily
3790    be got.  We used to use the actual mode value as the bit number, but there
3791    are more than 32 modes now.  Instead we use two tables: one indexed by
3792    hard register number, and one indexed by mode.  */
3793
3794 /* The purpose of sparc_mode_class is to shrink the range of modes so that
3795    they all fit (as bit numbers) in a 32-bit word (again).  Each real mode is
3796    mapped into one sparc_mode_class mode.  */
3797
3798 enum sparc_mode_class {
3799   S_MODE, D_MODE, T_MODE, O_MODE,
3800   SF_MODE, DF_MODE, TF_MODE, OF_MODE,
3801   CC_MODE, CCFP_MODE
3802 };
3803
3804 /* Modes for single-word and smaller quantities.  */
3805 #define S_MODES ((1 << (int) S_MODE) | (1 << (int) SF_MODE))
3806
3807 /* Modes for double-word and smaller quantities.  */
3808 #define D_MODES (S_MODES | (1 << (int) D_MODE) | (1 << DF_MODE))
3809
3810 /* Modes for quad-word and smaller quantities.  */
3811 #define T_MODES (D_MODES | (1 << (int) T_MODE) | (1 << (int) TF_MODE))
3812
3813 /* Modes for 8-word and smaller quantities.  */
3814 #define O_MODES (T_MODES | (1 << (int) O_MODE) | (1 << (int) OF_MODE))
3815
3816 /* Modes for single-float quantities.  We must allow any single word or
3817    smaller quantity.  This is because the fix/float conversion instructions
3818    take integer inputs/outputs from the float registers.  */
3819 #define SF_MODES (S_MODES)
3820
3821 /* Modes for double-float and smaller quantities.  */
3822 #define DF_MODES (D_MODES)
3823
3824 /* Modes for quad-float and smaller quantities.  */
3825 #define TF_MODES (DF_MODES | (1 << (int) TF_MODE))
3826
3827 /* Modes for quad-float pairs and smaller quantities.  */
3828 #define OF_MODES (TF_MODES | (1 << (int) OF_MODE))
3829
3830 /* Modes for double-float only quantities.  */
3831 #define DF_MODES_NO_S ((1 << (int) D_MODE) | (1 << (int) DF_MODE))
3832
3833 /* Modes for quad-float and double-float only quantities.  */
3834 #define TF_MODES_NO_S (DF_MODES_NO_S | (1 << (int) TF_MODE))
3835
3836 /* Modes for quad-float pairs and double-float only quantities.  */
3837 #define OF_MODES_NO_S (TF_MODES_NO_S | (1 << (int) OF_MODE))
3838
3839 /* Modes for condition codes.  */
3840 #define CC_MODES (1 << (int) CC_MODE)
3841 #define CCFP_MODES (1 << (int) CCFP_MODE)
3842
3843 /* Value is 1 if register/mode pair is acceptable on sparc.
3844    The funny mixture of D and T modes is because integer operations
3845    do not specially operate on tetra quantities, so non-quad-aligned
3846    registers can hold quadword quantities (except %o4 and %i4 because
3847    they cross fixed registers).  */
3848
3849 /* This points to either the 32 bit or the 64 bit version.  */
3850 const int *hard_regno_mode_classes;
3851
3852 static const int hard_32bit_mode_classes[] = {
3853   S_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
3854   T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
3855   T_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
3856   T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
3857
3858   OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
3859   OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
3860   OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
3861   OF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
3862
3863   /* FP regs f32 to f63.  Only the even numbered registers actually exist,
3864      and none can hold SFmode/SImode values.  */
3865   OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
3866   OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
3867   OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
3868   OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, TF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
3869
3870   /* %fcc[0123] */
3871   CCFP_MODES, CCFP_MODES, CCFP_MODES, CCFP_MODES,
3872
3873   /* %icc */
3874   CC_MODES
3875 };
3876
3877 static const int hard_64bit_mode_classes[] = {
3878   D_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
3879   O_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
3880   T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
3881   O_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
3882
3883   OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
3884   OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
3885   OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
3886   OF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
3887
3888   /* FP regs f32 to f63.  Only the even numbered registers actually exist,
3889      and none can hold SFmode/SImode values.  */
3890   OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
3891   OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
3892   OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
3893   OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, TF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
3894
3895   /* %fcc[0123] */
3896   CCFP_MODES, CCFP_MODES, CCFP_MODES, CCFP_MODES,
3897
3898   /* %icc */
3899   CC_MODES
3900 };
3901
3902 int sparc_mode_class [NUM_MACHINE_MODES];
3903
3904 enum reg_class sparc_regno_reg_class[FIRST_PSEUDO_REGISTER];
3905
3906 static void
3907 sparc_init_modes (void)
3908 {
3909   int i;
3910
3911   for (i = 0; i < NUM_MACHINE_MODES; i++)
3912     {
3913       switch (GET_MODE_CLASS (i))
3914         {
3915         case MODE_INT:
3916         case MODE_PARTIAL_INT:
3917         case MODE_COMPLEX_INT:
3918           if (GET_MODE_SIZE (i) <= 4)
3919             sparc_mode_class[i] = 1 << (int) S_MODE;
3920           else if (GET_MODE_SIZE (i) == 8)
3921             sparc_mode_class[i] = 1 << (int) D_MODE;
3922           else if (GET_MODE_SIZE (i) == 16)
3923             sparc_mode_class[i] = 1 << (int) T_MODE;
3924           else if (GET_MODE_SIZE (i) == 32)
3925             sparc_mode_class[i] = 1 << (int) O_MODE;
3926           else
3927             sparc_mode_class[i] = 0;
3928           break;
3929         case MODE_VECTOR_INT:
3930           if (GET_MODE_SIZE (i) <= 4)
3931             sparc_mode_class[i] = 1 << (int)SF_MODE;
3932           else if (GET_MODE_SIZE (i) == 8)
3933             sparc_mode_class[i] = 1 << (int)DF_MODE;
3934           break;
3935         case MODE_FLOAT:
3936         case MODE_COMPLEX_FLOAT:
3937           if (GET_MODE_SIZE (i) <= 4)
3938             sparc_mode_class[i] = 1 << (int) SF_MODE;
3939           else if (GET_MODE_SIZE (i) == 8)
3940             sparc_mode_class[i] = 1 << (int) DF_MODE;
3941           else if (GET_MODE_SIZE (i) == 16)
3942             sparc_mode_class[i] = 1 << (int) TF_MODE;
3943           else if (GET_MODE_SIZE (i) == 32)
3944             sparc_mode_class[i] = 1 << (int) OF_MODE;
3945           else
3946             sparc_mode_class[i] = 0;
3947           break;
3948         case MODE_CC:
3949           if (i == (int) CCFPmode || i == (int) CCFPEmode)
3950             sparc_mode_class[i] = 1 << (int) CCFP_MODE;
3951           else
3952             sparc_mode_class[i] = 1 << (int) CC_MODE;
3953           break;
3954         default:
3955           sparc_mode_class[i] = 0;
3956           break;
3957         }
3958     }
3959
3960   if (TARGET_ARCH64)
3961     hard_regno_mode_classes = hard_64bit_mode_classes;
3962   else
3963     hard_regno_mode_classes = hard_32bit_mode_classes;
3964
3965   /* Initialize the array used by REGNO_REG_CLASS.  */
3966   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3967     {
3968       if (i < 16 && TARGET_V8PLUS)
3969         sparc_regno_reg_class[i] = I64_REGS;
3970       else if (i < 32 || i == FRAME_POINTER_REGNUM)
3971         sparc_regno_reg_class[i] = GENERAL_REGS;
3972       else if (i < 64)
3973         sparc_regno_reg_class[i] = FP_REGS;
3974       else if (i < 96)
3975         sparc_regno_reg_class[i] = EXTRA_FP_REGS;
3976       else if (i < 100)
3977         sparc_regno_reg_class[i] = FPCC_REGS;
3978       else
3979         sparc_regno_reg_class[i] = NO_REGS;
3980     }
3981 }
3982 \f
3983 /* Compute the frame size required by the function.  This function is called
3984    during the reload pass and also by sparc_expand_prologue.  */
3985
3986 HOST_WIDE_INT
3987 sparc_compute_frame_size (HOST_WIDE_INT size, int leaf_function_p)
3988 {
3989   int outgoing_args_size = (crtl->outgoing_args_size
3990                             + REG_PARM_STACK_SPACE (current_function_decl));
3991   int n_regs = 0;  /* N_REGS is the number of 4-byte regs saved thus far.  */
3992   int i;
3993
3994   if (TARGET_ARCH64)
3995     {
3996       for (i = 0; i < 8; i++)
3997         if (df_regs_ever_live_p (i) && ! call_used_regs[i])
3998           n_regs += 2;
3999     }
4000   else
4001     {
4002       for (i = 0; i < 8; i += 2)
4003         if ((df_regs_ever_live_p (i) && ! call_used_regs[i])
4004             || (df_regs_ever_live_p (i+1) && ! call_used_regs[i+1]))
4005           n_regs += 2;
4006     }
4007
4008   for (i = 32; i < (TARGET_V9 ? 96 : 64); i += 2)
4009     if ((df_regs_ever_live_p (i) && ! call_used_regs[i])
4010         || (df_regs_ever_live_p (i+1) && ! call_used_regs[i+1]))
4011       n_regs += 2;
4012
4013   /* Set up values for use in prologue and epilogue.  */
4014   num_gfregs = n_regs;
4015
4016   if (leaf_function_p
4017       && n_regs == 0
4018       && size == 0
4019       && crtl->outgoing_args_size == 0)
4020     actual_fsize = apparent_fsize = 0;
4021   else
4022     {
4023       /* We subtract STARTING_FRAME_OFFSET, remember it's negative.  */
4024       apparent_fsize = (size - STARTING_FRAME_OFFSET + 7) & -8;
4025       apparent_fsize += n_regs * 4;
4026       actual_fsize = apparent_fsize + ((outgoing_args_size + 7) & -8);
4027     }
4028
4029   /* Make sure nothing can clobber our register windows.
4030      If a SAVE must be done, or there is a stack-local variable,
4031      the register window area must be allocated.  */
4032   if (! leaf_function_p || size > 0)
4033     actual_fsize += FIRST_PARM_OFFSET (current_function_decl);
4034
4035   return SPARC_STACK_ALIGN (actual_fsize);
4036 }
4037
4038 /* Output any necessary .register pseudo-ops.  */
4039
4040 void
4041 sparc_output_scratch_registers (FILE *file ATTRIBUTE_UNUSED)
4042 {
4043 #ifdef HAVE_AS_REGISTER_PSEUDO_OP
4044   int i;
4045
4046   if (TARGET_ARCH32)
4047     return;
4048
4049   /* Check if %g[2367] were used without
4050      .register being printed for them already.  */
4051   for (i = 2; i < 8; i++)
4052     {
4053       if (df_regs_ever_live_p (i)
4054           && ! sparc_hard_reg_printed [i])
4055         {
4056           sparc_hard_reg_printed [i] = 1;
4057           /* %g7 is used as TLS base register, use #ignore
4058              for it instead of #scratch.  */
4059           fprintf (file, "\t.register\t%%g%d, #%s\n", i,
4060                    i == 7 ? "ignore" : "scratch");
4061         }
4062       if (i == 3) i = 5;
4063     }
4064 #endif
4065 }
4066
4067 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
4068
4069 #if PROBE_INTERVAL > 4096
4070 #error Cannot use indexed addressing mode for stack probing
4071 #endif
4072
4073 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
4074    inclusive.  These are offsets from the current stack pointer.
4075
4076    Note that we don't use the REG+REG addressing mode for the probes because
4077    of the stack bias in 64-bit mode.  And it doesn't really buy us anything
4078    so the advantages of having a single code win here.  */
4079
4080 static void
4081 sparc_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
4082 {
4083   rtx g1 = gen_rtx_REG (Pmode, 1);
4084
4085   /* See if we have a constant small number of probes to generate.  If so,
4086      that's the easy case.  */
4087   if (size <= PROBE_INTERVAL)
4088     {
4089       emit_move_insn (g1, GEN_INT (first));
4090       emit_insn (gen_rtx_SET (VOIDmode, g1,
4091                               gen_rtx_MINUS (Pmode, stack_pointer_rtx, g1)));
4092       emit_stack_probe (plus_constant (g1, -size));
4093     }
4094
4095   /* The run-time loop is made up of 10 insns in the generic case while the
4096      compile-time loop is made up of 4+2*(n-2) insns for n # of intervals.  */
4097   else if (size <= 5 * PROBE_INTERVAL)
4098     {
4099       HOST_WIDE_INT i;
4100
4101       emit_move_insn (g1, GEN_INT (first + PROBE_INTERVAL));
4102       emit_insn (gen_rtx_SET (VOIDmode, g1,
4103                               gen_rtx_MINUS (Pmode, stack_pointer_rtx, g1)));
4104       emit_stack_probe (g1);
4105
4106       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 2 until
4107          it exceeds SIZE.  If only two probes are needed, this will not
4108          generate any code.  Then probe at FIRST + SIZE.  */
4109       for (i = 2 * PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
4110         {
4111           emit_insn (gen_rtx_SET (VOIDmode, g1,
4112                                   plus_constant (g1, -PROBE_INTERVAL)));
4113           emit_stack_probe (g1);
4114         }
4115
4116       emit_stack_probe (plus_constant (g1, (i - PROBE_INTERVAL) - size));
4117     }
4118
4119   /* Otherwise, do the same as above, but in a loop.  Note that we must be
4120      extra careful with variables wrapping around because we might be at
4121      the very top (or the very bottom) of the address space and we have
4122      to be able to handle this case properly; in particular, we use an
4123      equality test for the loop condition.  */
4124   else
4125     {
4126       HOST_WIDE_INT rounded_size;
4127       rtx g4 = gen_rtx_REG (Pmode, 4);
4128
4129       emit_move_insn (g1, GEN_INT (first));
4130
4131
4132       /* Step 1: round SIZE to the previous multiple of the interval.  */
4133
4134       rounded_size = size & -PROBE_INTERVAL;
4135       emit_move_insn (g4, GEN_INT (rounded_size));
4136
4137
4138       /* Step 2: compute initial and final value of the loop counter.  */
4139
4140       /* TEST_ADDR = SP + FIRST.  */
4141       emit_insn (gen_rtx_SET (VOIDmode, g1,
4142                               gen_rtx_MINUS (Pmode, stack_pointer_rtx, g1)));
4143
4144       /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE.  */
4145       emit_insn (gen_rtx_SET (VOIDmode, g4, gen_rtx_MINUS (Pmode, g1, g4)));
4146
4147
4148       /* Step 3: the loop
4149
4150          while (TEST_ADDR != LAST_ADDR)
4151            {
4152              TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
4153              probe at TEST_ADDR
4154            }
4155
4156          probes at FIRST + N * PROBE_INTERVAL for values of N from 1
4157          until it is equal to ROUNDED_SIZE.  */
4158
4159       if (TARGET_64BIT)
4160         emit_insn (gen_probe_stack_rangedi (g1, g1, g4));
4161       else
4162         emit_insn (gen_probe_stack_rangesi (g1, g1, g4));
4163
4164
4165       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
4166          that SIZE is equal to ROUNDED_SIZE.  */
4167
4168       if (size != rounded_size)
4169         emit_stack_probe (plus_constant (g4, rounded_size - size));
4170     }
4171
4172   /* Make sure nothing is scheduled before we are done.  */
4173   emit_insn (gen_blockage ());
4174 }
4175
4176 /* Probe a range of stack addresses from REG1 to REG2 inclusive.  These are
4177    absolute addresses.  */
4178
4179 const char *
4180 output_probe_stack_range (rtx reg1, rtx reg2)
4181 {
4182   static int labelno = 0;
4183   char loop_lab[32], end_lab[32];
4184   rtx xops[2];
4185
4186   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
4187   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
4188
4189   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
4190
4191    /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
4192   xops[0] = reg1;
4193   xops[1] = reg2;
4194   output_asm_insn ("cmp\t%0, %1", xops);
4195   if (TARGET_ARCH64)
4196     fputs ("\tbe,pn\t%xcc,", asm_out_file);
4197   else
4198     fputs ("\tbe\t", asm_out_file);
4199   assemble_name_raw (asm_out_file, end_lab);
4200   fputc ('\n', asm_out_file);
4201
4202   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
4203   xops[1] = GEN_INT (-PROBE_INTERVAL);
4204   output_asm_insn (" add\t%0, %1, %0", xops);
4205
4206   /* Probe at TEST_ADDR and branch.  */
4207   if (TARGET_ARCH64)
4208     fputs ("\tba,pt\t%xcc,", asm_out_file);
4209   else
4210     fputs ("\tba\t", asm_out_file);
4211   assemble_name_raw (asm_out_file, loop_lab);
4212   fputc ('\n', asm_out_file);
4213   xops[1] = GEN_INT (SPARC_STACK_BIAS);
4214   output_asm_insn (" st\t%%g0, [%0+%1]", xops);
4215
4216   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
4217
4218   return "";
4219 }
4220
4221 /* Save/restore call-saved registers from LOW to HIGH at BASE+OFFSET
4222    as needed.  LOW should be double-word aligned for 32-bit registers.
4223    Return the new OFFSET.  */
4224
4225 #define SORR_SAVE    0
4226 #define SORR_RESTORE 1
4227
4228 static int
4229 save_or_restore_regs (int low, int high, rtx base, int offset, int action)
4230 {
4231   rtx mem, insn;
4232   int i;
4233
4234   if (TARGET_ARCH64 && high <= 32)
4235     {
4236       for (i = low; i < high; i++)
4237         {
4238           if (df_regs_ever_live_p (i) && ! call_used_regs[i])
4239             {
4240               mem = gen_rtx_MEM (DImode, plus_constant (base, offset));
4241               set_mem_alias_set (mem, sparc_sr_alias_set);
4242               if (action == SORR_SAVE)
4243                 {
4244                   insn = emit_move_insn (mem, gen_rtx_REG (DImode, i));
4245                   RTX_FRAME_RELATED_P (insn) = 1;
4246                 }
4247               else  /* action == SORR_RESTORE */
4248                 emit_move_insn (gen_rtx_REG (DImode, i), mem);
4249               offset += 8;
4250             }
4251         }
4252     }
4253   else
4254     {
4255       for (i = low; i < high; i += 2)
4256         {
4257           bool reg0 = df_regs_ever_live_p (i) && ! call_used_regs[i];
4258           bool reg1 = df_regs_ever_live_p (i+1) && ! call_used_regs[i+1];
4259           enum machine_mode mode;
4260           int regno;
4261
4262           if (reg0 && reg1)
4263             {
4264               mode = i < 32 ? DImode : DFmode;
4265               regno = i;
4266             }
4267           else if (reg0)
4268             {
4269               mode = i < 32 ? SImode : SFmode;
4270               regno = i;
4271             }
4272           else if (reg1)
4273             {
4274               mode = i < 32 ? SImode : SFmode;
4275               regno = i + 1;
4276               offset += 4;
4277             }
4278           else
4279             continue;
4280
4281           mem = gen_rtx_MEM (mode, plus_constant (base, offset));
4282           set_mem_alias_set (mem, sparc_sr_alias_set);
4283           if (action == SORR_SAVE)
4284             {
4285               insn = emit_move_insn (mem, gen_rtx_REG (mode, regno));
4286               RTX_FRAME_RELATED_P (insn) = 1;
4287             }
4288           else  /* action == SORR_RESTORE */
4289             emit_move_insn (gen_rtx_REG (mode, regno), mem);
4290
4291           /* Always preserve double-word alignment.  */
4292           offset = (offset + 7) & -8;
4293         }
4294     }
4295
4296   return offset;
4297 }
4298
4299 /* Emit code to save call-saved registers.  */
4300
4301 static void
4302 emit_save_or_restore_regs (int action)
4303 {
4304   HOST_WIDE_INT offset;
4305   rtx base;
4306
4307   offset = frame_base_offset - apparent_fsize;
4308
4309   if (offset < -4096 || offset + num_gfregs * 4 > 4095)
4310     {
4311       /* ??? This might be optimized a little as %g1 might already have a
4312          value close enough that a single add insn will do.  */
4313       /* ??? Although, all of this is probably only a temporary fix
4314          because if %g1 can hold a function result, then
4315          sparc_expand_epilogue will lose (the result will be
4316          clobbered).  */
4317       base = gen_rtx_REG (Pmode, 1);
4318       emit_move_insn (base, GEN_INT (offset));
4319       emit_insn (gen_rtx_SET (VOIDmode,
4320                               base,
4321                               gen_rtx_PLUS (Pmode, frame_base_reg, base)));
4322       offset = 0;
4323     }
4324   else
4325     base = frame_base_reg;
4326
4327   offset = save_or_restore_regs (0, 8, base, offset, action);
4328   save_or_restore_regs (32, TARGET_V9 ? 96 : 64, base, offset, action);
4329 }
4330
4331 /* Generate a save_register_window insn.  */
4332
4333 static rtx
4334 gen_save_register_window (rtx increment)
4335 {
4336   if (TARGET_ARCH64)
4337     return gen_save_register_windowdi (increment);
4338   else
4339     return gen_save_register_windowsi (increment);
4340 }
4341
4342 /* Generate an increment for the stack pointer.  */
4343
4344 static rtx
4345 gen_stack_pointer_inc (rtx increment)
4346 {
4347   return gen_rtx_SET (VOIDmode,
4348                       stack_pointer_rtx,
4349                       gen_rtx_PLUS (Pmode,
4350                                     stack_pointer_rtx,
4351                                     increment));
4352 }
4353
4354 /* Generate a decrement for the stack pointer.  */
4355
4356 static rtx
4357 gen_stack_pointer_dec (rtx decrement)
4358 {
4359   return gen_rtx_SET (VOIDmode,
4360                       stack_pointer_rtx,
4361                       gen_rtx_MINUS (Pmode,
4362                                      stack_pointer_rtx,
4363                                      decrement));
4364 }
4365
4366 /* Expand the function prologue.  The prologue is responsible for reserving
4367    storage for the frame, saving the call-saved registers and loading the
4368    PIC register if needed.  */
4369
4370 void
4371 sparc_expand_prologue (void)
4372 {
4373   rtx insn;
4374   int i;
4375
4376   /* Compute a snapshot of current_function_uses_only_leaf_regs.  Relying
4377      on the final value of the flag means deferring the prologue/epilogue
4378      expansion until just before the second scheduling pass, which is too
4379      late to emit multiple epilogues or return insns.
4380
4381      Of course we are making the assumption that the value of the flag
4382      will not change between now and its final value.  Of the three parts
4383      of the formula, only the last one can reasonably vary.  Let's take a
4384      closer look, after assuming that the first two ones are set to true
4385      (otherwise the last value is effectively silenced).
4386
4387      If only_leaf_regs_used returns false, the global predicate will also
4388      be false so the actual frame size calculated below will be positive.
4389      As a consequence, the save_register_window insn will be emitted in
4390      the instruction stream; now this insn explicitly references %fp
4391      which is not a leaf register so only_leaf_regs_used will always
4392      return false subsequently.
4393
4394      If only_leaf_regs_used returns true, we hope that the subsequent
4395      optimization passes won't cause non-leaf registers to pop up.  For
4396      example, the regrename pass has special provisions to not rename to
4397      non-leaf registers in a leaf function.  */
4398   sparc_leaf_function_p
4399     = optimize > 0 && leaf_function_p () && only_leaf_regs_used ();
4400
4401   /* Need to use actual_fsize, since we are also allocating
4402      space for our callee (and our own register save area).  */
4403   actual_fsize
4404     = sparc_compute_frame_size (get_frame_size(), sparc_leaf_function_p);
4405
4406   /* Advertise that the data calculated just above are now valid.  */
4407   sparc_prologue_data_valid_p = true;
4408
4409   if (flag_stack_usage)
4410     current_function_static_stack_size = actual_fsize;
4411
4412   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK && actual_fsize)
4413     sparc_emit_probe_stack_range (STACK_CHECK_PROTECT, actual_fsize);
4414
4415   if (sparc_leaf_function_p)
4416     {
4417       frame_base_reg = stack_pointer_rtx;
4418       frame_base_offset = actual_fsize + SPARC_STACK_BIAS;
4419     }
4420   else
4421     {
4422       frame_base_reg = hard_frame_pointer_rtx;
4423       frame_base_offset = SPARC_STACK_BIAS;
4424     }
4425
4426   if (actual_fsize == 0)
4427     /* do nothing.  */ ;
4428   else if (sparc_leaf_function_p)
4429     {
4430       if (actual_fsize <= 4096)
4431         insn = emit_insn (gen_stack_pointer_inc (GEN_INT (-actual_fsize)));
4432       else if (actual_fsize <= 8192)
4433         {
4434           insn = emit_insn (gen_stack_pointer_inc (GEN_INT (-4096)));
4435           /* %sp is still the CFA register.  */
4436           RTX_FRAME_RELATED_P (insn) = 1;
4437           insn
4438             = emit_insn (gen_stack_pointer_inc (GEN_INT (4096-actual_fsize)));
4439         }
4440       else
4441         {
4442           rtx reg = gen_rtx_REG (Pmode, 1);
4443           emit_move_insn (reg, GEN_INT (-actual_fsize));
4444           insn = emit_insn (gen_stack_pointer_inc (reg));
4445           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
4446                         gen_stack_pointer_inc (GEN_INT (-actual_fsize)));
4447         }
4448
4449       RTX_FRAME_RELATED_P (insn) = 1;
4450     }
4451   else
4452     {
4453       if (actual_fsize <= 4096)
4454         insn = emit_insn (gen_save_register_window (GEN_INT (-actual_fsize)));
4455       else if (actual_fsize <= 8192)
4456         {
4457           insn = emit_insn (gen_save_register_window (GEN_INT (-4096)));
4458           /* %sp is not the CFA register anymore.  */
4459           emit_insn (gen_stack_pointer_inc (GEN_INT (4096-actual_fsize)));
4460         }
4461       else
4462         {
4463           rtx reg = gen_rtx_REG (Pmode, 1);
4464           emit_move_insn (reg, GEN_INT (-actual_fsize));
4465           insn = emit_insn (gen_save_register_window (reg));
4466         }
4467
4468       RTX_FRAME_RELATED_P (insn) = 1;
4469       for (i=0; i < XVECLEN (PATTERN (insn), 0); i++)
4470         RTX_FRAME_RELATED_P (XVECEXP (PATTERN (insn), 0, i)) = 1;
4471     }
4472
4473   if (num_gfregs)
4474     emit_save_or_restore_regs (SORR_SAVE);
4475
4476   /* Load the PIC register if needed.  */
4477   if (flag_pic && crtl->uses_pic_offset_table)
4478     load_pic_register ();
4479 }
4480
4481 /* This function generates the assembly code for function entry, which boils
4482    down to emitting the necessary .register directives.  */
4483
4484 static void
4485 sparc_asm_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4486 {
4487   /* Check that the assumption we made in sparc_expand_prologue is valid.  */
4488   gcc_assert (sparc_leaf_function_p == current_function_uses_only_leaf_regs);
4489
4490   sparc_output_scratch_registers (file);
4491 }
4492
4493 /* Expand the function epilogue, either normal or part of a sibcall.
4494    We emit all the instructions except the return or the call.  */
4495
4496 void
4497 sparc_expand_epilogue (void)
4498 {
4499   if (num_gfregs)
4500     emit_save_or_restore_regs (SORR_RESTORE);
4501
4502   if (actual_fsize == 0)
4503     /* do nothing.  */ ;
4504   else if (sparc_leaf_function_p)
4505     {
4506       if (actual_fsize <= 4096)
4507         emit_insn (gen_stack_pointer_dec (GEN_INT (- actual_fsize)));
4508       else if (actual_fsize <= 8192)
4509         {
4510           emit_insn (gen_stack_pointer_dec (GEN_INT (-4096)));
4511           emit_insn (gen_stack_pointer_dec (GEN_INT (4096 - actual_fsize)));
4512         }
4513       else
4514         {
4515           rtx reg = gen_rtx_REG (Pmode, 1);
4516           emit_move_insn (reg, GEN_INT (-actual_fsize));
4517           emit_insn (gen_stack_pointer_dec (reg));
4518         }
4519     }
4520 }
4521
4522 /* Return true if it is appropriate to emit `return' instructions in the
4523    body of a function.  */
4524
4525 bool
4526 sparc_can_use_return_insn_p (void)
4527 {
4528   return sparc_prologue_data_valid_p
4529          && (actual_fsize == 0 || !sparc_leaf_function_p);
4530 }
4531
4532 /* This function generates the assembly code for function exit.  */
4533
4534 static void
4535 sparc_asm_function_epilogue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4536 {
4537   /* If the last two instructions of a function are "call foo; dslot;"
4538      the return address might point to the first instruction in the next
4539      function and we have to output a dummy nop for the sake of sane
4540      backtraces in such cases.  This is pointless for sibling calls since
4541      the return address is explicitly adjusted.  */
4542
4543   rtx insn, last_real_insn;
4544
4545   insn = get_last_insn ();
4546
4547   last_real_insn = prev_real_insn (insn);
4548   if (last_real_insn
4549       && GET_CODE (last_real_insn) == INSN
4550       && GET_CODE (PATTERN (last_real_insn)) == SEQUENCE)
4551     last_real_insn = XVECEXP (PATTERN (last_real_insn), 0, 0);
4552
4553   if (last_real_insn
4554       && CALL_P (last_real_insn)
4555       && !SIBLING_CALL_P (last_real_insn))
4556     fputs("\tnop\n", file);
4557
4558   sparc_output_deferred_case_vectors ();
4559 }
4560
4561 /* Output a 'restore' instruction.  */
4562
4563 static void
4564 output_restore (rtx pat)
4565 {
4566   rtx operands[3];
4567
4568   if (! pat)
4569     {
4570       fputs ("\t restore\n", asm_out_file);
4571       return;
4572     }
4573
4574   gcc_assert (GET_CODE (pat) == SET);
4575
4576   operands[0] = SET_DEST (pat);
4577   pat = SET_SRC (pat);
4578
4579   switch (GET_CODE (pat))
4580     {
4581       case PLUS:
4582         operands[1] = XEXP (pat, 0);
4583         operands[2] = XEXP (pat, 1);
4584         output_asm_insn (" restore %r1, %2, %Y0", operands);
4585         break;
4586       case LO_SUM:
4587         operands[1] = XEXP (pat, 0);
4588         operands[2] = XEXP (pat, 1);
4589         output_asm_insn (" restore %r1, %%lo(%a2), %Y0", operands);
4590         break;
4591       case ASHIFT:
4592         operands[1] = XEXP (pat, 0);
4593         gcc_assert (XEXP (pat, 1) == const1_rtx);
4594         output_asm_insn (" restore %r1, %r1, %Y0", operands);
4595         break;
4596       default:
4597         operands[1] = pat;
4598         output_asm_insn (" restore %%g0, %1, %Y0", operands);
4599         break;
4600     }
4601 }
4602
4603 /* Output a return.  */
4604
4605 const char *
4606 output_return (rtx insn)
4607 {
4608   if (sparc_leaf_function_p)
4609     {
4610       /* This is a leaf function so we don't have to bother restoring the
4611          register window, which frees us from dealing with the convoluted
4612          semantics of restore/return.  We simply output the jump to the
4613          return address and the insn in the delay slot (if any).  */
4614
4615       gcc_assert (! crtl->calls_eh_return);
4616
4617       return "jmp\t%%o7+%)%#";
4618     }
4619   else
4620     {
4621       /* This is a regular function so we have to restore the register window.
4622          We may have a pending insn for the delay slot, which will be either
4623          combined with the 'restore' instruction or put in the delay slot of
4624          the 'return' instruction.  */
4625
4626       if (crtl->calls_eh_return)
4627         {
4628           /* If the function uses __builtin_eh_return, the eh_return
4629              machinery occupies the delay slot.  */
4630           gcc_assert (! final_sequence);
4631
4632           if (! flag_delayed_branch)
4633             fputs ("\tadd\t%fp, %g1, %fp\n", asm_out_file);
4634
4635           if (TARGET_V9)
4636             fputs ("\treturn\t%i7+8\n", asm_out_file);
4637           else
4638             fputs ("\trestore\n\tjmp\t%o7+8\n", asm_out_file);
4639
4640           if (flag_delayed_branch)
4641             fputs ("\t add\t%sp, %g1, %sp\n", asm_out_file);
4642           else
4643             fputs ("\t nop\n", asm_out_file);
4644         }
4645       else if (final_sequence)
4646         {
4647           rtx delay, pat;
4648
4649           delay = NEXT_INSN (insn);
4650           gcc_assert (delay);
4651
4652           pat = PATTERN (delay);
4653
4654           if (TARGET_V9 && ! epilogue_renumber (&pat, 1))
4655             {
4656               epilogue_renumber (&pat, 0);
4657               return "return\t%%i7+%)%#";
4658             }
4659           else
4660             {
4661               output_asm_insn ("jmp\t%%i7+%)", NULL);
4662               output_restore (pat);
4663               PATTERN (delay) = gen_blockage ();
4664               INSN_CODE (delay) = -1;
4665             }
4666         }
4667       else
4668         {
4669           /* The delay slot is empty.  */
4670           if (TARGET_V9)
4671             return "return\t%%i7+%)\n\t nop";
4672           else if (flag_delayed_branch)
4673             return "jmp\t%%i7+%)\n\t restore";
4674           else
4675             return "restore\n\tjmp\t%%o7+%)\n\t nop";
4676         }
4677     }
4678
4679   return "";
4680 }
4681
4682 /* Output a sibling call.  */
4683
4684 const char *
4685 output_sibcall (rtx insn, rtx call_operand)
4686 {
4687   rtx operands[1];
4688
4689   gcc_assert (flag_delayed_branch);
4690
4691   operands[0] = call_operand;
4692
4693   if (sparc_leaf_function_p)
4694     {
4695       /* This is a leaf function so we don't have to bother restoring the
4696          register window.  We simply output the jump to the function and
4697          the insn in the delay slot (if any).  */
4698
4699       gcc_assert (!(LEAF_SIBCALL_SLOT_RESERVED_P && final_sequence));
4700
4701       if (final_sequence)
4702         output_asm_insn ("sethi\t%%hi(%a0), %%g1\n\tjmp\t%%g1 + %%lo(%a0)%#",
4703                          operands);
4704       else
4705         /* Use or with rs2 %%g0 instead of mov, so that as/ld can optimize
4706            it into branch if possible.  */
4707         output_asm_insn ("or\t%%o7, %%g0, %%g1\n\tcall\t%a0, 0\n\t or\t%%g1, %%g0, %%o7",
4708                          operands);
4709     }
4710   else
4711     {
4712       /* This is a regular function so we have to restore the register window.
4713          We may have a pending insn for the delay slot, which will be combined
4714          with the 'restore' instruction.  */
4715
4716       output_asm_insn ("call\t%a0, 0", operands);
4717
4718       if (final_sequence)
4719         {
4720           rtx delay = NEXT_INSN (insn);
4721           gcc_assert (delay);
4722
4723           output_restore (PATTERN (delay));
4724
4725           PATTERN (delay) = gen_blockage ();
4726           INSN_CODE (delay) = -1;
4727         }
4728       else
4729         output_restore (NULL_RTX);
4730     }
4731
4732   return "";
4733 }
4734 \f
4735 /* Functions for handling argument passing.
4736
4737    For 32-bit, the first 6 args are normally in registers and the rest are
4738    pushed.  Any arg that starts within the first 6 words is at least
4739    partially passed in a register unless its data type forbids.
4740
4741    For 64-bit, the argument registers are laid out as an array of 16 elements
4742    and arguments are added sequentially.  The first 6 int args and up to the
4743    first 16 fp args (depending on size) are passed in regs.
4744
4745    Slot    Stack   Integral   Float   Float in structure   Double   Long Double
4746    ----    -----   --------   -----   ------------------   ------   -----------
4747     15   [SP+248]              %f31       %f30,%f31         %d30
4748     14   [SP+240]              %f29       %f28,%f29         %d28       %q28
4749     13   [SP+232]              %f27       %f26,%f27         %d26
4750     12   [SP+224]              %f25       %f24,%f25         %d24       %q24
4751     11   [SP+216]              %f23       %f22,%f23         %d22
4752     10   [SP+208]              %f21       %f20,%f21         %d20       %q20
4753      9   [SP+200]              %f19       %f18,%f19         %d18
4754      8   [SP+192]              %f17       %f16,%f17         %d16       %q16
4755      7   [SP+184]              %f15       %f14,%f15         %d14
4756      6   [SP+176]              %f13       %f12,%f13         %d12       %q12
4757      5   [SP+168]     %o5      %f11       %f10,%f11         %d10
4758      4   [SP+160]     %o4       %f9        %f8,%f9           %d8        %q8
4759      3   [SP+152]     %o3       %f7        %f6,%f7           %d6
4760      2   [SP+144]     %o2       %f5        %f4,%f5           %d4        %q4
4761      1   [SP+136]     %o1       %f3        %f2,%f3           %d2
4762      0   [SP+128]     %o0       %f1        %f0,%f1           %d0        %q0
4763
4764    Here SP = %sp if -mno-stack-bias or %sp+stack_bias otherwise.
4765
4766    Integral arguments are always passed as 64-bit quantities appropriately
4767    extended.
4768
4769    Passing of floating point values is handled as follows.
4770    If a prototype is in scope:
4771      If the value is in a named argument (i.e. not a stdarg function or a
4772      value not part of the `...') then the value is passed in the appropriate
4773      fp reg.
4774      If the value is part of the `...' and is passed in one of the first 6
4775      slots then the value is passed in the appropriate int reg.
4776      If the value is part of the `...' and is not passed in one of the first 6
4777      slots then the value is passed in memory.
4778    If a prototype is not in scope:
4779      If the value is one of the first 6 arguments the value is passed in the
4780      appropriate integer reg and the appropriate fp reg.
4781      If the value is not one of the first 6 arguments the value is passed in
4782      the appropriate fp reg and in memory.
4783
4784
4785    Summary of the calling conventions implemented by GCC on the SPARC:
4786
4787    32-bit ABI:
4788                                 size      argument     return value
4789
4790       small integer              <4       int. reg.      int. reg.
4791       word                        4       int. reg.      int. reg.
4792       double word                 8       int. reg.      int. reg.
4793
4794       _Complex small integer     <8       int. reg.      int. reg.
4795       _Complex word               8       int. reg.      int. reg.
4796       _Complex double word       16        memory        int. reg.
4797
4798       vector integer            <=8       int. reg.       FP reg.
4799       vector integer             >8        memory         memory
4800
4801       float                       4       int. reg.       FP reg.
4802       double                      8       int. reg.       FP reg.
4803       long double                16        memory         memory
4804
4805       _Complex float              8        memory         FP reg.
4806       _Complex double            16        memory         FP reg.
4807       _Complex long double       32        memory         FP reg.
4808
4809       vector float              any        memory         memory
4810
4811       aggregate                 any        memory         memory
4812
4813
4814
4815     64-bit ABI:
4816                                 size      argument     return value
4817
4818       small integer              <8       int. reg.      int. reg.
4819       word                        8       int. reg.      int. reg.
4820       double word                16       int. reg.      int. reg.
4821
4822       _Complex small integer    <16       int. reg.      int. reg.
4823       _Complex word              16       int. reg.      int. reg.
4824       _Complex double word       32        memory        int. reg.
4825
4826       vector integer           <=16        FP reg.        FP reg.
4827       vector integer       16<s<=32        memory         FP reg.
4828       vector integer            >32        memory         memory
4829
4830       float                       4        FP reg.        FP reg.
4831       double                      8        FP reg.        FP reg.
4832       long double                16        FP reg.        FP reg.
4833
4834       _Complex float              8        FP reg.        FP reg.
4835       _Complex double            16        FP reg.        FP reg.
4836       _Complex long double       32        memory         FP reg.
4837
4838       vector float             <=16        FP reg.        FP reg.
4839       vector float         16<s<=32        memory         FP reg.
4840       vector float              >32        memory         memory
4841
4842       aggregate                <=16         reg.           reg.
4843       aggregate            16<s<=32        memory          reg.
4844       aggregate                 >32        memory         memory
4845
4846
4847
4848 Note #1: complex floating-point types follow the extended SPARC ABIs as
4849 implemented by the Sun compiler.
4850
4851 Note #2: integral vector types follow the scalar floating-point types
4852 conventions to match what is implemented by the Sun VIS SDK.
4853
4854 Note #3: floating-point vector types follow the aggregate types
4855 conventions.  */
4856
4857
4858 /* Maximum number of int regs for args.  */
4859 #define SPARC_INT_ARG_MAX 6
4860 /* Maximum number of fp regs for args.  */
4861 #define SPARC_FP_ARG_MAX 16
4862
4863 #define ROUND_ADVANCE(SIZE) (((SIZE) + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
4864
4865 /* Handle the INIT_CUMULATIVE_ARGS macro.
4866    Initialize a variable CUM of type CUMULATIVE_ARGS
4867    for a call to a function whose data type is FNTYPE.
4868    For a library call, FNTYPE is 0.  */
4869
4870 void
4871 init_cumulative_args (struct sparc_args *cum, tree fntype,
4872                       rtx libname ATTRIBUTE_UNUSED,
4873                       tree fndecl ATTRIBUTE_UNUSED)
4874 {
4875   cum->words = 0;
4876   cum->prototype_p = fntype && TYPE_ARG_TYPES (fntype);
4877   cum->libcall_p = fntype == 0;
4878 }
4879
4880 /* Handle the TARGET_PROMOTE_PROTOTYPES target hook.
4881    When a prototype says `char' or `short', really pass an `int'.  */
4882
4883 static bool
4884 sparc_promote_prototypes (const_tree fntype ATTRIBUTE_UNUSED)
4885 {
4886   return TARGET_ARCH32 ? true : false;
4887 }
4888
4889 /* Handle promotion of pointer and integer arguments.  */
4890
4891 static enum machine_mode
4892 sparc_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
4893                              enum machine_mode mode,
4894                              int *punsignedp ATTRIBUTE_UNUSED,
4895                              const_tree fntype ATTRIBUTE_UNUSED,
4896                              int for_return ATTRIBUTE_UNUSED)
4897 {
4898   if (POINTER_TYPE_P (type))
4899     {
4900       *punsignedp = POINTERS_EXTEND_UNSIGNED;
4901       return Pmode;
4902     }
4903
4904   /* For TARGET_ARCH64 we need this, as we don't have instructions
4905      for arithmetic operations which do zero/sign extension at the same time,
4906      so without this we end up with a srl/sra after every assignment to an
4907      user variable,  which means very very bad code.  */
4908   if (TARGET_ARCH64
4909       && GET_MODE_CLASS (mode) == MODE_INT
4910       && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
4911     return word_mode;
4912
4913   return mode;
4914 }
4915
4916 /* Handle the TARGET_STRICT_ARGUMENT_NAMING target hook.  */
4917
4918 static bool
4919 sparc_strict_argument_naming (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED)
4920 {
4921   return TARGET_ARCH64 ? true : false;
4922 }
4923
4924 /* Scan the record type TYPE and return the following predicates:
4925     - INTREGS_P: the record contains at least one field or sub-field
4926       that is eligible for promotion in integer registers.
4927     - FP_REGS_P: the record contains at least one field or sub-field
4928       that is eligible for promotion in floating-point registers.
4929     - PACKED_P: the record contains at least one field that is packed.
4930
4931    Sub-fields are not taken into account for the PACKED_P predicate.  */
4932
4933 static void
4934 scan_record_type (tree type, int *intregs_p, int *fpregs_p, int *packed_p)
4935 {
4936   tree field;
4937
4938   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4939     {
4940       if (TREE_CODE (field) == FIELD_DECL)
4941         {
4942           if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
4943             scan_record_type (TREE_TYPE (field), intregs_p, fpregs_p, 0);
4944           else if ((FLOAT_TYPE_P (TREE_TYPE (field))
4945                    || TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
4946                   && TARGET_FPU)
4947             *fpregs_p = 1;
4948           else
4949             *intregs_p = 1;
4950
4951           if (packed_p && DECL_PACKED (field))
4952             *packed_p = 1;
4953         }
4954     }
4955 }
4956
4957 /* Compute the slot number to pass an argument in.
4958    Return the slot number or -1 if passing on the stack.
4959
4960    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4961     the preceding args and about the function being called.
4962    MODE is the argument's machine mode.
4963    TYPE is the data type of the argument (as a tree).
4964     This is null for libcalls where that information may
4965     not be available.
4966    NAMED is nonzero if this argument is a named parameter
4967     (otherwise it is an extra parameter matching an ellipsis).
4968    INCOMING_P is zero for FUNCTION_ARG, nonzero for FUNCTION_INCOMING_ARG.
4969    *PREGNO records the register number to use if scalar type.
4970    *PPADDING records the amount of padding needed in words.  */
4971
4972 static int
4973 function_arg_slotno (const struct sparc_args *cum, enum machine_mode mode,
4974                      tree type, int named, int incoming_p,
4975                      int *pregno, int *ppadding)
4976 {
4977   int regbase = (incoming_p
4978                  ? SPARC_INCOMING_INT_ARG_FIRST
4979                  : SPARC_OUTGOING_INT_ARG_FIRST);
4980   int slotno = cum->words;
4981   enum mode_class mclass;
4982   int regno;
4983
4984   *ppadding = 0;
4985
4986   if (type && TREE_ADDRESSABLE (type))
4987     return -1;
4988
4989   if (TARGET_ARCH32
4990       && mode == BLKmode
4991       && type
4992       && TYPE_ALIGN (type) % PARM_BOUNDARY != 0)
4993     return -1;
4994
4995   /* For SPARC64, objects requiring 16-byte alignment get it.  */
4996   if (TARGET_ARCH64
4997       && (type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode)) >= 128
4998       && (slotno & 1) != 0)
4999     slotno++, *ppadding = 1;
5000
5001   mclass = GET_MODE_CLASS (mode);
5002   if (type && TREE_CODE (type) == VECTOR_TYPE)
5003     {
5004       /* Vector types deserve special treatment because they are
5005          polymorphic wrt their mode, depending upon whether VIS
5006          instructions are enabled.  */
5007       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
5008         {
5009           /* The SPARC port defines no floating-point vector modes.  */
5010           gcc_assert (mode == BLKmode);
5011         }
5012       else
5013         {
5014           /* Integral vector types should either have a vector
5015              mode or an integral mode, because we are guaranteed
5016              by pass_by_reference that their size is not greater
5017              than 16 bytes and TImode is 16-byte wide.  */
5018           gcc_assert (mode != BLKmode);
5019
5020           /* Vector integers are handled like floats according to
5021              the Sun VIS SDK.  */
5022           mclass = MODE_FLOAT;
5023         }
5024     }
5025
5026   switch (mclass)
5027     {
5028     case MODE_FLOAT:
5029     case MODE_COMPLEX_FLOAT:
5030     case MODE_VECTOR_INT:
5031       if (TARGET_ARCH64 && TARGET_FPU && named)
5032         {
5033           if (slotno >= SPARC_FP_ARG_MAX)
5034             return -1;
5035           regno = SPARC_FP_ARG_FIRST + slotno * 2;
5036           /* Arguments filling only one single FP register are
5037              right-justified in the outer double FP register.  */
5038           if (GET_MODE_SIZE (mode) <= 4)
5039             regno++;
5040           break;
5041         }
5042       /* fallthrough */
5043
5044     case MODE_INT:
5045     case MODE_COMPLEX_INT:
5046       if (slotno >= SPARC_INT_ARG_MAX)
5047         return -1;
5048       regno = regbase + slotno;
5049       break;
5050
5051     case MODE_RANDOM:
5052       if (mode == VOIDmode)
5053         /* MODE is VOIDmode when generating the actual call.  */
5054         return -1;
5055
5056       gcc_assert (mode == BLKmode);
5057
5058       if (TARGET_ARCH32
5059           || !type
5060           || (TREE_CODE (type) != VECTOR_TYPE
5061               && TREE_CODE (type) != RECORD_TYPE))
5062         {
5063           if (slotno >= SPARC_INT_ARG_MAX)
5064             return -1;
5065           regno = regbase + slotno;
5066         }
5067       else  /* TARGET_ARCH64 && type */
5068         {
5069           int intregs_p = 0, fpregs_p = 0, packed_p = 0;
5070
5071           /* First see what kinds of registers we would need.  */
5072           if (TREE_CODE (type) == VECTOR_TYPE)
5073             fpregs_p = 1;
5074           else
5075             scan_record_type (type, &intregs_p, &fpregs_p, &packed_p);
5076
5077           /* The ABI obviously doesn't specify how packed structures
5078              are passed.  These are defined to be passed in int regs
5079              if possible, otherwise memory.  */
5080           if (packed_p || !named)
5081             fpregs_p = 0, intregs_p = 1;
5082
5083           /* If all arg slots are filled, then must pass on stack.  */
5084           if (fpregs_p && slotno >= SPARC_FP_ARG_MAX)
5085             return -1;
5086
5087           /* If there are only int args and all int arg slots are filled,
5088              then must pass on stack.  */
5089           if (!fpregs_p && intregs_p && slotno >= SPARC_INT_ARG_MAX)
5090             return -1;
5091
5092           /* Note that even if all int arg slots are filled, fp members may
5093              still be passed in regs if such regs are available.
5094              *PREGNO isn't set because there may be more than one, it's up
5095              to the caller to compute them.  */
5096           return slotno;
5097         }
5098       break;
5099
5100     default :
5101       gcc_unreachable ();
5102     }
5103
5104   *pregno = regno;
5105   return slotno;
5106 }
5107
5108 /* Handle recursive register counting for structure field layout.  */
5109
5110 struct function_arg_record_value_parms
5111 {
5112   rtx ret;              /* return expression being built.  */
5113   int slotno;           /* slot number of the argument.  */
5114   int named;            /* whether the argument is named.  */
5115   int regbase;          /* regno of the base register.  */
5116   int stack;            /* 1 if part of the argument is on the stack.  */
5117   int intoffset;        /* offset of the first pending integer field.  */
5118   unsigned int nregs;   /* number of words passed in registers.  */
5119 };
5120
5121 static void function_arg_record_value_3
5122  (HOST_WIDE_INT, struct function_arg_record_value_parms *);
5123 static void function_arg_record_value_2
5124  (const_tree, HOST_WIDE_INT, struct function_arg_record_value_parms *, bool);
5125 static void function_arg_record_value_1
5126  (const_tree, HOST_WIDE_INT, struct function_arg_record_value_parms *, bool);
5127 static rtx function_arg_record_value (const_tree, enum machine_mode, int, int, int);
5128 static rtx function_arg_union_value (int, enum machine_mode, int, int);
5129
5130 /* A subroutine of function_arg_record_value.  Traverse the structure
5131    recursively and determine how many registers will be required.  */
5132
5133 static void
5134 function_arg_record_value_1 (const_tree type, HOST_WIDE_INT startbitpos,
5135                              struct function_arg_record_value_parms *parms,
5136                              bool packed_p)
5137 {
5138   tree field;
5139
5140   /* We need to compute how many registers are needed so we can
5141      allocate the PARALLEL but before we can do that we need to know
5142      whether there are any packed fields.  The ABI obviously doesn't
5143      specify how structures are passed in this case, so they are
5144      defined to be passed in int regs if possible, otherwise memory,
5145      regardless of whether there are fp values present.  */
5146
5147   if (! packed_p)
5148     for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5149       {
5150         if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field))
5151           {
5152             packed_p = true;
5153             break;
5154           }
5155       }
5156
5157   /* Compute how many registers we need.  */
5158   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5159     {
5160       if (TREE_CODE (field) == FIELD_DECL)
5161         {
5162           HOST_WIDE_INT bitpos = startbitpos;
5163
5164           if (DECL_SIZE (field) != 0)
5165             {
5166               if (integer_zerop (DECL_SIZE (field)))
5167                 continue;
5168
5169               if (host_integerp (bit_position (field), 1))
5170                 bitpos += int_bit_position (field);
5171             }
5172
5173           /* ??? FIXME: else assume zero offset.  */
5174
5175           if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
5176             function_arg_record_value_1 (TREE_TYPE (field),
5177                                          bitpos,
5178                                          parms,
5179                                          packed_p);
5180           else if ((FLOAT_TYPE_P (TREE_TYPE (field))
5181                     || TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
5182                    && TARGET_FPU
5183                    && parms->named
5184                    && ! packed_p)
5185             {
5186               if (parms->intoffset != -1)
5187                 {
5188                   unsigned int startbit, endbit;
5189                   int intslots, this_slotno;
5190
5191                   startbit = parms->intoffset & -BITS_PER_WORD;
5192                   endbit   = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
5193
5194                   intslots = (endbit - startbit) / BITS_PER_WORD;
5195                   this_slotno = parms->slotno + parms->intoffset
5196                     / BITS_PER_WORD;
5197
5198                   if (intslots > 0 && intslots > SPARC_INT_ARG_MAX - this_slotno)
5199                     {
5200                       intslots = MAX (0, SPARC_INT_ARG_MAX - this_slotno);
5201                       /* We need to pass this field on the stack.  */
5202                       parms->stack = 1;
5203                     }
5204
5205                   parms->nregs += intslots;
5206                   parms->intoffset = -1;
5207                 }
5208
5209               /* There's no need to check this_slotno < SPARC_FP_ARG MAX.
5210                  If it wasn't true we wouldn't be here.  */
5211               if (TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE
5212                   && DECL_MODE (field) == BLKmode)
5213                 parms->nregs += TYPE_VECTOR_SUBPARTS (TREE_TYPE (field));
5214               else if (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE)
5215                 parms->nregs += 2;
5216               else
5217                 parms->nregs += 1;
5218             }
5219           else
5220             {
5221               if (parms->intoffset == -1)
5222                 parms->intoffset = bitpos;
5223             }
5224         }
5225     }
5226 }
5227
5228 /* A subroutine of function_arg_record_value.  Assign the bits of the
5229    structure between parms->intoffset and bitpos to integer registers.  */
5230
5231 static void
5232 function_arg_record_value_3 (HOST_WIDE_INT bitpos,
5233                              struct function_arg_record_value_parms *parms)
5234 {
5235   enum machine_mode mode;
5236   unsigned int regno;
5237   unsigned int startbit, endbit;
5238   int this_slotno, intslots, intoffset;
5239   rtx reg;
5240
5241   if (parms->intoffset == -1)
5242     return;
5243
5244   intoffset = parms->intoffset;
5245   parms->intoffset = -1;
5246
5247   startbit = intoffset & -BITS_PER_WORD;
5248   endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
5249   intslots = (endbit - startbit) / BITS_PER_WORD;
5250   this_slotno = parms->slotno + intoffset / BITS_PER_WORD;
5251
5252   intslots = MIN (intslots, SPARC_INT_ARG_MAX - this_slotno);
5253   if (intslots <= 0)
5254     return;
5255
5256   /* If this is the trailing part of a word, only load that much into
5257      the register.  Otherwise load the whole register.  Note that in
5258      the latter case we may pick up unwanted bits.  It's not a problem
5259      at the moment but may wish to revisit.  */
5260
5261   if (intoffset % BITS_PER_WORD != 0)
5262     mode = smallest_mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
5263                                    MODE_INT);
5264   else
5265     mode = word_mode;
5266
5267   intoffset /= BITS_PER_UNIT;
5268   do
5269     {
5270       regno = parms->regbase + this_slotno;
5271       reg = gen_rtx_REG (mode, regno);
5272       XVECEXP (parms->ret, 0, parms->stack + parms->nregs)
5273         = gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
5274
5275       this_slotno += 1;
5276       intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
5277       mode = word_mode;
5278       parms->nregs += 1;
5279       intslots -= 1;
5280     }
5281   while (intslots > 0);
5282 }
5283
5284 /* A subroutine of function_arg_record_value.  Traverse the structure
5285    recursively and assign bits to floating point registers.  Track which
5286    bits in between need integer registers; invoke function_arg_record_value_3
5287    to make that happen.  */
5288
5289 static void
5290 function_arg_record_value_2 (const_tree type, HOST_WIDE_INT startbitpos,
5291                              struct function_arg_record_value_parms *parms,
5292                              bool packed_p)
5293 {
5294   tree field;
5295
5296   if (! packed_p)
5297     for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5298       {
5299         if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field))
5300           {
5301             packed_p = true;
5302             break;
5303           }
5304       }
5305
5306   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5307     {
5308       if (TREE_CODE (field) == FIELD_DECL)
5309         {
5310           HOST_WIDE_INT bitpos = startbitpos;
5311
5312           if (DECL_SIZE (field) != 0)
5313             {
5314               if (integer_zerop (DECL_SIZE (field)))
5315                 continue;
5316
5317               if (host_integerp (bit_position (field), 1))
5318                 bitpos += int_bit_position (field);
5319             }
5320
5321           /* ??? FIXME: else assume zero offset.  */
5322
5323           if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
5324             function_arg_record_value_2 (TREE_TYPE (field),
5325                                          bitpos,
5326                                          parms,
5327                                          packed_p);
5328           else if ((FLOAT_TYPE_P (TREE_TYPE (field))
5329                     || TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
5330                    && TARGET_FPU
5331                    && parms->named
5332                    && ! packed_p)
5333             {
5334               int this_slotno = parms->slotno + bitpos / BITS_PER_WORD;
5335               int regno, nregs, pos;
5336               enum machine_mode mode = DECL_MODE (field);
5337               rtx reg;
5338
5339               function_arg_record_value_3 (bitpos, parms);
5340
5341               if (TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE
5342                   && mode == BLKmode)
5343                 {
5344                   mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (field)));
5345                   nregs = TYPE_VECTOR_SUBPARTS (TREE_TYPE (field));
5346                 }
5347               else if (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE)
5348                 {
5349                   mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (field)));
5350                   nregs = 2;
5351                 }
5352               else
5353                 nregs = 1;
5354
5355               regno = SPARC_FP_ARG_FIRST + this_slotno * 2;
5356               if (GET_MODE_SIZE (mode) <= 4 && (bitpos & 32) != 0)
5357                 regno++;
5358               reg = gen_rtx_REG (mode, regno);
5359               pos = bitpos / BITS_PER_UNIT;
5360               XVECEXP (parms->ret, 0, parms->stack + parms->nregs)
5361                 = gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (pos));
5362               parms->nregs += 1;
5363               while (--nregs > 0)
5364                 {
5365                   regno += GET_MODE_SIZE (mode) / 4;
5366                   reg = gen_rtx_REG (mode, regno);
5367                   pos += GET_MODE_SIZE (mode);
5368                   XVECEXP (parms->ret, 0, parms->stack + parms->nregs)
5369                     = gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (pos));
5370                   parms->nregs += 1;
5371                 }
5372             }
5373           else
5374             {
5375               if (parms->intoffset == -1)
5376                 parms->intoffset = bitpos;
5377             }
5378         }
5379     }
5380 }
5381
5382 /* Used by function_arg and sparc_function_value_1 to implement the complex
5383    conventions of the 64-bit ABI for passing and returning structures.
5384    Return an expression valid as a return value for the FUNCTION_ARG
5385    and TARGET_FUNCTION_VALUE.
5386
5387    TYPE is the data type of the argument (as a tree).
5388     This is null for libcalls where that information may
5389     not be available.
5390    MODE is the argument's machine mode.
5391    SLOTNO is the index number of the argument's slot in the parameter array.
5392    NAMED is nonzero if this argument is a named parameter
5393     (otherwise it is an extra parameter matching an ellipsis).
5394    REGBASE is the regno of the base register for the parameter array.  */
5395
5396 static rtx
5397 function_arg_record_value (const_tree type, enum machine_mode mode,
5398                            int slotno, int named, int regbase)
5399 {
5400   HOST_WIDE_INT typesize = int_size_in_bytes (type);
5401   struct function_arg_record_value_parms parms;
5402   unsigned int nregs;
5403
5404   parms.ret = NULL_RTX;
5405   parms.slotno = slotno;
5406   parms.named = named;
5407   parms.regbase = regbase;
5408   parms.stack = 0;
5409
5410   /* Compute how many registers we need.  */
5411   parms.nregs = 0;
5412   parms.intoffset = 0;
5413   function_arg_record_value_1 (type, 0, &parms, false);
5414
5415   /* Take into account pending integer fields.  */
5416   if (parms.intoffset != -1)
5417     {
5418       unsigned int startbit, endbit;
5419       int intslots, this_slotno;
5420
5421       startbit = parms.intoffset & -BITS_PER_WORD;
5422       endbit = (typesize*BITS_PER_UNIT + BITS_PER_WORD - 1) & -BITS_PER_WORD;
5423       intslots = (endbit - startbit) / BITS_PER_WORD;
5424       this_slotno = slotno + parms.intoffset / BITS_PER_WORD;
5425
5426       if (intslots > 0 && intslots > SPARC_INT_ARG_MAX - this_slotno)
5427         {
5428           intslots = MAX (0, SPARC_INT_ARG_MAX - this_slotno);
5429           /* We need to pass this field on the stack.  */
5430           parms.stack = 1;
5431         }
5432
5433       parms.nregs += intslots;
5434     }
5435   nregs = parms.nregs;
5436
5437   /* Allocate the vector and handle some annoying special cases.  */
5438   if (nregs == 0)
5439     {
5440       /* ??? Empty structure has no value?  Duh?  */
5441       if (typesize <= 0)
5442         {
5443           /* Though there's nothing really to store, return a word register
5444              anyway so the rest of gcc doesn't go nuts.  Returning a PARALLEL
5445              leads to breakage due to the fact that there are zero bytes to
5446              load.  */
5447           return gen_rtx_REG (mode, regbase);
5448         }
5449       else
5450         {
5451           /* ??? C++ has structures with no fields, and yet a size.  Give up
5452              for now and pass everything back in integer registers.  */
5453           nregs = (typesize + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5454         }
5455       if (nregs + slotno > SPARC_INT_ARG_MAX)
5456         nregs = SPARC_INT_ARG_MAX - slotno;
5457     }
5458   gcc_assert (nregs != 0);
5459
5460   parms.ret = gen_rtx_PARALLEL (mode, rtvec_alloc (parms.stack + nregs));
5461
5462   /* If at least one field must be passed on the stack, generate
5463      (parallel [(expr_list (nil) ...) ...]) so that all fields will
5464      also be passed on the stack.  We can't do much better because the
5465      semantics of TARGET_ARG_PARTIAL_BYTES doesn't handle the case
5466      of structures for which the fields passed exclusively in registers
5467      are not at the beginning of the structure.  */
5468   if (parms.stack)
5469     XVECEXP (parms.ret, 0, 0)
5470       = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
5471
5472   /* Fill in the entries.  */
5473   parms.nregs = 0;
5474   parms.intoffset = 0;
5475   function_arg_record_value_2 (type, 0, &parms, false);
5476   function_arg_record_value_3 (typesize * BITS_PER_UNIT, &parms);
5477
5478   gcc_assert (parms.nregs == nregs);
5479
5480   return parms.ret;
5481 }
5482
5483 /* Used by function_arg and sparc_function_value_1 to implement the conventions
5484    of the 64-bit ABI for passing and returning unions.
5485    Return an expression valid as a return value for the FUNCTION_ARG
5486    and TARGET_FUNCTION_VALUE.
5487
5488    SIZE is the size in bytes of the union.
5489    MODE is the argument's machine mode.
5490    REGNO is the hard register the union will be passed in.  */
5491
5492 static rtx
5493 function_arg_union_value (int size, enum machine_mode mode, int slotno,
5494                           int regno)
5495 {
5496   int nwords = ROUND_ADVANCE (size), i;
5497   rtx regs;
5498
5499   /* See comment in previous function for empty structures.  */
5500   if (nwords == 0)
5501     return gen_rtx_REG (mode, regno);
5502
5503   if (slotno == SPARC_INT_ARG_MAX - 1)
5504     nwords = 1;
5505
5506   regs = gen_rtx_PARALLEL (mode, rtvec_alloc (nwords));
5507
5508   for (i = 0; i < nwords; i++)
5509     {
5510       /* Unions are passed left-justified.  */
5511       XVECEXP (regs, 0, i)
5512         = gen_rtx_EXPR_LIST (VOIDmode,
5513                              gen_rtx_REG (word_mode, regno),
5514                              GEN_INT (UNITS_PER_WORD * i));
5515       regno++;
5516     }
5517
5518   return regs;
5519 }
5520
5521 /* Used by function_arg and sparc_function_value_1 to implement the conventions
5522    for passing and returning large (BLKmode) vectors.
5523    Return an expression valid as a return value for the FUNCTION_ARG
5524    and TARGET_FUNCTION_VALUE.
5525
5526    SIZE is the size in bytes of the vector (at least 8 bytes).
5527    REGNO is the FP hard register the vector will be passed in.  */
5528
5529 static rtx
5530 function_arg_vector_value (int size, int regno)
5531 {
5532   int i, nregs = size / 8;
5533   rtx regs;
5534
5535   regs = gen_rtx_PARALLEL (BLKmode, rtvec_alloc (nregs));
5536
5537   for (i = 0; i < nregs; i++)
5538     {
5539       XVECEXP (regs, 0, i)
5540         = gen_rtx_EXPR_LIST (VOIDmode,
5541                              gen_rtx_REG (DImode, regno + 2*i),
5542                              GEN_INT (i*8));
5543     }
5544
5545   return regs;
5546 }
5547
5548 /* Handle the FUNCTION_ARG macro.
5549    Determine where to put an argument to a function.
5550    Value is zero to push the argument on the stack,
5551    or a hard register in which to store the argument.
5552
5553    CUM is a variable of type CUMULATIVE_ARGS which gives info about
5554     the preceding args and about the function being called.
5555    MODE is the argument's machine mode.
5556    TYPE is the data type of the argument (as a tree).
5557     This is null for libcalls where that information may
5558     not be available.
5559    NAMED is nonzero if this argument is a named parameter
5560     (otherwise it is an extra parameter matching an ellipsis).
5561    INCOMING_P is zero for FUNCTION_ARG, nonzero for FUNCTION_INCOMING_ARG.  */
5562
5563 rtx
5564 function_arg (const struct sparc_args *cum, enum machine_mode mode,
5565               tree type, int named, int incoming_p)
5566 {
5567   int regbase = (incoming_p
5568                  ? SPARC_INCOMING_INT_ARG_FIRST
5569                  : SPARC_OUTGOING_INT_ARG_FIRST);
5570   int slotno, regno, padding;
5571   enum mode_class mclass = GET_MODE_CLASS (mode);
5572
5573   slotno = function_arg_slotno (cum, mode, type, named, incoming_p,
5574                                 &regno, &padding);
5575   if (slotno == -1)
5576     return 0;
5577
5578   /* Vector types deserve special treatment because they are polymorphic wrt
5579      their mode, depending upon whether VIS instructions are enabled.  */
5580   if (type && TREE_CODE (type) == VECTOR_TYPE)
5581     {
5582       HOST_WIDE_INT size = int_size_in_bytes (type);
5583       gcc_assert ((TARGET_ARCH32 && size <= 8)
5584                   || (TARGET_ARCH64 && size <= 16));
5585
5586       if (mode == BLKmode)
5587         return function_arg_vector_value (size,
5588                                           SPARC_FP_ARG_FIRST + 2*slotno);
5589       else
5590         mclass = MODE_FLOAT;
5591     }
5592
5593   if (TARGET_ARCH32)
5594     return gen_rtx_REG (mode, regno);
5595
5596   /* Structures up to 16 bytes in size are passed in arg slots on the stack
5597      and are promoted to registers if possible.  */
5598   if (type && TREE_CODE (type) == RECORD_TYPE)
5599     {
5600       HOST_WIDE_INT size = int_size_in_bytes (type);
5601       gcc_assert (size <= 16);
5602
5603       return function_arg_record_value (type, mode, slotno, named, regbase);
5604     }
5605
5606   /* Unions up to 16 bytes in size are passed in integer registers.  */
5607   else if (type && TREE_CODE (type) == UNION_TYPE)
5608     {
5609       HOST_WIDE_INT size = int_size_in_bytes (type);
5610       gcc_assert (size <= 16);
5611
5612       return function_arg_union_value (size, mode, slotno, regno);
5613     }
5614
5615   /* v9 fp args in reg slots beyond the int reg slots get passed in regs
5616      but also have the slot allocated for them.
5617      If no prototype is in scope fp values in register slots get passed
5618      in two places, either fp regs and int regs or fp regs and memory.  */
5619   else if ((mclass == MODE_FLOAT || mclass == MODE_COMPLEX_FLOAT)
5620            && SPARC_FP_REG_P (regno))
5621     {
5622       rtx reg = gen_rtx_REG (mode, regno);
5623       if (cum->prototype_p || cum->libcall_p)
5624         {
5625           /* "* 2" because fp reg numbers are recorded in 4 byte
5626              quantities.  */
5627 #if 0
5628           /* ??? This will cause the value to be passed in the fp reg and
5629              in the stack.  When a prototype exists we want to pass the
5630              value in the reg but reserve space on the stack.  That's an
5631              optimization, and is deferred [for a bit].  */
5632           if ((regno - SPARC_FP_ARG_FIRST) >= SPARC_INT_ARG_MAX * 2)
5633             return gen_rtx_PARALLEL (mode,
5634                             gen_rtvec (2,
5635                                        gen_rtx_EXPR_LIST (VOIDmode,
5636                                                 NULL_RTX, const0_rtx),
5637                                        gen_rtx_EXPR_LIST (VOIDmode,
5638                                                 reg, const0_rtx)));
5639           else
5640 #else
5641           /* ??? It seems that passing back a register even when past
5642              the area declared by REG_PARM_STACK_SPACE will allocate
5643              space appropriately, and will not copy the data onto the
5644              stack, exactly as we desire.
5645
5646              This is due to locate_and_pad_parm being called in
5647              expand_call whenever reg_parm_stack_space > 0, which
5648              while beneficial to our example here, would seem to be
5649              in error from what had been intended.  Ho hum...  -- r~ */
5650 #endif
5651             return reg;
5652         }
5653       else
5654         {
5655           rtx v0, v1;
5656
5657           if ((regno - SPARC_FP_ARG_FIRST) < SPARC_INT_ARG_MAX * 2)
5658             {
5659               int intreg;
5660
5661               /* On incoming, we don't need to know that the value
5662                  is passed in %f0 and %i0, and it confuses other parts
5663                  causing needless spillage even on the simplest cases.  */
5664               if (incoming_p)
5665                 return reg;
5666
5667               intreg = (SPARC_OUTGOING_INT_ARG_FIRST
5668                         + (regno - SPARC_FP_ARG_FIRST) / 2);
5669
5670               v0 = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
5671               v1 = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_REG (mode, intreg),
5672                                       const0_rtx);
5673               return gen_rtx_PARALLEL (mode, gen_rtvec (2, v0, v1));
5674             }
5675           else
5676             {
5677               v0 = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
5678               v1 = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
5679               return gen_rtx_PARALLEL (mode, gen_rtvec (2, v0, v1));
5680             }
5681         }
5682     }
5683
5684   /* All other aggregate types are passed in an integer register in a mode
5685      corresponding to the size of the type.  */
5686   else if (type && AGGREGATE_TYPE_P (type))
5687     {
5688       HOST_WIDE_INT size = int_size_in_bytes (type);
5689       gcc_assert (size <= 16);
5690
5691       mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
5692     }
5693
5694   return gen_rtx_REG (mode, regno);
5695 }
5696
5697 /* For an arg passed partly in registers and partly in memory,
5698    this is the number of bytes of registers used.
5699    For args passed entirely in registers or entirely in memory, zero.
5700
5701    Any arg that starts in the first 6 regs but won't entirely fit in them
5702    needs partial registers on v8.  On v9, structures with integer
5703    values in arg slots 5,6 will be passed in %o5 and SP+176, and complex fp
5704    values that begin in the last fp reg [where "last fp reg" varies with the
5705    mode] will be split between that reg and memory.  */
5706
5707 static int
5708 sparc_arg_partial_bytes (CUMULATIVE_ARGS *cum, enum machine_mode mode,
5709                          tree type, bool named)
5710 {
5711   int slotno, regno, padding;
5712
5713   /* We pass 0 for incoming_p here, it doesn't matter.  */
5714   slotno = function_arg_slotno (cum, mode, type, named, 0, &regno, &padding);
5715
5716   if (slotno == -1)
5717     return 0;
5718
5719   if (TARGET_ARCH32)
5720     {
5721       if ((slotno + (mode == BLKmode
5722                      ? ROUND_ADVANCE (int_size_in_bytes (type))
5723                      : ROUND_ADVANCE (GET_MODE_SIZE (mode))))
5724           > SPARC_INT_ARG_MAX)
5725         return (SPARC_INT_ARG_MAX - slotno) * UNITS_PER_WORD;
5726     }
5727   else
5728     {
5729       /* We are guaranteed by pass_by_reference that the size of the
5730          argument is not greater than 16 bytes, so we only need to return
5731          one word if the argument is partially passed in registers.  */
5732
5733       if (type && AGGREGATE_TYPE_P (type))
5734         {
5735           int size = int_size_in_bytes (type);
5736
5737           if (size > UNITS_PER_WORD
5738               && slotno == SPARC_INT_ARG_MAX - 1)
5739             return UNITS_PER_WORD;
5740         }
5741       else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT
5742                || (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5743                    && ! (TARGET_FPU && named)))
5744         {
5745           /* The complex types are passed as packed types.  */
5746           if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
5747               && slotno == SPARC_INT_ARG_MAX - 1)
5748             return UNITS_PER_WORD;
5749         }
5750       else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5751         {
5752           if ((slotno + GET_MODE_SIZE (mode) / UNITS_PER_WORD)
5753               > SPARC_FP_ARG_MAX)
5754             return UNITS_PER_WORD;
5755         }
5756     }
5757
5758   return 0;
5759 }
5760
5761 /* Handle the TARGET_PASS_BY_REFERENCE target hook.
5762    Specify whether to pass the argument by reference.  */
5763
5764 static bool
5765 sparc_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
5766                          enum machine_mode mode, const_tree type,
5767                          bool named ATTRIBUTE_UNUSED)
5768 {
5769   if (TARGET_ARCH32)
5770     /* Original SPARC 32-bit ABI says that structures and unions,
5771        and quad-precision floats are passed by reference.  For Pascal,
5772        also pass arrays by reference.  All other base types are passed
5773        in registers.
5774
5775        Extended ABI (as implemented by the Sun compiler) says that all
5776        complex floats are passed by reference.  Pass complex integers
5777        in registers up to 8 bytes.  More generally, enforce the 2-word
5778        cap for passing arguments in registers.
5779
5780        Vector ABI (as implemented by the Sun VIS SDK) says that vector
5781        integers are passed like floats of the same size, that is in
5782        registers up to 8 bytes.  Pass all vector floats by reference
5783        like structure and unions.  */
5784     return ((type && (AGGREGATE_TYPE_P (type) || VECTOR_FLOAT_TYPE_P (type)))
5785             || mode == SCmode
5786             /* Catch CDImode, TFmode, DCmode and TCmode.  */
5787             || GET_MODE_SIZE (mode) > 8
5788             || (type
5789                 && TREE_CODE (type) == VECTOR_TYPE
5790                 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8));
5791   else
5792     /* Original SPARC 64-bit ABI says that structures and unions
5793        smaller than 16 bytes are passed in registers, as well as
5794        all other base types.
5795
5796        Extended ABI (as implemented by the Sun compiler) says that
5797        complex floats are passed in registers up to 16 bytes.  Pass
5798        all complex integers in registers up to 16 bytes.  More generally,
5799        enforce the 2-word cap for passing arguments in registers.
5800
5801        Vector ABI (as implemented by the Sun VIS SDK) says that vector
5802        integers are passed like floats of the same size, that is in
5803        registers (up to 16 bytes).  Pass all vector floats like structure
5804        and unions.  */
5805     return ((type
5806              && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == VECTOR_TYPE)
5807              && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 16)
5808             /* Catch CTImode and TCmode.  */
5809             || GET_MODE_SIZE (mode) > 16);
5810 }
5811
5812 /* Handle the FUNCTION_ARG_ADVANCE macro.
5813    Update the data in CUM to advance over an argument
5814    of mode MODE and data type TYPE.
5815    TYPE is null for libcalls where that information may not be available.  */
5816
5817 void
5818 function_arg_advance (struct sparc_args *cum, enum machine_mode mode,
5819                       tree type, int named)
5820 {
5821   int regno, padding;
5822
5823   /* We pass 0 for incoming_p here, it doesn't matter.  */
5824   function_arg_slotno (cum, mode, type, named, 0, &regno, &padding);
5825
5826   /* If argument requires leading padding, add it.  */
5827   cum->words += padding;
5828
5829   if (TARGET_ARCH32)
5830     {
5831       cum->words += (mode != BLKmode
5832                      ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
5833                      : ROUND_ADVANCE (int_size_in_bytes (type)));
5834     }
5835   else
5836     {
5837       if (type && AGGREGATE_TYPE_P (type))
5838         {
5839           int size = int_size_in_bytes (type);
5840
5841           if (size <= 8)
5842             ++cum->words;
5843           else if (size <= 16)
5844             cum->words += 2;
5845           else /* passed by reference */
5846             ++cum->words;
5847         }
5848       else
5849         {
5850           cum->words += (mode != BLKmode
5851                          ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
5852                          : ROUND_ADVANCE (int_size_in_bytes (type)));
5853         }
5854     }
5855 }
5856
5857 /* Handle the FUNCTION_ARG_PADDING macro.
5858    For the 64 bit ABI structs are always stored left shifted in their
5859    argument slot.  */
5860
5861 enum direction
5862 function_arg_padding (enum machine_mode mode, const_tree type)
5863 {
5864   if (TARGET_ARCH64 && type != 0 && AGGREGATE_TYPE_P (type))
5865     return upward;
5866
5867   /* Fall back to the default.  */
5868   return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
5869 }
5870
5871 /* Handle the TARGET_RETURN_IN_MEMORY target hook.
5872    Specify whether to return the return value in memory.  */
5873
5874 static bool
5875 sparc_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
5876 {
5877   if (TARGET_ARCH32)
5878     /* Original SPARC 32-bit ABI says that structures and unions,
5879        and quad-precision floats are returned in memory.  All other
5880        base types are returned in registers.
5881
5882        Extended ABI (as implemented by the Sun compiler) says that
5883        all complex floats are returned in registers (8 FP registers
5884        at most for '_Complex long double').  Return all complex integers
5885        in registers (4 at most for '_Complex long long').
5886
5887        Vector ABI (as implemented by the Sun VIS SDK) says that vector
5888        integers are returned like floats of the same size, that is in
5889        registers up to 8 bytes and in memory otherwise.  Return all
5890        vector floats in memory like structure and unions; note that
5891        they always have BLKmode like the latter.  */
5892     return (TYPE_MODE (type) == BLKmode
5893             || TYPE_MODE (type) == TFmode
5894             || (TREE_CODE (type) == VECTOR_TYPE
5895                 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8));
5896   else
5897     /* Original SPARC 64-bit ABI says that structures and unions
5898        smaller than 32 bytes are returned in registers, as well as
5899        all other base types.
5900
5901        Extended ABI (as implemented by the Sun compiler) says that all
5902        complex floats are returned in registers (8 FP registers at most
5903        for '_Complex long double').  Return all complex integers in
5904        registers (4 at most for '_Complex TItype').
5905
5906        Vector ABI (as implemented by the Sun VIS SDK) says that vector
5907        integers are returned like floats of the same size, that is in
5908        registers.  Return all vector floats like structure and unions;
5909        note that they always have BLKmode like the latter.  */
5910     return ((TYPE_MODE (type) == BLKmode
5911              && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 32));
5912 }
5913
5914 /* Handle the TARGET_STRUCT_VALUE target hook.
5915    Return where to find the structure return value address.  */
5916
5917 static rtx
5918 sparc_struct_value_rtx (tree fndecl, int incoming)
5919 {
5920   if (TARGET_ARCH64)
5921     return 0;
5922   else
5923     {
5924       rtx mem;
5925
5926       if (incoming)
5927         mem = gen_rtx_MEM (Pmode, plus_constant (frame_pointer_rtx,
5928                                                  STRUCT_VALUE_OFFSET));
5929       else
5930         mem = gen_rtx_MEM (Pmode, plus_constant (stack_pointer_rtx,
5931                                                  STRUCT_VALUE_OFFSET));
5932
5933       /* Only follow the SPARC ABI for fixed-size structure returns.
5934          Variable size structure returns are handled per the normal
5935          procedures in GCC. This is enabled by -mstd-struct-return */
5936       if (incoming == 2
5937           && sparc_std_struct_return
5938           && TYPE_SIZE_UNIT (TREE_TYPE (fndecl))
5939           && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (fndecl))) == INTEGER_CST)
5940         {
5941           /* We must check and adjust the return address, as it is
5942              optional as to whether the return object is really
5943              provided.  */
5944           rtx ret_rtx = gen_rtx_REG (Pmode, 31);
5945           rtx scratch = gen_reg_rtx (SImode);
5946           rtx endlab = gen_label_rtx ();
5947
5948           /* Calculate the return object size */
5949           tree size = TYPE_SIZE_UNIT (TREE_TYPE (fndecl));
5950           rtx size_rtx = GEN_INT (TREE_INT_CST_LOW (size) & 0xfff);
5951           /* Construct a temporary return value */
5952           rtx temp_val = assign_stack_local (Pmode, TREE_INT_CST_LOW (size), 0);
5953
5954           /* Implement SPARC 32-bit psABI callee returns struck checking
5955              requirements:
5956
5957               Fetch the instruction where we will return to and see if
5958              it's an unimp instruction (the most significant 10 bits
5959              will be zero).  */
5960           emit_move_insn (scratch, gen_rtx_MEM (SImode,
5961                                                 plus_constant (ret_rtx, 8)));
5962           /* Assume the size is valid and pre-adjust */
5963           emit_insn (gen_add3_insn (ret_rtx, ret_rtx, GEN_INT (4)));
5964           emit_cmp_and_jump_insns (scratch, size_rtx, EQ, const0_rtx, SImode, 0, endlab);
5965           emit_insn (gen_sub3_insn (ret_rtx, ret_rtx, GEN_INT (4)));
5966           /* Assign stack temp:
5967              Write the address of the memory pointed to by temp_val into
5968              the memory pointed to by mem */
5969           emit_move_insn (mem, XEXP (temp_val, 0));
5970           emit_label (endlab);
5971         }
5972
5973       set_mem_alias_set (mem, struct_value_alias_set);
5974       return mem;
5975     }
5976 }
5977
5978 /* Handle TARGET_FUNCTION_VALUE, and TARGET_LIBCALL_VALUE target hook.
5979    For v9, function return values are subject to the same rules as arguments,
5980    except that up to 32 bytes may be returned in registers.  */
5981
5982 static rtx
5983 sparc_function_value_1 (const_tree type, enum machine_mode mode,
5984                         bool outgoing)
5985 {
5986   /* Beware that the two values are swapped here wrt function_arg.  */
5987   int regbase = (outgoing
5988                  ? SPARC_INCOMING_INT_ARG_FIRST
5989                  : SPARC_OUTGOING_INT_ARG_FIRST);
5990   enum mode_class mclass = GET_MODE_CLASS (mode);
5991   int regno;
5992
5993   /* Vector types deserve special treatment because they are polymorphic wrt
5994      their mode, depending upon whether VIS instructions are enabled.  */
5995   if (type && TREE_CODE (type) == VECTOR_TYPE)
5996     {
5997       HOST_WIDE_INT size = int_size_in_bytes (type);
5998       gcc_assert ((TARGET_ARCH32 && size <= 8)
5999                   || (TARGET_ARCH64 && size <= 32));
6000
6001       if (mode == BLKmode)
6002         return function_arg_vector_value (size,
6003                                           SPARC_FP_ARG_FIRST);
6004       else
6005         mclass = MODE_FLOAT;
6006     }
6007
6008   if (TARGET_ARCH64 && type)
6009     {
6010       /* Structures up to 32 bytes in size are returned in registers.  */
6011       if (TREE_CODE (type) == RECORD_TYPE)
6012         {
6013           HOST_WIDE_INT size = int_size_in_bytes (type);
6014           gcc_assert (size <= 32);
6015
6016           return function_arg_record_value (type, mode, 0, 1, regbase);
6017         }
6018
6019       /* Unions up to 32 bytes in size are returned in integer registers.  */
6020       else if (TREE_CODE (type) == UNION_TYPE)
6021         {
6022           HOST_WIDE_INT size = int_size_in_bytes (type);
6023           gcc_assert (size <= 32);
6024
6025           return function_arg_union_value (size, mode, 0, regbase);
6026         }
6027
6028       /* Objects that require it are returned in FP registers.  */
6029       else if (mclass == MODE_FLOAT || mclass == MODE_COMPLEX_FLOAT)
6030         ;
6031
6032       /* All other aggregate types are returned in an integer register in a
6033          mode corresponding to the size of the type.  */
6034       else if (AGGREGATE_TYPE_P (type))
6035         {
6036           /* All other aggregate types are passed in an integer register
6037              in a mode corresponding to the size of the type.  */
6038           HOST_WIDE_INT size = int_size_in_bytes (type);
6039           gcc_assert (size <= 32);
6040
6041           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
6042
6043           /* ??? We probably should have made the same ABI change in
6044              3.4.0 as the one we made for unions.   The latter was
6045              required by the SCD though, while the former is not
6046              specified, so we favored compatibility and efficiency.
6047
6048              Now we're stuck for aggregates larger than 16 bytes,
6049              because OImode vanished in the meantime.  Let's not
6050              try to be unduly clever, and simply follow the ABI
6051              for unions in that case.  */
6052           if (mode == BLKmode)
6053             return function_arg_union_value (size, mode, 0, regbase);
6054           else
6055             mclass = MODE_INT;
6056         }
6057
6058       /* This must match sparc_promote_function_mode.
6059          ??? Maybe 32-bit pointers should actually remain in Pmode?  */
6060       else if (mclass == MODE_INT && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
6061         mode = word_mode;
6062     }
6063
6064   if ((mclass == MODE_FLOAT || mclass == MODE_COMPLEX_FLOAT) && TARGET_FPU)
6065     regno = SPARC_FP_ARG_FIRST;
6066   else
6067     regno = regbase;
6068
6069   return gen_rtx_REG (mode, regno);
6070 }
6071
6072 /* Handle TARGET_FUNCTION_VALUE.
6073
6074    On SPARC the value is found in the first "output" register, but the called
6075    function leaves it in the first "input" register.  */
6076
6077 static rtx
6078 sparc_function_value (const_tree valtype,
6079                       const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
6080                       bool outgoing)
6081 {
6082   return sparc_function_value_1 (valtype, TYPE_MODE (valtype), outgoing);
6083 }
6084
6085 /* Handle TARGET_LIBCALL_VALUE.  */
6086
6087 static rtx
6088 sparc_libcall_value (enum machine_mode mode,
6089                      const_rtx fun ATTRIBUTE_UNUSED)
6090 {
6091   return sparc_function_value_1 (NULL_TREE, mode, false);
6092 }
6093
6094 /* Handle FUNCTION_VALUE_REGNO_P.  
6095    On SPARC, the first "output" reg is used for integer values, and
6096    the first floating point register is used for floating point values.  */
6097
6098 static bool
6099 sparc_function_value_regno_p (const unsigned int regno)
6100 {
6101   return (regno == 8 || regno == 32);
6102 }
6103
6104 /* Do what is necessary for `va_start'.  We look at the current function
6105    to determine if stdarg or varargs is used and return the address of
6106    the first unnamed parameter.  */
6107
6108 static rtx
6109 sparc_builtin_saveregs (void)
6110 {
6111   int first_reg = crtl->args.info.words;
6112   rtx address;
6113   int regno;
6114
6115   for (regno = first_reg; regno < SPARC_INT_ARG_MAX; regno++)
6116     emit_move_insn (gen_rtx_MEM (word_mode,
6117                                  gen_rtx_PLUS (Pmode,
6118                                                frame_pointer_rtx,
6119                                                GEN_INT (FIRST_PARM_OFFSET (0)
6120                                                         + (UNITS_PER_WORD
6121                                                            * regno)))),
6122                     gen_rtx_REG (word_mode,
6123                                  SPARC_INCOMING_INT_ARG_FIRST + regno));
6124
6125   address = gen_rtx_PLUS (Pmode,
6126                           frame_pointer_rtx,
6127                           GEN_INT (FIRST_PARM_OFFSET (0)
6128                                    + UNITS_PER_WORD * first_reg));
6129
6130   return address;
6131 }
6132
6133 /* Implement `va_start' for stdarg.  */
6134
6135 static void
6136 sparc_va_start (tree valist, rtx nextarg)
6137 {
6138   nextarg = expand_builtin_saveregs ();
6139   std_expand_builtin_va_start (valist, nextarg);
6140 }
6141
6142 /* Implement `va_arg' for stdarg.  */
6143
6144 static tree
6145 sparc_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
6146                        gimple_seq *post_p)
6147 {
6148   HOST_WIDE_INT size, rsize, align;
6149   tree addr, incr;
6150   bool indirect;
6151   tree ptrtype = build_pointer_type (type);
6152
6153   if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
6154     {
6155       indirect = true;
6156       size = rsize = UNITS_PER_WORD;
6157       align = 0;
6158     }
6159   else
6160     {
6161       indirect = false;
6162       size = int_size_in_bytes (type);
6163       rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
6164       align = 0;
6165
6166       if (TARGET_ARCH64)
6167         {
6168           /* For SPARC64, objects requiring 16-byte alignment get it.  */
6169           if (TYPE_ALIGN (type) >= 2 * (unsigned) BITS_PER_WORD)
6170             align = 2 * UNITS_PER_WORD;
6171
6172           /* SPARC-V9 ABI states that structures up to 16 bytes in size
6173              are left-justified in their slots.  */
6174           if (AGGREGATE_TYPE_P (type))
6175             {
6176               if (size == 0)
6177                 size = rsize = UNITS_PER_WORD;
6178               else
6179                 size = rsize;
6180             }
6181         }
6182     }
6183
6184   incr = valist;
6185   if (align)
6186     {
6187       incr = fold_build2 (POINTER_PLUS_EXPR, ptr_type_node, incr,
6188                           size_int (align - 1));
6189       incr = fold_convert (sizetype, incr);
6190       incr = fold_build2 (BIT_AND_EXPR, sizetype, incr,
6191                           size_int (-align));
6192       incr = fold_convert (ptr_type_node, incr);
6193     }
6194
6195   gimplify_expr (&incr, pre_p, post_p, is_gimple_val, fb_rvalue);
6196   addr = incr;
6197
6198   if (BYTES_BIG_ENDIAN && size < rsize)
6199     addr = fold_build2 (POINTER_PLUS_EXPR, ptr_type_node, incr,
6200                         size_int (rsize - size));
6201
6202   if (indirect)
6203     {
6204       addr = fold_convert (build_pointer_type (ptrtype), addr);
6205       addr = build_va_arg_indirect_ref (addr);
6206     }
6207
6208   /* If the address isn't aligned properly for the type, we need a temporary.
6209      FIXME: This is inefficient, usually we can do this in registers.  */
6210   else if (align == 0 && TYPE_ALIGN (type) > BITS_PER_WORD)
6211     {
6212       tree tmp = create_tmp_var (type, "va_arg_tmp");
6213       tree dest_addr = build_fold_addr_expr (tmp);
6214       tree copy = build_call_expr (implicit_built_in_decls[BUILT_IN_MEMCPY],
6215                                    3, dest_addr, addr, size_int (rsize));
6216       TREE_ADDRESSABLE (tmp) = 1;
6217       gimplify_and_add (copy, pre_p);
6218       addr = dest_addr;
6219     }
6220
6221   else
6222     addr = fold_convert (ptrtype, addr);
6223
6224   incr
6225     = fold_build2 (POINTER_PLUS_EXPR, ptr_type_node, incr, size_int (rsize));
6226   gimplify_assign (valist, incr, post_p);
6227
6228   return build_va_arg_indirect_ref (addr);
6229 }
6230 \f
6231 /* Implement the TARGET_VECTOR_MODE_SUPPORTED_P target hook.
6232    Specify whether the vector mode is supported by the hardware.  */
6233
6234 static bool
6235 sparc_vector_mode_supported_p (enum machine_mode mode)
6236 {
6237   return TARGET_VIS && VECTOR_MODE_P (mode) ? true : false;
6238 }
6239 \f
6240 /* Implement the TARGET_VECTORIZE_UNITS_PER_SIMD_WORD target hook.  */
6241
6242 static bool
6243 sparc_units_per_simd_word (enum machine_mode mode ATTRIBUTE_UNUSED)
6244 {
6245   return TARGET_VIS ? 8 : UNITS_PER_WORD;
6246 }
6247 \f
6248 /* Return the string to output an unconditional branch to LABEL, which is
6249    the operand number of the label.
6250
6251    DEST is the destination insn (i.e. the label), INSN is the source.  */
6252
6253 const char *
6254 output_ubranch (rtx dest, int label, rtx insn)
6255 {
6256   static char string[64];
6257   bool v9_form = false;
6258   char *p;
6259
6260   if (TARGET_V9 && INSN_ADDRESSES_SET_P ())
6261     {
6262       int delta = (INSN_ADDRESSES (INSN_UID (dest))
6263                    - INSN_ADDRESSES (INSN_UID (insn)));
6264       /* Leave some instructions for "slop".  */
6265       if (delta >= -260000 && delta < 260000)
6266         v9_form = true;
6267     }
6268
6269   if (v9_form)
6270     strcpy (string, "ba%*,pt\t%%xcc, ");
6271   else
6272     strcpy (string, "b%*\t");
6273
6274   p = strchr (string, '\0');
6275   *p++ = '%';
6276   *p++ = 'l';
6277   *p++ = '0' + label;
6278   *p++ = '%';
6279   *p++ = '(';
6280   *p = '\0';
6281
6282   return string;
6283 }
6284
6285 /* Return the string to output a conditional branch to LABEL, which is
6286    the operand number of the label.  OP is the conditional expression.
6287    XEXP (OP, 0) is assumed to be a condition code register (integer or
6288    floating point) and its mode specifies what kind of comparison we made.
6289
6290    DEST is the destination insn (i.e. the label), INSN is the source.
6291
6292    REVERSED is nonzero if we should reverse the sense of the comparison.
6293
6294    ANNUL is nonzero if we should generate an annulling branch.  */
6295
6296 const char *
6297 output_cbranch (rtx op, rtx dest, int label, int reversed, int annul,
6298                 rtx insn)
6299 {
6300   static char string[64];
6301   enum rtx_code code = GET_CODE (op);
6302   rtx cc_reg = XEXP (op, 0);
6303   enum machine_mode mode = GET_MODE (cc_reg);
6304   const char *labelno, *branch;
6305   int spaces = 8, far;
6306   char *p;
6307
6308   /* v9 branches are limited to +-1MB.  If it is too far away,
6309      change
6310
6311      bne,pt %xcc, .LC30
6312
6313      to
6314
6315      be,pn %xcc, .+12
6316       nop
6317      ba .LC30
6318
6319      and
6320
6321      fbne,a,pn %fcc2, .LC29
6322
6323      to
6324
6325      fbe,pt %fcc2, .+16
6326       nop
6327      ba .LC29  */
6328
6329   far = TARGET_V9 && (get_attr_length (insn) >= 3);
6330   if (reversed ^ far)
6331     {
6332       /* Reversal of FP compares takes care -- an ordered compare
6333          becomes an unordered compare and vice versa.  */
6334       if (mode == CCFPmode || mode == CCFPEmode)
6335         code = reverse_condition_maybe_unordered (code);
6336       else
6337         code = reverse_condition (code);
6338     }
6339
6340   /* Start by writing the branch condition.  */
6341   if (mode == CCFPmode || mode == CCFPEmode)
6342     {
6343       switch (code)
6344         {
6345         case NE:
6346           branch = "fbne";
6347           break;
6348         case EQ:
6349           branch = "fbe";
6350           break;
6351         case GE:
6352           branch = "fbge";
6353           break;
6354         case GT:
6355           branch = "fbg";
6356           break;
6357         case LE:
6358           branch = "fble";
6359           break;
6360         case LT:
6361           branch = "fbl";
6362           break;
6363         case UNORDERED:
6364           branch = "fbu";
6365           break;
6366         case ORDERED:
6367           branch = "fbo";
6368           break;
6369         case UNGT:
6370           branch = "fbug";
6371           break;
6372         case UNLT:
6373           branch = "fbul";
6374           break;
6375         case UNEQ:
6376           branch = "fbue";
6377           break;
6378         case UNGE:
6379           branch = "fbuge";
6380           break;
6381         case UNLE:
6382           branch = "fbule";
6383           break;
6384         case LTGT:
6385           branch = "fblg";
6386           break;
6387
6388         default:
6389           gcc_unreachable ();
6390         }
6391
6392       /* ??? !v9: FP branches cannot be preceded by another floating point
6393          insn.  Because there is currently no concept of pre-delay slots,
6394          we can fix this only by always emitting a nop before a floating
6395          point branch.  */
6396
6397       string[0] = '\0';
6398       if (! TARGET_V9)
6399         strcpy (string, "nop\n\t");
6400       strcat (string, branch);
6401     }
6402   else
6403     {
6404       switch (code)
6405         {
6406         case NE:
6407           branch = "bne";
6408           break;
6409         case EQ:
6410           branch = "be";
6411           break;
6412         case GE:
6413           if (mode == CC_NOOVmode || mode == CCX_NOOVmode)
6414             branch = "bpos";
6415           else
6416             branch = "bge";
6417           break;
6418         case GT:
6419           branch = "bg";
6420           break;
6421         case LE:
6422           branch = "ble";
6423           break;
6424         case LT:
6425           if (mode == CC_NOOVmode || mode == CCX_NOOVmode)
6426             branch = "bneg";
6427           else
6428             branch = "bl";
6429           break;
6430         case GEU:
6431           branch = "bgeu";
6432           break;
6433         case GTU:
6434           branch = "bgu";
6435           break;
6436         case LEU:
6437           branch = "bleu";
6438           break;
6439         case LTU:
6440           branch = "blu";
6441           break;
6442
6443         default:
6444           gcc_unreachable ();
6445         }
6446       strcpy (string, branch);
6447     }
6448   spaces -= strlen (branch);
6449   p = strchr (string, '\0');
6450
6451   /* Now add the annulling, the label, and a possible noop.  */
6452   if (annul && ! far)
6453     {
6454       strcpy (p, ",a");
6455       p += 2;
6456       spaces -= 2;
6457     }
6458
6459   if (TARGET_V9)
6460     {
6461       rtx note;
6462       int v8 = 0;
6463
6464       if (! far && insn && INSN_ADDRESSES_SET_P ())
6465         {
6466           int delta = (INSN_ADDRESSES (INSN_UID (dest))
6467                        - INSN_ADDRESSES (INSN_UID (insn)));
6468           /* Leave some instructions for "slop".  */
6469           if (delta < -260000 || delta >= 260000)
6470             v8 = 1;
6471         }
6472
6473       if (mode == CCFPmode || mode == CCFPEmode)
6474         {
6475           static char v9_fcc_labelno[] = "%%fccX, ";
6476           /* Set the char indicating the number of the fcc reg to use.  */
6477           v9_fcc_labelno[5] = REGNO (cc_reg) - SPARC_FIRST_V9_FCC_REG + '0';
6478           labelno = v9_fcc_labelno;
6479           if (v8)
6480             {
6481               gcc_assert (REGNO (cc_reg) == SPARC_FCC_REG);
6482               labelno = "";
6483             }
6484         }
6485       else if (mode == CCXmode || mode == CCX_NOOVmode)
6486         {
6487           labelno = "%%xcc, ";
6488           gcc_assert (! v8);
6489         }
6490       else
6491         {
6492           labelno = "%%icc, ";
6493           if (v8)
6494             labelno = "";
6495         }
6496
6497       if (*labelno && insn && (note = find_reg_note (insn, REG_BR_PROB, NULL_RTX)))
6498         {
6499           strcpy (p,
6500                   ((INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2) ^ far)
6501                   ? ",pt" : ",pn");
6502           p += 3;
6503           spaces -= 3;
6504         }
6505     }
6506   else
6507     labelno = "";
6508
6509   if (spaces > 0)
6510     *p++ = '\t';
6511   else
6512     *p++ = ' ';
6513   strcpy (p, labelno);
6514   p = strchr (p, '\0');
6515   if (far)
6516     {
6517       strcpy (p, ".+12\n\t nop\n\tb\t");
6518       /* Skip the next insn if requested or
6519          if we know that it will be a nop.  */
6520       if (annul || ! final_sequence)
6521         p[3] = '6';
6522       p += 14;
6523     }
6524   *p++ = '%';
6525   *p++ = 'l';
6526   *p++ = label + '0';
6527   *p++ = '%';
6528   *p++ = '#';
6529   *p = '\0';
6530
6531   return string;
6532 }
6533
6534 /* Emit a library call comparison between floating point X and Y.
6535    COMPARISON is the operator to compare with (EQ, NE, GT, etc).
6536    Return the new operator to be used in the comparison sequence.
6537
6538    TARGET_ARCH64 uses _Qp_* functions, which use pointers to TFmode
6539    values as arguments instead of the TFmode registers themselves,
6540    that's why we cannot call emit_float_lib_cmp.  */
6541
6542 rtx
6543 sparc_emit_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison)
6544 {
6545   const char *qpfunc;
6546   rtx slot0, slot1, result, tem, tem2, libfunc;
6547   enum machine_mode mode;
6548   enum rtx_code new_comparison;
6549
6550   switch (comparison)
6551     {
6552     case EQ:
6553       qpfunc = (TARGET_ARCH64 ? "_Qp_feq" : "_Q_feq");
6554       break;
6555
6556     case NE:
6557       qpfunc = (TARGET_ARCH64 ? "_Qp_fne" : "_Q_fne");
6558       break;
6559
6560     case GT:
6561       qpfunc = (TARGET_ARCH64 ? "_Qp_fgt" : "_Q_fgt");
6562       break;
6563
6564     case GE:
6565       qpfunc = (TARGET_ARCH64 ? "_Qp_fge" : "_Q_fge");
6566       break;
6567
6568     case LT:
6569       qpfunc = (TARGET_ARCH64 ? "_Qp_flt" : "_Q_flt");
6570       break;
6571
6572     case LE:
6573       qpfunc = (TARGET_ARCH64 ? "_Qp_fle" : "_Q_fle");
6574       break;
6575
6576     case ORDERED:
6577     case UNORDERED:
6578     case UNGT:
6579     case UNLT:
6580     case UNEQ:
6581     case UNGE:
6582     case UNLE:
6583     case LTGT:
6584       qpfunc = (TARGET_ARCH64 ? "_Qp_cmp" : "_Q_cmp");
6585       break;
6586
6587     default:
6588       gcc_unreachable ();
6589     }
6590
6591   if (TARGET_ARCH64)
6592     {
6593       if (MEM_P (x))
6594         slot0 = x;
6595       else
6596         {
6597           slot0 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode), 0);
6598           emit_move_insn (slot0, x);
6599         }
6600
6601       if (MEM_P (y))
6602         slot1 = y;
6603       else
6604         {
6605           slot1 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode), 0);
6606           emit_move_insn (slot1, y);
6607         }
6608
6609       libfunc = gen_rtx_SYMBOL_REF (Pmode, qpfunc);
6610       emit_library_call (libfunc, LCT_NORMAL,
6611                          DImode, 2,
6612                          XEXP (slot0, 0), Pmode,
6613                          XEXP (slot1, 0), Pmode);
6614       mode = DImode;
6615     }
6616   else
6617     {
6618       libfunc = gen_rtx_SYMBOL_REF (Pmode, qpfunc);
6619       emit_library_call (libfunc, LCT_NORMAL,
6620                          SImode, 2,
6621                          x, TFmode, y, TFmode);
6622       mode = SImode;
6623     }
6624
6625
6626   /* Immediately move the result of the libcall into a pseudo
6627      register so reload doesn't clobber the value if it needs
6628      the return register for a spill reg.  */
6629   result = gen_reg_rtx (mode);
6630   emit_move_insn (result, hard_libcall_value (mode, libfunc));
6631
6632   switch (comparison)
6633     {
6634     default:
6635       return gen_rtx_NE (VOIDmode, result, const0_rtx);
6636     case ORDERED:
6637     case UNORDERED:
6638       new_comparison = (comparison == UNORDERED ? EQ : NE);
6639       return gen_rtx_fmt_ee (new_comparison, VOIDmode, result, GEN_INT(3));
6640     case UNGT:
6641     case UNGE:
6642       new_comparison = (comparison == UNGT ? GT : NE);
6643       return gen_rtx_fmt_ee (new_comparison, VOIDmode, result, const1_rtx);
6644     case UNLE:
6645       return gen_rtx_NE (VOIDmode, result, const2_rtx);
6646     case UNLT:
6647       tem = gen_reg_rtx (mode);
6648       if (TARGET_ARCH32)
6649         emit_insn (gen_andsi3 (tem, result, const1_rtx));
6650       else
6651         emit_insn (gen_anddi3 (tem, result, const1_rtx));
6652       return gen_rtx_NE (VOIDmode, tem, const0_rtx);
6653     case UNEQ:
6654     case LTGT:
6655       tem = gen_reg_rtx (mode);
6656       if (TARGET_ARCH32)
6657         emit_insn (gen_addsi3 (tem, result, const1_rtx));
6658       else
6659         emit_insn (gen_adddi3 (tem, result, const1_rtx));
6660       tem2 = gen_reg_rtx (mode);
6661       if (TARGET_ARCH32)
6662         emit_insn (gen_andsi3 (tem2, tem, const2_rtx));
6663       else
6664         emit_insn (gen_anddi3 (tem2, tem, const2_rtx));
6665       new_comparison = (comparison == UNEQ ? EQ : NE);
6666       return gen_rtx_fmt_ee (new_comparison, VOIDmode, tem2, const0_rtx);
6667     }
6668
6669   gcc_unreachable ();
6670 }
6671
6672 /* Generate an unsigned DImode to FP conversion.  This is the same code
6673    optabs would emit if we didn't have TFmode patterns.  */
6674
6675 void
6676 sparc_emit_floatunsdi (rtx *operands, enum machine_mode mode)
6677 {
6678   rtx neglab, donelab, i0, i1, f0, in, out;
6679
6680   out = operands[0];
6681   in = force_reg (DImode, operands[1]);
6682   neglab = gen_label_rtx ();
6683   donelab = gen_label_rtx ();
6684   i0 = gen_reg_rtx (DImode);
6685   i1 = gen_reg_rtx (DImode);
6686   f0 = gen_reg_rtx (mode);
6687
6688   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, DImode, 0, neglab);
6689
6690   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
6691   emit_jump_insn (gen_jump (donelab));
6692   emit_barrier ();
6693
6694   emit_label (neglab);
6695
6696   emit_insn (gen_lshrdi3 (i0, in, const1_rtx));
6697   emit_insn (gen_anddi3 (i1, in, const1_rtx));
6698   emit_insn (gen_iordi3 (i0, i0, i1));
6699   emit_insn (gen_rtx_SET (VOIDmode, f0, gen_rtx_FLOAT (mode, i0)));
6700   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
6701
6702   emit_label (donelab);
6703 }
6704
6705 /* Generate an FP to unsigned DImode conversion.  This is the same code
6706    optabs would emit if we didn't have TFmode patterns.  */
6707
6708 void
6709 sparc_emit_fixunsdi (rtx *operands, enum machine_mode mode)
6710 {
6711   rtx neglab, donelab, i0, i1, f0, in, out, limit;
6712
6713   out = operands[0];
6714   in = force_reg (mode, operands[1]);
6715   neglab = gen_label_rtx ();
6716   donelab = gen_label_rtx ();
6717   i0 = gen_reg_rtx (DImode);
6718   i1 = gen_reg_rtx (DImode);
6719   limit = gen_reg_rtx (mode);
6720   f0 = gen_reg_rtx (mode);
6721
6722   emit_move_insn (limit,
6723                   CONST_DOUBLE_FROM_REAL_VALUE (
6724                     REAL_VALUE_ATOF ("9223372036854775808.0", mode), mode));
6725   emit_cmp_and_jump_insns (in, limit, GE, NULL_RTX, mode, 0, neglab);
6726
6727   emit_insn (gen_rtx_SET (VOIDmode,
6728                           out,
6729                           gen_rtx_FIX (DImode, gen_rtx_FIX (mode, in))));
6730   emit_jump_insn (gen_jump (donelab));
6731   emit_barrier ();
6732
6733   emit_label (neglab);
6734
6735   emit_insn (gen_rtx_SET (VOIDmode, f0, gen_rtx_MINUS (mode, in, limit)));
6736   emit_insn (gen_rtx_SET (VOIDmode,
6737                           i0,
6738                           gen_rtx_FIX (DImode, gen_rtx_FIX (mode, f0))));
6739   emit_insn (gen_movdi (i1, const1_rtx));
6740   emit_insn (gen_ashldi3 (i1, i1, GEN_INT (63)));
6741   emit_insn (gen_xordi3 (out, i0, i1));
6742
6743   emit_label (donelab);
6744 }
6745
6746 /* Return the string to output a conditional branch to LABEL, testing
6747    register REG.  LABEL is the operand number of the label; REG is the
6748    operand number of the reg.  OP is the conditional expression.  The mode
6749    of REG says what kind of comparison we made.
6750
6751    DEST is the destination insn (i.e. the label), INSN is the source.
6752
6753    REVERSED is nonzero if we should reverse the sense of the comparison.
6754
6755    ANNUL is nonzero if we should generate an annulling branch.  */
6756
6757 const char *
6758 output_v9branch (rtx op, rtx dest, int reg, int label, int reversed,
6759                  int annul, rtx insn)
6760 {
6761   static char string[64];
6762   enum rtx_code code = GET_CODE (op);
6763   enum machine_mode mode = GET_MODE (XEXP (op, 0));
6764   rtx note;
6765   int far;
6766   char *p;
6767
6768   /* branch on register are limited to +-128KB.  If it is too far away,
6769      change
6770
6771      brnz,pt %g1, .LC30
6772
6773      to
6774
6775      brz,pn %g1, .+12
6776       nop
6777      ba,pt %xcc, .LC30
6778
6779      and
6780
6781      brgez,a,pn %o1, .LC29
6782
6783      to
6784
6785      brlz,pt %o1, .+16
6786       nop
6787      ba,pt %xcc, .LC29  */
6788
6789   far = get_attr_length (insn) >= 3;
6790
6791   /* If not floating-point or if EQ or NE, we can just reverse the code.  */
6792   if (reversed ^ far)
6793     code = reverse_condition (code);
6794
6795   /* Only 64 bit versions of these instructions exist.  */
6796   gcc_assert (mode == DImode);
6797
6798   /* Start by writing the branch condition.  */
6799
6800   switch (code)
6801     {
6802     case NE:
6803       strcpy (string, "brnz");
6804       break;
6805
6806     case EQ:
6807       strcpy (string, "brz");
6808       break;
6809
6810     case GE:
6811       strcpy (string, "brgez");
6812       break;
6813
6814     case LT:
6815       strcpy (string, "brlz");
6816       break;
6817
6818     case LE:
6819       strcpy (string, "brlez");
6820       break;
6821
6822     case GT:
6823       strcpy (string, "brgz");
6824       break;
6825
6826     default:
6827       gcc_unreachable ();
6828     }
6829
6830   p = strchr (string, '\0');
6831
6832   /* Now add the annulling, reg, label, and nop.  */
6833   if (annul && ! far)
6834     {
6835       strcpy (p, ",a");
6836       p += 2;
6837     }
6838
6839   if (insn && (note = find_reg_note (insn, REG_BR_PROB, NULL_RTX)))
6840     {
6841       strcpy (p,
6842               ((INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2) ^ far)
6843               ? ",pt" : ",pn");
6844       p += 3;
6845     }
6846
6847   *p = p < string + 8 ? '\t' : ' ';
6848   p++;
6849   *p++ = '%';
6850   *p++ = '0' + reg;
6851   *p++ = ',';
6852   *p++ = ' ';
6853   if (far)
6854     {
6855       int veryfar = 1, delta;
6856
6857       if (INSN_ADDRESSES_SET_P ())
6858         {
6859           delta = (INSN_ADDRESSES (INSN_UID (dest))
6860                    - INSN_ADDRESSES (INSN_UID (insn)));
6861           /* Leave some instructions for "slop".  */
6862           if (delta >= -260000 && delta < 260000)
6863             veryfar = 0;
6864         }
6865
6866       strcpy (p, ".+12\n\t nop\n\t");
6867       /* Skip the next insn if requested or
6868          if we know that it will be a nop.  */
6869       if (annul || ! final_sequence)
6870         p[3] = '6';
6871       p += 12;
6872       if (veryfar)
6873         {
6874           strcpy (p, "b\t");
6875           p += 2;
6876         }
6877       else
6878         {
6879           strcpy (p, "ba,pt\t%%xcc, ");
6880           p += 13;
6881         }
6882     }
6883   *p++ = '%';
6884   *p++ = 'l';
6885   *p++ = '0' + label;
6886   *p++ = '%';
6887   *p++ = '#';
6888   *p = '\0';
6889
6890   return string;
6891 }
6892
6893 /* Return 1, if any of the registers of the instruction are %l[0-7] or %o[0-7].
6894    Such instructions cannot be used in the delay slot of return insn on v9.
6895    If TEST is 0, also rename all %i[0-7] registers to their %o[0-7] counterparts.
6896  */
6897
6898 static int
6899 epilogue_renumber (register rtx *where, int test)
6900 {
6901   register const char *fmt;
6902   register int i;
6903   register enum rtx_code code;
6904
6905   if (*where == 0)
6906     return 0;
6907
6908   code = GET_CODE (*where);
6909
6910   switch (code)
6911     {
6912     case REG:
6913       if (REGNO (*where) >= 8 && REGNO (*where) < 24)      /* oX or lX */
6914         return 1;
6915       if (! test && REGNO (*where) >= 24 && REGNO (*where) < 32)
6916         *where = gen_rtx_REG (GET_MODE (*where), OUTGOING_REGNO (REGNO(*where)));
6917     case SCRATCH:
6918     case CC0:
6919     case PC:
6920     case CONST_INT:
6921     case CONST_DOUBLE:
6922       return 0;
6923
6924       /* Do not replace the frame pointer with the stack pointer because
6925          it can cause the delayed instruction to load below the stack.
6926          This occurs when instructions like:
6927
6928          (set (reg/i:SI 24 %i0)
6929              (mem/f:SI (plus:SI (reg/f:SI 30 %fp)
6930                        (const_int -20 [0xffffffec])) 0))
6931
6932          are in the return delayed slot.  */
6933     case PLUS:
6934       if (GET_CODE (XEXP (*where, 0)) == REG
6935           && REGNO (XEXP (*where, 0)) == HARD_FRAME_POINTER_REGNUM
6936           && (GET_CODE (XEXP (*where, 1)) != CONST_INT
6937               || INTVAL (XEXP (*where, 1)) < SPARC_STACK_BIAS))
6938         return 1;
6939       break;
6940
6941     case MEM:
6942       if (SPARC_STACK_BIAS
6943           && GET_CODE (XEXP (*where, 0)) == REG
6944           && REGNO (XEXP (*where, 0)) == HARD_FRAME_POINTER_REGNUM)
6945         return 1;
6946       break;
6947
6948     default:
6949       break;
6950     }
6951
6952   fmt = GET_RTX_FORMAT (code);
6953
6954   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6955     {
6956       if (fmt[i] == 'E')
6957         {
6958           register int j;
6959           for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
6960             if (epilogue_renumber (&(XVECEXP (*where, i, j)), test))
6961               return 1;
6962         }
6963       else if (fmt[i] == 'e'
6964                && epilogue_renumber (&(XEXP (*where, i)), test))
6965         return 1;
6966     }
6967   return 0;
6968 }
6969 \f
6970 /* Leaf functions and non-leaf functions have different needs.  */
6971
6972 static const int
6973 reg_leaf_alloc_order[] = REG_LEAF_ALLOC_ORDER;
6974
6975 static const int
6976 reg_nonleaf_alloc_order[] = REG_ALLOC_ORDER;
6977
6978 static const int *const reg_alloc_orders[] = {
6979   reg_leaf_alloc_order,
6980   reg_nonleaf_alloc_order};
6981
6982 void
6983 order_regs_for_local_alloc (void)
6984 {
6985   static int last_order_nonleaf = 1;
6986
6987   if (df_regs_ever_live_p (15) != last_order_nonleaf)
6988     {
6989       last_order_nonleaf = !last_order_nonleaf;
6990       memcpy ((char *) reg_alloc_order,
6991               (const char *) reg_alloc_orders[last_order_nonleaf],
6992               FIRST_PSEUDO_REGISTER * sizeof (int));
6993     }
6994 }
6995 \f
6996 /* Return 1 if REG and MEM are legitimate enough to allow the various
6997    mem<-->reg splits to be run.  */
6998
6999 int
7000 sparc_splitdi_legitimate (rtx reg, rtx mem)
7001 {
7002   /* Punt if we are here by mistake.  */
7003   gcc_assert (reload_completed);
7004
7005   /* We must have an offsettable memory reference.  */
7006   if (! offsettable_memref_p (mem))
7007     return 0;
7008
7009   /* If we have legitimate args for ldd/std, we do not want
7010      the split to happen.  */
7011   if ((REGNO (reg) % 2) == 0
7012       && mem_min_alignment (mem, 8))
7013     return 0;
7014
7015   /* Success.  */
7016   return 1;
7017 }
7018
7019 /* Return 1 if x and y are some kind of REG and they refer to
7020    different hard registers.  This test is guaranteed to be
7021    run after reload.  */
7022
7023 int
7024 sparc_absnegfloat_split_legitimate (rtx x, rtx y)
7025 {
7026   if (GET_CODE (x) != REG)
7027     return 0;
7028   if (GET_CODE (y) != REG)
7029     return 0;
7030   if (REGNO (x) == REGNO (y))
7031     return 0;
7032   return 1;
7033 }
7034
7035 /* Return 1 if REGNO (reg1) is even and REGNO (reg1) == REGNO (reg2) - 1.
7036    This makes them candidates for using ldd and std insns.
7037
7038    Note reg1 and reg2 *must* be hard registers.  */
7039
7040 int
7041 registers_ok_for_ldd_peep (rtx reg1, rtx reg2)
7042 {
7043   /* We might have been passed a SUBREG.  */
7044   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
7045     return 0;
7046
7047   if (REGNO (reg1) % 2 != 0)
7048     return 0;
7049
7050   /* Integer ldd is deprecated in SPARC V9 */
7051   if (TARGET_V9 && REGNO (reg1) < 32)
7052     return 0;
7053
7054   return (REGNO (reg1) == REGNO (reg2) - 1);
7055 }
7056
7057 /* Return 1 if the addresses in mem1 and mem2 are suitable for use in
7058    an ldd or std insn.
7059
7060    This can only happen when addr1 and addr2, the addresses in mem1
7061    and mem2, are consecutive memory locations (addr1 + 4 == addr2).
7062    addr1 must also be aligned on a 64-bit boundary.
7063
7064    Also iff dependent_reg_rtx is not null it should not be used to
7065    compute the address for mem1, i.e. we cannot optimize a sequence
7066    like:
7067         ld [%o0], %o0
7068         ld [%o0 + 4], %o1
7069    to
7070         ldd [%o0], %o0
7071    nor:
7072         ld [%g3 + 4], %g3
7073         ld [%g3], %g2
7074    to
7075         ldd [%g3], %g2
7076
7077    But, note that the transformation from:
7078         ld [%g2 + 4], %g3
7079         ld [%g2], %g2
7080    to
7081         ldd [%g2], %g2
7082    is perfectly fine.  Thus, the peephole2 patterns always pass us
7083    the destination register of the first load, never the second one.
7084
7085    For stores we don't have a similar problem, so dependent_reg_rtx is
7086    NULL_RTX.  */
7087
7088 int
7089 mems_ok_for_ldd_peep (rtx mem1, rtx mem2, rtx dependent_reg_rtx)
7090 {
7091   rtx addr1, addr2;
7092   unsigned int reg1;
7093   HOST_WIDE_INT offset1;
7094
7095   /* The mems cannot be volatile.  */
7096   if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
7097     return 0;
7098
7099   /* MEM1 should be aligned on a 64-bit boundary.  */
7100   if (MEM_ALIGN (mem1) < 64)
7101     return 0;
7102
7103   addr1 = XEXP (mem1, 0);
7104   addr2 = XEXP (mem2, 0);
7105
7106   /* Extract a register number and offset (if used) from the first addr.  */
7107   if (GET_CODE (addr1) == PLUS)
7108     {
7109       /* If not a REG, return zero.  */
7110       if (GET_CODE (XEXP (addr1, 0)) != REG)
7111         return 0;
7112       else
7113         {
7114           reg1 = REGNO (XEXP (addr1, 0));
7115           /* The offset must be constant!  */
7116           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
7117             return 0;
7118           offset1 = INTVAL (XEXP (addr1, 1));
7119         }
7120     }
7121   else if (GET_CODE (addr1) != REG)
7122     return 0;
7123   else
7124     {
7125       reg1 = REGNO (addr1);
7126       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
7127       offset1 = 0;
7128     }
7129
7130   /* Make sure the second address is a (mem (plus (reg) (const_int).  */
7131   if (GET_CODE (addr2) != PLUS)
7132     return 0;
7133
7134   if (GET_CODE (XEXP (addr2, 0)) != REG
7135       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
7136     return 0;
7137
7138   if (reg1 != REGNO (XEXP (addr2, 0)))
7139     return 0;
7140
7141   if (dependent_reg_rtx != NULL_RTX && reg1 == REGNO (dependent_reg_rtx))
7142     return 0;
7143
7144   /* The first offset must be evenly divisible by 8 to ensure the
7145      address is 64 bit aligned.  */
7146   if (offset1 % 8 != 0)
7147     return 0;
7148
7149   /* The offset for the second addr must be 4 more than the first addr.  */
7150   if (INTVAL (XEXP (addr2, 1)) != offset1 + 4)
7151     return 0;
7152
7153   /* All the tests passed.  addr1 and addr2 are valid for ldd and std
7154      instructions.  */
7155   return 1;
7156 }
7157
7158 /* Return 1 if reg is a pseudo, or is the first register in
7159    a hard register pair.  This makes it suitable for use in
7160    ldd and std insns.  */
7161
7162 int
7163 register_ok_for_ldd (rtx reg)
7164 {
7165   /* We might have been passed a SUBREG.  */
7166   if (!REG_P (reg))
7167     return 0;
7168
7169   if (REGNO (reg) < FIRST_PSEUDO_REGISTER)
7170     return (REGNO (reg) % 2 == 0);
7171
7172   return 1;
7173 }
7174
7175 /* Return 1 if OP is a memory whose address is known to be
7176    aligned to 8-byte boundary, or a pseudo during reload.
7177    This makes it suitable for use in ldd and std insns.  */
7178
7179 int
7180 memory_ok_for_ldd (rtx op)
7181 {
7182   if (MEM_P (op))
7183     {
7184       /* In 64-bit mode, we assume that the address is word-aligned.  */
7185       if (TARGET_ARCH32 && !mem_min_alignment (op, 8))
7186         return 0;
7187
7188       if ((reload_in_progress || reload_completed)
7189           && !strict_memory_address_p (Pmode, XEXP (op, 0)))
7190         return 0;
7191     }
7192   else if (REG_P (op) && REGNO (op) >= FIRST_PSEUDO_REGISTER)
7193     {
7194       if (!(reload_in_progress && reg_renumber [REGNO (op)] < 0))
7195         return 0;
7196     }
7197   else
7198     return 0;
7199
7200   return 1;
7201 }
7202 \f
7203 /* Print operand X (an rtx) in assembler syntax to file FILE.
7204    CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
7205    For `%' followed by punctuation, CODE is the punctuation and X is null.  */
7206
7207 void
7208 print_operand (FILE *file, rtx x, int code)
7209 {
7210   switch (code)
7211     {
7212     case '#':
7213       /* Output an insn in a delay slot.  */
7214       if (final_sequence)
7215         sparc_indent_opcode = 1;
7216       else
7217         fputs ("\n\t nop", file);
7218       return;
7219     case '*':
7220       /* Output an annul flag if there's nothing for the delay slot and we
7221          are optimizing.  This is always used with '(' below.
7222          Sun OS 4.1.1 dbx can't handle an annulled unconditional branch;
7223          this is a dbx bug.  So, we only do this when optimizing.
7224          On UltraSPARC, a branch in a delay slot causes a pipeline flush.
7225          Always emit a nop in case the next instruction is a branch.  */
7226       if (! final_sequence && (optimize && (int)sparc_cpu < PROCESSOR_V9))
7227         fputs (",a", file);
7228       return;
7229     case '(':
7230       /* Output a 'nop' if there's nothing for the delay slot and we are
7231          not optimizing.  This is always used with '*' above.  */
7232       if (! final_sequence && ! (optimize && (int)sparc_cpu < PROCESSOR_V9))
7233         fputs ("\n\t nop", file);
7234       else if (final_sequence)
7235         sparc_indent_opcode = 1;
7236       return;
7237     case ')':
7238       /* Output the right displacement from the saved PC on function return.
7239          The caller may have placed an "unimp" insn immediately after the call
7240          so we have to account for it.  This insn is used in the 32-bit ABI
7241          when calling a function that returns a non zero-sized structure.  The
7242          64-bit ABI doesn't have it.  Be careful to have this test be the same
7243          as that for the call.  The exception is when sparc_std_struct_return
7244          is enabled, the psABI is followed exactly and the adjustment is made
7245          by the code in sparc_struct_value_rtx.  The call emitted is the same
7246          when sparc_std_struct_return is enabled. */
7247      if (!TARGET_ARCH64
7248          && cfun->returns_struct
7249          && !sparc_std_struct_return
7250          && DECL_SIZE (DECL_RESULT (current_function_decl))
7251          && TREE_CODE (DECL_SIZE (DECL_RESULT (current_function_decl)))
7252              == INTEGER_CST
7253          && !integer_zerop (DECL_SIZE (DECL_RESULT (current_function_decl))))
7254         fputs ("12", file);
7255       else
7256         fputc ('8', file);
7257       return;
7258     case '_':
7259       /* Output the Embedded Medium/Anywhere code model base register.  */
7260       fputs (EMBMEDANY_BASE_REG, file);
7261       return;
7262     case '&':
7263       /* Print some local dynamic TLS name.  */
7264       assemble_name (file, get_some_local_dynamic_name ());
7265       return;
7266
7267     case 'Y':
7268       /* Adjust the operand to take into account a RESTORE operation.  */
7269       if (GET_CODE (x) == CONST_INT)
7270         break;
7271       else if (GET_CODE (x) != REG)
7272         output_operand_lossage ("invalid %%Y operand");
7273       else if (REGNO (x) < 8)
7274         fputs (reg_names[REGNO (x)], file);
7275       else if (REGNO (x) >= 24 && REGNO (x) < 32)
7276         fputs (reg_names[REGNO (x)-16], file);
7277       else
7278         output_operand_lossage ("invalid %%Y operand");
7279       return;
7280     case 'L':
7281       /* Print out the low order register name of a register pair.  */
7282       if (WORDS_BIG_ENDIAN)
7283         fputs (reg_names[REGNO (x)+1], file);
7284       else
7285         fputs (reg_names[REGNO (x)], file);
7286       return;
7287     case 'H':
7288       /* Print out the high order register name of a register pair.  */
7289       if (WORDS_BIG_ENDIAN)
7290         fputs (reg_names[REGNO (x)], file);
7291       else
7292         fputs (reg_names[REGNO (x)+1], file);
7293       return;
7294     case 'R':
7295       /* Print out the second register name of a register pair or quad.
7296          I.e., R (%o0) => %o1.  */
7297       fputs (reg_names[REGNO (x)+1], file);
7298       return;
7299     case 'S':
7300       /* Print out the third register name of a register quad.
7301          I.e., S (%o0) => %o2.  */
7302       fputs (reg_names[REGNO (x)+2], file);
7303       return;
7304     case 'T':
7305       /* Print out the fourth register name of a register quad.
7306          I.e., T (%o0) => %o3.  */
7307       fputs (reg_names[REGNO (x)+3], file);
7308       return;
7309     case 'x':
7310       /* Print a condition code register.  */
7311       if (REGNO (x) == SPARC_ICC_REG)
7312         {
7313           /* We don't handle CC[X]_NOOVmode because they're not supposed
7314              to occur here.  */
7315           if (GET_MODE (x) == CCmode)
7316             fputs ("%icc", file);
7317           else if (GET_MODE (x) == CCXmode)
7318             fputs ("%xcc", file);
7319           else
7320             gcc_unreachable ();
7321         }
7322       else
7323         /* %fccN register */
7324         fputs (reg_names[REGNO (x)], file);
7325       return;
7326     case 'm':
7327       /* Print the operand's address only.  */
7328       output_address (XEXP (x, 0));
7329       return;
7330     case 'r':
7331       /* In this case we need a register.  Use %g0 if the
7332          operand is const0_rtx.  */
7333       if (x == const0_rtx
7334           || (GET_MODE (x) != VOIDmode && x == CONST0_RTX (GET_MODE (x))))
7335         {
7336           fputs ("%g0", file);
7337           return;
7338         }
7339       else
7340         break;
7341
7342     case 'A':
7343       switch (GET_CODE (x))
7344         {
7345         case IOR: fputs ("or", file); break;
7346         case AND: fputs ("and", file); break;
7347         case XOR: fputs ("xor", file); break;
7348         default: output_operand_lossage ("invalid %%A operand");
7349         }
7350       return;
7351
7352     case 'B':
7353       switch (GET_CODE (x))
7354         {
7355         case IOR: fputs ("orn", file); break;
7356         case AND: fputs ("andn", file); break;
7357         case XOR: fputs ("xnor", file); break;
7358         default: output_operand_lossage ("invalid %%B operand");
7359         }
7360       return;
7361
7362       /* These are used by the conditional move instructions.  */
7363     case 'c' :
7364     case 'C':
7365       {
7366         enum rtx_code rc = GET_CODE (x);
7367         
7368         if (code == 'c')
7369           {
7370             enum machine_mode mode = GET_MODE (XEXP (x, 0));
7371             if (mode == CCFPmode || mode == CCFPEmode)
7372               rc = reverse_condition_maybe_unordered (GET_CODE (x));
7373             else
7374               rc = reverse_condition (GET_CODE (x));
7375           }
7376         switch (rc)
7377           {
7378           case NE: fputs ("ne", file); break;
7379           case EQ: fputs ("e", file); break;
7380           case GE: fputs ("ge", file); break;
7381           case GT: fputs ("g", file); break;
7382           case LE: fputs ("le", file); break;
7383           case LT: fputs ("l", file); break;
7384           case GEU: fputs ("geu", file); break;
7385           case GTU: fputs ("gu", file); break;
7386           case LEU: fputs ("leu", file); break;
7387           case LTU: fputs ("lu", file); break;
7388           case LTGT: fputs ("lg", file); break;
7389           case UNORDERED: fputs ("u", file); break;
7390           case ORDERED: fputs ("o", file); break;
7391           case UNLT: fputs ("ul", file); break;
7392           case UNLE: fputs ("ule", file); break;
7393           case UNGT: fputs ("ug", file); break;
7394           case UNGE: fputs ("uge", file); break;
7395           case UNEQ: fputs ("ue", file); break;
7396           default: output_operand_lossage (code == 'c'
7397                                            ? "invalid %%c operand"
7398                                            : "invalid %%C operand");
7399           }
7400         return;
7401       }
7402
7403       /* These are used by the movr instruction pattern.  */
7404     case 'd':
7405     case 'D':
7406       {
7407         enum rtx_code rc = (code == 'd'
7408                             ? reverse_condition (GET_CODE (x))
7409                             : GET_CODE (x));
7410         switch (rc)
7411           {
7412           case NE: fputs ("ne", file); break;
7413           case EQ: fputs ("e", file); break;
7414           case GE: fputs ("gez", file); break;
7415           case LT: fputs ("lz", file); break;
7416           case LE: fputs ("lez", file); break;
7417           case GT: fputs ("gz", file); break;
7418           default: output_operand_lossage (code == 'd'
7419                                            ? "invalid %%d operand"
7420                                            : "invalid %%D operand");
7421           }
7422         return;
7423       }
7424
7425     case 'b':
7426       {
7427         /* Print a sign-extended character.  */
7428         int i = trunc_int_for_mode (INTVAL (x), QImode);
7429         fprintf (file, "%d", i);
7430         return;
7431       }
7432
7433     case 'f':
7434       /* Operand must be a MEM; write its address.  */
7435       if (GET_CODE (x) != MEM)
7436         output_operand_lossage ("invalid %%f operand");
7437       output_address (XEXP (x, 0));
7438       return;
7439
7440     case 's':
7441       {
7442         /* Print a sign-extended 32-bit value.  */
7443         HOST_WIDE_INT i;
7444         if (GET_CODE(x) == CONST_INT)
7445           i = INTVAL (x);
7446         else if (GET_CODE(x) == CONST_DOUBLE)
7447           i = CONST_DOUBLE_LOW (x);
7448         else
7449           {
7450             output_operand_lossage ("invalid %%s operand");
7451             return;
7452           }
7453         i = trunc_int_for_mode (i, SImode);
7454         fprintf (file, HOST_WIDE_INT_PRINT_DEC, i);
7455         return;
7456       }
7457
7458     case 0:
7459       /* Do nothing special.  */
7460       break;
7461
7462     default:
7463       /* Undocumented flag.  */
7464       output_operand_lossage ("invalid operand output code");
7465     }
7466
7467   if (GET_CODE (x) == REG)
7468     fputs (reg_names[REGNO (x)], file);
7469   else if (GET_CODE (x) == MEM)
7470     {
7471       fputc ('[', file);
7472         /* Poor Sun assembler doesn't understand absolute addressing.  */
7473       if (CONSTANT_P (XEXP (x, 0)))
7474         fputs ("%g0+", file);
7475       output_address (XEXP (x, 0));
7476       fputc (']', file);
7477     }
7478   else if (GET_CODE (x) == HIGH)
7479     {
7480       fputs ("%hi(", file);
7481       output_addr_const (file, XEXP (x, 0));
7482       fputc (')', file);
7483     }
7484   else if (GET_CODE (x) == LO_SUM)
7485     {
7486       print_operand (file, XEXP (x, 0), 0);
7487       if (TARGET_CM_MEDMID)
7488         fputs ("+%l44(", file);
7489       else
7490         fputs ("+%lo(", file);
7491       output_addr_const (file, XEXP (x, 1));
7492       fputc (')', file);
7493     }
7494   else if (GET_CODE (x) == CONST_DOUBLE
7495            && (GET_MODE (x) == VOIDmode
7496                || GET_MODE_CLASS (GET_MODE (x)) == MODE_INT))
7497     {
7498       if (CONST_DOUBLE_HIGH (x) == 0)
7499         fprintf (file, "%u", (unsigned int) CONST_DOUBLE_LOW (x));
7500       else if (CONST_DOUBLE_HIGH (x) == -1
7501                && CONST_DOUBLE_LOW (x) < 0)
7502         fprintf (file, "%d", (int) CONST_DOUBLE_LOW (x));
7503       else
7504         output_operand_lossage ("long long constant not a valid immediate operand");
7505     }
7506   else if (GET_CODE (x) == CONST_DOUBLE)
7507     output_operand_lossage ("floating point constant not a valid immediate operand");
7508   else { output_addr_const (file, x); }
7509 }
7510 \f
7511 /* Target hook for assembling integer objects.  The sparc version has
7512    special handling for aligned DI-mode objects.  */
7513
7514 static bool
7515 sparc_assemble_integer (rtx x, unsigned int size, int aligned_p)
7516 {
7517   /* ??? We only output .xword's for symbols and only then in environments
7518      where the assembler can handle them.  */
7519   if (aligned_p && size == 8
7520       && (GET_CODE (x) != CONST_INT && GET_CODE (x) != CONST_DOUBLE))
7521     {
7522       if (TARGET_V9)
7523         {
7524           assemble_integer_with_op ("\t.xword\t", x);
7525           return true;
7526         }
7527       else
7528         {
7529           assemble_aligned_integer (4, const0_rtx);
7530           assemble_aligned_integer (4, x);
7531           return true;
7532         }
7533     }
7534   return default_assemble_integer (x, size, aligned_p);
7535 }
7536 \f
7537 /* Return the value of a code used in the .proc pseudo-op that says
7538    what kind of result this function returns.  For non-C types, we pick
7539    the closest C type.  */
7540
7541 #ifndef SHORT_TYPE_SIZE
7542 #define SHORT_TYPE_SIZE (BITS_PER_UNIT * 2)
7543 #endif
7544
7545 #ifndef INT_TYPE_SIZE
7546 #define INT_TYPE_SIZE BITS_PER_WORD
7547 #endif
7548
7549 #ifndef LONG_TYPE_SIZE
7550 #define LONG_TYPE_SIZE BITS_PER_WORD
7551 #endif
7552
7553 #ifndef LONG_LONG_TYPE_SIZE
7554 #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
7555 #endif
7556
7557 #ifndef FLOAT_TYPE_SIZE
7558 #define FLOAT_TYPE_SIZE BITS_PER_WORD
7559 #endif
7560
7561 #ifndef DOUBLE_TYPE_SIZE
7562 #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
7563 #endif
7564
7565 #ifndef LONG_DOUBLE_TYPE_SIZE
7566 #define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
7567 #endif
7568
7569 unsigned long
7570 sparc_type_code (register tree type)
7571 {
7572   register unsigned long qualifiers = 0;
7573   register unsigned shift;
7574
7575   /* Only the first 30 bits of the qualifier are valid.  We must refrain from
7576      setting more, since some assemblers will give an error for this.  Also,
7577      we must be careful to avoid shifts of 32 bits or more to avoid getting
7578      unpredictable results.  */
7579
7580   for (shift = 6; shift < 30; shift += 2, type = TREE_TYPE (type))
7581     {
7582       switch (TREE_CODE (type))
7583         {
7584         case ERROR_MARK:
7585           return qualifiers;
7586
7587         case ARRAY_TYPE:
7588           qualifiers |= (3 << shift);
7589           break;
7590
7591         case FUNCTION_TYPE:
7592         case METHOD_TYPE:
7593           qualifiers |= (2 << shift);
7594           break;
7595
7596         case POINTER_TYPE:
7597         case REFERENCE_TYPE:
7598         case OFFSET_TYPE:
7599           qualifiers |= (1 << shift);
7600           break;
7601
7602         case RECORD_TYPE:
7603           return (qualifiers | 8);
7604
7605         case UNION_TYPE:
7606         case QUAL_UNION_TYPE:
7607           return (qualifiers | 9);
7608
7609         case ENUMERAL_TYPE:
7610           return (qualifiers | 10);
7611
7612         case VOID_TYPE:
7613           return (qualifiers | 16);
7614
7615         case INTEGER_TYPE:
7616           /* If this is a range type, consider it to be the underlying
7617              type.  */
7618           if (TREE_TYPE (type) != 0)
7619             break;
7620
7621           /* Carefully distinguish all the standard types of C,
7622              without messing up if the language is not C.  We do this by
7623              testing TYPE_PRECISION and TYPE_UNSIGNED.  The old code used to
7624              look at both the names and the above fields, but that's redundant.
7625              Any type whose size is between two C types will be considered
7626              to be the wider of the two types.  Also, we do not have a
7627              special code to use for "long long", so anything wider than
7628              long is treated the same.  Note that we can't distinguish
7629              between "int" and "long" in this code if they are the same
7630              size, but that's fine, since neither can the assembler.  */
7631
7632           if (TYPE_PRECISION (type) <= CHAR_TYPE_SIZE)
7633             return (qualifiers | (TYPE_UNSIGNED (type) ? 12 : 2));
7634
7635           else if (TYPE_PRECISION (type) <= SHORT_TYPE_SIZE)
7636             return (qualifiers | (TYPE_UNSIGNED (type) ? 13 : 3));
7637
7638           else if (TYPE_PRECISION (type) <= INT_TYPE_SIZE)
7639             return (qualifiers | (TYPE_UNSIGNED (type) ? 14 : 4));
7640
7641           else
7642             return (qualifiers | (TYPE_UNSIGNED (type) ? 15 : 5));
7643
7644         case REAL_TYPE:
7645           /* If this is a range type, consider it to be the underlying
7646              type.  */
7647           if (TREE_TYPE (type) != 0)
7648             break;
7649
7650           /* Carefully distinguish all the standard types of C,
7651              without messing up if the language is not C.  */
7652
7653           if (TYPE_PRECISION (type) == FLOAT_TYPE_SIZE)
7654             return (qualifiers | 6);
7655
7656           else
7657             return (qualifiers | 7);
7658
7659         case COMPLEX_TYPE:      /* GNU Fortran COMPLEX type.  */
7660           /* ??? We need to distinguish between double and float complex types,
7661              but I don't know how yet because I can't reach this code from
7662              existing front-ends.  */
7663           return (qualifiers | 7);      /* Who knows? */
7664
7665         case VECTOR_TYPE:
7666         case BOOLEAN_TYPE:      /* Boolean truth value type.  */
7667         case LANG_TYPE:         /* ? */
7668           return qualifiers;
7669
7670         default:
7671           gcc_unreachable ();           /* Not a type! */
7672         }
7673     }
7674
7675   return qualifiers;
7676 }
7677 \f
7678 /* Nested function support.  */
7679
7680 /* Emit RTL insns to initialize the variable parts of a trampoline.
7681    FNADDR is an RTX for the address of the function's pure code.
7682    CXT is an RTX for the static chain value for the function.
7683
7684    This takes 16 insns: 2 shifts & 2 ands (to split up addresses), 4 sethi
7685    (to load in opcodes), 4 iors (to merge address and opcodes), and 4 writes
7686    (to store insns).  This is a bit excessive.  Perhaps a different
7687    mechanism would be better here.
7688
7689    Emit enough FLUSH insns to synchronize the data and instruction caches.  */
7690
7691 static void
7692 sparc32_initialize_trampoline (rtx m_tramp, rtx fnaddr, rtx cxt)
7693 {
7694   /* SPARC 32-bit trampoline:
7695
7696         sethi   %hi(fn), %g1
7697         sethi   %hi(static), %g2
7698         jmp     %g1+%lo(fn)
7699         or      %g2, %lo(static), %g2
7700
7701     SETHI i,r  = 00rr rrr1 00ii iiii iiii iiii iiii iiii
7702     JMPL r+i,d = 10dd ddd1 1100 0rrr rr1i iiii iiii iiii
7703    */
7704
7705   emit_move_insn
7706     (adjust_address (m_tramp, SImode, 0),
7707      expand_binop (SImode, ior_optab,
7708                    expand_shift (RSHIFT_EXPR, SImode, fnaddr,
7709                                  size_int (10), 0, 1),
7710                    GEN_INT (trunc_int_for_mode (0x03000000, SImode)),
7711                    NULL_RTX, 1, OPTAB_DIRECT));
7712
7713   emit_move_insn
7714     (adjust_address (m_tramp, SImode, 4),
7715      expand_binop (SImode, ior_optab,
7716                    expand_shift (RSHIFT_EXPR, SImode, cxt,
7717                                  size_int (10), 0, 1),
7718                    GEN_INT (trunc_int_for_mode (0x05000000, SImode)),
7719                    NULL_RTX, 1, OPTAB_DIRECT));
7720
7721   emit_move_insn
7722     (adjust_address (m_tramp, SImode, 8),
7723      expand_binop (SImode, ior_optab,
7724                    expand_and (SImode, fnaddr, GEN_INT (0x3ff), NULL_RTX),
7725                    GEN_INT (trunc_int_for_mode (0x81c06000, SImode)),
7726                    NULL_RTX, 1, OPTAB_DIRECT));
7727
7728   emit_move_insn
7729     (adjust_address (m_tramp, SImode, 12),
7730      expand_binop (SImode, ior_optab,
7731                    expand_and (SImode, cxt, GEN_INT (0x3ff), NULL_RTX),
7732                    GEN_INT (trunc_int_for_mode (0x8410a000, SImode)),
7733                    NULL_RTX, 1, OPTAB_DIRECT));
7734
7735   /* On UltraSPARC a flush flushes an entire cache line.  The trampoline is
7736      aligned on a 16 byte boundary so one flush clears it all.  */
7737   emit_insn (gen_flush (validize_mem (adjust_address (m_tramp, SImode, 0))));
7738   if (sparc_cpu != PROCESSOR_ULTRASPARC
7739       && sparc_cpu != PROCESSOR_ULTRASPARC3
7740       && sparc_cpu != PROCESSOR_NIAGARA
7741       && sparc_cpu != PROCESSOR_NIAGARA2)
7742     emit_insn (gen_flush (validize_mem (adjust_address (m_tramp, SImode, 8))));
7743
7744   /* Call __enable_execute_stack after writing onto the stack to make sure
7745      the stack address is accessible.  */
7746 #ifdef ENABLE_EXECUTE_STACK
7747   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
7748                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
7749 #endif
7750
7751 }
7752
7753 /* The 64-bit version is simpler because it makes more sense to load the
7754    values as "immediate" data out of the trampoline.  It's also easier since
7755    we can read the PC without clobbering a register.  */
7756
7757 static void
7758 sparc64_initialize_trampoline (rtx m_tramp, rtx fnaddr, rtx cxt)
7759 {
7760   /* SPARC 64-bit trampoline:
7761
7762         rd      %pc, %g1
7763         ldx     [%g1+24], %g5
7764         jmp     %g5
7765         ldx     [%g1+16], %g5
7766         +16 bytes data
7767    */
7768
7769   emit_move_insn (adjust_address (m_tramp, SImode, 0),
7770                   GEN_INT (trunc_int_for_mode (0x83414000, SImode)));
7771   emit_move_insn (adjust_address (m_tramp, SImode, 4),
7772                   GEN_INT (trunc_int_for_mode (0xca586018, SImode)));
7773   emit_move_insn (adjust_address (m_tramp, SImode, 8),
7774                   GEN_INT (trunc_int_for_mode (0x81c14000, SImode)));
7775   emit_move_insn (adjust_address (m_tramp, SImode, 12),
7776                   GEN_INT (trunc_int_for_mode (0xca586010, SImode)));
7777   emit_move_insn (adjust_address (m_tramp, DImode, 16), cxt);
7778   emit_move_insn (adjust_address (m_tramp, DImode, 24), fnaddr);
7779   emit_insn (gen_flushdi (validize_mem (adjust_address (m_tramp, DImode, 0))));
7780
7781   if (sparc_cpu != PROCESSOR_ULTRASPARC
7782       && sparc_cpu != PROCESSOR_ULTRASPARC3
7783       && sparc_cpu != PROCESSOR_NIAGARA
7784       && sparc_cpu != PROCESSOR_NIAGARA2)
7785     emit_insn (gen_flushdi (validize_mem (adjust_address (m_tramp, DImode, 8))));
7786
7787   /* Call __enable_execute_stack after writing onto the stack to make sure
7788      the stack address is accessible.  */
7789 #ifdef ENABLE_EXECUTE_STACK
7790   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
7791                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
7792 #endif
7793 }
7794
7795 /* Worker for TARGET_TRAMPOLINE_INIT.  */
7796
7797 static void
7798 sparc_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
7799 {
7800   rtx fnaddr = force_reg (Pmode, XEXP (DECL_RTL (fndecl), 0));
7801   cxt = force_reg (Pmode, cxt);
7802   if (TARGET_ARCH64)
7803     sparc64_initialize_trampoline (m_tramp, fnaddr, cxt);
7804   else
7805     sparc32_initialize_trampoline (m_tramp, fnaddr, cxt);
7806 }
7807 \f
7808 /* Adjust the cost of a scheduling dependency.  Return the new cost of
7809    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
7810
7811 static int
7812 supersparc_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
7813 {
7814   enum attr_type insn_type;
7815
7816   if (! recog_memoized (insn))
7817     return 0;
7818
7819   insn_type = get_attr_type (insn);
7820
7821   if (REG_NOTE_KIND (link) == 0)
7822     {
7823       /* Data dependency; DEP_INSN writes a register that INSN reads some
7824          cycles later.  */
7825
7826       /* if a load, then the dependence must be on the memory address;
7827          add an extra "cycle".  Note that the cost could be two cycles
7828          if the reg was written late in an instruction group; we ca not tell
7829          here.  */
7830       if (insn_type == TYPE_LOAD || insn_type == TYPE_FPLOAD)
7831         return cost + 3;
7832
7833       /* Get the delay only if the address of the store is the dependence.  */
7834       if (insn_type == TYPE_STORE || insn_type == TYPE_FPSTORE)
7835         {
7836           rtx pat = PATTERN(insn);
7837           rtx dep_pat = PATTERN (dep_insn);
7838
7839           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
7840             return cost;  /* This should not happen!  */
7841
7842           /* The dependency between the two instructions was on the data that
7843              is being stored.  Assume that this implies that the address of the
7844              store is not dependent.  */
7845           if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
7846             return cost;
7847
7848           return cost + 3;  /* An approximation.  */
7849         }
7850
7851       /* A shift instruction cannot receive its data from an instruction
7852          in the same cycle; add a one cycle penalty.  */
7853       if (insn_type == TYPE_SHIFT)
7854         return cost + 3;   /* Split before cascade into shift.  */
7855     }
7856   else
7857     {
7858       /* Anti- or output- dependency; DEP_INSN reads/writes a register that
7859          INSN writes some cycles later.  */
7860
7861       /* These are only significant for the fpu unit; writing a fp reg before
7862          the fpu has finished with it stalls the processor.  */
7863
7864       /* Reusing an integer register causes no problems.  */
7865       if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
7866         return 0;
7867     }
7868         
7869   return cost;
7870 }
7871
7872 static int
7873 hypersparc_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
7874 {
7875   enum attr_type insn_type, dep_type;
7876   rtx pat = PATTERN(insn);
7877   rtx dep_pat = PATTERN (dep_insn);
7878
7879   if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
7880     return cost;
7881
7882   insn_type = get_attr_type (insn);
7883   dep_type = get_attr_type (dep_insn);
7884
7885   switch (REG_NOTE_KIND (link))
7886     {
7887     case 0:
7888       /* Data dependency; DEP_INSN writes a register that INSN reads some
7889          cycles later.  */
7890
7891       switch (insn_type)
7892         {
7893         case TYPE_STORE:
7894         case TYPE_FPSTORE:
7895           /* Get the delay iff the address of the store is the dependence.  */
7896           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
7897             return cost;
7898
7899           if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
7900             return cost;
7901           return cost + 3;
7902
7903         case TYPE_LOAD:
7904         case TYPE_SLOAD:
7905         case TYPE_FPLOAD:
7906           /* If a load, then the dependence must be on the memory address.  If
7907              the addresses aren't equal, then it might be a false dependency */
7908           if (dep_type == TYPE_STORE || dep_type == TYPE_FPSTORE)
7909             {
7910               if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET
7911                   || GET_CODE (SET_DEST (dep_pat)) != MEM
7912                   || GET_CODE (SET_SRC (pat)) != MEM
7913                   || ! rtx_equal_p (XEXP (SET_DEST (dep_pat), 0),
7914                                     XEXP (SET_SRC (pat), 0)))
7915                 return cost + 2;
7916
7917               return cost + 8;
7918             }
7919           break;
7920
7921         case TYPE_BRANCH:
7922           /* Compare to branch latency is 0.  There is no benefit from
7923              separating compare and branch.  */
7924           if (dep_type == TYPE_COMPARE)
7925             return 0;
7926           /* Floating point compare to branch latency is less than
7927              compare to conditional move.  */
7928           if (dep_type == TYPE_FPCMP)
7929             return cost - 1;
7930           break;
7931         default:
7932           break;
7933         }
7934         break;
7935
7936     case REG_DEP_ANTI:
7937       /* Anti-dependencies only penalize the fpu unit.  */
7938       if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
7939         return 0;
7940       break;
7941
7942     default:
7943       break;
7944     }
7945
7946   return cost;
7947 }
7948
7949 static int
7950 sparc_adjust_cost(rtx insn, rtx link, rtx dep, int cost)
7951 {
7952   switch (sparc_cpu)
7953     {
7954     case PROCESSOR_SUPERSPARC:
7955       cost = supersparc_adjust_cost (insn, link, dep, cost);
7956       break;
7957     case PROCESSOR_HYPERSPARC:
7958     case PROCESSOR_SPARCLITE86X:
7959       cost = hypersparc_adjust_cost (insn, link, dep, cost);
7960       break;
7961     default:
7962       break;
7963     }
7964   return cost;
7965 }
7966
7967 static void
7968 sparc_sched_init (FILE *dump ATTRIBUTE_UNUSED,
7969                   int sched_verbose ATTRIBUTE_UNUSED,
7970                   int max_ready ATTRIBUTE_UNUSED)
7971 {}
7972
7973 static int
7974 sparc_use_sched_lookahead (void)
7975 {
7976   if (sparc_cpu == PROCESSOR_NIAGARA
7977       || sparc_cpu == PROCESSOR_NIAGARA2)
7978     return 0;
7979   if (sparc_cpu == PROCESSOR_ULTRASPARC
7980       || sparc_cpu == PROCESSOR_ULTRASPARC3)
7981     return 4;
7982   if ((1 << sparc_cpu) &
7983       ((1 << PROCESSOR_SUPERSPARC) | (1 << PROCESSOR_HYPERSPARC) |
7984        (1 << PROCESSOR_SPARCLITE86X)))
7985     return 3;
7986   return 0;
7987 }
7988
7989 static int
7990 sparc_issue_rate (void)
7991 {
7992   switch (sparc_cpu)
7993     {
7994     case PROCESSOR_NIAGARA:
7995     case PROCESSOR_NIAGARA2:
7996     default:
7997       return 1;
7998     case PROCESSOR_V9:
7999       /* Assume V9 processors are capable of at least dual-issue.  */
8000       return 2;
8001     case PROCESSOR_SUPERSPARC:
8002       return 3;
8003     case PROCESSOR_HYPERSPARC:
8004     case PROCESSOR_SPARCLITE86X:
8005       return 2;
8006     case PROCESSOR_ULTRASPARC:
8007     case PROCESSOR_ULTRASPARC3:
8008       return 4;
8009     }
8010 }
8011
8012 static int
8013 set_extends (rtx insn)
8014 {
8015   register rtx pat = PATTERN (insn);
8016
8017   switch (GET_CODE (SET_SRC (pat)))
8018     {
8019       /* Load and some shift instructions zero extend.  */
8020     case MEM:
8021     case ZERO_EXTEND:
8022       /* sethi clears the high bits */
8023     case HIGH:
8024       /* LO_SUM is used with sethi.  sethi cleared the high
8025          bits and the values used with lo_sum are positive */
8026     case LO_SUM:
8027       /* Store flag stores 0 or 1 */
8028     case LT: case LTU:
8029     case GT: case GTU:
8030     case LE: case LEU:
8031     case GE: case GEU:
8032     case EQ:
8033     case NE:
8034       return 1;
8035     case AND:
8036       {
8037         rtx op0 = XEXP (SET_SRC (pat), 0);
8038         rtx op1 = XEXP (SET_SRC (pat), 1);
8039         if (GET_CODE (op1) == CONST_INT)
8040           return INTVAL (op1) >= 0;
8041         if (GET_CODE (op0) != REG)
8042           return 0;
8043         if (sparc_check_64 (op0, insn) == 1)
8044           return 1;
8045         return (GET_CODE (op1) == REG && sparc_check_64 (op1, insn) == 1);
8046       }
8047     case IOR:
8048     case XOR:
8049       {
8050         rtx op0 = XEXP (SET_SRC (pat), 0);
8051         rtx op1 = XEXP (SET_SRC (pat), 1);
8052         if (GET_CODE (op0) != REG || sparc_check_64 (op0, insn) <= 0)
8053           return 0;
8054         if (GET_CODE (op1) == CONST_INT)
8055           return INTVAL (op1) >= 0;
8056         return (GET_CODE (op1) == REG && sparc_check_64 (op1, insn) == 1);
8057       }
8058     case LSHIFTRT:
8059       return GET_MODE (SET_SRC (pat)) == SImode;
8060       /* Positive integers leave the high bits zero.  */
8061     case CONST_DOUBLE:
8062       return ! (CONST_DOUBLE_LOW (SET_SRC (pat)) & 0x80000000);
8063     case CONST_INT:
8064       return ! (INTVAL (SET_SRC (pat)) & 0x80000000);
8065     case ASHIFTRT:
8066     case SIGN_EXTEND:
8067       return - (GET_MODE (SET_SRC (pat)) == SImode);
8068     case REG:
8069       return sparc_check_64 (SET_SRC (pat), insn);
8070     default:
8071       return 0;
8072     }
8073 }
8074
8075 /* We _ought_ to have only one kind per function, but...  */
8076 static GTY(()) rtx sparc_addr_diff_list;
8077 static GTY(()) rtx sparc_addr_list;
8078
8079 void
8080 sparc_defer_case_vector (rtx lab, rtx vec, int diff)
8081 {
8082   vec = gen_rtx_EXPR_LIST (VOIDmode, lab, vec);
8083   if (diff)
8084     sparc_addr_diff_list
8085       = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_diff_list);
8086   else
8087     sparc_addr_list = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_list);
8088 }
8089
8090 static void
8091 sparc_output_addr_vec (rtx vec)
8092 {
8093   rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
8094   int idx, vlen = XVECLEN (body, 0);
8095
8096 #ifdef ASM_OUTPUT_ADDR_VEC_START
8097   ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
8098 #endif
8099
8100 #ifdef ASM_OUTPUT_CASE_LABEL
8101   ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
8102                          NEXT_INSN (lab));
8103 #else
8104   (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
8105 #endif
8106
8107   for (idx = 0; idx < vlen; idx++)
8108     {
8109       ASM_OUTPUT_ADDR_VEC_ELT
8110         (asm_out_file, CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 0, idx), 0)));
8111     }
8112
8113 #ifdef ASM_OUTPUT_ADDR_VEC_END
8114   ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
8115 #endif
8116 }
8117
8118 static void
8119 sparc_output_addr_diff_vec (rtx vec)
8120 {
8121   rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
8122   rtx base = XEXP (XEXP (body, 0), 0);
8123   int idx, vlen = XVECLEN (body, 1);
8124
8125 #ifdef ASM_OUTPUT_ADDR_VEC_START
8126   ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
8127 #endif
8128
8129 #ifdef ASM_OUTPUT_CASE_LABEL
8130   ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
8131                          NEXT_INSN (lab));
8132 #else
8133   (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
8134 #endif
8135
8136   for (idx = 0; idx < vlen; idx++)
8137     {
8138       ASM_OUTPUT_ADDR_DIFF_ELT
8139         (asm_out_file,
8140          body,
8141          CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 1, idx), 0)),
8142          CODE_LABEL_NUMBER (base));
8143     }
8144
8145 #ifdef ASM_OUTPUT_ADDR_VEC_END
8146   ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
8147 #endif
8148 }
8149
8150 static void
8151 sparc_output_deferred_case_vectors (void)
8152 {
8153   rtx t;
8154   int align;
8155
8156   if (sparc_addr_list == NULL_RTX
8157       && sparc_addr_diff_list == NULL_RTX)
8158     return;
8159
8160   /* Align to cache line in the function's code section.  */
8161   switch_to_section (current_function_section ());
8162
8163   align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
8164   if (align > 0)
8165     ASM_OUTPUT_ALIGN (asm_out_file, align);
8166
8167   for (t = sparc_addr_list; t ; t = XEXP (t, 1))
8168     sparc_output_addr_vec (XEXP (t, 0));
8169   for (t = sparc_addr_diff_list; t ; t = XEXP (t, 1))
8170     sparc_output_addr_diff_vec (XEXP (t, 0));
8171
8172   sparc_addr_list = sparc_addr_diff_list = NULL_RTX;
8173 }
8174
8175 /* Return 0 if the high 32 bits of X (the low word of X, if DImode) are
8176    unknown.  Return 1 if the high bits are zero, -1 if the register is
8177    sign extended.  */
8178 int
8179 sparc_check_64 (rtx x, rtx insn)
8180 {
8181   /* If a register is set only once it is safe to ignore insns this
8182      code does not know how to handle.  The loop will either recognize
8183      the single set and return the correct value or fail to recognize
8184      it and return 0.  */
8185   int set_once = 0;
8186   rtx y = x;
8187
8188   gcc_assert (GET_CODE (x) == REG);
8189
8190   if (GET_MODE (x) == DImode)
8191     y = gen_rtx_REG (SImode, REGNO (x) + WORDS_BIG_ENDIAN);
8192
8193   if (flag_expensive_optimizations
8194       && df && DF_REG_DEF_COUNT (REGNO (y)) == 1)
8195     set_once = 1;
8196
8197   if (insn == 0)
8198     {
8199       if (set_once)
8200         insn = get_last_insn_anywhere ();
8201       else
8202         return 0;
8203     }
8204
8205   while ((insn = PREV_INSN (insn)))
8206     {
8207       switch (GET_CODE (insn))
8208         {
8209         case JUMP_INSN:
8210         case NOTE:
8211           break;
8212         case CODE_LABEL:
8213         case CALL_INSN:
8214         default:
8215           if (! set_once)
8216             return 0;
8217           break;
8218         case INSN:
8219           {
8220             rtx pat = PATTERN (insn);
8221             if (GET_CODE (pat) != SET)
8222               return 0;
8223             if (rtx_equal_p (x, SET_DEST (pat)))
8224               return set_extends (insn);
8225             if (y && rtx_equal_p (y, SET_DEST (pat)))
8226               return set_extends (insn);
8227             if (reg_overlap_mentioned_p (SET_DEST (pat), y))
8228               return 0;
8229           }
8230         }
8231     }
8232   return 0;
8233 }
8234
8235 /* Returns assembly code to perform a DImode shift using
8236    a 64-bit global or out register on SPARC-V8+.  */
8237 const char *
8238 output_v8plus_shift (rtx *operands, rtx insn, const char *opcode)
8239 {
8240   static char asm_code[60];
8241
8242   /* The scratch register is only required when the destination
8243      register is not a 64-bit global or out register.  */
8244   if (which_alternative != 2)
8245     operands[3] = operands[0];
8246
8247   /* We can only shift by constants <= 63. */
8248   if (GET_CODE (operands[2]) == CONST_INT)
8249     operands[2] = GEN_INT (INTVAL (operands[2]) & 0x3f);
8250
8251   if (GET_CODE (operands[1]) == CONST_INT)
8252     {
8253       output_asm_insn ("mov\t%1, %3", operands);
8254     }
8255   else
8256     {
8257       output_asm_insn ("sllx\t%H1, 32, %3", operands);
8258       if (sparc_check_64 (operands[1], insn) <= 0)
8259         output_asm_insn ("srl\t%L1, 0, %L1", operands);
8260       output_asm_insn ("or\t%L1, %3, %3", operands);
8261     }
8262
8263   strcpy(asm_code, opcode);
8264
8265   if (which_alternative != 2)
8266     return strcat (asm_code, "\t%0, %2, %L0\n\tsrlx\t%L0, 32, %H0");
8267   else
8268     return strcat (asm_code, "\t%3, %2, %3\n\tsrlx\t%3, 32, %H0\n\tmov\t%3, %L0");
8269 }
8270 \f
8271 /* Output rtl to increment the profiler label LABELNO
8272    for profiling a function entry.  */
8273
8274 void
8275 sparc_profile_hook (int labelno)
8276 {
8277   char buf[32];
8278   rtx lab, fun;
8279
8280   fun = gen_rtx_SYMBOL_REF (Pmode, MCOUNT_FUNCTION);
8281   if (NO_PROFILE_COUNTERS)
8282     {
8283       emit_library_call (fun, LCT_NORMAL, VOIDmode, 0);
8284     }
8285   else
8286     {
8287       ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
8288       lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
8289       emit_library_call (fun, LCT_NORMAL, VOIDmode, 1, lab, Pmode);
8290     }
8291 }
8292 \f
8293 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
8294
8295 static void
8296 sparc_solaris_elf_asm_named_section (const char *name, unsigned int flags,
8297                                      tree decl ATTRIBUTE_UNUSED)
8298 {
8299   fprintf (asm_out_file, "\t.section\t\"%s\"", name);
8300
8301   if (!(flags & SECTION_DEBUG))
8302     fputs (",#alloc", asm_out_file);
8303   if (flags & SECTION_WRITE)
8304     fputs (",#write", asm_out_file);
8305   if (flags & SECTION_TLS)
8306     fputs (",#tls", asm_out_file);
8307   if (flags & SECTION_CODE)
8308     fputs (",#execinstr", asm_out_file);
8309
8310   /* ??? Handle SECTION_BSS.  */
8311
8312   fputc ('\n', asm_out_file);
8313 }
8314
8315 /* We do not allow indirect calls to be optimized into sibling calls.
8316
8317    We cannot use sibling calls when delayed branches are disabled
8318    because they will likely require the call delay slot to be filled.
8319
8320    Also, on SPARC 32-bit we cannot emit a sibling call when the
8321    current function returns a structure.  This is because the "unimp
8322    after call" convention would cause the callee to return to the
8323    wrong place.  The generic code already disallows cases where the
8324    function being called returns a structure.
8325
8326    It may seem strange how this last case could occur.  Usually there
8327    is code after the call which jumps to epilogue code which dumps the
8328    return value into the struct return area.  That ought to invalidate
8329    the sibling call right?  Well, in the C++ case we can end up passing
8330    the pointer to the struct return area to a constructor (which returns
8331    void) and then nothing else happens.  Such a sibling call would look
8332    valid without the added check here.
8333
8334    VxWorks PIC PLT entries require the global pointer to be initialized
8335    on entry.  We therefore can't emit sibling calls to them.  */
8336 static bool
8337 sparc_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
8338 {
8339   return (decl
8340           && flag_delayed_branch
8341           && (TARGET_ARCH64 || ! cfun->returns_struct)
8342           && !(TARGET_VXWORKS_RTP
8343                && flag_pic
8344                && !targetm.binds_local_p (decl)));
8345 }
8346 \f
8347 /* libfunc renaming.  */
8348 #include "config/gofast.h"
8349
8350 static void
8351 sparc_init_libfuncs (void)
8352 {
8353   if (TARGET_ARCH32)
8354     {
8355       /* Use the subroutines that Sun's library provides for integer
8356          multiply and divide.  The `*' prevents an underscore from
8357          being prepended by the compiler. .umul is a little faster
8358          than .mul.  */
8359       set_optab_libfunc (smul_optab, SImode, "*.umul");
8360       set_optab_libfunc (sdiv_optab, SImode, "*.div");
8361       set_optab_libfunc (udiv_optab, SImode, "*.udiv");
8362       set_optab_libfunc (smod_optab, SImode, "*.rem");
8363       set_optab_libfunc (umod_optab, SImode, "*.urem");
8364
8365       /* TFmode arithmetic.  These names are part of the SPARC 32bit ABI.  */
8366       set_optab_libfunc (add_optab, TFmode, "_Q_add");
8367       set_optab_libfunc (sub_optab, TFmode, "_Q_sub");
8368       set_optab_libfunc (neg_optab, TFmode, "_Q_neg");
8369       set_optab_libfunc (smul_optab, TFmode, "_Q_mul");
8370       set_optab_libfunc (sdiv_optab, TFmode, "_Q_div");
8371
8372       /* We can define the TFmode sqrt optab only if TARGET_FPU.  This
8373          is because with soft-float, the SFmode and DFmode sqrt
8374          instructions will be absent, and the compiler will notice and
8375          try to use the TFmode sqrt instruction for calls to the
8376          builtin function sqrt, but this fails.  */
8377       if (TARGET_FPU)
8378         set_optab_libfunc (sqrt_optab, TFmode, "_Q_sqrt");
8379
8380       set_optab_libfunc (eq_optab, TFmode, "_Q_feq");
8381       set_optab_libfunc (ne_optab, TFmode, "_Q_fne");
8382       set_optab_libfunc (gt_optab, TFmode, "_Q_fgt");
8383       set_optab_libfunc (ge_optab, TFmode, "_Q_fge");
8384       set_optab_libfunc (lt_optab, TFmode, "_Q_flt");
8385       set_optab_libfunc (le_optab, TFmode, "_Q_fle");
8386
8387       set_conv_libfunc (sext_optab,   TFmode, SFmode, "_Q_stoq");
8388       set_conv_libfunc (sext_optab,   TFmode, DFmode, "_Q_dtoq");
8389       set_conv_libfunc (trunc_optab,  SFmode, TFmode, "_Q_qtos");
8390       set_conv_libfunc (trunc_optab,  DFmode, TFmode, "_Q_qtod");
8391
8392       set_conv_libfunc (sfix_optab,   SImode, TFmode, "_Q_qtoi");
8393       set_conv_libfunc (ufix_optab,   SImode, TFmode, "_Q_qtou");
8394       set_conv_libfunc (sfloat_optab, TFmode, SImode, "_Q_itoq");
8395       set_conv_libfunc (ufloat_optab, TFmode, SImode, "_Q_utoq");
8396
8397       if (DITF_CONVERSION_LIBFUNCS)
8398         {
8399           set_conv_libfunc (sfix_optab,   DImode, TFmode, "_Q_qtoll");
8400           set_conv_libfunc (ufix_optab,   DImode, TFmode, "_Q_qtoull");
8401           set_conv_libfunc (sfloat_optab, TFmode, DImode, "_Q_lltoq");
8402           set_conv_libfunc (ufloat_optab, TFmode, DImode, "_Q_ulltoq");
8403         }
8404
8405       if (SUN_CONVERSION_LIBFUNCS)
8406         {
8407           set_conv_libfunc (sfix_optab, DImode, SFmode, "__ftoll");
8408           set_conv_libfunc (ufix_optab, DImode, SFmode, "__ftoull");
8409           set_conv_libfunc (sfix_optab, DImode, DFmode, "__dtoll");
8410           set_conv_libfunc (ufix_optab, DImode, DFmode, "__dtoull");
8411         }
8412     }
8413   if (TARGET_ARCH64)
8414     {
8415       /* In the SPARC 64bit ABI, SImode multiply and divide functions
8416          do not exist in the library.  Make sure the compiler does not
8417          emit calls to them by accident.  (It should always use the
8418          hardware instructions.)  */
8419       set_optab_libfunc (smul_optab, SImode, 0);
8420       set_optab_libfunc (sdiv_optab, SImode, 0);
8421       set_optab_libfunc (udiv_optab, SImode, 0);
8422       set_optab_libfunc (smod_optab, SImode, 0);
8423       set_optab_libfunc (umod_optab, SImode, 0);
8424
8425       if (SUN_INTEGER_MULTIPLY_64)
8426         {
8427           set_optab_libfunc (smul_optab, DImode, "__mul64");
8428           set_optab_libfunc (sdiv_optab, DImode, "__div64");
8429           set_optab_libfunc (udiv_optab, DImode, "__udiv64");
8430           set_optab_libfunc (smod_optab, DImode, "__rem64");
8431           set_optab_libfunc (umod_optab, DImode, "__urem64");
8432         }
8433
8434       if (SUN_CONVERSION_LIBFUNCS)
8435         {
8436           set_conv_libfunc (sfix_optab, DImode, SFmode, "__ftol");
8437           set_conv_libfunc (ufix_optab, DImode, SFmode, "__ftoul");
8438           set_conv_libfunc (sfix_optab, DImode, DFmode, "__dtol");
8439           set_conv_libfunc (ufix_optab, DImode, DFmode, "__dtoul");
8440         }
8441     }
8442
8443   gofast_maybe_init_libfuncs ();
8444 }
8445 \f
8446 #define def_builtin(NAME, CODE, TYPE) \
8447   add_builtin_function((NAME), (TYPE), (CODE), BUILT_IN_MD, NULL, \
8448                        NULL_TREE)
8449
8450 /* Implement the TARGET_INIT_BUILTINS target hook.
8451    Create builtin functions for special SPARC instructions.  */
8452
8453 static void
8454 sparc_init_builtins (void)
8455 {
8456   if (TARGET_VIS)
8457     sparc_vis_init_builtins ();
8458 }
8459
8460 /* Create builtin functions for VIS 1.0 instructions.  */
8461
8462 static void
8463 sparc_vis_init_builtins (void)
8464 {
8465   tree v4qi = build_vector_type (unsigned_intQI_type_node, 4);
8466   tree v8qi = build_vector_type (unsigned_intQI_type_node, 8);
8467   tree v4hi = build_vector_type (intHI_type_node, 4);
8468   tree v2hi = build_vector_type (intHI_type_node, 2);
8469   tree v2si = build_vector_type (intSI_type_node, 2);
8470
8471   tree v4qi_ftype_v4hi = build_function_type_list (v4qi, v4hi, 0);
8472   tree v8qi_ftype_v2si_v8qi = build_function_type_list (v8qi, v2si, v8qi, 0);
8473   tree v2hi_ftype_v2si = build_function_type_list (v2hi, v2si, 0);
8474   tree v4hi_ftype_v4qi = build_function_type_list (v4hi, v4qi, 0);
8475   tree v8qi_ftype_v4qi_v4qi = build_function_type_list (v8qi, v4qi, v4qi, 0);
8476   tree v4hi_ftype_v4qi_v4hi = build_function_type_list (v4hi, v4qi, v4hi, 0);
8477   tree v4hi_ftype_v4qi_v2hi = build_function_type_list (v4hi, v4qi, v2hi, 0);
8478   tree v2si_ftype_v4qi_v2hi = build_function_type_list (v2si, v4qi, v2hi, 0);
8479   tree v4hi_ftype_v8qi_v4hi = build_function_type_list (v4hi, v8qi, v4hi, 0);
8480   tree v4hi_ftype_v4hi_v4hi = build_function_type_list (v4hi, v4hi, v4hi, 0);
8481   tree v2si_ftype_v2si_v2si = build_function_type_list (v2si, v2si, v2si, 0);
8482   tree v8qi_ftype_v8qi_v8qi = build_function_type_list (v8qi, v8qi, v8qi, 0);
8483   tree di_ftype_v8qi_v8qi_di = build_function_type_list (intDI_type_node,
8484                                                          v8qi, v8qi,
8485                                                          intDI_type_node, 0);
8486   tree di_ftype_di_di = build_function_type_list (intDI_type_node,
8487                                                   intDI_type_node,
8488                                                   intDI_type_node, 0);
8489   tree ptr_ftype_ptr_si = build_function_type_list (ptr_type_node,
8490                                                     ptr_type_node,
8491                                                     intSI_type_node, 0);
8492   tree ptr_ftype_ptr_di = build_function_type_list (ptr_type_node,
8493                                                     ptr_type_node,
8494                                                     intDI_type_node, 0);
8495
8496   /* Packing and expanding vectors.  */
8497   def_builtin ("__builtin_vis_fpack16", CODE_FOR_fpack16_vis, v4qi_ftype_v4hi);
8498   def_builtin ("__builtin_vis_fpack32", CODE_FOR_fpack32_vis,
8499                v8qi_ftype_v2si_v8qi);
8500   def_builtin ("__builtin_vis_fpackfix", CODE_FOR_fpackfix_vis,
8501                v2hi_ftype_v2si);
8502   def_builtin ("__builtin_vis_fexpand", CODE_FOR_fexpand_vis, v4hi_ftype_v4qi);
8503   def_builtin ("__builtin_vis_fpmerge", CODE_FOR_fpmerge_vis,
8504                v8qi_ftype_v4qi_v4qi);
8505
8506   /* Multiplications.  */
8507   def_builtin ("__builtin_vis_fmul8x16", CODE_FOR_fmul8x16_vis,
8508                v4hi_ftype_v4qi_v4hi);
8509   def_builtin ("__builtin_vis_fmul8x16au", CODE_FOR_fmul8x16au_vis,
8510                v4hi_ftype_v4qi_v2hi);
8511   def_builtin ("__builtin_vis_fmul8x16al", CODE_FOR_fmul8x16al_vis,
8512                v4hi_ftype_v4qi_v2hi);
8513   def_builtin ("__builtin_vis_fmul8sux16", CODE_FOR_fmul8sux16_vis,
8514                v4hi_ftype_v8qi_v4hi);
8515   def_builtin ("__builtin_vis_fmul8ulx16", CODE_FOR_fmul8ulx16_vis,
8516                v4hi_ftype_v8qi_v4hi);
8517   def_builtin ("__builtin_vis_fmuld8sux16", CODE_FOR_fmuld8sux16_vis,
8518                v2si_ftype_v4qi_v2hi);
8519   def_builtin ("__builtin_vis_fmuld8ulx16", CODE_FOR_fmuld8ulx16_vis,
8520                v2si_ftype_v4qi_v2hi);
8521
8522   /* Data aligning.  */
8523   def_builtin ("__builtin_vis_faligndatav4hi", CODE_FOR_faligndatav4hi_vis,
8524                v4hi_ftype_v4hi_v4hi);
8525   def_builtin ("__builtin_vis_faligndatav8qi", CODE_FOR_faligndatav8qi_vis,
8526                v8qi_ftype_v8qi_v8qi);
8527   def_builtin ("__builtin_vis_faligndatav2si", CODE_FOR_faligndatav2si_vis,
8528                v2si_ftype_v2si_v2si);
8529   def_builtin ("__builtin_vis_faligndatadi", CODE_FOR_faligndatadi_vis,
8530                di_ftype_di_di);
8531   if (TARGET_ARCH64)
8532     def_builtin ("__builtin_vis_alignaddr", CODE_FOR_alignaddrdi_vis,
8533                  ptr_ftype_ptr_di);
8534   else
8535     def_builtin ("__builtin_vis_alignaddr", CODE_FOR_alignaddrsi_vis,
8536                  ptr_ftype_ptr_si);
8537
8538   /* Pixel distance.  */
8539   def_builtin ("__builtin_vis_pdist", CODE_FOR_pdist_vis,
8540                di_ftype_v8qi_v8qi_di);
8541 }
8542
8543 /* Handle TARGET_EXPAND_BUILTIN target hook.
8544    Expand builtin functions for sparc intrinsics.  */
8545
8546 static rtx
8547 sparc_expand_builtin (tree exp, rtx target,
8548                       rtx subtarget ATTRIBUTE_UNUSED,
8549                       enum machine_mode tmode ATTRIBUTE_UNUSED,
8550                       int ignore ATTRIBUTE_UNUSED)
8551 {
8552   tree arg;
8553   call_expr_arg_iterator iter;
8554   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
8555   unsigned int icode = DECL_FUNCTION_CODE (fndecl);
8556   rtx pat, op[4];
8557   enum machine_mode mode[4];
8558   int arg_count = 0;
8559
8560   mode[0] = insn_data[icode].operand[0].mode;
8561   if (!target
8562       || GET_MODE (target) != mode[0]
8563       || ! (*insn_data[icode].operand[0].predicate) (target, mode[0]))
8564     op[0] = gen_reg_rtx (mode[0]);
8565   else
8566     op[0] = target;
8567
8568   FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
8569     {
8570       arg_count++;
8571       mode[arg_count] = insn_data[icode].operand[arg_count].mode;
8572       op[arg_count] = expand_normal (arg);
8573
8574       if (! (*insn_data[icode].operand[arg_count].predicate) (op[arg_count],
8575                                                               mode[arg_count]))
8576         op[arg_count] = copy_to_mode_reg (mode[arg_count], op[arg_count]);
8577     }
8578
8579   switch (arg_count)
8580     {
8581     case 1:
8582       pat = GEN_FCN (icode) (op[0], op[1]);
8583       break;
8584     case 2:
8585       pat = GEN_FCN (icode) (op[0], op[1], op[2]);
8586       break;
8587     case 3:
8588       pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
8589       break;
8590     default:
8591       gcc_unreachable ();
8592     }
8593
8594   if (!pat)
8595     return NULL_RTX;
8596
8597   emit_insn (pat);
8598
8599   return op[0];
8600 }
8601
8602 static int
8603 sparc_vis_mul8x16 (int e8, int e16)
8604 {
8605   return (e8 * e16 + 128) / 256;
8606 }
8607
8608 /* Multiply the vector elements in ELTS0 to the elements in ELTS1 as specified
8609    by FNCODE.  All of the elements in ELTS0 and ELTS1 lists must be integer
8610    constants.  A tree list with the results of the multiplications is returned,
8611    and each element in the list is of INNER_TYPE.  */
8612
8613 static tree
8614 sparc_handle_vis_mul8x16 (int fncode, tree inner_type, tree elts0, tree elts1)
8615 {
8616   tree n_elts = NULL_TREE;
8617   int scale;
8618
8619   switch (fncode)
8620     {
8621     case CODE_FOR_fmul8x16_vis:
8622       for (; elts0 && elts1;
8623            elts0 = TREE_CHAIN (elts0), elts1 = TREE_CHAIN (elts1))
8624         {
8625           int val
8626             = sparc_vis_mul8x16 (TREE_INT_CST_LOW (TREE_VALUE (elts0)),
8627                                  TREE_INT_CST_LOW (TREE_VALUE (elts1)));
8628           n_elts = tree_cons (NULL_TREE,
8629                               build_int_cst (inner_type, val),
8630                               n_elts);
8631         }
8632       break;
8633
8634     case CODE_FOR_fmul8x16au_vis:
8635       scale = TREE_INT_CST_LOW (TREE_VALUE (elts1));
8636
8637       for (; elts0; elts0 = TREE_CHAIN (elts0))
8638         {
8639           int val
8640             = sparc_vis_mul8x16 (TREE_INT_CST_LOW (TREE_VALUE (elts0)),
8641                                  scale);
8642           n_elts = tree_cons (NULL_TREE,
8643                               build_int_cst (inner_type, val),
8644                               n_elts);
8645         }
8646       break;
8647
8648     case CODE_FOR_fmul8x16al_vis:
8649       scale = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (elts1)));
8650
8651       for (; elts0; elts0 = TREE_CHAIN (elts0))
8652         {
8653           int val
8654             = sparc_vis_mul8x16 (TREE_INT_CST_LOW (TREE_VALUE (elts0)),
8655                                  scale);
8656           n_elts = tree_cons (NULL_TREE,
8657                               build_int_cst (inner_type, val),
8658                               n_elts);
8659         }
8660       break;
8661
8662     default:
8663       gcc_unreachable ();
8664     }
8665
8666   return nreverse (n_elts);
8667
8668 }
8669 /* Handle TARGET_FOLD_BUILTIN target hook.
8670    Fold builtin functions for SPARC intrinsics.  If IGNORE is true the
8671    result of the function call is ignored.  NULL_TREE is returned if the
8672    function could not be folded.  */
8673
8674 static tree
8675 sparc_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED,
8676                     tree *args, bool ignore)
8677 {
8678   tree arg0, arg1, arg2;
8679   tree rtype = TREE_TYPE (TREE_TYPE (fndecl));
8680   enum insn_code icode = (enum insn_code) DECL_FUNCTION_CODE (fndecl);
8681
8682   if (ignore
8683       && icode != CODE_FOR_alignaddrsi_vis
8684       && icode != CODE_FOR_alignaddrdi_vis)
8685     return fold_convert (rtype, integer_zero_node);
8686
8687   switch (icode)
8688     {
8689     case CODE_FOR_fexpand_vis:
8690       arg0 = args[0];
8691       STRIP_NOPS (arg0);
8692
8693       if (TREE_CODE (arg0) == VECTOR_CST)
8694         {
8695           tree inner_type = TREE_TYPE (rtype);
8696           tree elts = TREE_VECTOR_CST_ELTS (arg0);
8697           tree n_elts = NULL_TREE;
8698
8699           for (; elts; elts = TREE_CHAIN (elts))
8700             {
8701               unsigned int val = TREE_INT_CST_LOW (TREE_VALUE (elts)) << 4;
8702               n_elts = tree_cons (NULL_TREE,
8703                                   build_int_cst (inner_type, val),
8704                                   n_elts);
8705             }
8706           return build_vector (rtype, nreverse (n_elts));
8707         }
8708       break;
8709
8710     case CODE_FOR_fmul8x16_vis:
8711     case CODE_FOR_fmul8x16au_vis:
8712     case CODE_FOR_fmul8x16al_vis:
8713       arg0 = args[0];
8714       arg1 = args[1];
8715       STRIP_NOPS (arg0);
8716       STRIP_NOPS (arg1);
8717
8718       if (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
8719         {
8720           tree inner_type = TREE_TYPE (rtype);
8721           tree elts0 = TREE_VECTOR_CST_ELTS (arg0);
8722           tree elts1 = TREE_VECTOR_CST_ELTS (arg1);
8723           tree n_elts = sparc_handle_vis_mul8x16 (icode, inner_type, elts0,
8724                                                   elts1);
8725
8726           return build_vector (rtype, n_elts);
8727         }
8728       break;
8729
8730     case CODE_FOR_fpmerge_vis:
8731       arg0 = args[0];
8732       arg1 = args[1];
8733       STRIP_NOPS (arg0);
8734       STRIP_NOPS (arg1);
8735
8736       if (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
8737         {
8738           tree elts0 = TREE_VECTOR_CST_ELTS (arg0);
8739           tree elts1 = TREE_VECTOR_CST_ELTS (arg1);
8740           tree n_elts = NULL_TREE;
8741
8742           for (; elts0 && elts1;
8743                elts0 = TREE_CHAIN (elts0), elts1 = TREE_CHAIN (elts1))
8744             {
8745               n_elts = tree_cons (NULL_TREE, TREE_VALUE (elts0), n_elts);
8746               n_elts = tree_cons (NULL_TREE, TREE_VALUE (elts1), n_elts);
8747             }
8748
8749           return build_vector (rtype, nreverse (n_elts));
8750         }
8751       break;
8752
8753     case CODE_FOR_pdist_vis:
8754       arg0 = args[0];
8755       arg1 = args[1];
8756       arg2 = args[2];
8757       STRIP_NOPS (arg0);
8758       STRIP_NOPS (arg1);
8759       STRIP_NOPS (arg2);
8760
8761       if (TREE_CODE (arg0) == VECTOR_CST
8762           && TREE_CODE (arg1) == VECTOR_CST
8763           && TREE_CODE (arg2) == INTEGER_CST)
8764         {
8765           int overflow = 0;
8766           unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg2);
8767           HOST_WIDE_INT high = TREE_INT_CST_HIGH (arg2);
8768           tree elts0 = TREE_VECTOR_CST_ELTS (arg0);
8769           tree elts1 = TREE_VECTOR_CST_ELTS (arg1);
8770
8771           for (; elts0 && elts1;
8772                elts0 = TREE_CHAIN (elts0), elts1 = TREE_CHAIN (elts1))
8773             {
8774               unsigned HOST_WIDE_INT
8775                 low0 = TREE_INT_CST_LOW (TREE_VALUE (elts0)),
8776                 low1 = TREE_INT_CST_LOW (TREE_VALUE (elts1));
8777               HOST_WIDE_INT high0 = TREE_INT_CST_HIGH (TREE_VALUE (elts0));
8778               HOST_WIDE_INT high1 = TREE_INT_CST_HIGH (TREE_VALUE (elts1));
8779
8780               unsigned HOST_WIDE_INT l;
8781               HOST_WIDE_INT h;
8782
8783               overflow |= neg_double (low1, high1, &l, &h);
8784               overflow |= add_double (low0, high0, l, h, &l, &h);
8785               if (h < 0)
8786                 overflow |= neg_double (l, h, &l, &h);
8787
8788               overflow |= add_double (low, high, l, h, &low, &high);
8789             }
8790
8791           gcc_assert (overflow == 0);
8792
8793           return build_int_cst_wide (rtype, low, high);
8794         }
8795
8796     default:
8797       break;
8798     }
8799
8800   return NULL_TREE;
8801 }
8802 \f
8803 /* ??? This duplicates information provided to the compiler by the
8804    ??? scheduler description.  Some day, teach genautomata to output
8805    ??? the latencies and then CSE will just use that.  */
8806
8807 static bool
8808 sparc_rtx_costs (rtx x, int code, int outer_code, int *total,
8809                  bool speed ATTRIBUTE_UNUSED)
8810 {
8811   enum machine_mode mode = GET_MODE (x);
8812   bool float_mode_p = FLOAT_MODE_P (mode);
8813
8814   switch (code)
8815     {
8816     case CONST_INT:
8817       if (INTVAL (x) < 0x1000 && INTVAL (x) >= -0x1000)
8818         {
8819           *total = 0;
8820           return true;
8821         }
8822       /* FALLTHRU */
8823
8824     case HIGH:
8825       *total = 2;
8826       return true;
8827
8828     case CONST:
8829     case LABEL_REF:
8830     case SYMBOL_REF:
8831       *total = 4;
8832       return true;
8833
8834     case CONST_DOUBLE:
8835       if (GET_MODE (x) == VOIDmode
8836           && ((CONST_DOUBLE_HIGH (x) == 0
8837                && CONST_DOUBLE_LOW (x) < 0x1000)
8838               || (CONST_DOUBLE_HIGH (x) == -1
8839                   && CONST_DOUBLE_LOW (x) < 0
8840                   && CONST_DOUBLE_LOW (x) >= -0x1000)))
8841         *total = 0;
8842       else
8843         *total = 8;
8844       return true;
8845
8846     case MEM:
8847       /* If outer-code was a sign or zero extension, a cost
8848          of COSTS_N_INSNS (1) was already added in.  This is
8849          why we are subtracting it back out.  */
8850       if (outer_code == ZERO_EXTEND)
8851         {
8852           *total = sparc_costs->int_zload - COSTS_N_INSNS (1);
8853         }
8854       else if (outer_code == SIGN_EXTEND)
8855         {
8856           *total = sparc_costs->int_sload - COSTS_N_INSNS (1);
8857         }
8858       else if (float_mode_p)
8859         {
8860           *total = sparc_costs->float_load;
8861         }
8862       else
8863         {
8864           *total = sparc_costs->int_load;
8865         }
8866
8867       return true;
8868
8869     case PLUS:
8870     case MINUS:
8871       if (float_mode_p)
8872         *total = sparc_costs->float_plusminus;
8873       else
8874         *total = COSTS_N_INSNS (1);
8875       return false;
8876
8877     case MULT:
8878       if (float_mode_p)
8879         *total = sparc_costs->float_mul;
8880       else if (! TARGET_HARD_MUL)
8881         *total = COSTS_N_INSNS (25);
8882       else
8883         {
8884           int bit_cost;
8885
8886           bit_cost = 0;
8887           if (sparc_costs->int_mul_bit_factor)
8888             {
8889               int nbits;
8890
8891               if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8892                 {
8893                   unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
8894                   for (nbits = 0; value != 0; value &= value - 1)
8895                     nbits++;
8896                 }
8897               else if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
8898                        && GET_MODE (XEXP (x, 1)) == VOIDmode)
8899                 {
8900                   rtx x1 = XEXP (x, 1);
8901                   unsigned HOST_WIDE_INT value1 = CONST_DOUBLE_LOW (x1);
8902                   unsigned HOST_WIDE_INT value2 = CONST_DOUBLE_HIGH (x1);
8903
8904                   for (nbits = 0; value1 != 0; value1 &= value1 - 1)
8905                     nbits++;
8906                   for (; value2 != 0; value2 &= value2 - 1)
8907                     nbits++;
8908                 }
8909               else
8910                 nbits = 7;
8911
8912               if (nbits < 3)
8913                 nbits = 3;
8914               bit_cost = (nbits - 3) / sparc_costs->int_mul_bit_factor;
8915               bit_cost = COSTS_N_INSNS (bit_cost);
8916             }
8917
8918           if (mode == DImode)
8919             *total = sparc_costs->int_mulX + bit_cost;
8920           else
8921             *total = sparc_costs->int_mul + bit_cost;
8922         }
8923       return false;
8924
8925     case ASHIFT:
8926     case ASHIFTRT:
8927     case LSHIFTRT:
8928       *total = COSTS_N_INSNS (1) + sparc_costs->shift_penalty;
8929       return false;
8930
8931     case DIV:
8932     case UDIV:
8933     case MOD:
8934     case UMOD:
8935       if (float_mode_p)
8936         {
8937           if (mode == DFmode)
8938             *total = sparc_costs->float_div_df;
8939           else
8940             *total = sparc_costs->float_div_sf;
8941         }
8942       else
8943         {
8944           if (mode == DImode)
8945             *total = sparc_costs->int_divX;
8946           else
8947             *total = sparc_costs->int_div;
8948         }
8949       return false;
8950
8951     case NEG:
8952       if (! float_mode_p)
8953         {
8954           *total = COSTS_N_INSNS (1);
8955           return false;
8956         }
8957       /* FALLTHRU */
8958
8959     case ABS:
8960     case FLOAT:
8961     case UNSIGNED_FLOAT:
8962     case FIX:
8963     case UNSIGNED_FIX:
8964     case FLOAT_EXTEND:
8965     case FLOAT_TRUNCATE:
8966       *total = sparc_costs->float_move;
8967       return false;
8968
8969     case SQRT:
8970       if (mode == DFmode)
8971         *total = sparc_costs->float_sqrt_df;
8972       else
8973         *total = sparc_costs->float_sqrt_sf;
8974       return false;
8975
8976     case COMPARE:
8977       if (float_mode_p)
8978         *total = sparc_costs->float_cmp;
8979       else
8980         *total = COSTS_N_INSNS (1);
8981       return false;
8982
8983     case IF_THEN_ELSE:
8984       if (float_mode_p)
8985         *total = sparc_costs->float_cmove;
8986       else
8987         *total = sparc_costs->int_cmove;
8988       return false;
8989
8990     case IOR:
8991       /* Handle the NAND vector patterns.  */
8992       if (sparc_vector_mode_supported_p (GET_MODE (x))
8993           && GET_CODE (XEXP (x, 0)) == NOT
8994           && GET_CODE (XEXP (x, 1)) == NOT)
8995         {
8996           *total = COSTS_N_INSNS (1);
8997           return true;
8998         }
8999       else
9000         return false;
9001
9002     default:
9003       return false;
9004     }
9005 }
9006
9007 /* Emit the sequence of insns SEQ while preserving the registers REG and REG2.
9008    This is achieved by means of a manual dynamic stack space allocation in
9009    the current frame.  We make the assumption that SEQ doesn't contain any
9010    function calls, with the possible exception of calls to the PIC helper.  */
9011
9012 static void
9013 emit_and_preserve (rtx seq, rtx reg, rtx reg2)
9014 {
9015   /* We must preserve the lowest 16 words for the register save area.  */
9016   HOST_WIDE_INT offset = 16*UNITS_PER_WORD;
9017   /* We really need only 2 words of fresh stack space.  */
9018   HOST_WIDE_INT size = SPARC_STACK_ALIGN (offset + 2*UNITS_PER_WORD);
9019
9020   rtx slot
9021     = gen_rtx_MEM (word_mode, plus_constant (stack_pointer_rtx,
9022                                              SPARC_STACK_BIAS + offset));
9023
9024   emit_insn (gen_stack_pointer_dec (GEN_INT (size)));
9025   emit_insn (gen_rtx_SET (VOIDmode, slot, reg));
9026   if (reg2)
9027     emit_insn (gen_rtx_SET (VOIDmode,
9028                             adjust_address (slot, word_mode, UNITS_PER_WORD),
9029                             reg2));
9030   emit_insn (seq);
9031   if (reg2)
9032     emit_insn (gen_rtx_SET (VOIDmode,
9033                             reg2,
9034                             adjust_address (slot, word_mode, UNITS_PER_WORD)));
9035   emit_insn (gen_rtx_SET (VOIDmode, reg, slot));
9036   emit_insn (gen_stack_pointer_inc (GEN_INT (size)));
9037 }
9038
9039 /* Output the assembler code for a thunk function.  THUNK_DECL is the
9040    declaration for the thunk function itself, FUNCTION is the decl for
9041    the target function.  DELTA is an immediate constant offset to be
9042    added to THIS.  If VCALL_OFFSET is nonzero, the word at address
9043    (*THIS + VCALL_OFFSET) should be additionally added to THIS.  */
9044
9045 static void
9046 sparc_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
9047                        HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
9048                        tree function)
9049 {
9050   rtx this_rtx, insn, funexp;
9051   unsigned int int_arg_first;
9052
9053   reload_completed = 1;
9054   epilogue_completed = 1;
9055
9056   emit_note (NOTE_INSN_PROLOGUE_END);
9057
9058   if (flag_delayed_branch)
9059     {
9060       /* We will emit a regular sibcall below, so we need to instruct
9061          output_sibcall that we are in a leaf function.  */
9062       sparc_leaf_function_p = current_function_uses_only_leaf_regs = 1;
9063
9064       /* This will cause final.c to invoke leaf_renumber_regs so we
9065          must behave as if we were in a not-yet-leafified function.  */
9066       int_arg_first = SPARC_INCOMING_INT_ARG_FIRST;
9067     }
9068   else
9069     {
9070       /* We will emit the sibcall manually below, so we will need to
9071          manually spill non-leaf registers.  */
9072       sparc_leaf_function_p = current_function_uses_only_leaf_regs = 0;
9073
9074       /* We really are in a leaf function.  */
9075       int_arg_first = SPARC_OUTGOING_INT_ARG_FIRST;
9076     }
9077
9078   /* Find the "this" pointer.  Normally in %o0, but in ARCH64 if the function
9079      returns a structure, the structure return pointer is there instead.  */
9080   if (TARGET_ARCH64
9081       && aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
9082     this_rtx = gen_rtx_REG (Pmode, int_arg_first + 1);
9083   else
9084     this_rtx = gen_rtx_REG (Pmode, int_arg_first);
9085
9086   /* Add DELTA.  When possible use a plain add, otherwise load it into
9087      a register first.  */
9088   if (delta)
9089     {
9090       rtx delta_rtx = GEN_INT (delta);
9091
9092       if (! SPARC_SIMM13_P (delta))
9093         {
9094           rtx scratch = gen_rtx_REG (Pmode, 1);
9095           emit_move_insn (scratch, delta_rtx);
9096           delta_rtx = scratch;
9097         }
9098
9099       /* THIS_RTX += DELTA.  */
9100       emit_insn (gen_add2_insn (this_rtx, delta_rtx));
9101     }
9102
9103   /* Add the word at address (*THIS_RTX + VCALL_OFFSET).  */
9104   if (vcall_offset)
9105     {
9106       rtx vcall_offset_rtx = GEN_INT (vcall_offset);
9107       rtx scratch = gen_rtx_REG (Pmode, 1);
9108
9109       gcc_assert (vcall_offset < 0);
9110
9111       /* SCRATCH = *THIS_RTX.  */
9112       emit_move_insn (scratch, gen_rtx_MEM (Pmode, this_rtx));
9113
9114       /* Prepare for adding VCALL_OFFSET.  The difficulty is that we
9115          may not have any available scratch register at this point.  */
9116       if (SPARC_SIMM13_P (vcall_offset))
9117         ;
9118       /* This is the case if ARCH64 (unless -ffixed-g5 is passed).  */
9119       else if (! fixed_regs[5]
9120                /* The below sequence is made up of at least 2 insns,
9121                   while the default method may need only one.  */
9122                && vcall_offset < -8192)
9123         {
9124           rtx scratch2 = gen_rtx_REG (Pmode, 5);
9125           emit_move_insn (scratch2, vcall_offset_rtx);
9126           vcall_offset_rtx = scratch2;
9127         }
9128       else
9129         {
9130           rtx increment = GEN_INT (-4096);
9131
9132           /* VCALL_OFFSET is a negative number whose typical range can be
9133              estimated as -32768..0 in 32-bit mode.  In almost all cases
9134              it is therefore cheaper to emit multiple add insns than
9135              spilling and loading the constant into a register (at least
9136              6 insns).  */
9137           while (! SPARC_SIMM13_P (vcall_offset))
9138             {
9139               emit_insn (gen_add2_insn (scratch, increment));
9140               vcall_offset += 4096;
9141             }
9142           vcall_offset_rtx = GEN_INT (vcall_offset); /* cannot be 0 */
9143         }
9144
9145       /* SCRATCH = *(*THIS_RTX + VCALL_OFFSET).  */
9146       emit_move_insn (scratch, gen_rtx_MEM (Pmode,
9147                                             gen_rtx_PLUS (Pmode,
9148                                                           scratch,
9149                                                           vcall_offset_rtx)));
9150
9151       /* THIS_RTX += *(*THIS_RTX + VCALL_OFFSET).  */
9152       emit_insn (gen_add2_insn (this_rtx, scratch));
9153     }
9154
9155   /* Generate a tail call to the target function.  */
9156   if (! TREE_USED (function))
9157     {
9158       assemble_external (function);
9159       TREE_USED (function) = 1;
9160     }
9161   funexp = XEXP (DECL_RTL (function), 0);
9162
9163   if (flag_delayed_branch)
9164     {
9165       funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
9166       insn = emit_call_insn (gen_sibcall (funexp));
9167       SIBLING_CALL_P (insn) = 1;
9168     }
9169   else
9170     {
9171       /* The hoops we have to jump through in order to generate a sibcall
9172          without using delay slots...  */
9173       rtx spill_reg, spill_reg2, seq, scratch = gen_rtx_REG (Pmode, 1);
9174
9175       if (flag_pic)
9176         {
9177           spill_reg = gen_rtx_REG (word_mode, 15);  /* %o7 */
9178           spill_reg2 = gen_rtx_REG (word_mode, PIC_OFFSET_TABLE_REGNUM);
9179           start_sequence ();
9180           /* Delay emitting the PIC helper function because it needs to
9181              change the section and we are emitting assembly code.  */
9182           load_pic_register ();  /* clobbers %o7 */
9183           scratch = sparc_legitimize_pic_address (funexp, scratch);
9184           seq = get_insns ();
9185           end_sequence ();
9186           emit_and_preserve (seq, spill_reg, spill_reg2);
9187         }
9188       else if (TARGET_ARCH32)
9189         {
9190           emit_insn (gen_rtx_SET (VOIDmode,
9191                                   scratch,
9192                                   gen_rtx_HIGH (SImode, funexp)));
9193           emit_insn (gen_rtx_SET (VOIDmode,
9194                                   scratch,
9195                                   gen_rtx_LO_SUM (SImode, scratch, funexp)));
9196         }
9197       else  /* TARGET_ARCH64 */
9198         {
9199           switch (sparc_cmodel)
9200             {
9201             case CM_MEDLOW:
9202             case CM_MEDMID:
9203               /* The destination can serve as a temporary.  */
9204               sparc_emit_set_symbolic_const64 (scratch, funexp, scratch);
9205               break;
9206
9207             case CM_MEDANY:
9208             case CM_EMBMEDANY:
9209               /* The destination cannot serve as a temporary.  */
9210               spill_reg = gen_rtx_REG (DImode, 15);  /* %o7 */
9211               start_sequence ();
9212               sparc_emit_set_symbolic_const64 (scratch, funexp, spill_reg);
9213               seq = get_insns ();
9214               end_sequence ();
9215               emit_and_preserve (seq, spill_reg, 0);
9216               break;
9217
9218             default:
9219               gcc_unreachable ();
9220             }
9221         }
9222
9223       emit_jump_insn (gen_indirect_jump (scratch));
9224     }
9225
9226   emit_barrier ();
9227
9228   /* Run just enough of rest_of_compilation to get the insns emitted.
9229      There's not really enough bulk here to make other passes such as
9230      instruction scheduling worth while.  Note that use_thunk calls
9231      assemble_start_function and assemble_end_function.  */
9232   insn = get_insns ();
9233   insn_locators_alloc ();
9234   shorten_branches (insn);
9235   final_start_function (insn, file, 1);
9236   final (insn, file, 1);
9237   final_end_function ();
9238
9239   reload_completed = 0;
9240   epilogue_completed = 0;
9241 }
9242
9243 /* Return true if sparc_output_mi_thunk would be able to output the
9244    assembler code for the thunk function specified by the arguments
9245    it is passed, and false otherwise.  */
9246 static bool
9247 sparc_can_output_mi_thunk (const_tree thunk_fndecl ATTRIBUTE_UNUSED,
9248                            HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
9249                            HOST_WIDE_INT vcall_offset,
9250                            const_tree function ATTRIBUTE_UNUSED)
9251 {
9252   /* Bound the loop used in the default method above.  */
9253   return (vcall_offset >= -32768 || ! fixed_regs[5]);
9254 }
9255
9256 /* How to allocate a 'struct machine_function'.  */
9257
9258 static struct machine_function *
9259 sparc_init_machine_status (void)
9260 {
9261   return ggc_alloc_cleared_machine_function ();
9262 }
9263
9264 /* Locate some local-dynamic symbol still in use by this function
9265    so that we can print its name in local-dynamic base patterns.  */
9266
9267 static const char *
9268 get_some_local_dynamic_name (void)
9269 {
9270   rtx insn;
9271
9272   if (cfun->machine->some_ld_name)
9273     return cfun->machine->some_ld_name;
9274
9275   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
9276     if (INSN_P (insn)
9277         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
9278       return cfun->machine->some_ld_name;
9279
9280   gcc_unreachable ();
9281 }
9282
9283 static int
9284 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
9285 {
9286   rtx x = *px;
9287
9288   if (x
9289       && GET_CODE (x) == SYMBOL_REF
9290       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
9291     {
9292       cfun->machine->some_ld_name = XSTR (x, 0);
9293       return 1;
9294     }
9295
9296   return 0;
9297 }
9298
9299 /* Handle the TARGET_DWARF_HANDLE_FRAME_UNSPEC hook.
9300    This is called from dwarf2out.c to emit call frame instructions
9301    for frame-related insns containing UNSPECs and UNSPEC_VOLATILEs. */
9302 static void
9303 sparc_dwarf_handle_frame_unspec (const char *label,
9304                                  rtx pattern ATTRIBUTE_UNUSED,
9305                                  int index ATTRIBUTE_UNUSED)
9306 {
9307   gcc_assert (index == UNSPECV_SAVEW);
9308   dwarf2out_window_save (label);
9309 }
9310
9311 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
9312    We need to emit DTP-relative relocations.  */
9313
9314 static void
9315 sparc_output_dwarf_dtprel (FILE *file, int size, rtx x)
9316 {
9317   switch (size)
9318     {
9319     case 4:
9320       fputs ("\t.word\t%r_tls_dtpoff32(", file);
9321       break;
9322     case 8:
9323       fputs ("\t.xword\t%r_tls_dtpoff64(", file);
9324       break;
9325     default:
9326       gcc_unreachable ();
9327     }
9328   output_addr_const (file, x);
9329   fputs (")", file);
9330 }
9331
9332 /* Do whatever processing is required at the end of a file.  */
9333
9334 static void
9335 sparc_file_end (void)
9336 {
9337   /* If need to emit the special PIC helper function, do so now.  */
9338   if (pic_helper_needed)
9339     {
9340       unsigned int regno = REGNO (pic_offset_table_rtx);
9341       const char *pic_name = reg_names[regno];
9342       char name[32];
9343 #ifdef DWARF2_UNWIND_INFO
9344       bool do_cfi;
9345 #endif
9346
9347       get_pc_thunk_name (name, regno);
9348       if (USE_HIDDEN_LINKONCE)
9349         {
9350           tree decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
9351                                   get_identifier (name),
9352                                   build_function_type (void_type_node,
9353                                                        void_list_node));
9354           DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
9355                                            NULL_TREE, void_type_node);
9356           TREE_STATIC (decl) = 1;
9357           make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
9358           DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
9359           DECL_VISIBILITY_SPECIFIED (decl) = 1;
9360           allocate_struct_function (decl, true);
9361           current_function_decl = decl;
9362           init_varasm_status ();
9363           assemble_start_function (decl, name);
9364         }
9365       else
9366         {
9367           const int align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
9368           switch_to_section (text_section);
9369           if (align > 0)
9370             ASM_OUTPUT_ALIGN (asm_out_file, align);
9371           ASM_OUTPUT_LABEL (asm_out_file, name);
9372         }
9373
9374 #ifdef DWARF2_UNWIND_INFO
9375       do_cfi = dwarf2out_do_cfi_asm ();
9376       if (do_cfi)
9377         fprintf (asm_out_file, "\t.cfi_startproc\n");
9378 #endif
9379       if (flag_delayed_branch)
9380         fprintf (asm_out_file, "\tjmp\t%%o7+8\n\t add\t%%o7, %s, %s\n",
9381                  pic_name, pic_name);
9382       else
9383         fprintf (asm_out_file, "\tadd\t%%o7, %s, %s\n\tjmp\t%%o7+8\n\t nop\n",
9384                  pic_name, pic_name);
9385 #ifdef DWARF2_UNWIND_INFO
9386       if (do_cfi)
9387         fprintf (asm_out_file, "\t.cfi_endproc\n");
9388 #endif
9389     }
9390
9391   if (NEED_INDICATE_EXEC_STACK)
9392     file_end_indicate_exec_stack ();
9393 }
9394
9395 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
9396 /* Implement TARGET_MANGLE_TYPE.  */
9397
9398 static const char *
9399 sparc_mangle_type (const_tree type)
9400 {
9401   if (!TARGET_64BIT
9402       && TYPE_MAIN_VARIANT (type) == long_double_type_node
9403       && TARGET_LONG_DOUBLE_128)
9404     return "g";
9405
9406   /* For all other types, use normal C++ mangling.  */
9407   return NULL;
9408 }
9409 #endif
9410
9411 /* Expand code to perform a 8 or 16-bit compare and swap by doing 32-bit
9412    compare and swap on the word containing the byte or half-word.  */
9413
9414 void
9415 sparc_expand_compare_and_swap_12 (rtx result, rtx mem, rtx oldval, rtx newval)
9416 {
9417   rtx addr1 = force_reg (Pmode, XEXP (mem, 0));
9418   rtx addr = gen_reg_rtx (Pmode);
9419   rtx off = gen_reg_rtx (SImode);
9420   rtx oldv = gen_reg_rtx (SImode);
9421   rtx newv = gen_reg_rtx (SImode);
9422   rtx oldvalue = gen_reg_rtx (SImode);
9423   rtx newvalue = gen_reg_rtx (SImode);
9424   rtx res = gen_reg_rtx (SImode);
9425   rtx resv = gen_reg_rtx (SImode);
9426   rtx memsi, val, mask, end_label, loop_label, cc;
9427
9428   emit_insn (gen_rtx_SET (VOIDmode, addr,
9429                           gen_rtx_AND (Pmode, addr1, GEN_INT (-4))));
9430
9431   if (Pmode != SImode)
9432     addr1 = gen_lowpart (SImode, addr1);
9433   emit_insn (gen_rtx_SET (VOIDmode, off,
9434                           gen_rtx_AND (SImode, addr1, GEN_INT (3))));
9435
9436   memsi = gen_rtx_MEM (SImode, addr);
9437   set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
9438   MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
9439
9440   val = force_reg (SImode, memsi);
9441
9442   emit_insn (gen_rtx_SET (VOIDmode, off,
9443                           gen_rtx_XOR (SImode, off,
9444                                        GEN_INT (GET_MODE (mem) == QImode
9445                                                 ? 3 : 2))));
9446
9447   emit_insn (gen_rtx_SET (VOIDmode, off,
9448                           gen_rtx_ASHIFT (SImode, off, GEN_INT (3))));
9449
9450   if (GET_MODE (mem) == QImode)
9451     mask = force_reg (SImode, GEN_INT (0xff));
9452   else
9453     mask = force_reg (SImode, GEN_INT (0xffff));
9454
9455   emit_insn (gen_rtx_SET (VOIDmode, mask,
9456                           gen_rtx_ASHIFT (SImode, mask, off)));
9457
9458   emit_insn (gen_rtx_SET (VOIDmode, val,
9459                           gen_rtx_AND (SImode, gen_rtx_NOT (SImode, mask),
9460                                        val)));
9461
9462   oldval = gen_lowpart (SImode, oldval);
9463   emit_insn (gen_rtx_SET (VOIDmode, oldv,
9464                           gen_rtx_ASHIFT (SImode, oldval, off)));
9465
9466   newval = gen_lowpart_common (SImode, newval);
9467   emit_insn (gen_rtx_SET (VOIDmode, newv,
9468                           gen_rtx_ASHIFT (SImode, newval, off)));
9469
9470   emit_insn (gen_rtx_SET (VOIDmode, oldv,
9471                           gen_rtx_AND (SImode, oldv, mask)));
9472
9473   emit_insn (gen_rtx_SET (VOIDmode, newv,
9474                           gen_rtx_AND (SImode, newv, mask)));
9475
9476   end_label = gen_label_rtx ();
9477   loop_label = gen_label_rtx ();
9478   emit_label (loop_label);
9479
9480   emit_insn (gen_rtx_SET (VOIDmode, oldvalue,
9481                           gen_rtx_IOR (SImode, oldv, val)));
9482
9483   emit_insn (gen_rtx_SET (VOIDmode, newvalue,
9484                           gen_rtx_IOR (SImode, newv, val)));
9485
9486   emit_insn (gen_sync_compare_and_swapsi (res, memsi, oldvalue, newvalue));
9487
9488   emit_cmp_and_jump_insns (res, oldvalue, EQ, NULL, SImode, 0, end_label);
9489
9490   emit_insn (gen_rtx_SET (VOIDmode, resv,
9491                           gen_rtx_AND (SImode, gen_rtx_NOT (SImode, mask),
9492                                        res)));
9493
9494   cc = gen_compare_reg_1 (NE, resv, val);
9495   emit_insn (gen_rtx_SET (VOIDmode, val, resv));
9496
9497   /* Use cbranchcc4 to separate the compare and branch!  */
9498   emit_jump_insn (gen_cbranchcc4 (gen_rtx_NE (VOIDmode, cc, const0_rtx),
9499                                   cc, const0_rtx, loop_label));
9500
9501   emit_label (end_label);
9502
9503   emit_insn (gen_rtx_SET (VOIDmode, res,
9504                           gen_rtx_AND (SImode, res, mask)));
9505
9506   emit_insn (gen_rtx_SET (VOIDmode, res,
9507                           gen_rtx_LSHIFTRT (SImode, res, off)));
9508
9509   emit_move_insn (result, gen_lowpart (GET_MODE (result), res));
9510 }
9511
9512 /* Implement TARGET_FRAME_POINTER_REQUIRED.  */
9513
9514 bool
9515 sparc_frame_pointer_required (void)
9516 {
9517   return !(leaf_function_p () && only_leaf_regs_used ());
9518 }
9519
9520 /* The way this is structured, we can't eliminate SFP in favor of SP
9521    if the frame pointer is required: we want to use the SFP->HFP elimination
9522    in that case.  But the test in update_eliminables doesn't know we are
9523    assuming below that we only do the former elimination.  */
9524
9525 bool
9526 sparc_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
9527 {
9528   return (to == HARD_FRAME_POINTER_REGNUM
9529           || !targetm.frame_pointer_required ());
9530 }
9531
9532 #include "gt-sparc.h"