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