OSDN Git Service

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