OSDN Git Service

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