]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/config/sparc/sparc.c
7f51dc870d905c612eeae2190fab3760af23349d
[thirdparty/gcc.git] / gcc / config / sparc / sparc.c
1 /* Subroutines for insn-output.c for SPARC.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4 64-bit SPARC-V9 support by Michael Tiemann, Jim Wilson, and Doug Evans,
5 at Cygnus Support.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "stringpool.h"
29 #include "stor-layout.h"
30 #include "calls.h"
31 #include "varasm.h"
32 #include "rtl.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "insn-config.h"
36 #include "insn-codes.h"
37 #include "conditions.h"
38 #include "output.h"
39 #include "insn-attr.h"
40 #include "flags.h"
41 #include "hashtab.h"
42 #include "hash-set.h"
43 #include "vec.h"
44 #include "machmode.h"
45 #include "input.h"
46 #include "function.h"
47 #include "except.h"
48 #include "expr.h"
49 #include "optabs.h"
50 #include "recog.h"
51 #include "diagnostic-core.h"
52 #include "ggc.h"
53 #include "tm_p.h"
54 #include "debug.h"
55 #include "target.h"
56 #include "target-def.h"
57 #include "common/common-target.h"
58 #include "hash-table.h"
59 #include "basic-block.h"
60 #include "tree-ssa-alias.h"
61 #include "internal-fn.h"
62 #include "gimple-fold.h"
63 #include "tree-eh.h"
64 #include "gimple-expr.h"
65 #include "is-a.h"
66 #include "gimple.h"
67 #include "gimplify.h"
68 #include "langhooks.h"
69 #include "reload.h"
70 #include "params.h"
71 #include "df.h"
72 #include "opts.h"
73 #include "tree-pass.h"
74 #include "context.h"
75 #include "wide-int.h"
76 #include "builtins.h"
77 #include "rtl-iter.h"
78
79 /* Processor costs */
80
81 struct processor_costs {
82 /* Integer load */
83 const int int_load;
84
85 /* Integer signed load */
86 const int int_sload;
87
88 /* Integer zeroed load */
89 const int int_zload;
90
91 /* Float load */
92 const int float_load;
93
94 /* fmov, fneg, fabs */
95 const int float_move;
96
97 /* fadd, fsub */
98 const int float_plusminus;
99
100 /* fcmp */
101 const int float_cmp;
102
103 /* fmov, fmovr */
104 const int float_cmove;
105
106 /* fmul */
107 const int float_mul;
108
109 /* fdivs */
110 const int float_div_sf;
111
112 /* fdivd */
113 const int float_div_df;
114
115 /* fsqrts */
116 const int float_sqrt_sf;
117
118 /* fsqrtd */
119 const int float_sqrt_df;
120
121 /* umul/smul */
122 const int int_mul;
123
124 /* mulX */
125 const int int_mulX;
126
127 /* integer multiply cost for each bit set past the most
128 significant 3, so the formula for multiply cost becomes:
129
130 if (rs1 < 0)
131 highest_bit = highest_clear_bit(rs1);
132 else
133 highest_bit = highest_set_bit(rs1);
134 if (highest_bit < 3)
135 highest_bit = 3;
136 cost = int_mul{,X} + ((highest_bit - 3) / int_mul_bit_factor);
137
138 A value of zero indicates that the multiply costs is fixed,
139 and not variable. */
140 const int int_mul_bit_factor;
141
142 /* udiv/sdiv */
143 const int int_div;
144
145 /* divX */
146 const int int_divX;
147
148 /* movcc, movr */
149 const int int_cmove;
150
151 /* penalty for shifts, due to scheduling rules etc. */
152 const int shift_penalty;
153 };
154
155 static const
156 struct processor_costs cypress_costs = {
157 COSTS_N_INSNS (2), /* int load */
158 COSTS_N_INSNS (2), /* int signed load */
159 COSTS_N_INSNS (2), /* int zeroed load */
160 COSTS_N_INSNS (2), /* float load */
161 COSTS_N_INSNS (5), /* fmov, fneg, fabs */
162 COSTS_N_INSNS (5), /* fadd, fsub */
163 COSTS_N_INSNS (1), /* fcmp */
164 COSTS_N_INSNS (1), /* fmov, fmovr */
165 COSTS_N_INSNS (7), /* fmul */
166 COSTS_N_INSNS (37), /* fdivs */
167 COSTS_N_INSNS (37), /* fdivd */
168 COSTS_N_INSNS (63), /* fsqrts */
169 COSTS_N_INSNS (63), /* fsqrtd */
170 COSTS_N_INSNS (1), /* imul */
171 COSTS_N_INSNS (1), /* imulX */
172 0, /* imul bit factor */
173 COSTS_N_INSNS (1), /* idiv */
174 COSTS_N_INSNS (1), /* idivX */
175 COSTS_N_INSNS (1), /* movcc/movr */
176 0, /* shift penalty */
177 };
178
179 static const
180 struct processor_costs supersparc_costs = {
181 COSTS_N_INSNS (1), /* int load */
182 COSTS_N_INSNS (1), /* int signed load */
183 COSTS_N_INSNS (1), /* int zeroed load */
184 COSTS_N_INSNS (0), /* float load */
185 COSTS_N_INSNS (3), /* fmov, fneg, fabs */
186 COSTS_N_INSNS (3), /* fadd, fsub */
187 COSTS_N_INSNS (3), /* fcmp */
188 COSTS_N_INSNS (1), /* fmov, fmovr */
189 COSTS_N_INSNS (3), /* fmul */
190 COSTS_N_INSNS (6), /* fdivs */
191 COSTS_N_INSNS (9), /* fdivd */
192 COSTS_N_INSNS (12), /* fsqrts */
193 COSTS_N_INSNS (12), /* fsqrtd */
194 COSTS_N_INSNS (4), /* imul */
195 COSTS_N_INSNS (4), /* imulX */
196 0, /* imul bit factor */
197 COSTS_N_INSNS (4), /* idiv */
198 COSTS_N_INSNS (4), /* idivX */
199 COSTS_N_INSNS (1), /* movcc/movr */
200 1, /* shift penalty */
201 };
202
203 static const
204 struct processor_costs hypersparc_costs = {
205 COSTS_N_INSNS (1), /* int load */
206 COSTS_N_INSNS (1), /* int signed load */
207 COSTS_N_INSNS (1), /* int zeroed load */
208 COSTS_N_INSNS (1), /* float load */
209 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
210 COSTS_N_INSNS (1), /* fadd, fsub */
211 COSTS_N_INSNS (1), /* fcmp */
212 COSTS_N_INSNS (1), /* fmov, fmovr */
213 COSTS_N_INSNS (1), /* fmul */
214 COSTS_N_INSNS (8), /* fdivs */
215 COSTS_N_INSNS (12), /* fdivd */
216 COSTS_N_INSNS (17), /* fsqrts */
217 COSTS_N_INSNS (17), /* fsqrtd */
218 COSTS_N_INSNS (17), /* imul */
219 COSTS_N_INSNS (17), /* imulX */
220 0, /* imul bit factor */
221 COSTS_N_INSNS (17), /* idiv */
222 COSTS_N_INSNS (17), /* idivX */
223 COSTS_N_INSNS (1), /* movcc/movr */
224 0, /* shift penalty */
225 };
226
227 static const
228 struct processor_costs leon_costs = {
229 COSTS_N_INSNS (1), /* int load */
230 COSTS_N_INSNS (1), /* int signed load */
231 COSTS_N_INSNS (1), /* int zeroed load */
232 COSTS_N_INSNS (1), /* float load */
233 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
234 COSTS_N_INSNS (1), /* fadd, fsub */
235 COSTS_N_INSNS (1), /* fcmp */
236 COSTS_N_INSNS (1), /* fmov, fmovr */
237 COSTS_N_INSNS (1), /* fmul */
238 COSTS_N_INSNS (15), /* fdivs */
239 COSTS_N_INSNS (15), /* fdivd */
240 COSTS_N_INSNS (23), /* fsqrts */
241 COSTS_N_INSNS (23), /* fsqrtd */
242 COSTS_N_INSNS (5), /* imul */
243 COSTS_N_INSNS (5), /* imulX */
244 0, /* imul bit factor */
245 COSTS_N_INSNS (5), /* idiv */
246 COSTS_N_INSNS (5), /* idivX */
247 COSTS_N_INSNS (1), /* movcc/movr */
248 0, /* shift penalty */
249 };
250
251 static const
252 struct processor_costs leon3_costs = {
253 COSTS_N_INSNS (1), /* int load */
254 COSTS_N_INSNS (1), /* int signed load */
255 COSTS_N_INSNS (1), /* int zeroed load */
256 COSTS_N_INSNS (1), /* float load */
257 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
258 COSTS_N_INSNS (1), /* fadd, fsub */
259 COSTS_N_INSNS (1), /* fcmp */
260 COSTS_N_INSNS (1), /* fmov, fmovr */
261 COSTS_N_INSNS (1), /* fmul */
262 COSTS_N_INSNS (14), /* fdivs */
263 COSTS_N_INSNS (15), /* fdivd */
264 COSTS_N_INSNS (22), /* fsqrts */
265 COSTS_N_INSNS (23), /* fsqrtd */
266 COSTS_N_INSNS (5), /* imul */
267 COSTS_N_INSNS (5), /* imulX */
268 0, /* imul bit factor */
269 COSTS_N_INSNS (35), /* idiv */
270 COSTS_N_INSNS (35), /* idivX */
271 COSTS_N_INSNS (1), /* movcc/movr */
272 0, /* shift penalty */
273 };
274
275 static const
276 struct processor_costs sparclet_costs = {
277 COSTS_N_INSNS (3), /* int load */
278 COSTS_N_INSNS (3), /* int signed load */
279 COSTS_N_INSNS (1), /* int zeroed load */
280 COSTS_N_INSNS (1), /* float load */
281 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
282 COSTS_N_INSNS (1), /* fadd, fsub */
283 COSTS_N_INSNS (1), /* fcmp */
284 COSTS_N_INSNS (1), /* fmov, fmovr */
285 COSTS_N_INSNS (1), /* fmul */
286 COSTS_N_INSNS (1), /* fdivs */
287 COSTS_N_INSNS (1), /* fdivd */
288 COSTS_N_INSNS (1), /* fsqrts */
289 COSTS_N_INSNS (1), /* fsqrtd */
290 COSTS_N_INSNS (5), /* imul */
291 COSTS_N_INSNS (5), /* imulX */
292 0, /* imul bit factor */
293 COSTS_N_INSNS (5), /* idiv */
294 COSTS_N_INSNS (5), /* idivX */
295 COSTS_N_INSNS (1), /* movcc/movr */
296 0, /* shift penalty */
297 };
298
299 static const
300 struct processor_costs ultrasparc_costs = {
301 COSTS_N_INSNS (2), /* int load */
302 COSTS_N_INSNS (3), /* int signed load */
303 COSTS_N_INSNS (2), /* int zeroed load */
304 COSTS_N_INSNS (2), /* float load */
305 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
306 COSTS_N_INSNS (4), /* fadd, fsub */
307 COSTS_N_INSNS (1), /* fcmp */
308 COSTS_N_INSNS (2), /* fmov, fmovr */
309 COSTS_N_INSNS (4), /* fmul */
310 COSTS_N_INSNS (13), /* fdivs */
311 COSTS_N_INSNS (23), /* fdivd */
312 COSTS_N_INSNS (13), /* fsqrts */
313 COSTS_N_INSNS (23), /* fsqrtd */
314 COSTS_N_INSNS (4), /* imul */
315 COSTS_N_INSNS (4), /* imulX */
316 2, /* imul bit factor */
317 COSTS_N_INSNS (37), /* idiv */
318 COSTS_N_INSNS (68), /* idivX */
319 COSTS_N_INSNS (2), /* movcc/movr */
320 2, /* shift penalty */
321 };
322
323 static const
324 struct processor_costs ultrasparc3_costs = {
325 COSTS_N_INSNS (2), /* int load */
326 COSTS_N_INSNS (3), /* int signed load */
327 COSTS_N_INSNS (3), /* int zeroed load */
328 COSTS_N_INSNS (2), /* float load */
329 COSTS_N_INSNS (3), /* fmov, fneg, fabs */
330 COSTS_N_INSNS (4), /* fadd, fsub */
331 COSTS_N_INSNS (5), /* fcmp */
332 COSTS_N_INSNS (3), /* fmov, fmovr */
333 COSTS_N_INSNS (4), /* fmul */
334 COSTS_N_INSNS (17), /* fdivs */
335 COSTS_N_INSNS (20), /* fdivd */
336 COSTS_N_INSNS (20), /* fsqrts */
337 COSTS_N_INSNS (29), /* fsqrtd */
338 COSTS_N_INSNS (6), /* imul */
339 COSTS_N_INSNS (6), /* imulX */
340 0, /* imul bit factor */
341 COSTS_N_INSNS (40), /* idiv */
342 COSTS_N_INSNS (71), /* idivX */
343 COSTS_N_INSNS (2), /* movcc/movr */
344 0, /* shift penalty */
345 };
346
347 static const
348 struct processor_costs niagara_costs = {
349 COSTS_N_INSNS (3), /* int load */
350 COSTS_N_INSNS (3), /* int signed load */
351 COSTS_N_INSNS (3), /* int zeroed load */
352 COSTS_N_INSNS (9), /* float load */
353 COSTS_N_INSNS (8), /* fmov, fneg, fabs */
354 COSTS_N_INSNS (8), /* fadd, fsub */
355 COSTS_N_INSNS (26), /* fcmp */
356 COSTS_N_INSNS (8), /* fmov, fmovr */
357 COSTS_N_INSNS (29), /* fmul */
358 COSTS_N_INSNS (54), /* fdivs */
359 COSTS_N_INSNS (83), /* fdivd */
360 COSTS_N_INSNS (100), /* fsqrts - not implemented in hardware */
361 COSTS_N_INSNS (100), /* fsqrtd - not implemented in hardware */
362 COSTS_N_INSNS (11), /* imul */
363 COSTS_N_INSNS (11), /* imulX */
364 0, /* imul bit factor */
365 COSTS_N_INSNS (72), /* idiv */
366 COSTS_N_INSNS (72), /* idivX */
367 COSTS_N_INSNS (1), /* movcc/movr */
368 0, /* shift penalty */
369 };
370
371 static const
372 struct processor_costs niagara2_costs = {
373 COSTS_N_INSNS (3), /* int load */
374 COSTS_N_INSNS (3), /* int signed load */
375 COSTS_N_INSNS (3), /* int zeroed load */
376 COSTS_N_INSNS (3), /* float load */
377 COSTS_N_INSNS (6), /* fmov, fneg, fabs */
378 COSTS_N_INSNS (6), /* fadd, fsub */
379 COSTS_N_INSNS (6), /* fcmp */
380 COSTS_N_INSNS (6), /* fmov, fmovr */
381 COSTS_N_INSNS (6), /* fmul */
382 COSTS_N_INSNS (19), /* fdivs */
383 COSTS_N_INSNS (33), /* fdivd */
384 COSTS_N_INSNS (19), /* fsqrts */
385 COSTS_N_INSNS (33), /* fsqrtd */
386 COSTS_N_INSNS (5), /* imul */
387 COSTS_N_INSNS (5), /* imulX */
388 0, /* imul bit factor */
389 COSTS_N_INSNS (26), /* idiv, average of 12 - 41 cycle range */
390 COSTS_N_INSNS (26), /* idivX, average of 12 - 41 cycle range */
391 COSTS_N_INSNS (1), /* movcc/movr */
392 0, /* shift penalty */
393 };
394
395 static const
396 struct processor_costs niagara3_costs = {
397 COSTS_N_INSNS (3), /* int load */
398 COSTS_N_INSNS (3), /* int signed load */
399 COSTS_N_INSNS (3), /* int zeroed load */
400 COSTS_N_INSNS (3), /* float load */
401 COSTS_N_INSNS (9), /* fmov, fneg, fabs */
402 COSTS_N_INSNS (9), /* fadd, fsub */
403 COSTS_N_INSNS (9), /* fcmp */
404 COSTS_N_INSNS (9), /* fmov, fmovr */
405 COSTS_N_INSNS (9), /* fmul */
406 COSTS_N_INSNS (23), /* fdivs */
407 COSTS_N_INSNS (37), /* fdivd */
408 COSTS_N_INSNS (23), /* fsqrts */
409 COSTS_N_INSNS (37), /* fsqrtd */
410 COSTS_N_INSNS (9), /* imul */
411 COSTS_N_INSNS (9), /* imulX */
412 0, /* imul bit factor */
413 COSTS_N_INSNS (31), /* idiv, average of 17 - 45 cycle range */
414 COSTS_N_INSNS (30), /* idivX, average of 16 - 44 cycle range */
415 COSTS_N_INSNS (1), /* movcc/movr */
416 0, /* shift penalty */
417 };
418
419 static const
420 struct processor_costs niagara4_costs = {
421 COSTS_N_INSNS (5), /* int load */
422 COSTS_N_INSNS (5), /* int signed load */
423 COSTS_N_INSNS (5), /* int zeroed load */
424 COSTS_N_INSNS (5), /* float load */
425 COSTS_N_INSNS (11), /* fmov, fneg, fabs */
426 COSTS_N_INSNS (11), /* fadd, fsub */
427 COSTS_N_INSNS (11), /* fcmp */
428 COSTS_N_INSNS (11), /* fmov, fmovr */
429 COSTS_N_INSNS (11), /* fmul */
430 COSTS_N_INSNS (24), /* fdivs */
431 COSTS_N_INSNS (37), /* fdivd */
432 COSTS_N_INSNS (24), /* fsqrts */
433 COSTS_N_INSNS (37), /* fsqrtd */
434 COSTS_N_INSNS (12), /* imul */
435 COSTS_N_INSNS (12), /* imulX */
436 0, /* imul bit factor */
437 COSTS_N_INSNS (50), /* idiv, average of 41 - 60 cycle range */
438 COSTS_N_INSNS (35), /* idivX, average of 26 - 44 cycle range */
439 COSTS_N_INSNS (1), /* movcc/movr */
440 0, /* shift penalty */
441 };
442
443 static const struct processor_costs *sparc_costs = &cypress_costs;
444
445 #ifdef HAVE_AS_RELAX_OPTION
446 /* If 'as' and 'ld' are relaxing tail call insns into branch always, use
447 "or %o7,%g0,X; call Y; or X,%g0,%o7" always, so that it can be optimized.
448 With sethi/jmp, neither 'as' nor 'ld' has an easy way how to find out if
449 somebody does not branch between the sethi and jmp. */
450 #define LEAF_SIBCALL_SLOT_RESERVED_P 1
451 #else
452 #define LEAF_SIBCALL_SLOT_RESERVED_P \
453 ((TARGET_ARCH64 && !TARGET_CM_MEDLOW) || flag_pic)
454 #endif
455
456 /* Vector to say how input registers are mapped to output registers.
457 HARD_FRAME_POINTER_REGNUM cannot be remapped by this function to
458 eliminate it. You must use -fomit-frame-pointer to get that. */
459 char leaf_reg_remap[] =
460 { 0, 1, 2, 3, 4, 5, 6, 7,
461 -1, -1, -1, -1, -1, -1, 14, -1,
462 -1, -1, -1, -1, -1, -1, -1, -1,
463 8, 9, 10, 11, 12, 13, -1, 15,
464
465 32, 33, 34, 35, 36, 37, 38, 39,
466 40, 41, 42, 43, 44, 45, 46, 47,
467 48, 49, 50, 51, 52, 53, 54, 55,
468 56, 57, 58, 59, 60, 61, 62, 63,
469 64, 65, 66, 67, 68, 69, 70, 71,
470 72, 73, 74, 75, 76, 77, 78, 79,
471 80, 81, 82, 83, 84, 85, 86, 87,
472 88, 89, 90, 91, 92, 93, 94, 95,
473 96, 97, 98, 99, 100, 101, 102};
474
475 /* Vector, indexed by hard register number, which contains 1
476 for a register that is allowable in a candidate for leaf
477 function treatment. */
478 char sparc_leaf_regs[] =
479 { 1, 1, 1, 1, 1, 1, 1, 1,
480 0, 0, 0, 0, 0, 0, 1, 0,
481 0, 0, 0, 0, 0, 0, 0, 0,
482 1, 1, 1, 1, 1, 1, 0, 1,
483 1, 1, 1, 1, 1, 1, 1, 1,
484 1, 1, 1, 1, 1, 1, 1, 1,
485 1, 1, 1, 1, 1, 1, 1, 1,
486 1, 1, 1, 1, 1, 1, 1, 1,
487 1, 1, 1, 1, 1, 1, 1, 1,
488 1, 1, 1, 1, 1, 1, 1, 1,
489 1, 1, 1, 1, 1, 1, 1, 1,
490 1, 1, 1, 1, 1, 1, 1, 1,
491 1, 1, 1, 1, 1, 1, 1};
492
493 struct GTY(()) machine_function
494 {
495 /* Size of the frame of the function. */
496 HOST_WIDE_INT frame_size;
497
498 /* Size of the frame of the function minus the register window save area
499 and the outgoing argument area. */
500 HOST_WIDE_INT apparent_frame_size;
501
502 /* Register we pretend the frame pointer is allocated to. Normally, this
503 is %fp, but if we are in a leaf procedure, this is (%sp + offset). We
504 record "offset" separately as it may be too big for (reg + disp). */
505 rtx frame_base_reg;
506 HOST_WIDE_INT frame_base_offset;
507
508 /* Number of global or FP registers to be saved (as 4-byte quantities). */
509 int n_global_fp_regs;
510
511 /* True if the current function is leaf and uses only leaf regs,
512 so that the SPARC leaf function optimization can be applied.
513 Private version of crtl->uses_only_leaf_regs, see
514 sparc_expand_prologue for the rationale. */
515 int leaf_function_p;
516
517 /* True if the prologue saves local or in registers. */
518 bool save_local_in_regs_p;
519
520 /* True if the data calculated by sparc_expand_prologue are valid. */
521 bool prologue_data_valid_p;
522 };
523
524 #define sparc_frame_size cfun->machine->frame_size
525 #define sparc_apparent_frame_size cfun->machine->apparent_frame_size
526 #define sparc_frame_base_reg cfun->machine->frame_base_reg
527 #define sparc_frame_base_offset cfun->machine->frame_base_offset
528 #define sparc_n_global_fp_regs cfun->machine->n_global_fp_regs
529 #define sparc_leaf_function_p cfun->machine->leaf_function_p
530 #define sparc_save_local_in_regs_p cfun->machine->save_local_in_regs_p
531 #define sparc_prologue_data_valid_p cfun->machine->prologue_data_valid_p
532
533 /* 1 if the next opcode is to be specially indented. */
534 int sparc_indent_opcode = 0;
535
536 static void sparc_option_override (void);
537 static void sparc_init_modes (void);
538 static void scan_record_type (const_tree, int *, int *, int *);
539 static int function_arg_slotno (const CUMULATIVE_ARGS *, enum machine_mode,
540 const_tree, bool, bool, int *, int *);
541
542 static int supersparc_adjust_cost (rtx_insn *, rtx, rtx_insn *, int);
543 static int hypersparc_adjust_cost (rtx_insn *, rtx, rtx_insn *, int);
544
545 static void sparc_emit_set_const32 (rtx, rtx);
546 static void sparc_emit_set_const64 (rtx, rtx);
547 static void sparc_output_addr_vec (rtx);
548 static void sparc_output_addr_diff_vec (rtx);
549 static void sparc_output_deferred_case_vectors (void);
550 static bool sparc_legitimate_address_p (enum machine_mode, rtx, bool);
551 static bool sparc_legitimate_constant_p (enum machine_mode, rtx);
552 static rtx sparc_builtin_saveregs (void);
553 static int epilogue_renumber (rtx *, int);
554 static bool sparc_assemble_integer (rtx, unsigned int, int);
555 static int set_extends (rtx_insn *);
556 static void sparc_asm_function_prologue (FILE *, HOST_WIDE_INT);
557 static void sparc_asm_function_epilogue (FILE *, HOST_WIDE_INT);
558 #ifdef TARGET_SOLARIS
559 static void sparc_solaris_elf_asm_named_section (const char *, unsigned int,
560 tree) ATTRIBUTE_UNUSED;
561 #endif
562 static int sparc_adjust_cost (rtx_insn *, rtx, rtx_insn *, int);
563 static int sparc_issue_rate (void);
564 static void sparc_sched_init (FILE *, int, int);
565 static int sparc_use_sched_lookahead (void);
566
567 static void emit_soft_tfmode_libcall (const char *, int, rtx *);
568 static void emit_soft_tfmode_binop (enum rtx_code, rtx *);
569 static void emit_soft_tfmode_unop (enum rtx_code, rtx *);
570 static void emit_soft_tfmode_cvt (enum rtx_code, rtx *);
571 static void emit_hard_tfmode_operation (enum rtx_code, rtx *);
572
573 static bool sparc_function_ok_for_sibcall (tree, tree);
574 static void sparc_init_libfuncs (void);
575 static void sparc_init_builtins (void);
576 static void sparc_fpu_init_builtins (void);
577 static void sparc_vis_init_builtins (void);
578 static tree sparc_builtin_decl (unsigned, bool);
579 static rtx sparc_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
580 static tree sparc_fold_builtin (tree, int, tree *, bool);
581 static void sparc_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
582 HOST_WIDE_INT, tree);
583 static bool sparc_can_output_mi_thunk (const_tree, HOST_WIDE_INT,
584 HOST_WIDE_INT, const_tree);
585 static struct machine_function * sparc_init_machine_status (void);
586 static bool sparc_cannot_force_const_mem (enum machine_mode, rtx);
587 static rtx sparc_tls_get_addr (void);
588 static rtx sparc_tls_got (void);
589 static int sparc_register_move_cost (enum machine_mode,
590 reg_class_t, reg_class_t);
591 static bool sparc_rtx_costs (rtx, int, int, int, int *, bool);
592 static rtx sparc_function_value (const_tree, const_tree, bool);
593 static rtx sparc_libcall_value (enum machine_mode, const_rtx);
594 static bool sparc_function_value_regno_p (const unsigned int);
595 static rtx sparc_struct_value_rtx (tree, int);
596 static enum machine_mode sparc_promote_function_mode (const_tree, enum machine_mode,
597 int *, const_tree, int);
598 static bool sparc_return_in_memory (const_tree, const_tree);
599 static bool sparc_strict_argument_naming (cumulative_args_t);
600 static void sparc_va_start (tree, rtx);
601 static tree sparc_gimplify_va_arg (tree, tree, gimple_seq *, gimple_seq *);
602 static bool sparc_vector_mode_supported_p (enum machine_mode);
603 static bool sparc_tls_referenced_p (rtx);
604 static rtx sparc_legitimize_tls_address (rtx);
605 static rtx sparc_legitimize_pic_address (rtx, rtx);
606 static rtx sparc_legitimize_address (rtx, rtx, enum machine_mode);
607 static rtx sparc_delegitimize_address (rtx);
608 static bool sparc_mode_dependent_address_p (const_rtx, addr_space_t);
609 static bool sparc_pass_by_reference (cumulative_args_t,
610 enum machine_mode, const_tree, bool);
611 static void sparc_function_arg_advance (cumulative_args_t,
612 enum machine_mode, const_tree, bool);
613 static rtx sparc_function_arg_1 (cumulative_args_t,
614 enum machine_mode, const_tree, bool, bool);
615 static rtx sparc_function_arg (cumulative_args_t,
616 enum machine_mode, const_tree, bool);
617 static rtx sparc_function_incoming_arg (cumulative_args_t,
618 enum machine_mode, const_tree, bool);
619 static unsigned int sparc_function_arg_boundary (enum machine_mode,
620 const_tree);
621 static int sparc_arg_partial_bytes (cumulative_args_t,
622 enum machine_mode, tree, bool);
623 static void sparc_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
624 static void sparc_file_end (void);
625 static bool sparc_frame_pointer_required (void);
626 static bool sparc_can_eliminate (const int, const int);
627 static rtx sparc_builtin_setjmp_frame_value (void);
628 static void sparc_conditional_register_usage (void);
629 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
630 static const char *sparc_mangle_type (const_tree);
631 #endif
632 static void sparc_trampoline_init (rtx, tree, rtx);
633 static enum machine_mode sparc_preferred_simd_mode (enum machine_mode);
634 static reg_class_t sparc_preferred_reload_class (rtx x, reg_class_t rclass);
635 static bool sparc_print_operand_punct_valid_p (unsigned char);
636 static void sparc_print_operand (FILE *, rtx, int);
637 static void sparc_print_operand_address (FILE *, rtx);
638 static reg_class_t sparc_secondary_reload (bool, rtx, reg_class_t,
639 enum machine_mode,
640 secondary_reload_info *);
641 static enum machine_mode sparc_cstore_mode (enum insn_code icode);
642 static void sparc_atomic_assign_expand_fenv (tree *, tree *, tree *);
643 \f
644 #ifdef SUBTARGET_ATTRIBUTE_TABLE
645 /* Table of valid machine attributes. */
646 static const struct attribute_spec sparc_attribute_table[] =
647 {
648 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
649 do_diagnostic } */
650 SUBTARGET_ATTRIBUTE_TABLE,
651 { NULL, 0, 0, false, false, false, NULL, false }
652 };
653 #endif
654 \f
655 /* Option handling. */
656
657 /* Parsed value. */
658 enum cmodel sparc_cmodel;
659
660 char sparc_hard_reg_printed[8];
661
662 /* Initialize the GCC target structure. */
663
664 /* The default is to use .half rather than .short for aligned HI objects. */
665 #undef TARGET_ASM_ALIGNED_HI_OP
666 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
667
668 #undef TARGET_ASM_UNALIGNED_HI_OP
669 #define TARGET_ASM_UNALIGNED_HI_OP "\t.uahalf\t"
670 #undef TARGET_ASM_UNALIGNED_SI_OP
671 #define TARGET_ASM_UNALIGNED_SI_OP "\t.uaword\t"
672 #undef TARGET_ASM_UNALIGNED_DI_OP
673 #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaxword\t"
674
675 /* The target hook has to handle DI-mode values. */
676 #undef TARGET_ASM_INTEGER
677 #define TARGET_ASM_INTEGER sparc_assemble_integer
678
679 #undef TARGET_ASM_FUNCTION_PROLOGUE
680 #define TARGET_ASM_FUNCTION_PROLOGUE sparc_asm_function_prologue
681 #undef TARGET_ASM_FUNCTION_EPILOGUE
682 #define TARGET_ASM_FUNCTION_EPILOGUE sparc_asm_function_epilogue
683
684 #undef TARGET_SCHED_ADJUST_COST
685 #define TARGET_SCHED_ADJUST_COST sparc_adjust_cost
686 #undef TARGET_SCHED_ISSUE_RATE
687 #define TARGET_SCHED_ISSUE_RATE sparc_issue_rate
688 #undef TARGET_SCHED_INIT
689 #define TARGET_SCHED_INIT sparc_sched_init
690 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
691 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD sparc_use_sched_lookahead
692
693 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
694 #define TARGET_FUNCTION_OK_FOR_SIBCALL sparc_function_ok_for_sibcall
695
696 #undef TARGET_INIT_LIBFUNCS
697 #define TARGET_INIT_LIBFUNCS sparc_init_libfuncs
698
699 #undef TARGET_LEGITIMIZE_ADDRESS
700 #define TARGET_LEGITIMIZE_ADDRESS sparc_legitimize_address
701 #undef TARGET_DELEGITIMIZE_ADDRESS
702 #define TARGET_DELEGITIMIZE_ADDRESS sparc_delegitimize_address
703 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
704 #define TARGET_MODE_DEPENDENT_ADDRESS_P sparc_mode_dependent_address_p
705
706 #undef TARGET_INIT_BUILTINS
707 #define TARGET_INIT_BUILTINS sparc_init_builtins
708 #undef TARGET_BUILTIN_DECL
709 #define TARGET_BUILTIN_DECL sparc_builtin_decl
710 #undef TARGET_EXPAND_BUILTIN
711 #define TARGET_EXPAND_BUILTIN sparc_expand_builtin
712 #undef TARGET_FOLD_BUILTIN
713 #define TARGET_FOLD_BUILTIN sparc_fold_builtin
714
715 #if TARGET_TLS
716 #undef TARGET_HAVE_TLS
717 #define TARGET_HAVE_TLS true
718 #endif
719
720 #undef TARGET_CANNOT_FORCE_CONST_MEM
721 #define TARGET_CANNOT_FORCE_CONST_MEM sparc_cannot_force_const_mem
722
723 #undef TARGET_ASM_OUTPUT_MI_THUNK
724 #define TARGET_ASM_OUTPUT_MI_THUNK sparc_output_mi_thunk
725 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
726 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK sparc_can_output_mi_thunk
727
728 #undef TARGET_RTX_COSTS
729 #define TARGET_RTX_COSTS sparc_rtx_costs
730 #undef TARGET_ADDRESS_COST
731 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
732 #undef TARGET_REGISTER_MOVE_COST
733 #define TARGET_REGISTER_MOVE_COST sparc_register_move_cost
734
735 #undef TARGET_PROMOTE_FUNCTION_MODE
736 #define TARGET_PROMOTE_FUNCTION_MODE sparc_promote_function_mode
737
738 #undef TARGET_FUNCTION_VALUE
739 #define TARGET_FUNCTION_VALUE sparc_function_value
740 #undef TARGET_LIBCALL_VALUE
741 #define TARGET_LIBCALL_VALUE sparc_libcall_value
742 #undef TARGET_FUNCTION_VALUE_REGNO_P
743 #define TARGET_FUNCTION_VALUE_REGNO_P sparc_function_value_regno_p
744
745 #undef TARGET_STRUCT_VALUE_RTX
746 #define TARGET_STRUCT_VALUE_RTX sparc_struct_value_rtx
747 #undef TARGET_RETURN_IN_MEMORY
748 #define TARGET_RETURN_IN_MEMORY sparc_return_in_memory
749 #undef TARGET_MUST_PASS_IN_STACK
750 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
751 #undef TARGET_PASS_BY_REFERENCE
752 #define TARGET_PASS_BY_REFERENCE sparc_pass_by_reference
753 #undef TARGET_ARG_PARTIAL_BYTES
754 #define TARGET_ARG_PARTIAL_BYTES sparc_arg_partial_bytes
755 #undef TARGET_FUNCTION_ARG_ADVANCE
756 #define TARGET_FUNCTION_ARG_ADVANCE sparc_function_arg_advance
757 #undef TARGET_FUNCTION_ARG
758 #define TARGET_FUNCTION_ARG sparc_function_arg
759 #undef TARGET_FUNCTION_INCOMING_ARG
760 #define TARGET_FUNCTION_INCOMING_ARG sparc_function_incoming_arg
761 #undef TARGET_FUNCTION_ARG_BOUNDARY
762 #define TARGET_FUNCTION_ARG_BOUNDARY sparc_function_arg_boundary
763
764 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
765 #define TARGET_EXPAND_BUILTIN_SAVEREGS sparc_builtin_saveregs
766 #undef TARGET_STRICT_ARGUMENT_NAMING
767 #define TARGET_STRICT_ARGUMENT_NAMING sparc_strict_argument_naming
768
769 #undef TARGET_EXPAND_BUILTIN_VA_START
770 #define TARGET_EXPAND_BUILTIN_VA_START sparc_va_start
771 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
772 #define TARGET_GIMPLIFY_VA_ARG_EXPR sparc_gimplify_va_arg
773
774 #undef TARGET_VECTOR_MODE_SUPPORTED_P
775 #define TARGET_VECTOR_MODE_SUPPORTED_P sparc_vector_mode_supported_p
776
777 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
778 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE sparc_preferred_simd_mode
779
780 #ifdef SUBTARGET_INSERT_ATTRIBUTES
781 #undef TARGET_INSERT_ATTRIBUTES
782 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
783 #endif
784
785 #ifdef SUBTARGET_ATTRIBUTE_TABLE
786 #undef TARGET_ATTRIBUTE_TABLE
787 #define TARGET_ATTRIBUTE_TABLE sparc_attribute_table
788 #endif
789
790 #undef TARGET_RELAXED_ORDERING
791 #define TARGET_RELAXED_ORDERING SPARC_RELAXED_ORDERING
792
793 #undef TARGET_OPTION_OVERRIDE
794 #define TARGET_OPTION_OVERRIDE sparc_option_override
795
796 #if TARGET_GNU_TLS && defined(HAVE_AS_SPARC_UA_PCREL)
797 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
798 #define TARGET_ASM_OUTPUT_DWARF_DTPREL sparc_output_dwarf_dtprel
799 #endif
800
801 #undef TARGET_ASM_FILE_END
802 #define TARGET_ASM_FILE_END sparc_file_end
803
804 #undef TARGET_FRAME_POINTER_REQUIRED
805 #define TARGET_FRAME_POINTER_REQUIRED sparc_frame_pointer_required
806
807 #undef TARGET_BUILTIN_SETJMP_FRAME_VALUE
808 #define TARGET_BUILTIN_SETJMP_FRAME_VALUE sparc_builtin_setjmp_frame_value
809
810 #undef TARGET_CAN_ELIMINATE
811 #define TARGET_CAN_ELIMINATE sparc_can_eliminate
812
813 #undef TARGET_PREFERRED_RELOAD_CLASS
814 #define TARGET_PREFERRED_RELOAD_CLASS sparc_preferred_reload_class
815
816 #undef TARGET_SECONDARY_RELOAD
817 #define TARGET_SECONDARY_RELOAD sparc_secondary_reload
818
819 #undef TARGET_CONDITIONAL_REGISTER_USAGE
820 #define TARGET_CONDITIONAL_REGISTER_USAGE sparc_conditional_register_usage
821
822 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
823 #undef TARGET_MANGLE_TYPE
824 #define TARGET_MANGLE_TYPE sparc_mangle_type
825 #endif
826
827 #undef TARGET_LEGITIMATE_ADDRESS_P
828 #define TARGET_LEGITIMATE_ADDRESS_P sparc_legitimate_address_p
829
830 #undef TARGET_LEGITIMATE_CONSTANT_P
831 #define TARGET_LEGITIMATE_CONSTANT_P sparc_legitimate_constant_p
832
833 #undef TARGET_TRAMPOLINE_INIT
834 #define TARGET_TRAMPOLINE_INIT sparc_trampoline_init
835
836 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
837 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P sparc_print_operand_punct_valid_p
838 #undef TARGET_PRINT_OPERAND
839 #define TARGET_PRINT_OPERAND sparc_print_operand
840 #undef TARGET_PRINT_OPERAND_ADDRESS
841 #define TARGET_PRINT_OPERAND_ADDRESS sparc_print_operand_address
842
843 /* The value stored by LDSTUB. */
844 #undef TARGET_ATOMIC_TEST_AND_SET_TRUEVAL
845 #define TARGET_ATOMIC_TEST_AND_SET_TRUEVAL 0xff
846
847 #undef TARGET_CSTORE_MODE
848 #define TARGET_CSTORE_MODE sparc_cstore_mode
849
850 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
851 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV sparc_atomic_assign_expand_fenv
852
853 struct gcc_target targetm = TARGET_INITIALIZER;
854
855 /* Return the memory reference contained in X if any, zero otherwise. */
856
857 static rtx
858 mem_ref (rtx x)
859 {
860 if (GET_CODE (x) == SIGN_EXTEND || GET_CODE (x) == ZERO_EXTEND)
861 x = XEXP (x, 0);
862
863 if (MEM_P (x))
864 return x;
865
866 return NULL_RTX;
867 }
868
869 /* We use a machine specific pass to enable workarounds for errata.
870 We need to have the (essentially) final form of the insn stream in order
871 to properly detect the various hazards. Therefore, this machine specific
872 pass runs as late as possible. The pass is inserted in the pass pipeline
873 at the end of sparc_option_override. */
874
875 static unsigned int
876 sparc_do_work_around_errata (void)
877 {
878 rtx_insn *insn, *next;
879
880 /* Force all instructions to be split into their final form. */
881 split_all_insns_noflow ();
882
883 /* Now look for specific patterns in the insn stream. */
884 for (insn = get_insns (); insn; insn = next)
885 {
886 bool insert_nop = false;
887 rtx set;
888
889 /* Look into the instruction in a delay slot. */
890 if (NONJUMP_INSN_P (insn))
891 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
892 insn = seq->insn (1);
893
894 /* Look for a single-word load into an odd-numbered FP register. */
895 if (sparc_fix_at697f
896 && NONJUMP_INSN_P (insn)
897 && (set = single_set (insn)) != NULL_RTX
898 && GET_MODE_SIZE (GET_MODE (SET_SRC (set))) == 4
899 && MEM_P (SET_SRC (set))
900 && REG_P (SET_DEST (set))
901 && REGNO (SET_DEST (set)) > 31
902 && REGNO (SET_DEST (set)) % 2 != 0)
903 {
904 /* The wrong dependency is on the enclosing double register. */
905 const unsigned int x = REGNO (SET_DEST (set)) - 1;
906 unsigned int src1, src2, dest;
907 int code;
908
909 next = next_active_insn (insn);
910 if (!next)
911 break;
912 /* If the insn is a branch, then it cannot be problematic. */
913 if (!NONJUMP_INSN_P (next) || GET_CODE (PATTERN (next)) == SEQUENCE)
914 continue;
915
916 extract_insn (next);
917 code = INSN_CODE (next);
918
919 switch (code)
920 {
921 case CODE_FOR_adddf3:
922 case CODE_FOR_subdf3:
923 case CODE_FOR_muldf3:
924 case CODE_FOR_divdf3:
925 dest = REGNO (recog_data.operand[0]);
926 src1 = REGNO (recog_data.operand[1]);
927 src2 = REGNO (recog_data.operand[2]);
928 if (src1 != src2)
929 {
930 /* Case [1-4]:
931 ld [address], %fx+1
932 FPOPd %f{x,y}, %f{y,x}, %f{x,y} */
933 if ((src1 == x || src2 == x)
934 && (dest == src1 || dest == src2))
935 insert_nop = true;
936 }
937 else
938 {
939 /* Case 5:
940 ld [address], %fx+1
941 FPOPd %fx, %fx, %fx */
942 if (src1 == x
943 && dest == src1
944 && (code == CODE_FOR_adddf3 || code == CODE_FOR_muldf3))
945 insert_nop = true;
946 }
947 break;
948
949 case CODE_FOR_sqrtdf2:
950 dest = REGNO (recog_data.operand[0]);
951 src1 = REGNO (recog_data.operand[1]);
952 /* Case 6:
953 ld [address], %fx+1
954 fsqrtd %fx, %fx */
955 if (src1 == x && dest == src1)
956 insert_nop = true;
957 break;
958
959 default:
960 break;
961 }
962 }
963
964 /* Look for a single-word load into an integer register. */
965 else if (sparc_fix_ut699
966 && NONJUMP_INSN_P (insn)
967 && (set = single_set (insn)) != NULL_RTX
968 && GET_MODE_SIZE (GET_MODE (SET_SRC (set))) <= 4
969 && mem_ref (SET_SRC (set)) != NULL_RTX
970 && REG_P (SET_DEST (set))
971 && REGNO (SET_DEST (set)) < 32)
972 {
973 /* There is no problem if the second memory access has a data
974 dependency on the first single-cycle load. */
975 rtx x = SET_DEST (set);
976
977 next = next_active_insn (insn);
978 if (!next)
979 break;
980 /* If the insn is a branch, then it cannot be problematic. */
981 if (!NONJUMP_INSN_P (next) || GET_CODE (PATTERN (next)) == SEQUENCE)
982 continue;
983
984 /* Look for a second memory access to/from an integer register. */
985 if ((set = single_set (next)) != NULL_RTX)
986 {
987 rtx src = SET_SRC (set);
988 rtx dest = SET_DEST (set);
989 rtx mem;
990
991 /* LDD is affected. */
992 if ((mem = mem_ref (src)) != NULL_RTX
993 && REG_P (dest)
994 && REGNO (dest) < 32
995 && !reg_mentioned_p (x, XEXP (mem, 0)))
996 insert_nop = true;
997
998 /* STD is *not* affected. */
999 else if (MEM_P (dest)
1000 && GET_MODE_SIZE (GET_MODE (dest)) <= 4
1001 && (src == CONST0_RTX (GET_MODE (dest))
1002 || (REG_P (src)
1003 && REGNO (src) < 32
1004 && REGNO (src) != REGNO (x)))
1005 && !reg_mentioned_p (x, XEXP (dest, 0)))
1006 insert_nop = true;
1007 }
1008 }
1009
1010 /* Look for a single-word load/operation into an FP register. */
1011 else if (sparc_fix_ut699
1012 && NONJUMP_INSN_P (insn)
1013 && (set = single_set (insn)) != NULL_RTX
1014 && GET_MODE_SIZE (GET_MODE (SET_SRC (set))) == 4
1015 && REG_P (SET_DEST (set))
1016 && REGNO (SET_DEST (set)) > 31)
1017 {
1018 /* Number of instructions in the problematic window. */
1019 const int n_insns = 4;
1020 /* The problematic combination is with the sibling FP register. */
1021 const unsigned int x = REGNO (SET_DEST (set));
1022 const unsigned int y = x ^ 1;
1023 rtx_insn *after;
1024 int i;
1025
1026 next = next_active_insn (insn);
1027 if (!next)
1028 break;
1029 /* If the insn is a branch, then it cannot be problematic. */
1030 if (!NONJUMP_INSN_P (next) || GET_CODE (PATTERN (next)) == SEQUENCE)
1031 continue;
1032
1033 /* Look for a second load/operation into the sibling FP register. */
1034 if (!((set = single_set (next)) != NULL_RTX
1035 && GET_MODE_SIZE (GET_MODE (SET_SRC (set))) == 4
1036 && REG_P (SET_DEST (set))
1037 && REGNO (SET_DEST (set)) == y))
1038 continue;
1039
1040 /* Look for a (possible) store from the FP register in the next N
1041 instructions, but bail out if it is again modified or if there
1042 is a store from the sibling FP register before this store. */
1043 for (after = next, i = 0; i < n_insns; i++)
1044 {
1045 bool branch_p;
1046
1047 after = next_active_insn (after);
1048 if (!after)
1049 break;
1050
1051 /* This is a branch with an empty delay slot. */
1052 if (!NONJUMP_INSN_P (after))
1053 {
1054 if (++i == n_insns)
1055 break;
1056 branch_p = true;
1057 after = NULL;
1058 }
1059 /* This is a branch with a filled delay slot. */
1060 else if (rtx_sequence *seq =
1061 dyn_cast <rtx_sequence *> (PATTERN (after)))
1062 {
1063 if (++i == n_insns)
1064 break;
1065 branch_p = true;
1066 after = seq->insn (1);
1067 }
1068 /* This is a regular instruction. */
1069 else
1070 branch_p = false;
1071
1072 if (after && (set = single_set (after)) != NULL_RTX)
1073 {
1074 const rtx src = SET_SRC (set);
1075 const rtx dest = SET_DEST (set);
1076 const unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
1077
1078 /* If the FP register is again modified before the store,
1079 then the store isn't affected. */
1080 if (REG_P (dest)
1081 && (REGNO (dest) == x
1082 || (REGNO (dest) == y && size == 8)))
1083 break;
1084
1085 if (MEM_P (dest) && REG_P (src))
1086 {
1087 /* If there is a store from the sibling FP register
1088 before the store, then the store is not affected. */
1089 if (REGNO (src) == y || (REGNO (src) == x && size == 8))
1090 break;
1091
1092 /* Otherwise, the store is affected. */
1093 if (REGNO (src) == x && size == 4)
1094 {
1095 insert_nop = true;
1096 break;
1097 }
1098 }
1099 }
1100
1101 /* If we have a branch in the first M instructions, then we
1102 cannot see the (M+2)th instruction so we play safe. */
1103 if (branch_p && i <= (n_insns - 2))
1104 {
1105 insert_nop = true;
1106 break;
1107 }
1108 }
1109 }
1110
1111 else
1112 next = NEXT_INSN (insn);
1113
1114 if (insert_nop)
1115 emit_insn_before (gen_nop (), next);
1116 }
1117
1118 return 0;
1119 }
1120
1121 namespace {
1122
1123 const pass_data pass_data_work_around_errata =
1124 {
1125 RTL_PASS, /* type */
1126 "errata", /* name */
1127 OPTGROUP_NONE, /* optinfo_flags */
1128 TV_MACH_DEP, /* tv_id */
1129 0, /* properties_required */
1130 0, /* properties_provided */
1131 0, /* properties_destroyed */
1132 0, /* todo_flags_start */
1133 0, /* todo_flags_finish */
1134 };
1135
1136 class pass_work_around_errata : public rtl_opt_pass
1137 {
1138 public:
1139 pass_work_around_errata(gcc::context *ctxt)
1140 : rtl_opt_pass(pass_data_work_around_errata, ctxt)
1141 {}
1142
1143 /* opt_pass methods: */
1144 virtual bool gate (function *)
1145 {
1146 /* The only errata we handle are those of the AT697F and UT699. */
1147 return sparc_fix_at697f != 0 || sparc_fix_ut699 != 0;
1148 }
1149
1150 virtual unsigned int execute (function *)
1151 {
1152 return sparc_do_work_around_errata ();
1153 }
1154
1155 }; // class pass_work_around_errata
1156
1157 } // anon namespace
1158
1159 rtl_opt_pass *
1160 make_pass_work_around_errata (gcc::context *ctxt)
1161 {
1162 return new pass_work_around_errata (ctxt);
1163 }
1164
1165 /* Helpers for TARGET_DEBUG_OPTIONS. */
1166 static void
1167 dump_target_flag_bits (const int flags)
1168 {
1169 if (flags & MASK_64BIT)
1170 fprintf (stderr, "64BIT ");
1171 if (flags & MASK_APP_REGS)
1172 fprintf (stderr, "APP_REGS ");
1173 if (flags & MASK_FASTER_STRUCTS)
1174 fprintf (stderr, "FASTER_STRUCTS ");
1175 if (flags & MASK_FLAT)
1176 fprintf (stderr, "FLAT ");
1177 if (flags & MASK_FMAF)
1178 fprintf (stderr, "FMAF ");
1179 if (flags & MASK_FPU)
1180 fprintf (stderr, "FPU ");
1181 if (flags & MASK_HARD_QUAD)
1182 fprintf (stderr, "HARD_QUAD ");
1183 if (flags & MASK_POPC)
1184 fprintf (stderr, "POPC ");
1185 if (flags & MASK_PTR64)
1186 fprintf (stderr, "PTR64 ");
1187 if (flags & MASK_STACK_BIAS)
1188 fprintf (stderr, "STACK_BIAS ");
1189 if (flags & MASK_UNALIGNED_DOUBLES)
1190 fprintf (stderr, "UNALIGNED_DOUBLES ");
1191 if (flags & MASK_V8PLUS)
1192 fprintf (stderr, "V8PLUS ");
1193 if (flags & MASK_VIS)
1194 fprintf (stderr, "VIS ");
1195 if (flags & MASK_VIS2)
1196 fprintf (stderr, "VIS2 ");
1197 if (flags & MASK_VIS3)
1198 fprintf (stderr, "VIS3 ");
1199 if (flags & MASK_CBCOND)
1200 fprintf (stderr, "CBCOND ");
1201 if (flags & MASK_DEPRECATED_V8_INSNS)
1202 fprintf (stderr, "DEPRECATED_V8_INSNS ");
1203 if (flags & MASK_SPARCLET)
1204 fprintf (stderr, "SPARCLET ");
1205 if (flags & MASK_SPARCLITE)
1206 fprintf (stderr, "SPARCLITE ");
1207 if (flags & MASK_V8)
1208 fprintf (stderr, "V8 ");
1209 if (flags & MASK_V9)
1210 fprintf (stderr, "V9 ");
1211 }
1212
1213 static void
1214 dump_target_flags (const char *prefix, const int flags)
1215 {
1216 fprintf (stderr, "%s: (%08x) [ ", prefix, flags);
1217 dump_target_flag_bits (flags);
1218 fprintf(stderr, "]\n");
1219 }
1220
1221 /* Validate and override various options, and do some machine dependent
1222 initialization. */
1223
1224 static void
1225 sparc_option_override (void)
1226 {
1227 static struct code_model {
1228 const char *const name;
1229 const enum cmodel value;
1230 } const cmodels[] = {
1231 { "32", CM_32 },
1232 { "medlow", CM_MEDLOW },
1233 { "medmid", CM_MEDMID },
1234 { "medany", CM_MEDANY },
1235 { "embmedany", CM_EMBMEDANY },
1236 { NULL, (enum cmodel) 0 }
1237 };
1238 const struct code_model *cmodel;
1239 /* Map TARGET_CPU_DEFAULT to value for -m{cpu,tune}=. */
1240 static struct cpu_default {
1241 const int cpu;
1242 const enum processor_type processor;
1243 } const cpu_default[] = {
1244 /* There must be one entry here for each TARGET_CPU value. */
1245 { TARGET_CPU_sparc, PROCESSOR_CYPRESS },
1246 { TARGET_CPU_v8, PROCESSOR_V8 },
1247 { TARGET_CPU_supersparc, PROCESSOR_SUPERSPARC },
1248 { TARGET_CPU_hypersparc, PROCESSOR_HYPERSPARC },
1249 { TARGET_CPU_leon, PROCESSOR_LEON },
1250 { TARGET_CPU_leon3, PROCESSOR_LEON3 },
1251 { TARGET_CPU_leon3v7, PROCESSOR_LEON3V7 },
1252 { TARGET_CPU_sparclite, PROCESSOR_F930 },
1253 { TARGET_CPU_sparclite86x, PROCESSOR_SPARCLITE86X },
1254 { TARGET_CPU_sparclet, PROCESSOR_TSC701 },
1255 { TARGET_CPU_v9, PROCESSOR_V9 },
1256 { TARGET_CPU_ultrasparc, PROCESSOR_ULTRASPARC },
1257 { TARGET_CPU_ultrasparc3, PROCESSOR_ULTRASPARC3 },
1258 { TARGET_CPU_niagara, PROCESSOR_NIAGARA },
1259 { TARGET_CPU_niagara2, PROCESSOR_NIAGARA2 },
1260 { TARGET_CPU_niagara3, PROCESSOR_NIAGARA3 },
1261 { TARGET_CPU_niagara4, PROCESSOR_NIAGARA4 },
1262 { -1, PROCESSOR_V7 }
1263 };
1264 const struct cpu_default *def;
1265 /* Table of values for -m{cpu,tune}=. This must match the order of
1266 the enum processor_type in sparc-opts.h. */
1267 static struct cpu_table {
1268 const char *const name;
1269 const int disable;
1270 const int enable;
1271 } const cpu_table[] = {
1272 { "v7", MASK_ISA, 0 },
1273 { "cypress", MASK_ISA, 0 },
1274 { "v8", MASK_ISA, MASK_V8 },
1275 /* TI TMS390Z55 supersparc */
1276 { "supersparc", MASK_ISA, MASK_V8 },
1277 { "hypersparc", MASK_ISA, MASK_V8|MASK_FPU },
1278 { "leon", MASK_ISA, MASK_V8|MASK_LEON|MASK_FPU },
1279 { "leon3", MASK_ISA, MASK_V8|MASK_LEON3|MASK_FPU },
1280 { "leon3v7", MASK_ISA, MASK_LEON3|MASK_FPU },
1281 { "sparclite", MASK_ISA, MASK_SPARCLITE },
1282 /* The Fujitsu MB86930 is the original sparclite chip, with no FPU. */
1283 { "f930", MASK_ISA|MASK_FPU, MASK_SPARCLITE },
1284 /* The Fujitsu MB86934 is the recent sparclite chip, with an FPU. */
1285 { "f934", MASK_ISA, MASK_SPARCLITE|MASK_FPU },
1286 { "sparclite86x", MASK_ISA|MASK_FPU, MASK_SPARCLITE },
1287 { "sparclet", MASK_ISA, MASK_SPARCLET },
1288 /* TEMIC sparclet */
1289 { "tsc701", MASK_ISA, MASK_SPARCLET },
1290 { "v9", MASK_ISA, MASK_V9 },
1291 /* UltraSPARC I, II, IIi */
1292 { "ultrasparc", MASK_ISA,
1293 /* Although insns using %y are deprecated, it is a clear win. */
1294 MASK_V9|MASK_DEPRECATED_V8_INSNS },
1295 /* UltraSPARC III */
1296 /* ??? Check if %y issue still holds true. */
1297 { "ultrasparc3", MASK_ISA,
1298 MASK_V9|MASK_DEPRECATED_V8_INSNS|MASK_VIS2 },
1299 /* UltraSPARC T1 */
1300 { "niagara", MASK_ISA,
1301 MASK_V9|MASK_DEPRECATED_V8_INSNS },
1302 /* UltraSPARC T2 */
1303 { "niagara2", MASK_ISA,
1304 MASK_V9|MASK_POPC|MASK_VIS2 },
1305 /* UltraSPARC T3 */
1306 { "niagara3", MASK_ISA,
1307 MASK_V9|MASK_POPC|MASK_VIS2|MASK_VIS3|MASK_FMAF },
1308 /* UltraSPARC T4 */
1309 { "niagara4", MASK_ISA,
1310 MASK_V9|MASK_POPC|MASK_VIS2|MASK_VIS3|MASK_FMAF|MASK_CBCOND },
1311 };
1312 const struct cpu_table *cpu;
1313 unsigned int i;
1314 int fpu;
1315
1316 if (sparc_debug_string != NULL)
1317 {
1318 const char *q;
1319 char *p;
1320
1321 p = ASTRDUP (sparc_debug_string);
1322 while ((q = strtok (p, ",")) != NULL)
1323 {
1324 bool invert;
1325 int mask;
1326
1327 p = NULL;
1328 if (*q == '!')
1329 {
1330 invert = true;
1331 q++;
1332 }
1333 else
1334 invert = false;
1335
1336 if (! strcmp (q, "all"))
1337 mask = MASK_DEBUG_ALL;
1338 else if (! strcmp (q, "options"))
1339 mask = MASK_DEBUG_OPTIONS;
1340 else
1341 error ("unknown -mdebug-%s switch", q);
1342
1343 if (invert)
1344 sparc_debug &= ~mask;
1345 else
1346 sparc_debug |= mask;
1347 }
1348 }
1349
1350 if (TARGET_DEBUG_OPTIONS)
1351 {
1352 dump_target_flags("Initial target_flags", target_flags);
1353 dump_target_flags("target_flags_explicit", target_flags_explicit);
1354 }
1355
1356 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1357 SUBTARGET_OVERRIDE_OPTIONS;
1358 #endif
1359
1360 #ifndef SPARC_BI_ARCH
1361 /* Check for unsupported architecture size. */
1362 if (! TARGET_64BIT != DEFAULT_ARCH32_P)
1363 error ("%s is not supported by this configuration",
1364 DEFAULT_ARCH32_P ? "-m64" : "-m32");
1365 #endif
1366
1367 /* We force all 64bit archs to use 128 bit long double */
1368 if (TARGET_64BIT && ! TARGET_LONG_DOUBLE_128)
1369 {
1370 error ("-mlong-double-64 not allowed with -m64");
1371 target_flags |= MASK_LONG_DOUBLE_128;
1372 }
1373
1374 /* Code model selection. */
1375 sparc_cmodel = SPARC_DEFAULT_CMODEL;
1376
1377 #ifdef SPARC_BI_ARCH
1378 if (TARGET_ARCH32)
1379 sparc_cmodel = CM_32;
1380 #endif
1381
1382 if (sparc_cmodel_string != NULL)
1383 {
1384 if (TARGET_ARCH64)
1385 {
1386 for (cmodel = &cmodels[0]; cmodel->name; cmodel++)
1387 if (strcmp (sparc_cmodel_string, cmodel->name) == 0)
1388 break;
1389 if (cmodel->name == NULL)
1390 error ("bad value (%s) for -mcmodel= switch", sparc_cmodel_string);
1391 else
1392 sparc_cmodel = cmodel->value;
1393 }
1394 else
1395 error ("-mcmodel= is not supported on 32 bit systems");
1396 }
1397
1398 /* Check that -fcall-saved-REG wasn't specified for out registers. */
1399 for (i = 8; i < 16; i++)
1400 if (!call_used_regs [i])
1401 {
1402 error ("-fcall-saved-REG is not supported for out registers");
1403 call_used_regs [i] = 1;
1404 }
1405
1406 fpu = target_flags & MASK_FPU; /* save current -mfpu status */
1407
1408 /* Set the default CPU. */
1409 if (!global_options_set.x_sparc_cpu_and_features)
1410 {
1411 for (def = &cpu_default[0]; def->cpu != -1; ++def)
1412 if (def->cpu == TARGET_CPU_DEFAULT)
1413 break;
1414 gcc_assert (def->cpu != -1);
1415 sparc_cpu_and_features = def->processor;
1416 }
1417
1418 if (!global_options_set.x_sparc_cpu)
1419 sparc_cpu = sparc_cpu_and_features;
1420
1421 cpu = &cpu_table[(int) sparc_cpu_and_features];
1422
1423 if (TARGET_DEBUG_OPTIONS)
1424 {
1425 fprintf (stderr, "sparc_cpu_and_features: %s\n", cpu->name);
1426 fprintf (stderr, "sparc_cpu: %s\n",
1427 cpu_table[(int) sparc_cpu].name);
1428 dump_target_flags ("cpu->disable", cpu->disable);
1429 dump_target_flags ("cpu->enable", cpu->enable);
1430 }
1431
1432 target_flags &= ~cpu->disable;
1433 target_flags |= (cpu->enable
1434 #ifndef HAVE_AS_FMAF_HPC_VIS3
1435 & ~(MASK_FMAF | MASK_VIS3)
1436 #endif
1437 #ifndef HAVE_AS_SPARC4
1438 & ~MASK_CBCOND
1439 #endif
1440 #ifndef HAVE_AS_LEON
1441 & ~(MASK_LEON | MASK_LEON3)
1442 #endif
1443 );
1444
1445 /* If -mfpu or -mno-fpu was explicitly used, don't override with
1446 the processor default. */
1447 if (target_flags_explicit & MASK_FPU)
1448 target_flags = (target_flags & ~MASK_FPU) | fpu;
1449
1450 /* -mvis2 implies -mvis */
1451 if (TARGET_VIS2)
1452 target_flags |= MASK_VIS;
1453
1454 /* -mvis3 implies -mvis2 and -mvis */
1455 if (TARGET_VIS3)
1456 target_flags |= MASK_VIS2 | MASK_VIS;
1457
1458 /* Don't allow -mvis, -mvis2, -mvis3, or -mfmaf if FPU is
1459 disabled. */
1460 if (! TARGET_FPU)
1461 target_flags &= ~(MASK_VIS | MASK_VIS2 | MASK_VIS3 | MASK_FMAF);
1462
1463 /* -mvis assumes UltraSPARC+, so we are sure v9 instructions
1464 are available.
1465 -m64 also implies v9. */
1466 if (TARGET_VIS || TARGET_ARCH64)
1467 {
1468 target_flags |= MASK_V9;
1469 target_flags &= ~(MASK_V8 | MASK_SPARCLET | MASK_SPARCLITE);
1470 }
1471
1472 /* -mvis also implies -mv8plus on 32-bit */
1473 if (TARGET_VIS && ! TARGET_ARCH64)
1474 target_flags |= MASK_V8PLUS;
1475
1476 /* Use the deprecated v8 insns for sparc64 in 32 bit mode. */
1477 if (TARGET_V9 && TARGET_ARCH32)
1478 target_flags |= MASK_DEPRECATED_V8_INSNS;
1479
1480 /* V8PLUS requires V9, makes no sense in 64 bit mode. */
1481 if (! TARGET_V9 || TARGET_ARCH64)
1482 target_flags &= ~MASK_V8PLUS;
1483
1484 /* Don't use stack biasing in 32 bit mode. */
1485 if (TARGET_ARCH32)
1486 target_flags &= ~MASK_STACK_BIAS;
1487
1488 /* Supply a default value for align_functions. */
1489 if (align_functions == 0
1490 && (sparc_cpu == PROCESSOR_ULTRASPARC
1491 || sparc_cpu == PROCESSOR_ULTRASPARC3
1492 || sparc_cpu == PROCESSOR_NIAGARA
1493 || sparc_cpu == PROCESSOR_NIAGARA2
1494 || sparc_cpu == PROCESSOR_NIAGARA3
1495 || sparc_cpu == PROCESSOR_NIAGARA4))
1496 align_functions = 32;
1497
1498 /* Validate PCC_STRUCT_RETURN. */
1499 if (flag_pcc_struct_return == DEFAULT_PCC_STRUCT_RETURN)
1500 flag_pcc_struct_return = (TARGET_ARCH64 ? 0 : 1);
1501
1502 /* Only use .uaxword when compiling for a 64-bit target. */
1503 if (!TARGET_ARCH64)
1504 targetm.asm_out.unaligned_op.di = NULL;
1505
1506 /* Do various machine dependent initializations. */
1507 sparc_init_modes ();
1508
1509 /* Set up function hooks. */
1510 init_machine_status = sparc_init_machine_status;
1511
1512 switch (sparc_cpu)
1513 {
1514 case PROCESSOR_V7:
1515 case PROCESSOR_CYPRESS:
1516 sparc_costs = &cypress_costs;
1517 break;
1518 case PROCESSOR_V8:
1519 case PROCESSOR_SPARCLITE:
1520 case PROCESSOR_SUPERSPARC:
1521 sparc_costs = &supersparc_costs;
1522 break;
1523 case PROCESSOR_F930:
1524 case PROCESSOR_F934:
1525 case PROCESSOR_HYPERSPARC:
1526 case PROCESSOR_SPARCLITE86X:
1527 sparc_costs = &hypersparc_costs;
1528 break;
1529 case PROCESSOR_LEON:
1530 sparc_costs = &leon_costs;
1531 break;
1532 case PROCESSOR_LEON3:
1533 case PROCESSOR_LEON3V7:
1534 sparc_costs = &leon3_costs;
1535 break;
1536 case PROCESSOR_SPARCLET:
1537 case PROCESSOR_TSC701:
1538 sparc_costs = &sparclet_costs;
1539 break;
1540 case PROCESSOR_V9:
1541 case PROCESSOR_ULTRASPARC:
1542 sparc_costs = &ultrasparc_costs;
1543 break;
1544 case PROCESSOR_ULTRASPARC3:
1545 sparc_costs = &ultrasparc3_costs;
1546 break;
1547 case PROCESSOR_NIAGARA:
1548 sparc_costs = &niagara_costs;
1549 break;
1550 case PROCESSOR_NIAGARA2:
1551 sparc_costs = &niagara2_costs;
1552 break;
1553 case PROCESSOR_NIAGARA3:
1554 sparc_costs = &niagara3_costs;
1555 break;
1556 case PROCESSOR_NIAGARA4:
1557 sparc_costs = &niagara4_costs;
1558 break;
1559 case PROCESSOR_NATIVE:
1560 gcc_unreachable ();
1561 };
1562
1563 if (sparc_memory_model == SMM_DEFAULT)
1564 {
1565 /* Choose the memory model for the operating system. */
1566 enum sparc_memory_model_type os_default = SUBTARGET_DEFAULT_MEMORY_MODEL;
1567 if (os_default != SMM_DEFAULT)
1568 sparc_memory_model = os_default;
1569 /* Choose the most relaxed model for the processor. */
1570 else if (TARGET_V9)
1571 sparc_memory_model = SMM_RMO;
1572 else if (TARGET_LEON3)
1573 sparc_memory_model = SMM_TSO;
1574 else if (TARGET_LEON)
1575 sparc_memory_model = SMM_SC;
1576 else if (TARGET_V8)
1577 sparc_memory_model = SMM_PSO;
1578 else
1579 sparc_memory_model = SMM_SC;
1580 }
1581
1582 #ifdef TARGET_DEFAULT_LONG_DOUBLE_128
1583 if (!(target_flags_explicit & MASK_LONG_DOUBLE_128))
1584 target_flags |= MASK_LONG_DOUBLE_128;
1585 #endif
1586
1587 if (TARGET_DEBUG_OPTIONS)
1588 dump_target_flags ("Final target_flags", target_flags);
1589
1590 maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
1591 ((sparc_cpu == PROCESSOR_ULTRASPARC
1592 || sparc_cpu == PROCESSOR_NIAGARA
1593 || sparc_cpu == PROCESSOR_NIAGARA2
1594 || sparc_cpu == PROCESSOR_NIAGARA3
1595 || sparc_cpu == PROCESSOR_NIAGARA4)
1596 ? 2
1597 : (sparc_cpu == PROCESSOR_ULTRASPARC3
1598 ? 8 : 3)),
1599 global_options.x_param_values,
1600 global_options_set.x_param_values);
1601 maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE,
1602 ((sparc_cpu == PROCESSOR_ULTRASPARC
1603 || sparc_cpu == PROCESSOR_ULTRASPARC3
1604 || sparc_cpu == PROCESSOR_NIAGARA
1605 || sparc_cpu == PROCESSOR_NIAGARA2
1606 || sparc_cpu == PROCESSOR_NIAGARA3
1607 || sparc_cpu == PROCESSOR_NIAGARA4)
1608 ? 64 : 32),
1609 global_options.x_param_values,
1610 global_options_set.x_param_values);
1611
1612 /* Disable save slot sharing for call-clobbered registers by default.
1613 The IRA sharing algorithm works on single registers only and this
1614 pessimizes for double floating-point registers. */
1615 if (!global_options_set.x_flag_ira_share_save_slots)
1616 flag_ira_share_save_slots = 0;
1617
1618 /* We register a machine specific pass to work around errata, if any.
1619 The pass mut be scheduled as late as possible so that we have the
1620 (essentially) final form of the insn stream to work on.
1621 Registering the pass must be done at start up. It's convenient to
1622 do it here. */
1623 opt_pass *errata_pass = make_pass_work_around_errata (g);
1624 struct register_pass_info insert_pass_work_around_errata =
1625 {
1626 errata_pass, /* pass */
1627 "dbr", /* reference_pass_name */
1628 1, /* ref_pass_instance_number */
1629 PASS_POS_INSERT_AFTER /* po_op */
1630 };
1631 register_pass (&insert_pass_work_around_errata);
1632 }
1633 \f
1634 /* Miscellaneous utilities. */
1635
1636 /* Nonzero if CODE, a comparison, is suitable for use in v9 conditional move
1637 or branch on register contents instructions. */
1638
1639 int
1640 v9_regcmp_p (enum rtx_code code)
1641 {
1642 return (code == EQ || code == NE || code == GE || code == LT
1643 || code == LE || code == GT);
1644 }
1645
1646 /* Nonzero if OP is a floating point constant which can
1647 be loaded into an integer register using a single
1648 sethi instruction. */
1649
1650 int
1651 fp_sethi_p (rtx op)
1652 {
1653 if (GET_CODE (op) == CONST_DOUBLE)
1654 {
1655 REAL_VALUE_TYPE r;
1656 long i;
1657
1658 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
1659 REAL_VALUE_TO_TARGET_SINGLE (r, i);
1660 return !SPARC_SIMM13_P (i) && SPARC_SETHI_P (i);
1661 }
1662
1663 return 0;
1664 }
1665
1666 /* Nonzero if OP is a floating point constant which can
1667 be loaded into an integer register using a single
1668 mov instruction. */
1669
1670 int
1671 fp_mov_p (rtx op)
1672 {
1673 if (GET_CODE (op) == CONST_DOUBLE)
1674 {
1675 REAL_VALUE_TYPE r;
1676 long i;
1677
1678 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
1679 REAL_VALUE_TO_TARGET_SINGLE (r, i);
1680 return SPARC_SIMM13_P (i);
1681 }
1682
1683 return 0;
1684 }
1685
1686 /* Nonzero if OP is a floating point constant which can
1687 be loaded into an integer register using a high/losum
1688 instruction sequence. */
1689
1690 int
1691 fp_high_losum_p (rtx op)
1692 {
1693 /* The constraints calling this should only be in
1694 SFmode move insns, so any constant which cannot
1695 be moved using a single insn will do. */
1696 if (GET_CODE (op) == CONST_DOUBLE)
1697 {
1698 REAL_VALUE_TYPE r;
1699 long i;
1700
1701 REAL_VALUE_FROM_CONST_DOUBLE (r, op);
1702 REAL_VALUE_TO_TARGET_SINGLE (r, i);
1703 return !SPARC_SIMM13_P (i) && !SPARC_SETHI_P (i);
1704 }
1705
1706 return 0;
1707 }
1708
1709 /* Return true if the address of LABEL can be loaded by means of the
1710 mov{si,di}_pic_label_ref patterns in PIC mode. */
1711
1712 static bool
1713 can_use_mov_pic_label_ref (rtx label)
1714 {
1715 /* VxWorks does not impose a fixed gap between segments; the run-time
1716 gap can be different from the object-file gap. We therefore can't
1717 assume X - _GLOBAL_OFFSET_TABLE_ is a link-time constant unless we
1718 are absolutely sure that X is in the same segment as the GOT.
1719 Unfortunately, the flexibility of linker scripts means that we
1720 can't be sure of that in general, so assume that GOT-relative
1721 accesses are never valid on VxWorks. */
1722 if (TARGET_VXWORKS_RTP)
1723 return false;
1724
1725 /* Similarly, if the label is non-local, it might end up being placed
1726 in a different section than the current one; now mov_pic_label_ref
1727 requires the label and the code to be in the same section. */
1728 if (LABEL_REF_NONLOCAL_P (label))
1729 return false;
1730
1731 /* Finally, if we are reordering basic blocks and partition into hot
1732 and cold sections, this might happen for any label. */
1733 if (flag_reorder_blocks_and_partition)
1734 return false;
1735
1736 return true;
1737 }
1738
1739 /* Expand a move instruction. Return true if all work is done. */
1740
1741 bool
1742 sparc_expand_move (enum machine_mode mode, rtx *operands)
1743 {
1744 /* Handle sets of MEM first. */
1745 if (GET_CODE (operands[0]) == MEM)
1746 {
1747 /* 0 is a register (or a pair of registers) on SPARC. */
1748 if (register_or_zero_operand (operands[1], mode))
1749 return false;
1750
1751 if (!reload_in_progress)
1752 {
1753 operands[0] = validize_mem (operands[0]);
1754 operands[1] = force_reg (mode, operands[1]);
1755 }
1756 }
1757
1758 /* Fixup TLS cases. */
1759 if (TARGET_HAVE_TLS
1760 && CONSTANT_P (operands[1])
1761 && sparc_tls_referenced_p (operands [1]))
1762 {
1763 operands[1] = sparc_legitimize_tls_address (operands[1]);
1764 return false;
1765 }
1766
1767 /* Fixup PIC cases. */
1768 if (flag_pic && CONSTANT_P (operands[1]))
1769 {
1770 if (pic_address_needs_scratch (operands[1]))
1771 operands[1] = sparc_legitimize_pic_address (operands[1], NULL_RTX);
1772
1773 /* We cannot use the mov{si,di}_pic_label_ref patterns in all cases. */
1774 if (GET_CODE (operands[1]) == LABEL_REF
1775 && can_use_mov_pic_label_ref (operands[1]))
1776 {
1777 if (mode == SImode)
1778 {
1779 emit_insn (gen_movsi_pic_label_ref (operands[0], operands[1]));
1780 return true;
1781 }
1782
1783 if (mode == DImode)
1784 {
1785 gcc_assert (TARGET_ARCH64);
1786 emit_insn (gen_movdi_pic_label_ref (operands[0], operands[1]));
1787 return true;
1788 }
1789 }
1790
1791 if (symbolic_operand (operands[1], mode))
1792 {
1793 operands[1]
1794 = sparc_legitimize_pic_address (operands[1],
1795 reload_in_progress
1796 ? operands[0] : NULL_RTX);
1797 return false;
1798 }
1799 }
1800
1801 /* If we are trying to toss an integer constant into FP registers,
1802 or loading a FP or vector constant, force it into memory. */
1803 if (CONSTANT_P (operands[1])
1804 && REG_P (operands[0])
1805 && (SPARC_FP_REG_P (REGNO (operands[0]))
1806 || SCALAR_FLOAT_MODE_P (mode)
1807 || VECTOR_MODE_P (mode)))
1808 {
1809 /* emit_group_store will send such bogosity to us when it is
1810 not storing directly into memory. So fix this up to avoid
1811 crashes in output_constant_pool. */
1812 if (operands [1] == const0_rtx)
1813 operands[1] = CONST0_RTX (mode);
1814
1815 /* We can clear or set to all-ones FP registers if TARGET_VIS, and
1816 always other regs. */
1817 if ((TARGET_VIS || REGNO (operands[0]) < SPARC_FIRST_FP_REG)
1818 && (const_zero_operand (operands[1], mode)
1819 || const_all_ones_operand (operands[1], mode)))
1820 return false;
1821
1822 if (REGNO (operands[0]) < SPARC_FIRST_FP_REG
1823 /* We are able to build any SF constant in integer registers
1824 with at most 2 instructions. */
1825 && (mode == SFmode
1826 /* And any DF constant in integer registers. */
1827 || (mode == DFmode
1828 && ! can_create_pseudo_p ())))
1829 return false;
1830
1831 operands[1] = force_const_mem (mode, operands[1]);
1832 if (!reload_in_progress)
1833 operands[1] = validize_mem (operands[1]);
1834 return false;
1835 }
1836
1837 /* Accept non-constants and valid constants unmodified. */
1838 if (!CONSTANT_P (operands[1])
1839 || GET_CODE (operands[1]) == HIGH
1840 || input_operand (operands[1], mode))
1841 return false;
1842
1843 switch (mode)
1844 {
1845 case QImode:
1846 /* All QImode constants require only one insn, so proceed. */
1847 break;
1848
1849 case HImode:
1850 case SImode:
1851 sparc_emit_set_const32 (operands[0], operands[1]);
1852 return true;
1853
1854 case DImode:
1855 /* input_operand should have filtered out 32-bit mode. */
1856 sparc_emit_set_const64 (operands[0], operands[1]);
1857 return true;
1858
1859 case TImode:
1860 {
1861 rtx high, low;
1862 /* TImode isn't available in 32-bit mode. */
1863 split_double (operands[1], &high, &low);
1864 emit_insn (gen_movdi (operand_subword (operands[0], 0, 0, TImode),
1865 high));
1866 emit_insn (gen_movdi (operand_subword (operands[0], 1, 0, TImode),
1867 low));
1868 }
1869 return true;
1870
1871 default:
1872 gcc_unreachable ();
1873 }
1874
1875 return false;
1876 }
1877
1878 /* Load OP1, a 32-bit constant, into OP0, a register.
1879 We know it can't be done in one insn when we get
1880 here, the move expander guarantees this. */
1881
1882 static void
1883 sparc_emit_set_const32 (rtx op0, rtx op1)
1884 {
1885 enum machine_mode mode = GET_MODE (op0);
1886 rtx temp = op0;
1887
1888 if (can_create_pseudo_p ())
1889 temp = gen_reg_rtx (mode);
1890
1891 if (GET_CODE (op1) == CONST_INT)
1892 {
1893 gcc_assert (!small_int_operand (op1, mode)
1894 && !const_high_operand (op1, mode));
1895
1896 /* Emit them as real moves instead of a HIGH/LO_SUM,
1897 this way CSE can see everything and reuse intermediate
1898 values if it wants. */
1899 emit_insn (gen_rtx_SET (VOIDmode, temp,
1900 GEN_INT (INTVAL (op1)
1901 & ~(HOST_WIDE_INT)0x3ff)));
1902
1903 emit_insn (gen_rtx_SET (VOIDmode,
1904 op0,
1905 gen_rtx_IOR (mode, temp,
1906 GEN_INT (INTVAL (op1) & 0x3ff))));
1907 }
1908 else
1909 {
1910 /* A symbol, emit in the traditional way. */
1911 emit_insn (gen_rtx_SET (VOIDmode, temp,
1912 gen_rtx_HIGH (mode, op1)));
1913 emit_insn (gen_rtx_SET (VOIDmode,
1914 op0, gen_rtx_LO_SUM (mode, temp, op1)));
1915 }
1916 }
1917
1918 /* Load OP1, a symbolic 64-bit constant, into OP0, a DImode register.
1919 If TEMP is nonzero, we are forbidden to use any other scratch
1920 registers. Otherwise, we are allowed to generate them as needed.
1921
1922 Note that TEMP may have TImode if the code model is TARGET_CM_MEDANY
1923 or TARGET_CM_EMBMEDANY (see the reload_indi and reload_outdi patterns). */
1924
1925 void
1926 sparc_emit_set_symbolic_const64 (rtx op0, rtx op1, rtx temp)
1927 {
1928 rtx temp1, temp2, temp3, temp4, temp5;
1929 rtx ti_temp = 0;
1930
1931 if (temp && GET_MODE (temp) == TImode)
1932 {
1933 ti_temp = temp;
1934 temp = gen_rtx_REG (DImode, REGNO (temp));
1935 }
1936
1937 /* SPARC-V9 code-model support. */
1938 switch (sparc_cmodel)
1939 {
1940 case CM_MEDLOW:
1941 /* The range spanned by all instructions in the object is less
1942 than 2^31 bytes (2GB) and the distance from any instruction
1943 to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1944 than 2^31 bytes (2GB).
1945
1946 The executable must be in the low 4TB of the virtual address
1947 space.
1948
1949 sethi %hi(symbol), %temp1
1950 or %temp1, %lo(symbol), %reg */
1951 if (temp)
1952 temp1 = temp; /* op0 is allowed. */
1953 else
1954 temp1 = gen_reg_rtx (DImode);
1955
1956 emit_insn (gen_rtx_SET (VOIDmode, temp1, gen_rtx_HIGH (DImode, op1)));
1957 emit_insn (gen_rtx_SET (VOIDmode, op0, gen_rtx_LO_SUM (DImode, temp1, op1)));
1958 break;
1959
1960 case CM_MEDMID:
1961 /* The range spanned by all instructions in the object is less
1962 than 2^31 bytes (2GB) and the distance from any instruction
1963 to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1964 than 2^31 bytes (2GB).
1965
1966 The executable must be in the low 16TB of the virtual address
1967 space.
1968
1969 sethi %h44(symbol), %temp1
1970 or %temp1, %m44(symbol), %temp2
1971 sllx %temp2, 12, %temp3
1972 or %temp3, %l44(symbol), %reg */
1973 if (temp)
1974 {
1975 temp1 = op0;
1976 temp2 = op0;
1977 temp3 = temp; /* op0 is allowed. */
1978 }
1979 else
1980 {
1981 temp1 = gen_reg_rtx (DImode);
1982 temp2 = gen_reg_rtx (DImode);
1983 temp3 = gen_reg_rtx (DImode);
1984 }
1985
1986 emit_insn (gen_seth44 (temp1, op1));
1987 emit_insn (gen_setm44 (temp2, temp1, op1));
1988 emit_insn (gen_rtx_SET (VOIDmode, temp3,
1989 gen_rtx_ASHIFT (DImode, temp2, GEN_INT (12))));
1990 emit_insn (gen_setl44 (op0, temp3, op1));
1991 break;
1992
1993 case CM_MEDANY:
1994 /* The range spanned by all instructions in the object is less
1995 than 2^31 bytes (2GB) and the distance from any instruction
1996 to the location of the label _GLOBAL_OFFSET_TABLE_ is less
1997 than 2^31 bytes (2GB).
1998
1999 The executable can be placed anywhere in the virtual address
2000 space.
2001
2002 sethi %hh(symbol), %temp1
2003 sethi %lm(symbol), %temp2
2004 or %temp1, %hm(symbol), %temp3
2005 sllx %temp3, 32, %temp4
2006 or %temp4, %temp2, %temp5
2007 or %temp5, %lo(symbol), %reg */
2008 if (temp)
2009 {
2010 /* It is possible that one of the registers we got for operands[2]
2011 might coincide with that of operands[0] (which is why we made
2012 it TImode). Pick the other one to use as our scratch. */
2013 if (rtx_equal_p (temp, op0))
2014 {
2015 gcc_assert (ti_temp);
2016 temp = gen_rtx_REG (DImode, REGNO (temp) + 1);
2017 }
2018 temp1 = op0;
2019 temp2 = temp; /* op0 is _not_ allowed, see above. */
2020 temp3 = op0;
2021 temp4 = op0;
2022 temp5 = op0;
2023 }
2024 else
2025 {
2026 temp1 = gen_reg_rtx (DImode);
2027 temp2 = gen_reg_rtx (DImode);
2028 temp3 = gen_reg_rtx (DImode);
2029 temp4 = gen_reg_rtx (DImode);
2030 temp5 = gen_reg_rtx (DImode);
2031 }
2032
2033 emit_insn (gen_sethh (temp1, op1));
2034 emit_insn (gen_setlm (temp2, op1));
2035 emit_insn (gen_sethm (temp3, temp1, op1));
2036 emit_insn (gen_rtx_SET (VOIDmode, temp4,
2037 gen_rtx_ASHIFT (DImode, temp3, GEN_INT (32))));
2038 emit_insn (gen_rtx_SET (VOIDmode, temp5,
2039 gen_rtx_PLUS (DImode, temp4, temp2)));
2040 emit_insn (gen_setlo (op0, temp5, op1));
2041 break;
2042
2043 case CM_EMBMEDANY:
2044 /* Old old old backwards compatibility kruft here.
2045 Essentially it is MEDLOW with a fixed 64-bit
2046 virtual base added to all data segment addresses.
2047 Text-segment stuff is computed like MEDANY, we can't
2048 reuse the code above because the relocation knobs
2049 look different.
2050
2051 Data segment: sethi %hi(symbol), %temp1
2052 add %temp1, EMBMEDANY_BASE_REG, %temp2
2053 or %temp2, %lo(symbol), %reg */
2054 if (data_segment_operand (op1, GET_MODE (op1)))
2055 {
2056 if (temp)
2057 {
2058 temp1 = temp; /* op0 is allowed. */
2059 temp2 = op0;
2060 }
2061 else
2062 {
2063 temp1 = gen_reg_rtx (DImode);
2064 temp2 = gen_reg_rtx (DImode);
2065 }
2066
2067 emit_insn (gen_embmedany_sethi (temp1, op1));
2068 emit_insn (gen_embmedany_brsum (temp2, temp1));
2069 emit_insn (gen_embmedany_losum (op0, temp2, op1));
2070 }
2071
2072 /* Text segment: sethi %uhi(symbol), %temp1
2073 sethi %hi(symbol), %temp2
2074 or %temp1, %ulo(symbol), %temp3
2075 sllx %temp3, 32, %temp4
2076 or %temp4, %temp2, %temp5
2077 or %temp5, %lo(symbol), %reg */
2078 else
2079 {
2080 if (temp)
2081 {
2082 /* It is possible that one of the registers we got for operands[2]
2083 might coincide with that of operands[0] (which is why we made
2084 it TImode). Pick the other one to use as our scratch. */
2085 if (rtx_equal_p (temp, op0))
2086 {
2087 gcc_assert (ti_temp);
2088 temp = gen_rtx_REG (DImode, REGNO (temp) + 1);
2089 }
2090 temp1 = op0;
2091 temp2 = temp; /* op0 is _not_ allowed, see above. */
2092 temp3 = op0;
2093 temp4 = op0;
2094 temp5 = op0;
2095 }
2096 else
2097 {
2098 temp1 = gen_reg_rtx (DImode);
2099 temp2 = gen_reg_rtx (DImode);
2100 temp3 = gen_reg_rtx (DImode);
2101 temp4 = gen_reg_rtx (DImode);
2102 temp5 = gen_reg_rtx (DImode);
2103 }
2104
2105 emit_insn (gen_embmedany_textuhi (temp1, op1));
2106 emit_insn (gen_embmedany_texthi (temp2, op1));
2107 emit_insn (gen_embmedany_textulo (temp3, temp1, op1));
2108 emit_insn (gen_rtx_SET (VOIDmode, temp4,
2109 gen_rtx_ASHIFT (DImode, temp3, GEN_INT (32))));
2110 emit_insn (gen_rtx_SET (VOIDmode, temp5,
2111 gen_rtx_PLUS (DImode, temp4, temp2)));
2112 emit_insn (gen_embmedany_textlo (op0, temp5, op1));
2113 }
2114 break;
2115
2116 default:
2117 gcc_unreachable ();
2118 }
2119 }
2120
2121 #if HOST_BITS_PER_WIDE_INT == 32
2122 static void
2123 sparc_emit_set_const64 (rtx op0 ATTRIBUTE_UNUSED, rtx op1 ATTRIBUTE_UNUSED)
2124 {
2125 gcc_unreachable ();
2126 }
2127 #else
2128 /* These avoid problems when cross compiling. If we do not
2129 go through all this hair then the optimizer will see
2130 invalid REG_EQUAL notes or in some cases none at all. */
2131 static rtx gen_safe_HIGH64 (rtx, HOST_WIDE_INT);
2132 static rtx gen_safe_SET64 (rtx, HOST_WIDE_INT);
2133 static rtx gen_safe_OR64 (rtx, HOST_WIDE_INT);
2134 static rtx gen_safe_XOR64 (rtx, HOST_WIDE_INT);
2135
2136 /* The optimizer is not to assume anything about exactly
2137 which bits are set for a HIGH, they are unspecified.
2138 Unfortunately this leads to many missed optimizations
2139 during CSE. We mask out the non-HIGH bits, and matches
2140 a plain movdi, to alleviate this problem. */
2141 static rtx
2142 gen_safe_HIGH64 (rtx dest, HOST_WIDE_INT val)
2143 {
2144 return gen_rtx_SET (VOIDmode, dest, GEN_INT (val & ~(HOST_WIDE_INT)0x3ff));
2145 }
2146
2147 static rtx
2148 gen_safe_SET64 (rtx dest, HOST_WIDE_INT val)
2149 {
2150 return gen_rtx_SET (VOIDmode, dest, GEN_INT (val));
2151 }
2152
2153 static rtx
2154 gen_safe_OR64 (rtx src, HOST_WIDE_INT val)
2155 {
2156 return gen_rtx_IOR (DImode, src, GEN_INT (val));
2157 }
2158
2159 static rtx
2160 gen_safe_XOR64 (rtx src, HOST_WIDE_INT val)
2161 {
2162 return gen_rtx_XOR (DImode, src, GEN_INT (val));
2163 }
2164
2165 /* Worker routines for 64-bit constant formation on arch64.
2166 One of the key things to be doing in these emissions is
2167 to create as many temp REGs as possible. This makes it
2168 possible for half-built constants to be used later when
2169 such values are similar to something required later on.
2170 Without doing this, the optimizer cannot see such
2171 opportunities. */
2172
2173 static void sparc_emit_set_const64_quick1 (rtx, rtx,
2174 unsigned HOST_WIDE_INT, int);
2175
2176 static void
2177 sparc_emit_set_const64_quick1 (rtx op0, rtx temp,
2178 unsigned HOST_WIDE_INT low_bits, int is_neg)
2179 {
2180 unsigned HOST_WIDE_INT high_bits;
2181
2182 if (is_neg)
2183 high_bits = (~low_bits) & 0xffffffff;
2184 else
2185 high_bits = low_bits;
2186
2187 emit_insn (gen_safe_HIGH64 (temp, high_bits));
2188 if (!is_neg)
2189 {
2190 emit_insn (gen_rtx_SET (VOIDmode, op0,
2191 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
2192 }
2193 else
2194 {
2195 /* If we are XOR'ing with -1, then we should emit a one's complement
2196 instead. This way the combiner will notice logical operations
2197 such as ANDN later on and substitute. */
2198 if ((low_bits & 0x3ff) == 0x3ff)
2199 {
2200 emit_insn (gen_rtx_SET (VOIDmode, op0,
2201 gen_rtx_NOT (DImode, temp)));
2202 }
2203 else
2204 {
2205 emit_insn (gen_rtx_SET (VOIDmode, op0,
2206 gen_safe_XOR64 (temp,
2207 (-(HOST_WIDE_INT)0x400
2208 | (low_bits & 0x3ff)))));
2209 }
2210 }
2211 }
2212
2213 static void sparc_emit_set_const64_quick2 (rtx, rtx, unsigned HOST_WIDE_INT,
2214 unsigned HOST_WIDE_INT, int);
2215
2216 static void
2217 sparc_emit_set_const64_quick2 (rtx op0, rtx temp,
2218 unsigned HOST_WIDE_INT high_bits,
2219 unsigned HOST_WIDE_INT low_immediate,
2220 int shift_count)
2221 {
2222 rtx temp2 = op0;
2223
2224 if ((high_bits & 0xfffffc00) != 0)
2225 {
2226 emit_insn (gen_safe_HIGH64 (temp, high_bits));
2227 if ((high_bits & ~0xfffffc00) != 0)
2228 emit_insn (gen_rtx_SET (VOIDmode, op0,
2229 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
2230 else
2231 temp2 = temp;
2232 }
2233 else
2234 {
2235 emit_insn (gen_safe_SET64 (temp, high_bits));
2236 temp2 = temp;
2237 }
2238
2239 /* Now shift it up into place. */
2240 emit_insn (gen_rtx_SET (VOIDmode, op0,
2241 gen_rtx_ASHIFT (DImode, temp2,
2242 GEN_INT (shift_count))));
2243
2244 /* If there is a low immediate part piece, finish up by
2245 putting that in as well. */
2246 if (low_immediate != 0)
2247 emit_insn (gen_rtx_SET (VOIDmode, op0,
2248 gen_safe_OR64 (op0, low_immediate)));
2249 }
2250
2251 static void sparc_emit_set_const64_longway (rtx, rtx, unsigned HOST_WIDE_INT,
2252 unsigned HOST_WIDE_INT);
2253
2254 /* Full 64-bit constant decomposition. Even though this is the
2255 'worst' case, we still optimize a few things away. */
2256 static void
2257 sparc_emit_set_const64_longway (rtx op0, rtx temp,
2258 unsigned HOST_WIDE_INT high_bits,
2259 unsigned HOST_WIDE_INT low_bits)
2260 {
2261 rtx sub_temp = op0;
2262
2263 if (can_create_pseudo_p ())
2264 sub_temp = gen_reg_rtx (DImode);
2265
2266 if ((high_bits & 0xfffffc00) != 0)
2267 {
2268 emit_insn (gen_safe_HIGH64 (temp, high_bits));
2269 if ((high_bits & ~0xfffffc00) != 0)
2270 emit_insn (gen_rtx_SET (VOIDmode,
2271 sub_temp,
2272 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
2273 else
2274 sub_temp = temp;
2275 }
2276 else
2277 {
2278 emit_insn (gen_safe_SET64 (temp, high_bits));
2279 sub_temp = temp;
2280 }
2281
2282 if (can_create_pseudo_p ())
2283 {
2284 rtx temp2 = gen_reg_rtx (DImode);
2285 rtx temp3 = gen_reg_rtx (DImode);
2286 rtx temp4 = gen_reg_rtx (DImode);
2287
2288 emit_insn (gen_rtx_SET (VOIDmode, temp4,
2289 gen_rtx_ASHIFT (DImode, sub_temp,
2290 GEN_INT (32))));
2291
2292 emit_insn (gen_safe_HIGH64 (temp2, low_bits));
2293 if ((low_bits & ~0xfffffc00) != 0)
2294 {
2295 emit_insn (gen_rtx_SET (VOIDmode, temp3,
2296 gen_safe_OR64 (temp2, (low_bits & 0x3ff))));
2297 emit_insn (gen_rtx_SET (VOIDmode, op0,
2298 gen_rtx_PLUS (DImode, temp4, temp3)));
2299 }
2300 else
2301 {
2302 emit_insn (gen_rtx_SET (VOIDmode, op0,
2303 gen_rtx_PLUS (DImode, temp4, temp2)));
2304 }
2305 }
2306 else
2307 {
2308 rtx low1 = GEN_INT ((low_bits >> (32 - 12)) & 0xfff);
2309 rtx low2 = GEN_INT ((low_bits >> (32 - 12 - 12)) & 0xfff);
2310 rtx low3 = GEN_INT ((low_bits >> (32 - 12 - 12 - 8)) & 0x0ff);
2311 int to_shift = 12;
2312
2313 /* We are in the middle of reload, so this is really
2314 painful. However we do still make an attempt to
2315 avoid emitting truly stupid code. */
2316 if (low1 != const0_rtx)
2317 {
2318 emit_insn (gen_rtx_SET (VOIDmode, op0,
2319 gen_rtx_ASHIFT (DImode, sub_temp,
2320 GEN_INT (to_shift))));
2321 emit_insn (gen_rtx_SET (VOIDmode, op0,
2322 gen_rtx_IOR (DImode, op0, low1)));
2323 sub_temp = op0;
2324 to_shift = 12;
2325 }
2326 else
2327 {
2328 to_shift += 12;
2329 }
2330 if (low2 != const0_rtx)
2331 {
2332 emit_insn (gen_rtx_SET (VOIDmode, op0,
2333 gen_rtx_ASHIFT (DImode, sub_temp,
2334 GEN_INT (to_shift))));
2335 emit_insn (gen_rtx_SET (VOIDmode, op0,
2336 gen_rtx_IOR (DImode, op0, low2)));
2337 sub_temp = op0;
2338 to_shift = 8;
2339 }
2340 else
2341 {
2342 to_shift += 8;
2343 }
2344 emit_insn (gen_rtx_SET (VOIDmode, op0,
2345 gen_rtx_ASHIFT (DImode, sub_temp,
2346 GEN_INT (to_shift))));
2347 if (low3 != const0_rtx)
2348 emit_insn (gen_rtx_SET (VOIDmode, op0,
2349 gen_rtx_IOR (DImode, op0, low3)));
2350 /* phew... */
2351 }
2352 }
2353
2354 /* Analyze a 64-bit constant for certain properties. */
2355 static void analyze_64bit_constant (unsigned HOST_WIDE_INT,
2356 unsigned HOST_WIDE_INT,
2357 int *, int *, int *);
2358
2359 static void
2360 analyze_64bit_constant (unsigned HOST_WIDE_INT high_bits,
2361 unsigned HOST_WIDE_INT low_bits,
2362 int *hbsp, int *lbsp, int *abbasp)
2363 {
2364 int lowest_bit_set, highest_bit_set, all_bits_between_are_set;
2365 int i;
2366
2367 lowest_bit_set = highest_bit_set = -1;
2368 i = 0;
2369 do
2370 {
2371 if ((lowest_bit_set == -1)
2372 && ((low_bits >> i) & 1))
2373 lowest_bit_set = i;
2374 if ((highest_bit_set == -1)
2375 && ((high_bits >> (32 - i - 1)) & 1))
2376 highest_bit_set = (64 - i - 1);
2377 }
2378 while (++i < 32
2379 && ((highest_bit_set == -1)
2380 || (lowest_bit_set == -1)));
2381 if (i == 32)
2382 {
2383 i = 0;
2384 do
2385 {
2386 if ((lowest_bit_set == -1)
2387 && ((high_bits >> i) & 1))
2388 lowest_bit_set = i + 32;
2389 if ((highest_bit_set == -1)
2390 && ((low_bits >> (32 - i - 1)) & 1))
2391 highest_bit_set = 32 - i - 1;
2392 }
2393 while (++i < 32
2394 && ((highest_bit_set == -1)
2395 || (lowest_bit_set == -1)));
2396 }
2397 /* If there are no bits set this should have gone out
2398 as one instruction! */
2399 gcc_assert (lowest_bit_set != -1 && highest_bit_set != -1);
2400 all_bits_between_are_set = 1;
2401 for (i = lowest_bit_set; i <= highest_bit_set; i++)
2402 {
2403 if (i < 32)
2404 {
2405 if ((low_bits & (1 << i)) != 0)
2406 continue;
2407 }
2408 else
2409 {
2410 if ((high_bits & (1 << (i - 32))) != 0)
2411 continue;
2412 }
2413 all_bits_between_are_set = 0;
2414 break;
2415 }
2416 *hbsp = highest_bit_set;
2417 *lbsp = lowest_bit_set;
2418 *abbasp = all_bits_between_are_set;
2419 }
2420
2421 static int const64_is_2insns (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2422
2423 static int
2424 const64_is_2insns (unsigned HOST_WIDE_INT high_bits,
2425 unsigned HOST_WIDE_INT low_bits)
2426 {
2427 int highest_bit_set, lowest_bit_set, all_bits_between_are_set;
2428
2429 if (high_bits == 0
2430 || high_bits == 0xffffffff)
2431 return 1;
2432
2433 analyze_64bit_constant (high_bits, low_bits,
2434 &highest_bit_set, &lowest_bit_set,
2435 &all_bits_between_are_set);
2436
2437 if ((highest_bit_set == 63
2438 || lowest_bit_set == 0)
2439 && all_bits_between_are_set != 0)
2440 return 1;
2441
2442 if ((highest_bit_set - lowest_bit_set) < 21)
2443 return 1;
2444
2445 return 0;
2446 }
2447
2448 static unsigned HOST_WIDE_INT create_simple_focus_bits (unsigned HOST_WIDE_INT,
2449 unsigned HOST_WIDE_INT,
2450 int, int);
2451
2452 static unsigned HOST_WIDE_INT
2453 create_simple_focus_bits (unsigned HOST_WIDE_INT high_bits,
2454 unsigned HOST_WIDE_INT low_bits,
2455 int lowest_bit_set, int shift)
2456 {
2457 HOST_WIDE_INT hi, lo;
2458
2459 if (lowest_bit_set < 32)
2460 {
2461 lo = (low_bits >> lowest_bit_set) << shift;
2462 hi = ((high_bits << (32 - lowest_bit_set)) << shift);
2463 }
2464 else
2465 {
2466 lo = 0;
2467 hi = ((high_bits >> (lowest_bit_set - 32)) << shift);
2468 }
2469 gcc_assert (! (hi & lo));
2470 return (hi | lo);
2471 }
2472
2473 /* Here we are sure to be arch64 and this is an integer constant
2474 being loaded into a register. Emit the most efficient
2475 insn sequence possible. Detection of all the 1-insn cases
2476 has been done already. */
2477 static void
2478 sparc_emit_set_const64 (rtx op0, rtx op1)
2479 {
2480 unsigned HOST_WIDE_INT high_bits, low_bits;
2481 int lowest_bit_set, highest_bit_set;
2482 int all_bits_between_are_set;
2483 rtx temp = 0;
2484
2485 /* Sanity check that we know what we are working with. */
2486 gcc_assert (TARGET_ARCH64
2487 && (GET_CODE (op0) == SUBREG
2488 || (REG_P (op0) && ! SPARC_FP_REG_P (REGNO (op0)))));
2489
2490 if (! can_create_pseudo_p ())
2491 temp = op0;
2492
2493 if (GET_CODE (op1) != CONST_INT)
2494 {
2495 sparc_emit_set_symbolic_const64 (op0, op1, temp);
2496 return;
2497 }
2498
2499 if (! temp)
2500 temp = gen_reg_rtx (DImode);
2501
2502 high_bits = ((INTVAL (op1) >> 32) & 0xffffffff);
2503 low_bits = (INTVAL (op1) & 0xffffffff);
2504
2505 /* low_bits bits 0 --> 31
2506 high_bits bits 32 --> 63 */
2507
2508 analyze_64bit_constant (high_bits, low_bits,
2509 &highest_bit_set, &lowest_bit_set,
2510 &all_bits_between_are_set);
2511
2512 /* First try for a 2-insn sequence. */
2513
2514 /* These situations are preferred because the optimizer can
2515 * do more things with them:
2516 * 1) mov -1, %reg
2517 * sllx %reg, shift, %reg
2518 * 2) mov -1, %reg
2519 * srlx %reg, shift, %reg
2520 * 3) mov some_small_const, %reg
2521 * sllx %reg, shift, %reg
2522 */
2523 if (((highest_bit_set == 63
2524 || lowest_bit_set == 0)
2525 && all_bits_between_are_set != 0)
2526 || ((highest_bit_set - lowest_bit_set) < 12))
2527 {
2528 HOST_WIDE_INT the_const = -1;
2529 int shift = lowest_bit_set;
2530
2531 if ((highest_bit_set != 63
2532 && lowest_bit_set != 0)
2533 || all_bits_between_are_set == 0)
2534 {
2535 the_const =
2536 create_simple_focus_bits (high_bits, low_bits,
2537 lowest_bit_set, 0);
2538 }
2539 else if (lowest_bit_set == 0)
2540 shift = -(63 - highest_bit_set);
2541
2542 gcc_assert (SPARC_SIMM13_P (the_const));
2543 gcc_assert (shift != 0);
2544
2545 emit_insn (gen_safe_SET64 (temp, the_const));
2546 if (shift > 0)
2547 emit_insn (gen_rtx_SET (VOIDmode,
2548 op0,
2549 gen_rtx_ASHIFT (DImode,
2550 temp,
2551 GEN_INT (shift))));
2552 else if (shift < 0)
2553 emit_insn (gen_rtx_SET (VOIDmode,
2554 op0,
2555 gen_rtx_LSHIFTRT (DImode,
2556 temp,
2557 GEN_INT (-shift))));
2558 return;
2559 }
2560
2561 /* Now a range of 22 or less bits set somewhere.
2562 * 1) sethi %hi(focus_bits), %reg
2563 * sllx %reg, shift, %reg
2564 * 2) sethi %hi(focus_bits), %reg
2565 * srlx %reg, shift, %reg
2566 */
2567 if ((highest_bit_set - lowest_bit_set) < 21)
2568 {
2569 unsigned HOST_WIDE_INT focus_bits =
2570 create_simple_focus_bits (high_bits, low_bits,
2571 lowest_bit_set, 10);
2572
2573 gcc_assert (SPARC_SETHI_P (focus_bits));
2574 gcc_assert (lowest_bit_set != 10);
2575
2576 emit_insn (gen_safe_HIGH64 (temp, focus_bits));
2577
2578 /* If lowest_bit_set == 10 then a sethi alone could have done it. */
2579 if (lowest_bit_set < 10)
2580 emit_insn (gen_rtx_SET (VOIDmode,
2581 op0,
2582 gen_rtx_LSHIFTRT (DImode, temp,
2583 GEN_INT (10 - lowest_bit_set))));
2584 else if (lowest_bit_set > 10)
2585 emit_insn (gen_rtx_SET (VOIDmode,
2586 op0,
2587 gen_rtx_ASHIFT (DImode, temp,
2588 GEN_INT (lowest_bit_set - 10))));
2589 return;
2590 }
2591
2592 /* 1) sethi %hi(low_bits), %reg
2593 * or %reg, %lo(low_bits), %reg
2594 * 2) sethi %hi(~low_bits), %reg
2595 * xor %reg, %lo(-0x400 | (low_bits & 0x3ff)), %reg
2596 */
2597 if (high_bits == 0
2598 || high_bits == 0xffffffff)
2599 {
2600 sparc_emit_set_const64_quick1 (op0, temp, low_bits,
2601 (high_bits == 0xffffffff));
2602 return;
2603 }
2604
2605 /* Now, try 3-insn sequences. */
2606
2607 /* 1) sethi %hi(high_bits), %reg
2608 * or %reg, %lo(high_bits), %reg
2609 * sllx %reg, 32, %reg
2610 */
2611 if (low_bits == 0)
2612 {
2613 sparc_emit_set_const64_quick2 (op0, temp, high_bits, 0, 32);
2614 return;
2615 }
2616
2617 /* We may be able to do something quick
2618 when the constant is negated, so try that. */
2619 if (const64_is_2insns ((~high_bits) & 0xffffffff,
2620 (~low_bits) & 0xfffffc00))
2621 {
2622 /* NOTE: The trailing bits get XOR'd so we need the
2623 non-negated bits, not the negated ones. */
2624 unsigned HOST_WIDE_INT trailing_bits = low_bits & 0x3ff;
2625
2626 if ((((~high_bits) & 0xffffffff) == 0
2627 && ((~low_bits) & 0x80000000) == 0)
2628 || (((~high_bits) & 0xffffffff) == 0xffffffff
2629 && ((~low_bits) & 0x80000000) != 0))
2630 {
2631 unsigned HOST_WIDE_INT fast_int = (~low_bits & 0xffffffff);
2632
2633 if ((SPARC_SETHI_P (fast_int)
2634 && (~high_bits & 0xffffffff) == 0)
2635 || SPARC_SIMM13_P (fast_int))
2636 emit_insn (gen_safe_SET64 (temp, fast_int));
2637 else
2638 sparc_emit_set_const64 (temp, GEN_INT (fast_int));
2639 }
2640 else
2641 {
2642 rtx negated_const;
2643 negated_const = GEN_INT (((~low_bits) & 0xfffffc00) |
2644 (((HOST_WIDE_INT)((~high_bits) & 0xffffffff))<<32));
2645 sparc_emit_set_const64 (temp, negated_const);
2646 }
2647
2648 /* If we are XOR'ing with -1, then we should emit a one's complement
2649 instead. This way the combiner will notice logical operations
2650 such as ANDN later on and substitute. */
2651 if (trailing_bits == 0x3ff)
2652 {
2653 emit_insn (gen_rtx_SET (VOIDmode, op0,
2654 gen_rtx_NOT (DImode, temp)));
2655 }
2656 else
2657 {
2658 emit_insn (gen_rtx_SET (VOIDmode,
2659 op0,
2660 gen_safe_XOR64 (temp,
2661 (-0x400 | trailing_bits))));
2662 }
2663 return;
2664 }
2665
2666 /* 1) sethi %hi(xxx), %reg
2667 * or %reg, %lo(xxx), %reg
2668 * sllx %reg, yyy, %reg
2669 *
2670 * ??? This is just a generalized version of the low_bits==0
2671 * thing above, FIXME...
2672 */
2673 if ((highest_bit_set - lowest_bit_set) < 32)
2674 {
2675 unsigned HOST_WIDE_INT focus_bits =
2676 create_simple_focus_bits (high_bits, low_bits,
2677 lowest_bit_set, 0);
2678
2679 /* We can't get here in this state. */
2680 gcc_assert (highest_bit_set >= 32 && lowest_bit_set < 32);
2681
2682 /* So what we know is that the set bits straddle the
2683 middle of the 64-bit word. */
2684 sparc_emit_set_const64_quick2 (op0, temp,
2685 focus_bits, 0,
2686 lowest_bit_set);
2687 return;
2688 }
2689
2690 /* 1) sethi %hi(high_bits), %reg
2691 * or %reg, %lo(high_bits), %reg
2692 * sllx %reg, 32, %reg
2693 * or %reg, low_bits, %reg
2694 */
2695 if (SPARC_SIMM13_P(low_bits)
2696 && ((int)low_bits > 0))
2697 {
2698 sparc_emit_set_const64_quick2 (op0, temp, high_bits, low_bits, 32);
2699 return;
2700 }
2701
2702 /* The easiest way when all else fails, is full decomposition. */
2703 sparc_emit_set_const64_longway (op0, temp, high_bits, low_bits);
2704 }
2705 #endif /* HOST_BITS_PER_WIDE_INT == 32 */
2706
2707 /* Given a comparison code (EQ, NE, etc.) and the first operand of a COMPARE,
2708 return the mode to be used for the comparison. For floating-point,
2709 CCFP[E]mode is used. CC_NOOVmode should be used when the first operand
2710 is a PLUS, MINUS, NEG, or ASHIFT. CCmode should be used when no special
2711 processing is needed. */
2712
2713 enum machine_mode
2714 select_cc_mode (enum rtx_code op, rtx x, rtx y ATTRIBUTE_UNUSED)
2715 {
2716 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2717 {
2718 switch (op)
2719 {
2720 case EQ:
2721 case NE:
2722 case UNORDERED:
2723 case ORDERED:
2724 case UNLT:
2725 case UNLE:
2726 case UNGT:
2727 case UNGE:
2728 case UNEQ:
2729 case LTGT:
2730 return CCFPmode;
2731
2732 case LT:
2733 case LE:
2734 case GT:
2735 case GE:
2736 return CCFPEmode;
2737
2738 default:
2739 gcc_unreachable ();
2740 }
2741 }
2742 else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
2743 || GET_CODE (x) == NEG || GET_CODE (x) == ASHIFT)
2744 {
2745 if (TARGET_ARCH64 && GET_MODE (x) == DImode)
2746 return CCX_NOOVmode;
2747 else
2748 return CC_NOOVmode;
2749 }
2750 else
2751 {
2752 if (TARGET_ARCH64 && GET_MODE (x) == DImode)
2753 return CCXmode;
2754 else
2755 return CCmode;
2756 }
2757 }
2758
2759 /* Emit the compare insn and return the CC reg for a CODE comparison
2760 with operands X and Y. */
2761
2762 static rtx
2763 gen_compare_reg_1 (enum rtx_code code, rtx x, rtx y)
2764 {
2765 enum machine_mode mode;
2766 rtx cc_reg;
2767
2768 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
2769 return x;
2770
2771 mode = SELECT_CC_MODE (code, x, y);
2772
2773 /* ??? We don't have movcc patterns so we cannot generate pseudo regs for the
2774 fcc regs (cse can't tell they're really call clobbered regs and will
2775 remove a duplicate comparison even if there is an intervening function
2776 call - it will then try to reload the cc reg via an int reg which is why
2777 we need the movcc patterns). It is possible to provide the movcc
2778 patterns by using the ldxfsr/stxfsr v9 insns. I tried it: you need two
2779 registers (say %g1,%g5) and it takes about 6 insns. A better fix would be
2780 to tell cse that CCFPE mode registers (even pseudos) are call
2781 clobbered. */
2782
2783 /* ??? This is an experiment. Rather than making changes to cse which may
2784 or may not be easy/clean, we do our own cse. This is possible because
2785 we will generate hard registers. Cse knows they're call clobbered (it
2786 doesn't know the same thing about pseudos). If we guess wrong, no big
2787 deal, but if we win, great! */
2788
2789 if (TARGET_V9 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2790 #if 1 /* experiment */
2791 {
2792 int reg;
2793 /* We cycle through the registers to ensure they're all exercised. */
2794 static int next_fcc_reg = 0;
2795 /* Previous x,y for each fcc reg. */
2796 static rtx prev_args[4][2];
2797
2798 /* Scan prev_args for x,y. */
2799 for (reg = 0; reg < 4; reg++)
2800 if (prev_args[reg][0] == x && prev_args[reg][1] == y)
2801 break;
2802 if (reg == 4)
2803 {
2804 reg = next_fcc_reg;
2805 prev_args[reg][0] = x;
2806 prev_args[reg][1] = y;
2807 next_fcc_reg = (next_fcc_reg + 1) & 3;
2808 }
2809 cc_reg = gen_rtx_REG (mode, reg + SPARC_FIRST_V9_FCC_REG);
2810 }
2811 #else
2812 cc_reg = gen_reg_rtx (mode);
2813 #endif /* ! experiment */
2814 else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2815 cc_reg = gen_rtx_REG (mode, SPARC_FCC_REG);
2816 else
2817 cc_reg = gen_rtx_REG (mode, SPARC_ICC_REG);
2818
2819 /* We shouldn't get there for TFmode if !TARGET_HARD_QUAD. If we do, this
2820 will only result in an unrecognizable insn so no point in asserting. */
2821 emit_insn (gen_rtx_SET (VOIDmode, cc_reg, gen_rtx_COMPARE (mode, x, y)));
2822
2823 return cc_reg;
2824 }
2825
2826
2827 /* Emit the compare insn and return the CC reg for the comparison in CMP. */
2828
2829 rtx
2830 gen_compare_reg (rtx cmp)
2831 {
2832 return gen_compare_reg_1 (GET_CODE (cmp), XEXP (cmp, 0), XEXP (cmp, 1));
2833 }
2834
2835 /* This function is used for v9 only.
2836 DEST is the target of the Scc insn.
2837 CODE is the code for an Scc's comparison.
2838 X and Y are the values we compare.
2839
2840 This function is needed to turn
2841
2842 (set (reg:SI 110)
2843 (gt (reg:CCX 100 %icc)
2844 (const_int 0)))
2845 into
2846 (set (reg:SI 110)
2847 (gt:DI (reg:CCX 100 %icc)
2848 (const_int 0)))
2849
2850 IE: The instruction recognizer needs to see the mode of the comparison to
2851 find the right instruction. We could use "gt:DI" right in the
2852 define_expand, but leaving it out allows us to handle DI, SI, etc. */
2853
2854 static int
2855 gen_v9_scc (rtx dest, enum rtx_code compare_code, rtx x, rtx y)
2856 {
2857 if (! TARGET_ARCH64
2858 && (GET_MODE (x) == DImode
2859 || GET_MODE (dest) == DImode))
2860 return 0;
2861
2862 /* Try to use the movrCC insns. */
2863 if (TARGET_ARCH64
2864 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT
2865 && y == const0_rtx
2866 && v9_regcmp_p (compare_code))
2867 {
2868 rtx op0 = x;
2869 rtx temp;
2870
2871 /* Special case for op0 != 0. This can be done with one instruction if
2872 dest == x. */
2873
2874 if (compare_code == NE
2875 && GET_MODE (dest) == DImode
2876 && rtx_equal_p (op0, dest))
2877 {
2878 emit_insn (gen_rtx_SET (VOIDmode, dest,
2879 gen_rtx_IF_THEN_ELSE (DImode,
2880 gen_rtx_fmt_ee (compare_code, DImode,
2881 op0, const0_rtx),
2882 const1_rtx,
2883 dest)));
2884 return 1;
2885 }
2886
2887 if (reg_overlap_mentioned_p (dest, op0))
2888 {
2889 /* Handle the case where dest == x.
2890 We "early clobber" the result. */
2891 op0 = gen_reg_rtx (GET_MODE (x));
2892 emit_move_insn (op0, x);
2893 }
2894
2895 emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
2896 if (GET_MODE (op0) != DImode)
2897 {
2898 temp = gen_reg_rtx (DImode);
2899 convert_move (temp, op0, 0);
2900 }
2901 else
2902 temp = op0;
2903 emit_insn (gen_rtx_SET (VOIDmode, dest,
2904 gen_rtx_IF_THEN_ELSE (GET_MODE (dest),
2905 gen_rtx_fmt_ee (compare_code, DImode,
2906 temp, const0_rtx),
2907 const1_rtx,
2908 dest)));
2909 return 1;
2910 }
2911 else
2912 {
2913 x = gen_compare_reg_1 (compare_code, x, y);
2914 y = const0_rtx;
2915
2916 gcc_assert (GET_MODE (x) != CC_NOOVmode
2917 && GET_MODE (x) != CCX_NOOVmode);
2918
2919 emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
2920 emit_insn (gen_rtx_SET (VOIDmode, dest,
2921 gen_rtx_IF_THEN_ELSE (GET_MODE (dest),
2922 gen_rtx_fmt_ee (compare_code,
2923 GET_MODE (x), x, y),
2924 const1_rtx, dest)));
2925 return 1;
2926 }
2927 }
2928
2929
2930 /* Emit an scc insn. For seq, sne, sgeu, and sltu, we can do this
2931 without jumps using the addx/subx instructions. */
2932
2933 bool
2934 emit_scc_insn (rtx operands[])
2935 {
2936 rtx tem;
2937 rtx x;
2938 rtx y;
2939 enum rtx_code code;
2940
2941 /* The quad-word fp compare library routines all return nonzero to indicate
2942 true, which is different from the equivalent libgcc routines, so we must
2943 handle them specially here. */
2944 if (GET_MODE (operands[2]) == TFmode && ! TARGET_HARD_QUAD)
2945 {
2946 operands[1] = sparc_emit_float_lib_cmp (operands[2], operands[3],
2947 GET_CODE (operands[1]));
2948 operands[2] = XEXP (operands[1], 0);
2949 operands[3] = XEXP (operands[1], 1);
2950 }
2951
2952 code = GET_CODE (operands[1]);
2953 x = operands[2];
2954 y = operands[3];
2955
2956 /* For seq/sne on v9 we use the same code as v8 (the addx/subx method has
2957 more applications). The exception to this is "reg != 0" which can
2958 be done in one instruction on v9 (so we do it). */
2959 if (code == EQ)
2960 {
2961 if (GET_MODE (x) == SImode)
2962 {
2963 rtx pat;
2964 if (TARGET_ARCH64)
2965 pat = gen_seqsidi_special (operands[0], x, y);
2966 else
2967 pat = gen_seqsisi_special (operands[0], x, y);
2968 emit_insn (pat);
2969 return true;
2970 }
2971 else if (GET_MODE (x) == DImode)
2972 {
2973 rtx pat = gen_seqdi_special (operands[0], x, y);
2974 emit_insn (pat);
2975 return true;
2976 }
2977 }
2978
2979 if (code == NE)
2980 {
2981 if (GET_MODE (x) == SImode)
2982 {
2983 rtx pat;
2984 if (TARGET_ARCH64)
2985 pat = gen_snesidi_special (operands[0], x, y);
2986 else
2987 pat = gen_snesisi_special (operands[0], x, y);
2988 emit_insn (pat);
2989 return true;
2990 }
2991 else if (GET_MODE (x) == DImode)
2992 {
2993 rtx pat;
2994 if (TARGET_VIS3)
2995 pat = gen_snedi_special_vis3 (operands[0], x, y);
2996 else
2997 pat = gen_snedi_special (operands[0], x, y);
2998 emit_insn (pat);
2999 return true;
3000 }
3001 }
3002
3003 if (TARGET_V9
3004 && TARGET_ARCH64
3005 && GET_MODE (x) == DImode
3006 && !(TARGET_VIS3
3007 && (code == GTU || code == LTU))
3008 && gen_v9_scc (operands[0], code, x, y))
3009 return true;
3010
3011 /* We can do LTU and GEU using the addx/subx instructions too. And
3012 for GTU/LEU, if both operands are registers swap them and fall
3013 back to the easy case. */
3014 if (code == GTU || code == LEU)
3015 {
3016 if ((GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
3017 && (GET_CODE (y) == REG || GET_CODE (y) == SUBREG))
3018 {
3019 tem = x;
3020 x = y;
3021 y = tem;
3022 code = swap_condition (code);
3023 }
3024 }
3025
3026 if (code == LTU
3027 || (!TARGET_VIS3 && code == GEU))
3028 {
3029 emit_insn (gen_rtx_SET (VOIDmode, operands[0],
3030 gen_rtx_fmt_ee (code, GET_MODE (operands[0]),
3031 gen_compare_reg_1 (code, x, y),
3032 const0_rtx)));
3033 return true;
3034 }
3035
3036 /* All the posibilities to use addx/subx based sequences has been
3037 exhausted, try for a 3 instruction sequence using v9 conditional
3038 moves. */
3039 if (TARGET_V9 && gen_v9_scc (operands[0], code, x, y))
3040 return true;
3041
3042 /* Nope, do branches. */
3043 return false;
3044 }
3045
3046 /* Emit a conditional jump insn for the v9 architecture using comparison code
3047 CODE and jump target LABEL.
3048 This function exists to take advantage of the v9 brxx insns. */
3049
3050 static void
3051 emit_v9_brxx_insn (enum rtx_code code, rtx op0, rtx label)
3052 {
3053 emit_jump_insn (gen_rtx_SET (VOIDmode,
3054 pc_rtx,
3055 gen_rtx_IF_THEN_ELSE (VOIDmode,
3056 gen_rtx_fmt_ee (code, GET_MODE (op0),
3057 op0, const0_rtx),
3058 gen_rtx_LABEL_REF (VOIDmode, label),
3059 pc_rtx)));
3060 }
3061
3062 /* Emit a conditional jump insn for the UA2011 architecture using
3063 comparison code CODE and jump target LABEL. This function exists
3064 to take advantage of the UA2011 Compare and Branch insns. */
3065
3066 static void
3067 emit_cbcond_insn (enum rtx_code code, rtx op0, rtx op1, rtx label)
3068 {
3069 rtx if_then_else;
3070
3071 if_then_else = gen_rtx_IF_THEN_ELSE (VOIDmode,
3072 gen_rtx_fmt_ee(code, GET_MODE(op0),
3073 op0, op1),
3074 gen_rtx_LABEL_REF (VOIDmode, label),
3075 pc_rtx);
3076
3077 emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, if_then_else));
3078 }
3079
3080 void
3081 emit_conditional_branch_insn (rtx operands[])
3082 {
3083 /* The quad-word fp compare library routines all return nonzero to indicate
3084 true, which is different from the equivalent libgcc routines, so we must
3085 handle them specially here. */
3086 if (GET_MODE (operands[1]) == TFmode && ! TARGET_HARD_QUAD)
3087 {
3088 operands[0] = sparc_emit_float_lib_cmp (operands[1], operands[2],
3089 GET_CODE (operands[0]));
3090 operands[1] = XEXP (operands[0], 0);
3091 operands[2] = XEXP (operands[0], 1);
3092 }
3093
3094 /* If we can tell early on that the comparison is against a constant
3095 that won't fit in the 5-bit signed immediate field of a cbcond,
3096 use one of the other v9 conditional branch sequences. */
3097 if (TARGET_CBCOND
3098 && GET_CODE (operands[1]) == REG
3099 && (GET_MODE (operands[1]) == SImode
3100 || (TARGET_ARCH64 && GET_MODE (operands[1]) == DImode))
3101 && (GET_CODE (operands[2]) != CONST_INT
3102 || SPARC_SIMM5_P (INTVAL (operands[2]))))
3103 {
3104 emit_cbcond_insn (GET_CODE (operands[0]), operands[1], operands[2], operands[3]);
3105 return;
3106 }
3107
3108 if (TARGET_ARCH64 && operands[2] == const0_rtx
3109 && GET_CODE (operands[1]) == REG
3110 && GET_MODE (operands[1]) == DImode)
3111 {
3112 emit_v9_brxx_insn (GET_CODE (operands[0]), operands[1], operands[3]);
3113 return;
3114 }
3115
3116 operands[1] = gen_compare_reg (operands[0]);
3117 operands[2] = const0_rtx;
3118 operands[0] = gen_rtx_fmt_ee (GET_CODE (operands[0]), VOIDmode,
3119 operands[1], operands[2]);
3120 emit_jump_insn (gen_cbranchcc4 (operands[0], operands[1], operands[2],
3121 operands[3]));
3122 }
3123
3124
3125 /* Generate a DFmode part of a hard TFmode register.
3126 REG is the TFmode hard register, LOW is 1 for the
3127 low 64bit of the register and 0 otherwise.
3128 */
3129 rtx
3130 gen_df_reg (rtx reg, int low)
3131 {
3132 int regno = REGNO (reg);
3133
3134 if ((WORDS_BIG_ENDIAN == 0) ^ (low != 0))
3135 regno += (TARGET_ARCH64 && SPARC_INT_REG_P (regno)) ? 1 : 2;
3136 return gen_rtx_REG (DFmode, regno);
3137 }
3138 \f
3139 /* Generate a call to FUNC with OPERANDS. Operand 0 is the return value.
3140 Unlike normal calls, TFmode operands are passed by reference. It is
3141 assumed that no more than 3 operands are required. */
3142
3143 static void
3144 emit_soft_tfmode_libcall (const char *func_name, int nargs, rtx *operands)
3145 {
3146 rtx ret_slot = NULL, arg[3], func_sym;
3147 int i;
3148
3149 /* We only expect to be called for conversions, unary, and binary ops. */
3150 gcc_assert (nargs == 2 || nargs == 3);
3151
3152 for (i = 0; i < nargs; ++i)
3153 {
3154 rtx this_arg = operands[i];
3155 rtx this_slot;
3156
3157 /* TFmode arguments and return values are passed by reference. */
3158 if (GET_MODE (this_arg) == TFmode)
3159 {
3160 int force_stack_temp;
3161
3162 force_stack_temp = 0;
3163 if (TARGET_BUGGY_QP_LIB && i == 0)
3164 force_stack_temp = 1;
3165
3166 if (GET_CODE (this_arg) == MEM
3167 && ! force_stack_temp)
3168 {
3169 tree expr = MEM_EXPR (this_arg);
3170 if (expr)
3171 mark_addressable (expr);
3172 this_arg = XEXP (this_arg, 0);
3173 }
3174 else if (CONSTANT_P (this_arg)
3175 && ! force_stack_temp)
3176 {
3177 this_slot = force_const_mem (TFmode, this_arg);
3178 this_arg = XEXP (this_slot, 0);
3179 }
3180 else
3181 {
3182 this_slot = assign_stack_temp (TFmode, GET_MODE_SIZE (TFmode));
3183
3184 /* Operand 0 is the return value. We'll copy it out later. */
3185 if (i > 0)
3186 emit_move_insn (this_slot, this_arg);
3187 else
3188 ret_slot = this_slot;
3189
3190 this_arg = XEXP (this_slot, 0);
3191 }
3192 }
3193
3194 arg[i] = this_arg;
3195 }
3196
3197 func_sym = gen_rtx_SYMBOL_REF (Pmode, func_name);
3198
3199 if (GET_MODE (operands[0]) == TFmode)
3200 {
3201 if (nargs == 2)
3202 emit_library_call (func_sym, LCT_NORMAL, VOIDmode, 2,
3203 arg[0], GET_MODE (arg[0]),
3204 arg[1], GET_MODE (arg[1]));
3205 else
3206 emit_library_call (func_sym, LCT_NORMAL, VOIDmode, 3,
3207 arg[0], GET_MODE (arg[0]),
3208 arg[1], GET_MODE (arg[1]),
3209 arg[2], GET_MODE (arg[2]));
3210
3211 if (ret_slot)
3212 emit_move_insn (operands[0], ret_slot);
3213 }
3214 else
3215 {
3216 rtx ret;
3217
3218 gcc_assert (nargs == 2);
3219
3220 ret = emit_library_call_value (func_sym, operands[0], LCT_NORMAL,
3221 GET_MODE (operands[0]), 1,
3222 arg[1], GET_MODE (arg[1]));
3223
3224 if (ret != operands[0])
3225 emit_move_insn (operands[0], ret);
3226 }
3227 }
3228
3229 /* Expand soft-float TFmode calls to sparc abi routines. */
3230
3231 static void
3232 emit_soft_tfmode_binop (enum rtx_code code, rtx *operands)
3233 {
3234 const char *func;
3235
3236 switch (code)
3237 {
3238 case PLUS:
3239 func = "_Qp_add";
3240 break;
3241 case MINUS:
3242 func = "_Qp_sub";
3243 break;
3244 case MULT:
3245 func = "_Qp_mul";
3246 break;
3247 case DIV:
3248 func = "_Qp_div";
3249 break;
3250 default:
3251 gcc_unreachable ();
3252 }
3253
3254 emit_soft_tfmode_libcall (func, 3, operands);
3255 }
3256
3257 static void
3258 emit_soft_tfmode_unop (enum rtx_code code, rtx *operands)
3259 {
3260 const char *func;
3261
3262 gcc_assert (code == SQRT);
3263 func = "_Qp_sqrt";
3264
3265 emit_soft_tfmode_libcall (func, 2, operands);
3266 }
3267
3268 static void
3269 emit_soft_tfmode_cvt (enum rtx_code code, rtx *operands)
3270 {
3271 const char *func;
3272
3273 switch (code)
3274 {
3275 case FLOAT_EXTEND:
3276 switch (GET_MODE (operands[1]))
3277 {
3278 case SFmode:
3279 func = "_Qp_stoq";
3280 break;
3281 case DFmode:
3282 func = "_Qp_dtoq";
3283 break;
3284 default:
3285 gcc_unreachable ();
3286 }
3287 break;
3288
3289 case FLOAT_TRUNCATE:
3290 switch (GET_MODE (operands[0]))
3291 {
3292 case SFmode:
3293 func = "_Qp_qtos";
3294 break;
3295 case DFmode:
3296 func = "_Qp_qtod";
3297 break;
3298 default:
3299 gcc_unreachable ();
3300 }
3301 break;
3302
3303 case FLOAT:
3304 switch (GET_MODE (operands[1]))
3305 {
3306 case SImode:
3307 func = "_Qp_itoq";
3308 if (TARGET_ARCH64)
3309 operands[1] = gen_rtx_SIGN_EXTEND (DImode, operands[1]);
3310 break;
3311 case DImode:
3312 func = "_Qp_xtoq";
3313 break;
3314 default:
3315 gcc_unreachable ();
3316 }
3317 break;
3318
3319 case UNSIGNED_FLOAT:
3320 switch (GET_MODE (operands[1]))
3321 {
3322 case SImode:
3323 func = "_Qp_uitoq";
3324 if (TARGET_ARCH64)
3325 operands[1] = gen_rtx_ZERO_EXTEND (DImode, operands[1]);
3326 break;
3327 case DImode:
3328 func = "_Qp_uxtoq";
3329 break;
3330 default:
3331 gcc_unreachable ();
3332 }
3333 break;
3334
3335 case FIX:
3336 switch (GET_MODE (operands[0]))
3337 {
3338 case SImode:
3339 func = "_Qp_qtoi";
3340 break;
3341 case DImode:
3342 func = "_Qp_qtox";
3343 break;
3344 default:
3345 gcc_unreachable ();
3346 }
3347 break;
3348
3349 case UNSIGNED_FIX:
3350 switch (GET_MODE (operands[0]))
3351 {
3352 case SImode:
3353 func = "_Qp_qtoui";
3354 break;
3355 case DImode:
3356 func = "_Qp_qtoux";
3357 break;
3358 default:
3359 gcc_unreachable ();
3360 }
3361 break;
3362
3363 default:
3364 gcc_unreachable ();
3365 }
3366
3367 emit_soft_tfmode_libcall (func, 2, operands);
3368 }
3369
3370 /* Expand a hard-float tfmode operation. All arguments must be in
3371 registers. */
3372
3373 static void
3374 emit_hard_tfmode_operation (enum rtx_code code, rtx *operands)
3375 {
3376 rtx op, dest;
3377
3378 if (GET_RTX_CLASS (code) == RTX_UNARY)
3379 {
3380 operands[1] = force_reg (GET_MODE (operands[1]), operands[1]);
3381 op = gen_rtx_fmt_e (code, GET_MODE (operands[0]), operands[1]);
3382 }
3383 else
3384 {
3385 operands[1] = force_reg (GET_MODE (operands[1]), operands[1]);
3386 operands[2] = force_reg (GET_MODE (operands[2]), operands[2]);
3387 op = gen_rtx_fmt_ee (code, GET_MODE (operands[0]),
3388 operands[1], operands[2]);
3389 }
3390
3391 if (register_operand (operands[0], VOIDmode))
3392 dest = operands[0];
3393 else
3394 dest = gen_reg_rtx (GET_MODE (operands[0]));
3395
3396 emit_insn (gen_rtx_SET (VOIDmode, dest, op));
3397
3398 if (dest != operands[0])
3399 emit_move_insn (operands[0], dest);
3400 }
3401
3402 void
3403 emit_tfmode_binop (enum rtx_code code, rtx *operands)
3404 {
3405 if (TARGET_HARD_QUAD)
3406 emit_hard_tfmode_operation (code, operands);
3407 else
3408 emit_soft_tfmode_binop (code, operands);
3409 }
3410
3411 void
3412 emit_tfmode_unop (enum rtx_code code, rtx *operands)
3413 {
3414 if (TARGET_HARD_QUAD)
3415 emit_hard_tfmode_operation (code, operands);
3416 else
3417 emit_soft_tfmode_unop (code, operands);
3418 }
3419
3420 void
3421 emit_tfmode_cvt (enum rtx_code code, rtx *operands)
3422 {
3423 if (TARGET_HARD_QUAD)
3424 emit_hard_tfmode_operation (code, operands);
3425 else
3426 emit_soft_tfmode_cvt (code, operands);
3427 }
3428 \f
3429 /* Return nonzero if a branch/jump/call instruction will be emitting
3430 nop into its delay slot. */
3431
3432 int
3433 empty_delay_slot (rtx_insn *insn)
3434 {
3435 rtx seq;
3436
3437 /* If no previous instruction (should not happen), return true. */
3438 if (PREV_INSN (insn) == NULL)
3439 return 1;
3440
3441 seq = NEXT_INSN (PREV_INSN (insn));
3442 if (GET_CODE (PATTERN (seq)) == SEQUENCE)
3443 return 0;
3444
3445 return 1;
3446 }
3447
3448 /* Return nonzero if we should emit a nop after a cbcond instruction.
3449 The cbcond instruction does not have a delay slot, however there is
3450 a severe performance penalty if a control transfer appears right
3451 after a cbcond. Therefore we emit a nop when we detect this
3452 situation. */
3453
3454 int
3455 emit_cbcond_nop (rtx insn)
3456 {
3457 rtx next = next_active_insn (insn);
3458
3459 if (!next)
3460 return 1;
3461
3462 if (NONJUMP_INSN_P (next)
3463 && GET_CODE (PATTERN (next)) == SEQUENCE)
3464 next = XVECEXP (PATTERN (next), 0, 0);
3465 else if (CALL_P (next)
3466 && GET_CODE (PATTERN (next)) == PARALLEL)
3467 {
3468 rtx delay = XVECEXP (PATTERN (next), 0, 1);
3469
3470 if (GET_CODE (delay) == RETURN)
3471 {
3472 /* It's a sibling call. Do not emit the nop if we're going
3473 to emit something other than the jump itself as the first
3474 instruction of the sibcall sequence. */
3475 if (sparc_leaf_function_p || TARGET_FLAT)
3476 return 0;
3477 }
3478 }
3479
3480 if (NONJUMP_INSN_P (next))
3481 return 0;
3482
3483 return 1;
3484 }
3485
3486 /* Return nonzero if TRIAL can go into the call delay slot. */
3487
3488 int
3489 eligible_for_call_delay (rtx_insn *trial)
3490 {
3491 rtx pat;
3492
3493 if (get_attr_in_branch_delay (trial) == IN_BRANCH_DELAY_FALSE)
3494 return 0;
3495
3496 /* Binutils allows
3497 call __tls_get_addr, %tgd_call (foo)
3498 add %l7, %o0, %o0, %tgd_add (foo)
3499 while Sun as/ld does not. */
3500 if (TARGET_GNU_TLS || !TARGET_TLS)
3501 return 1;
3502
3503 pat = PATTERN (trial);
3504
3505 /* We must reject tgd_add{32|64}, i.e.
3506 (set (reg) (plus (reg) (unspec [(reg) (symbol_ref)] UNSPEC_TLSGD)))
3507 and tldm_add{32|64}, i.e.
3508 (set (reg) (plus (reg) (unspec [(reg) (symbol_ref)] UNSPEC_TLSLDM)))
3509 for Sun as/ld. */
3510 if (GET_CODE (pat) == SET
3511 && GET_CODE (SET_SRC (pat)) == PLUS)
3512 {
3513 rtx unspec = XEXP (SET_SRC (pat), 1);
3514
3515 if (GET_CODE (unspec) == UNSPEC
3516 && (XINT (unspec, 1) == UNSPEC_TLSGD
3517 || XINT (unspec, 1) == UNSPEC_TLSLDM))
3518 return 0;
3519 }
3520
3521 return 1;
3522 }
3523
3524 /* Return nonzero if TRIAL, an insn, can be combined with a 'restore'
3525 instruction. RETURN_P is true if the v9 variant 'return' is to be
3526 considered in the test too.
3527
3528 TRIAL must be a SET whose destination is a REG appropriate for the
3529 'restore' instruction or, if RETURN_P is true, for the 'return'
3530 instruction. */
3531
3532 static int
3533 eligible_for_restore_insn (rtx trial, bool return_p)
3534 {
3535 rtx pat = PATTERN (trial);
3536 rtx src = SET_SRC (pat);
3537 bool src_is_freg = false;
3538 rtx src_reg;
3539
3540 /* Since we now can do moves between float and integer registers when
3541 VIS3 is enabled, we have to catch this case. We can allow such
3542 moves when doing a 'return' however. */
3543 src_reg = src;
3544 if (GET_CODE (src_reg) == SUBREG)
3545 src_reg = SUBREG_REG (src_reg);
3546 if (GET_CODE (src_reg) == REG
3547 && SPARC_FP_REG_P (REGNO (src_reg)))
3548 src_is_freg = true;
3549
3550 /* The 'restore src,%g0,dest' pattern for word mode and below. */
3551 if (GET_MODE_CLASS (GET_MODE (src)) != MODE_FLOAT
3552 && arith_operand (src, GET_MODE (src))
3553 && ! src_is_freg)
3554 {
3555 if (TARGET_ARCH64)
3556 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
3557 else
3558 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (SImode);
3559 }
3560
3561 /* The 'restore src,%g0,dest' pattern for double-word mode. */
3562 else if (GET_MODE_CLASS (GET_MODE (src)) != MODE_FLOAT
3563 && arith_double_operand (src, GET_MODE (src))
3564 && ! src_is_freg)
3565 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
3566
3567 /* The 'restore src,%g0,dest' pattern for float if no FPU. */
3568 else if (! TARGET_FPU && register_operand (src, SFmode))
3569 return 1;
3570
3571 /* The 'restore src,%g0,dest' pattern for double if no FPU. */
3572 else if (! TARGET_FPU && TARGET_ARCH64 && register_operand (src, DFmode))
3573 return 1;
3574
3575 /* If we have the 'return' instruction, anything that does not use
3576 local or output registers and can go into a delay slot wins. */
3577 else if (return_p && TARGET_V9 && !epilogue_renumber (&pat, 1))
3578 return 1;
3579
3580 /* The 'restore src1,src2,dest' pattern for SImode. */
3581 else if (GET_CODE (src) == PLUS
3582 && register_operand (XEXP (src, 0), SImode)
3583 && arith_operand (XEXP (src, 1), SImode))
3584 return 1;
3585
3586 /* The 'restore src1,src2,dest' pattern for DImode. */
3587 else if (GET_CODE (src) == PLUS
3588 && register_operand (XEXP (src, 0), DImode)
3589 && arith_double_operand (XEXP (src, 1), DImode))
3590 return 1;
3591
3592 /* The 'restore src1,%lo(src2),dest' pattern. */
3593 else if (GET_CODE (src) == LO_SUM
3594 && ! TARGET_CM_MEDMID
3595 && ((register_operand (XEXP (src, 0), SImode)
3596 && immediate_operand (XEXP (src, 1), SImode))
3597 || (TARGET_ARCH64
3598 && register_operand (XEXP (src, 0), DImode)
3599 && immediate_operand (XEXP (src, 1), DImode))))
3600 return 1;
3601
3602 /* The 'restore src,src,dest' pattern. */
3603 else if (GET_CODE (src) == ASHIFT
3604 && (register_operand (XEXP (src, 0), SImode)
3605 || register_operand (XEXP (src, 0), DImode))
3606 && XEXP (src, 1) == const1_rtx)
3607 return 1;
3608
3609 return 0;
3610 }
3611
3612 /* Return nonzero if TRIAL can go into the function return's delay slot. */
3613
3614 int
3615 eligible_for_return_delay (rtx_insn *trial)
3616 {
3617 int regno;
3618 rtx pat;
3619
3620 /* If the function uses __builtin_eh_return, the eh_return machinery
3621 occupies the delay slot. */
3622 if (crtl->calls_eh_return)
3623 return 0;
3624
3625 if (get_attr_in_branch_delay (trial) == IN_BRANCH_DELAY_FALSE)
3626 return 0;
3627
3628 /* In the case of a leaf or flat function, anything can go into the slot. */
3629 if (sparc_leaf_function_p || TARGET_FLAT)
3630 return 1;
3631
3632 if (!NONJUMP_INSN_P (trial))
3633 return 0;
3634
3635 pat = PATTERN (trial);
3636 if (GET_CODE (pat) == PARALLEL)
3637 {
3638 int i;
3639
3640 if (! TARGET_V9)
3641 return 0;
3642 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
3643 {
3644 rtx expr = XVECEXP (pat, 0, i);
3645 if (GET_CODE (expr) != SET)
3646 return 0;
3647 if (GET_CODE (SET_DEST (expr)) != REG)
3648 return 0;
3649 regno = REGNO (SET_DEST (expr));
3650 if (regno >= 8 && regno < 24)
3651 return 0;
3652 }
3653 return !epilogue_renumber (&pat, 1);
3654 }
3655
3656 if (GET_CODE (pat) != SET)
3657 return 0;
3658
3659 if (GET_CODE (SET_DEST (pat)) != REG)
3660 return 0;
3661
3662 regno = REGNO (SET_DEST (pat));
3663
3664 /* Otherwise, only operations which can be done in tandem with
3665 a `restore' or `return' insn can go into the delay slot. */
3666 if (regno >= 8 && regno < 24)
3667 return 0;
3668
3669 /* If this instruction sets up floating point register and we have a return
3670 instruction, it can probably go in. But restore will not work
3671 with FP_REGS. */
3672 if (! SPARC_INT_REG_P (regno))
3673 return TARGET_V9 && !epilogue_renumber (&pat, 1);
3674
3675 return eligible_for_restore_insn (trial, true);
3676 }
3677
3678 /* Return nonzero if TRIAL can go into the sibling call's delay slot. */
3679
3680 int
3681 eligible_for_sibcall_delay (rtx_insn *trial)
3682 {
3683 rtx pat;
3684
3685 if (get_attr_in_branch_delay (trial) == IN_BRANCH_DELAY_FALSE)
3686 return 0;
3687
3688 if (!NONJUMP_INSN_P (trial))
3689 return 0;
3690
3691 pat = PATTERN (trial);
3692
3693 if (sparc_leaf_function_p || TARGET_FLAT)
3694 {
3695 /* If the tail call is done using the call instruction,
3696 we have to restore %o7 in the delay slot. */
3697 if (LEAF_SIBCALL_SLOT_RESERVED_P)
3698 return 0;
3699
3700 /* %g1 is used to build the function address */
3701 if (reg_mentioned_p (gen_rtx_REG (Pmode, 1), pat))
3702 return 0;
3703
3704 return 1;
3705 }
3706
3707 if (GET_CODE (pat) != SET)
3708 return 0;
3709
3710 /* Otherwise, only operations which can be done in tandem with
3711 a `restore' insn can go into the delay slot. */
3712 if (GET_CODE (SET_DEST (pat)) != REG
3713 || (REGNO (SET_DEST (pat)) >= 8 && REGNO (SET_DEST (pat)) < 24)
3714 || ! SPARC_INT_REG_P (REGNO (SET_DEST (pat))))
3715 return 0;
3716
3717 /* If it mentions %o7, it can't go in, because sibcall will clobber it
3718 in most cases. */
3719 if (reg_mentioned_p (gen_rtx_REG (Pmode, 15), pat))
3720 return 0;
3721
3722 return eligible_for_restore_insn (trial, false);
3723 }
3724 \f
3725 /* Determine if it's legal to put X into the constant pool. This
3726 is not possible if X contains the address of a symbol that is
3727 not constant (TLS) or not known at final link time (PIC). */
3728
3729 static bool
3730 sparc_cannot_force_const_mem (enum machine_mode mode, rtx x)
3731 {
3732 switch (GET_CODE (x))
3733 {
3734 case CONST_INT:
3735 case CONST_DOUBLE:
3736 case CONST_VECTOR:
3737 /* Accept all non-symbolic constants. */
3738 return false;
3739
3740 case LABEL_REF:
3741 /* Labels are OK iff we are non-PIC. */
3742 return flag_pic != 0;
3743
3744 case SYMBOL_REF:
3745 /* 'Naked' TLS symbol references are never OK,
3746 non-TLS symbols are OK iff we are non-PIC. */
3747 if (SYMBOL_REF_TLS_MODEL (x))
3748 return true;
3749 else
3750 return flag_pic != 0;
3751
3752 case CONST:
3753 return sparc_cannot_force_const_mem (mode, XEXP (x, 0));
3754 case PLUS:
3755 case MINUS:
3756 return sparc_cannot_force_const_mem (mode, XEXP (x, 0))
3757 || sparc_cannot_force_const_mem (mode, XEXP (x, 1));
3758 case UNSPEC:
3759 return true;
3760 default:
3761 gcc_unreachable ();
3762 }
3763 }
3764 \f
3765 /* Global Offset Table support. */
3766 static GTY(()) rtx got_helper_rtx = NULL_RTX;
3767 static GTY(()) rtx global_offset_table_rtx = NULL_RTX;
3768
3769 /* Return the SYMBOL_REF for the Global Offset Table. */
3770
3771 static GTY(()) rtx sparc_got_symbol = NULL_RTX;
3772
3773 static rtx
3774 sparc_got (void)
3775 {
3776 if (!sparc_got_symbol)
3777 sparc_got_symbol = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3778
3779 return sparc_got_symbol;
3780 }
3781
3782 /* Ensure that we are not using patterns that are not OK with PIC. */
3783
3784 int
3785 check_pic (int i)
3786 {
3787 rtx op;
3788
3789 switch (flag_pic)
3790 {
3791 case 1:
3792 op = recog_data.operand[i];
3793 gcc_assert (GET_CODE (op) != SYMBOL_REF
3794 && (GET_CODE (op) != CONST
3795 || (GET_CODE (XEXP (op, 0)) == MINUS
3796 && XEXP (XEXP (op, 0), 0) == sparc_got ()
3797 && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST)));
3798 case 2:
3799 default:
3800 return 1;
3801 }
3802 }
3803
3804 /* Return true if X is an address which needs a temporary register when
3805 reloaded while generating PIC code. */
3806
3807 int
3808 pic_address_needs_scratch (rtx x)
3809 {
3810 /* An address which is a symbolic plus a non SMALL_INT needs a temp reg. */
3811 if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS
3812 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
3813 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3814 && ! SMALL_INT (XEXP (XEXP (x, 0), 1)))
3815 return 1;
3816
3817 return 0;
3818 }
3819
3820 /* Determine if a given RTX is a valid constant. We already know this
3821 satisfies CONSTANT_P. */
3822
3823 static bool
3824 sparc_legitimate_constant_p (enum machine_mode mode, rtx x)
3825 {
3826 switch (GET_CODE (x))
3827 {
3828 case CONST:
3829 case SYMBOL_REF:
3830 if (sparc_tls_referenced_p (x))
3831 return false;
3832 break;
3833
3834 case CONST_DOUBLE:
3835 if (GET_MODE (x) == VOIDmode)
3836 return true;
3837
3838 /* Floating point constants are generally not ok.
3839 The only exception is 0.0 and all-ones in VIS. */
3840 if (TARGET_VIS
3841 && SCALAR_FLOAT_MODE_P (mode)
3842 && (const_zero_operand (x, mode)
3843 || const_all_ones_operand (x, mode)))
3844 return true;
3845
3846 return false;
3847
3848 case CONST_VECTOR:
3849 /* Vector constants are generally not ok.
3850 The only exception is 0 or -1 in VIS. */
3851 if (TARGET_VIS
3852 && (const_zero_operand (x, mode)
3853 || const_all_ones_operand (x, mode)))
3854 return true;
3855
3856 return false;
3857
3858 default:
3859 break;
3860 }
3861
3862 return true;
3863 }
3864
3865 /* Determine if a given RTX is a valid constant address. */
3866
3867 bool
3868 constant_address_p (rtx x)
3869 {
3870 switch (GET_CODE (x))
3871 {
3872 case LABEL_REF:
3873 case CONST_INT:
3874 case HIGH:
3875 return true;
3876
3877 case CONST:
3878 if (flag_pic && pic_address_needs_scratch (x))
3879 return false;
3880 return sparc_legitimate_constant_p (Pmode, x);
3881
3882 case SYMBOL_REF:
3883 return !flag_pic && sparc_legitimate_constant_p (Pmode, x);
3884
3885 default:
3886 return false;
3887 }
3888 }
3889
3890 /* Nonzero if the constant value X is a legitimate general operand
3891 when generating PIC code. It is given that flag_pic is on and
3892 that X satisfies CONSTANT_P or is a CONST_DOUBLE. */
3893
3894 bool
3895 legitimate_pic_operand_p (rtx x)
3896 {
3897 if (pic_address_needs_scratch (x))
3898 return false;
3899 if (sparc_tls_referenced_p (x))
3900 return false;
3901 return true;
3902 }
3903
3904 #define RTX_OK_FOR_OFFSET_P(X, MODE) \
3905 (CONST_INT_P (X) \
3906 && INTVAL (X) >= -0x1000 \
3907 && INTVAL (X) < (0x1000 - GET_MODE_SIZE (MODE)))
3908
3909 #define RTX_OK_FOR_OLO10_P(X, MODE) \
3910 (CONST_INT_P (X) \
3911 && INTVAL (X) >= -0x1000 \
3912 && INTVAL (X) < (0xc00 - GET_MODE_SIZE (MODE)))
3913
3914 /* Handle the TARGET_LEGITIMATE_ADDRESS_P target hook.
3915
3916 On SPARC, the actual legitimate addresses must be REG+REG or REG+SMALLINT
3917 ordinarily. This changes a bit when generating PIC. */
3918
3919 static bool
3920 sparc_legitimate_address_p (enum machine_mode mode, rtx addr, bool strict)
3921 {
3922 rtx rs1 = NULL, rs2 = NULL, imm1 = NULL;
3923
3924 if (REG_P (addr) || GET_CODE (addr) == SUBREG)
3925 rs1 = addr;
3926 else if (GET_CODE (addr) == PLUS)
3927 {
3928 rs1 = XEXP (addr, 0);
3929 rs2 = XEXP (addr, 1);
3930
3931 /* Canonicalize. REG comes first, if there are no regs,
3932 LO_SUM comes first. */
3933 if (!REG_P (rs1)
3934 && GET_CODE (rs1) != SUBREG
3935 && (REG_P (rs2)
3936 || GET_CODE (rs2) == SUBREG
3937 || (GET_CODE (rs2) == LO_SUM && GET_CODE (rs1) != LO_SUM)))
3938 {
3939 rs1 = XEXP (addr, 1);
3940 rs2 = XEXP (addr, 0);
3941 }
3942
3943 if ((flag_pic == 1
3944 && rs1 == pic_offset_table_rtx
3945 && !REG_P (rs2)
3946 && GET_CODE (rs2) != SUBREG
3947 && GET_CODE (rs2) != LO_SUM
3948 && GET_CODE (rs2) != MEM
3949 && !(GET_CODE (rs2) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs2))
3950 && (! symbolic_operand (rs2, VOIDmode) || mode == Pmode)
3951 && (GET_CODE (rs2) != CONST_INT || SMALL_INT (rs2)))
3952 || ((REG_P (rs1)
3953 || GET_CODE (rs1) == SUBREG)
3954 && RTX_OK_FOR_OFFSET_P (rs2, mode)))
3955 {
3956 imm1 = rs2;
3957 rs2 = NULL;
3958 }
3959 else if ((REG_P (rs1) || GET_CODE (rs1) == SUBREG)
3960 && (REG_P (rs2) || GET_CODE (rs2) == SUBREG))
3961 {
3962 /* We prohibit REG + REG for TFmode when there are no quad move insns
3963 and we consequently need to split. We do this because REG+REG
3964 is not an offsettable address. If we get the situation in reload
3965 where source and destination of a movtf pattern are both MEMs with
3966 REG+REG address, then only one of them gets converted to an
3967 offsettable address. */
3968 if (mode == TFmode
3969 && ! (TARGET_ARCH64 && TARGET_HARD_QUAD))
3970 return 0;
3971
3972 /* Likewise for TImode, but in all cases. */
3973 if (mode == TImode)
3974 return 0;
3975
3976 /* We prohibit REG + REG on ARCH32 if not optimizing for
3977 DFmode/DImode because then mem_min_alignment is likely to be zero
3978 after reload and the forced split would lack a matching splitter
3979 pattern. */
3980 if (TARGET_ARCH32 && !optimize
3981 && (mode == DFmode || mode == DImode))
3982 return 0;
3983 }
3984 else if (USE_AS_OFFSETABLE_LO10
3985 && GET_CODE (rs1) == LO_SUM
3986 && TARGET_ARCH64
3987 && ! TARGET_CM_MEDMID
3988 && RTX_OK_FOR_OLO10_P (rs2, mode))
3989 {
3990 rs2 = NULL;
3991 imm1 = XEXP (rs1, 1);
3992 rs1 = XEXP (rs1, 0);
3993 if (!CONSTANT_P (imm1)
3994 || (GET_CODE (rs1) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs1)))
3995 return 0;
3996 }
3997 }
3998 else if (GET_CODE (addr) == LO_SUM)
3999 {
4000 rs1 = XEXP (addr, 0);
4001 imm1 = XEXP (addr, 1);
4002
4003 if (!CONSTANT_P (imm1)
4004 || (GET_CODE (rs1) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs1)))
4005 return 0;
4006
4007 /* We can't allow TFmode in 32-bit mode, because an offset greater
4008 than the alignment (8) may cause the LO_SUM to overflow. */
4009 if (mode == TFmode && TARGET_ARCH32)
4010 return 0;
4011 }
4012 else if (GET_CODE (addr) == CONST_INT && SMALL_INT (addr))
4013 return 1;
4014 else
4015 return 0;
4016
4017 if (GET_CODE (rs1) == SUBREG)
4018 rs1 = SUBREG_REG (rs1);
4019 if (!REG_P (rs1))
4020 return 0;
4021
4022 if (rs2)
4023 {
4024 if (GET_CODE (rs2) == SUBREG)
4025 rs2 = SUBREG_REG (rs2);
4026 if (!REG_P (rs2))
4027 return 0;
4028 }
4029
4030 if (strict)
4031 {
4032 if (!REGNO_OK_FOR_BASE_P (REGNO (rs1))
4033 || (rs2 && !REGNO_OK_FOR_BASE_P (REGNO (rs2))))
4034 return 0;
4035 }
4036 else
4037 {
4038 if ((! SPARC_INT_REG_P (REGNO (rs1))
4039 && REGNO (rs1) != FRAME_POINTER_REGNUM
4040 && REGNO (rs1) < FIRST_PSEUDO_REGISTER)
4041 || (rs2
4042 && (! SPARC_INT_REG_P (REGNO (rs2))
4043 && REGNO (rs2) != FRAME_POINTER_REGNUM
4044 && REGNO (rs2) < FIRST_PSEUDO_REGISTER)))
4045 return 0;
4046 }
4047 return 1;
4048 }
4049
4050 /* Return the SYMBOL_REF for the tls_get_addr function. */
4051
4052 static GTY(()) rtx sparc_tls_symbol = NULL_RTX;
4053
4054 static rtx
4055 sparc_tls_get_addr (void)
4056 {
4057 if (!sparc_tls_symbol)
4058 sparc_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_addr");
4059
4060 return sparc_tls_symbol;
4061 }
4062
4063 /* Return the Global Offset Table to be used in TLS mode. */
4064
4065 static rtx
4066 sparc_tls_got (void)
4067 {
4068 /* In PIC mode, this is just the PIC offset table. */
4069 if (flag_pic)
4070 {
4071 crtl->uses_pic_offset_table = 1;
4072 return pic_offset_table_rtx;
4073 }
4074
4075 /* In non-PIC mode, Sun as (unlike GNU as) emits PC-relative relocations for
4076 the GOT symbol with the 32-bit ABI, so we reload the GOT register. */
4077 if (TARGET_SUN_TLS && TARGET_ARCH32)
4078 {
4079 load_got_register ();
4080 return global_offset_table_rtx;
4081 }
4082
4083 /* In all other cases, we load a new pseudo with the GOT symbol. */
4084 return copy_to_reg (sparc_got ());
4085 }
4086
4087 /* Return true if X contains a thread-local symbol. */
4088
4089 static bool
4090 sparc_tls_referenced_p (rtx x)
4091 {
4092 if (!TARGET_HAVE_TLS)
4093 return false;
4094
4095 if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS)
4096 x = XEXP (XEXP (x, 0), 0);
4097
4098 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x))
4099 return true;
4100
4101 /* That's all we handle in sparc_legitimize_tls_address for now. */
4102 return false;
4103 }
4104
4105 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
4106 this (thread-local) address. */
4107
4108 static rtx
4109 sparc_legitimize_tls_address (rtx addr)
4110 {
4111 rtx temp1, temp2, temp3, ret, o0, got;
4112 rtx_insn *insn;
4113
4114 gcc_assert (can_create_pseudo_p ());
4115
4116 if (GET_CODE (addr) == SYMBOL_REF)
4117 switch (SYMBOL_REF_TLS_MODEL (addr))
4118 {
4119 case TLS_MODEL_GLOBAL_DYNAMIC:
4120 start_sequence ();
4121 temp1 = gen_reg_rtx (SImode);
4122 temp2 = gen_reg_rtx (SImode);
4123 ret = gen_reg_rtx (Pmode);
4124 o0 = gen_rtx_REG (Pmode, 8);
4125 got = sparc_tls_got ();
4126 emit_insn (gen_tgd_hi22 (temp1, addr));
4127 emit_insn (gen_tgd_lo10 (temp2, temp1, addr));
4128 if (TARGET_ARCH32)
4129 {
4130 emit_insn (gen_tgd_add32 (o0, got, temp2, addr));
4131 insn = emit_call_insn (gen_tgd_call32 (o0, sparc_tls_get_addr (),
4132 addr, const1_rtx));
4133 }
4134 else
4135 {
4136 emit_insn (gen_tgd_add64 (o0, got, temp2, addr));
4137 insn = emit_call_insn (gen_tgd_call64 (o0, sparc_tls_get_addr (),
4138 addr, const1_rtx));
4139 }
4140 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), o0);
4141 insn = get_insns ();
4142 end_sequence ();
4143 emit_libcall_block (insn, ret, o0, addr);
4144 break;
4145
4146 case TLS_MODEL_LOCAL_DYNAMIC:
4147 start_sequence ();
4148 temp1 = gen_reg_rtx (SImode);
4149 temp2 = gen_reg_rtx (SImode);
4150 temp3 = gen_reg_rtx (Pmode);
4151 ret = gen_reg_rtx (Pmode);
4152 o0 = gen_rtx_REG (Pmode, 8);
4153 got = sparc_tls_got ();
4154 emit_insn (gen_tldm_hi22 (temp1));
4155 emit_insn (gen_tldm_lo10 (temp2, temp1));
4156 if (TARGET_ARCH32)
4157 {
4158 emit_insn (gen_tldm_add32 (o0, got, temp2));
4159 insn = emit_call_insn (gen_tldm_call32 (o0, sparc_tls_get_addr (),
4160 const1_rtx));
4161 }
4162 else
4163 {
4164 emit_insn (gen_tldm_add64 (o0, got, temp2));
4165 insn = emit_call_insn (gen_tldm_call64 (o0, sparc_tls_get_addr (),
4166 const1_rtx));
4167 }
4168 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), o0);
4169 insn = get_insns ();
4170 end_sequence ();
4171 emit_libcall_block (insn, temp3, o0,
4172 gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
4173 UNSPEC_TLSLD_BASE));
4174 temp1 = gen_reg_rtx (SImode);
4175 temp2 = gen_reg_rtx (SImode);
4176 emit_insn (gen_tldo_hix22 (temp1, addr));
4177 emit_insn (gen_tldo_lox10 (temp2, temp1, addr));
4178 if (TARGET_ARCH32)
4179 emit_insn (gen_tldo_add32 (ret, temp3, temp2, addr));
4180 else
4181 emit_insn (gen_tldo_add64 (ret, temp3, temp2, addr));
4182 break;
4183
4184 case TLS_MODEL_INITIAL_EXEC:
4185 temp1 = gen_reg_rtx (SImode);
4186 temp2 = gen_reg_rtx (SImode);
4187 temp3 = gen_reg_rtx (Pmode);
4188 got = sparc_tls_got ();
4189 emit_insn (gen_tie_hi22 (temp1, addr));
4190 emit_insn (gen_tie_lo10 (temp2, temp1, addr));
4191 if (TARGET_ARCH32)
4192 emit_insn (gen_tie_ld32 (temp3, got, temp2, addr));
4193 else
4194 emit_insn (gen_tie_ld64 (temp3, got, temp2, addr));
4195 if (TARGET_SUN_TLS)
4196 {
4197 ret = gen_reg_rtx (Pmode);
4198 if (TARGET_ARCH32)
4199 emit_insn (gen_tie_add32 (ret, gen_rtx_REG (Pmode, 7),
4200 temp3, addr));
4201 else
4202 emit_insn (gen_tie_add64 (ret, gen_rtx_REG (Pmode, 7),
4203 temp3, addr));
4204 }
4205 else
4206 ret = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, 7), temp3);
4207 break;
4208
4209 case TLS_MODEL_LOCAL_EXEC:
4210 temp1 = gen_reg_rtx (Pmode);
4211 temp2 = gen_reg_rtx (Pmode);
4212 if (TARGET_ARCH32)
4213 {
4214 emit_insn (gen_tle_hix22_sp32 (temp1, addr));
4215 emit_insn (gen_tle_lox10_sp32 (temp2, temp1, addr));
4216 }
4217 else
4218 {
4219 emit_insn (gen_tle_hix22_sp64 (temp1, addr));
4220 emit_insn (gen_tle_lox10_sp64 (temp2, temp1, addr));
4221 }
4222 ret = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, 7), temp2);
4223 break;
4224
4225 default:
4226 gcc_unreachable ();
4227 }
4228
4229 else if (GET_CODE (addr) == CONST)
4230 {
4231 rtx base, offset;
4232
4233 gcc_assert (GET_CODE (XEXP (addr, 0)) == PLUS);
4234
4235 base = sparc_legitimize_tls_address (XEXP (XEXP (addr, 0), 0));
4236 offset = XEXP (XEXP (addr, 0), 1);
4237
4238 base = force_operand (base, NULL_RTX);
4239 if (!(GET_CODE (offset) == CONST_INT && SMALL_INT (offset)))
4240 offset = force_reg (Pmode, offset);
4241 ret = gen_rtx_PLUS (Pmode, base, offset);
4242 }
4243
4244 else
4245 gcc_unreachable (); /* for now ... */
4246
4247 return ret;
4248 }
4249
4250 /* Legitimize PIC addresses. If the address is already position-independent,
4251 we return ORIG. Newly generated position-independent addresses go into a
4252 reg. This is REG if nonzero, otherwise we allocate register(s) as
4253 necessary. */
4254
4255 static rtx
4256 sparc_legitimize_pic_address (rtx orig, rtx reg)
4257 {
4258 bool gotdata_op = false;
4259
4260 if (GET_CODE (orig) == SYMBOL_REF
4261 /* See the comment in sparc_expand_move. */
4262 || (GET_CODE (orig) == LABEL_REF && !can_use_mov_pic_label_ref (orig)))
4263 {
4264 rtx pic_ref, address;
4265 rtx_insn *insn;
4266
4267 if (reg == 0)
4268 {
4269 gcc_assert (can_create_pseudo_p ());
4270 reg = gen_reg_rtx (Pmode);
4271 }
4272
4273 if (flag_pic == 2)
4274 {
4275 /* If not during reload, allocate another temp reg here for loading
4276 in the address, so that these instructions can be optimized
4277 properly. */
4278 rtx temp_reg = (! can_create_pseudo_p ()
4279 ? reg : gen_reg_rtx (Pmode));
4280
4281 /* Must put the SYMBOL_REF inside an UNSPEC here so that cse
4282 won't get confused into thinking that these two instructions
4283 are loading in the true address of the symbol. If in the
4284 future a PIC rtx exists, that should be used instead. */
4285 if (TARGET_ARCH64)
4286 {
4287 emit_insn (gen_movdi_high_pic (temp_reg, orig));
4288 emit_insn (gen_movdi_lo_sum_pic (temp_reg, temp_reg, orig));
4289 }
4290 else
4291 {
4292 emit_insn (gen_movsi_high_pic (temp_reg, orig));
4293 emit_insn (gen_movsi_lo_sum_pic (temp_reg, temp_reg, orig));
4294 }
4295 address = temp_reg;
4296 gotdata_op = true;
4297 }
4298 else
4299 address = orig;
4300
4301 crtl->uses_pic_offset_table = 1;
4302 if (gotdata_op)
4303 {
4304 if (TARGET_ARCH64)
4305 insn = emit_insn (gen_movdi_pic_gotdata_op (reg,
4306 pic_offset_table_rtx,
4307 address, orig));
4308 else
4309 insn = emit_insn (gen_movsi_pic_gotdata_op (reg,
4310 pic_offset_table_rtx,
4311 address, orig));
4312 }
4313 else
4314 {
4315 pic_ref
4316 = gen_const_mem (Pmode,
4317 gen_rtx_PLUS (Pmode,
4318 pic_offset_table_rtx, address));
4319 insn = emit_move_insn (reg, pic_ref);
4320 }
4321
4322 /* Put a REG_EQUAL note on this insn, so that it can be optimized
4323 by loop. */
4324 set_unique_reg_note (insn, REG_EQUAL, orig);
4325 return reg;
4326 }
4327 else if (GET_CODE (orig) == CONST)
4328 {
4329 rtx base, offset;
4330
4331 if (GET_CODE (XEXP (orig, 0)) == PLUS
4332 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
4333 return orig;
4334
4335 if (reg == 0)
4336 {
4337 gcc_assert (can_create_pseudo_p ());
4338 reg = gen_reg_rtx (Pmode);
4339 }
4340
4341 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
4342 base = sparc_legitimize_pic_address (XEXP (XEXP (orig, 0), 0), reg);
4343 offset = sparc_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
4344 base == reg ? NULL_RTX : reg);
4345
4346 if (GET_CODE (offset) == CONST_INT)
4347 {
4348 if (SMALL_INT (offset))
4349 return plus_constant (Pmode, base, INTVAL (offset));
4350 else if (can_create_pseudo_p ())
4351 offset = force_reg (Pmode, offset);
4352 else
4353 /* If we reach here, then something is seriously wrong. */
4354 gcc_unreachable ();
4355 }
4356 return gen_rtx_PLUS (Pmode, base, offset);
4357 }
4358 else if (GET_CODE (orig) == LABEL_REF)
4359 /* ??? We ought to be checking that the register is live instead, in case
4360 it is eliminated. */
4361 crtl->uses_pic_offset_table = 1;
4362
4363 return orig;
4364 }
4365
4366 /* Try machine-dependent ways of modifying an illegitimate address X
4367 to be legitimate. If we find one, return the new, valid address.
4368
4369 OLDX is the address as it was before break_out_memory_refs was called.
4370 In some cases it is useful to look at this to decide what needs to be done.
4371
4372 MODE is the mode of the operand pointed to by X.
4373
4374 On SPARC, change REG+N into REG+REG, and REG+(X*Y) into REG+REG. */
4375
4376 static rtx
4377 sparc_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
4378 enum machine_mode mode)
4379 {
4380 rtx orig_x = x;
4381
4382 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == MULT)
4383 x = gen_rtx_PLUS (Pmode, XEXP (x, 1),
4384 force_operand (XEXP (x, 0), NULL_RTX));
4385 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == MULT)
4386 x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
4387 force_operand (XEXP (x, 1), NULL_RTX));
4388 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS)
4389 x = gen_rtx_PLUS (Pmode, force_operand (XEXP (x, 0), NULL_RTX),
4390 XEXP (x, 1));
4391 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == PLUS)
4392 x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
4393 force_operand (XEXP (x, 1), NULL_RTX));
4394
4395 if (x != orig_x && sparc_legitimate_address_p (mode, x, FALSE))
4396 return x;
4397
4398 if (sparc_tls_referenced_p (x))
4399 x = sparc_legitimize_tls_address (x);
4400 else if (flag_pic)
4401 x = sparc_legitimize_pic_address (x, NULL_RTX);
4402 else if (GET_CODE (x) == PLUS && CONSTANT_ADDRESS_P (XEXP (x, 1)))
4403 x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
4404 copy_to_mode_reg (Pmode, XEXP (x, 1)));
4405 else if (GET_CODE (x) == PLUS && CONSTANT_ADDRESS_P (XEXP (x, 0)))
4406 x = gen_rtx_PLUS (Pmode, XEXP (x, 1),
4407 copy_to_mode_reg (Pmode, XEXP (x, 0)));
4408 else if (GET_CODE (x) == SYMBOL_REF
4409 || GET_CODE (x) == CONST
4410 || GET_CODE (x) == LABEL_REF)
4411 x = copy_to_suggested_reg (x, NULL_RTX, Pmode);
4412
4413 return x;
4414 }
4415
4416 /* Delegitimize an address that was legitimized by the above function. */
4417
4418 static rtx
4419 sparc_delegitimize_address (rtx x)
4420 {
4421 x = delegitimize_mem_from_attrs (x);
4422
4423 if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 1)) == UNSPEC)
4424 switch (XINT (XEXP (x, 1), 1))
4425 {
4426 case UNSPEC_MOVE_PIC:
4427 case UNSPEC_TLSLE:
4428 x = XVECEXP (XEXP (x, 1), 0, 0);
4429 gcc_assert (GET_CODE (x) == SYMBOL_REF);
4430 break;
4431 default:
4432 break;
4433 }
4434
4435 /* This is generated by mov{si,di}_pic_label_ref in PIC mode. */
4436 if (GET_CODE (x) == MINUS
4437 && REG_P (XEXP (x, 0))
4438 && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM
4439 && GET_CODE (XEXP (x, 1)) == LO_SUM
4440 && GET_CODE (XEXP (XEXP (x, 1), 1)) == UNSPEC
4441 && XINT (XEXP (XEXP (x, 1), 1), 1) == UNSPEC_MOVE_PIC_LABEL)
4442 {
4443 x = XVECEXP (XEXP (XEXP (x, 1), 1), 0, 0);
4444 gcc_assert (GET_CODE (x) == LABEL_REF);
4445 }
4446
4447 return x;
4448 }
4449
4450 /* SPARC implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
4451 replace the input X, or the original X if no replacement is called for.
4452 The output parameter *WIN is 1 if the calling macro should goto WIN,
4453 0 if it should not.
4454
4455 For SPARC, we wish to handle addresses by splitting them into
4456 HIGH+LO_SUM pairs, retaining the LO_SUM in the memory reference.
4457 This cuts the number of extra insns by one.
4458
4459 Do nothing when generating PIC code and the address is a symbolic
4460 operand or requires a scratch register. */
4461
4462 rtx
4463 sparc_legitimize_reload_address (rtx x, enum machine_mode mode,
4464 int opnum, int type,
4465 int ind_levels ATTRIBUTE_UNUSED, int *win)
4466 {
4467 /* Decompose SImode constants into HIGH+LO_SUM. */
4468 if (CONSTANT_P (x)
4469 && (mode != TFmode || TARGET_ARCH64)
4470 && GET_MODE (x) == SImode
4471 && GET_CODE (x) != LO_SUM
4472 && GET_CODE (x) != HIGH
4473 && sparc_cmodel <= CM_MEDLOW
4474 && !(flag_pic
4475 && (symbolic_operand (x, Pmode) || pic_address_needs_scratch (x))))
4476 {
4477 x = gen_rtx_LO_SUM (GET_MODE (x), gen_rtx_HIGH (GET_MODE (x), x), x);
4478 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
4479 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
4480 opnum, (enum reload_type)type);
4481 *win = 1;
4482 return x;
4483 }
4484
4485 /* We have to recognize what we have already generated above. */
4486 if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == HIGH)
4487 {
4488 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
4489 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
4490 opnum, (enum reload_type)type);
4491 *win = 1;
4492 return x;
4493 }
4494
4495 *win = 0;
4496 return x;
4497 }
4498
4499 /* Return true if ADDR (a legitimate address expression)
4500 has an effect that depends on the machine mode it is used for.
4501
4502 In PIC mode,
4503
4504 (mem:HI [%l7+a])
4505
4506 is not equivalent to
4507
4508 (mem:QI [%l7+a]) (mem:QI [%l7+a+1])
4509
4510 because [%l7+a+1] is interpreted as the address of (a+1). */
4511
4512
4513 static bool
4514 sparc_mode_dependent_address_p (const_rtx addr,
4515 addr_space_t as ATTRIBUTE_UNUSED)
4516 {
4517 if (flag_pic && GET_CODE (addr) == PLUS)
4518 {
4519 rtx op0 = XEXP (addr, 0);
4520 rtx op1 = XEXP (addr, 1);
4521 if (op0 == pic_offset_table_rtx
4522 && symbolic_operand (op1, VOIDmode))
4523 return true;
4524 }
4525
4526 return false;
4527 }
4528
4529 #ifdef HAVE_GAS_HIDDEN
4530 # define USE_HIDDEN_LINKONCE 1
4531 #else
4532 # define USE_HIDDEN_LINKONCE 0
4533 #endif
4534
4535 static void
4536 get_pc_thunk_name (char name[32], unsigned int regno)
4537 {
4538 const char *reg_name = reg_names[regno];
4539
4540 /* Skip the leading '%' as that cannot be used in a
4541 symbol name. */
4542 reg_name += 1;
4543
4544 if (USE_HIDDEN_LINKONCE)
4545 sprintf (name, "__sparc_get_pc_thunk.%s", reg_name);
4546 else
4547 ASM_GENERATE_INTERNAL_LABEL (name, "LADDPC", regno);
4548 }
4549
4550 /* Wrapper around the load_pcrel_sym{si,di} patterns. */
4551
4552 static rtx
4553 gen_load_pcrel_sym (rtx op0, rtx op1, rtx op2, rtx op3)
4554 {
4555 int orig_flag_pic = flag_pic;
4556 rtx insn;
4557
4558 /* The load_pcrel_sym{si,di} patterns require absolute addressing. */
4559 flag_pic = 0;
4560 if (TARGET_ARCH64)
4561 insn = gen_load_pcrel_symdi (op0, op1, op2, op3);
4562 else
4563 insn = gen_load_pcrel_symsi (op0, op1, op2, op3);
4564 flag_pic = orig_flag_pic;
4565
4566 return insn;
4567 }
4568
4569 /* Emit code to load the GOT register. */
4570
4571 void
4572 load_got_register (void)
4573 {
4574 /* In PIC mode, this will retrieve pic_offset_table_rtx. */
4575 if (!global_offset_table_rtx)
4576 global_offset_table_rtx = gen_rtx_REG (Pmode, GLOBAL_OFFSET_TABLE_REGNUM);
4577
4578 if (TARGET_VXWORKS_RTP)
4579 emit_insn (gen_vxworks_load_got ());
4580 else
4581 {
4582 /* The GOT symbol is subject to a PC-relative relocation so we need a
4583 helper function to add the PC value and thus get the final value. */
4584 if (!got_helper_rtx)
4585 {
4586 char name[32];
4587 get_pc_thunk_name (name, GLOBAL_OFFSET_TABLE_REGNUM);
4588 got_helper_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
4589 }
4590
4591 emit_insn (gen_load_pcrel_sym (global_offset_table_rtx, sparc_got (),
4592 got_helper_rtx,
4593 GEN_INT (GLOBAL_OFFSET_TABLE_REGNUM)));
4594 }
4595
4596 /* Need to emit this whether or not we obey regdecls,
4597 since setjmp/longjmp can cause life info to screw up.
4598 ??? In the case where we don't obey regdecls, this is not sufficient
4599 since we may not fall out the bottom. */
4600 emit_use (global_offset_table_rtx);
4601 }
4602
4603 /* Emit a call instruction with the pattern given by PAT. ADDR is the
4604 address of the call target. */
4605
4606 void
4607 sparc_emit_call_insn (rtx pat, rtx addr)
4608 {
4609 rtx_insn *insn;
4610
4611 insn = emit_call_insn (pat);
4612
4613 /* The PIC register is live on entry to VxWorks PIC PLT entries. */
4614 if (TARGET_VXWORKS_RTP
4615 && flag_pic
4616 && GET_CODE (addr) == SYMBOL_REF
4617 && (SYMBOL_REF_DECL (addr)
4618 ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
4619 : !SYMBOL_REF_LOCAL_P (addr)))
4620 {
4621 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
4622 crtl->uses_pic_offset_table = 1;
4623 }
4624 }
4625 \f
4626 /* Return 1 if RTX is a MEM which is known to be aligned to at
4627 least a DESIRED byte boundary. */
4628
4629 int
4630 mem_min_alignment (rtx mem, int desired)
4631 {
4632 rtx addr, base, offset;
4633
4634 /* If it's not a MEM we can't accept it. */
4635 if (GET_CODE (mem) != MEM)
4636 return 0;
4637
4638 /* Obviously... */
4639 if (!TARGET_UNALIGNED_DOUBLES
4640 && MEM_ALIGN (mem) / BITS_PER_UNIT >= (unsigned)desired)
4641 return 1;
4642
4643 /* ??? The rest of the function predates MEM_ALIGN so
4644 there is probably a bit of redundancy. */
4645 addr = XEXP (mem, 0);
4646 base = offset = NULL_RTX;
4647 if (GET_CODE (addr) == PLUS)
4648 {
4649 if (GET_CODE (XEXP (addr, 0)) == REG)
4650 {
4651 base = XEXP (addr, 0);
4652
4653 /* What we are saying here is that if the base
4654 REG is aligned properly, the compiler will make
4655 sure any REG based index upon it will be so
4656 as well. */
4657 if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
4658 offset = XEXP (addr, 1);
4659 else
4660 offset = const0_rtx;
4661 }
4662 }
4663 else if (GET_CODE (addr) == REG)
4664 {
4665 base = addr;
4666 offset = const0_rtx;
4667 }
4668
4669 if (base != NULL_RTX)
4670 {
4671 int regno = REGNO (base);
4672
4673 if (regno != HARD_FRAME_POINTER_REGNUM && regno != STACK_POINTER_REGNUM)
4674 {
4675 /* Check if the compiler has recorded some information
4676 about the alignment of the base REG. If reload has
4677 completed, we already matched with proper alignments.
4678 If not running global_alloc, reload might give us
4679 unaligned pointer to local stack though. */
4680 if (((cfun != 0
4681 && REGNO_POINTER_ALIGN (regno) >= desired * BITS_PER_UNIT)
4682 || (optimize && reload_completed))
4683 && (INTVAL (offset) & (desired - 1)) == 0)
4684 return 1;
4685 }
4686 else
4687 {
4688 if (((INTVAL (offset) - SPARC_STACK_BIAS) & (desired - 1)) == 0)
4689 return 1;
4690 }
4691 }
4692 else if (! TARGET_UNALIGNED_DOUBLES
4693 || CONSTANT_P (addr)
4694 || GET_CODE (addr) == LO_SUM)
4695 {
4696 /* Anything else we know is properly aligned unless TARGET_UNALIGNED_DOUBLES
4697 is true, in which case we can only assume that an access is aligned if
4698 it is to a constant address, or the address involves a LO_SUM. */
4699 return 1;
4700 }
4701
4702 /* An obviously unaligned address. */
4703 return 0;
4704 }
4705
4706 \f
4707 /* Vectors to keep interesting information about registers where it can easily
4708 be got. We used to use the actual mode value as the bit number, but there
4709 are more than 32 modes now. Instead we use two tables: one indexed by
4710 hard register number, and one indexed by mode. */
4711
4712 /* The purpose of sparc_mode_class is to shrink the range of modes so that
4713 they all fit (as bit numbers) in a 32-bit word (again). Each real mode is
4714 mapped into one sparc_mode_class mode. */
4715
4716 enum sparc_mode_class {
4717 H_MODE, S_MODE, D_MODE, T_MODE, O_MODE,
4718 SF_MODE, DF_MODE, TF_MODE, OF_MODE,
4719 CC_MODE, CCFP_MODE
4720 };
4721
4722 /* Modes for single-word and smaller quantities. */
4723 #define S_MODES \
4724 ((1 << (int) H_MODE) | (1 << (int) S_MODE) | (1 << (int) SF_MODE))
4725
4726 /* Modes for double-word and smaller quantities. */
4727 #define D_MODES (S_MODES | (1 << (int) D_MODE) | (1 << DF_MODE))
4728
4729 /* Modes for quad-word and smaller quantities. */
4730 #define T_MODES (D_MODES | (1 << (int) T_MODE) | (1 << (int) TF_MODE))
4731
4732 /* Modes for 8-word and smaller quantities. */
4733 #define O_MODES (T_MODES | (1 << (int) O_MODE) | (1 << (int) OF_MODE))
4734
4735 /* Modes for single-float quantities. */
4736 #define SF_MODES ((1 << (int) S_MODE) | (1 << (int) SF_MODE))
4737
4738 /* Modes for double-float and smaller quantities. */
4739 #define DF_MODES (SF_MODES | (1 << (int) D_MODE) | (1 << DF_MODE))
4740
4741 /* Modes for quad-float and smaller quantities. */
4742 #define TF_MODES (DF_MODES | (1 << (int) TF_MODE))
4743
4744 /* Modes for quad-float pairs and smaller quantities. */
4745 #define OF_MODES (TF_MODES | (1 << (int) OF_MODE))
4746
4747 /* Modes for double-float only quantities. */
4748 #define DF_MODES_NO_S ((1 << (int) D_MODE) | (1 << (int) DF_MODE))
4749
4750 /* Modes for quad-float and double-float only quantities. */
4751 #define TF_MODES_NO_S (DF_MODES_NO_S | (1 << (int) TF_MODE))
4752
4753 /* Modes for quad-float pairs and double-float only quantities. */
4754 #define OF_MODES_NO_S (TF_MODES_NO_S | (1 << (int) OF_MODE))
4755
4756 /* Modes for condition codes. */
4757 #define CC_MODES (1 << (int) CC_MODE)
4758 #define CCFP_MODES (1 << (int) CCFP_MODE)
4759
4760 /* Value is 1 if register/mode pair is acceptable on sparc.
4761 The funny mixture of D and T modes is because integer operations
4762 do not specially operate on tetra quantities, so non-quad-aligned
4763 registers can hold quadword quantities (except %o4 and %i4 because
4764 they cross fixed registers). */
4765
4766 /* This points to either the 32 bit or the 64 bit version. */
4767 const int *hard_regno_mode_classes;
4768
4769 static const int hard_32bit_mode_classes[] = {
4770 S_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
4771 T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
4772 T_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
4773 T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
4774
4775 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4776 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4777 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4778 OF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
4779
4780 /* FP regs f32 to f63. Only the even numbered registers actually exist,
4781 and none can hold SFmode/SImode values. */
4782 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4783 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4784 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4785 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, TF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4786
4787 /* %fcc[0123] */
4788 CCFP_MODES, CCFP_MODES, CCFP_MODES, CCFP_MODES,
4789
4790 /* %icc, %sfp, %gsr */
4791 CC_MODES, 0, D_MODES
4792 };
4793
4794 static const int hard_64bit_mode_classes[] = {
4795 D_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
4796 O_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
4797 T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
4798 O_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
4799
4800 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4801 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4802 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
4803 OF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
4804
4805 /* FP regs f32 to f63. Only the even numbered registers actually exist,
4806 and none can hold SFmode/SImode values. */
4807 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4808 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4809 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4810 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, TF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
4811
4812 /* %fcc[0123] */
4813 CCFP_MODES, CCFP_MODES, CCFP_MODES, CCFP_MODES,
4814
4815 /* %icc, %sfp, %gsr */
4816 CC_MODES, 0, D_MODES
4817 };
4818
4819 int sparc_mode_class [NUM_MACHINE_MODES];
4820
4821 enum reg_class sparc_regno_reg_class[FIRST_PSEUDO_REGISTER];
4822
4823 static void
4824 sparc_init_modes (void)
4825 {
4826 int i;
4827
4828 for (i = 0; i < NUM_MACHINE_MODES; i++)
4829 {
4830 enum machine_mode m = (enum machine_mode) i;
4831 unsigned int size = GET_MODE_SIZE (m);
4832
4833 switch (GET_MODE_CLASS (m))
4834 {
4835 case MODE_INT:
4836 case MODE_PARTIAL_INT:
4837 case MODE_COMPLEX_INT:
4838 if (size < 4)
4839 sparc_mode_class[i] = 1 << (int) H_MODE;
4840 else if (size == 4)
4841 sparc_mode_class[i] = 1 << (int) S_MODE;
4842 else if (size == 8)
4843 sparc_mode_class[i] = 1 << (int) D_MODE;
4844 else if (size == 16)
4845 sparc_mode_class[i] = 1 << (int) T_MODE;
4846 else if (size == 32)
4847 sparc_mode_class[i] = 1 << (int) O_MODE;
4848 else
4849 sparc_mode_class[i] = 0;
4850 break;
4851 case MODE_VECTOR_INT:
4852 if (size == 4)
4853 sparc_mode_class[i] = 1 << (int) SF_MODE;
4854 else if (size == 8)
4855 sparc_mode_class[i] = 1 << (int) DF_MODE;
4856 else
4857 sparc_mode_class[i] = 0;
4858 break;
4859 case MODE_FLOAT:
4860 case MODE_COMPLEX_FLOAT:
4861 if (size == 4)
4862 sparc_mode_class[i] = 1 << (int) SF_MODE;
4863 else if (size == 8)
4864 sparc_mode_class[i] = 1 << (int) DF_MODE;
4865 else if (size == 16)
4866 sparc_mode_class[i] = 1 << (int) TF_MODE;
4867 else if (size == 32)
4868 sparc_mode_class[i] = 1 << (int) OF_MODE;
4869 else
4870 sparc_mode_class[i] = 0;
4871 break;
4872 case MODE_CC:
4873 if (m == CCFPmode || m == CCFPEmode)
4874 sparc_mode_class[i] = 1 << (int) CCFP_MODE;
4875 else
4876 sparc_mode_class[i] = 1 << (int) CC_MODE;
4877 break;
4878 default:
4879 sparc_mode_class[i] = 0;
4880 break;
4881 }
4882 }
4883
4884 if (TARGET_ARCH64)
4885 hard_regno_mode_classes = hard_64bit_mode_classes;
4886 else
4887 hard_regno_mode_classes = hard_32bit_mode_classes;
4888
4889 /* Initialize the array used by REGNO_REG_CLASS. */
4890 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4891 {
4892 if (i < 16 && TARGET_V8PLUS)
4893 sparc_regno_reg_class[i] = I64_REGS;
4894 else if (i < 32 || i == FRAME_POINTER_REGNUM)
4895 sparc_regno_reg_class[i] = GENERAL_REGS;
4896 else if (i < 64)
4897 sparc_regno_reg_class[i] = FP_REGS;
4898 else if (i < 96)
4899 sparc_regno_reg_class[i] = EXTRA_FP_REGS;
4900 else if (i < 100)
4901 sparc_regno_reg_class[i] = FPCC_REGS;
4902 else
4903 sparc_regno_reg_class[i] = NO_REGS;
4904 }
4905 }
4906 \f
4907 /* Return whether REGNO, a global or FP register, must be saved/restored. */
4908
4909 static inline bool
4910 save_global_or_fp_reg_p (unsigned int regno,
4911 int leaf_function ATTRIBUTE_UNUSED)
4912 {
4913 return !call_used_regs[regno] && df_regs_ever_live_p (regno);
4914 }
4915
4916 /* Return whether the return address register (%i7) is needed. */
4917
4918 static inline bool
4919 return_addr_reg_needed_p (int leaf_function)
4920 {
4921 /* If it is live, for example because of __builtin_return_address (0). */
4922 if (df_regs_ever_live_p (RETURN_ADDR_REGNUM))
4923 return true;
4924
4925 /* Otherwise, it is needed as save register if %o7 is clobbered. */
4926 if (!leaf_function
4927 /* Loading the GOT register clobbers %o7. */
4928 || crtl->uses_pic_offset_table
4929 || df_regs_ever_live_p (INCOMING_RETURN_ADDR_REGNUM))
4930 return true;
4931
4932 return false;
4933 }
4934
4935 /* Return whether REGNO, a local or in register, must be saved/restored. */
4936
4937 static bool
4938 save_local_or_in_reg_p (unsigned int regno, int leaf_function)
4939 {
4940 /* General case: call-saved registers live at some point. */
4941 if (!call_used_regs[regno] && df_regs_ever_live_p (regno))
4942 return true;
4943
4944 /* Frame pointer register (%fp) if needed. */
4945 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
4946 return true;
4947
4948 /* Return address register (%i7) if needed. */
4949 if (regno == RETURN_ADDR_REGNUM && return_addr_reg_needed_p (leaf_function))
4950 return true;
4951
4952 /* GOT register (%l7) if needed. */
4953 if (regno == PIC_OFFSET_TABLE_REGNUM && crtl->uses_pic_offset_table)
4954 return true;
4955
4956 /* If the function accesses prior frames, the frame pointer and the return
4957 address of the previous frame must be saved on the stack. */
4958 if (crtl->accesses_prior_frames
4959 && (regno == HARD_FRAME_POINTER_REGNUM || regno == RETURN_ADDR_REGNUM))
4960 return true;
4961
4962 return false;
4963 }
4964
4965 /* Compute the frame size required by the function. This function is called
4966 during the reload pass and also by sparc_expand_prologue. */
4967
4968 HOST_WIDE_INT
4969 sparc_compute_frame_size (HOST_WIDE_INT size, int leaf_function)
4970 {
4971 HOST_WIDE_INT frame_size, apparent_frame_size;
4972 int args_size, n_global_fp_regs = 0;
4973 bool save_local_in_regs_p = false;
4974 unsigned int i;
4975
4976 /* If the function allocates dynamic stack space, the dynamic offset is
4977 computed early and contains REG_PARM_STACK_SPACE, so we need to cope. */
4978 if (leaf_function && !cfun->calls_alloca)
4979 args_size = 0;
4980 else
4981 args_size = crtl->outgoing_args_size + REG_PARM_STACK_SPACE (cfun->decl);
4982
4983 /* Calculate space needed for global registers. */
4984 if (TARGET_ARCH64)
4985 for (i = 0; i < 8; i++)
4986 if (save_global_or_fp_reg_p (i, 0))
4987 n_global_fp_regs += 2;
4988 else
4989 for (i = 0; i < 8; i += 2)
4990 if (save_global_or_fp_reg_p (i, 0) || save_global_or_fp_reg_p (i + 1, 0))
4991 n_global_fp_regs += 2;
4992
4993 /* In the flat window model, find out which local and in registers need to
4994 be saved. We don't reserve space in the current frame for them as they
4995 will be spilled into the register window save area of the caller's frame.
4996 However, as soon as we use this register window save area, we must create
4997 that of the current frame to make it the live one. */
4998 if (TARGET_FLAT)
4999 for (i = 16; i < 32; i++)
5000 if (save_local_or_in_reg_p (i, leaf_function))
5001 {
5002 save_local_in_regs_p = true;
5003 break;
5004 }
5005
5006 /* Calculate space needed for FP registers. */
5007 for (i = 32; i < (TARGET_V9 ? 96 : 64); i += 2)
5008 if (save_global_or_fp_reg_p (i, 0) || save_global_or_fp_reg_p (i + 1, 0))
5009 n_global_fp_regs += 2;
5010
5011 if (size == 0
5012 && n_global_fp_regs == 0
5013 && args_size == 0
5014 && !save_local_in_regs_p)
5015 frame_size = apparent_frame_size = 0;
5016 else
5017 {
5018 /* We subtract STARTING_FRAME_OFFSET, remember it's negative. */
5019 apparent_frame_size = (size - STARTING_FRAME_OFFSET + 7) & -8;
5020 apparent_frame_size += n_global_fp_regs * 4;
5021
5022 /* We need to add the size of the outgoing argument area. */
5023 frame_size = apparent_frame_size + ((args_size + 7) & -8);
5024
5025 /* And that of the register window save area. */
5026 frame_size += FIRST_PARM_OFFSET (cfun->decl);
5027
5028 /* Finally, bump to the appropriate alignment. */
5029 frame_size = SPARC_STACK_ALIGN (frame_size);
5030 }
5031
5032 /* Set up values for use in prologue and epilogue. */
5033 sparc_frame_size = frame_size;
5034 sparc_apparent_frame_size = apparent_frame_size;
5035 sparc_n_global_fp_regs = n_global_fp_regs;
5036 sparc_save_local_in_regs_p = save_local_in_regs_p;
5037
5038 return frame_size;
5039 }
5040
5041 /* Implement the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
5042
5043 int
5044 sparc_initial_elimination_offset (int to)
5045 {
5046 int offset;
5047
5048 if (to == STACK_POINTER_REGNUM)
5049 offset = sparc_compute_frame_size (get_frame_size (), crtl->is_leaf);
5050 else
5051 offset = 0;
5052
5053 offset += SPARC_STACK_BIAS;
5054 return offset;
5055 }
5056
5057 /* Output any necessary .register pseudo-ops. */
5058
5059 void
5060 sparc_output_scratch_registers (FILE *file ATTRIBUTE_UNUSED)
5061 {
5062 #ifdef HAVE_AS_REGISTER_PSEUDO_OP
5063 int i;
5064
5065 if (TARGET_ARCH32)
5066 return;
5067
5068 /* Check if %g[2367] were used without
5069 .register being printed for them already. */
5070 for (i = 2; i < 8; i++)
5071 {
5072 if (df_regs_ever_live_p (i)
5073 && ! sparc_hard_reg_printed [i])
5074 {
5075 sparc_hard_reg_printed [i] = 1;
5076 /* %g7 is used as TLS base register, use #ignore
5077 for it instead of #scratch. */
5078 fprintf (file, "\t.register\t%%g%d, #%s\n", i,
5079 i == 7 ? "ignore" : "scratch");
5080 }
5081 if (i == 3) i = 5;
5082 }
5083 #endif
5084 }
5085
5086 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
5087
5088 #if PROBE_INTERVAL > 4096
5089 #error Cannot use indexed addressing mode for stack probing
5090 #endif
5091
5092 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
5093 inclusive. These are offsets from the current stack pointer.
5094
5095 Note that we don't use the REG+REG addressing mode for the probes because
5096 of the stack bias in 64-bit mode. And it doesn't really buy us anything
5097 so the advantages of having a single code win here. */
5098
5099 static void
5100 sparc_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
5101 {
5102 rtx g1 = gen_rtx_REG (Pmode, 1);
5103
5104 /* See if we have a constant small number of probes to generate. If so,
5105 that's the easy case. */
5106 if (size <= PROBE_INTERVAL)
5107 {
5108 emit_move_insn (g1, GEN_INT (first));
5109 emit_insn (gen_rtx_SET (VOIDmode, g1,
5110 gen_rtx_MINUS (Pmode, stack_pointer_rtx, g1)));
5111 emit_stack_probe (plus_constant (Pmode, g1, -size));
5112 }
5113
5114 /* The run-time loop is made up of 10 insns in the generic case while the
5115 compile-time loop is made up of 4+2*(n-2) insns for n # of intervals. */
5116 else if (size <= 5 * PROBE_INTERVAL)
5117 {
5118 HOST_WIDE_INT i;
5119
5120 emit_move_insn (g1, GEN_INT (first + PROBE_INTERVAL));
5121 emit_insn (gen_rtx_SET (VOIDmode, g1,
5122 gen_rtx_MINUS (Pmode, stack_pointer_rtx, g1)));
5123 emit_stack_probe (g1);
5124
5125 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 2 until
5126 it exceeds SIZE. If only two probes are needed, this will not
5127 generate any code. Then probe at FIRST + SIZE. */
5128 for (i = 2 * PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
5129 {
5130 emit_insn (gen_rtx_SET (VOIDmode, g1,
5131 plus_constant (Pmode, g1, -PROBE_INTERVAL)));
5132 emit_stack_probe (g1);
5133 }
5134
5135 emit_stack_probe (plus_constant (Pmode, g1,
5136 (i - PROBE_INTERVAL) - size));
5137 }
5138
5139 /* Otherwise, do the same as above, but in a loop. Note that we must be
5140 extra careful with variables wrapping around because we might be at
5141 the very top (or the very bottom) of the address space and we have
5142 to be able to handle this case properly; in particular, we use an
5143 equality test for the loop condition. */
5144 else
5145 {
5146 HOST_WIDE_INT rounded_size;
5147 rtx g4 = gen_rtx_REG (Pmode, 4);
5148
5149 emit_move_insn (g1, GEN_INT (first));
5150
5151
5152 /* Step 1: round SIZE to the previous multiple of the interval. */
5153
5154 rounded_size = size & -PROBE_INTERVAL;
5155 emit_move_insn (g4, GEN_INT (rounded_size));
5156
5157
5158 /* Step 2: compute initial and final value of the loop counter. */
5159
5160 /* TEST_ADDR = SP + FIRST. */
5161 emit_insn (gen_rtx_SET (VOIDmode, g1,
5162 gen_rtx_MINUS (Pmode, stack_pointer_rtx, g1)));
5163
5164 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
5165 emit_insn (gen_rtx_SET (VOIDmode, g4, gen_rtx_MINUS (Pmode, g1, g4)));
5166
5167
5168 /* Step 3: the loop
5169
5170 while (TEST_ADDR != LAST_ADDR)
5171 {
5172 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
5173 probe at TEST_ADDR
5174 }
5175
5176 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
5177 until it is equal to ROUNDED_SIZE. */
5178
5179 if (TARGET_ARCH64)
5180 emit_insn (gen_probe_stack_rangedi (g1, g1, g4));
5181 else
5182 emit_insn (gen_probe_stack_rangesi (g1, g1, g4));
5183
5184
5185 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
5186 that SIZE is equal to ROUNDED_SIZE. */
5187
5188 if (size != rounded_size)
5189 emit_stack_probe (plus_constant (Pmode, g4, rounded_size - size));
5190 }
5191
5192 /* Make sure nothing is scheduled before we are done. */
5193 emit_insn (gen_blockage ());
5194 }
5195
5196 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
5197 absolute addresses. */
5198
5199 const char *
5200 output_probe_stack_range (rtx reg1, rtx reg2)
5201 {
5202 static int labelno = 0;
5203 char loop_lab[32], end_lab[32];
5204 rtx xops[2];
5205
5206 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
5207 ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
5208
5209 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
5210
5211 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
5212 xops[0] = reg1;
5213 xops[1] = reg2;
5214 output_asm_insn ("cmp\t%0, %1", xops);
5215 if (TARGET_ARCH64)
5216 fputs ("\tbe,pn\t%xcc,", asm_out_file);
5217 else
5218 fputs ("\tbe\t", asm_out_file);
5219 assemble_name_raw (asm_out_file, end_lab);
5220 fputc ('\n', asm_out_file);
5221
5222 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
5223 xops[1] = GEN_INT (-PROBE_INTERVAL);
5224 output_asm_insn (" add\t%0, %1, %0", xops);
5225
5226 /* Probe at TEST_ADDR and branch. */
5227 if (TARGET_ARCH64)
5228 fputs ("\tba,pt\t%xcc,", asm_out_file);
5229 else
5230 fputs ("\tba\t", asm_out_file);
5231 assemble_name_raw (asm_out_file, loop_lab);
5232 fputc ('\n', asm_out_file);
5233 xops[1] = GEN_INT (SPARC_STACK_BIAS);
5234 output_asm_insn (" st\t%%g0, [%0+%1]", xops);
5235
5236 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
5237
5238 return "";
5239 }
5240
5241 /* Emit code to save/restore registers from LOW to HIGH at BASE+OFFSET as
5242 needed. LOW is supposed to be double-word aligned for 32-bit registers.
5243 SAVE_P decides whether a register must be saved/restored. ACTION_TRUE
5244 is the action to be performed if SAVE_P returns true and ACTION_FALSE
5245 the action to be performed if it returns false. Return the new offset. */
5246
5247 typedef bool (*sorr_pred_t) (unsigned int, int);
5248 typedef enum { SORR_NONE, SORR_ADVANCE, SORR_SAVE, SORR_RESTORE } sorr_act_t;
5249
5250 static int
5251 emit_save_or_restore_regs (unsigned int low, unsigned int high, rtx base,
5252 int offset, int leaf_function, sorr_pred_t save_p,
5253 sorr_act_t action_true, sorr_act_t action_false)
5254 {
5255 unsigned int i;
5256 rtx mem;
5257 rtx_insn *insn;
5258
5259 if (TARGET_ARCH64 && high <= 32)
5260 {
5261 int fp_offset = -1;
5262
5263 for (i = low; i < high; i++)
5264 {
5265 if (save_p (i, leaf_function))
5266 {
5267 mem = gen_frame_mem (DImode, plus_constant (Pmode,
5268 base, offset));
5269 if (action_true == SORR_SAVE)
5270 {
5271 insn = emit_move_insn (mem, gen_rtx_REG (DImode, i));
5272 RTX_FRAME_RELATED_P (insn) = 1;
5273 }
5274 else /* action_true == SORR_RESTORE */
5275 {
5276 /* The frame pointer must be restored last since its old
5277 value may be used as base address for the frame. This
5278 is problematic in 64-bit mode only because of the lack
5279 of double-word load instruction. */
5280 if (i == HARD_FRAME_POINTER_REGNUM)
5281 fp_offset = offset;
5282 else
5283 emit_move_insn (gen_rtx_REG (DImode, i), mem);
5284 }
5285 offset += 8;
5286 }
5287 else if (action_false == SORR_ADVANCE)
5288 offset += 8;
5289 }
5290
5291 if (fp_offset >= 0)
5292 {
5293 mem = gen_frame_mem (DImode, plus_constant (Pmode, base, fp_offset));
5294 emit_move_insn (hard_frame_pointer_rtx, mem);
5295 }
5296 }
5297 else
5298 {
5299 for (i = low; i < high; i += 2)
5300 {
5301 bool reg0 = save_p (i, leaf_function);
5302 bool reg1 = save_p (i + 1, leaf_function);
5303 enum machine_mode mode;
5304 int regno;
5305
5306 if (reg0 && reg1)
5307 {
5308 mode = SPARC_INT_REG_P (i) ? DImode : DFmode;
5309 regno = i;
5310 }
5311 else if (reg0)
5312 {
5313 mode = SPARC_INT_REG_P (i) ? SImode : SFmode;
5314 regno = i;
5315 }
5316 else if (reg1)
5317 {
5318 mode = SPARC_INT_REG_P (i) ? SImode : SFmode;
5319 regno = i + 1;
5320 offset += 4;
5321 }
5322 else
5323 {
5324 if (action_false == SORR_ADVANCE)
5325 offset += 8;
5326 continue;
5327 }
5328
5329 mem = gen_frame_mem (mode, plus_constant (Pmode, base, offset));
5330 if (action_true == SORR_SAVE)
5331 {
5332 insn = emit_move_insn (mem, gen_rtx_REG (mode, regno));
5333 RTX_FRAME_RELATED_P (insn) = 1;
5334 if (mode == DImode)
5335 {
5336 rtx set1, set2;
5337 mem = gen_frame_mem (SImode, plus_constant (Pmode, base,
5338 offset));
5339 set1 = gen_rtx_SET (VOIDmode, mem,
5340 gen_rtx_REG (SImode, regno));
5341 RTX_FRAME_RELATED_P (set1) = 1;
5342 mem
5343 = gen_frame_mem (SImode, plus_constant (Pmode, base,
5344 offset + 4));
5345 set2 = gen_rtx_SET (VOIDmode, mem,
5346 gen_rtx_REG (SImode, regno + 1));
5347 RTX_FRAME_RELATED_P (set2) = 1;
5348 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
5349 gen_rtx_PARALLEL (VOIDmode,
5350 gen_rtvec (2, set1, set2)));
5351 }
5352 }
5353 else /* action_true == SORR_RESTORE */
5354 emit_move_insn (gen_rtx_REG (mode, regno), mem);
5355
5356 /* Always preserve double-word alignment. */
5357 offset = (offset + 8) & -8;
5358 }
5359 }
5360
5361 return offset;
5362 }
5363
5364 /* Emit code to adjust BASE to OFFSET. Return the new base. */
5365
5366 static rtx
5367 emit_adjust_base_to_offset (rtx base, int offset)
5368 {
5369 /* ??? This might be optimized a little as %g1 might already have a
5370 value close enough that a single add insn will do. */
5371 /* ??? Although, all of this is probably only a temporary fix because
5372 if %g1 can hold a function result, then sparc_expand_epilogue will
5373 lose (the result will be clobbered). */
5374 rtx new_base = gen_rtx_REG (Pmode, 1);
5375 emit_move_insn (new_base, GEN_INT (offset));
5376 emit_insn (gen_rtx_SET (VOIDmode,
5377 new_base, gen_rtx_PLUS (Pmode, base, new_base)));
5378 return new_base;
5379 }
5380
5381 /* Emit code to save/restore call-saved global and FP registers. */
5382
5383 static void
5384 emit_save_or_restore_global_fp_regs (rtx base, int offset, sorr_act_t action)
5385 {
5386 if (offset < -4096 || offset + sparc_n_global_fp_regs * 4 > 4095)
5387 {
5388 base = emit_adjust_base_to_offset (base, offset);
5389 offset = 0;
5390 }
5391
5392 offset
5393 = emit_save_or_restore_regs (0, 8, base, offset, 0,
5394 save_global_or_fp_reg_p, action, SORR_NONE);
5395 emit_save_or_restore_regs (32, TARGET_V9 ? 96 : 64, base, offset, 0,
5396 save_global_or_fp_reg_p, action, SORR_NONE);
5397 }
5398
5399 /* Emit code to save/restore call-saved local and in registers. */
5400
5401 static void
5402 emit_save_or_restore_local_in_regs (rtx base, int offset, sorr_act_t action)
5403 {
5404 if (offset < -4096 || offset + 16 * UNITS_PER_WORD > 4095)
5405 {
5406 base = emit_adjust_base_to_offset (base, offset);
5407 offset = 0;
5408 }
5409
5410 emit_save_or_restore_regs (16, 32, base, offset, sparc_leaf_function_p,
5411 save_local_or_in_reg_p, action, SORR_ADVANCE);
5412 }
5413
5414 /* Emit a window_save insn. */
5415
5416 static rtx_insn *
5417 emit_window_save (rtx increment)
5418 {
5419 rtx_insn *insn = emit_insn (gen_window_save (increment));
5420 RTX_FRAME_RELATED_P (insn) = 1;
5421
5422 /* The incoming return address (%o7) is saved in %i7. */
5423 add_reg_note (insn, REG_CFA_REGISTER,
5424 gen_rtx_SET (VOIDmode,
5425 gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM),
5426 gen_rtx_REG (Pmode,
5427 INCOMING_RETURN_ADDR_REGNUM)));
5428
5429 /* The window save event. */
5430 add_reg_note (insn, REG_CFA_WINDOW_SAVE, const0_rtx);
5431
5432 /* The CFA is %fp, the hard frame pointer. */
5433 add_reg_note (insn, REG_CFA_DEF_CFA,
5434 plus_constant (Pmode, hard_frame_pointer_rtx,
5435 INCOMING_FRAME_SP_OFFSET));
5436
5437 return insn;
5438 }
5439
5440 /* Generate an increment for the stack pointer. */
5441
5442 static rtx
5443 gen_stack_pointer_inc (rtx increment)
5444 {
5445 return gen_rtx_SET (VOIDmode,
5446 stack_pointer_rtx,
5447 gen_rtx_PLUS (Pmode,
5448 stack_pointer_rtx,
5449 increment));
5450 }
5451
5452 /* Expand the function prologue. The prologue is responsible for reserving
5453 storage for the frame, saving the call-saved registers and loading the
5454 GOT register if needed. */
5455
5456 void
5457 sparc_expand_prologue (void)
5458 {
5459 HOST_WIDE_INT size;
5460 rtx_insn *insn;
5461
5462 /* Compute a snapshot of crtl->uses_only_leaf_regs. Relying
5463 on the final value of the flag means deferring the prologue/epilogue
5464 expansion until just before the second scheduling pass, which is too
5465 late to emit multiple epilogues or return insns.
5466
5467 Of course we are making the assumption that the value of the flag
5468 will not change between now and its final value. Of the three parts
5469 of the formula, only the last one can reasonably vary. Let's take a
5470 closer look, after assuming that the first two ones are set to true
5471 (otherwise the last value is effectively silenced).
5472
5473 If only_leaf_regs_used returns false, the global predicate will also
5474 be false so the actual frame size calculated below will be positive.
5475 As a consequence, the save_register_window insn will be emitted in
5476 the instruction stream; now this insn explicitly references %fp
5477 which is not a leaf register so only_leaf_regs_used will always
5478 return false subsequently.
5479
5480 If only_leaf_regs_used returns true, we hope that the subsequent
5481 optimization passes won't cause non-leaf registers to pop up. For
5482 example, the regrename pass has special provisions to not rename to
5483 non-leaf registers in a leaf function. */
5484 sparc_leaf_function_p
5485 = optimize > 0 && crtl->is_leaf && only_leaf_regs_used ();
5486
5487 size = sparc_compute_frame_size (get_frame_size(), sparc_leaf_function_p);
5488
5489 if (flag_stack_usage_info)
5490 current_function_static_stack_size = size;
5491
5492 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
5493 {
5494 if (crtl->is_leaf && !cfun->calls_alloca)
5495 {
5496 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
5497 sparc_emit_probe_stack_range (STACK_CHECK_PROTECT,
5498 size - STACK_CHECK_PROTECT);
5499 }
5500 else if (size > 0)
5501 sparc_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
5502 }
5503
5504 if (size == 0)
5505 ; /* do nothing. */
5506 else if (sparc_leaf_function_p)
5507 {
5508 rtx size_int_rtx = GEN_INT (-size);
5509
5510 if (size <= 4096)
5511 insn = emit_insn (gen_stack_pointer_inc (size_int_rtx));
5512 else if (size <= 8192)
5513 {
5514 insn = emit_insn (gen_stack_pointer_inc (GEN_INT (-4096)));
5515 RTX_FRAME_RELATED_P (insn) = 1;
5516
5517 /* %sp is still the CFA register. */
5518 insn = emit_insn (gen_stack_pointer_inc (GEN_INT (4096 - size)));
5519 }
5520 else
5521 {
5522 rtx size_rtx = gen_rtx_REG (Pmode, 1);
5523 emit_move_insn (size_rtx, size_int_rtx);
5524 insn = emit_insn (gen_stack_pointer_inc (size_rtx));
5525 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
5526 gen_stack_pointer_inc (size_int_rtx));
5527 }
5528
5529 RTX_FRAME_RELATED_P (insn) = 1;
5530 }
5531 else
5532 {
5533 rtx size_int_rtx = GEN_INT (-size);
5534
5535 if (size <= 4096)
5536 emit_window_save (size_int_rtx);
5537 else if (size <= 8192)
5538 {
5539 emit_window_save (GEN_INT (-4096));
5540
5541 /* %sp is not the CFA register anymore. */
5542 emit_insn (gen_stack_pointer_inc (GEN_INT (4096 - size)));
5543
5544 /* Make sure no %fp-based store is issued until after the frame is
5545 established. The offset between the frame pointer and the stack
5546 pointer is calculated relative to the value of the stack pointer
5547 at the end of the function prologue, and moving instructions that
5548 access the stack via the frame pointer between the instructions
5549 that decrement the stack pointer could result in accessing the
5550 register window save area, which is volatile. */
5551 emit_insn (gen_frame_blockage ());
5552 }
5553 else
5554 {
5555 rtx size_rtx = gen_rtx_REG (Pmode, 1);
5556 emit_move_insn (size_rtx, size_int_rtx);
5557 emit_window_save (size_rtx);
5558 }
5559 }
5560
5561 if (sparc_leaf_function_p)
5562 {
5563 sparc_frame_base_reg = stack_pointer_rtx;
5564 sparc_frame_base_offset = size + SPARC_STACK_BIAS;
5565 }
5566 else
5567 {
5568 sparc_frame_base_reg = hard_frame_pointer_rtx;
5569 sparc_frame_base_offset = SPARC_STACK_BIAS;
5570 }
5571
5572 if (sparc_n_global_fp_regs > 0)
5573 emit_save_or_restore_global_fp_regs (sparc_frame_base_reg,
5574 sparc_frame_base_offset
5575 - sparc_apparent_frame_size,
5576 SORR_SAVE);
5577
5578 /* Load the GOT register if needed. */
5579 if (crtl->uses_pic_offset_table)
5580 load_got_register ();
5581
5582 /* Advertise that the data calculated just above are now valid. */
5583 sparc_prologue_data_valid_p = true;
5584 }
5585
5586 /* Expand the function prologue. The prologue is responsible for reserving
5587 storage for the frame, saving the call-saved registers and loading the
5588 GOT register if needed. */
5589
5590 void
5591 sparc_flat_expand_prologue (void)
5592 {
5593 HOST_WIDE_INT size;
5594 rtx_insn *insn;
5595
5596 sparc_leaf_function_p = optimize > 0 && crtl->is_leaf;
5597
5598 size = sparc_compute_frame_size (get_frame_size(), sparc_leaf_function_p);
5599
5600 if (flag_stack_usage_info)
5601 current_function_static_stack_size = size;
5602
5603 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
5604 {
5605 if (crtl->is_leaf && !cfun->calls_alloca)
5606 {
5607 if (size > PROBE_INTERVAL && size > STACK_CHECK_PROTECT)
5608 sparc_emit_probe_stack_range (STACK_CHECK_PROTECT,
5609 size - STACK_CHECK_PROTECT);
5610 }
5611 else if (size > 0)
5612 sparc_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
5613 }
5614
5615 if (sparc_save_local_in_regs_p)
5616 emit_save_or_restore_local_in_regs (stack_pointer_rtx, SPARC_STACK_BIAS,
5617 SORR_SAVE);
5618
5619 if (size == 0)
5620 ; /* do nothing. */
5621 else
5622 {
5623 rtx size_int_rtx, size_rtx;
5624
5625 size_rtx = size_int_rtx = GEN_INT (-size);
5626
5627 /* We establish the frame (i.e. decrement the stack pointer) first, even
5628 if we use a frame pointer, because we cannot clobber any call-saved
5629 registers, including the frame pointer, if we haven't created a new
5630 register save area, for the sake of compatibility with the ABI. */
5631 if (size <= 4096)
5632 insn = emit_insn (gen_stack_pointer_inc (size_int_rtx));
5633 else if (size <= 8192 && !frame_pointer_needed)
5634 {
5635 insn = emit_insn (gen_stack_pointer_inc (GEN_INT (-4096)));
5636 RTX_FRAME_RELATED_P (insn) = 1;
5637 insn = emit_insn (gen_stack_pointer_inc (GEN_INT (4096 - size)));
5638 }
5639 else
5640 {
5641 size_rtx = gen_rtx_REG (Pmode, 1);
5642 emit_move_insn (size_rtx, size_int_rtx);
5643 insn = emit_insn (gen_stack_pointer_inc (size_rtx));
5644 add_reg_note (insn, REG_CFA_ADJUST_CFA,
5645 gen_stack_pointer_inc (size_int_rtx));
5646 }
5647 RTX_FRAME_RELATED_P (insn) = 1;
5648
5649 /* Ensure nothing is scheduled until after the frame is established. */
5650 emit_insn (gen_blockage ());
5651
5652 if (frame_pointer_needed)
5653 {
5654 insn = emit_insn (gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
5655 gen_rtx_MINUS (Pmode,
5656 stack_pointer_rtx,
5657 size_rtx)));
5658 RTX_FRAME_RELATED_P (insn) = 1;
5659
5660 add_reg_note (insn, REG_CFA_ADJUST_CFA,
5661 gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
5662 plus_constant (Pmode, stack_pointer_rtx,
5663 size)));
5664 }
5665
5666 if (return_addr_reg_needed_p (sparc_leaf_function_p))
5667 {
5668 rtx o7 = gen_rtx_REG (Pmode, INCOMING_RETURN_ADDR_REGNUM);
5669 rtx i7 = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
5670
5671 insn = emit_move_insn (i7, o7);
5672 RTX_FRAME_RELATED_P (insn) = 1;
5673
5674 add_reg_note (insn, REG_CFA_REGISTER,
5675 gen_rtx_SET (VOIDmode, i7, o7));
5676
5677 /* Prevent this instruction from ever being considered dead,
5678 even if this function has no epilogue. */
5679 emit_use (i7);
5680 }
5681 }
5682
5683 if (frame_pointer_needed)
5684 {
5685 sparc_frame_base_reg = hard_frame_pointer_rtx;
5686 sparc_frame_base_offset = SPARC_STACK_BIAS;
5687 }
5688 else
5689 {
5690 sparc_frame_base_reg = stack_pointer_rtx;
5691 sparc_frame_base_offset = size + SPARC_STACK_BIAS;
5692 }
5693
5694 if (sparc_n_global_fp_regs > 0)
5695 emit_save_or_restore_global_fp_regs (sparc_frame_base_reg,
5696 sparc_frame_base_offset
5697 - sparc_apparent_frame_size,
5698 SORR_SAVE);
5699
5700 /* Load the GOT register if needed. */
5701 if (crtl->uses_pic_offset_table)
5702 load_got_register ();
5703
5704 /* Advertise that the data calculated just above are now valid. */
5705 sparc_prologue_data_valid_p = true;
5706 }
5707
5708 /* This function generates the assembly code for function entry, which boils
5709 down to emitting the necessary .register directives. */
5710
5711 static void
5712 sparc_asm_function_prologue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
5713 {
5714 /* Check that the assumption we made in sparc_expand_prologue is valid. */
5715 if (!TARGET_FLAT)
5716 gcc_assert (sparc_leaf_function_p == crtl->uses_only_leaf_regs);
5717
5718 sparc_output_scratch_registers (file);
5719 }
5720
5721 /* Expand the function epilogue, either normal or part of a sibcall.
5722 We emit all the instructions except the return or the call. */
5723
5724 void
5725 sparc_expand_epilogue (bool for_eh)
5726 {
5727 HOST_WIDE_INT size = sparc_frame_size;
5728
5729 if (sparc_n_global_fp_regs > 0)
5730 emit_save_or_restore_global_fp_regs (sparc_frame_base_reg,
5731 sparc_frame_base_offset
5732 - sparc_apparent_frame_size,
5733 SORR_RESTORE);
5734
5735 if (size == 0 || for_eh)
5736 ; /* do nothing. */
5737 else if (sparc_leaf_function_p)
5738 {
5739 if (size <= 4096)
5740 emit_insn (gen_stack_pointer_inc (GEN_INT (size)));
5741 else if (size <= 8192)
5742 {
5743 emit_insn (gen_stack_pointer_inc (GEN_INT (4096)));
5744 emit_insn (gen_stack_pointer_inc (GEN_INT (size - 4096)));
5745 }
5746 else
5747 {
5748 rtx reg = gen_rtx_REG (Pmode, 1);
5749 emit_move_insn (reg, GEN_INT (size));
5750 emit_insn (gen_stack_pointer_inc (reg));
5751 }
5752 }
5753 }
5754
5755 /* Expand the function epilogue, either normal or part of a sibcall.
5756 We emit all the instructions except the return or the call. */
5757
5758 void
5759 sparc_flat_expand_epilogue (bool for_eh)
5760 {
5761 HOST_WIDE_INT size = sparc_frame_size;
5762
5763 if (sparc_n_global_fp_regs > 0)
5764 emit_save_or_restore_global_fp_regs (sparc_frame_base_reg,
5765 sparc_frame_base_offset
5766 - sparc_apparent_frame_size,
5767 SORR_RESTORE);
5768
5769 /* If we have a frame pointer, we'll need both to restore it before the
5770 frame is destroyed and use its current value in destroying the frame.
5771 Since we don't have an atomic way to do that in the flat window model,
5772 we save the current value into a temporary register (%g1). */
5773 if (frame_pointer_needed && !for_eh)
5774 emit_move_insn (gen_rtx_REG (Pmode, 1), hard_frame_pointer_rtx);
5775
5776 if (return_addr_reg_needed_p (sparc_leaf_function_p))
5777 emit_move_insn (gen_rtx_REG (Pmode, INCOMING_RETURN_ADDR_REGNUM),
5778 gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM));
5779
5780 if (sparc_save_local_in_regs_p)
5781 emit_save_or_restore_local_in_regs (sparc_frame_base_reg,
5782 sparc_frame_base_offset,
5783 SORR_RESTORE);
5784
5785 if (size == 0 || for_eh)
5786 ; /* do nothing. */
5787 else if (frame_pointer_needed)
5788 {
5789 /* Make sure the frame is destroyed after everything else is done. */
5790 emit_insn (gen_blockage ());
5791
5792 emit_move_insn (stack_pointer_rtx, gen_rtx_REG (Pmode, 1));
5793 }
5794 else
5795 {
5796 /* Likewise. */
5797 emit_insn (gen_blockage ());
5798
5799 if (size <= 4096)
5800 emit_insn (gen_stack_pointer_inc (GEN_INT (size)));
5801 else if (size <= 8192)
5802 {
5803 emit_insn (gen_stack_pointer_inc (GEN_INT (4096)));
5804 emit_insn (gen_stack_pointer_inc (GEN_INT (size - 4096)));
5805 }
5806 else
5807 {
5808 rtx reg = gen_rtx_REG (Pmode, 1);
5809 emit_move_insn (reg, GEN_INT (size));
5810 emit_insn (gen_stack_pointer_inc (reg));
5811 }
5812 }
5813 }
5814
5815 /* Return true if it is appropriate to emit `return' instructions in the
5816 body of a function. */
5817
5818 bool
5819 sparc_can_use_return_insn_p (void)
5820 {
5821 return sparc_prologue_data_valid_p
5822 && sparc_n_global_fp_regs == 0
5823 && TARGET_FLAT
5824 ? (sparc_frame_size == 0 && !sparc_save_local_in_regs_p)
5825 : (sparc_frame_size == 0 || !sparc_leaf_function_p);
5826 }
5827
5828 /* This function generates the assembly code for function exit. */
5829
5830 static void
5831 sparc_asm_function_epilogue (FILE *file, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
5832 {
5833 /* If the last two instructions of a function are "call foo; dslot;"
5834 the return address might point to the first instruction in the next
5835 function and we have to output a dummy nop for the sake of sane
5836 backtraces in such cases. This is pointless for sibling calls since
5837 the return address is explicitly adjusted. */
5838
5839 rtx insn, last_real_insn;
5840
5841 insn = get_last_insn ();
5842
5843 last_real_insn = prev_real_insn (insn);
5844 if (last_real_insn
5845 && NONJUMP_INSN_P (last_real_insn)
5846 && GET_CODE (PATTERN (last_real_insn)) == SEQUENCE)
5847 last_real_insn = XVECEXP (PATTERN (last_real_insn), 0, 0);
5848
5849 if (last_real_insn
5850 && CALL_P (last_real_insn)
5851 && !SIBLING_CALL_P (last_real_insn))
5852 fputs("\tnop\n", file);
5853
5854 sparc_output_deferred_case_vectors ();
5855 }
5856
5857 /* Output a 'restore' instruction. */
5858
5859 static void
5860 output_restore (rtx pat)
5861 {
5862 rtx operands[3];
5863
5864 if (! pat)
5865 {
5866 fputs ("\t restore\n", asm_out_file);
5867 return;
5868 }
5869
5870 gcc_assert (GET_CODE (pat) == SET);
5871
5872 operands[0] = SET_DEST (pat);
5873 pat = SET_SRC (pat);
5874
5875 switch (GET_CODE (pat))
5876 {
5877 case PLUS:
5878 operands[1] = XEXP (pat, 0);
5879 operands[2] = XEXP (pat, 1);
5880 output_asm_insn (" restore %r1, %2, %Y0", operands);
5881 break;
5882 case LO_SUM:
5883 operands[1] = XEXP (pat, 0);
5884 operands[2] = XEXP (pat, 1);
5885 output_asm_insn (" restore %r1, %%lo(%a2), %Y0", operands);
5886 break;
5887 case ASHIFT:
5888 operands[1] = XEXP (pat, 0);
5889 gcc_assert (XEXP (pat, 1) == const1_rtx);
5890 output_asm_insn (" restore %r1, %r1, %Y0", operands);
5891 break;
5892 default:
5893 operands[1] = pat;
5894 output_asm_insn (" restore %%g0, %1, %Y0", operands);
5895 break;
5896 }
5897 }
5898
5899 /* Output a return. */
5900
5901 const char *
5902 output_return (rtx_insn *insn)
5903 {
5904 if (crtl->calls_eh_return)
5905 {
5906 /* If the function uses __builtin_eh_return, the eh_return
5907 machinery occupies the delay slot. */
5908 gcc_assert (!final_sequence);
5909
5910 if (flag_delayed_branch)
5911 {
5912 if (!TARGET_FLAT && TARGET_V9)
5913 fputs ("\treturn\t%i7+8\n", asm_out_file);
5914 else
5915 {
5916 if (!TARGET_FLAT)
5917 fputs ("\trestore\n", asm_out_file);
5918
5919 fputs ("\tjmp\t%o7+8\n", asm_out_file);
5920 }
5921
5922 fputs ("\t add\t%sp, %g1, %sp\n", asm_out_file);
5923 }
5924 else
5925 {
5926 if (!TARGET_FLAT)
5927 fputs ("\trestore\n", asm_out_file);
5928
5929 fputs ("\tadd\t%sp, %g1, %sp\n", asm_out_file);
5930 fputs ("\tjmp\t%o7+8\n\t nop\n", asm_out_file);
5931 }
5932 }
5933 else if (sparc_leaf_function_p || TARGET_FLAT)
5934 {
5935 /* This is a leaf or flat function so we don't have to bother restoring
5936 the register window, which frees us from dealing with the convoluted
5937 semantics of restore/return. We simply output the jump to the
5938 return address and the insn in the delay slot (if any). */
5939
5940 return "jmp\t%%o7+%)%#";
5941 }
5942 else
5943 {
5944 /* This is a regular function so we have to restore the register window.
5945 We may have a pending insn for the delay slot, which will be either
5946 combined with the 'restore' instruction or put in the delay slot of
5947 the 'return' instruction. */
5948
5949 if (final_sequence)
5950 {
5951 rtx delay, pat;
5952
5953 delay = NEXT_INSN (insn);
5954 gcc_assert (delay);
5955
5956 pat = PATTERN (delay);
5957
5958 if (TARGET_V9 && ! epilogue_renumber (&pat, 1))
5959 {
5960 epilogue_renumber (&pat, 0);
5961 return "return\t%%i7+%)%#";
5962 }
5963 else
5964 {
5965 output_asm_insn ("jmp\t%%i7+%)", NULL);
5966 output_restore (pat);
5967 PATTERN (delay) = gen_blockage ();
5968 INSN_CODE (delay) = -1;
5969 }
5970 }
5971 else
5972 {
5973 /* The delay slot is empty. */
5974 if (TARGET_V9)
5975 return "return\t%%i7+%)\n\t nop";
5976 else if (flag_delayed_branch)
5977 return "jmp\t%%i7+%)\n\t restore";
5978 else
5979 return "restore\n\tjmp\t%%o7+%)\n\t nop";
5980 }
5981 }
5982
5983 return "";
5984 }
5985
5986 /* Output a sibling call. */
5987
5988 const char *
5989 output_sibcall (rtx_insn *insn, rtx call_operand)
5990 {
5991 rtx operands[1];
5992
5993 gcc_assert (flag_delayed_branch);
5994
5995 operands[0] = call_operand;
5996
5997 if (sparc_leaf_function_p || TARGET_FLAT)
5998 {
5999 /* This is a leaf or flat function so we don't have to bother restoring
6000 the register window. We simply output the jump to the function and
6001 the insn in the delay slot (if any). */
6002
6003 gcc_assert (!(LEAF_SIBCALL_SLOT_RESERVED_P && final_sequence));
6004
6005 if (final_sequence)
6006 output_asm_insn ("sethi\t%%hi(%a0), %%g1\n\tjmp\t%%g1 + %%lo(%a0)%#",
6007 operands);
6008 else
6009 /* Use or with rs2 %%g0 instead of mov, so that as/ld can optimize
6010 it into branch if possible. */
6011 output_asm_insn ("or\t%%o7, %%g0, %%g1\n\tcall\t%a0, 0\n\t or\t%%g1, %%g0, %%o7",
6012 operands);
6013 }
6014 else
6015 {
6016 /* This is a regular function so we have to restore the register window.
6017 We may have a pending insn for the delay slot, which will be combined
6018 with the 'restore' instruction. */
6019
6020 output_asm_insn ("call\t%a0, 0", operands);
6021
6022 if (final_sequence)
6023 {
6024 rtx_insn *delay = NEXT_INSN (insn);
6025 gcc_assert (delay);
6026
6027 output_restore (PATTERN (delay));
6028
6029 PATTERN (delay) = gen_blockage ();
6030 INSN_CODE (delay) = -1;
6031 }
6032 else
6033 output_restore (NULL_RTX);
6034 }
6035
6036 return "";
6037 }
6038 \f
6039 /* Functions for handling argument passing.
6040
6041 For 32-bit, the first 6 args are normally in registers and the rest are
6042 pushed. Any arg that starts within the first 6 words is at least
6043 partially passed in a register unless its data type forbids.
6044
6045 For 64-bit, the argument registers are laid out as an array of 16 elements
6046 and arguments are added sequentially. The first 6 int args and up to the
6047 first 16 fp args (depending on size) are passed in regs.
6048
6049 Slot Stack Integral Float Float in structure Double Long Double
6050 ---- ----- -------- ----- ------------------ ------ -----------
6051 15 [SP+248] %f31 %f30,%f31 %d30
6052 14 [SP+240] %f29 %f28,%f29 %d28 %q28
6053 13 [SP+232] %f27 %f26,%f27 %d26
6054 12 [SP+224] %f25 %f24,%f25 %d24 %q24
6055 11 [SP+216] %f23 %f22,%f23 %d22
6056 10 [SP+208] %f21 %f20,%f21 %d20 %q20
6057 9 [SP+200] %f19 %f18,%f19 %d18
6058 8 [SP+192] %f17 %f16,%f17 %d16 %q16
6059 7 [SP+184] %f15 %f14,%f15 %d14
6060 6 [SP+176] %f13 %f12,%f13 %d12 %q12
6061 5 [SP+168] %o5 %f11 %f10,%f11 %d10
6062 4 [SP+160] %o4 %f9 %f8,%f9 %d8 %q8
6063 3 [SP+152] %o3 %f7 %f6,%f7 %d6
6064 2 [SP+144] %o2 %f5 %f4,%f5 %d4 %q4
6065 1 [SP+136] %o1 %f3 %f2,%f3 %d2
6066 0 [SP+128] %o0 %f1 %f0,%f1 %d0 %q0
6067
6068 Here SP = %sp if -mno-stack-bias or %sp+stack_bias otherwise.
6069
6070 Integral arguments are always passed as 64-bit quantities appropriately
6071 extended.
6072
6073 Passing of floating point values is handled as follows.
6074 If a prototype is in scope:
6075 If the value is in a named argument (i.e. not a stdarg function or a
6076 value not part of the `...') then the value is passed in the appropriate
6077 fp reg.
6078 If the value is part of the `...' and is passed in one of the first 6
6079 slots then the value is passed in the appropriate int reg.
6080 If the value is part of the `...' and is not passed in one of the first 6
6081 slots then the value is passed in memory.
6082 If a prototype is not in scope:
6083 If the value is one of the first 6 arguments the value is passed in the
6084 appropriate integer reg and the appropriate fp reg.
6085 If the value is not one of the first 6 arguments the value is passed in
6086 the appropriate fp reg and in memory.
6087
6088
6089 Summary of the calling conventions implemented by GCC on the SPARC:
6090
6091 32-bit ABI:
6092 size argument return value
6093
6094 small integer <4 int. reg. int. reg.
6095 word 4 int. reg. int. reg.
6096 double word 8 int. reg. int. reg.
6097
6098 _Complex small integer <8 int. reg. int. reg.
6099 _Complex word 8 int. reg. int. reg.
6100 _Complex double word 16 memory int. reg.
6101
6102 vector integer <=8 int. reg. FP reg.
6103 vector integer >8 memory memory
6104
6105 float 4 int. reg. FP reg.
6106 double 8 int. reg. FP reg.
6107 long double 16 memory memory
6108
6109 _Complex float 8 memory FP reg.
6110 _Complex double 16 memory FP reg.
6111 _Complex long double 32 memory FP reg.
6112
6113 vector float any memory memory
6114
6115 aggregate any memory memory
6116
6117
6118
6119 64-bit ABI:
6120 size argument return value
6121
6122 small integer <8 int. reg. int. reg.
6123 word 8 int. reg. int. reg.
6124 double word 16 int. reg. int. reg.
6125
6126 _Complex small integer <16 int. reg. int. reg.
6127 _Complex word 16 int. reg. int. reg.
6128 _Complex double word 32 memory int. reg.
6129
6130 vector integer <=16 FP reg. FP reg.
6131 vector integer 16<s<=32 memory FP reg.
6132 vector integer >32 memory memory
6133
6134 float 4 FP reg. FP reg.
6135 double 8 FP reg. FP reg.
6136 long double 16 FP reg. FP reg.
6137
6138 _Complex float 8 FP reg. FP reg.
6139 _Complex double 16 FP reg. FP reg.
6140 _Complex long double 32 memory FP reg.
6141
6142 vector float <=16 FP reg. FP reg.
6143 vector float 16<s<=32 memory FP reg.
6144 vector float >32 memory memory
6145
6146 aggregate <=16 reg. reg.
6147 aggregate 16<s<=32 memory reg.
6148 aggregate >32 memory memory
6149
6150
6151
6152 Note #1: complex floating-point types follow the extended SPARC ABIs as
6153 implemented by the Sun compiler.
6154
6155 Note #2: integral vector types follow the scalar floating-point types
6156 conventions to match what is implemented by the Sun VIS SDK.
6157
6158 Note #3: floating-point vector types follow the aggregate types
6159 conventions. */
6160
6161
6162 /* Maximum number of int regs for args. */
6163 #define SPARC_INT_ARG_MAX 6
6164 /* Maximum number of fp regs for args. */
6165 #define SPARC_FP_ARG_MAX 16
6166
6167 #define ROUND_ADVANCE(SIZE) (((SIZE) + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
6168
6169 /* Handle the INIT_CUMULATIVE_ARGS macro.
6170 Initialize a variable CUM of type CUMULATIVE_ARGS
6171 for a call to a function whose data type is FNTYPE.
6172 For a library call, FNTYPE is 0. */
6173
6174 void
6175 init_cumulative_args (struct sparc_args *cum, tree fntype,
6176 rtx libname ATTRIBUTE_UNUSED,
6177 tree fndecl ATTRIBUTE_UNUSED)
6178 {
6179 cum->words = 0;
6180 cum->prototype_p = fntype && prototype_p (fntype);
6181 cum->libcall_p = fntype == 0;
6182 }
6183
6184 /* Handle promotion of pointer and integer arguments. */
6185
6186 static enum machine_mode
6187 sparc_promote_function_mode (const_tree type,
6188 enum machine_mode mode,
6189 int *punsignedp,
6190 const_tree fntype ATTRIBUTE_UNUSED,
6191 int for_return ATTRIBUTE_UNUSED)
6192 {
6193 if (type != NULL_TREE && POINTER_TYPE_P (type))
6194 {
6195 *punsignedp = POINTERS_EXTEND_UNSIGNED;
6196 return Pmode;
6197 }
6198
6199 /* Integral arguments are passed as full words, as per the ABI. */
6200 if (GET_MODE_CLASS (mode) == MODE_INT
6201 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
6202 return word_mode;
6203
6204 return mode;
6205 }
6206
6207 /* Handle the TARGET_STRICT_ARGUMENT_NAMING target hook. */
6208
6209 static bool
6210 sparc_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
6211 {
6212 return TARGET_ARCH64 ? true : false;
6213 }
6214
6215 /* Scan the record type TYPE and return the following predicates:
6216 - INTREGS_P: the record contains at least one field or sub-field
6217 that is eligible for promotion in integer registers.
6218 - FP_REGS_P: the record contains at least one field or sub-field
6219 that is eligible for promotion in floating-point registers.
6220 - PACKED_P: the record contains at least one field that is packed.
6221
6222 Sub-fields are not taken into account for the PACKED_P predicate. */
6223
6224 static void
6225 scan_record_type (const_tree type, int *intregs_p, int *fpregs_p,
6226 int *packed_p)
6227 {
6228 tree field;
6229
6230 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6231 {
6232 if (TREE_CODE (field) == FIELD_DECL)
6233 {
6234 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
6235 scan_record_type (TREE_TYPE (field), intregs_p, fpregs_p, 0);
6236 else if ((FLOAT_TYPE_P (TREE_TYPE (field))
6237 || TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
6238 && TARGET_FPU)
6239 *fpregs_p = 1;
6240 else
6241 *intregs_p = 1;
6242
6243 if (packed_p && DECL_PACKED (field))
6244 *packed_p = 1;
6245 }
6246 }
6247 }
6248
6249 /* Compute the slot number to pass an argument in.
6250 Return the slot number or -1 if passing on the stack.
6251
6252 CUM is a variable of type CUMULATIVE_ARGS which gives info about
6253 the preceding args and about the function being called.
6254 MODE is the argument's machine mode.
6255 TYPE is the data type of the argument (as a tree).
6256 This is null for libcalls where that information may
6257 not be available.
6258 NAMED is nonzero if this argument is a named parameter
6259 (otherwise it is an extra parameter matching an ellipsis).
6260 INCOMING_P is zero for FUNCTION_ARG, nonzero for FUNCTION_INCOMING_ARG.
6261 *PREGNO records the register number to use if scalar type.
6262 *PPADDING records the amount of padding needed in words. */
6263
6264 static int
6265 function_arg_slotno (const struct sparc_args *cum, enum machine_mode mode,
6266 const_tree type, bool named, bool incoming_p,
6267 int *pregno, int *ppadding)
6268 {
6269 int regbase = (incoming_p
6270 ? SPARC_INCOMING_INT_ARG_FIRST
6271 : SPARC_OUTGOING_INT_ARG_FIRST);
6272 int slotno = cum->words;
6273 enum mode_class mclass;
6274 int regno;
6275
6276 *ppadding = 0;
6277
6278 if (type && TREE_ADDRESSABLE (type))
6279 return -1;
6280
6281 if (TARGET_ARCH32
6282 && mode == BLKmode
6283 && type
6284 && TYPE_ALIGN (type) % PARM_BOUNDARY != 0)
6285 return -1;
6286
6287 /* For SPARC64, objects requiring 16-byte alignment get it. */
6288 if (TARGET_ARCH64
6289 && (type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode)) >= 128
6290 && (slotno & 1) != 0)
6291 slotno++, *ppadding = 1;
6292
6293 mclass = GET_MODE_CLASS (mode);
6294 if (type && TREE_CODE (type) == VECTOR_TYPE)
6295 {
6296 /* Vector types deserve special treatment because they are
6297 polymorphic wrt their mode, depending upon whether VIS
6298 instructions are enabled. */
6299 if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
6300 {
6301 /* The SPARC port defines no floating-point vector modes. */
6302 gcc_assert (mode == BLKmode);
6303 }
6304 else
6305 {
6306 /* Integral vector types should either have a vector
6307 mode or an integral mode, because we are guaranteed
6308 by pass_by_reference that their size is not greater
6309 than 16 bytes and TImode is 16-byte wide. */
6310 gcc_assert (mode != BLKmode);
6311
6312 /* Vector integers are handled like floats according to
6313 the Sun VIS SDK. */
6314 mclass = MODE_FLOAT;
6315 }
6316 }
6317
6318 switch (mclass)
6319 {
6320 case MODE_FLOAT:
6321 case MODE_COMPLEX_FLOAT:
6322 case MODE_VECTOR_INT:
6323 if (TARGET_ARCH64 && TARGET_FPU && named)
6324 {
6325 if (slotno >= SPARC_FP_ARG_MAX)
6326 return -1;
6327 regno = SPARC_FP_ARG_FIRST + slotno * 2;
6328 /* Arguments filling only one single FP register are
6329 right-justified in the outer double FP register. */
6330 if (GET_MODE_SIZE (mode) <= 4)
6331 regno++;
6332 break;
6333 }
6334 /* fallthrough */
6335
6336 case MODE_INT:
6337 case MODE_COMPLEX_INT:
6338 if (slotno >= SPARC_INT_ARG_MAX)
6339 return -1;
6340 regno = regbase + slotno;
6341 break;
6342
6343 case MODE_RANDOM:
6344 if (mode == VOIDmode)
6345 /* MODE is VOIDmode when generating the actual call. */
6346 return -1;
6347
6348 gcc_assert (mode == BLKmode);
6349
6350 if (TARGET_ARCH32
6351 || !type
6352 || (TREE_CODE (type) != VECTOR_TYPE
6353 && TREE_CODE (type) != RECORD_TYPE))
6354 {
6355 if (slotno >= SPARC_INT_ARG_MAX)
6356 return -1;
6357 regno = regbase + slotno;
6358 }
6359 else /* TARGET_ARCH64 && type */
6360 {
6361 int intregs_p = 0, fpregs_p = 0, packed_p = 0;
6362
6363 /* First see what kinds of registers we would need. */
6364 if (TREE_CODE (type) == VECTOR_TYPE)
6365 fpregs_p = 1;
6366 else
6367 scan_record_type (type, &intregs_p, &fpregs_p, &packed_p);
6368
6369 /* The ABI obviously doesn't specify how packed structures
6370 are passed. These are defined to be passed in int regs
6371 if possible, otherwise memory. */
6372 if (packed_p || !named)
6373 fpregs_p = 0, intregs_p = 1;
6374
6375 /* If all arg slots are filled, then must pass on stack. */
6376 if (fpregs_p && slotno >= SPARC_FP_ARG_MAX)
6377 return -1;
6378
6379 /* If there are only int args and all int arg slots are filled,
6380 then must pass on stack. */
6381 if (!fpregs_p && intregs_p && slotno >= SPARC_INT_ARG_MAX)
6382 return -1;
6383
6384 /* Note that even if all int arg slots are filled, fp members may
6385 still be passed in regs if such regs are available.
6386 *PREGNO isn't set because there may be more than one, it's up
6387 to the caller to compute them. */
6388 return slotno;
6389 }
6390 break;
6391
6392 default :
6393 gcc_unreachable ();
6394 }
6395
6396 *pregno = regno;
6397 return slotno;
6398 }
6399
6400 /* Handle recursive register counting for structure field layout. */
6401
6402 struct function_arg_record_value_parms
6403 {
6404 rtx ret; /* return expression being built. */
6405 int slotno; /* slot number of the argument. */
6406 int named; /* whether the argument is named. */
6407 int regbase; /* regno of the base register. */
6408 int stack; /* 1 if part of the argument is on the stack. */
6409 int intoffset; /* offset of the first pending integer field. */
6410 unsigned int nregs; /* number of words passed in registers. */
6411 };
6412
6413 static void function_arg_record_value_3
6414 (HOST_WIDE_INT, struct function_arg_record_value_parms *);
6415 static void function_arg_record_value_2
6416 (const_tree, HOST_WIDE_INT, struct function_arg_record_value_parms *, bool);
6417 static void function_arg_record_value_1
6418 (const_tree, HOST_WIDE_INT, struct function_arg_record_value_parms *, bool);
6419 static rtx function_arg_record_value (const_tree, enum machine_mode, int, int, int);
6420 static rtx function_arg_union_value (int, enum machine_mode, int, int);
6421
6422 /* A subroutine of function_arg_record_value. Traverse the structure
6423 recursively and determine how many registers will be required. */
6424
6425 static void
6426 function_arg_record_value_1 (const_tree type, HOST_WIDE_INT startbitpos,
6427 struct function_arg_record_value_parms *parms,
6428 bool packed_p)
6429 {
6430 tree field;
6431
6432 /* We need to compute how many registers are needed so we can
6433 allocate the PARALLEL but before we can do that we need to know
6434 whether there are any packed fields. The ABI obviously doesn't
6435 specify how structures are passed in this case, so they are
6436 defined to be passed in int regs if possible, otherwise memory,
6437 regardless of whether there are fp values present. */
6438
6439 if (! packed_p)
6440 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6441 {
6442 if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field))
6443 {
6444 packed_p = true;
6445 break;
6446 }
6447 }
6448
6449 /* Compute how many registers we need. */
6450 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6451 {
6452 if (TREE_CODE (field) == FIELD_DECL)
6453 {
6454 HOST_WIDE_INT bitpos = startbitpos;
6455
6456 if (DECL_SIZE (field) != 0)
6457 {
6458 if (integer_zerop (DECL_SIZE (field)))
6459 continue;
6460
6461 if (tree_fits_uhwi_p (bit_position (field)))
6462 bitpos += int_bit_position (field);
6463 }
6464
6465 /* ??? FIXME: else assume zero offset. */
6466
6467 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
6468 function_arg_record_value_1 (TREE_TYPE (field),
6469 bitpos,
6470 parms,
6471 packed_p);
6472 else if ((FLOAT_TYPE_P (TREE_TYPE (field))
6473 || TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
6474 && TARGET_FPU
6475 && parms->named
6476 && ! packed_p)
6477 {
6478 if (parms->intoffset != -1)
6479 {
6480 unsigned int startbit, endbit;
6481 int intslots, this_slotno;
6482
6483 startbit = parms->intoffset & -BITS_PER_WORD;
6484 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
6485
6486 intslots = (endbit - startbit) / BITS_PER_WORD;
6487 this_slotno = parms->slotno + parms->intoffset
6488 / BITS_PER_WORD;
6489
6490 if (intslots > 0 && intslots > SPARC_INT_ARG_MAX - this_slotno)
6491 {
6492 intslots = MAX (0, SPARC_INT_ARG_MAX - this_slotno);
6493 /* We need to pass this field on the stack. */
6494 parms->stack = 1;
6495 }
6496
6497 parms->nregs += intslots;
6498 parms->intoffset = -1;
6499 }
6500
6501 /* There's no need to check this_slotno < SPARC_FP_ARG MAX.
6502 If it wasn't true we wouldn't be here. */
6503 if (TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE
6504 && DECL_MODE (field) == BLKmode)
6505 parms->nregs += TYPE_VECTOR_SUBPARTS (TREE_TYPE (field));
6506 else if (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE)
6507 parms->nregs += 2;
6508 else
6509 parms->nregs += 1;
6510 }
6511 else
6512 {
6513 if (parms->intoffset == -1)
6514 parms->intoffset = bitpos;
6515 }
6516 }
6517 }
6518 }
6519
6520 /* A subroutine of function_arg_record_value. Assign the bits of the
6521 structure between parms->intoffset and bitpos to integer registers. */
6522
6523 static void
6524 function_arg_record_value_3 (HOST_WIDE_INT bitpos,
6525 struct function_arg_record_value_parms *parms)
6526 {
6527 enum machine_mode mode;
6528 unsigned int regno;
6529 unsigned int startbit, endbit;
6530 int this_slotno, intslots, intoffset;
6531 rtx reg;
6532
6533 if (parms->intoffset == -1)
6534 return;
6535
6536 intoffset = parms->intoffset;
6537 parms->intoffset = -1;
6538
6539 startbit = intoffset & -BITS_PER_WORD;
6540 endbit = (bitpos + BITS_PER_WORD - 1) & -BITS_PER_WORD;
6541 intslots = (endbit - startbit) / BITS_PER_WORD;
6542 this_slotno = parms->slotno + intoffset / BITS_PER_WORD;
6543
6544 intslots = MIN (intslots, SPARC_INT_ARG_MAX - this_slotno);
6545 if (intslots <= 0)
6546 return;
6547
6548 /* If this is the trailing part of a word, only load that much into
6549 the register. Otherwise load the whole register. Note that in
6550 the latter case we may pick up unwanted bits. It's not a problem
6551 at the moment but may wish to revisit. */
6552
6553 if (intoffset % BITS_PER_WORD != 0)
6554 mode = smallest_mode_for_size (BITS_PER_WORD - intoffset % BITS_PER_WORD,
6555 MODE_INT);
6556 else
6557 mode = word_mode;
6558
6559 intoffset /= BITS_PER_UNIT;
6560 do
6561 {
6562 regno = parms->regbase + this_slotno;
6563 reg = gen_rtx_REG (mode, regno);
6564 XVECEXP (parms->ret, 0, parms->stack + parms->nregs)
6565 = gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
6566
6567 this_slotno += 1;
6568 intoffset = (intoffset | (UNITS_PER_WORD-1)) + 1;
6569 mode = word_mode;
6570 parms->nregs += 1;
6571 intslots -= 1;
6572 }
6573 while (intslots > 0);
6574 }
6575
6576 /* A subroutine of function_arg_record_value. Traverse the structure
6577 recursively and assign bits to floating point registers. Track which
6578 bits in between need integer registers; invoke function_arg_record_value_3
6579 to make that happen. */
6580
6581 static void
6582 function_arg_record_value_2 (const_tree type, HOST_WIDE_INT startbitpos,
6583 struct function_arg_record_value_parms *parms,
6584 bool packed_p)
6585 {
6586 tree field;
6587
6588 if (! packed_p)
6589 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6590 {
6591 if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field))
6592 {
6593 packed_p = true;
6594 break;
6595 }
6596 }
6597
6598 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6599 {
6600 if (TREE_CODE (field) == FIELD_DECL)
6601 {
6602 HOST_WIDE_INT bitpos = startbitpos;
6603
6604 if (DECL_SIZE (field) != 0)
6605 {
6606 if (integer_zerop (DECL_SIZE (field)))
6607 continue;
6608
6609 if (tree_fits_uhwi_p (bit_position (field)))
6610 bitpos += int_bit_position (field);
6611 }
6612
6613 /* ??? FIXME: else assume zero offset. */
6614
6615 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE)
6616 function_arg_record_value_2 (TREE_TYPE (field),
6617 bitpos,
6618 parms,
6619 packed_p);
6620 else if ((FLOAT_TYPE_P (TREE_TYPE (field))
6621 || TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE)
6622 && TARGET_FPU
6623 && parms->named
6624 && ! packed_p)
6625 {
6626 int this_slotno = parms->slotno + bitpos / BITS_PER_WORD;
6627 int regno, nregs, pos;
6628 enum machine_mode mode = DECL_MODE (field);
6629 rtx reg;
6630
6631 function_arg_record_value_3 (bitpos, parms);
6632
6633 if (TREE_CODE (TREE_TYPE (field)) == VECTOR_TYPE
6634 && mode == BLKmode)
6635 {
6636 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (field)));
6637 nregs = TYPE_VECTOR_SUBPARTS (TREE_TYPE (field));
6638 }
6639 else if (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE)
6640 {
6641 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (field)));
6642 nregs = 2;
6643 }
6644 else
6645 nregs = 1;
6646
6647 regno = SPARC_FP_ARG_FIRST + this_slotno * 2;
6648 if (GET_MODE_SIZE (mode) <= 4 && (bitpos & 32) != 0)
6649 regno++;
6650 reg = gen_rtx_REG (mode, regno);
6651 pos = bitpos / BITS_PER_UNIT;
6652 XVECEXP (parms->ret, 0, parms->stack + parms->nregs)
6653 = gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (pos));
6654 parms->nregs += 1;
6655 while (--nregs > 0)
6656 {
6657 regno += GET_MODE_SIZE (mode) / 4;
6658 reg = gen_rtx_REG (mode, regno);
6659 pos += GET_MODE_SIZE (mode);
6660 XVECEXP (parms->ret, 0, parms->stack + parms->nregs)
6661 = gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (pos));
6662 parms->nregs += 1;
6663 }
6664 }
6665 else
6666 {
6667 if (parms->intoffset == -1)
6668 parms->intoffset = bitpos;
6669 }
6670 }
6671 }
6672 }
6673
6674 /* Used by function_arg and sparc_function_value_1 to implement the complex
6675 conventions of the 64-bit ABI for passing and returning structures.
6676 Return an expression valid as a return value for the FUNCTION_ARG
6677 and TARGET_FUNCTION_VALUE.
6678
6679 TYPE is the data type of the argument (as a tree).
6680 This is null for libcalls where that information may
6681 not be available.
6682 MODE is the argument's machine mode.
6683 SLOTNO is the index number of the argument's slot in the parameter array.
6684 NAMED is nonzero if this argument is a named parameter
6685 (otherwise it is an extra parameter matching an ellipsis).
6686 REGBASE is the regno of the base register for the parameter array. */
6687
6688 static rtx
6689 function_arg_record_value (const_tree type, enum machine_mode mode,
6690 int slotno, int named, int regbase)
6691 {
6692 HOST_WIDE_INT typesize = int_size_in_bytes (type);
6693 struct function_arg_record_value_parms parms;
6694 unsigned int nregs;
6695
6696 parms.ret = NULL_RTX;
6697 parms.slotno = slotno;
6698 parms.named = named;
6699 parms.regbase = regbase;
6700 parms.stack = 0;
6701
6702 /* Compute how many registers we need. */
6703 parms.nregs = 0;
6704 parms.intoffset = 0;
6705 function_arg_record_value_1 (type, 0, &parms, false);
6706
6707 /* Take into account pending integer fields. */
6708 if (parms.intoffset != -1)
6709 {
6710 unsigned int startbit, endbit;
6711 int intslots, this_slotno;
6712
6713 startbit = parms.intoffset & -BITS_PER_WORD;
6714 endbit = (typesize*BITS_PER_UNIT + BITS_PER_WORD - 1) & -BITS_PER_WORD;
6715 intslots = (endbit - startbit) / BITS_PER_WORD;
6716 this_slotno = slotno + parms.intoffset / BITS_PER_WORD;
6717
6718 if (intslots > 0 && intslots > SPARC_INT_ARG_MAX - this_slotno)
6719 {
6720 intslots = MAX (0, SPARC_INT_ARG_MAX - this_slotno);
6721 /* We need to pass this field on the stack. */
6722 parms.stack = 1;
6723 }
6724
6725 parms.nregs += intslots;
6726 }
6727 nregs = parms.nregs;
6728
6729 /* Allocate the vector and handle some annoying special cases. */
6730 if (nregs == 0)
6731 {
6732 /* ??? Empty structure has no value? Duh? */
6733 if (typesize <= 0)
6734 {
6735 /* Though there's nothing really to store, return a word register
6736 anyway so the rest of gcc doesn't go nuts. Returning a PARALLEL
6737 leads to breakage due to the fact that there are zero bytes to
6738 load. */
6739 return gen_rtx_REG (mode, regbase);
6740 }
6741 else
6742 {
6743 /* ??? C++ has structures with no fields, and yet a size. Give up
6744 for now and pass everything back in integer registers. */
6745 nregs = (typesize + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6746 }
6747 if (nregs + slotno > SPARC_INT_ARG_MAX)
6748 nregs = SPARC_INT_ARG_MAX - slotno;
6749 }
6750 gcc_assert (nregs != 0);
6751
6752 parms.ret = gen_rtx_PARALLEL (mode, rtvec_alloc (parms.stack + nregs));
6753
6754 /* If at least one field must be passed on the stack, generate
6755 (parallel [(expr_list (nil) ...) ...]) so that all fields will
6756 also be passed on the stack. We can't do much better because the
6757 semantics of TARGET_ARG_PARTIAL_BYTES doesn't handle the case
6758 of structures for which the fields passed exclusively in registers
6759 are not at the beginning of the structure. */
6760 if (parms.stack)
6761 XVECEXP (parms.ret, 0, 0)
6762 = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
6763
6764 /* Fill in the entries. */
6765 parms.nregs = 0;
6766 parms.intoffset = 0;
6767 function_arg_record_value_2 (type, 0, &parms, false);
6768 function_arg_record_value_3 (typesize * BITS_PER_UNIT, &parms);
6769
6770 gcc_assert (parms.nregs == nregs);
6771
6772 return parms.ret;
6773 }
6774
6775 /* Used by function_arg and sparc_function_value_1 to implement the conventions
6776 of the 64-bit ABI for passing and returning unions.
6777 Return an expression valid as a return value for the FUNCTION_ARG
6778 and TARGET_FUNCTION_VALUE.
6779
6780 SIZE is the size in bytes of the union.
6781 MODE is the argument's machine mode.
6782 REGNO is the hard register the union will be passed in. */
6783
6784 static rtx
6785 function_arg_union_value (int size, enum machine_mode mode, int slotno,
6786 int regno)
6787 {
6788 int nwords = ROUND_ADVANCE (size), i;
6789 rtx regs;
6790
6791 /* See comment in previous function for empty structures. */
6792 if (nwords == 0)
6793 return gen_rtx_REG (mode, regno);
6794
6795 if (slotno == SPARC_INT_ARG_MAX - 1)
6796 nwords = 1;
6797
6798 regs = gen_rtx_PARALLEL (mode, rtvec_alloc (nwords));
6799
6800 for (i = 0; i < nwords; i++)
6801 {
6802 /* Unions are passed left-justified. */
6803 XVECEXP (regs, 0, i)
6804 = gen_rtx_EXPR_LIST (VOIDmode,
6805 gen_rtx_REG (word_mode, regno),
6806 GEN_INT (UNITS_PER_WORD * i));
6807 regno++;
6808 }
6809
6810 return regs;
6811 }
6812
6813 /* Used by function_arg and sparc_function_value_1 to implement the conventions
6814 for passing and returning large (BLKmode) vectors.
6815 Return an expression valid as a return value for the FUNCTION_ARG
6816 and TARGET_FUNCTION_VALUE.
6817
6818 SIZE is the size in bytes of the vector (at least 8 bytes).
6819 REGNO is the FP hard register the vector will be passed in. */
6820
6821 static rtx
6822 function_arg_vector_value (int size, int regno)
6823 {
6824 int i, nregs = size / 8;
6825 rtx regs;
6826
6827 regs = gen_rtx_PARALLEL (BLKmode, rtvec_alloc (nregs));
6828
6829 for (i = 0; i < nregs; i++)
6830 {
6831 XVECEXP (regs, 0, i)
6832 = gen_rtx_EXPR_LIST (VOIDmode,
6833 gen_rtx_REG (DImode, regno + 2*i),
6834 GEN_INT (i*8));
6835 }
6836
6837 return regs;
6838 }
6839
6840 /* Determine where to put an argument to a function.
6841 Value is zero to push the argument on the stack,
6842 or a hard register in which to store the argument.
6843
6844 CUM is a variable of type CUMULATIVE_ARGS which gives info about
6845 the preceding args and about the function being called.
6846 MODE is the argument's machine mode.
6847 TYPE is the data type of the argument (as a tree).
6848 This is null for libcalls where that information may
6849 not be available.
6850 NAMED is true if this argument is a named parameter
6851 (otherwise it is an extra parameter matching an ellipsis).
6852 INCOMING_P is false for TARGET_FUNCTION_ARG, true for
6853 TARGET_FUNCTION_INCOMING_ARG. */
6854
6855 static rtx
6856 sparc_function_arg_1 (cumulative_args_t cum_v, enum machine_mode mode,
6857 const_tree type, bool named, bool incoming_p)
6858 {
6859 const CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
6860
6861 int regbase = (incoming_p
6862 ? SPARC_INCOMING_INT_ARG_FIRST
6863 : SPARC_OUTGOING_INT_ARG_FIRST);
6864 int slotno, regno, padding;
6865 enum mode_class mclass = GET_MODE_CLASS (mode);
6866
6867 slotno = function_arg_slotno (cum, mode, type, named, incoming_p,
6868 &regno, &padding);
6869 if (slotno == -1)
6870 return 0;
6871
6872 /* Vector types deserve special treatment because they are polymorphic wrt
6873 their mode, depending upon whether VIS instructions are enabled. */
6874 if (type && TREE_CODE (type) == VECTOR_TYPE)
6875 {
6876 HOST_WIDE_INT size = int_size_in_bytes (type);
6877 gcc_assert ((TARGET_ARCH32 && size <= 8)
6878 || (TARGET_ARCH64 && size <= 16));
6879
6880 if (mode == BLKmode)
6881 return function_arg_vector_value (size,
6882 SPARC_FP_ARG_FIRST + 2*slotno);
6883 else
6884 mclass = MODE_FLOAT;
6885 }
6886
6887 if (TARGET_ARCH32)
6888 return gen_rtx_REG (mode, regno);
6889
6890 /* Structures up to 16 bytes in size are passed in arg slots on the stack
6891 and are promoted to registers if possible. */
6892 if (type && TREE_CODE (type) == RECORD_TYPE)
6893 {
6894 HOST_WIDE_INT size = int_size_in_bytes (type);
6895 gcc_assert (size <= 16);
6896
6897 return function_arg_record_value (type, mode, slotno, named, regbase);
6898 }
6899
6900 /* Unions up to 16 bytes in size are passed in integer registers. */
6901 else if (type && TREE_CODE (type) == UNION_TYPE)
6902 {
6903 HOST_WIDE_INT size = int_size_in_bytes (type);
6904 gcc_assert (size <= 16);
6905
6906 return function_arg_union_value (size, mode, slotno, regno);
6907 }
6908
6909 /* v9 fp args in reg slots beyond the int reg slots get passed in regs
6910 but also have the slot allocated for them.
6911 If no prototype is in scope fp values in register slots get passed
6912 in two places, either fp regs and int regs or fp regs and memory. */
6913 else if ((mclass == MODE_FLOAT || mclass == MODE_COMPLEX_FLOAT)
6914 && SPARC_FP_REG_P (regno))
6915 {
6916 rtx reg = gen_rtx_REG (mode, regno);
6917 if (cum->prototype_p || cum->libcall_p)
6918 {
6919 /* "* 2" because fp reg numbers are recorded in 4 byte
6920 quantities. */
6921 #if 0
6922 /* ??? This will cause the value to be passed in the fp reg and
6923 in the stack. When a prototype exists we want to pass the
6924 value in the reg but reserve space on the stack. That's an
6925 optimization, and is deferred [for a bit]. */
6926 if ((regno - SPARC_FP_ARG_FIRST) >= SPARC_INT_ARG_MAX * 2)
6927 return gen_rtx_PARALLEL (mode,
6928 gen_rtvec (2,
6929 gen_rtx_EXPR_LIST (VOIDmode,
6930 NULL_RTX, const0_rtx),
6931 gen_rtx_EXPR_LIST (VOIDmode,
6932 reg, const0_rtx)));
6933 else
6934 #else
6935 /* ??? It seems that passing back a register even when past
6936 the area declared by REG_PARM_STACK_SPACE will allocate
6937 space appropriately, and will not copy the data onto the
6938 stack, exactly as we desire.
6939
6940 This is due to locate_and_pad_parm being called in
6941 expand_call whenever reg_parm_stack_space > 0, which
6942 while beneficial to our example here, would seem to be
6943 in error from what had been intended. Ho hum... -- r~ */
6944 #endif
6945 return reg;
6946 }
6947 else
6948 {
6949 rtx v0, v1;
6950
6951 if ((regno - SPARC_FP_ARG_FIRST) < SPARC_INT_ARG_MAX * 2)
6952 {
6953 int intreg;
6954
6955 /* On incoming, we don't need to know that the value
6956 is passed in %f0 and %i0, and it confuses other parts
6957 causing needless spillage even on the simplest cases. */
6958 if (incoming_p)
6959 return reg;
6960
6961 intreg = (SPARC_OUTGOING_INT_ARG_FIRST
6962 + (regno - SPARC_FP_ARG_FIRST) / 2);
6963
6964 v0 = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
6965 v1 = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_REG (mode, intreg),
6966 const0_rtx);
6967 return gen_rtx_PARALLEL (mode, gen_rtvec (2, v0, v1));
6968 }
6969 else
6970 {
6971 v0 = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
6972 v1 = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
6973 return gen_rtx_PARALLEL (mode, gen_rtvec (2, v0, v1));
6974 }
6975 }
6976 }
6977
6978 /* All other aggregate types are passed in an integer register in a mode
6979 corresponding to the size of the type. */
6980 else if (type && AGGREGATE_TYPE_P (type))
6981 {
6982 HOST_WIDE_INT size = int_size_in_bytes (type);
6983 gcc_assert (size <= 16);
6984
6985 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
6986 }
6987
6988 return gen_rtx_REG (mode, regno);
6989 }
6990
6991 /* Handle the TARGET_FUNCTION_ARG target hook. */
6992
6993 static rtx
6994 sparc_function_arg (cumulative_args_t cum, enum machine_mode mode,
6995 const_tree type, bool named)
6996 {
6997 return sparc_function_arg_1 (cum, mode, type, named, false);
6998 }
6999
7000 /* Handle the TARGET_FUNCTION_INCOMING_ARG target hook. */
7001
7002 static rtx
7003 sparc_function_incoming_arg (cumulative_args_t cum, enum machine_mode mode,
7004 const_tree type, bool named)
7005 {
7006 return sparc_function_arg_1 (cum, mode, type, named, true);
7007 }
7008
7009 /* For sparc64, objects requiring 16 byte alignment are passed that way. */
7010
7011 static unsigned int
7012 sparc_function_arg_boundary (enum machine_mode mode, const_tree type)
7013 {
7014 return ((TARGET_ARCH64
7015 && (GET_MODE_ALIGNMENT (mode) == 128
7016 || (type && TYPE_ALIGN (type) == 128)))
7017 ? 128
7018 : PARM_BOUNDARY);
7019 }
7020
7021 /* For an arg passed partly in registers and partly in memory,
7022 this is the number of bytes of registers used.
7023 For args passed entirely in registers or entirely in memory, zero.
7024
7025 Any arg that starts in the first 6 regs but won't entirely fit in them
7026 needs partial registers on v8. On v9, structures with integer
7027 values in arg slots 5,6 will be passed in %o5 and SP+176, and complex fp
7028 values that begin in the last fp reg [where "last fp reg" varies with the
7029 mode] will be split between that reg and memory. */
7030
7031 static int
7032 sparc_arg_partial_bytes (cumulative_args_t cum, enum machine_mode mode,
7033 tree type, bool named)
7034 {
7035 int slotno, regno, padding;
7036
7037 /* We pass false for incoming_p here, it doesn't matter. */
7038 slotno = function_arg_slotno (get_cumulative_args (cum), mode, type, named,
7039 false, &regno, &padding);
7040
7041 if (slotno == -1)
7042 return 0;
7043
7044 if (TARGET_ARCH32)
7045 {
7046 if ((slotno + (mode == BLKmode
7047 ? ROUND_ADVANCE (int_size_in_bytes (type))
7048 : ROUND_ADVANCE (GET_MODE_SIZE (mode))))
7049 > SPARC_INT_ARG_MAX)
7050 return (SPARC_INT_ARG_MAX - slotno) * UNITS_PER_WORD;
7051 }
7052 else
7053 {
7054 /* We are guaranteed by pass_by_reference that the size of the
7055 argument is not greater than 16 bytes, so we only need to return
7056 one word if the argument is partially passed in registers. */
7057
7058 if (type && AGGREGATE_TYPE_P (type))
7059 {
7060 int size = int_size_in_bytes (type);
7061
7062 if (size > UNITS_PER_WORD
7063 && slotno == SPARC_INT_ARG_MAX - 1)
7064 return UNITS_PER_WORD;
7065 }
7066 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_INT
7067 || (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
7068 && ! (TARGET_FPU && named)))
7069 {
7070 /* The complex types are passed as packed types. */
7071 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
7072 && slotno == SPARC_INT_ARG_MAX - 1)
7073 return UNITS_PER_WORD;
7074 }
7075 else if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT)
7076 {
7077 if ((slotno + GET_MODE_SIZE (mode) / UNITS_PER_WORD)
7078 > SPARC_FP_ARG_MAX)
7079 return UNITS_PER_WORD;
7080 }
7081 }
7082
7083 return 0;
7084 }
7085
7086 /* Handle the TARGET_PASS_BY_REFERENCE target hook.
7087 Specify whether to pass the argument by reference. */
7088
7089 static bool
7090 sparc_pass_by_reference (cumulative_args_t cum ATTRIBUTE_UNUSED,
7091 enum machine_mode mode, const_tree type,
7092 bool named ATTRIBUTE_UNUSED)
7093 {
7094 if (TARGET_ARCH32)
7095 /* Original SPARC 32-bit ABI says that structures and unions,
7096 and quad-precision floats are passed by reference. For Pascal,
7097 also pass arrays by reference. All other base types are passed
7098 in registers.
7099
7100 Extended ABI (as implemented by the Sun compiler) says that all
7101 complex floats are passed by reference. Pass complex integers
7102 in registers up to 8 bytes. More generally, enforce the 2-word
7103 cap for passing arguments in registers.
7104
7105 Vector ABI (as implemented by the Sun VIS SDK) says that vector
7106 integers are passed like floats of the same size, that is in
7107 registers up to 8 bytes. Pass all vector floats by reference
7108 like structure and unions. */
7109 return ((type && (AGGREGATE_TYPE_P (type) || VECTOR_FLOAT_TYPE_P (type)))
7110 || mode == SCmode
7111 /* Catch CDImode, TFmode, DCmode and TCmode. */
7112 || GET_MODE_SIZE (mode) > 8
7113 || (type
7114 && TREE_CODE (type) == VECTOR_TYPE
7115 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8));
7116 else
7117 /* Original SPARC 64-bit ABI says that structures and unions
7118 smaller than 16 bytes are passed in registers, as well as
7119 all other base types.
7120
7121 Extended ABI (as implemented by the Sun compiler) says that
7122 complex floats are passed in registers up to 16 bytes. Pass
7123 all complex integers in registers up to 16 bytes. More generally,
7124 enforce the 2-word cap for passing arguments in registers.
7125
7126 Vector ABI (as implemented by the Sun VIS SDK) says that vector
7127 integers are passed like floats of the same size, that is in
7128 registers (up to 16 bytes). Pass all vector floats like structure
7129 and unions. */
7130 return ((type
7131 && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == VECTOR_TYPE)
7132 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 16)
7133 /* Catch CTImode and TCmode. */
7134 || GET_MODE_SIZE (mode) > 16);
7135 }
7136
7137 /* Handle the TARGET_FUNCTION_ARG_ADVANCE hook.
7138 Update the data in CUM to advance over an argument
7139 of mode MODE and data type TYPE.
7140 TYPE is null for libcalls where that information may not be available. */
7141
7142 static void
7143 sparc_function_arg_advance (cumulative_args_t cum_v, enum machine_mode mode,
7144 const_tree type, bool named)
7145 {
7146 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
7147 int regno, padding;
7148
7149 /* We pass false for incoming_p here, it doesn't matter. */
7150 function_arg_slotno (cum, mode, type, named, false, &regno, &padding);
7151
7152 /* If argument requires leading padding, add it. */
7153 cum->words += padding;
7154
7155 if (TARGET_ARCH32)
7156 {
7157 cum->words += (mode != BLKmode
7158 ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
7159 : ROUND_ADVANCE (int_size_in_bytes (type)));
7160 }
7161 else
7162 {
7163 if (type && AGGREGATE_TYPE_P (type))
7164 {
7165 int size = int_size_in_bytes (type);
7166
7167 if (size <= 8)
7168 ++cum->words;
7169 else if (size <= 16)
7170 cum->words += 2;
7171 else /* passed by reference */
7172 ++cum->words;
7173 }
7174 else
7175 {
7176 cum->words += (mode != BLKmode
7177 ? ROUND_ADVANCE (GET_MODE_SIZE (mode))
7178 : ROUND_ADVANCE (int_size_in_bytes (type)));
7179 }
7180 }
7181 }
7182
7183 /* Handle the FUNCTION_ARG_PADDING macro.
7184 For the 64 bit ABI structs are always stored left shifted in their
7185 argument slot. */
7186
7187 enum direction
7188 function_arg_padding (enum machine_mode mode, const_tree type)
7189 {
7190 if (TARGET_ARCH64 && type != 0 && AGGREGATE_TYPE_P (type))
7191 return upward;
7192
7193 /* Fall back to the default. */
7194 return DEFAULT_FUNCTION_ARG_PADDING (mode, type);
7195 }
7196
7197 /* Handle the TARGET_RETURN_IN_MEMORY target hook.
7198 Specify whether to return the return value in memory. */
7199
7200 static bool
7201 sparc_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7202 {
7203 if (TARGET_ARCH32)
7204 /* Original SPARC 32-bit ABI says that structures and unions,
7205 and quad-precision floats are returned in memory. All other
7206 base types are returned in registers.
7207
7208 Extended ABI (as implemented by the Sun compiler) says that
7209 all complex floats are returned in registers (8 FP registers
7210 at most for '_Complex long double'). Return all complex integers
7211 in registers (4 at most for '_Complex long long').
7212
7213 Vector ABI (as implemented by the Sun VIS SDK) says that vector
7214 integers are returned like floats of the same size, that is in
7215 registers up to 8 bytes and in memory otherwise. Return all
7216 vector floats in memory like structure and unions; note that
7217 they always have BLKmode like the latter. */
7218 return (TYPE_MODE (type) == BLKmode
7219 || TYPE_MODE (type) == TFmode
7220 || (TREE_CODE (type) == VECTOR_TYPE
7221 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8));
7222 else
7223 /* Original SPARC 64-bit ABI says that structures and unions
7224 smaller than 32 bytes are returned in registers, as well as
7225 all other base types.
7226
7227 Extended ABI (as implemented by the Sun compiler) says that all
7228 complex floats are returned in registers (8 FP registers at most
7229 for '_Complex long double'). Return all complex integers in
7230 registers (4 at most for '_Complex TItype').
7231
7232 Vector ABI (as implemented by the Sun VIS SDK) says that vector
7233 integers are returned like floats of the same size, that is in
7234 registers. Return all vector floats like structure and unions;
7235 note that they always have BLKmode like the latter. */
7236 return (TYPE_MODE (type) == BLKmode
7237 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 32);
7238 }
7239
7240 /* Handle the TARGET_STRUCT_VALUE target hook.
7241 Return where to find the structure return value address. */
7242
7243 static rtx
7244 sparc_struct_value_rtx (tree fndecl, int incoming)
7245 {
7246 if (TARGET_ARCH64)
7247 return 0;
7248 else
7249 {
7250 rtx mem;
7251
7252 if (incoming)
7253 mem = gen_frame_mem (Pmode, plus_constant (Pmode, frame_pointer_rtx,
7254 STRUCT_VALUE_OFFSET));
7255 else
7256 mem = gen_frame_mem (Pmode, plus_constant (Pmode, stack_pointer_rtx,
7257 STRUCT_VALUE_OFFSET));
7258
7259 /* Only follow the SPARC ABI for fixed-size structure returns.
7260 Variable size structure returns are handled per the normal
7261 procedures in GCC. This is enabled by -mstd-struct-return */
7262 if (incoming == 2
7263 && sparc_std_struct_return
7264 && TYPE_SIZE_UNIT (TREE_TYPE (fndecl))
7265 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (fndecl))) == INTEGER_CST)
7266 {
7267 /* We must check and adjust the return address, as it is
7268 optional as to whether the return object is really
7269 provided. */
7270 rtx ret_reg = gen_rtx_REG (Pmode, 31);
7271 rtx scratch = gen_reg_rtx (SImode);
7272 rtx_code_label *endlab = gen_label_rtx ();
7273
7274 /* Calculate the return object size */
7275 tree size = TYPE_SIZE_UNIT (TREE_TYPE (fndecl));
7276 rtx size_rtx = GEN_INT (TREE_INT_CST_LOW (size) & 0xfff);
7277 /* Construct a temporary return value */
7278 rtx temp_val
7279 = assign_stack_local (Pmode, TREE_INT_CST_LOW (size), 0);
7280
7281 /* Implement SPARC 32-bit psABI callee return struct checking:
7282
7283 Fetch the instruction where we will return to and see if
7284 it's an unimp instruction (the most significant 10 bits
7285 will be zero). */
7286 emit_move_insn (scratch, gen_rtx_MEM (SImode,
7287 plus_constant (Pmode,
7288 ret_reg, 8)));
7289 /* Assume the size is valid and pre-adjust */
7290 emit_insn (gen_add3_insn (ret_reg, ret_reg, GEN_INT (4)));
7291 emit_cmp_and_jump_insns (scratch, size_rtx, EQ, const0_rtx, SImode,
7292 0, endlab);
7293 emit_insn (gen_sub3_insn (ret_reg, ret_reg, GEN_INT (4)));
7294 /* Write the address of the memory pointed to by temp_val into
7295 the memory pointed to by mem */
7296 emit_move_insn (mem, XEXP (temp_val, 0));
7297 emit_label (endlab);
7298 }
7299
7300 return mem;
7301 }
7302 }
7303
7304 /* Handle TARGET_FUNCTION_VALUE, and TARGET_LIBCALL_VALUE target hook.
7305 For v9, function return values are subject to the same rules as arguments,
7306 except that up to 32 bytes may be returned in registers. */
7307
7308 static rtx
7309 sparc_function_value_1 (const_tree type, enum machine_mode mode,
7310 bool outgoing)
7311 {
7312 /* Beware that the two values are swapped here wrt function_arg. */
7313 int regbase = (outgoing
7314 ? SPARC_INCOMING_INT_ARG_FIRST
7315 : SPARC_OUTGOING_INT_ARG_FIRST);
7316 enum mode_class mclass = GET_MODE_CLASS (mode);
7317 int regno;
7318
7319 /* Vector types deserve special treatment because they are polymorphic wrt
7320 their mode, depending upon whether VIS instructions are enabled. */
7321 if (type && TREE_CODE (type) == VECTOR_TYPE)
7322 {
7323 HOST_WIDE_INT size = int_size_in_bytes (type);
7324 gcc_assert ((TARGET_ARCH32 && size <= 8)
7325 || (TARGET_ARCH64 && size <= 32));
7326
7327 if (mode == BLKmode)
7328 return function_arg_vector_value (size,
7329 SPARC_FP_ARG_FIRST);
7330 else
7331 mclass = MODE_FLOAT;
7332 }
7333
7334 if (TARGET_ARCH64 && type)
7335 {
7336 /* Structures up to 32 bytes in size are returned in registers. */
7337 if (TREE_CODE (type) == RECORD_TYPE)
7338 {
7339 HOST_WIDE_INT size = int_size_in_bytes (type);
7340 gcc_assert (size <= 32);
7341
7342 return function_arg_record_value (type, mode, 0, 1, regbase);
7343 }
7344
7345 /* Unions up to 32 bytes in size are returned in integer registers. */
7346 else if (TREE_CODE (type) == UNION_TYPE)
7347 {
7348 HOST_WIDE_INT size = int_size_in_bytes (type);
7349 gcc_assert (size <= 32);
7350
7351 return function_arg_union_value (size, mode, 0, regbase);
7352 }
7353
7354 /* Objects that require it are returned in FP registers. */
7355 else if (mclass == MODE_FLOAT || mclass == MODE_COMPLEX_FLOAT)
7356 ;
7357
7358 /* All other aggregate types are returned in an integer register in a
7359 mode corresponding to the size of the type. */
7360 else if (AGGREGATE_TYPE_P (type))
7361 {
7362 /* All other aggregate types are passed in an integer register
7363 in a mode corresponding to the size of the type. */
7364 HOST_WIDE_INT size = int_size_in_bytes (type);
7365 gcc_assert (size <= 32);
7366
7367 mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
7368
7369 /* ??? We probably should have made the same ABI change in
7370 3.4.0 as the one we made for unions. The latter was
7371 required by the SCD though, while the former is not
7372 specified, so we favored compatibility and efficiency.
7373
7374 Now we're stuck for aggregates larger than 16 bytes,
7375 because OImode vanished in the meantime. Let's not
7376 try to be unduly clever, and simply follow the ABI
7377 for unions in that case. */
7378 if (mode == BLKmode)
7379 return function_arg_union_value (size, mode, 0, regbase);
7380 else
7381 mclass = MODE_INT;
7382 }
7383
7384 /* We should only have pointer and integer types at this point. This
7385 must match sparc_promote_function_mode. */
7386 else if (mclass == MODE_INT && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
7387 mode = word_mode;
7388 }
7389
7390 /* We should only have pointer and integer types at this point. This must
7391 match sparc_promote_function_mode. */
7392 else if (TARGET_ARCH32
7393 && mclass == MODE_INT
7394 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
7395 mode = word_mode;
7396
7397 if ((mclass == MODE_FLOAT || mclass == MODE_COMPLEX_FLOAT) && TARGET_FPU)
7398 regno = SPARC_FP_ARG_FIRST;
7399 else
7400 regno = regbase;
7401
7402 return gen_rtx_REG (mode, regno);
7403 }
7404
7405 /* Handle TARGET_FUNCTION_VALUE.
7406 On the SPARC, the value is found in the first "output" register, but the
7407 called function leaves it in the first "input" register. */
7408
7409 static rtx
7410 sparc_function_value (const_tree valtype,
7411 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
7412 bool outgoing)
7413 {
7414 return sparc_function_value_1 (valtype, TYPE_MODE (valtype), outgoing);
7415 }
7416
7417 /* Handle TARGET_LIBCALL_VALUE. */
7418
7419 static rtx
7420 sparc_libcall_value (enum machine_mode mode,
7421 const_rtx fun ATTRIBUTE_UNUSED)
7422 {
7423 return sparc_function_value_1 (NULL_TREE, mode, false);
7424 }
7425
7426 /* Handle FUNCTION_VALUE_REGNO_P.
7427 On the SPARC, the first "output" reg is used for integer values, and the
7428 first floating point register is used for floating point values. */
7429
7430 static bool
7431 sparc_function_value_regno_p (const unsigned int regno)
7432 {
7433 return (regno == 8 || regno == 32);
7434 }
7435
7436 /* Do what is necessary for `va_start'. We look at the current function
7437 to determine if stdarg or varargs is used and return the address of
7438 the first unnamed parameter. */
7439
7440 static rtx
7441 sparc_builtin_saveregs (void)
7442 {
7443 int first_reg = crtl->args.info.words;
7444 rtx address;
7445 int regno;
7446
7447 for (regno = first_reg; regno < SPARC_INT_ARG_MAX; regno++)
7448 emit_move_insn (gen_rtx_MEM (word_mode,
7449 gen_rtx_PLUS (Pmode,
7450 frame_pointer_rtx,
7451 GEN_INT (FIRST_PARM_OFFSET (0)
7452 + (UNITS_PER_WORD
7453 * regno)))),
7454 gen_rtx_REG (word_mode,
7455 SPARC_INCOMING_INT_ARG_FIRST + regno));
7456
7457 address = gen_rtx_PLUS (Pmode,
7458 frame_pointer_rtx,
7459 GEN_INT (FIRST_PARM_OFFSET (0)
7460 + UNITS_PER_WORD * first_reg));
7461
7462 return address;
7463 }
7464
7465 /* Implement `va_start' for stdarg. */
7466
7467 static void
7468 sparc_va_start (tree valist, rtx nextarg)
7469 {
7470 nextarg = expand_builtin_saveregs ();
7471 std_expand_builtin_va_start (valist, nextarg);
7472 }
7473
7474 /* Implement `va_arg' for stdarg. */
7475
7476 static tree
7477 sparc_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
7478 gimple_seq *post_p)
7479 {
7480 HOST_WIDE_INT size, rsize, align;
7481 tree addr, incr;
7482 bool indirect;
7483 tree ptrtype = build_pointer_type (type);
7484
7485 if (pass_by_reference (NULL, TYPE_MODE (type), type, false))
7486 {
7487 indirect = true;
7488 size = rsize = UNITS_PER_WORD;
7489 align = 0;
7490 }
7491 else
7492 {
7493 indirect = false;
7494 size = int_size_in_bytes (type);
7495 rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
7496 align = 0;
7497
7498 if (TARGET_ARCH64)
7499 {
7500 /* For SPARC64, objects requiring 16-byte alignment get it. */
7501 if (TYPE_ALIGN (type) >= 2 * (unsigned) BITS_PER_WORD)
7502 align = 2 * UNITS_PER_WORD;
7503
7504 /* SPARC-V9 ABI states that structures up to 16 bytes in size
7505 are left-justified in their slots. */
7506 if (AGGREGATE_TYPE_P (type))
7507 {
7508 if (size == 0)
7509 size = rsize = UNITS_PER_WORD;
7510 else
7511 size = rsize;
7512 }
7513 }
7514 }
7515
7516 incr = valist;
7517 if (align)
7518 {
7519 incr = fold_build_pointer_plus_hwi (incr, align - 1);
7520 incr = fold_convert (sizetype, incr);
7521 incr = fold_build2 (BIT_AND_EXPR, sizetype, incr,
7522 size_int (-align));
7523 incr = fold_convert (ptr_type_node, incr);
7524 }
7525
7526 gimplify_expr (&incr, pre_p, post_p, is_gimple_val, fb_rvalue);
7527 addr = incr;
7528
7529 if (BYTES_BIG_ENDIAN && size < rsize)
7530 addr = fold_build_pointer_plus_hwi (incr, rsize - size);
7531
7532 if (indirect)
7533 {
7534 addr = fold_convert (build_pointer_type (ptrtype), addr);
7535 addr = build_va_arg_indirect_ref (addr);
7536 }
7537
7538 /* If the address isn't aligned properly for the type, we need a temporary.
7539 FIXME: This is inefficient, usually we can do this in registers. */
7540 else if (align == 0 && TYPE_ALIGN (type) > BITS_PER_WORD)
7541 {
7542 tree tmp = create_tmp_var (type, "va_arg_tmp");
7543 tree dest_addr = build_fold_addr_expr (tmp);
7544 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
7545 3, dest_addr, addr, size_int (rsize));
7546 TREE_ADDRESSABLE (tmp) = 1;
7547 gimplify_and_add (copy, pre_p);
7548 addr = dest_addr;
7549 }
7550
7551 else
7552 addr = fold_convert (ptrtype, addr);
7553
7554 incr = fold_build_pointer_plus_hwi (incr, rsize);
7555 gimplify_assign (valist, incr, post_p);
7556
7557 return build_va_arg_indirect_ref (addr);
7558 }
7559 \f
7560 /* Implement the TARGET_VECTOR_MODE_SUPPORTED_P target hook.
7561 Specify whether the vector mode is supported by the hardware. */
7562
7563 static bool
7564 sparc_vector_mode_supported_p (enum machine_mode mode)
7565 {
7566 return TARGET_VIS && VECTOR_MODE_P (mode) ? true : false;
7567 }
7568 \f
7569 /* Implement the TARGET_VECTORIZE_PREFERRED_SIMD_MODE target hook. */
7570
7571 static enum machine_mode
7572 sparc_preferred_simd_mode (enum machine_mode mode)
7573 {
7574 if (TARGET_VIS)
7575 switch (mode)
7576 {
7577 case SImode:
7578 return V2SImode;
7579 case HImode:
7580 return V4HImode;
7581 case QImode:
7582 return V8QImode;
7583
7584 default:;
7585 }
7586
7587 return word_mode;
7588 }
7589 \f
7590 /* Return the string to output an unconditional branch to LABEL, which is
7591 the operand number of the label.
7592
7593 DEST is the destination insn (i.e. the label), INSN is the source. */
7594
7595 const char *
7596 output_ubranch (rtx dest, rtx_insn *insn)
7597 {
7598 static char string[64];
7599 bool v9_form = false;
7600 int delta;
7601 char *p;
7602
7603 /* Even if we are trying to use cbcond for this, evaluate
7604 whether we can use V9 branches as our backup plan. */
7605
7606 delta = 5000000;
7607 if (INSN_ADDRESSES_SET_P ())
7608 delta = (INSN_ADDRESSES (INSN_UID (dest))
7609 - INSN_ADDRESSES (INSN_UID (insn)));
7610
7611 /* Leave some instructions for "slop". */
7612 if (TARGET_V9 && delta >= -260000 && delta < 260000)
7613 v9_form = true;
7614
7615 if (TARGET_CBCOND)
7616 {
7617 bool emit_nop = emit_cbcond_nop (insn);
7618 bool far = false;
7619 const char *rval;
7620
7621 if (delta < -500 || delta > 500)
7622 far = true;
7623
7624 if (far)
7625 {
7626 if (v9_form)
7627 rval = "ba,a,pt\t%%xcc, %l0";
7628 else
7629 rval = "b,a\t%l0";
7630 }
7631 else
7632 {
7633 if (emit_nop)
7634 rval = "cwbe\t%%g0, %%g0, %l0\n\tnop";
7635 else
7636 rval = "cwbe\t%%g0, %%g0, %l0";
7637 }
7638 return rval;
7639 }
7640
7641 if (v9_form)
7642 strcpy (string, "ba%*,pt\t%%xcc, ");
7643 else
7644 strcpy (string, "b%*\t");
7645
7646 p = strchr (string, '\0');
7647 *p++ = '%';
7648 *p++ = 'l';
7649 *p++ = '0';
7650 *p++ = '%';
7651 *p++ = '(';
7652 *p = '\0';
7653
7654 return string;
7655 }
7656
7657 /* Return the string to output a conditional branch to LABEL, which is
7658 the operand number of the label. OP is the conditional expression.
7659 XEXP (OP, 0) is assumed to be a condition code register (integer or
7660 floating point) and its mode specifies what kind of comparison we made.
7661
7662 DEST is the destination insn (i.e. the label), INSN is the source.
7663
7664 REVERSED is nonzero if we should reverse the sense of the comparison.
7665
7666 ANNUL is nonzero if we should generate an annulling branch. */
7667
7668 const char *
7669 output_cbranch (rtx op, rtx dest, int label, int reversed, int annul,
7670 rtx_insn *insn)
7671 {
7672 static char string[64];
7673 enum rtx_code code = GET_CODE (op);
7674 rtx cc_reg = XEXP (op, 0);
7675 enum machine_mode mode = GET_MODE (cc_reg);
7676 const char *labelno, *branch;
7677 int spaces = 8, far;
7678 char *p;
7679
7680 /* v9 branches are limited to +-1MB. If it is too far away,
7681 change
7682
7683 bne,pt %xcc, .LC30
7684
7685 to
7686
7687 be,pn %xcc, .+12
7688 nop
7689 ba .LC30
7690
7691 and
7692
7693 fbne,a,pn %fcc2, .LC29
7694
7695 to
7696
7697 fbe,pt %fcc2, .+16
7698 nop
7699 ba .LC29 */
7700
7701 far = TARGET_V9 && (get_attr_length (insn) >= 3);
7702 if (reversed ^ far)
7703 {
7704 /* Reversal of FP compares takes care -- an ordered compare
7705 becomes an unordered compare and vice versa. */
7706 if (mode == CCFPmode || mode == CCFPEmode)
7707 code = reverse_condition_maybe_unordered (code);
7708 else
7709 code = reverse_condition (code);
7710 }
7711
7712 /* Start by writing the branch condition. */
7713 if (mode == CCFPmode || mode == CCFPEmode)
7714 {
7715 switch (code)
7716 {
7717 case NE:
7718 branch = "fbne";
7719 break;
7720 case EQ:
7721 branch = "fbe";
7722 break;
7723 case GE:
7724 branch = "fbge";
7725 break;
7726 case GT:
7727 branch = "fbg";
7728 break;
7729 case LE:
7730 branch = "fble";
7731 break;
7732 case LT:
7733 branch = "fbl";
7734 break;
7735 case UNORDERED:
7736 branch = "fbu";
7737 break;
7738 case ORDERED:
7739 branch = "fbo";
7740 break;
7741 case UNGT:
7742 branch = "fbug";
7743 break;
7744 case UNLT:
7745 branch = "fbul";
7746 break;
7747 case UNEQ:
7748 branch = "fbue";
7749 break;
7750 case UNGE:
7751 branch = "fbuge";
7752 break;
7753 case UNLE:
7754 branch = "fbule";
7755 break;
7756 case LTGT:
7757 branch = "fblg";
7758 break;
7759
7760 default:
7761 gcc_unreachable ();
7762 }
7763
7764 /* ??? !v9: FP branches cannot be preceded by another floating point
7765 insn. Because there is currently no concept of pre-delay slots,
7766 we can fix this only by always emitting a nop before a floating
7767 point branch. */
7768
7769 string[0] = '\0';
7770 if (! TARGET_V9)
7771 strcpy (string, "nop\n\t");
7772 strcat (string, branch);
7773 }
7774 else
7775 {
7776 switch (code)
7777 {
7778 case NE:
7779 branch = "bne";
7780 break;
7781 case EQ:
7782 branch = "be";
7783 break;
7784 case GE:
7785 if (mode == CC_NOOVmode || mode == CCX_NOOVmode)
7786 branch = "bpos";
7787 else
7788 branch = "bge";
7789 break;
7790 case GT:
7791 branch = "bg";
7792 break;
7793 case LE:
7794 branch = "ble";
7795 break;
7796 case LT:
7797 if (mode == CC_NOOVmode || mode == CCX_NOOVmode)
7798 branch = "bneg";
7799 else
7800 branch = "bl";
7801 break;
7802 case GEU:
7803 branch = "bgeu";
7804 break;
7805 case GTU:
7806 branch = "bgu";
7807 break;
7808 case LEU:
7809 branch = "bleu";
7810 break;
7811 case LTU:
7812 branch = "blu";
7813 break;
7814
7815 default:
7816 gcc_unreachable ();
7817 }
7818 strcpy (string, branch);
7819 }
7820 spaces -= strlen (branch);
7821 p = strchr (string, '\0');
7822
7823 /* Now add the annulling, the label, and a possible noop. */
7824 if (annul && ! far)
7825 {
7826 strcpy (p, ",a");
7827 p += 2;
7828 spaces -= 2;
7829 }
7830
7831 if (TARGET_V9)
7832 {
7833 rtx note;
7834 int v8 = 0;
7835
7836 if (! far && insn && INSN_ADDRESSES_SET_P ())
7837 {
7838 int delta = (INSN_ADDRESSES (INSN_UID (dest))
7839 - INSN_ADDRESSES (INSN_UID (insn)));
7840 /* Leave some instructions for "slop". */
7841 if (delta < -260000 || delta >= 260000)
7842 v8 = 1;
7843 }
7844
7845 if (mode == CCFPmode || mode == CCFPEmode)
7846 {
7847 static char v9_fcc_labelno[] = "%%fccX, ";
7848 /* Set the char indicating the number of the fcc reg to use. */
7849 v9_fcc_labelno[5] = REGNO (cc_reg) - SPARC_FIRST_V9_FCC_REG + '0';
7850 labelno = v9_fcc_labelno;
7851 if (v8)
7852 {
7853 gcc_assert (REGNO (cc_reg) == SPARC_FCC_REG);
7854 labelno = "";
7855 }
7856 }
7857 else if (mode == CCXmode || mode == CCX_NOOVmode)
7858 {
7859 labelno = "%%xcc, ";
7860 gcc_assert (! v8);
7861 }
7862 else
7863 {
7864 labelno = "%%icc, ";
7865 if (v8)
7866 labelno = "";
7867 }
7868
7869 if (*labelno && insn && (note = find_reg_note (insn, REG_BR_PROB, NULL_RTX)))
7870 {
7871 strcpy (p,
7872 ((XINT (note, 0) >= REG_BR_PROB_BASE / 2) ^ far)
7873 ? ",pt" : ",pn");
7874 p += 3;
7875 spaces -= 3;
7876 }
7877 }
7878 else
7879 labelno = "";
7880
7881 if (spaces > 0)
7882 *p++ = '\t';
7883 else
7884 *p++ = ' ';
7885 strcpy (p, labelno);
7886 p = strchr (p, '\0');
7887 if (far)
7888 {
7889 strcpy (p, ".+12\n\t nop\n\tb\t");
7890 /* Skip the next insn if requested or
7891 if we know that it will be a nop. */
7892 if (annul || ! final_sequence)
7893 p[3] = '6';
7894 p += 14;
7895 }
7896 *p++ = '%';
7897 *p++ = 'l';
7898 *p++ = label + '0';
7899 *p++ = '%';
7900 *p++ = '#';
7901 *p = '\0';
7902
7903 return string;
7904 }
7905
7906 /* Emit a library call comparison between floating point X and Y.
7907 COMPARISON is the operator to compare with (EQ, NE, GT, etc).
7908 Return the new operator to be used in the comparison sequence.
7909
7910 TARGET_ARCH64 uses _Qp_* functions, which use pointers to TFmode
7911 values as arguments instead of the TFmode registers themselves,
7912 that's why we cannot call emit_float_lib_cmp. */
7913
7914 rtx
7915 sparc_emit_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison)
7916 {
7917 const char *qpfunc;
7918 rtx slot0, slot1, result, tem, tem2, libfunc;
7919 enum machine_mode mode;
7920 enum rtx_code new_comparison;
7921
7922 switch (comparison)
7923 {
7924 case EQ:
7925 qpfunc = (TARGET_ARCH64 ? "_Qp_feq" : "_Q_feq");
7926 break;
7927
7928 case NE:
7929 qpfunc = (TARGET_ARCH64 ? "_Qp_fne" : "_Q_fne");
7930 break;
7931
7932 case GT:
7933 qpfunc = (TARGET_ARCH64 ? "_Qp_fgt" : "_Q_fgt");
7934 break;
7935
7936 case GE:
7937 qpfunc = (TARGET_ARCH64 ? "_Qp_fge" : "_Q_fge");
7938 break;
7939
7940 case LT:
7941 qpfunc = (TARGET_ARCH64 ? "_Qp_flt" : "_Q_flt");
7942 break;
7943
7944 case LE:
7945 qpfunc = (TARGET_ARCH64 ? "_Qp_fle" : "_Q_fle");
7946 break;
7947
7948 case ORDERED:
7949 case UNORDERED:
7950 case UNGT:
7951 case UNLT:
7952 case UNEQ:
7953 case UNGE:
7954 case UNLE:
7955 case LTGT:
7956 qpfunc = (TARGET_ARCH64 ? "_Qp_cmp" : "_Q_cmp");
7957 break;
7958
7959 default:
7960 gcc_unreachable ();
7961 }
7962
7963 if (TARGET_ARCH64)
7964 {
7965 if (MEM_P (x))
7966 {
7967 tree expr = MEM_EXPR (x);
7968 if (expr)
7969 mark_addressable (expr);
7970 slot0 = x;
7971 }
7972 else
7973 {
7974 slot0 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode));
7975 emit_move_insn (slot0, x);
7976 }
7977
7978 if (MEM_P (y))
7979 {
7980 tree expr = MEM_EXPR (y);
7981 if (expr)
7982 mark_addressable (expr);
7983 slot1 = y;
7984 }
7985 else
7986 {
7987 slot1 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode));
7988 emit_move_insn (slot1, y);
7989 }
7990
7991 libfunc = gen_rtx_SYMBOL_REF (Pmode, qpfunc);
7992 emit_library_call (libfunc, LCT_NORMAL,
7993 DImode, 2,
7994 XEXP (slot0, 0), Pmode,
7995 XEXP (slot1, 0), Pmode);
7996 mode = DImode;
7997 }
7998 else
7999 {
8000 libfunc = gen_rtx_SYMBOL_REF (Pmode, qpfunc);
8001 emit_library_call (libfunc, LCT_NORMAL,
8002 SImode, 2,
8003 x, TFmode, y, TFmode);
8004 mode = SImode;
8005 }
8006
8007
8008 /* Immediately move the result of the libcall into a pseudo
8009 register so reload doesn't clobber the value if it needs
8010 the return register for a spill reg. */
8011 result = gen_reg_rtx (mode);
8012 emit_move_insn (result, hard_libcall_value (mode, libfunc));
8013
8014 switch (comparison)
8015 {
8016 default:
8017 return gen_rtx_NE (VOIDmode, result, const0_rtx);
8018 case ORDERED:
8019 case UNORDERED:
8020 new_comparison = (comparison == UNORDERED ? EQ : NE);
8021 return gen_rtx_fmt_ee (new_comparison, VOIDmode, result, GEN_INT(3));
8022 case UNGT:
8023 case UNGE:
8024 new_comparison = (comparison == UNGT ? GT : NE);
8025 return gen_rtx_fmt_ee (new_comparison, VOIDmode, result, const1_rtx);
8026 case UNLE:
8027 return gen_rtx_NE (VOIDmode, result, const2_rtx);
8028 case UNLT:
8029 tem = gen_reg_rtx (mode);
8030 if (TARGET_ARCH32)
8031 emit_insn (gen_andsi3 (tem, result, const1_rtx));
8032 else
8033 emit_insn (gen_anddi3 (tem, result, const1_rtx));
8034 return gen_rtx_NE (VOIDmode, tem, const0_rtx);
8035 case UNEQ:
8036 case LTGT:
8037 tem = gen_reg_rtx (mode);
8038 if (TARGET_ARCH32)
8039 emit_insn (gen_addsi3 (tem, result, const1_rtx));
8040 else
8041 emit_insn (gen_adddi3 (tem, result, const1_rtx));
8042 tem2 = gen_reg_rtx (mode);
8043 if (TARGET_ARCH32)
8044 emit_insn (gen_andsi3 (tem2, tem, const2_rtx));
8045 else
8046 emit_insn (gen_anddi3 (tem2, tem, const2_rtx));
8047 new_comparison = (comparison == UNEQ ? EQ : NE);
8048 return gen_rtx_fmt_ee (new_comparison, VOIDmode, tem2, const0_rtx);
8049 }
8050
8051 gcc_unreachable ();
8052 }
8053
8054 /* Generate an unsigned DImode to FP conversion. This is the same code
8055 optabs would emit if we didn't have TFmode patterns. */
8056
8057 void
8058 sparc_emit_floatunsdi (rtx *operands, enum machine_mode mode)
8059 {
8060 rtx i0, i1, f0, in, out;
8061
8062 out = operands[0];
8063 in = force_reg (DImode, operands[1]);
8064 rtx_code_label *neglab = gen_label_rtx ();
8065 rtx_code_label *donelab = gen_label_rtx ();
8066 i0 = gen_reg_rtx (DImode);
8067 i1 = gen_reg_rtx (DImode);
8068 f0 = gen_reg_rtx (mode);
8069
8070 emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, DImode, 0, neglab);
8071
8072 emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
8073 emit_jump_insn (gen_jump (donelab));
8074 emit_barrier ();
8075
8076 emit_label (neglab);
8077
8078 emit_insn (gen_lshrdi3 (i0, in, const1_rtx));
8079 emit_insn (gen_anddi3 (i1, in, const1_rtx));
8080 emit_insn (gen_iordi3 (i0, i0, i1));
8081 emit_insn (gen_rtx_SET (VOIDmode, f0, gen_rtx_FLOAT (mode, i0)));
8082 emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
8083
8084 emit_label (donelab);
8085 }
8086
8087 /* Generate an FP to unsigned DImode conversion. This is the same code
8088 optabs would emit if we didn't have TFmode patterns. */
8089
8090 void
8091 sparc_emit_fixunsdi (rtx *operands, enum machine_mode mode)
8092 {
8093 rtx i0, i1, f0, in, out, limit;
8094
8095 out = operands[0];
8096 in = force_reg (mode, operands[1]);
8097 rtx_code_label *neglab = gen_label_rtx ();
8098 rtx_code_label *donelab = gen_label_rtx ();
8099 i0 = gen_reg_rtx (DImode);
8100 i1 = gen_reg_rtx (DImode);
8101 limit = gen_reg_rtx (mode);
8102 f0 = gen_reg_rtx (mode);
8103
8104 emit_move_insn (limit,
8105 CONST_DOUBLE_FROM_REAL_VALUE (
8106 REAL_VALUE_ATOF ("9223372036854775808.0", mode), mode));
8107 emit_cmp_and_jump_insns (in, limit, GE, NULL_RTX, mode, 0, neglab);
8108
8109 emit_insn (gen_rtx_SET (VOIDmode,
8110 out,
8111 gen_rtx_FIX (DImode, gen_rtx_FIX (mode, in))));
8112 emit_jump_insn (gen_jump (donelab));
8113 emit_barrier ();
8114
8115 emit_label (neglab);
8116
8117 emit_insn (gen_rtx_SET (VOIDmode, f0, gen_rtx_MINUS (mode, in, limit)));
8118 emit_insn (gen_rtx_SET (VOIDmode,
8119 i0,
8120 gen_rtx_FIX (DImode, gen_rtx_FIX (mode, f0))));
8121 emit_insn (gen_movdi (i1, const1_rtx));
8122 emit_insn (gen_ashldi3 (i1, i1, GEN_INT (63)));
8123 emit_insn (gen_xordi3 (out, i0, i1));
8124
8125 emit_label (donelab);
8126 }
8127
8128 /* Return the string to output a compare and branch instruction to DEST.
8129 DEST is the destination insn (i.e. the label), INSN is the source,
8130 and OP is the conditional expression. */
8131
8132 const char *
8133 output_cbcond (rtx op, rtx dest, rtx_insn *insn)
8134 {
8135 enum machine_mode mode = GET_MODE (XEXP (op, 0));
8136 enum rtx_code code = GET_CODE (op);
8137 const char *cond_str, *tmpl;
8138 int far, emit_nop, len;
8139 static char string[64];
8140 char size_char;
8141
8142 /* Compare and Branch is limited to +-2KB. If it is too far away,
8143 change
8144
8145 cxbne X, Y, .LC30
8146
8147 to
8148
8149 cxbe X, Y, .+16
8150 nop
8151 ba,pt xcc, .LC30
8152 nop */
8153
8154 len = get_attr_length (insn);
8155
8156 far = len == 4;
8157 emit_nop = len == 2;
8158
8159 if (far)
8160 code = reverse_condition (code);
8161
8162 size_char = ((mode == SImode) ? 'w' : 'x');
8163
8164 switch (code)
8165 {
8166 case NE:
8167 cond_str = "ne";
8168 break;
8169
8170 case EQ:
8171 cond_str = "e";
8172 break;
8173
8174 case GE:
8175 if (mode == CC_NOOVmode || mode == CCX_NOOVmode)
8176 cond_str = "pos";
8177 else
8178 cond_str = "ge";
8179 break;
8180
8181 case GT:
8182 cond_str = "g";
8183 break;
8184
8185 case LE:
8186 cond_str = "le";
8187 break;
8188
8189 case LT:
8190 if (mode == CC_NOOVmode || mode == CCX_NOOVmode)
8191 cond_str = "neg";
8192 else
8193 cond_str = "l";
8194 break;
8195
8196 case GEU:
8197 cond_str = "cc";
8198 break;
8199
8200 case GTU:
8201 cond_str = "gu";
8202 break;
8203
8204 case LEU:
8205 cond_str = "leu";
8206 break;
8207
8208 case LTU:
8209 cond_str = "cs";
8210 break;
8211
8212 default:
8213 gcc_unreachable ();
8214 }
8215
8216 if (far)
8217 {
8218 int veryfar = 1, delta;
8219
8220 if (INSN_ADDRESSES_SET_P ())
8221 {
8222 delta = (INSN_ADDRESSES (INSN_UID (dest))
8223 - INSN_ADDRESSES (INSN_UID (insn)));
8224 /* Leave some instructions for "slop". */
8225 if (delta >= -260000 && delta < 260000)
8226 veryfar = 0;
8227 }
8228
8229 if (veryfar)
8230 tmpl = "c%cb%s\t%%1, %%2, .+16\n\tnop\n\tb\t%%3\n\tnop";
8231 else
8232 tmpl = "c%cb%s\t%%1, %%2, .+16\n\tnop\n\tba,pt\t%%%%xcc, %%3\n\tnop";
8233 }
8234 else
8235 {
8236 if (emit_nop)
8237 tmpl = "c%cb%s\t%%1, %%2, %%3\n\tnop";
8238 else
8239 tmpl = "c%cb%s\t%%1, %%2, %%3";
8240 }
8241
8242 snprintf (string, sizeof(string), tmpl, size_char, cond_str);
8243
8244 return string;
8245 }
8246
8247 /* Return the string to output a conditional branch to LABEL, testing
8248 register REG. LABEL is the operand number of the label; REG is the
8249 operand number of the reg. OP is the conditional expression. The mode
8250 of REG says what kind of comparison we made.
8251
8252 DEST is the destination insn (i.e. the label), INSN is the source.
8253
8254 REVERSED is nonzero if we should reverse the sense of the comparison.
8255
8256 ANNUL is nonzero if we should generate an annulling branch. */
8257
8258 const char *
8259 output_v9branch (rtx op, rtx dest, int reg, int label, int reversed,
8260 int annul, rtx_insn *insn)
8261 {
8262 static char string[64];
8263 enum rtx_code code = GET_CODE (op);
8264 enum machine_mode mode = GET_MODE (XEXP (op, 0));
8265 rtx note;
8266 int far;
8267 char *p;
8268
8269 /* branch on register are limited to +-128KB. If it is too far away,
8270 change
8271
8272 brnz,pt %g1, .LC30
8273
8274 to
8275
8276 brz,pn %g1, .+12
8277 nop
8278 ba,pt %xcc, .LC30
8279
8280 and
8281
8282 brgez,a,pn %o1, .LC29
8283
8284 to
8285
8286 brlz,pt %o1, .+16
8287 nop
8288 ba,pt %xcc, .LC29 */
8289
8290 far = get_attr_length (insn) >= 3;
8291
8292 /* If not floating-point or if EQ or NE, we can just reverse the code. */
8293 if (reversed ^ far)
8294 code = reverse_condition (code);
8295
8296 /* Only 64 bit versions of these instructions exist. */
8297 gcc_assert (mode == DImode);
8298
8299 /* Start by writing the branch condition. */
8300
8301 switch (code)
8302 {
8303 case NE:
8304 strcpy (string, "brnz");
8305 break;
8306
8307 case EQ:
8308 strcpy (string, "brz");
8309 break;
8310
8311 case GE:
8312 strcpy (string, "brgez");
8313 break;
8314
8315 case LT:
8316 strcpy (string, "brlz");
8317 break;
8318
8319 case LE:
8320 strcpy (string, "brlez");
8321 break;
8322
8323 case GT:
8324 strcpy (string, "brgz");
8325 break;
8326
8327 default:
8328 gcc_unreachable ();
8329 }
8330
8331 p = strchr (string, '\0');
8332
8333 /* Now add the annulling, reg, label, and nop. */
8334 if (annul && ! far)
8335 {
8336 strcpy (p, ",a");
8337 p += 2;
8338 }
8339
8340 if (insn && (note = find_reg_note (insn, REG_BR_PROB, NULL_RTX)))
8341 {
8342 strcpy (p,
8343 ((XINT (note, 0) >= REG_BR_PROB_BASE / 2) ^ far)
8344 ? ",pt" : ",pn");
8345 p += 3;
8346 }
8347
8348 *p = p < string + 8 ? '\t' : ' ';
8349 p++;
8350 *p++ = '%';
8351 *p++ = '0' + reg;
8352 *p++ = ',';
8353 *p++ = ' ';
8354 if (far)
8355 {
8356 int veryfar = 1, delta;
8357
8358 if (INSN_ADDRESSES_SET_P ())
8359 {
8360 delta = (INSN_ADDRESSES (INSN_UID (dest))
8361 - INSN_ADDRESSES (INSN_UID (insn)));
8362 /* Leave some instructions for "slop". */
8363 if (delta >= -260000 && delta < 260000)
8364 veryfar = 0;
8365 }
8366
8367 strcpy (p, ".+12\n\t nop\n\t");
8368 /* Skip the next insn if requested or
8369 if we know that it will be a nop. */
8370 if (annul || ! final_sequence)
8371 p[3] = '6';
8372 p += 12;
8373 if (veryfar)
8374 {
8375 strcpy (p, "b\t");
8376 p += 2;
8377 }
8378 else
8379 {
8380 strcpy (p, "ba,pt\t%%xcc, ");
8381 p += 13;
8382 }
8383 }
8384 *p++ = '%';
8385 *p++ = 'l';
8386 *p++ = '0' + label;
8387 *p++ = '%';
8388 *p++ = '#';
8389 *p = '\0';
8390
8391 return string;
8392 }
8393
8394 /* Return 1, if any of the registers of the instruction are %l[0-7] or %o[0-7].
8395 Such instructions cannot be used in the delay slot of return insn on v9.
8396 If TEST is 0, also rename all %i[0-7] registers to their %o[0-7] counterparts.
8397 */
8398
8399 static int
8400 epilogue_renumber (register rtx *where, int test)
8401 {
8402 register const char *fmt;
8403 register int i;
8404 register enum rtx_code code;
8405
8406 if (*where == 0)
8407 return 0;
8408
8409 code = GET_CODE (*where);
8410
8411 switch (code)
8412 {
8413 case REG:
8414 if (REGNO (*where) >= 8 && REGNO (*where) < 24) /* oX or lX */
8415 return 1;
8416 if (! test && REGNO (*where) >= 24 && REGNO (*where) < 32)
8417 *where = gen_rtx_REG (GET_MODE (*where), OUTGOING_REGNO (REGNO(*where)));
8418 case SCRATCH:
8419 case CC0:
8420 case PC:
8421 case CONST_INT:
8422 case CONST_DOUBLE:
8423 return 0;
8424
8425 /* Do not replace the frame pointer with the stack pointer because
8426 it can cause the delayed instruction to load below the stack.
8427 This occurs when instructions like:
8428
8429 (set (reg/i:SI 24 %i0)
8430 (mem/f:SI (plus:SI (reg/f:SI 30 %fp)
8431 (const_int -20 [0xffffffec])) 0))
8432
8433 are in the return delayed slot. */
8434 case PLUS:
8435 if (GET_CODE (XEXP (*where, 0)) == REG
8436 && REGNO (XEXP (*where, 0)) == HARD_FRAME_POINTER_REGNUM
8437 && (GET_CODE (XEXP (*where, 1)) != CONST_INT
8438 || INTVAL (XEXP (*where, 1)) < SPARC_STACK_BIAS))
8439 return 1;
8440 break;
8441
8442 case MEM:
8443 if (SPARC_STACK_BIAS
8444 && GET_CODE (XEXP (*where, 0)) == REG
8445 && REGNO (XEXP (*where, 0)) == HARD_FRAME_POINTER_REGNUM)
8446 return 1;
8447 break;
8448
8449 default:
8450 break;
8451 }
8452
8453 fmt = GET_RTX_FORMAT (code);
8454
8455 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8456 {
8457 if (fmt[i] == 'E')
8458 {
8459 register int j;
8460 for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
8461 if (epilogue_renumber (&(XVECEXP (*where, i, j)), test))
8462 return 1;
8463 }
8464 else if (fmt[i] == 'e'
8465 && epilogue_renumber (&(XEXP (*where, i)), test))
8466 return 1;
8467 }
8468 return 0;
8469 }
8470 \f
8471 /* Leaf functions and non-leaf functions have different needs. */
8472
8473 static const int
8474 reg_leaf_alloc_order[] = REG_LEAF_ALLOC_ORDER;
8475
8476 static const int
8477 reg_nonleaf_alloc_order[] = REG_ALLOC_ORDER;
8478
8479 static const int *const reg_alloc_orders[] = {
8480 reg_leaf_alloc_order,
8481 reg_nonleaf_alloc_order};
8482
8483 void
8484 order_regs_for_local_alloc (void)
8485 {
8486 static int last_order_nonleaf = 1;
8487
8488 if (df_regs_ever_live_p (15) != last_order_nonleaf)
8489 {
8490 last_order_nonleaf = !last_order_nonleaf;
8491 memcpy ((char *) reg_alloc_order,
8492 (const char *) reg_alloc_orders[last_order_nonleaf],
8493 FIRST_PSEUDO_REGISTER * sizeof (int));
8494 }
8495 }
8496 \f
8497 /* Return 1 if REG and MEM are legitimate enough to allow the various
8498 mem<-->reg splits to be run. */
8499
8500 int
8501 sparc_splitdi_legitimate (rtx reg, rtx mem)
8502 {
8503 /* Punt if we are here by mistake. */
8504 gcc_assert (reload_completed);
8505
8506 /* We must have an offsettable memory reference. */
8507 if (! offsettable_memref_p (mem))
8508 return 0;
8509
8510 /* If we have legitimate args for ldd/std, we do not want
8511 the split to happen. */
8512 if ((REGNO (reg) % 2) == 0
8513 && mem_min_alignment (mem, 8))
8514 return 0;
8515
8516 /* Success. */
8517 return 1;
8518 }
8519
8520 /* Like sparc_splitdi_legitimate but for REG <--> REG moves. */
8521
8522 int
8523 sparc_split_regreg_legitimate (rtx reg1, rtx reg2)
8524 {
8525 int regno1, regno2;
8526
8527 if (GET_CODE (reg1) == SUBREG)
8528 reg1 = SUBREG_REG (reg1);
8529 if (GET_CODE (reg1) != REG)
8530 return 0;
8531 regno1 = REGNO (reg1);
8532
8533 if (GET_CODE (reg2) == SUBREG)
8534 reg2 = SUBREG_REG (reg2);
8535 if (GET_CODE (reg2) != REG)
8536 return 0;
8537 regno2 = REGNO (reg2);
8538
8539 if (SPARC_INT_REG_P (regno1) && SPARC_INT_REG_P (regno2))
8540 return 1;
8541
8542 if (TARGET_VIS3)
8543 {
8544 if ((SPARC_INT_REG_P (regno1) && SPARC_FP_REG_P (regno2))
8545 || (SPARC_FP_REG_P (regno1) && SPARC_INT_REG_P (regno2)))
8546 return 1;
8547 }
8548
8549 return 0;
8550 }
8551
8552 /* Return 1 if REGNO (reg1) is even and REGNO (reg1) == REGNO (reg2) - 1.
8553 This makes them candidates for using ldd and std insns.
8554
8555 Note reg1 and reg2 *must* be hard registers. */
8556
8557 int
8558 registers_ok_for_ldd_peep (rtx reg1, rtx reg2)
8559 {
8560 /* We might have been passed a SUBREG. */
8561 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
8562 return 0;
8563
8564 if (REGNO (reg1) % 2 != 0)
8565 return 0;
8566
8567 /* Integer ldd is deprecated in SPARC V9 */
8568 if (TARGET_V9 && SPARC_INT_REG_P (REGNO (reg1)))
8569 return 0;
8570
8571 return (REGNO (reg1) == REGNO (reg2) - 1);
8572 }
8573
8574 /* Return 1 if the addresses in mem1 and mem2 are suitable for use in
8575 an ldd or std insn.
8576
8577 This can only happen when addr1 and addr2, the addresses in mem1
8578 and mem2, are consecutive memory locations (addr1 + 4 == addr2).
8579 addr1 must also be aligned on a 64-bit boundary.
8580
8581 Also iff dependent_reg_rtx is not null it should not be used to
8582 compute the address for mem1, i.e. we cannot optimize a sequence
8583 like:
8584 ld [%o0], %o0
8585 ld [%o0 + 4], %o1
8586 to
8587 ldd [%o0], %o0
8588 nor:
8589 ld [%g3 + 4], %g3
8590 ld [%g3], %g2
8591 to
8592 ldd [%g3], %g2
8593
8594 But, note that the transformation from:
8595 ld [%g2 + 4], %g3
8596 ld [%g2], %g2
8597 to
8598 ldd [%g2], %g2
8599 is perfectly fine. Thus, the peephole2 patterns always pass us
8600 the destination register of the first load, never the second one.
8601
8602 For stores we don't have a similar problem, so dependent_reg_rtx is
8603 NULL_RTX. */
8604
8605 int
8606 mems_ok_for_ldd_peep (rtx mem1, rtx mem2, rtx dependent_reg_rtx)
8607 {
8608 rtx addr1, addr2;
8609 unsigned int reg1;
8610 HOST_WIDE_INT offset1;
8611
8612 /* The mems cannot be volatile. */
8613 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
8614 return 0;
8615
8616 /* MEM1 should be aligned on a 64-bit boundary. */
8617 if (MEM_ALIGN (mem1) < 64)
8618 return 0;
8619
8620 addr1 = XEXP (mem1, 0);
8621 addr2 = XEXP (mem2, 0);
8622
8623 /* Extract a register number and offset (if used) from the first addr. */
8624 if (GET_CODE (addr1) == PLUS)
8625 {
8626 /* If not a REG, return zero. */
8627 if (GET_CODE (XEXP (addr1, 0)) != REG)
8628 return 0;
8629 else
8630 {
8631 reg1 = REGNO (XEXP (addr1, 0));
8632 /* The offset must be constant! */
8633 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
8634 return 0;
8635 offset1 = INTVAL (XEXP (addr1, 1));
8636 }
8637 }
8638 else if (GET_CODE (addr1) != REG)
8639 return 0;
8640 else
8641 {
8642 reg1 = REGNO (addr1);
8643 /* This was a simple (mem (reg)) expression. Offset is 0. */
8644 offset1 = 0;
8645 }
8646
8647 /* Make sure the second address is a (mem (plus (reg) (const_int). */
8648 if (GET_CODE (addr2) != PLUS)
8649 return 0;
8650
8651 if (GET_CODE (XEXP (addr2, 0)) != REG
8652 || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
8653 return 0;
8654
8655 if (reg1 != REGNO (XEXP (addr2, 0)))
8656 return 0;
8657
8658 if (dependent_reg_rtx != NULL_RTX && reg1 == REGNO (dependent_reg_rtx))
8659 return 0;
8660
8661 /* The first offset must be evenly divisible by 8 to ensure the
8662 address is 64 bit aligned. */
8663 if (offset1 % 8 != 0)
8664 return 0;
8665
8666 /* The offset for the second addr must be 4 more than the first addr. */
8667 if (INTVAL (XEXP (addr2, 1)) != offset1 + 4)
8668 return 0;
8669
8670 /* All the tests passed. addr1 and addr2 are valid for ldd and std
8671 instructions. */
8672 return 1;
8673 }
8674
8675 /* Return the widened memory access made of MEM1 and MEM2 in MODE. */
8676
8677 rtx
8678 widen_mem_for_ldd_peep (rtx mem1, rtx mem2, enum machine_mode mode)
8679 {
8680 rtx x = widen_memory_access (mem1, mode, 0);
8681 MEM_NOTRAP_P (x) = MEM_NOTRAP_P (mem1) && MEM_NOTRAP_P (mem2);
8682 return x;
8683 }
8684
8685 /* Return 1 if reg is a pseudo, or is the first register in
8686 a hard register pair. This makes it suitable for use in
8687 ldd and std insns. */
8688
8689 int
8690 register_ok_for_ldd (rtx reg)
8691 {
8692 /* We might have been passed a SUBREG. */
8693 if (!REG_P (reg))
8694 return 0;
8695
8696 if (REGNO (reg) < FIRST_PSEUDO_REGISTER)
8697 return (REGNO (reg) % 2 == 0);
8698
8699 return 1;
8700 }
8701
8702 /* Return 1 if OP, a MEM, has an address which is known to be
8703 aligned to an 8-byte boundary. */
8704
8705 int
8706 memory_ok_for_ldd (rtx op)
8707 {
8708 /* In 64-bit mode, we assume that the address is word-aligned. */
8709 if (TARGET_ARCH32 && !mem_min_alignment (op, 8))
8710 return 0;
8711
8712 if (! can_create_pseudo_p ()
8713 && !strict_memory_address_p (Pmode, XEXP (op, 0)))
8714 return 0;
8715
8716 return 1;
8717 }
8718 \f
8719 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
8720
8721 static bool
8722 sparc_print_operand_punct_valid_p (unsigned char code)
8723 {
8724 if (code == '#'
8725 || code == '*'
8726 || code == '('
8727 || code == ')'
8728 || code == '_'
8729 || code == '&')
8730 return true;
8731
8732 return false;
8733 }
8734
8735 /* Implement TARGET_PRINT_OPERAND.
8736 Print operand X (an rtx) in assembler syntax to file FILE.
8737 CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
8738 For `%' followed by punctuation, CODE is the punctuation and X is null. */
8739
8740 static void
8741 sparc_print_operand (FILE *file, rtx x, int code)
8742 {
8743 switch (code)
8744 {
8745 case '#':
8746 /* Output an insn in a delay slot. */
8747 if (final_sequence)
8748 sparc_indent_opcode = 1;
8749 else
8750 fputs ("\n\t nop", file);
8751 return;
8752 case '*':
8753 /* Output an annul flag if there's nothing for the delay slot and we
8754 are optimizing. This is always used with '(' below.
8755 Sun OS 4.1.1 dbx can't handle an annulled unconditional branch;
8756 this is a dbx bug. So, we only do this when optimizing.
8757 On UltraSPARC, a branch in a delay slot causes a pipeline flush.
8758 Always emit a nop in case the next instruction is a branch. */
8759 if (! final_sequence && (optimize && (int)sparc_cpu < PROCESSOR_V9))
8760 fputs (",a", file);
8761 return;
8762 case '(':
8763 /* Output a 'nop' if there's nothing for the delay slot and we are
8764 not optimizing. This is always used with '*' above. */
8765 if (! final_sequence && ! (optimize && (int)sparc_cpu < PROCESSOR_V9))
8766 fputs ("\n\t nop", file);
8767 else if (final_sequence)
8768 sparc_indent_opcode = 1;
8769 return;
8770 case ')':
8771 /* Output the right displacement from the saved PC on function return.
8772 The caller may have placed an "unimp" insn immediately after the call
8773 so we have to account for it. This insn is used in the 32-bit ABI
8774 when calling a function that returns a non zero-sized structure. The
8775 64-bit ABI doesn't have it. Be careful to have this test be the same
8776 as that for the call. The exception is when sparc_std_struct_return
8777 is enabled, the psABI is followed exactly and the adjustment is made
8778 by the code in sparc_struct_value_rtx. The call emitted is the same
8779 when sparc_std_struct_return is enabled. */
8780 if (!TARGET_ARCH64
8781 && cfun->returns_struct
8782 && !sparc_std_struct_return
8783 && DECL_SIZE (DECL_RESULT (current_function_decl))
8784 && TREE_CODE (DECL_SIZE (DECL_RESULT (current_function_decl)))
8785 == INTEGER_CST
8786 && !integer_zerop (DECL_SIZE (DECL_RESULT (current_function_decl))))
8787 fputs ("12", file);
8788 else
8789 fputc ('8', file);
8790 return;
8791 case '_':
8792 /* Output the Embedded Medium/Anywhere code model base register. */
8793 fputs (EMBMEDANY_BASE_REG, file);
8794 return;
8795 case '&':
8796 /* Print some local dynamic TLS name. */
8797 if (const char *name = get_some_local_dynamic_name ())
8798 assemble_name (file, name);
8799 else
8800 output_operand_lossage ("'%%&' used without any "
8801 "local dynamic TLS references");
8802 return;
8803
8804 case 'Y':
8805 /* Adjust the operand to take into account a RESTORE operation. */
8806 if (GET_CODE (x) == CONST_INT)
8807 break;
8808 else if (GET_CODE (x) != REG)
8809 output_operand_lossage ("invalid %%Y operand");
8810 else if (REGNO (x) < 8)
8811 fputs (reg_names[REGNO (x)], file);
8812 else if (REGNO (x) >= 24 && REGNO (x) < 32)
8813 fputs (reg_names[REGNO (x)-16], file);
8814 else
8815 output_operand_lossage ("invalid %%Y operand");
8816 return;
8817 case 'L':
8818 /* Print out the low order register name of a register pair. */
8819 if (WORDS_BIG_ENDIAN)
8820 fputs (reg_names[REGNO (x)+1], file);
8821 else
8822 fputs (reg_names[REGNO (x)], file);
8823 return;
8824 case 'H':
8825 /* Print out the high order register name of a register pair. */
8826 if (WORDS_BIG_ENDIAN)
8827 fputs (reg_names[REGNO (x)], file);
8828 else
8829 fputs (reg_names[REGNO (x)+1], file);
8830 return;
8831 case 'R':
8832 /* Print out the second register name of a register pair or quad.
8833 I.e., R (%o0) => %o1. */
8834 fputs (reg_names[REGNO (x)+1], file);
8835 return;
8836 case 'S':
8837 /* Print out the third register name of a register quad.
8838 I.e., S (%o0) => %o2. */
8839 fputs (reg_names[REGNO (x)+2], file);
8840 return;
8841 case 'T':
8842 /* Print out the fourth register name of a register quad.
8843 I.e., T (%o0) => %o3. */
8844 fputs (reg_names[REGNO (x)+3], file);
8845 return;
8846 case 'x':
8847 /* Print a condition code register. */
8848 if (REGNO (x) == SPARC_ICC_REG)
8849 {
8850 /* We don't handle CC[X]_NOOVmode because they're not supposed
8851 to occur here. */
8852 if (GET_MODE (x) == CCmode)
8853 fputs ("%icc", file);
8854 else if (GET_MODE (x) == CCXmode)
8855 fputs ("%xcc", file);
8856 else
8857 gcc_unreachable ();
8858 }
8859 else
8860 /* %fccN register */
8861 fputs (reg_names[REGNO (x)], file);
8862 return;
8863 case 'm':
8864 /* Print the operand's address only. */
8865 output_address (XEXP (x, 0));
8866 return;
8867 case 'r':
8868 /* In this case we need a register. Use %g0 if the
8869 operand is const0_rtx. */
8870 if (x == const0_rtx
8871 || (GET_MODE (x) != VOIDmode && x == CONST0_RTX (GET_MODE (x))))
8872 {
8873 fputs ("%g0", file);
8874 return;
8875 }
8876 else
8877 break;
8878
8879 case 'A':
8880 switch (GET_CODE (x))
8881 {
8882 case IOR: fputs ("or", file); break;
8883 case AND: fputs ("and", file); break;
8884 case XOR: fputs ("xor", file); break;
8885 default: output_operand_lossage ("invalid %%A operand");
8886 }
8887 return;
8888
8889 case 'B':
8890 switch (GET_CODE (x))
8891 {
8892 case IOR: fputs ("orn", file); break;
8893 case AND: fputs ("andn", file); break;
8894 case XOR: fputs ("xnor", file); break;
8895 default: output_operand_lossage ("invalid %%B operand");
8896 }
8897 return;
8898
8899 /* This is used by the conditional move instructions. */
8900 case 'C':
8901 {
8902 enum rtx_code rc = GET_CODE (x);
8903
8904 switch (rc)
8905 {
8906 case NE: fputs ("ne", file); break;
8907 case EQ: fputs ("e", file); break;
8908 case GE: fputs ("ge", file); break;
8909 case GT: fputs ("g", file); break;
8910 case LE: fputs ("le", file); break;
8911 case LT: fputs ("l", file); break;
8912 case GEU: fputs ("geu", file); break;
8913 case GTU: fputs ("gu", file); break;
8914 case LEU: fputs ("leu", file); break;
8915 case LTU: fputs ("lu", file); break;
8916 case LTGT: fputs ("lg", file); break;
8917 case UNORDERED: fputs ("u", file); break;
8918 case ORDERED: fputs ("o", file); break;
8919 case UNLT: fputs ("ul", file); break;
8920 case UNLE: fputs ("ule", file); break;
8921 case UNGT: fputs ("ug", file); break;
8922 case UNGE: fputs ("uge", file); break;
8923 case UNEQ: fputs ("ue", file); break;
8924 default: output_operand_lossage ("invalid %%C operand");
8925 }
8926 return;
8927 }
8928
8929 /* This are used by the movr instruction pattern. */
8930 case 'D':
8931 {
8932 enum rtx_code rc = GET_CODE (x);
8933 switch (rc)
8934 {
8935 case NE: fputs ("ne", file); break;
8936 case EQ: fputs ("e", file); break;
8937 case GE: fputs ("gez", file); break;
8938 case LT: fputs ("lz", file); break;
8939 case LE: fputs ("lez", file); break;
8940 case GT: fputs ("gz", file); break;
8941 default: output_operand_lossage ("invalid %%D operand");
8942 }
8943 return;
8944 }
8945
8946 case 'b':
8947 {
8948 /* Print a sign-extended character. */
8949 int i = trunc_int_for_mode (INTVAL (x), QImode);
8950 fprintf (file, "%d", i);
8951 return;
8952 }
8953
8954 case 'f':
8955 /* Operand must be a MEM; write its address. */
8956 if (GET_CODE (x) != MEM)
8957 output_operand_lossage ("invalid %%f operand");
8958 output_address (XEXP (x, 0));
8959 return;
8960
8961 case 's':
8962 {
8963 /* Print a sign-extended 32-bit value. */
8964 HOST_WIDE_INT i;
8965 if (GET_CODE(x) == CONST_INT)
8966 i = INTVAL (x);
8967 else if (GET_CODE(x) == CONST_DOUBLE)
8968 i = CONST_DOUBLE_LOW (x);
8969 else
8970 {
8971 output_operand_lossage ("invalid %%s operand");
8972 return;
8973 }
8974 i = trunc_int_for_mode (i, SImode);
8975 fprintf (file, HOST_WIDE_INT_PRINT_DEC, i);
8976 return;
8977 }
8978
8979 case 0:
8980 /* Do nothing special. */
8981 break;
8982
8983 default:
8984 /* Undocumented flag. */
8985 output_operand_lossage ("invalid operand output code");
8986 }
8987
8988 if (GET_CODE (x) == REG)
8989 fputs (reg_names[REGNO (x)], file);
8990 else if (GET_CODE (x) == MEM)
8991 {
8992 fputc ('[', file);
8993 /* Poor Sun assembler doesn't understand absolute addressing. */
8994 if (CONSTANT_P (XEXP (x, 0)))
8995 fputs ("%g0+", file);
8996 output_address (XEXP (x, 0));
8997 fputc (']', file);
8998 }
8999 else if (GET_CODE (x) == HIGH)
9000 {
9001 fputs ("%hi(", file);
9002 output_addr_const (file, XEXP (x, 0));
9003 fputc (')', file);
9004 }
9005 else if (GET_CODE (x) == LO_SUM)
9006 {
9007 sparc_print_operand (file, XEXP (x, 0), 0);
9008 if (TARGET_CM_MEDMID)
9009 fputs ("+%l44(", file);
9010 else
9011 fputs ("+%lo(", file);
9012 output_addr_const (file, XEXP (x, 1));
9013 fputc (')', file);
9014 }
9015 else if (GET_CODE (x) == CONST_DOUBLE
9016 && (GET_MODE (x) == VOIDmode
9017 || GET_MODE_CLASS (GET_MODE (x)) == MODE_INT))
9018 {
9019 if (CONST_DOUBLE_HIGH (x) == 0)
9020 fprintf (file, "%u", (unsigned int) CONST_DOUBLE_LOW (x));
9021 else if (CONST_DOUBLE_HIGH (x) == -1
9022 && CONST_DOUBLE_LOW (x) < 0)
9023 fprintf (file, "%d", (int) CONST_DOUBLE_LOW (x));
9024 else
9025 output_operand_lossage ("long long constant not a valid immediate operand");
9026 }
9027 else if (GET_CODE (x) == CONST_DOUBLE)
9028 output_operand_lossage ("floating point constant not a valid immediate operand");
9029 else { output_addr_const (file, x); }
9030 }
9031
9032 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
9033
9034 static void
9035 sparc_print_operand_address (FILE *file, rtx x)
9036 {
9037 register rtx base, index = 0;
9038 int offset = 0;
9039 register rtx addr = x;
9040
9041 if (REG_P (addr))
9042 fputs (reg_names[REGNO (addr)], file);
9043 else if (GET_CODE (addr) == PLUS)
9044 {
9045 if (CONST_INT_P (XEXP (addr, 0)))
9046 offset = INTVAL (XEXP (addr, 0)), base = XEXP (addr, 1);
9047 else if (CONST_INT_P (XEXP (addr, 1)))
9048 offset = INTVAL (XEXP (addr, 1)), base = XEXP (addr, 0);
9049 else
9050 base = XEXP (addr, 0), index = XEXP (addr, 1);
9051 if (GET_CODE (base) == LO_SUM)
9052 {
9053 gcc_assert (USE_AS_OFFSETABLE_LO10
9054 && TARGET_ARCH64
9055 && ! TARGET_CM_MEDMID);
9056 output_operand (XEXP (base, 0), 0);
9057 fputs ("+%lo(", file);
9058 output_address (XEXP (base, 1));
9059 fprintf (file, ")+%d", offset);
9060 }
9061 else
9062 {
9063 fputs (reg_names[REGNO (base)], file);
9064 if (index == 0)
9065 fprintf (file, "%+d", offset);
9066 else if (REG_P (index))
9067 fprintf (file, "+%s", reg_names[REGNO (index)]);
9068 else if (GET_CODE (index) == SYMBOL_REF
9069 || GET_CODE (index) == LABEL_REF
9070 || GET_CODE (index) == CONST)
9071 fputc ('+', file), output_addr_const (file, index);
9072 else gcc_unreachable ();
9073 }
9074 }
9075 else if (GET_CODE (addr) == MINUS
9076 && GET_CODE (XEXP (addr, 1)) == LABEL_REF)
9077 {
9078 output_addr_const (file, XEXP (addr, 0));
9079 fputs ("-(", file);
9080 output_addr_const (file, XEXP (addr, 1));
9081 fputs ("-.)", file);
9082 }
9083 else if (GET_CODE (addr) == LO_SUM)
9084 {
9085 output_operand (XEXP (addr, 0), 0);
9086 if (TARGET_CM_MEDMID)
9087 fputs ("+%l44(", file);
9088 else
9089 fputs ("+%lo(", file);
9090 output_address (XEXP (addr, 1));
9091 fputc (')', file);
9092 }
9093 else if (flag_pic
9094 && GET_CODE (addr) == CONST
9095 && GET_CODE (XEXP (addr, 0)) == MINUS
9096 && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST
9097 && GET_CODE (XEXP (XEXP (XEXP (addr, 0), 1), 0)) == MINUS
9098 && XEXP (XEXP (XEXP (XEXP (addr, 0), 1), 0), 1) == pc_rtx)
9099 {
9100 addr = XEXP (addr, 0);
9101 output_addr_const (file, XEXP (addr, 0));
9102 /* Group the args of the second CONST in parenthesis. */
9103 fputs ("-(", file);
9104 /* Skip past the second CONST--it does nothing for us. */
9105 output_addr_const (file, XEXP (XEXP (addr, 1), 0));
9106 /* Close the parenthesis. */
9107 fputc (')', file);
9108 }
9109 else
9110 {
9111 output_addr_const (file, addr);
9112 }
9113 }
9114 \f
9115 /* Target hook for assembling integer objects. The sparc version has
9116 special handling for aligned DI-mode objects. */
9117
9118 static bool
9119 sparc_assemble_integer (rtx x, unsigned int size, int aligned_p)
9120 {
9121 /* ??? We only output .xword's for symbols and only then in environments
9122 where the assembler can handle them. */
9123 if (aligned_p && size == 8
9124 && (GET_CODE (x) != CONST_INT && GET_CODE (x) != CONST_DOUBLE))
9125 {
9126 if (TARGET_V9)
9127 {
9128 assemble_integer_with_op ("\t.xword\t", x);
9129 return true;
9130 }
9131 else
9132 {
9133 assemble_aligned_integer (4, const0_rtx);
9134 assemble_aligned_integer (4, x);
9135 return true;
9136 }
9137 }
9138 return default_assemble_integer (x, size, aligned_p);
9139 }
9140 \f
9141 /* Return the value of a code used in the .proc pseudo-op that says
9142 what kind of result this function returns. For non-C types, we pick
9143 the closest C type. */
9144
9145 #ifndef SHORT_TYPE_SIZE
9146 #define SHORT_TYPE_SIZE (BITS_PER_UNIT * 2)
9147 #endif
9148
9149 #ifndef INT_TYPE_SIZE
9150 #define INT_TYPE_SIZE BITS_PER_WORD
9151 #endif
9152
9153 #ifndef LONG_TYPE_SIZE
9154 #define LONG_TYPE_SIZE BITS_PER_WORD
9155 #endif
9156
9157 #ifndef LONG_LONG_TYPE_SIZE
9158 #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
9159 #endif
9160
9161 #ifndef FLOAT_TYPE_SIZE
9162 #define FLOAT_TYPE_SIZE BITS_PER_WORD
9163 #endif
9164
9165 #ifndef DOUBLE_TYPE_SIZE
9166 #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
9167 #endif
9168
9169 #ifndef LONG_DOUBLE_TYPE_SIZE
9170 #define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
9171 #endif
9172
9173 unsigned long
9174 sparc_type_code (register tree type)
9175 {
9176 register unsigned long qualifiers = 0;
9177 register unsigned shift;
9178
9179 /* Only the first 30 bits of the qualifier are valid. We must refrain from
9180 setting more, since some assemblers will give an error for this. Also,
9181 we must be careful to avoid shifts of 32 bits or more to avoid getting
9182 unpredictable results. */
9183
9184 for (shift = 6; shift < 30; shift += 2, type = TREE_TYPE (type))
9185 {
9186 switch (TREE_CODE (type))
9187 {
9188 case ERROR_MARK:
9189 return qualifiers;
9190
9191 case ARRAY_TYPE:
9192 qualifiers |= (3 << shift);
9193 break;
9194
9195 case FUNCTION_TYPE:
9196 case METHOD_TYPE:
9197 qualifiers |= (2 << shift);
9198 break;
9199
9200 case POINTER_TYPE:
9201 case REFERENCE_TYPE:
9202 case OFFSET_TYPE:
9203 qualifiers |= (1 << shift);
9204 break;
9205
9206 case RECORD_TYPE:
9207 return (qualifiers | 8);
9208
9209 case UNION_TYPE:
9210 case QUAL_UNION_TYPE:
9211 return (qualifiers | 9);
9212
9213 case ENUMERAL_TYPE:
9214 return (qualifiers | 10);
9215
9216 case VOID_TYPE:
9217 return (qualifiers | 16);
9218
9219 case INTEGER_TYPE:
9220 /* If this is a range type, consider it to be the underlying
9221 type. */
9222 if (TREE_TYPE (type) != 0)
9223 break;
9224
9225 /* Carefully distinguish all the standard types of C,
9226 without messing up if the language is not C. We do this by
9227 testing TYPE_PRECISION and TYPE_UNSIGNED. The old code used to
9228 look at both the names and the above fields, but that's redundant.
9229 Any type whose size is between two C types will be considered
9230 to be the wider of the two types. Also, we do not have a
9231 special code to use for "long long", so anything wider than
9232 long is treated the same. Note that we can't distinguish
9233 between "int" and "long" in this code if they are the same
9234 size, but that's fine, since neither can the assembler. */
9235
9236 if (TYPE_PRECISION (type) <= CHAR_TYPE_SIZE)
9237 return (qualifiers | (TYPE_UNSIGNED (type) ? 12 : 2));
9238
9239 else if (TYPE_PRECISION (type) <= SHORT_TYPE_SIZE)
9240 return (qualifiers | (TYPE_UNSIGNED (type) ? 13 : 3));
9241
9242 else if (TYPE_PRECISION (type) <= INT_TYPE_SIZE)
9243 return (qualifiers | (TYPE_UNSIGNED (type) ? 14 : 4));
9244
9245 else
9246 return (qualifiers | (TYPE_UNSIGNED (type) ? 15 : 5));
9247
9248 case REAL_TYPE:
9249 /* If this is a range type, consider it to be the underlying
9250 type. */
9251 if (TREE_TYPE (type) != 0)
9252 break;
9253
9254 /* Carefully distinguish all the standard types of C,
9255 without messing up if the language is not C. */
9256
9257 if (TYPE_PRECISION (type) == FLOAT_TYPE_SIZE)
9258 return (qualifiers | 6);
9259
9260 else
9261 return (qualifiers | 7);
9262
9263 case COMPLEX_TYPE: /* GNU Fortran COMPLEX type. */
9264 /* ??? We need to distinguish between double and float complex types,
9265 but I don't know how yet because I can't reach this code from
9266 existing front-ends. */
9267 return (qualifiers | 7); /* Who knows? */
9268
9269 case VECTOR_TYPE:
9270 case BOOLEAN_TYPE: /* Boolean truth value type. */
9271 case LANG_TYPE:
9272 case NULLPTR_TYPE:
9273 return qualifiers;
9274
9275 default:
9276 gcc_unreachable (); /* Not a type! */
9277 }
9278 }
9279
9280 return qualifiers;
9281 }
9282 \f
9283 /* Nested function support. */
9284
9285 /* Emit RTL insns to initialize the variable parts of a trampoline.
9286 FNADDR is an RTX for the address of the function's pure code.
9287 CXT is an RTX for the static chain value for the function.
9288
9289 This takes 16 insns: 2 shifts & 2 ands (to split up addresses), 4 sethi
9290 (to load in opcodes), 4 iors (to merge address and opcodes), and 4 writes
9291 (to store insns). This is a bit excessive. Perhaps a different
9292 mechanism would be better here.
9293
9294 Emit enough FLUSH insns to synchronize the data and instruction caches. */
9295
9296 static void
9297 sparc32_initialize_trampoline (rtx m_tramp, rtx fnaddr, rtx cxt)
9298 {
9299 /* SPARC 32-bit trampoline:
9300
9301 sethi %hi(fn), %g1
9302 sethi %hi(static), %g2
9303 jmp %g1+%lo(fn)
9304 or %g2, %lo(static), %g2
9305
9306 SETHI i,r = 00rr rrr1 00ii iiii iiii iiii iiii iiii
9307 JMPL r+i,d = 10dd ddd1 1100 0rrr rr1i iiii iiii iiii
9308 */
9309
9310 emit_move_insn
9311 (adjust_address (m_tramp, SImode, 0),
9312 expand_binop (SImode, ior_optab,
9313 expand_shift (RSHIFT_EXPR, SImode, fnaddr, 10, 0, 1),
9314 GEN_INT (trunc_int_for_mode (0x03000000, SImode)),
9315 NULL_RTX, 1, OPTAB_DIRECT));
9316
9317 emit_move_insn
9318 (adjust_address (m_tramp, SImode, 4),
9319 expand_binop (SImode, ior_optab,
9320 expand_shift (RSHIFT_EXPR, SImode, cxt, 10, 0, 1),
9321 GEN_INT (trunc_int_for_mode (0x05000000, SImode)),
9322 NULL_RTX, 1, OPTAB_DIRECT));
9323
9324 emit_move_insn
9325 (adjust_address (m_tramp, SImode, 8),
9326 expand_binop (SImode, ior_optab,
9327 expand_and (SImode, fnaddr, GEN_INT (0x3ff), NULL_RTX),
9328 GEN_INT (trunc_int_for_mode (0x81c06000, SImode)),
9329 NULL_RTX, 1, OPTAB_DIRECT));
9330
9331 emit_move_insn
9332 (adjust_address (m_tramp, SImode, 12),
9333 expand_binop (SImode, ior_optab,
9334 expand_and (SImode, cxt, GEN_INT (0x3ff), NULL_RTX),
9335 GEN_INT (trunc_int_for_mode (0x8410a000, SImode)),
9336 NULL_RTX, 1, OPTAB_DIRECT));
9337
9338 /* On UltraSPARC a flush flushes an entire cache line. The trampoline is
9339 aligned on a 16 byte boundary so one flush clears it all. */
9340 emit_insn (gen_flushsi (validize_mem (adjust_address (m_tramp, SImode, 0))));
9341 if (sparc_cpu != PROCESSOR_ULTRASPARC
9342 && sparc_cpu != PROCESSOR_ULTRASPARC3
9343 && sparc_cpu != PROCESSOR_NIAGARA
9344 && sparc_cpu != PROCESSOR_NIAGARA2
9345 && sparc_cpu != PROCESSOR_NIAGARA3
9346 && sparc_cpu != PROCESSOR_NIAGARA4)
9347 emit_insn (gen_flushsi (validize_mem (adjust_address (m_tramp, SImode, 8))));
9348
9349 /* Call __enable_execute_stack after writing onto the stack to make sure
9350 the stack address is accessible. */
9351 #ifdef HAVE_ENABLE_EXECUTE_STACK
9352 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
9353 LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
9354 #endif
9355
9356 }
9357
9358 /* The 64-bit version is simpler because it makes more sense to load the
9359 values as "immediate" data out of the trampoline. It's also easier since
9360 we can read the PC without clobbering a register. */
9361
9362 static void
9363 sparc64_initialize_trampoline (rtx m_tramp, rtx fnaddr, rtx cxt)
9364 {
9365 /* SPARC 64-bit trampoline:
9366
9367 rd %pc, %g1
9368 ldx [%g1+24], %g5
9369 jmp %g5
9370 ldx [%g1+16], %g5
9371 +16 bytes data
9372 */
9373
9374 emit_move_insn (adjust_address (m_tramp, SImode, 0),
9375 GEN_INT (trunc_int_for_mode (0x83414000, SImode)));
9376 emit_move_insn (adjust_address (m_tramp, SImode, 4),
9377 GEN_INT (trunc_int_for_mode (0xca586018, SImode)));
9378 emit_move_insn (adjust_address (m_tramp, SImode, 8),
9379 GEN_INT (trunc_int_for_mode (0x81c14000, SImode)));
9380 emit_move_insn (adjust_address (m_tramp, SImode, 12),
9381 GEN_INT (trunc_int_for_mode (0xca586010, SImode)));
9382 emit_move_insn (adjust_address (m_tramp, DImode, 16), cxt);
9383 emit_move_insn (adjust_address (m_tramp, DImode, 24), fnaddr);
9384 emit_insn (gen_flushdi (validize_mem (adjust_address (m_tramp, DImode, 0))));
9385
9386 if (sparc_cpu != PROCESSOR_ULTRASPARC
9387 && sparc_cpu != PROCESSOR_ULTRASPARC3
9388 && sparc_cpu != PROCESSOR_NIAGARA
9389 && sparc_cpu != PROCESSOR_NIAGARA2
9390 && sparc_cpu != PROCESSOR_NIAGARA3
9391 && sparc_cpu != PROCESSOR_NIAGARA4)
9392 emit_insn (gen_flushdi (validize_mem (adjust_address (m_tramp, DImode, 8))));
9393
9394 /* Call __enable_execute_stack after writing onto the stack to make sure
9395 the stack address is accessible. */
9396 #ifdef HAVE_ENABLE_EXECUTE_STACK
9397 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
9398 LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
9399 #endif
9400 }
9401
9402 /* Worker for TARGET_TRAMPOLINE_INIT. */
9403
9404 static void
9405 sparc_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
9406 {
9407 rtx fnaddr = force_reg (Pmode, XEXP (DECL_RTL (fndecl), 0));
9408 cxt = force_reg (Pmode, cxt);
9409 if (TARGET_ARCH64)
9410 sparc64_initialize_trampoline (m_tramp, fnaddr, cxt);
9411 else
9412 sparc32_initialize_trampoline (m_tramp, fnaddr, cxt);
9413 }
9414 \f
9415 /* Adjust the cost of a scheduling dependency. Return the new cost of
9416 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
9417
9418 static int
9419 supersparc_adjust_cost (rtx_insn *insn, rtx link, rtx_insn *dep_insn, int cost)
9420 {
9421 enum attr_type insn_type;
9422
9423 if (! recog_memoized (insn))
9424 return 0;
9425
9426 insn_type = get_attr_type (insn);
9427
9428 if (REG_NOTE_KIND (link) == 0)
9429 {
9430 /* Data dependency; DEP_INSN writes a register that INSN reads some
9431 cycles later. */
9432
9433 /* if a load, then the dependence must be on the memory address;
9434 add an extra "cycle". Note that the cost could be two cycles
9435 if the reg was written late in an instruction group; we ca not tell
9436 here. */
9437 if (insn_type == TYPE_LOAD || insn_type == TYPE_FPLOAD)
9438 return cost + 3;
9439
9440 /* Get the delay only if the address of the store is the dependence. */
9441 if (insn_type == TYPE_STORE || insn_type == TYPE_FPSTORE)
9442 {
9443 rtx pat = PATTERN(insn);
9444 rtx dep_pat = PATTERN (dep_insn);
9445
9446 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
9447 return cost; /* This should not happen! */
9448
9449 /* The dependency between the two instructions was on the data that
9450 is being stored. Assume that this implies that the address of the
9451 store is not dependent. */
9452 if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
9453 return cost;
9454
9455 return cost + 3; /* An approximation. */
9456 }
9457
9458 /* A shift instruction cannot receive its data from an instruction
9459 in the same cycle; add a one cycle penalty. */
9460 if (insn_type == TYPE_SHIFT)
9461 return cost + 3; /* Split before cascade into shift. */
9462 }
9463 else
9464 {
9465 /* Anti- or output- dependency; DEP_INSN reads/writes a register that
9466 INSN writes some cycles later. */
9467
9468 /* These are only significant for the fpu unit; writing a fp reg before
9469 the fpu has finished with it stalls the processor. */
9470
9471 /* Reusing an integer register causes no problems. */
9472 if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
9473 return 0;
9474 }
9475
9476 return cost;
9477 }
9478
9479 static int
9480 hypersparc_adjust_cost (rtx_insn *insn, rtx link, rtx_insn *dep_insn, int cost)
9481 {
9482 enum attr_type insn_type, dep_type;
9483 rtx pat = PATTERN(insn);
9484 rtx dep_pat = PATTERN (dep_insn);
9485
9486 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
9487 return cost;
9488
9489 insn_type = get_attr_type (insn);
9490 dep_type = get_attr_type (dep_insn);
9491
9492 switch (REG_NOTE_KIND (link))
9493 {
9494 case 0:
9495 /* Data dependency; DEP_INSN writes a register that INSN reads some
9496 cycles later. */
9497
9498 switch (insn_type)
9499 {
9500 case TYPE_STORE:
9501 case TYPE_FPSTORE:
9502 /* Get the delay iff the address of the store is the dependence. */
9503 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
9504 return cost;
9505
9506 if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
9507 return cost;
9508 return cost + 3;
9509
9510 case TYPE_LOAD:
9511 case TYPE_SLOAD:
9512 case TYPE_FPLOAD:
9513 /* If a load, then the dependence must be on the memory address. If
9514 the addresses aren't equal, then it might be a false dependency */
9515 if (dep_type == TYPE_STORE || dep_type == TYPE_FPSTORE)
9516 {
9517 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET
9518 || GET_CODE (SET_DEST (dep_pat)) != MEM
9519 || GET_CODE (SET_SRC (pat)) != MEM
9520 || ! rtx_equal_p (XEXP (SET_DEST (dep_pat), 0),
9521 XEXP (SET_SRC (pat), 0)))
9522 return cost + 2;
9523
9524 return cost + 8;
9525 }
9526 break;
9527
9528 case TYPE_BRANCH:
9529 /* Compare to branch latency is 0. There is no benefit from
9530 separating compare and branch. */
9531 if (dep_type == TYPE_COMPARE)
9532 return 0;
9533 /* Floating point compare to branch latency is less than
9534 compare to conditional move. */
9535 if (dep_type == TYPE_FPCMP)
9536 return cost - 1;
9537 break;
9538 default:
9539 break;
9540 }
9541 break;
9542
9543 case REG_DEP_ANTI:
9544 /* Anti-dependencies only penalize the fpu unit. */
9545 if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
9546 return 0;
9547 break;
9548
9549 default:
9550 break;
9551 }
9552
9553 return cost;
9554 }
9555
9556 static int
9557 sparc_adjust_cost(rtx_insn *insn, rtx link, rtx_insn *dep, int cost)
9558 {
9559 switch (sparc_cpu)
9560 {
9561 case PROCESSOR_SUPERSPARC:
9562 cost = supersparc_adjust_cost (insn, link, dep, cost);
9563 break;
9564 case PROCESSOR_HYPERSPARC:
9565 case PROCESSOR_SPARCLITE86X:
9566 cost = hypersparc_adjust_cost (insn, link, dep, cost);
9567 break;
9568 default:
9569 break;
9570 }
9571 return cost;
9572 }
9573
9574 static void
9575 sparc_sched_init (FILE *dump ATTRIBUTE_UNUSED,
9576 int sched_verbose ATTRIBUTE_UNUSED,
9577 int max_ready ATTRIBUTE_UNUSED)
9578 {}
9579
9580 static int
9581 sparc_use_sched_lookahead (void)
9582 {
9583 if (sparc_cpu == PROCESSOR_NIAGARA
9584 || sparc_cpu == PROCESSOR_NIAGARA2
9585 || sparc_cpu == PROCESSOR_NIAGARA3)
9586 return 0;
9587 if (sparc_cpu == PROCESSOR_NIAGARA4)
9588 return 2;
9589 if (sparc_cpu == PROCESSOR_ULTRASPARC
9590 || sparc_cpu == PROCESSOR_ULTRASPARC3)
9591 return 4;
9592 if ((1 << sparc_cpu) &
9593 ((1 << PROCESSOR_SUPERSPARC) | (1 << PROCESSOR_HYPERSPARC) |
9594 (1 << PROCESSOR_SPARCLITE86X)))
9595 return 3;
9596 return 0;
9597 }
9598
9599 static int
9600 sparc_issue_rate (void)
9601 {
9602 switch (sparc_cpu)
9603 {
9604 case PROCESSOR_NIAGARA:
9605 case PROCESSOR_NIAGARA2:
9606 case PROCESSOR_NIAGARA3:
9607 default:
9608 return 1;
9609 case PROCESSOR_NIAGARA4:
9610 case PROCESSOR_V9:
9611 /* Assume V9 processors are capable of at least dual-issue. */
9612 return 2;
9613 case PROCESSOR_SUPERSPARC:
9614 return 3;
9615 case PROCESSOR_HYPERSPARC:
9616 case PROCESSOR_SPARCLITE86X:
9617 return 2;
9618 case PROCESSOR_ULTRASPARC:
9619 case PROCESSOR_ULTRASPARC3:
9620 return 4;
9621 }
9622 }
9623
9624 static int
9625 set_extends (rtx_insn *insn)
9626 {
9627 register rtx pat = PATTERN (insn);
9628
9629 switch (GET_CODE (SET_SRC (pat)))
9630 {
9631 /* Load and some shift instructions zero extend. */
9632 case MEM:
9633 case ZERO_EXTEND:
9634 /* sethi clears the high bits */
9635 case HIGH:
9636 /* LO_SUM is used with sethi. sethi cleared the high
9637 bits and the values used with lo_sum are positive */
9638 case LO_SUM:
9639 /* Store flag stores 0 or 1 */
9640 case LT: case LTU:
9641 case GT: case GTU:
9642 case LE: case LEU:
9643 case GE: case GEU:
9644 case EQ:
9645 case NE:
9646 return 1;
9647 case AND:
9648 {
9649 rtx op0 = XEXP (SET_SRC (pat), 0);
9650 rtx op1 = XEXP (SET_SRC (pat), 1);
9651 if (GET_CODE (op1) == CONST_INT)
9652 return INTVAL (op1) >= 0;
9653 if (GET_CODE (op0) != REG)
9654 return 0;
9655 if (sparc_check_64 (op0, insn) == 1)
9656 return 1;
9657 return (GET_CODE (op1) == REG && sparc_check_64 (op1, insn) == 1);
9658 }
9659 case IOR:
9660 case XOR:
9661 {
9662 rtx op0 = XEXP (SET_SRC (pat), 0);
9663 rtx op1 = XEXP (SET_SRC (pat), 1);
9664 if (GET_CODE (op0) != REG || sparc_check_64 (op0, insn) <= 0)
9665 return 0;
9666 if (GET_CODE (op1) == CONST_INT)
9667 return INTVAL (op1) >= 0;
9668 return (GET_CODE (op1) == REG && sparc_check_64 (op1, insn) == 1);
9669 }
9670 case LSHIFTRT:
9671 return GET_MODE (SET_SRC (pat)) == SImode;
9672 /* Positive integers leave the high bits zero. */
9673 case CONST_DOUBLE:
9674 return ! (CONST_DOUBLE_LOW (SET_SRC (pat)) & 0x80000000);
9675 case CONST_INT:
9676 return ! (INTVAL (SET_SRC (pat)) & 0x80000000);
9677 case ASHIFTRT:
9678 case SIGN_EXTEND:
9679 return - (GET_MODE (SET_SRC (pat)) == SImode);
9680 case REG:
9681 return sparc_check_64 (SET_SRC (pat), insn);
9682 default:
9683 return 0;
9684 }
9685 }
9686
9687 /* We _ought_ to have only one kind per function, but... */
9688 static GTY(()) rtx sparc_addr_diff_list;
9689 static GTY(()) rtx sparc_addr_list;
9690
9691 void
9692 sparc_defer_case_vector (rtx lab, rtx vec, int diff)
9693 {
9694 vec = gen_rtx_EXPR_LIST (VOIDmode, lab, vec);
9695 if (diff)
9696 sparc_addr_diff_list
9697 = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_diff_list);
9698 else
9699 sparc_addr_list = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_list);
9700 }
9701
9702 static void
9703 sparc_output_addr_vec (rtx vec)
9704 {
9705 rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
9706 int idx, vlen = XVECLEN (body, 0);
9707
9708 #ifdef ASM_OUTPUT_ADDR_VEC_START
9709 ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
9710 #endif
9711
9712 #ifdef ASM_OUTPUT_CASE_LABEL
9713 ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
9714 NEXT_INSN (lab));
9715 #else
9716 (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
9717 #endif
9718
9719 for (idx = 0; idx < vlen; idx++)
9720 {
9721 ASM_OUTPUT_ADDR_VEC_ELT
9722 (asm_out_file, CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 0, idx), 0)));
9723 }
9724
9725 #ifdef ASM_OUTPUT_ADDR_VEC_END
9726 ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
9727 #endif
9728 }
9729
9730 static void
9731 sparc_output_addr_diff_vec (rtx vec)
9732 {
9733 rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
9734 rtx base = XEXP (XEXP (body, 0), 0);
9735 int idx, vlen = XVECLEN (body, 1);
9736
9737 #ifdef ASM_OUTPUT_ADDR_VEC_START
9738 ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
9739 #endif
9740
9741 #ifdef ASM_OUTPUT_CASE_LABEL
9742 ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
9743 NEXT_INSN (lab));
9744 #else
9745 (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
9746 #endif
9747
9748 for (idx = 0; idx < vlen; idx++)
9749 {
9750 ASM_OUTPUT_ADDR_DIFF_ELT
9751 (asm_out_file,
9752 body,
9753 CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 1, idx), 0)),
9754 CODE_LABEL_NUMBER (base));
9755 }
9756
9757 #ifdef ASM_OUTPUT_ADDR_VEC_END
9758 ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
9759 #endif
9760 }
9761
9762 static void
9763 sparc_output_deferred_case_vectors (void)
9764 {
9765 rtx t;
9766 int align;
9767
9768 if (sparc_addr_list == NULL_RTX
9769 && sparc_addr_diff_list == NULL_RTX)
9770 return;
9771
9772 /* Align to cache line in the function's code section. */
9773 switch_to_section (current_function_section ());
9774
9775 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
9776 if (align > 0)
9777 ASM_OUTPUT_ALIGN (asm_out_file, align);
9778
9779 for (t = sparc_addr_list; t ; t = XEXP (t, 1))
9780 sparc_output_addr_vec (XEXP (t, 0));
9781 for (t = sparc_addr_diff_list; t ; t = XEXP (t, 1))
9782 sparc_output_addr_diff_vec (XEXP (t, 0));
9783
9784 sparc_addr_list = sparc_addr_diff_list = NULL_RTX;
9785 }
9786
9787 /* Return 0 if the high 32 bits of X (the low word of X, if DImode) are
9788 unknown. Return 1 if the high bits are zero, -1 if the register is
9789 sign extended. */
9790 int
9791 sparc_check_64 (rtx x, rtx_insn *insn)
9792 {
9793 /* If a register is set only once it is safe to ignore insns this
9794 code does not know how to handle. The loop will either recognize
9795 the single set and return the correct value or fail to recognize
9796 it and return 0. */
9797 int set_once = 0;
9798 rtx y = x;
9799
9800 gcc_assert (GET_CODE (x) == REG);
9801
9802 if (GET_MODE (x) == DImode)
9803 y = gen_rtx_REG (SImode, REGNO (x) + WORDS_BIG_ENDIAN);
9804
9805 if (flag_expensive_optimizations
9806 && df && DF_REG_DEF_COUNT (REGNO (y)) == 1)
9807 set_once = 1;
9808
9809 if (insn == 0)
9810 {
9811 if (set_once)
9812 insn = get_last_insn_anywhere ();
9813 else
9814 return 0;
9815 }
9816
9817 while ((insn = PREV_INSN (insn)))
9818 {
9819 switch (GET_CODE (insn))
9820 {
9821 case JUMP_INSN:
9822 case NOTE:
9823 break;
9824 case CODE_LABEL:
9825 case CALL_INSN:
9826 default:
9827 if (! set_once)
9828 return 0;
9829 break;
9830 case INSN:
9831 {
9832 rtx pat = PATTERN (insn);
9833 if (GET_CODE (pat) != SET)
9834 return 0;
9835 if (rtx_equal_p (x, SET_DEST (pat)))
9836 return set_extends (insn);
9837 if (y && rtx_equal_p (y, SET_DEST (pat)))
9838 return set_extends (insn);
9839 if (reg_overlap_mentioned_p (SET_DEST (pat), y))
9840 return 0;
9841 }
9842 }
9843 }
9844 return 0;
9845 }
9846
9847 /* Output a wide shift instruction in V8+ mode. INSN is the instruction,
9848 OPERANDS are its operands and OPCODE is the mnemonic to be used. */
9849
9850 const char *
9851 output_v8plus_shift (rtx_insn *insn, rtx *operands, const char *opcode)
9852 {
9853 static char asm_code[60];
9854
9855 /* The scratch register is only required when the destination
9856 register is not a 64-bit global or out register. */
9857 if (which_alternative != 2)
9858 operands[3] = operands[0];
9859
9860 /* We can only shift by constants <= 63. */
9861 if (GET_CODE (operands[2]) == CONST_INT)
9862 operands[2] = GEN_INT (INTVAL (operands[2]) & 0x3f);
9863
9864 if (GET_CODE (operands[1]) == CONST_INT)
9865 {
9866 output_asm_insn ("mov\t%1, %3", operands);
9867 }
9868 else
9869 {
9870 output_asm_insn ("sllx\t%H1, 32, %3", operands);
9871 if (sparc_check_64 (operands[1], insn) <= 0)
9872 output_asm_insn ("srl\t%L1, 0, %L1", operands);
9873 output_asm_insn ("or\t%L1, %3, %3", operands);
9874 }
9875
9876 strcpy (asm_code, opcode);
9877
9878 if (which_alternative != 2)
9879 return strcat (asm_code, "\t%0, %2, %L0\n\tsrlx\t%L0, 32, %H0");
9880 else
9881 return
9882 strcat (asm_code, "\t%3, %2, %3\n\tsrlx\t%3, 32, %H0\n\tmov\t%3, %L0");
9883 }
9884 \f
9885 /* Output rtl to increment the profiler label LABELNO
9886 for profiling a function entry. */
9887
9888 void
9889 sparc_profile_hook (int labelno)
9890 {
9891 char buf[32];
9892 rtx lab, fun;
9893
9894 fun = gen_rtx_SYMBOL_REF (Pmode, MCOUNT_FUNCTION);
9895 if (NO_PROFILE_COUNTERS)
9896 {
9897 emit_library_call (fun, LCT_NORMAL, VOIDmode, 0);
9898 }
9899 else
9900 {
9901 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
9902 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
9903 emit_library_call (fun, LCT_NORMAL, VOIDmode, 1, lab, Pmode);
9904 }
9905 }
9906 \f
9907 #ifdef TARGET_SOLARIS
9908 /* Solaris implementation of TARGET_ASM_NAMED_SECTION. */
9909
9910 static void
9911 sparc_solaris_elf_asm_named_section (const char *name, unsigned int flags,
9912 tree decl ATTRIBUTE_UNUSED)
9913 {
9914 if (HAVE_COMDAT_GROUP && flags & SECTION_LINKONCE)
9915 {
9916 solaris_elf_asm_comdat_section (name, flags, decl);
9917 return;
9918 }
9919
9920 fprintf (asm_out_file, "\t.section\t\"%s\"", name);
9921
9922 if (!(flags & SECTION_DEBUG))
9923 fputs (",#alloc", asm_out_file);
9924 if (flags & SECTION_WRITE)
9925 fputs (",#write", asm_out_file);
9926 if (flags & SECTION_TLS)
9927 fputs (",#tls", asm_out_file);
9928 if (flags & SECTION_CODE)
9929 fputs (",#execinstr", asm_out_file);
9930
9931 /* Sun as only supports #nobits/#progbits since Solaris 10. */
9932 if (HAVE_AS_SPARC_NOBITS)
9933 {
9934 if (flags & SECTION_BSS)
9935 fputs (",#nobits", asm_out_file);
9936 else
9937 fputs (",#progbits", asm_out_file);
9938 }
9939
9940 fputc ('\n', asm_out_file);
9941 }
9942 #endif /* TARGET_SOLARIS */
9943
9944 /* We do not allow indirect calls to be optimized into sibling calls.
9945
9946 We cannot use sibling calls when delayed branches are disabled
9947 because they will likely require the call delay slot to be filled.
9948
9949 Also, on SPARC 32-bit we cannot emit a sibling call when the
9950 current function returns a structure. This is because the "unimp
9951 after call" convention would cause the callee to return to the
9952 wrong place. The generic code already disallows cases where the
9953 function being called returns a structure.
9954
9955 It may seem strange how this last case could occur. Usually there
9956 is code after the call which jumps to epilogue code which dumps the
9957 return value into the struct return area. That ought to invalidate
9958 the sibling call right? Well, in the C++ case we can end up passing
9959 the pointer to the struct return area to a constructor (which returns
9960 void) and then nothing else happens. Such a sibling call would look
9961 valid without the added check here.
9962
9963 VxWorks PIC PLT entries require the global pointer to be initialized
9964 on entry. We therefore can't emit sibling calls to them. */
9965 static bool
9966 sparc_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
9967 {
9968 return (decl
9969 && flag_delayed_branch
9970 && (TARGET_ARCH64 || ! cfun->returns_struct)
9971 && !(TARGET_VXWORKS_RTP
9972 && flag_pic
9973 && !targetm.binds_local_p (decl)));
9974 }
9975 \f
9976 /* libfunc renaming. */
9977
9978 static void
9979 sparc_init_libfuncs (void)
9980 {
9981 if (TARGET_ARCH32)
9982 {
9983 /* Use the subroutines that Sun's library provides for integer
9984 multiply and divide. The `*' prevents an underscore from
9985 being prepended by the compiler. .umul is a little faster
9986 than .mul. */
9987 set_optab_libfunc (smul_optab, SImode, "*.umul");
9988 set_optab_libfunc (sdiv_optab, SImode, "*.div");
9989 set_optab_libfunc (udiv_optab, SImode, "*.udiv");
9990 set_optab_libfunc (smod_optab, SImode, "*.rem");
9991 set_optab_libfunc (umod_optab, SImode, "*.urem");
9992
9993 /* TFmode arithmetic. These names are part of the SPARC 32bit ABI. */
9994 set_optab_libfunc (add_optab, TFmode, "_Q_add");
9995 set_optab_libfunc (sub_optab, TFmode, "_Q_sub");
9996 set_optab_libfunc (neg_optab, TFmode, "_Q_neg");
9997 set_optab_libfunc (smul_optab, TFmode, "_Q_mul");
9998 set_optab_libfunc (sdiv_optab, TFmode, "_Q_div");
9999
10000 /* We can define the TFmode sqrt optab only if TARGET_FPU. This
10001 is because with soft-float, the SFmode and DFmode sqrt
10002 instructions will be absent, and the compiler will notice and
10003 try to use the TFmode sqrt instruction for calls to the
10004 builtin function sqrt, but this fails. */
10005 if (TARGET_FPU)
10006 set_optab_libfunc (sqrt_optab, TFmode, "_Q_sqrt");
10007
10008 set_optab_libfunc (eq_optab, TFmode, "_Q_feq");
10009 set_optab_libfunc (ne_optab, TFmode, "_Q_fne");
10010 set_optab_libfunc (gt_optab, TFmode, "_Q_fgt");
10011 set_optab_libfunc (ge_optab, TFmode, "_Q_fge");
10012 set_optab_libfunc (lt_optab, TFmode, "_Q_flt");
10013 set_optab_libfunc (le_optab, TFmode, "_Q_fle");
10014
10015 set_conv_libfunc (sext_optab, TFmode, SFmode, "_Q_stoq");
10016 set_conv_libfunc (sext_optab, TFmode, DFmode, "_Q_dtoq");
10017 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_Q_qtos");
10018 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_Q_qtod");
10019
10020 set_conv_libfunc (sfix_optab, SImode, TFmode, "_Q_qtoi");
10021 set_conv_libfunc (ufix_optab, SImode, TFmode, "_Q_qtou");
10022 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_Q_itoq");
10023 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_Q_utoq");
10024
10025 if (DITF_CONVERSION_LIBFUNCS)
10026 {
10027 set_conv_libfunc (sfix_optab, DImode, TFmode, "_Q_qtoll");
10028 set_conv_libfunc (ufix_optab, DImode, TFmode, "_Q_qtoull");
10029 set_conv_libfunc (sfloat_optab, TFmode, DImode, "_Q_lltoq");
10030 set_conv_libfunc (ufloat_optab, TFmode, DImode, "_Q_ulltoq");
10031 }
10032
10033 if (SUN_CONVERSION_LIBFUNCS)
10034 {
10035 set_conv_libfunc (sfix_optab, DImode, SFmode, "__ftoll");
10036 set_conv_libfunc (ufix_optab, DImode, SFmode, "__ftoull");
10037 set_conv_libfunc (sfix_optab, DImode, DFmode, "__dtoll");
10038 set_conv_libfunc (ufix_optab, DImode, DFmode, "__dtoull");
10039 }
10040 }
10041 if (TARGET_ARCH64)
10042 {
10043 /* In the SPARC 64bit ABI, SImode multiply and divide functions
10044 do not exist in the library. Make sure the compiler does not
10045 emit calls to them by accident. (It should always use the
10046 hardware instructions.) */
10047 set_optab_libfunc (smul_optab, SImode, 0);
10048 set_optab_libfunc (sdiv_optab, SImode, 0);
10049 set_optab_libfunc (udiv_optab, SImode, 0);
10050 set_optab_libfunc (smod_optab, SImode, 0);
10051 set_optab_libfunc (umod_optab, SImode, 0);
10052
10053 if (SUN_INTEGER_MULTIPLY_64)
10054 {
10055 set_optab_libfunc (smul_optab, DImode, "__mul64");
10056 set_optab_libfunc (sdiv_optab, DImode, "__div64");
10057 set_optab_libfunc (udiv_optab, DImode, "__udiv64");
10058 set_optab_libfunc (smod_optab, DImode, "__rem64");
10059 set_optab_libfunc (umod_optab, DImode, "__urem64");
10060 }
10061
10062 if (SUN_CONVERSION_LIBFUNCS)
10063 {
10064 set_conv_libfunc (sfix_optab, DImode, SFmode, "__ftol");
10065 set_conv_libfunc (ufix_optab, DImode, SFmode, "__ftoul");
10066 set_conv_libfunc (sfix_optab, DImode, DFmode, "__dtol");
10067 set_conv_libfunc (ufix_optab, DImode, DFmode, "__dtoul");
10068 }
10069 }
10070 }
10071 \f
10072 /* SPARC builtins. */
10073 enum sparc_builtins
10074 {
10075 /* FPU builtins. */
10076 SPARC_BUILTIN_LDFSR,
10077 SPARC_BUILTIN_STFSR,
10078
10079 /* VIS 1.0 builtins. */
10080 SPARC_BUILTIN_FPACK16,
10081 SPARC_BUILTIN_FPACK32,
10082 SPARC_BUILTIN_FPACKFIX,
10083 SPARC_BUILTIN_FEXPAND,
10084 SPARC_BUILTIN_FPMERGE,
10085 SPARC_BUILTIN_FMUL8X16,
10086 SPARC_BUILTIN_FMUL8X16AU,
10087 SPARC_BUILTIN_FMUL8X16AL,
10088 SPARC_BUILTIN_FMUL8SUX16,
10089 SPARC_BUILTIN_FMUL8ULX16,
10090 SPARC_BUILTIN_FMULD8SUX16,
10091 SPARC_BUILTIN_FMULD8ULX16,
10092 SPARC_BUILTIN_FALIGNDATAV4HI,
10093 SPARC_BUILTIN_FALIGNDATAV8QI,
10094 SPARC_BUILTIN_FALIGNDATAV2SI,
10095 SPARC_BUILTIN_FALIGNDATADI,
10096 SPARC_BUILTIN_WRGSR,
10097 SPARC_BUILTIN_RDGSR,
10098 SPARC_BUILTIN_ALIGNADDR,
10099 SPARC_BUILTIN_ALIGNADDRL,
10100 SPARC_BUILTIN_PDIST,
10101 SPARC_BUILTIN_EDGE8,
10102 SPARC_BUILTIN_EDGE8L,
10103 SPARC_BUILTIN_EDGE16,
10104 SPARC_BUILTIN_EDGE16L,
10105 SPARC_BUILTIN_EDGE32,
10106 SPARC_BUILTIN_EDGE32L,
10107 SPARC_BUILTIN_FCMPLE16,
10108 SPARC_BUILTIN_FCMPLE32,
10109 SPARC_BUILTIN_FCMPNE16,
10110 SPARC_BUILTIN_FCMPNE32,
10111 SPARC_BUILTIN_FCMPGT16,
10112 SPARC_BUILTIN_FCMPGT32,
10113 SPARC_BUILTIN_FCMPEQ16,
10114 SPARC_BUILTIN_FCMPEQ32,
10115 SPARC_BUILTIN_FPADD16,
10116 SPARC_BUILTIN_FPADD16S,
10117 SPARC_BUILTIN_FPADD32,
10118 SPARC_BUILTIN_FPADD32S,
10119 SPARC_BUILTIN_FPSUB16,
10120 SPARC_BUILTIN_FPSUB16S,
10121 SPARC_BUILTIN_FPSUB32,
10122 SPARC_BUILTIN_FPSUB32S,
10123 SPARC_BUILTIN_ARRAY8,
10124 SPARC_BUILTIN_ARRAY16,
10125 SPARC_BUILTIN_ARRAY32,
10126
10127 /* VIS 2.0 builtins. */
10128 SPARC_BUILTIN_EDGE8N,
10129 SPARC_BUILTIN_EDGE8LN,
10130 SPARC_BUILTIN_EDGE16N,
10131 SPARC_BUILTIN_EDGE16LN,
10132 SPARC_BUILTIN_EDGE32N,
10133 SPARC_BUILTIN_EDGE32LN,
10134 SPARC_BUILTIN_BMASK,
10135 SPARC_BUILTIN_BSHUFFLEV4HI,
10136 SPARC_BUILTIN_BSHUFFLEV8QI,
10137 SPARC_BUILTIN_BSHUFFLEV2SI,
10138 SPARC_BUILTIN_BSHUFFLEDI,
10139
10140 /* VIS 3.0 builtins. */
10141 SPARC_BUILTIN_CMASK8,
10142 SPARC_BUILTIN_CMASK16,
10143 SPARC_BUILTIN_CMASK32,
10144 SPARC_BUILTIN_FCHKSM16,
10145 SPARC_BUILTIN_FSLL16,
10146 SPARC_BUILTIN_FSLAS16,
10147 SPARC_BUILTIN_FSRL16,
10148 SPARC_BUILTIN_FSRA16,
10149 SPARC_BUILTIN_FSLL32,
10150 SPARC_BUILTIN_FSLAS32,
10151 SPARC_BUILTIN_FSRL32,
10152 SPARC_BUILTIN_FSRA32,
10153 SPARC_BUILTIN_PDISTN,
10154 SPARC_BUILTIN_FMEAN16,
10155 SPARC_BUILTIN_FPADD64,
10156 SPARC_BUILTIN_FPSUB64,
10157 SPARC_BUILTIN_FPADDS16,
10158 SPARC_BUILTIN_FPADDS16S,
10159 SPARC_BUILTIN_FPSUBS16,
10160 SPARC_BUILTIN_FPSUBS16S,
10161 SPARC_BUILTIN_FPADDS32,
10162 SPARC_BUILTIN_FPADDS32S,
10163 SPARC_BUILTIN_FPSUBS32,
10164 SPARC_BUILTIN_FPSUBS32S,
10165 SPARC_BUILTIN_FUCMPLE8,
10166 SPARC_BUILTIN_FUCMPNE8,
10167 SPARC_BUILTIN_FUCMPGT8,
10168 SPARC_BUILTIN_FUCMPEQ8,
10169 SPARC_BUILTIN_FHADDS,
10170 SPARC_BUILTIN_FHADDD,
10171 SPARC_BUILTIN_FHSUBS,
10172 SPARC_BUILTIN_FHSUBD,
10173 SPARC_BUILTIN_FNHADDS,
10174 SPARC_BUILTIN_FNHADDD,
10175 SPARC_BUILTIN_UMULXHI,
10176 SPARC_BUILTIN_XMULX,
10177 SPARC_BUILTIN_XMULXHI,
10178
10179 SPARC_BUILTIN_MAX
10180 };
10181
10182 static GTY (()) tree sparc_builtins[(int) SPARC_BUILTIN_MAX];
10183 static enum insn_code sparc_builtins_icode[(int) SPARC_BUILTIN_MAX];
10184
10185 /* Add a SPARC builtin function with NAME, ICODE, CODE and TYPE. Return the
10186 function decl or NULL_TREE if the builtin was not added. */
10187
10188 static tree
10189 def_builtin (const char *name, enum insn_code icode, enum sparc_builtins code,
10190 tree type)
10191 {
10192 tree t
10193 = add_builtin_function (name, type, code, BUILT_IN_MD, NULL, NULL_TREE);
10194
10195 if (t)
10196 {
10197 sparc_builtins[code] = t;
10198 sparc_builtins_icode[code] = icode;
10199 }
10200
10201 return t;
10202 }
10203
10204 /* Likewise, but also marks the function as "const". */
10205
10206 static tree
10207 def_builtin_const (const char *name, enum insn_code icode,
10208 enum sparc_builtins code, tree type)
10209 {
10210 tree t = def_builtin (name, icode, code, type);
10211
10212 if (t)
10213 TREE_READONLY (t) = 1;
10214
10215 return t;
10216 }
10217
10218 /* Implement the TARGET_INIT_BUILTINS target hook.
10219 Create builtin functions for special SPARC instructions. */
10220
10221 static void
10222 sparc_init_builtins (void)
10223 {
10224 if (TARGET_FPU)
10225 sparc_fpu_init_builtins ();
10226
10227 if (TARGET_VIS)
10228 sparc_vis_init_builtins ();
10229 }
10230
10231 /* Create builtin functions for FPU instructions. */
10232
10233 static void
10234 sparc_fpu_init_builtins (void)
10235 {
10236 tree ftype
10237 = build_function_type_list (void_type_node,
10238 build_pointer_type (unsigned_type_node), 0);
10239 def_builtin ("__builtin_load_fsr", CODE_FOR_ldfsr,
10240 SPARC_BUILTIN_LDFSR, ftype);
10241 def_builtin ("__builtin_store_fsr", CODE_FOR_stfsr,
10242 SPARC_BUILTIN_STFSR, ftype);
10243 }
10244
10245 /* Create builtin functions for VIS instructions. */
10246
10247 static void
10248 sparc_vis_init_builtins (void)
10249 {
10250 tree v4qi = build_vector_type (unsigned_intQI_type_node, 4);
10251 tree v8qi = build_vector_type (unsigned_intQI_type_node, 8);
10252 tree v4hi = build_vector_type (intHI_type_node, 4);
10253 tree v2hi = build_vector_type (intHI_type_node, 2);
10254 tree v2si = build_vector_type (intSI_type_node, 2);
10255 tree v1si = build_vector_type (intSI_type_node, 1);
10256
10257 tree v4qi_ftype_v4hi = build_function_type_list (v4qi, v4hi, 0);
10258 tree v8qi_ftype_v2si_v8qi = build_function_type_list (v8qi, v2si, v8qi, 0);
10259 tree v2hi_ftype_v2si = build_function_type_list (v2hi, v2si, 0);
10260 tree v4hi_ftype_v4qi = build_function_type_list (v4hi, v4qi, 0);
10261 tree v8qi_ftype_v4qi_v4qi = build_function_type_list (v8qi, v4qi, v4qi, 0);
10262 tree v4hi_ftype_v4qi_v4hi = build_function_type_list (v4hi, v4qi, v4hi, 0);
10263 tree v4hi_ftype_v4qi_v2hi = build_function_type_list (v4hi, v4qi, v2hi, 0);
10264 tree v2si_ftype_v4qi_v2hi = build_function_type_list (v2si, v4qi, v2hi, 0);
10265 tree v4hi_ftype_v8qi_v4hi = build_function_type_list (v4hi, v8qi, v4hi, 0);
10266 tree v4hi_ftype_v4hi_v4hi = build_function_type_list (v4hi, v4hi, v4hi, 0);
10267 tree v2si_ftype_v2si_v2si = build_function_type_list (v2si, v2si, v2si, 0);
10268 tree v8qi_ftype_v8qi_v8qi = build_function_type_list (v8qi, v8qi, v8qi, 0);
10269 tree v2hi_ftype_v2hi_v2hi = build_function_type_list (v2hi, v2hi, v2hi, 0);
10270 tree v1si_ftype_v1si_v1si = build_function_type_list (v1si, v1si, v1si, 0);
10271 tree di_ftype_v8qi_v8qi_di = build_function_type_list (intDI_type_node,
10272 v8qi, v8qi,
10273 intDI_type_node, 0);
10274 tree di_ftype_v8qi_v8qi = build_function_type_list (intDI_type_node,
10275 v8qi, v8qi, 0);
10276 tree si_ftype_v8qi_v8qi = build_function_type_list (intSI_type_node,
10277 v8qi, v8qi, 0);
10278 tree di_ftype_di_di = build_function_type_list (intDI_type_node,
10279 intDI_type_node,
10280 intDI_type_node, 0);
10281 tree si_ftype_si_si = build_function_type_list (intSI_type_node,
10282 intSI_type_node,
10283 intSI_type_node, 0);
10284 tree ptr_ftype_ptr_si = build_function_type_list (ptr_type_node,
10285 ptr_type_node,
10286 intSI_type_node, 0);
10287 tree ptr_ftype_ptr_di = build_function_type_list (ptr_type_node,
10288 ptr_type_node,
10289 intDI_type_node, 0);
10290 tree si_ftype_ptr_ptr = build_function_type_list (intSI_type_node,
10291 ptr_type_node,
10292 ptr_type_node, 0);
10293 tree di_ftype_ptr_ptr = build_function_type_list (intDI_type_node,
10294 ptr_type_node,
10295 ptr_type_node, 0);
10296 tree si_ftype_v4hi_v4hi = build_function_type_list (intSI_type_node,
10297 v4hi, v4hi, 0);
10298 tree si_ftype_v2si_v2si = build_function_type_list (intSI_type_node,
10299 v2si, v2si, 0);
10300 tree di_ftype_v4hi_v4hi = build_function_type_list (intDI_type_node,
10301 v4hi, v4hi, 0);
10302 tree di_ftype_v2si_v2si = build_function_type_list (intDI_type_node,
10303 v2si, v2si, 0);
10304 tree void_ftype_di = build_function_type_list (void_type_node,
10305 intDI_type_node, 0);
10306 tree di_ftype_void = build_function_type_list (intDI_type_node,
10307 void_type_node, 0);
10308 tree void_ftype_si = build_function_type_list (void_type_node,
10309 intSI_type_node, 0);
10310 tree sf_ftype_sf_sf = build_function_type_list (float_type_node,
10311 float_type_node,
10312 float_type_node, 0);
10313 tree df_ftype_df_df = build_function_type_list (double_type_node,
10314 double_type_node,
10315 double_type_node, 0);
10316
10317 /* Packing and expanding vectors. */
10318 def_builtin ("__builtin_vis_fpack16", CODE_FOR_fpack16_vis,
10319 SPARC_BUILTIN_FPACK16, v4qi_ftype_v4hi);
10320 def_builtin ("__builtin_vis_fpack32", CODE_FOR_fpack32_vis,
10321 SPARC_BUILTIN_FPACK32, v8qi_ftype_v2si_v8qi);
10322 def_builtin ("__builtin_vis_fpackfix", CODE_FOR_fpackfix_vis,
10323 SPARC_BUILTIN_FPACKFIX, v2hi_ftype_v2si);
10324 def_builtin_const ("__builtin_vis_fexpand", CODE_FOR_fexpand_vis,
10325 SPARC_BUILTIN_FEXPAND, v4hi_ftype_v4qi);
10326 def_builtin_const ("__builtin_vis_fpmerge", CODE_FOR_fpmerge_vis,
10327 SPARC_BUILTIN_FPMERGE, v8qi_ftype_v4qi_v4qi);
10328
10329 /* Multiplications. */
10330 def_builtin_const ("__builtin_vis_fmul8x16", CODE_FOR_fmul8x16_vis,
10331 SPARC_BUILTIN_FMUL8X16, v4hi_ftype_v4qi_v4hi);
10332 def_builtin_const ("__builtin_vis_fmul8x16au", CODE_FOR_fmul8x16au_vis,
10333 SPARC_BUILTIN_FMUL8X16AU, v4hi_ftype_v4qi_v2hi);
10334 def_builtin_const ("__builtin_vis_fmul8x16al", CODE_FOR_fmul8x16al_vis,
10335 SPARC_BUILTIN_FMUL8X16AL, v4hi_ftype_v4qi_v2hi);
10336 def_builtin_const ("__builtin_vis_fmul8sux16", CODE_FOR_fmul8sux16_vis,
10337 SPARC_BUILTIN_FMUL8SUX16, v4hi_ftype_v8qi_v4hi);
10338 def_builtin_const ("__builtin_vis_fmul8ulx16", CODE_FOR_fmul8ulx16_vis,
10339 SPARC_BUILTIN_FMUL8ULX16, v4hi_ftype_v8qi_v4hi);
10340 def_builtin_const ("__builtin_vis_fmuld8sux16", CODE_FOR_fmuld8sux16_vis,
10341 SPARC_BUILTIN_FMULD8SUX16, v2si_ftype_v4qi_v2hi);
10342 def_builtin_const ("__builtin_vis_fmuld8ulx16", CODE_FOR_fmuld8ulx16_vis,
10343 SPARC_BUILTIN_FMULD8ULX16, v2si_ftype_v4qi_v2hi);
10344
10345 /* Data aligning. */
10346 def_builtin ("__builtin_vis_faligndatav4hi", CODE_FOR_faligndatav4hi_vis,
10347 SPARC_BUILTIN_FALIGNDATAV4HI, v4hi_ftype_v4hi_v4hi);
10348 def_builtin ("__builtin_vis_faligndatav8qi", CODE_FOR_faligndatav8qi_vis,
10349 SPARC_BUILTIN_FALIGNDATAV8QI, v8qi_ftype_v8qi_v8qi);
10350 def_builtin ("__builtin_vis_faligndatav2si", CODE_FOR_faligndatav2si_vis,
10351 SPARC_BUILTIN_FALIGNDATAV2SI, v2si_ftype_v2si_v2si);
10352 def_builtin ("__builtin_vis_faligndatadi", CODE_FOR_faligndatav1di_vis,
10353 SPARC_BUILTIN_FALIGNDATADI, di_ftype_di_di);
10354
10355 def_builtin ("__builtin_vis_write_gsr", CODE_FOR_wrgsr_vis,
10356 SPARC_BUILTIN_WRGSR, void_ftype_di);
10357 def_builtin ("__builtin_vis_read_gsr", CODE_FOR_rdgsr_vis,
10358 SPARC_BUILTIN_RDGSR, di_ftype_void);
10359
10360 if (TARGET_ARCH64)
10361 {
10362 def_builtin ("__builtin_vis_alignaddr", CODE_FOR_alignaddrdi_vis,
10363 SPARC_BUILTIN_ALIGNADDR, ptr_ftype_ptr_di);
10364 def_builtin ("__builtin_vis_alignaddrl", CODE_FOR_alignaddrldi_vis,
10365 SPARC_BUILTIN_ALIGNADDRL, ptr_ftype_ptr_di);
10366 }
10367 else
10368 {
10369 def_builtin ("__builtin_vis_alignaddr", CODE_FOR_alignaddrsi_vis,
10370 SPARC_BUILTIN_ALIGNADDR, ptr_ftype_ptr_si);
10371 def_builtin ("__builtin_vis_alignaddrl", CODE_FOR_alignaddrlsi_vis,
10372 SPARC_BUILTIN_ALIGNADDRL, ptr_ftype_ptr_si);
10373 }
10374
10375 /* Pixel distance. */
10376 def_builtin_const ("__builtin_vis_pdist", CODE_FOR_pdist_vis,
10377 SPARC_BUILTIN_PDIST, di_ftype_v8qi_v8qi_di);
10378
10379 /* Edge handling. */
10380 if (TARGET_ARCH64)
10381 {
10382 def_builtin_const ("__builtin_vis_edge8", CODE_FOR_edge8di_vis,
10383 SPARC_BUILTIN_EDGE8, di_ftype_ptr_ptr);
10384 def_builtin_const ("__builtin_vis_edge8l", CODE_FOR_edge8ldi_vis,
10385 SPARC_BUILTIN_EDGE8L, di_ftype_ptr_ptr);
10386 def_builtin_const ("__builtin_vis_edge16", CODE_FOR_edge16di_vis,
10387 SPARC_BUILTIN_EDGE16, di_ftype_ptr_ptr);
10388 def_builtin_const ("__builtin_vis_edge16l", CODE_FOR_edge16ldi_vis,
10389 SPARC_BUILTIN_EDGE16L, di_ftype_ptr_ptr);
10390 def_builtin_const ("__builtin_vis_edge32", CODE_FOR_edge32di_vis,
10391 SPARC_BUILTIN_EDGE32, di_ftype_ptr_ptr);
10392 def_builtin_const ("__builtin_vis_edge32l", CODE_FOR_edge32ldi_vis,
10393 SPARC_BUILTIN_EDGE32L, di_ftype_ptr_ptr);
10394 }
10395 else
10396 {
10397 def_builtin_const ("__builtin_vis_edge8", CODE_FOR_edge8si_vis,
10398 SPARC_BUILTIN_EDGE8, si_ftype_ptr_ptr);
10399 def_builtin_const ("__builtin_vis_edge8l", CODE_FOR_edge8lsi_vis,
10400 SPARC_BUILTIN_EDGE8L, si_ftype_ptr_ptr);
10401 def_builtin_const ("__builtin_vis_edge16", CODE_FOR_edge16si_vis,
10402 SPARC_BUILTIN_EDGE16, si_ftype_ptr_ptr);
10403 def_builtin_const ("__builtin_vis_edge16l", CODE_FOR_edge16lsi_vis,
10404 SPARC_BUILTIN_EDGE16L, si_ftype_ptr_ptr);
10405 def_builtin_const ("__builtin_vis_edge32", CODE_FOR_edge32si_vis,
10406 SPARC_BUILTIN_EDGE32, si_ftype_ptr_ptr);
10407 def_builtin_const ("__builtin_vis_edge32l", CODE_FOR_edge32lsi_vis,
10408 SPARC_BUILTIN_EDGE32L, si_ftype_ptr_ptr);
10409 }
10410
10411 /* Pixel compare. */
10412 if (TARGET_ARCH64)
10413 {
10414 def_builtin_const ("__builtin_vis_fcmple16", CODE_FOR_fcmple16di_vis,
10415 SPARC_BUILTIN_FCMPLE16, di_ftype_v4hi_v4hi);
10416 def_builtin_const ("__builtin_vis_fcmple32", CODE_FOR_fcmple32di_vis,
10417 SPARC_BUILTIN_FCMPLE32, di_ftype_v2si_v2si);
10418 def_builtin_const ("__builtin_vis_fcmpne16", CODE_FOR_fcmpne16di_vis,
10419 SPARC_BUILTIN_FCMPNE16, di_ftype_v4hi_v4hi);
10420 def_builtin_const ("__builtin_vis_fcmpne32", CODE_FOR_fcmpne32di_vis,
10421 SPARC_BUILTIN_FCMPNE32, di_ftype_v2si_v2si);
10422 def_builtin_const ("__builtin_vis_fcmpgt16", CODE_FOR_fcmpgt16di_vis,
10423 SPARC_BUILTIN_FCMPGT16, di_ftype_v4hi_v4hi);
10424 def_builtin_const ("__builtin_vis_fcmpgt32", CODE_FOR_fcmpgt32di_vis,
10425 SPARC_BUILTIN_FCMPGT32, di_ftype_v2si_v2si);
10426 def_builtin_const ("__builtin_vis_fcmpeq16", CODE_FOR_fcmpeq16di_vis,
10427 SPARC_BUILTIN_FCMPEQ16, di_ftype_v4hi_v4hi);
10428 def_builtin_const ("__builtin_vis_fcmpeq32", CODE_FOR_fcmpeq32di_vis,
10429 SPARC_BUILTIN_FCMPEQ32, di_ftype_v2si_v2si);
10430 }
10431 else
10432 {
10433 def_builtin_const ("__builtin_vis_fcmple16", CODE_FOR_fcmple16si_vis,
10434 SPARC_BUILTIN_FCMPLE16, si_ftype_v4hi_v4hi);
10435 def_builtin_const ("__builtin_vis_fcmple32", CODE_FOR_fcmple32si_vis,
10436 SPARC_BUILTIN_FCMPLE32, si_ftype_v2si_v2si);
10437 def_builtin_const ("__builtin_vis_fcmpne16", CODE_FOR_fcmpne16si_vis,
10438 SPARC_BUILTIN_FCMPNE16, si_ftype_v4hi_v4hi);
10439 def_builtin_const ("__builtin_vis_fcmpne32", CODE_FOR_fcmpne32si_vis,
10440 SPARC_BUILTIN_FCMPNE32, si_ftype_v2si_v2si);
10441 def_builtin_const ("__builtin_vis_fcmpgt16", CODE_FOR_fcmpgt16si_vis,
10442 SPARC_BUILTIN_FCMPGT16, si_ftype_v4hi_v4hi);
10443 def_builtin_const ("__builtin_vis_fcmpgt32", CODE_FOR_fcmpgt32si_vis,
10444 SPARC_BUILTIN_FCMPGT32, si_ftype_v2si_v2si);
10445 def_builtin_const ("__builtin_vis_fcmpeq16", CODE_FOR_fcmpeq16si_vis,
10446 SPARC_BUILTIN_FCMPEQ16, si_ftype_v4hi_v4hi);
10447 def_builtin_const ("__builtin_vis_fcmpeq32", CODE_FOR_fcmpeq32si_vis,
10448 SPARC_BUILTIN_FCMPEQ32, si_ftype_v2si_v2si);
10449 }
10450
10451 /* Addition and subtraction. */
10452 def_builtin_const ("__builtin_vis_fpadd16", CODE_FOR_addv4hi3,
10453 SPARC_BUILTIN_FPADD16, v4hi_ftype_v4hi_v4hi);
10454 def_builtin_const ("__builtin_vis_fpadd16s", CODE_FOR_addv2hi3,
10455 SPARC_BUILTIN_FPADD16S, v2hi_ftype_v2hi_v2hi);
10456 def_builtin_const ("__builtin_vis_fpadd32", CODE_FOR_addv2si3,
10457 SPARC_BUILTIN_FPADD32, v2si_ftype_v2si_v2si);
10458 def_builtin_const ("__builtin_vis_fpadd32s", CODE_FOR_addv1si3,
10459 SPARC_BUILTIN_FPADD32S, v1si_ftype_v1si_v1si);
10460 def_builtin_const ("__builtin_vis_fpsub16", CODE_FOR_subv4hi3,
10461 SPARC_BUILTIN_FPSUB16, v4hi_ftype_v4hi_v4hi);
10462 def_builtin_const ("__builtin_vis_fpsub16s", CODE_FOR_subv2hi3,
10463 SPARC_BUILTIN_FPSUB16S, v2hi_ftype_v2hi_v2hi);
10464 def_builtin_const ("__builtin_vis_fpsub32", CODE_FOR_subv2si3,
10465 SPARC_BUILTIN_FPSUB32, v2si_ftype_v2si_v2si);
10466 def_builtin_const ("__builtin_vis_fpsub32s", CODE_FOR_subv1si3,
10467 SPARC_BUILTIN_FPSUB32S, v1si_ftype_v1si_v1si);
10468
10469 /* Three-dimensional array addressing. */
10470 if (TARGET_ARCH64)
10471 {
10472 def_builtin_const ("__builtin_vis_array8", CODE_FOR_array8di_vis,
10473 SPARC_BUILTIN_ARRAY8, di_ftype_di_di);
10474 def_builtin_const ("__builtin_vis_array16", CODE_FOR_array16di_vis,
10475 SPARC_BUILTIN_ARRAY16, di_ftype_di_di);
10476 def_builtin_const ("__builtin_vis_array32", CODE_FOR_array32di_vis,
10477 SPARC_BUILTIN_ARRAY32, di_ftype_di_di);
10478 }
10479 else
10480 {
10481 def_builtin_const ("__builtin_vis_array8", CODE_FOR_array8si_vis,
10482 SPARC_BUILTIN_ARRAY8, si_ftype_si_si);
10483 def_builtin_const ("__builtin_vis_array16", CODE_FOR_array16si_vis,
10484 SPARC_BUILTIN_ARRAY16, si_ftype_si_si);
10485 def_builtin_const ("__builtin_vis_array32", CODE_FOR_array32si_vis,
10486 SPARC_BUILTIN_ARRAY32, si_ftype_si_si);
10487 }
10488
10489 if (TARGET_VIS2)
10490 {
10491 /* Edge handling. */
10492 if (TARGET_ARCH64)
10493 {
10494 def_builtin_const ("__builtin_vis_edge8n", CODE_FOR_edge8ndi_vis,
10495 SPARC_BUILTIN_EDGE8N, di_ftype_ptr_ptr);
10496 def_builtin_const ("__builtin_vis_edge8ln", CODE_FOR_edge8lndi_vis,
10497 SPARC_BUILTIN_EDGE8LN, di_ftype_ptr_ptr);
10498 def_builtin_const ("__builtin_vis_edge16n", CODE_FOR_edge16ndi_vis,
10499 SPARC_BUILTIN_EDGE16N, di_ftype_ptr_ptr);
10500 def_builtin_const ("__builtin_vis_edge16ln", CODE_FOR_edge16lndi_vis,
10501 SPARC_BUILTIN_EDGE16LN, di_ftype_ptr_ptr);
10502 def_builtin_const ("__builtin_vis_edge32n", CODE_FOR_edge32ndi_vis,
10503 SPARC_BUILTIN_EDGE32N, di_ftype_ptr_ptr);
10504 def_builtin_const ("__builtin_vis_edge32ln", CODE_FOR_edge32lndi_vis,
10505 SPARC_BUILTIN_EDGE32LN, di_ftype_ptr_ptr);
10506 }
10507 else
10508 {
10509 def_builtin_const ("__builtin_vis_edge8n", CODE_FOR_edge8nsi_vis,
10510 SPARC_BUILTIN_EDGE8N, si_ftype_ptr_ptr);
10511 def_builtin_const ("__builtin_vis_edge8ln", CODE_FOR_edge8lnsi_vis,
10512 SPARC_BUILTIN_EDGE8LN, si_ftype_ptr_ptr);
10513 def_builtin_const ("__builtin_vis_edge16n", CODE_FOR_edge16nsi_vis,
10514 SPARC_BUILTIN_EDGE16N, si_ftype_ptr_ptr);
10515 def_builtin_const ("__builtin_vis_edge16ln", CODE_FOR_edge16lnsi_vis,
10516 SPARC_BUILTIN_EDGE16LN, si_ftype_ptr_ptr);
10517 def_builtin_const ("__builtin_vis_edge32n", CODE_FOR_edge32nsi_vis,
10518 SPARC_BUILTIN_EDGE32N, si_ftype_ptr_ptr);
10519 def_builtin_const ("__builtin_vis_edge32ln", CODE_FOR_edge32lnsi_vis,
10520 SPARC_BUILTIN_EDGE32LN, si_ftype_ptr_ptr);
10521 }
10522
10523 /* Byte mask and shuffle. */
10524 if (TARGET_ARCH64)
10525 def_builtin ("__builtin_vis_bmask", CODE_FOR_bmaskdi_vis,
10526 SPARC_BUILTIN_BMASK, di_ftype_di_di);
10527 else
10528 def_builtin ("__builtin_vis_bmask", CODE_FOR_bmasksi_vis,
10529 SPARC_BUILTIN_BMASK, si_ftype_si_si);
10530 def_builtin ("__builtin_vis_bshufflev4hi", CODE_FOR_bshufflev4hi_vis,
10531 SPARC_BUILTIN_BSHUFFLEV4HI, v4hi_ftype_v4hi_v4hi);
10532 def_builtin ("__builtin_vis_bshufflev8qi", CODE_FOR_bshufflev8qi_vis,
10533 SPARC_BUILTIN_BSHUFFLEV8QI, v8qi_ftype_v8qi_v8qi);
10534 def_builtin ("__builtin_vis_bshufflev2si", CODE_FOR_bshufflev2si_vis,
10535 SPARC_BUILTIN_BSHUFFLEV2SI, v2si_ftype_v2si_v2si);
10536 def_builtin ("__builtin_vis_bshuffledi", CODE_FOR_bshufflev1di_vis,
10537 SPARC_BUILTIN_BSHUFFLEDI, di_ftype_di_di);
10538 }
10539
10540 if (TARGET_VIS3)
10541 {
10542 if (TARGET_ARCH64)
10543 {
10544 def_builtin ("__builtin_vis_cmask8", CODE_FOR_cmask8di_vis,
10545 SPARC_BUILTIN_CMASK8, void_ftype_di);
10546 def_builtin ("__builtin_vis_cmask16", CODE_FOR_cmask16di_vis,
10547 SPARC_BUILTIN_CMASK16, void_ftype_di);
10548 def_builtin ("__builtin_vis_cmask32", CODE_FOR_cmask32di_vis,
10549 SPARC_BUILTIN_CMASK32, void_ftype_di);
10550 }
10551 else
10552 {
10553 def_builtin ("__builtin_vis_cmask8", CODE_FOR_cmask8si_vis,
10554 SPARC_BUILTIN_CMASK8, void_ftype_si);
10555 def_builtin ("__builtin_vis_cmask16", CODE_FOR_cmask16si_vis,
10556 SPARC_BUILTIN_CMASK16, void_ftype_si);
10557 def_builtin ("__builtin_vis_cmask32", CODE_FOR_cmask32si_vis,
10558 SPARC_BUILTIN_CMASK32, void_ftype_si);
10559 }
10560
10561 def_builtin_const ("__builtin_vis_fchksm16", CODE_FOR_fchksm16_vis,
10562 SPARC_BUILTIN_FCHKSM16, v4hi_ftype_v4hi_v4hi);
10563
10564 def_builtin_const ("__builtin_vis_fsll16", CODE_FOR_vashlv4hi3,
10565 SPARC_BUILTIN_FSLL16, v4hi_ftype_v4hi_v4hi);
10566 def_builtin_const ("__builtin_vis_fslas16", CODE_FOR_vssashlv4hi3,
10567 SPARC_BUILTIN_FSLAS16, v4hi_ftype_v4hi_v4hi);
10568 def_builtin_const ("__builtin_vis_fsrl16", CODE_FOR_vlshrv4hi3,
10569 SPARC_BUILTIN_FSRL16, v4hi_ftype_v4hi_v4hi);
10570 def_builtin_const ("__builtin_vis_fsra16", CODE_FOR_vashrv4hi3,
10571 SPARC_BUILTIN_FSRA16, v4hi_ftype_v4hi_v4hi);
10572 def_builtin_const ("__builtin_vis_fsll32", CODE_FOR_vashlv2si3,
10573 SPARC_BUILTIN_FSLL32, v2si_ftype_v2si_v2si);
10574 def_builtin_const ("__builtin_vis_fslas32", CODE_FOR_vssashlv2si3,
10575 SPARC_BUILTIN_FSLAS32, v2si_ftype_v2si_v2si);
10576 def_builtin_const ("__builtin_vis_fsrl32", CODE_FOR_vlshrv2si3,
10577 SPARC_BUILTIN_FSRL32, v2si_ftype_v2si_v2si);
10578 def_builtin_const ("__builtin_vis_fsra32", CODE_FOR_vashrv2si3,
10579 SPARC_BUILTIN_FSRA32, v2si_ftype_v2si_v2si);
10580
10581 if (TARGET_ARCH64)
10582 def_builtin_const ("__builtin_vis_pdistn", CODE_FOR_pdistndi_vis,
10583 SPARC_BUILTIN_PDISTN, di_ftype_v8qi_v8qi);
10584 else
10585 def_builtin_const ("__builtin_vis_pdistn", CODE_FOR_pdistnsi_vis,
10586 SPARC_BUILTIN_PDISTN, si_ftype_v8qi_v8qi);
10587
10588 def_builtin_const ("__builtin_vis_fmean16", CODE_FOR_fmean16_vis,
10589 SPARC_BUILTIN_FMEAN16, v4hi_ftype_v4hi_v4hi);
10590 def_builtin_const ("__builtin_vis_fpadd64", CODE_FOR_fpadd64_vis,
10591 SPARC_BUILTIN_FPADD64, di_ftype_di_di);
10592 def_builtin_const ("__builtin_vis_fpsub64", CODE_FOR_fpsub64_vis,
10593 SPARC_BUILTIN_FPSUB64, di_ftype_di_di);
10594
10595 def_builtin_const ("__builtin_vis_fpadds16", CODE_FOR_ssaddv4hi3,
10596 SPARC_BUILTIN_FPADDS16, v4hi_ftype_v4hi_v4hi);
10597 def_builtin_const ("__builtin_vis_fpadds16s", CODE_FOR_ssaddv2hi3,
10598 SPARC_BUILTIN_FPADDS16S, v2hi_ftype_v2hi_v2hi);
10599 def_builtin_const ("__builtin_vis_fpsubs16", CODE_FOR_sssubv4hi3,
10600 SPARC_BUILTIN_FPSUBS16, v4hi_ftype_v4hi_v4hi);
10601 def_builtin_const ("__builtin_vis_fpsubs16s", CODE_FOR_sssubv2hi3,
10602 SPARC_BUILTIN_FPSUBS16S, v2hi_ftype_v2hi_v2hi);
10603 def_builtin_const ("__builtin_vis_fpadds32", CODE_FOR_ssaddv2si3,
10604 SPARC_BUILTIN_FPADDS32, v2si_ftype_v2si_v2si);
10605 def_builtin_const ("__builtin_vis_fpadds32s", CODE_FOR_ssaddv1si3,
10606 SPARC_BUILTIN_FPADDS32S, v1si_ftype_v1si_v1si);
10607 def_builtin_const ("__builtin_vis_fpsubs32", CODE_FOR_sssubv2si3,
10608 SPARC_BUILTIN_FPSUBS32, v2si_ftype_v2si_v2si);
10609 def_builtin_const ("__builtin_vis_fpsubs32s", CODE_FOR_sssubv1si3,
10610 SPARC_BUILTIN_FPSUBS32S, v1si_ftype_v1si_v1si);
10611
10612 if (TARGET_ARCH64)
10613 {
10614 def_builtin_const ("__builtin_vis_fucmple8", CODE_FOR_fucmple8di_vis,
10615 SPARC_BUILTIN_FUCMPLE8, di_ftype_v8qi_v8qi);
10616 def_builtin_const ("__builtin_vis_fucmpne8", CODE_FOR_fucmpne8di_vis,
10617 SPARC_BUILTIN_FUCMPNE8, di_ftype_v8qi_v8qi);
10618 def_builtin_const ("__builtin_vis_fucmpgt8", CODE_FOR_fucmpgt8di_vis,
10619 SPARC_BUILTIN_FUCMPGT8, di_ftype_v8qi_v8qi);
10620 def_builtin_const ("__builtin_vis_fucmpeq8", CODE_FOR_fucmpeq8di_vis,
10621 SPARC_BUILTIN_FUCMPEQ8, di_ftype_v8qi_v8qi);
10622 }
10623 else
10624 {
10625 def_builtin_const ("__builtin_vis_fucmple8", CODE_FOR_fucmple8si_vis,
10626 SPARC_BUILTIN_FUCMPLE8, si_ftype_v8qi_v8qi);
10627 def_builtin_const ("__builtin_vis_fucmpne8", CODE_FOR_fucmpne8si_vis,
10628 SPARC_BUILTIN_FUCMPNE8, si_ftype_v8qi_v8qi);
10629 def_builtin_const ("__builtin_vis_fucmpgt8", CODE_FOR_fucmpgt8si_vis,
10630 SPARC_BUILTIN_FUCMPGT8, si_ftype_v8qi_v8qi);
10631 def_builtin_const ("__builtin_vis_fucmpeq8", CODE_FOR_fucmpeq8si_vis,
10632 SPARC_BUILTIN_FUCMPEQ8, si_ftype_v8qi_v8qi);
10633 }
10634
10635 def_builtin_const ("__builtin_vis_fhadds", CODE_FOR_fhaddsf_vis,
10636 SPARC_BUILTIN_FHADDS, sf_ftype_sf_sf);
10637 def_builtin_const ("__builtin_vis_fhaddd", CODE_FOR_fhadddf_vis,
10638 SPARC_BUILTIN_FHADDD, df_ftype_df_df);
10639 def_builtin_const ("__builtin_vis_fhsubs", CODE_FOR_fhsubsf_vis,
10640 SPARC_BUILTIN_FHSUBS, sf_ftype_sf_sf);
10641 def_builtin_const ("__builtin_vis_fhsubd", CODE_FOR_fhsubdf_vis,
10642 SPARC_BUILTIN_FHSUBD, df_ftype_df_df);
10643 def_builtin_const ("__builtin_vis_fnhadds", CODE_FOR_fnhaddsf_vis,
10644 SPARC_BUILTIN_FNHADDS, sf_ftype_sf_sf);
10645 def_builtin_const ("__builtin_vis_fnhaddd", CODE_FOR_fnhadddf_vis,
10646 SPARC_BUILTIN_FNHADDD, df_ftype_df_df);
10647
10648 def_builtin_const ("__builtin_vis_umulxhi", CODE_FOR_umulxhi_vis,
10649 SPARC_BUILTIN_UMULXHI, di_ftype_di_di);
10650 def_builtin_const ("__builtin_vis_xmulx", CODE_FOR_xmulx_vis,
10651 SPARC_BUILTIN_XMULX, di_ftype_di_di);
10652 def_builtin_const ("__builtin_vis_xmulxhi", CODE_FOR_xmulxhi_vis,
10653 SPARC_BUILTIN_XMULXHI, di_ftype_di_di);
10654 }
10655 }
10656
10657 /* Implement TARGET_BUILTIN_DECL hook. */
10658
10659 static tree
10660 sparc_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
10661 {
10662 if (code >= SPARC_BUILTIN_MAX)
10663 return error_mark_node;
10664
10665 return sparc_builtins[code];
10666 }
10667
10668 /* Implemented TARGET_EXPAND_BUILTIN hook. */
10669
10670 static rtx
10671 sparc_expand_builtin (tree exp, rtx target,
10672 rtx subtarget ATTRIBUTE_UNUSED,
10673 enum machine_mode tmode ATTRIBUTE_UNUSED,
10674 int ignore ATTRIBUTE_UNUSED)
10675 {
10676 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
10677 enum sparc_builtins code = (enum sparc_builtins) DECL_FUNCTION_CODE (fndecl);
10678 enum insn_code icode = sparc_builtins_icode[code];
10679 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
10680 call_expr_arg_iterator iter;
10681 int arg_count = 0;
10682 rtx pat, op[4];
10683 tree arg;
10684
10685 if (nonvoid)
10686 {
10687 enum machine_mode tmode = insn_data[icode].operand[0].mode;
10688 if (!target
10689 || GET_MODE (target) != tmode
10690 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
10691 op[0] = gen_reg_rtx (tmode);
10692 else
10693 op[0] = target;
10694 }
10695
10696 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
10697 {
10698 const struct insn_operand_data *insn_op;
10699 int idx;
10700
10701 if (arg == error_mark_node)
10702 return NULL_RTX;
10703
10704 arg_count++;
10705 idx = arg_count - !nonvoid;
10706 insn_op = &insn_data[icode].operand[idx];
10707 op[arg_count] = expand_normal (arg);
10708
10709 if (code == SPARC_BUILTIN_LDFSR || code == SPARC_BUILTIN_STFSR)
10710 {
10711 if (!address_operand (op[arg_count], SImode))
10712 {
10713 op[arg_count] = convert_memory_address (Pmode, op[arg_count]);
10714 op[arg_count] = copy_addr_to_reg (op[arg_count]);
10715 }
10716 op[arg_count] = gen_rtx_MEM (SImode, op[arg_count]);
10717 }
10718
10719 else if (insn_op->mode == V1DImode
10720 && GET_MODE (op[arg_count]) == DImode)
10721 op[arg_count] = gen_lowpart (V1DImode, op[arg_count]);
10722
10723 else if (insn_op->mode == V1SImode
10724 && GET_MODE (op[arg_count]) == SImode)
10725 op[arg_count] = gen_lowpart (V1SImode, op[arg_count]);
10726
10727 if (! (*insn_data[icode].operand[idx].predicate) (op[arg_count],
10728 insn_op->mode))
10729 op[arg_count] = copy_to_mode_reg (insn_op->mode, op[arg_count]);
10730 }
10731
10732 switch (arg_count)
10733 {
10734 case 0:
10735 pat = GEN_FCN (icode) (op[0]);
10736 break;
10737 case 1:
10738 if (nonvoid)
10739 pat = GEN_FCN (icode) (op[0], op[1]);
10740 else
10741 pat = GEN_FCN (icode) (op[1]);
10742 break;
10743 case 2:
10744 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
10745 break;
10746 case 3:
10747 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
10748 break;
10749 default:
10750 gcc_unreachable ();
10751 }
10752
10753 if (!pat)
10754 return NULL_RTX;
10755
10756 emit_insn (pat);
10757
10758 return (nonvoid ? op[0] : const0_rtx);
10759 }
10760
10761 /* Return the upper 16 bits of the 8x16 multiplication. */
10762
10763 static int
10764 sparc_vis_mul8x16 (int e8, int e16)
10765 {
10766 return (e8 * e16 + 128) / 256;
10767 }
10768
10769 /* Multiply the VECTOR_CSTs CST0 and CST1 as specified by FNCODE and put
10770 the result into the array N_ELTS, whose elements are of INNER_TYPE. */
10771
10772 static void
10773 sparc_handle_vis_mul8x16 (tree *n_elts, enum sparc_builtins fncode,
10774 tree inner_type, tree cst0, tree cst1)
10775 {
10776 unsigned i, num = VECTOR_CST_NELTS (cst0);
10777 int scale;
10778
10779 switch (fncode)
10780 {
10781 case SPARC_BUILTIN_FMUL8X16:
10782 for (i = 0; i < num; ++i)
10783 {
10784 int val
10785 = sparc_vis_mul8x16 (TREE_INT_CST_LOW (VECTOR_CST_ELT (cst0, i)),
10786 TREE_INT_CST_LOW (VECTOR_CST_ELT (cst1, i)));
10787 n_elts[i] = build_int_cst (inner_type, val);
10788 }
10789 break;
10790
10791 case SPARC_BUILTIN_FMUL8X16AU:
10792 scale = TREE_INT_CST_LOW (VECTOR_CST_ELT (cst1, 0));
10793
10794 for (i = 0; i < num; ++i)
10795 {
10796 int val
10797 = sparc_vis_mul8x16 (TREE_INT_CST_LOW (VECTOR_CST_ELT (cst0, i)),
10798 scale);
10799 n_elts[i] = build_int_cst (inner_type, val);
10800 }
10801 break;
10802
10803 case SPARC_BUILTIN_FMUL8X16AL:
10804 scale = TREE_INT_CST_LOW (VECTOR_CST_ELT (cst1, 1));
10805
10806 for (i = 0; i < num; ++i)
10807 {
10808 int val
10809 = sparc_vis_mul8x16 (TREE_INT_CST_LOW (VECTOR_CST_ELT (cst0, i)),
10810 scale);
10811 n_elts[i] = build_int_cst (inner_type, val);
10812 }
10813 break;
10814
10815 default:
10816 gcc_unreachable ();
10817 }
10818 }
10819
10820 /* Implement TARGET_FOLD_BUILTIN hook.
10821
10822 Fold builtin functions for SPARC intrinsics. If IGNORE is true the
10823 result of the function call is ignored. NULL_TREE is returned if the
10824 function could not be folded. */
10825
10826 static tree
10827 sparc_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED,
10828 tree *args, bool ignore)
10829 {
10830 enum sparc_builtins code = (enum sparc_builtins) DECL_FUNCTION_CODE (fndecl);
10831 tree rtype = TREE_TYPE (TREE_TYPE (fndecl));
10832 tree arg0, arg1, arg2;
10833
10834 if (ignore)
10835 switch (code)
10836 {
10837 case SPARC_BUILTIN_LDFSR:
10838 case SPARC_BUILTIN_STFSR:
10839 case SPARC_BUILTIN_ALIGNADDR:
10840 case SPARC_BUILTIN_WRGSR:
10841 case SPARC_BUILTIN_BMASK:
10842 case SPARC_BUILTIN_CMASK8:
10843 case SPARC_BUILTIN_CMASK16:
10844 case SPARC_BUILTIN_CMASK32:
10845 break;
10846
10847 default:
10848 return build_zero_cst (rtype);
10849 }
10850
10851 switch (code)
10852 {
10853 case SPARC_BUILTIN_FEXPAND:
10854 arg0 = args[0];
10855 STRIP_NOPS (arg0);
10856
10857 if (TREE_CODE (arg0) == VECTOR_CST)
10858 {
10859 tree inner_type = TREE_TYPE (rtype);
10860 tree *n_elts;
10861 unsigned i;
10862
10863 n_elts = XALLOCAVEC (tree, VECTOR_CST_NELTS (arg0));
10864 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
10865 n_elts[i] = build_int_cst (inner_type,
10866 TREE_INT_CST_LOW
10867 (VECTOR_CST_ELT (arg0, i)) << 4);
10868 return build_vector (rtype, n_elts);
10869 }
10870 break;
10871
10872 case SPARC_BUILTIN_FMUL8X16:
10873 case SPARC_BUILTIN_FMUL8X16AU:
10874 case SPARC_BUILTIN_FMUL8X16AL:
10875 arg0 = args[0];
10876 arg1 = args[1];
10877 STRIP_NOPS (arg0);
10878 STRIP_NOPS (arg1);
10879
10880 if (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
10881 {
10882 tree inner_type = TREE_TYPE (rtype);
10883 tree *n_elts = XALLOCAVEC (tree, VECTOR_CST_NELTS (arg0));
10884 sparc_handle_vis_mul8x16 (n_elts, code, inner_type, arg0, arg1);
10885 return build_vector (rtype, n_elts);
10886 }
10887 break;
10888
10889 case SPARC_BUILTIN_FPMERGE:
10890 arg0 = args[0];
10891 arg1 = args[1];
10892 STRIP_NOPS (arg0);
10893 STRIP_NOPS (arg1);
10894
10895 if (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
10896 {
10897 tree *n_elts = XALLOCAVEC (tree, 2 * VECTOR_CST_NELTS (arg0));
10898 unsigned i;
10899 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
10900 {
10901 n_elts[2*i] = VECTOR_CST_ELT (arg0, i);
10902 n_elts[2*i+1] = VECTOR_CST_ELT (arg1, i);
10903 }
10904
10905 return build_vector (rtype, n_elts);
10906 }
10907 break;
10908
10909 case SPARC_BUILTIN_PDIST:
10910 case SPARC_BUILTIN_PDISTN:
10911 arg0 = args[0];
10912 arg1 = args[1];
10913 STRIP_NOPS (arg0);
10914 STRIP_NOPS (arg1);
10915 if (code == SPARC_BUILTIN_PDIST)
10916 {
10917 arg2 = args[2];
10918 STRIP_NOPS (arg2);
10919 }
10920 else
10921 arg2 = integer_zero_node;
10922
10923 if (TREE_CODE (arg0) == VECTOR_CST
10924 && TREE_CODE (arg1) == VECTOR_CST
10925 && TREE_CODE (arg2) == INTEGER_CST)
10926 {
10927 bool overflow = false;
10928 widest_int result = wi::to_widest (arg2);
10929 widest_int tmp;
10930 unsigned i;
10931
10932 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
10933 {
10934 tree e0 = VECTOR_CST_ELT (arg0, i);
10935 tree e1 = VECTOR_CST_ELT (arg1, i);
10936
10937 bool neg1_ovf, neg2_ovf, add1_ovf, add2_ovf;
10938
10939 tmp = wi::neg (wi::to_widest (e1), &neg1_ovf);
10940 tmp = wi::add (wi::to_widest (e0), tmp, SIGNED, &add1_ovf);
10941 if (wi::neg_p (tmp))
10942 tmp = wi::neg (tmp, &neg2_ovf);
10943 else
10944 neg2_ovf = false;
10945 result = wi::add (result, tmp, SIGNED, &add2_ovf);
10946 overflow |= neg1_ovf | neg2_ovf | add1_ovf | add2_ovf;
10947 }
10948
10949 gcc_assert (!overflow);
10950
10951 return wide_int_to_tree (rtype, result);
10952 }
10953
10954 default:
10955 break;
10956 }
10957
10958 return NULL_TREE;
10959 }
10960 \f
10961 /* ??? This duplicates information provided to the compiler by the
10962 ??? scheduler description. Some day, teach genautomata to output
10963 ??? the latencies and then CSE will just use that. */
10964
10965 static bool
10966 sparc_rtx_costs (rtx x, int code, int outer_code, int opno ATTRIBUTE_UNUSED,
10967 int *total, bool speed ATTRIBUTE_UNUSED)
10968 {
10969 enum machine_mode mode = GET_MODE (x);
10970 bool float_mode_p = FLOAT_MODE_P (mode);
10971
10972 switch (code)
10973 {
10974 case CONST_INT:
10975 if (INTVAL (x) < 0x1000 && INTVAL (x) >= -0x1000)
10976 {
10977 *total = 0;
10978 return true;
10979 }
10980 /* FALLTHRU */
10981
10982 case HIGH:
10983 *total = 2;
10984 return true;
10985
10986 case CONST:
10987 case LABEL_REF:
10988 case SYMBOL_REF:
10989 *total = 4;
10990 return true;
10991
10992 case CONST_DOUBLE:
10993 if (GET_MODE (x) == VOIDmode
10994 && ((CONST_DOUBLE_HIGH (x) == 0
10995 && CONST_DOUBLE_LOW (x) < 0x1000)
10996 || (CONST_DOUBLE_HIGH (x) == -1
10997 && CONST_DOUBLE_LOW (x) < 0
10998 && CONST_DOUBLE_LOW (x) >= -0x1000)))
10999 *total = 0;
11000 else
11001 *total = 8;
11002 return true;
11003
11004 case MEM:
11005 /* If outer-code was a sign or zero extension, a cost
11006 of COSTS_N_INSNS (1) was already added in. This is
11007 why we are subtracting it back out. */
11008 if (outer_code == ZERO_EXTEND)
11009 {
11010 *total = sparc_costs->int_zload - COSTS_N_INSNS (1);
11011 }
11012 else if (outer_code == SIGN_EXTEND)
11013 {
11014 *total = sparc_costs->int_sload - COSTS_N_INSNS (1);
11015 }
11016 else if (float_mode_p)
11017 {
11018 *total = sparc_costs->float_load;
11019 }
11020 else
11021 {
11022 *total = sparc_costs->int_load;
11023 }
11024
11025 return true;
11026
11027 case PLUS:
11028 case MINUS:
11029 if (float_mode_p)
11030 *total = sparc_costs->float_plusminus;
11031 else
11032 *total = COSTS_N_INSNS (1);
11033 return false;
11034
11035 case FMA:
11036 {
11037 rtx sub;
11038
11039 gcc_assert (float_mode_p);
11040 *total = sparc_costs->float_mul;
11041
11042 sub = XEXP (x, 0);
11043 if (GET_CODE (sub) == NEG)
11044 sub = XEXP (sub, 0);
11045 *total += rtx_cost (sub, FMA, 0, speed);
11046
11047 sub = XEXP (x, 2);
11048 if (GET_CODE (sub) == NEG)
11049 sub = XEXP (sub, 0);
11050 *total += rtx_cost (sub, FMA, 2, speed);
11051 return true;
11052 }
11053
11054 case MULT:
11055 if (float_mode_p)
11056 *total = sparc_costs->float_mul;
11057 else if (! TARGET_HARD_MUL)
11058 *total = COSTS_N_INSNS (25);
11059 else
11060 {
11061 int bit_cost;
11062
11063 bit_cost = 0;
11064 if (sparc_costs->int_mul_bit_factor)
11065 {
11066 int nbits;
11067
11068 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
11069 {
11070 unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
11071 for (nbits = 0; value != 0; value &= value - 1)
11072 nbits++;
11073 }
11074 else if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
11075 && GET_MODE (XEXP (x, 1)) == VOIDmode)
11076 {
11077 rtx x1 = XEXP (x, 1);
11078 unsigned HOST_WIDE_INT value1 = CONST_DOUBLE_LOW (x1);
11079 unsigned HOST_WIDE_INT value2 = CONST_DOUBLE_HIGH (x1);
11080
11081 for (nbits = 0; value1 != 0; value1 &= value1 - 1)
11082 nbits++;
11083 for (; value2 != 0; value2 &= value2 - 1)
11084 nbits++;
11085 }
11086 else
11087 nbits = 7;
11088
11089 if (nbits < 3)
11090 nbits = 3;
11091 bit_cost = (nbits - 3) / sparc_costs->int_mul_bit_factor;
11092 bit_cost = COSTS_N_INSNS (bit_cost);
11093 }
11094
11095 if (mode == DImode)
11096 *total = sparc_costs->int_mulX + bit_cost;
11097 else
11098 *total = sparc_costs->int_mul + bit_cost;
11099 }
11100 return false;
11101
11102 case ASHIFT:
11103 case ASHIFTRT:
11104 case LSHIFTRT:
11105 *total = COSTS_N_INSNS (1) + sparc_costs->shift_penalty;
11106 return false;
11107
11108 case DIV:
11109 case UDIV:
11110 case MOD:
11111 case UMOD:
11112 if (float_mode_p)
11113 {
11114 if (mode == DFmode)
11115 *total = sparc_costs->float_div_df;
11116 else
11117 *total = sparc_costs->float_div_sf;
11118 }
11119 else
11120 {
11121 if (mode == DImode)
11122 *total = sparc_costs->int_divX;
11123 else
11124 *total = sparc_costs->int_div;
11125 }
11126 return false;
11127
11128 case NEG:
11129 if (! float_mode_p)
11130 {
11131 *total = COSTS_N_INSNS (1);
11132 return false;
11133 }
11134 /* FALLTHRU */
11135
11136 case ABS:
11137 case FLOAT:
11138 case UNSIGNED_FLOAT:
11139 case FIX:
11140 case UNSIGNED_FIX:
11141 case FLOAT_EXTEND:
11142 case FLOAT_TRUNCATE:
11143 *total = sparc_costs->float_move;
11144 return false;
11145
11146 case SQRT:
11147 if (mode == DFmode)
11148 *total = sparc_costs->float_sqrt_df;
11149 else
11150 *total = sparc_costs->float_sqrt_sf;
11151 return false;
11152
11153 case COMPARE:
11154 if (float_mode_p)
11155 *total = sparc_costs->float_cmp;
11156 else
11157 *total = COSTS_N_INSNS (1);
11158 return false;
11159
11160 case IF_THEN_ELSE:
11161 if (float_mode_p)
11162 *total = sparc_costs->float_cmove;
11163 else
11164 *total = sparc_costs->int_cmove;
11165 return false;
11166
11167 case IOR:
11168 /* Handle the NAND vector patterns. */
11169 if (sparc_vector_mode_supported_p (GET_MODE (x))
11170 && GET_CODE (XEXP (x, 0)) == NOT
11171 && GET_CODE (XEXP (x, 1)) == NOT)
11172 {
11173 *total = COSTS_N_INSNS (1);
11174 return true;
11175 }
11176 else
11177 return false;
11178
11179 default:
11180 return false;
11181 }
11182 }
11183
11184 /* Return true if CLASS is either GENERAL_REGS or I64_REGS. */
11185
11186 static inline bool
11187 general_or_i64_p (reg_class_t rclass)
11188 {
11189 return (rclass == GENERAL_REGS || rclass == I64_REGS);
11190 }
11191
11192 /* Implement TARGET_REGISTER_MOVE_COST. */
11193
11194 static int
11195 sparc_register_move_cost (enum machine_mode mode ATTRIBUTE_UNUSED,
11196 reg_class_t from, reg_class_t to)
11197 {
11198 bool need_memory = false;
11199
11200 if (from == FPCC_REGS || to == FPCC_REGS)
11201 need_memory = true;
11202 else if ((FP_REG_CLASS_P (from) && general_or_i64_p (to))
11203 || (general_or_i64_p (from) && FP_REG_CLASS_P (to)))
11204 {
11205 if (TARGET_VIS3)
11206 {
11207 int size = GET_MODE_SIZE (mode);
11208 if (size == 8 || size == 4)
11209 {
11210 if (! TARGET_ARCH32 || size == 4)
11211 return 4;
11212 else
11213 return 6;
11214 }
11215 }
11216 need_memory = true;
11217 }
11218
11219 if (need_memory)
11220 {
11221 if (sparc_cpu == PROCESSOR_ULTRASPARC
11222 || sparc_cpu == PROCESSOR_ULTRASPARC3
11223 || sparc_cpu == PROCESSOR_NIAGARA
11224 || sparc_cpu == PROCESSOR_NIAGARA2
11225 || sparc_cpu == PROCESSOR_NIAGARA3
11226 || sparc_cpu == PROCESSOR_NIAGARA4)
11227 return 12;
11228
11229 return 6;
11230 }
11231
11232 return 2;
11233 }
11234
11235 /* Emit the sequence of insns SEQ while preserving the registers REG and REG2.
11236 This is achieved by means of a manual dynamic stack space allocation in
11237 the current frame. We make the assumption that SEQ doesn't contain any
11238 function calls, with the possible exception of calls to the GOT helper. */
11239
11240 static void
11241 emit_and_preserve (rtx seq, rtx reg, rtx reg2)
11242 {
11243 /* We must preserve the lowest 16 words for the register save area. */
11244 HOST_WIDE_INT offset = 16*UNITS_PER_WORD;
11245 /* We really need only 2 words of fresh stack space. */
11246 HOST_WIDE_INT size = SPARC_STACK_ALIGN (offset + 2*UNITS_PER_WORD);
11247
11248 rtx slot
11249 = gen_rtx_MEM (word_mode, plus_constant (Pmode, stack_pointer_rtx,
11250 SPARC_STACK_BIAS + offset));
11251
11252 emit_insn (gen_stack_pointer_inc (GEN_INT (-size)));
11253 emit_insn (gen_rtx_SET (VOIDmode, slot, reg));
11254 if (reg2)
11255 emit_insn (gen_rtx_SET (VOIDmode,
11256 adjust_address (slot, word_mode, UNITS_PER_WORD),
11257 reg2));
11258 emit_insn (seq);
11259 if (reg2)
11260 emit_insn (gen_rtx_SET (VOIDmode,
11261 reg2,
11262 adjust_address (slot, word_mode, UNITS_PER_WORD)));
11263 emit_insn (gen_rtx_SET (VOIDmode, reg, slot));
11264 emit_insn (gen_stack_pointer_inc (GEN_INT (size)));
11265 }
11266
11267 /* Output the assembler code for a thunk function. THUNK_DECL is the
11268 declaration for the thunk function itself, FUNCTION is the decl for
11269 the target function. DELTA is an immediate constant offset to be
11270 added to THIS. If VCALL_OFFSET is nonzero, the word at address
11271 (*THIS + VCALL_OFFSET) should be additionally added to THIS. */
11272
11273 static void
11274 sparc_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
11275 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
11276 tree function)
11277 {
11278 rtx this_rtx, funexp;
11279 rtx_insn *insn;
11280 unsigned int int_arg_first;
11281
11282 reload_completed = 1;
11283 epilogue_completed = 1;
11284
11285 emit_note (NOTE_INSN_PROLOGUE_END);
11286
11287 if (TARGET_FLAT)
11288 {
11289 sparc_leaf_function_p = 1;
11290
11291 int_arg_first = SPARC_OUTGOING_INT_ARG_FIRST;
11292 }
11293 else if (flag_delayed_branch)
11294 {
11295 /* We will emit a regular sibcall below, so we need to instruct
11296 output_sibcall that we are in a leaf function. */
11297 sparc_leaf_function_p = crtl->uses_only_leaf_regs = 1;
11298
11299 /* This will cause final.c to invoke leaf_renumber_regs so we
11300 must behave as if we were in a not-yet-leafified function. */
11301 int_arg_first = SPARC_INCOMING_INT_ARG_FIRST;
11302 }
11303 else
11304 {
11305 /* We will emit the sibcall manually below, so we will need to
11306 manually spill non-leaf registers. */
11307 sparc_leaf_function_p = crtl->uses_only_leaf_regs = 0;
11308
11309 /* We really are in a leaf function. */
11310 int_arg_first = SPARC_OUTGOING_INT_ARG_FIRST;
11311 }
11312
11313 /* Find the "this" pointer. Normally in %o0, but in ARCH64 if the function
11314 returns a structure, the structure return pointer is there instead. */
11315 if (TARGET_ARCH64
11316 && aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
11317 this_rtx = gen_rtx_REG (Pmode, int_arg_first + 1);
11318 else
11319 this_rtx = gen_rtx_REG (Pmode, int_arg_first);
11320
11321 /* Add DELTA. When possible use a plain add, otherwise load it into
11322 a register first. */
11323 if (delta)
11324 {
11325 rtx delta_rtx = GEN_INT (delta);
11326
11327 if (! SPARC_SIMM13_P (delta))
11328 {
11329 rtx scratch = gen_rtx_REG (Pmode, 1);
11330 emit_move_insn (scratch, delta_rtx);
11331 delta_rtx = scratch;
11332 }
11333
11334 /* THIS_RTX += DELTA. */
11335 emit_insn (gen_add2_insn (this_rtx, delta_rtx));
11336 }
11337
11338 /* Add the word at address (*THIS_RTX + VCALL_OFFSET). */
11339 if (vcall_offset)
11340 {
11341 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
11342 rtx scratch = gen_rtx_REG (Pmode, 1);
11343
11344 gcc_assert (vcall_offset < 0);
11345
11346 /* SCRATCH = *THIS_RTX. */
11347 emit_move_insn (scratch, gen_rtx_MEM (Pmode, this_rtx));
11348
11349 /* Prepare for adding VCALL_OFFSET. The difficulty is that we
11350 may not have any available scratch register at this point. */
11351 if (SPARC_SIMM13_P (vcall_offset))
11352 ;
11353 /* This is the case if ARCH64 (unless -ffixed-g5 is passed). */
11354 else if (! fixed_regs[5]
11355 /* The below sequence is made up of at least 2 insns,
11356 while the default method may need only one. */
11357 && vcall_offset < -8192)
11358 {
11359 rtx scratch2 = gen_rtx_REG (Pmode, 5);
11360 emit_move_insn (scratch2, vcall_offset_rtx);
11361 vcall_offset_rtx = scratch2;
11362 }
11363 else
11364 {
11365 rtx increment = GEN_INT (-4096);
11366
11367 /* VCALL_OFFSET is a negative number whose typical range can be
11368 estimated as -32768..0 in 32-bit mode. In almost all cases
11369 it is therefore cheaper to emit multiple add insns than
11370 spilling and loading the constant into a register (at least
11371 6 insns). */
11372 while (! SPARC_SIMM13_P (vcall_offset))
11373 {
11374 emit_insn (gen_add2_insn (scratch, increment));
11375 vcall_offset += 4096;
11376 }
11377 vcall_offset_rtx = GEN_INT (vcall_offset); /* cannot be 0 */
11378 }
11379
11380 /* SCRATCH = *(*THIS_RTX + VCALL_OFFSET). */
11381 emit_move_insn (scratch, gen_rtx_MEM (Pmode,
11382 gen_rtx_PLUS (Pmode,
11383 scratch,
11384 vcall_offset_rtx)));
11385
11386 /* THIS_RTX += *(*THIS_RTX + VCALL_OFFSET). */
11387 emit_insn (gen_add2_insn (this_rtx, scratch));
11388 }
11389
11390 /* Generate a tail call to the target function. */
11391 if (! TREE_USED (function))
11392 {
11393 assemble_external (function);
11394 TREE_USED (function) = 1;
11395 }
11396 funexp = XEXP (DECL_RTL (function), 0);
11397
11398 if (flag_delayed_branch)
11399 {
11400 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
11401 insn = emit_call_insn (gen_sibcall (funexp));
11402 SIBLING_CALL_P (insn) = 1;
11403 }
11404 else
11405 {
11406 /* The hoops we have to jump through in order to generate a sibcall
11407 without using delay slots... */
11408 rtx spill_reg, seq, scratch = gen_rtx_REG (Pmode, 1);
11409
11410 if (flag_pic)
11411 {
11412 spill_reg = gen_rtx_REG (word_mode, 15); /* %o7 */
11413 start_sequence ();
11414 load_got_register (); /* clobbers %o7 */
11415 scratch = sparc_legitimize_pic_address (funexp, scratch);
11416 seq = get_insns ();
11417 end_sequence ();
11418 emit_and_preserve (seq, spill_reg, pic_offset_table_rtx);
11419 }
11420 else if (TARGET_ARCH32)
11421 {
11422 emit_insn (gen_rtx_SET (VOIDmode,
11423 scratch,
11424 gen_rtx_HIGH (SImode, funexp)));
11425 emit_insn (gen_rtx_SET (VOIDmode,
11426 scratch,
11427 gen_rtx_LO_SUM (SImode, scratch, funexp)));
11428 }
11429 else /* TARGET_ARCH64 */
11430 {
11431 switch (sparc_cmodel)
11432 {
11433 case CM_MEDLOW:
11434 case CM_MEDMID:
11435 /* The destination can serve as a temporary. */
11436 sparc_emit_set_symbolic_const64 (scratch, funexp, scratch);
11437 break;
11438
11439 case CM_MEDANY:
11440 case CM_EMBMEDANY:
11441 /* The destination cannot serve as a temporary. */
11442 spill_reg = gen_rtx_REG (DImode, 15); /* %o7 */
11443 start_sequence ();
11444 sparc_emit_set_symbolic_const64 (scratch, funexp, spill_reg);
11445 seq = get_insns ();
11446 end_sequence ();
11447 emit_and_preserve (seq, spill_reg, 0);
11448 break;
11449
11450 default:
11451 gcc_unreachable ();
11452 }
11453 }
11454
11455 emit_jump_insn (gen_indirect_jump (scratch));
11456 }
11457
11458 emit_barrier ();
11459
11460 /* Run just enough of rest_of_compilation to get the insns emitted.
11461 There's not really enough bulk here to make other passes such as
11462 instruction scheduling worth while. Note that use_thunk calls
11463 assemble_start_function and assemble_end_function. */
11464 insn = get_insns ();
11465 shorten_branches (insn);
11466 final_start_function (insn, file, 1);
11467 final (insn, file, 1);
11468 final_end_function ();
11469
11470 reload_completed = 0;
11471 epilogue_completed = 0;
11472 }
11473
11474 /* Return true if sparc_output_mi_thunk would be able to output the
11475 assembler code for the thunk function specified by the arguments
11476 it is passed, and false otherwise. */
11477 static bool
11478 sparc_can_output_mi_thunk (const_tree thunk_fndecl ATTRIBUTE_UNUSED,
11479 HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
11480 HOST_WIDE_INT vcall_offset,
11481 const_tree function ATTRIBUTE_UNUSED)
11482 {
11483 /* Bound the loop used in the default method above. */
11484 return (vcall_offset >= -32768 || ! fixed_regs[5]);
11485 }
11486
11487 /* How to allocate a 'struct machine_function'. */
11488
11489 static struct machine_function *
11490 sparc_init_machine_status (void)
11491 {
11492 return ggc_cleared_alloc<machine_function> ();
11493 }
11494
11495 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
11496 We need to emit DTP-relative relocations. */
11497
11498 static void
11499 sparc_output_dwarf_dtprel (FILE *file, int size, rtx x)
11500 {
11501 switch (size)
11502 {
11503 case 4:
11504 fputs ("\t.word\t%r_tls_dtpoff32(", file);
11505 break;
11506 case 8:
11507 fputs ("\t.xword\t%r_tls_dtpoff64(", file);
11508 break;
11509 default:
11510 gcc_unreachable ();
11511 }
11512 output_addr_const (file, x);
11513 fputs (")", file);
11514 }
11515
11516 /* Do whatever processing is required at the end of a file. */
11517
11518 static void
11519 sparc_file_end (void)
11520 {
11521 /* If we need to emit the special GOT helper function, do so now. */
11522 if (got_helper_rtx)
11523 {
11524 const char *name = XSTR (got_helper_rtx, 0);
11525 const char *reg_name = reg_names[GLOBAL_OFFSET_TABLE_REGNUM];
11526 #ifdef DWARF2_UNWIND_INFO
11527 bool do_cfi;
11528 #endif
11529
11530 if (USE_HIDDEN_LINKONCE)
11531 {
11532 tree decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
11533 get_identifier (name),
11534 build_function_type_list (void_type_node,
11535 NULL_TREE));
11536 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
11537 NULL_TREE, void_type_node);
11538 TREE_PUBLIC (decl) = 1;
11539 TREE_STATIC (decl) = 1;
11540 make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
11541 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
11542 DECL_VISIBILITY_SPECIFIED (decl) = 1;
11543 resolve_unique_section (decl, 0, flag_function_sections);
11544 allocate_struct_function (decl, true);
11545 cfun->is_thunk = 1;
11546 current_function_decl = decl;
11547 init_varasm_status ();
11548 assemble_start_function (decl, name);
11549 }
11550 else
11551 {
11552 const int align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
11553 switch_to_section (text_section);
11554 if (align > 0)
11555 ASM_OUTPUT_ALIGN (asm_out_file, align);
11556 ASM_OUTPUT_LABEL (asm_out_file, name);
11557 }
11558
11559 #ifdef DWARF2_UNWIND_INFO
11560 do_cfi = dwarf2out_do_cfi_asm ();
11561 if (do_cfi)
11562 fprintf (asm_out_file, "\t.cfi_startproc\n");
11563 #endif
11564 if (flag_delayed_branch)
11565 fprintf (asm_out_file, "\tjmp\t%%o7+8\n\t add\t%%o7, %s, %s\n",
11566 reg_name, reg_name);
11567 else
11568 fprintf (asm_out_file, "\tadd\t%%o7, %s, %s\n\tjmp\t%%o7+8\n\t nop\n",
11569 reg_name, reg_name);
11570 #ifdef DWARF2_UNWIND_INFO
11571 if (do_cfi)
11572 fprintf (asm_out_file, "\t.cfi_endproc\n");
11573 #endif
11574 }
11575
11576 if (NEED_INDICATE_EXEC_STACK)
11577 file_end_indicate_exec_stack ();
11578
11579 #ifdef TARGET_SOLARIS
11580 solaris_file_end ();
11581 #endif
11582 }
11583
11584 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
11585 /* Implement TARGET_MANGLE_TYPE. */
11586
11587 static const char *
11588 sparc_mangle_type (const_tree type)
11589 {
11590 if (!TARGET_64BIT
11591 && TYPE_MAIN_VARIANT (type) == long_double_type_node
11592 && TARGET_LONG_DOUBLE_128)
11593 return "g";
11594
11595 /* For all other types, use normal C++ mangling. */
11596 return NULL;
11597 }
11598 #endif
11599
11600 /* Expand a membar instruction for various use cases. Both the LOAD_STORE
11601 and BEFORE_AFTER arguments of the form X_Y. They are two-bit masks where
11602 bit 0 indicates that X is true, and bit 1 indicates Y is true. */
11603
11604 void
11605 sparc_emit_membar_for_model (enum memmodel model,
11606 int load_store, int before_after)
11607 {
11608 /* Bits for the MEMBAR mmask field. */
11609 const int LoadLoad = 1;
11610 const int StoreLoad = 2;
11611 const int LoadStore = 4;
11612 const int StoreStore = 8;
11613
11614 int mm = 0, implied = 0;
11615
11616 switch (sparc_memory_model)
11617 {
11618 case SMM_SC:
11619 /* Sequential Consistency. All memory transactions are immediately
11620 visible in sequential execution order. No barriers needed. */
11621 implied = LoadLoad | StoreLoad | LoadStore | StoreStore;
11622 break;
11623
11624 case SMM_TSO:
11625 /* Total Store Ordering: all memory transactions with store semantics
11626 are followed by an implied StoreStore. */
11627 implied |= StoreStore;
11628
11629 /* If we're not looking for a raw barrer (before+after), then atomic
11630 operations get the benefit of being both load and store. */
11631 if (load_store == 3 && before_after == 1)
11632 implied |= StoreLoad;
11633 /* FALLTHRU */
11634
11635 case SMM_PSO:
11636 /* Partial Store Ordering: all memory transactions with load semantics
11637 are followed by an implied LoadLoad | LoadStore. */
11638 implied |= LoadLoad | LoadStore;
11639
11640 /* If we're not looking for a raw barrer (before+after), then atomic
11641 operations get the benefit of being both load and store. */
11642 if (load_store == 3 && before_after == 2)
11643 implied |= StoreLoad | StoreStore;
11644 /* FALLTHRU */
11645
11646 case SMM_RMO:
11647 /* Relaxed Memory Ordering: no implicit bits. */
11648 break;
11649
11650 default:
11651 gcc_unreachable ();
11652 }
11653
11654 if (before_after & 1)
11655 {
11656 if (model == MEMMODEL_RELEASE
11657 || model == MEMMODEL_ACQ_REL
11658 || model == MEMMODEL_SEQ_CST)
11659 {
11660 if (load_store & 1)
11661 mm |= LoadLoad | StoreLoad;
11662 if (load_store & 2)
11663 mm |= LoadStore | StoreStore;
11664 }
11665 }
11666 if (before_after & 2)
11667 {
11668 if (model == MEMMODEL_ACQUIRE
11669 || model == MEMMODEL_ACQ_REL
11670 || model == MEMMODEL_SEQ_CST)
11671 {
11672 if (load_store & 1)
11673 mm |= LoadLoad | LoadStore;
11674 if (load_store & 2)
11675 mm |= StoreLoad | StoreStore;
11676 }
11677 }
11678
11679 /* Remove the bits implied by the system memory model. */
11680 mm &= ~implied;
11681
11682 /* For raw barriers (before+after), always emit a barrier.
11683 This will become a compile-time barrier if needed. */
11684 if (mm || before_after == 3)
11685 emit_insn (gen_membar (GEN_INT (mm)));
11686 }
11687
11688 /* Expand code to perform a 8 or 16-bit compare and swap by doing 32-bit
11689 compare and swap on the word containing the byte or half-word. */
11690
11691 static void
11692 sparc_expand_compare_and_swap_12 (rtx bool_result, rtx result, rtx mem,
11693 rtx oldval, rtx newval)
11694 {
11695 rtx addr1 = force_reg (Pmode, XEXP (mem, 0));
11696 rtx addr = gen_reg_rtx (Pmode);
11697 rtx off = gen_reg_rtx (SImode);
11698 rtx oldv = gen_reg_rtx (SImode);
11699 rtx newv = gen_reg_rtx (SImode);
11700 rtx oldvalue = gen_reg_rtx (SImode);
11701 rtx newvalue = gen_reg_rtx (SImode);
11702 rtx res = gen_reg_rtx (SImode);
11703 rtx resv = gen_reg_rtx (SImode);
11704 rtx memsi, val, mask, cc;
11705
11706 emit_insn (gen_rtx_SET (VOIDmode, addr,
11707 gen_rtx_AND (Pmode, addr1, GEN_INT (-4))));
11708
11709 if (Pmode != SImode)
11710 addr1 = gen_lowpart (SImode, addr1);
11711 emit_insn (gen_rtx_SET (VOIDmode, off,
11712 gen_rtx_AND (SImode, addr1, GEN_INT (3))));
11713
11714 memsi = gen_rtx_MEM (SImode, addr);
11715 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
11716 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
11717
11718 val = copy_to_reg (memsi);
11719
11720 emit_insn (gen_rtx_SET (VOIDmode, off,
11721 gen_rtx_XOR (SImode, off,
11722 GEN_INT (GET_MODE (mem) == QImode
11723 ? 3 : 2))));
11724
11725 emit_insn (gen_rtx_SET (VOIDmode, off,
11726 gen_rtx_ASHIFT (SImode, off, GEN_INT (3))));
11727
11728 if (GET_MODE (mem) == QImode)
11729 mask = force_reg (SImode, GEN_INT (0xff));
11730 else
11731 mask = force_reg (SImode, GEN_INT (0xffff));
11732
11733 emit_insn (gen_rtx_SET (VOIDmode, mask,
11734 gen_rtx_ASHIFT (SImode, mask, off)));
11735
11736 emit_insn (gen_rtx_SET (VOIDmode, val,
11737 gen_rtx_AND (SImode, gen_rtx_NOT (SImode, mask),
11738 val)));
11739
11740 oldval = gen_lowpart (SImode, oldval);
11741 emit_insn (gen_rtx_SET (VOIDmode, oldv,
11742 gen_rtx_ASHIFT (SImode, oldval, off)));
11743
11744 newval = gen_lowpart_common (SImode, newval);
11745 emit_insn (gen_rtx_SET (VOIDmode, newv,
11746 gen_rtx_ASHIFT (SImode, newval, off)));
11747
11748 emit_insn (gen_rtx_SET (VOIDmode, oldv,
11749 gen_rtx_AND (SImode, oldv, mask)));
11750
11751 emit_insn (gen_rtx_SET (VOIDmode, newv,
11752 gen_rtx_AND (SImode, newv, mask)));
11753
11754 rtx_code_label *end_label = gen_label_rtx ();
11755 rtx_code_label *loop_label = gen_label_rtx ();
11756 emit_label (loop_label);
11757
11758 emit_insn (gen_rtx_SET (VOIDmode, oldvalue,
11759 gen_rtx_IOR (SImode, oldv, val)));
11760
11761 emit_insn (gen_rtx_SET (VOIDmode, newvalue,
11762 gen_rtx_IOR (SImode, newv, val)));
11763
11764 emit_move_insn (bool_result, const1_rtx);
11765
11766 emit_insn (gen_atomic_compare_and_swapsi_1 (res, memsi, oldvalue, newvalue));
11767
11768 emit_cmp_and_jump_insns (res, oldvalue, EQ, NULL, SImode, 0, end_label);
11769
11770 emit_insn (gen_rtx_SET (VOIDmode, resv,
11771 gen_rtx_AND (SImode, gen_rtx_NOT (SImode, mask),
11772 res)));
11773
11774 emit_move_insn (bool_result, const0_rtx);
11775
11776 cc = gen_compare_reg_1 (NE, resv, val);
11777 emit_insn (gen_rtx_SET (VOIDmode, val, resv));
11778
11779 /* Use cbranchcc4 to separate the compare and branch! */
11780 emit_jump_insn (gen_cbranchcc4 (gen_rtx_NE (VOIDmode, cc, const0_rtx),
11781 cc, const0_rtx, loop_label));
11782
11783 emit_label (end_label);
11784
11785 emit_insn (gen_rtx_SET (VOIDmode, res,
11786 gen_rtx_AND (SImode, res, mask)));
11787
11788 emit_insn (gen_rtx_SET (VOIDmode, res,
11789 gen_rtx_LSHIFTRT (SImode, res, off)));
11790
11791 emit_move_insn (result, gen_lowpart (GET_MODE (result), res));
11792 }
11793
11794 /* Expand code to perform a compare-and-swap. */
11795
11796 void
11797 sparc_expand_compare_and_swap (rtx operands[])
11798 {
11799 rtx bval, retval, mem, oldval, newval;
11800 enum machine_mode mode;
11801 enum memmodel model;
11802
11803 bval = operands[0];
11804 retval = operands[1];
11805 mem = operands[2];
11806 oldval = operands[3];
11807 newval = operands[4];
11808 model = (enum memmodel) INTVAL (operands[6]);
11809 mode = GET_MODE (mem);
11810
11811 sparc_emit_membar_for_model (model, 3, 1);
11812
11813 if (reg_overlap_mentioned_p (retval, oldval))
11814 oldval = copy_to_reg (oldval);
11815
11816 if (mode == QImode || mode == HImode)
11817 sparc_expand_compare_and_swap_12 (bval, retval, mem, oldval, newval);
11818 else
11819 {
11820 rtx (*gen) (rtx, rtx, rtx, rtx);
11821 rtx x;
11822
11823 if (mode == SImode)
11824 gen = gen_atomic_compare_and_swapsi_1;
11825 else
11826 gen = gen_atomic_compare_and_swapdi_1;
11827 emit_insn (gen (retval, mem, oldval, newval));
11828
11829 x = emit_store_flag (bval, EQ, retval, oldval, mode, 1, 1);
11830 if (x != bval)
11831 convert_move (bval, x, 1);
11832 }
11833
11834 sparc_emit_membar_for_model (model, 3, 2);
11835 }
11836
11837 void
11838 sparc_expand_vec_perm_bmask (enum machine_mode vmode, rtx sel)
11839 {
11840 rtx t_1, t_2, t_3;
11841
11842 sel = gen_lowpart (DImode, sel);
11843 switch (vmode)
11844 {
11845 case V2SImode:
11846 /* inp = xxxxxxxAxxxxxxxB */
11847 t_1 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (16),
11848 NULL_RTX, 1, OPTAB_DIRECT);
11849 /* t_1 = ....xxxxxxxAxxx. */
11850 sel = expand_simple_binop (SImode, AND, gen_lowpart (SImode, sel),
11851 GEN_INT (3), NULL_RTX, 1, OPTAB_DIRECT);
11852 t_1 = expand_simple_binop (SImode, AND, gen_lowpart (SImode, t_1),
11853 GEN_INT (0x30000), NULL_RTX, 1, OPTAB_DIRECT);
11854 /* sel = .......B */
11855 /* t_1 = ...A.... */
11856 sel = expand_simple_binop (SImode, IOR, sel, t_1, sel, 1, OPTAB_DIRECT);
11857 /* sel = ...A...B */
11858 sel = expand_mult (SImode, sel, GEN_INT (0x4444), sel, 1);
11859 /* sel = AAAABBBB * 4 */
11860 t_1 = force_reg (SImode, GEN_INT (0x01230123));
11861 /* sel = { A*4, A*4+1, A*4+2, ... } */
11862 break;
11863
11864 case V4HImode:
11865 /* inp = xxxAxxxBxxxCxxxD */
11866 t_1 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (8),
11867 NULL_RTX, 1, OPTAB_DIRECT);
11868 t_2 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (16),
11869 NULL_RTX, 1, OPTAB_DIRECT);
11870 t_3 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (24),
11871 NULL_RTX, 1, OPTAB_DIRECT);
11872 /* t_1 = ..xxxAxxxBxxxCxx */
11873 /* t_2 = ....xxxAxxxBxxxC */
11874 /* t_3 = ......xxxAxxxBxx */
11875 sel = expand_simple_binop (SImode, AND, gen_lowpart (SImode, sel),
11876 GEN_INT (0x07),
11877 NULL_RTX, 1, OPTAB_DIRECT);
11878 t_1 = expand_simple_binop (SImode, AND, gen_lowpart (SImode, t_1),
11879 GEN_INT (0x0700),
11880 NULL_RTX, 1, OPTAB_DIRECT);
11881 t_2 = expand_simple_binop (SImode, AND, gen_lowpart (SImode, t_2),
11882 GEN_INT (0x070000),
11883 NULL_RTX, 1, OPTAB_DIRECT);
11884 t_3 = expand_simple_binop (SImode, AND, gen_lowpart (SImode, t_3),
11885 GEN_INT (0x07000000),
11886 NULL_RTX, 1, OPTAB_DIRECT);
11887 /* sel = .......D */
11888 /* t_1 = .....C.. */
11889 /* t_2 = ...B.... */
11890 /* t_3 = .A...... */
11891 sel = expand_simple_binop (SImode, IOR, sel, t_1, sel, 1, OPTAB_DIRECT);
11892 t_2 = expand_simple_binop (SImode, IOR, t_2, t_3, t_2, 1, OPTAB_DIRECT);
11893 sel = expand_simple_binop (SImode, IOR, sel, t_2, sel, 1, OPTAB_DIRECT);
11894 /* sel = .A.B.C.D */
11895 sel = expand_mult (SImode, sel, GEN_INT (0x22), sel, 1);
11896 /* sel = AABBCCDD * 2 */
11897 t_1 = force_reg (SImode, GEN_INT (0x01010101));
11898 /* sel = { A*2, A*2+1, B*2, B*2+1, ... } */
11899 break;
11900
11901 case V8QImode:
11902 /* input = xAxBxCxDxExFxGxH */
11903 sel = expand_simple_binop (DImode, AND, sel,
11904 GEN_INT ((HOST_WIDE_INT)0x0f0f0f0f << 32
11905 | 0x0f0f0f0f),
11906 NULL_RTX, 1, OPTAB_DIRECT);
11907 /* sel = .A.B.C.D.E.F.G.H */
11908 t_1 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (4),
11909 NULL_RTX, 1, OPTAB_DIRECT);
11910 /* t_1 = ..A.B.C.D.E.F.G. */
11911 sel = expand_simple_binop (DImode, IOR, sel, t_1,
11912 NULL_RTX, 1, OPTAB_DIRECT);
11913 /* sel = .AABBCCDDEEFFGGH */
11914 sel = expand_simple_binop (DImode, AND, sel,
11915 GEN_INT ((HOST_WIDE_INT)0xff00ff << 32
11916 | 0xff00ff),
11917 NULL_RTX, 1, OPTAB_DIRECT);
11918 /* sel = ..AB..CD..EF..GH */
11919 t_1 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (8),
11920 NULL_RTX, 1, OPTAB_DIRECT);
11921 /* t_1 = ....AB..CD..EF.. */
11922 sel = expand_simple_binop (DImode, IOR, sel, t_1,
11923 NULL_RTX, 1, OPTAB_DIRECT);
11924 /* sel = ..ABABCDCDEFEFGH */
11925 sel = expand_simple_binop (DImode, AND, sel,
11926 GEN_INT ((HOST_WIDE_INT)0xffff << 32 | 0xffff),
11927 NULL_RTX, 1, OPTAB_DIRECT);
11928 /* sel = ....ABCD....EFGH */
11929 t_1 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (16),
11930 NULL_RTX, 1, OPTAB_DIRECT);
11931 /* t_1 = ........ABCD.... */
11932 sel = gen_lowpart (SImode, sel);
11933 t_1 = gen_lowpart (SImode, t_1);
11934 break;
11935
11936 default:
11937 gcc_unreachable ();
11938 }
11939
11940 /* Always perform the final addition/merge within the bmask insn. */
11941 emit_insn (gen_bmasksi_vis (gen_rtx_REG (SImode, 0), sel, t_1));
11942 }
11943
11944 /* Implement TARGET_FRAME_POINTER_REQUIRED. */
11945
11946 static bool
11947 sparc_frame_pointer_required (void)
11948 {
11949 /* If the stack pointer is dynamically modified in the function, it cannot
11950 serve as the frame pointer. */
11951 if (cfun->calls_alloca)
11952 return true;
11953
11954 /* If the function receives nonlocal gotos, it needs to save the frame
11955 pointer in the nonlocal_goto_save_area object. */
11956 if (cfun->has_nonlocal_label)
11957 return true;
11958
11959 /* In flat mode, that's it. */
11960 if (TARGET_FLAT)
11961 return false;
11962
11963 /* Otherwise, the frame pointer is required if the function isn't leaf. */
11964 return !(crtl->is_leaf && only_leaf_regs_used ());
11965 }
11966
11967 /* The way this is structured, we can't eliminate SFP in favor of SP
11968 if the frame pointer is required: we want to use the SFP->HFP elimination
11969 in that case. But the test in update_eliminables doesn't know we are
11970 assuming below that we only do the former elimination. */
11971
11972 static bool
11973 sparc_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
11974 {
11975 return to == HARD_FRAME_POINTER_REGNUM || !sparc_frame_pointer_required ();
11976 }
11977
11978 /* Return the hard frame pointer directly to bypass the stack bias. */
11979
11980 static rtx
11981 sparc_builtin_setjmp_frame_value (void)
11982 {
11983 return hard_frame_pointer_rtx;
11984 }
11985
11986 /* If !TARGET_FPU, then make the fp registers and fp cc regs fixed so that
11987 they won't be allocated. */
11988
11989 static void
11990 sparc_conditional_register_usage (void)
11991 {
11992 if (PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
11993 {
11994 fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
11995 call_used_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
11996 }
11997 /* If the user has passed -f{fixed,call-{used,saved}}-g5 */
11998 /* then honor it. */
11999 if (TARGET_ARCH32 && fixed_regs[5])
12000 fixed_regs[5] = 1;
12001 else if (TARGET_ARCH64 && fixed_regs[5] == 2)
12002 fixed_regs[5] = 0;
12003 if (! TARGET_V9)
12004 {
12005 int regno;
12006 for (regno = SPARC_FIRST_V9_FP_REG;
12007 regno <= SPARC_LAST_V9_FP_REG;
12008 regno++)
12009 fixed_regs[regno] = 1;
12010 /* %fcc0 is used by v8 and v9. */
12011 for (regno = SPARC_FIRST_V9_FCC_REG + 1;
12012 regno <= SPARC_LAST_V9_FCC_REG;
12013 regno++)
12014 fixed_regs[regno] = 1;
12015 }
12016 if (! TARGET_FPU)
12017 {
12018 int regno;
12019 for (regno = 32; regno < SPARC_LAST_V9_FCC_REG; regno++)
12020 fixed_regs[regno] = 1;
12021 }
12022 /* If the user has passed -f{fixed,call-{used,saved}}-g2 */
12023 /* then honor it. Likewise with g3 and g4. */
12024 if (fixed_regs[2] == 2)
12025 fixed_regs[2] = ! TARGET_APP_REGS;
12026 if (fixed_regs[3] == 2)
12027 fixed_regs[3] = ! TARGET_APP_REGS;
12028 if (TARGET_ARCH32 && fixed_regs[4] == 2)
12029 fixed_regs[4] = ! TARGET_APP_REGS;
12030 else if (TARGET_CM_EMBMEDANY)
12031 fixed_regs[4] = 1;
12032 else if (fixed_regs[4] == 2)
12033 fixed_regs[4] = 0;
12034 if (TARGET_FLAT)
12035 {
12036 int regno;
12037 /* Disable leaf functions. */
12038 memset (sparc_leaf_regs, 0, FIRST_PSEUDO_REGISTER);
12039 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
12040 leaf_reg_remap [regno] = regno;
12041 }
12042 if (TARGET_VIS)
12043 global_regs[SPARC_GSR_REG] = 1;
12044 }
12045
12046 /* Implement TARGET_PREFERRED_RELOAD_CLASS:
12047
12048 - We can't load constants into FP registers.
12049 - We can't load FP constants into integer registers when soft-float,
12050 because there is no soft-float pattern with a r/F constraint.
12051 - We can't load FP constants into integer registers for TFmode unless
12052 it is 0.0L, because there is no movtf pattern with a r/F constraint.
12053 - Try and reload integer constants (symbolic or otherwise) back into
12054 registers directly, rather than having them dumped to memory. */
12055
12056 static reg_class_t
12057 sparc_preferred_reload_class (rtx x, reg_class_t rclass)
12058 {
12059 enum machine_mode mode = GET_MODE (x);
12060 if (CONSTANT_P (x))
12061 {
12062 if (FP_REG_CLASS_P (rclass)
12063 || rclass == GENERAL_OR_FP_REGS
12064 || rclass == GENERAL_OR_EXTRA_FP_REGS
12065 || (GET_MODE_CLASS (mode) == MODE_FLOAT && ! TARGET_FPU)
12066 || (mode == TFmode && ! const_zero_operand (x, mode)))
12067 return NO_REGS;
12068
12069 if (GET_MODE_CLASS (mode) == MODE_INT)
12070 return GENERAL_REGS;
12071
12072 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
12073 {
12074 if (! FP_REG_CLASS_P (rclass)
12075 || !(const_zero_operand (x, mode)
12076 || const_all_ones_operand (x, mode)))
12077 return NO_REGS;
12078 }
12079 }
12080
12081 if (TARGET_VIS3
12082 && ! TARGET_ARCH64
12083 && (rclass == EXTRA_FP_REGS
12084 || rclass == GENERAL_OR_EXTRA_FP_REGS))
12085 {
12086 int regno = true_regnum (x);
12087
12088 if (SPARC_INT_REG_P (regno))
12089 return (rclass == EXTRA_FP_REGS
12090 ? FP_REGS : GENERAL_OR_FP_REGS);
12091 }
12092
12093 return rclass;
12094 }
12095
12096 /* Output a wide multiply instruction in V8+ mode. INSN is the instruction,
12097 OPERANDS are its operands and OPCODE is the mnemonic to be used. */
12098
12099 const char *
12100 output_v8plus_mult (rtx_insn *insn, rtx *operands, const char *opcode)
12101 {
12102 char mulstr[32];
12103
12104 gcc_assert (! TARGET_ARCH64);
12105
12106 if (sparc_check_64 (operands[1], insn) <= 0)
12107 output_asm_insn ("srl\t%L1, 0, %L1", operands);
12108 if (which_alternative == 1)
12109 output_asm_insn ("sllx\t%H1, 32, %H1", operands);
12110 if (GET_CODE (operands[2]) == CONST_INT)
12111 {
12112 if (which_alternative == 1)
12113 {
12114 output_asm_insn ("or\t%L1, %H1, %H1", operands);
12115 sprintf (mulstr, "%s\t%%H1, %%2, %%L0", opcode);
12116 output_asm_insn (mulstr, operands);
12117 return "srlx\t%L0, 32, %H0";
12118 }
12119 else
12120 {
12121 output_asm_insn ("sllx\t%H1, 32, %3", operands);
12122 output_asm_insn ("or\t%L1, %3, %3", operands);
12123 sprintf (mulstr, "%s\t%%3, %%2, %%3", opcode);
12124 output_asm_insn (mulstr, operands);
12125 output_asm_insn ("srlx\t%3, 32, %H0", operands);
12126 return "mov\t%3, %L0";
12127 }
12128 }
12129 else if (rtx_equal_p (operands[1], operands[2]))
12130 {
12131 if (which_alternative == 1)
12132 {
12133 output_asm_insn ("or\t%L1, %H1, %H1", operands);
12134 sprintf (mulstr, "%s\t%%H1, %%H1, %%L0", opcode);
12135 output_asm_insn (mulstr, operands);
12136 return "srlx\t%L0, 32, %H0";
12137 }
12138 else
12139 {
12140 output_asm_insn ("sllx\t%H1, 32, %3", operands);
12141 output_asm_insn ("or\t%L1, %3, %3", operands);
12142 sprintf (mulstr, "%s\t%%3, %%3, %%3", opcode);
12143 output_asm_insn (mulstr, operands);
12144 output_asm_insn ("srlx\t%3, 32, %H0", operands);
12145 return "mov\t%3, %L0";
12146 }
12147 }
12148 if (sparc_check_64 (operands[2], insn) <= 0)
12149 output_asm_insn ("srl\t%L2, 0, %L2", operands);
12150 if (which_alternative == 1)
12151 {
12152 output_asm_insn ("or\t%L1, %H1, %H1", operands);
12153 output_asm_insn ("sllx\t%H2, 32, %L1", operands);
12154 output_asm_insn ("or\t%L2, %L1, %L1", operands);
12155 sprintf (mulstr, "%s\t%%H1, %%L1, %%L0", opcode);
12156 output_asm_insn (mulstr, operands);
12157 return "srlx\t%L0, 32, %H0";
12158 }
12159 else
12160 {
12161 output_asm_insn ("sllx\t%H1, 32, %3", operands);
12162 output_asm_insn ("sllx\t%H2, 32, %4", operands);
12163 output_asm_insn ("or\t%L1, %3, %3", operands);
12164 output_asm_insn ("or\t%L2, %4, %4", operands);
12165 sprintf (mulstr, "%s\t%%3, %%4, %%3", opcode);
12166 output_asm_insn (mulstr, operands);
12167 output_asm_insn ("srlx\t%3, 32, %H0", operands);
12168 return "mov\t%3, %L0";
12169 }
12170 }
12171
12172 /* Subroutine of sparc_expand_vector_init. Emit code to initialize
12173 all fields of TARGET to ELT by means of VIS2 BSHUFFLE insn. MODE
12174 and INNER_MODE are the modes describing TARGET. */
12175
12176 static void
12177 vector_init_bshuffle (rtx target, rtx elt, enum machine_mode mode,
12178 enum machine_mode inner_mode)
12179 {
12180 rtx t1, final_insn, sel;
12181 int bmask;
12182
12183 t1 = gen_reg_rtx (mode);
12184
12185 elt = convert_modes (SImode, inner_mode, elt, true);
12186 emit_move_insn (gen_lowpart(SImode, t1), elt);
12187
12188 switch (mode)
12189 {
12190 case V2SImode:
12191 final_insn = gen_bshufflev2si_vis (target, t1, t1);
12192 bmask = 0x45674567;
12193 break;
12194 case V4HImode:
12195 final_insn = gen_bshufflev4hi_vis (target, t1, t1);
12196 bmask = 0x67676767;
12197 break;
12198 case V8QImode:
12199 final_insn = gen_bshufflev8qi_vis (target, t1, t1);
12200 bmask = 0x77777777;
12201 break;
12202 default:
12203 gcc_unreachable ();
12204 }
12205
12206 sel = force_reg (SImode, GEN_INT (bmask));
12207 emit_insn (gen_bmasksi_vis (gen_rtx_REG (SImode, 0), sel, const0_rtx));
12208 emit_insn (final_insn);
12209 }
12210
12211 /* Subroutine of sparc_expand_vector_init. Emit code to initialize
12212 all fields of TARGET to ELT in V8QI by means of VIS FPMERGE insn. */
12213
12214 static void
12215 vector_init_fpmerge (rtx target, rtx elt)
12216 {
12217 rtx t1, t2, t2_low, t3, t3_low;
12218
12219 t1 = gen_reg_rtx (V4QImode);
12220 elt = convert_modes (SImode, QImode, elt, true);
12221 emit_move_insn (gen_lowpart (SImode, t1), elt);
12222
12223 t2 = gen_reg_rtx (V8QImode);
12224 t2_low = gen_lowpart (V4QImode, t2);
12225 emit_insn (gen_fpmerge_vis (t2, t1, t1));
12226
12227 t3 = gen_reg_rtx (V8QImode);
12228 t3_low = gen_lowpart (V4QImode, t3);
12229 emit_insn (gen_fpmerge_vis (t3, t2_low, t2_low));
12230
12231 emit_insn (gen_fpmerge_vis (target, t3_low, t3_low));
12232 }
12233
12234 /* Subroutine of sparc_expand_vector_init. Emit code to initialize
12235 all fields of TARGET to ELT in V4HI by means of VIS FALIGNDATA insn. */
12236
12237 static void
12238 vector_init_faligndata (rtx target, rtx elt)
12239 {
12240 rtx t1 = gen_reg_rtx (V4HImode);
12241 int i;
12242
12243 elt = convert_modes (SImode, HImode, elt, true);
12244 emit_move_insn (gen_lowpart (SImode, t1), elt);
12245
12246 emit_insn (gen_alignaddrsi_vis (gen_reg_rtx (SImode),
12247 force_reg (SImode, GEN_INT (6)),
12248 const0_rtx));
12249
12250 for (i = 0; i < 4; i++)
12251 emit_insn (gen_faligndatav4hi_vis (target, t1, target));
12252 }
12253
12254 /* Emit code to initialize TARGET to values for individual fields VALS. */
12255
12256 void
12257 sparc_expand_vector_init (rtx target, rtx vals)
12258 {
12259 const enum machine_mode mode = GET_MODE (target);
12260 const enum machine_mode inner_mode = GET_MODE_INNER (mode);
12261 const int n_elts = GET_MODE_NUNITS (mode);
12262 int i, n_var = 0;
12263 bool all_same;
12264 rtx mem;
12265
12266 all_same = true;
12267 for (i = 0; i < n_elts; i++)
12268 {
12269 rtx x = XVECEXP (vals, 0, i);
12270 if (!CONSTANT_P (x))
12271 n_var++;
12272
12273 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
12274 all_same = false;
12275 }
12276
12277 if (n_var == 0)
12278 {
12279 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
12280 return;
12281 }
12282
12283 if (GET_MODE_SIZE (inner_mode) == GET_MODE_SIZE (mode))
12284 {
12285 if (GET_MODE_SIZE (inner_mode) == 4)
12286 {
12287 emit_move_insn (gen_lowpart (SImode, target),
12288 gen_lowpart (SImode, XVECEXP (vals, 0, 0)));
12289 return;
12290 }
12291 else if (GET_MODE_SIZE (inner_mode) == 8)
12292 {
12293 emit_move_insn (gen_lowpart (DImode, target),
12294 gen_lowpart (DImode, XVECEXP (vals, 0, 0)));
12295 return;
12296 }
12297 }
12298 else if (GET_MODE_SIZE (inner_mode) == GET_MODE_SIZE (word_mode)
12299 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_mode))
12300 {
12301 emit_move_insn (gen_highpart (word_mode, target),
12302 gen_lowpart (word_mode, XVECEXP (vals, 0, 0)));
12303 emit_move_insn (gen_lowpart (word_mode, target),
12304 gen_lowpart (word_mode, XVECEXP (vals, 0, 1)));
12305 return;
12306 }
12307
12308 if (all_same && GET_MODE_SIZE (mode) == 8)
12309 {
12310 if (TARGET_VIS2)
12311 {
12312 vector_init_bshuffle (target, XVECEXP (vals, 0, 0), mode, inner_mode);
12313 return;
12314 }
12315 if (mode == V8QImode)
12316 {
12317 vector_init_fpmerge (target, XVECEXP (vals, 0, 0));
12318 return;
12319 }
12320 if (mode == V4HImode)
12321 {
12322 vector_init_faligndata (target, XVECEXP (vals, 0, 0));
12323 return;
12324 }
12325 }
12326
12327 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
12328 for (i = 0; i < n_elts; i++)
12329 emit_move_insn (adjust_address_nv (mem, inner_mode,
12330 i * GET_MODE_SIZE (inner_mode)),
12331 XVECEXP (vals, 0, i));
12332 emit_move_insn (target, mem);
12333 }
12334
12335 /* Implement TARGET_SECONDARY_RELOAD. */
12336
12337 static reg_class_t
12338 sparc_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
12339 enum machine_mode mode, secondary_reload_info *sri)
12340 {
12341 enum reg_class rclass = (enum reg_class) rclass_i;
12342
12343 sri->icode = CODE_FOR_nothing;
12344 sri->extra_cost = 0;
12345
12346 /* We need a temporary when loading/storing a HImode/QImode value
12347 between memory and the FPU registers. This can happen when combine puts
12348 a paradoxical subreg in a float/fix conversion insn. */
12349 if (FP_REG_CLASS_P (rclass)
12350 && (mode == HImode || mode == QImode)
12351 && (GET_CODE (x) == MEM
12352 || ((GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
12353 && true_regnum (x) == -1)))
12354 return GENERAL_REGS;
12355
12356 /* On 32-bit we need a temporary when loading/storing a DFmode value
12357 between unaligned memory and the upper FPU registers. */
12358 if (TARGET_ARCH32
12359 && rclass == EXTRA_FP_REGS
12360 && mode == DFmode
12361 && GET_CODE (x) == MEM
12362 && ! mem_min_alignment (x, 8))
12363 return FP_REGS;
12364
12365 if (((TARGET_CM_MEDANY
12366 && symbolic_operand (x, mode))
12367 || (TARGET_CM_EMBMEDANY
12368 && text_segment_operand (x, mode)))
12369 && ! flag_pic)
12370 {
12371 if (in_p)
12372 sri->icode = direct_optab_handler (reload_in_optab, mode);
12373 else
12374 sri->icode = direct_optab_handler (reload_out_optab, mode);
12375 return NO_REGS;
12376 }
12377
12378 if (TARGET_VIS3 && TARGET_ARCH32)
12379 {
12380 int regno = true_regnum (x);
12381
12382 /* When using VIS3 fp<-->int register moves, on 32-bit we have
12383 to move 8-byte values in 4-byte pieces. This only works via
12384 FP_REGS, and not via EXTRA_FP_REGS. Therefore if we try to
12385 move between EXTRA_FP_REGS and GENERAL_REGS, we will need
12386 an FP_REGS intermediate move. */
12387 if ((rclass == EXTRA_FP_REGS && SPARC_INT_REG_P (regno))
12388 || ((general_or_i64_p (rclass)
12389 || rclass == GENERAL_OR_FP_REGS)
12390 && SPARC_FP_REG_P (regno)))
12391 {
12392 sri->extra_cost = 2;
12393 return FP_REGS;
12394 }
12395 }
12396
12397 return NO_REGS;
12398 }
12399
12400 /* Emit code to conditionally move either OPERANDS[2] or OPERANDS[3] into
12401 OPERANDS[0] in MODE. OPERANDS[1] is the operator of the condition. */
12402
12403 bool
12404 sparc_expand_conditional_move (enum machine_mode mode, rtx *operands)
12405 {
12406 enum rtx_code rc = GET_CODE (operands[1]);
12407 enum machine_mode cmp_mode;
12408 rtx cc_reg, dst, cmp;
12409
12410 cmp = operands[1];
12411 if (GET_MODE (XEXP (cmp, 0)) == DImode && !TARGET_ARCH64)
12412 return false;
12413
12414 if (GET_MODE (XEXP (cmp, 0)) == TFmode && !TARGET_HARD_QUAD)
12415 cmp = sparc_emit_float_lib_cmp (XEXP (cmp, 0), XEXP (cmp, 1), rc);
12416
12417 cmp_mode = GET_MODE (XEXP (cmp, 0));
12418 rc = GET_CODE (cmp);
12419
12420 dst = operands[0];
12421 if (! rtx_equal_p (operands[2], dst)
12422 && ! rtx_equal_p (operands[3], dst))
12423 {
12424 if (reg_overlap_mentioned_p (dst, cmp))
12425 dst = gen_reg_rtx (mode);
12426
12427 emit_move_insn (dst, operands[3]);
12428 }
12429 else if (operands[2] == dst)
12430 {
12431 operands[2] = operands[3];
12432
12433 if (GET_MODE_CLASS (cmp_mode) == MODE_FLOAT)
12434 rc = reverse_condition_maybe_unordered (rc);
12435 else
12436 rc = reverse_condition (rc);
12437 }
12438
12439 if (XEXP (cmp, 1) == const0_rtx
12440 && GET_CODE (XEXP (cmp, 0)) == REG
12441 && cmp_mode == DImode
12442 && v9_regcmp_p (rc))
12443 cc_reg = XEXP (cmp, 0);
12444 else
12445 cc_reg = gen_compare_reg_1 (rc, XEXP (cmp, 0), XEXP (cmp, 1));
12446
12447 cmp = gen_rtx_fmt_ee (rc, GET_MODE (cc_reg), cc_reg, const0_rtx);
12448
12449 emit_insn (gen_rtx_SET (VOIDmode, dst,
12450 gen_rtx_IF_THEN_ELSE (mode, cmp, operands[2], dst)));
12451
12452 if (dst != operands[0])
12453 emit_move_insn (operands[0], dst);
12454
12455 return true;
12456 }
12457
12458 /* Emit code to conditionally move a combination of OPERANDS[1] and OPERANDS[2]
12459 into OPERANDS[0] in MODE, depending on the outcome of the comparison of
12460 OPERANDS[4] and OPERANDS[5]. OPERANDS[3] is the operator of the condition.
12461 FCODE is the machine code to be used for OPERANDS[3] and CCODE the machine
12462 code to be used for the condition mask. */
12463
12464 void
12465 sparc_expand_vcond (enum machine_mode mode, rtx *operands, int ccode, int fcode)
12466 {
12467 rtx mask, cop0, cop1, fcmp, cmask, bshuf, gsr;
12468 enum rtx_code code = GET_CODE (operands[3]);
12469
12470 mask = gen_reg_rtx (Pmode);
12471 cop0 = operands[4];
12472 cop1 = operands[5];
12473 if (code == LT || code == GE)
12474 {
12475 rtx t;
12476
12477 code = swap_condition (code);
12478 t = cop0; cop0 = cop1; cop1 = t;
12479 }
12480
12481 gsr = gen_rtx_REG (DImode, SPARC_GSR_REG);
12482
12483 fcmp = gen_rtx_UNSPEC (Pmode,
12484 gen_rtvec (1, gen_rtx_fmt_ee (code, mode, cop0, cop1)),
12485 fcode);
12486
12487 cmask = gen_rtx_UNSPEC (DImode,
12488 gen_rtvec (2, mask, gsr),
12489 ccode);
12490
12491 bshuf = gen_rtx_UNSPEC (mode,
12492 gen_rtvec (3, operands[1], operands[2], gsr),
12493 UNSPEC_BSHUFFLE);
12494
12495 emit_insn (gen_rtx_SET (VOIDmode, mask, fcmp));
12496 emit_insn (gen_rtx_SET (VOIDmode, gsr, cmask));
12497
12498 emit_insn (gen_rtx_SET (VOIDmode, operands[0], bshuf));
12499 }
12500
12501 /* On sparc, any mode which naturally allocates into the float
12502 registers should return 4 here. */
12503
12504 unsigned int
12505 sparc_regmode_natural_size (enum machine_mode mode)
12506 {
12507 int size = UNITS_PER_WORD;
12508
12509 if (TARGET_ARCH64)
12510 {
12511 enum mode_class mclass = GET_MODE_CLASS (mode);
12512
12513 if (mclass == MODE_FLOAT || mclass == MODE_VECTOR_INT)
12514 size = 4;
12515 }
12516
12517 return size;
12518 }
12519
12520 /* Return TRUE if it is a good idea to tie two pseudo registers
12521 when one has mode MODE1 and one has mode MODE2.
12522 If HARD_REGNO_MODE_OK could produce different values for MODE1 and MODE2,
12523 for any hard reg, then this must be FALSE for correct output.
12524
12525 For V9 we have to deal with the fact that only the lower 32 floating
12526 point registers are 32-bit addressable. */
12527
12528 bool
12529 sparc_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
12530 {
12531 enum mode_class mclass1, mclass2;
12532 unsigned short size1, size2;
12533
12534 if (mode1 == mode2)
12535 return true;
12536
12537 mclass1 = GET_MODE_CLASS (mode1);
12538 mclass2 = GET_MODE_CLASS (mode2);
12539 if (mclass1 != mclass2)
12540 return false;
12541
12542 if (! TARGET_V9)
12543 return true;
12544
12545 /* Classes are the same and we are V9 so we have to deal with upper
12546 vs. lower floating point registers. If one of the modes is a
12547 4-byte mode, and the other is not, we have to mark them as not
12548 tieable because only the lower 32 floating point register are
12549 addressable 32-bits at a time.
12550
12551 We can't just test explicitly for SFmode, otherwise we won't
12552 cover the vector mode cases properly. */
12553
12554 if (mclass1 != MODE_FLOAT && mclass1 != MODE_VECTOR_INT)
12555 return true;
12556
12557 size1 = GET_MODE_SIZE (mode1);
12558 size2 = GET_MODE_SIZE (mode2);
12559 if ((size1 > 4 && size2 == 4)
12560 || (size2 > 4 && size1 == 4))
12561 return false;
12562
12563 return true;
12564 }
12565
12566 /* Implement TARGET_CSTORE_MODE. */
12567
12568 static enum machine_mode
12569 sparc_cstore_mode (enum insn_code icode ATTRIBUTE_UNUSED)
12570 {
12571 return (TARGET_ARCH64 ? DImode : SImode);
12572 }
12573
12574 /* Return the compound expression made of T1 and T2. */
12575
12576 static inline tree
12577 compound_expr (tree t1, tree t2)
12578 {
12579 return build2 (COMPOUND_EXPR, void_type_node, t1, t2);
12580 }
12581
12582 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook. */
12583
12584 static void
12585 sparc_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
12586 {
12587 if (!TARGET_FPU)
12588 return;
12589
12590 const unsigned HOST_WIDE_INT accrued_exception_mask = 0x1f << 5;
12591 const unsigned HOST_WIDE_INT trap_enable_mask = 0x1f << 23;
12592
12593 /* We generate the equivalent of feholdexcept (&fenv_var):
12594
12595 unsigned int fenv_var;
12596 __builtin_store_fsr (&fenv_var);
12597
12598 unsigned int tmp1_var;
12599 tmp1_var = fenv_var & ~(accrued_exception_mask | trap_enable_mask);
12600
12601 __builtin_load_fsr (&tmp1_var); */
12602
12603 tree fenv_var = create_tmp_var (unsigned_type_node, NULL);
12604 mark_addressable (fenv_var);
12605 tree fenv_addr = build_fold_addr_expr (fenv_var);
12606 tree stfsr = sparc_builtins[SPARC_BUILTIN_STFSR];
12607 tree hold_stfsr = build_call_expr (stfsr, 1, fenv_addr);
12608
12609 tree tmp1_var = create_tmp_var (unsigned_type_node, NULL);
12610 mark_addressable (tmp1_var);
12611 tree masked_fenv_var
12612 = build2 (BIT_AND_EXPR, unsigned_type_node, fenv_var,
12613 build_int_cst (unsigned_type_node,
12614 ~(accrued_exception_mask | trap_enable_mask)));
12615 tree hold_mask
12616 = build2 (MODIFY_EXPR, void_type_node, tmp1_var, masked_fenv_var);
12617
12618 tree tmp1_addr = build_fold_addr_expr (tmp1_var);
12619 tree ldfsr = sparc_builtins[SPARC_BUILTIN_LDFSR];
12620 tree hold_ldfsr = build_call_expr (ldfsr, 1, tmp1_addr);
12621
12622 *hold = compound_expr (compound_expr (hold_stfsr, hold_mask), hold_ldfsr);
12623
12624 /* We reload the value of tmp1_var to clear the exceptions:
12625
12626 __builtin_load_fsr (&tmp1_var); */
12627
12628 *clear = build_call_expr (ldfsr, 1, tmp1_addr);
12629
12630 /* We generate the equivalent of feupdateenv (&fenv_var):
12631
12632 unsigned int tmp2_var;
12633 __builtin_store_fsr (&tmp2_var);
12634
12635 __builtin_load_fsr (&fenv_var);
12636
12637 if (SPARC_LOW_FE_EXCEPT_VALUES)
12638 tmp2_var >>= 5;
12639 __atomic_feraiseexcept ((int) tmp2_var); */
12640
12641 tree tmp2_var = create_tmp_var (unsigned_type_node, NULL);
12642 mark_addressable (tmp2_var);
12643 tree tmp3_addr = build_fold_addr_expr (tmp2_var);
12644 tree update_stfsr = build_call_expr (stfsr, 1, tmp3_addr);
12645
12646 tree update_ldfsr = build_call_expr (ldfsr, 1, fenv_addr);
12647
12648 tree atomic_feraiseexcept
12649 = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
12650 tree update_call
12651 = build_call_expr (atomic_feraiseexcept, 1,
12652 fold_convert (integer_type_node, tmp2_var));
12653
12654 if (SPARC_LOW_FE_EXCEPT_VALUES)
12655 {
12656 tree shifted_tmp2_var
12657 = build2 (RSHIFT_EXPR, unsigned_type_node, tmp2_var,
12658 build_int_cst (unsigned_type_node, 5));
12659 tree update_shift
12660 = build2 (MODIFY_EXPR, void_type_node, tmp2_var, shifted_tmp2_var);
12661 update_call = compound_expr (update_shift, update_call);
12662 }
12663
12664 *update
12665 = compound_expr (compound_expr (update_stfsr, update_ldfsr), update_call);
12666 }
12667
12668 #include "gt-sparc.h"