OSDN Git Service

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