1 //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===// 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 /// Custom DAG lowering for SI 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifdef _MSC_VER 16 // Provide M_PI. 17 #define _USE_MATH_DEFINES 18 #endif 19 20 #include "SIISelLowering.h" 21 #include "AMDGPU.h" 22 #include "AMDGPUIntrinsicInfo.h" 23 #include "AMDGPUSubtarget.h" 24 #include "AMDGPUTargetMachine.h" 25 #include "SIDefines.h" 26 #include "SIInstrInfo.h" 27 #include "SIMachineFunctionInfo.h" 28 #include "SIRegisterInfo.h" 29 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 30 #include "Utils/AMDGPUBaseInfo.h" 31 #include "llvm/ADT/APFloat.h" 32 #include "llvm/ADT/APInt.h" 33 #include "llvm/ADT/ArrayRef.h" 34 #include "llvm/ADT/BitVector.h" 35 #include "llvm/ADT/SmallVector.h" 36 #include "llvm/ADT/Statistic.h" 37 #include "llvm/ADT/StringRef.h" 38 #include "llvm/ADT/StringSwitch.h" 39 #include "llvm/ADT/Twine.h" 40 #include "llvm/CodeGen/Analysis.h" 41 #include "llvm/CodeGen/CallingConvLower.h" 42 #include "llvm/CodeGen/DAGCombine.h" 43 #include "llvm/CodeGen/ISDOpcodes.h" 44 #include "llvm/CodeGen/MachineBasicBlock.h" 45 #include "llvm/CodeGen/MachineFrameInfo.h" 46 #include "llvm/CodeGen/MachineFunction.h" 47 #include "llvm/CodeGen/MachineInstr.h" 48 #include "llvm/CodeGen/MachineInstrBuilder.h" 49 #include "llvm/CodeGen/MachineMemOperand.h" 50 #include "llvm/CodeGen/MachineModuleInfo.h" 51 #include "llvm/CodeGen/MachineOperand.h" 52 #include "llvm/CodeGen/MachineRegisterInfo.h" 53 #include "llvm/CodeGen/SelectionDAG.h" 54 #include "llvm/CodeGen/SelectionDAGNodes.h" 55 #include "llvm/CodeGen/TargetCallingConv.h" 56 #include "llvm/CodeGen/TargetRegisterInfo.h" 57 #include "llvm/CodeGen/ValueTypes.h" 58 #include "llvm/IR/Constants.h" 59 #include "llvm/IR/DataLayout.h" 60 #include "llvm/IR/DebugLoc.h" 61 #include "llvm/IR/DerivedTypes.h" 62 #include "llvm/IR/DiagnosticInfo.h" 63 #include "llvm/IR/Function.h" 64 #include "llvm/IR/GlobalValue.h" 65 #include "llvm/IR/InstrTypes.h" 66 #include "llvm/IR/Instruction.h" 67 #include "llvm/IR/Instructions.h" 68 #include "llvm/IR/IntrinsicInst.h" 69 #include "llvm/IR/Type.h" 70 #include "llvm/Support/Casting.h" 71 #include "llvm/Support/CodeGen.h" 72 #include "llvm/Support/CommandLine.h" 73 #include "llvm/Support/Compiler.h" 74 #include "llvm/Support/ErrorHandling.h" 75 #include "llvm/Support/KnownBits.h" 76 #include "llvm/Support/MachineValueType.h" 77 #include "llvm/Support/MathExtras.h" 78 #include "llvm/Target/TargetOptions.h" 79 #include <cassert> 80 #include <cmath> 81 #include <cstdint> 82 #include <iterator> 83 #include <tuple> 84 #include <utility> 85 #include <vector> 86 87 using namespace llvm; 88 89 #define DEBUG_TYPE "si-lower" 90 91 STATISTIC(NumTailCalls, "Number of tail calls"); 92 93 static cl::opt<bool> EnableVGPRIndexMode( 94 "amdgpu-vgpr-index-mode", 95 cl::desc("Use GPR indexing mode instead of movrel for vector indexing"), 96 cl::init(false)); 97 98 static cl::opt<unsigned> AssumeFrameIndexHighZeroBits( 99 "amdgpu-frame-index-zero-bits", 100 cl::desc("High bits of frame index assumed to be zero"), 101 cl::init(5), 102 cl::ReallyHidden); 103 104 static unsigned findFirstFreeSGPR(CCState &CCInfo) { 105 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs(); 106 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) { 107 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) { 108 return AMDGPU::SGPR0 + Reg; 109 } 110 } 111 llvm_unreachable("Cannot allocate sgpr"); 112 } 113 114 SITargetLowering::SITargetLowering(const TargetMachine &TM, 115 const GCNSubtarget &STI) 116 : AMDGPUTargetLowering(TM, STI), 117 Subtarget(&STI) { 118 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass); 119 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); 120 121 addRegisterClass(MVT::i32, &AMDGPU::SReg_32_XM0RegClass); 122 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass); 123 124 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass); 125 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass); 126 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass); 127 128 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass); 129 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass); 130 131 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass); 132 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); 133 134 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass); 135 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass); 136 137 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass); 138 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass); 139 140 if (Subtarget->has16BitInsts()) { 141 addRegisterClass(MVT::i16, &AMDGPU::SReg_32_XM0RegClass); 142 addRegisterClass(MVT::f16, &AMDGPU::SReg_32_XM0RegClass); 143 144 // Unless there are also VOP3P operations, not operations are really legal. 145 addRegisterClass(MVT::v2i16, &AMDGPU::SReg_32_XM0RegClass); 146 addRegisterClass(MVT::v2f16, &AMDGPU::SReg_32_XM0RegClass); 147 addRegisterClass(MVT::v4i16, &AMDGPU::SReg_64RegClass); 148 addRegisterClass(MVT::v4f16, &AMDGPU::SReg_64RegClass); 149 } 150 151 computeRegisterProperties(Subtarget->getRegisterInfo()); 152 153 // We need to custom lower vector stores from local memory 154 setOperationAction(ISD::LOAD, MVT::v2i32, Custom); 155 setOperationAction(ISD::LOAD, MVT::v4i32, Custom); 156 setOperationAction(ISD::LOAD, MVT::v8i32, Custom); 157 setOperationAction(ISD::LOAD, MVT::v16i32, Custom); 158 setOperationAction(ISD::LOAD, MVT::i1, Custom); 159 160 setOperationAction(ISD::STORE, MVT::v2i32, Custom); 161 setOperationAction(ISD::STORE, MVT::v4i32, Custom); 162 setOperationAction(ISD::STORE, MVT::v8i32, Custom); 163 setOperationAction(ISD::STORE, MVT::v16i32, Custom); 164 setOperationAction(ISD::STORE, MVT::i1, Custom); 165 166 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Expand); 167 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand); 168 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand); 169 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand); 170 setTruncStoreAction(MVT::v32i32, MVT::v32i16, Expand); 171 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Expand); 172 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Expand); 173 setTruncStoreAction(MVT::v8i32, MVT::v8i8, Expand); 174 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand); 175 setTruncStoreAction(MVT::v32i32, MVT::v32i8, Expand); 176 177 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 178 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 179 180 setOperationAction(ISD::SELECT, MVT::i1, Promote); 181 setOperationAction(ISD::SELECT, MVT::i64, Custom); 182 setOperationAction(ISD::SELECT, MVT::f64, Promote); 183 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64); 184 185 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); 186 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); 187 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 188 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); 189 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand); 190 191 setOperationAction(ISD::SETCC, MVT::i1, Promote); 192 setOperationAction(ISD::SETCC, MVT::v2i1, Expand); 193 setOperationAction(ISD::SETCC, MVT::v4i1, Expand); 194 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32); 195 196 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand); 197 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 198 199 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom); 200 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom); 201 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); 202 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom); 203 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); 204 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom); 205 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom); 206 207 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); 208 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom); 209 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom); 210 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i16, Custom); 211 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f16, Custom); 212 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v2i16, Custom); 213 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v2f16, Custom); 214 215 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v2f16, Custom); 216 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::v4f16, Custom); 217 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); 218 219 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); 220 setOperationAction(ISD::INTRINSIC_VOID, MVT::v2i16, Custom); 221 setOperationAction(ISD::INTRINSIC_VOID, MVT::v2f16, Custom); 222 setOperationAction(ISD::INTRINSIC_VOID, MVT::v4f16, Custom); 223 224 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 225 setOperationAction(ISD::BR_CC, MVT::i1, Expand); 226 setOperationAction(ISD::BR_CC, MVT::i32, Expand); 227 setOperationAction(ISD::BR_CC, MVT::i64, Expand); 228 setOperationAction(ISD::BR_CC, MVT::f32, Expand); 229 setOperationAction(ISD::BR_CC, MVT::f64, Expand); 230 231 setOperationAction(ISD::UADDO, MVT::i32, Legal); 232 setOperationAction(ISD::USUBO, MVT::i32, Legal); 233 234 setOperationAction(ISD::ADDCARRY, MVT::i32, Legal); 235 setOperationAction(ISD::SUBCARRY, MVT::i32, Legal); 236 237 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand); 238 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand); 239 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand); 240 241 #if 0 242 setOperationAction(ISD::ADDCARRY, MVT::i64, Legal); 243 setOperationAction(ISD::SUBCARRY, MVT::i64, Legal); 244 #endif 245 246 // We only support LOAD/STORE and vector manipulation ops for vectors 247 // with > 4 elements. 248 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, 249 MVT::v2i64, MVT::v2f64, MVT::v4i16, MVT::v4f16 }) { 250 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { 251 switch (Op) { 252 case ISD::LOAD: 253 case ISD::STORE: 254 case ISD::BUILD_VECTOR: 255 case ISD::BITCAST: 256 case ISD::EXTRACT_VECTOR_ELT: 257 case ISD::INSERT_VECTOR_ELT: 258 case ISD::INSERT_SUBVECTOR: 259 case ISD::EXTRACT_SUBVECTOR: 260 case ISD::SCALAR_TO_VECTOR: 261 break; 262 case ISD::CONCAT_VECTORS: 263 setOperationAction(Op, VT, Custom); 264 break; 265 default: 266 setOperationAction(Op, VT, Expand); 267 break; 268 } 269 } 270 } 271 272 setOperationAction(ISD::FP_EXTEND, MVT::v4f32, Expand); 273 274 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that 275 // is expanded to avoid having two separate loops in case the index is a VGPR. 276 277 // Most operations are naturally 32-bit vector operations. We only support 278 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32. 279 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) { 280 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote); 281 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32); 282 283 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote); 284 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32); 285 286 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote); 287 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32); 288 289 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote); 290 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32); 291 } 292 293 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand); 294 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand); 295 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand); 296 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand); 297 298 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f16, Custom); 299 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom); 300 301 // Avoid stack access for these. 302 // TODO: Generalize to more vector types. 303 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i16, Custom); 304 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f16, Custom); 305 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom); 306 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f16, Custom); 307 308 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i16, Custom); 309 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f16, Custom); 310 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i8, Custom); 311 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i8, Custom); 312 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i8, Custom); 313 314 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i8, Custom); 315 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i8, Custom); 316 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i8, Custom); 317 318 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i16, Custom); 319 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f16, Custom); 320 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom); 321 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f16, Custom); 322 323 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling, 324 // and output demarshalling 325 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); 326 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 327 328 // We can't return success/failure, only the old value, 329 // let LLVM add the comparison 330 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand); 331 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand); 332 333 if (Subtarget->hasFlatAddressSpace()) { 334 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom); 335 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom); 336 } 337 338 setOperationAction(ISD::BSWAP, MVT::i32, Legal); 339 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); 340 341 // On SI this is s_memtime and s_memrealtime on VI. 342 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal); 343 setOperationAction(ISD::TRAP, MVT::Other, Custom); 344 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Custom); 345 346 if (Subtarget->has16BitInsts()) { 347 setOperationAction(ISD::FLOG, MVT::f16, Custom); 348 setOperationAction(ISD::FEXP, MVT::f16, Custom); 349 setOperationAction(ISD::FLOG10, MVT::f16, Custom); 350 } 351 352 // v_mad_f32 does not support denormals according to some sources. 353 if (!Subtarget->hasFP32Denormals()) 354 setOperationAction(ISD::FMAD, MVT::f32, Legal); 355 356 if (!Subtarget->hasBFI()) { 357 // fcopysign can be done in a single instruction with BFI. 358 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 359 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 360 } 361 362 if (!Subtarget->hasBCNT(32)) 363 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 364 365 if (!Subtarget->hasBCNT(64)) 366 setOperationAction(ISD::CTPOP, MVT::i64, Expand); 367 368 if (Subtarget->hasFFBH()) 369 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom); 370 371 if (Subtarget->hasFFBL()) 372 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Custom); 373 374 // We only really have 32-bit BFE instructions (and 16-bit on VI). 375 // 376 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any 377 // effort to match them now. We want this to be false for i64 cases when the 378 // extraction isn't restricted to the upper or lower half. Ideally we would 379 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that 380 // span the midpoint are probably relatively rare, so don't worry about them 381 // for now. 382 if (Subtarget->hasBFE()) 383 setHasExtractBitsInsn(true); 384 385 setOperationAction(ISD::FMINNUM, MVT::f64, Legal); 386 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); 387 388 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) { 389 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 390 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 391 setOperationAction(ISD::FRINT, MVT::f64, Legal); 392 } else { 393 setOperationAction(ISD::FCEIL, MVT::f64, Custom); 394 setOperationAction(ISD::FTRUNC, MVT::f64, Custom); 395 setOperationAction(ISD::FRINT, MVT::f64, Custom); 396 setOperationAction(ISD::FFLOOR, MVT::f64, Custom); 397 } 398 399 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 400 401 setOperationAction(ISD::FSIN, MVT::f32, Custom); 402 setOperationAction(ISD::FCOS, MVT::f32, Custom); 403 setOperationAction(ISD::FDIV, MVT::f32, Custom); 404 setOperationAction(ISD::FDIV, MVT::f64, Custom); 405 406 if (Subtarget->has16BitInsts()) { 407 setOperationAction(ISD::Constant, MVT::i16, Legal); 408 409 setOperationAction(ISD::SMIN, MVT::i16, Legal); 410 setOperationAction(ISD::SMAX, MVT::i16, Legal); 411 412 setOperationAction(ISD::UMIN, MVT::i16, Legal); 413 setOperationAction(ISD::UMAX, MVT::i16, Legal); 414 415 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Promote); 416 AddPromotedToType(ISD::SIGN_EXTEND, MVT::i16, MVT::i32); 417 418 setOperationAction(ISD::ROTR, MVT::i16, Promote); 419 setOperationAction(ISD::ROTL, MVT::i16, Promote); 420 421 setOperationAction(ISD::SDIV, MVT::i16, Promote); 422 setOperationAction(ISD::UDIV, MVT::i16, Promote); 423 setOperationAction(ISD::SREM, MVT::i16, Promote); 424 setOperationAction(ISD::UREM, MVT::i16, Promote); 425 426 setOperationAction(ISD::BSWAP, MVT::i16, Promote); 427 setOperationAction(ISD::BITREVERSE, MVT::i16, Promote); 428 429 setOperationAction(ISD::CTTZ, MVT::i16, Promote); 430 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote); 431 setOperationAction(ISD::CTLZ, MVT::i16, Promote); 432 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote); 433 setOperationAction(ISD::CTPOP, MVT::i16, Promote); 434 435 setOperationAction(ISD::SELECT_CC, MVT::i16, Expand); 436 437 setOperationAction(ISD::BR_CC, MVT::i16, Expand); 438 439 setOperationAction(ISD::LOAD, MVT::i16, Custom); 440 441 setTruncStoreAction(MVT::i64, MVT::i16, Expand); 442 443 setOperationAction(ISD::FP16_TO_FP, MVT::i16, Promote); 444 AddPromotedToType(ISD::FP16_TO_FP, MVT::i16, MVT::i32); 445 setOperationAction(ISD::FP_TO_FP16, MVT::i16, Promote); 446 AddPromotedToType(ISD::FP_TO_FP16, MVT::i16, MVT::i32); 447 448 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote); 449 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote); 450 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote); 451 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote); 452 453 // F16 - Constant Actions. 454 setOperationAction(ISD::ConstantFP, MVT::f16, Legal); 455 456 // F16 - Load/Store Actions. 457 setOperationAction(ISD::LOAD, MVT::f16, Promote); 458 AddPromotedToType(ISD::LOAD, MVT::f16, MVT::i16); 459 setOperationAction(ISD::STORE, MVT::f16, Promote); 460 AddPromotedToType(ISD::STORE, MVT::f16, MVT::i16); 461 462 // F16 - VOP1 Actions. 463 setOperationAction(ISD::FP_ROUND, MVT::f16, Custom); 464 setOperationAction(ISD::FCOS, MVT::f16, Promote); 465 setOperationAction(ISD::FSIN, MVT::f16, Promote); 466 setOperationAction(ISD::FP_TO_SINT, MVT::f16, Promote); 467 setOperationAction(ISD::FP_TO_UINT, MVT::f16, Promote); 468 setOperationAction(ISD::SINT_TO_FP, MVT::f16, Promote); 469 setOperationAction(ISD::UINT_TO_FP, MVT::f16, Promote); 470 setOperationAction(ISD::FROUND, MVT::f16, Custom); 471 472 // F16 - VOP2 Actions. 473 setOperationAction(ISD::BR_CC, MVT::f16, Expand); 474 setOperationAction(ISD::SELECT_CC, MVT::f16, Expand); 475 setOperationAction(ISD::FMAXNUM, MVT::f16, Legal); 476 setOperationAction(ISD::FMINNUM, MVT::f16, Legal); 477 setOperationAction(ISD::FDIV, MVT::f16, Custom); 478 479 // F16 - VOP3 Actions. 480 setOperationAction(ISD::FMA, MVT::f16, Legal); 481 if (!Subtarget->hasFP16Denormals()) 482 setOperationAction(ISD::FMAD, MVT::f16, Legal); 483 484 for (MVT VT : {MVT::v2i16, MVT::v2f16, MVT::v4i16, MVT::v4f16}) { 485 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { 486 switch (Op) { 487 case ISD::LOAD: 488 case ISD::STORE: 489 case ISD::BUILD_VECTOR: 490 case ISD::BITCAST: 491 case ISD::EXTRACT_VECTOR_ELT: 492 case ISD::INSERT_VECTOR_ELT: 493 case ISD::INSERT_SUBVECTOR: 494 case ISD::EXTRACT_SUBVECTOR: 495 case ISD::SCALAR_TO_VECTOR: 496 break; 497 case ISD::CONCAT_VECTORS: 498 setOperationAction(Op, VT, Custom); 499 break; 500 default: 501 setOperationAction(Op, VT, Expand); 502 break; 503 } 504 } 505 } 506 507 // XXX - Do these do anything? Vector constants turn into build_vector. 508 setOperationAction(ISD::Constant, MVT::v2i16, Legal); 509 setOperationAction(ISD::ConstantFP, MVT::v2f16, Legal); 510 511 setOperationAction(ISD::UNDEF, MVT::v2i16, Legal); 512 setOperationAction(ISD::UNDEF, MVT::v2f16, Legal); 513 514 setOperationAction(ISD::STORE, MVT::v2i16, Promote); 515 AddPromotedToType(ISD::STORE, MVT::v2i16, MVT::i32); 516 setOperationAction(ISD::STORE, MVT::v2f16, Promote); 517 AddPromotedToType(ISD::STORE, MVT::v2f16, MVT::i32); 518 519 setOperationAction(ISD::LOAD, MVT::v2i16, Promote); 520 AddPromotedToType(ISD::LOAD, MVT::v2i16, MVT::i32); 521 setOperationAction(ISD::LOAD, MVT::v2f16, Promote); 522 AddPromotedToType(ISD::LOAD, MVT::v2f16, MVT::i32); 523 524 setOperationAction(ISD::AND, MVT::v2i16, Promote); 525 AddPromotedToType(ISD::AND, MVT::v2i16, MVT::i32); 526 setOperationAction(ISD::OR, MVT::v2i16, Promote); 527 AddPromotedToType(ISD::OR, MVT::v2i16, MVT::i32); 528 setOperationAction(ISD::XOR, MVT::v2i16, Promote); 529 AddPromotedToType(ISD::XOR, MVT::v2i16, MVT::i32); 530 531 setOperationAction(ISD::LOAD, MVT::v4i16, Promote); 532 AddPromotedToType(ISD::LOAD, MVT::v4i16, MVT::v2i32); 533 setOperationAction(ISD::LOAD, MVT::v4f16, Promote); 534 AddPromotedToType(ISD::LOAD, MVT::v4f16, MVT::v2i32); 535 536 setOperationAction(ISD::STORE, MVT::v4i16, Promote); 537 AddPromotedToType(ISD::STORE, MVT::v4i16, MVT::v2i32); 538 setOperationAction(ISD::STORE, MVT::v4f16, Promote); 539 AddPromotedToType(ISD::STORE, MVT::v4f16, MVT::v2i32); 540 541 setOperationAction(ISD::ANY_EXTEND, MVT::v2i32, Expand); 542 setOperationAction(ISD::ZERO_EXTEND, MVT::v2i32, Expand); 543 setOperationAction(ISD::SIGN_EXTEND, MVT::v2i32, Expand); 544 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Expand); 545 546 setOperationAction(ISD::ANY_EXTEND, MVT::v4i32, Expand); 547 setOperationAction(ISD::ZERO_EXTEND, MVT::v4i32, Expand); 548 setOperationAction(ISD::SIGN_EXTEND, MVT::v4i32, Expand); 549 550 if (!Subtarget->hasVOP3PInsts()) { 551 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i16, Custom); 552 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f16, Custom); 553 } 554 555 setOperationAction(ISD::FNEG, MVT::v2f16, Legal); 556 // This isn't really legal, but this avoids the legalizer unrolling it (and 557 // allows matching fneg (fabs x) patterns) 558 setOperationAction(ISD::FABS, MVT::v2f16, Legal); 559 } 560 561 if (Subtarget->hasVOP3PInsts()) { 562 setOperationAction(ISD::ADD, MVT::v2i16, Legal); 563 setOperationAction(ISD::SUB, MVT::v2i16, Legal); 564 setOperationAction(ISD::MUL, MVT::v2i16, Legal); 565 setOperationAction(ISD::SHL, MVT::v2i16, Legal); 566 setOperationAction(ISD::SRL, MVT::v2i16, Legal); 567 setOperationAction(ISD::SRA, MVT::v2i16, Legal); 568 setOperationAction(ISD::SMIN, MVT::v2i16, Legal); 569 setOperationAction(ISD::UMIN, MVT::v2i16, Legal); 570 setOperationAction(ISD::SMAX, MVT::v2i16, Legal); 571 setOperationAction(ISD::UMAX, MVT::v2i16, Legal); 572 573 setOperationAction(ISD::FADD, MVT::v2f16, Legal); 574 setOperationAction(ISD::FMUL, MVT::v2f16, Legal); 575 setOperationAction(ISD::FMA, MVT::v2f16, Legal); 576 setOperationAction(ISD::FMINNUM, MVT::v2f16, Legal); 577 setOperationAction(ISD::FMAXNUM, MVT::v2f16, Legal); 578 setOperationAction(ISD::FCANONICALIZE, MVT::v2f16, Legal); 579 580 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i16, Custom); 581 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f16, Custom); 582 583 setOperationAction(ISD::SHL, MVT::v4i16, Custom); 584 setOperationAction(ISD::SRA, MVT::v4i16, Custom); 585 setOperationAction(ISD::SRL, MVT::v4i16, Custom); 586 setOperationAction(ISD::ADD, MVT::v4i16, Custom); 587 setOperationAction(ISD::SUB, MVT::v4i16, Custom); 588 setOperationAction(ISD::MUL, MVT::v4i16, Custom); 589 590 setOperationAction(ISD::SMIN, MVT::v4i16, Custom); 591 setOperationAction(ISD::SMAX, MVT::v4i16, Custom); 592 setOperationAction(ISD::UMIN, MVT::v4i16, Custom); 593 setOperationAction(ISD::UMAX, MVT::v4i16, Custom); 594 595 setOperationAction(ISD::FADD, MVT::v4f16, Custom); 596 setOperationAction(ISD::FMUL, MVT::v4f16, Custom); 597 setOperationAction(ISD::FMINNUM, MVT::v4f16, Custom); 598 setOperationAction(ISD::FMAXNUM, MVT::v4f16, Custom); 599 setOperationAction(ISD::FCANONICALIZE, MVT::v4f16, Custom); 600 601 setOperationAction(ISD::FEXP, MVT::v2f16, Custom); 602 setOperationAction(ISD::SELECT, MVT::v4i16, Custom); 603 setOperationAction(ISD::SELECT, MVT::v4f16, Custom); 604 } 605 606 setOperationAction(ISD::FNEG, MVT::v4f16, Custom); 607 setOperationAction(ISD::FABS, MVT::v4f16, Custom); 608 609 if (Subtarget->has16BitInsts()) { 610 setOperationAction(ISD::SELECT, MVT::v2i16, Promote); 611 AddPromotedToType(ISD::SELECT, MVT::v2i16, MVT::i32); 612 setOperationAction(ISD::SELECT, MVT::v2f16, Promote); 613 AddPromotedToType(ISD::SELECT, MVT::v2f16, MVT::i32); 614 } else { 615 // Legalization hack. 616 setOperationAction(ISD::SELECT, MVT::v2i16, Custom); 617 setOperationAction(ISD::SELECT, MVT::v2f16, Custom); 618 619 setOperationAction(ISD::FNEG, MVT::v2f16, Custom); 620 setOperationAction(ISD::FABS, MVT::v2f16, Custom); 621 } 622 623 for (MVT VT : { MVT::v4i16, MVT::v4f16, MVT::v2i8, MVT::v4i8, MVT::v8i8 }) { 624 setOperationAction(ISD::SELECT, VT, Custom); 625 } 626 627 setTargetDAGCombine(ISD::ADD); 628 setTargetDAGCombine(ISD::ADDCARRY); 629 setTargetDAGCombine(ISD::SUB); 630 setTargetDAGCombine(ISD::SUBCARRY); 631 setTargetDAGCombine(ISD::FADD); 632 setTargetDAGCombine(ISD::FSUB); 633 setTargetDAGCombine(ISD::FMINNUM); 634 setTargetDAGCombine(ISD::FMAXNUM); 635 setTargetDAGCombine(ISD::FMA); 636 setTargetDAGCombine(ISD::SMIN); 637 setTargetDAGCombine(ISD::SMAX); 638 setTargetDAGCombine(ISD::UMIN); 639 setTargetDAGCombine(ISD::UMAX); 640 setTargetDAGCombine(ISD::SETCC); 641 setTargetDAGCombine(ISD::AND); 642 setTargetDAGCombine(ISD::OR); 643 setTargetDAGCombine(ISD::XOR); 644 setTargetDAGCombine(ISD::SINT_TO_FP); 645 setTargetDAGCombine(ISD::UINT_TO_FP); 646 setTargetDAGCombine(ISD::FCANONICALIZE); 647 setTargetDAGCombine(ISD::SCALAR_TO_VECTOR); 648 setTargetDAGCombine(ISD::ZERO_EXTEND); 649 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT); 650 setTargetDAGCombine(ISD::BUILD_VECTOR); 651 652 // All memory operations. Some folding on the pointer operand is done to help 653 // matching the constant offsets in the addressing modes. 654 setTargetDAGCombine(ISD::LOAD); 655 setTargetDAGCombine(ISD::STORE); 656 setTargetDAGCombine(ISD::ATOMIC_LOAD); 657 setTargetDAGCombine(ISD::ATOMIC_STORE); 658 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP); 659 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); 660 setTargetDAGCombine(ISD::ATOMIC_SWAP); 661 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD); 662 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB); 663 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND); 664 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR); 665 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR); 666 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND); 667 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN); 668 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX); 669 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN); 670 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX); 671 672 setSchedulingPreference(Sched::RegPressure); 673 674 // SI at least has hardware support for floating point exceptions, but no way 675 // of using or handling them is implemented. They are also optional in OpenCL 676 // (Section 7.3) 677 setHasFloatingPointExceptions(Subtarget->hasFPExceptions()); 678 } 679 680 const GCNSubtarget *SITargetLowering::getSubtarget() const { 681 return Subtarget; 682 } 683 684 //===----------------------------------------------------------------------===// 685 // TargetLowering queries 686 //===----------------------------------------------------------------------===// 687 688 // v_mad_mix* support a conversion from f16 to f32. 689 // 690 // There is only one special case when denormals are enabled we don't currently, 691 // where this is OK to use. 692 bool SITargetLowering::isFPExtFoldable(unsigned Opcode, 693 EVT DestVT, EVT SrcVT) const { 694 return ((Opcode == ISD::FMAD && Subtarget->hasMadMixInsts()) || 695 (Opcode == ISD::FMA && Subtarget->hasFmaMixInsts())) && 696 DestVT.getScalarType() == MVT::f32 && !Subtarget->hasFP32Denormals() && 697 SrcVT.getScalarType() == MVT::f16; 698 } 699 700 bool SITargetLowering::isShuffleMaskLegal(ArrayRef<int>, EVT) const { 701 // SI has some legal vector types, but no legal vector operations. Say no 702 // shuffles are legal in order to prefer scalarizing some vector operations. 703 return false; 704 } 705 706 MVT SITargetLowering::getRegisterTypeForCallingConv(LLVMContext &Context, 707 CallingConv::ID CC, 708 EVT VT) const { 709 // TODO: Consider splitting all arguments into 32-bit pieces. 710 if (CC != CallingConv::AMDGPU_KERNEL && VT.isVector()) { 711 EVT ScalarVT = VT.getScalarType(); 712 unsigned Size = ScalarVT.getSizeInBits(); 713 if (Size == 32) 714 return ScalarVT.getSimpleVT(); 715 716 if (Size == 64) 717 return MVT::i32; 718 719 if (Size == 16 && 720 Subtarget->has16BitInsts() && 721 isPowerOf2_32(VT.getVectorNumElements())) 722 return VT.isInteger() ? MVT::v2i16 : MVT::v2f16; 723 } 724 725 return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT); 726 } 727 728 unsigned SITargetLowering::getNumRegistersForCallingConv(LLVMContext &Context, 729 CallingConv::ID CC, 730 EVT VT) const { 731 if (CC != CallingConv::AMDGPU_KERNEL && VT.isVector()) { 732 unsigned NumElts = VT.getVectorNumElements(); 733 EVT ScalarVT = VT.getScalarType(); 734 unsigned Size = ScalarVT.getSizeInBits(); 735 736 if (Size == 32) 737 return NumElts; 738 739 if (Size == 64) 740 return 2 * NumElts; 741 742 // FIXME: Fails to break down as we want with v3. 743 if (Size == 16 && Subtarget->has16BitInsts() && isPowerOf2_32(NumElts)) 744 return VT.getVectorNumElements() / 2; 745 } 746 747 return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT); 748 } 749 750 unsigned SITargetLowering::getVectorTypeBreakdownForCallingConv( 751 LLVMContext &Context, CallingConv::ID CC, 752 EVT VT, EVT &IntermediateVT, 753 unsigned &NumIntermediates, MVT &RegisterVT) const { 754 if (CC != CallingConv::AMDGPU_KERNEL && VT.isVector()) { 755 unsigned NumElts = VT.getVectorNumElements(); 756 EVT ScalarVT = VT.getScalarType(); 757 unsigned Size = ScalarVT.getSizeInBits(); 758 if (Size == 32) { 759 RegisterVT = ScalarVT.getSimpleVT(); 760 IntermediateVT = RegisterVT; 761 NumIntermediates = NumElts; 762 return NumIntermediates; 763 } 764 765 if (Size == 64) { 766 RegisterVT = MVT::i32; 767 IntermediateVT = RegisterVT; 768 NumIntermediates = 2 * NumElts; 769 return NumIntermediates; 770 } 771 772 // FIXME: We should fix the ABI to be the same on targets without 16-bit 773 // support, but unless we can properly handle 3-vectors, it will be still be 774 // inconsistent. 775 if (Size == 16 && Subtarget->has16BitInsts() && isPowerOf2_32(NumElts)) { 776 RegisterVT = VT.isInteger() ? MVT::v2i16 : MVT::v2f16; 777 IntermediateVT = RegisterVT; 778 NumIntermediates = NumElts / 2; 779 return NumIntermediates; 780 } 781 } 782 783 return TargetLowering::getVectorTypeBreakdownForCallingConv( 784 Context, CC, VT, IntermediateVT, NumIntermediates, RegisterVT); 785 } 786 787 bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 788 const CallInst &CI, 789 MachineFunction &MF, 790 unsigned IntrID) const { 791 if (const AMDGPU::RsrcIntrinsic *RsrcIntr = 792 AMDGPU::lookupRsrcIntrinsic(IntrID)) { 793 AttributeList Attr = Intrinsic::getAttributes(CI.getContext(), 794 (Intrinsic::ID)IntrID); 795 if (Attr.hasFnAttribute(Attribute::ReadNone)) 796 return false; 797 798 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 799 800 if (RsrcIntr->IsImage) { 801 Info.ptrVal = MFI->getImagePSV( 802 *MF.getSubtarget<GCNSubtarget>().getInstrInfo(), 803 CI.getArgOperand(RsrcIntr->RsrcArg)); 804 Info.align = 0; 805 } else { 806 Info.ptrVal = MFI->getBufferPSV( 807 *MF.getSubtarget<GCNSubtarget>().getInstrInfo(), 808 CI.getArgOperand(RsrcIntr->RsrcArg)); 809 } 810 811 Info.flags = MachineMemOperand::MODereferenceable; 812 if (Attr.hasFnAttribute(Attribute::ReadOnly)) { 813 Info.opc = ISD::INTRINSIC_W_CHAIN; 814 Info.memVT = MVT::getVT(CI.getType()); 815 Info.flags |= MachineMemOperand::MOLoad; 816 } else if (Attr.hasFnAttribute(Attribute::WriteOnly)) { 817 Info.opc = ISD::INTRINSIC_VOID; 818 Info.memVT = MVT::getVT(CI.getArgOperand(0)->getType()); 819 Info.flags |= MachineMemOperand::MOStore; 820 } else { 821 // Atomic 822 Info.opc = ISD::INTRINSIC_W_CHAIN; 823 Info.memVT = MVT::getVT(CI.getType()); 824 Info.flags = MachineMemOperand::MOLoad | 825 MachineMemOperand::MOStore | 826 MachineMemOperand::MODereferenceable; 827 828 // XXX - Should this be volatile without known ordering? 829 Info.flags |= MachineMemOperand::MOVolatile; 830 } 831 return true; 832 } 833 834 switch (IntrID) { 835 case Intrinsic::amdgcn_atomic_inc: 836 case Intrinsic::amdgcn_atomic_dec: 837 case Intrinsic::amdgcn_ds_fadd: 838 case Intrinsic::amdgcn_ds_fmin: 839 case Intrinsic::amdgcn_ds_fmax: { 840 Info.opc = ISD::INTRINSIC_W_CHAIN; 841 Info.memVT = MVT::getVT(CI.getType()); 842 Info.ptrVal = CI.getOperand(0); 843 Info.align = 0; 844 Info.flags = MachineMemOperand::MOLoad | MachineMemOperand::MOStore; 845 846 const ConstantInt *Vol = dyn_cast<ConstantInt>(CI.getOperand(4)); 847 if (!Vol || !Vol->isZero()) 848 Info.flags |= MachineMemOperand::MOVolatile; 849 850 return true; 851 } 852 853 default: 854 return false; 855 } 856 } 857 858 bool SITargetLowering::getAddrModeArguments(IntrinsicInst *II, 859 SmallVectorImpl<Value*> &Ops, 860 Type *&AccessTy) const { 861 switch (II->getIntrinsicID()) { 862 case Intrinsic::amdgcn_atomic_inc: 863 case Intrinsic::amdgcn_atomic_dec: 864 case Intrinsic::amdgcn_ds_fadd: 865 case Intrinsic::amdgcn_ds_fmin: 866 case Intrinsic::amdgcn_ds_fmax: { 867 Value *Ptr = II->getArgOperand(0); 868 AccessTy = II->getType(); 869 Ops.push_back(Ptr); 870 return true; 871 } 872 default: 873 return false; 874 } 875 } 876 877 bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const { 878 if (!Subtarget->hasFlatInstOffsets()) { 879 // Flat instructions do not have offsets, and only have the register 880 // address. 881 return AM.BaseOffs == 0 && AM.Scale == 0; 882 } 883 884 // GFX9 added a 13-bit signed offset. When using regular flat instructions, 885 // the sign bit is ignored and is treated as a 12-bit unsigned offset. 886 887 // Just r + i 888 return isUInt<12>(AM.BaseOffs) && AM.Scale == 0; 889 } 890 891 bool SITargetLowering::isLegalGlobalAddressingMode(const AddrMode &AM) const { 892 if (Subtarget->hasFlatGlobalInsts()) 893 return isInt<13>(AM.BaseOffs) && AM.Scale == 0; 894 895 if (!Subtarget->hasAddr64() || Subtarget->useFlatForGlobal()) { 896 // Assume the we will use FLAT for all global memory accesses 897 // on VI. 898 // FIXME: This assumption is currently wrong. On VI we still use 899 // MUBUF instructions for the r + i addressing mode. As currently 900 // implemented, the MUBUF instructions only work on buffer < 4GB. 901 // It may be possible to support > 4GB buffers with MUBUF instructions, 902 // by setting the stride value in the resource descriptor which would 903 // increase the size limit to (stride * 4GB). However, this is risky, 904 // because it has never been validated. 905 return isLegalFlatAddressingMode(AM); 906 } 907 908 return isLegalMUBUFAddressingMode(AM); 909 } 910 911 bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const { 912 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and 913 // additionally can do r + r + i with addr64. 32-bit has more addressing 914 // mode options. Depending on the resource constant, it can also do 915 // (i64 r0) + (i32 r1) * (i14 i). 916 // 917 // Private arrays end up using a scratch buffer most of the time, so also 918 // assume those use MUBUF instructions. Scratch loads / stores are currently 919 // implemented as mubuf instructions with offen bit set, so slightly 920 // different than the normal addr64. 921 if (!isUInt<12>(AM.BaseOffs)) 922 return false; 923 924 // FIXME: Since we can split immediate into soffset and immediate offset, 925 // would it make sense to allow any immediate? 926 927 switch (AM.Scale) { 928 case 0: // r + i or just i, depending on HasBaseReg. 929 return true; 930 case 1: 931 return true; // We have r + r or r + i. 932 case 2: 933 if (AM.HasBaseReg) { 934 // Reject 2 * r + r. 935 return false; 936 } 937 938 // Allow 2 * r as r + r 939 // Or 2 * r + i is allowed as r + r + i. 940 return true; 941 default: // Don't allow n * r 942 return false; 943 } 944 } 945 946 bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL, 947 const AddrMode &AM, Type *Ty, 948 unsigned AS, Instruction *I) const { 949 // No global is ever allowed as a base. 950 if (AM.BaseGV) 951 return false; 952 953 if (AS == AMDGPUASI.GLOBAL_ADDRESS) 954 return isLegalGlobalAddressingMode(AM); 955 956 if (AS == AMDGPUASI.CONSTANT_ADDRESS || 957 AS == AMDGPUASI.CONSTANT_ADDRESS_32BIT) { 958 // If the offset isn't a multiple of 4, it probably isn't going to be 959 // correctly aligned. 960 // FIXME: Can we get the real alignment here? 961 if (AM.BaseOffs % 4 != 0) 962 return isLegalMUBUFAddressingMode(AM); 963 964 // There are no SMRD extloads, so if we have to do a small type access we 965 // will use a MUBUF load. 966 // FIXME?: We also need to do this if unaligned, but we don't know the 967 // alignment here. 968 if (Ty->isSized() && DL.getTypeStoreSize(Ty) < 4) 969 return isLegalGlobalAddressingMode(AM); 970 971 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) { 972 // SMRD instructions have an 8-bit, dword offset on SI. 973 if (!isUInt<8>(AM.BaseOffs / 4)) 974 return false; 975 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) { 976 // On CI+, this can also be a 32-bit literal constant offset. If it fits 977 // in 8-bits, it can use a smaller encoding. 978 if (!isUInt<32>(AM.BaseOffs / 4)) 979 return false; 980 } else if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 981 // On VI, these use the SMEM format and the offset is 20-bit in bytes. 982 if (!isUInt<20>(AM.BaseOffs)) 983 return false; 984 } else 985 llvm_unreachable("unhandled generation"); 986 987 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. 988 return true; 989 990 if (AM.Scale == 1 && AM.HasBaseReg) 991 return true; 992 993 return false; 994 995 } else if (AS == AMDGPUASI.PRIVATE_ADDRESS) { 996 return isLegalMUBUFAddressingMode(AM); 997 } else if (AS == AMDGPUASI.LOCAL_ADDRESS || 998 AS == AMDGPUASI.REGION_ADDRESS) { 999 // Basic, single offset DS instructions allow a 16-bit unsigned immediate 1000 // field. 1001 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have 1002 // an 8-bit dword offset but we don't know the alignment here. 1003 if (!isUInt<16>(AM.BaseOffs)) 1004 return false; 1005 1006 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. 1007 return true; 1008 1009 if (AM.Scale == 1 && AM.HasBaseReg) 1010 return true; 1011 1012 return false; 1013 } else if (AS == AMDGPUASI.FLAT_ADDRESS || 1014 AS == AMDGPUASI.UNKNOWN_ADDRESS_SPACE) { 1015 // For an unknown address space, this usually means that this is for some 1016 // reason being used for pure arithmetic, and not based on some addressing 1017 // computation. We don't have instructions that compute pointers with any 1018 // addressing modes, so treat them as having no offset like flat 1019 // instructions. 1020 return isLegalFlatAddressingMode(AM); 1021 } else { 1022 llvm_unreachable("unhandled address space"); 1023 } 1024 } 1025 1026 bool SITargetLowering::canMergeStoresTo(unsigned AS, EVT MemVT, 1027 const SelectionDAG &DAG) const { 1028 if (AS == AMDGPUASI.GLOBAL_ADDRESS || AS == AMDGPUASI.FLAT_ADDRESS) { 1029 return (MemVT.getSizeInBits() <= 4 * 32); 1030 } else if (AS == AMDGPUASI.PRIVATE_ADDRESS) { 1031 unsigned MaxPrivateBits = 8 * getSubtarget()->getMaxPrivateElementSize(); 1032 return (MemVT.getSizeInBits() <= MaxPrivateBits); 1033 } else if (AS == AMDGPUASI.LOCAL_ADDRESS) { 1034 return (MemVT.getSizeInBits() <= 2 * 32); 1035 } 1036 return true; 1037 } 1038 1039 bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 1040 unsigned AddrSpace, 1041 unsigned Align, 1042 bool *IsFast) const { 1043 if (IsFast) 1044 *IsFast = false; 1045 1046 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96, 1047 // which isn't a simple VT. 1048 // Until MVT is extended to handle this, simply check for the size and 1049 // rely on the condition below: allow accesses if the size is a multiple of 4. 1050 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 && 1051 VT.getStoreSize() > 16)) { 1052 return false; 1053 } 1054 1055 if (AddrSpace == AMDGPUASI.LOCAL_ADDRESS || 1056 AddrSpace == AMDGPUASI.REGION_ADDRESS) { 1057 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte 1058 // aligned, 8 byte access in a single operation using ds_read2/write2_b32 1059 // with adjacent offsets. 1060 bool AlignedBy4 = (Align % 4 == 0); 1061 if (IsFast) 1062 *IsFast = AlignedBy4; 1063 1064 return AlignedBy4; 1065 } 1066 1067 // FIXME: We have to be conservative here and assume that flat operations 1068 // will access scratch. If we had access to the IR function, then we 1069 // could determine if any private memory was used in the function. 1070 if (!Subtarget->hasUnalignedScratchAccess() && 1071 (AddrSpace == AMDGPUASI.PRIVATE_ADDRESS || 1072 AddrSpace == AMDGPUASI.FLAT_ADDRESS)) { 1073 return false; 1074 } 1075 1076 if (Subtarget->hasUnalignedBufferAccess()) { 1077 // If we have an uniform constant load, it still requires using a slow 1078 // buffer instruction if unaligned. 1079 if (IsFast) { 1080 *IsFast = (AddrSpace == AMDGPUASI.CONSTANT_ADDRESS || 1081 AddrSpace == AMDGPUASI.CONSTANT_ADDRESS_32BIT) ? 1082 (Align % 4 == 0) : true; 1083 } 1084 1085 return true; 1086 } 1087 1088 // Smaller than dword value must be aligned. 1089 if (VT.bitsLT(MVT::i32)) 1090 return false; 1091 1092 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the 1093 // byte-address are ignored, thus forcing Dword alignment. 1094 // This applies to private, global, and constant memory. 1095 if (IsFast) 1096 *IsFast = true; 1097 1098 return VT.bitsGT(MVT::i32) && Align % 4 == 0; 1099 } 1100 1101 EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign, 1102 unsigned SrcAlign, bool IsMemset, 1103 bool ZeroMemset, 1104 bool MemcpyStrSrc, 1105 MachineFunction &MF) const { 1106 // FIXME: Should account for address space here. 1107 1108 // The default fallback uses the private pointer size as a guess for a type to 1109 // use. Make sure we switch these to 64-bit accesses. 1110 1111 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global 1112 return MVT::v4i32; 1113 1114 if (Size >= 8 && DstAlign >= 4) 1115 return MVT::v2i32; 1116 1117 // Use the default. 1118 return MVT::Other; 1119 } 1120 1121 static bool isFlatGlobalAddrSpace(unsigned AS, AMDGPUAS AMDGPUASI) { 1122 return AS == AMDGPUASI.GLOBAL_ADDRESS || 1123 AS == AMDGPUASI.FLAT_ADDRESS || 1124 AS == AMDGPUASI.CONSTANT_ADDRESS || 1125 AS == AMDGPUASI.CONSTANT_ADDRESS_32BIT; 1126 } 1127 1128 bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS, 1129 unsigned DestAS) const { 1130 return isFlatGlobalAddrSpace(SrcAS, AMDGPUASI) && 1131 isFlatGlobalAddrSpace(DestAS, AMDGPUASI); 1132 } 1133 1134 bool SITargetLowering::isMemOpHasNoClobberedMemOperand(const SDNode *N) const { 1135 const MemSDNode *MemNode = cast<MemSDNode>(N); 1136 const Value *Ptr = MemNode->getMemOperand()->getValue(); 1137 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr); 1138 return I && I->getMetadata("amdgpu.noclobber"); 1139 } 1140 1141 bool SITargetLowering::isCheapAddrSpaceCast(unsigned SrcAS, 1142 unsigned DestAS) const { 1143 // Flat -> private/local is a simple truncate. 1144 // Flat -> global is no-op 1145 if (SrcAS == AMDGPUASI.FLAT_ADDRESS) 1146 return true; 1147 1148 return isNoopAddrSpaceCast(SrcAS, DestAS); 1149 } 1150 1151 bool SITargetLowering::isMemOpUniform(const SDNode *N) const { 1152 const MemSDNode *MemNode = cast<MemSDNode>(N); 1153 1154 return AMDGPUInstrInfo::isUniformMMO(MemNode->getMemOperand()); 1155 } 1156 1157 TargetLoweringBase::LegalizeTypeAction 1158 SITargetLowering::getPreferredVectorAction(EVT VT) const { 1159 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16)) 1160 return TypeSplitVector; 1161 1162 return TargetLoweringBase::getPreferredVectorAction(VT); 1163 } 1164 1165 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 1166 Type *Ty) const { 1167 // FIXME: Could be smarter if called for vector constants. 1168 return true; 1169 } 1170 1171 bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const { 1172 if (Subtarget->has16BitInsts() && VT == MVT::i16) { 1173 switch (Op) { 1174 case ISD::LOAD: 1175 case ISD::STORE: 1176 1177 // These operations are done with 32-bit instructions anyway. 1178 case ISD::AND: 1179 case ISD::OR: 1180 case ISD::XOR: 1181 case ISD::SELECT: 1182 // TODO: Extensions? 1183 return true; 1184 default: 1185 return false; 1186 } 1187 } 1188 1189 // SimplifySetCC uses this function to determine whether or not it should 1190 // create setcc with i1 operands. We don't have instructions for i1 setcc. 1191 if (VT == MVT::i1 && Op == ISD::SETCC) 1192 return false; 1193 1194 return TargetLowering::isTypeDesirableForOp(Op, VT); 1195 } 1196 1197 SDValue SITargetLowering::lowerKernArgParameterPtr(SelectionDAG &DAG, 1198 const SDLoc &SL, 1199 SDValue Chain, 1200 uint64_t Offset) const { 1201 const DataLayout &DL = DAG.getDataLayout(); 1202 MachineFunction &MF = DAG.getMachineFunction(); 1203 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1204 1205 const ArgDescriptor *InputPtrReg; 1206 const TargetRegisterClass *RC; 1207 1208 std::tie(InputPtrReg, RC) 1209 = Info->getPreloadedValue(AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR); 1210 1211 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 1212 MVT PtrVT = getPointerTy(DL, AMDGPUASI.CONSTANT_ADDRESS); 1213 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL, 1214 MRI.getLiveInVirtReg(InputPtrReg->getRegister()), PtrVT); 1215 1216 return DAG.getObjectPtrOffset(SL, BasePtr, Offset); 1217 } 1218 1219 SDValue SITargetLowering::getImplicitArgPtr(SelectionDAG &DAG, 1220 const SDLoc &SL) const { 1221 uint64_t Offset = getImplicitParameterOffset(DAG.getMachineFunction(), 1222 FIRST_IMPLICIT); 1223 return lowerKernArgParameterPtr(DAG, SL, DAG.getEntryNode(), Offset); 1224 } 1225 1226 SDValue SITargetLowering::convertArgType(SelectionDAG &DAG, EVT VT, EVT MemVT, 1227 const SDLoc &SL, SDValue Val, 1228 bool Signed, 1229 const ISD::InputArg *Arg) const { 1230 if (Arg && (Arg->Flags.isSExt() || Arg->Flags.isZExt()) && 1231 VT.bitsLT(MemVT)) { 1232 unsigned Opc = Arg->Flags.isZExt() ? ISD::AssertZext : ISD::AssertSext; 1233 Val = DAG.getNode(Opc, SL, MemVT, Val, DAG.getValueType(VT)); 1234 } 1235 1236 if (MemVT.isFloatingPoint()) 1237 Val = getFPExtOrFPTrunc(DAG, Val, SL, VT); 1238 else if (Signed) 1239 Val = DAG.getSExtOrTrunc(Val, SL, VT); 1240 else 1241 Val = DAG.getZExtOrTrunc(Val, SL, VT); 1242 1243 return Val; 1244 } 1245 1246 SDValue SITargetLowering::lowerKernargMemParameter( 1247 SelectionDAG &DAG, EVT VT, EVT MemVT, 1248 const SDLoc &SL, SDValue Chain, 1249 uint64_t Offset, unsigned Align, bool Signed, 1250 const ISD::InputArg *Arg) const { 1251 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext()); 1252 PointerType *PtrTy = PointerType::get(Ty, AMDGPUASI.CONSTANT_ADDRESS); 1253 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); 1254 1255 // Try to avoid using an extload by loading earlier than the argument address, 1256 // and extracting the relevant bits. The load should hopefully be merged with 1257 // the previous argument. 1258 if (MemVT.getStoreSize() < 4 && Align < 4) { 1259 // TODO: Handle align < 4 and size >= 4 (can happen with packed structs). 1260 int64_t AlignDownOffset = alignDown(Offset, 4); 1261 int64_t OffsetDiff = Offset - AlignDownOffset; 1262 1263 EVT IntVT = MemVT.changeTypeToInteger(); 1264 1265 // TODO: If we passed in the base kernel offset we could have a better 1266 // alignment than 4, but we don't really need it. 1267 SDValue Ptr = lowerKernArgParameterPtr(DAG, SL, Chain, AlignDownOffset); 1268 SDValue Load = DAG.getLoad(MVT::i32, SL, Chain, Ptr, PtrInfo, 4, 1269 MachineMemOperand::MODereferenceable | 1270 MachineMemOperand::MOInvariant); 1271 1272 SDValue ShiftAmt = DAG.getConstant(OffsetDiff * 8, SL, MVT::i32); 1273 SDValue Extract = DAG.getNode(ISD::SRL, SL, MVT::i32, Load, ShiftAmt); 1274 1275 SDValue ArgVal = DAG.getNode(ISD::TRUNCATE, SL, IntVT, Extract); 1276 ArgVal = DAG.getNode(ISD::BITCAST, SL, MemVT, ArgVal); 1277 ArgVal = convertArgType(DAG, VT, MemVT, SL, ArgVal, Signed, Arg); 1278 1279 1280 return DAG.getMergeValues({ ArgVal, Load.getValue(1) }, SL); 1281 } 1282 1283 SDValue Ptr = lowerKernArgParameterPtr(DAG, SL, Chain, Offset); 1284 SDValue Load = DAG.getLoad(MemVT, SL, Chain, Ptr, PtrInfo, Align, 1285 MachineMemOperand::MODereferenceable | 1286 MachineMemOperand::MOInvariant); 1287 1288 SDValue Val = convertArgType(DAG, VT, MemVT, SL, Load, Signed, Arg); 1289 return DAG.getMergeValues({ Val, Load.getValue(1) }, SL); 1290 } 1291 1292 SDValue SITargetLowering::lowerStackParameter(SelectionDAG &DAG, CCValAssign &VA, 1293 const SDLoc &SL, SDValue Chain, 1294 const ISD::InputArg &Arg) const { 1295 MachineFunction &MF = DAG.getMachineFunction(); 1296 MachineFrameInfo &MFI = MF.getFrameInfo(); 1297 1298 if (Arg.Flags.isByVal()) { 1299 unsigned Size = Arg.Flags.getByValSize(); 1300 int FrameIdx = MFI.CreateFixedObject(Size, VA.getLocMemOffset(), false); 1301 return DAG.getFrameIndex(FrameIdx, MVT::i32); 1302 } 1303 1304 unsigned ArgOffset = VA.getLocMemOffset(); 1305 unsigned ArgSize = VA.getValVT().getStoreSize(); 1306 1307 int FI = MFI.CreateFixedObject(ArgSize, ArgOffset, true); 1308 1309 // Create load nodes to retrieve arguments from the stack. 1310 SDValue FIN = DAG.getFrameIndex(FI, MVT::i32); 1311 SDValue ArgValue; 1312 1313 // For NON_EXTLOAD, generic code in getLoad assert(ValVT == MemVT) 1314 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD; 1315 MVT MemVT = VA.getValVT(); 1316 1317 switch (VA.getLocInfo()) { 1318 default: 1319 break; 1320 case CCValAssign::BCvt: 1321 MemVT = VA.getLocVT(); 1322 break; 1323 case CCValAssign::SExt: 1324 ExtType = ISD::SEXTLOAD; 1325 break; 1326 case CCValAssign::ZExt: 1327 ExtType = ISD::ZEXTLOAD; 1328 break; 1329 case CCValAssign::AExt: 1330 ExtType = ISD::EXTLOAD; 1331 break; 1332 } 1333 1334 ArgValue = DAG.getExtLoad( 1335 ExtType, SL, VA.getLocVT(), Chain, FIN, 1336 MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), 1337 MemVT); 1338 return ArgValue; 1339 } 1340 1341 SDValue SITargetLowering::getPreloadedValue(SelectionDAG &DAG, 1342 const SIMachineFunctionInfo &MFI, 1343 EVT VT, 1344 AMDGPUFunctionArgInfo::PreloadedValue PVID) const { 1345 const ArgDescriptor *Reg; 1346 const TargetRegisterClass *RC; 1347 1348 std::tie(Reg, RC) = MFI.getPreloadedValue(PVID); 1349 return CreateLiveInRegister(DAG, RC, Reg->getRegister(), VT); 1350 } 1351 1352 static void processShaderInputArgs(SmallVectorImpl<ISD::InputArg> &Splits, 1353 CallingConv::ID CallConv, 1354 ArrayRef<ISD::InputArg> Ins, 1355 BitVector &Skipped, 1356 FunctionType *FType, 1357 SIMachineFunctionInfo *Info) { 1358 for (unsigned I = 0, E = Ins.size(), PSInputNum = 0; I != E; ++I) { 1359 const ISD::InputArg *Arg = &Ins[I]; 1360 1361 assert((!Arg->VT.isVector() || Arg->VT.getScalarSizeInBits() == 16) && 1362 "vector type argument should have been split"); 1363 1364 // First check if it's a PS input addr. 1365 if (CallConv == CallingConv::AMDGPU_PS && 1366 !Arg->Flags.isInReg() && !Arg->Flags.isByVal() && PSInputNum <= 15) { 1367 1368 bool SkipArg = !Arg->Used && !Info->isPSInputAllocated(PSInputNum); 1369 1370 // Inconveniently only the first part of the split is marked as isSplit, 1371 // so skip to the end. We only want to increment PSInputNum once for the 1372 // entire split argument. 1373 if (Arg->Flags.isSplit()) { 1374 while (!Arg->Flags.isSplitEnd()) { 1375 assert(!Arg->VT.isVector() && 1376 "unexpected vector split in ps argument type"); 1377 if (!SkipArg) 1378 Splits.push_back(*Arg); 1379 Arg = &Ins[++I]; 1380 } 1381 } 1382 1383 if (SkipArg) { 1384 // We can safely skip PS inputs. 1385 Skipped.set(Arg->getOrigArgIndex()); 1386 ++PSInputNum; 1387 continue; 1388 } 1389 1390 Info->markPSInputAllocated(PSInputNum); 1391 if (Arg->Used) 1392 Info->markPSInputEnabled(PSInputNum); 1393 1394 ++PSInputNum; 1395 } 1396 1397 Splits.push_back(*Arg); 1398 } 1399 } 1400 1401 // Allocate special inputs passed in VGPRs. 1402 static void allocateSpecialEntryInputVGPRs(CCState &CCInfo, 1403 MachineFunction &MF, 1404 const SIRegisterInfo &TRI, 1405 SIMachineFunctionInfo &Info) { 1406 if (Info.hasWorkItemIDX()) { 1407 unsigned Reg = AMDGPU::VGPR0; 1408 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 1409 1410 CCInfo.AllocateReg(Reg); 1411 Info.setWorkItemIDX(ArgDescriptor::createRegister(Reg)); 1412 } 1413 1414 if (Info.hasWorkItemIDY()) { 1415 unsigned Reg = AMDGPU::VGPR1; 1416 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 1417 1418 CCInfo.AllocateReg(Reg); 1419 Info.setWorkItemIDY(ArgDescriptor::createRegister(Reg)); 1420 } 1421 1422 if (Info.hasWorkItemIDZ()) { 1423 unsigned Reg = AMDGPU::VGPR2; 1424 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 1425 1426 CCInfo.AllocateReg(Reg); 1427 Info.setWorkItemIDZ(ArgDescriptor::createRegister(Reg)); 1428 } 1429 } 1430 1431 // Try to allocate a VGPR at the end of the argument list, or if no argument 1432 // VGPRs are left allocating a stack slot. 1433 static ArgDescriptor allocateVGPR32Input(CCState &CCInfo) { 1434 ArrayRef<MCPhysReg> ArgVGPRs 1435 = makeArrayRef(AMDGPU::VGPR_32RegClass.begin(), 32); 1436 unsigned RegIdx = CCInfo.getFirstUnallocated(ArgVGPRs); 1437 if (RegIdx == ArgVGPRs.size()) { 1438 // Spill to stack required. 1439 int64_t Offset = CCInfo.AllocateStack(4, 4); 1440 1441 return ArgDescriptor::createStack(Offset); 1442 } 1443 1444 unsigned Reg = ArgVGPRs[RegIdx]; 1445 Reg = CCInfo.AllocateReg(Reg); 1446 assert(Reg != AMDGPU::NoRegister); 1447 1448 MachineFunction &MF = CCInfo.getMachineFunction(); 1449 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 1450 return ArgDescriptor::createRegister(Reg); 1451 } 1452 1453 static ArgDescriptor allocateSGPR32InputImpl(CCState &CCInfo, 1454 const TargetRegisterClass *RC, 1455 unsigned NumArgRegs) { 1456 ArrayRef<MCPhysReg> ArgSGPRs = makeArrayRef(RC->begin(), 32); 1457 unsigned RegIdx = CCInfo.getFirstUnallocated(ArgSGPRs); 1458 if (RegIdx == ArgSGPRs.size()) 1459 report_fatal_error("ran out of SGPRs for arguments"); 1460 1461 unsigned Reg = ArgSGPRs[RegIdx]; 1462 Reg = CCInfo.AllocateReg(Reg); 1463 assert(Reg != AMDGPU::NoRegister); 1464 1465 MachineFunction &MF = CCInfo.getMachineFunction(); 1466 MF.addLiveIn(Reg, RC); 1467 return ArgDescriptor::createRegister(Reg); 1468 } 1469 1470 static ArgDescriptor allocateSGPR32Input(CCState &CCInfo) { 1471 return allocateSGPR32InputImpl(CCInfo, &AMDGPU::SGPR_32RegClass, 32); 1472 } 1473 1474 static ArgDescriptor allocateSGPR64Input(CCState &CCInfo) { 1475 return allocateSGPR32InputImpl(CCInfo, &AMDGPU::SGPR_64RegClass, 16); 1476 } 1477 1478 static void allocateSpecialInputVGPRs(CCState &CCInfo, 1479 MachineFunction &MF, 1480 const SIRegisterInfo &TRI, 1481 SIMachineFunctionInfo &Info) { 1482 if (Info.hasWorkItemIDX()) 1483 Info.setWorkItemIDX(allocateVGPR32Input(CCInfo)); 1484 1485 if (Info.hasWorkItemIDY()) 1486 Info.setWorkItemIDY(allocateVGPR32Input(CCInfo)); 1487 1488 if (Info.hasWorkItemIDZ()) 1489 Info.setWorkItemIDZ(allocateVGPR32Input(CCInfo)); 1490 } 1491 1492 static void allocateSpecialInputSGPRs(CCState &CCInfo, 1493 MachineFunction &MF, 1494 const SIRegisterInfo &TRI, 1495 SIMachineFunctionInfo &Info) { 1496 auto &ArgInfo = Info.getArgInfo(); 1497 1498 // TODO: Unify handling with private memory pointers. 1499 1500 if (Info.hasDispatchPtr()) 1501 ArgInfo.DispatchPtr = allocateSGPR64Input(CCInfo); 1502 1503 if (Info.hasQueuePtr()) 1504 ArgInfo.QueuePtr = allocateSGPR64Input(CCInfo); 1505 1506 if (Info.hasKernargSegmentPtr()) 1507 ArgInfo.KernargSegmentPtr = allocateSGPR64Input(CCInfo); 1508 1509 if (Info.hasDispatchID()) 1510 ArgInfo.DispatchID = allocateSGPR64Input(CCInfo); 1511 1512 // flat_scratch_init is not applicable for non-kernel functions. 1513 1514 if (Info.hasWorkGroupIDX()) 1515 ArgInfo.WorkGroupIDX = allocateSGPR32Input(CCInfo); 1516 1517 if (Info.hasWorkGroupIDY()) 1518 ArgInfo.WorkGroupIDY = allocateSGPR32Input(CCInfo); 1519 1520 if (Info.hasWorkGroupIDZ()) 1521 ArgInfo.WorkGroupIDZ = allocateSGPR32Input(CCInfo); 1522 1523 if (Info.hasImplicitArgPtr()) 1524 ArgInfo.ImplicitArgPtr = allocateSGPR64Input(CCInfo); 1525 } 1526 1527 // Allocate special inputs passed in user SGPRs. 1528 static void allocateHSAUserSGPRs(CCState &CCInfo, 1529 MachineFunction &MF, 1530 const SIRegisterInfo &TRI, 1531 SIMachineFunctionInfo &Info) { 1532 if (Info.hasImplicitBufferPtr()) { 1533 unsigned ImplicitBufferPtrReg = Info.addImplicitBufferPtr(TRI); 1534 MF.addLiveIn(ImplicitBufferPtrReg, &AMDGPU::SGPR_64RegClass); 1535 CCInfo.AllocateReg(ImplicitBufferPtrReg); 1536 } 1537 1538 // FIXME: How should these inputs interact with inreg / custom SGPR inputs? 1539 if (Info.hasPrivateSegmentBuffer()) { 1540 unsigned PrivateSegmentBufferReg = Info.addPrivateSegmentBuffer(TRI); 1541 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SGPR_128RegClass); 1542 CCInfo.AllocateReg(PrivateSegmentBufferReg); 1543 } 1544 1545 if (Info.hasDispatchPtr()) { 1546 unsigned DispatchPtrReg = Info.addDispatchPtr(TRI); 1547 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass); 1548 CCInfo.AllocateReg(DispatchPtrReg); 1549 } 1550 1551 if (Info.hasQueuePtr()) { 1552 unsigned QueuePtrReg = Info.addQueuePtr(TRI); 1553 MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass); 1554 CCInfo.AllocateReg(QueuePtrReg); 1555 } 1556 1557 if (Info.hasKernargSegmentPtr()) { 1558 unsigned InputPtrReg = Info.addKernargSegmentPtr(TRI); 1559 MF.addLiveIn(InputPtrReg, &AMDGPU::SGPR_64RegClass); 1560 CCInfo.AllocateReg(InputPtrReg); 1561 } 1562 1563 if (Info.hasDispatchID()) { 1564 unsigned DispatchIDReg = Info.addDispatchID(TRI); 1565 MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass); 1566 CCInfo.AllocateReg(DispatchIDReg); 1567 } 1568 1569 if (Info.hasFlatScratchInit()) { 1570 unsigned FlatScratchInitReg = Info.addFlatScratchInit(TRI); 1571 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass); 1572 CCInfo.AllocateReg(FlatScratchInitReg); 1573 } 1574 1575 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read 1576 // these from the dispatch pointer. 1577 } 1578 1579 // Allocate special input registers that are initialized per-wave. 1580 static void allocateSystemSGPRs(CCState &CCInfo, 1581 MachineFunction &MF, 1582 SIMachineFunctionInfo &Info, 1583 CallingConv::ID CallConv, 1584 bool IsShader) { 1585 if (Info.hasWorkGroupIDX()) { 1586 unsigned Reg = Info.addWorkGroupIDX(); 1587 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass); 1588 CCInfo.AllocateReg(Reg); 1589 } 1590 1591 if (Info.hasWorkGroupIDY()) { 1592 unsigned Reg = Info.addWorkGroupIDY(); 1593 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass); 1594 CCInfo.AllocateReg(Reg); 1595 } 1596 1597 if (Info.hasWorkGroupIDZ()) { 1598 unsigned Reg = Info.addWorkGroupIDZ(); 1599 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass); 1600 CCInfo.AllocateReg(Reg); 1601 } 1602 1603 if (Info.hasWorkGroupInfo()) { 1604 unsigned Reg = Info.addWorkGroupInfo(); 1605 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass); 1606 CCInfo.AllocateReg(Reg); 1607 } 1608 1609 if (Info.hasPrivateSegmentWaveByteOffset()) { 1610 // Scratch wave offset passed in system SGPR. 1611 unsigned PrivateSegmentWaveByteOffsetReg; 1612 1613 if (IsShader) { 1614 PrivateSegmentWaveByteOffsetReg = 1615 Info.getPrivateSegmentWaveByteOffsetSystemSGPR(); 1616 1617 // This is true if the scratch wave byte offset doesn't have a fixed 1618 // location. 1619 if (PrivateSegmentWaveByteOffsetReg == AMDGPU::NoRegister) { 1620 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo); 1621 Info.setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg); 1622 } 1623 } else 1624 PrivateSegmentWaveByteOffsetReg = Info.addPrivateSegmentWaveByteOffset(); 1625 1626 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass); 1627 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg); 1628 } 1629 } 1630 1631 static void reservePrivateMemoryRegs(const TargetMachine &TM, 1632 MachineFunction &MF, 1633 const SIRegisterInfo &TRI, 1634 SIMachineFunctionInfo &Info) { 1635 // Now that we've figured out where the scratch register inputs are, see if 1636 // should reserve the arguments and use them directly. 1637 MachineFrameInfo &MFI = MF.getFrameInfo(); 1638 bool HasStackObjects = MFI.hasStackObjects(); 1639 1640 // Record that we know we have non-spill stack objects so we don't need to 1641 // check all stack objects later. 1642 if (HasStackObjects) 1643 Info.setHasNonSpillStackObjects(true); 1644 1645 // Everything live out of a block is spilled with fast regalloc, so it's 1646 // almost certain that spilling will be required. 1647 if (TM.getOptLevel() == CodeGenOpt::None) 1648 HasStackObjects = true; 1649 1650 // For now assume stack access is needed in any callee functions, so we need 1651 // the scratch registers to pass in. 1652 bool RequiresStackAccess = HasStackObjects || MFI.hasCalls(); 1653 1654 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 1655 if (ST.isAmdCodeObjectV2(MF.getFunction())) { 1656 if (RequiresStackAccess) { 1657 // If we have stack objects, we unquestionably need the private buffer 1658 // resource. For the Code Object V2 ABI, this will be the first 4 user 1659 // SGPR inputs. We can reserve those and use them directly. 1660 1661 unsigned PrivateSegmentBufferReg = Info.getPreloadedReg( 1662 AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_BUFFER); 1663 Info.setScratchRSrcReg(PrivateSegmentBufferReg); 1664 1665 if (MFI.hasCalls()) { 1666 // If we have calls, we need to keep the frame register in a register 1667 // that won't be clobbered by a call, so ensure it is copied somewhere. 1668 1669 // This is not a problem for the scratch wave offset, because the same 1670 // registers are reserved in all functions. 1671 1672 // FIXME: Nothing is really ensuring this is a call preserved register, 1673 // it's just selected from the end so it happens to be. 1674 unsigned ReservedOffsetReg 1675 = TRI.reservedPrivateSegmentWaveByteOffsetReg(MF); 1676 Info.setScratchWaveOffsetReg(ReservedOffsetReg); 1677 } else { 1678 unsigned PrivateSegmentWaveByteOffsetReg = Info.getPreloadedReg( 1679 AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 1680 Info.setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg); 1681 } 1682 } else { 1683 unsigned ReservedBufferReg 1684 = TRI.reservedPrivateSegmentBufferReg(MF); 1685 unsigned ReservedOffsetReg 1686 = TRI.reservedPrivateSegmentWaveByteOffsetReg(MF); 1687 1688 // We tentatively reserve the last registers (skipping the last two 1689 // which may contain VCC). After register allocation, we'll replace 1690 // these with the ones immediately after those which were really 1691 // allocated. In the prologue copies will be inserted from the argument 1692 // to these reserved registers. 1693 Info.setScratchRSrcReg(ReservedBufferReg); 1694 Info.setScratchWaveOffsetReg(ReservedOffsetReg); 1695 } 1696 } else { 1697 unsigned ReservedBufferReg = TRI.reservedPrivateSegmentBufferReg(MF); 1698 1699 // Without HSA, relocations are used for the scratch pointer and the 1700 // buffer resource setup is always inserted in the prologue. Scratch wave 1701 // offset is still in an input SGPR. 1702 Info.setScratchRSrcReg(ReservedBufferReg); 1703 1704 if (HasStackObjects && !MFI.hasCalls()) { 1705 unsigned ScratchWaveOffsetReg = Info.getPreloadedReg( 1706 AMDGPUFunctionArgInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 1707 Info.setScratchWaveOffsetReg(ScratchWaveOffsetReg); 1708 } else { 1709 unsigned ReservedOffsetReg 1710 = TRI.reservedPrivateSegmentWaveByteOffsetReg(MF); 1711 Info.setScratchWaveOffsetReg(ReservedOffsetReg); 1712 } 1713 } 1714 } 1715 1716 bool SITargetLowering::supportSplitCSR(MachineFunction *MF) const { 1717 const SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>(); 1718 return !Info->isEntryFunction(); 1719 } 1720 1721 void SITargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const { 1722 1723 } 1724 1725 void SITargetLowering::insertCopiesSplitCSR( 1726 MachineBasicBlock *Entry, 1727 const SmallVectorImpl<MachineBasicBlock *> &Exits) const { 1728 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 1729 1730 const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent()); 1731 if (!IStart) 1732 return; 1733 1734 const TargetInstrInfo *TII = Subtarget->getInstrInfo(); 1735 MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo(); 1736 MachineBasicBlock::iterator MBBI = Entry->begin(); 1737 for (const MCPhysReg *I = IStart; *I; ++I) { 1738 const TargetRegisterClass *RC = nullptr; 1739 if (AMDGPU::SReg_64RegClass.contains(*I)) 1740 RC = &AMDGPU::SGPR_64RegClass; 1741 else if (AMDGPU::SReg_32RegClass.contains(*I)) 1742 RC = &AMDGPU::SGPR_32RegClass; 1743 else 1744 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 1745 1746 unsigned NewVR = MRI->createVirtualRegister(RC); 1747 // Create copy from CSR to a virtual register. 1748 Entry->addLiveIn(*I); 1749 BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR) 1750 .addReg(*I); 1751 1752 // Insert the copy-back instructions right before the terminator. 1753 for (auto *Exit : Exits) 1754 BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(), 1755 TII->get(TargetOpcode::COPY), *I) 1756 .addReg(NewVR); 1757 } 1758 } 1759 1760 SDValue SITargetLowering::LowerFormalArguments( 1761 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 1762 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 1763 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 1764 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 1765 1766 MachineFunction &MF = DAG.getMachineFunction(); 1767 const Function &Fn = MF.getFunction(); 1768 FunctionType *FType = MF.getFunction().getFunctionType(); 1769 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1770 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 1771 1772 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) { 1773 DiagnosticInfoUnsupported NoGraphicsHSA( 1774 Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc()); 1775 DAG.getContext()->diagnose(NoGraphicsHSA); 1776 return DAG.getEntryNode(); 1777 } 1778 1779 // Create stack objects that are used for emitting debugger prologue if 1780 // "amdgpu-debugger-emit-prologue" attribute was specified. 1781 if (ST.debuggerEmitPrologue()) 1782 createDebuggerPrologueStackObjects(MF); 1783 1784 SmallVector<ISD::InputArg, 16> Splits; 1785 SmallVector<CCValAssign, 16> ArgLocs; 1786 BitVector Skipped(Ins.size()); 1787 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 1788 *DAG.getContext()); 1789 1790 bool IsShader = AMDGPU::isShader(CallConv); 1791 bool IsKernel = AMDGPU::isKernel(CallConv); 1792 bool IsEntryFunc = AMDGPU::isEntryFunctionCC(CallConv); 1793 1794 if (!IsEntryFunc) { 1795 // 4 bytes are reserved at offset 0 for the emergency stack slot. Skip over 1796 // this when allocating argument fixed offsets. 1797 CCInfo.AllocateStack(4, 4); 1798 } 1799 1800 if (IsShader) { 1801 processShaderInputArgs(Splits, CallConv, Ins, Skipped, FType, Info); 1802 1803 // At least one interpolation mode must be enabled or else the GPU will 1804 // hang. 1805 // 1806 // Check PSInputAddr instead of PSInputEnable. The idea is that if the user 1807 // set PSInputAddr, the user wants to enable some bits after the compilation 1808 // based on run-time states. Since we can't know what the final PSInputEna 1809 // will look like, so we shouldn't do anything here and the user should take 1810 // responsibility for the correct programming. 1811 // 1812 // Otherwise, the following restrictions apply: 1813 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled. 1814 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be 1815 // enabled too. 1816 if (CallConv == CallingConv::AMDGPU_PS) { 1817 if ((Info->getPSInputAddr() & 0x7F) == 0 || 1818 ((Info->getPSInputAddr() & 0xF) == 0 && 1819 Info->isPSInputAllocated(11))) { 1820 CCInfo.AllocateReg(AMDGPU::VGPR0); 1821 CCInfo.AllocateReg(AMDGPU::VGPR1); 1822 Info->markPSInputAllocated(0); 1823 Info->markPSInputEnabled(0); 1824 } 1825 if (Subtarget->isAmdPalOS()) { 1826 // For isAmdPalOS, the user does not enable some bits after compilation 1827 // based on run-time states; the register values being generated here are 1828 // the final ones set in hardware. Therefore we need to apply the 1829 // workaround to PSInputAddr and PSInputEnable together. (The case where 1830 // a bit is set in PSInputAddr but not PSInputEnable is where the 1831 // frontend set up an input arg for a particular interpolation mode, but 1832 // nothing uses that input arg. Really we should have an earlier pass 1833 // that removes such an arg.) 1834 unsigned PsInputBits = Info->getPSInputAddr() & Info->getPSInputEnable(); 1835 if ((PsInputBits & 0x7F) == 0 || 1836 ((PsInputBits & 0xF) == 0 && 1837 (PsInputBits >> 11 & 1))) 1838 Info->markPSInputEnabled( 1839 countTrailingZeros(Info->getPSInputAddr(), ZB_Undefined)); 1840 } 1841 } 1842 1843 assert(!Info->hasDispatchPtr() && 1844 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() && 1845 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() && 1846 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() && 1847 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() && 1848 !Info->hasWorkItemIDZ()); 1849 } else if (IsKernel) { 1850 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX()); 1851 } else { 1852 Splits.append(Ins.begin(), Ins.end()); 1853 } 1854 1855 if (IsEntryFunc) { 1856 allocateSpecialEntryInputVGPRs(CCInfo, MF, *TRI, *Info); 1857 allocateHSAUserSGPRs(CCInfo, MF, *TRI, *Info); 1858 } 1859 1860 if (IsKernel) { 1861 analyzeFormalArgumentsCompute(CCInfo, Ins); 1862 } else { 1863 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, isVarArg); 1864 CCInfo.AnalyzeFormalArguments(Splits, AssignFn); 1865 } 1866 1867 SmallVector<SDValue, 16> Chains; 1868 1869 // FIXME: This is the minimum kernel argument alignment. We should improve 1870 // this to the maximum alignment of the arguments. 1871 // 1872 // FIXME: Alignment of explicit arguments totally broken with non-0 explicit 1873 // kern arg offset. 1874 const unsigned KernelArgBaseAlign = 16; 1875 1876 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { 1877 const ISD::InputArg &Arg = Ins[i]; 1878 if (Arg.isOrigArg() && Skipped[Arg.getOrigArgIndex()]) { 1879 InVals.push_back(DAG.getUNDEF(Arg.VT)); 1880 continue; 1881 } 1882 1883 CCValAssign &VA = ArgLocs[ArgIdx++]; 1884 MVT VT = VA.getLocVT(); 1885 1886 if (IsEntryFunc && VA.isMemLoc()) { 1887 VT = Ins[i].VT; 1888 EVT MemVT = VA.getLocVT(); 1889 1890 const uint64_t Offset = VA.getLocMemOffset(); 1891 unsigned Align = MinAlign(KernelArgBaseAlign, Offset); 1892 1893 SDValue Arg = lowerKernargMemParameter( 1894 DAG, VT, MemVT, DL, Chain, Offset, Align, Ins[i].Flags.isSExt(), &Ins[i]); 1895 Chains.push_back(Arg.getValue(1)); 1896 1897 auto *ParamTy = 1898 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex())); 1899 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS && 1900 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) { 1901 // On SI local pointers are just offsets into LDS, so they are always 1902 // less than 16-bits. On CI and newer they could potentially be 1903 // real pointers, so we can't guarantee their size. 1904 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg, 1905 DAG.getValueType(MVT::i16)); 1906 } 1907 1908 InVals.push_back(Arg); 1909 continue; 1910 } else if (!IsEntryFunc && VA.isMemLoc()) { 1911 SDValue Val = lowerStackParameter(DAG, VA, DL, Chain, Arg); 1912 InVals.push_back(Val); 1913 if (!Arg.Flags.isByVal()) 1914 Chains.push_back(Val.getValue(1)); 1915 continue; 1916 } 1917 1918 assert(VA.isRegLoc() && "Parameter must be in a register!"); 1919 1920 unsigned Reg = VA.getLocReg(); 1921 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); 1922 EVT ValVT = VA.getValVT(); 1923 1924 Reg = MF.addLiveIn(Reg, RC); 1925 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); 1926 1927 if (Arg.Flags.isSRet() && !getSubtarget()->enableHugePrivateBuffer()) { 1928 // The return object should be reasonably addressable. 1929 1930 // FIXME: This helps when the return is a real sret. If it is a 1931 // automatically inserted sret (i.e. CanLowerReturn returns false), an 1932 // extra copy is inserted in SelectionDAGBuilder which obscures this. 1933 unsigned NumBits = 32 - AssumeFrameIndexHighZeroBits; 1934 Val = DAG.getNode(ISD::AssertZext, DL, VT, Val, 1935 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), NumBits))); 1936 } 1937 1938 // If this is an 8 or 16-bit value, it is really passed promoted 1939 // to 32 bits. Insert an assert[sz]ext to capture this, then 1940 // truncate to the right size. 1941 switch (VA.getLocInfo()) { 1942 case CCValAssign::Full: 1943 break; 1944 case CCValAssign::BCvt: 1945 Val = DAG.getNode(ISD::BITCAST, DL, ValVT, Val); 1946 break; 1947 case CCValAssign::SExt: 1948 Val = DAG.getNode(ISD::AssertSext, DL, VT, Val, 1949 DAG.getValueType(ValVT)); 1950 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 1951 break; 1952 case CCValAssign::ZExt: 1953 Val = DAG.getNode(ISD::AssertZext, DL, VT, Val, 1954 DAG.getValueType(ValVT)); 1955 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 1956 break; 1957 case CCValAssign::AExt: 1958 Val = DAG.getNode(ISD::TRUNCATE, DL, ValVT, Val); 1959 break; 1960 default: 1961 llvm_unreachable("Unknown loc info!"); 1962 } 1963 1964 InVals.push_back(Val); 1965 } 1966 1967 if (!IsEntryFunc) { 1968 // Special inputs come after user arguments. 1969 allocateSpecialInputVGPRs(CCInfo, MF, *TRI, *Info); 1970 } 1971 1972 // Start adding system SGPRs. 1973 if (IsEntryFunc) { 1974 allocateSystemSGPRs(CCInfo, MF, *Info, CallConv, IsShader); 1975 } else { 1976 CCInfo.AllocateReg(Info->getScratchRSrcReg()); 1977 CCInfo.AllocateReg(Info->getScratchWaveOffsetReg()); 1978 CCInfo.AllocateReg(Info->getFrameOffsetReg()); 1979 allocateSpecialInputSGPRs(CCInfo, MF, *TRI, *Info); 1980 } 1981 1982 auto &ArgUsageInfo = 1983 DAG.getPass()->getAnalysis<AMDGPUArgumentUsageInfo>(); 1984 ArgUsageInfo.setFuncArgInfo(Fn, Info->getArgInfo()); 1985 1986 unsigned StackArgSize = CCInfo.getNextStackOffset(); 1987 Info->setBytesInStackArgArea(StackArgSize); 1988 1989 return Chains.empty() ? Chain : 1990 DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 1991 } 1992 1993 // TODO: If return values can't fit in registers, we should return as many as 1994 // possible in registers before passing on stack. 1995 bool SITargetLowering::CanLowerReturn( 1996 CallingConv::ID CallConv, 1997 MachineFunction &MF, bool IsVarArg, 1998 const SmallVectorImpl<ISD::OutputArg> &Outs, 1999 LLVMContext &Context) const { 2000 // Replacing returns with sret/stack usage doesn't make sense for shaders. 2001 // FIXME: Also sort of a workaround for custom vector splitting in LowerReturn 2002 // for shaders. Vector types should be explicitly handled by CC. 2003 if (AMDGPU::isEntryFunctionCC(CallConv)) 2004 return true; 2005 2006 SmallVector<CCValAssign, 16> RVLocs; 2007 CCState CCInfo(CallConv, IsVarArg, MF, RVLocs, Context); 2008 return CCInfo.CheckReturn(Outs, CCAssignFnForReturn(CallConv, IsVarArg)); 2009 } 2010 2011 SDValue 2012 SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 2013 bool isVarArg, 2014 const SmallVectorImpl<ISD::OutputArg> &Outs, 2015 const SmallVectorImpl<SDValue> &OutVals, 2016 const SDLoc &DL, SelectionDAG &DAG) const { 2017 MachineFunction &MF = DAG.getMachineFunction(); 2018 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 2019 2020 if (AMDGPU::isKernel(CallConv)) { 2021 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs, 2022 OutVals, DL, DAG); 2023 } 2024 2025 bool IsShader = AMDGPU::isShader(CallConv); 2026 2027 Info->setIfReturnsVoid(Outs.empty()); 2028 bool IsWaveEnd = Info->returnsVoid() && IsShader; 2029 2030 // CCValAssign - represent the assignment of the return value to a location. 2031 SmallVector<CCValAssign, 48> RVLocs; 2032 SmallVector<ISD::OutputArg, 48> Splits; 2033 2034 // CCState - Info about the registers and stack slots. 2035 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 2036 *DAG.getContext()); 2037 2038 // Analyze outgoing return values. 2039 CCInfo.AnalyzeReturn(Outs, CCAssignFnForReturn(CallConv, isVarArg)); 2040 2041 SDValue Flag; 2042 SmallVector<SDValue, 48> RetOps; 2043 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 2044 2045 // Add return address for callable functions. 2046 if (!Info->isEntryFunction()) { 2047 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 2048 SDValue ReturnAddrReg = CreateLiveInRegister( 2049 DAG, &AMDGPU::SReg_64RegClass, TRI->getReturnAddressReg(MF), MVT::i64); 2050 2051 // FIXME: Should be able to use a vreg here, but need a way to prevent it 2052 // from being allcoated to a CSR. 2053 2054 SDValue PhysReturnAddrReg = DAG.getRegister(TRI->getReturnAddressReg(MF), 2055 MVT::i64); 2056 2057 Chain = DAG.getCopyToReg(Chain, DL, PhysReturnAddrReg, ReturnAddrReg, Flag); 2058 Flag = Chain.getValue(1); 2059 2060 RetOps.push_back(PhysReturnAddrReg); 2061 } 2062 2063 // Copy the result values into the output registers. 2064 for (unsigned I = 0, RealRVLocIdx = 0, E = RVLocs.size(); I != E; 2065 ++I, ++RealRVLocIdx) { 2066 CCValAssign &VA = RVLocs[I]; 2067 assert(VA.isRegLoc() && "Can only return in registers!"); 2068 // TODO: Partially return in registers if return values don't fit. 2069 SDValue Arg = OutVals[RealRVLocIdx]; 2070 2071 // Copied from other backends. 2072 switch (VA.getLocInfo()) { 2073 case CCValAssign::Full: 2074 break; 2075 case CCValAssign::BCvt: 2076 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 2077 break; 2078 case CCValAssign::SExt: 2079 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg); 2080 break; 2081 case CCValAssign::ZExt: 2082 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg); 2083 break; 2084 case CCValAssign::AExt: 2085 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg); 2086 break; 2087 default: 2088 llvm_unreachable("Unknown loc info!"); 2089 } 2090 2091 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag); 2092 Flag = Chain.getValue(1); 2093 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 2094 } 2095 2096 // FIXME: Does sret work properly? 2097 if (!Info->isEntryFunction()) { 2098 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2099 const MCPhysReg *I = 2100 TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction()); 2101 if (I) { 2102 for (; *I; ++I) { 2103 if (AMDGPU::SReg_64RegClass.contains(*I)) 2104 RetOps.push_back(DAG.getRegister(*I, MVT::i64)); 2105 else if (AMDGPU::SReg_32RegClass.contains(*I)) 2106 RetOps.push_back(DAG.getRegister(*I, MVT::i32)); 2107 else 2108 llvm_unreachable("Unexpected register class in CSRsViaCopy!"); 2109 } 2110 } 2111 } 2112 2113 // Update chain and glue. 2114 RetOps[0] = Chain; 2115 if (Flag.getNode()) 2116 RetOps.push_back(Flag); 2117 2118 unsigned Opc = AMDGPUISD::ENDPGM; 2119 if (!IsWaveEnd) 2120 Opc = IsShader ? AMDGPUISD::RETURN_TO_EPILOG : AMDGPUISD::RET_FLAG; 2121 return DAG.getNode(Opc, DL, MVT::Other, RetOps); 2122 } 2123 2124 SDValue SITargetLowering::LowerCallResult( 2125 SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool IsVarArg, 2126 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 2127 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, bool IsThisReturn, 2128 SDValue ThisVal) const { 2129 CCAssignFn *RetCC = CCAssignFnForReturn(CallConv, IsVarArg); 2130 2131 // Assign locations to each value returned by this call. 2132 SmallVector<CCValAssign, 16> RVLocs; 2133 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), RVLocs, 2134 *DAG.getContext()); 2135 CCInfo.AnalyzeCallResult(Ins, RetCC); 2136 2137 // Copy all of the result registers out of their specified physreg. 2138 for (unsigned i = 0; i != RVLocs.size(); ++i) { 2139 CCValAssign VA = RVLocs[i]; 2140 SDValue Val; 2141 2142 if (VA.isRegLoc()) { 2143 Val = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), VA.getLocVT(), InFlag); 2144 Chain = Val.getValue(1); 2145 InFlag = Val.getValue(2); 2146 } else if (VA.isMemLoc()) { 2147 report_fatal_error("TODO: return values in memory"); 2148 } else 2149 llvm_unreachable("unknown argument location type"); 2150 2151 switch (VA.getLocInfo()) { 2152 case CCValAssign::Full: 2153 break; 2154 case CCValAssign::BCvt: 2155 Val = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Val); 2156 break; 2157 case CCValAssign::ZExt: 2158 Val = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Val, 2159 DAG.getValueType(VA.getValVT())); 2160 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 2161 break; 2162 case CCValAssign::SExt: 2163 Val = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Val, 2164 DAG.getValueType(VA.getValVT())); 2165 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 2166 break; 2167 case CCValAssign::AExt: 2168 Val = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Val); 2169 break; 2170 default: 2171 llvm_unreachable("Unknown loc info!"); 2172 } 2173 2174 InVals.push_back(Val); 2175 } 2176 2177 return Chain; 2178 } 2179 2180 // Add code to pass special inputs required depending on used features separate 2181 // from the explicit user arguments present in the IR. 2182 void SITargetLowering::passSpecialInputs( 2183 CallLoweringInfo &CLI, 2184 CCState &CCInfo, 2185 const SIMachineFunctionInfo &Info, 2186 SmallVectorImpl<std::pair<unsigned, SDValue>> &RegsToPass, 2187 SmallVectorImpl<SDValue> &MemOpChains, 2188 SDValue Chain) const { 2189 // If we don't have a call site, this was a call inserted by 2190 // legalization. These can never use special inputs. 2191 if (!CLI.CS) 2192 return; 2193 2194 const Function *CalleeFunc = CLI.CS.getCalledFunction(); 2195 assert(CalleeFunc); 2196 2197 SelectionDAG &DAG = CLI.DAG; 2198 const SDLoc &DL = CLI.DL; 2199 2200 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo(); 2201 2202 auto &ArgUsageInfo = 2203 DAG.getPass()->getAnalysis<AMDGPUArgumentUsageInfo>(); 2204 const AMDGPUFunctionArgInfo &CalleeArgInfo 2205 = ArgUsageInfo.lookupFuncArgInfo(*CalleeFunc); 2206 2207 const AMDGPUFunctionArgInfo &CallerArgInfo = Info.getArgInfo(); 2208 2209 // TODO: Unify with private memory register handling. This is complicated by 2210 // the fact that at least in kernels, the input argument is not necessarily 2211 // in the same location as the input. 2212 AMDGPUFunctionArgInfo::PreloadedValue InputRegs[] = { 2213 AMDGPUFunctionArgInfo::DISPATCH_PTR, 2214 AMDGPUFunctionArgInfo::QUEUE_PTR, 2215 AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR, 2216 AMDGPUFunctionArgInfo::DISPATCH_ID, 2217 AMDGPUFunctionArgInfo::WORKGROUP_ID_X, 2218 AMDGPUFunctionArgInfo::WORKGROUP_ID_Y, 2219 AMDGPUFunctionArgInfo::WORKGROUP_ID_Z, 2220 AMDGPUFunctionArgInfo::WORKITEM_ID_X, 2221 AMDGPUFunctionArgInfo::WORKITEM_ID_Y, 2222 AMDGPUFunctionArgInfo::WORKITEM_ID_Z, 2223 AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR 2224 }; 2225 2226 for (auto InputID : InputRegs) { 2227 const ArgDescriptor *OutgoingArg; 2228 const TargetRegisterClass *ArgRC; 2229 2230 std::tie(OutgoingArg, ArgRC) = CalleeArgInfo.getPreloadedValue(InputID); 2231 if (!OutgoingArg) 2232 continue; 2233 2234 const ArgDescriptor *IncomingArg; 2235 const TargetRegisterClass *IncomingArgRC; 2236 std::tie(IncomingArg, IncomingArgRC) 2237 = CallerArgInfo.getPreloadedValue(InputID); 2238 assert(IncomingArgRC == ArgRC); 2239 2240 // All special arguments are ints for now. 2241 EVT ArgVT = TRI->getSpillSize(*ArgRC) == 8 ? MVT::i64 : MVT::i32; 2242 SDValue InputReg; 2243 2244 if (IncomingArg) { 2245 InputReg = loadInputValue(DAG, ArgRC, ArgVT, DL, *IncomingArg); 2246 } else { 2247 // The implicit arg ptr is special because it doesn't have a corresponding 2248 // input for kernels, and is computed from the kernarg segment pointer. 2249 assert(InputID == AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR); 2250 InputReg = getImplicitArgPtr(DAG, DL); 2251 } 2252 2253 if (OutgoingArg->isRegister()) { 2254 RegsToPass.emplace_back(OutgoingArg->getRegister(), InputReg); 2255 } else { 2256 unsigned SpecialArgOffset = CCInfo.AllocateStack(ArgVT.getStoreSize(), 4); 2257 SDValue ArgStore = storeStackInputValue(DAG, DL, Chain, InputReg, 2258 SpecialArgOffset); 2259 MemOpChains.push_back(ArgStore); 2260 } 2261 } 2262 } 2263 2264 static bool canGuaranteeTCO(CallingConv::ID CC) { 2265 return CC == CallingConv::Fast; 2266 } 2267 2268 /// Return true if we might ever do TCO for calls with this calling convention. 2269 static bool mayTailCallThisCC(CallingConv::ID CC) { 2270 switch (CC) { 2271 case CallingConv::C: 2272 return true; 2273 default: 2274 return canGuaranteeTCO(CC); 2275 } 2276 } 2277 2278 bool SITargetLowering::isEligibleForTailCallOptimization( 2279 SDValue Callee, CallingConv::ID CalleeCC, bool IsVarArg, 2280 const SmallVectorImpl<ISD::OutputArg> &Outs, 2281 const SmallVectorImpl<SDValue> &OutVals, 2282 const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const { 2283 if (!mayTailCallThisCC(CalleeCC)) 2284 return false; 2285 2286 MachineFunction &MF = DAG.getMachineFunction(); 2287 const Function &CallerF = MF.getFunction(); 2288 CallingConv::ID CallerCC = CallerF.getCallingConv(); 2289 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 2290 const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); 2291 2292 // Kernels aren't callable, and don't have a live in return address so it 2293 // doesn't make sense to do a tail call with entry functions. 2294 if (!CallerPreserved) 2295 return false; 2296 2297 bool CCMatch = CallerCC == CalleeCC; 2298 2299 if (DAG.getTarget().Options.GuaranteedTailCallOpt) { 2300 if (canGuaranteeTCO(CalleeCC) && CCMatch) 2301 return true; 2302 return false; 2303 } 2304 2305 // TODO: Can we handle var args? 2306 if (IsVarArg) 2307 return false; 2308 2309 for (const Argument &Arg : CallerF.args()) { 2310 if (Arg.hasByValAttr()) 2311 return false; 2312 } 2313 2314 LLVMContext &Ctx = *DAG.getContext(); 2315 2316 // Check that the call results are passed in the same way. 2317 if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, Ctx, Ins, 2318 CCAssignFnForCall(CalleeCC, IsVarArg), 2319 CCAssignFnForCall(CallerCC, IsVarArg))) 2320 return false; 2321 2322 // The callee has to preserve all registers the caller needs to preserve. 2323 if (!CCMatch) { 2324 const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC); 2325 if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved)) 2326 return false; 2327 } 2328 2329 // Nothing more to check if the callee is taking no arguments. 2330 if (Outs.empty()) 2331 return true; 2332 2333 SmallVector<CCValAssign, 16> ArgLocs; 2334 CCState CCInfo(CalleeCC, IsVarArg, MF, ArgLocs, Ctx); 2335 2336 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForCall(CalleeCC, IsVarArg)); 2337 2338 const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>(); 2339 // If the stack arguments for this call do not fit into our own save area then 2340 // the call cannot be made tail. 2341 // TODO: Is this really necessary? 2342 if (CCInfo.getNextStackOffset() > FuncInfo->getBytesInStackArgArea()) 2343 return false; 2344 2345 const MachineRegisterInfo &MRI = MF.getRegInfo(); 2346 return parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals); 2347 } 2348 2349 bool SITargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const { 2350 if (!CI->isTailCall()) 2351 return false; 2352 2353 const Function *ParentFn = CI->getParent()->getParent(); 2354 if (AMDGPU::isEntryFunctionCC(ParentFn->getCallingConv())) 2355 return false; 2356 2357 auto Attr = ParentFn->getFnAttribute("disable-tail-calls"); 2358 return (Attr.getValueAsString() != "true"); 2359 } 2360 2361 // The wave scratch offset register is used as the global base pointer. 2362 SDValue SITargetLowering::LowerCall(CallLoweringInfo &CLI, 2363 SmallVectorImpl<SDValue> &InVals) const { 2364 SelectionDAG &DAG = CLI.DAG; 2365 const SDLoc &DL = CLI.DL; 2366 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs; 2367 SmallVector<SDValue, 32> &OutVals = CLI.OutVals; 2368 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins; 2369 SDValue Chain = CLI.Chain; 2370 SDValue Callee = CLI.Callee; 2371 bool &IsTailCall = CLI.IsTailCall; 2372 CallingConv::ID CallConv = CLI.CallConv; 2373 bool IsVarArg = CLI.IsVarArg; 2374 bool IsSibCall = false; 2375 bool IsThisReturn = false; 2376 MachineFunction &MF = DAG.getMachineFunction(); 2377 2378 if (IsVarArg) { 2379 return lowerUnhandledCall(CLI, InVals, 2380 "unsupported call to variadic function "); 2381 } 2382 2383 if (!CLI.CS.getInstruction()) 2384 report_fatal_error("unsupported libcall legalization"); 2385 2386 if (!CLI.CS.getCalledFunction()) { 2387 return lowerUnhandledCall(CLI, InVals, 2388 "unsupported indirect call to function "); 2389 } 2390 2391 if (IsTailCall && MF.getTarget().Options.GuaranteedTailCallOpt) { 2392 return lowerUnhandledCall(CLI, InVals, 2393 "unsupported required tail call to function "); 2394 } 2395 2396 if (AMDGPU::isShader(MF.getFunction().getCallingConv())) { 2397 // Note the issue is with the CC of the calling function, not of the call 2398 // itself. 2399 return lowerUnhandledCall(CLI, InVals, 2400 "unsupported call from graphics shader of function "); 2401 } 2402 2403 // The first 4 bytes are reserved for the callee's emergency stack slot. 2404 if (IsTailCall) { 2405 IsTailCall = isEligibleForTailCallOptimization( 2406 Callee, CallConv, IsVarArg, Outs, OutVals, Ins, DAG); 2407 if (!IsTailCall && CLI.CS && CLI.CS.isMustTailCall()) { 2408 report_fatal_error("failed to perform tail call elimination on a call " 2409 "site marked musttail"); 2410 } 2411 2412 bool TailCallOpt = MF.getTarget().Options.GuaranteedTailCallOpt; 2413 2414 // A sibling call is one where we're under the usual C ABI and not planning 2415 // to change that but can still do a tail call: 2416 if (!TailCallOpt && IsTailCall) 2417 IsSibCall = true; 2418 2419 if (IsTailCall) 2420 ++NumTailCalls; 2421 } 2422 2423 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Callee)) { 2424 // FIXME: Remove this hack for function pointer types after removing 2425 // support of old address space mapping. In the new address space 2426 // mapping the pointer in default address space is 64 bit, therefore 2427 // does not need this hack. 2428 if (Callee.getValueType() == MVT::i32) { 2429 const GlobalValue *GV = GA->getGlobal(); 2430 Callee = DAG.getGlobalAddress(GV, DL, MVT::i64, GA->getOffset(), false, 2431 GA->getTargetFlags()); 2432 } 2433 } 2434 assert(Callee.getValueType() == MVT::i64); 2435 2436 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 2437 2438 // Analyze operands of the call, assigning locations to each operand. 2439 SmallVector<CCValAssign, 16> ArgLocs; 2440 CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); 2441 CCAssignFn *AssignFn = CCAssignFnForCall(CallConv, IsVarArg); 2442 2443 // The first 4 bytes are reserved for the callee's emergency stack slot. 2444 CCInfo.AllocateStack(4, 4); 2445 2446 CCInfo.AnalyzeCallOperands(Outs, AssignFn); 2447 2448 // Get a count of how many bytes are to be pushed on the stack. 2449 unsigned NumBytes = CCInfo.getNextStackOffset(); 2450 2451 if (IsSibCall) { 2452 // Since we're not changing the ABI to make this a tail call, the memory 2453 // operands are already available in the caller's incoming argument space. 2454 NumBytes = 0; 2455 } 2456 2457 // FPDiff is the byte offset of the call's argument area from the callee's. 2458 // Stores to callee stack arguments will be placed in FixedStackSlots offset 2459 // by this amount for a tail call. In a sibling call it must be 0 because the 2460 // caller will deallocate the entire stack and the callee still expects its 2461 // arguments to begin at SP+0. Completely unused for non-tail calls. 2462 int32_t FPDiff = 0; 2463 MachineFrameInfo &MFI = MF.getFrameInfo(); 2464 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 2465 2466 SDValue CallerSavedFP; 2467 2468 // Adjust the stack pointer for the new arguments... 2469 // These operations are automatically eliminated by the prolog/epilog pass 2470 if (!IsSibCall) { 2471 Chain = DAG.getCALLSEQ_START(Chain, 0, 0, DL); 2472 2473 unsigned OffsetReg = Info->getScratchWaveOffsetReg(); 2474 2475 // In the HSA case, this should be an identity copy. 2476 SDValue ScratchRSrcReg 2477 = DAG.getCopyFromReg(Chain, DL, Info->getScratchRSrcReg(), MVT::v4i32); 2478 RegsToPass.emplace_back(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3, ScratchRSrcReg); 2479 2480 // TODO: Don't hardcode these registers and get from the callee function. 2481 SDValue ScratchWaveOffsetReg 2482 = DAG.getCopyFromReg(Chain, DL, OffsetReg, MVT::i32); 2483 RegsToPass.emplace_back(AMDGPU::SGPR4, ScratchWaveOffsetReg); 2484 2485 if (!Info->isEntryFunction()) { 2486 // Avoid clobbering this function's FP value. In the current convention 2487 // callee will overwrite this, so do save/restore around the call site. 2488 CallerSavedFP = DAG.getCopyFromReg(Chain, DL, 2489 Info->getFrameOffsetReg(), MVT::i32); 2490 } 2491 } 2492 2493 SmallVector<SDValue, 8> MemOpChains; 2494 MVT PtrVT = MVT::i32; 2495 2496 // Walk the register/memloc assignments, inserting copies/loads. 2497 for (unsigned i = 0, realArgIdx = 0, e = ArgLocs.size(); i != e; 2498 ++i, ++realArgIdx) { 2499 CCValAssign &VA = ArgLocs[i]; 2500 SDValue Arg = OutVals[realArgIdx]; 2501 2502 // Promote the value if needed. 2503 switch (VA.getLocInfo()) { 2504 case CCValAssign::Full: 2505 break; 2506 case CCValAssign::BCvt: 2507 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 2508 break; 2509 case CCValAssign::ZExt: 2510 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg); 2511 break; 2512 case CCValAssign::SExt: 2513 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg); 2514 break; 2515 case CCValAssign::AExt: 2516 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg); 2517 break; 2518 case CCValAssign::FPExt: 2519 Arg = DAG.getNode(ISD::FP_EXTEND, DL, VA.getLocVT(), Arg); 2520 break; 2521 default: 2522 llvm_unreachable("Unknown loc info!"); 2523 } 2524 2525 if (VA.isRegLoc()) { 2526 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 2527 } else { 2528 assert(VA.isMemLoc()); 2529 2530 SDValue DstAddr; 2531 MachinePointerInfo DstInfo; 2532 2533 unsigned LocMemOffset = VA.getLocMemOffset(); 2534 int32_t Offset = LocMemOffset; 2535 2536 SDValue PtrOff = DAG.getConstant(Offset, DL, PtrVT); 2537 2538 if (IsTailCall) { 2539 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 2540 unsigned OpSize = Flags.isByVal() ? 2541 Flags.getByValSize() : VA.getValVT().getStoreSize(); 2542 2543 Offset = Offset + FPDiff; 2544 int FI = MFI.CreateFixedObject(OpSize, Offset, true); 2545 2546 DstAddr = DAG.getFrameIndex(FI, PtrVT); 2547 DstInfo = MachinePointerInfo::getFixedStack(MF, FI); 2548 2549 // Make sure any stack arguments overlapping with where we're storing 2550 // are loaded before this eventual operation. Otherwise they'll be 2551 // clobbered. 2552 2553 // FIXME: Why is this really necessary? This seems to just result in a 2554 // lot of code to copy the stack and write them back to the same 2555 // locations, which are supposed to be immutable? 2556 Chain = addTokenForArgument(Chain, DAG, MFI, FI); 2557 } else { 2558 DstAddr = PtrOff; 2559 DstInfo = MachinePointerInfo::getStack(MF, LocMemOffset); 2560 } 2561 2562 if (Outs[i].Flags.isByVal()) { 2563 SDValue SizeNode = 2564 DAG.getConstant(Outs[i].Flags.getByValSize(), DL, MVT::i32); 2565 SDValue Cpy = DAG.getMemcpy( 2566 Chain, DL, DstAddr, Arg, SizeNode, Outs[i].Flags.getByValAlign(), 2567 /*isVol = */ false, /*AlwaysInline = */ true, 2568 /*isTailCall = */ false, DstInfo, 2569 MachinePointerInfo(UndefValue::get(Type::getInt8PtrTy( 2570 *DAG.getContext(), AMDGPUASI.PRIVATE_ADDRESS)))); 2571 2572 MemOpChains.push_back(Cpy); 2573 } else { 2574 SDValue Store = DAG.getStore(Chain, DL, Arg, DstAddr, DstInfo); 2575 MemOpChains.push_back(Store); 2576 } 2577 } 2578 } 2579 2580 // Copy special input registers after user input arguments. 2581 passSpecialInputs(CLI, CCInfo, *Info, RegsToPass, MemOpChains, Chain); 2582 2583 if (!MemOpChains.empty()) 2584 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); 2585 2586 // Build a sequence of copy-to-reg nodes chained together with token chain 2587 // and flag operands which copy the outgoing args into the appropriate regs. 2588 SDValue InFlag; 2589 for (auto &RegToPass : RegsToPass) { 2590 Chain = DAG.getCopyToReg(Chain, DL, RegToPass.first, 2591 RegToPass.second, InFlag); 2592 InFlag = Chain.getValue(1); 2593 } 2594 2595 2596 SDValue PhysReturnAddrReg; 2597 if (IsTailCall) { 2598 // Since the return is being combined with the call, we need to pass on the 2599 // return address. 2600 2601 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 2602 SDValue ReturnAddrReg = CreateLiveInRegister( 2603 DAG, &AMDGPU::SReg_64RegClass, TRI->getReturnAddressReg(MF), MVT::i64); 2604 2605 PhysReturnAddrReg = DAG.getRegister(TRI->getReturnAddressReg(MF), 2606 MVT::i64); 2607 Chain = DAG.getCopyToReg(Chain, DL, PhysReturnAddrReg, ReturnAddrReg, InFlag); 2608 InFlag = Chain.getValue(1); 2609 } 2610 2611 // We don't usually want to end the call-sequence here because we would tidy 2612 // the frame up *after* the call, however in the ABI-changing tail-call case 2613 // we've carefully laid out the parameters so that when sp is reset they'll be 2614 // in the correct location. 2615 if (IsTailCall && !IsSibCall) { 2616 Chain = DAG.getCALLSEQ_END(Chain, 2617 DAG.getTargetConstant(NumBytes, DL, MVT::i32), 2618 DAG.getTargetConstant(0, DL, MVT::i32), 2619 InFlag, DL); 2620 InFlag = Chain.getValue(1); 2621 } 2622 2623 std::vector<SDValue> Ops; 2624 Ops.push_back(Chain); 2625 Ops.push_back(Callee); 2626 2627 if (IsTailCall) { 2628 // Each tail call may have to adjust the stack by a different amount, so 2629 // this information must travel along with the operation for eventual 2630 // consumption by emitEpilogue. 2631 Ops.push_back(DAG.getTargetConstant(FPDiff, DL, MVT::i32)); 2632 2633 Ops.push_back(PhysReturnAddrReg); 2634 } 2635 2636 // Add argument registers to the end of the list so that they are known live 2637 // into the call. 2638 for (auto &RegToPass : RegsToPass) { 2639 Ops.push_back(DAG.getRegister(RegToPass.first, 2640 RegToPass.second.getValueType())); 2641 } 2642 2643 // Add a register mask operand representing the call-preserved registers. 2644 2645 auto *TRI = static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo()); 2646 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv); 2647 assert(Mask && "Missing call preserved mask for calling convention"); 2648 Ops.push_back(DAG.getRegisterMask(Mask)); 2649 2650 if (InFlag.getNode()) 2651 Ops.push_back(InFlag); 2652 2653 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 2654 2655 // If we're doing a tall call, use a TC_RETURN here rather than an 2656 // actual call instruction. 2657 if (IsTailCall) { 2658 MFI.setHasTailCall(); 2659 return DAG.getNode(AMDGPUISD::TC_RETURN, DL, NodeTys, Ops); 2660 } 2661 2662 // Returns a chain and a flag for retval copy to use. 2663 SDValue Call = DAG.getNode(AMDGPUISD::CALL, DL, NodeTys, Ops); 2664 Chain = Call.getValue(0); 2665 InFlag = Call.getValue(1); 2666 2667 if (CallerSavedFP) { 2668 SDValue FPReg = DAG.getRegister(Info->getFrameOffsetReg(), MVT::i32); 2669 Chain = DAG.getCopyToReg(Chain, DL, FPReg, CallerSavedFP, InFlag); 2670 InFlag = Chain.getValue(1); 2671 } 2672 2673 uint64_t CalleePopBytes = NumBytes; 2674 Chain = DAG.getCALLSEQ_END(Chain, DAG.getTargetConstant(0, DL, MVT::i32), 2675 DAG.getTargetConstant(CalleePopBytes, DL, MVT::i32), 2676 InFlag, DL); 2677 if (!Ins.empty()) 2678 InFlag = Chain.getValue(1); 2679 2680 // Handle result values, copying them out of physregs into vregs that we 2681 // return. 2682 return LowerCallResult(Chain, InFlag, CallConv, IsVarArg, Ins, DL, DAG, 2683 InVals, IsThisReturn, 2684 IsThisReturn ? OutVals[0] : SDValue()); 2685 } 2686 2687 unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT, 2688 SelectionDAG &DAG) const { 2689 unsigned Reg = StringSwitch<unsigned>(RegName) 2690 .Case("m0", AMDGPU::M0) 2691 .Case("exec", AMDGPU::EXEC) 2692 .Case("exec_lo", AMDGPU::EXEC_LO) 2693 .Case("exec_hi", AMDGPU::EXEC_HI) 2694 .Case("flat_scratch", AMDGPU::FLAT_SCR) 2695 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO) 2696 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI) 2697 .Default(AMDGPU::NoRegister); 2698 2699 if (Reg == AMDGPU::NoRegister) { 2700 report_fatal_error(Twine("invalid register name \"" 2701 + StringRef(RegName) + "\".")); 2702 2703 } 2704 2705 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS && 2706 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) { 2707 report_fatal_error(Twine("invalid register \"" 2708 + StringRef(RegName) + "\" for subtarget.")); 2709 } 2710 2711 switch (Reg) { 2712 case AMDGPU::M0: 2713 case AMDGPU::EXEC_LO: 2714 case AMDGPU::EXEC_HI: 2715 case AMDGPU::FLAT_SCR_LO: 2716 case AMDGPU::FLAT_SCR_HI: 2717 if (VT.getSizeInBits() == 32) 2718 return Reg; 2719 break; 2720 case AMDGPU::EXEC: 2721 case AMDGPU::FLAT_SCR: 2722 if (VT.getSizeInBits() == 64) 2723 return Reg; 2724 break; 2725 default: 2726 llvm_unreachable("missing register type checking"); 2727 } 2728 2729 report_fatal_error(Twine("invalid type for register \"" 2730 + StringRef(RegName) + "\".")); 2731 } 2732 2733 // If kill is not the last instruction, split the block so kill is always a 2734 // proper terminator. 2735 MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI, 2736 MachineBasicBlock *BB) const { 2737 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 2738 2739 MachineBasicBlock::iterator SplitPoint(&MI); 2740 ++SplitPoint; 2741 2742 if (SplitPoint == BB->end()) { 2743 // Don't bother with a new block. 2744 MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode())); 2745 return BB; 2746 } 2747 2748 MachineFunction *MF = BB->getParent(); 2749 MachineBasicBlock *SplitBB 2750 = MF->CreateMachineBasicBlock(BB->getBasicBlock()); 2751 2752 MF->insert(++MachineFunction::iterator(BB), SplitBB); 2753 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end()); 2754 2755 SplitBB->transferSuccessorsAndUpdatePHIs(BB); 2756 BB->addSuccessor(SplitBB); 2757 2758 MI.setDesc(TII->getKillTerminatorFromPseudo(MI.getOpcode())); 2759 return SplitBB; 2760 } 2761 2762 // Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the 2763 // wavefront. If the value is uniform and just happens to be in a VGPR, this 2764 // will only do one iteration. In the worst case, this will loop 64 times. 2765 // 2766 // TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value. 2767 static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop( 2768 const SIInstrInfo *TII, 2769 MachineRegisterInfo &MRI, 2770 MachineBasicBlock &OrigBB, 2771 MachineBasicBlock &LoopBB, 2772 const DebugLoc &DL, 2773 const MachineOperand &IdxReg, 2774 unsigned InitReg, 2775 unsigned ResultReg, 2776 unsigned PhiReg, 2777 unsigned InitSaveExecReg, 2778 int Offset, 2779 bool UseGPRIdxMode, 2780 bool IsIndirectSrc) { 2781 MachineBasicBlock::iterator I = LoopBB.begin(); 2782 2783 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 2784 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 2785 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 2786 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 2787 2788 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg) 2789 .addReg(InitReg) 2790 .addMBB(&OrigBB) 2791 .addReg(ResultReg) 2792 .addMBB(&LoopBB); 2793 2794 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec) 2795 .addReg(InitSaveExecReg) 2796 .addMBB(&OrigBB) 2797 .addReg(NewExec) 2798 .addMBB(&LoopBB); 2799 2800 // Read the next variant <- also loop target. 2801 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg) 2802 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef())); 2803 2804 // Compare the just read M0 value to all possible Idx values. 2805 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg) 2806 .addReg(CurrentIdxReg) 2807 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg()); 2808 2809 // Update EXEC, save the original EXEC value to VCC. 2810 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec) 2811 .addReg(CondReg, RegState::Kill); 2812 2813 MRI.setSimpleHint(NewExec, CondReg); 2814 2815 if (UseGPRIdxMode) { 2816 unsigned IdxReg; 2817 if (Offset == 0) { 2818 IdxReg = CurrentIdxReg; 2819 } else { 2820 IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 2821 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg) 2822 .addReg(CurrentIdxReg, RegState::Kill) 2823 .addImm(Offset); 2824 } 2825 unsigned IdxMode = IsIndirectSrc ? 2826 VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE; 2827 MachineInstr *SetOn = 2828 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 2829 .addReg(IdxReg, RegState::Kill) 2830 .addImm(IdxMode); 2831 SetOn->getOperand(3).setIsUndef(); 2832 } else { 2833 // Move index from VCC into M0 2834 if (Offset == 0) { 2835 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 2836 .addReg(CurrentIdxReg, RegState::Kill); 2837 } else { 2838 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) 2839 .addReg(CurrentIdxReg, RegState::Kill) 2840 .addImm(Offset); 2841 } 2842 } 2843 2844 // Update EXEC, switch all done bits to 0 and all todo bits to 1. 2845 MachineInstr *InsertPt = 2846 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC) 2847 .addReg(AMDGPU::EXEC) 2848 .addReg(NewExec); 2849 2850 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use 2851 // s_cbranch_scc0? 2852 2853 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover. 2854 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ)) 2855 .addMBB(&LoopBB); 2856 2857 return InsertPt->getIterator(); 2858 } 2859 2860 // This has slightly sub-optimal regalloc when the source vector is killed by 2861 // the read. The register allocator does not understand that the kill is 2862 // per-workitem, so is kept alive for the whole loop so we end up not re-using a 2863 // subregister from it, using 1 more VGPR than necessary. This was saved when 2864 // this was expanded after register allocation. 2865 static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII, 2866 MachineBasicBlock &MBB, 2867 MachineInstr &MI, 2868 unsigned InitResultReg, 2869 unsigned PhiReg, 2870 int Offset, 2871 bool UseGPRIdxMode, 2872 bool IsIndirectSrc) { 2873 MachineFunction *MF = MBB.getParent(); 2874 MachineRegisterInfo &MRI = MF->getRegInfo(); 2875 const DebugLoc &DL = MI.getDebugLoc(); 2876 MachineBasicBlock::iterator I(&MI); 2877 2878 unsigned DstReg = MI.getOperand(0).getReg(); 2879 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 2880 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 2881 2882 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec); 2883 2884 // Save the EXEC mask 2885 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec) 2886 .addReg(AMDGPU::EXEC); 2887 2888 // To insert the loop we need to split the block. Move everything after this 2889 // point to a new block, and insert a new empty block between the two. 2890 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock(); 2891 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock(); 2892 MachineFunction::iterator MBBI(MBB); 2893 ++MBBI; 2894 2895 MF->insert(MBBI, LoopBB); 2896 MF->insert(MBBI, RemainderBB); 2897 2898 LoopBB->addSuccessor(LoopBB); 2899 LoopBB->addSuccessor(RemainderBB); 2900 2901 // Move the rest of the block into a new block. 2902 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); 2903 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end()); 2904 2905 MBB.addSuccessor(LoopBB); 2906 2907 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 2908 2909 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx, 2910 InitResultReg, DstReg, PhiReg, TmpExec, 2911 Offset, UseGPRIdxMode, IsIndirectSrc); 2912 2913 MachineBasicBlock::iterator First = RemainderBB->begin(); 2914 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC) 2915 .addReg(SaveExec); 2916 2917 return InsPt; 2918 } 2919 2920 // Returns subreg index, offset 2921 static std::pair<unsigned, int> 2922 computeIndirectRegAndOffset(const SIRegisterInfo &TRI, 2923 const TargetRegisterClass *SuperRC, 2924 unsigned VecReg, 2925 int Offset) { 2926 int NumElts = TRI.getRegSizeInBits(*SuperRC) / 32; 2927 2928 // Skip out of bounds offsets, or else we would end up using an undefined 2929 // register. 2930 if (Offset >= NumElts || Offset < 0) 2931 return std::make_pair(AMDGPU::sub0, Offset); 2932 2933 return std::make_pair(AMDGPU::sub0 + Offset, 0); 2934 } 2935 2936 // Return true if the index is an SGPR and was set. 2937 static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII, 2938 MachineRegisterInfo &MRI, 2939 MachineInstr &MI, 2940 int Offset, 2941 bool UseGPRIdxMode, 2942 bool IsIndirectSrc) { 2943 MachineBasicBlock *MBB = MI.getParent(); 2944 const DebugLoc &DL = MI.getDebugLoc(); 2945 MachineBasicBlock::iterator I(&MI); 2946 2947 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 2948 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg()); 2949 2950 assert(Idx->getReg() != AMDGPU::NoRegister); 2951 2952 if (!TII->getRegisterInfo().isSGPRClass(IdxRC)) 2953 return false; 2954 2955 if (UseGPRIdxMode) { 2956 unsigned IdxMode = IsIndirectSrc ? 2957 VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE; 2958 if (Offset == 0) { 2959 MachineInstr *SetOn = 2960 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 2961 .add(*Idx) 2962 .addImm(IdxMode); 2963 2964 SetOn->getOperand(3).setIsUndef(); 2965 } else { 2966 unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 2967 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp) 2968 .add(*Idx) 2969 .addImm(Offset); 2970 MachineInstr *SetOn = 2971 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 2972 .addReg(Tmp, RegState::Kill) 2973 .addImm(IdxMode); 2974 2975 SetOn->getOperand(3).setIsUndef(); 2976 } 2977 2978 return true; 2979 } 2980 2981 if (Offset == 0) { 2982 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 2983 .add(*Idx); 2984 } else { 2985 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) 2986 .add(*Idx) 2987 .addImm(Offset); 2988 } 2989 2990 return true; 2991 } 2992 2993 // Control flow needs to be inserted if indexing with a VGPR. 2994 static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI, 2995 MachineBasicBlock &MBB, 2996 const GCNSubtarget &ST) { 2997 const SIInstrInfo *TII = ST.getInstrInfo(); 2998 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 2999 MachineFunction *MF = MBB.getParent(); 3000 MachineRegisterInfo &MRI = MF->getRegInfo(); 3001 3002 unsigned Dst = MI.getOperand(0).getReg(); 3003 unsigned SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg(); 3004 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); 3005 3006 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg); 3007 3008 unsigned SubReg; 3009 std::tie(SubReg, Offset) 3010 = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset); 3011 3012 bool UseGPRIdxMode = ST.useVGPRIndexMode(EnableVGPRIndexMode); 3013 3014 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) { 3015 MachineBasicBlock::iterator I(&MI); 3016 const DebugLoc &DL = MI.getDebugLoc(); 3017 3018 if (UseGPRIdxMode) { 3019 // TODO: Look at the uses to avoid the copy. This may require rescheduling 3020 // to avoid interfering with other uses, so probably requires a new 3021 // optimization pass. 3022 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) 3023 .addReg(SrcReg, RegState::Undef, SubReg) 3024 .addReg(SrcReg, RegState::Implicit) 3025 .addReg(AMDGPU::M0, RegState::Implicit); 3026 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 3027 } else { 3028 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) 3029 .addReg(SrcReg, RegState::Undef, SubReg) 3030 .addReg(SrcReg, RegState::Implicit); 3031 } 3032 3033 MI.eraseFromParent(); 3034 3035 return &MBB; 3036 } 3037 3038 const DebugLoc &DL = MI.getDebugLoc(); 3039 MachineBasicBlock::iterator I(&MI); 3040 3041 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3042 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3043 3044 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg); 3045 3046 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, 3047 Offset, UseGPRIdxMode, true); 3048 MachineBasicBlock *LoopBB = InsPt->getParent(); 3049 3050 if (UseGPRIdxMode) { 3051 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) 3052 .addReg(SrcReg, RegState::Undef, SubReg) 3053 .addReg(SrcReg, RegState::Implicit) 3054 .addReg(AMDGPU::M0, RegState::Implicit); 3055 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 3056 } else { 3057 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) 3058 .addReg(SrcReg, RegState::Undef, SubReg) 3059 .addReg(SrcReg, RegState::Implicit); 3060 } 3061 3062 MI.eraseFromParent(); 3063 3064 return LoopBB; 3065 } 3066 3067 static unsigned getMOVRELDPseudo(const SIRegisterInfo &TRI, 3068 const TargetRegisterClass *VecRC) { 3069 switch (TRI.getRegSizeInBits(*VecRC)) { 3070 case 32: // 4 bytes 3071 return AMDGPU::V_MOVRELD_B32_V1; 3072 case 64: // 8 bytes 3073 return AMDGPU::V_MOVRELD_B32_V2; 3074 case 128: // 16 bytes 3075 return AMDGPU::V_MOVRELD_B32_V4; 3076 case 256: // 32 bytes 3077 return AMDGPU::V_MOVRELD_B32_V8; 3078 case 512: // 64 bytes 3079 return AMDGPU::V_MOVRELD_B32_V16; 3080 default: 3081 llvm_unreachable("unsupported size for MOVRELD pseudos"); 3082 } 3083 } 3084 3085 static MachineBasicBlock *emitIndirectDst(MachineInstr &MI, 3086 MachineBasicBlock &MBB, 3087 const GCNSubtarget &ST) { 3088 const SIInstrInfo *TII = ST.getInstrInfo(); 3089 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 3090 MachineFunction *MF = MBB.getParent(); 3091 MachineRegisterInfo &MRI = MF->getRegInfo(); 3092 3093 unsigned Dst = MI.getOperand(0).getReg(); 3094 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src); 3095 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 3096 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val); 3097 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); 3098 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg()); 3099 3100 // This can be an immediate, but will be folded later. 3101 assert(Val->getReg()); 3102 3103 unsigned SubReg; 3104 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC, 3105 SrcVec->getReg(), 3106 Offset); 3107 bool UseGPRIdxMode = ST.useVGPRIndexMode(EnableVGPRIndexMode); 3108 3109 if (Idx->getReg() == AMDGPU::NoRegister) { 3110 MachineBasicBlock::iterator I(&MI); 3111 const DebugLoc &DL = MI.getDebugLoc(); 3112 3113 assert(Offset == 0); 3114 3115 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst) 3116 .add(*SrcVec) 3117 .add(*Val) 3118 .addImm(SubReg); 3119 3120 MI.eraseFromParent(); 3121 return &MBB; 3122 } 3123 3124 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) { 3125 MachineBasicBlock::iterator I(&MI); 3126 const DebugLoc &DL = MI.getDebugLoc(); 3127 3128 if (UseGPRIdxMode) { 3129 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect)) 3130 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst 3131 .add(*Val) 3132 .addReg(Dst, RegState::ImplicitDefine) 3133 .addReg(SrcVec->getReg(), RegState::Implicit) 3134 .addReg(AMDGPU::M0, RegState::Implicit); 3135 3136 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 3137 } else { 3138 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(TRI, VecRC)); 3139 3140 BuildMI(MBB, I, DL, MovRelDesc) 3141 .addReg(Dst, RegState::Define) 3142 .addReg(SrcVec->getReg()) 3143 .add(*Val) 3144 .addImm(SubReg - AMDGPU::sub0); 3145 } 3146 3147 MI.eraseFromParent(); 3148 return &MBB; 3149 } 3150 3151 if (Val->isReg()) 3152 MRI.clearKillFlags(Val->getReg()); 3153 3154 const DebugLoc &DL = MI.getDebugLoc(); 3155 3156 unsigned PhiReg = MRI.createVirtualRegister(VecRC); 3157 3158 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg, 3159 Offset, UseGPRIdxMode, false); 3160 MachineBasicBlock *LoopBB = InsPt->getParent(); 3161 3162 if (UseGPRIdxMode) { 3163 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect)) 3164 .addReg(PhiReg, RegState::Undef, SubReg) // vdst 3165 .add(*Val) // src0 3166 .addReg(Dst, RegState::ImplicitDefine) 3167 .addReg(PhiReg, RegState::Implicit) 3168 .addReg(AMDGPU::M0, RegState::Implicit); 3169 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 3170 } else { 3171 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(TRI, VecRC)); 3172 3173 BuildMI(*LoopBB, InsPt, DL, MovRelDesc) 3174 .addReg(Dst, RegState::Define) 3175 .addReg(PhiReg) 3176 .add(*Val) 3177 .addImm(SubReg - AMDGPU::sub0); 3178 } 3179 3180 MI.eraseFromParent(); 3181 3182 return LoopBB; 3183 } 3184 3185 MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter( 3186 MachineInstr &MI, MachineBasicBlock *BB) const { 3187 3188 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3189 MachineFunction *MF = BB->getParent(); 3190 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 3191 3192 if (TII->isMIMG(MI)) { 3193 if (MI.memoperands_empty() && MI.mayLoadOrStore()) { 3194 report_fatal_error("missing mem operand from MIMG instruction"); 3195 } 3196 // Add a memoperand for mimg instructions so that they aren't assumed to 3197 // be ordered memory instuctions. 3198 3199 return BB; 3200 } 3201 3202 switch (MI.getOpcode()) { 3203 case AMDGPU::S_ADD_U64_PSEUDO: 3204 case AMDGPU::S_SUB_U64_PSEUDO: { 3205 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 3206 const DebugLoc &DL = MI.getDebugLoc(); 3207 3208 MachineOperand &Dest = MI.getOperand(0); 3209 MachineOperand &Src0 = MI.getOperand(1); 3210 MachineOperand &Src1 = MI.getOperand(2); 3211 3212 unsigned DestSub0 = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 3213 unsigned DestSub1 = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 3214 3215 MachineOperand Src0Sub0 = TII->buildExtractSubRegOrImm(MI, MRI, 3216 Src0, &AMDGPU::SReg_64RegClass, AMDGPU::sub0, 3217 &AMDGPU::SReg_32_XM0RegClass); 3218 MachineOperand Src0Sub1 = TII->buildExtractSubRegOrImm(MI, MRI, 3219 Src0, &AMDGPU::SReg_64RegClass, AMDGPU::sub1, 3220 &AMDGPU::SReg_32_XM0RegClass); 3221 3222 MachineOperand Src1Sub0 = TII->buildExtractSubRegOrImm(MI, MRI, 3223 Src1, &AMDGPU::SReg_64RegClass, AMDGPU::sub0, 3224 &AMDGPU::SReg_32_XM0RegClass); 3225 MachineOperand Src1Sub1 = TII->buildExtractSubRegOrImm(MI, MRI, 3226 Src1, &AMDGPU::SReg_64RegClass, AMDGPU::sub1, 3227 &AMDGPU::SReg_32_XM0RegClass); 3228 3229 bool IsAdd = (MI.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO); 3230 3231 unsigned LoOpc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32; 3232 unsigned HiOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32; 3233 BuildMI(*BB, MI, DL, TII->get(LoOpc), DestSub0) 3234 .add(Src0Sub0) 3235 .add(Src1Sub0); 3236 BuildMI(*BB, MI, DL, TII->get(HiOpc), DestSub1) 3237 .add(Src0Sub1) 3238 .add(Src1Sub1); 3239 BuildMI(*BB, MI, DL, TII->get(TargetOpcode::REG_SEQUENCE), Dest.getReg()) 3240 .addReg(DestSub0) 3241 .addImm(AMDGPU::sub0) 3242 .addReg(DestSub1) 3243 .addImm(AMDGPU::sub1); 3244 MI.eraseFromParent(); 3245 return BB; 3246 } 3247 case AMDGPU::SI_INIT_M0: { 3248 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(), 3249 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 3250 .add(MI.getOperand(0)); 3251 MI.eraseFromParent(); 3252 return BB; 3253 } 3254 case AMDGPU::SI_INIT_EXEC: 3255 // This should be before all vector instructions. 3256 BuildMI(*BB, &*BB->begin(), MI.getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), 3257 AMDGPU::EXEC) 3258 .addImm(MI.getOperand(0).getImm()); 3259 MI.eraseFromParent(); 3260 return BB; 3261 3262 case AMDGPU::SI_INIT_EXEC_FROM_INPUT: { 3263 // Extract the thread count from an SGPR input and set EXEC accordingly. 3264 // Since BFM can't shift by 64, handle that case with CMP + CMOV. 3265 // 3266 // S_BFE_U32 count, input, {shift, 7} 3267 // S_BFM_B64 exec, count, 0 3268 // S_CMP_EQ_U32 count, 64 3269 // S_CMOV_B64 exec, -1 3270 MachineInstr *FirstMI = &*BB->begin(); 3271 MachineRegisterInfo &MRI = MF->getRegInfo(); 3272 unsigned InputReg = MI.getOperand(0).getReg(); 3273 unsigned CountReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3274 bool Found = false; 3275 3276 // Move the COPY of the input reg to the beginning, so that we can use it. 3277 for (auto I = BB->begin(); I != &MI; I++) { 3278 if (I->getOpcode() != TargetOpcode::COPY || 3279 I->getOperand(0).getReg() != InputReg) 3280 continue; 3281 3282 if (I == FirstMI) { 3283 FirstMI = &*++BB->begin(); 3284 } else { 3285 I->removeFromParent(); 3286 BB->insert(FirstMI, &*I); 3287 } 3288 Found = true; 3289 break; 3290 } 3291 assert(Found); 3292 (void)Found; 3293 3294 // This should be before all vector instructions. 3295 BuildMI(*BB, FirstMI, DebugLoc(), TII->get(AMDGPU::S_BFE_U32), CountReg) 3296 .addReg(InputReg) 3297 .addImm((MI.getOperand(1).getImm() & 0x7f) | 0x70000); 3298 BuildMI(*BB, FirstMI, DebugLoc(), TII->get(AMDGPU::S_BFM_B64), 3299 AMDGPU::EXEC) 3300 .addReg(CountReg) 3301 .addImm(0); 3302 BuildMI(*BB, FirstMI, DebugLoc(), TII->get(AMDGPU::S_CMP_EQ_U32)) 3303 .addReg(CountReg, RegState::Kill) 3304 .addImm(64); 3305 BuildMI(*BB, FirstMI, DebugLoc(), TII->get(AMDGPU::S_CMOV_B64), 3306 AMDGPU::EXEC) 3307 .addImm(-1); 3308 MI.eraseFromParent(); 3309 return BB; 3310 } 3311 3312 case AMDGPU::GET_GROUPSTATICSIZE: { 3313 DebugLoc DL = MI.getDebugLoc(); 3314 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32)) 3315 .add(MI.getOperand(0)) 3316 .addImm(MFI->getLDSSize()); 3317 MI.eraseFromParent(); 3318 return BB; 3319 } 3320 case AMDGPU::SI_INDIRECT_SRC_V1: 3321 case AMDGPU::SI_INDIRECT_SRC_V2: 3322 case AMDGPU::SI_INDIRECT_SRC_V4: 3323 case AMDGPU::SI_INDIRECT_SRC_V8: 3324 case AMDGPU::SI_INDIRECT_SRC_V16: 3325 return emitIndirectSrc(MI, *BB, *getSubtarget()); 3326 case AMDGPU::SI_INDIRECT_DST_V1: 3327 case AMDGPU::SI_INDIRECT_DST_V2: 3328 case AMDGPU::SI_INDIRECT_DST_V4: 3329 case AMDGPU::SI_INDIRECT_DST_V8: 3330 case AMDGPU::SI_INDIRECT_DST_V16: 3331 return emitIndirectDst(MI, *BB, *getSubtarget()); 3332 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO: 3333 case AMDGPU::SI_KILL_I1_PSEUDO: 3334 return splitKillBlock(MI, BB); 3335 case AMDGPU::V_CNDMASK_B64_PSEUDO: { 3336 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 3337 3338 unsigned Dst = MI.getOperand(0).getReg(); 3339 unsigned Src0 = MI.getOperand(1).getReg(); 3340 unsigned Src1 = MI.getOperand(2).getReg(); 3341 const DebugLoc &DL = MI.getDebugLoc(); 3342 unsigned SrcCond = MI.getOperand(3).getReg(); 3343 3344 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3345 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3346 unsigned SrcCondCopy = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 3347 3348 BuildMI(*BB, MI, DL, TII->get(AMDGPU::COPY), SrcCondCopy) 3349 .addReg(SrcCond); 3350 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo) 3351 .addReg(Src0, 0, AMDGPU::sub0) 3352 .addReg(Src1, 0, AMDGPU::sub0) 3353 .addReg(SrcCondCopy); 3354 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi) 3355 .addReg(Src0, 0, AMDGPU::sub1) 3356 .addReg(Src1, 0, AMDGPU::sub1) 3357 .addReg(SrcCondCopy); 3358 3359 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst) 3360 .addReg(DstLo) 3361 .addImm(AMDGPU::sub0) 3362 .addReg(DstHi) 3363 .addImm(AMDGPU::sub1); 3364 MI.eraseFromParent(); 3365 return BB; 3366 } 3367 case AMDGPU::SI_BR_UNDEF: { 3368 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3369 const DebugLoc &DL = MI.getDebugLoc(); 3370 MachineInstr *Br = BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CBRANCH_SCC1)) 3371 .add(MI.getOperand(0)); 3372 Br->getOperand(1).setIsUndef(true); // read undef SCC 3373 MI.eraseFromParent(); 3374 return BB; 3375 } 3376 case AMDGPU::ADJCALLSTACKUP: 3377 case AMDGPU::ADJCALLSTACKDOWN: { 3378 const SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>(); 3379 MachineInstrBuilder MIB(*MF, &MI); 3380 3381 // Add an implicit use of the frame offset reg to prevent the restore copy 3382 // inserted after the call from being reorderd after stack operations in the 3383 // the caller's frame. 3384 MIB.addReg(Info->getStackPtrOffsetReg(), RegState::ImplicitDefine) 3385 .addReg(Info->getStackPtrOffsetReg(), RegState::Implicit) 3386 .addReg(Info->getFrameOffsetReg(), RegState::Implicit); 3387 return BB; 3388 } 3389 case AMDGPU::SI_CALL_ISEL: 3390 case AMDGPU::SI_TCRETURN_ISEL: { 3391 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3392 const DebugLoc &DL = MI.getDebugLoc(); 3393 unsigned ReturnAddrReg = TII->getRegisterInfo().getReturnAddressReg(*MF); 3394 3395 MachineRegisterInfo &MRI = MF->getRegInfo(); 3396 unsigned GlobalAddrReg = MI.getOperand(0).getReg(); 3397 MachineInstr *PCRel = MRI.getVRegDef(GlobalAddrReg); 3398 assert(PCRel->getOpcode() == AMDGPU::SI_PC_ADD_REL_OFFSET); 3399 3400 const GlobalValue *G = PCRel->getOperand(1).getGlobal(); 3401 3402 MachineInstrBuilder MIB; 3403 if (MI.getOpcode() == AMDGPU::SI_CALL_ISEL) { 3404 MIB = BuildMI(*BB, MI, DL, TII->get(AMDGPU::SI_CALL), ReturnAddrReg) 3405 .add(MI.getOperand(0)) 3406 .addGlobalAddress(G); 3407 } else { 3408 MIB = BuildMI(*BB, MI, DL, TII->get(AMDGPU::SI_TCRETURN)) 3409 .add(MI.getOperand(0)) 3410 .addGlobalAddress(G); 3411 3412 // There is an additional imm operand for tcreturn, but it should be in the 3413 // right place already. 3414 } 3415 3416 for (unsigned I = 1, E = MI.getNumOperands(); I != E; ++I) 3417 MIB.add(MI.getOperand(I)); 3418 3419 MIB.cloneMemRefs(MI); 3420 MI.eraseFromParent(); 3421 return BB; 3422 } 3423 default: 3424 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); 3425 } 3426 } 3427 3428 bool SITargetLowering::hasBitPreservingFPLogic(EVT VT) const { 3429 return isTypeLegal(VT.getScalarType()); 3430 } 3431 3432 bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const { 3433 // This currently forces unfolding various combinations of fsub into fma with 3434 // free fneg'd operands. As long as we have fast FMA (controlled by 3435 // isFMAFasterThanFMulAndFAdd), we should perform these. 3436 3437 // When fma is quarter rate, for f64 where add / sub are at best half rate, 3438 // most of these combines appear to be cycle neutral but save on instruction 3439 // count / code size. 3440 return true; 3441 } 3442 3443 EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx, 3444 EVT VT) const { 3445 if (!VT.isVector()) { 3446 return MVT::i1; 3447 } 3448 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements()); 3449 } 3450 3451 MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT VT) const { 3452 // TODO: Should i16 be used always if legal? For now it would force VALU 3453 // shifts. 3454 return (VT == MVT::i16) ? MVT::i16 : MVT::i32; 3455 } 3456 3457 // Answering this is somewhat tricky and depends on the specific device which 3458 // have different rates for fma or all f64 operations. 3459 // 3460 // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other 3461 // regardless of which device (although the number of cycles differs between 3462 // devices), so it is always profitable for f64. 3463 // 3464 // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable 3465 // only on full rate devices. Normally, we should prefer selecting v_mad_f32 3466 // which we can always do even without fused FP ops since it returns the same 3467 // result as the separate operations and since it is always full 3468 // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32 3469 // however does not support denormals, so we do report fma as faster if we have 3470 // a fast fma device and require denormals. 3471 // 3472 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 3473 VT = VT.getScalarType(); 3474 3475 switch (VT.getSimpleVT().SimpleTy) { 3476 case MVT::f32: { 3477 // This is as fast on some subtargets. However, we always have full rate f32 3478 // mad available which returns the same result as the separate operations 3479 // which we should prefer over fma. We can't use this if we want to support 3480 // denormals, so only report this in these cases. 3481 if (Subtarget->hasFP32Denormals()) 3482 return Subtarget->hasFastFMAF32() || Subtarget->hasDLInsts(); 3483 3484 // If the subtarget has v_fmac_f32, that's just as good as v_mac_f32. 3485 return Subtarget->hasFastFMAF32() && Subtarget->hasDLInsts(); 3486 } 3487 case MVT::f64: 3488 return true; 3489 case MVT::f16: 3490 return Subtarget->has16BitInsts() && Subtarget->hasFP16Denormals(); 3491 default: 3492 break; 3493 } 3494 3495 return false; 3496 } 3497 3498 //===----------------------------------------------------------------------===// 3499 // Custom DAG Lowering Operations 3500 //===----------------------------------------------------------------------===// 3501 3502 // Work around LegalizeDAG doing the wrong thing and fully scalarizing if the 3503 // wider vector type is legal. 3504 SDValue SITargetLowering::splitUnaryVectorOp(SDValue Op, 3505 SelectionDAG &DAG) const { 3506 unsigned Opc = Op.getOpcode(); 3507 EVT VT = Op.getValueType(); 3508 assert(VT == MVT::v4f16); 3509 3510 SDValue Lo, Hi; 3511 std::tie(Lo, Hi) = DAG.SplitVectorOperand(Op.getNode(), 0); 3512 3513 SDLoc SL(Op); 3514 SDValue OpLo = DAG.getNode(Opc, SL, Lo.getValueType(), Lo, 3515 Op->getFlags()); 3516 SDValue OpHi = DAG.getNode(Opc, SL, Hi.getValueType(), Hi, 3517 Op->getFlags()); 3518 3519 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi); 3520 } 3521 3522 // Work around LegalizeDAG doing the wrong thing and fully scalarizing if the 3523 // wider vector type is legal. 3524 SDValue SITargetLowering::splitBinaryVectorOp(SDValue Op, 3525 SelectionDAG &DAG) const { 3526 unsigned Opc = Op.getOpcode(); 3527 EVT VT = Op.getValueType(); 3528 assert(VT == MVT::v4i16 || VT == MVT::v4f16); 3529 3530 SDValue Lo0, Hi0; 3531 std::tie(Lo0, Hi0) = DAG.SplitVectorOperand(Op.getNode(), 0); 3532 SDValue Lo1, Hi1; 3533 std::tie(Lo1, Hi1) = DAG.SplitVectorOperand(Op.getNode(), 1); 3534 3535 SDLoc SL(Op); 3536 3537 SDValue OpLo = DAG.getNode(Opc, SL, Lo0.getValueType(), Lo0, Lo1, 3538 Op->getFlags()); 3539 SDValue OpHi = DAG.getNode(Opc, SL, Hi0.getValueType(), Hi0, Hi1, 3540 Op->getFlags()); 3541 3542 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(Op), VT, OpLo, OpHi); 3543 } 3544 3545 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 3546 switch (Op.getOpcode()) { 3547 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); 3548 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 3549 case ISD::LOAD: { 3550 SDValue Result = LowerLOAD(Op, DAG); 3551 assert((!Result.getNode() || 3552 Result.getNode()->getNumValues() == 2) && 3553 "Load should return a value and a chain"); 3554 return Result; 3555 } 3556 3557 case ISD::FSIN: 3558 case ISD::FCOS: 3559 return LowerTrig(Op, DAG); 3560 case ISD::SELECT: return LowerSELECT(Op, DAG); 3561 case ISD::FDIV: return LowerFDIV(Op, DAG); 3562 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG); 3563 case ISD::STORE: return LowerSTORE(Op, DAG); 3564 case ISD::GlobalAddress: { 3565 MachineFunction &MF = DAG.getMachineFunction(); 3566 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 3567 return LowerGlobalAddress(MFI, Op, DAG); 3568 } 3569 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 3570 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG); 3571 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG); 3572 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG); 3573 case ISD::INSERT_VECTOR_ELT: 3574 return lowerINSERT_VECTOR_ELT(Op, DAG); 3575 case ISD::EXTRACT_VECTOR_ELT: 3576 return lowerEXTRACT_VECTOR_ELT(Op, DAG); 3577 case ISD::BUILD_VECTOR: 3578 return lowerBUILD_VECTOR(Op, DAG); 3579 case ISD::FP_ROUND: 3580 return lowerFP_ROUND(Op, DAG); 3581 case ISD::TRAP: 3582 return lowerTRAP(Op, DAG); 3583 case ISD::DEBUGTRAP: 3584 return lowerDEBUGTRAP(Op, DAG); 3585 case ISD::FABS: 3586 case ISD::FNEG: 3587 case ISD::FCANONICALIZE: 3588 return splitUnaryVectorOp(Op, DAG); 3589 case ISD::SHL: 3590 case ISD::SRA: 3591 case ISD::SRL: 3592 case ISD::ADD: 3593 case ISD::SUB: 3594 case ISD::MUL: 3595 case ISD::SMIN: 3596 case ISD::SMAX: 3597 case ISD::UMIN: 3598 case ISD::UMAX: 3599 case ISD::FMINNUM: 3600 case ISD::FMAXNUM: 3601 case ISD::FADD: 3602 case ISD::FMUL: 3603 return splitBinaryVectorOp(Op, DAG); 3604 } 3605 return SDValue(); 3606 } 3607 3608 static SDValue adjustLoadValueTypeImpl(SDValue Result, EVT LoadVT, 3609 const SDLoc &DL, 3610 SelectionDAG &DAG, bool Unpacked) { 3611 if (!LoadVT.isVector()) 3612 return Result; 3613 3614 if (Unpacked) { // From v2i32/v4i32 back to v2f16/v4f16. 3615 // Truncate to v2i16/v4i16. 3616 EVT IntLoadVT = LoadVT.changeTypeToInteger(); 3617 3618 // Workaround legalizer not scalarizing truncate after vector op 3619 // legalization byt not creating intermediate vector trunc. 3620 SmallVector<SDValue, 4> Elts; 3621 DAG.ExtractVectorElements(Result, Elts); 3622 for (SDValue &Elt : Elts) 3623 Elt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, Elt); 3624 3625 Result = DAG.getBuildVector(IntLoadVT, DL, Elts); 3626 3627 // Bitcast to original type (v2f16/v4f16). 3628 return DAG.getNode(ISD::BITCAST, DL, LoadVT, Result); 3629 } 3630 3631 // Cast back to the original packed type. 3632 return DAG.getNode(ISD::BITCAST, DL, LoadVT, Result); 3633 } 3634 3635 SDValue SITargetLowering::adjustLoadValueType(unsigned Opcode, 3636 MemSDNode *M, 3637 SelectionDAG &DAG, 3638 ArrayRef<SDValue> Ops, 3639 bool IsIntrinsic) const { 3640 SDLoc DL(M); 3641 3642 bool Unpacked = Subtarget->hasUnpackedD16VMem(); 3643 EVT LoadVT = M->getValueType(0); 3644 3645 EVT EquivLoadVT = LoadVT; 3646 if (Unpacked && LoadVT.isVector()) { 3647 EquivLoadVT = LoadVT.isVector() ? 3648 EVT::getVectorVT(*DAG.getContext(), MVT::i32, 3649 LoadVT.getVectorNumElements()) : LoadVT; 3650 } 3651 3652 // Change from v4f16/v2f16 to EquivLoadVT. 3653 SDVTList VTList = DAG.getVTList(EquivLoadVT, MVT::Other); 3654 3655 SDValue Load 3656 = DAG.getMemIntrinsicNode( 3657 IsIntrinsic ? (unsigned)ISD::INTRINSIC_W_CHAIN : Opcode, DL, 3658 VTList, Ops, M->getMemoryVT(), 3659 M->getMemOperand()); 3660 if (!Unpacked) // Just adjusted the opcode. 3661 return Load; 3662 3663 SDValue Adjusted = adjustLoadValueTypeImpl(Load, LoadVT, DL, DAG, Unpacked); 3664 3665 return DAG.getMergeValues({ Adjusted, Load.getValue(1) }, DL); 3666 } 3667 3668 static SDValue lowerICMPIntrinsic(const SITargetLowering &TLI, 3669 SDNode *N, SelectionDAG &DAG) { 3670 EVT VT = N->getValueType(0); 3671 const auto *CD = dyn_cast<ConstantSDNode>(N->getOperand(3)); 3672 if (!CD) 3673 return DAG.getUNDEF(VT); 3674 3675 int CondCode = CD->getSExtValue(); 3676 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE || 3677 CondCode > ICmpInst::Predicate::LAST_ICMP_PREDICATE) 3678 return DAG.getUNDEF(VT); 3679 3680 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode); 3681 3682 3683 SDValue LHS = N->getOperand(1); 3684 SDValue RHS = N->getOperand(2); 3685 3686 SDLoc DL(N); 3687 3688 EVT CmpVT = LHS.getValueType(); 3689 if (CmpVT == MVT::i16 && !TLI.isTypeLegal(MVT::i16)) { 3690 unsigned PromoteOp = ICmpInst::isSigned(IcInput) ? 3691 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 3692 LHS = DAG.getNode(PromoteOp, DL, MVT::i32, LHS); 3693 RHS = DAG.getNode(PromoteOp, DL, MVT::i32, RHS); 3694 } 3695 3696 ISD::CondCode CCOpcode = getICmpCondCode(IcInput); 3697 3698 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, LHS, RHS, 3699 DAG.getCondCode(CCOpcode)); 3700 } 3701 3702 static SDValue lowerFCMPIntrinsic(const SITargetLowering &TLI, 3703 SDNode *N, SelectionDAG &DAG) { 3704 EVT VT = N->getValueType(0); 3705 const auto *CD = dyn_cast<ConstantSDNode>(N->getOperand(3)); 3706 if (!CD) 3707 return DAG.getUNDEF(VT); 3708 3709 int CondCode = CD->getSExtValue(); 3710 if (CondCode < FCmpInst::Predicate::FIRST_FCMP_PREDICATE || 3711 CondCode > FCmpInst::Predicate::LAST_FCMP_PREDICATE) { 3712 return DAG.getUNDEF(VT); 3713 } 3714 3715 SDValue Src0 = N->getOperand(1); 3716 SDValue Src1 = N->getOperand(2); 3717 EVT CmpVT = Src0.getValueType(); 3718 SDLoc SL(N); 3719 3720 if (CmpVT == MVT::f16 && !TLI.isTypeLegal(CmpVT)) { 3721 Src0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0); 3722 Src1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1); 3723 } 3724 3725 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode); 3726 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput); 3727 return DAG.getNode(AMDGPUISD::SETCC, SL, VT, Src0, 3728 Src1, DAG.getCondCode(CCOpcode)); 3729 } 3730 3731 void SITargetLowering::ReplaceNodeResults(SDNode *N, 3732 SmallVectorImpl<SDValue> &Results, 3733 SelectionDAG &DAG) const { 3734 switch (N->getOpcode()) { 3735 case ISD::INSERT_VECTOR_ELT: { 3736 if (SDValue Res = lowerINSERT_VECTOR_ELT(SDValue(N, 0), DAG)) 3737 Results.push_back(Res); 3738 return; 3739 } 3740 case ISD::EXTRACT_VECTOR_ELT: { 3741 if (SDValue Res = lowerEXTRACT_VECTOR_ELT(SDValue(N, 0), DAG)) 3742 Results.push_back(Res); 3743 return; 3744 } 3745 case ISD::INTRINSIC_WO_CHAIN: { 3746 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 3747 switch (IID) { 3748 case Intrinsic::amdgcn_cvt_pkrtz: { 3749 SDValue Src0 = N->getOperand(1); 3750 SDValue Src1 = N->getOperand(2); 3751 SDLoc SL(N); 3752 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_PKRTZ_F16_F32, SL, MVT::i32, 3753 Src0, Src1); 3754 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Cvt)); 3755 return; 3756 } 3757 case Intrinsic::amdgcn_cvt_pknorm_i16: 3758 case Intrinsic::amdgcn_cvt_pknorm_u16: 3759 case Intrinsic::amdgcn_cvt_pk_i16: 3760 case Intrinsic::amdgcn_cvt_pk_u16: { 3761 SDValue Src0 = N->getOperand(1); 3762 SDValue Src1 = N->getOperand(2); 3763 SDLoc SL(N); 3764 unsigned Opcode; 3765 3766 if (IID == Intrinsic::amdgcn_cvt_pknorm_i16) 3767 Opcode = AMDGPUISD::CVT_PKNORM_I16_F32; 3768 else if (IID == Intrinsic::amdgcn_cvt_pknorm_u16) 3769 Opcode = AMDGPUISD::CVT_PKNORM_U16_F32; 3770 else if (IID == Intrinsic::amdgcn_cvt_pk_i16) 3771 Opcode = AMDGPUISD::CVT_PK_I16_I32; 3772 else 3773 Opcode = AMDGPUISD::CVT_PK_U16_U32; 3774 3775 EVT VT = N->getValueType(0); 3776 if (isTypeLegal(VT)) 3777 Results.push_back(DAG.getNode(Opcode, SL, VT, Src0, Src1)); 3778 else { 3779 SDValue Cvt = DAG.getNode(Opcode, SL, MVT::i32, Src0, Src1); 3780 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, Cvt)); 3781 } 3782 return; 3783 } 3784 } 3785 break; 3786 } 3787 case ISD::INTRINSIC_W_CHAIN: { 3788 if (SDValue Res = LowerINTRINSIC_W_CHAIN(SDValue(N, 0), DAG)) { 3789 Results.push_back(Res); 3790 Results.push_back(Res.getValue(1)); 3791 return; 3792 } 3793 3794 break; 3795 } 3796 case ISD::SELECT: { 3797 SDLoc SL(N); 3798 EVT VT = N->getValueType(0); 3799 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT); 3800 SDValue LHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(1)); 3801 SDValue RHS = DAG.getNode(ISD::BITCAST, SL, NewVT, N->getOperand(2)); 3802 3803 EVT SelectVT = NewVT; 3804 if (NewVT.bitsLT(MVT::i32)) { 3805 LHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, LHS); 3806 RHS = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, RHS); 3807 SelectVT = MVT::i32; 3808 } 3809 3810 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, SelectVT, 3811 N->getOperand(0), LHS, RHS); 3812 3813 if (NewVT != SelectVT) 3814 NewSelect = DAG.getNode(ISD::TRUNCATE, SL, NewVT, NewSelect); 3815 Results.push_back(DAG.getNode(ISD::BITCAST, SL, VT, NewSelect)); 3816 return; 3817 } 3818 case ISD::FNEG: { 3819 if (N->getValueType(0) != MVT::v2f16) 3820 break; 3821 3822 SDLoc SL(N); 3823 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, N->getOperand(0)); 3824 3825 SDValue Op = DAG.getNode(ISD::XOR, SL, MVT::i32, 3826 BC, 3827 DAG.getConstant(0x80008000, SL, MVT::i32)); 3828 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Op)); 3829 return; 3830 } 3831 case ISD::FABS: { 3832 if (N->getValueType(0) != MVT::v2f16) 3833 break; 3834 3835 SDLoc SL(N); 3836 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, N->getOperand(0)); 3837 3838 SDValue Op = DAG.getNode(ISD::AND, SL, MVT::i32, 3839 BC, 3840 DAG.getConstant(0x7fff7fff, SL, MVT::i32)); 3841 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Op)); 3842 return; 3843 } 3844 default: 3845 break; 3846 } 3847 } 3848 3849 /// Helper function for LowerBRCOND 3850 static SDNode *findUser(SDValue Value, unsigned Opcode) { 3851 3852 SDNode *Parent = Value.getNode(); 3853 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); 3854 I != E; ++I) { 3855 3856 if (I.getUse().get() != Value) 3857 continue; 3858 3859 if (I->getOpcode() == Opcode) 3860 return *I; 3861 } 3862 return nullptr; 3863 } 3864 3865 unsigned SITargetLowering::isCFIntrinsic(const SDNode *Intr) const { 3866 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) { 3867 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) { 3868 case Intrinsic::amdgcn_if: 3869 return AMDGPUISD::IF; 3870 case Intrinsic::amdgcn_else: 3871 return AMDGPUISD::ELSE; 3872 case Intrinsic::amdgcn_loop: 3873 return AMDGPUISD::LOOP; 3874 case Intrinsic::amdgcn_end_cf: 3875 llvm_unreachable("should not occur"); 3876 default: 3877 return 0; 3878 } 3879 } 3880 3881 // break, if_break, else_break are all only used as inputs to loop, not 3882 // directly as branch conditions. 3883 return 0; 3884 } 3885 3886 void SITargetLowering::createDebuggerPrologueStackObjects( 3887 MachineFunction &MF) const { 3888 // Create stack objects that are used for emitting debugger prologue. 3889 // 3890 // Debugger prologue writes work group IDs and work item IDs to scratch memory 3891 // at fixed location in the following format: 3892 // offset 0: work group ID x 3893 // offset 4: work group ID y 3894 // offset 8: work group ID z 3895 // offset 16: work item ID x 3896 // offset 20: work item ID y 3897 // offset 24: work item ID z 3898 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 3899 int ObjectIdx = 0; 3900 3901 // For each dimension: 3902 for (unsigned i = 0; i < 3; ++i) { 3903 // Create fixed stack object for work group ID. 3904 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true); 3905 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx); 3906 // Create fixed stack object for work item ID. 3907 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true); 3908 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx); 3909 } 3910 } 3911 3912 bool SITargetLowering::shouldEmitFixup(const GlobalValue *GV) const { 3913 const Triple &TT = getTargetMachine().getTargetTriple(); 3914 return (GV->getType()->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS || 3915 GV->getType()->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS_32BIT) && 3916 AMDGPU::shouldEmitConstantsToTextSection(TT); 3917 } 3918 3919 bool SITargetLowering::shouldEmitGOTReloc(const GlobalValue *GV) const { 3920 return (GV->getType()->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS || 3921 GV->getType()->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS || 3922 GV->getType()->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS_32BIT) && 3923 !shouldEmitFixup(GV) && 3924 !getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV); 3925 } 3926 3927 bool SITargetLowering::shouldEmitPCReloc(const GlobalValue *GV) const { 3928 return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV); 3929 } 3930 3931 /// This transforms the control flow intrinsics to get the branch destination as 3932 /// last parameter, also switches branch target with BR if the need arise 3933 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, 3934 SelectionDAG &DAG) const { 3935 SDLoc DL(BRCOND); 3936 3937 SDNode *Intr = BRCOND.getOperand(1).getNode(); 3938 SDValue Target = BRCOND.getOperand(2); 3939 SDNode *BR = nullptr; 3940 SDNode *SetCC = nullptr; 3941 3942 if (Intr->getOpcode() == ISD::SETCC) { 3943 // As long as we negate the condition everything is fine 3944 SetCC = Intr; 3945 Intr = SetCC->getOperand(0).getNode(); 3946 3947 } else { 3948 // Get the target from BR if we don't negate the condition 3949 BR = findUser(BRCOND, ISD::BR); 3950 Target = BR->getOperand(1); 3951 } 3952 3953 // FIXME: This changes the types of the intrinsics instead of introducing new 3954 // nodes with the correct types. 3955 // e.g. llvm.amdgcn.loop 3956 3957 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3 3958 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088> 3959 3960 unsigned CFNode = isCFIntrinsic(Intr); 3961 if (CFNode == 0) { 3962 // This is a uniform branch so we don't need to legalize. 3963 return BRCOND; 3964 } 3965 3966 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID || 3967 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN; 3968 3969 assert(!SetCC || 3970 (SetCC->getConstantOperandVal(1) == 1 && 3971 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == 3972 ISD::SETNE)); 3973 3974 // operands of the new intrinsic call 3975 SmallVector<SDValue, 4> Ops; 3976 if (HaveChain) 3977 Ops.push_back(BRCOND.getOperand(0)); 3978 3979 Ops.append(Intr->op_begin() + (HaveChain ? 2 : 1), Intr->op_end()); 3980 Ops.push_back(Target); 3981 3982 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end()); 3983 3984 // build the new intrinsic call 3985 SDNode *Result = DAG.getNode(CFNode, DL, DAG.getVTList(Res), Ops).getNode(); 3986 3987 if (!HaveChain) { 3988 SDValue Ops[] = { 3989 SDValue(Result, 0), 3990 BRCOND.getOperand(0) 3991 }; 3992 3993 Result = DAG.getMergeValues(Ops, DL).getNode(); 3994 } 3995 3996 if (BR) { 3997 // Give the branch instruction our target 3998 SDValue Ops[] = { 3999 BR->getOperand(0), 4000 BRCOND.getOperand(2) 4001 }; 4002 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops); 4003 DAG.ReplaceAllUsesWith(BR, NewBR.getNode()); 4004 BR = NewBR.getNode(); 4005 } 4006 4007 SDValue Chain = SDValue(Result, Result->getNumValues() - 1); 4008 4009 // Copy the intrinsic results to registers 4010 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { 4011 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); 4012 if (!CopyToReg) 4013 continue; 4014 4015 Chain = DAG.getCopyToReg( 4016 Chain, DL, 4017 CopyToReg->getOperand(1), 4018 SDValue(Result, i - 1), 4019 SDValue()); 4020 4021 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); 4022 } 4023 4024 // Remove the old intrinsic from the chain 4025 DAG.ReplaceAllUsesOfValueWith( 4026 SDValue(Intr, Intr->getNumValues() - 1), 4027 Intr->getOperand(0)); 4028 4029 return Chain; 4030 } 4031 4032 SDValue SITargetLowering::getFPExtOrFPTrunc(SelectionDAG &DAG, 4033 SDValue Op, 4034 const SDLoc &DL, 4035 EVT VT) const { 4036 return Op.getValueType().bitsLE(VT) ? 4037 DAG.getNode(ISD::FP_EXTEND, DL, VT, Op) : 4038 DAG.getNode(ISD::FTRUNC, DL, VT, Op); 4039 } 4040 4041 SDValue SITargetLowering::lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { 4042 assert(Op.getValueType() == MVT::f16 && 4043 "Do not know how to custom lower FP_ROUND for non-f16 type"); 4044 4045 SDValue Src = Op.getOperand(0); 4046 EVT SrcVT = Src.getValueType(); 4047 if (SrcVT != MVT::f64) 4048 return Op; 4049 4050 SDLoc DL(Op); 4051 4052 SDValue FpToFp16 = DAG.getNode(ISD::FP_TO_FP16, DL, MVT::i32, Src); 4053 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16); 4054 return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc); 4055 } 4056 4057 SDValue SITargetLowering::lowerTRAP(SDValue Op, SelectionDAG &DAG) const { 4058 SDLoc SL(Op); 4059 SDValue Chain = Op.getOperand(0); 4060 4061 if (Subtarget->getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbiHsa || 4062 !Subtarget->isTrapHandlerEnabled()) 4063 return DAG.getNode(AMDGPUISD::ENDPGM, SL, MVT::Other, Chain); 4064 4065 MachineFunction &MF = DAG.getMachineFunction(); 4066 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 4067 unsigned UserSGPR = Info->getQueuePtrUserSGPR(); 4068 assert(UserSGPR != AMDGPU::NoRegister); 4069 SDValue QueuePtr = CreateLiveInRegister( 4070 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64); 4071 SDValue SGPR01 = DAG.getRegister(AMDGPU::SGPR0_SGPR1, MVT::i64); 4072 SDValue ToReg = DAG.getCopyToReg(Chain, SL, SGPR01, 4073 QueuePtr, SDValue()); 4074 SDValue Ops[] = { 4075 ToReg, 4076 DAG.getTargetConstant(GCNSubtarget::TrapIDLLVMTrap, SL, MVT::i16), 4077 SGPR01, 4078 ToReg.getValue(1) 4079 }; 4080 return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops); 4081 } 4082 4083 SDValue SITargetLowering::lowerDEBUGTRAP(SDValue Op, SelectionDAG &DAG) const { 4084 SDLoc SL(Op); 4085 SDValue Chain = Op.getOperand(0); 4086 MachineFunction &MF = DAG.getMachineFunction(); 4087 4088 if (Subtarget->getTrapHandlerAbi() != GCNSubtarget::TrapHandlerAbiHsa || 4089 !Subtarget->isTrapHandlerEnabled()) { 4090 DiagnosticInfoUnsupported NoTrap(MF.getFunction(), 4091 "debugtrap handler not supported", 4092 Op.getDebugLoc(), 4093 DS_Warning); 4094 LLVMContext &Ctx = MF.getFunction().getContext(); 4095 Ctx.diagnose(NoTrap); 4096 return Chain; 4097 } 4098 4099 SDValue Ops[] = { 4100 Chain, 4101 DAG.getTargetConstant(GCNSubtarget::TrapIDLLVMDebugTrap, SL, MVT::i16) 4102 }; 4103 return DAG.getNode(AMDGPUISD::TRAP, SL, MVT::Other, Ops); 4104 } 4105 4106 SDValue SITargetLowering::getSegmentAperture(unsigned AS, const SDLoc &DL, 4107 SelectionDAG &DAG) const { 4108 // FIXME: Use inline constants (src_{shared, private}_base) instead. 4109 if (Subtarget->hasApertureRegs()) { 4110 unsigned Offset = AS == AMDGPUASI.LOCAL_ADDRESS ? 4111 AMDGPU::Hwreg::OFFSET_SRC_SHARED_BASE : 4112 AMDGPU::Hwreg::OFFSET_SRC_PRIVATE_BASE; 4113 unsigned WidthM1 = AS == AMDGPUASI.LOCAL_ADDRESS ? 4114 AMDGPU::Hwreg::WIDTH_M1_SRC_SHARED_BASE : 4115 AMDGPU::Hwreg::WIDTH_M1_SRC_PRIVATE_BASE; 4116 unsigned Encoding = 4117 AMDGPU::Hwreg::ID_MEM_BASES << AMDGPU::Hwreg::ID_SHIFT_ | 4118 Offset << AMDGPU::Hwreg::OFFSET_SHIFT_ | 4119 WidthM1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_; 4120 4121 SDValue EncodingImm = DAG.getTargetConstant(Encoding, DL, MVT::i16); 4122 SDValue ApertureReg = SDValue( 4123 DAG.getMachineNode(AMDGPU::S_GETREG_B32, DL, MVT::i32, EncodingImm), 0); 4124 SDValue ShiftAmount = DAG.getTargetConstant(WidthM1 + 1, DL, MVT::i32); 4125 return DAG.getNode(ISD::SHL, DL, MVT::i32, ApertureReg, ShiftAmount); 4126 } 4127 4128 MachineFunction &MF = DAG.getMachineFunction(); 4129 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 4130 unsigned UserSGPR = Info->getQueuePtrUserSGPR(); 4131 assert(UserSGPR != AMDGPU::NoRegister); 4132 4133 SDValue QueuePtr = CreateLiveInRegister( 4134 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64); 4135 4136 // Offset into amd_queue_t for group_segment_aperture_base_hi / 4137 // private_segment_aperture_base_hi. 4138 uint32_t StructOffset = (AS == AMDGPUASI.LOCAL_ADDRESS) ? 0x40 : 0x44; 4139 4140 SDValue Ptr = DAG.getObjectPtrOffset(DL, QueuePtr, StructOffset); 4141 4142 // TODO: Use custom target PseudoSourceValue. 4143 // TODO: We should use the value from the IR intrinsic call, but it might not 4144 // be available and how do we get it? 4145 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()), 4146 AMDGPUASI.CONSTANT_ADDRESS)); 4147 4148 MachinePointerInfo PtrInfo(V, StructOffset); 4149 return DAG.getLoad(MVT::i32, DL, QueuePtr.getValue(1), Ptr, PtrInfo, 4150 MinAlign(64, StructOffset), 4151 MachineMemOperand::MODereferenceable | 4152 MachineMemOperand::MOInvariant); 4153 } 4154 4155 SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op, 4156 SelectionDAG &DAG) const { 4157 SDLoc SL(Op); 4158 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op); 4159 4160 SDValue Src = ASC->getOperand(0); 4161 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64); 4162 4163 const AMDGPUTargetMachine &TM = 4164 static_cast<const AMDGPUTargetMachine &>(getTargetMachine()); 4165 4166 // flat -> local/private 4167 if (ASC->getSrcAddressSpace() == AMDGPUASI.FLAT_ADDRESS) { 4168 unsigned DestAS = ASC->getDestAddressSpace(); 4169 4170 if (DestAS == AMDGPUASI.LOCAL_ADDRESS || 4171 DestAS == AMDGPUASI.PRIVATE_ADDRESS) { 4172 unsigned NullVal = TM.getNullPointerValue(DestAS); 4173 SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32); 4174 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE); 4175 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src); 4176 4177 return DAG.getNode(ISD::SELECT, SL, MVT::i32, 4178 NonNull, Ptr, SegmentNullPtr); 4179 } 4180 } 4181 4182 // local/private -> flat 4183 if (ASC->getDestAddressSpace() == AMDGPUASI.FLAT_ADDRESS) { 4184 unsigned SrcAS = ASC->getSrcAddressSpace(); 4185 4186 if (SrcAS == AMDGPUASI.LOCAL_ADDRESS || 4187 SrcAS == AMDGPUASI.PRIVATE_ADDRESS) { 4188 unsigned NullVal = TM.getNullPointerValue(SrcAS); 4189 SDValue SegmentNullPtr = DAG.getConstant(NullVal, SL, MVT::i32); 4190 4191 SDValue NonNull 4192 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE); 4193 4194 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), SL, DAG); 4195 SDValue CvtPtr 4196 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture); 4197 4198 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull, 4199 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr), 4200 FlatNullPtr); 4201 } 4202 } 4203 4204 // global <-> flat are no-ops and never emitted. 4205 4206 const MachineFunction &MF = DAG.getMachineFunction(); 4207 DiagnosticInfoUnsupported InvalidAddrSpaceCast( 4208 MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc()); 4209 DAG.getContext()->diagnose(InvalidAddrSpaceCast); 4210 4211 return DAG.getUNDEF(ASC->getValueType(0)); 4212 } 4213 4214 SDValue SITargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op, 4215 SelectionDAG &DAG) const { 4216 SDValue Vec = Op.getOperand(0); 4217 SDValue InsVal = Op.getOperand(1); 4218 SDValue Idx = Op.getOperand(2); 4219 EVT VecVT = Vec.getValueType(); 4220 EVT EltVT = VecVT.getVectorElementType(); 4221 unsigned VecSize = VecVT.getSizeInBits(); 4222 unsigned EltSize = EltVT.getSizeInBits(); 4223 4224 4225 assert(VecSize <= 64); 4226 4227 unsigned NumElts = VecVT.getVectorNumElements(); 4228 SDLoc SL(Op); 4229 auto KIdx = dyn_cast<ConstantSDNode>(Idx); 4230 4231 if (NumElts == 4 && EltSize == 16 && KIdx) { 4232 SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Vec); 4233 4234 SDValue LoHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BCVec, 4235 DAG.getConstant(0, SL, MVT::i32)); 4236 SDValue HiHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BCVec, 4237 DAG.getConstant(1, SL, MVT::i32)); 4238 4239 SDValue LoVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, LoHalf); 4240 SDValue HiVec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i16, HiHalf); 4241 4242 unsigned Idx = KIdx->getZExtValue(); 4243 bool InsertLo = Idx < 2; 4244 SDValue InsHalf = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, MVT::v2i16, 4245 InsertLo ? LoVec : HiVec, 4246 DAG.getNode(ISD::BITCAST, SL, MVT::i16, InsVal), 4247 DAG.getConstant(InsertLo ? Idx : (Idx - 2), SL, MVT::i32)); 4248 4249 InsHalf = DAG.getNode(ISD::BITCAST, SL, MVT::i32, InsHalf); 4250 4251 SDValue Concat = InsertLo ? 4252 DAG.getBuildVector(MVT::v2i32, SL, { InsHalf, HiHalf }) : 4253 DAG.getBuildVector(MVT::v2i32, SL, { LoHalf, InsHalf }); 4254 4255 return DAG.getNode(ISD::BITCAST, SL, VecVT, Concat); 4256 } 4257 4258 if (isa<ConstantSDNode>(Idx)) 4259 return SDValue(); 4260 4261 MVT IntVT = MVT::getIntegerVT(VecSize); 4262 4263 // Avoid stack access for dynamic indexing. 4264 SDValue Val = InsVal; 4265 if (InsVal.getValueType() == MVT::f16) 4266 Val = DAG.getNode(ISD::BITCAST, SL, MVT::i16, InsVal); 4267 4268 // v_bfi_b32 (v_bfm_b32 16, (shl idx, 16)), val, vec 4269 SDValue ExtVal = DAG.getNode(ISD::ZERO_EXTEND, SL, IntVT, Val); 4270 4271 assert(isPowerOf2_32(EltSize)); 4272 SDValue ScaleFactor = DAG.getConstant(Log2_32(EltSize), SL, MVT::i32); 4273 4274 // Convert vector index to bit-index. 4275 SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, ScaleFactor); 4276 4277 SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, IntVT, Vec); 4278 SDValue BFM = DAG.getNode(ISD::SHL, SL, IntVT, 4279 DAG.getConstant(0xffff, SL, IntVT), 4280 ScaledIdx); 4281 4282 SDValue LHS = DAG.getNode(ISD::AND, SL, IntVT, BFM, ExtVal); 4283 SDValue RHS = DAG.getNode(ISD::AND, SL, IntVT, 4284 DAG.getNOT(SL, BFM, IntVT), BCVec); 4285 4286 SDValue BFI = DAG.getNode(ISD::OR, SL, IntVT, LHS, RHS); 4287 return DAG.getNode(ISD::BITCAST, SL, VecVT, BFI); 4288 } 4289 4290 SDValue SITargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op, 4291 SelectionDAG &DAG) const { 4292 SDLoc SL(Op); 4293 4294 EVT ResultVT = Op.getValueType(); 4295 SDValue Vec = Op.getOperand(0); 4296 SDValue Idx = Op.getOperand(1); 4297 EVT VecVT = Vec.getValueType(); 4298 unsigned VecSize = VecVT.getSizeInBits(); 4299 EVT EltVT = VecVT.getVectorElementType(); 4300 assert(VecSize <= 64); 4301 4302 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr); 4303 4304 // Make sure we do any optimizations that will make it easier to fold 4305 // source modifiers before obscuring it with bit operations. 4306 4307 // XXX - Why doesn't this get called when vector_shuffle is expanded? 4308 if (SDValue Combined = performExtractVectorEltCombine(Op.getNode(), DCI)) 4309 return Combined; 4310 4311 unsigned EltSize = EltVT.getSizeInBits(); 4312 assert(isPowerOf2_32(EltSize)); 4313 4314 MVT IntVT = MVT::getIntegerVT(VecSize); 4315 SDValue ScaleFactor = DAG.getConstant(Log2_32(EltSize), SL, MVT::i32); 4316 4317 // Convert vector index to bit-index (* EltSize) 4318 SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, ScaleFactor); 4319 4320 SDValue BC = DAG.getNode(ISD::BITCAST, SL, IntVT, Vec); 4321 SDValue Elt = DAG.getNode(ISD::SRL, SL, IntVT, BC, ScaledIdx); 4322 4323 if (ResultVT == MVT::f16) { 4324 SDValue Result = DAG.getNode(ISD::TRUNCATE, SL, MVT::i16, Elt); 4325 return DAG.getNode(ISD::BITCAST, SL, ResultVT, Result); 4326 } 4327 4328 return DAG.getAnyExtOrTrunc(Elt, SL, ResultVT); 4329 } 4330 4331 SDValue SITargetLowering::lowerBUILD_VECTOR(SDValue Op, 4332 SelectionDAG &DAG) const { 4333 SDLoc SL(Op); 4334 EVT VT = Op.getValueType(); 4335 4336 if (VT == MVT::v4i16 || VT == MVT::v4f16) { 4337 EVT HalfVT = MVT::getVectorVT(VT.getVectorElementType().getSimpleVT(), 2); 4338 4339 // Turn into pair of packed build_vectors. 4340 // TODO: Special case for constants that can be materialized with s_mov_b64. 4341 SDValue Lo = DAG.getBuildVector(HalfVT, SL, 4342 { Op.getOperand(0), Op.getOperand(1) }); 4343 SDValue Hi = DAG.getBuildVector(HalfVT, SL, 4344 { Op.getOperand(2), Op.getOperand(3) }); 4345 4346 SDValue CastLo = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Lo); 4347 SDValue CastHi = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Hi); 4348 4349 SDValue Blend = DAG.getBuildVector(MVT::v2i32, SL, { CastLo, CastHi }); 4350 return DAG.getNode(ISD::BITCAST, SL, VT, Blend); 4351 } 4352 4353 assert(VT == MVT::v2f16 || VT == MVT::v2i16); 4354 assert(!Subtarget->hasVOP3PInsts() && "this should be legal"); 4355 4356 SDValue Lo = Op.getOperand(0); 4357 SDValue Hi = Op.getOperand(1); 4358 4359 // Avoid adding defined bits with the zero_extend. 4360 if (Hi.isUndef()) { 4361 Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Lo); 4362 SDValue ExtLo = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Lo); 4363 return DAG.getNode(ISD::BITCAST, SL, VT, ExtLo); 4364 } 4365 4366 Hi = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Hi); 4367 Hi = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Hi); 4368 4369 SDValue ShlHi = DAG.getNode(ISD::SHL, SL, MVT::i32, Hi, 4370 DAG.getConstant(16, SL, MVT::i32)); 4371 if (Lo.isUndef()) 4372 return DAG.getNode(ISD::BITCAST, SL, VT, ShlHi); 4373 4374 Lo = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Lo); 4375 Lo = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Lo); 4376 4377 SDValue Or = DAG.getNode(ISD::OR, SL, MVT::i32, Lo, ShlHi); 4378 return DAG.getNode(ISD::BITCAST, SL, VT, Or); 4379 } 4380 4381 bool 4382 SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 4383 // We can fold offsets for anything that doesn't require a GOT relocation. 4384 return (GA->getAddressSpace() == AMDGPUASI.GLOBAL_ADDRESS || 4385 GA->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS || 4386 GA->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS_32BIT) && 4387 !shouldEmitGOTReloc(GA->getGlobal()); 4388 } 4389 4390 static SDValue 4391 buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV, 4392 const SDLoc &DL, unsigned Offset, EVT PtrVT, 4393 unsigned GAFlags = SIInstrInfo::MO_NONE) { 4394 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is 4395 // lowered to the following code sequence: 4396 // 4397 // For constant address space: 4398 // s_getpc_b64 s[0:1] 4399 // s_add_u32 s0, s0, $symbol 4400 // s_addc_u32 s1, s1, 0 4401 // 4402 // s_getpc_b64 returns the address of the s_add_u32 instruction and then 4403 // a fixup or relocation is emitted to replace $symbol with a literal 4404 // constant, which is a pc-relative offset from the encoding of the $symbol 4405 // operand to the global variable. 4406 // 4407 // For global address space: 4408 // s_getpc_b64 s[0:1] 4409 // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo 4410 // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi 4411 // 4412 // s_getpc_b64 returns the address of the s_add_u32 instruction and then 4413 // fixups or relocations are emitted to replace $symbol@*@lo and 4414 // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant, 4415 // which is a 64-bit pc-relative offset from the encoding of the $symbol 4416 // operand to the global variable. 4417 // 4418 // What we want here is an offset from the value returned by s_getpc 4419 // (which is the address of the s_add_u32 instruction) to the global 4420 // variable, but since the encoding of $symbol starts 4 bytes after the start 4421 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too 4422 // small. This requires us to add 4 to the global variable offset in order to 4423 // compute the correct address. 4424 SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, 4425 GAFlags); 4426 SDValue PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, 4427 GAFlags == SIInstrInfo::MO_NONE ? 4428 GAFlags : GAFlags + 1); 4429 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi); 4430 } 4431 4432 SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI, 4433 SDValue Op, 4434 SelectionDAG &DAG) const { 4435 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op); 4436 const GlobalValue *GV = GSD->getGlobal(); 4437 4438 if (GSD->getAddressSpace() != AMDGPUASI.CONSTANT_ADDRESS && 4439 GSD->getAddressSpace() != AMDGPUASI.CONSTANT_ADDRESS_32BIT && 4440 GSD->getAddressSpace() != AMDGPUASI.GLOBAL_ADDRESS && 4441 // FIXME: It isn't correct to rely on the type of the pointer. This should 4442 // be removed when address space 0 is 64-bit. 4443 !GV->getType()->getElementType()->isFunctionTy()) 4444 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); 4445 4446 SDLoc DL(GSD); 4447 EVT PtrVT = Op.getValueType(); 4448 4449 if (shouldEmitFixup(GV)) 4450 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT); 4451 else if (shouldEmitPCReloc(GV)) 4452 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT, 4453 SIInstrInfo::MO_REL32); 4454 4455 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT, 4456 SIInstrInfo::MO_GOTPCREL32); 4457 4458 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext()); 4459 PointerType *PtrTy = PointerType::get(Ty, AMDGPUASI.CONSTANT_ADDRESS); 4460 const DataLayout &DataLayout = DAG.getDataLayout(); 4461 unsigned Align = DataLayout.getABITypeAlignment(PtrTy); 4462 // FIXME: Use a PseudoSourceValue once those can be assigned an address space. 4463 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); 4464 4465 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align, 4466 MachineMemOperand::MODereferenceable | 4467 MachineMemOperand::MOInvariant); 4468 } 4469 4470 SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, 4471 const SDLoc &DL, SDValue V) const { 4472 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as 4473 // the destination register. 4474 // 4475 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions, 4476 // so we will end up with redundant moves to m0. 4477 // 4478 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result. 4479 4480 // A Null SDValue creates a glue result. 4481 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue, 4482 V, Chain); 4483 return SDValue(M0, 0); 4484 } 4485 4486 SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG, 4487 SDValue Op, 4488 MVT VT, 4489 unsigned Offset) const { 4490 SDLoc SL(Op); 4491 SDValue Param = lowerKernargMemParameter(DAG, MVT::i32, MVT::i32, SL, 4492 DAG.getEntryNode(), Offset, 4, false); 4493 // The local size values will have the hi 16-bits as zero. 4494 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param, 4495 DAG.getValueType(VT)); 4496 } 4497 4498 static SDValue emitNonHSAIntrinsicError(SelectionDAG &DAG, const SDLoc &DL, 4499 EVT VT) { 4500 DiagnosticInfoUnsupported BadIntrin(DAG.getMachineFunction().getFunction(), 4501 "non-hsa intrinsic with hsa target", 4502 DL.getDebugLoc()); 4503 DAG.getContext()->diagnose(BadIntrin); 4504 return DAG.getUNDEF(VT); 4505 } 4506 4507 static SDValue emitRemovedIntrinsicError(SelectionDAG &DAG, const SDLoc &DL, 4508 EVT VT) { 4509 DiagnosticInfoUnsupported BadIntrin(DAG.getMachineFunction().getFunction(), 4510 "intrinsic not supported on subtarget", 4511 DL.getDebugLoc()); 4512 DAG.getContext()->diagnose(BadIntrin); 4513 return DAG.getUNDEF(VT); 4514 } 4515 4516 static SDValue getBuildDwordsVector(SelectionDAG &DAG, SDLoc DL, 4517 ArrayRef<SDValue> Elts) { 4518 assert(!Elts.empty()); 4519 MVT Type; 4520 unsigned NumElts; 4521 4522 if (Elts.size() == 1) { 4523 Type = MVT::f32; 4524 NumElts = 1; 4525 } else if (Elts.size() == 2) { 4526 Type = MVT::v2f32; 4527 NumElts = 2; 4528 } else if (Elts.size() <= 4) { 4529 Type = MVT::v4f32; 4530 NumElts = 4; 4531 } else if (Elts.size() <= 8) { 4532 Type = MVT::v8f32; 4533 NumElts = 8; 4534 } else { 4535 assert(Elts.size() <= 16); 4536 Type = MVT::v16f32; 4537 NumElts = 16; 4538 } 4539 4540 SmallVector<SDValue, 16> VecElts(NumElts); 4541 for (unsigned i = 0; i < Elts.size(); ++i) { 4542 SDValue Elt = Elts[i]; 4543 if (Elt.getValueType() != MVT::f32) 4544 Elt = DAG.getBitcast(MVT::f32, Elt); 4545 VecElts[i] = Elt; 4546 } 4547 for (unsigned i = Elts.size(); i < NumElts; ++i) 4548 VecElts[i] = DAG.getUNDEF(MVT::f32); 4549 4550 if (NumElts == 1) 4551 return VecElts[0]; 4552 return DAG.getBuildVector(Type, DL, VecElts); 4553 } 4554 4555 static bool parseCachePolicy(SDValue CachePolicy, SelectionDAG &DAG, 4556 SDValue *GLC, SDValue *SLC) { 4557 auto CachePolicyConst = dyn_cast<ConstantSDNode>(CachePolicy.getNode()); 4558 if (!CachePolicyConst) 4559 return false; 4560 4561 uint64_t Value = CachePolicyConst->getZExtValue(); 4562 SDLoc DL(CachePolicy); 4563 if (GLC) { 4564 *GLC = DAG.getTargetConstant((Value & 0x1) ? 1 : 0, DL, MVT::i32); 4565 Value &= ~(uint64_t)0x1; 4566 } 4567 if (SLC) { 4568 *SLC = DAG.getTargetConstant((Value & 0x2) ? 1 : 0, DL, MVT::i32); 4569 Value &= ~(uint64_t)0x2; 4570 } 4571 4572 return Value == 0; 4573 } 4574 4575 SDValue SITargetLowering::lowerImage(SDValue Op, 4576 const AMDGPU::ImageDimIntrinsicInfo *Intr, 4577 SelectionDAG &DAG) const { 4578 SDLoc DL(Op); 4579 MachineFunction &MF = DAG.getMachineFunction(); 4580 const GCNSubtarget* ST = &MF.getSubtarget<GCNSubtarget>(); 4581 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode = 4582 AMDGPU::getMIMGBaseOpcodeInfo(Intr->BaseOpcode); 4583 const AMDGPU::MIMGDimInfo *DimInfo = AMDGPU::getMIMGDimInfo(Intr->Dim); 4584 const AMDGPU::MIMGLZMappingInfo *LZMappingInfo = 4585 AMDGPU::getMIMGLZMappingInfo(Intr->BaseOpcode); 4586 unsigned IntrOpcode = Intr->BaseOpcode; 4587 4588 SmallVector<EVT, 2> ResultTypes(Op->value_begin(), Op->value_end()); 4589 bool IsD16 = false; 4590 bool IsA16 = false; 4591 SDValue VData; 4592 int NumVDataDwords; 4593 unsigned AddrIdx; // Index of first address argument 4594 unsigned DMask; 4595 4596 if (BaseOpcode->Atomic) { 4597 VData = Op.getOperand(2); 4598 4599 bool Is64Bit = VData.getValueType() == MVT::i64; 4600 if (BaseOpcode->AtomicX2) { 4601 SDValue VData2 = Op.getOperand(3); 4602 VData = DAG.getBuildVector(Is64Bit ? MVT::v2i64 : MVT::v2i32, DL, 4603 {VData, VData2}); 4604 if (Is64Bit) 4605 VData = DAG.getBitcast(MVT::v4i32, VData); 4606 4607 ResultTypes[0] = Is64Bit ? MVT::v2i64 : MVT::v2i32; 4608 DMask = Is64Bit ? 0xf : 0x3; 4609 NumVDataDwords = Is64Bit ? 4 : 2; 4610 AddrIdx = 4; 4611 } else { 4612 DMask = Is64Bit ? 0x3 : 0x1; 4613 NumVDataDwords = Is64Bit ? 2 : 1; 4614 AddrIdx = 3; 4615 } 4616 } else { 4617 unsigned DMaskIdx; 4618 4619 if (BaseOpcode->Store) { 4620 VData = Op.getOperand(2); 4621 4622 MVT StoreVT = VData.getSimpleValueType(); 4623 if (StoreVT.getScalarType() == MVT::f16) { 4624 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS || 4625 !BaseOpcode->HasD16) 4626 return Op; // D16 is unsupported for this instruction 4627 4628 IsD16 = true; 4629 VData = handleD16VData(VData, DAG); 4630 } 4631 4632 NumVDataDwords = (VData.getValueType().getSizeInBits() + 31) / 32; 4633 DMaskIdx = 3; 4634 } else { 4635 MVT LoadVT = Op.getSimpleValueType(); 4636 if (LoadVT.getScalarType() == MVT::f16) { 4637 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS || 4638 !BaseOpcode->HasD16) 4639 return Op; // D16 is unsupported for this instruction 4640 4641 IsD16 = true; 4642 if (LoadVT.isVector() && Subtarget->hasUnpackedD16VMem()) 4643 ResultTypes[0] = (LoadVT == MVT::v2f16) ? MVT::v2i32 : MVT::v4i32; 4644 } 4645 4646 NumVDataDwords = (ResultTypes[0].getSizeInBits() + 31) / 32; 4647 DMaskIdx = isa<MemSDNode>(Op) ? 2 : 1; 4648 } 4649 4650 auto DMaskConst = dyn_cast<ConstantSDNode>(Op.getOperand(DMaskIdx)); 4651 if (!DMaskConst) 4652 return Op; 4653 4654 AddrIdx = DMaskIdx + 1; 4655 DMask = DMaskConst->getZExtValue(); 4656 if (!DMask && !BaseOpcode->Store) { 4657 // Eliminate no-op loads. Stores with dmask == 0 are *not* no-op: they 4658 // store the channels' default values. 4659 SDValue Undef = DAG.getUNDEF(Op.getValueType()); 4660 if (isa<MemSDNode>(Op)) 4661 return DAG.getMergeValues({Undef, Op.getOperand(0)}, DL); 4662 return Undef; 4663 } 4664 } 4665 4666 unsigned NumGradients = BaseOpcode->Gradients ? DimInfo->NumGradients : 0; 4667 unsigned NumCoords = BaseOpcode->Coordinates ? DimInfo->NumCoords : 0; 4668 unsigned NumLCM = BaseOpcode->LodOrClampOrMip ? 1 : 0; 4669 unsigned NumVAddrs = BaseOpcode->NumExtraArgs + NumGradients + 4670 NumCoords + NumLCM; 4671 unsigned NumMIVAddrs = NumVAddrs; 4672 4673 SmallVector<SDValue, 4> VAddrs; 4674 4675 // Optimize _L to _LZ when _L is zero 4676 if (LZMappingInfo) { 4677 if (auto ConstantLod = 4678 dyn_cast<ConstantFPSDNode>(Op.getOperand(AddrIdx+NumVAddrs-1))) { 4679 if (ConstantLod->isZero() || ConstantLod->isNegative()) { 4680 IntrOpcode = LZMappingInfo->LZ; // set new opcode to _lz variant of _l 4681 NumMIVAddrs--; // remove 'lod' 4682 } 4683 } 4684 } 4685 4686 // Check for 16 bit addresses and pack if true. 4687 unsigned DimIdx = AddrIdx + BaseOpcode->NumExtraArgs; 4688 MVT VAddrVT = Op.getOperand(DimIdx).getSimpleValueType(); 4689 if (VAddrVT.getScalarType() == MVT::f16 && 4690 ST->hasFeature(AMDGPU::FeatureR128A16)) { 4691 IsA16 = true; 4692 for (unsigned i = AddrIdx; i < (AddrIdx + NumMIVAddrs); ++i) { 4693 SDValue AddrLo, AddrHi; 4694 // Push back extra arguments. 4695 if (i < DimIdx) { 4696 AddrLo = Op.getOperand(i); 4697 } else { 4698 AddrLo = Op.getOperand(i); 4699 // Dz/dh, dz/dv and the last odd coord are packed with undef. Also, 4700 // in 1D, derivatives dx/dh and dx/dv are packed with undef. 4701 if (((i + 1) >= (AddrIdx + NumMIVAddrs)) || 4702 ((NumGradients / 2) % 2 == 1 && 4703 (i == DimIdx + (NumGradients / 2) - 1 || 4704 i == DimIdx + NumGradients - 1))) { 4705 AddrHi = DAG.getUNDEF(MVT::f16); 4706 } else { 4707 AddrHi = Op.getOperand(i + 1); 4708 i++; 4709 } 4710 AddrLo = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v2f16, 4711 {AddrLo, AddrHi}); 4712 AddrLo = DAG.getBitcast(MVT::i32, AddrLo); 4713 } 4714 VAddrs.push_back(AddrLo); 4715 } 4716 } else { 4717 for (unsigned i = 0; i < NumMIVAddrs; ++i) 4718 VAddrs.push_back(Op.getOperand(AddrIdx + i)); 4719 } 4720 4721 SDValue VAddr = getBuildDwordsVector(DAG, DL, VAddrs); 4722 4723 SDValue True = DAG.getTargetConstant(1, DL, MVT::i1); 4724 SDValue False = DAG.getTargetConstant(0, DL, MVT::i1); 4725 unsigned CtrlIdx; // Index of texfailctrl argument 4726 SDValue Unorm; 4727 if (!BaseOpcode->Sampler) { 4728 Unorm = True; 4729 CtrlIdx = AddrIdx + NumVAddrs + 1; 4730 } else { 4731 auto UnormConst = 4732 dyn_cast<ConstantSDNode>(Op.getOperand(AddrIdx + NumVAddrs + 2)); 4733 if (!UnormConst) 4734 return Op; 4735 4736 Unorm = UnormConst->getZExtValue() ? True : False; 4737 CtrlIdx = AddrIdx + NumVAddrs + 3; 4738 } 4739 4740 SDValue TexFail = Op.getOperand(CtrlIdx); 4741 auto TexFailConst = dyn_cast<ConstantSDNode>(TexFail.getNode()); 4742 if (!TexFailConst || TexFailConst->getZExtValue() != 0) 4743 return Op; 4744 4745 SDValue GLC; 4746 SDValue SLC; 4747 if (BaseOpcode->Atomic) { 4748 GLC = True; // TODO no-return optimization 4749 if (!parseCachePolicy(Op.getOperand(CtrlIdx + 1), DAG, nullptr, &SLC)) 4750 return Op; 4751 } else { 4752 if (!parseCachePolicy(Op.getOperand(CtrlIdx + 1), DAG, &GLC, &SLC)) 4753 return Op; 4754 } 4755 4756 SmallVector<SDValue, 14> Ops; 4757 if (BaseOpcode->Store || BaseOpcode->Atomic) 4758 Ops.push_back(VData); // vdata 4759 Ops.push_back(VAddr); 4760 Ops.push_back(Op.getOperand(AddrIdx + NumVAddrs)); // rsrc 4761 if (BaseOpcode->Sampler) 4762 Ops.push_back(Op.getOperand(AddrIdx + NumVAddrs + 1)); // sampler 4763 Ops.push_back(DAG.getTargetConstant(DMask, DL, MVT::i32)); 4764 Ops.push_back(Unorm); 4765 Ops.push_back(GLC); 4766 Ops.push_back(SLC); 4767 Ops.push_back(IsA16 && // a16 or r128 4768 ST->hasFeature(AMDGPU::FeatureR128A16) ? True : False); 4769 Ops.push_back(False); // tfe 4770 Ops.push_back(False); // lwe 4771 Ops.push_back(DimInfo->DA ? True : False); 4772 if (BaseOpcode->HasD16) 4773 Ops.push_back(IsD16 ? True : False); 4774 if (isa<MemSDNode>(Op)) 4775 Ops.push_back(Op.getOperand(0)); // chain 4776 4777 int NumVAddrDwords = VAddr.getValueType().getSizeInBits() / 32; 4778 int Opcode = -1; 4779 4780 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 4781 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx8, 4782 NumVDataDwords, NumVAddrDwords); 4783 if (Opcode == -1) 4784 Opcode = AMDGPU::getMIMGOpcode(IntrOpcode, AMDGPU::MIMGEncGfx6, 4785 NumVDataDwords, NumVAddrDwords); 4786 assert(Opcode != -1); 4787 4788 MachineSDNode *NewNode = DAG.getMachineNode(Opcode, DL, ResultTypes, Ops); 4789 if (auto MemOp = dyn_cast<MemSDNode>(Op)) { 4790 MachineMemOperand *MemRef = MemOp->getMemOperand(); 4791 DAG.setNodeMemRefs(NewNode, {MemRef}); 4792 } 4793 4794 if (BaseOpcode->AtomicX2) { 4795 SmallVector<SDValue, 1> Elt; 4796 DAG.ExtractVectorElements(SDValue(NewNode, 0), Elt, 0, 1); 4797 return DAG.getMergeValues({Elt[0], SDValue(NewNode, 1)}, DL); 4798 } else if (IsD16 && !BaseOpcode->Store) { 4799 MVT LoadVT = Op.getSimpleValueType(); 4800 SDValue Adjusted = adjustLoadValueTypeImpl( 4801 SDValue(NewNode, 0), LoadVT, DL, DAG, Subtarget->hasUnpackedD16VMem()); 4802 return DAG.getMergeValues({Adjusted, SDValue(NewNode, 1)}, DL); 4803 } 4804 4805 return SDValue(NewNode, 0); 4806 } 4807 4808 SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 4809 SelectionDAG &DAG) const { 4810 MachineFunction &MF = DAG.getMachineFunction(); 4811 auto MFI = MF.getInfo<SIMachineFunctionInfo>(); 4812 4813 EVT VT = Op.getValueType(); 4814 SDLoc DL(Op); 4815 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 4816 4817 // TODO: Should this propagate fast-math-flags? 4818 4819 switch (IntrinsicID) { 4820 case Intrinsic::amdgcn_implicit_buffer_ptr: { 4821 if (getSubtarget()->isAmdCodeObjectV2(MF.getFunction())) 4822 return emitNonHSAIntrinsicError(DAG, DL, VT); 4823 return getPreloadedValue(DAG, *MFI, VT, 4824 AMDGPUFunctionArgInfo::IMPLICIT_BUFFER_PTR); 4825 } 4826 case Intrinsic::amdgcn_dispatch_ptr: 4827 case Intrinsic::amdgcn_queue_ptr: { 4828 if (!Subtarget->isAmdCodeObjectV2(MF.getFunction())) { 4829 DiagnosticInfoUnsupported BadIntrin( 4830 MF.getFunction(), "unsupported hsa intrinsic without hsa target", 4831 DL.getDebugLoc()); 4832 DAG.getContext()->diagnose(BadIntrin); 4833 return DAG.getUNDEF(VT); 4834 } 4835 4836 auto RegID = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ? 4837 AMDGPUFunctionArgInfo::DISPATCH_PTR : AMDGPUFunctionArgInfo::QUEUE_PTR; 4838 return getPreloadedValue(DAG, *MFI, VT, RegID); 4839 } 4840 case Intrinsic::amdgcn_implicitarg_ptr: { 4841 if (MFI->isEntryFunction()) 4842 return getImplicitArgPtr(DAG, DL); 4843 return getPreloadedValue(DAG, *MFI, VT, 4844 AMDGPUFunctionArgInfo::IMPLICIT_ARG_PTR); 4845 } 4846 case Intrinsic::amdgcn_kernarg_segment_ptr: { 4847 return getPreloadedValue(DAG, *MFI, VT, 4848 AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR); 4849 } 4850 case Intrinsic::amdgcn_dispatch_id: { 4851 return getPreloadedValue(DAG, *MFI, VT, AMDGPUFunctionArgInfo::DISPATCH_ID); 4852 } 4853 case Intrinsic::amdgcn_rcp: 4854 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1)); 4855 case Intrinsic::amdgcn_rsq: 4856 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 4857 case Intrinsic::amdgcn_rsq_legacy: 4858 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 4859 return emitRemovedIntrinsicError(DAG, DL, VT); 4860 4861 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1)); 4862 case Intrinsic::amdgcn_rcp_legacy: 4863 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 4864 return emitRemovedIntrinsicError(DAG, DL, VT); 4865 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1)); 4866 case Intrinsic::amdgcn_rsq_clamp: { 4867 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) 4868 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1)); 4869 4870 Type *Type = VT.getTypeForEVT(*DAG.getContext()); 4871 APFloat Max = APFloat::getLargest(Type->getFltSemantics()); 4872 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true); 4873 4874 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 4875 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq, 4876 DAG.getConstantFP(Max, DL, VT)); 4877 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp, 4878 DAG.getConstantFP(Min, DL, VT)); 4879 } 4880 case Intrinsic::r600_read_ngroups_x: 4881 if (Subtarget->isAmdHsaOS()) 4882 return emitNonHSAIntrinsicError(DAG, DL, VT); 4883 4884 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4885 SI::KernelInputOffsets::NGROUPS_X, 4, false); 4886 case Intrinsic::r600_read_ngroups_y: 4887 if (Subtarget->isAmdHsaOS()) 4888 return emitNonHSAIntrinsicError(DAG, DL, VT); 4889 4890 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4891 SI::KernelInputOffsets::NGROUPS_Y, 4, false); 4892 case Intrinsic::r600_read_ngroups_z: 4893 if (Subtarget->isAmdHsaOS()) 4894 return emitNonHSAIntrinsicError(DAG, DL, VT); 4895 4896 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4897 SI::KernelInputOffsets::NGROUPS_Z, 4, false); 4898 case Intrinsic::r600_read_global_size_x: 4899 if (Subtarget->isAmdHsaOS()) 4900 return emitNonHSAIntrinsicError(DAG, DL, VT); 4901 4902 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4903 SI::KernelInputOffsets::GLOBAL_SIZE_X, 4, false); 4904 case Intrinsic::r600_read_global_size_y: 4905 if (Subtarget->isAmdHsaOS()) 4906 return emitNonHSAIntrinsicError(DAG, DL, VT); 4907 4908 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4909 SI::KernelInputOffsets::GLOBAL_SIZE_Y, 4, false); 4910 case Intrinsic::r600_read_global_size_z: 4911 if (Subtarget->isAmdHsaOS()) 4912 return emitNonHSAIntrinsicError(DAG, DL, VT); 4913 4914 return lowerKernargMemParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4915 SI::KernelInputOffsets::GLOBAL_SIZE_Z, 4, false); 4916 case Intrinsic::r600_read_local_size_x: 4917 if (Subtarget->isAmdHsaOS()) 4918 return emitNonHSAIntrinsicError(DAG, DL, VT); 4919 4920 return lowerImplicitZextParam(DAG, Op, MVT::i16, 4921 SI::KernelInputOffsets::LOCAL_SIZE_X); 4922 case Intrinsic::r600_read_local_size_y: 4923 if (Subtarget->isAmdHsaOS()) 4924 return emitNonHSAIntrinsicError(DAG, DL, VT); 4925 4926 return lowerImplicitZextParam(DAG, Op, MVT::i16, 4927 SI::KernelInputOffsets::LOCAL_SIZE_Y); 4928 case Intrinsic::r600_read_local_size_z: 4929 if (Subtarget->isAmdHsaOS()) 4930 return emitNonHSAIntrinsicError(DAG, DL, VT); 4931 4932 return lowerImplicitZextParam(DAG, Op, MVT::i16, 4933 SI::KernelInputOffsets::LOCAL_SIZE_Z); 4934 case Intrinsic::amdgcn_workgroup_id_x: 4935 case Intrinsic::r600_read_tgid_x: 4936 return getPreloadedValue(DAG, *MFI, VT, 4937 AMDGPUFunctionArgInfo::WORKGROUP_ID_X); 4938 case Intrinsic::amdgcn_workgroup_id_y: 4939 case Intrinsic::r600_read_tgid_y: 4940 return getPreloadedValue(DAG, *MFI, VT, 4941 AMDGPUFunctionArgInfo::WORKGROUP_ID_Y); 4942 case Intrinsic::amdgcn_workgroup_id_z: 4943 case Intrinsic::r600_read_tgid_z: 4944 return getPreloadedValue(DAG, *MFI, VT, 4945 AMDGPUFunctionArgInfo::WORKGROUP_ID_Z); 4946 case Intrinsic::amdgcn_workitem_id_x: { 4947 case Intrinsic::r600_read_tidig_x: 4948 return loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32, 4949 SDLoc(DAG.getEntryNode()), 4950 MFI->getArgInfo().WorkItemIDX); 4951 } 4952 case Intrinsic::amdgcn_workitem_id_y: 4953 case Intrinsic::r600_read_tidig_y: 4954 return loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32, 4955 SDLoc(DAG.getEntryNode()), 4956 MFI->getArgInfo().WorkItemIDY); 4957 case Intrinsic::amdgcn_workitem_id_z: 4958 case Intrinsic::r600_read_tidig_z: 4959 return loadInputValue(DAG, &AMDGPU::VGPR_32RegClass, MVT::i32, 4960 SDLoc(DAG.getEntryNode()), 4961 MFI->getArgInfo().WorkItemIDZ); 4962 case AMDGPUIntrinsic::SI_load_const: { 4963 SDValue Ops[] = { 4964 Op.getOperand(1), // Ptr 4965 Op.getOperand(2), // Offset 4966 DAG.getTargetConstant(0, DL, MVT::i1) // glc 4967 }; 4968 4969 MachineMemOperand *MMO = MF.getMachineMemOperand( 4970 MachinePointerInfo(), 4971 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable | 4972 MachineMemOperand::MOInvariant, 4973 VT.getStoreSize(), 4); 4974 SDVTList VTList = DAG.getVTList(MVT::i32); 4975 SDValue Load = DAG.getMemIntrinsicNode(AMDGPUISD::SBUFFER_LOAD, DL, 4976 VTList, Ops, MVT::i32, MMO); 4977 4978 return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Load); 4979 } 4980 case Intrinsic::amdgcn_s_buffer_load: { 4981 unsigned Cache = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue(); 4982 SDValue Ops[] = { 4983 Op.getOperand(1), // Ptr 4984 Op.getOperand(2), // Offset 4985 DAG.getTargetConstant(Cache & 1, DL, MVT::i1) // glc 4986 }; 4987 4988 MachineMemOperand *MMO = MF.getMachineMemOperand( 4989 MachinePointerInfo(), 4990 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable | 4991 MachineMemOperand::MOInvariant, 4992 VT.getStoreSize(), VT.getStoreSize()); 4993 return DAG.getMemIntrinsicNode(AMDGPUISD::SBUFFER_LOAD, DL, 4994 Op->getVTList(), Ops, VT, MMO); 4995 } 4996 case Intrinsic::amdgcn_fdiv_fast: 4997 return lowerFDIV_FAST(Op, DAG); 4998 case Intrinsic::amdgcn_interp_mov: { 4999 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4)); 5000 SDValue Glue = M0.getValue(1); 5001 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32, Op.getOperand(1), 5002 Op.getOperand(2), Op.getOperand(3), Glue); 5003 } 5004 case Intrinsic::amdgcn_interp_p1: { 5005 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4)); 5006 SDValue Glue = M0.getValue(1); 5007 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1), 5008 Op.getOperand(2), Op.getOperand(3), Glue); 5009 } 5010 case Intrinsic::amdgcn_interp_p2: { 5011 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5)); 5012 SDValue Glue = SDValue(M0.getNode(), 1); 5013 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1), 5014 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4), 5015 Glue); 5016 } 5017 case Intrinsic::amdgcn_sin: 5018 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1)); 5019 5020 case Intrinsic::amdgcn_cos: 5021 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1)); 5022 5023 case Intrinsic::amdgcn_log_clamp: { 5024 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) 5025 return SDValue(); 5026 5027 DiagnosticInfoUnsupported BadIntrin( 5028 MF.getFunction(), "intrinsic not supported on subtarget", 5029 DL.getDebugLoc()); 5030 DAG.getContext()->diagnose(BadIntrin); 5031 return DAG.getUNDEF(VT); 5032 } 5033 case Intrinsic::amdgcn_ldexp: 5034 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT, 5035 Op.getOperand(1), Op.getOperand(2)); 5036 5037 case Intrinsic::amdgcn_fract: 5038 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1)); 5039 5040 case Intrinsic::amdgcn_class: 5041 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT, 5042 Op.getOperand(1), Op.getOperand(2)); 5043 case Intrinsic::amdgcn_div_fmas: 5044 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT, 5045 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), 5046 Op.getOperand(4)); 5047 5048 case Intrinsic::amdgcn_div_fixup: 5049 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT, 5050 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 5051 5052 case Intrinsic::amdgcn_trig_preop: 5053 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT, 5054 Op.getOperand(1), Op.getOperand(2)); 5055 case Intrinsic::amdgcn_div_scale: { 5056 // 3rd parameter required to be a constant. 5057 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 5058 if (!Param) 5059 return DAG.getMergeValues({ DAG.getUNDEF(VT), DAG.getUNDEF(MVT::i1) }, DL); 5060 5061 // Translate to the operands expected by the machine instruction. The 5062 // first parameter must be the same as the first instruction. 5063 SDValue Numerator = Op.getOperand(1); 5064 SDValue Denominator = Op.getOperand(2); 5065 5066 // Note this order is opposite of the machine instruction's operations, 5067 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The 5068 // intrinsic has the numerator as the first operand to match a normal 5069 // division operation. 5070 5071 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator; 5072 5073 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0, 5074 Denominator, Numerator); 5075 } 5076 case Intrinsic::amdgcn_icmp: { 5077 return lowerICMPIntrinsic(*this, Op.getNode(), DAG); 5078 } 5079 case Intrinsic::amdgcn_fcmp: { 5080 return lowerFCMPIntrinsic(*this, Op.getNode(), DAG); 5081 } 5082 case Intrinsic::amdgcn_fmed3: 5083 return DAG.getNode(AMDGPUISD::FMED3, DL, VT, 5084 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 5085 case Intrinsic::amdgcn_fdot2: 5086 return DAG.getNode(AMDGPUISD::FDOT2, DL, VT, 5087 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), 5088 Op.getOperand(4)); 5089 case Intrinsic::amdgcn_fmul_legacy: 5090 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT, 5091 Op.getOperand(1), Op.getOperand(2)); 5092 case Intrinsic::amdgcn_sffbh: 5093 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1)); 5094 case Intrinsic::amdgcn_sbfe: 5095 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT, 5096 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 5097 case Intrinsic::amdgcn_ubfe: 5098 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT, 5099 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 5100 case Intrinsic::amdgcn_cvt_pkrtz: 5101 case Intrinsic::amdgcn_cvt_pknorm_i16: 5102 case Intrinsic::amdgcn_cvt_pknorm_u16: 5103 case Intrinsic::amdgcn_cvt_pk_i16: 5104 case Intrinsic::amdgcn_cvt_pk_u16: { 5105 // FIXME: Stop adding cast if v2f16/v2i16 are legal. 5106 EVT VT = Op.getValueType(); 5107 unsigned Opcode; 5108 5109 if (IntrinsicID == Intrinsic::amdgcn_cvt_pkrtz) 5110 Opcode = AMDGPUISD::CVT_PKRTZ_F16_F32; 5111 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pknorm_i16) 5112 Opcode = AMDGPUISD::CVT_PKNORM_I16_F32; 5113 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pknorm_u16) 5114 Opcode = AMDGPUISD::CVT_PKNORM_U16_F32; 5115 else if (IntrinsicID == Intrinsic::amdgcn_cvt_pk_i16) 5116 Opcode = AMDGPUISD::CVT_PK_I16_I32; 5117 else 5118 Opcode = AMDGPUISD::CVT_PK_U16_U32; 5119 5120 if (isTypeLegal(VT)) 5121 return DAG.getNode(Opcode, DL, VT, Op.getOperand(1), Op.getOperand(2)); 5122 5123 SDValue Node = DAG.getNode(Opcode, DL, MVT::i32, 5124 Op.getOperand(1), Op.getOperand(2)); 5125 return DAG.getNode(ISD::BITCAST, DL, VT, Node); 5126 } 5127 case Intrinsic::amdgcn_wqm: { 5128 SDValue Src = Op.getOperand(1); 5129 return SDValue(DAG.getMachineNode(AMDGPU::WQM, DL, Src.getValueType(), Src), 5130 0); 5131 } 5132 case Intrinsic::amdgcn_wwm: { 5133 SDValue Src = Op.getOperand(1); 5134 return SDValue(DAG.getMachineNode(AMDGPU::WWM, DL, Src.getValueType(), Src), 5135 0); 5136 } 5137 case Intrinsic::amdgcn_fmad_ftz: 5138 return DAG.getNode(AMDGPUISD::FMAD_FTZ, DL, VT, Op.getOperand(1), 5139 Op.getOperand(2), Op.getOperand(3)); 5140 default: 5141 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr = 5142 AMDGPU::getImageDimIntrinsicInfo(IntrinsicID)) 5143 return lowerImage(Op, ImageDimIntr, DAG); 5144 5145 return Op; 5146 } 5147 } 5148 5149 SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, 5150 SelectionDAG &DAG) const { 5151 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 5152 SDLoc DL(Op); 5153 5154 switch (IntrID) { 5155 case Intrinsic::amdgcn_atomic_inc: 5156 case Intrinsic::amdgcn_atomic_dec: 5157 case Intrinsic::amdgcn_ds_fadd: 5158 case Intrinsic::amdgcn_ds_fmin: 5159 case Intrinsic::amdgcn_ds_fmax: { 5160 MemSDNode *M = cast<MemSDNode>(Op); 5161 unsigned Opc; 5162 switch (IntrID) { 5163 case Intrinsic::amdgcn_atomic_inc: 5164 Opc = AMDGPUISD::ATOMIC_INC; 5165 break; 5166 case Intrinsic::amdgcn_atomic_dec: 5167 Opc = AMDGPUISD::ATOMIC_DEC; 5168 break; 5169 case Intrinsic::amdgcn_ds_fadd: 5170 Opc = AMDGPUISD::ATOMIC_LOAD_FADD; 5171 break; 5172 case Intrinsic::amdgcn_ds_fmin: 5173 Opc = AMDGPUISD::ATOMIC_LOAD_FMIN; 5174 break; 5175 case Intrinsic::amdgcn_ds_fmax: 5176 Opc = AMDGPUISD::ATOMIC_LOAD_FMAX; 5177 break; 5178 default: 5179 llvm_unreachable("Unknown intrinsic!"); 5180 } 5181 SDValue Ops[] = { 5182 M->getOperand(0), // Chain 5183 M->getOperand(2), // Ptr 5184 M->getOperand(3) // Value 5185 }; 5186 5187 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops, 5188 M->getMemoryVT(), M->getMemOperand()); 5189 } 5190 case Intrinsic::amdgcn_buffer_load: 5191 case Intrinsic::amdgcn_buffer_load_format: { 5192 unsigned Glc = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue(); 5193 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(6))->getZExtValue(); 5194 unsigned IdxEn = 1; 5195 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(3))) 5196 IdxEn = Idx->getZExtValue() != 0; 5197 SDValue Ops[] = { 5198 Op.getOperand(0), // Chain 5199 Op.getOperand(2), // rsrc 5200 Op.getOperand(3), // vindex 5201 SDValue(), // voffset -- will be set by setBufferOffsets 5202 SDValue(), // soffset -- will be set by setBufferOffsets 5203 SDValue(), // offset -- will be set by setBufferOffsets 5204 DAG.getConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy 5205 DAG.getConstant(IdxEn, DL, MVT::i1), // idxen 5206 }; 5207 5208 setBufferOffsets(Op.getOperand(4), DAG, &Ops[3]); 5209 unsigned Opc = (IntrID == Intrinsic::amdgcn_buffer_load) ? 5210 AMDGPUISD::BUFFER_LOAD : AMDGPUISD::BUFFER_LOAD_FORMAT; 5211 5212 EVT VT = Op.getValueType(); 5213 EVT IntVT = VT.changeTypeToInteger(); 5214 auto *M = cast<MemSDNode>(Op); 5215 EVT LoadVT = Op.getValueType(); 5216 5217 if (LoadVT.getScalarType() == MVT::f16) 5218 return adjustLoadValueType(AMDGPUISD::BUFFER_LOAD_FORMAT_D16, 5219 M, DAG, Ops); 5220 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, IntVT, 5221 M->getMemOperand()); 5222 } 5223 case Intrinsic::amdgcn_raw_buffer_load: 5224 case Intrinsic::amdgcn_raw_buffer_load_format: { 5225 auto Offsets = splitBufferOffsets(Op.getOperand(3), DAG); 5226 SDValue Ops[] = { 5227 Op.getOperand(0), // Chain 5228 Op.getOperand(2), // rsrc 5229 DAG.getConstant(0, DL, MVT::i32), // vindex 5230 Offsets.first, // voffset 5231 Op.getOperand(4), // soffset 5232 Offsets.second, // offset 5233 Op.getOperand(5), // cachepolicy 5234 DAG.getConstant(0, DL, MVT::i1), // idxen 5235 }; 5236 5237 unsigned Opc = (IntrID == Intrinsic::amdgcn_raw_buffer_load) ? 5238 AMDGPUISD::BUFFER_LOAD : AMDGPUISD::BUFFER_LOAD_FORMAT; 5239 5240 EVT VT = Op.getValueType(); 5241 EVT IntVT = VT.changeTypeToInteger(); 5242 auto *M = cast<MemSDNode>(Op); 5243 EVT LoadVT = Op.getValueType(); 5244 5245 if (LoadVT.getScalarType() == MVT::f16) 5246 return adjustLoadValueType(AMDGPUISD::BUFFER_LOAD_FORMAT_D16, 5247 M, DAG, Ops); 5248 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, IntVT, 5249 M->getMemOperand()); 5250 } 5251 case Intrinsic::amdgcn_struct_buffer_load: 5252 case Intrinsic::amdgcn_struct_buffer_load_format: { 5253 auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG); 5254 SDValue Ops[] = { 5255 Op.getOperand(0), // Chain 5256 Op.getOperand(2), // rsrc 5257 Op.getOperand(3), // vindex 5258 Offsets.first, // voffset 5259 Op.getOperand(5), // soffset 5260 Offsets.second, // offset 5261 Op.getOperand(6), // cachepolicy 5262 DAG.getConstant(1, DL, MVT::i1), // idxen 5263 }; 5264 5265 unsigned Opc = (IntrID == Intrinsic::amdgcn_struct_buffer_load) ? 5266 AMDGPUISD::BUFFER_LOAD : AMDGPUISD::BUFFER_LOAD_FORMAT; 5267 5268 EVT VT = Op.getValueType(); 5269 EVT IntVT = VT.changeTypeToInteger(); 5270 auto *M = cast<MemSDNode>(Op); 5271 EVT LoadVT = Op.getValueType(); 5272 5273 if (LoadVT.getScalarType() == MVT::f16) 5274 return adjustLoadValueType(AMDGPUISD::BUFFER_LOAD_FORMAT_D16, 5275 M, DAG, Ops); 5276 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, IntVT, 5277 M->getMemOperand()); 5278 } 5279 case Intrinsic::amdgcn_tbuffer_load: { 5280 MemSDNode *M = cast<MemSDNode>(Op); 5281 EVT LoadVT = Op.getValueType(); 5282 5283 unsigned Dfmt = cast<ConstantSDNode>(Op.getOperand(7))->getZExtValue(); 5284 unsigned Nfmt = cast<ConstantSDNode>(Op.getOperand(8))->getZExtValue(); 5285 unsigned Glc = cast<ConstantSDNode>(Op.getOperand(9))->getZExtValue(); 5286 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(10))->getZExtValue(); 5287 unsigned IdxEn = 1; 5288 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(3))) 5289 IdxEn = Idx->getZExtValue() != 0; 5290 SDValue Ops[] = { 5291 Op.getOperand(0), // Chain 5292 Op.getOperand(2), // rsrc 5293 Op.getOperand(3), // vindex 5294 Op.getOperand(4), // voffset 5295 Op.getOperand(5), // soffset 5296 Op.getOperand(6), // offset 5297 DAG.getConstant(Dfmt | (Nfmt << 4), DL, MVT::i32), // format 5298 DAG.getConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy 5299 DAG.getConstant(IdxEn, DL, MVT::i1), // idxen 5300 }; 5301 5302 if (LoadVT.getScalarType() == MVT::f16) 5303 return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16, 5304 M, DAG, Ops); 5305 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL, 5306 Op->getVTList(), Ops, LoadVT, 5307 M->getMemOperand()); 5308 } 5309 case Intrinsic::amdgcn_raw_tbuffer_load: { 5310 MemSDNode *M = cast<MemSDNode>(Op); 5311 EVT LoadVT = Op.getValueType(); 5312 auto Offsets = splitBufferOffsets(Op.getOperand(3), DAG); 5313 5314 SDValue Ops[] = { 5315 Op.getOperand(0), // Chain 5316 Op.getOperand(2), // rsrc 5317 DAG.getConstant(0, DL, MVT::i32), // vindex 5318 Offsets.first, // voffset 5319 Op.getOperand(4), // soffset 5320 Offsets.second, // offset 5321 Op.getOperand(5), // format 5322 Op.getOperand(6), // cachepolicy 5323 DAG.getConstant(0, DL, MVT::i1), // idxen 5324 }; 5325 5326 if (LoadVT.getScalarType() == MVT::f16) 5327 return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16, 5328 M, DAG, Ops); 5329 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL, 5330 Op->getVTList(), Ops, LoadVT, 5331 M->getMemOperand()); 5332 } 5333 case Intrinsic::amdgcn_struct_tbuffer_load: { 5334 MemSDNode *M = cast<MemSDNode>(Op); 5335 EVT LoadVT = Op.getValueType(); 5336 auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG); 5337 5338 SDValue Ops[] = { 5339 Op.getOperand(0), // Chain 5340 Op.getOperand(2), // rsrc 5341 Op.getOperand(3), // vindex 5342 Offsets.first, // voffset 5343 Op.getOperand(5), // soffset 5344 Offsets.second, // offset 5345 Op.getOperand(6), // format 5346 Op.getOperand(7), // cachepolicy 5347 DAG.getConstant(1, DL, MVT::i1), // idxen 5348 }; 5349 5350 if (LoadVT.getScalarType() == MVT::f16) 5351 return adjustLoadValueType(AMDGPUISD::TBUFFER_LOAD_FORMAT_D16, 5352 M, DAG, Ops); 5353 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_LOAD_FORMAT, DL, 5354 Op->getVTList(), Ops, LoadVT, 5355 M->getMemOperand()); 5356 } 5357 case Intrinsic::amdgcn_buffer_atomic_swap: 5358 case Intrinsic::amdgcn_buffer_atomic_add: 5359 case Intrinsic::amdgcn_buffer_atomic_sub: 5360 case Intrinsic::amdgcn_buffer_atomic_smin: 5361 case Intrinsic::amdgcn_buffer_atomic_umin: 5362 case Intrinsic::amdgcn_buffer_atomic_smax: 5363 case Intrinsic::amdgcn_buffer_atomic_umax: 5364 case Intrinsic::amdgcn_buffer_atomic_and: 5365 case Intrinsic::amdgcn_buffer_atomic_or: 5366 case Intrinsic::amdgcn_buffer_atomic_xor: { 5367 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(6))->getZExtValue(); 5368 unsigned IdxEn = 1; 5369 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(4))) 5370 IdxEn = Idx->getZExtValue() != 0; 5371 SDValue Ops[] = { 5372 Op.getOperand(0), // Chain 5373 Op.getOperand(2), // vdata 5374 Op.getOperand(3), // rsrc 5375 Op.getOperand(4), // vindex 5376 SDValue(), // voffset -- will be set by setBufferOffsets 5377 SDValue(), // soffset -- will be set by setBufferOffsets 5378 SDValue(), // offset -- will be set by setBufferOffsets 5379 DAG.getConstant(Slc << 1, DL, MVT::i32), // cachepolicy 5380 DAG.getConstant(IdxEn, DL, MVT::i1), // idxen 5381 }; 5382 setBufferOffsets(Op.getOperand(5), DAG, &Ops[4]); 5383 EVT VT = Op.getValueType(); 5384 5385 auto *M = cast<MemSDNode>(Op); 5386 unsigned Opcode = 0; 5387 5388 switch (IntrID) { 5389 case Intrinsic::amdgcn_buffer_atomic_swap: 5390 Opcode = AMDGPUISD::BUFFER_ATOMIC_SWAP; 5391 break; 5392 case Intrinsic::amdgcn_buffer_atomic_add: 5393 Opcode = AMDGPUISD::BUFFER_ATOMIC_ADD; 5394 break; 5395 case Intrinsic::amdgcn_buffer_atomic_sub: 5396 Opcode = AMDGPUISD::BUFFER_ATOMIC_SUB; 5397 break; 5398 case Intrinsic::amdgcn_buffer_atomic_smin: 5399 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMIN; 5400 break; 5401 case Intrinsic::amdgcn_buffer_atomic_umin: 5402 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMIN; 5403 break; 5404 case Intrinsic::amdgcn_buffer_atomic_smax: 5405 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMAX; 5406 break; 5407 case Intrinsic::amdgcn_buffer_atomic_umax: 5408 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMAX; 5409 break; 5410 case Intrinsic::amdgcn_buffer_atomic_and: 5411 Opcode = AMDGPUISD::BUFFER_ATOMIC_AND; 5412 break; 5413 case Intrinsic::amdgcn_buffer_atomic_or: 5414 Opcode = AMDGPUISD::BUFFER_ATOMIC_OR; 5415 break; 5416 case Intrinsic::amdgcn_buffer_atomic_xor: 5417 Opcode = AMDGPUISD::BUFFER_ATOMIC_XOR; 5418 break; 5419 default: 5420 llvm_unreachable("unhandled atomic opcode"); 5421 } 5422 5423 return DAG.getMemIntrinsicNode(Opcode, DL, Op->getVTList(), Ops, VT, 5424 M->getMemOperand()); 5425 } 5426 case Intrinsic::amdgcn_raw_buffer_atomic_swap: 5427 case Intrinsic::amdgcn_raw_buffer_atomic_add: 5428 case Intrinsic::amdgcn_raw_buffer_atomic_sub: 5429 case Intrinsic::amdgcn_raw_buffer_atomic_smin: 5430 case Intrinsic::amdgcn_raw_buffer_atomic_umin: 5431 case Intrinsic::amdgcn_raw_buffer_atomic_smax: 5432 case Intrinsic::amdgcn_raw_buffer_atomic_umax: 5433 case Intrinsic::amdgcn_raw_buffer_atomic_and: 5434 case Intrinsic::amdgcn_raw_buffer_atomic_or: 5435 case Intrinsic::amdgcn_raw_buffer_atomic_xor: { 5436 auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG); 5437 SDValue Ops[] = { 5438 Op.getOperand(0), // Chain 5439 Op.getOperand(2), // vdata 5440 Op.getOperand(3), // rsrc 5441 DAG.getConstant(0, DL, MVT::i32), // vindex 5442 Offsets.first, // voffset 5443 Op.getOperand(5), // soffset 5444 Offsets.second, // offset 5445 Op.getOperand(6), // cachepolicy 5446 DAG.getConstant(0, DL, MVT::i1), // idxen 5447 }; 5448 EVT VT = Op.getValueType(); 5449 5450 auto *M = cast<MemSDNode>(Op); 5451 unsigned Opcode = 0; 5452 5453 switch (IntrID) { 5454 case Intrinsic::amdgcn_raw_buffer_atomic_swap: 5455 Opcode = AMDGPUISD::BUFFER_ATOMIC_SWAP; 5456 break; 5457 case Intrinsic::amdgcn_raw_buffer_atomic_add: 5458 Opcode = AMDGPUISD::BUFFER_ATOMIC_ADD; 5459 break; 5460 case Intrinsic::amdgcn_raw_buffer_atomic_sub: 5461 Opcode = AMDGPUISD::BUFFER_ATOMIC_SUB; 5462 break; 5463 case Intrinsic::amdgcn_raw_buffer_atomic_smin: 5464 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMIN; 5465 break; 5466 case Intrinsic::amdgcn_raw_buffer_atomic_umin: 5467 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMIN; 5468 break; 5469 case Intrinsic::amdgcn_raw_buffer_atomic_smax: 5470 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMAX; 5471 break; 5472 case Intrinsic::amdgcn_raw_buffer_atomic_umax: 5473 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMAX; 5474 break; 5475 case Intrinsic::amdgcn_raw_buffer_atomic_and: 5476 Opcode = AMDGPUISD::BUFFER_ATOMIC_AND; 5477 break; 5478 case Intrinsic::amdgcn_raw_buffer_atomic_or: 5479 Opcode = AMDGPUISD::BUFFER_ATOMIC_OR; 5480 break; 5481 case Intrinsic::amdgcn_raw_buffer_atomic_xor: 5482 Opcode = AMDGPUISD::BUFFER_ATOMIC_XOR; 5483 break; 5484 default: 5485 llvm_unreachable("unhandled atomic opcode"); 5486 } 5487 5488 return DAG.getMemIntrinsicNode(Opcode, DL, Op->getVTList(), Ops, VT, 5489 M->getMemOperand()); 5490 } 5491 case Intrinsic::amdgcn_struct_buffer_atomic_swap: 5492 case Intrinsic::amdgcn_struct_buffer_atomic_add: 5493 case Intrinsic::amdgcn_struct_buffer_atomic_sub: 5494 case Intrinsic::amdgcn_struct_buffer_atomic_smin: 5495 case Intrinsic::amdgcn_struct_buffer_atomic_umin: 5496 case Intrinsic::amdgcn_struct_buffer_atomic_smax: 5497 case Intrinsic::amdgcn_struct_buffer_atomic_umax: 5498 case Intrinsic::amdgcn_struct_buffer_atomic_and: 5499 case Intrinsic::amdgcn_struct_buffer_atomic_or: 5500 case Intrinsic::amdgcn_struct_buffer_atomic_xor: { 5501 auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG); 5502 SDValue Ops[] = { 5503 Op.getOperand(0), // Chain 5504 Op.getOperand(2), // vdata 5505 Op.getOperand(3), // rsrc 5506 Op.getOperand(4), // vindex 5507 Offsets.first, // voffset 5508 Op.getOperand(6), // soffset 5509 Offsets.second, // offset 5510 Op.getOperand(7), // cachepolicy 5511 DAG.getConstant(1, DL, MVT::i1), // idxen 5512 }; 5513 EVT VT = Op.getValueType(); 5514 5515 auto *M = cast<MemSDNode>(Op); 5516 unsigned Opcode = 0; 5517 5518 switch (IntrID) { 5519 case Intrinsic::amdgcn_struct_buffer_atomic_swap: 5520 Opcode = AMDGPUISD::BUFFER_ATOMIC_SWAP; 5521 break; 5522 case Intrinsic::amdgcn_struct_buffer_atomic_add: 5523 Opcode = AMDGPUISD::BUFFER_ATOMIC_ADD; 5524 break; 5525 case Intrinsic::amdgcn_struct_buffer_atomic_sub: 5526 Opcode = AMDGPUISD::BUFFER_ATOMIC_SUB; 5527 break; 5528 case Intrinsic::amdgcn_struct_buffer_atomic_smin: 5529 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMIN; 5530 break; 5531 case Intrinsic::amdgcn_struct_buffer_atomic_umin: 5532 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMIN; 5533 break; 5534 case Intrinsic::amdgcn_struct_buffer_atomic_smax: 5535 Opcode = AMDGPUISD::BUFFER_ATOMIC_SMAX; 5536 break; 5537 case Intrinsic::amdgcn_struct_buffer_atomic_umax: 5538 Opcode = AMDGPUISD::BUFFER_ATOMIC_UMAX; 5539 break; 5540 case Intrinsic::amdgcn_struct_buffer_atomic_and: 5541 Opcode = AMDGPUISD::BUFFER_ATOMIC_AND; 5542 break; 5543 case Intrinsic::amdgcn_struct_buffer_atomic_or: 5544 Opcode = AMDGPUISD::BUFFER_ATOMIC_OR; 5545 break; 5546 case Intrinsic::amdgcn_struct_buffer_atomic_xor: 5547 Opcode = AMDGPUISD::BUFFER_ATOMIC_XOR; 5548 break; 5549 default: 5550 llvm_unreachable("unhandled atomic opcode"); 5551 } 5552 5553 return DAG.getMemIntrinsicNode(Opcode, DL, Op->getVTList(), Ops, VT, 5554 M->getMemOperand()); 5555 } 5556 case Intrinsic::amdgcn_buffer_atomic_cmpswap: { 5557 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(7))->getZExtValue(); 5558 unsigned IdxEn = 1; 5559 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(5))) 5560 IdxEn = Idx->getZExtValue() != 0; 5561 SDValue Ops[] = { 5562 Op.getOperand(0), // Chain 5563 Op.getOperand(2), // src 5564 Op.getOperand(3), // cmp 5565 Op.getOperand(4), // rsrc 5566 Op.getOperand(5), // vindex 5567 SDValue(), // voffset -- will be set by setBufferOffsets 5568 SDValue(), // soffset -- will be set by setBufferOffsets 5569 SDValue(), // offset -- will be set by setBufferOffsets 5570 DAG.getConstant(Slc << 1, DL, MVT::i32), // cachepolicy 5571 DAG.getConstant(IdxEn, DL, MVT::i1), // idxen 5572 }; 5573 setBufferOffsets(Op.getOperand(6), DAG, &Ops[5]); 5574 EVT VT = Op.getValueType(); 5575 auto *M = cast<MemSDNode>(Op); 5576 5577 return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL, 5578 Op->getVTList(), Ops, VT, M->getMemOperand()); 5579 } 5580 case Intrinsic::amdgcn_raw_buffer_atomic_cmpswap: { 5581 auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG); 5582 SDValue Ops[] = { 5583 Op.getOperand(0), // Chain 5584 Op.getOperand(2), // src 5585 Op.getOperand(3), // cmp 5586 Op.getOperand(4), // rsrc 5587 DAG.getConstant(0, DL, MVT::i32), // vindex 5588 Offsets.first, // voffset 5589 Op.getOperand(6), // soffset 5590 Offsets.second, // offset 5591 Op.getOperand(7), // cachepolicy 5592 DAG.getConstant(0, DL, MVT::i1), // idxen 5593 }; 5594 EVT VT = Op.getValueType(); 5595 auto *M = cast<MemSDNode>(Op); 5596 5597 return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL, 5598 Op->getVTList(), Ops, VT, M->getMemOperand()); 5599 } 5600 case Intrinsic::amdgcn_struct_buffer_atomic_cmpswap: { 5601 auto Offsets = splitBufferOffsets(Op.getOperand(6), DAG); 5602 SDValue Ops[] = { 5603 Op.getOperand(0), // Chain 5604 Op.getOperand(2), // src 5605 Op.getOperand(3), // cmp 5606 Op.getOperand(4), // rsrc 5607 Op.getOperand(5), // vindex 5608 Offsets.first, // voffset 5609 Op.getOperand(7), // soffset 5610 Offsets.second, // offset 5611 Op.getOperand(8), // cachepolicy 5612 DAG.getConstant(1, DL, MVT::i1), // idxen 5613 }; 5614 EVT VT = Op.getValueType(); 5615 auto *M = cast<MemSDNode>(Op); 5616 5617 return DAG.getMemIntrinsicNode(AMDGPUISD::BUFFER_ATOMIC_CMPSWAP, DL, 5618 Op->getVTList(), Ops, VT, M->getMemOperand()); 5619 } 5620 5621 default: 5622 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr = 5623 AMDGPU::getImageDimIntrinsicInfo(IntrID)) 5624 return lowerImage(Op, ImageDimIntr, DAG); 5625 5626 return SDValue(); 5627 } 5628 } 5629 5630 SDValue SITargetLowering::handleD16VData(SDValue VData, 5631 SelectionDAG &DAG) const { 5632 EVT StoreVT = VData.getValueType(); 5633 5634 // No change for f16 and legal vector D16 types. 5635 if (!StoreVT.isVector()) 5636 return VData; 5637 5638 SDLoc DL(VData); 5639 assert((StoreVT.getVectorNumElements() != 3) && "Handle v3f16"); 5640 5641 if (Subtarget->hasUnpackedD16VMem()) { 5642 // We need to unpack the packed data to store. 5643 EVT IntStoreVT = StoreVT.changeTypeToInteger(); 5644 SDValue IntVData = DAG.getNode(ISD::BITCAST, DL, IntStoreVT, VData); 5645 5646 EVT EquivStoreVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, 5647 StoreVT.getVectorNumElements()); 5648 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, EquivStoreVT, IntVData); 5649 return DAG.UnrollVectorOp(ZExt.getNode()); 5650 } 5651 5652 assert(isTypeLegal(StoreVT)); 5653 return VData; 5654 } 5655 5656 SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op, 5657 SelectionDAG &DAG) const { 5658 SDLoc DL(Op); 5659 SDValue Chain = Op.getOperand(0); 5660 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 5661 MachineFunction &MF = DAG.getMachineFunction(); 5662 5663 switch (IntrinsicID) { 5664 case Intrinsic::amdgcn_exp: { 5665 const ConstantSDNode *Tgt = cast<ConstantSDNode>(Op.getOperand(2)); 5666 const ConstantSDNode *En = cast<ConstantSDNode>(Op.getOperand(3)); 5667 const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(8)); 5668 const ConstantSDNode *VM = cast<ConstantSDNode>(Op.getOperand(9)); 5669 5670 const SDValue Ops[] = { 5671 Chain, 5672 DAG.getTargetConstant(Tgt->getZExtValue(), DL, MVT::i8), // tgt 5673 DAG.getTargetConstant(En->getZExtValue(), DL, MVT::i8), // en 5674 Op.getOperand(4), // src0 5675 Op.getOperand(5), // src1 5676 Op.getOperand(6), // src2 5677 Op.getOperand(7), // src3 5678 DAG.getTargetConstant(0, DL, MVT::i1), // compr 5679 DAG.getTargetConstant(VM->getZExtValue(), DL, MVT::i1) 5680 }; 5681 5682 unsigned Opc = Done->isNullValue() ? 5683 AMDGPUISD::EXPORT : AMDGPUISD::EXPORT_DONE; 5684 return DAG.getNode(Opc, DL, Op->getVTList(), Ops); 5685 } 5686 case Intrinsic::amdgcn_exp_compr: { 5687 const ConstantSDNode *Tgt = cast<ConstantSDNode>(Op.getOperand(2)); 5688 const ConstantSDNode *En = cast<ConstantSDNode>(Op.getOperand(3)); 5689 SDValue Src0 = Op.getOperand(4); 5690 SDValue Src1 = Op.getOperand(5); 5691 const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(6)); 5692 const ConstantSDNode *VM = cast<ConstantSDNode>(Op.getOperand(7)); 5693 5694 SDValue Undef = DAG.getUNDEF(MVT::f32); 5695 const SDValue Ops[] = { 5696 Chain, 5697 DAG.getTargetConstant(Tgt->getZExtValue(), DL, MVT::i8), // tgt 5698 DAG.getTargetConstant(En->getZExtValue(), DL, MVT::i8), // en 5699 DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src0), 5700 DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src1), 5701 Undef, // src2 5702 Undef, // src3 5703 DAG.getTargetConstant(1, DL, MVT::i1), // compr 5704 DAG.getTargetConstant(VM->getZExtValue(), DL, MVT::i1) 5705 }; 5706 5707 unsigned Opc = Done->isNullValue() ? 5708 AMDGPUISD::EXPORT : AMDGPUISD::EXPORT_DONE; 5709 return DAG.getNode(Opc, DL, Op->getVTList(), Ops); 5710 } 5711 case Intrinsic::amdgcn_s_sendmsg: 5712 case Intrinsic::amdgcn_s_sendmsghalt: { 5713 unsigned NodeOp = (IntrinsicID == Intrinsic::amdgcn_s_sendmsg) ? 5714 AMDGPUISD::SENDMSG : AMDGPUISD::SENDMSGHALT; 5715 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3)); 5716 SDValue Glue = Chain.getValue(1); 5717 return DAG.getNode(NodeOp, DL, MVT::Other, Chain, 5718 Op.getOperand(2), Glue); 5719 } 5720 case Intrinsic::amdgcn_init_exec: { 5721 return DAG.getNode(AMDGPUISD::INIT_EXEC, DL, MVT::Other, Chain, 5722 Op.getOperand(2)); 5723 } 5724 case Intrinsic::amdgcn_init_exec_from_input: { 5725 return DAG.getNode(AMDGPUISD::INIT_EXEC_FROM_INPUT, DL, MVT::Other, Chain, 5726 Op.getOperand(2), Op.getOperand(3)); 5727 } 5728 case AMDGPUIntrinsic::AMDGPU_kill: { 5729 SDValue Src = Op.getOperand(2); 5730 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) { 5731 if (!K->isNegative()) 5732 return Chain; 5733 5734 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32); 5735 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne); 5736 } 5737 5738 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src); 5739 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast); 5740 } 5741 case Intrinsic::amdgcn_s_barrier: { 5742 if (getTargetMachine().getOptLevel() > CodeGenOpt::None) { 5743 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 5744 unsigned WGSize = ST.getFlatWorkGroupSizes(MF.getFunction()).second; 5745 if (WGSize <= ST.getWavefrontSize()) 5746 return SDValue(DAG.getMachineNode(AMDGPU::WAVE_BARRIER, DL, MVT::Other, 5747 Op.getOperand(0)), 0); 5748 } 5749 return SDValue(); 5750 }; 5751 case AMDGPUIntrinsic::SI_tbuffer_store: { 5752 5753 // Extract vindex and voffset from vaddr as appropriate 5754 const ConstantSDNode *OffEn = cast<ConstantSDNode>(Op.getOperand(10)); 5755 const ConstantSDNode *IdxEn = cast<ConstantSDNode>(Op.getOperand(11)); 5756 SDValue VAddr = Op.getOperand(5); 5757 5758 SDValue Zero = DAG.getTargetConstant(0, DL, MVT::i32); 5759 5760 assert(!(OffEn->isOne() && IdxEn->isOne()) && 5761 "Legacy intrinsic doesn't support both offset and index - use new version"); 5762 5763 SDValue VIndex = IdxEn->isOne() ? VAddr : Zero; 5764 SDValue VOffset = OffEn->isOne() ? VAddr : Zero; 5765 5766 // Deal with the vec-3 case 5767 const ConstantSDNode *NumChannels = cast<ConstantSDNode>(Op.getOperand(4)); 5768 auto Opcode = NumChannels->getZExtValue() == 3 ? 5769 AMDGPUISD::TBUFFER_STORE_FORMAT_X3 : AMDGPUISD::TBUFFER_STORE_FORMAT; 5770 5771 unsigned Dfmt = cast<ConstantSDNode>(Op.getOperand(8))->getZExtValue(); 5772 unsigned Nfmt = cast<ConstantSDNode>(Op.getOperand(9))->getZExtValue(); 5773 unsigned Glc = cast<ConstantSDNode>(Op.getOperand(12))->getZExtValue(); 5774 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(13))->getZExtValue(); 5775 SDValue Ops[] = { 5776 Chain, 5777 Op.getOperand(3), // vdata 5778 Op.getOperand(2), // rsrc 5779 VIndex, 5780 VOffset, 5781 Op.getOperand(6), // soffset 5782 Op.getOperand(7), // inst_offset 5783 DAG.getConstant(Dfmt | (Nfmt << 4), DL, MVT::i32), // format 5784 DAG.getConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy 5785 DAG.getConstant(IdxEn->isOne(), DL, MVT::i1), // idxen 5786 }; 5787 5788 assert((cast<ConstantSDNode>(Op.getOperand(14)))->getZExtValue() == 0 && 5789 "Value of tfe other than zero is unsupported"); 5790 5791 EVT VT = Op.getOperand(3).getValueType(); 5792 MachineMemOperand *MMO = MF.getMachineMemOperand( 5793 MachinePointerInfo(), 5794 MachineMemOperand::MOStore, 5795 VT.getStoreSize(), 4); 5796 return DAG.getMemIntrinsicNode(Opcode, DL, 5797 Op->getVTList(), Ops, VT, MMO); 5798 } 5799 5800 case Intrinsic::amdgcn_tbuffer_store: { 5801 SDValue VData = Op.getOperand(2); 5802 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16); 5803 if (IsD16) 5804 VData = handleD16VData(VData, DAG); 5805 unsigned Dfmt = cast<ConstantSDNode>(Op.getOperand(8))->getZExtValue(); 5806 unsigned Nfmt = cast<ConstantSDNode>(Op.getOperand(9))->getZExtValue(); 5807 unsigned Glc = cast<ConstantSDNode>(Op.getOperand(10))->getZExtValue(); 5808 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(11))->getZExtValue(); 5809 unsigned IdxEn = 1; 5810 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(4))) 5811 IdxEn = Idx->getZExtValue() != 0; 5812 SDValue Ops[] = { 5813 Chain, 5814 VData, // vdata 5815 Op.getOperand(3), // rsrc 5816 Op.getOperand(4), // vindex 5817 Op.getOperand(5), // voffset 5818 Op.getOperand(6), // soffset 5819 Op.getOperand(7), // offset 5820 DAG.getConstant(Dfmt | (Nfmt << 4), DL, MVT::i32), // format 5821 DAG.getConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy 5822 DAG.getConstant(IdxEn, DL, MVT::i1), // idexen 5823 }; 5824 unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16 : 5825 AMDGPUISD::TBUFFER_STORE_FORMAT; 5826 MemSDNode *M = cast<MemSDNode>(Op); 5827 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 5828 M->getMemoryVT(), M->getMemOperand()); 5829 } 5830 5831 case Intrinsic::amdgcn_struct_tbuffer_store: { 5832 SDValue VData = Op.getOperand(2); 5833 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16); 5834 if (IsD16) 5835 VData = handleD16VData(VData, DAG); 5836 auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG); 5837 SDValue Ops[] = { 5838 Chain, 5839 VData, // vdata 5840 Op.getOperand(3), // rsrc 5841 Op.getOperand(4), // vindex 5842 Offsets.first, // voffset 5843 Op.getOperand(6), // soffset 5844 Offsets.second, // offset 5845 Op.getOperand(7), // format 5846 Op.getOperand(8), // cachepolicy 5847 DAG.getConstant(1, DL, MVT::i1), // idexen 5848 }; 5849 unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16 : 5850 AMDGPUISD::TBUFFER_STORE_FORMAT; 5851 MemSDNode *M = cast<MemSDNode>(Op); 5852 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 5853 M->getMemoryVT(), M->getMemOperand()); 5854 } 5855 5856 case Intrinsic::amdgcn_raw_tbuffer_store: { 5857 SDValue VData = Op.getOperand(2); 5858 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16); 5859 if (IsD16) 5860 VData = handleD16VData(VData, DAG); 5861 auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG); 5862 SDValue Ops[] = { 5863 Chain, 5864 VData, // vdata 5865 Op.getOperand(3), // rsrc 5866 DAG.getConstant(0, DL, MVT::i32), // vindex 5867 Offsets.first, // voffset 5868 Op.getOperand(5), // soffset 5869 Offsets.second, // offset 5870 Op.getOperand(6), // format 5871 Op.getOperand(7), // cachepolicy 5872 DAG.getConstant(0, DL, MVT::i1), // idexen 5873 }; 5874 unsigned Opc = IsD16 ? AMDGPUISD::TBUFFER_STORE_FORMAT_D16 : 5875 AMDGPUISD::TBUFFER_STORE_FORMAT; 5876 MemSDNode *M = cast<MemSDNode>(Op); 5877 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 5878 M->getMemoryVT(), M->getMemOperand()); 5879 } 5880 5881 case Intrinsic::amdgcn_buffer_store: 5882 case Intrinsic::amdgcn_buffer_store_format: { 5883 SDValue VData = Op.getOperand(2); 5884 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16); 5885 if (IsD16) 5886 VData = handleD16VData(VData, DAG); 5887 unsigned Glc = cast<ConstantSDNode>(Op.getOperand(6))->getZExtValue(); 5888 unsigned Slc = cast<ConstantSDNode>(Op.getOperand(7))->getZExtValue(); 5889 unsigned IdxEn = 1; 5890 if (auto Idx = dyn_cast<ConstantSDNode>(Op.getOperand(4))) 5891 IdxEn = Idx->getZExtValue() != 0; 5892 SDValue Ops[] = { 5893 Chain, 5894 VData, 5895 Op.getOperand(3), // rsrc 5896 Op.getOperand(4), // vindex 5897 SDValue(), // voffset -- will be set by setBufferOffsets 5898 SDValue(), // soffset -- will be set by setBufferOffsets 5899 SDValue(), // offset -- will be set by setBufferOffsets 5900 DAG.getConstant(Glc | (Slc << 1), DL, MVT::i32), // cachepolicy 5901 DAG.getConstant(IdxEn, DL, MVT::i1), // idxen 5902 }; 5903 setBufferOffsets(Op.getOperand(5), DAG, &Ops[4]); 5904 unsigned Opc = IntrinsicID == Intrinsic::amdgcn_buffer_store ? 5905 AMDGPUISD::BUFFER_STORE : AMDGPUISD::BUFFER_STORE_FORMAT; 5906 Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc; 5907 MemSDNode *M = cast<MemSDNode>(Op); 5908 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 5909 M->getMemoryVT(), M->getMemOperand()); 5910 } 5911 5912 case Intrinsic::amdgcn_raw_buffer_store: 5913 case Intrinsic::amdgcn_raw_buffer_store_format: { 5914 SDValue VData = Op.getOperand(2); 5915 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16); 5916 if (IsD16) 5917 VData = handleD16VData(VData, DAG); 5918 auto Offsets = splitBufferOffsets(Op.getOperand(4), DAG); 5919 SDValue Ops[] = { 5920 Chain, 5921 VData, 5922 Op.getOperand(3), // rsrc 5923 DAG.getConstant(0, DL, MVT::i32), // vindex 5924 Offsets.first, // voffset 5925 Op.getOperand(5), // soffset 5926 Offsets.second, // offset 5927 Op.getOperand(6), // cachepolicy 5928 DAG.getConstant(0, DL, MVT::i1), // idxen 5929 }; 5930 unsigned Opc = IntrinsicID == Intrinsic::amdgcn_raw_buffer_store ? 5931 AMDGPUISD::BUFFER_STORE : AMDGPUISD::BUFFER_STORE_FORMAT; 5932 Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc; 5933 MemSDNode *M = cast<MemSDNode>(Op); 5934 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 5935 M->getMemoryVT(), M->getMemOperand()); 5936 } 5937 5938 case Intrinsic::amdgcn_struct_buffer_store: 5939 case Intrinsic::amdgcn_struct_buffer_store_format: { 5940 SDValue VData = Op.getOperand(2); 5941 bool IsD16 = (VData.getValueType().getScalarType() == MVT::f16); 5942 if (IsD16) 5943 VData = handleD16VData(VData, DAG); 5944 auto Offsets = splitBufferOffsets(Op.getOperand(5), DAG); 5945 SDValue Ops[] = { 5946 Chain, 5947 VData, 5948 Op.getOperand(3), // rsrc 5949 Op.getOperand(4), // vindex 5950 Offsets.first, // voffset 5951 Op.getOperand(6), // soffset 5952 Offsets.second, // offset 5953 Op.getOperand(7), // cachepolicy 5954 DAG.getConstant(1, DL, MVT::i1), // idxen 5955 }; 5956 unsigned Opc = IntrinsicID == Intrinsic::amdgcn_struct_buffer_store ? 5957 AMDGPUISD::BUFFER_STORE : AMDGPUISD::BUFFER_STORE_FORMAT; 5958 Opc = IsD16 ? AMDGPUISD::BUFFER_STORE_FORMAT_D16 : Opc; 5959 MemSDNode *M = cast<MemSDNode>(Op); 5960 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, 5961 M->getMemoryVT(), M->getMemOperand()); 5962 } 5963 5964 default: { 5965 if (const AMDGPU::ImageDimIntrinsicInfo *ImageDimIntr = 5966 AMDGPU::getImageDimIntrinsicInfo(IntrinsicID)) 5967 return lowerImage(Op, ImageDimIntr, DAG); 5968 5969 return Op; 5970 } 5971 } 5972 } 5973 5974 // The raw.(t)buffer and struct.(t)buffer intrinsics have two offset args: 5975 // offset (the offset that is included in bounds checking and swizzling, to be 5976 // split between the instruction's voffset and immoffset fields) and soffset 5977 // (the offset that is excluded from bounds checking and swizzling, to go in 5978 // the instruction's soffset field). This function takes the first kind of 5979 // offset and figures out how to split it between voffset and immoffset. 5980 std::pair<SDValue, SDValue> SITargetLowering::splitBufferOffsets( 5981 SDValue Offset, SelectionDAG &DAG) const { 5982 SDLoc DL(Offset); 5983 const unsigned MaxImm = 4095; 5984 SDValue N0 = Offset; 5985 ConstantSDNode *C1 = nullptr; 5986 if (N0.getOpcode() == ISD::ADD) { 5987 if ((C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1)))) 5988 N0 = N0.getOperand(0); 5989 } else if ((C1 = dyn_cast<ConstantSDNode>(N0))) 5990 N0 = SDValue(); 5991 5992 if (C1) { 5993 unsigned ImmOffset = C1->getZExtValue(); 5994 // If the immediate value is too big for the immoffset field, put the value 5995 // mod 4096 into the immoffset field so that the value that is copied/added 5996 // for the voffset field is a multiple of 4096, and it stands more chance 5997 // of being CSEd with the copy/add for another similar load/store. 5998 unsigned Overflow = ImmOffset & ~MaxImm; 5999 ImmOffset -= Overflow; 6000 C1 = cast<ConstantSDNode>(DAG.getConstant(ImmOffset, DL, MVT::i32)); 6001 if (Overflow) { 6002 auto OverflowVal = DAG.getConstant(Overflow, DL, MVT::i32); 6003 if (!N0) 6004 N0 = OverflowVal; 6005 else { 6006 SDValue Ops[] = { N0, OverflowVal }; 6007 N0 = DAG.getNode(ISD::ADD, DL, MVT::i32, Ops); 6008 } 6009 } 6010 } 6011 if (!N0) 6012 N0 = DAG.getConstant(0, DL, MVT::i32); 6013 if (!C1) 6014 C1 = cast<ConstantSDNode>(DAG.getConstant(0, DL, MVT::i32)); 6015 return {N0, SDValue(C1, 0)}; 6016 } 6017 6018 // Analyze a combined offset from an amdgcn_buffer_ intrinsic and store the 6019 // three offsets (voffset, soffset and instoffset) into the SDValue[3] array 6020 // pointed to by Offsets. 6021 void SITargetLowering::setBufferOffsets(SDValue CombinedOffset, 6022 SelectionDAG &DAG, 6023 SDValue *Offsets) const { 6024 SDLoc DL(CombinedOffset); 6025 if (auto C = dyn_cast<ConstantSDNode>(CombinedOffset)) { 6026 uint32_t Imm = C->getZExtValue(); 6027 uint32_t SOffset, ImmOffset; 6028 if (AMDGPU::splitMUBUFOffset(Imm, SOffset, ImmOffset, Subtarget)) { 6029 Offsets[0] = DAG.getConstant(0, DL, MVT::i32); 6030 Offsets[1] = DAG.getConstant(SOffset, DL, MVT::i32); 6031 Offsets[2] = DAG.getConstant(ImmOffset, DL, MVT::i32); 6032 return; 6033 } 6034 } 6035 if (DAG.isBaseWithConstantOffset(CombinedOffset)) { 6036 SDValue N0 = CombinedOffset.getOperand(0); 6037 SDValue N1 = CombinedOffset.getOperand(1); 6038 uint32_t SOffset, ImmOffset; 6039 int Offset = cast<ConstantSDNode>(N1)->getSExtValue(); 6040 if (Offset >= 0 6041 && AMDGPU::splitMUBUFOffset(Offset, SOffset, ImmOffset, Subtarget)) { 6042 Offsets[0] = N0; 6043 Offsets[1] = DAG.getConstant(SOffset, DL, MVT::i32); 6044 Offsets[2] = DAG.getConstant(ImmOffset, DL, MVT::i32); 6045 return; 6046 } 6047 } 6048 Offsets[0] = CombinedOffset; 6049 Offsets[1] = DAG.getConstant(0, DL, MVT::i32); 6050 Offsets[2] = DAG.getConstant(0, DL, MVT::i32); 6051 } 6052 6053 static SDValue getLoadExtOrTrunc(SelectionDAG &DAG, 6054 ISD::LoadExtType ExtType, SDValue Op, 6055 const SDLoc &SL, EVT VT) { 6056 if (VT.bitsLT(Op.getValueType())) 6057 return DAG.getNode(ISD::TRUNCATE, SL, VT, Op); 6058 6059 switch (ExtType) { 6060 case ISD::SEXTLOAD: 6061 return DAG.getNode(ISD::SIGN_EXTEND, SL, VT, Op); 6062 case ISD::ZEXTLOAD: 6063 return DAG.getNode(ISD::ZERO_EXTEND, SL, VT, Op); 6064 case ISD::EXTLOAD: 6065 return DAG.getNode(ISD::ANY_EXTEND, SL, VT, Op); 6066 case ISD::NON_EXTLOAD: 6067 return Op; 6068 } 6069 6070 llvm_unreachable("invalid ext type"); 6071 } 6072 6073 SDValue SITargetLowering::widenLoad(LoadSDNode *Ld, DAGCombinerInfo &DCI) const { 6074 SelectionDAG &DAG = DCI.DAG; 6075 if (Ld->getAlignment() < 4 || Ld->isDivergent()) 6076 return SDValue(); 6077 6078 // FIXME: Constant loads should all be marked invariant. 6079 unsigned AS = Ld->getAddressSpace(); 6080 if (AS != AMDGPUASI.CONSTANT_ADDRESS && 6081 AS != AMDGPUASI.CONSTANT_ADDRESS_32BIT && 6082 (AS != AMDGPUAS::GLOBAL_ADDRESS || !Ld->isInvariant())) 6083 return SDValue(); 6084 6085 // Don't do this early, since it may interfere with adjacent load merging for 6086 // illegal types. We can avoid losing alignment information for exotic types 6087 // pre-legalize. 6088 EVT MemVT = Ld->getMemoryVT(); 6089 if ((MemVT.isSimple() && !DCI.isAfterLegalizeDAG()) || 6090 MemVT.getSizeInBits() >= 32) 6091 return SDValue(); 6092 6093 SDLoc SL(Ld); 6094 6095 assert((!MemVT.isVector() || Ld->getExtensionType() == ISD::NON_EXTLOAD) && 6096 "unexpected vector extload"); 6097 6098 // TODO: Drop only high part of range. 6099 SDValue Ptr = Ld->getBasePtr(); 6100 SDValue NewLoad = DAG.getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD, 6101 MVT::i32, SL, Ld->getChain(), Ptr, 6102 Ld->getOffset(), 6103 Ld->getPointerInfo(), MVT::i32, 6104 Ld->getAlignment(), 6105 Ld->getMemOperand()->getFlags(), 6106 Ld->getAAInfo(), 6107 nullptr); // Drop ranges 6108 6109 EVT TruncVT = EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits()); 6110 if (MemVT.isFloatingPoint()) { 6111 assert(Ld->getExtensionType() == ISD::NON_EXTLOAD && 6112 "unexpected fp extload"); 6113 TruncVT = MemVT.changeTypeToInteger(); 6114 } 6115 6116 SDValue Cvt = NewLoad; 6117 if (Ld->getExtensionType() == ISD::SEXTLOAD) { 6118 Cvt = DAG.getNode(ISD::SIGN_EXTEND_INREG, SL, MVT::i32, NewLoad, 6119 DAG.getValueType(TruncVT)); 6120 } else if (Ld->getExtensionType() == ISD::ZEXTLOAD || 6121 Ld->getExtensionType() == ISD::NON_EXTLOAD) { 6122 Cvt = DAG.getZeroExtendInReg(NewLoad, SL, TruncVT); 6123 } else { 6124 assert(Ld->getExtensionType() == ISD::EXTLOAD); 6125 } 6126 6127 EVT VT = Ld->getValueType(0); 6128 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); 6129 6130 DCI.AddToWorklist(Cvt.getNode()); 6131 6132 // We may need to handle exotic cases, such as i16->i64 extloads, so insert 6133 // the appropriate extension from the 32-bit load. 6134 Cvt = getLoadExtOrTrunc(DAG, Ld->getExtensionType(), Cvt, SL, IntVT); 6135 DCI.AddToWorklist(Cvt.getNode()); 6136 6137 // Handle conversion back to floating point if necessary. 6138 Cvt = DAG.getNode(ISD::BITCAST, SL, VT, Cvt); 6139 6140 return DAG.getMergeValues({ Cvt, NewLoad.getValue(1) }, SL); 6141 } 6142 6143 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { 6144 SDLoc DL(Op); 6145 LoadSDNode *Load = cast<LoadSDNode>(Op); 6146 ISD::LoadExtType ExtType = Load->getExtensionType(); 6147 EVT MemVT = Load->getMemoryVT(); 6148 6149 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) { 6150 if (MemVT == MVT::i16 && isTypeLegal(MVT::i16)) 6151 return SDValue(); 6152 6153 // FIXME: Copied from PPC 6154 // First, load into 32 bits, then truncate to 1 bit. 6155 6156 SDValue Chain = Load->getChain(); 6157 SDValue BasePtr = Load->getBasePtr(); 6158 MachineMemOperand *MMO = Load->getMemOperand(); 6159 6160 EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16; 6161 6162 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain, 6163 BasePtr, RealMemVT, MMO); 6164 6165 SDValue Ops[] = { 6166 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD), 6167 NewLD.getValue(1) 6168 }; 6169 6170 return DAG.getMergeValues(Ops, DL); 6171 } 6172 6173 if (!MemVT.isVector()) 6174 return SDValue(); 6175 6176 assert(Op.getValueType().getVectorElementType() == MVT::i32 && 6177 "Custom lowering for non-i32 vectors hasn't been implemented."); 6178 6179 unsigned Alignment = Load->getAlignment(); 6180 unsigned AS = Load->getAddressSpace(); 6181 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT, 6182 AS, Alignment)) { 6183 SDValue Ops[2]; 6184 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG); 6185 return DAG.getMergeValues(Ops, DL); 6186 } 6187 6188 MachineFunction &MF = DAG.getMachineFunction(); 6189 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 6190 // If there is a possibilty that flat instruction access scratch memory 6191 // then we need to use the same legalization rules we use for private. 6192 if (AS == AMDGPUASI.FLAT_ADDRESS) 6193 AS = MFI->hasFlatScratchInit() ? 6194 AMDGPUASI.PRIVATE_ADDRESS : AMDGPUASI.GLOBAL_ADDRESS; 6195 6196 unsigned NumElements = MemVT.getVectorNumElements(); 6197 6198 if (AS == AMDGPUASI.CONSTANT_ADDRESS || 6199 AS == AMDGPUASI.CONSTANT_ADDRESS_32BIT) { 6200 if (!Op->isDivergent() && Alignment >= 4) 6201 return SDValue(); 6202 // Non-uniform loads will be selected to MUBUF instructions, so they 6203 // have the same legalization requirements as global and private 6204 // loads. 6205 // 6206 } 6207 6208 if (AS == AMDGPUASI.CONSTANT_ADDRESS || 6209 AS == AMDGPUASI.CONSTANT_ADDRESS_32BIT || 6210 AS == AMDGPUASI.GLOBAL_ADDRESS) { 6211 if (Subtarget->getScalarizeGlobalBehavior() && !Op->isDivergent() && 6212 !Load->isVolatile() && isMemOpHasNoClobberedMemOperand(Load) && 6213 Alignment >= 4) 6214 return SDValue(); 6215 // Non-uniform loads will be selected to MUBUF instructions, so they 6216 // have the same legalization requirements as global and private 6217 // loads. 6218 // 6219 } 6220 if (AS == AMDGPUASI.CONSTANT_ADDRESS || 6221 AS == AMDGPUASI.CONSTANT_ADDRESS_32BIT || 6222 AS == AMDGPUASI.GLOBAL_ADDRESS || 6223 AS == AMDGPUASI.FLAT_ADDRESS) { 6224 if (NumElements > 4) 6225 return SplitVectorLoad(Op, DAG); 6226 // v4 loads are supported for private and global memory. 6227 return SDValue(); 6228 } 6229 if (AS == AMDGPUASI.PRIVATE_ADDRESS) { 6230 // Depending on the setting of the private_element_size field in the 6231 // resource descriptor, we can only make private accesses up to a certain 6232 // size. 6233 switch (Subtarget->getMaxPrivateElementSize()) { 6234 case 4: 6235 return scalarizeVectorLoad(Load, DAG); 6236 case 8: 6237 if (NumElements > 2) 6238 return SplitVectorLoad(Op, DAG); 6239 return SDValue(); 6240 case 16: 6241 // Same as global/flat 6242 if (NumElements > 4) 6243 return SplitVectorLoad(Op, DAG); 6244 return SDValue(); 6245 default: 6246 llvm_unreachable("unsupported private_element_size"); 6247 } 6248 } else if (AS == AMDGPUASI.LOCAL_ADDRESS) { 6249 // Use ds_read_b128 if possible. 6250 if (Subtarget->useDS128() && Load->getAlignment() >= 16 && 6251 MemVT.getStoreSize() == 16) 6252 return SDValue(); 6253 6254 if (NumElements > 2) 6255 return SplitVectorLoad(Op, DAG); 6256 } 6257 return SDValue(); 6258 } 6259 6260 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 6261 EVT VT = Op.getValueType(); 6262 assert(VT.getSizeInBits() == 64); 6263 6264 SDLoc DL(Op); 6265 SDValue Cond = Op.getOperand(0); 6266 6267 SDValue Zero = DAG.getConstant(0, DL, MVT::i32); 6268 SDValue One = DAG.getConstant(1, DL, MVT::i32); 6269 6270 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1)); 6271 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2)); 6272 6273 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero); 6274 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero); 6275 6276 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1); 6277 6278 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One); 6279 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One); 6280 6281 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1); 6282 6283 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi}); 6284 return DAG.getNode(ISD::BITCAST, DL, VT, Res); 6285 } 6286 6287 // Catch division cases where we can use shortcuts with rcp and rsq 6288 // instructions. 6289 SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op, 6290 SelectionDAG &DAG) const { 6291 SDLoc SL(Op); 6292 SDValue LHS = Op.getOperand(0); 6293 SDValue RHS = Op.getOperand(1); 6294 EVT VT = Op.getValueType(); 6295 const SDNodeFlags Flags = Op->getFlags(); 6296 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath || Flags.hasAllowReciprocal(); 6297 6298 if (!Unsafe && VT == MVT::f32 && Subtarget->hasFP32Denormals()) 6299 return SDValue(); 6300 6301 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) { 6302 if (Unsafe || VT == MVT::f32 || VT == MVT::f16) { 6303 if (CLHS->isExactlyValue(1.0)) { 6304 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to 6305 // the CI documentation has a worst case error of 1 ulp. 6306 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to 6307 // use it as long as we aren't trying to use denormals. 6308 // 6309 // v_rcp_f16 and v_rsq_f16 DO support denormals. 6310 6311 // 1.0 / sqrt(x) -> rsq(x) 6312 6313 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP 6314 // error seems really high at 2^29 ULP. 6315 if (RHS.getOpcode() == ISD::FSQRT) 6316 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0)); 6317 6318 // 1.0 / x -> rcp(x) 6319 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 6320 } 6321 6322 // Same as for 1.0, but expand the sign out of the constant. 6323 if (CLHS->isExactlyValue(-1.0)) { 6324 // -1.0 / x -> rcp (fneg x) 6325 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 6326 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS); 6327 } 6328 } 6329 } 6330 6331 if (Unsafe) { 6332 // Turn into multiply by the reciprocal. 6333 // x / y -> x * (1.0 / y) 6334 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 6335 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, Flags); 6336 } 6337 6338 return SDValue(); 6339 } 6340 6341 static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL, 6342 EVT VT, SDValue A, SDValue B, SDValue GlueChain) { 6343 if (GlueChain->getNumValues() <= 1) { 6344 return DAG.getNode(Opcode, SL, VT, A, B); 6345 } 6346 6347 assert(GlueChain->getNumValues() == 3); 6348 6349 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue); 6350 switch (Opcode) { 6351 default: llvm_unreachable("no chain equivalent for opcode"); 6352 case ISD::FMUL: 6353 Opcode = AMDGPUISD::FMUL_W_CHAIN; 6354 break; 6355 } 6356 6357 return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B, 6358 GlueChain.getValue(2)); 6359 } 6360 6361 static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL, 6362 EVT VT, SDValue A, SDValue B, SDValue C, 6363 SDValue GlueChain) { 6364 if (GlueChain->getNumValues() <= 1) { 6365 return DAG.getNode(Opcode, SL, VT, A, B, C); 6366 } 6367 6368 assert(GlueChain->getNumValues() == 3); 6369 6370 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue); 6371 switch (Opcode) { 6372 default: llvm_unreachable("no chain equivalent for opcode"); 6373 case ISD::FMA: 6374 Opcode = AMDGPUISD::FMA_W_CHAIN; 6375 break; 6376 } 6377 6378 return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B, C, 6379 GlueChain.getValue(2)); 6380 } 6381 6382 SDValue SITargetLowering::LowerFDIV16(SDValue Op, SelectionDAG &DAG) const { 6383 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG)) 6384 return FastLowered; 6385 6386 SDLoc SL(Op); 6387 SDValue Src0 = Op.getOperand(0); 6388 SDValue Src1 = Op.getOperand(1); 6389 6390 SDValue CvtSrc0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0); 6391 SDValue CvtSrc1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1); 6392 6393 SDValue RcpSrc1 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, CvtSrc1); 6394 SDValue Quot = DAG.getNode(ISD::FMUL, SL, MVT::f32, CvtSrc0, RcpSrc1); 6395 6396 SDValue FPRoundFlag = DAG.getTargetConstant(0, SL, MVT::i32); 6397 SDValue BestQuot = DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Quot, FPRoundFlag); 6398 6399 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f16, BestQuot, Src1, Src0); 6400 } 6401 6402 // Faster 2.5 ULP division that does not support denormals. 6403 SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const { 6404 SDLoc SL(Op); 6405 SDValue LHS = Op.getOperand(1); 6406 SDValue RHS = Op.getOperand(2); 6407 6408 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS); 6409 6410 const APFloat K0Val(BitsToFloat(0x6f800000)); 6411 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32); 6412 6413 const APFloat K1Val(BitsToFloat(0x2f800000)); 6414 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32); 6415 6416 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 6417 6418 EVT SetCCVT = 6419 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); 6420 6421 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT); 6422 6423 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One); 6424 6425 // TODO: Should this propagate fast-math-flags? 6426 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3); 6427 6428 // rcp does not support denormals. 6429 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1); 6430 6431 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0); 6432 6433 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul); 6434 } 6435 6436 SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const { 6437 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG)) 6438 return FastLowered; 6439 6440 SDLoc SL(Op); 6441 SDValue LHS = Op.getOperand(0); 6442 SDValue RHS = Op.getOperand(1); 6443 6444 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 6445 6446 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1); 6447 6448 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, 6449 RHS, RHS, LHS); 6450 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, 6451 LHS, RHS, LHS); 6452 6453 // Denominator is scaled to not be denormal, so using rcp is ok. 6454 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, 6455 DenominatorScaled); 6456 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, 6457 DenominatorScaled); 6458 6459 const unsigned Denorm32Reg = AMDGPU::Hwreg::ID_MODE | 6460 (4 << AMDGPU::Hwreg::OFFSET_SHIFT_) | 6461 (1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_); 6462 6463 const SDValue BitField = DAG.getTargetConstant(Denorm32Reg, SL, MVT::i16); 6464 6465 if (!Subtarget->hasFP32Denormals()) { 6466 SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue); 6467 const SDValue EnableDenormValue = DAG.getConstant(FP_DENORM_FLUSH_NONE, 6468 SL, MVT::i32); 6469 SDValue EnableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, BindParamVTs, 6470 DAG.getEntryNode(), 6471 EnableDenormValue, BitField); 6472 SDValue Ops[3] = { 6473 NegDivScale0, 6474 EnableDenorm.getValue(0), 6475 EnableDenorm.getValue(1) 6476 }; 6477 6478 NegDivScale0 = DAG.getMergeValues(Ops, SL); 6479 } 6480 6481 SDValue Fma0 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, 6482 ApproxRcp, One, NegDivScale0); 6483 6484 SDValue Fma1 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, 6485 ApproxRcp, Fma0); 6486 6487 SDValue Mul = getFPBinOp(DAG, ISD::FMUL, SL, MVT::f32, NumeratorScaled, 6488 Fma1, Fma1); 6489 6490 SDValue Fma2 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, 6491 NumeratorScaled, Mul); 6492 6493 SDValue Fma3 = getFPTernOp(DAG, ISD::FMA,SL, MVT::f32, Fma2, Fma1, Mul, Fma2); 6494 6495 SDValue Fma4 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, 6496 NumeratorScaled, Fma3); 6497 6498 if (!Subtarget->hasFP32Denormals()) { 6499 const SDValue DisableDenormValue = 6500 DAG.getConstant(FP_DENORM_FLUSH_IN_FLUSH_OUT, SL, MVT::i32); 6501 SDValue DisableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, MVT::Other, 6502 Fma4.getValue(1), 6503 DisableDenormValue, 6504 BitField, 6505 Fma4.getValue(2)); 6506 6507 SDValue OutputChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other, 6508 DisableDenorm, DAG.getRoot()); 6509 DAG.setRoot(OutputChain); 6510 } 6511 6512 SDValue Scale = NumeratorScaled.getValue(1); 6513 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, 6514 Fma4, Fma1, Fma3, Scale); 6515 6516 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS); 6517 } 6518 6519 SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const { 6520 if (DAG.getTarget().Options.UnsafeFPMath) 6521 return lowerFastUnsafeFDIV(Op, DAG); 6522 6523 SDLoc SL(Op); 6524 SDValue X = Op.getOperand(0); 6525 SDValue Y = Op.getOperand(1); 6526 6527 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); 6528 6529 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1); 6530 6531 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X); 6532 6533 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0); 6534 6535 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0); 6536 6537 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One); 6538 6539 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp); 6540 6541 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One); 6542 6543 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X); 6544 6545 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1); 6546 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3); 6547 6548 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64, 6549 NegDivScale0, Mul, DivScale1); 6550 6551 SDValue Scale; 6552 6553 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) { 6554 // Workaround a hardware bug on SI where the condition output from div_scale 6555 // is not usable. 6556 6557 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32); 6558 6559 // Figure out if the scale to use for div_fmas. 6560 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); 6561 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y); 6562 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0); 6563 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1); 6564 6565 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi); 6566 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi); 6567 6568 SDValue Scale0Hi 6569 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi); 6570 SDValue Scale1Hi 6571 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi); 6572 6573 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ); 6574 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ); 6575 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen); 6576 } else { 6577 Scale = DivScale1.getValue(1); 6578 } 6579 6580 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, 6581 Fma4, Fma3, Mul, Scale); 6582 6583 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X); 6584 } 6585 6586 SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const { 6587 EVT VT = Op.getValueType(); 6588 6589 if (VT == MVT::f32) 6590 return LowerFDIV32(Op, DAG); 6591 6592 if (VT == MVT::f64) 6593 return LowerFDIV64(Op, DAG); 6594 6595 if (VT == MVT::f16) 6596 return LowerFDIV16(Op, DAG); 6597 6598 llvm_unreachable("Unexpected type for fdiv"); 6599 } 6600 6601 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { 6602 SDLoc DL(Op); 6603 StoreSDNode *Store = cast<StoreSDNode>(Op); 6604 EVT VT = Store->getMemoryVT(); 6605 6606 if (VT == MVT::i1) { 6607 return DAG.getTruncStore(Store->getChain(), DL, 6608 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32), 6609 Store->getBasePtr(), MVT::i1, Store->getMemOperand()); 6610 } 6611 6612 assert(VT.isVector() && 6613 Store->getValue().getValueType().getScalarType() == MVT::i32); 6614 6615 unsigned AS = Store->getAddressSpace(); 6616 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT, 6617 AS, Store->getAlignment())) { 6618 return expandUnalignedStore(Store, DAG); 6619 } 6620 6621 MachineFunction &MF = DAG.getMachineFunction(); 6622 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 6623 // If there is a possibilty that flat instruction access scratch memory 6624 // then we need to use the same legalization rules we use for private. 6625 if (AS == AMDGPUASI.FLAT_ADDRESS) 6626 AS = MFI->hasFlatScratchInit() ? 6627 AMDGPUASI.PRIVATE_ADDRESS : AMDGPUASI.GLOBAL_ADDRESS; 6628 6629 unsigned NumElements = VT.getVectorNumElements(); 6630 if (AS == AMDGPUASI.GLOBAL_ADDRESS || 6631 AS == AMDGPUASI.FLAT_ADDRESS) { 6632 if (NumElements > 4) 6633 return SplitVectorStore(Op, DAG); 6634 return SDValue(); 6635 } else if (AS == AMDGPUASI.PRIVATE_ADDRESS) { 6636 switch (Subtarget->getMaxPrivateElementSize()) { 6637 case 4: 6638 return scalarizeVectorStore(Store, DAG); 6639 case 8: 6640 if (NumElements > 2) 6641 return SplitVectorStore(Op, DAG); 6642 return SDValue(); 6643 case 16: 6644 if (NumElements > 4) 6645 return SplitVectorStore(Op, DAG); 6646 return SDValue(); 6647 default: 6648 llvm_unreachable("unsupported private_element_size"); 6649 } 6650 } else if (AS == AMDGPUASI.LOCAL_ADDRESS) { 6651 // Use ds_write_b128 if possible. 6652 if (Subtarget->useDS128() && Store->getAlignment() >= 16 && 6653 VT.getStoreSize() == 16) 6654 return SDValue(); 6655 6656 if (NumElements > 2) 6657 return SplitVectorStore(Op, DAG); 6658 return SDValue(); 6659 } else { 6660 llvm_unreachable("unhandled address space"); 6661 } 6662 } 6663 6664 SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const { 6665 SDLoc DL(Op); 6666 EVT VT = Op.getValueType(); 6667 SDValue Arg = Op.getOperand(0); 6668 // TODO: Should this propagate fast-math-flags? 6669 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT, 6670 DAG.getNode(ISD::FMUL, DL, VT, Arg, 6671 DAG.getConstantFP(0.5/M_PI, DL, 6672 VT))); 6673 6674 switch (Op.getOpcode()) { 6675 case ISD::FCOS: 6676 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart); 6677 case ISD::FSIN: 6678 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart); 6679 default: 6680 llvm_unreachable("Wrong trig opcode"); 6681 } 6682 } 6683 6684 SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const { 6685 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op); 6686 assert(AtomicNode->isCompareAndSwap()); 6687 unsigned AS = AtomicNode->getAddressSpace(); 6688 6689 // No custom lowering required for local address space 6690 if (!isFlatGlobalAddrSpace(AS, AMDGPUASI)) 6691 return Op; 6692 6693 // Non-local address space requires custom lowering for atomic compare 6694 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2 6695 SDLoc DL(Op); 6696 SDValue ChainIn = Op.getOperand(0); 6697 SDValue Addr = Op.getOperand(1); 6698 SDValue Old = Op.getOperand(2); 6699 SDValue New = Op.getOperand(3); 6700 EVT VT = Op.getValueType(); 6701 MVT SimpleVT = VT.getSimpleVT(); 6702 MVT VecType = MVT::getVectorVT(SimpleVT, 2); 6703 6704 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old}); 6705 SDValue Ops[] = { ChainIn, Addr, NewOld }; 6706 6707 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(), 6708 Ops, VT, AtomicNode->getMemOperand()); 6709 } 6710 6711 //===----------------------------------------------------------------------===// 6712 // Custom DAG optimizations 6713 //===----------------------------------------------------------------------===// 6714 6715 SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N, 6716 DAGCombinerInfo &DCI) const { 6717 EVT VT = N->getValueType(0); 6718 EVT ScalarVT = VT.getScalarType(); 6719 if (ScalarVT != MVT::f32) 6720 return SDValue(); 6721 6722 SelectionDAG &DAG = DCI.DAG; 6723 SDLoc DL(N); 6724 6725 SDValue Src = N->getOperand(0); 6726 EVT SrcVT = Src.getValueType(); 6727 6728 // TODO: We could try to match extracting the higher bytes, which would be 6729 // easier if i8 vectors weren't promoted to i32 vectors, particularly after 6730 // types are legalized. v4i8 -> v4f32 is probably the only case to worry 6731 // about in practice. 6732 if (DCI.isAfterLegalizeDAG() && SrcVT == MVT::i32) { 6733 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) { 6734 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src); 6735 DCI.AddToWorklist(Cvt.getNode()); 6736 return Cvt; 6737 } 6738 } 6739 6740 return SDValue(); 6741 } 6742 6743 // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2) 6744 6745 // This is a variant of 6746 // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2), 6747 // 6748 // The normal DAG combiner will do this, but only if the add has one use since 6749 // that would increase the number of instructions. 6750 // 6751 // This prevents us from seeing a constant offset that can be folded into a 6752 // memory instruction's addressing mode. If we know the resulting add offset of 6753 // a pointer can be folded into an addressing offset, we can replace the pointer 6754 // operand with the add of new constant offset. This eliminates one of the uses, 6755 // and may allow the remaining use to also be simplified. 6756 // 6757 SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, 6758 unsigned AddrSpace, 6759 EVT MemVT, 6760 DAGCombinerInfo &DCI) const { 6761 SDValue N0 = N->getOperand(0); 6762 SDValue N1 = N->getOperand(1); 6763 6764 // We only do this to handle cases where it's profitable when there are 6765 // multiple uses of the add, so defer to the standard combine. 6766 if ((N0.getOpcode() != ISD::ADD && N0.getOpcode() != ISD::OR) || 6767 N0->hasOneUse()) 6768 return SDValue(); 6769 6770 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1); 6771 if (!CN1) 6772 return SDValue(); 6773 6774 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 6775 if (!CAdd) 6776 return SDValue(); 6777 6778 // If the resulting offset is too large, we can't fold it into the addressing 6779 // mode offset. 6780 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue(); 6781 Type *Ty = MemVT.getTypeForEVT(*DCI.DAG.getContext()); 6782 6783 AddrMode AM; 6784 AM.HasBaseReg = true; 6785 AM.BaseOffs = Offset.getSExtValue(); 6786 if (!isLegalAddressingMode(DCI.DAG.getDataLayout(), AM, Ty, AddrSpace)) 6787 return SDValue(); 6788 6789 SelectionDAG &DAG = DCI.DAG; 6790 SDLoc SL(N); 6791 EVT VT = N->getValueType(0); 6792 6793 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1); 6794 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32); 6795 6796 SDNodeFlags Flags; 6797 Flags.setNoUnsignedWrap(N->getFlags().hasNoUnsignedWrap() && 6798 (N0.getOpcode() == ISD::OR || 6799 N0->getFlags().hasNoUnsignedWrap())); 6800 6801 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset, Flags); 6802 } 6803 6804 SDValue SITargetLowering::performMemSDNodeCombine(MemSDNode *N, 6805 DAGCombinerInfo &DCI) const { 6806 SDValue Ptr = N->getBasePtr(); 6807 SelectionDAG &DAG = DCI.DAG; 6808 SDLoc SL(N); 6809 6810 // TODO: We could also do this for multiplies. 6811 if (Ptr.getOpcode() == ISD::SHL) { 6812 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), N->getAddressSpace(), 6813 N->getMemoryVT(), DCI); 6814 if (NewPtr) { 6815 SmallVector<SDValue, 8> NewOps(N->op_begin(), N->op_end()); 6816 6817 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr; 6818 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0); 6819 } 6820 } 6821 6822 return SDValue(); 6823 } 6824 6825 static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) { 6826 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) || 6827 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) || 6828 (Opc == ISD::XOR && Val == 0); 6829 } 6830 6831 // Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This 6832 // will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit 6833 // integer combine opportunities since most 64-bit operations are decomposed 6834 // this way. TODO: We won't want this for SALU especially if it is an inline 6835 // immediate. 6836 SDValue SITargetLowering::splitBinaryBitConstantOp( 6837 DAGCombinerInfo &DCI, 6838 const SDLoc &SL, 6839 unsigned Opc, SDValue LHS, 6840 const ConstantSDNode *CRHS) const { 6841 uint64_t Val = CRHS->getZExtValue(); 6842 uint32_t ValLo = Lo_32(Val); 6843 uint32_t ValHi = Hi_32(Val); 6844 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 6845 6846 if ((bitOpWithConstantIsReducible(Opc, ValLo) || 6847 bitOpWithConstantIsReducible(Opc, ValHi)) || 6848 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) { 6849 // If we need to materialize a 64-bit immediate, it will be split up later 6850 // anyway. Avoid creating the harder to understand 64-bit immediate 6851 // materialization. 6852 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi); 6853 } 6854 6855 return SDValue(); 6856 } 6857 6858 // Returns true if argument is a boolean value which is not serialized into 6859 // memory or argument and does not require v_cmdmask_b32 to be deserialized. 6860 static bool isBoolSGPR(SDValue V) { 6861 if (V.getValueType() != MVT::i1) 6862 return false; 6863 switch (V.getOpcode()) { 6864 default: break; 6865 case ISD::SETCC: 6866 case ISD::AND: 6867 case ISD::OR: 6868 case ISD::XOR: 6869 case AMDGPUISD::FP_CLASS: 6870 return true; 6871 } 6872 return false; 6873 } 6874 6875 // If a constant has all zeroes or all ones within each byte return it. 6876 // Otherwise return 0. 6877 static uint32_t getConstantPermuteMask(uint32_t C) { 6878 // 0xff for any zero byte in the mask 6879 uint32_t ZeroByteMask = 0; 6880 if (!(C & 0x000000ff)) ZeroByteMask |= 0x000000ff; 6881 if (!(C & 0x0000ff00)) ZeroByteMask |= 0x0000ff00; 6882 if (!(C & 0x00ff0000)) ZeroByteMask |= 0x00ff0000; 6883 if (!(C & 0xff000000)) ZeroByteMask |= 0xff000000; 6884 uint32_t NonZeroByteMask = ~ZeroByteMask; // 0xff for any non-zero byte 6885 if ((NonZeroByteMask & C) != NonZeroByteMask) 6886 return 0; // Partial bytes selected. 6887 return C; 6888 } 6889 6890 // Check if a node selects whole bytes from its operand 0 starting at a byte 6891 // boundary while masking the rest. Returns select mask as in the v_perm_b32 6892 // or -1 if not succeeded. 6893 // Note byte select encoding: 6894 // value 0-3 selects corresponding source byte; 6895 // value 0xc selects zero; 6896 // value 0xff selects 0xff. 6897 static uint32_t getPermuteMask(SelectionDAG &DAG, SDValue V) { 6898 assert(V.getValueSizeInBits() == 32); 6899 6900 if (V.getNumOperands() != 2) 6901 return ~0; 6902 6903 ConstantSDNode *N1 = dyn_cast<ConstantSDNode>(V.getOperand(1)); 6904 if (!N1) 6905 return ~0; 6906 6907 uint32_t C = N1->getZExtValue(); 6908 6909 switch (V.getOpcode()) { 6910 default: 6911 break; 6912 case ISD::AND: 6913 if (uint32_t ConstMask = getConstantPermuteMask(C)) { 6914 return (0x03020100 & ConstMask) | (0x0c0c0c0c & ~ConstMask); 6915 } 6916 break; 6917 6918 case ISD::OR: 6919 if (uint32_t ConstMask = getConstantPermuteMask(C)) { 6920 return (0x03020100 & ~ConstMask) | ConstMask; 6921 } 6922 break; 6923 6924 case ISD::SHL: 6925 if (C % 8) 6926 return ~0; 6927 6928 return uint32_t((0x030201000c0c0c0cull << C) >> 32); 6929 6930 case ISD::SRL: 6931 if (C % 8) 6932 return ~0; 6933 6934 return uint32_t(0x0c0c0c0c03020100ull >> C); 6935 } 6936 6937 return ~0; 6938 } 6939 6940 SDValue SITargetLowering::performAndCombine(SDNode *N, 6941 DAGCombinerInfo &DCI) const { 6942 if (DCI.isBeforeLegalize()) 6943 return SDValue(); 6944 6945 SelectionDAG &DAG = DCI.DAG; 6946 EVT VT = N->getValueType(0); 6947 SDValue LHS = N->getOperand(0); 6948 SDValue RHS = N->getOperand(1); 6949 6950 6951 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); 6952 if (VT == MVT::i64 && CRHS) { 6953 if (SDValue Split 6954 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS)) 6955 return Split; 6956 } 6957 6958 if (CRHS && VT == MVT::i32) { 6959 // and (srl x, c), mask => shl (bfe x, nb + c, mask >> nb), nb 6960 // nb = number of trailing zeroes in mask 6961 // It can be optimized out using SDWA for GFX8+ in the SDWA peephole pass, 6962 // given that we are selecting 8 or 16 bit fields starting at byte boundary. 6963 uint64_t Mask = CRHS->getZExtValue(); 6964 unsigned Bits = countPopulation(Mask); 6965 if (getSubtarget()->hasSDWA() && LHS->getOpcode() == ISD::SRL && 6966 (Bits == 8 || Bits == 16) && isShiftedMask_64(Mask) && !(Mask & 1)) { 6967 if (auto *CShift = dyn_cast<ConstantSDNode>(LHS->getOperand(1))) { 6968 unsigned Shift = CShift->getZExtValue(); 6969 unsigned NB = CRHS->getAPIntValue().countTrailingZeros(); 6970 unsigned Offset = NB + Shift; 6971 if ((Offset & (Bits - 1)) == 0) { // Starts at a byte or word boundary. 6972 SDLoc SL(N); 6973 SDValue BFE = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32, 6974 LHS->getOperand(0), 6975 DAG.getConstant(Offset, SL, MVT::i32), 6976 DAG.getConstant(Bits, SL, MVT::i32)); 6977 EVT NarrowVT = EVT::getIntegerVT(*DAG.getContext(), Bits); 6978 SDValue Ext = DAG.getNode(ISD::AssertZext, SL, VT, BFE, 6979 DAG.getValueType(NarrowVT)); 6980 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(LHS), VT, Ext, 6981 DAG.getConstant(NB, SDLoc(CRHS), MVT::i32)); 6982 return Shl; 6983 } 6984 } 6985 } 6986 6987 // and (perm x, y, c1), c2 -> perm x, y, permute_mask(c1, c2) 6988 if (LHS.hasOneUse() && LHS.getOpcode() == AMDGPUISD::PERM && 6989 isa<ConstantSDNode>(LHS.getOperand(2))) { 6990 uint32_t Sel = getConstantPermuteMask(Mask); 6991 if (!Sel) 6992 return SDValue(); 6993 6994 // Select 0xc for all zero bytes 6995 Sel = (LHS.getConstantOperandVal(2) & Sel) | (~Sel & 0x0c0c0c0c); 6996 SDLoc DL(N); 6997 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0), 6998 LHS.getOperand(1), DAG.getConstant(Sel, DL, MVT::i32)); 6999 } 7000 } 7001 7002 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) -> 7003 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity) 7004 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) { 7005 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); 7006 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get(); 7007 7008 SDValue X = LHS.getOperand(0); 7009 SDValue Y = RHS.getOperand(0); 7010 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X) 7011 return SDValue(); 7012 7013 if (LCC == ISD::SETO) { 7014 if (X != LHS.getOperand(1)) 7015 return SDValue(); 7016 7017 if (RCC == ISD::SETUNE) { 7018 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1)); 7019 if (!C1 || !C1->isInfinity() || C1->isNegative()) 7020 return SDValue(); 7021 7022 const uint32_t Mask = SIInstrFlags::N_NORMAL | 7023 SIInstrFlags::N_SUBNORMAL | 7024 SIInstrFlags::N_ZERO | 7025 SIInstrFlags::P_ZERO | 7026 SIInstrFlags::P_SUBNORMAL | 7027 SIInstrFlags::P_NORMAL; 7028 7029 static_assert(((~(SIInstrFlags::S_NAN | 7030 SIInstrFlags::Q_NAN | 7031 SIInstrFlags::N_INFINITY | 7032 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask, 7033 "mask not equal"); 7034 7035 SDLoc DL(N); 7036 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 7037 X, DAG.getConstant(Mask, DL, MVT::i32)); 7038 } 7039 } 7040 } 7041 7042 if (RHS.getOpcode() == ISD::SETCC && LHS.getOpcode() == AMDGPUISD::FP_CLASS) 7043 std::swap(LHS, RHS); 7044 7045 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == AMDGPUISD::FP_CLASS && 7046 RHS.hasOneUse()) { 7047 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); 7048 // and (fcmp seto), (fp_class x, mask) -> fp_class x, mask & ~(p_nan | n_nan) 7049 // and (fcmp setuo), (fp_class x, mask) -> fp_class x, mask & (p_nan | n_nan) 7050 const ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); 7051 if ((LCC == ISD::SETO || LCC == ISD::SETUO) && Mask && 7052 (RHS.getOperand(0) == LHS.getOperand(0) && 7053 LHS.getOperand(0) == LHS.getOperand(1))) { 7054 const unsigned OrdMask = SIInstrFlags::S_NAN | SIInstrFlags::Q_NAN; 7055 unsigned NewMask = LCC == ISD::SETO ? 7056 Mask->getZExtValue() & ~OrdMask : 7057 Mask->getZExtValue() & OrdMask; 7058 7059 SDLoc DL(N); 7060 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, RHS.getOperand(0), 7061 DAG.getConstant(NewMask, DL, MVT::i32)); 7062 } 7063 } 7064 7065 if (VT == MVT::i32 && 7066 (RHS.getOpcode() == ISD::SIGN_EXTEND || LHS.getOpcode() == ISD::SIGN_EXTEND)) { 7067 // and x, (sext cc from i1) => select cc, x, 0 7068 if (RHS.getOpcode() != ISD::SIGN_EXTEND) 7069 std::swap(LHS, RHS); 7070 if (isBoolSGPR(RHS.getOperand(0))) 7071 return DAG.getSelect(SDLoc(N), MVT::i32, RHS.getOperand(0), 7072 LHS, DAG.getConstant(0, SDLoc(N), MVT::i32)); 7073 } 7074 7075 // and (op x, c1), (op y, c2) -> perm x, y, permute_mask(c1, c2) 7076 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 7077 if (VT == MVT::i32 && LHS.hasOneUse() && RHS.hasOneUse() && 7078 N->isDivergent() && TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32) != -1) { 7079 uint32_t LHSMask = getPermuteMask(DAG, LHS); 7080 uint32_t RHSMask = getPermuteMask(DAG, RHS); 7081 if (LHSMask != ~0u && RHSMask != ~0u) { 7082 // Canonicalize the expression in an attempt to have fewer unique masks 7083 // and therefore fewer registers used to hold the masks. 7084 if (LHSMask > RHSMask) { 7085 std::swap(LHSMask, RHSMask); 7086 std::swap(LHS, RHS); 7087 } 7088 7089 // Select 0xc for each lane used from source operand. Zero has 0xc mask 7090 // set, 0xff have 0xff in the mask, actual lanes are in the 0-3 range. 7091 uint32_t LHSUsedLanes = ~(LHSMask & 0x0c0c0c0c) & 0x0c0c0c0c; 7092 uint32_t RHSUsedLanes = ~(RHSMask & 0x0c0c0c0c) & 0x0c0c0c0c; 7093 7094 // Check of we need to combine values from two sources within a byte. 7095 if (!(LHSUsedLanes & RHSUsedLanes) && 7096 // If we select high and lower word keep it for SDWA. 7097 // TODO: teach SDWA to work with v_perm_b32 and remove the check. 7098 !(LHSUsedLanes == 0x0c0c0000 && RHSUsedLanes == 0x00000c0c)) { 7099 // Each byte in each mask is either selector mask 0-3, or has higher 7100 // bits set in either of masks, which can be 0xff for 0xff or 0x0c for 7101 // zero. If 0x0c is in either mask it shall always be 0x0c. Otherwise 7102 // mask which is not 0xff wins. By anding both masks we have a correct 7103 // result except that 0x0c shall be corrected to give 0x0c only. 7104 uint32_t Mask = LHSMask & RHSMask; 7105 for (unsigned I = 0; I < 32; I += 8) { 7106 uint32_t ByteSel = 0xff << I; 7107 if ((LHSMask & ByteSel) == 0x0c || (RHSMask & ByteSel) == 0x0c) 7108 Mask &= (0x0c << I) & 0xffffffff; 7109 } 7110 7111 // Add 4 to each active LHS lane. It will not affect any existing 0xff 7112 // or 0x0c. 7113 uint32_t Sel = Mask | (LHSUsedLanes & 0x04040404); 7114 SDLoc DL(N); 7115 7116 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, 7117 LHS.getOperand(0), RHS.getOperand(0), 7118 DAG.getConstant(Sel, DL, MVT::i32)); 7119 } 7120 } 7121 } 7122 7123 return SDValue(); 7124 } 7125 7126 SDValue SITargetLowering::performOrCombine(SDNode *N, 7127 DAGCombinerInfo &DCI) const { 7128 SelectionDAG &DAG = DCI.DAG; 7129 SDValue LHS = N->getOperand(0); 7130 SDValue RHS = N->getOperand(1); 7131 7132 EVT VT = N->getValueType(0); 7133 if (VT == MVT::i1) { 7134 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2) 7135 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS && 7136 RHS.getOpcode() == AMDGPUISD::FP_CLASS) { 7137 SDValue Src = LHS.getOperand(0); 7138 if (Src != RHS.getOperand(0)) 7139 return SDValue(); 7140 7141 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); 7142 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); 7143 if (!CLHS || !CRHS) 7144 return SDValue(); 7145 7146 // Only 10 bits are used. 7147 static const uint32_t MaxMask = 0x3ff; 7148 7149 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask; 7150 SDLoc DL(N); 7151 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 7152 Src, DAG.getConstant(NewMask, DL, MVT::i32)); 7153 } 7154 7155 return SDValue(); 7156 } 7157 7158 // or (perm x, y, c1), c2 -> perm x, y, permute_mask(c1, c2) 7159 if (isa<ConstantSDNode>(RHS) && LHS.hasOneUse() && 7160 LHS.getOpcode() == AMDGPUISD::PERM && 7161 isa<ConstantSDNode>(LHS.getOperand(2))) { 7162 uint32_t Sel = getConstantPermuteMask(N->getConstantOperandVal(1)); 7163 if (!Sel) 7164 return SDValue(); 7165 7166 Sel |= LHS.getConstantOperandVal(2); 7167 SDLoc DL(N); 7168 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, LHS.getOperand(0), 7169 LHS.getOperand(1), DAG.getConstant(Sel, DL, MVT::i32)); 7170 } 7171 7172 // or (op x, c1), (op y, c2) -> perm x, y, permute_mask(c1, c2) 7173 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 7174 if (VT == MVT::i32 && LHS.hasOneUse() && RHS.hasOneUse() && 7175 N->isDivergent() && TII->pseudoToMCOpcode(AMDGPU::V_PERM_B32) != -1) { 7176 uint32_t LHSMask = getPermuteMask(DAG, LHS); 7177 uint32_t RHSMask = getPermuteMask(DAG, RHS); 7178 if (LHSMask != ~0u && RHSMask != ~0u) { 7179 // Canonicalize the expression in an attempt to have fewer unique masks 7180 // and therefore fewer registers used to hold the masks. 7181 if (LHSMask > RHSMask) { 7182 std::swap(LHSMask, RHSMask); 7183 std::swap(LHS, RHS); 7184 } 7185 7186 // Select 0xc for each lane used from source operand. Zero has 0xc mask 7187 // set, 0xff have 0xff in the mask, actual lanes are in the 0-3 range. 7188 uint32_t LHSUsedLanes = ~(LHSMask & 0x0c0c0c0c) & 0x0c0c0c0c; 7189 uint32_t RHSUsedLanes = ~(RHSMask & 0x0c0c0c0c) & 0x0c0c0c0c; 7190 7191 // Check of we need to combine values from two sources within a byte. 7192 if (!(LHSUsedLanes & RHSUsedLanes) && 7193 // If we select high and lower word keep it for SDWA. 7194 // TODO: teach SDWA to work with v_perm_b32 and remove the check. 7195 !(LHSUsedLanes == 0x0c0c0000 && RHSUsedLanes == 0x00000c0c)) { 7196 // Kill zero bytes selected by other mask. Zero value is 0xc. 7197 LHSMask &= ~RHSUsedLanes; 7198 RHSMask &= ~LHSUsedLanes; 7199 // Add 4 to each active LHS lane 7200 LHSMask |= LHSUsedLanes & 0x04040404; 7201 // Combine masks 7202 uint32_t Sel = LHSMask | RHSMask; 7203 SDLoc DL(N); 7204 7205 return DAG.getNode(AMDGPUISD::PERM, DL, MVT::i32, 7206 LHS.getOperand(0), RHS.getOperand(0), 7207 DAG.getConstant(Sel, DL, MVT::i32)); 7208 } 7209 } 7210 } 7211 7212 if (VT != MVT::i64) 7213 return SDValue(); 7214 7215 // TODO: This could be a generic combine with a predicate for extracting the 7216 // high half of an integer being free. 7217 7218 // (or i64:x, (zero_extend i32:y)) -> 7219 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x))) 7220 if (LHS.getOpcode() == ISD::ZERO_EXTEND && 7221 RHS.getOpcode() != ISD::ZERO_EXTEND) 7222 std::swap(LHS, RHS); 7223 7224 if (RHS.getOpcode() == ISD::ZERO_EXTEND) { 7225 SDValue ExtSrc = RHS.getOperand(0); 7226 EVT SrcVT = ExtSrc.getValueType(); 7227 if (SrcVT == MVT::i32) { 7228 SDLoc SL(N); 7229 SDValue LowLHS, HiBits; 7230 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG); 7231 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc); 7232 7233 DCI.AddToWorklist(LowOr.getNode()); 7234 DCI.AddToWorklist(HiBits.getNode()); 7235 7236 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 7237 LowOr, HiBits); 7238 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 7239 } 7240 } 7241 7242 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 7243 if (CRHS) { 7244 if (SDValue Split 7245 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS)) 7246 return Split; 7247 } 7248 7249 return SDValue(); 7250 } 7251 7252 SDValue SITargetLowering::performXorCombine(SDNode *N, 7253 DAGCombinerInfo &DCI) const { 7254 EVT VT = N->getValueType(0); 7255 if (VT != MVT::i64) 7256 return SDValue(); 7257 7258 SDValue LHS = N->getOperand(0); 7259 SDValue RHS = N->getOperand(1); 7260 7261 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); 7262 if (CRHS) { 7263 if (SDValue Split 7264 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS)) 7265 return Split; 7266 } 7267 7268 return SDValue(); 7269 } 7270 7271 // Instructions that will be lowered with a final instruction that zeros the 7272 // high result bits. 7273 // XXX - probably only need to list legal operations. 7274 static bool fp16SrcZerosHighBits(unsigned Opc) { 7275 switch (Opc) { 7276 case ISD::FADD: 7277 case ISD::FSUB: 7278 case ISD::FMUL: 7279 case ISD::FDIV: 7280 case ISD::FREM: 7281 case ISD::FMA: 7282 case ISD::FMAD: 7283 case ISD::FCANONICALIZE: 7284 case ISD::FP_ROUND: 7285 case ISD::UINT_TO_FP: 7286 case ISD::SINT_TO_FP: 7287 case ISD::FABS: 7288 // Fabs is lowered to a bit operation, but it's an and which will clear the 7289 // high bits anyway. 7290 case ISD::FSQRT: 7291 case ISD::FSIN: 7292 case ISD::FCOS: 7293 case ISD::FPOWI: 7294 case ISD::FPOW: 7295 case ISD::FLOG: 7296 case ISD::FLOG2: 7297 case ISD::FLOG10: 7298 case ISD::FEXP: 7299 case ISD::FEXP2: 7300 case ISD::FCEIL: 7301 case ISD::FTRUNC: 7302 case ISD::FRINT: 7303 case ISD::FNEARBYINT: 7304 case ISD::FROUND: 7305 case ISD::FFLOOR: 7306 case ISD::FMINNUM: 7307 case ISD::FMAXNUM: 7308 case AMDGPUISD::FRACT: 7309 case AMDGPUISD::CLAMP: 7310 case AMDGPUISD::COS_HW: 7311 case AMDGPUISD::SIN_HW: 7312 case AMDGPUISD::FMIN3: 7313 case AMDGPUISD::FMAX3: 7314 case AMDGPUISD::FMED3: 7315 case AMDGPUISD::FMAD_FTZ: 7316 case AMDGPUISD::RCP: 7317 case AMDGPUISD::RSQ: 7318 case AMDGPUISD::RCP_IFLAG: 7319 case AMDGPUISD::LDEXP: 7320 return true; 7321 default: 7322 // fcopysign, select and others may be lowered to 32-bit bit operations 7323 // which don't zero the high bits. 7324 return false; 7325 } 7326 } 7327 7328 SDValue SITargetLowering::performZeroExtendCombine(SDNode *N, 7329 DAGCombinerInfo &DCI) const { 7330 if (!Subtarget->has16BitInsts() || 7331 DCI.getDAGCombineLevel() < AfterLegalizeDAG) 7332 return SDValue(); 7333 7334 EVT VT = N->getValueType(0); 7335 if (VT != MVT::i32) 7336 return SDValue(); 7337 7338 SDValue Src = N->getOperand(0); 7339 if (Src.getValueType() != MVT::i16) 7340 return SDValue(); 7341 7342 // (i32 zext (i16 (bitcast f16:$src))) -> fp16_zext $src 7343 // FIXME: It is not universally true that the high bits are zeroed on gfx9. 7344 if (Src.getOpcode() == ISD::BITCAST) { 7345 SDValue BCSrc = Src.getOperand(0); 7346 if (BCSrc.getValueType() == MVT::f16 && 7347 fp16SrcZerosHighBits(BCSrc.getOpcode())) 7348 return DCI.DAG.getNode(AMDGPUISD::FP16_ZEXT, SDLoc(N), VT, BCSrc); 7349 } 7350 7351 return SDValue(); 7352 } 7353 7354 SDValue SITargetLowering::performClassCombine(SDNode *N, 7355 DAGCombinerInfo &DCI) const { 7356 SelectionDAG &DAG = DCI.DAG; 7357 SDValue Mask = N->getOperand(1); 7358 7359 // fp_class x, 0 -> false 7360 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) { 7361 if (CMask->isNullValue()) 7362 return DAG.getConstant(0, SDLoc(N), MVT::i1); 7363 } 7364 7365 if (N->getOperand(0).isUndef()) 7366 return DAG.getUNDEF(MVT::i1); 7367 7368 return SDValue(); 7369 } 7370 7371 SDValue SITargetLowering::performRcpCombine(SDNode *N, 7372 DAGCombinerInfo &DCI) const { 7373 EVT VT = N->getValueType(0); 7374 SDValue N0 = N->getOperand(0); 7375 7376 if (N0.isUndef()) 7377 return N0; 7378 7379 if (VT == MVT::f32 && (N0.getOpcode() == ISD::UINT_TO_FP || 7380 N0.getOpcode() == ISD::SINT_TO_FP)) { 7381 return DCI.DAG.getNode(AMDGPUISD::RCP_IFLAG, SDLoc(N), VT, N0, 7382 N->getFlags()); 7383 } 7384 7385 return AMDGPUTargetLowering::performRcpCombine(N, DCI); 7386 } 7387 7388 bool SITargetLowering::isCanonicalized(SelectionDAG &DAG, SDValue Op, 7389 unsigned MaxDepth) const { 7390 unsigned Opcode = Op.getOpcode(); 7391 if (Opcode == ISD::FCANONICALIZE) 7392 return true; 7393 7394 if (auto *CFP = dyn_cast<ConstantFPSDNode>(Op)) { 7395 auto F = CFP->getValueAPF(); 7396 if (F.isNaN() && F.isSignaling()) 7397 return false; 7398 return !F.isDenormal() || denormalsEnabledForType(Op.getValueType()); 7399 } 7400 7401 // If source is a result of another standard FP operation it is already in 7402 // canonical form. 7403 if (MaxDepth == 0) 7404 return false; 7405 7406 switch (Opcode) { 7407 // These will flush denorms if required. 7408 case ISD::FADD: 7409 case ISD::FSUB: 7410 case ISD::FMUL: 7411 case ISD::FCEIL: 7412 case ISD::FFLOOR: 7413 case ISD::FMA: 7414 case ISD::FMAD: 7415 case ISD::FSQRT: 7416 case ISD::FDIV: 7417 case ISD::FREM: 7418 case ISD::FP_ROUND: 7419 case ISD::FP_EXTEND: 7420 case AMDGPUISD::FMUL_LEGACY: 7421 case AMDGPUISD::FMAD_FTZ: 7422 case AMDGPUISD::RCP: 7423 case AMDGPUISD::RSQ: 7424 case AMDGPUISD::RSQ_CLAMP: 7425 case AMDGPUISD::RCP_LEGACY: 7426 case AMDGPUISD::RSQ_LEGACY: 7427 case AMDGPUISD::RCP_IFLAG: 7428 case AMDGPUISD::TRIG_PREOP: 7429 case AMDGPUISD::DIV_SCALE: 7430 case AMDGPUISD::DIV_FMAS: 7431 case AMDGPUISD::DIV_FIXUP: 7432 case AMDGPUISD::FRACT: 7433 case AMDGPUISD::LDEXP: 7434 case AMDGPUISD::CVT_PKRTZ_F16_F32: 7435 case AMDGPUISD::CVT_F32_UBYTE0: 7436 case AMDGPUISD::CVT_F32_UBYTE1: 7437 case AMDGPUISD::CVT_F32_UBYTE2: 7438 case AMDGPUISD::CVT_F32_UBYTE3: 7439 return true; 7440 7441 // It can/will be lowered or combined as a bit operation. 7442 // Need to check their input recursively to handle. 7443 case ISD::FNEG: 7444 case ISD::FABS: 7445 case ISD::FCOPYSIGN: 7446 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1); 7447 7448 case ISD::FSIN: 7449 case ISD::FCOS: 7450 case ISD::FSINCOS: 7451 return Op.getValueType().getScalarType() != MVT::f16; 7452 7453 case ISD::FMINNUM: 7454 case ISD::FMAXNUM: 7455 case AMDGPUISD::CLAMP: 7456 case AMDGPUISD::FMED3: 7457 case AMDGPUISD::FMAX3: 7458 case AMDGPUISD::FMIN3: { 7459 // FIXME: Shouldn't treat the generic operations different based these. 7460 bool IsIEEEMode = Subtarget->enableIEEEBit(DAG.getMachineFunction()); 7461 if (IsIEEEMode) { 7462 // snans will be quieted, so we only need to worry about denormals. 7463 if (Subtarget->supportsMinMaxDenormModes() || 7464 denormalsEnabledForType(Op.getValueType())) 7465 return true; 7466 7467 // Flushing may be required. 7468 // In pre-GFX9 targets V_MIN_F32 and others do not flush denorms. For such 7469 // targets need to check their input recursively. 7470 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1) && 7471 isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1); 7472 } 7473 7474 if (Subtarget->supportsMinMaxDenormModes() || 7475 denormalsEnabledForType(Op.getValueType())) { 7476 // Only quieting may be necessary. 7477 return DAG.isKnownNeverSNaN(Op.getOperand(0)) && 7478 DAG.isKnownNeverSNaN(Op.getOperand(1)); 7479 } 7480 7481 // Flushing and quieting may be necessary 7482 // With ieee_mode off, the nan is returned as-is, so if it is an sNaN it 7483 // needs to be quieted. 7484 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1) && 7485 isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1); 7486 } 7487 case ISD::SELECT: { 7488 return isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1) && 7489 isCanonicalized(DAG, Op.getOperand(2), MaxDepth - 1); 7490 } 7491 case ISD::BUILD_VECTOR: { 7492 for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) { 7493 SDValue SrcOp = Op.getOperand(i); 7494 if (!isCanonicalized(DAG, SrcOp, MaxDepth - 1)) 7495 return false; 7496 } 7497 7498 return true; 7499 } 7500 case ISD::EXTRACT_VECTOR_ELT: 7501 case ISD::EXTRACT_SUBVECTOR: { 7502 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1); 7503 } 7504 case ISD::INSERT_VECTOR_ELT: { 7505 return isCanonicalized(DAG, Op.getOperand(0), MaxDepth - 1) && 7506 isCanonicalized(DAG, Op.getOperand(1), MaxDepth - 1); 7507 } 7508 case ISD::UNDEF: 7509 // Could be anything. 7510 return false; 7511 7512 case ISD::INTRINSIC_WO_CHAIN: { 7513 unsigned IntrinsicID 7514 = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 7515 // TODO: Handle more intrinsics 7516 switch (IntrinsicID) { 7517 case Intrinsic::amdgcn_cvt_pkrtz: 7518 case Intrinsic::amdgcn_cubeid: 7519 case Intrinsic::amdgcn_frexp_mant: 7520 case Intrinsic::amdgcn_fdot2: 7521 return true; 7522 default: 7523 break; 7524 } 7525 7526 LLVM_FALLTHROUGH; 7527 } 7528 default: 7529 return denormalsEnabledForType(Op.getValueType()) && 7530 DAG.isKnownNeverSNaN(Op); 7531 } 7532 7533 llvm_unreachable("invalid operation"); 7534 } 7535 7536 // Constant fold canonicalize. 7537 7538 SDValue SITargetLowering::getCanonicalConstantFP( 7539 SelectionDAG &DAG, const SDLoc &SL, EVT VT, const APFloat &C) const { 7540 // Flush denormals to 0 if not enabled. 7541 if (C.isDenormal() && !denormalsEnabledForType(VT)) 7542 return DAG.getConstantFP(0.0, SL, VT); 7543 7544 if (C.isNaN()) { 7545 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics()); 7546 if (C.isSignaling()) { 7547 // Quiet a signaling NaN. 7548 // FIXME: Is this supposed to preserve payload bits? 7549 return DAG.getConstantFP(CanonicalQNaN, SL, VT); 7550 } 7551 7552 // Make sure it is the canonical NaN bitpattern. 7553 // 7554 // TODO: Can we use -1 as the canonical NaN value since it's an inline 7555 // immediate? 7556 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt()) 7557 return DAG.getConstantFP(CanonicalQNaN, SL, VT); 7558 } 7559 7560 // Already canonical. 7561 return DAG.getConstantFP(C, SL, VT); 7562 } 7563 7564 static bool vectorEltWillFoldAway(SDValue Op) { 7565 return Op.isUndef() || isa<ConstantFPSDNode>(Op); 7566 } 7567 7568 SDValue SITargetLowering::performFCanonicalizeCombine( 7569 SDNode *N, 7570 DAGCombinerInfo &DCI) const { 7571 SelectionDAG &DAG = DCI.DAG; 7572 SDValue N0 = N->getOperand(0); 7573 EVT VT = N->getValueType(0); 7574 7575 // fcanonicalize undef -> qnan 7576 if (N0.isUndef()) { 7577 APFloat QNaN = APFloat::getQNaN(SelectionDAG::EVTToAPFloatSemantics(VT)); 7578 return DAG.getConstantFP(QNaN, SDLoc(N), VT); 7579 } 7580 7581 if (ConstantFPSDNode *CFP = isConstOrConstSplatFP(N0)) { 7582 EVT VT = N->getValueType(0); 7583 return getCanonicalConstantFP(DAG, SDLoc(N), VT, CFP->getValueAPF()); 7584 } 7585 7586 // fcanonicalize (build_vector x, k) -> build_vector (fcanonicalize x), 7587 // (fcanonicalize k) 7588 // 7589 // fcanonicalize (build_vector x, undef) -> build_vector (fcanonicalize x), 0 7590 7591 // TODO: This could be better with wider vectors that will be split to v2f16, 7592 // and to consider uses since there aren't that many packed operations. 7593 if (N0.getOpcode() == ISD::BUILD_VECTOR && VT == MVT::v2f16 && 7594 isTypeLegal(MVT::v2f16)) { 7595 SDLoc SL(N); 7596 SDValue NewElts[2]; 7597 SDValue Lo = N0.getOperand(0); 7598 SDValue Hi = N0.getOperand(1); 7599 EVT EltVT = Lo.getValueType(); 7600 7601 if (vectorEltWillFoldAway(Lo) || vectorEltWillFoldAway(Hi)) { 7602 for (unsigned I = 0; I != 2; ++I) { 7603 SDValue Op = N0.getOperand(I); 7604 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op)) { 7605 NewElts[I] = getCanonicalConstantFP(DAG, SL, EltVT, 7606 CFP->getValueAPF()); 7607 } else if (Op.isUndef()) { 7608 // Handled below based on what the other operand is. 7609 NewElts[I] = Op; 7610 } else { 7611 NewElts[I] = DAG.getNode(ISD::FCANONICALIZE, SL, EltVT, Op); 7612 } 7613 } 7614 7615 // If one half is undef, and one is constant, perfer a splat vector rather 7616 // than the normal qNaN. If it's a register, prefer 0.0 since that's 7617 // cheaper to use and may be free with a packed operation. 7618 if (NewElts[0].isUndef()) { 7619 if (isa<ConstantFPSDNode>(NewElts[1])) 7620 NewElts[0] = isa<ConstantFPSDNode>(NewElts[1]) ? 7621 NewElts[1]: DAG.getConstantFP(0.0f, SL, EltVT); 7622 } 7623 7624 if (NewElts[1].isUndef()) { 7625 NewElts[1] = isa<ConstantFPSDNode>(NewElts[0]) ? 7626 NewElts[0] : DAG.getConstantFP(0.0f, SL, EltVT); 7627 } 7628 7629 return DAG.getBuildVector(VT, SL, NewElts); 7630 } 7631 } 7632 7633 return isCanonicalized(DAG, N0) ? N0 : SDValue(); 7634 } 7635 7636 static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) { 7637 switch (Opc) { 7638 case ISD::FMAXNUM: 7639 return AMDGPUISD::FMAX3; 7640 case ISD::SMAX: 7641 return AMDGPUISD::SMAX3; 7642 case ISD::UMAX: 7643 return AMDGPUISD::UMAX3; 7644 case ISD::FMINNUM: 7645 return AMDGPUISD::FMIN3; 7646 case ISD::SMIN: 7647 return AMDGPUISD::SMIN3; 7648 case ISD::UMIN: 7649 return AMDGPUISD::UMIN3; 7650 default: 7651 llvm_unreachable("Not a min/max opcode"); 7652 } 7653 } 7654 7655 SDValue SITargetLowering::performIntMed3ImmCombine( 7656 SelectionDAG &DAG, const SDLoc &SL, 7657 SDValue Op0, SDValue Op1, bool Signed) const { 7658 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1); 7659 if (!K1) 7660 return SDValue(); 7661 7662 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1)); 7663 if (!K0) 7664 return SDValue(); 7665 7666 if (Signed) { 7667 if (K0->getAPIntValue().sge(K1->getAPIntValue())) 7668 return SDValue(); 7669 } else { 7670 if (K0->getAPIntValue().uge(K1->getAPIntValue())) 7671 return SDValue(); 7672 } 7673 7674 EVT VT = K0->getValueType(0); 7675 unsigned Med3Opc = Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3; 7676 if (VT == MVT::i32 || (VT == MVT::i16 && Subtarget->hasMed3_16())) { 7677 return DAG.getNode(Med3Opc, SL, VT, 7678 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0)); 7679 } 7680 7681 // If there isn't a 16-bit med3 operation, convert to 32-bit. 7682 MVT NVT = MVT::i32; 7683 unsigned ExtOp = Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 7684 7685 SDValue Tmp1 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(0)); 7686 SDValue Tmp2 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(1)); 7687 SDValue Tmp3 = DAG.getNode(ExtOp, SL, NVT, Op1); 7688 7689 SDValue Med3 = DAG.getNode(Med3Opc, SL, NVT, Tmp1, Tmp2, Tmp3); 7690 return DAG.getNode(ISD::TRUNCATE, SL, VT, Med3); 7691 } 7692 7693 static ConstantFPSDNode *getSplatConstantFP(SDValue Op) { 7694 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Op)) 7695 return C; 7696 7697 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Op)) { 7698 if (ConstantFPSDNode *C = BV->getConstantFPSplatNode()) 7699 return C; 7700 } 7701 7702 return nullptr; 7703 } 7704 7705 SDValue SITargetLowering::performFPMed3ImmCombine(SelectionDAG &DAG, 7706 const SDLoc &SL, 7707 SDValue Op0, 7708 SDValue Op1) const { 7709 ConstantFPSDNode *K1 = getSplatConstantFP(Op1); 7710 if (!K1) 7711 return SDValue(); 7712 7713 ConstantFPSDNode *K0 = getSplatConstantFP(Op0.getOperand(1)); 7714 if (!K0) 7715 return SDValue(); 7716 7717 // Ordered >= (although NaN inputs should have folded away by now). 7718 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF()); 7719 if (Cmp == APFloat::cmpGreaterThan) 7720 return SDValue(); 7721 7722 // TODO: Check IEEE bit enabled? 7723 EVT VT = Op0.getValueType(); 7724 if (Subtarget->enableDX10Clamp()) { 7725 // If dx10_clamp is enabled, NaNs clamp to 0.0. This is the same as the 7726 // hardware fmed3 behavior converting to a min. 7727 // FIXME: Should this be allowing -0.0? 7728 if (K1->isExactlyValue(1.0) && K0->isExactlyValue(0.0)) 7729 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Op0.getOperand(0)); 7730 } 7731 7732 // med3 for f16 is only available on gfx9+, and not available for v2f16. 7733 if (VT == MVT::f32 || (VT == MVT::f16 && Subtarget->hasMed3_16())) { 7734 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a 7735 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would 7736 // then give the other result, which is different from med3 with a NaN 7737 // input. 7738 SDValue Var = Op0.getOperand(0); 7739 if (!DAG.isKnownNeverSNaN(Var)) 7740 return SDValue(); 7741 7742 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0), 7743 Var, SDValue(K0, 0), SDValue(K1, 0)); 7744 } 7745 7746 return SDValue(); 7747 } 7748 7749 SDValue SITargetLowering::performMinMaxCombine(SDNode *N, 7750 DAGCombinerInfo &DCI) const { 7751 SelectionDAG &DAG = DCI.DAG; 7752 7753 EVT VT = N->getValueType(0); 7754 unsigned Opc = N->getOpcode(); 7755 SDValue Op0 = N->getOperand(0); 7756 SDValue Op1 = N->getOperand(1); 7757 7758 // Only do this if the inner op has one use since this will just increases 7759 // register pressure for no benefit. 7760 7761 7762 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY && 7763 !VT.isVector() && VT != MVT::f64 && 7764 ((VT != MVT::f16 && VT != MVT::i16) || Subtarget->hasMin3Max3_16())) { 7765 // max(max(a, b), c) -> max3(a, b, c) 7766 // min(min(a, b), c) -> min3(a, b, c) 7767 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) { 7768 SDLoc DL(N); 7769 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 7770 DL, 7771 N->getValueType(0), 7772 Op0.getOperand(0), 7773 Op0.getOperand(1), 7774 Op1); 7775 } 7776 7777 // Try commuted. 7778 // max(a, max(b, c)) -> max3(a, b, c) 7779 // min(a, min(b, c)) -> min3(a, b, c) 7780 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) { 7781 SDLoc DL(N); 7782 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 7783 DL, 7784 N->getValueType(0), 7785 Op0, 7786 Op1.getOperand(0), 7787 Op1.getOperand(1)); 7788 } 7789 } 7790 7791 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1) 7792 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) { 7793 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true)) 7794 return Med3; 7795 } 7796 7797 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) { 7798 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false)) 7799 return Med3; 7800 } 7801 7802 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1) 7803 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) || 7804 (Opc == AMDGPUISD::FMIN_LEGACY && 7805 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) && 7806 (VT == MVT::f32 || VT == MVT::f64 || 7807 (VT == MVT::f16 && Subtarget->has16BitInsts()) || 7808 (VT == MVT::v2f16 && Subtarget->hasVOP3PInsts())) && 7809 Op0.hasOneUse()) { 7810 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1)) 7811 return Res; 7812 } 7813 7814 return SDValue(); 7815 } 7816 7817 static bool isClampZeroToOne(SDValue A, SDValue B) { 7818 if (ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A)) { 7819 if (ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B)) { 7820 // FIXME: Should this be allowing -0.0? 7821 return (CA->isExactlyValue(0.0) && CB->isExactlyValue(1.0)) || 7822 (CA->isExactlyValue(1.0) && CB->isExactlyValue(0.0)); 7823 } 7824 } 7825 7826 return false; 7827 } 7828 7829 // FIXME: Should only worry about snans for version with chain. 7830 SDValue SITargetLowering::performFMed3Combine(SDNode *N, 7831 DAGCombinerInfo &DCI) const { 7832 EVT VT = N->getValueType(0); 7833 // v_med3_f32 and v_max_f32 behave identically wrt denorms, exceptions and 7834 // NaNs. With a NaN input, the order of the operands may change the result. 7835 7836 SelectionDAG &DAG = DCI.DAG; 7837 SDLoc SL(N); 7838 7839 SDValue Src0 = N->getOperand(0); 7840 SDValue Src1 = N->getOperand(1); 7841 SDValue Src2 = N->getOperand(2); 7842 7843 if (isClampZeroToOne(Src0, Src1)) { 7844 // const_a, const_b, x -> clamp is safe in all cases including signaling 7845 // nans. 7846 // FIXME: Should this be allowing -0.0? 7847 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src2); 7848 } 7849 7850 // FIXME: dx10_clamp behavior assumed in instcombine. Should we really bother 7851 // handling no dx10-clamp? 7852 if (Subtarget->enableDX10Clamp()) { 7853 // If NaNs is clamped to 0, we are free to reorder the inputs. 7854 7855 if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1)) 7856 std::swap(Src0, Src1); 7857 7858 if (isa<ConstantFPSDNode>(Src1) && !isa<ConstantFPSDNode>(Src2)) 7859 std::swap(Src1, Src2); 7860 7861 if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1)) 7862 std::swap(Src0, Src1); 7863 7864 if (isClampZeroToOne(Src1, Src2)) 7865 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src0); 7866 } 7867 7868 return SDValue(); 7869 } 7870 7871 SDValue SITargetLowering::performCvtPkRTZCombine(SDNode *N, 7872 DAGCombinerInfo &DCI) const { 7873 SDValue Src0 = N->getOperand(0); 7874 SDValue Src1 = N->getOperand(1); 7875 if (Src0.isUndef() && Src1.isUndef()) 7876 return DCI.DAG.getUNDEF(N->getValueType(0)); 7877 return SDValue(); 7878 } 7879 7880 SDValue SITargetLowering::performExtractVectorEltCombine( 7881 SDNode *N, DAGCombinerInfo &DCI) const { 7882 SDValue Vec = N->getOperand(0); 7883 SelectionDAG &DAG = DCI.DAG; 7884 7885 EVT VecVT = Vec.getValueType(); 7886 EVT EltVT = VecVT.getVectorElementType(); 7887 7888 if ((Vec.getOpcode() == ISD::FNEG || 7889 Vec.getOpcode() == ISD::FABS) && allUsesHaveSourceMods(N)) { 7890 SDLoc SL(N); 7891 EVT EltVT = N->getValueType(0); 7892 SDValue Idx = N->getOperand(1); 7893 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, 7894 Vec.getOperand(0), Idx); 7895 return DAG.getNode(Vec.getOpcode(), SL, EltVT, Elt); 7896 } 7897 7898 // ScalarRes = EXTRACT_VECTOR_ELT ((vector-BINOP Vec1, Vec2), Idx) 7899 // => 7900 // Vec1Elt = EXTRACT_VECTOR_ELT(Vec1, Idx) 7901 // Vec2Elt = EXTRACT_VECTOR_ELT(Vec2, Idx) 7902 // ScalarRes = scalar-BINOP Vec1Elt, Vec2Elt 7903 if (Vec.hasOneUse() && DCI.isBeforeLegalize()) { 7904 SDLoc SL(N); 7905 EVT EltVT = N->getValueType(0); 7906 SDValue Idx = N->getOperand(1); 7907 unsigned Opc = Vec.getOpcode(); 7908 7909 switch(Opc) { 7910 default: 7911 return SDValue(); 7912 // TODO: Support other binary operations. 7913 case ISD::FADD: 7914 case ISD::FSUB: 7915 case ISD::FMUL: 7916 case ISD::ADD: 7917 case ISD::UMIN: 7918 case ISD::UMAX: 7919 case ISD::SMIN: 7920 case ISD::SMAX: 7921 case ISD::FMAXNUM: 7922 case ISD::FMINNUM: { 7923 SDValue Elt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, 7924 Vec.getOperand(0), Idx); 7925 SDValue Elt1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT, 7926 Vec.getOperand(1), Idx); 7927 7928 DCI.AddToWorklist(Elt0.getNode()); 7929 DCI.AddToWorklist(Elt1.getNode()); 7930 return DAG.getNode(Opc, SL, EltVT, Elt0, Elt1, Vec->getFlags()); 7931 } 7932 } 7933 } 7934 7935 if (!DCI.isBeforeLegalize()) 7936 return SDValue(); 7937 7938 unsigned VecSize = VecVT.getSizeInBits(); 7939 unsigned EltSize = EltVT.getSizeInBits(); 7940 7941 // Try to turn sub-dword accesses of vectors into accesses of the same 32-bit 7942 // elements. This exposes more load reduction opportunities by replacing 7943 // multiple small extract_vector_elements with a single 32-bit extract. 7944 auto *Idx = dyn_cast<ConstantSDNode>(N->getOperand(1)); 7945 if (EltSize <= 16 && 7946 EltVT.isByteSized() && 7947 VecSize > 32 && 7948 VecSize % 32 == 0 && 7949 Idx) { 7950 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VecVT); 7951 7952 unsigned BitIndex = Idx->getZExtValue() * EltSize; 7953 unsigned EltIdx = BitIndex / 32; 7954 unsigned LeftoverBitIdx = BitIndex % 32; 7955 SDLoc SL(N); 7956 7957 SDValue Cast = DAG.getNode(ISD::BITCAST, SL, NewVT, Vec); 7958 DCI.AddToWorklist(Cast.getNode()); 7959 7960 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Cast, 7961 DAG.getConstant(EltIdx, SL, MVT::i32)); 7962 DCI.AddToWorklist(Elt.getNode()); 7963 SDValue Srl = DAG.getNode(ISD::SRL, SL, MVT::i32, Elt, 7964 DAG.getConstant(LeftoverBitIdx, SL, MVT::i32)); 7965 DCI.AddToWorklist(Srl.getNode()); 7966 7967 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SL, EltVT.changeTypeToInteger(), Srl); 7968 DCI.AddToWorklist(Trunc.getNode()); 7969 return DAG.getNode(ISD::BITCAST, SL, EltVT, Trunc); 7970 } 7971 7972 return SDValue(); 7973 } 7974 7975 static bool convertBuildVectorCastElt(SelectionDAG &DAG, 7976 SDValue &Lo, SDValue &Hi) { 7977 if (Hi.getOpcode() == ISD::BITCAST && 7978 Hi.getOperand(0).getValueType() == MVT::f16 && 7979 (isa<ConstantSDNode>(Lo) || Lo.isUndef())) { 7980 Lo = DAG.getNode(ISD::BITCAST, SDLoc(Lo), MVT::f16, Lo); 7981 Hi = Hi.getOperand(0); 7982 return true; 7983 } 7984 7985 return false; 7986 } 7987 7988 SDValue SITargetLowering::performBuildVectorCombine( 7989 SDNode *N, DAGCombinerInfo &DCI) const { 7990 SDLoc SL(N); 7991 7992 if (!isTypeLegal(MVT::v2i16)) 7993 return SDValue(); 7994 SelectionDAG &DAG = DCI.DAG; 7995 EVT VT = N->getValueType(0); 7996 7997 if (VT == MVT::v2i16) { 7998 SDValue Lo = N->getOperand(0); 7999 SDValue Hi = N->getOperand(1); 8000 8001 // v2i16 build_vector (const|undef), (bitcast f16:$x) 8002 // -> bitcast (v2f16 build_vector const|undef, $x 8003 if (convertBuildVectorCastElt(DAG, Lo, Hi)) { 8004 SDValue NewVec = DAG.getBuildVector(MVT::v2f16, SL, { Lo, Hi }); 8005 return DAG.getNode(ISD::BITCAST, SL, VT, NewVec); 8006 } 8007 8008 if (convertBuildVectorCastElt(DAG, Hi, Lo)) { 8009 SDValue NewVec = DAG.getBuildVector(MVT::v2f16, SL, { Hi, Lo }); 8010 return DAG.getNode(ISD::BITCAST, SL, VT, NewVec); 8011 } 8012 } 8013 8014 return SDValue(); 8015 } 8016 8017 unsigned SITargetLowering::getFusedOpcode(const SelectionDAG &DAG, 8018 const SDNode *N0, 8019 const SDNode *N1) const { 8020 EVT VT = N0->getValueType(0); 8021 8022 // Only do this if we are not trying to support denormals. v_mad_f32 does not 8023 // support denormals ever. 8024 if ((VT == MVT::f32 && !Subtarget->hasFP32Denormals()) || 8025 (VT == MVT::f16 && !Subtarget->hasFP16Denormals())) 8026 return ISD::FMAD; 8027 8028 const TargetOptions &Options = DAG.getTarget().Options; 8029 if ((Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath || 8030 (N0->getFlags().hasAllowContract() && 8031 N1->getFlags().hasAllowContract())) && 8032 isFMAFasterThanFMulAndFAdd(VT)) { 8033 return ISD::FMA; 8034 } 8035 8036 return 0; 8037 } 8038 8039 static SDValue getMad64_32(SelectionDAG &DAG, const SDLoc &SL, 8040 EVT VT, 8041 SDValue N0, SDValue N1, SDValue N2, 8042 bool Signed) { 8043 unsigned MadOpc = Signed ? AMDGPUISD::MAD_I64_I32 : AMDGPUISD::MAD_U64_U32; 8044 SDVTList VTs = DAG.getVTList(MVT::i64, MVT::i1); 8045 SDValue Mad = DAG.getNode(MadOpc, SL, VTs, N0, N1, N2); 8046 return DAG.getNode(ISD::TRUNCATE, SL, VT, Mad); 8047 } 8048 8049 SDValue SITargetLowering::performAddCombine(SDNode *N, 8050 DAGCombinerInfo &DCI) const { 8051 SelectionDAG &DAG = DCI.DAG; 8052 EVT VT = N->getValueType(0); 8053 SDLoc SL(N); 8054 SDValue LHS = N->getOperand(0); 8055 SDValue RHS = N->getOperand(1); 8056 8057 if ((LHS.getOpcode() == ISD::MUL || RHS.getOpcode() == ISD::MUL) 8058 && Subtarget->hasMad64_32() && 8059 !VT.isVector() && VT.getScalarSizeInBits() > 32 && 8060 VT.getScalarSizeInBits() <= 64) { 8061 if (LHS.getOpcode() != ISD::MUL) 8062 std::swap(LHS, RHS); 8063 8064 SDValue MulLHS = LHS.getOperand(0); 8065 SDValue MulRHS = LHS.getOperand(1); 8066 SDValue AddRHS = RHS; 8067 8068 // TODO: Maybe restrict if SGPR inputs. 8069 if (numBitsUnsigned(MulLHS, DAG) <= 32 && 8070 numBitsUnsigned(MulRHS, DAG) <= 32) { 8071 MulLHS = DAG.getZExtOrTrunc(MulLHS, SL, MVT::i32); 8072 MulRHS = DAG.getZExtOrTrunc(MulRHS, SL, MVT::i32); 8073 AddRHS = DAG.getZExtOrTrunc(AddRHS, SL, MVT::i64); 8074 return getMad64_32(DAG, SL, VT, MulLHS, MulRHS, AddRHS, false); 8075 } 8076 8077 if (numBitsSigned(MulLHS, DAG) < 32 && numBitsSigned(MulRHS, DAG) < 32) { 8078 MulLHS = DAG.getSExtOrTrunc(MulLHS, SL, MVT::i32); 8079 MulRHS = DAG.getSExtOrTrunc(MulRHS, SL, MVT::i32); 8080 AddRHS = DAG.getSExtOrTrunc(AddRHS, SL, MVT::i64); 8081 return getMad64_32(DAG, SL, VT, MulLHS, MulRHS, AddRHS, true); 8082 } 8083 8084 return SDValue(); 8085 } 8086 8087 if (VT != MVT::i32 || !DCI.isAfterLegalizeDAG()) 8088 return SDValue(); 8089 8090 // add x, zext (setcc) => addcarry x, 0, setcc 8091 // add x, sext (setcc) => subcarry x, 0, setcc 8092 unsigned Opc = LHS.getOpcode(); 8093 if (Opc == ISD::ZERO_EXTEND || Opc == ISD::SIGN_EXTEND || 8094 Opc == ISD::ANY_EXTEND || Opc == ISD::ADDCARRY) 8095 std::swap(RHS, LHS); 8096 8097 Opc = RHS.getOpcode(); 8098 switch (Opc) { 8099 default: break; 8100 case ISD::ZERO_EXTEND: 8101 case ISD::SIGN_EXTEND: 8102 case ISD::ANY_EXTEND: { 8103 auto Cond = RHS.getOperand(0); 8104 if (!isBoolSGPR(Cond)) 8105 break; 8106 SDVTList VTList = DAG.getVTList(MVT::i32, MVT::i1); 8107 SDValue Args[] = { LHS, DAG.getConstant(0, SL, MVT::i32), Cond }; 8108 Opc = (Opc == ISD::SIGN_EXTEND) ? ISD::SUBCARRY : ISD::ADDCARRY; 8109 return DAG.getNode(Opc, SL, VTList, Args); 8110 } 8111 case ISD::ADDCARRY: { 8112 // add x, (addcarry y, 0, cc) => addcarry x, y, cc 8113 auto C = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); 8114 if (!C || C->getZExtValue() != 0) break; 8115 SDValue Args[] = { LHS, RHS.getOperand(0), RHS.getOperand(2) }; 8116 return DAG.getNode(ISD::ADDCARRY, SDLoc(N), RHS->getVTList(), Args); 8117 } 8118 } 8119 return SDValue(); 8120 } 8121 8122 SDValue SITargetLowering::performSubCombine(SDNode *N, 8123 DAGCombinerInfo &DCI) const { 8124 SelectionDAG &DAG = DCI.DAG; 8125 EVT VT = N->getValueType(0); 8126 8127 if (VT != MVT::i32) 8128 return SDValue(); 8129 8130 SDLoc SL(N); 8131 SDValue LHS = N->getOperand(0); 8132 SDValue RHS = N->getOperand(1); 8133 8134 unsigned Opc = LHS.getOpcode(); 8135 if (Opc != ISD::SUBCARRY) 8136 std::swap(RHS, LHS); 8137 8138 if (LHS.getOpcode() == ISD::SUBCARRY) { 8139 // sub (subcarry x, 0, cc), y => subcarry x, y, cc 8140 auto C = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); 8141 if (!C || C->getZExtValue() != 0) 8142 return SDValue(); 8143 SDValue Args[] = { LHS.getOperand(0), RHS, LHS.getOperand(2) }; 8144 return DAG.getNode(ISD::SUBCARRY, SDLoc(N), LHS->getVTList(), Args); 8145 } 8146 return SDValue(); 8147 } 8148 8149 SDValue SITargetLowering::performAddCarrySubCarryCombine(SDNode *N, 8150 DAGCombinerInfo &DCI) const { 8151 8152 if (N->getValueType(0) != MVT::i32) 8153 return SDValue(); 8154 8155 auto C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 8156 if (!C || C->getZExtValue() != 0) 8157 return SDValue(); 8158 8159 SelectionDAG &DAG = DCI.DAG; 8160 SDValue LHS = N->getOperand(0); 8161 8162 // addcarry (add x, y), 0, cc => addcarry x, y, cc 8163 // subcarry (sub x, y), 0, cc => subcarry x, y, cc 8164 unsigned LHSOpc = LHS.getOpcode(); 8165 unsigned Opc = N->getOpcode(); 8166 if ((LHSOpc == ISD::ADD && Opc == ISD::ADDCARRY) || 8167 (LHSOpc == ISD::SUB && Opc == ISD::SUBCARRY)) { 8168 SDValue Args[] = { LHS.getOperand(0), LHS.getOperand(1), N->getOperand(2) }; 8169 return DAG.getNode(Opc, SDLoc(N), N->getVTList(), Args); 8170 } 8171 return SDValue(); 8172 } 8173 8174 SDValue SITargetLowering::performFAddCombine(SDNode *N, 8175 DAGCombinerInfo &DCI) const { 8176 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 8177 return SDValue(); 8178 8179 SelectionDAG &DAG = DCI.DAG; 8180 EVT VT = N->getValueType(0); 8181 8182 SDLoc SL(N); 8183 SDValue LHS = N->getOperand(0); 8184 SDValue RHS = N->getOperand(1); 8185 8186 // These should really be instruction patterns, but writing patterns with 8187 // source modiifiers is a pain. 8188 8189 // fadd (fadd (a, a), b) -> mad 2.0, a, b 8190 if (LHS.getOpcode() == ISD::FADD) { 8191 SDValue A = LHS.getOperand(0); 8192 if (A == LHS.getOperand(1)) { 8193 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode()); 8194 if (FusedOp != 0) { 8195 const SDValue Two = DAG.getConstantFP(2.0, SL, VT); 8196 return DAG.getNode(FusedOp, SL, VT, A, Two, RHS); 8197 } 8198 } 8199 } 8200 8201 // fadd (b, fadd (a, a)) -> mad 2.0, a, b 8202 if (RHS.getOpcode() == ISD::FADD) { 8203 SDValue A = RHS.getOperand(0); 8204 if (A == RHS.getOperand(1)) { 8205 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode()); 8206 if (FusedOp != 0) { 8207 const SDValue Two = DAG.getConstantFP(2.0, SL, VT); 8208 return DAG.getNode(FusedOp, SL, VT, A, Two, LHS); 8209 } 8210 } 8211 } 8212 8213 return SDValue(); 8214 } 8215 8216 SDValue SITargetLowering::performFSubCombine(SDNode *N, 8217 DAGCombinerInfo &DCI) const { 8218 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 8219 return SDValue(); 8220 8221 SelectionDAG &DAG = DCI.DAG; 8222 SDLoc SL(N); 8223 EVT VT = N->getValueType(0); 8224 assert(!VT.isVector()); 8225 8226 // Try to get the fneg to fold into the source modifier. This undoes generic 8227 // DAG combines and folds them into the mad. 8228 // 8229 // Only do this if we are not trying to support denormals. v_mad_f32 does 8230 // not support denormals ever. 8231 SDValue LHS = N->getOperand(0); 8232 SDValue RHS = N->getOperand(1); 8233 if (LHS.getOpcode() == ISD::FADD) { 8234 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c) 8235 SDValue A = LHS.getOperand(0); 8236 if (A == LHS.getOperand(1)) { 8237 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode()); 8238 if (FusedOp != 0){ 8239 const SDValue Two = DAG.getConstantFP(2.0, SL, VT); 8240 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 8241 8242 return DAG.getNode(FusedOp, SL, VT, A, Two, NegRHS); 8243 } 8244 } 8245 } 8246 8247 if (RHS.getOpcode() == ISD::FADD) { 8248 // (fsub c, (fadd a, a)) -> mad -2.0, a, c 8249 8250 SDValue A = RHS.getOperand(0); 8251 if (A == RHS.getOperand(1)) { 8252 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode()); 8253 if (FusedOp != 0){ 8254 const SDValue NegTwo = DAG.getConstantFP(-2.0, SL, VT); 8255 return DAG.getNode(FusedOp, SL, VT, A, NegTwo, LHS); 8256 } 8257 } 8258 } 8259 8260 return SDValue(); 8261 } 8262 8263 SDValue SITargetLowering::performFMACombine(SDNode *N, 8264 DAGCombinerInfo &DCI) const { 8265 SelectionDAG &DAG = DCI.DAG; 8266 EVT VT = N->getValueType(0); 8267 SDLoc SL(N); 8268 8269 if (!Subtarget->hasDLInsts() || VT != MVT::f32) 8270 return SDValue(); 8271 8272 // FMA((F32)S0.x, (F32)S1. x, FMA((F32)S0.y, (F32)S1.y, (F32)z)) -> 8273 // FDOT2((V2F16)S0, (V2F16)S1, (F32)z)) 8274 SDValue Op1 = N->getOperand(0); 8275 SDValue Op2 = N->getOperand(1); 8276 SDValue FMA = N->getOperand(2); 8277 8278 if (FMA.getOpcode() != ISD::FMA || 8279 Op1.getOpcode() != ISD::FP_EXTEND || 8280 Op2.getOpcode() != ISD::FP_EXTEND) 8281 return SDValue(); 8282 8283 // fdot2_f32_f16 always flushes fp32 denormal operand and output to zero, 8284 // regardless of the denorm mode setting. Therefore, unsafe-fp-math/fp-contract 8285 // is sufficient to allow generaing fdot2. 8286 const TargetOptions &Options = DAG.getTarget().Options; 8287 if (Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath || 8288 (N->getFlags().hasAllowContract() && 8289 FMA->getFlags().hasAllowContract())) { 8290 Op1 = Op1.getOperand(0); 8291 Op2 = Op2.getOperand(0); 8292 if (Op1.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 8293 Op2.getOpcode() != ISD::EXTRACT_VECTOR_ELT) 8294 return SDValue(); 8295 8296 SDValue Vec1 = Op1.getOperand(0); 8297 SDValue Idx1 = Op1.getOperand(1); 8298 SDValue Vec2 = Op2.getOperand(0); 8299 8300 SDValue FMAOp1 = FMA.getOperand(0); 8301 SDValue FMAOp2 = FMA.getOperand(1); 8302 SDValue FMAAcc = FMA.getOperand(2); 8303 8304 if (FMAOp1.getOpcode() != ISD::FP_EXTEND || 8305 FMAOp2.getOpcode() != ISD::FP_EXTEND) 8306 return SDValue(); 8307 8308 FMAOp1 = FMAOp1.getOperand(0); 8309 FMAOp2 = FMAOp2.getOperand(0); 8310 if (FMAOp1.getOpcode() != ISD::EXTRACT_VECTOR_ELT || 8311 FMAOp2.getOpcode() != ISD::EXTRACT_VECTOR_ELT) 8312 return SDValue(); 8313 8314 SDValue Vec3 = FMAOp1.getOperand(0); 8315 SDValue Vec4 = FMAOp2.getOperand(0); 8316 SDValue Idx2 = FMAOp1.getOperand(1); 8317 8318 if (Idx1 != Op2.getOperand(1) || Idx2 != FMAOp2.getOperand(1) || 8319 // Idx1 and Idx2 cannot be the same. 8320 Idx1 == Idx2) 8321 return SDValue(); 8322 8323 if (Vec1 == Vec2 || Vec3 == Vec4) 8324 return SDValue(); 8325 8326 if (Vec1.getValueType() != MVT::v2f16 || Vec2.getValueType() != MVT::v2f16) 8327 return SDValue(); 8328 8329 if ((Vec1 == Vec3 && Vec2 == Vec4) || 8330 (Vec1 == Vec4 && Vec2 == Vec3)) { 8331 return DAG.getNode(AMDGPUISD::FDOT2, SL, MVT::f32, Vec1, Vec2, FMAAcc, 8332 DAG.getTargetConstant(0, SL, MVT::i1)); 8333 } 8334 } 8335 return SDValue(); 8336 } 8337 8338 SDValue SITargetLowering::performSetCCCombine(SDNode *N, 8339 DAGCombinerInfo &DCI) const { 8340 SelectionDAG &DAG = DCI.DAG; 8341 SDLoc SL(N); 8342 8343 SDValue LHS = N->getOperand(0); 8344 SDValue RHS = N->getOperand(1); 8345 EVT VT = LHS.getValueType(); 8346 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 8347 8348 auto CRHS = dyn_cast<ConstantSDNode>(RHS); 8349 if (!CRHS) { 8350 CRHS = dyn_cast<ConstantSDNode>(LHS); 8351 if (CRHS) { 8352 std::swap(LHS, RHS); 8353 CC = getSetCCSwappedOperands(CC); 8354 } 8355 } 8356 8357 if (CRHS) { 8358 if (VT == MVT::i32 && LHS.getOpcode() == ISD::SIGN_EXTEND && 8359 isBoolSGPR(LHS.getOperand(0))) { 8360 // setcc (sext from i1 cc), -1, ne|sgt|ult) => not cc => xor cc, -1 8361 // setcc (sext from i1 cc), -1, eq|sle|uge) => cc 8362 // setcc (sext from i1 cc), 0, eq|sge|ule) => not cc => xor cc, -1 8363 // setcc (sext from i1 cc), 0, ne|ugt|slt) => cc 8364 if ((CRHS->isAllOnesValue() && 8365 (CC == ISD::SETNE || CC == ISD::SETGT || CC == ISD::SETULT)) || 8366 (CRHS->isNullValue() && 8367 (CC == ISD::SETEQ || CC == ISD::SETGE || CC == ISD::SETULE))) 8368 return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0), 8369 DAG.getConstant(-1, SL, MVT::i1)); 8370 if ((CRHS->isAllOnesValue() && 8371 (CC == ISD::SETEQ || CC == ISD::SETLE || CC == ISD::SETUGE)) || 8372 (CRHS->isNullValue() && 8373 (CC == ISD::SETNE || CC == ISD::SETUGT || CC == ISD::SETLT))) 8374 return LHS.getOperand(0); 8375 } 8376 8377 uint64_t CRHSVal = CRHS->getZExtValue(); 8378 if ((CC == ISD::SETEQ || CC == ISD::SETNE) && 8379 LHS.getOpcode() == ISD::SELECT && 8380 isa<ConstantSDNode>(LHS.getOperand(1)) && 8381 isa<ConstantSDNode>(LHS.getOperand(2)) && 8382 LHS.getConstantOperandVal(1) != LHS.getConstantOperandVal(2) && 8383 isBoolSGPR(LHS.getOperand(0))) { 8384 // Given CT != FT: 8385 // setcc (select cc, CT, CF), CF, eq => xor cc, -1 8386 // setcc (select cc, CT, CF), CF, ne => cc 8387 // setcc (select cc, CT, CF), CT, ne => xor cc, -1 8388 // setcc (select cc, CT, CF), CT, eq => cc 8389 uint64_t CT = LHS.getConstantOperandVal(1); 8390 uint64_t CF = LHS.getConstantOperandVal(2); 8391 8392 if ((CF == CRHSVal && CC == ISD::SETEQ) || 8393 (CT == CRHSVal && CC == ISD::SETNE)) 8394 return DAG.getNode(ISD::XOR, SL, MVT::i1, LHS.getOperand(0), 8395 DAG.getConstant(-1, SL, MVT::i1)); 8396 if ((CF == CRHSVal && CC == ISD::SETNE) || 8397 (CT == CRHSVal && CC == ISD::SETEQ)) 8398 return LHS.getOperand(0); 8399 } 8400 } 8401 8402 if (VT != MVT::f32 && VT != MVT::f64 && (Subtarget->has16BitInsts() && 8403 VT != MVT::f16)) 8404 return SDValue(); 8405 8406 // Match isinf/isfinite pattern 8407 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity)) 8408 // (fcmp one (fabs x), inf) -> (fp_class x, 8409 // (p_normal | n_normal | p_subnormal | n_subnormal | p_zero | n_zero) 8410 if ((CC == ISD::SETOEQ || CC == ISD::SETONE) && LHS.getOpcode() == ISD::FABS) { 8411 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); 8412 if (!CRHS) 8413 return SDValue(); 8414 8415 const APFloat &APF = CRHS->getValueAPF(); 8416 if (APF.isInfinity() && !APF.isNegative()) { 8417 const unsigned IsInfMask = SIInstrFlags::P_INFINITY | 8418 SIInstrFlags::N_INFINITY; 8419 const unsigned IsFiniteMask = SIInstrFlags::N_ZERO | 8420 SIInstrFlags::P_ZERO | 8421 SIInstrFlags::N_NORMAL | 8422 SIInstrFlags::P_NORMAL | 8423 SIInstrFlags::N_SUBNORMAL | 8424 SIInstrFlags::P_SUBNORMAL; 8425 unsigned Mask = CC == ISD::SETOEQ ? IsInfMask : IsFiniteMask; 8426 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0), 8427 DAG.getConstant(Mask, SL, MVT::i32)); 8428 } 8429 } 8430 8431 return SDValue(); 8432 } 8433 8434 SDValue SITargetLowering::performCvtF32UByteNCombine(SDNode *N, 8435 DAGCombinerInfo &DCI) const { 8436 SelectionDAG &DAG = DCI.DAG; 8437 SDLoc SL(N); 8438 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0; 8439 8440 SDValue Src = N->getOperand(0); 8441 SDValue Srl = N->getOperand(0); 8442 if (Srl.getOpcode() == ISD::ZERO_EXTEND) 8443 Srl = Srl.getOperand(0); 8444 8445 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero. 8446 if (Srl.getOpcode() == ISD::SRL) { 8447 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x 8448 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x 8449 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x 8450 8451 if (const ConstantSDNode *C = 8452 dyn_cast<ConstantSDNode>(Srl.getOperand(1))) { 8453 Srl = DAG.getZExtOrTrunc(Srl.getOperand(0), SDLoc(Srl.getOperand(0)), 8454 EVT(MVT::i32)); 8455 8456 unsigned SrcOffset = C->getZExtValue() + 8 * Offset; 8457 if (SrcOffset < 32 && SrcOffset % 8 == 0) { 8458 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, SL, 8459 MVT::f32, Srl); 8460 } 8461 } 8462 } 8463 8464 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8); 8465 8466 KnownBits Known; 8467 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 8468 !DCI.isBeforeLegalizeOps()); 8469 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 8470 if (TLI.ShrinkDemandedConstant(Src, Demanded, TLO) || 8471 TLI.SimplifyDemandedBits(Src, Demanded, Known, TLO)) { 8472 DCI.CommitTargetLoweringOpt(TLO); 8473 } 8474 8475 return SDValue(); 8476 } 8477 8478 SDValue SITargetLowering::performClampCombine(SDNode *N, 8479 DAGCombinerInfo &DCI) const { 8480 ConstantFPSDNode *CSrc = dyn_cast<ConstantFPSDNode>(N->getOperand(0)); 8481 if (!CSrc) 8482 return SDValue(); 8483 8484 const APFloat &F = CSrc->getValueAPF(); 8485 APFloat Zero = APFloat::getZero(F.getSemantics()); 8486 APFloat::cmpResult Cmp0 = F.compare(Zero); 8487 if (Cmp0 == APFloat::cmpLessThan || 8488 (Cmp0 == APFloat::cmpUnordered && Subtarget->enableDX10Clamp())) { 8489 return DCI.DAG.getConstantFP(Zero, SDLoc(N), N->getValueType(0)); 8490 } 8491 8492 APFloat One(F.getSemantics(), "1.0"); 8493 APFloat::cmpResult Cmp1 = F.compare(One); 8494 if (Cmp1 == APFloat::cmpGreaterThan) 8495 return DCI.DAG.getConstantFP(One, SDLoc(N), N->getValueType(0)); 8496 8497 return SDValue(CSrc, 0); 8498 } 8499 8500 8501 SDValue SITargetLowering::PerformDAGCombine(SDNode *N, 8502 DAGCombinerInfo &DCI) const { 8503 switch (N->getOpcode()) { 8504 default: 8505 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 8506 case ISD::ADD: 8507 return performAddCombine(N, DCI); 8508 case ISD::SUB: 8509 return performSubCombine(N, DCI); 8510 case ISD::ADDCARRY: 8511 case ISD::SUBCARRY: 8512 return performAddCarrySubCarryCombine(N, DCI); 8513 case ISD::FADD: 8514 return performFAddCombine(N, DCI); 8515 case ISD::FSUB: 8516 return performFSubCombine(N, DCI); 8517 case ISD::SETCC: 8518 return performSetCCCombine(N, DCI); 8519 case ISD::FMAXNUM: 8520 case ISD::FMINNUM: 8521 case ISD::SMAX: 8522 case ISD::SMIN: 8523 case ISD::UMAX: 8524 case ISD::UMIN: 8525 case AMDGPUISD::FMIN_LEGACY: 8526 case AMDGPUISD::FMAX_LEGACY: { 8527 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG && 8528 getTargetMachine().getOptLevel() > CodeGenOpt::None) 8529 return performMinMaxCombine(N, DCI); 8530 break; 8531 } 8532 case ISD::FMA: 8533 return performFMACombine(N, DCI); 8534 case ISD::LOAD: { 8535 if (SDValue Widended = widenLoad(cast<LoadSDNode>(N), DCI)) 8536 return Widended; 8537 LLVM_FALLTHROUGH; 8538 } 8539 case ISD::STORE: 8540 case ISD::ATOMIC_LOAD: 8541 case ISD::ATOMIC_STORE: 8542 case ISD::ATOMIC_CMP_SWAP: 8543 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 8544 case ISD::ATOMIC_SWAP: 8545 case ISD::ATOMIC_LOAD_ADD: 8546 case ISD::ATOMIC_LOAD_SUB: 8547 case ISD::ATOMIC_LOAD_AND: 8548 case ISD::ATOMIC_LOAD_OR: 8549 case ISD::ATOMIC_LOAD_XOR: 8550 case ISD::ATOMIC_LOAD_NAND: 8551 case ISD::ATOMIC_LOAD_MIN: 8552 case ISD::ATOMIC_LOAD_MAX: 8553 case ISD::ATOMIC_LOAD_UMIN: 8554 case ISD::ATOMIC_LOAD_UMAX: 8555 case AMDGPUISD::ATOMIC_INC: 8556 case AMDGPUISD::ATOMIC_DEC: 8557 case AMDGPUISD::ATOMIC_LOAD_FADD: 8558 case AMDGPUISD::ATOMIC_LOAD_FMIN: 8559 case AMDGPUISD::ATOMIC_LOAD_FMAX: // TODO: Target mem intrinsics. 8560 if (DCI.isBeforeLegalize()) 8561 break; 8562 return performMemSDNodeCombine(cast<MemSDNode>(N), DCI); 8563 case ISD::AND: 8564 return performAndCombine(N, DCI); 8565 case ISD::OR: 8566 return performOrCombine(N, DCI); 8567 case ISD::XOR: 8568 return performXorCombine(N, DCI); 8569 case ISD::ZERO_EXTEND: 8570 return performZeroExtendCombine(N, DCI); 8571 case AMDGPUISD::FP_CLASS: 8572 return performClassCombine(N, DCI); 8573 case ISD::FCANONICALIZE: 8574 return performFCanonicalizeCombine(N, DCI); 8575 case AMDGPUISD::RCP: 8576 return performRcpCombine(N, DCI); 8577 case AMDGPUISD::FRACT: 8578 case AMDGPUISD::RSQ: 8579 case AMDGPUISD::RCP_LEGACY: 8580 case AMDGPUISD::RSQ_LEGACY: 8581 case AMDGPUISD::RCP_IFLAG: 8582 case AMDGPUISD::RSQ_CLAMP: 8583 case AMDGPUISD::LDEXP: { 8584 SDValue Src = N->getOperand(0); 8585 if (Src.isUndef()) 8586 return Src; 8587 break; 8588 } 8589 case ISD::SINT_TO_FP: 8590 case ISD::UINT_TO_FP: 8591 return performUCharToFloatCombine(N, DCI); 8592 case AMDGPUISD::CVT_F32_UBYTE0: 8593 case AMDGPUISD::CVT_F32_UBYTE1: 8594 case AMDGPUISD::CVT_F32_UBYTE2: 8595 case AMDGPUISD::CVT_F32_UBYTE3: 8596 return performCvtF32UByteNCombine(N, DCI); 8597 case AMDGPUISD::FMED3: 8598 return performFMed3Combine(N, DCI); 8599 case AMDGPUISD::CVT_PKRTZ_F16_F32: 8600 return performCvtPkRTZCombine(N, DCI); 8601 case AMDGPUISD::CLAMP: 8602 return performClampCombine(N, DCI); 8603 case ISD::SCALAR_TO_VECTOR: { 8604 SelectionDAG &DAG = DCI.DAG; 8605 EVT VT = N->getValueType(0); 8606 8607 // v2i16 (scalar_to_vector i16:x) -> v2i16 (bitcast (any_extend i16:x)) 8608 if (VT == MVT::v2i16 || VT == MVT::v2f16) { 8609 SDLoc SL(N); 8610 SDValue Src = N->getOperand(0); 8611 EVT EltVT = Src.getValueType(); 8612 if (EltVT == MVT::f16) 8613 Src = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Src); 8614 8615 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Src); 8616 return DAG.getNode(ISD::BITCAST, SL, VT, Ext); 8617 } 8618 8619 break; 8620 } 8621 case ISD::EXTRACT_VECTOR_ELT: 8622 return performExtractVectorEltCombine(N, DCI); 8623 case ISD::BUILD_VECTOR: 8624 return performBuildVectorCombine(N, DCI); 8625 } 8626 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 8627 } 8628 8629 /// Helper function for adjustWritemask 8630 static unsigned SubIdx2Lane(unsigned Idx) { 8631 switch (Idx) { 8632 default: return 0; 8633 case AMDGPU::sub0: return 0; 8634 case AMDGPU::sub1: return 1; 8635 case AMDGPU::sub2: return 2; 8636 case AMDGPU::sub3: return 3; 8637 } 8638 } 8639 8640 /// Adjust the writemask of MIMG instructions 8641 SDNode *SITargetLowering::adjustWritemask(MachineSDNode *&Node, 8642 SelectionDAG &DAG) const { 8643 unsigned Opcode = Node->getMachineOpcode(); 8644 8645 // Subtract 1 because the vdata output is not a MachineSDNode operand. 8646 int D16Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::d16) - 1; 8647 if (D16Idx >= 0 && Node->getConstantOperandVal(D16Idx)) 8648 return Node; // not implemented for D16 8649 8650 SDNode *Users[4] = { nullptr }; 8651 unsigned Lane = 0; 8652 unsigned DmaskIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::dmask) - 1; 8653 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx); 8654 unsigned NewDmask = 0; 8655 bool HasChain = Node->getNumValues() > 1; 8656 8657 if (OldDmask == 0) { 8658 // These are folded out, but on the chance it happens don't assert. 8659 return Node; 8660 } 8661 8662 // Try to figure out the used register components 8663 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); 8664 I != E; ++I) { 8665 8666 // Don't look at users of the chain. 8667 if (I.getUse().getResNo() != 0) 8668 continue; 8669 8670 // Abort if we can't understand the usage 8671 if (!I->isMachineOpcode() || 8672 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) 8673 return Node; 8674 8675 // Lane means which subreg of %vgpra_vgprb_vgprc_vgprd is used. 8676 // Note that subregs are packed, i.e. Lane==0 is the first bit set 8677 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit 8678 // set, etc. 8679 Lane = SubIdx2Lane(I->getConstantOperandVal(1)); 8680 8681 // Set which texture component corresponds to the lane. 8682 unsigned Comp; 8683 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) { 8684 Comp = countTrailingZeros(Dmask); 8685 Dmask &= ~(1 << Comp); 8686 } 8687 8688 // Abort if we have more than one user per component 8689 if (Users[Lane]) 8690 return Node; 8691 8692 Users[Lane] = *I; 8693 NewDmask |= 1 << Comp; 8694 } 8695 8696 // Abort if there's no change 8697 if (NewDmask == OldDmask) 8698 return Node; 8699 8700 unsigned BitsSet = countPopulation(NewDmask); 8701 8702 int NewOpcode = AMDGPU::getMaskedMIMGOp(Node->getMachineOpcode(), BitsSet); 8703 assert(NewOpcode != -1 && 8704 NewOpcode != static_cast<int>(Node->getMachineOpcode()) && 8705 "failed to find equivalent MIMG op"); 8706 8707 // Adjust the writemask in the node 8708 SmallVector<SDValue, 12> Ops; 8709 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx); 8710 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32)); 8711 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end()); 8712 8713 MVT SVT = Node->getValueType(0).getVectorElementType().getSimpleVT(); 8714 8715 MVT ResultVT = BitsSet == 1 ? 8716 SVT : MVT::getVectorVT(SVT, BitsSet == 3 ? 4 : BitsSet); 8717 SDVTList NewVTList = HasChain ? 8718 DAG.getVTList(ResultVT, MVT::Other) : DAG.getVTList(ResultVT); 8719 8720 8721 MachineSDNode *NewNode = DAG.getMachineNode(NewOpcode, SDLoc(Node), 8722 NewVTList, Ops); 8723 8724 if (HasChain) { 8725 // Update chain. 8726 DAG.setNodeMemRefs(NewNode, Node->memoperands()); 8727 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(NewNode, 1)); 8728 } 8729 8730 if (BitsSet == 1) { 8731 assert(Node->hasNUsesOfValue(1, 0)); 8732 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY, 8733 SDLoc(Node), Users[Lane]->getValueType(0), 8734 SDValue(NewNode, 0)); 8735 DAG.ReplaceAllUsesWith(Users[Lane], Copy); 8736 return nullptr; 8737 } 8738 8739 // Update the users of the node with the new indices 8740 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { 8741 SDNode *User = Users[i]; 8742 if (!User) 8743 continue; 8744 8745 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32); 8746 DAG.UpdateNodeOperands(User, SDValue(NewNode, 0), Op); 8747 8748 switch (Idx) { 8749 default: break; 8750 case AMDGPU::sub0: Idx = AMDGPU::sub1; break; 8751 case AMDGPU::sub1: Idx = AMDGPU::sub2; break; 8752 case AMDGPU::sub2: Idx = AMDGPU::sub3; break; 8753 } 8754 } 8755 8756 DAG.RemoveDeadNode(Node); 8757 return nullptr; 8758 } 8759 8760 static bool isFrameIndexOp(SDValue Op) { 8761 if (Op.getOpcode() == ISD::AssertZext) 8762 Op = Op.getOperand(0); 8763 8764 return isa<FrameIndexSDNode>(Op); 8765 } 8766 8767 /// Legalize target independent instructions (e.g. INSERT_SUBREG) 8768 /// with frame index operands. 8769 /// LLVM assumes that inputs are to these instructions are registers. 8770 SDNode *SITargetLowering::legalizeTargetIndependentNode(SDNode *Node, 8771 SelectionDAG &DAG) const { 8772 if (Node->getOpcode() == ISD::CopyToReg) { 8773 RegisterSDNode *DestReg = cast<RegisterSDNode>(Node->getOperand(1)); 8774 SDValue SrcVal = Node->getOperand(2); 8775 8776 // Insert a copy to a VReg_1 virtual register so LowerI1Copies doesn't have 8777 // to try understanding copies to physical registers. 8778 if (SrcVal.getValueType() == MVT::i1 && 8779 TargetRegisterInfo::isPhysicalRegister(DestReg->getReg())) { 8780 SDLoc SL(Node); 8781 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 8782 SDValue VReg = DAG.getRegister( 8783 MRI.createVirtualRegister(&AMDGPU::VReg_1RegClass), MVT::i1); 8784 8785 SDNode *Glued = Node->getGluedNode(); 8786 SDValue ToVReg 8787 = DAG.getCopyToReg(Node->getOperand(0), SL, VReg, SrcVal, 8788 SDValue(Glued, Glued ? Glued->getNumValues() - 1 : 0)); 8789 SDValue ToResultReg 8790 = DAG.getCopyToReg(ToVReg, SL, SDValue(DestReg, 0), 8791 VReg, ToVReg.getValue(1)); 8792 DAG.ReplaceAllUsesWith(Node, ToResultReg.getNode()); 8793 DAG.RemoveDeadNode(Node); 8794 return ToResultReg.getNode(); 8795 } 8796 } 8797 8798 SmallVector<SDValue, 8> Ops; 8799 for (unsigned i = 0; i < Node->getNumOperands(); ++i) { 8800 if (!isFrameIndexOp(Node->getOperand(i))) { 8801 Ops.push_back(Node->getOperand(i)); 8802 continue; 8803 } 8804 8805 SDLoc DL(Node); 8806 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, 8807 Node->getOperand(i).getValueType(), 8808 Node->getOperand(i)), 0)); 8809 } 8810 8811 return DAG.UpdateNodeOperands(Node, Ops); 8812 } 8813 8814 /// Fold the instructions after selecting them. 8815 /// Returns null if users were already updated. 8816 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, 8817 SelectionDAG &DAG) const { 8818 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 8819 unsigned Opcode = Node->getMachineOpcode(); 8820 8821 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() && 8822 !TII->isGather4(Opcode)) { 8823 return adjustWritemask(Node, DAG); 8824 } 8825 8826 if (Opcode == AMDGPU::INSERT_SUBREG || 8827 Opcode == AMDGPU::REG_SEQUENCE) { 8828 legalizeTargetIndependentNode(Node, DAG); 8829 return Node; 8830 } 8831 8832 switch (Opcode) { 8833 case AMDGPU::V_DIV_SCALE_F32: 8834 case AMDGPU::V_DIV_SCALE_F64: { 8835 // Satisfy the operand register constraint when one of the inputs is 8836 // undefined. Ordinarily each undef value will have its own implicit_def of 8837 // a vreg, so force these to use a single register. 8838 SDValue Src0 = Node->getOperand(0); 8839 SDValue Src1 = Node->getOperand(1); 8840 SDValue Src2 = Node->getOperand(2); 8841 8842 if ((Src0.isMachineOpcode() && 8843 Src0.getMachineOpcode() != AMDGPU::IMPLICIT_DEF) && 8844 (Src0 == Src1 || Src0 == Src2)) 8845 break; 8846 8847 MVT VT = Src0.getValueType().getSimpleVT(); 8848 const TargetRegisterClass *RC = getRegClassFor(VT); 8849 8850 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 8851 SDValue UndefReg = DAG.getRegister(MRI.createVirtualRegister(RC), VT); 8852 8853 SDValue ImpDef = DAG.getCopyToReg(DAG.getEntryNode(), SDLoc(Node), 8854 UndefReg, Src0, SDValue()); 8855 8856 // src0 must be the same register as src1 or src2, even if the value is 8857 // undefined, so make sure we don't violate this constraint. 8858 if (Src0.isMachineOpcode() && 8859 Src0.getMachineOpcode() == AMDGPU::IMPLICIT_DEF) { 8860 if (Src1.isMachineOpcode() && 8861 Src1.getMachineOpcode() != AMDGPU::IMPLICIT_DEF) 8862 Src0 = Src1; 8863 else if (Src2.isMachineOpcode() && 8864 Src2.getMachineOpcode() != AMDGPU::IMPLICIT_DEF) 8865 Src0 = Src2; 8866 else { 8867 assert(Src1.getMachineOpcode() == AMDGPU::IMPLICIT_DEF); 8868 Src0 = UndefReg; 8869 Src1 = UndefReg; 8870 } 8871 } else 8872 break; 8873 8874 SmallVector<SDValue, 4> Ops = { Src0, Src1, Src2 }; 8875 for (unsigned I = 3, N = Node->getNumOperands(); I != N; ++I) 8876 Ops.push_back(Node->getOperand(I)); 8877 8878 Ops.push_back(ImpDef.getValue(1)); 8879 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops); 8880 } 8881 default: 8882 break; 8883 } 8884 8885 return Node; 8886 } 8887 8888 /// Assign the register class depending on the number of 8889 /// bits set in the writemask 8890 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 8891 SDNode *Node) const { 8892 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 8893 8894 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 8895 8896 if (TII->isVOP3(MI.getOpcode())) { 8897 // Make sure constant bus requirements are respected. 8898 TII->legalizeOperandsVOP3(MRI, MI); 8899 return; 8900 } 8901 8902 // Replace unused atomics with the no return version. 8903 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode()); 8904 if (NoRetAtomicOp != -1) { 8905 if (!Node->hasAnyUseOfValue(0)) { 8906 MI.setDesc(TII->get(NoRetAtomicOp)); 8907 MI.RemoveOperand(0); 8908 return; 8909 } 8910 8911 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg 8912 // instruction, because the return type of these instructions is a vec2 of 8913 // the memory type, so it can be tied to the input operand. 8914 // This means these instructions always have a use, so we need to add a 8915 // special case to check if the atomic has only one extract_subreg use, 8916 // which itself has no uses. 8917 if ((Node->hasNUsesOfValue(1, 0) && 8918 Node->use_begin()->isMachineOpcode() && 8919 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG && 8920 !Node->use_begin()->hasAnyUseOfValue(0))) { 8921 unsigned Def = MI.getOperand(0).getReg(); 8922 8923 // Change this into a noret atomic. 8924 MI.setDesc(TII->get(NoRetAtomicOp)); 8925 MI.RemoveOperand(0); 8926 8927 // If we only remove the def operand from the atomic instruction, the 8928 // extract_subreg will be left with a use of a vreg without a def. 8929 // So we need to insert an implicit_def to avoid machine verifier 8930 // errors. 8931 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), 8932 TII->get(AMDGPU::IMPLICIT_DEF), Def); 8933 } 8934 return; 8935 } 8936 } 8937 8938 static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL, 8939 uint64_t Val) { 8940 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32); 8941 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0); 8942 } 8943 8944 MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG, 8945 const SDLoc &DL, 8946 SDValue Ptr) const { 8947 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 8948 8949 // Build the half of the subregister with the constants before building the 8950 // full 128-bit register. If we are building multiple resource descriptors, 8951 // this will allow CSEing of the 2-component register. 8952 const SDValue Ops0[] = { 8953 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32), 8954 buildSMovImm32(DAG, DL, 0), 8955 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 8956 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32), 8957 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32) 8958 }; 8959 8960 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, 8961 MVT::v2i32, Ops0), 0); 8962 8963 // Combine the constants and the pointer. 8964 const SDValue Ops1[] = { 8965 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), 8966 Ptr, 8967 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), 8968 SubRegHi, 8969 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32) 8970 }; 8971 8972 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1); 8973 } 8974 8975 /// Return a resource descriptor with the 'Add TID' bit enabled 8976 /// The TID (Thread ID) is multiplied by the stride value (bits [61:48] 8977 /// of the resource descriptor) to create an offset, which is added to 8978 /// the resource pointer. 8979 MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL, 8980 SDValue Ptr, uint32_t RsrcDword1, 8981 uint64_t RsrcDword2And3) const { 8982 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr); 8983 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr); 8984 if (RsrcDword1) { 8985 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi, 8986 DAG.getConstant(RsrcDword1, DL, MVT::i32)), 8987 0); 8988 } 8989 8990 SDValue DataLo = buildSMovImm32(DAG, DL, 8991 RsrcDword2And3 & UINT64_C(0xFFFFFFFF)); 8992 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32); 8993 8994 const SDValue Ops[] = { 8995 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), 8996 PtrLo, 8997 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 8998 PtrHi, 8999 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32), 9000 DataLo, 9001 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32), 9002 DataHi, 9003 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32) 9004 }; 9005 9006 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops); 9007 } 9008 9009 //===----------------------------------------------------------------------===// 9010 // SI Inline Assembly Support 9011 //===----------------------------------------------------------------------===// 9012 9013 std::pair<unsigned, const TargetRegisterClass *> 9014 SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 9015 StringRef Constraint, 9016 MVT VT) const { 9017 const TargetRegisterClass *RC = nullptr; 9018 if (Constraint.size() == 1) { 9019 switch (Constraint[0]) { 9020 default: 9021 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 9022 case 's': 9023 case 'r': 9024 switch (VT.getSizeInBits()) { 9025 default: 9026 return std::make_pair(0U, nullptr); 9027 case 32: 9028 case 16: 9029 RC = &AMDGPU::SReg_32_XM0RegClass; 9030 break; 9031 case 64: 9032 RC = &AMDGPU::SGPR_64RegClass; 9033 break; 9034 case 128: 9035 RC = &AMDGPU::SReg_128RegClass; 9036 break; 9037 case 256: 9038 RC = &AMDGPU::SReg_256RegClass; 9039 break; 9040 case 512: 9041 RC = &AMDGPU::SReg_512RegClass; 9042 break; 9043 } 9044 break; 9045 case 'v': 9046 switch (VT.getSizeInBits()) { 9047 default: 9048 return std::make_pair(0U, nullptr); 9049 case 32: 9050 case 16: 9051 RC = &AMDGPU::VGPR_32RegClass; 9052 break; 9053 case 64: 9054 RC = &AMDGPU::VReg_64RegClass; 9055 break; 9056 case 96: 9057 RC = &AMDGPU::VReg_96RegClass; 9058 break; 9059 case 128: 9060 RC = &AMDGPU::VReg_128RegClass; 9061 break; 9062 case 256: 9063 RC = &AMDGPU::VReg_256RegClass; 9064 break; 9065 case 512: 9066 RC = &AMDGPU::VReg_512RegClass; 9067 break; 9068 } 9069 break; 9070 } 9071 // We actually support i128, i16 and f16 as inline parameters 9072 // even if they are not reported as legal 9073 if (RC && (isTypeLegal(VT) || VT.SimpleTy == MVT::i128 || 9074 VT.SimpleTy == MVT::i16 || VT.SimpleTy == MVT::f16)) 9075 return std::make_pair(0U, RC); 9076 } 9077 9078 if (Constraint.size() > 1) { 9079 if (Constraint[1] == 'v') { 9080 RC = &AMDGPU::VGPR_32RegClass; 9081 } else if (Constraint[1] == 's') { 9082 RC = &AMDGPU::SGPR_32RegClass; 9083 } 9084 9085 if (RC) { 9086 uint32_t Idx; 9087 bool Failed = Constraint.substr(2).getAsInteger(10, Idx); 9088 if (!Failed && Idx < RC->getNumRegs()) 9089 return std::make_pair(RC->getRegister(Idx), RC); 9090 } 9091 } 9092 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 9093 } 9094 9095 SITargetLowering::ConstraintType 9096 SITargetLowering::getConstraintType(StringRef Constraint) const { 9097 if (Constraint.size() == 1) { 9098 switch (Constraint[0]) { 9099 default: break; 9100 case 's': 9101 case 'v': 9102 return C_RegisterClass; 9103 } 9104 } 9105 return TargetLowering::getConstraintType(Constraint); 9106 } 9107 9108 // Figure out which registers should be reserved for stack access. Only after 9109 // the function is legalized do we know all of the non-spill stack objects or if 9110 // calls are present. 9111 void SITargetLowering::finalizeLowering(MachineFunction &MF) const { 9112 MachineRegisterInfo &MRI = MF.getRegInfo(); 9113 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 9114 const MachineFrameInfo &MFI = MF.getFrameInfo(); 9115 const SIRegisterInfo *TRI = Subtarget->getRegisterInfo(); 9116 9117 if (Info->isEntryFunction()) { 9118 // Callable functions have fixed registers used for stack access. 9119 reservePrivateMemoryRegs(getTargetMachine(), MF, *TRI, *Info); 9120 } 9121 9122 // We have to assume the SP is needed in case there are calls in the function 9123 // during lowering. Calls are only detected after the function is 9124 // lowered. We're about to reserve registers, so don't bother using it if we 9125 // aren't really going to use it. 9126 bool NeedSP = !Info->isEntryFunction() || 9127 MFI.hasVarSizedObjects() || 9128 MFI.hasCalls(); 9129 9130 if (NeedSP) { 9131 unsigned ReservedStackPtrOffsetReg = TRI->reservedStackPtrOffsetReg(MF); 9132 Info->setStackPtrOffsetReg(ReservedStackPtrOffsetReg); 9133 9134 assert(Info->getStackPtrOffsetReg() != Info->getFrameOffsetReg()); 9135 assert(!TRI->isSubRegister(Info->getScratchRSrcReg(), 9136 Info->getStackPtrOffsetReg())); 9137 MRI.replaceRegWith(AMDGPU::SP_REG, Info->getStackPtrOffsetReg()); 9138 } 9139 9140 MRI.replaceRegWith(AMDGPU::PRIVATE_RSRC_REG, Info->getScratchRSrcReg()); 9141 MRI.replaceRegWith(AMDGPU::FP_REG, Info->getFrameOffsetReg()); 9142 MRI.replaceRegWith(AMDGPU::SCRATCH_WAVE_OFFSET_REG, 9143 Info->getScratchWaveOffsetReg()); 9144 9145 Info->limitOccupancy(MF); 9146 9147 TargetLoweringBase::finalizeLowering(MF); 9148 } 9149 9150 void SITargetLowering::computeKnownBitsForFrameIndex(const SDValue Op, 9151 KnownBits &Known, 9152 const APInt &DemandedElts, 9153 const SelectionDAG &DAG, 9154 unsigned Depth) const { 9155 TargetLowering::computeKnownBitsForFrameIndex(Op, Known, DemandedElts, 9156 DAG, Depth); 9157 9158 if (getSubtarget()->enableHugePrivateBuffer()) 9159 return; 9160 9161 // Technically it may be possible to have a dispatch with a single workitem 9162 // that uses the full private memory size, but that's not really useful. We 9163 // can't use vaddr in MUBUF instructions if we don't know the address 9164 // calculation won't overflow, so assume the sign bit is never set. 9165 Known.Zero.setHighBits(AssumeFrameIndexHighZeroBits); 9166 } 9167 9168 bool SITargetLowering::isSDNodeSourceOfDivergence(const SDNode * N, 9169 FunctionLoweringInfo * FLI, DivergenceAnalysis * DA) const 9170 { 9171 switch (N->getOpcode()) { 9172 case ISD::Register: 9173 case ISD::CopyFromReg: 9174 { 9175 const RegisterSDNode *R = nullptr; 9176 if (N->getOpcode() == ISD::Register) { 9177 R = dyn_cast<RegisterSDNode>(N); 9178 } 9179 else { 9180 R = dyn_cast<RegisterSDNode>(N->getOperand(1)); 9181 } 9182 if (R) 9183 { 9184 const MachineFunction * MF = FLI->MF; 9185 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 9186 const MachineRegisterInfo &MRI = MF->getRegInfo(); 9187 const SIRegisterInfo &TRI = ST.getInstrInfo()->getRegisterInfo(); 9188 unsigned Reg = R->getReg(); 9189 if (TRI.isPhysicalRegister(Reg)) 9190 return TRI.isVGPR(MRI, Reg); 9191 9192 if (MRI.isLiveIn(Reg)) { 9193 // workitem.id.x workitem.id.y workitem.id.z 9194 // Any VGPR formal argument is also considered divergent 9195 if (TRI.isVGPR(MRI, Reg)) 9196 return true; 9197 // Formal arguments of non-entry functions 9198 // are conservatively considered divergent 9199 else if (!AMDGPU::isEntryFunctionCC(FLI->Fn->getCallingConv())) 9200 return true; 9201 } 9202 return !DA || DA->isDivergent(FLI->getValueFromVirtualReg(Reg)); 9203 } 9204 } 9205 break; 9206 case ISD::LOAD: { 9207 const LoadSDNode *L = dyn_cast<LoadSDNode>(N); 9208 if (L->getMemOperand()->getAddrSpace() == 9209 Subtarget->getAMDGPUAS().PRIVATE_ADDRESS) 9210 return true; 9211 } break; 9212 case ISD::CALLSEQ_END: 9213 return true; 9214 break; 9215 case ISD::INTRINSIC_WO_CHAIN: 9216 { 9217 9218 } 9219 return AMDGPU::isIntrinsicSourceOfDivergence( 9220 cast<ConstantSDNode>(N->getOperand(0))->getZExtValue()); 9221 case ISD::INTRINSIC_W_CHAIN: 9222 return AMDGPU::isIntrinsicSourceOfDivergence( 9223 cast<ConstantSDNode>(N->getOperand(1))->getZExtValue()); 9224 // In some cases intrinsics that are a source of divergence have been 9225 // lowered to AMDGPUISD so we also need to check those too. 9226 case AMDGPUISD::INTERP_MOV: 9227 case AMDGPUISD::INTERP_P1: 9228 case AMDGPUISD::INTERP_P2: 9229 return true; 9230 } 9231 return false; 9232 } 9233 9234 bool SITargetLowering::denormalsEnabledForType(EVT VT) const { 9235 switch (VT.getScalarType().getSimpleVT().SimpleTy) { 9236 case MVT::f32: 9237 return Subtarget->hasFP32Denormals(); 9238 case MVT::f64: 9239 return Subtarget->hasFP64Denormals(); 9240 case MVT::f16: 9241 return Subtarget->hasFP16Denormals(); 9242 default: 9243 return false; 9244 } 9245 } 9246