xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AMDGPU/AMDGPUInstructions.td (revision 972a253a57b6f144b0e4a3e2080a2a0076ec55a0)
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  int Constant32Bit = 6;
22}
23
24def AddrSpaces : AddressSpacesImpl;
25
26
27class AMDGPUInst <dag outs, dag ins, string asm = "",
28  list<dag> pattern = []> : Instruction {
29  field bit isRegisterLoad = 0;
30  field bit isRegisterStore = 0;
31
32  let Namespace = "AMDGPU";
33  let OutOperandList = outs;
34  let InOperandList = ins;
35  let AsmString = asm;
36  let Pattern = pattern;
37  let Itinerary = NullALU;
38
39  // SoftFail is a field the disassembler can use to provide a way for
40  // instructions to not match without killing the whole decode process. It is
41  // mainly used for ARM, but Tablegen expects this field to exist or it fails
42  // to build the decode table.
43  field bits<96> SoftFail = 0;
44
45  let DecoderNamespace = Namespace;
46
47  let TSFlags{63} = isRegisterLoad;
48  let TSFlags{62} = isRegisterStore;
49}
50
51class AMDGPUShaderInst <dag outs, dag ins, string asm = "",
52  list<dag> pattern = []> : AMDGPUInst<outs, ins, asm, pattern> {
53
54  field bits<32> Inst = 0xffffffff;
55}
56
57//===---------------------------------------------------------------------===//
58// Return instruction
59//===---------------------------------------------------------------------===//
60
61class ILFormat<dag outs, dag ins, string asmstr, list<dag> pattern>
62: Instruction {
63
64     let Namespace = "AMDGPU";
65     dag OutOperandList = outs;
66     dag InOperandList = ins;
67     let Pattern = pattern;
68     let AsmString = !strconcat(asmstr, "\n");
69     let isPseudo = 1;
70     let Itinerary = NullALU;
71     bit hasIEEEFlag = 0;
72     bit hasZeroOpFlag = 0;
73     let mayLoad = 0;
74     let mayStore = 0;
75     let hasSideEffects = 0;
76     let isCodeGenOnly = 1;
77}
78
79def TruePredicate : Predicate<"">;
80
81// FIXME: Tablegen should specially supports this
82def FalsePredicate : Predicate<"false">;
83
84// Add a predicate to the list if does not already exist to deduplicate it.
85class PredConcat<list<Predicate> lst, Predicate pred> {
86  list<Predicate> ret =
87      !listconcat([pred], !filter(item, lst, !ne(item, pred)));
88}
89
90// Add a Register to the list if does not already exist
91class RegAppend<list<Register> lst, Register reg> {
92  list<Register> ret =
93      !listconcat([reg], !filter(item, lst, !ne(item, reg)));
94}
95// Get the union of two Register lists
96class RegListUnion<list<Register> lstA, list<Register> lstB> {
97  list<Register> ret =
98      !foldl(lstA, lstB, temp, item, RegAppend<temp, item>.ret);
99}
100
101class PredicateControl {
102  Predicate SubtargetPredicate = TruePredicate;
103  Predicate AssemblerPredicate = TruePredicate;
104  Predicate WaveSizePredicate = TruePredicate;
105  list<Predicate> OtherPredicates = [];
106  list<Predicate> Predicates = PredConcat<
107                                 PredConcat<PredConcat<OtherPredicates,
108                                                       SubtargetPredicate>.ret,
109                                            AssemblerPredicate>.ret,
110                                 WaveSizePredicate>.ret;
111}
112
113class AMDGPUPat<dag pattern, dag result> : Pat<pattern, result>,
114      PredicateControl;
115
116let RecomputePerFunction = 1 in {
117def FP16Denormals : Predicate<"MF->getInfo<SIMachineFunctionInfo>()->getMode().allFP64FP16Denormals()">;
118def FP32Denormals : Predicate<"MF->getInfo<SIMachineFunctionInfo>()->getMode().allFP32Denormals()">;
119def FP64Denormals : Predicate<"MF->getInfo<SIMachineFunctionInfo>()->getMode().allFP64FP16Denormals()">;
120def NoFP16Denormals : Predicate<"!MF->getInfo<SIMachineFunctionInfo>()->getMode().allFP64FP16Denormals()">;
121def NoFP32Denormals : Predicate<"!MF->getInfo<SIMachineFunctionInfo>()->getMode().allFP32Denormals()">;
122def NoFP64Denormals : Predicate<"!MF->getInfo<SIMachineFunctionInfo>()->getMode().allFP64FP16Denormals()">;
123def UnsafeFPMath : Predicate<"TM.Options.UnsafeFPMath">;
124}
125
126def FMA : Predicate<"Subtarget->hasFMA()">;
127
128def InstFlag : OperandWithDefaultOps <i32, (ops (i32 0))>;
129
130def u16ImmTarget : AsmOperandClass {
131  let Name = "U16Imm";
132  let RenderMethod = "addImmOperands";
133}
134
135def s16ImmTarget : AsmOperandClass {
136  let Name = "S16Imm";
137  let RenderMethod = "addImmOperands";
138}
139
140let OperandType = "OPERAND_IMMEDIATE" in {
141
142def u32imm : Operand<i32> {
143  let PrintMethod = "printU32ImmOperand";
144}
145
146def u16imm : Operand<i16> {
147  let PrintMethod = "printU16ImmOperand";
148  let ParserMatchClass = u16ImmTarget;
149}
150
151def s16imm : Operand<i16> {
152  let PrintMethod = "printU16ImmOperand";
153  let ParserMatchClass = s16ImmTarget;
154}
155
156def u8imm : Operand<i8> {
157  let PrintMethod = "printU8ImmOperand";
158}
159
160} // End OperandType = "OPERAND_IMMEDIATE"
161
162//===--------------------------------------------------------------------===//
163// Custom Operands
164//===--------------------------------------------------------------------===//
165def brtarget   : Operand<OtherVT>;
166
167//===----------------------------------------------------------------------===//
168// Misc. PatFrags
169//===----------------------------------------------------------------------===//
170
171class HasOneUseUnaryOp<SDPatternOperator op> : PatFrag<
172  (ops node:$src0),
173  (op $src0),
174  [{ return N->hasOneUse(); }]> {
175
176  let GISelPredicateCode = [{
177    return MRI.hasOneNonDBGUse(MI.getOperand(0).getReg());
178  }];
179}
180
181class HasOneUseBinOp<SDPatternOperator op> : PatFrag<
182  (ops node:$src0, node:$src1),
183  (op $src0, $src1),
184  [{ return N->hasOneUse(); }]> {
185  let GISelPredicateCode = [{
186    return MRI.hasOneNonDBGUse(MI.getOperand(0).getReg());
187  }];
188}
189
190class HasOneUseTernaryOp<SDPatternOperator op> : PatFrag<
191  (ops node:$src0, node:$src1, node:$src2),
192  (op $src0, $src1, $src2),
193  [{ return N->hasOneUse(); }]> {
194  let GISelPredicateCode = [{
195    return MRI.hasOneNonDBGUse(MI.getOperand(0).getReg());
196  }];
197}
198
199class is_canonicalized<SDPatternOperator op> : PatFrag<
200  (ops node:$src0, node:$src1),
201  (op $src0, $src1),
202  [{
203    const SITargetLowering &Lowering =
204              *static_cast<const SITargetLowering *>(getTargetLowering());
205
206    return Lowering.isCanonicalized(*CurDAG, N->getOperand(0)) &&
207      Lowering.isCanonicalized(*CurDAG, N->getOperand(1));
208   }]> {
209
210  // TODO: Improve the Legalizer for g_build_vector in Global Isel to match this class
211  let GISelPredicateCode = [{
212    const SITargetLowering *TLI = static_cast<const SITargetLowering *>(
213      MF.getSubtarget().getTargetLowering());
214
215    return TLI->isCanonicalized(MI.getOperand(1).getReg(), const_cast<MachineFunction&>(MF)) &&
216      TLI->isCanonicalized(MI.getOperand(2).getReg(), const_cast<MachineFunction&>(MF));
217  }];
218}
219
220
221let Properties = [SDNPCommutative, SDNPAssociative] in {
222def smax_oneuse : HasOneUseBinOp<smax>;
223def smin_oneuse : HasOneUseBinOp<smin>;
224def umax_oneuse : HasOneUseBinOp<umax>;
225def umin_oneuse : HasOneUseBinOp<umin>;
226
227def fminnum_oneuse : HasOneUseBinOp<fminnum>;
228def fmaxnum_oneuse : HasOneUseBinOp<fmaxnum>;
229
230def fminnum_ieee_oneuse : HasOneUseBinOp<fminnum_ieee>;
231def fmaxnum_ieee_oneuse : HasOneUseBinOp<fmaxnum_ieee>;
232
233
234def and_oneuse : HasOneUseBinOp<and>;
235def or_oneuse : HasOneUseBinOp<or>;
236def xor_oneuse : HasOneUseBinOp<xor>;
237} // Properties = [SDNPCommutative, SDNPAssociative]
238
239def not_oneuse : HasOneUseUnaryOp<not>;
240
241def add_oneuse : HasOneUseBinOp<add>;
242def sub_oneuse : HasOneUseBinOp<sub>;
243
244def srl_oneuse : HasOneUseBinOp<srl>;
245def shl_oneuse : HasOneUseBinOp<shl>;
246
247def select_oneuse : HasOneUseTernaryOp<select>;
248
249def AMDGPUmul_u24_oneuse : HasOneUseBinOp<AMDGPUmul_u24>;
250def AMDGPUmul_i24_oneuse : HasOneUseBinOp<AMDGPUmul_i24>;
251
252//===----------------------------------------------------------------------===//
253// PatFrags for shifts
254//===----------------------------------------------------------------------===//
255
256// Constrained shift PatFrags.
257
258def csh_mask_16 : PatFrag<(ops node:$src0), (and node:$src0, imm),
259  [{ return isUnneededShiftMask(N, 4); }]> {
260    let GISelPredicateCode = [{ return isUnneededShiftMask(MI, 4); }];
261  }
262
263def csh_mask_32 : PatFrag<(ops node:$src0), (and node:$src0, imm),
264  [{ return isUnneededShiftMask(N, 5); }]> {
265    let GISelPredicateCode = [{ return isUnneededShiftMask(MI, 5); }];
266  }
267
268def csh_mask_64 : PatFrag<(ops node:$src0), (and node:$src0, imm),
269  [{ return isUnneededShiftMask(N, 6); }]> {
270    let GISelPredicateCode = [{ return isUnneededShiftMask(MI, 6); }];
271  }
272
273foreach width = [16, 32, 64] in {
274defvar csh_mask = !cast<SDPatternOperator>("csh_mask_"#width);
275
276def cshl_#width : PatFrags<(ops node:$src0, node:$src1),
277  [(shl node:$src0, node:$src1), (shl node:$src0, (csh_mask node:$src1))]>;
278defvar cshl = !cast<SDPatternOperator>("cshl_"#width);
279def cshl_#width#_oneuse : HasOneUseBinOp<cshl>;
280def clshl_rev_#width : PatFrag <(ops node:$src0, node:$src1),
281  (cshl $src1, $src0)>;
282
283def csrl_#width : PatFrags<(ops node:$src0, node:$src1),
284  [(srl node:$src0, node:$src1), (srl node:$src0, (csh_mask node:$src1))]>;
285defvar csrl = !cast<SDPatternOperator>("csrl_"#width);
286def csrl_#width#_oneuse : HasOneUseBinOp<csrl>;
287def clshr_rev_#width : PatFrag <(ops node:$src0, node:$src1),
288  (csrl $src1, $src0)>;
289
290def csra_#width : PatFrags<(ops node:$src0, node:$src1),
291  [(sra node:$src0, node:$src1), (sra node:$src0, (csh_mask node:$src1))]>;
292defvar csra = !cast<SDPatternOperator>("csra_"#width);
293def csra_#width#_oneuse : HasOneUseBinOp<csra>;
294def cashr_rev_#width : PatFrag <(ops node:$src0, node:$src1),
295  (csra $src1, $src0)>;
296} // end foreach width
297
298def srl_16 : PatFrag<
299  (ops node:$src0), (srl_oneuse node:$src0, (i32 16))
300>;
301
302
303def hi_i16_elt : PatFrag<
304  (ops node:$src0), (i16 (trunc (i32 (srl_16 node:$src0))))
305>;
306
307
308def hi_f16_elt : PatLeaf<
309  (vt), [{
310  if (N->getOpcode() != ISD::BITCAST)
311    return false;
312  SDValue Tmp = N->getOperand(0);
313
314  if (Tmp.getOpcode() != ISD::SRL)
315    return false;
316    if (const auto *RHS = dyn_cast<ConstantSDNode>(Tmp.getOperand(1))
317      return RHS->getZExtValue() == 16;
318    return false;
319}]>;
320
321//===----------------------------------------------------------------------===//
322// PatLeafs for floating-point comparisons
323//===----------------------------------------------------------------------===//
324
325def COND_OEQ : PatFrags<(ops), [(OtherVT SETOEQ), (OtherVT SETEQ)]>;
326def COND_ONE : PatFrags<(ops), [(OtherVT SETONE), (OtherVT SETNE)]>;
327def COND_OGT : PatFrags<(ops), [(OtherVT SETOGT), (OtherVT SETGT)]>;
328def COND_OGE : PatFrags<(ops), [(OtherVT SETOGE), (OtherVT SETGE)]>;
329def COND_OLT : PatFrags<(ops), [(OtherVT SETOLT), (OtherVT SETLT)]>;
330def COND_OLE : PatFrags<(ops), [(OtherVT SETOLE), (OtherVT SETLE)]>;
331def COND_O   : PatFrags<(ops), [(OtherVT SETO)]>;
332def COND_UO  : PatFrags<(ops), [(OtherVT SETUO)]>;
333
334//===----------------------------------------------------------------------===//
335// PatLeafs for unsigned / unordered comparisons
336//===----------------------------------------------------------------------===//
337
338def COND_UEQ : PatFrag<(ops), (OtherVT SETUEQ)>;
339def COND_UNE : PatFrag<(ops), (OtherVT SETUNE)>;
340def COND_UGT : PatFrag<(ops), (OtherVT SETUGT)>;
341def COND_UGE : PatFrag<(ops), (OtherVT SETUGE)>;
342def COND_ULT : PatFrag<(ops), (OtherVT SETULT)>;
343def COND_ULE : PatFrag<(ops), (OtherVT SETULE)>;
344
345// XXX - For some reason R600 version is preferring to use unordered
346// for setne?
347def COND_UNE_NE  : PatFrags<(ops), [(OtherVT SETUNE), (OtherVT SETNE)]>;
348
349//===----------------------------------------------------------------------===//
350// PatLeafs for signed comparisons
351//===----------------------------------------------------------------------===//
352
353def COND_SGT : PatFrag<(ops), (OtherVT SETGT)>;
354def COND_SGE : PatFrag<(ops), (OtherVT SETGE)>;
355def COND_SLT : PatFrag<(ops), (OtherVT SETLT)>;
356def COND_SLE : PatFrag<(ops), (OtherVT SETLE)>;
357
358//===----------------------------------------------------------------------===//
359// PatLeafs for integer equality
360//===----------------------------------------------------------------------===//
361
362def COND_EQ : PatFrags<(ops), [(OtherVT SETEQ), (OtherVT SETUEQ)]>;
363def COND_NE : PatFrags<(ops), [(OtherVT SETNE), (OtherVT SETUNE)]>;
364
365// FIXME: Should not need code predicate
366//def COND_NULL : PatLeaf<(OtherVT null_frag)>;
367def COND_NULL : PatLeaf <
368  (cond),
369  [{(void)N; return false;}]
370>;
371
372//===----------------------------------------------------------------------===//
373// PatLeafs for Texture Constants
374//===----------------------------------------------------------------------===//
375
376def TEX_ARRAY : PatLeaf<
377  (imm),
378  [{uint32_t TType = (uint32_t)N->getZExtValue();
379    return TType == 9 || TType == 10 || TType == 16;
380  }]
381>;
382
383def TEX_RECT : PatLeaf<
384  (imm),
385  [{uint32_t TType = (uint32_t)N->getZExtValue();
386    return TType == 5;
387  }]
388>;
389
390def TEX_SHADOW : PatLeaf<
391  (imm),
392  [{uint32_t TType = (uint32_t)N->getZExtValue();
393    return (TType >= 6 && TType <= 8) || TType == 13;
394  }]
395>;
396
397def TEX_SHADOW_ARRAY : PatLeaf<
398  (imm),
399  [{uint32_t TType = (uint32_t)N->getZExtValue();
400    return TType == 11 || TType == 12 || TType == 17;
401  }]
402>;
403
404//===----------------------------------------------------------------------===//
405// Load/Store Pattern Fragments
406//===----------------------------------------------------------------------===//
407
408def atomic_cmp_swap_glue : SDNode <"ISD::ATOMIC_CMP_SWAP", SDTAtomic3,
409  [SDNPHasChain, SDNPMayStore, SDNPMayLoad, SDNPMemOperand, SDNPInGlue]
410>;
411
412class AddressSpaceList<list<int> AS> {
413  list<int> AddrSpaces = AS;
414}
415
416class Aligned<int Bytes> {
417  int MinAlignment = Bytes;
418}
419
420class StoreHi16<SDPatternOperator op, ValueType vt> : PatFrag <
421  (ops node:$value, node:$ptr), (op (srl node:$value, (i32 16)), node:$ptr)> {
422  let IsStore = 1;
423  let MemoryVT = vt;
424}
425
426def LoadAddress_constant : AddressSpaceList<[ AddrSpaces.Constant,
427                                              AddrSpaces.Constant32Bit ]>;
428def LoadAddress_global : AddressSpaceList<[ AddrSpaces.Global,
429                                            AddrSpaces.Constant,
430                                            AddrSpaces.Constant32Bit ]>;
431def StoreAddress_global : AddressSpaceList<[ AddrSpaces.Global ]>;
432
433def LoadAddress_flat : AddressSpaceList<[ AddrSpaces.Flat,
434                                          AddrSpaces.Global,
435                                          AddrSpaces.Constant,
436                                          AddrSpaces.Constant32Bit ]>;
437def StoreAddress_flat : AddressSpaceList<[ AddrSpaces.Flat, AddrSpaces.Global ]>;
438
439def LoadAddress_private : AddressSpaceList<[ AddrSpaces.Private ]>;
440def StoreAddress_private : AddressSpaceList<[ AddrSpaces.Private ]>;
441
442def LoadAddress_local : AddressSpaceList<[ AddrSpaces.Local ]>;
443def StoreAddress_local : AddressSpaceList<[ AddrSpaces.Local ]>;
444
445def LoadAddress_region : AddressSpaceList<[ AddrSpaces.Region ]>;
446def StoreAddress_region : AddressSpaceList<[ AddrSpaces.Region ]>;
447
448
449
450foreach as = [ "global", "flat", "constant", "local", "private", "region" ] in {
451let AddressSpaces = !cast<AddressSpaceList>("LoadAddress_"#as).AddrSpaces in {
452
453def load_#as : PatFrag<(ops node:$ptr), (unindexedload node:$ptr)> {
454  let IsLoad = 1;
455  let IsNonExtLoad = 1;
456}
457
458def extloadi8_#as  : PatFrag<(ops node:$ptr), (extloadi8 node:$ptr)> {
459  let IsLoad = 1;
460}
461
462def extloadi16_#as : PatFrag<(ops node:$ptr), (extloadi16 node:$ptr)> {
463  let IsLoad = 1;
464}
465
466def sextloadi8_#as  : PatFrag<(ops node:$ptr), (sextloadi8 node:$ptr)> {
467  let IsLoad = 1;
468}
469
470def sextloadi16_#as : PatFrag<(ops node:$ptr), (sextloadi16 node:$ptr)> {
471  let IsLoad = 1;
472}
473
474def zextloadi8_#as  : PatFrag<(ops node:$ptr), (zextloadi8 node:$ptr)> {
475  let IsLoad = 1;
476}
477
478def zextloadi16_#as : PatFrag<(ops node:$ptr), (zextloadi16 node:$ptr)> {
479  let IsLoad = 1;
480}
481
482def atomic_load_8_#as : PatFrag<(ops node:$ptr), (atomic_load_8 node:$ptr)> {
483  let IsAtomic = 1;
484  let MemoryVT = i8;
485}
486
487def atomic_load_16_#as : PatFrag<(ops node:$ptr), (atomic_load_16 node:$ptr)> {
488  let IsAtomic = 1;
489  let MemoryVT = i16;
490}
491
492def atomic_load_32_#as : PatFrag<(ops node:$ptr), (atomic_load_32 node:$ptr)> {
493  let IsAtomic = 1;
494  let MemoryVT = i32;
495}
496
497def atomic_load_64_#as : PatFrag<(ops node:$ptr), (atomic_load_64 node:$ptr)> {
498  let IsAtomic = 1;
499  let MemoryVT = i64;
500}
501} // End let AddressSpaces
502} // End foreach as
503
504
505foreach as = [ "global", "flat", "local", "private", "region" ] in {
506let IsStore = 1, AddressSpaces = !cast<AddressSpaceList>("StoreAddress_"#as).AddrSpaces in {
507def store_#as : PatFrag<(ops node:$val, node:$ptr),
508                    (unindexedstore node:$val, node:$ptr)> {
509  let IsTruncStore = 0;
510}
511
512// truncstore fragments.
513def truncstore_#as : PatFrag<(ops node:$val, node:$ptr),
514                             (unindexedstore node:$val, node:$ptr)> {
515  let IsTruncStore = 1;
516}
517
518// TODO: We don't really need the truncstore here. We can use
519// unindexedstore with MemoryVT directly, which will save an
520// unnecessary check that the memory size is less than the value type
521// in the generated matcher table.
522def truncstorei8_#as : PatFrag<(ops node:$val, node:$ptr),
523                               (truncstorei8 node:$val, node:$ptr)>;
524def truncstorei16_#as : PatFrag<(ops node:$val, node:$ptr),
525                                (truncstorei16 node:$val, node:$ptr)>;
526
527def store_hi16_#as : StoreHi16 <truncstorei16, i16>;
528def truncstorei8_hi16_#as : StoreHi16<truncstorei8, i8>;
529def truncstorei16_hi16_#as : StoreHi16<truncstorei16, i16>;
530
531} // End let IsStore = 1, AddressSpaces = ...
532
533let IsAtomic = 1, AddressSpaces = !cast<AddressSpaceList>("StoreAddress_"#as).AddrSpaces in {
534def atomic_store_8_#as : PatFrag<(ops node:$ptr, node:$val),
535                                 (atomic_store_8 node:$ptr, node:$val)>;
536def atomic_store_16_#as : PatFrag<(ops node:$ptr, node:$val),
537                                  (atomic_store_16 node:$ptr, node:$val)>;
538def atomic_store_32_#as : PatFrag<(ops node:$ptr, node:$val),
539                                  (atomic_store_32 node:$ptr, node:$val)>;
540def atomic_store_64_#as : PatFrag<(ops node:$ptr, node:$val),
541                                  (atomic_store_64 node:$ptr, node:$val)>;
542}
543} // End foreach as
544
545multiclass noret_op {
546  let HasNoUse = true in
547  def "_noret" : PatFrag<(ops node:$ptr, node:$data),
548    (!cast<SDPatternOperator>(NAME) node:$ptr, node:$data)>;
549}
550
551defm int_amdgcn_flat_atomic_fadd : noret_op;
552defm int_amdgcn_flat_atomic_fadd_v2bf16 : noret_op;
553defm int_amdgcn_flat_atomic_fmin : noret_op;
554defm int_amdgcn_flat_atomic_fmax : noret_op;
555defm int_amdgcn_global_atomic_fadd : noret_op;
556defm int_amdgcn_global_atomic_fadd_v2bf16 : noret_op;
557defm int_amdgcn_global_atomic_fmin : noret_op;
558defm int_amdgcn_global_atomic_fmax : noret_op;
559defm int_amdgcn_ds_fadd_v2bf16 : noret_op;
560
561multiclass noret_binary_atomic_op<SDNode atomic_op, bit IsInt = 1> {
562  let HasNoUse = true in
563  defm "_noret" : binary_atomic_op<atomic_op, IsInt>;
564}
565
566multiclass noret_ternary_atomic_op<SDNode atomic_op> {
567  let HasNoUse = true in
568  defm "_noret" : ternary_atomic_op<atomic_op>;
569}
570
571multiclass binary_atomic_op_all_as<SDNode atomic_op, bit IsInt = 1> {
572  foreach as = [ "global", "flat", "constant", "local", "private", "region" ] in {
573    let AddressSpaces = !cast<AddressSpaceList>("LoadAddress_"#as).AddrSpaces in {
574      defm "_"#as : binary_atomic_op<atomic_op, IsInt>;
575      defm "_"#as : noret_binary_atomic_op<atomic_op, IsInt>;
576    }
577  }
578}
579
580defm atomic_swap : binary_atomic_op_all_as<atomic_swap>;
581defm atomic_load_add : binary_atomic_op_all_as<atomic_load_add>;
582defm atomic_load_and : binary_atomic_op_all_as<atomic_load_and>;
583defm atomic_load_max : binary_atomic_op_all_as<atomic_load_max>;
584defm atomic_load_min : binary_atomic_op_all_as<atomic_load_min>;
585defm atomic_load_or : binary_atomic_op_all_as<atomic_load_or>;
586defm atomic_load_sub : binary_atomic_op_all_as<atomic_load_sub>;
587defm atomic_load_umax : binary_atomic_op_all_as<atomic_load_umax>;
588defm atomic_load_umin : binary_atomic_op_all_as<atomic_load_umin>;
589defm atomic_load_xor : binary_atomic_op_all_as<atomic_load_xor>;
590defm atomic_load_fadd : binary_atomic_op_all_as<atomic_load_fadd, 0>;
591let MemoryVT = v2f16 in
592defm atomic_load_fadd_v2f16 : binary_atomic_op_all_as<atomic_load_fadd, 0>;
593defm AMDGPUatomic_cmp_swap : binary_atomic_op_all_as<AMDGPUatomic_cmp_swap>;
594
595def load_align8_local : PatFrag<(ops node:$ptr), (load_local node:$ptr)>,
596                       Aligned<8> {
597  let IsLoad = 1;
598}
599
600def load_align16_local : PatFrag<(ops node:$ptr), (load_local node:$ptr)>,
601                        Aligned<16> {
602  let IsLoad = 1;
603}
604
605def store_align8_local: PatFrag<(ops node:$val, node:$ptr),
606                                (store_local node:$val, node:$ptr)>, Aligned<8> {
607  let IsStore = 1;
608}
609
610def store_align16_local: PatFrag<(ops node:$val, node:$ptr),
611                                (store_local node:$val, node:$ptr)>, Aligned<16> {
612  let IsStore = 1;
613}
614
615let AddressSpaces = StoreAddress_local.AddrSpaces in {
616defm atomic_cmp_swap_local : ternary_atomic_op<atomic_cmp_swap>;
617defm atomic_cmp_swap_local : noret_ternary_atomic_op<atomic_cmp_swap>;
618defm atomic_cmp_swap_local_m0 : noret_ternary_atomic_op<atomic_cmp_swap_glue>;
619defm atomic_cmp_swap_local_m0 : ternary_atomic_op<atomic_cmp_swap_glue>;
620}
621
622let AddressSpaces = StoreAddress_region.AddrSpaces in {
623defm atomic_cmp_swap_region : noret_ternary_atomic_op<atomic_cmp_swap>;
624defm atomic_cmp_swap_region_m0 : noret_ternary_atomic_op<atomic_cmp_swap_glue>;
625defm atomic_cmp_swap_region_m0 : ternary_atomic_op<atomic_cmp_swap_glue>;
626}
627
628//===----------------------------------------------------------------------===//
629// Misc Pattern Fragments
630//===----------------------------------------------------------------------===//
631
632class Constants {
633int TWO_PI = 0x40c90fdb;
634int PI = 0x40490fdb;
635int TWO_PI_INV = 0x3e22f983;
636int FP_4294966784 = 0x4f7ffffe; // 4294966784 = 4294967296 - 512 = 2^32 - 2^9
637int FP16_ONE = 0x3C00;
638int FP16_NEG_ONE = 0xBC00;
639int FP32_ONE = 0x3f800000;
640int FP32_NEG_ONE = 0xbf800000;
641int FP64_ONE = 0x3ff0000000000000;
642int FP64_NEG_ONE = 0xbff0000000000000;
643}
644def CONST : Constants;
645
646def FP_ZERO : PatLeaf <
647  (fpimm),
648  [{return N->getValueAPF().isZero();}]
649>;
650
651def FP_ONE : PatLeaf <
652  (fpimm),
653  [{return N->isExactlyValue(1.0);}]
654>;
655
656def FP_HALF : PatLeaf <
657  (fpimm),
658  [{return N->isExactlyValue(0.5);}]
659>;
660
661/* Generic helper patterns for intrinsics */
662/* -------------------------------------- */
663
664class POW_Common <AMDGPUInst log_ieee, AMDGPUInst exp_ieee, AMDGPUInst mul>
665  : AMDGPUPat <
666  (fpow f32:$src0, f32:$src1),
667  (exp_ieee (mul f32:$src1, (log_ieee f32:$src0)))
668>;
669
670/* Other helper patterns */
671/* --------------------- */
672
673/* Extract element pattern */
674class Extract_Element <ValueType sub_type, ValueType vec_type, int sub_idx,
675                       SubRegIndex sub_reg>
676  : AMDGPUPat<
677  (sub_type (extractelt vec_type:$src, sub_idx)),
678  (EXTRACT_SUBREG $src, sub_reg)
679>;
680
681/* Insert element pattern */
682class Insert_Element <ValueType elem_type, ValueType vec_type,
683                      int sub_idx, SubRegIndex sub_reg>
684  : AMDGPUPat <
685  (insertelt vec_type:$vec, elem_type:$elem, sub_idx),
686  (INSERT_SUBREG $vec, $elem, sub_reg)
687>;
688
689// XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
690// can handle COPY instructions.
691// bitconvert pattern
692class BitConvert <ValueType dt, ValueType st, RegisterClass rc> : AMDGPUPat <
693  (dt (bitconvert (st rc:$src0))),
694  (dt rc:$src0)
695>;
696
697// XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
698// can handle COPY instructions.
699class DwordAddrPat<ValueType vt, RegisterClass rc> : AMDGPUPat <
700  (vt (AMDGPUdwordaddr (vt rc:$addr))),
701  (vt rc:$addr)
702>;
703
704// rotr pattern
705class ROTRPattern <Instruction BIT_ALIGN> : AMDGPUPat <
706  (rotr i32:$src0, i32:$src1),
707  (BIT_ALIGN $src0, $src0, $src1)
708>;
709
710// Special conversion patterns
711
712def cvt_rpi_i32_f32 : PatFrag <
713  (ops node:$src),
714  (fp_to_sint (ffloor (fadd $src, FP_HALF))),
715  [{ (void) N; return TM.Options.NoNaNsFPMath; }]
716>;
717
718def cvt_flr_i32_f32 : PatFrag <
719  (ops node:$src),
720  (fp_to_sint (ffloor $src)),
721  [{ (void)N; return TM.Options.NoNaNsFPMath; }]
722>;
723
724let AddedComplexity = 2 in {
725class IMad24Pat<Instruction Inst, bit HasClamp = 0> : AMDGPUPat <
726  (add (AMDGPUmul_i24 i32:$src0, i32:$src1), i32:$src2),
727  !if(HasClamp, (Inst $src0, $src1, $src2, (i1 0)),
728                (Inst $src0, $src1, $src2))
729>;
730
731class UMad24Pat<Instruction Inst, bit HasClamp = 0> : AMDGPUPat <
732  (add (AMDGPUmul_u24 i32:$src0, i32:$src1), i32:$src2),
733  !if(HasClamp, (Inst $src0, $src1, $src2, (i1 0)),
734                (Inst $src0, $src1, $src2))
735>;
736} // AddedComplexity.
737
738class RcpPat<Instruction RcpInst, ValueType vt> : AMDGPUPat <
739  (fdiv FP_ONE, vt:$src),
740  (RcpInst $src)
741>;
742
743// Instructions which select to the same v_min_f*
744def fminnum_like : PatFrags<(ops node:$src0, node:$src1),
745  [(fminnum_ieee node:$src0, node:$src1),
746   (fminnum node:$src0, node:$src1)]
747>;
748
749// Instructions which select to the same v_max_f*
750def fmaxnum_like : PatFrags<(ops node:$src0, node:$src1),
751  [(fmaxnum_ieee node:$src0, node:$src1),
752   (fmaxnum node:$src0, node:$src1)]
753>;
754
755def fminnum_like_oneuse : PatFrags<(ops node:$src0, node:$src1),
756  [(fminnum_ieee_oneuse node:$src0, node:$src1),
757   (fminnum_oneuse node:$src0, node:$src1)]
758>;
759
760def fmaxnum_like_oneuse : PatFrags<(ops node:$src0, node:$src1),
761  [(fmaxnum_ieee_oneuse node:$src0, node:$src1),
762   (fmaxnum_oneuse node:$src0, node:$src1)]
763>;
764
765def any_fmad : PatFrags<(ops node:$src0, node:$src1, node:$src2),
766  [(fmad node:$src0, node:$src1, node:$src2),
767   (AMDGPUfmad_ftz node:$src0, node:$src1, node:$src2)]
768>;
769
770// FIXME: fsqrt should not select directly
771def any_amdgcn_sqrt : PatFrags<(ops node:$src0),
772  [(fsqrt node:$src0), (int_amdgcn_sqrt node:$src0)]
773>;
774