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