OSDN Git Service

2002-04-12 Eric Norum <eric.norum@usask.ca>
[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 == 0xffffffff)
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 a DESIRED 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 ATTRIBUTE_UNUSED;
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
3707          We have to still output a dummy nop for the sake of
3708          sane backtraces.  Otherwise, if the last two instructions
3709          of a function were call foo; dslot; this can make the return
3710          PC of foo (ie. address of call instruction plus 8) point to
3711          the first instruction in the next function.  */
3712       rtx insn;
3713
3714       fputs("\tnop\n", file);
3715
3716       insn = get_last_insn ();
3717       if (GET_CODE (insn) == NOTE)
3718               insn = prev_nonnote_insn (insn);
3719       if (insn && GET_CODE (insn) == BARRIER)
3720               goto output_vectors;
3721     }
3722
3723   if (num_gfregs)
3724     output_restore_regs (file, leaf_function);
3725
3726   /* Work out how to skip the caller's unimp instruction if required.  */
3727   if (leaf_function)
3728     ret = (SKIP_CALLERS_UNIMP_P ? "jmp\t%o7+12" : "retl");
3729   else
3730     ret = (SKIP_CALLERS_UNIMP_P ? "jmp\t%i7+12" : "ret");
3731
3732   if (! leaf_function)
3733     {
3734       if (current_function_calls_eh_return)
3735         {
3736           if (current_function_epilogue_delay_list)
3737             abort ();
3738           if (SKIP_CALLERS_UNIMP_P)
3739             abort ();
3740
3741           fputs ("\trestore\n\tretl\n\tadd\t%sp, %g1, %sp\n", file);
3742         }
3743       /* If we wound up with things in our delay slot, flush them here.  */
3744       else if (current_function_epilogue_delay_list)
3745         {
3746           rtx delay = PATTERN (XEXP (current_function_epilogue_delay_list, 0));
3747
3748           if (TARGET_V9 && ! epilogue_renumber (&delay, 1))
3749             {
3750               epilogue_renumber (&delay, 0);
3751               fputs (SKIP_CALLERS_UNIMP_P
3752                      ? "\treturn\t%i7+12\n"
3753                      : "\treturn\t%i7+8\n", file);
3754               final_scan_insn (XEXP (current_function_epilogue_delay_list, 0),
3755                                file, 1, 0, 0);
3756             }
3757           else
3758             {
3759               rtx insn, src;
3760
3761               if (GET_CODE (delay) != SET)
3762                 abort();
3763
3764               src = SET_SRC (delay);
3765               if (GET_CODE (src) == ASHIFT)
3766                 {
3767                   if (XEXP (src, 1) != const1_rtx)
3768                     abort();
3769                   SET_SRC (delay)
3770                     = gen_rtx_PLUS (GET_MODE (src), XEXP (src, 0),
3771                                     XEXP (src, 0));
3772                 }
3773
3774               insn = gen_rtx_PARALLEL (VOIDmode,
3775                                        gen_rtvec (2, delay,
3776                                                   gen_rtx_RETURN (VOIDmode)));
3777               insn = emit_jump_insn (insn);
3778
3779               sparc_emitting_epilogue = true;
3780               final_scan_insn (insn, file, 1, 0, 1);
3781               sparc_emitting_epilogue = false;
3782             }
3783         }
3784       else if (TARGET_V9 && ! SKIP_CALLERS_UNIMP_P)
3785         fputs ("\treturn\t%i7+8\n\tnop\n", file);
3786       else
3787         fprintf (file, "\t%s\n\trestore\n", ret);
3788     }
3789   /* All of the following cases are for leaf functions.  */
3790   else if (current_function_calls_eh_return)
3791     abort ();
3792   else if (current_function_epilogue_delay_list)
3793     {
3794       /* eligible_for_epilogue_delay_slot ensures that if this is a
3795          leaf function, then we will only have insn in the delay slot
3796          if the frame size is zero, thus no adjust for the stack is
3797          needed here.  */
3798       if (actual_fsize != 0)
3799         abort ();
3800       fprintf (file, "\t%s\n", ret);
3801       final_scan_insn (XEXP (current_function_epilogue_delay_list, 0),
3802                        file, 1, 0, 1);
3803     }
3804   /* Output 'nop' instead of 'sub %sp,-0,%sp' when no frame, so as to
3805          avoid generating confusing assembly language output.  */
3806   else if (actual_fsize == 0)
3807     fprintf (file, "\t%s\n\tnop\n", ret);
3808   else if (actual_fsize <= 4096)
3809     fprintf (file, "\t%s\n\tsub\t%%sp, -%d, %%sp\n", ret, actual_fsize);
3810   else if (actual_fsize <= 8192)
3811     fprintf (file, "\tsub\t%%sp, -4096, %%sp\n\t%s\n\tsub\t%%sp, -%d, %%sp\n",
3812              ret, actual_fsize - 4096);
3813   else if ((actual_fsize & 0x3ff) == 0)
3814     fprintf (file, "\tsethi\t%%hi(%d), %%g1\n\t%s\n\tadd\t%%sp, %%g1, %%sp\n",
3815              actual_fsize, ret);
3816   else           
3817     fprintf (file, "\tsethi\t%%hi(%d), %%g1\n\tor\t%%g1, %%lo(%d), %%g1\n\t%s\n\tadd\t%%sp, %%g1, %%sp\n",
3818              actual_fsize, actual_fsize, ret);
3819
3820  output_vectors:
3821   sparc_output_deferred_case_vectors ();
3822 }
3823
3824 /* Output a sibling call.  */
3825
3826 const char *
3827 output_sibcall (insn, call_operand)
3828      rtx insn, call_operand;
3829 {
3830   int leaf_regs = current_function_uses_only_leaf_regs;
3831   rtx operands[3];
3832   int delay_slot = dbr_sequence_length () > 0;
3833
3834   if (num_gfregs)
3835     {
3836       /* Call to restore global regs might clobber
3837          the delay slot. Instead of checking for this
3838          output the delay slot now.  */
3839       if (delay_slot)
3840         {
3841           rtx delay = NEXT_INSN (insn);
3842
3843           if (! delay)
3844             abort ();
3845
3846           final_scan_insn (delay, asm_out_file, 1, 0, 1);
3847           PATTERN (delay) = gen_blockage ();
3848           INSN_CODE (delay) = -1;
3849           delay_slot = 0;
3850         }
3851       output_restore_regs (asm_out_file, leaf_regs);
3852     }
3853
3854   operands[0] = call_operand;
3855
3856   if (leaf_regs)
3857     {
3858 #ifdef HAVE_AS_RELAX_OPTION
3859       /* If as and ld are relaxing tail call insns into branch always,
3860          use or %o7,%g0,X; call Y; or X,%g0,%o7 always, so that it can
3861          be optimized.  With sethi/jmpl as nor ld has no easy way how to
3862          find out if somebody does not branch between the sethi and jmpl.  */
3863       int spare_slot = 0;
3864 #else
3865       int spare_slot = ((TARGET_ARCH32 || TARGET_CM_MEDLOW) && ! flag_pic);
3866 #endif
3867       int size = 0;
3868
3869       if ((actual_fsize || ! spare_slot) && delay_slot)
3870         {
3871           rtx delay = NEXT_INSN (insn);
3872
3873           if (! delay)
3874             abort ();
3875
3876           final_scan_insn (delay, asm_out_file, 1, 0, 1);
3877           PATTERN (delay) = gen_blockage ();
3878           INSN_CODE (delay) = -1;
3879           delay_slot = 0;
3880         }
3881       if (actual_fsize)
3882         {
3883           if (actual_fsize <= 4096)
3884             size = actual_fsize;
3885           else if (actual_fsize <= 8192)
3886             {
3887               fputs ("\tsub\t%sp, -4096, %sp\n", asm_out_file);
3888               size = actual_fsize - 4096;
3889             }
3890           else if ((actual_fsize & 0x3ff) == 0)
3891             fprintf (asm_out_file,
3892                      "\tsethi\t%%hi(%d), %%g1\n\tadd\t%%sp, %%g1, %%sp\n",
3893                      actual_fsize);
3894           else
3895             {
3896               fprintf (asm_out_file,
3897                        "\tsethi\t%%hi(%d), %%g1\n\tor\t%%g1, %%lo(%d), %%g1\n",
3898                        actual_fsize, actual_fsize);
3899               fputs ("\tadd\t%%sp, %%g1, %%sp\n", asm_out_file);
3900             }
3901         }
3902       if (spare_slot)
3903         {
3904           output_asm_insn ("sethi\t%%hi(%a0), %%g1", operands);
3905           output_asm_insn ("jmpl\t%%g1 + %%lo(%a0), %%g0", operands);
3906           if (size)
3907             fprintf (asm_out_file, "\t sub\t%%sp, -%d, %%sp\n", size);
3908           else if (! delay_slot)
3909             fputs ("\t nop\n", asm_out_file);
3910         }
3911       else
3912         {
3913           if (size)
3914             fprintf (asm_out_file, "\tsub\t%%sp, -%d, %%sp\n", size);
3915           /* Use or with rs2 %%g0 instead of mov, so that as/ld can optimize
3916              it into branch if possible.  */
3917           output_asm_insn ("or\t%%o7, %%g0, %%g1", operands);
3918           output_asm_insn ("call\t%a0, 0", operands);
3919           output_asm_insn (" or\t%%g1, %%g0, %%o7", operands);
3920         }
3921       return "";
3922     }
3923
3924   output_asm_insn ("call\t%a0, 0", operands);
3925   if (delay_slot)
3926     {
3927       rtx delay = NEXT_INSN (insn), pat;
3928
3929       if (! delay)
3930         abort ();
3931
3932       pat = PATTERN (delay);
3933       if (GET_CODE (pat) != SET)
3934         abort ();
3935
3936       operands[0] = SET_DEST (pat);
3937       pat = SET_SRC (pat);
3938       switch (GET_CODE (pat))
3939         {
3940         case PLUS:
3941           operands[1] = XEXP (pat, 0);
3942           operands[2] = XEXP (pat, 1);
3943           output_asm_insn (" restore %r1, %2, %Y0", operands);
3944           break;
3945         case LO_SUM:
3946           operands[1] = XEXP (pat, 0);
3947           operands[2] = XEXP (pat, 1);
3948           output_asm_insn (" restore %r1, %%lo(%a2), %Y0", operands);
3949           break;
3950         case ASHIFT:
3951           operands[1] = XEXP (pat, 0);
3952           output_asm_insn (" restore %r1, %r1, %Y0", operands);
3953           break;
3954         default:
3955           operands[1] = pat;
3956           output_asm_insn (" restore %%g0, %1, %Y0", operands);
3957           break;
3958         }
3959       PATTERN (delay) = gen_blockage ();
3960       INSN_CODE (delay) = -1;
3961     }
3962   else
3963     fputs ("\t restore\n", asm_out_file);
3964   return "";
3965 }
3966 \f
3967 /* Functions for handling argument passing.
3968
3969    For v8 the first six args are normally in registers and the rest are
3970    pushed.  Any arg that starts within the first 6 words is at least
3971    partially passed in a register unless its data type forbids.
3972
3973    For v9, the argument registers are laid out as an array of 16 elements
3974    and arguments are added sequentially.  The first 6 int args and up to the
3975    first 16 fp args (depending on size) are passed in regs.
3976
3977    Slot    Stack   Integral   Float   Float in structure   Double   Long Double
3978    ----    -----   --------   -----   ------------------   ------   -----------
3979     15   [SP+248]              %f31       %f30,%f31         %d30
3980     14   [SP+240]              %f29       %f28,%f29         %d28       %q28
3981     13   [SP+232]              %f27       %f26,%f27         %d26
3982     12   [SP+224]              %f25       %f24,%f25         %d24       %q24
3983     11   [SP+216]              %f23       %f22,%f23         %d22
3984     10   [SP+208]              %f21       %f20,%f21         %d20       %q20
3985      9   [SP+200]              %f19       %f18,%f19         %d18
3986      8   [SP+192]              %f17       %f16,%f17         %d16       %q16
3987      7   [SP+184]              %f15       %f14,%f15         %d14
3988      6   [SP+176]              %f13       %f12,%f13         %d12       %q12
3989      5   [SP+168]     %o5      %f11       %f10,%f11         %d10
3990      4   [SP+160]     %o4       %f9        %f8,%f9           %d8        %q8
3991      3   [SP+152]     %o3       %f7        %f6,%f7           %d6
3992      2   [SP+144]     %o2       %f5        %f4,%f5           %d4        %q4
3993      1   [SP+136]     %o1       %f3        %f2,%f3           %d2
3994      0   [SP+128]     %o0       %f1        %f0,%f1           %d0        %q0
3995
3996    Here SP = %sp if -mno-stack-bias or %sp+stack_bias otherwise.
3997
3998    Integral arguments are always passed as 64 bit quantities appropriately
3999    extended.
4000
4001    Passing of floating point values is handled as follows.
4002    If a prototype is in scope:
4003      If the value is in a named argument (i.e. not a stdarg function or a
4004      value not part of the `...') then the value is passed in the appropriate
4005      fp reg.
4006      If the value is part of the `...' and is passed in one of the first 6
4007      slots then the value is passed in the appropriate int reg.
4008      If the value is part of the `...' and is not passed in one of the first 6
4009      slots then the value is passed in memory.
4010    If a prototype is not in scope:
4011      If the value is one of the first 6 arguments the value is passed in the
4012      appropriate integer reg and the appropriate fp reg.
4013      If the value is not one of the first 6 arguments the value is passed in
4014      the appropriate fp reg and in memory.
4015    */
4016
4017 /* Maximum number of int regs for args.  */
4018 #define SPARC_INT_ARG_MAX 6
4019 /* Maximum number of fp regs for args.  */
4020 #define SPARC_FP_ARG_MAX 16
4021
4022 #define ROUND_ADVANCE(SIZE) (((SIZE) + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
4023
4024 /* Handle the INIT_CUMULATIVE_ARGS macro.
4025    Initialize a variable CUM of type CUMULATIVE_ARGS
4026    for a call to a function whose data type is FNTYPE.
4027    For a library call, FNTYPE is 0.  */
4028
4029 void
4030 init_cumulative_args (cum, fntype, libname, indirect)
4031      CUMULATIVE_ARGS *cum;
4032      tree fntype;
4033      rtx libname ATTRIBUTE_UNUSED;
4034      int indirect ATTRIBUTE_UNUSED;
4035 {
4036   cum->words = 0;
4037   cum->prototype_p = fntype && TYPE_ARG_TYPES (fntype);
4038   cum->libcall_p = fntype == 0;
4039 }
4040
4041 /* Compute the slot number to pass an argument in.
4042    Returns the slot number or -1 if passing on the stack.
4043
4044    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4045     the preceding args and about the function being called.
4046    MODE is the argument's machine mode.
4047    TYPE is the data type of the argument (as a tree).
4048     This is null for libcalls where that information may
4049     not be available.
4050    NAMED is nonzero if this argument is a named parameter
4051     (otherwise it is an extra parameter matching an ellipsis).
4052    INCOMING_P is zero for FUNCTION_ARG, nonzero for FUNCTION_INCOMING_ARG.
4053    *PREGNO records the register number to use if scalar type.
4054    *PPADDING records the amount of padding needed in words.  */
4055
4056 static int
4057 function_arg_slotno (cum, mode, type, named, incoming_p, pregno, ppadding)
4058      const CUMULATIVE_ARGS *cum;
4059      enum machine_mode mode;
4060      tree type;
4061      int named;
4062      int incoming_p;
4063      int *pregno;
4064      int *ppadding;
4065 {
4066   int regbase = (incoming_p
4067                  ? SPARC_INCOMING_INT_ARG_FIRST
4068                  : SPARC_OUTGOING_INT_ARG_FIRST);
4069   int slotno = cum->words;
4070   int regno;
4071
4072   *ppadding = 0;
4073
4074   if (type != 0 && TREE_ADDRESSABLE (type))
4075     return -1;
4076   if (TARGET_ARCH32
4077       && type != 0 && mode == BLKmode
4078       && TYPE_ALIGN (type) % PARM_BOUNDARY != 0)
4079     return -1;
4080
4081   switch (mode)
4082     {
4083     case VOIDmode :
4084       /* MODE is VOIDmode when generating the actual call.
4085          See emit_call_1.  */
4086       return -1;
4087
4088     case QImode : case CQImode :
4089     case HImode : case CHImode :
4090     case SImode : case CSImode :
4091     case DImode : case CDImode :
4092     case TImode : case CTImode :
4093       if (slotno >= SPARC_INT_ARG_MAX)
4094         return -1;
4095       regno = regbase + slotno;
4096       break;
4097
4098     case SFmode : case SCmode :
4099     case DFmode : case DCmode :
4100     case TFmode : case TCmode :
4101       if (TARGET_ARCH32)
4102         {
4103           if (slotno >= SPARC_INT_ARG_MAX)
4104             return -1;
4105           regno = regbase + slotno;
4106         }
4107       else
4108         {
4109           if ((mode == TFmode || mode == TCmode)
4110               && (slotno & 1) != 0)
4111             slotno++, *ppadding = 1;
4112           if (TARGET_FPU && named)
4113             {
4114               if (slotno >= SPARC_FP_ARG_MAX)
4115                 return -1;
4116               regno = SPARC_FP_ARG_FIRST + slotno * 2;
4117               if (mode == SFmode)
4118                 regno++;
4119             }
4120           else
4121             {
4122               if (slotno >= SPARC_INT_ARG_MAX)
4123                 return -1;
4124               regno = regbase + slotno;
4125             }
4126         }
4127       break;
4128
4129     case BLKmode :
4130       /* For sparc64, objects requiring 16 byte alignment get it.  */
4131       if (TARGET_ARCH64)
4132         {
4133           if (type && TYPE_ALIGN (type) == 128 && (slotno & 1) != 0)
4134             slotno++, *ppadding = 1;
4135         }
4136
4137       if (TARGET_ARCH32
4138           || (type && TREE_CODE (type) == UNION_TYPE))
4139         {
4140           if (slotno >= SPARC_INT_ARG_MAX)
4141             return -1;
4142           regno = regbase + slotno;
4143         }
4144       else
4145         {
4146           tree field;
4147           int intregs_p = 0, fpregs_p = 0;
4148           /* The ABI obviously doesn't specify how packed
4149              structures are passed.  These are defined to be passed
4150              in int regs if possible, otherwise memory.  */
4151           int packed_p = 0;
4152
4153           /* First see what kinds of registers we need.  */
4154           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4155             {
4156               if (TREE_CODE (field) == FIELD_DECL)
4157                 {
4158                   if (TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4159                       && TARGET_FPU)
4160                     fpregs_p = 1;
4161                   else
4162                     intregs_p = 1;
4163                   if (DECL_PACKED (field))
4164                     packed_p = 1;
4165                 }
4166             }
4167           if (packed_p || !named)
4168             fpregs_p = 0, intregs_p = 1;
4169
4170           /* If all arg slots are filled, then must pass on stack.  */
4171           if (fpregs_p && slotno >= SPARC_FP_ARG_MAX)
4172             return -1;
4173           /* If there are only int args and all int arg slots are filled,
4174              then must pass on stack.  */
4175           if (!fpregs_p && intregs_p && slotno >= SPARC_INT_ARG_MAX)
4176             return -1;
4177           /* Note that even if all int arg slots are filled, fp members may
4178              still be passed in regs if such regs are available.
4179              *PREGNO isn't set because there may be more than one, it's up
4180              to the caller to compute them.  */
4181           return slotno;
4182         }
4183       break;
4184
4185     default :
4186       abort ();
4187     }
4188
4189   *pregno = regno;
4190   return slotno;
4191 }
4192
4193 /* Handle recursive register counting for structure field layout.  */
4194
4195 struct function_arg_record_value_parms
4196 {
4197   rtx ret;
4198   int slotno, named, regbase;
4199   unsigned int nregs;
4200   int intoffset;
4201 };
4202
4203 static void function_arg_record_value_3
4204         PARAMS ((HOST_WIDE_INT, struct function_arg_record_value_parms *));
4205 static void function_arg_record_value_2
4206         PARAMS ((tree, HOST_WIDE_INT,
4207                  struct function_arg_record_value_parms *));
4208 static void function_arg_record_value_1
4209         PARAMS ((tree, HOST_WIDE_INT,
4210                  struct function_arg_record_value_parms *));
4211 static rtx function_arg_record_value
4212         PARAMS ((tree, enum machine_mode, int, int, int));
4213
4214 /* A subroutine of function_arg_record_value.  Traverse the structure
4215    recusively and determine how many registers will be required.  */
4216
4217 static void
4218 function_arg_record_value_1 (type, startbitpos, parms)
4219      tree type;
4220      HOST_WIDE_INT startbitpos;
4221      struct function_arg_record_value_parms *parms;
4222 {
4223   tree field;
4224
4225   /* The ABI obviously doesn't specify how packed structures are
4226      passed.  These are defined to be passed in int regs if possible,
4227      otherwise memory.  */
4228   int packed_p = 0;
4229
4230   /* We need to compute how many registers are needed so we can
4231      allocate the PARALLEL but before we can do that we need to know
4232      whether there are any packed fields.  If there are, int regs are
4233      used regardless of whether there are fp values present.  */
4234   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4235     {
4236       if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field))
4237         {
4238           packed_p = 1;
4239           break;
4240         }
4241     }
4242
4243   /* Compute how many registers we need.  */
4244   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4245     {
4246       if (TREE_CODE (field) == FIELD_DECL)
4247         {
4248           HOST_WIDE_INT bitpos = startbitpos;
4249
4250           if (DECL_SIZE (field) != 0
4251               && host_integerp (bit_position (field), 1))
4252             bitpos += int_bit_position (field);
4253
4254           /* ??? FIXME: else assume zero offset.  */
4255
4256           if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
4257             function_arg_record_value_1 (TREE_TYPE (field), bitpos, parms);
4258           else if ((TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4259                     || (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE
4260                         && (TREE_CODE (TREE_TYPE (TREE_TYPE (field)))
4261                             == REAL_TYPE)))
4262                    && TARGET_FPU
4263                    && ! packed_p
4264                    && parms->named)
4265             {
4266               if (parms->intoffset != -1)
4267                 {
4268                   int intslots, this_slotno;
4269
4270                   intslots = (bitpos - parms->intoffset + BITS_PER_WORD - 1)
4271                     / BITS_PER_WORD;
4272                   this_slotno = parms->slotno + parms->intoffset
4273                     / BITS_PER_WORD;
4274
4275                   intslots = MIN (intslots, SPARC_INT_ARG_MAX - this_slotno);
4276                   intslots = MAX (intslots, 0);
4277                   parms->nregs += intslots;
4278                   parms->intoffset = -1;
4279                 }
4280
4281               /* There's no need to check this_slotno < SPARC_FP_ARG MAX.
4282                  If it wasn't true we wouldn't be here.  */
4283               parms->nregs += 1;
4284               if (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE)
4285                 parms->nregs += 1;
4286             }
4287           else
4288             {
4289               if (parms->intoffset == -1)
4290                 parms->intoffset = bitpos;
4291             }
4292         }
4293     }
4294 }
4295
4296 /* A subroutine of function_arg_record_value.  Assign the bits of the
4297    structure between parms->intoffset and bitpos to integer registers.  */
4298
4299 static void 
4300 function_arg_record_value_3 (bitpos, parms)
4301      HOST_WIDE_INT bitpos;
4302      struct function_arg_record_value_parms *parms;
4303 {
4304   enum machine_mode mode;
4305   unsigned int regno;
4306   unsigned int startbit, endbit;
4307   int this_slotno, intslots, intoffset;
4308   rtx reg;
4309
4310   if (parms->intoffset == -1)
4311     return;
4312
4313   intoffset = parms->intoffset;
4314   parms->intoffset = -1;
4315
4316   startbit = intoffset & -BITS_PER_WORD;
4317   endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
4318   intslots = (endbit - startbit) / BITS_PER_WORD;
4319   this_slotno = parms->slotno + intoffset / BITS_PER_WORD;
4320
4321   intslots = MIN (intslots, SPARC_INT_ARG_MAX - this_slotno);
4322   if (intslots <= 0)
4323     return;
4324
4325   /* If this is the trailing part of a word, only load that much into
4326      the register.  Otherwise load the whole register.  Note that in
4327      the latter case we may pick up unwanted bits.  It's not a problem
4328      at the moment but may wish to revisit.  */
4329
4330   if (intoffset % BITS_PER_WORD != 0)
4331     mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
4332                           MODE_INT, 0);
4333   else
4334     mode = word_mode;
4335
4336   intoffset /= BITS_PER_UNIT;
4337   do
4338     {
4339       regno = parms->regbase + this_slotno;
4340       reg = gen_rtx_REG (mode, regno);
4341       XVECEXP (parms->ret, 0, parms->nregs)
4342         = gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
4343
4344       this_slotno += 1;
4345       intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
4346       parms->nregs += 1;
4347       intslots -= 1;
4348     }
4349   while (intslots > 0);
4350 }
4351
4352 /* A subroutine of function_arg_record_value.  Traverse the structure
4353    recursively and assign bits to floating point registers.  Track which
4354    bits in between need integer registers; invoke function_arg_record_value_3
4355    to make that happen.  */
4356
4357 static void
4358 function_arg_record_value_2 (type, startbitpos, parms)
4359      tree type;
4360      HOST_WIDE_INT startbitpos;
4361      struct function_arg_record_value_parms *parms;
4362 {
4363   tree field;
4364   int packed_p = 0;
4365
4366   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4367     {
4368       if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field))
4369         {
4370           packed_p = 1;
4371           break;
4372         }
4373     }
4374
4375   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4376     {
4377       if (TREE_CODE (field) == FIELD_DECL)
4378         {
4379           HOST_WIDE_INT bitpos = startbitpos;
4380
4381           if (DECL_SIZE (field) != 0
4382               && host_integerp (bit_position (field), 1))
4383             bitpos += int_bit_position (field);
4384
4385           /* ??? FIXME: else assume zero offset.  */
4386
4387           if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
4388             function_arg_record_value_2 (TREE_TYPE (field), bitpos, parms);
4389           else if ((TREE_CODE (TREE_TYPE (field)) == REAL_TYPE
4390                     || (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE
4391                         && (TREE_CODE (TREE_TYPE (TREE_TYPE (field)))
4392                             == REAL_TYPE)))
4393                    && TARGET_FPU
4394                    && ! packed_p
4395                    && parms->named)
4396             {
4397               int this_slotno = parms->slotno + bitpos / BITS_PER_WORD;
4398               int regno;
4399               enum machine_mode mode = DECL_MODE (field);
4400               rtx reg;
4401
4402               function_arg_record_value_3 (bitpos, parms);
4403               regno = SPARC_FP_ARG_FIRST + this_slotno * 2
4404                       + ((mode == SFmode || mode == SCmode)
4405                          && (bitpos & 32) != 0);
4406               switch (mode)
4407                 {
4408                 case SCmode: mode = SFmode; break;
4409                 case DCmode: mode = DFmode; break;
4410                 case TCmode: mode = TFmode; break;
4411                 default: break;
4412                 }
4413               reg = gen_rtx_REG (mode, regno);
4414               XVECEXP (parms->ret, 0, parms->nregs)
4415                 = gen_rtx_EXPR_LIST (VOIDmode, reg,
4416                            GEN_INT (bitpos / BITS_PER_UNIT));
4417               parms->nregs += 1;
4418               if (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE)
4419                 {
4420                   regno += GET_MODE_SIZE (mode) / 4;
4421                   reg = gen_rtx_REG (mode, regno);
4422                   XVECEXP (parms->ret, 0, parms->nregs)
4423                     = gen_rtx_EXPR_LIST (VOIDmode, reg,
4424                         GEN_INT ((bitpos + GET_MODE_BITSIZE (mode))
4425                                  / BITS_PER_UNIT));
4426                   parms->nregs += 1;
4427                 }
4428             }
4429           else
4430             {
4431               if (parms->intoffset == -1)
4432                 parms->intoffset = bitpos;
4433             }
4434         }
4435     }
4436 }
4437
4438 /* Used by function_arg and function_value to implement the complex
4439    Sparc64 structure calling conventions.  */
4440
4441 static rtx
4442 function_arg_record_value (type, mode, slotno, named, regbase)
4443      tree type;
4444      enum machine_mode mode;
4445      int slotno, named, regbase;
4446 {
4447   HOST_WIDE_INT typesize = int_size_in_bytes (type);
4448   struct function_arg_record_value_parms parms;
4449   unsigned int nregs;
4450
4451   parms.ret = NULL_RTX;
4452   parms.slotno = slotno;
4453   parms.named = named;
4454   parms.regbase = regbase;
4455
4456   /* Compute how many registers we need.  */
4457   parms.nregs = 0;
4458   parms.intoffset = 0;
4459   function_arg_record_value_1 (type, 0, &parms);
4460
4461   if (parms.intoffset != -1)
4462     {
4463       unsigned int startbit, endbit;
4464       int intslots, this_slotno;
4465
4466       startbit = parms.intoffset & -BITS_PER_WORD;
4467       endbit = (typesize*BITS_PER_UNIT + BITS_PER_WORD - 1) & -BITS_PER_WORD;
4468       intslots = (endbit - startbit) / BITS_PER_WORD;
4469       this_slotno = slotno + parms.intoffset / BITS_PER_WORD;
4470
4471       intslots = MIN (intslots, SPARC_INT_ARG_MAX - this_slotno);
4472       intslots = MAX (intslots, 0);
4473
4474       parms.nregs += intslots;
4475     }
4476   nregs = parms.nregs;
4477
4478   /* Allocate the vector and handle some annoying special cases.  */
4479   if (nregs == 0)
4480     {
4481       /* ??? Empty structure has no value?  Duh?  */
4482       if (typesize <= 0)
4483         {
4484           /* Though there's nothing really to store, return a word register
4485              anyway so the rest of gcc doesn't go nuts.  Returning a PARALLEL
4486              leads to breakage due to the fact that there are zero bytes to
4487              load.  */
4488           return gen_rtx_REG (mode, regbase);
4489         }
4490       else
4491         {
4492           /* ??? C++ has structures with no fields, and yet a size.  Give up
4493              for now and pass everything back in integer registers.  */
4494           nregs = (typesize + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4495         }
4496       if (nregs + slotno > SPARC_INT_ARG_MAX)
4497         nregs = SPARC_INT_ARG_MAX - slotno;
4498     }
4499   if (nregs == 0)
4500     abort ();
4501
4502   parms.ret = gen_rtx_PARALLEL (mode, rtvec_alloc (nregs));
4503
4504   /* Fill in the entries.  */
4505   parms.nregs = 0;
4506   parms.intoffset = 0;
4507   function_arg_record_value_2 (type, 0, &parms);
4508   function_arg_record_value_3 (typesize * BITS_PER_UNIT, &parms);
4509
4510   if (parms.nregs != nregs)
4511     abort ();
4512
4513   return parms.ret;
4514 }
4515
4516 /* Handle the FUNCTION_ARG macro.
4517    Determine where to put an argument to a function.
4518    Value is zero to push the argument on the stack,
4519    or a hard register in which to store the argument.
4520
4521    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4522     the preceding args and about the function being called.
4523    MODE is the argument's machine mode.
4524    TYPE is the data type of the argument (as a tree).
4525     This is null for libcalls where that information may
4526     not be available.
4527    NAMED is nonzero if this argument is a named parameter
4528     (otherwise it is an extra parameter matching an ellipsis).
4529    INCOMING_P is zero for FUNCTION_ARG, nonzero for FUNCTION_INCOMING_ARG.  */
4530
4531 rtx
4532 function_arg (cum, mode, type, named, incoming_p)
4533      const CUMULATIVE_ARGS *cum;
4534      enum machine_mode mode;
4535      tree type;
4536      int named;
4537      int incoming_p;
4538 {
4539   int regbase = (incoming_p
4540                  ? SPARC_INCOMING_INT_ARG_FIRST
4541                  : SPARC_OUTGOING_INT_ARG_FIRST);
4542   int slotno, regno, padding;
4543   rtx reg;
4544
4545   slotno = function_arg_slotno (cum, mode, type, named, incoming_p,
4546                                 &regno, &padding);
4547
4548   if (slotno == -1)
4549     return 0;
4550
4551   if (TARGET_ARCH32)
4552     {
4553       reg = gen_rtx_REG (mode, regno);
4554       return reg;
4555     }
4556
4557   /* v9 fp args in reg slots beyond the int reg slots get passed in regs
4558      but also have the slot allocated for them.
4559      If no prototype is in scope fp values in register slots get passed
4560      in two places, either fp regs and int regs or fp regs and memory.  */
4561   if ((GET_MODE_CLASS (mode) == MODE_FLOAT
4562        || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4563       && SPARC_FP_REG_P (regno))
4564     {
4565       reg = gen_rtx_REG (mode, regno);
4566       if (cum->prototype_p || cum->libcall_p)
4567         {
4568           /* "* 2" because fp reg numbers are recorded in 4 byte
4569              quantities.  */
4570 #if 0
4571           /* ??? This will cause the value to be passed in the fp reg and
4572              in the stack.  When a prototype exists we want to pass the
4573              value in the reg but reserve space on the stack.  That's an
4574              optimization, and is deferred [for a bit].  */
4575           if ((regno - SPARC_FP_ARG_FIRST) >= SPARC_INT_ARG_MAX * 2)
4576             return gen_rtx_PARALLEL (mode,
4577                             gen_rtvec (2,
4578                                        gen_rtx_EXPR_LIST (VOIDmode,
4579                                                 NULL_RTX, const0_rtx),
4580                                        gen_rtx_EXPR_LIST (VOIDmode,
4581                                                 reg, const0_rtx)));
4582           else
4583 #else
4584           /* ??? It seems that passing back a register even when past
4585              the area declared by REG_PARM_STACK_SPACE will allocate
4586              space appropriately, and will not copy the data onto the
4587              stack, exactly as we desire.
4588
4589              This is due to locate_and_pad_parm being called in
4590              expand_call whenever reg_parm_stack_space > 0, which
4591              while benefical to our example here, would seem to be
4592              in error from what had been intended.  Ho hum...  -- r~ */
4593 #endif
4594             return reg;
4595         }
4596       else
4597         {
4598           rtx v0, v1;
4599
4600           if ((regno - SPARC_FP_ARG_FIRST) < SPARC_INT_ARG_MAX * 2)
4601             {
4602               int intreg;
4603
4604               /* On incoming, we don't need to know that the value
4605                  is passed in %f0 and %i0, and it confuses other parts
4606                  causing needless spillage even on the simplest cases.  */
4607               if (incoming_p)
4608                 return reg;
4609
4610               intreg = (SPARC_OUTGOING_INT_ARG_FIRST
4611                         + (regno - SPARC_FP_ARG_FIRST) / 2);
4612
4613               v0 = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
4614               v1 = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_REG (mode, intreg),
4615                                       const0_rtx);
4616               return gen_rtx_PARALLEL (mode, gen_rtvec (2, v0, v1));
4617             }
4618           else
4619             {
4620               v0 = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
4621               v1 = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
4622               return gen_rtx_PARALLEL (mode, gen_rtvec (2, v0, v1));
4623             }
4624         }
4625     }
4626   else if (type && TREE_CODE (type) == RECORD_TYPE)
4627     {
4628       /* Structures up to 16 bytes in size are passed in arg slots on the
4629          stack and are promoted to registers where possible.  */
4630
4631       if (int_size_in_bytes (type) > 16)
4632         abort (); /* shouldn't get here */
4633
4634       return function_arg_record_value (type, mode, slotno, named, regbase);
4635     }
4636   else if (type && TREE_CODE (type) == UNION_TYPE)
4637     {
4638       enum machine_mode mode;
4639       int bytes = int_size_in_bytes (type);
4640
4641       if (bytes > 16)
4642         abort ();
4643
4644       mode = mode_for_size (bytes * BITS_PER_UNIT, MODE_INT, 0);
4645       reg = gen_rtx_REG (mode, regno);
4646     }
4647   else
4648     {
4649       /* Scalar or complex int.  */
4650       reg = gen_rtx_REG (mode, regno);
4651     }
4652
4653   return reg;
4654 }
4655
4656 /* Handle the FUNCTION_ARG_PARTIAL_NREGS macro.
4657    For an arg passed partly in registers and partly in memory,
4658    this is the number of registers used.
4659    For args passed entirely in registers or entirely in memory, zero.
4660
4661    Any arg that starts in the first 6 regs but won't entirely fit in them
4662    needs partial registers on v8.  On v9, structures with integer
4663    values in arg slots 5,6 will be passed in %o5 and SP+176, and complex fp
4664    values that begin in the last fp reg [where "last fp reg" varies with the
4665    mode] will be split between that reg and memory.  */
4666
4667 int
4668 function_arg_partial_nregs (cum, mode, type, named)
4669      const CUMULATIVE_ARGS *cum;
4670      enum machine_mode mode;
4671      tree type;
4672      int named;
4673 {
4674   int slotno, regno, padding;
4675
4676   /* We pass 0 for incoming_p here, it doesn't matter.  */
4677   slotno = function_arg_slotno (cum, mode, type, named, 0, &regno, &padding);
4678
4679   if (slotno == -1)
4680     return 0;
4681
4682   if (TARGET_ARCH32)
4683     {
4684       if ((slotno + (mode == BLKmode
4685                      ? ROUND_ADVANCE (int_size_in_bytes (type))
4686                      : ROUND_ADVANCE (GET_MODE_SIZE (mode))))
4687           > NPARM_REGS (SImode))
4688         return NPARM_REGS (SImode) - slotno;
4689       return 0;
4690     }
4691   else
4692     {
4693       if (type && AGGREGATE_TYPE_P (type))
4694         {
4695           int size = int_size_in_bytes (type);
4696           int align = TYPE_ALIGN (type);
4697
4698           if (align == 16)
4699             slotno += slotno & 1;
4700           if (size > 8 && size <= 16
4701               && slotno == SPARC_INT_ARG_MAX - 1)
4702             return 1;
4703         }
4704       else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT
4705                || (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
4706                    && ! TARGET_FPU))
4707         {
4708           if (GET_MODE_ALIGNMENT (mode) == 128)
4709             {
4710               slotno += slotno & 1;
4711               if (slotno == SPARC_INT_ARG_MAX - 2)
4712                 return 1;
4713             }
4714           else
4715             {
4716               if (slotno == SPARC_INT_ARG_MAX - 1)
4717                 return 1;
4718             }
4719         }
4720       else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4721         {
4722           if (GET_MODE_ALIGNMENT (mode) == 128)
4723             slotno += slotno & 1;
4724           if ((slotno + GET_MODE_SIZE (mode) / UNITS_PER_WORD)
4725               > SPARC_FP_ARG_MAX)
4726             return 1;
4727         }
4728       return 0;
4729     }
4730 }
4731
4732 /* Handle the FUNCTION_ARG_PASS_BY_REFERENCE macro.
4733    !v9: The SPARC ABI stipulates passing struct arguments (of any size) and
4734    quad-precision floats by invisible reference.
4735    v9: Aggregates greater than 16 bytes are passed by reference.
4736    For Pascal, also pass arrays by reference.  */
4737
4738 int
4739 function_arg_pass_by_reference (cum, mode, type, named)
4740      const CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED;
4741      enum machine_mode mode;
4742      tree type;
4743      int named ATTRIBUTE_UNUSED;
4744 {
4745   if (TARGET_ARCH32)
4746     {
4747       return ((type && AGGREGATE_TYPE_P (type))
4748               || mode == TFmode || mode == TCmode);
4749     }
4750   else
4751     {
4752       return ((type && TREE_CODE (type) == ARRAY_TYPE)
4753               /* Consider complex values as aggregates, so care for TCmode.  */
4754               || GET_MODE_SIZE (mode) > 16
4755               || (type
4756                   && AGGREGATE_TYPE_P (type)
4757                   && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 16));
4758     }
4759 }
4760
4761 /* Handle the FUNCTION_ARG_ADVANCE macro.
4762    Update the data in CUM to advance over an argument
4763    of mode MODE and data type TYPE.
4764    TYPE is null for libcalls where that information may not be available.  */
4765
4766 void
4767 function_arg_advance (cum, mode, type, named)
4768      CUMULATIVE_ARGS *cum;
4769      enum machine_mode mode;
4770      tree type;
4771      int named;
4772 {
4773   int slotno, regno, padding;
4774
4775   /* We pass 0 for incoming_p here, it doesn't matter.  */
4776   slotno = function_arg_slotno (cum, mode, type, named, 0, &regno, &padding);
4777
4778   /* If register required leading padding, add it.  */
4779   if (slotno != -1)
4780     cum->words += padding;
4781
4782   if (TARGET_ARCH32)
4783     {
4784       cum->words += (mode != BLKmode
4785                      ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
4786                      : ROUND_ADVANCE (int_size_in_bytes (type)));
4787     }
4788   else
4789     {
4790       if (type && AGGREGATE_TYPE_P (type))
4791         {
4792           int size = int_size_in_bytes (type);
4793
4794           if (size <= 8)
4795             ++cum->words;
4796           else if (size <= 16)
4797             cum->words += 2;
4798           else /* passed by reference */
4799             ++cum->words;
4800         }
4801       else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
4802         {
4803           cum->words += 2;
4804         }
4805       else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
4806         {
4807           cum->words += GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4808         }
4809       else
4810         {
4811           cum->words += (mode != BLKmode
4812                          ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
4813                          : ROUND_ADVANCE (int_size_in_bytes (type)));
4814         }
4815     }
4816 }
4817
4818 /* Handle the FUNCTION_ARG_PADDING macro.
4819    For the 64 bit ABI structs are always stored left shifted in their
4820    argument slot.  */
4821
4822 enum direction
4823 function_arg_padding (mode, type)
4824      enum machine_mode mode;
4825      tree type;
4826 {
4827   if (TARGET_ARCH64 && type != 0 && AGGREGATE_TYPE_P (type))
4828     return upward;
4829
4830   /* This is the default definition.  */
4831   return (! BYTES_BIG_ENDIAN
4832           ? upward
4833           : ((mode == BLKmode
4834               ? (type && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
4835                  && int_size_in_bytes (type) < (PARM_BOUNDARY / BITS_PER_UNIT))
4836               : GET_MODE_BITSIZE (mode) < PARM_BOUNDARY)
4837              ? downward : upward));
4838 }
4839
4840 /* Handle FUNCTION_VALUE, FUNCTION_OUTGOING_VALUE, and LIBCALL_VALUE macros.
4841    For v9, function return values are subject to the same rules as arguments,
4842    except that up to 32-bytes may be returned in registers.  */
4843
4844 rtx
4845 function_value (type, mode, incoming_p)
4846      tree type;
4847      enum machine_mode mode;
4848      int incoming_p;
4849 {
4850   int regno;
4851   int regbase = (incoming_p
4852                  ? SPARC_OUTGOING_INT_ARG_FIRST
4853                  : SPARC_INCOMING_INT_ARG_FIRST);
4854
4855   if (TARGET_ARCH64 && type)
4856     {
4857       if (TREE_CODE (type) == RECORD_TYPE)
4858         {
4859           /* Structures up to 32 bytes in size are passed in registers,
4860              promoted to fp registers where possible.  */
4861
4862           if (int_size_in_bytes (type) > 32)
4863             abort (); /* shouldn't get here */
4864
4865           return function_arg_record_value (type, mode, 0, 1, regbase);
4866         }
4867       else if (AGGREGATE_TYPE_P (type))
4868         {
4869           /* All other aggregate types are passed in an integer register
4870              in a mode corresponding to the size of the type.  */
4871           HOST_WIDE_INT bytes = int_size_in_bytes (type);
4872
4873           if (bytes > 32)
4874             abort ();
4875
4876           mode = mode_for_size (bytes * BITS_PER_UNIT, MODE_INT, 0);
4877         }
4878     }
4879     
4880   if (TARGET_ARCH64
4881       && GET_MODE_CLASS (mode) == MODE_INT 
4882       && GET_MODE_SIZE (mode) < UNITS_PER_WORD
4883       && type && ! AGGREGATE_TYPE_P (type))
4884     mode = DImode;
4885
4886   if (incoming_p)
4887     regno = BASE_RETURN_VALUE_REG (mode);
4888   else
4889     regno = BASE_OUTGOING_VALUE_REG (mode);
4890
4891   return gen_rtx_REG (mode, regno);
4892 }
4893
4894 /* Do what is necessary for `va_start'.  We look at the current function
4895    to determine if stdarg or varargs is used and return the address of
4896    the first unnamed parameter.  */
4897
4898 rtx
4899 sparc_builtin_saveregs ()
4900 {
4901   int first_reg = current_function_args_info.words;
4902   rtx address;
4903   int regno;
4904
4905   for (regno = first_reg; regno < NPARM_REGS (word_mode); regno++)
4906     emit_move_insn (gen_rtx_MEM (word_mode,
4907                                  gen_rtx_PLUS (Pmode,
4908                                                frame_pointer_rtx,
4909                                                GEN_INT (FIRST_PARM_OFFSET (0)
4910                                                         + (UNITS_PER_WORD
4911                                                            * regno)))),
4912                     gen_rtx_REG (word_mode,
4913                                  BASE_INCOMING_ARG_REG (word_mode) + regno));
4914
4915   address = gen_rtx_PLUS (Pmode,
4916                           frame_pointer_rtx,
4917                           GEN_INT (FIRST_PARM_OFFSET (0)
4918                                    + UNITS_PER_WORD * first_reg));
4919
4920   return address;
4921 }
4922
4923 /* Implement `va_start' for varargs and stdarg.  */
4924
4925 void
4926 sparc_va_start (stdarg_p, valist, nextarg)
4927      int stdarg_p ATTRIBUTE_UNUSED;
4928      tree valist;
4929      rtx nextarg;
4930 {
4931   nextarg = expand_builtin_saveregs ();
4932   std_expand_builtin_va_start (1, valist, nextarg);
4933 }
4934
4935 /* Implement `va_arg'.  */
4936
4937 rtx
4938 sparc_va_arg (valist, type)
4939      tree valist, type;
4940 {
4941   HOST_WIDE_INT size, rsize, align;
4942   tree addr, incr;
4943   rtx addr_rtx;
4944   int indirect = 0;
4945
4946   /* Round up sizeof(type) to a word.  */
4947   size = int_size_in_bytes (type);
4948   rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
4949   align = 0;
4950
4951   if (TARGET_ARCH64)
4952     {
4953       if (TYPE_ALIGN (type) >= 2 * (unsigned) BITS_PER_WORD)
4954         align = 2 * UNITS_PER_WORD;
4955
4956       if (AGGREGATE_TYPE_P (type))
4957         {
4958           if ((unsigned HOST_WIDE_INT) size > 16)
4959             {
4960               indirect = 1;
4961               size = rsize = UNITS_PER_WORD;
4962             }
4963           /* SPARC v9 ABI states that structures up to 8 bytes in size are
4964              given one 8 byte slot.  */
4965           else if (size == 0)
4966             size = rsize = UNITS_PER_WORD;
4967           else
4968             size = rsize;
4969         }
4970     }
4971   else
4972     {
4973       if (AGGREGATE_TYPE_P (type)
4974           || TYPE_MODE (type) == TFmode
4975           || TYPE_MODE (type) == TCmode)
4976         {
4977           indirect = 1;
4978           size = rsize = UNITS_PER_WORD;
4979         }
4980     }
4981
4982   incr = valist;
4983   if (align)
4984     {
4985       incr = fold (build (PLUS_EXPR, ptr_type_node, incr,
4986                          build_int_2 (align - 1, 0)));
4987       incr = fold (build (BIT_AND_EXPR, ptr_type_node, incr,
4988                           build_int_2 (-align, -1)));
4989     }
4990
4991   addr = incr = save_expr (incr);
4992   if (BYTES_BIG_ENDIAN && size < rsize)
4993     {
4994       addr = fold (build (PLUS_EXPR, ptr_type_node, incr,
4995                           build_int_2 (rsize - size, 0)));
4996     }
4997   incr = fold (build (PLUS_EXPR, ptr_type_node, incr,
4998                       build_int_2 (rsize, 0)));
4999
5000   incr = build (MODIFY_EXPR, ptr_type_node, valist, incr);
5001   TREE_SIDE_EFFECTS (incr) = 1;
5002   expand_expr (incr, const0_rtx, VOIDmode, EXPAND_NORMAL);
5003
5004   addr_rtx = expand_expr (addr, NULL, Pmode, EXPAND_NORMAL);
5005
5006   /* If the address isn't aligned properly for the type,
5007      we may need to copy to a temporary.  
5008      FIXME: This is inefficient.  Usually we can do this
5009      in registers.  */
5010   if (align == 0
5011       && TYPE_ALIGN (type) > BITS_PER_WORD
5012       && !indirect)
5013     {
5014       /* FIXME: We really need to specify that the temporary is live
5015          for the whole function because expand_builtin_va_arg wants
5016          the alias set to be get_varargs_alias_set (), but in this
5017          case the alias set is that for TYPE and if the memory gets
5018          reused it will be reused with alias set TYPE.  */
5019       rtx tmp = assign_temp (type, 0, 1, 0);
5020       rtx dest_addr;
5021
5022       addr_rtx = force_reg (Pmode, addr_rtx);
5023       addr_rtx = gen_rtx_MEM (BLKmode, addr_rtx);
5024       set_mem_alias_set (addr_rtx, get_varargs_alias_set ());
5025       set_mem_align (addr_rtx, BITS_PER_WORD);
5026       tmp = shallow_copy_rtx (tmp);
5027       PUT_MODE (tmp, BLKmode);
5028       set_mem_alias_set (tmp, 0);
5029       
5030       dest_addr = emit_block_move (tmp, addr_rtx, GEN_INT (rsize));
5031       if (dest_addr != NULL_RTX)
5032         addr_rtx = dest_addr;
5033       else
5034         addr_rtx = XCEXP (tmp, 0, MEM);
5035     }
5036
5037   if (indirect)
5038     {
5039       addr_rtx = force_reg (Pmode, addr_rtx);
5040       addr_rtx = gen_rtx_MEM (Pmode, addr_rtx);
5041       set_mem_alias_set (addr_rtx, get_varargs_alias_set ());
5042     }
5043
5044   return addr_rtx;
5045 }
5046 \f
5047 /* Return the string to output a conditional branch to LABEL, which is
5048    the operand number of the label.  OP is the conditional expression.
5049    XEXP (OP, 0) is assumed to be a condition code register (integer or
5050    floating point) and its mode specifies what kind of comparison we made.
5051
5052    REVERSED is non-zero if we should reverse the sense of the comparison.
5053
5054    ANNUL is non-zero if we should generate an annulling branch.
5055
5056    NOOP is non-zero if we have to follow this branch by a noop.
5057
5058    INSN, if set, is the insn.  */
5059
5060 char *
5061 output_cbranch (op, dest, label, reversed, annul, noop, insn)
5062      rtx op, dest;
5063      int label;
5064      int reversed, annul, noop;
5065      rtx insn;
5066 {
5067   static char string[50];
5068   enum rtx_code code = GET_CODE (op);
5069   rtx cc_reg = XEXP (op, 0);
5070   enum machine_mode mode = GET_MODE (cc_reg);
5071   const char *labelno, *branch;
5072   int spaces = 8, far;
5073   char *p;
5074
5075   /* v9 branches are limited to +-1MB.  If it is too far away,
5076      change
5077
5078      bne,pt %xcc, .LC30
5079
5080      to
5081
5082      be,pn %xcc, .+12
5083      nop
5084      ba .LC30
5085
5086      and
5087
5088      fbne,a,pn %fcc2, .LC29
5089
5090      to
5091
5092      fbe,pt %fcc2, .+16
5093      nop
5094      ba .LC29  */
5095
5096   far = get_attr_length (insn) >= 3;
5097   if (reversed ^ far)
5098     {
5099       /* Reversal of FP compares takes care -- an ordered compare
5100          becomes an unordered compare and vice versa.  */
5101       if (mode == CCFPmode || mode == CCFPEmode)
5102         code = reverse_condition_maybe_unordered (code);
5103       else
5104         code = reverse_condition (code);
5105     }
5106
5107   /* Start by writing the branch condition.  */
5108   if (mode == CCFPmode || mode == CCFPEmode)
5109     {
5110       switch (code)
5111         {
5112         case NE:
5113           branch = "fbne";
5114           break;
5115         case EQ:
5116           branch = "fbe";
5117           break;
5118         case GE:
5119           branch = "fbge";
5120           break;
5121         case GT:
5122           branch = "fbg";
5123           break;
5124         case LE:
5125           branch = "fble";
5126           break;
5127         case LT:
5128           branch = "fbl";
5129           break;
5130         case UNORDERED:
5131           branch = "fbu";
5132           break;
5133         case ORDERED:
5134           branch = "fbo";
5135           break;
5136         case UNGT:
5137           branch = "fbug";
5138           break;
5139         case UNLT:
5140           branch = "fbul";
5141           break;
5142         case UNEQ:
5143           branch = "fbue";
5144           break;
5145         case UNGE:
5146           branch = "fbuge";
5147           break;
5148         case UNLE:
5149           branch = "fbule";
5150           break;
5151         case LTGT:
5152           branch = "fblg";
5153           break;
5154
5155         default:
5156           abort ();
5157         }
5158
5159       /* ??? !v9: FP branches cannot be preceded by another floating point
5160          insn.  Because there is currently no concept of pre-delay slots,
5161          we can fix this only by always emitting a nop before a floating
5162          point branch.  */
5163
5164       string[0] = '\0';
5165       if (! TARGET_V9)
5166         strcpy (string, "nop\n\t");
5167       strcat (string, branch);
5168     }
5169   else
5170     {
5171       switch (code)
5172         {
5173         case NE:
5174           branch = "bne";
5175           break;
5176         case EQ:
5177           branch = "be";
5178           break;
5179         case GE:
5180           if (mode == CC_NOOVmode || mode == CCX_NOOVmode)
5181             branch = "bpos";
5182           else
5183             branch = "bge";
5184           break;
5185         case GT:
5186           branch = "bg";
5187           break;
5188         case LE:
5189           branch = "ble";
5190           break;
5191         case LT:
5192           if (mode == CC_NOOVmode || mode == CCX_NOOVmode)
5193             branch = "bneg";
5194           else
5195             branch = "bl";
5196           break;
5197         case GEU:
5198           branch = "bgeu";
5199           break;
5200         case GTU:
5201           branch = "bgu";
5202           break;
5203         case LEU:
5204           branch = "bleu";
5205           break;
5206         case LTU:
5207           branch = "blu";
5208           break;
5209
5210         default:
5211           abort ();
5212         }
5213       strcpy (string, branch);
5214     }
5215   spaces -= strlen (branch);
5216   p = strchr (string, '\0');
5217
5218   /* Now add the annulling, the label, and a possible noop.  */
5219   if (annul && ! far)
5220     {
5221       strcpy (p, ",a");
5222       p += 2;
5223       spaces -= 2;
5224     }
5225
5226   if (! TARGET_V9)
5227     labelno = "";
5228   else
5229     {
5230       rtx note;
5231       int v8 = 0;
5232
5233       if (! far && insn && INSN_ADDRESSES_SET_P ())
5234         {
5235           int delta = (INSN_ADDRESSES (INSN_UID (dest))
5236                        - INSN_ADDRESSES (INSN_UID (insn)));
5237           /* Leave some instructions for "slop".  */
5238           if (delta < -260000 || delta >= 260000)
5239             v8 = 1;
5240         }
5241
5242       if (mode == CCFPmode || mode == CCFPEmode)
5243         {
5244           static char v9_fcc_labelno[] = "%%fccX, ";
5245           /* Set the char indicating the number of the fcc reg to use.  */
5246           v9_fcc_labelno[5] = REGNO (cc_reg) - SPARC_FIRST_V9_FCC_REG + '0';
5247           labelno = v9_fcc_labelno;
5248           if (v8)
5249             {
5250               if (REGNO (cc_reg) == SPARC_FCC_REG)
5251                 labelno = "";
5252               else
5253                 abort ();
5254             }
5255         }
5256       else if (mode == CCXmode || mode == CCX_NOOVmode)
5257         {
5258           labelno = "%%xcc, ";
5259           if (v8)
5260             abort ();
5261         }
5262       else
5263         {
5264           labelno = "%%icc, ";
5265           if (v8)
5266             labelno = "";
5267         }
5268
5269       if (*labelno && insn && (note = find_reg_note (insn, REG_BR_PROB, NULL_RTX)))
5270         {
5271           strcpy (p,
5272                   ((INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2) ^ far)
5273                   ? ",pt" : ",pn");
5274           p += 3;
5275           spaces -= 3;
5276         }
5277     }
5278   if (spaces > 0)
5279     *p++ = '\t';
5280   else
5281     *p++ = ' ';
5282   strcpy (p, labelno);
5283   p = strchr (p, '\0');
5284   if (far)
5285     {
5286       strcpy (p, ".+12\n\tnop\n\tb\t");
5287       if (annul || noop)
5288         p[3] = '6';
5289       p += 13;
5290     }
5291   *p++ = '%';
5292   *p++ = 'l';
5293   /* Set the char indicating the number of the operand containing the
5294      label_ref.  */
5295   *p++ = label + '0';
5296   *p = '\0';
5297   if (noop)
5298     strcpy (p, "\n\tnop");
5299
5300   return string;
5301 }
5302
5303 /* Emit a library call comparison between floating point X and Y.
5304    COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
5305    TARGET_ARCH64 uses _Qp_* functions, which use pointers to TFmode
5306    values as arguments instead of the TFmode registers themselves,
5307    that's why we cannot call emit_float_lib_cmp.  */
5308 void
5309 sparc_emit_float_lib_cmp (x, y, comparison)
5310      rtx x, y;
5311      enum rtx_code comparison;
5312 {
5313   const char *qpfunc;
5314   rtx slot0, slot1, result, tem, tem2;
5315   enum machine_mode mode;
5316
5317   switch (comparison)
5318     {
5319     case EQ:
5320       qpfunc = (TARGET_ARCH64) ? "_Qp_feq" : "_Q_feq";
5321       break;
5322
5323     case NE:
5324       qpfunc = (TARGET_ARCH64) ? "_Qp_fne" : "_Q_fne";
5325       break;
5326
5327     case GT:
5328       qpfunc = (TARGET_ARCH64) ? "_Qp_fgt" : "_Q_fgt";
5329       break;
5330
5331     case GE:
5332       qpfunc = (TARGET_ARCH64) ? "_Qp_fge" : "_Q_fge";
5333       break;
5334
5335     case LT:
5336       qpfunc = (TARGET_ARCH64) ? "_Qp_flt" : "_Q_flt";
5337       break;
5338
5339     case LE:
5340       qpfunc = (TARGET_ARCH64) ? "_Qp_fle" : "_Q_fle";
5341       break;
5342
5343     case ORDERED:
5344     case UNORDERED:
5345     case UNGT:
5346     case UNLT:
5347     case UNEQ:
5348     case UNGE:
5349     case UNLE:
5350     case LTGT:
5351       qpfunc = (TARGET_ARCH64) ? "_Qp_cmp" : "_Q_cmp";
5352       break;
5353
5354     default:
5355       abort();
5356       break;
5357     }
5358
5359   if (TARGET_ARCH64)
5360     {
5361       if (GET_CODE (x) != MEM)
5362         {
5363           slot0 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode), 0);
5364           emit_insn (gen_rtx_SET (VOIDmode, slot0, x));
5365         }
5366       else
5367         slot0 = x;
5368
5369       if (GET_CODE (y) != MEM)
5370         {
5371           slot1 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode), 0);
5372           emit_insn (gen_rtx_SET (VOIDmode, slot1, y));
5373         }
5374       else
5375         slot1 = y;
5376
5377       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, qpfunc), 1,
5378                          DImode, 2,
5379                          XEXP (slot0, 0), Pmode,
5380                          XEXP (slot1, 0), Pmode);
5381
5382       mode = DImode;
5383     }
5384   else
5385     {
5386       emit_library_call (gen_rtx_SYMBOL_REF (Pmode, qpfunc), 1,
5387                          SImode, 2,
5388                          x, TFmode, y, TFmode);
5389
5390       mode = SImode;
5391     }
5392
5393
5394   /* Immediately move the result of the libcall into a pseudo
5395      register so reload doesn't clobber the value if it needs
5396      the return register for a spill reg.  */
5397   result = gen_reg_rtx (mode);
5398   emit_move_insn (result, hard_libcall_value (mode));
5399
5400   switch (comparison)
5401     {
5402     default:
5403       emit_cmp_insn (result, const0_rtx, NE, NULL_RTX, mode, 0);
5404       break;
5405     case ORDERED:
5406     case UNORDERED:
5407       emit_cmp_insn (result, GEN_INT(3), comparison == UNORDERED ? EQ : NE,
5408                      NULL_RTX, mode, 0);
5409       break;
5410     case UNGT:
5411     case UNGE:
5412       emit_cmp_insn (result, const1_rtx,
5413                      comparison == UNGT ? GT : NE, NULL_RTX, mode, 0);
5414       break;
5415     case UNLE:
5416       emit_cmp_insn (result, const2_rtx, NE, NULL_RTX, mode, 0);
5417       break;
5418     case UNLT:
5419       tem = gen_reg_rtx (mode);
5420       if (TARGET_ARCH32)
5421         emit_insn (gen_andsi3 (tem, result, const1_rtx));
5422       else
5423         emit_insn (gen_anddi3 (tem, result, const1_rtx));
5424       emit_cmp_insn (tem, const0_rtx, NE, NULL_RTX, mode, 0);
5425       break;
5426     case UNEQ:
5427     case LTGT:
5428       tem = gen_reg_rtx (mode);
5429       if (TARGET_ARCH32)
5430         emit_insn (gen_addsi3 (tem, result, const1_rtx));
5431       else
5432         emit_insn (gen_adddi3 (tem, result, const1_rtx));
5433       tem2 = gen_reg_rtx (mode);
5434       if (TARGET_ARCH32)
5435         emit_insn (gen_andsi3 (tem2, tem, const2_rtx));
5436       else
5437         emit_insn (gen_anddi3 (tem2, tem, const2_rtx));
5438       emit_cmp_insn (tem2, const0_rtx, comparison == UNEQ ? EQ : NE,
5439                      NULL_RTX, mode, 0);
5440       break;
5441     }
5442 }
5443
5444 /* Generate an unsigned DImode to FP conversion.  This is the same code
5445    optabs would emit if we didn't have TFmode patterns.  */
5446
5447 void
5448 sparc_emit_floatunsdi (operands)
5449      rtx operands[2];
5450 {
5451   rtx neglab, donelab, i0, i1, f0, in, out;
5452   enum machine_mode mode;
5453
5454   out = operands[0];
5455   in = force_reg (DImode, operands[1]);
5456   mode = GET_MODE (out);
5457   neglab = gen_label_rtx ();
5458   donelab = gen_label_rtx ();
5459   i0 = gen_reg_rtx (DImode);
5460   i1 = gen_reg_rtx (DImode);
5461   f0 = gen_reg_rtx (mode);
5462
5463   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, DImode, 0, neglab);
5464
5465   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
5466   emit_jump_insn (gen_jump (donelab));
5467   emit_barrier ();
5468
5469   emit_label (neglab);
5470
5471   emit_insn (gen_lshrdi3 (i0, in, const1_rtx));
5472   emit_insn (gen_anddi3 (i1, in, const1_rtx));
5473   emit_insn (gen_iordi3 (i0, i0, i1));
5474   emit_insn (gen_rtx_SET (VOIDmode, f0, gen_rtx_FLOAT (mode, i0)));
5475   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
5476
5477   emit_label (donelab);
5478 }
5479
5480 /* Return the string to output a conditional branch to LABEL, testing
5481    register REG.  LABEL is the operand number of the label; REG is the
5482    operand number of the reg.  OP is the conditional expression.  The mode
5483    of REG says what kind of comparison we made.
5484
5485    REVERSED is non-zero if we should reverse the sense of the comparison.
5486
5487    ANNUL is non-zero if we should generate an annulling branch.
5488
5489    NOOP is non-zero if we have to follow this branch by a noop.  */
5490
5491 char *
5492 output_v9branch (op, dest, reg, label, reversed, annul, noop, insn)
5493      rtx op, dest;
5494      int reg, label;
5495      int reversed, annul, noop;
5496      rtx insn;
5497 {
5498   static char string[50];
5499   enum rtx_code code = GET_CODE (op);
5500   enum machine_mode mode = GET_MODE (XEXP (op, 0));
5501   rtx note;
5502   int far;
5503   char *p;
5504
5505   /* branch on register are limited to +-128KB.  If it is too far away,
5506      change
5507      
5508      brnz,pt %g1, .LC30
5509      
5510      to
5511      
5512      brz,pn %g1, .+12
5513      nop
5514      ba,pt %xcc, .LC30
5515      
5516      and
5517      
5518      brgez,a,pn %o1, .LC29
5519      
5520      to
5521      
5522      brlz,pt %o1, .+16
5523      nop
5524      ba,pt %xcc, .LC29  */
5525
5526   far = get_attr_length (insn) >= 3;
5527
5528   /* If not floating-point or if EQ or NE, we can just reverse the code.  */
5529   if (reversed ^ far)
5530     code = reverse_condition (code);
5531
5532   /* Only 64 bit versions of these instructions exist.  */
5533   if (mode != DImode)
5534     abort ();
5535
5536   /* Start by writing the branch condition.  */
5537
5538   switch (code)
5539     {
5540     case NE:
5541       strcpy (string, "brnz");
5542       break;
5543
5544     case EQ:
5545       strcpy (string, "brz");
5546       break;
5547
5548     case GE:
5549       strcpy (string, "brgez");
5550       break;
5551
5552     case LT:
5553       strcpy (string, "brlz");
5554       break;
5555
5556     case LE:
5557       strcpy (string, "brlez");
5558       break;
5559
5560     case GT:
5561       strcpy (string, "brgz");
5562       break;
5563
5564     default:
5565       abort ();
5566     }
5567
5568   p = strchr (string, '\0');
5569
5570   /* Now add the annulling, reg, label, and nop.  */
5571   if (annul && ! far)
5572     {
5573       strcpy (p, ",a");
5574       p += 2;
5575     }
5576
5577   if (insn && (note = find_reg_note (insn, REG_BR_PROB, NULL_RTX)))
5578     {
5579       strcpy (p,
5580               ((INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2) ^ far)
5581               ? ",pt" : ",pn");
5582       p += 3;
5583     }
5584
5585   *p = p < string + 8 ? '\t' : ' ';
5586   p++;
5587   *p++ = '%';
5588   *p++ = '0' + reg;
5589   *p++ = ',';
5590   *p++ = ' ';
5591   if (far)
5592     {
5593       int veryfar = 1, delta;
5594
5595       if (INSN_ADDRESSES_SET_P ())
5596         {
5597           delta = (INSN_ADDRESSES (INSN_UID (dest))
5598                    - INSN_ADDRESSES (INSN_UID (insn)));
5599           /* Leave some instructions for "slop".  */
5600           if (delta >= -260000 && delta < 260000)
5601             veryfar = 0;
5602         }
5603
5604       strcpy (p, ".+12\n\tnop\n\t");
5605       if (annul || noop)
5606         p[3] = '6';
5607       p += 11;
5608       if (veryfar)
5609         {
5610           strcpy (p, "b\t");
5611           p += 2;
5612         }
5613       else
5614         {
5615           strcpy (p, "ba,pt\t%%xcc, ");
5616           p += 13;
5617         }
5618     }
5619   *p++ = '%';
5620   *p++ = 'l';
5621   *p++ = '0' + label;
5622   *p = '\0';
5623
5624   if (noop)
5625     strcpy (p, "\n\tnop");
5626
5627   return string;
5628 }
5629
5630 /* Return 1, if any of the registers of the instruction are %l[0-7] or %o[0-7].
5631    Such instructions cannot be used in the delay slot of return insn on v9.
5632    If TEST is 0, also rename all %i[0-7] registers to their %o[0-7] counterparts.
5633  */
5634
5635 static int
5636 epilogue_renumber (where, test)
5637      register rtx *where;
5638      int test;
5639 {
5640   register const char *fmt;
5641   register int i;
5642   register enum rtx_code code;
5643
5644   if (*where == 0)
5645     return 0;
5646
5647   code = GET_CODE (*where);
5648
5649   switch (code)
5650     {
5651     case REG:
5652       if (REGNO (*where) >= 8 && REGNO (*where) < 24)      /* oX or lX */
5653         return 1;
5654       if (! test && REGNO (*where) >= 24 && REGNO (*where) < 32)
5655         *where = gen_rtx (REG, GET_MODE (*where), OUTGOING_REGNO (REGNO(*where)));
5656     case SCRATCH:
5657     case CC0:
5658     case PC:
5659     case CONST_INT:
5660     case CONST_DOUBLE:
5661       return 0;
5662
5663       /* Do not replace the frame pointer with the stack pointer because
5664          it can cause the delayed instruction to load below the stack.
5665          This occurs when instructions like:
5666
5667          (set (reg/i:SI 24 %i0)
5668              (mem/f:SI (plus:SI (reg/f:SI 30 %fp)
5669                        (const_int -20 [0xffffffec])) 0))
5670
5671          are in the return delayed slot.  */
5672     case PLUS:
5673       if (GET_CODE (XEXP (*where, 0)) == REG
5674           && REGNO (XEXP (*where, 0)) == HARD_FRAME_POINTER_REGNUM
5675           && (GET_CODE (XEXP (*where, 1)) != CONST_INT
5676               || INTVAL (XEXP (*where, 1)) < SPARC_STACK_BIAS))
5677         return 1;
5678       break;
5679
5680     case MEM:
5681       if (SPARC_STACK_BIAS
5682           && GET_CODE (XEXP (*where, 0)) == REG
5683           && REGNO (XEXP (*where, 0)) == HARD_FRAME_POINTER_REGNUM)
5684         return 1;
5685       break;
5686
5687     default:
5688       break;
5689     }
5690
5691   fmt = GET_RTX_FORMAT (code);
5692
5693   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5694     {
5695       if (fmt[i] == 'E')
5696         {
5697           register int j;
5698           for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
5699             if (epilogue_renumber (&(XVECEXP (*where, i, j)), test))
5700               return 1;
5701         }
5702       else if (fmt[i] == 'e'
5703                && epilogue_renumber (&(XEXP (*where, i)), test))
5704         return 1;
5705     }
5706   return 0;
5707 }
5708 \f
5709 /* Leaf functions and non-leaf functions have different needs.  */
5710
5711 static const int
5712 reg_leaf_alloc_order[] = REG_LEAF_ALLOC_ORDER;
5713
5714 static const int
5715 reg_nonleaf_alloc_order[] = REG_ALLOC_ORDER;
5716
5717 static const int *const reg_alloc_orders[] = {
5718   reg_leaf_alloc_order,
5719   reg_nonleaf_alloc_order};
5720
5721 void
5722 order_regs_for_local_alloc ()
5723 {
5724   static int last_order_nonleaf = 1;
5725
5726   if (regs_ever_live[15] != last_order_nonleaf)
5727     {
5728       last_order_nonleaf = !last_order_nonleaf;
5729       memcpy ((char *) reg_alloc_order,
5730               (const char *) reg_alloc_orders[last_order_nonleaf],
5731               FIRST_PSEUDO_REGISTER * sizeof (int));
5732     }
5733 }
5734 \f
5735 /* Return 1 if REG and MEM are legitimate enough to allow the various
5736    mem<-->reg splits to be run.  */
5737
5738 int
5739 sparc_splitdi_legitimate (reg, mem)
5740      rtx reg;
5741      rtx mem;
5742 {
5743   /* Punt if we are here by mistake.  */
5744   if (! reload_completed)
5745     abort ();
5746
5747   /* We must have an offsettable memory reference.  */
5748   if (! offsettable_memref_p (mem))
5749     return 0;
5750
5751   /* If we have legitimate args for ldd/std, we do not want
5752      the split to happen.  */
5753   if ((REGNO (reg) % 2) == 0
5754       && mem_min_alignment (mem, 8))
5755     return 0;
5756
5757   /* Success.  */
5758   return 1;
5759 }
5760
5761 /* Return 1 if x and y are some kind of REG and they refer to
5762    different hard registers.  This test is guarenteed to be
5763    run after reload.  */
5764
5765 int
5766 sparc_absnegfloat_split_legitimate (x, y)
5767      rtx x, y;
5768 {
5769   if (GET_CODE (x) != REG)
5770     return 0;
5771   if (GET_CODE (y) != REG)
5772     return 0;
5773   if (REGNO (x) == REGNO (y))
5774     return 0;
5775   return 1;
5776 }
5777
5778 /* Return 1 if REGNO (reg1) is even and REGNO (reg1) == REGNO (reg2) - 1.
5779    This makes them candidates for using ldd and std insns. 
5780
5781    Note reg1 and reg2 *must* be hard registers.  */
5782
5783 int
5784 registers_ok_for_ldd_peep (reg1, reg2)
5785      rtx reg1, reg2;
5786 {
5787   /* We might have been passed a SUBREG.  */
5788   if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG) 
5789     return 0;
5790
5791   if (REGNO (reg1) % 2 != 0)
5792     return 0;
5793
5794   /* Integer ldd is deprecated in SPARC V9 */ 
5795   if (TARGET_V9 && REGNO (reg1) < 32)                  
5796     return 0;                             
5797
5798   return (REGNO (reg1) == REGNO (reg2) - 1);
5799 }
5800
5801 /* Return 1 if the addresses in mem1 and mem2 are suitable for use in
5802    an ldd or std insn.
5803    
5804    This can only happen when addr1 and addr2, the addresses in mem1
5805    and mem2, are consecutive memory locations (addr1 + 4 == addr2).
5806    addr1 must also be aligned on a 64-bit boundary.
5807
5808    Also iff dependent_reg_rtx is not null it should not be used to
5809    compute the address for mem1, i.e. we cannot optimize a sequence
5810    like:
5811         ld [%o0], %o0
5812         ld [%o0 + 4], %o1
5813    to
5814         ldd [%o0], %o0
5815    nor:
5816         ld [%g3 + 4], %g3
5817         ld [%g3], %g2
5818    to
5819         ldd [%g3], %g2
5820
5821    But, note that the transformation from:
5822         ld [%g2 + 4], %g3
5823         ld [%g2], %g2
5824    to
5825         ldd [%g2], %g2
5826    is perfectly fine.  Thus, the peephole2 patterns always pass us
5827    the destination register of the first load, never the second one.
5828
5829    For stores we don't have a similar problem, so dependent_reg_rtx is
5830    NULL_RTX.  */
5831
5832 int
5833 mems_ok_for_ldd_peep (mem1, mem2, dependent_reg_rtx)
5834       rtx mem1, mem2, dependent_reg_rtx;
5835 {
5836   rtx addr1, addr2;
5837   unsigned int reg1;
5838   int offset1;
5839
5840   /* The mems cannot be volatile.  */
5841   if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
5842     return 0;
5843
5844   /* MEM1 should be aligned on a 64-bit boundary.  */
5845   if (MEM_ALIGN (mem1) < 64)
5846     return 0;
5847   
5848   addr1 = XEXP (mem1, 0);
5849   addr2 = XEXP (mem2, 0);
5850   
5851   /* Extract a register number and offset (if used) from the first addr.  */
5852   if (GET_CODE (addr1) == PLUS)
5853     {
5854       /* If not a REG, return zero.  */
5855       if (GET_CODE (XEXP (addr1, 0)) != REG)
5856         return 0;
5857       else
5858         {
5859           reg1 = REGNO (XEXP (addr1, 0));
5860           /* The offset must be constant!  */
5861           if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
5862             return 0;
5863           offset1 = INTVAL (XEXP (addr1, 1));
5864         }
5865     }
5866   else if (GET_CODE (addr1) != REG)
5867     return 0;
5868   else
5869     {
5870       reg1 = REGNO (addr1);
5871       /* This was a simple (mem (reg)) expression.  Offset is 0.  */
5872       offset1 = 0;
5873     }
5874
5875   /* Make sure the second address is a (mem (plus (reg) (const_int).  */
5876   if (GET_CODE (addr2) != PLUS)
5877     return 0;
5878
5879   if (GET_CODE (XEXP (addr2, 0)) != REG
5880       || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
5881     return 0;
5882
5883   if (reg1 != REGNO (XEXP (addr2, 0)))
5884     return 0;
5885
5886   if (dependent_reg_rtx != NULL_RTX && reg1 == REGNO (dependent_reg_rtx))
5887     return 0;
5888   
5889   /* The first offset must be evenly divisible by 8 to ensure the 
5890      address is 64 bit aligned.  */
5891   if (offset1 % 8 != 0)
5892     return 0;
5893
5894   /* The offset for the second addr must be 4 more than the first addr.  */
5895   if (INTVAL (XEXP (addr2, 1)) != offset1 + 4)
5896     return 0;
5897
5898   /* All the tests passed.  addr1 and addr2 are valid for ldd and std
5899      instructions.  */
5900   return 1;
5901 }
5902
5903 /* Return 1 if reg is a pseudo, or is the first register in 
5904    a hard register pair.  This makes it a candidate for use in
5905    ldd and std insns.  */
5906
5907 int
5908 register_ok_for_ldd (reg)
5909      rtx reg;
5910 {
5911   /* We might have been passed a SUBREG.  */
5912   if (GET_CODE (reg) != REG) 
5913     return 0;
5914
5915   if (REGNO (reg) < FIRST_PSEUDO_REGISTER)
5916     return (REGNO (reg) % 2 == 0);
5917   else 
5918     return 1;
5919 }
5920 \f
5921 /* Print operand X (an rtx) in assembler syntax to file FILE.
5922    CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
5923    For `%' followed by punctuation, CODE is the punctuation and X is null.  */
5924
5925 void
5926 print_operand (file, x, code)
5927      FILE *file;
5928      rtx x;
5929      int code;
5930 {
5931   switch (code)
5932     {
5933     case '#':
5934       /* Output a 'nop' if there's nothing for the delay slot.  */
5935       if (dbr_sequence_length () == 0)
5936         fputs ("\n\t nop", file);
5937       return;
5938     case '*':
5939       /* Output an annul flag if there's nothing for the delay slot and we
5940          are optimizing.  This is always used with '(' below.  */
5941       /* Sun OS 4.1.1 dbx can't handle an annulled unconditional branch;
5942          this is a dbx bug.  So, we only do this when optimizing.  */
5943       /* On UltraSPARC, a branch in a delay slot causes a pipeline flush.
5944          Always emit a nop in case the next instruction is a branch.  */
5945       if (dbr_sequence_length () == 0
5946           && (optimize && (int)sparc_cpu < PROCESSOR_V9))
5947         fputs (",a", file);
5948       return;
5949     case '(':
5950       /* Output a 'nop' if there's nothing for the delay slot and we are
5951          not optimizing.  This is always used with '*' above.  */
5952       if (dbr_sequence_length () == 0
5953           && ! (optimize && (int)sparc_cpu < PROCESSOR_V9))
5954         fputs ("\n\t nop", file);
5955       return;
5956     case '_':
5957       /* Output the Embedded Medium/Anywhere code model base register.  */
5958       fputs (EMBMEDANY_BASE_REG, file);
5959       return;
5960     case '@':
5961       /* Print out what we are using as the frame pointer.  This might
5962          be %fp, or might be %sp+offset.  */
5963       /* ??? What if offset is too big? Perhaps the caller knows it isn't? */
5964       fprintf (file, "%s+%d", frame_base_name, frame_base_offset);
5965       return;
5966     case 'Y':
5967       /* Adjust the operand to take into account a RESTORE operation.  */
5968       if (GET_CODE (x) == CONST_INT)
5969         break;
5970       else if (GET_CODE (x) != REG)
5971         output_operand_lossage ("invalid %%Y operand");
5972       else if (REGNO (x) < 8)
5973         fputs (reg_names[REGNO (x)], file);
5974       else if (REGNO (x) >= 24 && REGNO (x) < 32)
5975         fputs (reg_names[REGNO (x)-16], file);
5976       else
5977         output_operand_lossage ("invalid %%Y operand");
5978       return;
5979     case 'L':
5980       /* Print out the low order register name of a register pair.  */
5981       if (WORDS_BIG_ENDIAN)
5982         fputs (reg_names[REGNO (x)+1], file);
5983       else
5984         fputs (reg_names[REGNO (x)], file);
5985       return;
5986     case 'H':
5987       /* Print out the high order register name of a register pair.  */
5988       if (WORDS_BIG_ENDIAN)
5989         fputs (reg_names[REGNO (x)], file);
5990       else
5991         fputs (reg_names[REGNO (x)+1], file);
5992       return;
5993     case 'R':
5994       /* Print out the second register name of a register pair or quad.
5995          I.e., R (%o0) => %o1.  */
5996       fputs (reg_names[REGNO (x)+1], file);
5997       return;
5998     case 'S':
5999       /* Print out the third register name of a register quad.
6000          I.e., S (%o0) => %o2.  */
6001       fputs (reg_names[REGNO (x)+2], file);
6002       return;
6003     case 'T':
6004       /* Print out the fourth register name of a register quad.
6005          I.e., T (%o0) => %o3.  */
6006       fputs (reg_names[REGNO (x)+3], file);
6007       return;
6008     case 'x':
6009       /* Print a condition code register.  */
6010       if (REGNO (x) == SPARC_ICC_REG)
6011         {
6012           /* We don't handle CC[X]_NOOVmode because they're not supposed
6013              to occur here.  */
6014           if (GET_MODE (x) == CCmode)
6015             fputs ("%icc", file);
6016           else if (GET_MODE (x) == CCXmode)
6017             fputs ("%xcc", file);
6018           else
6019             abort ();
6020         }
6021       else
6022         /* %fccN register */
6023         fputs (reg_names[REGNO (x)], file);
6024       return;
6025     case 'm':
6026       /* Print the operand's address only.  */
6027       output_address (XEXP (x, 0));
6028       return;
6029     case 'r':
6030       /* In this case we need a register.  Use %g0 if the
6031          operand is const0_rtx.  */
6032       if (x == const0_rtx
6033           || (GET_MODE (x) != VOIDmode && x == CONST0_RTX (GET_MODE (x))))
6034         {
6035           fputs ("%g0", file);
6036           return;
6037         }
6038       else
6039         break;
6040
6041     case 'A':
6042       switch (GET_CODE (x))
6043         {
6044         case IOR: fputs ("or", file); break;
6045         case AND: fputs ("and", file); break;
6046         case XOR: fputs ("xor", file); break;
6047         default: output_operand_lossage ("invalid %%A operand");
6048         }
6049       return;
6050
6051     case 'B':
6052       switch (GET_CODE (x))
6053         {
6054         case IOR: fputs ("orn", file); break;
6055         case AND: fputs ("andn", file); break;
6056         case XOR: fputs ("xnor", file); break;
6057         default: output_operand_lossage ("invalid %%B operand");
6058         }
6059       return;
6060
6061       /* These are used by the conditional move instructions.  */
6062     case 'c' :
6063     case 'C':
6064       {
6065         enum rtx_code rc = GET_CODE (x);
6066         
6067         if (code == 'c')
6068           {
6069             enum machine_mode mode = GET_MODE (XEXP (x, 0));
6070             if (mode == CCFPmode || mode == CCFPEmode)
6071               rc = reverse_condition_maybe_unordered (GET_CODE (x));
6072             else
6073               rc = reverse_condition (GET_CODE (x));
6074           }
6075         switch (rc)
6076           {
6077           case NE: fputs ("ne", file); break;
6078           case EQ: fputs ("e", file); break;
6079           case GE: fputs ("ge", file); break;
6080           case GT: fputs ("g", file); break;
6081           case LE: fputs ("le", file); break;
6082           case LT: fputs ("l", file); break;
6083           case GEU: fputs ("geu", file); break;
6084           case GTU: fputs ("gu", file); break;
6085           case LEU: fputs ("leu", file); break;
6086           case LTU: fputs ("lu", file); break;
6087           case LTGT: fputs ("lg", file); break;
6088           case UNORDERED: fputs ("u", file); break;
6089           case ORDERED: fputs ("o", file); break;
6090           case UNLT: fputs ("ul", file); break;
6091           case UNLE: fputs ("ule", file); break;
6092           case UNGT: fputs ("ug", file); break;
6093           case UNGE: fputs ("uge", file); break;
6094           case UNEQ: fputs ("ue", file); break;
6095           default: output_operand_lossage (code == 'c'
6096                                            ? "invalid %%c operand"
6097                                            : "invalid %%C operand");
6098           }
6099         return;
6100       }
6101
6102       /* These are used by the movr instruction pattern.  */
6103     case 'd':
6104     case 'D':
6105       {
6106         enum rtx_code rc = (code == 'd'
6107                             ? reverse_condition (GET_CODE (x))
6108                             : GET_CODE (x));
6109         switch (rc)
6110           {
6111           case NE: fputs ("ne", file); break;
6112           case EQ: fputs ("e", file); break;
6113           case GE: fputs ("gez", file); break;
6114           case LT: fputs ("lz", file); break;
6115           case LE: fputs ("lez", file); break;
6116           case GT: fputs ("gz", file); break;
6117           default: output_operand_lossage (code == 'd'
6118                                            ? "invalid %%d operand"
6119                                            : "invalid %%D operand");
6120           }
6121         return;
6122       }
6123
6124     case 'b':
6125       {
6126         /* Print a sign-extended character.  */
6127         int i = trunc_int_for_mode (INTVAL (x), QImode);
6128         fprintf (file, "%d", i);
6129         return;
6130       }
6131
6132     case 'f':
6133       /* Operand must be a MEM; write its address.  */
6134       if (GET_CODE (x) != MEM)
6135         output_operand_lossage ("invalid %%f operand");
6136       output_address (XEXP (x, 0));
6137       return;
6138
6139     case 0:
6140       /* Do nothing special.  */
6141       break;
6142
6143     default:
6144       /* Undocumented flag.  */
6145       output_operand_lossage ("invalid operand output code");
6146     }
6147
6148   if (GET_CODE (x) == REG)
6149     fputs (reg_names[REGNO (x)], file);
6150   else if (GET_CODE (x) == MEM)
6151     {
6152       fputc ('[', file);
6153         /* Poor Sun assembler doesn't understand absolute addressing.  */
6154       if (CONSTANT_P (XEXP (x, 0)))
6155         fputs ("%g0+", file);
6156       output_address (XEXP (x, 0));
6157       fputc (']', file);
6158     }
6159   else if (GET_CODE (x) == HIGH)
6160     {
6161       fputs ("%hi(", file);
6162       output_addr_const (file, XEXP (x, 0));
6163       fputc (')', file);
6164     }
6165   else if (GET_CODE (x) == LO_SUM)
6166     {
6167       print_operand (file, XEXP (x, 0), 0);
6168       if (TARGET_CM_MEDMID)
6169         fputs ("+%l44(", file);
6170       else
6171         fputs ("+%lo(", file);
6172       output_addr_const (file, XEXP (x, 1));
6173       fputc (')', file);
6174     }
6175   else if (GET_CODE (x) == CONST_DOUBLE
6176            && (GET_MODE (x) == VOIDmode
6177                || GET_MODE_CLASS (GET_MODE (x)) == MODE_INT))
6178     {
6179       if (CONST_DOUBLE_HIGH (x) == 0)
6180         fprintf (file, "%u", (unsigned int) CONST_DOUBLE_LOW (x));
6181       else if (CONST_DOUBLE_HIGH (x) == -1
6182                && CONST_DOUBLE_LOW (x) < 0)
6183         fprintf (file, "%d", (int) CONST_DOUBLE_LOW (x));
6184       else
6185         output_operand_lossage ("long long constant not a valid immediate operand");
6186     }
6187   else if (GET_CODE (x) == CONST_DOUBLE)
6188     output_operand_lossage ("floating point constant not a valid immediate operand");
6189   else { output_addr_const (file, x); }
6190 }
6191 \f
6192 /* Target hook for assembling integer objects.  The sparc version has
6193    special handling for aligned DI-mode objects.  */
6194
6195 static bool
6196 sparc_assemble_integer (x, size, aligned_p)
6197      rtx x;
6198      unsigned int size;
6199      int aligned_p;
6200 {
6201   /* ??? We only output .xword's for symbols and only then in environments
6202      where the assembler can handle them.  */
6203   if (aligned_p && size == 8
6204       && (GET_CODE (x) != CONST_INT && GET_CODE (x) != CONST_DOUBLE))
6205     {
6206       if (TARGET_V9)
6207         {
6208           assemble_integer_with_op ("\t.xword\t", x);
6209           return true;
6210         }
6211       else
6212         {
6213           assemble_aligned_integer (4, const0_rtx);
6214           assemble_aligned_integer (4, x);
6215           return true;
6216         }
6217     }
6218   return default_assemble_integer (x, size, aligned_p);
6219 }
6220 \f
6221 /* Return the value of a code used in the .proc pseudo-op that says
6222    what kind of result this function returns.  For non-C types, we pick
6223    the closest C type.  */
6224
6225 #ifndef SHORT_TYPE_SIZE
6226 #define SHORT_TYPE_SIZE (BITS_PER_UNIT * 2)
6227 #endif
6228
6229 #ifndef INT_TYPE_SIZE
6230 #define INT_TYPE_SIZE BITS_PER_WORD
6231 #endif
6232
6233 #ifndef LONG_TYPE_SIZE
6234 #define LONG_TYPE_SIZE BITS_PER_WORD
6235 #endif
6236
6237 #ifndef LONG_LONG_TYPE_SIZE
6238 #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
6239 #endif
6240
6241 #ifndef FLOAT_TYPE_SIZE
6242 #define FLOAT_TYPE_SIZE BITS_PER_WORD
6243 #endif
6244
6245 #ifndef DOUBLE_TYPE_SIZE
6246 #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
6247 #endif
6248
6249 #ifndef LONG_DOUBLE_TYPE_SIZE
6250 #define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
6251 #endif
6252
6253 unsigned long
6254 sparc_type_code (type)
6255      register tree type;
6256 {
6257   register unsigned long qualifiers = 0;
6258   register unsigned shift;
6259
6260   /* Only the first 30 bits of the qualifier are valid.  We must refrain from
6261      setting more, since some assemblers will give an error for this.  Also,
6262      we must be careful to avoid shifts of 32 bits or more to avoid getting
6263      unpredictable results.  */
6264
6265   for (shift = 6; shift < 30; shift += 2, type = TREE_TYPE (type))
6266     {
6267       switch (TREE_CODE (type))
6268         {
6269         case ERROR_MARK:
6270           return qualifiers;
6271   
6272         case ARRAY_TYPE:
6273           qualifiers |= (3 << shift);
6274           break;
6275
6276         case FUNCTION_TYPE:
6277         case METHOD_TYPE:
6278           qualifiers |= (2 << shift);
6279           break;
6280
6281         case POINTER_TYPE:
6282         case REFERENCE_TYPE:
6283         case OFFSET_TYPE:
6284           qualifiers |= (1 << shift);
6285           break;
6286
6287         case RECORD_TYPE:
6288           return (qualifiers | 8);
6289
6290         case UNION_TYPE:
6291         case QUAL_UNION_TYPE:
6292           return (qualifiers | 9);
6293
6294         case ENUMERAL_TYPE:
6295           return (qualifiers | 10);
6296
6297         case VOID_TYPE:
6298           return (qualifiers | 16);
6299
6300         case INTEGER_TYPE:
6301           /* If this is a range type, consider it to be the underlying
6302              type.  */
6303           if (TREE_TYPE (type) != 0)
6304             break;
6305
6306           /* Carefully distinguish all the standard types of C,
6307              without messing up if the language is not C.  We do this by
6308              testing TYPE_PRECISION and TREE_UNSIGNED.  The old code used to
6309              look at both the names and the above fields, but that's redundant.
6310              Any type whose size is between two C types will be considered
6311              to be the wider of the two types.  Also, we do not have a
6312              special code to use for "long long", so anything wider than
6313              long is treated the same.  Note that we can't distinguish
6314              between "int" and "long" in this code if they are the same
6315              size, but that's fine, since neither can the assembler.  */
6316
6317           if (TYPE_PRECISION (type) <= CHAR_TYPE_SIZE)
6318             return (qualifiers | (TREE_UNSIGNED (type) ? 12 : 2));
6319   
6320           else if (TYPE_PRECISION (type) <= SHORT_TYPE_SIZE)
6321             return (qualifiers | (TREE_UNSIGNED (type) ? 13 : 3));
6322   
6323           else if (TYPE_PRECISION (type) <= INT_TYPE_SIZE)
6324             return (qualifiers | (TREE_UNSIGNED (type) ? 14 : 4));
6325   
6326           else
6327             return (qualifiers | (TREE_UNSIGNED (type) ? 15 : 5));
6328   
6329         case REAL_TYPE:
6330           /* If this is a range type, consider it to be the underlying
6331              type.  */
6332           if (TREE_TYPE (type) != 0)
6333             break;
6334
6335           /* Carefully distinguish all the standard types of C,
6336              without messing up if the language is not C.  */
6337
6338           if (TYPE_PRECISION (type) == FLOAT_TYPE_SIZE)
6339             return (qualifiers | 6);
6340
6341           else 
6342             return (qualifiers | 7);
6343   
6344         case COMPLEX_TYPE:      /* GNU Fortran COMPLEX type.  */
6345           /* ??? We need to distinguish between double and float complex types,
6346              but I don't know how yet because I can't reach this code from
6347              existing front-ends.  */
6348           return (qualifiers | 7);      /* Who knows? */
6349
6350         case CHAR_TYPE:         /* GNU Pascal CHAR type.  Not used in C.  */
6351         case BOOLEAN_TYPE:      /* GNU Fortran BOOLEAN type.  */
6352         case FILE_TYPE:         /* GNU Pascal FILE type.  */
6353         case SET_TYPE:          /* GNU Pascal SET type.  */
6354         case LANG_TYPE:         /* ? */
6355           return qualifiers;
6356   
6357         default:
6358           abort ();             /* Not a type! */
6359         }
6360     }
6361
6362   return qualifiers;
6363 }
6364 \f
6365 /* Nested function support.  */
6366
6367 /* Emit RTL insns to initialize the variable parts of a trampoline.
6368    FNADDR is an RTX for the address of the function's pure code.
6369    CXT is an RTX for the static chain value for the function.
6370
6371    This takes 16 insns: 2 shifts & 2 ands (to split up addresses), 4 sethi
6372    (to load in opcodes), 4 iors (to merge address and opcodes), and 4 writes
6373    (to store insns).  This is a bit excessive.  Perhaps a different
6374    mechanism would be better here.
6375
6376    Emit enough FLUSH insns to synchronize the data and instruction caches.  */
6377
6378 void
6379 sparc_initialize_trampoline (tramp, fnaddr, cxt)
6380      rtx tramp, fnaddr, cxt;
6381 {
6382   /* SPARC 32 bit trampoline:
6383
6384         sethi   %hi(fn), %g1
6385         sethi   %hi(static), %g2
6386         jmp     %g1+%lo(fn)
6387         or      %g2, %lo(static), %g2
6388
6389     SETHI i,r  = 00rr rrr1 00ii iiii iiii iiii iiii iiii
6390     JMPL r+i,d = 10dd ddd1 1100 0rrr rr1i iiii iiii iiii
6391    */
6392 #ifdef TRANSFER_FROM_TRAMPOLINE
6393   emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "__enable_execute_stack"),
6394                      0, VOIDmode, 1, tramp, Pmode);
6395 #endif
6396
6397   emit_move_insn
6398     (gen_rtx_MEM (SImode, plus_constant (tramp, 0)),
6399      expand_binop (SImode, ior_optab,
6400                    expand_shift (RSHIFT_EXPR, SImode, fnaddr,
6401                                  size_int (10), 0, 1),
6402                    GEN_INT (trunc_int_for_mode (0x03000000, SImode)),
6403                    NULL_RTX, 1, OPTAB_DIRECT));
6404
6405   emit_move_insn
6406     (gen_rtx_MEM (SImode, plus_constant (tramp, 4)),
6407      expand_binop (SImode, ior_optab,
6408                    expand_shift (RSHIFT_EXPR, SImode, cxt,
6409                                  size_int (10), 0, 1),
6410                    GEN_INT (trunc_int_for_mode (0x05000000, SImode)),
6411                    NULL_RTX, 1, OPTAB_DIRECT));
6412
6413   emit_move_insn
6414     (gen_rtx_MEM (SImode, plus_constant (tramp, 8)),
6415      expand_binop (SImode, ior_optab,
6416                    expand_and (SImode, fnaddr, GEN_INT (0x3ff), NULL_RTX),
6417                    GEN_INT (trunc_int_for_mode (0x81c06000, SImode)),
6418                    NULL_RTX, 1, OPTAB_DIRECT));
6419
6420   emit_move_insn
6421     (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
6422      expand_binop (SImode, ior_optab,
6423                    expand_and (SImode, cxt, GEN_INT (0x3ff), NULL_RTX),
6424                    GEN_INT (trunc_int_for_mode (0x8410a000, SImode)),
6425                    NULL_RTX, 1, OPTAB_DIRECT));
6426
6427   /* On UltraSPARC a flush flushes an entire cache line.  The trampoline is
6428      aligned on a 16 byte boundary so one flush clears it all.  */
6429   emit_insn (gen_flush (validize_mem (gen_rtx_MEM (SImode, tramp))));
6430   if (sparc_cpu != PROCESSOR_ULTRASPARC)
6431     emit_insn (gen_flush (validize_mem (gen_rtx_MEM (SImode,
6432                                                      plus_constant (tramp, 8)))));
6433 }
6434
6435 /* The 64 bit version is simpler because it makes more sense to load the
6436    values as "immediate" data out of the trampoline.  It's also easier since
6437    we can read the PC without clobbering a register.  */
6438
6439 void
6440 sparc64_initialize_trampoline (tramp, fnaddr, cxt)
6441      rtx tramp, fnaddr, cxt;
6442 {
6443 #ifdef TRANSFER_FROM_TRAMPOLINE
6444   emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "__enable_execute_stack"),
6445                      0, VOIDmode, 1, tramp, Pmode);
6446 #endif
6447
6448   /*
6449         rd      %pc, %g1
6450         ldx     [%g1+24], %g5
6451         jmp     %g5
6452         ldx     [%g1+16], %g5
6453         +16 bytes data
6454    */
6455
6456   emit_move_insn (gen_rtx_MEM (SImode, tramp),
6457                   GEN_INT (trunc_int_for_mode (0x83414000, SImode)));
6458   emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 4)),
6459                   GEN_INT (trunc_int_for_mode (0xca586018, SImode)));
6460   emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 8)),
6461                   GEN_INT (trunc_int_for_mode (0x81c14000, SImode)));
6462   emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
6463                   GEN_INT (trunc_int_for_mode (0xca586010, SImode)));
6464   emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 16)), cxt);
6465   emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 24)), fnaddr);
6466   emit_insn (gen_flushdi (validize_mem (gen_rtx_MEM (DImode, tramp))));
6467
6468   if (sparc_cpu != PROCESSOR_ULTRASPARC)
6469     emit_insn (gen_flushdi (validize_mem (gen_rtx_MEM (DImode, plus_constant (tramp, 8)))));
6470 }
6471 \f
6472 /* Subroutines to support a flat (single) register window calling
6473    convention.  */
6474
6475 /* Single-register window sparc stack frames look like:
6476
6477              Before call                        After call
6478         +-----------------------+       +-----------------------+
6479    high |                       |       |                       |
6480    mem  |  caller's temps.      |       |  caller's temps.      |
6481         |                       |       |                       |
6482         +-----------------------+       +-----------------------+
6483         |                       |       |                       |
6484         |  arguments on stack.  |       |  arguments on stack.  |
6485         |                       |       |                       |
6486         +-----------------------+FP+92->+-----------------------+
6487         |  6 words to save      |       |  6 words to save      |
6488         |  arguments passed     |       |  arguments passed     |
6489         |  in registers, even   |       |  in registers, even   |
6490         |  if not passed.       |       |  if not passed.       |
6491  SP+68->+-----------------------+FP+68->+-----------------------+
6492         | 1 word struct addr    |       | 1 word struct addr    |
6493         +-----------------------+FP+64->+-----------------------+
6494         |                       |       |                       |
6495         | 16 word reg save area |       | 16 word reg save area |
6496         |                       |       |                       |
6497     SP->+-----------------------+   FP->+-----------------------+
6498                                         | 4 word area for       |
6499                                         | fp/alu reg moves      |
6500                                  FP-16->+-----------------------+
6501                                         |                       |
6502                                         |  local variables      |
6503                                         |                       |
6504                                         +-----------------------+
6505                                         |                       |
6506                                         |  fp register save     |
6507                                         |                       |
6508                                         +-----------------------+
6509                                         |                       |
6510                                         |  gp register save     |
6511                                         |                       |
6512                                         +-----------------------+
6513                                         |                       |
6514                                         |  alloca allocations   |
6515                                         |                       |
6516                                         +-----------------------+
6517                                         |                       |
6518                                         |  arguments on stack   |
6519                                         |                       |
6520                                  SP+92->+-----------------------+
6521                                         |  6 words to save      |
6522                                         |  arguments passed     |
6523                                         |  in registers, even   |
6524    low                                  |  if not passed.       |
6525    memory                        SP+68->+-----------------------+
6526                                         | 1 word struct addr    |
6527                                  SP+64->+-----------------------+
6528                                         |                       |
6529                                         I 16 word reg save area |
6530                                         |                       |
6531                                     SP->+-----------------------+  */
6532
6533 /* Structure to be filled in by sparc_flat_compute_frame_size with register
6534    save masks, and offsets for the current function.  */
6535
6536 struct sparc_frame_info
6537 {
6538   unsigned long total_size;     /* # bytes that the entire frame takes up.  */
6539   unsigned long var_size;       /* # bytes that variables take up.  */
6540   unsigned long args_size;      /* # bytes that outgoing arguments take up.  */
6541   unsigned long extra_size;     /* # bytes of extra gunk.  */
6542   unsigned int  gp_reg_size;    /* # bytes needed to store gp regs.  */
6543   unsigned int  fp_reg_size;    /* # bytes needed to store fp regs.  */
6544   unsigned long gmask;          /* Mask of saved gp registers.  */
6545   unsigned long fmask;          /* Mask of saved fp registers.  */
6546   unsigned long reg_offset;     /* Offset from new sp to store regs.  */
6547   int           initialized;    /* Nonzero if frame size already calculated.  */
6548 };
6549
6550 /* Current frame information calculated by sparc_flat_compute_frame_size.  */
6551 struct sparc_frame_info current_frame_info;
6552
6553 /* Zero structure to initialize current_frame_info.  */
6554 struct sparc_frame_info zero_frame_info;
6555
6556 /* Tell prologue and epilogue if register REGNO should be saved / restored.  */
6557
6558 #define RETURN_ADDR_REGNUM 15
6559 #define HARD_FRAME_POINTER_MASK (1 << (HARD_FRAME_POINTER_REGNUM))
6560 #define RETURN_ADDR_MASK (1 << (RETURN_ADDR_REGNUM))
6561
6562 #define MUST_SAVE_REGISTER(regno) \
6563  ((regs_ever_live[regno] && !call_used_regs[regno])                     \
6564   || (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)       \
6565   || (regno == RETURN_ADDR_REGNUM && regs_ever_live[RETURN_ADDR_REGNUM]))
6566
6567 /* Return the bytes needed to compute the frame pointer from the current
6568    stack pointer.  */
6569
6570 unsigned long
6571 sparc_flat_compute_frame_size (size)
6572      int size;                  /* # of var. bytes allocated.  */
6573 {
6574   int regno;
6575   unsigned long total_size;     /* # bytes that the entire frame takes up.  */
6576   unsigned long var_size;       /* # bytes that variables take up.  */
6577   unsigned long args_size;      /* # bytes that outgoing arguments take up.  */
6578   unsigned long extra_size;     /* # extra bytes.  */
6579   unsigned int  gp_reg_size;    /* # bytes needed to store gp regs.  */
6580   unsigned int  fp_reg_size;    /* # bytes needed to store fp regs.  */
6581   unsigned long gmask;          /* Mask of saved gp registers.  */
6582   unsigned long fmask;          /* Mask of saved fp registers.  */
6583   unsigned long reg_offset;     /* Offset to register save area.  */
6584   int           need_aligned_p; /* 1 if need the save area 8 byte aligned.  */
6585
6586   /* This is the size of the 16 word reg save area, 1 word struct addr
6587      area, and 4 word fp/alu register copy area.  */
6588   extra_size = -STARTING_FRAME_OFFSET + FIRST_PARM_OFFSET(0);
6589   var_size = size;
6590   gp_reg_size = 0;
6591   fp_reg_size = 0;
6592   gmask = 0;
6593   fmask = 0;
6594   reg_offset = 0;
6595   need_aligned_p = 0;
6596
6597   args_size = 0;
6598   if (!leaf_function_p ())
6599     {
6600       /* Also include the size needed for the 6 parameter registers.  */
6601       args_size = current_function_outgoing_args_size + 24;
6602     }
6603   total_size = var_size + args_size;
6604
6605   /* Calculate space needed for gp registers.  */
6606   for (regno = 1; regno <= 31; regno++)
6607     {
6608       if (MUST_SAVE_REGISTER (regno))
6609         {
6610           /* If we need to save two regs in a row, ensure there's room to bump
6611              up the address to align it to a doubleword boundary.  */
6612           if ((regno & 0x1) == 0 && MUST_SAVE_REGISTER (regno+1))
6613             {
6614               if (gp_reg_size % 8 != 0)
6615                 gp_reg_size += 4;
6616               gp_reg_size += 2 * UNITS_PER_WORD;
6617               gmask |= 3 << regno;
6618               regno++;
6619               need_aligned_p = 1;
6620             }
6621           else
6622             {
6623               gp_reg_size += UNITS_PER_WORD;
6624               gmask |= 1 << regno;
6625             }
6626         }
6627     }
6628
6629   /* Calculate space needed for fp registers.  */
6630   for (regno = 32; regno <= 63; regno++)
6631     {
6632       if (regs_ever_live[regno] && !call_used_regs[regno])
6633         {
6634           fp_reg_size += UNITS_PER_WORD;
6635           fmask |= 1 << (regno - 32);
6636         }
6637     }
6638
6639   if (gmask || fmask)
6640     {
6641       int n;
6642       reg_offset = FIRST_PARM_OFFSET(0) + args_size;
6643       /* Ensure save area is 8 byte aligned if we need it.  */
6644       n = reg_offset % 8;
6645       if (need_aligned_p && n != 0)
6646         {
6647           total_size += 8 - n;
6648           reg_offset += 8 - n;
6649         }
6650       total_size += gp_reg_size + fp_reg_size;
6651     }
6652
6653   /* If we must allocate a stack frame at all, we must also allocate 
6654      room for register window spillage, so as to be binary compatible
6655      with libraries and operating systems that do not use -mflat.  */
6656   if (total_size > 0)
6657     total_size += extra_size;
6658   else
6659     extra_size = 0;
6660
6661   total_size = SPARC_STACK_ALIGN (total_size);
6662
6663   /* Save other computed information.  */
6664   current_frame_info.total_size  = total_size;
6665   current_frame_info.var_size    = var_size;
6666   current_frame_info.args_size   = args_size;
6667   current_frame_info.extra_size  = extra_size;
6668   current_frame_info.gp_reg_size = gp_reg_size;
6669   current_frame_info.fp_reg_size = fp_reg_size;
6670   current_frame_info.gmask       = gmask;
6671   current_frame_info.fmask       = fmask;
6672   current_frame_info.reg_offset  = reg_offset;
6673   current_frame_info.initialized = reload_completed;
6674
6675   /* Ok, we're done.  */
6676   return total_size;
6677 }
6678 \f
6679 /* Save/restore registers in GMASK and FMASK at register BASE_REG plus offset
6680    OFFSET.
6681
6682    BASE_REG must be 8 byte aligned.  This allows us to test OFFSET for
6683    appropriate alignment and use DOUBLEWORD_OP when we can.  We assume
6684    [BASE_REG+OFFSET] will always be a valid address.
6685
6686    WORD_OP is either "st" for save, "ld" for restore.
6687    DOUBLEWORD_OP is either "std" for save, "ldd" for restore.  */
6688
6689 void
6690 sparc_flat_save_restore (file, base_reg, offset, gmask, fmask, word_op,
6691                          doubleword_op, base_offset)
6692      FILE *file;
6693      const char *base_reg;
6694      unsigned int offset;
6695      unsigned long gmask;
6696      unsigned long fmask;
6697      const char *word_op;
6698      const char *doubleword_op;
6699      unsigned long base_offset;
6700 {
6701   int regno;
6702
6703   if (gmask == 0 && fmask == 0)
6704     return;
6705
6706   /* Save registers starting from high to low.  We've already saved the
6707      previous frame pointer and previous return address for the debugger's
6708      sake.  The debugger allows us to not need a nop in the epilog if at least
6709      one register is reloaded in addition to return address.  */
6710
6711   if (gmask)
6712     {
6713       for (regno = 1; regno <= 31; regno++)
6714         {
6715           if ((gmask & (1L << regno)) != 0)
6716             {
6717               if ((regno & 0x1) == 0 && ((gmask & (1L << (regno+1))) != 0))
6718                 {
6719                   /* We can save two registers in a row.  If we're not at a
6720                      double word boundary, move to one.
6721                      sparc_flat_compute_frame_size ensures there's room to do
6722                      this.  */
6723                   if (offset % 8 != 0)
6724                     offset += UNITS_PER_WORD;
6725
6726                   if (word_op[0] == 's')
6727                     {
6728                       fprintf (file, "\t%s\t%s, [%s+%d]\n",
6729                                doubleword_op, reg_names[regno],
6730                                base_reg, offset);
6731                       if (dwarf2out_do_frame ())
6732                         {
6733                           char *l = dwarf2out_cfi_label ();
6734                           dwarf2out_reg_save (l, regno, offset + base_offset);
6735                           dwarf2out_reg_save
6736                             (l, regno+1, offset+base_offset + UNITS_PER_WORD);
6737                         }
6738                     }
6739                   else
6740                     fprintf (file, "\t%s\t[%s+%d], %s\n",
6741                              doubleword_op, base_reg, offset,
6742                              reg_names[regno]);
6743
6744                   offset += 2 * UNITS_PER_WORD;
6745                   regno++;
6746                 }
6747               else
6748                 {
6749                   if (word_op[0] == 's')
6750                     {
6751                       fprintf (file, "\t%s\t%s, [%s+%d]\n",
6752                                word_op, reg_names[regno],
6753                                base_reg, offset);
6754                       if (dwarf2out_do_frame ())
6755                         dwarf2out_reg_save ("", regno, offset + base_offset);
6756                     }
6757                   else
6758                     fprintf (file, "\t%s\t[%s+%d], %s\n",
6759                              word_op, base_reg, offset, reg_names[regno]);
6760
6761                   offset += UNITS_PER_WORD;
6762                 }
6763             }
6764         }
6765     }
6766
6767   if (fmask)
6768     {
6769       for (regno = 32; regno <= 63; regno++)
6770         {
6771           if ((fmask & (1L << (regno - 32))) != 0)
6772             {
6773               if (word_op[0] == 's')
6774                 {
6775                   fprintf (file, "\t%s\t%s, [%s+%d]\n",
6776                            word_op, reg_names[regno],
6777                            base_reg, offset);
6778                   if (dwarf2out_do_frame ())
6779                     dwarf2out_reg_save ("", regno, offset + base_offset);
6780                 }
6781               else
6782                 fprintf (file, "\t%s\t[%s+%d], %s\n",
6783                          word_op, base_reg, offset, reg_names[regno]);
6784
6785               offset += UNITS_PER_WORD;
6786             }
6787         }
6788     }
6789 }
6790 \f
6791 /* Set up the stack and frame (if desired) for the function.  */
6792
6793 static void
6794 sparc_flat_function_prologue (file, size)
6795      FILE *file;
6796      HOST_WIDE_INT size;
6797 {
6798   const char *sp_str = reg_names[STACK_POINTER_REGNUM];
6799   unsigned long gmask = current_frame_info.gmask;
6800
6801   sparc_output_scratch_registers (file);
6802
6803   /* This is only for the human reader.  */
6804   fprintf (file, "\t%s#PROLOGUE# 0\n", ASM_COMMENT_START);
6805   fprintf (file, "\t%s# vars= %ld, regs= %d/%d, args= %d, extra= %ld\n",
6806            ASM_COMMENT_START,
6807            current_frame_info.var_size,
6808            current_frame_info.gp_reg_size / 4,
6809            current_frame_info.fp_reg_size / 4,
6810            current_function_outgoing_args_size,
6811            current_frame_info.extra_size);
6812
6813   size = SPARC_STACK_ALIGN (size);
6814   size = (! current_frame_info.initialized
6815           ? sparc_flat_compute_frame_size (size)
6816           : current_frame_info.total_size);
6817
6818   /* These cases shouldn't happen.  Catch them now.  */
6819   if (size == 0 && (gmask || current_frame_info.fmask))
6820     abort ();
6821
6822   /* Allocate our stack frame by decrementing %sp.
6823      At present, the only algorithm gdb can use to determine if this is a
6824      flat frame is if we always set %i7 if we set %sp.  This can be optimized
6825      in the future by putting in some sort of debugging information that says
6826      this is a `flat' function.  However, there is still the case of debugging
6827      code without such debugging information (including cases where most fns
6828      have such info, but there is one that doesn't).  So, always do this now
6829      so we don't get a lot of code out there that gdb can't handle.
6830      If the frame pointer isn't needn't then that's ok - gdb won't be able to
6831      distinguish us from a non-flat function but there won't (and shouldn't)
6832      be any differences anyway.  The return pc is saved (if necessary) right
6833      after %i7 so gdb won't have to look too far to find it.  */
6834   if (size > 0)
6835     {
6836       unsigned int reg_offset = current_frame_info.reg_offset;
6837       const char *const fp_str = reg_names[HARD_FRAME_POINTER_REGNUM];
6838       static const char *const t1_str = "%g1";
6839
6840       /* Things get a little tricky if local variables take up more than ~4096
6841          bytes and outgoing arguments take up more than ~4096 bytes.  When that
6842          happens, the register save area can't be accessed from either end of
6843          the frame.  Handle this by decrementing %sp to the start of the gp
6844          register save area, save the regs, update %i7, and then set %sp to its
6845          final value.  Given that we only have one scratch register to play
6846          with it is the cheapest solution, and it helps gdb out as it won't
6847          slow down recognition of flat functions.
6848          Don't change the order of insns emitted here without checking with
6849          the gdb folk first.  */
6850
6851       /* Is the entire register save area offsettable from %sp?  */
6852       if (reg_offset < 4096 - 64 * (unsigned) UNITS_PER_WORD)
6853         {
6854           if (size <= 4096)
6855             {
6856               fprintf (file, "\tadd\t%s, %d, %s\n",
6857                        sp_str, (int) -size, sp_str);
6858               if (gmask & HARD_FRAME_POINTER_MASK)
6859                 {
6860                   fprintf (file, "\tst\t%s, [%s+%d]\n",
6861                            fp_str, sp_str, reg_offset);
6862                   fprintf (file, "\tsub\t%s, %d, %s\t%s# set up frame pointer\n",
6863                            sp_str, (int) -size, fp_str, ASM_COMMENT_START);
6864                   reg_offset += 4;
6865                 }
6866             }
6867           else
6868             {
6869               fprintf (file, "\tset\t");
6870               fprintf (file, HOST_WIDE_INT_PRINT_DEC, size);
6871               fprintf (file, ", %s\n\tsub\t%s, %s, %s\n",
6872                        t1_str, sp_str, t1_str, sp_str);
6873               if (gmask & HARD_FRAME_POINTER_MASK)
6874                 {
6875                   fprintf (file, "\tst\t%s, [%s+%d]\n",
6876                            fp_str, sp_str, reg_offset);
6877                   fprintf (file, "\tadd\t%s, %s, %s\t%s# set up frame pointer\n",
6878                            sp_str, t1_str, fp_str, ASM_COMMENT_START);
6879                   reg_offset += 4;
6880                 }
6881             }
6882           if (dwarf2out_do_frame ())
6883             {
6884               char *l = dwarf2out_cfi_label ();
6885               if (gmask & HARD_FRAME_POINTER_MASK)
6886                 {
6887                   dwarf2out_reg_save (l, HARD_FRAME_POINTER_REGNUM,
6888                                       reg_offset - 4 - size);
6889                   dwarf2out_def_cfa (l, HARD_FRAME_POINTER_REGNUM, 0);
6890                 }
6891               else
6892                 dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, size);
6893             }
6894           if (gmask & RETURN_ADDR_MASK)
6895             {
6896               fprintf (file, "\tst\t%s, [%s+%d]\n",
6897                        reg_names[RETURN_ADDR_REGNUM], sp_str, reg_offset);
6898               if (dwarf2out_do_frame ())
6899                 dwarf2out_return_save ("", reg_offset - size);
6900               reg_offset += 4;
6901             }
6902           sparc_flat_save_restore (file, sp_str, reg_offset,
6903                                    gmask & ~(HARD_FRAME_POINTER_MASK | RETURN_ADDR_MASK),
6904                                    current_frame_info.fmask,
6905                                    "st", "std", -size);
6906         }
6907       else
6908         {
6909           /* Subtract %sp in two steps, but make sure there is always a
6910              64 byte register save area, and %sp is properly aligned.  */
6911           /* Amount to decrement %sp by, the first time.  */
6912           unsigned HOST_WIDE_INT size1 = ((size - reg_offset + 64) + 15) & -16;
6913           /* Offset to register save area from %sp.  */
6914           unsigned HOST_WIDE_INT offset = size1 - (size - reg_offset);
6915           
6916           if (size1 <= 4096)
6917             {
6918               fprintf (file, "\tadd\t%s, %d, %s\n",
6919                        sp_str, (int) -size1, sp_str);
6920               if (gmask & HARD_FRAME_POINTER_MASK)
6921                 {
6922                   fprintf (file, "\tst\t%s, [%s+%d]\n\tsub\t%s, %d, %s\t%s# set up frame pointer\n",
6923                            fp_str, sp_str, (int) offset, sp_str, (int) -size1,
6924                            fp_str, ASM_COMMENT_START);
6925                   offset += 4;
6926                 }
6927             }
6928           else
6929             {
6930               fprintf (file, "\tset\t");
6931               fprintf (file, HOST_WIDE_INT_PRINT_DEC, size1);
6932               fprintf (file, ", %s\n\tsub\t%s, %s, %s\n",
6933                        t1_str, sp_str, t1_str, sp_str);
6934               if (gmask & HARD_FRAME_POINTER_MASK)
6935                 {
6936                   fprintf (file, "\tst\t%s, [%s+%d]\n\tadd\t%s, %s, %s\t%s# set up frame pointer\n",
6937                            fp_str, sp_str, (int) offset, sp_str, t1_str,
6938                            fp_str, ASM_COMMENT_START);
6939                   offset += 4;
6940                 }
6941             }
6942           if (dwarf2out_do_frame ())
6943             {
6944               char *l = dwarf2out_cfi_label ();
6945               if (gmask & HARD_FRAME_POINTER_MASK)
6946                 {
6947                   dwarf2out_reg_save (l, HARD_FRAME_POINTER_REGNUM,
6948                                       offset - 4 - size1);
6949                   dwarf2out_def_cfa (l, HARD_FRAME_POINTER_REGNUM, 0);
6950                 }
6951               else
6952                 dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, size1);
6953             }
6954           if (gmask & RETURN_ADDR_MASK)
6955             {
6956               fprintf (file, "\tst\t%s, [%s+%d]\n",
6957                        reg_names[RETURN_ADDR_REGNUM], sp_str, (int) offset);
6958               if (dwarf2out_do_frame ())
6959                 /* offset - size1 == reg_offset - size
6960                    if reg_offset were updated above like offset.  */
6961                 dwarf2out_return_save ("", offset - size1);
6962               offset += 4;
6963             }
6964           sparc_flat_save_restore (file, sp_str, offset,
6965                                    gmask & ~(HARD_FRAME_POINTER_MASK | RETURN_ADDR_MASK),
6966                                    current_frame_info.fmask,
6967                                    "st", "std", -size1);
6968           fprintf (file, "\tset\t");
6969           fprintf (file, HOST_WIDE_INT_PRINT_DEC, size - size1);
6970           fprintf (file, ", %s\n\tsub\t%s, %s, %s\n",
6971                    t1_str, sp_str, t1_str, sp_str);
6972           if (dwarf2out_do_frame ())
6973             if (! (gmask & HARD_FRAME_POINTER_MASK))
6974               dwarf2out_def_cfa ("", STACK_POINTER_REGNUM, size);
6975         }
6976     }
6977
6978   fprintf (file, "\t%s#PROLOGUE# 1\n", ASM_COMMENT_START);
6979 }
6980 \f
6981 /* Do any necessary cleanup after a function to restore stack, frame,
6982    and regs.  */
6983
6984 static void
6985 sparc_flat_function_epilogue (file, size)
6986      FILE *file;
6987      HOST_WIDE_INT size;
6988 {
6989   rtx epilogue_delay = current_function_epilogue_delay_list;
6990   int noepilogue = FALSE;
6991
6992   /* This is only for the human reader.  */
6993   fprintf (file, "\t%s#EPILOGUE#\n", ASM_COMMENT_START);
6994
6995   /* The epilogue does not depend on any registers, but the stack
6996      registers, so we assume that if we have 1 pending nop, it can be
6997      ignored, and 2 it must be filled (2 nops occur for integer
6998      multiply and divide).  */
6999
7000   size = SPARC_STACK_ALIGN (size);
7001   size = (!current_frame_info.initialized
7002            ? sparc_flat_compute_frame_size (size)
7003            : current_frame_info.total_size);
7004
7005   if (size == 0 && epilogue_delay == 0)
7006     {
7007       rtx insn = get_last_insn ();
7008
7009       /* If the last insn was a BARRIER, we don't have to write any code
7010          because a jump (aka return) was put there.  */
7011       if (GET_CODE (insn) == NOTE)
7012         insn = prev_nonnote_insn (insn);
7013       if (insn && GET_CODE (insn) == BARRIER)
7014         noepilogue = TRUE;
7015     }
7016
7017   if (!noepilogue)
7018     {
7019       unsigned HOST_WIDE_INT reg_offset = current_frame_info.reg_offset;
7020       unsigned HOST_WIDE_INT size1;
7021       const char *const sp_str = reg_names[STACK_POINTER_REGNUM];
7022       const char *const fp_str = reg_names[HARD_FRAME_POINTER_REGNUM];
7023       static const char *const t1_str = "%g1";
7024
7025       /* In the reload sequence, we don't need to fill the load delay
7026          slots for most of the loads, also see if we can fill the final
7027          delay slot if not otherwise filled by the reload sequence.  */
7028
7029       if (size > 4095)
7030         {
7031           fprintf (file, "\tset\t");
7032           fprintf (file, HOST_WIDE_INT_PRINT_DEC, size);
7033           fprintf (file, ", %s\n", t1_str);
7034         }
7035
7036       if (frame_pointer_needed)
7037         {
7038           if (size > 4095)
7039             fprintf (file,"\tsub\t%s, %s, %s\t\t%s# sp not trusted here\n",
7040                      fp_str, t1_str, sp_str, ASM_COMMENT_START);
7041           else
7042             fprintf (file,"\tsub\t%s, %d, %s\t\t%s# sp not trusted here\n",
7043                      fp_str, (int) size, sp_str, ASM_COMMENT_START);
7044         }
7045
7046       /* Is the entire register save area offsettable from %sp?  */
7047       if (reg_offset < 4096 - 64 * (unsigned) UNITS_PER_WORD)
7048         {
7049           size1 = 0;
7050         }
7051       else
7052         {
7053           /* Restore %sp in two steps, but make sure there is always a
7054              64 byte register save area, and %sp is properly aligned.  */
7055           /* Amount to increment %sp by, the first time.  */
7056           size1 = ((reg_offset - 64 - 16) + 15) & -16;
7057           /* Offset to register save area from %sp.  */
7058           reg_offset = size1 - reg_offset;
7059
7060           fprintf (file, "\tset\t");
7061           fprintf (file, HOST_WIDE_INT_PRINT_DEC, size1);
7062           fprintf (file, ", %s\n\tadd\t%s, %s, %s\n",
7063                    t1_str, sp_str, t1_str, sp_str);
7064         }
7065
7066       /* We must restore the frame pointer and return address reg first
7067          because they are treated specially by the prologue output code.  */
7068       if (current_frame_info.gmask & HARD_FRAME_POINTER_MASK)
7069         {
7070           fprintf (file, "\tld\t[%s+%d], %s\n",
7071                    sp_str, (int) reg_offset, fp_str);
7072           reg_offset += 4;
7073         }
7074       if (current_frame_info.gmask & RETURN_ADDR_MASK)
7075         {
7076           fprintf (file, "\tld\t[%s+%d], %s\n",
7077                    sp_str, (int) reg_offset, reg_names[RETURN_ADDR_REGNUM]);
7078           reg_offset += 4;
7079         }
7080
7081       /* Restore any remaining saved registers.  */
7082       sparc_flat_save_restore (file, sp_str, reg_offset,
7083                                current_frame_info.gmask & ~(HARD_FRAME_POINTER_MASK | RETURN_ADDR_MASK),
7084                                current_frame_info.fmask,
7085                                "ld", "ldd", 0);
7086
7087       /* If we had to increment %sp in two steps, record it so the second
7088          restoration in the epilogue finishes up.  */
7089       if (size1 > 0)
7090         {
7091           size -= size1;
7092           if (size > 4095)
7093             {
7094               fprintf (file, "\tset\t");
7095               fprintf (file, HOST_WIDE_INT_PRINT_DEC, size);
7096               fprintf (file, ", %s\n", t1_str);
7097             }
7098         }
7099
7100       if (current_function_returns_struct)
7101         fprintf (file, "\tjmp\t%%o7+12\n");
7102       else
7103         fprintf (file, "\tretl\n");
7104
7105       /* If the only register saved is the return address, we need a
7106          nop, unless we have an instruction to put into it.  Otherwise
7107          we don't since reloading multiple registers doesn't reference
7108          the register being loaded.  */
7109
7110       if (epilogue_delay)
7111         {
7112           if (size)
7113             abort ();
7114           final_scan_insn (XEXP (epilogue_delay, 0), file, 1, -2, 1);
7115         }
7116
7117       else if (size > 4095)
7118         fprintf (file, "\tadd\t%s, %s, %s\n", sp_str, t1_str, sp_str);
7119
7120       else if (size > 0)
7121         fprintf (file, "\tadd\t%s, %d, %s\n", sp_str, (int) size, sp_str);
7122
7123       else
7124         fprintf (file, "\tnop\n");
7125     }
7126
7127   /* Reset state info for each function.  */
7128   current_frame_info = zero_frame_info;
7129
7130   sparc_output_deferred_case_vectors ();
7131 }
7132 \f
7133 /* Define the number of delay slots needed for the function epilogue.
7134
7135    On the sparc, we need a slot if either no stack has been allocated,
7136    or the only register saved is the return register.  */
7137
7138 int
7139 sparc_flat_epilogue_delay_slots ()
7140 {
7141   if (!current_frame_info.initialized)
7142     (void) sparc_flat_compute_frame_size (get_frame_size ());
7143
7144   if (current_frame_info.total_size == 0)
7145     return 1;
7146
7147   return 0;
7148 }
7149
7150 /* Return true if TRIAL is a valid insn for the epilogue delay slot.
7151    Any single length instruction which doesn't reference the stack or frame
7152    pointer is OK.  */
7153
7154 int
7155 sparc_flat_eligible_for_epilogue_delay (trial, slot)
7156      rtx trial;
7157      int slot ATTRIBUTE_UNUSED;
7158 {
7159   rtx pat = PATTERN (trial);
7160
7161   if (get_attr_length (trial) != 1)
7162     return 0;
7163
7164   if (! reg_mentioned_p (stack_pointer_rtx, pat)
7165       && ! reg_mentioned_p (frame_pointer_rtx, pat))
7166     return 1;
7167
7168   return 0;
7169 }
7170 \f
7171 /* Adjust the cost of a scheduling dependency.  Return the new cost of
7172    a dependency LINK or INSN on DEP_INSN.  COST is the current cost.  */
7173
7174 static int
7175 supersparc_adjust_cost (insn, link, dep_insn, cost)
7176      rtx insn;
7177      rtx link;
7178      rtx dep_insn;
7179      int cost;
7180 {
7181   enum attr_type insn_type;
7182
7183   if (! recog_memoized (insn))
7184     return 0;
7185
7186   insn_type = get_attr_type (insn);
7187
7188   if (REG_NOTE_KIND (link) == 0)
7189     {
7190       /* Data dependency; DEP_INSN writes a register that INSN reads some
7191          cycles later.  */
7192
7193       /* if a load, then the dependence must be on the memory address;
7194          add an extra "cycle".  Note that the cost could be two cycles
7195          if the reg was written late in an instruction group; we ca not tell
7196          here.  */
7197       if (insn_type == TYPE_LOAD || insn_type == TYPE_FPLOAD)
7198         return cost + 3;
7199
7200       /* Get the delay only if the address of the store is the dependence.  */
7201       if (insn_type == TYPE_STORE || insn_type == TYPE_FPSTORE)
7202         {
7203           rtx pat = PATTERN(insn);
7204           rtx dep_pat = PATTERN (dep_insn);
7205
7206           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
7207             return cost;  /* This should not happen!  */
7208
7209           /* The dependency between the two instructions was on the data that
7210              is being stored.  Assume that this implies that the address of the
7211              store is not dependent.  */
7212           if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
7213             return cost;
7214
7215           return cost + 3;  /* An approximation.  */
7216         }
7217
7218       /* A shift instruction cannot receive its data from an instruction
7219          in the same cycle; add a one cycle penalty.  */
7220       if (insn_type == TYPE_SHIFT)
7221         return cost + 3;   /* Split before cascade into shift.  */
7222     }
7223   else
7224     {
7225       /* Anti- or output- dependency; DEP_INSN reads/writes a register that
7226          INSN writes some cycles later.  */
7227
7228       /* These are only significant for the fpu unit; writing a fp reg before
7229          the fpu has finished with it stalls the processor.  */
7230
7231       /* Reusing an integer register causes no problems.  */
7232       if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
7233         return 0;
7234     }
7235         
7236   return cost;
7237 }
7238
7239 static int
7240 hypersparc_adjust_cost (insn, link, dep_insn, cost)
7241      rtx insn;
7242      rtx link;
7243      rtx dep_insn;
7244      int cost;
7245 {
7246   enum attr_type insn_type, dep_type;
7247   rtx pat = PATTERN(insn);
7248   rtx dep_pat = PATTERN (dep_insn);
7249
7250   if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
7251     return cost;
7252
7253   insn_type = get_attr_type (insn);
7254   dep_type = get_attr_type (dep_insn);
7255
7256   switch (REG_NOTE_KIND (link))
7257     {
7258     case 0:
7259       /* Data dependency; DEP_INSN writes a register that INSN reads some
7260          cycles later.  */
7261
7262       switch (insn_type)
7263         {
7264         case TYPE_STORE:
7265         case TYPE_FPSTORE:
7266           /* Get the delay iff the address of the store is the dependence.  */
7267           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
7268             return cost;
7269
7270           if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
7271             return cost;
7272           return cost + 3;
7273
7274         case TYPE_LOAD:
7275         case TYPE_SLOAD:
7276         case TYPE_FPLOAD:
7277           /* If a load, then the dependence must be on the memory address.  If
7278              the addresses aren't equal, then it might be a false dependency */
7279           if (dep_type == TYPE_STORE || dep_type == TYPE_FPSTORE)
7280             {
7281               if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET
7282                   || GET_CODE (SET_DEST (dep_pat)) != MEM        
7283                   || GET_CODE (SET_SRC (pat)) != MEM
7284                   || ! rtx_equal_p (XEXP (SET_DEST (dep_pat), 0),
7285                                     XEXP (SET_SRC (pat), 0)))
7286                 return cost + 2;
7287
7288               return cost + 8;        
7289             }
7290           break;
7291
7292         case TYPE_BRANCH:
7293           /* Compare to branch latency is 0.  There is no benefit from
7294              separating compare and branch.  */
7295           if (dep_type == TYPE_COMPARE)
7296             return 0;
7297           /* Floating point compare to branch latency is less than
7298              compare to conditional move.  */
7299           if (dep_type == TYPE_FPCMP)
7300             return cost - 1;
7301           break;
7302         default:
7303           break;
7304         }
7305         break;
7306
7307     case REG_DEP_ANTI:
7308       /* Anti-dependencies only penalize the fpu unit.  */
7309       if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
7310         return 0;
7311       break;
7312
7313     default:
7314       break;
7315     }    
7316
7317   return cost;
7318 }
7319
7320 static int
7321 ultrasparc_adjust_cost (insn, link, dep_insn, cost)
7322      rtx insn;
7323      rtx link;
7324      rtx dep_insn;
7325      int cost;
7326 {
7327   enum attr_type insn_type, dep_type;
7328   rtx pat = PATTERN(insn);
7329   rtx dep_pat = PATTERN (dep_insn);
7330
7331   if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
7332     return cost;
7333
7334   insn_type = get_attr_type (insn);
7335   dep_type = get_attr_type (dep_insn);
7336
7337   /* Nothing issues in parallel with integer multiplies, so
7338      mark as zero cost since the scheduler can not do anything
7339      about it.  */
7340   if (insn_type == TYPE_IMUL || insn_type == TYPE_IDIV)
7341     return 0;
7342
7343 #define SLOW_FP(dep_type) \
7344 (dep_type == TYPE_FPSQRTS || dep_type == TYPE_FPSQRTD || \
7345  dep_type == TYPE_FPDIVS || dep_type == TYPE_FPDIVD)
7346
7347   switch (REG_NOTE_KIND (link))
7348     {
7349     case 0:
7350       /* Data dependency; DEP_INSN writes a register that INSN reads some
7351          cycles later.  */
7352
7353       if (dep_type == TYPE_CMOVE)
7354         {
7355           /* Instructions that read the result of conditional moves cannot
7356              be in the same group or the following group.  */
7357           return cost + 1;
7358         }
7359
7360       switch (insn_type)
7361         {
7362           /* UltraSPARC can dual issue a store and an instruction setting
7363              the value stored, except for divide and square root.  */
7364         case TYPE_FPSTORE:
7365           if (! SLOW_FP (dep_type))
7366             return 0;
7367           return cost;
7368
7369         case TYPE_STORE:
7370           if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
7371             return cost;
7372
7373           if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
7374             /* The dependency between the two instructions is on the data
7375                that is being stored.  Assume that the address of the store
7376                is not also dependent.  */
7377             return 0;
7378           return cost;
7379
7380         case TYPE_LOAD:
7381         case TYPE_SLOAD:
7382         case TYPE_FPLOAD:
7383           /* A load does not return data until at least 11 cycles after
7384              a store to the same location.  3 cycles are accounted for
7385              in the load latency; add the other 8 here.  */
7386           if (dep_type == TYPE_STORE || dep_type == TYPE_FPSTORE)
7387             {
7388               /* If the addresses are not equal this may be a false
7389                  dependency because pointer aliasing could not be
7390                  determined.  Add only 2 cycles in that case.  2 is
7391                  an arbitrary compromise between 8, which would cause
7392                  the scheduler to generate worse code elsewhere to
7393                  compensate for a dependency which might not really
7394                  exist, and 0.  */
7395               if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET
7396                   || GET_CODE (SET_SRC (pat)) != MEM
7397                   || GET_CODE (SET_DEST (dep_pat)) != MEM
7398                   || ! rtx_equal_p (XEXP (SET_SRC (pat), 0),
7399                                     XEXP (SET_DEST (dep_pat), 0)))
7400                 return cost + 2;
7401
7402               return cost + 8;
7403             }
7404           return cost;
7405
7406         case TYPE_BRANCH:
7407           /* Compare to branch latency is 0.  There is no benefit from
7408              separating compare and branch.  */
7409           if (dep_type == TYPE_COMPARE)
7410             return 0;
7411           /* Floating point compare to branch latency is less than
7412              compare to conditional move.  */
7413           if (dep_type == TYPE_FPCMP)
7414             return cost - 1;
7415           return cost;
7416
7417         case TYPE_FPCMOVE:
7418           /* FMOVR class instructions can not issue in the same cycle
7419              or the cycle after an instruction which writes any
7420              integer register.  Model this as cost 2 for dependent
7421              instructions.  */
7422           if (dep_type == TYPE_IALU
7423               && cost < 2)
7424             return 2;
7425           /* Otherwise check as for integer conditional moves.  */
7426
7427         case TYPE_CMOVE:
7428           /* Conditional moves involving integer registers wait until
7429              3 cycles after loads return data.  The interlock applies
7430              to all loads, not just dependent loads, but that is hard
7431              to model.  */
7432           if (dep_type == TYPE_LOAD || dep_type == TYPE_SLOAD)
7433             return cost + 3;
7434           return cost;
7435
7436         default:
7437           break;
7438         }
7439       break;
7440
7441     case REG_DEP_ANTI:
7442       /* Divide and square root lock destination registers for full latency.  */
7443       if (! SLOW_FP (dep_type))
7444         return 0;
7445       break;
7446
7447     case REG_DEP_OUTPUT:
7448       /* IEU and FPU instruction that have the same destination
7449          register cannot be grouped together.  */
7450       return cost + 1;
7451
7452     default:
7453       break;
7454     }
7455
7456   /* Other costs not accounted for:
7457      - Single precision floating point loads lock the other half of
7458        the even/odd register pair.
7459      - Several hazards associated with ldd/std are ignored because these
7460        instructions are rarely generated for V9.
7461      - The floating point pipeline can not have both a single and double
7462        precision operation active at the same time.  Format conversions
7463        and graphics instructions are given honorary double precision status.
7464      - call and jmpl are always the first instruction in a group.  */
7465
7466   return cost;
7467
7468 #undef SLOW_FP
7469 }
7470
7471 static int
7472 sparc_adjust_cost(insn, link, dep, cost)
7473      rtx insn;
7474      rtx link;
7475      rtx dep;
7476      int cost;
7477 {
7478   switch (sparc_cpu)
7479     {
7480     case PROCESSOR_SUPERSPARC:
7481       cost = supersparc_adjust_cost (insn, link, dep, cost);
7482       break;
7483     case PROCESSOR_HYPERSPARC:
7484     case PROCESSOR_SPARCLITE86X:
7485       cost = hypersparc_adjust_cost (insn, link, dep, cost);
7486       break;
7487     case PROCESSOR_ULTRASPARC:
7488       cost = ultrasparc_adjust_cost (insn, link, dep, cost);
7489       break;
7490     default:
7491       break;
7492     }
7493   return cost;
7494 }
7495
7496 /* This describes the state of the UltraSPARC pipeline during
7497    instruction scheduling.  */
7498
7499 #define TMASK(__x)      ((unsigned)1 << ((int)(__x)))
7500 #define UMASK(__x)      ((unsigned)1 << ((int)(__x)))
7501
7502 enum ultra_code { NONE=0, /* no insn at all                             */
7503                   IEU0,   /* shifts and conditional moves               */
7504                   IEU1,   /* condition code setting insns, calls+jumps  */
7505                   IEUN,   /* all other single cycle ieu insns           */
7506                   LSU,    /* loads and stores                           */
7507                   CTI,    /* branches                                   */
7508                   FPM,    /* FPU pipeline 1, multiplies and divides     */
7509                   FPA,    /* FPU pipeline 2, all other operations       */
7510                   SINGLE, /* single issue instructions                  */
7511                   NUM_ULTRA_CODES };
7512
7513 static enum ultra_code ultra_code_from_mask PARAMS ((int));
7514 static void ultra_schedule_insn PARAMS ((rtx *, rtx *, int, enum ultra_code));
7515
7516 static const char *const ultra_code_names[NUM_ULTRA_CODES] = {
7517   "NONE", "IEU0", "IEU1", "IEUN", "LSU", "CTI",
7518   "FPM", "FPA", "SINGLE" };
7519
7520 struct ultrasparc_pipeline_state {
7521   /* The insns in this group.  */
7522   rtx group[4];
7523
7524   /* The code for each insn.  */
7525   enum ultra_code codes[4];
7526
7527   /* Which insns in this group have been committed by the
7528      scheduler.  This is how we determine how many more
7529      can issue this cycle.  */
7530   char commit[4];
7531
7532   /* How many insns in this group.  */
7533   char group_size;
7534
7535   /* Mask of free slots still in this group.  */
7536   char free_slot_mask;
7537
7538   /* The slotter uses the following to determine what other
7539      insn types can still make their way into this group.  */
7540   char contents [NUM_ULTRA_CODES];
7541   char num_ieu_insns;
7542 };
7543
7544 #define ULTRA_NUM_HIST  8
7545 static struct ultrasparc_pipeline_state ultra_pipe_hist[ULTRA_NUM_HIST];
7546 static int ultra_cur_hist;
7547 static int ultra_cycles_elapsed;
7548
7549 #define ultra_pipe      (ultra_pipe_hist[ultra_cur_hist])
7550
7551 /* Given TYPE_MASK compute the ultra_code it has.  */
7552 static enum ultra_code
7553 ultra_code_from_mask (type_mask)
7554      int type_mask;
7555 {
7556   if (type_mask & (TMASK (TYPE_SHIFT) | TMASK (TYPE_CMOVE)))
7557     return IEU0;
7558   else if (type_mask & (TMASK (TYPE_COMPARE) |
7559                         TMASK (TYPE_CALL) |
7560                         TMASK (TYPE_SIBCALL) |
7561                         TMASK (TYPE_UNCOND_BRANCH)))
7562     return IEU1;
7563   else if (type_mask & TMASK (TYPE_IALU))
7564     return IEUN;
7565   else if (type_mask & (TMASK (TYPE_LOAD) | TMASK (TYPE_SLOAD) |
7566                         TMASK (TYPE_STORE) | TMASK (TYPE_FPLOAD) |
7567                         TMASK (TYPE_FPSTORE)))
7568     return LSU;
7569   else if (type_mask & (TMASK (TYPE_FPMUL) | TMASK (TYPE_FPDIVS) |
7570                         TMASK (TYPE_FPDIVD) | TMASK (TYPE_FPSQRTS) |
7571                         TMASK (TYPE_FPSQRTD)))
7572     return FPM;
7573   else if (type_mask & (TMASK (TYPE_FPMOVE) | TMASK (TYPE_FPCMOVE) |
7574                         TMASK (TYPE_FP) | TMASK (TYPE_FPCMP)))
7575     return FPA;
7576   else if (type_mask & TMASK (TYPE_BRANCH))
7577     return CTI;
7578
7579   return SINGLE;
7580 }
7581
7582 /* Check INSN (a conditional move) and make sure that it's
7583    results are available at this cycle.  Return 1 if the
7584    results are in fact ready.  */
7585 static int
7586 ultra_cmove_results_ready_p (insn)
7587      rtx insn;
7588 {
7589   struct ultrasparc_pipeline_state *up;
7590   int entry, slot;
7591
7592   /* If this got dispatched in the previous
7593      group, the results are not ready.  */
7594   entry = (ultra_cur_hist - 1) & (ULTRA_NUM_HIST - 1);
7595   up = &ultra_pipe_hist[entry];
7596   slot = 4;
7597   while (--slot >= 0)
7598     if (up->group[slot] == insn)
7599       return 0;
7600
7601   return 1;
7602 }
7603
7604 /* Walk backwards in pipeline history looking for FPU
7605    operations which use a mode different than FPMODE and
7606    will create a stall if an insn using FPMODE were to be
7607    dispatched this cycle.  */
7608 static int
7609 ultra_fpmode_conflict_exists (fpmode)
7610      enum machine_mode fpmode;
7611 {
7612   int hist_ent;
7613   int hist_lim;
7614
7615   hist_ent = (ultra_cur_hist - 1) & (ULTRA_NUM_HIST - 1);
7616   if (ultra_cycles_elapsed < 4)
7617     hist_lim = ultra_cycles_elapsed;
7618   else
7619     hist_lim = 4;
7620   while (hist_lim > 0)
7621     {
7622       struct ultrasparc_pipeline_state *up = &ultra_pipe_hist[hist_ent];
7623       int slot = 4;
7624
7625       while (--slot >= 0)
7626         {
7627           rtx insn = up->group[slot];
7628           enum machine_mode this_mode;
7629           rtx pat;
7630
7631           if (! insn
7632               || GET_CODE (insn) != INSN
7633               || (pat = PATTERN (insn)) == 0
7634               || GET_CODE (pat) != SET)
7635             continue;
7636
7637           this_mode = GET_MODE (SET_DEST (pat));
7638           if ((this_mode != SFmode
7639                && this_mode != DFmode)
7640               || this_mode == fpmode)
7641             continue;
7642
7643           /* If it is not FMOV, FABS, FNEG, FDIV, or FSQRT then
7644              we will get a stall.  Loads and stores are independent
7645              of these rules.  */
7646           if (GET_CODE (SET_SRC (pat)) != ABS
7647               && GET_CODE (SET_SRC (pat)) != NEG
7648               && ((TMASK (get_attr_type (insn)) &
7649                    (TMASK (TYPE_FPDIVS) | TMASK (TYPE_FPDIVD) |
7650                     TMASK (TYPE_FPMOVE) | TMASK (TYPE_FPSQRTS) |
7651                     TMASK (TYPE_FPSQRTD) |
7652                     TMASK (TYPE_LOAD) | TMASK (TYPE_STORE))) == 0))
7653             return 1;
7654         }
7655       hist_lim--;
7656       hist_ent = (hist_ent - 1) & (ULTRA_NUM_HIST - 1);
7657     }
7658
7659   /* No conflicts, safe to dispatch.  */
7660   return 0;
7661 }
7662
7663 /* Find an instruction in LIST which has one of the
7664    type attributes enumerated in TYPE_MASK.  START
7665    says where to begin the search.
7666
7667    NOTE: This scheme depends upon the fact that we
7668          have less than 32 distinct type attributes.  */
7669
7670 static int ultra_types_avail;
7671
7672 static rtx *
7673 ultra_find_type (type_mask, list, start)
7674      int type_mask;
7675      rtx *list;
7676      int start;
7677 {
7678   int i;
7679
7680   /* Short circuit if no such insn exists in the ready
7681      at the moment.  */
7682   if ((type_mask & ultra_types_avail) == 0)
7683     return 0;
7684
7685   for (i = start; i >= 0; i--)
7686     {
7687       rtx insn = list[i];
7688
7689       if (recog_memoized (insn) >= 0
7690           && (TMASK(get_attr_type (insn)) & type_mask))
7691         {
7692           enum machine_mode fpmode = SFmode;
7693           rtx pat = 0;
7694           int slot;
7695           int check_depend = 0;
7696           int check_fpmode_conflict = 0;
7697
7698           if (GET_CODE (insn) == INSN
7699               && (pat = PATTERN(insn)) != 0
7700               && GET_CODE (pat) == SET
7701               && !(type_mask & (TMASK (TYPE_STORE) |
7702                                 TMASK (TYPE_FPSTORE))))
7703             {
7704               check_depend = 1;
7705               if (GET_MODE (SET_DEST (pat)) == SFmode
7706                   || GET_MODE (SET_DEST (pat)) == DFmode)
7707                 {
7708                   fpmode = GET_MODE (SET_DEST (pat));
7709                   check_fpmode_conflict = 1;
7710                 }
7711             }
7712
7713           slot = 4;
7714           while(--slot >= 0)
7715             {
7716               rtx slot_insn = ultra_pipe.group[slot];
7717               rtx slot_pat;
7718
7719               /* Already issued, bad dependency, or FPU
7720                  mode conflict.  */
7721               if (slot_insn != 0
7722                   && (slot_pat = PATTERN (slot_insn)) != 0
7723                   && ((insn == slot_insn)
7724                       || (check_depend == 1
7725                           && GET_CODE (slot_insn) == INSN
7726                           && GET_CODE (slot_pat) == SET
7727                           && ((GET_CODE (SET_DEST (slot_pat)) == REG
7728                                && GET_CODE (SET_SRC (pat)) == REG
7729                                && REGNO (SET_DEST (slot_pat)) ==
7730                                     REGNO (SET_SRC (pat)))
7731                               || (GET_CODE (SET_DEST (slot_pat)) == SUBREG
7732                                   && GET_CODE (SET_SRC (pat)) == SUBREG
7733                                   && REGNO (SUBREG_REG (SET_DEST (slot_pat))) ==
7734                                        REGNO (SUBREG_REG (SET_SRC (pat)))
7735                                   && SUBREG_BYTE (SET_DEST (slot_pat)) ==
7736                                        SUBREG_BYTE (SET_SRC (pat)))))
7737                       || (check_fpmode_conflict == 1
7738                           && GET_CODE (slot_insn) == INSN
7739                           && GET_CODE (slot_pat) == SET
7740                           && (GET_MODE (SET_DEST (slot_pat)) == SFmode
7741                               || GET_MODE (SET_DEST (slot_pat)) == DFmode)
7742                           && GET_MODE (SET_DEST (slot_pat)) != fpmode)))
7743                 goto next;
7744             }
7745
7746           /* Check for peculiar result availability and dispatch
7747              interference situations.  */
7748           if (pat != 0
7749               && ultra_cycles_elapsed > 0)
7750             {
7751               rtx link;
7752
7753               for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
7754                 {
7755                   rtx link_insn = XEXP (link, 0);
7756                   if (GET_CODE (link_insn) == INSN
7757                       && recog_memoized (link_insn) >= 0
7758                       && (TMASK (get_attr_type (link_insn)) &
7759                           (TMASK (TYPE_CMOVE) | TMASK (TYPE_FPCMOVE)))
7760                       && ! ultra_cmove_results_ready_p (link_insn))
7761                     goto next;
7762                 }
7763
7764               if (check_fpmode_conflict
7765                   && ultra_fpmode_conflict_exists (fpmode))
7766                 goto next;
7767             }
7768
7769           return &list[i];
7770         }
7771     next:
7772       ;
7773     }
7774   return 0;
7775 }
7776
7777 static void
7778 ultra_build_types_avail (ready, n_ready)
7779   rtx *ready;
7780   int n_ready;
7781 {
7782   int i = n_ready - 1;
7783
7784   ultra_types_avail = 0;
7785   while(i >= 0)
7786     {
7787       rtx insn = ready[i];
7788
7789       if (recog_memoized (insn) >= 0)
7790         ultra_types_avail |= TMASK (get_attr_type (insn));
7791
7792       i -= 1;
7793     }
7794 }
7795
7796 /* Place insn pointed to my IP into the pipeline.
7797    Make element THIS of READY be that insn if it
7798    is not already.  TYPE indicates the pipeline class
7799    this insn falls into.  */
7800 static void
7801 ultra_schedule_insn (ip, ready, this, type)
7802      rtx *ip;
7803      rtx *ready;
7804      int this;
7805      enum ultra_code type;
7806 {
7807   int pipe_slot;
7808   char mask = ultra_pipe.free_slot_mask;
7809   rtx temp;
7810
7811   /* Obtain free slot.  */
7812   for (pipe_slot = 0; pipe_slot < 4; pipe_slot++)
7813     if ((mask & (1 << pipe_slot)) != 0)
7814       break;
7815   if (pipe_slot == 4)
7816     abort ();
7817
7818   /* In it goes, and it hasn't been committed yet.  */
7819   ultra_pipe.group[pipe_slot] = *ip;
7820   ultra_pipe.codes[pipe_slot] = type;
7821   ultra_pipe.contents[type] = 1;
7822   if (UMASK (type) &
7823       (UMASK (IEUN) | UMASK (IEU0) | UMASK (IEU1)))
7824     ultra_pipe.num_ieu_insns += 1;
7825
7826   ultra_pipe.free_slot_mask = (mask & ~(1 << pipe_slot));
7827   ultra_pipe.group_size += 1;
7828   ultra_pipe.commit[pipe_slot] = 0;
7829
7830   /* Update ready list.  */
7831   temp = *ip;
7832   while (ip != &ready[this])
7833     {
7834       ip[0] = ip[1];
7835       ++ip;
7836     }
7837   *ip = temp;
7838 }
7839
7840 /* Advance to the next pipeline group.  */
7841 static void
7842 ultra_flush_pipeline ()
7843 {
7844   ultra_cur_hist = (ultra_cur_hist + 1) & (ULTRA_NUM_HIST - 1);
7845   ultra_cycles_elapsed += 1;
7846   memset ((char *) &ultra_pipe, 0, sizeof ultra_pipe);
7847   ultra_pipe.free_slot_mask = 0xf;
7848 }
7849
7850 /* Init our data structures for this current block.  */
7851 static void
7852 ultrasparc_sched_init ()
7853 {
7854   memset ((char *) ultra_pipe_hist, 0, sizeof ultra_pipe_hist);
7855   ultra_cur_hist = 0;
7856   ultra_cycles_elapsed = 0;
7857   ultra_pipe.free_slot_mask = 0xf;
7858 }
7859
7860 static void
7861 sparc_sched_init (dump, sched_verbose, max_ready)
7862      FILE *dump ATTRIBUTE_UNUSED;
7863      int sched_verbose ATTRIBUTE_UNUSED;
7864      int max_ready ATTRIBUTE_UNUSED;
7865 {
7866   if (sparc_cpu == PROCESSOR_ULTRASPARC)
7867     ultrasparc_sched_init ();
7868 }
7869   
7870 /* INSN has been scheduled, update pipeline commit state
7871    and return how many instructions are still to be
7872    scheduled in this group.  */
7873 static int
7874 ultrasparc_variable_issue (insn)
7875      rtx insn;
7876 {
7877   struct ultrasparc_pipeline_state *up = &ultra_pipe;
7878   int i, left_to_fire;
7879
7880   left_to_fire = 0;
7881   for (i = 0; i < 4; i++)
7882     {
7883       if (up->group[i] == 0)
7884         continue;
7885
7886       if (up->group[i] == insn)
7887         {
7888           up->commit[i] = 1;
7889         }
7890       else if (! up->commit[i])
7891         left_to_fire++;
7892     }
7893
7894   return left_to_fire;
7895 }
7896
7897 static int
7898 sparc_variable_issue (dump, sched_verbose, insn, cim)
7899      FILE *dump ATTRIBUTE_UNUSED;
7900      int sched_verbose ATTRIBUTE_UNUSED;
7901      rtx insn;
7902      int cim;
7903 {
7904   if (sparc_cpu == PROCESSOR_ULTRASPARC)
7905     return ultrasparc_variable_issue (insn);
7906   else
7907     return cim - 1;
7908 }
7909
7910 /* In actual_hazard_this_instance, we may have yanked some
7911    instructions from the ready list due to conflict cost
7912    adjustments.  If so, and such an insn was in our pipeline
7913    group, remove it and update state.  */
7914 static void
7915 ultra_rescan_pipeline_state (ready, n_ready)
7916      rtx *ready;
7917      int n_ready;
7918 {
7919   struct ultrasparc_pipeline_state *up = &ultra_pipe;
7920   int i;
7921
7922   for (i = 0; i < 4; i++)
7923     {
7924       rtx insn = up->group[i];
7925       int j;
7926
7927       if (! insn)
7928         continue;
7929
7930       /* If it has been committed, then it was removed from
7931          the ready list because it was actually scheduled,
7932          and that is not the case we are searching for here.  */
7933       if (up->commit[i] != 0)
7934         continue;
7935
7936       for (j = n_ready - 1; j >= 0; j--)
7937         if (ready[j] == insn)
7938           break;
7939
7940       /* If we didn't find it, toss it.  */
7941       if (j < 0)
7942         {
7943           enum ultra_code ucode = up->codes[i];
7944
7945           up->group[i] = 0;
7946           up->codes[i] = NONE;
7947           up->contents[ucode] = 0;
7948           if (UMASK (ucode) &
7949               (UMASK (IEUN) | UMASK (IEU0) | UMASK (IEU1)))
7950             up->num_ieu_insns -= 1;
7951
7952           up->free_slot_mask |= (1 << i);
7953           up->group_size -= 1;
7954           up->commit[i] = 0;
7955         }
7956     }
7957 }
7958
7959 static void
7960 ultrasparc_sched_reorder (dump, sched_verbose, ready, n_ready)
7961      FILE *dump;
7962      int sched_verbose;
7963      rtx *ready;
7964      int n_ready;
7965 {
7966   struct ultrasparc_pipeline_state *up = &ultra_pipe;
7967   int i, this_insn;
7968
7969   if (sched_verbose)
7970     {
7971       int n;
7972
7973       fprintf (dump, "\n;;\tUltraSPARC Looking at [");
7974       for (n = n_ready - 1; n >= 0; n--)
7975         {
7976           rtx insn = ready[n];
7977           enum ultra_code ucode;
7978
7979           if (recog_memoized (insn) < 0)
7980             continue;
7981           ucode = ultra_code_from_mask (TMASK (get_attr_type (insn)));
7982           if (n != 0)
7983             fprintf (dump, "%s(%d) ",
7984                      ultra_code_names[ucode],
7985                      INSN_UID (insn));
7986           else
7987             fprintf (dump, "%s(%d)",
7988                      ultra_code_names[ucode],
7989                      INSN_UID (insn));
7990         }
7991       fprintf (dump, "]\n");
7992     }
7993
7994   this_insn = n_ready - 1;
7995
7996   /* Skip over junk we don't understand.  */
7997   while ((this_insn >= 0)
7998          && recog_memoized (ready[this_insn]) < 0)
7999     this_insn--;
8000
8001   ultra_build_types_avail (ready, this_insn + 1);
8002
8003   while (this_insn >= 0) {
8004     int old_group_size = up->group_size;
8005
8006     if (up->group_size != 0)
8007       {
8008         int num_committed;
8009
8010         num_committed = (up->commit[0] + up->commit[1] +
8011                          up->commit[2] + up->commit[3]);
8012         /* If nothing has been commited from our group, or all of
8013            them have.  Clear out the (current cycle's) pipeline
8014            state and start afresh.  */
8015         if (num_committed == 0
8016             || num_committed == up->group_size)
8017           {
8018             ultra_flush_pipeline ();
8019             up = &ultra_pipe;
8020             old_group_size = 0;
8021           }
8022         else
8023           {
8024             /* OK, some ready list insns got requeued and thus removed
8025                from the ready list.  Account for this fact.  */
8026             ultra_rescan_pipeline_state (ready, n_ready);
8027
8028             /* Something "changed", make this look like a newly
8029                formed group so the code at the end of the loop
8030                knows that progress was in fact made.  */
8031             if (up->group_size != old_group_size)
8032               old_group_size = 0;
8033           }
8034       }
8035
8036     if (up->group_size == 0)
8037       {
8038         /* If the pipeline is (still) empty and we have any single
8039            group insns, get them out now as this is a good time.  */
8040         rtx *ip = ultra_find_type ((TMASK (TYPE_RETURN) | TMASK (TYPE_IDIV) |
8041                                     TMASK (TYPE_IMUL) | TMASK (TYPE_CMOVE) |
8042                                     TMASK (TYPE_MULTI) | TMASK (TYPE_MISC)),
8043                                    ready, this_insn);
8044         if (ip)
8045           {
8046             ultra_schedule_insn (ip, ready, this_insn, SINGLE);
8047             break;
8048           }
8049
8050         /* If we are not in the process of emptying out the pipe, try to
8051            obtain an instruction which must be the first in it's group.  */
8052         ip = ultra_find_type ((TMASK (TYPE_CALL) |
8053                                TMASK (TYPE_SIBCALL) |
8054                                TMASK (TYPE_CALL_NO_DELAY_SLOT) |
8055                                TMASK (TYPE_UNCOND_BRANCH)),
8056                               ready, this_insn);
8057         if (ip)
8058           {
8059             ultra_schedule_insn (ip, ready, this_insn, IEU1);
8060             this_insn--;
8061           }
8062         else if ((ip = ultra_find_type ((TMASK (TYPE_FPDIVS) |
8063                                          TMASK (TYPE_FPDIVD) |
8064                                          TMASK (TYPE_FPSQRTS) |
8065                                          TMASK (TYPE_FPSQRTD)),
8066                                         ready, this_insn)) != 0)
8067           {
8068             ultra_schedule_insn (ip, ready, this_insn, FPM);
8069             this_insn--;
8070           }
8071       }
8072
8073     /* Try to fill the integer pipeline.  First, look for an IEU0 specific
8074        operation.  We can't do more IEU operations if the first 3 slots are
8075        all full or we have dispatched two IEU insns already.  */
8076     if ((up->free_slot_mask & 0x7) != 0
8077         && up->num_ieu_insns < 2
8078         && up->contents[IEU0] == 0
8079         && up->contents[IEUN] == 0)
8080       {
8081         rtx *ip = ultra_find_type (TMASK(TYPE_SHIFT), ready, this_insn);
8082         if (ip)
8083           {
8084             ultra_schedule_insn (ip, ready, this_insn, IEU0);
8085             this_insn--;
8086           }
8087       }
8088
8089     /* If we can, try to find an IEU1 specific or an unnamed
8090        IEU instruction.  */
8091     if ((up->free_slot_mask & 0x7) != 0
8092         && up->num_ieu_insns < 2)
8093       {
8094         rtx *ip = ultra_find_type ((TMASK (TYPE_IALU) |
8095                                     (up->contents[IEU1] == 0 ? TMASK (TYPE_COMPARE) : 0)),
8096                                    ready, this_insn);
8097         if (ip)
8098           {
8099             rtx insn = *ip;
8100
8101             ultra_schedule_insn (ip, ready, this_insn,
8102                                  (!up->contents[IEU1]
8103                                   && get_attr_type (insn) == TYPE_COMPARE)
8104                                  ? IEU1 : IEUN);
8105             this_insn--;
8106           }
8107       }
8108
8109     /* If only one IEU insn has been found, try to find another unnamed
8110        IEU operation or an IEU1 specific one.  */
8111     if ((up->free_slot_mask & 0x7) != 0
8112         && up->num_ieu_insns < 2)
8113       {
8114         rtx *ip;
8115         int tmask = TMASK (TYPE_IALU);
8116
8117         if (!up->contents[IEU1])
8118           tmask |= TMASK (TYPE_COMPARE);
8119         ip = ultra_find_type (tmask, ready, this_insn);
8120         if (ip)
8121           {
8122             rtx insn = *ip;
8123
8124             ultra_schedule_insn (ip, ready, this_insn,
8125                                  (!up->contents[IEU1]
8126                                   && get_attr_type (insn) == TYPE_COMPARE)
8127                                  ? IEU1 : IEUN);
8128             this_insn--;
8129           }
8130       }
8131
8132     /* Try for a load or store, but such an insn can only be issued
8133        if it is within' one of the first 3 slots.  */
8134     if ((up->free_slot_mask & 0x7) != 0
8135         && up->contents[LSU] == 0)
8136       {
8137         rtx *ip = ultra_find_type ((TMASK (TYPE_LOAD) | TMASK (TYPE_SLOAD) |
8138                                    TMASK (TYPE_STORE) | TMASK (TYPE_FPLOAD) |
8139                                    TMASK (TYPE_FPSTORE)), ready, this_insn);
8140         if (ip)
8141           {
8142             ultra_schedule_insn (ip, ready, this_insn, LSU);
8143             this_insn--;
8144           }
8145       }
8146
8147     /* Now find FPU operations, first FPM class.  But not divisions or
8148        square-roots because those will break the group up.  Unlike all
8149        the previous types, these can go in any slot.  */
8150     if (up->free_slot_mask != 0
8151         && up->contents[FPM] == 0)
8152       {
8153         rtx *ip = ultra_find_type (TMASK (TYPE_FPMUL), ready, this_insn);
8154         if (ip)
8155           {
8156             ultra_schedule_insn (ip, ready, this_insn, FPM);
8157             this_insn--;
8158           }
8159       }
8160     
8161     /* Continue on with FPA class if we have not filled the group already.  */
8162     if (up->free_slot_mask != 0
8163         && up->contents[FPA] == 0)
8164       {
8165         rtx *ip = ultra_find_type ((TMASK (TYPE_FPMOVE) | TMASK (TYPE_FPCMOVE) |
8166                                     TMASK (TYPE_FP) | TMASK (TYPE_FPCMP)),
8167                                    ready, this_insn);
8168         if (ip)
8169           {
8170             ultra_schedule_insn (ip, ready, this_insn, FPA);
8171             this_insn--;
8172           }
8173       }
8174
8175     /* Finally, maybe stick a branch in here.  */
8176     if (up->free_slot_mask != 0
8177         && up->contents[CTI] == 0)
8178       {
8179         rtx *ip = ultra_find_type (TMASK (TYPE_BRANCH), ready, this_insn);
8180
8181         /* Try to slip in a branch only if it is one of the
8182            next 2 in the ready list.  */
8183         if (ip && ((&ready[this_insn] - ip) < 2))
8184           {
8185             ultra_schedule_insn (ip, ready, this_insn, CTI);
8186             this_insn--;
8187           }
8188       }
8189
8190     up->group_size = 0;
8191     for (i = 0; i < 4; i++)
8192       if ((up->free_slot_mask & (1 << i)) == 0)
8193         up->group_size++;
8194
8195     /* See if we made any progress...  */
8196     if (old_group_size != up->group_size)
8197       break;
8198
8199     /* Clean out the (current cycle's) pipeline state
8200        and try once more.  If we placed no instructions
8201        into the pipeline at all, it means a real hard
8202        conflict exists with some earlier issued instruction
8203        so we must advance to the next cycle to clear it up.  */
8204     if (up->group_size == 0)
8205       {
8206         ultra_flush_pipeline ();
8207         up = &ultra_pipe;
8208       }
8209     else
8210       {
8211         memset ((char *) &ultra_pipe, 0, sizeof ultra_pipe);
8212         ultra_pipe.free_slot_mask = 0xf;
8213       }
8214   }
8215
8216   if (sched_verbose)
8217     {
8218       int n, gsize;
8219
8220       fprintf (dump, ";;\tUltraSPARC Launched   [");
8221       gsize = up->group_size;
8222       for (n = 0; n < 4; n++)
8223         {
8224           rtx insn = up->group[n];
8225
8226           if (! insn)
8227             continue;
8228
8229           gsize -= 1;
8230           if (gsize != 0)
8231             fprintf (dump, "%s(%d) ",
8232                      ultra_code_names[up->codes[n]],
8233                      INSN_UID (insn));
8234           else
8235             fprintf (dump, "%s(%d)",
8236                      ultra_code_names[up->codes[n]],
8237                      INSN_UID (insn));
8238         }
8239       fprintf (dump, "]\n");
8240     }
8241 }
8242
8243 static int
8244 sparc_sched_reorder (dump, sched_verbose, ready, n_readyp, clock)
8245      FILE *dump;
8246      int sched_verbose;
8247      rtx *ready;
8248      int *n_readyp;
8249      int clock ATTRIBUTE_UNUSED;
8250 {
8251   if (sparc_cpu == PROCESSOR_ULTRASPARC)
8252     ultrasparc_sched_reorder (dump, sched_verbose, ready, *n_readyp);
8253   return sparc_issue_rate ();
8254 }
8255
8256 static int                                                           
8257 sparc_issue_rate ()
8258 {
8259   switch (sparc_cpu)
8260     {
8261     default:                                 
8262       return 1;                                                    
8263     case PROCESSOR_V9:                                                
8264       /* Assume V9 processors are capable of at least dual-issue.  */
8265       return 2;
8266     case PROCESSOR_SUPERSPARC:                                        
8267       return 3;                                                      
8268     case PROCESSOR_HYPERSPARC:
8269     case PROCESSOR_SPARCLITE86X:
8270       return 2;
8271     case PROCESSOR_ULTRASPARC:                                            
8272       return 4;                                                    
8273     }
8274 }
8275
8276 static int
8277 set_extends (insn)
8278      rtx insn;
8279 {
8280   register rtx pat = PATTERN (insn);
8281
8282   switch (GET_CODE (SET_SRC (pat)))
8283     {
8284       /* Load and some shift instructions zero extend.  */
8285     case MEM:
8286     case ZERO_EXTEND:
8287       /* sethi clears the high bits */
8288     case HIGH:
8289       /* LO_SUM is used with sethi.  sethi cleared the high
8290          bits and the values used with lo_sum are positive */
8291     case LO_SUM:
8292       /* Store flag stores 0 or 1 */
8293     case LT: case LTU:
8294     case GT: case GTU:
8295     case LE: case LEU:
8296     case GE: case GEU:
8297     case EQ:
8298     case NE:
8299       return 1;
8300     case AND:
8301       {
8302         rtx op0 = XEXP (SET_SRC (pat), 0);
8303         rtx op1 = XEXP (SET_SRC (pat), 1);
8304         if (GET_CODE (op1) == CONST_INT)
8305           return INTVAL (op1) >= 0;
8306         if (GET_CODE (op0) != REG)
8307           return 0;
8308         if (sparc_check_64 (op0, insn) == 1)
8309           return 1;
8310         return (GET_CODE (op1) == REG && sparc_check_64 (op1, insn) == 1);
8311       }
8312     case IOR:
8313     case XOR:
8314       {
8315         rtx op0 = XEXP (SET_SRC (pat), 0);
8316         rtx op1 = XEXP (SET_SRC (pat), 1);
8317         if (GET_CODE (op0) != REG || sparc_check_64 (op0, insn) <= 0)
8318           return 0;
8319         if (GET_CODE (op1) == CONST_INT)
8320           return INTVAL (op1) >= 0;
8321         return (GET_CODE (op1) == REG && sparc_check_64 (op1, insn) == 1);
8322       }
8323     case ASHIFT:
8324     case LSHIFTRT:
8325       return GET_MODE (SET_SRC (pat)) == SImode;
8326       /* Positive integers leave the high bits zero.  */
8327     case CONST_DOUBLE:
8328       return ! (CONST_DOUBLE_LOW (SET_SRC (pat)) & 0x80000000);
8329     case CONST_INT:
8330       return ! (INTVAL (SET_SRC (pat)) & 0x80000000);
8331     case ASHIFTRT:
8332     case SIGN_EXTEND:
8333       return - (GET_MODE (SET_SRC (pat)) == SImode);
8334     case REG:
8335       return sparc_check_64 (SET_SRC (pat), insn);
8336     default:
8337       return 0;
8338     }
8339 }
8340
8341 /* We _ought_ to have only one kind per function, but...  */
8342 static rtx sparc_addr_diff_list;
8343 static rtx sparc_addr_list;
8344
8345 void
8346 sparc_defer_case_vector (lab, vec, diff)
8347      rtx lab, vec;
8348      int diff;
8349 {
8350   vec = gen_rtx_EXPR_LIST (VOIDmode, lab, vec);
8351   if (diff)
8352     sparc_addr_diff_list
8353       = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_diff_list);
8354   else
8355     sparc_addr_list = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_list);
8356 }
8357
8358 static void 
8359 sparc_output_addr_vec (vec)
8360      rtx vec;
8361 {
8362   rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
8363   int idx, vlen = XVECLEN (body, 0);
8364
8365 #ifdef ASM_OUTPUT_ADDR_VEC_START  
8366   ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
8367 #endif
8368
8369 #ifdef ASM_OUTPUT_CASE_LABEL
8370   ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
8371                          NEXT_INSN (lab));
8372 #else
8373   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
8374 #endif
8375
8376   for (idx = 0; idx < vlen; idx++)
8377     {
8378       ASM_OUTPUT_ADDR_VEC_ELT
8379         (asm_out_file, CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 0, idx), 0)));
8380     }
8381     
8382 #ifdef ASM_OUTPUT_ADDR_VEC_END
8383   ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
8384 #endif
8385 }
8386
8387 static void 
8388 sparc_output_addr_diff_vec (vec)
8389      rtx vec;
8390 {
8391   rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
8392   rtx base = XEXP (XEXP (body, 0), 0);
8393   int idx, vlen = XVECLEN (body, 1);
8394
8395 #ifdef ASM_OUTPUT_ADDR_VEC_START  
8396   ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
8397 #endif
8398
8399 #ifdef ASM_OUTPUT_CASE_LABEL
8400   ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
8401                          NEXT_INSN (lab));
8402 #else
8403   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
8404 #endif
8405
8406   for (idx = 0; idx < vlen; idx++)
8407     {
8408       ASM_OUTPUT_ADDR_DIFF_ELT
8409         (asm_out_file,
8410          body,
8411          CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 1, idx), 0)),
8412          CODE_LABEL_NUMBER (base));
8413     }
8414     
8415 #ifdef ASM_OUTPUT_ADDR_VEC_END
8416   ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
8417 #endif
8418 }
8419
8420 static void
8421 sparc_output_deferred_case_vectors ()
8422 {
8423   rtx t;
8424   int align;
8425
8426   if (sparc_addr_list == NULL_RTX
8427       && sparc_addr_diff_list == NULL_RTX)
8428     return;
8429
8430   /* Align to cache line in the function's code section.  */
8431   function_section (current_function_decl);
8432
8433   align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
8434   if (align > 0)
8435     ASM_OUTPUT_ALIGN (asm_out_file, align);
8436   
8437   for (t = sparc_addr_list; t ; t = XEXP (t, 1))
8438     sparc_output_addr_vec (XEXP (t, 0));
8439   for (t = sparc_addr_diff_list; t ; t = XEXP (t, 1))
8440     sparc_output_addr_diff_vec (XEXP (t, 0));
8441
8442   sparc_addr_list = sparc_addr_diff_list = NULL_RTX;
8443 }
8444
8445 /* Return 0 if the high 32 bits of X (the low word of X, if DImode) are
8446    unknown.  Return 1 if the high bits are zero, -1 if the register is
8447    sign extended.  */
8448 int
8449 sparc_check_64 (x, insn)
8450      rtx x, insn;
8451 {
8452   /* If a register is set only once it is safe to ignore insns this
8453      code does not know how to handle.  The loop will either recognize
8454      the single set and return the correct value or fail to recognize
8455      it and return 0.  */
8456   int set_once = 0;
8457   rtx y = x;
8458
8459   if (GET_CODE (x) != REG)
8460     abort ();
8461
8462   if (GET_MODE (x) == DImode)
8463     y = gen_rtx_REG (SImode, REGNO (x) + WORDS_BIG_ENDIAN);
8464
8465   if (flag_expensive_optimizations
8466       && REG_N_SETS (REGNO (y)) == 1)
8467     set_once = 1;
8468
8469   if (insn == 0)
8470     {
8471       if (set_once)
8472         insn = get_last_insn_anywhere ();
8473       else
8474         return 0;
8475     }
8476
8477   while ((insn = PREV_INSN (insn)))
8478     {
8479       switch (GET_CODE (insn))
8480         {
8481         case JUMP_INSN:
8482         case NOTE:
8483           break;
8484         case CODE_LABEL:
8485         case CALL_INSN:
8486         default:
8487           if (! set_once)
8488             return 0;
8489           break;
8490         case INSN:
8491           {
8492             rtx pat = PATTERN (insn);
8493             if (GET_CODE (pat) != SET)
8494               return 0;
8495             if (rtx_equal_p (x, SET_DEST (pat)))
8496               return set_extends (insn);
8497             if (y && rtx_equal_p (y, SET_DEST (pat)))
8498               return set_extends (insn);
8499             if (reg_overlap_mentioned_p (SET_DEST (pat), y))
8500               return 0;
8501           }
8502         }
8503     }
8504   return 0;
8505 }
8506
8507 char *
8508 sparc_v8plus_shift (operands, insn, opcode)
8509      rtx *operands;
8510      rtx insn;
8511      const char *opcode;
8512 {
8513   static char asm_code[60];
8514
8515   if (GET_CODE (operands[3]) == SCRATCH)
8516     operands[3] = operands[0];
8517   if (GET_CODE (operands[1]) == CONST_INT)
8518     {
8519       output_asm_insn ("mov\t%1, %3", operands);
8520     }
8521   else
8522     {
8523       output_asm_insn ("sllx\t%H1, 32, %3", operands);
8524       if (sparc_check_64 (operands[1], insn) <= 0)
8525         output_asm_insn ("srl\t%L1, 0, %L1", operands);
8526       output_asm_insn ("or\t%L1, %3, %3", operands);
8527     }
8528
8529   strcpy(asm_code, opcode);
8530   if (which_alternative != 2)
8531     return strcat (asm_code, "\t%0, %2, %L0\n\tsrlx\t%L0, 32, %H0");
8532   else
8533     return strcat (asm_code, "\t%3, %2, %3\n\tsrlx\t%3, 32, %H0\n\tmov\t%3, %L0");
8534 }
8535 \f
8536 /* Output rtl to increment the profiler label LABELNO
8537    for profiling a function entry.  */
8538
8539 void
8540 sparc_profile_hook (labelno)
8541      int labelno;
8542 {
8543   char buf[32];
8544   rtx lab, fun;
8545
8546   ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
8547   lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
8548   fun = gen_rtx_SYMBOL_REF (Pmode, MCOUNT_FUNCTION);
8549
8550   emit_library_call (fun, 0, VOIDmode, 1, lab, Pmode);
8551 }
8552 \f
8553 /* Mark ARG, which is really a struct ultrasparc_pipline_state *, for
8554    GC.  */
8555
8556 static void
8557 mark_ultrasparc_pipeline_state (arg)
8558      void *arg;
8559 {
8560   struct ultrasparc_pipeline_state *ups;
8561   size_t i;
8562
8563   ups = (struct ultrasparc_pipeline_state *) arg;
8564   for (i = 0; i < ARRAY_SIZE (ups->group); ++i)
8565     ggc_mark_rtx (ups->group[i]);
8566 }
8567
8568 /* Called to register all of our global variables with the garbage
8569    collector.  */
8570
8571 static void
8572 sparc_add_gc_roots ()
8573 {
8574   ggc_add_rtx_root (&sparc_compare_op0, 1);
8575   ggc_add_rtx_root (&sparc_compare_op1, 1);
8576   ggc_add_rtx_root (&global_offset_table, 1);
8577   ggc_add_rtx_root (&get_pc_symbol, 1);
8578   ggc_add_rtx_root (&sparc_addr_diff_list, 1);
8579   ggc_add_rtx_root (&sparc_addr_list, 1);
8580   ggc_add_root (ultra_pipe_hist, ARRAY_SIZE (ultra_pipe_hist),
8581                 sizeof (ultra_pipe_hist[0]), &mark_ultrasparc_pipeline_state);
8582 }
8583
8584 #ifdef OBJECT_FORMAT_ELF
8585 static void
8586 sparc_elf_asm_named_section (name, flags)
8587      const char *name;
8588      unsigned int flags;
8589 {
8590   if (flags & SECTION_MERGE)
8591     {
8592       /* entsize cannot be expressed in this section attributes
8593          encoding style.  */
8594       default_elf_asm_named_section (name, flags);
8595       return;
8596     }
8597
8598   fprintf (asm_out_file, "\t.section\t\"%s\"", name);
8599
8600   if (!(flags & SECTION_DEBUG))
8601     fputs (",#alloc", asm_out_file);
8602   if (flags & SECTION_WRITE)
8603     fputs (",#write", asm_out_file);
8604   if (flags & SECTION_CODE)
8605     fputs (",#execinstr", asm_out_file);
8606
8607   /* ??? Handle SECTION_BSS.  */
8608
8609   fputc ('\n', asm_out_file);
8610 }
8611 #endif /* OBJECT_FORMAT_ELF */
8612
8613 int
8614 sparc_extra_constraint_check (op, c, strict)
8615      rtx op;
8616      char c;
8617      int strict;
8618 {
8619   int reload_ok_mem;
8620
8621   if (TARGET_ARCH64
8622       && (c == 'T' || c == 'U'))
8623     return 0;
8624
8625   switch (c)
8626     {
8627     case 'Q':
8628       return fp_sethi_p (op);
8629
8630     case 'R':
8631       return fp_mov_p (op);
8632
8633     case 'S':
8634       return fp_high_losum_p (op);
8635
8636     case 'U':
8637       if (! strict
8638           || (GET_CODE (op) == REG
8639               && (REGNO (op) < FIRST_PSEUDO_REGISTER
8640                   || reg_renumber[REGNO (op)] >= 0)))
8641         return register_ok_for_ldd (op);
8642
8643       return 0;
8644
8645     case 'W':
8646     case 'T':
8647       break;
8648
8649     default:
8650       return 0;
8651     }
8652
8653   /* Our memory extra constraints have to emulate the
8654      behavior of 'm' and 'o' in order for reload to work
8655      correctly.  */
8656   if (GET_CODE (op) == MEM)
8657     {
8658       reload_ok_mem = 0;
8659       if ((TARGET_ARCH64 || mem_min_alignment (op, 8))
8660           && (! strict
8661               || strict_memory_address_p (Pmode, XEXP (op, 0))))
8662         reload_ok_mem = 1;
8663     }
8664   else
8665     {
8666       reload_ok_mem = (reload_in_progress
8667                        && GET_CODE (op) == REG
8668                        && REGNO (op) >= FIRST_PSEUDO_REGISTER
8669                        && reg_renumber [REGNO (op)] < 0);
8670     }
8671
8672   return reload_ok_mem;
8673 }