xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUInstructions.td (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
1//===-- AMDGPUInstructions.td - Common instruction defs ---*- tablegen -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains instruction defs that are common to all hw codegen
10// targets.
11//
12//===----------------------------------------------------------------------===//
13
14class AddressSpacesImpl {
15  int Flat = 0;
16  int Global = 1;
17  int Region = 2;
18  int Local = 3;
19  int Constant = 4;
20  int Private = 5;
21}
22
23def AddrSpaces : AddressSpacesImpl;
24
25
26class AMDGPUInst <dag outs, dag ins, string asm = "",
27  list<dag> pattern = []> : Instruction {
28  field bit isRegisterLoad = 0;
29  field bit isRegisterStore = 0;
30
31  let Namespace = "AMDGPU";
32  let OutOperandList = outs;
33  let InOperandList = ins;
34  let AsmString = asm;
35  let Pattern = pattern;
36  let Itinerary = NullALU;
37
38  // SoftFail is a field the disassembler can use to provide a way for
39  // instructions to not match without killing the whole decode process. It is
40  // mainly used for ARM, but Tablegen expects this field to exist or it fails
41  // to build the decode table.
42  field bits<64> SoftFail = 0;
43
44  let DecoderNamespace = Namespace;
45
46  let TSFlags{63} = isRegisterLoad;
47  let TSFlags{62} = isRegisterStore;
48}
49
50class AMDGPUShaderInst <dag outs, dag ins, string asm = "",
51  list<dag> pattern = []> : AMDGPUInst<outs, ins, asm, pattern> {
52
53  field bits<32> Inst = 0xffffffff;
54}
55
56//===---------------------------------------------------------------------===//
57// Return instruction
58//===---------------------------------------------------------------------===//
59
60class ILFormat<dag outs, dag ins, string asmstr, list<dag> pattern>
61: Instruction {
62
63     let Namespace = "AMDGPU";
64     dag OutOperandList = outs;
65     dag InOperandList = ins;
66     let Pattern = pattern;
67     let AsmString = !strconcat(asmstr, "\n");
68     let isPseudo = 1;
69     let Itinerary = NullALU;
70     bit hasIEEEFlag = 0;
71     bit hasZeroOpFlag = 0;
72     let mayLoad = 0;
73     let mayStore = 0;
74     let hasSideEffects = 0;
75     let isCodeGenOnly = 1;
76}
77
78def TruePredicate : Predicate<"">;
79
80class PredicateControl {
81  Predicate SubtargetPredicate = TruePredicate;
82  list<Predicate> AssemblerPredicates = [];
83  Predicate AssemblerPredicate = TruePredicate;
84  Predicate WaveSizePredicate = TruePredicate;
85  list<Predicate> OtherPredicates = [];
86  list<Predicate> Predicates = !listconcat([SubtargetPredicate,
87                                            AssemblerPredicate,
88                                            WaveSizePredicate],
89                                            AssemblerPredicates,
90                                            OtherPredicates);
91}
92class AMDGPUPat<dag pattern, dag result> : Pat<pattern, result>,
93      PredicateControl;
94
95def FP16Denormals : Predicate<"Subtarget->hasFP16Denormals()">;
96def FP32Denormals : Predicate<"Subtarget->hasFP32Denormals()">;
97def FP64Denormals : Predicate<"Subtarget->hasFP64Denormals()">;
98def NoFP16Denormals : Predicate<"!Subtarget->hasFP16Denormals()">;
99def NoFP32Denormals : Predicate<"!Subtarget->hasFP32Denormals()">;
100def NoFP64Denormals : Predicate<"!Subtarget->hasFP64Denormals()">;
101def UnsafeFPMath : Predicate<"TM.Options.UnsafeFPMath">;
102def FMA : Predicate<"Subtarget->hasFMA()">;
103
104def InstFlag : OperandWithDefaultOps <i32, (ops (i32 0))>;
105
106def u16ImmTarget : AsmOperandClass {
107  let Name = "U16Imm";
108  let RenderMethod = "addImmOperands";
109}
110
111def s16ImmTarget : AsmOperandClass {
112  let Name = "S16Imm";
113  let RenderMethod = "addImmOperands";
114}
115
116let OperandType = "OPERAND_IMMEDIATE" in {
117
118def u32imm : Operand<i32> {
119  let PrintMethod = "printU32ImmOperand";
120}
121
122def u16imm : Operand<i16> {
123  let PrintMethod = "printU16ImmOperand";
124  let ParserMatchClass = u16ImmTarget;
125}
126
127def s16imm : Operand<i16> {
128  let PrintMethod = "printU16ImmOperand";
129  let ParserMatchClass = s16ImmTarget;
130}
131
132def u8imm : Operand<i8> {
133  let PrintMethod = "printU8ImmOperand";
134}
135
136} // End OperandType = "OPERAND_IMMEDIATE"
137
138//===--------------------------------------------------------------------===//
139// Custom Operands
140//===--------------------------------------------------------------------===//
141def brtarget   : Operand<OtherVT>;
142
143//===----------------------------------------------------------------------===//
144// Misc. PatFrags
145//===----------------------------------------------------------------------===//
146
147class HasOneUseUnaryOp<SDPatternOperator op> : PatFrag<
148  (ops node:$src0),
149  (op $src0),
150  [{ return N->hasOneUse(); }]
151>;
152
153class HasOneUseBinOp<SDPatternOperator op> : PatFrag<
154  (ops node:$src0, node:$src1),
155  (op $src0, $src1),
156  [{ return N->hasOneUse(); }]
157>;
158
159class HasOneUseTernaryOp<SDPatternOperator op> : PatFrag<
160  (ops node:$src0, node:$src1, node:$src2),
161  (op $src0, $src1, $src2),
162  [{ return N->hasOneUse(); }]
163>;
164
165let Properties = [SDNPCommutative, SDNPAssociative] in {
166def smax_oneuse : HasOneUseBinOp<smax>;
167def smin_oneuse : HasOneUseBinOp<smin>;
168def umax_oneuse : HasOneUseBinOp<umax>;
169def umin_oneuse : HasOneUseBinOp<umin>;
170
171def fminnum_oneuse : HasOneUseBinOp<fminnum>;
172def fmaxnum_oneuse : HasOneUseBinOp<fmaxnum>;
173
174def fminnum_ieee_oneuse : HasOneUseBinOp<fminnum_ieee>;
175def fmaxnum_ieee_oneuse : HasOneUseBinOp<fmaxnum_ieee>;
176
177
178def and_oneuse : HasOneUseBinOp<and>;
179def or_oneuse : HasOneUseBinOp<or>;
180def xor_oneuse : HasOneUseBinOp<xor>;
181} // Properties = [SDNPCommutative, SDNPAssociative]
182
183def not_oneuse : HasOneUseUnaryOp<not>;
184
185def add_oneuse : HasOneUseBinOp<add>;
186def sub_oneuse : HasOneUseBinOp<sub>;
187
188def srl_oneuse : HasOneUseBinOp<srl>;
189def shl_oneuse : HasOneUseBinOp<shl>;
190
191def select_oneuse : HasOneUseTernaryOp<select>;
192
193def AMDGPUmul_u24_oneuse : HasOneUseBinOp<AMDGPUmul_u24>;
194def AMDGPUmul_i24_oneuse : HasOneUseBinOp<AMDGPUmul_i24>;
195
196def srl_16 : PatFrag<
197  (ops node:$src0), (srl_oneuse node:$src0, (i32 16))
198>;
199
200
201def hi_i16_elt : PatFrag<
202  (ops node:$src0), (i16 (trunc (i32 (srl_16 node:$src0))))
203>;
204
205
206def hi_f16_elt : PatLeaf<
207  (vt), [{
208  if (N->getOpcode() != ISD::BITCAST)
209    return false;
210  SDValue Tmp = N->getOperand(0);
211
212  if (Tmp.getOpcode() != ISD::SRL)
213    return false;
214    if (const auto *RHS = dyn_cast<ConstantSDNode>(Tmp.getOperand(1))
215      return RHS->getZExtValue() == 16;
216    return false;
217}]>;
218
219//===----------------------------------------------------------------------===//
220// PatLeafs for floating-point comparisons
221//===----------------------------------------------------------------------===//
222
223def COND_OEQ : PatFrags<(ops), [(OtherVT SETOEQ), (OtherVT SETEQ)]>;
224def COND_ONE : PatFrags<(ops), [(OtherVT SETONE), (OtherVT SETNE)]>;
225def COND_OGT : PatFrags<(ops), [(OtherVT SETOGT), (OtherVT SETGT)]>;
226def COND_OGE : PatFrags<(ops), [(OtherVT SETOGE), (OtherVT SETGE)]>;
227def COND_OLT : PatFrags<(ops), [(OtherVT SETOLT), (OtherVT SETLT)]>;
228def COND_OLE : PatFrags<(ops), [(OtherVT SETOLE), (OtherVT SETLE)]>;
229def COND_O   : PatFrags<(ops), [(OtherVT SETO)]>;
230def COND_UO  : PatFrags<(ops), [(OtherVT SETUO)]>;
231
232//===----------------------------------------------------------------------===//
233// PatLeafs for unsigned / unordered comparisons
234//===----------------------------------------------------------------------===//
235
236def COND_UEQ : PatFrag<(ops), (OtherVT SETUEQ)>;
237def COND_UNE : PatFrag<(ops), (OtherVT SETUNE)>;
238def COND_UGT : PatFrag<(ops), (OtherVT SETUGT)>;
239def COND_UGE : PatFrag<(ops), (OtherVT SETUGE)>;
240def COND_ULT : PatFrag<(ops), (OtherVT SETULT)>;
241def COND_ULE : PatFrag<(ops), (OtherVT SETULE)>;
242
243// XXX - For some reason R600 version is preferring to use unordered
244// for setne?
245def COND_UNE_NE  : PatFrags<(ops), [(OtherVT SETUNE), (OtherVT SETNE)]>;
246
247//===----------------------------------------------------------------------===//
248// PatLeafs for signed comparisons
249//===----------------------------------------------------------------------===//
250
251def COND_SGT : PatFrag<(ops), (OtherVT SETGT)>;
252def COND_SGE : PatFrag<(ops), (OtherVT SETGE)>;
253def COND_SLT : PatFrag<(ops), (OtherVT SETLT)>;
254def COND_SLE : PatFrag<(ops), (OtherVT SETLE)>;
255
256//===----------------------------------------------------------------------===//
257// PatLeafs for integer equality
258//===----------------------------------------------------------------------===//
259
260def COND_EQ : PatFrags<(ops), [(OtherVT SETEQ), (OtherVT SETUEQ)]>;
261def COND_NE : PatFrags<(ops), [(OtherVT SETNE), (OtherVT SETUNE)]>;
262
263// FIXME: Should not need code predicate
264//def COND_NULL : PatLeaf<(OtherVT null_frag)>;
265def COND_NULL : PatLeaf <
266  (cond),
267  [{(void)N; return false;}]
268>;
269
270//===----------------------------------------------------------------------===//
271// PatLeafs for Texture Constants
272//===----------------------------------------------------------------------===//
273
274def TEX_ARRAY : PatLeaf<
275  (imm),
276  [{uint32_t TType = (uint32_t)N->getZExtValue();
277    return TType == 9 || TType == 10 || TType == 16;
278  }]
279>;
280
281def TEX_RECT : PatLeaf<
282  (imm),
283  [{uint32_t TType = (uint32_t)N->getZExtValue();
284    return TType == 5;
285  }]
286>;
287
288def TEX_SHADOW : PatLeaf<
289  (imm),
290  [{uint32_t TType = (uint32_t)N->getZExtValue();
291    return (TType >= 6 && TType <= 8) || TType == 13;
292  }]
293>;
294
295def TEX_SHADOW_ARRAY : PatLeaf<
296  (imm),
297  [{uint32_t TType = (uint32_t)N->getZExtValue();
298    return TType == 11 || TType == 12 || TType == 17;
299  }]
300>;
301
302//===----------------------------------------------------------------------===//
303// Load/Store Pattern Fragments
304//===----------------------------------------------------------------------===//
305
306def atomic_cmp_swap_glue : SDNode <"ISD::ATOMIC_CMP_SWAP", SDTAtomic3,
307  [SDNPHasChain, SDNPMayStore, SDNPMayLoad, SDNPMemOperand, SDNPInGlue]
308>;
309
310class AddressSpaceList<list<int> AS> {
311  list<int> AddrSpaces = AS;
312}
313
314class Aligned<int Bytes> {
315  int MinAlignment = Bytes;
316}
317
318class LoadFrag <SDPatternOperator op> : PatFrag<(ops node:$ptr), (op node:$ptr)>;
319
320class StoreFrag<SDPatternOperator op> : PatFrag <
321  (ops node:$value, node:$ptr), (op node:$value, node:$ptr)
322>;
323
324class StoreHi16<SDPatternOperator op> : PatFrag <
325  (ops node:$value, node:$ptr), (op (srl node:$value, (i32 16)), node:$ptr)
326>;
327
328def LoadAddress_constant : AddressSpaceList<[  AddrSpaces.Constant ]>;
329def LoadAddress_global : AddressSpaceList<[  AddrSpaces.Global, AddrSpaces.Constant ]>;
330def StoreAddress_global : AddressSpaceList<[ AddrSpaces.Global ]>;
331
332def LoadAddress_flat : AddressSpaceList<[  AddrSpaces.Flat,
333                                           AddrSpaces.Global,
334                                           AddrSpaces.Constant ]>;
335def StoreAddress_flat : AddressSpaceList<[ AddrSpaces.Flat, AddrSpaces.Global ]>;
336
337def LoadAddress_private : AddressSpaceList<[ AddrSpaces.Private ]>;
338def StoreAddress_private : AddressSpaceList<[ AddrSpaces.Private ]>;
339
340def LoadAddress_local : AddressSpaceList<[ AddrSpaces.Local ]>;
341def StoreAddress_local : AddressSpaceList<[ AddrSpaces.Local ]>;
342
343def LoadAddress_region : AddressSpaceList<[ AddrSpaces.Region ]>;
344def StoreAddress_region : AddressSpaceList<[ AddrSpaces.Region ]>;
345
346
347
348class GlobalLoadAddress : CodePatPred<[{
349  auto AS = cast<MemSDNode>(N)->getAddressSpace();
350  return AS == AMDGPUAS::GLOBAL_ADDRESS || AS == AMDGPUAS::CONSTANT_ADDRESS;
351}]>;
352
353class FlatLoadAddress : CodePatPred<[{
354  const auto AS = cast<MemSDNode>(N)->getAddressSpace();
355  return AS == AMDGPUAS::FLAT_ADDRESS ||
356         AS == AMDGPUAS::GLOBAL_ADDRESS ||
357         AS == AMDGPUAS::CONSTANT_ADDRESS;
358}]>;
359
360class GlobalAddress : CodePatPred<[{
361  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
362}]>;
363
364class PrivateAddress : CodePatPred<[{
365  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
366}]>;
367
368class LocalAddress : CodePatPred<[{
369  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
370}]>;
371
372class RegionAddress : CodePatPred<[{
373  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::REGION_ADDRESS;
374}]>;
375
376class FlatStoreAddress : CodePatPred<[{
377  const auto AS = cast<MemSDNode>(N)->getAddressSpace();
378  return AS == AMDGPUAS::FLAT_ADDRESS ||
379         AS == AMDGPUAS::GLOBAL_ADDRESS;
380}]>;
381
382// TODO: Remove these when stores to new PatFrag format.
383class PrivateStore <SDPatternOperator op> : StoreFrag <op>, PrivateAddress;
384class LocalStore <SDPatternOperator op> : StoreFrag <op>, LocalAddress;
385class RegionStore <SDPatternOperator op> : StoreFrag <op>, RegionAddress;
386class GlobalStore <SDPatternOperator op> : StoreFrag<op>, GlobalAddress;
387class FlatStore <SDPatternOperator op> : StoreFrag <op>, FlatStoreAddress;
388
389
390foreach as = [ "global", "flat", "constant", "local", "private", "region" ] in {
391let AddressSpaces = !cast<AddressSpaceList>("LoadAddress_"#as).AddrSpaces in {
392
393def load_#as : PatFrag<(ops node:$ptr), (unindexedload node:$ptr)> {
394  let IsLoad = 1;
395  let IsNonExtLoad = 1;
396}
397
398def extloadi8_#as  : PatFrag<(ops node:$ptr), (extload node:$ptr)> {
399  let IsLoad = 1;
400  let MemoryVT = i8;
401}
402
403def extloadi16_#as : PatFrag<(ops node:$ptr), (extload node:$ptr)> {
404  let IsLoad = 1;
405  let MemoryVT = i16;
406}
407
408def sextloadi8_#as  : PatFrag<(ops node:$ptr), (sextload node:$ptr)> {
409  let IsLoad = 1;
410  let MemoryVT = i8;
411}
412
413def sextloadi16_#as : PatFrag<(ops node:$ptr), (sextload node:$ptr)> {
414  let IsLoad = 1;
415  let MemoryVT = i16;
416}
417
418def zextloadi8_#as  : PatFrag<(ops node:$ptr), (zextload node:$ptr)> {
419  let IsLoad = 1;
420  let MemoryVT = i8;
421}
422
423def zextloadi16_#as : PatFrag<(ops node:$ptr), (zextload node:$ptr)> {
424  let IsLoad = 1;
425  let MemoryVT = i16;
426}
427
428def atomic_load_32_#as : PatFrag<(ops node:$ptr), (atomic_load_32 node:$ptr)> {
429  let IsAtomic = 1;
430  let MemoryVT = i32;
431}
432
433def atomic_load_64_#as : PatFrag<(ops node:$ptr), (atomic_load_64 node:$ptr)> {
434  let IsAtomic = 1;
435  let MemoryVT = i64;
436}
437
438def store_#as : PatFrag<(ops node:$val, node:$ptr),
439                    (unindexedstore node:$val, node:$ptr)> {
440  let IsStore = 1;
441  let IsTruncStore = 0;
442}
443
444// truncstore fragments.
445def truncstore_#as : PatFrag<(ops node:$val, node:$ptr),
446                             (unindexedstore node:$val, node:$ptr)> {
447  let IsStore = 1;
448  let IsTruncStore = 1;
449}
450
451// TODO: We don't really need the truncstore here. We can use
452// unindexedstore with MemoryVT directly, which will save an
453// unnecessary check that the memory size is less than the value type
454// in the generated matcher table.
455def truncstorei8_#as : PatFrag<(ops node:$val, node:$ptr),
456                               (truncstore node:$val, node:$ptr)> {
457  let IsStore = 1;
458  let MemoryVT = i8;
459}
460
461def truncstorei16_#as : PatFrag<(ops node:$val, node:$ptr),
462                                (truncstore node:$val, node:$ptr)> {
463  let IsStore = 1;
464  let MemoryVT = i16;
465}
466
467defm atomic_store_#as : binary_atomic_op<atomic_store>;
468
469} // End let AddressSpaces = ...
470} // End foreach AddrSpace
471
472
473multiclass ret_noret_binary_atomic_op<SDNode atomic_op, bit IsInt = 1> {
474  foreach as = [ "global", "flat", "constant", "local", "private", "region" ] in {
475    let AddressSpaces = !cast<AddressSpaceList>("LoadAddress_"#as).AddrSpaces in {
476      defm "_"#as : binary_atomic_op<atomic_op, IsInt>;
477
478      let PredicateCode = [{return (SDValue(N, 0).use_empty());}] in {
479        defm "_"#as#"_noret" : binary_atomic_op<atomic_op, IsInt>;
480      }
481
482      let PredicateCode = [{return !(SDValue(N, 0).use_empty());}] in {
483        defm "_"#as#"_ret" : binary_atomic_op<atomic_op, IsInt>;
484      }
485    }
486  }
487}
488
489defm atomic_swap : ret_noret_binary_atomic_op<atomic_swap>;
490defm atomic_load_add : ret_noret_binary_atomic_op<atomic_load_add>;
491defm atomic_load_and : ret_noret_binary_atomic_op<atomic_load_and>;
492defm atomic_load_max : ret_noret_binary_atomic_op<atomic_load_max>;
493defm atomic_load_min : ret_noret_binary_atomic_op<atomic_load_min>;
494defm atomic_load_or : ret_noret_binary_atomic_op<atomic_load_or>;
495defm atomic_load_sub : ret_noret_binary_atomic_op<atomic_load_sub>;
496defm atomic_load_umax : ret_noret_binary_atomic_op<atomic_load_umax>;
497defm atomic_load_umin : ret_noret_binary_atomic_op<atomic_load_umin>;
498defm atomic_load_xor : ret_noret_binary_atomic_op<atomic_load_xor>;
499defm atomic_load_fadd : ret_noret_binary_atomic_op<atomic_load_fadd, 0>;
500
501
502def store_hi16_private : StoreHi16 <truncstorei16>, PrivateAddress;
503def truncstorei8_hi16_private : StoreHi16<truncstorei8>, PrivateAddress;
504
505def store_atomic_global : GlobalStore<atomic_store>;
506def truncstorei8_hi16_global : StoreHi16 <truncstorei8>, GlobalAddress;
507def truncstorei16_hi16_global : StoreHi16 <truncstorei16>, GlobalAddress;
508
509def store_local_hi16 : StoreHi16 <truncstorei16>, LocalAddress;
510def truncstorei8_local_hi16 : StoreHi16<truncstorei8>, LocalAddress;
511def atomic_store_local : LocalStore <atomic_store>;
512
513
514def load_align8_local : PatFrag <(ops node:$ptr), (load_local node:$ptr)> {
515  let IsLoad = 1;
516  let IsNonExtLoad = 1;
517  let MinAlignment = 8;
518}
519
520def load_align16_local : PatFrag <(ops node:$ptr), (load_local node:$ptr)> {
521  let IsLoad = 1;
522  let IsNonExtLoad = 1;
523  let MinAlignment = 16;
524}
525
526def store_align8_local: PatFrag<(ops node:$val, node:$ptr),
527                                (store_local node:$val, node:$ptr)>, Aligned<8> {
528  let IsStore = 1;
529  let IsTruncStore = 0;
530}
531
532def store_align16_local: PatFrag<(ops node:$val, node:$ptr),
533                                (store_local node:$val, node:$ptr)>, Aligned<16> {
534  let IsStore = 1;
535  let IsTruncStore = 0;
536}
537
538
539def atomic_store_flat  : FlatStore <atomic_store>;
540def truncstorei8_hi16_flat  : StoreHi16<truncstorei8>, FlatStoreAddress;
541def truncstorei16_hi16_flat : StoreHi16<truncstorei16>, FlatStoreAddress;
542
543
544class local_binary_atomic_op<SDNode atomic_op> :
545  PatFrag<(ops node:$ptr, node:$value),
546    (atomic_op node:$ptr, node:$value), [{
547  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
548}]>;
549
550class region_binary_atomic_op<SDNode atomic_op> :
551  PatFrag<(ops node:$ptr, node:$value),
552    (atomic_op node:$ptr, node:$value), [{
553  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::REGION_ADDRESS;
554}]>;
555
556
557def mskor_global : PatFrag<(ops node:$val, node:$ptr),
558                            (AMDGPUstore_mskor node:$val, node:$ptr), [{
559  return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
560}]>;
561
562let AddressSpaces = StoreAddress_local.AddrSpaces in {
563defm atomic_cmp_swap_local : ternary_atomic_op<atomic_cmp_swap>;
564defm atomic_cmp_swap_local_m0 : ternary_atomic_op<atomic_cmp_swap_glue>;
565}
566
567let AddressSpaces = StoreAddress_region.AddrSpaces in {
568defm atomic_cmp_swap_region : ternary_atomic_op<atomic_cmp_swap>;
569defm atomic_cmp_swap_region_m0 : ternary_atomic_op<atomic_cmp_swap_glue>;
570}
571
572class global_binary_atomic_op_frag<SDNode atomic_op> : PatFrag<
573    (ops node:$ptr, node:$value),
574    (atomic_op node:$ptr, node:$value),
575    [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;}]>;
576
577// Legacy.
578def AMDGPUatomic_cmp_swap_global : PatFrag<
579  (ops node:$ptr, node:$value),
580  (AMDGPUatomic_cmp_swap node:$ptr, node:$value)>, GlobalAddress;
581
582def atomic_cmp_swap_global : PatFrag<
583  (ops node:$ptr, node:$cmp, node:$value),
584  (atomic_cmp_swap node:$ptr, node:$cmp, node:$value)>, GlobalAddress;
585
586
587def atomic_cmp_swap_global_noret : PatFrag<
588  (ops node:$ptr, node:$cmp, node:$value),
589  (atomic_cmp_swap node:$ptr, node:$cmp, node:$value),
590  [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && (SDValue(N, 0).use_empty());}]>;
591
592def atomic_cmp_swap_global_ret : PatFrag<
593  (ops node:$ptr, node:$cmp, node:$value),
594  (atomic_cmp_swap node:$ptr, node:$cmp, node:$value),
595  [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && (!SDValue(N, 0).use_empty());}]>;
596
597//===----------------------------------------------------------------------===//
598// Misc Pattern Fragments
599//===----------------------------------------------------------------------===//
600
601class Constants {
602int TWO_PI = 0x40c90fdb;
603int PI = 0x40490fdb;
604int TWO_PI_INV = 0x3e22f983;
605int FP_UINT_MAX_PLUS_1 = 0x4f800000;    // 1 << 32 in floating point encoding
606int FP16_ONE = 0x3C00;
607int FP16_NEG_ONE = 0xBC00;
608int FP32_ONE = 0x3f800000;
609int FP32_NEG_ONE = 0xbf800000;
610int FP64_ONE = 0x3ff0000000000000;
611int FP64_NEG_ONE = 0xbff0000000000000;
612}
613def CONST : Constants;
614
615def FP_ZERO : PatLeaf <
616  (fpimm),
617  [{return N->getValueAPF().isZero();}]
618>;
619
620def FP_ONE : PatLeaf <
621  (fpimm),
622  [{return N->isExactlyValue(1.0);}]
623>;
624
625def FP_HALF : PatLeaf <
626  (fpimm),
627  [{return N->isExactlyValue(0.5);}]
628>;
629
630/* Generic helper patterns for intrinsics */
631/* -------------------------------------- */
632
633class POW_Common <AMDGPUInst log_ieee, AMDGPUInst exp_ieee, AMDGPUInst mul>
634  : AMDGPUPat <
635  (fpow f32:$src0, f32:$src1),
636  (exp_ieee (mul f32:$src1, (log_ieee f32:$src0)))
637>;
638
639/* Other helper patterns */
640/* --------------------- */
641
642/* Extract element pattern */
643class Extract_Element <ValueType sub_type, ValueType vec_type, int sub_idx,
644                       SubRegIndex sub_reg>
645  : AMDGPUPat<
646  (sub_type (extractelt vec_type:$src, sub_idx)),
647  (EXTRACT_SUBREG $src, sub_reg)
648>;
649
650/* Insert element pattern */
651class Insert_Element <ValueType elem_type, ValueType vec_type,
652                      int sub_idx, SubRegIndex sub_reg>
653  : AMDGPUPat <
654  (insertelt vec_type:$vec, elem_type:$elem, sub_idx),
655  (INSERT_SUBREG $vec, $elem, sub_reg)
656>;
657
658// XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
659// can handle COPY instructions.
660// bitconvert pattern
661class BitConvert <ValueType dt, ValueType st, RegisterClass rc> : AMDGPUPat <
662  (dt (bitconvert (st rc:$src0))),
663  (dt rc:$src0)
664>;
665
666// XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
667// can handle COPY instructions.
668class DwordAddrPat<ValueType vt, RegisterClass rc> : AMDGPUPat <
669  (vt (AMDGPUdwordaddr (vt rc:$addr))),
670  (vt rc:$addr)
671>;
672
673// BFI_INT patterns
674
675multiclass BFIPatterns <Instruction BFI_INT,
676                        Instruction LoadImm32,
677                        RegisterClass RC64> {
678  // Definition from ISA doc:
679  // (y & x) | (z & ~x)
680  def : AMDGPUPat <
681    (or (and i32:$y, i32:$x), (and i32:$z, (not i32:$x))),
682    (BFI_INT $x, $y, $z)
683  >;
684
685  // 64-bit version
686  def : AMDGPUPat <
687    (or (and i64:$y, i64:$x), (and i64:$z, (not i64:$x))),
688    (REG_SEQUENCE RC64,
689      (BFI_INT (i32 (EXTRACT_SUBREG $x, sub0)),
690               (i32 (EXTRACT_SUBREG $y, sub0)),
691               (i32 (EXTRACT_SUBREG $z, sub0))), sub0,
692      (BFI_INT (i32 (EXTRACT_SUBREG $x, sub1)),
693               (i32 (EXTRACT_SUBREG $y, sub1)),
694               (i32 (EXTRACT_SUBREG $z, sub1))), sub1)
695  >;
696
697  // SHA-256 Ch function
698  // z ^ (x & (y ^ z))
699  def : AMDGPUPat <
700    (xor i32:$z, (and i32:$x, (xor i32:$y, i32:$z))),
701    (BFI_INT $x, $y, $z)
702  >;
703
704  // 64-bit version
705  def : AMDGPUPat <
706    (xor i64:$z, (and i64:$x, (xor i64:$y, i64:$z))),
707    (REG_SEQUENCE RC64,
708      (BFI_INT (i32 (EXTRACT_SUBREG $x, sub0)),
709               (i32 (EXTRACT_SUBREG $y, sub0)),
710               (i32 (EXTRACT_SUBREG $z, sub0))), sub0,
711      (BFI_INT (i32 (EXTRACT_SUBREG $x, sub1)),
712               (i32 (EXTRACT_SUBREG $y, sub1)),
713               (i32 (EXTRACT_SUBREG $z, sub1))), sub1)
714  >;
715
716  def : AMDGPUPat <
717    (fcopysign f32:$src0, f32:$src1),
718    (BFI_INT (LoadImm32 (i32 0x7fffffff)), $src0, $src1)
719  >;
720
721  def : AMDGPUPat <
722    (f32 (fcopysign f32:$src0, f64:$src1)),
723    (BFI_INT (LoadImm32 (i32 0x7fffffff)), $src0,
724             (i32 (EXTRACT_SUBREG $src1, sub1)))
725  >;
726
727  def : AMDGPUPat <
728    (f64 (fcopysign f64:$src0, f64:$src1)),
729    (REG_SEQUENCE RC64,
730      (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
731      (BFI_INT (LoadImm32 (i32 0x7fffffff)),
732               (i32 (EXTRACT_SUBREG $src0, sub1)),
733               (i32 (EXTRACT_SUBREG $src1, sub1))), sub1)
734  >;
735
736  def : AMDGPUPat <
737    (f64 (fcopysign f64:$src0, f32:$src1)),
738    (REG_SEQUENCE RC64,
739      (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
740      (BFI_INT (LoadImm32 (i32 0x7fffffff)),
741               (i32 (EXTRACT_SUBREG $src0, sub1)),
742               $src1), sub1)
743  >;
744}
745
746// SHA-256 Ma patterns
747
748// ((x & z) | (y & (x | z))) -> BFI_INT (XOR x, y), z, y
749multiclass SHA256MaPattern <Instruction BFI_INT, Instruction XOR, RegisterClass RC64> {
750  def : AMDGPUPat <
751    (or (and i32:$x, i32:$z), (and i32:$y, (or i32:$x, i32:$z))),
752    (BFI_INT (XOR i32:$x, i32:$y), i32:$z, i32:$y)
753  >;
754
755  def : AMDGPUPat <
756    (or (and i64:$x, i64:$z), (and i64:$y, (or i64:$x, i64:$z))),
757    (REG_SEQUENCE RC64,
758      (BFI_INT (XOR (i32 (EXTRACT_SUBREG $x, sub0)),
759                    (i32 (EXTRACT_SUBREG $y, sub0))),
760               (i32 (EXTRACT_SUBREG $z, sub0)),
761               (i32 (EXTRACT_SUBREG $y, sub0))), sub0,
762      (BFI_INT (XOR (i32 (EXTRACT_SUBREG $x, sub1)),
763                    (i32 (EXTRACT_SUBREG $y, sub1))),
764               (i32 (EXTRACT_SUBREG $z, sub1)),
765               (i32 (EXTRACT_SUBREG $y, sub1))), sub1)
766  >;
767}
768
769// Bitfield extract patterns
770
771def IMMZeroBasedBitfieldMask : PatLeaf <(imm), [{
772  return isMask_32(N->getZExtValue());
773}]>;
774
775def IMMPopCount : SDNodeXForm<imm, [{
776  return CurDAG->getTargetConstant(countPopulation(N->getZExtValue()), SDLoc(N),
777                                   MVT::i32);
778}]>;
779
780multiclass BFEPattern <Instruction UBFE, Instruction SBFE, Instruction MOV> {
781  def : AMDGPUPat <
782    (i32 (and (i32 (srl i32:$src, i32:$rshift)), IMMZeroBasedBitfieldMask:$mask)),
783    (UBFE $src, $rshift, (MOV (i32 (IMMPopCount $mask))))
784  >;
785
786  // x & ((1 << y) - 1)
787  def : AMDGPUPat <
788    (and i32:$src, (add_oneuse (shl_oneuse 1, i32:$width), -1)),
789    (UBFE $src, (MOV (i32 0)), $width)
790  >;
791
792  // x & ~(-1 << y)
793  def : AMDGPUPat <
794    (and i32:$src, (xor_oneuse (shl_oneuse -1, i32:$width), -1)),
795    (UBFE $src, (MOV (i32 0)), $width)
796  >;
797
798  // x & (-1 >> (bitwidth - y))
799  def : AMDGPUPat <
800    (and i32:$src, (srl_oneuse -1, (sub 32, i32:$width))),
801    (UBFE $src, (MOV (i32 0)), $width)
802  >;
803
804  // x << (bitwidth - y) >> (bitwidth - y)
805  def : AMDGPUPat <
806    (srl (shl_oneuse i32:$src, (sub 32, i32:$width)), (sub 32, i32:$width)),
807    (UBFE $src, (MOV (i32 0)), $width)
808  >;
809
810  def : AMDGPUPat <
811    (sra (shl_oneuse i32:$src, (sub 32, i32:$width)), (sub 32, i32:$width)),
812    (SBFE $src, (MOV (i32 0)), $width)
813  >;
814}
815
816// rotr pattern
817class ROTRPattern <Instruction BIT_ALIGN> : AMDGPUPat <
818  (rotr i32:$src0, i32:$src1),
819  (BIT_ALIGN $src0, $src0, $src1)
820>;
821
822multiclass IntMed3Pat<Instruction med3Inst,
823                 SDPatternOperator min,
824                 SDPatternOperator max,
825                 SDPatternOperator min_oneuse,
826                 SDPatternOperator max_oneuse,
827                 ValueType vt = i32> {
828
829  // This matches 16 permutations of
830  // min(max(a, b), max(min(a, b), c))
831  def : AMDGPUPat <
832  (min (max_oneuse vt:$src0, vt:$src1),
833       (max_oneuse (min_oneuse vt:$src0, vt:$src1), vt:$src2)),
834  (med3Inst vt:$src0, vt:$src1, vt:$src2)
835>;
836
837  // This matches 16 permutations of
838  // max(min(x, y), min(max(x, y), z))
839  def : AMDGPUPat <
840  (max (min_oneuse vt:$src0, vt:$src1),
841       (min_oneuse (max_oneuse vt:$src0, vt:$src1), vt:$src2)),
842  (med3Inst $src0, $src1, $src2)
843>;
844}
845
846// Special conversion patterns
847
848def cvt_rpi_i32_f32 : PatFrag <
849  (ops node:$src),
850  (fp_to_sint (ffloor (fadd $src, FP_HALF))),
851  [{ (void) N; return TM.Options.NoNaNsFPMath; }]
852>;
853
854def cvt_flr_i32_f32 : PatFrag <
855  (ops node:$src),
856  (fp_to_sint (ffloor $src)),
857  [{ (void)N; return TM.Options.NoNaNsFPMath; }]
858>;
859
860let AddedComplexity = 2 in {
861class IMad24Pat<Instruction Inst, bit HasClamp = 0> : AMDGPUPat <
862  (add (AMDGPUmul_i24 i32:$src0, i32:$src1), i32:$src2),
863  !if(HasClamp, (Inst $src0, $src1, $src2, (i1 0)),
864                (Inst $src0, $src1, $src2))
865>;
866
867class UMad24Pat<Instruction Inst, bit HasClamp = 0> : AMDGPUPat <
868  (add (AMDGPUmul_u24 i32:$src0, i32:$src1), i32:$src2),
869  !if(HasClamp, (Inst $src0, $src1, $src2, (i1 0)),
870                (Inst $src0, $src1, $src2))
871>;
872} // AddedComplexity.
873
874class RcpPat<Instruction RcpInst, ValueType vt> : AMDGPUPat <
875  (fdiv FP_ONE, vt:$src),
876  (RcpInst $src)
877>;
878
879class RsqPat<Instruction RsqInst, ValueType vt> : AMDGPUPat <
880  (AMDGPUrcp (fsqrt vt:$src)),
881  (RsqInst $src)
882>;
883
884// Instructions which select to the same v_min_f*
885def fminnum_like : PatFrags<(ops node:$src0, node:$src1),
886  [(fminnum_ieee node:$src0, node:$src1),
887   (fminnum node:$src0, node:$src1)]
888>;
889
890// Instructions which select to the same v_max_f*
891def fmaxnum_like : PatFrags<(ops node:$src0, node:$src1),
892  [(fmaxnum_ieee node:$src0, node:$src1),
893   (fmaxnum node:$src0, node:$src1)]
894>;
895
896def fminnum_like_oneuse : PatFrags<(ops node:$src0, node:$src1),
897  [(fminnum_ieee_oneuse node:$src0, node:$src1),
898   (fminnum_oneuse node:$src0, node:$src1)]
899>;
900
901def fmaxnum_like_oneuse : PatFrags<(ops node:$src0, node:$src1),
902  [(fmaxnum_ieee_oneuse node:$src0, node:$src1),
903   (fmaxnum_oneuse node:$src0, node:$src1)]
904>;
905