OSDN Git Service

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