Debian Codegraph @

gcc/config/gcn/gcn.md

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 ;;- See file "rtl.def" for documentation on define_insn, match_*, et. al.
18
19 (include "predicates.md")
20 (include "constraints.md")
21
22 ;; {{{ Constants and enums
23
24 ; Named registers
25 (define_constants
26 [(CC_SAVE_REG 22)
27 (FLAT_SCRATCH_REG 102)
28 (FLAT_SCRATCH_LO_REG 102)
29 (FLAT_SCRATCH_HI_REG 103)
30 (XNACK_MASK_REG 104)
31 (XNACK_MASK_LO_REG 104)
32 (XNACK_MASK_HI_REG 105)
33 (VCC_REG 106)
34 (VCC_LO_REG 106)
35 (VCC_HI_REG 107)
36 (VCCZ_REG 108)
37 (TBA_REG 109)
38 (TBA_LO_REG 109)
39 (TBA_HI_REG 110)
40 (TMA_REG 111)
41 (TMA_LO_REG 111)
42 (TMA_HI_REG 112)
43 (TTMP0_REG 113)
44 (TTMP11_REG 124)
45 (M0_REG 125)
46 (EXEC_REG 126)
47 (EXEC_LO_REG 126)
48 (EXEC_HI_REG 127)
49 (EXECZ_REG 128)
50 (SCC_REG 129)])
51
52 (define_constants
53 [(SP_REGNUM 16)
54 (LR_REGNUM 18)
55 (AP_REGNUM 672)
56 (FP_REGNUM 674)])
57
58 (define_c_enum "unspecv" [
59 UNSPECV_PROLOGUE_USE
60 UNSPECV_KERNEL_RETURN
61 UNSPECV_BARRIER
62 UNSPECV_ATOMIC
63 UNSPECV_ICACHE_INV])
64
65 (define_c_enum "unspec" [
66 UNSPEC_ADDPTR
67 UNSPEC_VECTOR
68 UNSPEC_BPERMUTE
69 UNSPEC_SGPRBASE
70 UNSPEC_MEMORY_BARRIER
71 UNSPEC_SMIN_DPP_SHR UNSPEC_SMAX_DPP_SHR
72 UNSPEC_UMIN_DPP_SHR UNSPEC_UMAX_DPP_SHR
73 UNSPEC_PLUS_DPP_SHR
74 UNSPEC_PLUS_CARRY_DPP_SHR UNSPEC_PLUS_CARRY_IN_DPP_SHR
75 UNSPEC_AND_DPP_SHR UNSPEC_IOR_DPP_SHR UNSPEC_XOR_DPP_SHR
76 UNSPEC_MOV_DPP_SHR
77 UNSPEC_MOV_DPP_SWAP_PAIRS
78 UNSPEC_MOV_DPP_DISTRIBUTE_EVEN
79 UNSPEC_MOV_DPP_DISTRIBUTE_ODD
80 UNSPEC_CMUL UNSPEC_CMUL_CONJ
81 UNSPEC_CMUL_ADD UNSPEC_CMUL_SUB
82 UNSPEC_CADD90
83 UNSPEC_CADD270
84 UNSPEC_GATHER
85 UNSPEC_SCATTER
86 UNSPEC_RCP
87 UNSPEC_FLBIT_INT
88 UNSPEC_FLOOR UNSPEC_CEIL UNSPEC_SIN UNSPEC_COS UNSPEC_EXP2 UNSPEC_LOG2
89 UNSPEC_LDEXP UNSPEC_FREXP_EXP UNSPEC_FREXP_MANT
90 UNSPEC_DIV_SCALE UNSPEC_DIV_FMAS UNSPEC_DIV_FIXUP])
91
92 ;; }}}
93 ;; {{{ Attributes
94
95 ; Instruction type (encoding) as described in the ISA specification.
96 ; The following table summarizes possible operands of individual instruction
97 ; types and corresponding constraints.
98 ;
99 ; sop2 - scalar, two inputs, one output
100 ; ssrc0/ssrc1: sgpr 0-102; flat_scratch,xnack,vcc,tba,tma,ttmp0-11,exec
101 ; vccz,execz,scc,inline immedate,fp inline immediate
102 ; sdst: sgpr 0-102; flat_scratch,xnack,vcc,tba,tma,ttmp0-11,exec
103 ;
104 ; Constraints "=SD, SD", "SSA,SSB","SSB,SSA"
105 ;
106 ; sopk - scalar, inline constant input, one output
107 ; simm16: 16bit inline constant
108 ; sdst: same as sop2/ssrc0
109 ;
110 ; Constraints "=SD", "J"
111 ;
112 ; sop1 - scalar, one input, one output
113 ; ssrc0: same as sop2/ssrc0. FIXME: manual omit VCCZ
114 ; sdst: same as sop2/sdst
115 ;
116 ; Constraints "=SD", "SSA"
117 ;
118 ; sopc - scalar, two inputs, one comparsion
119 ; ssrc0: same as sop2/ssc0.
120 ;
121 ; Constraints "SSI,SSA","SSA,SSI"
122 ;
123 ; sopp - scalar, one constant input, one special
124 ; simm16
125 ;
126 ; smem - scalar memory
127 ; sbase: aligned pair of sgprs. Specify {size[15:0], base[47:0]} in
128 ; dwords
129 ; sdata: sgpr0-102, flat_scratch, xnack, vcc, tba, tma
130 ; offset: sgpr or 20bit unsigned byte offset
131 ;
132 ; vop2 - vector, two inputs, one output
133 ; vsrc0: sgpr0-102,flat_scratch,xnack,vcc,tba,ttmp0-11,m0,exec,
134 ; inline constant -16 to -64, fp inline immediate, vccz, execz,
135 ; scc, lds, literal constant, vgpr0-255
136 ; vsrc1: vgpr0-255
137 ; vdst: vgpr0-255
138 ; Limitations: At most one SGPR, at most one constant
139 ; if constant is used, SGPR must be M0
140 ; Only SRC0 can be LDS_DIRECT
141 ;
142 ; constraints: "=v", "vBSv", "v"
143 ;
144 ; vop1 - vector, one input, one output
145 ; vsrc0: same as vop2/src0
146 ; vdst: vgpr0-255
147 ;
148 ; constraints: "=v", "vBSv"
149 ;
150 ; vopc - vector, two inputs, one comparsion output;
151 ; vsrc0: same as vop2/src0
152 ; vsrc1: vgpr0-255
153 ; vdst:
154 ;
155 ; constraints: "vASv", "v"
156 ;
157 ; vop3a - vector, three inputs, one output
158 ; vdst: vgpr0-255, for v_cmp sgpr or vcc
159 ; abs,clamp
160 ; vsrc0: sgpr0-102,vcc,tba,ttmp0-11,m0,exec,
161 ; inline constant -16 to -64, fp inline immediate, vccz, execz,
162 ; scc, lds_direct
163 ; FIXME: really missing 1/pi? really 104 SGPRs
164 ;
165 ; vop3b - vector, three inputs, one vector output, one scalar output
166 ; vsrc0,vsrc1,vsrc2: same as vop3a vsrc0
167 ; vdst: vgpr0-255
168 ; sdst: sgpr0-103/vcc/tba/tma/ttmp0-11
169 ;
170 ; vop3p_mai - vector, three inputs, one vector output
171 ; vsrc0,vsrc1,vsrc2: inline constant -16 to -64, fp inline immediate,
172 ; (acc or arch) vgpr0-255
173 ; vdst: (acc or arch) vgpr0-255
174 ;
175 ; vop_sdwa - second dword for vop1/vop2/vopc for specifying sub-dword address
176 ; src0: vgpr0-255
177 ; dst_sel: BYTE_0-3, WORD_0-1, DWORD
178 ; dst_unused: UNUSED_PAD, UNUSED_SEXT, UNUSED_PRESERVE
179 ; clamp: true/false
180 ; src0_sel: BYTE_0-3, WORD_0-1, DWORD
181 ; flags: src0_sext, src0_neg, src0_abs, src1_sel, src1_sext, src1_neg,
182 ; src1_abs
183 ;
184 ; vop_dpp - second dword for vop1/vop2/vopc for specifying data-parallel ops
185 ; src0: vgpr0-255
186 ; dpp_ctrl: quad_perm, row_sl0-15, row_sr0-15, row_rr0-15, wf_sl1,
187 ; wf_rl1, wf_sr1, wf_rr1, row_mirror, row_half_mirror,
188 ; bcast15, bcast31
189 ; flags: src0_neg, src0_abs, src1_neg, src1_abs
190 ; bank_mask: 4-bit mask
191 ; row_mask: 4-bit mask
192 ;
193 ; ds - Local and global data share instructions.
194 ; offset0: 8-bit constant
195 ; offset1: 8-bit constant
196 ; flag: gds
197 ; addr: vgpr0-255
198 ; data0: vgpr0-255
199 ; data1: vgpr0-255
200 ; vdst: vgpr0-255
201 ;
202 ; mubuf - Untyped memory buffer operation. First word with LDS, second word
203 ; non-LDS.
204 ; offset: 12-bit constant
205 ; vaddr: vgpr0-255
206 ; vdata: vgpr0-255
207 ; srsrc: sgpr0-102
208 ; soffset: sgpr0-102
209 ; flags: offen, idxen, glc, lds, slc, tfe
210 ;
211 ; mtbuf - Typed memory buffer operation. Two words
212 ; offset: 12-bit constant
213 ; dfmt: 4-bit constant
214 ; nfmt: 3-bit constant
215 ; vaddr: vgpr0-255
216 ; vdata: vgpr0-255
217 ; srsrc: sgpr0-102
218 ; soffset: sgpr0-102
219 ; flags: offen, idxen, glc, lds, slc, tfe
220 ;
221 ; flat - flat or global memory operations
222 ; flags: glc, slc
223 ; addr: vgpr0-255
224 ; data: vgpr0-255
225 ; vdst: vgpr0-255
226 ;
227 ; mult - expands to multiple instructions (pseudo encoding)
228 ;
229 ; vmult - as mult, when a vector instruction is used.
230
231 (define_attr "type"
232 "unknown,sop1,sop2,sopk,sopc,sopp,smem,ds,vop2,vop1,vopc,
233 vop3a,vop3b,vop3p_mai,vop_sdwa,vop_dpp,mubuf,mtbuf,flat,mult,
234 vmult"
235 (const_string "unknown"))
236
237 ; Set if instruction is executed in scalar or vector unit
238
239 (define_attr "unit" "unknown,scalar,vector"
240 (cond [(eq_attr "type" "sop1,sop2,sopk,sopc,sopp,smem,mult")
241 (const_string "scalar")
242 (eq_attr "type" "vop2,vop1,vopc,vop3a,vop3b,ds,vop3p_mai,
243 vop_sdwa,vop_dpp,flat,vmult")
244 (const_string "vector")]
245 (const_string "unknown")))
246
247 ; All vector instructions run as 64 threads as predicated by the EXEC
248 ; register. Scalar operations in vector register require a single lane
249 ; enabled, vector moves require a full set of lanes enabled, and most vector
250 ; operations handle the lane masking themselves.
251 ; The md_reorg pass is responsible for ensuring that EXEC is set appropriately
252 ; according to the following settings:
253 ; auto - md_reorg will inspect def/use to determine what to do.
254 ; none - exec is not needed.
255 ; single - disable all but lane zero.
256 ; full - enable all lanes.
257
258 (define_attr "exec" "auto,none,single,full"
259 (const_string "auto"))
260
261 ; Infer the (worst-case) length from the instruction type by default. Many
262 ; types can have an optional immediate word following, which we include here.
263 ; "Multiple" types are counted as two 64-bit instructions. This is just a
264 ; default fallback: it can be overridden per-alternative in insn patterns for
265 ; greater accuracy.
266
267 (define_attr "length" ""
268 (cond [(eq_attr "type" "sop1") (const_int 8)
269 (eq_attr "type" "sop2") (const_int 8)
270 (eq_attr "type" "sopk") (const_int 8)
271 (eq_attr "type" "sopc") (const_int 8)
272 (eq_attr "type" "sopp") (const_int 4)
273 (eq_attr "type" "smem") (const_int 8)
274 (eq_attr "type" "ds") (const_int 8)
275 (eq_attr "type" "vop1") (const_int 8)
276 (eq_attr "type" "vop2") (const_int 8)
277 (eq_attr "type" "vopc") (const_int 8)
278 (eq_attr "type" "vop3a") (const_int 8)
279 (eq_attr "type" "vop3b") (const_int 8)
280 (eq_attr "type" "vop_sdwa") (const_int 8)
281 (eq_attr "type" "vop_dpp") (const_int 8)
282 (eq_attr "type" "flat") (const_int 8)
283 (eq_attr "type" "mult") (const_int 16)
284 (eq_attr "type" "vmult") (const_int 16)]
285 (const_int 4)))
286
287 ; Disable alternatives that only apply to specific ISA variants.
288
289 (define_attr "cdna" "any,cdna2" (const_string "any"))
290 (define_attr "rdna" "any,no,yes" (const_string "any"))
291
292 (define_attr "xnack" "na,off,on" (const_string "na"))
293
294 (define_attr "enabled" ""
295 (cond [(and (eq_attr "rdna" "no")
296 (ne (symbol_ref "TARGET_RDNA2_PLUS") (const_int 0)))
297 (const_int 0)
298 (and (eq_attr "rdna" "yes")
299 (eq (symbol_ref "TARGET_RDNA2_PLUS") (const_int 0)))
300 (const_int 0)
301 (and (eq_attr "cdna" "cdna2")
302 (eq (symbol_ref "TARGET_CDNA2_PLUS") (const_int 0)))
303 (const_int 0)
304 (and (eq_attr "xnack" "off")
305 (ne (symbol_ref "TARGET_XNACK") (const_int 0)))
306 (const_int 0)
307 (and (eq_attr "xnack" "on")
308 (eq (symbol_ref "TARGET_XNACK") (const_int 0)))
309 (const_int 0)]
310 (const_int 1)))
311
312 ; We need to be able to identify v_readlane and v_writelane with
313 ; SGPR lane selection in order to handle "Manually Inserted Wait States".
314
315 (define_attr "laneselect" "yes,no" (const_string "no"))
316
317 ; Identify instructions that require a "Manually Inserted Wait State" if
318 ; their inputs are overwritten by subsequent instructions.
319
320 (define_attr "delayeduse" "yes,no" (const_string "no"))
321
322 ; Identify instructions that require "Manually Inserted Wait State" if
323 ; a previous instruction writes to VCC. The number gives the number of NOPs.
324
325 (define_attr "vccwait" "" (const_int 0))
326
327 ;; }}}
328 ;; {{{ Iterators useful across the wole machine description
329
330 (define_mode_iterator SIDI [SI DI])
331 (define_mode_iterator SFDF [SF DF])
332 (define_mode_iterator SISF [SI SF])
333 (define_mode_iterator QIHI [QI HI])
334 (define_mode_iterator DIDF [DI DF])
335 (define_mode_iterator FP [HF SF DF])
336 (define_mode_iterator FP_1REG [HF SF])
337
338 ;; }}}
339 ;; {{{ Attributes.
340
341 ; Translate RTX code into GCN instruction mnemonics with and without
342 ; suffixes such as _b32, etc.
343
344 (define_code_attr mnemonic
345 [(minus "sub%i")
346 (plus "add%i")
347 (ashift "lshl%b")
348 (lshiftrt "lshr%b")
349 (ashiftrt "ashr%i")
350 (and "and%B")
351 (ior "or%B")
352 (xor "xor%B")
353 (mult "mul%i")
354 (smin "min%i")
355 (smax "max%i")
356 (umin "min%u")
357 (umax "max%u")
358 (not "not%B")
359 (popcount "bcnt_u32%b")])
360
361 (define_code_attr bare_mnemonic
362 [(plus "add")
363 (minus "sub")
364 (and "and")
365 (ior "or")
366 (xor "xor")])
367
368 (define_code_attr s_mnemonic
369 [(not "not%b")
370 (popcount "bcnt1_i32%b")
371 (clz "flbit_i32%b")
372 (ctz "ff1_i32%b")
373 (clrsb "flbit_i32%i")])
374
375 (define_code_attr revmnemonic
376 [(minus "subrev%i")
377 (ashift "lshlrev%b")
378 (lshiftrt "lshrrev%b")
379 (ashiftrt "ashrrev%i")])
380
381 ; Translate RTX code into corresponding expander name.
382
383 (define_code_attr expander
384 [(and "and")
385 (ior "ior")
386 (xor "xor")
387 (plus "add")
388 (minus "sub")
389 (ashift "ashl")
390 (lshiftrt "lshr")
391 (ashiftrt "ashr")
392 (mult "mul")
393 (smin "smin")
394 (smax "smax")
395 (umin "umin")
396 (umax "umax")
397 (not "one_cmpl")
398 (popcount "popcount")
399 (clz "clz")
400 (ctz "ctz")
401 (sign_extend "extend")
402 (zero_extend "zero_extend")])
403
404 (define_code_attr fexpander
405 [(smin "fmin")
406 (smax "fmax")])
407
408 ;; }}}
409 ;; {{{ Miscellaneous instructions
410
411 (define_insn "nop"
412 [(const_int 0)]
413 ""
414 "s_nop\t0x0"
415 [(set_attr "type" "sopp")])
416
417 ; FIXME: What should the value of the immediate be? Zero is disallowed, so
418 ; pick 1 for now.
419 (define_insn "trap"
420 [(trap_if (const_int 1) (const_int 0))]
421 ""
422 "s_trap\t1"
423 [(set_attr "type" "sopp")])
424
425 ;; }}}
426 ;; {{{ Moves
427
428 ;; All scalar modes we support moves in.
429 (define_mode_iterator MOV_MODE [BI QI HI SI DI TI SF DF])
430
431 ; This is the entry point for creating all kinds of scalar moves,
432 ; including reloads and symbols.
433
434 (define_expand "mov<mode>"
435 [(set (match_operand:MOV_MODE 0 "nonimmediate_operand")
436 (match_operand:MOV_MODE 1 "general_operand"))]
437 ""
438 {
439 if (SUBREG_P (operands[1])
440 && GET_MODE (operands[1]) == SImode
441 && GET_MODE (SUBREG_REG (operands[1])) == BImode)
442 {
443 /* (reg:BI VCC) has nregs==2 to ensure it gets clobbered as a whole,
444 but (subreg:SI (reg:BI VCC)) doesn't, which causes the LRA liveness
445 checks to assert. Transform this:
446 (set (reg:SI) (subreg:SI (reg:BI)))
447 to this:
448 (set (subreg:BI (reg:SI)) (reg:BI)) */
449 operands[0] = gen_rtx_SUBREG (BImode, operands[0], 0);
450 operands[1] = SUBREG_REG (operands[1]);
451 }
452 if (SUBREG_P (operands[0])
453 && GET_MODE (operands[0]) == SImode
454 && GET_MODE (SUBREG_REG (operands[0])) == BImode)
455 {
456 /* Likewise, transform this:
457 (set (subreg:SI (reg:BI)) (reg:SI))
458 to this:
459 (set (reg:BI) (subreg:BI (reg:SI))) */
460 operands[0] = SUBREG_REG (operands[0]);
461 operands[1] = gen_rtx_SUBREG (BImode, operands[1], 0);
462 }
463
464 if (MEM_P (operands[0]))
465 operands[1] = force_reg (<MODE>mode, operands[1]);
466
467 if (!lra_in_progress && !reload_completed
468 && !gcn_valid_move_p (<MODE>mode, operands[0], operands[1]))
469 {
470 /* Something is probably trying to generate a move
471 which can only work indirectly.
472 E.g. Move from LDS memory to SGPR hardreg
473 or MEM:QI to SGPR. */
474 rtx tmpreg = gen_reg_rtx (<MODE>mode);
475 emit_insn (gen_mov<mode> (tmpreg, operands[1]));
476 emit_insn (gen_mov<mode> (operands[0], tmpreg));
477 DONE;
478 }
479
480 if (<MODE>mode == DImode
481 && (GET_CODE (operands[1]) == SYMBOL_REF
482 || GET_CODE (operands[1]) == LABEL_REF))
483 {
484 if (lra_in_progress)
485 emit_insn (gen_movdi_symbol_save_scc (operands[0], operands[1]));
486 else
487 emit_insn (gen_movdi_symbol (operands[0], operands[1]));
488 DONE;
489 }
490 })
491
492 ; Split invalid moves into two valid moves
493
494 (define_split
495 [(set (match_operand:MOV_MODE 0 "nonimmediate_operand")
496 (match_operand:MOV_MODE 1 "general_operand"))]
497 "!reload_completed && !lra_in_progress
498 && !gcn_valid_move_p (<MODE>mode, operands[0], operands[1])"
499 [(set (match_dup 2) (match_dup 1))
500 (set (match_dup 0) (match_dup 2))]
501 {
502 operands[2] = gen_reg_rtx(<MODE>mode);
503 })
504
505 ; We need BImode move so we can reload flags registers.
506
507 (define_insn "*movbi"
508 [(set (match_operand:BI 0 "nonimmediate_operand"
509 "=Sg, v,Sg,cs,cV,cV,Sm,&Sm,RS, v,&v,RF, v,&v,RM")
510 (match_operand:BI 1 "gcn_load_operand"
511 "SSA,vSvA, v,SS, v,SS,RS, RS,Sm,RF,RF, v,RM,RM, v"))]
512 ""
513 {
514 /* SCC as an operand is currently not accepted by the LLVM assembler, so
515 we emit bytes directly as a workaround. */
516 switch (which_alternative) {
517 case 0:
518 return "s_mov_b32\t%0, %1";
519 case 1:
520 if (REG_P (operands[1]) && REGNO (operands[1]) == SCC_REG)
521 return "; v_mov_b32\t%0, %1\;"
522 ".byte\t0xfd\;"
523 ".byte\t0x2\;"
524 ".byte\t((%V0<<1)&0xff)\;"
525 ".byte\t0x7e|(%V0>>7)";
526 else
527 return "v_mov_b32\t%0, %1";
528 case 2:
529 return "v_readlane_b32\t%0, %1, 0";
530 case 3:
531 return "s_cmpk_lg_u32\t%1, 0";
532 case 4:
533 return "v_cmp_ne_u32\tvcc, 0, %1";
534 case 5:
535 return "s_mov_b32\tvcc_lo, %1\;"
536 "s_mov_b32\tvcc_hi, 0";
537 case 6:
538 case 7:
539 return "s_load_dword\t%0, %A1\;s_waitcnt\tlgkmcnt(0)";
540 case 8:
541 return "s_store_dword\t%1, %A0";
542 case 9:
543 case 10:
544 return "flat_load_dword\t%0, %A1%O1%g1\;s_waitcnt\t0";
545 case 11:
546 return "flat_store_dword\t%A0, %1%O0%g0";
547 case 12:
548 case 13:
549 return "global_load_dword\t%0, %A1%O1%g1\;s_waitcnt\tvmcnt(0)";
550 case 14:
551 return "global_store_dword\t%A0, %1%O0%g0";
552 default:
553 gcc_unreachable ();
554 }
555 }
556 [(set_attr "type" "sop1,vop1,vop3a,sopk,vopc,mult,smem,smem,smem,flat,flat,
557 flat,flat,flat,flat")
558 (set_attr "exec" "*,*,none,*,*,*,*,*,*,*,*,*,*,*,*")
559 (set_attr "length" "4,4,4,4,4,8,12,12,12,12,12,12,12,12,12")
560 (set_attr "xnack" "*,*,*,*,*,*,off,on,*,off,on,*,off,on,*")])
561
562 ; 32bit move pattern
563
564 (define_insn "*mov<mode>_insn"
565 [(set (match_operand:SISF 0 "nonimmediate_operand")
566 (match_operand:SISF 1 "gcn_load_operand"))]
567 ""
568 {@ [cons: =0, 1; attrs: type, exec, length, cdna, xnack]
569 [SD ,SSA ;sop1 ,* ,4 ,* ,* ] s_mov_b32\t%0, %1
570 [SD ,J ;sopk ,* ,4 ,* ,* ] s_movk_i32\t%0, %1
571 [SD ,B ;sop1 ,* ,8 ,* ,* ] s_mov_b32\t%0, %1
572 [SD ,RB ;smem ,* ,12,* ,off] s_buffer_load%s0\t%0, s[0:3], %1\;s_waitcnt\tlgkmcnt(0)
573 [&SD ,RB ;smem ,* ,12,* ,on ] ^
574 [RB ,Sm ;smem ,* ,12,* ,* ] s_buffer_store%s1\t%1, s[0:3], %0
575 [Sm ,RS ;smem ,* ,12,* ,off] s_load_dword\t%0, %A1\;s_waitcnt\tlgkmcnt(0)
576 [&Sm ,RS ;smem ,* ,12,* ,on ] ^
577 [RS ,Sm ;smem ,* ,12,* ,* ] s_store_dword\t%1, %A0
578 [v ,v ;vop1 ,* ,4 ,* ,* ] v_mov_b32\t%0, %1
579 [Sg ,v ;vop3a,none,8 ,* ,* ] v_readlane_b32\t%0, %1, 0
580 [v ,Sv ;vop3a,none,8 ,* ,* ] v_writelane_b32\t%0, %1, 0
581 [v ,^a ;vop3p_mai,*,8,* ,* ] v_accvgpr_read_b32\t%0, %1
582 [a ,v ;vop3p_mai,*,8,* ,* ] v_accvgpr_write_b32\t%0, %1
583 [a ,a ;vop1 ,* ,4,cdna2,* ] v_accvgpr_mov_b32\t%0, %1
584 [v ,RF ;flat ,* ,12,* ,off] flat_load_dword\t%0, %A1%O1%g1\;s_waitcnt\t0
585 [&v ,RF ;flat ,* ,12,* ,on ] ^
586 [^a ,RF ;flat ,* ,12,cdna2,off] ^
587 [&^a ,RF ;flat ,* ,12,cdna2,on ] ^
588 [RF ,v ;flat ,* ,12,* ,* ] flat_store_dword\t%A0, %1%O0%g0
589 [RF ,a ;flat ,* ,12,cdna2,* ] ^
590 [v ,B ;vop1 ,* ,8 ,* ,* ] v_mov_b32\t%0, %1
591 [RLRG,v ;ds ,* ,12,* ,* ] ds_write_b32\t%A0, %1%O0\;s_waitcnt\tlgkmcnt(0)
592 [v ,RLRG;ds ,* ,12,* ,* ] ds_read_b32\t%0, %A1%O1\;s_waitcnt\tlgkmcnt(0)
593 [SD ,Y ;sop1 ,* ,8 ,* ,* ] s_mov_b32\t%0, %1
594 [v ,RM ;flat ,* ,12,* ,off] global_load_dword\t%0, %A1%O1%g1\;s_waitcnt\tvmcnt(0)
595 [&v ,RM ;flat ,* ,12,* ,on ] ^
596 [^a ,RM ;flat ,* ,12,cdna2,off] ^
597 [&^a ,RM ;flat ,* ,12,cdna2,on ] ^
598 [RM ,v ;flat ,* ,12,* ,* ] global_store_dword\t%A0, %1%O0%g0
599 [RM ,a ;flat ,* ,12,cdna2,* ] ^
600 })
601
602 ; 8/16bit move pattern
603 ; TODO: implement combined load and zero_extend, but *only* for -msram-ecc=on
604
605 (define_insn "*mov<mode>_insn"
606 [(set (match_operand:QIHI 0 "nonimmediate_operand")
607 (match_operand:QIHI 1 "gcn_load_operand"))]
608 "gcn_valid_move_p (<MODE>mode, operands[0], operands[1])"
609 {@ [cons: =0, 1; attrs: type, exec, length, cdna, xnack]
610 [SD ,SSA ;sop1 ,* ,4 ,* ,* ] s_mov_b32\t%0, %1
611 [SD ,J ;sopk ,* ,4 ,* ,* ] s_movk_i32\t%0, %1
612 [SD ,B ;sop1 ,* ,8 ,* ,* ] s_mov_b32\t%0, %1
613 [v ,v ;vop1 ,* ,4 ,* ,* ] v_mov_b32\t%0, %1
614 [Sg ,v ;vop3a,none,4 ,* ,* ] v_readlane_b32\t%0, %1, 0
615 [v ,Sv ;vop3a,none,4 ,* ,* ] v_writelane_b32\t%0, %1, 0
616 [v ,^a ;vop3p_mai,*,8,* ,* ] v_accvgpr_read_b32\t%0, %1
617 [a ,v ;vop3p_mai,*,8,* ,* ] v_accvgpr_write_b32\t%0, %1
618 [a ,a ;vop1 ,* ,8,cdna2,* ] v_accvgpr_mov_b32\t%0, %1
619 [v ,RF ;flat ,* ,12,* ,off] flat_load%o1\t%0, %A1%O1%g1\;s_waitcnt\t0
620 [&v ,RF ;flat ,* ,12,* ,on ] ^
621 [^a ,RF ;flat ,* ,12,cdna2,off] ^
622 [&^a ,RF ;flat ,* ,12,cdna2,on ] ^
623 [RF ,v ;flat ,* ,12,* ,* ] flat_store%s0\t%A0, %1%O0%g0
624 [RF ,a ;flat ,* ,12,cdna2,* ] ^
625 [v ,B ;vop1 ,* ,8 ,* ,* ] v_mov_b32\t%0, %1
626 [RLRG,v ;ds ,* ,12,* ,* ] ds_write%b0\t%A0, %1%O0\;s_waitcnt\tlgkmcnt(0)
627 [v ,RLRG;ds ,* ,12,* ,* ] ds_read%u1\t%0, %A1%O1\;s_waitcnt\tlgkmcnt(0)
628 [v ,RM ;flat ,* ,12,* ,off] global_load%o1\t%0, %A1%O1%g1\;s_waitcnt\tvmcnt(0)
629 [&v ,RM ;flat ,* ,12,* ,on ] ^
630 [^a ,RM ;flat ,* ,12,cdna2,off] ^
631 [&^a ,RM ;flat ,* ,12,cdna2,on ] ^
632 [RM ,v ;flat ,* ,12,* ,* ] global_store%s0\t%A0, %1%O0%g0
633 [RM ,a ;flat ,* ,12,cdna2,* ] ^
634 })
635
636 ; 64bit move pattern
637
638 (define_insn_and_split "*mov<mode>_insn"
639 [(set (match_operand:DIDF 0 "nonimmediate_operand")
640 (match_operand:DIDF 1 "general_operand"))]
641 "GET_CODE(operands[1]) != SYMBOL_REF"
642 {@ [cons: =0, 1; attrs: type, length, cdna, xnack]
643 [SD ,SSA ;sop1 ,4 ,* ,* ] s_mov_b64\t%0, %1
644 [SD ,C ;sop1 ,8 ,* ,* ] ^
645 [SD ,DB ;mult ,* ,* ,* ] #
646 [RS ,Sm ;smem ,12,* ,* ] s_store_dwordx2\t%1, %A0
647 [Sm ,RS ;smem ,12,* ,off] s_load_dwordx2\t%0, %A1\;s_waitcnt\tlgkmcnt(0)
648 [&Sm ,RS ;smem ,12,* ,on ] ^
649 [v ,v ;vmult,* ,* ,* ] #
650 [v ,DB ;vmult,* ,* ,* ] #
651 [Sg ,v ;vmult,* ,* ,* ] #
652 [v ,Sv ;vmult,* ,* ,* ] #
653 [v ,^a ;vmult,* ,* ,* ] #
654 [a ,v ;vmult,* ,* ,* ] #
655 [a ,a ;vmult,* ,cdna2,* ] #
656 [v ,RF ;flat ,12,* ,off] flat_load_dwordx2\t%0, %A1%O1%g1\;s_waitcnt\t0
657 [&v ,RF ;flat ,12,* ,on ] ^
658 [^a ,RF ;flat ,12,cdna2,off] ^
659 [&^a ,RF ;flat ,12,cdna2,on ] ^
660 [RF ,v ;flat ,12,* ,* ] flat_store_dwordx2\t%A0, %1%O0%g0
661 [RF ,a ;flat ,12,cdna2,* ] ^
662 [RLRG,v ;ds ,12,* ,* ] ds_write_b64\t%A0, %1%O0\;s_waitcnt\tlgkmcnt(0)
663 [v ,RLRG;ds ,12,* ,* ] ds_read_b64\t%0, %A1%O1\;s_waitcnt\tlgkmcnt(0)
664 [v ,RM ;flat ,12,* ,off] global_load_dwordx2\t%0, %A1%O1%g1\;s_waitcnt\tvmcnt(0)
665 [&v ,RM ;flat ,12,* ,on ] ^
666 [^a ,RM ;flat ,12,cdna2,off] ^
667 [&^a ,RM ;flat ,12,cdna2,on ] ^
668 [RM ,v ;flat ,12,* ,* ] global_store_dwordx2\t%A0, %1%O0%g0
669 [RM ,a ;flat ,12,cdna2,* ] ^
670 }
671 "reload_completed
672 && ((!MEM_P (operands[0]) && !MEM_P (operands[1])
673 && !gcn_sgpr_move_p (operands[0], operands[1]))
674 || (GET_CODE (operands[1]) == CONST_INT
675 && !gcn_constant64_p (operands[1])))"
676 [(set (match_dup 0) (match_dup 1))
677 (set (match_dup 2) (match_dup 3))]
678 {
679 rtx inlo = gen_lowpart (SImode, operands[1]);
680 rtx inhi = gen_highpart_mode (SImode, <MODE>mode, operands[1]);
681 rtx outlo = gen_lowpart (SImode, operands[0]);
682 rtx outhi = gen_highpart_mode (SImode, <MODE>mode, operands[0]);
683
684 /* Ensure that overlapping registers aren't corrupted. */
685 if (reg_overlap_mentioned_p (outlo, inhi))
686 {
687 operands[0] = outhi;
688 operands[1] = inhi;
689 operands[2] = outlo;
690 operands[3] = inlo;
691 }
692 else
693 {
694 operands[0] = outlo;
695 operands[1] = inlo;
696 operands[2] = outhi;
697 operands[3] = inhi;
698 }
699 })
700
701 ; 128-bit move.
702
703 (define_insn_and_split "*movti_insn"
704 [(set (match_operand:TI 0 "nonimmediate_operand")
705 (match_operand:TI 1 "general_operand" ))]
706 ""
707 {@ [cons: =0, 1; attrs: type, delayeduse, length, cdna, xnack]
708 [SD ,SSB;mult ,* ,* ,* ,* ] #
709 [RS ,Sm ;smem ,* ,12,* ,* ] s_store_dwordx4\t%1, %A0
710 [Sm ,RS ;smem ,yes,12,* ,off] s_load_dwordx4\t%0, %A1\;s_waitcnt\tlgkmcnt(0)
711 [&Sm,RS ;smem ,yes,12,* ,on ] ^
712 [RF ,v ;flat ,* ,12,* ,* ] flat_store_dwordx4\t%A0, %1%O0%g0
713 [RF ,a ;flat ,* ,12,cdna2,* ] ^
714 [v ,RF ;flat ,* ,12,* ,off] flat_load_dwordx4\t%0, %A1%O1%g1\;s_waitcnt\t0
715 [&v ,RF ;flat ,* ,12,* ,on ] ^
716 [^a ,RF ;flat ,* ,12,cdna2,off] ^
717 [&^a,RF ;flat ,* ,12,cdna2,on ] ^
718 [v ,v ;vmult,* ,* ,* ,* ] #
719 [v ,Sv ;vmult,* ,* ,* ,* ] #
720 [SD ,v ;vmult,* ,* ,* ,* ] #
721 [RM ,v ;flat ,yes,12,* ,* ] global_store_dwordx4\t%A0, %1%O0%g0
722 [RM ,a ;flat ,yes,12,cdna2,* ] ^
723 [v ,RM ;flat ,* ,12,* ,off] global_load_dwordx4\t%0, %A1%O1%g1\;s_waitcnt\tvmcnt(0)
724 [&v ,RM ;flat ,* ,12,* ,on ] ^
725 [^a ,RM ;flat ,* ,12,cdna2,off] ^
726 [&^a,RM ;flat ,* ,12,cdna2,on ] ^
727 [RL ,v ;ds ,* ,12,* ,* ] ds_write_b128\t%A0, %1%O0\;s_waitcnt\tlgkmcnt(0)
728 [v ,RL ;ds ,* ,12,* ,* ] ds_read_b128\t%0, %A1%O1\;s_waitcnt\tlgkmcnt(0)
729 [v ,^a ;vmult,* ,* ,* ,* ] #
730 [a ,v ;vmult,* ,* ,* ,* ] #
731 [a ,a ;vmult,* ,* ,cdna2,* ] #
732 }
733 "reload_completed
734 && REG_P (operands[0])
735 && (REG_P (operands[1]) || GET_CODE (operands[1]) == CONST_INT)"
736 [(set (match_dup 0) (match_dup 1))
737 (set (match_dup 2) (match_dup 3))
738 (set (match_dup 4) (match_dup 5))
739 (set (match_dup 6) (match_dup 7))]
740 {
741 gcc_assert (rtx_equal_p (operands[0], operands[1])
742 || !reg_overlap_mentioned_p (operands[0], operands[1]));
743 operands[6] = gcn_operand_part (TImode, operands[0], 3);
744 operands[7] = gcn_operand_part (TImode, operands[1], 3);
745 operands[4] = gcn_operand_part (TImode, operands[0], 2);
746 operands[5] = gcn_operand_part (TImode, operands[1], 2);
747 operands[2] = gcn_operand_part (TImode, operands[0], 1);
748 operands[3] = gcn_operand_part (TImode, operands[1], 1);
749 operands[0] = gcn_operand_part (TImode, operands[0], 0);
750 operands[1] = gcn_operand_part (TImode, operands[1], 0);
751 })
752
753 ;; }}}
754 ;; {{{ Prologue/Epilogue
755
756 (define_insn "prologue_use"
757 [(unspec_volatile [(match_operand 0 "register_operand")] UNSPECV_PROLOGUE_USE)]
758 "1 /* This comment silences a warning for operands[2]. */"
759 ""
760 [(set_attr "length" "0")])
761
762 (define_expand "prologue"
763 [(const_int 0)]
764 ""
765 {
766 gcn_expand_prologue ();
767 DONE;
768 })
769
770 (define_expand "epilogue"
771 [(const_int 0)]
772 ""
773 {
774 gcn_expand_epilogue ();
775 DONE;
776 })
777
778 ;; }}}
779 ;; {{{ Control flow
780
781 ; This pattern must satisfy simplejump_p, which means it cannot be a parallel
782 ; that clobbers SCC. Thus, we must preserve SCC if we're generating a long
783 ; branch sequence.
784
785 (define_insn "jump"
786 [(set (pc)
787 (label_ref (match_operand 0)))]
788 ""
789 {
790 if (get_attr_length (insn) == 4)
791 return "s_branch\t%0";
792 else
793 /* !!! This sequence clobbers EXEC_SAVE_REG and CC_SAVE_REG. */
794 return "s_mov_b32\ts22, scc\;"
795 "s_getpc_b64\ts[20:21]\;"
796 "s_add_u32\ts20, s20, %0@rel32@lo+4\;"
797 "s_addc_u32\ts21, s21, %0@rel32@hi+4\;"
798 "s_cmpk_lg_u32\ts22, 0\;"
799 "s_setpc_b64\ts[20:21]";
800 }
801 [(set_attr "type" "sopp")
802 (set (attr "length")
803 (if_then_else (and (ge (minus (match_dup 0) (pc))
804 (const_int -131072))
805 (lt (minus (match_dup 0) (pc))
806 (const_int 131072)))
807 (const_int 4)
808 (const_int 32)))])
809
810 (define_insn "indirect_jump"
811 [(set (pc)
812 (match_operand:DI 0 "register_operand" "Sg"))]
813 ""
814 "s_setpc_b64\t%0"
815 [(set_attr "type" "sop1")
816 (set_attr "length" "4")])
817
818 (define_insn "cjump"
819 [(set (pc)
820 (if_then_else
821 (match_operator:BI 1 "gcn_conditional_operator"
822 [(match_operand:BI 2 "gcn_conditional_register_operand" "ca,cV")
823 (const_int 0)])
824 (label_ref (match_operand 0))
825 (pc)))]
826 ""
827 {
828 if (get_attr_length (insn) == 4)
829 return "s_cbranch%C1\t%0";
830 else
831 {
832 /* !!! This sequence clobbers EXEC_SAVE_REG and CC_SAVE_REG but
833 restores SCC. */
834 if (REGNO (operands[2]) == SCC_REG)
835 {
836 if (GET_CODE (operands[1]) == EQ)
837 return "s_cbranch%c1\t.Lskip%=\;"
838 "s_getpc_b64\ts[20:21]\;"
839 "s_add_u32\ts20, s20, %0@rel32@lo+4\;"
840 "s_addc_u32\ts21, s21, %0@rel32@hi+4\;"
841 "s_cmp_lg_u32\t0, 0\;"
842 "s_setpc_b64\ts[20:21]\n"
843 ".Lskip%=:";
844 else
845 return "s_cbranch%c1\t.Lskip%=\;"
846 "s_getpc_b64\ts[20:21]\;"
847 "s_add_u32\ts20, s20, %0@rel32@lo+4\;"
848 "s_addc_u32\ts21, s21, %0@rel32@hi+4\;"
849 "s_cmp_eq_u32\t0, 0\;"
850 "s_setpc_b64\ts[20:21]\n"
851 ".Lskip%=:";
852 }
853 else
854 return "s_cbranch%c1\t.Lskip%=\;"
855 "s_mov_b32\ts22, scc\;"
856 "s_getpc_b64\ts[20:21]\;"
857 "s_add_u32\ts20, s20, %0@rel32@lo+4\;"
858 "s_addc_u32\ts21, s21, %0@rel32@hi+4\;"
859 "s_cmpk_lg_u32\ts22, 0\;"
860 "s_setpc_b64\ts[20:21]\n"
861 ".Lskip%=:";
862 }
863 }
864 [(set_attr "type" "sopp")
865 (set (attr "length")
866 (if_then_else (and (ge (minus (match_dup 0) (pc))
867 (const_int -131072))
868 (lt (minus (match_dup 0) (pc))
869 (const_int 131072)))
870 (const_int 4)
871 (const_int 36)))])
872
873 ; Returning from a normal function is different to returning from a
874 ; kernel function.
875
876 (define_insn "gcn_return"
877 [(return)]
878 ""
879 {
880 if (cfun && cfun->machine && cfun->machine->normal_function)
881 return "s_setpc_b64\ts[18:19]";
882 else
883 return "s_waitcnt\tlgkmcnt(0)\;s_endpgm";
884 }
885 [(set_attr "type" "sop1")
886 (set_attr "length" "12")])
887
888 (define_expand "call"
889 [(parallel [(call (match_operand 0 "")
890 (match_operand 1 ""))
891 (clobber (reg:DI LR_REGNUM))
892 (clobber (match_scratch:DI 2))])]
893 ""
894 {})
895
896 (define_insn "gcn_simple_call"
897 [(call (mem (match_operand 0 "immediate_operand" "Y,B"))
898 (match_operand 1 "const_int_operand"))
899 (clobber (reg:DI LR_REGNUM))
900 (clobber (match_scratch:DI 2 "=&Sg,X"))]
901 ""
902 "@
903 s_getpc_b64\t%2\;s_add_u32\t%L2, %L2, %0@rel32@lo+4\;s_addc_u32\t%H2, %H2, %0@rel32@hi+4\;s_swappc_b64\ts[18:19], %2
904 s_swappc_b64\ts[18:19], %0"
905 [(set_attr "type" "mult,sop1")
906 (set_attr "length" "24,4")])
907
908 (define_insn "movdi_symbol"
909 [(set (match_operand:DI 0 "nonimmediate_operand" "=Sg")
910 (match_operand:DI 1 "general_operand" "Y"))
911 (clobber (reg:BI SCC_REG))]
912 "GET_CODE (operands[1]) == SYMBOL_REF || GET_CODE (operands[1]) == LABEL_REF"
913 {
914 /* This s_load may not be XNACK-safe on devices where the GOT may fault.
915 DGPUs are most likely fine. */
916 if (SYMBOL_REF_P (operands[1])
917 && SYMBOL_REF_WEAK (operands[1]))
918 return "s_getpc_b64\t%0\;"
919 "s_add_u32\t%L0, %L0, %1@gotpcrel32@lo+4\;"
920 "s_addc_u32\t%H0, %H0, %1@gotpcrel32@hi+4\;"
921 "s_load_dwordx2\t%0, %0\;"
922 "s_waitcnt\tlgkmcnt(0)";
923
924 return "s_getpc_b64\t%0\;"
925 "s_add_u32\t%L0, %L0, %1@rel32@lo+4\;"
926 "s_addc_u32\t%H0, %H0, %1@rel32@hi+4";
927 }
928 [(set_attr "type" "mult")
929 (set_attr "length" "32")])
930
931 (define_insn "movdi_symbol_save_scc"
932 [(set (match_operand:DI 0 "nonimmediate_operand" "=Sg")
933 (match_operand:DI 1 "general_operand" "Y"))
934 (clobber (reg:BI CC_SAVE_REG))]
935 "(GET_CODE (operands[1]) == SYMBOL_REF || GET_CODE (operands[1]) == LABEL_REF)
936 && (lra_in_progress || reload_completed)"
937 {
938 /* !!! These sequences clobber CC_SAVE_REG. */
939
940 /* This s_load may not be XNACK-safe on devices where the GOT may fault.
941 DGPUs are most likely fine. */
942 if (SYMBOL_REF_P (operands[1])
943 && SYMBOL_REF_WEAK (operands[1]))
944 return "s_mov_b32\ts22, scc\;"
945 "s_getpc_b64\t%0\;"
946 "s_add_u32\t%L0, %L0, %1@gotpcrel32@lo+4\;"
947 "s_addc_u32\t%H0, %H0, %1@gotpcrel32@hi+4\;"
948 "s_load_dwordx2\t%0, %0\;"
949 "s_cmpk_lg_u32\ts22, 0\;"
950 "s_waitcnt\tlgkmcnt(0)";
951
952 return "s_mov_b32\ts22, scc\;"
953 "s_getpc_b64\t%0\;"
954 "s_add_u32\t%L0, %L0, %1@rel32@lo+4\;"
955 "s_addc_u32\t%H0, %H0, %1@rel32@hi+4\;"
956 "s_cmpk_lg_u32\ts22, 0";
957 }
958 [(set_attr "type" "mult")
959 (set_attr "length" "40")])
960
961
962 (define_insn "gcn_indirect_call"
963 [(call (mem (match_operand:DI 0 "register_operand" "Sg"))
964 (match_operand 1 "" ""))
965 (clobber (reg:DI LR_REGNUM))
966 (clobber (match_scratch:DI 2 "=X"))]
967 ""
968 "s_swappc_b64\ts[18:19], %0"
969 [(set_attr "type" "sop1")
970 (set_attr "length" "4")])
971
972 (define_expand "call_value"
973 [(parallel [(set (match_operand 0 "")
974 (call (match_operand 1 "")
975 (match_operand 2 "")))
976 (clobber (reg:DI LR_REGNUM))
977 (clobber (match_scratch:DI 3))])]
978 ""
979 {})
980
981 (define_insn "gcn_call_value"
982 [(set (match_operand 0 "register_operand" "=Sgv,Sgv")
983 (call (mem (match_operand 1 "immediate_operand" " Y, B"))
984 (match_operand 2 "const_int_operand")))
985 (clobber (reg:DI LR_REGNUM))
986 (clobber (match_scratch:DI 3 "=&Sg, X"))]
987 ""
988 "@
989 s_getpc_b64\t%3\;s_add_u32\t%L3, %L3, %1@rel32@lo+4\;s_addc_u32\t%H3, %H3, %1@rel32@hi+4\;s_swappc_b64\ts[18:19], %3
990 s_swappc_b64\ts[18:19], %1"
991 [(set_attr "type" "sop1")
992 (set_attr "length" "24")])
993
994 (define_insn "gcn_call_value_indirect"
995 [(set (match_operand 0 "register_operand" "=Sgv")
996 (call (mem (match_operand:DI 1 "register_operand" " Sg"))
997 (match_operand 2 "" "")))
998 (clobber (reg:DI LR_REGNUM))
999 (clobber (match_scratch:DI 3 "= X"))]
1000 ""
1001 "s_swappc_b64\ts[18:19], %1"
1002 [(set_attr "type" "sop1")
1003 (set_attr "length" "4")])
1004
1005 ; GCN does not have an instruction to clear only part of the instruction
1006 ; cache, so the operands are ignored.
1007
1008 (define_insn "clear_icache"
1009 [(unspec_volatile
1010 [(match_operand 0 "") (match_operand 1 "")]
1011 UNSPECV_ICACHE_INV)]
1012 ""
1013 "s_icache_inv"
1014 [(set_attr "type" "sopp")
1015 (set_attr "length" "4")])
1016
1017 (define_expand "exception_receiver"
1018 [(const_int 0)]
1019 ""
1020 {
1021 if (!fake_exceptions)
1022 sorry ("exception handling not supported");
1023 DONE;
1024 })
1025
1026 ;; }}}
1027 ;; {{{ Conditionals
1028
1029 ; 32-bit compare, scalar unit only
1030
1031 (define_insn "cstoresi4"
1032 [(set (match_operand:BI 0 "gcn_conditional_register_operand"
1033 "=cs, cs, cs, cs")
1034 (match_operator:BI 1 "gcn_compare_operator"
1035 [(match_operand:SI 2 "gcn_alu_operand" "SSA,SSA,SSB, SS")
1036 (match_operand:SI 3 "gcn_alu_operand" "SSA,SSL, SS,SSB")]))]
1037 ""
1038 "@
1039 s_cmp%D1\t%2, %3
1040 s_cmpk%D1\t%2, %3
1041 s_cmp%D1\t%2, %3
1042 s_cmp%D1\t%2, %3"
1043 [(set_attr "type" "sopc,sopk,sopk,sopk")
1044 (set_attr "length" "4,4,8,8")])
1045
1046 (define_expand "cbranchsi4"
1047 [(match_operator 0 "gcn_compare_operator"
1048 [(match_operand:SI 1 "gcn_alu_operand")
1049 (match_operand:SI 2 "gcn_alu_operand")])
1050 (match_operand 3)]
1051 ""
1052 {
1053 rtx cc = gen_reg_rtx (BImode);
1054 emit_insn (gen_cstoresi4 (cc, operands[0], operands[1], operands[2]));
1055 emit_jump_insn (gen_cjump (operands[3],
1056 gen_rtx_NE (BImode, cc, const0_rtx), cc));
1057 DONE;
1058 })
1059
1060 ; 64-bit compare; either unit, but scalar allows limited operators
1061
1062 (define_expand "cstoredi4"
1063 [(set (match_operand:BI 0 "gcn_conditional_register_operand")
1064 (match_operator:BI 1 "gcn_compare_operator"
1065 [(match_operand:DI 2 "gcn_alu_operand")
1066 (match_operand:DI 3 "gcn_alu_operand")]))]
1067 ""
1068 {})
1069
1070 (define_insn "cstoredi4_vec_and_scalar"
1071 [(set (match_operand:BI 0 "gcn_conditional_register_operand" "= cs, cV")
1072 (match_operator:BI 1 "gcn_compare_64bit_operator"
1073 [(match_operand:DI 2 "gcn_alu_operand" "%SSA,vSvC")
1074 (match_operand:DI 3 "gcn_alu_operand" " SSC, v")]))]
1075 ""
1076 "@
1077 s_cmp%D1\t%2, %3
1078 v_cmp%E1\tvcc, %2, %3"
1079 [(set_attr "type" "sopc,vopc")
1080 (set_attr "length" "8")])
1081
1082 (define_insn "cstoredi4_vector"
1083 [(set (match_operand:BI 0 "gcn_conditional_register_operand" "= cV")
1084 (match_operator:BI 1 "gcn_compare_operator"
1085 [(match_operand:DI 2 "gcn_alu_operand" "vSvB")
1086 (match_operand:DI 3 "gcn_alu_operand" " v")]))]
1087 ""
1088 "v_cmp%E1\tvcc, %2, %3"
1089 [(set_attr "type" "vopc")
1090 (set_attr "length" "8")])
1091
1092 (define_expand "cbranchdi4"
1093 [(match_operator 0 "gcn_compare_operator"
1094 [(match_operand:DI 1 "gcn_alu_operand")
1095 (match_operand:DI 2 "gcn_alu_operand")])
1096 (match_operand 3)]
1097 ""
1098 {
1099 rtx cc = gen_reg_rtx (BImode);
1100 emit_insn (gen_cstoredi4 (cc, operands[0], operands[1], operands[2]));
1101 emit_jump_insn (gen_cjump (operands[3],
1102 gen_rtx_NE (BImode, cc, const0_rtx), cc));
1103 DONE;
1104 })
1105
1106 ; FP compare; vector unit only
1107
1108 (define_insn "cstore<mode>4"
1109 [(set (match_operand:BI 0 "gcn_conditional_register_operand" "=cV")
1110 (match_operator:BI 1 "gcn_fp_compare_operator"
1111 [(match_operand:SFDF 2 "gcn_alu_operand" "vB")
1112 (match_operand:SFDF 3 "gcn_alu_operand" "v")]))]
1113 ""
1114 "v_cmp%E1\tvcc, %2, %3"
1115 [(set_attr "type" "vopc")
1116 (set_attr "length" "8")])
1117
1118 (define_expand "cbranch<mode>4"
1119 [(match_operator 0 "gcn_fp_compare_operator"
1120 [(match_operand:SFDF 1 "gcn_alu_operand")
1121 (match_operand:SFDF 2 "gcn_alu_operand")])
1122 (match_operand 3)]
1123 ""
1124 {
1125 rtx cc = gen_reg_rtx (BImode);
1126 emit_insn (gen_cstore<mode>4 (cc, operands[0], operands[1], operands[2]));
1127 emit_jump_insn (gen_cjump (operands[3],
1128 gen_rtx_NE (BImode, cc, const0_rtx), cc));
1129 DONE;
1130 })
1131
1132 ;; }}}
1133 ;; {{{ ALU special cases: Plus
1134
1135 (define_insn "addsi3"
1136 [(set (match_operand:SI 0 "register_operand" "= Sg, Sg, Sg, v")
1137 (plus:SI (match_operand:SI 1 "gcn_alu_operand" "%SgA, 0,SgA, v")
1138 (match_operand:SI 2 "gcn_alu_operand" " SgA,SgJ, B,vBSv")))
1139 (clobber (match_scratch:BI 3 "= cs, cs, cs, X"))
1140 (clobber (match_scratch:DI 4 "= X, X, X, cV"))]
1141 ""
1142 "@
1143 s_add_i32\t%0, %1, %2
1144 s_addk_i32\t%0, %2
1145 s_add_i32\t%0, %1, %2
1146 v_add_co_u32\t%0, vcc, %2, %1"
1147 [(set_attr "type" "sop2,sopk,sop2,vop2")
1148 (set_attr "length" "4,4,8,8")])
1149
1150 (define_expand "addsi3_scc"
1151 [(parallel [(set (match_operand:SI 0 "register_operand")
1152 (plus:SI (match_operand:SI 1 "gcn_alu_operand")
1153 (match_operand:SI 2 "gcn_alu_operand")))
1154 (clobber (reg:BI SCC_REG))
1155 (clobber (scratch:DI))])]
1156 ""
1157 {})
1158
1159 ; Having this as an insn_and_split allows us to keep together DImode adds
1160 ; through some RTL optimisation passes, and means the CC reg we set isn't
1161 ; dependent on the constraint alternative (which doesn't seem to work well).
1162
1163 ; If v_addc_u32 is used to add with carry, a 32-bit literal constant cannot be
1164 ; used as an operand due to the read of VCC, so we restrict constants to the
1165 ; inlinable range for that alternative.
1166
1167 (define_insn_and_split "adddi3"
1168 [(set (match_operand:DI 0 "register_operand" "=Sg, v")
1169 (plus:DI (match_operand:DI 1 "register_operand" " Sg, v")
1170 (match_operand:DI 2 "nonmemory_operand" "SgB,vA")))
1171 (clobber (match_scratch:BI 3 "=cs, X"))
1172 (clobber (match_scratch:DI 4 "= X,cV"))]
1173 ""
1174 "#"
1175 "&& reload_completed"
1176 [(const_int 0)]
1177 {
1178 rtx cc = gen_rtx_REG (BImode, gcn_vgpr_register_operand (operands[1],
1179 DImode)
1180 ? VCC_REG : SCC_REG);
1181
1182 emit_insn (gen_addsi3_scalar_carry
1183 (gcn_operand_part (DImode, operands[0], 0),
1184 gcn_operand_part (DImode, operands[1], 0),
1185 gcn_operand_part (DImode, operands[2], 0),
1186 cc));
1187 rtx val = gcn_operand_part (DImode, operands[2], 1);
1188 if (val != const0_rtx)
1189 emit_insn (gen_addcsi3_scalar
1190 (gcn_operand_part (DImode, operands[0], 1),
1191 gcn_operand_part (DImode, operands[1], 1),
1192 gcn_operand_part (DImode, operands[2], 1),
1193 cc, cc));
1194 else
1195 emit_insn (gen_addcsi3_scalar_zero
1196 (gcn_operand_part (DImode, operands[0], 1),
1197 gcn_operand_part (DImode, operands[1], 1),
1198 cc));
1199 DONE;
1200 }
1201 [(set_attr "type" "mult,vmult")
1202 (set_attr "length" "8")])
1203
1204 (define_expand "adddi3_scc"
1205 [(parallel [(set (match_operand:DI 0 "register_operand")
1206 (plus:DI (match_operand:DI 1 "register_operand")
1207 (match_operand:DI 2 "nonmemory_operand")))
1208 (clobber (reg:BI SCC_REG))
1209 (clobber (scratch:DI))])]
1210 ""
1211 {})
1212
1213 ;; Add with carry.
1214
1215 (define_insn "addsi3_scalar_carry"
1216 [(set (match_operand:SI 0 "register_operand" "= Sg, v")
1217 (plus:SI (match_operand:SI 1 "gcn_alu_operand" "%SgA, v")
1218 (match_operand:SI 2 "gcn_alu_operand" " SgB,vB")))
1219 (set (match_operand:BI 3 "register_operand" "= cs,cV")
1220 (ltu:BI (plus:SI (match_dup 1)
1221 (match_dup 2))
1222 (match_dup 1)))]
1223 ""
1224 "@
1225 s_add_u32\t%0, %1, %2
1226 v_add_co_u32\t%0, vcc, %2, %1"
1227 [(set_attr "type" "sop2,vop2")
1228 (set_attr "length" "8,8")])
1229
1230 (define_insn "addsi3_scalar_carry_cst"
1231 [(set (match_operand:SI 0 "register_operand" "=Sg, v")
1232 (plus:SI (match_operand:SI 1 "gcn_alu_operand" "SgA, v")
1233 (match_operand:SI 2 "const_int_operand" " n, n")))
1234 (set (match_operand:BI 4 "register_operand" "=cs,cV")
1235 (geu:BI (plus:SI (match_dup 1)
1236 (match_dup 2))
1237 (match_operand:SI 3 "const_int_operand" " n, n")))]
1238 "INTVAL (operands[2]) == -INTVAL (operands[3])"
1239 "@
1240 s_add_u32\t%0, %1, %2
1241 v_add_co_u32\t%0, vcc, %2, %1"
1242 [(set_attr "type" "sop2,vop2")
1243 (set_attr "length" "4")])
1244
1245 (define_insn "addcsi3_scalar"
1246 [(set (match_operand:SI 0 "register_operand" "= Sg, v")
1247 (plus:SI (plus:SI (zero_extend:SI
1248 (match_operand:BI 3 "register_operand" "= cs,cV"))
1249 (match_operand:SI 1 "gcn_alu_operand" "%SgA, v"))
1250 (match_operand:SI 2 "gcn_alu_operand" " SgB,vA")))
1251 (set (match_operand:BI 4 "register_operand" "= 3, 3")
1252 (ior:BI (ltu:BI (plus:SI
1253 (plus:SI
1254 (zero_extend:SI (match_dup 3))
1255 (match_dup 1))
1256 (match_dup 2))
1257 (match_dup 2))
1258 (ltu:BI (plus:SI (zero_extend:SI (match_dup 3)) (match_dup 1))
1259 (match_dup 1))))]
1260 ""
1261 "@
1262 s_addc_u32\t%0, %1, %2
1263 {v_addc_co_u32|v_add_co_ci_u32}\t%0, vcc, %2, %1, vcc"
1264 [(set_attr "type" "sop2,vop2")
1265 (set_attr "length" "8,4")])
1266
1267 (define_insn "addcsi3_scalar_zero"
1268 [(set (match_operand:SI 0 "register_operand" "=Sg, v")
1269 (plus:SI (zero_extend:SI
1270 (match_operand:BI 2 "register_operand" "=cs,cV"))
1271 (match_operand:SI 1 "gcn_alu_operand" "SgA, v")))
1272 (set (match_dup 2)
1273 (ltu:BI (plus:SI (zero_extend:SI (match_dup 2))
1274 (match_dup 1))
1275 (match_dup 1)))]
1276 ""
1277 "@
1278 s_addc_u32\t%0, %1, 0
1279 {v_addc_co_u32|v_add_co_ci_u32}\t%0, vcc, 0, %1, vcc"
1280 [(set_attr "type" "sop2,vop2")
1281 (set_attr "length" "4")])
1282
1283 ; "addptr" is the same as "add" except that it must not write to VCC or SCC
1284 ; as a side-effect. Unfortunately GCN does not have a suitable instruction
1285 ; for this, so we use CC_SAVE_REG as a temp.
1286 ; Note that it is not safe to save/clobber/restore as separate insns because
1287 ; doing so will break data-flow analysis, so this must use multiple
1288 ; instructions in one insn.
1289 ;
1290 ; The "v0" should be just "v", but somehow the "0" helps LRA not loop forever
1291 ; on testcase pr54713-2.c with -O0. It's only an optimization hint anyway.
1292 ;
1293 ; The SGPR alternative is preferred as it is typically used with mov_sgprbase.
1294
1295 (define_insn "addptrdi3"
1296 [(set (match_operand:DI 0 "register_operand" "= v, Sg")
1297 (unspec:DI [
1298 (plus:DI (match_operand:DI 1 "register_operand" "^v0,Sg0")
1299 (match_operand:DI 2 "nonmemory_operand" "vDA,SgDB"))]
1300 UNSPEC_ADDPTR))]
1301 ""
1302 {
1303 if (which_alternative == 0)
1304 {
1305 rtx new_operands[4] = { operands[0], operands[1], operands[2],
1306 gen_rtx_REG (DImode, CC_SAVE_REG) };
1307
1308 output_asm_insn ("v_add_co_u32\t%L0, %3, %L2, %L1", new_operands);
1309 output_asm_insn ("{v_addc_co_u32|v_add_co_ci_u32}\t%H0, %3, %H2, %H1, %3",
1310 new_operands);
1311 }
1312 else
1313 {
1314 rtx new_operands[4] = { operands[0], operands[1], operands[2],
1315 gen_rtx_REG (BImode, CC_SAVE_REG) };
1316
1317 output_asm_insn ("s_mov_b32\t%3, scc", new_operands);
1318 output_asm_insn ("s_add_u32\t%L0, %L1, %L2", new_operands);
1319 output_asm_insn ("s_addc_u32\t%H0, %H1, %H2", new_operands);
1320 output_asm_insn ("s_cmpk_lg_u32\t%3, 0", new_operands);
1321 }
1322
1323 return "";
1324 }
1325 [(set_attr "type" "vmult,mult")
1326 (set_attr "length" "16,24")])
1327
1328 ;; }}}
1329 ;; {{{ ALU special cases: Minus
1330
1331 (define_insn "subsi3"
1332 [(set (match_operand:SI 0 "register_operand" "=Sg, Sg, v, v")
1333 (minus:SI (match_operand:SI 1 "gcn_alu_operand" "SgA,SgA, v,vBSv")
1334 (match_operand:SI 2 "gcn_alu_operand" "SgA, B, vBSv, v")))
1335 (clobber (match_scratch:BI 3 "=cs, cs, X, X"))
1336 (clobber (match_scratch:DI 4 "= X, X, cV, cV"))]
1337 ""
1338 "@
1339 s_sub_i32\t%0, %1, %2
1340 s_sub_i32\t%0, %1, %2
1341 v_subrev_co_u32\t%0, vcc, %2, %1
1342 v_sub_co_u32\t%0, vcc, %1, %2"
1343 [(set_attr "type" "sop2,sop2,vop2,vop2")
1344 (set_attr "length" "4,8,8,8")])
1345
1346 (define_insn_and_split "subdi3"
1347 [(set (match_operand:DI 0 "register_operand" "=Sg, Sg")
1348 (minus:DI
1349 (match_operand:DI 1 "gcn_alu_operand" "SgA,SgB")
1350 (match_operand:DI 2 "gcn_alu_operand" "SgB,SgA")))
1351 (clobber (reg:BI SCC_REG))]
1352 ""
1353 "#"
1354 "reload_completed"
1355 [(const_int 0)]
1356 {
1357 emit_insn (gen_subsi3_scalar_carry
1358 (gcn_operand_part (DImode, operands[0], 0),
1359 gcn_operand_part (DImode, operands[1], 0),
1360 gcn_operand_part (DImode, operands[2], 0)));
1361 rtx val = gcn_operand_part (DImode, operands[2], 1);
1362 if (val != const0_rtx)
1363 emit_insn (gen_subcsi3_scalar
1364 (gcn_operand_part (DImode, operands[0], 1),
1365 gcn_operand_part (DImode, operands[1], 1),
1366 gcn_operand_part (DImode, operands[2], 1)));
1367 else
1368 emit_insn (gen_subcsi3_scalar_zero
1369 (gcn_operand_part (DImode, operands[0], 1),
1370 gcn_operand_part (DImode, operands[1], 1)));
1371 DONE;
1372 }
1373 [(set_attr "length" "8")])
1374
1375 (define_insn "subsi3_scalar_carry"
1376 [(set (match_operand:SI 0 "register_operand" "=Sg, Sg")
1377 (minus:SI (match_operand:SI 1 "gcn_alu_operand" "SgA,SgB")
1378 (match_operand:SI 2 "gcn_alu_operand" "SgB,SgA")))
1379 (set (reg:BI SCC_REG)
1380 (gtu:BI (minus:SI (match_dup 1)
1381 (match_dup 2))
1382 (match_dup 1)))]
1383 ""
1384 "s_sub_u32\t%0, %1, %2"
1385 [(set_attr "type" "sop2")
1386 (set_attr "length" "8")])
1387
1388 (define_insn "subsi3_scalar_carry_cst"
1389 [(set (match_operand:SI 0 "register_operand" "=Sg")
1390 (minus:SI (match_operand:SI 1 "gcn_alu_operand" "SgA")
1391 (match_operand:SI 2 "const_int_operand" " n")))
1392 (set (reg:BI SCC_REG)
1393 (leu:BI (minus:SI (match_dup 1)
1394 (match_dup 2))
1395 (match_operand:SI 3 "const_int_operand" " n")))]
1396 "INTVAL (operands[2]) == -INTVAL (operands[3])"
1397 "s_sub_u32\t%0, %1, %2"
1398 [(set_attr "type" "sop2")
1399 (set_attr "length" "4")])
1400
1401 (define_insn "subcsi3_scalar"
1402 [(set (match_operand:SI 0 "register_operand" "=Sg, Sg")
1403 (minus:SI (minus:SI (zero_extend:SI (reg:BI SCC_REG))
1404 (match_operand:SI 1 "gcn_alu_operand" "SgA,SgB"))
1405 (match_operand:SI 2 "gcn_alu_operand" "SgB,SgA")))
1406 (set (reg:BI SCC_REG)
1407 (ior:BI (gtu:BI (minus:SI (minus:SI (zero_extend:SI (reg:BI SCC_REG))
1408 (match_dup 1))
1409 (match_dup 2))
1410 (match_dup 1))
1411 (gtu:BI (minus:SI (zero_extend:SI (reg:BI SCC_REG))
1412 (match_dup 1))
1413 (match_dup 1))))]
1414 ""
1415 "s_subb_u32\t%0, %1, %2"
1416 [(set_attr "type" "sop2")
1417 (set_attr "length" "8")])
1418
1419 (define_insn "subcsi3_scalar_zero"
1420 [(set (match_operand:SI 0 "register_operand" "=Sg")
1421 (minus:SI (zero_extend:SI (reg:BI SCC_REG))
1422 (match_operand:SI 1 "gcn_alu_operand" "SgA")))
1423 (set (reg:BI SCC_REG)
1424 (gtu:BI (minus:SI (zero_extend:SI (reg:BI SCC_REG)) (match_dup 1))
1425 (match_dup 1)))]
1426 ""
1427 "s_subb_u32\t%0, %1, 0"
1428 [(set_attr "type" "sop2")
1429 (set_attr "length" "4")])
1430
1431 ;; }}}
1432 ;; {{{ ALU: mult
1433
1434 ; Vector multiply has vop3a encoding, but no corresponding vop2a, so no long
1435 ; immediate.
1436 ; The "s_mulk_i32" variant sets SCC to indicate overflow (which we don't care
1437 ; about here, but we need to indicate the clobbering).
1438 (define_insn "mulsi3"
1439 [(set (match_operand:SI 0 "register_operand" "= Sg,Sg, Sg, v")
1440 (mult:SI (match_operand:SI 1 "gcn_alu_operand" "%SgA, 0,SgA, v")
1441 (match_operand:SI 2 "gcn_alu_operand" " SgA, J, B,vASv")))
1442 (clobber (match_scratch:BI 3 "=X,cs, X, X"))]
1443 ""
1444 "@
1445 s_mul_i32\t%0, %1, %2
1446 s_mulk_i32\t%0, %2
1447 s_mul_i32\t%0, %1, %2
1448 v_mul_lo_u32\t%0, %1, %2"
1449 [(set_attr "type" "sop2,sopk,sop2,vop3a")
1450 (set_attr "length" "4,4,8,4")])
1451
1452 (define_code_iterator any_extend [sign_extend zero_extend])
1453 (define_code_attr sgnsuffix [(sign_extend "%i") (zero_extend "%u")])
1454 (define_code_attr su [(sign_extend "s") (zero_extend "u")])
1455 (define_code_attr u [(sign_extend "") (zero_extend "u")])
1456 (define_code_attr iu [(sign_extend "i") (zero_extend "u")])
1457 (define_code_attr e [(sign_extend "e") (zero_extend "")])
1458
1459 (define_expand "<su>mulsi3_highpart"
1460 [(set (match_operand:SI 0 "register_operand" "")
1461 (truncate:SI
1462 (lshiftrt:DI
1463 (mult:DI
1464 (any_extend:DI
1465 (match_operand:SI 1 "register_operand" ""))
1466 (any_extend:DI
1467 (match_operand:SI 2 "gcn_alu_operand" "")))
1468 (const_int 32))))]
1469 ""
1470 {
1471 if (REG_P (operands[2]))
1472 emit_insn (gen_<su>mulsi3_highpart_reg (operands[0], operands[1],
1473 operands[2]));
1474 else
1475 emit_insn (gen_<su>mulsi3_highpart_imm (operands[0], operands[1],
1476 operands[2]));
1477
1478 DONE;
1479 })
1480
1481 (define_insn "<su>mulsi3_highpart_reg"
1482 [(set (match_operand:SI 0 "register_operand" "=Sg, v")
1483 (truncate:SI
1484 (lshiftrt:DI
1485 (mult:DI
1486 (any_extend:DI
1487 (match_operand:SI 1 "register_operand" "%Sg, v"))
1488 (any_extend:DI
1489 (match_operand:SI 2 "register_operand" "Sg,vSv")))
1490 (const_int 32))))]
1491 ""
1492 "@
1493 s_mul_hi<sgnsuffix>0\t%0, %1, %2
1494 v_mul_hi<sgnsuffix>0\t%0, %2, %1"
1495 [(set_attr "type" "sop2,vop3a")
1496 (set_attr "length" "4,8")])
1497
1498 (define_insn "<su>mulsi3_highpart_imm"
1499 [(set (match_operand:SI 0 "register_operand" "=Sg,Sg,v")
1500 (truncate:SI
1501 (lshiftrt:DI
1502 (mult:DI
1503 (any_extend:DI
1504 (match_operand:SI 1 "register_operand" "Sg,Sg,v"))
1505 (match_operand:DI 2 "gcn_32bit_immediate_operand" "A, B,A"))
1506 (const_int 32))))]
1507 ""
1508 "@
1509 s_mul_hi<sgnsuffix>0\t%0, %1, %2
1510 s_mul_hi<sgnsuffix>0\t%0, %1, %2
1511 v_mul_hi<sgnsuffix>0\t%0, %2, %1"
1512 [(set_attr "type" "sop2,sop2,vop3a")
1513 (set_attr "length" "4,8,8")])
1514
1515 (define_expand "<su>mulsidi3"
1516 [(set (match_operand:DI 0 "register_operand" "")
1517 (mult:DI (any_extend:DI
1518 (match_operand:SI 1 "register_operand" ""))
1519 (any_extend:DI
1520 (match_operand:SI 2 "nonmemory_operand" ""))))]
1521 ""
1522 {
1523 if (REG_P (operands[2]))
1524 emit_insn (gen_<su>mulsidi3_reg (operands[0], operands[1], operands[2]));
1525 else
1526 emit_insn (gen_<su>mulsidi3_imm (operands[0], operands[1], operands[2]));
1527
1528 DONE;
1529 })
1530
1531 (define_insn_and_split "<su>mulsidi3_reg"
1532 [(set (match_operand:DI 0 "register_operand" "=&Sg, &v")
1533 (mult:DI (any_extend:DI
1534 (match_operand:SI 1 "register_operand" "%Sg, v"))
1535 (any_extend:DI
1536 (match_operand:SI 2 "register_operand" "Sg,vSv"))))]
1537 ""
1538 "#"
1539 "reload_completed"
1540 [(const_int 0)]
1541 {
1542 rtx dstlo = gen_lowpart (SImode, operands[0]);
1543 rtx dsthi = gen_highpart_mode (SImode, DImode, operands[0]);
1544 emit_insn (gen_mulsi3 (dstlo, operands[1], operands[2]));
1545 emit_insn (gen_<su>mulsi3_highpart (dsthi, operands[1], operands[2]));
1546 DONE;
1547 })
1548
1549 (define_insn_and_split "<su>mulsidi3_imm"
1550 [(set (match_operand:DI 0 "register_operand" "=&Sg,&Sg,&v")
1551 (mult:DI (any_extend:DI
1552 (match_operand:SI 1 "register_operand" "Sg, Sg, v"))
1553 (match_operand:DI 2 "gcn_32bit_immediate_operand"
1554 "A, B, A")))]
1555 ""
1556 "#"
1557 "&& reload_completed"
1558 [(const_int 0)]
1559 {
1560 rtx dstlo = gen_lowpart (SImode, operands[0]);
1561 rtx dsthi = gen_highpart_mode (SImode, DImode, operands[0]);
1562 emit_insn (gen_mulsi3 (dstlo, operands[1], operands[2]));
1563 emit_insn (gen_<su>mulsi3_highpart (dsthi, operands[1], operands[2]));
1564 DONE;
1565 })
1566
1567 (define_insn_and_split "muldi3"
1568 [(set (match_operand:DI 0 "register_operand" "=&Sg,&Sg, &v,&v")
1569 (mult:DI (match_operand:DI 1 "register_operand" "%Sg, Sg, v, v")
1570 (match_operand:DI 2 "nonmemory_operand" "Sg, i,vSv, A")))
1571 (clobber (match_scratch:SI 3 "=&Sg,&Sg,&v,&v"))
1572 (clobber (match_scratch:BI 4 "=cs, cs, X, X"))
1573 (clobber (match_scratch:DI 5 "=X, X,cV,cV"))]
1574 ""
1575 "#"
1576 "reload_completed"
1577 [(const_int 0)]
1578 {
1579 rtx tmp = operands[3];
1580 rtx dsthi = gen_highpart_mode (SImode, DImode, operands[0]);
1581 rtx op1lo = gcn_operand_part (DImode, operands[1], 0);
1582 rtx op1hi = gcn_operand_part (DImode, operands[1], 1);
1583 rtx op2lo = gcn_operand_part (DImode, operands[2], 0);
1584 rtx op2hi = gcn_operand_part (DImode, operands[2], 1);
1585 emit_insn (gen_umulsidi3 (operands[0], op1lo, op2lo));
1586 emit_insn (gen_mulsi3 (tmp, op1lo, op2hi));
1587 rtx add = gen_rtx_SET (dsthi, gen_rtx_PLUS (SImode, dsthi, tmp));
1588 rtx clob1 = gen_rtx_CLOBBER (VOIDmode, operands[4]);
1589 rtx clob2 = gen_rtx_CLOBBER (VOIDmode, operands[5]);
1590 add = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (3, add, clob1, clob2));
1591 emit_insn (add);
1592 emit_insn (gen_mulsi3 (tmp, op1hi, op2lo));
1593 add = gen_rtx_SET (dsthi, gen_rtx_PLUS (SImode, dsthi, tmp));
1594 clob1 = gen_rtx_CLOBBER (VOIDmode, operands[4]);
1595 clob2 = gen_rtx_CLOBBER (VOIDmode, operands[5]);
1596 add = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (3, add, clob1, clob2));
1597 emit_insn (add);
1598 DONE;
1599 })
1600
1601 (define_insn "<u>mulhisi3"
1602 [(set (match_operand:SI 0 "register_operand" "=v")
1603 (mult:SI
1604 (any_extend:SI (match_operand:HI 1 "register_operand" "%v"))
1605 (any_extend:SI (match_operand:HI 2 "register_operand" " v"))))]
1606 "TARGET_SDWA"
1607 "v_mul_<iu>32_<iu>24_sdwa\t%0, %<e>1, %<e>2 src0_sel:WORD_0 src1_sel:WORD_0"
1608 [(set_attr "type" "vop_sdwa")
1609 (set_attr "length" "8")])
1610
1611 (define_insn "<u>mulqihi3_scalar"
1612 [(set (match_operand:HI 0 "register_operand" "=v")
1613 (mult:HI
1614 (any_extend:HI (match_operand:QI 1 "register_operand" "%v"))
1615 (any_extend:HI (match_operand:QI 2 "register_operand" " v"))))]
1616 "TARGET_SDWA"
1617 "v_mul_<iu>32_<iu>24_sdwa\t%0, %<e>1, %<e>2 src0_sel:BYTE_0 src1_sel:BYTE_0"
1618 [(set_attr "type" "vop_sdwa")
1619 (set_attr "length" "8")])
1620
1621 ;; }}}
1622 ;; {{{ ALU: generic 32-bit unop
1623
1624 (define_code_iterator bitunop [not popcount])
1625 (define_code_attr popcount_extra_op [(not "") (popcount ", 0")])
1626
1627 (define_insn "<expander>si2"
1628 [(set (match_operand:SI 0 "register_operand" "=Sg, v")
1629 (bitunop:SI
1630 (match_operand:SI 1 "gcn_alu_operand" "SgB,vSvB")))
1631 (clobber (match_scratch:BI 2 "=cs, X"))]
1632 ""
1633 "@
1634 s_<s_mnemonic>0\t%0, %1
1635 v_<mnemonic>0\t%0, %1<popcount_extra_op>"
1636 [(set_attr "type" "sop1,vop1")
1637 (set_attr "length" "8")])
1638
1639 (define_code_iterator countzeros [clz ctz])
1640
1641 (define_insn "<expander>si2"
1642 [(set (match_operand:SI 0 "register_operand" "=Sg,Sg")
1643 (countzeros:SI
1644 (match_operand:SI 1 "gcn_alu_operand" "SgA, B")))]
1645 ""
1646 "s_<s_mnemonic>1\t%0, %1"
1647 [(set_attr "type" "sop1")
1648 (set_attr "length" "4,8")])
1649
1650 ; The truncate ensures that a constant passed to operand 1 is treated as DImode
1651 (define_insn "<expander>di2"
1652 [(set (match_operand:SI 0 "register_operand" "=Sg,Sg")
1653 (truncate:SI
1654 (countzeros:DI
1655 (match_operand:DI 1 "gcn_alu_operand" "SgA, B"))))]
1656 ""
1657 "s_<s_mnemonic>1\t%0, %1"
1658 [(set_attr "type" "sop1")
1659 (set_attr "length" "4,8")])
1660
1661 (define_insn "gcn_flbit<mode>_int"
1662 [(set (match_operand:SI 0 "register_operand" "=Sg,Sg")
1663 (unspec:SI [(match_operand:SIDI 1 "gcn_alu_operand" "SgA, B")]
1664 UNSPEC_FLBIT_INT))]
1665 ""
1666 {
1667 if (<MODE>mode == SImode)
1668 return "s_flbit_i32\t%0, %1";
1669 else
1670 return "s_flbit_i32_i64\t%0, %1";
1671 }
1672 [(set_attr "type" "sop1")
1673 (set_attr "length" "4,8")])
1674
1675 (define_expand "clrsb<mode>2"
1676 [(set (match_operand:SI 0 "register_operand" "")
1677 (clrsb:SI (match_operand:SIDI 1 "gcn_alu_operand" "")))]
1678 ""
1679 {
1680 rtx tmp = gen_reg_rtx (SImode);
1681 /* FLBIT_I* counts sign or zero bits at the most-significant end of the
1682 input register (and returns -1 for 0/-1 inputs). We want the number of
1683 *redundant* bits (i.e. that value minus one), and an answer of 31/63 for
1684 0/-1 inputs. We can do that in three instructions... */
1685 emit_insn (gen_gcn_flbit<mode>_int (tmp, operands[1]));
1686 emit_insn (gen_uminsi3 (tmp, tmp,
1687 gen_int_mode (GET_MODE_BITSIZE (<MODE>mode),
1688 SImode)));
1689 /* If we put this last, it can potentially be folded into a subsequent
1690 arithmetic operation. */
1691 emit_insn (gen_subsi3 (operands[0], tmp, const1_rtx));
1692 DONE;
1693 })
1694
1695 ;; }}}
1696 ;; {{{ ALU: generic 32-bit binop
1697
1698 ; No plus and mult - they have variant with 16bit immediate
1699 ; and thus are defined later.
1700 (define_code_iterator binop [and ior xor smin smax umin umax
1701 ashift lshiftrt ashiftrt])
1702 (define_code_iterator vec_and_scalar_com [and ior xor smin smax umin umax])
1703 (define_code_iterator vec_and_scalar_nocom [ashift lshiftrt ashiftrt])
1704
1705 (define_insn "<expander>si3"
1706 [(set (match_operand:SI 0 "gcn_valu_dst_operand" "= Sg, v,RD")
1707 (vec_and_scalar_com:SI
1708 (match_operand:SI 1 "gcn_valu_src0_operand" "%SgA,vSvB, 0")
1709 (match_operand:SI 2 "gcn_alu_operand" " SgB, v, v")))
1710 (clobber (match_scratch:BI 3 "= cs, X, X"))]
1711 ""
1712 "@
1713 s_<mnemonic>0\t%0, %1, %2
1714 v_<mnemonic>0\t%0, %1, %2
1715 ds_<mnemonic>0\t%A0, %2%O0"
1716 [(set_attr "type" "sop2,vop2,ds")
1717 (set_attr "length" "8")])
1718
1719 (define_insn "<expander>si3"
1720 [(set (match_operand:SI 0 "register_operand" "=Sg, Sg, v")
1721 (vec_and_scalar_nocom:SI
1722 (match_operand:SI 1 "gcn_alu_operand" "SgB,SgA, v")
1723 (match_operand:SI 2 "gcn_alu_operand" "SgA,SgB,vSvB")))
1724 (clobber (match_scratch:BI 3 "=cs, cs, X"))]
1725 ""
1726 "@
1727 s_<mnemonic>0\t%0, %1, %2
1728 s_<mnemonic>0\t%0, %1, %2
1729 v_<revmnemonic>0\t%0, %2, %1"
1730 [(set_attr "type" "sop2,sop2,vop2")
1731 (set_attr "length" "8")])
1732
1733 (define_expand "<expander>si3_scc"
1734 [(parallel [(set (match_operand:SI 0 "gcn_valu_dst_operand")
1735 (binop:SI
1736 (match_operand:SI 1 "gcn_valu_src0_operand")
1737 (match_operand:SI 2 "gcn_alu_operand")))
1738 (clobber (reg:BI SCC_REG))])]
1739 ""
1740 {})
1741
1742 ;; }}}
1743 ;; {{{ ALU: generic 64-bit
1744
1745 (define_insn_and_split "one_cmpldi2"
1746 [(set (match_operand:DI 0 "register_operand" "=Sg, v")
1747 (not:DI (match_operand:DI 1 "gcn_alu_operand" "SgA,vSvDB")))
1748 (clobber (match_scratch:BI 2 "=cs, X"))]
1749 ""
1750 "#"
1751 "reload_completed"
1752 [(parallel [(set (match_dup 3) (not:SI (match_dup 4)))
1753 (clobber (match_dup 2))])
1754 (parallel [(set (match_dup 5) (not:SI (match_dup 6)))
1755 (clobber (match_dup 2))])]
1756 {
1757 operands[3] = gcn_operand_part (DImode, operands[0], 0);
1758 operands[4] = gcn_operand_part (DImode, operands[1], 0);
1759 operands[5] = gcn_operand_part (DImode, operands[0], 1);
1760 operands[6] = gcn_operand_part (DImode, operands[1], 1);
1761 }
1762 [(set_attr "type" "mult")]
1763 )
1764
1765 (define_code_iterator vec_and_scalar64_com [and ior xor])
1766
1767 (define_insn_and_split "<expander>di3"
1768 [(set (match_operand:DI 0 "register_operand" "= Sg, v")
1769 (vec_and_scalar64_com:DI
1770 (match_operand:DI 1 "gcn_alu_operand" "%SgA,vSvDB")
1771 (match_operand:DI 2 "gcn_alu_operand" " SgC, v")))
1772 (clobber (match_scratch:BI 3 "= cs, X"))]
1773 ""
1774 "@
1775 s_<mnemonic>0\t%0, %1, %2
1776 #"
1777 "reload_completed && gcn_vgpr_register_operand (operands[0], DImode)"
1778 [(parallel [(set (match_dup 4)
1779 (vec_and_scalar64_com:SI (match_dup 5) (match_dup 6)))
1780 (clobber (match_dup 3))])
1781 (parallel [(set (match_dup 7)
1782 (vec_and_scalar64_com:SI (match_dup 8) (match_dup 9)))
1783 (clobber (match_dup 3))])]
1784 {
1785 operands[4] = gcn_operand_part (DImode, operands[0], 0);
1786 operands[5] = gcn_operand_part (DImode, operands[1], 0);
1787 operands[6] = gcn_operand_part (DImode, operands[2], 0);
1788 operands[7] = gcn_operand_part (DImode, operands[0], 1);
1789 operands[8] = gcn_operand_part (DImode, operands[1], 1);
1790 operands[9] = gcn_operand_part (DImode, operands[2], 1);
1791 }
1792 [(set_attr "type" "sop2,vop2")
1793 (set_attr "length" "8")])
1794
1795 (define_insn "<expander>di3"
1796 [(set (match_operand:DI 0 "register_operand" "=Sg, Sg, v")
1797 (vec_and_scalar_nocom:DI
1798 (match_operand:DI 1 "gcn_alu_operand" "SgC,SgA, v")
1799 (match_operand:SI 2 "gcn_alu_operand" "SgA,SgC,vSvC")))
1800 (clobber (match_scratch:BI 3 "=cs, cs, X"))]
1801 ""
1802 "@
1803 s_<mnemonic>0\t%0, %1, %2
1804 s_<mnemonic>0\t%0, %1, %2
1805 v_<revmnemonic>0\t%0, %2, %1"
1806 [(set_attr "type" "sop2,sop2,vop2")
1807 (set_attr "length" "8")])
1808
1809 ;; }}}
1810 ;; {{{ ALU: generic 128-bit binop
1811
1812 ; TImode shifts can't be synthesized by the middle-end
1813 (define_expand "<expander>ti3"
1814 [(set (match_operand:TI 0 "register_operand")
1815 (vec_and_scalar_nocom:TI
1816 (match_operand:TI 1 "gcn_alu_operand")
1817 (match_operand:SI 2 "gcn_alu_operand")))]
1818 ""
1819 {
1820 rtx dest = operands[0];
1821 rtx src = operands[1];
1822 rtx shift = operands[2];
1823
1824 enum {ashr, lshr, ashl} shiftop = <expander>;
1825 rtx (*inverse_shift_fn) (rtx, rtx, rtx)
1826 = (shiftop == ashl ? gen_lshrdi3 : gen_ashldi3);
1827 rtx (*logical_shift_fn) (rtx, rtx, rtx)
1828 = (shiftop == ashl ? gen_ashldi3 : gen_lshrdi3);
1829
1830 /* We shift "from" one subreg "to" the other, according to shiftop. */
1831 int from = (shiftop == ashl ? 0 : 8);
1832 int to = (shiftop == ashl ? 8 : 0);
1833 rtx destfrom = simplify_gen_subreg (DImode, dest, TImode, from);
1834 rtx destto = simplify_gen_subreg (DImode, dest, TImode, to);
1835 rtx srcfrom = simplify_gen_subreg (DImode, src, TImode, from);
1836 rtx srcto = simplify_gen_subreg (DImode, src, TImode, to);
1837
1838 int shiftval = (CONST_INT_P (shift) ? INTVAL (shift) : -1);
1839 enum {RUNTIME, ZERO, SMALL, LARGE} shiftcomparison
1840 = (!CONST_INT_P (shift) ? RUNTIME
1841 : shiftval == 0 ? ZERO
1842 : shiftval < 64 ? SMALL
1843 : LARGE);
1844
1845 rtx large_label, zero_label, exit_label;
1846
1847 if (shiftcomparison == RUNTIME)
1848 {
1849 zero_label = gen_label_rtx ();
1850 large_label = gen_label_rtx ();
1851 exit_label = gen_label_rtx ();
1852
1853 rtx cond = gen_rtx_EQ (VOIDmode, shift, const0_rtx);
1854 emit_insn (gen_cbranchsi4 (cond, shift, const0_rtx, zero_label));
1855
1856 rtx sixtyfour = GEN_INT (64);
1857 cond = gen_rtx_GE (VOIDmode, shift, sixtyfour);
1858 emit_insn (gen_cbranchsi4 (cond, shift, sixtyfour, large_label));
1859 }
1860
1861 if (shiftcomparison == SMALL || shiftcomparison == RUNTIME)
1862 {
1863 /* Shift both parts by the same amount, then patch in the bits that
1864 cross the boundary.
1865 This does *not* work for zero-length shifts. */
1866 rtx tmpto1 = gen_reg_rtx (DImode);
1867 rtx tmpto2 = gen_reg_rtx (DImode);
1868 emit_insn (gen_<expander>di3 (destfrom, srcfrom, shift));
1869 emit_insn (logical_shift_fn (tmpto1, srcto, shift));
1870 rtx lessershiftval = gen_reg_rtx (SImode);
1871 emit_insn (gen_subsi3 (lessershiftval, GEN_INT (64), shift));
1872 emit_insn (inverse_shift_fn (tmpto2, srcfrom, lessershiftval));
1873 emit_insn (gen_iordi3 (destto, tmpto1, tmpto2));
1874 }
1875
1876 if (shiftcomparison == RUNTIME)
1877 {
1878 emit_jump_insn (gen_jump (exit_label));
1879 emit_barrier ();
1880
1881 emit_label (zero_label);
1882 }
1883
1884 if (shiftcomparison == ZERO || shiftcomparison == RUNTIME)
1885 emit_move_insn (dest, src);
1886
1887 if (shiftcomparison == RUNTIME)
1888 {
1889 emit_jump_insn (gen_jump (exit_label));
1890 emit_barrier ();
1891
1892 emit_label (large_label);
1893 }
1894
1895 if (shiftcomparison == LARGE || shiftcomparison == RUNTIME)
1896 {
1897 /* Do the shift within one part, and set the other part appropriately.
1898 Shifts of 128+ bits are an error. */
1899 rtx lessershiftval = gen_reg_rtx (SImode);
1900 emit_insn (gen_subsi3 (lessershiftval, shift, GEN_INT (64)));
1901 emit_insn (gen_<expander>di3 (destto, srcfrom, lessershiftval));
1902 if (shiftop == ashr)
1903 emit_insn (gen_ashrdi3 (destfrom, srcfrom, GEN_INT (63)));
1904 else
1905 emit_move_insn (destfrom, const0_rtx);
1906 }
1907
1908 if (shiftcomparison == RUNTIME)
1909 emit_label (exit_label);
1910
1911 DONE;
1912 })
1913
1914 ;; }}}
1915 ;; {{{ Atomics
1916
1917 ; Each compute unit has it's own L1 cache. The L2 cache is shared between
1918 ; all the compute units. Any load or store instruction can skip L1 and
1919 ; access L2 directly using the "glc" flag. Atomic instructions also skip
1920 ; L1. The L1 cache can be flushed and invalidated using instructions.
1921 ;
1922 ; Therefore, in order for "acquire" and "release" atomic modes to work
1923 ; correctly across compute units we must flush before each "release"
1924 ; and invalidate the cache after each "acquire". It might seem like
1925 ; invalidation could be safely done before an "acquire", but since each
1926 ; compute unit can run up to 40 threads simultaneously, all reading values
1927 ; into the L1 cache, this is not actually safe.
1928 ;
1929 ; Additionally, scalar flat instructions access L2 via a different cache
1930 ; (the "constant cache"), so they have separate constrol instructions. We
1931 ; do not attempt to invalidate both caches at once; instead, atomics
1932 ; operating on scalar flat pointers will flush the constant cache, and
1933 ; atomics operating on flat or global pointers will flush L1. It is up to
1934 ; the programmer to get this right.
1935
1936 (define_code_iterator atomicops [plus minus and ior xor])
1937 (define_mode_attr X [(SI "") (DI "_X2")])
1938
1939 ;; TODO compare_and_swap test_and_set inc dec
1940 ;; Hardware also supports min and max, but GCC does not.
1941
1942 (define_expand "memory_barrier"
1943 [(set (match_dup 0)
1944 (unspec:BLK [(match_dup 0)] UNSPEC_MEMORY_BARRIER))]
1945 ""
1946 {
1947 operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
1948 MEM_VOLATILE_P (operands[0]) = 1;
1949 })
1950
1951 (define_insn "*memory_barrier"
1952 [(set (match_operand:BLK 0)
1953 (unspec:BLK [(match_dup 0)] UNSPEC_MEMORY_BARRIER))]
1954 "TARGET_WBINVL1_CACHE"
1955 "buffer_wbinvl1_vol"
1956 [(set_attr "type" "mubuf")
1957 (set_attr "length" "4")])
1958
1959 (define_insn "*memory_barrier"
1960 [(set (match_operand:BLK 0)
1961 (unspec:BLK [(match_dup 0)] UNSPEC_MEMORY_BARRIER))]
1962 "TARGET_GLn_CACHE"
1963 "buffer_gl1_inv\;buffer_gl0_inv"
1964 [(set_attr "type" "mult")
1965 (set_attr "length" "8")])
1966
1967 ; FIXME: These patterns have been disabled as they do not seem to work
1968 ; reliably - they can cause hangs or incorrect results.
1969 ; TODO: flush caches according to memory model
1970 (define_insn "atomic_fetch_<bare_mnemonic><mode>"
1971 [(set (match_operand:SIDI 0 "register_operand" "=Sm, v, v")
1972 (match_operand:SIDI 1 "memory_operand" "+RS,RF,RM"))
1973 (set (match_dup 1)
1974 (unspec_volatile:SIDI
1975 [(atomicops:SIDI
1976 (match_dup 1)
1977 (match_operand:SIDI 2 "register_operand" " Sm, v, v"))]
1978 UNSPECV_ATOMIC))
1979 (use (match_operand 3 "const_int_operand"))]
1980 "0 /* Disabled. */"
1981 "@
1982 s_atomic_<bare_mnemonic><X>\t%0, %1, %2 glc\;s_waitcnt\tlgkmcnt(0)
1983 flat_atomic_<bare_mnemonic><X>\t%0, %1, %2 glc\;s_waitcnt\t0
1984 global_atomic_<bare_mnemonic><X>\t%0, %A1, %2%O1 glc\;s_waitcnt\tvmcnt(0)"
1985 [(set_attr "type" "smem,flat,flat")
1986 (set_attr "length" "12")])
1987
1988 ; FIXME: These patterns are disabled because the instructions don't
1989 ; seem to work as advertised. Specifically, OMP "team distribute"
1990 ; reductions apparently "lose" some of the writes, similar to what
1991 ; you might expect from a concurrent non-atomic read-modify-write.
1992 ; TODO: flush caches according to memory model
1993 (define_insn "atomic_<bare_mnemonic><mode>"
1994 [(set (match_operand:SIDI 0 "memory_operand" "+RS,RF,RM")
1995 (unspec_volatile:SIDI
1996 [(atomicops:SIDI
1997 (match_dup 0)
1998 (match_operand:SIDI 1 "register_operand" " Sm, v, v"))]
1999 UNSPECV_ATOMIC))
2000 (use (match_operand 2 "const_int_operand"))]
2001 "0 /* Disabled. */"
2002 "@
2003 s_atomic_<bare_mnemonic><X>\t%0, %1\;s_waitcnt\tlgkmcnt(0)
2004 flat_atomic_<bare_mnemonic><X>\t%0, %1\;s_waitcnt\t0
2005 global_atomic_<bare_mnemonic><X>\t%A0, %1%O0\;s_waitcnt\tvmcnt(0)"
2006 [(set_attr "type" "smem,flat,flat")
2007 (set_attr "length" "12")])
2008
2009 (define_mode_attr x2 [(SI "DI") (DI "TI")])
2010 (define_mode_attr size [(SI "4") (DI "8")])
2011 (define_mode_attr bitsize [(SI "32") (DI "64")])
2012
2013 (define_expand "sync_compare_and_swap<mode>"
2014 [(match_operand:SIDI 0 "register_operand")
2015 (match_operand:SIDI 1 "memory_operand")
2016 (match_operand:SIDI 2 "register_operand")
2017 (match_operand:SIDI 3 "register_operand")]
2018 ""
2019 {
2020 if (MEM_ADDR_SPACE (operands[1]) == ADDR_SPACE_LDS)
2021 {
2022 emit_insn (gen_sync_compare_and_swap<mode>_lds_insn (operands[0],
2023 operands[1],
2024 operands[2],
2025 operands[3]));
2026 DONE;
2027 }
2028
2029 /* Operands 2 and 3 must be placed in consecutive registers, and passed
2030 as a combined value. */
2031 rtx src_cmp = gen_reg_rtx (<x2>mode);
2032 emit_move_insn (gen_rtx_SUBREG (<MODE>mode, src_cmp, 0), operands[3]);
2033 emit_move_insn (gen_rtx_SUBREG (<MODE>mode, src_cmp, <size>), operands[2]);
2034 emit_insn (gen_sync_compare_and_swap<mode>_insn (operands[0],
2035 operands[1],
2036 src_cmp));
2037 DONE;
2038 })
2039
2040 (define_insn "sync_compare_and_swap<mode>_insn"
2041 [(set (match_operand:SIDI 0 "register_operand" "=Sm, v, v")
2042 (match_operand:SIDI 1 "memory_operand" "+RS,RF,RM"))
2043 (set (match_dup 1)
2044 (unspec_volatile:SIDI
2045 [(match_operand:<x2> 2 "register_operand" " Sm, v, v")]
2046 UNSPECV_ATOMIC))]
2047 ""
2048 "@
2049 s_atomic_cmpswap<X>\t%0, %1, %2 glc\;s_waitcnt\tlgkmcnt(0)
2050 flat_atomic_cmpswap<X>\t%0, %1, %2 glc\;s_waitcnt\t0
2051 global_atomic_cmpswap<X>\t%0, %A1, %2%O1 glc\;s_waitcnt\tvmcnt(0)"
2052 [(set_attr "type" "smem,flat,flat")
2053 (set_attr "length" "12")
2054 (set_attr "delayeduse" "*,yes,yes")])
2055
2056 (define_insn "sync_compare_and_swap<mode>_lds_insn"
2057 [(set (match_operand:SIDI 0 "register_operand" "= v")
2058 (unspec_volatile:SIDI
2059 [(match_operand:SIDI 1 "memory_operand" "+RL")]
2060 UNSPECV_ATOMIC))
2061 (set (match_dup 1)
2062 (unspec_volatile:SIDI
2063 [(match_operand:SIDI 2 "register_operand" " v")
2064 (match_operand:SIDI 3 "register_operand" " v")]
2065 UNSPECV_ATOMIC))]
2066 ""
2067 {
2068 if (TARGET_RDNA3)
2069 return "ds_cmpstore_rtn_b<bitsize> %0, %1, %3, %2\;s_waitcnt\tlgkmcnt(0)";
2070 else
2071 return "ds_cmpst_rtn_b<bitsize> %0, %1, %2, %3\;s_waitcnt\tlgkmcnt(0)";
2072 }
2073 [(set_attr "type" "ds")
2074 (set_attr "length" "12")])
2075
2076 (define_insn "atomic_load<mode>"
2077 [(set (match_operand:SIDI 0 "register_operand" "=Sm, v, v")
2078 (unspec_volatile:SIDI
2079 [(match_operand:SIDI 1 "memory_operand" " RS,RF,RM")]
2080 UNSPECV_ATOMIC))
2081 (use (match_operand:SIDI 2 "immediate_operand" " i, i, i"))]
2082 ""
2083 {
2084 /* FIXME: RDNA cache instructions may be too conservative? */
2085 switch (INTVAL (operands[2]))
2086 {
2087 case MEMMODEL_RELAXED:
2088 switch (which_alternative)
2089 {
2090 case 0:
2091 return "s_load%o0\t%0, %A1 glc\;s_waitcnt\tlgkmcnt(0)";
2092 case 1:
2093 return (TARGET_RDNA2 /* Not GFX11. */
2094 ? "flat_load%o0\t%0, %A1%O1 glc dlc\;s_waitcnt\t0"
2095 : "flat_load%o0\t%0, %A1%O1 glc\;s_waitcnt\t0");
2096 case 2:
2097 return (TARGET_RDNA2 /* Not GFX11. */
2098 ? "global_load%o0\t%0, %A1%O1 glc dlc\;s_waitcnt\tvmcnt(0)"
2099 : "global_load%o0\t%0, %A1%O1 glc\;s_waitcnt\tvmcnt(0)");
2100 }
2101 break;
2102 case MEMMODEL_CONSUME:
2103 case MEMMODEL_ACQUIRE:
2104 case MEMMODEL_SYNC_ACQUIRE:
2105 switch (which_alternative)
2106 {
2107 case 0:
2108 return "s_load%o0\t%0, %A1 glc\;s_waitcnt\tlgkmcnt(0)\;"
2109 "s_dcache_wb_vol";
2110 case 1:
2111 return (TARGET_RDNA2
2112 ? "flat_load%o0\t%0, %A1%O1 glc dlc\;s_waitcnt\t0\;"
2113 "buffer_gl1_inv\;buffer_gl0_inv"
2114 : TARGET_RDNA3
2115 ? "flat_load%o0\t%0, %A1%O1 glc\;s_waitcnt\t0\;"
2116 "buffer_gl1_inv\;buffer_gl0_inv"
2117 : "flat_load%o0\t%0, %A1%O1 glc\;s_waitcnt\t0\;"
2118 "buffer_wbinvl1_vol");
2119 case 2:
2120 return (TARGET_RDNA2
2121 ? "global_load%o0\t%0, %A1%O1 glc dlc\;s_waitcnt\tvmcnt(0)\;"
2122 "buffer_gl1_inv\;buffer_gl0_inv"
2123 : TARGET_RDNA3
2124 ? "global_load%o0\t%0, %A1%O1 glc\;s_waitcnt\tvmcnt(0)\;"
2125 "buffer_gl1_inv\;buffer_gl0_inv"
2126 : "global_load%o0\t%0, %A1%O1 glc\;s_waitcnt\tvmcnt(0)\;"
2127 "buffer_wbinvl1_vol");
2128 }
2129 break;
2130 case MEMMODEL_ACQ_REL:
2131 case MEMMODEL_SEQ_CST:
2132 case MEMMODEL_SYNC_SEQ_CST:
2133 switch (which_alternative)
2134 {
2135 case 0:
2136 return "s_dcache_wb_vol\;s_load%o0\t%0, %A1 glc\;"
2137 "s_waitcnt\tlgkmcnt(0)\;s_dcache_inv_vol";
2138 case 1:
2139 return (TARGET_RDNA2
2140 ? "buffer_gl1_inv\;buffer_gl0_inv\;flat_load%o0\t%0, %A1%O1 glc dlc\;"
2141 "s_waitcnt\t0\;buffer_gl1_inv\;buffer_gl0_inv"
2142 : TARGET_RDNA3
2143 ? "buffer_gl1_inv\;buffer_gl0_inv\;flat_load%o0\t%0, %A1%O1 glc\;"
2144 "s_waitcnt\t0\;buffer_gl1_inv\;buffer_gl0_inv"
2145 : "buffer_wbinvl1_vol\;flat_load%o0\t%0, %A1%O1 glc\;"
2146 "s_waitcnt\t0\;buffer_wbinvl1_vol");
2147 case 2:
2148 return (TARGET_RDNA2
2149 ? "buffer_gl1_inv\;buffer_gl0_inv\;global_load%o0\t%0, %A1%O1 glc dlc\;"
2150 "s_waitcnt\tvmcnt(0)\;buffer_gl1_inv\;buffer_gl0_inv"
2151 : TARGET_RDNA3
2152 ? "buffer_gl1_inv\;buffer_gl0_inv\;global_load%o0\t%0, %A1%O1 glc\;"
2153 "s_waitcnt\tvmcnt(0)\;buffer_gl1_inv\;buffer_gl0_inv"
2154 : "buffer_wbinvl1_vol\;global_load%o0\t%0, %A1%O1 glc\;"
2155 "s_waitcnt\tvmcnt(0)\;buffer_wbinvl1_vol");
2156 }
2157 break;
2158 }
2159 gcc_unreachable ();
2160 }
2161 [(set_attr "type" "smem,flat,flat")
2162 (set_attr "length" "28")
2163 (set_attr "rdna" "no,*,*")])
2164
2165 (define_insn "atomic_store<mode>"
2166 [(set (match_operand:SIDI 0 "memory_operand" "=RS,RF,RM")
2167 (unspec_volatile:SIDI
2168 [(match_operand:SIDI 1 "register_operand" " Sm, v, v")]
2169 UNSPECV_ATOMIC))
2170 (use (match_operand:SIDI 2 "immediate_operand" " i, i, i"))]
2171 ""
2172 {
2173 switch (INTVAL (operands[2]))
2174 {
2175 case MEMMODEL_RELAXED:
2176 switch (which_alternative)
2177 {
2178 case 0:
2179 return "s_store%o1\t%1, %A0 glc\;s_waitcnt\tlgkmcnt(0)";
2180 case 1:
2181 return "flat_store%o1\t%A0, %1%O0 glc\;s_waitcnt\t0";
2182 case 2:
2183 return "global_store%o1\t%A0, %1%O0 glc\;s_waitcnt\tvmcnt(0)";
2184 }
2185 break;
2186 case MEMMODEL_RELEASE:
2187 case MEMMODEL_SYNC_RELEASE:
2188 switch (which_alternative)
2189 {
2190 case 0:
2191 return "s_dcache_wb_vol\;s_store%o1\t%1, %A0 glc";
2192 case 1:
2193 return (TARGET_GLn_CACHE
2194 ? "buffer_gl1_inv\;buffer_gl0_inv\;flat_store%o1\t%A0, %1%O0 glc"
2195 : TARGET_WBINVL1_CACHE
2196 ? "buffer_wbinvl1_vol\;flat_store%o1\t%A0, %1%O0 glc"
2197 : "error: cache architectire unspecified");
2198 case 2:
2199 return (TARGET_GLn_CACHE
2200 ? "buffer_gl1_inv\;buffer_gl0_inv\;global_store%o1\t%A0, %1%O0 glc"
2201 : TARGET_WBINVL1_CACHE
2202 ? "buffer_wbinvl1_vol\;global_store%o1\t%A0, %1%O0 glc"
2203 : "error: cache architecture unspecified");
2204 }
2205 break;
2206 case MEMMODEL_ACQ_REL:
2207 case MEMMODEL_SEQ_CST:
2208 case MEMMODEL_SYNC_SEQ_CST:
2209 switch (which_alternative)
2210 {
2211 case 0:
2212 return "s_dcache_wb_vol\;s_store%o1\t%1, %A0 glc\;"
2213 "s_waitcnt\tlgkmcnt(0)\;s_dcache_inv_vol";
2214 case 1:
2215 return (TARGET_GLn_CACHE
2216 ? "buffer_gl1_inv\;buffer_gl0_inv\;flat_store%o1\t%A0, %1%O0 glc\;"
2217 "s_waitcnt\t0\;buffer_gl1_inv\;buffer_gl0_inv"
2218 : TARGET_WBINVL1_CACHE
2219 ? "buffer_wbinvl1_vol\;flat_store%o1\t%A0, %1%O0 glc\;"
2220 "s_waitcnt\t0\;buffer_wbinvl1_vol"
2221 : "error: cache architecture unspecified");
2222 case 2:
2223 return (TARGET_GLn_CACHE
2224 ? "buffer_gl1_inv\;buffer_gl0_inv\;global_store%o1\t%A0, %1%O0 glc\;"
2225 "s_waitcnt\tvmcnt(0)\;buffer_gl1_inv\;buffer_gl0_inv"
2226 : TARGET_WBINVL1_CACHE
2227 ? "buffer_wbinvl1_vol\;global_store%o1\t%A0, %1%O0 glc\;"
2228 "s_waitcnt\tvmcnt(0)\;buffer_wbinvl1_vol"
2229 : "error: cache architecture unspecified");
2230 }
2231 break;
2232 }
2233 gcc_unreachable ();
2234 }
2235 [(set_attr "type" "smem,flat,flat")
2236 (set_attr "length" "28")
2237 (set_attr "rdna" "no,*,*")])
2238
2239 (define_insn "atomic_exchange<mode>"
2240 [(set (match_operand:SIDI 0 "register_operand" "=Sm, v, v")
2241 (match_operand:SIDI 1 "memory_operand" "+RS,RF,RM"))
2242 (set (match_dup 1)
2243 (unspec_volatile:SIDI
2244 [(match_operand:SIDI 2 "register_operand" " Sm, v, v")]
2245 UNSPECV_ATOMIC))
2246 (use (match_operand 3 "immediate_operand"))]
2247 ""
2248 {
2249 switch (INTVAL (operands[3]))
2250 {
2251 case MEMMODEL_RELAXED:
2252 switch (which_alternative)
2253 {
2254 case 0:
2255 return "s_atomic_swap<X>\t%0, %1, %2 glc\;s_waitcnt\tlgkmcnt(0)";
2256 case 1:
2257 return "flat_atomic_swap<X>\t%0, %1, %2 glc\;s_waitcnt\t0";
2258 case 2:
2259 return "global_atomic_swap<X>\t%0, %A1, %2%O1 glc\;"
2260 "s_waitcnt\tvmcnt(0)";
2261 }
2262 break;
2263 case MEMMODEL_CONSUME:
2264 case MEMMODEL_ACQUIRE:
2265 case MEMMODEL_SYNC_ACQUIRE:
2266 switch (which_alternative)
2267 {
2268 case 0:
2269 return "s_atomic_swap<X>\t%0, %1, %2 glc\;s_waitcnt\tlgkmcnt(0)\;"
2270 "s_dcache_wb_vol\;s_dcache_inv_vol";
2271 case 1:
2272 return (TARGET_GLn_CACHE
2273 ? "flat_atomic_swap<X>\t%0, %1, %2 glc\;s_waitcnt\t0\;"
2274 "buffer_gl1_inv\;buffer_gl0_inv"
2275 : TARGET_WBINVL1_CACHE
2276 ? "flat_atomic_swap<X>\t%0, %1, %2 glc\;s_waitcnt\t0\;"
2277 "buffer_wbinvl1_vol"
2278 : "error: cache architecture unspecified");
2279 case 2:
2280 return (TARGET_GLn_CACHE
2281 ? "global_atomic_swap<X>\t%0, %A1, %2%O1 glc\;"
2282 "s_waitcnt\tvmcnt(0)\;buffer_gl1_inv\;buffer_gl0_inv"
2283 : TARGET_WBINVL1_CACHE
2284 ? "global_atomic_swap<X>\t%0, %A1, %2%O1 glc\;"
2285 "s_waitcnt\tvmcnt(0)\;buffer_wbinvl1_vol"
2286 : "error: cache architecture unspecified");
2287 }
2288 break;
2289 case MEMMODEL_RELEASE:
2290 case MEMMODEL_SYNC_RELEASE:
2291 switch (which_alternative)
2292 {
2293 case 0:
2294 return "s_dcache_wb_vol\;s_atomic_swap<X>\t%0, %1, %2 glc\;"
2295 "s_waitcnt\tlgkmcnt(0)";
2296 case 1:
2297 return (TARGET_GLn_CACHE
2298 ? "buffer_gl1_inv\;buffer_gl0_inv\;flat_atomic_swap<X>\t%0, %1, %2 glc\;"
2299 "s_waitcnt\t0"
2300 : TARGET_WBINVL1_CACHE
2301 ? "buffer_wbinvl1_vol\;flat_atomic_swap<X>\t%0, %1, %2 glc\;"
2302 "s_waitcnt\t0"
2303 : "error: cache architecture unspecified");
2304 case 2:
2305 return (TARGET_GLn_CACHE
2306 ? "buffer_gl1_inv\;buffer_gl0_inv\;"
2307 "global_atomic_swap<X>\t%0, %A1, %2%O1 glc\;"
2308 "s_waitcnt\tvmcnt(0)"
2309 : TARGET_WBINVL1_CACHE
2310 ? "buffer_wbinvl1_vol\;"
2311 "global_atomic_swap<X>\t%0, %A1, %2%O1 glc\;"
2312 "s_waitcnt\tvmcnt(0)"
2313 : "error: cache architecture unspecified");
2314 }
2315 break;
2316 case MEMMODEL_ACQ_REL:
2317 case MEMMODEL_SEQ_CST:
2318 case MEMMODEL_SYNC_SEQ_CST:
2319 switch (which_alternative)
2320 {
2321 case 0:
2322 return "s_dcache_wb_vol\;s_atomic_swap<X>\t%0, %1, %2 glc\;"
2323 "s_waitcnt\tlgkmcnt(0)\;s_dcache_inv_vol";
2324 case 1:
2325 return (TARGET_GLn_CACHE
2326 ? "buffer_gl1_inv\;buffer_gl0_inv\;flat_atomic_swap<X>\t%0, %1, %2 glc\;"
2327 "s_waitcnt\t0\;buffer_gl1_inv\;buffer_gl0_inv"
2328 : TARGET_WBINVL1_CACHE
2329 ? "buffer_wbinvl1_vol\;flat_atomic_swap<X>\t%0, %1, %2 glc\;"
2330 "s_waitcnt\t0\;buffer_wbinvl1_vol"
2331 : "error: cache architecture unspecified");
2332 case 2:
2333 return (TARGET_GLn_CACHE
2334 ? "buffer_gl1_inv\;buffer_gl0_inv\;"
2335 "global_atomic_swap<X>\t%0, %A1, %2%O1 glc\;"
2336 "s_waitcnt\tvmcnt(0)\;buffer_gl1_inv\;buffer_gl0_inv"
2337 : TARGET_WBINVL1_CACHE
2338 ? "buffer_wbinvl1_vol\;"
2339 "global_atomic_swap<X>\t%0, %A1, %2%O1 glc\;"
2340 "s_waitcnt\tvmcnt(0)\;buffer_wbinvl1_vol"
2341 : "error: cache architecture unspecified");
2342 }
2343 break;
2344 }
2345 gcc_unreachable ();
2346 }
2347 [(set_attr "type" "smem,flat,flat")
2348 (set_attr "length" "28")
2349 (set_attr "rdna" "no,*,*")])
2350
2351 ;; }}}
2352 ;; {{{ OpenACC / OpenMP
2353
2354 (define_expand "oacc_dim_size"
2355 [(match_operand:SI 0 "register_operand")
2356 (match_operand:SI 1 "const_int_operand")]
2357 ""
2358 {
2359 rtx tmp = gcn_oacc_dim_size (INTVAL (operands[1]));
2360 emit_move_insn (operands[0], gen_lowpart (SImode, tmp));
2361 DONE;
2362 })
2363
2364 (define_expand "oacc_dim_pos"
2365 [(match_operand:SI 0 "register_operand")
2366 (match_operand:SI 1 "const_int_operand")]
2367 ""
2368 {
2369 emit_move_insn (operands[0], gcn_oacc_dim_pos (INTVAL (operands[1])));
2370 DONE;
2371 })
2372
2373 (define_expand "gcn_wavefront_barrier"
2374 [(set (match_dup 0)
2375 (unspec_volatile:BLK [(match_dup 0)] UNSPECV_BARRIER))]
2376 ""
2377 {
2378 operands[0] = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (Pmode));
2379 MEM_VOLATILE_P (operands[0]) = 1;
2380 })
2381
2382 (define_insn "*gcn_wavefront_barrier"
2383 [(set (match_operand:BLK 0 "")
2384 (unspec_volatile:BLK [(match_dup 0)] UNSPECV_BARRIER))]
2385 ""
2386 "s_barrier"
2387 [(set_attr "type" "sopp")])
2388
2389 (define_expand "oacc_fork"
2390 [(set (match_operand:SI 0 "")
2391 (match_operand:SI 1 ""))
2392 (use (match_operand:SI 2 ""))]
2393 ""
2394 {
2395 /* We need to have oacc_fork/oacc_join named patterns as a pair,
2396 but the fork isn't actually used. */
2397 gcc_unreachable ();
2398 })
2399
2400 (define_expand "oacc_join"
2401 [(set (match_operand:SI 0 "")
2402 (match_operand:SI 1 ""))
2403 (use (match_operand:SI 2 ""))]
2404 ""
2405 {
2406 emit_insn (gen_gcn_wavefront_barrier ());
2407 DONE;
2408 })
2409
2410 ;; }}}
2411
2412 (include "gcn-valu.md")