OSDN Git Service

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