OSDN Git Service

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