OSDN Git Service

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