1 //===-- AMDGPUISelLowering.h - AMDGPU Lowering Interface --------*- C++ -*-===// 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 /// \file 10 /// Interface definition of the TargetLowering class that is common 11 /// to all AMD GPUs. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H 16 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H 17 18 #include "AMDGPU.h" 19 #include "llvm/CodeGen/CallingConvLower.h" 20 #include "llvm/CodeGen/TargetLowering.h" 21 22 namespace llvm { 23 24 class AMDGPUMachineFunction; 25 class AMDGPUSubtarget; 26 struct ArgDescriptor; 27 28 class AMDGPUTargetLowering : public TargetLowering { 29 private: 30 const AMDGPUSubtarget *Subtarget; 31 32 /// \returns AMDGPUISD::FFBH_U32 node if the incoming \p Op may have been 33 /// legalized from a smaller type VT. Need to match pre-legalized type because 34 /// the generic legalization inserts the add/sub between the select and 35 /// compare. 36 SDValue getFFBX_U32(SelectionDAG &DAG, SDValue Op, const SDLoc &DL, unsigned Opc) const; 37 38 public: 39 static unsigned numBitsUnsigned(SDValue Op, SelectionDAG &DAG); 40 static unsigned numBitsSigned(SDValue Op, SelectionDAG &DAG); 41 static bool hasDefinedInitializer(const GlobalValue *GV); 42 43 protected: 44 SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const; 45 SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const; 46 /// Split a vector store into multiple scalar stores. 47 /// \returns The resulting chain. 48 49 SDValue LowerFREM(SDValue Op, SelectionDAG &DAG) const; 50 SDValue LowerFCEIL(SDValue Op, SelectionDAG &DAG) const; 51 SDValue LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const; 52 SDValue LowerFRINT(SDValue Op, SelectionDAG &DAG) const; 53 SDValue LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const; 54 55 SDValue LowerFROUND_LegalFTRUNC(SDValue Op, SelectionDAG &DAG) const; 56 SDValue LowerFROUND64(SDValue Op, SelectionDAG &DAG) const; 57 SDValue LowerFROUND(SDValue Op, SelectionDAG &DAG) const; 58 SDValue LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const; 59 SDValue LowerFLOG(SDValue Op, SelectionDAG &DAG, 60 double Log2BaseInverted) const; 61 SDValue lowerFEXP(SDValue Op, SelectionDAG &DAG) const; 62 63 SDValue LowerCTLZ_CTTZ(SDValue Op, SelectionDAG &DAG) const; 64 65 SDValue LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG, bool Signed) const; 66 SDValue LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG, bool Signed) const; 67 SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) const; 68 SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) const; 69 70 SDValue LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG, bool Signed) const; 71 SDValue LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const; 72 SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const; 73 SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const; 74 75 SDValue LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const; 76 77 protected: 78 bool shouldCombineMemoryType(EVT VT) const; 79 SDValue performLoadCombine(SDNode *N, DAGCombinerInfo &DCI) const; 80 SDValue performStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const; 81 SDValue performAssertSZExtCombine(SDNode *N, DAGCombinerInfo &DCI) const; 82 SDValue performIntrinsicWOChainCombine(SDNode *N, DAGCombinerInfo &DCI) const; 83 84 SDValue splitBinaryBitConstantOpImpl(DAGCombinerInfo &DCI, const SDLoc &SL, 85 unsigned Opc, SDValue LHS, 86 uint32_t ValLo, uint32_t ValHi) const; 87 SDValue performShlCombine(SDNode *N, DAGCombinerInfo &DCI) const; 88 SDValue performSraCombine(SDNode *N, DAGCombinerInfo &DCI) const; 89 SDValue performSrlCombine(SDNode *N, DAGCombinerInfo &DCI) const; 90 SDValue performTruncateCombine(SDNode *N, DAGCombinerInfo &DCI) const; 91 SDValue performMulCombine(SDNode *N, DAGCombinerInfo &DCI) const; 92 SDValue performMulhsCombine(SDNode *N, DAGCombinerInfo &DCI) const; 93 SDValue performMulhuCombine(SDNode *N, DAGCombinerInfo &DCI) const; 94 SDValue performMulLoHi24Combine(SDNode *N, DAGCombinerInfo &DCI) const; 95 SDValue performCtlz_CttzCombine(const SDLoc &SL, SDValue Cond, SDValue LHS, 96 SDValue RHS, DAGCombinerInfo &DCI) const; 97 SDValue performSelectCombine(SDNode *N, DAGCombinerInfo &DCI) const; 98 99 bool isConstantCostlierToNegate(SDValue N) const; 100 SDValue performFNegCombine(SDNode *N, DAGCombinerInfo &DCI) const; 101 SDValue performFAbsCombine(SDNode *N, DAGCombinerInfo &DCI) const; 102 SDValue performRcpCombine(SDNode *N, DAGCombinerInfo &DCI) const; 103 104 static EVT getEquivalentMemType(LLVMContext &Context, EVT VT); 105 106 virtual SDValue LowerGlobalAddress(AMDGPUMachineFunction *MFI, SDValue Op, 107 SelectionDAG &DAG) const; 108 109 /// Return 64-bit value Op as two 32-bit integers. 110 std::pair<SDValue, SDValue> split64BitValue(SDValue Op, 111 SelectionDAG &DAG) const; 112 SDValue getLoHalf64(SDValue Op, SelectionDAG &DAG) const; 113 SDValue getHiHalf64(SDValue Op, SelectionDAG &DAG) const; 114 115 /// Split a vector type into two parts. The first part is a power of two 116 /// vector. The second part is whatever is left over, and is a scalar if it 117 /// would otherwise be a 1-vector. 118 std::pair<EVT, EVT> getSplitDestVTs(const EVT &VT, SelectionDAG &DAG) const; 119 120 /// Split a vector value into two parts of types LoVT and HiVT. HiVT could be 121 /// scalar. 122 std::pair<SDValue, SDValue> splitVector(const SDValue &N, const SDLoc &DL, 123 const EVT &LoVT, const EVT &HighVT, 124 SelectionDAG &DAG) const; 125 126 /// Split a vector load into 2 loads of half the vector. 127 SDValue SplitVectorLoad(SDValue Op, SelectionDAG &DAG) const; 128 129 /// Widen a vector load from vec3 to vec4. 130 SDValue WidenVectorLoad(SDValue Op, SelectionDAG &DAG) const; 131 132 /// Split a vector store into 2 stores of half the vector. 133 SDValue SplitVectorStore(SDValue Op, SelectionDAG &DAG) const; 134 135 SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const; 136 SDValue LowerSDIVREM(SDValue Op, SelectionDAG &DAG) const; 137 SDValue LowerUDIVREM(SDValue Op, SelectionDAG &DAG) const; 138 SDValue LowerDIVREM24(SDValue Op, SelectionDAG &DAG, bool sign) const; 139 void LowerUDIVREM64(SDValue Op, SelectionDAG &DAG, 140 SmallVectorImpl<SDValue> &Results) const; 141 142 void analyzeFormalArgumentsCompute( 143 CCState &State, 144 const SmallVectorImpl<ISD::InputArg> &Ins) const; 145 146 public: 147 AMDGPUTargetLowering(const TargetMachine &TM, const AMDGPUSubtarget &STI); 148 149 bool mayIgnoreSignedZero(SDValue Op) const { 150 if (getTargetMachine().Options.NoSignedZerosFPMath) 151 return true; 152 153 const auto Flags = Op.getNode()->getFlags(); 154 if (Flags.isDefined()) 155 return Flags.hasNoSignedZeros(); 156 157 return false; 158 } 159 160 static inline SDValue stripBitcast(SDValue Val) { 161 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val; 162 } 163 164 static bool allUsesHaveSourceMods(const SDNode *N, 165 unsigned CostThreshold = 4); 166 bool isFAbsFree(EVT VT) const override; 167 bool isFNegFree(EVT VT) const override; 168 bool isTruncateFree(EVT Src, EVT Dest) const override; 169 bool isTruncateFree(Type *Src, Type *Dest) const override; 170 171 bool isZExtFree(Type *Src, Type *Dest) const override; 172 bool isZExtFree(EVT Src, EVT Dest) const override; 173 bool isZExtFree(SDValue Val, EVT VT2) const override; 174 175 NegatibleCost getNegatibleCost(SDValue Op, SelectionDAG &DAG, 176 bool LegalOperations, bool ForCodeSize, 177 unsigned Depth) const override; 178 179 bool isNarrowingProfitable(EVT VT1, EVT VT2) const override; 180 181 MVT getVectorIdxTy(const DataLayout &) const override; 182 bool isSelectSupported(SelectSupportKind) const override; 183 184 bool isFPImmLegal(const APFloat &Imm, EVT VT, 185 bool ForCodeSize) const override; 186 bool ShouldShrinkFPConstant(EVT VT) const override; 187 bool shouldReduceLoadWidth(SDNode *Load, 188 ISD::LoadExtType ExtType, 189 EVT ExtVT) const override; 190 191 bool isLoadBitCastBeneficial(EVT, EVT, const SelectionDAG &DAG, 192 const MachineMemOperand &MMO) const final; 193 194 bool storeOfVectorConstantIsCheap(EVT MemVT, 195 unsigned NumElem, 196 unsigned AS) const override; 197 bool aggressivelyPreferBuildVectorSources(EVT VecVT) const override; 198 bool isCheapToSpeculateCttz() const override; 199 bool isCheapToSpeculateCtlz() const override; 200 201 bool isSDNodeAlwaysUniform(const SDNode *N) const override; 202 static CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg); 203 static CCAssignFn *CCAssignFnForReturn(CallingConv::ID CC, bool IsVarArg); 204 205 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 206 const SmallVectorImpl<ISD::OutputArg> &Outs, 207 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL, 208 SelectionDAG &DAG) const override; 209 210 SDValue addTokenForArgument(SDValue Chain, 211 SelectionDAG &DAG, 212 MachineFrameInfo &MFI, 213 int ClobberedFI) const; 214 215 SDValue lowerUnhandledCall(CallLoweringInfo &CLI, 216 SmallVectorImpl<SDValue> &InVals, 217 StringRef Reason) const; 218 SDValue LowerCall(CallLoweringInfo &CLI, 219 SmallVectorImpl<SDValue> &InVals) const override; 220 221 SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, 222 SelectionDAG &DAG) const; 223 224 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override; 225 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override; 226 void ReplaceNodeResults(SDNode * N, 227 SmallVectorImpl<SDValue> &Results, 228 SelectionDAG &DAG) const override; 229 230 SDValue combineFMinMaxLegacy(const SDLoc &DL, EVT VT, SDValue LHS, 231 SDValue RHS, SDValue True, SDValue False, 232 SDValue CC, DAGCombinerInfo &DCI) const; 233 234 const char* getTargetNodeName(unsigned Opcode) const override; 235 236 // FIXME: Turn off MergeConsecutiveStores() before Instruction Selection for 237 // AMDGPU. Commit r319036, 238 // (https://github.com/llvm/llvm-project/commit/db77e57ea86d941a4262ef60261692f4cb6893e6) 239 // turned on MergeConsecutiveStores() before Instruction Selection for all 240 // targets. Enough AMDGPU compiles go into an infinite loop ( 241 // MergeConsecutiveStores() merges two stores; LegalizeStoreOps() un-merges; 242 // MergeConsecutiveStores() re-merges, etc. ) to warrant turning it off for 243 // now. 244 bool mergeStoresAfterLegalization(EVT) const override { return false; } 245 246 bool isFsqrtCheap(SDValue Operand, SelectionDAG &DAG) const override { 247 return true; 248 } 249 SDValue getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled, 250 int &RefinementSteps, bool &UseOneConstNR, 251 bool Reciprocal) const override; 252 SDValue getRecipEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled, 253 int &RefinementSteps) const override; 254 255 virtual SDNode *PostISelFolding(MachineSDNode *N, 256 SelectionDAG &DAG) const = 0; 257 258 /// Determine which of the bits specified in \p Mask are known to be 259 /// either zero or one and return them in the \p KnownZero and \p KnownOne 260 /// bitsets. 261 void computeKnownBitsForTargetNode(const SDValue Op, 262 KnownBits &Known, 263 const APInt &DemandedElts, 264 const SelectionDAG &DAG, 265 unsigned Depth = 0) const override; 266 267 unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts, 268 const SelectionDAG &DAG, 269 unsigned Depth = 0) const override; 270 271 bool isKnownNeverNaNForTargetNode(SDValue Op, 272 const SelectionDAG &DAG, 273 bool SNaN = false, 274 unsigned Depth = 0) const override; 275 276 /// Helper function that adds Reg to the LiveIn list of the DAG's 277 /// MachineFunction. 278 /// 279 /// \returns a RegisterSDNode representing Reg if \p RawReg is true, otherwise 280 /// a copy from the register. 281 SDValue CreateLiveInRegister(SelectionDAG &DAG, 282 const TargetRegisterClass *RC, 283 unsigned Reg, EVT VT, 284 const SDLoc &SL, 285 bool RawReg = false) const; 286 SDValue CreateLiveInRegister(SelectionDAG &DAG, 287 const TargetRegisterClass *RC, 288 unsigned Reg, EVT VT) const { 289 return CreateLiveInRegister(DAG, RC, Reg, VT, SDLoc(DAG.getEntryNode())); 290 } 291 292 // Returns the raw live in register rather than a copy from it. 293 SDValue CreateLiveInRegisterRaw(SelectionDAG &DAG, 294 const TargetRegisterClass *RC, 295 unsigned Reg, EVT VT) const { 296 return CreateLiveInRegister(DAG, RC, Reg, VT, SDLoc(DAG.getEntryNode()), true); 297 } 298 299 /// Similar to CreateLiveInRegister, except value maybe loaded from a stack 300 /// slot rather than passed in a register. 301 SDValue loadStackInputValue(SelectionDAG &DAG, 302 EVT VT, 303 const SDLoc &SL, 304 int64_t Offset) const; 305 306 SDValue storeStackInputValue(SelectionDAG &DAG, 307 const SDLoc &SL, 308 SDValue Chain, 309 SDValue ArgVal, 310 int64_t Offset) const; 311 312 SDValue loadInputValue(SelectionDAG &DAG, 313 const TargetRegisterClass *RC, 314 EVT VT, const SDLoc &SL, 315 const ArgDescriptor &Arg) const; 316 317 enum ImplicitParameter { 318 FIRST_IMPLICIT, 319 GRID_DIM = FIRST_IMPLICIT, 320 GRID_OFFSET, 321 }; 322 323 /// Helper function that returns the byte offset of the given 324 /// type of implicit parameter. 325 uint32_t getImplicitParameterOffset(const MachineFunction &MF, 326 const ImplicitParameter Param) const; 327 328 MVT getFenceOperandTy(const DataLayout &DL) const override { 329 return MVT::i32; 330 } 331 332 AtomicExpansionKind shouldExpandAtomicRMWInIR(AtomicRMWInst *) const override; 333 }; 334 335 namespace AMDGPUISD { 336 337 enum NodeType : unsigned { 338 // AMDIL ISD Opcodes 339 FIRST_NUMBER = ISD::BUILTIN_OP_END, 340 UMUL, // 32bit unsigned multiplication 341 BRANCH_COND, 342 // End AMDIL ISD Opcodes 343 344 // Function call. 345 CALL, 346 TC_RETURN, 347 TRAP, 348 349 // Masked control flow nodes. 350 IF, 351 ELSE, 352 LOOP, 353 354 // A uniform kernel return that terminates the wavefront. 355 ENDPGM, 356 357 // Return to a shader part's epilog code. 358 RETURN_TO_EPILOG, 359 360 // Return with values from a non-entry function. 361 RET_FLAG, 362 363 DWORDADDR, 364 FRACT, 365 366 /// CLAMP value between 0.0 and 1.0. NaN clamped to 0, following clamp output 367 /// modifier behavior with dx10_enable. 368 CLAMP, 369 370 // This is SETCC with the full mask result which is used for a compare with a 371 // result bit per item in the wavefront. 372 SETCC, 373 SETREG, 374 375 DENORM_MODE, 376 377 // FP ops with input and output chain. 378 FMA_W_CHAIN, 379 FMUL_W_CHAIN, 380 381 // SIN_HW, COS_HW - f32 for SI, 1 ULP max error, valid from -100 pi to 100 pi. 382 // Denormals handled on some parts. 383 COS_HW, 384 SIN_HW, 385 FMAX_LEGACY, 386 FMIN_LEGACY, 387 388 FMAX3, 389 SMAX3, 390 UMAX3, 391 FMIN3, 392 SMIN3, 393 UMIN3, 394 FMED3, 395 SMED3, 396 UMED3, 397 FDOT2, 398 URECIP, 399 DIV_SCALE, 400 DIV_FMAS, 401 DIV_FIXUP, 402 // For emitting ISD::FMAD when f32 denormals are enabled because mac/mad is 403 // treated as an illegal operation. 404 FMAD_FTZ, 405 TRIG_PREOP, // 1 ULP max error for f64 406 407 // RCP, RSQ - For f32, 1 ULP max error, no denormal handling. 408 // For f64, max error 2^29 ULP, handles denormals. 409 RCP, 410 RSQ, 411 RCP_LEGACY, 412 RSQ_LEGACY, 413 RCP_IFLAG, 414 FMUL_LEGACY, 415 RSQ_CLAMP, 416 LDEXP, 417 FP_CLASS, 418 DOT4, 419 CARRY, 420 BORROW, 421 BFE_U32, // Extract range of bits with zero extension to 32-bits. 422 BFE_I32, // Extract range of bits with sign extension to 32-bits. 423 BFI, // (src0 & src1) | (~src0 & src2) 424 BFM, // Insert a range of bits into a 32-bit word. 425 FFBH_U32, // ctlz with -1 if input is zero. 426 FFBH_I32, 427 FFBL_B32, // cttz with -1 if input is zero. 428 MUL_U24, 429 MUL_I24, 430 MULHI_U24, 431 MULHI_I24, 432 MAD_U24, 433 MAD_I24, 434 MAD_U64_U32, 435 MAD_I64_I32, 436 MUL_LOHI_I24, 437 MUL_LOHI_U24, 438 PERM, 439 TEXTURE_FETCH, 440 R600_EXPORT, 441 CONST_ADDRESS, 442 REGISTER_LOAD, 443 REGISTER_STORE, 444 SAMPLE, 445 SAMPLEB, 446 SAMPLED, 447 SAMPLEL, 448 449 // These cvt_f32_ubyte* nodes need to remain consecutive and in order. 450 CVT_F32_UBYTE0, 451 CVT_F32_UBYTE1, 452 CVT_F32_UBYTE2, 453 CVT_F32_UBYTE3, 454 455 // Convert two float 32 numbers into a single register holding two packed f16 456 // with round to zero. 457 CVT_PKRTZ_F16_F32, 458 CVT_PKNORM_I16_F32, 459 CVT_PKNORM_U16_F32, 460 CVT_PK_I16_I32, 461 CVT_PK_U16_U32, 462 463 // Same as the standard node, except the high bits of the resulting integer 464 // are known 0. 465 FP_TO_FP16, 466 467 // Wrapper around fp16 results that are known to zero the high bits. 468 FP16_ZEXT, 469 470 /// This node is for VLIW targets and it is used to represent a vector 471 /// that is stored in consecutive registers with the same channel. 472 /// For example: 473 /// |X |Y|Z|W| 474 /// T0|v.x| | | | 475 /// T1|v.y| | | | 476 /// T2|v.z| | | | 477 /// T3|v.w| | | | 478 BUILD_VERTICAL_VECTOR, 479 /// Pointer to the start of the shader's constant data. 480 CONST_DATA_PTR, 481 PC_ADD_REL_OFFSET, 482 LDS, 483 DUMMY_CHAIN, 484 FIRST_MEM_OPCODE_NUMBER = ISD::FIRST_TARGET_MEMORY_OPCODE, 485 LOAD_D16_HI, 486 LOAD_D16_LO, 487 LOAD_D16_HI_I8, 488 LOAD_D16_HI_U8, 489 LOAD_D16_LO_I8, 490 LOAD_D16_LO_U8, 491 492 STORE_MSKOR, 493 LOAD_CONSTANT, 494 TBUFFER_STORE_FORMAT, 495 TBUFFER_STORE_FORMAT_D16, 496 TBUFFER_LOAD_FORMAT, 497 TBUFFER_LOAD_FORMAT_D16, 498 DS_ORDERED_COUNT, 499 ATOMIC_CMP_SWAP, 500 ATOMIC_INC, 501 ATOMIC_DEC, 502 ATOMIC_LOAD_FMIN, 503 ATOMIC_LOAD_FMAX, 504 BUFFER_LOAD, 505 BUFFER_LOAD_UBYTE, 506 BUFFER_LOAD_USHORT, 507 BUFFER_LOAD_BYTE, 508 BUFFER_LOAD_SHORT, 509 BUFFER_LOAD_FORMAT, 510 BUFFER_LOAD_FORMAT_D16, 511 SBUFFER_LOAD, 512 BUFFER_STORE, 513 BUFFER_STORE_BYTE, 514 BUFFER_STORE_SHORT, 515 BUFFER_STORE_FORMAT, 516 BUFFER_STORE_FORMAT_D16, 517 BUFFER_ATOMIC_SWAP, 518 BUFFER_ATOMIC_ADD, 519 BUFFER_ATOMIC_SUB, 520 BUFFER_ATOMIC_SMIN, 521 BUFFER_ATOMIC_UMIN, 522 BUFFER_ATOMIC_SMAX, 523 BUFFER_ATOMIC_UMAX, 524 BUFFER_ATOMIC_AND, 525 BUFFER_ATOMIC_OR, 526 BUFFER_ATOMIC_XOR, 527 BUFFER_ATOMIC_INC, 528 BUFFER_ATOMIC_DEC, 529 BUFFER_ATOMIC_CMPSWAP, 530 BUFFER_ATOMIC_FADD, 531 BUFFER_ATOMIC_PK_FADD, 532 ATOMIC_PK_FADD, 533 534 LAST_AMDGPU_ISD_NUMBER 535 }; 536 537 538 } // End namespace AMDGPUISD 539 540 } // End namespace llvm 541 542 #endif 543