OSDN Git Service

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