OSDN Git Service

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