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