OSDN Git Service

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