Debian Codegraph @

gcc/config/sparc/sparc.cc

raw
1 /* Subroutines for insn-output.cc for SPARC.
2 Copyright (C) 1987-2025 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann ([email protected])
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 #define IN_TARGET_CODE 1
24
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "backend.h"
29 #include "target.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "memmodel.h"
33 #include "gimple.h"
34 #include "df.h"
35 #include "tm_p.h"
36 #include "stringpool.h"
37 #include "attribs.h"
38 #include "expmed.h"
39 #include "optabs.h"
40 #include "regs.h"
41 #include "emit-rtl.h"
42 #include "recog.h"
43 #include "diagnostic-core.h"
44 #include "alias.h"
45 #include "fold-const.h"
46 #include "stor-layout.h"
47 #include "calls.h"
48 #include "varasm.h"
49 #include "output.h"
50 #include "insn-attr.h"
51 #include "explow.h"
52 #include "expr.h"
53 #include "debug.h"
54 #include "cfgrtl.h"
55 #include "common/common-target.h"
56 #include "gimplify.h"
57 #include "langhooks.h"
58 #include "reload.h"
59 #include "tree-pass.h"
60 #include "context.h"
61 #include "builtins.h"
62 #include "tree-vector-builder.h"
63 #include "opts.h"
64 #include "dwarf2out.h"
65
66 /* This file should be included last. */
67 #include "target-def.h"
68
69 /* Processor costs */
70
71 struct processor_costs {
72 /* Integer load */
73 const int int_load;
74
75 /* Integer signed load */
76 const int int_sload;
77
78 /* Integer zeroed load */
79 const int int_zload;
80
81 /* Float load */
82 const int float_load;
83
84 /* fmov, fneg, fabs */
85 const int float_move;
86
87 /* fadd, fsub */
88 const int float_plusminus;
89
90 /* fcmp */
91 const int float_cmp;
92
93 /* fmov, fmovr */
94 const int float_cmove;
95
96 /* fmul */
97 const int float_mul;
98
99 /* fdivs */
100 const int float_div_sf;
101
102 /* fdivd */
103 const int float_div_df;
104
105 /* fsqrts */
106 const int float_sqrt_sf;
107
108 /* fsqrtd */
109 const int float_sqrt_df;
110
111 /* umul/smul */
112 const int int_mul;
113
114 /* mulX */
115 const int int_mulX;
116
117 /* integer multiply cost for each bit set past the most
118 significant 3, so the formula for multiply cost becomes:
119
120 if (rs1 < 0)
121 highest_bit = highest_clear_bit(rs1);
122 else
123 highest_bit = highest_set_bit(rs1);
124 if (highest_bit < 3)
125 highest_bit = 3;
126 cost = int_mul{,X} + ((highest_bit - 3) / int_mul_bit_factor);
127
128 A value of zero indicates that the multiply costs is fixed,
129 and not variable. */
130 const int int_mul_bit_factor;
131
132 /* udiv/sdiv */
133 const int int_div;
134
135 /* divX */
136 const int int_divX;
137
138 /* movcc, movr */
139 const int int_cmove;
140
141 /* penalty for shifts, due to scheduling rules etc. */
142 const int shift_penalty;
143
144 /* cost of a (predictable) branch. */
145 const int branch_cost;
146 };
147
148 static const
149 struct processor_costs cypress_costs = {
150 COSTS_N_INSNS (2), /* int load */
151 COSTS_N_INSNS (2), /* int signed load */
152 COSTS_N_INSNS (2), /* int zeroed load */
153 COSTS_N_INSNS (2), /* float load */
154 COSTS_N_INSNS (5), /* fmov, fneg, fabs */
155 COSTS_N_INSNS (5), /* fadd, fsub */
156 COSTS_N_INSNS (1), /* fcmp */
157 COSTS_N_INSNS (1), /* fmov, fmovr */
158 COSTS_N_INSNS (7), /* fmul */
159 COSTS_N_INSNS (37), /* fdivs */
160 COSTS_N_INSNS (37), /* fdivd */
161 COSTS_N_INSNS (63), /* fsqrts */
162 COSTS_N_INSNS (63), /* fsqrtd */
163 COSTS_N_INSNS (1), /* imul */
164 COSTS_N_INSNS (1), /* imulX */
165 0, /* imul bit factor */
166 COSTS_N_INSNS (1), /* idiv */
167 COSTS_N_INSNS (1), /* idivX */
168 COSTS_N_INSNS (1), /* movcc/movr */
169 0, /* shift penalty */
170 3 /* branch cost */
171 };
172
173 static const
174 struct processor_costs supersparc_costs = {
175 COSTS_N_INSNS (1), /* int load */
176 COSTS_N_INSNS (1), /* int signed load */
177 COSTS_N_INSNS (1), /* int zeroed load */
178 COSTS_N_INSNS (0), /* float load */
179 COSTS_N_INSNS (3), /* fmov, fneg, fabs */
180 COSTS_N_INSNS (3), /* fadd, fsub */
181 COSTS_N_INSNS (3), /* fcmp */
182 COSTS_N_INSNS (1), /* fmov, fmovr */
183 COSTS_N_INSNS (3), /* fmul */
184 COSTS_N_INSNS (6), /* fdivs */
185 COSTS_N_INSNS (9), /* fdivd */
186 COSTS_N_INSNS (12), /* fsqrts */
187 COSTS_N_INSNS (12), /* fsqrtd */
188 COSTS_N_INSNS (4), /* imul */
189 COSTS_N_INSNS (4), /* imulX */
190 0, /* imul bit factor */
191 COSTS_N_INSNS (4), /* idiv */
192 COSTS_N_INSNS (4), /* idivX */
193 COSTS_N_INSNS (1), /* movcc/movr */
194 1, /* shift penalty */
195 3 /* branch cost */
196 };
197
198 static const
199 struct processor_costs hypersparc_costs = {
200 COSTS_N_INSNS (1), /* int load */
201 COSTS_N_INSNS (1), /* int signed load */
202 COSTS_N_INSNS (1), /* int zeroed load */
203 COSTS_N_INSNS (1), /* float load */
204 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
205 COSTS_N_INSNS (1), /* fadd, fsub */
206 COSTS_N_INSNS (1), /* fcmp */
207 COSTS_N_INSNS (1), /* fmov, fmovr */
208 COSTS_N_INSNS (1), /* fmul */
209 COSTS_N_INSNS (8), /* fdivs */
210 COSTS_N_INSNS (12), /* fdivd */
211 COSTS_N_INSNS (17), /* fsqrts */
212 COSTS_N_INSNS (17), /* fsqrtd */
213 COSTS_N_INSNS (17), /* imul */
214 COSTS_N_INSNS (17), /* imulX */
215 0, /* imul bit factor */
216 COSTS_N_INSNS (17), /* idiv */
217 COSTS_N_INSNS (17), /* idivX */
218 COSTS_N_INSNS (1), /* movcc/movr */
219 0, /* shift penalty */
220 3 /* branch cost */
221 };
222
223 static const
224 struct processor_costs leon_costs = {
225 COSTS_N_INSNS (1), /* int load */
226 COSTS_N_INSNS (1), /* int signed load */
227 COSTS_N_INSNS (1), /* int zeroed load */
228 COSTS_N_INSNS (1), /* float load */
229 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
230 COSTS_N_INSNS (1), /* fadd, fsub */
231 COSTS_N_INSNS (1), /* fcmp */
232 COSTS_N_INSNS (1), /* fmov, fmovr */
233 COSTS_N_INSNS (1), /* fmul */
234 COSTS_N_INSNS (15), /* fdivs */
235 COSTS_N_INSNS (15), /* fdivd */
236 COSTS_N_INSNS (23), /* fsqrts */
237 COSTS_N_INSNS (23), /* fsqrtd */
238 COSTS_N_INSNS (5), /* imul */
239 COSTS_N_INSNS (5), /* imulX */
240 0, /* imul bit factor */
241 COSTS_N_INSNS (5), /* idiv */
242 COSTS_N_INSNS (5), /* idivX */
243 COSTS_N_INSNS (1), /* movcc/movr */
244 0, /* shift penalty */
245 3 /* branch cost */
246 };
247
248 static const
249 struct processor_costs leon3_costs = {
250 COSTS_N_INSNS (1), /* int load */
251 COSTS_N_INSNS (1), /* int signed load */
252 COSTS_N_INSNS (1), /* int zeroed load */
253 COSTS_N_INSNS (1), /* float load */
254 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
255 COSTS_N_INSNS (1), /* fadd, fsub */
256 COSTS_N_INSNS (1), /* fcmp */
257 COSTS_N_INSNS (1), /* fmov, fmovr */
258 COSTS_N_INSNS (1), /* fmul */
259 COSTS_N_INSNS (14), /* fdivs */
260 COSTS_N_INSNS (15), /* fdivd */
261 COSTS_N_INSNS (22), /* fsqrts */
262 COSTS_N_INSNS (23), /* fsqrtd */
263 COSTS_N_INSNS (5), /* imul */
264 COSTS_N_INSNS (5), /* imulX */
265 0, /* imul bit factor */
266 COSTS_N_INSNS (35), /* idiv */
267 COSTS_N_INSNS (35), /* idivX */
268 COSTS_N_INSNS (1), /* movcc/movr */
269 0, /* shift penalty */
270 3 /* branch cost */
271 };
272
273 static const
274 struct processor_costs leon5_costs = {
275 COSTS_N_INSNS (1), /* int load */
276 COSTS_N_INSNS (1), /* int signed load */
277 COSTS_N_INSNS (1), /* int zeroed load */
278 COSTS_N_INSNS (1), /* float load */
279 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
280 COSTS_N_INSNS (1), /* fadd, fsub */
281 COSTS_N_INSNS (1), /* fcmp */
282 COSTS_N_INSNS (1), /* fmov, fmovr */
283 COSTS_N_INSNS (1), /* fmul */
284 COSTS_N_INSNS (17), /* fdivs */
285 COSTS_N_INSNS (18), /* fdivd */
286 COSTS_N_INSNS (25), /* fsqrts */
287 COSTS_N_INSNS (26), /* fsqrtd */
288 COSTS_N_INSNS (4), /* imul */
289 COSTS_N_INSNS (4), /* imulX */
290 0, /* imul bit factor */
291 COSTS_N_INSNS (35), /* idiv */
292 COSTS_N_INSNS (35), /* idivX */
293 COSTS_N_INSNS (1), /* movcc/movr */
294 0, /* shift penalty */
295 3 /* branch cost */
296 };
297
298 static const
299 struct processor_costs sparclet_costs = {
300 COSTS_N_INSNS (3), /* int load */
301 COSTS_N_INSNS (3), /* int signed load */
302 COSTS_N_INSNS (1), /* int zeroed load */
303 COSTS_N_INSNS (1), /* float load */
304 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
305 COSTS_N_INSNS (1), /* fadd, fsub */
306 COSTS_N_INSNS (1), /* fcmp */
307 COSTS_N_INSNS (1), /* fmov, fmovr */
308 COSTS_N_INSNS (1), /* fmul */
309 COSTS_N_INSNS (1), /* fdivs */
310 COSTS_N_INSNS (1), /* fdivd */
311 COSTS_N_INSNS (1), /* fsqrts */
312 COSTS_N_INSNS (1), /* fsqrtd */
313 COSTS_N_INSNS (5), /* imul */
314 COSTS_N_INSNS (5), /* imulX */
315 0, /* imul bit factor */
316 COSTS_N_INSNS (5), /* idiv */
317 COSTS_N_INSNS (5), /* idivX */
318 COSTS_N_INSNS (1), /* movcc/movr */
319 0, /* shift penalty */
320 3 /* branch cost */
321 };
322
323 static const
324 struct processor_costs ultrasparc_costs = {
325 COSTS_N_INSNS (2), /* int load */
326 COSTS_N_INSNS (3), /* int signed load */
327 COSTS_N_INSNS (2), /* int zeroed load */
328 COSTS_N_INSNS (2), /* float load */
329 COSTS_N_INSNS (1), /* fmov, fneg, fabs */
330 COSTS_N_INSNS (4), /* fadd, fsub */
331 COSTS_N_INSNS (1), /* fcmp */
332 COSTS_N_INSNS (2), /* fmov, fmovr */
333 COSTS_N_INSNS (4), /* fmul */
334 COSTS_N_INSNS (13), /* fdivs */
335 COSTS_N_INSNS (23), /* fdivd */
336 COSTS_N_INSNS (13), /* fsqrts */
337 COSTS_N_INSNS (23), /* fsqrtd */
338 COSTS_N_INSNS (4), /* imul */
339 COSTS_N_INSNS (4), /* imulX */
340 2, /* imul bit factor */
341 COSTS_N_INSNS (37), /* idiv */
342 COSTS_N_INSNS (68), /* idivX */
343 COSTS_N_INSNS (2), /* movcc/movr */
344 2, /* shift penalty */
345 2 /* branch cost */
346 };
347
348 static const
349 struct processor_costs ultrasparc3_costs = {
350 COSTS_N_INSNS (2), /* int load */
351 COSTS_N_INSNS (3), /* int signed load */
352 COSTS_N_INSNS (3), /* int zeroed load */
353 COSTS_N_INSNS (2), /* float load */
354 COSTS_N_INSNS (3), /* fmov, fneg, fabs */
355 COSTS_N_INSNS (4), /* fadd, fsub */
356 COSTS_N_INSNS (5), /* fcmp */
357 COSTS_N_INSNS (3), /* fmov, fmovr */
358 COSTS_N_INSNS (4), /* fmul */
359 COSTS_N_INSNS (17), /* fdivs */
360 COSTS_N_INSNS (20), /* fdivd */
361 COSTS_N_INSNS (20), /* fsqrts */
362 COSTS_N_INSNS (29), /* fsqrtd */
363 COSTS_N_INSNS (6), /* imul */
364 COSTS_N_INSNS (6), /* imulX */
365 0, /* imul bit factor */
366 COSTS_N_INSNS (40), /* idiv */
367 COSTS_N_INSNS (71), /* idivX */
368 COSTS_N_INSNS (2), /* movcc/movr */
369 0, /* shift penalty */
370 2 /* branch cost */
371 };
372
373 static const
374 struct processor_costs niagara_costs = {
375 COSTS_N_INSNS (3), /* int load */
376 COSTS_N_INSNS (3), /* int signed load */
377 COSTS_N_INSNS (3), /* int zeroed load */
378 COSTS_N_INSNS (9), /* float load */
379 COSTS_N_INSNS (8), /* fmov, fneg, fabs */
380 COSTS_N_INSNS (8), /* fadd, fsub */
381 COSTS_N_INSNS (26), /* fcmp */
382 COSTS_N_INSNS (8), /* fmov, fmovr */
383 COSTS_N_INSNS (29), /* fmul */
384 COSTS_N_INSNS (54), /* fdivs */
385 COSTS_N_INSNS (83), /* fdivd */
386 COSTS_N_INSNS (100), /* fsqrts - not implemented in hardware */
387 COSTS_N_INSNS (100), /* fsqrtd - not implemented in hardware */
388 COSTS_N_INSNS (11), /* imul */
389 COSTS_N_INSNS (11), /* imulX */
390 0, /* imul bit factor */
391 COSTS_N_INSNS (72), /* idiv */
392 COSTS_N_INSNS (72), /* idivX */
393 COSTS_N_INSNS (1), /* movcc/movr */
394 0, /* shift penalty */
395 4 /* branch cost */
396 };
397
398 static const
399 struct processor_costs niagara2_costs = {
400 COSTS_N_INSNS (3), /* int load */
401 COSTS_N_INSNS (3), /* int signed load */
402 COSTS_N_INSNS (3), /* int zeroed load */
403 COSTS_N_INSNS (3), /* float load */
404 COSTS_N_INSNS (6), /* fmov, fneg, fabs */
405 COSTS_N_INSNS (6), /* fadd, fsub */
406 COSTS_N_INSNS (6), /* fcmp */
407 COSTS_N_INSNS (6), /* fmov, fmovr */
408 COSTS_N_INSNS (6), /* fmul */
409 COSTS_N_INSNS (19), /* fdivs */
410 COSTS_N_INSNS (33), /* fdivd */
411 COSTS_N_INSNS (19), /* fsqrts */
412 COSTS_N_INSNS (33), /* fsqrtd */
413 COSTS_N_INSNS (5), /* imul */
414 COSTS_N_INSNS (5), /* imulX */
415 0, /* imul bit factor */
416 COSTS_N_INSNS (26), /* idiv, average of 12 - 41 cycle range */
417 COSTS_N_INSNS (26), /* idivX, average of 12 - 41 cycle range */
418 COSTS_N_INSNS (1), /* movcc/movr */
419 0, /* shift penalty */
420 5 /* branch cost */
421 };
422
423 static const
424 struct processor_costs niagara3_costs = {
425 COSTS_N_INSNS (3), /* int load */
426 COSTS_N_INSNS (3), /* int signed load */
427 COSTS_N_INSNS (3), /* int zeroed load */
428 COSTS_N_INSNS (3), /* float load */
429 COSTS_N_INSNS (9), /* fmov, fneg, fabs */
430 COSTS_N_INSNS (9), /* fadd, fsub */
431 COSTS_N_INSNS (9), /* fcmp */
432 COSTS_N_INSNS (9), /* fmov, fmovr */
433 COSTS_N_INSNS (9), /* fmul */
434 COSTS_N_INSNS (23), /* fdivs */
435 COSTS_N_INSNS (37), /* fdivd */
436 COSTS_N_INSNS (23), /* fsqrts */
437 COSTS_N_INSNS (37), /* fsqrtd */
438 COSTS_N_INSNS (9), /* imul */
439 COSTS_N_INSNS (9), /* imulX */
440 0, /* imul bit factor */
441 COSTS_N_INSNS (31), /* idiv, average of 17 - 45 cycle range */
442 COSTS_N_INSNS (30), /* idivX, average of 16 - 44 cycle range */
443 COSTS_N_INSNS (1), /* movcc/movr */
444 0, /* shift penalty */
445 5 /* branch cost */
446 };
447
448 static const
449 struct processor_costs niagara4_costs = {
450 COSTS_N_INSNS (5), /* int load */
451 COSTS_N_INSNS (5), /* int signed load */
452 COSTS_N_INSNS (5), /* int zeroed load */
453 COSTS_N_INSNS (5), /* float load */
454 COSTS_N_INSNS (11), /* fmov, fneg, fabs */
455 COSTS_N_INSNS (11), /* fadd, fsub */
456 COSTS_N_INSNS (11), /* fcmp */
457 COSTS_N_INSNS (11), /* fmov, fmovr */
458 COSTS_N_INSNS (11), /* fmul */
459 COSTS_N_INSNS (24), /* fdivs */
460 COSTS_N_INSNS (37), /* fdivd */
461 COSTS_N_INSNS (24), /* fsqrts */
462 COSTS_N_INSNS (37), /* fsqrtd */
463 COSTS_N_INSNS (12), /* imul */
464 COSTS_N_INSNS (12), /* imulX */
465 0, /* imul bit factor */
466 COSTS_N_INSNS (50), /* idiv, average of 41 - 60 cycle range */
467 COSTS_N_INSNS (35), /* idivX, average of 26 - 44 cycle range */
468 COSTS_N_INSNS (1), /* movcc/movr */
469 0, /* shift penalty */
470 2 /* branch cost */
471 };
472
473 static const
474 struct processor_costs niagara7_costs = {
475 COSTS_N_INSNS (5), /* int load */
476 COSTS_N_INSNS (5), /* int signed load */
477 COSTS_N_INSNS (5), /* int zeroed load */
478 COSTS_N_INSNS (5), /* float load */
479 COSTS_N_INSNS (11), /* fmov, fneg, fabs */
480 COSTS_N_INSNS (11), /* fadd, fsub */
481 COSTS_N_INSNS (11), /* fcmp */
482 COSTS_N_INSNS (11), /* fmov, fmovr */
483 COSTS_N_INSNS (11), /* fmul */
484 COSTS_N_INSNS (24), /* fdivs */
485 COSTS_N_INSNS (37), /* fdivd */
486 COSTS_N_INSNS (24), /* fsqrts */
487 COSTS_N_INSNS (37), /* fsqrtd */
488 COSTS_N_INSNS (12), /* imul */
489 COSTS_N_INSNS (12), /* imulX */
490 0, /* imul bit factor */
491 COSTS_N_INSNS (51), /* idiv, average of 42 - 61 cycle range */
492 COSTS_N_INSNS (35), /* idivX, average of 26 - 44 cycle range */
493 COSTS_N_INSNS (1), /* movcc/movr */
494 0, /* shift penalty */
495 1 /* branch cost */
496 };
497
498 static const
499 struct processor_costs m8_costs = {
500 COSTS_N_INSNS (3), /* int load */
501 COSTS_N_INSNS (3), /* int signed load */
502 COSTS_N_INSNS (3), /* int zeroed load */
503 COSTS_N_INSNS (3), /* float load */
504 COSTS_N_INSNS (9), /* fmov, fneg, fabs */
505 COSTS_N_INSNS (9), /* fadd, fsub */
506 COSTS_N_INSNS (9), /* fcmp */
507 COSTS_N_INSNS (9), /* fmov, fmovr */
508 COSTS_N_INSNS (9), /* fmul */
509 COSTS_N_INSNS (26), /* fdivs */
510 COSTS_N_INSNS (30), /* fdivd */
511 COSTS_N_INSNS (33), /* fsqrts */
512 COSTS_N_INSNS (41), /* fsqrtd */
513 COSTS_N_INSNS (12), /* imul */
514 COSTS_N_INSNS (10), /* imulX */
515 0, /* imul bit factor */
516 COSTS_N_INSNS (57), /* udiv/sdiv */
517 COSTS_N_INSNS (30), /* udivx/sdivx */
518 COSTS_N_INSNS (1), /* movcc/movr */
519 0, /* shift penalty */
520 1 /* branch cost */
521 };
522
523 static const struct processor_costs *sparc_costs = &cypress_costs;
524
525 #ifdef HAVE_AS_RELAX_OPTION
526 /* If 'as' and 'ld' are relaxing tail call insns into branch always, use
527 "or %o7,%g0,X; call Y; or X,%g0,%o7" always, so that it can be optimized.
528 With sethi/jmp, neither 'as' nor 'ld' has an easy way how to find out if
529 somebody does not branch between the sethi and jmp. */
530 #define LEAF_SIBCALL_SLOT_RESERVED_P 1
531 #else
532 #define LEAF_SIBCALL_SLOT_RESERVED_P \
533 ((TARGET_ARCH64 && !TARGET_CM_MEDLOW) || flag_pic)
534 #endif
535
536 /* Vector, indexed by hard register number, which contains 1
537 for a register that is allowable in a candidate for leaf
538 function treatment. */
539 char sparc_leaf_regs[] =
540 { 1, 1, 1, 1, 1, 1, 1, 1,
541 0, 0, 0, 0, 0, 0, 1, 0,
542 0, 0, 0, 0, 0, 0, 0, 0,
543 1, 1, 1, 1, 1, 1, 0, 1,
544 1, 1, 1, 1, 1, 1, 1, 1,
545 1, 1, 1, 1, 1, 1, 1, 1,
546 1, 1, 1, 1, 1, 1, 1, 1,
547 1, 1, 1, 1, 1, 1, 1, 1,
548 1, 1, 1, 1, 1, 1, 1, 1,
549 1, 1, 1, 1, 1, 1, 1, 1,
550 1, 1, 1, 1, 1, 1, 1, 1,
551 1, 1, 1, 1, 1, 1, 1, 1,
552 1, 1, 1, 1, 1, 1, 1};
553
554 struct GTY(()) machine_function
555 {
556 /* Size of the frame of the function. */
557 HOST_WIDE_INT frame_size;
558
559 /* Size of the frame of the function minus the register window save area
560 and the outgoing argument area. */
561 HOST_WIDE_INT apparent_frame_size;
562
563 /* Register we pretend the frame pointer is allocated to. Normally, this
564 is %fp, but if we are in a leaf procedure, this is (%sp + offset). We
565 record "offset" separately as it may be too big for (reg + disp). */
566 rtx frame_base_reg;
567 HOST_WIDE_INT frame_base_offset;
568
569 /* Number of global or FP registers to be saved (as 4-byte quantities). */
570 int n_global_fp_regs;
571
572 /* True if the current function is leaf and uses only leaf regs,
573 so that the SPARC leaf function optimization can be applied.
574 Private version of crtl->uses_only_leaf_regs, see
575 sparc_expand_prologue for the rationale. */
576 int leaf_function_p;
577
578 /* True if the prologue saves local or in registers. */
579 bool save_local_in_regs_p;
580
581 /* True if the data calculated by sparc_expand_prologue are valid. */
582 bool prologue_data_valid_p;
583 };
584
585 #define sparc_frame_size cfun->machine->frame_size
586 #define sparc_apparent_frame_size cfun->machine->apparent_frame_size
587 #define sparc_frame_base_reg cfun->machine->frame_base_reg
588 #define sparc_frame_base_offset cfun->machine->frame_base_offset
589 #define sparc_n_global_fp_regs cfun->machine->n_global_fp_regs
590 #define sparc_leaf_function_p cfun->machine->leaf_function_p
591 #define sparc_save_local_in_regs_p cfun->machine->save_local_in_regs_p
592 #define sparc_prologue_data_valid_p cfun->machine->prologue_data_valid_p
593
594 /* 1 if the next opcode is to be specially indented. */
595 int sparc_indent_opcode = 0;
596
597 static void sparc_option_override (void);
598 static void sparc_init_modes (void);
599 static int function_arg_slotno (const CUMULATIVE_ARGS *, machine_mode,
600 const_tree, bool, bool, int *, int *);
601
602 static int supersparc_adjust_cost (rtx_insn *, int, rtx_insn *, int);
603 static int hypersparc_adjust_cost (rtx_insn *, int, rtx_insn *, int);
604 static int leon5_adjust_cost (rtx_insn *, int, rtx_insn *, int);
605
606 static void sparc_emit_set_const32 (rtx, rtx);
607 static void sparc_emit_set_const64 (rtx, rtx);
608 static void sparc_output_addr_vec (rtx);
609 static void sparc_output_addr_diff_vec (rtx);
610 static void sparc_output_deferred_case_vectors (void);
611 static bool sparc_legitimate_address_p (machine_mode, rtx, bool,
612 code_helper = ERROR_MARK);
613 static bool sparc_legitimate_constant_p (machine_mode, rtx);
614 static rtx sparc_builtin_saveregs (void);
615 static int epilogue_renumber (rtx *, int);
616 static bool sparc_assemble_integer (rtx, unsigned int, int);
617 static int set_extends (rtx_insn *);
618 static void sparc_asm_function_prologue (FILE *);
619 static void sparc_asm_function_epilogue (FILE *);
620 #ifdef TARGET_SOLARIS
621 static void sparc_solaris_elf_asm_named_section (const char *, unsigned int,
622 tree) ATTRIBUTE_UNUSED;
623 #endif
624 static int sparc_adjust_cost (rtx_insn *, int, rtx_insn *, int, unsigned int);
625 static int sparc_issue_rate (void);
626 static void sparc_sched_init (FILE *, int, int);
627 static int sparc_use_sched_lookahead (void);
628
629 static void emit_soft_tfmode_libcall (const char *, int, rtx *);
630 static void emit_soft_tfmode_binop (enum rtx_code, rtx *);
631 static void emit_soft_tfmode_unop (enum rtx_code, rtx *);
632 static void emit_soft_tfmode_cvt (enum rtx_code, rtx *);
633 static void emit_hard_tfmode_operation (enum rtx_code, rtx *);
634
635 static bool sparc_function_ok_for_sibcall (tree, tree);
636 static void sparc_init_libfuncs (void);
637 static void sparc_init_builtins (void);
638 static void sparc_fpu_init_builtins (void);
639 static void sparc_vis_init_builtins (void);
640 static tree sparc_builtin_decl (unsigned, bool);
641 static rtx sparc_expand_builtin (tree, rtx, rtx, machine_mode, int);
642 static tree sparc_fold_builtin (tree, int, tree *, bool);
643 static void sparc_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
644 HOST_WIDE_INT, tree);
645 static bool sparc_can_output_mi_thunk (const_tree, HOST_WIDE_INT,
646 HOST_WIDE_INT, const_tree);
647 static struct machine_function * sparc_init_machine_status (void);
648 static bool sparc_cannot_force_const_mem (machine_mode, rtx);
649 static rtx sparc_tls_get_addr (void);
650 static rtx sparc_tls_got (void);
651 static int sparc_register_move_cost (machine_mode,
652 reg_class_t, reg_class_t);
653 static bool sparc_rtx_costs (rtx, machine_mode, int, int, int *, bool);
654 static machine_mode sparc_promote_function_mode (const_tree, machine_mode,
655 int *, const_tree, int);
656 static bool sparc_strict_argument_naming (cumulative_args_t);
657 static void sparc_va_start (tree, rtx);
658 static tree sparc_gimplify_va_arg (tree, tree, gimple_seq *, gimple_seq *);
659 static bool sparc_vector_mode_supported_p (machine_mode);
660 static bool sparc_tls_referenced_p (rtx);
661 static rtx sparc_legitimize_tls_address (rtx);
662 static rtx sparc_legitimize_pic_address (rtx, rtx);
663 static rtx sparc_legitimize_address (rtx, rtx, machine_mode);
664 static rtx sparc_delegitimize_address (rtx);
665 static bool sparc_mode_dependent_address_p (const_rtx, addr_space_t);
666 static bool sparc_pass_by_reference (cumulative_args_t,
667 const function_arg_info &);
668 static void sparc_function_arg_advance (cumulative_args_t,
669 const function_arg_info &);
670 static rtx sparc_function_arg (cumulative_args_t, const function_arg_info &);
671 static rtx sparc_function_incoming_arg (cumulative_args_t,
672 const function_arg_info &);
673 static pad_direction sparc_function_arg_padding (machine_mode, const_tree);
674 static unsigned int sparc_function_arg_boundary (machine_mode,
675 const_tree);
676 static int sparc_arg_partial_bytes (cumulative_args_t,
677 const function_arg_info &);
678 static bool sparc_return_in_memory (const_tree, const_tree);
679 static rtx sparc_struct_value_rtx (tree, int);
680 static rtx sparc_function_value (const_tree, const_tree, bool);
681 static rtx sparc_libcall_value (machine_mode, const_rtx);
682 static bool sparc_function_value_regno_p (const unsigned int);
683 static unsigned HOST_WIDE_INT sparc_asan_shadow_offset (void);
684 static void sparc_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
685 static bool sparc_output_cfi_directive (FILE *, dw_cfi_ref);
686 static bool sparc_dw_cfi_oprnd1_desc (dwarf_call_frame_info,
687 dw_cfi_oprnd_type &);
688 static void sparc_file_end (void);
689 static bool sparc_frame_pointer_required (void);
690 static bool sparc_can_eliminate (const int, const int);
691 static void sparc_conditional_register_usage (void);
692 static bool sparc_use_pseudo_pic_reg (void);
693 static void sparc_init_pic_reg (void);
694 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
695 static const char *sparc_mangle_type (const_tree);
696 #endif
697 static void sparc_trampoline_init (rtx, tree, rtx);
698 static machine_mode sparc_preferred_simd_mode (scalar_mode);
699 static reg_class_t sparc_preferred_reload_class (rtx x, reg_class_t rclass);
700 static bool sparc_print_operand_punct_valid_p (unsigned char);
701 static void sparc_print_operand (FILE *, rtx, int);
702 static void sparc_print_operand_address (FILE *, machine_mode, rtx);
703 static reg_class_t sparc_secondary_reload (bool, rtx, reg_class_t,
704 machine_mode,
705 secondary_reload_info *);
706 static bool sparc_secondary_memory_needed (machine_mode, reg_class_t,
707 reg_class_t);
708 static machine_mode sparc_secondary_memory_needed_mode (machine_mode);
709 static scalar_int_mode sparc_cstore_mode (enum insn_code icode);
710 static void sparc_atomic_assign_expand_fenv (tree *, tree *, tree *);
711 static bool sparc_fixed_condition_code_regs (unsigned int *, unsigned int *);
712 static unsigned int sparc_min_arithmetic_precision (void);
713 static unsigned int sparc_hard_regno_nregs (unsigned int, machine_mode);
714 static bool sparc_hard_regno_mode_ok (unsigned int, machine_mode);
715 static bool sparc_modes_tieable_p (machine_mode, machine_mode);
716 static bool sparc_can_change_mode_class (machine_mode, machine_mode,
717 reg_class_t);
718 static HOST_WIDE_INT sparc_constant_alignment (const_tree, HOST_WIDE_INT);
719 static bool sparc_vectorize_vec_perm_const (machine_mode, machine_mode,
720 rtx, rtx, rtx,
721 const vec_perm_indices &);
722 static opt_machine_mode sparc_get_mask_mode (machine_mode);
723 static bool sparc_can_follow_jump (const rtx_insn *, const rtx_insn *);
724 static HARD_REG_SET sparc_zero_call_used_regs (HARD_REG_SET);
725 static machine_mode sparc_c_mode_for_floating_type (enum tree_index);
726
727 #ifdef SUBTARGET_ATTRIBUTE_TABLE
728 /* Table of valid machine attributes. */
729 TARGET_GNU_ATTRIBUTES (sparc_attribute_table,
730 {
731 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
732 do_diagnostic, handler, exclude } */
733 SUBTARGET_ATTRIBUTE_TABLE
734 });
735 #endif
736
737 char sparc_hard_reg_printed[8];
738
739 /* Initialize the GCC target structure. */
740
741 /* The default is to use .half rather than .short for aligned HI objects. */
742 #undef TARGET_ASM_ALIGNED_HI_OP
743 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
744
745 #undef TARGET_ASM_UNALIGNED_HI_OP
746 #define TARGET_ASM_UNALIGNED_HI_OP "\t.uahalf\t"
747 #undef TARGET_ASM_UNALIGNED_SI_OP
748 #define TARGET_ASM_UNALIGNED_SI_OP "\t.uaword\t"
749 #undef TARGET_ASM_UNALIGNED_DI_OP
750 #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaxword\t"
751
752 /* The target hook has to handle DI-mode values. */
753 #undef TARGET_ASM_INTEGER
754 #define TARGET_ASM_INTEGER sparc_assemble_integer
755
756 #undef TARGET_ASM_FUNCTION_PROLOGUE
757 #define TARGET_ASM_FUNCTION_PROLOGUE sparc_asm_function_prologue
758 #undef TARGET_ASM_FUNCTION_EPILOGUE
759 #define TARGET_ASM_FUNCTION_EPILOGUE sparc_asm_function_epilogue
760
761 #undef TARGET_SCHED_ADJUST_COST
762 #define TARGET_SCHED_ADJUST_COST sparc_adjust_cost
763 #undef TARGET_SCHED_ISSUE_RATE
764 #define TARGET_SCHED_ISSUE_RATE sparc_issue_rate
765 #undef TARGET_SCHED_INIT
766 #define TARGET_SCHED_INIT sparc_sched_init
767 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
768 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD sparc_use_sched_lookahead
769
770 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
771 #define TARGET_FUNCTION_OK_FOR_SIBCALL sparc_function_ok_for_sibcall
772
773 #undef TARGET_INIT_LIBFUNCS
774 #define TARGET_INIT_LIBFUNCS sparc_init_libfuncs
775
776 #undef TARGET_LEGITIMIZE_ADDRESS
777 #define TARGET_LEGITIMIZE_ADDRESS sparc_legitimize_address
778 #undef TARGET_DELEGITIMIZE_ADDRESS
779 #define TARGET_DELEGITIMIZE_ADDRESS sparc_delegitimize_address
780 #undef TARGET_MODE_DEPENDENT_ADDRESS_P
781 #define TARGET_MODE_DEPENDENT_ADDRESS_P sparc_mode_dependent_address_p
782
783 #undef TARGET_INIT_BUILTINS
784 #define TARGET_INIT_BUILTINS sparc_init_builtins
785 #undef TARGET_BUILTIN_DECL
786 #define TARGET_BUILTIN_DECL sparc_builtin_decl
787 #undef TARGET_EXPAND_BUILTIN
788 #define TARGET_EXPAND_BUILTIN sparc_expand_builtin
789 #undef TARGET_FOLD_BUILTIN
790 #define TARGET_FOLD_BUILTIN sparc_fold_builtin
791
792 #if TARGET_TLS
793 #undef TARGET_HAVE_TLS
794 #define TARGET_HAVE_TLS true
795 #endif
796
797 #undef TARGET_CANNOT_FORCE_CONST_MEM
798 #define TARGET_CANNOT_FORCE_CONST_MEM sparc_cannot_force_const_mem
799
800 #undef TARGET_ASM_OUTPUT_MI_THUNK
801 #define TARGET_ASM_OUTPUT_MI_THUNK sparc_output_mi_thunk
802 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
803 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK sparc_can_output_mi_thunk
804
805 #undef TARGET_RTX_COSTS
806 #define TARGET_RTX_COSTS sparc_rtx_costs
807 #undef TARGET_ADDRESS_COST
808 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
809 #undef TARGET_REGISTER_MOVE_COST
810 #define TARGET_REGISTER_MOVE_COST sparc_register_move_cost
811
812 #undef TARGET_PROMOTE_FUNCTION_MODE
813 #define TARGET_PROMOTE_FUNCTION_MODE sparc_promote_function_mode
814 #undef TARGET_STRICT_ARGUMENT_NAMING
815 #define TARGET_STRICT_ARGUMENT_NAMING sparc_strict_argument_naming
816
817 #undef TARGET_MUST_PASS_IN_STACK
818 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
819 #undef TARGET_PASS_BY_REFERENCE
820 #define TARGET_PASS_BY_REFERENCE sparc_pass_by_reference
821 #undef TARGET_ARG_PARTIAL_BYTES
822 #define TARGET_ARG_PARTIAL_BYTES sparc_arg_partial_bytes
823 #undef TARGET_FUNCTION_ARG_ADVANCE
824 #define TARGET_FUNCTION_ARG_ADVANCE sparc_function_arg_advance
825 #undef TARGET_FUNCTION_ARG
826 #define TARGET_FUNCTION_ARG sparc_function_arg
827 #undef TARGET_FUNCTION_INCOMING_ARG
828 #define TARGET_FUNCTION_INCOMING_ARG sparc_function_incoming_arg
829 #undef TARGET_FUNCTION_ARG_PADDING
830 #define TARGET_FUNCTION_ARG_PADDING sparc_function_arg_padding
831 #undef TARGET_FUNCTION_ARG_BOUNDARY
832 #define TARGET_FUNCTION_ARG_BOUNDARY sparc_function_arg_boundary
833
834 #undef TARGET_RETURN_IN_MEMORY
835 #define TARGET_RETURN_IN_MEMORY sparc_return_in_memory
836 #undef TARGET_STRUCT_VALUE_RTX
837 #define TARGET_STRUCT_VALUE_RTX sparc_struct_value_rtx
838 #undef TARGET_FUNCTION_VALUE
839 #define TARGET_FUNCTION_VALUE sparc_function_value
840 #undef TARGET_LIBCALL_VALUE
841 #define TARGET_LIBCALL_VALUE sparc_libcall_value
842 #undef TARGET_FUNCTION_VALUE_REGNO_P
843 #define TARGET_FUNCTION_VALUE_REGNO_P sparc_function_value_regno_p
844
845 #undef TARGET_EXPAND_BUILTIN_SAVEREGS
846 #define TARGET_EXPAND_BUILTIN_SAVEREGS sparc_builtin_saveregs
847
848 #undef TARGET_ASAN_SHADOW_OFFSET
849 #define TARGET_ASAN_SHADOW_OFFSET sparc_asan_shadow_offset
850
851 #undef TARGET_EXPAND_BUILTIN_VA_START
852 #define TARGET_EXPAND_BUILTIN_VA_START sparc_va_start
853 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
854 #define TARGET_GIMPLIFY_VA_ARG_EXPR sparc_gimplify_va_arg
855
856 #undef TARGET_VECTOR_MODE_SUPPORTED_P
857 #define TARGET_VECTOR_MODE_SUPPORTED_P sparc_vector_mode_supported_p
858
859 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
860 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE sparc_preferred_simd_mode
861
862 #ifdef SUBTARGET_INSERT_ATTRIBUTES
863 #undef TARGET_INSERT_ATTRIBUTES
864 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
865 #endif
866
867 #ifdef SUBTARGET_ATTRIBUTE_TABLE
868 #undef TARGET_ATTRIBUTE_TABLE
869 #define TARGET_ATTRIBUTE_TABLE sparc_attribute_table
870 #endif
871
872 #undef TARGET_OPTION_OVERRIDE
873 #define TARGET_OPTION_OVERRIDE sparc_option_override
874
875 #ifdef TARGET_THREAD_SSP_OFFSET
876 #undef TARGET_STACK_PROTECT_GUARD
877 #define TARGET_STACK_PROTECT_GUARD hook_tree_void_null
878 #endif
879
880 #if TARGET_GNU_TLS && defined(HAVE_AS_SPARC_UA_PCREL)
881 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
882 #define TARGET_ASM_OUTPUT_DWARF_DTPREL sparc_output_dwarf_dtprel
883 #endif
884
885 #undef TARGET_OUTPUT_CFI_DIRECTIVE
886 #define TARGET_OUTPUT_CFI_DIRECTIVE sparc_output_cfi_directive
887
888 #undef TARGET_DW_CFI_OPRND1_DESC
889 #define TARGET_DW_CFI_OPRND1_DESC sparc_dw_cfi_oprnd1_desc
890
891 #undef TARGET_ASM_FILE_END
892 #define TARGET_ASM_FILE_END sparc_file_end
893
894 #undef TARGET_FRAME_POINTER_REQUIRED
895 #define TARGET_FRAME_POINTER_REQUIRED sparc_frame_pointer_required
896
897 #undef TARGET_CAN_ELIMINATE
898 #define TARGET_CAN_ELIMINATE sparc_can_eliminate
899
900 #undef TARGET_PREFERRED_RELOAD_CLASS
901 #define TARGET_PREFERRED_RELOAD_CLASS sparc_preferred_reload_class
902
903 #undef TARGET_SECONDARY_RELOAD
904 #define TARGET_SECONDARY_RELOAD sparc_secondary_reload
905 #undef TARGET_SECONDARY_MEMORY_NEEDED
906 #define TARGET_SECONDARY_MEMORY_NEEDED sparc_secondary_memory_needed
907 #undef TARGET_SECONDARY_MEMORY_NEEDED_MODE
908 #define TARGET_SECONDARY_MEMORY_NEEDED_MODE sparc_secondary_memory_needed_mode
909
910 #undef TARGET_CONDITIONAL_REGISTER_USAGE
911 #define TARGET_CONDITIONAL_REGISTER_USAGE sparc_conditional_register_usage
912
913 #undef TARGET_INIT_PIC_REG
914 #define TARGET_INIT_PIC_REG sparc_init_pic_reg
915
916 #undef TARGET_USE_PSEUDO_PIC_REG
917 #define TARGET_USE_PSEUDO_PIC_REG sparc_use_pseudo_pic_reg
918
919 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
920 #undef TARGET_MANGLE_TYPE
921 #define TARGET_MANGLE_TYPE sparc_mangle_type
922 #endif
923
924 #undef TARGET_LEGITIMATE_ADDRESS_P
925 #define TARGET_LEGITIMATE_ADDRESS_P sparc_legitimate_address_p
926
927 #undef TARGET_LEGITIMATE_CONSTANT_P
928 #define TARGET_LEGITIMATE_CONSTANT_P sparc_legitimate_constant_p
929
930 #undef TARGET_TRAMPOLINE_INIT
931 #define TARGET_TRAMPOLINE_INIT sparc_trampoline_init
932
933 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
934 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P sparc_print_operand_punct_valid_p
935 #undef TARGET_PRINT_OPERAND
936 #define TARGET_PRINT_OPERAND sparc_print_operand
937 #undef TARGET_PRINT_OPERAND_ADDRESS
938 #define TARGET_PRINT_OPERAND_ADDRESS sparc_print_operand_address
939
940 /* The value stored by LDSTUB. */
941 #undef TARGET_ATOMIC_TEST_AND_SET_TRUEVAL
942 #define TARGET_ATOMIC_TEST_AND_SET_TRUEVAL 0xff
943
944 #undef TARGET_CSTORE_MODE
945 #define TARGET_CSTORE_MODE sparc_cstore_mode
946
947 #undef TARGET_ATOMIC_ASSIGN_EXPAND_FENV
948 #define TARGET_ATOMIC_ASSIGN_EXPAND_FENV sparc_atomic_assign_expand_fenv
949
950 #undef TARGET_FIXED_CONDITION_CODE_REGS
951 #define TARGET_FIXED_CONDITION_CODE_REGS sparc_fixed_condition_code_regs
952
953 #undef TARGET_MIN_ARITHMETIC_PRECISION
954 #define TARGET_MIN_ARITHMETIC_PRECISION sparc_min_arithmetic_precision
955
956 #undef TARGET_CUSTOM_FUNCTION_DESCRIPTORS
957 #define TARGET_CUSTOM_FUNCTION_DESCRIPTORS 1
958
959 #undef TARGET_HARD_REGNO_NREGS
960 #define TARGET_HARD_REGNO_NREGS sparc_hard_regno_nregs
961 #undef TARGET_HARD_REGNO_MODE_OK
962 #define TARGET_HARD_REGNO_MODE_OK sparc_hard_regno_mode_ok
963
964 #undef TARGET_MODES_TIEABLE_P
965 #define TARGET_MODES_TIEABLE_P sparc_modes_tieable_p
966
967 #undef TARGET_CAN_CHANGE_MODE_CLASS
968 #define TARGET_CAN_CHANGE_MODE_CLASS sparc_can_change_mode_class
969
970 #undef TARGET_CONSTANT_ALIGNMENT
971 #define TARGET_CONSTANT_ALIGNMENT sparc_constant_alignment
972
973 #undef TARGET_VECTORIZE_VEC_PERM_CONST
974 #define TARGET_VECTORIZE_VEC_PERM_CONST sparc_vectorize_vec_perm_const
975
976 #undef TARGET_VECTORIZE_GET_MASK_MODE
977 #define TARGET_VECTORIZE_GET_MASK_MODE sparc_get_mask_mode
978
979 #undef TARGET_CAN_FOLLOW_JUMP
980 #define TARGET_CAN_FOLLOW_JUMP sparc_can_follow_jump
981
982 #undef TARGET_ZERO_CALL_USED_REGS
983 #define TARGET_ZERO_CALL_USED_REGS sparc_zero_call_used_regs
984
985 #undef TARGET_C_MODE_FOR_FLOATING_TYPE
986 #define TARGET_C_MODE_FOR_FLOATING_TYPE sparc_c_mode_for_floating_type
987
988 struct gcc_target targetm = TARGET_INITIALIZER;
989
990 /* Return the memory reference contained in X if any, zero otherwise. */
991
992 static rtx
993 mem_ref (rtx x)
994 {
995 if (GET_CODE (x) == SIGN_EXTEND || GET_CODE (x) == ZERO_EXTEND)
996 x = XEXP (x, 0);
997
998 if (MEM_P (x))
999 return x;
1000
1001 return NULL_RTX;
1002 }
1003
1004 /* True if any of INSN's source register(s) is REG. */
1005
1006 static bool
1007 insn_uses_reg_p (rtx_insn *insn, unsigned int reg)
1008 {
1009 extract_insn (insn);
1010 return ((REG_P (recog_data.operand[1])
1011 && REGNO (recog_data.operand[1]) == reg)
1012 || (recog_data.n_operands == 3
1013 && REG_P (recog_data.operand[2])
1014 && REGNO (recog_data.operand[2]) == reg));
1015 }
1016
1017 /* True if INSN is a floating-point division or square-root. */
1018
1019 static bool
1020 div_sqrt_insn_p (rtx_insn *insn)
1021 {
1022 if (GET_CODE (PATTERN (insn)) != SET)
1023 return false;
1024
1025 switch (get_attr_type (insn))
1026 {
1027 case TYPE_FPDIVS:
1028 case TYPE_FPSQRTS:
1029 case TYPE_FPDIVD:
1030 case TYPE_FPSQRTD:
1031 return true;
1032 default:
1033 return false;
1034 }
1035 }
1036
1037 /* True if INSN is a floating-point instruction. */
1038
1039 static bool
1040 fpop_insn_p (rtx_insn *insn)
1041 {
1042 if (GET_CODE (PATTERN (insn)) != SET)
1043 return false;
1044
1045 switch (get_attr_type (insn))
1046 {
1047 case TYPE_FPMOVE:
1048 case TYPE_FPCMOVE:
1049 case TYPE_FP:
1050 case TYPE_FPCMP:
1051 case TYPE_FPMUL:
1052 case TYPE_FPDIVS:
1053 case TYPE_FPSQRTS:
1054 case TYPE_FPDIVD:
1055 case TYPE_FPSQRTD:
1056 return true;
1057 default:
1058 return false;
1059 }
1060 }
1061
1062 /* True if INSN is an atomic instruction. */
1063
1064 static bool
1065 atomic_insn_for_leon3_p (rtx_insn *insn)
1066 {
1067 switch (INSN_CODE (insn))
1068 {
1069 case CODE_FOR_membar_storeload:
1070 case CODE_FOR_swapsi:
1071 case CODE_FOR_ldstub:
1072 case CODE_FOR_atomic_compare_and_swap_leon3_1:
1073 return true;
1074 default:
1075 return false;
1076 }
1077 }
1078
1079 /* True if INSN is a store instruction. */
1080
1081 static bool
1082 store_insn_p (rtx_insn *insn)
1083 {
1084 if (GET_CODE (PATTERN (insn)) != SET)
1085 return false;
1086
1087 switch (get_attr_type (insn))
1088 {
1089 case TYPE_STORE:
1090 case TYPE_FPSTORE:
1091 return true;
1092 default:
1093 return false;
1094 }
1095 }
1096
1097 /* True if INSN is a load instruction. */
1098
1099 static bool
1100 load_insn_p (rtx_insn *insn)
1101 {
1102 if (GET_CODE (PATTERN (insn)) != SET)
1103 return false;
1104
1105 switch (get_attr_type (insn))
1106 {
1107 case TYPE_LOAD:
1108 case TYPE_SLOAD:
1109 case TYPE_FPLOAD:
1110 return true;
1111 default:
1112 return false;
1113 }
1114 }
1115
1116 /* We use a machine specific pass to enable workarounds for errata.
1117
1118 We need to have the (essentially) final form of the insn stream in order
1119 to properly detect the various hazards. Therefore, this machine specific
1120 pass runs as late as possible. */
1121
1122 /* True if INSN is a md pattern or asm statement. */
1123 #define USEFUL_INSN_P(INSN) \
1124 (NONDEBUG_INSN_P (INSN) \
1125 && GET_CODE (PATTERN (INSN)) != USE \
1126 && GET_CODE (PATTERN (INSN)) != CLOBBER)
1127
1128 rtx_insn *
1129 next_active_non_empty_insn (rtx_insn *insn)
1130 {
1131 insn = next_active_insn (insn);
1132
1133 while (insn
1134 && (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
1135 || GET_CODE (PATTERN (insn)) == ASM_INPUT
1136 || get_attr_length (insn) == 0
1137 || (USEFUL_INSN_P (insn)
1138 && (asm_noperands (PATTERN (insn)) >= 0)
1139 && !strcmp (decode_asm_operands (PATTERN (insn),
1140 NULL, NULL, NULL,
1141 NULL, NULL), ""))))
1142 insn = next_active_insn (insn);
1143
1144 return insn;
1145 }
1146
1147 static unsigned int
1148 sparc_do_work_around_errata (void)
1149 {
1150 rtx_insn *insn, *next;
1151 bool find_first_useful = true;
1152
1153 /* Force all instructions to be split into their final form. */
1154 split_all_insns_noflow ();
1155
1156 /* Now look for specific patterns in the insn stream. */
1157 for (insn = get_insns (); insn; insn = next)
1158 {
1159 bool insert_nop = false;
1160 rtx set;
1161 rtx_insn *jump;
1162 rtx_sequence *seq;
1163
1164 /* Look into the instruction in a delay slot. */
1165 if (NONJUMP_INSN_P (insn)
1166 && (seq = dyn_cast <rtx_sequence *> (PATTERN (insn))))
1167 {
1168 jump = seq->insn (0);
1169 insn = seq->insn (1);
1170 }
1171 else if (JUMP_P (insn))
1172 jump = insn;
1173 else
1174 jump = NULL;
1175
1176 /* Do not begin function with atomic instruction. */
1177 if (sparc_fix_ut700
1178 && find_first_useful
1179 && USEFUL_INSN_P (insn))
1180 {
1181 find_first_useful = false;
1182 if (atomic_insn_for_leon3_p (insn))
1183 emit_insn_before (gen_nop (), insn);
1184 }
1185
1186 /* Place a NOP at the branch target of an integer branch if it is a
1187 floating-point operation or a floating-point branch. */
1188 if (sparc_fix_gr712rc
1189 && jump
1190 && jump_to_label_p (jump)
1191 && get_attr_branch_type (jump) == BRANCH_TYPE_ICC)
1192 {
1193 rtx_insn *target = next_active_insn (JUMP_LABEL_AS_INSN (jump));
1194 if (target
1195 && (fpop_insn_p (target)
1196 || (JUMP_P (target)
1197 && get_attr_branch_type (target) == BRANCH_TYPE_FCC)))
1198 emit_insn_before (gen_nop (), target);
1199 }
1200
1201 /* Insert a NOP between load instruction and atomic instruction. Insert
1202 a NOP at branch target if there is a load in delay slot and an atomic
1203 instruction at branch target. */
1204 if (sparc_fix_ut700
1205 && NONJUMP_INSN_P (insn)
1206 && load_insn_p (insn))
1207 {
1208 if (jump && jump_to_label_p (jump))
1209 {
1210 rtx_insn *target = next_active_insn (JUMP_LABEL_AS_INSN (jump));
1211 if (target && atomic_insn_for_leon3_p (target))
1212 emit_insn_before (gen_nop (), target);
1213 }
1214
1215 next = next_active_non_empty_insn (insn);
1216 if (!next)
1217 break;
1218
1219 if (atomic_insn_for_leon3_p (next))
1220 insert_nop = true;
1221 }
1222
1223 /* Look for a sequence that starts with a fdiv or fsqrt instruction and
1224 ends with another fdiv or fsqrt instruction with no dependencies on
1225 the former, along with an appropriate pattern in between. */
1226 if (sparc_fix_lost_divsqrt
1227 && NONJUMP_INSN_P (insn)
1228 && div_sqrt_insn_p (insn))
1229 {
1230 int i;
1231 int fp_found = 0;
1232 rtx_insn *after;
1233
1234 const unsigned int dest_reg = REGNO (SET_DEST (single_set (insn)));
1235
1236 next = next_active_insn (insn);
1237 if (!next)
1238 break;
1239
1240 for (after = next, i = 0; i < 4; i++)
1241 {
1242 /* Count floating-point operations. */
1243 if (i != 3 && fpop_insn_p (after))
1244 {
1245 /* If the insn uses the destination register of
1246 the div/sqrt, then it cannot be problematic. */
1247 if (insn_uses_reg_p (after, dest_reg))
1248 break;
1249 fp_found++;
1250 }
1251
1252 /* Count floating-point loads. */
1253 if (i != 3
1254 && (set = single_set (after)) != NULL_RTX
1255 && REG_P (SET_DEST (set))
1256 && REGNO (SET_DEST (set)) > 31)
1257 {
1258 /* If the insn uses the destination register of
1259 the div/sqrt, then it cannot be problematic. */
1260 if (REGNO (SET_DEST (set)) == dest_reg)
1261 break;
1262 fp_found++;
1263 }
1264
1265 /* Check if this is a problematic sequence. */
1266 if (i > 1
1267 && fp_found >= 2
1268 && div_sqrt_insn_p (after))
1269 {
1270 /* If this is the short version of the problematic
1271 sequence we add two NOPs in a row to also prevent
1272 the long version. */
1273 if (i == 2)
1274 emit_insn_before (gen_nop (), next);
1275 insert_nop = true;
1276 break;
1277 }
1278
1279 /* No need to scan past a second div/sqrt. */
1280 if (div_sqrt_insn_p (after))
1281 break;
1282
1283 /* Insert NOP before branch. */
1284 if (i < 3
1285 && (!NONJUMP_INSN_P (after)
1286 || GET_CODE (PATTERN (after)) == SEQUENCE))
1287 {
1288 insert_nop = true;
1289 break;
1290 }
1291
1292 after = next_active_insn (after);
1293 if (!after)
1294 break;
1295 }
1296 }
1297
1298 /* Look for either of these two sequences:
1299
1300 Sequence A:
1301 1. store of word size or less (e.g. st / stb / sth / stf)
1302 2. any single instruction that is not a load or store
1303 3. any store instruction (e.g. st / stb / sth / stf / std / stdf)
1304
1305 Sequence B:
1306 1. store of double word size (e.g. std / stdf)
1307 2. any store instruction (e.g. st / stb / sth / stf / std / stdf) */
1308 if (sparc_fix_b2bst
1309 && NONJUMP_INSN_P (insn)
1310 && (set = single_set (insn)) != NULL_RTX
1311 && store_insn_p (insn))
1312 {
1313 /* Sequence B begins with a double-word store. */
1314 bool seq_b = GET_MODE_SIZE (GET_MODE (SET_DEST (set))) == 8;
1315 rtx_insn *after;
1316 int i;
1317
1318 next = next_active_non_empty_insn (insn);
1319 if (!next)
1320 break;
1321
1322 for (after = next, i = 0; i < 2; i++)
1323 {
1324 /* If the insn is a branch, then it cannot be problematic. */
1325 if (!NONJUMP_INSN_P (after)
1326 || GET_CODE (PATTERN (after)) == SEQUENCE)
1327 break;
1328
1329 /* Sequence B is only two instructions long. */
1330 if (seq_b)
1331 {
1332 /* Add NOP if followed by a store. */
1333 if (store_insn_p (after))
1334 insert_nop = true;
1335
1336 /* Otherwise it is ok. */
1337 break;
1338 }
1339
1340 /* If the second instruction is a load or a store,
1341 then the sequence cannot be problematic. */
1342 if (i == 0)
1343 {
1344 if ((set = single_set (after)) != NULL_RTX
1345 && (MEM_P (SET_DEST (set)) || mem_ref (SET_SRC (set))))
1346 break;
1347
1348 after = next_active_non_empty_insn (after);
1349 if (!after)
1350 break;
1351 }
1352
1353 /* Add NOP if third instruction is a store. */
1354 if (i == 1
1355 && store_insn_p (after))
1356 insert_nop = true;
1357 }
1358 }
1359
1360 /* Look for a single-word load into an odd-numbered FP register. */
1361 else if (sparc_fix_at697f
1362 && NONJUMP_INSN_P (insn)
1363 && (set = single_set (insn)) != NULL_RTX
1364 && GET_MODE_SIZE (GET_MODE (SET_SRC (set))) == 4
1365 && mem_ref (SET_SRC (set))
1366 && REG_P (SET_DEST (set))
1367 && REGNO (SET_DEST (set)) > 31
1368 && REGNO (SET_DEST (set)) % 2 != 0)
1369 {
1370 /* The wrong dependency is on the enclosing double register. */
1371 const unsigned int x = REGNO (SET_DEST (set)) - 1;
1372 unsigned int src1, src2, dest;
1373 int code;
1374
1375 next = next_active_insn (insn);
1376 if (!next)
1377 break;
1378 /* If the insn is a branch, then it cannot be problematic. */
1379 if (!NONJUMP_INSN_P (next) || GET_CODE (PATTERN (next)) == SEQUENCE)
1380 continue;
1381
1382 extract_insn (next);
1383 code = INSN_CODE (next);
1384
1385 switch (code)
1386 {
1387 case CODE_FOR_adddf3:
1388 case CODE_FOR_subdf3:
1389 case CODE_FOR_muldf3:
1390 case CODE_FOR_divdf3:
1391 dest = REGNO (recog_data.operand[0]);
1392 src1 = REGNO (recog_data.operand[1]);
1393 src2 = REGNO (recog_data.operand[2]);
1394 if (src1 != src2)
1395 {
1396 /* Case [1-4]:
1397 ld [address], %fx+1
1398 FPOPd %f{x,y}, %f{y,x}, %f{x,y} */
1399 if ((src1 == x || src2 == x)
1400 && (dest == src1 || dest == src2))
1401 insert_nop = true;
1402 }
1403 else
1404 {
1405 /* Case 5:
1406 ld [address], %fx+1
1407 FPOPd %fx, %fx, %fx */
1408 if (src1 == x
1409 && dest == src1
1410 && (code == CODE_FOR_adddf3 || code == CODE_FOR_muldf3))
1411 insert_nop = true;
1412 }
1413 break;
1414
1415 case CODE_FOR_sqrtdf2:
1416 dest = REGNO (recog_data.operand[0]);
1417 src1 = REGNO (recog_data.operand[1]);
1418 /* Case 6:
1419 ld [address], %fx+1
1420 fsqrtd %fx, %fx */
1421 if (src1 == x && dest == src1)
1422 insert_nop = true;
1423 break;
1424
1425 default:
1426 break;
1427 }
1428 }
1429
1430 /* Look for a single-word load into an integer register. */
1431 else if (sparc_fix_ut699
1432 && NONJUMP_INSN_P (insn)
1433 && (set = single_set (insn)) != NULL_RTX
1434 && GET_MODE_SIZE (GET_MODE (SET_SRC (set))) <= 4
1435 && (mem_ref (SET_SRC (set)) != NULL_RTX
1436 || INSN_CODE (insn) == CODE_FOR_movsi_pic_gotdata_op)
1437 && REG_P (SET_DEST (set))
1438 && REGNO (SET_DEST (set)) < 32)
1439 {
1440 /* There is no problem if the second memory access has a data
1441 dependency on the first single-cycle load. */
1442 rtx x = SET_DEST (set);
1443
1444 next = next_active_insn (insn);
1445 if (!next)
1446 break;
1447 /* If the insn is a branch, then it cannot be problematic. */
1448 if (!NONJUMP_INSN_P (next) || GET_CODE (PATTERN (next)) == SEQUENCE)
1449 continue;
1450
1451 /* Look for a second memory access to/from an integer register. */
1452 if ((set = single_set (next)) != NULL_RTX)
1453 {
1454 rtx src = SET_SRC (set);
1455 rtx dest = SET_DEST (set);
1456 rtx mem;
1457
1458 /* LDD is affected. */
1459 if ((mem = mem_ref (src)) != NULL_RTX
1460 && REG_P (dest)
1461 && REGNO (dest) < 32
1462 && !reg_mentioned_p (x, XEXP (mem, 0)))
1463 insert_nop = true;
1464
1465 /* STD is *not* affected. */
1466 else if (MEM_P (dest)
1467 && GET_MODE_SIZE (GET_MODE (dest)) <= 4
1468 && (src == CONST0_RTX (GET_MODE (dest))
1469 || (REG_P (src)
1470 && REGNO (src) < 32
1471 && REGNO (src) != REGNO (x)))
1472 && !reg_mentioned_p (x, XEXP (dest, 0)))
1473 insert_nop = true;
1474
1475 /* GOT accesses uses LD. */
1476 else if (INSN_CODE (next) == CODE_FOR_movsi_pic_gotdata_op
1477 && !reg_mentioned_p (x, XEXP (XEXP (src, 0), 1)))
1478 insert_nop = true;
1479 }
1480 }
1481
1482 /* Look for a single-word load/operation into an FP register. */
1483 else if (sparc_fix_ut699
1484 && NONJUMP_INSN_P (insn)
1485 && (set = single_set (insn)) != NULL_RTX
1486 && GET_MODE_SIZE (GET_MODE (SET_SRC (set))) == 4
1487 && REG_P (SET_DEST (set))
1488 && REGNO (SET_DEST (set)) > 31)
1489 {
1490 /* Number of instructions in the problematic window. */
1491 const int n_insns = 4;
1492 /* The problematic combination is with the sibling FP register. */
1493 const unsigned int x = REGNO (SET_DEST (set));
1494 const unsigned int y = x ^ 1;
1495 rtx_insn *after;
1496 int i;
1497
1498 next = next_active_insn (insn);
1499 if (!next)
1500 break;
1501 /* If the insn is a branch, then it cannot be problematic. */
1502 if (!NONJUMP_INSN_P (next) || GET_CODE (PATTERN (next)) == SEQUENCE)
1503 continue;
1504
1505 /* Look for a second load/operation into the sibling FP register. */
1506 if (!((set = single_set (next)) != NULL_RTX
1507 && GET_MODE_SIZE (GET_MODE (SET_SRC (set))) == 4
1508 && REG_P (SET_DEST (set))
1509 && REGNO (SET_DEST (set)) == y))
1510 continue;
1511
1512 /* Look for a (possible) store from the FP register in the next N
1513 instructions, but bail out if it is again modified or if there
1514 is a store from the sibling FP register before this store. */
1515 for (after = next, i = 0; i < n_insns; i++)
1516 {
1517 bool branch_p;
1518
1519 after = next_active_insn (after);
1520 if (!after)
1521 break;
1522
1523 /* This is a branch with an empty delay slot. */
1524 if (!NONJUMP_INSN_P (after))
1525 {
1526 if (++i == n_insns)
1527 break;
1528 branch_p = true;
1529 after = NULL;
1530 }
1531 /* This is a branch with a filled delay slot. */
1532 else if (rtx_sequence *seq =
1533 dyn_cast <rtx_sequence *> (PATTERN (after)))
1534 {
1535 if (++i == n_insns)
1536 break;
1537 branch_p = true;
1538 after = seq->insn (1);
1539 }
1540 /* This is a regular instruction. */
1541 else
1542 branch_p = false;
1543
1544 if (after && (set = single_set (after)) != NULL_RTX)
1545 {
1546 const rtx src = SET_SRC (set);
1547 const rtx dest = SET_DEST (set);
1548 const unsigned int size = GET_MODE_SIZE (GET_MODE (dest));
1549
1550 /* If the FP register is again modified before the store,
1551 then the store isn't affected. */
1552 if (REG_P (dest)
1553 && (REGNO (dest) == x
1554 || (REGNO (dest) == y && size == 8)))
1555 break;
1556
1557 if (MEM_P (dest) && REG_P (src))
1558 {
1559 /* If there is a store from the sibling FP register
1560 before the store, then the store is not affected. */
1561 if (REGNO (src) == y || (REGNO (src) == x && size == 8))
1562 break;
1563
1564 /* Otherwise, the store is affected. */
1565 if (REGNO (src) == x && size == 4)
1566 {
1567 insert_nop = true;
1568 break;
1569 }
1570 }
1571 }
1572
1573 /* If we have a branch in the first M instructions, then we
1574 cannot see the (M+2)th instruction so we play safe. */
1575 if (branch_p && i <= (n_insns - 2))
1576 {
1577 insert_nop = true;
1578 break;
1579 }
1580 }
1581 }
1582
1583 else
1584 next = NEXT_INSN (insn);
1585
1586 if (insert_nop)
1587 emit_insn_before (gen_nop (), next);
1588 }
1589
1590 return 0;
1591 }
1592
1593 namespace {
1594
1595 const pass_data pass_data_work_around_errata =
1596 {
1597 RTL_PASS, /* type */
1598 "errata", /* name */
1599 OPTGROUP_NONE, /* optinfo_flags */
1600 TV_MACH_DEP, /* tv_id */
1601 0, /* properties_required */
1602 0, /* properties_provided */
1603 0, /* properties_destroyed */
1604 0, /* todo_flags_start */
1605 0, /* todo_flags_finish */
1606 };
1607
1608 class pass_work_around_errata : public rtl_opt_pass
1609 {
1610 public:
1611 pass_work_around_errata(gcc::context *ctxt)
1612 : rtl_opt_pass(pass_data_work_around_errata, ctxt)
1613 {}
1614
1615 /* opt_pass methods: */
1616 virtual bool gate (function *)
1617 {
1618 return sparc_fix_at697f
1619 || sparc_fix_ut699 || sparc_fix_ut700 || sparc_fix_gr712rc
1620 || sparc_fix_b2bst || sparc_fix_lost_divsqrt;
1621 }
1622
1623 virtual unsigned int execute (function *)
1624 {
1625 return sparc_do_work_around_errata ();
1626 }
1627
1628 }; // class pass_work_around_errata
1629
1630 } // anon namespace
1631
1632 rtl_opt_pass *
1633 make_pass_work_around_errata (gcc::context *ctxt)
1634 {
1635 return new pass_work_around_errata (ctxt);
1636 }
1637
1638 /* Helpers for TARGET_DEBUG_OPTIONS. */
1639 static void
1640 dump_target_flag_bits (const int flags)
1641 {
1642 if (flags & MASK_64BIT)
1643 fprintf (stderr, "64BIT ");
1644 if (flags & MASK_APP_REGS)
1645 fprintf (stderr, "APP_REGS ");
1646 if (flags & MASK_FASTER_STRUCTS)
1647 fprintf (stderr, "FASTER_STRUCTS ");
1648 if (flags & MASK_FLAT)
1649 fprintf (stderr, "FLAT ");
1650 if (flags & MASK_FMAF)
1651 fprintf (stderr, "FMAF ");
1652 if (flags & MASK_FSMULD)
1653 fprintf (stderr, "FSMULD ");
1654 if (flags & MASK_FPU)
1655 fprintf (stderr, "FPU ");
1656 if (flags & MASK_HARD_QUAD)
1657 fprintf (stderr, "HARD_QUAD ");
1658 if (flags & MASK_POPC)
1659 fprintf (stderr, "POPC ");
1660 if (flags & MASK_PTR64)
1661 fprintf (stderr, "PTR64 ");
1662 if (flags & MASK_STACK_BIAS)
1663 fprintf (stderr, "STACK_BIAS ");
1664 if (flags & MASK_UNALIGNED_DOUBLES)
1665 fprintf (stderr, "UNALIGNED_DOUBLES ");
1666 if (flags & MASK_V8PLUS)
1667 fprintf (stderr, "V8PLUS ");
1668 if (flags & MASK_VIS)
1669 fprintf (stderr, "VIS ");
1670 if (flags & MASK_VIS2)
1671 fprintf (stderr, "VIS2 ");
1672 if (flags & MASK_VIS3)
1673 fprintf (stderr, "VIS3 ");
1674 if (flags & MASK_VIS3B)
1675 fprintf (stderr, "VIS3B ");
1676 if (flags & MASK_VIS4)
1677 fprintf (stderr, "VIS4 ");
1678 if (flags & MASK_VIS4B)
1679 fprintf (stderr, "VIS4B ");
1680 if (flags & MASK_CBCOND)
1681 fprintf (stderr, "CBCOND ");
1682 if (flags & MASK_DEPRECATED_V8_INSNS)
1683 fprintf (stderr, "DEPRECATED_V8_INSNS ");
1684 if (flags & MASK_LEON)
1685 fprintf (stderr, "LEON ");
1686 if (flags & MASK_LEON3)
1687 fprintf (stderr, "LEON3 ");
1688 if (flags & MASK_SPARCLET)
1689 fprintf (stderr, "SPARCLET ");
1690 if (flags & MASK_SPARCLITE)
1691 fprintf (stderr, "SPARCLITE ");
1692 if (flags & MASK_V8)
1693 fprintf (stderr, "V8 ");
1694 if (flags & MASK_V9)
1695 fprintf (stderr, "V9 ");
1696 }
1697
1698 static void
1699 dump_target_flags (const char *prefix, const int flags)
1700 {
1701 fprintf (stderr, "%s: (%08x) [ ", prefix, flags);
1702 dump_target_flag_bits (flags);
1703 fprintf(stderr, "]\n");
1704 }
1705
1706 /* Validate and override various options, and do some machine dependent
1707 initialization. */
1708
1709 static void
1710 sparc_option_override (void)
1711 {
1712 /* Map TARGET_CPU_DEFAULT to value for -m{cpu,tune}=. */
1713 static struct cpu_default {
1714 const int cpu;
1715 const enum sparc_processor_type processor;
1716 } const cpu_default[] = {
1717 /* There must be one entry here for each TARGET_CPU value. */
1718 { TARGET_CPU_sparc, PROCESSOR_CYPRESS },
1719 { TARGET_CPU_v8, PROCESSOR_V8 },
1720 { TARGET_CPU_supersparc, PROCESSOR_SUPERSPARC },
1721 { TARGET_CPU_hypersparc, PROCESSOR_HYPERSPARC },
1722 { TARGET_CPU_leon, PROCESSOR_LEON },
1723 { TARGET_CPU_leon3, PROCESSOR_LEON3 },
1724 { TARGET_CPU_leon5, PROCESSOR_LEON5 },
1725 { TARGET_CPU_leon3v7, PROCESSOR_LEON3V7 },
1726 { TARGET_CPU_sparclite, PROCESSOR_F930 },
1727 { TARGET_CPU_sparclite86x, PROCESSOR_SPARCLITE86X },
1728 { TARGET_CPU_sparclet, PROCESSOR_TSC701 },
1729 { TARGET_CPU_v9, PROCESSOR_V9 },
1730 { TARGET_CPU_ultrasparc, PROCESSOR_ULTRASPARC },
1731 { TARGET_CPU_ultrasparc3, PROCESSOR_ULTRASPARC3 },
1732 { TARGET_CPU_niagara, PROCESSOR_NIAGARA },
1733 { TARGET_CPU_niagara2, PROCESSOR_NIAGARA2 },
1734 { TARGET_CPU_niagara3, PROCESSOR_NIAGARA3 },
1735 { TARGET_CPU_niagara4, PROCESSOR_NIAGARA4 },
1736 { TARGET_CPU_niagara7, PROCESSOR_NIAGARA7 },
1737 { TARGET_CPU_m8, PROCESSOR_M8 },
1738 { -1, PROCESSOR_V7 }
1739 };
1740 const struct cpu_default *def;
1741 /* Table of values for -m{cpu,tune}=. This must match the order of
1742 the enum processor_type in sparc-opts.h. */
1743 static struct cpu_table {
1744 const char *const name;
1745 const int disable;
1746 const int enable;
1747 } const cpu_table[] = {
1748 { "v7", MASK_ISA, 0 },
1749 { "cypress", MASK_ISA, 0 },
1750 { "v8", MASK_ISA, MASK_V8 },
1751 /* TI TMS390Z55 supersparc */
1752 { "supersparc", MASK_ISA, MASK_V8 },
1753 { "hypersparc", MASK_ISA, MASK_V8 },
1754 { "leon", MASK_ISA|MASK_FSMULD, MASK_V8|MASK_LEON },
1755 { "leon3", MASK_ISA, MASK_V8|MASK_LEON3 },
1756 { "leon5", MASK_ISA, MASK_V8|MASK_LEON3 },
1757 { "leon3v7", MASK_ISA, MASK_LEON3 },
1758 { "sparclite", MASK_ISA, MASK_SPARCLITE },
1759 /* The Fujitsu MB86930 is the original sparclite chip, with no FPU. */
1760 { "f930", MASK_ISA|MASK_FPU, MASK_SPARCLITE },
1761 /* The Fujitsu MB86934 is the recent sparclite chip, with an FPU. */
1762 { "f934", MASK_ISA, MASK_SPARCLITE },
1763 { "sparclite86x", MASK_ISA|MASK_FPU, MASK_SPARCLITE },
1764 { "sparclet", MASK_ISA, MASK_SPARCLET },
1765 /* TEMIC sparclet */
1766 { "tsc701", MASK_ISA, MASK_SPARCLET },
1767 { "v9", MASK_ISA, MASK_V9 },
1768 /* UltraSPARC I, II, IIi */
1769 { "ultrasparc", MASK_ISA,
1770 /* Although insns using %y are deprecated, it is a clear win. */
1771 MASK_V9|MASK_DEPRECATED_V8_INSNS },
1772 /* UltraSPARC III */
1773 /* ??? Check if %y issue still holds true. */
1774 { "ultrasparc3", MASK_ISA,
1775 MASK_V9|MASK_DEPRECATED_V8_INSNS|MASK_VIS2 },
1776 /* UltraSPARC T1 */
1777 { "niagara", MASK_ISA,
1778 MASK_V9|MASK_DEPRECATED_V8_INSNS },
1779 /* UltraSPARC T2 */
1780 { "niagara2", MASK_ISA,
1781 MASK_V9|MASK_POPC|MASK_VIS2 },
1782 /* UltraSPARC T3 */
1783 { "niagara3", MASK_ISA,
1784 MASK_V9|MASK_POPC|MASK_VIS3|MASK_FMAF },
1785 /* UltraSPARC T4 */
1786 { "niagara4", MASK_ISA,
1787 MASK_V9|MASK_POPC|MASK_VIS3|MASK_FMAF|MASK_CBCOND },
1788 /* UltraSPARC M7 */
1789 { "niagara7", MASK_ISA,
1790 MASK_V9|MASK_POPC|MASK_VIS4|MASK_FMAF|MASK_CBCOND|MASK_SUBXC },
1791 /* UltraSPARC M8 */
1792 { "m8", MASK_ISA,
1793 MASK_V9|MASK_POPC|MASK_VIS4B|MASK_FMAF|MASK_CBCOND|MASK_SUBXC }
1794 };
1795 const struct cpu_table *cpu;
1796 unsigned int i;
1797
1798 if (sparc_debug_string != NULL)
1799 {
1800 const char *q;
1801 char *p;
1802
1803 p = ASTRDUP (sparc_debug_string);
1804 while ((q = strtok (p, ",")) != NULL)
1805 {
1806 bool invert;
1807 int mask;
1808
1809 p = NULL;
1810 if (*q == '!')
1811 {
1812 invert = true;
1813 q++;
1814 }
1815 else
1816 invert = false;
1817
1818 if (! strcmp (q, "all"))
1819 mask = MASK_DEBUG_ALL;
1820 else if (! strcmp (q, "options"))
1821 mask = MASK_DEBUG_OPTIONS;
1822 else
1823 error ("unknown %<-mdebug-%s%> switch", q);
1824
1825 if (invert)
1826 sparc_debug &= ~mask;
1827 else
1828 sparc_debug |= mask;
1829 }
1830 }
1831
1832 /* Enable the FsMULd instruction by default if not explicitly specified by
1833 the user. It may be later disabled by the CPU (explicitly or not). */
1834 if (TARGET_FPU && !(target_flags_explicit & MASK_FSMULD))
1835 target_flags |= MASK_FSMULD;
1836
1837 if (TARGET_DEBUG_OPTIONS)
1838 {
1839 dump_target_flags("Initial target_flags", target_flags);
1840 dump_target_flags("target_flags_explicit", target_flags_explicit);
1841 }
1842
1843 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1844 SUBTARGET_OVERRIDE_OPTIONS;
1845 #endif
1846
1847 #ifndef SPARC_BI_ARCH
1848 /* Check for unsupported architecture size. */
1849 if (!TARGET_64BIT != DEFAULT_ARCH32_P)
1850 error ("%s is not supported by this configuration",
1851 DEFAULT_ARCH32_P ? "-m64" : "-m32");
1852 #endif
1853
1854 /* We force all 64bit archs to use 128 bit long double */
1855 if (TARGET_ARCH64 && !TARGET_LONG_DOUBLE_128)
1856 {
1857 error ("%<-mlong-double-64%> not allowed with %<-m64%>");
1858 target_flags |= MASK_LONG_DOUBLE_128;
1859 }
1860
1861 /* Check that -fcall-saved-REG wasn't specified for out registers. */
1862 for (i = 8; i < 16; i++)
1863 if (!call_used_regs [i])
1864 {
1865 error ("%<-fcall-saved-REG%> is not supported for out registers");
1866 call_used_regs [i] = 1;
1867 }
1868
1869 /* Set the default CPU if no -mcpu option was specified. */
1870 if (!OPTION_SET_P (sparc_cpu_and_features))
1871 {
1872 for (def = &cpu_default[0]; def->cpu != -1; ++def)
1873 if (def->cpu == TARGET_CPU_DEFAULT)
1874 break;
1875 gcc_assert (def->cpu != -1);
1876 sparc_cpu_and_features = def->processor;
1877 }
1878
1879 /* Set the default CPU if no -mtune option was specified. */
1880 if (!OPTION_SET_P (sparc_cpu))
1881 sparc_cpu = sparc_cpu_and_features;
1882
1883 cpu = &cpu_table[(int) sparc_cpu_and_features];
1884
1885 if (TARGET_DEBUG_OPTIONS)
1886 {
1887 fprintf (stderr, "sparc_cpu_and_features: %s\n", cpu->name);
1888 dump_target_flags ("cpu->disable", cpu->disable);
1889 dump_target_flags ("cpu->enable", cpu->enable);
1890 }
1891
1892 target_flags &= ~cpu->disable;
1893 target_flags |= (cpu->enable
1894 #ifndef HAVE_AS_FMAF_HPC_VIS3
1895 & ~(MASK_FMAF | MASK_VIS3)
1896 #endif
1897 #ifndef HAVE_AS_SPARC4
1898 & ~MASK_CBCOND
1899 #endif
1900 #ifndef HAVE_AS_SPARC5_VIS4
1901 & ~(MASK_VIS4 | MASK_SUBXC)
1902 #endif
1903 #ifndef HAVE_AS_SPARC6
1904 & ~(MASK_VIS4B)
1905 #endif
1906 #ifndef HAVE_AS_LEON
1907 & ~(MASK_LEON | MASK_LEON3)
1908 #endif
1909 & ~(target_flags_explicit & MASK_FEATURES)
1910 );
1911
1912 /* FsMULd is a V8 instruction. */
1913 if (!TARGET_V8 && !TARGET_V9)
1914 target_flags &= ~MASK_FSMULD;
1915
1916 /* -mvis2 implies -mvis. */
1917 if (TARGET_VIS2)
1918 target_flags |= MASK_VIS;
1919
1920 /* -mvis3 implies -mvis2 and -mvis. */
1921 if (TARGET_VIS3)
1922 target_flags |= MASK_VIS2 | MASK_VIS;
1923
1924 /* -mvis3b implies -mvis3, -mvis2 and -mvis. */
1925 if (TARGET_VIS3B)
1926 target_flags |= MASK_VIS3 | MASK_VIS2 | MASK_VIS;
1927
1928 /* -mvis4 implies -mvis3b, -mvis3, -mvis2 and -mvis. */
1929 if (TARGET_VIS4)
1930 target_flags |= MASK_VIS3B | MASK_VIS3 | MASK_VIS2 | MASK_VIS;
1931
1932 /* -mvis4b implies -mvis4, -mvis3b, -mvis3, -mvis2 and -mvis */
1933 if (TARGET_VIS4B)
1934 target_flags |= MASK_VIS4 | MASK_VIS3B | MASK_VIS3 | MASK_VIS2 | MASK_VIS;
1935
1936 /* Don't allow -mvis, -mvis2, -mvis3, -mvis3b, -mvis4, -mvis4b, -mfmaf and
1937 -mfsmuld if FPU is disabled. */
1938 if (!TARGET_FPU)
1939 target_flags &= ~(MASK_VIS | MASK_VIS2 | MASK_VIS3 | MASK_VIS3B
1940 | MASK_VIS4 | MASK_VIS4B | MASK_FMAF | MASK_FSMULD);
1941
1942 /* -mvis assumes UltraSPARC+, so we are sure v9 instructions
1943 are available; -m64 also implies v9. */
1944 if (TARGET_VIS || TARGET_ARCH64)
1945 {
1946 target_flags |= MASK_V9;
1947 target_flags &= ~(MASK_V8 | MASK_SPARCLET | MASK_SPARCLITE);
1948 }
1949
1950 /* -mvis also implies -mv8plus on 32-bit. */
1951 if (TARGET_VIS && !TARGET_ARCH64)
1952 target_flags |= MASK_V8PLUS;
1953
1954 /* Use the deprecated v8 insns for sparc64 in 32-bit mode. */
1955 if (TARGET_V9 && TARGET_ARCH32)
1956 target_flags |= MASK_DEPRECATED_V8_INSNS;
1957
1958 /* V8PLUS requires V9 and makes no sense in 64-bit mode. */
1959 if (!TARGET_V9 || TARGET_ARCH64)
1960 target_flags &= ~MASK_V8PLUS;
1961
1962 /* Don't use stack biasing in 32-bit mode. */
1963 if (TARGET_ARCH32)
1964 target_flags &= ~MASK_STACK_BIAS;
1965
1966 /* Enable applicable errata workarounds for LEON3FT. */
1967 if (sparc_fix_ut699 || sparc_fix_ut700 || sparc_fix_gr712rc)
1968 {
1969 sparc_fix_b2bst = 1;
1970 sparc_fix_lost_divsqrt = 1;
1971 }
1972
1973 /* Disable FsMULd for the UT699 since it doesn't work correctly. */
1974 if (sparc_fix_ut699)
1975 target_flags &= ~MASK_FSMULD;
1976
1977 #ifdef TARGET_DEFAULT_LONG_DOUBLE_128
1978 if (!(target_flags_explicit & MASK_LONG_DOUBLE_128))
1979 target_flags |= MASK_LONG_DOUBLE_128;
1980 #endif
1981
1982 if (TARGET_DEBUG_OPTIONS)
1983 dump_target_flags ("Final target_flags", target_flags);
1984
1985 /* Set the code model if no -mcmodel option was specified. */
1986 if (OPTION_SET_P (sparc_code_model))
1987 {
1988 if (TARGET_ARCH32)
1989 error ("%<-mcmodel=%> is not supported in 32-bit mode");
1990 }
1991 else
1992 {
1993 if (TARGET_ARCH32)
1994 sparc_code_model = CM_32;
1995 else
1996 sparc_code_model = SPARC_DEFAULT_CMODEL;
1997 }
1998
1999 /* Set the memory model if no -mmemory-model option was specified. */
2000 if (!OPTION_SET_P (sparc_memory_model))
2001 {
2002 /* Choose the memory model for the operating system. */
2003 enum sparc_memory_model_type os_default = SUBTARGET_DEFAULT_MEMORY_MODEL;
2004 if (os_default != SMM_DEFAULT)
2005 sparc_memory_model = os_default;
2006 /* Choose the most relaxed model for the processor. */
2007 else if (TARGET_V9)
2008 sparc_memory_model = SMM_RMO;
2009 else if (TARGET_LEON3)
2010 sparc_memory_model = SMM_TSO;
2011 else if (TARGET_LEON)
2012 sparc_memory_model = SMM_SC;
2013 else if (TARGET_V8)
2014 sparc_memory_model = SMM_PSO;
2015 else
2016 sparc_memory_model = SMM_SC;
2017 }
2018
2019 /* Supply a default value for align_functions. */
2020 if (flag_align_functions && !str_align_functions)
2021 {
2022 if (sparc_cpu == PROCESSOR_ULTRASPARC
2023 || sparc_cpu == PROCESSOR_ULTRASPARC3
2024 || sparc_cpu == PROCESSOR_NIAGARA
2025 || sparc_cpu == PROCESSOR_NIAGARA2
2026 || sparc_cpu == PROCESSOR_NIAGARA3
2027 || sparc_cpu == PROCESSOR_NIAGARA4)
2028 str_align_functions = "32";
2029 else if (sparc_cpu == PROCESSOR_NIAGARA7
2030 || sparc_cpu == PROCESSOR_M8)
2031 str_align_functions = "64";
2032 }
2033
2034 /* Validate PCC_STRUCT_RETURN. */
2035 if (flag_pcc_struct_return == DEFAULT_PCC_STRUCT_RETURN)
2036 flag_pcc_struct_return = (TARGET_ARCH64 ? 0 : 1);
2037
2038 /* Only use .uaxword when compiling for a 64-bit target. */
2039 if (!TARGET_ARCH64)
2040 targetm.asm_out.unaligned_op.di = NULL;
2041
2042 /* Set the processor costs. */
2043 switch (sparc_cpu)
2044 {
2045 case PROCESSOR_V7:
2046 case PROCESSOR_CYPRESS:
2047 sparc_costs = &cypress_costs;
2048 break;
2049 case PROCESSOR_V8:
2050 case PROCESSOR_SPARCLITE:
2051 case PROCESSOR_SUPERSPARC:
2052 sparc_costs = &supersparc_costs;
2053 break;
2054 case PROCESSOR_F930:
2055 case PROCESSOR_F934:
2056 case PROCESSOR_HYPERSPARC:
2057 case PROCESSOR_SPARCLITE86X:
2058 sparc_costs = &hypersparc_costs;
2059 break;
2060 case PROCESSOR_LEON:
2061 sparc_costs = &leon_costs;
2062 break;
2063 case PROCESSOR_LEON3:
2064 case PROCESSOR_LEON3V7:
2065 sparc_costs = &leon3_costs;
2066 break;
2067 case PROCESSOR_LEON5:
2068 sparc_costs = &leon5_costs;
2069 break;
2070 case PROCESSOR_SPARCLET:
2071 case PROCESSOR_TSC701:
2072 sparc_costs = &sparclet_costs;
2073 break;
2074 case PROCESSOR_V9:
2075 case PROCESSOR_ULTRASPARC:
2076 sparc_costs = &ultrasparc_costs;
2077 break;
2078 case PROCESSOR_ULTRASPARC3:
2079 sparc_costs = &ultrasparc3_costs;
2080 break;
2081 case PROCESSOR_NIAGARA:
2082 sparc_costs = &niagara_costs;
2083 break;
2084 case PROCESSOR_NIAGARA2:
2085 sparc_costs = &niagara2_costs;
2086 break;
2087 case PROCESSOR_NIAGARA3:
2088 sparc_costs = &niagara3_costs;
2089 break;
2090 case PROCESSOR_NIAGARA4:
2091 sparc_costs = &niagara4_costs;
2092 break;
2093 case PROCESSOR_NIAGARA7:
2094 sparc_costs = &niagara7_costs;
2095 break;
2096 case PROCESSOR_M8:
2097 sparc_costs = &m8_costs;
2098 break;
2099 case PROCESSOR_NATIVE:
2100 gcc_unreachable ();
2101 };
2102
2103 /* param_simultaneous_prefetches is the number of prefetches that
2104 can run at the same time. More important, it is the threshold
2105 defining when additional prefetches will be dropped by the
2106 hardware.
2107
2108 The UltraSPARC-III features a documented prefetch queue with a
2109 size of 8. Additional prefetches issued in the cpu are
2110 dropped.
2111
2112 Niagara processors are different. In these processors prefetches
2113 are handled much like regular loads. The L1 miss buffer is 32
2114 entries, but prefetches start getting affected when 30 entries
2115 become occupied. That occupation could be a mix of regular loads
2116 and prefetches though. And that buffer is shared by all threads.
2117 Once the threshold is reached, if the core is running a single
2118 thread the prefetch will retry. If more than one thread is
2119 running, the prefetch will be dropped.
2120
2121 All this makes it very difficult to determine how many
2122 simultaneous prefetches can be issued simultaneously, even in a
2123 single-threaded program. Experimental results show that setting
2124 this parameter to 32 works well when the number of threads is not
2125 high. */
2126 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
2127 param_simultaneous_prefetches,
2128 ((sparc_cpu == PROCESSOR_ULTRASPARC
2129 || sparc_cpu == PROCESSOR_NIAGARA
2130 || sparc_cpu == PROCESSOR_NIAGARA2
2131 || sparc_cpu == PROCESSOR_NIAGARA3
2132 || sparc_cpu == PROCESSOR_NIAGARA4)
2133 ? 2
2134 : (sparc_cpu == PROCESSOR_ULTRASPARC3
2135 ? 8 : ((sparc_cpu == PROCESSOR_NIAGARA7
2136 || sparc_cpu == PROCESSOR_M8)
2137 ? 32 : 3))));
2138
2139 /* param_l1_cache_line_size is the size of the L1 cache line, in
2140 bytes.
2141
2142 The Oracle SPARC Architecture (previously the UltraSPARC
2143 Architecture) specification states that when a PREFETCH[A]
2144 instruction is executed an implementation-specific amount of data
2145 is prefetched, and that it is at least 64 bytes long (aligned to
2146 at least 64 bytes).
2147
2148 However, this is not correct. The M7 (and implementations prior
2149 to that) does not guarantee a 64B prefetch into a cache if the
2150 line size is smaller. A single cache line is all that is ever
2151 prefetched. So for the M7, where the L1D$ has 32B lines and the
2152 L2D$ and L3 have 64B lines, a prefetch will prefetch 64B into the
2153 L2 and L3, but only 32B are brought into the L1D$. (Assuming it
2154 is a read_n prefetch, which is the only type which allocates to
2155 the L1.) */
2156 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
2157 param_l1_cache_line_size,
2158 (sparc_cpu == PROCESSOR_M8 ? 64 : 32));
2159
2160 /* param_l1_cache_size is the size of the L1D$ (most SPARC chips use
2161 Hardvard level-1 caches) in kilobytes. Both UltraSPARC and
2162 Niagara processors feature a L1D$ of 16KB. */
2163 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
2164 param_l1_cache_size,
2165 ((sparc_cpu == PROCESSOR_ULTRASPARC
2166 || sparc_cpu == PROCESSOR_ULTRASPARC3
2167 || sparc_cpu == PROCESSOR_NIAGARA
2168 || sparc_cpu == PROCESSOR_NIAGARA2
2169 || sparc_cpu == PROCESSOR_NIAGARA3
2170 || sparc_cpu == PROCESSOR_NIAGARA4
2171 || sparc_cpu == PROCESSOR_NIAGARA7
2172 || sparc_cpu == PROCESSOR_M8)
2173 ? 16 : 64));
2174
2175 /* param_l2_cache_size is the size fo the L2 in kilobytes. Note
2176 that 512 is the default in params.def. */
2177 SET_OPTION_IF_UNSET (&global_options, &global_options_set,
2178 param_l2_cache_size,
2179 ((sparc_cpu == PROCESSOR_NIAGARA4
2180 || sparc_cpu == PROCESSOR_M8)
2181 ? 128 : (sparc_cpu == PROCESSOR_NIAGARA7
2182 ? 256 : 512)));
2183
2184
2185 /* Disable save slot sharing for call-clobbered registers by default.
2186 The IRA sharing algorithm works on single registers only and this
2187 pessimizes for double floating-point registers. */
2188 if (!OPTION_SET_P (flag_ira_share_save_slots))
2189 flag_ira_share_save_slots = 0;
2190
2191 /* Only enable REE by default in 64-bit mode where it helps to eliminate
2192 redundant 32-to-64-bit extensions. */
2193 if (!OPTION_SET_P (flag_ree) && TARGET_ARCH32)
2194 flag_ree = 0;
2195
2196 /* Do various machine dependent initializations. */
2197 sparc_init_modes ();
2198
2199 /* Set up function hooks. */
2200 init_machine_status = sparc_init_machine_status;
2201 }
2202
2203 /* Miscellaneous utilities. */
2204
2205 /* Nonzero if CODE, a comparison, is suitable for use in v9 conditional move
2206 or branch on register contents instructions. */
2207
2208 int
2209 v9_regcmp_p (enum rtx_code code)
2210 {
2211 return (code == EQ || code == NE || code == GE || code == LT
2212 || code == LE || code == GT);
2213 }
2214
2215 /* Nonzero if OP is a floating point constant which can
2216 be loaded into an integer register using a single
2217 sethi instruction. */
2218
2219 int
2220 fp_sethi_p (rtx op)
2221 {
2222 if (GET_CODE (op) == CONST_DOUBLE)
2223 {
2224 long i;
2225
2226 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (op), i);
2227 return !SPARC_SIMM13_P (i) && SPARC_SETHI_P (i);
2228 }
2229
2230 return 0;
2231 }
2232
2233 /* Nonzero if OP is a floating point constant which can
2234 be loaded into an integer register using a single
2235 mov instruction. */
2236
2237 int
2238 fp_mov_p (rtx op)
2239 {
2240 if (GET_CODE (op) == CONST_DOUBLE)
2241 {
2242 long i;
2243
2244 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (op), i);
2245 return SPARC_SIMM13_P (i);
2246 }
2247
2248 return 0;
2249 }
2250
2251 /* Nonzero if OP is a floating point constant which can
2252 be loaded into an integer register using a high/losum
2253 instruction sequence. */
2254
2255 int
2256 fp_high_losum_p (rtx op)
2257 {
2258 /* The constraints calling this should only be in
2259 SFmode move insns, so any constant which cannot
2260 be moved using a single insn will do. */
2261 if (GET_CODE (op) == CONST_DOUBLE)
2262 {
2263 long i;
2264
2265 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (op), i);
2266 return !SPARC_SIMM13_P (i) && !SPARC_SETHI_P (i);
2267 }
2268
2269 return 0;
2270 }
2271
2272 /* Return true if the address of LABEL can be loaded by means of the
2273 mov{si,di}_pic_label_ref patterns in PIC mode. */
2274
2275 static bool
2276 can_use_mov_pic_label_ref (rtx label)
2277 {
2278 /* VxWorks does not impose a fixed gap between segments; the run-time
2279 gap can be different from the object-file gap. We therefore can't
2280 assume X - _GLOBAL_OFFSET_TABLE_ is a link-time constant unless we
2281 are absolutely sure that X is in the same segment as the GOT.
2282 Unfortunately, the flexibility of linker scripts means that we
2283 can't be sure of that in general, so assume that GOT-relative
2284 accesses are never valid on VxWorks. */
2285 if (TARGET_VXWORKS_RTP)
2286 return false;
2287
2288 /* Similarly, if the label is non-local, it might end up being placed
2289 in a different section than the current one; now mov_pic_label_ref
2290 requires the label and the code to be in the same section. */
2291 if (LABEL_REF_NONLOCAL_P (label))
2292 return false;
2293
2294 /* Finally, if we are reordering basic blocks and partition into hot
2295 and cold sections, this might happen for any label. */
2296 if (flag_reorder_blocks_and_partition)
2297 return false;
2298
2299 return true;
2300 }
2301
2302 /* Expand a move instruction. Return true if all work is done. */
2303
2304 bool
2305 sparc_expand_move (machine_mode mode, rtx *operands)
2306 {
2307 /* Handle sets of MEM first. */
2308 if (GET_CODE (operands[0]) == MEM)
2309 {
2310 /* 0 is a register (or a pair of registers) on SPARC. */
2311 if (register_or_zero_operand (operands[1], mode))
2312 return false;
2313
2314 if (!reload_in_progress)
2315 {
2316 operands[0] = validize_mem (operands[0]);
2317 operands[1] = force_reg (mode, operands[1]);
2318 }
2319 }
2320
2321 /* Fix up TLS cases. */
2322 if (TARGET_HAVE_TLS
2323 && CONSTANT_P (operands[1])
2324 && sparc_tls_referenced_p (operands [1]))
2325 {
2326 operands[1] = sparc_legitimize_tls_address (operands[1]);
2327 return false;
2328 }
2329
2330 /* Fix up PIC cases. */
2331 if (flag_pic && CONSTANT_P (operands[1]))
2332 {
2333 if (pic_address_needs_scratch (operands[1]))
2334 operands[1] = sparc_legitimize_pic_address (operands[1], NULL_RTX);
2335
2336 /* We cannot use the mov{si,di}_pic_label_ref patterns in all cases. */
2337 if ((GET_CODE (operands[1]) == LABEL_REF
2338 && can_use_mov_pic_label_ref (operands[1]))
2339 || (GET_CODE (operands[1]) == CONST
2340 && GET_CODE (XEXP (operands[1], 0)) == PLUS
2341 && GET_CODE (XEXP (XEXP (operands[1], 0), 0)) == LABEL_REF
2342 && GET_CODE (XEXP (XEXP (operands[1], 0), 1)) == CONST_INT
2343 && can_use_mov_pic_label_ref (XEXP (XEXP (operands[1], 0), 0))))
2344 {
2345 if (mode == SImode)
2346 {
2347 emit_insn (gen_movsi_pic_label_ref (operands[0], operands[1]));
2348 return true;
2349 }
2350
2351 if (mode == DImode)
2352 {
2353 emit_insn (gen_movdi_pic_label_ref (operands[0], operands[1]));
2354 return true;
2355 }
2356 }
2357
2358 if (symbolic_operand (operands[1], mode))
2359 {
2360 operands[1]
2361 = sparc_legitimize_pic_address (operands[1],
2362 reload_in_progress
2363 ? operands[0] : NULL_RTX);
2364 return false;
2365 }
2366 }
2367
2368 /* If we are trying to toss an integer constant into FP registers,
2369 or loading a FP or vector constant, force it into memory. */
2370 if (CONSTANT_P (operands[1])
2371 && REG_P (operands[0])
2372 && (SPARC_FP_REG_P (REGNO (operands[0]))
2373 || SCALAR_FLOAT_MODE_P (mode)
2374 || VECTOR_MODE_P (mode)))
2375 {
2376 /* emit_group_store will send such bogosity to us when it is
2377 not storing directly into memory. So fix this up to avoid
2378 crashes in output_constant_pool. */
2379 if (operands [1] == const0_rtx)
2380 operands[1] = CONST0_RTX (mode);
2381
2382 /* We can clear or set to all-ones FP registers if TARGET_VIS, and
2383 always other regs. */
2384 if ((TARGET_VIS || REGNO (operands[0]) < SPARC_FIRST_FP_REG)
2385 && (const_zero_operand (operands[1], mode)
2386 || const_all_ones_operand (operands[1], mode)))
2387 return false;
2388
2389 if (REGNO (operands[0]) < SPARC_FIRST_FP_REG
2390 /* We are able to build any SF constant in integer registers
2391 with at most 2 instructions. */
2392 && (mode == SFmode
2393 /* And any DF constant in integer registers if needed. */
2394 || (mode == DFmode && !can_create_pseudo_p ())))
2395 return false;
2396
2397 operands[1] = force_const_mem (mode, operands[1]);
2398 if (!reload_in_progress)
2399 operands[1] = validize_mem (operands[1]);
2400 return false;
2401 }
2402
2403 /* Accept non-constants and valid constants unmodified. */
2404 if (!CONSTANT_P (operands[1])
2405 || GET_CODE (operands[1]) == HIGH
2406 || input_operand (operands[1], mode))
2407 return false;
2408
2409 switch (mode)
2410 {
2411 case E_QImode:
2412 /* All QImode constants require only one insn, so proceed. */
2413 break;
2414
2415 case E_HImode:
2416 case E_SImode:
2417 sparc_emit_set_const32 (operands[0], operands[1]);
2418 return true;
2419
2420 case E_DImode:
2421 /* input_operand should have filtered out 32-bit mode. */
2422 sparc_emit_set_const64 (operands[0], operands[1]);
2423 return true;
2424
2425 case E_TImode:
2426 {
2427 rtx high, low;
2428 /* TImode isn't available in 32-bit mode. */
2429 split_double (operands[1], &high, &low);
2430 emit_insn (gen_movdi (operand_subword (operands[0], 0, 0, TImode),
2431 high));
2432 emit_insn (gen_movdi (operand_subword (operands[0], 1, 0, TImode),
2433 low));
2434 }
2435 return true;
2436
2437 default:
2438 gcc_unreachable ();
2439 }
2440
2441 return false;
2442 }
2443
2444 /* Load OP1, a 32-bit constant, into OP0, a register.
2445 We know it can't be done in one insn when we get
2446 here, the move expander guarantees this. */
2447
2448 static void
2449 sparc_emit_set_const32 (rtx op0, rtx op1)
2450 {
2451 machine_mode mode = GET_MODE (op0);
2452 rtx temp = op0;
2453
2454 if (can_create_pseudo_p ())
2455 temp = gen_reg_rtx (mode);
2456
2457 if (GET_CODE (op1) == CONST_INT)
2458 {
2459 gcc_assert (!small_int_operand (op1, mode)
2460 && !const_high_operand (op1, mode));
2461
2462 /* Emit them as real moves instead of a HIGH/LO_SUM,
2463 this way CSE can see everything and reuse intermediate
2464 values if it wants. */
2465 emit_insn (gen_rtx_SET (temp, GEN_INT (INTVAL (op1)
2466 & ~(HOST_WIDE_INT) 0x3ff)));
2467
2468 emit_insn (gen_rtx_SET (op0,
2469 gen_rtx_IOR (mode, temp,
2470 GEN_INT (INTVAL (op1) & 0x3ff))));
2471 }
2472 else
2473 {
2474 /* A symbol, emit in the traditional way. */
2475 emit_insn (gen_rtx_SET (temp, gen_rtx_HIGH (mode, op1)));
2476 emit_insn (gen_rtx_SET (op0, gen_rtx_LO_SUM (mode, temp, op1)));
2477 }
2478 }
2479
2480 /* Load OP1, a symbolic 64-bit constant, into OP0, a DImode register.
2481 If TEMP is nonzero, we are forbidden to use any other scratch
2482 registers. Otherwise, we are allowed to generate them as needed.
2483
2484 Note that TEMP may have TImode if the code model is TARGET_CM_MEDANY
2485 or TARGET_CM_EMBMEDANY (see the reload_indi and reload_outdi patterns). */
2486
2487 void
2488 sparc_emit_set_symbolic_const64 (rtx op0, rtx op1, rtx temp)
2489 {
2490 rtx cst, temp1, temp2, temp3, temp4, temp5;
2491 rtx ti_temp = 0;
2492
2493 /* Deal with too large offsets. */
2494 if (GET_CODE (op1) == CONST
2495 && GET_CODE (XEXP (op1, 0)) == PLUS
2496 && CONST_INT_P (cst = XEXP (XEXP (op1, 0), 1))
2497 && trunc_int_for_mode (INTVAL (cst), SImode) != INTVAL (cst))
2498 {
2499 gcc_assert (!temp);
2500 temp1 = gen_reg_rtx (DImode);
2501 temp2 = gen_reg_rtx (DImode);
2502 sparc_emit_set_const64 (temp2, cst);
2503 sparc_emit_set_symbolic_const64 (temp1, XEXP (XEXP (op1, 0), 0),
2504 NULL_RTX);
2505 emit_insn (gen_rtx_SET (op0, gen_rtx_PLUS (DImode, temp1, temp2)));
2506 return;
2507 }
2508
2509 if (temp && GET_MODE (temp) == TImode)
2510 {
2511 ti_temp = temp;
2512 temp = gen_rtx_REG (DImode, REGNO (temp));
2513 }
2514
2515 /* SPARC-V9 code model support. */
2516 switch (sparc_code_model)
2517 {
2518 case CM_MEDLOW:
2519 /* The range spanned by all instructions in the object is less
2520 than 2^31 bytes (2GB) and the distance from any instruction
2521 to the location of the label _GLOBAL_OFFSET_TABLE_ is less
2522 than 2^31 bytes (2GB).
2523
2524 The executable must be in the low 4TB of the virtual address
2525 space.
2526
2527 sethi %hi(symbol), %temp1
2528 or %temp1, %lo(symbol), %reg */
2529 if (temp)
2530 temp1 = temp; /* op0 is allowed. */
2531 else
2532 temp1 = gen_reg_rtx (DImode);
2533
2534 emit_insn (gen_rtx_SET (temp1, gen_rtx_HIGH (DImode, op1)));
2535 emit_insn (gen_rtx_SET (op0, gen_rtx_LO_SUM (DImode, temp1, op1)));
2536 break;
2537
2538 case CM_MEDMID:
2539 /* The range spanned by all instructions in the object is less
2540 than 2^31 bytes (2GB) and the distance from any instruction
2541 to the location of the label _GLOBAL_OFFSET_TABLE_ is less
2542 than 2^31 bytes (2GB).
2543
2544 The executable must be in the low 16TB of the virtual address
2545 space.
2546
2547 sethi %h44(symbol), %temp1
2548 or %temp1, %m44(symbol), %temp2
2549 sllx %temp2, 12, %temp3
2550 or %temp3, %l44(symbol), %reg */
2551 if (temp)
2552 {
2553 temp1 = op0;
2554 temp2 = op0;
2555 temp3 = temp; /* op0 is allowed. */
2556 }
2557 else
2558 {
2559 temp1 = gen_reg_rtx (DImode);
2560 temp2 = gen_reg_rtx (DImode);
2561 temp3 = gen_reg_rtx (DImode);
2562 }
2563
2564 emit_insn (gen_seth44 (temp1, op1));
2565 emit_insn (gen_setm44 (temp2, temp1, op1));
2566 emit_insn (gen_rtx_SET (temp3,
2567 gen_rtx_ASHIFT (DImode, temp2, GEN_INT (12))));
2568 emit_insn (gen_setl44 (op0, temp3, op1));
2569 break;
2570
2571 case CM_MEDANY:
2572 /* The range spanned by all instructions in the object is less
2573 than 2^31 bytes (2GB) and the distance from any instruction
2574 to the location of the label _GLOBAL_OFFSET_TABLE_ is less
2575 than 2^31 bytes (2GB).
2576
2577 The executable can be placed anywhere in the virtual address
2578 space.
2579
2580 sethi %hh(symbol), %temp1
2581 sethi %lm(symbol), %temp2
2582 or %temp1, %hm(symbol), %temp3
2583 sllx %temp3, 32, %temp4
2584 or %temp4, %temp2, %temp5
2585 or %temp5, %lo(symbol), %reg */
2586 if (temp)
2587 {
2588 /* It is possible that one of the registers we got for operands[2]
2589 might coincide with that of operands[0] (which is why we made
2590 it TImode). Pick the other one to use as our scratch. */
2591 if (rtx_equal_p (temp, op0))
2592 {
2593 gcc_assert (ti_temp);
2594 temp = gen_rtx_REG (DImode, REGNO (temp) + 1);
2595 }
2596 temp1 = op0;
2597 temp2 = temp; /* op0 is _not_ allowed, see above. */
2598 temp3 = op0;
2599 temp4 = op0;
2600 temp5 = op0;
2601 }
2602 else
2603 {
2604 temp1 = gen_reg_rtx (DImode);
2605 temp2 = gen_reg_rtx (DImode);
2606 temp3 = gen_reg_rtx (DImode);
2607 temp4 = gen_reg_rtx (DImode);
2608 temp5 = gen_reg_rtx (DImode);
2609 }
2610
2611 emit_insn (gen_sethh (temp1, op1));
2612 emit_insn (gen_setlm (temp2, op1));
2613 emit_insn (gen_sethm (temp3, temp1, op1));
2614 emit_insn (gen_rtx_SET (temp4,
2615 gen_rtx_ASHIFT (DImode, temp3, GEN_INT (32))));
2616 emit_insn (gen_rtx_SET (temp5, gen_rtx_PLUS (DImode, temp4, temp2)));
2617 emit_insn (gen_setlo (op0, temp5, op1));
2618 break;
2619
2620 case CM_EMBMEDANY:
2621 /* Old old old backwards compatibility kruft here.
2622 Essentially it is MEDLOW with a fixed 64-bit
2623 virtual base added to all data segment addresses.
2624 Text-segment stuff is computed like MEDANY, we can't
2625 reuse the code above because the relocation knobs
2626 look different.
2627
2628 Data segment: sethi %hi(symbol), %temp1
2629 add %temp1, EMBMEDANY_BASE_REG, %temp2
2630 or %temp2, %lo(symbol), %reg */
2631 if (data_segment_operand (op1, GET_MODE (op1)))
2632 {
2633 if (temp)
2634 {
2635 temp1 = temp; /* op0 is allowed. */
2636 temp2 = op0;
2637 }
2638 else
2639 {
2640 temp1 = gen_reg_rtx (DImode);
2641 temp2 = gen_reg_rtx (DImode);
2642 }
2643
2644 emit_insn (gen_embmedany_sethi (temp1, op1));
2645 emit_insn (gen_embmedany_brsum (temp2, temp1));
2646 emit_insn (gen_embmedany_losum (op0, temp2, op1));
2647 }
2648
2649 /* Text segment: sethi %uhi(symbol), %temp1
2650 sethi %hi(symbol), %temp2
2651 or %temp1, %ulo(symbol), %temp3
2652 sllx %temp3, 32, %temp4
2653 or %temp4, %temp2, %temp5
2654 or %temp5, %lo(symbol), %reg */
2655 else
2656 {
2657 if (temp)
2658 {
2659 /* It is possible that one of the registers we got for operands[2]
2660 might coincide with that of operands[0] (which is why we made
2661 it TImode). Pick the other one to use as our scratch. */
2662 if (rtx_equal_p (temp, op0))
2663 {
2664 gcc_assert (ti_temp);
2665 temp = gen_rtx_REG (DImode, REGNO (temp) + 1);
2666 }
2667 temp1 = op0;
2668 temp2 = temp; /* op0 is _not_ allowed, see above. */
2669 temp3 = op0;
2670 temp4 = op0;
2671 temp5 = op0;
2672 }
2673 else
2674 {
2675 temp1 = gen_reg_rtx (DImode);
2676 temp2 = gen_reg_rtx (DImode);
2677 temp3 = gen_reg_rtx (DImode);
2678 temp4 = gen_reg_rtx (DImode);
2679 temp5 = gen_reg_rtx (DImode);
2680 }
2681
2682 emit_insn (gen_embmedany_textuhi (temp1, op1));
2683 emit_insn (gen_embmedany_texthi (temp2, op1));
2684 emit_insn (gen_embmedany_textulo (temp3, temp1, op1));
2685 emit_insn (gen_rtx_SET (temp4,
2686 gen_rtx_ASHIFT (DImode, temp3, GEN_INT (32))));
2687 emit_insn (gen_rtx_SET (temp5, gen_rtx_PLUS (DImode, temp4, temp2)));
2688 emit_insn (gen_embmedany_textlo (op0, temp5, op1));
2689 }
2690 break;
2691
2692 default:
2693 gcc_unreachable ();
2694 }
2695 }
2696
2697 /* These avoid problems when cross compiling. If we do not
2698 go through all this hair then the optimizer will see
2699 invalid REG_EQUAL notes or in some cases none at all. */
2700 static rtx gen_safe_HIGH64 (rtx, HOST_WIDE_INT);
2701 static rtx gen_safe_SET64 (rtx, HOST_WIDE_INT);
2702 static rtx gen_safe_OR64 (rtx, HOST_WIDE_INT);
2703 static rtx gen_safe_XOR64 (rtx, HOST_WIDE_INT);
2704
2705 /* The optimizer is not to assume anything about exactly
2706 which bits are set for a HIGH, they are unspecified.
2707 Unfortunately this leads to many missed optimizations
2708 during CSE. We mask out the non-HIGH bits, and matches
2709 a plain movdi, to alleviate this problem. */
2710 static rtx
2711 gen_safe_HIGH64 (rtx dest, HOST_WIDE_INT val)
2712 {
2713 return gen_rtx_SET (dest, GEN_INT (val & ~(HOST_WIDE_INT)0x3ff));
2714 }
2715
2716 static rtx
2717 gen_safe_SET64 (rtx dest, HOST_WIDE_INT val)
2718 {
2719 return gen_rtx_SET (dest, GEN_INT (val));
2720 }
2721
2722 static rtx
2723 gen_safe_OR64 (rtx src, HOST_WIDE_INT val)
2724 {
2725 return gen_rtx_IOR (DImode, src, GEN_INT (val));
2726 }
2727
2728 static rtx
2729 gen_safe_XOR64 (rtx src, HOST_WIDE_INT val)
2730 {
2731 return gen_rtx_XOR (DImode, src, GEN_INT (val));
2732 }
2733
2734 /* Worker routines for 64-bit constant formation on arch64.
2735 One of the key things to be doing in these emissions is
2736 to create as many temp REGs as possible. This makes it
2737 possible for half-built constants to be used later when
2738 such values are similar to something required later on.
2739 Without doing this, the optimizer cannot see such
2740 opportunities. */
2741
2742 static void sparc_emit_set_const64_quick1 (rtx, rtx,
2743 unsigned HOST_WIDE_INT, int);
2744
2745 static void
2746 sparc_emit_set_const64_quick1 (rtx op0, rtx temp,
2747 unsigned HOST_WIDE_INT low_bits, int is_neg)
2748 {
2749 unsigned HOST_WIDE_INT high_bits;
2750
2751 if (is_neg)
2752 high_bits = (~low_bits) & 0xffffffff;
2753 else
2754 high_bits = low_bits;
2755
2756 emit_insn (gen_safe_HIGH64 (temp, high_bits));
2757 if (!is_neg)
2758 {
2759 emit_insn (gen_rtx_SET (op0, gen_safe_OR64 (temp, (high_bits & 0x3ff))));
2760 }
2761 else
2762 {
2763 /* If we are XOR'ing with -1, then we should emit a one's complement
2764 instead. This way the combiner will notice logical operations
2765 such as ANDN later on and substitute. */
2766 if ((low_bits & 0x3ff) == 0x3ff)
2767 {
2768 emit_insn (gen_rtx_SET (op0, gen_rtx_NOT (DImode, temp)));
2769 }
2770 else
2771 {
2772 emit_insn (gen_rtx_SET (op0,
2773 gen_safe_XOR64 (temp,
2774 (-(HOST_WIDE_INT)0x400
2775 | (low_bits & 0x3ff)))));
2776 }
2777 }
2778 }
2779
2780 static void sparc_emit_set_const64_quick2 (rtx, rtx, unsigned HOST_WIDE_INT,
2781 unsigned HOST_WIDE_INT, int);
2782
2783 static void
2784 sparc_emit_set_const64_quick2 (rtx op0, rtx temp,
2785 unsigned HOST_WIDE_INT high_bits,
2786 unsigned HOST_WIDE_INT low_immediate,
2787 int shift_count)
2788 {
2789 rtx temp2 = op0;
2790
2791 if ((high_bits & 0xfffffc00) != 0)
2792 {
2793 emit_insn (gen_safe_HIGH64 (temp, high_bits));
2794 if ((high_bits & ~0xfffffc00) != 0)
2795 emit_insn (gen_rtx_SET (op0,
2796 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
2797 else
2798 temp2 = temp;
2799 }
2800 else
2801 {
2802 emit_insn (gen_safe_SET64 (temp, high_bits));
2803 temp2 = temp;
2804 }
2805
2806 /* Now shift it up into place. */
2807 emit_insn (gen_rtx_SET (op0, gen_rtx_ASHIFT (DImode, temp2,
2808 GEN_INT (shift_count))));
2809
2810 /* If there is a low immediate part piece, finish up by
2811 putting that in as well. */
2812 if (low_immediate != 0)
2813 emit_insn (gen_rtx_SET (op0, gen_safe_OR64 (op0, low_immediate)));
2814 }
2815
2816 static void sparc_emit_set_const64_longway (rtx, rtx, unsigned HOST_WIDE_INT,
2817 unsigned HOST_WIDE_INT);
2818
2819 /* Full 64-bit constant decomposition. Even though this is the
2820 'worst' case, we still optimize a few things away. */
2821 static void
2822 sparc_emit_set_const64_longway (rtx op0, rtx temp,
2823 unsigned HOST_WIDE_INT high_bits,
2824 unsigned HOST_WIDE_INT low_bits)
2825 {
2826 rtx sub_temp = op0;
2827
2828 if (can_create_pseudo_p ())
2829 sub_temp = gen_reg_rtx (DImode);
2830
2831 if ((high_bits & 0xfffffc00) != 0)
2832 {
2833 emit_insn (gen_safe_HIGH64 (temp, high_bits));
2834 if ((high_bits & ~0xfffffc00) != 0)
2835 emit_insn (gen_rtx_SET (sub_temp,
2836 gen_safe_OR64 (temp, (high_bits & 0x3ff))));
2837 else
2838 sub_temp = temp;
2839 }
2840 else
2841 {
2842 emit_insn (gen_safe_SET64 (temp, high_bits));
2843 sub_temp = temp;
2844 }
2845
2846 if (can_create_pseudo_p ())
2847 {
2848 rtx temp2 = gen_reg_rtx (DImode);
2849 rtx temp3 = gen_reg_rtx (DImode);
2850 rtx temp4 = gen_reg_rtx (DImode);
2851
2852 emit_insn (gen_rtx_SET (temp4, gen_rtx_ASHIFT (DImode, sub_temp,
2853 GEN_INT (32))));
2854
2855 emit_insn (gen_safe_HIGH64 (temp2, low_bits));
2856 if ((low_bits & ~0xfffffc00) != 0)
2857 {
2858 emit_insn (gen_rtx_SET (temp3,
2859 gen_safe_OR64 (temp2, (low_bits & 0x3ff))));
2860 emit_insn (gen_rtx_SET (op0, gen_rtx_PLUS (DImode, temp4, temp3)));
2861 }
2862 else
2863 {
2864 emit_insn (gen_rtx_SET (op0, gen_rtx_PLUS (DImode, temp4, temp2)));
2865 }
2866 }
2867 else
2868 {
2869 rtx low1 = GEN_INT ((low_bits >> (32 - 12)) & 0xfff);
2870 rtx low2 = GEN_INT ((low_bits >> (32 - 12 - 12)) & 0xfff);
2871 rtx low3 = GEN_INT ((low_bits >> (32 - 12 - 12 - 8)) & 0x0ff);
2872 int to_shift = 12;
2873
2874 /* We are in the middle of reload, so this is really
2875 painful. However we do still make an attempt to
2876 avoid emitting truly stupid code. */
2877 if (low1 != const0_rtx)
2878 {
2879 emit_insn (gen_rtx_SET (op0, gen_rtx_ASHIFT (DImode, sub_temp,
2880 GEN_INT (to_shift))));
2881 emit_insn (gen_rtx_SET (op0, gen_rtx_IOR (DImode, op0, low1)));
2882 sub_temp = op0;
2883 to_shift = 12;
2884 }
2885 else
2886 {
2887 to_shift += 12;
2888 }
2889 if (low2 != const0_rtx)
2890 {
2891 emit_insn (gen_rtx_SET (op0, gen_rtx_ASHIFT (DImode, sub_temp,
2892 GEN_INT (to_shift))));
2893 emit_insn (gen_rtx_SET (op0, gen_rtx_IOR (DImode, op0, low2)));
2894 sub_temp = op0;
2895 to_shift = 8;
2896 }
2897 else
2898 {
2899 to_shift += 8;
2900 }
2901 emit_insn (gen_rtx_SET (op0, gen_rtx_ASHIFT (DImode, sub_temp,
2902 GEN_INT (to_shift))));
2903 if (low3 != const0_rtx)
2904 emit_insn (gen_rtx_SET (op0, gen_rtx_IOR (DImode, op0, low3)));
2905 /* phew... */
2906 }
2907 }
2908
2909 /* Analyze a 64-bit constant for certain properties. */
2910 static void analyze_64bit_constant (unsigned HOST_WIDE_INT,
2911 unsigned HOST_WIDE_INT,
2912 int *, int *, int *);
2913
2914 static void
2915 analyze_64bit_constant (unsigned HOST_WIDE_INT high_bits,
2916 unsigned HOST_WIDE_INT low_bits,
2917 int *hbsp, int *lbsp, int *abbasp)
2918 {
2919 int lowest_bit_set, highest_bit_set, all_bits_between_are_set;
2920 int i;
2921
2922 lowest_bit_set = highest_bit_set = -1;
2923 i = 0;
2924 do
2925 {
2926 if ((lowest_bit_set == -1)
2927 && ((low_bits >> i) & 1))
2928 lowest_bit_set = i;
2929 if ((highest_bit_set == -1)
2930 && ((high_bits >> (32 - i - 1)) & 1))
2931 highest_bit_set = (64 - i - 1);
2932 }
2933 while (++i < 32
2934 && ((highest_bit_set == -1)
2935 || (lowest_bit_set == -1)));
2936 if (i == 32)
2937 {
2938 i = 0;
2939 do
2940 {
2941 if ((lowest_bit_set == -1)
2942 && ((high_bits >> i) & 1))
2943 lowest_bit_set = i + 32;
2944 if ((highest_bit_set == -1)
2945 && ((low_bits >> (32 - i - 1)) & 1))
2946 highest_bit_set = 32 - i - 1;
2947 }
2948 while (++i < 32
2949 && ((highest_bit_set == -1)
2950 || (lowest_bit_set == -1)));
2951 }
2952 /* If there are no bits set this should have gone out
2953 as one instruction! */
2954 gcc_assert (lowest_bit_set != -1 && highest_bit_set != -1);
2955 all_bits_between_are_set = 1;
2956 for (i = lowest_bit_set; i <= highest_bit_set; i++)
2957 {
2958 if (i < 32)
2959 {
2960 if ((low_bits & (1 << i)) != 0)
2961 continue;
2962 }
2963 else
2964 {
2965 if ((high_bits & (1 << (i - 32))) != 0)
2966 continue;
2967 }
2968 all_bits_between_are_set = 0;
2969 break;
2970 }
2971 *hbsp = highest_bit_set;
2972 *lbsp = lowest_bit_set;
2973 *abbasp = all_bits_between_are_set;
2974 }
2975
2976 static int const64_is_2insns (unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2977
2978 static int
2979 const64_is_2insns (unsigned HOST_WIDE_INT high_bits,
2980 unsigned HOST_WIDE_INT low_bits)
2981 {
2982 int highest_bit_set, lowest_bit_set, all_bits_between_are_set;
2983
2984 if (high_bits == 0
2985 || high_bits == 0xffffffff)
2986 return 1;
2987
2988 analyze_64bit_constant (high_bits, low_bits,
2989 &highest_bit_set, &lowest_bit_set,
2990 &all_bits_between_are_set);
2991
2992 if ((highest_bit_set == 63
2993 || lowest_bit_set == 0)
2994 && all_bits_between_are_set != 0)
2995 return 1;
2996
2997 if ((highest_bit_set - lowest_bit_set) < 21)
2998 return 1;
2999
3000 return 0;
3001 }
3002
3003 static unsigned HOST_WIDE_INT create_simple_focus_bits (unsigned HOST_WIDE_INT,
3004 unsigned HOST_WIDE_INT,
3005 int, int);
3006
3007 static unsigned HOST_WIDE_INT
3008 create_simple_focus_bits (unsigned HOST_WIDE_INT high_bits,
3009 unsigned HOST_WIDE_INT low_bits,
3010 int lowest_bit_set, int shift)
3011 {
3012 HOST_WIDE_INT hi, lo;
3013
3014 if (lowest_bit_set < 32)
3015 {
3016 lo = (low_bits >> lowest_bit_set) << shift;
3017 hi = ((high_bits << (32 - lowest_bit_set)) << shift);
3018 }
3019 else
3020 {
3021 lo = 0;
3022 hi = ((high_bits >> (lowest_bit_set - 32)) << shift);
3023 }
3024 gcc_assert (! (hi & lo));
3025 return (hi | lo);
3026 }
3027
3028 /* Here we are sure to be arch64 and this is an integer constant
3029 being loaded into a register. Emit the most efficient
3030 insn sequence possible. Detection of all the 1-insn cases
3031 has been done already. */
3032 static void
3033 sparc_emit_set_const64 (rtx op0, rtx op1)
3034 {
3035 unsigned HOST_WIDE_INT high_bits, low_bits;
3036 int lowest_bit_set, highest_bit_set;
3037 int all_bits_between_are_set;
3038 rtx temp = 0;
3039
3040 /* Sanity check that we know what we are working with. */
3041 gcc_assert (TARGET_ARCH64
3042 && (GET_CODE (op0) == SUBREG
3043 || (REG_P (op0) && ! SPARC_FP_REG_P (REGNO (op0)))));
3044
3045 if (! can_create_pseudo_p ())
3046 temp = op0;
3047
3048 if (GET_CODE (op1) != CONST_INT)
3049 {
3050 sparc_emit_set_symbolic_const64 (op0, op1, temp);
3051 return;
3052 }
3053
3054 if (! temp)
3055 temp = gen_reg_rtx (DImode);
3056
3057 high_bits = ((INTVAL (op1) >> 32) & 0xffffffff);
3058 low_bits = (INTVAL (op1) & 0xffffffff);
3059
3060 /* low_bits bits 0 --> 31
3061 high_bits bits 32 --> 63 */
3062
3063 analyze_64bit_constant (high_bits, low_bits,
3064 &highest_bit_set, &lowest_bit_set,
3065 &all_bits_between_are_set);
3066
3067 /* First try for a 2-insn sequence. */
3068
3069 /* These situations are preferred because the optimizer can
3070 * do more things with them:
3071 * 1) mov -1, %reg
3072 * sllx %reg, shift, %reg
3073 * 2) mov -1, %reg
3074 * srlx %reg, shift, %reg
3075 * 3) mov some_small_const, %reg
3076 * sllx %reg, shift, %reg
3077 */
3078 if (((highest_bit_set == 63
3079 || lowest_bit_set == 0)
3080 && all_bits_between_are_set != 0)
3081 || ((highest_bit_set - lowest_bit_set) < 12))
3082 {
3083 HOST_WIDE_INT the_const = -1;
3084 int shift = lowest_bit_set;
3085
3086 if ((highest_bit_set != 63
3087 && lowest_bit_set != 0)
3088 || all_bits_between_are_set == 0)
3089 {
3090 the_const =
3091 create_simple_focus_bits (high_bits, low_bits,
3092 lowest_bit_set, 0);
3093 }
3094 else if (lowest_bit_set == 0)
3095 shift = -(63 - highest_bit_set);
3096
3097 gcc_assert (SPARC_SIMM13_P (the_const));
3098 gcc_assert (shift != 0);
3099
3100 emit_insn (gen_safe_SET64 (temp, the_const));
3101 if (shift > 0)
3102 emit_insn (gen_rtx_SET (op0, gen_rtx_ASHIFT (DImode, temp,
3103 GEN_INT (shift))));
3104 else if (shift < 0)
3105 emit_insn (gen_rtx_SET (op0, gen_rtx_LSHIFTRT (DImode, temp,
3106 GEN_INT (-shift))));
3107 return;
3108 }
3109
3110 /* Now a range of 22 or less bits set somewhere.
3111 * 1) sethi %hi(focus_bits), %reg
3112 * sllx %reg, shift, %reg
3113 * 2) sethi %hi(focus_bits), %reg
3114 * srlx %reg, shift, %reg
3115 */
3116 if ((highest_bit_set - lowest_bit_set) < 21)
3117 {
3118 unsigned HOST_WIDE_INT focus_bits =
3119 create_simple_focus_bits (high_bits, low_bits,
3120 lowest_bit_set, 10);
3121
3122 gcc_assert (SPARC_SETHI_P (focus_bits));
3123 gcc_assert (lowest_bit_set != 10);
3124
3125 emit_insn (gen_safe_HIGH64 (temp, focus_bits));
3126
3127 /* If lowest_bit_set == 10 then a sethi alone could have done it. */
3128 if (lowest_bit_set < 10)
3129 emit_insn (gen_rtx_SET (op0,
3130 gen_rtx_LSHIFTRT (DImode, temp,
3131 GEN_INT (10 - lowest_bit_set))));
3132 else if (lowest_bit_set > 10)
3133 emit_insn (gen_rtx_SET (op0,
3134 gen_rtx_ASHIFT (DImode, temp,
3135 GEN_INT (lowest_bit_set - 10))));
3136 return;
3137 }
3138
3139 /* 1) sethi %hi(low_bits), %reg
3140 * or %reg, %lo(low_bits), %reg
3141 * 2) sethi %hi(~low_bits), %reg
3142 * xor %reg, %lo(-0x400 | (low_bits & 0x3ff)), %reg
3143 */
3144 if (high_bits == 0
3145 || high_bits == 0xffffffff)
3146 {
3147 sparc_emit_set_const64_quick1 (op0, temp, low_bits,
3148 (high_bits == 0xffffffff));
3149 return;
3150 }
3151
3152 /* Now, try 3-insn sequences. */
3153
3154 /* 1) sethi %hi(high_bits), %reg
3155 * or %reg, %lo(high_bits), %reg
3156 * sllx %reg, 32, %reg
3157 */
3158 if (low_bits == 0)
3159 {
3160 sparc_emit_set_const64_quick2 (op0, temp, high_bits, 0, 32);
3161 return;
3162 }
3163
3164 /* We may be able to do something quick
3165 when the constant is negated, so try that. */
3166 if (const64_is_2insns ((~high_bits) & 0xffffffff,
3167 (~low_bits) & 0xfffffc00))
3168 {
3169 /* NOTE: The trailing bits get XOR'd so we need the
3170 non-negated bits, not the negated ones. */
3171 unsigned HOST_WIDE_INT trailing_bits = low_bits & 0x3ff;
3172
3173 if ((((~high_bits) & 0xffffffff) == 0
3174 && ((~low_bits) & 0x80000000) == 0)
3175 || (((~high_bits) & 0xffffffff) == 0xffffffff
3176 && ((~low_bits) & 0x80000000) != 0))
3177 {
3178 unsigned HOST_WIDE_INT fast_int = (~low_bits & 0xffffffff);
3179
3180 if ((SPARC_SETHI_P (fast_int)
3181 && (~high_bits & 0xffffffff) == 0)
3182 || SPARC_SIMM13_P (fast_int))
3183 emit_insn (gen_safe_SET64 (temp, fast_int));
3184 else
3185 sparc_emit_set_const64 (temp, GEN_INT (fast_int));
3186 }
3187 else
3188 {
3189 rtx negated_const;
3190 negated_const = GEN_INT (((~low_bits) & 0xfffffc00) |
3191 (((HOST_WIDE_INT)((~high_bits) & 0xffffffff))<<32));
3192 sparc_emit_set_const64 (temp, negated_const);
3193 }
3194
3195 /* If we are XOR'ing with -1, then we should emit a one's complement
3196 instead. This way the combiner will notice logical operations
3197 such as ANDN later on and substitute. */
3198 if (trailing_bits == 0x3ff)
3199 {
3200 emit_insn (gen_rtx_SET (op0, gen_rtx_NOT (DImode, temp)));
3201 }
3202 else
3203 {
3204 emit_insn (gen_rtx_SET (op0,
3205 gen_safe_XOR64 (temp,
3206 (-0x400 | trailing_bits))));
3207 }
3208 return;
3209 }
3210
3211 /* 1) sethi %hi(xxx), %reg
3212 * or %reg, %lo(xxx), %reg
3213 * sllx %reg, yyy, %reg
3214 *
3215 * ??? This is just a generalized version of the low_bits==0
3216 * thing above, FIXME...
3217 */
3218 if ((highest_bit_set - lowest_bit_set) < 32)
3219 {
3220 unsigned HOST_WIDE_INT focus_bits =
3221 create_simple_focus_bits (high_bits, low_bits,
3222 lowest_bit_set, 0);
3223
3224 /* We can't get here in this state. */
3225 gcc_assert (highest_bit_set >= 32 && lowest_bit_set < 32);
3226
3227 /* So what we know is that the set bits straddle the
3228 middle of the 64-bit word. */
3229 sparc_emit_set_const64_quick2 (op0, temp,
3230 focus_bits, 0,
3231 lowest_bit_set);
3232 return;
3233 }
3234
3235 /* 1) sethi %hi(high_bits), %reg
3236 * or %reg, %lo(high_bits), %reg
3237 * sllx %reg, 32, %reg
3238 * or %reg, low_bits, %reg
3239 */
3240 if (SPARC_SIMM13_P (low_bits) && ((int)low_bits > 0))
3241 {
3242 sparc_emit_set_const64_quick2 (op0, temp, high_bits, low_bits, 32);
3243 return;
3244 }
3245
3246 /* The easiest way when all else fails, is full decomposition. */
3247 sparc_emit_set_const64_longway (op0, temp, high_bits, low_bits);
3248 }
3249
3250 /* Implement TARGET_FIXED_CONDITION_CODE_REGS. */
3251
3252 static bool
3253 sparc_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
3254 {
3255 *p1 = SPARC_ICC_REG;
3256 *p2 = SPARC_FCC_REG;
3257 return true;
3258 }
3259
3260 /* Implement TARGET_MIN_ARITHMETIC_PRECISION. */
3261
3262 static unsigned int
3263 sparc_min_arithmetic_precision (void)
3264 {
3265 return 32;
3266 }
3267
3268 /* Given a comparison code (EQ, NE, etc.) and the first operand of a COMPARE,
3269 return the mode to be used for the comparison. For floating-point,
3270 CCFP[E]mode is used. CCNZmode should be used when the first operand
3271 is a PLUS, MINUS, NEG, or ASHIFT. CCmode should be used when no special
3272 processing is needed. */
3273
3274 machine_mode
3275 select_cc_mode (enum rtx_code op, rtx x, rtx y)
3276 {
3277 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3278 {
3279 switch (op)
3280 {
3281 case EQ:
3282 case NE:
3283 case UNORDERED:
3284 case ORDERED:
3285 case UNLT:
3286 case UNLE:
3287 case UNGT:
3288 case UNGE:
3289 case UNEQ:
3290 return CCFPmode;
3291
3292 case LT:
3293 case LE:
3294 case GT:
3295 case GE:
3296 case LTGT:
3297 return CCFPEmode;
3298
3299 default:
3300 gcc_unreachable ();
3301 }
3302 }
3303 else if ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
3304 || GET_CODE (x) == NEG || GET_CODE (x) == ASHIFT)
3305 && y == const0_rtx)
3306 {
3307 if (TARGET_ARCH64 && GET_MODE (x) == DImode)
3308 return CCXNZmode;
3309 else
3310 return CCNZmode;
3311 }
3312 else
3313 {
3314 /* This is for the cmp<mode>_sne pattern. */
3315 if (GET_CODE (x) == NOT && y == constm1_rtx)
3316 {
3317 if (TARGET_ARCH64 && GET_MODE (x) == DImode)
3318 return CCXCmode;
3319 else
3320 return CCCmode;
3321 }
3322
3323 /* This is for the [u]addvdi4_sp32 and [u]subvdi4_sp32 patterns. */
3324 if (!TARGET_ARCH64 && GET_MODE (x) == DImode)
3325 {
3326 if (GET_CODE (y) == UNSPEC
3327 && (XINT (y, 1) == UNSPEC_ADDV
3328 || XINT (y, 1) == UNSPEC_SUBV
3329 || XINT (y, 1) == UNSPEC_NEGV))
3330 return CCVmode;
3331 else
3332 return CCCmode;
3333 }
3334
3335 if (TARGET_ARCH64 && GET_MODE (x) == DImode)
3336 return CCXmode;
3337 else
3338 return CCmode;
3339 }
3340 }
3341
3342 /* Emit the compare insn and return the CC reg for a CODE comparison
3343 with operands X and Y. */
3344
3345 static rtx
3346 gen_compare_reg_1 (enum rtx_code code, rtx x, rtx y)
3347 {
3348 machine_mode mode;
3349 rtx cc_reg;
3350
3351 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_CC)
3352 return x;
3353
3354 mode = SELECT_CC_MODE (code, x, y);
3355
3356 /* ??? We don't have movcc patterns so we cannot generate pseudo regs for the
3357 fcc regs (cse can't tell they're really call clobbered regs and will
3358 remove a duplicate comparison even if there is an intervening function
3359 call - it will then try to reload the cc reg via an int reg which is why
3360 we need the movcc patterns). It is possible to provide the movcc
3361 patterns by using the ldxfsr/stxfsr v9 insns. I tried it: you need two
3362 registers (say %g1,%g5) and it takes about 6 insns. A better fix would be
3363 to tell cse that CCFPE mode registers (even pseudos) are call
3364 clobbered. */
3365
3366 /* ??? This is an experiment. Rather than making changes to cse which may
3367 or may not be easy/clean, we do our own cse. This is possible because
3368 we will generate hard registers. Cse knows they're call clobbered (it
3369 doesn't know the same thing about pseudos). If we guess wrong, no big
3370 deal, but if we win, great! */
3371
3372 if (TARGET_V9 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3373 #if 1 /* experiment */
3374 {
3375 int reg;
3376 /* We cycle through the registers to ensure they're all exercised. */
3377 static int next_fcc_reg = 0;
3378 /* Previous x,y for each fcc reg. */
3379 static rtx prev_args[4][2];
3380
3381 /* Scan prev_args for x,y. */
3382 for (reg = 0; reg < 4; reg++)
3383 if (prev_args[reg][0] == x && prev_args[reg][1] == y)
3384 break;
3385 if (reg == 4)
3386 {
3387 reg = next_fcc_reg;
3388 prev_args[reg][0] = x;
3389 prev_args[reg][1] = y;
3390 next_fcc_reg = (next_fcc_reg + 1) & 3;
3391 }
3392 cc_reg = gen_rtx_REG (mode, reg + SPARC_FIRST_V9_FCC_REG);
3393 }
3394 #else
3395 cc_reg = gen_reg_rtx (mode);
3396 #endif /* ! experiment */
3397 else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3398 cc_reg = gen_rtx_REG (mode, SPARC_FCC_REG);
3399 else
3400 cc_reg = gen_rtx_REG (mode, SPARC_ICC_REG);
3401
3402 /* We shouldn't get there for TFmode if !TARGET_HARD_QUAD. If we do, this
3403 will only result in an unrecognizable insn so no point in asserting. */
3404 emit_insn (gen_rtx_SET (cc_reg, gen_rtx_COMPARE (mode, x, y)));
3405
3406 return cc_reg;
3407 }
3408
3409
3410 /* Emit the compare insn and return the CC reg for the comparison in CMP. */
3411
3412 rtx
3413 gen_compare_reg (rtx cmp)
3414 {
3415 return gen_compare_reg_1 (GET_CODE (cmp), XEXP (cmp, 0), XEXP (cmp, 1));
3416 }
3417
3418 /* This function is used for v9 only.
3419 DEST is the target of the Scc insn.
3420 CODE is the code for an Scc's comparison.
3421 X and Y are the values we compare.
3422
3423 This function is needed to turn
3424
3425 (set (reg:SI 110)
3426 (gt (reg:CCX 100 %icc)
3427 (const_int 0)))
3428 into
3429 (set (reg:SI 110)
3430 (gt:DI (reg:CCX 100 %icc)
3431 (const_int 0)))
3432
3433 IE: The instruction recognizer needs to see the mode of the comparison to
3434 find the right instruction. We could use "gt:DI" right in the
3435 define_expand, but leaving it out allows us to handle DI, SI, etc. */
3436
3437 static int
3438 gen_v9_scc (rtx dest, enum rtx_code compare_code, rtx x, rtx y)
3439 {
3440 if (! TARGET_ARCH64
3441 && (GET_MODE (x) == DImode
3442 || GET_MODE (dest) == DImode))
3443 return 0;
3444
3445 /* Try to use the movrCC insns. */
3446 if (TARGET_ARCH64
3447 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT
3448 && y == const0_rtx
3449 && v9_regcmp_p (compare_code))
3450 {
3451 rtx op0 = x;
3452 rtx temp;
3453
3454 /* Special case for op0 != 0. This can be done with one instruction if
3455 dest == x. */
3456
3457 if (compare_code == NE
3458 && GET_MODE (dest) == DImode
3459 && rtx_equal_p (op0, dest))
3460 {
3461 emit_insn (gen_rtx_SET (dest,
3462 gen_rtx_IF_THEN_ELSE (DImode,
3463 gen_rtx_fmt_ee (compare_code, DImode,
3464 op0, const0_rtx),
3465 const1_rtx,
3466 dest)));
3467 return 1;
3468 }
3469
3470 if (reg_overlap_mentioned_p (dest, op0))
3471 {
3472 /* Handle the case where dest == x.
3473 We "early clobber" the result. */
3474 op0 = gen_reg_rtx (GET_MODE (x));
3475 emit_move_insn (op0, x);
3476 }
3477
3478 emit_insn (gen_rtx_SET (dest, const0_rtx));
3479 if (GET_MODE (op0) != DImode)
3480 {
3481 temp = gen_reg_rtx (DImode);
3482 convert_move (temp, op0, 0);
3483 }
3484 else
3485 temp = op0;
3486 emit_insn (gen_rtx_SET (dest,
3487 gen_rtx_IF_THEN_ELSE (GET_MODE (dest),
3488 gen_rtx_fmt_ee (compare_code, DImode,
3489 temp, const0_rtx),
3490 const1_rtx,
3491 dest)));
3492 return 1;
3493 }
3494 else
3495 {
3496 x = gen_compare_reg_1 (compare_code, x, y);
3497 y = const0_rtx;
3498
3499 emit_insn (gen_rtx_SET (dest, const0_rtx));
3500 emit_insn (gen_rtx_SET (dest,
3501 gen_rtx_IF_THEN_ELSE (GET_MODE (dest),
3502 gen_rtx_fmt_ee (compare_code,
3503 GET_MODE (x), x, y),
3504 const1_rtx, dest)));
3505 return 1;
3506 }
3507 }
3508
3509
3510 /* Emit an scc insn. For seq, sne, sgeu, and sltu, we can do this
3511 without jumps using the addx/subx instructions. */
3512
3513 bool
3514 emit_scc_insn (rtx operands[])
3515 {
3516 rtx tem, x, y;
3517 enum rtx_code code;
3518 machine_mode mode;
3519
3520 /* The quad-word fp compare library routines all return nonzero to indicate
3521 true, which is different from the equivalent libgcc routines, so we must
3522 handle them specially here. */
3523 if (GET_MODE (operands[2]) == TFmode && ! TARGET_HARD_QUAD)
3524 {
3525 operands[1] = sparc_emit_float_lib_cmp (operands[2], operands[3],
3526 GET_CODE (operands[1]));
3527 operands[2] = XEXP (operands[1], 0);
3528 operands[3] = XEXP (operands[1], 1);
3529 }
3530
3531 code = GET_CODE (operands[1]);
3532 x = operands[2];
3533 y = operands[3];
3534 mode = GET_MODE (x);
3535
3536 /* For seq/sne on v9 we use the same code as v8 (the addx/subx method has
3537 more applications). The exception to this is "reg != 0" which can
3538 be done in one instruction on v9 (so we do it). */
3539 if ((code == EQ || code == NE) && (mode == SImode || mode == DImode))
3540 {
3541 if (y != const0_rtx)
3542 x = force_reg (mode, gen_rtx_XOR (mode, x, y));
3543
3544 rtx pat = gen_rtx_SET (operands[0],
3545 gen_rtx_fmt_ee (code, GET_MODE (operands[0]),
3546 x, const0_rtx));
3547
3548 /* If we can use addx/subx or addxc, add a clobber for CC. */
3549 if (mode == SImode || (code == NE && TARGET_VIS3))
3550 {
3551 rtx clobber
3552 = gen_rtx_CLOBBER (VOIDmode,
3553 gen_rtx_REG (mode == SImode ? CCmode : CCXmode,
3554 SPARC_ICC_REG));
3555 pat = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, pat, clobber));
3556 }
3557
3558 emit_insn (pat);
3559 return true;
3560 }
3561
3562 /* We can do LTU in DImode using the addxc instruction with VIS3. */
3563 if (TARGET_ARCH64
3564 && mode == DImode
3565 && !((code == LTU || code == GTU) && TARGET_VIS3)
3566 && gen_v9_scc (operands[0], code, x, y))
3567 return true;
3568
3569 /* We can do LTU and GEU using the addx/subx instructions too. And
3570 for GTU/LEU, if both operands are registers swap them and fall
3571 back to the easy case. */
3572 if (code == GTU || code == LEU)
3573 {
3574 if ((GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
3575 && (GET_CODE (y) == REG || GET_CODE (y) == SUBREG))
3576 {
3577 tem = x;
3578 x = y;
3579 y = tem;
3580 code = swap_condition (code);
3581 }
3582 }
3583
3584 if (code == LTU || code == GEU)
3585 {
3586 emit_insn (gen_rtx_SET (operands[0],
3587 gen_rtx_fmt_ee (code, GET_MODE (operands[0]),
3588 gen_compare_reg_1 (code, x, y),
3589 const0_rtx)));
3590 return true;
3591 }
3592
3593 /* All the posibilities to use addx/subx based sequences has been
3594 exhausted, try for a 3 instruction sequence using v9 conditional
3595 moves. */
3596 if (TARGET_V9 && gen_v9_scc (operands[0], code, x, y))
3597 return true;
3598
3599 /* Nope, do branches. */
3600 return false;
3601 }
3602
3603 /* Emit a conditional jump insn for the v9 architecture using comparison code
3604 CODE and jump target LABEL.
3605 This function exists to take advantage of the v9 brxx insns. */
3606
3607 static void
3608 emit_v9_brxx_insn (enum rtx_code code, rtx op0, rtx label)
3609 {
3610 emit_jump_insn (gen_rtx_SET (pc_rtx,
3611 gen_rtx_IF_THEN_ELSE (VOIDmode,
3612 gen_rtx_fmt_ee (code, GET_MODE (op0),
3613 op0, const0_rtx),
3614 gen_rtx_LABEL_REF (VOIDmode, label),
3615 pc_rtx)));
3616 }
3617
3618 /* Emit a conditional jump insn for the UA2011 architecture using
3619 comparison code CODE and jump target LABEL. This function exists
3620 to take advantage of the UA2011 Compare and Branch insns. */
3621
3622 static void
3623 emit_cbcond_insn (enum rtx_code code, rtx op0, rtx op1, rtx label)
3624 {
3625 rtx if_then_else;
3626
3627 if_then_else = gen_rtx_IF_THEN_ELSE (VOIDmode,
3628 gen_rtx_fmt_ee(code, GET_MODE(op0),
3629 op0, op1),
3630 gen_rtx_LABEL_REF (VOIDmode, label),
3631 pc_rtx);
3632
3633 emit_jump_insn (gen_rtx_SET (pc_rtx, if_then_else));
3634 }
3635
3636 void
3637 emit_conditional_branch_insn (rtx operands[])
3638 {
3639 /* The quad-word fp compare library routines all return nonzero to indicate
3640 true, which is different from the equivalent libgcc routines, so we must
3641 handle them specially here. */
3642 if (GET_MODE (operands[1]) == TFmode && ! TARGET_HARD_QUAD)
3643 {
3644 operands[0] = sparc_emit_float_lib_cmp (operands[1], operands[2],
3645 GET_CODE (operands[0]));
3646 operands[1] = XEXP (operands[0], 0);
3647 operands[2] = XEXP (operands[0], 1);
3648 }
3649
3650 /* If we can tell early on that the comparison is against a constant
3651 that won't fit in the 5-bit signed immediate field of a cbcond,
3652 use one of the other v9 conditional branch sequences. */
3653 if (TARGET_CBCOND
3654 && GET_CODE (operands[1]) == REG
3655 && (GET_MODE (operands[1]) == SImode
3656 || (TARGET_ARCH64 && GET_MODE (operands[1]) == DImode))
3657 && (GET_CODE (operands[2]) != CONST_INT
3658 || SPARC_SIMM5_P (INTVAL (operands[2]))))
3659 {
3660 emit_cbcond_insn (GET_CODE (operands[0]), operands[1], operands[2], operands[3]);
3661 return;
3662 }
3663
3664 if (TARGET_ARCH64 && operands[2] == const0_rtx
3665 && GET_CODE (operands[1]) == REG
3666 && GET_MODE (operands[1]) == DImode)
3667 {
3668 emit_v9_brxx_insn (GET_CODE (operands[0]), operands[1], operands[3]);
3669 return;
3670 }
3671
3672 operands[1] = gen_compare_reg (operands[0]);
3673 operands[2] = const0_rtx;
3674 operands[0] = gen_rtx_fmt_ee (GET_CODE (operands[0]), VOIDmode,
3675 operands[1], operands[2]);
3676 emit_jump_insn (gen_cbranchcc4 (operands[0], operands[1], operands[2],
3677 operands[3]));
3678 }
3679
3680
3681 /* Generate a DFmode part of a hard TFmode register.
3682 REG is the TFmode hard register, LOW is 1 for the
3683 low 64bit of the register and 0 otherwise.
3684 */
3685 rtx
3686 gen_df_reg (rtx reg, int low)
3687 {
3688 int regno = REGNO (reg);
3689
3690 if ((WORDS_BIG_ENDIAN == 0) ^ (low != 0))
3691 regno += (TARGET_ARCH64 && SPARC_INT_REG_P (regno)) ? 1 : 2;
3692 return gen_rtx_REG (DFmode, regno);
3693 }
3694
3695 /* Generate a call to FUNC with OPERANDS. Operand 0 is the return value.
3696 Unlike normal calls, TFmode operands are passed by reference. It is
3697 assumed that no more than 3 operands are required. */
3698
3699 static void
3700 emit_soft_tfmode_libcall (const char *func_name, int nargs, rtx *operands)
3701 {
3702 rtx ret_slot = NULL, arg[3], func_sym;
3703 int i;
3704
3705 /* We only expect to be called for conversions, unary, and binary ops. */
3706 gcc_assert (nargs == 2 || nargs == 3);
3707
3708 for (i = 0; i < nargs; ++i)
3709 {
3710 rtx this_arg = operands[i];
3711 rtx this_slot;
3712
3713 /* TFmode arguments and return values are passed by reference. */
3714 if (GET_MODE (this_arg) == TFmode)
3715 {
3716 int force_stack_temp;
3717
3718 force_stack_temp = 0;
3719 if (TARGET_BUGGY_QP_LIB && i == 0)
3720 force_stack_temp = 1;
3721
3722 if (GET_CODE (this_arg) == MEM
3723 && ! force_stack_temp)
3724 {
3725 tree expr = MEM_EXPR (this_arg);
3726 if (expr)
3727 mark_addressable (expr);
3728 this_arg = XEXP (this_arg, 0);
3729 }
3730 else if (CONSTANT_P (this_arg)
3731 && ! force_stack_temp)
3732 {
3733 this_slot = force_const_mem (TFmode, this_arg);
3734 this_arg = XEXP (this_slot, 0);
3735 }
3736 else
3737 {
3738 this_slot = assign_stack_temp (TFmode, GET_MODE_SIZE (TFmode));
3739
3740 /* Operand 0 is the return value. We'll copy it out later. */
3741 if (i > 0)
3742 emit_move_insn (this_slot, this_arg);
3743 else
3744 ret_slot = this_slot;
3745
3746 this_arg = XEXP (this_slot, 0);
3747 }
3748 }
3749
3750 arg[i] = this_arg;
3751 }
3752
3753 func_sym = gen_rtx_SYMBOL_REF (Pmode, func_name);
3754
3755 if (GET_MODE (operands[0]) == TFmode)
3756 {
3757 if (nargs == 2)
3758 emit_library_call (func_sym, LCT_NORMAL, VOIDmode,
3759 arg[0], GET_MODE (arg[0]),
3760 arg[1], GET_MODE (arg[1]));
3761 else
3762 emit_library_call (func_sym, LCT_NORMAL, VOIDmode,
3763 arg[0], GET_MODE (arg[0]),
3764 arg[1], GET_MODE (arg[1]),
3765 arg[2], GET_MODE (arg[2]));
3766
3767 if (ret_slot)
3768 emit_move_insn (operands[0], ret_slot);
3769 }
3770 else
3771 {
3772 rtx ret;
3773
3774 gcc_assert (nargs == 2);
3775
3776 ret = emit_library_call_value (func_sym, operands[0], LCT_NORMAL,
3777 GET_MODE (operands[0]),
3778 arg[1], GET_MODE (arg[1]));
3779
3780 if (ret != operands[0])
3781 emit_move_insn (operands[0], ret);
3782 }
3783 }
3784
3785 /* Expand soft-float TFmode calls to sparc abi routines. */
3786
3787 static void
3788 emit_soft_tfmode_binop (enum rtx_code code, rtx *operands)
3789 {
3790 const char *func;
3791
3792 switch (code)
3793 {
3794 case PLUS:
3795 func = "_Qp_add";
3796 break;
3797 case MINUS:
3798 func = "_Qp_sub";
3799 break;
3800 case MULT:
3801 func = "_Qp_mul";
3802 break;
3803 case DIV:
3804 func = "_Qp_div";
3805 break;
3806 default:
3807 gcc_unreachable ();
3808 }
3809
3810 emit_soft_tfmode_libcall (func, 3, operands);
3811 }
3812
3813 static void
3814 emit_soft_tfmode_unop (enum rtx_code code, rtx *operands)
3815 {
3816 const char *func;
3817
3818 gcc_assert (code == SQRT);
3819 func = "_Qp_sqrt";
3820
3821 emit_soft_tfmode_libcall (func, 2, operands);
3822 }
3823
3824 static void
3825 emit_soft_tfmode_cvt (enum rtx_code code, rtx *operands)
3826 {
3827 const char *func;
3828
3829 switch (code)
3830 {
3831 case FLOAT_EXTEND:
3832 switch (GET_MODE (operands[1]))
3833 {
3834 case E_SFmode:
3835 func = "_Qp_stoq";
3836 break;
3837 case E_DFmode:
3838 func = "_Qp_dtoq";
3839 break;
3840 default:
3841 gcc_unreachable ();
3842 }
3843 break;
3844
3845 case FLOAT_TRUNCATE:
3846 switch (GET_MODE (operands[0]))
3847 {
3848 case E_SFmode:
3849 func = "_Qp_qtos";
3850 break;
3851 case E_DFmode:
3852 func = "_Qp_qtod";
3853 break;
3854 default:
3855 gcc_unreachable ();
3856 }
3857 break;
3858
3859 case FLOAT:
3860 switch (GET_MODE (operands[1]))
3861 {
3862 case E_SImode:
3863 func = "_Qp_itoq";
3864 if (TARGET_ARCH64)
3865 operands[1] = gen_rtx_SIGN_EXTEND (DImode, operands[1]);
3866 break;
3867 case E_DImode:
3868 func = "_Qp_xtoq";
3869 break;
3870 default:
3871 gcc_unreachable ();
3872 }
3873 break;
3874
3875 case UNSIGNED_FLOAT:
3876 switch (GET_MODE (operands[1]))
3877 {
3878 case E_SImode:
3879 func = "_Qp_uitoq";
3880 if (TARGET_ARCH64)
3881 operands[1] = gen_rtx_ZERO_EXTEND (DImode, operands[1]);
3882 break;
3883 case E_DImode:
3884 func = "_Qp_uxtoq";
3885 break;
3886 default:
3887 gcc_unreachable ();
3888 }
3889 break;
3890
3891 case FIX:
3892 switch (GET_MODE (operands[0]))
3893 {
3894 case E_SImode:
3895 func = "_Qp_qtoi";
3896 break;
3897 case E_DImode:
3898 func = "_Qp_qtox";
3899 break;
3900 default:
3901 gcc_unreachable ();
3902 }
3903 break;
3904
3905 case UNSIGNED_FIX:
3906 switch (GET_MODE (operands[0]))
3907 {
3908 case E_SImode:
3909 func = "_Qp_qtoui";
3910 break;
3911 case E_DImode:
3912 func = "_Qp_qtoux";
3913 break;
3914 default:
3915 gcc_unreachable ();
3916 }
3917 break;
3918
3919 default:
3920 gcc_unreachable ();
3921 }
3922
3923 emit_soft_tfmode_libcall (func, 2, operands);
3924 }
3925
3926 /* Expand a hard-float tfmode operation. All arguments must be in
3927 registers. */
3928
3929 static void
3930 emit_hard_tfmode_operation (enum rtx_code code, rtx *operands)
3931 {
3932 rtx op, dest;
3933
3934 if (GET_RTX_CLASS (code) == RTX_UNARY)
3935 {
3936 operands[1] = force_reg (GET_MODE (operands[1]), operands[1]);
3937 op = gen_rtx_fmt_e (code, GET_MODE (operands[0]), operands[1]);
3938 }
3939 else
3940 {
3941 operands[1] = force_reg (GET_MODE (operands[1]), operands[1]);
3942 operands[2] = force_reg (GET_MODE (operands[2]), operands[2]);
3943 op = gen_rtx_fmt_ee (code, GET_MODE (operands[0]),
3944 operands[1], operands[2]);
3945 }
3946
3947 if (register_operand (operands[0], VOIDmode))
3948 dest = operands[0];
3949 else
3950 dest = gen_reg_rtx (GET_MODE (operands[0]));
3951
3952 emit_insn (gen_rtx_SET (dest, op));
3953
3954 if (dest != operands[0])
3955 emit_move_insn (operands[0], dest);
3956 }
3957
3958 void
3959 emit_tfmode_binop (enum rtx_code code, rtx *operands)
3960 {
3961 if (TARGET_HARD_QUAD)
3962 emit_hard_tfmode_operation (code, operands);
3963 else
3964 emit_soft_tfmode_binop (code, operands);
3965 }
3966
3967 void
3968 emit_tfmode_unop (enum rtx_code code, rtx *operands)
3969 {
3970 if (TARGET_HARD_QUAD)
3971 emit_hard_tfmode_operation (code, operands);
3972 else
3973 emit_soft_tfmode_unop (code, operands);
3974 }
3975
3976 void
3977 emit_tfmode_cvt (enum rtx_code code, rtx *operands)
3978 {
3979 if (TARGET_HARD_QUAD)
3980 emit_hard_tfmode_operation (code, operands);
3981 else
3982 emit_soft_tfmode_cvt (code, operands);
3983 }
3984
3985 /* Return nonzero if a branch/jump/call instruction will be emitting
3986 nop into its delay slot. */
3987
3988 int
3989 empty_delay_slot (rtx_insn *insn)
3990 {
3991 rtx seq;
3992
3993 /* If no previous instruction (should not happen), return true. */
3994 if (PREV_INSN (insn) == NULL)
3995 return 1;
3996
3997 seq = NEXT_INSN (PREV_INSN (insn));
3998 if (GET_CODE (PATTERN (seq)) == SEQUENCE)
3999 return 0;
4000
4001 return 1;
4002 }
4003
4004 /* Return nonzero if we should emit a nop after a cbcond instruction.
4005 The cbcond instruction does not have a delay slot, however there is
4006 a severe performance penalty if a control transfer appears right
4007 after a cbcond. Therefore we emit a nop when we detect this
4008 situation. */
4009
4010 int
4011 emit_cbcond_nop (rtx_insn *insn)
4012 {
4013 rtx next = next_active_insn (insn);
4014
4015 if (!next)
4016 return 1;
4017
4018 if (NONJUMP_INSN_P (next)
4019 && GET_CODE (PATTERN (next)) == SEQUENCE)
4020 next = XVECEXP (PATTERN (next), 0, 0);
4021 else if (CALL_P (next)
4022 && GET_CODE (PATTERN (next)) == PARALLEL)
4023 {
4024 rtx delay = XVECEXP (PATTERN (next), 0, 1);
4025
4026 if (GET_CODE (delay) == RETURN)
4027 {
4028 /* It's a sibling call. Do not emit the nop if we're going
4029 to emit something other than the jump itself as the first
4030 instruction of the sibcall sequence. */
4031 if (sparc_leaf_function_p || TARGET_FLAT)
4032 return 0;
4033 }
4034 }
4035
4036 if (NONJUMP_INSN_P (next))
4037 return 0;
4038
4039 return 1;
4040 }
4041
4042 /* Return nonzero if TRIAL, an insn, can be combined with a 'restore'
4043 instruction. RETURN_P is true if the v9 variant 'return' is to be
4044 considered in the test too.
4045
4046 TRIAL must be a SET whose destination is a REG appropriate for the
4047 'restore' instruction or, if RETURN_P is true, for the 'return'
4048 instruction. */
4049
4050 static int
4051 eligible_for_restore_insn (rtx trial, bool return_p)
4052 {
4053 rtx pat = PATTERN (trial);
4054 rtx src = SET_SRC (pat);
4055 bool src_is_freg = false;
4056 rtx src_reg;
4057
4058 /* Since we now can do moves between float and integer registers when
4059 VIS3 is enabled, we have to catch this case. We can allow such
4060 moves when doing a 'return' however. */
4061 src_reg = src;
4062 if (GET_CODE (src_reg) == SUBREG)
4063 src_reg = SUBREG_REG (src_reg);
4064 if (GET_CODE (src_reg) == REG
4065 && SPARC_FP_REG_P (REGNO (src_reg)))
4066 src_is_freg = true;
4067
4068 /* The 'restore src,%g0,dest' pattern for word mode and below. */
4069 if (GET_MODE_CLASS (GET_MODE (src)) != MODE_FLOAT
4070 && arith_operand (src, GET_MODE (src))
4071 && ! src_is_freg)
4072 {
4073 if (TARGET_ARCH64)
4074 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
4075 else
4076 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (SImode);
4077 }
4078
4079 /* The 'restore src,%g0,dest' pattern for double-word mode. */
4080 else if (GET_MODE_CLASS (GET_MODE (src)) != MODE_FLOAT
4081 && arith_double_operand (src, GET_MODE (src))
4082 && ! src_is_freg)
4083 return GET_MODE_SIZE (GET_MODE (src)) <= GET_MODE_SIZE (DImode);
4084
4085 /* The 'restore src,%g0,dest' pattern for float if no FPU. */
4086 else if (! TARGET_FPU && register_operand (src, SFmode))
4087 return 1;
4088
4089 /* The 'restore src,%g0,dest' pattern for double if no FPU. */
4090 else if (! TARGET_FPU && TARGET_ARCH64 && register_operand (src, DFmode))
4091 return 1;
4092
4093 /* If we have the 'return' instruction, anything that does not use
4094 local or output registers and can go into a delay slot wins. */
4095 else if (return_p && TARGET_V9 && !epilogue_renumber (&pat, 1))
4096 return 1;
4097
4098 /* The 'restore src1,src2,dest' pattern for SImode. */
4099 else if (GET_CODE (src) == PLUS
4100 && register_operand (XEXP (src, 0), SImode)
4101 && arith_operand (XEXP (src, 1), SImode))
4102 return 1;
4103
4104 /* The 'restore src1,src2,dest' pattern for DImode. */
4105 else if (GET_CODE (src) == PLUS
4106 && register_operand (XEXP (src, 0), DImode)
4107 && arith_double_operand (XEXP (src, 1), DImode))
4108 return 1;
4109
4110 /* The 'restore src1,%lo(src2),dest' pattern. */
4111 else if (GET_CODE (src) == LO_SUM
4112 && ! TARGET_CM_MEDMID
4113 && ((register_operand (XEXP (src, 0), SImode)
4114 && immediate_operand (XEXP (src, 1), SImode))
4115 || (TARGET_ARCH64
4116 && register_operand (XEXP (src, 0), DImode)
4117 && immediate_operand (XEXP (src, 1), DImode))))
4118 return 1;
4119
4120 /* The 'restore src,src,dest' pattern. */
4121 else if (GET_CODE (src) == ASHIFT
4122 && (register_operand (XEXP (src, 0), SImode)
4123 || register_operand (XEXP (src, 0), DImode))
4124 && XEXP (src, 1) == const1_rtx)
4125 return 1;
4126
4127 return 0;
4128 }
4129
4130 /* Return nonzero if TRIAL can go into the function return's delay slot. */
4131
4132 int
4133 eligible_for_return_delay (rtx_insn *trial)
4134 {
4135 int regno;
4136 rtx pat;
4137
4138 /* If the function uses __builtin_eh_return, the eh_return machinery
4139 occupies the delay slot. */
4140 if (crtl->calls_eh_return)
4141 return 0;
4142
4143 if (get_attr_in_branch_delay (trial) == IN_BRANCH_DELAY_FALSE)
4144 return 0;
4145
4146 /* In the case of a leaf or flat function, anything can go into the slot. */
4147 if (sparc_leaf_function_p || TARGET_FLAT)
4148 return 1;
4149
4150 if (!NONJUMP_INSN_P (trial))
4151 return 0;
4152
4153 pat = PATTERN (trial);
4154 if (GET_CODE (pat) == PARALLEL)
4155 {
4156 int i;
4157
4158 if (! TARGET_V9)
4159 return 0;
4160 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
4161 {
4162 rtx expr = XVECEXP (pat, 0, i);
4163 if (GET_CODE (expr) != SET)
4164 return 0;
4165 if (GET_CODE (SET_DEST (expr)) != REG)
4166 return 0;
4167 regno = REGNO (SET_DEST (expr));
4168 if (regno >= 8 && regno < 24)
4169 return 0;
4170 }
4171 return !epilogue_renumber (&pat, 1);
4172 }
4173
4174 if (GET_CODE (pat) != SET)
4175 return 0;
4176
4177 if (GET_CODE (SET_DEST (pat)) != REG)
4178 return 0;
4179
4180 regno = REGNO (SET_DEST (pat));
4181
4182 /* Otherwise, only operations which can be done in tandem with
4183 a `restore' or `return' insn can go into the delay slot. */
4184 if (regno >= 8 && regno < 24)
4185 return 0;
4186
4187 /* If this instruction sets up floating point register and we have a return
4188 instruction, it can probably go in. But restore will not work
4189 with FP_REGS. */
4190 if (! SPARC_INT_REG_P (regno))
4191 return TARGET_V9 && !epilogue_renumber (&pat, 1);
4192
4193 return eligible_for_restore_insn (trial, true);
4194 }
4195
4196 /* Return nonzero if TRIAL can go into the sibling call's delay slot. */
4197
4198 int
4199 eligible_for_sibcall_delay (rtx_insn *trial)
4200 {
4201 rtx pat;
4202
4203 if (get_attr_in_branch_delay (trial) == IN_BRANCH_DELAY_FALSE)
4204 return 0;
4205
4206 if (!NONJUMP_INSN_P (trial))
4207 return 0;
4208
4209 pat = PATTERN (trial);
4210
4211 if (sparc_leaf_function_p || TARGET_FLAT)
4212 {
4213 /* If the tail call is done using the call instruction,
4214 we have to restore %o7 in the delay slot. */
4215 if (LEAF_SIBCALL_SLOT_RESERVED_P)
4216 return 0;
4217
4218 /* %g1 is used to build the function address */
4219 if (reg_mentioned_p (gen_rtx_REG (Pmode, 1), pat))
4220 return 0;
4221
4222 return 1;
4223 }
4224
4225 if (GET_CODE (pat) != SET)
4226 return 0;
4227
4228 /* Otherwise, only operations which can be done in tandem with
4229 a `restore' insn can go into the delay slot. */
4230 if (GET_CODE (SET_DEST (pat)) != REG
4231 || (REGNO (SET_DEST (pat)) >= 8 && REGNO (SET_DEST (pat)) < 24)
4232 || ! SPARC_INT_REG_P (REGNO (SET_DEST (pat))))
4233 return 0;
4234
4235 /* If it mentions %o7, it can't go in, because sibcall will clobber it
4236 in most cases. */
4237 if (reg_mentioned_p (gen_rtx_REG (Pmode, 15), pat))
4238 return 0;
4239
4240 return eligible_for_restore_insn (trial, false);
4241 }
4242
4243 /* Determine if it's legal to put X into the constant pool. This
4244 is not possible if X contains the address of a symbol that is
4245 not constant (TLS) or not known at final link time (PIC). */
4246
4247 static bool
4248 sparc_cannot_force_const_mem (machine_mode mode, rtx x)
4249 {
4250 switch (GET_CODE (x))
4251 {
4252 case CONST_INT:
4253 case CONST_WIDE_INT:
4254 case CONST_DOUBLE:
4255 case CONST_VECTOR:
4256 /* Accept all non-symbolic constants. */
4257 return false;
4258
4259 case LABEL_REF:
4260 /* Labels are OK iff we are non-PIC. */
4261 return flag_pic != 0;
4262
4263 case SYMBOL_REF:
4264 /* 'Naked' TLS symbol references are never OK,
4265 non-TLS symbols are OK iff we are non-PIC. */
4266 if (SYMBOL_REF_TLS_MODEL (x))
4267 return true;
4268 else
4269 return flag_pic != 0;
4270
4271 case CONST:
4272 return sparc_cannot_force_const_mem (mode, XEXP (x, 0));
4273 case PLUS:
4274 case MINUS:
4275 return sparc_cannot_force_const_mem (mode, XEXP (x, 0))
4276 || sparc_cannot_force_const_mem (mode, XEXP (x, 1));
4277 case UNSPEC:
4278 return true;
4279 default:
4280 gcc_unreachable ();
4281 }
4282 }
4283
4284 /* Global Offset Table support. */
4285 static GTY(()) rtx got_symbol_rtx = NULL_RTX;
4286 static GTY(()) rtx got_register_rtx = NULL_RTX;
4287 static GTY(()) rtx got_helper_rtx = NULL_RTX;
4288
4289 static GTY(()) bool got_helper_needed = false;
4290
4291 /* Return the SYMBOL_REF for the Global Offset Table. */
4292
4293 static rtx
4294 sparc_got (void)
4295 {
4296 if (!got_symbol_rtx)
4297 got_symbol_rtx = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
4298
4299 return got_symbol_rtx;
4300 }
4301
4302 /* Output the load_pcrel_sym pattern. */
4303
4304 const char *
4305 output_load_pcrel_sym (rtx *operands)
4306 {
4307 if (flag_delayed_branch)
4308 {
4309 output_asm_insn ("sethi\t%%hi(%a1-4), %0", operands);
4310 output_asm_insn ("call\t%a2", operands);
4311 output_asm_insn (" add\t%0, %%lo(%a1+4), %0", operands);
4312 }
4313 else
4314 {
4315 output_asm_insn ("sethi\t%%hi(%a1-8), %0", operands);
4316 output_asm_insn ("add\t%0, %%lo(%a1-4), %0", operands);
4317 output_asm_insn ("call\t%a2", operands);
4318 output_asm_insn (" nop", NULL);
4319 }
4320
4321 if (operands[2] == got_helper_rtx)
4322 got_helper_needed = true;
4323
4324 return "";
4325 }
4326
4327 #ifdef HAVE_GAS_HIDDEN
4328 # define USE_HIDDEN_LINKONCE 1
4329 #else
4330 # define USE_HIDDEN_LINKONCE 0
4331 #endif
4332
4333 /* Emit code to load the GOT register. */
4334
4335 void
4336 load_got_register (void)
4337 {
4338 rtx insn;
4339
4340 if (TARGET_VXWORKS_RTP)
4341 {
4342 if (!got_register_rtx)
4343 got_register_rtx = pic_offset_table_rtx;
4344
4345 insn = gen_vxworks_load_got ();
4346 }
4347 else
4348 {
4349 if (!got_register_rtx)
4350 got_register_rtx = gen_rtx_REG (Pmode, GLOBAL_OFFSET_TABLE_REGNUM);
4351
4352 /* The GOT symbol is subject to a PC-relative relocation so we need a
4353 helper function to add the PC value and thus get the final value. */
4354 if (!got_helper_rtx)
4355 {
4356 char name[32];
4357
4358 /* Skip the leading '%' as that cannot be used in a symbol name. */
4359 if (USE_HIDDEN_LINKONCE)
4360 sprintf (name, "__sparc_get_pc_thunk.%s",
4361 reg_names[REGNO (got_register_rtx)] + 1);
4362 else
4363 ASM_GENERATE_INTERNAL_LABEL (name, "LADDPC",
4364 REGNO (got_register_rtx));
4365
4366 got_helper_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
4367 }
4368
4369 /* The load_pcrel_sym{si,di} patterns require absolute addressing. */
4370 const int orig_flag_pic = flag_pic;
4371 flag_pic = 0;
4372 insn = gen_load_pcrel_sym (Pmode,
4373 got_register_rtx,
4374 sparc_got (),
4375 got_helper_rtx,
4376 GEN_INT (GLOBAL_OFFSET_TABLE_REGNUM));
4377 flag_pic = orig_flag_pic;
4378 }
4379
4380 emit_insn (insn);
4381 }
4382
4383 /* Ensure that we are not using patterns that are not OK with PIC. */
4384
4385 int
4386 check_pic (int i)
4387 {
4388 rtx op;
4389
4390 switch (flag_pic)
4391 {
4392 case 1:
4393 op = recog_data.operand[i];
4394 gcc_assert (GET_CODE (op) != SYMBOL_REF
4395 && (GET_CODE (op) != CONST
4396 || (GET_CODE (XEXP (op, 0)) == MINUS
4397 && XEXP (XEXP (op, 0), 0) == sparc_got ()
4398 && GET_CODE (XEXP (XEXP (op, 0), 1)) == CONST)));
4399 /* fallthrough */
4400 case 2:
4401 default:
4402 return 1;
4403 }
4404 }
4405
4406 /* Return true if X is an address which needs a temporary register when
4407 reloaded while generating PIC code. */
4408
4409 int
4410 pic_address_needs_scratch (rtx x)
4411 {
4412 /* An address which is a symbolic plus a non SMALL_INT needs a temp reg. */
4413 if (GET_CODE (x) == CONST
4414 && GET_CODE (XEXP (x, 0)) == PLUS
4415 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
4416 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4417 && !SMALL_INT (XEXP (XEXP (x, 0), 1)))
4418 return 1;
4419
4420 return 0;
4421 }
4422
4423 /* Determine if a given RTX is a valid constant. We already know this
4424 satisfies CONSTANT_P. */
4425
4426 static bool
4427 sparc_legitimate_constant_p (machine_mode mode, rtx x)
4428 {
4429 switch (GET_CODE (x))
4430 {
4431 case CONST:
4432 case SYMBOL_REF:
4433 if (sparc_tls_referenced_p (x))
4434 return false;
4435 break;
4436
4437 case CONST_DOUBLE:
4438 /* Floating point constants are generally not ok.
4439 The only exception is 0.0 and all-ones in VIS. */
4440 if (TARGET_VIS
4441 && SCALAR_FLOAT_MODE_P (mode)
4442 && (const_zero_operand (x, mode)
4443 || const_all_ones_operand (x, mode)))
4444 return true;
4445
4446 return false;
4447
4448 case CONST_VECTOR:
4449 /* Vector constants are generally not ok.
4450 The only exception is 0 or -1 in VIS. */
4451 if (TARGET_VIS
4452 && (const_zero_operand (x, mode)
4453 || const_all_ones_operand (x, mode)))
4454 return true;
4455
4456 return false;
4457
4458 default:
4459 break;
4460 }
4461
4462 return true;
4463 }
4464
4465 /* Determine if a given RTX is a valid constant address. */
4466
4467 bool
4468 constant_address_p (rtx x)
4469 {
4470 switch (GET_CODE (x))
4471 {
4472 case LABEL_REF:
4473 case CONST_INT:
4474 case HIGH:
4475 return true;
4476
4477 case CONST:
4478 if (flag_pic && pic_address_needs_scratch (x))
4479 return false;
4480 return sparc_legitimate_constant_p (Pmode, x);
4481
4482 case SYMBOL_REF:
4483 return !flag_pic && sparc_legitimate_constant_p (Pmode, x);
4484
4485 default:
4486 return false;
4487 }
4488 }
4489
4490 /* Nonzero if the constant value X is a legitimate general operand
4491 when generating PIC code. It is given that flag_pic is on and
4492 that X satisfies CONSTANT_P. */
4493
4494 bool
4495 legitimate_pic_operand_p (rtx x)
4496 {
4497 if (pic_address_needs_scratch (x))
4498 return false;
4499 if (sparc_tls_referenced_p (x))
4500 return false;
4501 return true;
4502 }
4503
4504 /* Return true if X is a representation of the PIC register. */
4505
4506 static bool
4507 sparc_pic_register_p (rtx x)
4508 {
4509 if (!REG_P (x) || !pic_offset_table_rtx)
4510 return false;
4511
4512 if (x == pic_offset_table_rtx)
4513 return true;
4514
4515 if (!HARD_REGISTER_P (pic_offset_table_rtx)
4516 && (HARD_REGISTER_P (x) || lra_in_progress || reload_in_progress)
4517 && ORIGINAL_REGNO (x) == REGNO (pic_offset_table_rtx))
4518 return true;
4519
4520 return false;
4521 }
4522
4523 #define RTX_OK_FOR_OFFSET_P(X, MODE) \
4524 (CONST_INT_P (X) \
4525 && INTVAL (X) >= -0x1000 \
4526 && INTVAL (X) <= (0x1000 - GET_MODE_SIZE (MODE)))
4527
4528 #define RTX_OK_FOR_OLO10_P(X, MODE) \
4529 (CONST_INT_P (X) \
4530 && INTVAL (X) >= -0x1000 \
4531 && INTVAL (X) <= (0xc00 - GET_MODE_SIZE (MODE)))
4532
4533 /* Handle the TARGET_LEGITIMATE_ADDRESS_P target hook.
4534
4535 On SPARC, the actual legitimate addresses must be REG+REG or REG+SMALLINT
4536 ordinarily. This changes a bit when generating PIC. */
4537
4538 static bool
4539 sparc_legitimate_address_p (machine_mode mode, rtx addr, bool strict,
4540 code_helper)
4541 {
4542 rtx rs1 = NULL, rs2 = NULL, imm1 = NULL;
4543
4544 if (REG_P (addr) || GET_CODE (addr) == SUBREG)
4545 rs1 = addr;
4546 else if (GET_CODE (addr) == PLUS)
4547 {
4548 rs1 = XEXP (addr, 0);
4549 rs2 = XEXP (addr, 1);
4550
4551 /* Canonicalize. REG comes first, if there are no regs,
4552 LO_SUM comes first. */
4553 if (!REG_P (rs1)
4554 && GET_CODE (rs1) != SUBREG
4555 && (REG_P (rs2)
4556 || GET_CODE (rs2) == SUBREG
4557 || (GET_CODE (rs2) == LO_SUM && GET_CODE (rs1) != LO_SUM)))
4558 {
4559 rs1 = XEXP (addr, 1);
4560 rs2 = XEXP (addr, 0);
4561 }
4562
4563 if ((flag_pic == 1
4564 && sparc_pic_register_p (rs1)
4565 && !REG_P (rs2)
4566 && GET_CODE (rs2) != SUBREG
4567 && GET_CODE (rs2) != LO_SUM
4568 && GET_CODE (rs2) != MEM
4569 && !(GET_CODE (rs2) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs2))
4570 && (! symbolic_operand (rs2, VOIDmode) || mode == Pmode)
4571 && (GET_CODE (rs2) != CONST_INT || SMALL_INT (rs2)))
4572 || ((REG_P (rs1)
4573 || GET_CODE (rs1) == SUBREG)
4574 && RTX_OK_FOR_OFFSET_P (rs2, mode)))
4575 {
4576 imm1 = rs2;
4577 rs2 = NULL;
4578 }
4579 else if ((REG_P (rs1) || GET_CODE (rs1) == SUBREG)
4580 && (REG_P (rs2) || GET_CODE (rs2) == SUBREG))
4581 {
4582 /* We prohibit REG + REG for TFmode when there are no quad move insns
4583 and we consequently need to split. We do this because REG+REG
4584 is not an offsettable address. If we get the situation in reload
4585 where source and destination of a movtf pattern are both MEMs with
4586 REG+REG address, then only one of them gets converted to an
4587 offsettable address. */
4588 if (mode == TFmode
4589 && ! (TARGET_ARCH64 && TARGET_HARD_QUAD))
4590 return 0;
4591
4592 /* Likewise for TImode, but in all cases. */
4593 if (mode == TImode)
4594 return 0;
4595
4596 /* We prohibit REG + REG on ARCH32 if not optimizing for
4597 DFmode/DImode because then mem_min_alignment is likely to be zero
4598 after reload and the forced split would lack a matching splitter
4599 pattern. */
4600 if (TARGET_ARCH32 && !optimize
4601 && (mode == DFmode || mode == DImode))
4602 return 0;
4603 }
4604 else if (USE_AS_OFFSETABLE_LO10
4605 && GET_CODE (rs1) == LO_SUM
4606 && TARGET_ARCH64
4607 && ! TARGET_CM_MEDMID
4608 && RTX_OK_FOR_OLO10_P (rs2, mode))
4609 {
4610 rs2 = NULL;
4611 imm1 = XEXP (rs1, 1);
4612 rs1 = XEXP (rs1, 0);
4613 if (!CONSTANT_P (imm1)
4614 || (GET_CODE (rs1) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs1)))
4615 return 0;
4616 }
4617 }
4618 else if (GET_CODE (addr) == LO_SUM)
4619 {
4620 rs1 = XEXP (addr, 0);
4621 imm1 = XEXP (addr, 1);
4622
4623 if (!CONSTANT_P (imm1)
4624 || (GET_CODE (rs1) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (rs1)))
4625 return 0;
4626
4627 /* We can't allow TFmode in 32-bit mode, because an offset greater
4628 than the alignment (8) may cause the LO_SUM to overflow. */
4629 if (mode == TFmode && TARGET_ARCH32)
4630 return 0;
4631
4632 /* During reload, accept the HIGH+LO_SUM construct generated by
4633 sparc_legitimize_reload_address. */
4634 if (reload_in_progress
4635 && GET_CODE (rs1) == HIGH
4636 && XEXP (rs1, 0) == imm1)
4637 return 1;
4638 }
4639 else if (GET_CODE (addr) == CONST_INT && SMALL_INT (addr))
4640 return 1;
4641 else
4642 return 0;
4643
4644 if (GET_CODE (rs1) == SUBREG)
4645 rs1 = SUBREG_REG (rs1);
4646 if (!REG_P (rs1))
4647 return 0;
4648
4649 if (rs2)
4650 {
4651 if (GET_CODE (rs2) == SUBREG)
4652 rs2 = SUBREG_REG (rs2);
4653 if (!REG_P (rs2))
4654 return 0;
4655 }
4656
4657 if (strict)
4658 {
4659 if (!REGNO_OK_FOR_BASE_P (REGNO (rs1))
4660 || (rs2 && !REGNO_OK_FOR_BASE_P (REGNO (rs2))))
4661 return 0;
4662 }
4663 else
4664 {
4665 if ((! SPARC_INT_REG_P (REGNO (rs1))
4666 && REGNO (rs1) != FRAME_POINTER_REGNUM
4667 && REGNO (rs1) < FIRST_PSEUDO_REGISTER)
4668 || (rs2
4669 && (! SPARC_INT_REG_P (REGNO (rs2))
4670 && REGNO (rs2) != FRAME_POINTER_REGNUM
4671 && REGNO (rs2) < FIRST_PSEUDO_REGISTER)))
4672 return 0;
4673 }
4674 return 1;
4675 }
4676
4677 /* Return the SYMBOL_REF for the tls_get_addr function. */
4678
4679 static GTY(()) rtx sparc_tls_symbol = NULL_RTX;
4680
4681 static rtx
4682 sparc_tls_get_addr (void)
4683 {
4684 if (!sparc_tls_symbol)
4685 sparc_tls_symbol = gen_rtx_SYMBOL_REF (Pmode, "__tls_get_addr");
4686
4687 return sparc_tls_symbol;
4688 }
4689
4690 /* Return the Global Offset Table to be used in TLS mode. */
4691
4692 static rtx
4693 sparc_tls_got (void)
4694 {
4695 /* In PIC mode, this is just the PIC offset table. */
4696 if (flag_pic)
4697 {
4698 crtl->uses_pic_offset_table = 1;
4699 return pic_offset_table_rtx;
4700 }
4701
4702 /* In non-PIC mode, Sun as (unlike GNU as) emits PC-relative relocations for
4703 the GOT symbol with the 32-bit ABI, so we reload the GOT register. */
4704 if (TARGET_SUN_TLS && TARGET_ARCH32)
4705 {
4706 load_got_register ();
4707 return got_register_rtx;
4708 }
4709
4710 /* In all other cases, we load a new pseudo with the GOT symbol. */
4711 return copy_to_reg (sparc_got ());
4712 }
4713
4714 /* Return true if X contains a thread-local symbol. */
4715
4716 static bool
4717 sparc_tls_referenced_p (rtx x)
4718 {
4719 if (!TARGET_HAVE_TLS)
4720 return false;
4721
4722 if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS)
4723 x = XEXP (XEXP (x, 0), 0);
4724
4725 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x))
4726 return true;
4727
4728 /* That's all we handle in sparc_legitimize_tls_address for now. */
4729 return false;
4730 }
4731
4732 /* ADDR contains a thread-local SYMBOL_REF. Generate code to compute
4733 this (thread-local) address. */
4734
4735 static rtx
4736 sparc_legitimize_tls_address (rtx addr)
4737 {
4738 rtx temp1, temp2, temp3, ret, o0, got;
4739 rtx_insn *insn;
4740
4741 gcc_assert (can_create_pseudo_p ());
4742
4743 if (GET_CODE (addr) == SYMBOL_REF)
4744 /* Although the various sethi/or sequences generate SImode values, many of
4745 them can be transformed by the linker when relaxing and, if relaxing to
4746 local-exec, will become a sethi/xor pair, which is signed and therefore
4747 a full DImode value in 64-bit mode. Thus we must use Pmode, lest these
4748 values be spilled onto the stack in 64-bit mode. */
4749 switch (SYMBOL_REF_TLS_MODEL (addr))
4750 {
4751 case TLS_MODEL_GLOBAL_DYNAMIC:
4752 start_sequence ();
4753 temp1 = gen_reg_rtx (Pmode);
4754 temp2 = gen_reg_rtx (Pmode);
4755 ret = gen_reg_rtx (Pmode);
4756 o0 = gen_rtx_REG (Pmode, 8);
4757 got = sparc_tls_got ();
4758 emit_insn (gen_tgd_hi22 (Pmode, temp1, addr));
4759 emit_insn (gen_tgd_lo10 (Pmode, temp2, temp1, addr));
4760 emit_insn (gen_tgd_add (Pmode, o0, got, temp2, addr));
4761 insn = emit_call_insn (gen_tgd_call (Pmode, o0, sparc_tls_get_addr (),
4762 addr, const1_rtx));
4763 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), o0);
4764 RTL_CONST_CALL_P (insn) = 1;
4765 insn = get_insns ();
4766 end_sequence ();
4767 emit_libcall_block (insn, ret, o0, addr);
4768 break;
4769
4770 case TLS_MODEL_LOCAL_DYNAMIC:
4771 start_sequence ();
4772 temp1 = gen_reg_rtx (Pmode);
4773 temp2 = gen_reg_rtx (Pmode);
4774 temp3 = gen_reg_rtx (Pmode);
4775 ret = gen_reg_rtx (Pmode);
4776 o0 = gen_rtx_REG (Pmode, 8);
4777 got = sparc_tls_got ();
4778 emit_insn (gen_tldm_hi22 (Pmode, temp1));
4779 emit_insn (gen_tldm_lo10 (Pmode, temp2, temp1));
4780 emit_insn (gen_tldm_add (Pmode, o0, got, temp2));
4781 insn = emit_call_insn (gen_tldm_call (Pmode, o0, sparc_tls_get_addr (),
4782 const1_rtx));
4783 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), o0);
4784 RTL_CONST_CALL_P (insn) = 1;
4785 insn = get_insns ();
4786 end_sequence ();
4787 /* Attach a unique REG_EQUAL, to allow the RTL optimizers to
4788 share the LD_BASE result with other LD model accesses. */
4789 emit_libcall_block (insn, temp3, o0,
4790 gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
4791 UNSPEC_TLSLD_BASE));
4792 temp1 = gen_reg_rtx (Pmode);
4793 temp2 = gen_reg_rtx (Pmode);
4794 emit_insn (gen_tldo_hix22 (Pmode, temp1, addr));
4795 emit_insn (gen_tldo_lox10 (Pmode, temp2, temp1, addr));
4796 emit_insn (gen_tldo_add (Pmode, ret, temp3, temp2, addr));
4797 break;
4798
4799 case TLS_MODEL_INITIAL_EXEC:
4800 temp1 = gen_reg_rtx (Pmode);
4801 temp2 = gen_reg_rtx (Pmode);
4802 temp3 = gen_reg_rtx (Pmode);
4803 got = sparc_tls_got ();
4804 emit_insn (gen_tie_hi22 (Pmode, temp1, addr));
4805 emit_insn (gen_tie_lo10 (Pmode, temp2, temp1, addr));
4806 if (TARGET_ARCH32)
4807 emit_insn (gen_tie_ld32 (temp3, got, temp2, addr));
4808 else
4809 emit_insn (gen_tie_ld64 (temp3, got, temp2, addr));
4810 if (TARGET_SUN_TLS)
4811 {
4812 ret = gen_reg_rtx (Pmode);
4813 emit_insn (gen_tie_add (Pmode, ret, gen_rtx_REG (Pmode, 7),
4814 temp3, addr));
4815 }
4816 else
4817 ret = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, 7), temp3);
4818 break;
4819
4820 case TLS_MODEL_LOCAL_EXEC:
4821 temp1 = gen_reg_rtx (Pmode);
4822 temp2 = gen_reg_rtx (Pmode);
4823 emit_insn (gen_tle_hix22 (Pmode, temp1, addr));
4824 emit_insn (gen_tle_lox10 (Pmode, temp2, temp1, addr));
4825 ret = gen_rtx_PLUS (Pmode, gen_rtx_REG (Pmode, 7), temp2);
4826 break;
4827
4828 default:
4829 gcc_unreachable ();
4830 }
4831
4832 else if (GET_CODE (addr) == CONST)
4833 {
4834 rtx base, offset;
4835
4836 gcc_assert (GET_CODE (XEXP (addr, 0)) == PLUS);
4837
4838 base = sparc_legitimize_tls_address (XEXP (XEXP (addr, 0), 0));
4839 offset = XEXP (XEXP (addr, 0), 1);
4840
4841 base = force_operand (base, NULL_RTX);
4842 if (!(GET_CODE (offset) == CONST_INT && SMALL_INT (offset)))
4843 offset = force_reg (Pmode, offset);
4844 ret = gen_rtx_PLUS (Pmode, base, offset);
4845 }
4846
4847 else
4848 gcc_unreachable (); /* for now ... */
4849
4850 return ret;
4851 }
4852
4853 /* Legitimize PIC addresses. If the address is already position-independent,
4854 we return ORIG. Newly generated position-independent addresses go into a
4855 reg. This is REG if nonzero, otherwise we allocate register(s) as
4856 necessary. */
4857
4858 static rtx
4859 sparc_legitimize_pic_address (rtx orig, rtx reg)
4860 {
4861 if (GET_CODE (orig) == SYMBOL_REF
4862 /* See the comment in sparc_expand_move. */
4863 || (GET_CODE (orig) == LABEL_REF && !can_use_mov_pic_label_ref (orig)))
4864 {
4865 bool gotdata_op = false;
4866 rtx pic_ref, address;
4867 rtx_insn *insn;
4868
4869 if (!reg)
4870 {
4871 gcc_assert (can_create_pseudo_p ());
4872 reg = gen_reg_rtx (Pmode);
4873 }
4874
4875 if (flag_pic == 2)
4876 {
4877 /* If not during reload, allocate another temp reg here for loading
4878 in the address, so that these instructions can be optimized
4879 properly. */
4880 rtx temp_reg = can_create_pseudo_p () ? gen_reg_rtx (Pmode) : reg;
4881
4882 /* Must put the SYMBOL_REF inside an UNSPEC here so that cse
4883 won't get confused into thinking that these two instructions
4884 are loading in the true address of the symbol. If in the
4885 future a PIC rtx exists, that should be used instead. */
4886 if (TARGET_ARCH64)
4887 {
4888 emit_insn (gen_movdi_high_pic (temp_reg, orig));
4889 emit_insn (gen_movdi_lo_sum_pic (temp_reg, temp_reg, orig));
4890 }
4891 else
4892 {
4893 emit_insn (gen_movsi_high_pic (temp_reg, orig));
4894 emit_insn (gen_movsi_lo_sum_pic (temp_reg, temp_reg, orig));
4895 }
4896
4897 address = temp_reg;
4898 gotdata_op = true;
4899 }
4900 else
4901 address = orig;
4902
4903 crtl->uses_pic_offset_table = 1;
4904 if (gotdata_op)
4905 {
4906 if (TARGET_ARCH64)
4907 insn = emit_insn (gen_movdi_pic_gotdata_op (reg,
4908 pic_offset_table_rtx,
4909 address, orig));
4910 else
4911 insn = emit_insn (gen_movsi_pic_gotdata_op (reg,
4912 pic_offset_table_rtx,
4913 address, orig));
4914 }
4915 else
4916 {
4917 pic_ref
4918 = gen_const_mem (Pmode,
4919 gen_rtx_PLUS (Pmode,
4920 pic_offset_table_rtx, address));
4921 insn = emit_move_insn (reg, pic_ref);
4922 }
4923
4924 /* Put a REG_EQUAL note on this insn, so that it can be optimized
4925 by loop. */
4926 set_unique_reg_note (insn, REG_EQUAL, orig);
4927 return reg;
4928 }
4929 else if (GET_CODE (orig) == CONST)
4930 {
4931 rtx base, offset;
4932
4933 if (GET_CODE (XEXP (orig, 0)) == PLUS
4934 && sparc_pic_register_p (XEXP (XEXP (orig, 0), 0)))
4935 return orig;
4936
4937 if (!reg)
4938 {
4939 gcc_assert (can_create_pseudo_p ());
4940 reg = gen_reg_rtx (Pmode);
4941 }
4942
4943 gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
4944 base = sparc_legitimize_pic_address (XEXP (XEXP (orig, 0), 0), reg);
4945 offset = sparc_legitimize_pic_address (XEXP (XEXP (orig, 0), 1),
4946 base == reg ? NULL_RTX : reg);
4947
4948 if (GET_CODE (offset) == CONST_INT)
4949 {
4950 if (SMALL_INT (offset))
4951 return plus_constant (Pmode, base, INTVAL (offset));
4952 else if (can_create_pseudo_p ())
4953 offset = force_reg (Pmode, offset);
4954 else
4955 /* If we reach here, then something is seriously wrong. */
4956 gcc_unreachable ();
4957 }
4958 return gen_rtx_PLUS (Pmode, base, offset);
4959 }
4960 else if (GET_CODE (orig) == LABEL_REF)
4961 /* ??? We ought to be checking that the register is live instead, in case
4962 it is eliminated. */
4963 crtl->uses_pic_offset_table = 1;
4964
4965 return orig;
4966 }
4967
4968 /* Try machine-dependent ways of modifying an illegitimate address X
4969 to be legitimate. If we find one, return the new, valid address.
4970
4971 OLDX is the address as it was before break_out_memory_refs was called.
4972 In some cases it is useful to look at this to decide what needs to be done.
4973
4974 MODE is the mode of the operand pointed to by X.
4975
4976 On SPARC, change REG+N into REG+REG, and REG+(X*Y) into REG+REG. */
4977
4978 static rtx
4979 sparc_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
4980 machine_mode mode)
4981 {
4982 rtx orig_x = x;
4983
4984 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == MULT)
4985 x = gen_rtx_PLUS (Pmode, XEXP (x, 1),
4986 force_operand (XEXP (x, 0), NULL_RTX));
4987 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == MULT)
4988 x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
4989 force_operand (XEXP (x, 1), NULL_RTX));
4990 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS)
4991 x = gen_rtx_PLUS (Pmode, force_operand (XEXP (x, 0), NULL_RTX),
4992 XEXP (x, 1));
4993 if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == PLUS)
4994 x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
4995 force_operand (XEXP (x, 1), NULL_RTX));
4996
4997 if (x != orig_x && sparc_legitimate_address_p (mode, x, FALSE))
4998 return x;
4999
5000 if (sparc_tls_referenced_p (x))
5001 x = sparc_legitimize_tls_address (x);
5002 else if (flag_pic)
5003 x = sparc_legitimize_pic_address (x, NULL_RTX);
5004 else if (GET_CODE (x) == PLUS && CONSTANT_ADDRESS_P (XEXP (x, 1)))
5005 x = gen_rtx_PLUS (Pmode, XEXP (x, 0),
5006 copy_to_mode_reg (Pmode, XEXP (x, 1)));
5007 else if (GET_CODE (x) == PLUS && CONSTANT_ADDRESS_P (XEXP (x, 0)))
5008 x = gen_rtx_PLUS (Pmode, XEXP (x, 1),
5009 copy_to_mode_reg (Pmode, XEXP (x, 0)));
5010 else if (GET_CODE (x) == SYMBOL_REF
5011 || GET_CODE (x) == CONST
5012 || GET_CODE (x) == LABEL_REF)
5013 x = copy_to_suggested_reg (x, NULL_RTX, Pmode);
5014
5015 return x;
5016 }
5017
5018 /* Delegitimize an address that was legitimized by the above function. */
5019
5020 static rtx
5021 sparc_delegitimize_address (rtx x)
5022 {
5023 x = delegitimize_mem_from_attrs (x);
5024
5025 if (GET_CODE (x) == LO_SUM)
5026 x = XEXP (x, 1);
5027
5028 if (GET_CODE (x) == UNSPEC)
5029 switch (XINT (x, 1))
5030 {
5031 case UNSPEC_MOVE_PIC:
5032 case UNSPEC_TLSLE:
5033 x = XVECEXP (x, 0, 0);
5034 gcc_assert (GET_CODE (x) == SYMBOL_REF);
5035 break;
5036 case UNSPEC_MOVE_GOTDATA:
5037 x = XVECEXP (x, 0, 2);
5038 gcc_assert (GET_CODE (x) == SYMBOL_REF);
5039 break;
5040 default:
5041 break;
5042 }
5043
5044 /* This is generated by mov{si,di}_pic_label_ref in PIC mode. */
5045 if (GET_CODE (x) == MINUS
5046 && (XEXP (x, 0) == got_register_rtx
5047 || sparc_pic_register_p (XEXP (x, 0))))
5048 {
5049 rtx y = XEXP (x, 1);
5050
5051 if (GET_CODE (y) == LO_SUM)
5052 y = XEXP (y, 1);
5053
5054 if (GET_CODE (y) == UNSPEC && XINT (y, 1) == UNSPEC_MOVE_PIC_LABEL)
5055 {
5056 x = XVECEXP (y, 0, 0);
5057 gcc_assert (GET_CODE (x) == LABEL_REF
5058 || (GET_CODE (x) == CONST
5059 && GET_CODE (XEXP (x, 0)) == PLUS
5060 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
5061 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT));
5062 }
5063 }
5064
5065 return x;
5066 }
5067
5068 /* SPARC implementation of LEGITIMIZE_RELOAD_ADDRESS. Returns a value to
5069 replace the input X, or the original X if no replacement is called for.
5070 The output parameter *WIN is 1 if the calling macro should goto WIN,
5071 0 if it should not.
5072
5073 For SPARC, we wish to handle addresses by splitting them into
5074 HIGH+LO_SUM pairs, retaining the LO_SUM in the memory reference.
5075 This cuts the number of extra insns by one.
5076
5077 Do nothing when generating PIC code and the address is a symbolic
5078 operand or requires a scratch register. */
5079
5080 rtx
5081 sparc_legitimize_reload_address (rtx x, machine_mode mode,
5082 int opnum, int type,
5083 int ind_levels ATTRIBUTE_UNUSED, int *win)
5084 {
5085 /* Decompose SImode constants into HIGH+LO_SUM. */
5086 if (CONSTANT_P (x)
5087 && (mode != TFmode || TARGET_ARCH64)
5088 && GET_MODE (x) == SImode
5089 && GET_CODE (x) != LO_SUM
5090 && GET_CODE (x) != HIGH
5091 && sparc_code_model <= CM_MEDLOW
5092 && !(flag_pic
5093 && (symbolic_operand (x, Pmode) || pic_address_needs_scratch (x))))
5094 {
5095 x = gen_rtx_LO_SUM (GET_MODE (x), gen_rtx_HIGH (GET_MODE (x), x), x);
5096 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
5097 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
5098 opnum, (enum reload_type)type);
5099 *win = 1;
5100 return x;
5101 }
5102
5103 /* We have to recognize what we have already generated above. */
5104 if (GET_CODE (x) == LO_SUM && GET_CODE (XEXP (x, 0)) == HIGH)
5105 {
5106 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
5107 BASE_REG_CLASS, GET_MODE (x), VOIDmode, 0, 0,
5108 opnum, (enum reload_type)type);
5109 *win = 1;
5110 return x;
5111 }
5112
5113 *win = 0;
5114 return x;
5115 }
5116
5117 /* Return true if ADDR (a legitimate address expression)
5118 has an effect that depends on the machine mode it is used for.
5119
5120 In PIC mode,
5121
5122 (mem:HI [%l7+a])
5123
5124 is not equivalent to
5125
5126 (mem:QI [%l7+a]) (mem:QI [%l7+a+1])
5127
5128 because [%l7+a+1] is interpreted as the address of (a+1). */
5129
5130
5131 static bool
5132 sparc_mode_dependent_address_p (const_rtx addr,
5133 addr_space_t as ATTRIBUTE_UNUSED)
5134 {
5135 if (GET_CODE (addr) == PLUS
5136 && sparc_pic_register_p (XEXP (addr, 0))
5137 && symbolic_operand (XEXP (addr, 1), VOIDmode))
5138 return true;
5139
5140 return false;
5141 }
5142
5143 /* Emit a call instruction with the pattern given by PAT. ADDR is the
5144 address of the call target. */
5145
5146 void
5147 sparc_emit_call_insn (rtx pat, rtx addr)
5148 {
5149 rtx_insn *insn;
5150
5151 insn = emit_call_insn (pat);
5152
5153 /* The PIC register is live on entry to VxWorks PIC PLT entries. */
5154 if (TARGET_VXWORKS_RTP
5155 && flag_pic
5156 && GET_CODE (addr) == SYMBOL_REF
5157 && (SYMBOL_REF_DECL (addr)
5158 ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
5159 : !SYMBOL_REF_LOCAL_P (addr)))
5160 {
5161 use_reg (&CALL_INSN_FUNCTION_USAGE (insn), pic_offset_table_rtx);
5162 crtl->uses_pic_offset_table = 1;
5163 }
5164 }
5165
5166 /* Return 1 if RTX is a MEM which is known to be aligned to at
5167 least a DESIRED byte boundary. */
5168
5169 int
5170 mem_min_alignment (rtx mem, int desired)
5171 {
5172 rtx addr, base, offset;
5173
5174 /* If it's not a MEM we can't accept it. */
5175 if (GET_CODE (mem) != MEM)
5176 return 0;
5177
5178 /* Obviously... */
5179 if (!TARGET_UNALIGNED_DOUBLES
5180 && MEM_ALIGN (mem) / BITS_PER_UNIT >= (unsigned)desired)
5181 return 1;
5182
5183 /* ??? The rest of the function predates MEM_ALIGN so
5184 there is probably a bit of redundancy. */
5185 addr = XEXP (mem, 0);
5186 base = offset = NULL_RTX;
5187 if (GET_CODE (addr) == PLUS)
5188 {
5189 if (GET_CODE (XEXP (addr, 0)) == REG)
5190 {
5191 base = XEXP (addr, 0);
5192
5193 /* What we are saying here is that if the base
5194 REG is aligned properly, the compiler will make
5195 sure any REG based index upon it will be so
5196 as well. */
5197 if (GET_CODE (XEXP (addr, 1)) == CONST_INT)
5198 offset = XEXP (addr, 1);
5199 else
5200 offset = const0_rtx;
5201 }
5202 }
5203 else if (GET_CODE (addr) == REG)
5204 {
5205 base = addr;
5206 offset = const0_rtx;
5207 }
5208
5209 if (base != NULL_RTX)
5210 {
5211 int regno = REGNO (base);
5212
5213 if (regno != HARD_FRAME_POINTER_REGNUM && regno != STACK_POINTER_REGNUM)
5214 {
5215 /* Check if the compiler has recorded some information
5216 about the alignment of the base REG. If reload has
5217 completed, we already matched with proper alignments.
5218 If not running global_alloc, reload might give us
5219 unaligned pointer to local stack though. */
5220 if (((cfun != 0
5221 && REGNO_POINTER_ALIGN (regno) >= desired * BITS_PER_UNIT)
5222 || (optimize && reload_completed))
5223 && (INTVAL (offset) & (desired - 1)) == 0)
5224 return 1;
5225 }
5226 else
5227 {
5228 if (((INTVAL (offset) - SPARC_STACK_BIAS) & (desired - 1)) == 0)
5229 return 1;
5230 }
5231 }
5232 else if (! TARGET_UNALIGNED_DOUBLES
5233 || CONSTANT_P (addr)
5234 || GET_CODE (addr) == LO_SUM)
5235 {
5236 /* Anything else we know is properly aligned unless TARGET_UNALIGNED_DOUBLES
5237 is true, in which case we can only assume that an access is aligned if
5238 it is to a constant address, or the address involves a LO_SUM. */
5239 return 1;
5240 }
5241
5242 /* An obviously unaligned address. */
5243 return 0;
5244 }
5245
5246
5247 /* Vectors to keep interesting information about registers where it can easily
5248 be got. We used to use the actual mode value as the bit number, but there
5249 are more than 32 modes now. Instead we use two tables: one indexed by
5250 hard register number, and one indexed by mode. */
5251
5252 /* The purpose of sparc_mode_class is to shrink the range of modes so that
5253 they all fit (as bit numbers) in a 32-bit word (again). Each real mode is
5254 mapped into one sparc_mode_class mode. */
5255
5256 enum sparc_mode_class {
5257 H_MODE, S_MODE, D_MODE, T_MODE, O_MODE,
5258 SF_MODE, DF_MODE, TF_MODE, OF_MODE,
5259 CC_MODE, CCFP_MODE
5260 };
5261
5262 /* Modes for single-word and smaller quantities. */
5263 #define S_MODES \
5264 ((1 << (int) H_MODE) | (1 << (int) S_MODE) | (1 << (int) SF_MODE))
5265
5266 /* Modes for double-word and smaller quantities. */
5267 #define D_MODES (S_MODES | (1 << (int) D_MODE) | (1 << (int) DF_MODE))
5268
5269 /* Modes for quad-word and smaller quantities. */
5270 #define T_MODES (D_MODES | (1 << (int) T_MODE) | (1 << (int) TF_MODE))
5271
5272 /* Modes for 8-word and smaller quantities. */
5273 #define O_MODES (T_MODES | (1 << (int) O_MODE) | (1 << (int) OF_MODE))
5274
5275 /* Modes for single-float quantities. */
5276 #define SF_MODES ((1 << (int) S_MODE) | (1 << (int) SF_MODE))
5277
5278 /* Modes for double-float and smaller quantities. */
5279 #define DF_MODES (SF_MODES | (1 << (int) D_MODE) | (1 << (int) DF_MODE))
5280
5281 /* Modes for quad-float and smaller quantities. */
5282 #define TF_MODES (DF_MODES | (1 << (int) TF_MODE))
5283
5284 /* Modes for quad-float pairs and smaller quantities. */
5285 #define OF_MODES (TF_MODES | (1 << (int) OF_MODE))
5286
5287 /* Modes for double-float only quantities. */
5288 #define DF_MODES_NO_S ((1 << (int) D_MODE) | (1 << (int) DF_MODE))
5289
5290 /* Modes for quad-float and double-float only quantities. */
5291 #define TF_MODES_NO_S (DF_MODES_NO_S | (1 << (int) TF_MODE))
5292
5293 /* Modes for quad-float pairs and double-float only quantities. */
5294 #define OF_MODES_NO_S (TF_MODES_NO_S | (1 << (int) OF_MODE))
5295
5296 /* Modes for condition codes. */
5297 #define CC_MODES (1 << (int) CC_MODE)
5298 #define CCFP_MODES (1 << (int) CCFP_MODE)
5299
5300 /* Value is 1 if register/mode pair is acceptable on sparc.
5301
5302 The funny mixture of D and T modes is because integer operations
5303 do not specially operate on tetra quantities, so non-quad-aligned
5304 registers can hold quadword quantities (except %o4 and %i4 because
5305 they cross fixed registers).
5306
5307 ??? Note that, despite the settings, non-double-aligned parameter
5308 registers can hold double-word quantities in 32-bit mode. */
5309
5310 /* This points to either the 32-bit or the 64-bit version. */
5311 static const int *hard_regno_mode_classes;
5312
5313 static const int hard_32bit_mode_classes[] = {
5314 S_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
5315 T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
5316 T_MODES, S_MODES, T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES,
5317 T_MODES, S_MODES, T_MODES, S_MODES, D_MODES, S_MODES, D_MODES, S_MODES,
5318
5319 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
5320 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
5321 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
5322 OF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
5323
5324 /* FP regs f32 to f63. Only the even numbered registers actually exist,
5325 and none can hold SFmode/SImode values. */
5326 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
5327 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
5328 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
5329 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, TF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
5330
5331 /* %fcc[0123] */
5332 CCFP_MODES, CCFP_MODES, CCFP_MODES, CCFP_MODES,
5333
5334 /* %icc, %sfp, %gsr */
5335 CC_MODES, 0, D_MODES
5336 };
5337
5338 static const int hard_64bit_mode_classes[] = {
5339 D_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
5340 O_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
5341 T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
5342 O_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES, T_MODES, D_MODES,
5343
5344 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
5345 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
5346 OF_MODES, SF_MODES, DF_MODES, SF_MODES, OF_MODES, SF_MODES, DF_MODES, SF_MODES,
5347 OF_MODES, SF_MODES, DF_MODES, SF_MODES, TF_MODES, SF_MODES, DF_MODES, SF_MODES,
5348
5349 /* FP regs f32 to f63. Only the even numbered registers actually exist,
5350 and none can hold SFmode/SImode values. */
5351 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
5352 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
5353 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, OF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
5354 OF_MODES_NO_S, 0, DF_MODES_NO_S, 0, TF_MODES_NO_S, 0, DF_MODES_NO_S, 0,
5355
5356 /* %fcc[0123] */
5357 CCFP_MODES, CCFP_MODES, CCFP_MODES, CCFP_MODES,
5358
5359 /* %icc, %sfp, %gsr */
5360 CC_MODES, 0, D_MODES
5361 };
5362
5363 static int sparc_mode_class [NUM_MACHINE_MODES];
5364
5365 enum reg_class sparc_regno_reg_class[FIRST_PSEUDO_REGISTER];
5366
5367 static void
5368 sparc_init_modes (void)
5369 {
5370 int i;
5371
5372 for (i = 0; i < NUM_MACHINE_MODES; i++)
5373 {
5374 machine_mode m = (machine_mode) i;
5375 unsigned int size = GET_MODE_SIZE (m);
5376
5377 switch (GET_MODE_CLASS (m))
5378 {
5379 case MODE_INT:
5380 case MODE_PARTIAL_INT:
5381 case MODE_COMPLEX_INT:
5382 if (size < 4)
5383 sparc_mode_class[i] = 1 << (int) H_MODE;
5384 else if (size == 4)
5385 sparc_mode_class[i] = 1 << (int) S_MODE;
5386 else if (size == 8)
5387 sparc_mode_class[i] = 1 << (int) D_MODE;
5388 else if (size == 16)
5389 sparc_mode_class[i] = 1 << (int) T_MODE;
5390 else if (size == 32)
5391 sparc_mode_class[i] = 1 << (int) O_MODE;
5392 else
5393 sparc_mode_class[i] = 0;
5394 break;
5395 case MODE_VECTOR_INT:
5396 if (size == 4)
5397 sparc_mode_class[i] = 1 << (int) SF_MODE;
5398 else if (size == 8)
5399 sparc_mode_class[i] = 1 << (int) DF_MODE;
5400 else
5401 sparc_mode_class[i] = 0;
5402 break;
5403 case MODE_FLOAT:
5404 case MODE_COMPLEX_FLOAT:
5405 if (size == 4)
5406 sparc_mode_class[i] = 1 << (int) SF_MODE;
5407 else if (size == 8)
5408 sparc_mode_class[i] = 1 << (int) DF_MODE;
5409 else if (size == 16)
5410 sparc_mode_class[i] = 1 << (int) TF_MODE;
5411 else if (size == 32)
5412 sparc_mode_class[i] = 1 << (int) OF_MODE;
5413 else
5414 sparc_mode_class[i] = 0;
5415 break;
5416 case MODE_CC:
5417 if (m == CCFPmode || m == CCFPEmode)
5418 sparc_mode_class[i] = 1 << (int) CCFP_MODE;
5419 else
5420 sparc_mode_class[i] = 1 << (int) CC_MODE;
5421 break;
5422 default:
5423 sparc_mode_class[i] = 0;
5424 break;
5425 }
5426 }
5427
5428 if (TARGET_ARCH64)
5429 hard_regno_mode_classes = hard_64bit_mode_classes;
5430 else
5431 hard_regno_mode_classes = hard_32bit_mode_classes;
5432
5433 /* Initialize the array used by REGNO_REG_CLASS. */
5434 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5435 {
5436 if (i < 16 && TARGET_V8PLUS)
5437 sparc_regno_reg_class[i] = I64_REGS;
5438 else if (i < 32 || i == FRAME_POINTER_REGNUM)
5439 sparc_regno_reg_class[i] = GENERAL_REGS;
5440 else if (i < 64)
5441 sparc_regno_reg_class[i] = FP_REGS;
5442 else if (i < 96)
5443 sparc_regno_reg_class[i] = EXTRA_FP_REGS;
5444 else if (i < 100)
5445 sparc_regno_reg_class[i] = FPCC_REGS;
5446 else
5447 sparc_regno_reg_class[i] = NO_REGS;
5448 }
5449 }
5450
5451 /* Return whether REGNO, a global or FP register, must be saved/restored. */
5452
5453 static inline bool
5454 save_global_or_fp_reg_p (unsigned int regno,
5455 int leaf_function ATTRIBUTE_UNUSED)
5456 {
5457 return !call_used_or_fixed_reg_p (regno) && df_regs_ever_live_p (regno);
5458 }
5459
5460 /* Return whether the return address register (%i7) is needed. */
5461
5462 static inline bool
5463 return_addr_reg_needed_p (int leaf_function)
5464 {
5465 /* If it is live, for example because of __builtin_return_address (0). */
5466 if (df_regs_ever_live_p (RETURN_ADDR_REGNUM))
5467 return true;
5468
5469 /* Otherwise, it is needed as save register if %o7 is clobbered. */
5470 if (!leaf_function
5471 /* Loading the GOT register clobbers %o7. */
5472 || crtl->uses_pic_offset_table
5473 || df_regs_ever_live_p (INCOMING_RETURN_ADDR_REGNUM))
5474 return true;
5475
5476 return false;
5477 }
5478
5479 /* Return whether REGNO, a local or in register, must be saved/restored. */
5480
5481 static bool
5482 save_local_or_in_reg_p (unsigned int regno, int leaf_function)
5483 {
5484 /* General case: call-saved registers live at some point. */
5485 if (!call_used_or_fixed_reg_p (regno) && df_regs_ever_live_p (regno))
5486 return true;
5487
5488 /* Frame pointer register (%fp) if needed. */
5489 if (regno == HARD_FRAME_POINTER_REGNUM && frame_pointer_needed)
5490 return true;
5491
5492 /* Return address register (%i7) if needed. */
5493 if (regno == RETURN_ADDR_REGNUM && return_addr_reg_needed_p (leaf_function))
5494 return true;
5495
5496 /* GOT register (%l7) if needed. */
5497 if (got_register_rtx && regno == REGNO (got_register_rtx))
5498 return true;
5499
5500 /* If the function accesses prior frames, the frame pointer and the return
5501 address of the previous frame must be saved on the stack. */
5502 if (crtl->accesses_prior_frames
5503 && (regno == HARD_FRAME_POINTER_REGNUM || regno == RETURN_ADDR_REGNUM))
5504 return true;
5505
5506 return false;
5507 }
5508
5509 /* Compute the frame size required by the function. This function is called
5510 during the reload pass and also by sparc_expand_prologue. */
5511
5512 static HOST_WIDE_INT
5513 sparc_compute_frame_size (HOST_WIDE_INT size, int leaf_function)
5514 {
5515 HOST_WIDE_INT frame_size, apparent_frame_size;
5516 int args_size, n_global_fp_regs = 0;
5517 bool save_local_in_regs_p = false;
5518 unsigned int i;
5519
5520 /* If the function allocates dynamic stack space, the dynamic offset is
5521 computed early and contains REG_PARM_STACK_SPACE, so we need to cope. */
5522 if (leaf_function && !cfun->calls_alloca)
5523 args_size = 0;
5524 else
5525 args_size = crtl->outgoing_args_size + REG_PARM_STACK_SPACE (cfun->decl);
5526
5527 /* Calculate space needed for global registers. */
5528 if (TARGET_ARCH64)
5529 {
5530 for (i = 0; i < 8; i++)
5531 if (save_global_or_fp_reg_p (i, 0))
5532 n_global_fp_regs += 2;
5533 }
5534 else
5535 {
5536 for (i = 0; i < 8; i += 2)
5537 if (save_global_or_fp_reg_p (i, 0)
5538 || save_global_or_fp_reg_p (i + 1, 0))
5539 n_global_fp_regs += 2;
5540 }
5541
5542 /* In the flat window model, find out which local and in registers need to
5543 be saved. We don't reserve space in the current frame for them as they
5544 will be spilled into the register window save area of the caller's frame.
5545 However, as soon as we use this register window save area, we must create
5546 that of the current frame to make it the live one. */
5547 if (TARGET_FLAT)
5548 for (i = 16; i < 32; i++)
5549 if (save_local_or_in_reg_p (i, leaf_function))
5550 {
5551 save_local_in_regs_p = true;
5552 break;
5553 }
5554
5555 /* Calculate space needed for FP registers. */
5556 for (i = 32; i < (TARGET_V9 ? 96 : 64); i += 2)
5557 if (save_global_or_fp_reg_p (i, 0) || save_global_or_fp_reg_p (i + 1, 0))
5558 n_global_fp_regs += 2;
5559
5560 if (size == 0
5561 && n_global_fp_regs == 0
5562 && args_size == 0
5563 && !save_local_in_regs_p)
5564 frame_size = apparent_frame_size = 0;
5565 else
5566 {
5567 /* Start from the apparent frame size. */
5568 apparent_frame_size = ROUND_UP (size, 8) + n_global_fp_regs * 4;
5569
5570 /* We need to add the size of the outgoing argument area. */
5571 frame_size = apparent_frame_size + ROUND_UP (args_size, 8);
5572
5573 /* And that of the register window save area. */
5574 frame_size += FIRST_PARM_OFFSET (cfun->decl);
5575
5576 /* Finally, bump to the appropriate alignment. */
5577 frame_size = SPARC_STACK_ALIGN (frame_size);
5578 }
5579
5580 /* Set up values for use in prologue and epilogue. */
5581 sparc_frame_size = frame_size;
5582 sparc_apparent_frame_size = apparent_frame_size;
5583 sparc_n_global_fp_regs = n_global_fp_regs;
5584 sparc_save_local_in_regs_p = save_local_in_regs_p;
5585
5586 return frame_size;
5587 }
5588
5589 /* Implement the macro INITIAL_ELIMINATION_OFFSET, return the OFFSET. */
5590
5591 int
5592 sparc_initial_elimination_offset (int to)
5593 {
5594 int offset;
5595
5596 if (to == STACK_POINTER_REGNUM)
5597 offset = sparc_compute_frame_size (get_frame_size (), crtl->is_leaf);
5598 else
5599 offset = 0;
5600
5601 offset += SPARC_STACK_BIAS;
5602 return offset;
5603 }
5604
5605 /* Output any necessary .register pseudo-ops. */
5606
5607 void
5608 sparc_output_scratch_registers (FILE *file ATTRIBUTE_UNUSED)
5609 {
5610 int i;
5611
5612 if (TARGET_ARCH32)
5613 return;
5614
5615 /* Check if %g[2367] were used without
5616 .register being printed for them already. */
5617 for (i = 2; i < 8; i++)
5618 {
5619 if (df_regs_ever_live_p (i)
5620 && ! sparc_hard_reg_printed [i])
5621 {
5622 sparc_hard_reg_printed [i] = 1;
5623 /* %g7 is used as TLS base register, use #ignore
5624 for it instead of #scratch. */
5625 fprintf (file, "\t.register\t%%g%d, #%s\n", i,
5626 i == 7 ? "ignore" : "scratch");
5627 }
5628 if (i == 3) i = 5;
5629 }
5630 }
5631
5632 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
5633
5634 #if PROBE_INTERVAL > 4096
5635 #error Cannot use indexed addressing mode for stack probing
5636 #endif
5637
5638 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
5639 inclusive. These are offsets from the current stack pointer.
5640
5641 Note that we don't use the REG+REG addressing mode for the probes because
5642 of the stack bias in 64-bit mode. And it doesn't really buy us anything
5643 so the advantages of having a single code win here. */
5644
5645 static void
5646 sparc_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
5647 {
5648 rtx g1 = gen_rtx_REG (Pmode, 1);
5649
5650 /* See if we have a constant small number of probes to generate. If so,
5651 that's the easy case. */
5652 if (size <= PROBE_INTERVAL)
5653 {
5654 emit_move_insn (g1, GEN_INT (first));
5655 emit_insn (gen_rtx_SET (g1,
5656 gen_rtx_MINUS (Pmode, stack_pointer_rtx, g1)));
5657 emit_stack_probe (plus_constant (Pmode, g1, -size));
5658 }
5659
5660 /* The run-time loop is made up of 9 insns in the generic case while the
5661 compile-time loop is made up of 4+2*(n-2) insns for n # of intervals. */
5662 else if (size <= 4 * PROBE_INTERVAL)
5663 {
5664 HOST_WIDE_INT i;
5665
5666 emit_move_insn (g1, GEN_INT (first + PROBE_INTERVAL));
5667 emit_insn (gen_rtx_SET (g1,
5668 gen_rtx_MINUS (Pmode, stack_pointer_rtx, g1)));
5669 emit_stack_probe (g1);
5670
5671 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 2 until
5672 it exceeds SIZE. If only two probes are needed, this will not
5673 generate any code. Then probe at FIRST + SIZE. */
5674 for (i = 2 * PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
5675 {
5676 emit_insn (gen_rtx_SET (g1,
5677 plus_constant (Pmode, g1, -PROBE_INTERVAL)));
5678 emit_stack_probe (g1);
5679 }
5680
5681 emit_stack_probe (plus_constant (Pmode, g1,
5682 (i - PROBE_INTERVAL) - size));
5683 }
5684
5685 /* Otherwise, do the same as above, but in a loop. Note that we must be
5686 extra careful with variables wrapping around because we might be at
5687 the very top (or the very bottom) of the address space and we have
5688 to be able to handle this case properly; in particular, we use an
5689 equality test for the loop condition. */
5690 else
5691 {
5692 HOST_WIDE_INT rounded_size;
5693 rtx g4 = gen_rtx_REG (Pmode, 4);
5694
5695 emit_move_insn (g1, GEN_INT (first));
5696
5697
5698 /* Step 1: round SIZE to the previous multiple of the interval. */
5699
5700 rounded_size = ROUND_DOWN (size, PROBE_INTERVAL);
5701 emit_move_insn (g4, GEN_INT (rounded_size));
5702
5703
5704 /* Step 2: compute initial and final value of the loop counter. */
5705
5706 /* TEST_ADDR = SP + FIRST. */
5707 emit_insn (gen_rtx_SET (g1,
5708 gen_rtx_MINUS (Pmode, stack_pointer_rtx, g1)));
5709
5710 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
5711 emit_insn (gen_rtx_SET (g4, gen_rtx_MINUS (Pmode, g1, g4)));
5712
5713
5714 /* Step 3: the loop
5715
5716 while (TEST_ADDR != LAST_ADDR)
5717 {
5718 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
5719 probe at TEST_ADDR
5720 }
5721
5722 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
5723 until it is equal to ROUNDED_SIZE. */
5724
5725 emit_insn (gen_probe_stack_range (Pmode, g1, g1, g4));
5726
5727
5728 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
5729 that SIZE is equal to ROUNDED_SIZE. */
5730
5731 if (size != rounded_size)
5732 emit_stack_probe (plus_constant (Pmode, g4, rounded_size - size));
5733 }
5734
5735 /* Make sure nothing is scheduled before we are done. */
5736 emit_insn (gen_blockage ());
5737 }
5738
5739 /* Probe a range of stack addresses from REG1 to REG2 inclusive. These are
5740 absolute addresses. */
5741
5742 const char *
5743 output_probe_stack_range (rtx reg1, rtx reg2)
5744 {
5745 static int labelno = 0;
5746 char loop_lab[32];
5747 rtx xops[2];
5748
5749 ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
5750
5751 /* Loop. */
5752 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
5753
5754 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
5755 xops[0] = reg1;
5756 xops[1] = GEN_INT (-PROBE_INTERVAL);
5757 output_asm_insn ("add\t%0, %1, %0", xops);
5758
5759 /* Test if TEST_ADDR == LAST_ADDR. */
5760 xops[1] = reg2;
5761 output_asm_insn ("cmp\t%0, %1", xops);
5762
5763 /* Probe at TEST_ADDR and branch. */
5764 if (TARGET_ARCH64)
5765 fputs ("\tbne,pt\t%xcc,", asm_out_file);
5766 else
5767 fputs ("\tbne\t", asm_out_file);
5768 assemble_name_raw (asm_out_file, loop_lab);
5769 fputc ('\n', asm_out_file);
5770 xops[1] = GEN_INT (SPARC_STACK_BIAS);
5771 output_asm_insn (" st\t%%g0, [%0+%1]", xops);
5772
5773 return "";
5774 }
5775
5776 /* Emit code to save/restore registers from LOW to HIGH at BASE+OFFSET as
5777 needed. LOW is supposed to be double-word aligned for 32-bit registers.
5778 SAVE_P decides whether a register must be saved/restored. ACTION_TRUE
5779 is the action to be performed if SAVE_P returns true and ACTION_FALSE
5780 the action to be performed if it returns false. Return the new offset. */
5781
5782 typedef bool (*sorr_pred_t) (unsigned int, int);
5783 typedef enum { SORR_NONE, SORR_ADVANCE, SORR_SAVE, SORR_RESTORE } sorr_act_t;
5784
5785 static int
5786 emit_save_or_restore_regs (unsigned int low, unsigned int high, rtx base,
5787 int offset, int leaf_function, sorr_pred_t save_p,
5788 sorr_act_t action_true, sorr_act_t action_false)
5789 {
5790 unsigned int i;
5791 rtx mem;
5792 rtx_insn *insn;
5793
5794 if (TARGET_ARCH64 && high <= 32)
5795 {
5796 int fp_offset = -1;
5797
5798 for (i = low; i < high; i++)
5799 {
5800 if (save_p (i, leaf_function))
5801 {
5802 mem = gen_frame_mem (DImode, plus_constant (Pmode,
5803 base, offset));
5804 if (action_true == SORR_SAVE)
5805 {
5806 insn = emit_move_insn (mem, gen_rtx_REG (DImode, i));
5807 RTX_FRAME_RELATED_P (insn) = 1;
5808 }
5809 else /* action_true == SORR_RESTORE */
5810 {
5811 /* The frame pointer must be restored last since its old
5812 value may be used as base address for the frame. This
5813 is problematic in 64-bit mode only because of the lack
5814 of double-word load instruction. */
5815 if (i == HARD_FRAME_POINTER_REGNUM)
5816 fp_offset = offset;
5817 else
5818 emit_move_insn (gen_rtx_REG (DImode, i), mem);
5819 }
5820 offset += 8;
5821 }
5822 else if (action_false == SORR_ADVANCE)
5823 offset += 8;
5824 }
5825
5826 if (fp_offset >= 0)
5827 {
5828 mem = gen_frame_mem (DImode, plus_constant (Pmode, base, fp_offset));
5829 emit_move_insn (hard_frame_pointer_rtx, mem);
5830 }
5831 }
5832 else
5833 {
5834 for (i = low; i < high; i += 2)
5835 {
5836 bool reg0 = save_p (i, leaf_function);
5837 bool reg1 = save_p (i + 1, leaf_function);
5838 machine_mode mode;
5839 int regno;
5840
5841 if (reg0 && reg1)
5842 {
5843 mode = SPARC_INT_REG_P (i) ? E_DImode : E_DFmode;
5844 regno = i;
5845 }
5846 else if (reg0)
5847 {
5848 mode = SPARC_INT_REG_P (i) ? E_SImode : E_SFmode;
5849 regno = i;
5850 }
5851 else if (reg1)
5852 {
5853 mode = SPARC_INT_REG_P (i) ? E_SImode : E_SFmode;
5854 regno = i + 1;
5855 offset += 4;
5856 }
5857 else
5858 {
5859 if (action_false == SORR_ADVANCE)
5860 offset += 8;
5861 continue;
5862 }
5863
5864 mem = gen_frame_mem (mode, plus_constant (Pmode, base, offset));
5865 if (action_true == SORR_SAVE)
5866 {
5867 insn = emit_move_insn (mem, gen_rtx_REG (mode, regno));
5868 RTX_FRAME_RELATED_P (insn) = 1;
5869 if (mode == DImode)
5870 {
5871 rtx set1, set2;
5872 mem = gen_frame_mem (SImode, plus_constant (Pmode, base,
5873 offset));
5874 set1 = gen_rtx_SET (mem, gen_rtx_REG (SImode, regno));
5875 RTX_FRAME_RELATED_P (set1) = 1;
5876 mem
5877 = gen_frame_mem (SImode, plus_constant (Pmode, base,
5878 offset + 4));
5879 set2 = gen_rtx_SET (mem, gen_rtx_REG (SImode, regno + 1));
5880 RTX_FRAME_RELATED_P (set2) = 1;
5881 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
5882 gen_rtx_PARALLEL (VOIDmode,
5883 gen_rtvec (2, set1, set2)));
5884 }
5885 }
5886 else /* action_true == SORR_RESTORE */
5887 emit_move_insn (gen_rtx_REG (mode, regno), mem);
5888
5889 /* Bump and round down to double word
5890 in case we already bumped by 4. */
5891 offset = ROUND_DOWN (offset + 8, 8);
5892 }
5893 }
5894
5895 return offset;
5896 }
5897
5898 /* Emit code to adjust BASE to OFFSET. Return the new base. */
5899
5900 static rtx
5901 emit_adjust_base_to_offset (rtx base, int offset)
5902 {
5903 /* ??? This might be optimized a little as %g1 might already have a
5904 value close enough that a single add insn will do. */
5905 /* ??? Although, all of this is probably only a temporary fix because
5906 if %g1 can hold a function result, then sparc_expand_epilogue will
5907 lose (the result will be clobbered). */
5908 rtx new_base = gen_rtx_REG (Pmode, 1);
5909 emit_move_insn (new_base, GEN_INT (offset));
5910 emit_insn (gen_rtx_SET (new_base, gen_rtx_PLUS (Pmode, base, new_base)));
5911 return new_base;
5912 }
5913
5914 /* Emit code to save/restore call-saved global and FP registers. */
5915
5916 static void
5917 emit_save_or_restore_global_fp_regs (rtx base, int offset, sorr_act_t action)
5918 {
5919 if (offset < -4096 || offset + sparc_n_global_fp_regs * 4 > 4095)
5920 {
5921 base = emit_adjust_base_to_offset (base, offset);
5922 offset = 0;
5923 }
5924
5925 offset
5926 = emit_save_or_restore_regs (0, 8, base, offset, 0,
5927 save_global_or_fp_reg_p, action, SORR_NONE);
5928 emit_save_or_restore_regs (32, TARGET_V9 ? 96 : 64, base, offset, 0,
5929 save_global_or_fp_reg_p, action, SORR_NONE);
5930 }
5931
5932 /* Emit code to save/restore call-saved local and in registers. */
5933
5934 static void
5935 emit_save_or_restore_local_in_regs (rtx base, int offset, sorr_act_t action)
5936 {
5937 if (offset < -4096 || offset + 16 * UNITS_PER_WORD > 4095)
5938 {
5939 base = emit_adjust_base_to_offset (base, offset);
5940 offset = 0;
5941 }
5942
5943 emit_save_or_restore_regs (16, 32, base, offset, sparc_leaf_function_p,
5944 save_local_or_in_reg_p, action, SORR_ADVANCE);
5945 }
5946
5947 /* Emit a window_save insn. */
5948
5949 static rtx_insn *
5950 emit_window_save (rtx increment)
5951 {
5952 rtx_insn *insn = emit_insn (gen_window_save (increment));
5953 RTX_FRAME_RELATED_P (insn) = 1;
5954
5955 /* The incoming return address (%o7) is saved in %i7. */
5956 add_reg_note (insn, REG_CFA_REGISTER,
5957 gen_rtx_SET (gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM),
5958 gen_rtx_REG (Pmode,
5959 INCOMING_RETURN_ADDR_REGNUM)));
5960
5961 /* The window save event. */
5962 add_reg_note (insn, REG_CFA_WINDOW_SAVE, const0_rtx);
5963
5964 /* The CFA is %fp, the hard frame pointer. */
5965 add_reg_note (insn, REG_CFA_DEF_CFA,
5966 plus_constant (Pmode, hard_frame_pointer_rtx,
5967 INCOMING_FRAME_SP_OFFSET));
5968
5969 return insn;
5970 }
5971
5972 /* Generate an increment for the stack pointer. */
5973
5974 static rtx
5975 gen_stack_pointer_inc (rtx increment)
5976 {
5977 return gen_rtx_SET (stack_pointer_rtx,
5978 gen_rtx_PLUS (Pmode,
5979 stack_pointer_rtx,
5980 increment));
5981 }
5982
5983 /* Expand the function prologue. The prologue is responsible for reserving
5984 storage for the frame, saving the call-saved registers and loading the
5985 GOT register if needed. */
5986
5987 void
5988 sparc_expand_prologue (void)
5989 {
5990 HOST_WIDE_INT size;
5991 rtx_insn *insn;
5992
5993 /* Compute a snapshot of crtl->uses_only_leaf_regs. Relying
5994 on the final value of the flag means deferring the prologue/epilogue
5995 expansion until just before the second scheduling pass, which is too
5996 late to emit multiple epilogues or return insns.
5997
5998 Of course we are making the assumption that the value of the flag
5999 will not change between now and its final value. Of the three parts
6000 of the formula, only the last one can reasonably vary. Let's take a
6001 closer look, after assuming that the first two ones are set to true
6002 (otherwise the last value is effectively silenced).
6003
6004 If only_leaf_regs_used returns false, the global predicate will also
6005 be false so the actual frame size calculated below will be positive.
6006 As a consequence, the save_register_window insn will be emitted in
6007 the instruction stream; now this insn explicitly references %fp
6008 which is not a leaf register so only_leaf_regs_used will always
6009 return false subsequently.
6010
6011 If only_leaf_regs_used returns true, we hope that the subsequent
6012 optimization passes won't cause non-leaf registers to pop up. For
6013 example, the regrename pass has special provisions to not rename to
6014 non-leaf registers in a leaf function. */
6015 sparc_leaf_function_p
6016 = optimize > 0 && crtl->is_leaf && only_leaf_regs_used ();
6017
6018 size = sparc_compute_frame_size (get_frame_size(), sparc_leaf_function_p);
6019
6020 if (flag_stack_usage_info)
6021 current_function_static_stack_size = size;
6022
6023 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK
6024 || flag_stack_clash_protection)
6025 {
6026 if (crtl->is_leaf && !cfun->calls_alloca)
6027 {
6028 if (size > PROBE_INTERVAL && size > get_stack_check_protect ())
6029 sparc_emit_probe_stack_range (get_stack_check_protect (),
6030 size - get_stack_check_protect ());
6031 }
6032 else if (size > 0)
6033 sparc_emit_probe_stack_range (get_stack_check_protect (), size);
6034 }
6035
6036 if (size == 0)
6037 ; /* do nothing. */
6038 else if (sparc_leaf_function_p)
6039 {
6040 rtx size_int_rtx = GEN_INT (-size);
6041
6042 if (size <= 4096)
6043 insn = emit_insn (gen_stack_pointer_inc (size_int_rtx));
6044 else if (size <= 8192)
6045 {
6046 insn = emit_insn (gen_stack_pointer_inc (GEN_INT (-4096)));
6047 RTX_FRAME_RELATED_P (insn) = 1;
6048
6049 /* %sp is still the CFA register. */
6050 insn = emit_insn (gen_stack_pointer_inc (GEN_INT (4096 - size)));
6051 }
6052 else
6053 {
6054 rtx size_rtx = gen_rtx_REG (Pmode, 1);
6055 emit_move_insn (size_rtx, size_int_rtx);
6056 insn = emit_insn (gen_stack_pointer_inc (size_rtx));
6057 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
6058 gen_stack_pointer_inc (size_int_rtx));
6059 }
6060
6061 RTX_FRAME_RELATED_P (insn) = 1;
6062
6063 /* Ensure no memory access is done before the frame is established. */
6064 emit_insn (gen_frame_blockage ());
6065 }
6066 else
6067 {
6068 rtx size_int_rtx = GEN_INT (-size);
6069
6070 if (size <= 4096)
6071 emit_window_save (size_int_rtx);
6072 else if (size <= 8192)
6073 {
6074 emit_window_save (GEN_INT (-4096));
6075
6076 /* %sp is not the CFA register anymore. */
6077 emit_insn (gen_stack_pointer_inc (GEN_INT (4096 - size)));
6078
6079 /* Likewise. */
6080 emit_insn (gen_frame_blockage ());
6081 }
6082 else
6083 {
6084 rtx size_rtx = gen_rtx_REG (Pmode, 1);
6085 emit_move_insn (size_rtx, size_int_rtx);
6086 emit_window_save (size_rtx);
6087 }
6088 }
6089
6090 if (sparc_leaf_function_p)
6091 {
6092 sparc_frame_base_reg = stack_pointer_rtx;
6093 sparc_frame_base_offset = size + SPARC_STACK_BIAS;
6094 }
6095 else
6096 {
6097 sparc_frame_base_reg = hard_frame_pointer_rtx;
6098 sparc_frame_base_offset = SPARC_STACK_BIAS;
6099 }
6100
6101 if (sparc_n_global_fp_regs > 0)
6102 emit_save_or_restore_global_fp_regs (sparc_frame_base_reg,
6103 sparc_frame_base_offset
6104 - sparc_apparent_frame_size,
6105 SORR_SAVE);
6106
6107 /* Advertise that the data calculated just above are now valid. */
6108 sparc_prologue_data_valid_p = true;
6109 }
6110
6111 /* Expand the function prologue. The prologue is responsible for reserving
6112 storage for the frame, saving the call-saved registers and loading the
6113 GOT register if needed. */
6114
6115 void
6116 sparc_flat_expand_prologue (void)
6117 {
6118 HOST_WIDE_INT size;
6119 rtx_insn *insn;
6120
6121 sparc_leaf_function_p = optimize > 0 && crtl->is_leaf;
6122
6123 size = sparc_compute_frame_size (get_frame_size(), sparc_leaf_function_p);
6124
6125 if (flag_stack_usage_info)
6126 current_function_static_stack_size = size;
6127
6128 if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK
6129 || flag_stack_clash_protection)
6130 {
6131 if (crtl->is_leaf && !cfun->calls_alloca)
6132 {
6133 if (size > PROBE_INTERVAL && size > get_stack_check_protect ())
6134 sparc_emit_probe_stack_range (get_stack_check_protect (),
6135 size - get_stack_check_protect ());
6136 }
6137 else if (size > 0)
6138 sparc_emit_probe_stack_range (get_stack_check_protect (), size);
6139 }
6140
6141 if (sparc_save_local_in_regs_p)
6142 emit_save_or_restore_local_in_regs (stack_pointer_rtx, SPARC_STACK_BIAS,
6143 SORR_SAVE);
6144
6145 if (size == 0)
6146 ; /* do nothing. */
6147 else
6148 {
6149 rtx size_int_rtx, size_rtx;
6150
6151 size_rtx = size_int_rtx = GEN_INT (-size);
6152
6153 /* We establish the frame (i.e. decrement the stack pointer) first, even
6154 if we use a frame pointer, because we cannot clobber any call-saved
6155 registers, including the frame pointer, if we haven't created a new
6156 register save area, for the sake of compatibility with the ABI. */
6157 if (size <= 4096)
6158 insn = emit_insn (gen_stack_pointer_inc (size_int_rtx));
6159 else if (size <= 8192 && !frame_pointer_needed)
6160 {
6161 insn = emit_insn (gen_stack_pointer_inc (GEN_INT (-4096)));
6162 RTX_FRAME_RELATED_P (insn) = 1;
6163 insn = emit_insn (gen_stack_pointer_inc (GEN_INT (4096 - size)));
6164 }
6165 else
6166 {
6167 size_rtx = gen_rtx_REG (Pmode, 1);
6168 emit_move_insn (size_rtx, size_int_rtx);
6169 insn = emit_insn (gen_stack_pointer_inc (size_rtx));
6170 add_reg_note (insn, REG_CFA_ADJUST_CFA,
6171 gen_stack_pointer_inc (size_int_rtx));
6172 }
6173 RTX_FRAME_RELATED_P (insn) = 1;
6174
6175 /* Ensure no memory access is done before the frame is established. */
6176 emit_insn (gen_frame_blockage ());
6177
6178 if (frame_pointer_needed)
6179 {
6180 insn = emit_insn (gen_rtx_SET (hard_frame_pointer_rtx,
6181 gen_rtx_MINUS (Pmode,
6182 stack_pointer_rtx,
6183 size_rtx)));
6184 RTX_FRAME_RELATED_P (insn) = 1;
6185
6186 add_reg_note (insn, REG_CFA_ADJUST_CFA,
6187 gen_rtx_SET (hard_frame_pointer_rtx,
6188 plus_constant (Pmode, stack_pointer_rtx,
6189 size)));
6190 }
6191
6192 if (return_addr_reg_needed_p (sparc_leaf_function_p))
6193 {
6194 rtx o7 = gen_rtx_REG (Pmode, INCOMING_RETURN_ADDR_REGNUM);
6195 rtx i7 = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
6196
6197 insn = emit_move_insn (i7, o7);
6198 RTX_FRAME_RELATED_P (insn) = 1;
6199
6200 add_reg_note (insn, REG_CFA_REGISTER, gen_rtx_SET (i7, o7));
6201
6202 /* Prevent this instruction from ever being considered dead,
6203 even if this function has no epilogue. */
6204 emit_use (i7);
6205 }
6206 }
6207
6208 if (frame_pointer_needed)
6209 {
6210 sparc_frame_base_reg = hard_frame_pointer_rtx;
6211 sparc_frame_base_offset = SPARC_STACK_BIAS;
6212 }
6213 else
6214 {
6215 sparc_frame_base_reg = stack_pointer_rtx;
6216 sparc_frame_base_offset = size + SPARC_STACK_BIAS;
6217 }
6218
6219 if (sparc_n_global_fp_regs > 0)
6220 emit_save_or_restore_global_fp_regs (sparc_frame_base_reg,
6221 sparc_frame_base_offset
6222 - sparc_apparent_frame_size,
6223 SORR_SAVE);
6224
6225 /* Advertise that the data calculated just above are now valid. */
6226 sparc_prologue_data_valid_p = true;
6227 }
6228
6229 /* This function generates the assembly code for function entry, which boils
6230 down to emitting the necessary .register directives. */
6231
6232 static void
6233 sparc_asm_function_prologue (FILE *file)
6234 {
6235 /* Check that the assumption we made in sparc_expand_prologue is valid. */
6236 if (!TARGET_FLAT)
6237 gcc_assert (sparc_leaf_function_p == crtl->uses_only_leaf_regs);
6238
6239 sparc_output_scratch_registers (file);
6240 }
6241
6242 /* Expand the function epilogue, either normal or part of a sibcall.
6243 We emit all the instructions except the return or the call. */
6244
6245 void
6246 sparc_expand_epilogue (bool for_eh)
6247 {
6248 HOST_WIDE_INT size = sparc_frame_size;
6249
6250 if (cfun->calls_alloca)
6251 emit_insn (gen_frame_blockage ());
6252
6253 if (sparc_n_global_fp_regs > 0)
6254 emit_save_or_restore_global_fp_regs (sparc_frame_base_reg,
6255 sparc_frame_base_offset
6256 - sparc_apparent_frame_size,
6257 SORR_RESTORE);
6258
6259 if (size == 0 || for_eh)
6260 ; /* do nothing. */
6261 else if (sparc_leaf_function_p)
6262 {
6263 /* Ensure no memory access is done after the frame is destroyed. */
6264 emit_insn (gen_frame_blockage ());
6265
6266 if (size <= 4096)
6267 emit_insn (gen_stack_pointer_inc (GEN_INT (size)));
6268 else if (size <= 8192)
6269 {
6270 emit_insn (gen_stack_pointer_inc (GEN_INT (4096)));
6271 emit_insn (gen_stack_pointer_inc (GEN_INT (size - 4096)));
6272 }
6273 else
6274 {
6275 rtx reg = gen_rtx_REG (Pmode, 1);
6276 emit_move_insn (reg, GEN_INT (size));
6277 emit_insn (gen_stack_pointer_inc (reg));
6278 }
6279 }
6280 }
6281
6282 /* Expand the function epilogue, either normal or part of a sibcall.
6283 We emit all the instructions except the return or the call. */
6284
6285 void
6286 sparc_flat_expand_epilogue (bool for_eh)
6287 {
6288 HOST_WIDE_INT size = sparc_frame_size;
6289
6290 if (sparc_n_global_fp_regs > 0)
6291 emit_save_or_restore_global_fp_regs (sparc_frame_base_reg,
6292 sparc_frame_base_offset
6293 - sparc_apparent_frame_size,
6294 SORR_RESTORE);
6295
6296 /* If we have a frame pointer, we'll need both to restore it before the
6297 frame is destroyed and use its current value in destroying the frame.
6298 Since we don't have an atomic way to do that in the flat window model,
6299 we save the current value into a temporary register (%g1). */
6300 if (frame_pointer_needed && !for_eh)
6301 emit_move_insn (gen_rtx_REG (Pmode, 1), hard_frame_pointer_rtx);
6302
6303 if (return_addr_reg_needed_p (sparc_leaf_function_p))
6304 emit_move_insn (gen_rtx_REG (Pmode, INCOMING_RETURN_ADDR_REGNUM),
6305 gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM));
6306
6307 if (sparc_save_local_in_regs_p)
6308 emit_save_or_restore_local_in_regs (sparc_frame_base_reg,
6309 sparc_frame_base_offset,
6310 SORR_RESTORE);
6311
6312 if (size == 0 || for_eh)
6313 ; /* do nothing. */
6314 else if (frame_pointer_needed)
6315 {
6316 /* Ensure no memory access is done after the frame is destroyed. */
6317 emit_insn (gen_frame_blockage ());
6318
6319 emit_move_insn (stack_pointer_rtx, gen_rtx_REG (Pmode, 1));
6320 }
6321 else
6322 {
6323 /* Likewise. */
6324 emit_insn (gen_frame_blockage ());
6325
6326 if (size <= 4096)
6327 emit_insn (gen_stack_pointer_inc (GEN_INT (size)));
6328 else if (size <= 8192)
6329 {
6330 emit_insn (gen_stack_pointer_inc (GEN_INT (4096)));
6331 emit_insn (gen_stack_pointer_inc (GEN_INT (size - 4096)));
6332 }
6333 else
6334 {
6335 rtx reg = gen_rtx_REG (Pmode, 1);
6336 emit_move_insn (reg, GEN_INT (size));
6337 emit_insn (gen_stack_pointer_inc (reg));
6338 }
6339 }
6340 }
6341
6342 /* Return true if it is appropriate to emit `return' instructions in the
6343 body of a function. */
6344
6345 bool
6346 sparc_can_use_return_insn_p (void)
6347 {
6348 return sparc_prologue_data_valid_p
6349 && sparc_n_global_fp_regs == 0
6350 && TARGET_FLAT
6351 ? (sparc_frame_size == 0 && !sparc_save_local_in_regs_p)
6352 : (sparc_frame_size == 0 || !sparc_leaf_function_p);
6353 }
6354
6355 /* This function generates the assembly code for function exit. */
6356
6357 static void
6358 sparc_asm_function_epilogue (FILE *file)
6359 {
6360 /* If the last two instructions of a function are "call foo; dslot;"
6361 the return address might point to the first instruction in the next
6362 function and we have to output a dummy nop for the sake of sane
6363 backtraces in such cases. This is pointless for sibling calls since
6364 the return address is explicitly adjusted. */
6365
6366 rtx_insn *insn = get_last_insn ();
6367
6368 rtx last_real_insn = prev_real_insn (insn);
6369 if (last_real_insn
6370 && NONJUMP_INSN_P (last_real_insn)
6371 && GET_CODE (PATTERN (last_real_insn)) == SEQUENCE)
6372 last_real_insn = XVECEXP (PATTERN (last_real_insn), 0, 0);
6373
6374 if (last_real_insn
6375 && CALL_P (last_real_insn)
6376 && !SIBLING_CALL_P (last_real_insn))
6377 fputs("\tnop\n", file);
6378
6379 sparc_output_deferred_case_vectors ();
6380 }
6381
6382 /* Output a 'restore' instruction. */
6383
6384 static void
6385 output_restore (rtx pat)
6386 {
6387 rtx operands[3];
6388
6389 if (! pat)
6390 {
6391 fputs ("\t restore\n", asm_out_file);
6392 return;
6393 }
6394
6395 gcc_assert (GET_CODE (pat) == SET);
6396
6397 operands[0] = SET_DEST (pat);
6398 pat = SET_SRC (pat);
6399
6400 switch (GET_CODE (pat))
6401 {
6402 case PLUS:
6403 operands[1] = XEXP (pat, 0);
6404 operands[2] = XEXP (pat, 1);
6405 output_asm_insn (" restore %r1, %2, %Y0", operands);
6406 break;
6407 case LO_SUM:
6408 operands[1] = XEXP (pat, 0);
6409 operands[2] = XEXP (pat, 1);
6410 output_asm_insn (" restore %r1, %%lo(%a2), %Y0", operands);
6411 break;
6412 case ASHIFT:
6413 operands[1] = XEXP (pat, 0);
6414 gcc_assert (XEXP (pat, 1) == const1_rtx);
6415 output_asm_insn (" restore %r1, %r1, %Y0", operands);
6416 break;
6417 default:
6418 operands[1] = pat;
6419 output_asm_insn (" restore %%g0, %1, %Y0", operands);
6420 break;
6421 }
6422 }
6423
6424 /* Output a return. */
6425
6426 const char *
6427 output_return (rtx_insn *insn)
6428 {
6429 if (crtl->calls_eh_return)
6430 {
6431 /* If the function uses __builtin_eh_return, the eh_return
6432 machinery occupies the delay slot. */
6433 gcc_assert (!final_sequence);
6434
6435 if (flag_delayed_branch)
6436 {
6437 if (!TARGET_FLAT && TARGET_V9)
6438 fputs ("\treturn\t%i7+8\n", asm_out_file);
6439 else
6440 {
6441 if (!TARGET_FLAT)
6442 fputs ("\trestore\n", asm_out_file);
6443
6444 fputs ("\tjmp\t%o7+8\n", asm_out_file);
6445 }
6446
6447 fputs ("\t add\t%sp, %g1, %sp\n", asm_out_file);
6448 }
6449 else
6450 {
6451 if (!TARGET_FLAT)
6452 fputs ("\trestore\n", asm_out_file);
6453
6454 fputs ("\tadd\t%sp, %g1, %sp\n", asm_out_file);
6455 fputs ("\tjmp\t%o7+8\n\t nop\n", asm_out_file);
6456 }
6457 }
6458 else if (sparc_leaf_function_p || TARGET_FLAT)
6459 {
6460 /* This is a leaf or flat function so we don't have to bother restoring
6461 the register window, which frees us from dealing with the convoluted
6462 semantics of restore/return. We simply output the jump to the
6463 return address and the insn in the delay slot (if any). */
6464
6465 return "jmp\t%%o7+%)%#";
6466 }
6467 else
6468 {
6469 /* This is a regular function so we have to restore the register window.
6470 We may have a pending insn for the delay slot, which will be either
6471 combined with the 'restore' instruction or put in the delay slot of
6472 the 'return' instruction. */
6473
6474 if (final_sequence)
6475 {
6476 rtx_insn *delay;
6477 rtx pat;
6478
6479 delay = NEXT_INSN (insn);
6480 gcc_assert (delay);
6481
6482 pat = PATTERN (delay);
6483
6484 if (TARGET_V9 && ! epilogue_renumber (&pat, 1))
6485 {
6486 epilogue_renumber (&pat, 0);
6487 return "return\t%%i7+%)%#";
6488 }
6489 else
6490 {
6491 output_asm_insn ("jmp\t%%i7+%)", NULL);
6492
6493 /* We're going to output the insn in the delay slot manually.
6494 Make sure to output its source location first. */
6495 PATTERN (delay) = gen_blockage ();
6496 INSN_CODE (delay) = -1;
6497 final_scan_insn (delay, asm_out_file, optimize, 0, NULL);
6498 INSN_LOCATION (delay) = UNKNOWN_LOCATION;
6499
6500 output_restore (pat);
6501 }
6502 }
6503 else
6504 {
6505 /* The delay slot is empty. */
6506 if (TARGET_V9)
6507 return "return\t%%i7+%)\n\t nop";
6508 else if (flag_delayed_branch)
6509 return "jmp\t%%i7+%)\n\t restore";
6510 else
6511 return "restore\n\tjmp\t%%o7+%)\n\t nop";
6512 }
6513 }
6514
6515 return "";
6516 }
6517
6518 /* Output a sibling call. */
6519
6520 const char *
6521 output_sibcall (rtx_insn *insn, rtx call_operand)
6522 {
6523 rtx operands[1];
6524
6525 gcc_assert (flag_delayed_branch);
6526
6527 operands[0] = call_operand;
6528
6529 if (sparc_leaf_function_p || TARGET_FLAT)
6530 {
6531 /* This is a leaf or flat function so we don't have to bother restoring
6532 the register window. We simply output the jump to the function and
6533 the insn in the delay slot (if any). */
6534
6535 gcc_assert (!(LEAF_SIBCALL_SLOT_RESERVED_P && final_sequence));
6536
6537 if (final_sequence)
6538 output_asm_insn ("sethi\t%%hi(%a0), %%g1\n\tjmp\t%%g1 + %%lo(%a0)%#",
6539 operands);
6540 else
6541 /* Use or with rs2 %%g0 instead of mov, so that as/ld can optimize
6542 it into branch if possible. */
6543 output_asm_insn ("or\t%%o7, %%g0, %%g1\n\tcall\t%a0, 0\n\t or\t%%g1, %%g0, %%o7",
6544 operands);
6545 }
6546 else
6547 {
6548 /* This is a regular function so we have to restore the register window.
6549 We may have a pending insn for the delay slot, which will be combined
6550 with the 'restore' instruction. */
6551
6552 output_asm_insn ("call\t%a0, 0", operands);
6553
6554 if (final_sequence)
6555 {
6556 rtx_insn *delay;
6557 rtx pat;
6558
6559 delay = NEXT_INSN (insn);
6560 gcc_assert (delay);
6561
6562 pat = PATTERN (delay);
6563
6564 /* We're going to output the insn in the delay slot manually.
6565 Make sure to output its source location first. */
6566 PATTERN (delay) = gen_blockage ();
6567 INSN_CODE (delay) = -1;
6568 final_scan_insn (delay, asm_out_file, optimize, 0, NULL);
6569 INSN_LOCATION (delay) = UNKNOWN_LOCATION;
6570
6571 output_restore (pat);
6572 }
6573 else
6574 output_restore (NULL_RTX);
6575 }
6576
6577 return "";
6578 }
6579
6580 /* Functions for handling argument passing.
6581
6582 For 32-bit, the first 6 args are normally in registers and the rest are
6583 pushed. Any arg that starts within the first 6 words is at least
6584 partially passed in a register unless its data type forbids.
6585
6586 For 64-bit, the argument registers are laid out as an array of 16 elements
6587 and arguments are added sequentially. The first 6 int args and up to the
6588 first 16 fp args (depending on size) are passed in regs.
6589
6590 Slot Stack Integral Float Float in structure Double Long Double
6591 ---- ----- -------- ----- ------------------ ------ -----------
6592 15 [SP+248] %f31 %f30,%f31 %d30
6593 14 [SP+240] %f29 %f28,%f29 %d28 %q28
6594 13 [SP+232] %f27 %f26,%f27 %d26
6595 12 [SP+224] %f25 %f24,%f25 %d24 %q24
6596 11 [SP+216] %f23 %f22,%f23 %d22
6597 10 [SP+208] %f21 %f20,%f21 %d20 %q20
6598 9 [SP+200] %f19 %f18,%f19 %d18
6599 8 [SP+192] %f17 %f16,%f17 %d16 %q16
6600 7 [SP+184] %f15 %f14,%f15 %d14
6601 6 [SP+176] %f13 %f12,%f13 %d12 %q12
6602 5 [SP+168] %o5 %f11 %f10,%f11 %d10
6603 4 [SP+160] %o4 %f9 %f8,%f9 %d8 %q8
6604 3 [SP+152] %o3 %f7 %f6,%f7 %d6
6605 2 [SP+144] %o2 %f5 %f4,%f5 %d4 %q4
6606 1 [SP+136] %o1 %f3 %f2,%f3 %d2
6607 0 [SP+128] %o0 %f1 %f0,%f1 %d0 %q0
6608
6609 Here SP = %sp if -mno-stack-bias or %sp+stack_bias otherwise.
6610
6611 Integral arguments are always passed as 64-bit quantities appropriately
6612 extended.
6613
6614 Passing of floating point values is handled as follows.
6615 If a prototype is in scope:
6616 If the value is in a named argument (i.e. not a stdarg function or a
6617 value not part of the `...') then the value is passed in the appropriate
6618 fp reg.
6619 If the value is part of the `...' and is passed in one of the first 6
6620 slots then the value is passed in the appropriate int reg.
6621 If the value is part of the `...' and is not passed in one of the first 6
6622 slots then the value is passed in memory.
6623 If a prototype is not in scope:
6624 If the value is one of the first 6 arguments the value is passed in the
6625 appropriate integer reg and the appropriate fp reg.
6626 If the value is not one of the first 6 arguments the value is passed in
6627 the appropriate fp reg and in memory.
6628
6629
6630 Summary of the calling conventions implemented by GCC on the SPARC:
6631
6632 32-bit ABI:
6633 size argument return value
6634
6635 small integer <4 int. reg. int. reg.
6636 word 4 int. reg. int. reg.
6637 double word 8 int. reg. int. reg.
6638
6639 _Complex small integer <8 int. reg. int. reg.
6640 _Complex word 8 int. reg. int. reg.
6641 _Complex double word 16 memory int. reg.
6642
6643 vector integer <=8 int. reg. FP reg.
6644 vector integer >8 memory memory
6645
6646 float 4 int. reg. FP reg.
6647 double 8 int. reg. FP reg.
6648 long double 16 memory memory
6649
6650 _Complex float 8 memory FP reg.
6651 _Complex double 16 memory FP reg.
6652 _Complex long double 32 memory FP reg.
6653
6654 vector float any memory memory
6655
6656 aggregate any memory memory
6657
6658
6659
6660 64-bit ABI:
6661 size argument return value
6662
6663 small integer <8 int. reg. int. reg.
6664 word 8 int. reg. int. reg.
6665 double word 16 int. reg. int. reg.
6666
6667 _Complex small integer <16 int. reg. int. reg.
6668 _Complex word 16 int. reg. int. reg.
6669 _Complex double word 32 memory int. reg.
6670
6671 vector integer <=16 FP reg. FP reg.
6672 vector integer 16<s<=32 memory FP reg.
6673 vector integer >32 memory memory
6674
6675 float 4 FP reg. FP reg.
6676 double 8 FP reg. FP reg.
6677 long double 16 FP reg. FP reg.
6678
6679 _Complex float 8 FP reg. FP reg.
6680 _Complex double 16 FP reg. FP reg.
6681 _Complex long double 32 memory FP reg.
6682
6683 vector float <=16 FP reg. FP reg.
6684 vector float 16<s<=32 memory FP reg.
6685 vector float >32 memory memory
6686
6687 aggregate <=16 reg. reg.
6688 aggregate 16<s<=32 memory reg.
6689 aggregate >32 memory memory
6690
6691
6692
6693 Note #1: complex floating-point types follow the extended SPARC ABIs as
6694 implemented by the Sun compiler.
6695
6696 Note #2: integer vector types follow the scalar floating-point types
6697 conventions to match what is implemented by the Sun VIS SDK.
6698
6699 Note #3: floating-point vector types follow the aggregate types
6700 conventions. */
6701
6702
6703 /* Maximum number of int regs for args. */
6704 #define SPARC_INT_ARG_MAX 6
6705 /* Maximum number of fp regs for args. */
6706 #define SPARC_FP_ARG_MAX 16
6707 /* Number of words (partially) occupied for a given size in units. */
6708 #define CEIL_NWORDS(SIZE) CEIL((SIZE), UNITS_PER_WORD)
6709
6710 /* Handle the INIT_CUMULATIVE_ARGS macro.
6711 Initialize a variable CUM of type CUMULATIVE_ARGS
6712 for a call to a function whose data type is FNTYPE.
6713 For a library call, FNTYPE is 0. */
6714
6715 void
6716 init_cumulative_args (struct sparc_args *cum, tree fntype, rtx, tree)
6717 {
6718 cum->words = 0;
6719 cum->prototype_p = fntype && prototype_p (fntype);
6720 cum->libcall_p = !fntype;
6721 }
6722
6723 /* Handle promotion of pointer and integer arguments. */
6724
6725 static machine_mode
6726 sparc_promote_function_mode (const_tree type, machine_mode mode,
6727 int *punsignedp, const_tree, int)
6728 {
6729 if (type && POINTER_TYPE_P (type))
6730 {
6731 *punsignedp = POINTERS_EXTEND_UNSIGNED;
6732 return Pmode;
6733 }
6734
6735 /* Integral arguments are passed as full words, as per the ABI. */
6736 if (GET_MODE_CLASS (mode) == MODE_INT
6737 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
6738 return word_mode;
6739
6740 return mode;
6741 }
6742
6743 /* Handle the TARGET_STRICT_ARGUMENT_NAMING target hook. */
6744
6745 static bool
6746 sparc_strict_argument_naming (cumulative_args_t ca ATTRIBUTE_UNUSED)
6747 {
6748 return TARGET_ARCH64 ? true : false;
6749 }
6750
6751 /* Handle the TARGET_PASS_BY_REFERENCE target hook.
6752 Specify whether to pass the argument by reference. */
6753
6754 static bool
6755 sparc_pass_by_reference (cumulative_args_t, const function_arg_info &arg)
6756 {
6757 tree type = arg.type;
6758 machine_mode mode = arg.mode;
6759 if (TARGET_ARCH32)
6760 /* Original SPARC 32-bit ABI says that structures and unions,
6761 and quad-precision floats are passed by reference.
6762 All other base types are passed in registers.
6763
6764 Extended ABI (as implemented by the Sun compiler) says that all
6765 complex floats are passed by reference. Pass complex integers
6766 in registers up to 8 bytes. More generally, enforce the 2-word
6767 cap for passing arguments in registers.
6768
6769 Vector ABI (as implemented by the Sun VIS SDK) says that integer
6770 vectors are passed like floats of the same size, that is in
6771 registers up to 8 bytes. Pass all vector floats by reference
6772 like structure and unions. */
6773 return ((type && (AGGREGATE_TYPE_P (type) || VECTOR_FLOAT_TYPE_P (type)))
6774 || mode == SCmode
6775 /* Catch CDImode, TFmode, DCmode and TCmode. */
6776 || GET_MODE_SIZE (mode) > 8
6777 || (type
6778 && VECTOR_TYPE_P (type)
6779 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8));
6780 else
6781 /* Original SPARC 64-bit ABI says that structures and unions
6782 smaller than 16 bytes are passed in registers, as well as
6783 all other base types.
6784
6785 Extended ABI (as implemented by the Sun compiler) says that
6786 complex floats are passed in registers up to 16 bytes. Pass
6787 all complex integers in registers up to 16 bytes. More generally,
6788 enforce the 2-word cap for passing arguments in registers.
6789
6790 Vector ABI (as implemented by the Sun VIS SDK) says that integer
6791 vectors are passed like floats of the same size, that is in
6792 registers (up to 16 bytes). Pass all vector floats like structure
6793 and unions. */
6794 return ((type
6795 && (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
6796 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 16)
6797 /* Catch CTImode and TCmode. */
6798 || GET_MODE_SIZE (mode) > 16);
6799 }
6800
6801 /* Return true if TYPE is considered as a floating-point type by the ABI. */
6802
6803 static bool
6804 fp_type_for_abi (const_tree type)
6805 {
6806 /* This is the original GCC implementation. */
6807 if (FLOAT_TYPE_P (type) || VECTOR_TYPE_P (type))
6808 return true;
6809
6810 /* This has been introduced in GCC 14 to match the vendor compiler. */
6811 if (SUN_V9_ABI_COMPATIBILITY && TREE_CODE (type) == ARRAY_TYPE)
6812 return fp_type_for_abi (TREE_TYPE (type));
6813
6814 return false;
6815 }
6816
6817 /* Traverse the record TYPE recursively and call FUNC on its fields.
6818 NAMED is true if this is for a named parameter. DATA is passed
6819 to FUNC for each field. OFFSET is the starting position and
6820 PACKED is true if we are inside a packed record. */
6821
6822 template <typename T, void Func (const_tree, int, bool, T*)>
6823 static void
6824 traverse_record_type (const_tree type, bool named, T *data,
6825 int offset = 0, bool packed = false)
6826 {
6827 /* The ABI obviously doesn't specify how packed structures are passed.
6828 These are passed in integer regs if possible, otherwise memory. */
6829 if (!packed)
6830 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6831 if (TREE_CODE (field) == FIELD_DECL && DECL_PACKED (field))
6832 {
6833 packed = true;
6834 break;
6835 }
6836
6837 /* Walk the real fields, but skip those with no size or a zero size.
6838 ??? Fields with variable offset are handled as having zero offset. */
6839 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6840 if (TREE_CODE (field) == FIELD_DECL)
6841 {
6842 if (!DECL_SIZE (field) || integer_zerop (DECL_SIZE (field)))
6843 continue;
6844
6845 int bitpos = offset;
6846 if (TREE_CODE (DECL_FIELD_OFFSET (field)) == INTEGER_CST)
6847 bitpos += int_bit_position (field);
6848
6849 tree field_type = TREE_TYPE (field);
6850 if (TREE_CODE (field_type) == RECORD_TYPE)
6851 traverse_record_type<T, Func> (field_type, named, data, bitpos,
6852 packed);
6853 else
6854 {
6855 const bool fp_type = fp_type_for_abi (field_type);
6856 Func (field, bitpos, fp_type && named && !packed && TARGET_FPU,
6857 data);
6858 }
6859 }
6860 }
6861
6862 /* Handle recursive register classifying for structure layout. */
6863
6864 typedef struct
6865 {
6866 bool fp_regs; /* true if field eligible to FP registers. */
6867 bool fp_regs_in_first_word; /* true if such field in first word. */
6868 } classify_data_t;
6869
6870 /* A subroutine of function_arg_slotno. Classify the field. */
6871
6872 inline void
6873 classify_registers (const_tree, int bitpos, bool fp, classify_data_t *data)
6874 {
6875 if (fp)
6876 {
6877 data->fp_regs = true;
6878 if (bitpos < BITS_PER_WORD)
6879 data->fp_regs_in_first_word = true;
6880 }
6881 }
6882
6883 /* Compute the slot number to pass an argument in.
6884 Return the slot number or -1 if passing on the stack.
6885
6886 CUM is a variable of type CUMULATIVE_ARGS which gives info about
6887 the preceding args and about the function being called.
6888 MODE is the argument's machine mode.
6889 TYPE is the data type of the argument (as a tree).
6890 This is null for libcalls where that information may
6891 not be available.
6892 NAMED is nonzero if this argument is a named parameter
6893 (otherwise it is an extra parameter matching an ellipsis).
6894 INCOMING is zero for FUNCTION_ARG, nonzero for FUNCTION_INCOMING_ARG.
6895 *PREGNO records the register number to use if scalar type.
6896 *PPADDING records the amount of padding needed in words. */
6897
6898 static int
6899 function_arg_slotno (const struct sparc_args *cum, machine_mode mode,
6900 const_tree type, bool named, bool incoming,
6901 int *pregno, int *ppadding)
6902 {
6903 const int regbase
6904 = incoming ? SPARC_INCOMING_INT_ARG_FIRST : SPARC_OUTGOING_INT_ARG_FIRST;
6905 int slotno = cum->words, regno;
6906 enum mode_class mclass = GET_MODE_CLASS (mode);
6907
6908 /* Silence warnings in the callers. */
6909 *pregno = -1;
6910 *ppadding = -1;
6911
6912 if (type && TREE_ADDRESSABLE (type))
6913 return -1;
6914
6915 /* In 64-bit mode, objects requiring 16-byte alignment get it. */
6916 if (TARGET_ARCH64
6917 && (type ? TYPE_ALIGN (type) : GET_MODE_ALIGNMENT (mode)) >= 128
6918 && (slotno & 1) != 0)
6919 {
6920 slotno++;
6921 *ppadding = 1;
6922 }
6923 else
6924 *ppadding = 0;
6925
6926 /* Vector types deserve special treatment because they are polymorphic wrt
6927 their mode, depending upon whether VIS instructions are enabled. */
6928 if (type && VECTOR_TYPE_P (type))
6929 {
6930 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (type)))
6931 {
6932 /* The SPARC port defines no floating-point vector modes. */
6933 gcc_assert (mode == BLKmode);
6934 }
6935 else
6936 {
6937 /* Integer vector types should either have a vector
6938 mode or an integral mode, because we are guaranteed
6939 by pass_by_reference that their size is not greater
6940 than 16 bytes and TImode is 16-byte wide. */
6941 gcc_assert (mode != BLKmode);
6942
6943 /* Integer vectors are handled like floats as per
6944 the Sun VIS SDK. */
6945 mclass = MODE_FLOAT;
6946 }
6947 }
6948
6949 switch (mclass)
6950 {
6951 case MODE_FLOAT:
6952 case MODE_COMPLEX_FLOAT:
6953 case MODE_VECTOR_INT:
6954 if (TARGET_ARCH64 && TARGET_FPU && named)
6955 {
6956 /* If all arg slots are filled, then must pass on stack. */
6957 if (slotno >= SPARC_FP_ARG_MAX)
6958 return -1;
6959
6960 regno = SPARC_FP_ARG_FIRST + slotno * 2;
6961 /* Arguments filling only one single FP register are
6962 right-justified in the outer double FP register. */
6963 if (GET_MODE_SIZE (mode) <= 4)
6964 regno++;
6965 break;
6966 }
6967 /* fallthrough */
6968
6969 case MODE_INT:
6970 case MODE_COMPLEX_INT:
6971 /* If all arg slots are filled, then must pass on stack. */
6972 if (slotno >= SPARC_INT_ARG_MAX)
6973 return -1;
6974
6975 regno = regbase + slotno;
6976 break;
6977
6978 case MODE_RANDOM:
6979 /* MODE is VOIDmode when generating the actual call. */
6980 if (mode == VOIDmode)
6981 return -1;
6982
6983 if (TARGET_64BIT && TARGET_FPU && named
6984 && type
6985 && (TREE_CODE (type) == RECORD_TYPE || VECTOR_TYPE_P (type)))
6986 {
6987 /* If all arg slots are filled, then must pass on stack. */
6988 if (slotno >= SPARC_FP_ARG_MAX)
6989 return -1;
6990
6991 if (TREE_CODE (type) == RECORD_TYPE)
6992 {
6993 classify_data_t data = { false, false };
6994 traverse_record_type<classify_data_t, classify_registers>
6995 (type, named, &data);
6996
6997 if (data.fp_regs)
6998 {
6999 /* If all FP slots are filled except for the last one and
7000 there is no FP field in the first word, then must pass
7001 on stack. */
7002 if (slotno >= SPARC_FP_ARG_MAX - 1
7003 && !data.fp_regs_in_first_word)
7004 return -1;
7005 }
7006 else
7007 {
7008 /* If all int slots are filled, then must pass on stack. */
7009 if (slotno >= SPARC_INT_ARG_MAX)
7010 return -1;
7011 }
7012
7013 /* PREGNO isn't set since both int and FP regs can be used. */
7014 return slotno;
7015 }
7016
7017 regno = SPARC_FP_ARG_FIRST + slotno * 2;
7018 }
7019 else
7020 {
7021 /* If all arg slots are filled, then must pass on stack. */
7022 if (slotno >= SPARC_INT_ARG_MAX)
7023 return -1;
7024
7025 regno = regbase + slotno;
7026 }
7027 break;
7028
7029 default :
7030 gcc_unreachable ();
7031 }
7032
7033 *pregno = regno;
7034 return slotno;
7035 }
7036
7037 /* Handle recursive register counting/assigning for structure layout. */
7038
7039 typedef struct
7040 {
7041 int slotno; /* slot number of the argument. */
7042 int regbase; /* regno of the base register. */
7043 int intoffset; /* offset of the first pending integer field. */
7044 int nregs; /* number of words passed in registers. */
7045 bool stack; /* true if part of the argument is on the stack. */
7046 rtx ret; /* return expression being built. */
7047 } assign_data_t;
7048
7049 /* A subroutine of function_arg_record_value. Compute the number of integer
7050 registers to be assigned between PARMS->intoffset and BITPOS. Return
7051 true if at least one integer register is assigned or false otherwise. */
7052
7053 static bool
7054 compute_int_layout (int bitpos, assign_data_t *data, int *pnregs)
7055 {
7056 if (data->intoffset < 0)
7057 return false;
7058
7059 const int intoffset = data->intoffset;
7060 data->intoffset = -1;
7061
7062 const int this_slotno = data->slotno + intoffset / BITS_PER_WORD;
7063 const unsigned int startbit = ROUND_DOWN (intoffset, BITS_PER_WORD);
7064 const unsigned int endbit = ROUND_UP (bitpos, BITS_PER_WORD);
7065 int nregs = (endbit - startbit) / BITS_PER_WORD;
7066
7067 if (nregs > 0 && nregs > SPARC_INT_ARG_MAX - this_slotno)
7068 {
7069 nregs = SPARC_INT_ARG_MAX - this_slotno;
7070
7071 /* We need to pass this field (partly) on the stack. */
7072 data->stack = 1;
7073 }
7074
7075 if (nregs <= 0)
7076 return false;
7077
7078 *pnregs = nregs;
7079 return true;
7080 }
7081
7082 /* A subroutine of function_arg_record_value. Compute the number and the mode
7083 of the FP registers to be assigned for FIELD. Return true if at least one
7084 FP register is assigned or false otherwise. */
7085
7086 static bool
7087 compute_fp_layout (const_tree field, int bitpos, assign_data_t *data,
7088 int *pnregs, machine_mode *pmode)
7089 {
7090 const int this_slotno = data->slotno + bitpos / BITS_PER_WORD;
7091 machine_mode mode = DECL_MODE (field);
7092 int nregs, nslots;
7093
7094 /* Slots are counted as words while regs are counted as having the size of
7095 the (inner) mode. */
7096 if (VECTOR_TYPE_P (TREE_TYPE (field)) && mode == BLKmode)
7097 {
7098 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (field)));
7099 nregs = TYPE_VECTOR_SUBPARTS (TREE_TYPE (field));
7100 }
7101 else if (TREE_CODE (TREE_TYPE (field)) == COMPLEX_TYPE)
7102 {
7103 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (field)));
7104 nregs = 2;
7105 }
7106 else if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE)
7107 {
7108 tree elt_type = strip_array_types (TREE_TYPE (field));
7109 mode = TYPE_MODE (elt_type);
7110 nregs
7111 = int_size_in_bytes (TREE_TYPE (field)) / int_size_in_bytes (elt_type);
7112 }
7113 else
7114 nregs = 1;
7115
7116 nslots = CEIL_NWORDS (nregs * GET_MODE_SIZE (mode));
7117
7118 if (nslots > SPARC_FP_ARG_MAX - this_slotno)
7119 {
7120 nslots = SPARC_FP_ARG_MAX - this_slotno;
7121 nregs = (nslots * UNITS_PER_WORD) / GET_MODE_SIZE (mode);
7122
7123 /* We need to pass this field (partly) on the stack. */
7124 data->stack = 1;
7125
7126 if (nregs <= 0)
7127 return false;
7128 }
7129
7130 *pnregs = nregs;
7131 *pmode = mode;
7132 return true;
7133 }
7134
7135 /* A subroutine of function_arg_record_value. Count the number of registers
7136 to be assigned for FIELD and between PARMS->intoffset and BITPOS. */
7137
7138 inline void
7139 count_registers (const_tree field, int bitpos, bool fp, assign_data_t *data)
7140 {
7141 if (fp)
7142 {
7143 int nregs;
7144 machine_mode mode;
7145
7146 if (compute_int_layout (bitpos, data, &nregs))
7147 data->nregs += nregs;
7148
7149 if (compute_fp_layout (field, bitpos, data, &nregs, &mode))
7150 data->nregs += nregs;
7151 }
7152 else
7153 {
7154 if (data->intoffset < 0)
7155 data->intoffset = bitpos;
7156 }
7157 }
7158
7159 /* A subroutine of function_arg_record_value. Assign the bits of the
7160 structure between PARMS->intoffset and BITPOS to integer registers. */
7161
7162 static void
7163 assign_int_registers (int bitpos, assign_data_t *data)
7164 {
7165 int intoffset = data->intoffset;
7166 machine_mode mode;
7167 int nregs;
7168
7169 if (!compute_int_layout (bitpos, data, &nregs))
7170 return;
7171
7172 /* If this is the trailing part of a word, only load that much into
7173 the register. Otherwise load the whole register. Note that in
7174 the latter case we may pick up unwanted bits. It's not a problem
7175 at the moment but may wish to revisit. */
7176 if (intoffset % BITS_PER_WORD != 0)
7177 mode = smallest_int_mode_for_size (BITS_PER_WORD
7178 - intoffset % BITS_PER_WORD).require ();
7179 else
7180 mode = word_mode;
7181
7182 const int this_slotno = data->slotno + intoffset / BITS_PER_WORD;
7183 unsigned int regno = data->regbase + this_slotno;
7184 intoffset /= BITS_PER_UNIT;
7185
7186 do
7187 {
7188 rtx reg = gen_rtx_REG (mode, regno);
7189 XVECEXP (data->ret, 0, data->stack + data->nregs)
7190 = gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (intoffset));
7191 data->nregs += 1;
7192 mode = word_mode;
7193 regno += 1;
7194 intoffset = (intoffset | (UNITS_PER_WORD - 1)) + 1;
7195 }
7196 while (--nregs > 0);
7197 }
7198
7199 /* A subroutine of function_arg_record_value. Assign FIELD at position
7200 BITPOS to FP registers. */
7201
7202 static void
7203 assign_fp_registers (const_tree field, int bitpos, assign_data_t *data)
7204 {
7205 int nregs;
7206 machine_mode mode;
7207
7208 if (!compute_fp_layout (field, bitpos, data, &nregs, &mode))
7209 return;
7210
7211 const int this_slotno = data->slotno + bitpos / BITS_PER_WORD;
7212 int regno = SPARC_FP_ARG_FIRST + this_slotno * 2;
7213 if (GET_MODE_SIZE (mode) <= 4 && (bitpos & 32) != 0)
7214 regno++;
7215 int pos = bitpos / BITS_PER_UNIT;
7216
7217 do
7218 {
7219 rtx reg = gen_rtx_REG (mode, regno);
7220 XVECEXP (data->ret, 0, data->stack + data->nregs)
7221 = gen_rtx_EXPR_LIST (VOIDmode, reg, GEN_INT (pos));
7222 data->nregs += 1;
7223 regno += GET_MODE_SIZE (mode) / 4;
7224 pos += GET_MODE_SIZE (mode);
7225 }
7226 while (--nregs > 0);
7227 }
7228
7229 /* A subroutine of function_arg_record_value. Assign FIELD and the bits of
7230 the structure between PARMS->intoffset and BITPOS to registers. */
7231
7232 inline void
7233 assign_registers (const_tree field, int bitpos, bool fp, assign_data_t *data)
7234 {
7235 if (fp)
7236 {
7237 assign_int_registers (bitpos, data);
7238
7239 assign_fp_registers (field, bitpos, data);
7240 }
7241 else
7242 {
7243 if (data->intoffset < 0)
7244 data->intoffset = bitpos;
7245 }
7246 }
7247
7248 /* Used by function_arg and function_value to implement the complex
7249 conventions of the 64-bit ABI for passing and returning structures.
7250 Return an expression valid as a return value for the FUNCTION_ARG
7251 and TARGET_FUNCTION_VALUE.
7252
7253 TYPE is the data type of the argument (as a tree).
7254 This is null for libcalls where that information may
7255 not be available.
7256 MODE is the argument's machine mode.
7257 SLOTNO is the index number of the argument's slot in the parameter array.
7258 NAMED is true if this argument is a named parameter
7259 (otherwise it is an extra parameter matching an ellipsis).
7260 REGBASE is the regno of the base register for the parameter array. */
7261
7262 static rtx
7263 function_arg_record_value (const_tree type, machine_mode mode,
7264 int slotno, bool named, int regbase)
7265 {
7266 const int size = int_size_in_bytes (type);
7267 assign_data_t data;
7268 int nregs;
7269
7270 data.slotno = slotno;
7271 data.regbase = regbase;
7272
7273 /* Count how many registers we need. */
7274 data.nregs = 0;
7275 data.intoffset = 0;
7276 data.stack = false;
7277 traverse_record_type<assign_data_t, count_registers> (type, named, &data);
7278
7279 /* Take into account pending integer fields. */
7280 if (compute_int_layout (size * BITS_PER_UNIT, &data, &nregs))
7281 data.nregs += nregs;
7282
7283 /* Allocate the vector and handle some annoying special cases. */
7284 nregs = data.nregs;
7285
7286 if (nregs == 0)
7287 {
7288 /* ??? Empty structure has no value? Duh? */
7289 if (size <= 0)
7290 {
7291 /* Though there's nothing really to store, return a word register
7292 anyway so the rest of gcc doesn't go nuts. Returning a PARALLEL
7293 leads to breakage due to the fact that there are zero bytes to
7294 load. */
7295 return gen_rtx_REG (mode, regbase);
7296 }
7297
7298 /* ??? C++ has structures with no fields, and yet a size. Give up
7299 for now and pass everything back in integer registers. */
7300 nregs = CEIL_NWORDS (size);
7301 if (nregs + slotno > SPARC_INT_ARG_MAX)
7302 nregs = SPARC_INT_ARG_MAX - slotno;
7303 }
7304
7305 gcc_assert (nregs > 0);
7306
7307 data.ret = gen_rtx_PARALLEL (mode, rtvec_alloc (data.stack + nregs));
7308
7309 /* If at least one field must be passed on the stack, generate
7310 (parallel [(expr_list (nil) ...) ...]) so that all fields will
7311 also be passed on the stack. We can't do much better because the
7312 semantics of TARGET_ARG_PARTIAL_BYTES doesn't handle the case
7313 of structures for which the fields passed exclusively in registers
7314 are not at the beginning of the structure. */
7315 if (data.stack)
7316 XVECEXP (data.ret, 0, 0)
7317 = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
7318
7319 /* Assign the registers. */
7320 data.nregs = 0;
7321 data.intoffset = 0;
7322 traverse_record_type<assign_data_t, assign_registers> (type, named, &data);
7323
7324 /* Assign pending integer fields. */
7325 assign_int_registers (size * BITS_PER_UNIT, &data);
7326
7327 gcc_assert (data.nregs == nregs);
7328
7329 return data.ret;
7330 }
7331
7332 /* Used by function_arg and function_value to implement the conventions
7333 of the 64-bit ABI for passing and returning unions.
7334 Return an expression valid as a return value for the FUNCTION_ARG
7335 and TARGET_FUNCTION_VALUE.
7336
7337 SIZE is the size in bytes of the union.
7338 MODE is the argument's machine mode.
7339 SLOTNO is the index number of the argument's slot in the parameter array.
7340 REGNO is the hard register the union will be passed in. */
7341
7342 static rtx
7343 function_arg_union_value (int size, machine_mode mode, int slotno, int regno)
7344 {
7345 unsigned int nwords;
7346
7347 /* See comment in function_arg_record_value for empty structures. */
7348 if (size <= 0)
7349 return gen_rtx_REG (mode, regno);
7350
7351 if (slotno == SPARC_INT_ARG_MAX - 1)
7352 nwords = 1;
7353 else
7354 nwords = CEIL_NWORDS (size);
7355
7356 rtx regs = gen_rtx_PARALLEL (mode, rtvec_alloc (nwords));
7357
7358 /* Unions are passed left-justified. */
7359 for (unsigned int i = 0; i < nwords; i++)
7360 XVECEXP (regs, 0, i)
7361 = gen_rtx_EXPR_LIST (VOIDmode,
7362 gen_rtx_REG (word_mode, regno + i),
7363 GEN_INT (UNITS_PER_WORD * i));
7364
7365 return regs;
7366 }
7367
7368 /* Used by function_arg and function_value to implement the conventions
7369 of the 64-bit ABI for passing and returning BLKmode vectors.
7370 Return an expression valid as a return value for the FUNCTION_ARG
7371 and TARGET_FUNCTION_VALUE.
7372
7373 SIZE is the size in bytes of the vector.
7374 SLOTNO is the index number of the argument's slot in the parameter array.
7375 NAMED is true if this argument is a named parameter
7376 (otherwise it is an extra parameter matching an ellipsis).
7377 REGNO is the hard register the vector will be passed in. */
7378
7379 static rtx
7380 function_arg_vector_value (int size, int slotno, bool named, int regno)
7381 {
7382 const int mult = (named ? 2 : 1);
7383 unsigned int nwords;
7384
7385 if (slotno == (named ? SPARC_FP_ARG_MAX : SPARC_INT_ARG_MAX) - 1)
7386 nwords = 1;
7387 else
7388 nwords = CEIL_NWORDS (size);
7389
7390 rtx regs = gen_rtx_PARALLEL (BLKmode, rtvec_alloc (nwords));
7391
7392 if (size < UNITS_PER_WORD)
7393 XVECEXP (regs, 0, 0)
7394 = gen_rtx_EXPR_LIST (VOIDmode,
7395 gen_rtx_REG (SImode, regno),
7396 const0_rtx);
7397 else
7398 for (unsigned int i = 0; i < nwords; i++)
7399 XVECEXP (regs, 0, i)
7400 = gen_rtx_EXPR_LIST (VOIDmode,
7401 gen_rtx_REG (word_mode, regno + i * mult),
7402 GEN_INT (i * UNITS_PER_WORD));
7403
7404 return regs;
7405 }
7406
7407 /* Determine where to put an argument to a function.
7408 Value is zero to push the argument on the stack,
7409 or a hard register in which to store the argument.
7410
7411 CUM is a variable of type CUMULATIVE_ARGS which gives info about
7412 the preceding args and about the function being called.
7413 ARG is a description of the argument.
7414 INCOMING_P is false for TARGET_FUNCTION_ARG, true for
7415 TARGET_FUNCTION_INCOMING_ARG. */
7416
7417 static rtx
7418 sparc_function_arg_1 (cumulative_args_t cum_v, const function_arg_info &arg,
7419 bool incoming)
7420 {
7421 const CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
7422 const int regbase
7423 = incoming ? SPARC_INCOMING_INT_ARG_FIRST : SPARC_OUTGOING_INT_ARG_FIRST;
7424 int slotno, regno, padding;
7425 tree type = arg.type;
7426 machine_mode mode = arg.mode;
7427 enum mode_class mclass = GET_MODE_CLASS (mode);
7428 bool named = arg.named;
7429
7430 slotno
7431 = function_arg_slotno (cum, mode, type, named, incoming, &regno, &padding);
7432 if (slotno == -1)
7433 return 0;
7434
7435 /* Integer vectors are handled like floats as per the Sun VIS SDK. */
7436 if (type && VECTOR_INTEGER_TYPE_P (type))
7437 mclass = MODE_FLOAT;
7438
7439 if (TARGET_ARCH32)
7440 return gen_rtx_REG (mode, regno);
7441
7442 /* Structures up to 16 bytes in size are passed in arg slots on the stack
7443 and are promoted to registers if possible. */
7444 if (type && TREE_CODE (type) == RECORD_TYPE)
7445 {
7446 const int size = int_size_in_bytes (type);
7447 gcc_assert (size <= 16);
7448
7449 return function_arg_record_value (type, mode, slotno, named, regbase);
7450 }
7451
7452 /* Unions up to 16 bytes in size are passed in integer registers. */
7453 else if (type && TREE_CODE (type) == UNION_TYPE)
7454 {
7455 const int size = int_size_in_bytes (type);
7456 gcc_assert (size <= 16);
7457
7458 return function_arg_union_value (size, mode, slotno, regno);
7459 }
7460
7461 /* Floating-point vectors up to 16 bytes are passed in registers. */
7462 else if (type && VECTOR_TYPE_P (type) && mode == BLKmode)
7463 {
7464 const int size = int_size_in_bytes (type);
7465 gcc_assert (size <= 16);
7466
7467 return function_arg_vector_value (size, slotno, named, regno);
7468 }
7469
7470 /* v9 fp args in reg slots beyond the int reg slots get passed in regs
7471 but also have the slot allocated for them.
7472 If no prototype is in scope fp values in register slots get passed
7473 in two places, either fp regs and int regs or fp regs and memory. */
7474 else if ((mclass == MODE_FLOAT || mclass == MODE_COMPLEX_FLOAT)
7475 && SPARC_FP_REG_P (regno))
7476 {
7477 rtx reg = gen_rtx_REG (mode, regno);
7478 if (cum->prototype_p || cum->libcall_p)
7479 return reg;
7480 else
7481 {
7482 rtx v0, v1;
7483
7484 if ((regno - SPARC_FP_ARG_FIRST) < SPARC_INT_ARG_MAX * 2)
7485 {
7486 int intreg;
7487
7488 /* On incoming, we don't need to know that the value
7489 is passed in %f0 and %i0, and it confuses other parts
7490 causing needless spillage even on the simplest cases. */
7491 if (incoming)
7492 return reg;
7493
7494 intreg = (SPARC_OUTGOING_INT_ARG_FIRST
7495 + (regno - SPARC_FP_ARG_FIRST) / 2);
7496
7497 v0 = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
7498 v1 = gen_rtx_EXPR_LIST (VOIDmode, gen_rtx_REG (mode, intreg),
7499 const0_rtx);
7500 return gen_rtx_PARALLEL (mode, gen_rtvec (2, v0, v1));
7501 }
7502 else
7503 {
7504 v0 = gen_rtx_EXPR_LIST (VOIDmode, NULL_RTX, const0_rtx);
7505 v1 = gen_rtx_EXPR_LIST (VOIDmode, reg, const0_rtx);
7506 return gen_rtx_PARALLEL (mode, gen_rtvec (2, v0, v1));
7507 }
7508 }
7509 }
7510
7511 /* All other aggregate types are passed in an integer register in a mode
7512 corresponding to the size of the type. */
7513 else if (type && AGGREGATE_TYPE_P (type))
7514 {
7515 const int size = int_size_in_bytes (type);
7516 gcc_assert (size <= 16);
7517
7518 mode = int_mode_for_size (size * BITS_PER_UNIT, 0).else_blk ();
7519 }
7520
7521 return gen_rtx_REG (mode, regno);
7522 }
7523
7524 /* Handle the TARGET_FUNCTION_ARG target hook. */
7525
7526 static rtx
7527 sparc_function_arg (cumulative_args_t cum, const function_arg_info &arg)
7528 {
7529 return sparc_function_arg_1 (cum, arg, false);
7530 }
7531
7532 /* Handle the TARGET_FUNCTION_INCOMING_ARG target hook. */
7533
7534 static rtx
7535 sparc_function_incoming_arg (cumulative_args_t cum,
7536 const function_arg_info &arg)
7537 {
7538 return sparc_function_arg_1 (cum, arg, true);
7539 }
7540
7541 /* For sparc64, objects requiring 16 byte alignment are passed that way. */
7542
7543 static unsigned int
7544 sparc_function_arg_boundary (machine_mode mode, const_tree type)
7545 {
7546 return ((TARGET_ARCH64
7547 && (GET_MODE_ALIGNMENT (mode) == 128
7548 || (type && TYPE_ALIGN (type) == 128)))
7549 ? 128
7550 : PARM_BOUNDARY);
7551 }
7552
7553 /* For an arg passed partly in registers and partly in memory,
7554 this is the number of bytes of registers used.
7555 For args passed entirely in registers or entirely in memory, zero.
7556
7557 Any arg that starts in the first 6 regs but won't entirely fit in them
7558 needs partial registers on v8. On v9, structures with integer
7559 values in arg slots 5,6 will be passed in %o5 and SP+176, and complex fp
7560 values that begin in the last fp reg [where "last fp reg" varies with the
7561 mode] will be split between that reg and memory. */
7562
7563 static int
7564 sparc_arg_partial_bytes (cumulative_args_t cum, const function_arg_info &arg)
7565 {
7566 int slotno, regno, padding;
7567
7568 /* We pass false for incoming here, it doesn't matter. */
7569 slotno = function_arg_slotno (get_cumulative_args (cum), arg.mode, arg.type,
7570 arg.named, false, &regno, &padding);
7571
7572 if (slotno == -1)
7573 return 0;
7574
7575 if (TARGET_ARCH32)
7576 {
7577 /* We are guaranteed by pass_by_reference that the size of the
7578 argument is not greater than 8 bytes, so we only need to return
7579 one word if the argument is partially passed in registers. */
7580 const int size = GET_MODE_SIZE (arg.mode);
7581
7582 if (size > UNITS_PER_WORD && slotno == SPARC_INT_ARG_MAX - 1)
7583 return UNITS_PER_WORD;
7584 }
7585 else
7586 {
7587 /* We are guaranteed by pass_by_reference that the size of the
7588 argument is not greater than 16 bytes, so we only need to return
7589 one word if the argument is partially passed in registers. */
7590 if (arg.aggregate_type_p ())
7591 {
7592 const int size = int_size_in_bytes (arg.type);
7593
7594 if (size > UNITS_PER_WORD
7595 && (slotno == SPARC_INT_ARG_MAX - 1
7596 || slotno == SPARC_FP_ARG_MAX - 1))
7597 return UNITS_PER_WORD;
7598 }
7599 else if (GET_MODE_CLASS (arg.mode) == MODE_COMPLEX_INT
7600 || ((GET_MODE_CLASS (arg.mode) == MODE_COMPLEX_FLOAT
7601 || (arg.type && VECTOR_TYPE_P (arg.type)))
7602 && !(TARGET_FPU && arg.named)))
7603 {
7604 const int size = (arg.type && VECTOR_FLOAT_TYPE_P (arg.type))
7605 ? int_size_in_bytes (arg.type)
7606 : GET_MODE_SIZE (arg.mode);
7607
7608 if (size > UNITS_PER_WORD && slotno == SPARC_INT_ARG_MAX - 1)
7609 return UNITS_PER_WORD;
7610 }
7611 else if (GET_MODE_CLASS (arg.mode) == MODE_COMPLEX_FLOAT
7612 || (arg.type && VECTOR_TYPE_P (arg.type)))
7613 {
7614 const int size = (arg.type && VECTOR_FLOAT_TYPE_P (arg.type))
7615 ? int_size_in_bytes (arg.type)
7616 : GET_MODE_SIZE (arg.mode);
7617
7618 if (size > UNITS_PER_WORD && slotno == SPARC_FP_ARG_MAX - 1)
7619 return UNITS_PER_WORD;
7620 }
7621 }
7622
7623 return 0;
7624 }
7625
7626 /* Handle the TARGET_FUNCTION_ARG_ADVANCE hook.
7627 Update the data in CUM to advance over argument ARG. */
7628
7629 static void
7630 sparc_function_arg_advance (cumulative_args_t cum_v,
7631 const function_arg_info &arg)
7632 {
7633 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
7634 tree type = arg.type;
7635 machine_mode mode = arg.mode;
7636 int regno, padding;
7637
7638 /* We pass false for incoming here, it doesn't matter. */
7639 function_arg_slotno (cum, mode, type, arg.named, false, &regno, &padding);
7640
7641 /* If argument requires leading padding, add it. */
7642 cum->words += padding;
7643
7644 if (TARGET_ARCH32)
7645 cum->words += CEIL_NWORDS (GET_MODE_SIZE (mode));
7646 else
7647 {
7648 /* For types that can have BLKmode, get the size from the type. */
7649 if (type && (AGGREGATE_TYPE_P (type) || VECTOR_FLOAT_TYPE_P (type)))
7650 {
7651 const int size = int_size_in_bytes (type);
7652
7653 /* See comment in function_arg_record_value for empty structures. */
7654 if (size <= 0)
7655 cum->words++;
7656 else
7657 cum->words += CEIL_NWORDS (size);
7658 }
7659 else
7660 cum->words += CEIL_NWORDS (GET_MODE_SIZE (mode));
7661 }
7662 }
7663
7664 /* Implement TARGET_FUNCTION_ARG_PADDING. For the 64-bit ABI structs
7665 are always stored left shifted in their argument slot. */
7666
7667 static pad_direction
7668 sparc_function_arg_padding (machine_mode mode, const_tree type)
7669 {
7670 if (TARGET_ARCH64 && type && AGGREGATE_TYPE_P (type))
7671 return PAD_UPWARD;
7672
7673 /* Fall back to the default. */
7674 return default_function_arg_padding (mode, type);
7675 }
7676
7677 /* Handle the TARGET_RETURN_IN_MEMORY target hook.
7678 Specify whether to return the return value in memory. */
7679
7680 static bool
7681 sparc_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
7682 {
7683 if (TARGET_ARCH32)
7684 /* Original SPARC 32-bit ABI says that structures and unions, and
7685 quad-precision floats are returned in memory. But note that the
7686 first part is implemented through -fpcc-struct-return being the
7687 default, so here we only implement -freg-struct-return instead.
7688 All other base types are returned in registers.
7689
7690 Extended ABI (as implemented by the Sun compiler) says that
7691 all complex floats are returned in registers (8 FP registers
7692 at most for '_Complex long double'). Return all complex integers
7693 in registers (4 at most for '_Complex long long').
7694
7695 Vector ABI (as implemented by the Sun VIS SDK) says that vector
7696 integers are returned like floats of the same size, that is in
7697 registers up to 8 bytes and in memory otherwise. Return all
7698 vector floats in memory like structure and unions; note that
7699 they always have BLKmode like the latter. */
7700 return (TYPE_MODE (type) == BLKmode
7701 || TYPE_MODE (type) == TFmode
7702 || (TREE_CODE (type) == VECTOR_TYPE
7703 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 8));
7704 else
7705 /* Original SPARC 64-bit ABI says that structures and unions
7706 smaller than 32 bytes are returned in registers, as well as
7707 all other base types.
7708
7709 Extended ABI (as implemented by the Sun compiler) says that all
7710 complex floats are returned in registers (8 FP registers at most
7711 for '_Complex long double'). Return all complex integers in
7712 registers (4 at most for '_Complex TItype').
7713
7714 Vector ABI (as implemented by the Sun VIS SDK) says that vector
7715 integers are returned like floats of the same size, that is in
7716 registers. Return all vector floats like structure and unions;
7717 note that they always have BLKmode like the latter. */
7718 return (TYPE_MODE (type) == BLKmode
7719 && (unsigned HOST_WIDE_INT) int_size_in_bytes (type) > 32);
7720 }
7721
7722 /* Handle the TARGET_STRUCT_VALUE target hook.
7723 Return where to find the structure return value address. */
7724
7725 static rtx
7726 sparc_struct_value_rtx (tree fndecl, int incoming)
7727 {
7728 if (TARGET_ARCH64)
7729 return NULL_RTX;
7730 else
7731 {
7732 rtx mem;
7733
7734 if (incoming)
7735 mem = gen_frame_mem (Pmode, plus_constant (Pmode, frame_pointer_rtx,
7736 STRUCT_VALUE_OFFSET));
7737 else
7738 mem = gen_frame_mem (Pmode, plus_constant (Pmode, stack_pointer_rtx,
7739 STRUCT_VALUE_OFFSET));
7740
7741 /* Only follow the SPARC ABI for fixed-size structure returns.
7742 Variable size structure returns are handled per the normal
7743 procedures in GCC. This is enabled by -mstd-struct-return */
7744 if (incoming == 2
7745 && sparc_std_struct_return
7746 && TYPE_SIZE_UNIT (TREE_TYPE (fndecl))
7747 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (fndecl))) == INTEGER_CST)
7748 {
7749 /* We must check and adjust the return address, as it is optional
7750 as to whether the return object is really provided. */
7751 rtx ret_reg = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
7752 rtx scratch = gen_reg_rtx (SImode);
7753 rtx_code_label *endlab = gen_label_rtx ();
7754
7755 /* Calculate the return object size. */
7756 tree size = TYPE_SIZE_UNIT (TREE_TYPE (fndecl));
7757 rtx size_rtx = GEN_INT (TREE_INT_CST_LOW (size) & 0xfff);
7758 /* Construct a temporary return value. */
7759 rtx temp_val
7760 = assign_stack_local (Pmode, TREE_INT_CST_LOW (size), 0);
7761
7762 /* Implement SPARC 32-bit psABI callee return struct checking:
7763
7764 Fetch the instruction where we will return to and see if
7765 it's an unimp instruction (the most significant 10 bits
7766 will be zero). */
7767 emit_move_insn (scratch, gen_rtx_MEM (SImode,
7768 plus_constant (Pmode,
7769 ret_reg, 8)));
7770 /* Assume the size is valid and pre-adjust. */
7771 emit_insn (gen_add3_insn (ret_reg, ret_reg, GEN_INT (4)));
7772 emit_cmp_and_jump_insns (scratch, size_rtx, EQ, const0_rtx, SImode,
7773 0, endlab);
7774 emit_insn (gen_sub3_insn (ret_reg, ret_reg, GEN_INT (4)));
7775 /* Write the address of the memory pointed to by temp_val into
7776 the memory pointed to by mem. */
7777 emit_move_insn (mem, XEXP (temp_val, 0));
7778 emit_label (endlab);
7779 }
7780
7781 return mem;
7782 }
7783 }
7784
7785 /* Handle TARGET_FUNCTION_VALUE, and TARGET_LIBCALL_VALUE target hook.
7786 For v9, function return values are subject to the same rules as arguments,
7787 except that up to 32 bytes may be returned in registers. */
7788
7789 static rtx
7790 sparc_function_value_1 (const_tree type, machine_mode mode, bool outgoing)
7791 {
7792 /* Beware that the two values are swapped here wrt function_arg. */
7793 const int regbase
7794 = outgoing ? SPARC_INCOMING_INT_ARG_FIRST : SPARC_OUTGOING_INT_ARG_FIRST;
7795 enum mode_class mclass = GET_MODE_CLASS (mode);
7796 int regno;
7797
7798 /* Integer vectors are handled like floats as per the Sun VIS SDK.
7799 Note that integer vectors larger than 16 bytes have BLKmode so
7800 they need to be handled like floating-point vectors below. */
7801 if (type && VECTOR_INTEGER_TYPE_P (type) && mode != BLKmode)
7802 mclass = MODE_FLOAT;
7803
7804 if (TARGET_ARCH64 && type)
7805 {
7806 /* Structures up to 32 bytes in size are returned in registers. */
7807 if (TREE_CODE (type) == RECORD_TYPE)
7808 {
7809 const int size = int_size_in_bytes (type);
7810 gcc_assert (size <= 32);
7811
7812 return function_arg_record_value (type, mode, 0, true, regbase);
7813 }
7814
7815 /* Unions up to 32 bytes in size are returned in integer registers. */
7816 else if (TREE_CODE (type) == UNION_TYPE)
7817 {
7818 const int size = int_size_in_bytes (type);
7819 gcc_assert (size <= 32);
7820
7821 return function_arg_union_value (size, mode, 0, regbase);
7822 }
7823
7824 /* Vectors up to 32 bytes are returned in FP registers. */
7825 else if (VECTOR_TYPE_P (type) && mode == BLKmode)
7826 {
7827 const int size = int_size_in_bytes (type);
7828 gcc_assert (size <= 32);
7829
7830 return function_arg_vector_value (size, 0, true, SPARC_FP_ARG_FIRST);
7831 }
7832
7833 /* Objects that require it are returned in FP registers. */
7834 else if (mclass == MODE_FLOAT || mclass == MODE_COMPLEX_FLOAT)
7835 ;
7836
7837 /* All other aggregate types are returned in an integer register in a
7838 mode corresponding to the size of the type. */
7839 else if (AGGREGATE_TYPE_P (type))
7840 {
7841 /* All other aggregate types are passed in an integer register
7842 in a mode corresponding to the size of the type. */
7843 const int size = int_size_in_bytes (type);
7844 gcc_assert (size <= 32);
7845
7846 mode = int_mode_for_size (size * BITS_PER_UNIT, 0).else_blk ();
7847
7848 /* ??? We probably should have made the same ABI change in
7849 3.4.0 as the one we made for unions. The latter was
7850 required by the SCD though, while the former is not
7851 specified, so we favored compatibility and efficiency.
7852
7853 Now we're stuck for aggregates larger than 16 bytes,
7854 because OImode vanished in the meantime. Let's not
7855 try to be unduly clever, and simply follow the ABI
7856 for unions in that case. */
7857 if (mode == BLKmode)
7858 return function_arg_union_value (size, mode, 0, regbase);
7859 else
7860 mclass = MODE_INT;
7861 }
7862
7863 /* We should only have pointer and integer types at this point. This
7864 must match sparc_promote_function_mode. */
7865 else if (mclass == MODE_INT && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
7866 mode = word_mode;
7867 }
7868
7869 /* We should only have pointer and integer types at this point, except with
7870 -freg-struct-return. This must match sparc_promote_function_mode. */
7871 else if (TARGET_ARCH32
7872 && !(type && AGGREGATE_TYPE_P (type))
7873 && mclass == MODE_INT
7874 && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
7875 mode = word_mode;
7876
7877 if ((mclass == MODE_FLOAT || mclass == MODE_COMPLEX_FLOAT) && TARGET_FPU)
7878 regno = SPARC_FP_ARG_FIRST;
7879 else
7880 regno = regbase;
7881
7882 return gen_rtx_REG (mode, regno);
7883 }
7884
7885 /* Handle TARGET_FUNCTION_VALUE.
7886 On the SPARC, the value is found in the first "output" register, but the
7887 called function leaves it in the first "input" register. */
7888
7889 static rtx
7890 sparc_function_value (const_tree valtype,
7891 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
7892 bool outgoing)
7893 {
7894 return sparc_function_value_1 (valtype, TYPE_MODE (valtype), outgoing);
7895 }
7896
7897 /* Handle TARGET_LIBCALL_VALUE. */
7898
7899 static rtx
7900 sparc_libcall_value (machine_mode mode,
7901 const_rtx fun ATTRIBUTE_UNUSED)
7902 {
7903 return sparc_function_value_1 (NULL_TREE, mode, false);
7904 }
7905
7906 /* Handle FUNCTION_VALUE_REGNO_P.
7907 On the SPARC, the first "output" reg is used for integer values, and the
7908 first floating point register is used for floating point values. */
7909
7910 static bool
7911 sparc_function_value_regno_p (const unsigned int regno)
7912 {
7913 return (regno == 8 || (TARGET_FPU && regno == 32));
7914 }
7915
7916 /* Do what is necessary for `va_start'. We look at the current function
7917 to determine if stdarg or varargs is used and return the address of
7918 the first unnamed parameter. */
7919
7920 static rtx
7921 sparc_builtin_saveregs (void)
7922 {
7923 int first_reg = crtl->args.info.words;
7924 rtx address;
7925 int regno;
7926
7927 for (regno = first_reg; regno < SPARC_INT_ARG_MAX; regno++)
7928 emit_move_insn (gen_rtx_MEM (word_mode,
7929 gen_rtx_PLUS (Pmode,
7930 frame_pointer_rtx,
7931 GEN_INT (FIRST_PARM_OFFSET (0)
7932 + (UNITS_PER_WORD
7933 * regno)))),
7934 gen_rtx_REG (word_mode,
7935 SPARC_INCOMING_INT_ARG_FIRST + regno));
7936
7937 address = gen_rtx_PLUS (Pmode,
7938 frame_pointer_rtx,
7939 GEN_INT (FIRST_PARM_OFFSET (0)
7940 + UNITS_PER_WORD * first_reg));
7941
7942 return address;
7943 }
7944
7945 /* Implement `va_start' for stdarg. */
7946
7947 static void
7948 sparc_va_start (tree valist, rtx nextarg)
7949 {
7950 nextarg = expand_builtin_saveregs ();
7951 std_expand_builtin_va_start (valist, nextarg);
7952 }
7953
7954 /* Implement `va_arg' for stdarg. */
7955
7956 static tree
7957 sparc_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
7958 gimple_seq *post_p)
7959 {
7960 HOST_WIDE_INT size, rsize, align;
7961 tree addr, incr;
7962 bool indirect;
7963 tree ptrtype = build_pointer_type (type);
7964
7965 if (pass_va_arg_by_reference (type))
7966 {
7967 indirect = true;
7968 size = rsize = UNITS_PER_WORD;
7969 align = 0;
7970 }
7971 else
7972 {
7973 indirect = false;
7974 size = int_size_in_bytes (type);
7975 rsize = ROUND_UP (size, UNITS_PER_WORD);
7976 align = 0;
7977
7978 if (TARGET_ARCH64)
7979 {
7980 /* For SPARC64, objects requiring 16-byte alignment get it. */
7981 if (TYPE_ALIGN (type) >= 2 * (unsigned) BITS_PER_WORD)
7982 align = 2 * UNITS_PER_WORD;
7983
7984 /* SPARC-V9 ABI states that structures up to 16 bytes in size
7985 are left-justified in their slots. */
7986 if (AGGREGATE_TYPE_P (type))
7987 {
7988 if (size == 0)
7989 size = rsize = UNITS_PER_WORD;
7990 else
7991 size = rsize;
7992 }
7993 }
7994 }
7995
7996 incr = valist;
7997 if (align)
7998 {
7999 incr = fold_build_pointer_plus_hwi (incr, align - 1);
8000 incr = fold_convert (sizetype, incr);
8001 incr = fold_build2 (BIT_AND_EXPR, sizetype, incr,
8002 size_int (-align));
8003 incr = fold_convert (ptr_type_node, incr);
8004 }
8005
8006 gimplify_expr (&incr, pre_p, post_p, is_gimple_val, fb_rvalue);
8007 addr = incr;
8008
8009 if (BYTES_BIG_ENDIAN && size < rsize)
8010 addr = fold_build_pointer_plus_hwi (incr, rsize - size);
8011
8012 if (indirect)
8013 {
8014 addr = fold_convert (build_pointer_type (ptrtype), addr);
8015 addr = build_va_arg_indirect_ref (addr);
8016 }
8017
8018 /* If the address isn't aligned properly for the type, we need a temporary.
8019 FIXME: This is inefficient, usually we can do this in registers. */
8020 else if (align == 0 && TYPE_ALIGN (type) > BITS_PER_WORD)
8021 {
8022 tree tmp = create_tmp_var (type, "va_arg_tmp");
8023 tree dest_addr = build_fold_addr_expr (tmp);
8024 tree copy = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMCPY),
8025 3, dest_addr, addr, size_int (rsize));
8026 TREE_ADDRESSABLE (tmp) = 1;
8027 gimplify_and_add (copy, pre_p);
8028 addr = dest_addr;
8029 }
8030
8031 else
8032 addr = fold_convert (ptrtype, addr);
8033
8034 incr = fold_build_pointer_plus_hwi (incr, rsize);
8035 gimplify_assign (valist, incr, post_p);
8036
8037 return build_va_arg_indirect_ref (addr);
8038 }
8039
8040 /* Implement the TARGET_VECTOR_MODE_SUPPORTED_P target hook.
8041 Specify whether the vector mode is supported by the hardware. */
8042
8043 static bool
8044 sparc_vector_mode_supported_p (machine_mode mode)
8045 {
8046 return TARGET_VIS && VECTOR_MODE_P (mode) ? true : false;
8047 }
8048
8049 /* Implement the TARGET_VECTORIZE_PREFERRED_SIMD_MODE target hook. */
8050
8051 static machine_mode
8052 sparc_preferred_simd_mode (scalar_mode mode)
8053 {
8054 if (TARGET_VIS)
8055 switch (mode)
8056 {
8057 case E_SImode:
8058 return V2SImode;
8059 case E_HImode:
8060 return V4HImode;
8061 case E_QImode:
8062 return V8QImode;
8063
8064 default:;
8065 }
8066
8067 return word_mode;
8068 }
8069
8070 /* Implement TARGET_CAN_FOLLOW_JUMP. */
8071
8072 static bool
8073 sparc_can_follow_jump (const rtx_insn *follower, const rtx_insn *followee)
8074 {
8075 /* Do not fold unconditional jumps that have been created for crossing
8076 partition boundaries. */
8077 if (CROSSING_JUMP_P (followee) && !CROSSING_JUMP_P (follower))
8078 return false;
8079
8080 return true;
8081 }
8082
8083 /* Return the string to output an unconditional branch to LABEL, which is
8084 the operand number of the label.
8085
8086 DEST is the destination insn (i.e. the label), INSN is the source. */
8087
8088 const char *
8089 output_ubranch (rtx dest, rtx_insn *insn)
8090 {
8091 static char string[64];
8092 bool v9_form = false;
8093 int delta;
8094 char *p;
8095
8096 /* Even if we are trying to use cbcond for this, evaluate
8097 whether we can use V9 branches as our backup plan. */
8098 delta = 5000000;
8099 if (!CROSSING_JUMP_P (insn) && INSN_ADDRESSES_SET_P ())
8100 delta = (INSN_ADDRESSES (INSN_UID (dest))
8101 - INSN_ADDRESSES (INSN_UID (insn)));
8102
8103 /* Leave some instructions for "slop". */
8104 if (TARGET_V9 && delta >= -260000 && delta < 260000)
8105 v9_form = true;
8106
8107 if (TARGET_CBCOND)
8108 {
8109 bool emit_nop = emit_cbcond_nop (insn);
8110 bool far = false;
8111 const char *rval;
8112
8113 if (delta < -500 || delta > 500)
8114 far = true;
8115
8116 if (far)
8117 {
8118 if (v9_form)
8119 rval = "ba,a,pt\t%%xcc, %l0";
8120 else
8121 rval = "b,a\t%l0";
8122 }
8123 else
8124 {
8125 if (emit_nop)
8126 rval = "cwbe\t%%g0, %%g0, %l0\n\tnop";
8127 else
8128 rval = "cwbe\t%%g0, %%g0, %l0";
8129 }
8130 return rval;
8131 }
8132
8133 if (v9_form)
8134 strcpy (string, "ba%*,pt\t%%xcc, ");
8135 else
8136 strcpy (string, "b%*\t");
8137
8138 p = strchr (string, '\0');
8139 *p++ = '%';
8140 *p++ = 'l';
8141 *p++ = '0';
8142 *p++ = '%';
8143 *p++ = '(';
8144 *p = '\0';
8145
8146 return string;
8147 }
8148
8149 /* Return the string to output a conditional branch to LABEL, which is
8150 the operand number of the label. OP is the conditional expression.
8151 XEXP (OP, 0) is assumed to be a condition code register (integer or
8152 floating point) and its mode specifies what kind of comparison we made.
8153
8154 DEST is the destination insn (i.e. the label), INSN is the source.
8155
8156 REVERSED is nonzero if we should reverse the sense of the comparison.
8157
8158 ANNUL is nonzero if we should generate an annulling branch. */
8159
8160 const char *
8161 output_cbranch (rtx op, rtx dest, int label, int reversed, int annul,
8162 rtx_insn *insn)
8163 {
8164 static char string[64];
8165 enum rtx_code code = GET_CODE (op);
8166 rtx cc_reg = XEXP (op, 0);
8167 machine_mode mode = GET_MODE (cc_reg);
8168 const char *labelno, *branch;
8169 int spaces = 8, far;
8170 char *p;
8171
8172 /* v9 branches are limited to +-1MB. If it is too far away,
8173 change
8174
8175 bne,pt %xcc, .LC30
8176
8177 to
8178
8179 be,pn %xcc, .+12
8180 nop
8181 ba .LC30
8182
8183 and
8184
8185 fbne,a,pn %fcc2, .LC29
8186
8187 to
8188
8189 fbe,pt %fcc2, .+16
8190 nop
8191 ba .LC29 */
8192
8193 far = TARGET_V9 && (get_attr_length (insn) >= 3);
8194 if (reversed ^ far)
8195 {
8196 /* Reversal of FP compares takes care -- an ordered compare
8197 becomes an unordered compare and vice versa. */
8198 if (mode == CCFPmode || mode == CCFPEmode)
8199 code = reverse_condition_maybe_unordered (code);
8200 else
8201 code = reverse_condition (code);
8202 }
8203
8204 /* Start by writing the branch condition. */
8205 if (mode == CCFPmode || mode == CCFPEmode)
8206 {
8207 switch (code)
8208 {
8209 case NE:
8210 branch = "fbne";
8211 break;
8212 case EQ:
8213 branch = "fbe";
8214 break;
8215 case GE:
8216 branch = "fbge";
8217 break;
8218 case GT:
8219 branch = "fbg";
8220 break;
8221 case LE:
8222 branch = "fble";
8223 break;
8224 case LT:
8225 branch = "fbl";
8226 break;
8227 case UNORDERED:
8228 branch = "fbu";
8229 break;
8230 case ORDERED:
8231 branch = "fbo";
8232 break;
8233 case UNGT:
8234 branch = "fbug";
8235 break;
8236 case UNLT:
8237 branch = "fbul";
8238 break;
8239 case UNEQ:
8240 branch = "fbue";
8241 break;
8242 case UNGE:
8243 branch = "fbuge";
8244 break;
8245 case UNLE:
8246 branch = "fbule";
8247 break;
8248 case LTGT:
8249 branch = "fblg";
8250 break;
8251 default:
8252 gcc_unreachable ();
8253 }
8254
8255 /* ??? !v9: FP branches cannot be preceded by another floating point
8256 insn. Because there is currently no concept of pre-delay slots,
8257 we can fix this only by always emitting a nop before a floating
8258 point branch. */
8259
8260 string[0] = '\0';
8261 if (! TARGET_V9)
8262 strcpy (string, "nop\n\t");
8263 strcat (string, branch);
8264 }
8265 else
8266 {
8267 switch (code)
8268 {
8269 case NE:
8270 if (mode == CCVmode || mode == CCXVmode)
8271 branch = "bvs";
8272 else
8273 branch = "bne";
8274 break;
8275 case EQ:
8276 if (mode == CCVmode || mode == CCXVmode)
8277 branch = "bvc";
8278 else
8279 branch = "be";
8280 break;
8281 case GE:
8282 if (mode == CCNZmode || mode == CCXNZmode)
8283 branch = "bpos";
8284 else
8285 branch = "bge";
8286 break;
8287 case GT:
8288 branch = "bg";
8289 break;
8290 case LE:
8291 branch = "ble";
8292 break;
8293 case LT:
8294 if (mode == CCNZmode || mode == CCXNZmode)
8295 branch = "bneg";
8296 else
8297 branch = "bl";
8298 break;
8299 case GEU:
8300 branch = "bgeu";
8301 break;
8302 case GTU:
8303 branch = "bgu";
8304 break;
8305 case LEU:
8306 branch = "bleu";
8307 break;
8308 case LTU:
8309 branch = "blu";
8310 break;
8311 default:
8312 gcc_unreachable ();
8313 }
8314 strcpy (string, branch);
8315 }
8316 spaces -= strlen (branch);
8317 p = strchr (string, '\0');
8318
8319 /* Now add the annulling, the label, and a possible noop. */
8320 if (annul && ! far)
8321 {
8322 strcpy (p, ",a");
8323 p += 2;
8324 spaces -= 2;
8325 }
8326
8327 if (TARGET_V9)
8328 {
8329 rtx note;
8330 int v8 = 0;
8331
8332 if (! far && insn && INSN_ADDRESSES_SET_P ())
8333 {
8334 int delta = (INSN_ADDRESSES (INSN_UID (dest))
8335 - INSN_ADDRESSES (INSN_UID (insn)));
8336 /* Leave some instructions for "slop". */
8337 if (delta < -260000 || delta >= 260000)
8338 v8 = 1;
8339 }
8340
8341 switch (mode)
8342 {
8343 case E_CCmode:
8344 case E_CCNZmode:
8345 case E_CCCmode:
8346 case E_CCVmode:
8347 labelno = "%%icc, ";
8348 if (v8)
8349 labelno = "";
8350 break;
8351 case E_CCXmode:
8352 case E_CCXNZmode:
8353 case E_CCXCmode:
8354 case E_CCXVmode:
8355 labelno = "%%xcc, ";
8356 gcc_assert (!v8);
8357 break;
8358 case E_CCFPmode:
8359 case E_CCFPEmode:
8360 {
8361 static char v9_fcc_labelno[] = "%%fccX, ";
8362 /* Set the char indicating the number of the fcc reg to use. */
8363 v9_fcc_labelno[5] = REGNO (cc_reg) - SPARC_FIRST_V9_FCC_REG + '0';
8364 labelno = v9_fcc_labelno;
8365 if (v8)
8366 {
8367 gcc_assert (REGNO (cc_reg) == SPARC_FCC_REG);
8368 labelno = "";
8369 }
8370 }
8371 break;
8372 default:
8373 gcc_unreachable ();
8374 }
8375
8376 if (*labelno && insn && (note = find_reg_note (insn, REG_BR_PROB, NULL_RTX)))
8377 {
8378 strcpy (p,
8379 ((profile_probability::from_reg_br_prob_note (XINT (note, 0))
8380 >= profile_probability::even ()) ^ far)
8381 ? ",pt" : ",pn");
8382 p += 3;
8383 spaces -= 3;
8384 }
8385 }
8386 else
8387 labelno = "";
8388
8389 if (spaces > 0)
8390 *p++ = '\t';
8391 else
8392 *p++ = ' ';
8393 strcpy (p, labelno);
8394 p = strchr (p, '\0');
8395 if (far)
8396 {
8397 strcpy (p, ".+12\n\t nop\n\tb\t");
8398 /* Skip the next insn if requested or
8399 if we know that it will be a nop. */
8400 if (annul || ! final_sequence)
8401 p[3] = '6';
8402 p += 14;
8403 }
8404 *p++ = '%';
8405 *p++ = 'l';
8406 *p++ = label + '0';
8407 *p++ = '%';
8408 *p++ = '#';
8409 *p = '\0';
8410
8411 return string;
8412 }
8413
8414 /* Emit a library call comparison between floating point X and Y.
8415 COMPARISON is the operator to compare with (EQ, NE, GT, etc).
8416 Return the new operator to be used in the comparison sequence.
8417
8418 TARGET_ARCH64 uses _Qp_* functions, which use pointers to TFmode
8419 values as arguments instead of the TFmode registers themselves,
8420 that's why we cannot call emit_float_lib_cmp. */
8421
8422 rtx
8423 sparc_emit_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison)
8424 {
8425 const char *qpfunc;
8426 rtx slot0, slot1, result, tem, tem2, libfunc;
8427 machine_mode mode;
8428 enum rtx_code new_comparison;
8429
8430 switch (comparison)
8431 {
8432 case EQ:
8433 qpfunc = (TARGET_ARCH64 ? "_Qp_feq" : "_Q_feq");
8434 break;
8435
8436 case NE:
8437 qpfunc = (TARGET_ARCH64 ? "_Qp_fne" : "_Q_fne");
8438 break;
8439
8440 case GT:
8441 qpfunc = (TARGET_ARCH64 ? "_Qp_fgt" : "_Q_fgt");
8442 break;
8443
8444 case GE:
8445 qpfunc = (TARGET_ARCH64 ? "_Qp_fge" : "_Q_fge");
8446 break;
8447
8448 case LT:
8449 qpfunc = (TARGET_ARCH64 ? "_Qp_flt" : "_Q_flt");
8450 break;
8451
8452 case LE:
8453 qpfunc = (TARGET_ARCH64 ? "_Qp_fle" : "_Q_fle");
8454 break;
8455
8456 case ORDERED:
8457 case UNORDERED:
8458 case UNGT:
8459 case UNLT:
8460 case UNEQ:
8461 case UNGE:
8462 case UNLE:
8463 case LTGT:
8464 qpfunc = (TARGET_ARCH64 ? "_Qp_cmp" : "_Q_cmp");
8465 break;
8466
8467 default:
8468 gcc_unreachable ();
8469 }
8470
8471 if (TARGET_ARCH64)
8472 {
8473 if (MEM_P (x))
8474 {
8475 tree expr = MEM_EXPR (x);
8476 if (expr)
8477 mark_addressable (expr);
8478 slot0 = x;
8479 }
8480 else
8481 {
8482 slot0 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode));
8483 emit_move_insn (slot0, x);
8484 }
8485
8486 if (MEM_P (y))
8487 {
8488 tree expr = MEM_EXPR (y);
8489 if (expr)
8490 mark_addressable (expr);
8491 slot1 = y;
8492 }
8493 else
8494 {
8495 slot1 = assign_stack_temp (TFmode, GET_MODE_SIZE(TFmode));
8496 emit_move_insn (slot1, y);
8497 }
8498
8499 libfunc = gen_rtx_SYMBOL_REF (Pmode, qpfunc);
8500 emit_library_call (libfunc, LCT_NORMAL,
8501 DImode,
8502 XEXP (slot0, 0), Pmode,
8503 XEXP (slot1, 0), Pmode);
8504 mode = DImode;
8505 }
8506 else
8507 {
8508 libfunc = gen_rtx_SYMBOL_REF (Pmode, qpfunc);
8509 emit_library_call (libfunc, LCT_NORMAL,
8510 SImode,
8511 x, TFmode, y, TFmode);
8512 mode = SImode;
8513 }
8514
8515
8516 /* Immediately move the result of the libcall into a pseudo
8517 register so reload doesn't clobber the value if it needs
8518 the return register for a spill reg. */
8519 result = gen_reg_rtx (mode);
8520 emit_move_insn (result, hard_libcall_value (mode, libfunc));
8521
8522 switch (comparison)
8523 {
8524 default:
8525 return gen_rtx_NE (VOIDmode, result, const0_rtx);
8526 case ORDERED:
8527 case UNORDERED:
8528 new_comparison = (comparison == UNORDERED ? EQ : NE);
8529 return gen_rtx_fmt_ee (new_comparison, VOIDmode, result, GEN_INT(3));
8530 case UNGT:
8531 case UNGE:
8532 new_comparison = (comparison == UNGT ? GT : NE);
8533 return gen_rtx_fmt_ee (new_comparison, VOIDmode, result, const1_rtx);
8534 case UNLE:
8535 return gen_rtx_NE (VOIDmode, result, const2_rtx);
8536 case UNLT:
8537 tem = gen_reg_rtx (mode);
8538 if (TARGET_ARCH32)
8539 emit_insn (gen_andsi3 (tem, result, const1_rtx));
8540 else
8541 emit_insn (gen_anddi3 (tem, result, const1_rtx));
8542 return gen_rtx_NE (VOIDmode, tem, const0_rtx);
8543 case UNEQ:
8544 case LTGT:
8545 tem = gen_reg_rtx (mode);
8546 if (TARGET_ARCH32)
8547 emit_insn (gen_addsi3 (tem, result, const1_rtx));
8548 else
8549 emit_insn (gen_adddi3 (tem, result, const1_rtx));
8550 tem2 = gen_reg_rtx (mode);
8551 if (TARGET_ARCH32)
8552 emit_insn (gen_andsi3 (tem2, tem, const2_rtx));
8553 else
8554 emit_insn (gen_anddi3 (tem2, tem, const2_rtx));
8555 new_comparison = (comparison == UNEQ ? EQ : NE);
8556 return gen_rtx_fmt_ee (new_comparison, VOIDmode, tem2, const0_rtx);
8557 }
8558
8559 gcc_unreachable ();
8560 }
8561
8562 /* Generate an unsigned DImode to FP conversion. This is the same code
8563 optabs would emit if we didn't have TFmode patterns. */
8564
8565 void
8566 sparc_emit_floatunsdi (rtx *operands, machine_mode mode)
8567 {
8568 rtx i0, i1, f0, in, out;
8569
8570 out = operands[0];
8571 in = force_reg (DImode, operands[1]);
8572 rtx_code_label *neglab = gen_label_rtx ();
8573 rtx_code_label *donelab = gen_label_rtx ();
8574 i0 = gen_reg_rtx (DImode);
8575 i1 = gen_reg_rtx (DImode);
8576 f0 = gen_reg_rtx (mode);
8577
8578 emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, DImode, 0, neglab);
8579
8580 emit_insn (gen_rtx_SET (out, gen_rtx_FLOAT (mode, in)));
8581 emit_jump_insn (gen_jump (donelab));
8582 emit_barrier ();
8583
8584 emit_label (neglab);
8585
8586 emit_insn (gen_lshrdi3 (i0, in, const1_rtx));
8587 emit_insn (gen_anddi3 (i1, in, const1_rtx));
8588 emit_insn (gen_iordi3 (i0, i0, i1));
8589 emit_insn (gen_rtx_SET (f0, gen_rtx_FLOAT (mode, i0)));
8590 emit_insn (gen_rtx_SET (out, gen_rtx_PLUS (mode, f0, f0)));
8591
8592 emit_label (donelab);
8593 }
8594
8595 /* Generate an FP to unsigned DImode conversion. This is the same code
8596 optabs would emit if we didn't have TFmode patterns. */
8597
8598 void
8599 sparc_emit_fixunsdi (rtx *operands, machine_mode mode)
8600 {
8601 rtx i0, i1, f0, in, out, limit;
8602
8603 out = operands[0];
8604 in = force_reg (mode, operands[1]);
8605 rtx_code_label *neglab = gen_label_rtx ();
8606 rtx_code_label *donelab = gen_label_rtx ();
8607 i0 = gen_reg_rtx (DImode);
8608 i1 = gen_reg_rtx (DImode);
8609 limit = gen_reg_rtx (mode);
8610 f0 = gen_reg_rtx (mode);
8611
8612 emit_move_insn (limit,
8613 const_double_from_real_value (
8614 REAL_VALUE_ATOF ("9223372036854775808.0", mode), mode));
8615 emit_cmp_and_jump_insns (in, limit, GE, NULL_RTX, mode, 0, neglab);
8616
8617 emit_insn (gen_rtx_SET (out,
8618 gen_rtx_FIX (DImode, gen_rtx_FIX (mode, in))));
8619 emit_jump_insn (gen_jump (donelab));
8620 emit_barrier ();
8621
8622 emit_label (neglab);
8623
8624 emit_insn (gen_rtx_SET (f0, gen_rtx_MINUS (mode, in, limit)));
8625 emit_insn (gen_rtx_SET (i0,
8626 gen_rtx_FIX (DImode, gen_rtx_FIX (mode, f0))));
8627 emit_insn (gen_movdi (i1, const1_rtx));
8628 emit_insn (gen_ashldi3 (i1, i1, GEN_INT (63)));
8629 emit_insn (gen_xordi3 (out, i0, i1));
8630
8631 emit_label (donelab);
8632 }
8633
8634 /* Return the string to output a compare and branch instruction to DEST.
8635 DEST is the destination insn (i.e. the label), INSN is the source,
8636 and OP is the conditional expression. */
8637
8638 const char *
8639 output_cbcond (rtx op, rtx dest, rtx_insn *insn)
8640 {
8641 machine_mode mode = GET_MODE (XEXP (op, 0));
8642 enum rtx_code code = GET_CODE (op);
8643 const char *cond_str, *tmpl;
8644 int far, emit_nop, len;
8645 static char string[64];
8646 char size_char;
8647
8648 /* Compare and Branch is limited to +-2KB. If it is too far away,
8649 change
8650
8651 cxbne X, Y, .LC30
8652
8653 to
8654
8655 cxbe X, Y, .+16
8656 nop
8657 ba,pt xcc, .LC30
8658 nop */
8659
8660 len = get_attr_length (insn);
8661
8662 far = len == 4;
8663 emit_nop = len == 2;
8664
8665 if (far)
8666 code = reverse_condition (code);
8667
8668 size_char = ((mode == SImode) ? 'w' : 'x');
8669
8670 switch (code)
8671 {
8672 case NE:
8673 cond_str = "ne";
8674 break;
8675
8676 case EQ:
8677 cond_str = "e";
8678 break;
8679
8680 case GE:
8681 cond_str = "ge";
8682 break;
8683
8684 case GT:
8685 cond_str = "g";
8686 break;
8687
8688 case LE:
8689 cond_str = "le";
8690 break;
8691
8692 case LT:
8693 cond_str = "l";
8694 break;
8695
8696 case GEU:
8697 cond_str = "cc";
8698 break;
8699
8700 case GTU:
8701 cond_str = "gu";
8702 break;
8703
8704 case LEU:
8705 cond_str = "leu";
8706 break;
8707
8708 case LTU:
8709 cond_str = "cs";
8710 break;
8711
8712 default:
8713 gcc_unreachable ();
8714 }
8715
8716 if (far)
8717 {
8718 int veryfar = 1, delta;
8719
8720 if (INSN_ADDRESSES_SET_P ())
8721 {
8722 delta = (INSN_ADDRESSES (INSN_UID (dest))
8723 - INSN_ADDRESSES (INSN_UID (insn)));
8724 /* Leave some instructions for "slop". */
8725 if (delta >= -260000 && delta < 260000)
8726 veryfar = 0;
8727 }
8728
8729 if (veryfar)
8730 tmpl = "c%cb%s\t%%1, %%2, .+16\n\tnop\n\tb\t%%3\n\tnop";
8731 else
8732 tmpl = "c%cb%s\t%%1, %%2, .+16\n\tnop\n\tba,pt\t%%%%xcc, %%3\n\tnop";
8733 }
8734 else
8735 {
8736 if (emit_nop)
8737 tmpl = "c%cb%s\t%%1, %%2, %%3\n\tnop";
8738 else
8739 tmpl = "c%cb%s\t%%1, %%2, %%3";
8740 }
8741
8742 snprintf (string, sizeof(string), tmpl, size_char, cond_str);
8743
8744 return string;
8745 }
8746
8747 /* Return the string to output a conditional branch to LABEL, testing
8748 register REG. LABEL is the operand number of the label; REG is the
8749 operand number of the reg. OP is the conditional expression. The mode
8750 of REG says what kind of comparison we made.
8751
8752 DEST is the destination insn (i.e. the label), INSN is the source.
8753
8754 REVERSED is nonzero if we should reverse the sense of the comparison.
8755
8756 ANNUL is nonzero if we should generate an annulling branch. */
8757
8758 const char *
8759 output_v9branch (rtx op, rtx dest, int reg, int label, int reversed,
8760 int annul, rtx_insn *insn)
8761 {
8762 static char string[64];
8763 enum rtx_code code = GET_CODE (op);
8764 machine_mode mode = GET_MODE (XEXP (op, 0));
8765 rtx note;
8766 int far;
8767 char *p;
8768
8769 /* branch on register are limited to +-128KB. If it is too far away,
8770 change
8771
8772 brnz,pt %g1, .LC30
8773
8774 to
8775
8776 brz,pn %g1, .+12
8777 nop
8778 ba,pt %xcc, .LC30
8779
8780 and
8781
8782 brgez,a,pn %o1, .LC29
8783
8784 to
8785
8786 brlz,pt %o1, .+16
8787 nop
8788 ba,pt %xcc, .LC29 */
8789
8790 far = get_attr_length (insn) >= 3;
8791
8792 /* If not floating-point or if EQ or NE, we can just reverse the code. */
8793 if (reversed ^ far)
8794 code = reverse_condition (code);
8795
8796 /* Only 64-bit versions of these instructions exist. */
8797 gcc_assert (mode == DImode);
8798
8799 /* Start by writing the branch condition. */
8800
8801 switch (code)
8802 {
8803 case NE:
8804 strcpy (string, "brnz");
8805 break;
8806
8807 case EQ:
8808 strcpy (string, "brz");
8809 break;
8810
8811 case GE:
8812 strcpy (string, "brgez");
8813 break;
8814
8815 case LT:
8816 strcpy (string, "brlz");
8817 break;
8818
8819 case LE:
8820 strcpy (string, "brlez");
8821 break;
8822
8823 case GT:
8824 strcpy (string, "brgz");
8825 break;
8826
8827 default:
8828 gcc_unreachable ();
8829 }
8830
8831 p = strchr (string, '\0');
8832
8833 /* Now add the annulling, reg, label, and nop. */
8834 if (annul && ! far)
8835 {
8836 strcpy (p, ",a");
8837 p += 2;
8838 }
8839
8840 if (insn && (note = find_reg_note (insn, REG_BR_PROB, NULL_RTX)))
8841 {
8842 strcpy (p,
8843 ((profile_probability::from_reg_br_prob_note (XINT (note, 0))
8844 >= profile_probability::even ()) ^ far)
8845 ? ",pt" : ",pn");
8846 p += 3;
8847 }
8848
8849 *p = p < string + 8 ? '\t' : ' ';
8850 p++;
8851 *p++ = '%';
8852 *p++ = '0' + reg;
8853 *p++ = ',';
8854 *p++ = ' ';
8855 if (far)
8856 {
8857 int veryfar = 1, delta;
8858
8859 if (INSN_ADDRESSES_SET_P ())
8860 {
8861 delta = (INSN_ADDRESSES (INSN_UID (dest))
8862 - INSN_ADDRESSES (INSN_UID (insn)));
8863 /* Leave some instructions for "slop". */
8864 if (delta >= -260000 && delta < 260000)
8865 veryfar = 0;
8866 }
8867
8868 strcpy (p, ".+12\n\t nop\n\t");
8869 /* Skip the next insn if requested or
8870 if we know that it will be a nop. */
8871 if (annul || ! final_sequence)
8872 p[3] = '6';
8873 p += 12;
8874 if (veryfar)
8875 {
8876 strcpy (p, "b\t");
8877 p += 2;
8878 }
8879 else
8880 {
8881 strcpy (p, "ba,pt\t%%xcc, ");
8882 p += 13;
8883 }
8884 }
8885 *p++ = '%';
8886 *p++ = 'l';
8887 *p++ = '0' + label;
8888 *p++ = '%';
8889 *p++ = '#';
8890 *p = '\0';
8891
8892 return string;
8893 }
8894
8895 /* Return 1, if any of the registers of the instruction are %l[0-7] or %o[0-7].
8896 Such instructions cannot be used in the delay slot of return insn on v9.
8897 If TEST is 0, also rename all %i[0-7] registers to their %o[0-7] counterparts.
8898 */
8899
8900 static int
8901 epilogue_renumber (rtx *where, int test)
8902 {
8903 const char *fmt;
8904 int i;
8905 enum rtx_code code;
8906
8907 if (*where == 0)
8908 return 0;
8909
8910 code = GET_CODE (*where);
8911
8912 switch (code)
8913 {
8914 case REG:
8915 if (REGNO (*where) >= 8 && REGNO (*where) < 24) /* oX or lX */
8916 return 1;
8917 if (! test && REGNO (*where) >= 24 && REGNO (*where) < 32)
8918 {
8919 if (ORIGINAL_REGNO (*where))
8920 {
8921 rtx n = gen_raw_REG (GET_MODE (*where),
8922 OUTGOING_REGNO (REGNO (*where)));
8923 ORIGINAL_REGNO (n) = ORIGINAL_REGNO (*where);
8924 *where = n;
8925 }
8926 else
8927 *where = gen_rtx_REG (GET_MODE (*where),
8928 OUTGOING_REGNO (REGNO (*where)));
8929 }
8930 return 0;
8931
8932 case SCRATCH:
8933 case PC:
8934 case CONST_INT:
8935 case CONST_WIDE_INT:
8936 case CONST_DOUBLE:
8937 return 0;
8938
8939 /* Do not replace the frame pointer with the stack pointer because
8940 it can cause the delayed instruction to load below the stack.
8941 This occurs when instructions like:
8942
8943 (set (reg/i:SI 24 %i0)
8944 (mem/f:SI (plus:SI (reg/f:SI 30 %fp)
8945 (const_int -20 [0xffffffec])) 0))
8946
8947 are in the return delayed slot. */
8948 case PLUS:
8949 if (GET_CODE (XEXP (*where, 0)) == REG
8950 && REGNO (XEXP (*where, 0)) == HARD_FRAME_POINTER_REGNUM
8951 && (GET_CODE (XEXP (*where, 1)) != CONST_INT
8952 || INTVAL (XEXP (*where, 1)) < SPARC_STACK_BIAS))
8953 return 1;
8954 break;
8955
8956 case MEM:
8957 if (SPARC_STACK_BIAS
8958 && GET_CODE (XEXP (*where, 0)) == REG
8959 && REGNO (XEXP (*where, 0)) == HARD_FRAME_POINTER_REGNUM)
8960 return 1;
8961 break;
8962
8963 default:
8964 break;
8965 }
8966
8967 fmt = GET_RTX_FORMAT (code);
8968
8969 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8970 {
8971 if (fmt[i] == 'E')
8972 {
8973 int j;
8974 for (j = XVECLEN (*where, i) - 1; j >= 0; j--)
8975 if (epilogue_renumber (&(XVECEXP (*where, i, j)), test))
8976 return 1;
8977 }
8978 else if (fmt[i] == 'e'
8979 && epilogue_renumber (&(XEXP (*where, i)), test))
8980 return 1;
8981 }
8982 return 0;
8983 }
8984
8985 /* Leaf functions and non-leaf functions have different needs. */
8986
8987 static const int reg_leaf_alloc_order[] = REG_LEAF_ALLOC_ORDER;
8988
8989 static const int reg_nonleaf_alloc_order[] = REG_ALLOC_ORDER;
8990
8991 static const int *const reg_alloc_orders[] =
8992 {
8993 reg_leaf_alloc_order,
8994 reg_nonleaf_alloc_order
8995 };
8996
8997 void
8998 sparc_order_regs_for_local_alloc (void)
8999 {
9000 static int last_order_nonleaf = 1;
9001
9002 if (df_regs_ever_live_p (15) != last_order_nonleaf)
9003 {
9004 last_order_nonleaf = !last_order_nonleaf;
9005 memcpy ((char *) reg_alloc_order,
9006 (const char *) reg_alloc_orders[last_order_nonleaf],
9007 FIRST_PSEUDO_REGISTER * sizeof (int));
9008 }
9009 }
9010
9011 int
9012 sparc_leaf_reg_remap (int regno)
9013 {
9014 gcc_checking_assert (regno >= 0);
9015
9016 /* Do not remap in flat mode. */
9017 if (TARGET_FLAT)
9018 return regno;
9019
9020 /* Do not remap global, stack pointer or floating-point registers. */
9021 if (regno < 8 || regno == STACK_POINTER_REGNUM || regno > SPARC_LAST_INT_REG)
9022 return regno;
9023
9024 /* Neither out nor local nor frame pointer registers must appear. */
9025 if ((regno >= 8 && regno <= 23) || regno == HARD_FRAME_POINTER_REGNUM)
9026 return -1;
9027
9028 /* Remap in to out registers. */
9029 return regno - 16;
9030 }
9031
9032 /* Return 1 if REG and MEM are legitimate enough to allow the various
9033 MEM<-->REG splits to be run. */
9034
9035 int
9036 sparc_split_reg_mem_legitimate (rtx reg, rtx mem)
9037 {
9038 /* Punt if we are here by mistake. */
9039 gcc_assert (reload_completed);
9040
9041 /* We must have an offsettable memory reference. */
9042 if (!offsettable_memref_p (mem))
9043 return 0;
9044
9045 /* If we have legitimate args for ldd/std, we do not want
9046 the split to happen. */
9047 if ((REGNO (reg) % 2) == 0 && mem_min_alignment (mem, 8))
9048 return 0;
9049
9050 /* Success. */
9051 return 1;
9052 }
9053
9054 /* Split a REG <-- MEM move into a pair of moves in MODE. */
9055
9056 void
9057 sparc_split_reg_mem (rtx dest, rtx src, machine_mode mode)
9058 {
9059 rtx high_part = gen_highpart (mode, dest);
9060 rtx low_part = gen_lowpart (mode, dest);
9061 rtx word0 = adjust_address (src, mode, 0);
9062 rtx word1 = adjust_address (src, mode, 4);
9063
9064 if (reg_overlap_mentioned_p (high_part, word1))
9065 {
9066 emit_move_insn_1 (low_part, word1);
9067 emit_move_insn_1 (high_part, word0);
9068 }
9069 else
9070 {
9071 emit_move_insn_1 (high_part, word0);
9072 emit_move_insn_1 (low_part, word1);
9073 }
9074 }
9075
9076 /* Split a MEM <-- REG move into a pair of moves in MODE. */
9077
9078 void
9079 sparc_split_mem_reg (rtx dest, rtx src, machine_mode mode)
9080 {
9081 rtx word0 = adjust_address (dest, mode, 0);
9082 rtx word1 = adjust_address (dest, mode, 4);
9083 rtx high_part = gen_highpart (mode, src);
9084 rtx low_part = gen_lowpart (mode, src);
9085
9086 emit_move_insn_1 (word0, high_part);
9087 emit_move_insn_1 (word1, low_part);
9088 }
9089
9090 /* Like sparc_split_reg_mem_legitimate but for REG <--> REG moves. */
9091
9092 int
9093 sparc_split_reg_reg_legitimate (rtx reg1, rtx reg2)
9094 {
9095 /* Punt if we are here by mistake. */
9096 gcc_assert (reload_completed);
9097
9098 if (GET_CODE (reg1) == SUBREG)
9099 reg1 = SUBREG_REG (reg1);
9100 if (GET_CODE (reg1) != REG)
9101 return 0;
9102 const int regno1 = REGNO (reg1);
9103
9104 if (GET_CODE (reg2) == SUBREG)
9105 reg2 = SUBREG_REG (reg2);
9106 if (GET_CODE (reg2) != REG)
9107 return 0;
9108 const int regno2 = REGNO (reg2);
9109
9110 if (SPARC_INT_REG_P (regno1) && SPARC_INT_REG_P (regno2))
9111 return 1;
9112
9113 if (TARGET_VIS3)
9114 {
9115 if ((SPARC_INT_REG_P (regno1) && SPARC_FP_REG_P (regno2))
9116 || (SPARC_FP_REG_P (regno1) && SPARC_INT_REG_P (regno2)))
9117 return 1;
9118 }
9119
9120 return 0;
9121 }
9122
9123 /* Split a REG <--> REG move into a pair of moves in MODE. */
9124
9125 void
9126 sparc_split_reg_reg (rtx dest, rtx src, machine_mode mode)
9127 {
9128 rtx dest1 = gen_highpart (mode, dest);
9129 rtx dest2 = gen_lowpart (mode, dest);
9130 rtx src1 = gen_highpart (mode, src);
9131 rtx src2 = gen_lowpart (mode, src);
9132
9133 /* Now emit using the real source and destination we found, swapping
9134 the order if we detect overlap. */
9135 if (reg_overlap_mentioned_p (dest1, src2))
9136 {
9137 emit_move_insn_1 (dest2, src2);
9138 emit_move_insn_1 (dest1, src1);
9139 }
9140 else
9141 {
9142 emit_move_insn_1 (dest1, src1);
9143 emit_move_insn_1 (dest2, src2);
9144 }
9145 }
9146
9147 /* Return 1 if REGNO (reg1) is even and REGNO (reg1) == REGNO (reg2) - 1.
9148 This makes them candidates for using ldd and std insns.
9149
9150 Note reg1 and reg2 *must* be hard registers. */
9151
9152 int
9153 registers_ok_for_ldd_peep (rtx reg1, rtx reg2)
9154 {
9155 /* We might have been passed a SUBREG. */
9156 if (GET_CODE (reg1) != REG || GET_CODE (reg2) != REG)
9157 return 0;
9158
9159 if (REGNO (reg1) % 2 != 0)
9160 return 0;
9161
9162 /* Integer ldd is deprecated in SPARC V9 */
9163 if (TARGET_V9 && SPARC_INT_REG_P (REGNO (reg1)))
9164 return 0;
9165
9166 return (REGNO (reg1) == REGNO (reg2) - 1);
9167 }
9168
9169 /* Return 1 if the addresses in mem1 and mem2 are suitable for use in
9170 an ldd or std insn.
9171
9172 This can only happen when addr1 and addr2, the addresses in mem1
9173 and mem2, are consecutive memory locations (addr1 + 4 == addr2).
9174 addr1 must also be aligned on a 64-bit boundary.
9175
9176 Also iff dependent_reg_rtx is not null it should not be used to
9177 compute the address for mem1, i.e. we cannot optimize a sequence
9178 like:
9179 ld [%o0], %o0
9180 ld [%o0 + 4], %o1
9181 to
9182 ldd [%o0], %o0
9183 nor:
9184 ld [%g3 + 4], %g3
9185 ld [%g3], %g2
9186 to
9187 ldd [%g3], %g2
9188
9189 But, note that the transformation from:
9190 ld [%g2 + 4], %g3
9191 ld [%g2], %g2
9192 to
9193 ldd [%g2], %g2
9194 is perfectly fine. Thus, the peephole2 patterns always pass us
9195 the destination register of the first load, never the second one.
9196
9197 For stores we don't have a similar problem, so dependent_reg_rtx is
9198 NULL_RTX. */
9199
9200 int
9201 mems_ok_for_ldd_peep (rtx mem1, rtx mem2, rtx dependent_reg_rtx)
9202 {
9203 rtx addr1, addr2;
9204 unsigned int reg1;
9205 HOST_WIDE_INT offset1;
9206
9207 /* The mems cannot be volatile. */
9208 if (MEM_VOLATILE_P (mem1) || MEM_VOLATILE_P (mem2))
9209 return 0;
9210
9211 /* MEM1 should be aligned on a 64-bit boundary. */
9212 if (MEM_ALIGN (mem1) < 64)
9213 return 0;
9214
9215 addr1 = XEXP (mem1, 0);
9216 addr2 = XEXP (mem2, 0);
9217
9218 /* Extract a register number and offset (if used) from the first addr. */
9219 if (GET_CODE (addr1) == PLUS)
9220 {
9221 /* If not a REG, return zero. */
9222 if (GET_CODE (XEXP (addr1, 0)) != REG)
9223 return 0;
9224 else
9225 {
9226 reg1 = REGNO (XEXP (addr1, 0));
9227 /* The offset must be constant! */
9228 if (GET_CODE (XEXP (addr1, 1)) != CONST_INT)
9229 return 0;
9230 offset1 = INTVAL (XEXP (addr1, 1));
9231 }
9232 }
9233 else if (GET_CODE (addr1) != REG)
9234 return 0;
9235 else
9236 {
9237 reg1 = REGNO (addr1);
9238 /* This was a simple (mem (reg)) expression. Offset is 0. */
9239 offset1 = 0;
9240 }
9241
9242 /* Make sure the second address is a (mem (plus (reg) (const_int). */
9243 if (GET_CODE (addr2) != PLUS)
9244 return 0;
9245
9246 if (GET_CODE (XEXP (addr2, 0)) != REG
9247 || GET_CODE (XEXP (addr2, 1)) != CONST_INT)
9248 return 0;
9249
9250 if (reg1 != REGNO (XEXP (addr2, 0)))
9251 return 0;
9252
9253 if (dependent_reg_rtx != NULL_RTX && reg1 == REGNO (dependent_reg_rtx))
9254 return 0;
9255
9256 /* The first offset must be evenly divisible by 8 to ensure the
9257 address is 64-bit aligned. */
9258 if (offset1 % 8 != 0)
9259 return 0;
9260
9261 /* The offset for the second addr must be 4 more than the first addr. */
9262 if (INTVAL (XEXP (addr2, 1)) != offset1 + 4)
9263 return 0;
9264
9265 /* All the tests passed. addr1 and addr2 are valid for ldd and std
9266 instructions. */
9267 return 1;
9268 }
9269
9270 /* Return the widened memory access made of MEM1 and MEM2 in MODE. */
9271
9272 rtx
9273 widen_mem_for_ldd_peep (rtx mem1, rtx mem2, machine_mode mode)
9274 {
9275 rtx x = widen_memory_access (mem1, mode, 0);
9276 MEM_NOTRAP_P (x) = MEM_NOTRAP_P (mem1) && MEM_NOTRAP_P (mem2);
9277 return x;
9278 }
9279
9280 /* Return 1 if reg is a pseudo, or is the first register in
9281 a hard register pair. This makes it suitable for use in
9282 ldd and std insns. */
9283
9284 int
9285 register_ok_for_ldd (rtx reg)
9286 {
9287 /* We might have been passed a SUBREG. */
9288 if (!REG_P (reg))
9289 return 0;
9290
9291 if (REGNO (reg) < FIRST_PSEUDO_REGISTER)
9292 return (REGNO (reg) % 2 == 0);
9293
9294 return 1;
9295 }
9296
9297 /* Return 1 if OP, a MEM, has an address which is known to be
9298 aligned to an 8-byte boundary. */
9299
9300 int
9301 memory_ok_for_ldd (rtx op)
9302 {
9303 if (!mem_min_alignment (op, 8))
9304 return 0;
9305
9306 /* We need to perform the job of a memory constraint. */
9307 if ((reload_in_progress || reload_completed)
9308 && !strict_memory_address_p (Pmode, XEXP (op, 0)))
9309 return 0;
9310
9311 if (lra_in_progress && !memory_address_p (Pmode, XEXP (op, 0)))
9312 return 0;
9313
9314 return 1;
9315 }
9316
9317 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
9318
9319 static bool
9320 sparc_print_operand_punct_valid_p (unsigned char code)
9321 {
9322 if (code == '#'
9323 || code == '*'
9324 || code == '('
9325 || code == ')'
9326 || code == '_'
9327 || code == '&')
9328 return true;
9329
9330 return false;
9331 }
9332
9333 /* Implement TARGET_PRINT_OPERAND.
9334 Print operand X (an rtx) in assembler syntax to file FILE.
9335 CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
9336 For `%' followed by punctuation, CODE is the punctuation and X is null. */
9337
9338 static void
9339 sparc_print_operand (FILE *file, rtx x, int code)
9340 {
9341 const char *s;
9342
9343 switch (code)
9344 {
9345 case '#':
9346 /* Output an insn in a delay slot. */
9347 if (final_sequence)
9348 sparc_indent_opcode = 1;
9349 else
9350 fputs ("\n\t nop", file);
9351 return;
9352 case '*':
9353 /* Output an annul flag if there's nothing for the delay slot and we
9354 are optimizing. This is always used with '(' below.
9355 Sun OS 4.1.1 dbx can't handle an annulled unconditional branch;
9356 this is a dbx bug. So, we only do this when optimizing.
9357 On UltraSPARC, a branch in a delay slot causes a pipeline flush.
9358 Always emit a nop in case the next instruction is a branch. */
9359 if (! final_sequence && (optimize && (int)sparc_cpu < PROCESSOR_V9))
9360 fputs (",a", file);
9361 return;
9362 case '(':
9363 /* Output a 'nop' if there's nothing for the delay slot and we are
9364 not optimizing. This is always used with '*' above. */
9365 if (! final_sequence && ! (optimize && (int)sparc_cpu < PROCESSOR_V9))
9366 fputs ("\n\t nop", file);
9367 else if (final_sequence)
9368 sparc_indent_opcode = 1;
9369 return;
9370 case ')':
9371 /* Output the right displacement from the saved PC on function return.
9372 The caller may have placed an "unimp" insn immediately after the call
9373 so we have to account for it. This insn is used in the 32-bit ABI
9374 when calling a function that returns a non zero-sized structure. The
9375 64-bit ABI doesn't have it. Be careful to have this test be the same
9376 as that for the call. The exception is when sparc_std_struct_return
9377 is enabled, the psABI is followed exactly and the adjustment is made
9378 by the code in sparc_struct_value_rtx. The call emitted is the same
9379 when sparc_std_struct_return is enabled. */
9380 if (!TARGET_ARCH64
9381 && cfun->returns_struct
9382 && !sparc_std_struct_return
9383 && DECL_SIZE (DECL_RESULT (current_function_decl))
9384 && TREE_CODE (DECL_SIZE (DECL_RESULT (current_function_decl)))
9385 == INTEGER_CST
9386 && !integer_zerop (DECL_SIZE (DECL_RESULT (current_function_decl))))
9387 fputs ("12", file);
9388 else
9389 fputc ('8', file);
9390 return;
9391 case '_':
9392 /* Output the Embedded Medium/Anywhere code model base register. */
9393 fputs (EMBMEDANY_BASE_REG, file);
9394 return;
9395 case '&':
9396 /* Print some local dynamic TLS name. */
9397 if (const char *name = get_some_local_dynamic_name ())
9398 assemble_name (file, name);
9399 else
9400 output_operand_lossage ("'%%&' used without any "
9401 "local dynamic TLS references");
9402 return;
9403
9404 case 'Y':
9405 /* Adjust the operand to take into account a RESTORE operation. */
9406 if (GET_CODE (x) == CONST_INT)
9407 break;
9408 else if (GET_CODE (x) != REG)
9409 output_operand_lossage ("invalid %%Y operand");
9410 else if (REGNO (x) < 8)
9411 fputs (reg_names[REGNO (x)], file);
9412 else if (REGNO (x) >= 24 && REGNO (x) < 32)
9413 fputs (reg_names[REGNO (x)-16], file);
9414 else
9415 output_operand_lossage ("invalid %%Y operand");
9416 return;
9417 case 'L':
9418 /* Print out the low order register name of a register pair. */
9419 if (WORDS_BIG_ENDIAN)
9420 fputs (reg_names[REGNO (x)+1], file);
9421 else
9422 fputs (reg_names[REGNO (x)], file);
9423 return;
9424 case 'H':
9425 /* Print out the high order register name of a register pair. */
9426 if (WORDS_BIG_ENDIAN)
9427 fputs (reg_names[REGNO (x)], file);
9428 else
9429 fputs (reg_names[REGNO (x)+1], file);
9430 return;
9431 case 'R':
9432 /* Print out the second register name of a register pair or quad.
9433 I.e., R (%o0) => %o1. */
9434 fputs (reg_names[REGNO (x)+1], file);
9435 return;
9436 case 'S':
9437 /* Print out the third register name of a register quad.
9438 I.e., S (%o0) => %o2. */
9439 fputs (reg_names[REGNO (x)+2], file);
9440 return;
9441 case 'T':
9442 /* Print out the fourth register name of a register quad.
9443 I.e., T (%o0) => %o3. */
9444 fputs (reg_names[REGNO (x)+3], file);
9445 return;
9446 case 'x':
9447 /* Print a condition code register. */
9448 if (REGNO (x) == SPARC_ICC_REG)
9449 {
9450 switch (GET_MODE (x))
9451 {
9452 case E_CCmode:
9453 case E_CCNZmode:
9454 case E_CCCmode:
9455 case E_CCVmode:
9456 s = "%icc";
9457 break;
9458 case E_CCXmode:
9459 case E_CCXNZmode:
9460 case E_CCXCmode:
9461 case E_CCXVmode:
9462 s = "%xcc";
9463 break;
9464 default:
9465 gcc_unreachable ();
9466 }
9467 fputs (s, file);
9468 }
9469 else
9470 /* %fccN register */
9471 fputs (reg_names[REGNO (x)], file);
9472 return;
9473 case 'm':
9474 /* Print the operand's address only. */
9475 output_address (GET_MODE (x), XEXP (x, 0));
9476 return;
9477 case 'r':
9478 /* In this case we need a register. Use %g0 if the
9479 operand is const0_rtx. */
9480 if (x == const0_rtx
9481 || (GET_MODE (x) != VOIDmode && x == CONST0_RTX (GET_MODE (x))))
9482 {
9483 fputs ("%g0", file);
9484 return;
9485 }
9486 else
9487 break;
9488
9489 case 'A':
9490 switch (GET_CODE (x))
9491 {
9492 case IOR:
9493 s = "or";
9494 break;
9495 case AND:
9496 s = "and";
9497 break;
9498 case XOR:
9499 s = "xor";
9500 break;
9501 default:
9502 output_operand_lossage ("invalid %%A operand");
9503 s = "";
9504 break;
9505 }
9506 fputs (s, file);
9507 return;
9508
9509 case 'B':
9510 switch (GET_CODE (x))
9511 {
9512 case IOR:
9513 s = "orn";
9514 break;
9515 case AND:
9516 s = "andn";
9517 break;
9518 case XOR:
9519 s = "xnor";
9520 break;
9521 default:
9522 output_operand_lossage ("invalid %%B operand");
9523 s = "";
9524 break;
9525 }
9526 fputs (s, file);
9527 return;
9528
9529 /* This is used by the conditional move instructions. */
9530 case 'C':
9531 {
9532 machine_mode mode = GET_MODE (XEXP (x, 0));
9533 switch (GET_CODE (x))
9534 {
9535 case NE:
9536 if (mode == CCVmode || mode == CCXVmode)
9537 s = "vs";
9538 else
9539 s = "ne";
9540 break;
9541 case EQ:
9542 if (mode == CCVmode || mode == CCXVmode)
9543 s = "vc";
9544 else
9545 s = "e";
9546 break;
9547 case GE:
9548 if (mode == CCNZmode || mode == CCXNZmode)
9549 s = "pos";
9550 else
9551 s = "ge";
9552 break;
9553 case GT:
9554 s = "g";
9555 break;
9556 case LE:
9557 s = "le";
9558 break;
9559 case LT:
9560 if (mode == CCNZmode || mode == CCXNZmode)
9561 s = "neg";
9562 else
9563 s = "l";
9564 break;
9565 case GEU:
9566 s = "geu";
9567 break;
9568 case GTU:
9569 s = "gu";
9570 break;
9571 case LEU:
9572 s = "leu";
9573 break;
9574 case LTU:
9575 s = "lu";
9576 break;
9577 case LTGT:
9578 s = "lg";
9579 break;
9580 case UNORDERED:
9581 s = "u";
9582 break;
9583 case ORDERED:
9584 s = "o";
9585 break;
9586 case UNLT:
9587 s = "ul";
9588 break;
9589 case UNLE:
9590 s = "ule";
9591 break;
9592 case UNGT:
9593 s = "ug";
9594 break;
9595 case UNGE:
9596 s = "uge"
9597 ; break;
9598 case UNEQ:
9599 s = "ue";
9600 break;
9601 default:
9602 output_operand_lossage ("invalid %%C operand");
9603 s = "";
9604 break;
9605 }
9606 fputs (s, file);
9607 return;
9608 }
9609
9610 /* This are used by the movr instruction pattern. */
9611 case 'D':
9612 {
9613 switch (GET_CODE (x))
9614 {
9615 case NE:
9616 s = "ne";
9617 break;
9618 case EQ:
9619 s = "e";
9620 break;
9621 case GE:
9622 s = "gez";
9623 break;
9624 case LT:
9625 s = "lz";
9626 break;
9627 case LE:
9628 s = "lez";
9629 break;
9630 case GT:
9631 s = "gz";
9632 break;
9633 default:
9634 output_operand_lossage ("invalid %%D operand");
9635 s = "";
9636 break;
9637 }
9638 fputs (s, file);
9639 return;
9640 }
9641
9642 case 'b':
9643 {
9644 /* Print a sign-extended character. */
9645 int i = trunc_int_for_mode (INTVAL (x), QImode);
9646 fprintf (file, "%d", i);
9647 return;
9648 }
9649
9650 case 'f':
9651 /* Operand must be a MEM; write its address. */
9652 if (GET_CODE (x) != MEM)
9653 output_operand_lossage ("invalid %%f operand");
9654 output_address (GET_MODE (x), XEXP (x, 0));
9655 return;
9656
9657 case 's':
9658 {
9659 /* Print a sign-extended 32-bit value. */
9660 HOST_WIDE_INT i;
9661 if (GET_CODE(x) == CONST_INT)
9662 i = INTVAL (x);
9663 else
9664 {
9665 output_operand_lossage ("invalid %%s operand");
9666 return;
9667 }
9668 i = trunc_int_for_mode (i, SImode);
9669 fprintf (file, HOST_WIDE_INT_PRINT_DEC, i);
9670 return;
9671 }
9672
9673 case 0:
9674 /* Do nothing special. */
9675 break;
9676
9677 default:
9678 /* Undocumented flag. */
9679 output_operand_lossage ("invalid operand output code");
9680 }
9681
9682 if (GET_CODE (x) == REG)
9683 fputs (reg_names[REGNO (x)], file);
9684 else if (GET_CODE (x) == MEM)
9685 {
9686 fputc ('[', file);
9687 /* Poor Sun assembler doesn't understand absolute addressing. */
9688 if (CONSTANT_P (XEXP (x, 0)))
9689 fputs ("%g0+", file);
9690 output_address (GET_MODE (x), XEXP (x, 0));
9691 fputc (']', file);
9692 }
9693 else if (GET_CODE (x) == HIGH)
9694 {
9695 fputs ("%hi(", file);
9696 output_addr_const (file, XEXP (x, 0));
9697 fputc (')', file);
9698 }
9699 else if (GET_CODE (x) == LO_SUM)
9700 {
9701 sparc_print_operand (file, XEXP (x, 0), 0);
9702 if (TARGET_CM_MEDMID)
9703 fputs ("+%l44(", file);
9704 else
9705 fputs ("+%lo(", file);
9706 output_addr_const (file, XEXP (x, 1));
9707 fputc (')', file);
9708 }
9709 else if (GET_CODE (x) == CONST_DOUBLE)
9710 output_operand_lossage ("floating-point constant not a valid immediate operand");
9711 else
9712 output_addr_const (file, x);
9713 }
9714
9715 /* Implement TARGET_PRINT_OPERAND_ADDRESS. */
9716
9717 static void
9718 sparc_print_operand_address (FILE *file, machine_mode /*mode*/, rtx x)
9719 {
9720 rtx base, index = 0;
9721 int offset = 0;
9722 rtx addr = x;
9723
9724 if (REG_P (addr))
9725 fputs (reg_names[REGNO (addr)], file);
9726 else if (GET_CODE (addr) == PLUS)
9727 {
9728 if (CONST_INT_P (XEXP (addr, 0)))
9729 offset = INTVAL (XEXP (addr, 0)), base = XEXP (addr, 1);
9730 else if (CONST_INT_P (XEXP (addr, 1)))
9731 offset = INTVAL (XEXP (addr, 1)), base = XEXP (addr, 0);
9732 else
9733 base = XEXP (addr, 0), index = XEXP (addr, 1);
9734 if (GET_CODE (base) == LO_SUM)
9735 {
9736 gcc_assert (USE_AS_OFFSETABLE_LO10
9737 && TARGET_ARCH64
9738 && ! TARGET_CM_MEDMID);
9739 output_operand (XEXP (base, 0), 0);
9740 fputs ("+%lo(", file);
9741 output_address (VOIDmode, XEXP (base, 1));
9742 fprintf (file, ")+%d", offset);
9743 }
9744 else
9745 {
9746 fputs (reg_names[REGNO (base)], file);
9747 if (index == 0)
9748 fprintf (file, "%+d", offset);
9749 else if (REG_P (index))
9750 fprintf (file, "+%s", reg_names[REGNO (index)]);
9751 else if (GET_CODE (index) == SYMBOL_REF
9752 || GET_CODE (index) == LABEL_REF
9753 || GET_CODE (index) == CONST)
9754 fputc ('+', file), output_addr_const (file, index);
9755 else gcc_unreachable ();
9756 }
9757 }
9758 else if (GET_CODE (addr) == MINUS
9759 && GET_CODE (XEXP (addr, 1)) == LABEL_REF)
9760 {
9761 output_addr_const (file, XEXP (addr, 0));
9762 fputs ("-(", file);
9763 output_addr_const (file, XEXP (addr, 1));
9764 fputs ("-.)", file);
9765 }
9766 else if (GET_CODE (addr) == LO_SUM)
9767 {
9768 output_operand (XEXP (addr, 0), 0);
9769 if (TARGET_CM_MEDMID)
9770 fputs ("+%l44(", file);
9771 else
9772 fputs ("+%lo(", file);
9773 output_address (VOIDmode, XEXP (addr, 1));
9774 fputc (')', file);
9775 }
9776 else if (flag_pic
9777 && GET_CODE (addr) == CONST
9778 && GET_CODE (XEXP (addr, 0)) == MINUS
9779 && GET_CODE (XEXP (XEXP (addr, 0), 1)) == CONST
9780 && GET_CODE (XEXP (XEXP (XEXP (addr, 0), 1), 0)) == MINUS
9781 && XEXP (XEXP (XEXP (XEXP (addr, 0), 1), 0), 1) == pc_rtx)
9782 {
9783 addr = XEXP (addr, 0);
9784 output_addr_const (file, XEXP (addr, 0));
9785 /* Group the args of the second CONST in parenthesis. */
9786 fputs ("-(", file);
9787 /* Skip past the second CONST--it does nothing for us. */
9788 output_addr_const (file, XEXP (XEXP (addr, 1), 0));
9789 /* Close the parenthesis. */
9790 fputc (')', file);
9791 }
9792 else
9793 {
9794 output_addr_const (file, addr);
9795 }
9796 }
9797
9798 /* Target hook for assembling integer objects. The sparc version has
9799 special handling for aligned DI-mode objects. */
9800
9801 static bool
9802 sparc_assemble_integer (rtx x, unsigned int size, int aligned_p)
9803 {
9804 /* ??? We only output .xword's for symbols and only then in environments
9805 where the assembler can handle them. */
9806 if (aligned_p && size == 8 && GET_CODE (x) != CONST_INT)
9807 {
9808 if (TARGET_V9)
9809 {
9810 assemble_integer_with_op ("\t.xword\t", x);
9811 return true;
9812 }
9813 else
9814 {
9815 assemble_aligned_integer (4, const0_rtx);
9816 assemble_aligned_integer (4, x);
9817 return true;
9818 }
9819 }
9820 return default_assemble_integer (x, size, aligned_p);
9821 }
9822
9823 /* Return the value of a code used in the .proc pseudo-op that says
9824 what kind of result this function returns. For non-C types, we pick
9825 the closest C type. */
9826
9827 #ifndef SHORT_TYPE_SIZE
9828 #define SHORT_TYPE_SIZE (BITS_PER_UNIT * 2)
9829 #endif
9830
9831 #ifndef INT_TYPE_SIZE
9832 #define INT_TYPE_SIZE BITS_PER_WORD
9833 #endif
9834
9835 #ifndef LONG_TYPE_SIZE
9836 #define LONG_TYPE_SIZE BITS_PER_WORD
9837 #endif
9838
9839 #ifndef LONG_LONG_TYPE_SIZE
9840 #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
9841 #endif
9842
9843 unsigned long
9844 sparc_type_code (tree type)
9845 {
9846 unsigned long qualifiers = 0;
9847 unsigned shift;
9848
9849 /* Only the first 30 bits of the qualifier are valid. We must refrain from
9850 setting more, since some assemblers will give an error for this. Also,
9851 we must be careful to avoid shifts of 32 bits or more to avoid getting
9852 unpredictable results. */
9853
9854 for (shift = 6; shift < 30; shift += 2, type = TREE_TYPE (type))
9855 {
9856 switch (TREE_CODE (type))
9857 {
9858 case ERROR_MARK:
9859 return qualifiers;
9860
9861 case ARRAY_TYPE:
9862 qualifiers |= (3 << shift);
9863 break;
9864
9865 case FUNCTION_TYPE:
9866 case METHOD_TYPE:
9867 qualifiers |= (2 << shift);
9868 break;
9869
9870 case POINTER_TYPE:
9871 case REFERENCE_TYPE:
9872 case OFFSET_TYPE:
9873 qualifiers |= (1 << shift);
9874 break;
9875
9876 case RECORD_TYPE:
9877 return (qualifiers | 8);
9878
9879 case UNION_TYPE:
9880 case QUAL_UNION_TYPE:
9881 return (qualifiers | 9);
9882
9883 case ENUMERAL_TYPE:
9884 return (qualifiers | 10);
9885
9886 case VOID_TYPE:
9887 return (qualifiers | 16);
9888
9889 case INTEGER_TYPE:
9890 /* If this is a range type, consider it to be the underlying
9891 type. */
9892 if (TREE_TYPE (type) != 0)
9893 break;
9894
9895 /* Carefully distinguish all the standard types of C,
9896 without messing up if the language is not C. We do this by
9897 testing TYPE_PRECISION and TYPE_UNSIGNED. The old code used to
9898 look at both the names and the above fields, but that's redundant.
9899 Any type whose size is between two C types will be considered
9900 to be the wider of the two types. Also, we do not have a
9901 special code to use for "long long", so anything wider than
9902 long is treated the same. Note that we can't distinguish
9903 between "int" and "long" in this code if they are the same
9904 size, but that's fine, since neither can the assembler. */
9905
9906 if (TYPE_PRECISION (type) <= CHAR_TYPE_SIZE)
9907 return (qualifiers | (TYPE_UNSIGNED (type) ? 12 : 2));
9908
9909 else if (TYPE_PRECISION (type) <= SHORT_TYPE_SIZE)
9910 return (qualifiers | (TYPE_UNSIGNED (type) ? 13 : 3));
9911
9912 else if (TYPE_PRECISION (type) <= INT_TYPE_SIZE)
9913 return (qualifiers | (TYPE_UNSIGNED (type) ? 14 : 4));
9914
9915 else
9916 return (qualifiers | (TYPE_UNSIGNED (type) ? 15 : 5));
9917
9918 case REAL_TYPE:
9919 /* If this is a range type, consider it to be the underlying
9920 type. */
9921 if (TREE_TYPE (type) != 0)
9922 break;
9923
9924 /* Carefully distinguish all the standard types of C,
9925 without messing up if the language is not C. */
9926
9927 if (TYPE_PRECISION (type) == TYPE_PRECISION (float_type_node))
9928 return (qualifiers | 6);
9929
9930 else
9931 return (qualifiers | 7);
9932
9933 case COMPLEX_TYPE: /* GNU Fortran COMPLEX type. */
9934 /* ??? We need to distinguish between double and float complex types,
9935 but I don't know how yet because I can't reach this code from
9936 existing front-ends. */
9937 return (qualifiers | 7); /* Who knows? */
9938
9939 case VECTOR_TYPE:
9940 case BOOLEAN_TYPE: /* Boolean truth value type. */
9941 case LANG_TYPE:
9942 case NULLPTR_TYPE:
9943 return qualifiers;
9944
9945 default:
9946 gcc_unreachable (); /* Not a type! */
9947 }
9948 }
9949
9950 return qualifiers;
9951 }
9952
9953 /* Nested function support. */
9954
9955 /* Emit RTL insns to initialize the variable parts of a trampoline.
9956 FNADDR is an RTX for the address of the function's pure code.
9957 CXT is an RTX for the static chain value for the function.
9958
9959 This takes 16 insns: 2 shifts & 2 ands (to split up addresses), 4 sethi
9960 (to load in opcodes), 4 iors (to merge address and opcodes), and 4 writes
9961 (to store insns). This is a bit excessive. Perhaps a different
9962 mechanism would be better here.
9963
9964 Emit enough FLUSH insns to synchronize the data and instruction caches. */
9965
9966 static void
9967 sparc32_initialize_trampoline (rtx m_tramp, rtx fnaddr, rtx cxt)
9968 {
9969 /* SPARC 32-bit trampoline:
9970
9971 sethi %hi(fn), %g1
9972 sethi %hi(static), %g2
9973 jmp %g1+%lo(fn)
9974 or %g2, %lo(static), %g2
9975
9976 SETHI i,r = 00rr rrr1 00ii iiii iiii iiii iiii iiii
9977 JMPL r+i,d = 10dd ddd1 1100 0rrr rr1i iiii iiii iiii
9978 */
9979
9980 emit_move_insn
9981 (adjust_address (m_tramp, SImode, 0),
9982 expand_binop (SImode, ior_optab,
9983 expand_shift (RSHIFT_EXPR, SImode, fnaddr, 10, 0, 1),
9984 GEN_INT (trunc_int_for_mode (0x03000000, SImode)),
9985 NULL_RTX, 1, OPTAB_DIRECT));
9986
9987 emit_move_insn
9988 (adjust_address (m_tramp, SImode, 4),
9989 expand_binop (SImode, ior_optab,
9990 expand_shift (RSHIFT_EXPR, SImode, cxt, 10, 0, 1),
9991 GEN_INT (trunc_int_for_mode (0x05000000, SImode)),
9992 NULL_RTX, 1, OPTAB_DIRECT));
9993
9994 emit_move_insn
9995 (adjust_address (m_tramp, SImode, 8),
9996 expand_binop (SImode, ior_optab,
9997 expand_and (SImode, fnaddr, GEN_INT (0x3ff), NULL_RTX),
9998 GEN_INT (trunc_int_for_mode (0x81c06000, SImode)),
9999 NULL_RTX, 1, OPTAB_DIRECT));
10000
10001 emit_move_insn
10002 (adjust_address (m_tramp, SImode, 12),
10003 expand_binop (SImode, ior_optab,
10004 expand_and (SImode, cxt, GEN_INT (0x3ff), NULL_RTX),
10005 GEN_INT (trunc_int_for_mode (0x8410a000, SImode)),
10006 NULL_RTX, 1, OPTAB_DIRECT));
10007
10008 emit_insn
10009 (gen_flush (SImode, validize_mem (adjust_address (m_tramp, SImode, 0))));
10010
10011 /* On UltraSPARC a flush flushes an entire cache line. The trampoline is
10012 aligned on a 16 byte boundary so one flush clears it all. */
10013 if (sparc_cpu != PROCESSOR_ULTRASPARC
10014 && sparc_cpu != PROCESSOR_ULTRASPARC3
10015 && sparc_cpu != PROCESSOR_NIAGARA
10016 && sparc_cpu != PROCESSOR_NIAGARA2
10017 && sparc_cpu != PROCESSOR_NIAGARA3
10018 && sparc_cpu != PROCESSOR_NIAGARA4
10019 && sparc_cpu != PROCESSOR_NIAGARA7
10020 && sparc_cpu != PROCESSOR_M8)
10021 emit_insn
10022 (gen_flush (SImode, validize_mem (adjust_address (m_tramp, SImode, 8))));
10023
10024 /* Call __enable_execute_stack after writing onto the stack to make sure
10025 the stack address is accessible. */
10026 #ifdef HAVE_ENABLE_EXECUTE_STACK
10027 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
10028 LCT_NORMAL, VOIDmode, XEXP (m_tramp, 0), Pmode);
10029 #endif
10030
10031 }
10032
10033 /* The 64-bit version is simpler because it makes more sense to load the
10034 values as "immediate" data out of the trampoline. It's also easier since
10035 we can read the PC without clobbering a register. */
10036
10037 static void
10038 sparc64_initialize_trampoline (rtx m_tramp, rtx fnaddr, rtx cxt)
10039 {
10040 /* SPARC 64-bit trampoline:
10041
10042 rd %pc, %g1
10043 ldx [%g1+24], %g5
10044 jmp %g5
10045 ldx [%g1+16], %g5
10046 +16 bytes data
10047 */
10048
10049 emit_move_insn (adjust_address (m_tramp, SImode, 0),
10050 GEN_INT (trunc_int_for_mode (0x83414000, SImode)));
10051 emit_move_insn (adjust_address (m_tramp, SImode, 4),
10052 GEN_INT (trunc_int_for_mode (0xca586018, SImode)));
10053 emit_move_insn (adjust_address (m_tramp, SImode, 8),
10054 GEN_INT (trunc_int_for_mode (0x81c14000, SImode)));
10055 emit_move_insn (adjust_address (m_tramp, SImode, 12),
10056 GEN_INT (trunc_int_for_mode (0xca586010, SImode)));
10057 emit_move_insn (adjust_address (m_tramp, DImode, 16), cxt);
10058 emit_move_insn (adjust_address (m_tramp, DImode, 24), fnaddr);
10059 emit_insn
10060 (gen_flush (DImode, validize_mem (adjust_address (m_tramp, DImode, 0))));
10061
10062 /* On UltraSPARC a flush flushes an entire cache line. The trampoline is
10063 aligned on a 16 byte boundary so one flush clears it all. */
10064 if (sparc_cpu != PROCESSOR_ULTRASPARC
10065 && sparc_cpu != PROCESSOR_ULTRASPARC3
10066 && sparc_cpu != PROCESSOR_NIAGARA
10067 && sparc_cpu != PROCESSOR_NIAGARA2
10068 && sparc_cpu != PROCESSOR_NIAGARA3
10069 && sparc_cpu != PROCESSOR_NIAGARA4
10070 && sparc_cpu != PROCESSOR_NIAGARA7
10071 && sparc_cpu != PROCESSOR_M8)
10072 emit_insn
10073 (gen_flush (DImode, validize_mem (adjust_address (m_tramp, DImode, 8))));
10074
10075 /* Call __enable_execute_stack after writing onto the stack to make sure
10076 the stack address is accessible. */
10077 #ifdef HAVE_ENABLE_EXECUTE_STACK
10078 emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
10079 LCT_NORMAL, VOIDmode, XEXP (m_tramp, 0), Pmode);
10080 #endif
10081 }
10082
10083 /* Worker for TARGET_TRAMPOLINE_INIT. */
10084
10085 static void
10086 sparc_trampoline_init (rtx m_tramp, tree fndecl, rtx cxt)
10087 {
10088 rtx fnaddr = force_reg (Pmode, XEXP (DECL_RTL (fndecl), 0));
10089 cxt = force_reg (Pmode, cxt);
10090 if (TARGET_ARCH64)
10091 sparc64_initialize_trampoline (m_tramp, fnaddr, cxt);
10092 else
10093 sparc32_initialize_trampoline (m_tramp, fnaddr, cxt);
10094 }
10095
10096 /* Adjust the cost of a scheduling dependency. Return the new cost of
10097 a dependency LINK or INSN on DEP_INSN. COST is the current cost. */
10098
10099 static int
10100 supersparc_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep_insn,
10101 int cost)
10102 {
10103 enum attr_type insn_type;
10104
10105 if (recog_memoized (insn) < 0)
10106 return cost;
10107
10108 insn_type = get_attr_type (insn);
10109
10110 if (dep_type == 0)
10111 {
10112 /* Data dependency; DEP_INSN writes a register that INSN reads some
10113 cycles later. */
10114
10115 /* if a load, then the dependence must be on the memory address;
10116 add an extra "cycle". Note that the cost could be two cycles
10117 if the reg was written late in an instruction group; we ca not tell
10118 here. */
10119 if (insn_type == TYPE_LOAD || insn_type == TYPE_FPLOAD)
10120 return cost + 3;
10121
10122 /* Get the delay only if the address of the store is the dependence. */
10123 if (insn_type == TYPE_STORE || insn_type == TYPE_FPSTORE)
10124 {
10125 rtx pat = PATTERN(insn);
10126 rtx dep_pat = PATTERN (dep_insn);
10127
10128 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
10129 return cost; /* This should not happen! */
10130
10131 /* The dependency between the two instructions was on the data that
10132 is being stored. Assume that this implies that the address of the
10133 store is not dependent. */
10134 if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
10135 return cost;
10136
10137 return cost + 3; /* An approximation. */
10138 }
10139
10140 /* A shift instruction cannot receive its data from an instruction
10141 in the same cycle; add a one cycle penalty. */
10142 if (insn_type == TYPE_SHIFT)
10143 return cost + 3; /* Split before cascade into shift. */
10144 }
10145 else
10146 {
10147 /* Anti- or output- dependency; DEP_INSN reads/writes a register that
10148 INSN writes some cycles later. */
10149
10150 /* These are only significant for the fpu unit; writing a fp reg before
10151 the fpu has finished with it stalls the processor. */
10152
10153 /* Reusing an integer register causes no problems. */
10154 if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
10155 return 0;
10156 }
10157
10158 return cost;
10159 }
10160
10161 static int
10162 hypersparc_adjust_cost (rtx_insn *insn, int dtype, rtx_insn *dep_insn,
10163 int cost)
10164 {
10165 enum attr_type insn_type, dep_type;
10166 rtx pat = PATTERN(insn);
10167 rtx dep_pat = PATTERN (dep_insn);
10168
10169 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
10170 return cost;
10171
10172 insn_type = get_attr_type (insn);
10173 dep_type = get_attr_type (dep_insn);
10174
10175 switch (dtype)
10176 {
10177 case 0:
10178 /* Data dependency; DEP_INSN writes a register that INSN reads some
10179 cycles later. */
10180
10181 switch (insn_type)
10182 {
10183 case TYPE_STORE:
10184 case TYPE_FPSTORE:
10185 /* Get the delay iff the address of the store is the dependence. */
10186 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
10187 return cost;
10188
10189 if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
10190 return cost;
10191 return cost + 3;
10192
10193 case TYPE_LOAD:
10194 case TYPE_SLOAD:
10195 case TYPE_FPLOAD:
10196 /* If a load, then the dependence must be on the memory address. If
10197 the addresses aren't equal, then it might be a false dependency */
10198 if (dep_type == TYPE_STORE || dep_type == TYPE_FPSTORE)
10199 {
10200 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET
10201 || GET_CODE (SET_DEST (dep_pat)) != MEM
10202 || GET_CODE (SET_SRC (pat)) != MEM
10203 || ! rtx_equal_p (XEXP (SET_DEST (dep_pat), 0),
10204 XEXP (SET_SRC (pat), 0)))
10205 return cost + 2;
10206
10207 return cost + 8;
10208 }
10209 break;
10210
10211 case TYPE_BRANCH:
10212 /* Compare to branch latency is 0. There is no benefit from
10213 separating compare and branch. */
10214 if (dep_type == TYPE_COMPARE)
10215 return 0;
10216 /* Floating point compare to branch latency is less than
10217 compare to conditional move. */
10218 if (dep_type == TYPE_FPCMP)
10219 return cost - 1;
10220 break;
10221 default:
10222 break;
10223 }
10224 break;
10225
10226 case REG_DEP_ANTI:
10227 /* Anti-dependencies only penalize the fpu unit. */
10228 if (insn_type == TYPE_IALU || insn_type == TYPE_SHIFT)
10229 return 0;
10230 break;
10231
10232 default:
10233 break;
10234 }
10235
10236 return cost;
10237 }
10238
10239 static int
10240 leon5_adjust_cost (rtx_insn *insn, int dtype, rtx_insn *dep_insn,
10241 int cost)
10242 {
10243 enum attr_type insn_type, dep_type;
10244 rtx pat = PATTERN (insn);
10245 rtx dep_pat = PATTERN (dep_insn);
10246
10247 if (recog_memoized (insn) < 0 || recog_memoized (dep_insn) < 0)
10248 return cost;
10249
10250 insn_type = get_attr_type (insn);
10251 dep_type = get_attr_type (dep_insn);
10252
10253 switch (dtype)
10254 {
10255 case REG_DEP_TRUE:
10256 /* Data dependency; DEP_INSN writes a register that INSN reads some
10257 cycles later. */
10258
10259 switch (insn_type)
10260 {
10261 case TYPE_STORE:
10262 /* Try to schedule three instructions between the store and
10263 the ALU instruction that generated the data. */
10264 if (dep_type == TYPE_IALU || dep_type == TYPE_SHIFT)
10265 {
10266 if (GET_CODE (pat) != SET || GET_CODE (dep_pat) != SET)
10267 break;
10268
10269 if (rtx_equal_p (SET_DEST (dep_pat), SET_SRC (pat)))
10270 return 4;
10271 }
10272 break;
10273 default:
10274 break;
10275 }
10276 break;
10277 case REG_DEP_ANTI:
10278 /* Penalize anti-dependencies for FPU instructions. */
10279 if (fpop_insn_p (insn) || insn_type == TYPE_FPLOAD)
10280 return 4;
10281 break;
10282 default:
10283 break;
10284 }
10285
10286 return cost;
10287 }
10288
10289 static int
10290 sparc_adjust_cost (rtx_insn *insn, int dep_type, rtx_insn *dep, int cost,
10291 unsigned int)
10292 {
10293 switch (sparc_cpu)
10294 {
10295 case PROCESSOR_LEON5:
10296 cost = leon5_adjust_cost (insn, dep_type, dep, cost);
10297 break;
10298 case PROCESSOR_SUPERSPARC:
10299 cost = supersparc_adjust_cost (insn, dep_type, dep, cost);
10300 break;
10301 case PROCESSOR_HYPERSPARC:
10302 case PROCESSOR_SPARCLITE86X:
10303 cost = hypersparc_adjust_cost (insn, dep_type, dep, cost);
10304 break;
10305 default:
10306 break;
10307 }
10308 return cost;
10309 }
10310
10311 static void
10312 sparc_sched_init (FILE *dump ATTRIBUTE_UNUSED,
10313 int sched_verbose ATTRIBUTE_UNUSED,
10314 int max_ready ATTRIBUTE_UNUSED)
10315 {}
10316
10317 static int
10318 sparc_use_sched_lookahead (void)
10319 {
10320 switch (sparc_cpu)
10321 {
10322 case PROCESSOR_ULTRASPARC:
10323 case PROCESSOR_ULTRASPARC3:
10324 return 4;
10325 case PROCESSOR_SUPERSPARC:
10326 case PROCESSOR_HYPERSPARC:
10327 case PROCESSOR_SPARCLITE86X:
10328 return 3;
10329 case PROCESSOR_NIAGARA4:
10330 case PROCESSOR_NIAGARA7:
10331 case PROCESSOR_M8:
10332 return 2;
10333 case PROCESSOR_NIAGARA:
10334 case PROCESSOR_NIAGARA2:
10335 case PROCESSOR_NIAGARA3:
10336 default:
10337 return 0;
10338 }
10339 }
10340
10341 static int
10342 sparc_issue_rate (void)
10343 {
10344 switch (sparc_cpu)
10345 {
10346 case PROCESSOR_ULTRASPARC:
10347 case PROCESSOR_ULTRASPARC3:
10348 case PROCESSOR_M8:
10349 return 4;
10350 case PROCESSOR_SUPERSPARC:
10351 return 3;
10352 case PROCESSOR_HYPERSPARC:
10353 case PROCESSOR_SPARCLITE86X:
10354 case PROCESSOR_V9:
10355 /* Assume V9 processors are capable of at least dual-issue. */
10356 case PROCESSOR_NIAGARA4:
10357 case PROCESSOR_NIAGARA7:
10358 return 2;
10359 case PROCESSOR_NIAGARA:
10360 case PROCESSOR_NIAGARA2:
10361 case PROCESSOR_NIAGARA3:
10362 default:
10363 return 1;
10364 }
10365 }
10366
10367 int
10368 sparc_branch_cost (bool speed_p, bool predictable_p)
10369 {
10370 if (!speed_p)
10371 return 2;
10372
10373 /* For pre-V9 processors we use a single value (usually 3) to take into
10374 account the potential annulling of the delay slot (which ends up being
10375 a bubble in the pipeline slot) plus a cycle to take into consideration
10376 the instruction cache effects.
10377
10378 On V9 and later processors, which have branch prediction facilities,
10379 we take into account whether the branch is (easily) predictable. */
10380 const int cost = sparc_costs->branch_cost;
10381
10382 switch (sparc_cpu)
10383 {
10384 case PROCESSOR_V9:
10385 case PROCESSOR_ULTRASPARC:
10386 case PROCESSOR_ULTRASPARC3:
10387 case PROCESSOR_NIAGARA:
10388 case PROCESSOR_NIAGARA2:
10389 case PROCESSOR_NIAGARA3:
10390 case PROCESSOR_NIAGARA4:
10391 case PROCESSOR_NIAGARA7:
10392 case PROCESSOR_M8:
10393 return cost + (predictable_p ? 0 : 2);
10394
10395 default:
10396 return cost;
10397 }
10398 }
10399
10400 static int
10401 set_extends (rtx_insn *insn)
10402 {
10403 rtx pat = PATTERN (insn);
10404
10405 switch (GET_CODE (SET_SRC (pat)))
10406 {
10407 /* Load and some shift instructions zero extend. */
10408 case MEM:
10409 case ZERO_EXTEND:
10410 /* sethi clears the high bits */
10411 case HIGH:
10412 /* LO_SUM is used with sethi. sethi cleared the high
10413 bits and the values used with lo_sum are positive */
10414 case LO_SUM:
10415 /* Store flag stores 0 or 1 */
10416 case LT: case LTU:
10417 case GT: case GTU:
10418 case LE: case LEU:
10419 case GE: case GEU:
10420 case EQ:
10421 case NE:
10422 return 1;
10423 case AND:
10424 {
10425 rtx op0 = XEXP (SET_SRC (pat), 0);
10426 rtx op1 = XEXP (SET_SRC (pat), 1);
10427 if (GET_CODE (op1) == CONST_INT)
10428 return INTVAL (op1) >= 0;
10429 if (GET_CODE (op0) != REG)
10430 return 0;
10431 if (sparc_check_64 (op0, insn) == 1)
10432 return 1;
10433 return (GET_CODE (op1) == REG && sparc_check_64 (op1, insn) == 1);
10434 }
10435 case IOR:
10436 case XOR:
10437 {
10438 rtx op0 = XEXP (SET_SRC (pat), 0);
10439 rtx op1 = XEXP (SET_SRC (pat), 1);
10440 if (GET_CODE (op0) != REG || sparc_check_64 (op0, insn) <= 0)
10441 return 0;
10442 if (GET_CODE (op1) == CONST_INT)
10443 return INTVAL (op1) >= 0;
10444 return (GET_CODE (op1) == REG && sparc_check_64 (op1, insn) == 1);
10445 }
10446 case LSHIFTRT:
10447 return GET_MODE (SET_SRC (pat)) == SImode;
10448 /* Positive integers leave the high bits zero. */
10449 case CONST_INT:
10450 return !(INTVAL (SET_SRC (pat)) & 0x80000000);
10451 case ASHIFTRT:
10452 case SIGN_EXTEND:
10453 return - (GET_MODE (SET_SRC (pat)) == SImode);
10454 case REG:
10455 return sparc_check_64 (SET_SRC (pat), insn);
10456 default:
10457 return 0;
10458 }
10459 }
10460
10461 /* We _ought_ to have only one kind per function, but... */
10462 static GTY(()) rtx sparc_addr_diff_list;
10463 static GTY(()) rtx sparc_addr_list;
10464
10465 void
10466 sparc_defer_case_vector (rtx lab, rtx vec, int diff)
10467 {
10468 vec = gen_rtx_EXPR_LIST (VOIDmode, lab, vec);
10469 if (diff)
10470 sparc_addr_diff_list
10471 = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_diff_list);
10472 else
10473 sparc_addr_list = gen_rtx_EXPR_LIST (VOIDmode, vec, sparc_addr_list);
10474 }
10475
10476 static void
10477 sparc_output_addr_vec (rtx vec)
10478 {
10479 rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
10480 int idx, vlen = XVECLEN (body, 0);
10481
10482 #ifdef ASM_OUTPUT_ADDR_VEC_START
10483 ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
10484 #endif
10485
10486 #ifdef ASM_OUTPUT_CASE_LABEL
10487 ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
10488 NEXT_INSN (lab));
10489 #else
10490 (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
10491 #endif
10492
10493 for (idx = 0; idx < vlen; idx++)
10494 {
10495 ASM_OUTPUT_ADDR_VEC_ELT
10496 (asm_out_file, CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 0, idx), 0)));
10497 }
10498
10499 #ifdef ASM_OUTPUT_ADDR_VEC_END
10500 ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
10501 #endif
10502 }
10503
10504 static void
10505 sparc_output_addr_diff_vec (rtx vec)
10506 {
10507 rtx lab = XEXP (vec, 0), body = XEXP (vec, 1);
10508 rtx base = XEXP (XEXP (body, 0), 0);
10509 int idx, vlen = XVECLEN (body, 1);
10510
10511 #ifdef ASM_OUTPUT_ADDR_VEC_START
10512 ASM_OUTPUT_ADDR_VEC_START (asm_out_file);
10513 #endif
10514
10515 #ifdef ASM_OUTPUT_CASE_LABEL
10516 ASM_OUTPUT_CASE_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (lab),
10517 NEXT_INSN (lab));
10518 #else
10519 (*targetm.asm_out.internal_label) (asm_out_file, "L", CODE_LABEL_NUMBER (lab));
10520 #endif
10521
10522 for (idx = 0; idx < vlen; idx++)
10523 {
10524 ASM_OUTPUT_ADDR_DIFF_ELT
10525 (asm_out_file,
10526 body,
10527 CODE_LABEL_NUMBER (XEXP (XVECEXP (body, 1, idx), 0)),
10528 CODE_LABEL_NUMBER (base));
10529 }
10530
10531 #ifdef ASM_OUTPUT_ADDR_VEC_END
10532 ASM_OUTPUT_ADDR_VEC_END (asm_out_file);
10533 #endif
10534 }
10535
10536 static void
10537 sparc_output_deferred_case_vectors (void)
10538 {
10539 rtx t;
10540 int align;
10541
10542 if (sparc_addr_list == NULL_RTX
10543 && sparc_addr_diff_list == NULL_RTX)
10544 return;
10545
10546 /* Align to cache line in the function's code section. */
10547 switch_to_section (current_function_section ());
10548
10549 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
10550 if (align > 0)
10551 ASM_OUTPUT_ALIGN (asm_out_file, align);
10552
10553 for (t = sparc_addr_list; t ; t = XEXP (t, 1))
10554 sparc_output_addr_vec (XEXP (t, 0));
10555 for (t = sparc_addr_diff_list; t ; t = XEXP (t, 1))
10556 sparc_output_addr_diff_vec (XEXP (t, 0));
10557
10558 sparc_addr_list = sparc_addr_diff_list = NULL_RTX;
10559 }
10560
10561 /* Return 0 if the high 32 bits of X (the low word of X, if DImode) are
10562 unknown. Return 1 if the high bits are zero, -1 if the register is
10563 sign extended. */
10564 int
10565 sparc_check_64 (rtx x, rtx_insn *insn)
10566 {
10567 /* If a register is set only once it is safe to ignore insns this
10568 code does not know how to handle. The loop will either recognize
10569 the single set and return the correct value or fail to recognize
10570 it and return 0. */
10571 int set_once = 0;
10572 rtx y = x;
10573
10574 gcc_assert (GET_CODE (x) == REG);
10575
10576 if (GET_MODE (x) == DImode)
10577 y = gen_rtx_REG (SImode, REGNO (x) + WORDS_BIG_ENDIAN);
10578
10579 if (flag_expensive_optimizations
10580 && df && DF_REG_DEF_COUNT (REGNO (y)) == 1)
10581 set_once = 1;
10582
10583 if (insn == 0)
10584 {
10585 if (set_once)
10586 insn = get_last_insn_anywhere ();
10587 else
10588 return 0;
10589 }
10590
10591 while ((insn = PREV_INSN (insn)))
10592 {
10593 switch (GET_CODE (insn))
10594 {
10595 case JUMP_INSN:
10596 case NOTE:
10597 break;
10598 case CODE_LABEL:
10599 case CALL_INSN:
10600 default:
10601 if (! set_once)
10602 return 0;
10603 break;
10604 case INSN:
10605 {
10606 rtx pat = PATTERN (insn);
10607 if (GET_CODE (pat) != SET)
10608 return 0;
10609 if (rtx_equal_p (x, SET_DEST (pat)))
10610 return set_extends (insn);
10611 if (y && rtx_equal_p (y, SET_DEST (pat)))
10612 return set_extends (insn);
10613 if (reg_overlap_mentioned_p (SET_DEST (pat), y))
10614 return 0;
10615 }
10616 }
10617 }
10618 return 0;
10619 }
10620
10621 /* Output a wide shift instruction in V8+ mode. INSN is the instruction,
10622 OPERANDS are its operands and OPCODE is the mnemonic to be used. */
10623
10624 const char *
10625 output_v8plus_shift (rtx_insn *insn, rtx *operands, const char *opcode)
10626 {
10627 static char asm_code[60];
10628
10629 /* The scratch register is only required when the destination
10630 register is not a 64-bit global or out register. */
10631 if (which_alternative != 2)
10632 operands[3] = operands[0];
10633
10634 /* We can only shift by constants <= 63. */
10635 if (GET_CODE (operands[2]) == CONST_INT)
10636 operands[2] = GEN_INT (INTVAL (operands[2]) & 0x3f);
10637
10638 if (GET_CODE (operands[1]) == CONST_INT)
10639 {
10640 output_asm_insn ("mov\t%1, %3", operands);
10641 }
10642 else
10643 {
10644 output_asm_insn ("sllx\t%H1, 32, %3", operands);
10645 if (sparc_check_64 (operands[1], insn) <= 0)
10646 output_asm_insn ("srl\t%L1, 0, %L1", operands);
10647 output_asm_insn ("or\t%L1, %3, %3", operands);
10648 }
10649
10650 strcpy (asm_code, opcode);
10651
10652 if (which_alternative != 2)
10653 return strcat (asm_code, "\t%0, %2, %L0\n\tsrlx\t%L0, 32, %H0");
10654 else
10655 return
10656 strcat (asm_code, "\t%3, %2, %3\n\tsrlx\t%3, 32, %H0\n\tmov\t%3, %L0");
10657 }
10658
10659 /* Output rtl to increment the profiler label LABELNO
10660 for profiling a function entry. */
10661
10662 void
10663 sparc_profile_hook (int labelno)
10664 {
10665 char buf[32];
10666 rtx lab, fun;
10667
10668 fun = gen_rtx_SYMBOL_REF (Pmode, MCOUNT_FUNCTION);
10669 if (NO_PROFILE_COUNTERS)
10670 {
10671 emit_library_call (fun, LCT_NORMAL, VOIDmode);
10672 }
10673 else
10674 {
10675 ASM_GENERATE_INTERNAL_LABEL (buf, "LP", labelno);
10676 lab = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
10677 emit_library_call (fun, LCT_NORMAL, VOIDmode, lab, Pmode);
10678 }
10679 }
10680
10681 #ifdef TARGET_SOLARIS
10682 /* Solaris implementation of TARGET_ASM_NAMED_SECTION. */
10683
10684 static void
10685 sparc_solaris_elf_asm_named_section (const char *name, unsigned int flags,
10686 tree decl ATTRIBUTE_UNUSED)
10687 {
10688 if (HAVE_COMDAT_GROUP && flags & SECTION_LINKONCE)
10689 {
10690 solaris_elf_asm_comdat_section (name, flags, decl);
10691 return;
10692 }
10693
10694 fprintf (asm_out_file, "\t.section\t\"%s\"", name);
10695
10696 if (!(flags & SECTION_DEBUG))
10697 fputs (",#alloc", asm_out_file);
10698 #if HAVE_GAS_SECTION_EXCLUDE
10699 if (flags & SECTION_EXCLUDE)
10700 fputs (",#exclude", asm_out_file);
10701 #endif
10702 if (flags & SECTION_WRITE)
10703 fputs (",#write", asm_out_file);
10704 if (flags & SECTION_TLS)
10705 fputs (",#tls", asm_out_file);
10706 if (flags & SECTION_CODE)
10707 fputs (",#execinstr", asm_out_file);
10708
10709 if (flags & SECTION_NOTYPE)
10710 ;
10711 else if (flags & SECTION_BSS)
10712 fputs (",#nobits", asm_out_file);
10713 else
10714 fputs (",#progbits", asm_out_file);
10715
10716 fputc ('\n', asm_out_file);
10717 }
10718 #endif /* TARGET_SOLARIS */
10719
10720 /* We do not allow indirect calls to be optimized into sibling calls.
10721
10722 We cannot use sibling calls when delayed branches are disabled
10723 because they will likely require the call delay slot to be filled.
10724
10725 Also, on SPARC 32-bit we cannot emit a sibling call when the
10726 current function returns a structure. This is because the "unimp
10727 after call" convention would cause the callee to return to the
10728 wrong place. The generic code already disallows cases where the
10729 function being called returns a structure.
10730
10731 It may seem strange how this last case could occur. Usually there
10732 is code after the call which jumps to epilogue code which dumps the
10733 return value into the struct return area. That ought to invalidate
10734 the sibling call right? Well, in the C++ case we can end up passing
10735 the pointer to the struct return area to a constructor (which returns
10736 void) and then nothing else happens. Such a sibling call would look
10737 valid without the added check here.
10738
10739 VxWorks PIC PLT entries require the global pointer to be initialized
10740 on entry. We therefore can't emit sibling calls to them. */
10741 static bool
10742 sparc_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
10743 {
10744 return (decl
10745 && flag_delayed_branch
10746 && (TARGET_ARCH64 || ! cfun->returns_struct)
10747 && !(TARGET_VXWORKS_RTP
10748 && flag_pic
10749 && !targetm.binds_local_p (decl)));
10750 }
10751
10752 /* libfunc renaming. */
10753
10754 static void
10755 sparc_init_libfuncs (void)
10756 {
10757 if (TARGET_ARCH32)
10758 {
10759 /* Use the subroutines that Sun's library provides for integer
10760 multiply and divide. The `*' prevents an underscore from
10761 being prepended by the compiler. .umul is a little faster
10762 than .mul. */
10763 set_optab_libfunc (smul_optab, SImode, "*.umul");
10764 set_optab_libfunc (sdiv_optab, SImode, "*.div");
10765 set_optab_libfunc (udiv_optab, SImode, "*.udiv");
10766 set_optab_libfunc (smod_optab, SImode, "*.rem");
10767 set_optab_libfunc (umod_optab, SImode, "*.urem");
10768
10769 /* TFmode arithmetic. These names are part of the SPARC 32bit ABI. */
10770 set_optab_libfunc (add_optab, TFmode, "_Q_add");
10771 set_optab_libfunc (sub_optab, TFmode, "_Q_sub");
10772 set_optab_libfunc (neg_optab, TFmode, "_Q_neg");
10773 set_optab_libfunc (smul_optab, TFmode, "_Q_mul");
10774 set_optab_libfunc (sdiv_optab, TFmode, "_Q_div");
10775
10776 /* We can define the TFmode sqrt optab only if TARGET_FPU. This
10777 is because with soft-float, the SFmode and DFmode sqrt
10778 instructions will be absent, and the compiler will notice and
10779 try to use the TFmode sqrt instruction for calls to the
10780 builtin function sqrt, but this fails. */
10781 if (TARGET_FPU)
10782 set_optab_libfunc (sqrt_optab, TFmode, "_Q_sqrt");
10783
10784 set_optab_libfunc (eq_optab, TFmode, "_Q_feq");
10785 set_optab_libfunc (ne_optab, TFmode, "_Q_fne");
10786 set_optab_libfunc (gt_optab, TFmode, "_Q_fgt");
10787 set_optab_libfunc (ge_optab, TFmode, "_Q_fge");
10788 set_optab_libfunc (lt_optab, TFmode, "_Q_flt");
10789 set_optab_libfunc (le_optab, TFmode, "_Q_fle");
10790
10791 set_conv_libfunc (sext_optab, TFmode, SFmode, "_Q_stoq");
10792 set_conv_libfunc (sext_optab, TFmode, DFmode, "_Q_dtoq");
10793 set_conv_libfunc (trunc_optab, SFmode, TFmode, "_Q_qtos");
10794 set_conv_libfunc (trunc_optab, DFmode, TFmode, "_Q_qtod");
10795
10796 set_conv_libfunc (sfix_optab, SImode, TFmode, "_Q_qtoi");
10797 set_conv_libfunc (ufix_optab, SImode, TFmode, "_Q_qtou");
10798 set_conv_libfunc (sfloat_optab, TFmode, SImode, "_Q_itoq");
10799 set_conv_libfunc (ufloat_optab, TFmode, SImode, "_Q_utoq");
10800
10801 if (DITF_CONVERSION_LIBFUNCS)
10802 {
10803 set_conv_libfunc (sfix_optab, DImode, TFmode, "_Q_qtoll");
10804 set_conv_libfunc (ufix_optab, DImode, TFmode, "_Q_qtoull");
10805 set_conv_libfunc (sfloat_optab, TFmode, DImode, "_Q_lltoq");
10806 set_conv_libfunc (ufloat_optab, TFmode, DImode, "_Q_ulltoq");
10807 }
10808
10809 if (SUN_CONVERSION_LIBFUNCS)
10810 {
10811 set_conv_libfunc (sfix_optab, DImode, SFmode, "__ftoll");
10812 set_conv_libfunc (ufix_optab, DImode, SFmode, "__ftoull");
10813 set_conv_libfunc (sfix_optab, DImode, DFmode, "__dtoll");
10814 set_conv_libfunc (ufix_optab, DImode, DFmode, "__dtoull");
10815 }
10816 }
10817 if (TARGET_ARCH64)
10818 {
10819 /* In the SPARC 64bit ABI, SImode multiply and divide functions
10820 do not exist in the library. Make sure the compiler does not
10821 emit calls to them by accident. (It should always use the
10822 hardware instructions.) */
10823 set_optab_libfunc (smul_optab, SImode, 0);
10824 set_optab_libfunc (sdiv_optab, SImode, 0);
10825 set_optab_libfunc (udiv_optab, SImode, 0);
10826 set_optab_libfunc (smod_optab, SImode, 0);
10827 set_optab_libfunc (umod_optab, SImode, 0);
10828
10829 if (SUN_INTEGER_MULTIPLY_64)
10830 {
10831 set_optab_libfunc (smul_optab, DImode, "__mul64");
10832 set_optab_libfunc (sdiv_optab, DImode, "__div64");
10833 set_optab_libfunc (udiv_optab, DImode, "__udiv64");
10834 set_optab_libfunc (smod_optab, DImode, "__rem64");
10835 set_optab_libfunc (umod_optab, DImode, "__urem64");
10836 }
10837
10838 if (SUN_CONVERSION_LIBFUNCS)
10839 {
10840 set_conv_libfunc (sfix_optab, DImode, SFmode, "__ftol");
10841 set_conv_libfunc (ufix_optab, DImode, SFmode, "__ftoul");
10842 set_conv_libfunc (sfix_optab, DImode, DFmode, "__dtol");
10843 set_conv_libfunc (ufix_optab, DImode, DFmode, "__dtoul");
10844 }
10845 }
10846 }
10847
10848 /* SPARC builtins. */
10849 enum sparc_builtins
10850 {
10851 /* FPU builtins. */
10852 SPARC_BUILTIN_LDFSR,
10853 SPARC_BUILTIN_STFSR,
10854
10855 /* VIS 1.0 builtins. */
10856 SPARC_BUILTIN_FPACK16,
10857 SPARC_BUILTIN_FPACK32,
10858 SPARC_BUILTIN_FPACKFIX,
10859 SPARC_BUILTIN_FEXPAND,
10860 SPARC_BUILTIN_FPMERGE,
10861 SPARC_BUILTIN_FMUL8X16,
10862 SPARC_BUILTIN_FMUL8X16AU,
10863 SPARC_BUILTIN_FMUL8X16AL,
10864 SPARC_BUILTIN_FMUL8SUX16,
10865 SPARC_BUILTIN_FMUL8ULX16,
10866 SPARC_BUILTIN_FMULD8SUX16,
10867 SPARC_BUILTIN_FMULD8ULX16,
10868 SPARC_BUILTIN_FALIGNDATAV4HI,
10869 SPARC_BUILTIN_FALIGNDATAV8QI,
10870 SPARC_BUILTIN_FALIGNDATAV2SI,
10871 SPARC_BUILTIN_FALIGNDATADI,
10872 SPARC_BUILTIN_WRGSR,
10873 SPARC_BUILTIN_RDGSR,
10874 SPARC_BUILTIN_ALIGNADDR,
10875 SPARC_BUILTIN_ALIGNADDRL,
10876 SPARC_BUILTIN_PDIST,
10877 SPARC_BUILTIN_EDGE8,
10878 SPARC_BUILTIN_EDGE8L,
10879 SPARC_BUILTIN_EDGE16,
10880 SPARC_BUILTIN_EDGE16L,
10881 SPARC_BUILTIN_EDGE32,
10882 SPARC_BUILTIN_EDGE32L,
10883 SPARC_BUILTIN_FCMPLE16,
10884 SPARC_BUILTIN_FCMPLE32,
10885 SPARC_BUILTIN_FCMPNE16,
10886 SPARC_BUILTIN_FCMPNE32,
10887 SPARC_BUILTIN_FCMPGT16,
10888 SPARC_BUILTIN_FCMPGT32,
10889 SPARC_BUILTIN_FCMPEQ16,
10890 SPARC_BUILTIN_FCMPEQ32,
10891 SPARC_BUILTIN_FPADD16,
10892 SPARC_BUILTIN_FPADD16S,
10893 SPARC_BUILTIN_FPADD32,
10894 SPARC_BUILTIN_FPADD32S,
10895 SPARC_BUILTIN_FPSUB16,
10896 SPARC_BUILTIN_FPSUB16S,
10897 SPARC_BUILTIN_FPSUB32,
10898 SPARC_BUILTIN_FPSUB32S,
10899 SPARC_BUILTIN_ARRAY8,
10900 SPARC_BUILTIN_ARRAY16,
10901 SPARC_BUILTIN_ARRAY32,
10902
10903 /* VIS 2.0 builtins. */
10904 SPARC_BUILTIN_EDGE8N,
10905 SPARC_BUILTIN_EDGE8LN,
10906 SPARC_BUILTIN_EDGE16N,
10907 SPARC_BUILTIN_EDGE16LN,
10908 SPARC_BUILTIN_EDGE32N,
10909 SPARC_BUILTIN_EDGE32LN,
10910 SPARC_BUILTIN_BMASK,
10911 SPARC_BUILTIN_BSHUFFLEV4HI,
10912 SPARC_BUILTIN_BSHUFFLEV8QI,
10913 SPARC_BUILTIN_BSHUFFLEV2SI,
10914 SPARC_BUILTIN_BSHUFFLEDI,
10915
10916 /* VIS 3.0 builtins. */
10917 SPARC_BUILTIN_CMASK8,
10918 SPARC_BUILTIN_CMASK16,
10919 SPARC_BUILTIN_CMASK32,
10920 SPARC_BUILTIN_FCHKSM16,
10921 SPARC_BUILTIN_FSLL16,
10922 SPARC_BUILTIN_FSLAS16,
10923 SPARC_BUILTIN_FSRL16,
10924 SPARC_BUILTIN_FSRA16,
10925 SPARC_BUILTIN_FSLL32,
10926 SPARC_BUILTIN_FSLAS32,
10927 SPARC_BUILTIN_FSRL32,
10928 SPARC_BUILTIN_FSRA32,
10929 SPARC_BUILTIN_PDISTN,
10930 SPARC_BUILTIN_FMEAN16,
10931 SPARC_BUILTIN_FPADD64,
10932 SPARC_BUILTIN_FPSUB64,
10933 SPARC_BUILTIN_FPADDS16,
10934 SPARC_BUILTIN_FPADDS16S,
10935 SPARC_BUILTIN_FPSUBS16,
10936 SPARC_BUILTIN_FPSUBS16S,
10937 SPARC_BUILTIN_FPADDS32,
10938 SPARC_BUILTIN_FPADDS32S,
10939 SPARC_BUILTIN_FPSUBS32,
10940 SPARC_BUILTIN_FPSUBS32S,
10941 SPARC_BUILTIN_FUCMPLE8,
10942 SPARC_BUILTIN_FUCMPNE8,
10943 SPARC_BUILTIN_FUCMPGT8,
10944 SPARC_BUILTIN_FUCMPEQ8,
10945 SPARC_BUILTIN_FHADDS,
10946 SPARC_BUILTIN_FHADDD,
10947 SPARC_BUILTIN_FHSUBS,
10948 SPARC_BUILTIN_FHSUBD,
10949 SPARC_BUILTIN_FNHADDS,
10950 SPARC_BUILTIN_FNHADDD,
10951 SPARC_BUILTIN_UMULXHI,
10952 SPARC_BUILTIN_XMULX,
10953 SPARC_BUILTIN_XMULXHI,
10954
10955 /* VIS 4.0 builtins. */
10956 SPARC_BUILTIN_FPADD8,
10957 SPARC_BUILTIN_FPADDS8,
10958 SPARC_BUILTIN_FPADDUS8,
10959 SPARC_BUILTIN_FPADDUS16,
10960 SPARC_BUILTIN_FPCMPLE8,
10961 SPARC_BUILTIN_FPCMPGT8,
10962 SPARC_BUILTIN_FPCMPULE16,
10963 SPARC_BUILTIN_FPCMPUGT16,
10964 SPARC_BUILTIN_FPCMPULE32,
10965 SPARC_BUILTIN_FPCMPUGT32,
10966 SPARC_BUILTIN_FPMAX8,
10967 SPARC_BUILTIN_FPMAX16,
10968 SPARC_BUILTIN_FPMAX32,
10969 SPARC_BUILTIN_FPMAXU8,
10970 SPARC_BUILTIN_FPMAXU16,
10971 SPARC_BUILTIN_FPMAXU32,
10972 SPARC_BUILTIN_FPMIN8,
10973 SPARC_BUILTIN_FPMIN16,
10974 SPARC_BUILTIN_FPMIN32,
10975 SPARC_BUILTIN_FPMINU8,
10976 SPARC_BUILTIN_FPMINU16,
10977 SPARC_BUILTIN_FPMINU32,
10978 SPARC_BUILTIN_FPSUB8,
10979 SPARC_BUILTIN_FPSUBS8,
10980 SPARC_BUILTIN_FPSUBUS8,
10981 SPARC_BUILTIN_FPSUBUS16,
10982
10983 /* VIS 4.0B builtins. */
10984
10985 /* Note that all the DICTUNPACK* entries should be kept
10986 contiguous. */
10987 SPARC_BUILTIN_FIRST_DICTUNPACK,
10988 SPARC_BUILTIN_DICTUNPACK8 = SPARC_BUILTIN_FIRST_DICTUNPACK,
10989 SPARC_BUILTIN_DICTUNPACK16,
10990 SPARC_BUILTIN_DICTUNPACK32,
10991 SPARC_BUILTIN_LAST_DICTUNPACK = SPARC_BUILTIN_DICTUNPACK32,
10992
10993 /* Note that all the FPCMP*SHL entries should be kept
10994 contiguous. */
10995 SPARC_BUILTIN_FIRST_FPCMPSHL,
10996 SPARC_BUILTIN_FPCMPLE8SHL = SPARC_BUILTIN_FIRST_FPCMPSHL,
10997 SPARC_BUILTIN_FPCMPGT8SHL,
10998 SPARC_BUILTIN_FPCMPEQ8SHL,
10999 SPARC_BUILTIN_FPCMPNE8SHL,
11000 SPARC_BUILTIN_FPCMPLE16SHL,
11001 SPARC_BUILTIN_FPCMPGT16SHL,
11002 SPARC_BUILTIN_FPCMPEQ16SHL,
11003 SPARC_BUILTIN_FPCMPNE16SHL,
11004 SPARC_BUILTIN_FPCMPLE32SHL,
11005 SPARC_BUILTIN_FPCMPGT32SHL,
11006 SPARC_BUILTIN_FPCMPEQ32SHL,
11007 SPARC_BUILTIN_FPCMPNE32SHL,
11008 SPARC_BUILTIN_FPCMPULE8SHL,
11009 SPARC_BUILTIN_FPCMPUGT8SHL,
11010 SPARC_BUILTIN_FPCMPULE16SHL,
11011 SPARC_BUILTIN_FPCMPUGT16SHL,
11012 SPARC_BUILTIN_FPCMPULE32SHL,
11013 SPARC_BUILTIN_FPCMPUGT32SHL,
11014 SPARC_BUILTIN_FPCMPDE8SHL,
11015 SPARC_BUILTIN_FPCMPDE16SHL,
11016 SPARC_BUILTIN_FPCMPDE32SHL,
11017 SPARC_BUILTIN_FPCMPUR8SHL,
11018 SPARC_BUILTIN_FPCMPUR16SHL,
11019 SPARC_BUILTIN_FPCMPUR32SHL,
11020 SPARC_BUILTIN_LAST_FPCMPSHL = SPARC_BUILTIN_FPCMPUR32SHL,
11021
11022 SPARC_BUILTIN_MAX
11023 };
11024
11025 static GTY (()) tree sparc_builtins[(int) SPARC_BUILTIN_MAX];
11026 static enum insn_code sparc_builtins_icode[(int) SPARC_BUILTIN_MAX];
11027
11028 /* Return true if OPVAL can be used for operand OPNUM of instruction ICODE.
11029 The instruction should require a constant operand of some sort. The
11030 function prints an error if OPVAL is not valid. */
11031
11032 static int
11033 check_constant_argument (enum insn_code icode, int opnum, rtx opval)
11034 {
11035 if (GET_CODE (opval) != CONST_INT)
11036 {
11037 error ("%qs expects a constant argument", insn_data[icode].name);
11038 return false;
11039 }
11040
11041 if (!(*insn_data[icode].operand[opnum].predicate) (opval, VOIDmode))
11042 {
11043 error ("constant argument out of range for %qs", insn_data[icode].name);
11044 return false;
11045 }
11046 return true;
11047 }
11048
11049 /* Add a SPARC builtin function with NAME, ICODE, CODE and TYPE. Return the
11050 function decl or NULL_TREE if the builtin was not added. */
11051
11052 static tree
11053 def_builtin (const char *name, enum insn_code icode, enum sparc_builtins code,
11054 tree type)
11055 {
11056 tree t
11057 = add_builtin_function (name, type, code, BUILT_IN_MD, NULL, NULL_TREE);
11058
11059 if (t)
11060 {
11061 sparc_builtins[code] = t;
11062 sparc_builtins_icode[code] = icode;
11063 }
11064
11065 return t;
11066 }
11067
11068 /* Likewise, but also marks the function as "const". */
11069
11070 static tree
11071 def_builtin_const (const char *name, enum insn_code icode,
11072 enum sparc_builtins code, tree type)
11073 {
11074 tree t = def_builtin (name, icode, code, type);
11075
11076 if (t)
11077 TREE_READONLY (t) = 1;
11078
11079 return t;
11080 }
11081
11082 /* Implement the TARGET_INIT_BUILTINS target hook.
11083 Create builtin functions for special SPARC instructions. */
11084
11085 static void
11086 sparc_init_builtins (void)
11087 {
11088 if (TARGET_FPU)
11089 sparc_fpu_init_builtins ();
11090
11091 if (TARGET_VIS)
11092 sparc_vis_init_builtins ();
11093 }
11094
11095 /* Create builtin functions for FPU instructions. */
11096
11097 static void
11098 sparc_fpu_init_builtins (void)
11099 {
11100 tree ftype
11101 = build_function_type_list (void_type_node,
11102 build_pointer_type (unsigned_type_node), 0);
11103 def_builtin ("__builtin_load_fsr", CODE_FOR_ldfsr,
11104 SPARC_BUILTIN_LDFSR, ftype);
11105 def_builtin ("__builtin_store_fsr", CODE_FOR_stfsr,
11106 SPARC_BUILTIN_STFSR, ftype);
11107 }
11108
11109 /* Create builtin functions for VIS instructions. */
11110
11111 static void
11112 sparc_vis_init_builtins (void)
11113 {
11114 tree v4qi = build_vector_type (unsigned_intQI_type_node, 4);
11115 tree v8qi = build_vector_type (unsigned_intQI_type_node, 8);
11116 tree v4hi = build_vector_type (intHI_type_node, 4);
11117 tree v2hi = build_vector_type (intHI_type_node, 2);
11118 tree v2si = build_vector_type (intSI_type_node, 2);
11119 tree v1si = build_vector_type (intSI_type_node, 1);
11120
11121 tree v4qi_ftype_v4hi = build_function_type_list (v4qi, v4hi, 0);
11122 tree v8qi_ftype_v2si_v8qi = build_function_type_list (v8qi, v2si, v8qi, 0);
11123 tree v2hi_ftype_v2si = build_function_type_list (v2hi, v2si, 0);
11124 tree v4hi_ftype_v4qi = build_function_type_list (v4hi, v4qi, 0);
11125 tree v8qi_ftype_v4qi_v4qi = build_function_type_list (v8qi, v4qi, v4qi, 0);
11126 tree v4hi_ftype_v4qi_v4hi = build_function_type_list (v4hi, v4qi, v4hi, 0);
11127 tree v4hi_ftype_v4qi_v2hi = build_function_type_list (v4hi, v4qi, v2hi, 0);
11128 tree v2si_ftype_v4qi_v2hi = build_function_type_list (v2si, v4qi, v2hi, 0);
11129 tree v4hi_ftype_v8qi_v4hi = build_function_type_list (v4hi, v8qi, v4hi, 0);
11130 tree v4hi_ftype_v4hi_v4hi = build_function_type_list (v4hi, v4hi, v4hi, 0);
11131 tree v2si_ftype_v2si_v2si = build_function_type_list (v2si, v2si, v2si, 0);
11132 tree v8qi_ftype_v8qi_v8qi = build_function_type_list (v8qi, v8qi, v8qi, 0);
11133 tree v2hi_ftype_v2hi_v2hi = build_function_type_list (v2hi, v2hi, v2hi, 0);
11134 tree v1si_ftype_v1si_v1si = build_function_type_list (v1si, v1si, v1si, 0);
11135 tree di_ftype_v8qi_v8qi_di = build_function_type_list (intDI_type_node,
11136 v8qi, v8qi,
11137 intDI_type_node, 0);
11138 tree di_ftype_v8qi_v8qi = build_function_type_list (intDI_type_node,
11139 v8qi, v8qi, 0);
11140 tree si_ftype_v8qi_v8qi = build_function_type_list (intSI_type_node,
11141 v8qi, v8qi, 0);
11142 tree v8qi_ftype_df_si = build_function_type_list (v8qi, double_type_node,
11143 intSI_type_node, 0);
11144 tree v4hi_ftype_df_si = build_function_type_list (v4hi, double_type_node,
11145 intSI_type_node, 0);
11146 tree v2si_ftype_df_si = build_function_type_list (v2si, double_type_node,
11147 intDI_type_node, 0);
11148 tree di_ftype_di_di = build_function_type_list (intDI_type_node,
11149 intDI_type_node,
11150 intDI_type_node, 0);
11151 tree si_ftype_si_si = build_function_type_list (intSI_type_node,
11152 intSI_type_node,
11153 intSI_type_node, 0);
11154 tree ptr_ftype_ptr_si = build_function_type_list (ptr_type_node,
11155 ptr_type_node,
11156 intSI_type_node, 0);
11157 tree ptr_ftype_ptr_di = build_function_type_list (ptr_type_node,
11158 ptr_type_node,
11159 intDI_type_node, 0);
11160 tree si_ftype_ptr_ptr = build_function_type_list (intSI_type_node,
11161 ptr_type_node,
11162 ptr_type_node, 0);
11163 tree di_ftype_ptr_ptr = build_function_type_list (intDI_type_node,
11164 ptr_type_node,
11165 ptr_type_node, 0);
11166 tree si_ftype_v4hi_v4hi = build_function_type_list (intSI_type_node,
11167 v4hi, v4hi, 0);
11168 tree si_ftype_v2si_v2si = build_function_type_list (intSI_type_node,
11169 v2si, v2si, 0);
11170 tree di_ftype_v4hi_v4hi = build_function_type_list (intDI_type_node,
11171 v4hi, v4hi, 0);
11172 tree di_ftype_v2si_v2si = build_function_type_list (intDI_type_node,
11173 v2si, v2si, 0);
11174 tree void_ftype_di = build_function_type_list (void_type_node,
11175 intDI_type_node, 0);
11176 tree di_ftype_void = build_function_type_list (intDI_type_node,
11177 void_type_node, 0);
11178 tree void_ftype_si = build_function_type_list (void_type_node,
11179 intSI_type_node, 0);
11180 tree sf_ftype_sf_sf = build_function_type_list (float_type_node,
11181 float_type_node,
11182 float_type_node, 0);
11183 tree df_ftype_df_df = build_function_type_list (double_type_node,
11184 double_type_node,
11185 double_type_node, 0);
11186
11187 /* Packing and expanding vectors. */
11188 def_builtin ("__builtin_vis_fpack16", CODE_FOR_fpack16_vis,
11189 SPARC_BUILTIN_FPACK16, v4qi_ftype_v4hi);
11190 def_builtin ("__builtin_vis_fpack32", CODE_FOR_fpack32_vis,
11191 SPARC_BUILTIN_FPACK32, v8qi_ftype_v2si_v8qi);
11192 def_builtin ("__builtin_vis_fpackfix", CODE_FOR_fpackfix_vis,
11193 SPARC_BUILTIN_FPACKFIX, v2hi_ftype_v2si);
11194 def_builtin_const ("__builtin_vis_fexpand", CODE_FOR_fexpand_vis,
11195 SPARC_BUILTIN_FEXPAND, v4hi_ftype_v4qi);
11196 def_builtin_const ("__builtin_vis_fpmerge", CODE_FOR_fpmerge_vis,
11197 SPARC_BUILTIN_FPMERGE, v8qi_ftype_v4qi_v4qi);
11198
11199 /* Multiplications. */
11200 def_builtin_const ("__builtin_vis_fmul8x16", CODE_FOR_fmul8x16_vis,
11201 SPARC_BUILTIN_FMUL8X16, v4hi_ftype_v4qi_v4hi);
11202 def_builtin_const ("__builtin_vis_fmul8x16au", CODE_FOR_fmul8x16au_vis,
11203 SPARC_BUILTIN_FMUL8X16AU, v4hi_ftype_v4qi_v2hi);
11204 def_builtin_const ("__builtin_vis_fmul8x16al", CODE_FOR_fmul8x16al_vis,
11205 SPARC_BUILTIN_FMUL8X16AL, v4hi_ftype_v4qi_v2hi);
11206 def_builtin_const ("__builtin_vis_fmul8sux16", CODE_FOR_fmul8sux16_vis,
11207 SPARC_BUILTIN_FMUL8SUX16, v4hi_ftype_v8qi_v4hi);
11208 def_builtin_const ("__builtin_vis_fmul8ulx16", CODE_FOR_fmul8ulx16_vis,
11209 SPARC_BUILTIN_FMUL8ULX16, v4hi_ftype_v8qi_v4hi);
11210 def_builtin_const ("__builtin_vis_fmuld8sux16", CODE_FOR_fmuld8sux16_vis,
11211 SPARC_BUILTIN_FMULD8SUX16, v2si_ftype_v4qi_v2hi);
11212 def_builtin_const ("__builtin_vis_fmuld8ulx16", CODE_FOR_fmuld8ulx16_vis,
11213 SPARC_BUILTIN_FMULD8ULX16, v2si_ftype_v4qi_v2hi);
11214
11215 /* Data aligning. */
11216 def_builtin ("__builtin_vis_faligndatav4hi", CODE_FOR_faligndatav4hi_vis,
11217 SPARC_BUILTIN_FALIGNDATAV4HI, v4hi_ftype_v4hi_v4hi);
11218 def_builtin ("__builtin_vis_faligndatav8qi", CODE_FOR_faligndatav8qi_vis,
11219 SPARC_BUILTIN_FALIGNDATAV8QI, v8qi_ftype_v8qi_v8qi);
11220 def_builtin ("__builtin_vis_faligndatav2si", CODE_FOR_faligndatav2si_vis,
11221 SPARC_BUILTIN_FALIGNDATAV2SI, v2si_ftype_v2si_v2si);
11222 def_builtin ("__builtin_vis_faligndatadi", CODE_FOR_faligndatav1di_vis,
11223 SPARC_BUILTIN_FALIGNDATADI, di_ftype_di_di);
11224
11225 def_builtin ("__builtin_vis_write_gsr", CODE_FOR_wrgsr_vis,
11226 SPARC_BUILTIN_WRGSR, void_ftype_di);
11227 def_builtin ("__builtin_vis_read_gsr", CODE_FOR_rdgsr_vis,
11228 SPARC_BUILTIN_RDGSR, di_ftype_void);
11229
11230 if (TARGET_ARCH64)
11231 {
11232 def_builtin ("__builtin_vis_alignaddr", CODE_FOR_alignaddrdi_vis,
11233 SPARC_BUILTIN_ALIGNADDR, ptr_ftype_ptr_di);
11234 def_builtin ("__builtin_vis_alignaddrl", CODE_FOR_alignaddrldi_vis,
11235 SPARC_BUILTIN_ALIGNADDRL, ptr_ftype_ptr_di);
11236 }
11237 else
11238 {
11239 def_builtin ("__builtin_vis_alignaddr", CODE_FOR_alignaddrsi_vis,
11240 SPARC_BUILTIN_ALIGNADDR, ptr_ftype_ptr_si);
11241 def_builtin ("__builtin_vis_alignaddrl", CODE_FOR_alignaddrlsi_vis,
11242 SPARC_BUILTIN_ALIGNADDRL, ptr_ftype_ptr_si);
11243 }
11244
11245 /* Pixel distance. */
11246 def_builtin_const ("__builtin_vis_pdist", CODE_FOR_pdist_vis,
11247 SPARC_BUILTIN_PDIST, di_ftype_v8qi_v8qi_di);
11248
11249 /* Edge handling. */
11250 if (TARGET_ARCH64)
11251 {
11252 def_builtin_const ("__builtin_vis_edge8", CODE_FOR_edge8di_vis,
11253 SPARC_BUILTIN_EDGE8, di_ftype_ptr_ptr);
11254 def_builtin_const ("__builtin_vis_edge8l", CODE_FOR_edge8ldi_vis,
11255 SPARC_BUILTIN_EDGE8L, di_ftype_ptr_ptr);
11256 def_builtin_const ("__builtin_vis_edge16", CODE_FOR_edge16di_vis,
11257 SPARC_BUILTIN_EDGE16, di_ftype_ptr_ptr);
11258 def_builtin_const ("__builtin_vis_edge16l", CODE_FOR_edge16ldi_vis,
11259 SPARC_BUILTIN_EDGE16L, di_ftype_ptr_ptr);
11260 def_builtin_const ("__builtin_vis_edge32", CODE_FOR_edge32di_vis,
11261 SPARC_BUILTIN_EDGE32, di_ftype_ptr_ptr);
11262 def_builtin_const ("__builtin_vis_edge32l", CODE_FOR_edge32ldi_vis,
11263 SPARC_BUILTIN_EDGE32L, di_ftype_ptr_ptr);
11264 }
11265 else
11266 {
11267 def_builtin_const ("__builtin_vis_edge8", CODE_FOR_edge8si_vis,
11268 SPARC_BUILTIN_EDGE8, si_ftype_ptr_ptr);
11269 def_builtin_const ("__builtin_vis_edge8l", CODE_FOR_edge8lsi_vis,
11270 SPARC_BUILTIN_EDGE8L, si_ftype_ptr_ptr);
11271 def_builtin_const ("__builtin_vis_edge16", CODE_FOR_edge16si_vis,
11272 SPARC_BUILTIN_EDGE16, si_ftype_ptr_ptr);
11273 def_builtin_const ("__builtin_vis_edge16l", CODE_FOR_edge16lsi_vis,
11274 SPARC_BUILTIN_EDGE16L, si_ftype_ptr_ptr);
11275 def_builtin_const ("__builtin_vis_edge32", CODE_FOR_edge32si_vis,
11276 SPARC_BUILTIN_EDGE32, si_ftype_ptr_ptr);
11277 def_builtin_const ("__builtin_vis_edge32l", CODE_FOR_edge32lsi_vis,
11278 SPARC_BUILTIN_EDGE32L, si_ftype_ptr_ptr);
11279 }
11280
11281 /* Pixel compare. */
11282 if (TARGET_ARCH64)
11283 {
11284 def_builtin_const ("__builtin_vis_fcmple16", CODE_FOR_fpcmple16di_vis,
11285 SPARC_BUILTIN_FCMPLE16, di_ftype_v4hi_v4hi);
11286 def_builtin_const ("__builtin_vis_fcmple32", CODE_FOR_fpcmple32di_vis,
11287 SPARC_BUILTIN_FCMPLE32, di_ftype_v2si_v2si);
11288 def_builtin_const ("__builtin_vis_fcmpne16", CODE_FOR_fpcmpne16di_vis,
11289 SPARC_BUILTIN_FCMPNE16, di_ftype_v4hi_v4hi);
11290 def_builtin_const ("__builtin_vis_fcmpne32", CODE_FOR_fpcmpne32di_vis,
11291 SPARC_BUILTIN_FCMPNE32, di_ftype_v2si_v2si);
11292 def_builtin_const ("__builtin_vis_fcmpgt16", CODE_FOR_fpcmpgt16di_vis,
11293 SPARC_BUILTIN_FCMPGT16, di_ftype_v4hi_v4hi);
11294 def_builtin_const ("__builtin_vis_fcmpgt32", CODE_FOR_fpcmpgt32di_vis,
11295 SPARC_BUILTIN_FCMPGT32, di_ftype_v2si_v2si);
11296 def_builtin_const ("__builtin_vis_fcmpeq16", CODE_FOR_fpcmpeq16di_vis,
11297 SPARC_BUILTIN_FCMPEQ16, di_ftype_v4hi_v4hi);
11298 def_builtin_const ("__builtin_vis_fcmpeq32", CODE_FOR_fpcmpeq32di_vis,
11299 SPARC_BUILTIN_FCMPEQ32, di_ftype_v2si_v2si);
11300 }
11301 else
11302 {
11303 def_builtin_const ("__builtin_vis_fcmple16", CODE_FOR_fpcmple16si_vis,
11304 SPARC_BUILTIN_FCMPLE16, si_ftype_v4hi_v4hi);
11305 def_builtin_const ("__builtin_vis_fcmple32", CODE_FOR_fpcmple32si_vis,
11306 SPARC_BUILTIN_FCMPLE32, si_ftype_v2si_v2si);
11307 def_builtin_const ("__builtin_vis_fcmpne16", CODE_FOR_fpcmpne16si_vis,
11308 SPARC_BUILTIN_FCMPNE16, si_ftype_v4hi_v4hi);
11309 def_builtin_const ("__builtin_vis_fcmpne32", CODE_FOR_fpcmpne32si_vis,
11310 SPARC_BUILTIN_FCMPNE32, si_ftype_v2si_v2si);
11311 def_builtin_const ("__builtin_vis_fcmpgt16", CODE_FOR_fpcmpgt16si_vis,
11312 SPARC_BUILTIN_FCMPGT16, si_ftype_v4hi_v4hi);
11313 def_builtin_const ("__builtin_vis_fcmpgt32", CODE_FOR_fpcmpgt32si_vis,
11314 SPARC_BUILTIN_FCMPGT32, si_ftype_v2si_v2si);
11315 def_builtin_const ("__builtin_vis_fcmpeq16", CODE_FOR_fpcmpeq16si_vis,
11316 SPARC_BUILTIN_FCMPEQ16, si_ftype_v4hi_v4hi);
11317 def_builtin_const ("__builtin_vis_fcmpeq32", CODE_FOR_fpcmpeq32si_vis,
11318 SPARC_BUILTIN_FCMPEQ32, si_ftype_v2si_v2si);
11319 }
11320
11321 /* Addition and subtraction. */
11322 def_builtin_const ("__builtin_vis_fpadd16", CODE_FOR_addv4hi3,
11323 SPARC_BUILTIN_FPADD16, v4hi_ftype_v4hi_v4hi);
11324 def_builtin_const ("__builtin_vis_fpadd16s", CODE_FOR_addv2hi3,
11325 SPARC_BUILTIN_FPADD16S, v2hi_ftype_v2hi_v2hi);
11326 def_builtin_const ("__builtin_vis_fpadd32", CODE_FOR_addv2si3,
11327 SPARC_BUILTIN_FPADD32, v2si_ftype_v2si_v2si);
11328 def_builtin_const ("__builtin_vis_fpadd32s", CODE_FOR_addv1si3,
11329 SPARC_BUILTIN_FPADD32S, v1si_ftype_v1si_v1si);
11330 def_builtin_const ("__builtin_vis_fpsub16", CODE_FOR_subv4hi3,
11331 SPARC_BUILTIN_FPSUB16, v4hi_ftype_v4hi_v4hi);
11332 def_builtin_const ("__builtin_vis_fpsub16s", CODE_FOR_subv2hi3,
11333 SPARC_BUILTIN_FPSUB16S, v2hi_ftype_v2hi_v2hi);
11334 def_builtin_const ("__builtin_vis_fpsub32", CODE_FOR_subv2si3,
11335 SPARC_BUILTIN_FPSUB32, v2si_ftype_v2si_v2si);
11336 def_builtin_const ("__builtin_vis_fpsub32s", CODE_FOR_subv1si3,
11337 SPARC_BUILTIN_FPSUB32S, v1si_ftype_v1si_v1si);
11338
11339 /* Three-dimensional array addressing. */
11340 if (TARGET_ARCH64)
11341 {
11342 def_builtin_const ("__builtin_vis_array8", CODE_FOR_array8di_vis,
11343 SPARC_BUILTIN_ARRAY8, di_ftype_di_di);
11344 def_builtin_const ("__builtin_vis_array16", CODE_FOR_array16di_vis,
11345 SPARC_BUILTIN_ARRAY16, di_ftype_di_di);
11346 def_builtin_const ("__builtin_vis_array32", CODE_FOR_array32di_vis,
11347 SPARC_BUILTIN_ARRAY32, di_ftype_di_di);
11348 }
11349 else
11350 {
11351 def_builtin_const ("__builtin_vis_array8", CODE_FOR_array8si_vis,
11352 SPARC_BUILTIN_ARRAY8, si_ftype_si_si);
11353 def_builtin_const ("__builtin_vis_array16", CODE_FOR_array16si_vis,
11354 SPARC_BUILTIN_ARRAY16, si_ftype_si_si);
11355 def_builtin_const ("__builtin_vis_array32", CODE_FOR_array32si_vis,
11356 SPARC_BUILTIN_ARRAY32, si_ftype_si_si);
11357 }
11358
11359 if (TARGET_VIS2)
11360 {
11361 /* Edge handling. */
11362 if (TARGET_ARCH64)
11363 {
11364 def_builtin_const ("__builtin_vis_edge8n", CODE_FOR_edge8ndi_vis,
11365 SPARC_BUILTIN_EDGE8N, di_ftype_ptr_ptr);
11366 def_builtin_const ("__builtin_vis_edge8ln", CODE_FOR_edge8lndi_vis,
11367 SPARC_BUILTIN_EDGE8LN, di_ftype_ptr_ptr);
11368 def_builtin_const ("__builtin_vis_edge16n", CODE_FOR_edge16ndi_vis,
11369 SPARC_BUILTIN_EDGE16N, di_ftype_ptr_ptr);
11370 def_builtin_const ("__builtin_vis_edge16ln", CODE_FOR_edge16lndi_vis,
11371 SPARC_BUILTIN_EDGE16LN, di_ftype_ptr_ptr);
11372 def_builtin_const ("__builtin_vis_edge32n", CODE_FOR_edge32ndi_vis,
11373 SPARC_BUILTIN_EDGE32N, di_ftype_ptr_ptr);
11374 def_builtin_const ("__builtin_vis_edge32ln", CODE_FOR_edge32lndi_vis,
11375 SPARC_BUILTIN_EDGE32LN, di_ftype_ptr_ptr);
11376 }
11377 else
11378 {
11379 def_builtin_const ("__builtin_vis_edge8n", CODE_FOR_edge8nsi_vis,
11380 SPARC_BUILTIN_EDGE8N, si_ftype_ptr_ptr);
11381 def_builtin_const ("__builtin_vis_edge8ln", CODE_FOR_edge8lnsi_vis,
11382 SPARC_BUILTIN_EDGE8LN, si_ftype_ptr_ptr);
11383 def_builtin_const ("__builtin_vis_edge16n", CODE_FOR_edge16nsi_vis,
11384 SPARC_BUILTIN_EDGE16N, si_ftype_ptr_ptr);
11385 def_builtin_const ("__builtin_vis_edge16ln", CODE_FOR_edge16lnsi_vis,
11386 SPARC_BUILTIN_EDGE16LN, si_ftype_ptr_ptr);
11387 def_builtin_const ("__builtin_vis_edge32n", CODE_FOR_edge32nsi_vis,
11388 SPARC_BUILTIN_EDGE32N, si_ftype_ptr_ptr);
11389 def_builtin_const ("__builtin_vis_edge32ln", CODE_FOR_edge32lnsi_vis,
11390 SPARC_BUILTIN_EDGE32LN, si_ftype_ptr_ptr);
11391 }
11392
11393 /* Byte mask and shuffle. */
11394 if (TARGET_ARCH64)
11395 def_builtin ("__builtin_vis_bmask", CODE_FOR_bmaskdi_vis,
11396 SPARC_BUILTIN_BMASK, di_ftype_di_di);
11397 else
11398 def_builtin ("__builtin_vis_bmask", CODE_FOR_bmasksi_vis,
11399 SPARC_BUILTIN_BMASK, si_ftype_si_si);
11400 def_builtin ("__builtin_vis_bshufflev4hi", CODE_FOR_bshufflev4hi_vis,
11401 SPARC_BUILTIN_BSHUFFLEV4HI, v4hi_ftype_v4hi_v4hi);
11402 def_builtin ("__builtin_vis_bshufflev8qi", CODE_FOR_bshufflev8qi_vis,
11403 SPARC_BUILTIN_BSHUFFLEV8QI, v8qi_ftype_v8qi_v8qi);
11404 def_builtin ("__builtin_vis_bshufflev2si", CODE_FOR_bshufflev2si_vis,
11405 SPARC_BUILTIN_BSHUFFLEV2SI, v2si_ftype_v2si_v2si);
11406 def_builtin ("__builtin_vis_bshuffledi", CODE_FOR_bshufflev1di_vis,
11407 SPARC_BUILTIN_BSHUFFLEDI, di_ftype_di_di);
11408 }
11409
11410 if (TARGET_VIS3)
11411 {
11412 if (TARGET_ARCH64)
11413 {
11414 def_builtin ("__builtin_vis_cmask8", CODE_FOR_cmask8di_vis,
11415 SPARC_BUILTIN_CMASK8, void_ftype_di);
11416 def_builtin ("__builtin_vis_cmask16", CODE_FOR_cmask16di_vis,
11417 SPARC_BUILTIN_CMASK16, void_ftype_di);
11418 def_builtin ("__builtin_vis_cmask32", CODE_FOR_cmask32di_vis,
11419 SPARC_BUILTIN_CMASK32, void_ftype_di);
11420 }
11421 else
11422 {
11423 def_builtin ("__builtin_vis_cmask8", CODE_FOR_cmask8si_vis,
11424 SPARC_BUILTIN_CMASK8, void_ftype_si);
11425 def_builtin ("__builtin_vis_cmask16", CODE_FOR_cmask16si_vis,
11426 SPARC_BUILTIN_CMASK16, void_ftype_si);
11427 def_builtin ("__builtin_vis_cmask32", CODE_FOR_cmask32si_vis,
11428 SPARC_BUILTIN_CMASK32, void_ftype_si);
11429 }
11430
11431 def_builtin_const ("__builtin_vis_fchksm16", CODE_FOR_fchksm16_vis,
11432 SPARC_BUILTIN_FCHKSM16, v4hi_ftype_v4hi_v4hi);
11433
11434 def_builtin_const ("__builtin_vis_fsll16", CODE_FOR_vashlv4hi3,
11435 SPARC_BUILTIN_FSLL16, v4hi_ftype_v4hi_v4hi);
11436 def_builtin_const ("__builtin_vis_fslas16", CODE_FOR_vssashlv4hi3,
11437 SPARC_BUILTIN_FSLAS16, v4hi_ftype_v4hi_v4hi);
11438 def_builtin_const ("__builtin_vis_fsrl16", CODE_FOR_vlshrv4hi3,
11439 SPARC_BUILTIN_FSRL16, v4hi_ftype_v4hi_v4hi);
11440 def_builtin_const ("__builtin_vis_fsra16", CODE_FOR_vashrv4hi3,
11441 SPARC_BUILTIN_FSRA16, v4hi_ftype_v4hi_v4hi);
11442 def_builtin_const ("__builtin_vis_fsll32", CODE_FOR_vashlv2si3,
11443 SPARC_BUILTIN_FSLL32, v2si_ftype_v2si_v2si);
11444 def_builtin_const ("__builtin_vis_fslas32", CODE_FOR_vssashlv2si3,
11445 SPARC_BUILTIN_FSLAS32, v2si_ftype_v2si_v2si);
11446 def_builtin_const ("__builtin_vis_fsrl32", CODE_FOR_vlshrv2si3,
11447 SPARC_BUILTIN_FSRL32, v2si_ftype_v2si_v2si);
11448 def_builtin_const ("__builtin_vis_fsra32", CODE_FOR_vashrv2si3,
11449 SPARC_BUILTIN_FSRA32, v2si_ftype_v2si_v2si);
11450
11451 if (TARGET_ARCH64)
11452 def_builtin_const ("__builtin_vis_pdistn", CODE_FOR_pdistndi_vis,
11453 SPARC_BUILTIN_PDISTN, di_ftype_v8qi_v8qi);
11454 else
11455 def_builtin_const ("__builtin_vis_pdistn", CODE_FOR_pdistnsi_vis,
11456 SPARC_BUILTIN_PDISTN, si_ftype_v8qi_v8qi);
11457
11458 def_builtin_const ("__builtin_vis_fmean16", CODE_FOR_fmean16_vis,
11459 SPARC_BUILTIN_FMEAN16, v4hi_ftype_v4hi_v4hi);
11460
11461 def_builtin_const ("__builtin_vis_fpadds16", CODE_FOR_ssaddv4hi3,
11462 SPARC_BUILTIN_FPADDS16, v4hi_ftype_v4hi_v4hi);
11463 def_builtin_const ("__builtin_vis_fpadds16s", CODE_FOR_ssaddv2hi3,
11464 SPARC_BUILTIN_FPADDS16S, v2hi_ftype_v2hi_v2hi);
11465 def_builtin_const ("__builtin_vis_fpsubs16", CODE_FOR_sssubv4hi3,
11466 SPARC_BUILTIN_FPSUBS16, v4hi_ftype_v4hi_v4hi);
11467 def_builtin_const ("__builtin_vis_fpsubs16s", CODE_FOR_sssubv2hi3,
11468 SPARC_BUILTIN_FPSUBS16S, v2hi_ftype_v2hi_v2hi);
11469 def_builtin_const ("__builtin_vis_fpadds32", CODE_FOR_ssaddv2si3,
11470 SPARC_BUILTIN_FPADDS32, v2si_ftype_v2si_v2si);
11471 def_builtin_const ("__builtin_vis_fpadds32s", CODE_FOR_ssaddv1si3,
11472 SPARC_BUILTIN_FPADDS32S, v1si_ftype_v1si_v1si);
11473 def_builtin_const ("__builtin_vis_fpsubs32", CODE_FOR_sssubv2si3,
11474 SPARC_BUILTIN_FPSUBS32, v2si_ftype_v2si_v2si);
11475 def_builtin_const ("__builtin_vis_fpsubs32s", CODE_FOR_sssubv1si3,
11476 SPARC_BUILTIN_FPSUBS32S, v1si_ftype_v1si_v1si);
11477
11478 def_builtin_const ("__builtin_vis_fhadds", CODE_FOR_fhaddsf_vis,
11479 SPARC_BUILTIN_FHADDS, sf_ftype_sf_sf);
11480 def_builtin_const ("__builtin_vis_fhaddd", CODE_FOR_fhadddf_vis,
11481 SPARC_BUILTIN_FHADDD, df_ftype_df_df);
11482 def_builtin_const ("__builtin_vis_fhsubs", CODE_FOR_fhsubsf_vis,
11483 SPARC_BUILTIN_FHSUBS, sf_ftype_sf_sf);
11484 def_builtin_const ("__builtin_vis_fhsubd", CODE_FOR_fhsubdf_vis,
11485 SPARC_BUILTIN_FHSUBD, df_ftype_df_df);
11486 def_builtin_const ("__builtin_vis_fnhadds", CODE_FOR_fnhaddsf_vis,
11487 SPARC_BUILTIN_FNHADDS, sf_ftype_sf_sf);
11488 def_builtin_const ("__builtin_vis_fnhaddd", CODE_FOR_fnhadddf_vis,
11489 SPARC_BUILTIN_FNHADDD, df_ftype_df_df);
11490
11491 def_builtin_const ("__builtin_vis_umulxhi", CODE_FOR_umulxhi_vis,
11492 SPARC_BUILTIN_UMULXHI, di_ftype_di_di);
11493 def_builtin_const ("__builtin_vis_xmulx", CODE_FOR_xmulx_vis,
11494 SPARC_BUILTIN_XMULX, di_ftype_di_di);
11495 def_builtin_const ("__builtin_vis_xmulxhi", CODE_FOR_xmulxhi_vis,
11496 SPARC_BUILTIN_XMULXHI, di_ftype_di_di);
11497 }
11498
11499 if (TARGET_VIS3B)
11500 {
11501 def_builtin_const ("__builtin_vis_fpadd64", CODE_FOR_fpadd64_vis,
11502 SPARC_BUILTIN_FPADD64, di_ftype_di_di);
11503 def_builtin_const ("__builtin_vis_fpsub64", CODE_FOR_fpsub64_vis,
11504 SPARC_BUILTIN_FPSUB64, di_ftype_di_di);
11505
11506 if (TARGET_ARCH64)
11507 {
11508 def_builtin_const ("__builtin_vis_fucmple8", CODE_FOR_fpcmpule8di_vis,
11509 SPARC_BUILTIN_FUCMPLE8, di_ftype_v8qi_v8qi);
11510 def_builtin_const ("__builtin_vis_fucmpne8", CODE_FOR_fpcmpne8di_vis,
11511 SPARC_BUILTIN_FUCMPNE8, di_ftype_v8qi_v8qi);
11512 def_builtin_const ("__builtin_vis_fucmpgt8", CODE_FOR_fpcmpugt8di_vis,
11513 SPARC_BUILTIN_FUCMPGT8, di_ftype_v8qi_v8qi);
11514 def_builtin_const ("__builtin_vis_fucmpeq8", CODE_FOR_fpcmpeq8di_vis,
11515 SPARC_BUILTIN_FUCMPEQ8, di_ftype_v8qi_v8qi);
11516 }
11517 else
11518 {
11519 def_builtin_const ("__builtin_vis_fucmple8", CODE_FOR_fpcmpule8si_vis,
11520 SPARC_BUILTIN_FUCMPLE8, si_ftype_v8qi_v8qi);
11521 def_builtin_const ("__builtin_vis_fucmpne8", CODE_FOR_fpcmpne8si_vis,
11522 SPARC_BUILTIN_FUCMPNE8, si_ftype_v8qi_v8qi);
11523 def_builtin_const ("__builtin_vis_fucmpgt8", CODE_FOR_fpcmpugt8si_vis,
11524 SPARC_BUILTIN_FUCMPGT8, si_ftype_v8qi_v8qi);
11525 def_builtin_const ("__builtin_vis_fucmpeq8", CODE_FOR_fpcmpeq8si_vis,
11526 SPARC_BUILTIN_FUCMPEQ8, si_ftype_v8qi_v8qi);
11527 }
11528 }
11529
11530 if (TARGET_VIS4)
11531 {
11532 def_builtin_const ("__builtin_vis_fpadd8", CODE_FOR_addv8qi3,
11533 SPARC_BUILTIN_FPADD8, v8qi_ftype_v8qi_v8qi);
11534 def_builtin_const ("__builtin_vis_fpadds8", CODE_FOR_ssaddv8qi3,
11535 SPARC_BUILTIN_FPADDS8, v8qi_ftype_v8qi_v8qi);
11536 def_builtin_const ("__builtin_vis_fpaddus8", CODE_FOR_usaddv8qi3,
11537 SPARC_BUILTIN_FPADDUS8, v8qi_ftype_v8qi_v8qi);
11538 def_builtin_const ("__builtin_vis_fpaddus16", CODE_FOR_usaddv4hi3,
11539 SPARC_BUILTIN_FPADDUS16, v4hi_ftype_v4hi_v4hi);
11540
11541
11542 if (TARGET_ARCH64)
11543 {
11544 def_builtin_const ("__builtin_vis_fpcmple8", CODE_FOR_fpcmple8di_vis,
11545 SPARC_BUILTIN_FPCMPLE8, di_ftype_v8qi_v8qi);
11546 def_builtin_const ("__builtin_vis_fpcmpgt8", CODE_FOR_fpcmpgt8di_vis,
11547 SPARC_BUILTIN_FPCMPGT8, di_ftype_v8qi_v8qi);
11548 def_builtin_const ("__builtin_vis_fpcmpule16", CODE_FOR_fpcmpule16di_vis,
11549 SPARC_BUILTIN_FPCMPULE16, di_ftype_v4hi_v4hi);
11550 def_builtin_const ("__builtin_vis_fpcmpugt16", CODE_FOR_fpcmpugt16di_vis,
11551 SPARC_BUILTIN_FPCMPUGT16, di_ftype_v4hi_v4hi);
11552 def_builtin_const ("__builtin_vis_fpcmpule32", CODE_FOR_fpcmpule32di_vis,
11553 SPARC_BUILTIN_FPCMPULE32, di_ftype_v2si_v2si);
11554 def_builtin_const ("__builtin_vis_fpcmpugt32", CODE_FOR_fpcmpugt32di_vis,
11555 SPARC_BUILTIN_FPCMPUGT32, di_ftype_v2si_v2si);
11556 }
11557 else
11558 {
11559 def_builtin_const ("__builtin_vis_fpcmple8", CODE_FOR_fpcmple8si_vis,
11560 SPARC_BUILTIN_FPCMPLE8, si_ftype_v8qi_v8qi);
11561 def_builtin_const ("__builtin_vis_fpcmpgt8", CODE_FOR_fpcmpgt8si_vis,
11562 SPARC_BUILTIN_FPCMPGT8, si_ftype_v8qi_v8qi);
11563 def_builtin_const ("__builtin_vis_fpcmpule16", CODE_FOR_fpcmpule16si_vis,
11564 SPARC_BUILTIN_FPCMPULE16, si_ftype_v4hi_v4hi);
11565 def_builtin_const ("__builtin_vis_fpcmpugt16", CODE_FOR_fpcmpugt16si_vis,
11566 SPARC_BUILTIN_FPCMPUGT16, si_ftype_v4hi_v4hi);
11567 def_builtin_const ("__builtin_vis_fpcmpule32", CODE_FOR_fpcmpule32si_vis,
11568 SPARC_BUILTIN_FPCMPULE32, di_ftype_v2si_v2si);
11569 def_builtin_const ("__builtin_vis_fpcmpugt32", CODE_FOR_fpcmpugt32si_vis,
11570 SPARC_BUILTIN_FPCMPUGT32, di_ftype_v2si_v2si);
11571 }
11572
11573 def_builtin_const ("__builtin_vis_fpmax8", CODE_FOR_maxv8qi3,
11574 SPARC_BUILTIN_FPMAX8, v8qi_ftype_v8qi_v8qi);
11575 def_builtin_const ("__builtin_vis_fpmax16", CODE_FOR_maxv4hi3,
11576 SPARC_BUILTIN_FPMAX16, v4hi_ftype_v4hi_v4hi);
11577 def_builtin_const ("__builtin_vis_fpmax32", CODE_FOR_maxv2si3,
11578 SPARC_BUILTIN_FPMAX32, v2si_ftype_v2si_v2si);
11579 def_builtin_const ("__builtin_vis_fpmaxu8", CODE_FOR_maxuv8qi3,
11580 SPARC_BUILTIN_FPMAXU8, v8qi_ftype_v8qi_v8qi);
11581 def_builtin_const ("__builtin_vis_fpmaxu16", CODE_FOR_maxuv4hi3,
11582 SPARC_BUILTIN_FPMAXU16, v4hi_ftype_v4hi_v4hi);
11583 def_builtin_const ("__builtin_vis_fpmaxu32", CODE_FOR_maxuv2si3,
11584 SPARC_BUILTIN_FPMAXU32, v2si_ftype_v2si_v2si);
11585 def_builtin_const ("__builtin_vis_fpmin8", CODE_FOR_minv8qi3,
11586 SPARC_BUILTIN_FPMIN8, v8qi_ftype_v8qi_v8qi);
11587 def_builtin_const ("__builtin_vis_fpmin16", CODE_FOR_minv4hi3,
11588 SPARC_BUILTIN_FPMIN16, v4hi_ftype_v4hi_v4hi);
11589 def_builtin_const ("__builtin_vis_fpmin32", CODE_FOR_minv2si3,
11590 SPARC_BUILTIN_FPMIN32, v2si_ftype_v2si_v2si);
11591 def_builtin_const ("__builtin_vis_fpminu8", CODE_FOR_minuv8qi3,
11592 SPARC_BUILTIN_FPMINU8, v8qi_ftype_v8qi_v8qi);
11593 def_builtin_const ("__builtin_vis_fpminu16", CODE_FOR_minuv4hi3,
11594 SPARC_BUILTIN_FPMINU16, v4hi_ftype_v4hi_v4hi);
11595 def_builtin_const ("__builtin_vis_fpminu32", CODE_FOR_minuv2si3,
11596 SPARC_BUILTIN_FPMINU32, v2si_ftype_v2si_v2si);
11597 def_builtin_const ("__builtin_vis_fpsub8", CODE_FOR_subv8qi3,
11598 SPARC_BUILTIN_FPSUB8, v8qi_ftype_v8qi_v8qi);
11599 def_builtin_const ("__builtin_vis_fpsubs8", CODE_FOR_sssubv8qi3,
11600 SPARC_BUILTIN_FPSUBS8, v8qi_ftype_v8qi_v8qi);
11601 def_builtin_const ("__builtin_vis_fpsubus8", CODE_FOR_ussubv8qi3,
11602 SPARC_BUILTIN_FPSUBUS8, v8qi_ftype_v8qi_v8qi);
11603 def_builtin_const ("__builtin_vis_fpsubus16", CODE_FOR_ussubv4hi3,
11604 SPARC_BUILTIN_FPSUBUS16, v4hi_ftype_v4hi_v4hi);
11605 }
11606
11607 if (TARGET_VIS4B)
11608 {
11609 def_builtin_const ("__builtin_vis_dictunpack8", CODE_FOR_dictunpack8,
11610 SPARC_BUILTIN_DICTUNPACK8, v8qi_ftype_df_si);
11611 def_builtin_const ("__builtin_vis_dictunpack16", CODE_FOR_dictunpack16,
11612 SPARC_BUILTIN_DICTUNPACK16, v4hi_ftype_df_si);
11613 def_builtin_const ("__builtin_vis_dictunpack32", CODE_FOR_dictunpack32,
11614 SPARC_BUILTIN_DICTUNPACK32, v2si_ftype_df_si);
11615
11616 if (TARGET_ARCH64)
11617 {
11618 tree di_ftype_v8qi_v8qi_si = build_function_type_list (intDI_type_node,
11619 v8qi, v8qi,
11620 intSI_type_node, 0);
11621 tree di_ftype_v4hi_v4hi_si = build_function_type_list (intDI_type_node,
11622 v4hi, v4hi,
11623 intSI_type_node, 0);
11624 tree di_ftype_v2si_v2si_si = build_function_type_list (intDI_type_node,
11625 v2si, v2si,
11626 intSI_type_node, 0);
11627
11628 def_builtin_const ("__builtin_vis_fpcmple8shl", CODE_FOR_fpcmple8dishl,
11629 SPARC_BUILTIN_FPCMPLE8SHL, di_ftype_v8qi_v8qi_si);
11630 def_builtin_const ("__builtin_vis_fpcmpgt8shl", CODE_FOR_fpcmpgt8dishl,
11631 SPARC_BUILTIN_FPCMPGT8SHL, di_ftype_v8qi_v8qi_si);
11632 def_builtin_const ("__builtin_vis_fpcmpeq8shl", CODE_FOR_fpcmpeq8dishl,
11633 SPARC_BUILTIN_FPCMPEQ8SHL, di_ftype_v8qi_v8qi_si);
11634 def_builtin_const ("__builtin_vis_fpcmpne8shl", CODE_FOR_fpcmpne8dishl,
11635 SPARC_BUILTIN_FPCMPNE8SHL, di_ftype_v8qi_v8qi_si);
11636
11637 def_builtin_const ("__builtin_vis_fpcmple16shl", CODE_FOR_fpcmple16dishl,
11638 SPARC_BUILTIN_FPCMPLE16SHL, di_ftype_v4hi_v4hi_si);
11639 def_builtin_const ("__builtin_vis_fpcmpgt16shl", CODE_FOR_fpcmpgt16dishl,
11640 SPARC_BUILTIN_FPCMPGT16SHL, di_ftype_v4hi_v4hi_si);
11641 def_builtin_const ("__builtin_vis_fpcmpeq16shl", CODE_FOR_fpcmpeq16dishl,
11642 SPARC_BUILTIN_FPCMPEQ16SHL, di_ftype_v4hi_v4hi_si);
11643 def_builtin_const ("__builtin_vis_fpcmpne16shl", CODE_FOR_fpcmpne16dishl,
11644 SPARC_BUILTIN_FPCMPNE16SHL, di_ftype_v4hi_v4hi_si);
11645
11646 def_builtin_const ("__builtin_vis_fpcmple32shl", CODE_FOR_fpcmple32dishl,
11647 SPARC_BUILTIN_FPCMPLE32SHL, di_ftype_v2si_v2si_si);
11648 def_builtin_const ("__builtin_vis_fpcmpgt32shl", CODE_FOR_fpcmpgt32dishl,
11649 SPARC_BUILTIN_FPCMPGT32SHL, di_ftype_v2si_v2si_si);
11650 def_builtin_const ("__builtin_vis_fpcmpeq32shl", CODE_FOR_fpcmpeq32dishl,
11651 SPARC_BUILTIN_FPCMPEQ32SHL, di_ftype_v2si_v2si_si);
11652 def_builtin_const ("__builtin_vis_fpcmpne32shl", CODE_FOR_fpcmpne32dishl,
11653 SPARC_BUILTIN_FPCMPNE32SHL, di_ftype_v2si_v2si_si);
11654
11655
11656 def_builtin_const ("__builtin_vis_fpcmpule8shl", CODE_FOR_fpcmpule8dishl,
11657 SPARC_BUILTIN_FPCMPULE8SHL, di_ftype_v8qi_v8qi_si);
11658 def_builtin_const ("__builtin_vis_fpcmpugt8shl", CODE_FOR_fpcmpugt8dishl,
11659 SPARC_BUILTIN_FPCMPUGT8SHL, di_ftype_v8qi_v8qi_si);
11660
11661 def_builtin_const ("__builtin_vis_fpcmpule16shl", CODE_FOR_fpcmpule16dishl,
11662 SPARC_BUILTIN_FPCMPULE16SHL, di_ftype_v4hi_v4hi_si);
11663 def_builtin_const ("__builtin_vis_fpcmpugt16shl", CODE_FOR_fpcmpugt16dishl,
11664 SPARC_BUILTIN_FPCMPUGT16SHL, di_ftype_v4hi_v4hi_si);
11665
11666 def_builtin_const ("__builtin_vis_fpcmpule32shl", CODE_FOR_fpcmpule32dishl,
11667 SPARC_BUILTIN_FPCMPULE32SHL, di_ftype_v2si_v2si_si);
11668 def_builtin_const ("__builtin_vis_fpcmpugt32shl", CODE_FOR_fpcmpugt32dishl,
11669 SPARC_BUILTIN_FPCMPUGT32SHL, di_ftype_v2si_v2si_si);
11670
11671 def_builtin_const ("__builtin_vis_fpcmpde8shl", CODE_FOR_fpcmpde8dishl,
11672 SPARC_BUILTIN_FPCMPDE8SHL, di_ftype_v8qi_v8qi_si);
11673 def_builtin_const ("__builtin_vis_fpcmpde16shl", CODE_FOR_fpcmpde16dishl,
11674 SPARC_BUILTIN_FPCMPDE16SHL, di_ftype_v4hi_v4hi_si);
11675 def_builtin_const ("__builtin_vis_fpcmpde32shl", CODE_FOR_fpcmpde32dishl,
11676 SPARC_BUILTIN_FPCMPDE32SHL, di_ftype_v2si_v2si_si);
11677
11678 def_builtin_const ("__builtin_vis_fpcmpur8shl", CODE_FOR_fpcmpur8dishl,
11679 SPARC_BUILTIN_FPCMPUR8SHL, di_ftype_v8qi_v8qi_si);
11680 def_builtin_const ("__builtin_vis_fpcmpur16shl", CODE_FOR_fpcmpur16dishl,
11681 SPARC_BUILTIN_FPCMPUR16SHL, di_ftype_v4hi_v4hi_si);
11682 def_builtin_const ("__builtin_vis_fpcmpur32shl", CODE_FOR_fpcmpur32dishl,
11683 SPARC_BUILTIN_FPCMPUR32SHL, di_ftype_v2si_v2si_si);
11684
11685 }
11686 else
11687 {
11688 tree si_ftype_v8qi_v8qi_si = build_function_type_list (intSI_type_node,
11689 v8qi, v8qi,
11690 intSI_type_node, 0);
11691 tree si_ftype_v4hi_v4hi_si = build_function_type_list (intSI_type_node,
11692 v4hi, v4hi,
11693 intSI_type_node, 0);
11694 tree si_ftype_v2si_v2si_si = build_function_type_list (intSI_type_node,
11695 v2si, v2si,
11696 intSI_type_node, 0);
11697
11698 def_builtin_const ("__builtin_vis_fpcmple8shl", CODE_FOR_fpcmple8sishl,
11699 SPARC_BUILTIN_FPCMPLE8SHL, si_ftype_v8qi_v8qi_si);
11700 def_builtin_const ("__builtin_vis_fpcmpgt8shl", CODE_FOR_fpcmpgt8sishl,
11701 SPARC_BUILTIN_FPCMPGT8SHL, si_ftype_v8qi_v8qi_si);
11702 def_builtin_const ("__builtin_vis_fpcmpeq8shl", CODE_FOR_fpcmpeq8sishl,
11703 SPARC_BUILTIN_FPCMPEQ8SHL, si_ftype_v8qi_v8qi_si);
11704 def_builtin_const ("__builtin_vis_fpcmpne8shl", CODE_FOR_fpcmpne8sishl,
11705 SPARC_BUILTIN_FPCMPNE8SHL, si_ftype_v8qi_v8qi_si);
11706
11707 def_builtin_const ("__builtin_vis_fpcmple16shl", CODE_FOR_fpcmple16sishl,
11708 SPARC_BUILTIN_FPCMPLE16SHL, si_ftype_v4hi_v4hi_si);
11709 def_builtin_const ("__builtin_vis_fpcmpgt16shl", CODE_FOR_fpcmpgt16sishl,
11710 SPARC_BUILTIN_FPCMPGT16SHL, si_ftype_v4hi_v4hi_si);
11711 def_builtin_const ("__builtin_vis_fpcmpeq16shl", CODE_FOR_fpcmpeq16sishl,
11712 SPARC_BUILTIN_FPCMPEQ16SHL, si_ftype_v4hi_v4hi_si);
11713 def_builtin_const ("__builtin_vis_fpcmpne16shl", CODE_FOR_fpcmpne16sishl,
11714 SPARC_BUILTIN_FPCMPNE16SHL, si_ftype_v4hi_v4hi_si);
11715
11716 def_builtin_const ("__builtin_vis_fpcmple32shl", CODE_FOR_fpcmple32sishl,
11717 SPARC_BUILTIN_FPCMPLE32SHL, si_ftype_v2si_v2si_si);
11718 def_builtin_const ("__builtin_vis_fpcmpgt32shl", CODE_FOR_fpcmpgt32sishl,
11719 SPARC_BUILTIN_FPCMPGT32SHL, si_ftype_v2si_v2si_si);
11720 def_builtin_const ("__builtin_vis_fpcmpeq32shl", CODE_FOR_fpcmpeq32sishl,
11721 SPARC_BUILTIN_FPCMPEQ32SHL, si_ftype_v2si_v2si_si);
11722 def_builtin_const ("__builtin_vis_fpcmpne32shl", CODE_FOR_fpcmpne32sishl,
11723 SPARC_BUILTIN_FPCMPNE32SHL, si_ftype_v2si_v2si_si);
11724
11725
11726 def_builtin_const ("__builtin_vis_fpcmpule8shl", CODE_FOR_fpcmpule8sishl,
11727 SPARC_BUILTIN_FPCMPULE8SHL, si_ftype_v8qi_v8qi_si);
11728 def_builtin_const ("__builtin_vis_fpcmpugt8shl", CODE_FOR_fpcmpugt8sishl,
11729 SPARC_BUILTIN_FPCMPUGT8SHL, si_ftype_v8qi_v8qi_si);
11730
11731 def_builtin_const ("__builtin_vis_fpcmpule16shl", CODE_FOR_fpcmpule16sishl,
11732 SPARC_BUILTIN_FPCMPULE16SHL, si_ftype_v4hi_v4hi_si);
11733 def_builtin_const ("__builtin_vis_fpcmpugt16shl", CODE_FOR_fpcmpugt16sishl,
11734 SPARC_BUILTIN_FPCMPUGT16SHL, si_ftype_v4hi_v4hi_si);
11735
11736 def_builtin_const ("__builtin_vis_fpcmpule32shl", CODE_FOR_fpcmpule32sishl,
11737 SPARC_BUILTIN_FPCMPULE32SHL, si_ftype_v2si_v2si_si);
11738 def_builtin_const ("__builtin_vis_fpcmpugt32shl", CODE_FOR_fpcmpugt32sishl,
11739 SPARC_BUILTIN_FPCMPUGT32SHL, si_ftype_v2si_v2si_si);
11740
11741 def_builtin_const ("__builtin_vis_fpcmpde8shl", CODE_FOR_fpcmpde8sishl,
11742 SPARC_BUILTIN_FPCMPDE8SHL, si_ftype_v8qi_v8qi_si);
11743 def_builtin_const ("__builtin_vis_fpcmpde16shl", CODE_FOR_fpcmpde16sishl,
11744 SPARC_BUILTIN_FPCMPDE16SHL, si_ftype_v4hi_v4hi_si);
11745 def_builtin_const ("__builtin_vis_fpcmpde32shl", CODE_FOR_fpcmpde32sishl,
11746 SPARC_BUILTIN_FPCMPDE32SHL, si_ftype_v2si_v2si_si);
11747
11748 def_builtin_const ("__builtin_vis_fpcmpur8shl", CODE_FOR_fpcmpur8sishl,
11749 SPARC_BUILTIN_FPCMPUR8SHL, si_ftype_v8qi_v8qi_si);
11750 def_builtin_const ("__builtin_vis_fpcmpur16shl", CODE_FOR_fpcmpur16sishl,
11751 SPARC_BUILTIN_FPCMPUR16SHL, si_ftype_v4hi_v4hi_si);
11752 def_builtin_const ("__builtin_vis_fpcmpur32shl", CODE_FOR_fpcmpur32sishl,
11753 SPARC_BUILTIN_FPCMPUR32SHL, si_ftype_v2si_v2si_si);
11754 }
11755 }
11756 }
11757
11758 /* Implement TARGET_BUILTIN_DECL hook. */
11759
11760 static tree
11761 sparc_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
11762 {
11763 if (code >= SPARC_BUILTIN_MAX)
11764 return error_mark_node;
11765
11766 return sparc_builtins[code];
11767 }
11768
11769 /* Implemented TARGET_EXPAND_BUILTIN hook. */
11770
11771 static rtx
11772 sparc_expand_builtin (tree exp, rtx target,
11773 rtx subtarget ATTRIBUTE_UNUSED,
11774 machine_mode tmode ATTRIBUTE_UNUSED,
11775 int ignore ATTRIBUTE_UNUSED)
11776 {
11777 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
11778 enum sparc_builtins code
11779 = (enum sparc_builtins) DECL_MD_FUNCTION_CODE (fndecl);
11780 enum insn_code icode = sparc_builtins_icode[code];
11781 bool nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
11782 call_expr_arg_iterator iter;
11783 int arg_count = 0;
11784 rtx pat, op[4];
11785 tree arg;
11786
11787 if (nonvoid)
11788 {
11789 machine_mode tmode = insn_data[icode].operand[0].mode;
11790 if (!target
11791 || GET_MODE (target) != tmode
11792 || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
11793 op[0] = gen_reg_rtx (tmode);
11794 else
11795 op[0] = target;
11796 }
11797 else
11798 op[0] = NULL_RTX;
11799
11800 FOR_EACH_CALL_EXPR_ARG (arg, iter, exp)
11801 {
11802 const struct insn_operand_data *insn_op;
11803 int idx;
11804
11805 if (arg == error_mark_node)
11806 return NULL_RTX;
11807
11808 arg_count++;
11809 idx = arg_count - !nonvoid;
11810 insn_op = &insn_data[icode].operand[idx];
11811 op[arg_count] = expand_normal (arg);
11812
11813 /* Some of the builtins require constant arguments. We check
11814 for this here. */
11815 if ((code >= SPARC_BUILTIN_FIRST_FPCMPSHL
11816 && code <= SPARC_BUILTIN_LAST_FPCMPSHL
11817 && arg_count == 3)
11818 || (code >= SPARC_BUILTIN_FIRST_DICTUNPACK
11819 && code <= SPARC_BUILTIN_LAST_DICTUNPACK
11820 && arg_count == 2))
11821 {
11822 if (!check_constant_argument (icode, idx, op[arg_count]))
11823 return const0_rtx;
11824 }
11825
11826 if (code == SPARC_BUILTIN_LDFSR || code == SPARC_BUILTIN_STFSR)
11827 {
11828 if (!address_operand (op[arg_count], SImode))
11829 {
11830 op[arg_count] = convert_memory_address (Pmode, op[arg_count]);
11831 op[arg_count] = copy_addr_to_reg (op[arg_count]);
11832 }
11833 op[arg_count] = gen_rtx_MEM (SImode, op[arg_count]);
11834 }
11835
11836 else if (insn_op->mode == V1DImode
11837 && GET_MODE (op[arg_count]) == DImode)
11838 op[arg_count] = gen_lowpart (V1DImode, op[arg_count]);
11839
11840 else if (insn_op->mode == V1SImode
11841 && GET_MODE (op[arg_count]) == SImode)
11842 op[arg_count] = gen_lowpart (V1SImode, op[arg_count]);
11843
11844 if (! (*insn_data[icode].operand[idx].predicate) (op[arg_count],
11845 insn_op->mode))
11846 op[arg_count] = copy_to_mode_reg (insn_op->mode, op[arg_count]);
11847 }
11848
11849 switch (arg_count)
11850 {
11851 case 0:
11852 pat = GEN_FCN (icode) (op[0]);
11853 break;
11854 case 1:
11855 if (nonvoid)
11856 pat = GEN_FCN (icode) (op[0], op[1]);
11857 else
11858 pat = GEN_FCN (icode) (op[1]);
11859 break;
11860 case 2:
11861 pat = GEN_FCN (icode) (op[0], op[1], op[2]);
11862 break;
11863 case 3:
11864 pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
11865 break;
11866 default:
11867 gcc_unreachable ();
11868 }
11869
11870 if (!pat)
11871 return NULL_RTX;
11872
11873 emit_insn (pat);
11874
11875 return (nonvoid ? op[0] : const0_rtx);
11876 }
11877
11878 /* Return the upper 16 bits of the 8x16 multiplication. */
11879
11880 static int
11881 sparc_vis_mul8x16 (int e8, int e16)
11882 {
11883 return (e8 * e16 + 128) / 256;
11884 }
11885
11886 /* Multiply the VECTOR_CSTs CST0 and CST1 as specified by FNCODE and put
11887 the result into the array N_ELTS, whose elements are of INNER_TYPE. */
11888
11889 static void
11890 sparc_handle_vis_mul8x16 (vec<tree> *n_elts, enum sparc_builtins fncode,
11891 tree inner_type, tree cst0, tree cst1)
11892 {
11893 unsigned i, num = VECTOR_CST_NELTS (cst0);
11894 int scale;
11895
11896 switch (fncode)
11897 {
11898 case SPARC_BUILTIN_FMUL8X16:
11899 for (i = 0; i < num; ++i)
11900 {
11901 int val
11902 = sparc_vis_mul8x16 (TREE_INT_CST_LOW (VECTOR_CST_ELT (cst0, i)),
11903 TREE_INT_CST_LOW (VECTOR_CST_ELT (cst1, i)));
11904 n_elts->quick_push (build_int_cst (inner_type, val));
11905 }
11906 break;
11907
11908 case SPARC_BUILTIN_FMUL8X16AU:
11909 scale = TREE_INT_CST_LOW (VECTOR_CST_ELT (cst1, 0));
11910
11911 for (i = 0; i < num; ++i)
11912 {
11913 int val
11914 = sparc_vis_mul8x16 (TREE_INT_CST_LOW (VECTOR_CST_ELT (cst0, i)),
11915 scale);
11916 n_elts->quick_push (build_int_cst (inner_type, val));
11917 }
11918 break;
11919
11920 case SPARC_BUILTIN_FMUL8X16AL:
11921 scale = TREE_INT_CST_LOW (VECTOR_CST_ELT (cst1, 1));
11922
11923 for (i = 0; i < num; ++i)
11924 {
11925 int val
11926 = sparc_vis_mul8x16 (TREE_INT_CST_LOW (VECTOR_CST_ELT (cst0, i)),
11927 scale);
11928 n_elts->quick_push (build_int_cst (inner_type, val));
11929 }
11930 break;
11931
11932 default:
11933 gcc_unreachable ();
11934 }
11935 }
11936
11937 /* Implement TARGET_FOLD_BUILTIN hook.
11938
11939 Fold builtin functions for SPARC intrinsics. If IGNORE is true the
11940 result of the function call is ignored. NULL_TREE is returned if the
11941 function could not be folded. */
11942
11943 static tree
11944 sparc_fold_builtin (tree fndecl, int n_args ATTRIBUTE_UNUSED,
11945 tree *args, bool ignore)
11946 {
11947 enum sparc_builtins code
11948 = (enum sparc_builtins) DECL_MD_FUNCTION_CODE (fndecl);
11949 tree rtype = TREE_TYPE (TREE_TYPE (fndecl));
11950 tree arg0, arg1, arg2;
11951
11952 if (ignore)
11953 switch (code)
11954 {
11955 case SPARC_BUILTIN_LDFSR:
11956 case SPARC_BUILTIN_STFSR:
11957 case SPARC_BUILTIN_ALIGNADDR:
11958 case SPARC_BUILTIN_WRGSR:
11959 case SPARC_BUILTIN_BMASK:
11960 case SPARC_BUILTIN_CMASK8:
11961 case SPARC_BUILTIN_CMASK16:
11962 case SPARC_BUILTIN_CMASK32:
11963 break;
11964
11965 default:
11966 return build_zero_cst (rtype);
11967 }
11968
11969 switch (code)
11970 {
11971 case SPARC_BUILTIN_FEXPAND:
11972 arg0 = args[0];
11973 STRIP_NOPS (arg0);
11974
11975 if (TREE_CODE (arg0) == VECTOR_CST)
11976 {
11977 tree inner_type = TREE_TYPE (rtype);
11978 unsigned i;
11979
11980 tree_vector_builder n_elts (rtype, VECTOR_CST_NELTS (arg0), 1);
11981 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
11982 {
11983 unsigned HOST_WIDE_INT val
11984 = TREE_INT_CST_LOW (VECTOR_CST_ELT (arg0, i));
11985 n_elts.quick_push (build_int_cst (inner_type, val << 4));
11986 }
11987 return n_elts.build ();
11988 }
11989 break;
11990
11991 case SPARC_BUILTIN_FMUL8X16:
11992 case SPARC_BUILTIN_FMUL8X16AU:
11993 case SPARC_BUILTIN_FMUL8X16AL:
11994 arg0 = args[0];
11995 arg1 = args[1];
11996 STRIP_NOPS (arg0);
11997 STRIP_NOPS (arg1);
11998
11999 if (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
12000 {
12001 tree inner_type = TREE_TYPE (rtype);
12002 tree_vector_builder n_elts (rtype, VECTOR_CST_NELTS (arg0), 1);
12003 sparc_handle_vis_mul8x16 (&n_elts, code, inner_type, arg0, arg1);
12004 return n_elts.build ();
12005 }
12006 break;
12007
12008 case SPARC_BUILTIN_FPMERGE:
12009 arg0 = args[0];
12010 arg1 = args[1];
12011 STRIP_NOPS (arg0);
12012 STRIP_NOPS (arg1);
12013
12014 if (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
12015 {
12016 tree_vector_builder n_elts (rtype, 2 * VECTOR_CST_NELTS (arg0), 1);
12017 unsigned i;
12018 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
12019 {
12020 n_elts.quick_push (VECTOR_CST_ELT (arg0, i));
12021 n_elts.quick_push (VECTOR_CST_ELT (arg1, i));
12022 }
12023
12024 return n_elts.build ();
12025 }
12026 break;
12027
12028 case SPARC_BUILTIN_PDIST:
12029 case SPARC_BUILTIN_PDISTN:
12030 arg0 = args[0];
12031 arg1 = args[1];
12032 STRIP_NOPS (arg0);
12033 STRIP_NOPS (arg1);
12034 if (code == SPARC_BUILTIN_PDIST)
12035 {
12036 arg2 = args[2];
12037 STRIP_NOPS (arg2);
12038 }
12039 else
12040 arg2 = integer_zero_node;
12041
12042 if (TREE_CODE (arg0) == VECTOR_CST
12043 && TREE_CODE (arg1) == VECTOR_CST
12044 && TREE_CODE (arg2) == INTEGER_CST)
12045 {
12046 bool overflow = false;
12047 widest_int result = wi::to_widest (arg2);
12048 widest_int tmp;
12049 unsigned i;
12050
12051 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
12052 {
12053 tree e0 = VECTOR_CST_ELT (arg0, i);
12054 tree e1 = VECTOR_CST_ELT (arg1, i);
12055
12056 wi::overflow_type neg1_ovf, neg2_ovf, add1_ovf, add2_ovf;
12057
12058 tmp = wi::neg (wi::to_widest (e1), &neg1_ovf);
12059 tmp = wi::add (wi::to_widest (e0), tmp, SIGNED, &add1_ovf);
12060 if (wi::neg_p (tmp))
12061 tmp = wi::neg (tmp, &neg2_ovf);
12062 else
12063 neg2_ovf = wi::OVF_NONE;
12064 result = wi::add (result, tmp, SIGNED, &add2_ovf);
12065 overflow |= ((neg1_ovf != wi::OVF_NONE)
12066 | (neg2_ovf != wi::OVF_NONE)
12067 | (add1_ovf != wi::OVF_NONE)
12068 | (add2_ovf != wi::OVF_NONE));
12069 }
12070
12071 gcc_assert (!overflow);
12072
12073 return wide_int_to_tree (rtype, result);
12074 }
12075
12076 default:
12077 break;
12078 }
12079
12080 return NULL_TREE;
12081 }
12082
12083 /* ??? This duplicates information provided to the compiler by the
12084 ??? scheduler description. Some day, teach genautomata to output
12085 ??? the latencies and then CSE will just use that. */
12086
12087 static bool
12088 sparc_rtx_costs (rtx x, machine_mode mode, int outer_code,
12089 int opno ATTRIBUTE_UNUSED,
12090 int *total, bool speed ATTRIBUTE_UNUSED)
12091 {
12092 int code = GET_CODE (x);
12093 bool float_mode_p = FLOAT_MODE_P (mode);
12094
12095 switch (code)
12096 {
12097 case CONST_INT:
12098 if (SMALL_INT (x))
12099 *total = 0;
12100 else
12101 *total = 2;
12102 return true;
12103
12104 case CONST_WIDE_INT:
12105 *total = 0;
12106 if (!SPARC_SIMM13_P (CONST_WIDE_INT_ELT (x, 0)))
12107 *total += 2;
12108 if (!SPARC_SIMM13_P (CONST_WIDE_INT_ELT (x, 1)))
12109 *total += 2;
12110 return true;
12111
12112 case HIGH:
12113 *total = 2;
12114 return true;
12115
12116 case CONST:
12117 case LABEL_REF:
12118 case SYMBOL_REF:
12119 *total = 4;
12120 return true;
12121
12122 case CONST_DOUBLE:
12123 *total = 8;
12124 return true;
12125
12126 case MEM:
12127 /* If outer-code was a sign or zero extension, a cost
12128 of COSTS_N_INSNS (1) was already added in. This is
12129 why we are subtracting it back out. */
12130 if (outer_code == ZERO_EXTEND)
12131 {
12132 *total = sparc_costs->int_zload - COSTS_N_INSNS (1);
12133 }
12134 else if (outer_code == SIGN_EXTEND)
12135 {
12136 *total = sparc_costs->int_sload - COSTS_N_INSNS (1);
12137 }
12138 else if (float_mode_p)
12139 {
12140 *total = sparc_costs->float_load;
12141 }
12142 else
12143 {
12144 *total = sparc_costs->int_load;
12145 }
12146
12147 return true;
12148
12149 case PLUS:
12150 case MINUS:
12151 if (float_mode_p)
12152 *total = sparc_costs->float_plusminus;
12153 else
12154 *total = COSTS_N_INSNS (1);
12155 return false;
12156
12157 case FMA:
12158 {
12159 rtx sub;
12160
12161 gcc_assert (float_mode_p);
12162 *total = sparc_costs->float_mul;
12163
12164 sub = XEXP (x, 0);
12165 if (GET_CODE (sub) == NEG)
12166 sub = XEXP (sub, 0);
12167 *total += rtx_cost (sub, mode, FMA, 0, speed);
12168
12169 sub = XEXP (x, 2);
12170 if (GET_CODE (sub) == NEG)
12171 sub = XEXP (sub, 0);
12172 *total += rtx_cost (sub, mode, FMA, 2, speed);
12173 return true;
12174 }
12175
12176 case MULT:
12177 if (float_mode_p)
12178 *total = sparc_costs->float_mul;
12179 else if (TARGET_ARCH32 && !TARGET_HARD_MUL)
12180 *total = COSTS_N_INSNS (25);
12181 else
12182 {
12183 int bit_cost;
12184
12185 bit_cost = 0;
12186 if (sparc_costs->int_mul_bit_factor)
12187 {
12188 int nbits;
12189
12190 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
12191 {
12192 unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
12193 for (nbits = 0; value != 0; value &= value - 1)
12194 nbits++;
12195 }
12196 else
12197 nbits = 7;
12198
12199 if (nbits < 3)
12200 nbits = 3;
12201 bit_cost = (nbits - 3) / sparc_costs->int_mul_bit_factor;
12202 bit_cost = COSTS_N_INSNS (bit_cost);
12203 }
12204
12205 if (mode == DImode || !TARGET_HARD_MUL)
12206 *total = sparc_costs->int_mulX + bit_cost;
12207 else
12208 *total = sparc_costs->int_mul + bit_cost;
12209 }
12210 return false;
12211
12212 case ASHIFT:
12213 case ASHIFTRT:
12214 case LSHIFTRT:
12215 *total = COSTS_N_INSNS (1) + sparc_costs->shift_penalty;
12216 return false;
12217
12218 case DIV:
12219 case UDIV:
12220 case MOD:
12221 case UMOD:
12222 if (float_mode_p)
12223 {
12224 if (mode == DFmode)
12225 *total = sparc_costs->float_div_df;
12226 else
12227 *total = sparc_costs->float_div_sf;
12228 }
12229 else
12230 {
12231 if (mode == DImode)
12232 *total = sparc_costs->int_divX;
12233 else
12234 *total = sparc_costs->int_div;
12235 }
12236 return false;
12237
12238 case NEG:
12239 if (! float_mode_p)
12240 {
12241 *total = COSTS_N_INSNS (1);
12242 return false;
12243 }
12244 /* FALLTHRU */
12245
12246 case ABS:
12247 case FLOAT:
12248 case UNSIGNED_FLOAT:
12249 case FIX:
12250 case UNSIGNED_FIX:
12251 case FLOAT_EXTEND:
12252 case FLOAT_TRUNCATE:
12253 *total = sparc_costs->float_move;
12254 return false;
12255
12256 case SQRT:
12257 if (mode == DFmode)
12258 *total = sparc_costs->float_sqrt_df;
12259 else
12260 *total = sparc_costs->float_sqrt_sf;
12261 return false;
12262
12263 case COMPARE:
12264 if (float_mode_p)
12265 *total = sparc_costs->float_cmp;
12266 else
12267 *total = COSTS_N_INSNS (1);
12268 return false;
12269
12270 case IF_THEN_ELSE:
12271 if (float_mode_p)
12272 *total = sparc_costs->float_cmove;
12273 else
12274 *total = sparc_costs->int_cmove;
12275 return false;
12276
12277 case IOR:
12278 /* Handle the NAND vector patterns. */
12279 if (sparc_vector_mode_supported_p (mode)
12280 && GET_CODE (XEXP (x, 0)) == NOT
12281 && GET_CODE (XEXP (x, 1)) == NOT)
12282 {
12283 *total = COSTS_N_INSNS (1);
12284 return true;
12285 }
12286 else
12287 return false;
12288
12289 default:
12290 return false;
12291 }
12292 }
12293
12294 /* Return true if CLASS is either GENERAL_REGS or I64_REGS. */
12295
12296 static inline bool
12297 general_or_i64_p (reg_class_t rclass)
12298 {
12299 return (rclass == GENERAL_REGS || rclass == I64_REGS);
12300 }
12301
12302 /* Implement TARGET_REGISTER_MOVE_COST. */
12303
12304 static int
12305 sparc_register_move_cost (machine_mode mode ATTRIBUTE_UNUSED,
12306 reg_class_t from, reg_class_t to)
12307 {
12308 bool need_memory = false;
12309
12310 /* This helps postreload CSE to eliminate redundant comparisons. */
12311 if (from == NO_REGS || to == NO_REGS)
12312 return 100;
12313
12314 if (from == FPCC_REGS || to == FPCC_REGS)
12315 need_memory = true;
12316 else if ((FP_REG_CLASS_P (from) && general_or_i64_p (to))
12317 || (general_or_i64_p (from) && FP_REG_CLASS_P (to)))
12318 {
12319 if (TARGET_VIS3)
12320 {
12321 int size = GET_MODE_SIZE (mode);
12322 if (size == 8 || size == 4)
12323 {
12324 if (! TARGET_ARCH32 || size == 4)
12325 return 4;
12326 else
12327 return 6;
12328 }
12329 }
12330 need_memory = true;
12331 }
12332
12333 if (need_memory)
12334 {
12335 if (sparc_cpu == PROCESSOR_ULTRASPARC
12336 || sparc_cpu == PROCESSOR_ULTRASPARC3
12337 || sparc_cpu == PROCESSOR_NIAGARA
12338 || sparc_cpu == PROCESSOR_NIAGARA2
12339 || sparc_cpu == PROCESSOR_NIAGARA3
12340 || sparc_cpu == PROCESSOR_NIAGARA4
12341 || sparc_cpu == PROCESSOR_NIAGARA7
12342 || sparc_cpu == PROCESSOR_M8)
12343 return 12;
12344
12345 return 6;
12346 }
12347
12348 return 2;
12349 }
12350
12351 /* Emit the sequence of insns SEQ while preserving the registers REG and REG2.
12352 This is achieved by means of a manual dynamic stack space allocation in
12353 the current frame. We make the assumption that SEQ doesn't contain any
12354 function calls, with the possible exception of calls to the GOT helper. */
12355
12356 static void
12357 emit_and_preserve (rtx seq, rtx reg, rtx reg2)
12358 {
12359 /* We must preserve the lowest 16 words for the register save area. */
12360 HOST_WIDE_INT offset = 16*UNITS_PER_WORD;
12361 /* We really need only 2 words of fresh stack space. */
12362 HOST_WIDE_INT size = SPARC_STACK_ALIGN (offset + 2*UNITS_PER_WORD);
12363
12364 rtx slot
12365 = gen_rtx_MEM (word_mode, plus_constant (Pmode, stack_pointer_rtx,
12366 SPARC_STACK_BIAS + offset));
12367
12368 emit_insn (gen_stack_pointer_inc (GEN_INT (-size)));
12369 emit_insn (gen_rtx_SET (slot, reg));
12370 if (reg2)
12371 emit_insn (gen_rtx_SET (adjust_address (slot, word_mode, UNITS_PER_WORD),
12372 reg2));
12373 emit_insn (seq);
12374 if (reg2)
12375 emit_insn (gen_rtx_SET (reg2,
12376 adjust_address (slot, word_mode, UNITS_PER_WORD)));
12377 emit_insn (gen_rtx_SET (reg, slot));
12378 emit_insn (gen_stack_pointer_inc (GEN_INT (size)));
12379 }
12380
12381 /* Output the assembler code for a thunk function. THUNK_DECL is the
12382 declaration for the thunk function itself, FUNCTION is the decl for
12383 the target function. DELTA is an immediate constant offset to be
12384 added to THIS. If VCALL_OFFSET is nonzero, the word at address
12385 (*THIS + VCALL_OFFSET) should be additionally added to THIS. */
12386
12387 static void
12388 sparc_output_mi_thunk (FILE *file, tree thunk_fndecl ATTRIBUTE_UNUSED,
12389 HOST_WIDE_INT delta, HOST_WIDE_INT vcall_offset,
12390 tree function)
12391 {
12392 const char *fnname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk_fndecl));
12393 rtx this_rtx, funexp;
12394 rtx_insn *insn;
12395 unsigned int int_arg_first;
12396
12397 reload_completed = 1;
12398 epilogue_completed = 1;
12399
12400 emit_note (NOTE_INSN_PROLOGUE_END);
12401
12402 if (TARGET_FLAT)
12403 {
12404 sparc_leaf_function_p = 1;
12405
12406 int_arg_first = SPARC_OUTGOING_INT_ARG_FIRST;
12407 }
12408 else if (flag_delayed_branch)
12409 {
12410 /* We will emit a regular sibcall below, so we need to instruct
12411 output_sibcall that we are in a leaf function. */
12412 sparc_leaf_function_p = crtl->uses_only_leaf_regs = 1;
12413
12414 /* This will cause final.cc to invoke leaf_renumber_regs so we
12415 must behave as if we were in a not-yet-leafified function. */
12416 int_arg_first = SPARC_INCOMING_INT_ARG_FIRST;
12417 }
12418 else
12419 {
12420 /* We will emit the sibcall manually below, so we will need to
12421 manually spill non-leaf registers. */
12422 sparc_leaf_function_p = crtl->uses_only_leaf_regs = 0;
12423
12424 /* We really are in a leaf function. */
12425 int_arg_first = SPARC_OUTGOING_INT_ARG_FIRST;
12426 }
12427
12428 /* Find the "this" pointer. Normally in %o0, but in ARCH64 if the function
12429 returns a structure, the structure return pointer is there instead. */
12430 if (TARGET_ARCH64
12431 && aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function))
12432 this_rtx = gen_rtx_REG (Pmode, int_arg_first + 1);
12433 else
12434 this_rtx = gen_rtx_REG (Pmode, int_arg_first);
12435
12436 /* Add DELTA. When possible use a plain add, otherwise load it into
12437 a register first. */
12438 if (delta)
12439 {
12440 rtx delta_rtx = GEN_INT (delta);
12441
12442 if (! SPARC_SIMM13_P (delta))
12443 {
12444 rtx scratch = gen_rtx_REG (Pmode, 1);
12445 emit_move_insn (scratch, delta_rtx);
12446 delta_rtx = scratch;
12447 }
12448
12449 /* THIS_RTX += DELTA. */
12450 emit_insn (gen_add2_insn (this_rtx, delta_rtx));
12451 }
12452
12453 /* Add the word at address (*THIS_RTX + VCALL_OFFSET). */
12454 if (vcall_offset)
12455 {
12456 rtx vcall_offset_rtx = GEN_INT (vcall_offset);
12457 rtx scratch = gen_rtx_REG (Pmode, 1);
12458
12459 gcc_assert (vcall_offset < 0);
12460
12461 /* SCRATCH = *THIS_RTX. */
12462 emit_move_insn (scratch, gen_rtx_MEM (Pmode, this_rtx));
12463
12464 /* Prepare for adding VCALL_OFFSET. The difficulty is that we
12465 may not have any available scratch register at this point. */
12466 if (SPARC_SIMM13_P (vcall_offset))
12467 ;
12468 /* This is the case if ARCH64 (unless -ffixed-g5 is passed). */
12469 else if (! fixed_regs[5]
12470 /* The below sequence is made up of at least 2 insns,
12471 while the default method may need only one. */
12472 && vcall_offset < -8192)
12473 {
12474 rtx scratch2 = gen_rtx_REG (Pmode, 5);
12475 emit_move_insn (scratch2, vcall_offset_rtx);
12476 vcall_offset_rtx = scratch2;
12477 }
12478 else
12479 {
12480 rtx increment = GEN_INT (-4096);
12481
12482 /* VCALL_OFFSET is a negative number whose typical range can be
12483 estimated as -32768..0 in 32-bit mode. In almost all cases
12484 it is therefore cheaper to emit multiple add insns than
12485 spilling and loading the constant into a register (at least
12486 6 insns). */
12487 while (! SPARC_SIMM13_P (vcall_offset))
12488 {
12489 emit_insn (gen_add2_insn (scratch, increment));
12490 vcall_offset += 4096;
12491 }
12492 vcall_offset_rtx = GEN_INT (vcall_offset); /* cannot be 0 */
12493 }
12494
12495 /* SCRATCH = *(*THIS_RTX + VCALL_OFFSET). */
12496 emit_move_insn (scratch, gen_rtx_MEM (Pmode,
12497 gen_rtx_PLUS (Pmode,
12498 scratch,
12499 vcall_offset_rtx)));
12500
12501 /* THIS_RTX += *(*THIS_RTX + VCALL_OFFSET). */
12502 emit_insn (gen_add2_insn (this_rtx, scratch));
12503 }
12504
12505 /* Generate a tail call to the target function. */
12506 if (! TREE_USED (function))
12507 {
12508 assemble_external (function);
12509 TREE_USED (function) = 1;
12510 }
12511 funexp = XEXP (DECL_RTL (function), 0);
12512
12513 if (flag_delayed_branch)
12514 {
12515 funexp = gen_rtx_MEM (FUNCTION_MODE, funexp);
12516 insn = emit_call_insn (gen_sibcall (funexp));
12517 SIBLING_CALL_P (insn) = 1;
12518 }
12519 else
12520 {
12521 /* The hoops we have to jump through in order to generate a sibcall
12522 without using delay slots... */
12523 rtx spill_reg, seq, scratch = gen_rtx_REG (Pmode, 1);
12524
12525 if (flag_pic)
12526 {
12527 spill_reg = gen_rtx_REG (word_mode, 15); /* %o7 */
12528 start_sequence ();
12529 load_got_register (); /* clobbers %o7 */
12530 if (!TARGET_VXWORKS_RTP)
12531 pic_offset_table_rtx = got_register_rtx;
12532 scratch = sparc_legitimize_pic_address (funexp, scratch);
12533 seq = get_insns ();
12534 end_sequence ();
12535 emit_and_preserve (seq, spill_reg, pic_offset_table_rtx);
12536 }
12537 else if (TARGET_ARCH32)
12538 {
12539 emit_insn (gen_rtx_SET (scratch,
12540 gen_rtx_HIGH (SImode, funexp)));
12541 emit_insn (gen_rtx_SET (scratch,
12542 gen_rtx_LO_SUM (SImode, scratch, funexp)));
12543 }
12544 else /* TARGET_ARCH64 */
12545 {
12546 switch (sparc_code_model)
12547 {
12548 case CM_MEDLOW:
12549 case CM_MEDMID:
12550 /* The destination can serve as a temporary. */
12551 sparc_emit_set_symbolic_const64 (scratch, funexp, scratch);
12552 break;
12553
12554 case CM_MEDANY:
12555 case CM_EMBMEDANY:
12556 /* The destination cannot serve as a temporary. */
12557 spill_reg = gen_rtx_REG (DImode, 15); /* %o7 */
12558 start_sequence ();
12559 sparc_emit_set_symbolic_const64 (scratch, funexp, spill_reg);
12560 seq = get_insns ();
12561 end_sequence ();
12562 emit_and_preserve (seq, spill_reg, 0);
12563 break;
12564
12565 default:
12566 gcc_unreachable ();
12567 }
12568 }
12569
12570 emit_jump_insn (gen_indirect_jump (scratch));
12571 }
12572
12573 emit_barrier ();
12574
12575 /* Run just enough of rest_of_compilation to get the insns emitted.
12576 There's not really enough bulk here to make other passes such as
12577 instruction scheduling worth while. */
12578 insn = get_insns ();
12579 shorten_branches (insn);
12580 assemble_start_function (thunk_fndecl, fnname);
12581 final_start_function (insn, file, 1);
12582 final (insn, file, 1);
12583 final_end_function ();
12584 assemble_end_function (thunk_fndecl, fnname);
12585
12586 reload_completed = 0;
12587 epilogue_completed = 0;
12588 }
12589
12590 /* Return true if sparc_output_mi_thunk would be able to output the
12591 assembler code for the thunk function specified by the arguments
12592 it is passed, and false otherwise. */
12593 static bool
12594 sparc_can_output_mi_thunk (const_tree thunk_fndecl ATTRIBUTE_UNUSED,
12595 HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
12596 HOST_WIDE_INT vcall_offset,
12597 const_tree function ATTRIBUTE_UNUSED)
12598 {
12599 /* Bound the loop used in the default method above. */
12600 return (vcall_offset >= -32768 || ! fixed_regs[5]);
12601 }
12602
12603 /* How to allocate a 'struct machine_function'. */
12604
12605 static struct machine_function *
12606 sparc_init_machine_status (void)
12607 {
12608 return ggc_cleared_alloc<machine_function> ();
12609 }
12610
12611 /* Implement the TARGET_ASAN_SHADOW_OFFSET hook. */
12612
12613 static unsigned HOST_WIDE_INT
12614 sparc_asan_shadow_offset (void)
12615 {
12616 return TARGET_ARCH64 ? (HOST_WIDE_INT_1 << 43) : (HOST_WIDE_INT_1 << 29);
12617 }
12618
12619 /* This is called from dwarf2out.cc via TARGET_ASM_OUTPUT_DWARF_DTPREL.
12620 We need to emit DTP-relative relocations. */
12621
12622 static void
12623 sparc_output_dwarf_dtprel (FILE *file, int size, rtx x)
12624 {
12625 switch (size)
12626 {
12627 case 4:
12628 fputs ("\t.word\t%r_tls_dtpoff32(", file);
12629 break;
12630 case 8:
12631 fputs ("\t.xword\t%r_tls_dtpoff64(", file);
12632 break;
12633 default:
12634 gcc_unreachable ();
12635 }
12636 output_addr_const (file, x);
12637 fputs (")", file);
12638 }
12639
12640 /* Implement TARGET_OUTPUT_CFI_DIRECTIVE. */
12641 static bool
12642 sparc_output_cfi_directive (FILE *f, dw_cfi_ref cfi)
12643 {
12644 if (cfi->dw_cfi_opc == DW_CFA_GNU_window_save)
12645 {
12646 fprintf (f, "\t.cfi_window_save\n");
12647 return true;
12648 }
12649 return false;
12650 }
12651
12652 /* Implement TARGET_DW_CFI_OPRND1_DESC. */
12653 static bool
12654 sparc_dw_cfi_oprnd1_desc (dwarf_call_frame_info cfi_opc,
12655 dw_cfi_oprnd_type &oprnd_type)
12656 {
12657 if (cfi_opc == DW_CFA_GNU_window_save)
12658 {
12659 oprnd_type = dw_cfi_oprnd_unused;
12660 return true;
12661 }
12662 return false;
12663 }
12664
12665 /* Do whatever processing is required at the end of a file. */
12666
12667 static void
12668 sparc_file_end (void)
12669 {
12670 /* If we need to emit the special GOT helper function, do so now. */
12671 if (got_helper_needed)
12672 {
12673 const char *name = XSTR (got_helper_rtx, 0);
12674 #ifdef DWARF2_UNWIND_INFO
12675 bool do_cfi;
12676 #endif
12677
12678 if (USE_HIDDEN_LINKONCE)
12679 {
12680 tree decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
12681 get_identifier (name),
12682 build_function_type_list (void_type_node,
12683 NULL_TREE));
12684 DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
12685 NULL_TREE, void_type_node);
12686 TREE_PUBLIC (decl) = 1;
12687 TREE_STATIC (decl) = 1;
12688 make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
12689 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
12690 DECL_VISIBILITY_SPECIFIED (decl) = 1;
12691 resolve_unique_section (decl, 0, flag_function_sections);
12692 allocate_struct_function (decl, true);
12693 cfun->is_thunk = 1;
12694 current_function_decl = decl;
12695 init_varasm_status ();
12696 assemble_start_function (decl, name);
12697 }
12698 else
12699 {
12700 const int align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
12701 switch_to_section (text_section);
12702 if (align > 0)
12703 ASM_OUTPUT_ALIGN (asm_out_file, align);
12704 ASM_OUTPUT_LABEL (asm_out_file, name);
12705 }
12706
12707 #ifdef DWARF2_UNWIND_INFO
12708 do_cfi = dwarf2out_do_cfi_asm ();
12709 if (do_cfi)
12710 output_asm_insn (".cfi_startproc", NULL);
12711 #endif
12712 if (flag_delayed_branch)
12713 {
12714 output_asm_insn ("jmp\t%%o7+8", NULL);
12715 output_asm_insn (" add\t%%o7, %0, %0", &got_register_rtx);
12716 }
12717 else
12718 {
12719 output_asm_insn ("add\t%%o7, %0, %0", &got_register_rtx);
12720 output_asm_insn ("jmp\t%%o7+8", NULL);
12721 output_asm_insn (" nop", NULL);
12722 }
12723 #ifdef DWARF2_UNWIND_INFO
12724 if (do_cfi)
12725 output_asm_insn (".cfi_endproc", NULL);
12726 #endif
12727 }
12728
12729 if (NEED_INDICATE_EXEC_STACK)
12730 file_end_indicate_exec_stack ();
12731
12732 #ifdef TARGET_SOLARIS
12733 solaris_file_end ();
12734 #endif
12735 }
12736
12737 #ifdef TARGET_ALTERNATE_LONG_DOUBLE_MANGLING
12738 /* Implement TARGET_MANGLE_TYPE. */
12739
12740 static const char *
12741 sparc_mangle_type (const_tree type)
12742 {
12743 if (TARGET_ARCH32
12744 && TYPE_MAIN_VARIANT (type) == long_double_type_node
12745 && TARGET_LONG_DOUBLE_128)
12746 return "g";
12747
12748 /* For all other types, use normal C++ mangling. */
12749 return NULL;
12750 }
12751 #endif
12752
12753 /* Expand a membar instruction for various use cases. Both the LOAD_STORE
12754 and BEFORE_AFTER arguments of the form X_Y. They are two-bit masks where
12755 bit 0 indicates that X is true, and bit 1 indicates Y is true. */
12756
12757 void
12758 sparc_emit_membar_for_model (enum memmodel model,
12759 int load_store, int before_after)
12760 {
12761 /* Bits for the MEMBAR mmask field. */
12762 const int LoadLoad = 1;
12763 const int StoreLoad = 2;
12764 const int LoadStore = 4;
12765 const int StoreStore = 8;
12766
12767 int mm = 0, implied = 0;
12768
12769 switch (sparc_memory_model)
12770 {
12771 case SMM_SC:
12772 /* Sequential Consistency. All memory transactions are immediately
12773 visible in sequential execution order. No barriers needed. */
12774 implied = LoadLoad | StoreLoad | LoadStore | StoreStore;
12775 break;
12776
12777 case SMM_TSO:
12778 /* Total Store Ordering: all memory transactions with store semantics
12779 are followed by an implied StoreStore. */
12780 implied |= StoreStore;
12781
12782 /* If we're not looking for a raw barrer (before+after), then atomic
12783 operations get the benefit of being both load and store. */
12784 if (load_store == 3 && before_after == 1)
12785 implied |= StoreLoad;
12786 /* FALLTHRU */
12787
12788 case SMM_PSO:
12789 /* Partial Store Ordering: all memory transactions with load semantics
12790 are followed by an implied LoadLoad | LoadStore. */
12791 implied |= LoadLoad | LoadStore;
12792
12793 /* If we're not looking for a raw barrer (before+after), then atomic
12794 operations get the benefit of being both load and store. */
12795 if (load_store == 3 && before_after == 2)
12796 implied |= StoreLoad | StoreStore;
12797 /* FALLTHRU */
12798
12799 case SMM_RMO:
12800 /* Relaxed Memory Ordering: no implicit bits. */
12801 break;
12802
12803 default:
12804 gcc_unreachable ();
12805 }
12806
12807 if (before_after & 1)
12808 {
12809 if (is_mm_release (model) || is_mm_acq_rel (model)
12810 || is_mm_seq_cst (model))
12811 {
12812 if (load_store & 1)
12813 mm |= LoadLoad | StoreLoad;
12814 if (load_store & 2)
12815 mm |= LoadStore | StoreStore;
12816 }
12817 }
12818 if (before_after & 2)
12819 {
12820 if (is_mm_acquire (model) || is_mm_acq_rel (model)
12821 || is_mm_seq_cst (model))
12822 {
12823 if (load_store & 1)
12824 mm |= LoadLoad | LoadStore;
12825 if (load_store & 2)
12826 mm |= StoreLoad | StoreStore;
12827 }
12828 }
12829
12830 /* Remove the bits implied by the system memory model. */
12831 mm &= ~implied;
12832
12833 /* For raw barriers (before+after), always emit a barrier.
12834 This will become a compile-time barrier if needed. */
12835 if (mm || before_after == 3)
12836 emit_insn (gen_membar (GEN_INT (mm)));
12837 }
12838
12839 /* Expand code to perform a 8 or 16-bit compare and swap by doing 32-bit
12840 compare and swap on the word containing the byte or half-word. */
12841
12842 static void
12843 sparc_expand_compare_and_swap_12 (rtx bool_result, rtx result, rtx mem,
12844 rtx oldval, rtx newval)
12845 {
12846 rtx addr1 = force_reg (Pmode, XEXP (mem, 0));
12847 rtx addr = gen_reg_rtx (Pmode);
12848 rtx off = gen_reg_rtx (SImode);
12849 rtx oldv = gen_reg_rtx (SImode);
12850 rtx newv = gen_reg_rtx (SImode);
12851 rtx oldvalue = gen_reg_rtx (SImode);
12852 rtx newvalue = gen_reg_rtx (SImode);
12853 rtx res = gen_reg_rtx (SImode);
12854 rtx resv = gen_reg_rtx (SImode);
12855 rtx memsi, val, mask, cc;
12856
12857 emit_insn (gen_rtx_SET (addr, gen_rtx_AND (Pmode, addr1, GEN_INT (-4))));
12858
12859 if (Pmode != SImode)
12860 addr1 = gen_lowpart (SImode, addr1);
12861 emit_insn (gen_rtx_SET (off, gen_rtx_AND (SImode, addr1, GEN_INT (3))));
12862
12863 memsi = gen_rtx_MEM (SImode, addr);
12864 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
12865 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
12866
12867 val = copy_to_reg (memsi);
12868
12869 emit_insn (gen_rtx_SET (off,
12870 gen_rtx_XOR (SImode, off,
12871 GEN_INT (GET_MODE (mem) == QImode
12872 ? 3 : 2))));
12873
12874 emit_insn (gen_rtx_SET (off, gen_rtx_ASHIFT (SImode, off, GEN_INT (3))));
12875
12876 if (GET_MODE (mem) == QImode)
12877 mask = force_reg (SImode, GEN_INT (0xff));
12878 else
12879 mask = force_reg (SImode, GEN_INT (0xffff));
12880
12881 emit_insn (gen_rtx_SET (mask, gen_rtx_ASHIFT (SImode, mask, off)));
12882
12883 emit_insn (gen_rtx_SET (val,
12884 gen_rtx_AND (SImode, gen_rtx_NOT (SImode, mask),
12885 val)));
12886
12887 oldval = gen_lowpart (SImode, oldval);
12888 emit_insn (gen_rtx_SET (oldv, gen_rtx_ASHIFT (SImode, oldval, off)));
12889
12890 newval = gen_lowpart_common (SImode, newval);
12891 emit_insn (gen_rtx_SET (newv, gen_rtx_ASHIFT (SImode, newval, off)));
12892
12893 emit_insn (gen_rtx_SET (oldv, gen_rtx_AND (SImode, oldv, mask)));
12894
12895 emit_insn (gen_rtx_SET (newv, gen_rtx_AND (SImode, newv, mask)));
12896
12897 rtx_code_label *end_label = gen_label_rtx ();
12898 rtx_code_label *loop_label = gen_label_rtx ();
12899 emit_label (loop_label);
12900
12901 emit_insn (gen_rtx_SET (oldvalue, gen_rtx_IOR (SImode, oldv, val)));
12902
12903 emit_insn (gen_rtx_SET (newvalue, gen_rtx_IOR (SImode, newv, val)));
12904
12905 emit_move_insn (bool_result, const1_rtx);
12906
12907 emit_insn (gen_atomic_compare_and_swapsi_1 (res, memsi, oldvalue, newvalue));
12908
12909 emit_cmp_and_jump_insns (res, oldvalue, EQ, NULL, SImode, 0, end_label);
12910
12911 emit_insn (gen_rtx_SET (resv,
12912 gen_rtx_AND (SImode, gen_rtx_NOT (SImode, mask),
12913 res)));
12914
12915 emit_move_insn (bool_result, const0_rtx);
12916
12917 cc = gen_compare_reg_1 (NE, resv, val);
12918 emit_insn (gen_rtx_SET (val, resv));
12919
12920 /* Use cbranchcc4 to separate the compare and branch! */
12921 emit_jump_insn (gen_cbranchcc4 (gen_rtx_NE (VOIDmode, cc, const0_rtx),
12922 cc, const0_rtx, loop_label));
12923
12924 emit_label (end_label);
12925
12926 emit_insn (gen_rtx_SET (res, gen_rtx_AND (SImode, res, mask)));
12927
12928 emit_insn (gen_rtx_SET (res, gen_rtx_LSHIFTRT (SImode, res, off)));
12929
12930 emit_move_insn (result, gen_lowpart (GET_MODE (result), res));
12931 }
12932
12933 /* Expand code to perform a compare-and-swap. */
12934
12935 void
12936 sparc_expand_compare_and_swap (rtx operands[])
12937 {
12938 rtx bval, retval, mem, oldval, newval;
12939 machine_mode mode;
12940 enum memmodel model;
12941
12942 bval = operands[0];
12943 retval = operands[1];
12944 mem = operands[2];
12945 oldval = operands[3];
12946 newval = operands[4];
12947 model = (enum memmodel) INTVAL (operands[6]);
12948 mode = GET_MODE (mem);
12949
12950 sparc_emit_membar_for_model (model, 3, 1);
12951
12952 if (reg_overlap_mentioned_p (retval, oldval))
12953 oldval = copy_to_reg (oldval);
12954
12955 if (mode == QImode || mode == HImode)
12956 sparc_expand_compare_and_swap_12 (bval, retval, mem, oldval, newval);
12957 else
12958 {
12959 rtx (*gen) (rtx, rtx, rtx, rtx);
12960 rtx x;
12961
12962 if (mode == SImode)
12963 gen = gen_atomic_compare_and_swapsi_1;
12964 else
12965 gen = gen_atomic_compare_and_swapdi_1;
12966 emit_insn (gen (retval, mem, oldval, newval));
12967
12968 x = emit_store_flag (bval, EQ, retval, oldval, mode, 1, 1);
12969 if (x != bval)
12970 convert_move (bval, x, 1);
12971 }
12972
12973 sparc_emit_membar_for_model (model, 3, 2);
12974 }
12975
12976 void
12977 sparc_expand_vec_perm_bmask (machine_mode vmode, rtx sel)
12978 {
12979 rtx t_1, t_2, t_3;
12980
12981 sel = gen_lowpart (DImode, sel);
12982 switch (vmode)
12983 {
12984 case E_V2SImode:
12985 /* inp = xxxxxxxAxxxxxxxB */
12986 t_1 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (16),
12987 NULL_RTX, 1, OPTAB_DIRECT);
12988 /* t_1 = ....xxxxxxxAxxx. */
12989 sel = expand_simple_binop (SImode, AND, gen_lowpart (SImode, sel),
12990 GEN_INT (3), NULL_RTX, 1, OPTAB_DIRECT);
12991 t_1 = expand_simple_binop (SImode, AND, gen_lowpart (SImode, t_1),
12992 GEN_INT (0x30000), NULL_RTX, 1, OPTAB_DIRECT);
12993 /* sel = .......B */
12994 /* t_1 = ...A.... */
12995 sel = expand_simple_binop (SImode, IOR, sel, t_1, sel, 1, OPTAB_DIRECT);
12996 /* sel = ...A...B */
12997 sel = expand_mult (SImode, sel, GEN_INT (0x4444), sel, 1);
12998 /* sel = AAAABBBB * 4 */
12999 t_1 = force_reg (SImode, GEN_INT (0x01230123));
13000 /* sel = { A*4, A*4+1, A*4+2, ... } */
13001 break;
13002
13003 case E_V4HImode:
13004 /* inp = xxxAxxxBxxxCxxxD */
13005 t_1 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (8),
13006 NULL_RTX, 1, OPTAB_DIRECT);
13007 t_2 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (16),
13008 NULL_RTX, 1, OPTAB_DIRECT);
13009 t_3 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (24),
13010 NULL_RTX, 1, OPTAB_DIRECT);
13011 /* t_1 = ..xxxAxxxBxxxCxx */
13012 /* t_2 = ....xxxAxxxBxxxC */
13013 /* t_3 = ......xxxAxxxBxx */
13014 sel = expand_simple_binop (SImode, AND, gen_lowpart (SImode, sel),
13015 GEN_INT (0x07),
13016 NULL_RTX, 1, OPTAB_DIRECT);
13017 t_1 = expand_simple_binop (SImode, AND, gen_lowpart (SImode, t_1),
13018 GEN_INT (0x0700),
13019 NULL_RTX, 1, OPTAB_DIRECT);
13020 t_2 = expand_simple_binop (SImode, AND, gen_lowpart (SImode, t_2),
13021 GEN_INT (0x070000),
13022 NULL_RTX, 1, OPTAB_DIRECT);
13023 t_3 = expand_simple_binop (SImode, AND, gen_lowpart (SImode, t_3),
13024 GEN_INT (0x07000000),
13025 NULL_RTX, 1, OPTAB_DIRECT);
13026 /* sel = .......D */
13027 /* t_1 = .....C.. */
13028 /* t_2 = ...B.... */
13029 /* t_3 = .A...... */
13030 sel = expand_simple_binop (SImode, IOR, sel, t_1, sel, 1, OPTAB_DIRECT);
13031 t_2 = expand_simple_binop (SImode, IOR, t_2, t_3, t_2, 1, OPTAB_DIRECT);
13032 sel = expand_simple_binop (SImode, IOR, sel, t_2, sel, 1, OPTAB_DIRECT);
13033 /* sel = .A.B.C.D */
13034 sel = expand_mult (SImode, sel, GEN_INT (0x22), sel, 1);
13035 /* sel = AABBCCDD * 2 */
13036 t_1 = force_reg (SImode, GEN_INT (0x01010101));
13037 /* sel = { A*2, A*2+1, B*2, B*2+1, ... } */
13038 break;
13039
13040 case E_V8QImode:
13041 /* input = xAxBxCxDxExFxGxH */
13042 sel = expand_simple_binop (DImode, AND, sel,
13043 GEN_INT ((HOST_WIDE_INT)0x0f0f0f0f << 32
13044 | 0x0f0f0f0f),
13045 NULL_RTX, 1, OPTAB_DIRECT);
13046 /* sel = .A.B.C.D.E.F.G.H */
13047 t_1 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (4),
13048 NULL_RTX, 1, OPTAB_DIRECT);
13049 /* t_1 = ..A.B.C.D.E.F.G. */
13050 sel = expand_simple_binop (DImode, IOR, sel, t_1,
13051 NULL_RTX, 1, OPTAB_DIRECT);
13052 /* sel = .AABBCCDDEEFFGGH */
13053 sel = expand_simple_binop (DImode, AND, sel,
13054 GEN_INT ((HOST_WIDE_INT)0xff00ff << 32
13055 | 0xff00ff),
13056 NULL_RTX, 1, OPTAB_DIRECT);
13057 /* sel = ..AB..CD..EF..GH */
13058 t_1 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (8),
13059 NULL_RTX, 1, OPTAB_DIRECT);
13060 /* t_1 = ....AB..CD..EF.. */
13061 sel = expand_simple_binop (DImode, IOR, sel, t_1,
13062 NULL_RTX, 1, OPTAB_DIRECT);
13063 /* sel = ..ABABCDCDEFEFGH */
13064 sel = expand_simple_binop (DImode, AND, sel,
13065 GEN_INT ((HOST_WIDE_INT)0xffff << 32 | 0xffff),
13066 NULL_RTX, 1, OPTAB_DIRECT);
13067 /* sel = ....ABCD....EFGH */
13068 t_1 = expand_simple_binop (DImode, LSHIFTRT, sel, GEN_INT (16),
13069 NULL_RTX, 1, OPTAB_DIRECT);
13070 /* t_1 = ........ABCD.... */
13071 sel = gen_lowpart (SImode, sel);
13072 t_1 = gen_lowpart (SImode, t_1);
13073 break;
13074
13075 default:
13076 gcc_unreachable ();
13077 }
13078
13079 /* Always perform the final addition/merge within the bmask insn. */
13080 emit_insn (gen_bmasksi_vis (gen_reg_rtx (SImode), sel, t_1));
13081 }
13082
13083 /* Implement TARGET_VEC_PERM_CONST. */
13084
13085 static bool
13086 sparc_vectorize_vec_perm_const (machine_mode vmode, machine_mode op_mode,
13087 rtx target, rtx op0, rtx op1,
13088 const vec_perm_indices &sel)
13089 {
13090 if (vmode != op_mode)
13091 return false;
13092
13093 if (!TARGET_VIS2)
13094 return false;
13095
13096 /* All 8-byte permutes are supported. */
13097 if (!target)
13098 return GET_MODE_SIZE (vmode) == 8;
13099
13100 /* Force target-independent code to convert constant permutations on other
13101 modes down to V8QI. Rely on this to avoid the complexity of the byte
13102 order of the permutation. */
13103 if (vmode != V8QImode)
13104 return false;
13105
13106 rtx nop0 = force_reg (vmode, op0);
13107 if (op0 == op1)
13108 op1 = nop0;
13109 op0 = nop0;
13110 op1 = force_reg (vmode, op1);
13111
13112 unsigned int i, mask;
13113 for (i = mask = 0; i < 8; ++i)
13114 mask |= (sel[i] & 0xf) << (28 - i*4);
13115 rtx mask_rtx = force_reg (SImode, gen_int_mode (mask, SImode));
13116
13117 emit_insn (gen_bmasksi_vis (gen_reg_rtx (SImode), mask_rtx, const0_rtx));
13118 emit_insn (gen_bshufflev8qi_vis (target, op0, op1));
13119 return true;
13120 }
13121
13122 /* Implement TARGET_VECTORIZE_GET_MASK_MODE. */
13123
13124 static opt_machine_mode
13125 sparc_get_mask_mode (machine_mode)
13126 {
13127 return Pmode;
13128 }
13129
13130 /* Implement TARGET_FRAME_POINTER_REQUIRED. */
13131
13132 static bool
13133 sparc_frame_pointer_required (void)
13134 {
13135 /* If the stack pointer is dynamically modified in the function, it cannot
13136 serve as the frame pointer. */
13137 if (cfun->calls_alloca)
13138 return true;
13139
13140 /* If the function receives nonlocal gotos, it needs to save the frame
13141 pointer in the nonlocal_goto_save_area object. */
13142 if (cfun->has_nonlocal_label)
13143 return true;
13144
13145 /* In flat mode, that's it. */
13146 if (TARGET_FLAT)
13147 return false;
13148
13149 /* Otherwise, the frame pointer is required if the function isn't leaf, but
13150 we cannot use sparc_leaf_function_p since it hasn't been computed yet. */
13151 return !(optimize > 0 && crtl->is_leaf && only_leaf_regs_used ());
13152 }
13153
13154 /* The way this is structured, we can't eliminate SFP in favor of SP
13155 if the frame pointer is required: we want to use the SFP->HFP elimination
13156 in that case. But the test in update_eliminables doesn't know we are
13157 assuming below that we only do the former elimination. */
13158
13159 static bool
13160 sparc_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
13161 {
13162 return to == HARD_FRAME_POINTER_REGNUM || !sparc_frame_pointer_required ();
13163 }
13164
13165 /* If !TARGET_FPU, then make the fp registers and fp cc regs fixed so that
13166 they won't be allocated. */
13167
13168 static void
13169 sparc_conditional_register_usage (void)
13170 {
13171 if (PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM)
13172 fixed_regs[PIC_OFFSET_TABLE_REGNUM] = 1;
13173 /* If the user has passed -f{fixed,call-{used,saved}}-g5 */
13174 /* then honor it. */
13175 if (TARGET_ARCH32 && fixed_regs[5])
13176 fixed_regs[5] = 1;
13177 else if (TARGET_ARCH64 && fixed_regs[5] == 2)
13178 fixed_regs[5] = 0;
13179 if (! TARGET_V9)
13180 {
13181 int regno;
13182 for (regno = SPARC_FIRST_V9_FP_REG;
13183 regno <= SPARC_LAST_V9_FP_REG;
13184 regno++)
13185 fixed_regs[regno] = 1;
13186 /* %fcc0 is used by v8 and v9. */
13187 for (regno = SPARC_FIRST_V9_FCC_REG + 1;
13188 regno <= SPARC_LAST_V9_FCC_REG;
13189 regno++)
13190 fixed_regs[regno] = 1;
13191 }
13192 if (! TARGET_FPU)
13193 {
13194 int regno;
13195 for (regno = 32; regno < SPARC_LAST_V9_FCC_REG; regno++)
13196 fixed_regs[regno] = 1;
13197 }
13198 /* If the user has passed -f{fixed,call-{used,saved}}-g2 */
13199 /* then honor it. Likewise with g3 and g4. */
13200 if (fixed_regs[2] == 2)
13201 fixed_regs[2] = ! TARGET_APP_REGS;
13202 if (fixed_regs[3] == 2)
13203 fixed_regs[3] = ! TARGET_APP_REGS;
13204 if (TARGET_ARCH32 && fixed_regs[4] == 2)
13205 fixed_regs[4] = ! TARGET_APP_REGS;
13206 else if (TARGET_CM_EMBMEDANY)
13207 fixed_regs[4] = 1;
13208 else if (fixed_regs[4] == 2)
13209 fixed_regs[4] = 0;
13210
13211 /* Disable leaf function optimization in flat mode. */
13212 if (TARGET_FLAT)
13213 memset (sparc_leaf_regs, 0, FIRST_PSEUDO_REGISTER);
13214
13215 if (TARGET_VIS)
13216 global_regs[SPARC_GSR_REG] = 1;
13217 }
13218
13219 /* Implement TARGET_USE_PSEUDO_PIC_REG. */
13220
13221 static bool
13222 sparc_use_pseudo_pic_reg (void)
13223 {
13224 return !TARGET_VXWORKS_RTP && flag_pic;
13225 }
13226
13227 /* Implement TARGET_INIT_PIC_REG. */
13228
13229 static void
13230 sparc_init_pic_reg (void)
13231 {
13232 edge entry_edge;
13233 rtx_insn *seq;
13234
13235 /* In PIC mode, we need to always initialize the PIC register if optimization
13236 is enabled, because we are called from IRA and LRA may later force things
13237 to the constant pool for optimization purposes. */
13238 if (!flag_pic || (!crtl->uses_pic_offset_table && !optimize))
13239 return;
13240
13241 start_sequence ();
13242 load_got_register ();
13243 if (!TARGET_VXWORKS_RTP)
13244 emit_move_insn (pic_offset_table_rtx, got_register_rtx);
13245 seq = get_insns ();
13246 end_sequence ();
13247
13248 entry_edge = single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun));
13249 insert_insn_on_edge (seq, entry_edge);
13250 commit_one_edge_insertion (entry_edge);
13251 }
13252
13253 /* Implement TARGET_PREFERRED_RELOAD_CLASS:
13254
13255 - We can't load constants into FP registers.
13256 - We can't load FP constants into integer registers when soft-float,
13257 because there is no soft-float pattern with a r/F constraint.
13258 - We can't load FP constants into integer registers for TFmode unless
13259 it is 0.0L, because there is no movtf pattern with a r/F constraint.
13260 - Try and reload integer constants (symbolic or otherwise) back into
13261 registers directly, rather than having them dumped to memory. */
13262
13263 static reg_class_t
13264 sparc_preferred_reload_class (rtx x, reg_class_t rclass)
13265 {
13266 machine_mode mode = GET_MODE (x);
13267 if (CONSTANT_P (x))
13268 {
13269 if (FP_REG_CLASS_P (rclass)
13270 || rclass == GENERAL_OR_FP_REGS
13271 || rclass == GENERAL_OR_EXTRA_FP_REGS
13272 || (GET_MODE_CLASS (mode) == MODE_FLOAT && ! TARGET_FPU)
13273 || (mode == TFmode && ! const_zero_operand (x, mode)))
13274 return NO_REGS;
13275
13276 if (GET_MODE_CLASS (mode) == MODE_INT)
13277 return GENERAL_REGS;
13278
13279 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
13280 {
13281 if (! FP_REG_CLASS_P (rclass)
13282 || !(const_zero_operand (x, mode)
13283 || const_all_ones_operand (x, mode)))
13284 return NO_REGS;
13285 }
13286 }
13287
13288 if (TARGET_VIS3
13289 && ! TARGET_ARCH64
13290 && (rclass == EXTRA_FP_REGS
13291 || rclass == GENERAL_OR_EXTRA_FP_REGS))
13292 {
13293 int regno = true_regnum (x);
13294
13295 if (SPARC_INT_REG_P (regno))
13296 return (rclass == EXTRA_FP_REGS
13297 ? FP_REGS : GENERAL_OR_FP_REGS);
13298 }
13299
13300 return rclass;
13301 }
13302
13303 /* Output a wide multiply instruction in V8+ mode. INSN is the instruction,
13304 OPERANDS are its operands and OPCODE is the mnemonic to be used. */
13305
13306 const char *
13307 output_v8plus_mult (rtx_insn *insn, rtx *operands, const char *opcode)
13308 {
13309 char mulstr[32];
13310
13311 gcc_assert (! TARGET_ARCH64);
13312
13313 if (sparc_check_64 (operands[1], insn) <= 0)
13314 output_asm_insn ("srl\t%L1, 0, %L1", operands);
13315 if (which_alternative == 1)
13316 output_asm_insn ("sllx\t%H1, 32, %H1", operands);
13317 if (GET_CODE (operands[2]) == CONST_INT)
13318 {
13319 if (which_alternative == 1)
13320 {
13321 output_asm_insn ("or\t%L1, %H1, %H1", operands);
13322 sprintf (mulstr, "%s\t%%H1, %%2, %%L0", opcode);
13323 output_asm_insn (mulstr, operands);
13324 return "srlx\t%L0, 32, %H0";
13325 }
13326 else
13327 {
13328 output_asm_insn ("sllx\t%H1, 32, %3", operands);
13329 output_asm_insn ("or\t%L1, %3, %3", operands);
13330 sprintf (mulstr, "%s\t%%3, %%2, %%3", opcode);
13331 output_asm_insn (mulstr, operands);
13332 output_asm_insn ("srlx\t%3, 32, %H0", operands);
13333 return "mov\t%3, %L0";
13334 }
13335 }
13336 else if (rtx_equal_p (operands[1], operands[2]))
13337 {
13338 if (which_alternative == 1)
13339 {
13340 output_asm_insn ("or\t%L1, %H1, %H1", operands);
13341 sprintf (mulstr, "%s\t%%H1, %%H1, %%L0", opcode);
13342 output_asm_insn (mulstr, operands);
13343 return "srlx\t%L0, 32, %H0";
13344 }
13345 else
13346 {
13347 output_asm_insn ("sllx\t%H1, 32, %3", operands);
13348 output_asm_insn ("or\t%L1, %3, %3", operands);
13349 sprintf (mulstr, "%s\t%%3, %%3, %%3", opcode);
13350 output_asm_insn (mulstr, operands);
13351 output_asm_insn ("srlx\t%3, 32, %H0", operands);
13352 return "mov\t%3, %L0";
13353 }
13354 }
13355 if (sparc_check_64 (operands[2], insn) <= 0)
13356 output_asm_insn ("srl\t%L2, 0, %L2", operands);
13357 if (which_alternative == 1)
13358 {
13359 output_asm_insn ("or\t%L1, %H1, %H1", operands);
13360 output_asm_insn ("sllx\t%H2, 32, %L1", operands);
13361 output_asm_insn ("or\t%L2, %L1, %L1", operands);
13362 sprintf (mulstr, "%s\t%%H1, %%L1, %%L0", opcode);
13363 output_asm_insn (mulstr, operands);
13364 return "srlx\t%L0, 32, %H0";
13365 }
13366 else
13367 {
13368 output_asm_insn ("sllx\t%H1, 32, %3", operands);
13369 output_asm_insn ("sllx\t%H2, 32, %4", operands);
13370 output_asm_insn ("or\t%L1, %3, %3", operands);
13371 output_asm_insn ("or\t%L2, %4, %4", operands);
13372 sprintf (mulstr, "%s\t%%3, %%4, %%3", opcode);
13373 output_asm_insn (mulstr, operands);
13374 output_asm_insn ("srlx\t%3, 32, %H0", operands);
13375 return "mov\t%3, %L0";
13376 }
13377 }
13378
13379 /* Subroutine of sparc_expand_vector_init. Emit code to initialize
13380 all fields of TARGET to ELT by means of VIS2 BSHUFFLE insn. MODE
13381 and INNER_MODE are the modes describing TARGET. */
13382
13383 static void
13384 vector_init_bshuffle (rtx target, rtx elt, machine_mode mode,
13385 machine_mode inner_mode)
13386 {
13387 rtx t1, final_insn, sel;
13388 int bmask;
13389
13390 t1 = gen_reg_rtx (mode);
13391
13392 elt = convert_modes (SImode, inner_mode, elt, true);
13393 emit_move_insn (gen_lowpart(SImode, t1), elt);
13394
13395 switch (mode)
13396 {
13397 case E_V2SImode:
13398 final_insn = gen_bshufflev2si_vis (target, t1, t1);
13399 bmask = 0x45674567;
13400 break;
13401 case E_V4HImode:
13402 final_insn = gen_bshufflev4hi_vis (target, t1, t1);
13403 bmask = 0x67676767;
13404 break;
13405 case E_V8QImode:
13406 final_insn = gen_bshufflev8qi_vis (target, t1, t1);
13407 bmask = 0x77777777;
13408 break;
13409 default:
13410 gcc_unreachable ();
13411 }
13412
13413 sel = force_reg (SImode, GEN_INT (bmask));
13414 emit_insn (gen_bmasksi_vis (gen_reg_rtx (SImode), sel, const0_rtx));
13415 emit_insn (final_insn);
13416 }
13417
13418 /* Subroutine of sparc_expand_vector_init. Emit code to initialize
13419 all fields of TARGET to ELT in V8QI by means of VIS FPMERGE insn. */
13420
13421 static void
13422 vector_init_fpmerge (rtx target, rtx elt)
13423 {
13424 rtx t1, t2, t2_low, t3, t3_low;
13425
13426 t1 = gen_reg_rtx (V4QImode);
13427 elt = convert_modes (SImode, QImode, elt, true);
13428 emit_move_insn (gen_lowpart (SImode, t1), elt);
13429
13430 t2 = gen_reg_rtx (V8QImode);
13431 t2_low = gen_lowpart (V4QImode, t2);
13432 emit_insn (gen_fpmerge_vis (t2, t1, t1));
13433
13434 t3 = gen_reg_rtx (V8QImode);
13435 t3_low = gen_lowpart (V4QImode, t3);
13436 emit_insn (gen_fpmerge_vis (t3, t2_low, t2_low));
13437
13438 emit_insn (gen_fpmerge_vis (target, t3_low, t3_low));
13439 }
13440
13441 /* Subroutine of sparc_expand_vector_init. Emit code to initialize
13442 all fields of TARGET to ELT in V4HI by means of VIS FALIGNDATA insn. */
13443
13444 static void
13445 vector_init_faligndata (rtx target, rtx elt)
13446 {
13447 rtx t1 = gen_reg_rtx (V4HImode);
13448 int i;
13449
13450 elt = convert_modes (SImode, HImode, elt, true);
13451 emit_move_insn (gen_lowpart (SImode, t1), elt);
13452
13453 emit_insn (gen_alignaddrsi_vis (gen_reg_rtx (SImode),
13454 force_reg (SImode, GEN_INT (6)),
13455 const0_rtx));
13456
13457 for (i = 0; i < 4; i++)
13458 emit_insn (gen_faligndatav4hi_vis (target, t1, target));
13459 }
13460
13461 /* Emit code to initialize TARGET to values for individual fields VALS. */
13462
13463 void
13464 sparc_expand_vector_init (rtx target, rtx vals)
13465 {
13466 const machine_mode mode = GET_MODE (target);
13467 const machine_mode inner_mode = GET_MODE_INNER (mode);
13468 const int n_elts = GET_MODE_NUNITS (mode);
13469 int i, n_var = 0;
13470 bool all_same = true;
13471 rtx mem;
13472
13473 for (i = 0; i < n_elts; i++)
13474 {
13475 rtx x = XVECEXP (vals, 0, i);
13476 if (!(CONST_SCALAR_INT_P (x) || CONST_DOUBLE_P (x) || CONST_FIXED_P (x)))
13477 n_var++;
13478
13479 if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
13480 all_same = false;
13481 }
13482
13483 if (n_var == 0)
13484 {
13485 emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
13486 return;
13487 }
13488
13489 if (GET_MODE_SIZE (inner_mode) == GET_MODE_SIZE (mode))
13490 {
13491 if (GET_MODE_SIZE (inner_mode) == 4)
13492 {
13493 emit_move_insn (gen_lowpart (SImode, target),
13494 gen_lowpart (SImode, XVECEXP (vals, 0, 0)));
13495 return;
13496 }
13497 else if (GET_MODE_SIZE (inner_mode) == 8)
13498 {
13499 emit_move_insn (gen_lowpart (DImode, target),
13500 gen_lowpart (DImode, XVECEXP (vals, 0, 0)));
13501 return;
13502 }
13503 }
13504 else if (GET_MODE_SIZE (inner_mode) == GET_MODE_SIZE (word_mode)
13505 && GET_MODE_SIZE (mode) == 2 * GET_MODE_SIZE (word_mode))
13506 {
13507 emit_move_insn (gen_highpart (word_mode, target),
13508 gen_lowpart (word_mode, XVECEXP (vals, 0, 0)));
13509 emit_move_insn (gen_lowpart (word_mode, target),
13510 gen_lowpart (word_mode, XVECEXP (vals, 0, 1)));
13511 return;
13512 }
13513
13514 if (all_same && GET_MODE_SIZE (mode) == 8)
13515 {
13516 if (TARGET_VIS2)
13517 {
13518 vector_init_bshuffle (target, XVECEXP (vals, 0, 0), mode, inner_mode);
13519 return;
13520 }
13521 if (mode == V8QImode)
13522 {
13523 vector_init_fpmerge (target, XVECEXP (vals, 0, 0));
13524 return;
13525 }
13526 if (mode == V4HImode)
13527 {
13528 vector_init_faligndata (target, XVECEXP (vals, 0, 0));
13529 return;
13530 }
13531 }
13532
13533 mem = assign_stack_temp (mode, GET_MODE_SIZE (mode));
13534 for (i = 0; i < n_elts; i++)
13535 emit_move_insn (adjust_address_nv (mem, inner_mode,
13536 i * GET_MODE_SIZE (inner_mode)),
13537 XVECEXP (vals, 0, i));
13538 emit_move_insn (target, mem);
13539 }
13540
13541 /* Implement TARGET_SECONDARY_RELOAD. */
13542
13543 static reg_class_t
13544 sparc_secondary_reload (bool in_p, rtx x, reg_class_t rclass_i,
13545 machine_mode mode, secondary_reload_info *sri)
13546 {
13547 enum reg_class rclass = (enum reg_class) rclass_i;
13548
13549 sri->icode = CODE_FOR_nothing;
13550 sri->extra_cost = 0;
13551
13552 /* We need a temporary when loading/storing a HImode/QImode value
13553 between memory and the FPU registers. This can happen when combine puts
13554 a paradoxical subreg in a float/fix conversion insn. */
13555 if (FP_REG_CLASS_P (rclass)
13556 && (mode == HImode || mode == QImode)
13557 && (GET_CODE (x) == MEM
13558 || ((GET_CODE (x) == REG || GET_CODE (x) == SUBREG)
13559 && true_regnum (x) == -1)))
13560 return GENERAL_REGS;
13561
13562 /* On 32-bit we need a temporary when loading/storing a DFmode value
13563 between unaligned memory and the upper FPU registers. */
13564 if (TARGET_ARCH32
13565 && rclass == EXTRA_FP_REGS
13566 && mode == DFmode
13567 && GET_CODE (x) == MEM
13568 && ! mem_min_alignment (x, 8))
13569 return FP_REGS;
13570
13571 if (((TARGET_CM_MEDANY
13572 && symbolic_operand (x, mode))
13573 || (TARGET_CM_EMBMEDANY
13574 && text_segment_operand (x, mode)))
13575 && ! flag_pic)
13576 {
13577 if (in_p)
13578 sri->icode = direct_optab_handler (reload_in_optab, mode);
13579 else
13580 sri->icode = direct_optab_handler (reload_out_optab, mode);
13581 return NO_REGS;
13582 }
13583
13584 if (TARGET_VIS3 && TARGET_ARCH32)
13585 {
13586 int regno = true_regnum (x);
13587
13588 /* When using VIS3 fp<-->int register moves, on 32-bit we have
13589 to move 8-byte values in 4-byte pieces. This only works via
13590 FP_REGS, and not via EXTRA_FP_REGS. Therefore if we try to
13591 move between EXTRA_FP_REGS and GENERAL_REGS, we will need
13592 an FP_REGS intermediate move. */
13593 if ((rclass == EXTRA_FP_REGS && SPARC_INT_REG_P (regno))
13594 || ((general_or_i64_p (rclass)
13595 || rclass == GENERAL_OR_FP_REGS)
13596 && SPARC_FP_REG_P (regno)))
13597 {
13598 sri->extra_cost = 2;
13599 return FP_REGS;
13600 }
13601 }
13602
13603 return NO_REGS;
13604 }
13605
13606 /* Implement TARGET_SECONDARY_MEMORY_NEEDED.
13607
13608 On SPARC when not VIS3 it is not possible to directly move data
13609 between GENERAL_REGS and FP_REGS. */
13610
13611 static bool
13612 sparc_secondary_memory_needed (machine_mode mode, reg_class_t class1,
13613 reg_class_t class2)
13614 {
13615 return ((FP_REG_CLASS_P (class1) != FP_REG_CLASS_P (class2))
13616 && (! TARGET_VIS3
13617 || GET_MODE_SIZE (mode) > 8
13618 || GET_MODE_SIZE (mode) < 4));
13619 }
13620
13621 /* Implement TARGET_SECONDARY_MEMORY_NEEDED_MODE.
13622
13623 get_secondary_mem widens its argument to BITS_PER_WORD which loses on v9
13624 because the movsi and movsf patterns don't handle r/f moves.
13625 For v8 we copy the default definition. */
13626
13627 static machine_mode
13628 sparc_secondary_memory_needed_mode (machine_mode mode)
13629 {
13630 if (TARGET_ARCH64)
13631 {
13632 if (GET_MODE_BITSIZE (mode) < 32)
13633 return mode_for_size (32, GET_MODE_CLASS (mode), 0).require ();
13634 return mode;
13635 }
13636 else
13637 {
13638 if (GET_MODE_BITSIZE (mode) < BITS_PER_WORD)
13639 return mode_for_size (BITS_PER_WORD,
13640 GET_MODE_CLASS (mode), 0).require ();
13641 return mode;
13642 }
13643 }
13644
13645 /* Emit code to conditionally move either OPERANDS[2] or OPERANDS[3] into
13646 OPERANDS[0] in MODE. OPERANDS[1] is the operator of the condition. */
13647
13648 bool
13649 sparc_expand_conditional_move (machine_mode mode, rtx *operands)
13650 {
13651 enum rtx_code rc = GET_CODE (operands[1]);
13652 machine_mode cmp_mode;
13653 rtx cc_reg, dst, cmp;
13654
13655 cmp = operands[1];
13656 if (GET_MODE (XEXP (cmp, 0)) == DImode && !TARGET_ARCH64)
13657 return false;
13658
13659 if (GET_MODE (XEXP (cmp, 0)) == TFmode && !TARGET_HARD_QUAD)
13660 cmp = sparc_emit_float_lib_cmp (XEXP (cmp, 0), XEXP (cmp, 1), rc);
13661
13662 cmp_mode = GET_MODE (XEXP (cmp, 0));
13663 rc = GET_CODE (cmp);
13664
13665 dst = operands[0];
13666 if (! rtx_equal_p (operands[2], dst)
13667 && ! rtx_equal_p (operands[3], dst))
13668 {
13669 if (reg_overlap_mentioned_p (dst, cmp))
13670 dst = gen_reg_rtx (mode);
13671
13672 emit_move_insn (dst, operands[3]);
13673 }
13674 else if (operands[2] == dst)
13675 {
13676 operands[2] = operands[3];
13677
13678 if (GET_MODE_CLASS (cmp_mode) == MODE_FLOAT)
13679 rc = reverse_condition_maybe_unordered (rc);
13680 else
13681 rc = reverse_condition (rc);
13682 }
13683
13684 if (XEXP (cmp, 1) == const0_rtx
13685 && GET_CODE (XEXP (cmp, 0)) == REG
13686 && cmp_mode == DImode
13687 && v9_regcmp_p (rc))
13688 cc_reg = XEXP (cmp, 0);
13689 else
13690 cc_reg = gen_compare_reg_1 (rc, XEXP (cmp, 0), XEXP (cmp, 1));
13691
13692 cmp = gen_rtx_fmt_ee (rc, GET_MODE (cc_reg), cc_reg, const0_rtx);
13693
13694 emit_insn (gen_rtx_SET (dst,
13695 gen_rtx_IF_THEN_ELSE (mode, cmp, operands[2], dst)));
13696
13697 if (dst != operands[0])
13698 emit_move_insn (operands[0], dst);
13699
13700 return true;
13701 }
13702
13703 /* Emit code to conditionally move a combination of OPERANDS[1] and OPERANDS[2]
13704 into OPERANDS[0] in MODE, depending on the mask in OPERANDS[3]. CODE is the
13705 machine code to be used for the cmask instruction. */
13706
13707 void
13708 sparc_expand_vcond_mask (machine_mode mode, rtx *operands, int code)
13709 {
13710 rtx gsr = gen_rtx_REG (DImode, SPARC_GSR_REG);
13711 rtx cmask = gen_rtx_UNSPEC (DImode,
13712 gen_rtvec (2, operands[3], gsr),
13713 code);
13714 rtx bshuf = gen_rtx_UNSPEC (mode,
13715 gen_rtvec (3, operands[1], operands[2], gsr),
13716 UNSPEC_BSHUFFLE);
13717 emit_insn (gen_rtx_SET (gsr, cmask));
13718 emit_insn (gen_rtx_SET (operands[0], bshuf));
13719 }
13720
13721 /* On the SPARC, any mode which naturally allocates into the single float
13722 registers should return 4 here. */
13723
13724 unsigned int
13725 sparc_regmode_natural_size (machine_mode mode)
13726 {
13727 const enum mode_class cl = GET_MODE_CLASS (mode);
13728
13729 if ((cl == MODE_FLOAT || cl == MODE_VECTOR_INT) && GET_MODE_SIZE (mode) <= 4)
13730 return 4;
13731
13732 return UNITS_PER_WORD;
13733 }
13734
13735 /* Implement TARGET_HARD_REGNO_NREGS.
13736
13737 On SPARC, ordinary registers hold 32 bits worth; this means both
13738 integer and floating point registers. On v9, integer regs hold 64
13739 bits worth; floating point regs hold 32 bits worth (this includes the
13740 new fp regs as even the odd ones are included in the hard register
13741 count). */
13742
13743 static unsigned int
13744 sparc_hard_regno_nregs (unsigned int regno, machine_mode mode)
13745 {
13746 if (regno == SPARC_GSR_REG)
13747 return 1;
13748 if (TARGET_ARCH64)
13749 {
13750 if (SPARC_INT_REG_P (regno) || regno == FRAME_POINTER_REGNUM)
13751 return CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD);
13752 return CEIL (GET_MODE_SIZE (mode), 4);
13753 }
13754 return CEIL (GET_MODE_SIZE (mode), UNITS_PER_WORD);
13755 }
13756
13757 /* Implement TARGET_HARD_REGNO_MODE_OK.
13758
13759 ??? Because of the funny way we pass parameters we should allow certain
13760 ??? types of float/complex values to be in integer registers during
13761 ??? RTL generation. This only matters on arch32. */
13762
13763 static bool
13764 sparc_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
13765 {
13766 return (hard_regno_mode_classes[regno] & sparc_mode_class[mode]) != 0;
13767 }
13768
13769 /* Implement TARGET_MODES_TIEABLE_P.
13770
13771 For V9 we have to deal with the fact that only the lower 32 floating
13772 point registers are 32-bit addressable. */
13773
13774 static bool
13775 sparc_modes_tieable_p (machine_mode mode1, machine_mode mode2)
13776 {
13777 enum mode_class mclass1, mclass2;
13778 unsigned short size1, size2;
13779
13780 if (mode1 == mode2)
13781 return true;
13782
13783 mclass1 = GET_MODE_CLASS (mode1);
13784 mclass2 = GET_MODE_CLASS (mode2);
13785 if (mclass1 != mclass2)
13786 return false;
13787
13788 if (! TARGET_V9)
13789 return true;
13790
13791 /* Classes are the same and we are V9 so we have to deal with upper
13792 vs. lower floating point registers. If one of the modes is a
13793 4-byte mode, and the other is not, we have to mark them as not
13794 tieable because only the lower 32 floating point register are
13795 addressable 32-bits at a time.
13796
13797 We can't just test explicitly for SFmode, otherwise we won't
13798 cover the vector mode cases properly. */
13799
13800 if (mclass1 != MODE_FLOAT && mclass1 != MODE_VECTOR_INT)
13801 return true;
13802
13803 size1 = GET_MODE_SIZE (mode1);
13804 size2 = GET_MODE_SIZE (mode2);
13805 if ((size1 > 4 && size2 == 4)
13806 || (size2 > 4 && size1 == 4))
13807 return false;
13808
13809 return true;
13810 }
13811
13812 /* Implement TARGET_CSTORE_MODE. */
13813
13814 static scalar_int_mode
13815 sparc_cstore_mode (enum insn_code icode ATTRIBUTE_UNUSED)
13816 {
13817 return (TARGET_ARCH64 ? DImode : SImode);
13818 }
13819
13820 /* Return the compound expression made of T1 and T2. */
13821
13822 static inline tree
13823 compound_expr (tree t1, tree t2)
13824 {
13825 return build2 (COMPOUND_EXPR, void_type_node, t1, t2);
13826 }
13827
13828 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV hook. */
13829
13830 static void
13831 sparc_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update)
13832 {
13833 if (!TARGET_FPU)
13834 return;
13835
13836 const unsigned HOST_WIDE_INT accrued_exception_mask = 0x1f << 5;
13837 const unsigned HOST_WIDE_INT trap_enable_mask = 0x1f << 23;
13838
13839 /* We generate the equivalent of feholdexcept (&fenv_var):
13840
13841 unsigned int fenv_var;
13842 __builtin_store_fsr (&fenv_var);
13843
13844 unsigned int tmp1_var;
13845 tmp1_var = fenv_var & ~(accrued_exception_mask | trap_enable_mask);
13846
13847 __builtin_load_fsr (&tmp1_var); */
13848
13849 tree fenv_var = create_tmp_var_raw (unsigned_type_node);
13850 TREE_ADDRESSABLE (fenv_var) = 1;
13851 tree fenv_addr = build_fold_addr_expr (fenv_var);
13852 tree stfsr = sparc_builtins[SPARC_BUILTIN_STFSR];
13853 tree hold_stfsr
13854 = build4 (TARGET_EXPR, unsigned_type_node, fenv_var,
13855 build_call_expr (stfsr, 1, fenv_addr), NULL_TREE, NULL_TREE);
13856
13857 tree tmp1_var = create_tmp_var_raw (unsigned_type_node);
13858 TREE_ADDRESSABLE (tmp1_var) = 1;
13859 tree masked_fenv_var
13860 = build2 (BIT_AND_EXPR, unsigned_type_node, fenv_var,
13861 build_int_cst (unsigned_type_node,
13862 ~(accrued_exception_mask | trap_enable_mask)));
13863 tree hold_mask
13864 = build4 (TARGET_EXPR, unsigned_type_node, tmp1_var, masked_fenv_var,
13865 NULL_TREE, NULL_TREE);
13866
13867 tree tmp1_addr = build_fold_addr_expr (tmp1_var);
13868 tree ldfsr = sparc_builtins[SPARC_BUILTIN_LDFSR];
13869 tree hold_ldfsr = build_call_expr (ldfsr, 1, tmp1_addr);
13870
13871 *hold = compound_expr (compound_expr (hold_stfsr, hold_mask), hold_ldfsr);
13872
13873 /* We reload the value of tmp1_var to clear the exceptions:
13874
13875 __builtin_load_fsr (&tmp1_var); */
13876
13877 *clear = build_call_expr (ldfsr, 1, tmp1_addr);
13878
13879 /* We generate the equivalent of feupdateenv (&fenv_var):
13880
13881 unsigned int tmp2_var;
13882 __builtin_store_fsr (&tmp2_var);
13883
13884 __builtin_load_fsr (&fenv_var);
13885
13886 if (SPARC_LOW_FE_EXCEPT_VALUES)
13887 tmp2_var >>= 5;
13888 __atomic_feraiseexcept ((int) tmp2_var); */
13889
13890 tree tmp2_var = create_tmp_var_raw (unsigned_type_node);
13891 TREE_ADDRESSABLE (tmp2_var) = 1;
13892 tree tmp2_addr = build_fold_addr_expr (tmp2_var);
13893 tree update_stfsr
13894 = build4 (TARGET_EXPR, unsigned_type_node, tmp2_var,
13895 build_call_expr (stfsr, 1, tmp2_addr), NULL_TREE, NULL_TREE);
13896
13897 tree update_ldfsr = build_call_expr (ldfsr, 1, fenv_addr);
13898
13899 tree atomic_feraiseexcept
13900 = builtin_decl_implicit (BUILT_IN_ATOMIC_FERAISEEXCEPT);
13901 tree update_call
13902 = build_call_expr (atomic_feraiseexcept, 1,
13903 fold_convert (integer_type_node, tmp2_var));
13904
13905 if (SPARC_LOW_FE_EXCEPT_VALUES)
13906 {
13907 tree shifted_tmp2_var
13908 = build2 (RSHIFT_EXPR, unsigned_type_node, tmp2_var,
13909 build_int_cst (unsigned_type_node, 5));
13910 tree update_shift
13911 = build2 (MODIFY_EXPR, void_type_node, tmp2_var, shifted_tmp2_var);
13912 update_call = compound_expr (update_shift, update_call);
13913 }
13914
13915 *update
13916 = compound_expr (compound_expr (update_stfsr, update_ldfsr), update_call);
13917 }
13918
13919 /* Implement TARGET_CAN_CHANGE_MODE_CLASS. Borrowed from the PA port.
13920
13921 SImode loads to floating-point registers are not zero-extended.
13922 The definition for LOAD_EXTEND_OP specifies that integer loads
13923 narrower than BITS_PER_WORD will be zero-extended. As a result,
13924 we inhibit changes from SImode unless they are to a mode that is
13925 identical in size.
13926
13927 Likewise for SFmode, since word-mode paradoxical subregs are
13928 problematic on big-endian architectures. */
13929
13930 static bool
13931 sparc_can_change_mode_class (machine_mode from, machine_mode to,
13932 reg_class_t rclass)
13933 {
13934 if (TARGET_ARCH64
13935 && GET_MODE_SIZE (from) == 4
13936 && GET_MODE_SIZE (to) != 4)
13937 return !reg_classes_intersect_p (rclass, FP_REGS);
13938 return true;
13939 }
13940
13941 /* Implement TARGET_CONSTANT_ALIGNMENT. */
13942
13943 static HOST_WIDE_INT
13944 sparc_constant_alignment (const_tree exp, HOST_WIDE_INT align)
13945 {
13946 if (TREE_CODE (exp) == STRING_CST)
13947 return MAX (align, FASTEST_ALIGNMENT);
13948 return align;
13949 }
13950
13951 /* Implement TARGET_ZERO_CALL_USED_REGS.
13952
13953 Generate a sequence of instructions that zero registers specified by
13954 NEED_ZEROED_HARDREGS. Return the ZEROED_HARDREGS that are actually
13955 zeroed. */
13956
13957 static HARD_REG_SET
13958 sparc_zero_call_used_regs (HARD_REG_SET need_zeroed_hardregs)
13959 {
13960 for (unsigned int regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
13961 if (TEST_HARD_REG_BIT (need_zeroed_hardregs, regno))
13962 {
13963 /* Do not touch the CC registers or the FP registers if no VIS. */
13964 if (regno >= SPARC_FCC_REG
13965 || (regno >= SPARC_FIRST_FP_REG && !TARGET_VIS))
13966 CLEAR_HARD_REG_BIT (need_zeroed_hardregs, regno);
13967
13968 /* Do not access the odd upper FP registers individually. */
13969 else if (regno >= SPARC_FIRST_V9_FP_REG && (regno & 1))
13970 ;
13971
13972 /* Use the most natural mode for the registers, which is not given by
13973 regno_reg_rtx/reg_raw_mode for the FP registers on the SPARC. */
13974 else
13975 {
13976 machine_mode mode;
13977 rtx reg;
13978
13979 if (regno < SPARC_FIRST_FP_REG)
13980 {
13981 reg = regno_reg_rtx[regno];
13982 mode = GET_MODE (reg);
13983 }
13984 else
13985 {
13986 mode = regno < SPARC_FIRST_V9_FP_REG ? SFmode : DFmode;
13987 reg = gen_raw_REG (mode, regno);
13988 }
13989
13990 emit_move_insn (reg, CONST0_RTX (mode));
13991 }
13992 }
13993
13994 return need_zeroed_hardregs;
13995 }
13996
13997 /* Implement TARGET_C_MODE_FOR_FLOATING_TYPE. Return TFmode or DFmode
13998 for TI_LONG_DOUBLE_TYPE and the default for others. */
13999
14000 static machine_mode
14001 sparc_c_mode_for_floating_type (enum tree_index ti)
14002 {
14003 if (ti == TI_LONG_DOUBLE_TYPE)
14004 return SPARC_LONG_DOUBLE_TYPE_SIZE == 128 ? TFmode : DFmode;
14005 return default_mode_for_floating_type (ti);
14006 }
14007
14008 #include "gt-sparc.h"