OSDN Git Service

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