1 //===-- AMDGPUISelLowering.h - AMDGPU Lowering Interface --------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 /// \file 11 /// \brief Interface definition of the TargetLowering class that is common 12 /// to all AMD GPUs. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H 17 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H 18 19 #include "llvm/Target/TargetLowering.h" 20 21 namespace llvm { 22 23 class AMDGPUMachineFunction; 24 class AMDGPUSubtarget; 25 class MachineRegisterInfo; 26 27 class AMDGPUTargetLowering : public TargetLowering { 28 protected: 29 const AMDGPUSubtarget *Subtarget; 30 31 SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const; 32 SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const; 33 SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const; 34 /// \brief Split a vector store into multiple scalar stores. 35 /// \returns The resulting chain. 36 37 SDValue LowerFREM(SDValue Op, SelectionDAG &DAG) const; 38 SDValue LowerFCEIL(SDValue Op, SelectionDAG &DAG) const; 39 SDValue LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const; 40 SDValue LowerFRINT(SDValue Op, SelectionDAG &DAG) const; 41 SDValue LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const; 42 43 SDValue LowerFROUND32(SDValue Op, SelectionDAG &DAG) const; 44 SDValue LowerFROUND64(SDValue Op, SelectionDAG &DAG) const; 45 SDValue LowerFROUND(SDValue Op, SelectionDAG &DAG) const; 46 SDValue LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const; 47 48 SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) const; 49 50 SDValue LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG, bool Signed) const; 51 SDValue LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG, bool Signed) const; 52 SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) const; 53 SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) const; 54 55 SDValue LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG, bool Signed) const; 56 SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const; 57 SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const; 58 59 SDValue LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const; 60 61 protected: 62 bool shouldCombineMemoryType(EVT VT) const; 63 SDValue performLoadCombine(SDNode *N, DAGCombinerInfo &DCI) const; 64 SDValue performStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const; 65 66 SDValue splitBinaryBitConstantOpImpl(DAGCombinerInfo &DCI, const SDLoc &SL, 67 unsigned Opc, SDValue LHS, 68 uint32_t ValLo, uint32_t ValHi) const; 69 SDValue performShlCombine(SDNode *N, DAGCombinerInfo &DCI) const; 70 SDValue performSraCombine(SDNode *N, DAGCombinerInfo &DCI) const; 71 SDValue performSrlCombine(SDNode *N, DAGCombinerInfo &DCI) const; 72 SDValue performMulCombine(SDNode *N, DAGCombinerInfo &DCI) const; 73 SDValue performMulhsCombine(SDNode *N, DAGCombinerInfo &DCI) const; 74 SDValue performMulhuCombine(SDNode *N, DAGCombinerInfo &DCI) const; 75 SDValue performMulLoHi24Combine(SDNode *N, DAGCombinerInfo &DCI) const; 76 SDValue performCtlzCombine(const SDLoc &SL, SDValue Cond, SDValue LHS, 77 SDValue RHS, DAGCombinerInfo &DCI) const; 78 SDValue performSelectCombine(SDNode *N, DAGCombinerInfo &DCI) const; 79 80 static EVT getEquivalentMemType(LLVMContext &Context, EVT VT); 81 82 virtual SDValue LowerGlobalAddress(AMDGPUMachineFunction *MFI, SDValue Op, 83 SelectionDAG &DAG) const; 84 85 /// Return 64-bit value Op as two 32-bit integers. 86 std::pair<SDValue, SDValue> split64BitValue(SDValue Op, 87 SelectionDAG &DAG) const; 88 SDValue getLoHalf64(SDValue Op, SelectionDAG &DAG) const; 89 SDValue getHiHalf64(SDValue Op, SelectionDAG &DAG) const; 90 91 /// \brief Split a vector load into 2 loads of half the vector. 92 SDValue SplitVectorLoad(SDValue Op, SelectionDAG &DAG) const; 93 94 /// \brief Split a vector store into 2 stores of half the vector. 95 SDValue SplitVectorStore(SDValue Op, SelectionDAG &DAG) const; 96 97 SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const; 98 SDValue LowerSDIVREM(SDValue Op, SelectionDAG &DAG) const; 99 SDValue LowerUDIVREM(SDValue Op, SelectionDAG &DAG) const; 100 SDValue LowerDIVREM24(SDValue Op, SelectionDAG &DAG, bool sign) const; 101 void LowerUDIVREM64(SDValue Op, SelectionDAG &DAG, 102 SmallVectorImpl<SDValue> &Results) const; 103 void analyzeFormalArgumentsCompute(CCState &State, 104 const SmallVectorImpl<ISD::InputArg> &Ins) const; 105 void AnalyzeFormalArguments(CCState &State, 106 const SmallVectorImpl<ISD::InputArg> &Ins) const; 107 void AnalyzeReturn(CCState &State, 108 const SmallVectorImpl<ISD::OutputArg> &Outs) const; 109 110 public: 111 AMDGPUTargetLowering(const TargetMachine &TM, const AMDGPUSubtarget &STI); 112 113 bool isFAbsFree(EVT VT) const override; 114 bool isFNegFree(EVT VT) const override; 115 bool isTruncateFree(EVT Src, EVT Dest) const override; 116 bool isTruncateFree(Type *Src, Type *Dest) const override; 117 118 bool isZExtFree(Type *Src, Type *Dest) const override; 119 bool isZExtFree(EVT Src, EVT Dest) const override; 120 bool isZExtFree(SDValue Val, EVT VT2) const override; 121 122 bool isNarrowingProfitable(EVT VT1, EVT VT2) const override; 123 124 MVT getVectorIdxTy(const DataLayout &) const override; 125 bool isSelectSupported(SelectSupportKind) const override; 126 127 bool isFPImmLegal(const APFloat &Imm, EVT VT) const override; 128 bool ShouldShrinkFPConstant(EVT VT) const override; 129 bool shouldReduceLoadWidth(SDNode *Load, 130 ISD::LoadExtType ExtType, 131 EVT ExtVT) const override; 132 133 bool isLoadBitCastBeneficial(EVT, EVT) const final; 134 135 bool storeOfVectorConstantIsCheap(EVT MemVT, 136 unsigned NumElem, 137 unsigned AS) const override; 138 bool aggressivelyPreferBuildVectorSources(EVT VecVT) const override; 139 bool isCheapToSpeculateCttz() const override; 140 bool isCheapToSpeculateCtlz() const override; 141 142 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 143 const SmallVectorImpl<ISD::OutputArg> &Outs, 144 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL, 145 SelectionDAG &DAG) const override; 146 SDValue LowerCall(CallLoweringInfo &CLI, 147 SmallVectorImpl<SDValue> &InVals) const override; 148 149 SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, 150 SelectionDAG &DAG) const; 151 152 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override; 153 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override; 154 void ReplaceNodeResults(SDNode * N, 155 SmallVectorImpl<SDValue> &Results, 156 SelectionDAG &DAG) const override; 157 158 SDValue CombineFMinMaxLegacy(const SDLoc &DL, EVT VT, SDValue LHS, 159 SDValue RHS, SDValue True, SDValue False, 160 SDValue CC, DAGCombinerInfo &DCI) const; 161 162 const char* getTargetNodeName(unsigned Opcode) const override; 163 164 bool isFsqrtCheap(SDValue Operand, SelectionDAG &DAG) const override { 165 return true; 166 } 167 SDValue getRsqrtEstimate(SDValue Operand, 168 DAGCombinerInfo &DCI, 169 unsigned &RefinementSteps, 170 bool &UseOneConstNR) const override; 171 SDValue getRecipEstimate(SDValue Operand, 172 DAGCombinerInfo &DCI, 173 unsigned &RefinementSteps) const override; 174 175 virtual SDNode *PostISelFolding(MachineSDNode *N, 176 SelectionDAG &DAG) const = 0; 177 178 /// \brief Determine which of the bits specified in \p Mask are known to be 179 /// either zero or one and return them in the \p KnownZero and \p KnownOne 180 /// bitsets. 181 void computeKnownBitsForTargetNode(const SDValue Op, 182 APInt &KnownZero, 183 APInt &KnownOne, 184 const SelectionDAG &DAG, 185 unsigned Depth = 0) const override; 186 187 unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const SelectionDAG &DAG, 188 unsigned Depth = 0) const override; 189 190 /// \brief Helper function that adds Reg to the LiveIn list of the DAG's 191 /// MachineFunction. 192 /// 193 /// \returns a RegisterSDNode representing Reg. 194 virtual SDValue CreateLiveInRegister(SelectionDAG &DAG, 195 const TargetRegisterClass *RC, 196 unsigned Reg, EVT VT) const; 197 198 enum ImplicitParameter { 199 FIRST_IMPLICIT, 200 GRID_DIM = FIRST_IMPLICIT, 201 GRID_OFFSET, 202 }; 203 204 /// \brief Helper function that returns the byte offset of the given 205 /// type of implicit parameter. 206 uint32_t getImplicitParameterOffset(const AMDGPUMachineFunction *MFI, 207 const ImplicitParameter Param) const; 208 }; 209 210 namespace AMDGPUISD { 211 212 enum NodeType : unsigned { 213 // AMDIL ISD Opcodes 214 FIRST_NUMBER = ISD::BUILTIN_OP_END, 215 CALL, // Function call based on a single integer 216 UMUL, // 32bit unsigned multiplication 217 BRANCH_COND, 218 // End AMDIL ISD Opcodes 219 ENDPGM, 220 RETURN, 221 DWORDADDR, 222 FRACT, 223 CLAMP, 224 // This is SETCC with the full mask result which is used for a compare with a 225 // result bit per item in the wavefront. 226 SETCC, 227 228 // SIN_HW, COS_HW - f32 for SI, 1 ULP max error, valid from -100 pi to 100 pi. 229 // Denormals handled on some parts. 230 COS_HW, 231 SIN_HW, 232 FMAX_LEGACY, 233 FMIN_LEGACY, 234 FMAX3, 235 SMAX3, 236 UMAX3, 237 FMIN3, 238 SMIN3, 239 UMIN3, 240 FMED3, 241 SMED3, 242 UMED3, 243 URECIP, 244 DIV_SCALE, 245 DIV_FMAS, 246 DIV_FIXUP, 247 TRIG_PREOP, // 1 ULP max error for f64 248 249 // RCP, RSQ - For f32, 1 ULP max error, no denormal handling. 250 // For f64, max error 2^29 ULP, handles denormals. 251 RCP, 252 RSQ, 253 RCP_LEGACY, 254 RSQ_LEGACY, 255 FMUL_LEGACY, 256 RSQ_CLAMP, 257 LDEXP, 258 FP_CLASS, 259 DOT4, 260 CARRY, 261 BORROW, 262 BFE_U32, // Extract range of bits with zero extension to 32-bits. 263 BFE_I32, // Extract range of bits with sign extension to 32-bits. 264 BFI, // (src0 & src1) | (~src0 & src2) 265 BFM, // Insert a range of bits into a 32-bit word. 266 FFBH_U32, // ctlz with -1 if input is zero. 267 FFBH_I32, 268 MUL_U24, 269 MUL_I24, 270 MULHI_U24, 271 MULHI_I24, 272 MAD_U24, 273 MAD_I24, 274 MUL_LOHI_I24, 275 MUL_LOHI_U24, 276 TEXTURE_FETCH, 277 EXPORT, 278 CONST_ADDRESS, 279 REGISTER_LOAD, 280 REGISTER_STORE, 281 LOAD_INPUT, 282 SAMPLE, 283 SAMPLEB, 284 SAMPLED, 285 SAMPLEL, 286 287 // These cvt_f32_ubyte* nodes need to remain consecutive and in order. 288 CVT_F32_UBYTE0, 289 CVT_F32_UBYTE1, 290 CVT_F32_UBYTE2, 291 CVT_F32_UBYTE3, 292 /// This node is for VLIW targets and it is used to represent a vector 293 /// that is stored in consecutive registers with the same channel. 294 /// For example: 295 /// |X |Y|Z|W| 296 /// T0|v.x| | | | 297 /// T1|v.y| | | | 298 /// T2|v.z| | | | 299 /// T3|v.w| | | | 300 BUILD_VERTICAL_VECTOR, 301 /// Pointer to the start of the shader's constant data. 302 CONST_DATA_PTR, 303 SENDMSG, 304 INTERP_MOV, 305 INTERP_P1, 306 INTERP_P2, 307 PC_ADD_REL_OFFSET, 308 KILL, 309 FIRST_MEM_OPCODE_NUMBER = ISD::FIRST_TARGET_MEMORY_OPCODE, 310 STORE_MSKOR, 311 LOAD_CONSTANT, 312 TBUFFER_STORE_FORMAT, 313 ATOMIC_CMP_SWAP, 314 ATOMIC_INC, 315 ATOMIC_DEC, 316 LAST_AMDGPU_ISD_NUMBER 317 }; 318 319 320 } // End namespace AMDGPUISD 321 322 } // End namespace llvm 323 324 #endif 325