OSDN Git Service

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