OSDN Git Service

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