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