* target.h (struct gcc_target): Add calls.pass_by_reference.
[official-gcc.git] / gcc / config / sparc / sparc.c
blob1dbd0020f8aaf0d0b6b5438aec4e83f043f99aef
1 /* Subroutines for insn-output.c for SPARC.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 64-bit SPARC-V9 support by Michael Tiemann, Jim Wilson, and Doug Evans,
6 at Cygnus Support.
8 This file is part of GCC.
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.
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.
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. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "rtl.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "real.h"
34 #include "insn-config.h"
35 #include "conditions.h"
36 #include "output.h"
37 #include "insn-attr.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "recog.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "debug.h"
47 #include "target.h"
48 #include "target-def.h"
49 #include "cfglayout.h"
50 #include "tree-gimple.h"
52 /* Processor costs */
53 static const
54 struct processor_costs cypress_costs = {
55 COSTS_N_INSNS (2), /* int load */
56 COSTS_N_INSNS (2), /* int signed load */
57 COSTS_N_INSNS (2), /* int zeroed load */
58 COSTS_N_INSNS (2), /* float load */
59 COSTS_N_INSNS (5), /* fmov, fneg, fabs */
60 COSTS_N_INSNS (5), /* fadd, fsub */
61 COSTS_N_INSNS (1), /* fcmp */
62 COSTS_N_INSNS (1), /* fmov, fmovr */
63 COSTS_N_INSNS (7), /* fmul */
64 COSTS_N_INSNS (37), /* fdivs */
65 COSTS_N_INSNS (37), /* fdivd */
66 COSTS_N_INSNS (63), /* fsqrts */
67 COSTS_N_INSNS (63), /* fsqrtd */
68 COSTS_N_INSNS (1), /* imul */
69 COSTS_N_INSNS (1), /* imulX */
70 0, /* imul bit factor */
71 COSTS_N_INSNS (1), /* idiv */
72 COSTS_N_INSNS (1), /* idivX */
73 COSTS_N_INSNS (1), /* movcc/movr */
74 0, /* shift penalty */
77 static const
78 struct processor_costs supersparc_costs = {
79 COSTS_N_INSNS (1), /* int load */
80 COSTS_N_INSNS (1), /* int signed load */
81 COSTS_N_INSNS (1), /* int zeroed load */
82 COSTS_N_INSNS (0), /* float load */
83 COSTS_N_INSNS (3), /* fmov, fneg, fabs */
84 COSTS_N_INSNS (3), /* fadd, fsub */
85 COSTS_N_INSNS (3), /* fcmp */
86 COSTS_N_INSNS (1), /* fmov, fmovr */
87 COSTS_N_INSNS (3), /* fmul */
88 COSTS_N_INSNS (6), /* fdivs */
89 COSTS_N_INSNS (9), /* fdivd */
90 COSTS_N_INSNS (12), /* fsqrts */
91 COSTS_N_INSNS (12), /* fsqrtd */
92 COSTS_N_INSNS (4), /* imul */
93 COSTS_N_INSNS (4), /* imulX */
94 0, /* imul bit factor */
95 COSTS_N_INSNS (4), /* idiv */
96 COSTS_N_INSNS (4), /* idivX */
97 COSTS_N_INSNS (1), /* movcc/movr */
98 1, /* shift penalty */
101 static const
102 struct processor_costs hypersparc_costs = {
103 COSTS_N_INSNS (1), /* int load */
104 COSTS_N_INSNS (1), /* int signed load */
105 COSTS_N_INSNS (1), /* int zeroed load */
106 COSTS_N_INSNS (1), /* float load */
107 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
108 COSTS_N_INSNS (1), /* fadd, fsub */
109 COSTS_N_INSNS (1), /* fcmp */
110 COSTS_N_INSNS (1), /* fmov, fmovr */
111 COSTS_N_INSNS (1), /* fmul */
112 COSTS_N_INSNS (8), /* fdivs */
113 COSTS_N_INSNS (12), /* fdivd */
114 COSTS_N_INSNS (17), /* fsqrts */
115 COSTS_N_INSNS (17), /* fsqrtd */
116 COSTS_N_INSNS (17), /* imul */
117 COSTS_N_INSNS (17), /* imulX */
118 0, /* imul bit factor */
119 COSTS_N_INSNS (17), /* idiv */
120 COSTS_N_INSNS (17), /* idivX */
121 COSTS_N_INSNS (1), /* movcc/movr */
122 0, /* shift penalty */
125 static const
126 struct processor_costs sparclet_costs = {
127 COSTS_N_INSNS (3), /* int load */
128 COSTS_N_INSNS (3), /* int signed load */
129 COSTS_N_INSNS (1), /* int zeroed load */
130 COSTS_N_INSNS (1), /* float load */
131 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
132 COSTS_N_INSNS (1), /* fadd, fsub */
133 COSTS_N_INSNS (1), /* fcmp */
134 COSTS_N_INSNS (1), /* fmov, fmovr */
135 COSTS_N_INSNS (1), /* fmul */
136 COSTS_N_INSNS (1), /* fdivs */
137 COSTS_N_INSNS (1), /* fdivd */
138 COSTS_N_INSNS (1), /* fsqrts */
139 COSTS_N_INSNS (1), /* fsqrtd */
140 COSTS_N_INSNS (5), /* imul */
141 COSTS_N_INSNS (5), /* imulX */
142 0, /* imul bit factor */
143 COSTS_N_INSNS (5), /* idiv */
144 COSTS_N_INSNS (5), /* idivX */
145 COSTS_N_INSNS (1), /* movcc/movr */
146 0, /* shift penalty */
149 static const
150 struct processor_costs ultrasparc_costs = {
151 COSTS_N_INSNS (2), /* int load */
152 COSTS_N_INSNS (3), /* int signed load */
153 COSTS_N_INSNS (2), /* int zeroed load */
154 COSTS_N_INSNS (2), /* float load */
155 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
156 COSTS_N_INSNS (4), /* fadd, fsub */
157 COSTS_N_INSNS (1), /* fcmp */
158 COSTS_N_INSNS (2), /* fmov, fmovr */
159 COSTS_N_INSNS (4), /* fmul */
160 COSTS_N_INSNS (13), /* fdivs */
161 COSTS_N_INSNS (23), /* fdivd */
162 COSTS_N_INSNS (13), /* fsqrts */
163 COSTS_N_INSNS (23), /* fsqrtd */
164 COSTS_N_INSNS (4), /* imul */
165 COSTS_N_INSNS (4), /* imulX */
166 2, /* imul bit factor */
167 COSTS_N_INSNS (37), /* idiv */
168 COSTS_N_INSNS (68), /* idivX */
169 COSTS_N_INSNS (2), /* movcc/movr */
170 2, /* shift penalty */
173 static const
174 struct processor_costs ultrasparc3_costs = {
175 COSTS_N_INSNS (2), /* int load */
176 COSTS_N_INSNS (3), /* int signed load */
177 COSTS_N_INSNS (3), /* int zeroed load */
178 COSTS_N_INSNS (2), /* float load */
179 COSTS_N_INSNS (3), /* fmov, fneg, fabs */
180 COSTS_N_INSNS (4), /* fadd, fsub */
181 COSTS_N_INSNS (5), /* fcmp */
182 COSTS_N_INSNS (3), /* fmov, fmovr */
183 COSTS_N_INSNS (4), /* fmul */
184 COSTS_N_INSNS (17), /* fdivs */
185 COSTS_N_INSNS (20), /* fdivd */
186 COSTS_N_INSNS (20), /* fsqrts */
187 COSTS_N_INSNS (29), /* fsqrtd */
188 COSTS_N_INSNS (6), /* imul */
189 COSTS_N_INSNS (6), /* imulX */
190 0, /* imul bit factor */
191 COSTS_N_INSNS (40), /* idiv */
192 COSTS_N_INSNS (71), /* idivX */
193 COSTS_N_INSNS (2), /* movcc/movr */
194 0, /* shift penalty */
197 const struct processor_costs *sparc_costs = &cypress_costs;
199 #ifdef HAVE_AS_RELAX_OPTION
200 /* If 'as' and 'ld' are relaxing tail call insns into branch always, use
201 "or %o7,%g0,X; call Y; or X,%g0,%o7" always, so that it can be optimized.
202 With sethi/jmp, neither 'as' nor 'ld' has an easy way how to find out if
203 somebody does not branch between the sethi and jmp. */
204 #define LEAF_SIBCALL_SLOT_RESERVED_P 1
205 #else
206 #define LEAF_SIBCALL_SLOT_RESERVED_P \
207 ((TARGET_ARCH64 && !TARGET_CM_MEDLOW) || flag_pic)
208 #endif
210 /* Global variables for machine-dependent things. */
212 /* Size of frame. Need to know this to emit return insns from leaf procedures.
213 ACTUAL_FSIZE is set by sparc_compute_frame_size() which is called during the
214 reload pass. This is important as the value is later used for scheduling
215 (to see what can go in a delay slot).
216 APPARENT_FSIZE is the size of the stack less the register save area and less
217 the outgoing argument area. It is used when saving call preserved regs. */
218 static HOST_WIDE_INT apparent_fsize;
219 static HOST_WIDE_INT actual_fsize;
221 /* Number of live general or floating point registers needed to be
222 saved (as 4-byte quantities). */
223 static int num_gfregs;
225 /* The alias set for prologue/epilogue register save/restore. */
226 static GTY(()) int sparc_sr_alias_set;
228 /* Save the operands last given to a compare for use when we
229 generate a scc or bcc insn. */
230 rtx sparc_compare_op0, sparc_compare_op1;
232 /* Vector to say how input registers are mapped to output registers.
233 HARD_FRAME_POINTER_REGNUM cannot be remapped by this function to
234 eliminate it. You must use -fomit-frame-pointer to get that. */
235 char leaf_reg_remap[] =
236 { 0, 1, 2, 3, 4, 5, 6, 7,
237 -1, -1, -1, -1, -1, -1, 14, -1,
238 -1, -1, -1, -1, -1, -1, -1, -1,
239 8, 9, 10, 11, 12, 13, -1, 15,
241 32, 33, 34, 35, 36, 37, 38, 39,
242 40, 41, 42, 43, 44, 45, 46, 47,
243 48, 49, 50, 51, 52, 53, 54, 55,
244 56, 57, 58, 59, 60, 61, 62, 63,
245 64, 65, 66, 67, 68, 69, 70, 71,
246 72, 73, 74, 75, 76, 77, 78, 79,
247 80, 81, 82, 83, 84, 85, 86, 87,
248 88, 89, 90, 91, 92, 93, 94, 95,
249 96, 97, 98, 99, 100};
251 /* Vector, indexed by hard register number, which contains 1
252 for a register that is allowable in a candidate for leaf
253 function treatment. */
254 char sparc_leaf_regs[] =
255 { 1, 1, 1, 1, 1, 1, 1, 1,
256 0, 0, 0, 0, 0, 0, 1, 0,
257 0, 0, 0, 0, 0, 0, 0, 0,
258 1, 1, 1, 1, 1, 1, 0, 1,
259 1, 1, 1, 1, 1, 1, 1, 1,
260 1, 1, 1, 1, 1, 1, 1, 1,
261 1, 1, 1, 1, 1, 1, 1, 1,
262 1, 1, 1, 1, 1, 1, 1, 1,
263 1, 1, 1, 1, 1, 1, 1, 1,
264 1, 1, 1, 1, 1, 1, 1, 1,
265 1, 1, 1, 1, 1, 1, 1, 1,
266 1, 1, 1, 1, 1, 1, 1, 1,
267 1, 1, 1, 1, 1};
269 struct machine_function GTY(())
271 /* Some local-dynamic TLS symbol name. */
272 const char *some_ld_name;
275 /* Register we pretend to think the frame pointer is allocated to.
276 Normally, this is %fp, but if we are in a leaf procedure, this
277 is %sp+"something". We record "something" separately as it may
278 be too big for reg+constant addressing. */
280 static rtx frame_base_reg;
281 static HOST_WIDE_INT frame_base_offset;
283 static void sparc_init_modes (void);
284 static void scan_record_type (tree, int *, int *, int *);
285 static int function_arg_slotno (const CUMULATIVE_ARGS *, enum machine_mode,
286 tree, int, int, int *, int *);
288 static int supersparc_adjust_cost (rtx, rtx, rtx, int);
289 static int hypersparc_adjust_cost (rtx, rtx, rtx, int);
291 static void sparc_output_addr_vec (rtx);
292 static void sparc_output_addr_diff_vec (rtx);
293 static void sparc_output_deferred_case_vectors (void);
294 static rtx sparc_builtin_saveregs (void);
295 static int epilogue_renumber (rtx *, int);
296 static bool sparc_assemble_integer (rtx, unsigned int, int);
297 static int set_extends (rtx);
298 static void load_pic_register (void);
299 static int save_or_restore_regs (int, int, rtx, int, int);
300 static void emit_save_regs (void);
301 static void emit_restore_regs (void);
302 static void sparc_asm_function_prologue (FILE *, HOST_WIDE_INT);
303 static void sparc_asm_function_epilogue (FILE *, HOST_WIDE_INT);
304 #ifdef OBJECT_FORMAT_ELF
305 static void sparc_elf_asm_named_section (const char *, unsigned int);
306 #endif
308 static int sparc_adjust_cost (rtx, rtx, rtx, int);
309 static int sparc_issue_rate (void);
310 static void sparc_sched_init (FILE *, int, int);
311 static int sparc_use_sched_lookahead (void);
313 static void emit_soft_tfmode_libcall (const char *, int, rtx *);
314 static void emit_soft_tfmode_binop (enum rtx_code, rtx *);
315 static void emit_soft_tfmode_unop (enum rtx_code, rtx *);
316 static void emit_soft_tfmode_cvt (enum rtx_code, rtx *);
317 static void emit_hard_tfmode_operation (enum rtx_code, rtx *);
319 static bool sparc_function_ok_for_sibcall (tree, tree);
320 static void sparc_init_libfuncs (void);
321 static void sparc_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
322 HOST_WIDE_INT, tree);
323 static struct machine_function * sparc_init_machine_status (void);
324 static bool sparc_cannot_force_const_mem (rtx);
325 static rtx sparc_tls_get_addr (void);
326 static rtx sparc_tls_got (void);
327 static const char *get_some_local_dynamic_name (void);
328 static int get_some_local_dynamic_name_1 (rtx *, void *);
329 static bool sparc_rtx_costs (rtx, int, int, int *);
330 static bool sparc_promote_prototypes (tree);
331 static rtx sparc_struct_value_rtx (tree, int);
332 static bool sparc_return_in_memory (tree, tree);
333 static bool sparc_strict_argument_naming (CUMULATIVE_ARGS *);
334 static tree sparc_gimplify_va_arg (tree, tree, tree *, tree *);
335 static bool sparc_pass_by_reference (CUMULATIVE_ARGS *,
336 enum machine_mode, tree, bool);
338 /* Option handling. */
340 /* Code model option as passed by user. */
341 const char *sparc_cmodel_string;
342 /* Parsed value. */
343 enum cmodel sparc_cmodel;
345 char sparc_hard_reg_printed[8];
347 struct sparc_cpu_select sparc_select[] =
349 /* switch name, tune arch */
350 { (char *)0, "default", 1, 1 },
351 { (char *)0, "-mcpu=", 1, 1 },
352 { (char *)0, "-mtune=", 1, 0 },
353 { 0, 0, 0, 0 }
356 /* CPU type. This is set from TARGET_CPU_DEFAULT and -m{cpu,tune}=xxx. */
357 enum processor_type sparc_cpu;
359 /* Initialize the GCC target structure. */
361 /* The sparc default is to use .half rather than .short for aligned
362 HI objects. Use .word instead of .long on non-ELF systems. */
363 #undef TARGET_ASM_ALIGNED_HI_OP
364 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
365 #ifndef OBJECT_FORMAT_ELF
366 #undef TARGET_ASM_ALIGNED_SI_OP
367 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
368 #endif
370 #undef TARGET_ASM_UNALIGNED_HI_OP
371 #define TARGET_ASM_UNALIGNED_HI_OP "\t.uahalf\t"
372 #undef TARGET_ASM_UNALIGNED_SI_OP
373 #define TARGET_ASM_UNALIGNED_SI_OP "\t.uaword\t"
374 #undef TARGET_ASM_UNALIGNED_DI_OP
375 #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaxword\t"
377 /* The target hook has to handle DI-mode values. */
378 #undef TARGET_ASM_INTEGER
379 #define TARGET_ASM_INTEGER sparc_assemble_integer
381 #undef TARGET_ASM_FUNCTION_PROLOGUE
382 #define TARGET_ASM_FUNCTION_PROLOGUE sparc_asm_function_prologue
383 #undef TARGET_ASM_FUNCTION_EPILOGUE
384 #define TARGET_ASM_FUNCTION_EPILOGUE sparc_asm_function_epilogue
386 #undef TARGET_SCHED_ADJUST_COST
387 #define TARGET_SCHED_ADJUST_COST sparc_adjust_cost
388 #undef TARGET_SCHED_ISSUE_RATE
389 #define TARGET_SCHED_ISSUE_RATE sparc_issue_rate
390 #undef TARGET_SCHED_INIT
391 #define TARGET_SCHED_INIT sparc_sched_init
392 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE
393 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE hook_int_void_1
394 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
395 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD sparc_use_sched_lookahead
397 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
398 #define TARGET_FUNCTION_OK_FOR_SIBCALL sparc_function_ok_for_sibcall
400 #undef TARGET_INIT_LIBFUNCS
401 #define TARGET_INIT_LIBFUNCS sparc_init_libfuncs
403 #ifdef HAVE_AS_TLS
404 #undef TARGET_HAVE_TLS
405 #define TARGET_HAVE_TLS true
406 #endif
407 #undef TARGET_CANNOT_FORCE_CONST_MEM
408 #define TARGET_CANNOT_FORCE_CONST_MEM sparc_cannot_force_const_mem
410 #undef TARGET_ASM_OUTPUT_MI_THUNK
411 #define TARGET_ASM_OUTPUT_MI_THUNK sparc_output_mi_thunk
412 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
413 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
415 #undef TARGET_RTX_COSTS
416 #define TARGET_RTX_COSTS sparc_rtx_costs
417 #undef TARGET_ADDRESS_COST
418 #define TARGET_ADDRESS_COST hook_int_rtx_0
420 /* This is only needed for TARGET_ARCH64, but since PROMOTE_FUNCTION_MODE is a
421 no-op for TARGET_ARCH32 this is ok. Otherwise we'd need to add a runtime
422 test for this value. */
423 #undef TARGET_PROMOTE_FUNCTION_ARGS
424 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
426 /* This is only needed for TARGET_ARCH64, but since PROMOTE_FUNCTION_MODE is a
427 no-op for TARGET_ARCH32 this is ok. Otherwise we'd need to add a runtime
428 test for this value. */
429 #undef TARGET_PROMOTE_FUNCTION_RETURN
430 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
432 #undef TARGET_PROMOTE_PROTOTYPES
433 #define TARGET_PROMOTE_PROTOTYPES sparc_promote_prototypes
435 #undef TARGET_STRUCT_VALUE_RTX
436 #define TARGET_STRUCT_VALUE_RTX sparc_struct_value_rtx
437 #undef TARGET_RETURN_IN_MEMORY
438 #define TARGET_RETURN_IN_MEMORY sparc_return_in_memory
439 #undef TARGET_MUST_PASS_IN_STACK
440 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
441 #undef TARGET_PASS_BY_REFERENCE
442 #define TARGET_PASS_BY_REFERENCE sparc_pass_by_reference
444 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
445 #define TARGET_EXPAND_BUILTIN_SAVEREGS sparc_builtin_saveregs
446 #undef TARGET_STRICT_ARGUMENT_NAMING
447 #define TARGET_STRICT_ARGUMENT_NAMING sparc_strict_argument_naming
449 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
450 #define TARGET_GIMPLIFY_VA_ARG_EXPR sparc_gimplify_va_arg
452 #undef TARGET_LATE_RTL_PROLOGUE_EPILOGUE
453 #define TARGET_LATE_RTL_PROLOGUE_EPILOGUE true
455 struct gcc_target targetm = TARGET_INITIALIZER;
457 /* Validate and override various options, and do some machine dependent
458 initialization. */
460 void
461 sparc_override_options (void)
463 static struct code_model {
464 const char *const name;
465 const int value;
466 } const cmodels[] = {
467 { "32", CM_32 },
468 { "medlow", CM_MEDLOW },
469 { "medmid", CM_MEDMID },
470 { "medany", CM_MEDANY },
471 { "embmedany", CM_EMBMEDANY },
472 { 0, 0 }
474 const struct code_model *cmodel;
475 /* Map TARGET_CPU_DEFAULT to value for -m{arch,tune}=. */
476 static struct cpu_default {
477 const int cpu;
478 const char *const name;
479 } const cpu_default[] = {
480 /* There must be one entry here for each TARGET_CPU value. */
481 { TARGET_CPU_sparc, "cypress" },
482 { TARGET_CPU_sparclet, "tsc701" },
483 { TARGET_CPU_sparclite, "f930" },
484 { TARGET_CPU_v8, "v8" },
485 { TARGET_CPU_hypersparc, "hypersparc" },
486 { TARGET_CPU_sparclite86x, "sparclite86x" },
487 { TARGET_CPU_supersparc, "supersparc" },
488 { TARGET_CPU_v9, "v9" },
489 { TARGET_CPU_ultrasparc, "ultrasparc" },
490 { TARGET_CPU_ultrasparc3, "ultrasparc3" },
491 { 0, 0 }
493 const struct cpu_default *def;
494 /* Table of values for -m{cpu,tune}=. */
495 static struct cpu_table {
496 const char *const name;
497 const enum processor_type processor;
498 const int disable;
499 const int enable;
500 } const cpu_table[] = {
501 { "v7", PROCESSOR_V7, MASK_ISA, 0 },
502 { "cypress", PROCESSOR_CYPRESS, MASK_ISA, 0 },
503 { "v8", PROCESSOR_V8, MASK_ISA, MASK_V8 },
504 /* TI TMS390Z55 supersparc */
505 { "supersparc", PROCESSOR_SUPERSPARC, MASK_ISA, MASK_V8 },
506 { "sparclite", PROCESSOR_SPARCLITE, MASK_ISA, MASK_SPARCLITE },
507 /* The Fujitsu MB86930 is the original sparclite chip, with no fpu.
508 The Fujitsu MB86934 is the recent sparclite chip, with an fpu. */
509 { "f930", PROCESSOR_F930, MASK_ISA|MASK_FPU, MASK_SPARCLITE },
510 { "f934", PROCESSOR_F934, MASK_ISA, MASK_SPARCLITE|MASK_FPU },
511 { "hypersparc", PROCESSOR_HYPERSPARC, MASK_ISA, MASK_V8|MASK_FPU },
512 { "sparclite86x", PROCESSOR_SPARCLITE86X, MASK_ISA|MASK_FPU,
513 MASK_SPARCLITE },
514 { "sparclet", PROCESSOR_SPARCLET, MASK_ISA, MASK_SPARCLET },
515 /* TEMIC sparclet */
516 { "tsc701", PROCESSOR_TSC701, MASK_ISA, MASK_SPARCLET },
517 { "v9", PROCESSOR_V9, MASK_ISA, MASK_V9 },
518 /* TI ultrasparc I, II, IIi */
519 { "ultrasparc", PROCESSOR_ULTRASPARC, MASK_ISA, MASK_V9
520 /* Although insns using %y are deprecated, it is a clear win on current
521 ultrasparcs. */
522 |MASK_DEPRECATED_V8_INSNS},
523 /* TI ultrasparc III */
524 /* ??? Check if %y issue still holds true in ultra3. */
525 { "ultrasparc3", PROCESSOR_ULTRASPARC3, MASK_ISA, MASK_V9|MASK_DEPRECATED_V8_INSNS},
526 { 0, 0, 0, 0 }
528 const struct cpu_table *cpu;
529 const struct sparc_cpu_select *sel;
530 int fpu;
532 #ifndef SPARC_BI_ARCH
533 /* Check for unsupported architecture size. */
534 if (! TARGET_64BIT != DEFAULT_ARCH32_P)
535 error ("%s is not supported by this configuration",
536 DEFAULT_ARCH32_P ? "-m64" : "-m32");
537 #endif
539 /* We force all 64bit archs to use 128 bit long double */
540 if (TARGET_64BIT && ! TARGET_LONG_DOUBLE_128)
542 error ("-mlong-double-64 not allowed with -m64");
543 target_flags |= MASK_LONG_DOUBLE_128;
546 /* Code model selection. */
547 sparc_cmodel = SPARC_DEFAULT_CMODEL;
549 #ifdef SPARC_BI_ARCH
550 if (TARGET_ARCH32)
551 sparc_cmodel = CM_32;
552 #endif
554 if (sparc_cmodel_string != NULL)
556 if (TARGET_ARCH64)
558 for (cmodel = &cmodels[0]; cmodel->name; cmodel++)
559 if (strcmp (sparc_cmodel_string, cmodel->name) == 0)
560 break;
561 if (cmodel->name == NULL)
562 error ("bad value (%s) for -mcmodel= switch", sparc_cmodel_string);
563 else
564 sparc_cmodel = cmodel->value;
566 else
567 error ("-mcmodel= is not supported on 32 bit systems");
570 fpu = TARGET_FPU; /* save current -mfpu status */
572 /* Set the default CPU. */
573 for (def = &cpu_default[0]; def->name; ++def)
574 if (def->cpu == TARGET_CPU_DEFAULT)
575 break;
576 if (! def->name)
577 abort ();
578 sparc_select[0].string = def->name;
580 for (sel = &sparc_select[0]; sel->name; ++sel)
582 if (sel->string)
584 for (cpu = &cpu_table[0]; cpu->name; ++cpu)
585 if (! strcmp (sel->string, cpu->name))
587 if (sel->set_tune_p)
588 sparc_cpu = cpu->processor;
590 if (sel->set_arch_p)
592 target_flags &= ~cpu->disable;
593 target_flags |= cpu->enable;
595 break;
598 if (! cpu->name)
599 error ("bad value (%s) for %s switch", sel->string, sel->name);
603 /* If -mfpu or -mno-fpu was explicitly used, don't override with
604 the processor default. Clear MASK_FPU_SET to avoid confusing
605 the reverse mapping from switch values to names. */
606 if (TARGET_FPU_SET)
608 target_flags = (target_flags & ~MASK_FPU) | fpu;
609 target_flags &= ~MASK_FPU_SET;
612 /* Don't allow -mvis if FPU is disabled. */
613 if (! TARGET_FPU)
614 target_flags &= ~MASK_VIS;
616 /* -mvis assumes UltraSPARC+, so we are sure v9 instructions
617 are available.
618 -m64 also implies v9. */
619 if (TARGET_VIS || TARGET_ARCH64)
621 target_flags |= MASK_V9;
622 target_flags &= ~(MASK_V8 | MASK_SPARCLET | MASK_SPARCLITE);
625 /* Use the deprecated v8 insns for sparc64 in 32 bit mode. */
626 if (TARGET_V9 && TARGET_ARCH32)
627 target_flags |= MASK_DEPRECATED_V8_INSNS;
629 /* V8PLUS requires V9, makes no sense in 64 bit mode. */
630 if (! TARGET_V9 || TARGET_ARCH64)
631 target_flags &= ~MASK_V8PLUS;
633 /* Don't use stack biasing in 32 bit mode. */
634 if (TARGET_ARCH32)
635 target_flags &= ~MASK_STACK_BIAS;
637 /* Supply a default value for align_functions. */
638 if (align_functions == 0
639 && (sparc_cpu == PROCESSOR_ULTRASPARC
640 || sparc_cpu == PROCESSOR_ULTRASPARC3))
641 align_functions = 32;
643 /* Validate PCC_STRUCT_RETURN. */
644 if (flag_pcc_struct_return == DEFAULT_PCC_STRUCT_RETURN)
645 flag_pcc_struct_return = (TARGET_ARCH64 ? 0 : 1);
647 /* Only use .uaxword when compiling for a 64-bit target. */
648 if (!TARGET_ARCH64)
649 targetm.asm_out.unaligned_op.di = NULL;
651 /* Do various machine dependent initializations. */
652 sparc_init_modes ();
654 /* Acquire a unique set number for our register saves and restores. */
655 sparc_sr_alias_set = new_alias_set ();
657 /* Set up function hooks. */
658 init_machine_status = sparc_init_machine_status;
660 switch (sparc_cpu)
662 case PROCESSOR_V7:
663 case PROCESSOR_CYPRESS:
664 sparc_costs = &cypress_costs;
665 break;
666 case PROCESSOR_V8:
667 case PROCESSOR_SPARCLITE:
668 case PROCESSOR_SUPERSPARC:
669 sparc_costs = &supersparc_costs;
670 break;
671 case PROCESSOR_F930:
672 case PROCESSOR_F934:
673 case PROCESSOR_HYPERSPARC:
674 case PROCESSOR_SPARCLITE86X:
675 sparc_costs = &hypersparc_costs;
676 break;
677 case PROCESSOR_SPARCLET:
678 case PROCESSOR_TSC701:
679 sparc_costs = &sparclet_costs;
680 break;
681 case PROCESSOR_V9:
682 case PROCESSOR_ULTRASPARC:
683 sparc_costs = &ultrasparc_costs;
684 break;
685 case PROCESSOR_ULTRASPARC3:
686 sparc_costs = &ultrasparc3_costs;
687 break;
691 /* Miscellaneous utilities. */
693 /* Nonzero if CODE, a comparison, is suitable for use in v9 conditional move
694 or branch on register contents instructions. */
697 v9_regcmp_p (enum rtx_code code)
699 return (code == EQ || code == NE || code == GE || code == LT
700 || code == LE || code == GT);
704 /* Operand constraints. */
706 /* Return nonzero only if OP is a register of mode MODE,
707 or const0_rtx. */
710 reg_or_0_operand (rtx op, enum machine_mode mode)
712 if (register_operand (op, mode))
713 return 1;
714 if (op == const0_rtx)
715 return 1;
716 if (GET_MODE (op) == VOIDmode && GET_CODE (op) == CONST_DOUBLE
717 && CONST_DOUBLE_HIGH (op) == 0
718 && CONST_DOUBLE_LOW (op) == 0)
719 return 1;
720 if (fp_zero_operand (op, mode))
721 return 1;
722 return 0;
725 /* Return nonzero only if OP is const1_rtx. */
728 const1_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
730 return op == const1_rtx;
733 /* Nonzero if OP is a floating point value with value 0.0. */
736 fp_zero_operand (rtx op, enum machine_mode mode)
738 if (GET_MODE_CLASS (GET_MODE (op)) != MODE_FLOAT)
739 return 0;
740 return op == CONST0_RTX (mode);
743 /* Nonzero if OP is a register operand in floating point register. */
746 fp_register_operand (rtx op, enum machine_mode mode)
748 if (! register_operand (op, mode))
749 return 0;
750 if (GET_CODE (op) == SUBREG)
751 op = SUBREG_REG (op);
752 return GET_CODE (op) == REG && SPARC_FP_REG_P (REGNO (op));
755 /* Nonzero if OP is a floating point constant which can
756 be loaded into an integer register using a single
757 sethi instruction. */
760 fp_sethi_p (rtx op)
762 if (GET_CODE (op) == CONST_DOUBLE)
764 REAL_VALUE_TYPE r;
765 long i;
767 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
768 if (REAL_VALUES_EQUAL (r, dconst0) &&
769 ! REAL_VALUE_MINUS_ZERO (r))
770 return 0;
771 REAL_VALUE_TO_TARGET_SINGLE (r, i);
772 if (SPARC_SETHI_P (i))
773 return 1;
776 return 0;
779 /* Nonzero if OP is a floating point constant which can
780 be loaded into an integer register using a single
781 mov instruction. */
784 fp_mov_p (rtx op)
786 if (GET_CODE (op) == CONST_DOUBLE)
788 REAL_VALUE_TYPE r;
789 long i;
791 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
792 if (REAL_VALUES_EQUAL (r, dconst0) &&
793 ! REAL_VALUE_MINUS_ZERO (r))
794 return 0;
795 REAL_VALUE_TO_TARGET_SINGLE (r, i);
796 if (SPARC_SIMM13_P (i))
797 return 1;
800 return 0;
803 /* Nonzero if OP is a floating point constant which can
804 be loaded into an integer register using a high/losum
805 instruction sequence. */
808 fp_high_losum_p (rtx op)
810 /* The constraints calling this should only be in
811 SFmode move insns, so any constant which cannot
812 be moved using a single insn will do. */
813 if (GET_CODE (op) == CONST_DOUBLE)
815 REAL_VALUE_TYPE r;
816 long i;
818 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
819 if (REAL_VALUES_EQUAL (r, dconst0) &&
820 ! REAL_VALUE_MINUS_ZERO (r))
821 return 0;
822 REAL_VALUE_TO_TARGET_SINGLE (r, i);
823 if (! SPARC_SETHI_P (i)
824 && ! SPARC_SIMM13_P (i))
825 return 1;
828 return 0;
831 /* Nonzero if OP is an integer register. */
834 intreg_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
836 return (register_operand (op, SImode)
837 || (TARGET_ARCH64 && register_operand (op, DImode)));
840 /* Nonzero if OP is a floating point condition code register. */
843 fcc_reg_operand (rtx op, enum machine_mode mode)
845 /* This can happen when recog is called from combine. Op may be a MEM.
846 Fail instead of calling abort in this case. */
847 if (GET_CODE (op) != REG)
848 return 0;
850 if (mode != VOIDmode && mode != GET_MODE (op))
851 return 0;
852 if (mode == VOIDmode
853 && (GET_MODE (op) != CCFPmode && GET_MODE (op) != CCFPEmode))
854 return 0;
856 #if 0 /* ??? ==> 1 when %fcc0-3 are pseudos first. See gen_compare_reg(). */
857 if (reg_renumber == 0)
858 return REGNO (op) >= FIRST_PSEUDO_REGISTER;
859 return REGNO_OK_FOR_CCFP_P (REGNO (op));
860 #else
861 return (unsigned) REGNO (op) - SPARC_FIRST_V9_FCC_REG < 4;
862 #endif
865 /* Nonzero if OP is a floating point condition code fcc0 register. */
868 fcc0_reg_operand (rtx op, enum machine_mode mode)
870 /* This can happen when recog is called from combine. Op may be a MEM.
871 Fail instead of calling abort in this case. */
872 if (GET_CODE (op) != REG)
873 return 0;
875 if (mode != VOIDmode && mode != GET_MODE (op))
876 return 0;
877 if (mode == VOIDmode
878 && (GET_MODE (op) != CCFPmode && GET_MODE (op) != CCFPEmode))
879 return 0;
881 return REGNO (op) == SPARC_FCC_REG;
884 /* Nonzero if OP is an integer or floating point condition code register. */
887 icc_or_fcc_reg_operand (rtx op, enum machine_mode mode)
889 if (GET_CODE (op) == REG && REGNO (op) == SPARC_ICC_REG)
891 if (mode != VOIDmode && mode != GET_MODE (op))
892 return 0;
893 if (mode == VOIDmode
894 && GET_MODE (op) != CCmode && GET_MODE (op) != CCXmode)
895 return 0;
896 return 1;
899 return fcc_reg_operand (op, mode);
902 /* Call insn on SPARC can take a PC-relative constant address, or any regular
903 memory address. */
906 call_operand (rtx op, enum machine_mode mode)
908 if (GET_CODE (op) != MEM)
909 abort ();
910 op = XEXP (op, 0);
911 return (symbolic_operand (op, mode) || memory_address_p (Pmode, op));
915 call_operand_address (rtx op, enum machine_mode mode)
917 return (symbolic_operand (op, mode) || memory_address_p (Pmode, op));
920 /* If OP is a SYMBOL_REF of a thread-local symbol, return its TLS mode,
921 otherwise return 0. */
924 tls_symbolic_operand (rtx op)
926 if (GET_CODE (op) != SYMBOL_REF)
927 return 0;
928 return SYMBOL_REF_TLS_MODEL (op);
932 tgd_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
934 return tls_symbolic_operand (op) == TLS_MODEL_GLOBAL_DYNAMIC;
938 tld_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
940 return tls_symbolic_operand (op) == TLS_MODEL_LOCAL_DYNAMIC;
944 tie_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
946 return tls_symbolic_operand (op) == TLS_MODEL_INITIAL_EXEC;
950 tle_symbolic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
952 return tls_symbolic_operand (op) == TLS_MODEL_LOCAL_EXEC;
955 /* Returns 1 if OP is either a symbol reference or a sum of a symbol
956 reference and a constant. */
959 symbolic_operand (register rtx op, enum machine_mode mode)
961 enum machine_mode omode = GET_MODE (op);
963 if (omode != mode && omode != VOIDmode && mode != VOIDmode)
964 return 0;
966 switch (GET_CODE (op))
968 case SYMBOL_REF:
969 return !SYMBOL_REF_TLS_MODEL (op);
971 case LABEL_REF:
972 return 1;
974 case CONST:
975 op = XEXP (op, 0);
976 return (((GET_CODE (XEXP (op, 0)) == SYMBOL_REF
977 && !SYMBOL_REF_TLS_MODEL (XEXP (op, 0)))
978 || GET_CODE (XEXP (op, 0)) == LABEL_REF)
979 && GET_CODE (XEXP (op, 1)) == CONST_INT);
981 default:
982 return 0;
986 /* Return truth value of statement that OP is a symbolic memory
987 operand of mode MODE. */
990 symbolic_memory_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
992 if (GET_CODE (op) == SUBREG)
993 op = SUBREG_REG (op);
994 if (GET_CODE (op) != MEM)
995 return 0;
996 op = XEXP (op, 0);
997 return ((GET_CODE (op) == SYMBOL_REF && !SYMBOL_REF_TLS_MODEL (op))
998 || GET_CODE (op) == CONST || GET_CODE (op) == HIGH
999 || GET_CODE (op) == LABEL_REF);
1002 /* Return truth value of statement that OP is a LABEL_REF of mode MODE. */
1005 label_ref_operand (rtx op, enum machine_mode mode)
1007 if (GET_CODE (op) != LABEL_REF)
1008 return 0;
1009 if (GET_MODE (op) != mode)
1010 return 0;
1011 return 1;
1014 /* Return 1 if the operand is an argument used in generating pic references
1015 in either the medium/low or medium/anywhere code models of sparc64. */
1018 sp64_medium_pic_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1020 /* Check for (const (minus (symbol_ref:GOT)
1021 (const (minus (label) (pc))))). */
1022 if (GET_CODE (op) != CONST)
1023 return 0;
1024 op = XEXP (op, 0);
1025 if (GET_CODE (op) != MINUS)
1026 return 0;
1027 if (GET_CODE (XEXP (op, 0)) != SYMBOL_REF)
1028 return 0;
1029 /* ??? Ensure symbol is GOT. */
1030 if (GET_CODE (XEXP (op, 1)) != CONST)
1031 return 0;
1032 if (GET_CODE (XEXP (XEXP (op, 1), 0)) != MINUS)
1033 return 0;
1034 return 1;
1037 /* Return 1 if the operand is a data segment reference. This includes
1038 the readonly data segment, or in other words anything but the text segment.
1039 This is needed in the medium/anywhere code model on v9. These values
1040 are accessed with EMBMEDANY_BASE_REG. */
1043 data_segment_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1045 switch (GET_CODE (op))
1047 case SYMBOL_REF :
1048 return ! SYMBOL_REF_FUNCTION_P (op);
1049 case PLUS :
1050 /* Assume canonical format of symbol + constant.
1051 Fall through. */
1052 case CONST :
1053 return data_segment_operand (XEXP (op, 0), VOIDmode);
1054 default :
1055 return 0;
1059 /* Return 1 if the operand is a text segment reference.
1060 This is needed in the medium/anywhere code model on v9. */
1063 text_segment_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1065 switch (GET_CODE (op))
1067 case LABEL_REF :
1068 return 1;
1069 case SYMBOL_REF :
1070 return SYMBOL_REF_FUNCTION_P (op);
1071 case PLUS :
1072 /* Assume canonical format of symbol + constant.
1073 Fall through. */
1074 case CONST :
1075 return text_segment_operand (XEXP (op, 0), VOIDmode);
1076 default :
1077 return 0;
1081 /* Return 1 if the operand is either a register or a memory operand that is
1082 not symbolic. */
1085 reg_or_nonsymb_mem_operand (register rtx op, enum machine_mode mode)
1087 if (register_operand (op, mode))
1088 return 1;
1090 if (memory_operand (op, mode) && ! symbolic_memory_operand (op, mode))
1091 return 1;
1093 return 0;
1097 splittable_symbolic_memory_operand (rtx op,
1098 enum machine_mode mode ATTRIBUTE_UNUSED)
1100 if (GET_CODE (op) != MEM)
1101 return 0;
1102 if (! symbolic_operand (XEXP (op, 0), Pmode))
1103 return 0;
1104 return 1;
1108 splittable_immediate_memory_operand (rtx op,
1109 enum machine_mode mode ATTRIBUTE_UNUSED)
1111 if (GET_CODE (op) != MEM)
1112 return 0;
1113 if (! immediate_operand (XEXP (op, 0), Pmode))
1114 return 0;
1115 return 1;
1118 /* Return truth value of whether OP is EQ or NE. */
1121 eq_or_neq (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1123 return (GET_CODE (op) == EQ || GET_CODE (op) == NE);
1126 /* Return 1 if this is a comparison operator, but not an EQ, NE, GEU,
1127 or LTU for non-floating-point. We handle those specially. */
1130 normal_comp_operator (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1132 enum rtx_code code;
1134 if (!COMPARISON_P (op))
1135 return 0;
1137 if (GET_MODE (XEXP (op, 0)) == CCFPmode
1138 || GET_MODE (XEXP (op, 0)) == CCFPEmode)
1139 return 1;
1141 code = GET_CODE (op);
1142 return (code != NE && code != EQ && code != GEU && code != LTU);
1145 /* Return 1 if this is a comparison operator. This allows the use of
1146 MATCH_OPERATOR to recognize all the branch insns. */
1149 noov_compare_op (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1151 enum rtx_code code;
1153 if (!COMPARISON_P (op))
1154 return 0;
1156 code = GET_CODE (op);
1157 if (GET_MODE (XEXP (op, 0)) == CC_NOOVmode
1158 || GET_MODE (XEXP (op, 0)) == CCX_NOOVmode)
1159 /* These are the only branches which work with CC_NOOVmode. */
1160 return (code == EQ || code == NE || code == GE || code == LT);
1161 return 1;
1164 /* Return 1 if this is a 64-bit comparison operator. This allows the use of
1165 MATCH_OPERATOR to recognize all the branch insns. */
1168 noov_compare64_op (register rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1170 enum rtx_code code;
1172 if (! TARGET_V9)
1173 return 0;
1175 if (!COMPARISON_P (op))
1176 return 0;
1178 code = GET_CODE (op);
1179 if (GET_MODE (XEXP (op, 0)) == CCX_NOOVmode)
1180 /* These are the only branches which work with CCX_NOOVmode. */
1181 return (code == EQ || code == NE || code == GE || code == LT);
1182 return (GET_MODE (XEXP (op, 0)) == CCXmode);
1185 /* Nonzero if OP is a comparison operator suitable for use in v9
1186 conditional move or branch on register contents instructions. */
1189 v9_regcmp_op (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1191 enum rtx_code code;
1193 if (!COMPARISON_P (op))
1194 return 0;
1196 code = GET_CODE (op);
1197 return v9_regcmp_p (code);
1200 /* Return 1 if this is a SIGN_EXTEND or ZERO_EXTEND operation. */
1203 extend_op (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1205 return GET_CODE (op) == SIGN_EXTEND || GET_CODE (op) == ZERO_EXTEND;
1208 /* Return nonzero if OP is an operator of mode MODE which can set
1209 the condition codes explicitly. We do not include PLUS and MINUS
1210 because these require CC_NOOVmode, which we handle explicitly. */
1213 cc_arithop (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1215 if (GET_CODE (op) == AND
1216 || GET_CODE (op) == IOR
1217 || GET_CODE (op) == XOR)
1218 return 1;
1220 return 0;
1223 /* Return nonzero if OP is an operator of mode MODE which can bitwise
1224 complement its second operand and set the condition codes explicitly. */
1227 cc_arithopn (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1229 /* XOR is not here because combine canonicalizes (xor (not ...) ...)
1230 and (xor ... (not ...)) to (not (xor ...)). */
1231 return (GET_CODE (op) == AND
1232 || GET_CODE (op) == IOR);
1235 /* Return true if OP is a register, or is a CONST_INT that can fit in a
1236 signed 13 bit immediate field. This is an acceptable SImode operand for
1237 most 3 address instructions. */
1240 arith_operand (rtx op, enum machine_mode mode)
1242 if (register_operand (op, mode))
1243 return 1;
1244 if (GET_CODE (op) != CONST_INT)
1245 return 0;
1246 return SMALL_INT32 (op);
1249 /* Return true if OP is a constant 4096 */
1252 arith_4096_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1254 if (GET_CODE (op) != CONST_INT)
1255 return 0;
1256 else
1257 return INTVAL (op) == 4096;
1260 /* Return true if OP is suitable as second operand for add/sub */
1263 arith_add_operand (rtx op, enum machine_mode mode)
1265 return arith_operand (op, mode) || arith_4096_operand (op, mode);
1268 /* Return true if OP is a CONST_INT or a CONST_DOUBLE which can fit in the
1269 immediate field of OR and XOR instructions. Used for 64-bit
1270 constant formation patterns. */
1272 const64_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1274 return ((GET_CODE (op) == CONST_INT
1275 && SPARC_SIMM13_P (INTVAL (op)))
1276 #if HOST_BITS_PER_WIDE_INT != 64
1277 || (GET_CODE (op) == CONST_DOUBLE
1278 && SPARC_SIMM13_P (CONST_DOUBLE_LOW (op))
1279 && (CONST_DOUBLE_HIGH (op) ==
1280 ((CONST_DOUBLE_LOW (op) & 0x80000000) != 0 ?
1281 (HOST_WIDE_INT)-1 : 0)))
1282 #endif
1286 /* The same, but only for sethi instructions. */
1288 const64_high_operand (rtx op, enum machine_mode mode)
1290 return ((GET_CODE (op) == CONST_INT
1291 && (INTVAL (op) & ~(HOST_WIDE_INT)0x3ff) != 0
1292 && SPARC_SETHI_P (INTVAL (op) & GET_MODE_MASK (mode))
1294 || (GET_CODE (op) == CONST_DOUBLE
1295 && CONST_DOUBLE_HIGH (op) == 0
1296 && (CONST_DOUBLE_LOW (op) & ~(HOST_WIDE_INT)0x3ff) != 0
1297 && SPARC_SETHI_P (CONST_DOUBLE_LOW (op))));
1300 /* Return true if OP is a register, or is a CONST_INT that can fit in a
1301 signed 11 bit immediate field. This is an acceptable SImode operand for
1302 the movcc instructions. */
1305 arith11_operand (rtx op, enum machine_mode mode)
1307 return (register_operand (op, mode)
1308 || (GET_CODE (op) == CONST_INT && SPARC_SIMM11_P (INTVAL (op))));
1311 /* Return true if OP is a register, or is a CONST_INT that can fit in a
1312 signed 10 bit immediate field. This is an acceptable SImode operand for
1313 the movrcc instructions. */
1316 arith10_operand (rtx op, enum machine_mode mode)
1318 return (register_operand (op, mode)
1319 || (GET_CODE (op) == CONST_INT && SPARC_SIMM10_P (INTVAL (op))));
1322 /* Return true if OP is a register, is a CONST_INT that fits in a 13 bit
1323 immediate field, or is a CONST_DOUBLE whose both parts fit in a 13 bit
1324 immediate field.
1325 ARCH64: Return true if OP is a register, or is a CONST_INT or CONST_DOUBLE that
1326 can fit in a 13 bit immediate field. This is an acceptable DImode operand
1327 for most 3 address instructions. */
1330 arith_double_operand (rtx op, enum machine_mode mode)
1332 return (register_operand (op, mode)
1333 || (GET_CODE (op) == CONST_INT && SMALL_INT (op))
1334 || (! TARGET_ARCH64
1335 && GET_CODE (op) == CONST_DOUBLE
1336 && (unsigned HOST_WIDE_INT) (CONST_DOUBLE_LOW (op) + 0x1000) < 0x2000
1337 && (unsigned HOST_WIDE_INT) (CONST_DOUBLE_HIGH (op) + 0x1000) < 0x2000)
1338 || (TARGET_ARCH64
1339 && GET_CODE (op) == CONST_DOUBLE
1340 && (unsigned HOST_WIDE_INT) (CONST_DOUBLE_LOW (op) + 0x1000) < 0x2000
1341 && ((CONST_DOUBLE_HIGH (op) == -1
1342 && (CONST_DOUBLE_LOW (op) & 0x1000) == 0x1000)
1343 || (CONST_DOUBLE_HIGH (op) == 0
1344 && (CONST_DOUBLE_LOW (op) & 0x1000) == 0))));
1347 /* Return true if OP is a constant 4096 for DImode on ARCH64 */
1350 arith_double_4096_operand (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1352 return (TARGET_ARCH64 &&
1353 ((GET_CODE (op) == CONST_INT && INTVAL (op) == 4096) ||
1354 (GET_CODE (op) == CONST_DOUBLE &&
1355 CONST_DOUBLE_LOW (op) == 4096 &&
1356 CONST_DOUBLE_HIGH (op) == 0)));
1359 /* Return true if OP is suitable as second operand for add/sub in DImode */
1362 arith_double_add_operand (rtx op, enum machine_mode mode)
1364 return arith_double_operand (op, mode) || arith_double_4096_operand (op, mode);
1367 /* Return true if OP is a register, or is a CONST_INT or CONST_DOUBLE that
1368 can fit in an 11 bit immediate field. This is an acceptable DImode
1369 operand for the movcc instructions. */
1370 /* ??? Replace with arith11_operand? */
1373 arith11_double_operand (rtx op, enum machine_mode mode)
1375 return (register_operand (op, mode)
1376 || (GET_CODE (op) == CONST_DOUBLE
1377 && (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
1378 && (unsigned HOST_WIDE_INT) (CONST_DOUBLE_LOW (op) + 0x400) < 0x800
1379 && ((CONST_DOUBLE_HIGH (op) == -1
1380 && (CONST_DOUBLE_LOW (op) & 0x400) == 0x400)
1381 || (CONST_DOUBLE_HIGH (op) == 0
1382 && (CONST_DOUBLE_LOW (op) & 0x400) == 0)))
1383 || (GET_CODE (op) == CONST_INT
1384 && (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
1385 && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x400) < 0x800));
1388 /* Return true if OP is a register, or is a CONST_INT or CONST_DOUBLE that
1389 can fit in an 10 bit immediate field. This is an acceptable DImode
1390 operand for the movrcc instructions. */
1391 /* ??? Replace with arith10_operand? */
1394 arith10_double_operand (rtx op, enum machine_mode mode)
1396 return (register_operand (op, mode)
1397 || (GET_CODE (op) == CONST_DOUBLE
1398 && (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
1399 && (unsigned) (CONST_DOUBLE_LOW (op) + 0x200) < 0x400
1400 && ((CONST_DOUBLE_HIGH (op) == -1
1401 && (CONST_DOUBLE_LOW (op) & 0x200) == 0x200)
1402 || (CONST_DOUBLE_HIGH (op) == 0
1403 && (CONST_DOUBLE_LOW (op) & 0x200) == 0)))
1404 || (GET_CODE (op) == CONST_INT
1405 && (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
1406 && (unsigned HOST_WIDE_INT) (INTVAL (op) + 0x200) < 0x400));
1409 /* Return truth value of whether OP is an integer which fits the
1410 range constraining immediate operands in most three-address insns,
1411 which have a 13 bit immediate field. */
1414 small_int (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1416 return (GET_CODE (op) == CONST_INT && SMALL_INT (op));
1420 small_int_or_double (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1422 return ((GET_CODE (op) == CONST_INT && SMALL_INT (op))
1423 || (GET_CODE (op) == CONST_DOUBLE
1424 && CONST_DOUBLE_HIGH (op) == 0
1425 && SPARC_SIMM13_P (CONST_DOUBLE_LOW (op))));
1428 /* Recognize operand values for the umul instruction. That instruction sign
1429 extends immediate values just like all other sparc instructions, but
1430 interprets the extended result as an unsigned number. */
1433 uns_small_int (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1435 #if HOST_BITS_PER_WIDE_INT > 32
1436 /* All allowed constants will fit a CONST_INT. */
1437 return (GET_CODE (op) == CONST_INT
1438 && ((INTVAL (op) >= 0 && INTVAL (op) < 0x1000)
1439 || (INTVAL (op) >= 0xFFFFF000
1440 && INTVAL (op) <= 0xFFFFFFFF)));
1441 #else
1442 return ((GET_CODE (op) == CONST_INT && (unsigned) INTVAL (op) < 0x1000)
1443 || (GET_CODE (op) == CONST_DOUBLE
1444 && CONST_DOUBLE_HIGH (op) == 0
1445 && (unsigned) CONST_DOUBLE_LOW (op) - 0xFFFFF000 < 0x1000));
1446 #endif
1450 uns_arith_operand (rtx op, enum machine_mode mode)
1452 return register_operand (op, mode) || uns_small_int (op, mode);
1455 /* Return truth value of statement that OP is a call-clobbered register. */
1457 clobbered_register (rtx op, enum machine_mode mode ATTRIBUTE_UNUSED)
1459 return (GET_CODE (op) == REG && call_used_regs[REGNO (op)]);
1462 /* Return 1 if OP is a valid operand for the source of a move insn. */
1465 input_operand (rtx op, enum machine_mode mode)
1467 /* If both modes are non-void they must be the same. */
1468 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && mode != GET_MODE (op))
1469 return 0;
1471 /* Allow any one instruction integer constant, and all CONST_INT
1472 variants when we are working in DImode and !arch64. */
1473 if (GET_MODE_CLASS (mode) == MODE_INT
1474 && ((GET_CODE (op) == CONST_INT
1475 && (SPARC_SETHI_P (INTVAL (op) & GET_MODE_MASK (mode))
1476 || SPARC_SIMM13_P (INTVAL (op))
1477 || (mode == DImode
1478 && ! TARGET_ARCH64)))
1479 || (TARGET_ARCH64
1480 && GET_CODE (op) == CONST_DOUBLE
1481 && ((CONST_DOUBLE_HIGH (op) == 0
1482 && SPARC_SETHI_P (CONST_DOUBLE_LOW (op)))
1484 #if HOST_BITS_PER_WIDE_INT == 64
1485 (CONST_DOUBLE_HIGH (op) == 0
1486 && SPARC_SIMM13_P (CONST_DOUBLE_LOW (op)))
1487 #else
1488 (SPARC_SIMM13_P (CONST_DOUBLE_LOW (op))
1489 && (((CONST_DOUBLE_LOW (op) & 0x80000000) == 0
1490 && CONST_DOUBLE_HIGH (op) == 0)
1491 || (CONST_DOUBLE_HIGH (op) == -1
1492 && CONST_DOUBLE_LOW (op) & 0x80000000) != 0))
1493 #endif
1494 ))))
1495 return 1;
1497 /* If !arch64 and this is a DImode const, allow it so that
1498 the splits can be generated. */
1499 if (! TARGET_ARCH64
1500 && mode == DImode
1501 && GET_CODE (op) == CONST_DOUBLE)
1502 return 1;
1504 if (register_operand (op, mode))
1505 return 1;
1507 if (GET_MODE_CLASS (mode) == MODE_FLOAT
1508 && GET_CODE (op) == CONST_DOUBLE)
1509 return 1;
1511 /* If this is a SUBREG, look inside so that we handle
1512 paradoxical ones. */
1513 if (GET_CODE (op) == SUBREG)
1514 op = SUBREG_REG (op);
1516 /* Check for valid MEM forms. */
1517 if (GET_CODE (op) == MEM)
1519 rtx inside = XEXP (op, 0);
1521 if (GET_CODE (inside) == LO_SUM)
1523 /* We can't allow these because all of the splits
1524 (eventually as they trickle down into DFmode
1525 splits) require offsettable memory references. */
1526 if (! TARGET_V9
1527 && GET_MODE (op) == TFmode)
1528 return 0;
1530 return (register_operand (XEXP (inside, 0), Pmode)
1531 && CONSTANT_P (XEXP (inside, 1)));
1533 return memory_address_p (mode, inside);
1536 return 0;
1539 /* Return 1 if OP is valid for the lhs of a compare insn. */
1542 compare_operand (rtx op, enum machine_mode mode)
1544 if (GET_CODE (op) == ZERO_EXTRACT)
1545 return (register_operand (XEXP (op, 0), mode)
1546 && small_int_or_double (XEXP (op, 1), mode)
1547 && small_int_or_double (XEXP (op, 2), mode)
1548 /* This matches cmp_zero_extract. */
1549 && ((mode == SImode
1550 && ((GET_CODE (XEXP (op, 2)) == CONST_INT
1551 && INTVAL (XEXP (op, 2)) > 19)
1552 || (GET_CODE (XEXP (op, 2)) == CONST_DOUBLE
1553 && CONST_DOUBLE_LOW (XEXP (op, 2)) > 19)))
1554 /* This matches cmp_zero_extract_sp64. */
1555 || (mode == DImode
1556 && TARGET_ARCH64
1557 && ((GET_CODE (XEXP (op, 2)) == CONST_INT
1558 && INTVAL (XEXP (op, 2)) > 51)
1559 || (GET_CODE (XEXP (op, 2)) == CONST_DOUBLE
1560 && CONST_DOUBLE_LOW (XEXP (op, 2)) > 51)))));
1561 else
1562 return register_operand (op, mode);
1566 /* We know it can't be done in one insn when we get here,
1567 the movsi expander guarantees this. */
1568 void
1569 sparc_emit_set_const32 (rtx op0, rtx op1)
1571 enum machine_mode mode = GET_MODE (op0);
1572 rtx temp;
1574 if (GET_CODE (op1) == CONST_INT)
1576 HOST_WIDE_INT value = INTVAL (op1);
1578 if (SPARC_SETHI_P (value & GET_MODE_MASK (mode))
1579 || SPARC_SIMM13_P (value))
1580 abort ();
1583 /* Full 2-insn decomposition is needed. */
1584 if (reload_in_progress || reload_completed)
1585 temp = op0;
1586 else
1587 temp = gen_reg_rtx (mode);
1589 if (GET_CODE (op1) == CONST_INT)
1591 /* Emit them as real moves instead of a HIGH/LO_SUM,
1592 this way CSE can see everything and reuse intermediate
1593 values if it wants. */
1594 if (TARGET_ARCH64
1595 && HOST_BITS_PER_WIDE_INT != 64
1596 && (INTVAL (op1) & 0x80000000) != 0)
1597 emit_insn (gen_rtx_SET
1598 (VOIDmode, temp,
1599 immed_double_const (INTVAL (op1) & ~(HOST_WIDE_INT)0x3ff,
1600 0, DImode)));
1601 else
1602 emit_insn (gen_rtx_SET (VOIDmode, temp,
1603 GEN_INT (INTVAL (op1)
1604 & ~(HOST_WIDE_INT)0x3ff)));
1606 emit_insn (gen_rtx_SET (VOIDmode,
1607 op0,
1608 gen_rtx_IOR (mode, temp,
1609 GEN_INT (INTVAL (op1) & 0x3ff))));
1611 else
1613 /* A symbol, emit in the traditional way. */
1614 emit_insn (gen_rtx_SET (VOIDmode, temp,
1615 gen_rtx_HIGH (mode, op1)));
1616 emit_insn (gen_rtx_SET (VOIDmode,
1617 op0, gen_rtx_LO_SUM (mode, temp, op1)));
1623 /* Load OP1, a symbolic 64-bit constant, into OP0, a DImode register.
1624 If TEMP is non-zero, we are forbidden to use any other scratch
1625 registers. Otherwise, we are allowed to generate them as needed.
1627 Note that TEMP may have TImode if the code model is TARGET_CM_MEDANY
1628 or TARGET_CM_EMBMEDANY (see the reload_indi and reload_outdi patterns). */
1629 void
1630 sparc_emit_set_symbolic_const64 (rtx op0, rtx op1, rtx temp)
1632 rtx temp1, temp2, temp3, temp4, temp5;
1633 rtx ti_temp = 0;
1635 if (temp && GET_MODE (temp) == TImode)
1637 ti_temp = temp;
1638 temp = gen_rtx_REG (DImode, REGNO (temp));
1641 /* SPARC-V9 code-model support. */
1642 switch (sparc_cmodel)
1644 case CM_MEDLOW:
1645 /* The range spanned by all instructions in the object is less
1646 than 2^31 bytes (2GB) and the distance from any instruction
1647 to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1648 than 2^31 bytes (2GB).
1650 The executable must be in the low 4TB of the virtual address
1651 space.
1653 sethi %hi(symbol), %temp1
1654 or %temp1, %lo(symbol), %reg */
1655 if (temp)
1656 temp1 = temp; /* op0 is allowed. */
1657 else
1658 temp1 = gen_reg_rtx (DImode);
1660 emit_insn (gen_rtx_SET (VOIDmode, temp1, gen_rtx_HIGH (DImode, op1)));
1661 emit_insn (gen_rtx_SET (VOIDmode, op0, gen_rtx_LO_SUM (DImode, temp1, op1)));
1662 break;
1664 case CM_MEDMID:
1665 /* The range spanned by all instructions in the object is less
1666 than 2^31 bytes (2GB) and the distance from any instruction
1667 to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1668 than 2^31 bytes (2GB).
1670 The executable must be in the low 16TB of the virtual address
1671 space.
1673 sethi %h44(symbol), %temp1
1674 or %temp1, %m44(symbol), %temp2
1675 sllx %temp2, 12, %temp3
1676 or %temp3, %l44(symbol), %reg */
1677 if (temp)
1679 temp1 = op0;
1680 temp2 = op0;
1681 temp3 = temp; /* op0 is allowed. */
1683 else
1685 temp1 = gen_reg_rtx (DImode);
1686 temp2 = gen_reg_rtx (DImode);
1687 temp3 = gen_reg_rtx (DImode);
1690 emit_insn (gen_seth44 (temp1, op1));
1691 emit_insn (gen_setm44 (temp2, temp1, op1));
1692 emit_insn (gen_rtx_SET (VOIDmode, temp3,
1693 gen_rtx_ASHIFT (DImode, temp2, GEN_INT (12))));
1694 emit_insn (gen_setl44 (op0, temp3, op1));
1695 break;
1697 case CM_MEDANY:
1698 /* The range spanned by all instructions in the object is less
1699 than 2^31 bytes (2GB) and the distance from any instruction
1700 to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1701 than 2^31 bytes (2GB).
1703 The executable can be placed anywhere in the virtual address
1704 space.
1706 sethi %hh(symbol), %temp1
1707 sethi %lm(symbol), %temp2
1708 or %temp1, %hm(symbol), %temp3
1709 sllx %temp3, 32, %temp4
1710 or %temp4, %temp2, %temp5
1711 or %temp5, %lo(symbol), %reg */
1712 if (temp)
1714 /* It is possible that one of the registers we got for operands[2]
1715 might coincide with that of operands[0] (which is why we made
1716 it TImode). Pick the other one to use as our scratch. */
1717 if (rtx_equal_p (temp, op0))
1719 if (ti_temp)
1720 temp = gen_rtx_REG (DImode, REGNO (temp) + 1);
1721 else
1722 abort();
1724 temp1 = op0;
1725 temp2 = temp; /* op0 is _not_ allowed, see above. */
1726 temp3 = op0;
1727 temp4 = op0;
1728 temp5 = op0;
1730 else
1732 temp1 = gen_reg_rtx (DImode);
1733 temp2 = gen_reg_rtx (DImode);
1734 temp3 = gen_reg_rtx (DImode);
1735 temp4 = gen_reg_rtx (DImode);
1736 temp5 = gen_reg_rtx (DImode);
1739 emit_insn (gen_sethh (temp1, op1));
1740 emit_insn (gen_setlm (temp2, op1));
1741 emit_insn (gen_sethm (temp3, temp1, op1));
1742 emit_insn (gen_rtx_SET (VOIDmode, temp4,
1743 gen_rtx_ASHIFT (DImode, temp3, GEN_INT (32))));
1744 emit_insn (gen_rtx_SET (VOIDmode, temp5,
1745 gen_rtx_PLUS (DImode, temp4, temp2)));
1746 emit_insn (gen_setlo (op0, temp5, op1));
1747 break;
1749 case CM_EMBMEDANY:
1750 /* Old old old backwards compatibility kruft here.
1751 Essentially it is MEDLOW with a fixed 64-bit
1752 virtual base added to all data segment addresses.
1753 Text-segment stuff is computed like MEDANY, we can't
1754 reuse the code above because the relocation knobs
1755 look different.
1757 Data segment: sethi %hi(symbol), %temp1
1758 add %temp1, EMBMEDANY_BASE_REG, %temp2
1759 or %temp2, %lo(symbol), %reg */
1760 if (data_segment_operand (op1, GET_MODE (op1)))
1762 if (temp)
1764 temp1 = temp; /* op0 is allowed. */
1765 temp2 = op0;
1767 else
1769 temp1 = gen_reg_rtx (DImode);
1770 temp2 = gen_reg_rtx (DImode);
1773 emit_insn (gen_embmedany_sethi (temp1, op1));
1774 emit_insn (gen_embmedany_brsum (temp2, temp1));
1775 emit_insn (gen_embmedany_losum (op0, temp2, op1));
1778 /* Text segment: sethi %uhi(symbol), %temp1
1779 sethi %hi(symbol), %temp2
1780 or %temp1, %ulo(symbol), %temp3
1781 sllx %temp3, 32, %temp4
1782 or %temp4, %temp2, %temp5
1783 or %temp5, %lo(symbol), %reg */
1784 else
1786 if (temp)
1788 /* It is possible that one of the registers we got for operands[2]
1789 might coincide with that of operands[0] (which is why we made
1790 it TImode). Pick the other one to use as our scratch. */
1791 if (rtx_equal_p (temp, op0))
1793 if (ti_temp)
1794 temp = gen_rtx_REG (DImode, REGNO (temp) + 1);
1795 else
1796 abort();
1798 temp1 = op0;
1799 temp2 = temp; /* op0 is _not_ allowed, see above. */
1800 temp3 = op0;
1801 temp4 = op0;
1802 temp5 = op0;
1804 else
1806 temp1 = gen_reg_rtx (DImode);
1807 temp2 = gen_reg_rtx (DImode);
1808 temp3 = gen_reg_rtx (DImode);
1809 temp4 = gen_reg_rtx (DImode);
1810 temp5 = gen_reg_rtx (DImode);
1813 emit_insn (gen_embmedany_textuhi (temp1, op1));
1814 emit_insn (gen_embmedany_texthi (temp2, op1));
1815 emit_insn (gen_embmedany_textulo (temp3, temp1, op1));
1816 emit_insn (gen_rtx_SET (VOIDmode, temp4,
1817 gen_rtx_ASHIFT (DImode, temp3, GEN_INT (32))));
1818 emit_insn (gen_rtx_SET (VOIDmode, temp5,
1819 gen_rtx_PLUS (DImode, temp4, temp2)));
1820 emit_insn (gen_embmedany_textlo (op0, temp5, op1));
1822 break;
1824 default:
1825 abort();
1829 /* These avoid problems when cross compiling. If we do not
1830 go through all this hair then the optimizer will see
1831 invalid REG_EQUAL notes or in some cases none at all. */
1832 static void sparc_emit_set_safe_HIGH64 (rtx, HOST_WIDE_INT);
1833 static rtx gen_safe_SET64 (rtx, HOST_WIDE_INT);
1834 static rtx gen_safe_OR64 (rtx, HOST_WIDE_INT);
1835 static rtx gen_safe_XOR64 (rtx, HOST_WIDE_INT);
1837 #if HOST_BITS_PER_WIDE_INT == 64
1838 #define GEN_HIGHINT64(__x) GEN_INT ((__x) & ~(HOST_WIDE_INT)0x3ff)
1839 #define GEN_INT64(__x) GEN_INT (__x)
1840 #else
1841 #define GEN_HIGHINT64(__x) \
1842 immed_double_const ((__x) & ~(HOST_WIDE_INT)0x3ff, 0, DImode)
1843 #define GEN_INT64(__x) \
1844 immed_double_const ((__x) & 0xffffffff, \
1845 ((__x) & 0x80000000 ? -1 : 0), DImode)
1846 #endif
1848 /* The optimizer is not to assume anything about exactly
1849 which bits are set for a HIGH, they are unspecified.
1850 Unfortunately this leads to many missed optimizations
1851 during CSE. We mask out the non-HIGH bits, and matches
1852 a plain movdi, to alleviate this problem. */
1853 static void
1854 sparc_emit_set_safe_HIGH64 (rtx dest, HOST_WIDE_INT val)
1856 emit_insn (gen_rtx_SET (VOIDmode, dest, GEN_HIGHINT64 (val)));
1859 static rtx
1860 gen_safe_SET64 (rtx dest, HOST_WIDE_INT val)
1862 return gen_rtx_SET (VOIDmode, dest, GEN_INT64 (val));
1865 static rtx
1866 gen_safe_OR64 (rtx src, HOST_WIDE_INT val)
1868 return gen_rtx_IOR (DImode, src, GEN_INT64 (val));
1871 static rtx
1872 gen_safe_XOR64 (rtx src, HOST_WIDE_INT val)
1874 return gen_rtx_XOR (DImode, src, GEN_INT64 (val));
1877 /* Worker routines for 64-bit constant formation on arch64.
1878 One of the key things to be doing in these emissions is
1879 to create as many temp REGs as possible. This makes it
1880 possible for half-built constants to be used later when
1881 such values are similar to something required later on.
1882 Without doing this, the optimizer cannot see such
1883 opportunities. */
1885 static void sparc_emit_set_const64_quick1 (rtx, rtx,
1886 unsigned HOST_WIDE_INT, int);
1888 static void
1889 sparc_emit_set_const64_quick1 (rtx op0, rtx temp,
1890 unsigned HOST_WIDE_INT low_bits, int is_neg)
1892 unsigned HOST_WIDE_INT high_bits;
1894 if (is_neg)
1895 high_bits = (~low_bits) & 0xffffffff;
1896 else
1897 high_bits = low_bits;
1899 sparc_emit_set_safe_HIGH64 (temp, high_bits);
1900 if (!is_neg)
1902 emit_insn (gen_rtx_SET (VOIDmode, op0,
1903 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
1905 else
1907 /* If we are XOR'ing with -1, then we should emit a one's complement
1908 instead. This way the combiner will notice logical operations
1909 such as ANDN later on and substitute. */
1910 if ((low_bits & 0x3ff) == 0x3ff)
1912 emit_insn (gen_rtx_SET (VOIDmode, op0,
1913 gen_rtx_NOT (DImode, temp)));
1915 else
1917 emit_insn (gen_rtx_SET (VOIDmode, op0,
1918 gen_safe_XOR64 (temp,
1919 (-(HOST_WIDE_INT)0x400
1920 | (low_bits & 0x3ff)))));
1925 static void sparc_emit_set_const64_quick2 (rtx, rtx, unsigned HOST_WIDE_INT,
1926 unsigned HOST_WIDE_INT, int);
1928 static void
1929 sparc_emit_set_const64_quick2 (rtx op0, rtx temp,
1930 unsigned HOST_WIDE_INT high_bits,
1931 unsigned HOST_WIDE_INT low_immediate,
1932 int shift_count)
1934 rtx temp2 = op0;
1936 if ((high_bits & 0xfffffc00) != 0)
1938 sparc_emit_set_safe_HIGH64 (temp, high_bits);
1939 if ((high_bits & ~0xfffffc00) != 0)
1940 emit_insn (gen_rtx_SET (VOIDmode, op0,
1941 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
1942 else
1943 temp2 = temp;
1945 else
1947 emit_insn (gen_safe_SET64 (temp, high_bits));
1948 temp2 = temp;
1951 /* Now shift it up into place. */
1952 emit_insn (gen_rtx_SET (VOIDmode, op0,
1953 gen_rtx_ASHIFT (DImode, temp2,
1954 GEN_INT (shift_count))));
1956 /* If there is a low immediate part piece, finish up by
1957 putting that in as well. */
1958 if (low_immediate != 0)
1959 emit_insn (gen_rtx_SET (VOIDmode, op0,
1960 gen_safe_OR64 (op0, low_immediate)));
1963 static void sparc_emit_set_const64_longway (rtx, rtx, unsigned HOST_WIDE_INT,
1964 unsigned HOST_WIDE_INT);
1966 /* Full 64-bit constant decomposition. Even though this is the
1967 'worst' case, we still optimize a few things away. */
1968 static void
1969 sparc_emit_set_const64_longway (rtx op0, rtx temp,
1970 unsigned HOST_WIDE_INT high_bits,
1971 unsigned HOST_WIDE_INT low_bits)
1973 rtx sub_temp;
1975 if (reload_in_progress || reload_completed)
1976 sub_temp = op0;
1977 else
1978 sub_temp = gen_reg_rtx (DImode);
1980 if ((high_bits & 0xfffffc00) != 0)
1982 sparc_emit_set_safe_HIGH64 (temp, high_bits);
1983 if ((high_bits & ~0xfffffc00) != 0)
1984 emit_insn (gen_rtx_SET (VOIDmode,
1985 sub_temp,
1986 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
1987 else
1988 sub_temp = temp;
1990 else
1992 emit_insn (gen_safe_SET64 (temp, high_bits));
1993 sub_temp = temp;
1996 if (!reload_in_progress && !reload_completed)
1998 rtx temp2 = gen_reg_rtx (DImode);
1999 rtx temp3 = gen_reg_rtx (DImode);
2000 rtx temp4 = gen_reg_rtx (DImode);
2002 emit_insn (gen_rtx_SET (VOIDmode, temp4,
2003 gen_rtx_ASHIFT (DImode, sub_temp,
2004 GEN_INT (32))));
2006 sparc_emit_set_safe_HIGH64 (temp2, low_bits);
2007 if ((low_bits & ~0xfffffc00) != 0)
2009 emit_insn (gen_rtx_SET (VOIDmode, temp3,
2010 gen_safe_OR64 (temp2, (low_bits & 0x3ff))));
2011 emit_insn (gen_rtx_SET (VOIDmode, op0,
2012 gen_rtx_PLUS (DImode, temp4, temp3)));
2014 else
2016 emit_insn (gen_rtx_SET (VOIDmode, op0,
2017 gen_rtx_PLUS (DImode, temp4, temp2)));
2020 else
2022 rtx low1 = GEN_INT ((low_bits >> (32 - 12)) & 0xfff);
2023 rtx low2 = GEN_INT ((low_bits >> (32 - 12 - 12)) & 0xfff);
2024 rtx low3 = GEN_INT ((low_bits >> (32 - 12 - 12 - 8)) & 0x0ff);
2025 int to_shift = 12;
2027 /* We are in the middle of reload, so this is really
2028 painful. However we do still make an attempt to
2029 avoid emitting truly stupid code. */
2030 if (low1 != const0_rtx)
2032 emit_insn (gen_rtx_SET (VOIDmode, op0,
2033 gen_rtx_ASHIFT (DImode, sub_temp,
2034 GEN_INT (to_shift))));
2035 emit_insn (gen_rtx_SET (VOIDmode, op0,
2036 gen_rtx_IOR (DImode, op0, low1)));
2037 sub_temp = op0;
2038 to_shift = 12;
2040 else
2042 to_shift += 12;
2044 if (low2 != const0_rtx)
2046 emit_insn (gen_rtx_SET (VOIDmode, op0,
2047 gen_rtx_ASHIFT (DImode, sub_temp,
2048 GEN_INT (to_shift))));
2049 emit_insn (gen_rtx_SET (VOIDmode, op0,
2050 gen_rtx_IOR (DImode, op0, low2)));
2051 sub_temp = op0;
2052 to_shift = 8;
2054 else
2056 to_shift += 8;
2058 emit_insn (gen_rtx_SET (VOIDmode, op0,
2059 gen_rtx_ASHIFT (DImode, sub_temp,
2060 GEN_INT (to_shift))));
2061 if (low3 != const0_rtx)
2062 emit_insn (gen_rtx_SET (VOIDmode, op0,
2063 gen_rtx_IOR (DImode, op0, low3)));
2064 /* phew... */
2068 /* Analyze a 64-bit constant for certain properties. */
2069 static void analyze_64bit_constant (unsigned HOST_WIDE_INT,
2070 unsigned HOST_WIDE_INT,
2071 int *, int *, int *);
2073 static void
2074 analyze_64bit_constant (unsigned HOST_WIDE_INT high_bits,
2075 unsigned HOST_WIDE_INT low_bits,
2076 int *hbsp, int *lbsp, int *abbasp)
2078 int lowest_bit_set, highest_bit_set, all_bits_between_are_set;
2079 int i;
2081 lowest_bit_set = highest_bit_set = -1;
2082 i = 0;
2085 if ((lowest_bit_set == -1)
2086 && ((low_bits >> i) & 1))
2087 lowest_bit_set = i;
2088 if ((highest_bit_set == -1)
2089 && ((high_bits >> (32 - i - 1)) & 1))
2090 highest_bit_set = (64 - i - 1);
2092 while (++i < 32
2093 && ((highest_bit_set == -1)
2094 || (lowest_bit_set == -1)));
2095 if (i == 32)
2097 i = 0;
2100 if ((lowest_bit_set == -1)
2101 && ((high_bits >> i) & 1))
2102 lowest_bit_set = i + 32;
2103 if ((highest_bit_set == -1)
2104 && ((low_bits >> (32 - i - 1)) & 1))
2105 highest_bit_set = 32 - i - 1;
2107 while (++i < 32
2108 && ((highest_bit_set == -1)
2109 || (lowest_bit_set == -1)));
2111 /* If there are no bits set this should have gone out
2112 as one instruction! */
2113 if (lowest_bit_set == -1
2114 || highest_bit_set == -1)
2115 abort ();
2116 all_bits_between_are_set = 1;
2117 for (i = lowest_bit_set; i <= highest_bit_set; i++)
2119 if (i < 32)
2121 if ((low_bits & (1 << i)) != 0)
2122 continue;
2124 else
2126 if ((high_bits & (1 << (i - 32))) != 0)
2127 continue;
2129 all_bits_between_are_set = 0;
2130 break;
2132 *hbsp = highest_bit_set;
2133 *lbsp = lowest_bit_set;
2134 *abbasp = all_bits_between_are_set;
2137 static int const64_is_2insns (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2139 static int
2140 const64_is_2insns (unsigned HOST_WIDE_INT high_bits,
2141 unsigned HOST_WIDE_INT low_bits)
2143 int highest_bit_set, lowest_bit_set, all_bits_between_are_set;
2145 if (high_bits == 0
2146 || high_bits == 0xffffffff)
2147 return 1;
2149 analyze_64bit_constant (high_bits, low_bits,
2150 &highest_bit_set, &lowest_bit_set,
2151 &all_bits_between_are_set);
2153 if ((highest_bit_set == 63
2154 || lowest_bit_set == 0)
2155 && all_bits_between_are_set != 0)
2156 return 1;
2158 if ((highest_bit_set - lowest_bit_set) < 21)
2159 return 1;
2161 return 0;
2164 static unsigned HOST_WIDE_INT create_simple_focus_bits (unsigned HOST_WIDE_INT,
2165 unsigned HOST_WIDE_INT,
2166 int, int);
2168 static unsigned HOST_WIDE_INT
2169 create_simple_focus_bits (unsigned HOST_WIDE_INT high_bits,
2170 unsigned HOST_WIDE_INT low_bits,
2171 int lowest_bit_set, int shift)
2173 HOST_WIDE_INT hi, lo;
2175 if (lowest_bit_set < 32)
2177 lo = (low_bits >> lowest_bit_set) << shift;
2178 hi = ((high_bits << (32 - lowest_bit_set)) << shift);
2180 else
2182 lo = 0;
2183 hi = ((high_bits >> (lowest_bit_set - 32)) << shift);
2185 if (hi & lo)
2186 abort ();
2187 return (hi | lo);
2190 /* Here we are sure to be arch64 and this is an integer constant
2191 being loaded into a register. Emit the most efficient
2192 insn sequence possible. Detection of all the 1-insn cases
2193 has been done already. */
2194 void
2195 sparc_emit_set_const64 (rtx op0, rtx op1)
2197 unsigned HOST_WIDE_INT high_bits, low_bits;
2198 int lowest_bit_set, highest_bit_set;
2199 int all_bits_between_are_set;
2200 rtx temp = 0;
2202 /* Sanity check that we know what we are working with. */
2203 if (! TARGET_ARCH64)
2204 abort ();
2206 if (GET_CODE (op0) != SUBREG)
2208 if (GET_CODE (op0) != REG
2209 || (REGNO (op0) >= SPARC_FIRST_FP_REG
2210 && REGNO (op0) <= SPARC_LAST_V9_FP_REG))
2211 abort ();
2214 if (reload_in_progress || reload_completed)
2215 temp = op0;
2217 if (GET_CODE (op1) != CONST_DOUBLE
2218 && GET_CODE (op1) != CONST_INT)
2220 sparc_emit_set_symbolic_const64 (op0, op1, temp);
2221 return;
2224 if (! temp)
2225 temp = gen_reg_rtx (DImode);
2227 if (GET_CODE (op1) == CONST_DOUBLE)
2229 #if HOST_BITS_PER_WIDE_INT == 64
2230 high_bits = (CONST_DOUBLE_LOW (op1) >> 32) & 0xffffffff;
2231 low_bits = CONST_DOUBLE_LOW (op1) & 0xffffffff;
2232 #else
2233 high_bits = CONST_DOUBLE_HIGH (op1);
2234 low_bits = CONST_DOUBLE_LOW (op1);
2235 #endif
2237 else
2239 #if HOST_BITS_PER_WIDE_INT == 64
2240 high_bits = ((INTVAL (op1) >> 32) & 0xffffffff);
2241 low_bits = (INTVAL (op1) & 0xffffffff);
2242 #else
2243 high_bits = ((INTVAL (op1) < 0) ?
2244 0xffffffff :
2245 0x00000000);
2246 low_bits = INTVAL (op1);
2247 #endif
2250 /* low_bits bits 0 --> 31
2251 high_bits bits 32 --> 63 */
2253 analyze_64bit_constant (high_bits, low_bits,
2254 &highest_bit_set, &lowest_bit_set,
2255 &all_bits_between_are_set);
2257 /* First try for a 2-insn sequence. */
2259 /* These situations are preferred because the optimizer can
2260 * do more things with them:
2261 * 1) mov -1, %reg
2262 * sllx %reg, shift, %reg
2263 * 2) mov -1, %reg
2264 * srlx %reg, shift, %reg
2265 * 3) mov some_small_const, %reg
2266 * sllx %reg, shift, %reg
2268 if (((highest_bit_set == 63
2269 || lowest_bit_set == 0)
2270 && all_bits_between_are_set != 0)
2271 || ((highest_bit_set - lowest_bit_set) < 12))
2273 HOST_WIDE_INT the_const = -1;
2274 int shift = lowest_bit_set;
2276 if ((highest_bit_set != 63
2277 && lowest_bit_set != 0)
2278 || all_bits_between_are_set == 0)
2280 the_const =
2281 create_simple_focus_bits (high_bits, low_bits,
2282 lowest_bit_set, 0);
2284 else if (lowest_bit_set == 0)
2285 shift = -(63 - highest_bit_set);
2287 if (! SPARC_SIMM13_P (the_const))
2288 abort ();
2290 emit_insn (gen_safe_SET64 (temp, the_const));
2291 if (shift > 0)
2292 emit_insn (gen_rtx_SET (VOIDmode,
2293 op0,
2294 gen_rtx_ASHIFT (DImode,
2295 temp,
2296 GEN_INT (shift))));
2297 else if (shift < 0)
2298 emit_insn (gen_rtx_SET (VOIDmode,
2299 op0,
2300 gen_rtx_LSHIFTRT (DImode,
2301 temp,
2302 GEN_INT (-shift))));
2303 else
2304 abort ();
2305 return;
2308 /* Now a range of 22 or less bits set somewhere.
2309 * 1) sethi %hi(focus_bits), %reg
2310 * sllx %reg, shift, %reg
2311 * 2) sethi %hi(focus_bits), %reg
2312 * srlx %reg, shift, %reg
2314 if ((highest_bit_set - lowest_bit_set) < 21)
2316 unsigned HOST_WIDE_INT focus_bits =
2317 create_simple_focus_bits (high_bits, low_bits,
2318 lowest_bit_set, 10);
2320 if (! SPARC_SETHI_P (focus_bits))
2321 abort ();
2323 sparc_emit_set_safe_HIGH64 (temp, focus_bits);
2325 /* If lowest_bit_set == 10 then a sethi alone could have done it. */
2326 if (lowest_bit_set < 10)
2327 emit_insn (gen_rtx_SET (VOIDmode,
2328 op0,
2329 gen_rtx_LSHIFTRT (DImode, temp,
2330 GEN_INT (10 - lowest_bit_set))));
2331 else if (lowest_bit_set > 10)
2332 emit_insn (gen_rtx_SET (VOIDmode,
2333 op0,
2334 gen_rtx_ASHIFT (DImode, temp,
2335 GEN_INT (lowest_bit_set - 10))));
2336 else
2337 abort ();
2338 return;
2341 /* 1) sethi %hi(low_bits), %reg
2342 * or %reg, %lo(low_bits), %reg
2343 * 2) sethi %hi(~low_bits), %reg
2344 * xor %reg, %lo(-0x400 | (low_bits & 0x3ff)), %reg
2346 if (high_bits == 0
2347 || high_bits == 0xffffffff)
2349 sparc_emit_set_const64_quick1 (op0, temp, low_bits,
2350 (high_bits == 0xffffffff));
2351 return;
2354 /* Now, try 3-insn sequences. */
2356 /* 1) sethi %hi(high_bits), %reg
2357 * or %reg, %lo(high_bits), %reg
2358 * sllx %reg, 32, %reg
2360 if (low_bits == 0)
2362 sparc_emit_set_const64_quick2 (op0, temp, high_bits, 0, 32);
2363 return;
2366 /* We may be able to do something quick
2367 when the constant is negated, so try that. */
2368 if (const64_is_2insns ((~high_bits) & 0xffffffff,
2369 (~low_bits) & 0xfffffc00))
2371 /* NOTE: The trailing bits get XOR'd so we need the
2372 non-negated bits, not the negated ones. */
2373 unsigned HOST_WIDE_INT trailing_bits = low_bits & 0x3ff;
2375 if ((((~high_bits) & 0xffffffff) == 0
2376 && ((~low_bits) & 0x80000000) == 0)
2377 || (((~high_bits) & 0xffffffff) == 0xffffffff
2378 && ((~low_bits) & 0x80000000) != 0))
2380 int fast_int = (~low_bits & 0xffffffff);
2382 if ((SPARC_SETHI_P (fast_int)
2383 && (~high_bits & 0xffffffff) == 0)
2384 || SPARC_SIMM13_P (fast_int))
2385 emit_insn (gen_safe_SET64 (temp, fast_int));
2386 else
2387 sparc_emit_set_const64 (temp, GEN_INT64 (fast_int));
2389 else
2391 rtx negated_const;
2392 #if HOST_BITS_PER_WIDE_INT == 64
2393 negated_const = GEN_INT (((~low_bits) & 0xfffffc00) |
2394 (((HOST_WIDE_INT)((~high_bits) & 0xffffffff))<<32));
2395 #else
2396 negated_const = immed_double_const ((~low_bits) & 0xfffffc00,
2397 (~high_bits) & 0xffffffff,
2398 DImode);
2399 #endif
2400 sparc_emit_set_const64 (temp, negated_const);
2403 /* If we are XOR'ing with -1, then we should emit a one's complement
2404 instead. This way the combiner will notice logical operations
2405 such as ANDN later on and substitute. */
2406 if (trailing_bits == 0x3ff)
2408 emit_insn (gen_rtx_SET (VOIDmode, op0,
2409 gen_rtx_NOT (DImode, temp)));
2411 else
2413 emit_insn (gen_rtx_SET (VOIDmode,
2414 op0,
2415 gen_safe_XOR64 (temp,
2416 (-0x400 | trailing_bits))));
2418 return;
2421 /* 1) sethi %hi(xxx), %reg
2422 * or %reg, %lo(xxx), %reg
2423 * sllx %reg, yyy, %reg
2425 * ??? This is just a generalized version of the low_bits==0
2426 * thing above, FIXME...
2428 if ((highest_bit_set - lowest_bit_set) < 32)
2430 unsigned HOST_WIDE_INT focus_bits =
2431 create_simple_focus_bits (high_bits, low_bits,
2432 lowest_bit_set, 0);
2434 /* We can't get here in this state. */
2435 if (highest_bit_set < 32
2436 || lowest_bit_set >= 32)
2437 abort ();
2439 /* So what we know is that the set bits straddle the
2440 middle of the 64-bit word. */
2441 sparc_emit_set_const64_quick2 (op0, temp,
2442 focus_bits, 0,
2443 lowest_bit_set);
2444 return;
2447 /* 1) sethi %hi(high_bits), %reg
2448 * or %reg, %lo(high_bits), %reg
2449 * sllx %reg, 32, %reg
2450 * or %reg, low_bits, %reg
2452 if (SPARC_SIMM13_P(low_bits)
2453 && ((int)low_bits > 0))
2455 sparc_emit_set_const64_quick2 (op0, temp, high_bits, low_bits, 32);
2456 return;
2459 /* The easiest way when all else fails, is full decomposition. */
2460 #if 0
2461 printf ("sparc_emit_set_const64: Hard constant [%08lx%08lx] neg[%08lx%08lx]\n",
2462 high_bits, low_bits, ~high_bits, ~low_bits);
2463 #endif
2464 sparc_emit_set_const64_longway (op0, temp, high_bits, low_bits);
2467 /* Given a comparison code (EQ, NE, etc.) and the first operand of a COMPARE,
2468 return the mode to be used for the comparison. For floating-point,
2469 CCFP[E]mode is used. CC_NOOVmode should be used when the first operand
2470 is a PLUS, MINUS, NEG, or ASHIFT. CCmode should be used when no special
2471 processing is needed. */
2473 enum machine_mode
2474 select_cc_mode (enum rtx_code op, rtx x, rtx y ATTRIBUTE_UNUSED)
2476 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2478 switch (op)
2480 case EQ:
2481 case NE:
2482 case UNORDERED:
2483 case ORDERED:
2484 case UNLT:
2485 case UNLE:
2486 case UNGT:
2487 case UNGE:
2488 case UNEQ:
2489 case LTGT:
2490 return CCFPmode;
2492 case LT:
2493 case LE:
2494 case GT:
2495 case GE:
2496 return CCFPEmode;
2498 default:
2499 abort ();
2502 else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
2503 || GET_CODE (x) == NEG || GET_CODE (x) == ASHIFT)
2505 if (TARGET_ARCH64 && GET_MODE (x) == DImode)
2506 return CCX_NOOVmode;
2507 else
2508 return CC_NOOVmode;
2510 else
2512 if (TARGET_ARCH64 && GET_MODE (x) == DImode)
2513 return CCXmode;
2514 else
2515 return CCmode;
2519 /* X and Y are two things to compare using CODE. Emit the compare insn and
2520 return the rtx for the cc reg in the proper mode. */
2523 gen_compare_reg (enum rtx_code code, rtx x, rtx y)
2525 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
2526 rtx cc_reg;
2528 /* ??? We don't have movcc patterns so we cannot generate pseudo regs for the
2529 fcc regs (cse can't tell they're really call clobbered regs and will
2530 remove a duplicate comparison even if there is an intervening function
2531 call - it will then try to reload the cc reg via an int reg which is why
2532 we need the movcc patterns). It is possible to provide the movcc
2533 patterns by using the ldxfsr/stxfsr v9 insns. I tried it: you need two
2534 registers (say %g1,%g5) and it takes about 6 insns. A better fix would be
2535 to tell cse that CCFPE mode registers (even pseudos) are call
2536 clobbered. */
2538 /* ??? This is an experiment. Rather than making changes to cse which may
2539 or may not be easy/clean, we do our own cse. This is possible because
2540 we will generate hard registers. Cse knows they're call clobbered (it
2541 doesn't know the same thing about pseudos). If we guess wrong, no big
2542 deal, but if we win, great! */
2544 if (TARGET_V9 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2545 #if 1 /* experiment */
2547 int reg;
2548 /* We cycle through the registers to ensure they're all exercised. */
2549 static int next_fcc_reg = 0;
2550 /* Previous x,y for each fcc reg. */
2551 static rtx prev_args[4][2];
2553 /* Scan prev_args for x,y. */
2554 for (reg = 0; reg < 4; reg++)
2555 if (prev_args[reg][0] == x && prev_args[reg][1] == y)
2556 break;
2557 if (reg == 4)
2559 reg = next_fcc_reg;
2560 prev_args[reg][0] = x;
2561 prev_args[reg][1] = y;
2562 next_fcc_reg = (next_fcc_reg + 1) & 3;
2564 cc_reg = gen_rtx_REG (mode, reg + SPARC_FIRST_V9_FCC_REG);
2566 #else
2567 cc_reg = gen_reg_rtx (mode);
2568 #endif /* ! experiment */
2569 else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2570 cc_reg = gen_rtx_REG (mode, SPARC_FCC_REG);
2571 else
2572 cc_reg = gen_rtx_REG (mode, SPARC_ICC_REG);
2574 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
2575 gen_rtx_COMPARE (mode, x, y)));
2577 return cc_reg;
2580 /* This function is used for v9 only.
2581 CODE is the code for an Scc's comparison.
2582 OPERANDS[0] is the target of the Scc insn.
2583 OPERANDS[1] is the value we compare against const0_rtx (which hasn't
2584 been generated yet).
2586 This function is needed to turn
2588 (set (reg:SI 110)
2589 (gt (reg:CCX 100 %icc)
2590 (const_int 0)))
2591 into
2592 (set (reg:SI 110)
2593 (gt:DI (reg:CCX 100 %icc)
2594 (const_int 0)))
2596 IE: The instruction recognizer needs to see the mode of the comparison to
2597 find the right instruction. We could use "gt:DI" right in the
2598 define_expand, but leaving it out allows us to handle DI, SI, etc.
2600 We refer to the global sparc compare operands sparc_compare_op0 and
2601 sparc_compare_op1. */
2604 gen_v9_scc (enum rtx_code compare_code, register rtx *operands)
2606 rtx temp, op0, op1;
2608 if (! TARGET_ARCH64
2609 && (GET_MODE (sparc_compare_op0) == DImode
2610 || GET_MODE (operands[0]) == DImode))
2611 return 0;
2613 op0 = sparc_compare_op0;
2614 op1 = sparc_compare_op1;
2616 /* Try to use the movrCC insns. */
2617 if (TARGET_ARCH64
2618 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
2619 && op1 == const0_rtx
2620 && v9_regcmp_p (compare_code))
2622 /* Special case for op0 != 0. This can be done with one instruction if
2623 operands[0] == sparc_compare_op0. */
2625 if (compare_code == NE
2626 && GET_MODE (operands[0]) == DImode
2627 && rtx_equal_p (op0, operands[0]))
2629 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
2630 gen_rtx_IF_THEN_ELSE (DImode,
2631 gen_rtx_fmt_ee (compare_code, DImode,
2632 op0, const0_rtx),
2633 const1_rtx,
2634 operands[0])));
2635 return 1;
2638 if (reg_overlap_mentioned_p (operands[0], op0))
2640 /* Handle the case where operands[0] == sparc_compare_op0.
2641 We "early clobber" the result. */
2642 op0 = gen_reg_rtx (GET_MODE (sparc_compare_op0));
2643 emit_move_insn (op0, sparc_compare_op0);
2646 emit_insn (gen_rtx_SET (VOIDmode, operands[0], const0_rtx));
2647 if (GET_MODE (op0) != DImode)
2649 temp = gen_reg_rtx (DImode);
2650 convert_move (temp, op0, 0);
2652 else
2653 temp = op0;
2654 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
2655 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
2656 gen_rtx_fmt_ee (compare_code, DImode,
2657 temp, const0_rtx),
2658 const1_rtx,
2659 operands[0])));
2660 return 1;
2662 else
2664 operands[1] = gen_compare_reg (compare_code, op0, op1);
2666 switch (GET_MODE (operands[1]))
2668 case CCmode :
2669 case CCXmode :
2670 case CCFPEmode :
2671 case CCFPmode :
2672 break;
2673 default :
2674 abort ();
2676 emit_insn (gen_rtx_SET (VOIDmode, operands[0], const0_rtx));
2677 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
2678 gen_rtx_IF_THEN_ELSE (GET_MODE (operands[0]),
2679 gen_rtx_fmt_ee (compare_code,
2680 GET_MODE (operands[1]),
2681 operands[1], const0_rtx),
2682 const1_rtx, operands[0])));
2683 return 1;
2687 /* Emit a conditional jump insn for the v9 architecture using comparison code
2688 CODE and jump target LABEL.
2689 This function exists to take advantage of the v9 brxx insns. */
2691 void
2692 emit_v9_brxx_insn (enum rtx_code code, rtx op0, rtx label)
2694 emit_jump_insn (gen_rtx_SET (VOIDmode,
2695 pc_rtx,
2696 gen_rtx_IF_THEN_ELSE (VOIDmode,
2697 gen_rtx_fmt_ee (code, GET_MODE (op0),
2698 op0, const0_rtx),
2699 gen_rtx_LABEL_REF (VOIDmode, label),
2700 pc_rtx)));
2703 /* Generate a DFmode part of a hard TFmode register.
2704 REG is the TFmode hard register, LOW is 1 for the
2705 low 64bit of the register and 0 otherwise.
2708 gen_df_reg (rtx reg, int low)
2710 int regno = REGNO (reg);
2712 if ((WORDS_BIG_ENDIAN == 0) ^ (low != 0))
2713 regno += (TARGET_ARCH64 && regno < 32) ? 1 : 2;
2714 return gen_rtx_REG (DFmode, regno);
2717 /* Generate a call to FUNC with OPERANDS. Operand 0 is the return value.
2718 Unlike normal calls, TFmode operands are passed by reference. It is
2719 assumed that no more than 3 operands are required. */
2721 static void
2722 emit_soft_tfmode_libcall (const char *func_name, int nargs, rtx *operands)
2724 rtx ret_slot = NULL, arg[3], func_sym;
2725 int i;
2727 /* We only expect to be called for conversions, unary, and binary ops. */
2728 if (nargs < 2 || nargs > 3)
2729 abort ();
2731 for (i = 0; i < nargs; ++i)
2733 rtx this_arg = operands[i];
2734 rtx this_slot;
2736 /* TFmode arguments and return values are passed by reference. */
2737 if (GET_MODE (this_arg) == TFmode)
2739 int force_stack_temp;
2741 force_stack_temp = 0;
2742 if (TARGET_BUGGY_QP_LIB && i == 0)
2743 force_stack_temp = 1;
2745 if (GET_CODE (this_arg) == MEM
2746 && ! force_stack_temp)
2747 this_arg = XEXP (this_arg, 0);
2748 else if (CONSTANT_P (this_arg)
2749 && ! force_stack_temp)
2751 this_slot = force_const_mem (TFmode, this_arg);
2752 this_arg = XEXP (this_slot, 0);
2754 else
2756 this_slot = assign_stack_temp (TFmode, GET_MODE_SIZE (TFmode), 0);
2758 /* Operand 0 is the return value. We'll copy it out later. */
2759 if (i > 0)
2760 emit_move_insn (this_slot, this_arg);
2761 else
2762 ret_slot = this_slot;
2764 this_arg = XEXP (this_slot, 0);
2768 arg[i] = this_arg;
2771 func_sym = gen_rtx_SYMBOL_REF (Pmode, func_name);
2773 if (GET_MODE (operands[0]) == TFmode)
2775 if (nargs == 2)
2776 emit_library_call (func_sym, LCT_NORMAL, VOIDmode, 2,
2777 arg[0], GET_MODE (arg[0]),
2778 arg[1], GET_MODE (arg[1]));
2779 else
2780 emit_library_call (func_sym, LCT_NORMAL, VOIDmode, 3,
2781 arg[0], GET_MODE (arg[0]),
2782 arg[1], GET_MODE (arg[1]),
2783 arg[2], GET_MODE (arg[2]));
2785 if (ret_slot)
2786 emit_move_insn (operands[0], ret_slot);
2788 else
2790 rtx ret;
2792 if (nargs != 2)
2793 abort ();
2795 ret = emit_library_call_value (func_sym, operands[0], LCT_NORMAL,
2796 GET_MODE (operands[0]), 1,
2797 arg[1], GET_MODE (arg[1]));
2799 if (ret != operands[0])
2800 emit_move_insn (operands[0], ret);
2804 /* Expand soft-float TFmode calls to sparc abi routines. */
2806 static void
2807 emit_soft_tfmode_binop (enum rtx_code code, rtx *operands)
2809 const char *func;
2811 switch (code)
2813 case PLUS:
2814 func = "_Qp_add";
2815 break;
2816 case MINUS:
2817 func = "_Qp_sub";
2818 break;
2819 case MULT:
2820 func = "_Qp_mul";
2821 break;
2822 case DIV:
2823 func = "_Qp_div";
2824 break;
2825 default:
2826 abort ();
2829 emit_soft_tfmode_libcall (func, 3, operands);
2832 static void
2833 emit_soft_tfmode_unop (enum rtx_code code, rtx *operands)
2835 const char *func;
2837 switch (code)
2839 case SQRT:
2840 func = "_Qp_sqrt";
2841 break;
2842 default:
2843 abort ();
2846 emit_soft_tfmode_libcall (func, 2, operands);
2849 static void
2850 emit_soft_tfmode_cvt (enum rtx_code code, rtx *operands)
2852 const char *func;
2854 switch (code)
2856 case FLOAT_EXTEND:
2857 switch (GET_MODE (operands[1]))
2859 case SFmode:
2860 func = "_Qp_stoq";
2861 break;
2862 case DFmode:
2863 func = "_Qp_dtoq";
2864 break;
2865 default:
2866 abort ();
2868 break;
2870 case FLOAT_TRUNCATE:
2871 switch (GET_MODE (operands[0]))
2873 case SFmode:
2874 func = "_Qp_qtos";
2875 break;
2876 case DFmode:
2877 func = "_Qp_qtod";
2878 break;
2879 default:
2880 abort ();
2882 break;
2884 case FLOAT:
2885 switch (GET_MODE (operands[1]))
2887 case SImode:
2888 func = "_Qp_itoq";
2889 break;
2890 case DImode:
2891 func = "_Qp_xtoq";
2892 break;
2893 default:
2894 abort ();
2896 break;
2898 case UNSIGNED_FLOAT:
2899 switch (GET_MODE (operands[1]))
2901 case SImode:
2902 func = "_Qp_uitoq";
2903 break;
2904 case DImode:
2905 func = "_Qp_uxtoq";
2906 break;
2907 default:
2908 abort ();
2910 break;
2912 case FIX:
2913 switch (GET_MODE (operands[0]))
2915 case SImode:
2916 func = "_Qp_qtoi";
2917 break;
2918 case DImode:
2919 func = "_Qp_qtox";
2920 break;
2921 default:
2922 abort ();
2924 break;
2926 case UNSIGNED_FIX:
2927 switch (GET_MODE (operands[0]))
2929 case SImode:
2930 func = "_Qp_qtoui";
2931 break;
2932 case DImode:
2933 func = "_Qp_qtoux";
2934 break;
2935 default:
2936 abort ();
2938 break;
2940 default:
2941 abort ();
2944 emit_soft_tfmode_libcall (func, 2, operands);
2947 /* Expand a hard-float tfmode operation. All arguments must be in
2948 registers. */
2950 static void
2951 emit_hard_tfmode_operation (enum rtx_code code, rtx *operands)
2953 rtx op, dest;
2955 if (GET_RTX_CLASS (code) == RTX_UNARY)
2957 operands[1] = force_reg (GET_MODE (operands[1]), operands[1]);
2958 op = gen_rtx_fmt_e (code, GET_MODE (operands[0]), operands[1]);
2960 else
2962 operands[1] = force_reg (GET_MODE (operands[1]), operands[1]);
2963 operands[2] = force_reg (GET_MODE (operands[2]), operands[2]);
2964 op = gen_rtx_fmt_ee (code, GET_MODE (operands[0]),
2965 operands[1], operands[2]);
2968 if (register_operand (operands[0], VOIDmode))
2969 dest = operands[0];
2970 else
2971 dest = gen_reg_rtx (GET_MODE (operands[0]));
2973 emit_insn (gen_rtx_SET (VOIDmode, dest, op));
2975 if (dest != operands[0])
2976 emit_move_insn (operands[0], dest);
2979 void
2980 emit_tfmode_binop (enum rtx_code code, rtx *operands)
2982 if (TARGET_HARD_QUAD)
2983 emit_hard_tfmode_operation (code, operands);
2984 else
2985 emit_soft_tfmode_binop (code, operands);
2988 void
2989 emit_tfmode_unop (enum rtx_code code, rtx *operands)
2991 if (TARGET_HARD_QUAD)
2992 emit_hard_tfmode_operation (code, operands);
2993 else
2994 emit_soft_tfmode_unop (code, operands);
2997 void
2998 emit_tfmode_cvt (enum rtx_code code, rtx *operands)
3000 if (TARGET_HARD_QUAD)
3001 emit_hard_tfmode_operation (code, operands);
3002 else
3003 emit_soft_tfmode_cvt (code, operands);
3006 /* Return nonzero if a branch/jump/call instruction will be emitting
3007 nop into its delay slot. */
3010 empty_delay_slot (rtx insn)
3012 rtx seq;
3014 /* If no previous instruction (should not happen), return true. */
3015 if (PREV_INSN (insn) == NULL)
3016 return 1;
3018 seq = NEXT_INSN (PREV_INSN (insn));
3019 if (GET_CODE (PATTERN (seq)) == SEQUENCE)
3020 return 0;
3022 return 1;
3025 /* Return nonzero if TRIAL can go into the call delay slot. */
3028 tls_call_delay (rtx trial)
3030 rtx pat, unspec;
3032 /* Binutils allows
3033 call __tls_get_addr, %tgd_call (foo)
3034 add %l7, %o0, %o0, %tgd_add (foo)
3035 while Sun as/ld does not. */
3036 if (TARGET_GNU_TLS || !TARGET_TLS)
3037 return 1;
3039 pat = PATTERN (trial);
3040 if (GET_CODE (pat) != SET || GET_CODE (SET_DEST (pat)) != PLUS)
3041 return 1;
3043 unspec = XEXP (SET_DEST (pat), 1);
3044 if (GET_CODE (unspec) != UNSPEC
3045 || (XINT (unspec, 1) != UNSPEC_TLSGD
3046 && XINT (unspec, 1) != UNSPEC_TLSLDM))
3047 return 1;
3049 return 0;
3052 /* Return nonzero if TRIAL, an insn, can be combined with a 'restore'
3053 instruction. RETURN_P is true if the v9 variant 'return' is to be
3054 considered in the test too.
3056 TRIAL must be a SET whose destination is a REG appropriate for the
3057 'restore' instruction or, if RETURN_P is true, for the 'return'
3058 instruction. */
3060 static int
3061 eligible_for_restore_insn (rtx trial, bool return_p)
3063 rtx pat = PATTERN (trial);
3064 rtx src = SET_SRC (pat);
3066 /* The 'restore src,%g0,dest' pattern for word mode and below. */
3067 if (GET_MODE_CLASS (GET_MODE (src)) != MODE_FLOAT
3068 && arith_operand (src, GET_MODE (src)))
3070 if (TARGET_ARCH64)
3071 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
3072 else
3073 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (SImode);
3076 /* The 'restore src,%g0,dest' pattern for double-word mode. */
3077 else if (GET_MODE_CLASS (GET_MODE (src)) != MODE_FLOAT
3078 && arith_double_operand (src, GET_MODE (src)))
3079 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
3081 /* The 'restore src,%g0,dest' pattern for float if no FPU. */
3082 else if (! TARGET_FPU && register_operand (src, SFmode))
3083 return 1;
3085 /* The 'restore src,%g0,dest' pattern for double if no FPU. */
3086 else if (! TARGET_FPU && TARGET_ARCH64 && register_operand (src, DFmode))
3087 return 1;
3089 /* If we have the 'return' instruction, anything that does not use
3090 local or output registers and can go into a delay slot wins. */
3091 else if (return_p && TARGET_V9 && ! epilogue_renumber (&pat, 1)
3092 && (get_attr_in_uncond_branch_delay (trial)
3093 == IN_UNCOND_BRANCH_DELAY_TRUE))
3094 return 1;
3096 /* The 'restore src1,src2,dest' pattern for SImode. */
3097 else if (GET_CODE (src) == PLUS
3098 && register_operand (XEXP (src, 0), SImode)
3099 && arith_operand (XEXP (src, 1), SImode))
3100 return 1;
3102 /* The 'restore src1,src2,dest' pattern for DImode. */
3103 else if (GET_CODE (src) == PLUS
3104 && register_operand (XEXP (src, 0), DImode)
3105 && arith_double_operand (XEXP (src, 1), DImode))
3106 return 1;
3108 /* The 'restore src1,%lo(src2),dest' pattern. */
3109 else if (GET_CODE (src) == LO_SUM
3110 && ! TARGET_CM_MEDMID
3111 && ((register_operand (XEXP (src, 0), SImode)
3112 && immediate_operand (XEXP (src, 1), SImode))
3113 || (TARGET_ARCH64
3114 && register_operand (XEXP (src, 0), DImode)
3115 && immediate_operand (XEXP (src, 1), DImode))))
3116 return 1;
3118 /* The 'restore src,src,dest' pattern. */
3119 else if (GET_CODE (src) == ASHIFT
3120 && (register_operand (XEXP (src, 0), SImode)
3121 || register_operand (XEXP (src, 0), DImode))
3122 && XEXP (src, 1) == const1_rtx)
3123 return 1;
3125 return 0;
3128 /* Return nonzero if TRIAL can go into the function return's
3129 delay slot. */
3132 eligible_for_return_delay (rtx trial)
3134 int leaf_function_p = current_function_uses_only_leaf_regs;
3135 rtx pat;
3137 if (GET_CODE (trial) != INSN || GET_CODE (PATTERN (trial)) != SET)
3138 return 0;
3140 if (get_attr_length (trial) != 1)
3141 return 0;
3143 /* If there are any call-saved registers, we should scan TRIAL if it
3144 does not reference them. For now just make it easy. */
3145 if (num_gfregs)
3146 return 0;
3148 /* If the function uses __builtin_eh_return, the eh_return machinery
3149 occupies the delay slot. */
3150 if (current_function_calls_eh_return)
3151 return 0;
3153 /* In the case of a true leaf function, anything can go into the slot. */
3154 if (leaf_function_p)
3155 return get_attr_in_uncond_branch_delay (trial)
3156 == IN_UNCOND_BRANCH_DELAY_TRUE;
3158 pat = PATTERN (trial);
3160 /* Otherwise, only operations which can be done in tandem with
3161 a `restore' or `return' insn can go into the delay slot. */
3162 if (GET_CODE (SET_DEST (pat)) != REG
3163 || (REGNO (SET_DEST (pat)) >= 8 && REGNO (SET_DEST (pat)) < 24))
3164 return 0;
3166 /* If this instruction sets up floating point register and we have a return
3167 instruction, it can probably go in. But restore will not work
3168 with FP_REGS. */
3169 if (REGNO (SET_DEST (pat)) >= 32)
3170 return (TARGET_V9
3171 && ! epilogue_renumber (&pat, 1)
3172 && (get_attr_in_uncond_branch_delay (trial)
3173 == IN_UNCOND_BRANCH_DELAY_TRUE));
3175 return eligible_for_restore_insn (trial, true);
3178 /* Return nonzero if TRIAL can go into the sibling call's
3179 delay slot. */
3182 eligible_for_sibcall_delay (rtx trial)
3184 int leaf_function_p = current_function_uses_only_leaf_regs;
3185 rtx pat;
3187 if (GET_CODE (trial) != INSN || GET_CODE (PATTERN (trial)) != SET)
3188 return 0;
3190 if (get_attr_length (trial) != 1)
3191 return 0;
3193 pat = PATTERN (trial);
3195 if (leaf_function_p)
3197 /* If the tail call is done using the call instruction,
3198 we have to restore %o7 in the delay slot. */
3199 if (LEAF_SIBCALL_SLOT_RESERVED_P)
3200 return 0;
3202 /* %g1 is used to build the function address */
3203 if (reg_mentioned_p (gen_rtx_REG (Pmode, 1), pat))
3204 return 0;
3206 return 1;
3209 /* Otherwise, only operations which can be done in tandem with
3210 a `restore' insn can go into the delay slot. */
3211 if (GET_CODE (SET_DEST (pat)) != REG
3212 || (REGNO (SET_DEST (pat)) >= 8 && REGNO (SET_DEST (pat)) < 24)
3213 || REGNO (SET_DEST (pat)) >= 32)
3214 return 0;
3216 /* If it mentions %o7, it can't go in, because sibcall will clobber it
3217 in most cases. */
3218 if (reg_mentioned_p (gen_rtx_REG (Pmode, 15), pat))
3219 return 0;
3221 return eligible_for_restore_insn (trial, false);
3225 short_branch (int uid1, int uid2)
3227 int delta = INSN_ADDRESSES (uid1) - INSN_ADDRESSES (uid2);
3229 /* Leave a few words of "slop". */
3230 if (delta >= -1023 && delta <= 1022)
3231 return 1;
3233 return 0;
3236 /* Return nonzero if REG is not used after INSN.
3237 We assume REG is a reload reg, and therefore does
3238 not live past labels or calls or jumps. */
3240 reg_unused_after (rtx reg, rtx insn)
3242 enum rtx_code code, prev_code = UNKNOWN;
3244 while ((insn = NEXT_INSN (insn)))
3246 if (prev_code == CALL_INSN && call_used_regs[REGNO (reg)])
3247 return 1;
3249 code = GET_CODE (insn);
3250 if (GET_CODE (insn) == CODE_LABEL)
3251 return 1;
3253 if (INSN_P (insn))
3255 rtx set = single_set (insn);
3256 int in_src = set && reg_overlap_mentioned_p (reg, SET_SRC (set));
3257 if (set && in_src)
3258 return 0;
3259 if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
3260 return 1;
3261 if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
3262 return 0;
3264 prev_code = code;
3266 return 1;
3269 /* Determine if it's legal to put X into the constant pool. This
3270 is not possible if X contains the address of a symbol that is
3271 not constant (TLS) or not known at final link time (PIC). */
3273 static bool
3274 sparc_cannot_force_const_mem (rtx x)
3276 switch (GET_CODE (x))
3278 case CONST_INT:
3279 case CONST_DOUBLE:
3280 /* Accept all non-symbolic constants. */
3281 return false;
3283 case LABEL_REF:
3284 /* Labels are OK iff we are non-PIC. */
3285 return flag_pic != 0;
3287 case SYMBOL_REF:
3288 /* 'Naked' TLS symbol references are never OK,
3289 non-TLS symbols are OK iff we are non-PIC. */
3290 if (SYMBOL_REF_TLS_MODEL (x))
3291 return true;
3292 else
3293 return flag_pic != 0;
3295 case CONST:
3296 return sparc_cannot_force_const_mem (XEXP (x, 0));
3297 case PLUS:
3298 case MINUS:
3299 return sparc_cannot_force_const_mem (XEXP (x, 0))
3300 || sparc_cannot_force_const_mem (XEXP (x, 1));
3301 case UNSPEC:
3302 return true;
3303 default:
3304 abort ();
3308 /* The table we use to reference PIC data. */
3309 static GTY(()) rtx global_offset_table;
3311 /* The function we use to get at it. */
3312 static GTY(()) rtx add_pc_to_pic_symbol;
3313 static GTY(()) char add_pc_to_pic_symbol_name[256];
3315 /* Ensure that we are not using patterns that are not OK with PIC. */
3318 check_pic (int i)
3320 switch (flag_pic)
3322 case 1:
3323 if (GET_CODE (recog_data.operand[i]) == SYMBOL_REF
3324 || (GET_CODE (recog_data.operand[i]) == CONST
3325 && ! (GET_CODE (XEXP (recog_data.operand[i], 0)) == MINUS
3326 && (XEXP (XEXP (recog_data.operand[i], 0), 0)
3327 == global_offset_table)
3328 && (GET_CODE (XEXP (XEXP (recog_data.operand[i], 0), 1))
3329 == CONST))))
3330 abort ();
3331 case 2:
3332 default:
3333 return 1;
3337 /* Return true if X is an address which needs a temporary register when
3338 reloaded while generating PIC code. */
3341 pic_address_needs_scratch (rtx x)
3343 /* An address which is a symbolic plus a non SMALL_INT needs a temp reg. */
3344 if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
3345 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
3346 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3347 && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
3348 return 1;
3350 return 0;
3353 /* Determine if a given RTX is a valid constant. We already know this
3354 satisfies CONSTANT_P. */
3356 bool
3357 legitimate_constant_p (rtx x)
3359 rtx inner;
3361 switch (GET_CODE (x))
3363 case SYMBOL_REF:
3364 /* TLS symbols are not constant. */
3365 if (SYMBOL_REF_TLS_MODEL (x))
3366 return false;
3367 break;
3369 case CONST:
3370 inner = XEXP (x, 0);
3372 /* Offsets of TLS symbols are never valid.
3373 Discourage CSE from creating them. */
3374 if (GET_CODE (inner) == PLUS
3375 && tls_symbolic_operand (XEXP (inner, 0)))
3376 return false;
3377 break;
3379 case CONST_DOUBLE:
3380 if (GET_MODE (x) == VOIDmode)
3381 return true;
3383 /* Floating point constants are generally not ok.
3384 The only exception is 0.0 in VIS. */
3385 if (TARGET_VIS
3386 && (GET_MODE (x) == SFmode
3387 || GET_MODE (x) == DFmode
3388 || GET_MODE (x) == TFmode)
3389 && fp_zero_operand (x, GET_MODE (x)))
3390 return true;
3392 return false;
3394 default:
3395 break;
3398 return true;
3401 /* Determine if a given RTX is a valid constant address. */
3403 bool
3404 constant_address_p (rtx x)
3406 switch (GET_CODE (x))
3408 case LABEL_REF:
3409 case CONST_INT:
3410 case HIGH:
3411 return true;
3413 case CONST:
3414 if (flag_pic && pic_address_needs_scratch (x))
3415 return false;
3416 return legitimate_constant_p (x);
3418 case SYMBOL_REF:
3419 return !flag_pic && legitimate_constant_p (x);
3421 default:
3422 return false;
3426 /* Nonzero if the constant value X is a legitimate general operand
3427 when generating PIC code. It is given that flag_pic is on and
3428 that X satisfies CONSTANT_P or is a CONST_DOUBLE. */
3430 bool
3431 legitimate_pic_operand_p (rtx x)
3433 if (pic_address_needs_scratch (x))
3434 return false;
3435 if (tls_symbolic_operand (x)
3436 || (GET_CODE (x) == CONST
3437 && GET_CODE (XEXP (x, 0)) == PLUS
3438 && tls_symbolic_operand (XEXP (XEXP (x, 0), 0))))
3439 return false;
3440 return true;
3443 /* Return nonzero if ADDR is a valid memory address.
3444 STRICT specifies whether strict register checking applies. */
3447 legitimate_address_p (enum machine_mode mode, rtx addr, int strict)
3449 rtx rs1 = NULL, rs2 = NULL, imm1 = NULL, imm2;
3451 if (REG_P (addr) || GET_CODE (addr) == SUBREG)
3452 rs1 = addr;
3453 else if (GET_CODE (addr) == PLUS)
3455 rs1 = XEXP (addr, 0);
3456 rs2 = XEXP (addr, 1);
3458 /* Canonicalize. REG comes first, if there are no regs,
3459 LO_SUM comes first. */
3460 if (!REG_P (rs1)
3461 && GET_CODE (rs1) != SUBREG
3462 && (REG_P (rs2)
3463 || GET_CODE (rs2) == SUBREG
3464 || (GET_CODE (rs2) == LO_SUM && GET_CODE (rs1) != LO_SUM)))
3466 rs1 = XEXP (addr, 1);
3467 rs2 = XEXP (addr, 0);
3470 if ((flag_pic == 1
3471 && rs1 == pic_offset_table_rtx
3472 && !REG_P (rs2)
3473 && GET_CODE (rs2) != SUBREG
3474 && GET_CODE (rs2) != LO_SUM
3475 && GET_CODE (rs2) != MEM
3476 && !tls_symbolic_operand (rs2)
3477 && (! symbolic_operand (rs2, VOIDmode) || mode == Pmode)
3478 && (GET_CODE (rs2) != CONST_INT || SMALL_INT (rs2)))
3479 || ((REG_P (rs1)
3480 || GET_CODE (rs1) == SUBREG)
3481 && RTX_OK_FOR_OFFSET_P (rs2)))
3483 imm1 = rs2;
3484 rs2 = NULL;
3486 else if ((REG_P (rs1) || GET_CODE (rs1) == SUBREG)
3487 && (REG_P (rs2) || GET_CODE (rs2) == SUBREG))
3489 /* We prohibit REG + REG for TFmode when there are no instructions
3490 which accept REG+REG instructions. We do this because REG+REG
3491 is not an offsetable address. If we get the situation in reload
3492 where source and destination of a movtf pattern are both MEMs with
3493 REG+REG address, then only one of them gets converted to an
3494 offsetable address. */
3495 if (mode == TFmode
3496 && !(TARGET_FPU && TARGET_ARCH64 && TARGET_V9
3497 && TARGET_HARD_QUAD))
3498 return 0;
3500 /* We prohibit REG + REG on ARCH32 if not optimizing for
3501 DFmode/DImode because then mem_min_alignment is likely to be zero
3502 after reload and the forced split would lack a matching splitter
3503 pattern. */
3504 if (TARGET_ARCH32 && !optimize
3505 && (mode == DFmode || mode == DImode))
3506 return 0;
3508 else if (USE_AS_OFFSETABLE_LO10
3509 && GET_CODE (rs1) == LO_SUM
3510 && TARGET_ARCH64
3511 && ! TARGET_CM_MEDMID
3512 && RTX_OK_FOR_OLO10_P (rs2))
3514 imm2 = rs2;
3515 rs2 = NULL;
3516 imm1 = XEXP (rs1, 1);
3517 rs1 = XEXP (rs1, 0);
3518 if (! CONSTANT_P (imm1) || tls_symbolic_operand (rs1))
3519 return 0;
3522 else if (GET_CODE (addr) == LO_SUM)
3524 rs1 = XEXP (addr, 0);
3525 imm1 = XEXP (addr, 1);
3527 if (! CONSTANT_P (imm1) || tls_symbolic_operand (rs1))
3528 return 0;
3530 /* We can't allow TFmode, because an offset greater than or equal to the
3531 alignment (8) may cause the LO_SUM to overflow if !v9. */
3532 if (mode == TFmode && !TARGET_V9)
3533 return 0;
3535 else if (GET_CODE (addr) == CONST_INT && SMALL_INT (addr))
3536 return 1;
3537 else
3538 return 0;
3540 if (GET_CODE (rs1) == SUBREG)
3541 rs1 = SUBREG_REG (rs1);
3542 if (!REG_P (rs1))
3543 return 0;
3545 if (rs2)
3547 if (GET_CODE (rs2) == SUBREG)
3548 rs2 = SUBREG_REG (rs2);
3549 if (!REG_P (rs2))
3550 return 0;
3553 if (strict)
3555 if (!REGNO_OK_FOR_BASE_P (REGNO (rs1))
3556 || (rs2 && !REGNO_OK_FOR_BASE_P (REGNO (rs2))))
3557 return 0;
3559 else
3561 if ((REGNO (rs1) >= 32
3562 && REGNO (rs1) != FRAME_POINTER_REGNUM
3563 && REGNO (rs1) < FIRST_PSEUDO_REGISTER)
3564 || (rs2
3565 && (REGNO (rs2) >= 32
3566 && REGNO (rs2) != FRAME_POINTER_REGNUM
3567 && REGNO (rs2) < FIRST_PSEUDO_REGISTER)))
3568 return 0;
3570 return 1;
3573 /* Construct the SYMBOL_REF for the tls_get_offset function. */
3575 static GTY(()) rtx sparc_tls_symbol;
3576 static rtx
3577 sparc_tls_get_addr (void)
3579 if (!sparc_tls_symbol)
3580 sparc_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_addr");
3582 return sparc_tls_symbol;
3585 static rtx
3586 sparc_tls_got (void)
3588 rtx temp;
3589 if (flag_pic)
3591 current_function_uses_pic_offset_table = 1;
3592 return pic_offset_table_rtx;
3595 if (!global_offset_table)
3596 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3597 temp = gen_reg_rtx (Pmode);
3598 emit_move_insn (temp, global_offset_table);
3599 return temp;
3603 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
3604 this (thread-local) address. */
3607 legitimize_tls_address (rtx addr)
3609 rtx temp1, temp2, temp3, ret, o0, got, insn;
3611 if (no_new_pseudos)
3612 abort ();
3614 if (GET_CODE (addr) == SYMBOL_REF)
3615 switch (SYMBOL_REF_TLS_MODEL (addr))
3617 case TLS_MODEL_GLOBAL_DYNAMIC:
3618 start_sequence ();
3619 temp1 = gen_reg_rtx (SImode);
3620 temp2 = gen_reg_rtx (SImode);
3621 ret = gen_reg_rtx (Pmode);
3622 o0 = gen_rtx_REG (Pmode, 8);
3623 got = sparc_tls_got ();
3624 emit_insn (gen_tgd_hi22 (temp1, addr));
3625 emit_insn (gen_tgd_lo10 (temp2, temp1, addr));
3626 if (TARGET_ARCH32)
3628 emit_insn (gen_tgd_add32 (o0, got, temp2, addr));
3629 insn = emit_call_insn (gen_tgd_call32 (o0, sparc_tls_get_addr (),
3630 addr, const1_rtx));
3632 else
3634 emit_insn (gen_tgd_add64 (o0, got, temp2, addr));
3635 insn = emit_call_insn (gen_tgd_call64 (o0, sparc_tls_get_addr (),
3636 addr, const1_rtx));
3638 CALL_INSN_FUNCTION_USAGE (insn)
3639 = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_USE (VOIDmode, o0),
3640 CALL_INSN_FUNCTION_USAGE (insn));
3641 insn = get_insns ();
3642 end_sequence ();
3643 emit_libcall_block (insn, ret, o0, addr);
3644 break;
3646 case TLS_MODEL_LOCAL_DYNAMIC:
3647 start_sequence ();
3648 temp1 = gen_reg_rtx (SImode);
3649 temp2 = gen_reg_rtx (SImode);
3650 temp3 = gen_reg_rtx (Pmode);
3651 ret = gen_reg_rtx (Pmode);
3652 o0 = gen_rtx_REG (Pmode, 8);
3653 got = sparc_tls_got ();
3654 emit_insn (gen_tldm_hi22 (temp1));
3655 emit_insn (gen_tldm_lo10 (temp2, temp1));
3656 if (TARGET_ARCH32)
3658 emit_insn (gen_tldm_add32 (o0, got, temp2));
3659 insn = emit_call_insn (gen_tldm_call32 (o0, sparc_tls_get_addr (),
3660 const1_rtx));
3662 else
3664 emit_insn (gen_tldm_add64 (o0, got, temp2));
3665 insn = emit_call_insn (gen_tldm_call64 (o0, sparc_tls_get_addr (),
3666 const1_rtx));
3668 CALL_INSN_FUNCTION_USAGE (insn)
3669 = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_USE (VOIDmode, o0),
3670 CALL_INSN_FUNCTION_USAGE (insn));
3671 insn = get_insns ();
3672 end_sequence ();
3673 emit_libcall_block (insn, temp3, o0,
3674 gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
3675 UNSPEC_TLSLD_BASE));
3676 temp1 = gen_reg_rtx (SImode);
3677 temp2 = gen_reg_rtx (SImode);
3678 emit_insn (gen_tldo_hix22 (temp1, addr));
3679 emit_insn (gen_tldo_lox10 (temp2, temp1, addr));
3680 if (TARGET_ARCH32)
3681 emit_insn (gen_tldo_add32 (ret, temp3, temp2, addr));
3682 else
3683 emit_insn (gen_tldo_add64 (ret, temp3, temp2, addr));
3684 break;
3686 case TLS_MODEL_INITIAL_EXEC:
3687 temp1 = gen_reg_rtx (SImode);
3688 temp2 = gen_reg_rtx (SImode);
3689 temp3 = gen_reg_rtx (Pmode);
3690 got = sparc_tls_got ();
3691 emit_insn (gen_tie_hi22 (temp1, addr));
3692 emit_insn (gen_tie_lo10 (temp2, temp1, addr));
3693 if (TARGET_ARCH32)
3694 emit_insn (gen_tie_ld32 (temp3, got, temp2, addr));
3695 else
3696 emit_insn (gen_tie_ld64 (temp3, got, temp2, addr));
3697 if (TARGET_SUN_TLS)
3699 ret = gen_reg_rtx (Pmode);
3700 if (TARGET_ARCH32)
3701 emit_insn (gen_tie_add32 (ret, gen_rtx_REG (Pmode, 7),
3702 temp3, addr));
3703 else
3704 emit_insn (gen_tie_add64 (ret, gen_rtx_REG (Pmode, 7),
3705 temp3, addr));
3707 else
3708 ret = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, 7), temp3);
3709 break;
3711 case TLS_MODEL_LOCAL_EXEC:
3712 temp1 = gen_reg_rtx (Pmode);
3713 temp2 = gen_reg_rtx (Pmode);
3714 if (TARGET_ARCH32)
3716 emit_insn (gen_tle_hix22_sp32 (temp1, addr));
3717 emit_insn (gen_tle_lox10_sp32 (temp2, temp1, addr));
3719 else
3721 emit_insn (gen_tle_hix22_sp64 (temp1, addr));
3722 emit_insn (gen_tle_lox10_sp64 (temp2, temp1, addr));
3724 ret = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, 7), temp2);
3725 break;
3727 default:
3728 abort ();
3731 else
3732 abort (); /* for now ... */
3734 return ret;
3738 /* Legitimize PIC addresses. If the address is already position-independent,
3739 we return ORIG. Newly generated position-independent addresses go into a
3740 reg. This is REG if nonzero, otherwise we allocate register(s) as
3741 necessary. */
3744 legitimize_pic_address (rtx orig, enum machine_mode mode ATTRIBUTE_UNUSED,
3745 rtx reg)
3747 if (GET_CODE (orig) == SYMBOL_REF)
3749 rtx pic_ref, address;
3750 rtx insn;
3752 if (reg == 0)
3754 if (reload_in_progress || reload_completed)
3755 abort ();
3756 else
3757 reg = gen_reg_rtx (Pmode);
3760 if (flag_pic == 2)
3762 /* If not during reload, allocate another temp reg here for loading
3763 in the address, so that these instructions can be optimized
3764 properly. */
3765 rtx temp_reg = ((reload_in_progress || reload_completed)
3766 ? reg : gen_reg_rtx (Pmode));
3768 /* Must put the SYMBOL_REF inside an UNSPEC here so that cse
3769 won't get confused into thinking that these two instructions
3770 are loading in the true address of the symbol. If in the
3771 future a PIC rtx exists, that should be used instead. */
3772 if (Pmode == SImode)
3774 emit_insn (gen_movsi_high_pic (temp_reg, orig));
3775 emit_insn (gen_movsi_lo_sum_pic (temp_reg, temp_reg, orig));
3777 else
3779 emit_insn (gen_movdi_high_pic (temp_reg, orig));
3780 emit_insn (gen_movdi_lo_sum_pic (temp_reg, temp_reg, orig));
3782 address = temp_reg;
3784 else
3785 address = orig;
3787 pic_ref = gen_rtx_MEM (Pmode,
3788 gen_rtx_PLUS (Pmode,
3789 pic_offset_table_rtx, address));
3790 current_function_uses_pic_offset_table = 1;
3791 RTX_UNCHANGING_P (pic_ref) = 1;
3792 insn = emit_move_insn (reg, pic_ref);
3793 /* Put a REG_EQUAL note on this insn, so that it can be optimized
3794 by loop. */
3795 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3796 REG_NOTES (insn));
3797 return reg;
3799 else if (GET_CODE (orig) == CONST)
3801 rtx base, offset;
3803 if (GET_CODE (XEXP (orig, 0)) == PLUS
3804 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3805 return orig;
3807 if (reg == 0)
3809 if (reload_in_progress || reload_completed)
3810 abort ();
3811 else
3812 reg = gen_reg_rtx (Pmode);
3815 if (GET_CODE (XEXP (orig, 0)) == PLUS)
3817 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3818 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3819 base == reg ? 0 : reg);
3821 else
3822 abort ();
3824 if (GET_CODE (offset) == CONST_INT)
3826 if (SMALL_INT (offset))
3827 return plus_constant (base, INTVAL (offset));
3828 else if (! reload_in_progress && ! reload_completed)
3829 offset = force_reg (Pmode, offset);
3830 else
3831 /* If we reach here, then something is seriously wrong. */
3832 abort ();
3834 return gen_rtx_PLUS (Pmode, base, offset);
3836 else if (GET_CODE (orig) == LABEL_REF)
3837 /* ??? Why do we do this? */
3838 /* Now movsi_pic_label_ref uses it, but we ought to be checking that
3839 the register is live instead, in case it is eliminated. */
3840 current_function_uses_pic_offset_table = 1;
3842 return orig;
3845 /* Try machine-dependent ways of modifying an illegitimate address X
3846 to be legitimate. If we find one, return the new, valid address.
3848 OLDX is the address as it was before break_out_memory_refs was called.
3849 In some cases it is useful to look at this to decide what needs to be done.
3851 MODE is the mode of the operand pointed to by X. */
3854 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
3856 rtx orig_x = x;
3858 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == MULT)
3859 x = gen_rtx_PLUS (Pmode, XEXP (x, 1),
3860 force_operand (XEXP (x, 0), NULL_RTX));
3861 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == MULT)
3862 x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
3863 force_operand (XEXP (x, 1), NULL_RTX));
3864 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS)
3865 x = gen_rtx_PLUS (Pmode, force_operand (XEXP (x, 0), NULL_RTX),
3866 XEXP (x, 1));
3867 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == PLUS)
3868 x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
3869 force_operand (XEXP (x, 1), NULL_RTX));
3871 if (x != orig_x && legitimate_address_p (mode, x, FALSE))
3872 return x;
3874 if (tls_symbolic_operand (x))
3875 x = legitimize_tls_address (x);
3876 else if (flag_pic)
3877 x = legitimize_pic_address (x, mode, 0);
3878 else if (GET_CODE (x) == PLUS && CONSTANT_ADDRESS_P (XEXP (x, 1)))
3879 x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
3880 copy_to_mode_reg (Pmode, XEXP (x, 1)));
3881 else if (GET_CODE (x) == PLUS && CONSTANT_ADDRESS_P (XEXP (x, 0)))
3882 x = gen_rtx_PLUS (Pmode, XEXP (x, 1),
3883 copy_to_mode_reg (Pmode, XEXP (x, 0)));
3884 else if (GET_CODE (x) == SYMBOL_REF
3885 || GET_CODE (x) == CONST
3886 || GET_CODE (x) == LABEL_REF)
3887 x = copy_to_suggested_reg (x, NULL_RTX, Pmode);
3888 return x;
3891 /* Emit the special PIC prologue. */
3893 static void
3894 load_pic_register (void)
3896 int orig_flag_pic = flag_pic;
3898 /* If we haven't emitted the special helper function, do so now. */
3899 if (add_pc_to_pic_symbol_name[0] == 0)
3901 const char *pic_name = reg_names[REGNO (pic_offset_table_rtx)];
3902 int align;
3904 ASM_GENERATE_INTERNAL_LABEL (add_pc_to_pic_symbol_name, "LADDPC", 0);
3905 text_section ();
3907 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3908 if (align > 0)
3909 ASM_OUTPUT_ALIGN (asm_out_file, align);
3910 ASM_OUTPUT_LABEL (asm_out_file, add_pc_to_pic_symbol_name);
3911 if (flag_delayed_branch)
3912 fprintf (asm_out_file, "\tjmp %%o7+8\n\t add\t%%o7, %s, %s\n",
3913 pic_name, pic_name);
3914 else
3915 fprintf (asm_out_file, "\tadd\t%%o7, %s, %s\n\tjmp %%o7+8\n\t nop\n",
3916 pic_name, pic_name);
3919 /* Initialize every time through, since we can't easily
3920 know this to be permanent. */
3921 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3922 add_pc_to_pic_symbol = gen_rtx_SYMBOL_REF (Pmode, add_pc_to_pic_symbol_name);
3924 flag_pic = 0;
3925 emit_insn (gen_load_pcrel_sym (pic_offset_table_rtx, global_offset_table,
3926 add_pc_to_pic_symbol));
3927 flag_pic = orig_flag_pic;
3929 /* Need to emit this whether or not we obey regdecls,
3930 since setjmp/longjmp can cause life info to screw up.
3931 ??? In the case where we don't obey regdecls, this is not sufficient
3932 since we may not fall out the bottom. */
3933 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3936 /* Return 1 if RTX is a MEM which is known to be aligned to at
3937 least a DESIRED byte boundary. */
3940 mem_min_alignment (rtx mem, int desired)
3942 rtx addr, base, offset;
3944 /* If it's not a MEM we can't accept it. */
3945 if (GET_CODE (mem) != MEM)
3946 return 0;
3948 addr = XEXP (mem, 0);
3949 base = offset = NULL_RTX;
3950 if (GET_CODE (addr) == PLUS)
3952 if (GET_CODE (XEXP (addr, 0)) == REG)
3954 base = XEXP (addr, 0);
3956 /* What we are saying here is that if the base
3957 REG is aligned properly, the compiler will make
3958 sure any REG based index upon it will be so
3959 as well. */
3960 if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
3961 offset = XEXP (addr, 1);
3962 else
3963 offset = const0_rtx;
3966 else if (GET_CODE (addr) == REG)
3968 base = addr;
3969 offset = const0_rtx;
3972 if (base != NULL_RTX)
3974 int regno = REGNO (base);
3976 if (regno != HARD_FRAME_POINTER_REGNUM && regno != STACK_POINTER_REGNUM)
3978 /* Check if the compiler has recorded some information
3979 about the alignment of the base REG. If reload has
3980 completed, we already matched with proper alignments.
3981 If not running global_alloc, reload might give us
3982 unaligned pointer to local stack though. */
3983 if (((cfun != 0
3984 && REGNO_POINTER_ALIGN (regno) >= desired * BITS_PER_UNIT)
3985 || (optimize && reload_completed))
3986 && (INTVAL (offset) & (desired - 1)) == 0)
3987 return 1;
3989 else
3991 if (((INTVAL (offset) - SPARC_STACK_BIAS) & (desired - 1)) == 0)
3992 return 1;
3995 else if (! TARGET_UNALIGNED_DOUBLES
3996 || CONSTANT_P (addr)
3997 || GET_CODE (addr) == LO_SUM)
3999 /* Anything else we know is properly aligned unless TARGET_UNALIGNED_DOUBLES
4000 is true, in which case we can only assume that an access is aligned if
4001 it is to a constant address, or the address involves a LO_SUM. */
4002 return 1;
4005 /* An obviously unaligned address. */
4006 return 0;
4010 /* Vectors to keep interesting information about registers where it can easily
4011 be got. We used to use the actual mode value as the bit number, but there
4012 are more than 32 modes now. Instead we use two tables: one indexed by
4013 hard register number, and one indexed by mode. */
4015 /* The purpose of sparc_mode_class is to shrink the range of modes so that
4016 they all fit (as bit numbers) in a 32 bit word (again). Each real mode is
4017 mapped into one sparc_mode_class mode. */
4019 enum sparc_mode_class {
4020 S_MODE, D_MODE, T_MODE, O_MODE,
4021 SF_MODE, DF_MODE, TF_MODE, OF_MODE,
4022 CC_MODE, CCFP_MODE
4025 /* Modes for single-word and smaller quantities. */
4026 #define S_MODES ((1 << (int) S_MODE) | (1 << (int) SF_MODE))
4028 /* Modes for double-word and smaller quantities. */
4029 #define D_MODES (S_MODES | (1 << (int) D_MODE) | (1 << DF_MODE))
4031 /* Modes for quad-word and smaller quantities. */
4032 #define T_MODES (D_MODES | (1 << (int) T_MODE) | (1 << (int) TF_MODE))
4034 /* Modes for 8-word and smaller quantities. */
4035 #define O_MODES (T_MODES | (1 << (int) O_MODE) | (1 << (int) OF_MODE))
4037 /* Modes for single-float quantities. We must allow any single word or
4038 smaller quantity. This is because the fix/float conversion instructions
4039 take integer inputs/outputs from the float registers. */
4040 #define SF_MODES (S_MODES)
4042 /* Modes for double-float and smaller quantities. */
4043 #define DF_MODES (S_MODES | D_MODES)
4045 /* Modes for double-float only quantities. */
4046 #define DF_MODES_NO_S ((1 << (int) D_MODE) | (1 << (int) DF_MODE))
4048 /* Modes for quad-float only quantities. */
4049 #define TF_ONLY_MODES (1 << (int) TF_MODE)
4051 /* Modes for quad-float and smaller quantities. */
4052 #define TF_MODES (DF_MODES | TF_ONLY_MODES)
4054 /* Modes for quad-float and double-float quantities. */
4055 #define TF_MODES_NO_S (DF_MODES_NO_S | TF_ONLY_MODES)
4057 /* Modes for quad-float pair only quantities. */
4058 #define OF_ONLY_MODES (1 << (int) OF_MODE)
4060 /* Modes for quad-float pairs and smaller quantities. */
4061 #define OF_MODES (TF_MODES | OF_ONLY_MODES)
4063 #define OF_MODES_NO_S (TF_MODES_NO_S | OF_ONLY_MODES)
4065 /* Modes for condition codes. */
4066 #define CC_MODES (1 << (int) CC_MODE)
4067 #define CCFP_MODES (1 << (int) CCFP_MODE)
4069 /* Value is 1 if register/mode pair is acceptable on sparc.
4070 The funny mixture of D and T modes is because integer operations
4071 do not specially operate on tetra quantities, so non-quad-aligned
4072 registers can hold quadword quantities (except %o4 and %i4 because
4073 they cross fixed registers). */
4075 /* This points to either the 32 bit or the 64 bit version. */
4076 const int *hard_regno_mode_classes;
4078 static const int hard_32bit_mode_classes[] = {
4079 S_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
4080 T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
4081 T_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
4082 T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
4084 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4085 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4086 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4087 OF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
4089 /* FP regs f32 to f63. Only the even numbered registers actually exist,
4090 and none can hold SFmode/SImode values. */
4091 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4092 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4093 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4094 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, TF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4096 /* %fcc[0123] */
4097 CCFP_MODES, CCFP_MODES, CCFP_MODES, CCFP_MODES,
4099 /* %icc */
4100 CC_MODES
4103 static const int hard_64bit_mode_classes[] = {
4104 D_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
4105 O_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
4106 T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
4107 O_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
4109 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4110 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4111 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4112 OF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
4114 /* FP regs f32 to f63. Only the even numbered registers actually exist,
4115 and none can hold SFmode/SImode values. */
4116 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4117 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4118 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4119 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, TF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4121 /* %fcc[0123] */
4122 CCFP_MODES, CCFP_MODES, CCFP_MODES, CCFP_MODES,
4124 /* %icc */
4125 CC_MODES
4128 int sparc_mode_class [NUM_MACHINE_MODES];
4130 enum reg_class sparc_regno_reg_class[FIRST_PSEUDO_REGISTER];
4132 static void
4133 sparc_init_modes (void)
4135 int i;
4137 for (i = 0; i < NUM_MACHINE_MODES; i++)
4139 switch (GET_MODE_CLASS (i))
4141 case MODE_INT:
4142 case MODE_PARTIAL_INT:
4143 case MODE_COMPLEX_INT:
4144 if (GET_MODE_SIZE (i) <= 4)
4145 sparc_mode_class[i] = 1 << (int) S_MODE;
4146 else if (GET_MODE_SIZE (i) == 8)
4147 sparc_mode_class[i] = 1 << (int) D_MODE;
4148 else if (GET_MODE_SIZE (i) == 16)
4149 sparc_mode_class[i] = 1 << (int) T_MODE;
4150 else if (GET_MODE_SIZE (i) == 32)
4151 sparc_mode_class[i] = 1 << (int) O_MODE;
4152 else
4153 sparc_mode_class[i] = 0;
4154 break;
4155 case MODE_FLOAT:
4156 case MODE_COMPLEX_FLOAT:
4157 if (GET_MODE_SIZE (i) <= 4)
4158 sparc_mode_class[i] = 1 << (int) SF_MODE;
4159 else if (GET_MODE_SIZE (i) == 8)
4160 sparc_mode_class[i] = 1 << (int) DF_MODE;
4161 else if (GET_MODE_SIZE (i) == 16)
4162 sparc_mode_class[i] = 1 << (int) TF_MODE;
4163 else if (GET_MODE_SIZE (i) == 32)
4164 sparc_mode_class[i] = 1 << (int) OF_MODE;
4165 else
4166 sparc_mode_class[i] = 0;
4167 break;
4168 case MODE_CC:
4169 if (i == (int) CCFPmode || i == (int) CCFPEmode)
4170 sparc_mode_class[i] = 1 << (int) CCFP_MODE;
4171 else
4172 sparc_mode_class[i] = 1 << (int) CC_MODE;
4173 break;
4174 default:
4175 sparc_mode_class[i] = 0;
4176 break;
4180 if (TARGET_ARCH64)
4181 hard_regno_mode_classes = hard_64bit_mode_classes;
4182 else
4183 hard_regno_mode_classes = hard_32bit_mode_classes;
4185 /* Initialize the array used by REGNO_REG_CLASS. */
4186 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4188 if (i < 16 && TARGET_V8PLUS)
4189 sparc_regno_reg_class[i] = I64_REGS;
4190 else if (i < 32 || i == FRAME_POINTER_REGNUM)
4191 sparc_regno_reg_class[i] = GENERAL_REGS;
4192 else if (i < 64)
4193 sparc_regno_reg_class[i] = FP_REGS;
4194 else if (i < 96)
4195 sparc_regno_reg_class[i] = EXTRA_FP_REGS;
4196 else if (i < 100)
4197 sparc_regno_reg_class[i] = FPCC_REGS;
4198 else
4199 sparc_regno_reg_class[i] = NO_REGS;
4203 /* Compute the frame size required by the function. This function is called
4204 during the reload pass and also by sparc_expand_prologue. */
4206 HOST_WIDE_INT
4207 sparc_compute_frame_size (HOST_WIDE_INT size, int leaf_function_p)
4209 int outgoing_args_size = (current_function_outgoing_args_size
4210 + REG_PARM_STACK_SPACE (current_function_decl));
4211 int n_regs = 0; /* N_REGS is the number of 4-byte regs saved thus far. */
4212 int i;
4214 if (TARGET_ARCH64)
4216 for (i = 0; i < 8; i++)
4217 if (regs_ever_live[i] && ! call_used_regs[i])
4218 n_regs += 2;
4220 else
4222 for (i = 0; i < 8; i += 2)
4223 if ((regs_ever_live[i] && ! call_used_regs[i])
4224 || (regs_ever_live[i+1] && ! call_used_regs[i+1]))
4225 n_regs += 2;
4228 for (i = 32; i < (TARGET_V9 ? 96 : 64); i += 2)
4229 if ((regs_ever_live[i] && ! call_used_regs[i])
4230 || (regs_ever_live[i+1] && ! call_used_regs[i+1]))
4231 n_regs += 2;
4233 /* Set up values for use in prologue and epilogue. */
4234 num_gfregs = n_regs;
4236 if (leaf_function_p
4237 && n_regs == 0
4238 && size == 0
4239 && current_function_outgoing_args_size == 0)
4240 actual_fsize = apparent_fsize = 0;
4241 else
4243 /* We subtract STARTING_FRAME_OFFSET, remember it's negative. */
4244 apparent_fsize = (size - STARTING_FRAME_OFFSET + 7) & -8;
4245 apparent_fsize += n_regs * 4;
4246 actual_fsize = apparent_fsize + ((outgoing_args_size + 7) & -8);
4249 /* Make sure nothing can clobber our register windows.
4250 If a SAVE must be done, or there is a stack-local variable,
4251 the register window area must be allocated.
4252 ??? For v8 we apparently need an additional 8 bytes of reserved space. */
4253 if (! leaf_function_p || size > 0)
4254 actual_fsize += (16 * UNITS_PER_WORD) + (TARGET_ARCH64 ? 0 : 8);
4256 return SPARC_STACK_ALIGN (actual_fsize);
4259 /* Output any necessary .register pseudo-ops. */
4261 void
4262 sparc_output_scratch_registers (FILE *file ATTRIBUTE_UNUSED)
4264 #ifdef HAVE_AS_REGISTER_PSEUDO_OP
4265 int i;
4267 if (TARGET_ARCH32)
4268 return;
4270 /* Check if %g[2367] were used without
4271 .register being printed for them already. */
4272 for (i = 2; i < 8; i++)
4274 if (regs_ever_live [i]
4275 && ! sparc_hard_reg_printed [i])
4277 sparc_hard_reg_printed [i] = 1;
4278 fprintf (file, "\t.register\t%%g%d, #scratch\n", i);
4280 if (i == 3) i = 5;
4282 #endif
4285 /* Save/restore call-saved registers from LOW to HIGH at BASE+OFFSET
4286 as needed. LOW should be double-word aligned for 32-bit registers.
4287 Return the new OFFSET. */
4289 #define SORR_SAVE 0
4290 #define SORR_RESTORE 1
4292 static int
4293 save_or_restore_regs (int low, int high, rtx base, int offset, int action)
4295 rtx mem, insn;
4296 int i;
4298 if (TARGET_ARCH64 && high <= 32)
4300 for (i = low; i < high; i++)
4302 if (regs_ever_live[i] && ! call_used_regs[i])
4304 mem = gen_rtx_MEM (DImode, plus_constant (base, offset));
4305 set_mem_alias_set (mem, sparc_sr_alias_set);
4306 if (action == SORR_SAVE)
4308 insn = emit_move_insn (mem, gen_rtx_REG (DImode, i));
4309 RTX_FRAME_RELATED_P (insn) = 1;
4311 else /* action == SORR_RESTORE */
4312 emit_move_insn (gen_rtx_REG (DImode, i), mem);
4313 offset += 8;
4317 else
4319 for (i = low; i < high; i += 2)
4321 bool reg0 = regs_ever_live[i] && ! call_used_regs[i];
4322 bool reg1 = regs_ever_live[i+1] && ! call_used_regs[i+1];
4323 enum machine_mode mode;
4324 int regno;
4326 if (reg0 && reg1)
4328 mode = i < 32 ? DImode : DFmode;
4329 regno = i;
4331 else if (reg0)
4333 mode = i < 32 ? SImode : SFmode;
4334 regno = i;
4336 else if (reg1)
4338 mode = i < 32 ? SImode : SFmode;
4339 regno = i + 1;
4340 offset += 4;
4342 else
4343 continue;
4345 mem = gen_rtx_MEM (mode, plus_constant (base, offset));
4346 set_mem_alias_set (mem, sparc_sr_alias_set);
4347 if (action == SORR_SAVE)
4349 insn = emit_move_insn (mem, gen_rtx_REG (mode, regno));
4350 RTX_FRAME_RELATED_P (insn) = 1;
4352 else /* action == SORR_RESTORE */
4353 emit_move_insn (gen_rtx_REG (mode, regno), mem);
4355 /* Always preserve double-word alignment. */
4356 offset = (offset + 7) & -8;
4360 return offset;
4363 /* Emit code to save call-saved registers. */
4365 static void
4366 emit_save_regs (void)
4368 HOST_WIDE_INT offset;
4369 rtx base;
4371 offset = frame_base_offset - apparent_fsize;
4373 if (offset < -4096 || offset + num_gfregs * 4 > 4096)
4375 /* ??? This might be optimized a little as %g1 might already have a
4376 value close enough that a single add insn will do. */
4377 /* ??? Although, all of this is probably only a temporary fix
4378 because if %g1 can hold a function result, then
4379 sparc_expand_epilogue will lose (the result will be
4380 clobbered). */
4381 base = gen_rtx_REG (Pmode, 1);
4382 emit_move_insn (base, GEN_INT (offset));
4383 emit_insn (gen_rtx_SET (VOIDmode,
4384 base,
4385 gen_rtx_PLUS (Pmode, frame_base_reg, base)));
4386 offset = 0;
4388 else
4389 base = frame_base_reg;
4391 offset = save_or_restore_regs (0, 8, base, offset, SORR_SAVE);
4392 save_or_restore_regs (32, TARGET_V9 ? 96 : 64, base, offset, SORR_SAVE);
4395 /* Emit code to restore call-saved registers. */
4397 static void
4398 emit_restore_regs (void)
4400 HOST_WIDE_INT offset;
4401 rtx base;
4403 offset = frame_base_offset - apparent_fsize;
4405 if (offset < -4096 || offset + num_gfregs * 4 > 4096 - 8 /*double*/)
4407 base = gen_rtx_REG (Pmode, 1);
4408 emit_move_insn (base, GEN_INT (offset));
4409 emit_insn (gen_rtx_SET (VOIDmode,
4410 base,
4411 gen_rtx_PLUS (Pmode, frame_base_reg, base)));
4412 offset = 0;
4414 else
4415 base = frame_base_reg;
4417 offset = save_or_restore_regs (0, 8, base, offset, SORR_RESTORE);
4418 save_or_restore_regs (32, TARGET_V9 ? 96 : 64, base, offset, SORR_RESTORE);
4421 /* Emit an increment for the stack pointer. */
4423 static void
4424 emit_stack_pointer_increment (rtx increment)
4426 if (TARGET_ARCH64)
4427 emit_insn (gen_adddi3 (stack_pointer_rtx, stack_pointer_rtx, increment));
4428 else
4429 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, increment));
4432 /* Emit a decrement for the stack pointer. */
4434 static void
4435 emit_stack_pointer_decrement (rtx decrement)
4437 if (TARGET_ARCH64)
4438 emit_insn (gen_subdi3 (stack_pointer_rtx, stack_pointer_rtx, decrement));
4439 else
4440 emit_insn (gen_subsi3 (stack_pointer_rtx, stack_pointer_rtx, decrement));
4443 /* Expand the function prologue. The prologue is responsible for reserving
4444 storage for the frame, saving the call-saved registers and loading the
4445 PIC register if needed. */
4447 void
4448 sparc_expand_prologue (void)
4450 int leaf_function_p = current_function_uses_only_leaf_regs;
4452 /* Need to use actual_fsize, since we are also allocating
4453 space for our callee (and our own register save area). */
4454 actual_fsize = sparc_compute_frame_size (get_frame_size(), leaf_function_p);
4456 if (leaf_function_p)
4458 frame_base_reg = stack_pointer_rtx;
4459 frame_base_offset = actual_fsize + SPARC_STACK_BIAS;
4461 else
4463 frame_base_reg = hard_frame_pointer_rtx;
4464 frame_base_offset = SPARC_STACK_BIAS;
4467 if (actual_fsize == 0)
4468 /* do nothing. */ ;
4469 else if (leaf_function_p)
4471 if (actual_fsize <= 4096)
4472 emit_stack_pointer_increment (GEN_INT (- actual_fsize));
4473 else if (actual_fsize <= 8192)
4475 emit_stack_pointer_increment (GEN_INT (-4096));
4476 emit_stack_pointer_increment (GEN_INT (4096 - actual_fsize));
4478 else
4480 rtx reg = gen_rtx_REG (Pmode, 1);
4481 emit_move_insn (reg, GEN_INT (-actual_fsize));
4482 emit_stack_pointer_increment (reg);
4485 else
4487 if (actual_fsize <= 4096)
4488 emit_insn (gen_save_register_window (GEN_INT (-actual_fsize)));
4489 else if (actual_fsize <= 8192)
4491 emit_insn (gen_save_register_window (GEN_INT (-4096)));
4492 emit_stack_pointer_increment (GEN_INT (4096 - actual_fsize));
4494 else
4496 rtx reg = gen_rtx_REG (Pmode, 1);
4497 emit_move_insn (reg, GEN_INT (-actual_fsize));
4498 emit_insn (gen_save_register_window (reg));
4502 /* Call-saved registers are saved just above the outgoing argument area. */
4503 if (num_gfregs)
4504 emit_save_regs ();
4506 /* Load the PIC register if needed. */
4507 if (flag_pic && current_function_uses_pic_offset_table)
4508 load_pic_register ();
4511 /* This function generates the assembly code for function entry, which boils
4512 down to emitting the necessary .register directives. It also informs the
4513 DWARF-2 back-end on the layout of the frame.
4515 ??? Historical cruft: "On SPARC, move-double insns between fpu and cpu need
4516 an 8-byte block of memory. If any fpu reg is used in the function, we
4517 allocate such a block here, at the bottom of the frame, just in case it's
4518 needed." Could this explain the -8 in emit_restore_regs? */
4520 static void
4521 sparc_asm_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4523 int leaf_function_p = current_function_uses_only_leaf_regs;
4525 sparc_output_scratch_registers (file);
4527 if (dwarf2out_do_frame () && actual_fsize)
4529 char *label = dwarf2out_cfi_label ();
4531 /* The canonical frame address refers to the top of the frame. */
4532 dwarf2out_def_cfa (label,
4533 leaf_function_p
4534 ? STACK_POINTER_REGNUM
4535 : HARD_FRAME_POINTER_REGNUM,
4536 frame_base_offset);
4538 if (! leaf_function_p)
4540 /* Note the register window save. This tells the unwinder that
4541 it needs to restore the window registers from the previous
4542 frame's window save area at 0(cfa). */
4543 dwarf2out_window_save (label);
4545 /* The return address (-8) is now in %i7. */
4546 dwarf2out_return_reg (label, 31);
4551 /* Expand the function epilogue, either normal or part of a sibcall.
4552 We emit all the instructions except the return or the call. */
4554 void
4555 sparc_expand_epilogue (void)
4557 int leaf_function_p = current_function_uses_only_leaf_regs;
4559 if (num_gfregs)
4560 emit_restore_regs ();
4562 if (actual_fsize == 0)
4563 /* do nothing. */ ;
4564 else if (leaf_function_p)
4566 if (actual_fsize <= 4096)
4567 emit_stack_pointer_decrement (GEN_INT (- actual_fsize));
4568 else if (actual_fsize <= 8192)
4570 emit_stack_pointer_decrement (GEN_INT (-4096));
4571 emit_stack_pointer_decrement (GEN_INT (4096 - actual_fsize));
4573 else
4575 rtx reg = gen_rtx_REG (Pmode, 1);
4576 emit_move_insn (reg, GEN_INT (-actual_fsize));
4577 emit_stack_pointer_decrement (reg);
4582 /* This function generates the assembly code for function exit. */
4584 static void
4585 sparc_asm_function_epilogue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4587 /* If code does not drop into the epilogue, we have to still output
4588 a dummy nop for the sake of sane backtraces. Otherwise, if the
4589 last two instructions of a function were "call foo; dslot;" this
4590 can make the return PC of foo (ie. address of call instruction
4591 plus 8) point to the first instruction in the next function. */
4593 rtx insn, last_real_insn;
4595 insn = get_last_insn ();
4597 last_real_insn = prev_real_insn (insn);
4598 if (last_real_insn
4599 && GET_CODE (last_real_insn) == INSN
4600 && GET_CODE (PATTERN (last_real_insn)) == SEQUENCE)
4601 last_real_insn = XVECEXP (PATTERN (last_real_insn), 0, 0);
4603 if (last_real_insn && GET_CODE (last_real_insn) == CALL_INSN)
4604 fputs("\tnop\n", file);
4606 sparc_output_deferred_case_vectors ();
4609 /* Output a 'restore' instruction. */
4611 static void
4612 output_restore (rtx pat)
4614 rtx operands[3];
4616 if (! pat)
4618 fputs ("\t restore\n", asm_out_file);
4619 return;
4622 if (GET_CODE (pat) != SET)
4623 abort ();
4625 operands[0] = SET_DEST (pat);
4626 pat = SET_SRC (pat);
4628 switch (GET_CODE (pat))
4630 case PLUS:
4631 operands[1] = XEXP (pat, 0);
4632 operands[2] = XEXP (pat, 1);
4633 output_asm_insn (" restore %r1, %2, %Y0", operands);
4634 break;
4635 case LO_SUM:
4636 operands[1] = XEXP (pat, 0);
4637 operands[2] = XEXP (pat, 1);
4638 output_asm_insn (" restore %r1, %%lo(%a2), %Y0", operands);
4639 break;
4640 case ASHIFT:
4641 operands[1] = XEXP (pat, 0);
4642 if (XEXP (pat, 1) != const1_rtx)
4643 abort();
4644 output_asm_insn (" restore %r1, %r1, %Y0", operands);
4645 break;
4646 default:
4647 operands[1] = pat;
4648 output_asm_insn (" restore %%g0, %1, %Y0", operands);
4649 break;
4653 /* Output a return. */
4655 const char *
4656 output_return (rtx insn)
4658 int leaf_function_p = current_function_uses_only_leaf_regs;
4659 bool delay_slot_filled_p = dbr_sequence_length () > 0;
4660 /* True if the caller has placed an "unimp" insn immediately after the call.
4661 This insn is used in the 32-bit ABI when calling a function that returns
4662 a non zero-sized structure. The 64-bit ABI doesn't have it. Be careful
4663 to have this test be the same as that used on the call. */
4664 bool sparc_skip_caller_unimp
4665 = ! TARGET_ARCH64
4666 && current_function_returns_struct
4667 && (TREE_CODE (DECL_SIZE (DECL_RESULT (current_function_decl)))
4668 == INTEGER_CST)
4669 && ! integer_zerop (DECL_SIZE (DECL_RESULT (current_function_decl)));
4671 if (leaf_function_p)
4673 /* This is a leaf function so we don't have to bother restoring the
4674 register window, which frees us from dealing with the convoluted
4675 semantics of restore/return. We simply output the jump to the
4676 return address and the insn in the delay slot, which usually is
4677 the substraction restoring the stack pointer %sp. */
4679 if (current_function_calls_eh_return)
4680 abort ();
4682 fprintf (asm_out_file, "\tjmp\t%%o7+%d\n", sparc_skip_caller_unimp ? 12 : 8);
4684 if (delay_slot_filled_p)
4686 rtx delay = NEXT_INSN (insn);
4687 if (! delay)
4688 abort ();
4690 final_scan_insn (delay, asm_out_file, 1, 0, 1, NULL);
4691 PATTERN (delay) = gen_blockage ();
4692 INSN_CODE (delay) = -1;
4694 else
4695 fputs ("\t nop\n", asm_out_file);
4697 else
4699 /* This is a regular function so we have to restore the register window.
4700 We may have a pending insn for the delay slot, which will be either
4701 combined with the 'restore' instruction or put in the delay slot of
4702 the 'return' instruction. */
4704 if (current_function_calls_eh_return)
4706 /* If the function uses __builtin_eh_return, the eh_return
4707 machinery occupies the delay slot. */
4708 if (delay_slot_filled_p || sparc_skip_caller_unimp)
4709 abort ();
4711 if (! flag_delayed_branch)
4712 fputs ("\tadd\t%fp, %g1, %fp\n", asm_out_file);
4714 if (TARGET_V9)
4715 fputs ("\treturn\t%i7+8\n", asm_out_file);
4716 else
4717 fputs ("\trestore\n\tjmp\t%o7+8\n", asm_out_file);
4719 if (flag_delayed_branch)
4720 fputs ("\t add\t%sp, %g1, %sp\n", asm_out_file);
4721 else
4722 fputs ("\t nop\n", asm_out_file);
4724 else if (delay_slot_filled_p)
4726 rtx delay, pat;
4728 delay = NEXT_INSN (insn);
4729 if (! delay)
4730 abort ();
4732 pat = PATTERN (delay);
4734 if (TARGET_V9 && ! epilogue_renumber (&pat, 1))
4736 epilogue_renumber (&pat, 0);
4737 fprintf (asm_out_file, "\treturn\t%%i7+%d\n",
4738 sparc_skip_caller_unimp ? 12 : 8);
4739 final_scan_insn (delay, asm_out_file, 1, 0, 1, NULL);
4741 else
4743 fprintf (asm_out_file, "\tjmp\t%%i7+%d\n",
4744 sparc_skip_caller_unimp ? 12 : 8);
4745 output_restore (pat);
4748 PATTERN (delay) = gen_blockage ();
4749 INSN_CODE (delay) = -1;
4751 else
4753 /* The delay slot is empty. */
4754 if (TARGET_V9)
4755 fprintf (asm_out_file, "\treturn\t%%i7+%d\n\t nop\n",
4756 sparc_skip_caller_unimp ? 12 : 8);
4757 else if (flag_delayed_branch)
4758 fprintf (asm_out_file, "\tjmp\t%%i7+%d\n\t restore\n",
4759 sparc_skip_caller_unimp ? 12 : 8);
4760 else
4761 fprintf (asm_out_file, "\trestore\n\tjmp\t%%o7+%d\n\t nop\n",
4762 sparc_skip_caller_unimp ? 12 : 8);
4766 return "";
4769 /* Output a sibling call. */
4771 const char *
4772 output_sibcall (rtx insn, rtx call_operand)
4774 int leaf_function_p = current_function_uses_only_leaf_regs;
4775 bool delay_slot_filled_p = dbr_sequence_length () > 0;
4776 rtx operands[1];
4778 if (! flag_delayed_branch)
4779 abort();
4781 operands[0] = call_operand;
4783 if (leaf_function_p)
4785 /* This is a leaf function so we don't have to bother restoring the
4786 register window. We simply output the jump to the function and
4787 the insn in the delay slot (if any). */
4789 if (LEAF_SIBCALL_SLOT_RESERVED_P && delay_slot_filled_p)
4790 abort();
4792 if (delay_slot_filled_p)
4794 rtx delay = NEXT_INSN (insn);
4795 if (! delay)
4796 abort ();
4798 output_asm_insn ("sethi\t%%hi(%a0), %%g1", operands);
4799 output_asm_insn ("jmp\t%%g1 + %%lo(%a0)", operands);
4800 final_scan_insn (delay, asm_out_file, 1, 0, 1, NULL);
4802 PATTERN (delay) = gen_blockage ();
4803 INSN_CODE (delay) = -1;
4805 else
4807 /* Use or with rs2 %%g0 instead of mov, so that as/ld can optimize
4808 it into branch if possible. */
4809 output_asm_insn ("or\t%%o7, %%g0, %%g1", operands);
4810 output_asm_insn ("call\t%a0, 0", operands);
4811 output_asm_insn (" or\t%%g1, %%g0, %%o7", operands);
4814 else
4816 /* This is a regular function so we have to restore the register window.
4817 We may have a pending insn for the delay slot, which will be combined
4818 with the 'restore' instruction. */
4820 output_asm_insn ("call\t%a0, 0", operands);
4822 if (delay_slot_filled_p)
4824 rtx delay = NEXT_INSN (insn);
4825 if (! delay)
4826 abort ();
4828 output_restore (PATTERN (delay));
4830 PATTERN (delay) = gen_blockage ();
4831 INSN_CODE (delay) = -1;
4833 else
4834 output_restore (NULL_RTX);
4837 return "";
4840 /* Functions for handling argument passing.
4842 For 32-bit, the first 6 args are normally in registers and the rest are
4843 pushed. Any arg that starts within the first 6 words is at least
4844 partially passed in a register unless its data type forbids.
4846 For 64-bit, the argument registers are laid out as an array of 16 elements
4847 and arguments are added sequentially. The first 6 int args and up to the
4848 first 16 fp args (depending on size) are passed in regs.
4850 Slot Stack Integral Float Float in structure Double Long Double
4851 ---- ----- -------- ----- ------------------ ------ -----------
4852 15 [SP+248] %f31 %f30,%f31 %d30
4853 14 [SP+240] %f29 %f28,%f29 %d28 %q28
4854 13 [SP+232] %f27 %f26,%f27 %d26
4855 12 [SP+224] %f25 %f24,%f25 %d24 %q24
4856 11 [SP+216] %f23 %f22,%f23 %d22
4857 10 [SP+208] %f21 %f20,%f21 %d20 %q20
4858 9 [SP+200] %f19 %f18,%f19 %d18
4859 8 [SP+192] %f17 %f16,%f17 %d16 %q16
4860 7 [SP+184] %f15 %f14,%f15 %d14
4861 6 [SP+176] %f13 %f12,%f13 %d12 %q12
4862 5 [SP+168] %o5 %f11 %f10,%f11 %d10
4863 4 [SP+160] %o4 %f9 %f8,%f9 %d8 %q8
4864 3 [SP+152] %o3 %f7 %f6,%f7 %d6
4865 2 [SP+144] %o2 %f5 %f4,%f5 %d4 %q4
4866 1 [SP+136] %o1 %f3 %f2,%f3 %d2
4867 0 [SP+128] %o0 %f1 %f0,%f1 %d0 %q0
4869 Here SP = %sp if -mno-stack-bias or %sp+stack_bias otherwise.
4871 Integral arguments are always passed as 64-bit quantities appropriately
4872 extended.
4874 Passing of floating point values is handled as follows.
4875 If a prototype is in scope:
4876 If the value is in a named argument (i.e. not a stdarg function or a
4877 value not part of the `...') then the value is passed in the appropriate
4878 fp reg.
4879 If the value is part of the `...' and is passed in one of the first 6
4880 slots then the value is passed in the appropriate int reg.
4881 If the value is part of the `...' and is not passed in one of the first 6
4882 slots then the value is passed in memory.
4883 If a prototype is not in scope:
4884 If the value is one of the first 6 arguments the value is passed in the
4885 appropriate integer reg and the appropriate fp reg.
4886 If the value is not one of the first 6 arguments the value is passed in
4887 the appropriate fp reg and in memory.
4890 Summary of the calling conventions implemented by GCC on SPARC:
4892 32-bit ABI:
4893 size argument return value
4895 small integer <4 int. reg. int. reg.
4896 word 4 int. reg. int. reg.
4897 double word 8 int. reg. int. reg.
4899 _Complex small integer <8 int. reg. int. reg.
4900 _Complex word 8 int. reg. int. reg.
4901 _Complex double word 16 memory int. reg.
4903 vector integer <=8 int. reg. FP reg.
4904 vector integer >8 memory memory
4906 float 4 int. reg. FP reg.
4907 double 8 int. reg. FP reg.
4908 long double 16 memory memory
4910 _Complex float 8 memory FP reg.
4911 _Complex double 16 memory FP reg.
4912 _Complex long double 32 memory FP reg.
4914 vector float <=32 memory FP reg.
4915 vector float >32 memory memory
4917 aggregate any memory memory
4921 64-bit ABI:
4922 size argument return value
4924 small integer <8 int. reg. int. reg.
4925 word 8 int. reg. int. reg.
4926 double word 16 int. reg. int. reg.
4928 _Complex small integer <16 int. reg. int. reg.
4929 _Complex word 16 int. reg. int. reg.
4930 _Complex double word 32 memory int. reg.
4932 vector integer <=16 FP reg. FP reg.
4933 vector integer 16<s<=32 memory FP reg.
4934 vector integer >32 memory memory
4936 float 4 FP reg. FP reg.
4937 double 8 FP reg. FP reg.
4938 long double 16 FP reg. FP reg.
4940 _Complex float 8 FP reg. FP reg.
4941 _Complex double 16 FP reg. FP reg.
4942 _Complex long double 32 memory FP reg.
4944 vector float <=16 FP reg. FP reg.
4945 vector float 16<s<=32 memory FP reg.
4946 vector float >32 memory memory
4948 aggregate <=16 reg. reg.
4949 aggregate 16<s<=32 memory reg.
4950 aggregate >32 memory memory
4954 Note #1: complex floating-point types follow the extended SPARC ABIs as
4955 implemented by the Sun compiler.
4957 Note #2: integral vector types follow the scalar floating-point types
4958 conventions to match what is implemented by the Sun VIS SDK.
4960 Note #3: floating-point vector types follow the complex floating-point
4961 types conventions. */
4964 /* Maximum number of int regs for args. */
4965 #define SPARC_INT_ARG_MAX 6
4966 /* Maximum number of fp regs for args. */
4967 #define SPARC_FP_ARG_MAX 16
4969 #define ROUND_ADVANCE(SIZE) (((SIZE) + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
4971 /* Handle the INIT_CUMULATIVE_ARGS macro.
4972 Initialize a variable CUM of type CUMULATIVE_ARGS
4973 for a call to a function whose data type is FNTYPE.
4974 For a library call, FNTYPE is 0. */
4976 void
4977 init_cumulative_args (struct sparc_args *cum, tree fntype,
4978 rtx libname ATTRIBUTE_UNUSED,
4979 tree fndecl ATTRIBUTE_UNUSED)
4981 cum->words = 0;
4982 cum->prototype_p = fntype && TYPE_ARG_TYPES (fntype);
4983 cum->libcall_p = fntype == 0;
4986 /* Handle the TARGET_PROMOTE_PROTOTYPES target hook.
4987 When a prototype says `char' or `short', really pass an `int'. */
4989 static bool
4990 sparc_promote_prototypes (tree fntype ATTRIBUTE_UNUSED)
4992 return TARGET_ARCH32 ? true : false;
4995 /* Handle the TARGET_STRICT_ARGUMENT_NAMING target hook. */
4997 static bool
4998 sparc_strict_argument_naming (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED)
5000 return TARGET_ARCH64 ? true : false;
5003 /* Scan the record type TYPE and return the following predicates:
5004 - INTREGS_P: the record contains at least one field or sub-field
5005 that is eligible for promotion in integer registers.
5006 - FP_REGS_P: the record contains at least one field or sub-field
5007 that is eligible for promotion in floating-point registers.
5008 - PACKED_P: the record contains at least one field that is packed.
5010 Sub-fields are not taken into account for the PACKED_P predicate. */
5012 static void
5013 scan_record_type (tree type, int *intregs_p, int *fpregs_p, int *packed_p)
5015 tree field;
5017 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5019 if (TREE_CODE (field) == FIELD_DECL)
5021 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
5022 scan_record_type (TREE_TYPE (field), intregs_p, fpregs_p, 0);
5023 else if (FLOAT_TYPE_P (TREE_TYPE (field)) && TARGET_FPU)
5024 *fpregs_p = 1;
5025 else
5026 *intregs_p = 1;
5028 if (packed_p && DECL_PACKED (field))
5029 *packed_p = 1;
5034 /* Compute the slot number to pass an argument in.
5035 Return the slot number or -1 if passing on the stack.
5037 CUM is a variable of type CUMULATIVE_ARGS which gives info about
5038 the preceding args and about the function being called.
5039 MODE is the argument's machine mode.
5040 TYPE is the data type of the argument (as a tree).
5041 This is null for libcalls where that information may
5042 not be available.
5043 NAMED is nonzero if this argument is a named parameter
5044 (otherwise it is an extra parameter matching an ellipsis).
5045 INCOMING_P is zero for FUNCTION_ARG, nonzero for FUNCTION_INCOMING_ARG.
5046 *PREGNO records the register number to use if scalar type.
5047 *PPADDING records the amount of padding needed in words. */
5049 static int
5050 function_arg_slotno (const struct sparc_args *cum, enum machine_mode mode,
5051 tree type, int named, int incoming_p,
5052 int *pregno, int *ppadding)
5054 int regbase = (incoming_p
5055 ? SPARC_INCOMING_INT_ARG_FIRST
5056 : SPARC_OUTGOING_INT_ARG_FIRST);
5057 int slotno = cum->words;
5058 int regno;
5060 *ppadding = 0;
5062 if (type && TREE_ADDRESSABLE (type))
5063 return -1;
5065 if (TARGET_ARCH32
5066 && mode == BLKmode
5067 && type
5068 && TYPE_ALIGN (type) % PARM_BOUNDARY != 0)
5069 return -1;
5071 /* For SPARC64, objects requiring 16-byte alignment get it. */
5072 if (TARGET_ARCH64
5073 && GET_MODE_ALIGNMENT (mode) >= 2 * BITS_PER_WORD
5074 && (slotno & 1) != 0)
5075 slotno++, *ppadding = 1;
5077 switch (GET_MODE_CLASS (mode))
5079 case MODE_FLOAT:
5080 case MODE_COMPLEX_FLOAT:
5081 case MODE_VECTOR_INT:
5082 case MODE_VECTOR_FLOAT:
5083 if (TARGET_ARCH64 && TARGET_FPU && named)
5085 if (slotno >= SPARC_FP_ARG_MAX)
5086 return -1;
5087 regno = SPARC_FP_ARG_FIRST + slotno * 2;
5088 /* Arguments filling only one single FP register are
5089 right-justified in the outer double FP register. */
5090 if (GET_MODE_SIZE (mode) <= 4)
5091 regno++;
5092 break;
5094 /* fallthrough */
5096 case MODE_INT:
5097 case MODE_COMPLEX_INT:
5098 if (slotno >= SPARC_INT_ARG_MAX)
5099 return -1;
5100 regno = regbase + slotno;
5101 break;
5103 case MODE_RANDOM:
5104 if (mode == VOIDmode)
5105 /* MODE is VOIDmode when generating the actual call. */
5106 return -1;
5108 if (mode != BLKmode)
5109 abort ();
5111 /* For SPARC64, objects requiring 16-byte alignment get it. */
5112 if (TARGET_ARCH64
5113 && type
5114 && TYPE_ALIGN (type) >= 2 * BITS_PER_WORD
5115 && (slotno & 1) != 0)
5116 slotno++, *ppadding = 1;
5118 if (TARGET_ARCH32 || (type && TREE_CODE (type) == UNION_TYPE))
5120 if (slotno >= SPARC_INT_ARG_MAX)
5121 return -1;
5122 regno = regbase + slotno;
5124 else /* TARGET_ARCH64 && type && TREE_CODE (type) == RECORD_TYPE */
5126 int intregs_p = 0, fpregs_p = 0, packed_p = 0;
5128 /* First see what kinds of registers we would need. */
5129 scan_record_type (type, &intregs_p, &fpregs_p, &packed_p);
5131 /* The ABI obviously doesn't specify how packed structures
5132 are passed. These are defined to be passed in int regs
5133 if possible, otherwise memory. */
5134 if (packed_p || !named)
5135 fpregs_p = 0, intregs_p = 1;
5137 /* If all arg slots are filled, then must pass on stack. */
5138 if (fpregs_p && slotno >= SPARC_FP_ARG_MAX)
5139 return -1;
5141 /* If there are only int args and all int arg slots are filled,
5142 then must pass on stack. */
5143 if (!fpregs_p && intregs_p && slotno >= SPARC_INT_ARG_MAX)
5144 return -1;
5146 /* Note that even if all int arg slots are filled, fp members may
5147 still be passed in regs if such regs are available.
5148 *PREGNO isn't set because there may be more than one, it's up
5149 to the caller to compute them. */
5150 return slotno;
5152 break;
5154 default :
5155 abort ();
5158 *pregno = regno;
5159 return slotno;
5162 /* Handle recursive register counting for structure field layout. */
5164 struct function_arg_record_value_parms
5166 rtx ret; /* return expression being built. */
5167 int slotno; /* slot number of the argument. */
5168 int named; /* whether the argument is named. */
5169 int regbase; /* regno of the base register. */
5170 int stack; /* 1 if part of the argument is on the stack. */
5171 int intoffset; /* offset of the first pending integer field. */
5172 unsigned int nregs; /* number of words passed in registers. */
5175 static void function_arg_record_value_3
5176 (HOST_WIDE_INT, struct function_arg_record_value_parms *);
5177 static void function_arg_record_value_2
5178 (tree, HOST_WIDE_INT, struct function_arg_record_value_parms *, bool);
5179 static void function_arg_record_value_1
5180 (tree, HOST_WIDE_INT, struct function_arg_record_value_parms *, bool);
5181 static rtx function_arg_record_value (tree, enum machine_mode, int, int, int);
5182 static rtx function_arg_union_value (int, enum machine_mode, int);
5184 /* A subroutine of function_arg_record_value. Traverse the structure
5185 recursively and determine how many registers will be required. */
5187 static void
5188 function_arg_record_value_1 (tree type, HOST_WIDE_INT startbitpos,
5189 struct function_arg_record_value_parms *parms,
5190 bool packed_p)
5192 tree field;
5194 /* We need to compute how many registers are needed so we can
5195 allocate the PARALLEL but before we can do that we need to know
5196 whether there are any packed fields. The ABI obviously doesn't
5197 specify how structures are passed in this case, so they are
5198 defined to be passed in int regs if possible, otherwise memory,
5199 regardless of whether there are fp values present. */
5201 if (! packed_p)
5202 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5204 if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field))
5206 packed_p = true;
5207 break;
5211 /* Compute how many registers we need. */
5212 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5214 if (TREE_CODE (field) == FIELD_DECL)
5216 HOST_WIDE_INT bitpos = startbitpos;
5218 if (DECL_SIZE (field) != 0
5219 && host_integerp (bit_position (field), 1))
5220 bitpos += int_bit_position (field);
5222 /* ??? FIXME: else assume zero offset. */
5224 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
5225 function_arg_record_value_1 (TREE_TYPE (field),
5226 bitpos,
5227 parms,
5228 packed_p);
5229 else if ((FLOAT_TYPE_P (TREE_TYPE (field))
5230 || TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
5231 && TARGET_FPU
5232 && parms->named
5233 && ! packed_p)
5235 if (parms->intoffset != -1)
5237 unsigned int startbit, endbit;
5238 int intslots, this_slotno;
5240 startbit = parms->intoffset & -BITS_PER_WORD;
5241 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
5243 intslots = (endbit - startbit) / BITS_PER_WORD;
5244 this_slotno = parms->slotno + parms->intoffset
5245 / BITS_PER_WORD;
5247 if (intslots > 0 && intslots > SPARC_INT_ARG_MAX - this_slotno)
5249 intslots = MAX (0, SPARC_INT_ARG_MAX - this_slotno);
5250 /* We need to pass this field on the stack. */
5251 parms->stack = 1;
5254 parms->nregs += intslots;
5255 parms->intoffset = -1;
5258 /* There's no need to check this_slotno < SPARC_FP_ARG MAX.
5259 If it wasn't true we wouldn't be here. */
5260 parms->nregs += 1;
5261 if (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE)
5262 parms->nregs += 1;
5264 else
5266 if (parms->intoffset == -1)
5267 parms->intoffset = bitpos;
5273 /* A subroutine of function_arg_record_value. Assign the bits of the
5274 structure between parms->intoffset and bitpos to integer registers. */
5276 static void
5277 function_arg_record_value_3 (HOST_WIDE_INT bitpos,
5278 struct function_arg_record_value_parms *parms)
5280 enum machine_mode mode;
5281 unsigned int regno;
5282 unsigned int startbit, endbit;
5283 int this_slotno, intslots, intoffset;
5284 rtx reg;
5286 if (parms->intoffset == -1)
5287 return;
5289 intoffset = parms->intoffset;
5290 parms->intoffset = -1;
5292 startbit = intoffset & -BITS_PER_WORD;
5293 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
5294 intslots = (endbit - startbit) / BITS_PER_WORD;
5295 this_slotno = parms->slotno + intoffset / BITS_PER_WORD;
5297 intslots = MIN (intslots, SPARC_INT_ARG_MAX - this_slotno);
5298 if (intslots <= 0)
5299 return;
5301 /* If this is the trailing part of a word, only load that much into
5302 the register. Otherwise load the whole register. Note that in
5303 the latter case we may pick up unwanted bits. It's not a problem
5304 at the moment but may wish to revisit. */
5306 if (intoffset % BITS_PER_WORD != 0)
5307 mode = mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
5308 MODE_INT, 0);
5309 else
5310 mode = word_mode;
5312 intoffset /= BITS_PER_UNIT;
5315 regno = parms->regbase + this_slotno;
5316 reg = gen_rtx_REG (mode, regno);
5317 XVECEXP (parms->ret, 0, parms->stack + parms->nregs)
5318 = gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
5320 this_slotno += 1;
5321 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
5322 mode = word_mode;
5323 parms->nregs += 1;
5324 intslots -= 1;
5326 while (intslots > 0);
5329 /* A subroutine of function_arg_record_value. Traverse the structure
5330 recursively and assign bits to floating point registers. Track which
5331 bits in between need integer registers; invoke function_arg_record_value_3
5332 to make that happen. */
5334 static void
5335 function_arg_record_value_2 (tree type, HOST_WIDE_INT startbitpos,
5336 struct function_arg_record_value_parms *parms,
5337 bool packed_p)
5339 tree field;
5341 if (! packed_p)
5342 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5344 if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field))
5346 packed_p = true;
5347 break;
5351 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
5353 if (TREE_CODE (field) == FIELD_DECL)
5355 HOST_WIDE_INT bitpos = startbitpos;
5357 if (DECL_SIZE (field) != 0
5358 && host_integerp (bit_position (field), 1))
5359 bitpos += int_bit_position (field);
5361 /* ??? FIXME: else assume zero offset. */
5363 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
5364 function_arg_record_value_2 (TREE_TYPE (field),
5365 bitpos,
5366 parms,
5367 packed_p);
5368 else if ((FLOAT_TYPE_P (TREE_TYPE (field))
5369 || TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
5370 && TARGET_FPU
5371 && parms->named
5372 && ! packed_p)
5374 int this_slotno = parms->slotno + bitpos / BITS_PER_WORD;
5375 int regno;
5376 enum machine_mode mode = DECL_MODE (field);
5377 rtx reg;
5379 function_arg_record_value_3 (bitpos, parms);
5380 switch (mode)
5382 case SCmode: mode = SFmode; break;
5383 case DCmode: mode = DFmode; break;
5384 case TCmode: mode = TFmode; break;
5385 default: break;
5387 regno = SPARC_FP_ARG_FIRST + this_slotno * 2;
5388 if (GET_MODE_SIZE (mode) <= 4 && (bitpos & 32) != 0)
5389 regno++;
5390 reg = gen_rtx_REG (mode, regno);
5391 XVECEXP (parms->ret, 0, parms->stack + parms->nregs)
5392 = gen_rtx_EXPR_LIST (VOIDmode, reg,
5393 GEN_INT (bitpos / BITS_PER_UNIT));
5394 parms->nregs += 1;
5395 if (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE)
5397 regno += GET_MODE_SIZE (mode) / 4;
5398 reg = gen_rtx_REG (mode, regno);
5399 XVECEXP (parms->ret, 0, parms->stack + parms->nregs)
5400 = gen_rtx_EXPR_LIST (VOIDmode, reg,
5401 GEN_INT ((bitpos + GET_MODE_BITSIZE (mode))
5402 / BITS_PER_UNIT));
5403 parms->nregs += 1;
5406 else
5408 if (parms->intoffset == -1)
5409 parms->intoffset = bitpos;
5415 /* Used by function_arg and function_value to implement the complex
5416 conventions of the 64-bit ABI for passing and returning structures.
5417 Return an expression valid as a return value for the two macros
5418 FUNCTION_ARG and FUNCTION_VALUE.
5420 TYPE is the data type of the argument (as a tree).
5421 This is null for libcalls where that information may
5422 not be available.
5423 MODE is the argument's machine mode.
5424 SLOTNO is the index number of the argument's slot in the parameter array.
5425 NAMED is nonzero if this argument is a named parameter
5426 (otherwise it is an extra parameter matching an ellipsis).
5427 REGBASE is the regno of the base register for the parameter array. */
5429 static rtx
5430 function_arg_record_value (tree type, enum machine_mode mode,
5431 int slotno, int named, int regbase)
5433 HOST_WIDE_INT typesize = int_size_in_bytes (type);
5434 struct function_arg_record_value_parms parms;
5435 unsigned int nregs;
5437 parms.ret = NULL_RTX;
5438 parms.slotno = slotno;
5439 parms.named = named;
5440 parms.regbase = regbase;
5441 parms.stack = 0;
5443 /* Compute how many registers we need. */
5444 parms.nregs = 0;
5445 parms.intoffset = 0;
5446 function_arg_record_value_1 (type, 0, &parms, false);
5448 /* Take into account pending integer fields. */
5449 if (parms.intoffset != -1)
5451 unsigned int startbit, endbit;
5452 int intslots, this_slotno;
5454 startbit = parms.intoffset & -BITS_PER_WORD;
5455 endbit = (typesize*BITS_PER_UNIT + BITS_PER_WORD - 1) & -BITS_PER_WORD;
5456 intslots = (endbit - startbit) / BITS_PER_WORD;
5457 this_slotno = slotno + parms.intoffset / BITS_PER_WORD;
5459 if (intslots > 0 && intslots > SPARC_INT_ARG_MAX - this_slotno)
5461 intslots = MAX (0, SPARC_INT_ARG_MAX - this_slotno);
5462 /* We need to pass this field on the stack. */
5463 parms.stack = 1;
5466 parms.nregs += intslots;
5468 nregs = parms.nregs;
5470 /* Allocate the vector and handle some annoying special cases. */
5471 if (nregs == 0)
5473 /* ??? Empty structure has no value? Duh? */
5474 if (typesize <= 0)
5476 /* Though there's nothing really to store, return a word register
5477 anyway so the rest of gcc doesn't go nuts. Returning a PARALLEL
5478 leads to breakage due to the fact that there are zero bytes to
5479 load. */
5480 return gen_rtx_REG (mode, regbase);
5482 else
5484 /* ??? C++ has structures with no fields, and yet a size. Give up
5485 for now and pass everything back in integer registers. */
5486 nregs = (typesize + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5488 if (nregs + slotno > SPARC_INT_ARG_MAX)
5489 nregs = SPARC_INT_ARG_MAX - slotno;
5491 if (nregs == 0)
5492 abort ();
5494 parms.ret = gen_rtx_PARALLEL (mode, rtvec_alloc (parms.stack + nregs));
5496 /* If at least one field must be passed on the stack, generate
5497 (parallel [(expr_list (nil) ...) ...]) so that all fields will
5498 also be passed on the stack. We can't do much better because the
5499 semantics of FUNCTION_ARG_PARTIAL_NREGS doesn't handle the case
5500 of structures for which the fields passed exclusively in registers
5501 are not at the beginning of the structure. */
5502 if (parms.stack)
5503 XVECEXP (parms.ret, 0, 0)
5504 = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
5506 /* Fill in the entries. */
5507 parms.nregs = 0;
5508 parms.intoffset = 0;
5509 function_arg_record_value_2 (type, 0, &parms, false);
5510 function_arg_record_value_3 (typesize * BITS_PER_UNIT, &parms);
5512 if (parms.nregs != nregs)
5513 abort ();
5515 return parms.ret;
5518 /* Used by function_arg and function_value to implement the conventions
5519 of the 64-bit ABI for passing and returning unions.
5520 Return an expression valid as a return value for the two macros
5521 FUNCTION_ARG and FUNCTION_VALUE.
5523 SIZE is the size in bytes of the union.
5524 MODE is the argument's machine mode.
5525 REGNO is the hard register the union will be passed in. */
5527 static rtx
5528 function_arg_union_value (int size, enum machine_mode mode, int regno)
5530 int nwords = ROUND_ADVANCE (size), i;
5531 rtx regs;
5533 /* Unions are passed left-justified. */
5534 regs = gen_rtx_PARALLEL (mode, rtvec_alloc (nwords));
5536 for (i = 0; i < nwords; i++)
5537 XVECEXP (regs, 0, i)
5538 = gen_rtx_EXPR_LIST (VOIDmode,
5539 gen_rtx_REG (word_mode, regno + i),
5540 GEN_INT (UNITS_PER_WORD * i));
5542 return regs;
5545 /* Handle the FUNCTION_ARG macro.
5546 Determine where to put an argument to a function.
5547 Value is zero to push the argument on the stack,
5548 or a hard register in which to store the argument.
5550 CUM is a variable of type CUMULATIVE_ARGS which gives info about
5551 the preceding args and about the function being called.
5552 MODE is the argument's machine mode.
5553 TYPE is the data type of the argument (as a tree).
5554 This is null for libcalls where that information may
5555 not be available.
5556 NAMED is nonzero if this argument is a named parameter
5557 (otherwise it is an extra parameter matching an ellipsis).
5558 INCOMING_P is zero for FUNCTION_ARG, nonzero for FUNCTION_INCOMING_ARG. */
5561 function_arg (const struct sparc_args *cum, enum machine_mode mode,
5562 tree type, int named, int incoming_p)
5564 int regbase = (incoming_p
5565 ? SPARC_INCOMING_INT_ARG_FIRST
5566 : SPARC_OUTGOING_INT_ARG_FIRST);
5567 int slotno, regno, padding;
5568 rtx reg;
5570 slotno = function_arg_slotno (cum, mode, type, named, incoming_p,
5571 &regno, &padding);
5573 if (slotno == -1)
5574 return 0;
5576 if (TARGET_ARCH32)
5578 reg = gen_rtx_REG (mode, regno);
5579 return reg;
5582 if (type && TREE_CODE (type) == RECORD_TYPE)
5584 /* Structures up to 16 bytes in size are passed in arg slots on the
5585 stack and are promoted to registers where possible. */
5587 if (int_size_in_bytes (type) > 16)
5588 abort (); /* shouldn't get here */
5590 return function_arg_record_value (type, mode, slotno, named, regbase);
5592 else if (type && TREE_CODE (type) == UNION_TYPE)
5594 HOST_WIDE_INT size = int_size_in_bytes (type);
5596 if (size > 16)
5597 abort (); /* shouldn't get here */
5599 return function_arg_union_value (size, mode, regno);
5601 /* v9 fp args in reg slots beyond the int reg slots get passed in regs
5602 but also have the slot allocated for them.
5603 If no prototype is in scope fp values in register slots get passed
5604 in two places, either fp regs and int regs or fp regs and memory. */
5605 else if ((GET_MODE_CLASS (mode) == MODE_FLOAT
5606 || GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5607 || GET_MODE_CLASS (mode) == MODE_VECTOR_INT
5608 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
5609 && SPARC_FP_REG_P (regno))
5611 reg = gen_rtx_REG (mode, regno);
5612 if (cum->prototype_p || cum->libcall_p)
5614 /* "* 2" because fp reg numbers are recorded in 4 byte
5615 quantities. */
5616 #if 0
5617 /* ??? This will cause the value to be passed in the fp reg and
5618 in the stack. When a prototype exists we want to pass the
5619 value in the reg but reserve space on the stack. That's an
5620 optimization, and is deferred [for a bit]. */
5621 if ((regno - SPARC_FP_ARG_FIRST) >= SPARC_INT_ARG_MAX * 2)
5622 return gen_rtx_PARALLEL (mode,
5623 gen_rtvec (2,
5624 gen_rtx_EXPR_LIST (VOIDmode,
5625 NULL_RTX, const0_rtx),
5626 gen_rtx_EXPR_LIST (VOIDmode,
5627 reg, const0_rtx)));
5628 else
5629 #else
5630 /* ??? It seems that passing back a register even when past
5631 the area declared by REG_PARM_STACK_SPACE will allocate
5632 space appropriately, and will not copy the data onto the
5633 stack, exactly as we desire.
5635 This is due to locate_and_pad_parm being called in
5636 expand_call whenever reg_parm_stack_space > 0, which
5637 while beneficial to our example here, would seem to be
5638 in error from what had been intended. Ho hum... -- r~ */
5639 #endif
5640 return reg;
5642 else
5644 rtx v0, v1;
5646 if ((regno - SPARC_FP_ARG_FIRST) < SPARC_INT_ARG_MAX * 2)
5648 int intreg;
5650 /* On incoming, we don't need to know that the value
5651 is passed in %f0 and %i0, and it confuses other parts
5652 causing needless spillage even on the simplest cases. */
5653 if (incoming_p)
5654 return reg;
5656 intreg = (SPARC_OUTGOING_INT_ARG_FIRST
5657 + (regno - SPARC_FP_ARG_FIRST) / 2);
5659 v0 = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
5660 v1 = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_REG (mode, intreg),
5661 const0_rtx);
5662 return gen_rtx_PARALLEL (mode, gen_rtvec (2, v0, v1));
5664 else
5666 v0 = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
5667 v1 = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
5668 return gen_rtx_PARALLEL (mode, gen_rtvec (2, v0, v1));
5672 else
5674 /* Scalar or complex int. */
5675 reg = gen_rtx_REG (mode, regno);
5678 return reg;
5681 /* Handle the FUNCTION_ARG_PARTIAL_NREGS macro.
5682 For an arg passed partly in registers and partly in memory,
5683 this is the number of registers used.
5684 For args passed entirely in registers or entirely in memory, zero.
5686 Any arg that starts in the first 6 regs but won't entirely fit in them
5687 needs partial registers on v8. On v9, structures with integer
5688 values in arg slots 5,6 will be passed in %o5 and SP+176, and complex fp
5689 values that begin in the last fp reg [where "last fp reg" varies with the
5690 mode] will be split between that reg and memory. */
5693 function_arg_partial_nregs (const struct sparc_args *cum,
5694 enum machine_mode mode, tree type, int named)
5696 int slotno, regno, padding;
5698 /* We pass 0 for incoming_p here, it doesn't matter. */
5699 slotno = function_arg_slotno (cum, mode, type, named, 0, &regno, &padding);
5701 if (slotno == -1)
5702 return 0;
5704 if (TARGET_ARCH32)
5706 if ((slotno + (mode == BLKmode
5707 ? ROUND_ADVANCE (int_size_in_bytes (type))
5708 : ROUND_ADVANCE (GET_MODE_SIZE (mode))))
5709 > SPARC_INT_ARG_MAX)
5710 return SPARC_INT_ARG_MAX - slotno;
5712 else
5714 /* We are guaranteed by pass_by_reference that the size of the
5715 argument is not greater than 16 bytes, so we only need to
5716 return 1 if the argument is partially passed in registers. */
5718 if (type && AGGREGATE_TYPE_P (type))
5720 int size = int_size_in_bytes (type);
5722 if (size > UNITS_PER_WORD
5723 && slotno == SPARC_INT_ARG_MAX - 1)
5724 return 1;
5726 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT
5727 || (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
5728 && ! (TARGET_FPU && named)))
5730 /* The complex types are passed as packed types. */
5731 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
5732 && slotno == SPARC_INT_ARG_MAX - 1)
5733 return 1;
5735 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
5737 if ((slotno + GET_MODE_SIZE (mode) / UNITS_PER_WORD)
5738 > SPARC_FP_ARG_MAX)
5739 return 1;
5743 return 0;
5746 /* Return true if the argument should be passed by reference.
5747 !v9: The SPARC ABI stipulates passing struct arguments (of any size) and
5748 quad-precision floats by invisible reference.
5749 v9: Aggregates greater than 16 bytes are passed by reference.
5750 For Pascal, also pass arrays by reference. */
5752 static bool
5753 sparc_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
5754 enum machine_mode mode, tree type,
5755 bool named ATTRIBUTE_UNUSED)
5757 if (TARGET_ARCH32)
5759 return ((type && AGGREGATE_TYPE_P (type))
5760 /* Extended ABI (as implemented by the Sun compiler) says
5761 that all complex floats are passed in memory. */
5762 || mode == SCmode
5763 /* Enforce the 2-word cap for passing arguments in registers.
5764 This affects CDImode, TFmode, DCmode, TCmode and large
5765 vector modes. */
5766 || GET_MODE_SIZE (mode) > 8);
5768 else
5770 return ((type && TREE_CODE (type) == ARRAY_TYPE)
5771 || (type
5772 && AGGREGATE_TYPE_P (type)
5773 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 16)
5774 /* Enforce the 2-word cap for passing arguments in registers.
5775 This affects CTImode, TCmode and large vector modes. */
5776 || GET_MODE_SIZE (mode) > 16);
5780 /* Handle the FUNCTION_ARG_ADVANCE macro.
5781 Update the data in CUM to advance over an argument
5782 of mode MODE and data type TYPE.
5783 TYPE is null for libcalls where that information may not be available. */
5785 void
5786 function_arg_advance (struct sparc_args *cum, enum machine_mode mode,
5787 tree type, int named)
5789 int slotno, regno, padding;
5791 /* We pass 0 for incoming_p here, it doesn't matter. */
5792 slotno = function_arg_slotno (cum, mode, type, named, 0, &regno, &padding);
5794 /* If register required leading padding, add it. */
5795 if (slotno != -1)
5796 cum->words += padding;
5798 if (TARGET_ARCH32)
5800 cum->words += (mode != BLKmode
5801 ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
5802 : ROUND_ADVANCE (int_size_in_bytes (type)));
5804 else
5806 if (type && AGGREGATE_TYPE_P (type))
5808 int size = int_size_in_bytes (type);
5810 if (size <= 8)
5811 ++cum->words;
5812 else if (size <= 16)
5813 cum->words += 2;
5814 else /* passed by reference */
5815 ++cum->words;
5817 else
5819 cum->words += (mode != BLKmode
5820 ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
5821 : ROUND_ADVANCE (int_size_in_bytes (type)));
5826 /* Handle the FUNCTION_ARG_PADDING macro.
5827 For the 64 bit ABI structs are always stored left shifted in their
5828 argument slot. */
5830 enum direction
5831 function_arg_padding (enum machine_mode mode, tree type)
5833 if (TARGET_ARCH64 && type != 0 && AGGREGATE_TYPE_P (type))
5834 return upward;
5836 /* Fall back to the default. */
5837 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
5840 /* Handle the TARGET_RETURN_IN_MEMORY target hook.
5841 Specify whether to return the return value in memory. */
5843 static bool
5844 sparc_return_in_memory (tree type, tree fntype ATTRIBUTE_UNUSED)
5846 if (TARGET_ARCH32)
5847 /* Original SPARC 32-bit ABI says that quad-precision floats
5848 and all structures are returned in memory. Extended ABI
5849 (as implemented by the Sun compiler) says that all complex
5850 floats are returned in registers (8 FP registers at most
5851 for '_Complex long double'). Return all complex integers
5852 in registers (4 at most for '_Complex long long'). */
5853 return (TYPE_MODE (type) == BLKmode
5854 || TYPE_MODE (type) == TFmode
5855 /* Integral vector types follow the scalar FP types conventions. */
5856 || (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_VECTOR_INT
5857 && GET_MODE_SIZE (TYPE_MODE (type)) > 8)
5858 /* FP vector types follow the complex FP types conventions. */
5859 || (GET_MODE_CLASS (TYPE_MODE (type)) == MODE_VECTOR_FLOAT
5860 && GET_MODE_SIZE (TYPE_MODE (type)) > 32));
5861 else
5862 /* Original SPARC 64-bit ABI says that structures and unions
5863 smaller than 32 bytes are returned in registers. Extended
5864 ABI (as implemented by the Sun compiler) says that all complex
5865 floats are returned in registers (8 FP registers at most
5866 for '_Complex long double'). Return all complex integers
5867 in registers (4 at most for '_Complex TItype'). */
5868 return ((TYPE_MODE (type) == BLKmode
5869 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 32)
5870 || GET_MODE_SIZE (TYPE_MODE (type)) > 32);
5873 /* Handle the TARGET_STRUCT_VALUE target hook.
5874 Return where to find the structure return value address. */
5876 static rtx
5877 sparc_struct_value_rtx (tree fndecl ATTRIBUTE_UNUSED, int incoming)
5879 if (TARGET_ARCH64)
5880 return 0;
5881 else
5883 if (incoming)
5884 return gen_rtx_MEM (Pmode, plus_constant (frame_pointer_rtx,
5885 STRUCT_VALUE_OFFSET));
5886 else
5887 return gen_rtx_MEM (Pmode, plus_constant (stack_pointer_rtx,
5888 STRUCT_VALUE_OFFSET));
5892 /* Handle FUNCTION_VALUE, FUNCTION_OUTGOING_VALUE, and LIBCALL_VALUE macros.
5893 For v9, function return values are subject to the same rules as arguments,
5894 except that up to 32 bytes may be returned in registers. */
5897 function_value (tree type, enum machine_mode mode, int incoming_p)
5899 /* Beware that the two values are swapped here wrt function_arg. */
5900 int regbase = (incoming_p
5901 ? SPARC_OUTGOING_INT_ARG_FIRST
5902 : SPARC_INCOMING_INT_ARG_FIRST);
5903 int regno;
5905 if (TARGET_ARCH64 && type)
5907 if (TREE_CODE (type) == RECORD_TYPE)
5909 /* Structures up to 32 bytes in size are passed in registers,
5910 promoted to fp registers where possible. */
5912 if (int_size_in_bytes (type) > 32)
5913 abort (); /* shouldn't get here */
5915 return function_arg_record_value (type, mode, 0, 1, regbase);
5917 else if (TREE_CODE (type) == UNION_TYPE)
5919 HOST_WIDE_INT size = int_size_in_bytes (type);
5921 if (size > 32)
5922 abort (); /* shouldn't get here */
5924 return function_arg_union_value (size, mode, regbase);
5926 else if (AGGREGATE_TYPE_P (type))
5928 /* All other aggregate types are passed in an integer register
5929 in a mode corresponding to the size of the type. */
5930 HOST_WIDE_INT bytes = int_size_in_bytes (type);
5932 if (bytes > 32)
5933 abort (); /* shouldn't get here */
5935 mode = mode_for_size (bytes * BITS_PER_UNIT, MODE_INT, 0);
5937 /* ??? We probably should have made the same ABI change in
5938 3.4.0 as the one we made for unions. The latter was
5939 required by the SCD though, while the former is not
5940 specified, so we favored compatibility and efficiency.
5942 Now we're stuck for aggregates larger than 16 bytes,
5943 because OImode vanished in the meantime. Let's not
5944 try to be unduly clever, and simply follow the ABI
5945 for unions in that case. */
5946 if (mode == BLKmode)
5947 return function_arg_union_value (bytes, mode, regbase);
5949 else if (GET_MODE_CLASS (mode) == MODE_INT
5950 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5951 mode = word_mode;
5954 if (TARGET_FPU && (FLOAT_MODE_P (mode) || VECTOR_MODE_P (mode)))
5955 regno = SPARC_FP_ARG_FIRST;
5956 else
5957 regno = regbase;
5959 return gen_rtx_REG (mode, regno);
5962 /* Do what is necessary for `va_start'. We look at the current function
5963 to determine if stdarg or varargs is used and return the address of
5964 the first unnamed parameter. */
5966 static rtx
5967 sparc_builtin_saveregs (void)
5969 int first_reg = current_function_args_info.words;
5970 rtx address;
5971 int regno;
5973 for (regno = first_reg; regno < SPARC_INT_ARG_MAX; regno++)
5974 emit_move_insn (gen_rtx_MEM (word_mode,
5975 gen_rtx_PLUS (Pmode,
5976 frame_pointer_rtx,
5977 GEN_INT (FIRST_PARM_OFFSET (0)
5978 + (UNITS_PER_WORD
5979 * regno)))),
5980 gen_rtx_REG (word_mode,
5981 SPARC_INCOMING_INT_ARG_FIRST + regno));
5983 address = gen_rtx_PLUS (Pmode,
5984 frame_pointer_rtx,
5985 GEN_INT (FIRST_PARM_OFFSET (0)
5986 + UNITS_PER_WORD * first_reg));
5988 return address;
5991 /* Implement `va_start' for stdarg. */
5993 void
5994 sparc_va_start (tree valist, rtx nextarg)
5996 nextarg = expand_builtin_saveregs ();
5997 std_expand_builtin_va_start (valist, nextarg);
6000 /* Implement `va_arg' for stdarg. */
6002 static tree
6003 sparc_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
6005 HOST_WIDE_INT size, rsize, align;
6006 tree addr, incr;
6007 bool indirect;
6008 tree ptrtype = build_pointer_type (type);
6010 if (pass_by_reference (NULL, TYPE_MODE (type), type, 0))
6012 indirect = true;
6013 size = rsize = UNITS_PER_WORD;
6014 align = 0;
6016 else
6018 indirect = false;
6019 size = int_size_in_bytes (type);
6020 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
6021 align = 0;
6023 if (TARGET_ARCH64)
6025 /* For SPARC64, objects requiring 16-byte alignment get it. */
6026 if (TYPE_ALIGN (type) >= 2 * (unsigned) BITS_PER_WORD)
6027 align = 2 * UNITS_PER_WORD;
6029 /* SPARC-V9 ABI states that structures up to 16 bytes in size
6030 are given whole slots as needed. */
6031 if (AGGREGATE_TYPE_P (type))
6033 if (size == 0)
6034 size = rsize = UNITS_PER_WORD;
6035 else
6036 size = rsize;
6041 incr = valist;
6042 if (align)
6044 incr = fold (build2 (PLUS_EXPR, ptr_type_node, incr,
6045 ssize_int (align - 1)));
6046 incr = fold (build2 (BIT_AND_EXPR, ptr_type_node, incr,
6047 ssize_int (-align)));
6050 gimplify_expr (&incr, pre_p, post_p, is_gimple_val, fb_rvalue);
6051 addr = incr;
6053 if (BYTES_BIG_ENDIAN && size < rsize)
6054 addr = fold (build2 (PLUS_EXPR, ptr_type_node, incr,
6055 ssize_int (rsize - size)));
6057 if (indirect)
6059 addr = fold_convert (build_pointer_type (ptrtype), addr);
6060 addr = build_fold_indirect_ref (addr);
6062 /* If the address isn't aligned properly for the type,
6063 we may need to copy to a temporary.
6064 FIXME: This is inefficient. Usually we can do this
6065 in registers. */
6066 else if (align == 0
6067 && TYPE_ALIGN (type) > BITS_PER_WORD)
6069 tree tmp = create_tmp_var (type, "va_arg_tmp");
6070 tree dest_addr = build_fold_addr_expr (tmp);
6072 tree copy = build_function_call_expr
6073 (implicit_built_in_decls[BUILT_IN_MEMCPY],
6074 tree_cons (NULL_TREE, dest_addr,
6075 tree_cons (NULL_TREE, addr,
6076 tree_cons (NULL_TREE, size_int (rsize),
6077 NULL_TREE))));
6079 gimplify_and_add (copy, pre_p);
6080 addr = dest_addr;
6082 else
6083 addr = fold_convert (ptrtype, addr);
6085 incr = fold (build2 (PLUS_EXPR, ptr_type_node, incr, ssize_int (rsize)));
6086 incr = build2 (MODIFY_EXPR, ptr_type_node, valist, incr);
6087 gimplify_and_add (incr, post_p);
6089 return build_fold_indirect_ref (addr);
6092 /* Return the string to output an unconditional branch to LABEL, which is
6093 the operand number of the label.
6095 DEST is the destination insn (i.e. the label), INSN is the source. */
6097 const char *
6098 output_ubranch (rtx dest, int label, rtx insn)
6100 static char string[64];
6101 bool noop = false;
6102 char *p;
6104 /* TurboSPARC is reported to have problems with
6105 with
6106 foo: b,a foo
6107 i.e. an empty loop with the annul bit set. The workaround is to use
6108 foo: b foo; nop
6109 instead. */
6111 if (! TARGET_V9 && flag_delayed_branch
6112 && (INSN_ADDRESSES (INSN_UID (dest))
6113 == INSN_ADDRESSES (INSN_UID (insn))))
6115 strcpy (string, "b\t");
6116 noop = true;
6118 else
6120 bool v9_form = false;
6122 if (TARGET_V9 && INSN_ADDRESSES_SET_P ())
6124 int delta = (INSN_ADDRESSES (INSN_UID (dest))
6125 - INSN_ADDRESSES (INSN_UID (insn)));
6126 /* Leave some instructions for "slop". */
6127 if (delta >= -260000 && delta < 260000)
6128 v9_form = true;
6131 if (v9_form)
6132 strcpy (string, "ba%*,pt\t%%xcc, ");
6133 else
6134 strcpy (string, "b%*\t");
6137 p = strchr (string, '\0');
6138 *p++ = '%';
6139 *p++ = 'l';
6140 *p++ = '0' + label;
6141 *p++ = '%';
6142 if (noop)
6143 *p++ = '#';
6144 else
6145 *p++ = '(';
6146 *p = '\0';
6148 return string;
6151 /* Return the string to output a conditional branch to LABEL, which is
6152 the operand number of the label. OP is the conditional expression.
6153 XEXP (OP, 0) is assumed to be a condition code register (integer or
6154 floating point) and its mode specifies what kind of comparison we made.
6156 DEST is the destination insn (i.e. the label), INSN is the source.
6158 REVERSED is nonzero if we should reverse the sense of the comparison.
6160 ANNUL is nonzero if we should generate an annulling branch.
6162 NOOP is nonzero if we have to follow this branch by a noop. */
6164 const char *
6165 output_cbranch (rtx op, rtx dest, int label, int reversed, int annul,
6166 int noop, rtx insn)
6168 static char string[64];
6169 enum rtx_code code = GET_CODE (op);
6170 rtx cc_reg = XEXP (op, 0);
6171 enum machine_mode mode = GET_MODE (cc_reg);
6172 const char *labelno, *branch;
6173 int spaces = 8, far;
6174 char *p;
6176 /* v9 branches are limited to +-1MB. If it is too far away,
6177 change
6179 bne,pt %xcc, .LC30
6183 be,pn %xcc, .+12
6185 ba .LC30
6189 fbne,a,pn %fcc2, .LC29
6193 fbe,pt %fcc2, .+16
6195 ba .LC29 */
6197 far = TARGET_V9 && (get_attr_length (insn) >= 3);
6198 if (reversed ^ far)
6200 /* Reversal of FP compares takes care -- an ordered compare
6201 becomes an unordered compare and vice versa. */
6202 if (mode == CCFPmode || mode == CCFPEmode)
6203 code = reverse_condition_maybe_unordered (code);
6204 else
6205 code = reverse_condition (code);
6208 /* Start by writing the branch condition. */
6209 if (mode == CCFPmode || mode == CCFPEmode)
6211 switch (code)
6213 case NE:
6214 branch = "fbne";
6215 break;
6216 case EQ:
6217 branch = "fbe";
6218 break;
6219 case GE:
6220 branch = "fbge";
6221 break;
6222 case GT:
6223 branch = "fbg";
6224 break;
6225 case LE:
6226 branch = "fble";
6227 break;
6228 case LT:
6229 branch = "fbl";
6230 break;
6231 case UNORDERED:
6232 branch = "fbu";
6233 break;
6234 case ORDERED:
6235 branch = "fbo";
6236 break;
6237 case UNGT:
6238 branch = "fbug";
6239 break;
6240 case UNLT:
6241 branch = "fbul";
6242 break;
6243 case UNEQ:
6244 branch = "fbue";
6245 break;
6246 case UNGE:
6247 branch = "fbuge";
6248 break;
6249 case UNLE:
6250 branch = "fbule";
6251 break;
6252 case LTGT:
6253 branch = "fblg";
6254 break;
6256 default:
6257 abort ();
6260 /* ??? !v9: FP branches cannot be preceded by another floating point
6261 insn. Because there is currently no concept of pre-delay slots,
6262 we can fix this only by always emitting a nop before a floating
6263 point branch. */
6265 string[0] = '\0';
6266 if (! TARGET_V9)
6267 strcpy (string, "nop\n\t");
6268 strcat (string, branch);
6270 else
6272 switch (code)
6274 case NE:
6275 branch = "bne";
6276 break;
6277 case EQ:
6278 branch = "be";
6279 break;
6280 case GE:
6281 if (mode == CC_NOOVmode || mode == CCX_NOOVmode)
6282 branch = "bpos";
6283 else
6284 branch = "bge";
6285 break;
6286 case GT:
6287 branch = "bg";
6288 break;
6289 case LE:
6290 branch = "ble";
6291 break;
6292 case LT:
6293 if (mode == CC_NOOVmode || mode == CCX_NOOVmode)
6294 branch = "bneg";
6295 else
6296 branch = "bl";
6297 break;
6298 case GEU:
6299 branch = "bgeu";
6300 break;
6301 case GTU:
6302 branch = "bgu";
6303 break;
6304 case LEU:
6305 branch = "bleu";
6306 break;
6307 case LTU:
6308 branch = "blu";
6309 break;
6311 default:
6312 abort ();
6314 strcpy (string, branch);
6316 spaces -= strlen (branch);
6317 p = strchr (string, '\0');
6319 /* Now add the annulling, the label, and a possible noop. */
6320 if (annul && ! far)
6322 strcpy (p, ",a");
6323 p += 2;
6324 spaces -= 2;
6327 if (TARGET_V9)
6329 rtx note;
6330 int v8 = 0;
6332 if (! far && insn && INSN_ADDRESSES_SET_P ())
6334 int delta = (INSN_ADDRESSES (INSN_UID (dest))
6335 - INSN_ADDRESSES (INSN_UID (insn)));
6336 /* Leave some instructions for "slop". */
6337 if (delta < -260000 || delta >= 260000)
6338 v8 = 1;
6341 if (mode == CCFPmode || mode == CCFPEmode)
6343 static char v9_fcc_labelno[] = "%%fccX, ";
6344 /* Set the char indicating the number of the fcc reg to use. */
6345 v9_fcc_labelno[5] = REGNO (cc_reg) - SPARC_FIRST_V9_FCC_REG + '0';
6346 labelno = v9_fcc_labelno;
6347 if (v8)
6349 if (REGNO (cc_reg) == SPARC_FCC_REG)
6350 labelno = "";
6351 else
6352 abort ();
6355 else if (mode == CCXmode || mode == CCX_NOOVmode)
6357 labelno = "%%xcc, ";
6358 if (v8)
6359 abort ();
6361 else
6363 labelno = "%%icc, ";
6364 if (v8)
6365 labelno = "";
6368 if (*labelno && insn && (note = find_reg_note (insn, REG_BR_PROB, NULL_RTX)))
6370 strcpy (p,
6371 ((INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2) ^ far)
6372 ? ",pt" : ",pn");
6373 p += 3;
6374 spaces -= 3;
6377 else
6378 labelno = "";
6380 if (spaces > 0)
6381 *p++ = '\t';
6382 else
6383 *p++ = ' ';
6384 strcpy (p, labelno);
6385 p = strchr (p, '\0');
6386 if (far)
6388 strcpy (p, ".+12\n\t nop\n\tb\t");
6389 if (annul || noop)
6390 p[3] = '6';
6391 p += 14;
6393 *p++ = '%';
6394 *p++ = 'l';
6395 /* Set the char indicating the number of the operand containing the
6396 label_ref. */
6397 *p++ = label + '0';
6398 *p = '\0';
6399 if (noop)
6400 strcpy (p, "\n\t nop");
6402 return string;
6405 /* Emit a library call comparison between floating point X and Y.
6406 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
6407 TARGET_ARCH64 uses _Qp_* functions, which use pointers to TFmode
6408 values as arguments instead of the TFmode registers themselves,
6409 that's why we cannot call emit_float_lib_cmp. */
6410 void
6411 sparc_emit_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison)
6413 const char *qpfunc;
6414 rtx slot0, slot1, result, tem, tem2;
6415 enum machine_mode mode;
6417 switch (comparison)
6419 case EQ:
6420 qpfunc = (TARGET_ARCH64) ? "_Qp_feq" : "_Q_feq";
6421 break;
6423 case NE:
6424 qpfunc = (TARGET_ARCH64) ? "_Qp_fne" : "_Q_fne";
6425 break;
6427 case GT:
6428 qpfunc = (TARGET_ARCH64) ? "_Qp_fgt" : "_Q_fgt";
6429 break;
6431 case GE:
6432 qpfunc = (TARGET_ARCH64) ? "_Qp_fge" : "_Q_fge";
6433 break;
6435 case LT:
6436 qpfunc = (TARGET_ARCH64) ? "_Qp_flt" : "_Q_flt";
6437 break;
6439 case LE:
6440 qpfunc = (TARGET_ARCH64) ? "_Qp_fle" : "_Q_fle";
6441 break;
6443 case ORDERED:
6444 case UNORDERED:
6445 case UNGT:
6446 case UNLT:
6447 case UNEQ:
6448 case UNGE:
6449 case UNLE:
6450 case LTGT:
6451 qpfunc = (TARGET_ARCH64) ? "_Qp_cmp" : "_Q_cmp";
6452 break;
6454 default:
6455 abort();
6456 break;
6459 if (TARGET_ARCH64)
6461 if (GET_CODE (x) != MEM)
6463 slot0 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode), 0);
6464 emit_insn (gen_rtx_SET (VOIDmode, slot0, x));
6466 else
6467 slot0 = x;
6469 if (GET_CODE (y) != MEM)
6471 slot1 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode), 0);
6472 emit_insn (gen_rtx_SET (VOIDmode, slot1, y));
6474 else
6475 slot1 = y;
6477 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, qpfunc), LCT_NORMAL,
6478 DImode, 2,
6479 XEXP (slot0, 0), Pmode,
6480 XEXP (slot1, 0), Pmode);
6482 mode = DImode;
6484 else
6486 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, qpfunc), LCT_NORMAL,
6487 SImode, 2,
6488 x, TFmode, y, TFmode);
6490 mode = SImode;
6494 /* Immediately move the result of the libcall into a pseudo
6495 register so reload doesn't clobber the value if it needs
6496 the return register for a spill reg. */
6497 result = gen_reg_rtx (mode);
6498 emit_move_insn (result, hard_libcall_value (mode));
6500 switch (comparison)
6502 default:
6503 emit_cmp_insn (result, const0_rtx, NE, NULL_RTX, mode, 0);
6504 break;
6505 case ORDERED:
6506 case UNORDERED:
6507 emit_cmp_insn (result, GEN_INT(3), comparison == UNORDERED ? EQ : NE,
6508 NULL_RTX, mode, 0);
6509 break;
6510 case UNGT:
6511 case UNGE:
6512 emit_cmp_insn (result, const1_rtx,
6513 comparison == UNGT ? GT : NE, NULL_RTX, mode, 0);
6514 break;
6515 case UNLE:
6516 emit_cmp_insn (result, const2_rtx, NE, NULL_RTX, mode, 0);
6517 break;
6518 case UNLT:
6519 tem = gen_reg_rtx (mode);
6520 if (TARGET_ARCH32)
6521 emit_insn (gen_andsi3 (tem, result, const1_rtx));
6522 else
6523 emit_insn (gen_anddi3 (tem, result, const1_rtx));
6524 emit_cmp_insn (tem, const0_rtx, NE, NULL_RTX, mode, 0);
6525 break;
6526 case UNEQ:
6527 case LTGT:
6528 tem = gen_reg_rtx (mode);
6529 if (TARGET_ARCH32)
6530 emit_insn (gen_addsi3 (tem, result, const1_rtx));
6531 else
6532 emit_insn (gen_adddi3 (tem, result, const1_rtx));
6533 tem2 = gen_reg_rtx (mode);
6534 if (TARGET_ARCH32)
6535 emit_insn (gen_andsi3 (tem2, tem, const2_rtx));
6536 else
6537 emit_insn (gen_anddi3 (tem2, tem, const2_rtx));
6538 emit_cmp_insn (tem2, const0_rtx, comparison == UNEQ ? EQ : NE,
6539 NULL_RTX, mode, 0);
6540 break;
6544 /* Generate an unsigned DImode to FP conversion. This is the same code
6545 optabs would emit if we didn't have TFmode patterns. */
6547 void
6548 sparc_emit_floatunsdi (rtx *operands, enum machine_mode mode)
6550 rtx neglab, donelab, i0, i1, f0, in, out;
6552 out = operands[0];
6553 in = force_reg (DImode, operands[1]);
6554 neglab = gen_label_rtx ();
6555 donelab = gen_label_rtx ();
6556 i0 = gen_reg_rtx (DImode);
6557 i1 = gen_reg_rtx (DImode);
6558 f0 = gen_reg_rtx (mode);
6560 emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, DImode, 0, neglab);
6562 emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
6563 emit_jump_insn (gen_jump (donelab));
6564 emit_barrier ();
6566 emit_label (neglab);
6568 emit_insn (gen_lshrdi3 (i0, in, const1_rtx));
6569 emit_insn (gen_anddi3 (i1, in, const1_rtx));
6570 emit_insn (gen_iordi3 (i0, i0, i1));
6571 emit_insn (gen_rtx_SET (VOIDmode, f0, gen_rtx_FLOAT (mode, i0)));
6572 emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
6574 emit_label (donelab);
6577 /* Generate an FP to unsigned DImode conversion. This is the same code
6578 optabs would emit if we didn't have TFmode patterns. */
6580 void
6581 sparc_emit_fixunsdi (rtx *operands, enum machine_mode mode)
6583 rtx neglab, donelab, i0, i1, f0, in, out, limit;
6585 out = operands[0];
6586 in = force_reg (mode, operands[1]);
6587 neglab = gen_label_rtx ();
6588 donelab = gen_label_rtx ();
6589 i0 = gen_reg_rtx (DImode);
6590 i1 = gen_reg_rtx (DImode);
6591 limit = gen_reg_rtx (mode);
6592 f0 = gen_reg_rtx (mode);
6594 emit_move_insn (limit,
6595 CONST_DOUBLE_FROM_REAL_VALUE (
6596 REAL_VALUE_ATOF ("9223372036854775808.0", mode), mode));
6597 emit_cmp_and_jump_insns (in, limit, GE, NULL_RTX, mode, 0, neglab);
6599 emit_insn (gen_rtx_SET (VOIDmode,
6600 out,
6601 gen_rtx_FIX (DImode, gen_rtx_FIX (mode, in))));
6602 emit_jump_insn (gen_jump (donelab));
6603 emit_barrier ();
6605 emit_label (neglab);
6607 emit_insn (gen_rtx_SET (VOIDmode, f0, gen_rtx_MINUS (mode, in, limit)));
6608 emit_insn (gen_rtx_SET (VOIDmode,
6610 gen_rtx_FIX (DImode, gen_rtx_FIX (mode, f0))));
6611 emit_insn (gen_movdi (i1, const1_rtx));
6612 emit_insn (gen_ashldi3 (i1, i1, GEN_INT (63)));
6613 emit_insn (gen_xordi3 (out, i0, i1));
6615 emit_label (donelab);
6618 /* Return the string to output a conditional branch to LABEL, testing
6619 register REG. LABEL is the operand number of the label; REG is the
6620 operand number of the reg. OP is the conditional expression. The mode
6621 of REG says what kind of comparison we made.
6623 DEST is the destination insn (i.e. the label), INSN is the source.
6625 REVERSED is nonzero if we should reverse the sense of the comparison.
6627 ANNUL is nonzero if we should generate an annulling branch.
6629 NOOP is nonzero if we have to follow this branch by a noop. */
6631 const char *
6632 output_v9branch (rtx op, rtx dest, int reg, int label, int reversed,
6633 int annul, int noop, rtx insn)
6635 static char string[64];
6636 enum rtx_code code = GET_CODE (op);
6637 enum machine_mode mode = GET_MODE (XEXP (op, 0));
6638 rtx note;
6639 int far;
6640 char *p;
6642 /* branch on register are limited to +-128KB. If it is too far away,
6643 change
6645 brnz,pt %g1, .LC30
6649 brz,pn %g1, .+12
6651 ba,pt %xcc, .LC30
6655 brgez,a,pn %o1, .LC29
6659 brlz,pt %o1, .+16
6661 ba,pt %xcc, .LC29 */
6663 far = get_attr_length (insn) >= 3;
6665 /* If not floating-point or if EQ or NE, we can just reverse the code. */
6666 if (reversed ^ far)
6667 code = reverse_condition (code);
6669 /* Only 64 bit versions of these instructions exist. */
6670 if (mode != DImode)
6671 abort ();
6673 /* Start by writing the branch condition. */
6675 switch (code)
6677 case NE:
6678 strcpy (string, "brnz");
6679 break;
6681 case EQ:
6682 strcpy (string, "brz");
6683 break;
6685 case GE:
6686 strcpy (string, "brgez");
6687 break;
6689 case LT:
6690 strcpy (string, "brlz");
6691 break;
6693 case LE:
6694 strcpy (string, "brlez");
6695 break;
6697 case GT:
6698 strcpy (string, "brgz");
6699 break;
6701 default:
6702 abort ();
6705 p = strchr (string, '\0');
6707 /* Now add the annulling, reg, label, and nop. */
6708 if (annul && ! far)
6710 strcpy (p, ",a");
6711 p += 2;
6714 if (insn && (note = find_reg_note (insn, REG_BR_PROB, NULL_RTX)))
6716 strcpy (p,
6717 ((INTVAL (XEXP (note, 0)) >= REG_BR_PROB_BASE / 2) ^ far)
6718 ? ",pt" : ",pn");
6719 p += 3;
6722 *p = p < string + 8 ? '\t' : ' ';
6723 p++;
6724 *p++ = '%';
6725 *p++ = '0' + reg;
6726 *p++ = ',';
6727 *p++ = ' ';
6728 if (far)
6730 int veryfar = 1, delta;
6732 if (INSN_ADDRESSES_SET_P ())
6734 delta = (INSN_ADDRESSES (INSN_UID (dest))
6735 - INSN_ADDRESSES (INSN_UID (insn)));
6736 /* Leave some instructions for "slop". */
6737 if (delta >= -260000 && delta < 260000)
6738 veryfar = 0;
6741 strcpy (p, ".+12\n\t nop\n\t");
6742 if (annul || noop)
6743 p[3] = '6';
6744 p += 12;
6745 if (veryfar)
6747 strcpy (p, "b\t");
6748 p += 2;
6750 else
6752 strcpy (p, "ba,pt\t%%xcc, ");
6753 p += 13;
6756 *p++ = '%';
6757 *p++ = 'l';
6758 *p++ = '0' + label;
6759 *p = '\0';
6761 if (noop)
6762 strcpy (p, "\n\t nop");
6764 return string;
6767 /* Return 1, if any of the registers of the instruction are %l[0-7] or %o[0-7].
6768 Such instructions cannot be used in the delay slot of return insn on v9.
6769 If TEST is 0, also rename all %i[0-7] registers to their %o[0-7] counterparts.
6772 static int
6773 epilogue_renumber (register rtx *where, int test)
6775 register const char *fmt;
6776 register int i;
6777 register enum rtx_code code;
6779 if (*where == 0)
6780 return 0;
6782 code = GET_CODE (*where);
6784 switch (code)
6786 case REG:
6787 if (REGNO (*where) >= 8 && REGNO (*where) < 24) /* oX or lX */
6788 return 1;
6789 if (! test && REGNO (*where) >= 24 && REGNO (*where) < 32)
6790 *where = gen_rtx_REG (GET_MODE (*where), OUTGOING_REGNO (REGNO(*where)));
6791 case SCRATCH:
6792 case CC0:
6793 case PC:
6794 case CONST_INT:
6795 case CONST_DOUBLE:
6796 return 0;
6798 /* Do not replace the frame pointer with the stack pointer because
6799 it can cause the delayed instruction to load below the stack.
6800 This occurs when instructions like:
6802 (set (reg/i:SI 24 %i0)
6803 (mem/f:SI (plus:SI (reg/f:SI 30 %fp)
6804 (const_int -20 [0xffffffec])) 0))
6806 are in the return delayed slot. */
6807 case PLUS:
6808 if (GET_CODE (XEXP (*where, 0)) == REG
6809 && REGNO (XEXP (*where, 0)) == HARD_FRAME_POINTER_REGNUM
6810 && (GET_CODE (XEXP (*where, 1)) != CONST_INT
6811 || INTVAL (XEXP (*where, 1)) < SPARC_STACK_BIAS))
6812 return 1;
6813 break;
6815 case MEM:
6816 if (SPARC_STACK_BIAS
6817 && GET_CODE (XEXP (*where, 0)) == REG
6818 && REGNO (XEXP (*where, 0)) == HARD_FRAME_POINTER_REGNUM)
6819 return 1;
6820 break;
6822 default:
6823 break;
6826 fmt = GET_RTX_FORMAT (code);
6828 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6830 if (fmt[i] == 'E')
6832 register int j;
6833 for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
6834 if (epilogue_renumber (&(XVECEXP (*where, i, j)), test))
6835 return 1;
6837 else if (fmt[i] == 'e'
6838 && epilogue_renumber (&(XEXP (*where, i)), test))
6839 return 1;
6841 return 0;
6844 /* Leaf functions and non-leaf functions have different needs. */
6846 static const int
6847 reg_leaf_alloc_order[] = REG_LEAF_ALLOC_ORDER;
6849 static const int
6850 reg_nonleaf_alloc_order[] = REG_ALLOC_ORDER;
6852 static const int *const reg_alloc_orders[] = {
6853 reg_leaf_alloc_order,
6854 reg_nonleaf_alloc_order};
6856 void
6857 order_regs_for_local_alloc (void)
6859 static int last_order_nonleaf = 1;
6861 if (regs_ever_live[15] != last_order_nonleaf)
6863 last_order_nonleaf = !last_order_nonleaf;
6864 memcpy ((char *) reg_alloc_order,
6865 (const char *) reg_alloc_orders[last_order_nonleaf],
6866 FIRST_PSEUDO_REGISTER * sizeof (int));
6870 /* Return 1 if REG and MEM are legitimate enough to allow the various
6871 mem<-->reg splits to be run. */
6874 sparc_splitdi_legitimate (rtx reg, rtx mem)
6876 /* Punt if we are here by mistake. */
6877 if (! reload_completed)
6878 abort ();
6880 /* We must have an offsettable memory reference. */
6881 if (! offsettable_memref_p (mem))
6882 return 0;
6884 /* If we have legitimate args for ldd/std, we do not want
6885 the split to happen. */
6886 if ((REGNO (reg) % 2) == 0
6887 && mem_min_alignment (mem, 8))
6888 return 0;
6890 /* Success. */
6891 return 1;
6894 /* Return 1 if x and y are some kind of REG and they refer to
6895 different hard registers. This test is guaranteed to be
6896 run after reload. */
6899 sparc_absnegfloat_split_legitimate (rtx x, rtx y)
6901 if (GET_CODE (x) != REG)
6902 return 0;
6903 if (GET_CODE (y) != REG)
6904 return 0;
6905 if (REGNO (x) == REGNO (y))
6906 return 0;
6907 return 1;
6910 /* Return 1 if REGNO (reg1) is even and REGNO (reg1) == REGNO (reg2) - 1.
6911 This makes them candidates for using ldd and std insns.
6913 Note reg1 and reg2 *must* be hard registers. */
6916 registers_ok_for_ldd_peep (rtx reg1, rtx reg2)
6918 /* We might have been passed a SUBREG. */
6919 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
6920 return 0;
6922 if (REGNO (reg1) % 2 != 0)
6923 return 0;
6925 /* Integer ldd is deprecated in SPARC V9 */
6926 if (TARGET_V9 && REGNO (reg1) < 32)
6927 return 0;
6929 return (REGNO (reg1) == REGNO (reg2) - 1);
6932 /* Return 1 if the addresses in mem1 and mem2 are suitable for use in
6933 an ldd or std insn.
6935 This can only happen when addr1 and addr2, the addresses in mem1
6936 and mem2, are consecutive memory locations (addr1 + 4 == addr2).
6937 addr1 must also be aligned on a 64-bit boundary.
6939 Also iff dependent_reg_rtx is not null it should not be used to
6940 compute the address for mem1, i.e. we cannot optimize a sequence
6941 like:
6942 ld [%o0], %o0
6943 ld [%o0 + 4], %o1
6945 ldd [%o0], %o0
6946 nor:
6947 ld [%g3 + 4], %g3
6948 ld [%g3], %g2
6950 ldd [%g3], %g2
6952 But, note that the transformation from:
6953 ld [%g2 + 4], %g3
6954 ld [%g2], %g2
6956 ldd [%g2], %g2
6957 is perfectly fine. Thus, the peephole2 patterns always pass us
6958 the destination register of the first load, never the second one.
6960 For stores we don't have a similar problem, so dependent_reg_rtx is
6961 NULL_RTX. */
6964 mems_ok_for_ldd_peep (rtx mem1, rtx mem2, rtx dependent_reg_rtx)
6966 rtx addr1, addr2;
6967 unsigned int reg1;
6968 HOST_WIDE_INT offset1;
6970 /* The mems cannot be volatile. */
6971 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
6972 return 0;
6974 /* MEM1 should be aligned on a 64-bit boundary. */
6975 if (MEM_ALIGN (mem1) < 64)
6976 return 0;
6978 addr1 = XEXP (mem1, 0);
6979 addr2 = XEXP (mem2, 0);
6981 /* Extract a register number and offset (if used) from the first addr. */
6982 if (GET_CODE (addr1) == PLUS)
6984 /* If not a REG, return zero. */
6985 if (GET_CODE (XEXP (addr1, 0)) != REG)
6986 return 0;
6987 else
6989 reg1 = REGNO (XEXP (addr1, 0));
6990 /* The offset must be constant! */
6991 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
6992 return 0;
6993 offset1 = INTVAL (XEXP (addr1, 1));
6996 else if (GET_CODE (addr1) != REG)
6997 return 0;
6998 else
7000 reg1 = REGNO (addr1);
7001 /* This was a simple (mem (reg)) expression. Offset is 0. */
7002 offset1 = 0;
7005 /* Make sure the second address is a (mem (plus (reg) (const_int). */
7006 if (GET_CODE (addr2) != PLUS)
7007 return 0;
7009 if (GET_CODE (XEXP (addr2, 0)) != REG
7010 || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
7011 return 0;
7013 if (reg1 != REGNO (XEXP (addr2, 0)))
7014 return 0;
7016 if (dependent_reg_rtx != NULL_RTX && reg1 == REGNO (dependent_reg_rtx))
7017 return 0;
7019 /* The first offset must be evenly divisible by 8 to ensure the
7020 address is 64 bit aligned. */
7021 if (offset1 % 8 != 0)
7022 return 0;
7024 /* The offset for the second addr must be 4 more than the first addr. */
7025 if (INTVAL (XEXP (addr2, 1)) != offset1 + 4)
7026 return 0;
7028 /* All the tests passed. addr1 and addr2 are valid for ldd and std
7029 instructions. */
7030 return 1;
7033 /* Return 1 if reg is a pseudo, or is the first register in
7034 a hard register pair. This makes it a candidate for use in
7035 ldd and std insns. */
7038 register_ok_for_ldd (rtx reg)
7040 /* We might have been passed a SUBREG. */
7041 if (GET_CODE (reg) != REG)
7042 return 0;
7044 if (REGNO (reg) < FIRST_PSEUDO_REGISTER)
7045 return (REGNO (reg) % 2 == 0);
7046 else
7047 return 1;
7050 /* Print operand X (an rtx) in assembler syntax to file FILE.
7051 CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
7052 For `%' followed by punctuation, CODE is the punctuation and X is null. */
7054 void
7055 print_operand (FILE *file, rtx x, int code)
7057 switch (code)
7059 case '#':
7060 /* Output a 'nop' if there's nothing for the delay slot. */
7061 if (dbr_sequence_length () == 0)
7062 fputs ("\n\t nop", file);
7063 return;
7064 case '*':
7065 /* Output an annul flag if there's nothing for the delay slot and we
7066 are optimizing. This is always used with '(' below. */
7067 /* Sun OS 4.1.1 dbx can't handle an annulled unconditional branch;
7068 this is a dbx bug. So, we only do this when optimizing. */
7069 /* On UltraSPARC, a branch in a delay slot causes a pipeline flush.
7070 Always emit a nop in case the next instruction is a branch. */
7071 if (dbr_sequence_length () == 0
7072 && (optimize && (int)sparc_cpu < PROCESSOR_V9))
7073 fputs (",a", file);
7074 return;
7075 case '(':
7076 /* Output a 'nop' if there's nothing for the delay slot and we are
7077 not optimizing. This is always used with '*' above. */
7078 if (dbr_sequence_length () == 0
7079 && ! (optimize && (int)sparc_cpu < PROCESSOR_V9))
7080 fputs ("\n\t nop", file);
7081 return;
7082 case '_':
7083 /* Output the Embedded Medium/Anywhere code model base register. */
7084 fputs (EMBMEDANY_BASE_REG, file);
7085 return;
7086 case '@':
7087 /* Print out what we are using as the frame pointer. This might
7088 be %fp, or might be %sp+offset. */
7089 /* ??? What if offset is too big? Perhaps the caller knows it isn't? */
7090 fprintf (file, "%s+"HOST_WIDE_INT_PRINT_DEC,
7091 reg_names[REGNO (frame_base_reg)], frame_base_offset);
7092 return;
7093 case '&':
7094 /* Print some local dynamic TLS name. */
7095 assemble_name (file, get_some_local_dynamic_name ());
7096 return;
7097 case 'Y':
7098 /* Adjust the operand to take into account a RESTORE operation. */
7099 if (GET_CODE (x) == CONST_INT)
7100 break;
7101 else if (GET_CODE (x) != REG)
7102 output_operand_lossage ("invalid %%Y operand");
7103 else if (REGNO (x) < 8)
7104 fputs (reg_names[REGNO (x)], file);
7105 else if (REGNO (x) >= 24 && REGNO (x) < 32)
7106 fputs (reg_names[REGNO (x)-16], file);
7107 else
7108 output_operand_lossage ("invalid %%Y operand");
7109 return;
7110 case 'L':
7111 /* Print out the low order register name of a register pair. */
7112 if (WORDS_BIG_ENDIAN)
7113 fputs (reg_names[REGNO (x)+1], file);
7114 else
7115 fputs (reg_names[REGNO (x)], file);
7116 return;
7117 case 'H':
7118 /* Print out the high order register name of a register pair. */
7119 if (WORDS_BIG_ENDIAN)
7120 fputs (reg_names[REGNO (x)], file);
7121 else
7122 fputs (reg_names[REGNO (x)+1], file);
7123 return;
7124 case 'R':
7125 /* Print out the second register name of a register pair or quad.
7126 I.e., R (%o0) => %o1. */
7127 fputs (reg_names[REGNO (x)+1], file);
7128 return;
7129 case 'S':
7130 /* Print out the third register name of a register quad.
7131 I.e., S (%o0) => %o2. */
7132 fputs (reg_names[REGNO (x)+2], file);
7133 return;
7134 case 'T':
7135 /* Print out the fourth register name of a register quad.
7136 I.e., T (%o0) => %o3. */
7137 fputs (reg_names[REGNO (x)+3], file);
7138 return;
7139 case 'x':
7140 /* Print a condition code register. */
7141 if (REGNO (x) == SPARC_ICC_REG)
7143 /* We don't handle CC[X]_NOOVmode because they're not supposed
7144 to occur here. */
7145 if (GET_MODE (x) == CCmode)
7146 fputs ("%icc", file);
7147 else if (GET_MODE (x) == CCXmode)
7148 fputs ("%xcc", file);
7149 else
7150 abort ();
7152 else
7153 /* %fccN register */
7154 fputs (reg_names[REGNO (x)], file);
7155 return;
7156 case 'm':
7157 /* Print the operand's address only. */
7158 output_address (XEXP (x, 0));
7159 return;
7160 case 'r':
7161 /* In this case we need a register. Use %g0 if the
7162 operand is const0_rtx. */
7163 if (x == const0_rtx
7164 || (GET_MODE (x) != VOIDmode && x == CONST0_RTX (GET_MODE (x))))
7166 fputs ("%g0", file);
7167 return;
7169 else
7170 break;
7172 case 'A':
7173 switch (GET_CODE (x))
7175 case IOR: fputs ("or", file); break;
7176 case AND: fputs ("and", file); break;
7177 case XOR: fputs ("xor", file); break;
7178 default: output_operand_lossage ("invalid %%A operand");
7180 return;
7182 case 'B':
7183 switch (GET_CODE (x))
7185 case IOR: fputs ("orn", file); break;
7186 case AND: fputs ("andn", file); break;
7187 case XOR: fputs ("xnor", file); break;
7188 default: output_operand_lossage ("invalid %%B operand");
7190 return;
7192 /* These are used by the conditional move instructions. */
7193 case 'c' :
7194 case 'C':
7196 enum rtx_code rc = GET_CODE (x);
7198 if (code == 'c')
7200 enum machine_mode mode = GET_MODE (XEXP (x, 0));
7201 if (mode == CCFPmode || mode == CCFPEmode)
7202 rc = reverse_condition_maybe_unordered (GET_CODE (x));
7203 else
7204 rc = reverse_condition (GET_CODE (x));
7206 switch (rc)
7208 case NE: fputs ("ne", file); break;
7209 case EQ: fputs ("e", file); break;
7210 case GE: fputs ("ge", file); break;
7211 case GT: fputs ("g", file); break;
7212 case LE: fputs ("le", file); break;
7213 case LT: fputs ("l", file); break;
7214 case GEU: fputs ("geu", file); break;
7215 case GTU: fputs ("gu", file); break;
7216 case LEU: fputs ("leu", file); break;
7217 case LTU: fputs ("lu", file); break;
7218 case LTGT: fputs ("lg", file); break;
7219 case UNORDERED: fputs ("u", file); break;
7220 case ORDERED: fputs ("o", file); break;
7221 case UNLT: fputs ("ul", file); break;
7222 case UNLE: fputs ("ule", file); break;
7223 case UNGT: fputs ("ug", file); break;
7224 case UNGE: fputs ("uge", file); break;
7225 case UNEQ: fputs ("ue", file); break;
7226 default: output_operand_lossage (code == 'c'
7227 ? "invalid %%c operand"
7228 : "invalid %%C operand");
7230 return;
7233 /* These are used by the movr instruction pattern. */
7234 case 'd':
7235 case 'D':
7237 enum rtx_code rc = (code == 'd'
7238 ? reverse_condition (GET_CODE (x))
7239 : GET_CODE (x));
7240 switch (rc)
7242 case NE: fputs ("ne", file); break;
7243 case EQ: fputs ("e", file); break;
7244 case GE: fputs ("gez", file); break;
7245 case LT: fputs ("lz", file); break;
7246 case LE: fputs ("lez", file); break;
7247 case GT: fputs ("gz", file); break;
7248 default: output_operand_lossage (code == 'd'
7249 ? "invalid %%d operand"
7250 : "invalid %%D operand");
7252 return;
7255 case 'b':
7257 /* Print a sign-extended character. */
7258 int i = trunc_int_for_mode (INTVAL (x), QImode);
7259 fprintf (file, "%d", i);
7260 return;
7263 case 'f':
7264 /* Operand must be a MEM; write its address. */
7265 if (GET_CODE (x) != MEM)
7266 output_operand_lossage ("invalid %%f operand");
7267 output_address (XEXP (x, 0));
7268 return;
7270 case 's':
7272 /* Print a sign-extended 32-bit value. */
7273 HOST_WIDE_INT i;
7274 if (GET_CODE(x) == CONST_INT)
7275 i = INTVAL (x);
7276 else if (GET_CODE(x) == CONST_DOUBLE)
7277 i = CONST_DOUBLE_LOW (x);
7278 else
7280 output_operand_lossage ("invalid %%s operand");
7281 return;
7283 i = trunc_int_for_mode (i, SImode);
7284 fprintf (file, HOST_WIDE_INT_PRINT_DEC, i);
7285 return;
7288 case 0:
7289 /* Do nothing special. */
7290 break;
7292 default:
7293 /* Undocumented flag. */
7294 output_operand_lossage ("invalid operand output code");
7297 if (GET_CODE (x) == REG)
7298 fputs (reg_names[REGNO (x)], file);
7299 else if (GET_CODE (x) == MEM)
7301 fputc ('[', file);
7302 /* Poor Sun assembler doesn't understand absolute addressing. */
7303 if (CONSTANT_P (XEXP (x, 0)))
7304 fputs ("%g0+", file);
7305 output_address (XEXP (x, 0));
7306 fputc (']', file);
7308 else if (GET_CODE (x) == HIGH)
7310 fputs ("%hi(", file);
7311 output_addr_const (file, XEXP (x, 0));
7312 fputc (')', file);
7314 else if (GET_CODE (x) == LO_SUM)
7316 print_operand (file, XEXP (x, 0), 0);
7317 if (TARGET_CM_MEDMID)
7318 fputs ("+%l44(", file);
7319 else
7320 fputs ("+%lo(", file);
7321 output_addr_const (file, XEXP (x, 1));
7322 fputc (')', file);
7324 else if (GET_CODE (x) == CONST_DOUBLE
7325 && (GET_MODE (x) == VOIDmode
7326 || GET_MODE_CLASS (GET_MODE (x)) == MODE_INT))
7328 if (CONST_DOUBLE_HIGH (x) == 0)
7329 fprintf (file, "%u", (unsigned int) CONST_DOUBLE_LOW (x));
7330 else if (CONST_DOUBLE_HIGH (x) == -1
7331 && CONST_DOUBLE_LOW (x) < 0)
7332 fprintf (file, "%d", (int) CONST_DOUBLE_LOW (x));
7333 else
7334 output_operand_lossage ("long long constant not a valid immediate operand");
7336 else if (GET_CODE (x) == CONST_DOUBLE)
7337 output_operand_lossage ("floating point constant not a valid immediate operand");
7338 else { output_addr_const (file, x); }
7341 /* Target hook for assembling integer objects. The sparc version has
7342 special handling for aligned DI-mode objects. */
7344 static bool
7345 sparc_assemble_integer (rtx x, unsigned int size, int aligned_p)
7347 /* ??? We only output .xword's for symbols and only then in environments
7348 where the assembler can handle them. */
7349 if (aligned_p && size == 8
7350 && (GET_CODE (x) != CONST_INT && GET_CODE (x) != CONST_DOUBLE))
7352 if (TARGET_V9)
7354 assemble_integer_with_op ("\t.xword\t", x);
7355 return true;
7357 else
7359 assemble_aligned_integer (4, const0_rtx);
7360 assemble_aligned_integer (4, x);
7361 return true;
7364 return default_assemble_integer (x, size, aligned_p);
7367 /* Return the value of a code used in the .proc pseudo-op that says
7368 what kind of result this function returns. For non-C types, we pick
7369 the closest C type. */
7371 #ifndef SHORT_TYPE_SIZE
7372 #define SHORT_TYPE_SIZE (BITS_PER_UNIT * 2)
7373 #endif
7375 #ifndef INT_TYPE_SIZE
7376 #define INT_TYPE_SIZE BITS_PER_WORD
7377 #endif
7379 #ifndef LONG_TYPE_SIZE
7380 #define LONG_TYPE_SIZE BITS_PER_WORD
7381 #endif
7383 #ifndef LONG_LONG_TYPE_SIZE
7384 #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
7385 #endif
7387 #ifndef FLOAT_TYPE_SIZE
7388 #define FLOAT_TYPE_SIZE BITS_PER_WORD
7389 #endif
7391 #ifndef DOUBLE_TYPE_SIZE
7392 #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
7393 #endif
7395 #ifndef LONG_DOUBLE_TYPE_SIZE
7396 #define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
7397 #endif
7399 unsigned long
7400 sparc_type_code (register tree type)
7402 register unsigned long qualifiers = 0;
7403 register unsigned shift;
7405 /* Only the first 30 bits of the qualifier are valid. We must refrain from
7406 setting more, since some assemblers will give an error for this. Also,
7407 we must be careful to avoid shifts of 32 bits or more to avoid getting
7408 unpredictable results. */
7410 for (shift = 6; shift < 30; shift += 2, type = TREE_TYPE (type))
7412 switch (TREE_CODE (type))
7414 case ERROR_MARK:
7415 return qualifiers;
7417 case ARRAY_TYPE:
7418 qualifiers |= (3 << shift);
7419 break;
7421 case FUNCTION_TYPE:
7422 case METHOD_TYPE:
7423 qualifiers |= (2 << shift);
7424 break;
7426 case POINTER_TYPE:
7427 case REFERENCE_TYPE:
7428 case OFFSET_TYPE:
7429 qualifiers |= (1 << shift);
7430 break;
7432 case RECORD_TYPE:
7433 return (qualifiers | 8);
7435 case UNION_TYPE:
7436 case QUAL_UNION_TYPE:
7437 return (qualifiers | 9);
7439 case ENUMERAL_TYPE:
7440 return (qualifiers | 10);
7442 case VOID_TYPE:
7443 return (qualifiers | 16);
7445 case INTEGER_TYPE:
7446 /* If this is a range type, consider it to be the underlying
7447 type. */
7448 if (TREE_TYPE (type) != 0)
7449 break;
7451 /* Carefully distinguish all the standard types of C,
7452 without messing up if the language is not C. We do this by
7453 testing TYPE_PRECISION and TYPE_UNSIGNED. The old code used to
7454 look at both the names and the above fields, but that's redundant.
7455 Any type whose size is between two C types will be considered
7456 to be the wider of the two types. Also, we do not have a
7457 special code to use for "long long", so anything wider than
7458 long is treated the same. Note that we can't distinguish
7459 between "int" and "long" in this code if they are the same
7460 size, but that's fine, since neither can the assembler. */
7462 if (TYPE_PRECISION (type) <= CHAR_TYPE_SIZE)
7463 return (qualifiers | (TYPE_UNSIGNED (type) ? 12 : 2));
7465 else if (TYPE_PRECISION (type) <= SHORT_TYPE_SIZE)
7466 return (qualifiers | (TYPE_UNSIGNED (type) ? 13 : 3));
7468 else if (TYPE_PRECISION (type) <= INT_TYPE_SIZE)
7469 return (qualifiers | (TYPE_UNSIGNED (type) ? 14 : 4));
7471 else
7472 return (qualifiers | (TYPE_UNSIGNED (type) ? 15 : 5));
7474 case REAL_TYPE:
7475 /* If this is a range type, consider it to be the underlying
7476 type. */
7477 if (TREE_TYPE (type) != 0)
7478 break;
7480 /* Carefully distinguish all the standard types of C,
7481 without messing up if the language is not C. */
7483 if (TYPE_PRECISION (type) == FLOAT_TYPE_SIZE)
7484 return (qualifiers | 6);
7486 else
7487 return (qualifiers | 7);
7489 case COMPLEX_TYPE: /* GNU Fortran COMPLEX type. */
7490 /* ??? We need to distinguish between double and float complex types,
7491 but I don't know how yet because I can't reach this code from
7492 existing front-ends. */
7493 return (qualifiers | 7); /* Who knows? */
7495 case VECTOR_TYPE:
7496 case CHAR_TYPE: /* GNU Pascal CHAR type. Not used in C. */
7497 case BOOLEAN_TYPE: /* GNU Fortran BOOLEAN type. */
7498 case FILE_TYPE: /* GNU Pascal FILE type. */
7499 case SET_TYPE: /* GNU Pascal SET type. */
7500 case LANG_TYPE: /* ? */
7501 return qualifiers;
7503 default:
7504 abort (); /* Not a type! */
7508 return qualifiers;
7511 /* Nested function support. */
7513 /* Emit RTL insns to initialize the variable parts of a trampoline.
7514 FNADDR is an RTX for the address of the function's pure code.
7515 CXT is an RTX for the static chain value for the function.
7517 This takes 16 insns: 2 shifts & 2 ands (to split up addresses), 4 sethi
7518 (to load in opcodes), 4 iors (to merge address and opcodes), and 4 writes
7519 (to store insns). This is a bit excessive. Perhaps a different
7520 mechanism would be better here.
7522 Emit enough FLUSH insns to synchronize the data and instruction caches. */
7524 void
7525 sparc_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
7527 /* SPARC 32-bit trampoline:
7529 sethi %hi(fn), %g1
7530 sethi %hi(static), %g2
7531 jmp %g1+%lo(fn)
7532 or %g2, %lo(static), %g2
7534 SETHI i,r = 00rr rrr1 00ii iiii iiii iiii iiii iiii
7535 JMPL r+i,d = 10dd ddd1 1100 0rrr rr1i iiii iiii iiii
7538 emit_move_insn
7539 (gen_rtx_MEM (SImode, plus_constant (tramp, 0)),
7540 expand_binop (SImode, ior_optab,
7541 expand_shift (RSHIFT_EXPR, SImode, fnaddr,
7542 size_int (10), 0, 1),
7543 GEN_INT (trunc_int_for_mode (0x03000000, SImode)),
7544 NULL_RTX, 1, OPTAB_DIRECT));
7546 emit_move_insn
7547 (gen_rtx_MEM (SImode, plus_constant (tramp, 4)),
7548 expand_binop (SImode, ior_optab,
7549 expand_shift (RSHIFT_EXPR, SImode, cxt,
7550 size_int (10), 0, 1),
7551 GEN_INT (trunc_int_for_mode (0x05000000, SImode)),
7552 NULL_RTX, 1, OPTAB_DIRECT));
7554 emit_move_insn
7555 (gen_rtx_MEM (SImode, plus_constant (tramp, 8)),
7556 expand_binop (SImode, ior_optab,
7557 expand_and (SImode, fnaddr, GEN_INT (0x3ff), NULL_RTX),
7558 GEN_INT (trunc_int_for_mode (0x81c06000, SImode)),
7559 NULL_RTX, 1, OPTAB_DIRECT));
7561 emit_move_insn
7562 (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
7563 expand_binop (SImode, ior_optab,
7564 expand_and (SImode, cxt, GEN_INT (0x3ff), NULL_RTX),
7565 GEN_INT (trunc_int_for_mode (0x8410a000, SImode)),
7566 NULL_RTX, 1, OPTAB_DIRECT));
7568 /* On UltraSPARC a flush flushes an entire cache line. The trampoline is
7569 aligned on a 16 byte boundary so one flush clears it all. */
7570 emit_insn (gen_flush (validize_mem (gen_rtx_MEM (SImode, tramp))));
7571 if (sparc_cpu != PROCESSOR_ULTRASPARC
7572 && sparc_cpu != PROCESSOR_ULTRASPARC3)
7573 emit_insn (gen_flush (validize_mem (gen_rtx_MEM (SImode,
7574 plus_constant (tramp, 8)))));
7576 /* Call __enable_execute_stack after writing onto the stack to make sure
7577 the stack address is accessible. */
7578 #ifdef TRANSFER_FROM_TRAMPOLINE
7579 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
7580 LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
7581 #endif
7585 /* The 64-bit version is simpler because it makes more sense to load the
7586 values as "immediate" data out of the trampoline. It's also easier since
7587 we can read the PC without clobbering a register. */
7589 void
7590 sparc64_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
7592 /* SPARC 64-bit trampoline:
7594 rd %pc, %g1
7595 ldx [%g1+24], %g5
7596 jmp %g5
7597 ldx [%g1+16], %g5
7598 +16 bytes data
7601 emit_move_insn (gen_rtx_MEM (SImode, tramp),
7602 GEN_INT (trunc_int_for_mode (0x83414000, SImode)));
7603 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 4)),
7604 GEN_INT (trunc_int_for_mode (0xca586018, SImode)));
7605 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 8)),
7606 GEN_INT (trunc_int_for_mode (0x81c14000, SImode)));
7607 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
7608 GEN_INT (trunc_int_for_mode (0xca586010, SImode)));
7609 emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 16)), cxt);
7610 emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 24)), fnaddr);
7611 emit_insn (gen_flushdi (validize_mem (gen_rtx_MEM (DImode, tramp))));
7613 if (sparc_cpu != PROCESSOR_ULTRASPARC
7614 && sparc_cpu != PROCESSOR_ULTRASPARC3)
7615 emit_insn (gen_flushdi (validize_mem (gen_rtx_MEM (DImode, plus_constant (tramp, 8)))));
7617 /* Call __enable_execute_stack after writing onto the stack to make sure
7618 the stack address is accessible. */
7619 #ifdef TRANSFER_FROM_TRAMPOLINE
7620 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
7621 LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
7622 #endif
7625 /* Adjust the cost of a scheduling dependency. Return the new cost of
7626 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
7628 static int
7629 supersparc_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
7631 enum attr_type insn_type;
7633 if (! recog_memoized (insn))
7634 return 0;
7636 insn_type = get_attr_type (insn);
7638 if (REG_NOTE_KIND (link) == 0)
7640 /* Data dependency; DEP_INSN writes a register that INSN reads some
7641 cycles later. */
7643 /* if a load, then the dependence must be on the memory address;
7644 add an extra "cycle". Note that the cost could be two cycles
7645 if the reg was written late in an instruction group; we ca not tell
7646 here. */
7647 if (insn_type == TYPE_LOAD || insn_type == TYPE_FPLOAD)
7648 return cost + 3;
7650 /* Get the delay only if the address of the store is the dependence. */
7651 if (insn_type == TYPE_STORE || insn_type == TYPE_FPSTORE)
7653 rtx pat = PATTERN(insn);
7654 rtx dep_pat = PATTERN (dep_insn);
7656 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
7657 return cost; /* This should not happen! */
7659 /* The dependency between the two instructions was on the data that
7660 is being stored. Assume that this implies that the address of the
7661 store is not dependent. */
7662 if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
7663 return cost;
7665 return cost + 3; /* An approximation. */
7668 /* A shift instruction cannot receive its data from an instruction
7669 in the same cycle; add a one cycle penalty. */
7670 if (insn_type == TYPE_SHIFT)
7671 return cost + 3; /* Split before cascade into shift. */
7673 else
7675 /* Anti- or output- dependency; DEP_INSN reads/writes a register that
7676 INSN writes some cycles later. */
7678 /* These are only significant for the fpu unit; writing a fp reg before
7679 the fpu has finished with it stalls the processor. */
7681 /* Reusing an integer register causes no problems. */
7682 if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
7683 return 0;
7686 return cost;
7689 static int
7690 hypersparc_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
7692 enum attr_type insn_type, dep_type;
7693 rtx pat = PATTERN(insn);
7694 rtx dep_pat = PATTERN (dep_insn);
7696 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
7697 return cost;
7699 insn_type = get_attr_type (insn);
7700 dep_type = get_attr_type (dep_insn);
7702 switch (REG_NOTE_KIND (link))
7704 case 0:
7705 /* Data dependency; DEP_INSN writes a register that INSN reads some
7706 cycles later. */
7708 switch (insn_type)
7710 case TYPE_STORE:
7711 case TYPE_FPSTORE:
7712 /* Get the delay iff the address of the store is the dependence. */
7713 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
7714 return cost;
7716 if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
7717 return cost;
7718 return cost + 3;
7720 case TYPE_LOAD:
7721 case TYPE_SLOAD:
7722 case TYPE_FPLOAD:
7723 /* If a load, then the dependence must be on the memory address. If
7724 the addresses aren't equal, then it might be a false dependency */
7725 if (dep_type == TYPE_STORE || dep_type == TYPE_FPSTORE)
7727 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET
7728 || GET_CODE (SET_DEST (dep_pat)) != MEM
7729 || GET_CODE (SET_SRC (pat)) != MEM
7730 || ! rtx_equal_p (XEXP (SET_DEST (dep_pat), 0),
7731 XEXP (SET_SRC (pat), 0)))
7732 return cost + 2;
7734 return cost + 8;
7736 break;
7738 case TYPE_BRANCH:
7739 /* Compare to branch latency is 0. There is no benefit from
7740 separating compare and branch. */
7741 if (dep_type == TYPE_COMPARE)
7742 return 0;
7743 /* Floating point compare to branch latency is less than
7744 compare to conditional move. */
7745 if (dep_type == TYPE_FPCMP)
7746 return cost - 1;
7747 break;
7748 default:
7749 break;
7751 break;
7753 case REG_DEP_ANTI:
7754 /* Anti-dependencies only penalize the fpu unit. */
7755 if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
7756 return 0;
7757 break;
7759 default:
7760 break;
7763 return cost;
7766 static int
7767 sparc_adjust_cost(rtx insn, rtx link, rtx dep, int cost)
7769 switch (sparc_cpu)
7771 case PROCESSOR_SUPERSPARC:
7772 cost = supersparc_adjust_cost (insn, link, dep, cost);
7773 break;
7774 case PROCESSOR_HYPERSPARC:
7775 case PROCESSOR_SPARCLITE86X:
7776 cost = hypersparc_adjust_cost (insn, link, dep, cost);
7777 break;
7778 default:
7779 break;
7781 return cost;
7784 static void
7785 sparc_sched_init (FILE *dump ATTRIBUTE_UNUSED,
7786 int sched_verbose ATTRIBUTE_UNUSED,
7787 int max_ready ATTRIBUTE_UNUSED)
7791 static int
7792 sparc_use_sched_lookahead (void)
7794 if (sparc_cpu == PROCESSOR_ULTRASPARC
7795 || sparc_cpu == PROCESSOR_ULTRASPARC3)
7796 return 4;
7797 if ((1 << sparc_cpu) &
7798 ((1 << PROCESSOR_SUPERSPARC) | (1 << PROCESSOR_HYPERSPARC) |
7799 (1 << PROCESSOR_SPARCLITE86X)))
7800 return 3;
7801 return 0;
7804 static int
7805 sparc_issue_rate (void)
7807 switch (sparc_cpu)
7809 default:
7810 return 1;
7811 case PROCESSOR_V9:
7812 /* Assume V9 processors are capable of at least dual-issue. */
7813 return 2;
7814 case PROCESSOR_SUPERSPARC:
7815 return 3;
7816 case PROCESSOR_HYPERSPARC:
7817 case PROCESSOR_SPARCLITE86X:
7818 return 2;
7819 case PROCESSOR_ULTRASPARC:
7820 case PROCESSOR_ULTRASPARC3:
7821 return 4;
7825 static int
7826 set_extends (rtx insn)
7828 register rtx pat = PATTERN (insn);
7830 switch (GET_CODE (SET_SRC (pat)))
7832 /* Load and some shift instructions zero extend. */
7833 case MEM:
7834 case ZERO_EXTEND:
7835 /* sethi clears the high bits */
7836 case HIGH:
7837 /* LO_SUM is used with sethi. sethi cleared the high
7838 bits and the values used with lo_sum are positive */
7839 case LO_SUM:
7840 /* Store flag stores 0 or 1 */
7841 case LT: case LTU:
7842 case GT: case GTU:
7843 case LE: case LEU:
7844 case GE: case GEU:
7845 case EQ:
7846 case NE:
7847 return 1;
7848 case AND:
7850 rtx op0 = XEXP (SET_SRC (pat), 0);
7851 rtx op1 = XEXP (SET_SRC (pat), 1);
7852 if (GET_CODE (op1) == CONST_INT)
7853 return INTVAL (op1) >= 0;
7854 if (GET_CODE (op0) != REG)
7855 return 0;
7856 if (sparc_check_64 (op0, insn) == 1)
7857 return 1;
7858 return (GET_CODE (op1) == REG && sparc_check_64 (op1, insn) == 1);
7860 case IOR:
7861 case XOR:
7863 rtx op0 = XEXP (SET_SRC (pat), 0);
7864 rtx op1 = XEXP (SET_SRC (pat), 1);
7865 if (GET_CODE (op0) != REG || sparc_check_64 (op0, insn) <= 0)
7866 return 0;
7867 if (GET_CODE (op1) == CONST_INT)
7868 return INTVAL (op1) >= 0;
7869 return (GET_CODE (op1) == REG && sparc_check_64 (op1, insn) == 1);
7871 case LSHIFTRT:
7872 return GET_MODE (SET_SRC (pat)) == SImode;
7873 /* Positive integers leave the high bits zero. */
7874 case CONST_DOUBLE:
7875 return ! (CONST_DOUBLE_LOW (SET_SRC (pat)) & 0x80000000);
7876 case CONST_INT:
7877 return ! (INTVAL (SET_SRC (pat)) & 0x80000000);
7878 case ASHIFTRT:
7879 case SIGN_EXTEND:
7880 return - (GET_MODE (SET_SRC (pat)) == SImode);
7881 case REG:
7882 return sparc_check_64 (SET_SRC (pat), insn);
7883 default:
7884 return 0;
7888 /* We _ought_ to have only one kind per function, but... */
7889 static GTY(()) rtx sparc_addr_diff_list;
7890 static GTY(()) rtx sparc_addr_list;
7892 void
7893 sparc_defer_case_vector (rtx lab, rtx vec, int diff)
7895 vec = gen_rtx_EXPR_LIST (VOIDmode, lab, vec);
7896 if (diff)
7897 sparc_addr_diff_list
7898 = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_diff_list);
7899 else
7900 sparc_addr_list = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_list);
7903 static void
7904 sparc_output_addr_vec (rtx vec)
7906 rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
7907 int idx, vlen = XVECLEN (body, 0);
7909 #ifdef ASM_OUTPUT_ADDR_VEC_START
7910 ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
7911 #endif
7913 #ifdef ASM_OUTPUT_CASE_LABEL
7914 ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
7915 NEXT_INSN (lab));
7916 #else
7917 (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
7918 #endif
7920 for (idx = 0; idx < vlen; idx++)
7922 ASM_OUTPUT_ADDR_VEC_ELT
7923 (asm_out_file, CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 0, idx), 0)));
7926 #ifdef ASM_OUTPUT_ADDR_VEC_END
7927 ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
7928 #endif
7931 static void
7932 sparc_output_addr_diff_vec (rtx vec)
7934 rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
7935 rtx base = XEXP (XEXP (body, 0), 0);
7936 int idx, vlen = XVECLEN (body, 1);
7938 #ifdef ASM_OUTPUT_ADDR_VEC_START
7939 ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
7940 #endif
7942 #ifdef ASM_OUTPUT_CASE_LABEL
7943 ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
7944 NEXT_INSN (lab));
7945 #else
7946 (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
7947 #endif
7949 for (idx = 0; idx < vlen; idx++)
7951 ASM_OUTPUT_ADDR_DIFF_ELT
7952 (asm_out_file,
7953 body,
7954 CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 1, idx), 0)),
7955 CODE_LABEL_NUMBER (base));
7958 #ifdef ASM_OUTPUT_ADDR_VEC_END
7959 ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
7960 #endif
7963 static void
7964 sparc_output_deferred_case_vectors (void)
7966 rtx t;
7967 int align;
7969 if (sparc_addr_list == NULL_RTX
7970 && sparc_addr_diff_list == NULL_RTX)
7971 return;
7973 /* Align to cache line in the function's code section. */
7974 function_section (current_function_decl);
7976 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
7977 if (align > 0)
7978 ASM_OUTPUT_ALIGN (asm_out_file, align);
7980 for (t = sparc_addr_list; t ; t = XEXP (t, 1))
7981 sparc_output_addr_vec (XEXP (t, 0));
7982 for (t = sparc_addr_diff_list; t ; t = XEXP (t, 1))
7983 sparc_output_addr_diff_vec (XEXP (t, 0));
7985 sparc_addr_list = sparc_addr_diff_list = NULL_RTX;
7988 /* Return 0 if the high 32 bits of X (the low word of X, if DImode) are
7989 unknown. Return 1 if the high bits are zero, -1 if the register is
7990 sign extended. */
7992 sparc_check_64 (rtx x, rtx insn)
7994 /* If a register is set only once it is safe to ignore insns this
7995 code does not know how to handle. The loop will either recognize
7996 the single set and return the correct value or fail to recognize
7997 it and return 0. */
7998 int set_once = 0;
7999 rtx y = x;
8001 if (GET_CODE (x) != REG)
8002 abort ();
8004 if (GET_MODE (x) == DImode)
8005 y = gen_rtx_REG (SImode, REGNO (x) + WORDS_BIG_ENDIAN);
8007 if (flag_expensive_optimizations
8008 && REG_N_SETS (REGNO (y)) == 1)
8009 set_once = 1;
8011 if (insn == 0)
8013 if (set_once)
8014 insn = get_last_insn_anywhere ();
8015 else
8016 return 0;
8019 while ((insn = PREV_INSN (insn)))
8021 switch (GET_CODE (insn))
8023 case JUMP_INSN:
8024 case NOTE:
8025 break;
8026 case CODE_LABEL:
8027 case CALL_INSN:
8028 default:
8029 if (! set_once)
8030 return 0;
8031 break;
8032 case INSN:
8034 rtx pat = PATTERN (insn);
8035 if (GET_CODE (pat) != SET)
8036 return 0;
8037 if (rtx_equal_p (x, SET_DEST (pat)))
8038 return set_extends (insn);
8039 if (y && rtx_equal_p (y, SET_DEST (pat)))
8040 return set_extends (insn);
8041 if (reg_overlap_mentioned_p (SET_DEST (pat), y))
8042 return 0;
8046 return 0;
8049 /* Returns assembly code to perform a DImode shift using
8050 a 64-bit global or out register on SPARC-V8+. */
8051 const char *
8052 output_v8plus_shift (rtx *operands, rtx insn, const char *opcode)
8054 static char asm_code[60];
8056 /* The scratch register is only required when the destination
8057 register is not a 64-bit global or out register. */
8058 if (which_alternative != 2)
8059 operands[3] = operands[0];
8061 /* We can only shift by constants <= 63. */
8062 if (GET_CODE (operands[2]) == CONST_INT)
8063 operands[2] = GEN_INT (INTVAL (operands[2]) & 0x3f);
8065 if (GET_CODE (operands[1]) == CONST_INT)
8067 output_asm_insn ("mov\t%1, %3", operands);
8069 else
8071 output_asm_insn ("sllx\t%H1, 32, %3", operands);
8072 if (sparc_check_64 (operands[1], insn) <= 0)
8073 output_asm_insn ("srl\t%L1, 0, %L1", operands);
8074 output_asm_insn ("or\t%L1, %3, %3", operands);
8077 strcpy(asm_code, opcode);
8079 if (which_alternative != 2)
8080 return strcat (asm_code, "\t%0, %2, %L0\n\tsrlx\t%L0, 32, %H0");
8081 else
8082 return strcat (asm_code, "\t%3, %2, %3\n\tsrlx\t%3, 32, %H0\n\tmov\t%3, %L0");
8085 /* Output rtl to increment the profiler label LABELNO
8086 for profiling a function entry. */
8088 void
8089 sparc_profile_hook (int labelno)
8091 char buf[32];
8092 rtx lab, fun;
8094 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
8095 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
8096 fun = gen_rtx_SYMBOL_REF (Pmode, MCOUNT_FUNCTION);
8098 emit_library_call (fun, LCT_NORMAL, VOIDmode, 1, lab, Pmode);
8101 #ifdef OBJECT_FORMAT_ELF
8102 static void
8103 sparc_elf_asm_named_section (const char *name, unsigned int flags)
8105 if (flags & SECTION_MERGE)
8107 /* entsize cannot be expressed in this section attributes
8108 encoding style. */
8109 default_elf_asm_named_section (name, flags);
8110 return;
8113 fprintf (asm_out_file, "\t.section\t\"%s\"", name);
8115 if (!(flags & SECTION_DEBUG))
8116 fputs (",#alloc", asm_out_file);
8117 if (flags & SECTION_WRITE)
8118 fputs (",#write", asm_out_file);
8119 if (flags & SECTION_TLS)
8120 fputs (",#tls", asm_out_file);
8121 if (flags & SECTION_CODE)
8122 fputs (",#execinstr", asm_out_file);
8124 /* ??? Handle SECTION_BSS. */
8126 fputc ('\n', asm_out_file);
8128 #endif /* OBJECT_FORMAT_ELF */
8130 /* We do not allow indirect calls to be optimized into sibling calls.
8132 We cannot use sibling calls when delayed branches are disabled
8133 because they will likely require the call delay slot to be filled.
8135 Also, on SPARC 32-bit we cannot emit a sibling call when the
8136 current function returns a structure. This is because the "unimp
8137 after call" convention would cause the callee to return to the
8138 wrong place. The generic code already disallows cases where the
8139 function being called returns a structure.
8141 It may seem strange how this last case could occur. Usually there
8142 is code after the call which jumps to epilogue code which dumps the
8143 return value into the struct return area. That ought to invalidate
8144 the sibling call right? Well, in the C++ case we can end up passing
8145 the pointer to the struct return area to a constructor (which returns
8146 void) and then nothing else happens. Such a sibling call would look
8147 valid without the added check here. */
8148 static bool
8149 sparc_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
8151 return (decl
8152 && flag_delayed_branch
8153 && (TARGET_ARCH64 || ! current_function_returns_struct));
8156 /* libfunc renaming. */
8157 #include "config/gofast.h"
8159 static void
8160 sparc_init_libfuncs (void)
8162 if (TARGET_ARCH32)
8164 /* Use the subroutines that Sun's library provides for integer
8165 multiply and divide. The `*' prevents an underscore from
8166 being prepended by the compiler. .umul is a little faster
8167 than .mul. */
8168 set_optab_libfunc (smul_optab, SImode, "*.umul");
8169 set_optab_libfunc (sdiv_optab, SImode, "*.div");
8170 set_optab_libfunc (udiv_optab, SImode, "*.udiv");
8171 set_optab_libfunc (smod_optab, SImode, "*.rem");
8172 set_optab_libfunc (umod_optab, SImode, "*.urem");
8174 /* TFmode arithmetic. These names are part of the SPARC 32bit ABI. */
8175 set_optab_libfunc (add_optab, TFmode, "_Q_add");
8176 set_optab_libfunc (sub_optab, TFmode, "_Q_sub");
8177 set_optab_libfunc (neg_optab, TFmode, "_Q_neg");
8178 set_optab_libfunc (smul_optab, TFmode, "_Q_mul");
8179 set_optab_libfunc (sdiv_optab, TFmode, "_Q_div");
8181 /* We can define the TFmode sqrt optab only if TARGET_FPU. This
8182 is because with soft-float, the SFmode and DFmode sqrt
8183 instructions will be absent, and the compiler will notice and
8184 try to use the TFmode sqrt instruction for calls to the
8185 builtin function sqrt, but this fails. */
8186 if (TARGET_FPU)
8187 set_optab_libfunc (sqrt_optab, TFmode, "_Q_sqrt");
8189 set_optab_libfunc (eq_optab, TFmode, "_Q_feq");
8190 set_optab_libfunc (ne_optab, TFmode, "_Q_fne");
8191 set_optab_libfunc (gt_optab, TFmode, "_Q_fgt");
8192 set_optab_libfunc (ge_optab, TFmode, "_Q_fge");
8193 set_optab_libfunc (lt_optab, TFmode, "_Q_flt");
8194 set_optab_libfunc (le_optab, TFmode, "_Q_fle");
8196 set_conv_libfunc (sext_optab, TFmode, SFmode, "_Q_stoq");
8197 set_conv_libfunc (sext_optab, TFmode, DFmode, "_Q_dtoq");
8198 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_Q_qtos");
8199 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_Q_qtod");
8201 set_conv_libfunc (sfix_optab, SImode, TFmode, "_Q_qtoi");
8202 set_conv_libfunc (ufix_optab, SImode, TFmode, "_Q_qtou");
8203 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_Q_itoq");
8205 if (DITF_CONVERSION_LIBFUNCS)
8207 set_conv_libfunc (sfix_optab, DImode, TFmode, "_Q_qtoll");
8208 set_conv_libfunc (ufix_optab, DImode, TFmode, "_Q_qtoull");
8209 set_conv_libfunc (sfloat_optab, TFmode, DImode, "_Q_lltoq");
8212 if (SUN_CONVERSION_LIBFUNCS)
8214 set_conv_libfunc (sfix_optab, DImode, SFmode, "__ftoll");
8215 set_conv_libfunc (ufix_optab, DImode, SFmode, "__ftoull");
8216 set_conv_libfunc (sfix_optab, DImode, DFmode, "__dtoll");
8217 set_conv_libfunc (ufix_optab, DImode, DFmode, "__dtoull");
8220 if (TARGET_ARCH64)
8222 /* In the SPARC 64bit ABI, SImode multiply and divide functions
8223 do not exist in the library. Make sure the compiler does not
8224 emit calls to them by accident. (It should always use the
8225 hardware instructions.) */
8226 set_optab_libfunc (smul_optab, SImode, 0);
8227 set_optab_libfunc (sdiv_optab, SImode, 0);
8228 set_optab_libfunc (udiv_optab, SImode, 0);
8229 set_optab_libfunc (smod_optab, SImode, 0);
8230 set_optab_libfunc (umod_optab, SImode, 0);
8232 if (SUN_INTEGER_MULTIPLY_64)
8234 set_optab_libfunc (smul_optab, DImode, "__mul64");
8235 set_optab_libfunc (sdiv_optab, DImode, "__div64");
8236 set_optab_libfunc (udiv_optab, DImode, "__udiv64");
8237 set_optab_libfunc (smod_optab, DImode, "__rem64");
8238 set_optab_libfunc (umod_optab, DImode, "__urem64");
8241 if (SUN_CONVERSION_LIBFUNCS)
8243 set_conv_libfunc (sfix_optab, DImode, SFmode, "__ftol");
8244 set_conv_libfunc (ufix_optab, DImode, SFmode, "__ftoul");
8245 set_conv_libfunc (sfix_optab, DImode, DFmode, "__dtol");
8246 set_conv_libfunc (ufix_optab, DImode, DFmode, "__dtoul");
8250 gofast_maybe_init_libfuncs ();
8254 sparc_extra_constraint_check (rtx op, int c, int strict)
8256 int reload_ok_mem;
8258 if (TARGET_ARCH64
8259 && (c == 'T' || c == 'U'))
8260 return 0;
8262 switch (c)
8264 case 'Q':
8265 return fp_sethi_p (op);
8267 case 'R':
8268 return fp_mov_p (op);
8270 case 'S':
8271 return fp_high_losum_p (op);
8273 case 'U':
8274 if (! strict
8275 || (GET_CODE (op) == REG
8276 && (REGNO (op) < FIRST_PSEUDO_REGISTER
8277 || reg_renumber[REGNO (op)] >= 0)))
8278 return register_ok_for_ldd (op);
8280 return 0;
8282 case 'W':
8283 case 'T':
8284 break;
8286 default:
8287 return 0;
8290 /* Our memory extra constraints have to emulate the
8291 behavior of 'm' and 'o' in order for reload to work
8292 correctly. */
8293 if (GET_CODE (op) == MEM)
8295 reload_ok_mem = 0;
8296 if ((TARGET_ARCH64 || mem_min_alignment (op, 8))
8297 && (! strict
8298 || strict_memory_address_p (Pmode, XEXP (op, 0))))
8299 reload_ok_mem = 1;
8301 else
8303 reload_ok_mem = (reload_in_progress
8304 && GET_CODE (op) == REG
8305 && REGNO (op) >= FIRST_PSEUDO_REGISTER
8306 && reg_renumber [REGNO (op)] < 0);
8309 return reload_ok_mem;
8312 /* ??? This duplicates information provided to the compiler by the
8313 ??? scheduler description. Some day, teach genautomata to output
8314 ??? the latencies and then CSE will just use that. */
8316 static bool
8317 sparc_rtx_costs (rtx x, int code, int outer_code, int *total)
8319 enum machine_mode mode = GET_MODE (x);
8320 bool float_mode_p = FLOAT_MODE_P (mode);
8322 switch (code)
8324 case CONST_INT:
8325 if (INTVAL (x) < 0x1000 && INTVAL (x) >= -0x1000)
8327 *total = 0;
8328 return true;
8330 /* FALLTHRU */
8332 case HIGH:
8333 *total = 2;
8334 return true;
8336 case CONST:
8337 case LABEL_REF:
8338 case SYMBOL_REF:
8339 *total = 4;
8340 return true;
8342 case CONST_DOUBLE:
8343 if (GET_MODE (x) == DImode
8344 && ((XINT (x, 3) == 0
8345 && (unsigned HOST_WIDE_INT) XINT (x, 2) < 0x1000)
8346 || (XINT (x, 3) == -1
8347 && XINT (x, 2) < 0
8348 && XINT (x, 2) >= -0x1000)))
8349 *total = 0;
8350 else
8351 *total = 8;
8352 return true;
8354 case MEM:
8355 /* If outer-code was a sign or zero extension, a cost
8356 of COSTS_N_INSNS (1) was already added in. This is
8357 why we are subtracting it back out. */
8358 if (outer_code == ZERO_EXTEND)
8360 *total = sparc_costs->int_zload - COSTS_N_INSNS (1);
8362 else if (outer_code == SIGN_EXTEND)
8364 *total = sparc_costs->int_sload - COSTS_N_INSNS (1);
8366 else if (float_mode_p)
8368 *total = sparc_costs->float_load;
8370 else
8372 *total = sparc_costs->int_load;
8375 return true;
8377 case PLUS:
8378 case MINUS:
8379 if (float_mode_p)
8380 *total = sparc_costs->float_plusminus;
8381 else
8382 *total = COSTS_N_INSNS (1);
8383 return false;
8385 case MULT:
8386 if (float_mode_p)
8387 *total = sparc_costs->float_mul;
8388 else
8390 int bit_cost;
8392 bit_cost = 0;
8393 if (sparc_costs->int_mul_bit_factor)
8395 int nbits;
8397 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8399 unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
8400 for (nbits = 0; value != 0; value &= value - 1)
8401 nbits++;
8403 else if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
8404 && GET_MODE (XEXP (x, 1)) == DImode)
8406 rtx x1 = XEXP (x, 1);
8407 unsigned HOST_WIDE_INT value1 = XINT (x1, 2);
8408 unsigned HOST_WIDE_INT value2 = XINT (x1, 3);
8410 for (nbits = 0; value1 != 0; value1 &= value1 - 1)
8411 nbits++;
8412 for (; value2 != 0; value2 &= value2 - 1)
8413 nbits++;
8415 else
8416 nbits = 7;
8418 if (nbits < 3)
8419 nbits = 3;
8420 bit_cost = (nbits - 3) / sparc_costs->int_mul_bit_factor;
8421 bit_cost = COSTS_N_INSNS (bit_cost);
8424 if (mode == DImode)
8425 *total = sparc_costs->int_mulX + bit_cost;
8426 else
8427 *total = sparc_costs->int_mul + bit_cost;
8429 return false;
8431 case ASHIFT:
8432 case ASHIFTRT:
8433 case LSHIFTRT:
8434 *total = COSTS_N_INSNS (1) + sparc_costs->shift_penalty;
8435 return false;
8437 case DIV:
8438 case UDIV:
8439 case MOD:
8440 case UMOD:
8441 if (float_mode_p)
8443 if (mode == DFmode)
8444 *total = sparc_costs->float_div_df;
8445 else
8446 *total = sparc_costs->float_div_sf;
8448 else
8450 if (mode == DImode)
8451 *total = sparc_costs->int_divX;
8452 else
8453 *total = sparc_costs->int_div;
8455 return false;
8457 case NEG:
8458 if (! float_mode_p)
8460 *total = COSTS_N_INSNS (1);
8461 return false;
8463 /* FALLTHRU */
8465 case ABS:
8466 case FLOAT:
8467 case UNSIGNED_FLOAT:
8468 case FIX:
8469 case UNSIGNED_FIX:
8470 case FLOAT_EXTEND:
8471 case FLOAT_TRUNCATE:
8472 *total = sparc_costs->float_move;
8473 return false;
8475 case SQRT:
8476 if (mode == DFmode)
8477 *total = sparc_costs->float_sqrt_df;
8478 else
8479 *total = sparc_costs->float_sqrt_sf;
8480 return false;
8482 case COMPARE:
8483 if (float_mode_p)
8484 *total = sparc_costs->float_cmp;
8485 else
8486 *total = COSTS_N_INSNS (1);
8487 return false;
8489 case IF_THEN_ELSE:
8490 if (float_mode_p)
8491 *total = sparc_costs->float_cmove;
8492 else
8493 *total = sparc_costs->int_cmove;
8494 return false;
8496 default:
8497 return false;
8501 /* Emit the sequence of insns SEQ while preserving the register REG. */
8503 static void
8504 emit_and_preserve (rtx seq, rtx reg)
8506 rtx slot = gen_rtx_MEM (word_mode,
8507 plus_constant (stack_pointer_rtx, SPARC_STACK_BIAS));
8509 emit_stack_pointer_decrement (GEN_INT (UNITS_PER_WORD));
8510 emit_insn (gen_rtx_SET (VOIDmode, slot, reg));
8511 emit_insn (seq);
8512 emit_insn (gen_rtx_SET (VOIDmode, reg, slot));
8513 emit_stack_pointer_increment (GEN_INT (UNITS_PER_WORD));
8516 /* Output code to add DELTA to the first argument, and then jump to FUNCTION.
8517 Used for C++ multiple inheritance. */
8519 static void
8520 sparc_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
8521 HOST_WIDE_INT delta,
8522 HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
8523 tree function)
8525 rtx this, insn, funexp, delta_rtx;
8526 unsigned int int_arg_first;
8528 reload_completed = 1;
8529 epilogue_completed = 1;
8530 no_new_pseudos = 1;
8531 reset_block_changes ();
8533 emit_note (NOTE_INSN_PROLOGUE_END);
8535 if (flag_delayed_branch)
8537 /* We will emit a regular sibcall below, so we need to instruct
8538 output_sibcall that we are in a leaf function. */
8539 current_function_uses_only_leaf_regs = 1;
8541 /* This will cause final.c to invoke leaf_renumber_regs so we
8542 must behave as if we were in a not-yet-leafified function. */
8543 int_arg_first = SPARC_INCOMING_INT_ARG_FIRST;
8545 else
8547 /* We will emit the sibcall manually below, so we will need to
8548 manually spill non-leaf registers. */
8549 current_function_uses_only_leaf_regs = 0;
8551 /* We really are in a leaf function. */
8552 int_arg_first = SPARC_OUTGOING_INT_ARG_FIRST;
8555 /* Find the "this" pointer. Normally in %o0, but in ARCH64 if the function
8556 returns a structure, the structure return pointer is there instead. */
8557 if (TARGET_ARCH64 && aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
8558 this = gen_rtx_REG (Pmode, int_arg_first + 1);
8559 else
8560 this = gen_rtx_REG (Pmode, int_arg_first);
8562 /* Add DELTA. When possible use a plain add, otherwise load it into
8563 a register first. */
8564 delta_rtx = GEN_INT (delta);
8565 if (!SPARC_SIMM13_P (delta))
8567 rtx scratch = gen_rtx_REG (Pmode, 1);
8569 if (input_operand (delta_rtx, GET_MODE (scratch)))
8570 emit_insn (gen_rtx_SET (VOIDmode, scratch, delta_rtx));
8571 else
8573 if (TARGET_ARCH64)
8574 sparc_emit_set_const64 (scratch, delta_rtx);
8575 else
8576 sparc_emit_set_const32 (scratch, delta_rtx);
8579 delta_rtx = scratch;
8582 emit_insn (gen_rtx_SET (VOIDmode,
8583 this,
8584 gen_rtx_PLUS (Pmode, this, delta_rtx)));
8586 /* Generate a tail call to the target function. */
8587 if (! TREE_USED (function))
8589 assemble_external (function);
8590 TREE_USED (function) = 1;
8592 funexp = XEXP (DECL_RTL (function), 0);
8594 if (flag_delayed_branch)
8596 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
8597 insn = emit_call_insn (gen_sibcall (funexp));
8598 SIBLING_CALL_P (insn) = 1;
8600 else
8602 /* The hoops we have to jump through in order to generate a sibcall
8603 without using delay slots... */
8604 rtx spill_reg, seq, scratch = gen_rtx_REG (Pmode, 1);
8606 if (flag_pic)
8608 spill_reg = gen_rtx_REG (word_mode, 15); /* %o7 */
8609 start_sequence ();
8610 load_pic_register (); /* clobbers %o7 */
8611 scratch = legitimize_pic_address (funexp, Pmode, scratch);
8612 seq = get_insns ();
8613 end_sequence ();
8614 emit_and_preserve (seq, spill_reg);
8616 else if (TARGET_ARCH32)
8618 emit_insn (gen_rtx_SET (VOIDmode,
8619 scratch,
8620 gen_rtx_HIGH (SImode, funexp)));
8621 emit_insn (gen_rtx_SET (VOIDmode,
8622 scratch,
8623 gen_rtx_LO_SUM (SImode, scratch, funexp)));
8625 else /* TARGET_ARCH64 */
8627 switch (sparc_cmodel)
8629 case CM_MEDLOW:
8630 case CM_MEDMID:
8631 /* The destination can serve as a temporary. */
8632 sparc_emit_set_symbolic_const64 (scratch, funexp, scratch);
8633 break;
8635 case CM_MEDANY:
8636 case CM_EMBMEDANY:
8637 /* The destination cannot serve as a temporary. */
8638 spill_reg = gen_rtx_REG (DImode, 15); /* %o7 */
8639 start_sequence ();
8640 sparc_emit_set_symbolic_const64 (scratch, funexp, spill_reg);
8641 seq = get_insns ();
8642 end_sequence ();
8643 emit_and_preserve (seq, spill_reg);
8644 break;
8646 default:
8647 abort();
8651 emit_jump_insn (gen_indirect_jump (scratch));
8654 emit_barrier ();
8656 /* Run just enough of rest_of_compilation to get the insns emitted.
8657 There's not really enough bulk here to make other passes such as
8658 instruction scheduling worth while. Note that use_thunk calls
8659 assemble_start_function and assemble_end_function. */
8660 insn = get_insns ();
8661 insn_locators_initialize ();
8662 shorten_branches (insn);
8663 final_start_function (insn, file, 1);
8664 final (insn, file, 1, 0);
8665 final_end_function ();
8667 reload_completed = 0;
8668 epilogue_completed = 0;
8669 no_new_pseudos = 0;
8672 /* How to allocate a 'struct machine_function'. */
8674 static struct machine_function *
8675 sparc_init_machine_status (void)
8677 return ggc_alloc_cleared (sizeof (struct machine_function));
8680 /* Locate some local-dynamic symbol still in use by this function
8681 so that we can print its name in local-dynamic base patterns. */
8683 static const char *
8684 get_some_local_dynamic_name (void)
8686 rtx insn;
8688 if (cfun->machine->some_ld_name)
8689 return cfun->machine->some_ld_name;
8691 for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
8692 if (INSN_P (insn)
8693 && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
8694 return cfun->machine->some_ld_name;
8696 abort ();
8699 static int
8700 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
8702 rtx x = *px;
8704 if (x
8705 && GET_CODE (x) == SYMBOL_REF
8706 && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
8708 cfun->machine->some_ld_name = XSTR (x, 0);
8709 return 1;
8712 return 0;
8715 /* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
8716 We need to emit DTP-relative relocations. */
8718 void
8719 sparc_output_dwarf_dtprel (FILE *file, int size, rtx x)
8721 switch (size)
8723 case 4:
8724 fputs ("\t.word\t%r_tls_dtpoff32(", file);
8725 break;
8726 case 8:
8727 fputs ("\t.xword\t%r_tls_dtpoff64(", file);
8728 break;
8729 default:
8730 abort ();
8732 output_addr_const (file, x);
8733 fputs (")", file);
8736 #include "gt-sparc.h"