Debian Codegraph @

gcc/config/gcn/gcn.cc

raw
1 /* Copyright (C) 2016-2025 Free Software Foundation, Inc.
2
3 This file is free software; you can redistribute it and/or modify it under
4 the terms of the GNU General Public License as published by the Free
5 Software Foundation; either version 3 of the License, or (at your option)
6 any later version.
7
8 This file is distributed in the hope that it will be useful, but WITHOUT
9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with GCC; see the file COPYING3. If not see
15 <http://www.gnu.org/licenses/>. */
16
17 /* {{{ Includes. */
18
19 /* We want GET_MODE_SIZE et al to return integers, please. */
20 #define IN_TARGET_CODE 1
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "memmodel.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "df.h"
31 #include "tm_p.h"
32 #include "stringpool.h"
33 #include "optabs.h"
34 #include "regs.h"
35 #include "emit-rtl.h"
36 #include "recog.h"
37 #include "diagnostic-core.h"
38 #include "insn-attr.h"
39 #include "fold-const.h"
40 #include "calls.h"
41 #include "explow.h"
42 #include "expr.h"
43 #include "output.h"
44 #include "cfgrtl.h"
45 #include "langhooks.h"
46 #include "builtins.h"
47 #include "omp-general.h"
48 #include "print-rtl.h"
49 #include "attribs.h"
50 #include "varasm.h"
51 #include "intl.h"
52 #include "rtl-iter.h"
53 #include "dwarf2.h"
54 #include "gimple.h"
55 #include "cgraph.h"
56 #include "case-cfn-macros.h"
57
58 /* This file should be included last. */
59 #include "target-def.h"
60
61 /* }}} */
62 /* {{{ Global variables. */
63
64 /* Constants used by FP instructions. */
65
66 static REAL_VALUE_TYPE dconst4, dconst1over2pi;
67 static bool ext_gcn_constants_init = 0;
68
69 /* Holds the ISA variant, derived from the command line parameters. */
70
71 enum gcn_isa gcn_isa = ISA_GCN5; /* Default to GCN5. */
72
73 /* Reserve this much space for LDS (for propagating variables from
74 worker-single mode to worker-partitioned mode), per workgroup. Global
75 analysis could calculate an exact bound, but we don't do that yet.
76
77 We want to permit full occupancy, so size accordingly. */
78
79 /* Use this as a default, but allow it to grow if the user requests a large
80 amount of gang-private shared-memory space. */
81 static int acc_lds_size = 0x600;
82
83 #define OMP_LDS_SIZE 0x600 /* 0x600 is 1/40 total, rounded down. */
84 #define ACC_LDS_SIZE acc_lds_size
85 #define OTHER_LDS_SIZE 65536 /* If in doubt, reserve all of it. */
86
87 #define LDS_SIZE (flag_openacc ? ACC_LDS_SIZE \
88 : flag_openmp ? OMP_LDS_SIZE \
89 : OTHER_LDS_SIZE)
90
91 static int gang_private_hwm = 32;
92 static hash_map<tree, int> lds_allocs;
93
94 /* The number of registers usable by normal non-kernel functions.
95 The SGPR count includes any special extra registers such as VCC. */
96
97 #define MAX_NORMAL_SGPR_COUNT 62 // i.e. 64 with VCC
98 #define MAX_NORMAL_VGPR_COUNT 24
99 #define MAX_NORMAL_AVGPR_COUNT 24
100
101 /* Import all the data from gcn-devices.def.
102 The PROCESSOR_GFXnnn should be indices for this table. */
103 const struct gcn_device_def gcn_devices[] = {
104 #define GCN_DEVICE(name, NAME, ELF, ISA, XNACK, SRAMECC, WAVE64, CU, VGPRS, \
105 GEN_VER, ARCH_FAM, ...) \
106 {PROCESSOR_ ## NAME, #name, #NAME, ISA, XNACK, SRAMECC, WAVE64, CU, VGPRS, \
107 GEN_VER, #ARCH_FAM},
108 #include "gcn-devices.def"
109 };
110
111 /* }}} */
112 /* {{{ Initialization and options. */
113
114 /* Initialize machine_function. */
115
116 static struct machine_function *
117 gcn_init_machine_status (void)
118 {
119 struct machine_function *f;
120
121 f = ggc_cleared_alloc<machine_function> ();
122
123 // FIXME: re-enable global addressing with safety for LDS-flat addresses
124 //if (TARGET_GCN3)
125 f->use_flat_addressing = true;
126
127 return f;
128 }
129
130 /* Implement TARGET_OPTION_OVERRIDE.
131
132 Override option settings where defaults are variable, or we have specific
133 needs to consider. */
134
135 static void
136 gcn_option_override (void)
137 {
138 init_machine_status = gcn_init_machine_status;
139
140 /* The HSA runtime does not respect ELF load addresses, so force PIE. */
141 if (!flag_pie)
142 flag_pie = 2;
143 if (!flag_pic)
144 flag_pic = flag_pie;
145
146 gcc_assert (gcn_arch >= 0 && gcn_arch < PROCESSOR_COUNT);
147 gcn_isa = gcn_devices[gcn_arch].isa;
148
149 /* Reserve 1Kb (somewhat arbitrarily) of LDS space for reduction results and
150 worker broadcasts. */
151 if (gang_private_size_opt == -1)
152 gang_private_size_opt = 512;
153 else if (gang_private_size_opt < gang_private_hwm)
154 gang_private_size_opt = gang_private_hwm;
155 else if (gang_private_size_opt >= acc_lds_size - 1024)
156 {
157 /* We need some space for reductions and worker broadcasting. If the
158 user requests a large amount of gang-private LDS space, we might not
159 have enough left for the former. Increase the LDS allocation in that
160 case, although this may reduce the maximum occupancy on the
161 hardware. */
162 acc_lds_size = gang_private_size_opt + 1024;
163 if (acc_lds_size > 32768)
164 acc_lds_size = 32768;
165 }
166
167 /* gfx1030 and gfx1100 do not support XNACK. */
168 if (gcn_devices[gcn_arch].xnack_default == HSACO_ATTR_UNSUPPORTED)
169 {
170 if (flag_xnack == HSACO_ATTR_ON)
171 error ("%<-mxnack=on%> is incompatible with %<-march=%s%>",
172 gcn_devices[gcn_arch].name);
173 /* Allow HSACO_ATTR_ANY silently. */
174 flag_xnack = HSACO_ATTR_UNSUPPORTED;
175 }
176
177 /* There's no need for XNACK on devices without USM, and there are register
178 allocation problems caused by the early-clobber when AVGPR spills are not
179 available.
180 FIXME: can the regalloc mean the default can be really "any"? */
181 if (flag_xnack == HSACO_ATTR_DEFAULT)
182 flag_xnack = gcn_devices[gcn_arch].xnack_default;
183
184 if (flag_sram_ecc == HSACO_ATTR_DEFAULT)
185 flag_sram_ecc = gcn_devices[gcn_arch].sramecc_default;
186 }
187
188 /* }}} */
189 /* {{{ Attributes. */
190
191 /* This table defines the arguments that are permitted in
192 __attribute__ ((amdgpu_hsa_kernel (...))).
193
194 The names and values correspond to the HSA metadata that is encoded
195 into the assembler file and binary. */
196
197 static const struct gcn_kernel_arg_type
198 {
199 const char *name;
200 const char *header_pseudo;
201 machine_mode mode;
202
203 /* This should be set to -1 or -2 for a dynamically allocated register
204 number. Use -1 if this argument contributes to the user_sgpr_count,
205 -2 otherwise. */
206 int fixed_regno;
207 } gcn_kernel_arg_types[] = {
208 {"exec", NULL, DImode, EXEC_REG},
209 #define PRIVATE_SEGMENT_BUFFER_ARG 1
210 {"private_segment_buffer",
211 ".amdhsa_user_sgpr_private_segment_buffer", TImode, -1},
212 #define DISPATCH_PTR_ARG 2
213 {"dispatch_ptr", ".amdhsa_user_sgpr_dispatch_ptr", DImode, -1},
214 #define QUEUE_PTR_ARG 3
215 {"queue_ptr", ".amdhsa_user_sgpr_queue_ptr", DImode, -1},
216 #define KERNARG_SEGMENT_PTR_ARG 4
217 {"kernarg_segment_ptr", ".amdhsa_user_sgpr_kernarg_segment_ptr", DImode, -1},
218 {"dispatch_id", ".amdhsa_user_sgpr_dispatch_id", DImode, -1},
219 #define FLAT_SCRATCH_INIT_ARG 6
220 {"flat_scratch_init", ".amdhsa_user_sgpr_flat_scratch_init", DImode, -1},
221 #define FLAT_SCRATCH_SEGMENT_SIZE_ARG 7
222 {"private_segment_size", ".amdhsa_user_sgpr_private_segment_size", SImode, -1},
223 #define WORKGROUP_ID_X_ARG 8
224 {"workgroup_id_X", ".amdhsa_system_sgpr_workgroup_id_x", SImode, -2},
225 {"workgroup_id_Y", ".amdhsa_system_sgpr_workgroup_id_y", SImode, -2},
226 {"workgroup_id_Z", ".amdhsa_system_sgpr_workgroup_id_z", SImode, -2},
227 {"workgroup_info", ".amdhsa_system_sgpr_workgroup_info", SImode, -1},
228 #define PRIVATE_SEGMENT_WAVE_OFFSET_ARG 12
229 {"private_segment_wave_offset",
230 ".amdhsa_system_sgpr_private_segment_wavefront_offset", SImode, -2},
231 #define WORK_ITEM_ID_X_ARG 13
232 {"work_item_id_X", NULL, V64SImode, FIRST_VGPR_REG},
233 #define WORK_ITEM_ID_Y_ARG 14
234 {"work_item_id_Y", NULL, V64SImode, FIRST_VGPR_REG + 1},
235 #define WORK_ITEM_ID_Z_ARG 15
236 {"work_item_id_Z", NULL, V64SImode, FIRST_VGPR_REG + 2}
237 };
238
239 static const long default_requested_args
240 = (1 << DISPATCH_PTR_ARG)
241 | (1 << QUEUE_PTR_ARG)
242 | (1 << KERNARG_SEGMENT_PTR_ARG)
243 | (1 << WORKGROUP_ID_X_ARG)
244 | (1 << WORK_ITEM_ID_X_ARG)
245 | (1 << WORK_ITEM_ID_Y_ARG)
246 | (1 << WORK_ITEM_ID_Z_ARG);
247
248 /* Extract parameter settings from __attribute__((amdgpu_hsa_kernel ())).
249 This function also sets the default values for some arguments.
250
251 Return true on success, with ARGS populated. */
252
253 static bool
254 gcn_parse_amdgpu_hsa_kernel_attribute (struct gcn_kernel_args *args,
255 tree list)
256 {
257 bool err = false;
258 args->requested = default_requested_args;
259 args->nargs = 0;
260
261 for (int a = 0; a < GCN_KERNEL_ARG_TYPES; a++)
262 args->reg[a] = -1;
263
264 for (; list; list = TREE_CHAIN (list))
265 {
266 const char *str;
267 if (TREE_CODE (TREE_VALUE (list)) != STRING_CST)
268 {
269 error ("%<amdgpu_hsa_kernel%> attribute requires string constant "
270 "arguments");
271 break;
272 }
273 str = TREE_STRING_POINTER (TREE_VALUE (list));
274 int a;
275 for (a = 0; a < GCN_KERNEL_ARG_TYPES; a++)
276 {
277 if (!strcmp (str, gcn_kernel_arg_types[a].name))
278 break;
279 }
280 if (a == GCN_KERNEL_ARG_TYPES)
281 {
282 error ("unknown specifier %qs in %<amdgpu_hsa_kernel%> attribute",
283 str);
284 err = true;
285 break;
286 }
287 if (args->requested & (1 << a))
288 {
289 error ("duplicated parameter specifier %qs in %<amdgpu_hsa_kernel%> "
290 "attribute", str);
291 err = true;
292 break;
293 }
294 args->requested |= (1 << a);
295 args->order[args->nargs++] = a;
296 }
297
298 /* Requesting WORK_ITEM_ID_Z_ARG implies requesting WORK_ITEM_ID_X_ARG and
299 WORK_ITEM_ID_Y_ARG. Similarly, requesting WORK_ITEM_ID_Y_ARG implies
300 requesting WORK_ITEM_ID_X_ARG. */
301 if (args->requested & (1 << WORK_ITEM_ID_Z_ARG))
302 args->requested |= (1 << WORK_ITEM_ID_Y_ARG);
303 if (args->requested & (1 << WORK_ITEM_ID_Y_ARG))
304 args->requested |= (1 << WORK_ITEM_ID_X_ARG);
305
306 int sgpr_regno = FIRST_SGPR_REG;
307 args->nsgprs = 0;
308 for (int a = 0; a < GCN_KERNEL_ARG_TYPES; a++)
309 {
310 if (!(args->requested & (1 << a)))
311 continue;
312
313 if (gcn_kernel_arg_types[a].fixed_regno >= 0)
314 args->reg[a] = gcn_kernel_arg_types[a].fixed_regno;
315 else
316 {
317 int reg_count;
318
319 switch (gcn_kernel_arg_types[a].mode)
320 {
321 case E_SImode:
322 reg_count = 1;
323 break;
324 case E_DImode:
325 reg_count = 2;
326 break;
327 case E_TImode:
328 reg_count = 4;
329 break;
330 default:
331 gcc_unreachable ();
332 }
333 args->reg[a] = sgpr_regno;
334 sgpr_regno += reg_count;
335 if (gcn_kernel_arg_types[a].fixed_regno == -1)
336 args->nsgprs += reg_count;
337 }
338 }
339 if (sgpr_regno > FIRST_SGPR_REG + 16)
340 {
341 error ("too many arguments passed in sgpr registers");
342 }
343 return err;
344 }
345
346 /* Referenced by TARGET_ATTRIBUTE_TABLE.
347
348 Validates target specific attributes. */
349
350 static tree
351 gcn_handle_amdgpu_hsa_kernel_attribute (tree *node, tree name,
352 tree args, int, bool *no_add_attrs)
353 {
354 if (!FUNC_OR_METHOD_TYPE_P (*node))
355 {
356 warning (OPT_Wattributes, "%qE attribute only applies to functions",
357 name);
358 *no_add_attrs = true;
359 return NULL_TREE;
360 }
361
362 /* Can combine regparm with all attributes but fastcall, and thiscall. */
363 if (is_attribute_p ("gcnhsa_kernel", name))
364 {
365 struct gcn_kernel_args kernelarg;
366
367 if (gcn_parse_amdgpu_hsa_kernel_attribute (&kernelarg, args))
368 *no_add_attrs = true;
369
370 return NULL_TREE;
371 }
372
373 return NULL_TREE;
374 }
375
376 /* Implement TARGET_ATTRIBUTE_TABLE.
377
378 Create target-specific __attribute__ types. */
379
380 TARGET_GNU_ATTRIBUTES (gcn_attribute_table, {
381 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
382 affects_type_identity } */
383 {"amdgpu_hsa_kernel", 0, GCN_KERNEL_ARG_TYPES, false, true,
384 true, true, gcn_handle_amdgpu_hsa_kernel_attribute, NULL}
385 });
386
387 /* }}} */
388 /* {{{ Registers and modes. */
389
390 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P. */
391
392 bool
393 gcn_scalar_mode_supported_p (scalar_mode mode)
394 {
395 return (mode == BImode
396 || mode == QImode
397 || mode == HImode /* || mode == HFmode */
398 || mode == SImode || mode == SFmode
399 || mode == DImode || mode == DFmode
400 || mode == TImode);
401 }
402
403 /* Return a vector mode with N lanes of MODE. */
404
405 static machine_mode
406 VnMODE (int n, machine_mode mode)
407 {
408 switch (mode)
409 {
410 case E_QImode:
411 switch (n)
412 {
413 case 2: return V2QImode;
414 case 4: return V4QImode;
415 case 8: return V8QImode;
416 case 16: return V16QImode;
417 case 32: return V32QImode;
418 case 64: return V64QImode;
419 }
420 break;
421 case E_HImode:
422 switch (n)
423 {
424 case 2: return V2HImode;
425 case 4: return V4HImode;
426 case 8: return V8HImode;
427 case 16: return V16HImode;
428 case 32: return V32HImode;
429 case 64: return V64HImode;
430 }
431 break;
432 case E_HFmode:
433 switch (n)
434 {
435 case 2: return V2HFmode;
436 case 4: return V4HFmode;
437 case 8: return V8HFmode;
438 case 16: return V16HFmode;
439 case 32: return V32HFmode;
440 case 64: return V64HFmode;
441 }
442 break;
443 case E_SImode:
444 switch (n)
445 {
446 case 2: return V2SImode;
447 case 4: return V4SImode;
448 case 8: return V8SImode;
449 case 16: return V16SImode;
450 case 32: return V32SImode;
451 case 64: return V64SImode;
452 }
453 break;
454 case E_SFmode:
455 switch (n)
456 {
457 case 2: return V2SFmode;
458 case 4: return V4SFmode;
459 case 8: return V8SFmode;
460 case 16: return V16SFmode;
461 case 32: return V32SFmode;
462 case 64: return V64SFmode;
463 }
464 break;
465 case E_DImode:
466 switch (n)
467 {
468 case 2: return V2DImode;
469 case 4: return V4DImode;
470 case 8: return V8DImode;
471 case 16: return V16DImode;
472 case 32: return V32DImode;
473 case 64: return V64DImode;
474 }
475 break;
476 case E_DFmode:
477 switch (n)
478 {
479 case 2: return V2DFmode;
480 case 4: return V4DFmode;
481 case 8: return V8DFmode;
482 case 16: return V16DFmode;
483 case 32: return V32DFmode;
484 case 64: return V64DFmode;
485 }
486 break;
487 default:
488 break;
489 }
490
491 return VOIDmode;
492 }
493
494 /* Implement TARGET_CLASS_MAX_NREGS.
495
496 Return the number of hard registers needed to hold a value of MODE in
497 a register of class RCLASS. */
498
499 static unsigned char
500 gcn_class_max_nregs (reg_class_t rclass, machine_mode mode)
501 {
502 /* Scalar registers are 32bit, vector registers are in fact tuples of
503 64 lanes. */
504 if (rclass == VGPR_REGS || rclass == AVGPR_REGS
505 || rclass == ALL_VGPR_REGS)
506 {
507 if (vgpr_1reg_mode_p (mode))
508 return 1;
509 if (vgpr_2reg_mode_p (mode))
510 return 2;
511 /* TImode is used by DImode compare_and_swap. */
512 if (vgpr_4reg_mode_p (mode))
513 return 4;
514 }
515 else if (rclass == VCC_CONDITIONAL_REG && mode == BImode)
516 return 2;
517
518 /* Vector modes in SGPRs are not supposed to happen (disallowed by
519 gcn_hard_regno_mode_ok), but there are some patterns that have an "Sv"
520 constraint and are used by splitters, post-reload.
521 This ensures that we don't accidentally mark the following 63 scalar
522 registers as "live". */
523 if (rclass == SGPR_REGS && VECTOR_MODE_P (mode))
524 return CEIL (GET_MODE_SIZE (GET_MODE_INNER (mode)), 4);
525
526 return CEIL (GET_MODE_SIZE (mode), 4);
527 }
528
529 /* Implement TARGET_HARD_REGNO_NREGS.
530
531 Return the number of hard registers needed to hold a value of MODE in
532 REGNO. */
533
534 unsigned int
535 gcn_hard_regno_nregs (unsigned int regno, machine_mode mode)
536 {
537 return gcn_class_max_nregs (REGNO_REG_CLASS (regno), mode);
538 }
539
540 /* Implement TARGET_HARD_REGNO_MODE_OK.
541
542 Return true if REGNO can hold value in MODE. */
543
544 bool
545 gcn_hard_regno_mode_ok (unsigned int regno, machine_mode mode)
546 {
547 /* Treat a complex mode as if it were a scalar mode of the same overall
548 size for the purposes of allocating hard registers. */
549 if (COMPLEX_MODE_P (mode))
550 switch (mode)
551 {
552 case E_CQImode:
553 case E_CHImode:
554 mode = SImode;
555 break;
556 case E_CSImode:
557 mode = DImode;
558 break;
559 case E_CDImode:
560 mode = TImode;
561 break;
562 case E_HCmode:
563 mode = SFmode;
564 break;
565 case E_SCmode:
566 mode = DFmode;
567 break;
568 default:
569 /* Not supported. */
570 return false;
571 }
572
573 switch (regno)
574 {
575 case FLAT_SCRATCH_LO_REG:
576 case XNACK_MASK_LO_REG:
577 case TBA_LO_REG:
578 case TMA_LO_REG:
579 return (mode == SImode || mode == DImode);
580 case VCC_LO_REG:
581 case EXEC_LO_REG:
582 return (mode == BImode || mode == SImode || mode == DImode);
583 case M0_REG:
584 case FLAT_SCRATCH_HI_REG:
585 case XNACK_MASK_HI_REG:
586 case TBA_HI_REG:
587 case TMA_HI_REG:
588 return mode == SImode;
589 case VCC_HI_REG:
590 return false;
591 case EXEC_HI_REG:
592 return mode == SImode /*|| mode == V32BImode */ ;
593 case SCC_REG:
594 case VCCZ_REG:
595 case EXECZ_REG:
596 return mode == BImode;
597 }
598 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
599 return true;
600 if (SGPR_REGNO_P (regno))
601 /* We restrict double register values to aligned registers. */
602 return (sgpr_1reg_mode_p (mode)
603 || (!((regno - FIRST_SGPR_REG) & 1) && sgpr_2reg_mode_p (mode))
604 || (((regno - FIRST_SGPR_REG) & 3) == 0 && mode == TImode));
605 if (VGPR_REGNO_P (regno) || (AVGPR_REGNO_P (regno) && TARGET_AVGPRS))
606 /* Vector instructions do not care about the alignment of register
607 pairs, but where there is no 64-bit instruction, many of the
608 define_split do not work if the input and output registers partially
609 overlap. We tried to fix this with early clobber and match
610 constraints, but it was bug prone, added complexity, and conflicts
611 with the 'U0' constraints on vec_merge.
612 Therefore, we restrict ourselved to aligned registers. */
613 return (vgpr_1reg_mode_p (mode)
614 || (!((regno - FIRST_VGPR_REG) & 1) && vgpr_2reg_mode_p (mode))
615 /* TImode is used by DImode compare_and_swap,
616 and by DIVMOD V64DImode libfuncs. */
617 || (!((regno - FIRST_VGPR_REG) & 3) && vgpr_4reg_mode_p (mode)));
618 return false;
619 }
620
621 /* Implement REGNO_REG_CLASS via gcn.h.
622
623 Return smallest class containing REGNO. */
624
625 enum reg_class
626 gcn_regno_reg_class (int regno)
627 {
628 switch (regno)
629 {
630 case SCC_REG:
631 return SCC_CONDITIONAL_REG;
632 case VCC_LO_REG:
633 case VCC_HI_REG:
634 return VCC_CONDITIONAL_REG;
635 case VCCZ_REG:
636 return VCCZ_CONDITIONAL_REG;
637 case EXECZ_REG:
638 return EXECZ_CONDITIONAL_REG;
639 case EXEC_LO_REG:
640 case EXEC_HI_REG:
641 return EXEC_MASK_REG;
642 }
643 if (VGPR_REGNO_P (regno))
644 return VGPR_REGS;
645 if (AVGPR_REGNO_P (regno))
646 return AVGPR_REGS;
647 if (SGPR_REGNO_P (regno))
648 return SGPR_REGS;
649 if (regno < FIRST_VGPR_REG)
650 return GENERAL_REGS;
651 if (regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM)
652 return AFP_REGS;
653 return ALL_REGS;
654 }
655
656 /* Implement TARGET_CAN_CHANGE_MODE_CLASS.
657
658 GCC assumes that lowpart contains first part of value as stored in memory.
659 This is not the case for vector registers. */
660
661 bool
662 gcn_can_change_mode_class (machine_mode from, machine_mode to,
663 reg_class_t regclass)
664 {
665 if (!vgpr_vector_mode_p (from) && !vgpr_vector_mode_p (to))
666 return true;
667
668 /* Vector conversions are only valid when changing mode with a fixed number
669 of lanes, or changing number of lanes with a fixed mode. Anything else
670 would require actual data movement. */
671 if (VECTOR_MODE_P (from) && VECTOR_MODE_P (to)
672 && GET_MODE_NUNITS (from) != GET_MODE_NUNITS (to)
673 && GET_MODE_INNER (from) != GET_MODE_INNER (to))
674 return false;
675
676 /* Vector/scalar conversions are only permitted when the scalar mode
677 is the same or smaller than the inner vector mode. */
678 if ((VECTOR_MODE_P (from) && !VECTOR_MODE_P (to)
679 && GET_MODE_SIZE (to) >= GET_MODE_SIZE (GET_MODE_INNER (from)))
680 || (VECTOR_MODE_P (to) && !VECTOR_MODE_P (from)
681 && GET_MODE_SIZE (from) >= GET_MODE_SIZE (GET_MODE_INNER (to))))
682 return false;
683
684 return (gcn_class_max_nregs (regclass, from)
685 == gcn_class_max_nregs (regclass, to));
686 }
687
688 /* Implement TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P.
689
690 When this hook returns true for MODE, the compiler allows
691 registers explicitly used in the rtl to be used as spill registers
692 but prevents the compiler from extending the lifetime of these
693 registers. */
694
695 bool
696 gcn_small_register_classes_for_mode_p (machine_mode mode)
697 {
698 /* We allocate into exec and vcc regs. Those make small register class. */
699 return mode == DImode || mode == SImode;
700 }
701
702 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.
703
704 Returns true if pseudos that have been assigned to registers of class RCLASS
705 would likely be spilled because registers of RCLASS are needed for spill
706 registers. */
707
708 static bool
709 gcn_class_likely_spilled_p (reg_class_t rclass)
710 {
711 return (rclass == EXEC_MASK_REG
712 || reg_classes_intersect_p (ALL_CONDITIONAL_REGS, rclass));
713 }
714
715 /* Implement TARGET_MODES_TIEABLE_P.
716
717 Returns true if a value of MODE1 is accessible in MODE2 without
718 copying. */
719
720 bool
721 gcn_modes_tieable_p (machine_mode mode1, machine_mode mode2)
722 {
723 if (VECTOR_MODE_P (mode1) || VECTOR_MODE_P (mode2))
724 {
725 int vf1 = (VECTOR_MODE_P (mode1) ? GET_MODE_NUNITS (mode1) : 1);
726 int vf2 = (VECTOR_MODE_P (mode2) ? GET_MODE_NUNITS (mode2) : 1);
727 machine_mode inner1 = (vf1 > 1 ? GET_MODE_INNER (mode1) : mode1);
728 machine_mode inner2 = (vf2 > 1 ? GET_MODE_INNER (mode2) : mode2);
729
730 return (vf1 == vf2 || (inner1 == inner2 && vf2 <= vf1));
731 }
732
733 return (GET_MODE_BITSIZE (mode1) <= MAX_FIXED_MODE_SIZE
734 && GET_MODE_BITSIZE (mode2) <= MAX_FIXED_MODE_SIZE);
735 }
736
737 /* Implement TARGET_TRULY_NOOP_TRUNCATION.
738
739 Returns true if it is safe to “convert” a value of INPREC bits to one of
740 OUTPREC bits (where OUTPREC is smaller than INPREC) by merely operating on
741 it as if it had only OUTPREC bits. */
742
743 bool
744 gcn_truly_noop_truncation (poly_uint64 outprec, poly_uint64 inprec)
745 {
746 return ((inprec <= 32) && (outprec <= inprec));
747 }
748
749 /* Return N-th part of value occupying multiple registers. */
750
751 rtx
752 gcn_operand_part (machine_mode mode, rtx op, int n)
753 {
754 int vf = VECTOR_MODE_P (mode) ? GET_MODE_NUNITS (mode) : 1;
755
756 if (vf > 1)
757 {
758 machine_mode vsimode = VnMODE (vf, SImode);
759
760 if (REG_P (op))
761 {
762 gcc_assert (REGNO (op) + n < FIRST_PSEUDO_REGISTER);
763 return gen_rtx_REG (vsimode, REGNO (op) + n);
764 }
765 if (GET_CODE (op) == CONST_VECTOR)
766 {
767 int units = GET_MODE_NUNITS (mode);
768 rtvec v = rtvec_alloc (units);
769
770 for (int i = 0; i < units; ++i)
771 RTVEC_ELT (v, i) = gcn_operand_part (GET_MODE_INNER (mode),
772 CONST_VECTOR_ELT (op, i), n);
773
774 return gen_rtx_CONST_VECTOR (vsimode, v);
775 }
776 if (GET_CODE (op) == UNSPEC && XINT (op, 1) == UNSPEC_VECTOR)
777 return gcn_gen_undef (vsimode);
778 gcc_unreachable ();
779 }
780 else if (GET_MODE_SIZE (mode) == 8 && REG_P (op))
781 {
782 gcc_assert (REGNO (op) + n < FIRST_PSEUDO_REGISTER);
783 return gen_rtx_REG (SImode, REGNO (op) + n);
784 }
785 else
786 {
787 if (GET_CODE (op) == UNSPEC && XINT (op, 1) == UNSPEC_VECTOR)
788 return gcn_gen_undef (SImode);
789
790 /* If it's a constant then let's assume it is of the largest mode
791 available, otherwise simplify_gen_subreg will fail. */
792 if (mode == VOIDmode && CONST_INT_P (op))
793 mode = DImode;
794 return simplify_gen_subreg (SImode, op, mode, n * 4);
795 }
796 }
797
798 /* Return N-th part of value occupying multiple registers. */
799
800 rtx
801 gcn_operand_doublepart (machine_mode mode, rtx op, int n)
802 {
803 return simplify_gen_subreg (DImode, op, mode, n * 8);
804 }
805
806 /* Return true if OP can be split into subregs or high/low parts.
807 This is always true for scalars, but not normally true for vectors.
808 However, for vectors in hardregs we can use the low and high registers. */
809
810 bool
811 gcn_can_split_p (machine_mode, rtx op)
812 {
813 if (vgpr_vector_mode_p (GET_MODE (op)))
814 {
815 if (GET_CODE (op) == SUBREG)
816 op = SUBREG_REG (op);
817 if (!REG_P (op))
818 return true;
819 return REGNO (op) <= FIRST_PSEUDO_REGISTER;
820 }
821 return true;
822 }
823
824 /* Implement TARGET_SPILL_CLASS.
825
826 Return class of registers which could be used for pseudo of MODE
827 and of class RCLASS for spilling instead of memory. Return NO_REGS
828 if it is not possible or non-profitable. */
829
830 static reg_class_t
831 gcn_spill_class (reg_class_t c, machine_mode /*mode */ )
832 {
833 if (reg_classes_intersect_p (ALL_CONDITIONAL_REGS, c)
834 || c == VCC_CONDITIONAL_REG || c == EXEC_MASK_REG)
835 return SGPR_REGS;
836 else
837 return c == VGPR_REGS && TARGET_AVGPRS ? AVGPR_REGS : NO_REGS;
838 }
839
840 /* Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS.
841
842 Change allocno class for given pseudo from allocno and best class
843 calculated by IRA. */
844
845 static reg_class_t
846 gcn_ira_change_pseudo_allocno_class (int regno, reg_class_t cl,
847 reg_class_t best_cl)
848 {
849 /* Avoid returning classes that contain both vgpr and sgpr registers. */
850 if (cl != ALL_REGS && cl != SRCDST_REGS && cl != ALL_GPR_REGS)
851 return cl;
852 if (best_cl != ALL_REGS && best_cl != SRCDST_REGS
853 && best_cl != ALL_GPR_REGS)
854 return best_cl;
855
856 machine_mode mode = PSEUDO_REGNO_MODE (regno);
857 if (vgpr_vector_mode_p (mode))
858 return VGPR_REGS;
859
860 return GENERAL_REGS;
861 }
862
863 /* Create a new DImode pseudo reg and emit an instruction to initialize
864 it to VAL. */
865
866 rtx
867 get_exec (int64_t val)
868 {
869 rtx reg = gen_reg_rtx (DImode);
870 emit_insn (gen_rtx_SET (reg, gen_int_mode (val, DImode)));
871 return reg;
872 }
873
874 rtx
875 get_exec (machine_mode mode)
876 {
877 int vf = (VECTOR_MODE_P (mode) ? GET_MODE_NUNITS (mode) : 1);
878 return get_exec (0xffffffffffffffffUL >> (64-vf));
879 }
880
881 /* }}} */
882 /* {{{ Immediate constants. */
883
884 /* Initialize shared numeric constants. */
885
886 static void
887 init_ext_gcn_constants (void)
888 {
889 real_from_integer (&dconst4, DFmode, 4, SIGNED);
890
891 /* FIXME: this constant probably does not match what hardware really loads.
892 Reality check it eventually. */
893 real_from_string (&dconst1over2pi,
894 "0.15915494309189532");
895 real_convert (&dconst1over2pi, SFmode, &dconst1over2pi);
896
897 ext_gcn_constants_init = 1;
898 }
899
900 REAL_VALUE_TYPE
901 gcn_dconst1over2pi (void)
902 {
903 if (!ext_gcn_constants_init)
904 init_ext_gcn_constants ();
905 return dconst1over2pi;
906 }
907
908 /* Return non-zero if X is a constant that can appear as an inline operand.
909 This is 0, 0.5, -0.5, 1, -1, 2, -2, 4,-4, 1/(2*pi)
910 Or a vector of those.
911 The value returned should be the encoding of this constant. */
912
913 int
914 gcn_inline_fp_constant_p (rtx x, bool allow_vector)
915 {
916 machine_mode mode = GET_MODE (x);
917 int vf = VECTOR_MODE_P (mode) ? GET_MODE_NUNITS (mode) : 1;
918
919 if (vf > 1)
920 mode = GET_MODE_INNER (mode);
921
922 if (vf > 1
923 && (mode == HFmode || mode == SFmode || mode == DFmode)
924 && allow_vector)
925 {
926 int n;
927 if (GET_CODE (x) != CONST_VECTOR)
928 return 0;
929 n = gcn_inline_fp_constant_p (CONST_VECTOR_ELT (x, 0), false);
930 if (!n)
931 return 0;
932 for (int i = 1; i < vf; i++)
933 if (CONST_VECTOR_ELT (x, i) != CONST_VECTOR_ELT (x, 0))
934 return 0;
935 return 1;
936 }
937
938 if (mode != HFmode && mode != SFmode && mode != DFmode)
939 return 0;
940
941 const REAL_VALUE_TYPE *r;
942
943 if (x == CONST0_RTX (mode))
944 return 128;
945 if (x == CONST1_RTX (mode))
946 return 242;
947
948 r = CONST_DOUBLE_REAL_VALUE (x);
949
950 if (real_identical (r, &dconstm1))
951 return 243;
952
953 if (real_identical (r, &dconsthalf))
954 return 240;
955 if (real_identical (r, &dconstm1))
956 return 243;
957 if (real_identical (r, &dconst2))
958 return 244;
959 if (real_identical (r, &dconst4))
960 return 246;
961 if (real_identical (r, &dconst1over2pi))
962 return 248;
963 if (!ext_gcn_constants_init)
964 init_ext_gcn_constants ();
965 real_value_negate (r);
966 if (real_identical (r, &dconsthalf))
967 return 241;
968 if (real_identical (r, &dconst2))
969 return 245;
970 if (real_identical (r, &dconst4))
971 return 247;
972
973 /* FIXME: add 4, -4 and 1/(2*PI). */
974
975 return 0;
976 }
977
978 /* Return non-zero if X is a constant that can appear as an immediate operand.
979 This is 0, 0.5, -0.5, 1, -1, 2, -2, 4,-4, 1/(2*pi)
980 Or a vector of those.
981 The value returned should be the encoding of this constant. */
982
983 bool
984 gcn_fp_constant_p (rtx x, bool allow_vector)
985 {
986 machine_mode mode = GET_MODE (x);
987 int vf = VECTOR_MODE_P (mode) ? GET_MODE_NUNITS (mode) : 1;
988
989 if (vf > 1)
990 mode = GET_MODE_INNER (mode);
991
992 if (vf > 1
993 && (mode == HFmode || mode == SFmode || mode == DFmode)
994 && allow_vector)
995 {
996 int n;
997 if (GET_CODE (x) != CONST_VECTOR)
998 return false;
999 n = gcn_fp_constant_p (CONST_VECTOR_ELT (x, 0), false);
1000 if (!n)
1001 return false;
1002 for (int i = 1; i < vf; i++)
1003 if (CONST_VECTOR_ELT (x, i) != CONST_VECTOR_ELT (x, 0))
1004 return false;
1005 return true;
1006 }
1007 if (mode != HFmode && mode != SFmode && mode != DFmode)
1008 return false;
1009
1010 if (gcn_inline_fp_constant_p (x, false))
1011 return true;
1012 /* FIXME: It is not clear how 32bit immediates are interpreted here. */
1013 return (mode != DFmode);
1014 }
1015
1016 /* Return true if X is a constant representable as an inline immediate
1017 constant in a 32-bit instruction encoding. */
1018
1019 bool
1020 gcn_inline_constant_p (rtx x)
1021 {
1022 if (GET_CODE (x) == CONST_INT)
1023 return INTVAL (x) >= -16 && INTVAL (x) <= 64;
1024 if (GET_CODE (x) == CONST_DOUBLE)
1025 return gcn_inline_fp_constant_p (x, false);
1026 if (GET_CODE (x) == CONST_VECTOR)
1027 {
1028 int n;
1029 if (!vgpr_vector_mode_p (GET_MODE (x)))
1030 return false;
1031 n = gcn_inline_constant_p (CONST_VECTOR_ELT (x, 0));
1032 if (!n)
1033 return false;
1034 for (int i = 1; i < 64; i++)
1035 if (CONST_VECTOR_ELT (x, i) != CONST_VECTOR_ELT (x, 0))
1036 return false;
1037 return 1;
1038 }
1039 return false;
1040 }
1041
1042 /* Return true if X is a constant representable as an immediate constant
1043 in a 32 or 64-bit instruction encoding. */
1044
1045 bool
1046 gcn_constant_p (rtx x)
1047 {
1048 switch (GET_CODE (x))
1049 {
1050 case CONST_INT:
1051 return true;
1052
1053 case CONST_DOUBLE:
1054 return gcn_fp_constant_p (x, false);
1055
1056 case CONST_VECTOR:
1057 {
1058 int n;
1059 if (!vgpr_vector_mode_p (GET_MODE (x)))
1060 return false;
1061 n = gcn_constant_p (CONST_VECTOR_ELT (x, 0));
1062 if (!n)
1063 return false;
1064 for (int i = 1; i < 64; i++)
1065 if (CONST_VECTOR_ELT (x, i) != CONST_VECTOR_ELT (x, 0))
1066 return false;
1067 return true;
1068 }
1069
1070 case SYMBOL_REF:
1071 case LABEL_REF:
1072 return true;
1073
1074 default:
1075 ;
1076 }
1077
1078 return false;
1079 }
1080
1081 /* Return true if X is a constant representable as two inline immediate
1082 constants in a 64-bit instruction that is split into two 32-bit
1083 instructions.
1084 When MIXED is set, the low-part is permitted to use the full 32-bits. */
1085
1086 bool
1087 gcn_inline_constant64_p (rtx x, bool mixed)
1088 {
1089 if (GET_CODE (x) == CONST_VECTOR)
1090 {
1091 if (!vgpr_vector_mode_p (GET_MODE (x)))
1092 return false;
1093 if (!gcn_inline_constant64_p (CONST_VECTOR_ELT (x, 0), mixed))
1094 return false;
1095 for (int i = 1; i < 64; i++)
1096 if (CONST_VECTOR_ELT (x, i) != CONST_VECTOR_ELT (x, 0))
1097 return false;
1098
1099 return true;
1100 }
1101
1102 if (GET_CODE (x) != CONST_INT)
1103 return false;
1104
1105 rtx val_lo = gcn_operand_part (DImode, x, 0);
1106 rtx val_hi = gcn_operand_part (DImode, x, 1);
1107 return ((mixed || gcn_inline_constant_p (val_lo))
1108 && gcn_inline_constant_p (val_hi));
1109 }
1110
1111 /* Return true if X is a constant representable as an immediate constant
1112 in a 32 or 64-bit instruction encoding where the hardware will
1113 extend the immediate to 64-bits. */
1114
1115 bool
1116 gcn_constant64_p (rtx x)
1117 {
1118 if (!gcn_constant_p (x))
1119 return false;
1120
1121 if (GET_CODE (x) != CONST_INT)
1122 return true;
1123
1124 /* Negative numbers are only allowed if they can be encoded within src0,
1125 because the 32-bit immediates do not get sign-extended.
1126 Unsigned numbers must not be encodable as 32-bit -1..-16, because the
1127 assembler will use a src0 inline immediate and that will get
1128 sign-extended. */
1129 HOST_WIDE_INT val = INTVAL (x);
1130 return (((val & 0xffffffff) == val /* Positive 32-bit. */
1131 && (val & 0xfffffff0) != 0xfffffff0) /* Not -1..-16. */
1132 || gcn_inline_constant_p (x)); /* Src0. */
1133 }
1134
1135 /* Implement TARGET_LEGITIMATE_CONSTANT_P.
1136
1137 Returns true if X is a legitimate constant for a MODE immediate operand. */
1138
1139 bool
1140 gcn_legitimate_constant_p (machine_mode, rtx x)
1141 {
1142 return gcn_constant_p (x);
1143 }
1144
1145 /* Return true if X is a CONST_VECTOR of single constant. */
1146
1147 static bool
1148 single_cst_vector_p (rtx x)
1149 {
1150 if (GET_CODE (x) != CONST_VECTOR)
1151 return false;
1152 for (int i = 1; i < 64; i++)
1153 if (CONST_VECTOR_ELT (x, i) != CONST_VECTOR_ELT (x, 0))
1154 return false;
1155 return true;
1156 }
1157
1158 /* Create a CONST_VECTOR of duplicated value A. */
1159
1160 rtx
1161 gcn_vec_constant (machine_mode mode, int a)
1162 {
1163 /*if (!a)
1164 return CONST0_RTX (mode);
1165 if (a == -1)
1166 return CONSTM1_RTX (mode);
1167 if (a == 1)
1168 return CONST1_RTX (mode);
1169 if (a == 2)
1170 return CONST2_RTX (mode);*/
1171
1172 int units = GET_MODE_NUNITS (mode);
1173 machine_mode innermode = GET_MODE_INNER (mode);
1174
1175 rtx tem;
1176 if (FLOAT_MODE_P (innermode))
1177 {
1178 REAL_VALUE_TYPE rv;
1179 real_from_integer (&rv, VOIDmode, a, SIGNED);
1180 tem = const_double_from_real_value (rv, innermode);
1181 }
1182 else
1183 tem = gen_int_mode (a, innermode);
1184
1185 rtvec v = rtvec_alloc (units);
1186 for (int i = 0; i < units; ++i)
1187 RTVEC_ELT (v, i) = tem;
1188
1189 return gen_rtx_CONST_VECTOR (mode, v);
1190 }
1191
1192 /* Create a CONST_VECTOR of duplicated value A. */
1193
1194 rtx
1195 gcn_vec_constant (machine_mode mode, rtx a)
1196 {
1197 int units = GET_MODE_NUNITS (mode);
1198 rtvec v = rtvec_alloc (units);
1199
1200 for (int i = 0; i < units; ++i)
1201 RTVEC_ELT (v, i) = a;
1202
1203 return gen_rtx_CONST_VECTOR (mode, v);
1204 }
1205
1206 /* Create an undefined vector value, used where an insn operand is
1207 optional. */
1208
1209 rtx
1210 gcn_gen_undef (machine_mode mode)
1211 {
1212 return gen_rtx_UNSPEC (mode, gen_rtvec (1, const0_rtx), UNSPEC_VECTOR);
1213 }
1214
1215 /* }}} */
1216 /* {{{ Utility functions. */
1217
1218 /* Generalised accessor functions for instruction patterns.
1219 The machine desription '@' prefix does something similar, but as of
1220 GCC 10 is incompatible with define_subst, and anyway it doesn't
1221 auto-handle the exec feature.
1222
1223 Four macros are provided; each function only needs one:
1224
1225 GEN_VN - create accessor functions for all sizes of one mode
1226 GEN_VNM - create accessor functions for all sizes of all modes
1227 GEN_VN_NOEXEC - for insns without "_exec" variants
1228 GEN_VNM_NOEXEC - likewise
1229
1230 E.g. add<mode>3
1231 GEN_VNM (add, 3, A(rtx dest, rtx s1, rtx s2), A(dest, s1, s2)
1232
1233 gen_addvNsi3 (dst, a, b)
1234 -> calls gen_addv64si3, or gen_addv32si3, etc.
1235
1236 gen_addvNm3 (dst, a, b)
1237 -> calls gen_addv64qi3, or gen_addv2di3, etc.
1238
1239 The mode is determined from the first parameter, which must be called
1240 "dest" (or else the macro doesn't work).
1241
1242 Each function has two optional parameters at the end: merge_src and exec.
1243 If exec is non-null, the function will call the "_exec" variant of the
1244 insn. If exec is non-null but merge_src is null then an undef unspec
1245 will be created.
1246
1247 E.g. cont.
1248 gen_addvNsi3 (v64sidst, a, b, oldval, exec)
1249 -> calls gen_addv64si3_exec (v64sidst, a, b, oldval, exec)
1250
1251 gen_addvNm3 (v2qidst, a, b, NULL, exec)
1252 -> calls gen_addv2qi3_exec (v2qidst, a, b,
1253 gcn_gen_undef (V2QImode), exec)
1254 */
1255
1256 #define A(...) __VA_ARGS__
1257 #define GEN_VN_NOEXEC(PREFIX, SUFFIX, PARAMS, ARGS) \
1258 static rtx \
1259 gen_##PREFIX##vN##SUFFIX (PARAMS) \
1260 { \
1261 machine_mode mode = GET_MODE (dest); \
1262 int n = GET_MODE_NUNITS (mode); \
1263 \
1264 switch (n) \
1265 { \
1266 case 2: return gen_##PREFIX##v2##SUFFIX (ARGS); \
1267 case 4: return gen_##PREFIX##v4##SUFFIX (ARGS); \
1268 case 8: return gen_##PREFIX##v8##SUFFIX (ARGS); \
1269 case 16: return gen_##PREFIX##v16##SUFFIX (ARGS); \
1270 case 32: return gen_##PREFIX##v32##SUFFIX (ARGS); \
1271 case 64: return gen_##PREFIX##v64##SUFFIX (ARGS); \
1272 } \
1273 \
1274 gcc_unreachable (); \
1275 return NULL_RTX; \
1276 }
1277
1278 #define GEN_VNM_NOEXEC(PREFIX, SUFFIX, PARAMS, ARGS) \
1279 GEN_VN_NOEXEC (PREFIX, qi##SUFFIX, A(PARAMS), A(ARGS)) \
1280 GEN_VN_NOEXEC (PREFIX, hi##SUFFIX, A(PARAMS), A(ARGS)) \
1281 GEN_VN_NOEXEC (PREFIX, hf##SUFFIX, A(PARAMS), A(ARGS)) \
1282 GEN_VN_NOEXEC (PREFIX, si##SUFFIX, A(PARAMS), A(ARGS)) \
1283 GEN_VN_NOEXEC (PREFIX, sf##SUFFIX, A(PARAMS), A(ARGS)) \
1284 GEN_VN_NOEXEC (PREFIX, di##SUFFIX, A(PARAMS), A(ARGS)) \
1285 GEN_VN_NOEXEC (PREFIX, df##SUFFIX, A(PARAMS), A(ARGS)) \
1286 static rtx \
1287 gen_##PREFIX##vNm##SUFFIX (PARAMS) \
1288 { \
1289 machine_mode mode = GET_MODE_INNER (GET_MODE (dest)); \
1290 \
1291 switch (mode) \
1292 { \
1293 case E_QImode: return gen_##PREFIX##vNqi##SUFFIX (ARGS); \
1294 case E_HImode: return gen_##PREFIX##vNhi##SUFFIX (ARGS); \
1295 case E_HFmode: return gen_##PREFIX##vNhf##SUFFIX (ARGS); \
1296 case E_SImode: return gen_##PREFIX##vNsi##SUFFIX (ARGS); \
1297 case E_SFmode: return gen_##PREFIX##vNsf##SUFFIX (ARGS); \
1298 case E_DImode: return gen_##PREFIX##vNdi##SUFFIX (ARGS); \
1299 case E_DFmode: return gen_##PREFIX##vNdf##SUFFIX (ARGS); \
1300 default: \
1301 break; \
1302 } \
1303 \
1304 gcc_unreachable (); \
1305 return NULL_RTX; \
1306 }
1307
1308 #define GEN_VN(PREFIX, SUFFIX, PARAMS, ARGS) \
1309 static rtx \
1310 gen_##PREFIX##vN##SUFFIX (PARAMS, rtx merge_src=NULL, rtx exec=NULL) \
1311 { \
1312 machine_mode mode = GET_MODE (dest); \
1313 int n = GET_MODE_NUNITS (mode); \
1314 \
1315 if (exec && !merge_src) \
1316 merge_src = gcn_gen_undef (mode); \
1317 \
1318 if (exec) \
1319 switch (n) \
1320 { \
1321 case 2: return gen_##PREFIX##v2##SUFFIX##_exec (ARGS, merge_src, exec); \
1322 case 4: return gen_##PREFIX##v4##SUFFIX##_exec (ARGS, merge_src, exec); \
1323 case 8: return gen_##PREFIX##v8##SUFFIX##_exec (ARGS, merge_src, exec); \
1324 case 16: return gen_##PREFIX##v16##SUFFIX##_exec (ARGS, merge_src, exec); \
1325 case 32: return gen_##PREFIX##v32##SUFFIX##_exec (ARGS, merge_src, exec); \
1326 case 64: return gen_##PREFIX##v64##SUFFIX##_exec (ARGS, merge_src, exec); \
1327 } \
1328 else \
1329 switch (n) \
1330 { \
1331 case 2: return gen_##PREFIX##v2##SUFFIX (ARGS); \
1332 case 4: return gen_##PREFIX##v4##SUFFIX (ARGS); \
1333 case 8: return gen_##PREFIX##v8##SUFFIX (ARGS); \
1334 case 16: return gen_##PREFIX##v16##SUFFIX (ARGS); \
1335 case 32: return gen_##PREFIX##v32##SUFFIX (ARGS); \
1336 case 64: return gen_##PREFIX##v64##SUFFIX (ARGS); \
1337 } \
1338 \
1339 gcc_unreachable (); \
1340 return NULL_RTX; \
1341 }
1342
1343 #define GEN_VNM(PREFIX, SUFFIX, PARAMS, ARGS) \
1344 GEN_VN (PREFIX, qi##SUFFIX, A(PARAMS), A(ARGS)) \
1345 GEN_VN (PREFIX, hi##SUFFIX, A(PARAMS), A(ARGS)) \
1346 GEN_VN (PREFIX, hf##SUFFIX, A(PARAMS), A(ARGS)) \
1347 GEN_VN (PREFIX, si##SUFFIX, A(PARAMS), A(ARGS)) \
1348 GEN_VN (PREFIX, sf##SUFFIX, A(PARAMS), A(ARGS)) \
1349 GEN_VN (PREFIX, di##SUFFIX, A(PARAMS), A(ARGS)) \
1350 GEN_VN (PREFIX, df##SUFFIX, A(PARAMS), A(ARGS)) \
1351 USE_TI (GEN_VN (PREFIX, ti##SUFFIX, A(PARAMS), A(ARGS))) \
1352 static rtx \
1353 gen_##PREFIX##vNm##SUFFIX (PARAMS, rtx merge_src=NULL, rtx exec=NULL) \
1354 { \
1355 machine_mode mode = GET_MODE_INNER (GET_MODE (dest)); \
1356 \
1357 switch (mode) \
1358 { \
1359 case E_QImode: return gen_##PREFIX##vNqi##SUFFIX (ARGS, merge_src, exec); \
1360 case E_HImode: return gen_##PREFIX##vNhi##SUFFIX (ARGS, merge_src, exec); \
1361 case E_HFmode: return gen_##PREFIX##vNhf##SUFFIX (ARGS, merge_src, exec); \
1362 case E_SImode: return gen_##PREFIX##vNsi##SUFFIX (ARGS, merge_src, exec); \
1363 case E_SFmode: return gen_##PREFIX##vNsf##SUFFIX (ARGS, merge_src, exec); \
1364 case E_DImode: return gen_##PREFIX##vNdi##SUFFIX (ARGS, merge_src, exec); \
1365 case E_DFmode: return gen_##PREFIX##vNdf##SUFFIX (ARGS, merge_src, exec); \
1366 case E_TImode: \
1367 USE_TI (return gen_##PREFIX##vNti##SUFFIX (ARGS, merge_src, exec);) \
1368 default: \
1369 break; \
1370 } \
1371 \
1372 gcc_unreachable (); \
1373 return NULL_RTX; \
1374 }
1375
1376 /* These have TImode support. */
1377 #define USE_TI(ARGS) ARGS
1378 GEN_VNM (mov,, A(rtx dest, rtx src), A(dest, src))
1379 GEN_VNM (vec_duplicate,, A(rtx dest, rtx src), A(dest, src))
1380
1381 /* These do not have TImode support. */
1382 #undef USE_TI
1383 #define USE_TI(ARGS)
1384 GEN_VNM (add,3, A(rtx dest, rtx src1, rtx src2), A(dest, src1, src2))
1385 GEN_VN (add,si3_dup, A(rtx dest, rtx src1, rtx src2), A(dest, src1, src2))
1386 GEN_VN (add,si3_vcc_dup, A(rtx dest, rtx src1, rtx src2, rtx vcc),
1387 A(dest, src1, src2, vcc))
1388 GEN_VN (add,di3_sext_dup2, A(rtx dest, rtx src1, rtx src2), A(dest, src1, src2))
1389 GEN_VN (add,di3_vcc_zext_dup, A(rtx dest, rtx src1, rtx src2, rtx vcc),
1390 A(dest, src1, src2, vcc))
1391 GEN_VN (add,di3_zext_dup2, A(rtx dest, rtx src1, rtx src2), A(dest, src1, src2))
1392 GEN_VN (add,di3_vcc_zext_dup2, A(rtx dest, rtx src1, rtx src2, rtx vcc),
1393 A(dest, src1, src2, vcc))
1394 GEN_VN (addc,si3, A(rtx dest, rtx src1, rtx src2, rtx vccout, rtx vccin),
1395 A(dest, src1, src2, vccout, vccin))
1396 GEN_VN (and,si3, A(rtx dest, rtx src1, rtx src2), A(dest, src1, src2))
1397 GEN_VN (ashl,si3, A(rtx dest, rtx src, rtx shift), A(dest, src, shift))
1398 GEN_VNM_NOEXEC (ds_bpermute,, A(rtx dest, rtx addr, rtx src, rtx exec),
1399 A(dest, addr, src, exec))
1400 GEN_VNM (gather,_expr, A(rtx dest, rtx addr, rtx as, rtx vol),
1401 A(dest, addr, as, vol))
1402 GEN_VN (mul,si3_dup, A(rtx dest, rtx src1, rtx src2), A(dest, src1, src2))
1403 GEN_VN (sub,si3, A(rtx dest, rtx src1, rtx src2), A(dest, src1, src2))
1404 GEN_VN_NOEXEC (vec_series,si, A(rtx dest, rtx x, rtx c), A(dest, x, c))
1405
1406 #undef USE_TI
1407 #undef GEN_VNM
1408 #undef GEN_VN
1409 #undef GET_VN_FN
1410 #undef A
1411
1412 /* Return true if OP is a PARALLEL of CONST_INTs that form a linear
1413 series with step STEP. */
1414
1415 bool
1416 gcn_stepped_zero_int_parallel_p (rtx op, int step)
1417 {
1418 if (GET_CODE (op) != PARALLEL || !CONST_INT_P (XVECEXP (op, 0, 0)))
1419 return false;
1420
1421 unsigned HOST_WIDE_INT base = 0;
1422 for (int i = 0; i < XVECLEN (op, 0); ++i)
1423 if (!CONST_INT_P (XVECEXP (op, 0, i))
1424 || UINTVAL (XVECEXP (op, 0, i)) != base + i * step)
1425 return false;
1426
1427 return true;
1428 }
1429
1430 /* }}} */
1431 /* {{{ Addresses, pointers and moves. */
1432
1433 /* Return true is REG is a valid place to store a pointer,
1434 for instructions that require an SGPR.
1435 FIXME rename. */
1436
1437 static bool
1438 gcn_address_register_p (rtx reg, machine_mode mode, bool strict)
1439 {
1440 if (GET_CODE (reg) == SUBREG)
1441 reg = SUBREG_REG (reg);
1442
1443 if (!REG_P (reg))
1444 return false;
1445
1446 if (GET_MODE (reg) != mode)
1447 return false;
1448
1449 int regno = REGNO (reg);
1450
1451 if (regno >= FIRST_PSEUDO_REGISTER)
1452 {
1453 if (!strict)
1454 return true;
1455
1456 if (!reg_renumber)
1457 return false;
1458
1459 regno = reg_renumber[regno];
1460 }
1461
1462 return (SGPR_REGNO_P (regno) || regno == M0_REG
1463 || regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM);
1464 }
1465
1466 /* Return true is REG is a valid place to store a pointer,
1467 for instructions that require a VGPR. */
1468
1469 static bool
1470 gcn_vec_address_register_p (rtx reg, machine_mode mode, bool strict)
1471 {
1472 if (GET_CODE (reg) == SUBREG)
1473 reg = SUBREG_REG (reg);
1474
1475 if (!REG_P (reg))
1476 return false;
1477
1478 if (GET_MODE (reg) != mode)
1479 return false;
1480
1481 int regno = REGNO (reg);
1482
1483 if (regno >= FIRST_PSEUDO_REGISTER)
1484 {
1485 if (!strict)
1486 return true;
1487
1488 if (!reg_renumber)
1489 return false;
1490
1491 regno = reg_renumber[regno];
1492 }
1493
1494 return VGPR_REGNO_P (regno);
1495 }
1496
1497 /* Return true if X would be valid inside a MEM using the Flat address
1498 space. */
1499
1500 bool
1501 gcn_flat_address_p (rtx x, machine_mode mode)
1502 {
1503 bool vec_mode = (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
1504 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT);
1505
1506 if (vec_mode && gcn_address_register_p (x, DImode, false))
1507 return true;
1508
1509 if (!vec_mode && gcn_vec_address_register_p (x, DImode, false))
1510 return true;
1511
1512 if (GET_CODE (x) == PLUS
1513 && gcn_vec_address_register_p (XEXP (x, 0), DImode, false)
1514 && CONST_INT_P (XEXP (x, 1)))
1515 return true;
1516
1517 return false;
1518 }
1519
1520 /* Return true if X would be valid inside a MEM using the Scalar Flat
1521 address space. */
1522
1523 bool
1524 gcn_scalar_flat_address_p (rtx x)
1525 {
1526 if (gcn_address_register_p (x, DImode, false))
1527 return true;
1528
1529 if (GET_CODE (x) == PLUS
1530 && gcn_address_register_p (XEXP (x, 0), DImode, false)
1531 && CONST_INT_P (XEXP (x, 1)))
1532 return true;
1533
1534 return false;
1535 }
1536
1537 /* Return true if MEM X would be valid for the Scalar Flat address space. */
1538
1539 bool
1540 gcn_scalar_flat_mem_p (rtx x)
1541 {
1542 if (!MEM_P (x))
1543 return false;
1544
1545 if (GET_MODE_SIZE (GET_MODE (x)) < 4)
1546 return false;
1547
1548 return gcn_scalar_flat_address_p (XEXP (x, 0));
1549 }
1550
1551 /* Return true if X would be valid inside a MEM using the LDS or GDS
1552 address spaces. */
1553
1554 bool
1555 gcn_ds_address_p (rtx x)
1556 {
1557 if (gcn_vec_address_register_p (x, SImode, false))
1558 return true;
1559
1560 if (GET_CODE (x) == PLUS
1561 && gcn_vec_address_register_p (XEXP (x, 0), SImode, false)
1562 && CONST_INT_P (XEXP (x, 1)))
1563 return true;
1564
1565 return false;
1566 }
1567
1568 /* Return true if ADDR would be valid inside a MEM using the Global
1569 address space. */
1570
1571 bool
1572 gcn_global_address_p (rtx addr)
1573 {
1574 if (gcn_address_register_p (addr, DImode, false)
1575 || gcn_vec_address_register_p (addr, DImode, false))
1576 return true;
1577
1578 if (GET_CODE (addr) == PLUS)
1579 {
1580 rtx base = XEXP (addr, 0);
1581 rtx offset = XEXP (addr, 1);
1582 int offsetbits = (TARGET_11BIT_GLOBAL_OFFSET ? 11 : 12);
1583 bool immediate_p = (CONST_INT_P (offset)
1584 && INTVAL (offset) >= -(1 << offsetbits)
1585 && INTVAL (offset) < (1 << offsetbits));
1586
1587 if ((gcn_address_register_p (base, DImode, false)
1588 || gcn_vec_address_register_p (base, DImode, false))
1589 && immediate_p)
1590 /* SGPR + CONST or VGPR + CONST */
1591 return true;
1592
1593 if (gcn_address_register_p (base, DImode, false)
1594 && gcn_vgpr_register_operand (offset, SImode))
1595 /* SPGR + VGPR */
1596 return true;
1597
1598 if (GET_CODE (base) == PLUS
1599 && gcn_address_register_p (XEXP (base, 0), DImode, false)
1600 && gcn_vgpr_register_operand (XEXP (base, 1), SImode)
1601 && immediate_p)
1602 /* (SGPR + VGPR) + CONST */
1603 return true;
1604 }
1605
1606 return false;
1607 }
1608
1609 /* Implement TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P.
1610
1611 Recognizes RTL expressions that are valid memory addresses for an
1612 instruction. The MODE argument is the machine mode for the MEM
1613 expression that wants to use this address.
1614
1615 It only recognizes address in canonical form. LEGITIMIZE_ADDRESS should
1616 convert common non-canonical forms to canonical form so that they will
1617 be recognized. */
1618
1619 static bool
1620 gcn_addr_space_legitimate_address_p (machine_mode mode, rtx x, bool strict,
1621 addr_space_t as, code_helper = ERROR_MARK)
1622 {
1623 if (AS_SCALAR_FLAT_P (as))
1624 {
1625 if (mode == QImode || mode == HImode)
1626 return 0;
1627
1628 switch (GET_CODE (x))
1629 {
1630 case REG:
1631 return gcn_address_register_p (x, DImode, strict);
1632 /* Addresses are in the form BASE+OFFSET
1633 OFFSET is either 20bit unsigned immediate, SGPR or M0.
1634 Writes and atomics do not accept SGPR. */
1635 case PLUS:
1636 {
1637 rtx x0 = XEXP (x, 0);
1638 rtx x1 = XEXP (x, 1);
1639 if (!gcn_address_register_p (x0, DImode, strict))
1640 return false;
1641 /* FIXME: This is disabled because of the mode mismatch between
1642 SImode (for the address or m0 register) and the DImode PLUS.
1643 We'll need a zero_extend or similar.
1644
1645 if (gcn_m0_register_p (x1, SImode, strict)
1646 || gcn_address_register_p (x1, SImode, strict))
1647 return true;
1648 else*/
1649 if (GET_CODE (x1) == CONST_INT)
1650 {
1651 if (INTVAL (x1) >= 0 && INTVAL (x1) < (1 << 20)
1652 /* The low bits of the offset are ignored, even when
1653 they're meant to realign the pointer. */
1654 && !(INTVAL (x1) & 0x3))
1655 return true;
1656 }
1657 return false;
1658 }
1659
1660 default:
1661 break;
1662 }
1663 }
1664 else if (AS_SCRATCH_P (as))
1665 return gcn_address_register_p (x, SImode, strict);
1666 else if (AS_FLAT_P (as) || AS_FLAT_SCRATCH_P (as))
1667 {
1668 if (GET_CODE (x) == REG)
1669 return ((GET_MODE_CLASS (mode) == MODE_VECTOR_INT
1670 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
1671 ? gcn_address_register_p (x, DImode, strict)
1672 : gcn_vec_address_register_p (x, DImode, strict));
1673 else
1674 {
1675 if (GET_CODE (x) == PLUS)
1676 {
1677 rtx x1 = XEXP (x, 1);
1678
1679 if (VECTOR_MODE_P (mode)
1680 ? !gcn_address_register_p (x, DImode, strict)
1681 : !gcn_vec_address_register_p (x, DImode, strict))
1682 return false;
1683
1684 if (GET_CODE (x1) == CONST_INT)
1685 {
1686 if (INTVAL (x1) >= 0 && INTVAL (x1) < (1 << 12)
1687 /* The low bits of the offset are ignored, even when
1688 they're meant to realign the pointer. */
1689 && !(INTVAL (x1) & 0x3))
1690 return true;
1691 }
1692 }
1693 return false;
1694 }
1695 }
1696 else if (AS_GLOBAL_P (as))
1697 {
1698 if (GET_CODE (x) == REG)
1699 return (gcn_address_register_p (x, DImode, strict)
1700 || (!VECTOR_MODE_P (mode)
1701 && gcn_vec_address_register_p (x, DImode, strict)));
1702 else if (GET_CODE (x) == PLUS)
1703 {
1704 rtx base = XEXP (x, 0);
1705 rtx offset = XEXP (x, 1);
1706
1707 int offsetbits = (TARGET_11BIT_GLOBAL_OFFSET ? 11 : 12);
1708 bool immediate_p = (GET_CODE (offset) == CONST_INT
1709 /* Signed 12/13-bit immediate. */
1710 && INTVAL (offset) >= -(1 << offsetbits)
1711 && INTVAL (offset) < (1 << offsetbits)
1712 /* The low bits of the offset are ignored, even
1713 when they're meant to realign the pointer. */
1714 && !(INTVAL (offset) & 0x3));
1715
1716 if (!VECTOR_MODE_P (mode))
1717 {
1718 if ((gcn_address_register_p (base, DImode, strict)
1719 || gcn_vec_address_register_p (base, DImode, strict))
1720 && immediate_p)
1721 /* SGPR + CONST or VGPR + CONST */
1722 return true;
1723
1724 if (gcn_address_register_p (base, DImode, strict)
1725 && gcn_vgpr_register_operand (offset, SImode))
1726 /* SGPR + VGPR */
1727 return true;
1728
1729 if (GET_CODE (base) == PLUS
1730 && gcn_address_register_p (XEXP (base, 0), DImode, strict)
1731 && gcn_vgpr_register_operand (XEXP (base, 1), SImode)
1732 && immediate_p)
1733 /* (SGPR + VGPR) + CONST */
1734 return true;
1735 }
1736 else
1737 {
1738 if (gcn_address_register_p (base, DImode, strict)
1739 && immediate_p)
1740 /* SGPR + CONST */
1741 return true;
1742 }
1743 }
1744 else
1745 return false;
1746 }
1747 else if (AS_ANY_DS_P (as))
1748 switch (GET_CODE (x))
1749 {
1750 case REG:
1751 return (VECTOR_MODE_P (mode)
1752 ? gcn_address_register_p (x, SImode, strict)
1753 : gcn_vec_address_register_p (x, SImode, strict));
1754 /* Addresses are in the form BASE+OFFSET
1755 OFFSET is either 20bit unsigned immediate, SGPR or M0.
1756 Writes and atomics do not accept SGPR. */
1757 case PLUS:
1758 {
1759 rtx x0 = XEXP (x, 0);
1760 rtx x1 = XEXP (x, 1);
1761 if (!gcn_vec_address_register_p (x0, DImode, strict))
1762 return false;
1763 if (GET_CODE (x1) == REG)
1764 {
1765 if (GET_CODE (x1) != REG
1766 || (REGNO (x1) <= FIRST_PSEUDO_REGISTER
1767 && !gcn_ssrc_register_operand (x1, DImode)))
1768 return false;
1769 }
1770 else if (GET_CODE (x1) == CONST_VECTOR
1771 && GET_CODE (CONST_VECTOR_ELT (x1, 0)) == CONST_INT
1772 && single_cst_vector_p (x1))
1773 {
1774 x1 = CONST_VECTOR_ELT (x1, 0);
1775 if (INTVAL (x1) >= 0 && INTVAL (x1) < (1 << 20))
1776 return true;
1777 }
1778 return false;
1779 }
1780
1781 default:
1782 break;
1783 }
1784 else
1785 gcc_unreachable ();
1786 return false;
1787 }
1788
1789 /* Implement TARGET_ADDR_SPACE_POINTER_MODE.
1790
1791 Return the appropriate mode for a named address pointer. */
1792
1793 static scalar_int_mode
1794 gcn_addr_space_pointer_mode (addr_space_t addrspace)
1795 {
1796 switch (addrspace)
1797 {
1798 case ADDR_SPACE_SCRATCH:
1799 case ADDR_SPACE_LDS:
1800 case ADDR_SPACE_GDS:
1801 return SImode;
1802 case ADDR_SPACE_DEFAULT:
1803 case ADDR_SPACE_FLAT:
1804 case ADDR_SPACE_FLAT_SCRATCH:
1805 case ADDR_SPACE_SCALAR_FLAT:
1806 return DImode;
1807 default:
1808 gcc_unreachable ();
1809 }
1810 }
1811
1812 /* Implement TARGET_ADDR_SPACE_ADDRESS_MODE.
1813
1814 Return the appropriate mode for a named address space address. */
1815
1816 static scalar_int_mode
1817 gcn_addr_space_address_mode (addr_space_t addrspace)
1818 {
1819 return gcn_addr_space_pointer_mode (addrspace);
1820 }
1821
1822 /* Implement TARGET_ADDR_SPACE_SUBSET_P.
1823
1824 Determine if one named address space is a subset of another. */
1825
1826 static bool
1827 gcn_addr_space_subset_p (addr_space_t subset, addr_space_t superset)
1828 {
1829 if (subset == superset)
1830 return true;
1831 /* FIXME is this true? */
1832 if (AS_FLAT_P (superset) || AS_SCALAR_FLAT_P (superset))
1833 return true;
1834 return false;
1835 }
1836
1837 /* Convert from one address space to another. */
1838
1839 static rtx
1840 gcn_addr_space_convert (rtx op, tree from_type, tree to_type)
1841 {
1842 gcc_assert (POINTER_TYPE_P (from_type));
1843 gcc_assert (POINTER_TYPE_P (to_type));
1844
1845 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (from_type));
1846 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (to_type));
1847
1848 if (AS_LDS_P (as_from) && AS_FLAT_P (as_to))
1849 {
1850 /* The high bits of the QUEUE_PTR_ARG register are used by
1851 GCN_BUILTIN_FIRST_CALL_THIS_THREAD_P, so mask them out. */
1852 rtx queue_reg = gen_rtx_REG (DImode,
1853 cfun->machine->args.reg[QUEUE_PTR_ARG]);
1854 rtx queue_ptr = gen_reg_rtx (DImode);
1855 emit_insn (gen_anddi3 (queue_ptr, queue_reg, GEN_INT (0xffffffffffff)));
1856 rtx group_seg_aperture_hi = gen_rtx_MEM (SImode,
1857 gen_rtx_PLUS (DImode, queue_ptr,
1858 gen_int_mode (64, SImode)));
1859 rtx tmp = gen_reg_rtx (DImode);
1860
1861 emit_move_insn (gen_lowpart (SImode, tmp), op);
1862 emit_move_insn (gen_highpart_mode (SImode, DImode, tmp),
1863 group_seg_aperture_hi);
1864
1865 return tmp;
1866 }
1867 else if (as_from == as_to)
1868 return op;
1869 else
1870 gcc_unreachable ();
1871 }
1872
1873 /* Implement TARGET_ADDR_SPACE_DEBUG.
1874
1875 Return the dwarf address space class for each hardware address space. */
1876
1877 static int
1878 gcn_addr_space_debug (addr_space_t as)
1879 {
1880 switch (as)
1881 {
1882 case ADDR_SPACE_DEFAULT:
1883 case ADDR_SPACE_FLAT:
1884 case ADDR_SPACE_SCALAR_FLAT:
1885 case ADDR_SPACE_FLAT_SCRATCH:
1886 return DW_ADDR_none;
1887 case ADDR_SPACE_GLOBAL:
1888 return 1; // DW_ADDR_LLVM_global
1889 case ADDR_SPACE_LDS:
1890 return 3; // DW_ADDR_LLVM_group
1891 case ADDR_SPACE_SCRATCH:
1892 return 4; // DW_ADDR_LLVM_private
1893 case ADDR_SPACE_GDS:
1894 return 0x8000; // DW_ADDR_AMDGPU_region
1895 }
1896 gcc_unreachable ();
1897 }
1898
1899
1900 /* Implement REGNO_MODE_CODE_OK_FOR_BASE_P via gcn.h
1901
1902 Retun true if REGNO is OK for memory adressing. */
1903
1904 bool
1905 gcn_regno_mode_code_ok_for_base_p (int regno,
1906 machine_mode, addr_space_t as, int, int)
1907 {
1908 if (regno >= FIRST_PSEUDO_REGISTER)
1909 {
1910 if (reg_renumber)
1911 regno = reg_renumber[regno];
1912 else
1913 return true;
1914 }
1915 if (AS_FLAT_P (as))
1916 return (VGPR_REGNO_P (regno)
1917 || regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM);
1918 else if (AS_SCALAR_FLAT_P (as))
1919 return (SGPR_REGNO_P (regno)
1920 || regno == ARG_POINTER_REGNUM || regno == FRAME_POINTER_REGNUM);
1921 else if (AS_GLOBAL_P (as))
1922 {
1923 return (SGPR_REGNO_P (regno)
1924 || VGPR_REGNO_P (regno)
1925 || regno == ARG_POINTER_REGNUM
1926 || regno == FRAME_POINTER_REGNUM);
1927 }
1928 else
1929 /* For now. */
1930 return false;
1931 }
1932
1933 /* Implement MODE_CODE_BASE_REG_CLASS via gcn.h.
1934
1935 Return a suitable register class for memory addressing. */
1936
1937 reg_class
1938 gcn_mode_code_base_reg_class (machine_mode mode, addr_space_t as, int oc,
1939 int ic)
1940 {
1941 switch (as)
1942 {
1943 case ADDR_SPACE_DEFAULT:
1944 return gcn_mode_code_base_reg_class (mode, DEFAULT_ADDR_SPACE, oc, ic);
1945 case ADDR_SPACE_SCALAR_FLAT:
1946 case ADDR_SPACE_SCRATCH:
1947 return SGPR_REGS;
1948 break;
1949 case ADDR_SPACE_FLAT:
1950 case ADDR_SPACE_FLAT_SCRATCH:
1951 case ADDR_SPACE_LDS:
1952 case ADDR_SPACE_GDS:
1953 return ((GET_MODE_CLASS (mode) == MODE_VECTOR_INT
1954 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
1955 ? SGPR_REGS : VGPR_REGS);
1956 case ADDR_SPACE_GLOBAL:
1957 return ((GET_MODE_CLASS (mode) == MODE_VECTOR_INT
1958 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
1959 ? SGPR_REGS : ALL_GPR_REGS);
1960 }
1961 gcc_unreachable ();
1962 }
1963
1964 /* Implement REGNO_OK_FOR_INDEX_P via gcn.h.
1965
1966 Return true if REGNO is OK for index of memory addressing. */
1967
1968 bool
1969 regno_ok_for_index_p (int regno)
1970 {
1971 if (regno >= FIRST_PSEUDO_REGISTER)
1972 {
1973 if (reg_renumber)
1974 regno = reg_renumber[regno];
1975 else
1976 return true;
1977 }
1978 return regno == M0_REG || VGPR_REGNO_P (regno);
1979 }
1980
1981 /* Expand vector init of OP0 by VEC.
1982 Implements vec_init instruction pattern. */
1983
1984 void
1985 gcn_expand_vector_init (rtx op0, rtx vec)
1986 {
1987 rtx val[64];
1988 machine_mode mode = GET_MODE (op0);
1989 int vf = GET_MODE_NUNITS (mode);
1990 machine_mode addrmode = VnMODE (vf, DImode);
1991 machine_mode offsetmode = VnMODE (vf, SImode);
1992
1993 int64_t mem_mask = 0;
1994 int64_t item_mask[64];
1995 rtx ramp = gen_reg_rtx (offsetmode);
1996 rtx addr = gen_reg_rtx (addrmode);
1997
1998 int unit_size = GET_MODE_SIZE (GET_MODE_INNER (GET_MODE (op0)));
1999 emit_insn (gen_mulvNsi3_dup (ramp, gen_rtx_REG (offsetmode, VGPR_REGNO (1)),
2000 GEN_INT (unit_size)));
2001
2002 bool simple_repeat = true;
2003
2004 /* Expand nested vectors into one vector. */
2005 int item_count = XVECLEN (vec, 0);
2006 for (int i = 0, j = 0; i < item_count; i++)
2007 {
2008 rtx item = XVECEXP (vec, 0, i);
2009 machine_mode mode = GET_MODE (item);
2010 int units = VECTOR_MODE_P (mode) ? GET_MODE_NUNITS (mode) : 1;
2011 item_mask[j] = (((uint64_t)-1)>>(64-units)) << j;
2012
2013 if (simple_repeat && i != 0)
2014 simple_repeat = item == XVECEXP (vec, 0, i-1);
2015
2016 /* If its a vector of values then copy them into the final location. */
2017 if (GET_CODE (item) == CONST_VECTOR)
2018 {
2019 for (int k = 0; k < units; k++)
2020 val[j++] = XVECEXP (item, 0, k);
2021 continue;
2022 }
2023 /* Otherwise, we have a scalar or an expression that expands... */
2024
2025 if (MEM_P (item))
2026 {
2027 rtx base = XEXP (item, 0);
2028 if (MEM_ADDR_SPACE (item) == DEFAULT_ADDR_SPACE
2029 && REG_P (base))
2030 {
2031 /* We have a simple vector load. We can put the addresses in
2032 the vector, combine it with any other such MEMs, and load it
2033 all with a single gather at the end. */
2034 int64_t mask = ((0xffffffffffffffffUL
2035 >> (64-GET_MODE_NUNITS (mode)))
2036 << j);
2037 rtx exec = get_exec (mask);
2038 emit_insn (gen_subvNsi3
2039 (ramp, ramp,
2040 gcn_vec_constant (offsetmode, j*unit_size),
2041 ramp, exec));
2042 emit_insn (gen_addvNdi3_zext_dup2
2043 (addr, ramp, base,
2044 (mem_mask ? addr : gcn_gen_undef (addrmode)),
2045 exec));
2046 mem_mask |= mask;
2047 }
2048 else
2049 /* The MEM is non-trivial, so let's load it independently. */
2050 item = force_reg (mode, item);
2051 }
2052 else if (!CONST_INT_P (item) && !CONST_DOUBLE_P (item))
2053 /* The item may be a symbol_ref, or something else non-trivial. */
2054 item = force_reg (mode, item);
2055
2056 /* Duplicate the vector across each item.
2057 It is either a smaller vector register that needs shifting,
2058 or a MEM that needs loading. */
2059 val[j] = item;
2060 j += units;
2061 }
2062
2063 int64_t initialized_mask = 0;
2064 rtx prev = NULL;
2065
2066 if (mem_mask)
2067 {
2068 emit_insn (gen_gathervNm_expr
2069 (op0, gen_rtx_PLUS (addrmode, addr,
2070 gen_rtx_VEC_DUPLICATE (addrmode,
2071 const0_rtx)),
2072 GEN_INT (DEFAULT_ADDR_SPACE), GEN_INT (0),
2073 NULL, get_exec (mem_mask)));
2074 prev = op0;
2075 initialized_mask = mem_mask;
2076 }
2077
2078 if (simple_repeat && item_count > 1 && !prev)
2079 {
2080 /* Special case for instances of {A, B, A, B, A, B, ....}, etc. */
2081 rtx src = gen_rtx_SUBREG (mode, val[0], 0);
2082 rtx input_vf_mask = GEN_INT (GET_MODE_NUNITS (GET_MODE (val[0]))-1);
2083
2084 rtx permutation = gen_reg_rtx (VnMODE (vf, SImode));
2085 emit_insn (gen_vec_seriesvNsi (permutation, GEN_INT (0), GEN_INT (1)));
2086 rtx mask_dup = gen_reg_rtx (VnMODE (vf, SImode));
2087 emit_insn (gen_vec_duplicatevNsi (mask_dup, input_vf_mask));
2088 emit_insn (gen_andvNsi3 (permutation, permutation, mask_dup));
2089 emit_insn (gen_ashlvNsi3 (permutation, permutation, GEN_INT (2)));
2090 emit_insn (gen_ds_bpermutevNm (op0, permutation, src, get_exec (mode)));
2091 return;
2092 }
2093
2094 /* Write each value, elementwise, but coalesce matching values into one
2095 instruction, where possible. */
2096 for (int i = 0; i < vf; i++)
2097 if (!(initialized_mask & ((int64_t) 1 << i)))
2098 {
2099 if (gcn_constant_p (val[i]))
2100 emit_insn (gen_movvNm (op0, gcn_vec_constant (mode, val[i]), prev,
2101 get_exec (item_mask[i])));
2102 else if (VECTOR_MODE_P (GET_MODE (val[i]))
2103 && (GET_MODE_NUNITS (GET_MODE (val[i])) == vf
2104 || i == 0))
2105 emit_insn (gen_movvNm (op0, gen_rtx_SUBREG (mode, val[i], 0), prev,
2106 get_exec (item_mask[i])));
2107 else if (VECTOR_MODE_P (GET_MODE (val[i])))
2108 {
2109 rtx permutation = gen_reg_rtx (VnMODE (vf, SImode));
2110 emit_insn (gen_vec_seriesvNsi (permutation, GEN_INT (-i*4),
2111 GEN_INT (4)));
2112 rtx tmp = gen_reg_rtx (mode);
2113 emit_insn (gen_ds_bpermutevNm (tmp, permutation,
2114 gen_rtx_SUBREG (mode, val[i], 0),
2115 get_exec (-1)));
2116 emit_insn (gen_movvNm (op0, tmp, prev, get_exec (item_mask[i])));
2117 }
2118 else
2119 {
2120 rtx reg = force_reg (GET_MODE_INNER (mode), val[i]);
2121 emit_insn (gen_vec_duplicatevNm (op0, reg, prev,
2122 get_exec (item_mask[i])));
2123 }
2124
2125 initialized_mask |= item_mask[i];
2126 prev = op0;
2127 }
2128 }
2129
2130 /* Load vector constant where n-th lane contains BASE+n*VAL. */
2131
2132 static rtx
2133 strided_constant (machine_mode mode, int base, int val)
2134 {
2135 rtx x = gen_reg_rtx (mode);
2136 emit_move_insn (x, gcn_vec_constant (mode, base));
2137 emit_insn (gen_addvNm3 (x, x, gcn_vec_constant (mode, val * 32),
2138 x, get_exec (0xffffffff00000000)));
2139 emit_insn (gen_addvNm3 (x, x, gcn_vec_constant (mode, val * 16),
2140 x, get_exec (0xffff0000ffff0000)));
2141 emit_insn (gen_addvNm3 (x, x, gcn_vec_constant (mode, val * 8),
2142 x, get_exec (0xff00ff00ff00ff00)));
2143 emit_insn (gen_addvNm3 (x, x, gcn_vec_constant (mode, val * 4),
2144 x, get_exec (0xf0f0f0f0f0f0f0f0)));
2145 emit_insn (gen_addvNm3 (x, x, gcn_vec_constant (mode, val * 2),
2146 x, get_exec (0xcccccccccccccccc)));
2147 emit_insn (gen_addvNm3 (x, x, gcn_vec_constant (mode, val * 1),
2148 x, get_exec (0xaaaaaaaaaaaaaaaa)));
2149 return x;
2150 }
2151
2152 /* Implement TARGET_ADDR_SPACE_LEGITIMIZE_ADDRESS. */
2153
2154 static rtx
2155 gcn_addr_space_legitimize_address (rtx x, rtx old, machine_mode mode,
2156 addr_space_t as)
2157 {
2158 switch (as)
2159 {
2160 case ADDR_SPACE_DEFAULT:
2161 return gcn_addr_space_legitimize_address (x, old, mode,
2162 DEFAULT_ADDR_SPACE);
2163 case ADDR_SPACE_SCALAR_FLAT:
2164 case ADDR_SPACE_SCRATCH:
2165 /* Instructions working on vectors need the address to be in
2166 a register. */
2167 if (vgpr_vector_mode_p (mode))
2168 return force_reg (GET_MODE (x), x);
2169
2170 return x;
2171 case ADDR_SPACE_FLAT:
2172 case ADDR_SPACE_FLAT_SCRATCH:
2173 case ADDR_SPACE_GLOBAL:
2174 return x;
2175 case ADDR_SPACE_LDS:
2176 case ADDR_SPACE_GDS:
2177 /* FIXME: LDS support offsets, handle them!. */
2178 if (vgpr_vector_mode_p (mode)
2179 && GET_MODE_INNER (GET_MODE (x)) != SImode)
2180 {
2181 machine_mode simode = VnMODE (GET_MODE_NUNITS (mode), SImode);
2182 rtx addrs = gen_reg_rtx (simode);
2183 rtx base = force_reg (SImode, x);
2184 rtx offsets = strided_constant (simode, 0,
2185 GET_MODE_UNIT_SIZE (mode));
2186
2187 emit_insn (gen_vec_duplicatevNsi (addrs, base));
2188 emit_insn (gen_addvNsi3 (addrs, offsets, addrs));
2189 return addrs;
2190 }
2191 return x;
2192 }
2193 gcc_unreachable ();
2194 }
2195
2196 /* Convert a (mem:<MODE> (reg:DI)) to (mem:<MODE> (reg:VnDI)) with the
2197 proper vector of stepped addresses.
2198
2199 MEM will be a DImode address of a vector in an SGPR.
2200 TMP will be a VnDImode VGPR pair or (scratch:VnDI). */
2201
2202 rtx
2203 gcn_expand_scalar_to_vector_address (machine_mode mode, rtx exec, rtx mem,
2204 rtx tmp)
2205 {
2206 machine_mode pmode = VnMODE (GET_MODE_NUNITS (mode), DImode);
2207 machine_mode offmode = VnMODE (GET_MODE_NUNITS (mode), SImode);
2208 gcc_assert (MEM_P (mem));
2209 rtx mem_base = XEXP (mem, 0);
2210 rtx mem_index = NULL_RTX;
2211
2212 if (GET_CODE (mem_base) == PLUS)
2213 {
2214 mem_index = XEXP (mem_base, 1);
2215 mem_base = XEXP (mem_base, 0);
2216 }
2217
2218 /* RF and RM base registers for vector modes should be always an SGPR. */
2219 gcc_assert (SGPR_REGNO_P (REGNO (mem_base))
2220 || REGNO (mem_base) >= FIRST_PSEUDO_REGISTER);
2221
2222 machine_mode inner = GET_MODE_INNER (mode);
2223 int shift = exact_log2 (GET_MODE_SIZE (inner));
2224 rtx ramp = gen_rtx_REG (offmode, VGPR_REGNO (1));
2225 rtx new_base = NULL_RTX;
2226 addr_space_t as = MEM_ADDR_SPACE (mem);
2227
2228 rtx tmplo = (REG_P (tmp)
2229 ? gcn_operand_part (pmode, tmp, 0)
2230 : gen_reg_rtx (offmode));
2231
2232 /* tmplo[:] = ramp[:] << shift */
2233 emit_insn (gen_ashlvNsi3 (tmplo, ramp,
2234 gen_int_mode (shift, SImode),
2235 NULL, exec));
2236
2237 if (AS_FLAT_P (as))
2238 {
2239 rtx vcc = gen_rtx_REG (DImode, CC_SAVE_REG);
2240
2241 if (REG_P (tmp))
2242 {
2243 rtx mem_base_lo = gcn_operand_part (DImode, mem_base, 0);
2244 rtx mem_base_hi = gcn_operand_part (DImode, mem_base, 1);
2245 rtx tmphi = gcn_operand_part (pmode, tmp, 1);
2246
2247 /* tmphi[:] = mem_base_hi */
2248 emit_insn (gen_vec_duplicatevNsi (tmphi, mem_base_hi, NULL, exec));
2249
2250 /* tmp[:] += zext (mem_base) */
2251 if (exec)
2252 {
2253 emit_insn (gen_addvNsi3_vcc_dup (tmplo, mem_base_lo, tmplo,
2254 vcc, NULL, exec));
2255 emit_insn (gen_addcvNsi3 (tmphi, tmphi, const0_rtx,
2256 vcc, vcc, NULL, exec));
2257 }
2258 else
2259 emit_insn (gen_addvNdi3_vcc_zext_dup (tmp, mem_base_lo, tmp, vcc));
2260 }
2261 else
2262 {
2263 tmp = gen_reg_rtx (pmode);
2264 emit_insn (gen_addvNdi3_vcc_zext_dup2 (tmp, tmplo, mem_base, vcc,
2265 NULL, exec));
2266 }
2267
2268 new_base = tmp;
2269 }
2270 else if (AS_ANY_DS_P (as))
2271 {
2272 emit_insn (gen_addvNsi3_dup (tmplo, tmplo, mem_base, NULL, exec));
2273 new_base = tmplo;
2274 }
2275 else
2276 {
2277 mem_base = gen_rtx_VEC_DUPLICATE (pmode, mem_base);
2278 new_base = gen_rtx_PLUS (pmode, mem_base,
2279 gen_rtx_SIGN_EXTEND (pmode, tmplo));
2280 }
2281
2282 return gen_rtx_PLUS (GET_MODE (new_base), new_base,
2283 gen_rtx_VEC_DUPLICATE (GET_MODE (new_base),
2284 (mem_index ? mem_index
2285 : const0_rtx)));
2286 }
2287
2288 /* Convert a BASE address, a vector of OFFSETS, and a SCALE, to addresses
2289 suitable for the given address space. This is indented for use in
2290 gather/scatter patterns.
2291
2292 The offsets may be signed or unsigned, according to UNSIGNED_P.
2293 If EXEC is set then _exec patterns will be used, otherwise plain.
2294
2295 Return values.
2296 ADDR_SPACE_FLAT - return VnDImode vector of absolute addresses.
2297 ADDR_SPACE_GLOBAL - return VnSImode vector of offsets. */
2298
2299 rtx
2300 gcn_expand_scaled_offsets (addr_space_t as, rtx base, rtx offsets, rtx scale,
2301 bool unsigned_p, rtx exec)
2302 {
2303 int vf = GET_MODE_NUNITS (GET_MODE (offsets));
2304 rtx tmpsi = gen_reg_rtx (VnMODE (vf, SImode));
2305 rtx tmpdi = gen_reg_rtx (VnMODE (vf, DImode));
2306
2307 if (CONST_INT_P (scale)
2308 && INTVAL (scale) > 0
2309 && exact_log2 (INTVAL (scale)) >= 0)
2310 emit_insn (gen_ashlvNsi3 (tmpsi, offsets,
2311 GEN_INT (exact_log2 (INTVAL (scale))),
2312 NULL, exec));
2313 else
2314 emit_insn (gen_mulvNsi3_dup (tmpsi, offsets, scale, NULL, exec));
2315
2316 /* "Global" instructions do not support negative register offsets. */
2317 if (as == ADDR_SPACE_FLAT || !unsigned_p)
2318 {
2319 if (unsigned_p)
2320 emit_insn (gen_addvNdi3_zext_dup2 (tmpdi, tmpsi, base, NULL, exec));
2321 else
2322 emit_insn (gen_addvNdi3_sext_dup2 (tmpdi, tmpsi, base, NULL, exec));
2323 return tmpdi;
2324 }
2325 else if (as == ADDR_SPACE_GLOBAL)
2326 return tmpsi;
2327
2328 gcc_unreachable ();
2329 }
2330
2331 /* Return true if move from OP0 to OP1 is known to be executed in vector
2332 unit. */
2333
2334 bool
2335 gcn_vgpr_move_p (rtx op0, rtx op1)
2336 {
2337 if (MEM_P (op0) && AS_SCALAR_FLAT_P (MEM_ADDR_SPACE (op0)))
2338 return true;
2339 if (MEM_P (op1) && AS_SCALAR_FLAT_P (MEM_ADDR_SPACE (op1)))
2340 return true;
2341 return ((REG_P (op0) && VGPR_REGNO_P (REGNO (op0)))
2342 || (REG_P (op1) && VGPR_REGNO_P (REGNO (op1)))
2343 || vgpr_vector_mode_p (GET_MODE (op0)));
2344 }
2345
2346 /* Return true if move from OP0 to OP1 is known to be executed in scalar
2347 unit. Used in the machine description. */
2348
2349 bool
2350 gcn_sgpr_move_p (rtx op0, rtx op1)
2351 {
2352 if (MEM_P (op0) && AS_SCALAR_FLAT_P (MEM_ADDR_SPACE (op0)))
2353 return true;
2354 if (MEM_P (op1) && AS_SCALAR_FLAT_P (MEM_ADDR_SPACE (op1)))
2355 return true;
2356 if (!REG_P (op0)
2357 || REGNO (op0) >= FIRST_PSEUDO_REGISTER
2358 || VGPR_REGNO_P (REGNO (op0))
2359 || AVGPR_REGNO_P (REGNO (op0)))
2360 return false;
2361 if (REG_P (op1)
2362 && REGNO (op1) < FIRST_PSEUDO_REGISTER
2363 && !VGPR_REGNO_P (REGNO (op1))
2364 && !AVGPR_REGNO_P (REGNO (op1)))
2365 return true;
2366 return immediate_operand (op1, VOIDmode) || memory_operand (op1, VOIDmode);
2367 }
2368
2369 /* Implement TARGET_SECONDARY_RELOAD.
2370
2371 The address space determines which registers can be used for loads and
2372 stores. */
2373
2374 static reg_class_t
2375 gcn_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
2376 machine_mode reload_mode, secondary_reload_info *sri)
2377 {
2378 reg_class_t result = NO_REGS;
2379 bool spilled_pseudo =
2380 (REG_P (x) || GET_CODE (x) == SUBREG) && true_regnum (x) == -1;
2381
2382 if (dump_file && (dump_flags & TDF_DETAILS))
2383 {
2384 fprintf (dump_file, "gcn_secondary_reload: ");
2385 dump_value_slim (dump_file, x, 1);
2386 fprintf (dump_file, " %s %s:%s", (in_p ? "->" : "<-"),
2387 reg_class_names[rclass], GET_MODE_NAME (reload_mode));
2388 if (REG_P (x) || GET_CODE (x) == SUBREG)
2389 fprintf (dump_file, " (true regnum: %d \"%s\")", true_regnum (x),
2390 (true_regnum (x) >= 0
2391 && true_regnum (x) < FIRST_PSEUDO_REGISTER
2392 ? reg_names[true_regnum (x)]
2393 : (spilled_pseudo ? "stack spill" : "??")));
2394 fprintf (dump_file, "\n");
2395 }
2396
2397 /* Some callers don't use or initialize icode. */
2398 sri->icode = CODE_FOR_nothing;
2399
2400 if (MEM_P (x) || spilled_pseudo)
2401 {
2402 addr_space_t as = DEFAULT_ADDR_SPACE;
2403
2404 /* If we have a spilled pseudo, we can't find the address space
2405 directly, but we know it's in ADDR_SPACE_FLAT space for GCN3 or
2406 ADDR_SPACE_GLOBAL for GCN5. */
2407 if (MEM_P (x))
2408 as = MEM_ADDR_SPACE (x);
2409
2410 if (as == ADDR_SPACE_DEFAULT)
2411 as = DEFAULT_ADDR_SPACE;
2412
2413 switch (as)
2414 {
2415 case ADDR_SPACE_SCALAR_FLAT:
2416 result =
2417 ((!MEM_P (x) || rclass == SGPR_REGS) ? NO_REGS : SGPR_REGS);
2418 break;
2419 case ADDR_SPACE_FLAT:
2420 case ADDR_SPACE_FLAT_SCRATCH:
2421 case ADDR_SPACE_GLOBAL:
2422 if (GET_MODE_CLASS (reload_mode) == MODE_VECTOR_INT
2423 || GET_MODE_CLASS (reload_mode) == MODE_VECTOR_FLOAT)
2424 {
2425 sri->icode = code_for_mov_sgprbase (reload_mode);
2426 break;
2427 }
2428 /* Fallthrough. */
2429 case ADDR_SPACE_LDS:
2430 case ADDR_SPACE_GDS:
2431 case ADDR_SPACE_SCRATCH:
2432 result = (rclass == VGPR_REGS ? NO_REGS : VGPR_REGS);
2433 break;
2434 }
2435
2436 /* CDNA1 doesn't have an instruction for going between the accumulator
2437 registers and memory. Go via a VGPR in this case. */
2438 if (!TARGET_AVGPR_MEMOPS
2439 && rclass == AVGPR_REGS && result != VGPR_REGS)
2440 result = VGPR_REGS;
2441 }
2442
2443 if (dump_file && (dump_flags & TDF_DETAILS))
2444 fprintf (dump_file, " <= %s (icode: %s)\n", reg_class_names[result],
2445 get_insn_name (sri->icode));
2446
2447 return result;
2448 }
2449
2450 /* Update register usage after having seen the compiler flags and kernel
2451 attributes. We typically want to fix registers that contain values
2452 set by the HSA runtime. */
2453
2454 static void
2455 gcn_conditional_register_usage (void)
2456 {
2457 /* Some architectures have a register allocation granularity that does not
2458 permit use of the full register count. */
2459 for (int i = 256 - (256 % TARGET_VGPR_GRANULARITY);
2460 i < 256;
2461 i++)
2462 fixed_regs[VGPR_REGNO (i)] = call_used_regs[VGPR_REGNO (i)] = 1;
2463
2464 if (!cfun || !cfun->machine)
2465 return;
2466
2467 if (cfun->machine->normal_function)
2468 {
2469 /* Restrict the set of SGPRs, VGPRs and AVGPRs used by non-kernel
2470 functions. */
2471 for (int i = SGPR_REGNO (MAX_NORMAL_SGPR_COUNT);
2472 i <= LAST_SGPR_REG; i++)
2473 fixed_regs[i] = 1, call_used_regs[i] = 1;
2474
2475 for (int i = VGPR_REGNO (MAX_NORMAL_VGPR_COUNT);
2476 i <= LAST_VGPR_REG; i++)
2477 fixed_regs[i] = 1, call_used_regs[i] = 1;
2478
2479 for (int i = AVGPR_REGNO (MAX_NORMAL_AVGPR_COUNT);
2480 i <= LAST_AVGPR_REG; i++)
2481 fixed_regs[i] = 1, call_used_regs[i] = 1;
2482 return;
2483 }
2484
2485 /* If the set of requested args is the default set, nothing more needs to
2486 be done. */
2487 if (cfun->machine->args.requested == default_requested_args)
2488 return;
2489
2490 /* Requesting a set of args different from the default violates the ABI. */
2491 if (!leaf_function_p ())
2492 warning (0, "A non-default set of initial values has been requested, "
2493 "which violates the ABI");
2494
2495 for (int i = SGPR_REGNO (0); i < SGPR_REGNO (14); i++)
2496 fixed_regs[i] = 0;
2497
2498 /* Fix the runtime argument register containing values that may be
2499 needed later. DISPATCH_PTR_ARG and FLAT_SCRATCH_* should not be
2500 needed after the prologue so there's no need to fix them. */
2501 if (cfun->machine->args.reg[PRIVATE_SEGMENT_WAVE_OFFSET_ARG] >= 0)
2502 fixed_regs[cfun->machine->args.reg[PRIVATE_SEGMENT_WAVE_OFFSET_ARG]] = 1;
2503 if (cfun->machine->args.reg[PRIVATE_SEGMENT_BUFFER_ARG] >= 0)
2504 {
2505 /* The upper 32-bits of the 64-bit descriptor are not used, so allow
2506 the containing registers to be used for other purposes. */
2507 fixed_regs[cfun->machine->args.reg[PRIVATE_SEGMENT_BUFFER_ARG]] = 1;
2508 fixed_regs[cfun->machine->args.reg[PRIVATE_SEGMENT_BUFFER_ARG] + 1] = 1;
2509 }
2510 if (cfun->machine->args.reg[KERNARG_SEGMENT_PTR_ARG] >= 0)
2511 {
2512 fixed_regs[cfun->machine->args.reg[KERNARG_SEGMENT_PTR_ARG]] = 1;
2513 fixed_regs[cfun->machine->args.reg[KERNARG_SEGMENT_PTR_ARG] + 1] = 1;
2514 }
2515 if (cfun->machine->args.reg[DISPATCH_PTR_ARG] >= 0)
2516 {
2517 fixed_regs[cfun->machine->args.reg[DISPATCH_PTR_ARG]] = 1;
2518 fixed_regs[cfun->machine->args.reg[DISPATCH_PTR_ARG] + 1] = 1;
2519 }
2520 if (cfun->machine->args.reg[QUEUE_PTR_ARG] >= 0)
2521 {
2522 fixed_regs[cfun->machine->args.reg[QUEUE_PTR_ARG]] = 1;
2523 fixed_regs[cfun->machine->args.reg[QUEUE_PTR_ARG] + 1] = 1;
2524 }
2525 if (cfun->machine->args.reg[WORKGROUP_ID_X_ARG] >= 0)
2526 fixed_regs[cfun->machine->args.reg[WORKGROUP_ID_X_ARG]] = 1;
2527 if (cfun->machine->args.reg[WORK_ITEM_ID_X_ARG] >= 0)
2528 fixed_regs[cfun->machine->args.reg[WORK_ITEM_ID_X_ARG]] = 1;
2529 if (cfun->machine->args.reg[WORK_ITEM_ID_Y_ARG] >= 0)
2530 fixed_regs[cfun->machine->args.reg[WORK_ITEM_ID_Y_ARG]] = 1;
2531 if (cfun->machine->args.reg[WORK_ITEM_ID_Z_ARG] >= 0)
2532 fixed_regs[cfun->machine->args.reg[WORK_ITEM_ID_Z_ARG]] = 1;
2533 }
2534
2535 static bool
2536 gcn_vgpr_equivalent_register_operand (rtx x, machine_mode mode)
2537 {
2538 if (gcn_vgpr_register_operand (x, mode))
2539 return true;
2540 if (TARGET_AVGPR_MEMOPS && gcn_avgpr_register_operand (x, mode))
2541 return true;
2542 return false;
2543 }
2544
2545 /* Determine if a load or store is valid, according to the register classes
2546 and address space. Used primarily by the machine description to decide
2547 when to split a move into two steps. */
2548
2549 bool
2550 gcn_valid_move_p (machine_mode mode, rtx dest, rtx src)
2551 {
2552 if (!MEM_P (dest) && !MEM_P (src))
2553 {
2554 if (gcn_vgpr_register_operand (src, mode)
2555 && gcn_avgpr_register_operand (dest, mode))
2556 return true;
2557 if (gcn_avgpr_register_operand (src, mode)
2558 && gcn_vgpr_register_operand (dest, mode))
2559 return true;
2560 if (TARGET_AVGPR_MEMOPS
2561 && gcn_avgpr_register_operand (src, mode)
2562 && gcn_avgpr_register_operand (dest, mode))
2563 return true;
2564 if (gcn_avgpr_hard_register_operand (src, mode)
2565 || gcn_avgpr_hard_register_operand (dest, mode))
2566 return false;
2567 return true;
2568 }
2569
2570 if (MEM_P (dest)
2571 && AS_FLAT_P (MEM_ADDR_SPACE (dest))
2572 && (gcn_flat_address_p (XEXP (dest, 0), mode)
2573 || GET_CODE (XEXP (dest, 0)) == SYMBOL_REF
2574 || GET_CODE (XEXP (dest, 0)) == LABEL_REF)
2575 && gcn_vgpr_equivalent_register_operand (src, mode))
2576 return true;
2577 else if (MEM_P (src)
2578 && AS_FLAT_P (MEM_ADDR_SPACE (src))
2579 && (gcn_flat_address_p (XEXP (src, 0), mode)
2580 || GET_CODE (XEXP (src, 0)) == SYMBOL_REF
2581 || GET_CODE (XEXP (src, 0)) == LABEL_REF)
2582 && gcn_vgpr_equivalent_register_operand (dest, mode))
2583 return true;
2584
2585 if (MEM_P (dest)
2586 && AS_GLOBAL_P (MEM_ADDR_SPACE (dest))
2587 && (gcn_global_address_p (XEXP (dest, 0))
2588 || GET_CODE (XEXP (dest, 0)) == SYMBOL_REF
2589 || GET_CODE (XEXP (dest, 0)) == LABEL_REF)
2590 && gcn_vgpr_equivalent_register_operand (src, mode))
2591 return true;
2592 else if (MEM_P (src)
2593 && AS_GLOBAL_P (MEM_ADDR_SPACE (src))
2594 && (gcn_global_address_p (XEXP (src, 0))
2595 || GET_CODE (XEXP (src, 0)) == SYMBOL_REF
2596 || GET_CODE (XEXP (src, 0)) == LABEL_REF)
2597 && gcn_vgpr_equivalent_register_operand (dest, mode))
2598 return true;
2599
2600 if (MEM_P (dest)
2601 && MEM_ADDR_SPACE (dest) == ADDR_SPACE_SCALAR_FLAT
2602 && (gcn_scalar_flat_address_p (XEXP (dest, 0))
2603 || GET_CODE (XEXP (dest, 0)) == SYMBOL_REF
2604 || GET_CODE (XEXP (dest, 0)) == LABEL_REF)
2605 && gcn_ssrc_register_operand (src, mode))
2606 return true;
2607 else if (MEM_P (src)
2608 && MEM_ADDR_SPACE (src) == ADDR_SPACE_SCALAR_FLAT
2609 && (gcn_scalar_flat_address_p (XEXP (src, 0))
2610 || GET_CODE (XEXP (src, 0)) == SYMBOL_REF
2611 || GET_CODE (XEXP (src, 0)) == LABEL_REF)
2612 && gcn_sdst_register_operand (dest, mode))
2613 return true;
2614
2615 if (MEM_P (dest)
2616 && AS_ANY_DS_P (MEM_ADDR_SPACE (dest))
2617 && gcn_ds_address_p (XEXP (dest, 0))
2618 && gcn_vgpr_equivalent_register_operand (src, mode))
2619 return true;
2620 else if (MEM_P (src)
2621 && AS_ANY_DS_P (MEM_ADDR_SPACE (src))
2622 && gcn_ds_address_p (XEXP (src, 0))
2623 && gcn_vgpr_equivalent_register_operand (dest, mode))
2624 return true;
2625
2626 return false;
2627 }
2628
2629 /* }}} */
2630 /* {{{ Functions and ABI. */
2631
2632 /* Implement TARGET_FUNCTION_VALUE.
2633
2634 Define how to find the value returned by a function.
2635 The register location is always the same, but the mode depends on
2636 VALTYPE. */
2637
2638 static rtx
2639 gcn_function_value (const_tree valtype, const_tree, bool)
2640 {
2641 machine_mode mode = TYPE_MODE (valtype);
2642
2643 if (INTEGRAL_TYPE_P (valtype)
2644 && GET_MODE_CLASS (mode) == MODE_INT
2645 && GET_MODE_SIZE (mode) < 4)
2646 mode = SImode;
2647
2648 return gen_rtx_REG (mode, RETURN_VALUE_REG);
2649 }
2650
2651 /* Implement TARGET_FUNCTION_VALUE_REGNO_P.
2652
2653 Return true if N is a possible register number for the function return
2654 value. */
2655
2656 static bool
2657 gcn_function_value_regno_p (const unsigned int n)
2658 {
2659 return n == RETURN_VALUE_REG;
2660 }
2661
2662 /* Calculate the number of registers required to hold function argument
2663 ARG. */
2664
2665 static int
2666 num_arg_regs (const function_arg_info &arg)
2667 {
2668 if (targetm.calls.must_pass_in_stack (arg))
2669 return 0;
2670
2671 int size = arg.promoted_size_in_bytes ();
2672 int regsize = UNITS_PER_WORD * (VECTOR_MODE_P (arg.mode)
2673 ? GET_MODE_NUNITS (arg.mode) : 1);
2674 return (size + regsize - 1) / regsize;
2675 }
2676
2677 /* Implement TARGET_STRICT_ARGUMENT_NAMING.
2678
2679 Return true if the location where a function argument is passed
2680 depends on whether or not it is a named argument
2681
2682 For gcn, we know how to handle functions declared as stdarg: by
2683 passing an extra pointer to the unnamed arguments. However, the
2684 Fortran frontend can produce a different situation, where a
2685 function pointer is declared with no arguments, but the actual
2686 function and calls to it take more arguments. In that case, we
2687 want to ensure the call matches the definition of the function. */
2688
2689 static bool
2690 gcn_strict_argument_naming (cumulative_args_t cum_v)
2691 {
2692 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2693
2694 return cum->fntype == NULL_TREE || stdarg_p (cum->fntype);
2695 }
2696
2697 /* Implement TARGET_PRETEND_OUTGOING_VARARGS_NAMED.
2698
2699 See comment on gcn_strict_argument_naming. */
2700
2701 static bool
2702 gcn_pretend_outgoing_varargs_named (cumulative_args_t cum_v)
2703 {
2704 return !gcn_strict_argument_naming (cum_v);
2705 }
2706
2707 /* Implement TARGET_FUNCTION_ARG.
2708
2709 Return an RTX indicating whether a function argument is passed in a register
2710 and if so, which register. */
2711
2712 static rtx
2713 gcn_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
2714 {
2715 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2716 if (cum->normal_function)
2717 {
2718 if (!arg.named || arg.end_marker_p ())
2719 return 0;
2720
2721 if (targetm.calls.must_pass_in_stack (arg))
2722 return 0;
2723
2724 int first_reg = (VECTOR_MODE_P (arg.mode)
2725 ? FIRST_VPARM_REG : FIRST_PARM_REG);
2726 int cum_num = (VECTOR_MODE_P (arg.mode)
2727 ? cum->vnum : cum->num);
2728 int reg_num = first_reg + cum_num;
2729 int num_regs = num_arg_regs (arg);
2730 if (num_regs > 0)
2731 while (reg_num % num_regs != 0)
2732 reg_num++;
2733 if (reg_num + num_regs <= first_reg + NUM_PARM_REGS)
2734 return gen_rtx_REG (arg.mode, reg_num);
2735 }
2736 else
2737 {
2738 if (cum->num >= cum->args.nargs)
2739 {
2740 cum->offset = (cum->offset + TYPE_ALIGN (arg.type) / 8 - 1)
2741 & -(TYPE_ALIGN (arg.type) / 8);
2742 cfun->machine->kernarg_segment_alignment
2743 = MAX ((unsigned) cfun->machine->kernarg_segment_alignment,
2744 TYPE_ALIGN (arg.type) / 8);
2745 rtx addr = gen_rtx_REG (DImode,
2746 cum->args.reg[KERNARG_SEGMENT_PTR_ARG]);
2747 if (cum->offset)
2748 addr = gen_rtx_PLUS (DImode, addr,
2749 gen_int_mode (cum->offset, DImode));
2750 rtx mem = gen_rtx_MEM (arg.mode, addr);
2751 set_mem_attributes (mem, arg.type, 1);
2752 set_mem_addr_space (mem, ADDR_SPACE_SCALAR_FLAT);
2753 MEM_READONLY_P (mem) = 1;
2754 return mem;
2755 }
2756
2757 int a = cum->args.order[cum->num];
2758 if (arg.mode != gcn_kernel_arg_types[a].mode)
2759 {
2760 error ("wrong type of argument %s", gcn_kernel_arg_types[a].name);
2761 return 0;
2762 }
2763 return gen_rtx_REG ((machine_mode) gcn_kernel_arg_types[a].mode,
2764 cum->args.reg[a]);
2765 }
2766 return 0;
2767 }
2768
2769 /* Implement TARGET_FUNCTION_ARG_ADVANCE.
2770
2771 Updates the summarizer variable pointed to by CUM_V to advance past an
2772 argument in the argument list. */
2773
2774 static void
2775 gcn_function_arg_advance (cumulative_args_t cum_v,
2776 const function_arg_info &arg)
2777 {
2778 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2779
2780 if (cum->normal_function)
2781 {
2782 if (!arg.named)
2783 return;
2784
2785 int first_reg = (VECTOR_MODE_P (arg.mode)
2786 ? FIRST_VPARM_REG : FIRST_PARM_REG);
2787 int *cum_num = (VECTOR_MODE_P (arg.mode)
2788 ? &cum->vnum : &cum->num);
2789 int num_regs = num_arg_regs (arg);
2790 if (num_regs > 0)
2791 while ((first_reg + *cum_num) % num_regs != 0)
2792 (*cum_num)++;
2793 *cum_num += num_regs;
2794 }
2795 else
2796 {
2797 if (cum->num < cum->args.nargs)
2798 cum->num++;
2799 else
2800 {
2801 cum->offset += tree_to_uhwi (TYPE_SIZE_UNIT (arg.type));
2802 cfun->machine->kernarg_segment_byte_size = cum->offset;
2803 }
2804 }
2805 }
2806
2807 /* Implement TARGET_ARG_PARTIAL_BYTES.
2808
2809 Returns the number of bytes at the beginning of an argument that must be put
2810 in registers. The value must be zero for arguments that are passed entirely
2811 in registers or that are entirely pushed on the stack. */
2812
2813 static int
2814 gcn_arg_partial_bytes (cumulative_args_t cum_v, const function_arg_info &arg)
2815 {
2816 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
2817
2818 if (!arg.named)
2819 return 0;
2820
2821 if (targetm.calls.must_pass_in_stack (arg))
2822 return 0;
2823
2824 int cum_num = (VECTOR_MODE_P (arg.mode) ? cum->vnum : cum->num);
2825 int regsize = UNITS_PER_WORD * (VECTOR_MODE_P (arg.mode)
2826 ? GET_MODE_NUNITS (arg.mode) : 1);
2827
2828 if (cum_num >= NUM_PARM_REGS)
2829 return 0;
2830
2831 /* If the argument fits entirely in registers, return 0. */
2832 if (cum_num + num_arg_regs (arg) <= NUM_PARM_REGS)
2833 return 0;
2834
2835 return (NUM_PARM_REGS - cum_num) * regsize;
2836 }
2837
2838 /* A normal function which takes a pointer argument may be passed a pointer to
2839 LDS space (via a high-bits-set aperture), and that only works with FLAT
2840 addressing, not GLOBAL. Force FLAT addressing if the function has an
2841 incoming pointer parameter. NOTE: This is a heuristic that works in the
2842 offloading case, but in general, a function might read global pointer
2843 variables, etc. that may refer to LDS space or other special memory areas
2844 not supported by GLOBAL instructions, and then this argument check would not
2845 suffice. */
2846
2847 static void
2848 gcn_detect_incoming_pointer_arg (tree fndecl)
2849 {
2850 gcc_assert (cfun && cfun->machine);
2851
2852 for (tree arg = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2853 arg;
2854 arg = TREE_CHAIN (arg))
2855 if (POINTER_TYPE_P (TREE_VALUE (arg)))
2856 cfun->machine->use_flat_addressing = true;
2857 }
2858
2859 /* Implement INIT_CUMULATIVE_ARGS, via gcn.h.
2860
2861 Initialize a variable CUM of type CUMULATIVE_ARGS for a call to a function
2862 whose data type is FNTYPE. For a library call, FNTYPE is 0. */
2863
2864 void
2865 gcn_init_cumulative_args (CUMULATIVE_ARGS *cum /* Argument info to init */ ,
2866 tree fntype /* tree ptr for function decl */ ,
2867 rtx libname /* SYMBOL_REF of library name or 0 */ ,
2868 tree fndecl, int caller)
2869 {
2870 memset (cum, 0, sizeof (*cum));
2871 cum->fntype = fntype;
2872 if (libname)
2873 {
2874 gcc_assert (cfun && cfun->machine);
2875 cum->normal_function = true;
2876 if (!caller)
2877 {
2878 cfun->machine->normal_function = true;
2879 gcn_detect_incoming_pointer_arg (fndecl);
2880 }
2881 return;
2882 }
2883 tree attr = NULL;
2884 if (fndecl)
2885 attr = lookup_attribute ("amdgpu_hsa_kernel", DECL_ATTRIBUTES (fndecl));
2886 if (fndecl && !attr)
2887 attr = lookup_attribute ("amdgpu_hsa_kernel",
2888 TYPE_ATTRIBUTES (TREE_TYPE (fndecl)));
2889 if (!attr && fntype)
2890 attr = lookup_attribute ("amdgpu_hsa_kernel", TYPE_ATTRIBUTES (fntype));
2891 /* Handle main () as kernel, so we can run testsuite.
2892 Handle OpenACC kernels similarly to main. */
2893 if (!attr && !caller && fndecl
2894 && (MAIN_NAME_P (DECL_NAME (fndecl))
2895 || lookup_attribute ("omp target entrypoint",
2896 DECL_ATTRIBUTES (fndecl)) != NULL_TREE))
2897 gcn_parse_amdgpu_hsa_kernel_attribute (&cum->args, NULL_TREE);
2898 else
2899 {
2900 if (!attr || caller)
2901 {
2902 gcc_assert (cfun && cfun->machine);
2903 cum->normal_function = true;
2904 if (!caller)
2905 cfun->machine->normal_function = true;
2906 }
2907 gcn_parse_amdgpu_hsa_kernel_attribute
2908 (&cum->args, attr ? TREE_VALUE (attr) : NULL_TREE);
2909 }
2910 cfun->machine->args = cum->args;
2911 if (!caller && cfun->machine->normal_function)
2912 gcn_detect_incoming_pointer_arg (fndecl);
2913
2914 reinit_regs ();
2915 }
2916
2917 static bool
2918 gcn_return_in_memory (const_tree type, const_tree ARG_UNUSED (fntype))
2919 {
2920 machine_mode mode = TYPE_MODE (type);
2921 HOST_WIDE_INT size = int_size_in_bytes (type);
2922
2923 if (AGGREGATE_TYPE_P (type))
2924 return true;
2925
2926 if (mode == BLKmode)
2927 return true;
2928
2929 if ((!VECTOR_TYPE_P (type) && size > 2 * UNITS_PER_WORD)
2930 || size > 2 * UNITS_PER_WORD * 64)
2931 return true;
2932
2933 return false;
2934 }
2935
2936 /* Implement TARGET_PROMOTE_FUNCTION_MODE.
2937
2938 Return the mode to use for outgoing function arguments. */
2939
2940 machine_mode
2941 gcn_promote_function_mode (const_tree ARG_UNUSED (type), machine_mode mode,
2942 int *ARG_UNUSED (punsignedp),
2943 const_tree ARG_UNUSED (funtype),
2944 int ARG_UNUSED (for_return))
2945 {
2946 if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) < 4)
2947 return SImode;
2948
2949 return mode;
2950 }
2951
2952 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR.
2953
2954 Derived from hppa_gimplify_va_arg_expr. The generic routine doesn't handle
2955 ARGS_GROW_DOWNWARDS. */
2956
2957 static tree
2958 gcn_gimplify_va_arg_expr (tree valist, tree type,
2959 gimple_seq *ARG_UNUSED (pre_p),
2960 gimple_seq *ARG_UNUSED (post_p))
2961 {
2962 tree ptr = build_pointer_type (type);
2963 tree valist_type;
2964 tree t, u;
2965 bool indirect;
2966
2967 indirect = pass_va_arg_by_reference (type);
2968 if (indirect)
2969 {
2970 type = ptr;
2971 ptr = build_pointer_type (type);
2972 }
2973 valist_type = TREE_TYPE (valist);
2974
2975 /* Args grow down. Not handled by generic routines. */
2976
2977 u = fold_convert (sizetype, size_in_bytes (type));
2978 u = fold_build1 (NEGATE_EXPR, sizetype, u);
2979 t = fold_build_pointer_plus (valist, u);
2980
2981 /* Align to 8 byte boundary. */
2982
2983 u = build_int_cst (TREE_TYPE (t), -8);
2984 t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t, u);
2985 t = fold_convert (valist_type, t);
2986
2987 t = build2 (MODIFY_EXPR, valist_type, valist, t);
2988
2989 t = fold_convert (ptr, t);
2990 t = build_va_arg_indirect_ref (t);
2991
2992 if (indirect)
2993 t = build_va_arg_indirect_ref (t);
2994
2995 return t;
2996 }
2997
2998 /* Return 1 if TRAIT NAME is present in the OpenMP context's
2999 device trait set, return 0 if not present in any OpenMP context in the
3000 whole translation unit, or -1 if not present in the current OpenMP context
3001 but might be present in another OpenMP context in the same TU. */
3002
3003 int
3004 gcn_omp_device_kind_arch_isa (enum omp_device_kind_arch_isa trait,
3005 const char *name)
3006 {
3007 switch (trait)
3008 {
3009 case omp_device_kind:
3010 return strcmp (name, "gpu") == 0;
3011 case omp_device_arch:
3012 return strcmp (name, "amdgcn") == 0 || strcmp (name, "gcn") == 0;
3013 case omp_device_isa:
3014 return strcmp (name, gcn_devices[gcn_arch].name) == 0;
3015 default:
3016 gcc_unreachable ();
3017 }
3018 }
3019
3020 /* Calculate stack offsets needed to create prologues and epilogues. */
3021
3022 static struct machine_function *
3023 gcn_compute_frame_offsets (void)
3024 {
3025 machine_function *offsets = cfun->machine;
3026
3027 if (reload_completed)
3028 return offsets;
3029
3030 offsets->need_frame_pointer = frame_pointer_needed;
3031
3032 offsets->outgoing_args_size = crtl->outgoing_args_size;
3033 offsets->pretend_size = crtl->args.pretend_args_size;
3034
3035 offsets->local_vars = get_frame_size ();
3036
3037 offsets->lr_needs_saving = (!leaf_function_p ()
3038 || df_regs_ever_live_p (LR_REGNUM)
3039 || df_regs_ever_live_p (LR_REGNUM + 1));
3040
3041 offsets->callee_saves = offsets->lr_needs_saving ? 8 : 0;
3042
3043 for (int regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
3044 if ((df_regs_ever_live_p (regno) && !call_used_or_fixed_reg_p (regno))
3045 || ((regno & ~1) == HARD_FRAME_POINTER_REGNUM
3046 && frame_pointer_needed))
3047 offsets->callee_saves += (VGPR_REGNO_P (regno)
3048 || AVGPR_REGNO_P (regno) ? 256 : 4);
3049
3050 /* Round up to 64-bit boundary to maintain stack alignment. */
3051 offsets->callee_saves = (offsets->callee_saves + 7) & ~7;
3052
3053 return offsets;
3054 }
3055
3056 /* Insert code into the prologue or epilogue to store or load any
3057 callee-save register to/from the stack.
3058
3059 Helper function for gcn_expand_prologue and gcn_expand_epilogue. */
3060
3061 static void
3062 move_callee_saved_registers (rtx sp, machine_function *offsets,
3063 bool prologue)
3064 {
3065 int regno, offset, saved_scalars;
3066 rtx exec = gen_rtx_REG (DImode, EXEC_REG);
3067 rtx vcc = gen_rtx_REG (DImode, VCC_LO_REG);
3068 rtx offreg = gen_rtx_REG (SImode, SGPR_REGNO (22));
3069 rtx as = gen_rtx_CONST_INT (VOIDmode, STACK_ADDR_SPACE);
3070 HOST_WIDE_INT exec_set = 0;
3071 int offreg_set = 0;
3072 auto_vec<int> saved_sgprs;
3073
3074 start_sequence ();
3075
3076 /* Move scalars into two vector registers. */
3077 for (regno = 0, saved_scalars = 0; regno < FIRST_VGPR_REG; regno++)
3078 if ((df_regs_ever_live_p (regno) && !call_used_or_fixed_reg_p (regno))
3079 || ((regno & ~1) == LINK_REGNUM && offsets->lr_needs_saving)
3080 || ((regno & ~1) == HARD_FRAME_POINTER_REGNUM
3081 && offsets->need_frame_pointer))
3082 {
3083 rtx reg = gen_rtx_REG (SImode, regno);
3084 rtx vreg = gen_rtx_REG (V64SImode,
3085 VGPR_REGNO (6 + (saved_scalars / 64)));
3086 int lane = saved_scalars % 64;
3087
3088 if (prologue)
3089 {
3090 emit_insn (gen_vec_setv64si (vreg, reg, GEN_INT (lane)));
3091 saved_sgprs.safe_push (regno);
3092 }
3093 else
3094 emit_insn (gen_vec_extractv64sisi (reg, vreg, GEN_INT (lane)));
3095
3096 saved_scalars++;
3097 }
3098
3099 rtx move_scalars = get_insns ();
3100 end_sequence ();
3101 start_sequence ();
3102
3103 /* Ensure that all vector lanes are moved. */
3104 exec_set = -1;
3105 emit_move_insn (exec, GEN_INT (exec_set));
3106
3107 /* Set up a vector stack pointer. */
3108 rtx _0_1_2_3 = gen_rtx_REG (V64SImode, VGPR_REGNO (1));
3109 rtx _0_4_8_12 = gen_rtx_REG (V64SImode, VGPR_REGNO (3));
3110 emit_insn (gen_ashlv64si3_exec (_0_4_8_12, _0_1_2_3, GEN_INT (2),
3111 gcn_gen_undef (V64SImode), exec));
3112 rtx vsp = gen_rtx_REG (V64DImode, VGPR_REGNO (4));
3113 emit_insn (gen_vec_duplicatev64di_exec (vsp, sp, gcn_gen_undef (V64DImode),
3114 exec));
3115 emit_insn (gen_addv64si3_vcc_exec (gcn_operand_part (V64SImode, vsp, 0),
3116 gcn_operand_part (V64SImode, vsp, 0),
3117 _0_4_8_12, vcc, gcn_gen_undef (V64SImode),
3118 exec));
3119 emit_insn (gen_addcv64si3_exec (gcn_operand_part (V64SImode, vsp, 1),
3120 gcn_operand_part (V64SImode, vsp, 1),
3121 const0_rtx, vcc, vcc,
3122 gcn_gen_undef (V64SImode), exec));
3123
3124 /* Move vectors. */
3125 for (regno = FIRST_VGPR_REG, offset = 0;
3126 regno < FIRST_PSEUDO_REGISTER; regno++)
3127 if ((df_regs_ever_live_p (regno) && !call_used_or_fixed_reg_p (regno))
3128 || (regno == VGPR_REGNO (6) && saved_scalars > 0)
3129 || (regno == VGPR_REGNO (7) && saved_scalars > 63))
3130 {
3131 rtx reg = gen_rtx_REG (V64SImode, regno);
3132 int size = 256;
3133
3134 if (regno == VGPR_REGNO (6) && saved_scalars < 64)
3135 size = saved_scalars * 4;
3136 else if (regno == VGPR_REGNO (7) && saved_scalars < 128)
3137 size = (saved_scalars - 64) * 4;
3138
3139 if (size != 256 || exec_set != -1)
3140 {
3141 exec_set = ((unsigned HOST_WIDE_INT) 1 << (size / 4)) - 1;
3142 emit_move_insn (exec, gen_int_mode (exec_set, DImode));
3143 }
3144
3145 if (prologue)
3146 {
3147 rtx insn = emit_insn (gen_scatterv64si_insn_1offset_exec
3148 (vsp, const0_rtx, reg, as, const0_rtx,
3149 exec));
3150
3151 /* Add CFI metadata. */
3152 rtx note;
3153 if (regno == VGPR_REGNO (6) || regno == VGPR_REGNO (7))
3154 {
3155 int start = (regno == VGPR_REGNO (7) ? 64 : 0);
3156 int count = MIN (saved_scalars - start, 64);
3157 int add_lr = (regno == VGPR_REGNO (6)
3158 && offsets->lr_needs_saving);
3159 int lrdest = -1;
3160 rtvec seq = rtvec_alloc (count + add_lr);
3161
3162 /* Add an REG_FRAME_RELATED_EXPR entry for each scalar
3163 register that was saved in this batch. */
3164 for (int idx = 0; idx < count; idx++)
3165 {
3166 int stackaddr = offset + idx * 4;
3167 rtx dest = gen_rtx_MEM (SImode,
3168 gen_rtx_PLUS
3169 (DImode, sp,
3170 GEN_INT (stackaddr)));
3171 rtx src = gen_rtx_REG (SImode, saved_sgprs[start + idx]);
3172 rtx set = gen_rtx_SET (dest, src);
3173 RTX_FRAME_RELATED_P (set) = 1;
3174 RTVEC_ELT (seq, idx) = set;
3175
3176 if (saved_sgprs[start + idx] == LINK_REGNUM)
3177 lrdest = stackaddr;
3178 }
3179
3180 /* Add an additional expression for DWARF_LINK_REGISTER if
3181 LINK_REGNUM was saved. */
3182 if (lrdest != -1)
3183 {
3184 rtx dest = gen_rtx_MEM (DImode,
3185 gen_rtx_PLUS
3186 (DImode, sp,
3187 GEN_INT (lrdest)));
3188 rtx src = gen_rtx_REG (DImode, DWARF_LINK_REGISTER);
3189 rtx set = gen_rtx_SET (dest, src);
3190 RTX_FRAME_RELATED_P (set) = 1;
3191 RTVEC_ELT (seq, count) = set;
3192 }
3193
3194 note = gen_rtx_SEQUENCE (VOIDmode, seq);
3195 }
3196 else
3197 {
3198 rtx dest = gen_rtx_MEM (V64SImode,
3199 gen_rtx_PLUS (DImode, sp,
3200 GEN_INT (offset)));
3201 rtx src = gen_rtx_REG (V64SImode, regno);
3202 note = gen_rtx_SET (dest, src);
3203 }
3204 RTX_FRAME_RELATED_P (insn) = 1;
3205 add_reg_note (insn, REG_FRAME_RELATED_EXPR, note);
3206 }
3207 else
3208 emit_insn (gen_gatherv64si_insn_1offset_exec
3209 (reg, vsp, const0_rtx, as, const0_rtx,
3210 gcn_gen_undef (V64SImode), exec));
3211
3212 /* Move our VSP to the next stack entry. */
3213 if (offreg_set != size)
3214 {
3215 offreg_set = size;
3216 emit_move_insn (offreg, GEN_INT (size));
3217 }
3218 if (exec_set != -1)
3219 {
3220 exec_set = -1;
3221 emit_move_insn (exec, GEN_INT (exec_set));
3222 }
3223 emit_insn (gen_addv64si3_vcc_dup_exec
3224 (gcn_operand_part (V64SImode, vsp, 0),
3225 offreg, gcn_operand_part (V64SImode, vsp, 0),
3226 vcc, gcn_gen_undef (V64SImode), exec));
3227 emit_insn (gen_addcv64si3_exec
3228 (gcn_operand_part (V64SImode, vsp, 1),
3229 gcn_operand_part (V64SImode, vsp, 1),
3230 const0_rtx, vcc, vcc, gcn_gen_undef (V64SImode), exec));
3231
3232 offset += size;
3233 }
3234
3235 rtx move_vectors = get_insns ();
3236 end_sequence ();
3237
3238 if (prologue)
3239 {
3240 emit_insn (move_scalars);
3241 emit_insn (move_vectors);
3242 }
3243 else
3244 {
3245 emit_insn (move_vectors);
3246 emit_insn (move_scalars);
3247 }
3248
3249 /* This happens when a new register becomes "live" after reload.
3250 Check your splitters! */
3251 gcc_assert (offset <= offsets->callee_saves);
3252 }
3253
3254 /* Generate prologue. Called from gen_prologue during pro_and_epilogue pass.
3255
3256 For a non-kernel function, the stack layout looks like this (interim),
3257 growing *upwards*:
3258
3259 hi | + ...
3260 |__________________| <-- current SP
3261 | outgoing args |
3262 |__________________|
3263 | (alloca space) |
3264 |__________________|
3265 | local vars |
3266 |__________________| <-- FP/hard FP
3267 | callee-save regs |
3268 |__________________| <-- soft arg pointer
3269 | pretend args |
3270 |__________________| <-- incoming SP
3271 | incoming args |
3272 lo |..................|
3273
3274 This implies arguments (beyond the first N in registers) must grow
3275 downwards (as, apparently, PA has them do).
3276
3277 For a kernel function we have the simpler:
3278
3279 hi | + ...
3280 |__________________| <-- current SP
3281 | outgoing args |
3282 |__________________|
3283 | (alloca space) |
3284 |__________________|
3285 | local vars |
3286 lo |__________________| <-- FP/hard FP
3287
3288 */
3289
3290 void
3291 gcn_expand_prologue ()
3292 {
3293 machine_function *offsets = gcn_compute_frame_offsets ();
3294
3295 if (!cfun || !cfun->machine || cfun->machine->normal_function)
3296 {
3297 rtx sp = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
3298 rtx sp_hi = gcn_operand_part (Pmode, sp, 1);
3299 rtx sp_lo = gcn_operand_part (Pmode, sp, 0);
3300 rtx fp = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
3301 rtx fp_hi = gcn_operand_part (Pmode, fp, 1);
3302 rtx fp_lo = gcn_operand_part (Pmode, fp, 0);
3303
3304 start_sequence ();
3305
3306 if (offsets->pretend_size > 0)
3307 {
3308 /* FIXME: Do the actual saving of register pretend args to the stack.
3309 Register order needs consideration. */
3310 }
3311
3312 /* Save callee-save regs. */
3313 move_callee_saved_registers (sp, offsets, true);
3314
3315 HOST_WIDE_INT sp_adjust = offsets->pretend_size
3316 + offsets->callee_saves
3317 + offsets->local_vars + offsets->outgoing_args_size;
3318 if (sp_adjust > 0)
3319 {
3320 /* Adding RTX_FRAME_RELATED_P effectively disables spliting, so
3321 we use split add explictly, and specify the DImode add in
3322 the note. */
3323 rtx scc = gen_rtx_REG (BImode, SCC_REG);
3324 rtx adjustment = gen_int_mode (sp_adjust, SImode);
3325 rtx insn = emit_insn (gen_addsi3_scalar_carry (sp_lo, sp_lo,
3326 adjustment, scc));
3327 if (!offsets->need_frame_pointer)
3328 {
3329 RTX_FRAME_RELATED_P (insn) = 1;
3330 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3331 gen_rtx_SET (sp,
3332 gen_rtx_PLUS (DImode, sp,
3333 adjustment)));
3334 }
3335 emit_insn (gen_addcsi3_scalar_zero (sp_hi, sp_hi, scc));
3336 }
3337
3338 if (offsets->need_frame_pointer)
3339 {
3340 /* Adding RTX_FRAME_RELATED_P effectively disables spliting, so
3341 we use split add explictly, and specify the DImode add in
3342 the note. */
3343 rtx scc = gen_rtx_REG (BImode, SCC_REG);
3344 int fp_adjust = -(offsets->local_vars + offsets->outgoing_args_size);
3345 rtx adjustment = gen_int_mode (fp_adjust, SImode);
3346 rtx insn = emit_insn (gen_addsi3_scalar_carry(fp_lo, sp_lo,
3347 adjustment, scc));
3348 emit_insn (gen_addcsi3_scalar (fp_hi, sp_hi,
3349 (fp_adjust < 0 ? GEN_INT (-1)
3350 : const0_rtx),
3351 scc, scc));
3352
3353 /* Set the CFA to the entry stack address, as an offset from the
3354 frame pointer. This is preferred because the frame pointer is
3355 saved in each frame, whereas the stack pointer is not. */
3356 RTX_FRAME_RELATED_P (insn) = 1;
3357 add_reg_note (insn, REG_CFA_DEF_CFA,
3358 gen_rtx_PLUS (DImode, fp,
3359 GEN_INT (-(offsets->pretend_size
3360 + offsets->callee_saves))));
3361 }
3362
3363 rtx_insn *seq = get_insns ();
3364 end_sequence ();
3365
3366 emit_insn (seq);
3367 }
3368 else
3369 {
3370 if (TARGET_PACKED_WORK_ITEMS)
3371 {
3372 /* v0 conatins the X, Y and Z dimensions all in one.
3373 Expand them out for ABI compatibility. */
3374 /* TODO: implement and use zero_extract. */
3375 rtx v1 = gen_rtx_REG (V64SImode, VGPR_REGNO (1));
3376 emit_insn (gen_andv64si3 (v1, gen_rtx_REG (V64SImode, VGPR_REGNO (0)),
3377 gen_rtx_CONST_INT (VOIDmode, 0x3FF << 10)));
3378 emit_insn (gen_lshrv64si3 (v1, v1, gen_rtx_CONST_INT (VOIDmode, 10)));
3379 emit_insn (gen_prologue_use (v1));
3380
3381 rtx v2 = gen_rtx_REG (V64SImode, VGPR_REGNO (2));
3382 emit_insn (gen_andv64si3 (v2, gen_rtx_REG (V64SImode, VGPR_REGNO (0)),
3383 gen_rtx_CONST_INT (VOIDmode, 0x3FF << 20)));
3384 emit_insn (gen_lshrv64si3 (v2, v2, gen_rtx_CONST_INT (VOIDmode, 20)));
3385 emit_insn (gen_prologue_use (v2));
3386 }
3387
3388 /* We no longer use the private segment for the stack (it's not
3389 accessible to reverse offload), so we must calculate a wave offset
3390 from the grid dimensions and stack size, which is calculated on the
3391 host, and passed in the kernargs region.
3392 See libgomp-gcn.h for details. */
3393 rtx wave_offset = gen_rtx_REG (SImode, FIRST_PARM_REG);
3394
3395 rtx num_waves_mem = gcn_oacc_dim_size (1);
3396 rtx num_waves = gen_rtx_REG (SImode, FIRST_PARM_REG+1);
3397 set_mem_addr_space (num_waves_mem, ADDR_SPACE_SCALAR_FLAT);
3398 emit_move_insn (num_waves, num_waves_mem);
3399
3400 rtx workgroup_num = gcn_oacc_dim_pos (0);
3401 rtx wave_num = gen_rtx_REG (SImode, FIRST_PARM_REG+2);
3402 emit_move_insn(wave_num, gcn_oacc_dim_pos (1));
3403
3404 rtx thread_id = gen_rtx_REG (SImode, FIRST_PARM_REG+3);
3405 emit_insn (gen_mulsi3 (thread_id, num_waves, workgroup_num));
3406 emit_insn (gen_addsi3_scc (thread_id, thread_id, wave_num));
3407
3408 rtx kernarg_reg = gen_rtx_REG (DImode, cfun->machine->args.reg
3409 [KERNARG_SEGMENT_PTR_ARG]);
3410 rtx stack_size_mem = gen_rtx_MEM (SImode,
3411 gen_rtx_PLUS (DImode, kernarg_reg,
3412 GEN_INT (52)));
3413 set_mem_addr_space (stack_size_mem, ADDR_SPACE_SCALAR_FLAT);
3414 emit_move_insn (wave_offset, stack_size_mem);
3415
3416 emit_insn (gen_mulsi3 (wave_offset, wave_offset, thread_id));
3417
3418 /* The FLAT_SCRATCH_INIT is not usually needed, but can be enabled
3419 via the function attributes. */
3420 if (cfun->machine->args.requested & (1 << FLAT_SCRATCH_INIT_ARG))
3421 {
3422 rtx fs_init_lo =
3423 gen_rtx_REG (SImode,
3424 cfun->machine->args.reg[FLAT_SCRATCH_INIT_ARG]);
3425 rtx fs_init_hi =
3426 gen_rtx_REG (SImode,
3427 cfun->machine->args.reg[FLAT_SCRATCH_INIT_ARG] + 1);
3428 rtx fs_reg_lo = gen_rtx_REG (SImode, FLAT_SCRATCH_REG);
3429 rtx fs_reg_hi = gen_rtx_REG (SImode, FLAT_SCRATCH_REG + 1);
3430
3431 /*rtx queue = gen_rtx_REG(DImode,
3432 cfun->machine->args.reg[QUEUE_PTR_ARG]);
3433 rtx aperture = gen_rtx_MEM (SImode,
3434 gen_rtx_PLUS (DImode, queue,
3435 gen_int_mode (68, SImode)));
3436 set_mem_addr_space (aperture, ADDR_SPACE_SCALAR_FLAT);*/
3437
3438 /* Set up flat_scratch. */
3439 emit_insn (gen_addsi3_scc (fs_reg_hi, fs_init_lo, wave_offset));
3440 emit_insn (gen_lshrsi3_scc (fs_reg_hi, fs_reg_hi,
3441 gen_int_mode (8, SImode)));
3442 emit_move_insn (fs_reg_lo, fs_init_hi);
3443 }
3444
3445 /* Set up frame pointer and stack pointer. */
3446 rtx sp = gen_rtx_REG (DImode, STACK_POINTER_REGNUM);
3447 rtx sp_hi = simplify_gen_subreg (SImode, sp, DImode, 4);
3448 rtx sp_lo = simplify_gen_subreg (SImode, sp, DImode, 0);
3449 rtx fp = gen_rtx_REG (DImode, HARD_FRAME_POINTER_REGNUM);
3450 rtx fp_hi = simplify_gen_subreg (SImode, fp, DImode, 4);
3451 rtx fp_lo = simplify_gen_subreg (SImode, fp, DImode, 0);
3452
3453 HOST_WIDE_INT sp_adjust = (offsets->local_vars
3454 + offsets->outgoing_args_size);
3455
3456 /* Initialize FP and SP from space allocated on the host. */
3457 rtx stack_addr_mem = gen_rtx_MEM (DImode,
3458 gen_rtx_PLUS (DImode, kernarg_reg,
3459 GEN_INT (40)));
3460 set_mem_addr_space (stack_addr_mem, ADDR_SPACE_SCALAR_FLAT);
3461 emit_move_insn (fp, stack_addr_mem);
3462 rtx scc = gen_rtx_REG (BImode, SCC_REG);
3463 emit_insn (gen_addsi3_scalar_carry (fp_lo, fp_lo, wave_offset, scc));
3464 emit_insn (gen_addcsi3_scalar_zero (fp_hi, fp_hi, scc));
3465
3466 /* Adding RTX_FRAME_RELATED_P effectively disables spliting, so we use
3467 split add explictly, and specify the DImode add in the note.
3468 The DWARF info expects that the callee-save data is in the frame,
3469 even though it isn't (because this is the entry point), so we
3470 make a notional adjustment to the DWARF frame offset here. */
3471 rtx dbg_adjustment = gen_int_mode (sp_adjust + offsets->callee_saves,
3472 DImode);
3473 rtx insn;
3474 if (sp_adjust > 0)
3475 {
3476 rtx scc = gen_rtx_REG (BImode, SCC_REG);
3477 rtx adjustment = gen_int_mode (sp_adjust, DImode);
3478 insn = emit_insn (gen_addsi3_scalar_carry(sp_lo, fp_lo, adjustment,
3479 scc));
3480 emit_insn (gen_addcsi3_scalar_zero (sp_hi, fp_hi, scc));
3481 }
3482 else
3483 insn = emit_move_insn (sp, fp);
3484 RTX_FRAME_RELATED_P (insn) = 1;
3485 add_reg_note (insn, REG_FRAME_RELATED_EXPR,
3486 gen_rtx_SET (sp, gen_rtx_PLUS (DImode, sp,
3487 dbg_adjustment)));
3488
3489 if (offsets->need_frame_pointer)
3490 {
3491 /* Set the CFA to the entry stack address, as an offset from the
3492 frame pointer. This is necessary when alloca is used, and
3493 harmless otherwise. */
3494 rtx neg_adjust = gen_int_mode (-offsets->callee_saves, DImode);
3495 add_reg_note (insn, REG_CFA_DEF_CFA,
3496 gen_rtx_PLUS (DImode, fp, neg_adjust));
3497 }
3498
3499 /* Make sure the flat scratch reg doesn't get optimised away. */
3500 emit_insn (gen_prologue_use (gen_rtx_REG (DImode, FLAT_SCRATCH_REG)));
3501 }
3502
3503 /* Ensure that the scheduler doesn't do anything unexpected. */
3504 emit_insn (gen_blockage ());
3505
3506 if (cfun && cfun->machine && !cfun->machine->normal_function && flag_openmp)
3507 {
3508 /* OpenMP kernels have an implicit call to gomp_gcn_enter_kernel. */
3509 rtx fn_reg = gen_rtx_REG (Pmode, FIRST_PARM_REG);
3510 emit_move_insn (fn_reg, gen_rtx_SYMBOL_REF (Pmode,
3511 "gomp_gcn_enter_kernel"));
3512 emit_call_insn (gen_gcn_indirect_call (fn_reg, const0_rtx));
3513 }
3514 }
3515
3516 /* Generate epilogue. Called from gen_epilogue during pro_and_epilogue pass.
3517
3518 See gcn_expand_prologue for stack details. */
3519
3520 void
3521 gcn_expand_epilogue (void)
3522 {
3523 /* Ensure that the scheduler doesn't do anything unexpected. */
3524 emit_insn (gen_blockage ());
3525
3526 if (!cfun || !cfun->machine || cfun->machine->normal_function)
3527 {
3528 machine_function *offsets = gcn_compute_frame_offsets ();
3529 rtx sp = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
3530 rtx fp = gen_rtx_REG (Pmode, HARD_FRAME_POINTER_REGNUM);
3531
3532 HOST_WIDE_INT sp_adjust = offsets->callee_saves + offsets->pretend_size;
3533
3534 if (offsets->need_frame_pointer)
3535 {
3536 /* Restore old SP from the frame pointer. */
3537 if (sp_adjust > 0)
3538 emit_insn (gen_subdi3 (sp, fp, gen_int_mode (sp_adjust, DImode)));
3539 else
3540 emit_move_insn (sp, fp);
3541 }
3542 else
3543 {
3544 /* Restore old SP from current SP. */
3545 sp_adjust += offsets->outgoing_args_size + offsets->local_vars;
3546
3547 if (sp_adjust > 0)
3548 emit_insn (gen_subdi3 (sp, sp, gen_int_mode (sp_adjust, DImode)));
3549 }
3550
3551 move_callee_saved_registers (sp, offsets, false);
3552
3553 /* There's no explicit use of the link register on the return insn. Emit
3554 one here instead. */
3555 if (offsets->lr_needs_saving)
3556 emit_use (gen_rtx_REG (DImode, LINK_REGNUM));
3557
3558 /* Similar for frame pointer. */
3559 if (offsets->need_frame_pointer)
3560 emit_use (gen_rtx_REG (DImode, HARD_FRAME_POINTER_REGNUM));
3561 }
3562 else if (flag_openmp)
3563 {
3564 /* OpenMP kernels have an implicit call to gomp_gcn_exit_kernel. */
3565 rtx fn_reg = gen_rtx_REG (Pmode, FIRST_PARM_REG);
3566 emit_move_insn (fn_reg,
3567 gen_rtx_SYMBOL_REF (Pmode, "gomp_gcn_exit_kernel"));
3568 emit_call_insn (gen_gcn_indirect_call (fn_reg, const0_rtx));
3569 }
3570 else if (TREE_CODE (TREE_TYPE (DECL_RESULT (cfun->decl))) != VOID_TYPE)
3571 {
3572 /* Assume that an exit value compatible with gcn-run is expected.
3573 That is, the third input parameter is an int*.
3574
3575 We can't allocate any new registers, but the dispatch_ptr and
3576 kernarg_reg are dead after this, so we'll use those. */
3577 rtx dispatch_ptr_reg = gen_rtx_REG (DImode, cfun->machine->args.reg
3578 [DISPATCH_PTR_ARG]);
3579 rtx kernarg_reg = gen_rtx_REG (DImode, cfun->machine->args.reg
3580 [KERNARG_SEGMENT_PTR_ARG]);
3581 rtx retptr_mem = gen_rtx_MEM (DImode,
3582 gen_rtx_PLUS (DImode, kernarg_reg,
3583 GEN_INT (16)));
3584 set_mem_addr_space (retptr_mem, ADDR_SPACE_SCALAR_FLAT);
3585 emit_move_insn (dispatch_ptr_reg, retptr_mem);
3586
3587 rtx retval_addr = gen_rtx_REG (DImode, FIRST_VPARM_REG + 2);
3588 emit_move_insn (retval_addr, dispatch_ptr_reg);
3589 rtx retval_mem = gen_rtx_MEM (SImode, retval_addr);
3590 set_mem_addr_space (retval_mem, ADDR_SPACE_FLAT);
3591 emit_move_insn (retval_mem, gen_rtx_REG (SImode, RETURN_VALUE_REG));
3592 }
3593
3594 emit_jump_insn (gen_gcn_return ());
3595 }
3596
3597 /* Implement TARGET_FRAME_POINTER_REQUIRED.
3598
3599 Return true if the frame pointer should not be eliminated. */
3600
3601 bool
3602 gcn_frame_pointer_rqd (void)
3603 {
3604 /* GDB needs the frame pointer in order to unwind properly,
3605 but that's not important for the entry point, unless alloca is used.
3606 It's not important for code execution, so we should repect the
3607 -fomit-frame-pointer flag. */
3608 return (!flag_omit_frame_pointer
3609 && cfun
3610 && (cfun->calls_alloca
3611 || (cfun->machine && cfun->machine->normal_function)));
3612 }
3613
3614 /* Implement TARGET_CAN_ELIMINATE.
3615
3616 Return true if the compiler is allowed to try to replace register number
3617 FROM_REG with register number TO_REG.
3618
3619 FIXME: is the default "true" not enough? Should this be a negative set? */
3620
3621 bool
3622 gcn_can_eliminate_p (int /*from_reg */ , int to_reg)
3623 {
3624 return (to_reg == HARD_FRAME_POINTER_REGNUM
3625 || to_reg == STACK_POINTER_REGNUM);
3626 }
3627
3628 /* Implement INITIAL_ELIMINATION_OFFSET.
3629
3630 Returns the initial difference between the specified pair of registers, in
3631 terms of stack position. */
3632
3633 HOST_WIDE_INT
3634 gcn_initial_elimination_offset (int from, int to)
3635 {
3636 machine_function *offsets = gcn_compute_frame_offsets ();
3637
3638 switch (from)
3639 {
3640 case ARG_POINTER_REGNUM:
3641 if (to == STACK_POINTER_REGNUM)
3642 return -(offsets->callee_saves + offsets->local_vars
3643 + offsets->outgoing_args_size);
3644 else if (to == FRAME_POINTER_REGNUM || to == HARD_FRAME_POINTER_REGNUM)
3645 return -offsets->callee_saves;
3646 else
3647 gcc_unreachable ();
3648 break;
3649
3650 case FRAME_POINTER_REGNUM:
3651 if (to == STACK_POINTER_REGNUM)
3652 return -(offsets->local_vars + offsets->outgoing_args_size);
3653 else if (to == HARD_FRAME_POINTER_REGNUM)
3654 return 0;
3655 else
3656 gcc_unreachable ();
3657 break;
3658
3659 default:
3660 gcc_unreachable ();
3661 }
3662 }
3663
3664 /* Implement HARD_REGNO_RENAME_OK.
3665
3666 Return true if it is permissible to rename a hard register from
3667 FROM_REG to TO_REG. */
3668
3669 bool
3670 gcn_hard_regno_rename_ok (unsigned int from_reg, unsigned int to_reg)
3671 {
3672 if (from_reg == SCC_REG
3673 || from_reg == VCC_LO_REG || from_reg == VCC_HI_REG
3674 || from_reg == EXEC_LO_REG || from_reg == EXEC_HI_REG
3675 || to_reg == SCC_REG
3676 || to_reg == VCC_LO_REG || to_reg == VCC_HI_REG
3677 || to_reg == EXEC_LO_REG || to_reg == EXEC_HI_REG)
3678 return false;
3679
3680 /* Allow the link register to be used if it was saved. */
3681 if ((to_reg & ~1) == LINK_REGNUM)
3682 return !cfun || cfun->machine->lr_needs_saving;
3683
3684 /* Allow the registers used for the static chain to be used if the chain is
3685 not in active use. */
3686 if ((to_reg & ~1) == STATIC_CHAIN_REGNUM)
3687 return !cfun
3688 || !(cfun->static_chain_decl
3689 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM)
3690 && df_regs_ever_live_p (STATIC_CHAIN_REGNUM + 1));
3691
3692 return true;
3693 }
3694
3695 /* Implement HARD_REGNO_CALLER_SAVE_MODE.
3696
3697 Which mode is required for saving NREGS of a pseudo-register in
3698 call-clobbered hard register REGNO. */
3699
3700 machine_mode
3701 gcn_hard_regno_caller_save_mode (unsigned int regno, unsigned int nregs,
3702 machine_mode regmode)
3703 {
3704 machine_mode result = choose_hard_reg_mode (regno, nregs, NULL);
3705
3706 if (VECTOR_MODE_P (result) && !VECTOR_MODE_P (regmode))
3707 result = (nregs == 1 ? SImode : DImode);
3708
3709 return result;
3710 }
3711
3712 /* Implement TARGET_ASM_TRAMPOLINE_TEMPLATE.
3713
3714 Output assembler code for a block containing the constant parts
3715 of a trampoline, leaving space for the variable parts. */
3716
3717 static void
3718 gcn_asm_trampoline_template (FILE *f)
3719 {
3720 /* The source operand of the move instructions must be a 32-bit
3721 constant following the opcode. */
3722 asm_fprintf (f, "\ts_mov_b32\ts%i, 0xffff\n", STATIC_CHAIN_REGNUM);
3723 asm_fprintf (f, "\ts_mov_b32\ts%i, 0xffff\n", STATIC_CHAIN_REGNUM + 1);
3724 asm_fprintf (f, "\ts_mov_b32\ts%i, 0xffff\n", CC_SAVE_REG);
3725 asm_fprintf (f, "\ts_mov_b32\ts%i, 0xffff\n", CC_SAVE_REG + 1);
3726 asm_fprintf (f, "\ts_setpc_b64\ts[%i:%i]\n", CC_SAVE_REG, CC_SAVE_REG + 1);
3727 asm_fprintf (f, "\t.align 8\n");
3728 }
3729
3730 /* Implement TARGET_TRAMPOLINE_INIT.
3731
3732 Emit RTL insns to initialize the variable parts of a trampoline.
3733 FNDECL is the decl of the target address, M_TRAMP is a MEM for
3734 the trampoline, and CHAIN_VALUE is an RTX for the static chain
3735 to be passed to the target function. */
3736
3737 static void
3738 gcn_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
3739 {
3740 emit_block_move (m_tramp, assemble_trampoline_template (),
3741 GEN_INT (TRAMPOLINE_SIZE), BLOCK_OP_NORMAL);
3742
3743 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
3744 rtx chain_value_reg = copy_to_reg (chain_value);
3745 rtx fnaddr_reg = copy_to_reg (fnaddr);
3746
3747 for (int i = 0; i < 4; i++)
3748 {
3749 rtx mem = adjust_address (m_tramp, SImode, i * 8 + 4);
3750 rtx reg = i < 2 ? chain_value_reg : fnaddr_reg;
3751 emit_move_insn (mem, gen_rtx_SUBREG (SImode, reg, (i % 2) * 4));
3752 }
3753
3754 rtx tramp_addr = XEXP (m_tramp, 0);
3755 emit_insn (gen_clear_icache (tramp_addr,
3756 plus_constant (ptr_mode, tramp_addr,
3757 TRAMPOLINE_SIZE)));
3758 }
3759
3760 /* Implement TARGET_EXPAND_DIVMOD_LIBFUNC.
3761
3762 There are divmod libfuncs for all modes except TImode. They return the
3763 two values packed into a larger integer/vector. */
3764
3765 void
3766 gcn_expand_divmod_libfunc (rtx libfunc, machine_mode mode, rtx op0, rtx op1,
3767 rtx *quot, rtx *rem)
3768 {
3769 machine_mode innermode = (VECTOR_MODE_P (mode)
3770 ? GET_MODE_INNER (mode) : mode);
3771 machine_mode wideinnermode = VOIDmode;
3772 machine_mode widemode = VOIDmode;
3773
3774 switch (innermode)
3775 {
3776 case E_QImode:
3777 case E_HImode:
3778 case E_SImode:
3779 wideinnermode = DImode;
3780 break;
3781 case E_DImode:
3782 wideinnermode = TImode;
3783 break;
3784 default:
3785 gcc_unreachable ();
3786 }
3787
3788 if (VECTOR_MODE_P (mode))
3789 widemode = VnMODE (GET_MODE_NUNITS (mode), wideinnermode);
3790 else
3791 widemode = wideinnermode;
3792
3793 emit_library_call_value (libfunc, gen_rtx_REG (widemode, RETURN_VALUE_REG),
3794 LCT_NORMAL, widemode, op0, mode, op1, mode);
3795
3796 *quot = gen_rtx_REG (mode, RETURN_VALUE_REG);
3797 *rem = gen_rtx_REG (mode,
3798 RETURN_VALUE_REG + (wideinnermode == TImode ? 2 : 1));
3799 }
3800
3801 /* }}} */
3802 /* {{{ Miscellaneous. */
3803
3804 /* Implement TARGET_CANNOT_COPY_INSN_P.
3805
3806 Return true if INSN must not be duplicated. */
3807
3808 static bool
3809 gcn_cannot_copy_insn_p (rtx_insn *insn)
3810 {
3811 if (recog_memoized (insn) == CODE_FOR_gcn_wavefront_barrier)
3812 return true;
3813
3814 return false;
3815 }
3816
3817 /* Implement TARGET_DEBUG_UNWIND_INFO.
3818
3819 Defines the mechanism that will be used for describing frame unwind
3820 information to the debugger. */
3821
3822 static enum unwind_info_type
3823 gcn_debug_unwind_info ()
3824 {
3825 return UI_DWARF2;
3826 }
3827
3828 /* Determine if there is a suitable hardware conversion instruction.
3829 Used primarily by the machine description. */
3830
3831 bool
3832 gcn_valid_cvt_p (machine_mode from, machine_mode to, enum gcn_cvt_t op)
3833 {
3834 if (VECTOR_MODE_P (from) != VECTOR_MODE_P (to))
3835 return false;
3836
3837 if (VECTOR_MODE_P (from))
3838 {
3839 if (GET_MODE_NUNITS (from) != GET_MODE_NUNITS (to))
3840 return false;
3841
3842 from = GET_MODE_INNER (from);
3843 to = GET_MODE_INNER (to);
3844 }
3845
3846 switch (op)
3847 {
3848 case fix_trunc_cvt:
3849 case fixuns_trunc_cvt:
3850 if (GET_MODE_CLASS (from) != MODE_FLOAT
3851 || GET_MODE_CLASS (to) != MODE_INT)
3852 return false;
3853 break;
3854 case float_cvt:
3855 case floatuns_cvt:
3856 if (GET_MODE_CLASS (from) != MODE_INT
3857 || GET_MODE_CLASS (to) != MODE_FLOAT)
3858 return false;
3859 break;
3860 case extend_cvt:
3861 if (GET_MODE_CLASS (from) != MODE_FLOAT
3862 || GET_MODE_CLASS (to) != MODE_FLOAT
3863 || GET_MODE_SIZE (from) >= GET_MODE_SIZE (to))
3864 return false;
3865 break;
3866 case trunc_cvt:
3867 if (GET_MODE_CLASS (from) != MODE_FLOAT
3868 || GET_MODE_CLASS (to) != MODE_FLOAT
3869 || GET_MODE_SIZE (from) <= GET_MODE_SIZE (to))
3870 return false;
3871 break;
3872 }
3873
3874 return ((to == HImode && from == HFmode)
3875 || (to == SImode && (from == SFmode || from == DFmode))
3876 || (to == HFmode && (from == HImode || from == SFmode))
3877 || (to == SFmode && (from == SImode || from == HFmode
3878 || from == DFmode))
3879 || (to == DFmode && (from == SImode || from == SFmode)));
3880 }
3881
3882 /* Implement TARGET_EMUTLS_VAR_INIT.
3883
3884 Disable emutls (gthr-gcn.h does not support it, yet). */
3885
3886 tree
3887 gcn_emutls_var_init (tree, tree decl, tree)
3888 {
3889 sorry_at (DECL_SOURCE_LOCATION (decl), "TLS is not implemented for GCN.");
3890 return NULL_TREE;
3891 }
3892
3893 /* }}} */
3894 /* {{{ Costs. */
3895
3896 /* Implement TARGET_RTX_COSTS.
3897
3898 Compute a (partial) cost for rtx X. Return true if the complete
3899 cost has been computed, and false if subexpressions should be
3900 scanned. In either case, *TOTAL contains the cost result. */
3901
3902 static bool
3903 gcn_rtx_costs (rtx x, machine_mode, int, int, int *total, bool)
3904 {
3905 enum rtx_code code = GET_CODE (x);
3906 switch (code)
3907 {
3908 case CONST:
3909 case CONST_DOUBLE:
3910 case CONST_VECTOR:
3911 case CONST_INT:
3912 if (gcn_inline_constant_p (x))
3913 *total = 0;
3914 else if (code == CONST_INT
3915 && ((unsigned HOST_WIDE_INT) INTVAL (x) + 0x8000) < 0x10000)
3916 *total = 1;
3917 else if (gcn_constant_p (x))
3918 *total = 2;
3919 else
3920 *total = vgpr_vector_mode_p (GET_MODE (x)) ? 64 : 4;
3921 return true;
3922
3923 case DIV:
3924 *total = 100;
3925 return false;
3926
3927 default:
3928 *total = 3;
3929 return false;
3930 }
3931 }
3932
3933 /* Implement TARGET_MEMORY_MOVE_COST.
3934
3935 Return the cost of moving data of mode M between a
3936 register and memory. A value of 2 is the default; this cost is
3937 relative to those in `REGISTER_MOVE_COST'.
3938
3939 This function is used extensively by register_move_cost that is used to
3940 build tables at startup. Make it inline in this case.
3941 When IN is 2, return maximum of in and out move cost.
3942
3943 If moving between registers and memory is more expensive than
3944 between two registers, you should define this macro to express the
3945 relative cost.
3946
3947 Model also increased moving costs of QImode registers in non
3948 Q_REGS classes. */
3949
3950 #define LOAD_COST 32
3951 #define STORE_COST 32
3952 static int
3953 gcn_memory_move_cost (machine_mode mode, reg_class_t regclass, bool in)
3954 {
3955 int nregs = CEIL (GET_MODE_SIZE (mode), 4);
3956 switch (regclass)
3957 {
3958 case SCC_CONDITIONAL_REG:
3959 case VCCZ_CONDITIONAL_REG:
3960 case VCC_CONDITIONAL_REG:
3961 case EXECZ_CONDITIONAL_REG:
3962 case ALL_CONDITIONAL_REGS:
3963 case SGPR_REGS:
3964 case SGPR_EXEC_REGS:
3965 case EXEC_MASK_REG:
3966 case SGPR_VOP_SRC_REGS:
3967 case SGPR_MEM_SRC_REGS:
3968 case SGPR_SRC_REGS:
3969 case SGPR_DST_REGS:
3970 case GENERAL_REGS:
3971 case AFP_REGS:
3972 if (!in)
3973 return (STORE_COST + 2) * nregs;
3974 return LOAD_COST * nregs;
3975 case VGPR_REGS:
3976 if (in)
3977 return (LOAD_COST + 2) * nregs;
3978 return STORE_COST * nregs;
3979 case AVGPR_REGS:
3980 case ALL_VGPR_REGS:
3981 if (in)
3982 return (LOAD_COST + (TARGET_CDNA2_MEM_COSTS ? 2 : 4)) * nregs;
3983 return (STORE_COST + (TARGET_CDNA2_MEM_COSTS ? 0 : 2)) * nregs;
3984 case ALL_REGS:
3985 case ALL_GPR_REGS:
3986 case SRCDST_REGS:
3987 if (in)
3988 return (LOAD_COST + 2) * nregs;
3989 return (STORE_COST + 2) * nregs;
3990 default:
3991 gcc_unreachable ();
3992 }
3993 }
3994
3995 /* Implement TARGET_REGISTER_MOVE_COST.
3996
3997 Return the cost of moving data from a register in class CLASS1 to
3998 one in class CLASS2. Base value is 2. */
3999
4000 static int
4001 gcn_register_move_cost (machine_mode, reg_class_t dst, reg_class_t src)
4002 {
4003 if (src == AVGPR_REGS)
4004 {
4005 if (dst == AVGPR_REGS)
4006 return !TARGET_AVGPR_MEMOPS ? 6 : 2;
4007 if (dst != VGPR_REGS)
4008 return 6;
4009 }
4010 if (dst == AVGPR_REGS && src != VGPR_REGS)
4011 return 6;
4012 /* Increase cost of moving from and to vector registers. While this is
4013 fast in hardware (I think), it has hidden cost of setting up the exec
4014 flags. */
4015 if ((src < VGPR_REGS) != (dst < VGPR_REGS))
4016 return 4;
4017 return 2;
4018 }
4019
4020 /* }}} */
4021 /* {{{ Builtins. */
4022
4023 /* Type codes used by GCN built-in definitions. */
4024
4025 enum gcn_builtin_type_index
4026 {
4027 GCN_BTI_END_OF_PARAMS,
4028
4029 GCN_BTI_VOID,
4030 GCN_BTI_BOOL,
4031 GCN_BTI_INT,
4032 GCN_BTI_UINT,
4033 GCN_BTI_SIZE_T,
4034 GCN_BTI_LLINT,
4035 GCN_BTI_LLUINT,
4036 GCN_BTI_EXEC,
4037
4038 GCN_BTI_SF,
4039 GCN_BTI_V64SI,
4040 GCN_BTI_V64SF,
4041 GCN_BTI_V64DF,
4042 GCN_BTI_V64PTR,
4043 GCN_BTI_SIPTR,
4044 GCN_BTI_SFPTR,
4045 GCN_BTI_VOIDPTR,
4046
4047 GCN_BTI_LDS_VOIDPTR,
4048
4049 GCN_BTI_MAX
4050 };
4051
4052 static GTY(()) tree gcn_builtin_types[GCN_BTI_MAX];
4053
4054 #define exec_type_node (gcn_builtin_types[GCN_BTI_EXEC])
4055 #define sf_type_node (gcn_builtin_types[GCN_BTI_SF])
4056 #define v64si_type_node (gcn_builtin_types[GCN_BTI_V64SI])
4057 #define v64sf_type_node (gcn_builtin_types[GCN_BTI_V64SF])
4058 #define v64df_type_node (gcn_builtin_types[GCN_BTI_V64DF])
4059 #define v64ptr_type_node (gcn_builtin_types[GCN_BTI_V64PTR])
4060 #define siptr_type_node (gcn_builtin_types[GCN_BTI_SIPTR])
4061 #define sfptr_type_node (gcn_builtin_types[GCN_BTI_SFPTR])
4062 #define voidptr_type_node (gcn_builtin_types[GCN_BTI_VOIDPTR])
4063 #define size_t_type_node (gcn_builtin_types[GCN_BTI_SIZE_T])
4064
4065 static rtx gcn_expand_builtin_1 (tree, rtx, rtx, machine_mode, int,
4066 struct gcn_builtin_description *);
4067 static rtx gcn_expand_builtin_binop (tree, rtx, rtx, machine_mode, int,
4068 struct gcn_builtin_description *);
4069
4070 struct gcn_builtin_description;
4071 typedef rtx (*gcn_builtin_expander) (tree, rtx, rtx, machine_mode, int,
4072 struct gcn_builtin_description *);
4073
4074 enum gcn_builtin_type
4075 {
4076 B_UNIMPLEMENTED, /* Sorry out */
4077 B_INSN, /* Emit a pattern */
4078 B_OVERLOAD /* Placeholder for an overloaded function */
4079 };
4080
4081 struct gcn_builtin_description
4082 {
4083 int fcode;
4084 int icode;
4085 const char *name;
4086 enum gcn_builtin_type type;
4087 /* The first element of parm is always the return type. The rest
4088 are a zero terminated list of parameters. */
4089 int parm[6];
4090 gcn_builtin_expander expander;
4091 };
4092
4093 /* Read in the GCN builtins from gcn-builtins.def. */
4094
4095 extern GTY(()) struct gcn_builtin_description gcn_builtins[GCN_BUILTIN_MAX];
4096
4097 struct gcn_builtin_description gcn_builtins[] = {
4098 #define DEF_BUILTIN(fcode, icode, name, type, params, expander) \
4099 {GCN_BUILTIN_ ## fcode, icode, name, type, params, expander},
4100
4101 #define DEF_BUILTIN_BINOP_INT_FP(fcode, ic, name) \
4102 {GCN_BUILTIN_ ## fcode ## _V64SI, \
4103 CODE_FOR_ ## ic ##v64si3_exec, name "_v64int", B_INSN, \
4104 {GCN_BTI_V64SI, GCN_BTI_EXEC, GCN_BTI_V64SI, GCN_BTI_V64SI, \
4105 GCN_BTI_V64SI, GCN_BTI_END_OF_PARAMS}, gcn_expand_builtin_binop}, \
4106 {GCN_BUILTIN_ ## fcode ## _V64SI_unspec, \
4107 CODE_FOR_ ## ic ##v64si3_exec, name "_v64int_unspec", B_INSN, \
4108 {GCN_BTI_V64SI, GCN_BTI_EXEC, GCN_BTI_V64SI, GCN_BTI_V64SI, \
4109 GCN_BTI_END_OF_PARAMS}, gcn_expand_builtin_binop},
4110
4111 #include "gcn-builtins.def"
4112 #undef DEF_BUILTIN_BINOP_INT_FP
4113 #undef DEF_BUILTIN
4114 };
4115
4116 static GTY(()) tree gcn_builtin_decls[GCN_BUILTIN_MAX];
4117
4118 /* Implement TARGET_BUILTIN_DECL.
4119
4120 Return the GCN builtin for CODE. */
4121
4122 tree
4123 gcn_builtin_decl (unsigned code, bool ARG_UNUSED (initialize_p))
4124 {
4125 if (code >= GCN_BUILTIN_MAX)
4126 return error_mark_node;
4127
4128 return gcn_builtin_decls[code];
4129 }
4130
4131 /* Helper function for gcn_init_builtins. */
4132
4133 static void
4134 gcn_init_builtin_types (void)
4135 {
4136 gcn_builtin_types[GCN_BTI_VOID] = void_type_node;
4137 gcn_builtin_types[GCN_BTI_BOOL] = boolean_type_node;
4138 gcn_builtin_types[GCN_BTI_INT] = intSI_type_node;
4139 gcn_builtin_types[GCN_BTI_UINT] = unsigned_type_for (intSI_type_node);
4140 gcn_builtin_types[GCN_BTI_SIZE_T] = size_type_node;
4141 gcn_builtin_types[GCN_BTI_LLINT] = intDI_type_node;
4142 gcn_builtin_types[GCN_BTI_LLUINT] = unsigned_type_for (intDI_type_node);
4143
4144 exec_type_node = unsigned_intDI_type_node;
4145 sf_type_node = float32_type_node;
4146 v64si_type_node = build_vector_type (intSI_type_node, 64);
4147 v64sf_type_node = build_vector_type (float_type_node, 64);
4148 v64df_type_node = build_vector_type (double_type_node, 64);
4149 v64ptr_type_node = build_vector_type (unsigned_intDI_type_node
4150 /*build_pointer_type
4151 (integer_type_node) */
4152 , 64);
4153 tree tmp = build_distinct_type_copy (intSI_type_node);
4154 TYPE_ADDR_SPACE (tmp) = ADDR_SPACE_DEFAULT;
4155 siptr_type_node = build_pointer_type (tmp);
4156
4157 tmp = build_distinct_type_copy (float_type_node);
4158 TYPE_ADDR_SPACE (tmp) = ADDR_SPACE_DEFAULT;
4159 sfptr_type_node = build_pointer_type (tmp);
4160
4161 tmp = build_distinct_type_copy (void_type_node);
4162 TYPE_ADDR_SPACE (tmp) = ADDR_SPACE_DEFAULT;
4163 voidptr_type_node = build_pointer_type (tmp);
4164
4165 tmp = build_distinct_type_copy (void_type_node);
4166 TYPE_ADDR_SPACE (tmp) = ADDR_SPACE_LDS;
4167 gcn_builtin_types[GCN_BTI_LDS_VOIDPTR] = build_pointer_type (tmp);
4168 }
4169
4170 /* Implement TARGET_INIT_BUILTINS.
4171
4172 Set up all builtin functions for this target. */
4173
4174 static void
4175 gcn_init_builtins (void)
4176 {
4177 gcn_init_builtin_types ();
4178
4179 struct gcn_builtin_description *d;
4180 unsigned int i;
4181 for (i = 0, d = gcn_builtins; i < GCN_BUILTIN_MAX; i++, d++)
4182 {
4183 tree p;
4184 char name[64]; /* build_function will make a copy. */
4185 int parm;
4186
4187 /* FIXME: Is this necessary/useful? */
4188 if (d->name == 0)
4189 continue;
4190
4191 /* Find last parm. */
4192 for (parm = 1; d->parm[parm] != GCN_BTI_END_OF_PARAMS; parm++)
4193 ;
4194
4195 p = void_list_node;
4196 while (parm > 1)
4197 p = tree_cons (NULL_TREE, gcn_builtin_types[d->parm[--parm]], p);
4198
4199 p = build_function_type (gcn_builtin_types[d->parm[0]], p);
4200
4201 sprintf (name, "__builtin_gcn_%s", d->name);
4202 gcn_builtin_decls[i]
4203 = add_builtin_function (name, p, i, BUILT_IN_MD, NULL, NULL_TREE);
4204
4205 /* These builtins don't throw. */
4206 TREE_NOTHROW (gcn_builtin_decls[i]) = 1;
4207 }
4208
4209 /* These builtins need to take/return an LDS pointer: override the generic
4210 versions here. */
4211
4212 set_builtin_decl (BUILT_IN_GOACC_SINGLE_START,
4213 gcn_builtin_decls[GCN_BUILTIN_ACC_SINGLE_START], false);
4214
4215 set_builtin_decl (BUILT_IN_GOACC_SINGLE_COPY_START,
4216 gcn_builtin_decls[GCN_BUILTIN_ACC_SINGLE_COPY_START],
4217 false);
4218
4219 set_builtin_decl (BUILT_IN_GOACC_SINGLE_COPY_END,
4220 gcn_builtin_decls[GCN_BUILTIN_ACC_SINGLE_COPY_END],
4221 false);
4222
4223 set_builtin_decl (BUILT_IN_GOACC_BARRIER,
4224 gcn_builtin_decls[GCN_BUILTIN_ACC_BARRIER], false);
4225 }
4226
4227 /* Implement TARGET_INIT_LIBFUNCS. */
4228
4229 static void
4230 gcn_init_libfuncs (void)
4231 {
4232 /* BITS_PER_UNIT * 2 is 64 bits, which causes
4233 optabs-libfuncs.cc:gen_int_libfunc to omit TImode (i.e 128 bits)
4234 libcalls that we need to support operations for that type. Initialise
4235 them here instead. */
4236 set_optab_libfunc (udiv_optab, TImode, "__udivti3");
4237 set_optab_libfunc (umod_optab, TImode, "__umodti3");
4238 set_optab_libfunc (sdiv_optab, TImode, "__divti3");
4239 set_optab_libfunc (smod_optab, TImode, "__modti3");
4240 set_optab_libfunc (smul_optab, TImode, "__multi3");
4241 set_optab_libfunc (addv_optab, TImode, "__addvti3");
4242 set_optab_libfunc (subv_optab, TImode, "__subvti3");
4243 set_optab_libfunc (negv_optab, TImode, "__negvti2");
4244 set_optab_libfunc (absv_optab, TImode, "__absvti2");
4245 set_optab_libfunc (smulv_optab, TImode, "__mulvti3");
4246 set_optab_libfunc (ffs_optab, TImode, "__ffsti2");
4247 set_optab_libfunc (clz_optab, TImode, "__clzti2");
4248 set_optab_libfunc (ctz_optab, TImode, "__ctzti2");
4249 set_optab_libfunc (clrsb_optab, TImode, "__clrsbti2");
4250 set_optab_libfunc (popcount_optab, TImode, "__popcountti2");
4251 set_optab_libfunc (parity_optab, TImode, "__parityti2");
4252 set_optab_libfunc (bswap_optab, TImode, "__bswapti2");
4253
4254 set_optab_libfunc (sdivmod_optab, SImode, "__divmodsi4");
4255 set_optab_libfunc (udivmod_optab, SImode, "__udivmodsi4");
4256 set_optab_libfunc (sdivmod_optab, DImode, "__divmoddi4");
4257 set_optab_libfunc (udivmod_optab, DImode, "__udivmoddi4");
4258
4259 set_optab_libfunc (sdiv_optab, V2QImode, "__divv2qi3");
4260 set_optab_libfunc (udiv_optab, V2QImode, "__udivv2qi3");
4261 set_optab_libfunc (smod_optab, V2QImode, "__modv2qi3");
4262 set_optab_libfunc (umod_optab, V2QImode, "__umodv2qi3");
4263 #if 0
4264 set_optab_libfunc (sdivmod_optab, V2QImode, "__divmodv2qi4");
4265 set_optab_libfunc (udivmod_optab, V2QImode, "__udivmodv2qi4");
4266 #endif
4267 set_optab_libfunc (sdiv_optab, V4QImode, "__divv4qi3");
4268 set_optab_libfunc (udiv_optab, V4QImode, "__udivv4qi3");
4269 set_optab_libfunc (smod_optab, V4QImode, "__modv4qi3");
4270 set_optab_libfunc (umod_optab, V4QImode, "__umodv4qi3");
4271 #if 0
4272 set_optab_libfunc (sdivmod_optab, V4QImode, "__divmodv4qi4");
4273 set_optab_libfunc (udivmod_optab, V4QImode, "__udivmodv4qi4");
4274 #endif
4275 set_optab_libfunc (sdiv_optab, V8QImode, "__divv8qi3");
4276 set_optab_libfunc (udiv_optab, V8QImode, "__udivv8qi3");
4277 set_optab_libfunc (smod_optab, V8QImode, "__modv8qi3");
4278 set_optab_libfunc (umod_optab, V8QImode, "__umodv8qi3");
4279 #if 0
4280 set_optab_libfunc (sdivmod_optab, V8QImode, "__divmodv8qi4");
4281 set_optab_libfunc (udivmod_optab, V8QImode, "__udivmodv8qi4");
4282 #endif
4283 set_optab_libfunc (sdiv_optab, V16QImode, "__divv16qi3");
4284 set_optab_libfunc (udiv_optab, V16QImode, "__udivv16qi3");
4285 set_optab_libfunc (smod_optab, V16QImode, "__modv16qi3");
4286 set_optab_libfunc (umod_optab, V16QImode, "__umodv16qi3");
4287 #if 0
4288 set_optab_libfunc (sdivmod_optab, V16QImode, "__divmodv16qi4");
4289 set_optab_libfunc (udivmod_optab, V16QImode, "__udivmodv16qi4");
4290 #endif
4291 set_optab_libfunc (sdiv_optab, V32QImode, "__divv32qi3");
4292 set_optab_libfunc (udiv_optab, V32QImode, "__udivv32qi3");
4293 set_optab_libfunc (smod_optab, V32QImode, "__modv32qi3");
4294 set_optab_libfunc (umod_optab, V32QImode, "__umodv32qi3");
4295 #if 0
4296 set_optab_libfunc (sdivmod_optab, V32QImode, "__divmodv32qi4");
4297 set_optab_libfunc (udivmod_optab, V32QImode, "__udivmodv32qi4");
4298 #endif
4299 set_optab_libfunc (sdiv_optab, V64QImode, "__divv64qi3");
4300 set_optab_libfunc (udiv_optab, V64QImode, "__udivv64qi3");
4301 set_optab_libfunc (smod_optab, V64QImode, "__modv64qi3");
4302 set_optab_libfunc (umod_optab, V64QImode, "__umodv64qi3");
4303 #if 0
4304 set_optab_libfunc (sdivmod_optab, V64QImode, "__divmodv64qi4");
4305 set_optab_libfunc (udivmod_optab, V64QImode, "__udivmodv64qi4");
4306 #endif
4307
4308 set_optab_libfunc (sdiv_optab, V2HImode, "__divv2hi3");
4309 set_optab_libfunc (udiv_optab, V2HImode, "__udivv2hi3");
4310 set_optab_libfunc (smod_optab, V2HImode, "__modv2hi3");
4311 set_optab_libfunc (umod_optab, V2HImode, "__umodv2hi3");
4312 #if 0
4313 set_optab_libfunc (sdivmod_optab, V2HImode, "__divmodv2hi4");
4314 set_optab_libfunc (udivmod_optab, V2HImode, "__udivmodv2hi4");
4315 #endif
4316 set_optab_libfunc (sdiv_optab, V4HImode, "__divv4hi3");
4317 set_optab_libfunc (udiv_optab, V4HImode, "__udivv4hi3");
4318 set_optab_libfunc (smod_optab, V4HImode, "__modv4hi3");
4319 set_optab_libfunc (umod_optab, V4HImode, "__umodv4hi3");
4320 #if 0
4321 set_optab_libfunc (sdivmod_optab, V4HImode, "__divmodv4hi4");
4322 set_optab_libfunc (udivmod_optab, V4HImode, "__udivmodv4hi4");
4323 #endif
4324 set_optab_libfunc (sdiv_optab, V8HImode, "__divv8hi3");
4325 set_optab_libfunc (udiv_optab, V8HImode, "__udivv8hi3");
4326 set_optab_libfunc (smod_optab, V8HImode, "__modv8hi3");
4327 set_optab_libfunc (umod_optab, V8HImode, "__umodv8hi3");
4328 #if 0
4329 set_optab_libfunc (sdivmod_optab, V8HImode, "__divmodv8hi4");
4330 set_optab_libfunc (udivmod_optab, V8HImode, "__udivmodv8hi4");
4331 #endif
4332 set_optab_libfunc (sdiv_optab, V16HImode, "__divv16hi3");
4333 set_optab_libfunc (udiv_optab, V16HImode, "__udivv16hi3");
4334 set_optab_libfunc (smod_optab, V16HImode, "__modv16hi3");
4335 set_optab_libfunc (umod_optab, V16HImode, "__umodv16hi3");
4336 #if 0
4337 set_optab_libfunc (sdivmod_optab, V16HImode, "__divmodv16hi4");
4338 set_optab_libfunc (udivmod_optab, V16HImode, "__udivmodv16hi4");
4339 #endif
4340 set_optab_libfunc (sdiv_optab, V32HImode, "__divv32hi3");
4341 set_optab_libfunc (udiv_optab, V32HImode, "__udivv32hi3");
4342 set_optab_libfunc (smod_optab, V32HImode, "__modv32hi3");
4343 set_optab_libfunc (umod_optab, V32HImode, "__umodv32hi3");
4344 #if 0
4345 set_optab_libfunc (sdivmod_optab, V32HImode, "__divmodv32hi4");
4346 set_optab_libfunc (udivmod_optab, V32HImode, "__udivmodv32hi4");
4347 #endif
4348 set_optab_libfunc (sdiv_optab, V64HImode, "__divv64hi3");
4349 set_optab_libfunc (udiv_optab, V64HImode, "__udivv64hi3");
4350 set_optab_libfunc (smod_optab, V64HImode, "__modv64hi3");
4351 set_optab_libfunc (umod_optab, V64HImode, "__umodv64hi3");
4352 #if 0
4353 set_optab_libfunc (sdivmod_optab, V64HImode, "__divmodv64hi4");
4354 set_optab_libfunc (udivmod_optab, V64HImode, "__udivmodv64hi4");
4355 #endif
4356
4357 set_optab_libfunc (sdiv_optab, V2SImode, "__divv2si3");
4358 set_optab_libfunc (udiv_optab, V2SImode, "__udivv2si3");
4359 set_optab_libfunc (smod_optab, V2SImode, "__modv2si3");
4360 set_optab_libfunc (umod_optab, V2SImode, "__umodv2si3");
4361 #if 0
4362 set_optab_libfunc (sdivmod_optab, V2SImode, "__divmodv2si4");
4363 set_optab_libfunc (udivmod_optab, V2SImode, "__udivmodv2si4");
4364 #endif
4365 set_optab_libfunc (sdiv_optab, V4SImode, "__divv4si3");
4366 set_optab_libfunc (udiv_optab, V4SImode, "__udivv4si3");
4367 set_optab_libfunc (smod_optab, V4SImode, "__modv4si3");
4368 set_optab_libfunc (umod_optab, V4SImode, "__umodv4si3");
4369 #if 0
4370 set_optab_libfunc (sdivmod_optab, V4SImode, "__divmodv4si4");
4371 set_optab_libfunc (udivmod_optab, V4SImode, "__udivmodv4si4");
4372 #endif
4373 set_optab_libfunc (sdiv_optab, V8SImode, "__divv8si3");
4374 set_optab_libfunc (udiv_optab, V8SImode, "__udivv8si3");
4375 set_optab_libfunc (smod_optab, V8SImode, "__modv8si3");
4376 set_optab_libfunc (umod_optab, V8SImode, "__umodv8si3");
4377 #if 0
4378 set_optab_libfunc (sdivmod_optab, V8SImode, "__divmodv8si4");
4379 set_optab_libfunc (udivmod_optab, V8SImode, "__udivmodv8si4");
4380 #endif
4381 set_optab_libfunc (sdiv_optab, V16SImode, "__divv16si3");
4382 set_optab_libfunc (udiv_optab, V16SImode, "__udivv16si3");
4383 set_optab_libfunc (smod_optab, V16SImode, "__modv16si3");
4384 set_optab_libfunc (umod_optab, V16SImode, "__umodv16si3");
4385 #if 0
4386 set_optab_libfunc (sdivmod_optab, V16SImode, "__divmodv16si4");
4387 set_optab_libfunc (udivmod_optab, V16SImode, "__udivmodv16si4");
4388 #endif
4389 set_optab_libfunc (sdiv_optab, V32SImode, "__divv32si3");
4390 set_optab_libfunc (udiv_optab, V32SImode, "__udivv32si3");
4391 set_optab_libfunc (smod_optab, V32SImode, "__modv32si3");
4392 set_optab_libfunc (umod_optab, V32SImode, "__umodv32si3");
4393 #if 0
4394 set_optab_libfunc (sdivmod_optab, V32SImode, "__divmodv32si4");
4395 set_optab_libfunc (udivmod_optab, V32SImode, "__udivmodv32si4");
4396 #endif
4397 set_optab_libfunc (sdiv_optab, V64SImode, "__divv64si3");
4398 set_optab_libfunc (udiv_optab, V64SImode, "__udivv64si3");
4399 set_optab_libfunc (smod_optab, V64SImode, "__modv64si3");
4400 set_optab_libfunc (umod_optab, V64SImode, "__umodv64si3");
4401 #if 0
4402 set_optab_libfunc (sdivmod_optab, V64SImode, "__divmodv64si4");
4403 set_optab_libfunc (udivmod_optab, V64SImode, "__udivmodv64si4");
4404 #endif
4405
4406 set_optab_libfunc (sdiv_optab, V2DImode, "__divv2di3");
4407 set_optab_libfunc (udiv_optab, V2DImode, "__udivv2di3");
4408 set_optab_libfunc (smod_optab, V2DImode, "__modv2di3");
4409 set_optab_libfunc (umod_optab, V2DImode, "__umodv2di3");
4410 #if 0
4411 set_optab_libfunc (sdivmod_optab, V2DImode, "__divmodv2di4");
4412 set_optab_libfunc (udivmod_optab, V2DImode, "__udivmodv2di4");
4413 #endif
4414 set_optab_libfunc (sdiv_optab, V4DImode, "__divv4di3");
4415 set_optab_libfunc (udiv_optab, V4DImode, "__udivv4di3");
4416 set_optab_libfunc (smod_optab, V4DImode, "__modv4di3");
4417 set_optab_libfunc (umod_optab, V4DImode, "__umodv4di3");
4418 #if 0
4419 set_optab_libfunc (sdivmod_optab, V4DImode, "__divmodv4di4");
4420 set_optab_libfunc (udivmod_optab, V4DImode, "__udivmodv4di4");
4421 #endif
4422 set_optab_libfunc (sdiv_optab, V8DImode, "__divv8di3");
4423 set_optab_libfunc (udiv_optab, V8DImode, "__udivv8di3");
4424 set_optab_libfunc (smod_optab, V8DImode, "__modv8di3");
4425 set_optab_libfunc (umod_optab, V8DImode, "__umodv8di3");
4426 #if 0
4427 set_optab_libfunc (sdivmod_optab, V8DImode, "__divmodv8di4");
4428 set_optab_libfunc (udivmod_optab, V8DImode, "__udivmodv8di4");
4429 #endif
4430 set_optab_libfunc (sdiv_optab, V16DImode, "__divv16di3");
4431 set_optab_libfunc (udiv_optab, V16DImode, "__udivv16di3");
4432 set_optab_libfunc (smod_optab, V16DImode, "__modv16di3");
4433 set_optab_libfunc (umod_optab, V16DImode, "__umodv16di3");
4434 #if 0
4435 set_optab_libfunc (sdivmod_optab, V16DImode, "__divmodv16di4");
4436 set_optab_libfunc (udivmod_optab, V16DImode, "__udivmodv16di4");
4437 #endif
4438 set_optab_libfunc (sdiv_optab, V32DImode, "__divv32di3");
4439 set_optab_libfunc (udiv_optab, V32DImode, "__udivv32di3");
4440 set_optab_libfunc (smod_optab, V32DImode, "__modv32di3");
4441 set_optab_libfunc (umod_optab, V32DImode, "__umodv32di3");
4442 #if 0
4443 set_optab_libfunc (sdivmod_optab, V32DImode, "__divmodv32di4");
4444 set_optab_libfunc (udivmod_optab, V32DImode, "__udivmodv32di4");
4445 #endif
4446 set_optab_libfunc (sdiv_optab, V64DImode, "__divv64di3");
4447 set_optab_libfunc (udiv_optab, V64DImode, "__udivv64di3");
4448 set_optab_libfunc (smod_optab, V64DImode, "__modv64di3");
4449 set_optab_libfunc (umod_optab, V64DImode, "__umodv64di3");
4450 #if 0
4451 set_optab_libfunc (sdivmod_optab, V64DImode, "__divmodv64di4");
4452 set_optab_libfunc (udivmod_optab, V64DImode, "__udivmodv64di4");
4453 #endif
4454 }
4455
4456 /* Expand the CMP_SWAP GCN builtins. We have our own versions that do
4457 not require taking the address of any object, other than the memory
4458 cell being operated on.
4459
4460 Helper function for gcn_expand_builtin_1. */
4461
4462 static rtx
4463 gcn_expand_cmp_swap (tree exp, rtx target)
4464 {
4465 machine_mode mode = TYPE_MODE (TREE_TYPE (exp));
4466 addr_space_t as
4467 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (CALL_EXPR_ARG (exp, 0))));
4468 machine_mode as_mode = gcn_addr_space_address_mode (as);
4469
4470 if (!target)
4471 target = gen_reg_rtx (mode);
4472
4473 rtx addr = expand_expr (CALL_EXPR_ARG (exp, 0),
4474 NULL_RTX, as_mode, EXPAND_NORMAL);
4475 rtx cmp = expand_expr (CALL_EXPR_ARG (exp, 1),
4476 NULL_RTX, mode, EXPAND_NORMAL);
4477 rtx src = expand_expr (CALL_EXPR_ARG (exp, 2),
4478 NULL_RTX, mode, EXPAND_NORMAL);
4479 rtx pat;
4480
4481 rtx mem = gen_rtx_MEM (mode, force_reg (as_mode, addr));
4482 set_mem_addr_space (mem, as);
4483
4484 if (!REG_P (cmp))
4485 cmp = copy_to_mode_reg (mode, cmp);
4486 if (!REG_P (src))
4487 src = copy_to_mode_reg (mode, src);
4488
4489 if (mode == SImode)
4490 pat = gen_sync_compare_and_swapsi (target, mem, cmp, src);
4491 else
4492 pat = gen_sync_compare_and_swapdi (target, mem, cmp, src);
4493
4494 emit_insn (pat);
4495
4496 return target;
4497 }
4498
4499 /* Expand many different builtins.
4500
4501 Intended for use in gcn-builtins.def. */
4502
4503 static rtx
4504 gcn_expand_builtin_1 (tree exp, rtx target, rtx /*subtarget */ ,
4505 machine_mode /*mode */ , int ignore,
4506 struct gcn_builtin_description *)
4507 {
4508 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
4509 switch (DECL_MD_FUNCTION_CODE (fndecl))
4510 {
4511 case GCN_BUILTIN_FLAT_LOAD_INT32:
4512 {
4513 if (ignore)
4514 return target;
4515 /*rtx exec = */
4516 force_reg (DImode,
4517 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX, DImode,
4518 EXPAND_NORMAL));
4519 /*rtx ptr = */
4520 force_reg (V64DImode,
4521 expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, V64DImode,
4522 EXPAND_NORMAL));
4523 /*emit_insn (gen_vector_flat_loadv64si
4524 (target, gcn_gen_undef (V64SImode), ptr, exec)); */
4525 return target;
4526 }
4527 case GCN_BUILTIN_FLAT_LOAD_PTR_INT32:
4528 case GCN_BUILTIN_FLAT_LOAD_PTR_FLOAT:
4529 {
4530 if (ignore)
4531 return target;
4532 rtx exec = force_reg (DImode,
4533 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4534 DImode,
4535 EXPAND_NORMAL));
4536 rtx ptr = force_reg (DImode,
4537 expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
4538 V64DImode,
4539 EXPAND_NORMAL));
4540 rtx offsets = force_reg (V64SImode,
4541 expand_expr (CALL_EXPR_ARG (exp, 2),
4542 NULL_RTX, V64DImode,
4543 EXPAND_NORMAL));
4544 rtx addrs = gen_reg_rtx (V64DImode);
4545 rtx tmp = gen_reg_rtx (V64SImode);
4546 emit_insn (gen_ashlv64si3_exec (tmp, offsets,
4547 GEN_INT (2),
4548 gcn_gen_undef (V64SImode), exec));
4549 emit_insn (gen_addv64di3_zext_dup2_exec (addrs, tmp, ptr,
4550 gcn_gen_undef (V64DImode),
4551 exec));
4552 rtx mem = gen_rtx_MEM (GET_MODE (target), addrs);
4553 /*set_mem_addr_space (mem, ADDR_SPACE_FLAT); */
4554 /* FIXME: set attributes. */
4555 emit_insn (gen_movvNm (target, mem, NULL, exec));
4556 return target;
4557 }
4558 case GCN_BUILTIN_FLAT_STORE_PTR_INT32:
4559 case GCN_BUILTIN_FLAT_STORE_PTR_FLOAT:
4560 {
4561 rtx exec = force_reg (DImode,
4562 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4563 DImode,
4564 EXPAND_NORMAL));
4565 rtx ptr = force_reg (DImode,
4566 expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
4567 V64DImode,
4568 EXPAND_NORMAL));
4569 rtx offsets = force_reg (V64SImode,
4570 expand_expr (CALL_EXPR_ARG (exp, 2),
4571 NULL_RTX, V64DImode,
4572 EXPAND_NORMAL));
4573 machine_mode vmode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (exp,
4574 3)));
4575 rtx val = force_reg (vmode,
4576 expand_expr (CALL_EXPR_ARG (exp, 3), NULL_RTX,
4577 vmode,
4578 EXPAND_NORMAL));
4579 rtx addrs = gen_reg_rtx (V64DImode);
4580 rtx tmp = gen_reg_rtx (V64SImode);
4581 emit_insn (gen_ashlv64si3_exec (tmp, offsets,
4582 GEN_INT (2),
4583 gcn_gen_undef (V64SImode), exec));
4584 emit_insn (gen_addv64di3_zext_dup2_exec (addrs, tmp, ptr,
4585 gcn_gen_undef (V64DImode),
4586 exec));
4587 rtx mem = gen_rtx_MEM (vmode, addrs);
4588 /*set_mem_addr_space (mem, ADDR_SPACE_FLAT); */
4589 /* FIXME: set attributes. */
4590 emit_insn (gen_movvNm (mem, val, NULL, exec));
4591 return target;
4592 }
4593 case GCN_BUILTIN_SQRTVF:
4594 {
4595 if (ignore)
4596 return target;
4597 rtx arg = force_reg (V64SFmode,
4598 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4599 V64SFmode,
4600 EXPAND_NORMAL));
4601 emit_insn (gen_sqrtv64sf2 (target, arg));
4602 return target;
4603 }
4604 case GCN_BUILTIN_SQRTF:
4605 {
4606 if (ignore)
4607 return target;
4608 rtx arg = force_reg (SFmode,
4609 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4610 SFmode,
4611 EXPAND_NORMAL));
4612 emit_insn (gen_sqrtsf2 (target, arg));
4613 return target;
4614 }
4615 case GCN_BUILTIN_FABSVF:
4616 {
4617 if (ignore)
4618 return target;
4619 rtx arg = force_reg (V64SFmode,
4620 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4621 V64SFmode,
4622 EXPAND_NORMAL));
4623 emit_insn (gen_absv64sf2 (target, arg));
4624 return target;
4625 }
4626 case GCN_BUILTIN_FABSV:
4627 {
4628 if (ignore)
4629 return target;
4630 rtx arg = force_reg (V64DFmode,
4631 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4632 V64DFmode,
4633 EXPAND_NORMAL));
4634 emit_insn (gen_absv64df2 (target, arg));
4635 return target;
4636 }
4637 case GCN_BUILTIN_FLOORVF:
4638 {
4639 if (ignore)
4640 return target;
4641 rtx arg = force_reg (V64SFmode,
4642 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4643 V64SFmode,
4644 EXPAND_NORMAL));
4645 emit_insn (gen_floorv64sf2 (target, arg));
4646 return target;
4647 }
4648 case GCN_BUILTIN_FLOORV:
4649 {
4650 if (ignore)
4651 return target;
4652 rtx arg = force_reg (V64DFmode,
4653 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4654 V64DFmode,
4655 EXPAND_NORMAL));
4656 emit_insn (gen_floorv64df2 (target, arg));
4657 return target;
4658 }
4659 case GCN_BUILTIN_LDEXPVF:
4660 {
4661 if (ignore)
4662 return target;
4663 rtx arg1 = force_reg (V64SFmode,
4664 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4665 V64SFmode,
4666 EXPAND_NORMAL));
4667 rtx arg2 = force_reg (V64SImode,
4668 expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
4669 V64SImode,
4670 EXPAND_NORMAL));
4671 emit_insn (gen_ldexpv64sf3 (target, arg1, arg2));
4672 return target;
4673 }
4674 case GCN_BUILTIN_LDEXPV:
4675 {
4676 if (ignore)
4677 return target;
4678 rtx arg1 = force_reg (V64DFmode,
4679 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4680 V64DFmode,
4681 EXPAND_NORMAL));
4682 rtx arg2 = force_reg (V64SImode,
4683 expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX,
4684 V64SImode,
4685 EXPAND_NORMAL));
4686 emit_insn (gen_ldexpv64df3 (target, arg1, arg2));
4687 return target;
4688 }
4689 case GCN_BUILTIN_FREXPVF_EXP:
4690 {
4691 if (ignore)
4692 return target;
4693 rtx arg = force_reg (V64SFmode,
4694 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4695 V64SFmode,
4696 EXPAND_NORMAL));
4697 emit_insn (gen_frexpv64sf_exp2 (target, arg));
4698 return target;
4699 }
4700 case GCN_BUILTIN_FREXPVF_MANT:
4701 {
4702 if (ignore)
4703 return target;
4704 rtx arg = force_reg (V64SFmode,
4705 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4706 V64SFmode,
4707 EXPAND_NORMAL));
4708 emit_insn (gen_frexpv64sf_mant2 (target, arg));
4709 return target;
4710 }
4711 case GCN_BUILTIN_FREXPV_EXP:
4712 {
4713 if (ignore)
4714 return target;
4715 rtx arg = force_reg (V64DFmode,
4716 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4717 V64DFmode,
4718 EXPAND_NORMAL));
4719 emit_insn (gen_frexpv64df_exp2 (target, arg));
4720 return target;
4721 }
4722 case GCN_BUILTIN_FREXPV_MANT:
4723 {
4724 if (ignore)
4725 return target;
4726 rtx arg = force_reg (V64DFmode,
4727 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4728 V64DFmode,
4729 EXPAND_NORMAL));
4730 emit_insn (gen_frexpv64df_mant2 (target, arg));
4731 return target;
4732 }
4733 case GCN_BUILTIN_OMP_DIM_SIZE:
4734 {
4735 if (ignore)
4736 return target;
4737 emit_insn (gen_oacc_dim_size (target,
4738 expand_expr (CALL_EXPR_ARG (exp, 0),
4739 NULL_RTX, SImode,
4740 EXPAND_NORMAL)));
4741 return target;
4742 }
4743 case GCN_BUILTIN_OMP_DIM_POS:
4744 {
4745 if (ignore)
4746 return target;
4747 emit_insn (gen_oacc_dim_pos (target,
4748 expand_expr (CALL_EXPR_ARG (exp, 0),
4749 NULL_RTX, SImode,
4750 EXPAND_NORMAL)));
4751 return target;
4752 }
4753 case GCN_BUILTIN_CMP_SWAP:
4754 case GCN_BUILTIN_CMP_SWAPLL:
4755 return gcn_expand_cmp_swap (exp, target);
4756
4757 case GCN_BUILTIN_ACC_SINGLE_START:
4758 {
4759 if (ignore)
4760 return target;
4761
4762 rtx wavefront = gcn_oacc_dim_pos (1);
4763 rtx cond = gen_rtx_EQ (VOIDmode, wavefront, const0_rtx);
4764 rtx cc = (target && REG_P (target)) ? target : gen_reg_rtx (BImode);
4765 emit_insn (gen_cstoresi4 (cc, cond, wavefront, const0_rtx));
4766 return cc;
4767 }
4768
4769 case GCN_BUILTIN_ACC_SINGLE_COPY_START:
4770 {
4771 rtx blk = force_reg (SImode,
4772 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX,
4773 SImode, EXPAND_NORMAL));
4774 rtx wavefront = gcn_oacc_dim_pos (1);
4775 rtx cond = gen_rtx_NE (VOIDmode, wavefront, const0_rtx);
4776 rtx not_zero = gen_label_rtx ();
4777 emit_insn (gen_cbranchsi4 (cond, wavefront, const0_rtx, not_zero));
4778 emit_move_insn (blk, const0_rtx);
4779 emit_label (not_zero);
4780 return blk;
4781 }
4782
4783 case GCN_BUILTIN_ACC_SINGLE_COPY_END:
4784 return target;
4785
4786 case GCN_BUILTIN_ACC_BARRIER:
4787 emit_insn (gen_gcn_wavefront_barrier ());
4788 return target;
4789
4790 case GCN_BUILTIN_GET_STACK_LIMIT:
4791 {
4792 /* stackbase = (stack_segment_decr & 0x0000ffffffffffff)
4793 + stack_wave_offset);
4794 seg_size = dispatch_ptr->private_segment_size;
4795 stacklimit = stackbase + seg_size*64;
4796 with segsize = *(uint32_t *) ((char *) dispatch_ptr
4797 + 6*sizeof(int16_t) + 3*sizeof(int32_t));
4798 cf. struct hsa_kernel_dispatch_packet_s in the HSA doc. */
4799 rtx ptr;
4800 if (cfun->machine->args.reg[DISPATCH_PTR_ARG] >= 0
4801 && cfun->machine->args.reg[KERNARG_SEGMENT_PTR_ARG] >= 0)
4802 {
4803 rtx num_waves_mem = gcn_oacc_dim_size (1);
4804 rtx num_waves = gen_reg_rtx (SImode);
4805 set_mem_addr_space (num_waves_mem, ADDR_SPACE_SCALAR_FLAT);
4806 emit_move_insn (num_waves, num_waves_mem);
4807
4808 rtx workgroup_num = gcn_oacc_dim_pos (0);
4809 rtx wave_num = gen_reg_rtx (SImode);
4810 emit_move_insn(wave_num, gcn_oacc_dim_pos (1));
4811
4812 rtx thread_id = gen_reg_rtx (SImode);
4813 emit_insn (gen_mulsi3 (thread_id, num_waves, workgroup_num));
4814 emit_insn (gen_addsi3_scc (thread_id, thread_id, wave_num));
4815
4816 rtx kernarg_reg = gen_rtx_REG (DImode, cfun->machine->args.reg
4817 [KERNARG_SEGMENT_PTR_ARG]);
4818 rtx stack_size_mem = gen_rtx_MEM (SImode,
4819 gen_rtx_PLUS (DImode,
4820 kernarg_reg,
4821 GEN_INT (52)));
4822 set_mem_addr_space (stack_size_mem, ADDR_SPACE_SCALAR_FLAT);
4823 rtx stack_size = gen_reg_rtx (SImode);
4824 emit_move_insn (stack_size, stack_size_mem);
4825
4826 rtx wave_offset = gen_reg_rtx (SImode);
4827 emit_insn (gen_mulsi3 (wave_offset, stack_size, thread_id));
4828
4829 rtx stack_limit_offset = gen_reg_rtx (SImode);
4830 emit_insn (gen_addsi3 (stack_limit_offset, wave_offset,
4831 stack_size));
4832
4833 rtx stack_limit_offset_di = gen_reg_rtx (DImode);
4834 emit_move_insn (gen_rtx_SUBREG (SImode, stack_limit_offset_di, 4),
4835 const0_rtx);
4836 emit_move_insn (gen_rtx_SUBREG (SImode, stack_limit_offset_di, 0),
4837 stack_limit_offset);
4838
4839 rtx stack_addr_mem = gen_rtx_MEM (DImode,
4840 gen_rtx_PLUS (DImode,
4841 kernarg_reg,
4842 GEN_INT (40)));
4843 set_mem_addr_space (stack_addr_mem, ADDR_SPACE_SCALAR_FLAT);
4844 rtx stack_addr = gen_reg_rtx (DImode);
4845 emit_move_insn (stack_addr, stack_addr_mem);
4846
4847 ptr = gen_rtx_PLUS (DImode, stack_addr, stack_limit_offset_di);
4848 }
4849 else
4850 {
4851 ptr = gen_reg_rtx (DImode);
4852 emit_move_insn (ptr, const0_rtx);
4853 }
4854 return ptr;
4855 }
4856 case GCN_BUILTIN_KERNARG_PTR:
4857 {
4858 rtx ptr;
4859 if (cfun->machine->args.reg[KERNARG_SEGMENT_PTR_ARG] >= 0)
4860 ptr = gen_rtx_REG (DImode,
4861 cfun->machine->args.reg[KERNARG_SEGMENT_PTR_ARG]);
4862 else
4863 {
4864 ptr = gen_reg_rtx (DImode);
4865 emit_move_insn (ptr, const0_rtx);
4866 }
4867 return ptr;
4868 }
4869 case GCN_BUILTIN_DISPATCH_PTR:
4870 {
4871 rtx ptr;
4872 if (cfun->machine->args.reg[DISPATCH_PTR_ARG] >= 0)
4873 ptr = gen_rtx_REG (DImode,
4874 cfun->machine->args.reg[DISPATCH_PTR_ARG]);
4875 else
4876 {
4877 ptr = gen_reg_rtx (DImode);
4878 emit_move_insn (ptr, const0_rtx);
4879 }
4880 return ptr;
4881 }
4882 case GCN_BUILTIN_FIRST_CALL_THIS_THREAD_P:
4883 {
4884 /* Stash a marker in the unused upper 16 bits of QUEUE_PTR_ARG to
4885 indicate whether it was the first call. */
4886 rtx result = gen_reg_rtx (BImode);
4887 emit_move_insn (result, const0_rtx);
4888 if (cfun->machine->args.reg[QUEUE_PTR_ARG] >= 0)
4889 {
4890 rtx not_first = gen_label_rtx ();
4891 rtx reg = gen_rtx_REG (DImode,
4892 cfun->machine->args.reg[QUEUE_PTR_ARG]);
4893 reg = gcn_operand_part (DImode, reg, 1);
4894 rtx cmp = force_reg (SImode,
4895 gen_rtx_LSHIFTRT (SImode, reg, GEN_INT (16)));
4896 emit_insn (gen_cstoresi4 (result, gen_rtx_NE (BImode, cmp,
4897 GEN_INT(12345)),
4898 cmp, GEN_INT(12345)));
4899 emit_jump_insn (gen_cjump (not_first, gen_rtx_EQ (BImode, result,
4900 const0_rtx),
4901 result));
4902 emit_move_insn (reg,
4903 force_reg (SImode,
4904 gen_rtx_IOR (SImode,
4905 gen_rtx_AND (SImode, reg, GEN_INT (0x0000ffff)),
4906 GEN_INT (12345L << 16))));
4907 emit_insn (gen_rtx_USE (VOIDmode, reg));
4908 emit_label (not_first);
4909 }
4910 return result;
4911 }
4912 default:
4913 gcc_unreachable ();
4914 }
4915 }
4916
4917 /* Expansion of simple arithmetic and bit binary operation builtins.
4918
4919 Intended for use with gcn_builtins table. */
4920
4921 static rtx
4922 gcn_expand_builtin_binop (tree exp, rtx target, rtx /*subtarget */ ,
4923 machine_mode /*mode */ , int ignore,
4924 struct gcn_builtin_description *d)
4925 {
4926 int icode = d->icode;
4927 if (ignore)
4928 return target;
4929
4930 rtx exec = force_reg (DImode,
4931 expand_expr (CALL_EXPR_ARG (exp, 0), NULL_RTX, DImode,
4932 EXPAND_NORMAL));
4933
4934 machine_mode m1 = insn_data[icode].operand[1].mode;
4935 rtx arg1 = expand_expr (CALL_EXPR_ARG (exp, 1), NULL_RTX, m1,
4936 EXPAND_NORMAL);
4937 if (!insn_data[icode].operand[1].predicate (arg1, m1))
4938 arg1 = force_reg (m1, arg1);
4939
4940 machine_mode m2 = insn_data[icode].operand[2].mode;
4941 rtx arg2 = expand_expr (CALL_EXPR_ARG (exp, 2), NULL_RTX, m2,
4942 EXPAND_NORMAL);
4943 if (!insn_data[icode].operand[2].predicate (arg2, m2))
4944 arg2 = force_reg (m2, arg2);
4945
4946 rtx arg_prev;
4947 if (call_expr_nargs (exp) == 4)
4948 {
4949 machine_mode m_prev = insn_data[icode].operand[4].mode;
4950 arg_prev = force_reg (m_prev,
4951 expand_expr (CALL_EXPR_ARG (exp, 3), NULL_RTX,
4952 m_prev, EXPAND_NORMAL));
4953 }
4954 else
4955 arg_prev = gcn_gen_undef (GET_MODE (target));
4956
4957 rtx pat = GEN_FCN (icode) (target, arg1, arg2, exec, arg_prev);
4958 emit_insn (pat);
4959 return target;
4960 }
4961
4962 /* Implement TARGET_EXPAND_BUILTIN.
4963
4964 Expand an expression EXP that calls a built-in function, with result going
4965 to TARGET if that's convenient (and in mode MODE if that's convenient).
4966 SUBTARGET may be used as the target for computing one of EXP's operands.
4967 IGNORE is nonzero if the value is to be ignored. */
4968
4969 rtx
4970 gcn_expand_builtin (tree exp, rtx target, rtx subtarget, machine_mode mode,
4971 int ignore)
4972 {
4973 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
4974 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
4975 struct gcn_builtin_description *d;
4976
4977 gcc_assert (fcode < GCN_BUILTIN_MAX);
4978 d = &gcn_builtins[fcode];
4979
4980 if (d->type == B_UNIMPLEMENTED)
4981 sorry ("Builtin not implemented");
4982
4983 return d->expander (exp, target, subtarget, mode, ignore, d);
4984 }
4985
4986 /* }}} */
4987 /* {{{ Vectorization. */
4988
4989 /* Implement TARGET_VECTORIZE_GET_MASK_MODE.
4990
4991 A vector mask is a value that holds one boolean result for every element in
4992 a vector. */
4993
4994 opt_machine_mode
4995 gcn_vectorize_get_mask_mode (machine_mode)
4996 {
4997 /* GCN uses a DImode bit-mask. */
4998 return DImode;
4999 }
5000
5001 /* Return an RTX that references a vector with the i-th lane containing
5002 PERM[i]*4.
5003
5004 Helper function for gcn_vectorize_vec_perm_const. */
5005
5006 static rtx
5007 gcn_make_vec_perm_address (unsigned int *perm, int nelt)
5008 {
5009 machine_mode mode = VnMODE (nelt, SImode);
5010 rtx x = gen_reg_rtx (mode);
5011 emit_move_insn (x, gcn_vec_constant (mode, 0));
5012
5013 /* Permutation addresses use byte addressing. With each vector lane being
5014 4 bytes wide, and with 64 lanes in total, only bits 2..7 are significant,
5015 so only set those.
5016
5017 The permutation given to the vec_perm* patterns range from 0 to 2N-1 to
5018 select between lanes in two vectors, but as the DS_BPERMUTE* instructions
5019 only take one source vector, the most-significant bit can be ignored
5020 here. Instead, we can use EXEC masking to select the relevant part of
5021 each source vector after they are permuted separately. */
5022 uint64_t bit_mask = 1 << 2;
5023 for (int i = 2; i < 8; i++, bit_mask <<= 1)
5024 {
5025 uint64_t exec_mask = 0;
5026 uint64_t lane_mask = 1;
5027 for (int j = 0; j < nelt; j++, lane_mask <<= 1)
5028 if (((perm[j] % nelt) * 4) & bit_mask)
5029 exec_mask |= lane_mask;
5030
5031 if (exec_mask)
5032 emit_insn (gen_addvNsi3 (x, x, gcn_vec_constant (mode, bit_mask),
5033 x, get_exec (exec_mask)));
5034 }
5035
5036 return x;
5037 }
5038
5039 /* Implement TARGET_VECTORIZE_VEC_PERM_CONST.
5040
5041 Return true if permutation with SEL is possible.
5042
5043 If DST/SRC0/SRC1 are non-null, emit the instructions to perform the
5044 permutations. */
5045
5046 static bool
5047 gcn_vectorize_vec_perm_const (machine_mode vmode, machine_mode op_mode,
5048 rtx dst, rtx src0, rtx src1,
5049 const vec_perm_indices & sel)
5050 {
5051 if (vmode != op_mode
5052 || !VECTOR_MODE_P (vmode)
5053 || GET_MODE_INNER (vmode) == TImode)
5054 return false;
5055
5056 unsigned int nelt = GET_MODE_NUNITS (vmode);
5057
5058 gcc_assert (VECTOR_MODE_P (vmode));
5059 gcc_assert (nelt <= 64);
5060 gcc_assert (sel.length () == nelt);
5061
5062 unsigned int perm[64];
5063 for (unsigned int i = 0; i < nelt; ++i)
5064 perm[i] = sel[i] & (2 * nelt - 1);
5065 for (unsigned int i = nelt; i < 64; ++i)
5066 perm[i] = 0;
5067
5068 /* RDNA devices can only do permutations within each group of 32-lanes.
5069 Reject permutations that cross the boundary. */
5070 if (TARGET_WAVE64_COMPAT)
5071 for (unsigned int i = 0; i < nelt; i++)
5072 if (i < 32 ? (perm[i] % nelt) > 31 : (perm[i] % nelt) < 32)
5073 return false;
5074
5075 /* All vector permutations are possible on other architectures,
5076 with varying degrees of efficiency depending on the permutation. */
5077 if (!dst)
5078 return true;
5079
5080 src0 = force_reg (vmode, src0);
5081 src1 = force_reg (vmode, src1);
5082
5083 /* Make life a bit easier by swapping operands if necessary so that
5084 the first element always comes from src0. */
5085 if (perm[0] >= nelt)
5086 {
5087 std::swap (src0, src1);
5088
5089 for (unsigned int i = 0; i < nelt; ++i)
5090 if (perm[i] < nelt)
5091 perm[i] += nelt;
5092 else
5093 perm[i] -= nelt;
5094 }
5095
5096 /* TODO: There are more efficient ways to implement certain permutations
5097 using ds_swizzle_b32 and/or DPP. Test for and expand them here, before
5098 this more inefficient generic approach is used. */
5099
5100 int64_t src1_lanes = 0;
5101 int64_t lane_bit = 1;
5102
5103 for (unsigned int i = 0; i < nelt; ++i, lane_bit <<= 1)
5104 {
5105 /* Set the bits for lanes from src1. */
5106 if (perm[i] >= nelt)
5107 src1_lanes |= lane_bit;
5108 }
5109
5110 rtx addr = gcn_make_vec_perm_address (perm, nelt);
5111
5112 /* Load elements from src0 to dst. */
5113 gcc_assert ((~src1_lanes) & (0xffffffffffffffffUL > (64-nelt)));
5114 emit_insn (gen_ds_bpermutevNm (dst, addr, src0, get_exec (vmode)));
5115
5116 /* Load elements from src1 to dst. */
5117 if (src1_lanes)
5118 {
5119 /* Masking a lane masks both the destination and source lanes for
5120 DS_BPERMUTE, so we need to have all lanes enabled for the permute,
5121 then add an extra masked move to merge the results of permuting
5122 the two source vectors together.
5123 */
5124 rtx tmp = gen_reg_rtx (vmode);
5125 emit_insn (gen_ds_bpermutevNm (tmp, addr, src1, get_exec (vmode)));
5126 emit_insn (gen_movvNm (dst, tmp, dst, get_exec (src1_lanes)));
5127 }
5128
5129 return true;
5130 }
5131
5132 /* Implements TARGET_VECTOR_MODE_SUPPORTED_P.
5133
5134 Return nonzero if vector MODE is supported with at least move
5135 instructions. */
5136
5137 static bool
5138 gcn_vector_mode_supported_p (machine_mode mode)
5139 {
5140 return (mode == V64QImode || mode == V64HImode
5141 || mode == V64SImode || mode == V64DImode
5142 || mode == V64SFmode || mode == V64DFmode
5143 || mode == V32QImode || mode == V32HImode
5144 || mode == V32SImode || mode == V32DImode
5145 || mode == V32SFmode || mode == V32DFmode
5146 || mode == V16QImode || mode == V16HImode
5147 || mode == V16SImode || mode == V16DImode
5148 || mode == V16SFmode || mode == V16DFmode
5149 || mode == V8QImode || mode == V8HImode
5150 || mode == V8SImode || mode == V8DImode
5151 || mode == V8SFmode || mode == V8DFmode
5152 || mode == V4QImode || mode == V4HImode
5153 || mode == V4SImode || mode == V4DImode
5154 || mode == V4SFmode || mode == V4DFmode
5155 || mode == V2QImode || mode == V2HImode
5156 || mode == V2SImode || mode == V2DImode
5157 || mode == V2SFmode || mode == V2DFmode
5158 /* TImode vectors are allowed to exist for divmod, but there
5159 are almost no instructions defined for them, and the
5160 autovectorizer does not use them. */
5161 || mode == V64TImode || mode == V32TImode
5162 || mode == V16TImode || mode == V8TImode
5163 || mode == V4TImode || mode == V2TImode);
5164 }
5165
5166 /* Implement TARGET_VECTORIZE_PREFERRED_SIMD_MODE.
5167
5168 Enables autovectorization for all supported modes. */
5169
5170 static machine_mode
5171 gcn_vectorize_preferred_simd_mode (scalar_mode mode)
5172 {
5173 bool v32;
5174 if (gcn_preferred_vectorization_factor == 32)
5175 v32 = true;
5176 else if (gcn_preferred_vectorization_factor == 64)
5177 v32 = false;
5178 else if (gcn_preferred_vectorization_factor != -1)
5179 gcc_unreachable ();
5180 else if (TARGET_WAVE64_COMPAT)
5181 /* RDNA devices have 32-lane vectors with limited support for 64-bit vectors
5182 (in particular, permute operations are only available for cases that don't
5183 span the 32-lane boundary).
5184
5185 From the RDNA3 manual: "Hardware may choose to skip either half if the
5186 EXEC mask for that half is all zeros...". This means that preferring
5187 32-lanes is a good stop-gap until we have proper wave32 support. */
5188 v32 = true;
5189 else
5190 v32 = false;
5191
5192 if (v32)
5193 switch (mode)
5194 {
5195 case E_QImode:
5196 return V32QImode;
5197 case E_HImode:
5198 return V32HImode;
5199 case E_SImode:
5200 return V32SImode;
5201 case E_DImode:
5202 return V32DImode;
5203 case E_SFmode:
5204 return V32SFmode;
5205 case E_DFmode:
5206 return V32DFmode;
5207 default:
5208 return word_mode;
5209 }
5210
5211 switch (mode)
5212 {
5213 case E_QImode:
5214 return V64QImode;
5215 case E_HImode:
5216 return V64HImode;
5217 case E_SImode:
5218 return V64SImode;
5219 case E_DImode:
5220 return V64DImode;
5221 case E_SFmode:
5222 return V64SFmode;
5223 case E_DFmode:
5224 return V64DFmode;
5225 default:
5226 return word_mode;
5227 }
5228 }
5229
5230 /* Implement TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_MODES.
5231
5232 Try all the vector modes. */
5233
5234 unsigned int gcn_autovectorize_vector_modes (vector_modes *modes,
5235 bool ARG_UNUSED (all))
5236 {
5237 modes->safe_push (V64QImode);
5238 modes->safe_push (V64HImode);
5239 modes->safe_push (V64SImode);
5240 modes->safe_push (V64SFmode);
5241 modes->safe_push (V64DImode);
5242 modes->safe_push (V64DFmode);
5243
5244 modes->safe_push (V32QImode);
5245 modes->safe_push (V32HImode);
5246 modes->safe_push (V32SImode);
5247 modes->safe_push (V32SFmode);
5248 modes->safe_push (V32DImode);
5249 modes->safe_push (V32DFmode);
5250
5251 modes->safe_push (V16QImode);
5252 modes->safe_push (V16HImode);
5253 modes->safe_push (V16SImode);
5254 modes->safe_push (V16SFmode);
5255 modes->safe_push (V16DImode);
5256 modes->safe_push (V16DFmode);
5257
5258 modes->safe_push (V8QImode);
5259 modes->safe_push (V8HImode);
5260 modes->safe_push (V8SImode);
5261 modes->safe_push (V8SFmode);
5262 modes->safe_push (V8DImode);
5263 modes->safe_push (V8DFmode);
5264
5265 modes->safe_push (V4QImode);
5266 modes->safe_push (V4HImode);
5267 modes->safe_push (V4SImode);
5268 modes->safe_push (V4SFmode);
5269 modes->safe_push (V4DImode);
5270 modes->safe_push (V4DFmode);
5271
5272 modes->safe_push (V2QImode);
5273 modes->safe_push (V2HImode);
5274 modes->safe_push (V2SImode);
5275 modes->safe_push (V2SFmode);
5276 modes->safe_push (V2DImode);
5277 modes->safe_push (V2DFmode);
5278
5279 /* We shouldn't need VECT_COMPARE_COSTS as they should all cost the same. */
5280 return 0;
5281 }
5282
5283 /* Implement TARGET_VECTORIZE_RELATED_MODE.
5284
5285 All GCN vectors are 64-lane, so this is simpler than other architectures.
5286 In particular, we do *not* want to match vector bit-size. */
5287
5288 static opt_machine_mode
5289 gcn_related_vector_mode (machine_mode vector_mode,
5290 scalar_mode element_mode, poly_uint64 nunits)
5291 {
5292 int n = nunits.to_constant ();
5293
5294 if (n == 0)
5295 n = GET_MODE_NUNITS (vector_mode);
5296
5297 return VnMODE (n, element_mode);
5298 }
5299
5300 /* Implement TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT.
5301
5302 Returns the preferred alignment in bits for accesses to vectors of type type
5303 in vectorized code. This might be less than or greater than the ABI-defined
5304 value returned by TARGET_VECTOR_ALIGNMENT. It can be equal to the alignment
5305 of a single element, in which case the vectorizer will not try to optimize
5306 for alignment. */
5307
5308 static poly_uint64
5309 gcn_preferred_vector_alignment (const_tree type)
5310 {
5311 return TYPE_ALIGN (TREE_TYPE (type));
5312 }
5313
5314 /* Implement TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT.
5315
5316 Return true if the target supports misaligned vector store/load of a
5317 specific factor denoted in the misalignment parameter. */
5318
5319 static bool
5320 gcn_vectorize_support_vector_misalignment (machine_mode ARG_UNUSED (mode),
5321 const_tree type, int misalignment,
5322 bool is_packed)
5323 {
5324 if (is_packed)
5325 return false;
5326
5327 /* If the misalignment is unknown, we should be able to handle the access
5328 so long as it is not to a member of a packed data structure. */
5329 if (misalignment == -1)
5330 return true;
5331
5332 /* Return true if the misalignment is a multiple of the natural alignment
5333 of the vector's element type. This is probably always going to be
5334 true in practice, since we've already established that this isn't a
5335 packed access. */
5336 return misalignment % TYPE_ALIGN_UNIT (type) == 0;
5337 }
5338
5339 /* Implement TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE.
5340
5341 Return true if vector alignment is reachable (by peeling N iterations) for
5342 the given scalar type TYPE. */
5343
5344 static bool
5345 gcn_vector_alignment_reachable (const_tree ARG_UNUSED (type), bool is_packed)
5346 {
5347 /* Vectors which aren't in packed structures will not be less aligned than
5348 the natural alignment of their element type, so this is safe. */
5349 return !is_packed;
5350 }
5351
5352 /* Generate DPP pairwise swap instruction.
5353 This instruction swaps the values in each even lane with the value in the
5354 next one:
5355 a, b, c, d -> b, a, d, c.
5356 The opcode is given by INSN. */
5357
5358 char *
5359 gcn_expand_dpp_swap_pairs_insn (machine_mode mode, const char *insn,
5360 int ARG_UNUSED (unspec))
5361 {
5362 static char buf[128];
5363 const char *dpp;
5364
5365 /* Add the DPP modifiers. */
5366 dpp = "quad_perm:[1,0,3,2]";
5367
5368 if (vgpr_2reg_mode_p (mode))
5369 sprintf (buf, "%s\t%%L0, %%L1 %s\n\t%s\t%%H0, %%H1 %s",
5370 insn, dpp, insn, dpp);
5371 else
5372 sprintf (buf, "%s\t%%0, %%1 %s", insn, dpp);
5373
5374 return buf;
5375 }
5376
5377 /* Generate DPP distribute even instruction.
5378 This instruction copies the value in each even lane to the next one:
5379 a, b, c, d -> a, a, c, c.
5380 The opcode is given by INSN. */
5381
5382 char *
5383 gcn_expand_dpp_distribute_even_insn (machine_mode mode, const char *insn,
5384 int ARG_UNUSED (unspec))
5385 {
5386 static char buf[128];
5387 const char *dpp;
5388
5389 /* Add the DPP modifiers. */
5390 dpp = "quad_perm:[0,0,2,2]";
5391
5392 if (vgpr_2reg_mode_p (mode))
5393 sprintf (buf, "%s\t%%L0, %%L1 %s\n\t%s\t%%H0, %%H1 %s",
5394 insn, dpp, insn, dpp);
5395 else
5396 sprintf (buf, "%s\t%%0, %%1 %s", insn, dpp);
5397
5398 return buf;
5399 }
5400
5401 /* Generate DPP distribute odd instruction.
5402 This isntruction copies the value in each odd lane to the previous one:
5403 a, b, c, d -> b, b, d, d.
5404 The opcode is given by INSN. */
5405
5406 char *
5407 gcn_expand_dpp_distribute_odd_insn (machine_mode mode, const char *insn,
5408 int ARG_UNUSED (unspec))
5409 {
5410 static char buf[128];
5411 const char *dpp;
5412
5413 /* Add the DPP modifiers. */
5414 dpp = "quad_perm:[1,1,3,3]";
5415
5416 if (vgpr_2reg_mode_p (mode))
5417 sprintf (buf, "%s\t%%L0, %%L1 %s\n\t%s\t%%H0, %%H1 %s",
5418 insn, dpp, insn, dpp);
5419 else
5420 sprintf (buf, "%s\t%%0, %%1 %s", insn, dpp);
5421
5422 return buf;
5423 }
5424
5425 /* Generate DPP instructions used for vector reductions.
5426
5427 The opcode is given by INSN.
5428 The first operand of the operation is shifted right by SHIFT vector lanes.
5429 SHIFT must be a power of 2. If SHIFT is 16, the 15th lane of each row is
5430 broadcast the next row (thereby acting like a shift of 16 for the end of
5431 each row). If SHIFT is 32, lane 31 is broadcast to all the
5432 following lanes (thereby acting like a shift of 32 for lane 63). */
5433
5434 char *
5435 gcn_expand_dpp_shr_insn (machine_mode mode, const char *insn,
5436 int unspec, int shift)
5437 {
5438 gcc_checking_assert (TARGET_DPP_FULL);
5439
5440 static char buf[128];
5441 const char *dpp;
5442 const char *vcc_in = "";
5443 const char *vcc_out = "";
5444
5445 /* Add the vcc operand if needed. */
5446 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
5447 {
5448 if (unspec == UNSPEC_PLUS_CARRY_IN_DPP_SHR)
5449 vcc_in = ", vcc";
5450
5451 if (unspec == UNSPEC_PLUS_CARRY_DPP_SHR
5452 || unspec == UNSPEC_PLUS_CARRY_IN_DPP_SHR)
5453 vcc_out = ", vcc";
5454 }
5455
5456 /* Add the DPP modifiers. */
5457 switch (shift)
5458 {
5459 case 1:
5460 dpp = "row_shr:1 bound_ctrl:0";
5461 break;
5462 case 2:
5463 dpp = "row_shr:2 bound_ctrl:0";
5464 break;
5465 case 4:
5466 dpp = "row_shr:4 bank_mask:0xe";
5467 break;
5468 case 8:
5469 dpp = "row_shr:8 bank_mask:0xc";
5470 break;
5471 case 16:
5472 dpp = "row_bcast:15 row_mask:0xa";
5473 break;
5474 case 32:
5475 dpp = "row_bcast:31 row_mask:0xc";
5476 break;
5477 default:
5478 gcc_unreachable ();
5479 }
5480
5481 if (unspec == UNSPEC_MOV_DPP_SHR && vgpr_2reg_mode_p (mode))
5482 sprintf (buf, "%s\t%%L0, %%L1 %s\n\t%s\t%%H0, %%H1 %s",
5483 insn, dpp, insn, dpp);
5484 else if (unspec == UNSPEC_MOV_DPP_SHR)
5485 sprintf (buf, "%s\t%%0, %%1 %s", insn, dpp);
5486 else
5487 sprintf (buf, "%s\t%%0%s, %%1, %%2%s %s", insn, vcc_out, vcc_in, dpp);
5488
5489 return buf;
5490 }
5491
5492 /* Generate vector reductions in terms of DPP instructions.
5493
5494 The vector register SRC of mode MODE is reduced using the operation given
5495 by UNSPEC, and the scalar result is returned in lane 63 of a vector
5496 register (or lane 31, 15, 7, 3, 1 for partial vectors). */
5497
5498 rtx
5499 gcn_expand_reduc_scalar (machine_mode mode, rtx src, int unspec)
5500 {
5501 gcc_checking_assert (TARGET_DPP_FULL);
5502
5503 machine_mode orig_mode = mode;
5504 machine_mode scalar_mode = GET_MODE_INNER (mode);
5505 int vf = GET_MODE_NUNITS (mode);
5506 bool use_moves = (((unspec == UNSPEC_SMIN_DPP_SHR
5507 || unspec == UNSPEC_SMAX_DPP_SHR
5508 || unspec == UNSPEC_UMIN_DPP_SHR
5509 || unspec == UNSPEC_UMAX_DPP_SHR)
5510 && (scalar_mode == DImode
5511 || scalar_mode == DFmode))
5512 || (unspec == UNSPEC_PLUS_DPP_SHR
5513 && scalar_mode == DFmode));
5514 rtx_code code = (unspec == UNSPEC_SMIN_DPP_SHR ? SMIN
5515 : unspec == UNSPEC_SMAX_DPP_SHR ? SMAX
5516 : unspec == UNSPEC_UMIN_DPP_SHR ? UMIN
5517 : unspec == UNSPEC_UMAX_DPP_SHR ? UMAX
5518 : unspec == UNSPEC_PLUS_DPP_SHR ? PLUS
5519 : UNKNOWN);
5520 bool use_extends = ((unspec == UNSPEC_SMIN_DPP_SHR
5521 || unspec == UNSPEC_SMAX_DPP_SHR
5522 || unspec == UNSPEC_UMIN_DPP_SHR
5523 || unspec == UNSPEC_UMAX_DPP_SHR)
5524 && (scalar_mode == QImode
5525 || scalar_mode == HImode));
5526 bool unsignedp = (unspec == UNSPEC_UMIN_DPP_SHR
5527 || unspec == UNSPEC_UMAX_DPP_SHR);
5528 bool use_plus_carry = unspec == UNSPEC_PLUS_DPP_SHR
5529 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT
5530 && scalar_mode == DImode;
5531
5532 if (use_plus_carry)
5533 unspec = UNSPEC_PLUS_CARRY_DPP_SHR;
5534
5535 if (use_extends)
5536 {
5537 mode = VnMODE (vf, SImode);
5538 rtx tmp = gen_reg_rtx (mode);
5539 convert_move (tmp, src, unsignedp);
5540 src = tmp;
5541 }
5542
5543 /* Perform reduction by first performing the reduction operation on every
5544 pair of lanes, then on every pair of results from the previous
5545 iteration (thereby effectively reducing every 4 lanes) and so on until
5546 all lanes are reduced. */
5547 rtx in, out = force_reg (mode, src);
5548 int iterations = exact_log2 (vf);
5549 for (int i = 0, shift = 1; i < iterations; i++, shift <<= 1)
5550 {
5551 rtx shift_val = gen_rtx_CONST_INT (VOIDmode, shift);
5552 in = out;
5553 out = gen_reg_rtx (mode);
5554
5555 if (use_moves)
5556 {
5557 rtx tmp = gen_reg_rtx (mode);
5558 emit_insn (gen_dpp_move (mode, tmp, in, shift_val));
5559 rtx insn = gen_rtx_SET (out, gen_rtx_fmt_ee (code, mode, tmp, in));
5560 if (scalar_mode == DImode)
5561 {
5562 rtx clobber = gen_rtx_CLOBBER (VOIDmode,
5563 gen_rtx_REG (DImode, VCC_REG));
5564 insn = gen_rtx_PARALLEL (VOIDmode,
5565 gen_rtvec (2, insn, clobber));
5566 }
5567 emit_insn (insn);
5568 }
5569 else
5570 {
5571 rtx insn = gen_rtx_SET (out,
5572 gen_rtx_UNSPEC (mode,
5573 gen_rtvec (3, in, in,
5574 shift_val),
5575 unspec));
5576
5577 /* Add clobber for instructions that set the carry flags. */
5578 if (use_plus_carry)
5579 {
5580 rtx clobber = gen_rtx_CLOBBER (VOIDmode,
5581 gen_rtx_REG (DImode, VCC_REG));
5582 insn = gen_rtx_PARALLEL (VOIDmode,
5583 gen_rtvec (2, insn, clobber));
5584 }
5585
5586 emit_insn (insn);
5587 }
5588 }
5589
5590 if (use_extends)
5591 {
5592 rtx tmp = gen_reg_rtx (orig_mode);
5593 convert_move (tmp, out, unsignedp);
5594 out = tmp;
5595 }
5596
5597 return out;
5598 }
5599
5600 /* Implement TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST. */
5601
5602 int
5603 gcn_vectorization_cost (enum vect_cost_for_stmt ARG_UNUSED (type_of_cost),
5604 tree ARG_UNUSED (vectype), int ARG_UNUSED (misalign))
5605 {
5606 /* Always vectorize. */
5607 return 1;
5608 }
5609
5610 /* Implement TARGET_SIMD_CLONE_COMPUTE_VECSIZE_AND_SIMDLEN. */
5611
5612 static int
5613 gcn_simd_clone_compute_vecsize_and_simdlen (struct cgraph_node *ARG_UNUSED (node),
5614 struct cgraph_simd_clone *clonei,
5615 tree ARG_UNUSED (base_type),
5616 int ARG_UNUSED (num),
5617 bool explicit_p)
5618 {
5619 if (known_eq (clonei->simdlen, 0U))
5620 clonei->simdlen = 64;
5621 else if (maybe_ne (clonei->simdlen, 64U))
5622 {
5623 /* Note that x86 has a similar message that is likely to trigger on
5624 sizes that are OK for gcn; the user can't win. */
5625 if (explicit_p)
5626 warning_at (DECL_SOURCE_LOCATION (node->decl), 0,
5627 "unsupported simdlen %wd (amdgcn)",
5628 clonei->simdlen.to_constant ());
5629 return 0;
5630 }
5631
5632 clonei->vecsize_mangle = 'n';
5633 clonei->vecsize_int = 0;
5634 clonei->vecsize_float = 0;
5635
5636 /* DImode ought to be more natural here, but VOIDmode produces better code,
5637 at present, due to the shift-and-test steps not being optimized away
5638 inside the in-branch clones. */
5639 clonei->mask_mode = VOIDmode;
5640
5641 return 1;
5642 }
5643
5644 /* Implement TARGET_SIMD_CLONE_ADJUST. */
5645
5646 static void
5647 gcn_simd_clone_adjust (struct cgraph_node *ARG_UNUSED (node))
5648 {
5649 /* This hook has to be defined when
5650 TARGET_SIMD_CLONE_COMPUTE_VECSIZE_AND_SIMDLEN is defined, but we don't
5651 need it to do anything yet. */
5652 }
5653
5654 /* Implement TARGET_SIMD_CLONE_USABLE. */
5655
5656 static int
5657 gcn_simd_clone_usable (struct cgraph_node *ARG_UNUSED (node),
5658 machine_mode ARG_UNUSED (vector_mode))
5659 {
5660 /* We don't need to do anything here because
5661 gcn_simd_clone_compute_vecsize_and_simdlen currently only returns one
5662 possibility. */
5663 return 0;
5664 }
5665
5666 tree mathfn_built_in_explicit (tree, combined_fn);
5667
5668 /* Implement TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION.
5669 Return the function declaration of the vectorized version of the builtin
5670 in the math library if available. */
5671
5672 tree
5673 gcn_vectorize_builtin_vectorized_function (unsigned int fn, tree type_out,
5674 tree type_in)
5675 {
5676 if (TREE_CODE (type_out) != VECTOR_TYPE
5677 || TREE_CODE (type_in) != VECTOR_TYPE)
5678 return NULL_TREE;
5679
5680 machine_mode out_mode = TYPE_MODE (TREE_TYPE (type_out));
5681 int out_n = TYPE_VECTOR_SUBPARTS (type_out);
5682 combined_fn cfn = combined_fn (fn);
5683
5684 /* Keep this consistent with the list of vectorized math routines. */
5685 int implicit_p;
5686 switch (fn)
5687 {
5688 CASE_CFN_ACOS:
5689 CASE_CFN_ACOSH:
5690 CASE_CFN_ASIN:
5691 CASE_CFN_ASINH:
5692 CASE_CFN_ATAN:
5693 CASE_CFN_ATAN2:
5694 CASE_CFN_ATANH:
5695 CASE_CFN_COPYSIGN:
5696 CASE_CFN_COS:
5697 CASE_CFN_COSH:
5698 CASE_CFN_ERF:
5699 CASE_CFN_EXP:
5700 CASE_CFN_EXP2:
5701 CASE_CFN_FINITE:
5702 CASE_CFN_FMOD:
5703 CASE_CFN_GAMMA:
5704 CASE_CFN_HYPOT:
5705 CASE_CFN_ISNAN:
5706 CASE_CFN_LGAMMA:
5707 CASE_CFN_LOG:
5708 CASE_CFN_LOG10:
5709 CASE_CFN_LOG2:
5710 CASE_CFN_POW:
5711 CASE_CFN_REMAINDER:
5712 CASE_CFN_RINT:
5713 CASE_CFN_SIN:
5714 CASE_CFN_SINH:
5715 CASE_CFN_SQRT:
5716 CASE_CFN_TAN:
5717 CASE_CFN_TANH:
5718 CASE_CFN_TGAMMA:
5719 implicit_p = 1;
5720 break;
5721
5722 CASE_CFN_SCALB:
5723 CASE_CFN_SIGNIFICAND:
5724 implicit_p = 0;
5725 break;
5726
5727 default:
5728 return NULL_TREE;
5729 }
5730
5731 tree out_t_node = (out_mode == DFmode) ? double_type_node : float_type_node;
5732 tree fndecl = implicit_p ? mathfn_built_in (out_t_node, cfn)
5733 : mathfn_built_in_explicit (out_t_node, cfn);
5734
5735 const char *bname = IDENTIFIER_POINTER (DECL_NAME (fndecl));
5736 char name[20];
5737 sprintf (name, out_mode == DFmode ? "v%ddf_%s" : "v%dsf_%s",
5738 out_n, bname + 10);
5739
5740 unsigned arity = 0;
5741 for (tree args = DECL_ARGUMENTS (fndecl); args; args = TREE_CHAIN (args))
5742 arity++;
5743
5744 tree fntype = (arity == 1)
5745 ? build_function_type_list (type_out, type_in, NULL)
5746 : build_function_type_list (type_out, type_in, type_in, NULL);
5747
5748 /* Build a function declaration for the vectorized function. */
5749 tree new_fndecl = build_decl (BUILTINS_LOCATION,
5750 FUNCTION_DECL, get_identifier (name), fntype);
5751 TREE_PUBLIC (new_fndecl) = 1;
5752 DECL_EXTERNAL (new_fndecl) = 1;
5753 DECL_IS_NOVOPS (new_fndecl) = 1;
5754 TREE_READONLY (new_fndecl) = 1;
5755
5756 return new_fndecl;
5757 }
5758
5759 /* Implement TARGET_LIBC_HAS_FUNCTION. */
5760
5761 bool
5762 gcn_libc_has_function (enum function_class fn_class,
5763 tree type)
5764 {
5765 return bsd_libc_has_function (fn_class, type);
5766 }
5767
5768 /* }}} */
5769 /* {{{ md_reorg pass. */
5770
5771 /* Identify VMEM instructions from their "type" attribute. */
5772
5773 static bool
5774 gcn_vmem_insn_p (attr_type type)
5775 {
5776 switch (type)
5777 {
5778 case TYPE_MUBUF:
5779 case TYPE_MTBUF:
5780 case TYPE_FLAT:
5781 case TYPE_VOP3P_MAI:
5782 return true;
5783 case TYPE_UNKNOWN:
5784 case TYPE_SOP1:
5785 case TYPE_SOP2:
5786 case TYPE_SOPK:
5787 case TYPE_SOPC:
5788 case TYPE_SOPP:
5789 case TYPE_SMEM:
5790 case TYPE_DS:
5791 case TYPE_VOP2:
5792 case TYPE_VOP1:
5793 case TYPE_VOPC:
5794 case TYPE_VOP3A:
5795 case TYPE_VOP3B:
5796 case TYPE_VOP_SDWA:
5797 case TYPE_VOP_DPP:
5798 case TYPE_MULT:
5799 case TYPE_VMULT:
5800 return false;
5801 }
5802 gcc_unreachable ();
5803 return false;
5804 }
5805
5806 /* If INSN sets the EXEC register to a constant value, return the value,
5807 otherwise return zero. */
5808
5809 static int64_t
5810 gcn_insn_exec_value (rtx_insn *insn)
5811 {
5812 if (!NONDEBUG_INSN_P (insn))
5813 return 0;
5814
5815 rtx pattern = PATTERN (insn);
5816
5817 if (GET_CODE (pattern) == SET)
5818 {
5819 rtx dest = XEXP (pattern, 0);
5820 rtx src = XEXP (pattern, 1);
5821
5822 if (GET_MODE (dest) == DImode
5823 && REG_P (dest) && REGNO (dest) == EXEC_REG
5824 && CONST_INT_P (src))
5825 return INTVAL (src);
5826 }
5827
5828 return 0;
5829 }
5830
5831 /* Sets the EXEC register before INSN to the value that it had after
5832 LAST_EXEC_DEF. The constant value of the EXEC register is returned if
5833 known, otherwise it returns zero. */
5834
5835 static int64_t
5836 gcn_restore_exec (rtx_insn *insn, rtx_insn *last_exec_def, int64_t curr_exec,
5837 bool curr_exec_known, bool &last_exec_def_saved)
5838 {
5839 rtx exec_reg = gen_rtx_REG (DImode, EXEC_REG);
5840 rtx exec;
5841
5842 int64_t exec_value = gcn_insn_exec_value (last_exec_def);
5843
5844 if (exec_value)
5845 {
5846 /* If the EXEC value is a constant and it happens to be the same as the
5847 current EXEC value, the restore can be skipped. */
5848 if (curr_exec_known && exec_value == curr_exec)
5849 return exec_value;
5850
5851 exec = GEN_INT (exec_value);
5852 }
5853 else
5854 {
5855 /* If the EXEC value is not a constant, save it in a register after the
5856 point of definition. */
5857 rtx exec_save_reg = gen_rtx_REG (DImode, EXEC_SAVE_REG);
5858
5859 if (!last_exec_def_saved)
5860 {
5861 start_sequence ();
5862 emit_move_insn (exec_save_reg, exec_reg);
5863 rtx_insn *seq = get_insns ();
5864 end_sequence ();
5865
5866 emit_insn_after (seq, last_exec_def);
5867 if (dump_file && (dump_flags & TDF_DETAILS))
5868 fprintf (dump_file, "Saving EXEC after insn %d.\n",
5869 INSN_UID (last_exec_def));
5870
5871 last_exec_def_saved = true;
5872 }
5873
5874 exec = exec_save_reg;
5875 }
5876
5877 /* Restore EXEC register before the usage. */
5878 start_sequence ();
5879 emit_move_insn (exec_reg, exec);
5880 rtx_insn *seq = get_insns ();
5881 end_sequence ();
5882 emit_insn_before (seq, insn);
5883
5884 if (dump_file && (dump_flags & TDF_DETAILS))
5885 {
5886 if (exec_value)
5887 fprintf (dump_file, "Restoring EXEC to %ld before insn %d.\n",
5888 exec_value, INSN_UID (insn));
5889 else
5890 fprintf (dump_file,
5891 "Restoring EXEC from saved value before insn %d.\n",
5892 INSN_UID (insn));
5893 }
5894
5895 return exec_value;
5896 }
5897
5898 /* Implement TARGET_MACHINE_DEPENDENT_REORG.
5899
5900 Ensure that pipeline dependencies and lane masking are set correctly. */
5901
5902 static void
5903 gcn_md_reorg (void)
5904 {
5905 basic_block bb;
5906 rtx exec_reg = gen_rtx_REG (DImode, EXEC_REG);
5907 regset_head live;
5908
5909 INIT_REG_SET (&live);
5910
5911 compute_bb_for_insn ();
5912
5913 if (!optimize)
5914 {
5915 split_all_insns ();
5916 if (dump_file && (dump_flags & TDF_DETAILS))
5917 {
5918 fprintf (dump_file, "After split:\n");
5919 print_rtl_with_bb (dump_file, get_insns (), dump_flags);
5920 }
5921
5922 /* Update data-flow information for split instructions. */
5923 df_insn_rescan_all ();
5924 }
5925
5926 df_live_add_problem ();
5927 df_live_set_all_dirty ();
5928 df_analyze ();
5929
5930 /* This pass ensures that the EXEC register is set correctly, according
5931 to the "exec" attribute. However, care must be taken so that the
5932 value that reaches explicit uses of the EXEC register remains the
5933 same as before.
5934 */
5935
5936 FOR_EACH_BB_FN (bb, cfun)
5937 {
5938 if (dump_file && (dump_flags & TDF_DETAILS))
5939 fprintf (dump_file, "BB %d:\n", bb->index);
5940
5941 rtx_insn *insn, *curr;
5942 rtx_insn *last_exec_def = BB_HEAD (bb);
5943 bool last_exec_def_saved = false;
5944 bool curr_exec_explicit = true;
5945 bool curr_exec_known = true;
5946 int64_t curr_exec = 0; /* 0 here means 'the value is that of EXEC
5947 after last_exec_def is executed'. */
5948
5949 bitmap live_in = DF_LR_IN (bb);
5950 bool exec_live_on_entry = false;
5951 if (bitmap_bit_p (live_in, EXEC_LO_REG)
5952 || bitmap_bit_p (live_in, EXEC_HI_REG))
5953 {
5954 if (dump_file)
5955 fprintf (dump_file, "EXEC reg is live on entry to block %d\n",
5956 (int) bb->index);
5957 exec_live_on_entry = true;
5958 }
5959
5960 FOR_BB_INSNS_SAFE (bb, insn, curr)
5961 {
5962 if (!NONDEBUG_INSN_P (insn))
5963 continue;
5964
5965 if (GET_CODE (PATTERN (insn)) == USE
5966 || GET_CODE (PATTERN (insn)) == CLOBBER)
5967 continue;
5968
5969 HARD_REG_SET defs, uses;
5970 CLEAR_HARD_REG_SET (defs);
5971 CLEAR_HARD_REG_SET (uses);
5972 note_stores (insn, record_hard_reg_sets, &defs);
5973 note_uses (&PATTERN (insn), record_hard_reg_uses, &uses);
5974
5975 bool exec_lo_def_p = TEST_HARD_REG_BIT (defs, EXEC_LO_REG);
5976 bool exec_hi_def_p = TEST_HARD_REG_BIT (defs, EXEC_HI_REG);
5977 bool exec_used = (hard_reg_set_intersect_p
5978 (uses, reg_class_contents[(int) EXEC_MASK_REG])
5979 || TEST_HARD_REG_BIT (uses, EXECZ_REG));
5980
5981 /* Check the instruction for implicit setting of EXEC via an
5982 attribute. */
5983 attr_exec exec_attr = get_attr_exec (insn);
5984 int64_t new_exec;
5985
5986 switch (exec_attr)
5987 {
5988 case EXEC_NONE:
5989 new_exec = 0;
5990 break;
5991
5992 case EXEC_SINGLE:
5993 /* Instructions that do not involve memory accesses only require
5994 bit 0 of EXEC to be set. */
5995 if (gcn_vmem_insn_p (get_attr_type (insn))
5996 || get_attr_type (insn) == TYPE_DS)
5997 new_exec = 1;
5998 else
5999 new_exec = curr_exec | 1;
6000 break;
6001
6002 case EXEC_FULL:
6003 new_exec = -1;
6004 break;
6005
6006 default: /* Auto-detect what setting is appropriate. */
6007 {
6008 new_exec = 0;
6009
6010 /* If EXEC is referenced explicitly then we don't need to do
6011 anything to set it, so we're done. */
6012 if (exec_used)
6013 break;
6014
6015 /* Scan the insn for VGPRs defs or uses. The mode determines
6016 what kind of exec is needed. */
6017 subrtx_iterator::array_type array;
6018 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
6019 {
6020 const_rtx x = *iter;
6021 if (REG_P (x) && (VGPR_REGNO_P (REGNO (x))
6022 || AVGPR_REGNO_P (REGNO (x))))
6023 {
6024 if (VECTOR_MODE_P (GET_MODE (x)))
6025 {
6026 int vf = GET_MODE_NUNITS (GET_MODE (x));
6027 new_exec = MAX ((uint64_t)new_exec,
6028 0xffffffffffffffffUL >> (64-vf));
6029 }
6030 else if (new_exec == 0)
6031 new_exec = 1;
6032 }
6033 }
6034 }
6035 break;
6036 }
6037
6038 if (new_exec && (!curr_exec_known || new_exec != curr_exec))
6039 {
6040 start_sequence ();
6041 emit_move_insn (exec_reg, GEN_INT (new_exec));
6042 rtx_insn *seq = get_insns ();
6043 end_sequence ();
6044 emit_insn_before (seq, insn);
6045
6046 if (dump_file && (dump_flags & TDF_DETAILS))
6047 fprintf (dump_file, "Setting EXEC to %ld before insn %d.\n",
6048 new_exec, INSN_UID (insn));
6049
6050 curr_exec = new_exec;
6051 curr_exec_explicit = false;
6052 curr_exec_known = true;
6053 }
6054 else if (new_exec && dump_file && (dump_flags & TDF_DETAILS))
6055 {
6056 fprintf (dump_file, "Exec already is %ld before insn %d.\n",
6057 new_exec, INSN_UID (insn));
6058 }
6059
6060 /* The state of the EXEC register is unknown after a
6061 function call. */
6062 if (CALL_P (insn))
6063 curr_exec_known = false;
6064
6065 /* Handle explicit uses of EXEC. If the instruction is a partial
6066 explicit definition of EXEC, then treat it as an explicit use of
6067 EXEC as well. */
6068 if (exec_used || exec_lo_def_p != exec_hi_def_p)
6069 {
6070 /* An instruction that explicitly uses EXEC should not also
6071 implicitly define it. */
6072 gcc_assert (!exec_used || !new_exec);
6073
6074 if (!curr_exec_known || !curr_exec_explicit)
6075 {
6076 /* Restore the previous explicitly defined value. */
6077 curr_exec = gcn_restore_exec (insn, last_exec_def,
6078 curr_exec, curr_exec_known,
6079 last_exec_def_saved);
6080 curr_exec_explicit = true;
6081 curr_exec_known = true;
6082 }
6083 }
6084
6085 /* Handle explicit definitions of EXEC. */
6086 if (exec_lo_def_p || exec_hi_def_p)
6087 {
6088 last_exec_def = insn;
6089 last_exec_def_saved = false;
6090 curr_exec = gcn_insn_exec_value (insn);
6091 curr_exec_explicit = true;
6092 curr_exec_known = true;
6093
6094 if (dump_file && (dump_flags & TDF_DETAILS))
6095 fprintf (dump_file,
6096 "Found %s definition of EXEC at insn %d.\n",
6097 exec_lo_def_p == exec_hi_def_p ? "full" : "partial",
6098 INSN_UID (insn));
6099 }
6100
6101 exec_live_on_entry = false;
6102 }
6103
6104 COPY_REG_SET (&live, DF_LR_OUT (bb));
6105 df_simulate_initialize_backwards (bb, &live);
6106
6107 /* If EXEC is live after the basic block, restore the value of EXEC
6108 at the end of the block. */
6109 if ((REGNO_REG_SET_P (&live, EXEC_LO_REG)
6110 || REGNO_REG_SET_P (&live, EXEC_HI_REG))
6111 && (!curr_exec_known || !curr_exec_explicit || exec_live_on_entry))
6112 {
6113 rtx_insn *end_insn = BB_END (bb);
6114
6115 /* If the instruction is not a jump instruction, do the restore
6116 after the last instruction in the basic block. */
6117 if (NONJUMP_INSN_P (end_insn))
6118 end_insn = NEXT_INSN (end_insn);
6119
6120 gcn_restore_exec (end_insn, last_exec_def, curr_exec,
6121 curr_exec_known, last_exec_def_saved);
6122 }
6123 }
6124
6125 CLEAR_REG_SET (&live);
6126
6127 /* "Manually Inserted Wait States (NOPs)."
6128
6129 GCN hardware detects most kinds of register dependencies, but there
6130 are some exceptions documented in the ISA manual. This pass
6131 detects the missed cases, and inserts the documented number of NOPs
6132 required for correct execution. */
6133
6134 const int max_waits = 5;
6135 struct ilist
6136 {
6137 rtx_insn *insn;
6138 attr_unit unit;
6139 attr_delayeduse delayeduse;
6140 HARD_REG_SET writes;
6141 HARD_REG_SET reads;
6142 int age;
6143 } back[max_waits];
6144 int oldest = 0;
6145 for (int i = 0; i < max_waits; i++)
6146 back[i].insn = NULL;
6147
6148 rtx_insn *insn, *last_insn = NULL;
6149 for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn))
6150 {
6151 if (!NONDEBUG_INSN_P (insn))
6152 continue;
6153
6154 if (GET_CODE (PATTERN (insn)) == USE
6155 || GET_CODE (PATTERN (insn)) == CLOBBER)
6156 continue;
6157
6158 attr_type itype = get_attr_type (insn);
6159 attr_unit iunit = get_attr_unit (insn);
6160 attr_delayeduse idelayeduse = get_attr_delayeduse (insn);
6161 int ivccwait = get_attr_vccwait (insn);
6162 HARD_REG_SET ireads, iwrites;
6163 CLEAR_HARD_REG_SET (ireads);
6164 CLEAR_HARD_REG_SET (iwrites);
6165 note_stores (insn, record_hard_reg_sets, &iwrites);
6166 note_uses (&PATTERN (insn), record_hard_reg_uses, &ireads);
6167
6168 /* Scan recent previous instructions for dependencies not handled in
6169 hardware. */
6170 int nops_rqd = 0;
6171 for (int i = oldest; i < oldest + max_waits; i++)
6172 {
6173 struct ilist *prev_insn = &back[i % max_waits];
6174
6175 if (!prev_insn->insn)
6176 continue;
6177
6178 HARD_REG_SET depregs = prev_insn->writes & ireads;
6179
6180 /* VALU writes SGPR followed by VMEM reading the same SGPR
6181 requires 5 wait states. */
6182 if ((prev_insn->age + nops_rqd) < 5
6183 && prev_insn->unit == UNIT_VECTOR
6184 && gcn_vmem_insn_p (itype)
6185 && hard_reg_set_intersect_p
6186 (depregs, reg_class_contents[(int) SGPR_REGS]))
6187 nops_rqd = 5 - prev_insn->age;
6188
6189 /* VALU sets VCC/EXEC followed by VALU uses VCCZ/EXECZ
6190 requires 5 wait states. */
6191 if ((prev_insn->age + nops_rqd) < 5
6192 && prev_insn->unit == UNIT_VECTOR
6193 && iunit == UNIT_VECTOR
6194 && ((hard_reg_set_intersect_p
6195 (prev_insn->writes,
6196 reg_class_contents[(int) EXEC_MASK_REG])
6197 && TEST_HARD_REG_BIT (ireads, EXECZ_REG))
6198 ||
6199 (hard_reg_set_intersect_p
6200 (prev_insn->writes,
6201 reg_class_contents[(int) VCC_CONDITIONAL_REG])
6202 && TEST_HARD_REG_BIT (ireads, VCCZ_REG))))
6203 nops_rqd = 5 - prev_insn->age;
6204
6205 /* VALU writes SGPR/VCC followed by v_{read,write}lane using
6206 SGPR/VCC as lane select requires 4 wait states. */
6207 if ((prev_insn->age + nops_rqd) < 4
6208 && prev_insn->unit == UNIT_VECTOR
6209 && get_attr_laneselect (insn) == LANESELECT_YES
6210 && (hard_reg_set_intersect_p
6211 (depregs, reg_class_contents[(int) SGPR_REGS])
6212 || hard_reg_set_intersect_p
6213 (depregs, reg_class_contents[(int) VCC_CONDITIONAL_REG])))
6214 nops_rqd = 4 - prev_insn->age;
6215
6216 /* VALU writes VGPR followed by VALU_DPP reading that VGPR
6217 requires 2 wait states. */
6218 if ((prev_insn->age + nops_rqd) < 2
6219 && prev_insn->unit == UNIT_VECTOR
6220 && itype == TYPE_VOP_DPP)
6221 {
6222 if (hard_reg_set_intersect_p
6223 (depregs, reg_class_contents[(int) VGPR_REGS]))
6224 nops_rqd = 2 - prev_insn->age;
6225 }
6226
6227 /* Store that requires input registers are not overwritten by
6228 following instruction. */
6229 if ((prev_insn->age + nops_rqd) < 1
6230 && prev_insn->delayeduse == DELAYEDUSE_YES
6231 && ((hard_reg_set_intersect_p
6232 (prev_insn->reads, iwrites))))
6233 nops_rqd = 1 - prev_insn->age;
6234
6235 /* Instruction that requires VCC is not written too close before
6236 using it. */
6237 if (prev_insn->age < ivccwait
6238 && (hard_reg_set_intersect_p
6239 (prev_insn->writes,
6240 reg_class_contents[(int)VCC_CONDITIONAL_REG])))
6241 nops_rqd = ivccwait - prev_insn->age;
6242
6243 /* CDNA1: write VGPR before v_accvgpr_write reads it. */
6244 if (TARGET_AVGPR_CDNA1_NOPS
6245 && (prev_insn->age + nops_rqd) < 2
6246 && hard_reg_set_intersect_p
6247 (depregs, reg_class_contents[(int) VGPR_REGS])
6248 && hard_reg_set_intersect_p
6249 (iwrites, reg_class_contents[(int) AVGPR_REGS]))
6250 nops_rqd = 2 - prev_insn->age;
6251
6252 /* CDNA1: v_accvgpr_write writes AVGPR before v_accvgpr_read. */
6253 if (TARGET_AVGPR_CDNA1_NOPS
6254 && (prev_insn->age + nops_rqd) < 3
6255 && hard_reg_set_intersect_p
6256 (depregs, reg_class_contents[(int) AVGPR_REGS])
6257 && hard_reg_set_intersect_p
6258 (iwrites, reg_class_contents[(int) VGPR_REGS]))
6259 nops_rqd = 3 - prev_insn->age;
6260
6261 /* CDNA1: Undocumented(?!) read-after-write when restoring values
6262 from AVGPRs to VGPRS. Observed problem was for address register
6263 of flat_load instruction, but others may be affected? */
6264 if (TARGET_AVGPR_CDNA1_NOPS
6265 && (prev_insn->age + nops_rqd) < 2
6266 && hard_reg_set_intersect_p
6267 (prev_insn->reads, reg_class_contents[(int) AVGPR_REGS])
6268 && hard_reg_set_intersect_p
6269 (depregs, reg_class_contents[(int) VGPR_REGS]))
6270 nops_rqd = 2 - prev_insn->age;
6271 }
6272
6273 /* Insert the required number of NOPs. */
6274 for (int i = nops_rqd; i > 0; i--)
6275 emit_insn_after (gen_nop (), last_insn);
6276
6277 /* Age the previous instructions. We can also ignore writes to
6278 registers subsequently overwritten. */
6279 HARD_REG_SET written;
6280 CLEAR_HARD_REG_SET (written);
6281 for (int i = oldest + max_waits - 1; i > oldest; i--)
6282 {
6283 struct ilist *prev_insn = &back[i % max_waits];
6284
6285 /* Assume all instructions are equivalent to one "wait", the same
6286 as s_nop. This is probably true for SALU, but not VALU (which
6287 may take longer), so this is not optimal. However, AMD do
6288 not publish the cycle times for instructions. */
6289 prev_insn->age += 1 + nops_rqd;
6290
6291 written |= iwrites;
6292 prev_insn->writes &= ~written;
6293 }
6294
6295 /* Track the current instruction as a previous instruction. */
6296 back[oldest].insn = insn;
6297 back[oldest].unit = iunit;
6298 back[oldest].delayeduse = idelayeduse;
6299 back[oldest].writes = iwrites;
6300 back[oldest].reads = ireads;
6301 back[oldest].age = 0;
6302 oldest = (oldest + 1) % max_waits;
6303
6304 last_insn = insn;
6305 }
6306 }
6307
6308 /* }}} */
6309 /* {{{ OpenACC / OpenMP. */
6310
6311 #define GCN_DEFAULT_GANGS 0 /* Choose at runtime. */
6312 #define GCN_DEFAULT_WORKERS 0 /* Choose at runtime. */
6313 #define GCN_DEFAULT_VECTORS 1 /* Use autovectorization only, for now. */
6314
6315 /* Implement TARGET_GOACC_VALIDATE_DIMS.
6316
6317 Check the launch dimensions provided for an OpenACC compute
6318 region, or routine. */
6319
6320 static bool
6321 gcn_goacc_validate_dims (tree decl, int dims[], int fn_level,
6322 unsigned /*used*/)
6323 {
6324 bool changed = false;
6325 const int max_workers = 16;
6326
6327 /* The vector size must appear to be 64, to the user, unless this is a
6328 SEQ routine. The real, internal value is always 1, which means use
6329 autovectorization, but the user should not see that. */
6330 if (fn_level <= GOMP_DIM_VECTOR && fn_level >= -1
6331 && dims[GOMP_DIM_VECTOR] >= 0)
6332 {
6333 if (fn_level < 0 && dims[GOMP_DIM_VECTOR] >= 0
6334 && dims[GOMP_DIM_VECTOR] != 64)
6335 warning_at (decl ? DECL_SOURCE_LOCATION (decl) : UNKNOWN_LOCATION,
6336 OPT_Wopenacc_dims,
6337 (dims[GOMP_DIM_VECTOR]
6338 ? G_("using %<vector_length (64)%>, ignoring %d")
6339 : G_("using %<vector_length (64)%>, "
6340 "ignoring runtime setting")),
6341 dims[GOMP_DIM_VECTOR]);
6342 dims[GOMP_DIM_VECTOR] = 1;
6343 changed = true;
6344 }
6345
6346 /* Check the num workers is not too large. */
6347 if (dims[GOMP_DIM_WORKER] > max_workers)
6348 {
6349 warning_at (decl ? DECL_SOURCE_LOCATION (decl) : UNKNOWN_LOCATION,
6350 OPT_Wopenacc_dims,
6351 "using %<num_workers (%d)%>, ignoring %d",
6352 max_workers, dims[GOMP_DIM_WORKER]);
6353 dims[GOMP_DIM_WORKER] = max_workers;
6354 changed = true;
6355 }
6356
6357 /* Set global defaults. */
6358 if (!decl)
6359 {
6360 dims[GOMP_DIM_VECTOR] = GCN_DEFAULT_VECTORS;
6361 if (dims[GOMP_DIM_WORKER] < 0)
6362 dims[GOMP_DIM_WORKER] = GCN_DEFAULT_WORKERS;
6363 if (dims[GOMP_DIM_GANG] < 0)
6364 dims[GOMP_DIM_GANG] = GCN_DEFAULT_GANGS;
6365 changed = true;
6366 }
6367
6368 return changed;
6369 }
6370
6371 /* Helper function for oacc_dim_size instruction.
6372 Also used for OpenMP, via builtin_gcn_dim_size, and the omp_gcn pass. */
6373
6374 rtx
6375 gcn_oacc_dim_size (int dim)
6376 {
6377 if (dim < 0 || dim > 2)
6378 error ("offload dimension out of range (%d)", dim);
6379
6380 /* Vectors are a special case. */
6381 if (dim == 2)
6382 return const1_rtx; /* Think of this as 1 times 64. */
6383
6384 static int offset[] = {
6385 /* Offsets into dispatch packet. */
6386 12, /* X dim = Gang / Team / Work-group. */
6387 20, /* Z dim = Worker / Thread / Wavefront. */
6388 16 /* Y dim = Vector / SIMD / Work-item. */
6389 };
6390 rtx addr = gen_rtx_PLUS (DImode,
6391 gen_rtx_REG (DImode,
6392 cfun->machine->args.
6393 reg[DISPATCH_PTR_ARG]),
6394 GEN_INT (offset[dim]));
6395 rtx mem = gen_rtx_MEM (SImode, addr);
6396 set_mem_addr_space (mem, ADDR_SPACE_SCALAR_FLAT);
6397 return mem;
6398 }
6399
6400 /* Helper function for oacc_dim_pos instruction.
6401 Also used for OpenMP, via builtin_gcn_dim_pos, and the omp_gcn pass. */
6402
6403 rtx
6404 gcn_oacc_dim_pos (int dim)
6405 {
6406 if (dim < 0 || dim > 2)
6407 error ("offload dimension out of range (%d)", dim);
6408
6409 static const int reg[] = {
6410 WORKGROUP_ID_X_ARG, /* Gang / Team / Work-group. */
6411 WORK_ITEM_ID_Z_ARG, /* Worker / Thread / Wavefront. */
6412 WORK_ITEM_ID_Y_ARG /* Vector / SIMD / Work-item. */
6413 };
6414
6415 int reg_num = cfun->machine->args.reg[reg[dim]];
6416
6417 /* The information must have been requested by the kernel. */
6418 gcc_assert (reg_num >= 0);
6419
6420 return gen_rtx_REG (SImode, reg_num);
6421 }
6422
6423 /* Implement TARGET_GOACC_FORK_JOIN. */
6424
6425 static bool
6426 gcn_fork_join (gcall *call, const int dims[], bool is_fork)
6427 {
6428 tree arg = gimple_call_arg (call, 2);
6429 unsigned axis = TREE_INT_CST_LOW (arg);
6430
6431 if (!is_fork && axis == GOMP_DIM_WORKER && dims[axis] != 1)
6432 return true;
6433
6434 return false;
6435 }
6436
6437 /* Implement ???????
6438 FIXME make this a real hook.
6439
6440 Adjust FNDECL such that options inherited from the host compiler
6441 are made appropriate for the accelerator compiler. */
6442
6443 void
6444 gcn_fixup_accel_lto_options (tree fndecl)
6445 {
6446 tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
6447 if (!func_optimize)
6448 return;
6449
6450 tree old_optimize
6451 = build_optimization_node (&global_options, &global_options_set);
6452 tree new_optimize;
6453
6454 /* If the function changed the optimization levels as well as
6455 setting target options, start with the optimizations
6456 specified. */
6457 if (func_optimize != old_optimize)
6458 cl_optimization_restore (&global_options, &global_options_set,
6459 TREE_OPTIMIZATION (func_optimize));
6460
6461 gcn_option_override ();
6462
6463 /* The target attributes may also change some optimization flags,
6464 so update the optimization options if necessary. */
6465 new_optimize = build_optimization_node (&global_options,
6466 &global_options_set);
6467
6468 if (old_optimize != new_optimize)
6469 {
6470 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
6471 cl_optimization_restore (&global_options, &global_options_set,
6472 TREE_OPTIMIZATION (old_optimize));
6473 }
6474 }
6475
6476 /* Implement TARGET_GOACC_SHARED_MEM_LAYOUT hook. */
6477
6478 static void
6479 gcn_shared_mem_layout (unsigned HOST_WIDE_INT *lo,
6480 unsigned HOST_WIDE_INT *hi,
6481 int ARG_UNUSED (dims[GOMP_DIM_MAX]),
6482 unsigned HOST_WIDE_INT
6483 ARG_UNUSED (private_size[GOMP_DIM_MAX]),
6484 unsigned HOST_WIDE_INT reduction_size[GOMP_DIM_MAX])
6485 {
6486 *lo = gang_private_size_opt + reduction_size[GOMP_DIM_WORKER];
6487 /* !!! We can maybe use dims[] to estimate the maximum number of work
6488 groups/wavefronts/etc. we will launch, and therefore tune the maximum
6489 amount of LDS we should use. For now, use a minimal amount to try to
6490 maximise occupancy. */
6491 *hi = acc_lds_size;
6492 machine_function *machfun = cfun->machine;
6493 machfun->reduction_base = gang_private_size_opt;
6494 machfun->reduction_limit
6495 = gang_private_size_opt + reduction_size[GOMP_DIM_WORKER];
6496 }
6497
6498 /* }}} */
6499 /* {{{ ASM Output. */
6500
6501 /* Implement TARGET_ASM_FILE_START.
6502
6503 Print assembler file header text. */
6504
6505 static void
6506 output_file_start (void)
6507 {
6508 /* In HSACOv4 no attribute setting means the binary supports "any" hardware
6509 configuration. */
6510 const char *xnack = (flag_xnack == HSACO_ATTR_ON ? ":xnack+"
6511 : flag_xnack == HSACO_ATTR_OFF ? ":xnack-"
6512 : "" /* Unsupported or "any". */);
6513 const char *sram_ecc = (flag_sram_ecc == HSACO_ATTR_ON ? ":sramecc+"
6514 : flag_sram_ecc == HSACO_ATTR_OFF ? ":sramecc-"
6515 : "" /* Unsupported or "any". */);
6516 const char *cpu = gcn_devices[gcn_arch].name;
6517
6518 fprintf(asm_out_file, "\t.amdgcn_target \"amdgcn-unknown-amdhsa--%s%s%s\"\n",
6519 cpu, sram_ecc, xnack);
6520 }
6521
6522 /* Implement ASM_DECLARE_FUNCTION_NAME via gcn-hsa.h.
6523
6524 Print the initial definition of a function name.
6525
6526 For GCN kernel entry points this includes all the HSA meta-data, special
6527 alignment constraints that don't apply to regular functions, and magic
6528 comments that pass information to mkoffload. */
6529
6530 void
6531 gcn_hsa_declare_function_name (FILE *file, const char *name,
6532 tree decl ATTRIBUTE_UNUSED)
6533 {
6534 int sgpr, vgpr, avgpr;
6535 bool xnack_enabled = TARGET_XNACK;
6536
6537 fputs ("\n\n", file);
6538
6539 if (cfun && cfun->machine && cfun->machine->normal_function)
6540 {
6541 fputs ("\t.type\t", file);
6542 assemble_name (file, name);
6543 fputs (",@function\n", file);
6544 assemble_name (file, name);
6545 fputs (":\n", file);
6546 return;
6547 }
6548
6549 /* Determine count of sgpr/vgpr registers by looking for last
6550 one used. */
6551 for (sgpr = LAST_SGPR_REG - FIRST_SGPR_REG; sgpr >= 0; sgpr--)
6552 if (df_regs_ever_live_p (FIRST_SGPR_REG + sgpr))
6553 break;
6554 sgpr++;
6555 for (vgpr = LAST_VGPR_REG - FIRST_VGPR_REG; vgpr >= 0; vgpr--)
6556 if (df_regs_ever_live_p (FIRST_VGPR_REG + vgpr))
6557 break;
6558 vgpr++;
6559 for (avgpr = LAST_AVGPR_REG - FIRST_AVGPR_REG; avgpr >= 0; avgpr--)
6560 if (df_regs_ever_live_p (FIRST_AVGPR_REG + avgpr))
6561 break;
6562 avgpr++;
6563
6564 /* The main function epilogue uses v8, but df doesn't see that. */
6565 if (vgpr < 9)
6566 vgpr = 9;
6567
6568 if (!leaf_function_p ())
6569 {
6570 /* We can't know how many registers function calls might use. */
6571 if (vgpr < MAX_NORMAL_VGPR_COUNT)
6572 vgpr = MAX_NORMAL_VGPR_COUNT;
6573 if (sgpr < MAX_NORMAL_SGPR_COUNT)
6574 sgpr = MAX_NORMAL_SGPR_COUNT;
6575 if (avgpr < MAX_NORMAL_AVGPR_COUNT)
6576 avgpr = MAX_NORMAL_AVGPR_COUNT;
6577 }
6578
6579 /* SIMD32 devices count double in wavefront64 mode. */
6580 if (TARGET_WAVE64_COMPAT)
6581 vgpr *= 2;
6582
6583 /* Round up to the allocation block size. */
6584 int vgpr_block_size = TARGET_VGPR_GRANULARITY;
6585 if (vgpr % vgpr_block_size)
6586 vgpr += vgpr_block_size - (vgpr % vgpr_block_size);
6587 if (avgpr % vgpr_block_size)
6588 avgpr += vgpr_block_size - (avgpr % vgpr_block_size);
6589
6590 switch_to_section (readonly_data_section);
6591 fputs ("\t.p2align\t6\n"
6592 "\t.amdhsa_kernel\t", file);
6593 assemble_name (file, name);
6594 fputs ("\n", file);
6595 int reg = FIRST_SGPR_REG;
6596 for (int a = 0; a < GCN_KERNEL_ARG_TYPES; a++)
6597 {
6598 int reg_first = -1;
6599 int reg_last;
6600 if ((cfun->machine->args.requested & (1 << a))
6601 && (gcn_kernel_arg_types[a].fixed_regno < 0))
6602 {
6603 reg_first = reg;
6604 reg_last = (reg_first
6605 + (GET_MODE_SIZE (gcn_kernel_arg_types[a].mode)
6606 / UNITS_PER_WORD) - 1);
6607 reg = reg_last + 1;
6608 }
6609
6610 if (gcn_kernel_arg_types[a].header_pseudo)
6611 {
6612 fprintf (file, "\t %s%s\t%i",
6613 (cfun->machine->args.requested & (1 << a)) != 0 ? "" : ";",
6614 gcn_kernel_arg_types[a].header_pseudo,
6615 (cfun->machine->args.requested & (1 << a)) != 0);
6616 if (reg_first != -1)
6617 {
6618 fprintf (file, " ; (");
6619 for (int i = reg_first; i <= reg_last; ++i)
6620 {
6621 if (i != reg_first)
6622 fprintf (file, ", ");
6623 fprintf (file, "%s", reg_names[i]);
6624 }
6625 fprintf (file, ")");
6626 }
6627 fprintf (file, "\n");
6628 }
6629 else if (gcn_kernel_arg_types[a].fixed_regno >= 0
6630 && cfun->machine->args.requested & (1 << a))
6631 fprintf (file, "\t ; %s\t%i (%s)\n",
6632 gcn_kernel_arg_types[a].name,
6633 (cfun->machine->args.requested & (1 << a)) != 0,
6634 reg_names[gcn_kernel_arg_types[a].fixed_regno]);
6635 }
6636 fprintf (file, "\t .amdhsa_system_vgpr_workitem_id\t%i\n",
6637 (cfun->machine->args.requested & (1 << WORK_ITEM_ID_Z_ARG))
6638 ? 2
6639 : cfun->machine->args.requested & (1 << WORK_ITEM_ID_Y_ARG)
6640 ? 1 : 0);
6641 int next_free_vgpr = vgpr;
6642 if (TARGET_AVGPR_COMBINED)
6643 next_free_vgpr += avgpr;
6644 else if (TARGET_AVGPRS && avgpr > vgpr)
6645 next_free_vgpr = avgpr;
6646 fprintf (file,
6647 "\t .amdhsa_next_free_vgpr\t%i\n"
6648 "\t .amdhsa_next_free_sgpr\t%i\n"
6649 "\t .amdhsa_reserve_vcc\t1\n"
6650 "\t .amdhsa_reserve_xnack_mask\t%i\n"
6651 "\t .amdhsa_private_segment_fixed_size\t0\n"
6652 "\t .amdhsa_group_segment_fixed_size\t%u\n"
6653 "\t .amdhsa_float_denorm_mode_32\t3\n"
6654 "\t .amdhsa_float_denorm_mode_16_64\t3\n",
6655 next_free_vgpr,
6656 sgpr,
6657 xnack_enabled,
6658 LDS_SIZE);
6659 /* Not supported with 'architected flat scratch'. */
6660 if (!TARGET_ARCHITECTED_FLAT_SCRATCH)
6661 fprintf (file,
6662 "\t .amdhsa_reserve_flat_scratch\t0\n");
6663 if (TARGET_AVGPR_COMBINED)
6664 fprintf (file,
6665 "\t .amdhsa_accum_offset\t%i\n",
6666 vgpr); /* The AGPRs come after the VGPRs. */
6667 if (TARGET_TGSPLIT)
6668 fprintf (file,
6669 "\t .amdhsa_tg_split\t0\n");
6670 fputs ("\t.end_amdhsa_kernel\n", file);
6671
6672 #if 1
6673 /* The following is YAML embedded in assembler; tabs are not allowed. */
6674
6675 /* 'amdhsa.version': code object V3 = 1.0, V4 = 1.1, V5/V6 = 1.2. */
6676 /* Keep in sync with 'amdhsa-code-object' in gen-gcn-device-macros.awk. */
6677 fprintf (file,
6678 " .amdgpu_metadata\n"
6679 " amdhsa.version:\n"
6680 " - 1\n"
6681 " - %d\n"
6682 " amdhsa.kernels:\n"
6683 " - .name: ",
6684 gcn_devices[gcn_arch].generic_version ? 2 /* V6 */ : 1 /* V4 */);
6685 assemble_name (file, name);
6686 fputs ("\n .symbol: ", file);
6687 assemble_name (file, name);
6688 fprintf (file,
6689 ".kd\n"
6690 " .kernarg_segment_size: %i\n"
6691 " .kernarg_segment_align: %i\n"
6692 " .group_segment_fixed_size: %u\n"
6693 " .private_segment_fixed_size: 0\n"
6694 " .wavefront_size: 64\n"
6695 " .sgpr_count: %i\n"
6696 " .vgpr_count: %i%s\n"
6697 " .max_flat_workgroup_size: 1024\n",
6698 cfun->machine->kernarg_segment_byte_size,
6699 cfun->machine->kernarg_segment_alignment,
6700 LDS_SIZE,
6701 sgpr, next_free_vgpr,
6702 (TARGET_WAVE64_COMPAT
6703 ? " ; wavefrontsize64 counts double on SIMD32"
6704 : ""));
6705 if (TARGET_AVGPRS)
6706 fprintf (file, " .agpr_count: %i\n", avgpr);
6707 fputs (" .end_amdgpu_metadata\n", file);
6708 #endif
6709
6710 switch_to_section (current_function_section ());
6711 fputs ("\t.align\t256\n", file);
6712 fputs ("\t.type\t", file);
6713 assemble_name (file, name);
6714 fputs (",@function\n", file);
6715 ASM_OUTPUT_FUNCTION_LABEL (file, name, decl);
6716
6717 /* This comment is read by mkoffload. */
6718 if (flag_openacc)
6719 fprintf (file, "\t;; OPENACC-DIMS: %d, %d, %d : %s\n",
6720 oacc_get_fn_dim_size (cfun->decl, GOMP_DIM_GANG),
6721 oacc_get_fn_dim_size (cfun->decl, GOMP_DIM_WORKER),
6722 oacc_get_fn_dim_size (cfun->decl, GOMP_DIM_VECTOR), name);
6723 }
6724
6725 /* Implement TARGET_ASM_INIT_SECTIONS. */
6726
6727 static void
6728 gcn_asm_init_sections (void)
6729 {
6730 /* With the default 'exception_section' (via
6731 'gcc/except.cc:switch_to_exception_section'), the assembler fails:
6732 /tmp/ccTYJljP.s:95:2: error: changed section flags for .gcc_except_table, expected: 0x2
6733 .section .gcc_except_table,"aw",@progbits
6734 ^
6735 The flags for '.gcc_except_table' don't match the pre-defined ones that
6736 the assembler expects: 'SHF_ALLOC' ('a'), without 'SHF_WRITE' ('w').
6737 As we're not actually implementing exception handling, keep things simple,
6738 and allocate a "fake" section. */
6739 exception_section = get_section (".fake_gcc_except_table",
6740 SECTION_DEBUG /* '!SHF_ALLOC' */
6741 | SECTION_EXCLUDE /* 'SHF_EXCLUDE' */,
6742 NULL);
6743 }
6744
6745 /* Implement TARGET_ASM_SELECT_SECTION.
6746
6747 Return the section into which EXP should be placed. */
6748
6749 static section *
6750 gcn_asm_select_section (tree exp, int reloc, unsigned HOST_WIDE_INT align)
6751 {
6752 if (TREE_TYPE (exp) != error_mark_node
6753 && TYPE_ADDR_SPACE (TREE_TYPE (exp)) == ADDR_SPACE_LDS)
6754 {
6755 if (!DECL_P (exp))
6756 return get_section (".lds_bss",
6757 SECTION_WRITE | SECTION_BSS | SECTION_DEBUG,
6758 NULL);
6759
6760 return get_named_section (exp, ".lds_bss", reloc);
6761 }
6762
6763 return default_elf_select_section (exp, reloc, align);
6764 }
6765
6766 /* Implement TARGET_ASM_FUNCTION_PROLOGUE.
6767
6768 Emits custom text into the assembler file at the head of each function. */
6769
6770 static void
6771 gcn_target_asm_function_prologue (FILE *file)
6772 {
6773 machine_function *offsets = gcn_compute_frame_offsets ();
6774
6775 asm_fprintf (file, "\t; using %s addressing in function\n",
6776 offsets->use_flat_addressing ? "flat" : "global");
6777
6778 if (offsets->normal_function)
6779 {
6780 asm_fprintf (file, "\t; frame pointer needed: %s\n",
6781 offsets->need_frame_pointer ? "true" : "false");
6782 asm_fprintf (file, "\t; lr needs saving: %s\n",
6783 offsets->lr_needs_saving ? "true" : "false");
6784 asm_fprintf (file, "\t; outgoing args size: %wd\n",
6785 offsets->outgoing_args_size);
6786 asm_fprintf (file, "\t; pretend size: %wd\n", offsets->pretend_size);
6787 asm_fprintf (file, "\t; local vars size: %wd\n", offsets->local_vars);
6788 asm_fprintf (file, "\t; callee save size: %wd\n",
6789 offsets->callee_saves);
6790 }
6791 else
6792 {
6793 asm_fprintf (file, "\t; HSA kernel entry point\n");
6794 asm_fprintf (file, "\t; local vars size: %wd\n", offsets->local_vars);
6795 asm_fprintf (file, "\t; outgoing args size: %wd\n",
6796 offsets->outgoing_args_size);
6797 }
6798 }
6799
6800 /* Helper function for print_operand and print_operand_address.
6801
6802 Print a register as the assembler requires, according to mode and name. */
6803
6804 static void
6805 print_reg (FILE *file, rtx x)
6806 {
6807 machine_mode mode = GET_MODE (x);
6808 if (VECTOR_MODE_P (mode))
6809 mode = GET_MODE_INNER (mode);
6810 if (mode == BImode || mode == QImode || mode == HImode || mode == SImode
6811 || mode == HFmode || mode == SFmode)
6812 fprintf (file, "%s", reg_names[REGNO (x)]);
6813 else if (mode == DImode || mode == DFmode)
6814 {
6815 if (SGPR_REGNO_P (REGNO (x)))
6816 fprintf (file, "s[%i:%i]", REGNO (x) - FIRST_SGPR_REG,
6817 REGNO (x) - FIRST_SGPR_REG + 1);
6818 else if (VGPR_REGNO_P (REGNO (x)))
6819 fprintf (file, "v[%i:%i]", REGNO (x) - FIRST_VGPR_REG,
6820 REGNO (x) - FIRST_VGPR_REG + 1);
6821 else if (AVGPR_REGNO_P (REGNO (x)))
6822 fprintf (file, "a[%i:%i]", REGNO (x) - FIRST_AVGPR_REG,
6823 REGNO (x) - FIRST_AVGPR_REG + 1);
6824 else if (REGNO (x) == FLAT_SCRATCH_REG)
6825 fprintf (file, "flat_scratch");
6826 else if (REGNO (x) == EXEC_REG)
6827 fprintf (file, "exec");
6828 else if (REGNO (x) == VCC_LO_REG)
6829 fprintf (file, "vcc");
6830 else
6831 fprintf (file, "[%s:%s]",
6832 reg_names[REGNO (x)], reg_names[REGNO (x) + 1]);
6833 }
6834 else if (mode == TImode)
6835 {
6836 if (SGPR_REGNO_P (REGNO (x)))
6837 fprintf (file, "s[%i:%i]", REGNO (x) - FIRST_SGPR_REG,
6838 REGNO (x) - FIRST_SGPR_REG + 3);
6839 else if (VGPR_REGNO_P (REGNO (x)))
6840 fprintf (file, "v[%i:%i]", REGNO (x) - FIRST_VGPR_REG,
6841 REGNO (x) - FIRST_VGPR_REG + 3);
6842 else if (AVGPR_REGNO_P (REGNO (x)))
6843 fprintf (file, "a[%i:%i]", REGNO (x) - FIRST_AVGPR_REG,
6844 REGNO (x) - FIRST_AVGPR_REG + 3);
6845 else
6846 gcc_unreachable ();
6847 }
6848 else
6849 gcc_unreachable ();
6850 }
6851
6852 /* Implement TARGET_SECTION_TYPE_FLAGS.
6853
6854 Return a set of section attributes for use by TARGET_ASM_NAMED_SECTION. */
6855
6856 static unsigned int
6857 gcn_section_type_flags (tree decl, const char *name, int reloc)
6858 {
6859 if (strcmp (name, ".lds_bss") == 0)
6860 return SECTION_WRITE | SECTION_BSS | SECTION_DEBUG;
6861
6862 return default_section_type_flags (decl, name, reloc);
6863 }
6864
6865 /* Helper function for gcn_asm_output_symbol_ref.
6866
6867 FIXME: This function is used to lay out gang-private variables in LDS
6868 on a per-CU basis.
6869 There may be cases in which gang-private variables in different compilation
6870 units could clobber each other. In that case we should be relying on the
6871 linker to lay out gang-private LDS space, but that doesn't appear to be
6872 possible at present. */
6873
6874 static void
6875 gcn_print_lds_decl (FILE *f, tree var)
6876 {
6877 int *offset;
6878 if ((offset = lds_allocs.get (var)))
6879 fprintf (f, "%u", (unsigned) *offset);
6880 else
6881 {
6882 unsigned HOST_WIDE_INT align = DECL_ALIGN_UNIT (var);
6883 tree type = TREE_TYPE (var);
6884 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6885 if (size > align && size > 4 && align < 8)
6886 align = 8;
6887
6888 gang_private_hwm = ((gang_private_hwm + align - 1) & ~(align - 1));
6889
6890 lds_allocs.put (var, gang_private_hwm);
6891 fprintf (f, "%u", gang_private_hwm);
6892 gang_private_hwm += size;
6893 if (gang_private_hwm > gang_private_size_opt)
6894 error ("%d bytes of gang-private data-share memory exhausted"
6895 " (increase with %<-mgang-private-size=%d%>, for example)",
6896 gang_private_size_opt, gang_private_hwm);
6897 }
6898 }
6899
6900 /* Implement ASM_OUTPUT_SYMBOL_REF via gcn-hsa.h. */
6901
6902 void
6903 gcn_asm_output_symbol_ref (FILE *file, rtx x)
6904 {
6905 tree decl;
6906 if (cfun
6907 && (decl = SYMBOL_REF_DECL (x)) != 0
6908 && VAR_P (decl)
6909 && AS_LDS_P (TYPE_ADDR_SPACE (TREE_TYPE (decl))))
6910 {
6911 /* LDS symbols (emitted using this hook) are only used at present
6912 to propagate worker values from an active thread to neutered
6913 threads. Use the same offset for each such block, but don't
6914 use zero because null pointers are used to identify the active
6915 thread in GOACC_single_copy_start calls. */
6916 gcn_print_lds_decl (file, decl);
6917 }
6918 else
6919 {
6920 assemble_name (file, XSTR (x, 0));
6921 /* FIXME: See above -- this condition is unreachable. */
6922 if (cfun
6923 && (decl = SYMBOL_REF_DECL (x)) != 0
6924 && VAR_P (decl)
6925 && AS_LDS_P (TYPE_ADDR_SPACE (TREE_TYPE (decl))))
6926 fputs ("@abs32", file);
6927 }
6928 }
6929
6930 void
6931 gcn_asm_weaken_decl (FILE *stream, tree decl, const char *name,
6932 const char *value)
6933 {
6934 if (!value
6935 && DECL_EXTERNAL (decl))
6936 /* Don't emit weak undefined symbols; see PR119369. */
6937 return;
6938 if (value)
6939 ASM_OUTPUT_WEAK_ALIAS (stream, name, value);
6940 else
6941 ASM_WEAKEN_LABEL (stream, name);
6942 }
6943
6944 /* Implement TARGET_CONSTANT_ALIGNMENT.
6945
6946 Returns the alignment in bits of a constant that is being placed in memory.
6947 CONSTANT is the constant and BASIC_ALIGN is the alignment that the object
6948 would ordinarily have. */
6949
6950 static HOST_WIDE_INT
6951 gcn_constant_alignment (const_tree ARG_UNUSED (constant),
6952 HOST_WIDE_INT basic_align)
6953 {
6954 return basic_align > 128 ? basic_align : 128;
6955 }
6956
6957 /* Implement PRINT_OPERAND_ADDRESS via gcn.h. */
6958
6959 void
6960 print_operand_address (FILE *file, rtx mem)
6961 {
6962 gcc_assert (MEM_P (mem));
6963
6964 rtx reg;
6965 rtx offset;
6966 addr_space_t as = MEM_ADDR_SPACE (mem);
6967 rtx addr = XEXP (mem, 0);
6968 gcc_assert (REG_P (addr) || GET_CODE (addr) == PLUS);
6969
6970 if (AS_SCRATCH_P (as))
6971 switch (GET_CODE (addr))
6972 {
6973 case REG:
6974 print_reg (file, addr);
6975 break;
6976
6977 case PLUS:
6978 reg = XEXP (addr, 0);
6979 offset = XEXP (addr, 1);
6980 print_reg (file, reg);
6981 if (GET_CODE (offset) == CONST_INT)
6982 fprintf (file, " offset:" HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
6983 else
6984 abort ();
6985 break;
6986
6987 default:
6988 debug_rtx (addr);
6989 abort ();
6990 }
6991 else if (AS_ANY_FLAT_P (as))
6992 {
6993 if (GET_CODE (addr) == REG)
6994 print_reg (file, addr);
6995 else
6996 print_reg (file, XEXP (addr, 0));
6997 }
6998 else if (AS_GLOBAL_P (as))
6999 {
7000 rtx base = addr;
7001 rtx vgpr_offset = NULL_RTX;
7002
7003 if (GET_CODE (addr) == PLUS)
7004 {
7005 base = XEXP (addr, 0);
7006
7007 if (GET_CODE (base) == PLUS)
7008 {
7009 /* (SGPR + VGPR) + CONST */
7010 vgpr_offset = XEXP (base, 1);
7011 base = XEXP (base, 0);
7012 }
7013 else
7014 {
7015 rtx offset = XEXP (addr, 1);
7016
7017 if (REG_P (offset))
7018 /* SGPR + VGPR */
7019 vgpr_offset = offset;
7020 else if (CONST_INT_P (offset))
7021 /* VGPR + CONST or SGPR + CONST */
7022 ;
7023 else
7024 output_operand_lossage ("bad ADDR_SPACE_GLOBAL address");
7025 }
7026 }
7027
7028 if (REG_P (base))
7029 {
7030 if (VGPR_REGNO_P (REGNO (base)))
7031 print_reg (file, base);
7032 else if (SGPR_REGNO_P (REGNO (base)))
7033 {
7034 /* The assembler requires a 64-bit VGPR pair here, even though
7035 the offset should be only 32-bit. */
7036 if (vgpr_offset == NULL_RTX)
7037 /* In this case, the vector offset is zero, so we use the first
7038 lane of v1, which is initialized to zero. */
7039 fprintf (file, "v1");
7040 else if (REG_P (vgpr_offset)
7041 && VGPR_REGNO_P (REGNO (vgpr_offset)))
7042 fprintf (file, "v%d", REGNO (vgpr_offset) - FIRST_VGPR_REG);
7043 else
7044 output_operand_lossage ("bad ADDR_SPACE_GLOBAL address");
7045 }
7046 }
7047 else
7048 output_operand_lossage ("bad ADDR_SPACE_GLOBAL address");
7049 }
7050 else if (AS_ANY_DS_P (as))
7051 switch (GET_CODE (addr))
7052 {
7053 case REG:
7054 print_reg (file, addr);
7055 break;
7056
7057 case PLUS:
7058 reg = XEXP (addr, 0);
7059 print_reg (file, reg);
7060 break;
7061
7062 default:
7063 debug_rtx (addr);
7064 abort ();
7065 }
7066 else
7067 switch (GET_CODE (addr))
7068 {
7069 case REG:
7070 print_reg (file, addr);
7071 fprintf (file, ", 0");
7072 break;
7073
7074 case PLUS:
7075 reg = XEXP (addr, 0);
7076 offset = XEXP (addr, 1);
7077 print_reg (file, reg);
7078 fprintf (file, ", ");
7079 if (GET_CODE (offset) == REG)
7080 print_reg (file, reg);
7081 else if (GET_CODE (offset) == CONST_INT)
7082 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7083 else
7084 abort ();
7085 break;
7086
7087 default:
7088 debug_rtx (addr);
7089 abort ();
7090 }
7091 }
7092
7093 /* Implement PRINT_OPERAND via gcn.h.
7094
7095 b - print operand size as untyped operand (b8/b16/b32/b64)
7096 B - print operand size as SI/DI untyped operand (b32/b32/b32/b64)
7097 i - print operand size as untyped operand (i16/b32/i64)
7098 I - print operand size as SI/DI untyped operand(i32/b32/i64)
7099 u - print operand size as untyped operand (u16/u32/u64)
7100 U - print operand size as SI/DI untyped operand(u32/u64)
7101 o - print operand size as memory access size for loads
7102 (ubyte/ushort/dword/dwordx2/wordx3/dwordx4)
7103 s - print operand size as memory access size for stores
7104 (byte/short/dword/dwordx2/wordx3/dwordx4)
7105 C - print conditional code for s_cbranch (_sccz/_sccnz/_vccz/_vccnz...)
7106 c - print inverse conditional code for s_cbranch
7107 D - print conditional code for s_cmp (eq_u64/lg_u64...)
7108 E - print conditional code for v_cmp (eq_u64/ne_u64...)
7109 A - print address in formatting suitable for given address space.
7110 O - print offset:n for data share operations.
7111 g - print "glc", if appropriate for given MEM
7112 L - print low-part of a multi-reg value
7113 H - print second part of a multi-reg value (high-part of 2-reg value)
7114 J - print third part of a multi-reg value
7115 K - print fourth part of a multi-reg value
7116 */
7117
7118 void
7119 print_operand (FILE *file, rtx x, int code)
7120 {
7121 rtx_code xcode = x ? GET_CODE (x) : UNKNOWN;
7122 bool invert = false;
7123 switch (code)
7124 {
7125 /* Instructions have the following suffixes.
7126 If there are two suffixes, the first is the destination type,
7127 and the second is the source type.
7128
7129 B32 Bitfield (untyped data) 32-bit
7130 B64 Bitfield (untyped data) 64-bit
7131 F16 floating-point 16-bit
7132 F32 floating-point 32-bit (IEEE 754 single-precision float)
7133 F64 floating-point 64-bit (IEEE 754 double-precision float)
7134 I16 signed 32-bit integer
7135 I32 signed 32-bit integer
7136 I64 signed 64-bit integer
7137 U16 unsigned 32-bit integer
7138 U32 unsigned 32-bit integer
7139 U64 unsigned 64-bit integer */
7140
7141 /* Print operand size as untyped suffix. */
7142 case 'b':
7143 {
7144 const char *s = "";
7145 machine_mode mode = GET_MODE (x);
7146 if (VECTOR_MODE_P (mode))
7147 mode = GET_MODE_INNER (mode);
7148 switch (GET_MODE_SIZE (mode))
7149 {
7150 case 1:
7151 s = "_b8";
7152 break;
7153 case 2:
7154 s = "_b16";
7155 break;
7156 case 4:
7157 s = "_b32";
7158 break;
7159 case 8:
7160 s = "_b64";
7161 break;
7162 default:
7163 output_operand_lossage ("invalid operand %%xn code");
7164 return;
7165 }
7166 fputs (s, file);
7167 }
7168 return;
7169 case 'B':
7170 {
7171 const char *s = "";
7172 machine_mode mode = GET_MODE (x);
7173 if (VECTOR_MODE_P (mode))
7174 mode = GET_MODE_INNER (mode);
7175 switch (GET_MODE_SIZE (mode))
7176 {
7177 case 1:
7178 case 2:
7179 case 4:
7180 s = "_b32";
7181 break;
7182 case 8:
7183 s = "_b64";
7184 break;
7185 default:
7186 output_operand_lossage ("invalid operand %%xn code");
7187 return;
7188 }
7189 fputs (s, file);
7190 }
7191 return;
7192 case 'e':
7193 fputs ("sext(", file);
7194 print_operand (file, x, 0);
7195 fputs (")", file);
7196 return;
7197 case 'i':
7198 case 'I':
7199 case 'u':
7200 case 'U':
7201 {
7202 bool signed_p = code == 'i';
7203 bool min32_p = code == 'I' || code == 'U';
7204 const char *s = "";
7205 machine_mode mode = GET_MODE (x);
7206 if (VECTOR_MODE_P (mode))
7207 mode = GET_MODE_INNER (mode);
7208 if (mode == VOIDmode)
7209 switch (GET_CODE (x))
7210 {
7211 case CONST_INT:
7212 s = signed_p ? "_i32" : "_u32";
7213 break;
7214 case CONST_DOUBLE:
7215 s = "_f64";
7216 break;
7217 default:
7218 output_operand_lossage ("invalid operand %%xn code");
7219 return;
7220 }
7221 else if (FLOAT_MODE_P (mode))
7222 switch (GET_MODE_SIZE (mode))
7223 {
7224 case 2:
7225 s = "_f16";
7226 break;
7227 case 4:
7228 s = "_f32";
7229 break;
7230 case 8:
7231 s = "_f64";
7232 break;
7233 default:
7234 output_operand_lossage ("invalid operand %%xn code");
7235 return;
7236 }
7237 else if (min32_p)
7238 switch (GET_MODE_SIZE (mode))
7239 {
7240 case 1:
7241 case 2:
7242 case 4:
7243 s = signed_p ? "_i32" : "_u32";
7244 break;
7245 case 8:
7246 s = signed_p ? "_i64" : "_u64";
7247 break;
7248 default:
7249 output_operand_lossage ("invalid operand %%xn code");
7250 return;
7251 }
7252 else
7253 switch (GET_MODE_SIZE (mode))
7254 {
7255 case 1:
7256 s = signed_p ? "_i8" : "_u8";
7257 break;
7258 case 2:
7259 s = signed_p ? "_i16" : "_u16";
7260 break;
7261 case 4:
7262 s = signed_p ? "_i32" : "_u32";
7263 break;
7264 case 8:
7265 s = signed_p ? "_i64" : "_u64";
7266 break;
7267 default:
7268 output_operand_lossage ("invalid operand %%xn code");
7269 return;
7270 }
7271 fputs (s, file);
7272 }
7273 return;
7274 /* Print operand size as untyped suffix. */
7275 case 'o':
7276 {
7277 const char *s = 0;
7278 machine_mode mode = GET_MODE (x);
7279 if (VECTOR_MODE_P (mode))
7280 mode = GET_MODE_INNER (mode);
7281
7282 switch (mode)
7283 {
7284 case E_QImode:
7285 s = "_ubyte";
7286 break;
7287 case E_HImode:
7288 case E_HFmode:
7289 s = "_ushort";
7290 break;
7291 default:
7292 break;
7293 }
7294
7295 if (s)
7296 {
7297 fputs (s, file);
7298 return;
7299 }
7300
7301 /* Fall-through - the other cases for 'o' are the same as for 's'. */
7302 gcc_fallthrough();
7303 }
7304 case 's':
7305 {
7306 const char *s;
7307 machine_mode mode = GET_MODE (x);
7308 if (VECTOR_MODE_P (mode))
7309 mode = GET_MODE_INNER (mode);
7310
7311 switch (mode)
7312 {
7313 case E_QImode:
7314 s = "_byte";
7315 break;
7316 case E_HImode:
7317 case E_HFmode:
7318 s = "_short";
7319 break;
7320 case E_SImode:
7321 case E_SFmode:
7322 s = "_dword";
7323 break;
7324 case E_DImode:
7325 case E_DFmode:
7326 s = "_dwordx2";
7327 break;
7328 case E_TImode:
7329 s = "_dwordx4";
7330 break;
7331 default:
7332 output_operand_lossage ("invalid operand %%xn code");
7333 return;
7334 }
7335 fputs (s, file);
7336 }
7337 return;
7338 case 'A':
7339 if (xcode != MEM)
7340 {
7341 output_operand_lossage ("invalid %%xn code");
7342 return;
7343 }
7344 print_operand_address (file, x);
7345 return;
7346 case 'O':
7347 {
7348 if (xcode != MEM)
7349 {
7350 output_operand_lossage ("invalid %%xn code");
7351 return;
7352 }
7353 if (AS_GDS_P (MEM_ADDR_SPACE (x)))
7354 fprintf (file, " gds");
7355
7356 rtx x0 = XEXP (x, 0);
7357 if (AS_GLOBAL_P (MEM_ADDR_SPACE (x)))
7358 {
7359 fprintf (file, ", ");
7360
7361 rtx base = x0;
7362 rtx const_offset = NULL_RTX;
7363
7364 if (GET_CODE (base) == PLUS)
7365 {
7366 rtx offset = XEXP (x0, 1);
7367 base = XEXP (x0, 0);
7368
7369 if (GET_CODE (base) == PLUS)
7370 /* (SGPR + VGPR) + CONST */
7371 /* Ignore the VGPR offset for this operand. */
7372 base = XEXP (base, 0);
7373
7374 if (CONST_INT_P (offset))
7375 const_offset = XEXP (x0, 1);
7376 else if (REG_P (offset))
7377 /* SGPR + VGPR */
7378 /* Ignore the VGPR offset for this operand. */
7379 ;
7380 else
7381 output_operand_lossage ("bad ADDR_SPACE_GLOBAL address");
7382 }
7383
7384 if (REG_P (base))
7385 {
7386 if (VGPR_REGNO_P (REGNO (base)))
7387 /* The VGPR address is specified in the %A operand. */
7388 fprintf (file, "off");
7389 else if (SGPR_REGNO_P (REGNO (base)))
7390 print_reg (file, base);
7391 else
7392 output_operand_lossage ("bad ADDR_SPACE_GLOBAL address");
7393 }
7394 else
7395 output_operand_lossage ("bad ADDR_SPACE_GLOBAL address");
7396
7397 if (const_offset != NULL_RTX)
7398 fprintf (file, " offset:" HOST_WIDE_INT_PRINT_DEC,
7399 INTVAL (const_offset));
7400
7401 return;
7402 }
7403
7404 if (GET_CODE (x0) == REG)
7405 return;
7406 if (GET_CODE (x0) != PLUS)
7407 {
7408 output_operand_lossage ("invalid %%xn code");
7409 return;
7410 }
7411 rtx val = XEXP (x0, 1);
7412 if (GET_CODE (val) == CONST_VECTOR)
7413 val = CONST_VECTOR_ELT (val, 0);
7414 if (GET_CODE (val) != CONST_INT)
7415 {
7416 output_operand_lossage ("invalid %%xn code");
7417 return;
7418 }
7419 fprintf (file, " offset:" HOST_WIDE_INT_PRINT_DEC, INTVAL (val));
7420
7421 }
7422 return;
7423 case 'c':
7424 invert = true;
7425 /* Fall through. */
7426 case 'C':
7427 {
7428 const char *s;
7429 bool num = false;
7430 if ((xcode != EQ && xcode != NE) || !REG_P (XEXP (x, 0)))
7431 {
7432 output_operand_lossage ("invalid %%xn code");
7433 return;
7434 }
7435 switch (REGNO (XEXP (x, 0)))
7436 {
7437 case VCC_REG:
7438 case VCCZ_REG:
7439 s = "_vcc";
7440 break;
7441 case SCC_REG:
7442 /* For some reason llvm-mc insists on scc0 instead of sccz. */
7443 num = true;
7444 s = "_scc";
7445 break;
7446 case EXECZ_REG:
7447 s = "_exec";
7448 break;
7449 default:
7450 output_operand_lossage ("invalid %%xn code");
7451 return;
7452 }
7453 fputs (s, file);
7454 if (xcode == (invert ? NE : EQ))
7455 fputc (num ? '0' : 'z', file);
7456 else
7457 fputs (num ? "1" : "nz", file);
7458 return;
7459 }
7460 case 'D':
7461 {
7462 const char *s;
7463 bool cmp_signed = false;
7464 switch (xcode)
7465 {
7466 case EQ:
7467 s = "_eq_";
7468 break;
7469 case NE:
7470 s = "_lg_";
7471 break;
7472 case LT:
7473 s = "_lt_";
7474 cmp_signed = true;
7475 break;
7476 case LE:
7477 s = "_le_";
7478 cmp_signed = true;
7479 break;
7480 case GT:
7481 s = "_gt_";
7482 cmp_signed = true;
7483 break;
7484 case GE:
7485 s = "_ge_";
7486 cmp_signed = true;
7487 break;
7488 case LTU:
7489 s = "_lt_";
7490 break;
7491 case LEU:
7492 s = "_le_";
7493 break;
7494 case GTU:
7495 s = "_gt_";
7496 break;
7497 case GEU:
7498 s = "_ge_";
7499 break;
7500 default:
7501 output_operand_lossage ("invalid %%xn code");
7502 return;
7503 }
7504 fputs (s, file);
7505 fputc (cmp_signed ? 'i' : 'u', file);
7506
7507 machine_mode mode = GET_MODE (XEXP (x, 0));
7508
7509 if (mode == VOIDmode)
7510 mode = GET_MODE (XEXP (x, 1));
7511
7512 /* If both sides are constants, then assume the instruction is in
7513 SImode since s_cmp can only do integer compares. */
7514 if (mode == VOIDmode)
7515 mode = SImode;
7516
7517 switch (GET_MODE_SIZE (mode))
7518 {
7519 case 4:
7520 s = "32";
7521 break;
7522 case 8:
7523 s = "64";
7524 break;
7525 default:
7526 output_operand_lossage ("invalid operand %%xn code");
7527 return;
7528 }
7529 fputs (s, file);
7530 return;
7531 }
7532 case 'E':
7533 {
7534 const char *s;
7535 bool cmp_signed = false;
7536 machine_mode mode = GET_MODE (XEXP (x, 0));
7537
7538 if (mode == VOIDmode)
7539 mode = GET_MODE (XEXP (x, 1));
7540
7541 /* If both sides are constants, assume the instruction is in SFmode
7542 if either operand is floating point, otherwise assume SImode. */
7543 if (mode == VOIDmode)
7544 {
7545 if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
7546 || GET_CODE (XEXP (x, 1)) == CONST_DOUBLE)
7547 mode = SFmode;
7548 else
7549 mode = SImode;
7550 }
7551
7552 /* Use the same format code for vector comparisons. */
7553 if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
7554 || GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
7555 mode = GET_MODE_INNER (mode);
7556
7557 bool float_p = GET_MODE_CLASS (mode) == MODE_FLOAT;
7558
7559 switch (xcode)
7560 {
7561 case EQ:
7562 s = "_eq_";
7563 break;
7564 case NE:
7565 s = float_p ? "_neq_" : "_ne_";
7566 break;
7567 case LT:
7568 s = "_lt_";
7569 cmp_signed = true;
7570 break;
7571 case LE:
7572 s = "_le_";
7573 cmp_signed = true;
7574 break;
7575 case GT:
7576 s = "_gt_";
7577 cmp_signed = true;
7578 break;
7579 case GE:
7580 s = "_ge_";
7581 cmp_signed = true;
7582 break;
7583 case LTU:
7584 s = "_lt_";
7585 break;
7586 case LEU:
7587 s = "_le_";
7588 break;
7589 case GTU:
7590 s = "_gt_";
7591 break;
7592 case GEU:
7593 s = "_ge_";
7594 break;
7595 case ORDERED:
7596 s = "_o_";
7597 break;
7598 case UNORDERED:
7599 s = "_u_";
7600 break;
7601 case UNEQ:
7602 s = "_nlg_";
7603 break;
7604 case UNGE:
7605 s = "_nlt_";
7606 break;
7607 case UNGT:
7608 s = "_nle_";
7609 break;
7610 case UNLE:
7611 s = "_ngt_";
7612 break;
7613 case UNLT:
7614 s = "_nge_";
7615 break;
7616 case LTGT:
7617 s = "_lg_";
7618 break;
7619 default:
7620 output_operand_lossage ("invalid %%xn code");
7621 return;
7622 }
7623 fputs (s, file);
7624 fputc (float_p ? 'f' : cmp_signed ? 'i' : 'u', file);
7625
7626 switch (GET_MODE_SIZE (mode))
7627 {
7628 case 1:
7629 output_operand_lossage ("operand %%xn code invalid for QImode");
7630 return;
7631 case 2:
7632 s = "16";
7633 break;
7634 case 4:
7635 s = "32";
7636 break;
7637 case 8:
7638 s = "64";
7639 break;
7640 default:
7641 output_operand_lossage ("invalid operand %%xn code");
7642 return;
7643 }
7644 fputs (s, file);
7645 return;
7646 }
7647 case 'L':
7648 print_operand (file, gcn_operand_part (GET_MODE (x), x, 0), 0);
7649 return;
7650 case 'H':
7651 print_operand (file, gcn_operand_part (GET_MODE (x), x, 1), 0);
7652 return;
7653 case 'J':
7654 print_operand (file, gcn_operand_part (GET_MODE (x), x, 2), 0);
7655 return;
7656 case 'K':
7657 print_operand (file, gcn_operand_part (GET_MODE (x), x, 3), 0);
7658 return;
7659 case 'R':
7660 /* Print a scalar register number as an integer. Temporary hack. */
7661 gcc_assert (REG_P (x));
7662 fprintf (file, "%u", (int) REGNO (x));
7663 return;
7664 case 'V':
7665 /* Print a vector register number as an integer. Temporary hack. */
7666 gcc_assert (REG_P (x));
7667 fprintf (file, "%u", (int) REGNO (x) - FIRST_VGPR_REG);
7668 return;
7669 case 0:
7670 if (xcode == REG)
7671 print_reg (file, x);
7672 else if (xcode == MEM)
7673 output_address (GET_MODE (x), x);
7674 else if (xcode == CONST_INT)
7675 fprintf (file, "%i", (int) INTVAL (x));
7676 else if (xcode == CONST_VECTOR)
7677 print_operand (file, CONST_VECTOR_ELT (x, 0), code);
7678 else if (xcode == CONST_DOUBLE)
7679 {
7680 const char *str;
7681 switch (gcn_inline_fp_constant_p (x, false))
7682 {
7683 case 240:
7684 str = "0.5";
7685 break;
7686 case 241:
7687 str = "-0.5";
7688 break;
7689 case 242:
7690 str = "1.0";
7691 break;
7692 case 243:
7693 str = "-1.0";
7694 break;
7695 case 244:
7696 str = "2.0";
7697 break;
7698 case 245:
7699 str = "-2.0";
7700 break;
7701 case 246:
7702 str = "4.0";
7703 break;
7704 case 247:
7705 str = "-4.0";
7706 break;
7707 case 248:
7708 str = "0.15915494";
7709 break;
7710 default:
7711 rtx ix = simplify_gen_subreg (GET_MODE (x) == DFmode
7712 ? DImode : SImode,
7713 x, GET_MODE (x), 0);
7714 if (x)
7715 print_operand (file, ix, code);
7716 else
7717 output_operand_lossage ("invalid fp constant");
7718 return;
7719 break;
7720 }
7721 fprintf (file, str);
7722 return;
7723 }
7724 else
7725 output_addr_const (file, x);
7726 return;
7727 case 'g':
7728 gcc_assert (xcode == MEM);
7729 if (MEM_VOLATILE_P (x))
7730 fputs (" glc", file);
7731 return;
7732 default:
7733 output_operand_lossage ("invalid %%xn code");
7734 }
7735 gcc_unreachable ();
7736 }
7737
7738 /* Implement DEBUGGER_REGNO macro.
7739
7740 Return the DWARF register number that corresponds to the GCC internal
7741 REGNO. */
7742
7743 unsigned int
7744 gcn_dwarf_register_number (unsigned int regno)
7745 {
7746 /* Registers defined in DWARF. */
7747 if (regno == EXEC_LO_REG)
7748 return 17;
7749 /* We need to use a more complex DWARF expression for this
7750 else if (regno == EXEC_HI_REG)
7751 return 17; */
7752 else if (regno == VCC_LO_REG)
7753 return 768;
7754 /* We need to use a more complex DWARF expression for this
7755 else if (regno == VCC_HI_REG)
7756 return 768; */
7757 else if (regno == SCC_REG)
7758 return 128;
7759 else if (regno == DWARF_LINK_REGISTER)
7760 return 16;
7761 else if (SGPR_REGNO_P (regno))
7762 {
7763 if (regno - FIRST_SGPR_REG < 64)
7764 return (regno - FIRST_SGPR_REG + 32);
7765 else
7766 return (regno - FIRST_SGPR_REG + 1024);
7767 }
7768 else if (VGPR_REGNO_P (regno))
7769 return (regno - FIRST_VGPR_REG + 2560);
7770 else if (AVGPR_REGNO_P (regno))
7771 return (regno - FIRST_AVGPR_REG + 3072);
7772
7773 /* Otherwise, there's nothing sensible to do. */
7774 return regno + 100000;
7775 }
7776
7777 /* Implement TARGET_DWARF_REGISTER_SPAN.
7778
7779 DImode and Vector DImode require additional registers. */
7780
7781 static rtx
7782 gcn_dwarf_register_span (rtx rtl)
7783 {
7784 machine_mode mode = GET_MODE (rtl);
7785
7786 if (VECTOR_MODE_P (mode))
7787 mode = GET_MODE_INNER (mode);
7788
7789 if (GET_MODE_SIZE (mode) != 8)
7790 return NULL_RTX;
7791
7792 unsigned regno = REGNO (rtl);
7793
7794 if (regno == DWARF_LINK_REGISTER)
7795 return NULL_RTX;
7796
7797 rtx p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (2));
7798 XVECEXP (p, 0, 0) = gen_rtx_REG (SImode, regno);
7799 XVECEXP (p, 0, 1) = gen_rtx_REG (SImode, regno + 1);
7800
7801 return p;
7802 }
7803
7804 /* }}} */
7805 /* {{{ TARGET hook overrides. */
7806
7807 #undef TARGET_ADDR_SPACE_ADDRESS_MODE
7808 #define TARGET_ADDR_SPACE_ADDRESS_MODE gcn_addr_space_address_mode
7809 #undef TARGET_ADDR_SPACE_DEBUG
7810 #define TARGET_ADDR_SPACE_DEBUG gcn_addr_space_debug
7811 #undef TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P
7812 #define TARGET_ADDR_SPACE_LEGITIMATE_ADDRESS_P \
7813 gcn_addr_space_legitimate_address_p
7814 #undef TARGET_ADDR_SPACE_LEGITIMIZE_ADDRESS
7815 #define TARGET_ADDR_SPACE_LEGITIMIZE_ADDRESS gcn_addr_space_legitimize_address
7816 #undef TARGET_ADDR_SPACE_POINTER_MODE
7817 #define TARGET_ADDR_SPACE_POINTER_MODE gcn_addr_space_pointer_mode
7818 #undef TARGET_ADDR_SPACE_SUBSET_P
7819 #define TARGET_ADDR_SPACE_SUBSET_P gcn_addr_space_subset_p
7820 #undef TARGET_ADDR_SPACE_CONVERT
7821 #define TARGET_ADDR_SPACE_CONVERT gcn_addr_space_convert
7822 #undef TARGET_ARG_PARTIAL_BYTES
7823 #define TARGET_ARG_PARTIAL_BYTES gcn_arg_partial_bytes
7824 #undef TARGET_ASM_ALIGNED_DI_OP
7825 #define TARGET_ASM_ALIGNED_DI_OP "\t.8byte\t"
7826 #undef TARGET_ASM_INIT_SECTIONS
7827 #define TARGET_ASM_INIT_SECTIONS gcn_asm_init_sections
7828 #undef TARGET_ASM_FILE_START
7829 #define TARGET_ASM_FILE_START output_file_start
7830 #undef TARGET_ASM_FUNCTION_PROLOGUE
7831 #define TARGET_ASM_FUNCTION_PROLOGUE gcn_target_asm_function_prologue
7832 #undef TARGET_ASM_SELECT_SECTION
7833 #define TARGET_ASM_SELECT_SECTION gcn_asm_select_section
7834 #undef TARGET_ASM_TRAMPOLINE_TEMPLATE
7835 #define TARGET_ASM_TRAMPOLINE_TEMPLATE gcn_asm_trampoline_template
7836 #undef TARGET_ATTRIBUTE_TABLE
7837 #define TARGET_ATTRIBUTE_TABLE gcn_attribute_table
7838 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_MODES
7839 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_MODES \
7840 gcn_autovectorize_vector_modes
7841 #undef TARGET_BUILTIN_DECL
7842 #define TARGET_BUILTIN_DECL gcn_builtin_decl
7843 #undef TARGET_CAN_CHANGE_MODE_CLASS
7844 #define TARGET_CAN_CHANGE_MODE_CLASS gcn_can_change_mode_class
7845 #undef TARGET_CAN_ELIMINATE
7846 #define TARGET_CAN_ELIMINATE gcn_can_eliminate_p
7847 #undef TARGET_CANNOT_COPY_INSN_P
7848 #define TARGET_CANNOT_COPY_INSN_P gcn_cannot_copy_insn_p
7849 #undef TARGET_CLASS_LIKELY_SPILLED_P
7850 #define TARGET_CLASS_LIKELY_SPILLED_P gcn_class_likely_spilled_p
7851 #undef TARGET_CLASS_MAX_NREGS
7852 #define TARGET_CLASS_MAX_NREGS gcn_class_max_nregs
7853 #undef TARGET_CONDITIONAL_REGISTER_USAGE
7854 #define TARGET_CONDITIONAL_REGISTER_USAGE gcn_conditional_register_usage
7855 #undef TARGET_CONSTANT_ALIGNMENT
7856 #define TARGET_CONSTANT_ALIGNMENT gcn_constant_alignment
7857 #undef TARGET_DEBUG_UNWIND_INFO
7858 #define TARGET_DEBUG_UNWIND_INFO gcn_debug_unwind_info
7859 #undef TARGET_DWARF_REGISTER_SPAN
7860 #define TARGET_DWARF_REGISTER_SPAN gcn_dwarf_register_span
7861 #undef TARGET_EMUTLS_VAR_INIT
7862 #define TARGET_EMUTLS_VAR_INIT gcn_emutls_var_init
7863 #undef TARGET_EXPAND_BUILTIN
7864 #define TARGET_EXPAND_BUILTIN gcn_expand_builtin
7865 #undef TARGET_EXPAND_DIVMOD_LIBFUNC
7866 #define TARGET_EXPAND_DIVMOD_LIBFUNC gcn_expand_divmod_libfunc
7867 #undef TARGET_FRAME_POINTER_REQUIRED
7868 #define TARGET_FRAME_POINTER_REQUIRED gcn_frame_pointer_rqd
7869 #undef TARGET_FUNCTION_ARG
7870 #undef TARGET_FUNCTION_ARG_ADVANCE
7871 #define TARGET_FUNCTION_ARG_ADVANCE gcn_function_arg_advance
7872 #define TARGET_FUNCTION_ARG gcn_function_arg
7873 #undef TARGET_FUNCTION_VALUE
7874 #define TARGET_FUNCTION_VALUE gcn_function_value
7875 #undef TARGET_FUNCTION_VALUE_REGNO_P
7876 #define TARGET_FUNCTION_VALUE_REGNO_P gcn_function_value_regno_p
7877 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
7878 #define TARGET_GIMPLIFY_VA_ARG_EXPR gcn_gimplify_va_arg_expr
7879 #undef TARGET_OMP_DEVICE_KIND_ARCH_ISA
7880 #define TARGET_OMP_DEVICE_KIND_ARCH_ISA gcn_omp_device_kind_arch_isa
7881 #undef TARGET_GOACC_ADJUST_PRIVATE_DECL
7882 #define TARGET_GOACC_ADJUST_PRIVATE_DECL gcn_goacc_adjust_private_decl
7883 #undef TARGET_GOACC_CREATE_WORKER_BROADCAST_RECORD
7884 #define TARGET_GOACC_CREATE_WORKER_BROADCAST_RECORD \
7885 gcn_goacc_create_worker_broadcast_record
7886 #undef TARGET_GOACC_FORK_JOIN
7887 #define TARGET_GOACC_FORK_JOIN gcn_fork_join
7888 #undef TARGET_GOACC_REDUCTION
7889 #define TARGET_GOACC_REDUCTION gcn_goacc_reduction
7890 #undef TARGET_GOACC_VALIDATE_DIMS
7891 #define TARGET_GOACC_VALIDATE_DIMS gcn_goacc_validate_dims
7892 #undef TARGET_GOACC_SHARED_MEM_LAYOUT
7893 #define TARGET_GOACC_SHARED_MEM_LAYOUT gcn_shared_mem_layout
7894 #undef TARGET_HARD_REGNO_MODE_OK
7895 #define TARGET_HARD_REGNO_MODE_OK gcn_hard_regno_mode_ok
7896 #undef TARGET_HARD_REGNO_NREGS
7897 #define TARGET_HARD_REGNO_NREGS gcn_hard_regno_nregs
7898 #undef TARGET_HAVE_SPECULATION_SAFE_VALUE
7899 #define TARGET_HAVE_SPECULATION_SAFE_VALUE speculation_safe_value_not_needed
7900 #undef TARGET_INIT_BUILTINS
7901 #define TARGET_INIT_BUILTINS gcn_init_builtins
7902 #undef TARGET_INIT_LIBFUNCS
7903 #define TARGET_INIT_LIBFUNCS gcn_init_libfuncs
7904 #undef TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS
7905 #define TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS \
7906 gcn_ira_change_pseudo_allocno_class
7907 #undef TARGET_LEGITIMATE_CONSTANT_P
7908 #define TARGET_LEGITIMATE_CONSTANT_P gcn_legitimate_constant_p
7909 #undef TARGET_LIBC_HAS_FUNCTION
7910 #define TARGET_LIBC_HAS_FUNCTION gcn_libc_has_function
7911 #undef TARGET_LRA_P
7912 #define TARGET_LRA_P hook_bool_void_true
7913 #undef TARGET_MACHINE_DEPENDENT_REORG
7914 #define TARGET_MACHINE_DEPENDENT_REORG gcn_md_reorg
7915 #undef TARGET_MEMORY_MOVE_COST
7916 #define TARGET_MEMORY_MOVE_COST gcn_memory_move_cost
7917 #undef TARGET_MODES_TIEABLE_P
7918 #define TARGET_MODES_TIEABLE_P gcn_modes_tieable_p
7919 #undef TARGET_OPTION_OVERRIDE
7920 #define TARGET_OPTION_OVERRIDE gcn_option_override
7921 #undef TARGET_PRETEND_OUTGOING_VARARGS_NAMED
7922 #define TARGET_PRETEND_OUTGOING_VARARGS_NAMED \
7923 gcn_pretend_outgoing_varargs_named
7924 #undef TARGET_PROMOTE_FUNCTION_MODE
7925 #define TARGET_PROMOTE_FUNCTION_MODE gcn_promote_function_mode
7926 #undef TARGET_REGISTER_MOVE_COST
7927 #define TARGET_REGISTER_MOVE_COST gcn_register_move_cost
7928 #undef TARGET_RETURN_IN_MEMORY
7929 #define TARGET_RETURN_IN_MEMORY gcn_return_in_memory
7930 #undef TARGET_RTX_COSTS
7931 #define TARGET_RTX_COSTS gcn_rtx_costs
7932 #undef TARGET_SECONDARY_RELOAD
7933 #define TARGET_SECONDARY_RELOAD gcn_secondary_reload
7934 #undef TARGET_SECTION_TYPE_FLAGS
7935 #define TARGET_SECTION_TYPE_FLAGS gcn_section_type_flags
7936 #undef TARGET_SCALAR_MODE_SUPPORTED_P
7937 #define TARGET_SCALAR_MODE_SUPPORTED_P gcn_scalar_mode_supported_p
7938 #undef TARGET_SIMD_CLONE_ADJUST
7939 #define TARGET_SIMD_CLONE_ADJUST gcn_simd_clone_adjust
7940 #undef TARGET_SIMD_CLONE_COMPUTE_VECSIZE_AND_SIMDLEN
7941 #define TARGET_SIMD_CLONE_COMPUTE_VECSIZE_AND_SIMDLEN \
7942 gcn_simd_clone_compute_vecsize_and_simdlen
7943 #undef TARGET_SIMD_CLONE_USABLE
7944 #define TARGET_SIMD_CLONE_USABLE gcn_simd_clone_usable
7945 #undef TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P
7946 #define TARGET_SMALL_REGISTER_CLASSES_FOR_MODE_P \
7947 gcn_small_register_classes_for_mode_p
7948 #undef TARGET_SPILL_CLASS
7949 #define TARGET_SPILL_CLASS gcn_spill_class
7950 #undef TARGET_STRICT_ARGUMENT_NAMING
7951 #define TARGET_STRICT_ARGUMENT_NAMING gcn_strict_argument_naming
7952 #undef TARGET_TRAMPOLINE_INIT
7953 #define TARGET_TRAMPOLINE_INIT gcn_trampoline_init
7954 #undef TARGET_TRULY_NOOP_TRUNCATION
7955 #define TARGET_TRULY_NOOP_TRUNCATION gcn_truly_noop_truncation
7956 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
7957 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST gcn_vectorization_cost
7958 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
7959 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
7960 gcn_vectorize_builtin_vectorized_function
7961 #undef TARGET_VECTORIZE_GET_MASK_MODE
7962 #define TARGET_VECTORIZE_GET_MASK_MODE gcn_vectorize_get_mask_mode
7963 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
7964 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE gcn_vectorize_preferred_simd_mode
7965 #undef TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT
7966 #define TARGET_VECTORIZE_PREFERRED_VECTOR_ALIGNMENT \
7967 gcn_preferred_vector_alignment
7968 #undef TARGET_VECTORIZE_RELATED_MODE
7969 #define TARGET_VECTORIZE_RELATED_MODE gcn_related_vector_mode
7970 #undef TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT
7971 #define TARGET_VECTORIZE_SUPPORT_VECTOR_MISALIGNMENT \
7972 gcn_vectorize_support_vector_misalignment
7973 #undef TARGET_VECTORIZE_VEC_PERM_CONST
7974 #define TARGET_VECTORIZE_VEC_PERM_CONST gcn_vectorize_vec_perm_const
7975 #undef TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE
7976 #define TARGET_VECTORIZE_VECTOR_ALIGNMENT_REACHABLE \
7977 gcn_vector_alignment_reachable
7978 #undef TARGET_VECTOR_MODE_SUPPORTED_P
7979 #define TARGET_VECTOR_MODE_SUPPORTED_P gcn_vector_mode_supported_p
7980
7981 #undef TARGET_DOCUMENTATION_NAME
7982 #define TARGET_DOCUMENTATION_NAME "AMD GCN"
7983
7984 struct gcc_target targetm = TARGET_INITIALIZER;
7985
7986 #include "gt-gcn.h"
7987 /* }}} */