OSDN Git Service

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