]> gcc.gnu.org Git - gcc.git/blame - gcc/config/sparc/sparc.c
fix copyrights
[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)
fbd039b2 3274 FILE *file ATTRIBUTE_UNUSED;
1cb36a98
RH
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 {
fbd039b2 4545 if (TYPE_ALIGN (type) >= 2 * (unsigned) BITS_PER_WORD)
a8b2c8a1
RH
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 4823 char *qpfunc;
5c5c34a4
JJ
4824 rtx slot0, slot1, result, tem, tem2;
4825 enum machine_mode mode;
47ac041c
JJ
4826
4827 switch (comparison)
4828 {
4829 case EQ:
5c5c34a4 4830 qpfunc = (TARGET_ARCH64) ? "_Qp_feq" : "_Q_feq";
47ac041c
JJ
4831 break;
4832
4833 case NE:
5c5c34a4 4834 qpfunc = (TARGET_ARCH64) ? "_Qp_fne" : "_Q_fne";
47ac041c
JJ
4835 break;
4836
4837 case GT:
5c5c34a4 4838 qpfunc = (TARGET_ARCH64) ? "_Qp_fgt" : "_Q_fgt";
47ac041c
JJ
4839 break;
4840
4841 case GE:
5c5c34a4 4842 qpfunc = (TARGET_ARCH64) ? "_Qp_fge" : "_Q_fge";
47ac041c
JJ
4843 break;
4844
4845 case LT:
5c5c34a4 4846 qpfunc = (TARGET_ARCH64) ? "_Qp_flt" : "_Q_flt";
47ac041c
JJ
4847 break;
4848
4849 case LE:
5c5c34a4
JJ
4850 qpfunc = (TARGET_ARCH64) ? "_Qp_fle" : "_Q_fle";
4851 break;
4852
4853 case ORDERED:
4854 case UNORDERED:
4855 case UNGT:
4856 case UNLT:
4857 case UNEQ:
4858 case UNGE:
4859 case UNLE:
4860 case LTGT:
4861 qpfunc = (TARGET_ARCH64) ? "_Qp_cmp" : "_Q_cmp";
47ac041c
JJ
4862 break;
4863
4864 default:
4865 abort();
4866 break;
4867 }
4868
5c5c34a4 4869 if (TARGET_ARCH64)
47ac041c 4870 {
5c5c34a4
JJ
4871 if (GET_CODE (x) != MEM)
4872 {
4873 slot0 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode), 0);
4874 emit_insn (gen_rtx_SET (VOIDmode, slot0, x));
4875 }
4876 else
4877 slot0 = x;
4878
4879 if (GET_CODE (y) != MEM)
4880 {
4881 slot1 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode), 0);
4882 emit_insn (gen_rtx_SET (VOIDmode, slot1, y));
4883 }
4884 else
4885 slot1 = y;
47ac041c 4886
5c5c34a4
JJ
4887 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, qpfunc), 1,
4888 DImode, 2,
4889 XEXP (slot0, 0), Pmode,
4890 XEXP (slot1, 0), Pmode);
4891
4892 mode = DImode;
4893 }
4894 else
47ac041c 4895 {
5c5c34a4
JJ
4896 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, qpfunc), 1,
4897 SImode, 2,
4898 x, TFmode, y, TFmode);
4899
4900 mode = SImode;
47ac041c
JJ
4901 }
4902
47ac041c
JJ
4903
4904 /* Immediately move the result of the libcall into a pseudo
4905 register so reload doesn't clobber the value if it needs
4906 the return register for a spill reg. */
5c5c34a4
JJ
4907 result = gen_reg_rtx (mode);
4908 emit_move_insn (result, hard_libcall_value (mode));
47ac041c 4909
5c5c34a4
JJ
4910 switch (comparison)
4911 {
4912 default:
4913 emit_cmp_insn (result, const0_rtx, NE,
4914 NULL_RTX, mode, 0, 0);
4915 break;
4916 case ORDERED:
4917 case UNORDERED:
4918 emit_cmp_insn (result, GEN_INT(3),
4919 (comparison == UNORDERED) ? EQ : NE,
4920 NULL_RTX, mode, 0, 0);
4921 break;
4922 case UNGT:
4923 case UNGE:
4924 emit_cmp_insn (result, const1_rtx,
4925 (comparison == UNGT) ? GT : NE,
4926 NULL_RTX, mode, 0, 0);
4927 break;
4928 case UNLE:
4929 emit_cmp_insn (result, const2_rtx, NE,
4930 NULL_RTX, mode, 0, 0);
4931 break;
4932 case UNLT:
4933 tem = gen_reg_rtx (mode);
4934 if (TARGET_ARCH32)
4935 emit_insn (gen_andsi3 (tem, result, const1_rtx));
4936 else
4937 emit_insn (gen_anddi3 (tem, result, const1_rtx));
4938 emit_cmp_insn (tem, const0_rtx, NE,
4939 NULL_RTX, mode, 0, 0);
4940 break;
4941 case UNEQ:
4942 case LTGT:
4943 tem = gen_reg_rtx (mode);
4944 if (TARGET_ARCH32)
4945 emit_insn (gen_addsi3 (tem, result, const1_rtx));
4946 else
4947 emit_insn (gen_adddi3 (tem, result, const1_rtx));
4948 tem2 = gen_reg_rtx (mode);
4949 if (TARGET_ARCH32)
4950 emit_insn (gen_andsi3 (tem2, tem, const2_rtx));
4951 else
4952 emit_insn (gen_anddi3 (tem2, tem, const2_rtx));
4953 emit_cmp_insn (tem2, const0_rtx,
4954 (comparison == UNEQ) ? EQ : NE,
4955 NULL_RTX, mode, 0, 0);
4956 break;
4957 }
47ac041c 4958}
5c5c34a4 4959
61a55e8b
DE
4960/* Return the string to output a conditional branch to LABEL, testing
4961 register REG. LABEL is the operand number of the label; REG is the
4962 operand number of the reg. OP is the conditional expression. The mode
4963 of REG says what kind of comparison we made.
4964
4965 REVERSED is non-zero if we should reverse the sense of the comparison.
4966
4967 ANNUL is non-zero if we should generate an annulling branch.
4968
4969 NOOP is non-zero if we have to follow this branch by a noop. */
4970
4971char *
e0d80184 4972output_v9branch (op, reg, label, reversed, annul, noop, insn)
61a55e8b
DE
4973 rtx op;
4974 int reg, label;
4975 int reversed, annul, noop;
e0d80184 4976 rtx insn;
61a55e8b
DE
4977{
4978 static char string[20];
4979 enum rtx_code code = GET_CODE (op);
4980 enum machine_mode mode = GET_MODE (XEXP (op, 0));
e0d80184
DM
4981 static char labelno[] = "%X, %lX";
4982 rtx note;
4983 int spaces = 8;
61a55e8b
DE
4984
4985 /* If not floating-point or if EQ or NE, we can just reverse the code. */
4986 if (reversed)
4987 code = reverse_condition (code), reversed = 0;
4988
4989 /* Only 64 bit versions of these instructions exist. */
4990 if (mode != DImode)
4991 abort ();
4992
4993 /* Start by writing the branch condition. */
4994
4995 switch (code)
4996 {
4997 case NE:
4998 strcpy (string, "brnz");
e0d80184 4999 spaces -= 4;
61a55e8b
DE
5000 break;
5001
5002 case EQ:
5003 strcpy (string, "brz");
e0d80184 5004 spaces -= 3;
61a55e8b
DE
5005 break;
5006
5007 case GE:
5008 strcpy (string, "brgez");
e0d80184 5009 spaces -= 5;
61a55e8b
DE
5010 break;
5011
5012 case LT:
5013 strcpy (string, "brlz");
e0d80184 5014 spaces -= 4;
61a55e8b
DE
5015 break;
5016
5017 case LE:
5018 strcpy (string, "brlez");
e0d80184 5019 spaces -= 5;
61a55e8b
DE
5020 break;
5021
5022 case GT:
5023 strcpy (string, "brgz");
e0d80184 5024 spaces -= 4;
61a55e8b
DE
5025 break;
5026
5027 default:
5028 abort ();
5029 }
5030
5031 /* Now add the annulling, reg, label, and nop. */
5032 if (annul)
e0d80184
DM
5033 {
5034 strcat (string, ",a");
5035 spaces -= 2;
5036 }
61a55e8b 5037
e0d80184
DM
5038 if (insn && (note = find_reg_note (insn, REG_BR_PRED, NULL_RTX)))
5039 {
5040 strcat (string,
5041 INTVAL (XEXP (note, 0)) & ATTR_FLAG_likely ? ",pt" : ",pn");
5042 spaces -= 3;
5043 }
61a55e8b 5044
e0d80184 5045 labelno[1] = reg + '0';
61a55e8b 5046 labelno[6] = label + '0';
e0d80184
DM
5047 if (spaces > 0)
5048 strcat (string, "\t");
5049 else
5050 strcat (string, " ");
ab835497
RK
5051 strcat (string, labelno);
5052
5053 if (noop)
5054 strcat (string, "\n\tnop");
5055
5056 return string;
5057}
5058
e48addee
JJ
5059/* Return 1, if any of the registers of the instruction are %l[0-7] or %o[0-7].
5060 Such instructions cannot be used in the delay slot of return insn on v9.
5061 If TEST is 0, also rename all %i[0-7] registers to their %o[0-7] counterparts.
5062 */
284d86e9 5063
e48addee
JJ
5064static int
5065epilogue_renumber (where, test)
5066 register rtx *where;
5067 int test;
284d86e9 5068{
e48addee
JJ
5069 register const char *fmt;
5070 register int i;
5071 register enum rtx_code code;
5072
5073 if (*where == 0)
5074 return 0;
5075
5076 code = GET_CODE (*where);
284d86e9
JC
5077
5078 switch (code)
5079 {
284d86e9 5080 case REG:
e48addee
JJ
5081 if (REGNO (*where) >= 8 && REGNO (*where) < 24) /* oX or lX */
5082 return 1;
5083 if (! test && REGNO (*where) >= 24 && REGNO (*where) < 32)
5084 *where = gen_rtx (REG, GET_MODE (*where), OUTGOING_REGNO (REGNO(*where)));
5085 case SCRATCH:
5086 case CC0:
5087 case PC:
284d86e9
JC
5088 case CONST_INT:
5089 case CONST_DOUBLE:
e48addee 5090 return 0;
76a7c776
DM
5091
5092 default:
5093 break;
e48addee 5094 }
284d86e9 5095
e48addee 5096 fmt = GET_RTX_FORMAT (code);
284d86e9 5097
e48addee
JJ
5098 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5099 {
5100 if (fmt[i] == 'E')
5101 {
5102 register int j;
5103 for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
5104 if (epilogue_renumber (&(XVECEXP (*where, i, j)), test))
5105 return 1;
5106 }
5107 else if (fmt[i] == 'e'
5108 && epilogue_renumber (&(XEXP (*where, i)), test))
5109 return 1;
284d86e9 5110 }
e48addee 5111 return 0;
284d86e9
JC
5112}
5113
5114/* Output assembler code to return from a function. */
61a55e8b 5115
3bb5de61 5116const char *
ab835497
RK
5117output_return (operands)
5118 rtx *operands;
5119{
284d86e9
JC
5120 rtx delay = final_sequence ? XVECEXP (final_sequence, 0, 1) : 0;
5121
ab835497
RK
5122 if (leaf_label)
5123 {
5124 operands[0] = leaf_label;
284d86e9 5125 return "b%* %l0%(";
ab835497 5126 }
54ff41b7 5127 else if (current_function_uses_only_leaf_regs)
ab835497 5128 {
284d86e9
JC
5129 /* No delay slot in a leaf function. */
5130 if (delay)
5131 abort ();
5132
915f619f
JW
5133 /* If we didn't allocate a frame pointer for the current function,
5134 the stack pointer might have been adjusted. Output code to
5135 restore it now. */
5136
3a598fbe 5137 operands[0] = GEN_INT (actual_fsize);
915f619f
JW
5138
5139 /* Use sub of negated value in first two cases instead of add to
5140 allow actual_fsize == 4096. */
5141
5142 if (actual_fsize <= 4096)
ab835497 5143 {
61a55e8b 5144 if (SKIP_CALLERS_UNIMP_P)
e0d80184 5145 return "jmp\t%%o7+12\n\tsub\t%%sp, -%0, %%sp";
ab835497 5146 else
e0d80184 5147 return "retl\n\tsub\t%%sp, -%0, %%sp";
ab835497 5148 }
915f619f 5149 else if (actual_fsize <= 8192)
ab835497 5150 {
3a598fbe 5151 operands[0] = GEN_INT (actual_fsize - 4096);
61a55e8b 5152 if (SKIP_CALLERS_UNIMP_P)
e0d80184 5153 return "sub\t%%sp, -4096, %%sp\n\tjmp\t%%o7+12\n\tsub\t%%sp, -%0, %%sp";
915f619f 5154 else
e0d80184 5155 return "sub\t%%sp, -4096, %%sp\n\tretl\n\tsub\t%%sp, -%0, %%sp";
915f619f 5156 }
61a55e8b 5157 else if (SKIP_CALLERS_UNIMP_P)
915f619f
JW
5158 {
5159 if ((actual_fsize & 0x3ff) != 0)
e0d80184 5160 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 5161 else
e0d80184 5162 return "sethi\t%%hi(%a0), %%g1\n\tjmp\t%%o7+12\n\tadd\t%%sp, %%g1, %%sp";
915f619f
JW
5163 }
5164 else
5165 {
5166 if ((actual_fsize & 0x3ff) != 0)
ab835497 5167 return "sethi %%hi(%a0),%%g1\n\tor %%g1,%%lo(%a0),%%g1\n\tretl\n\tadd %%sp,%%g1,%%sp";
915f619f
JW
5168 else
5169 return "sethi %%hi(%a0),%%g1\n\tretl\n\tadd %%sp,%%g1,%%sp";
ab835497
RK
5170 }
5171 }
284d86e9
JC
5172 else if (TARGET_V9)
5173 {
5174 if (delay)
5175 {
e48addee
JJ
5176 epilogue_renumber (&SET_DEST (PATTERN (delay)), 0);
5177 epilogue_renumber (&SET_SRC (PATTERN (delay)), 0);
284d86e9
JC
5178 }
5179 if (SKIP_CALLERS_UNIMP_P)
e0d80184 5180 return "return\t%%i7+12%#";
284d86e9 5181 else
e0d80184 5182 return "return\t%%i7+8%#";
284d86e9 5183 }
ab835497
RK
5184 else
5185 {
284d86e9
JC
5186 if (delay)
5187 abort ();
61a55e8b 5188 if (SKIP_CALLERS_UNIMP_P)
e0d80184 5189 return "jmp\t%%i7+12\n\trestore";
ab835497
RK
5190 else
5191 return "ret\n\trestore";
5192 }
5193}
ab835497
RK
5194\f
5195/* Leaf functions and non-leaf functions have different needs. */
5196
5197static int
5198reg_leaf_alloc_order[] = REG_LEAF_ALLOC_ORDER;
5199
5200static int
5201reg_nonleaf_alloc_order[] = REG_ALLOC_ORDER;
5202
5203static int *reg_alloc_orders[] = {
5204 reg_leaf_alloc_order,
5205 reg_nonleaf_alloc_order};
5206
5207void
5208order_regs_for_local_alloc ()
5209{
5210 static int last_order_nonleaf = 1;
5211
5212 if (regs_ever_live[15] != last_order_nonleaf)
5213 {
5214 last_order_nonleaf = !last_order_nonleaf;
d2889939
RK
5215 bcopy ((char *) reg_alloc_orders[last_order_nonleaf],
5216 (char *) reg_alloc_order, FIRST_PSEUDO_REGISTER * sizeof (int));
ab835497
RK
5217 }
5218}
5219\f
e0d80184
DM
5220/* Return 1 if REG and MEM are legitimate enough to allow the various
5221 mem<-->reg splits to be run. */
5222
5223int
e61c29e9 5224sparc_splitdi_legitimate (reg, mem)
e0d80184
DM
5225 rtx reg;
5226 rtx mem;
5227{
e0d80184
DM
5228 /* Punt if we are here by mistake. */
5229 if (! reload_completed)
9208e4b2 5230 abort ();
e0d80184
DM
5231
5232 /* We must have an offsettable memory reference. */
5233 if (! offsettable_memref_p (mem))
5234 return 0;
5235
5236 /* If we have legitimate args for ldd/std, we do not want
5237 the split to happen. */
5238 if ((REGNO (reg) % 2) == 0
5239 && mem_min_alignment (mem, 8))
5240 return 0;
5241
5242 /* Success. */
5243 return 1;
5244}
5245
e61c29e9
DM
5246/* Return 1 if x and y are some kind of REG and they refer to
5247 different hard registers. This test is guarenteed to be
5248 run after reload. */
5249
5250int
5251sparc_absnegfloat_split_legitimate (x, y)
5252 rtx x, y;
5253{
5254 if (GET_CODE (x) == SUBREG)
5255 x = alter_subreg (x);
5256 if (GET_CODE (x) != REG)
5257 return 0;
5258 if (GET_CODE (y) == SUBREG)
5259 y = alter_subreg (y);
5260 if (GET_CODE (y) != REG)
5261 return 0;
5262 if (REGNO (x) == REGNO (y))
5263 return 0;
5264 return 1;
5265}
5266
35016322
JW
5267/* Return 1 if REGNO (reg1) is even and REGNO (reg1) == REGNO (reg2) - 1.
5268 This makes them candidates for using ldd and std insns.
5269
bfd6bc60 5270 Note reg1 and reg2 *must* be hard registers. */
35016322
JW
5271
5272int
7c56249d 5273registers_ok_for_ldd_peep (reg1, reg2)
35016322
JW
5274 rtx reg1, reg2;
5275{
35016322
JW
5276 /* We might have been passed a SUBREG. */
5277 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
5278 return 0;
5279
35016322
JW
5280 if (REGNO (reg1) % 2 != 0)
5281 return 0;
5282
bfd6bc60
JC
5283 /* Integer ldd is deprecated in SPARC V9 */
5284 if (TARGET_V9 && REGNO (reg1) < 32)
5285 return 0;
5286
35016322 5287 return (REGNO (reg1) == REGNO (reg2) - 1);
35016322
JW
5288}
5289
5290/* Return 1 if addr1 and addr2 are suitable for use in an ldd or
5291 std insn.
5292
5293 This can only happen when addr1 and addr2 are consecutive memory
5294 locations (addr1 + 4 == addr2). addr1 must also be aligned on a
5295 64 bit boundary (addr1 % 8 == 0).
5296
5297 We know %sp and %fp are kept aligned on a 64 bit boundary. Other
5298 registers are assumed to *never* be properly aligned and are
5299 rejected.
5300
5301 Knowing %sp and %fp are kept aligned on a 64 bit boundary, we
5302 need only check that the offset for addr1 % 8 == 0. */
5303
5304int
7c56249d 5305addrs_ok_for_ldd_peep (addr1, addr2)
35016322
JW
5306 rtx addr1, addr2;
5307{
5308 int reg1, offset1;
5309
5310 /* Extract a register number and offset (if used) from the first addr. */
5311 if (GET_CODE (addr1) == PLUS)
5312 {
5313 /* If not a REG, return zero. */
5314 if (GET_CODE (XEXP (addr1, 0)) != REG)
5315 return 0;
5316 else
5317 {
5318 reg1 = REGNO (XEXP (addr1, 0));
5319 /* The offset must be constant! */
5320 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
5321 return 0;
5322 offset1 = INTVAL (XEXP (addr1, 1));
5323 }
5324 }
5325 else if (GET_CODE (addr1) != REG)
5326 return 0;
5327 else
5328 {
5329 reg1 = REGNO (addr1);
5330 /* This was a simple (mem (reg)) expression. Offset is 0. */
5331 offset1 = 0;
5332 }
5333
5334 /* Make sure the second address is a (mem (plus (reg) (const_int). */
5335 if (GET_CODE (addr2) != PLUS)
5336 return 0;
5337
5338 if (GET_CODE (XEXP (addr2, 0)) != REG
5339 || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
5340 return 0;
5341
5342 /* Only %fp and %sp are allowed. Additionally both addresses must
5343 use the same register. */
5344 if (reg1 != FRAME_POINTER_REGNUM && reg1 != STACK_POINTER_REGNUM)
5345 return 0;
5346
5347 if (reg1 != REGNO (XEXP (addr2, 0)))
5348 return 0;
5349
2296cba3 5350 /* The first offset must be evenly divisible by 8 to ensure the
35016322
JW
5351 address is 64 bit aligned. */
5352 if (offset1 % 8 != 0)
5353 return 0;
5354
5355 /* The offset for the second addr must be 4 more than the first addr. */
5356 if (INTVAL (XEXP (addr2, 1)) != offset1 + 4)
5357 return 0;
5358
5359 /* All the tests passed. addr1 and addr2 are valid for ldd and std
5360 instructions. */
5361 return 1;
5362}
7c56249d
JL
5363
5364/* Return 1 if reg is a pseudo, or is the first register in
5365 a hard register pair. This makes it a candidate for use in
5366 ldd and std insns. */
5367
5368int
5369register_ok_for_ldd (reg)
5370 rtx reg;
5371{
7c56249d
JL
5372 /* We might have been passed a SUBREG. */
5373 if (GET_CODE (reg) != REG)
5374 return 0;
5375
5376 if (REGNO (reg) < FIRST_PSEUDO_REGISTER)
5377 return (REGNO (reg) % 2 == 0);
5378 else
5379 return 1;
7c56249d 5380}
ab835497 5381\f
ab835497
RK
5382/* Print operand X (an rtx) in assembler syntax to file FILE.
5383 CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
5384 For `%' followed by punctuation, CODE is the punctuation and X is null. */
5385
5386void
5387print_operand (file, x, code)
5388 FILE *file;
5389 rtx x;
5390 int code;
5391{
5392 switch (code)
5393 {
5394 case '#':
5395 /* Output a 'nop' if there's nothing for the delay slot. */
5396 if (dbr_sequence_length () == 0)
e0d80184 5397 fputs ("\n\t nop", file);
ab835497
RK
5398 return;
5399 case '*':
c6ce0969
JW
5400 /* Output an annul flag if there's nothing for the delay slot and we
5401 are optimizing. This is always used with '(' below. */
5402 /* Sun OS 4.1.1 dbx can't handle an annulled unconditional branch;
5403 this is a dbx bug. So, we only do this when optimizing. */
bfd6bc60
JC
5404 /* On UltraSPARC, a branch in a delay slot causes a pipeline flush.
5405 Always emit a nop in case the next instruction is a branch. */
5406 if (dbr_sequence_length () == 0
284d86e9 5407 && (optimize && (int)sparc_cpu < PROCESSOR_V9))
c6ce0969
JW
5408 fputs (",a", file);
5409 return;
5410 case '(':
5411 /* Output a 'nop' if there's nothing for the delay slot and we are
5412 not optimizing. This is always used with '*' above. */
bfd6bc60 5413 if (dbr_sequence_length () == 0
284d86e9 5414 && ! (optimize && (int)sparc_cpu < PROCESSOR_V9))
e0d80184 5415 fputs ("\n\t nop", file);
ab835497 5416 return;
61a55e8b 5417 case '_':
a0a301fc
DE
5418 /* Output the Embedded Medium/Anywhere code model base register. */
5419 fputs (EMBMEDANY_BASE_REG, file);
61a55e8b
DE
5420 return;
5421 case '@':
5422 /* Print out what we are using as the frame pointer. This might
5423 be %fp, or might be %sp+offset. */
5424 /* ??? What if offset is too big? Perhaps the caller knows it isn't? */
5425 fprintf (file, "%s+%d", frame_base_name, frame_base_offset);
5426 return;
ab835497
RK
5427 case 'Y':
5428 /* Adjust the operand to take into account a RESTORE operation. */
bfd6bc60
JC
5429 if (GET_CODE (x) == CONST_INT)
5430 break;
5431 else if (GET_CODE (x) != REG)
b3b1e8bd
JW
5432 output_operand_lossage ("Invalid %%Y operand");
5433 else if (REGNO (x) < 8)
ab835497
RK
5434 fputs (reg_names[REGNO (x)], file);
5435 else if (REGNO (x) >= 24 && REGNO (x) < 32)
5436 fputs (reg_names[REGNO (x)-16], file);
5437 else
b3b1e8bd 5438 output_operand_lossage ("Invalid %%Y operand");
ab835497 5439 return;
0f39a9aa
DE
5440 case 'L':
5441 /* Print out the low order register name of a register pair. */
5442 if (WORDS_BIG_ENDIAN)
5443 fputs (reg_names[REGNO (x)+1], file);
5444 else
5445 fputs (reg_names[REGNO (x)], file);
5446 return;
5447 case 'H':
5448 /* Print out the high order register name of a register pair. */
5449 if (WORDS_BIG_ENDIAN)
5450 fputs (reg_names[REGNO (x)], file);
5451 else
5452 fputs (reg_names[REGNO (x)+1], file);
5453 return;
ab835497 5454 case 'R':
795068a4 5455 /* Print out the second register name of a register pair or quad.
ab835497
RK
5456 I.e., R (%o0) => %o1. */
5457 fputs (reg_names[REGNO (x)+1], file);
5458 return;
795068a4
JW
5459 case 'S':
5460 /* Print out the third register name of a register quad.
5461 I.e., S (%o0) => %o2. */
5462 fputs (reg_names[REGNO (x)+2], file);
5463 return;
5464 case 'T':
5465 /* Print out the fourth register name of a register quad.
5466 I.e., T (%o0) => %o3. */
5467 fputs (reg_names[REGNO (x)+3], file);
5468 return;
304b7a23
DE
5469 case 'x':
5470 /* Print a condition code register. */
5471 if (REGNO (x) == SPARC_ICC_REG)
5472 {
5473 /* We don't handle CC[X]_NOOVmode because they're not supposed
5474 to occur here. */
5475 if (GET_MODE (x) == CCmode)
5476 fputs ("%icc", file);
5477 else if (GET_MODE (x) == CCXmode)
5478 fputs ("%xcc", file);
5479 else
5480 abort ();
5481 }
5482 else
5483 /* %fccN register */
5484 fputs (reg_names[REGNO (x)], file);
5485 return;
ab835497
RK
5486 case 'm':
5487 /* Print the operand's address only. */
5488 output_address (XEXP (x, 0));
5489 return;
5490 case 'r':
5491 /* In this case we need a register. Use %g0 if the
77a02b01 5492 operand is const0_rtx. */
76052e74
RS
5493 if (x == const0_rtx
5494 || (GET_MODE (x) != VOIDmode && x == CONST0_RTX (GET_MODE (x))))
ab835497
RK
5495 {
5496 fputs ("%g0", file);
5497 return;
5498 }
5499 else
5500 break;
5501
61a55e8b 5502 case 'A':
ab835497
RK
5503 switch (GET_CODE (x))
5504 {
5505 case IOR: fputs ("or", file); break;
5506 case AND: fputs ("and", file); break;
5507 case XOR: fputs ("xor", file); break;
b3b1e8bd 5508 default: output_operand_lossage ("Invalid %%A operand");
ab835497
RK
5509 }
5510 return;
5511
5512 case 'B':
5513 switch (GET_CODE (x))
5514 {
5515 case IOR: fputs ("orn", file); break;
5516 case AND: fputs ("andn", file); break;
5517 case XOR: fputs ("xnor", file); break;
b3b1e8bd 5518 default: output_operand_lossage ("Invalid %%B operand");
ab835497
RK
5519 }
5520 return;
5521
304b7a23
DE
5522 /* These are used by the conditional move instructions. */
5523 case 'c' :
61a55e8b 5524 case 'C':
304b7a23 5525 {
5c5c34a4
JJ
5526 enum rtx_code rc = GET_CODE (x);
5527
5528 if (code == 'c')
5529 {
5530 enum machine_mode mode = GET_MODE (XEXP (x, 0));
5531 if (mode == CCFPmode || mode == CCFPEmode)
5532 rc = reverse_condition_maybe_unordered (GET_CODE (x));
5533 else
5534 rc = reverse_condition (GET_CODE (x));
5535 }
304b7a23
DE
5536 switch (rc)
5537 {
5538 case NE: fputs ("ne", file); break;
5539 case EQ: fputs ("e", file); break;
5540 case GE: fputs ("ge", file); break;
5541 case GT: fputs ("g", file); break;
5542 case LE: fputs ("le", file); break;
5543 case LT: fputs ("l", file); break;
5544 case GEU: fputs ("geu", file); break;
5545 case GTU: fputs ("gu", file); break;
5546 case LEU: fputs ("leu", file); break;
5547 case LTU: fputs ("lu", file); break;
5c5c34a4
JJ
5548 case LTGT: fputs ("lg", file); break;
5549 case UNORDERED: fputs ("u", file); break;
5550 case ORDERED: fputs ("o", file); break;
5551 case UNLT: fputs ("ul", file); break;
5552 case UNLE: fputs ("ule", file); break;
5553 case UNGT: fputs ("ug", file); break;
5554 case UNGE: fputs ("uge", file); break;
5555 case UNEQ: fputs ("ue", file); break;
304b7a23
DE
5556 default: output_operand_lossage (code == 'c'
5557 ? "Invalid %%c operand"
5558 : "Invalid %%C operand");
5559 }
5560 return;
5561 }
5562
5563 /* These are used by the movr instruction pattern. */
5564 case 'd':
61a55e8b 5565 case 'D':
304b7a23
DE
5566 {
5567 enum rtx_code rc = (code == 'd'
5568 ? reverse_condition (GET_CODE (x))
5569 : GET_CODE (x));
5570 switch (rc)
5571 {
5572 case NE: fputs ("ne", file); break;
5573 case EQ: fputs ("e", file); break;
5574 case GE: fputs ("gez", file); break;
5575 case LT: fputs ("lz", file); break;
5576 case LE: fputs ("lez", file); break;
5577 case GT: fputs ("gz", file); break;
5578 default: output_operand_lossage (code == 'd'
5579 ? "Invalid %%d operand"
5580 : "Invalid %%D operand");
5581 }
5582 return;
5583 }
61a55e8b 5584
ab835497
RK
5585 case 'b':
5586 {
5587 /* Print a sign-extended character. */
5588 int i = INTVAL (x) & 0xff;
5589 if (i & 0x80)
5590 i |= 0xffffff00;
5591 fprintf (file, "%d", i);
5592 return;
5593 }
5594
d2889939
RK
5595 case 'f':
5596 /* Operand must be a MEM; write its address. */
5597 if (GET_CODE (x) != MEM)
5598 output_operand_lossage ("Invalid %%f operand");
5599 output_address (XEXP (x, 0));
5600 return;
5601
ab835497
RK
5602 case 0:
5603 /* Do nothing special. */
5604 break;
5605
5606 default:
5607 /* Undocumented flag. */
415f583e 5608 output_operand_lossage ("invalid operand output code");
ab835497
RK
5609 }
5610
5611 if (GET_CODE (x) == REG)
5612 fputs (reg_names[REGNO (x)], file);
5613 else if (GET_CODE (x) == MEM)
5614 {
5615 fputc ('[', file);
ab835497 5616 /* Poor Sun assembler doesn't understand absolute addressing. */
c4ce6853
DE
5617 if (CONSTANT_P (XEXP (x, 0))
5618 && ! TARGET_LIVE_G0)
ab835497
RK
5619 fputs ("%g0+", file);
5620 output_address (XEXP (x, 0));
5621 fputc (']', file);
5622 }
5623 else if (GET_CODE (x) == HIGH)
5624 {
5625 fputs ("%hi(", file);
5626 output_addr_const (file, XEXP (x, 0));
5627 fputc (')', file);
5628 }
5629 else if (GET_CODE (x) == LO_SUM)
5630 {
5631 print_operand (file, XEXP (x, 0), 0);
e0d80184
DM
5632 if (TARGET_CM_MEDMID)
5633 fputs ("+%l44(", file);
5634 else
5635 fputs ("+%lo(", file);
ab835497
RK
5636 output_addr_const (file, XEXP (x, 1));
5637 fputc (')', file);
5638 }
e601abce
JW
5639 else if (GET_CODE (x) == CONST_DOUBLE
5640 && (GET_MODE (x) == VOIDmode
5641 || GET_MODE_CLASS (GET_MODE (x)) == MODE_INT))
ab835497
RK
5642 {
5643 if (CONST_DOUBLE_HIGH (x) == 0)
0d9484c5 5644 fprintf (file, "%u", (unsigned int) CONST_DOUBLE_LOW (x));
ab835497
RK
5645 else if (CONST_DOUBLE_HIGH (x) == -1
5646 && CONST_DOUBLE_LOW (x) < 0)
0d9484c5 5647 fprintf (file, "%d", (int) CONST_DOUBLE_LOW (x));
ab835497 5648 else
e601abce 5649 output_operand_lossage ("long long constant not a valid immediate operand");
ab835497 5650 }
e601abce
JW
5651 else if (GET_CODE (x) == CONST_DOUBLE)
5652 output_operand_lossage ("floating point constant not a valid immediate operand");
ab835497
RK
5653 else { output_addr_const (file, x); }
5654}
5655\f
5656/* This function outputs assembler code for VALUE to FILE, where VALUE is
5657 a 64 bit (DImode) value. */
5658
5659/* ??? If there is a 64 bit counterpart to .word that the assembler
5660 understands, then using that would simply this code greatly. */
61a55e8b
DE
5661/* ??? We only output .xword's for symbols and only then in environments
5662 where the assembler can handle them. */
ab835497
RK
5663
5664void
5665output_double_int (file, value)
5666 FILE *file;
5667 rtx value;
5668{
5669 if (GET_CODE (value) == CONST_INT)
5670 {
f4f8e370
DE
5671 /* ??? This has endianness issues. */
5672#if HOST_BITS_PER_WIDE_INT == 64
5673 HOST_WIDE_INT xword = INTVAL (value);
5674 HOST_WIDE_INT high, low;
5675
5676 high = (xword >> 32) & 0xffffffff;
5677 low = xword & 0xffffffff;
3a598fbe
JL
5678 ASM_OUTPUT_INT (file, GEN_INT (high));
5679 ASM_OUTPUT_INT (file, GEN_INT (low));
f4f8e370 5680#else
ab835497
RK
5681 if (INTVAL (value) < 0)
5682 ASM_OUTPUT_INT (file, constm1_rtx);
5683 else
5684 ASM_OUTPUT_INT (file, const0_rtx);
5685 ASM_OUTPUT_INT (file, value);
f4f8e370 5686#endif
ab835497
RK
5687 }
5688 else if (GET_CODE (value) == CONST_DOUBLE)
5689 {
3a598fbe
JL
5690 ASM_OUTPUT_INT (file, GEN_INT (CONST_DOUBLE_HIGH (value)));
5691 ASM_OUTPUT_INT (file, GEN_INT (CONST_DOUBLE_LOW (value)));
ab835497
RK
5692 }
5693 else if (GET_CODE (value) == SYMBOL_REF
5694 || GET_CODE (value) == CONST
61a55e8b 5695 || GET_CODE (value) == PLUS
a9e27770 5696 || (TARGET_ARCH64 &&
61a55e8b 5697 (GET_CODE (value) == LABEL_REF
9fa9626b 5698 || GET_CODE (value) == CODE_LABEL
61a55e8b 5699 || GET_CODE (value) == MINUS)))
ab835497 5700 {
f3b8847b 5701 if (! TARGET_V9)
61a55e8b
DE
5702 {
5703 ASM_OUTPUT_INT (file, const0_rtx);
5704 ASM_OUTPUT_INT (file, value);
5705 }
5706 else
5707 {
5708 fprintf (file, "\t%s\t", ASM_LONGLONG);
5709 output_addr_const (file, value);
5710 fprintf (file, "\n");
5711 }
ab835497
RK
5712 }
5713 else
5714 abort ();
5715}
210aa14a 5716\f
b0468b84
RK
5717/* Return the value of a code used in the .proc pseudo-op that says
5718 what kind of result this function returns. For non-C types, we pick
5719 the closest C type. */
5720
77a02b01
JW
5721#ifndef CHAR_TYPE_SIZE
5722#define CHAR_TYPE_SIZE BITS_PER_UNIT
5723#endif
5724
5725#ifndef SHORT_TYPE_SIZE
5726#define SHORT_TYPE_SIZE (BITS_PER_UNIT * 2)
5727#endif
5728
5729#ifndef INT_TYPE_SIZE
5730#define INT_TYPE_SIZE BITS_PER_WORD
5731#endif
5732
5733#ifndef LONG_TYPE_SIZE
5734#define LONG_TYPE_SIZE BITS_PER_WORD
5735#endif
5736
5737#ifndef LONG_LONG_TYPE_SIZE
5738#define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
5739#endif
5740
5741#ifndef FLOAT_TYPE_SIZE
5742#define FLOAT_TYPE_SIZE BITS_PER_WORD
5743#endif
5744
5745#ifndef DOUBLE_TYPE_SIZE
5746#define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
5747#endif
5748
5749#ifndef LONG_DOUBLE_TYPE_SIZE
5750#define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
5751#endif
210aa14a
RS
5752
5753unsigned long
5754sparc_type_code (type)
5755 register tree type;
5756{
5757 register unsigned long qualifiers = 0;
e5e809f4 5758 register unsigned shift;
210aa14a 5759
abc95ed3 5760 /* Only the first 30 bits of the qualifier are valid. We must refrain from
aee2c3c5
JW
5761 setting more, since some assemblers will give an error for this. Also,
5762 we must be careful to avoid shifts of 32 bits or more to avoid getting
5763 unpredictable results. */
5764
e5e809f4 5765 for (shift = 6; shift < 30; shift += 2, type = TREE_TYPE (type))
210aa14a
RS
5766 {
5767 switch (TREE_CODE (type))
5768 {
5769 case ERROR_MARK:
5770 return qualifiers;
5771
5772 case ARRAY_TYPE:
e5e809f4 5773 qualifiers |= (3 << shift);
210aa14a
RS
5774 break;
5775
5776 case FUNCTION_TYPE:
5777 case METHOD_TYPE:
e5e809f4 5778 qualifiers |= (2 << shift);
210aa14a
RS
5779 break;
5780
5781 case POINTER_TYPE:
5782 case REFERENCE_TYPE:
5783 case OFFSET_TYPE:
e5e809f4 5784 qualifiers |= (1 << shift);
210aa14a 5785 break;
ab835497 5786
210aa14a
RS
5787 case RECORD_TYPE:
5788 return (qualifiers | 8);
5789
5790 case UNION_TYPE:
b0468b84 5791 case QUAL_UNION_TYPE:
210aa14a
RS
5792 return (qualifiers | 9);
5793
5794 case ENUMERAL_TYPE:
5795 return (qualifiers | 10);
5796
5797 case VOID_TYPE:
5798 return (qualifiers | 16);
5799
5800 case INTEGER_TYPE:
654209e6
JW
5801 /* If this is a range type, consider it to be the underlying
5802 type. */
5803 if (TREE_TYPE (type) != 0)
e5e809f4 5804 break;
654209e6 5805
77a02b01 5806 /* Carefully distinguish all the standard types of C,
b0468b84
RK
5807 without messing up if the language is not C. We do this by
5808 testing TYPE_PRECISION and TREE_UNSIGNED. The old code used to
5809 look at both the names and the above fields, but that's redundant.
5810 Any type whose size is between two C types will be considered
5811 to be the wider of the two types. Also, we do not have a
5812 special code to use for "long long", so anything wider than
5813 long is treated the same. Note that we can't distinguish
5814 between "int" and "long" in this code if they are the same
5815 size, but that's fine, since neither can the assembler. */
5816
5817 if (TYPE_PRECISION (type) <= CHAR_TYPE_SIZE)
5818 return (qualifiers | (TREE_UNSIGNED (type) ? 12 : 2));
77a02b01 5819
b0468b84
RK
5820 else if (TYPE_PRECISION (type) <= SHORT_TYPE_SIZE)
5821 return (qualifiers | (TREE_UNSIGNED (type) ? 13 : 3));
77a02b01 5822
b0468b84 5823 else if (TYPE_PRECISION (type) <= INT_TYPE_SIZE)
77a02b01
JW
5824 return (qualifiers | (TREE_UNSIGNED (type) ? 14 : 4));
5825
b0468b84 5826 else
77a02b01
JW
5827 return (qualifiers | (TREE_UNSIGNED (type) ? 15 : 5));
5828
210aa14a 5829 case REAL_TYPE:
e5e809f4
JL
5830 /* If this is a range type, consider it to be the underlying
5831 type. */
5832 if (TREE_TYPE (type) != 0)
5833 break;
5834
77a02b01
JW
5835 /* Carefully distinguish all the standard types of C,
5836 without messing up if the language is not C. */
b0468b84 5837
77a02b01 5838 if (TYPE_PRECISION (type) == FLOAT_TYPE_SIZE)
210aa14a 5839 return (qualifiers | 6);
b0468b84
RK
5840
5841 else
5842 return (qualifiers | 7);
210aa14a
RS
5843
5844 case COMPLEX_TYPE: /* GNU Fortran COMPLEX type. */
13d39dbc 5845 /* ??? We need to distinguish between double and float complex types,
c82aa69a
JW
5846 but I don't know how yet because I can't reach this code from
5847 existing front-ends. */
5848 return (qualifiers | 7); /* Who knows? */
5849
210aa14a
RS
5850 case CHAR_TYPE: /* GNU Pascal CHAR type. Not used in C. */
5851 case BOOLEAN_TYPE: /* GNU Fortran BOOLEAN type. */
5852 case FILE_TYPE: /* GNU Pascal FILE type. */
26902ae0 5853 case SET_TYPE: /* GNU Pascal SET type. */
210aa14a 5854 case LANG_TYPE: /* ? */
26902ae0 5855 return qualifiers;
210aa14a
RS
5856
5857 default:
5858 abort (); /* Not a type! */
5859 }
5860 }
e5e809f4
JL
5861
5862 return qualifiers;
210aa14a 5863}
ead69aea 5864\f
61a55e8b
DE
5865/* Nested function support. */
5866
5867/* Emit RTL insns to initialize the variable parts of a trampoline.
5868 FNADDR is an RTX for the address of the function's pure code.
5869 CXT is an RTX for the static chain value for the function.
5870
5871 This takes 16 insns: 2 shifts & 2 ands (to split up addresses), 4 sethi
5872 (to load in opcodes), 4 iors (to merge address and opcodes), and 4 writes
5873 (to store insns). This is a bit excessive. Perhaps a different
5874 mechanism would be better here.
5875
849a528d 5876 Emit enough FLUSH insns to synchronize the data and instruction caches. */
61a55e8b
DE
5877
5878void
5879sparc_initialize_trampoline (tramp, fnaddr, cxt)
5880 rtx tramp, fnaddr, cxt;
5881{
c6b0465b
JC
5882 /* SPARC 32 bit trampoline:
5883
e0d80184
DM
5884 sethi %hi(fn), %g1
5885 sethi %hi(static), %g2
5886 jmp %g1+%lo(fn)
5887 or %g2, %lo(static), %g2
c6b0465b
JC
5888
5889 SETHI i,r = 00rr rrr1 00ii iiii iiii iiii iiii iiii
5890 JMPL r+i,d = 10dd ddd1 1100 0rrr rr1i iiii iiii iiii
5891 */
959eb758
DM
5892#ifdef TRANSFER_FROM_TRAMPOLINE
5893 emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "__enable_execute_stack"),
5894 0, VOIDmode, 1, tramp, Pmode);
5895#endif
c6b0465b
JC
5896
5897 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 0)),
5898 expand_binop (SImode, ior_optab,
5899 expand_shift (RSHIFT_EXPR, SImode, fnaddr,
5900 size_int (10), 0, 1),
5901 GEN_INT (0x03000000),
5902 NULL_RTX, 1, OPTAB_DIRECT));
5903
5904 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 4)),
5905 expand_binop (SImode, ior_optab,
5906 expand_shift (RSHIFT_EXPR, SImode, cxt,
5907 size_int (10), 0, 1),
5908 GEN_INT (0x05000000),
5909 NULL_RTX, 1, OPTAB_DIRECT));
5910
5911 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 8)),
5912 expand_binop (SImode, ior_optab,
5913 expand_and (fnaddr, GEN_INT (0x3ff), NULL_RTX),
5914 GEN_INT (0x81c06000),
5915 NULL_RTX, 1, OPTAB_DIRECT));
5916
5917 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
5918 expand_binop (SImode, ior_optab,
5919 expand_and (cxt, GEN_INT (0x3ff), NULL_RTX),
5920 GEN_INT (0x8410a000),
5921 NULL_RTX, 1, OPTAB_DIRECT));
5922
254110c2 5923 emit_insn (gen_flush (validize_mem (gen_rtx_MEM (SImode, tramp))));
c6b0465b
JC
5924 /* On UltraSPARC a flush flushes an entire cache line. The trampoline is
5925 aligned on a 16 byte boundary so one flush clears it all. */
5926 if (sparc_cpu != PROCESSOR_ULTRASPARC)
5927 emit_insn (gen_flush (validize_mem (gen_rtx_MEM (SImode,
5928 plus_constant (tramp, 8)))));
61a55e8b
DE
5929}
5930
849a528d
RK
5931/* The 64 bit version is simpler because it makes more sense to load the
5932 values as "immediate" data out of the trampoline. It's also easier since
5933 we can read the PC without clobbering a register. */
5934
61a55e8b
DE
5935void
5936sparc64_initialize_trampoline (tramp, fnaddr, cxt)
5937 rtx tramp, fnaddr, cxt;
5938{
345a6161
DM
5939#ifdef TRANSFER_FROM_TRAMPOLINE
5940 emit_library_call (gen_rtx (SYMBOL_REF, Pmode, "__enable_execute_stack"),
5941 0, VOIDmode, 1, tramp, Pmode);
5942#endif
5943
c6b0465b 5944 /*
e0d80184
DM
5945 rd %pc, %g1
5946 ldx [%g1+24], %g5
5947 jmp %g5
5948 ldx [%g1+16], %g5
c6b0465b
JC
5949 +16 bytes data
5950 */
5951
5952 emit_move_insn (gen_rtx_MEM (SImode, tramp),
5953 GEN_INT (0x83414000));
5954 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 4)),
5955 GEN_INT (0xca586018));
5956 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 8)),
345a6161 5957 GEN_INT (0x81c14000));
c6b0465b
JC
5958 emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
5959 GEN_INT (0xca586010));
5960 emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 16)), cxt);
345a6161 5961 emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 24)), fnaddr);
d4203cb4 5962 emit_insn (gen_flushdi (validize_mem (gen_rtx_MEM (DImode, tramp))));
345a6161 5963
c6b0465b 5964 if (sparc_cpu != PROCESSOR_ULTRASPARC)
d4203cb4 5965 emit_insn (gen_flushdi (validize_mem (gen_rtx_MEM (DImode, plus_constant (tramp, 8)))));
61a55e8b
DE
5966}
5967\f
c819be5b
JW
5968/* Subroutines to support a flat (single) register window calling
5969 convention. */
5970
5971/* Single-register window sparc stack frames look like:
5972
5973 Before call After call
5974 +-----------------------+ +-----------------------+
86735b02
DE
5975 high | | | |
5976 mem | caller's temps. | | caller's temps. |
c819be5b
JW
5977 | | | |
5978 +-----------------------+ +-----------------------+
5979 | | | |
5980 | arguments on stack. | | arguments on stack. |
86735b02
DE
5981 | | | |
5982 +-----------------------+FP+92->+-----------------------+
c819be5b
JW
5983 | 6 words to save | | 6 words to save |
5984 | arguments passed | | arguments passed |
5985 | in registers, even | | in registers, even |
86735b02
DE
5986 | if not passed. | | if not passed. |
5987 SP+68->+-----------------------+FP+68->+-----------------------+
5988 | 1 word struct addr | | 1 word struct addr |
5989 +-----------------------+FP+64->+-----------------------+
5990 | | | |
5991 | 16 word reg save area | | 16 word reg save area |
5992 | | | |
5993 SP->+-----------------------+ FP->+-----------------------+
5994 | 4 word area for |
5995 | fp/alu reg moves |
5996 FP-16->+-----------------------+
5997 | |
5998 | local variables |
5999 | |
6000 +-----------------------+
6001 | |
c819be5b 6002 | fp register save |
86735b02
DE
6003 | |
6004 +-----------------------+
6005 | |
c819be5b
JW
6006 | gp register save |
6007 | |
86735b02
DE
6008 +-----------------------+
6009 | |
c819be5b 6010 | alloca allocations |
86735b02
DE
6011 | |
6012 +-----------------------+
6013 | |
c819be5b 6014 | arguments on stack |
86735b02
DE
6015 | |
6016 SP+92->+-----------------------+
c819be5b 6017 | 6 words to save |
86735b02 6018 | arguments passed |
c819be5b 6019 | in registers, even |
86735b02
DE
6020 low | if not passed. |
6021 memory SP+68->+-----------------------+
6022 | 1 word struct addr |
6023 SP+64->+-----------------------+
6024 | |
6025 I 16 word reg save area |
6026 | |
6027 SP->+-----------------------+ */
c819be5b 6028
c6aa9ce1 6029/* Structure to be filled in by sparc_flat_compute_frame_size with register
c819be5b
JW
6030 save masks, and offsets for the current function. */
6031
6032struct sparc_frame_info
6033{
6034 unsigned long total_size; /* # bytes that the entire frame takes up. */
6035 unsigned long var_size; /* # bytes that variables take up. */
6036 unsigned long args_size; /* # bytes that outgoing arguments take up. */
6037 unsigned long extra_size; /* # bytes of extra gunk. */
6038 unsigned int gp_reg_size; /* # bytes needed to store gp regs. */
6039 unsigned int fp_reg_size; /* # bytes needed to store fp regs. */
86735b02 6040 unsigned long gmask; /* Mask of saved gp registers. */
c819be5b 6041 unsigned long fmask; /* Mask of saved fp registers. */
86735b02 6042 unsigned long reg_offset; /* Offset from new sp to store regs. */
c819be5b
JW
6043 int initialized; /* Nonzero if frame size already calculated. */
6044};
6045
c6aa9ce1 6046/* Current frame information calculated by sparc_flat_compute_frame_size. */
c819be5b
JW
6047struct sparc_frame_info current_frame_info;
6048
6049/* Zero structure to initialize current_frame_info. */
6050struct sparc_frame_info zero_frame_info;
6051
6052/* Tell prologue and epilogue if register REGNO should be saved / restored. */
6053
86735b02
DE
6054#define RETURN_ADDR_REGNUM 15
6055#define FRAME_POINTER_MASK (1 << (FRAME_POINTER_REGNUM))
6056#define RETURN_ADDR_MASK (1 << (RETURN_ADDR_REGNUM))
6057
c819be5b
JW
6058#define MUST_SAVE_REGISTER(regno) \
6059 ((regs_ever_live[regno] && !call_used_regs[regno]) \
6060 || (regno == FRAME_POINTER_REGNUM && frame_pointer_needed) \
86735b02 6061 || (regno == RETURN_ADDR_REGNUM && regs_ever_live[RETURN_ADDR_REGNUM]))
c819be5b
JW
6062
6063/* Return the bytes needed to compute the frame pointer from the current
6064 stack pointer. */
6065
6066unsigned long
c6aa9ce1 6067sparc_flat_compute_frame_size (size)
c819be5b
JW
6068 int size; /* # of var. bytes allocated. */
6069{
6070 int regno;
6071 unsigned long total_size; /* # bytes that the entire frame takes up. */
6072 unsigned long var_size; /* # bytes that variables take up. */
6073 unsigned long args_size; /* # bytes that outgoing arguments take up. */
6074 unsigned long extra_size; /* # extra bytes. */
6075 unsigned int gp_reg_size; /* # bytes needed to store gp regs. */
6076 unsigned int fp_reg_size; /* # bytes needed to store fp regs. */
86735b02 6077 unsigned long gmask; /* Mask of saved gp registers. */
c819be5b 6078 unsigned long fmask; /* Mask of saved fp registers. */
86735b02
DE
6079 unsigned long reg_offset; /* Offset to register save area. */
6080 int need_aligned_p; /* 1 if need the save area 8 byte aligned. */
c819be5b
JW
6081
6082 /* This is the size of the 16 word reg save area, 1 word struct addr
6083 area, and 4 word fp/alu register copy area. */
8c64be75
SC
6084 extra_size = -STARTING_FRAME_OFFSET + FIRST_PARM_OFFSET(0);
6085 var_size = size;
6086 gp_reg_size = 0;
6087 fp_reg_size = 0;
6088 gmask = 0;
6089 fmask = 0;
6090 reg_offset = 0;
86735b02 6091 need_aligned_p = 0;
c819be5b 6092
8c64be75
SC
6093 args_size = 0;
6094 if (!leaf_function_p ())
6095 {
6096 /* Also include the size needed for the 6 parameter registers. */
6097 args_size = current_function_outgoing_args_size + 24;
6098 }
6099 total_size = var_size + args_size;
6100
c819be5b
JW
6101 /* Calculate space needed for gp registers. */
6102 for (regno = 1; regno <= 31; regno++)
6103 {
6104 if (MUST_SAVE_REGISTER (regno))
6105 {
86735b02
DE
6106 /* If we need to save two regs in a row, ensure there's room to bump
6107 up the address to align it to a doubleword boundary. */
c819be5b
JW
6108 if ((regno & 0x1) == 0 && MUST_SAVE_REGISTER (regno+1))
6109 {
6110 if (gp_reg_size % 8 != 0)
86735b02 6111 gp_reg_size += 4;
c819be5b 6112 gp_reg_size += 2 * UNITS_PER_WORD;
86735b02 6113 gmask |= 3 << regno;
c819be5b 6114 regno++;
86735b02 6115 need_aligned_p = 1;
c819be5b
JW
6116 }
6117 else
6118 {
6119 gp_reg_size += UNITS_PER_WORD;
86735b02 6120 gmask |= 1 << regno;
c819be5b
JW
6121 }
6122 }
6123 }
c819be5b
JW
6124
6125 /* Calculate space needed for fp registers. */
6126 for (regno = 32; regno <= 63; regno++)
6127 {
6128 if (regs_ever_live[regno] && !call_used_regs[regno])
6129 {
6130 fp_reg_size += UNITS_PER_WORD;
6131 fmask |= 1 << (regno - 32);
6132 }
6133 }
6134
86735b02
DE
6135 if (gmask || fmask)
6136 {
6137 int n;
6138 reg_offset = FIRST_PARM_OFFSET(0) + args_size;
6139 /* Ensure save area is 8 byte aligned if we need it. */
6140 n = reg_offset % 8;
6141 if (need_aligned_p && n != 0)
6142 {
6143 total_size += 8 - n;
6144 reg_offset += 8 - n;
6145 }
6146 total_size += gp_reg_size + fp_reg_size;
6147 }
c819be5b 6148
8c64be75
SC
6149 /* If we must allocate a stack frame at all, we must also allocate
6150 room for register window spillage, so as to be binary compatible
6151 with libraries and operating systems that do not use -mflat. */
6152 if (total_size > 0)
6153 total_size += extra_size;
6154 else
6155 extra_size = 0;
c819be5b
JW
6156
6157 total_size = SPARC_STACK_ALIGN (total_size);
6158
6159 /* Save other computed information. */
6160 current_frame_info.total_size = total_size;
6161 current_frame_info.var_size = var_size;
6162 current_frame_info.args_size = args_size;
6163 current_frame_info.extra_size = extra_size;
6164 current_frame_info.gp_reg_size = gp_reg_size;
6165 current_frame_info.fp_reg_size = fp_reg_size;
86735b02 6166 current_frame_info.gmask = gmask;
c819be5b 6167 current_frame_info.fmask = fmask;
86735b02 6168 current_frame_info.reg_offset = reg_offset;
c819be5b
JW
6169 current_frame_info.initialized = reload_completed;
6170
c819be5b
JW
6171 /* Ok, we're done. */
6172 return total_size;
6173}
6174\f
86735b02
DE
6175/* Save/restore registers in GMASK and FMASK at register BASE_REG plus offset
6176 OFFSET.
6177
6178 BASE_REG must be 8 byte aligned. This allows us to test OFFSET for
6179 appropriate alignment and use DOUBLEWORD_OP when we can. We assume
6180 [BASE_REG+OFFSET] will always be a valid address.
6181
6182 WORD_OP is either "st" for save, "ld" for restore.
6183 DOUBLEWORD_OP is either "std" for save, "ldd" for restore. */
c819be5b
JW
6184
6185void
96987e96
DE
6186sparc_flat_save_restore (file, base_reg, offset, gmask, fmask, word_op,
6187 doubleword_op, base_offset)
86735b02 6188 FILE *file;
b1474bb7 6189 const char *base_reg;
86735b02
DE
6190 unsigned int offset;
6191 unsigned long gmask;
6192 unsigned long fmask;
b1474bb7
KG
6193 const char *word_op;
6194 const char *doubleword_op;
c53aa195 6195 unsigned long base_offset;
c819be5b
JW
6196{
6197 int regno;
c819be5b 6198
86735b02
DE
6199 if (gmask == 0 && fmask == 0)
6200 return;
c819be5b 6201
86735b02
DE
6202 /* Save registers starting from high to low. We've already saved the
6203 previous frame pointer and previous return address for the debugger's
6204 sake. The debugger allows us to not need a nop in the epilog if at least
6205 one register is reloaded in addition to return address. */
c819be5b 6206
86735b02 6207 if (gmask)
c819be5b
JW
6208 {
6209 for (regno = 1; regno <= 31; regno++)
6210 {
86735b02 6211 if ((gmask & (1L << regno)) != 0)
c819be5b 6212 {
86735b02 6213 if ((regno & 0x1) == 0 && ((gmask & (1L << (regno+1))) != 0))
c819be5b 6214 {
86735b02
DE
6215 /* We can save two registers in a row. If we're not at a
6216 double word boundary, move to one.
6217 sparc_flat_compute_frame_size ensures there's room to do
6218 this. */
6219 if (offset % 8 != 0)
6220 offset += UNITS_PER_WORD;
6221
c819be5b 6222 if (word_op[0] == 's')
c53aa195 6223 {
e0d80184 6224 fprintf (file, "\t%s\t%s, [%s+%d]\n",
c53aa195
JM
6225 doubleword_op, reg_names[regno],
6226 base_reg, offset);
0021b564 6227 if (dwarf2out_do_frame ())
c53aa195 6228 {
96987e96 6229 char *l = dwarf2out_cfi_label ();
c53aa195
JM
6230 dwarf2out_reg_save (l, regno, offset + base_offset);
6231 dwarf2out_reg_save
6232 (l, regno+1, offset+base_offset + UNITS_PER_WORD);
6233 }
c53aa195 6234 }
c819be5b 6235 else
e0d80184 6236 fprintf (file, "\t%s\t[%s+%d], %s\n",
86735b02 6237 doubleword_op, base_reg, offset,
c819be5b
JW
6238 reg_names[regno]);
6239
86735b02 6240 offset += 2 * UNITS_PER_WORD;
c819be5b
JW
6241 regno++;
6242 }
6243 else
6244 {
6245 if (word_op[0] == 's')
c53aa195 6246 {
e0d80184 6247 fprintf (file, "\t%s\t%s, [%s+%d]\n",
c53aa195
JM
6248 word_op, reg_names[regno],
6249 base_reg, offset);
0021b564 6250 if (dwarf2out_do_frame ())
c53aa195 6251 dwarf2out_reg_save ("", regno, offset + base_offset);
c53aa195 6252 }
c819be5b 6253 else
e0d80184 6254 fprintf (file, "\t%s\t[%s+%d], %s\n",
86735b02 6255 word_op, base_reg, offset, reg_names[regno]);
c819be5b 6256
86735b02 6257 offset += UNITS_PER_WORD;
c819be5b
JW
6258 }
6259 }
6260 }
6261 }
6262
6263 if (fmask)
6264 {
6265 for (regno = 32; regno <= 63; regno++)
6266 {
6267 if ((fmask & (1L << (regno - 32))) != 0)
6268 {
6269 if (word_op[0] == 's')
c53aa195 6270 {
e0d80184 6271 fprintf (file, "\t%s\t%s, [%s+%d]\n",
c53aa195
JM
6272 word_op, reg_names[regno],
6273 base_reg, offset);
0021b564 6274 if (dwarf2out_do_frame ())
c53aa195 6275 dwarf2out_reg_save ("", regno, offset + base_offset);
c53aa195 6276 }
c819be5b 6277 else
e0d80184 6278 fprintf (file, "\t%s\t[%s+%d], %s\n",
86735b02 6279 word_op, base_reg, offset, reg_names[regno]);
c819be5b 6280
86735b02 6281 offset += UNITS_PER_WORD;
c819be5b
JW
6282 }
6283 }
6284 }
6285}
6286\f
6287/* Set up the stack and frame (if desired) for the function. */
6288
6289void
c6aa9ce1 6290sparc_flat_output_function_prologue (file, size)
c819be5b
JW
6291 FILE *file;
6292 int size;
6293{
99f44eba 6294 const char *sp_str = reg_names[STACK_POINTER_REGNUM];
86735b02 6295 unsigned long gmask = current_frame_info.gmask;
a07c1915 6296
1cb36a98
RH
6297 sparc_output_scratch_registers (file);
6298
c6aa9ce1 6299 /* This is only for the human reader. */
bf62bbf1 6300 fprintf (file, "\t%s#PROLOGUE# 0\n", ASM_COMMENT_START);
5e9defae 6301 fprintf (file, "\t%s# vars= %ld, regs= %d/%d, args= %d, extra= %ld\n",
bf62bbf1 6302 ASM_COMMENT_START,
86735b02
DE
6303 current_frame_info.var_size,
6304 current_frame_info.gp_reg_size / 4,
9f8b85b8 6305 current_frame_info.fp_reg_size / 4,
86735b02
DE
6306 current_function_outgoing_args_size,
6307 current_frame_info.extra_size);
c819be5b
JW
6308
6309 size = SPARC_STACK_ALIGN (size);
86735b02
DE
6310 size = (! current_frame_info.initialized
6311 ? sparc_flat_compute_frame_size (size)
6312 : current_frame_info.total_size);
c819be5b 6313
86735b02
DE
6314 /* These cases shouldn't happen. Catch them now. */
6315 if (size == 0 && (gmask || current_frame_info.fmask))
6316 abort ();
c819be5b 6317
86735b02
DE
6318 /* Allocate our stack frame by decrementing %sp.
6319 At present, the only algorithm gdb can use to determine if this is a
6320 flat frame is if we always set %i7 if we set %sp. This can be optimized
6321 in the future by putting in some sort of debugging information that says
6322 this is a `flat' function. However, there is still the case of debugging
6323 code without such debugging information (including cases where most fns
6324 have such info, but there is one that doesn't). So, always do this now
6325 so we don't get a lot of code out there that gdb can't handle.
6326 If the frame pointer isn't needn't then that's ok - gdb won't be able to
6327 distinguish us from a non-flat function but there won't (and shouldn't)
6328 be any differences anyway. The return pc is saved (if necessary) right
6329 after %i7 so gdb won't have to look too far to find it. */
6330 if (size > 0)
c819be5b 6331 {
86735b02 6332 unsigned int reg_offset = current_frame_info.reg_offset;
99f44eba 6333 const char *fp_str = reg_names[FRAME_POINTER_REGNUM];
3bb5de61 6334 const char *t1_str = "%g1";
86735b02
DE
6335
6336 /* Things get a little tricky if local variables take up more than ~4096
6337 bytes and outgoing arguments take up more than ~4096 bytes. When that
6338 happens, the register save area can't be accessed from either end of
6339 the frame. Handle this by decrementing %sp to the start of the gp
6340 register save area, save the regs, update %i7, and then set %sp to its
6341 final value. Given that we only have one scratch register to play
6342 with it is the cheapest solution, and it helps gdb out as it won't
6343 slow down recognition of flat functions.
6344 Don't change the order of insns emitted here without checking with
6345 the gdb folk first. */
6346
ddd5a7c1 6347 /* Is the entire register save area offsettable from %sp? */
fbd039b2 6348 if (reg_offset < 4096 - 64 * (unsigned) UNITS_PER_WORD)
86735b02
DE
6349 {
6350 if (size <= 4096)
6351 {
e0d80184 6352 fprintf (file, "\tadd\t%s, %d, %s\n",
86735b02
DE
6353 sp_str, -size, sp_str);
6354 if (gmask & FRAME_POINTER_MASK)
6355 {
e0d80184 6356 fprintf (file, "\tst\t%s, [%s+%d]\n",
86735b02 6357 fp_str, sp_str, reg_offset);
e0d80184 6358 fprintf (file, "\tsub\t%s, %d, %s\t%s# set up frame pointer\n",
bf62bbf1 6359 sp_str, -size, fp_str, ASM_COMMENT_START);
86735b02
DE
6360 reg_offset += 4;
6361 }
6362 }
6363 else
6364 {
e0d80184 6365 fprintf (file, "\tset\t%d, %s\n\tsub\t%s, %s, %s\n",
86735b02
DE
6366 size, t1_str, sp_str, t1_str, sp_str);
6367 if (gmask & FRAME_POINTER_MASK)
6368 {
e0d80184 6369 fprintf (file, "\tst\t%s, [%s+%d]\n",
86735b02 6370 fp_str, sp_str, reg_offset);
e0d80184 6371 fprintf (file, "\tadd\t%s, %s, %s\t%s# set up frame pointer\n",
bf62bbf1 6372 sp_str, t1_str, fp_str, ASM_COMMENT_START);
86735b02
DE
6373 reg_offset += 4;
6374 }
6375 }
0021b564 6376 if (dwarf2out_do_frame ())
c53aa195 6377 {
96987e96 6378 char *l = dwarf2out_cfi_label ();
c53aa195
JM
6379 if (gmask & FRAME_POINTER_MASK)
6380 {
6381 dwarf2out_reg_save (l, FRAME_POINTER_REGNUM,
6382 reg_offset - 4 - size);
6383 dwarf2out_def_cfa (l, FRAME_POINTER_REGNUM, 0);
6384 }
6385 else
6386 dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, size);
6387 }
86735b02
DE
6388 if (gmask & RETURN_ADDR_MASK)
6389 {
e0d80184 6390 fprintf (file, "\tst\t%s, [%s+%d]\n",
86735b02 6391 reg_names[RETURN_ADDR_REGNUM], sp_str, reg_offset);
0021b564 6392 if (dwarf2out_do_frame ())
c53aa195 6393 dwarf2out_return_save ("", reg_offset - size);
86735b02
DE
6394 reg_offset += 4;
6395 }
6396 sparc_flat_save_restore (file, sp_str, reg_offset,
6397 gmask & ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK),
6398 current_frame_info.fmask,
84e884e9 6399 "st", "std", -size);
86735b02 6400 }
c819be5b 6401 else
86735b02
DE
6402 {
6403 /* Subtract %sp in two steps, but make sure there is always a
6404 64 byte register save area, and %sp is properly aligned. */
6405 /* Amount to decrement %sp by, the first time. */
6406 unsigned int size1 = ((size - reg_offset + 64) + 15) & -16;
6407 /* Offset to register save area from %sp. */
6408 unsigned int offset = size1 - (size - reg_offset);
6409
6410 if (size1 <= 4096)
6411 {
e0d80184 6412 fprintf (file, "\tadd\t%s, %d, %s\n",
86735b02
DE
6413 sp_str, -size1, sp_str);
6414 if (gmask & FRAME_POINTER_MASK)
6415 {
e0d80184 6416 fprintf (file, "\tst\t%s, [%s+%d]\n\tsub\t%s, %d, %s\t%s# set up frame pointer\n",
bf62bbf1
DE
6417 fp_str, sp_str, offset, sp_str, -size1, fp_str,
6418 ASM_COMMENT_START);
86735b02
DE
6419 offset += 4;
6420 }
6421 }
6422 else
6423 {
e0d80184 6424 fprintf (file, "\tset\t%d, %s\n\tsub\t%s, %s, %s\n",
86735b02
DE
6425 size1, t1_str, sp_str, t1_str, sp_str);
6426 if (gmask & FRAME_POINTER_MASK)
6427 {
e0d80184 6428 fprintf (file, "\tst\t%s, [%s+%d]\n\tadd\t%s, %s, %s\t%s# set up frame pointer\n",
bf62bbf1
DE
6429 fp_str, sp_str, offset, sp_str, t1_str, fp_str,
6430 ASM_COMMENT_START);
86735b02
DE
6431 offset += 4;
6432 }
6433 }
0021b564 6434 if (dwarf2out_do_frame ())
c53aa195 6435 {
96987e96 6436 char *l = dwarf2out_cfi_label ();
c53aa195
JM
6437 if (gmask & FRAME_POINTER_MASK)
6438 {
6439 dwarf2out_reg_save (l, FRAME_POINTER_REGNUM,
6440 offset - 4 - size1);
6441 dwarf2out_def_cfa (l, FRAME_POINTER_REGNUM, 0);
6442 }
6443 else
6444 dwarf2out_def_cfa (l, STACK_POINTER_REGNUM, size1);
6445 }
86735b02
DE
6446 if (gmask & RETURN_ADDR_MASK)
6447 {
e0d80184 6448 fprintf (file, "\tst\t%s, [%s+%d]\n",
86735b02 6449 reg_names[RETURN_ADDR_REGNUM], sp_str, offset);
0021b564 6450 if (dwarf2out_do_frame ())
c53aa195
JM
6451 /* offset - size1 == reg_offset - size
6452 if reg_offset were updated above like offset. */
6453 dwarf2out_return_save ("", offset - size1);
86735b02
DE
6454 offset += 4;
6455 }
6456 sparc_flat_save_restore (file, sp_str, offset,
6457 gmask & ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK),
6458 current_frame_info.fmask,
84e884e9 6459 "st", "std", -size1);
e0d80184 6460 fprintf (file, "\tset\t%d, %s\n\tsub\t%s, %s, %s\n",
86735b02 6461 size - size1, t1_str, sp_str, t1_str, sp_str);
0021b564 6462 if (dwarf2out_do_frame ())
c53aa195
JM
6463 if (! (gmask & FRAME_POINTER_MASK))
6464 dwarf2out_def_cfa ("", STACK_POINTER_REGNUM, size);
86735b02 6465 }
c819be5b 6466 }
c6aa9ce1 6467
bf62bbf1 6468 fprintf (file, "\t%s#PROLOGUE# 1\n", ASM_COMMENT_START);
c819be5b
JW
6469}
6470\f
6471/* Do any necessary cleanup after a function to restore stack, frame,
6472 and regs. */
6473
6474void
c6aa9ce1 6475sparc_flat_output_function_epilogue (file, size)
c819be5b
JW
6476 FILE *file;
6477 int size;
6478{
c819be5b
JW
6479 rtx epilogue_delay = current_function_epilogue_delay_list;
6480 int noepilogue = FALSE;
c819be5b 6481
c6aa9ce1 6482 /* This is only for the human reader. */
bf62bbf1 6483 fprintf (file, "\t%s#EPILOGUE#\n", ASM_COMMENT_START);
c6aa9ce1 6484
c819be5b
JW
6485 /* The epilogue does not depend on any registers, but the stack
6486 registers, so we assume that if we have 1 pending nop, it can be
6487 ignored, and 2 it must be filled (2 nops occur for integer
6488 multiply and divide). */
6489
6490 size = SPARC_STACK_ALIGN (size);
86735b02 6491 size = (!current_frame_info.initialized
c6aa9ce1 6492 ? sparc_flat_compute_frame_size (size)
c819be5b
JW
6493 : current_frame_info.total_size);
6494
86735b02 6495 if (size == 0 && epilogue_delay == 0)
c819be5b
JW
6496 {
6497 rtx insn = get_last_insn ();
6498
6499 /* If the last insn was a BARRIER, we don't have to write any code
6500 because a jump (aka return) was put there. */
6501 if (GET_CODE (insn) == NOTE)
6502 insn = prev_nonnote_insn (insn);
6503 if (insn && GET_CODE (insn) == BARRIER)
6504 noepilogue = TRUE;
6505 }
6506
6507 if (!noepilogue)
6508 {
86735b02
DE
6509 unsigned int reg_offset = current_frame_info.reg_offset;
6510 unsigned int size1;
99f44eba
KG
6511 const char *sp_str = reg_names[STACK_POINTER_REGNUM];
6512 const char *fp_str = reg_names[FRAME_POINTER_REGNUM];
3bb5de61 6513 const char *t1_str = "%g1";
86735b02 6514
c819be5b
JW
6515 /* In the reload sequence, we don't need to fill the load delay
6516 slots for most of the loads, also see if we can fill the final
6517 delay slot if not otherwise filled by the reload sequence. */
6518
86735b02 6519 if (size > 4095)
e0d80184 6520 fprintf (file, "\tset\t%d, %s\n", size, t1_str);
c819be5b
JW
6521
6522 if (frame_pointer_needed)
6523 {
86735b02 6524 if (size > 4095)
e0d80184 6525 fprintf (file,"\tsub\t%s, %s, %s\t\t%s# sp not trusted here\n",
bf62bbf1 6526 fp_str, t1_str, sp_str, ASM_COMMENT_START);
c819be5b 6527 else
e0d80184 6528 fprintf (file,"\tsub\t%s, %d, %s\t\t%s# sp not trusted here\n",
bf62bbf1 6529 fp_str, size, sp_str, ASM_COMMENT_START);
c819be5b
JW
6530 }
6531
ddd5a7c1 6532 /* Is the entire register save area offsettable from %sp? */
fbd039b2 6533 if (reg_offset < 4096 - 64 * (unsigned) UNITS_PER_WORD)
86735b02
DE
6534 {
6535 size1 = 0;
6536 }
6537 else
6538 {
6539 /* Restore %sp in two steps, but make sure there is always a
6540 64 byte register save area, and %sp is properly aligned. */
6541 /* Amount to increment %sp by, the first time. */
6542 size1 = ((reg_offset - 64 - 16) + 15) & -16;
6543 /* Offset to register save area from %sp. */
6544 reg_offset = size1 - reg_offset;
6545
e0d80184 6546 fprintf (file, "\tset\t%d, %s\n\tadd\t%s, %s, %s\n",
86735b02
DE
6547 size1, t1_str, sp_str, t1_str, sp_str);
6548 }
6549
6550 /* We must restore the frame pointer and return address reg first
6551 because they are treated specially by the prologue output code. */
6552 if (current_frame_info.gmask & FRAME_POINTER_MASK)
6553 {
e0d80184 6554 fprintf (file, "\tld\t[%s+%d], %s\n",
86735b02
DE
6555 sp_str, reg_offset, fp_str);
6556 reg_offset += 4;
6557 }
6558 if (current_frame_info.gmask & RETURN_ADDR_MASK)
6559 {
e0d80184 6560 fprintf (file, "\tld\t[%s+%d], %s\n",
86735b02
DE
6561 sp_str, reg_offset, reg_names[RETURN_ADDR_REGNUM]);
6562 reg_offset += 4;
6563 }
6564
6565 /* Restore any remaining saved registers. */
6566 sparc_flat_save_restore (file, sp_str, reg_offset,
6567 current_frame_info.gmask & ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK),
6568 current_frame_info.fmask,
c53aa195 6569 "ld", "ldd", 0);
86735b02
DE
6570
6571 /* If we had to increment %sp in two steps, record it so the second
6572 restoration in the epilogue finishes up. */
6573 if (size1 > 0)
6574 {
6575 size -= size1;
6576 if (size > 4095)
e0d80184 6577 fprintf (file, "\tset\t%d, %s\n",
86735b02
DE
6578 size, t1_str);
6579 }
c819be5b 6580
c819be5b 6581 if (current_function_returns_struct)
e0d80184 6582 fprintf (file, "\tjmp\t%%o7+12\n");
c819be5b
JW
6583 else
6584 fprintf (file, "\tretl\n");
6585
6586 /* If the only register saved is the return address, we need a
6587 nop, unless we have an instruction to put into it. Otherwise
6588 we don't since reloading multiple registers doesn't reference
6589 the register being loaded. */
6590
6591 if (epilogue_delay)
6592 {
86735b02 6593 if (size)
c819be5b
JW
6594 abort ();
6595 final_scan_insn (XEXP (epilogue_delay, 0), file, 1, -2, 1);
6596 }
6597
86735b02 6598 else if (size > 4095)
e0d80184 6599 fprintf (file, "\tadd\t%s, %s, %s\n", sp_str, t1_str, sp_str);
c819be5b 6600
86735b02 6601 else if (size > 0)
e0d80184 6602 fprintf (file, "\tadd\t%s, %d, %s\n", sp_str, size, sp_str);
c819be5b
JW
6603
6604 else
6605 fprintf (file, "\tnop\n");
6606 }
6607
6608 /* Reset state info for each function. */
6609 current_frame_info = zero_frame_info;
e0d80184
DM
6610
6611 sparc_output_deferred_case_vectors ();
c819be5b
JW
6612}
6613\f
6614/* Define the number of delay slots needed for the function epilogue.
6615
6616 On the sparc, we need a slot if either no stack has been allocated,
6617 or the only register saved is the return register. */
6618
6619int
c6aa9ce1 6620sparc_flat_epilogue_delay_slots ()
c819be5b
JW
6621{
6622 if (!current_frame_info.initialized)
c6aa9ce1 6623 (void) sparc_flat_compute_frame_size (get_frame_size ());
c819be5b
JW
6624
6625 if (current_frame_info.total_size == 0)
6626 return 1;
6627
c819be5b
JW
6628 return 0;
6629}
6630
6631/* Return true is TRIAL is a valid insn for the epilogue delay slot.
6632 Any single length instruction which doesn't reference the stack or frame
6633 pointer is OK. */
6634
6635int
c6aa9ce1 6636sparc_flat_eligible_for_epilogue_delay (trial, slot)
c819be5b 6637 rtx trial;
db3d4438 6638 int slot ATTRIBUTE_UNUSED;
c819be5b 6639{
c4ce6853
DE
6640 rtx pat = PATTERN (trial);
6641
6642 if (get_attr_length (trial) != 1)
6643 return 0;
6644
6645 /* If %g0 is live, there are lots of things we can't handle.
6646 Rather than trying to find them all now, let's punt and only
6647 optimize things as necessary. */
6648 if (TARGET_LIVE_G0)
6649 return 0;
6650
6651 if (! reg_mentioned_p (stack_pointer_rtx, pat)
6652 && ! reg_mentioned_p (frame_pointer_rtx, pat))
c819be5b 6653 return 1;
c4ce6853 6654
c819be5b
JW
6655 return 0;
6656}
dbb54862
JW
6657\f
6658/* Adjust the cost of a scheduling dependency. Return the new cost of
6659 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
6660
8947065c 6661static int
dbb54862
JW
6662supersparc_adjust_cost (insn, link, dep_insn, cost)
6663 rtx insn;
6664 rtx link;
6665 rtx dep_insn;
6666 int cost;
6667{
6668 enum attr_type insn_type;
6669
6670 if (! recog_memoized (insn))
6671 return 0;
6672
6673 insn_type = get_attr_type (insn);
6674
6675 if (REG_NOTE_KIND (link) == 0)
6676 {
6677 /* Data dependency; DEP_INSN writes a register that INSN reads some
6678 cycles later. */
6679
6680 /* if a load, then the dependence must be on the memory address;
3bc8b61e
DM
6681 add an extra "cycle". Note that the cost could be two cycles
6682 if the reg was written late in an instruction group; we ca not tell
dbb54862
JW
6683 here. */
6684 if (insn_type == TYPE_LOAD || insn_type == TYPE_FPLOAD)
6685 return cost + 3;
6686
6687 /* Get the delay only if the address of the store is the dependence. */
6688 if (insn_type == TYPE_STORE || insn_type == TYPE_FPSTORE)
6689 {
6690 rtx pat = PATTERN(insn);
6691 rtx dep_pat = PATTERN (dep_insn);
6692
6693 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
3bc8b61e 6694 return cost; /* This should not happen! */
dbb54862
JW
6695
6696 /* The dependency between the two instructions was on the data that
6697 is being stored. Assume that this implies that the address of the
6698 store is not dependent. */
6699 if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
6700 return cost;
6701
6702 return cost + 3; /* An approximation. */
6703 }
6704
6705 /* A shift instruction cannot receive its data from an instruction
6706 in the same cycle; add a one cycle penalty. */
6707 if (insn_type == TYPE_SHIFT)
6708 return cost + 3; /* Split before cascade into shift. */
6709 }
6710 else
6711 {
6712 /* Anti- or output- dependency; DEP_INSN reads/writes a register that
6713 INSN writes some cycles later. */
6714
6715 /* These are only significant for the fpu unit; writing a fp reg before
6716 the fpu has finished with it stalls the processor. */
6717
6718 /* Reusing an integer register causes no problems. */
6719 if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
6720 return 0;
6721 }
6722
6723 return cost;
6724}
bfd6bc60 6725
8947065c
RH
6726static int
6727hypersparc_adjust_cost (insn, link, dep_insn, cost)
6728 rtx insn;
6729 rtx link;
6730 rtx dep_insn;
6731 int cost;
6732{
6733 enum attr_type insn_type, dep_type;
6734 rtx pat = PATTERN(insn);
6735 rtx dep_pat = PATTERN (dep_insn);
6736
6737 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
6738 return cost;
6739
6740 insn_type = get_attr_type (insn);
6741 dep_type = get_attr_type (dep_insn);
6742
6743 switch (REG_NOTE_KIND (link))
6744 {
6745 case 0:
6746 /* Data dependency; DEP_INSN writes a register that INSN reads some
6747 cycles later. */
6748
6749 switch (insn_type)
6750 {
6751 case TYPE_STORE:
6752 case TYPE_FPSTORE:
6753 /* Get the delay iff the address of the store is the dependence. */
6754 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
6755 return cost;
6756
6757 if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
6758 return cost;
6759 return cost + 3;
6760
6761 case TYPE_LOAD:
6762 case TYPE_SLOAD:
6763 case TYPE_FPLOAD:
6764 /* If a load, then the dependence must be on the memory address. If
6765 the addresses aren't equal, then it might be a false dependency */
6766 if (dep_type == TYPE_STORE || dep_type == TYPE_FPSTORE)
6767 {
6768 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET
6769 || GET_CODE (SET_DEST (dep_pat)) != MEM
6770 || GET_CODE (SET_SRC (pat)) != MEM
6771 || ! rtx_equal_p (XEXP (SET_DEST (dep_pat), 0),
6772 XEXP (SET_SRC (pat), 0)))
6773 return cost + 2;
6774
6775 return cost + 8;
6776 }
6777 break;
6778
6779 case TYPE_BRANCH:
6780 /* Compare to branch latency is 0. There is no benefit from
6781 separating compare and branch. */
6782 if (dep_type == TYPE_COMPARE)
6783 return 0;
6784 /* Floating point compare to branch latency is less than
6785 compare to conditional move. */
6786 if (dep_type == TYPE_FPCMP)
6787 return cost - 1;
6788 break;
3bb5de61
KG
6789 default:
6790 break;
8947065c
RH
6791 }
6792 break;
6793
6794 case REG_DEP_ANTI:
6795 /* Anti-dependencies only penalize the fpu unit. */
6796 if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
6797 return 0;
6798 break;
6799
6800 default:
6801 break;
6802 }
6803
6804 return cost;
6805}
6806
6807static int
6808ultrasparc_adjust_cost (insn, link, dep_insn, cost)
6809 rtx insn;
6810 rtx link;
6811 rtx dep_insn;
6812 int cost;
6813{
6814 enum attr_type insn_type, dep_type;
6815 rtx pat = PATTERN(insn);
6816 rtx dep_pat = PATTERN (dep_insn);
6817
6818 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
6819 return cost;
6820
6821 insn_type = get_attr_type (insn);
6822 dep_type = get_attr_type (dep_insn);
6823
6824 /* Nothing issues in parallel with integer multiplies, so
6825 mark as zero cost since the scheduler can not do anything
6826 about it. */
6827 if (insn_type == TYPE_IMUL)
6828 return 0;
6829
6830#define SLOW_FP(dep_type) \
c0ec7a75
DM
6831(dep_type == TYPE_FPSQRTS || dep_type == TYPE_FPSQRTD || \
6832 dep_type == TYPE_FPDIVS || dep_type == TYPE_FPDIVD)
8947065c
RH
6833
6834 switch (REG_NOTE_KIND (link))
6835 {
6836 case 0:
6837 /* Data dependency; DEP_INSN writes a register that INSN reads some
6838 cycles later. */
6839
6840 if (dep_type == TYPE_CMOVE)
6841 {
6842 /* Instructions that read the result of conditional moves cannot
6843 be in the same group or the following group. */
6844 return cost + 1;
6845 }
6846
6847 switch (insn_type)
6848 {
6849 /* UltraSPARC can dual issue a store and an instruction setting
6850 the value stored, except for divide and square root. */
6851 case TYPE_FPSTORE:
6852 if (! SLOW_FP (dep_type))
6853 return 0;
6854 return cost;
6855
6856 case TYPE_STORE:
6857 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
6858 return cost;
6859
6860 if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
6861 /* The dependency between the two instructions is on the data
6862 that is being stored. Assume that the address of the store
6863 is not also dependent. */
6864 return 0;
6865 return cost;
6866
6867 case TYPE_LOAD:
6868 case TYPE_SLOAD:
6869 case TYPE_FPLOAD:
6870 /* A load does not return data until at least 11 cycles after
6871 a store to the same location. 3 cycles are accounted for
6872 in the load latency; add the other 8 here. */
6873 if (dep_type == TYPE_STORE || dep_type == TYPE_FPSTORE)
6874 {
6875 /* If the addresses are not equal this may be a false
6876 dependency because pointer aliasing could not be
6877 determined. Add only 2 cycles in that case. 2 is
6878 an arbitrary compromise between 8, which would cause
6879 the scheduler to generate worse code elsewhere to
6880 compensate for a dependency which might not really
6881 exist, and 0. */
6882 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET
6883 || GET_CODE (SET_SRC (pat)) != MEM
6884 || GET_CODE (SET_DEST (dep_pat)) != MEM
6885 || ! rtx_equal_p (XEXP (SET_SRC (pat), 0),
6886 XEXP (SET_DEST (dep_pat), 0)))
6887 return cost + 2;
6888
6889 return cost + 8;
6890 }
6891 return cost;
6892
6893 case TYPE_BRANCH:
6894 /* Compare to branch latency is 0. There is no benefit from
6895 separating compare and branch. */
6896 if (dep_type == TYPE_COMPARE)
6897 return 0;
6898 /* Floating point compare to branch latency is less than
6899 compare to conditional move. */
6900 if (dep_type == TYPE_FPCMP)
6901 return cost - 1;
6902 return cost;
6903
6904 case TYPE_FPCMOVE:
6905 /* FMOVR class instructions can not issue in the same cycle
6906 or the cycle after an instruction which writes any
6907 integer register. Model this as cost 2 for dependent
6908 instructions. */
6909 if ((dep_type == TYPE_IALU || dep_type == TYPE_UNARY
6910 || dep_type == TYPE_BINARY)
6911 && cost < 2)
6912 return 2;
6913 /* Otherwise check as for integer conditional moves. */
6914
6915 case TYPE_CMOVE:
6916 /* Conditional moves involving integer registers wait until
6917 3 cycles after loads return data. The interlock applies
6918 to all loads, not just dependent loads, but that is hard
6919 to model. */
6920 if (dep_type == TYPE_LOAD || dep_type == TYPE_SLOAD)
6921 return cost + 3;
6922 return cost;
6923
6924 default:
6925 break;
6926 }
6927 break;
6928
6929 case REG_DEP_ANTI:
6930 /* Divide and square root lock destination registers for full latency. */
6931 if (! SLOW_FP (dep_type))
6932 return 0;
6933 break;
6934
6935 case REG_DEP_OUTPUT:
6936 /* IEU and FPU instruction that have the same destination
6937 register cannot be grouped together. */
6938 return cost + 1;
6939
6940 default:
6941 break;
6942 }
6943
6944 /* Other costs not accounted for:
6945 - Single precision floating point loads lock the other half of
6946 the even/odd register pair.
6947 - Several hazards associated with ldd/std are ignored because these
6948 instructions are rarely generated for V9.
6949 - The floating point pipeline can not have both a single and double
6950 precision operation active at the same time. Format conversions
6951 and graphics instructions are given honorary double precision status.
6952 - call and jmpl are always the first instruction in a group. */
6953
6954 return cost;
6955
6956#undef SLOW_FP
6957}
6958
6959int
6960sparc_adjust_cost(insn, link, dep, cost)
6961 rtx insn;
6962 rtx link;
6963 rtx dep;
6964 int cost;
6965{
6966 switch (sparc_cpu)
6967 {
6968 case PROCESSOR_SUPERSPARC:
6969 cost = supersparc_adjust_cost (insn, link, dep, cost);
6970 break;
6971 case PROCESSOR_HYPERSPARC:
6972 case PROCESSOR_SPARCLITE86X:
6973 cost = hypersparc_adjust_cost (insn, link, dep, cost);
6974 break;
6975 case PROCESSOR_ULTRASPARC:
6976 cost = ultrasparc_adjust_cost (insn, link, dep, cost);
6977 break;
6978 default:
6979 break;
6980 }
6981 return cost;
6982}
6983
3bc8b61e
DM
6984/* This describes the state of the UltraSPARC pipeline during
6985 instruction scheduling. */
6986
b96a5ea5
KG
6987#define TMASK(__x) ((unsigned)1 << ((int)(__x)))
6988#define UMASK(__x) ((unsigned)1 << ((int)(__x)))
3bc8b61e
DM
6989
6990enum ultra_code { NONE=0, /* no insn at all */
6991 IEU0, /* shifts and conditional moves */
6992 IEU1, /* condition code setting insns, calls+jumps */
6993 IEUN, /* all other single cycle ieu insns */
6994 LSU, /* loads and stores */
6995 CTI, /* branches */
6996 FPM, /* FPU pipeline 1, multiplies and divides */
6997 FPA, /* FPU pipeline 2, all other operations */
6998 SINGLE, /* single issue instructions */
6999 NUM_ULTRA_CODES };
7000
f6da8bc3
KG
7001static enum ultra_code ultra_code_from_mask PARAMS ((int));
7002static void ultra_schedule_insn PARAMS ((rtx *, rtx *, int, enum ultra_code));
b1474bb7 7003
3bb5de61 7004static const char *ultra_code_names[NUM_ULTRA_CODES] = {
3bc8b61e
DM
7005 "NONE", "IEU0", "IEU1", "IEUN", "LSU", "CTI",
7006 "FPM", "FPA", "SINGLE" };
7007
7008struct ultrasparc_pipeline_state {
7009 /* The insns in this group. */
7010 rtx group[4];
7011
7012 /* The code for each insn. */
7013 enum ultra_code codes[4];
7014
7015 /* Which insns in this group have been committed by the
7016 scheduler. This is how we determine how many more
7017 can issue this cycle. */
7018 char commit[4];
7019
7020 /* How many insns in this group. */
7021 char group_size;
7022
7023 /* Mask of free slots still in this group. */
7024 char free_slot_mask;
7025
7026 /* The slotter uses the following to determine what other
7027 insn types can still make their way into this group. */
7028 char contents [NUM_ULTRA_CODES];
7029 char num_ieu_insns;
7030};
7031
7032#define ULTRA_NUM_HIST 8
7033static struct ultrasparc_pipeline_state ultra_pipe_hist[ULTRA_NUM_HIST];
7034static int ultra_cur_hist;
7035static int ultra_cycles_elapsed;
7036
7037#define ultra_pipe (ultra_pipe_hist[ultra_cur_hist])
7038
7039/* Given TYPE_MASK compute the ultra_code it has. */
7040static enum ultra_code
7041ultra_code_from_mask (type_mask)
7042 int type_mask;
7043{
3bc8b61e
DM
7044 if (type_mask & (TMASK (TYPE_SHIFT) | TMASK (TYPE_CMOVE)))
7045 return IEU0;
7046 else if (type_mask & (TMASK (TYPE_COMPARE) |
7047 TMASK (TYPE_CALL) |
7048 TMASK (TYPE_UNCOND_BRANCH)))
7049 return IEU1;
7050 else if (type_mask & (TMASK (TYPE_IALU) | TMASK (TYPE_BINARY) |
7051 TMASK (TYPE_MOVE) | TMASK (TYPE_UNARY)))
7052 return IEUN;
7053 else if (type_mask & (TMASK (TYPE_LOAD) | TMASK (TYPE_SLOAD) |
7054 TMASK (TYPE_STORE) | TMASK (TYPE_FPLOAD) |
7055 TMASK (TYPE_FPSTORE)))
7056 return LSU;
7057 else if (type_mask & (TMASK (TYPE_FPMUL) | TMASK (TYPE_FPDIVS) |
c0ec7a75
DM
7058 TMASK (TYPE_FPDIVD) | TMASK (TYPE_FPSQRTS) |
7059 TMASK (TYPE_FPSQRTD)))
3bc8b61e
DM
7060 return FPM;
7061 else if (type_mask & (TMASK (TYPE_FPMOVE) | TMASK (TYPE_FPCMOVE) |
7062 TMASK (TYPE_FP) | TMASK (TYPE_FPCMP)))
7063 return FPA;
7064 else if (type_mask & TMASK (TYPE_BRANCH))
7065 return CTI;
7066
7067 return SINGLE;
7068}
7069
7070/* Check INSN (a conditional move) and make sure that it's
7071 results are available at this cycle. Return 1 if the
7072 results are in fact ready. */
7073static int
7074ultra_cmove_results_ready_p (insn)
7075 rtx insn;
7076{
7077 struct ultrasparc_pipeline_state *up;
7078 int entry, slot;
7079
7080 /* If this got dispatched in the previous
7081 group, the results are not ready. */
1f0e6e28 7082 entry = (ultra_cur_hist - 1) & (ULTRA_NUM_HIST - 1);
3bc8b61e
DM
7083 up = &ultra_pipe_hist[entry];
7084 slot = 4;
7085 while (--slot >= 0)
7086 if (up->group[slot] == insn)
c305b1a7 7087 return 0;
3bc8b61e 7088
c305b1a7 7089 return 1;
3bc8b61e
DM
7090}
7091
7092/* Walk backwards in pipeline history looking for FPU
7093 operations which use a mode different than FPMODE and
7094 will create a stall if an insn using FPMODE were to be
7095 dispatched this cycle. */
7096static int
7097ultra_fpmode_conflict_exists (fpmode)
7098 enum machine_mode fpmode;
7099{
7100 int hist_ent;
7101 int hist_lim;
7102
1f0e6e28 7103 hist_ent = (ultra_cur_hist - 1) & (ULTRA_NUM_HIST - 1);
3bc8b61e
DM
7104 if (ultra_cycles_elapsed < 4)
7105 hist_lim = ultra_cycles_elapsed;
7106 else
7107 hist_lim = 4;
7108 while (hist_lim > 0)
7109 {
7110 struct ultrasparc_pipeline_state *up = &ultra_pipe_hist[hist_ent];
7111 int slot = 4;
7112
7113 while (--slot >= 0)
7114 {
7115 rtx insn = up->group[slot];
7116 enum machine_mode this_mode;
3bc8b61e
DM
7117 rtx pat;
7118
7119 if (! insn
7120 || GET_CODE (insn) != INSN
7121 || (pat = PATTERN (insn)) == 0
7122 || GET_CODE (pat) != SET)
7123 continue;
7124
7125 this_mode = GET_MODE (SET_DEST (pat));
7126 if ((this_mode != SFmode
7127 && this_mode != DFmode)
7128 || this_mode == fpmode)
7129 continue;
7130
7131 /* If it is not FMOV, FABS, FNEG, FDIV, or FSQRT then
ddf80874
DM
7132 we will get a stall. Loads and stores are independant
7133 of these rules. */
3bc8b61e
DM
7134 if (GET_CODE (SET_SRC (pat)) != ABS
7135 && GET_CODE (SET_SRC (pat)) != NEG
7136 && ((TMASK (get_attr_type (insn)) &
7137 (TMASK (TYPE_FPDIVS) | TMASK (TYPE_FPDIVD) |
c0ec7a75
DM
7138 TMASK (TYPE_FPMOVE) | TMASK (TYPE_FPSQRTS) |
7139 TMASK (TYPE_FPSQRTD) |
ddf80874 7140 TMASK (TYPE_LOAD) | TMASK (TYPE_STORE))) == 0))
3bc8b61e
DM
7141 return 1;
7142 }
7143 hist_lim--;
1f0e6e28 7144 hist_ent = (hist_ent - 1) & (ULTRA_NUM_HIST - 1);
3bc8b61e
DM
7145 }
7146
7147 /* No conflicts, safe to dispatch. */
7148 return 0;
7149}
7150
7151/* Find an instruction in LIST which has one of the
7152 type attributes enumerated in TYPE_MASK. START
7153 says where to begin the search.
7154
7155 NOTE: This scheme depends upon the fact that we
7156 have less than 32 distinct type attributes. */
5a5325cb
DM
7157
7158static int ultra_types_avail;
7159
3bc8b61e
DM
7160static rtx *
7161ultra_find_type (type_mask, list, start)
7162 int type_mask;
7163 rtx *list;
7164 int start;
7165{
7166 int i;
7167
5a5325cb
DM
7168 /* Short circuit if no such insn exists in the ready
7169 at the moment. */
7170 if ((type_mask & ultra_types_avail) == 0)
7171 return 0;
7172
3bc8b61e
DM
7173 for (i = start; i >= 0; i--)
7174 {
7175 rtx insn = list[i];
7176
7177 if (recog_memoized (insn) >= 0
7178 && (TMASK(get_attr_type (insn)) & type_mask))
7179 {
ddf80874 7180 enum machine_mode fpmode = SFmode;
3bc8b61e
DM
7181 rtx pat = 0;
7182 int slot;
7183 int check_depend = 0;
7184 int check_fpmode_conflict = 0;
7185
7186 if (GET_CODE (insn) == INSN
7187 && (pat = PATTERN(insn)) != 0
7188 && GET_CODE (pat) == SET
7189 && !(type_mask & (TMASK (TYPE_STORE) |
7190 TMASK (TYPE_FPSTORE))))
7191 {
7192 check_depend = 1;
7193 if (GET_MODE (SET_DEST (pat)) == SFmode
7194 || GET_MODE (SET_DEST (pat)) == DFmode)
7195 {
7196 fpmode = GET_MODE (SET_DEST (pat));
7197 check_fpmode_conflict = 1;
7198 }
7199 }
7200
7201 slot = 4;
7202 while(--slot >= 0)
7203 {
7204 rtx slot_insn = ultra_pipe.group[slot];
7205 rtx slot_pat;
7206
7207 /* Already issued, bad dependency, or FPU
7208 mode conflict. */
7209 if (slot_insn != 0
7210 && (slot_pat = PATTERN (slot_insn)) != 0
7211 && ((insn == slot_insn)
7212 || (check_depend == 1
7213 && GET_CODE (slot_insn) == INSN
7214 && GET_CODE (slot_pat) == SET
5a5325cb
DM
7215 && ((GET_CODE (SET_DEST (slot_pat)) == REG
7216 && GET_CODE (SET_SRC (pat)) == REG
7217 && REGNO (SET_DEST (slot_pat)) ==
7218 REGNO (SET_SRC (pat)))
7219 || (GET_CODE (SET_DEST (slot_pat)) == SUBREG
7220 && GET_CODE (SET_SRC (pat)) == SUBREG
7221 && REGNO (SUBREG_REG (SET_DEST (slot_pat))) ==
7222 REGNO (SUBREG_REG (SET_SRC (pat)))
7223 && SUBREG_WORD (SET_DEST (slot_pat)) ==
ddf80874 7224 SUBREG_WORD (SET_SRC (pat)))))
3bc8b61e
DM
7225 || (check_fpmode_conflict == 1
7226 && GET_CODE (slot_insn) == INSN
7227 && GET_CODE (slot_pat) == SET
ddf80874
DM
7228 && (GET_MODE (SET_DEST (slot_pat)) == SFmode
7229 || GET_MODE (SET_DEST (slot_pat)) == DFmode)
7230 && GET_MODE (SET_DEST (slot_pat)) != fpmode)))
3bc8b61e
DM
7231 goto next;
7232 }
7233
7234 /* Check for peculiar result availability and dispatch
7235 interference situations. */
7236 if (pat != 0
7237 && ultra_cycles_elapsed > 0)
7238 {
7239 rtx link;
7240
7241 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
7242 {
7243 rtx link_insn = XEXP (link, 0);
7244 if (GET_CODE (link_insn) == INSN
7245 && recog_memoized (link_insn) >= 0
7246 && (TMASK (get_attr_type (link_insn)) &
7247 (TMASK (TYPE_CMOVE) | TMASK (TYPE_FPCMOVE)))
7248 && ! ultra_cmove_results_ready_p (link_insn))
7249 goto next;
7250 }
7251
7252 if (check_fpmode_conflict
7253 && ultra_fpmode_conflict_exists (fpmode))
7254 goto next;
7255 }
7256
7257 return &list[i];
7258 }
7259 next:
5a5325cb 7260 ;
3bc8b61e
DM
7261 }
7262 return 0;
7263}
7264
5a5325cb
DM
7265static void
7266ultra_build_types_avail (ready, n_ready)
7267 rtx *ready;
7268 int n_ready;
7269{
7270 int i = n_ready - 1;
7271
7272 ultra_types_avail = 0;
7273 while(i >= 0)
7274 {
7275 rtx insn = ready[i];
7276
7277 if (recog_memoized (insn) >= 0)
7278 ultra_types_avail |= TMASK (get_attr_type (insn));
7279
7280 i -= 1;
7281 }
7282}
7283
3bc8b61e
DM
7284/* Place insn pointed to my IP into the pipeline.
7285 Make element THIS of READY be that insn if it
7286 is not already. TYPE indicates the pipeline class
7287 this insn falls into. */
7288static void
7289ultra_schedule_insn (ip, ready, this, type)
7290 rtx *ip;
7291 rtx *ready;
7292 int this;
7293 enum ultra_code type;
7294{
7295 int pipe_slot;
7296 char mask = ultra_pipe.free_slot_mask;
c0ec7a75 7297 rtx temp;
3bc8b61e
DM
7298
7299 /* Obtain free slot. */
7300 for (pipe_slot = 0; pipe_slot < 4; pipe_slot++)
7301 if ((mask & (1 << pipe_slot)) != 0)
7302 break;
7303 if (pipe_slot == 4)
7304 abort ();
7305
7306 /* In it goes, and it hasn't been committed yet. */
7307 ultra_pipe.group[pipe_slot] = *ip;
7308 ultra_pipe.codes[pipe_slot] = type;
7309 ultra_pipe.contents[type] = 1;
7310 if (UMASK (type) &
7311 (UMASK (IEUN) | UMASK (IEU0) | UMASK (IEU1)))
7312 ultra_pipe.num_ieu_insns += 1;
7313
7314 ultra_pipe.free_slot_mask = (mask & ~(1 << pipe_slot));
7315 ultra_pipe.group_size += 1;
7316 ultra_pipe.commit[pipe_slot] = 0;
7317
7318 /* Update ready list. */
c0ec7a75
DM
7319 temp = *ip;
7320 while (ip != &ready[this])
3bc8b61e 7321 {
c0ec7a75
DM
7322 ip[0] = ip[1];
7323 ++ip;
3bc8b61e 7324 }
c0ec7a75 7325 *ip = temp;
3bc8b61e
DM
7326}
7327
7328/* Advance to the next pipeline group. */
7329static void
7330ultra_flush_pipeline ()
7331{
1f0e6e28 7332 ultra_cur_hist = (ultra_cur_hist + 1) & (ULTRA_NUM_HIST - 1);
3bc8b61e
DM
7333 ultra_cycles_elapsed += 1;
7334 bzero ((char *) &ultra_pipe, sizeof ultra_pipe);
7335 ultra_pipe.free_slot_mask = 0xf;
7336}
7337
3bc8b61e
DM
7338/* Init our data structures for this current block. */
7339void
7340ultrasparc_sched_init (dump, sched_verbose)
ddf80874
DM
7341 FILE *dump ATTRIBUTE_UNUSED;
7342 int sched_verbose ATTRIBUTE_UNUSED;
3bc8b61e 7343{
b96a5ea5 7344 bzero ((char *) ultra_pipe_hist, sizeof ultra_pipe_hist);
3bc8b61e
DM
7345 ultra_cur_hist = 0;
7346 ultra_cycles_elapsed = 0;
ddf80874 7347 ultra_pipe.free_slot_mask = 0xf;
3bc8b61e
DM
7348}
7349
7350/* INSN has been scheduled, update pipeline commit state
7351 and return how many instructions are still to be
7352 scheduled in this group. */
bfd6bc60 7353int
3bc8b61e
DM
7354ultrasparc_variable_issue (insn)
7355 rtx insn;
7356{
7357 struct ultrasparc_pipeline_state *up = &ultra_pipe;
7358 int i, left_to_fire;
7359
7360 left_to_fire = 0;
7361 for (i = 0; i < 4; i++)
7362 {
7363 if (up->group[i] == 0)
7364 continue;
7365
7366 if (up->group[i] == insn)
7367 {
7368 up->commit[i] = 1;
7369 }
7370 else if (! up->commit[i])
7371 left_to_fire++;
7372 }
7373
7374 return left_to_fire;
7375}
7376
7377/* In actual_hazard_this_instance, we may have yanked some
7378 instructions from the ready list due to conflict cost
7379 adjustments. If so, and such an insn was in our pipeline
7380 group, remove it and update state. */
7381static void
7382ultra_rescan_pipeline_state (ready, n_ready)
7383 rtx *ready;
7384 int n_ready;
7385{
7386 struct ultrasparc_pipeline_state *up = &ultra_pipe;
7387 int i;
7388
7389 for (i = 0; i < 4; i++)
7390 {
7391 rtx insn = up->group[i];
3bc8b61e
DM
7392 int j;
7393
7394 if (! insn)
7395 continue;
7396
7397 /* If it has been committed, then it was removed from
7398 the ready list because it was actually scheduled,
7399 and that is not the case we are searching for here. */
7400 if (up->commit[i] != 0)
7401 continue;
7402
7403 for (j = n_ready - 1; j >= 0; j--)
7404 if (ready[j] == insn)
7405 break;
7406
7407 /* If we didn't find it, toss it. */
7408 if (j < 0)
7409 {
7410 enum ultra_code ucode = up->codes[i];
7411
7412 up->group[i] = 0;
7413 up->codes[i] = NONE;
7414 up->contents[ucode] = 0;
7415 if (UMASK (ucode) &
7416 (UMASK (IEUN) | UMASK (IEU0) | UMASK (IEU1)))
7417 up->num_ieu_insns -= 1;
7418
7419 up->free_slot_mask |= (1 << i);
7420 up->group_size -= 1;
7421 up->commit[i] = 0;
7422 }
7423 }
7424}
7425
7426void
7427ultrasparc_sched_reorder (dump, sched_verbose, ready, n_ready)
7428 FILE *dump;
7429 int sched_verbose;
7430 rtx *ready;
7431 int n_ready;
7432{
7433 struct ultrasparc_pipeline_state *up = &ultra_pipe;
7434 int i, this_insn;
7435
3bc8b61e
DM
7436 if (sched_verbose)
7437 {
7438 int n;
7439
7440 fprintf (dump, "\n;;\tUltraSPARC Looking at [");
7441 for (n = n_ready - 1; n >= 0; n--)
7442 {
7443 rtx insn = ready[n];
7444 enum ultra_code ucode;
7445
7446 if (recog_memoized (insn) < 0)
7447 continue;
7448 ucode = ultra_code_from_mask (TMASK (get_attr_type (insn)));
7449 if (n != 0)
7450 fprintf (dump, "%s(%d) ",
7451 ultra_code_names[ucode],
7452 INSN_UID (insn));
7453 else
7454 fprintf (dump, "%s(%d)",
7455 ultra_code_names[ucode],
7456 INSN_UID (insn));
7457 }
7458 fprintf (dump, "]\n");
7459 }
7460
7461 this_insn = n_ready - 1;
7462
7463 /* Skip over junk we don't understand. */
7464 while ((this_insn >= 0)
7465 && recog_memoized (ready[this_insn]) < 0)
7466 this_insn--;
7467
5a5325cb
DM
7468 ultra_build_types_avail (ready, this_insn + 1);
7469
3bc8b61e
DM
7470 while (this_insn >= 0) {
7471 int old_group_size = up->group_size;
7472
7473 if (up->group_size != 0)
7474 {
7475 int num_committed;
7476
7477 num_committed = (up->commit[0] + up->commit[1] +
7478 up->commit[2] + up->commit[3]);
7479 /* If nothing has been commited from our group, or all of
7480 them have. Clear out the (current cycle's) pipeline
7481 state and start afresh. */
7482 if (num_committed == 0
7483 || num_committed == up->group_size)
7484 {
ddf80874
DM
7485 ultra_flush_pipeline ();
7486 up = &ultra_pipe;
3bc8b61e
DM
7487 old_group_size = 0;
7488 }
7489 else
7490 {
7491 /* OK, some ready list insns got requeued and thus removed
7492 from the ready list. Account for this fact. */
7493 ultra_rescan_pipeline_state (ready, n_ready);
7494
7495 /* Something "changed", make this look like a newly
7496 formed group so the code at the end of the loop
7497 knows that progress was in fact made. */
7498 if (up->group_size != old_group_size)
ddf80874 7499 old_group_size = 0;
3bc8b61e
DM
7500 }
7501 }
7502
7503 if (up->group_size == 0)
7504 {
7505 /* If the pipeline is (still) empty and we have any single
7506 group insns, get them out now as this is a good time. */
7507 rtx *ip = ultra_find_type ((TMASK (TYPE_RETURN) | TMASK (TYPE_ADDRESS) |
7508 TMASK (TYPE_IMUL) | TMASK (TYPE_CMOVE) |
7509 TMASK (TYPE_MULTI) | TMASK (TYPE_MISC)),
7510 ready, this_insn);
7511 if (ip)
7512 {
7513 ultra_schedule_insn (ip, ready, this_insn, SINGLE);
7514 break;
7515 }
7516
7517 /* If we are not in the process of emptying out the pipe, try to
7518 obtain an instruction which must be the first in it's group. */
7519 ip = ultra_find_type ((TMASK (TYPE_CALL) |
7520 TMASK (TYPE_CALL_NO_DELAY_SLOT) |
7521 TMASK (TYPE_UNCOND_BRANCH)),
7522 ready, this_insn);
7523 if (ip)
7524 {
7525 ultra_schedule_insn (ip, ready, this_insn, IEU1);
7526 this_insn--;
7527 }
7528 else if ((ip = ultra_find_type ((TMASK (TYPE_FPDIVS) |
7529 TMASK (TYPE_FPDIVD) |
c0ec7a75
DM
7530 TMASK (TYPE_FPSQRTS) |
7531 TMASK (TYPE_FPSQRTD)),
3bc8b61e
DM
7532 ready, this_insn)) != 0)
7533 {
7534 ultra_schedule_insn (ip, ready, this_insn, FPM);
7535 this_insn--;
7536 }
7537 }
7538
7539 /* Try to fill the integer pipeline. First, look for an IEU0 specific
7540 operation. We can't do more IEU operations if the first 3 slots are
7541 all full or we have dispatched two IEU insns already. */
7542 if ((up->free_slot_mask & 0x7) != 0
7543 && up->num_ieu_insns < 2
7544 && up->contents[IEU0] == 0
7545 && up->contents[IEUN] == 0)
7546 {
7547 rtx *ip = ultra_find_type (TMASK(TYPE_SHIFT), ready, this_insn);
7548 if (ip)
7549 {
7550 ultra_schedule_insn (ip, ready, this_insn, IEU0);
7551 this_insn--;
7552 }
7553 }
7554
7555 /* If we can, try to find an IEU1 specific or an unnamed
7556 IEU instruction. */
7557 if ((up->free_slot_mask & 0x7) != 0
7558 && up->num_ieu_insns < 2)
7559 {
7560 rtx *ip = ultra_find_type ((TMASK (TYPE_IALU) | TMASK (TYPE_BINARY) |
7561 TMASK (TYPE_MOVE) | TMASK (TYPE_UNARY) |
7562 (up->contents[IEU1] == 0 ? TMASK (TYPE_COMPARE) : 0)),
7563 ready, this_insn);
7564 if (ip)
7565 {
7566 rtx insn = *ip;
7567
7568 ultra_schedule_insn (ip, ready, this_insn,
7569 (!up->contents[IEU1]
7570 && get_attr_type (insn) == TYPE_COMPARE)
7571 ? IEU1 : IEUN);
7572 this_insn--;
7573 }
7574 }
7575
7576 /* If only one IEU insn has been found, try to find another unnamed
7577 IEU operation or an IEU1 specific one. */
7578 if ((up->free_slot_mask & 0x7) != 0
7579 && up->num_ieu_insns < 2)
7580 {
7581 rtx *ip;
7582 int tmask = (TMASK (TYPE_IALU) | TMASK (TYPE_BINARY) |
7583 TMASK (TYPE_MOVE) | TMASK (TYPE_UNARY));
7584
7585 if (!up->contents[IEU1])
7586 tmask |= TMASK (TYPE_COMPARE);
7587 ip = ultra_find_type (tmask, ready, this_insn);
7588 if (ip)
7589 {
7590 rtx insn = *ip;
7591
7592 ultra_schedule_insn (ip, ready, this_insn,
7593 (!up->contents[IEU1]
7594 && get_attr_type (insn) == TYPE_COMPARE)
7595 ? IEU1 : IEUN);
7596 this_insn--;
7597 }
7598 }
7599
7600 /* Try for a load or store, but such an insn can only be issued
7601 if it is within' one of the first 3 slots. */
7602 if ((up->free_slot_mask & 0x7) != 0
7603 && up->contents[LSU] == 0)
7604 {
7605 rtx *ip = ultra_find_type ((TMASK (TYPE_LOAD) | TMASK (TYPE_SLOAD) |
7606 TMASK (TYPE_STORE) | TMASK (TYPE_FPLOAD) |
7607 TMASK (TYPE_FPSTORE)), ready, this_insn);
7608 if (ip)
7609 {
7610 ultra_schedule_insn (ip, ready, this_insn, LSU);
7611 this_insn--;
7612 }
7613 }
7614
7615 /* Now find FPU operations, first FPM class. But not divisions or
7616 square-roots because those will break the group up. Unlike all
7617 the previous types, these can go in any slot. */
7618 if (up->free_slot_mask != 0
7619 && up->contents[FPM] == 0)
7620 {
7621 rtx *ip = ultra_find_type (TMASK (TYPE_FPMUL), ready, this_insn);
7622 if (ip)
7623 {
7624 ultra_schedule_insn (ip, ready, this_insn, FPM);
7625 this_insn--;
7626 }
7627 }
7628
7629 /* Continue on with FPA class if we have not filled the group already. */
7630 if (up->free_slot_mask != 0
7631 && up->contents[FPA] == 0)
7632 {
7633 rtx *ip = ultra_find_type ((TMASK (TYPE_FPMOVE) | TMASK (TYPE_FPCMOVE) |
7634 TMASK (TYPE_FP) | TMASK (TYPE_FPCMP)),
7635 ready, this_insn);
7636 if (ip)
7637 {
7638 ultra_schedule_insn (ip, ready, this_insn, FPA);
7639 this_insn--;
7640 }
7641 }
7642
7643 /* Finally, maybe stick a branch in here. */
7644 if (up->free_slot_mask != 0
7645 && up->contents[CTI] == 0)
7646 {
7647 rtx *ip = ultra_find_type (TMASK (TYPE_BRANCH), ready, this_insn);
7648
7649 /* Try to slip in a branch only if it is one of the
7650 next 2 in the ready list. */
7651 if (ip && ((&ready[this_insn] - ip) < 2))
7652 {
7653 ultra_schedule_insn (ip, ready, this_insn, CTI);
7654 this_insn--;
7655 }
7656 }
7657
7658 up->group_size = 0;
7659 for (i = 0; i < 4; i++)
7660 if ((up->free_slot_mask & (1 << i)) == 0)
7661 up->group_size++;
7662
7663 /* See if we made any progress... */
7664 if (old_group_size != up->group_size)
7665 break;
7666
7667 /* Clean out the (current cycle's) pipeline state
ddf80874
DM
7668 and try once more. If we placed no instructions
7669 into the pipeline at all, it means a real hard
7670 conflict exists with some earlier issued instruction
7671 so we must advance to the next cycle to clear it up. */
7672 if (up->group_size == 0)
7673 {
7674 ultra_flush_pipeline ();
7675 up = &ultra_pipe;
7676 }
7677 else
7678 {
7679 bzero ((char *) &ultra_pipe, sizeof ultra_pipe);
7680 ultra_pipe.free_slot_mask = 0xf;
7681 }
3bc8b61e
DM
7682 }
7683
7684 if (sched_verbose)
7685 {
7686 int n, gsize;
7687
7688 fprintf (dump, ";;\tUltraSPARC Launched [");
7689 gsize = up->group_size;
7690 for (n = 0; n < 4; n++)
7691 {
7692 rtx insn = up->group[n];
7693
7694 if (! insn)
7695 continue;
7696
7697 gsize -= 1;
7698 if (gsize != 0)
7699 fprintf (dump, "%s(%d) ",
7700 ultra_code_names[up->codes[n]],
7701 INSN_UID (insn));
7702 else
7703 fprintf (dump, "%s(%d)",
7704 ultra_code_names[up->codes[n]],
7705 INSN_UID (insn));
7706 }
7707 fprintf (dump, "]\n");
7708 }
7709}
7710
bfd6bc60
JC
7711int
7712sparc_issue_rate ()
7713{
7714 switch (sparc_cpu)
7715 {
7716 default:
7717 return 1;
bfd6bc60 7718 case PROCESSOR_V9:
284d86e9 7719 /* Assume V9 processors are capable of at least dual-issue. */
bfd6bc60
JC
7720 return 2;
7721 case PROCESSOR_SUPERSPARC:
7722 return 3;
393049a9
RH
7723 case PROCESSOR_HYPERSPARC:
7724 case PROCESSOR_SPARCLITE86X:
7725 return 2;
bfd6bc60
JC
7726 case PROCESSOR_ULTRASPARC:
7727 return 4;
7728 }
7729}
284d86e9
JC
7730
7731static int
7732set_extends(x, insn)
7733 rtx x, insn;
7734{
7735 register rtx pat = PATTERN (insn);
7736
7737 switch (GET_CODE (SET_SRC (pat)))
7738 {
7739 /* Load and some shift instructions zero extend. */
7740 case MEM:
7741 case ZERO_EXTEND:
7742 /* sethi clears the high bits */
7743 case HIGH:
7744 /* LO_SUM is used with sethi. sethi cleared the high
7745 bits and the values used with lo_sum are positive */
7746 case LO_SUM:
284d86e9
JC
7747 /* Store flag stores 0 or 1 */
7748 case LT: case LTU:
7749 case GT: case GTU:
7750 case LE: case LEU:
7751 case GE: case GEU:
7752 case EQ:
7753 case NE:
7754 return 1;
7755 case AND:
7756 {
7757 rtx op1 = XEXP (SET_SRC (pat), 1);
7758 if (GET_CODE (op1) == CONST_INT)
7759 return INTVAL (op1) >= 0;
7760 if (GET_CODE (XEXP (SET_SRC (pat), 0)) == REG
7761 && sparc_check_64 (XEXP (SET_SRC (pat), 0), insn) == 1)
7762 return 1;
7763 if (GET_CODE (op1) == REG
7764 && sparc_check_64 ((op1), insn) == 1)
7765 return 1;
7766 }
7767 case ASHIFT:
7768 case LSHIFTRT:
7769 return GET_MODE (SET_SRC (pat)) == SImode;
7770 /* Positive integers leave the high bits zero. */
7771 case CONST_DOUBLE:
7772 return ! (CONST_DOUBLE_LOW (x) & 0x80000000);
7773 case CONST_INT:
7774 return ! (INTVAL (x) & 0x80000000);
7775 case ASHIFTRT:
7776 case SIGN_EXTEND:
7777 return - (GET_MODE (SET_SRC (pat)) == SImode);
7778 default:
7779 return 0;
7780 }
7781}
7782
e0d80184
DM
7783/* We _ought_ to have only one kind per function, but... */
7784static rtx sparc_addr_diff_list;
7785static rtx sparc_addr_list;
7786
7787void
7788sparc_defer_case_vector (lab, vec, diff)
7789 rtx lab, vec;
7790 int diff;
7791{
7792 vec = gen_rtx_EXPR_LIST (VOIDmode, lab, vec);
7793 if (diff)
7794 sparc_addr_diff_list
7795 = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_diff_list);
7796 else
7797 sparc_addr_list = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_list);
7798}
7799
7800static void
7801sparc_output_addr_vec (vec)
7802 rtx vec;
7803{
7804 rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
7805 int idx, vlen = XVECLEN (body, 0);
7806
d1accaa3
JJ
7807#ifdef ASM_OUTPUT_ADDR_VEC_START
7808 ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
7809#endif
7810
e0d80184
DM
7811#ifdef ASM_OUTPUT_CASE_LABEL
7812 ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
7813 NEXT_INSN (lab));
7814#else
7815 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
7816#endif
7817
7818 for (idx = 0; idx < vlen; idx++)
7819 {
7820 ASM_OUTPUT_ADDR_VEC_ELT
7821 (asm_out_file, CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 0, idx), 0)));
7822 }
d1accaa3
JJ
7823
7824#ifdef ASM_OUTPUT_ADDR_VEC_END
7825 ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
7826#endif
e0d80184
DM
7827}
7828
7829static void
7830sparc_output_addr_diff_vec (vec)
7831 rtx vec;
7832{
7833 rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
7834 rtx base = XEXP (XEXP (body, 0), 0);
7835 int idx, vlen = XVECLEN (body, 1);
7836
d1accaa3
JJ
7837#ifdef ASM_OUTPUT_ADDR_VEC_START
7838 ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
7839#endif
7840
e0d80184
DM
7841#ifdef ASM_OUTPUT_CASE_LABEL
7842 ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
7843 NEXT_INSN (lab));
7844#else
7845 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
7846#endif
7847
7848 for (idx = 0; idx < vlen; idx++)
7849 {
7850 ASM_OUTPUT_ADDR_DIFF_ELT
7851 (asm_out_file,
7852 body,
7853 CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 1, idx), 0)),
7854 CODE_LABEL_NUMBER (base));
7855 }
d1accaa3
JJ
7856
7857#ifdef ASM_OUTPUT_ADDR_VEC_END
7858 ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
7859#endif
e0d80184
DM
7860}
7861
7862static void
7863sparc_output_deferred_case_vectors ()
7864{
7865 rtx t;
d676da68 7866 int align;
e0d80184 7867
f3b8847b
DM
7868 if (sparc_addr_list == NULL_RTX
7869 && sparc_addr_diff_list == NULL_RTX)
7870 return;
7871
e0d80184
DM
7872 /* Align to cache line in the function's code section. */
7873 function_section (current_function_decl);
d676da68
DM
7874
7875 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
7876 if (align > 0)
f3b8847b 7877 ASM_OUTPUT_ALIGN (asm_out_file, align);
e0d80184
DM
7878
7879 for (t = sparc_addr_list; t ; t = XEXP (t, 1))
7880 sparc_output_addr_vec (XEXP (t, 0));
7881 for (t = sparc_addr_diff_list; t ; t = XEXP (t, 1))
7882 sparc_output_addr_diff_vec (XEXP (t, 0));
7883
7884 sparc_addr_list = sparc_addr_diff_list = NULL_RTX;
7885}
7886
284d86e9
JC
7887/* Return 0 if the high 32 bits of X (the low word of X, if DImode) are
7888 unknown. Return 1 if the high bits are zero, -1 if the register is
7889 sign extended. */
7890int
7891sparc_check_64 (x, insn)
7892 rtx x, insn;
7893{
7894 /* If a register is set only once it is safe to ignore insns this
7895 code does not know how to handle. The loop will either recognize
7896 the single set and return the correct value or fail to recognize
7897 it and return 0. */
7898 int set_once = 0;
7899
7900 if (GET_CODE (x) == REG
7901 && flag_expensive_optimizations
7902 && REG_N_SETS (REGNO (x)) == 1)
7903 set_once = 1;
7904
7905 if (insn == 0)
d6f4ec51
KG
7906 {
7907 if (set_once)
7908 insn = get_last_insn_anywhere ();
7909 else
7910 return 0;
7911 }
284d86e9 7912
d6f4ec51 7913 while ((insn = PREV_INSN (insn)))
284d86e9
JC
7914 {
7915 switch (GET_CODE (insn))
7916 {
7917 case JUMP_INSN:
7918 case NOTE:
7919 break;
7920 case CODE_LABEL:
7921 case CALL_INSN:
7922 default:
7923 if (! set_once)
7924 return 0;
7925 break;
7926 case INSN:
7927 {
7928 rtx pat = PATTERN (insn);
7929 if (GET_CODE (pat) != SET)
7930 return 0;
7931 if (rtx_equal_p (x, SET_DEST (pat)))
7932 return set_extends (x, insn);
7933 if (reg_overlap_mentioned_p (SET_DEST (pat), x))
7934 return 0;
7935 }
7936 }
7937 }
7938 return 0;
7939}
7940
7941char *
7942sparc_v8plus_shift (operands, insn, opcode)
7943 rtx *operands;
7944 rtx insn;
b1474bb7 7945 const char *opcode;
284d86e9
JC
7946{
7947 static char asm_code[60];
7948
7949 if (GET_CODE (operands[3]) == SCRATCH)
7950 operands[3] = operands[0];
c6b0465b
JC
7951 if (GET_CODE (operands[1]) == CONST_INT)
7952 {
7953 output_asm_insn ("mov %1,%3", operands);
7954 }
7955 else
7956 {
7957 output_asm_insn ("sllx %H1,32,%3", operands);
7958 if (sparc_check_64 (operands[1], insn) <= 0)
7959 output_asm_insn ("srl %L1,0,%L1", operands);
7960 output_asm_insn ("or %L1,%3,%3", operands);
7961 }
284d86e9
JC
7962
7963 strcpy(asm_code, opcode);
7964 if (which_alternative != 2)
7965 return strcat (asm_code, " %0,%2,%L0\n\tsrlx %L0,32,%H0");
7966 else
7967 return strcat (asm_code, " %3,%2,%3\n\tsrlx %3,32,%H0\n\tmov %3,%L0");
7968}
7969
7970
7971/* Return 1 if DEST and SRC reference only global and in registers. */
7972
7973int
7974sparc_return_peephole_ok (dest, src)
7975 rtx dest, src;
7976{
7977 if (! TARGET_V9)
7978 return 0;
54ff41b7 7979 if (current_function_uses_only_leaf_regs)
284d86e9
JC
7980 return 0;
7981 if (GET_CODE (src) != CONST_INT
7982 && (GET_CODE (src) != REG || ! IN_OR_GLOBAL_P (src)))
7983 return 0;
7984 return IN_OR_GLOBAL_P (dest);
7985}
2be15d0f
RH
7986\f
7987/* Output assembler code to FILE to increment profiler label # LABELNO
7988 for profiling a function entry.
7989
7990 32 bit sparc uses %g2 as the STATIC_CHAIN_REGNUM which gets clobbered
7991 during profiling so we need to save/restore it around the call to mcount.
7992 We're guaranteed that a save has just been done, and we use the space
7993 allocated for intreg/fpreg value passing. */
7994
7995void
7996sparc_function_profiler (file, labelno)
7997 FILE *file;
7998 int labelno;
7999{
8000 char buf[32];
8001 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
8002
8003 if (! TARGET_ARCH64)
8004 fputs ("\tst\t%g2,[%fp-4]\n", file);
8005
8006 fputs ("\tsethi\t%hi(", file);
8007 assemble_name (file, buf);
8008 fputs ("),%o0\n", file);
8009
8010 fputs ("\tcall\t", file);
8011 assemble_name (file, MCOUNT_FUNCTION);
8012 putc ('\n', file);
8013
8014 fputs ("\t or\t%o0,%lo(", file);
8015 assemble_name (file, buf);
8016 fputs ("),%o0\n", file);
8017
8018 if (! TARGET_ARCH64)
8019 fputs ("\tld\t[%fp-4],%g2\n", file);
8020}
8021
8022
8023/* The following macro shall output assembler code to FILE
8024 to initialize basic-block profiling.
8025
8026 If profile_block_flag == 2
8027
8028 Output code to call the subroutine `__bb_init_trace_func'
8029 and pass two parameters to it. The first parameter is
8030 the address of a block allocated in the object module.
8031 The second parameter is the number of the first basic block
8032 of the function.
8033
8034 The name of the block is a local symbol made with this statement:
8035
8036 ASM_GENERATE_INTERNAL_LABEL (BUFFER, "LPBX", 0);
8037
8038 Of course, since you are writing the definition of
8039 `ASM_GENERATE_INTERNAL_LABEL' as well as that of this macro, you
8040 can take a short cut in the definition of this macro and use the
8041 name that you know will result.
8042
8043 The number of the first basic block of the function is
8044 passed to the macro in BLOCK_OR_LABEL.
8045
8046 If described in a virtual assembler language the code to be
8047 output looks like:
8048
8049 parameter1 <- LPBX0
8050 parameter2 <- BLOCK_OR_LABEL
8051 call __bb_init_trace_func
8052
8053 else if profile_block_flag != 0
8054
8055 Output code to call the subroutine `__bb_init_func'
8056 and pass one single parameter to it, which is the same
8057 as the first parameter to `__bb_init_trace_func'.
8058
8059 The first word of this parameter is a flag which will be nonzero if
8060 the object module has already been initialized. So test this word
8061 first, and do not call `__bb_init_func' if the flag is nonzero.
8062 Note: When profile_block_flag == 2 the test need not be done
8063 but `__bb_init_trace_func' *must* be called.
8064
8065 BLOCK_OR_LABEL may be used to generate a label number as a
8066 branch destination in case `__bb_init_func' will not be called.
8067
8068 If described in a virtual assembler language the code to be
8069 output looks like:
8070
8071 cmp (LPBX0),0
8072 jne local_label
8073 parameter1 <- LPBX0
8074 call __bb_init_func
8075 local_label:
8076
8077*/
8078
8079void
8080sparc_function_block_profiler(file, block_or_label)
8081 FILE *file;
8082 int block_or_label;
8083{
8084 char LPBX[32];
8085 ASM_GENERATE_INTERNAL_LABEL (LPBX, "LPBX", 0);
8086
8087 if (profile_block_flag == 2)
8088 {
8089 fputs ("\tsethi\t%hi(", file);
8090 assemble_name (file, LPBX);
8091 fputs ("),%o0\n", file);
8092
8093 fprintf (file, "\tsethi\t%%hi(%d),%%o1\n", block_or_label);
8094
8095 fputs ("\tor\t%o0,%lo(", file);
8096 assemble_name (file, LPBX);
8097 fputs ("),%o0\n", file);
8098
8099 fprintf (file, "\tcall\t%s__bb_init_trace_func\n", user_label_prefix);
8100
8101 fprintf (file, "\t or\t%%o1,%%lo(%d),%%o1\n", block_or_label);
8102 }
8103 else if (profile_block_flag != 0)
8104 {
8105 char LPBY[32];
8106 ASM_GENERATE_INTERNAL_LABEL (LPBY, "LPBY", block_or_label);
8107
8108 fputs ("\tsethi\t%hi(", file);
8109 assemble_name (file, LPBX);
8110 fputs ("),%o0\n", file);
8111
8112 fputs ("\tld\t[%lo(", file);
8113 assemble_name (file, LPBX);
8114 fputs (")+%o0],%o1\n", file);
8115
8116 fputs ("\ttst\t%o1\n", file);
8117
8118 if (TARGET_V9)
8119 {
8120 fputs ("\tbne,pn\t%icc,", file);
8121 assemble_name (file, LPBY);
8122 putc ('\n', file);
8123 }
8124 else
8125 {
8126 fputs ("\tbne\t", file);
8127 assemble_name (file, LPBY);
8128 putc ('\n', file);
8129 }
8130
8131 fputs ("\t or\t%o0,%lo(", file);
8132 assemble_name (file, LPBX);
8133 fputs ("),%o0\n", file);
8134
8135 fprintf (file, "\tcall\t%s__bb_init_func\n\t nop\n", user_label_prefix);
8136
8137 ASM_OUTPUT_INTERNAL_LABEL (file, "LPBY", block_or_label);
8138 }
8139}
8140
8141/* The following macro shall output assembler code to FILE
8142 to increment a counter associated with basic block number BLOCKNO.
8143
8144 If profile_block_flag == 2
8145
8146 Output code to initialize the global structure `__bb' and
8147 call the function `__bb_trace_func' which will increment the
8148 counter.
8149
8150 `__bb' consists of two words. In the first word the number
8151 of the basic block has to be stored. In the second word
8152 the address of a block allocated in the object module
8153 has to be stored.
8154
8155 The basic block number is given by BLOCKNO.
8156
8157 The address of the block is given by the label created with
8158
8159 ASM_GENERATE_INTERNAL_LABEL (BUFFER, "LPBX", 0);
8160
8161 by FUNCTION_BLOCK_PROFILER.
8162
8163 Of course, since you are writing the definition of
8164 `ASM_GENERATE_INTERNAL_LABEL' as well as that of this macro, you
8165 can take a short cut in the definition of this macro and use the
8166 name that you know will result.
8167
8168 If described in a virtual assembler language the code to be
8169 output looks like:
8170
8171 move BLOCKNO -> (__bb)
8172 move LPBX0 -> (__bb+4)
8173 call __bb_trace_func
8174
8175 Note that function `__bb_trace_func' must not change the
8176 machine state, especially the flag register. To grant
8177 this, you must output code to save and restore registers
8178 either in this macro or in the macros MACHINE_STATE_SAVE
8179 and MACHINE_STATE_RESTORE. The last two macros will be
8180 used in the function `__bb_trace_func', so you must make
8181 sure that the function prologue does not change any
8182 register prior to saving it with MACHINE_STATE_SAVE.
8183
8184 else if profile_block_flag != 0
8185
8186 Output code to increment the counter directly.
8187 Basic blocks are numbered separately from zero within each
8188 compiled object module. The count associated with block number
8189 BLOCKNO is at index BLOCKNO in an array of words; the name of
8190 this array is a local symbol made with this statement:
8191
8192 ASM_GENERATE_INTERNAL_LABEL (BUFFER, "LPBX", 2);
8193
8194 Of course, since you are writing the definition of
8195 `ASM_GENERATE_INTERNAL_LABEL' as well as that of this macro, you
8196 can take a short cut in the definition of this macro and use the
8197 name that you know will result.
8198
8199 If described in a virtual assembler language, the code to be
8200 output looks like:
8201
8202 inc (LPBX2+4*BLOCKNO)
8203
8204*/
8205
8206void
8207sparc_block_profiler(file, blockno)
8208 FILE *file;
8209 int blockno;
8210{
8211 char LPBX[32];
a330e73b 8212 int bbreg = TARGET_ARCH64 ? 4 : 2;
2be15d0f
RH
8213
8214 if (profile_block_flag == 2)
8215 {
8216 ASM_GENERATE_INTERNAL_LABEL (LPBX, "LPBX", 0);
8217
8218 fprintf (file, "\tsethi\t%%hi(%s__bb),%%g1\n", user_label_prefix);
a330e73b 8219 fprintf (file, "\tsethi\t%%hi(%d),%%g%d\n", blockno, bbreg);
2be15d0f 8220 fprintf (file, "\tor\t%%g1,%%lo(%s__bb),%%g1\n", user_label_prefix);
a330e73b 8221 fprintf (file, "\tor\t%%g%d,%%lo(%d),%%g%d\n", bbreg, blockno, bbreg);
2be15d0f 8222
a330e73b 8223 fprintf (file, "\tst\t%%g%d,[%%g1]\n", bbreg);
2be15d0f
RH
8224
8225 fputs ("\tsethi\t%hi(", file);
8226 assemble_name (file, LPBX);
a330e73b 8227 fprintf (file, "),%%g%d\n", bbreg);
2be15d0f 8228
110b0302 8229 fputs ("\tor\t%o2,%lo(", file);
2be15d0f 8230 assemble_name (file, LPBX);
a330e73b 8231 fprintf (file, "),%%g%d\n", bbreg);
2be15d0f 8232
a330e73b
JJ
8233 fprintf (file, "\tst\t%%g%d,[%%g1+4]\n", bbreg);
8234 fprintf (file, "\tmov\t%%o7,%%g%d\n", bbreg);
2be15d0f
RH
8235
8236 fprintf (file, "\tcall\t%s__bb_trace_func\n\t nop\n", user_label_prefix);
8237
a330e73b 8238 fprintf (file, "\tmov\t%%g%d,%%o7\n", bbreg);
2be15d0f
RH
8239 }
8240 else if (profile_block_flag != 0)
8241 {
8242 ASM_GENERATE_INTERNAL_LABEL (LPBX, "LPBX", 2);
8243
8244 fputs ("\tsethi\t%hi(", file);
8245 assemble_name (file, LPBX);
8246 fprintf (file, "+%d),%%g1\n", blockno*4);
8247
8248 fputs ("\tld\t[%g1+%lo(", file);
8249 assemble_name (file, LPBX);
a330e73b
JJ
8250 if (TARGET_ARCH64 && USE_AS_OFFSETABLE_LO10)
8251 fprintf (file, ")+%d],%%g%d\n", blockno*4, bbreg);
8252 else
8253 fprintf (file, "+%d)],%%g%d\n", blockno*4, bbreg);
2be15d0f 8254
a330e73b 8255 fprintf (file, "\tadd\t%%g%d,1,%%g%d\n", bbreg, bbreg);
2be15d0f 8256
a330e73b 8257 fprintf (file, "\tst\t%%g%d,[%%g1+%%lo(", bbreg);
2be15d0f 8258 assemble_name (file, LPBX);
a330e73b
JJ
8259 if (TARGET_ARCH64 && USE_AS_OFFSETABLE_LO10)
8260 fprintf (file, ")+%d]\n", blockno*4);
8261 else
8262 fprintf (file, "+%d)]\n", blockno*4);
2be15d0f
RH
8263 }
8264}
8265
8266/* The following macro shall output assembler code to FILE
8267 to indicate a return from function during basic-block profiling.
8268
8269 If profile_block_flag == 2:
8270
8271 Output assembler code to call function `__bb_trace_ret'.
8272
8273 Note that function `__bb_trace_ret' must not change the
8274 machine state, especially the flag register. To grant
8275 this, you must output code to save and restore registers
8276 either in this macro or in the macros MACHINE_STATE_SAVE_RET
8277 and MACHINE_STATE_RESTORE_RET. The last two macros will be
8278 used in the function `__bb_trace_ret', so you must make
8279 sure that the function prologue does not change any
8280 register prior to saving it with MACHINE_STATE_SAVE_RET.
8281
8282 else if profile_block_flag != 0:
8283
8284 The macro will not be used, so it need not distinguish
8285 these cases.
8286*/
8287
8288void
8289sparc_function_block_profiler_exit(file)
8290 FILE *file;
8291{
8292 if (profile_block_flag == 2)
8293 fprintf (file, "\tcall\t%s__bb_trace_ret\n\t nop\n", user_label_prefix);
8294 else
8295 abort ();
8296}
d07d525a
MM
8297
8298/* Mark ARG, which is really a struct ultrasparc_pipline_state *, for
8299 GC. */
8300
8301static void
8302mark_ultrasparc_pipeline_state (arg)
8303 void *arg;
8304{
8305 struct ultrasparc_pipeline_state *ups;
8306 size_t i;
8307
8308 ups = (struct ultrasparc_pipeline_state *) arg;
8309 for (i = 0; i < sizeof (ups->group) / sizeof (rtx); ++i)
8310 ggc_mark_rtx (ups->group[i]);
8311}
8312
8313/* Called to register all of our global variables with the garbage
8314 collector. */
8315
8316static void
8317sparc_add_gc_roots ()
8318{
8319 ggc_add_rtx_root (&sparc_compare_op0, 1);
8320 ggc_add_rtx_root (&sparc_compare_op1, 1);
8321 ggc_add_rtx_root (&leaf_label, 1);
8322 ggc_add_rtx_root (&global_offset_table, 1);
8323 ggc_add_rtx_root (&get_pc_symbol, 1);
8324 ggc_add_rtx_root (&sparc_addr_diff_list, 1);
8325 ggc_add_rtx_root (&sparc_addr_list, 1);
8326 ggc_add_root (ultra_pipe_hist,
8327 sizeof (ultra_pipe_hist) / sizeof (ultra_pipe_hist[0]),
8328 sizeof (ultra_pipe_hist[0]),
8329 &mark_ultrasparc_pipeline_state);
8330}
This page took 1.832818 seconds and 5 git commands to generate.