1 //===-- AMDGPUInstructions.td - Common instruction defs ---*- tablegen -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains instruction defs that are common to all hw codegen
12 //===----------------------------------------------------------------------===//
14 class AddressSpacesImpl {
23 def AddrSpaces : AddressSpacesImpl;
26 class AMDGPUInst <dag outs, dag ins, string asm = "",
27 list<dag> pattern = []> : Instruction {
28 field bit isRegisterLoad = 0;
29 field bit isRegisterStore = 0;
31 let Namespace = "AMDGPU";
32 let OutOperandList = outs;
33 let InOperandList = ins;
35 let Pattern = pattern;
36 let Itinerary = NullALU;
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;
44 let DecoderNamespace = Namespace;
46 let TSFlags{63} = isRegisterLoad;
47 let TSFlags{62} = isRegisterStore;
50 class AMDGPUShaderInst <dag outs, dag ins, string asm = "",
51 list<dag> pattern = []> : AMDGPUInst<outs, ins, asm, pattern> {
53 field bits<32> Inst = 0xffffffff;
56 //===---------------------------------------------------------------------===//
58 //===---------------------------------------------------------------------===//
60 class ILFormat<dag outs, dag ins, string asmstr, list<dag> pattern>
63 let Namespace = "AMDGPU";
64 dag OutOperandList = outs;
65 dag InOperandList = ins;
66 let Pattern = pattern;
67 let AsmString = !strconcat(asmstr, "\n");
69 let Itinerary = NullALU;
71 bit hasZeroOpFlag = 0;
74 let hasSideEffects = 0;
75 let isCodeGenOnly = 1;
78 def TruePredicate : Predicate<"true">;
80 class 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,
92 class AMDGPUPat<dag pattern, dag result> : Pat<pattern, result>,
95 def FP16Denormals : Predicate<"Subtarget->hasFP16Denormals()">;
96 def FP32Denormals : Predicate<"Subtarget->hasFP32Denormals()">;
97 def FP64Denormals : Predicate<"Subtarget->hasFP64Denormals()">;
98 def NoFP16Denormals : Predicate<"!Subtarget->hasFP16Denormals()">;
99 def NoFP32Denormals : Predicate<"!Subtarget->hasFP32Denormals()">;
100 def NoFP64Denormals : Predicate<"!Subtarget->hasFP64Denormals()">;
101 def UnsafeFPMath : Predicate<"TM.Options.UnsafeFPMath">;
102 def FMA : Predicate<"Subtarget->hasFMA()">;
104 def InstFlag : OperandWithDefaultOps <i32, (ops (i32 0))>;
106 def u16ImmTarget : AsmOperandClass {
108 let RenderMethod = "addImmOperands";
111 def s16ImmTarget : AsmOperandClass {
113 let RenderMethod = "addImmOperands";
116 let OperandType = "OPERAND_IMMEDIATE" in {
118 def u32imm : Operand<i32> {
119 let PrintMethod = "printU32ImmOperand";
122 def u16imm : Operand<i16> {
123 let PrintMethod = "printU16ImmOperand";
124 let ParserMatchClass = u16ImmTarget;
127 def s16imm : Operand<i16> {
128 let PrintMethod = "printU16ImmOperand";
129 let ParserMatchClass = s16ImmTarget;
132 def u8imm : Operand<i8> {
133 let PrintMethod = "printU8ImmOperand";
136 } // End OperandType = "OPERAND_IMMEDIATE"
138 //===--------------------------------------------------------------------===//
140 //===--------------------------------------------------------------------===//
141 def brtarget : Operand<OtherVT>;
143 //===----------------------------------------------------------------------===//
145 //===----------------------------------------------------------------------===//
147 class HasOneUseUnaryOp<SDPatternOperator op> : PatFrag<
150 [{ return N->hasOneUse(); }]
153 class HasOneUseBinOp<SDPatternOperator op> : PatFrag<
154 (ops node:$src0, node:$src1),
156 [{ return N->hasOneUse(); }]
159 class HasOneUseTernaryOp<SDPatternOperator op> : PatFrag<
160 (ops node:$src0, node:$src1, node:$src2),
161 (op $src0, $src1, $src2),
162 [{ return N->hasOneUse(); }]
165 let Properties = [SDNPCommutative, SDNPAssociative] in {
166 def smax_oneuse : HasOneUseBinOp<smax>;
167 def smin_oneuse : HasOneUseBinOp<smin>;
168 def umax_oneuse : HasOneUseBinOp<umax>;
169 def umin_oneuse : HasOneUseBinOp<umin>;
171 def fminnum_oneuse : HasOneUseBinOp<fminnum>;
172 def fmaxnum_oneuse : HasOneUseBinOp<fmaxnum>;
174 def fminnum_ieee_oneuse : HasOneUseBinOp<fminnum_ieee>;
175 def fmaxnum_ieee_oneuse : HasOneUseBinOp<fmaxnum_ieee>;
178 def and_oneuse : HasOneUseBinOp<and>;
179 def or_oneuse : HasOneUseBinOp<or>;
180 def xor_oneuse : HasOneUseBinOp<xor>;
181 } // Properties = [SDNPCommutative, SDNPAssociative]
183 def not_oneuse : HasOneUseUnaryOp<not>;
185 def add_oneuse : HasOneUseBinOp<add>;
186 def sub_oneuse : HasOneUseBinOp<sub>;
188 def srl_oneuse : HasOneUseBinOp<srl>;
189 def shl_oneuse : HasOneUseBinOp<shl>;
191 def select_oneuse : HasOneUseTernaryOp<select>;
193 def AMDGPUmul_u24_oneuse : HasOneUseBinOp<AMDGPUmul_u24>;
194 def AMDGPUmul_i24_oneuse : HasOneUseBinOp<AMDGPUmul_i24>;
196 def srl_16 : PatFrag<
197 (ops node:$src0), (srl_oneuse node:$src0, (i32 16))
201 def hi_i16_elt : PatFrag<
202 (ops node:$src0), (i16 (trunc (i32 (srl_16 node:$src0))))
206 def hi_f16_elt : PatLeaf<
208 if (N->getOpcode() != ISD::BITCAST)
210 SDValue Tmp = N->getOperand(0);
212 if (Tmp.getOpcode() != ISD::SRL)
214 if (const auto *RHS = dyn_cast<ConstantSDNode>(Tmp.getOperand(1))
215 return RHS->getZExtValue() == 16;
219 //===----------------------------------------------------------------------===//
220 // PatLeafs for floating-point comparisons
221 //===----------------------------------------------------------------------===//
223 def COND_OEQ : PatFrags<(ops), [(OtherVT SETOEQ), (OtherVT SETEQ)]>;
224 def COND_ONE : PatFrags<(ops), [(OtherVT SETONE), (OtherVT SETNE)]>;
225 def COND_OGT : PatFrags<(ops), [(OtherVT SETOGT), (OtherVT SETGT)]>;
226 def COND_OGE : PatFrags<(ops), [(OtherVT SETOGE), (OtherVT SETGE)]>;
227 def COND_OLT : PatFrags<(ops), [(OtherVT SETOLT), (OtherVT SETLT)]>;
228 def COND_OLE : PatFrags<(ops), [(OtherVT SETOLE), (OtherVT SETLE)]>;
229 def COND_O : PatFrags<(ops), [(OtherVT SETO)]>;
230 def COND_UO : PatFrags<(ops), [(OtherVT SETUO)]>;
232 //===----------------------------------------------------------------------===//
233 // PatLeafs for unsigned / unordered comparisons
234 //===----------------------------------------------------------------------===//
236 def COND_UEQ : PatFrag<(ops), (OtherVT SETUEQ)>;
237 def COND_UNE : PatFrag<(ops), (OtherVT SETUNE)>;
238 def COND_UGT : PatFrag<(ops), (OtherVT SETUGT)>;
239 def COND_UGE : PatFrag<(ops), (OtherVT SETUGE)>;
240 def COND_ULT : PatFrag<(ops), (OtherVT SETULT)>;
241 def COND_ULE : PatFrag<(ops), (OtherVT SETULE)>;
243 // XXX - For some reason R600 version is preferring to use unordered
245 def COND_UNE_NE : PatFrags<(ops), [(OtherVT SETUNE), (OtherVT SETNE)]>;
247 //===----------------------------------------------------------------------===//
248 // PatLeafs for signed comparisons
249 //===----------------------------------------------------------------------===//
251 def COND_SGT : PatFrag<(ops), (OtherVT SETGT)>;
252 def COND_SGE : PatFrag<(ops), (OtherVT SETGE)>;
253 def COND_SLT : PatFrag<(ops), (OtherVT SETLT)>;
254 def COND_SLE : PatFrag<(ops), (OtherVT SETLE)>;
256 //===----------------------------------------------------------------------===//
257 // PatLeafs for integer equality
258 //===----------------------------------------------------------------------===//
260 def COND_EQ : PatFrags<(ops), [(OtherVT SETEQ), (OtherVT SETUEQ)]>;
261 def COND_NE : PatFrags<(ops), [(OtherVT SETNE), (OtherVT SETUNE)]>;
263 // FIXME: Should not need code predicate
264 //def COND_NULL : PatLeaf<(OtherVT null_frag)>;
265 def COND_NULL : PatLeaf <
267 [{(void)N; return false;}]
270 //===----------------------------------------------------------------------===//
271 // PatLeafs for Texture Constants
272 //===----------------------------------------------------------------------===//
274 def TEX_ARRAY : PatLeaf<
276 [{uint32_t TType = (uint32_t)N->getZExtValue();
277 return TType == 9 || TType == 10 || TType == 16;
281 def TEX_RECT : PatLeaf<
283 [{uint32_t TType = (uint32_t)N->getZExtValue();
288 def TEX_SHADOW : PatLeaf<
290 [{uint32_t TType = (uint32_t)N->getZExtValue();
291 return (TType >= 6 && TType <= 8) || TType == 13;
295 def TEX_SHADOW_ARRAY : PatLeaf<
297 [{uint32_t TType = (uint32_t)N->getZExtValue();
298 return TType == 11 || TType == 12 || TType == 17;
302 //===----------------------------------------------------------------------===//
303 // Load/Store Pattern Fragments
304 //===----------------------------------------------------------------------===//
306 class AddressSpaceList<list<int> AS> {
307 list<int> AddrSpaces = AS;
310 class Aligned8Bytes <dag ops, dag frag> : PatFrag <ops, frag, [{
311 return cast<MemSDNode>(N)->getAlignment() % 8 == 0;
314 class Aligned16Bytes <dag ops, dag frag> : PatFrag <ops, frag, [{
315 return cast<MemSDNode>(N)->getAlignment() >= 16;
318 class LoadFrag <SDPatternOperator op> : PatFrag<(ops node:$ptr), (op node:$ptr)>;
320 class StoreFrag<SDPatternOperator op> : PatFrag <
321 (ops node:$value, node:$ptr), (op node:$value, node:$ptr)
324 class StoreHi16<SDPatternOperator op> : PatFrag <
325 (ops node:$value, node:$ptr), (op (srl node:$value, (i32 16)), node:$ptr)
328 def LoadAddress_constant : AddressSpaceList<[ AddrSpaces.Constant ]>;
329 def LoadAddress_global : AddressSpaceList<[ AddrSpaces.Global, AddrSpaces.Constant ]>;
330 def StoreAddress_global : AddressSpaceList<[ AddrSpaces.Global ]>;
332 def LoadAddress_flat : AddressSpaceList<[ AddrSpaces.Flat,
334 AddrSpaces.Constant ]>;
335 def StoreAddress_flat : AddressSpaceList<[ AddrSpaces.Flat, AddrSpaces.Global ]>;
337 def LoadAddress_private : AddressSpaceList<[ AddrSpaces.Private ]>;
338 def StoreAddress_private : AddressSpaceList<[ AddrSpaces.Private ]>;
340 def LoadAddress_local : AddressSpaceList<[ AddrSpaces.Local ]>;
341 def StoreAddress_local : AddressSpaceList<[ AddrSpaces.Local ]>;
343 def LoadAddress_region : AddressSpaceList<[ AddrSpaces.Region ]>;
344 def StoreAddress_region : AddressSpaceList<[ AddrSpaces.Region ]>;
348 class GlobalLoadAddress : CodePatPred<[{
349 auto AS = cast<MemSDNode>(N)->getAddressSpace();
350 return AS == AMDGPUAS::GLOBAL_ADDRESS || AS == AMDGPUAS::CONSTANT_ADDRESS;
353 class 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;
360 class GlobalAddress : CodePatPred<[{
361 return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
364 class PrivateAddress : CodePatPred<[{
365 return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
368 class LocalAddress : CodePatPred<[{
369 return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
372 class RegionAddress : CodePatPred<[{
373 return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::REGION_ADDRESS;
376 class FlatStoreAddress : CodePatPred<[{
377 const auto AS = cast<MemSDNode>(N)->getAddressSpace();
378 return AS == AMDGPUAS::FLAT_ADDRESS ||
379 AS == AMDGPUAS::GLOBAL_ADDRESS;
382 // TODO: Remove these when stores to new PatFrag format.
383 class PrivateStore <SDPatternOperator op> : StoreFrag <op>, PrivateAddress;
384 class LocalStore <SDPatternOperator op> : StoreFrag <op>, LocalAddress;
385 class RegionStore <SDPatternOperator op> : StoreFrag <op>, RegionAddress;
386 class GlobalStore <SDPatternOperator op> : StoreFrag<op>, GlobalAddress;
387 class FlatStore <SDPatternOperator op> : StoreFrag <op>, FlatStoreAddress;
390 foreach as = [ "global", "flat", "constant", "local", "private", "region" ] in {
391 let AddressSpaces = !cast<AddressSpaceList>("LoadAddress_"#as).AddrSpaces in {
393 def load_#as : PatFrag<(ops node:$ptr), (unindexedload node:$ptr)> {
395 let IsNonExtLoad = 1;
398 def extloadi8_#as : PatFrag<(ops node:$ptr), (extload node:$ptr)> {
403 def extloadi16_#as : PatFrag<(ops node:$ptr), (extload node:$ptr)> {
408 def sextloadi8_#as : PatFrag<(ops node:$ptr), (sextload node:$ptr)> {
413 def sextloadi16_#as : PatFrag<(ops node:$ptr), (sextload node:$ptr)> {
418 def zextloadi8_#as : PatFrag<(ops node:$ptr), (zextload node:$ptr)> {
423 def zextloadi16_#as : PatFrag<(ops node:$ptr), (zextload node:$ptr)> {
428 def atomic_load_32_#as : PatFrag<(ops node:$ptr), (atomic_load_32 node:$ptr)> {
433 def atomic_load_64_#as : PatFrag<(ops node:$ptr), (atomic_load_64 node:$ptr)> {
438 def store_#as : PatFrag<(ops node:$val, node:$ptr),
439 (unindexedstore node:$val, node:$ptr)> {
441 let IsTruncStore = 0;
444 // truncstore fragments.
445 def truncstore_#as : PatFrag<(ops node:$val, node:$ptr),
446 (unindexedstore node:$val, node:$ptr)> {
448 let IsTruncStore = 1;
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.
455 def truncstorei8_#as : PatFrag<(ops node:$val, node:$ptr),
456 (truncstore node:$val, node:$ptr)> {
461 def truncstorei16_#as : PatFrag<(ops node:$val, node:$ptr),
462 (truncstore node:$val, node:$ptr)> {
467 defm atomic_store_#as : binary_atomic_op<atomic_store>;
469 } // End let AddressSpaces = ...
470 } // End foreach AddrSpace
473 def store_hi16_private : StoreHi16 <truncstorei16>, PrivateAddress;
474 def truncstorei8_hi16_private : StoreHi16<truncstorei8>, PrivateAddress;
476 def store_atomic_global : GlobalStore<atomic_store>;
477 def truncstorei8_hi16_global : StoreHi16 <truncstorei8>, GlobalAddress;
478 def truncstorei16_hi16_global : StoreHi16 <truncstorei16>, GlobalAddress;
480 def store_local_hi16 : StoreHi16 <truncstorei16>, LocalAddress;
481 def truncstorei8_local_hi16 : StoreHi16<truncstorei8>, LocalAddress;
482 def atomic_store_local : LocalStore <atomic_store>;
484 def load_align8_local : Aligned8Bytes <
485 (ops node:$ptr), (load_local node:$ptr)
488 def load_align16_local : Aligned16Bytes <
489 (ops node:$ptr), (load_local node:$ptr)
492 def store_align8_local : Aligned8Bytes <
493 (ops node:$val, node:$ptr), (store_local node:$val, node:$ptr)
496 def store_align16_local : Aligned16Bytes <
497 (ops node:$val, node:$ptr), (store_local node:$val, node:$ptr)
500 def atomic_store_flat : FlatStore <atomic_store>;
501 def truncstorei8_hi16_flat : StoreHi16<truncstorei8>, FlatStoreAddress;
502 def truncstorei16_hi16_flat : StoreHi16<truncstorei16>, FlatStoreAddress;
505 class local_binary_atomic_op<SDNode atomic_op> :
506 PatFrag<(ops node:$ptr, node:$value),
507 (atomic_op node:$ptr, node:$value), [{
508 return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
511 class region_binary_atomic_op<SDNode atomic_op> :
512 PatFrag<(ops node:$ptr, node:$value),
513 (atomic_op node:$ptr, node:$value), [{
514 return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::REGION_ADDRESS;
518 def atomic_swap_local : local_binary_atomic_op<atomic_swap>;
519 def atomic_load_add_local : local_binary_atomic_op<atomic_load_add>;
520 def atomic_load_sub_local : local_binary_atomic_op<atomic_load_sub>;
521 def atomic_load_and_local : local_binary_atomic_op<atomic_load_and>;
522 def atomic_load_or_local : local_binary_atomic_op<atomic_load_or>;
523 def atomic_load_xor_local : local_binary_atomic_op<atomic_load_xor>;
524 def atomic_load_nand_local : local_binary_atomic_op<atomic_load_nand>;
525 def atomic_load_min_local : local_binary_atomic_op<atomic_load_min>;
526 def atomic_load_max_local : local_binary_atomic_op<atomic_load_max>;
527 def atomic_load_umin_local : local_binary_atomic_op<atomic_load_umin>;
528 def atomic_load_umax_local : local_binary_atomic_op<atomic_load_umax>;
530 def mskor_global : PatFrag<(ops node:$val, node:$ptr),
531 (AMDGPUstore_mskor node:$val, node:$ptr), [{
532 return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
535 class AtomicCmpSwapLocal <SDNode cmp_swap_node> : PatFrag<
536 (ops node:$ptr, node:$cmp, node:$swap),
537 (cmp_swap_node node:$ptr, node:$cmp, node:$swap), [{
538 AtomicSDNode *AN = cast<AtomicSDNode>(N);
539 return AN->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS;
542 class AtomicCmpSwapRegion <SDNode cmp_swap_node> : PatFrag<
543 (ops node:$ptr, node:$cmp, node:$swap),
544 (cmp_swap_node node:$ptr, node:$cmp, node:$swap), [{
545 AtomicSDNode *AN = cast<AtomicSDNode>(N);
546 return AN->getAddressSpace() == AMDGPUAS::REGION_ADDRESS;
549 def atomic_cmp_swap_local : AtomicCmpSwapLocal <atomic_cmp_swap>;
551 class global_binary_atomic_op_frag<SDNode atomic_op> : PatFrag<
552 (ops node:$ptr, node:$value),
553 (atomic_op node:$ptr, node:$value),
554 [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;}]>;
556 multiclass global_binary_atomic_op<SDNode atomic_op> {
557 def "" : global_binary_atomic_op_frag<atomic_op>;
559 def _noret : PatFrag<
560 (ops node:$ptr, node:$value),
561 (atomic_op node:$ptr, node:$value),
562 [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && (SDValue(N, 0).use_empty());}]>;
565 (ops node:$ptr, node:$value),
566 (atomic_op node:$ptr, node:$value),
567 [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && (!SDValue(N, 0).use_empty());}]>;
570 defm atomic_swap_global : global_binary_atomic_op<atomic_swap>;
571 defm atomic_add_global : global_binary_atomic_op<atomic_load_add>;
572 defm atomic_and_global : global_binary_atomic_op<atomic_load_and>;
573 defm atomic_max_global : global_binary_atomic_op<atomic_load_max>;
574 defm atomic_min_global : global_binary_atomic_op<atomic_load_min>;
575 defm atomic_or_global : global_binary_atomic_op<atomic_load_or>;
576 defm atomic_sub_global : global_binary_atomic_op<atomic_load_sub>;
577 defm atomic_umax_global : global_binary_atomic_op<atomic_load_umax>;
578 defm atomic_umin_global : global_binary_atomic_op<atomic_load_umin>;
579 defm atomic_xor_global : global_binary_atomic_op<atomic_load_xor>;
582 def AMDGPUatomic_cmp_swap_global : PatFrag<
583 (ops node:$ptr, node:$value),
584 (AMDGPUatomic_cmp_swap node:$ptr, node:$value)>, GlobalAddress;
586 def atomic_cmp_swap_global : PatFrag<
587 (ops node:$ptr, node:$cmp, node:$value),
588 (atomic_cmp_swap node:$ptr, node:$cmp, node:$value)>, GlobalAddress;
591 def atomic_cmp_swap_global_noret : PatFrag<
592 (ops node:$ptr, node:$cmp, node:$value),
593 (atomic_cmp_swap node:$ptr, node:$cmp, node:$value),
594 [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && (SDValue(N, 0).use_empty());}]>;
596 def atomic_cmp_swap_global_ret : PatFrag<
597 (ops node:$ptr, node:$cmp, node:$value),
598 (atomic_cmp_swap node:$ptr, node:$cmp, node:$value),
599 [{return cast<MemSDNode>(N)->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && (!SDValue(N, 0).use_empty());}]>;
601 //===----------------------------------------------------------------------===//
602 // Misc Pattern Fragments
603 //===----------------------------------------------------------------------===//
606 int TWO_PI = 0x40c90fdb;
608 int TWO_PI_INV = 0x3e22f983;
609 int FP_UINT_MAX_PLUS_1 = 0x4f800000; // 1 << 32 in floating point encoding
610 int FP16_ONE = 0x3C00;
611 int FP16_NEG_ONE = 0xBC00;
612 int FP32_ONE = 0x3f800000;
613 int FP32_NEG_ONE = 0xbf800000;
614 int FP64_ONE = 0x3ff0000000000000;
615 int FP64_NEG_ONE = 0xbff0000000000000;
617 def CONST : Constants;
619 def FP_ZERO : PatLeaf <
621 [{return N->getValueAPF().isZero();}]
624 def FP_ONE : PatLeaf <
626 [{return N->isExactlyValue(1.0);}]
629 def FP_HALF : PatLeaf <
631 [{return N->isExactlyValue(0.5);}]
634 /* Generic helper patterns for intrinsics */
635 /* -------------------------------------- */
637 class POW_Common <AMDGPUInst log_ieee, AMDGPUInst exp_ieee, AMDGPUInst mul>
639 (fpow f32:$src0, f32:$src1),
640 (exp_ieee (mul f32:$src1, (log_ieee f32:$src0)))
643 /* Other helper patterns */
644 /* --------------------- */
646 /* Extract element pattern */
647 class Extract_Element <ValueType sub_type, ValueType vec_type, int sub_idx,
650 (sub_type (extractelt vec_type:$src, sub_idx)),
651 (EXTRACT_SUBREG $src, sub_reg)
654 /* Insert element pattern */
655 class Insert_Element <ValueType elem_type, ValueType vec_type,
656 int sub_idx, SubRegIndex sub_reg>
658 (insertelt vec_type:$vec, elem_type:$elem, sub_idx),
659 (INSERT_SUBREG $vec, $elem, sub_reg)
662 // XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
663 // can handle COPY instructions.
664 // bitconvert pattern
665 class BitConvert <ValueType dt, ValueType st, RegisterClass rc> : AMDGPUPat <
666 (dt (bitconvert (st rc:$src0))),
670 // XXX: Convert to new syntax and use COPY_TO_REG, once the DFAPacketizer
671 // can handle COPY instructions.
672 class DwordAddrPat<ValueType vt, RegisterClass rc> : AMDGPUPat <
673 (vt (AMDGPUdwordaddr (vt rc:$addr))),
679 multiclass BFIPatterns <Instruction BFI_INT,
680 Instruction LoadImm32,
681 RegisterClass RC64> {
682 // Definition from ISA doc:
683 // (y & x) | (z & ~x)
685 (or (and i32:$y, i32:$x), (and i32:$z, (not i32:$x))),
691 (or (and i64:$y, i64:$x), (and i64:$z, (not i64:$x))),
693 (BFI_INT (i32 (EXTRACT_SUBREG $x, sub0)),
694 (i32 (EXTRACT_SUBREG $y, sub0)),
695 (i32 (EXTRACT_SUBREG $z, sub0))), sub0,
696 (BFI_INT (i32 (EXTRACT_SUBREG $x, sub1)),
697 (i32 (EXTRACT_SUBREG $y, sub1)),
698 (i32 (EXTRACT_SUBREG $z, sub1))), sub1)
701 // SHA-256 Ch function
704 (xor i32:$z, (and i32:$x, (xor i32:$y, i32:$z))),
710 (xor i64:$z, (and i64:$x, (xor i64:$y, i64:$z))),
712 (BFI_INT (i32 (EXTRACT_SUBREG $x, sub0)),
713 (i32 (EXTRACT_SUBREG $y, sub0)),
714 (i32 (EXTRACT_SUBREG $z, sub0))), sub0,
715 (BFI_INT (i32 (EXTRACT_SUBREG $x, sub1)),
716 (i32 (EXTRACT_SUBREG $y, sub1)),
717 (i32 (EXTRACT_SUBREG $z, sub1))), sub1)
721 (fcopysign f32:$src0, f32:$src1),
722 (BFI_INT (LoadImm32 (i32 0x7fffffff)), $src0, $src1)
726 (f32 (fcopysign f32:$src0, f64:$src1)),
727 (BFI_INT (LoadImm32 (i32 0x7fffffff)), $src0,
728 (i32 (EXTRACT_SUBREG $src1, sub1)))
732 (f64 (fcopysign f64:$src0, f64:$src1)),
734 (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
735 (BFI_INT (LoadImm32 (i32 0x7fffffff)),
736 (i32 (EXTRACT_SUBREG $src0, sub1)),
737 (i32 (EXTRACT_SUBREG $src1, sub1))), sub1)
741 (f64 (fcopysign f64:$src0, f32:$src1)),
743 (i32 (EXTRACT_SUBREG $src0, sub0)), sub0,
744 (BFI_INT (LoadImm32 (i32 0x7fffffff)),
745 (i32 (EXTRACT_SUBREG $src0, sub1)),
750 // SHA-256 Ma patterns
752 // ((x & z) | (y & (x | z))) -> BFI_INT (XOR x, y), z, y
753 multiclass SHA256MaPattern <Instruction BFI_INT, Instruction XOR, RegisterClass RC64> {
755 (or (and i32:$x, i32:$z), (and i32:$y, (or i32:$x, i32:$z))),
756 (BFI_INT (XOR i32:$x, i32:$y), i32:$z, i32:$y)
760 (or (and i64:$x, i64:$z), (and i64:$y, (or i64:$x, i64:$z))),
762 (BFI_INT (XOR (i32 (EXTRACT_SUBREG $x, sub0)),
763 (i32 (EXTRACT_SUBREG $y, sub0))),
764 (i32 (EXTRACT_SUBREG $z, sub0)),
765 (i32 (EXTRACT_SUBREG $y, sub0))), sub0,
766 (BFI_INT (XOR (i32 (EXTRACT_SUBREG $x, sub1)),
767 (i32 (EXTRACT_SUBREG $y, sub1))),
768 (i32 (EXTRACT_SUBREG $z, sub1)),
769 (i32 (EXTRACT_SUBREG $y, sub1))), sub1)
773 // Bitfield extract patterns
775 def IMMZeroBasedBitfieldMask : PatLeaf <(imm), [{
776 return isMask_32(N->getZExtValue());
779 def IMMPopCount : SDNodeXForm<imm, [{
780 return CurDAG->getTargetConstant(countPopulation(N->getZExtValue()), SDLoc(N),
784 multiclass BFEPattern <Instruction UBFE, Instruction SBFE, Instruction MOV> {
786 (i32 (and (i32 (srl i32:$src, i32:$rshift)), IMMZeroBasedBitfieldMask:$mask)),
787 (UBFE $src, $rshift, (MOV (i32 (IMMPopCount $mask))))
790 // x & ((1 << y) - 1)
792 (and i32:$src, (add_oneuse (shl_oneuse 1, i32:$width), -1)),
793 (UBFE $src, (MOV (i32 0)), $width)
798 (and i32:$src, (xor_oneuse (shl_oneuse -1, i32:$width), -1)),
799 (UBFE $src, (MOV (i32 0)), $width)
802 // x & (-1 >> (bitwidth - y))
804 (and i32:$src, (srl_oneuse -1, (sub 32, i32:$width))),
805 (UBFE $src, (MOV (i32 0)), $width)
808 // x << (bitwidth - y) >> (bitwidth - y)
810 (srl (shl_oneuse i32:$src, (sub 32, i32:$width)), (sub 32, i32:$width)),
811 (UBFE $src, (MOV (i32 0)), $width)
815 (sra (shl_oneuse i32:$src, (sub 32, i32:$width)), (sub 32, i32:$width)),
816 (SBFE $src, (MOV (i32 0)), $width)
821 class ROTRPattern <Instruction BIT_ALIGN> : AMDGPUPat <
822 (rotr i32:$src0, i32:$src1),
823 (BIT_ALIGN $src0, $src0, $src1)
826 multiclass IntMed3Pat<Instruction med3Inst,
827 SDPatternOperator min,
828 SDPatternOperator max,
829 SDPatternOperator min_oneuse,
830 SDPatternOperator max_oneuse,
831 ValueType vt = i32> {
833 // This matches 16 permutations of
834 // min(max(a, b), max(min(a, b), c))
836 (min (max_oneuse vt:$src0, vt:$src1),
837 (max_oneuse (min_oneuse vt:$src0, vt:$src1), vt:$src2)),
838 (med3Inst vt:$src0, vt:$src1, vt:$src2)
841 // This matches 16 permutations of
842 // max(min(x, y), min(max(x, y), z))
844 (max (min_oneuse vt:$src0, vt:$src1),
845 (min_oneuse (max_oneuse vt:$src0, vt:$src1), vt:$src2)),
846 (med3Inst $src0, $src1, $src2)
850 // Special conversion patterns
852 def cvt_rpi_i32_f32 : PatFrag <
854 (fp_to_sint (ffloor (fadd $src, FP_HALF))),
855 [{ (void) N; return TM.Options.NoNaNsFPMath; }]
858 def cvt_flr_i32_f32 : PatFrag <
860 (fp_to_sint (ffloor $src)),
861 [{ (void)N; return TM.Options.NoNaNsFPMath; }]
864 let AddedComplexity = 2 in {
865 class IMad24Pat<Instruction Inst, bit HasClamp = 0> : AMDGPUPat <
866 (add (AMDGPUmul_i24 i32:$src0, i32:$src1), i32:$src2),
867 !if(HasClamp, (Inst $src0, $src1, $src2, (i1 0)),
868 (Inst $src0, $src1, $src2))
871 class UMad24Pat<Instruction Inst, bit HasClamp = 0> : AMDGPUPat <
872 (add (AMDGPUmul_u24 i32:$src0, i32:$src1), i32:$src2),
873 !if(HasClamp, (Inst $src0, $src1, $src2, (i1 0)),
874 (Inst $src0, $src1, $src2))
876 } // AddedComplexity.
878 class RcpPat<Instruction RcpInst, ValueType vt> : AMDGPUPat <
879 (fdiv FP_ONE, vt:$src),
883 class RsqPat<Instruction RsqInst, ValueType vt> : AMDGPUPat <
884 (AMDGPUrcp (fsqrt vt:$src)),
888 // Instructions which select to the same v_min_f*
889 def fminnum_like : PatFrags<(ops node:$src0, node:$src1),
890 [(fminnum_ieee node:$src0, node:$src1),
891 (fminnum node:$src0, node:$src1)]
894 // Instructions which select to the same v_max_f*
895 def fmaxnum_like : PatFrags<(ops node:$src0, node:$src1),
896 [(fmaxnum_ieee node:$src0, node:$src1),
897 (fmaxnum node:$src0, node:$src1)]
900 def fminnum_like_oneuse : PatFrags<(ops node:$src0, node:$src1),
901 [(fminnum_ieee_oneuse node:$src0, node:$src1),
902 (fminnum_oneuse node:$src0, node:$src1)]
905 def fmaxnum_like_oneuse : PatFrags<(ops node:$src0, node:$src1),
906 [(fmaxnum_ieee_oneuse node:$src0, node:$src1),
907 (fmaxnum_oneuse node:$src0, node:$src1)]