OSDN Git Service

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