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 /// \brief 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 "AMDGPU.h" 21 #include "AMDGPUIntrinsicInfo.h" 22 #include "AMDGPUSubtarget.h" 23 #include "SIDefines.h" 24 #include "SIISelLowering.h" 25 #include "SIInstrInfo.h" 26 #include "SIMachineFunctionInfo.h" 27 #include "SIRegisterInfo.h" 28 #include "Utils/AMDGPUBaseInfo.h" 29 #include "llvm/ADT/APFloat.h" 30 #include "llvm/ADT/APInt.h" 31 #include "llvm/ADT/ArrayRef.h" 32 #include "llvm/ADT/BitVector.h" 33 #include "llvm/ADT/SmallVector.h" 34 #include "llvm/ADT/StringRef.h" 35 #include "llvm/ADT/StringSwitch.h" 36 #include "llvm/ADT/Twine.h" 37 #include "llvm/CodeGen/Analysis.h" 38 #include "llvm/CodeGen/CallingConvLower.h" 39 #include "llvm/CodeGen/DAGCombine.h" 40 #include "llvm/CodeGen/ISDOpcodes.h" 41 #include "llvm/CodeGen/MachineBasicBlock.h" 42 #include "llvm/CodeGen/MachineFrameInfo.h" 43 #include "llvm/CodeGen/MachineFunction.h" 44 #include "llvm/CodeGen/MachineInstr.h" 45 #include "llvm/CodeGen/MachineInstrBuilder.h" 46 #include "llvm/CodeGen/MachineMemOperand.h" 47 #include "llvm/CodeGen/MachineOperand.h" 48 #include "llvm/CodeGen/MachineRegisterInfo.h" 49 #include "llvm/CodeGen/MachineValueType.h" 50 #include "llvm/CodeGen/SelectionDAG.h" 51 #include "llvm/CodeGen/SelectionDAGNodes.h" 52 #include "llvm/CodeGen/ValueTypes.h" 53 #include "llvm/IR/Constants.h" 54 #include "llvm/IR/DataLayout.h" 55 #include "llvm/IR/DebugLoc.h" 56 #include "llvm/IR/DerivedTypes.h" 57 #include "llvm/IR/DiagnosticInfo.h" 58 #include "llvm/IR/Function.h" 59 #include "llvm/IR/GlobalValue.h" 60 #include "llvm/IR/InstrTypes.h" 61 #include "llvm/IR/Instruction.h" 62 #include "llvm/IR/Instructions.h" 63 #include "llvm/IR/Type.h" 64 #include "llvm/Support/Casting.h" 65 #include "llvm/Support/CodeGen.h" 66 #include "llvm/Support/CommandLine.h" 67 #include "llvm/Support/Compiler.h" 68 #include "llvm/Support/ErrorHandling.h" 69 #include "llvm/Support/MathExtras.h" 70 #include "llvm/Target/TargetCallingConv.h" 71 #include "llvm/Target/TargetMachine.h" 72 #include "llvm/Target/TargetOptions.h" 73 #include "llvm/Target/TargetRegisterInfo.h" 74 #include <cassert> 75 #include <cmath> 76 #include <cstdint> 77 #include <iterator> 78 #include <tuple> 79 #include <utility> 80 #include <vector> 81 82 using namespace llvm; 83 84 static cl::opt<bool> EnableVGPRIndexMode( 85 "amdgpu-vgpr-index-mode", 86 cl::desc("Use GPR indexing mode instead of movrel for vector indexing"), 87 cl::init(false)); 88 89 static unsigned findFirstFreeSGPR(CCState &CCInfo) { 90 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs(); 91 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) { 92 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) { 93 return AMDGPU::SGPR0 + Reg; 94 } 95 } 96 llvm_unreachable("Cannot allocate sgpr"); 97 } 98 99 SITargetLowering::SITargetLowering(const TargetMachine &TM, 100 const SISubtarget &STI) 101 : AMDGPUTargetLowering(TM, STI) { 102 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass); 103 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass); 104 105 addRegisterClass(MVT::i32, &AMDGPU::SReg_32_XM0RegClass); 106 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass); 107 108 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass); 109 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass); 110 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass); 111 112 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass); 113 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass); 114 115 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass); 116 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass); 117 118 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass); 119 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass); 120 121 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass); 122 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass); 123 124 if (Subtarget->has16BitInsts()) { 125 addRegisterClass(MVT::i16, &AMDGPU::SReg_32_XM0RegClass); 126 addRegisterClass(MVT::f16, &AMDGPU::SReg_32_XM0RegClass); 127 } 128 129 if (Subtarget->hasVOP3PInsts()) { 130 addRegisterClass(MVT::v2i16, &AMDGPU::SReg_32_XM0RegClass); 131 addRegisterClass(MVT::v2f16, &AMDGPU::SReg_32_XM0RegClass); 132 } 133 134 computeRegisterProperties(STI.getRegisterInfo()); 135 136 // We need to custom lower vector stores from local memory 137 setOperationAction(ISD::LOAD, MVT::v2i32, Custom); 138 setOperationAction(ISD::LOAD, MVT::v4i32, Custom); 139 setOperationAction(ISD::LOAD, MVT::v8i32, Custom); 140 setOperationAction(ISD::LOAD, MVT::v16i32, Custom); 141 setOperationAction(ISD::LOAD, MVT::i1, Custom); 142 143 setOperationAction(ISD::STORE, MVT::v2i32, Custom); 144 setOperationAction(ISD::STORE, MVT::v4i32, Custom); 145 setOperationAction(ISD::STORE, MVT::v8i32, Custom); 146 setOperationAction(ISD::STORE, MVT::v16i32, Custom); 147 setOperationAction(ISD::STORE, MVT::i1, Custom); 148 149 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Expand); 150 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand); 151 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand); 152 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand); 153 setTruncStoreAction(MVT::v32i32, MVT::v32i16, Expand); 154 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Expand); 155 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Expand); 156 setTruncStoreAction(MVT::v8i32, MVT::v8i8, Expand); 157 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand); 158 setTruncStoreAction(MVT::v32i32, MVT::v32i8, Expand); 159 160 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom); 161 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom); 162 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand); 163 164 setOperationAction(ISD::SELECT, MVT::i1, Promote); 165 setOperationAction(ISD::SELECT, MVT::i64, Custom); 166 setOperationAction(ISD::SELECT, MVT::f64, Promote); 167 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64); 168 169 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand); 170 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand); 171 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand); 172 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand); 173 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand); 174 175 setOperationAction(ISD::SETCC, MVT::i1, Promote); 176 setOperationAction(ISD::SETCC, MVT::v2i1, Expand); 177 setOperationAction(ISD::SETCC, MVT::v4i1, Expand); 178 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32); 179 180 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand); 181 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand); 182 183 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom); 184 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom); 185 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom); 186 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom); 187 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom); 188 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom); 189 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom); 190 191 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom); 192 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom); 193 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); 194 setOperationAction(ISD::INTRINSIC_VOID, MVT::v2i16, Custom); 195 setOperationAction(ISD::INTRINSIC_VOID, MVT::v2f16, Custom); 196 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v2f16, Custom); 197 198 setOperationAction(ISD::BRCOND, MVT::Other, Custom); 199 setOperationAction(ISD::BR_CC, MVT::i1, Expand); 200 setOperationAction(ISD::BR_CC, MVT::i32, Expand); 201 setOperationAction(ISD::BR_CC, MVT::i64, Expand); 202 setOperationAction(ISD::BR_CC, MVT::f32, Expand); 203 setOperationAction(ISD::BR_CC, MVT::f64, Expand); 204 205 setOperationAction(ISD::UADDO, MVT::i32, Legal); 206 setOperationAction(ISD::USUBO, MVT::i32, Legal); 207 208 // We only support LOAD/STORE and vector manipulation ops for vectors 209 // with > 4 elements. 210 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, 211 MVT::v2i64, MVT::v2f64}) { 212 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { 213 switch (Op) { 214 case ISD::LOAD: 215 case ISD::STORE: 216 case ISD::BUILD_VECTOR: 217 case ISD::BITCAST: 218 case ISD::EXTRACT_VECTOR_ELT: 219 case ISD::INSERT_VECTOR_ELT: 220 case ISD::INSERT_SUBVECTOR: 221 case ISD::EXTRACT_SUBVECTOR: 222 case ISD::SCALAR_TO_VECTOR: 223 break; 224 case ISD::CONCAT_VECTORS: 225 setOperationAction(Op, VT, Custom); 226 break; 227 default: 228 setOperationAction(Op, VT, Expand); 229 break; 230 } 231 } 232 } 233 234 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that 235 // is expanded to avoid having two separate loops in case the index is a VGPR. 236 237 // Most operations are naturally 32-bit vector operations. We only support 238 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32. 239 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) { 240 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote); 241 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32); 242 243 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote); 244 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32); 245 246 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote); 247 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32); 248 249 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote); 250 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32); 251 } 252 253 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand); 254 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand); 255 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand); 256 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand); 257 258 // Avoid stack access for these. 259 // TODO: Generalize to more vector types. 260 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i16, Custom); 261 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f16, Custom); 262 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i16, Custom); 263 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f16, Custom); 264 265 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling, 266 // and output demarshalling 267 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); 268 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom); 269 270 // We can't return success/failure, only the old value, 271 // let LLVM add the comparison 272 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand); 273 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand); 274 275 if (getSubtarget()->hasFlatAddressSpace()) { 276 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom); 277 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom); 278 } 279 280 setOperationAction(ISD::BSWAP, MVT::i32, Legal); 281 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal); 282 283 // On SI this is s_memtime and s_memrealtime on VI. 284 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal); 285 setOperationAction(ISD::TRAP, MVT::Other, Legal); 286 setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal); 287 288 setOperationAction(ISD::FMINNUM, MVT::f64, Legal); 289 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal); 290 291 if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) { 292 setOperationAction(ISD::FTRUNC, MVT::f64, Legal); 293 setOperationAction(ISD::FCEIL, MVT::f64, Legal); 294 setOperationAction(ISD::FRINT, MVT::f64, Legal); 295 } 296 297 setOperationAction(ISD::FFLOOR, MVT::f64, Legal); 298 299 setOperationAction(ISD::FSIN, MVT::f32, Custom); 300 setOperationAction(ISD::FCOS, MVT::f32, Custom); 301 setOperationAction(ISD::FDIV, MVT::f32, Custom); 302 setOperationAction(ISD::FDIV, MVT::f64, Custom); 303 304 if (Subtarget->has16BitInsts()) { 305 setOperationAction(ISD::Constant, MVT::i16, Legal); 306 307 setOperationAction(ISD::SMIN, MVT::i16, Legal); 308 setOperationAction(ISD::SMAX, MVT::i16, Legal); 309 310 setOperationAction(ISD::UMIN, MVT::i16, Legal); 311 setOperationAction(ISD::UMAX, MVT::i16, Legal); 312 313 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Promote); 314 AddPromotedToType(ISD::SIGN_EXTEND, MVT::i16, MVT::i32); 315 316 setOperationAction(ISD::ROTR, MVT::i16, Promote); 317 setOperationAction(ISD::ROTL, MVT::i16, Promote); 318 319 setOperationAction(ISD::SDIV, MVT::i16, Promote); 320 setOperationAction(ISD::UDIV, MVT::i16, Promote); 321 setOperationAction(ISD::SREM, MVT::i16, Promote); 322 setOperationAction(ISD::UREM, MVT::i16, Promote); 323 324 setOperationAction(ISD::BSWAP, MVT::i16, Promote); 325 setOperationAction(ISD::BITREVERSE, MVT::i16, Promote); 326 327 setOperationAction(ISD::CTTZ, MVT::i16, Promote); 328 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote); 329 setOperationAction(ISD::CTLZ, MVT::i16, Promote); 330 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote); 331 332 setOperationAction(ISD::SELECT_CC, MVT::i16, Expand); 333 334 setOperationAction(ISD::BR_CC, MVT::i16, Expand); 335 336 setOperationAction(ISD::LOAD, MVT::i16, Custom); 337 338 setTruncStoreAction(MVT::i64, MVT::i16, Expand); 339 340 setOperationAction(ISD::FP16_TO_FP, MVT::i16, Promote); 341 AddPromotedToType(ISD::FP16_TO_FP, MVT::i16, MVT::i32); 342 setOperationAction(ISD::FP_TO_FP16, MVT::i16, Promote); 343 AddPromotedToType(ISD::FP_TO_FP16, MVT::i16, MVT::i32); 344 345 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote); 346 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote); 347 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote); 348 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote); 349 350 // F16 - Constant Actions. 351 setOperationAction(ISD::ConstantFP, MVT::f16, Legal); 352 353 // F16 - Load/Store Actions. 354 setOperationAction(ISD::LOAD, MVT::f16, Promote); 355 AddPromotedToType(ISD::LOAD, MVT::f16, MVT::i16); 356 setOperationAction(ISD::STORE, MVT::f16, Promote); 357 AddPromotedToType(ISD::STORE, MVT::f16, MVT::i16); 358 359 // F16 - VOP1 Actions. 360 setOperationAction(ISD::FP_ROUND, MVT::f16, Custom); 361 setOperationAction(ISD::FCOS, MVT::f16, Promote); 362 setOperationAction(ISD::FSIN, MVT::f16, Promote); 363 setOperationAction(ISD::FP_TO_SINT, MVT::f16, Promote); 364 setOperationAction(ISD::FP_TO_UINT, MVT::f16, Promote); 365 setOperationAction(ISD::SINT_TO_FP, MVT::f16, Promote); 366 setOperationAction(ISD::UINT_TO_FP, MVT::f16, Promote); 367 368 // F16 - VOP2 Actions. 369 setOperationAction(ISD::BR_CC, MVT::f16, Expand); 370 setOperationAction(ISD::SELECT_CC, MVT::f16, Expand); 371 setOperationAction(ISD::FMAXNUM, MVT::f16, Legal); 372 setOperationAction(ISD::FMINNUM, MVT::f16, Legal); 373 setOperationAction(ISD::FDIV, MVT::f16, Custom); 374 375 // F16 - VOP3 Actions. 376 setOperationAction(ISD::FMA, MVT::f16, Legal); 377 if (!Subtarget->hasFP16Denormals()) 378 setOperationAction(ISD::FMAD, MVT::f16, Legal); 379 } 380 381 if (Subtarget->hasVOP3PInsts()) { 382 for (MVT VT : {MVT::v2i16, MVT::v2f16}) { 383 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) { 384 switch (Op) { 385 case ISD::LOAD: 386 case ISD::STORE: 387 case ISD::BUILD_VECTOR: 388 case ISD::BITCAST: 389 case ISD::EXTRACT_VECTOR_ELT: 390 case ISD::INSERT_VECTOR_ELT: 391 case ISD::INSERT_SUBVECTOR: 392 case ISD::EXTRACT_SUBVECTOR: 393 case ISD::SCALAR_TO_VECTOR: 394 break; 395 case ISD::CONCAT_VECTORS: 396 setOperationAction(Op, VT, Custom); 397 break; 398 default: 399 setOperationAction(Op, VT, Expand); 400 break; 401 } 402 } 403 } 404 405 // XXX - Do these do anything? Vector constants turn into build_vector. 406 setOperationAction(ISD::Constant, MVT::v2i16, Legal); 407 setOperationAction(ISD::ConstantFP, MVT::v2f16, Legal); 408 409 setOperationAction(ISD::STORE, MVT::v2i16, Promote); 410 AddPromotedToType(ISD::STORE, MVT::v2i16, MVT::i32); 411 setOperationAction(ISD::STORE, MVT::v2f16, Promote); 412 AddPromotedToType(ISD::STORE, MVT::v2f16, MVT::i32); 413 414 setOperationAction(ISD::LOAD, MVT::v2i16, Promote); 415 AddPromotedToType(ISD::LOAD, MVT::v2i16, MVT::i32); 416 setOperationAction(ISD::LOAD, MVT::v2f16, Promote); 417 AddPromotedToType(ISD::LOAD, MVT::v2f16, MVT::i32); 418 419 setOperationAction(ISD::AND, MVT::v2i16, Promote); 420 AddPromotedToType(ISD::AND, MVT::v2i16, MVT::i32); 421 setOperationAction(ISD::OR, MVT::v2i16, Promote); 422 AddPromotedToType(ISD::OR, MVT::v2i16, MVT::i32); 423 setOperationAction(ISD::XOR, MVT::v2i16, Promote); 424 AddPromotedToType(ISD::XOR, MVT::v2i16, MVT::i32); 425 setOperationAction(ISD::SELECT, MVT::v2i16, Promote); 426 AddPromotedToType(ISD::SELECT, MVT::v2i16, MVT::i32); 427 setOperationAction(ISD::SELECT, MVT::v2f16, Promote); 428 AddPromotedToType(ISD::SELECT, MVT::v2f16, MVT::i32); 429 430 setOperationAction(ISD::ADD, MVT::v2i16, Legal); 431 setOperationAction(ISD::SUB, MVT::v2i16, Legal); 432 setOperationAction(ISD::MUL, MVT::v2i16, Legal); 433 setOperationAction(ISD::SHL, MVT::v2i16, Legal); 434 setOperationAction(ISD::SRL, MVT::v2i16, Legal); 435 setOperationAction(ISD::SRA, MVT::v2i16, Legal); 436 setOperationAction(ISD::SMIN, MVT::v2i16, Legal); 437 setOperationAction(ISD::UMIN, MVT::v2i16, Legal); 438 setOperationAction(ISD::SMAX, MVT::v2i16, Legal); 439 setOperationAction(ISD::UMAX, MVT::v2i16, Legal); 440 441 setOperationAction(ISD::FADD, MVT::v2f16, Legal); 442 setOperationAction(ISD::FNEG, MVT::v2f16, Legal); 443 setOperationAction(ISD::FMUL, MVT::v2f16, Legal); 444 setOperationAction(ISD::FMA, MVT::v2f16, Legal); 445 setOperationAction(ISD::FMINNUM, MVT::v2f16, Legal); 446 setOperationAction(ISD::FMAXNUM, MVT::v2f16, Legal); 447 448 // This isn't really legal, but this avoids the legalizer unrolling it (and 449 // allows matching fneg (fabs x) patterns) 450 setOperationAction(ISD::FABS, MVT::v2f16, Legal); 451 452 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i16, Custom); 453 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f16, Custom); 454 455 setOperationAction(ISD::ZERO_EXTEND, MVT::v2i32, Expand); 456 setOperationAction(ISD::SIGN_EXTEND, MVT::v2i32, Expand); 457 setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Expand); 458 } 459 460 setTargetDAGCombine(ISD::FADD); 461 setTargetDAGCombine(ISD::FSUB); 462 setTargetDAGCombine(ISD::FMINNUM); 463 setTargetDAGCombine(ISD::FMAXNUM); 464 setTargetDAGCombine(ISD::SMIN); 465 setTargetDAGCombine(ISD::SMAX); 466 setTargetDAGCombine(ISD::UMIN); 467 setTargetDAGCombine(ISD::UMAX); 468 setTargetDAGCombine(ISD::SETCC); 469 setTargetDAGCombine(ISD::AND); 470 setTargetDAGCombine(ISD::OR); 471 setTargetDAGCombine(ISD::XOR); 472 setTargetDAGCombine(ISD::SINT_TO_FP); 473 setTargetDAGCombine(ISD::UINT_TO_FP); 474 setTargetDAGCombine(ISD::FCANONICALIZE); 475 setTargetDAGCombine(ISD::SCALAR_TO_VECTOR); 476 477 // All memory operations. Some folding on the pointer operand is done to help 478 // matching the constant offsets in the addressing modes. 479 setTargetDAGCombine(ISD::LOAD); 480 setTargetDAGCombine(ISD::STORE); 481 setTargetDAGCombine(ISD::ATOMIC_LOAD); 482 setTargetDAGCombine(ISD::ATOMIC_STORE); 483 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP); 484 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS); 485 setTargetDAGCombine(ISD::ATOMIC_SWAP); 486 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD); 487 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB); 488 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND); 489 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR); 490 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR); 491 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND); 492 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN); 493 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX); 494 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN); 495 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX); 496 497 setSchedulingPreference(Sched::RegPressure); 498 } 499 500 const SISubtarget *SITargetLowering::getSubtarget() const { 501 return static_cast<const SISubtarget *>(Subtarget); 502 } 503 504 //===----------------------------------------------------------------------===// 505 // TargetLowering queries 506 //===----------------------------------------------------------------------===// 507 508 bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, 509 const CallInst &CI, 510 unsigned IntrID) const { 511 switch (IntrID) { 512 case Intrinsic::amdgcn_atomic_inc: 513 case Intrinsic::amdgcn_atomic_dec: 514 Info.opc = ISD::INTRINSIC_W_CHAIN; 515 Info.memVT = MVT::getVT(CI.getType()); 516 Info.ptrVal = CI.getOperand(0); 517 Info.align = 0; 518 Info.vol = false; 519 Info.readMem = true; 520 Info.writeMem = true; 521 return true; 522 default: 523 return false; 524 } 525 } 526 527 bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &, 528 EVT) const { 529 // SI has some legal vector types, but no legal vector operations. Say no 530 // shuffles are legal in order to prefer scalarizing some vector operations. 531 return false; 532 } 533 534 bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const { 535 // Flat instructions do not have offsets, and only have the register 536 // address. 537 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1); 538 } 539 540 bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const { 541 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and 542 // additionally can do r + r + i with addr64. 32-bit has more addressing 543 // mode options. Depending on the resource constant, it can also do 544 // (i64 r0) + (i32 r1) * (i14 i). 545 // 546 // Private arrays end up using a scratch buffer most of the time, so also 547 // assume those use MUBUF instructions. Scratch loads / stores are currently 548 // implemented as mubuf instructions with offen bit set, so slightly 549 // different than the normal addr64. 550 if (!isUInt<12>(AM.BaseOffs)) 551 return false; 552 553 // FIXME: Since we can split immediate into soffset and immediate offset, 554 // would it make sense to allow any immediate? 555 556 switch (AM.Scale) { 557 case 0: // r + i or just i, depending on HasBaseReg. 558 return true; 559 case 1: 560 return true; // We have r + r or r + i. 561 case 2: 562 if (AM.HasBaseReg) { 563 // Reject 2 * r + r. 564 return false; 565 } 566 567 // Allow 2 * r as r + r 568 // Or 2 * r + i is allowed as r + r + i. 569 return true; 570 default: // Don't allow n * r 571 return false; 572 } 573 } 574 575 bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL, 576 const AddrMode &AM, Type *Ty, 577 unsigned AS) const { 578 // No global is ever allowed as a base. 579 if (AM.BaseGV) 580 return false; 581 582 switch (AS) { 583 case AMDGPUAS::GLOBAL_ADDRESS: 584 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 585 // Assume the we will use FLAT for all global memory accesses 586 // on VI. 587 // FIXME: This assumption is currently wrong. On VI we still use 588 // MUBUF instructions for the r + i addressing mode. As currently 589 // implemented, the MUBUF instructions only work on buffer < 4GB. 590 // It may be possible to support > 4GB buffers with MUBUF instructions, 591 // by setting the stride value in the resource descriptor which would 592 // increase the size limit to (stride * 4GB). However, this is risky, 593 // because it has never been validated. 594 return isLegalFlatAddressingMode(AM); 595 } 596 597 return isLegalMUBUFAddressingMode(AM); 598 599 case AMDGPUAS::CONSTANT_ADDRESS: 600 // If the offset isn't a multiple of 4, it probably isn't going to be 601 // correctly aligned. 602 // FIXME: Can we get the real alignment here? 603 if (AM.BaseOffs % 4 != 0) 604 return isLegalMUBUFAddressingMode(AM); 605 606 // There are no SMRD extloads, so if we have to do a small type access we 607 // will use a MUBUF load. 608 // FIXME?: We also need to do this if unaligned, but we don't know the 609 // alignment here. 610 if (DL.getTypeStoreSize(Ty) < 4) 611 return isLegalMUBUFAddressingMode(AM); 612 613 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) { 614 // SMRD instructions have an 8-bit, dword offset on SI. 615 if (!isUInt<8>(AM.BaseOffs / 4)) 616 return false; 617 } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) { 618 // On CI+, this can also be a 32-bit literal constant offset. If it fits 619 // in 8-bits, it can use a smaller encoding. 620 if (!isUInt<32>(AM.BaseOffs / 4)) 621 return false; 622 } else if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 623 // On VI, these use the SMEM format and the offset is 20-bit in bytes. 624 if (!isUInt<20>(AM.BaseOffs)) 625 return false; 626 } else 627 llvm_unreachable("unhandled generation"); 628 629 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. 630 return true; 631 632 if (AM.Scale == 1 && AM.HasBaseReg) 633 return true; 634 635 return false; 636 637 case AMDGPUAS::PRIVATE_ADDRESS: 638 return isLegalMUBUFAddressingMode(AM); 639 640 case AMDGPUAS::LOCAL_ADDRESS: 641 case AMDGPUAS::REGION_ADDRESS: 642 // Basic, single offset DS instructions allow a 16-bit unsigned immediate 643 // field. 644 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have 645 // an 8-bit dword offset but we don't know the alignment here. 646 if (!isUInt<16>(AM.BaseOffs)) 647 return false; 648 649 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg. 650 return true; 651 652 if (AM.Scale == 1 && AM.HasBaseReg) 653 return true; 654 655 return false; 656 657 case AMDGPUAS::FLAT_ADDRESS: 658 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE: 659 // For an unknown address space, this usually means that this is for some 660 // reason being used for pure arithmetic, and not based on some addressing 661 // computation. We don't have instructions that compute pointers with any 662 // addressing modes, so treat them as having no offset like flat 663 // instructions. 664 return isLegalFlatAddressingMode(AM); 665 666 default: 667 llvm_unreachable("unhandled address space"); 668 } 669 } 670 671 bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT, 672 unsigned AddrSpace, 673 unsigned Align, 674 bool *IsFast) const { 675 if (IsFast) 676 *IsFast = false; 677 678 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96, 679 // which isn't a simple VT. 680 // Until MVT is extended to handle this, simply check for the size and 681 // rely on the condition below: allow accesses if the size is a multiple of 4. 682 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 && 683 VT.getStoreSize() > 16)) { 684 return false; 685 } 686 687 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS || 688 AddrSpace == AMDGPUAS::REGION_ADDRESS) { 689 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte 690 // aligned, 8 byte access in a single operation using ds_read2/write2_b32 691 // with adjacent offsets. 692 bool AlignedBy4 = (Align % 4 == 0); 693 if (IsFast) 694 *IsFast = AlignedBy4; 695 696 return AlignedBy4; 697 } 698 699 // FIXME: We have to be conservative here and assume that flat operations 700 // will access scratch. If we had access to the IR function, then we 701 // could determine if any private memory was used in the function. 702 if (!Subtarget->hasUnalignedScratchAccess() && 703 (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS || 704 AddrSpace == AMDGPUAS::FLAT_ADDRESS)) { 705 return false; 706 } 707 708 if (Subtarget->hasUnalignedBufferAccess()) { 709 // If we have an uniform constant load, it still requires using a slow 710 // buffer instruction if unaligned. 711 if (IsFast) { 712 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ? 713 (Align % 4 == 0) : true; 714 } 715 716 return true; 717 } 718 719 // Smaller than dword value must be aligned. 720 if (VT.bitsLT(MVT::i32)) 721 return false; 722 723 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the 724 // byte-address are ignored, thus forcing Dword alignment. 725 // This applies to private, global, and constant memory. 726 if (IsFast) 727 *IsFast = true; 728 729 return VT.bitsGT(MVT::i32) && Align % 4 == 0; 730 } 731 732 EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign, 733 unsigned SrcAlign, bool IsMemset, 734 bool ZeroMemset, 735 bool MemcpyStrSrc, 736 MachineFunction &MF) const { 737 // FIXME: Should account for address space here. 738 739 // The default fallback uses the private pointer size as a guess for a type to 740 // use. Make sure we switch these to 64-bit accesses. 741 742 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global 743 return MVT::v4i32; 744 745 if (Size >= 8 && DstAlign >= 4) 746 return MVT::v2i32; 747 748 // Use the default. 749 return MVT::Other; 750 } 751 752 static bool isFlatGlobalAddrSpace(unsigned AS) { 753 return AS == AMDGPUAS::GLOBAL_ADDRESS || 754 AS == AMDGPUAS::FLAT_ADDRESS || 755 AS == AMDGPUAS::CONSTANT_ADDRESS; 756 } 757 758 bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS, 759 unsigned DestAS) const { 760 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS); 761 } 762 763 bool SITargetLowering::isMemOpHasNoClobberedMemOperand(const SDNode *N) const { 764 const MemSDNode *MemNode = cast<MemSDNode>(N); 765 const Value *Ptr = MemNode->getMemOperand()->getValue(); 766 const Instruction *I = dyn_cast<Instruction>(Ptr); 767 return I && I->getMetadata("amdgpu.noclobber"); 768 } 769 770 bool SITargetLowering::isCheapAddrSpaceCast(unsigned SrcAS, 771 unsigned DestAS) const { 772 // Flat -> private/local is a simple truncate. 773 // Flat -> global is no-op 774 if (SrcAS == AMDGPUAS::FLAT_ADDRESS) 775 return true; 776 777 return isNoopAddrSpaceCast(SrcAS, DestAS); 778 } 779 780 bool SITargetLowering::isMemOpUniform(const SDNode *N) const { 781 const MemSDNode *MemNode = cast<MemSDNode>(N); 782 783 return AMDGPU::isUniformMMO(MemNode->getMemOperand()); 784 } 785 786 TargetLoweringBase::LegalizeTypeAction 787 SITargetLowering::getPreferredVectorAction(EVT VT) const { 788 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16)) 789 return TypeSplitVector; 790 791 return TargetLoweringBase::getPreferredVectorAction(VT); 792 } 793 794 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, 795 Type *Ty) const { 796 // FIXME: Could be smarter if called for vector constants. 797 return true; 798 } 799 800 bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const { 801 if (Subtarget->has16BitInsts() && VT == MVT::i16) { 802 switch (Op) { 803 case ISD::LOAD: 804 case ISD::STORE: 805 806 // These operations are done with 32-bit instructions anyway. 807 case ISD::AND: 808 case ISD::OR: 809 case ISD::XOR: 810 case ISD::SELECT: 811 // TODO: Extensions? 812 return true; 813 default: 814 return false; 815 } 816 } 817 818 // SimplifySetCC uses this function to determine whether or not it should 819 // create setcc with i1 operands. We don't have instructions for i1 setcc. 820 if (VT == MVT::i1 && Op == ISD::SETCC) 821 return false; 822 823 return TargetLowering::isTypeDesirableForOp(Op, VT); 824 } 825 826 SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG, 827 const SDLoc &SL, SDValue Chain, 828 unsigned Offset) const { 829 const DataLayout &DL = DAG.getDataLayout(); 830 MachineFunction &MF = DAG.getMachineFunction(); 831 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 832 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR); 833 834 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo(); 835 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS); 836 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL, 837 MRI.getLiveInVirtReg(InputPtrReg), PtrVT); 838 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr, 839 DAG.getConstant(Offset, SL, PtrVT)); 840 } 841 842 SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT, 843 const SDLoc &SL, SDValue Chain, 844 unsigned Offset, bool Signed, 845 const ISD::InputArg *Arg) const { 846 const DataLayout &DL = DAG.getDataLayout(); 847 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext()); 848 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); 849 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); 850 851 unsigned Align = DL.getABITypeAlignment(Ty); 852 853 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset); 854 SDValue Load = DAG.getLoad(MemVT, SL, Chain, Ptr, PtrInfo, Align, 855 MachineMemOperand::MONonTemporal | 856 MachineMemOperand::MODereferenceable | 857 MachineMemOperand::MOInvariant); 858 859 SDValue Val = Load; 860 if (Arg && (Arg->Flags.isSExt() || Arg->Flags.isZExt()) && 861 VT.bitsLT(MemVT)) { 862 unsigned Opc = Arg->Flags.isZExt() ? ISD::AssertZext : ISD::AssertSext; 863 Val = DAG.getNode(Opc, SL, MemVT, Val, DAG.getValueType(VT)); 864 } 865 866 if (MemVT.isFloatingPoint()) 867 Val = getFPExtOrFPTrunc(DAG, Val, SL, VT); 868 else if (Signed) 869 Val = DAG.getSExtOrTrunc(Val, SL, VT); 870 else 871 Val = DAG.getZExtOrTrunc(Val, SL, VT); 872 873 return DAG.getMergeValues({ Val, Load.getValue(1) }, SL); 874 } 875 876 SDValue SITargetLowering::LowerFormalArguments( 877 SDValue Chain, CallingConv::ID CallConv, bool isVarArg, 878 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL, 879 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { 880 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 881 882 MachineFunction &MF = DAG.getMachineFunction(); 883 FunctionType *FType = MF.getFunction()->getFunctionType(); 884 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 885 const SISubtarget &ST = MF.getSubtarget<SISubtarget>(); 886 887 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) { 888 const Function *Fn = MF.getFunction(); 889 DiagnosticInfoUnsupported NoGraphicsHSA( 890 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc()); 891 DAG.getContext()->diagnose(NoGraphicsHSA); 892 return DAG.getEntryNode(); 893 } 894 895 // Create stack objects that are used for emitting debugger prologue if 896 // "amdgpu-debugger-emit-prologue" attribute was specified. 897 if (ST.debuggerEmitPrologue()) 898 createDebuggerPrologueStackObjects(MF); 899 900 SmallVector<ISD::InputArg, 16> Splits; 901 BitVector Skipped(Ins.size()); 902 903 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) { 904 const ISD::InputArg &Arg = Ins[i]; 905 906 // First check if it's a PS input addr 907 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() && 908 !Arg.Flags.isByVal() && PSInputNum <= 15) { 909 910 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) { 911 // We can safely skip PS inputs 912 Skipped.set(i); 913 ++PSInputNum; 914 continue; 915 } 916 917 Info->markPSInputAllocated(PSInputNum); 918 if (Arg.Used) 919 Info->PSInputEna |= 1 << PSInputNum; 920 921 ++PSInputNum; 922 } 923 924 if (AMDGPU::isShader(CallConv)) { 925 // Second split vertices into their elements 926 if (Arg.VT.isVector()) { 927 ISD::InputArg NewArg = Arg; 928 NewArg.Flags.setSplit(); 929 NewArg.VT = Arg.VT.getVectorElementType(); 930 931 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a 932 // three or five element vertex only needs three or five registers, 933 // NOT four or eight. 934 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); 935 unsigned NumElements = ParamType->getVectorNumElements(); 936 937 for (unsigned j = 0; j != NumElements; ++j) { 938 Splits.push_back(NewArg); 939 NewArg.PartOffset += NewArg.VT.getStoreSize(); 940 } 941 } else { 942 Splits.push_back(Arg); 943 } 944 } 945 } 946 947 SmallVector<CCValAssign, 16> ArgLocs; 948 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs, 949 *DAG.getContext()); 950 951 // At least one interpolation mode must be enabled or else the GPU will hang. 952 // 953 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set 954 // PSInputAddr, the user wants to enable some bits after the compilation 955 // based on run-time states. Since we can't know what the final PSInputEna 956 // will look like, so we shouldn't do anything here and the user should take 957 // responsibility for the correct programming. 958 // 959 // Otherwise, the following restrictions apply: 960 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled. 961 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be 962 // enabled too. 963 if (CallConv == CallingConv::AMDGPU_PS && 964 ((Info->getPSInputAddr() & 0x7F) == 0 || 965 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) { 966 CCInfo.AllocateReg(AMDGPU::VGPR0); 967 CCInfo.AllocateReg(AMDGPU::VGPR1); 968 Info->markPSInputAllocated(0); 969 Info->PSInputEna |= 1; 970 } 971 972 if (!AMDGPU::isShader(CallConv)) { 973 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX()); 974 } else { 975 assert(!Info->hasDispatchPtr() && 976 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() && 977 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() && 978 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() && 979 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() && 980 !Info->hasWorkItemIDZ()); 981 } 982 983 if (Info->hasPrivateMemoryInputPtr()) { 984 unsigned PrivateMemoryPtrReg = Info->addPrivateMemoryPtr(*TRI); 985 MF.addLiveIn(PrivateMemoryPtrReg, &AMDGPU::SReg_64RegClass); 986 CCInfo.AllocateReg(PrivateMemoryPtrReg); 987 } 988 989 // FIXME: How should these inputs interact with inreg / custom SGPR inputs? 990 if (Info->hasPrivateSegmentBuffer()) { 991 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI); 992 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass); 993 CCInfo.AllocateReg(PrivateSegmentBufferReg); 994 } 995 996 if (Info->hasDispatchPtr()) { 997 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI); 998 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass); 999 CCInfo.AllocateReg(DispatchPtrReg); 1000 } 1001 1002 if (Info->hasQueuePtr()) { 1003 unsigned QueuePtrReg = Info->addQueuePtr(*TRI); 1004 MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass); 1005 CCInfo.AllocateReg(QueuePtrReg); 1006 } 1007 1008 if (Info->hasKernargSegmentPtr()) { 1009 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI); 1010 MF.addLiveIn(InputPtrReg, &AMDGPU::SGPR_64RegClass); 1011 CCInfo.AllocateReg(InputPtrReg); 1012 } 1013 1014 if (Info->hasDispatchID()) { 1015 unsigned DispatchIDReg = Info->addDispatchID(*TRI); 1016 MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass); 1017 CCInfo.AllocateReg(DispatchIDReg); 1018 } 1019 1020 if (Info->hasFlatScratchInit()) { 1021 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI); 1022 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass); 1023 CCInfo.AllocateReg(FlatScratchInitReg); 1024 } 1025 1026 if (!AMDGPU::isShader(CallConv)) 1027 analyzeFormalArgumentsCompute(CCInfo, Ins); 1028 else 1029 AnalyzeFormalArguments(CCInfo, Splits); 1030 1031 SmallVector<SDValue, 16> Chains; 1032 1033 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) { 1034 const ISD::InputArg &Arg = Ins[i]; 1035 if (Skipped[i]) { 1036 InVals.push_back(DAG.getUNDEF(Arg.VT)); 1037 continue; 1038 } 1039 1040 CCValAssign &VA = ArgLocs[ArgIdx++]; 1041 MVT VT = VA.getLocVT(); 1042 1043 if (VA.isMemLoc()) { 1044 VT = Ins[i].VT; 1045 EVT MemVT = VA.getLocVT(); 1046 const unsigned Offset = Subtarget->getExplicitKernelArgOffset(MF) + 1047 VA.getLocMemOffset(); 1048 // The first 36 bytes of the input buffer contains information about 1049 // thread group and global sizes. 1050 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain, 1051 Offset, Ins[i].Flags.isSExt(), 1052 &Ins[i]); 1053 Chains.push_back(Arg.getValue(1)); 1054 1055 auto *ParamTy = 1056 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex())); 1057 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS && 1058 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) { 1059 // On SI local pointers are just offsets into LDS, so they are always 1060 // less than 16-bits. On CI and newer they could potentially be 1061 // real pointers, so we can't guarantee their size. 1062 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg, 1063 DAG.getValueType(MVT::i16)); 1064 } 1065 1066 InVals.push_back(Arg); 1067 Info->setABIArgOffset(Offset + MemVT.getStoreSize()); 1068 continue; 1069 } 1070 assert(VA.isRegLoc() && "Parameter must be in a register!"); 1071 1072 unsigned Reg = VA.getLocReg(); 1073 1074 if (VT == MVT::i64) { 1075 // For now assume it is a pointer 1076 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0, 1077 &AMDGPU::SGPR_64RegClass); 1078 Reg = MF.addLiveIn(Reg, &AMDGPU::SGPR_64RegClass); 1079 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT); 1080 InVals.push_back(Copy); 1081 continue; 1082 } 1083 1084 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT); 1085 1086 Reg = MF.addLiveIn(Reg, RC); 1087 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT); 1088 1089 if (Arg.VT.isVector()) { 1090 // Build a vector from the registers 1091 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex()); 1092 unsigned NumElements = ParamType->getVectorNumElements(); 1093 1094 SmallVector<SDValue, 4> Regs; 1095 Regs.push_back(Val); 1096 for (unsigned j = 1; j != NumElements; ++j) { 1097 Reg = ArgLocs[ArgIdx++].getLocReg(); 1098 Reg = MF.addLiveIn(Reg, RC); 1099 1100 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT); 1101 Regs.push_back(Copy); 1102 } 1103 1104 // Fill up the missing vector elements 1105 NumElements = Arg.VT.getVectorNumElements() - NumElements; 1106 Regs.append(NumElements, DAG.getUNDEF(VT)); 1107 1108 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs)); 1109 continue; 1110 } 1111 1112 InVals.push_back(Val); 1113 } 1114 1115 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read 1116 // these from the dispatch pointer. 1117 1118 // Start adding system SGPRs. 1119 if (Info->hasWorkGroupIDX()) { 1120 unsigned Reg = Info->addWorkGroupIDX(); 1121 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass); 1122 CCInfo.AllocateReg(Reg); 1123 } 1124 1125 if (Info->hasWorkGroupIDY()) { 1126 unsigned Reg = Info->addWorkGroupIDY(); 1127 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass); 1128 CCInfo.AllocateReg(Reg); 1129 } 1130 1131 if (Info->hasWorkGroupIDZ()) { 1132 unsigned Reg = Info->addWorkGroupIDZ(); 1133 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass); 1134 CCInfo.AllocateReg(Reg); 1135 } 1136 1137 if (Info->hasWorkGroupInfo()) { 1138 unsigned Reg = Info->addWorkGroupInfo(); 1139 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass); 1140 CCInfo.AllocateReg(Reg); 1141 } 1142 1143 if (Info->hasPrivateSegmentWaveByteOffset()) { 1144 // Scratch wave offset passed in system SGPR. 1145 unsigned PrivateSegmentWaveByteOffsetReg; 1146 1147 if (AMDGPU::isShader(CallConv)) { 1148 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo); 1149 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg); 1150 } else 1151 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset(); 1152 1153 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass); 1154 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg); 1155 } 1156 1157 // Now that we've figured out where the scratch register inputs are, see if 1158 // should reserve the arguments and use them directly. 1159 bool HasStackObjects = MF.getFrameInfo().hasStackObjects(); 1160 // Record that we know we have non-spill stack objects so we don't need to 1161 // check all stack objects later. 1162 if (HasStackObjects) 1163 Info->setHasNonSpillStackObjects(true); 1164 1165 // Everything live out of a block is spilled with fast regalloc, so it's 1166 // almost certain that spilling will be required. 1167 if (getTargetMachine().getOptLevel() == CodeGenOpt::None) 1168 HasStackObjects = true; 1169 1170 if (ST.isAmdCodeObjectV2(MF)) { 1171 if (HasStackObjects) { 1172 // If we have stack objects, we unquestionably need the private buffer 1173 // resource. For the Code Object V2 ABI, this will be the first 4 user 1174 // SGPR inputs. We can reserve those and use them directly. 1175 1176 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue( 1177 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER); 1178 Info->setScratchRSrcReg(PrivateSegmentBufferReg); 1179 1180 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue( 1181 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 1182 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg); 1183 } else { 1184 unsigned ReservedBufferReg 1185 = TRI->reservedPrivateSegmentBufferReg(MF); 1186 unsigned ReservedOffsetReg 1187 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); 1188 1189 // We tentatively reserve the last registers (skipping the last two 1190 // which may contain VCC). After register allocation, we'll replace 1191 // these with the ones immediately after those which were really 1192 // allocated. In the prologue copies will be inserted from the argument 1193 // to these reserved registers. 1194 Info->setScratchRSrcReg(ReservedBufferReg); 1195 Info->setScratchWaveOffsetReg(ReservedOffsetReg); 1196 } 1197 } else { 1198 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF); 1199 1200 // Without HSA, relocations are used for the scratch pointer and the 1201 // buffer resource setup is always inserted in the prologue. Scratch wave 1202 // offset is still in an input SGPR. 1203 Info->setScratchRSrcReg(ReservedBufferReg); 1204 1205 if (HasStackObjects) { 1206 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue( 1207 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET); 1208 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg); 1209 } else { 1210 unsigned ReservedOffsetReg 1211 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF); 1212 Info->setScratchWaveOffsetReg(ReservedOffsetReg); 1213 } 1214 } 1215 1216 if (Info->hasWorkItemIDX()) { 1217 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X); 1218 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 1219 CCInfo.AllocateReg(Reg); 1220 } 1221 1222 if (Info->hasWorkItemIDY()) { 1223 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y); 1224 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 1225 CCInfo.AllocateReg(Reg); 1226 } 1227 1228 if (Info->hasWorkItemIDZ()) { 1229 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z); 1230 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass); 1231 CCInfo.AllocateReg(Reg); 1232 } 1233 1234 if (Chains.empty()) 1235 return Chain; 1236 1237 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); 1238 } 1239 1240 SDValue 1241 SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, 1242 bool isVarArg, 1243 const SmallVectorImpl<ISD::OutputArg> &Outs, 1244 const SmallVectorImpl<SDValue> &OutVals, 1245 const SDLoc &DL, SelectionDAG &DAG) const { 1246 MachineFunction &MF = DAG.getMachineFunction(); 1247 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1248 1249 if (!AMDGPU::isShader(CallConv)) 1250 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs, 1251 OutVals, DL, DAG); 1252 1253 Info->setIfReturnsVoid(Outs.size() == 0); 1254 1255 SmallVector<ISD::OutputArg, 48> Splits; 1256 SmallVector<SDValue, 48> SplitVals; 1257 1258 // Split vectors into their elements. 1259 for (unsigned i = 0, e = Outs.size(); i != e; ++i) { 1260 const ISD::OutputArg &Out = Outs[i]; 1261 1262 if (Out.VT.isVector()) { 1263 MVT VT = Out.VT.getVectorElementType(); 1264 ISD::OutputArg NewOut = Out; 1265 NewOut.Flags.setSplit(); 1266 NewOut.VT = VT; 1267 1268 // We want the original number of vector elements here, e.g. 1269 // three or five, not four or eight. 1270 unsigned NumElements = Out.ArgVT.getVectorNumElements(); 1271 1272 for (unsigned j = 0; j != NumElements; ++j) { 1273 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i], 1274 DAG.getConstant(j, DL, MVT::i32)); 1275 SplitVals.push_back(Elem); 1276 Splits.push_back(NewOut); 1277 NewOut.PartOffset += NewOut.VT.getStoreSize(); 1278 } 1279 } else { 1280 SplitVals.push_back(OutVals[i]); 1281 Splits.push_back(Out); 1282 } 1283 } 1284 1285 // CCValAssign - represent the assignment of the return value to a location. 1286 SmallVector<CCValAssign, 48> RVLocs; 1287 1288 // CCState - Info about the registers and stack slots. 1289 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, 1290 *DAG.getContext()); 1291 1292 // Analyze outgoing return values. 1293 AnalyzeReturn(CCInfo, Splits); 1294 1295 SDValue Flag; 1296 SmallVector<SDValue, 48> RetOps; 1297 RetOps.push_back(Chain); // Operand #0 = Chain (updated below) 1298 1299 // Copy the result values into the output registers. 1300 for (unsigned i = 0, realRVLocIdx = 0; 1301 i != RVLocs.size(); 1302 ++i, ++realRVLocIdx) { 1303 CCValAssign &VA = RVLocs[i]; 1304 assert(VA.isRegLoc() && "Can only return in registers!"); 1305 1306 SDValue Arg = SplitVals[realRVLocIdx]; 1307 1308 // Copied from other backends. 1309 switch (VA.getLocInfo()) { 1310 default: llvm_unreachable("Unknown loc info!"); 1311 case CCValAssign::Full: 1312 break; 1313 case CCValAssign::BCvt: 1314 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 1315 break; 1316 } 1317 1318 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag); 1319 Flag = Chain.getValue(1); 1320 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 1321 } 1322 1323 // Update chain and glue. 1324 RetOps[0] = Chain; 1325 if (Flag.getNode()) 1326 RetOps.push_back(Flag); 1327 1328 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN; 1329 return DAG.getNode(Opc, DL, MVT::Other, RetOps); 1330 } 1331 1332 unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT, 1333 SelectionDAG &DAG) const { 1334 unsigned Reg = StringSwitch<unsigned>(RegName) 1335 .Case("m0", AMDGPU::M0) 1336 .Case("exec", AMDGPU::EXEC) 1337 .Case("exec_lo", AMDGPU::EXEC_LO) 1338 .Case("exec_hi", AMDGPU::EXEC_HI) 1339 .Case("flat_scratch", AMDGPU::FLAT_SCR) 1340 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO) 1341 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI) 1342 .Default(AMDGPU::NoRegister); 1343 1344 if (Reg == AMDGPU::NoRegister) { 1345 report_fatal_error(Twine("invalid register name \"" 1346 + StringRef(RegName) + "\".")); 1347 1348 } 1349 1350 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS && 1351 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) { 1352 report_fatal_error(Twine("invalid register \"" 1353 + StringRef(RegName) + "\" for subtarget.")); 1354 } 1355 1356 switch (Reg) { 1357 case AMDGPU::M0: 1358 case AMDGPU::EXEC_LO: 1359 case AMDGPU::EXEC_HI: 1360 case AMDGPU::FLAT_SCR_LO: 1361 case AMDGPU::FLAT_SCR_HI: 1362 if (VT.getSizeInBits() == 32) 1363 return Reg; 1364 break; 1365 case AMDGPU::EXEC: 1366 case AMDGPU::FLAT_SCR: 1367 if (VT.getSizeInBits() == 64) 1368 return Reg; 1369 break; 1370 default: 1371 llvm_unreachable("missing register type checking"); 1372 } 1373 1374 report_fatal_error(Twine("invalid type for register \"" 1375 + StringRef(RegName) + "\".")); 1376 } 1377 1378 // If kill is not the last instruction, split the block so kill is always a 1379 // proper terminator. 1380 MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI, 1381 MachineBasicBlock *BB) const { 1382 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1383 1384 MachineBasicBlock::iterator SplitPoint(&MI); 1385 ++SplitPoint; 1386 1387 if (SplitPoint == BB->end()) { 1388 // Don't bother with a new block. 1389 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR)); 1390 return BB; 1391 } 1392 1393 MachineFunction *MF = BB->getParent(); 1394 MachineBasicBlock *SplitBB 1395 = MF->CreateMachineBasicBlock(BB->getBasicBlock()); 1396 1397 MF->insert(++MachineFunction::iterator(BB), SplitBB); 1398 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end()); 1399 1400 SplitBB->transferSuccessorsAndUpdatePHIs(BB); 1401 BB->addSuccessor(SplitBB); 1402 1403 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR)); 1404 return SplitBB; 1405 } 1406 1407 // Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the 1408 // wavefront. If the value is uniform and just happens to be in a VGPR, this 1409 // will only do one iteration. In the worst case, this will loop 64 times. 1410 // 1411 // TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value. 1412 static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop( 1413 const SIInstrInfo *TII, 1414 MachineRegisterInfo &MRI, 1415 MachineBasicBlock &OrigBB, 1416 MachineBasicBlock &LoopBB, 1417 const DebugLoc &DL, 1418 const MachineOperand &IdxReg, 1419 unsigned InitReg, 1420 unsigned ResultReg, 1421 unsigned PhiReg, 1422 unsigned InitSaveExecReg, 1423 int Offset, 1424 bool UseGPRIdxMode) { 1425 MachineBasicBlock::iterator I = LoopBB.begin(); 1426 1427 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1428 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1429 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 1430 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1431 1432 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg) 1433 .addReg(InitReg) 1434 .addMBB(&OrigBB) 1435 .addReg(ResultReg) 1436 .addMBB(&LoopBB); 1437 1438 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec) 1439 .addReg(InitSaveExecReg) 1440 .addMBB(&OrigBB) 1441 .addReg(NewExec) 1442 .addMBB(&LoopBB); 1443 1444 // Read the next variant <- also loop target. 1445 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg) 1446 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef())); 1447 1448 // Compare the just read M0 value to all possible Idx values. 1449 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg) 1450 .addReg(CurrentIdxReg) 1451 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg()); 1452 1453 if (UseGPRIdxMode) { 1454 unsigned IdxReg; 1455 if (Offset == 0) { 1456 IdxReg = CurrentIdxReg; 1457 } else { 1458 IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 1459 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg) 1460 .addReg(CurrentIdxReg, RegState::Kill) 1461 .addImm(Offset); 1462 } 1463 1464 MachineInstr *SetIdx = 1465 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_IDX)) 1466 .addReg(IdxReg, RegState::Kill); 1467 SetIdx->getOperand(2).setIsUndef(); 1468 } else { 1469 // Move index from VCC into M0 1470 if (Offset == 0) { 1471 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 1472 .addReg(CurrentIdxReg, RegState::Kill); 1473 } else { 1474 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) 1475 .addReg(CurrentIdxReg, RegState::Kill) 1476 .addImm(Offset); 1477 } 1478 } 1479 1480 // Update EXEC, save the original EXEC value to VCC. 1481 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec) 1482 .addReg(CondReg, RegState::Kill); 1483 1484 MRI.setSimpleHint(NewExec, CondReg); 1485 1486 // Update EXEC, switch all done bits to 0 and all todo bits to 1. 1487 MachineInstr *InsertPt = 1488 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC) 1489 .addReg(AMDGPU::EXEC) 1490 .addReg(NewExec); 1491 1492 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use 1493 // s_cbranch_scc0? 1494 1495 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover. 1496 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ)) 1497 .addMBB(&LoopBB); 1498 1499 return InsertPt->getIterator(); 1500 } 1501 1502 // This has slightly sub-optimal regalloc when the source vector is killed by 1503 // the read. The register allocator does not understand that the kill is 1504 // per-workitem, so is kept alive for the whole loop so we end up not re-using a 1505 // subregister from it, using 1 more VGPR than necessary. This was saved when 1506 // this was expanded after register allocation. 1507 static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII, 1508 MachineBasicBlock &MBB, 1509 MachineInstr &MI, 1510 unsigned InitResultReg, 1511 unsigned PhiReg, 1512 int Offset, 1513 bool UseGPRIdxMode) { 1514 MachineFunction *MF = MBB.getParent(); 1515 MachineRegisterInfo &MRI = MF->getRegInfo(); 1516 const DebugLoc &DL = MI.getDebugLoc(); 1517 MachineBasicBlock::iterator I(&MI); 1518 1519 unsigned DstReg = MI.getOperand(0).getReg(); 1520 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1521 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1522 1523 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec); 1524 1525 // Save the EXEC mask 1526 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec) 1527 .addReg(AMDGPU::EXEC); 1528 1529 // To insert the loop we need to split the block. Move everything after this 1530 // point to a new block, and insert a new empty block between the two. 1531 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock(); 1532 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock(); 1533 MachineFunction::iterator MBBI(MBB); 1534 ++MBBI; 1535 1536 MF->insert(MBBI, LoopBB); 1537 MF->insert(MBBI, RemainderBB); 1538 1539 LoopBB->addSuccessor(LoopBB); 1540 LoopBB->addSuccessor(RemainderBB); 1541 1542 // Move the rest of the block into a new block. 1543 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); 1544 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end()); 1545 1546 MBB.addSuccessor(LoopBB); 1547 1548 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 1549 1550 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx, 1551 InitResultReg, DstReg, PhiReg, TmpExec, 1552 Offset, UseGPRIdxMode); 1553 1554 MachineBasicBlock::iterator First = RemainderBB->begin(); 1555 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC) 1556 .addReg(SaveExec); 1557 1558 return InsPt; 1559 } 1560 1561 // Returns subreg index, offset 1562 static std::pair<unsigned, int> 1563 computeIndirectRegAndOffset(const SIRegisterInfo &TRI, 1564 const TargetRegisterClass *SuperRC, 1565 unsigned VecReg, 1566 int Offset) { 1567 int NumElts = SuperRC->getSize() / 4; 1568 1569 // Skip out of bounds offsets, or else we would end up using an undefined 1570 // register. 1571 if (Offset >= NumElts || Offset < 0) 1572 return std::make_pair(AMDGPU::sub0, Offset); 1573 1574 return std::make_pair(AMDGPU::sub0 + Offset, 0); 1575 } 1576 1577 // Return true if the index is an SGPR and was set. 1578 static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII, 1579 MachineRegisterInfo &MRI, 1580 MachineInstr &MI, 1581 int Offset, 1582 bool UseGPRIdxMode, 1583 bool IsIndirectSrc) { 1584 MachineBasicBlock *MBB = MI.getParent(); 1585 const DebugLoc &DL = MI.getDebugLoc(); 1586 MachineBasicBlock::iterator I(&MI); 1587 1588 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 1589 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg()); 1590 1591 assert(Idx->getReg() != AMDGPU::NoRegister); 1592 1593 if (!TII->getRegisterInfo().isSGPRClass(IdxRC)) 1594 return false; 1595 1596 if (UseGPRIdxMode) { 1597 unsigned IdxMode = IsIndirectSrc ? 1598 VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE; 1599 if (Offset == 0) { 1600 MachineInstr *SetOn = 1601 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1602 .add(*Idx) 1603 .addImm(IdxMode); 1604 1605 SetOn->getOperand(3).setIsUndef(); 1606 } else { 1607 unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 1608 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp) 1609 .add(*Idx) 1610 .addImm(Offset); 1611 MachineInstr *SetOn = 1612 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1613 .addReg(Tmp, RegState::Kill) 1614 .addImm(IdxMode); 1615 1616 SetOn->getOperand(3).setIsUndef(); 1617 } 1618 1619 return true; 1620 } 1621 1622 if (Offset == 0) { 1623 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 1624 .add(*Idx); 1625 } else { 1626 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0) 1627 .add(*Idx) 1628 .addImm(Offset); 1629 } 1630 1631 return true; 1632 } 1633 1634 // Control flow needs to be inserted if indexing with a VGPR. 1635 static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI, 1636 MachineBasicBlock &MBB, 1637 const SISubtarget &ST) { 1638 const SIInstrInfo *TII = ST.getInstrInfo(); 1639 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 1640 MachineFunction *MF = MBB.getParent(); 1641 MachineRegisterInfo &MRI = MF->getRegInfo(); 1642 1643 unsigned Dst = MI.getOperand(0).getReg(); 1644 unsigned SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg(); 1645 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); 1646 1647 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg); 1648 1649 unsigned SubReg; 1650 std::tie(SubReg, Offset) 1651 = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset); 1652 1653 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode; 1654 1655 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) { 1656 MachineBasicBlock::iterator I(&MI); 1657 const DebugLoc &DL = MI.getDebugLoc(); 1658 1659 if (UseGPRIdxMode) { 1660 // TODO: Look at the uses to avoid the copy. This may require rescheduling 1661 // to avoid interfering with other uses, so probably requires a new 1662 // optimization pass. 1663 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) 1664 .addReg(SrcReg, RegState::Undef, SubReg) 1665 .addReg(SrcReg, RegState::Implicit) 1666 .addReg(AMDGPU::M0, RegState::Implicit); 1667 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1668 } else { 1669 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) 1670 .addReg(SrcReg, RegState::Undef, SubReg) 1671 .addReg(SrcReg, RegState::Implicit); 1672 } 1673 1674 MI.eraseFromParent(); 1675 1676 return &MBB; 1677 } 1678 1679 const DebugLoc &DL = MI.getDebugLoc(); 1680 MachineBasicBlock::iterator I(&MI); 1681 1682 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1683 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1684 1685 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg); 1686 1687 if (UseGPRIdxMode) { 1688 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1689 .addImm(0) // Reset inside loop. 1690 .addImm(VGPRIndexMode::SRC0_ENABLE); 1691 SetOn->getOperand(3).setIsUndef(); 1692 1693 // Disable again after the loop. 1694 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1695 } 1696 1697 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset, UseGPRIdxMode); 1698 MachineBasicBlock *LoopBB = InsPt->getParent(); 1699 1700 if (UseGPRIdxMode) { 1701 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst) 1702 .addReg(SrcReg, RegState::Undef, SubReg) 1703 .addReg(SrcReg, RegState::Implicit) 1704 .addReg(AMDGPU::M0, RegState::Implicit); 1705 } else { 1706 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst) 1707 .addReg(SrcReg, RegState::Undef, SubReg) 1708 .addReg(SrcReg, RegState::Implicit); 1709 } 1710 1711 MI.eraseFromParent(); 1712 1713 return LoopBB; 1714 } 1715 1716 static unsigned getMOVRELDPseudo(const TargetRegisterClass *VecRC) { 1717 switch (VecRC->getSize()) { 1718 case 4: 1719 return AMDGPU::V_MOVRELD_B32_V1; 1720 case 8: 1721 return AMDGPU::V_MOVRELD_B32_V2; 1722 case 16: 1723 return AMDGPU::V_MOVRELD_B32_V4; 1724 case 32: 1725 return AMDGPU::V_MOVRELD_B32_V8; 1726 case 64: 1727 return AMDGPU::V_MOVRELD_B32_V16; 1728 default: 1729 llvm_unreachable("unsupported size for MOVRELD pseudos"); 1730 } 1731 } 1732 1733 static MachineBasicBlock *emitIndirectDst(MachineInstr &MI, 1734 MachineBasicBlock &MBB, 1735 const SISubtarget &ST) { 1736 const SIInstrInfo *TII = ST.getInstrInfo(); 1737 const SIRegisterInfo &TRI = TII->getRegisterInfo(); 1738 MachineFunction *MF = MBB.getParent(); 1739 MachineRegisterInfo &MRI = MF->getRegInfo(); 1740 1741 unsigned Dst = MI.getOperand(0).getReg(); 1742 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src); 1743 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx); 1744 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val); 1745 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm(); 1746 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg()); 1747 1748 // This can be an immediate, but will be folded later. 1749 assert(Val->getReg()); 1750 1751 unsigned SubReg; 1752 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC, 1753 SrcVec->getReg(), 1754 Offset); 1755 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode; 1756 1757 if (Idx->getReg() == AMDGPU::NoRegister) { 1758 MachineBasicBlock::iterator I(&MI); 1759 const DebugLoc &DL = MI.getDebugLoc(); 1760 1761 assert(Offset == 0); 1762 1763 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst) 1764 .add(*SrcVec) 1765 .add(*Val) 1766 .addImm(SubReg); 1767 1768 MI.eraseFromParent(); 1769 return &MBB; 1770 } 1771 1772 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) { 1773 MachineBasicBlock::iterator I(&MI); 1774 const DebugLoc &DL = MI.getDebugLoc(); 1775 1776 if (UseGPRIdxMode) { 1777 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect)) 1778 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst 1779 .add(*Val) 1780 .addReg(Dst, RegState::ImplicitDefine) 1781 .addReg(SrcVec->getReg(), RegState::Implicit) 1782 .addReg(AMDGPU::M0, RegState::Implicit); 1783 1784 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1785 } else { 1786 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(VecRC)); 1787 1788 BuildMI(MBB, I, DL, MovRelDesc) 1789 .addReg(Dst, RegState::Define) 1790 .addReg(SrcVec->getReg()) 1791 .add(*Val) 1792 .addImm(SubReg - AMDGPU::sub0); 1793 } 1794 1795 MI.eraseFromParent(); 1796 return &MBB; 1797 } 1798 1799 if (Val->isReg()) 1800 MRI.clearKillFlags(Val->getReg()); 1801 1802 const DebugLoc &DL = MI.getDebugLoc(); 1803 1804 if (UseGPRIdxMode) { 1805 MachineBasicBlock::iterator I(&MI); 1806 1807 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON)) 1808 .addImm(0) // Reset inside loop. 1809 .addImm(VGPRIndexMode::DST_ENABLE); 1810 SetOn->getOperand(3).setIsUndef(); 1811 1812 // Disable again after the loop. 1813 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF)); 1814 } 1815 1816 unsigned PhiReg = MRI.createVirtualRegister(VecRC); 1817 1818 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg, 1819 Offset, UseGPRIdxMode); 1820 MachineBasicBlock *LoopBB = InsPt->getParent(); 1821 1822 if (UseGPRIdxMode) { 1823 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect)) 1824 .addReg(PhiReg, RegState::Undef, SubReg) // vdst 1825 .add(*Val) // src0 1826 .addReg(Dst, RegState::ImplicitDefine) 1827 .addReg(PhiReg, RegState::Implicit) 1828 .addReg(AMDGPU::M0, RegState::Implicit); 1829 } else { 1830 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(VecRC)); 1831 1832 BuildMI(*LoopBB, InsPt, DL, MovRelDesc) 1833 .addReg(Dst, RegState::Define) 1834 .addReg(PhiReg) 1835 .add(*Val) 1836 .addImm(SubReg - AMDGPU::sub0); 1837 } 1838 1839 MI.eraseFromParent(); 1840 1841 return LoopBB; 1842 } 1843 1844 MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter( 1845 MachineInstr &MI, MachineBasicBlock *BB) const { 1846 1847 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1848 MachineFunction *MF = BB->getParent(); 1849 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 1850 1851 if (TII->isMIMG(MI)) { 1852 if (!MI.memoperands_empty()) 1853 return BB; 1854 // Add a memoperand for mimg instructions so that they aren't assumed to 1855 // be ordered memory instuctions. 1856 1857 MachinePointerInfo PtrInfo(MFI->getImagePSV()); 1858 MachineMemOperand::Flags Flags = MachineMemOperand::MODereferenceable; 1859 if (MI.mayStore()) 1860 Flags |= MachineMemOperand::MOStore; 1861 1862 if (MI.mayLoad()) 1863 Flags |= MachineMemOperand::MOLoad; 1864 1865 auto MMO = MF->getMachineMemOperand(PtrInfo, Flags, 0, 0); 1866 MI.addMemOperand(*MF, MMO); 1867 return BB; 1868 } 1869 1870 switch (MI.getOpcode()) { 1871 case AMDGPU::S_TRAP_PSEUDO: { 1872 const DebugLoc &DL = MI.getDebugLoc(); 1873 const int TrapType = MI.getOperand(0).getImm(); 1874 1875 if (Subtarget->getTrapHandlerAbi() == SISubtarget::TrapHandlerAbiHsa && 1876 Subtarget->isTrapHandlerEnabled()) { 1877 1878 MachineFunction *MF = BB->getParent(); 1879 SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>(); 1880 unsigned UserSGPR = Info->getQueuePtrUserSGPR(); 1881 assert(UserSGPR != AMDGPU::NoRegister); 1882 1883 if (!BB->isLiveIn(UserSGPR)) 1884 BB->addLiveIn(UserSGPR); 1885 1886 BuildMI(*BB, MI, DL, TII->get(AMDGPU::COPY), AMDGPU::SGPR0_SGPR1) 1887 .addReg(UserSGPR); 1888 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_TRAP)) 1889 .addImm(TrapType) 1890 .addReg(AMDGPU::SGPR0_SGPR1, RegState::Implicit); 1891 } else { 1892 switch (TrapType) { 1893 case SISubtarget::TrapIDLLVMTrap: 1894 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_ENDPGM)); 1895 break; 1896 case SISubtarget::TrapIDLLVMDebugTrap: { 1897 DiagnosticInfoUnsupported NoTrap(*MF->getFunction(), 1898 "debugtrap handler not supported", 1899 DL, 1900 DS_Warning); 1901 LLVMContext &C = MF->getFunction()->getContext(); 1902 C.diagnose(NoTrap); 1903 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_NOP)) 1904 .addImm(0); 1905 break; 1906 } 1907 default: 1908 llvm_unreachable("unsupported trap handler type!"); 1909 } 1910 } 1911 1912 MI.eraseFromParent(); 1913 return BB; 1914 } 1915 case AMDGPU::SI_INIT_M0: 1916 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(), 1917 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0) 1918 .add(MI.getOperand(0)); 1919 MI.eraseFromParent(); 1920 return BB; 1921 1922 case AMDGPU::GET_GROUPSTATICSIZE: { 1923 DebugLoc DL = MI.getDebugLoc(); 1924 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32)) 1925 .add(MI.getOperand(0)) 1926 .addImm(MFI->getLDSSize()); 1927 MI.eraseFromParent(); 1928 return BB; 1929 } 1930 case AMDGPU::SI_INDIRECT_SRC_V1: 1931 case AMDGPU::SI_INDIRECT_SRC_V2: 1932 case AMDGPU::SI_INDIRECT_SRC_V4: 1933 case AMDGPU::SI_INDIRECT_SRC_V8: 1934 case AMDGPU::SI_INDIRECT_SRC_V16: 1935 return emitIndirectSrc(MI, *BB, *getSubtarget()); 1936 case AMDGPU::SI_INDIRECT_DST_V1: 1937 case AMDGPU::SI_INDIRECT_DST_V2: 1938 case AMDGPU::SI_INDIRECT_DST_V4: 1939 case AMDGPU::SI_INDIRECT_DST_V8: 1940 case AMDGPU::SI_INDIRECT_DST_V16: 1941 return emitIndirectDst(MI, *BB, *getSubtarget()); 1942 case AMDGPU::SI_KILL: 1943 return splitKillBlock(MI, BB); 1944 case AMDGPU::V_CNDMASK_B64_PSEUDO: { 1945 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 1946 1947 unsigned Dst = MI.getOperand(0).getReg(); 1948 unsigned Src0 = MI.getOperand(1).getReg(); 1949 unsigned Src1 = MI.getOperand(2).getReg(); 1950 const DebugLoc &DL = MI.getDebugLoc(); 1951 unsigned SrcCond = MI.getOperand(3).getReg(); 1952 1953 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1954 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1955 1956 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo) 1957 .addReg(Src0, 0, AMDGPU::sub0) 1958 .addReg(Src1, 0, AMDGPU::sub0) 1959 .addReg(SrcCond); 1960 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi) 1961 .addReg(Src0, 0, AMDGPU::sub1) 1962 .addReg(Src1, 0, AMDGPU::sub1) 1963 .addReg(SrcCond); 1964 1965 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst) 1966 .addReg(DstLo) 1967 .addImm(AMDGPU::sub0) 1968 .addReg(DstHi) 1969 .addImm(AMDGPU::sub1); 1970 MI.eraseFromParent(); 1971 return BB; 1972 } 1973 case AMDGPU::SI_BR_UNDEF: { 1974 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 1975 const DebugLoc &DL = MI.getDebugLoc(); 1976 MachineInstr *Br = BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CBRANCH_SCC1)) 1977 .add(MI.getOperand(0)); 1978 Br->getOperand(1).setIsUndef(true); // read undef SCC 1979 MI.eraseFromParent(); 1980 return BB; 1981 } 1982 default: 1983 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB); 1984 } 1985 } 1986 1987 bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const { 1988 // This currently forces unfolding various combinations of fsub into fma with 1989 // free fneg'd operands. As long as we have fast FMA (controlled by 1990 // isFMAFasterThanFMulAndFAdd), we should perform these. 1991 1992 // When fma is quarter rate, for f64 where add / sub are at best half rate, 1993 // most of these combines appear to be cycle neutral but save on instruction 1994 // count / code size. 1995 return true; 1996 } 1997 1998 EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx, 1999 EVT VT) const { 2000 if (!VT.isVector()) { 2001 return MVT::i1; 2002 } 2003 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements()); 2004 } 2005 2006 MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT VT) const { 2007 // TODO: Should i16 be used always if legal? For now it would force VALU 2008 // shifts. 2009 return (VT == MVT::i16) ? MVT::i16 : MVT::i32; 2010 } 2011 2012 // Answering this is somewhat tricky and depends on the specific device which 2013 // have different rates for fma or all f64 operations. 2014 // 2015 // v_fma_f64 and v_mul_f64 always take the same number of cycles as each other 2016 // regardless of which device (although the number of cycles differs between 2017 // devices), so it is always profitable for f64. 2018 // 2019 // v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable 2020 // only on full rate devices. Normally, we should prefer selecting v_mad_f32 2021 // which we can always do even without fused FP ops since it returns the same 2022 // result as the separate operations and since it is always full 2023 // rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32 2024 // however does not support denormals, so we do report fma as faster if we have 2025 // a fast fma device and require denormals. 2026 // 2027 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 2028 VT = VT.getScalarType(); 2029 2030 switch (VT.getSimpleVT().SimpleTy) { 2031 case MVT::f32: 2032 // This is as fast on some subtargets. However, we always have full rate f32 2033 // mad available which returns the same result as the separate operations 2034 // which we should prefer over fma. We can't use this if we want to support 2035 // denormals, so only report this in these cases. 2036 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32(); 2037 case MVT::f64: 2038 return true; 2039 case MVT::f16: 2040 return Subtarget->has16BitInsts() && Subtarget->hasFP16Denormals(); 2041 default: 2042 break; 2043 } 2044 2045 return false; 2046 } 2047 2048 //===----------------------------------------------------------------------===// 2049 // Custom DAG Lowering Operations 2050 //===----------------------------------------------------------------------===// 2051 2052 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { 2053 switch (Op.getOpcode()) { 2054 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG); 2055 case ISD::BRCOND: return LowerBRCOND(Op, DAG); 2056 case ISD::LOAD: { 2057 SDValue Result = LowerLOAD(Op, DAG); 2058 assert((!Result.getNode() || 2059 Result.getNode()->getNumValues() == 2) && 2060 "Load should return a value and a chain"); 2061 return Result; 2062 } 2063 2064 case ISD::FSIN: 2065 case ISD::FCOS: 2066 return LowerTrig(Op, DAG); 2067 case ISD::SELECT: return LowerSELECT(Op, DAG); 2068 case ISD::FDIV: return LowerFDIV(Op, DAG); 2069 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG); 2070 case ISD::STORE: return LowerSTORE(Op, DAG); 2071 case ISD::GlobalAddress: { 2072 MachineFunction &MF = DAG.getMachineFunction(); 2073 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 2074 return LowerGlobalAddress(MFI, Op, DAG); 2075 } 2076 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); 2077 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG); 2078 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG); 2079 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG); 2080 case ISD::INSERT_VECTOR_ELT: 2081 return lowerINSERT_VECTOR_ELT(Op, DAG); 2082 case ISD::EXTRACT_VECTOR_ELT: 2083 return lowerEXTRACT_VECTOR_ELT(Op, DAG); 2084 case ISD::FP_ROUND: 2085 return lowerFP_ROUND(Op, DAG); 2086 } 2087 return SDValue(); 2088 } 2089 2090 void SITargetLowering::ReplaceNodeResults(SDNode *N, 2091 SmallVectorImpl<SDValue> &Results, 2092 SelectionDAG &DAG) const { 2093 switch (N->getOpcode()) { 2094 case ISD::INSERT_VECTOR_ELT: { 2095 if (SDValue Res = lowerINSERT_VECTOR_ELT(SDValue(N, 0), DAG)) 2096 Results.push_back(Res); 2097 return; 2098 } 2099 case ISD::EXTRACT_VECTOR_ELT: { 2100 if (SDValue Res = lowerEXTRACT_VECTOR_ELT(SDValue(N, 0), DAG)) 2101 Results.push_back(Res); 2102 return; 2103 } 2104 case ISD::INTRINSIC_WO_CHAIN: { 2105 unsigned IID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 2106 switch (IID) { 2107 case Intrinsic::amdgcn_cvt_pkrtz: { 2108 SDValue Src0 = N->getOperand(1); 2109 SDValue Src1 = N->getOperand(2); 2110 SDLoc SL(N); 2111 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_PKRTZ_F16_F32, SL, MVT::i32, 2112 Src0, Src1); 2113 2114 Results.push_back(DAG.getNode(ISD::BITCAST, SL, MVT::v2f16, Cvt)); 2115 return; 2116 } 2117 default: 2118 break; 2119 } 2120 } 2121 default: 2122 break; 2123 } 2124 } 2125 2126 /// \brief Helper function for LowerBRCOND 2127 static SDNode *findUser(SDValue Value, unsigned Opcode) { 2128 2129 SDNode *Parent = Value.getNode(); 2130 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end(); 2131 I != E; ++I) { 2132 2133 if (I.getUse().get() != Value) 2134 continue; 2135 2136 if (I->getOpcode() == Opcode) 2137 return *I; 2138 } 2139 return nullptr; 2140 } 2141 2142 bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const { 2143 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) { 2144 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) { 2145 case AMDGPUIntrinsic::amdgcn_if: 2146 case AMDGPUIntrinsic::amdgcn_else: 2147 case AMDGPUIntrinsic::amdgcn_end_cf: 2148 case AMDGPUIntrinsic::amdgcn_loop: 2149 return true; 2150 default: 2151 return false; 2152 } 2153 } 2154 2155 if (Intr->getOpcode() == ISD::INTRINSIC_WO_CHAIN) { 2156 switch (cast<ConstantSDNode>(Intr->getOperand(0))->getZExtValue()) { 2157 case AMDGPUIntrinsic::amdgcn_break: 2158 case AMDGPUIntrinsic::amdgcn_if_break: 2159 case AMDGPUIntrinsic::amdgcn_else_break: 2160 return true; 2161 default: 2162 return false; 2163 } 2164 } 2165 2166 return false; 2167 } 2168 2169 void SITargetLowering::createDebuggerPrologueStackObjects( 2170 MachineFunction &MF) const { 2171 // Create stack objects that are used for emitting debugger prologue. 2172 // 2173 // Debugger prologue writes work group IDs and work item IDs to scratch memory 2174 // at fixed location in the following format: 2175 // offset 0: work group ID x 2176 // offset 4: work group ID y 2177 // offset 8: work group ID z 2178 // offset 16: work item ID x 2179 // offset 20: work item ID y 2180 // offset 24: work item ID z 2181 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 2182 int ObjectIdx = 0; 2183 2184 // For each dimension: 2185 for (unsigned i = 0; i < 3; ++i) { 2186 // Create fixed stack object for work group ID. 2187 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true); 2188 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx); 2189 // Create fixed stack object for work item ID. 2190 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true); 2191 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx); 2192 } 2193 } 2194 2195 bool SITargetLowering::shouldEmitFixup(const GlobalValue *GV) const { 2196 const Triple &TT = getTargetMachine().getTargetTriple(); 2197 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS && 2198 AMDGPU::shouldEmitConstantsToTextSection(TT); 2199 } 2200 2201 bool SITargetLowering::shouldEmitGOTReloc(const GlobalValue *GV) const { 2202 return (GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS || 2203 GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) && 2204 !shouldEmitFixup(GV) && 2205 !getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV); 2206 } 2207 2208 bool SITargetLowering::shouldEmitPCReloc(const GlobalValue *GV) const { 2209 return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV); 2210 } 2211 2212 /// This transforms the control flow intrinsics to get the branch destination as 2213 /// last parameter, also switches branch target with BR if the need arise 2214 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND, 2215 SelectionDAG &DAG) const { 2216 SDLoc DL(BRCOND); 2217 2218 SDNode *Intr = BRCOND.getOperand(1).getNode(); 2219 SDValue Target = BRCOND.getOperand(2); 2220 SDNode *BR = nullptr; 2221 SDNode *SetCC = nullptr; 2222 2223 if (Intr->getOpcode() == ISD::SETCC) { 2224 // As long as we negate the condition everything is fine 2225 SetCC = Intr; 2226 Intr = SetCC->getOperand(0).getNode(); 2227 2228 } else { 2229 // Get the target from BR if we don't negate the condition 2230 BR = findUser(BRCOND, ISD::BR); 2231 Target = BR->getOperand(1); 2232 } 2233 2234 // FIXME: This changes the types of the intrinsics instead of introducing new 2235 // nodes with the correct types. 2236 // e.g. llvm.amdgcn.loop 2237 2238 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3 2239 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088> 2240 2241 if (!isCFIntrinsic(Intr)) { 2242 // This is a uniform branch so we don't need to legalize. 2243 return BRCOND; 2244 } 2245 2246 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID || 2247 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN; 2248 2249 assert(!SetCC || 2250 (SetCC->getConstantOperandVal(1) == 1 && 2251 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() == 2252 ISD::SETNE)); 2253 2254 // operands of the new intrinsic call 2255 SmallVector<SDValue, 4> Ops; 2256 if (HaveChain) 2257 Ops.push_back(BRCOND.getOperand(0)); 2258 2259 Ops.append(Intr->op_begin() + (HaveChain ? 1 : 0), Intr->op_end()); 2260 Ops.push_back(Target); 2261 2262 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end()); 2263 2264 // build the new intrinsic call 2265 SDNode *Result = DAG.getNode( 2266 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL, 2267 DAG.getVTList(Res), Ops).getNode(); 2268 2269 if (!HaveChain) { 2270 SDValue Ops[] = { 2271 SDValue(Result, 0), 2272 BRCOND.getOperand(0) 2273 }; 2274 2275 Result = DAG.getMergeValues(Ops, DL).getNode(); 2276 } 2277 2278 if (BR) { 2279 // Give the branch instruction our target 2280 SDValue Ops[] = { 2281 BR->getOperand(0), 2282 BRCOND.getOperand(2) 2283 }; 2284 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops); 2285 DAG.ReplaceAllUsesWith(BR, NewBR.getNode()); 2286 BR = NewBR.getNode(); 2287 } 2288 2289 SDValue Chain = SDValue(Result, Result->getNumValues() - 1); 2290 2291 // Copy the intrinsic results to registers 2292 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) { 2293 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg); 2294 if (!CopyToReg) 2295 continue; 2296 2297 Chain = DAG.getCopyToReg( 2298 Chain, DL, 2299 CopyToReg->getOperand(1), 2300 SDValue(Result, i - 1), 2301 SDValue()); 2302 2303 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0)); 2304 } 2305 2306 // Remove the old intrinsic from the chain 2307 DAG.ReplaceAllUsesOfValueWith( 2308 SDValue(Intr, Intr->getNumValues() - 1), 2309 Intr->getOperand(0)); 2310 2311 return Chain; 2312 } 2313 2314 SDValue SITargetLowering::getFPExtOrFPTrunc(SelectionDAG &DAG, 2315 SDValue Op, 2316 const SDLoc &DL, 2317 EVT VT) const { 2318 return Op.getValueType().bitsLE(VT) ? 2319 DAG.getNode(ISD::FP_EXTEND, DL, VT, Op) : 2320 DAG.getNode(ISD::FTRUNC, DL, VT, Op); 2321 } 2322 2323 SDValue SITargetLowering::lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { 2324 assert(Op.getValueType() == MVT::f16 && 2325 "Do not know how to custom lower FP_ROUND for non-f16 type"); 2326 2327 SDValue Src = Op.getOperand(0); 2328 EVT SrcVT = Src.getValueType(); 2329 if (SrcVT != MVT::f64) 2330 return Op; 2331 2332 SDLoc DL(Op); 2333 2334 SDValue FpToFp16 = DAG.getNode(ISD::FP_TO_FP16, DL, MVT::i32, Src); 2335 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16); 2336 return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc);; 2337 } 2338 2339 SDValue SITargetLowering::getSegmentAperture(unsigned AS, 2340 SelectionDAG &DAG) const { 2341 2342 if (Subtarget->hasApertureRegs()) { // Read from Aperture Registers directly. 2343 unsigned RegNo = (AS == AMDGPUAS::LOCAL_ADDRESS) ? AMDGPU::SRC_SHARED_BASE : 2344 AMDGPU::SRC_PRIVATE_BASE; 2345 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass, RegNo, MVT::i32); 2346 } 2347 2348 SDLoc SL; 2349 MachineFunction &MF = DAG.getMachineFunction(); 2350 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 2351 unsigned UserSGPR = Info->getQueuePtrUserSGPR(); 2352 assert(UserSGPR != AMDGPU::NoRegister); 2353 2354 SDValue QueuePtr = CreateLiveInRegister( 2355 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64); 2356 2357 // Offset into amd_queue_t for group_segment_aperture_base_hi / 2358 // private_segment_aperture_base_hi. 2359 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44; 2360 2361 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr, 2362 DAG.getConstant(StructOffset, SL, MVT::i64)); 2363 2364 // TODO: Use custom target PseudoSourceValue. 2365 // TODO: We should use the value from the IR intrinsic call, but it might not 2366 // be available and how do we get it? 2367 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()), 2368 AMDGPUAS::CONSTANT_ADDRESS)); 2369 2370 MachinePointerInfo PtrInfo(V, StructOffset); 2371 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo, 2372 MinAlign(64, StructOffset), 2373 MachineMemOperand::MODereferenceable | 2374 MachineMemOperand::MOInvariant); 2375 } 2376 2377 SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op, 2378 SelectionDAG &DAG) const { 2379 SDLoc SL(Op); 2380 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op); 2381 2382 SDValue Src = ASC->getOperand(0); 2383 2384 // FIXME: Really support non-0 null pointers. 2385 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32); 2386 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64); 2387 2388 // flat -> local/private 2389 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) { 2390 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || 2391 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { 2392 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE); 2393 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src); 2394 2395 return DAG.getNode(ISD::SELECT, SL, MVT::i32, 2396 NonNull, Ptr, SegmentNullPtr); 2397 } 2398 } 2399 2400 // local/private -> flat 2401 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) { 2402 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS || 2403 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) { 2404 SDValue NonNull 2405 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE); 2406 2407 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG); 2408 SDValue CvtPtr 2409 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture); 2410 2411 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull, 2412 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr), 2413 FlatNullPtr); 2414 } 2415 } 2416 2417 // global <-> flat are no-ops and never emitted. 2418 2419 const MachineFunction &MF = DAG.getMachineFunction(); 2420 DiagnosticInfoUnsupported InvalidAddrSpaceCast( 2421 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc()); 2422 DAG.getContext()->diagnose(InvalidAddrSpaceCast); 2423 2424 return DAG.getUNDEF(ASC->getValueType(0)); 2425 } 2426 2427 SDValue SITargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op, 2428 SelectionDAG &DAG) const { 2429 SDValue Idx = Op.getOperand(2); 2430 if (isa<ConstantSDNode>(Idx)) 2431 return SDValue(); 2432 2433 // Avoid stack access for dynamic indexing. 2434 SDLoc SL(Op); 2435 SDValue Vec = Op.getOperand(0); 2436 SDValue Val = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Op.getOperand(1)); 2437 2438 // v_bfi_b32 (v_bfm_b32 16, (shl idx, 16)), val, vec 2439 SDValue ExtVal = DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i32, Val); 2440 2441 // Convert vector index to bit-index. 2442 SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, 2443 DAG.getConstant(16, SL, MVT::i32)); 2444 2445 SDValue BCVec = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Vec); 2446 2447 SDValue BFM = DAG.getNode(ISD::SHL, SL, MVT::i32, 2448 DAG.getConstant(0xffff, SL, MVT::i32), 2449 ScaledIdx); 2450 2451 SDValue LHS = DAG.getNode(ISD::AND, SL, MVT::i32, BFM, ExtVal); 2452 SDValue RHS = DAG.getNode(ISD::AND, SL, MVT::i32, 2453 DAG.getNOT(SL, BFM, MVT::i32), BCVec); 2454 2455 SDValue BFI = DAG.getNode(ISD::OR, SL, MVT::i32, LHS, RHS); 2456 return DAG.getNode(ISD::BITCAST, SL, Op.getValueType(), BFI); 2457 } 2458 2459 SDValue SITargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op, 2460 SelectionDAG &DAG) const { 2461 SDLoc SL(Op); 2462 2463 EVT ResultVT = Op.getValueType(); 2464 SDValue Vec = Op.getOperand(0); 2465 SDValue Idx = Op.getOperand(1); 2466 2467 if (const ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx)) { 2468 SDValue Result = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Vec); 2469 2470 if (CIdx->getZExtValue() == 1) { 2471 Result = DAG.getNode(ISD::SRL, SL, MVT::i32, Result, 2472 DAG.getConstant(16, SL, MVT::i32)); 2473 } else { 2474 assert(CIdx->getZExtValue() == 0); 2475 } 2476 2477 if (ResultVT.bitsLT(MVT::i32)) 2478 Result = DAG.getNode(ISD::TRUNCATE, SL, MVT::i16, Result); 2479 return DAG.getNode(ISD::BITCAST, SL, ResultVT, Result); 2480 } 2481 2482 SDValue Sixteen = DAG.getConstant(16, SL, MVT::i32); 2483 2484 // Convert vector index to bit-index. 2485 SDValue ScaledIdx = DAG.getNode(ISD::SHL, SL, MVT::i32, Idx, Sixteen); 2486 2487 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::i32, Vec); 2488 SDValue Elt = DAG.getNode(ISD::SRL, SL, MVT::i32, BC, ScaledIdx); 2489 2490 SDValue Result = Elt; 2491 if (ResultVT.bitsLT(MVT::i32)) 2492 Result = DAG.getNode(ISD::TRUNCATE, SL, MVT::i16, Result); 2493 2494 return DAG.getNode(ISD::BITCAST, SL, ResultVT, Result); 2495 } 2496 2497 bool 2498 SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 2499 // We can fold offsets for anything that doesn't require a GOT relocation. 2500 return (GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS || 2501 GA->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) && 2502 !shouldEmitGOTReloc(GA->getGlobal()); 2503 } 2504 2505 static SDValue 2506 buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV, 2507 const SDLoc &DL, unsigned Offset, EVT PtrVT, 2508 unsigned GAFlags = SIInstrInfo::MO_NONE) { 2509 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is 2510 // lowered to the following code sequence: 2511 // 2512 // For constant address space: 2513 // s_getpc_b64 s[0:1] 2514 // s_add_u32 s0, s0, $symbol 2515 // s_addc_u32 s1, s1, 0 2516 // 2517 // s_getpc_b64 returns the address of the s_add_u32 instruction and then 2518 // a fixup or relocation is emitted to replace $symbol with a literal 2519 // constant, which is a pc-relative offset from the encoding of the $symbol 2520 // operand to the global variable. 2521 // 2522 // For global address space: 2523 // s_getpc_b64 s[0:1] 2524 // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo 2525 // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi 2526 // 2527 // s_getpc_b64 returns the address of the s_add_u32 instruction and then 2528 // fixups or relocations are emitted to replace $symbol@*@lo and 2529 // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant, 2530 // which is a 64-bit pc-relative offset from the encoding of the $symbol 2531 // operand to the global variable. 2532 // 2533 // What we want here is an offset from the value returned by s_getpc 2534 // (which is the address of the s_add_u32 instruction) to the global 2535 // variable, but since the encoding of $symbol starts 4 bytes after the start 2536 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too 2537 // small. This requires us to add 4 to the global variable offset in order to 2538 // compute the correct address. 2539 SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, 2540 GAFlags); 2541 SDValue PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4, 2542 GAFlags == SIInstrInfo::MO_NONE ? 2543 GAFlags : GAFlags + 1); 2544 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi); 2545 } 2546 2547 SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI, 2548 SDValue Op, 2549 SelectionDAG &DAG) const { 2550 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op); 2551 2552 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS && 2553 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS) 2554 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG); 2555 2556 SDLoc DL(GSD); 2557 const GlobalValue *GV = GSD->getGlobal(); 2558 EVT PtrVT = Op.getValueType(); 2559 2560 if (shouldEmitFixup(GV)) 2561 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT); 2562 else if (shouldEmitPCReloc(GV)) 2563 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT, 2564 SIInstrInfo::MO_REL32); 2565 2566 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT, 2567 SIInstrInfo::MO_GOTPCREL32); 2568 2569 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext()); 2570 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS); 2571 const DataLayout &DataLayout = DAG.getDataLayout(); 2572 unsigned Align = DataLayout.getABITypeAlignment(PtrTy); 2573 // FIXME: Use a PseudoSourceValue once those can be assigned an address space. 2574 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy)); 2575 2576 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align, 2577 MachineMemOperand::MODereferenceable | 2578 MachineMemOperand::MOInvariant); 2579 } 2580 2581 SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, 2582 const SDLoc &DL, SDValue V) const { 2583 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as 2584 // the destination register. 2585 // 2586 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions, 2587 // so we will end up with redundant moves to m0. 2588 // 2589 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result. 2590 2591 // A Null SDValue creates a glue result. 2592 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue, 2593 V, Chain); 2594 return SDValue(M0, 0); 2595 } 2596 2597 SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG, 2598 SDValue Op, 2599 MVT VT, 2600 unsigned Offset) const { 2601 SDLoc SL(Op); 2602 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL, 2603 DAG.getEntryNode(), Offset, false); 2604 // The local size values will have the hi 16-bits as zero. 2605 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param, 2606 DAG.getValueType(VT)); 2607 } 2608 2609 static SDValue emitNonHSAIntrinsicError(SelectionDAG &DAG, const SDLoc &DL, 2610 EVT VT) { 2611 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(), 2612 "non-hsa intrinsic with hsa target", 2613 DL.getDebugLoc()); 2614 DAG.getContext()->diagnose(BadIntrin); 2615 return DAG.getUNDEF(VT); 2616 } 2617 2618 static SDValue emitRemovedIntrinsicError(SelectionDAG &DAG, const SDLoc &DL, 2619 EVT VT) { 2620 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(), 2621 "intrinsic not supported on subtarget", 2622 DL.getDebugLoc()); 2623 DAG.getContext()->diagnose(BadIntrin); 2624 return DAG.getUNDEF(VT); 2625 } 2626 2627 SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, 2628 SelectionDAG &DAG) const { 2629 MachineFunction &MF = DAG.getMachineFunction(); 2630 auto MFI = MF.getInfo<SIMachineFunctionInfo>(); 2631 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo(); 2632 2633 EVT VT = Op.getValueType(); 2634 SDLoc DL(Op); 2635 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); 2636 2637 // TODO: Should this propagate fast-math-flags? 2638 2639 switch (IntrinsicID) { 2640 case Intrinsic::amdgcn_implicit_buffer_ptr: { 2641 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER); 2642 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT); 2643 } 2644 case Intrinsic::amdgcn_dispatch_ptr: 2645 case Intrinsic::amdgcn_queue_ptr: { 2646 if (!Subtarget->isAmdCodeObjectV2(MF)) { 2647 DiagnosticInfoUnsupported BadIntrin( 2648 *MF.getFunction(), "unsupported hsa intrinsic without hsa target", 2649 DL.getDebugLoc()); 2650 DAG.getContext()->diagnose(BadIntrin); 2651 return DAG.getUNDEF(VT); 2652 } 2653 2654 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ? 2655 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR; 2656 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, 2657 TRI->getPreloadedValue(MF, Reg), VT); 2658 } 2659 case Intrinsic::amdgcn_implicitarg_ptr: { 2660 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT); 2661 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset); 2662 } 2663 case Intrinsic::amdgcn_kernarg_segment_ptr: { 2664 unsigned Reg 2665 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR); 2666 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT); 2667 } 2668 case Intrinsic::amdgcn_dispatch_id: { 2669 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID); 2670 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT); 2671 } 2672 case Intrinsic::amdgcn_rcp: 2673 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1)); 2674 case Intrinsic::amdgcn_rsq: 2675 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 2676 case Intrinsic::amdgcn_rsq_legacy: 2677 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 2678 return emitRemovedIntrinsicError(DAG, DL, VT); 2679 2680 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1)); 2681 case Intrinsic::amdgcn_rcp_legacy: 2682 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 2683 return emitRemovedIntrinsicError(DAG, DL, VT); 2684 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1)); 2685 case Intrinsic::amdgcn_rsq_clamp: { 2686 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS) 2687 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1)); 2688 2689 Type *Type = VT.getTypeForEVT(*DAG.getContext()); 2690 APFloat Max = APFloat::getLargest(Type->getFltSemantics()); 2691 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true); 2692 2693 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1)); 2694 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq, 2695 DAG.getConstantFP(Max, DL, VT)); 2696 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp, 2697 DAG.getConstantFP(Min, DL, VT)); 2698 } 2699 case Intrinsic::r600_read_ngroups_x: 2700 if (Subtarget->isAmdHsaOS()) 2701 return emitNonHSAIntrinsicError(DAG, DL, VT); 2702 2703 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2704 SI::KernelInputOffsets::NGROUPS_X, false); 2705 case Intrinsic::r600_read_ngroups_y: 2706 if (Subtarget->isAmdHsaOS()) 2707 return emitNonHSAIntrinsicError(DAG, DL, VT); 2708 2709 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2710 SI::KernelInputOffsets::NGROUPS_Y, false); 2711 case Intrinsic::r600_read_ngroups_z: 2712 if (Subtarget->isAmdHsaOS()) 2713 return emitNonHSAIntrinsicError(DAG, DL, VT); 2714 2715 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2716 SI::KernelInputOffsets::NGROUPS_Z, false); 2717 case Intrinsic::r600_read_global_size_x: 2718 if (Subtarget->isAmdHsaOS()) 2719 return emitNonHSAIntrinsicError(DAG, DL, VT); 2720 2721 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2722 SI::KernelInputOffsets::GLOBAL_SIZE_X, false); 2723 case Intrinsic::r600_read_global_size_y: 2724 if (Subtarget->isAmdHsaOS()) 2725 return emitNonHSAIntrinsicError(DAG, DL, VT); 2726 2727 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2728 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false); 2729 case Intrinsic::r600_read_global_size_z: 2730 if (Subtarget->isAmdHsaOS()) 2731 return emitNonHSAIntrinsicError(DAG, DL, VT); 2732 2733 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 2734 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false); 2735 case Intrinsic::r600_read_local_size_x: 2736 if (Subtarget->isAmdHsaOS()) 2737 return emitNonHSAIntrinsicError(DAG, DL, VT); 2738 2739 return lowerImplicitZextParam(DAG, Op, MVT::i16, 2740 SI::KernelInputOffsets::LOCAL_SIZE_X); 2741 case Intrinsic::r600_read_local_size_y: 2742 if (Subtarget->isAmdHsaOS()) 2743 return emitNonHSAIntrinsicError(DAG, DL, VT); 2744 2745 return lowerImplicitZextParam(DAG, Op, MVT::i16, 2746 SI::KernelInputOffsets::LOCAL_SIZE_Y); 2747 case Intrinsic::r600_read_local_size_z: 2748 if (Subtarget->isAmdHsaOS()) 2749 return emitNonHSAIntrinsicError(DAG, DL, VT); 2750 2751 return lowerImplicitZextParam(DAG, Op, MVT::i16, 2752 SI::KernelInputOffsets::LOCAL_SIZE_Z); 2753 case Intrinsic::amdgcn_workgroup_id_x: 2754 case Intrinsic::r600_read_tgid_x: 2755 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass, 2756 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT); 2757 case Intrinsic::amdgcn_workgroup_id_y: 2758 case Intrinsic::r600_read_tgid_y: 2759 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass, 2760 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT); 2761 case Intrinsic::amdgcn_workgroup_id_z: 2762 case Intrinsic::r600_read_tgid_z: 2763 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass, 2764 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT); 2765 case Intrinsic::amdgcn_workitem_id_x: 2766 case Intrinsic::r600_read_tidig_x: 2767 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 2768 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT); 2769 case Intrinsic::amdgcn_workitem_id_y: 2770 case Intrinsic::r600_read_tidig_y: 2771 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 2772 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT); 2773 case Intrinsic::amdgcn_workitem_id_z: 2774 case Intrinsic::r600_read_tidig_z: 2775 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass, 2776 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT); 2777 case AMDGPUIntrinsic::SI_load_const: { 2778 SDValue Ops[] = { 2779 Op.getOperand(1), 2780 Op.getOperand(2) 2781 }; 2782 2783 MachineMemOperand *MMO = MF.getMachineMemOperand( 2784 MachinePointerInfo(), 2785 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable | 2786 MachineMemOperand::MOInvariant, 2787 VT.getStoreSize(), 4); 2788 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL, 2789 Op->getVTList(), Ops, VT, MMO); 2790 } 2791 case AMDGPUIntrinsic::amdgcn_fdiv_fast: 2792 return lowerFDIV_FAST(Op, DAG); 2793 case AMDGPUIntrinsic::SI_vs_load_input: 2794 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT, 2795 Op.getOperand(1), 2796 Op.getOperand(2), 2797 Op.getOperand(3)); 2798 case Intrinsic::amdgcn_interp_mov: { 2799 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4)); 2800 SDValue Glue = M0.getValue(1); 2801 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32, Op.getOperand(1), 2802 Op.getOperand(2), Op.getOperand(3), Glue); 2803 } 2804 case Intrinsic::amdgcn_interp_p1: { 2805 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4)); 2806 SDValue Glue = M0.getValue(1); 2807 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1), 2808 Op.getOperand(2), Op.getOperand(3), Glue); 2809 } 2810 case Intrinsic::amdgcn_interp_p2: { 2811 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5)); 2812 SDValue Glue = SDValue(M0.getNode(), 1); 2813 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1), 2814 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4), 2815 Glue); 2816 } 2817 case Intrinsic::amdgcn_sin: 2818 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1)); 2819 2820 case Intrinsic::amdgcn_cos: 2821 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1)); 2822 2823 case Intrinsic::amdgcn_log_clamp: { 2824 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS) 2825 return SDValue(); 2826 2827 DiagnosticInfoUnsupported BadIntrin( 2828 *MF.getFunction(), "intrinsic not supported on subtarget", 2829 DL.getDebugLoc()); 2830 DAG.getContext()->diagnose(BadIntrin); 2831 return DAG.getUNDEF(VT); 2832 } 2833 case Intrinsic::amdgcn_ldexp: 2834 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT, 2835 Op.getOperand(1), Op.getOperand(2)); 2836 2837 case Intrinsic::amdgcn_fract: 2838 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1)); 2839 2840 case Intrinsic::amdgcn_class: 2841 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT, 2842 Op.getOperand(1), Op.getOperand(2)); 2843 case Intrinsic::amdgcn_div_fmas: 2844 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT, 2845 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), 2846 Op.getOperand(4)); 2847 2848 case Intrinsic::amdgcn_div_fixup: 2849 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT, 2850 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 2851 2852 case Intrinsic::amdgcn_trig_preop: 2853 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT, 2854 Op.getOperand(1), Op.getOperand(2)); 2855 case Intrinsic::amdgcn_div_scale: { 2856 // 3rd parameter required to be a constant. 2857 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 2858 if (!Param) 2859 return DAG.getUNDEF(VT); 2860 2861 // Translate to the operands expected by the machine instruction. The 2862 // first parameter must be the same as the first instruction. 2863 SDValue Numerator = Op.getOperand(1); 2864 SDValue Denominator = Op.getOperand(2); 2865 2866 // Note this order is opposite of the machine instruction's operations, 2867 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The 2868 // intrinsic has the numerator as the first operand to match a normal 2869 // division operation. 2870 2871 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator; 2872 2873 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0, 2874 Denominator, Numerator); 2875 } 2876 case Intrinsic::amdgcn_icmp: { 2877 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 2878 if (!CD) 2879 return DAG.getUNDEF(VT); 2880 2881 int CondCode = CD->getSExtValue(); 2882 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE || 2883 CondCode > ICmpInst::Predicate::LAST_ICMP_PREDICATE) 2884 return DAG.getUNDEF(VT); 2885 2886 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode); 2887 ISD::CondCode CCOpcode = getICmpCondCode(IcInput); 2888 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1), 2889 Op.getOperand(2), DAG.getCondCode(CCOpcode)); 2890 } 2891 case Intrinsic::amdgcn_fcmp: { 2892 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3)); 2893 if (!CD) 2894 return DAG.getUNDEF(VT); 2895 2896 int CondCode = CD->getSExtValue(); 2897 if (CondCode < FCmpInst::Predicate::FIRST_FCMP_PREDICATE || 2898 CondCode > FCmpInst::Predicate::LAST_FCMP_PREDICATE) 2899 return DAG.getUNDEF(VT); 2900 2901 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode); 2902 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput); 2903 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1), 2904 Op.getOperand(2), DAG.getCondCode(CCOpcode)); 2905 } 2906 case Intrinsic::amdgcn_fmed3: 2907 return DAG.getNode(AMDGPUISD::FMED3, DL, VT, 2908 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 2909 case Intrinsic::amdgcn_fmul_legacy: 2910 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT, 2911 Op.getOperand(1), Op.getOperand(2)); 2912 case Intrinsic::amdgcn_sffbh: 2913 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1)); 2914 case Intrinsic::amdgcn_sbfe: 2915 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT, 2916 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 2917 case Intrinsic::amdgcn_ubfe: 2918 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT, 2919 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); 2920 case Intrinsic::amdgcn_cvt_pkrtz: { 2921 // FIXME: Stop adding cast if v2f16 legal. 2922 EVT VT = Op.getValueType(); 2923 SDValue Node = DAG.getNode(AMDGPUISD::CVT_PKRTZ_F16_F32, DL, MVT::i32, 2924 Op.getOperand(1), Op.getOperand(2)); 2925 return DAG.getNode(ISD::BITCAST, DL, VT, Node); 2926 } 2927 case AMDGPUIntrinsic::SI_packf16: { // Legacy name 2928 EVT VT = Op.getValueType(); 2929 return DAG.getNode(AMDGPUISD::CVT_PKRTZ_F16_F32, DL, VT, 2930 Op.getOperand(1), Op.getOperand(2)); 2931 } 2932 default: 2933 return AMDGPUTargetLowering::LowerOperation(Op, DAG); 2934 } 2935 } 2936 2937 SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op, 2938 SelectionDAG &DAG) const { 2939 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 2940 SDLoc DL(Op); 2941 switch (IntrID) { 2942 case Intrinsic::amdgcn_atomic_inc: 2943 case Intrinsic::amdgcn_atomic_dec: { 2944 MemSDNode *M = cast<MemSDNode>(Op); 2945 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ? 2946 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC; 2947 SDValue Ops[] = { 2948 M->getOperand(0), // Chain 2949 M->getOperand(2), // Ptr 2950 M->getOperand(3) // Value 2951 }; 2952 2953 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops, 2954 M->getMemoryVT(), M->getMemOperand()); 2955 } 2956 case Intrinsic::amdgcn_buffer_load: 2957 case Intrinsic::amdgcn_buffer_load_format: { 2958 SDValue Ops[] = { 2959 Op.getOperand(0), // Chain 2960 Op.getOperand(2), // rsrc 2961 Op.getOperand(3), // vindex 2962 Op.getOperand(4), // offset 2963 Op.getOperand(5), // glc 2964 Op.getOperand(6) // slc 2965 }; 2966 MachineFunction &MF = DAG.getMachineFunction(); 2967 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 2968 2969 unsigned Opc = (IntrID == Intrinsic::amdgcn_buffer_load) ? 2970 AMDGPUISD::BUFFER_LOAD : AMDGPUISD::BUFFER_LOAD_FORMAT; 2971 EVT VT = Op.getValueType(); 2972 EVT IntVT = VT.changeTypeToInteger(); 2973 2974 MachineMemOperand *MMO = MF.getMachineMemOperand( 2975 MachinePointerInfo(MFI->getBufferPSV()), 2976 MachineMemOperand::MOLoad, 2977 VT.getStoreSize(), VT.getStoreSize()); 2978 2979 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, IntVT, MMO); 2980 } 2981 default: 2982 return SDValue(); 2983 } 2984 } 2985 2986 SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op, 2987 SelectionDAG &DAG) const { 2988 MachineFunction &MF = DAG.getMachineFunction(); 2989 SDLoc DL(Op); 2990 SDValue Chain = Op.getOperand(0); 2991 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); 2992 2993 switch (IntrinsicID) { 2994 case Intrinsic::amdgcn_exp: { 2995 const ConstantSDNode *Tgt = cast<ConstantSDNode>(Op.getOperand(2)); 2996 const ConstantSDNode *En = cast<ConstantSDNode>(Op.getOperand(3)); 2997 const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(8)); 2998 const ConstantSDNode *VM = cast<ConstantSDNode>(Op.getOperand(9)); 2999 3000 const SDValue Ops[] = { 3001 Chain, 3002 DAG.getTargetConstant(Tgt->getZExtValue(), DL, MVT::i8), // tgt 3003 DAG.getTargetConstant(En->getZExtValue(), DL, MVT::i8), // en 3004 Op.getOperand(4), // src0 3005 Op.getOperand(5), // src1 3006 Op.getOperand(6), // src2 3007 Op.getOperand(7), // src3 3008 DAG.getTargetConstant(0, DL, MVT::i1), // compr 3009 DAG.getTargetConstant(VM->getZExtValue(), DL, MVT::i1) 3010 }; 3011 3012 unsigned Opc = Done->isNullValue() ? 3013 AMDGPUISD::EXPORT : AMDGPUISD::EXPORT_DONE; 3014 return DAG.getNode(Opc, DL, Op->getVTList(), Ops); 3015 } 3016 case Intrinsic::amdgcn_exp_compr: { 3017 const ConstantSDNode *Tgt = cast<ConstantSDNode>(Op.getOperand(2)); 3018 const ConstantSDNode *En = cast<ConstantSDNode>(Op.getOperand(3)); 3019 SDValue Src0 = Op.getOperand(4); 3020 SDValue Src1 = Op.getOperand(5); 3021 const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(6)); 3022 const ConstantSDNode *VM = cast<ConstantSDNode>(Op.getOperand(7)); 3023 3024 SDValue Undef = DAG.getUNDEF(MVT::f32); 3025 const SDValue Ops[] = { 3026 Chain, 3027 DAG.getTargetConstant(Tgt->getZExtValue(), DL, MVT::i8), // tgt 3028 DAG.getTargetConstant(En->getZExtValue(), DL, MVT::i8), // en 3029 DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src0), 3030 DAG.getNode(ISD::BITCAST, DL, MVT::f32, Src1), 3031 Undef, // src2 3032 Undef, // src3 3033 DAG.getTargetConstant(1, DL, MVT::i1), // compr 3034 DAG.getTargetConstant(VM->getZExtValue(), DL, MVT::i1) 3035 }; 3036 3037 unsigned Opc = Done->isNullValue() ? 3038 AMDGPUISD::EXPORT : AMDGPUISD::EXPORT_DONE; 3039 return DAG.getNode(Opc, DL, Op->getVTList(), Ops); 3040 } 3041 case Intrinsic::amdgcn_s_sendmsg: 3042 case Intrinsic::amdgcn_s_sendmsghalt: { 3043 unsigned NodeOp = (IntrinsicID == Intrinsic::amdgcn_s_sendmsg) ? 3044 AMDGPUISD::SENDMSG : AMDGPUISD::SENDMSGHALT; 3045 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3)); 3046 SDValue Glue = Chain.getValue(1); 3047 return DAG.getNode(NodeOp, DL, MVT::Other, Chain, 3048 Op.getOperand(2), Glue); 3049 } 3050 case AMDGPUIntrinsic::SI_tbuffer_store: { 3051 SDValue Ops[] = { 3052 Chain, 3053 Op.getOperand(2), 3054 Op.getOperand(3), 3055 Op.getOperand(4), 3056 Op.getOperand(5), 3057 Op.getOperand(6), 3058 Op.getOperand(7), 3059 Op.getOperand(8), 3060 Op.getOperand(9), 3061 Op.getOperand(10), 3062 Op.getOperand(11), 3063 Op.getOperand(12), 3064 Op.getOperand(13), 3065 Op.getOperand(14) 3066 }; 3067 3068 EVT VT = Op.getOperand(3).getValueType(); 3069 3070 MachineMemOperand *MMO = MF.getMachineMemOperand( 3071 MachinePointerInfo(), 3072 MachineMemOperand::MOStore, 3073 VT.getStoreSize(), 4); 3074 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL, 3075 Op->getVTList(), Ops, VT, MMO); 3076 } 3077 case AMDGPUIntrinsic::AMDGPU_kill: { 3078 SDValue Src = Op.getOperand(2); 3079 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) { 3080 if (!K->isNegative()) 3081 return Chain; 3082 3083 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32); 3084 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne); 3085 } 3086 3087 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src); 3088 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast); 3089 } 3090 case AMDGPUIntrinsic::SI_export: { // Legacy intrinsic. 3091 const ConstantSDNode *En = cast<ConstantSDNode>(Op.getOperand(2)); 3092 const ConstantSDNode *VM = cast<ConstantSDNode>(Op.getOperand(3)); 3093 const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(4)); 3094 const ConstantSDNode *Tgt = cast<ConstantSDNode>(Op.getOperand(5)); 3095 const ConstantSDNode *Compr = cast<ConstantSDNode>(Op.getOperand(6)); 3096 3097 const SDValue Ops[] = { 3098 Chain, 3099 DAG.getTargetConstant(Tgt->getZExtValue(), DL, MVT::i8), 3100 DAG.getTargetConstant(En->getZExtValue(), DL, MVT::i8), 3101 Op.getOperand(7), // src0 3102 Op.getOperand(8), // src1 3103 Op.getOperand(9), // src2 3104 Op.getOperand(10), // src3 3105 DAG.getTargetConstant(Compr->getZExtValue(), DL, MVT::i1), 3106 DAG.getTargetConstant(VM->getZExtValue(), DL, MVT::i1) 3107 }; 3108 3109 unsigned Opc = Done->isNullValue() ? 3110 AMDGPUISD::EXPORT : AMDGPUISD::EXPORT_DONE; 3111 return DAG.getNode(Opc, DL, Op->getVTList(), Ops); 3112 } 3113 default: 3114 return SDValue(); 3115 } 3116 } 3117 3118 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const { 3119 SDLoc DL(Op); 3120 LoadSDNode *Load = cast<LoadSDNode>(Op); 3121 ISD::LoadExtType ExtType = Load->getExtensionType(); 3122 EVT MemVT = Load->getMemoryVT(); 3123 3124 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) { 3125 // FIXME: Copied from PPC 3126 // First, load into 32 bits, then truncate to 1 bit. 3127 3128 SDValue Chain = Load->getChain(); 3129 SDValue BasePtr = Load->getBasePtr(); 3130 MachineMemOperand *MMO = Load->getMemOperand(); 3131 3132 EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16; 3133 3134 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain, 3135 BasePtr, RealMemVT, MMO); 3136 3137 SDValue Ops[] = { 3138 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD), 3139 NewLD.getValue(1) 3140 }; 3141 3142 return DAG.getMergeValues(Ops, DL); 3143 } 3144 3145 if (!MemVT.isVector()) 3146 return SDValue(); 3147 3148 assert(Op.getValueType().getVectorElementType() == MVT::i32 && 3149 "Custom lowering for non-i32 vectors hasn't been implemented."); 3150 3151 unsigned AS = Load->getAddressSpace(); 3152 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT, 3153 AS, Load->getAlignment())) { 3154 SDValue Ops[2]; 3155 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG); 3156 return DAG.getMergeValues(Ops, DL); 3157 } 3158 3159 MachineFunction &MF = DAG.getMachineFunction(); 3160 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 3161 // If there is a possibilty that flat instruction access scratch memory 3162 // then we need to use the same legalization rules we use for private. 3163 if (AS == AMDGPUAS::FLAT_ADDRESS) 3164 AS = MFI->hasFlatScratchInit() ? 3165 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS; 3166 3167 unsigned NumElements = MemVT.getVectorNumElements(); 3168 switch (AS) { 3169 case AMDGPUAS::CONSTANT_ADDRESS: 3170 if (isMemOpUniform(Load)) 3171 return SDValue(); 3172 // Non-uniform loads will be selected to MUBUF instructions, so they 3173 // have the same legalization requirements as global and private 3174 // loads. 3175 // 3176 LLVM_FALLTHROUGH; 3177 case AMDGPUAS::GLOBAL_ADDRESS: 3178 if (Subtarget->getScalarizeGlobalBehavior() && isMemOpUniform(Load) && 3179 isMemOpHasNoClobberedMemOperand(Load)) 3180 return SDValue(); 3181 // Non-uniform loads will be selected to MUBUF instructions, so they 3182 // have the same legalization requirements as global and private 3183 // loads. 3184 // 3185 LLVM_FALLTHROUGH; 3186 case AMDGPUAS::FLAT_ADDRESS: 3187 if (NumElements > 4) 3188 return SplitVectorLoad(Op, DAG); 3189 // v4 loads are supported for private and global memory. 3190 return SDValue(); 3191 case AMDGPUAS::PRIVATE_ADDRESS: 3192 // Depending on the setting of the private_element_size field in the 3193 // resource descriptor, we can only make private accesses up to a certain 3194 // size. 3195 switch (Subtarget->getMaxPrivateElementSize()) { 3196 case 4: 3197 return scalarizeVectorLoad(Load, DAG); 3198 case 8: 3199 if (NumElements > 2) 3200 return SplitVectorLoad(Op, DAG); 3201 return SDValue(); 3202 case 16: 3203 // Same as global/flat 3204 if (NumElements > 4) 3205 return SplitVectorLoad(Op, DAG); 3206 return SDValue(); 3207 default: 3208 llvm_unreachable("unsupported private_element_size"); 3209 } 3210 case AMDGPUAS::LOCAL_ADDRESS: 3211 if (NumElements > 2) 3212 return SplitVectorLoad(Op, DAG); 3213 3214 if (NumElements == 2) 3215 return SDValue(); 3216 3217 // If properly aligned, if we split we might be able to use ds_read_b64. 3218 return SplitVectorLoad(Op, DAG); 3219 default: 3220 return SDValue(); 3221 } 3222 } 3223 3224 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { 3225 if (Op.getValueType() != MVT::i64) 3226 return SDValue(); 3227 3228 SDLoc DL(Op); 3229 SDValue Cond = Op.getOperand(0); 3230 3231 SDValue Zero = DAG.getConstant(0, DL, MVT::i32); 3232 SDValue One = DAG.getConstant(1, DL, MVT::i32); 3233 3234 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1)); 3235 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2)); 3236 3237 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero); 3238 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero); 3239 3240 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1); 3241 3242 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One); 3243 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One); 3244 3245 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1); 3246 3247 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi}); 3248 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res); 3249 } 3250 3251 // Catch division cases where we can use shortcuts with rcp and rsq 3252 // instructions. 3253 SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op, 3254 SelectionDAG &DAG) const { 3255 SDLoc SL(Op); 3256 SDValue LHS = Op.getOperand(0); 3257 SDValue RHS = Op.getOperand(1); 3258 EVT VT = Op.getValueType(); 3259 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath; 3260 3261 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) { 3262 if (Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()) || 3263 VT == MVT::f16) { 3264 if (CLHS->isExactlyValue(1.0)) { 3265 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to 3266 // the CI documentation has a worst case error of 1 ulp. 3267 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to 3268 // use it as long as we aren't trying to use denormals. 3269 // 3270 // v_rcp_f16 and v_rsq_f16 DO support denormals. 3271 3272 // 1.0 / sqrt(x) -> rsq(x) 3273 3274 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP 3275 // error seems really high at 2^29 ULP. 3276 if (RHS.getOpcode() == ISD::FSQRT) 3277 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0)); 3278 3279 // 1.0 / x -> rcp(x) 3280 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 3281 } 3282 3283 // Same as for 1.0, but expand the sign out of the constant. 3284 if (CLHS->isExactlyValue(-1.0)) { 3285 // -1.0 / x -> rcp (fneg x) 3286 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 3287 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS); 3288 } 3289 } 3290 } 3291 3292 const SDNodeFlags *Flags = Op->getFlags(); 3293 3294 if (Unsafe || Flags->hasAllowReciprocal()) { 3295 // Turn into multiply by the reciprocal. 3296 // x / y -> x * (1.0 / y) 3297 SDNodeFlags Flags; 3298 Flags.setUnsafeAlgebra(true); 3299 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS); 3300 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags); 3301 } 3302 3303 return SDValue(); 3304 } 3305 3306 static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL, 3307 EVT VT, SDValue A, SDValue B, SDValue GlueChain) { 3308 if (GlueChain->getNumValues() <= 1) { 3309 return DAG.getNode(Opcode, SL, VT, A, B); 3310 } 3311 3312 assert(GlueChain->getNumValues() == 3); 3313 3314 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue); 3315 switch (Opcode) { 3316 default: llvm_unreachable("no chain equivalent for opcode"); 3317 case ISD::FMUL: 3318 Opcode = AMDGPUISD::FMUL_W_CHAIN; 3319 break; 3320 } 3321 3322 return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B, 3323 GlueChain.getValue(2)); 3324 } 3325 3326 static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL, 3327 EVT VT, SDValue A, SDValue B, SDValue C, 3328 SDValue GlueChain) { 3329 if (GlueChain->getNumValues() <= 1) { 3330 return DAG.getNode(Opcode, SL, VT, A, B, C); 3331 } 3332 3333 assert(GlueChain->getNumValues() == 3); 3334 3335 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue); 3336 switch (Opcode) { 3337 default: llvm_unreachable("no chain equivalent for opcode"); 3338 case ISD::FMA: 3339 Opcode = AMDGPUISD::FMA_W_CHAIN; 3340 break; 3341 } 3342 3343 return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B, C, 3344 GlueChain.getValue(2)); 3345 } 3346 3347 SDValue SITargetLowering::LowerFDIV16(SDValue Op, SelectionDAG &DAG) const { 3348 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG)) 3349 return FastLowered; 3350 3351 SDLoc SL(Op); 3352 SDValue Src0 = Op.getOperand(0); 3353 SDValue Src1 = Op.getOperand(1); 3354 3355 SDValue CvtSrc0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0); 3356 SDValue CvtSrc1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1); 3357 3358 SDValue RcpSrc1 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, CvtSrc1); 3359 SDValue Quot = DAG.getNode(ISD::FMUL, SL, MVT::f32, CvtSrc0, RcpSrc1); 3360 3361 SDValue FPRoundFlag = DAG.getTargetConstant(0, SL, MVT::i32); 3362 SDValue BestQuot = DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Quot, FPRoundFlag); 3363 3364 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f16, BestQuot, Src1, Src0); 3365 } 3366 3367 // Faster 2.5 ULP division that does not support denormals. 3368 SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const { 3369 SDLoc SL(Op); 3370 SDValue LHS = Op.getOperand(1); 3371 SDValue RHS = Op.getOperand(2); 3372 3373 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS); 3374 3375 const APFloat K0Val(BitsToFloat(0x6f800000)); 3376 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32); 3377 3378 const APFloat K1Val(BitsToFloat(0x2f800000)); 3379 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32); 3380 3381 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 3382 3383 EVT SetCCVT = 3384 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32); 3385 3386 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT); 3387 3388 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One); 3389 3390 // TODO: Should this propagate fast-math-flags? 3391 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3); 3392 3393 // rcp does not support denormals. 3394 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1); 3395 3396 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0); 3397 3398 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul); 3399 } 3400 3401 SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const { 3402 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG)) 3403 return FastLowered; 3404 3405 SDLoc SL(Op); 3406 SDValue LHS = Op.getOperand(0); 3407 SDValue RHS = Op.getOperand(1); 3408 3409 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32); 3410 3411 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1); 3412 3413 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, 3414 RHS, RHS, LHS); 3415 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, 3416 LHS, RHS, LHS); 3417 3418 // Denominator is scaled to not be denormal, so using rcp is ok. 3419 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, 3420 DenominatorScaled); 3421 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, 3422 DenominatorScaled); 3423 3424 const unsigned Denorm32Reg = AMDGPU::Hwreg::ID_MODE | 3425 (4 << AMDGPU::Hwreg::OFFSET_SHIFT_) | 3426 (1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_); 3427 3428 const SDValue BitField = DAG.getTargetConstant(Denorm32Reg, SL, MVT::i16); 3429 3430 if (!Subtarget->hasFP32Denormals()) { 3431 SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue); 3432 const SDValue EnableDenormValue = DAG.getConstant(FP_DENORM_FLUSH_NONE, 3433 SL, MVT::i32); 3434 SDValue EnableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, BindParamVTs, 3435 DAG.getEntryNode(), 3436 EnableDenormValue, BitField); 3437 SDValue Ops[3] = { 3438 NegDivScale0, 3439 EnableDenorm.getValue(0), 3440 EnableDenorm.getValue(1) 3441 }; 3442 3443 NegDivScale0 = DAG.getMergeValues(Ops, SL); 3444 } 3445 3446 SDValue Fma0 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, 3447 ApproxRcp, One, NegDivScale0); 3448 3449 SDValue Fma1 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, 3450 ApproxRcp, Fma0); 3451 3452 SDValue Mul = getFPBinOp(DAG, ISD::FMUL, SL, MVT::f32, NumeratorScaled, 3453 Fma1, Fma1); 3454 3455 SDValue Fma2 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, 3456 NumeratorScaled, Mul); 3457 3458 SDValue Fma3 = getFPTernOp(DAG, ISD::FMA,SL, MVT::f32, Fma2, Fma1, Mul, Fma2); 3459 3460 SDValue Fma4 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, 3461 NumeratorScaled, Fma3); 3462 3463 if (!Subtarget->hasFP32Denormals()) { 3464 const SDValue DisableDenormValue = 3465 DAG.getConstant(FP_DENORM_FLUSH_IN_FLUSH_OUT, SL, MVT::i32); 3466 SDValue DisableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, MVT::Other, 3467 Fma4.getValue(1), 3468 DisableDenormValue, 3469 BitField, 3470 Fma4.getValue(2)); 3471 3472 SDValue OutputChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other, 3473 DisableDenorm, DAG.getRoot()); 3474 DAG.setRoot(OutputChain); 3475 } 3476 3477 SDValue Scale = NumeratorScaled.getValue(1); 3478 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, 3479 Fma4, Fma1, Fma3, Scale); 3480 3481 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS); 3482 } 3483 3484 SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const { 3485 if (DAG.getTarget().Options.UnsafeFPMath) 3486 return lowerFastUnsafeFDIV(Op, DAG); 3487 3488 SDLoc SL(Op); 3489 SDValue X = Op.getOperand(0); 3490 SDValue Y = Op.getOperand(1); 3491 3492 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64); 3493 3494 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1); 3495 3496 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X); 3497 3498 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0); 3499 3500 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0); 3501 3502 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One); 3503 3504 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp); 3505 3506 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One); 3507 3508 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X); 3509 3510 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1); 3511 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3); 3512 3513 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64, 3514 NegDivScale0, Mul, DivScale1); 3515 3516 SDValue Scale; 3517 3518 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) { 3519 // Workaround a hardware bug on SI where the condition output from div_scale 3520 // is not usable. 3521 3522 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32); 3523 3524 // Figure out if the scale to use for div_fmas. 3525 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X); 3526 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y); 3527 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0); 3528 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1); 3529 3530 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi); 3531 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi); 3532 3533 SDValue Scale0Hi 3534 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi); 3535 SDValue Scale1Hi 3536 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi); 3537 3538 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ); 3539 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ); 3540 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen); 3541 } else { 3542 Scale = DivScale1.getValue(1); 3543 } 3544 3545 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64, 3546 Fma4, Fma3, Mul, Scale); 3547 3548 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X); 3549 } 3550 3551 SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const { 3552 EVT VT = Op.getValueType(); 3553 3554 if (VT == MVT::f32) 3555 return LowerFDIV32(Op, DAG); 3556 3557 if (VT == MVT::f64) 3558 return LowerFDIV64(Op, DAG); 3559 3560 if (VT == MVT::f16) 3561 return LowerFDIV16(Op, DAG); 3562 3563 llvm_unreachable("Unexpected type for fdiv"); 3564 } 3565 3566 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const { 3567 SDLoc DL(Op); 3568 StoreSDNode *Store = cast<StoreSDNode>(Op); 3569 EVT VT = Store->getMemoryVT(); 3570 3571 if (VT == MVT::i1) { 3572 return DAG.getTruncStore(Store->getChain(), DL, 3573 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32), 3574 Store->getBasePtr(), MVT::i1, Store->getMemOperand()); 3575 } 3576 3577 assert(VT.isVector() && 3578 Store->getValue().getValueType().getScalarType() == MVT::i32); 3579 3580 unsigned AS = Store->getAddressSpace(); 3581 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT, 3582 AS, Store->getAlignment())) { 3583 return expandUnalignedStore(Store, DAG); 3584 } 3585 3586 MachineFunction &MF = DAG.getMachineFunction(); 3587 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 3588 // If there is a possibilty that flat instruction access scratch memory 3589 // then we need to use the same legalization rules we use for private. 3590 if (AS == AMDGPUAS::FLAT_ADDRESS) 3591 AS = MFI->hasFlatScratchInit() ? 3592 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS; 3593 3594 unsigned NumElements = VT.getVectorNumElements(); 3595 switch (AS) { 3596 case AMDGPUAS::GLOBAL_ADDRESS: 3597 case AMDGPUAS::FLAT_ADDRESS: 3598 if (NumElements > 4) 3599 return SplitVectorStore(Op, DAG); 3600 return SDValue(); 3601 case AMDGPUAS::PRIVATE_ADDRESS: { 3602 switch (Subtarget->getMaxPrivateElementSize()) { 3603 case 4: 3604 return scalarizeVectorStore(Store, DAG); 3605 case 8: 3606 if (NumElements > 2) 3607 return SplitVectorStore(Op, DAG); 3608 return SDValue(); 3609 case 16: 3610 if (NumElements > 4) 3611 return SplitVectorStore(Op, DAG); 3612 return SDValue(); 3613 default: 3614 llvm_unreachable("unsupported private_element_size"); 3615 } 3616 } 3617 case AMDGPUAS::LOCAL_ADDRESS: { 3618 if (NumElements > 2) 3619 return SplitVectorStore(Op, DAG); 3620 3621 if (NumElements == 2) 3622 return Op; 3623 3624 // If properly aligned, if we split we might be able to use ds_write_b64. 3625 return SplitVectorStore(Op, DAG); 3626 } 3627 default: 3628 llvm_unreachable("unhandled address space"); 3629 } 3630 } 3631 3632 SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const { 3633 SDLoc DL(Op); 3634 EVT VT = Op.getValueType(); 3635 SDValue Arg = Op.getOperand(0); 3636 // TODO: Should this propagate fast-math-flags? 3637 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT, 3638 DAG.getNode(ISD::FMUL, DL, VT, Arg, 3639 DAG.getConstantFP(0.5/M_PI, DL, 3640 VT))); 3641 3642 switch (Op.getOpcode()) { 3643 case ISD::FCOS: 3644 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart); 3645 case ISD::FSIN: 3646 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart); 3647 default: 3648 llvm_unreachable("Wrong trig opcode"); 3649 } 3650 } 3651 3652 SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const { 3653 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op); 3654 assert(AtomicNode->isCompareAndSwap()); 3655 unsigned AS = AtomicNode->getAddressSpace(); 3656 3657 // No custom lowering required for local address space 3658 if (!isFlatGlobalAddrSpace(AS)) 3659 return Op; 3660 3661 // Non-local address space requires custom lowering for atomic compare 3662 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2 3663 SDLoc DL(Op); 3664 SDValue ChainIn = Op.getOperand(0); 3665 SDValue Addr = Op.getOperand(1); 3666 SDValue Old = Op.getOperand(2); 3667 SDValue New = Op.getOperand(3); 3668 EVT VT = Op.getValueType(); 3669 MVT SimpleVT = VT.getSimpleVT(); 3670 MVT VecType = MVT::getVectorVT(SimpleVT, 2); 3671 3672 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old}); 3673 SDValue Ops[] = { ChainIn, Addr, NewOld }; 3674 3675 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(), 3676 Ops, VT, AtomicNode->getMemOperand()); 3677 } 3678 3679 //===----------------------------------------------------------------------===// 3680 // Custom DAG optimizations 3681 //===----------------------------------------------------------------------===// 3682 3683 SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N, 3684 DAGCombinerInfo &DCI) const { 3685 EVT VT = N->getValueType(0); 3686 EVT ScalarVT = VT.getScalarType(); 3687 if (ScalarVT != MVT::f32) 3688 return SDValue(); 3689 3690 SelectionDAG &DAG = DCI.DAG; 3691 SDLoc DL(N); 3692 3693 SDValue Src = N->getOperand(0); 3694 EVT SrcVT = Src.getValueType(); 3695 3696 // TODO: We could try to match extracting the higher bytes, which would be 3697 // easier if i8 vectors weren't promoted to i32 vectors, particularly after 3698 // types are legalized. v4i8 -> v4f32 is probably the only case to worry 3699 // about in practice. 3700 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) { 3701 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) { 3702 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src); 3703 DCI.AddToWorklist(Cvt.getNode()); 3704 return Cvt; 3705 } 3706 } 3707 3708 return SDValue(); 3709 } 3710 3711 /// \brief Return true if the given offset Size in bytes can be folded into 3712 /// the immediate offsets of a memory instruction for the given address space. 3713 static bool canFoldOffset(unsigned OffsetSize, unsigned AS, 3714 const SISubtarget &STI) { 3715 switch (AS) { 3716 case AMDGPUAS::GLOBAL_ADDRESS: 3717 // MUBUF instructions a 12-bit offset in bytes. 3718 return isUInt<12>(OffsetSize); 3719 case AMDGPUAS::CONSTANT_ADDRESS: 3720 // SMRD instructions have an 8-bit offset in dwords on SI and 3721 // a 20-bit offset in bytes on VI. 3722 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 3723 return isUInt<20>(OffsetSize); 3724 else 3725 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4); 3726 case AMDGPUAS::LOCAL_ADDRESS: 3727 case AMDGPUAS::REGION_ADDRESS: 3728 // The single offset versions have a 16-bit offset in bytes. 3729 return isUInt<16>(OffsetSize); 3730 case AMDGPUAS::PRIVATE_ADDRESS: 3731 // Indirect register addressing does not use any offsets. 3732 default: 3733 return false; 3734 } 3735 } 3736 3737 // (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2) 3738 3739 // This is a variant of 3740 // (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2), 3741 // 3742 // The normal DAG combiner will do this, but only if the add has one use since 3743 // that would increase the number of instructions. 3744 // 3745 // This prevents us from seeing a constant offset that can be folded into a 3746 // memory instruction's addressing mode. If we know the resulting add offset of 3747 // a pointer can be folded into an addressing offset, we can replace the pointer 3748 // operand with the add of new constant offset. This eliminates one of the uses, 3749 // and may allow the remaining use to also be simplified. 3750 // 3751 SDValue SITargetLowering::performSHLPtrCombine(SDNode *N, 3752 unsigned AddrSpace, 3753 DAGCombinerInfo &DCI) const { 3754 SDValue N0 = N->getOperand(0); 3755 SDValue N1 = N->getOperand(1); 3756 3757 if (N0.getOpcode() != ISD::ADD) 3758 return SDValue(); 3759 3760 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1); 3761 if (!CN1) 3762 return SDValue(); 3763 3764 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 3765 if (!CAdd) 3766 return SDValue(); 3767 3768 // If the resulting offset is too large, we can't fold it into the addressing 3769 // mode offset. 3770 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue(); 3771 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget())) 3772 return SDValue(); 3773 3774 SelectionDAG &DAG = DCI.DAG; 3775 SDLoc SL(N); 3776 EVT VT = N->getValueType(0); 3777 3778 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1); 3779 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32); 3780 3781 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset); 3782 } 3783 3784 SDValue SITargetLowering::performMemSDNodeCombine(MemSDNode *N, 3785 DAGCombinerInfo &DCI) const { 3786 SDValue Ptr = N->getBasePtr(); 3787 SelectionDAG &DAG = DCI.DAG; 3788 SDLoc SL(N); 3789 3790 // TODO: We could also do this for multiplies. 3791 unsigned AS = N->getAddressSpace(); 3792 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) { 3793 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI); 3794 if (NewPtr) { 3795 SmallVector<SDValue, 8> NewOps(N->op_begin(), N->op_end()); 3796 3797 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr; 3798 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0); 3799 } 3800 } 3801 3802 return SDValue(); 3803 } 3804 3805 static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) { 3806 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) || 3807 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) || 3808 (Opc == ISD::XOR && Val == 0); 3809 } 3810 3811 // Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This 3812 // will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit 3813 // integer combine opportunities since most 64-bit operations are decomposed 3814 // this way. TODO: We won't want this for SALU especially if it is an inline 3815 // immediate. 3816 SDValue SITargetLowering::splitBinaryBitConstantOp( 3817 DAGCombinerInfo &DCI, 3818 const SDLoc &SL, 3819 unsigned Opc, SDValue LHS, 3820 const ConstantSDNode *CRHS) const { 3821 uint64_t Val = CRHS->getZExtValue(); 3822 uint32_t ValLo = Lo_32(Val); 3823 uint32_t ValHi = Hi_32(Val); 3824 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 3825 3826 if ((bitOpWithConstantIsReducible(Opc, ValLo) || 3827 bitOpWithConstantIsReducible(Opc, ValHi)) || 3828 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) { 3829 // If we need to materialize a 64-bit immediate, it will be split up later 3830 // anyway. Avoid creating the harder to understand 64-bit immediate 3831 // materialization. 3832 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi); 3833 } 3834 3835 return SDValue(); 3836 } 3837 3838 SDValue SITargetLowering::performAndCombine(SDNode *N, 3839 DAGCombinerInfo &DCI) const { 3840 if (DCI.isBeforeLegalize()) 3841 return SDValue(); 3842 3843 SelectionDAG &DAG = DCI.DAG; 3844 EVT VT = N->getValueType(0); 3845 SDValue LHS = N->getOperand(0); 3846 SDValue RHS = N->getOperand(1); 3847 3848 3849 if (VT == MVT::i64) { 3850 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); 3851 if (CRHS) { 3852 if (SDValue Split 3853 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS)) 3854 return Split; 3855 } 3856 } 3857 3858 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) -> 3859 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity) 3860 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) { 3861 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get(); 3862 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get(); 3863 3864 SDValue X = LHS.getOperand(0); 3865 SDValue Y = RHS.getOperand(0); 3866 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X) 3867 return SDValue(); 3868 3869 if (LCC == ISD::SETO) { 3870 if (X != LHS.getOperand(1)) 3871 return SDValue(); 3872 3873 if (RCC == ISD::SETUNE) { 3874 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1)); 3875 if (!C1 || !C1->isInfinity() || C1->isNegative()) 3876 return SDValue(); 3877 3878 const uint32_t Mask = SIInstrFlags::N_NORMAL | 3879 SIInstrFlags::N_SUBNORMAL | 3880 SIInstrFlags::N_ZERO | 3881 SIInstrFlags::P_ZERO | 3882 SIInstrFlags::P_SUBNORMAL | 3883 SIInstrFlags::P_NORMAL; 3884 3885 static_assert(((~(SIInstrFlags::S_NAN | 3886 SIInstrFlags::Q_NAN | 3887 SIInstrFlags::N_INFINITY | 3888 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask, 3889 "mask not equal"); 3890 3891 SDLoc DL(N); 3892 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 3893 X, DAG.getConstant(Mask, DL, MVT::i32)); 3894 } 3895 } 3896 } 3897 3898 return SDValue(); 3899 } 3900 3901 SDValue SITargetLowering::performOrCombine(SDNode *N, 3902 DAGCombinerInfo &DCI) const { 3903 SelectionDAG &DAG = DCI.DAG; 3904 SDValue LHS = N->getOperand(0); 3905 SDValue RHS = N->getOperand(1); 3906 3907 EVT VT = N->getValueType(0); 3908 if (VT == MVT::i1) { 3909 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2) 3910 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS && 3911 RHS.getOpcode() == AMDGPUISD::FP_CLASS) { 3912 SDValue Src = LHS.getOperand(0); 3913 if (Src != RHS.getOperand(0)) 3914 return SDValue(); 3915 3916 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1)); 3917 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1)); 3918 if (!CLHS || !CRHS) 3919 return SDValue(); 3920 3921 // Only 10 bits are used. 3922 static const uint32_t MaxMask = 0x3ff; 3923 3924 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask; 3925 SDLoc DL(N); 3926 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1, 3927 Src, DAG.getConstant(NewMask, DL, MVT::i32)); 3928 } 3929 3930 return SDValue(); 3931 } 3932 3933 if (VT != MVT::i64) 3934 return SDValue(); 3935 3936 // TODO: This could be a generic combine with a predicate for extracting the 3937 // high half of an integer being free. 3938 3939 // (or i64:x, (zero_extend i32:y)) -> 3940 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x))) 3941 if (LHS.getOpcode() == ISD::ZERO_EXTEND && 3942 RHS.getOpcode() != ISD::ZERO_EXTEND) 3943 std::swap(LHS, RHS); 3944 3945 if (RHS.getOpcode() == ISD::ZERO_EXTEND) { 3946 SDValue ExtSrc = RHS.getOperand(0); 3947 EVT SrcVT = ExtSrc.getValueType(); 3948 if (SrcVT == MVT::i32) { 3949 SDLoc SL(N); 3950 SDValue LowLHS, HiBits; 3951 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG); 3952 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc); 3953 3954 DCI.AddToWorklist(LowOr.getNode()); 3955 DCI.AddToWorklist(HiBits.getNode()); 3956 3957 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, 3958 LowOr, HiBits); 3959 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec); 3960 } 3961 } 3962 3963 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1)); 3964 if (CRHS) { 3965 if (SDValue Split 3966 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS)) 3967 return Split; 3968 } 3969 3970 return SDValue(); 3971 } 3972 3973 SDValue SITargetLowering::performXorCombine(SDNode *N, 3974 DAGCombinerInfo &DCI) const { 3975 EVT VT = N->getValueType(0); 3976 if (VT != MVT::i64) 3977 return SDValue(); 3978 3979 SDValue LHS = N->getOperand(0); 3980 SDValue RHS = N->getOperand(1); 3981 3982 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS); 3983 if (CRHS) { 3984 if (SDValue Split 3985 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS)) 3986 return Split; 3987 } 3988 3989 return SDValue(); 3990 } 3991 3992 SDValue SITargetLowering::performClassCombine(SDNode *N, 3993 DAGCombinerInfo &DCI) const { 3994 SelectionDAG &DAG = DCI.DAG; 3995 SDValue Mask = N->getOperand(1); 3996 3997 // fp_class x, 0 -> false 3998 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) { 3999 if (CMask->isNullValue()) 4000 return DAG.getConstant(0, SDLoc(N), MVT::i1); 4001 } 4002 4003 if (N->getOperand(0).isUndef()) 4004 return DAG.getUNDEF(MVT::i1); 4005 4006 return SDValue(); 4007 } 4008 4009 // Constant fold canonicalize. 4010 SDValue SITargetLowering::performFCanonicalizeCombine( 4011 SDNode *N, 4012 DAGCombinerInfo &DCI) const { 4013 ConstantFPSDNode *CFP = isConstOrConstSplatFP(N->getOperand(0)); 4014 if (!CFP) 4015 return SDValue(); 4016 4017 SelectionDAG &DAG = DCI.DAG; 4018 const APFloat &C = CFP->getValueAPF(); 4019 4020 // Flush denormals to 0 if not enabled. 4021 if (C.isDenormal()) { 4022 EVT VT = N->getValueType(0); 4023 EVT SVT = VT.getScalarType(); 4024 if (SVT == MVT::f32 && !Subtarget->hasFP32Denormals()) 4025 return DAG.getConstantFP(0.0, SDLoc(N), VT); 4026 4027 if (SVT == MVT::f64 && !Subtarget->hasFP64Denormals()) 4028 return DAG.getConstantFP(0.0, SDLoc(N), VT); 4029 4030 if (SVT == MVT::f16 && !Subtarget->hasFP16Denormals()) 4031 return DAG.getConstantFP(0.0, SDLoc(N), VT); 4032 } 4033 4034 if (C.isNaN()) { 4035 EVT VT = N->getValueType(0); 4036 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics()); 4037 if (C.isSignaling()) { 4038 // Quiet a signaling NaN. 4039 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT); 4040 } 4041 4042 // Make sure it is the canonical NaN bitpattern. 4043 // 4044 // TODO: Can we use -1 as the canonical NaN value since it's an inline 4045 // immediate? 4046 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt()) 4047 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT); 4048 } 4049 4050 return N->getOperand(0); 4051 } 4052 4053 static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) { 4054 switch (Opc) { 4055 case ISD::FMAXNUM: 4056 return AMDGPUISD::FMAX3; 4057 case ISD::SMAX: 4058 return AMDGPUISD::SMAX3; 4059 case ISD::UMAX: 4060 return AMDGPUISD::UMAX3; 4061 case ISD::FMINNUM: 4062 return AMDGPUISD::FMIN3; 4063 case ISD::SMIN: 4064 return AMDGPUISD::SMIN3; 4065 case ISD::UMIN: 4066 return AMDGPUISD::UMIN3; 4067 default: 4068 llvm_unreachable("Not a min/max opcode"); 4069 } 4070 } 4071 4072 SDValue SITargetLowering::performIntMed3ImmCombine( 4073 SelectionDAG &DAG, const SDLoc &SL, 4074 SDValue Op0, SDValue Op1, bool Signed) const { 4075 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1); 4076 if (!K1) 4077 return SDValue(); 4078 4079 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1)); 4080 if (!K0) 4081 return SDValue(); 4082 4083 if (Signed) { 4084 if (K0->getAPIntValue().sge(K1->getAPIntValue())) 4085 return SDValue(); 4086 } else { 4087 if (K0->getAPIntValue().uge(K1->getAPIntValue())) 4088 return SDValue(); 4089 } 4090 4091 EVT VT = K0->getValueType(0); 4092 unsigned Med3Opc = Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3; 4093 if (VT == MVT::i32 || (VT == MVT::i16 && Subtarget->hasMed3_16())) { 4094 return DAG.getNode(Med3Opc, SL, VT, 4095 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0)); 4096 } 4097 4098 // If there isn't a 16-bit med3 operation, convert to 32-bit. 4099 MVT NVT = MVT::i32; 4100 unsigned ExtOp = Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 4101 4102 SDValue Tmp1 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(0)); 4103 SDValue Tmp2 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(1)); 4104 SDValue Tmp3 = DAG.getNode(ExtOp, SL, NVT, Op1); 4105 4106 SDValue Med3 = DAG.getNode(Med3Opc, SL, NVT, Tmp1, Tmp2, Tmp3); 4107 return DAG.getNode(ISD::TRUNCATE, SL, VT, Med3); 4108 } 4109 4110 static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) { 4111 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions()) 4112 return true; 4113 4114 return DAG.isKnownNeverNaN(Op); 4115 } 4116 4117 SDValue SITargetLowering::performFPMed3ImmCombine(SelectionDAG &DAG, 4118 const SDLoc &SL, 4119 SDValue Op0, 4120 SDValue Op1) const { 4121 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1); 4122 if (!K1) 4123 return SDValue(); 4124 4125 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1)); 4126 if (!K0) 4127 return SDValue(); 4128 4129 // Ordered >= (although NaN inputs should have folded away by now). 4130 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF()); 4131 if (Cmp == APFloat::cmpGreaterThan) 4132 return SDValue(); 4133 4134 // TODO: Check IEEE bit enabled? 4135 EVT VT = K0->getValueType(0); 4136 if (Subtarget->enableDX10Clamp()) { 4137 // If dx10_clamp is enabled, NaNs clamp to 0.0. This is the same as the 4138 // hardware fmed3 behavior converting to a min. 4139 // FIXME: Should this be allowing -0.0? 4140 if (K1->isExactlyValue(1.0) && K0->isExactlyValue(0.0)) 4141 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Op0.getOperand(0)); 4142 } 4143 4144 // med3 for f16 is only available on gfx9+. 4145 if (VT == MVT::f64 || (VT == MVT::f16 && !Subtarget->hasMed3_16())) 4146 return SDValue(); 4147 4148 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a 4149 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then 4150 // give the other result, which is different from med3 with a NaN input. 4151 SDValue Var = Op0.getOperand(0); 4152 if (!isKnownNeverSNan(DAG, Var)) 4153 return SDValue(); 4154 4155 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0), 4156 Var, SDValue(K0, 0), SDValue(K1, 0)); 4157 } 4158 4159 SDValue SITargetLowering::performMinMaxCombine(SDNode *N, 4160 DAGCombinerInfo &DCI) const { 4161 SelectionDAG &DAG = DCI.DAG; 4162 4163 EVT VT = N->getValueType(0); 4164 unsigned Opc = N->getOpcode(); 4165 SDValue Op0 = N->getOperand(0); 4166 SDValue Op1 = N->getOperand(1); 4167 4168 // Only do this if the inner op has one use since this will just increases 4169 // register pressure for no benefit. 4170 4171 4172 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY && 4173 VT != MVT::f64) { 4174 // max(max(a, b), c) -> max3(a, b, c) 4175 // min(min(a, b), c) -> min3(a, b, c) 4176 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) { 4177 SDLoc DL(N); 4178 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 4179 DL, 4180 N->getValueType(0), 4181 Op0.getOperand(0), 4182 Op0.getOperand(1), 4183 Op1); 4184 } 4185 4186 // Try commuted. 4187 // max(a, max(b, c)) -> max3(a, b, c) 4188 // min(a, min(b, c)) -> min3(a, b, c) 4189 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) { 4190 SDLoc DL(N); 4191 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc), 4192 DL, 4193 N->getValueType(0), 4194 Op0, 4195 Op1.getOperand(0), 4196 Op1.getOperand(1)); 4197 } 4198 } 4199 4200 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1) 4201 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) { 4202 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true)) 4203 return Med3; 4204 } 4205 4206 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) { 4207 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false)) 4208 return Med3; 4209 } 4210 4211 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1) 4212 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) || 4213 (Opc == AMDGPUISD::FMIN_LEGACY && 4214 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) && 4215 (VT == MVT::f32 || VT == MVT::f64 || 4216 (VT == MVT::f16 && Subtarget->has16BitInsts())) && 4217 Op0.hasOneUse()) { 4218 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1)) 4219 return Res; 4220 } 4221 4222 return SDValue(); 4223 } 4224 4225 static bool isClampZeroToOne(SDValue A, SDValue B) { 4226 if (ConstantFPSDNode *CA = dyn_cast<ConstantFPSDNode>(A)) { 4227 if (ConstantFPSDNode *CB = dyn_cast<ConstantFPSDNode>(B)) { 4228 // FIXME: Should this be allowing -0.0? 4229 return (CA->isExactlyValue(0.0) && CB->isExactlyValue(1.0)) || 4230 (CA->isExactlyValue(1.0) && CB->isExactlyValue(0.0)); 4231 } 4232 } 4233 4234 return false; 4235 } 4236 4237 // FIXME: Should only worry about snans for version with chain. 4238 SDValue SITargetLowering::performFMed3Combine(SDNode *N, 4239 DAGCombinerInfo &DCI) const { 4240 EVT VT = N->getValueType(0); 4241 // v_med3_f32 and v_max_f32 behave identically wrt denorms, exceptions and 4242 // NaNs. With a NaN input, the order of the operands may change the result. 4243 4244 SelectionDAG &DAG = DCI.DAG; 4245 SDLoc SL(N); 4246 4247 SDValue Src0 = N->getOperand(0); 4248 SDValue Src1 = N->getOperand(1); 4249 SDValue Src2 = N->getOperand(2); 4250 4251 if (isClampZeroToOne(Src0, Src1)) { 4252 // const_a, const_b, x -> clamp is safe in all cases including signaling 4253 // nans. 4254 // FIXME: Should this be allowing -0.0? 4255 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src2); 4256 } 4257 4258 // FIXME: dx10_clamp behavior assumed in instcombine. Should we really bother 4259 // handling no dx10-clamp? 4260 if (Subtarget->enableDX10Clamp()) { 4261 // If NaNs is clamped to 0, we are free to reorder the inputs. 4262 4263 if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1)) 4264 std::swap(Src0, Src1); 4265 4266 if (isa<ConstantFPSDNode>(Src1) && !isa<ConstantFPSDNode>(Src2)) 4267 std::swap(Src1, Src2); 4268 4269 if (isa<ConstantFPSDNode>(Src0) && !isa<ConstantFPSDNode>(Src1)) 4270 std::swap(Src0, Src1); 4271 4272 if (isClampZeroToOne(Src1, Src2)) 4273 return DAG.getNode(AMDGPUISD::CLAMP, SL, VT, Src0); 4274 } 4275 4276 return SDValue(); 4277 } 4278 4279 SDValue SITargetLowering::performCvtPkRTZCombine(SDNode *N, 4280 DAGCombinerInfo &DCI) const { 4281 SDValue Src0 = N->getOperand(0); 4282 SDValue Src1 = N->getOperand(1); 4283 if (Src0.isUndef() && Src1.isUndef()) 4284 return DCI.DAG.getUNDEF(N->getValueType(0)); 4285 return SDValue(); 4286 } 4287 4288 unsigned SITargetLowering::getFusedOpcode(const SelectionDAG &DAG, 4289 const SDNode *N0, 4290 const SDNode *N1) const { 4291 EVT VT = N0->getValueType(0); 4292 4293 // Only do this if we are not trying to support denormals. v_mad_f32 does not 4294 // support denormals ever. 4295 if ((VT == MVT::f32 && !Subtarget->hasFP32Denormals()) || 4296 (VT == MVT::f16 && !Subtarget->hasFP16Denormals())) 4297 return ISD::FMAD; 4298 4299 const TargetOptions &Options = DAG.getTarget().Options; 4300 if ((Options.AllowFPOpFusion == FPOpFusion::Fast || 4301 Options.UnsafeFPMath || 4302 (cast<BinaryWithFlagsSDNode>(N0)->Flags.hasUnsafeAlgebra() && 4303 cast<BinaryWithFlagsSDNode>(N1)->Flags.hasUnsafeAlgebra())) && 4304 isFMAFasterThanFMulAndFAdd(VT)) { 4305 return ISD::FMA; 4306 } 4307 4308 return 0; 4309 } 4310 4311 SDValue SITargetLowering::performFAddCombine(SDNode *N, 4312 DAGCombinerInfo &DCI) const { 4313 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 4314 return SDValue(); 4315 4316 SelectionDAG &DAG = DCI.DAG; 4317 EVT VT = N->getValueType(0); 4318 4319 SDLoc SL(N); 4320 SDValue LHS = N->getOperand(0); 4321 SDValue RHS = N->getOperand(1); 4322 4323 // These should really be instruction patterns, but writing patterns with 4324 // source modiifiers is a pain. 4325 4326 // fadd (fadd (a, a), b) -> mad 2.0, a, b 4327 if (LHS.getOpcode() == ISD::FADD) { 4328 SDValue A = LHS.getOperand(0); 4329 if (A == LHS.getOperand(1)) { 4330 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode()); 4331 if (FusedOp != 0) { 4332 const SDValue Two = DAG.getConstantFP(2.0, SL, VT); 4333 return DAG.getNode(FusedOp, SL, VT, A, Two, RHS); 4334 } 4335 } 4336 } 4337 4338 // fadd (b, fadd (a, a)) -> mad 2.0, a, b 4339 if (RHS.getOpcode() == ISD::FADD) { 4340 SDValue A = RHS.getOperand(0); 4341 if (A == RHS.getOperand(1)) { 4342 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode()); 4343 if (FusedOp != 0) { 4344 const SDValue Two = DAG.getConstantFP(2.0, SL, VT); 4345 return DAG.getNode(FusedOp, SL, VT, A, Two, LHS); 4346 } 4347 } 4348 } 4349 4350 return SDValue(); 4351 } 4352 4353 SDValue SITargetLowering::performFSubCombine(SDNode *N, 4354 DAGCombinerInfo &DCI) const { 4355 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG) 4356 return SDValue(); 4357 4358 SelectionDAG &DAG = DCI.DAG; 4359 SDLoc SL(N); 4360 EVT VT = N->getValueType(0); 4361 assert(!VT.isVector()); 4362 4363 // Try to get the fneg to fold into the source modifier. This undoes generic 4364 // DAG combines and folds them into the mad. 4365 // 4366 // Only do this if we are not trying to support denormals. v_mad_f32 does 4367 // not support denormals ever. 4368 SDValue LHS = N->getOperand(0); 4369 SDValue RHS = N->getOperand(1); 4370 if (LHS.getOpcode() == ISD::FADD) { 4371 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c) 4372 SDValue A = LHS.getOperand(0); 4373 if (A == LHS.getOperand(1)) { 4374 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode()); 4375 if (FusedOp != 0){ 4376 const SDValue Two = DAG.getConstantFP(2.0, SL, VT); 4377 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS); 4378 4379 return DAG.getNode(FusedOp, SL, VT, A, Two, NegRHS); 4380 } 4381 } 4382 } 4383 4384 if (RHS.getOpcode() == ISD::FADD) { 4385 // (fsub c, (fadd a, a)) -> mad -2.0, a, c 4386 4387 SDValue A = RHS.getOperand(0); 4388 if (A == RHS.getOperand(1)) { 4389 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode()); 4390 if (FusedOp != 0){ 4391 const SDValue NegTwo = DAG.getConstantFP(-2.0, SL, VT); 4392 return DAG.getNode(FusedOp, SL, VT, A, NegTwo, LHS); 4393 } 4394 } 4395 } 4396 4397 return SDValue(); 4398 } 4399 4400 SDValue SITargetLowering::performSetCCCombine(SDNode *N, 4401 DAGCombinerInfo &DCI) const { 4402 SelectionDAG &DAG = DCI.DAG; 4403 SDLoc SL(N); 4404 4405 SDValue LHS = N->getOperand(0); 4406 SDValue RHS = N->getOperand(1); 4407 EVT VT = LHS.getValueType(); 4408 4409 if (VT != MVT::f32 && VT != MVT::f64 && (Subtarget->has16BitInsts() && 4410 VT != MVT::f16)) 4411 return SDValue(); 4412 4413 // Match isinf pattern 4414 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity)) 4415 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); 4416 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) { 4417 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS); 4418 if (!CRHS) 4419 return SDValue(); 4420 4421 const APFloat &APF = CRHS->getValueAPF(); 4422 if (APF.isInfinity() && !APF.isNegative()) { 4423 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY; 4424 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0), 4425 DAG.getConstant(Mask, SL, MVT::i32)); 4426 } 4427 } 4428 4429 return SDValue(); 4430 } 4431 4432 SDValue SITargetLowering::performCvtF32UByteNCombine(SDNode *N, 4433 DAGCombinerInfo &DCI) const { 4434 SelectionDAG &DAG = DCI.DAG; 4435 SDLoc SL(N); 4436 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0; 4437 4438 SDValue Src = N->getOperand(0); 4439 SDValue Srl = N->getOperand(0); 4440 if (Srl.getOpcode() == ISD::ZERO_EXTEND) 4441 Srl = Srl.getOperand(0); 4442 4443 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero. 4444 if (Srl.getOpcode() == ISD::SRL) { 4445 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x 4446 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x 4447 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x 4448 4449 if (const ConstantSDNode *C = 4450 dyn_cast<ConstantSDNode>(Srl.getOperand(1))) { 4451 Srl = DAG.getZExtOrTrunc(Srl.getOperand(0), SDLoc(Srl.getOperand(0)), 4452 EVT(MVT::i32)); 4453 4454 unsigned SrcOffset = C->getZExtValue() + 8 * Offset; 4455 if (SrcOffset < 32 && SrcOffset % 8 == 0) { 4456 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, SL, 4457 MVT::f32, Srl); 4458 } 4459 } 4460 } 4461 4462 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8); 4463 4464 APInt KnownZero, KnownOne; 4465 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), 4466 !DCI.isBeforeLegalizeOps()); 4467 const TargetLowering &TLI = DAG.getTargetLoweringInfo(); 4468 if (TLO.ShrinkDemandedConstant(Src, Demanded) || 4469 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) { 4470 DCI.CommitTargetLoweringOpt(TLO); 4471 } 4472 4473 return SDValue(); 4474 } 4475 4476 SDValue SITargetLowering::PerformDAGCombine(SDNode *N, 4477 DAGCombinerInfo &DCI) const { 4478 switch (N->getOpcode()) { 4479 default: 4480 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 4481 case ISD::FADD: 4482 return performFAddCombine(N, DCI); 4483 case ISD::FSUB: 4484 return performFSubCombine(N, DCI); 4485 case ISD::SETCC: 4486 return performSetCCCombine(N, DCI); 4487 case ISD::FMAXNUM: 4488 case ISD::FMINNUM: 4489 case ISD::SMAX: 4490 case ISD::SMIN: 4491 case ISD::UMAX: 4492 case ISD::UMIN: 4493 case AMDGPUISD::FMIN_LEGACY: 4494 case AMDGPUISD::FMAX_LEGACY: { 4495 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG && 4496 getTargetMachine().getOptLevel() > CodeGenOpt::None) 4497 return performMinMaxCombine(N, DCI); 4498 break; 4499 } 4500 case ISD::LOAD: 4501 case ISD::STORE: 4502 case ISD::ATOMIC_LOAD: 4503 case ISD::ATOMIC_STORE: 4504 case ISD::ATOMIC_CMP_SWAP: 4505 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: 4506 case ISD::ATOMIC_SWAP: 4507 case ISD::ATOMIC_LOAD_ADD: 4508 case ISD::ATOMIC_LOAD_SUB: 4509 case ISD::ATOMIC_LOAD_AND: 4510 case ISD::ATOMIC_LOAD_OR: 4511 case ISD::ATOMIC_LOAD_XOR: 4512 case ISD::ATOMIC_LOAD_NAND: 4513 case ISD::ATOMIC_LOAD_MIN: 4514 case ISD::ATOMIC_LOAD_MAX: 4515 case ISD::ATOMIC_LOAD_UMIN: 4516 case ISD::ATOMIC_LOAD_UMAX: 4517 case AMDGPUISD::ATOMIC_INC: 4518 case AMDGPUISD::ATOMIC_DEC: // TODO: Target mem intrinsics. 4519 if (DCI.isBeforeLegalize()) 4520 break; 4521 return performMemSDNodeCombine(cast<MemSDNode>(N), DCI); 4522 case ISD::AND: 4523 return performAndCombine(N, DCI); 4524 case ISD::OR: 4525 return performOrCombine(N, DCI); 4526 case ISD::XOR: 4527 return performXorCombine(N, DCI); 4528 case AMDGPUISD::FP_CLASS: 4529 return performClassCombine(N, DCI); 4530 case ISD::FCANONICALIZE: 4531 return performFCanonicalizeCombine(N, DCI); 4532 case AMDGPUISD::FRACT: 4533 case AMDGPUISD::RCP: 4534 case AMDGPUISD::RSQ: 4535 case AMDGPUISD::RCP_LEGACY: 4536 case AMDGPUISD::RSQ_LEGACY: 4537 case AMDGPUISD::RSQ_CLAMP: 4538 case AMDGPUISD::LDEXP: { 4539 SDValue Src = N->getOperand(0); 4540 if (Src.isUndef()) 4541 return Src; 4542 break; 4543 } 4544 case ISD::SINT_TO_FP: 4545 case ISD::UINT_TO_FP: 4546 return performUCharToFloatCombine(N, DCI); 4547 case AMDGPUISD::CVT_F32_UBYTE0: 4548 case AMDGPUISD::CVT_F32_UBYTE1: 4549 case AMDGPUISD::CVT_F32_UBYTE2: 4550 case AMDGPUISD::CVT_F32_UBYTE3: 4551 return performCvtF32UByteNCombine(N, DCI); 4552 case AMDGPUISD::FMED3: 4553 return performFMed3Combine(N, DCI); 4554 case AMDGPUISD::CVT_PKRTZ_F16_F32: 4555 return performCvtPkRTZCombine(N, DCI); 4556 case ISD::SCALAR_TO_VECTOR: { 4557 SelectionDAG &DAG = DCI.DAG; 4558 EVT VT = N->getValueType(0); 4559 4560 // v2i16 (scalar_to_vector i16:x) -> v2i16 (bitcast (any_extend i16:x)) 4561 if (VT == MVT::v2i16 || VT == MVT::v2f16) { 4562 SDLoc SL(N); 4563 SDValue Src = N->getOperand(0); 4564 EVT EltVT = Src.getValueType(); 4565 if (EltVT == MVT::f16) 4566 Src = DAG.getNode(ISD::BITCAST, SL, MVT::i16, Src); 4567 4568 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SL, MVT::i32, Src); 4569 return DAG.getNode(ISD::BITCAST, SL, VT, Ext); 4570 } 4571 4572 break; 4573 } 4574 } 4575 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI); 4576 } 4577 4578 /// \brief Helper function for adjustWritemask 4579 static unsigned SubIdx2Lane(unsigned Idx) { 4580 switch (Idx) { 4581 default: return 0; 4582 case AMDGPU::sub0: return 0; 4583 case AMDGPU::sub1: return 1; 4584 case AMDGPU::sub2: return 2; 4585 case AMDGPU::sub3: return 3; 4586 } 4587 } 4588 4589 /// \brief Adjust the writemask of MIMG instructions 4590 void SITargetLowering::adjustWritemask(MachineSDNode *&Node, 4591 SelectionDAG &DAG) const { 4592 SDNode *Users[4] = { }; 4593 unsigned Lane = 0; 4594 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3; 4595 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx); 4596 unsigned NewDmask = 0; 4597 4598 // Try to figure out the used register components 4599 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end(); 4600 I != E; ++I) { 4601 4602 // Don't look at users of the chain. 4603 if (I.getUse().getResNo() != 0) 4604 continue; 4605 4606 // Abort if we can't understand the usage 4607 if (!I->isMachineOpcode() || 4608 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG) 4609 return; 4610 4611 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used. 4612 // Note that subregs are packed, i.e. Lane==0 is the first bit set 4613 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit 4614 // set, etc. 4615 Lane = SubIdx2Lane(I->getConstantOperandVal(1)); 4616 4617 // Set which texture component corresponds to the lane. 4618 unsigned Comp; 4619 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) { 4620 assert(Dmask); 4621 Comp = countTrailingZeros(Dmask); 4622 Dmask &= ~(1 << Comp); 4623 } 4624 4625 // Abort if we have more than one user per component 4626 if (Users[Lane]) 4627 return; 4628 4629 Users[Lane] = *I; 4630 NewDmask |= 1 << Comp; 4631 } 4632 4633 // Abort if there's no change 4634 if (NewDmask == OldDmask) 4635 return; 4636 4637 // Adjust the writemask in the node 4638 std::vector<SDValue> Ops; 4639 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx); 4640 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32)); 4641 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end()); 4642 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops); 4643 4644 // If we only got one lane, replace it with a copy 4645 // (if NewDmask has only one bit set...) 4646 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) { 4647 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(), 4648 MVT::i32); 4649 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, 4650 SDLoc(), Users[Lane]->getValueType(0), 4651 SDValue(Node, 0), RC); 4652 DAG.ReplaceAllUsesWith(Users[Lane], Copy); 4653 return; 4654 } 4655 4656 // Update the users of the node with the new indices 4657 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) { 4658 SDNode *User = Users[i]; 4659 if (!User) 4660 continue; 4661 4662 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32); 4663 DAG.UpdateNodeOperands(User, User->getOperand(0), Op); 4664 4665 switch (Idx) { 4666 default: break; 4667 case AMDGPU::sub0: Idx = AMDGPU::sub1; break; 4668 case AMDGPU::sub1: Idx = AMDGPU::sub2; break; 4669 case AMDGPU::sub2: Idx = AMDGPU::sub3; break; 4670 } 4671 } 4672 } 4673 4674 static bool isFrameIndexOp(SDValue Op) { 4675 if (Op.getOpcode() == ISD::AssertZext) 4676 Op = Op.getOperand(0); 4677 4678 return isa<FrameIndexSDNode>(Op); 4679 } 4680 4681 /// \brief Legalize target independent instructions (e.g. INSERT_SUBREG) 4682 /// with frame index operands. 4683 /// LLVM assumes that inputs are to these instructions are registers. 4684 void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node, 4685 SelectionDAG &DAG) const { 4686 4687 SmallVector<SDValue, 8> Ops; 4688 for (unsigned i = 0; i < Node->getNumOperands(); ++i) { 4689 if (!isFrameIndexOp(Node->getOperand(i))) { 4690 Ops.push_back(Node->getOperand(i)); 4691 continue; 4692 } 4693 4694 SDLoc DL(Node); 4695 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, 4696 Node->getOperand(i).getValueType(), 4697 Node->getOperand(i)), 0)); 4698 } 4699 4700 DAG.UpdateNodeOperands(Node, Ops); 4701 } 4702 4703 /// \brief Fold the instructions after selecting them. 4704 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node, 4705 SelectionDAG &DAG) const { 4706 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 4707 unsigned Opcode = Node->getMachineOpcode(); 4708 4709 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() && 4710 !TII->isGather4(Opcode)) 4711 adjustWritemask(Node, DAG); 4712 4713 if (Opcode == AMDGPU::INSERT_SUBREG || 4714 Opcode == AMDGPU::REG_SEQUENCE) { 4715 legalizeTargetIndependentNode(Node, DAG); 4716 return Node; 4717 } 4718 return Node; 4719 } 4720 4721 /// \brief Assign the register class depending on the number of 4722 /// bits set in the writemask 4723 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI, 4724 SDNode *Node) const { 4725 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 4726 4727 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 4728 4729 if (TII->isVOP3(MI.getOpcode())) { 4730 // Make sure constant bus requirements are respected. 4731 TII->legalizeOperandsVOP3(MRI, MI); 4732 return; 4733 } 4734 4735 if (TII->isMIMG(MI)) { 4736 unsigned VReg = MI.getOperand(0).getReg(); 4737 const TargetRegisterClass *RC = MRI.getRegClass(VReg); 4738 // TODO: Need mapping tables to handle other cases (register classes). 4739 if (RC != &AMDGPU::VReg_128RegClass) 4740 return; 4741 4742 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4; 4743 unsigned Writemask = MI.getOperand(DmaskIdx).getImm(); 4744 unsigned BitsSet = 0; 4745 for (unsigned i = 0; i < 4; ++i) 4746 BitsSet += Writemask & (1 << i) ? 1 : 0; 4747 switch (BitsSet) { 4748 default: return; 4749 case 1: RC = &AMDGPU::VGPR_32RegClass; break; 4750 case 2: RC = &AMDGPU::VReg_64RegClass; break; 4751 case 3: RC = &AMDGPU::VReg_96RegClass; break; 4752 } 4753 4754 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet); 4755 MI.setDesc(TII->get(NewOpcode)); 4756 MRI.setRegClass(VReg, RC); 4757 return; 4758 } 4759 4760 // Replace unused atomics with the no return version. 4761 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode()); 4762 if (NoRetAtomicOp != -1) { 4763 if (!Node->hasAnyUseOfValue(0)) { 4764 MI.setDesc(TII->get(NoRetAtomicOp)); 4765 MI.RemoveOperand(0); 4766 return; 4767 } 4768 4769 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg 4770 // instruction, because the return type of these instructions is a vec2 of 4771 // the memory type, so it can be tied to the input operand. 4772 // This means these instructions always have a use, so we need to add a 4773 // special case to check if the atomic has only one extract_subreg use, 4774 // which itself has no uses. 4775 if ((Node->hasNUsesOfValue(1, 0) && 4776 Node->use_begin()->isMachineOpcode() && 4777 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG && 4778 !Node->use_begin()->hasAnyUseOfValue(0))) { 4779 unsigned Def = MI.getOperand(0).getReg(); 4780 4781 // Change this into a noret atomic. 4782 MI.setDesc(TII->get(NoRetAtomicOp)); 4783 MI.RemoveOperand(0); 4784 4785 // If we only remove the def operand from the atomic instruction, the 4786 // extract_subreg will be left with a use of a vreg without a def. 4787 // So we need to insert an implicit_def to avoid machine verifier 4788 // errors. 4789 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), 4790 TII->get(AMDGPU::IMPLICIT_DEF), Def); 4791 } 4792 return; 4793 } 4794 } 4795 4796 static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL, 4797 uint64_t Val) { 4798 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32); 4799 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0); 4800 } 4801 4802 MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG, 4803 const SDLoc &DL, 4804 SDValue Ptr) const { 4805 const SIInstrInfo *TII = getSubtarget()->getInstrInfo(); 4806 4807 // Build the half of the subregister with the constants before building the 4808 // full 128-bit register. If we are building multiple resource descriptors, 4809 // this will allow CSEing of the 2-component register. 4810 const SDValue Ops0[] = { 4811 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32), 4812 buildSMovImm32(DAG, DL, 0), 4813 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 4814 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32), 4815 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32) 4816 }; 4817 4818 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, 4819 MVT::v2i32, Ops0), 0); 4820 4821 // Combine the constants and the pointer. 4822 const SDValue Ops1[] = { 4823 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), 4824 Ptr, 4825 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32), 4826 SubRegHi, 4827 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32) 4828 }; 4829 4830 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1); 4831 } 4832 4833 /// \brief Return a resource descriptor with the 'Add TID' bit enabled 4834 /// The TID (Thread ID) is multiplied by the stride value (bits [61:48] 4835 /// of the resource descriptor) to create an offset, which is added to 4836 /// the resource pointer. 4837 MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL, 4838 SDValue Ptr, uint32_t RsrcDword1, 4839 uint64_t RsrcDword2And3) const { 4840 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr); 4841 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr); 4842 if (RsrcDword1) { 4843 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi, 4844 DAG.getConstant(RsrcDword1, DL, MVT::i32)), 4845 0); 4846 } 4847 4848 SDValue DataLo = buildSMovImm32(DAG, DL, 4849 RsrcDword2And3 & UINT64_C(0xFFFFFFFF)); 4850 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32); 4851 4852 const SDValue Ops[] = { 4853 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32), 4854 PtrLo, 4855 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 4856 PtrHi, 4857 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32), 4858 DataLo, 4859 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32), 4860 DataHi, 4861 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32) 4862 }; 4863 4864 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops); 4865 } 4866 4867 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG, 4868 const TargetRegisterClass *RC, 4869 unsigned Reg, EVT VT) const { 4870 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT); 4871 4872 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()), 4873 cast<RegisterSDNode>(VReg)->getReg(), VT); 4874 } 4875 4876 //===----------------------------------------------------------------------===// 4877 // SI Inline Assembly Support 4878 //===----------------------------------------------------------------------===// 4879 4880 std::pair<unsigned, const TargetRegisterClass *> 4881 SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 4882 StringRef Constraint, 4883 MVT VT) const { 4884 if (!isTypeLegal(VT)) 4885 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 4886 4887 if (Constraint.size() == 1) { 4888 switch (Constraint[0]) { 4889 case 's': 4890 case 'r': 4891 switch (VT.getSizeInBits()) { 4892 default: 4893 return std::make_pair(0U, nullptr); 4894 case 32: 4895 case 16: 4896 return std::make_pair(0U, &AMDGPU::SReg_32_XM0RegClass); 4897 case 64: 4898 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass); 4899 case 128: 4900 return std::make_pair(0U, &AMDGPU::SReg_128RegClass); 4901 case 256: 4902 return std::make_pair(0U, &AMDGPU::SReg_256RegClass); 4903 case 512: 4904 return std::make_pair(0U, &AMDGPU::SReg_512RegClass); 4905 } 4906 4907 case 'v': 4908 switch (VT.getSizeInBits()) { 4909 default: 4910 return std::make_pair(0U, nullptr); 4911 case 32: 4912 case 16: 4913 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass); 4914 case 64: 4915 return std::make_pair(0U, &AMDGPU::VReg_64RegClass); 4916 case 96: 4917 return std::make_pair(0U, &AMDGPU::VReg_96RegClass); 4918 case 128: 4919 return std::make_pair(0U, &AMDGPU::VReg_128RegClass); 4920 case 256: 4921 return std::make_pair(0U, &AMDGPU::VReg_256RegClass); 4922 case 512: 4923 return std::make_pair(0U, &AMDGPU::VReg_512RegClass); 4924 } 4925 } 4926 } 4927 4928 if (Constraint.size() > 1) { 4929 const TargetRegisterClass *RC = nullptr; 4930 if (Constraint[1] == 'v') { 4931 RC = &AMDGPU::VGPR_32RegClass; 4932 } else if (Constraint[1] == 's') { 4933 RC = &AMDGPU::SGPR_32RegClass; 4934 } 4935 4936 if (RC) { 4937 uint32_t Idx; 4938 bool Failed = Constraint.substr(2).getAsInteger(10, Idx); 4939 if (!Failed && Idx < RC->getNumRegs()) 4940 return std::make_pair(RC->getRegister(Idx), RC); 4941 } 4942 } 4943 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); 4944 } 4945 4946 SITargetLowering::ConstraintType 4947 SITargetLowering::getConstraintType(StringRef Constraint) const { 4948 if (Constraint.size() == 1) { 4949 switch (Constraint[0]) { 4950 default: break; 4951 case 's': 4952 case 'v': 4953 return C_RegisterClass; 4954 } 4955 } 4956 return TargetLowering::getConstraintType(Constraint); 4957 } 4958