OSDN Git Service

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