OSDN Git Service

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