1 //===- SIInstrInfo.cpp - SI Instruction Information ----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// SI Implementation of TargetInstrInfo. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "SIInstrInfo.h" 15 #include "AMDGPU.h" 16 #include "AMDGPUInstrInfo.h" 17 #include "GCNHazardRecognizer.h" 18 #include "GCNSubtarget.h" 19 #include "MCTargetDesc/AMDGPUMCTargetDesc.h" 20 #include "SIMachineFunctionInfo.h" 21 #include "llvm/Analysis/ValueTracking.h" 22 #include "llvm/CodeGen/LiveVariables.h" 23 #include "llvm/CodeGen/MachineDominators.h" 24 #include "llvm/CodeGen/RegisterScavenging.h" 25 #include "llvm/CodeGen/ScheduleDAG.h" 26 #include "llvm/IR/DiagnosticInfo.h" 27 #include "llvm/IR/IntrinsicsAMDGPU.h" 28 #include "llvm/Support/CommandLine.h" 29 #include "llvm/Target/TargetMachine.h" 30 31 using namespace llvm; 32 33 #define DEBUG_TYPE "si-instr-info" 34 35 #define GET_INSTRINFO_CTOR_DTOR 36 #include "AMDGPUGenInstrInfo.inc" 37 38 namespace llvm { 39 40 class AAResults; 41 42 namespace AMDGPU { 43 #define GET_D16ImageDimIntrinsics_IMPL 44 #define GET_ImageDimIntrinsicTable_IMPL 45 #define GET_RsrcIntrinsics_IMPL 46 #include "AMDGPUGenSearchableTables.inc" 47 } 48 } 49 50 51 // Must be at least 4 to be able to branch over minimum unconditional branch 52 // code. This is only for making it possible to write reasonably small tests for 53 // long branches. 54 static cl::opt<unsigned> 55 BranchOffsetBits("amdgpu-s-branch-bits", cl::ReallyHidden, cl::init(16), 56 cl::desc("Restrict range of branch instructions (DEBUG)")); 57 58 static cl::opt<bool> Fix16BitCopies( 59 "amdgpu-fix-16-bit-physreg-copies", 60 cl::desc("Fix copies between 32 and 16 bit registers by extending to 32 bit"), 61 cl::init(true), 62 cl::ReallyHidden); 63 64 SIInstrInfo::SIInstrInfo(const GCNSubtarget &ST) 65 : AMDGPUGenInstrInfo(AMDGPU::ADJCALLSTACKUP, AMDGPU::ADJCALLSTACKDOWN), 66 RI(ST), ST(ST) { 67 SchedModel.init(&ST); 68 } 69 70 //===----------------------------------------------------------------------===// 71 // TargetInstrInfo callbacks 72 //===----------------------------------------------------------------------===// 73 74 static unsigned getNumOperandsNoGlue(SDNode *Node) { 75 unsigned N = Node->getNumOperands(); 76 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue) 77 --N; 78 return N; 79 } 80 81 /// Returns true if both nodes have the same value for the given 82 /// operand \p Op, or if both nodes do not have this operand. 83 static bool nodesHaveSameOperandValue(SDNode *N0, SDNode* N1, unsigned OpName) { 84 unsigned Opc0 = N0->getMachineOpcode(); 85 unsigned Opc1 = N1->getMachineOpcode(); 86 87 int Op0Idx = AMDGPU::getNamedOperandIdx(Opc0, OpName); 88 int Op1Idx = AMDGPU::getNamedOperandIdx(Opc1, OpName); 89 90 if (Op0Idx == -1 && Op1Idx == -1) 91 return true; 92 93 94 if ((Op0Idx == -1 && Op1Idx != -1) || 95 (Op1Idx == -1 && Op0Idx != -1)) 96 return false; 97 98 // getNamedOperandIdx returns the index for the MachineInstr's operands, 99 // which includes the result as the first operand. We are indexing into the 100 // MachineSDNode's operands, so we need to skip the result operand to get 101 // the real index. 102 --Op0Idx; 103 --Op1Idx; 104 105 return N0->getOperand(Op0Idx) == N1->getOperand(Op1Idx); 106 } 107 108 bool SIInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI, 109 AAResults *AA) const { 110 // TODO: The generic check fails for VALU instructions that should be 111 // rematerializable due to implicit reads of exec. We really want all of the 112 // generic logic for this except for this. 113 switch (MI.getOpcode()) { 114 case AMDGPU::V_MOV_B32_e32: 115 case AMDGPU::V_MOV_B32_e64: 116 case AMDGPU::V_MOV_B64_PSEUDO: 117 case AMDGPU::V_ACCVGPR_READ_B32_e64: 118 case AMDGPU::V_ACCVGPR_WRITE_B32_e64: 119 // No implicit operands. 120 return MI.getNumOperands() == MI.getDesc().getNumOperands(); 121 default: 122 return false; 123 } 124 } 125 126 bool SIInstrInfo::areLoadsFromSameBasePtr(SDNode *Load0, SDNode *Load1, 127 int64_t &Offset0, 128 int64_t &Offset1) const { 129 if (!Load0->isMachineOpcode() || !Load1->isMachineOpcode()) 130 return false; 131 132 unsigned Opc0 = Load0->getMachineOpcode(); 133 unsigned Opc1 = Load1->getMachineOpcode(); 134 135 // Make sure both are actually loads. 136 if (!get(Opc0).mayLoad() || !get(Opc1).mayLoad()) 137 return false; 138 139 if (isDS(Opc0) && isDS(Opc1)) { 140 141 // FIXME: Handle this case: 142 if (getNumOperandsNoGlue(Load0) != getNumOperandsNoGlue(Load1)) 143 return false; 144 145 // Check base reg. 146 if (Load0->getOperand(0) != Load1->getOperand(0)) 147 return false; 148 149 // Skip read2 / write2 variants for simplicity. 150 // TODO: We should report true if the used offsets are adjacent (excluded 151 // st64 versions). 152 int Offset0Idx = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset); 153 int Offset1Idx = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset); 154 if (Offset0Idx == -1 || Offset1Idx == -1) 155 return false; 156 157 // XXX - be careful of datalesss loads 158 // getNamedOperandIdx returns the index for MachineInstrs. Since they 159 // include the output in the operand list, but SDNodes don't, we need to 160 // subtract the index by one. 161 Offset0Idx -= get(Opc0).NumDefs; 162 Offset1Idx -= get(Opc1).NumDefs; 163 Offset0 = cast<ConstantSDNode>(Load0->getOperand(Offset0Idx))->getZExtValue(); 164 Offset1 = cast<ConstantSDNode>(Load1->getOperand(Offset1Idx))->getZExtValue(); 165 return true; 166 } 167 168 if (isSMRD(Opc0) && isSMRD(Opc1)) { 169 // Skip time and cache invalidation instructions. 170 if (AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::sbase) == -1 || 171 AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::sbase) == -1) 172 return false; 173 174 assert(getNumOperandsNoGlue(Load0) == getNumOperandsNoGlue(Load1)); 175 176 // Check base reg. 177 if (Load0->getOperand(0) != Load1->getOperand(0)) 178 return false; 179 180 const ConstantSDNode *Load0Offset = 181 dyn_cast<ConstantSDNode>(Load0->getOperand(1)); 182 const ConstantSDNode *Load1Offset = 183 dyn_cast<ConstantSDNode>(Load1->getOperand(1)); 184 185 if (!Load0Offset || !Load1Offset) 186 return false; 187 188 Offset0 = Load0Offset->getZExtValue(); 189 Offset1 = Load1Offset->getZExtValue(); 190 return true; 191 } 192 193 // MUBUF and MTBUF can access the same addresses. 194 if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1))) { 195 196 // MUBUF and MTBUF have vaddr at different indices. 197 if (!nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::soffset) || 198 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::vaddr) || 199 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::srsrc)) 200 return false; 201 202 int OffIdx0 = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset); 203 int OffIdx1 = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset); 204 205 if (OffIdx0 == -1 || OffIdx1 == -1) 206 return false; 207 208 // getNamedOperandIdx returns the index for MachineInstrs. Since they 209 // include the output in the operand list, but SDNodes don't, we need to 210 // subtract the index by one. 211 OffIdx0 -= get(Opc0).NumDefs; 212 OffIdx1 -= get(Opc1).NumDefs; 213 214 SDValue Off0 = Load0->getOperand(OffIdx0); 215 SDValue Off1 = Load1->getOperand(OffIdx1); 216 217 // The offset might be a FrameIndexSDNode. 218 if (!isa<ConstantSDNode>(Off0) || !isa<ConstantSDNode>(Off1)) 219 return false; 220 221 Offset0 = cast<ConstantSDNode>(Off0)->getZExtValue(); 222 Offset1 = cast<ConstantSDNode>(Off1)->getZExtValue(); 223 return true; 224 } 225 226 return false; 227 } 228 229 static bool isStride64(unsigned Opc) { 230 switch (Opc) { 231 case AMDGPU::DS_READ2ST64_B32: 232 case AMDGPU::DS_READ2ST64_B64: 233 case AMDGPU::DS_WRITE2ST64_B32: 234 case AMDGPU::DS_WRITE2ST64_B64: 235 return true; 236 default: 237 return false; 238 } 239 } 240 241 bool SIInstrInfo::getMemOperandsWithOffsetWidth( 242 const MachineInstr &LdSt, SmallVectorImpl<const MachineOperand *> &BaseOps, 243 int64_t &Offset, bool &OffsetIsScalable, unsigned &Width, 244 const TargetRegisterInfo *TRI) const { 245 if (!LdSt.mayLoadOrStore()) 246 return false; 247 248 unsigned Opc = LdSt.getOpcode(); 249 OffsetIsScalable = false; 250 const MachineOperand *BaseOp, *OffsetOp; 251 int DataOpIdx; 252 253 if (isDS(LdSt)) { 254 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::addr); 255 OffsetOp = getNamedOperand(LdSt, AMDGPU::OpName::offset); 256 if (OffsetOp) { 257 // Normal, single offset LDS instruction. 258 if (!BaseOp) { 259 // DS_CONSUME/DS_APPEND use M0 for the base address. 260 // TODO: find the implicit use operand for M0 and use that as BaseOp? 261 return false; 262 } 263 BaseOps.push_back(BaseOp); 264 Offset = OffsetOp->getImm(); 265 // Get appropriate operand, and compute width accordingly. 266 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 267 if (DataOpIdx == -1) 268 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0); 269 Width = getOpSize(LdSt, DataOpIdx); 270 } else { 271 // The 2 offset instructions use offset0 and offset1 instead. We can treat 272 // these as a load with a single offset if the 2 offsets are consecutive. 273 // We will use this for some partially aligned loads. 274 const MachineOperand *Offset0Op = 275 getNamedOperand(LdSt, AMDGPU::OpName::offset0); 276 const MachineOperand *Offset1Op = 277 getNamedOperand(LdSt, AMDGPU::OpName::offset1); 278 279 unsigned Offset0 = Offset0Op->getImm(); 280 unsigned Offset1 = Offset1Op->getImm(); 281 if (Offset0 + 1 != Offset1) 282 return false; 283 284 // Each of these offsets is in element sized units, so we need to convert 285 // to bytes of the individual reads. 286 287 unsigned EltSize; 288 if (LdSt.mayLoad()) 289 EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, 0)) / 16; 290 else { 291 assert(LdSt.mayStore()); 292 int Data0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0); 293 EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, Data0Idx)) / 8; 294 } 295 296 if (isStride64(Opc)) 297 EltSize *= 64; 298 299 BaseOps.push_back(BaseOp); 300 Offset = EltSize * Offset0; 301 // Get appropriate operand(s), and compute width accordingly. 302 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 303 if (DataOpIdx == -1) { 304 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0); 305 Width = getOpSize(LdSt, DataOpIdx); 306 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data1); 307 Width += getOpSize(LdSt, DataOpIdx); 308 } else { 309 Width = getOpSize(LdSt, DataOpIdx); 310 } 311 } 312 return true; 313 } 314 315 if (isMUBUF(LdSt) || isMTBUF(LdSt)) { 316 const MachineOperand *SOffset = getNamedOperand(LdSt, AMDGPU::OpName::soffset); 317 if (SOffset && SOffset->isReg()) { 318 // We can only handle this if it's a stack access, as any other resource 319 // would require reporting multiple base registers. 320 const MachineOperand *AddrReg = getNamedOperand(LdSt, AMDGPU::OpName::vaddr); 321 if (AddrReg && !AddrReg->isFI()) 322 return false; 323 324 const MachineOperand *RSrc = getNamedOperand(LdSt, AMDGPU::OpName::srsrc); 325 const SIMachineFunctionInfo *MFI 326 = LdSt.getParent()->getParent()->getInfo<SIMachineFunctionInfo>(); 327 if (RSrc->getReg() != MFI->getScratchRSrcReg()) 328 return false; 329 330 const MachineOperand *OffsetImm = 331 getNamedOperand(LdSt, AMDGPU::OpName::offset); 332 BaseOps.push_back(RSrc); 333 BaseOps.push_back(SOffset); 334 Offset = OffsetImm->getImm(); 335 } else { 336 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::srsrc); 337 if (!BaseOp) // e.g. BUFFER_WBINVL1_VOL 338 return false; 339 BaseOps.push_back(BaseOp); 340 341 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::vaddr); 342 if (BaseOp) 343 BaseOps.push_back(BaseOp); 344 345 const MachineOperand *OffsetImm = 346 getNamedOperand(LdSt, AMDGPU::OpName::offset); 347 Offset = OffsetImm->getImm(); 348 if (SOffset) // soffset can be an inline immediate. 349 Offset += SOffset->getImm(); 350 } 351 // Get appropriate operand, and compute width accordingly. 352 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 353 if (DataOpIdx == -1) 354 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata); 355 Width = getOpSize(LdSt, DataOpIdx); 356 return true; 357 } 358 359 if (isMIMG(LdSt)) { 360 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc); 361 BaseOps.push_back(&LdSt.getOperand(SRsrcIdx)); 362 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0); 363 if (VAddr0Idx >= 0) { 364 // GFX10 possible NSA encoding. 365 for (int I = VAddr0Idx; I < SRsrcIdx; ++I) 366 BaseOps.push_back(&LdSt.getOperand(I)); 367 } else { 368 BaseOps.push_back(getNamedOperand(LdSt, AMDGPU::OpName::vaddr)); 369 } 370 Offset = 0; 371 // Get appropriate operand, and compute width accordingly. 372 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata); 373 Width = getOpSize(LdSt, DataOpIdx); 374 return true; 375 } 376 377 if (isSMRD(LdSt)) { 378 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::sbase); 379 if (!BaseOp) // e.g. S_MEMTIME 380 return false; 381 BaseOps.push_back(BaseOp); 382 OffsetOp = getNamedOperand(LdSt, AMDGPU::OpName::offset); 383 Offset = OffsetOp ? OffsetOp->getImm() : 0; 384 // Get appropriate operand, and compute width accordingly. 385 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::sdst); 386 Width = getOpSize(LdSt, DataOpIdx); 387 return true; 388 } 389 390 if (isFLAT(LdSt)) { 391 // Instructions have either vaddr or saddr or both or none. 392 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::vaddr); 393 if (BaseOp) 394 BaseOps.push_back(BaseOp); 395 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::saddr); 396 if (BaseOp) 397 BaseOps.push_back(BaseOp); 398 Offset = getNamedOperand(LdSt, AMDGPU::OpName::offset)->getImm(); 399 // Get appropriate operand, and compute width accordingly. 400 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 401 if (DataOpIdx == -1) 402 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata); 403 Width = getOpSize(LdSt, DataOpIdx); 404 return true; 405 } 406 407 return false; 408 } 409 410 static bool memOpsHaveSameBasePtr(const MachineInstr &MI1, 411 ArrayRef<const MachineOperand *> BaseOps1, 412 const MachineInstr &MI2, 413 ArrayRef<const MachineOperand *> BaseOps2) { 414 // Only examine the first "base" operand of each instruction, on the 415 // assumption that it represents the real base address of the memory access. 416 // Other operands are typically offsets or indices from this base address. 417 if (BaseOps1.front()->isIdenticalTo(*BaseOps2.front())) 418 return true; 419 420 if (!MI1.hasOneMemOperand() || !MI2.hasOneMemOperand()) 421 return false; 422 423 auto MO1 = *MI1.memoperands_begin(); 424 auto MO2 = *MI2.memoperands_begin(); 425 if (MO1->getAddrSpace() != MO2->getAddrSpace()) 426 return false; 427 428 auto Base1 = MO1->getValue(); 429 auto Base2 = MO2->getValue(); 430 if (!Base1 || !Base2) 431 return false; 432 Base1 = getUnderlyingObject(Base1); 433 Base2 = getUnderlyingObject(Base2); 434 435 if (isa<UndefValue>(Base1) || isa<UndefValue>(Base2)) 436 return false; 437 438 return Base1 == Base2; 439 } 440 441 bool SIInstrInfo::shouldClusterMemOps(ArrayRef<const MachineOperand *> BaseOps1, 442 ArrayRef<const MachineOperand *> BaseOps2, 443 unsigned NumLoads, 444 unsigned NumBytes) const { 445 // If the mem ops (to be clustered) do not have the same base ptr, then they 446 // should not be clustered 447 if (!BaseOps1.empty() && !BaseOps2.empty()) { 448 const MachineInstr &FirstLdSt = *BaseOps1.front()->getParent(); 449 const MachineInstr &SecondLdSt = *BaseOps2.front()->getParent(); 450 if (!memOpsHaveSameBasePtr(FirstLdSt, BaseOps1, SecondLdSt, BaseOps2)) 451 return false; 452 } else if (!BaseOps1.empty() || !BaseOps2.empty()) { 453 // If only one base op is empty, they do not have the same base ptr 454 return false; 455 } 456 457 // In order to avoid regester pressure, on an average, the number of DWORDS 458 // loaded together by all clustered mem ops should not exceed 8. This is an 459 // empirical value based on certain observations and performance related 460 // experiments. 461 // The good thing about this heuristic is - it avoids clustering of too many 462 // sub-word loads, and also avoids clustering of wide loads. Below is the 463 // brief summary of how the heuristic behaves for various `LoadSize`. 464 // (1) 1 <= LoadSize <= 4: cluster at max 8 mem ops 465 // (2) 5 <= LoadSize <= 8: cluster at max 4 mem ops 466 // (3) 9 <= LoadSize <= 12: cluster at max 2 mem ops 467 // (4) 13 <= LoadSize <= 16: cluster at max 2 mem ops 468 // (5) LoadSize >= 17: do not cluster 469 const unsigned LoadSize = NumBytes / NumLoads; 470 const unsigned NumDWORDs = ((LoadSize + 3) / 4) * NumLoads; 471 return NumDWORDs <= 8; 472 } 473 474 // FIXME: This behaves strangely. If, for example, you have 32 load + stores, 475 // the first 16 loads will be interleaved with the stores, and the next 16 will 476 // be clustered as expected. It should really split into 2 16 store batches. 477 // 478 // Loads are clustered until this returns false, rather than trying to schedule 479 // groups of stores. This also means we have to deal with saying different 480 // address space loads should be clustered, and ones which might cause bank 481 // conflicts. 482 // 483 // This might be deprecated so it might not be worth that much effort to fix. 484 bool SIInstrInfo::shouldScheduleLoadsNear(SDNode *Load0, SDNode *Load1, 485 int64_t Offset0, int64_t Offset1, 486 unsigned NumLoads) const { 487 assert(Offset1 > Offset0 && 488 "Second offset should be larger than first offset!"); 489 // If we have less than 16 loads in a row, and the offsets are within 64 490 // bytes, then schedule together. 491 492 // A cacheline is 64 bytes (for global memory). 493 return (NumLoads <= 16 && (Offset1 - Offset0) < 64); 494 } 495 496 static void reportIllegalCopy(const SIInstrInfo *TII, MachineBasicBlock &MBB, 497 MachineBasicBlock::iterator MI, 498 const DebugLoc &DL, MCRegister DestReg, 499 MCRegister SrcReg, bool KillSrc, 500 const char *Msg = "illegal SGPR to VGPR copy") { 501 MachineFunction *MF = MBB.getParent(); 502 DiagnosticInfoUnsupported IllegalCopy(MF->getFunction(), Msg, DL, DS_Error); 503 LLVMContext &C = MF->getFunction().getContext(); 504 C.diagnose(IllegalCopy); 505 506 BuildMI(MBB, MI, DL, TII->get(AMDGPU::SI_ILLEGAL_COPY), DestReg) 507 .addReg(SrcReg, getKillRegState(KillSrc)); 508 } 509 510 /// Handle copying from SGPR to AGPR, or from AGPR to AGPR. It is not possible 511 /// to directly copy, so an intermediate VGPR needs to be used. 512 static void indirectCopyToAGPR(const SIInstrInfo &TII, 513 MachineBasicBlock &MBB, 514 MachineBasicBlock::iterator MI, 515 const DebugLoc &DL, MCRegister DestReg, 516 MCRegister SrcReg, bool KillSrc, 517 RegScavenger &RS, 518 Register ImpDefSuperReg = Register(), 519 Register ImpUseSuperReg = Register()) { 520 const SIRegisterInfo &RI = TII.getRegisterInfo(); 521 522 assert(AMDGPU::SReg_32RegClass.contains(SrcReg) || 523 AMDGPU::AGPR_32RegClass.contains(SrcReg)); 524 525 // First try to find defining accvgpr_write to avoid temporary registers. 526 for (auto Def = MI, E = MBB.begin(); Def != E; ) { 527 --Def; 528 if (!Def->definesRegister(SrcReg, &RI)) 529 continue; 530 if (Def->getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32_e64) 531 break; 532 533 MachineOperand &DefOp = Def->getOperand(1); 534 assert(DefOp.isReg() || DefOp.isImm()); 535 536 if (DefOp.isReg()) { 537 // Check that register source operand if not clobbered before MI. 538 // Immediate operands are always safe to propagate. 539 bool SafeToPropagate = true; 540 for (auto I = Def; I != MI && SafeToPropagate; ++I) 541 if (I->modifiesRegister(DefOp.getReg(), &RI)) 542 SafeToPropagate = false; 543 544 if (!SafeToPropagate) 545 break; 546 547 DefOp.setIsKill(false); 548 } 549 550 MachineInstrBuilder Builder = 551 BuildMI(MBB, MI, DL, TII.get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg) 552 .add(DefOp); 553 if (ImpDefSuperReg) 554 Builder.addReg(ImpDefSuperReg, RegState::Define | RegState::Implicit); 555 556 if (ImpUseSuperReg) { 557 Builder.addReg(ImpUseSuperReg, 558 getKillRegState(KillSrc) | RegState::Implicit); 559 } 560 561 return; 562 } 563 564 RS.enterBasicBlock(MBB); 565 RS.forward(MI); 566 567 // Ideally we want to have three registers for a long reg_sequence copy 568 // to hide 2 waitstates between v_mov_b32 and accvgpr_write. 569 unsigned MaxVGPRs = RI.getRegPressureLimit(&AMDGPU::VGPR_32RegClass, 570 *MBB.getParent()); 571 572 // Registers in the sequence are allocated contiguously so we can just 573 // use register number to pick one of three round-robin temps. 574 unsigned RegNo = DestReg % 3; 575 Register Tmp = RS.scavengeRegister(&AMDGPU::VGPR_32RegClass, 0); 576 if (!Tmp) 577 report_fatal_error("Cannot scavenge VGPR to copy to AGPR"); 578 RS.setRegUsed(Tmp); 579 580 if (!TII.getSubtarget().hasGFX90AInsts()) { 581 // Only loop through if there are any free registers left, otherwise 582 // scavenger may report a fatal error without emergency spill slot 583 // or spill with the slot. 584 while (RegNo-- && RS.FindUnusedReg(&AMDGPU::VGPR_32RegClass)) { 585 Register Tmp2 = RS.scavengeRegister(&AMDGPU::VGPR_32RegClass, 0); 586 if (!Tmp2 || RI.getHWRegIndex(Tmp2) >= MaxVGPRs) 587 break; 588 Tmp = Tmp2; 589 RS.setRegUsed(Tmp); 590 } 591 } 592 593 // Insert copy to temporary VGPR. 594 unsigned TmpCopyOp = AMDGPU::V_MOV_B32_e32; 595 if (AMDGPU::AGPR_32RegClass.contains(SrcReg)) { 596 TmpCopyOp = AMDGPU::V_ACCVGPR_READ_B32_e64; 597 } else { 598 assert(AMDGPU::SReg_32RegClass.contains(SrcReg)); 599 } 600 601 MachineInstrBuilder UseBuilder = BuildMI(MBB, MI, DL, TII.get(TmpCopyOp), Tmp) 602 .addReg(SrcReg, getKillRegState(KillSrc)); 603 if (ImpUseSuperReg) { 604 UseBuilder.addReg(ImpUseSuperReg, 605 getKillRegState(KillSrc) | RegState::Implicit); 606 } 607 608 MachineInstrBuilder DefBuilder 609 = BuildMI(MBB, MI, DL, TII.get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg) 610 .addReg(Tmp, RegState::Kill); 611 612 if (ImpDefSuperReg) 613 DefBuilder.addReg(ImpDefSuperReg, RegState::Define | RegState::Implicit); 614 } 615 616 static void expandSGPRCopy(const SIInstrInfo &TII, MachineBasicBlock &MBB, 617 MachineBasicBlock::iterator MI, const DebugLoc &DL, 618 MCRegister DestReg, MCRegister SrcReg, bool KillSrc, 619 const TargetRegisterClass *RC, bool Forward) { 620 const SIRegisterInfo &RI = TII.getRegisterInfo(); 621 ArrayRef<int16_t> BaseIndices = RI.getRegSplitParts(RC, 4); 622 MachineBasicBlock::iterator I = MI; 623 MachineInstr *FirstMI = nullptr, *LastMI = nullptr; 624 625 for (unsigned Idx = 0; Idx < BaseIndices.size(); ++Idx) { 626 int16_t SubIdx = BaseIndices[Idx]; 627 Register Reg = RI.getSubReg(DestReg, SubIdx); 628 unsigned Opcode = AMDGPU::S_MOV_B32; 629 630 // Is SGPR aligned? If so try to combine with next. 631 Register Src = RI.getSubReg(SrcReg, SubIdx); 632 bool AlignedDest = ((Reg - AMDGPU::SGPR0) % 2) == 0; 633 bool AlignedSrc = ((Src - AMDGPU::SGPR0) % 2) == 0; 634 if (AlignedDest && AlignedSrc && (Idx + 1 < BaseIndices.size())) { 635 // Can use SGPR64 copy 636 unsigned Channel = RI.getChannelFromSubReg(SubIdx); 637 SubIdx = RI.getSubRegFromChannel(Channel, 2); 638 Opcode = AMDGPU::S_MOV_B64; 639 Idx++; 640 } 641 642 LastMI = BuildMI(MBB, I, DL, TII.get(Opcode), RI.getSubReg(DestReg, SubIdx)) 643 .addReg(RI.getSubReg(SrcReg, SubIdx)) 644 .addReg(SrcReg, RegState::Implicit); 645 646 if (!FirstMI) 647 FirstMI = LastMI; 648 649 if (!Forward) 650 I--; 651 } 652 653 assert(FirstMI && LastMI); 654 if (!Forward) 655 std::swap(FirstMI, LastMI); 656 657 FirstMI->addOperand( 658 MachineOperand::CreateReg(DestReg, true /*IsDef*/, true /*IsImp*/)); 659 660 if (KillSrc) 661 LastMI->addRegisterKilled(SrcReg, &RI); 662 } 663 664 void SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 665 MachineBasicBlock::iterator MI, 666 const DebugLoc &DL, MCRegister DestReg, 667 MCRegister SrcReg, bool KillSrc) const { 668 const TargetRegisterClass *RC = RI.getPhysRegClass(DestReg); 669 670 // FIXME: This is hack to resolve copies between 16 bit and 32 bit 671 // registers until all patterns are fixed. 672 if (Fix16BitCopies && 673 ((RI.getRegSizeInBits(*RC) == 16) ^ 674 (RI.getRegSizeInBits(*RI.getPhysRegClass(SrcReg)) == 16))) { 675 MCRegister &RegToFix = (RI.getRegSizeInBits(*RC) == 16) ? DestReg : SrcReg; 676 MCRegister Super = RI.get32BitRegister(RegToFix); 677 assert(RI.getSubReg(Super, AMDGPU::lo16) == RegToFix); 678 RegToFix = Super; 679 680 if (DestReg == SrcReg) { 681 // Insert empty bundle since ExpandPostRA expects an instruction here. 682 BuildMI(MBB, MI, DL, get(AMDGPU::BUNDLE)); 683 return; 684 } 685 686 RC = RI.getPhysRegClass(DestReg); 687 } 688 689 if (RC == &AMDGPU::VGPR_32RegClass) { 690 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) || 691 AMDGPU::SReg_32RegClass.contains(SrcReg) || 692 AMDGPU::AGPR_32RegClass.contains(SrcReg)); 693 unsigned Opc = AMDGPU::AGPR_32RegClass.contains(SrcReg) ? 694 AMDGPU::V_ACCVGPR_READ_B32_e64 : AMDGPU::V_MOV_B32_e32; 695 BuildMI(MBB, MI, DL, get(Opc), DestReg) 696 .addReg(SrcReg, getKillRegState(KillSrc)); 697 return; 698 } 699 700 if (RC == &AMDGPU::SReg_32_XM0RegClass || 701 RC == &AMDGPU::SReg_32RegClass) { 702 if (SrcReg == AMDGPU::SCC) { 703 BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B32), DestReg) 704 .addImm(1) 705 .addImm(0); 706 return; 707 } 708 709 if (DestReg == AMDGPU::VCC_LO) { 710 if (AMDGPU::SReg_32RegClass.contains(SrcReg)) { 711 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), AMDGPU::VCC_LO) 712 .addReg(SrcReg, getKillRegState(KillSrc)); 713 } else { 714 // FIXME: Hack until VReg_1 removed. 715 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg)); 716 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32)) 717 .addImm(0) 718 .addReg(SrcReg, getKillRegState(KillSrc)); 719 } 720 721 return; 722 } 723 724 if (!AMDGPU::SReg_32RegClass.contains(SrcReg)) { 725 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 726 return; 727 } 728 729 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg) 730 .addReg(SrcReg, getKillRegState(KillSrc)); 731 return; 732 } 733 734 if (RC == &AMDGPU::SReg_64RegClass) { 735 if (SrcReg == AMDGPU::SCC) { 736 BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B64), DestReg) 737 .addImm(1) 738 .addImm(0); 739 return; 740 } 741 742 if (DestReg == AMDGPU::VCC) { 743 if (AMDGPU::SReg_64RegClass.contains(SrcReg)) { 744 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), AMDGPU::VCC) 745 .addReg(SrcReg, getKillRegState(KillSrc)); 746 } else { 747 // FIXME: Hack until VReg_1 removed. 748 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg)); 749 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32)) 750 .addImm(0) 751 .addReg(SrcReg, getKillRegState(KillSrc)); 752 } 753 754 return; 755 } 756 757 if (!AMDGPU::SReg_64RegClass.contains(SrcReg)) { 758 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 759 return; 760 } 761 762 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg) 763 .addReg(SrcReg, getKillRegState(KillSrc)); 764 return; 765 } 766 767 if (DestReg == AMDGPU::SCC) { 768 // Copying 64-bit or 32-bit sources to SCC barely makes sense, 769 // but SelectionDAG emits such copies for i1 sources. 770 if (AMDGPU::SReg_64RegClass.contains(SrcReg)) { 771 // This copy can only be produced by patterns 772 // with explicit SCC, which are known to be enabled 773 // only for subtargets with S_CMP_LG_U64 present. 774 assert(ST.hasScalarCompareEq64()); 775 BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U64)) 776 .addReg(SrcReg, getKillRegState(KillSrc)) 777 .addImm(0); 778 } else { 779 assert(AMDGPU::SReg_32RegClass.contains(SrcReg)); 780 BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U32)) 781 .addReg(SrcReg, getKillRegState(KillSrc)) 782 .addImm(0); 783 } 784 785 return; 786 } 787 788 if (RC == &AMDGPU::AGPR_32RegClass) { 789 if (AMDGPU::VGPR_32RegClass.contains(SrcReg)) { 790 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg) 791 .addReg(SrcReg, getKillRegState(KillSrc)); 792 return; 793 } 794 795 if (AMDGPU::AGPR_32RegClass.contains(SrcReg) && ST.hasGFX90AInsts()) { 796 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_MOV_B32), DestReg) 797 .addReg(SrcReg, getKillRegState(KillSrc)); 798 return; 799 } 800 801 // FIXME: Pass should maintain scavenger to avoid scan through the block on 802 // every AGPR spill. 803 RegScavenger RS; 804 indirectCopyToAGPR(*this, MBB, MI, DL, DestReg, SrcReg, KillSrc, RS); 805 return; 806 } 807 808 const unsigned Size = RI.getRegSizeInBits(*RC); 809 if (Size == 16) { 810 assert(AMDGPU::VGPR_LO16RegClass.contains(SrcReg) || 811 AMDGPU::VGPR_HI16RegClass.contains(SrcReg) || 812 AMDGPU::SReg_LO16RegClass.contains(SrcReg) || 813 AMDGPU::AGPR_LO16RegClass.contains(SrcReg)); 814 815 bool IsSGPRDst = AMDGPU::SReg_LO16RegClass.contains(DestReg); 816 bool IsSGPRSrc = AMDGPU::SReg_LO16RegClass.contains(SrcReg); 817 bool IsAGPRDst = AMDGPU::AGPR_LO16RegClass.contains(DestReg); 818 bool IsAGPRSrc = AMDGPU::AGPR_LO16RegClass.contains(SrcReg); 819 bool DstLow = AMDGPU::VGPR_LO16RegClass.contains(DestReg) || 820 AMDGPU::SReg_LO16RegClass.contains(DestReg) || 821 AMDGPU::AGPR_LO16RegClass.contains(DestReg); 822 bool SrcLow = AMDGPU::VGPR_LO16RegClass.contains(SrcReg) || 823 AMDGPU::SReg_LO16RegClass.contains(SrcReg) || 824 AMDGPU::AGPR_LO16RegClass.contains(SrcReg); 825 MCRegister NewDestReg = RI.get32BitRegister(DestReg); 826 MCRegister NewSrcReg = RI.get32BitRegister(SrcReg); 827 828 if (IsSGPRDst) { 829 if (!IsSGPRSrc) { 830 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 831 return; 832 } 833 834 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), NewDestReg) 835 .addReg(NewSrcReg, getKillRegState(KillSrc)); 836 return; 837 } 838 839 if (IsAGPRDst || IsAGPRSrc) { 840 if (!DstLow || !SrcLow) { 841 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc, 842 "Cannot use hi16 subreg with an AGPR!"); 843 } 844 845 copyPhysReg(MBB, MI, DL, NewDestReg, NewSrcReg, KillSrc); 846 return; 847 } 848 849 if (IsSGPRSrc && !ST.hasSDWAScalar()) { 850 if (!DstLow || !SrcLow) { 851 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc, 852 "Cannot use hi16 subreg on VI!"); 853 } 854 855 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), NewDestReg) 856 .addReg(NewSrcReg, getKillRegState(KillSrc)); 857 return; 858 } 859 860 auto MIB = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_sdwa), NewDestReg) 861 .addImm(0) // src0_modifiers 862 .addReg(NewSrcReg) 863 .addImm(0) // clamp 864 .addImm(DstLow ? AMDGPU::SDWA::SdwaSel::WORD_0 865 : AMDGPU::SDWA::SdwaSel::WORD_1) 866 .addImm(AMDGPU::SDWA::DstUnused::UNUSED_PRESERVE) 867 .addImm(SrcLow ? AMDGPU::SDWA::SdwaSel::WORD_0 868 : AMDGPU::SDWA::SdwaSel::WORD_1) 869 .addReg(NewDestReg, RegState::Implicit | RegState::Undef); 870 // First implicit operand is $exec. 871 MIB->tieOperands(0, MIB->getNumOperands() - 1); 872 return; 873 } 874 875 if (RC->hasSuperClassEq(&AMDGPU::VReg_64RegClass) && 876 !RI.hasAGPRs(RI.getPhysRegClass(SrcReg))) { 877 if (ST.hasPackedFP32Ops()) { 878 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), DestReg) 879 .addImm(SISrcMods::OP_SEL_1) 880 .addReg(SrcReg) 881 .addImm(SISrcMods::OP_SEL_0 | SISrcMods::OP_SEL_1) 882 .addReg(SrcReg) 883 .addImm(0) // op_sel_lo 884 .addImm(0) // op_sel_hi 885 .addImm(0) // neg_lo 886 .addImm(0) // neg_hi 887 .addImm(0) // clamp 888 .addReg(SrcReg, getKillRegState(KillSrc) | RegState::Implicit); 889 return; 890 } 891 } 892 893 const bool Forward = RI.getHWRegIndex(DestReg) <= RI.getHWRegIndex(SrcReg); 894 if (RI.isSGPRClass(RC)) { 895 if (!RI.isSGPRClass(RI.getPhysRegClass(SrcReg))) { 896 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 897 return; 898 } 899 expandSGPRCopy(*this, MBB, MI, DL, DestReg, SrcReg, KillSrc, RC, Forward); 900 return; 901 } 902 903 unsigned EltSize = 4; 904 unsigned Opcode = AMDGPU::V_MOV_B32_e32; 905 if (RI.hasAGPRs(RC)) { 906 Opcode = (RI.hasVGPRs(RI.getPhysRegClass(SrcReg))) ? 907 AMDGPU::V_ACCVGPR_WRITE_B32_e64 : AMDGPU::INSTRUCTION_LIST_END; 908 } else if (RI.hasVGPRs(RC) && RI.hasAGPRs(RI.getPhysRegClass(SrcReg))) { 909 Opcode = AMDGPU::V_ACCVGPR_READ_B32_e64; 910 } else if ((Size % 64 == 0) && RI.hasVGPRs(RC) && 911 !RI.hasAGPRs(RI.getPhysRegClass(SrcReg))) { 912 // TODO: In 96-bit case, could do a 64-bit mov and then a 32-bit mov. 913 if (ST.hasPackedFP32Ops()) { 914 Opcode = AMDGPU::V_PK_MOV_B32; 915 EltSize = 8; 916 } 917 } 918 919 // For the cases where we need an intermediate instruction/temporary register 920 // (destination is an AGPR), we need a scavenger. 921 // 922 // FIXME: The pass should maintain this for us so we don't have to re-scan the 923 // whole block for every handled copy. 924 std::unique_ptr<RegScavenger> RS; 925 if (Opcode == AMDGPU::INSTRUCTION_LIST_END) 926 RS.reset(new RegScavenger()); 927 928 ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RC, EltSize); 929 930 // If there is an overlap, we can't kill the super-register on the last 931 // instruction, since it will also kill the components made live by this def. 932 const bool CanKillSuperReg = KillSrc && !RI.regsOverlap(SrcReg, DestReg); 933 934 for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) { 935 unsigned SubIdx; 936 if (Forward) 937 SubIdx = SubIndices[Idx]; 938 else 939 SubIdx = SubIndices[SubIndices.size() - Idx - 1]; 940 941 bool UseKill = CanKillSuperReg && Idx == SubIndices.size() - 1; 942 943 if (Opcode == AMDGPU::INSTRUCTION_LIST_END) { 944 Register ImpDefSuper = Idx == 0 ? Register(DestReg) : Register(); 945 Register ImpUseSuper = SrcReg; 946 indirectCopyToAGPR(*this, MBB, MI, DL, RI.getSubReg(DestReg, SubIdx), 947 RI.getSubReg(SrcReg, SubIdx), UseKill, *RS, 948 ImpDefSuper, ImpUseSuper); 949 } else if (Opcode == AMDGPU::V_PK_MOV_B32) { 950 Register DstSubReg = RI.getSubReg(DestReg, SubIdx); 951 Register SrcSubReg = RI.getSubReg(SrcReg, SubIdx); 952 MachineInstrBuilder MIB = 953 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), DstSubReg) 954 .addImm(SISrcMods::OP_SEL_1) 955 .addReg(SrcSubReg) 956 .addImm(SISrcMods::OP_SEL_0 | SISrcMods::OP_SEL_1) 957 .addReg(SrcSubReg) 958 .addImm(0) // op_sel_lo 959 .addImm(0) // op_sel_hi 960 .addImm(0) // neg_lo 961 .addImm(0) // neg_hi 962 .addImm(0) // clamp 963 .addReg(SrcReg, getKillRegState(UseKill) | RegState::Implicit); 964 if (Idx == 0) 965 MIB.addReg(DestReg, RegState::Define | RegState::Implicit); 966 } else { 967 MachineInstrBuilder Builder = 968 BuildMI(MBB, MI, DL, get(Opcode), RI.getSubReg(DestReg, SubIdx)) 969 .addReg(RI.getSubReg(SrcReg, SubIdx)); 970 if (Idx == 0) 971 Builder.addReg(DestReg, RegState::Define | RegState::Implicit); 972 973 Builder.addReg(SrcReg, getKillRegState(UseKill) | RegState::Implicit); 974 } 975 } 976 } 977 978 int SIInstrInfo::commuteOpcode(unsigned Opcode) const { 979 int NewOpc; 980 981 // Try to map original to commuted opcode 982 NewOpc = AMDGPU::getCommuteRev(Opcode); 983 if (NewOpc != -1) 984 // Check if the commuted (REV) opcode exists on the target. 985 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1; 986 987 // Try to map commuted to original opcode 988 NewOpc = AMDGPU::getCommuteOrig(Opcode); 989 if (NewOpc != -1) 990 // Check if the original (non-REV) opcode exists on the target. 991 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1; 992 993 return Opcode; 994 } 995 996 void SIInstrInfo::materializeImmediate(MachineBasicBlock &MBB, 997 MachineBasicBlock::iterator MI, 998 const DebugLoc &DL, unsigned DestReg, 999 int64_t Value) const { 1000 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1001 const TargetRegisterClass *RegClass = MRI.getRegClass(DestReg); 1002 if (RegClass == &AMDGPU::SReg_32RegClass || 1003 RegClass == &AMDGPU::SGPR_32RegClass || 1004 RegClass == &AMDGPU::SReg_32_XM0RegClass || 1005 RegClass == &AMDGPU::SReg_32_XM0_XEXECRegClass) { 1006 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg) 1007 .addImm(Value); 1008 return; 1009 } 1010 1011 if (RegClass == &AMDGPU::SReg_64RegClass || 1012 RegClass == &AMDGPU::SGPR_64RegClass || 1013 RegClass == &AMDGPU::SReg_64_XEXECRegClass) { 1014 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg) 1015 .addImm(Value); 1016 return; 1017 } 1018 1019 if (RegClass == &AMDGPU::VGPR_32RegClass) { 1020 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg) 1021 .addImm(Value); 1022 return; 1023 } 1024 if (RegClass->hasSuperClassEq(&AMDGPU::VReg_64RegClass)) { 1025 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO), DestReg) 1026 .addImm(Value); 1027 return; 1028 } 1029 1030 unsigned EltSize = 4; 1031 unsigned Opcode = AMDGPU::V_MOV_B32_e32; 1032 if (RI.isSGPRClass(RegClass)) { 1033 if (RI.getRegSizeInBits(*RegClass) > 32) { 1034 Opcode = AMDGPU::S_MOV_B64; 1035 EltSize = 8; 1036 } else { 1037 Opcode = AMDGPU::S_MOV_B32; 1038 EltSize = 4; 1039 } 1040 } 1041 1042 ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RegClass, EltSize); 1043 for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) { 1044 int64_t IdxValue = Idx == 0 ? Value : 0; 1045 1046 MachineInstrBuilder Builder = BuildMI(MBB, MI, DL, 1047 get(Opcode), RI.getSubReg(DestReg, SubIndices[Idx])); 1048 Builder.addImm(IdxValue); 1049 } 1050 } 1051 1052 const TargetRegisterClass * 1053 SIInstrInfo::getPreferredSelectRegClass(unsigned Size) const { 1054 return &AMDGPU::VGPR_32RegClass; 1055 } 1056 1057 void SIInstrInfo::insertVectorSelect(MachineBasicBlock &MBB, 1058 MachineBasicBlock::iterator I, 1059 const DebugLoc &DL, Register DstReg, 1060 ArrayRef<MachineOperand> Cond, 1061 Register TrueReg, 1062 Register FalseReg) const { 1063 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1064 const TargetRegisterClass *BoolXExecRC = 1065 RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 1066 assert(MRI.getRegClass(DstReg) == &AMDGPU::VGPR_32RegClass && 1067 "Not a VGPR32 reg"); 1068 1069 if (Cond.size() == 1) { 1070 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1071 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg) 1072 .add(Cond[0]); 1073 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1074 .addImm(0) 1075 .addReg(FalseReg) 1076 .addImm(0) 1077 .addReg(TrueReg) 1078 .addReg(SReg); 1079 } else if (Cond.size() == 2) { 1080 assert(Cond[0].isImm() && "Cond[0] is not an immediate"); 1081 switch (Cond[0].getImm()) { 1082 case SIInstrInfo::SCC_TRUE: { 1083 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1084 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32 1085 : AMDGPU::S_CSELECT_B64), SReg) 1086 .addImm(1) 1087 .addImm(0); 1088 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1089 .addImm(0) 1090 .addReg(FalseReg) 1091 .addImm(0) 1092 .addReg(TrueReg) 1093 .addReg(SReg); 1094 break; 1095 } 1096 case SIInstrInfo::SCC_FALSE: { 1097 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1098 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32 1099 : AMDGPU::S_CSELECT_B64), SReg) 1100 .addImm(0) 1101 .addImm(1); 1102 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1103 .addImm(0) 1104 .addReg(FalseReg) 1105 .addImm(0) 1106 .addReg(TrueReg) 1107 .addReg(SReg); 1108 break; 1109 } 1110 case SIInstrInfo::VCCNZ: { 1111 MachineOperand RegOp = Cond[1]; 1112 RegOp.setImplicit(false); 1113 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1114 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg) 1115 .add(RegOp); 1116 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1117 .addImm(0) 1118 .addReg(FalseReg) 1119 .addImm(0) 1120 .addReg(TrueReg) 1121 .addReg(SReg); 1122 break; 1123 } 1124 case SIInstrInfo::VCCZ: { 1125 MachineOperand RegOp = Cond[1]; 1126 RegOp.setImplicit(false); 1127 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1128 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg) 1129 .add(RegOp); 1130 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1131 .addImm(0) 1132 .addReg(TrueReg) 1133 .addImm(0) 1134 .addReg(FalseReg) 1135 .addReg(SReg); 1136 break; 1137 } 1138 case SIInstrInfo::EXECNZ: { 1139 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1140 Register SReg2 = MRI.createVirtualRegister(RI.getBoolRC()); 1141 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32 1142 : AMDGPU::S_OR_SAVEEXEC_B64), SReg2) 1143 .addImm(0); 1144 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32 1145 : AMDGPU::S_CSELECT_B64), SReg) 1146 .addImm(1) 1147 .addImm(0); 1148 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1149 .addImm(0) 1150 .addReg(FalseReg) 1151 .addImm(0) 1152 .addReg(TrueReg) 1153 .addReg(SReg); 1154 break; 1155 } 1156 case SIInstrInfo::EXECZ: { 1157 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1158 Register SReg2 = MRI.createVirtualRegister(RI.getBoolRC()); 1159 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32 1160 : AMDGPU::S_OR_SAVEEXEC_B64), SReg2) 1161 .addImm(0); 1162 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32 1163 : AMDGPU::S_CSELECT_B64), SReg) 1164 .addImm(0) 1165 .addImm(1); 1166 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1167 .addImm(0) 1168 .addReg(FalseReg) 1169 .addImm(0) 1170 .addReg(TrueReg) 1171 .addReg(SReg); 1172 llvm_unreachable("Unhandled branch predicate EXECZ"); 1173 break; 1174 } 1175 default: 1176 llvm_unreachable("invalid branch predicate"); 1177 } 1178 } else { 1179 llvm_unreachable("Can only handle Cond size 1 or 2"); 1180 } 1181 } 1182 1183 Register SIInstrInfo::insertEQ(MachineBasicBlock *MBB, 1184 MachineBasicBlock::iterator I, 1185 const DebugLoc &DL, 1186 Register SrcReg, int Value) const { 1187 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 1188 Register Reg = MRI.createVirtualRegister(RI.getBoolRC()); 1189 BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_EQ_I32_e64), Reg) 1190 .addImm(Value) 1191 .addReg(SrcReg); 1192 1193 return Reg; 1194 } 1195 1196 Register SIInstrInfo::insertNE(MachineBasicBlock *MBB, 1197 MachineBasicBlock::iterator I, 1198 const DebugLoc &DL, 1199 Register SrcReg, int Value) const { 1200 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 1201 Register Reg = MRI.createVirtualRegister(RI.getBoolRC()); 1202 BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_NE_I32_e64), Reg) 1203 .addImm(Value) 1204 .addReg(SrcReg); 1205 1206 return Reg; 1207 } 1208 1209 unsigned SIInstrInfo::getMovOpcode(const TargetRegisterClass *DstRC) const { 1210 1211 if (RI.hasAGPRs(DstRC)) 1212 return AMDGPU::COPY; 1213 if (RI.getRegSizeInBits(*DstRC) == 32) { 1214 return RI.isSGPRClass(DstRC) ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32; 1215 } else if (RI.getRegSizeInBits(*DstRC) == 64 && RI.isSGPRClass(DstRC)) { 1216 return AMDGPU::S_MOV_B64; 1217 } else if (RI.getRegSizeInBits(*DstRC) == 64 && !RI.isSGPRClass(DstRC)) { 1218 return AMDGPU::V_MOV_B64_PSEUDO; 1219 } 1220 return AMDGPU::COPY; 1221 } 1222 1223 const MCInstrDesc & 1224 SIInstrInfo::getIndirectGPRIDXPseudo(unsigned VecSize, 1225 bool IsIndirectSrc) const { 1226 if (IsIndirectSrc) { 1227 if (VecSize <= 32) // 4 bytes 1228 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V1); 1229 if (VecSize <= 64) // 8 bytes 1230 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V2); 1231 if (VecSize <= 96) // 12 bytes 1232 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V3); 1233 if (VecSize <= 128) // 16 bytes 1234 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V4); 1235 if (VecSize <= 160) // 20 bytes 1236 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V5); 1237 if (VecSize <= 256) // 32 bytes 1238 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V8); 1239 if (VecSize <= 512) // 64 bytes 1240 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V16); 1241 if (VecSize <= 1024) // 128 bytes 1242 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V32); 1243 1244 llvm_unreachable("unsupported size for IndirectRegReadGPRIDX pseudos"); 1245 } 1246 1247 if (VecSize <= 32) // 4 bytes 1248 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V1); 1249 if (VecSize <= 64) // 8 bytes 1250 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V2); 1251 if (VecSize <= 96) // 12 bytes 1252 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V3); 1253 if (VecSize <= 128) // 16 bytes 1254 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V4); 1255 if (VecSize <= 160) // 20 bytes 1256 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V5); 1257 if (VecSize <= 256) // 32 bytes 1258 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V8); 1259 if (VecSize <= 512) // 64 bytes 1260 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V16); 1261 if (VecSize <= 1024) // 128 bytes 1262 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V32); 1263 1264 llvm_unreachable("unsupported size for IndirectRegWriteGPRIDX pseudos"); 1265 } 1266 1267 static unsigned getIndirectVGPRWriteMovRelPseudoOpc(unsigned VecSize) { 1268 if (VecSize <= 32) // 4 bytes 1269 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V1; 1270 if (VecSize <= 64) // 8 bytes 1271 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V2; 1272 if (VecSize <= 96) // 12 bytes 1273 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V3; 1274 if (VecSize <= 128) // 16 bytes 1275 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V4; 1276 if (VecSize <= 160) // 20 bytes 1277 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V5; 1278 if (VecSize <= 256) // 32 bytes 1279 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V8; 1280 if (VecSize <= 512) // 64 bytes 1281 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V16; 1282 if (VecSize <= 1024) // 128 bytes 1283 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V32; 1284 1285 llvm_unreachable("unsupported size for IndirectRegWrite pseudos"); 1286 } 1287 1288 static unsigned getIndirectSGPRWriteMovRelPseudo32(unsigned VecSize) { 1289 if (VecSize <= 32) // 4 bytes 1290 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V1; 1291 if (VecSize <= 64) // 8 bytes 1292 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V2; 1293 if (VecSize <= 96) // 12 bytes 1294 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V3; 1295 if (VecSize <= 128) // 16 bytes 1296 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V4; 1297 if (VecSize <= 160) // 20 bytes 1298 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V5; 1299 if (VecSize <= 256) // 32 bytes 1300 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V8; 1301 if (VecSize <= 512) // 64 bytes 1302 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V16; 1303 if (VecSize <= 1024) // 128 bytes 1304 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V32; 1305 1306 llvm_unreachable("unsupported size for IndirectRegWrite pseudos"); 1307 } 1308 1309 static unsigned getIndirectSGPRWriteMovRelPseudo64(unsigned VecSize) { 1310 if (VecSize <= 64) // 8 bytes 1311 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V1; 1312 if (VecSize <= 128) // 16 bytes 1313 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V2; 1314 if (VecSize <= 256) // 32 bytes 1315 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V4; 1316 if (VecSize <= 512) // 64 bytes 1317 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V8; 1318 if (VecSize <= 1024) // 128 bytes 1319 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V16; 1320 1321 llvm_unreachable("unsupported size for IndirectRegWrite pseudos"); 1322 } 1323 1324 const MCInstrDesc & 1325 SIInstrInfo::getIndirectRegWriteMovRelPseudo(unsigned VecSize, unsigned EltSize, 1326 bool IsSGPR) const { 1327 if (IsSGPR) { 1328 switch (EltSize) { 1329 case 32: 1330 return get(getIndirectSGPRWriteMovRelPseudo32(VecSize)); 1331 case 64: 1332 return get(getIndirectSGPRWriteMovRelPseudo64(VecSize)); 1333 default: 1334 llvm_unreachable("invalid reg indexing elt size"); 1335 } 1336 } 1337 1338 assert(EltSize == 32 && "invalid reg indexing elt size"); 1339 return get(getIndirectVGPRWriteMovRelPseudoOpc(VecSize)); 1340 } 1341 1342 static unsigned getSGPRSpillSaveOpcode(unsigned Size) { 1343 switch (Size) { 1344 case 4: 1345 return AMDGPU::SI_SPILL_S32_SAVE; 1346 case 8: 1347 return AMDGPU::SI_SPILL_S64_SAVE; 1348 case 12: 1349 return AMDGPU::SI_SPILL_S96_SAVE; 1350 case 16: 1351 return AMDGPU::SI_SPILL_S128_SAVE; 1352 case 20: 1353 return AMDGPU::SI_SPILL_S160_SAVE; 1354 case 24: 1355 return AMDGPU::SI_SPILL_S192_SAVE; 1356 case 32: 1357 return AMDGPU::SI_SPILL_S256_SAVE; 1358 case 64: 1359 return AMDGPU::SI_SPILL_S512_SAVE; 1360 case 128: 1361 return AMDGPU::SI_SPILL_S1024_SAVE; 1362 default: 1363 llvm_unreachable("unknown register size"); 1364 } 1365 } 1366 1367 static unsigned getVGPRSpillSaveOpcode(unsigned Size) { 1368 switch (Size) { 1369 case 4: 1370 return AMDGPU::SI_SPILL_V32_SAVE; 1371 case 8: 1372 return AMDGPU::SI_SPILL_V64_SAVE; 1373 case 12: 1374 return AMDGPU::SI_SPILL_V96_SAVE; 1375 case 16: 1376 return AMDGPU::SI_SPILL_V128_SAVE; 1377 case 20: 1378 return AMDGPU::SI_SPILL_V160_SAVE; 1379 case 24: 1380 return AMDGPU::SI_SPILL_V192_SAVE; 1381 case 32: 1382 return AMDGPU::SI_SPILL_V256_SAVE; 1383 case 64: 1384 return AMDGPU::SI_SPILL_V512_SAVE; 1385 case 128: 1386 return AMDGPU::SI_SPILL_V1024_SAVE; 1387 default: 1388 llvm_unreachable("unknown register size"); 1389 } 1390 } 1391 1392 static unsigned getAGPRSpillSaveOpcode(unsigned Size) { 1393 switch (Size) { 1394 case 4: 1395 return AMDGPU::SI_SPILL_A32_SAVE; 1396 case 8: 1397 return AMDGPU::SI_SPILL_A64_SAVE; 1398 case 12: 1399 return AMDGPU::SI_SPILL_A96_SAVE; 1400 case 16: 1401 return AMDGPU::SI_SPILL_A128_SAVE; 1402 case 20: 1403 return AMDGPU::SI_SPILL_A160_SAVE; 1404 case 24: 1405 return AMDGPU::SI_SPILL_A192_SAVE; 1406 case 32: 1407 return AMDGPU::SI_SPILL_A256_SAVE; 1408 case 64: 1409 return AMDGPU::SI_SPILL_A512_SAVE; 1410 case 128: 1411 return AMDGPU::SI_SPILL_A1024_SAVE; 1412 default: 1413 llvm_unreachable("unknown register size"); 1414 } 1415 } 1416 1417 void SIInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 1418 MachineBasicBlock::iterator MI, 1419 Register SrcReg, bool isKill, 1420 int FrameIndex, 1421 const TargetRegisterClass *RC, 1422 const TargetRegisterInfo *TRI) const { 1423 MachineFunction *MF = MBB.getParent(); 1424 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 1425 MachineFrameInfo &FrameInfo = MF->getFrameInfo(); 1426 const DebugLoc &DL = MBB.findDebugLoc(MI); 1427 1428 MachinePointerInfo PtrInfo 1429 = MachinePointerInfo::getFixedStack(*MF, FrameIndex); 1430 MachineMemOperand *MMO = MF->getMachineMemOperand( 1431 PtrInfo, MachineMemOperand::MOStore, FrameInfo.getObjectSize(FrameIndex), 1432 FrameInfo.getObjectAlign(FrameIndex)); 1433 unsigned SpillSize = TRI->getSpillSize(*RC); 1434 1435 if (RI.isSGPRClass(RC)) { 1436 MFI->setHasSpilledSGPRs(); 1437 assert(SrcReg != AMDGPU::M0 && "m0 should not be spilled"); 1438 assert(SrcReg != AMDGPU::EXEC_LO && SrcReg != AMDGPU::EXEC_HI && 1439 SrcReg != AMDGPU::EXEC && "exec should not be spilled"); 1440 1441 // We are only allowed to create one new instruction when spilling 1442 // registers, so we need to use pseudo instruction for spilling SGPRs. 1443 const MCInstrDesc &OpDesc = get(getSGPRSpillSaveOpcode(SpillSize)); 1444 1445 // The SGPR spill/restore instructions only work on number sgprs, so we need 1446 // to make sure we are using the correct register class. 1447 if (SrcReg.isVirtual() && SpillSize == 4) { 1448 MachineRegisterInfo &MRI = MF->getRegInfo(); 1449 MRI.constrainRegClass(SrcReg, &AMDGPU::SReg_32_XM0_XEXECRegClass); 1450 } 1451 1452 BuildMI(MBB, MI, DL, OpDesc) 1453 .addReg(SrcReg, getKillRegState(isKill)) // data 1454 .addFrameIndex(FrameIndex) // addr 1455 .addMemOperand(MMO) 1456 .addReg(MFI->getStackPtrOffsetReg(), RegState::Implicit); 1457 1458 if (RI.spillSGPRToVGPR()) 1459 FrameInfo.setStackID(FrameIndex, TargetStackID::SGPRSpill); 1460 return; 1461 } 1462 1463 unsigned Opcode = RI.hasAGPRs(RC) ? getAGPRSpillSaveOpcode(SpillSize) 1464 : getVGPRSpillSaveOpcode(SpillSize); 1465 MFI->setHasSpilledVGPRs(); 1466 1467 BuildMI(MBB, MI, DL, get(Opcode)) 1468 .addReg(SrcReg, getKillRegState(isKill)) // data 1469 .addFrameIndex(FrameIndex) // addr 1470 .addReg(MFI->getStackPtrOffsetReg()) // scratch_offset 1471 .addImm(0) // offset 1472 .addMemOperand(MMO); 1473 } 1474 1475 static unsigned getSGPRSpillRestoreOpcode(unsigned Size) { 1476 switch (Size) { 1477 case 4: 1478 return AMDGPU::SI_SPILL_S32_RESTORE; 1479 case 8: 1480 return AMDGPU::SI_SPILL_S64_RESTORE; 1481 case 12: 1482 return AMDGPU::SI_SPILL_S96_RESTORE; 1483 case 16: 1484 return AMDGPU::SI_SPILL_S128_RESTORE; 1485 case 20: 1486 return AMDGPU::SI_SPILL_S160_RESTORE; 1487 case 24: 1488 return AMDGPU::SI_SPILL_S192_RESTORE; 1489 case 32: 1490 return AMDGPU::SI_SPILL_S256_RESTORE; 1491 case 64: 1492 return AMDGPU::SI_SPILL_S512_RESTORE; 1493 case 128: 1494 return AMDGPU::SI_SPILL_S1024_RESTORE; 1495 default: 1496 llvm_unreachable("unknown register size"); 1497 } 1498 } 1499 1500 static unsigned getVGPRSpillRestoreOpcode(unsigned Size) { 1501 switch (Size) { 1502 case 4: 1503 return AMDGPU::SI_SPILL_V32_RESTORE; 1504 case 8: 1505 return AMDGPU::SI_SPILL_V64_RESTORE; 1506 case 12: 1507 return AMDGPU::SI_SPILL_V96_RESTORE; 1508 case 16: 1509 return AMDGPU::SI_SPILL_V128_RESTORE; 1510 case 20: 1511 return AMDGPU::SI_SPILL_V160_RESTORE; 1512 case 24: 1513 return AMDGPU::SI_SPILL_V192_RESTORE; 1514 case 32: 1515 return AMDGPU::SI_SPILL_V256_RESTORE; 1516 case 64: 1517 return AMDGPU::SI_SPILL_V512_RESTORE; 1518 case 128: 1519 return AMDGPU::SI_SPILL_V1024_RESTORE; 1520 default: 1521 llvm_unreachable("unknown register size"); 1522 } 1523 } 1524 1525 static unsigned getAGPRSpillRestoreOpcode(unsigned Size) { 1526 switch (Size) { 1527 case 4: 1528 return AMDGPU::SI_SPILL_A32_RESTORE; 1529 case 8: 1530 return AMDGPU::SI_SPILL_A64_RESTORE; 1531 case 12: 1532 return AMDGPU::SI_SPILL_A96_RESTORE; 1533 case 16: 1534 return AMDGPU::SI_SPILL_A128_RESTORE; 1535 case 20: 1536 return AMDGPU::SI_SPILL_A160_RESTORE; 1537 case 24: 1538 return AMDGPU::SI_SPILL_A192_RESTORE; 1539 case 32: 1540 return AMDGPU::SI_SPILL_A256_RESTORE; 1541 case 64: 1542 return AMDGPU::SI_SPILL_A512_RESTORE; 1543 case 128: 1544 return AMDGPU::SI_SPILL_A1024_RESTORE; 1545 default: 1546 llvm_unreachable("unknown register size"); 1547 } 1548 } 1549 1550 void SIInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 1551 MachineBasicBlock::iterator MI, 1552 Register DestReg, int FrameIndex, 1553 const TargetRegisterClass *RC, 1554 const TargetRegisterInfo *TRI) const { 1555 MachineFunction *MF = MBB.getParent(); 1556 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 1557 MachineFrameInfo &FrameInfo = MF->getFrameInfo(); 1558 const DebugLoc &DL = MBB.findDebugLoc(MI); 1559 unsigned SpillSize = TRI->getSpillSize(*RC); 1560 1561 MachinePointerInfo PtrInfo 1562 = MachinePointerInfo::getFixedStack(*MF, FrameIndex); 1563 1564 MachineMemOperand *MMO = MF->getMachineMemOperand( 1565 PtrInfo, MachineMemOperand::MOLoad, FrameInfo.getObjectSize(FrameIndex), 1566 FrameInfo.getObjectAlign(FrameIndex)); 1567 1568 if (RI.isSGPRClass(RC)) { 1569 MFI->setHasSpilledSGPRs(); 1570 assert(DestReg != AMDGPU::M0 && "m0 should not be reloaded into"); 1571 assert(DestReg != AMDGPU::EXEC_LO && DestReg != AMDGPU::EXEC_HI && 1572 DestReg != AMDGPU::EXEC && "exec should not be spilled"); 1573 1574 // FIXME: Maybe this should not include a memoperand because it will be 1575 // lowered to non-memory instructions. 1576 const MCInstrDesc &OpDesc = get(getSGPRSpillRestoreOpcode(SpillSize)); 1577 if (DestReg.isVirtual() && SpillSize == 4) { 1578 MachineRegisterInfo &MRI = MF->getRegInfo(); 1579 MRI.constrainRegClass(DestReg, &AMDGPU::SReg_32_XM0_XEXECRegClass); 1580 } 1581 1582 if (RI.spillSGPRToVGPR()) 1583 FrameInfo.setStackID(FrameIndex, TargetStackID::SGPRSpill); 1584 BuildMI(MBB, MI, DL, OpDesc, DestReg) 1585 .addFrameIndex(FrameIndex) // addr 1586 .addMemOperand(MMO) 1587 .addReg(MFI->getStackPtrOffsetReg(), RegState::Implicit); 1588 1589 return; 1590 } 1591 1592 unsigned Opcode = RI.hasAGPRs(RC) ? getAGPRSpillRestoreOpcode(SpillSize) 1593 : getVGPRSpillRestoreOpcode(SpillSize); 1594 BuildMI(MBB, MI, DL, get(Opcode), DestReg) 1595 .addFrameIndex(FrameIndex) // vaddr 1596 .addReg(MFI->getStackPtrOffsetReg()) // scratch_offset 1597 .addImm(0) // offset 1598 .addMemOperand(MMO); 1599 } 1600 1601 void SIInstrInfo::insertNoop(MachineBasicBlock &MBB, 1602 MachineBasicBlock::iterator MI) const { 1603 insertNoops(MBB, MI, 1); 1604 } 1605 1606 void SIInstrInfo::insertNoops(MachineBasicBlock &MBB, 1607 MachineBasicBlock::iterator MI, 1608 unsigned Quantity) const { 1609 DebugLoc DL = MBB.findDebugLoc(MI); 1610 while (Quantity > 0) { 1611 unsigned Arg = std::min(Quantity, 8u); 1612 Quantity -= Arg; 1613 BuildMI(MBB, MI, DL, get(AMDGPU::S_NOP)).addImm(Arg - 1); 1614 } 1615 } 1616 1617 void SIInstrInfo::insertReturn(MachineBasicBlock &MBB) const { 1618 auto MF = MBB.getParent(); 1619 SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>(); 1620 1621 assert(Info->isEntryFunction()); 1622 1623 if (MBB.succ_empty()) { 1624 bool HasNoTerminator = MBB.getFirstTerminator() == MBB.end(); 1625 if (HasNoTerminator) { 1626 if (Info->returnsVoid()) { 1627 BuildMI(MBB, MBB.end(), DebugLoc(), get(AMDGPU::S_ENDPGM)).addImm(0); 1628 } else { 1629 BuildMI(MBB, MBB.end(), DebugLoc(), get(AMDGPU::SI_RETURN_TO_EPILOG)); 1630 } 1631 } 1632 } 1633 } 1634 1635 unsigned SIInstrInfo::getNumWaitStates(const MachineInstr &MI) { 1636 switch (MI.getOpcode()) { 1637 default: return 1; // FIXME: Do wait states equal cycles? 1638 1639 case AMDGPU::S_NOP: 1640 return MI.getOperand(0).getImm() + 1; 1641 } 1642 } 1643 1644 bool SIInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 1645 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 1646 MachineBasicBlock &MBB = *MI.getParent(); 1647 DebugLoc DL = MBB.findDebugLoc(MI); 1648 switch (MI.getOpcode()) { 1649 default: return TargetInstrInfo::expandPostRAPseudo(MI); 1650 case AMDGPU::S_MOV_B64_term: 1651 // This is only a terminator to get the correct spill code placement during 1652 // register allocation. 1653 MI.setDesc(get(AMDGPU::S_MOV_B64)); 1654 break; 1655 1656 case AMDGPU::S_MOV_B32_term: 1657 // This is only a terminator to get the correct spill code placement during 1658 // register allocation. 1659 MI.setDesc(get(AMDGPU::S_MOV_B32)); 1660 break; 1661 1662 case AMDGPU::S_XOR_B64_term: 1663 // This is only a terminator to get the correct spill code placement during 1664 // register allocation. 1665 MI.setDesc(get(AMDGPU::S_XOR_B64)); 1666 break; 1667 1668 case AMDGPU::S_XOR_B32_term: 1669 // This is only a terminator to get the correct spill code placement during 1670 // register allocation. 1671 MI.setDesc(get(AMDGPU::S_XOR_B32)); 1672 break; 1673 case AMDGPU::S_OR_B64_term: 1674 // This is only a terminator to get the correct spill code placement during 1675 // register allocation. 1676 MI.setDesc(get(AMDGPU::S_OR_B64)); 1677 break; 1678 case AMDGPU::S_OR_B32_term: 1679 // This is only a terminator to get the correct spill code placement during 1680 // register allocation. 1681 MI.setDesc(get(AMDGPU::S_OR_B32)); 1682 break; 1683 1684 case AMDGPU::S_ANDN2_B64_term: 1685 // This is only a terminator to get the correct spill code placement during 1686 // register allocation. 1687 MI.setDesc(get(AMDGPU::S_ANDN2_B64)); 1688 break; 1689 1690 case AMDGPU::S_ANDN2_B32_term: 1691 // This is only a terminator to get the correct spill code placement during 1692 // register allocation. 1693 MI.setDesc(get(AMDGPU::S_ANDN2_B32)); 1694 break; 1695 1696 case AMDGPU::S_AND_B64_term: 1697 // This is only a terminator to get the correct spill code placement during 1698 // register allocation. 1699 MI.setDesc(get(AMDGPU::S_AND_B64)); 1700 break; 1701 1702 case AMDGPU::S_AND_B32_term: 1703 // This is only a terminator to get the correct spill code placement during 1704 // register allocation. 1705 MI.setDesc(get(AMDGPU::S_AND_B32)); 1706 break; 1707 1708 case AMDGPU::V_MOV_B64_PSEUDO: { 1709 Register Dst = MI.getOperand(0).getReg(); 1710 Register DstLo = RI.getSubReg(Dst, AMDGPU::sub0); 1711 Register DstHi = RI.getSubReg(Dst, AMDGPU::sub1); 1712 1713 const MachineOperand &SrcOp = MI.getOperand(1); 1714 // FIXME: Will this work for 64-bit floating point immediates? 1715 assert(!SrcOp.isFPImm()); 1716 if (SrcOp.isImm()) { 1717 APInt Imm(64, SrcOp.getImm()); 1718 APInt Lo(32, Imm.getLoBits(32).getZExtValue()); 1719 APInt Hi(32, Imm.getHiBits(32).getZExtValue()); 1720 if (ST.hasPackedFP32Ops() && Lo == Hi && isInlineConstant(Lo)) { 1721 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), Dst) 1722 .addImm(SISrcMods::OP_SEL_1) 1723 .addImm(Lo.getSExtValue()) 1724 .addImm(SISrcMods::OP_SEL_1) 1725 .addImm(Lo.getSExtValue()) 1726 .addImm(0) // op_sel_lo 1727 .addImm(0) // op_sel_hi 1728 .addImm(0) // neg_lo 1729 .addImm(0) // neg_hi 1730 .addImm(0); // clamp 1731 } else { 1732 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo) 1733 .addImm(Lo.getZExtValue()) 1734 .addReg(Dst, RegState::Implicit | RegState::Define); 1735 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi) 1736 .addImm(Hi.getZExtValue()) 1737 .addReg(Dst, RegState::Implicit | RegState::Define); 1738 } 1739 } else { 1740 assert(SrcOp.isReg()); 1741 if (ST.hasPackedFP32Ops() && 1742 !RI.isAGPR(MBB.getParent()->getRegInfo(), SrcOp.getReg())) { 1743 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), Dst) 1744 .addImm(SISrcMods::OP_SEL_1) // src0_mod 1745 .addReg(SrcOp.getReg()) 1746 .addImm(SISrcMods::OP_SEL_0 | SISrcMods::OP_SEL_1) // src1_mod 1747 .addReg(SrcOp.getReg()) 1748 .addImm(0) // op_sel_lo 1749 .addImm(0) // op_sel_hi 1750 .addImm(0) // neg_lo 1751 .addImm(0) // neg_hi 1752 .addImm(0); // clamp 1753 } else { 1754 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo) 1755 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub0)) 1756 .addReg(Dst, RegState::Implicit | RegState::Define); 1757 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi) 1758 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub1)) 1759 .addReg(Dst, RegState::Implicit | RegState::Define); 1760 } 1761 } 1762 MI.eraseFromParent(); 1763 break; 1764 } 1765 case AMDGPU::V_MOV_B64_DPP_PSEUDO: { 1766 expandMovDPP64(MI); 1767 break; 1768 } 1769 case AMDGPU::V_SET_INACTIVE_B32: { 1770 unsigned NotOpc = ST.isWave32() ? AMDGPU::S_NOT_B32 : AMDGPU::S_NOT_B64; 1771 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 1772 auto FirstNot = BuildMI(MBB, MI, DL, get(NotOpc), Exec).addReg(Exec); 1773 FirstNot->addRegisterDead(AMDGPU::SCC, TRI); // SCC is overwritten 1774 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), MI.getOperand(0).getReg()) 1775 .add(MI.getOperand(2)); 1776 BuildMI(MBB, MI, DL, get(NotOpc), Exec) 1777 .addReg(Exec); 1778 MI.eraseFromParent(); 1779 break; 1780 } 1781 case AMDGPU::V_SET_INACTIVE_B64: { 1782 unsigned NotOpc = ST.isWave32() ? AMDGPU::S_NOT_B32 : AMDGPU::S_NOT_B64; 1783 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 1784 auto FirstNot = BuildMI(MBB, MI, DL, get(NotOpc), Exec).addReg(Exec); 1785 FirstNot->addRegisterDead(AMDGPU::SCC, TRI); // SCC is overwritten 1786 MachineInstr *Copy = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO), 1787 MI.getOperand(0).getReg()) 1788 .add(MI.getOperand(2)); 1789 expandPostRAPseudo(*Copy); 1790 BuildMI(MBB, MI, DL, get(NotOpc), Exec) 1791 .addReg(Exec); 1792 MI.eraseFromParent(); 1793 break; 1794 } 1795 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V1: 1796 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V2: 1797 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V3: 1798 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V4: 1799 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V5: 1800 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V8: 1801 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V16: 1802 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V32: 1803 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V1: 1804 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V2: 1805 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V3: 1806 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V4: 1807 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V5: 1808 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V8: 1809 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V16: 1810 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V32: 1811 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V1: 1812 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V2: 1813 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V4: 1814 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V8: 1815 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V16: { 1816 const TargetRegisterClass *EltRC = getOpRegClass(MI, 2); 1817 1818 unsigned Opc; 1819 if (RI.hasVGPRs(EltRC)) { 1820 Opc = AMDGPU::V_MOVRELD_B32_e32; 1821 } else { 1822 Opc = RI.getRegSizeInBits(*EltRC) == 64 ? AMDGPU::S_MOVRELD_B64 1823 : AMDGPU::S_MOVRELD_B32; 1824 } 1825 1826 const MCInstrDesc &OpDesc = get(Opc); 1827 Register VecReg = MI.getOperand(0).getReg(); 1828 bool IsUndef = MI.getOperand(1).isUndef(); 1829 unsigned SubReg = MI.getOperand(3).getImm(); 1830 assert(VecReg == MI.getOperand(1).getReg()); 1831 1832 MachineInstrBuilder MIB = 1833 BuildMI(MBB, MI, DL, OpDesc) 1834 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef) 1835 .add(MI.getOperand(2)) 1836 .addReg(VecReg, RegState::ImplicitDefine) 1837 .addReg(VecReg, RegState::Implicit | (IsUndef ? RegState::Undef : 0)); 1838 1839 const int ImpDefIdx = 1840 OpDesc.getNumOperands() + OpDesc.getNumImplicitUses(); 1841 const int ImpUseIdx = ImpDefIdx + 1; 1842 MIB->tieOperands(ImpDefIdx, ImpUseIdx); 1843 MI.eraseFromParent(); 1844 break; 1845 } 1846 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V1: 1847 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V2: 1848 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V3: 1849 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V4: 1850 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V5: 1851 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V8: 1852 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V16: 1853 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V32: { 1854 assert(ST.useVGPRIndexMode()); 1855 Register VecReg = MI.getOperand(0).getReg(); 1856 bool IsUndef = MI.getOperand(1).isUndef(); 1857 Register Idx = MI.getOperand(3).getReg(); 1858 Register SubReg = MI.getOperand(4).getImm(); 1859 1860 MachineInstr *SetOn = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_ON)) 1861 .addReg(Idx) 1862 .addImm(AMDGPU::VGPRIndexMode::DST_ENABLE); 1863 SetOn->getOperand(3).setIsUndef(); 1864 1865 const MCInstrDesc &OpDesc = get(AMDGPU::V_MOV_B32_indirect); 1866 MachineInstrBuilder MIB = 1867 BuildMI(MBB, MI, DL, OpDesc) 1868 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef) 1869 .add(MI.getOperand(2)) 1870 .addReg(VecReg, RegState::ImplicitDefine) 1871 .addReg(VecReg, 1872 RegState::Implicit | (IsUndef ? RegState::Undef : 0)); 1873 1874 const int ImpDefIdx = OpDesc.getNumOperands() + OpDesc.getNumImplicitUses(); 1875 const int ImpUseIdx = ImpDefIdx + 1; 1876 MIB->tieOperands(ImpDefIdx, ImpUseIdx); 1877 1878 MachineInstr *SetOff = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_OFF)); 1879 1880 finalizeBundle(MBB, SetOn->getIterator(), std::next(SetOff->getIterator())); 1881 1882 MI.eraseFromParent(); 1883 break; 1884 } 1885 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V1: 1886 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V2: 1887 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V3: 1888 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V4: 1889 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V5: 1890 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V8: 1891 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V16: 1892 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V32: { 1893 assert(ST.useVGPRIndexMode()); 1894 Register Dst = MI.getOperand(0).getReg(); 1895 Register VecReg = MI.getOperand(1).getReg(); 1896 bool IsUndef = MI.getOperand(1).isUndef(); 1897 Register Idx = MI.getOperand(2).getReg(); 1898 Register SubReg = MI.getOperand(3).getImm(); 1899 1900 MachineInstr *SetOn = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_ON)) 1901 .addReg(Idx) 1902 .addImm(AMDGPU::VGPRIndexMode::SRC0_ENABLE); 1903 SetOn->getOperand(3).setIsUndef(); 1904 1905 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32)) 1906 .addDef(Dst) 1907 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef) 1908 .addReg(VecReg, RegState::Implicit | (IsUndef ? RegState::Undef : 0)) 1909 .addReg(AMDGPU::M0, RegState::Implicit); 1910 1911 MachineInstr *SetOff = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_OFF)); 1912 1913 finalizeBundle(MBB, SetOn->getIterator(), std::next(SetOff->getIterator())); 1914 1915 MI.eraseFromParent(); 1916 break; 1917 } 1918 case AMDGPU::SI_PC_ADD_REL_OFFSET: { 1919 MachineFunction &MF = *MBB.getParent(); 1920 Register Reg = MI.getOperand(0).getReg(); 1921 Register RegLo = RI.getSubReg(Reg, AMDGPU::sub0); 1922 Register RegHi = RI.getSubReg(Reg, AMDGPU::sub1); 1923 1924 // Create a bundle so these instructions won't be re-ordered by the 1925 // post-RA scheduler. 1926 MIBundleBuilder Bundler(MBB, MI); 1927 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_GETPC_B64), Reg)); 1928 1929 // Add 32-bit offset from this instruction to the start of the 1930 // constant data. 1931 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_ADD_U32), RegLo) 1932 .addReg(RegLo) 1933 .add(MI.getOperand(1))); 1934 1935 MachineInstrBuilder MIB = BuildMI(MF, DL, get(AMDGPU::S_ADDC_U32), RegHi) 1936 .addReg(RegHi); 1937 MIB.add(MI.getOperand(2)); 1938 1939 Bundler.append(MIB); 1940 finalizeBundle(MBB, Bundler.begin()); 1941 1942 MI.eraseFromParent(); 1943 break; 1944 } 1945 case AMDGPU::ENTER_WWM: { 1946 // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when 1947 // WWM is entered. 1948 MI.setDesc(get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32 1949 : AMDGPU::S_OR_SAVEEXEC_B64)); 1950 break; 1951 } 1952 case AMDGPU::EXIT_WWM: { 1953 // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when 1954 // WWM is exited. 1955 MI.setDesc(get(ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64)); 1956 break; 1957 } 1958 } 1959 return true; 1960 } 1961 1962 std::pair<MachineInstr*, MachineInstr*> 1963 SIInstrInfo::expandMovDPP64(MachineInstr &MI) const { 1964 assert (MI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO); 1965 1966 MachineBasicBlock &MBB = *MI.getParent(); 1967 DebugLoc DL = MBB.findDebugLoc(MI); 1968 MachineFunction *MF = MBB.getParent(); 1969 MachineRegisterInfo &MRI = MF->getRegInfo(); 1970 Register Dst = MI.getOperand(0).getReg(); 1971 unsigned Part = 0; 1972 MachineInstr *Split[2]; 1973 1974 for (auto Sub : { AMDGPU::sub0, AMDGPU::sub1 }) { 1975 auto MovDPP = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_dpp)); 1976 if (Dst.isPhysical()) { 1977 MovDPP.addDef(RI.getSubReg(Dst, Sub)); 1978 } else { 1979 assert(MRI.isSSA()); 1980 auto Tmp = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 1981 MovDPP.addDef(Tmp); 1982 } 1983 1984 for (unsigned I = 1; I <= 2; ++I) { // old and src operands. 1985 const MachineOperand &SrcOp = MI.getOperand(I); 1986 assert(!SrcOp.isFPImm()); 1987 if (SrcOp.isImm()) { 1988 APInt Imm(64, SrcOp.getImm()); 1989 Imm.ashrInPlace(Part * 32); 1990 MovDPP.addImm(Imm.getLoBits(32).getZExtValue()); 1991 } else { 1992 assert(SrcOp.isReg()); 1993 Register Src = SrcOp.getReg(); 1994 if (Src.isPhysical()) 1995 MovDPP.addReg(RI.getSubReg(Src, Sub)); 1996 else 1997 MovDPP.addReg(Src, SrcOp.isUndef() ? RegState::Undef : 0, Sub); 1998 } 1999 } 2000 2001 for (unsigned I = 3; I < MI.getNumExplicitOperands(); ++I) 2002 MovDPP.addImm(MI.getOperand(I).getImm()); 2003 2004 Split[Part] = MovDPP; 2005 ++Part; 2006 } 2007 2008 if (Dst.isVirtual()) 2009 BuildMI(MBB, MI, DL, get(AMDGPU::REG_SEQUENCE), Dst) 2010 .addReg(Split[0]->getOperand(0).getReg()) 2011 .addImm(AMDGPU::sub0) 2012 .addReg(Split[1]->getOperand(0).getReg()) 2013 .addImm(AMDGPU::sub1); 2014 2015 MI.eraseFromParent(); 2016 return std::make_pair(Split[0], Split[1]); 2017 } 2018 2019 bool SIInstrInfo::swapSourceModifiers(MachineInstr &MI, 2020 MachineOperand &Src0, 2021 unsigned Src0OpName, 2022 MachineOperand &Src1, 2023 unsigned Src1OpName) const { 2024 MachineOperand *Src0Mods = getNamedOperand(MI, Src0OpName); 2025 if (!Src0Mods) 2026 return false; 2027 2028 MachineOperand *Src1Mods = getNamedOperand(MI, Src1OpName); 2029 assert(Src1Mods && 2030 "All commutable instructions have both src0 and src1 modifiers"); 2031 2032 int Src0ModsVal = Src0Mods->getImm(); 2033 int Src1ModsVal = Src1Mods->getImm(); 2034 2035 Src1Mods->setImm(Src0ModsVal); 2036 Src0Mods->setImm(Src1ModsVal); 2037 return true; 2038 } 2039 2040 static MachineInstr *swapRegAndNonRegOperand(MachineInstr &MI, 2041 MachineOperand &RegOp, 2042 MachineOperand &NonRegOp) { 2043 Register Reg = RegOp.getReg(); 2044 unsigned SubReg = RegOp.getSubReg(); 2045 bool IsKill = RegOp.isKill(); 2046 bool IsDead = RegOp.isDead(); 2047 bool IsUndef = RegOp.isUndef(); 2048 bool IsDebug = RegOp.isDebug(); 2049 2050 if (NonRegOp.isImm()) 2051 RegOp.ChangeToImmediate(NonRegOp.getImm()); 2052 else if (NonRegOp.isFI()) 2053 RegOp.ChangeToFrameIndex(NonRegOp.getIndex()); 2054 else if (NonRegOp.isGlobal()) { 2055 RegOp.ChangeToGA(NonRegOp.getGlobal(), NonRegOp.getOffset(), 2056 NonRegOp.getTargetFlags()); 2057 } else 2058 return nullptr; 2059 2060 // Make sure we don't reinterpret a subreg index in the target flags. 2061 RegOp.setTargetFlags(NonRegOp.getTargetFlags()); 2062 2063 NonRegOp.ChangeToRegister(Reg, false, false, IsKill, IsDead, IsUndef, IsDebug); 2064 NonRegOp.setSubReg(SubReg); 2065 2066 return &MI; 2067 } 2068 2069 MachineInstr *SIInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, 2070 unsigned Src0Idx, 2071 unsigned Src1Idx) const { 2072 assert(!NewMI && "this should never be used"); 2073 2074 unsigned Opc = MI.getOpcode(); 2075 int CommutedOpcode = commuteOpcode(Opc); 2076 if (CommutedOpcode == -1) 2077 return nullptr; 2078 2079 assert(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) == 2080 static_cast<int>(Src0Idx) && 2081 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1) == 2082 static_cast<int>(Src1Idx) && 2083 "inconsistency with findCommutedOpIndices"); 2084 2085 MachineOperand &Src0 = MI.getOperand(Src0Idx); 2086 MachineOperand &Src1 = MI.getOperand(Src1Idx); 2087 2088 MachineInstr *CommutedMI = nullptr; 2089 if (Src0.isReg() && Src1.isReg()) { 2090 if (isOperandLegal(MI, Src1Idx, &Src0)) { 2091 // Be sure to copy the source modifiers to the right place. 2092 CommutedMI 2093 = TargetInstrInfo::commuteInstructionImpl(MI, NewMI, Src0Idx, Src1Idx); 2094 } 2095 2096 } else if (Src0.isReg() && !Src1.isReg()) { 2097 // src0 should always be able to support any operand type, so no need to 2098 // check operand legality. 2099 CommutedMI = swapRegAndNonRegOperand(MI, Src0, Src1); 2100 } else if (!Src0.isReg() && Src1.isReg()) { 2101 if (isOperandLegal(MI, Src1Idx, &Src0)) 2102 CommutedMI = swapRegAndNonRegOperand(MI, Src1, Src0); 2103 } else { 2104 // FIXME: Found two non registers to commute. This does happen. 2105 return nullptr; 2106 } 2107 2108 if (CommutedMI) { 2109 swapSourceModifiers(MI, Src0, AMDGPU::OpName::src0_modifiers, 2110 Src1, AMDGPU::OpName::src1_modifiers); 2111 2112 CommutedMI->setDesc(get(CommutedOpcode)); 2113 } 2114 2115 return CommutedMI; 2116 } 2117 2118 // This needs to be implemented because the source modifiers may be inserted 2119 // between the true commutable operands, and the base 2120 // TargetInstrInfo::commuteInstruction uses it. 2121 bool SIInstrInfo::findCommutedOpIndices(const MachineInstr &MI, 2122 unsigned &SrcOpIdx0, 2123 unsigned &SrcOpIdx1) const { 2124 return findCommutedOpIndices(MI.getDesc(), SrcOpIdx0, SrcOpIdx1); 2125 } 2126 2127 bool SIInstrInfo::findCommutedOpIndices(MCInstrDesc Desc, unsigned &SrcOpIdx0, 2128 unsigned &SrcOpIdx1) const { 2129 if (!Desc.isCommutable()) 2130 return false; 2131 2132 unsigned Opc = Desc.getOpcode(); 2133 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 2134 if (Src0Idx == -1) 2135 return false; 2136 2137 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 2138 if (Src1Idx == -1) 2139 return false; 2140 2141 return fixCommutedOpIndices(SrcOpIdx0, SrcOpIdx1, Src0Idx, Src1Idx); 2142 } 2143 2144 bool SIInstrInfo::isBranchOffsetInRange(unsigned BranchOp, 2145 int64_t BrOffset) const { 2146 // BranchRelaxation should never have to check s_setpc_b64 because its dest 2147 // block is unanalyzable. 2148 assert(BranchOp != AMDGPU::S_SETPC_B64); 2149 2150 // Convert to dwords. 2151 BrOffset /= 4; 2152 2153 // The branch instructions do PC += signext(SIMM16 * 4) + 4, so the offset is 2154 // from the next instruction. 2155 BrOffset -= 1; 2156 2157 return isIntN(BranchOffsetBits, BrOffset); 2158 } 2159 2160 MachineBasicBlock *SIInstrInfo::getBranchDestBlock( 2161 const MachineInstr &MI) const { 2162 if (MI.getOpcode() == AMDGPU::S_SETPC_B64) { 2163 // This would be a difficult analysis to perform, but can always be legal so 2164 // there's no need to analyze it. 2165 return nullptr; 2166 } 2167 2168 return MI.getOperand(0).getMBB(); 2169 } 2170 2171 unsigned SIInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB, 2172 MachineBasicBlock &DestBB, 2173 const DebugLoc &DL, 2174 int64_t BrOffset, 2175 RegScavenger *RS) const { 2176 assert(RS && "RegScavenger required for long branching"); 2177 assert(MBB.empty() && 2178 "new block should be inserted for expanding unconditional branch"); 2179 assert(MBB.pred_size() == 1); 2180 2181 MachineFunction *MF = MBB.getParent(); 2182 MachineRegisterInfo &MRI = MF->getRegInfo(); 2183 2184 // FIXME: Virtual register workaround for RegScavenger not working with empty 2185 // blocks. 2186 Register PCReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 2187 2188 auto I = MBB.end(); 2189 2190 // We need to compute the offset relative to the instruction immediately after 2191 // s_getpc_b64. Insert pc arithmetic code before last terminator. 2192 MachineInstr *GetPC = BuildMI(MBB, I, DL, get(AMDGPU::S_GETPC_B64), PCReg); 2193 2194 // TODO: Handle > 32-bit block address. 2195 if (BrOffset >= 0) { 2196 BuildMI(MBB, I, DL, get(AMDGPU::S_ADD_U32)) 2197 .addReg(PCReg, RegState::Define, AMDGPU::sub0) 2198 .addReg(PCReg, 0, AMDGPU::sub0) 2199 .addMBB(&DestBB, MO_LONG_BRANCH_FORWARD); 2200 BuildMI(MBB, I, DL, get(AMDGPU::S_ADDC_U32)) 2201 .addReg(PCReg, RegState::Define, AMDGPU::sub1) 2202 .addReg(PCReg, 0, AMDGPU::sub1) 2203 .addImm(0); 2204 } else { 2205 // Backwards branch. 2206 BuildMI(MBB, I, DL, get(AMDGPU::S_SUB_U32)) 2207 .addReg(PCReg, RegState::Define, AMDGPU::sub0) 2208 .addReg(PCReg, 0, AMDGPU::sub0) 2209 .addMBB(&DestBB, MO_LONG_BRANCH_BACKWARD); 2210 BuildMI(MBB, I, DL, get(AMDGPU::S_SUBB_U32)) 2211 .addReg(PCReg, RegState::Define, AMDGPU::sub1) 2212 .addReg(PCReg, 0, AMDGPU::sub1) 2213 .addImm(0); 2214 } 2215 2216 // Insert the indirect branch after the other terminator. 2217 BuildMI(&MBB, DL, get(AMDGPU::S_SETPC_B64)) 2218 .addReg(PCReg); 2219 2220 // FIXME: If spilling is necessary, this will fail because this scavenger has 2221 // no emergency stack slots. It is non-trivial to spill in this situation, 2222 // because the restore code needs to be specially placed after the 2223 // jump. BranchRelaxation then needs to be made aware of the newly inserted 2224 // block. 2225 // 2226 // If a spill is needed for the pc register pair, we need to insert a spill 2227 // restore block right before the destination block, and insert a short branch 2228 // into the old destination block's fallthrough predecessor. 2229 // e.g.: 2230 // 2231 // s_cbranch_scc0 skip_long_branch: 2232 // 2233 // long_branch_bb: 2234 // spill s[8:9] 2235 // s_getpc_b64 s[8:9] 2236 // s_add_u32 s8, s8, restore_bb 2237 // s_addc_u32 s9, s9, 0 2238 // s_setpc_b64 s[8:9] 2239 // 2240 // skip_long_branch: 2241 // foo; 2242 // 2243 // ..... 2244 // 2245 // dest_bb_fallthrough_predecessor: 2246 // bar; 2247 // s_branch dest_bb 2248 // 2249 // restore_bb: 2250 // restore s[8:9] 2251 // fallthrough dest_bb 2252 /// 2253 // dest_bb: 2254 // buzz; 2255 2256 RS->enterBasicBlockEnd(MBB); 2257 Register Scav = RS->scavengeRegisterBackwards( 2258 AMDGPU::SReg_64RegClass, 2259 MachineBasicBlock::iterator(GetPC), false, 0); 2260 MRI.replaceRegWith(PCReg, Scav); 2261 MRI.clearVirtRegs(); 2262 RS->setRegUsed(Scav); 2263 2264 return 4 + 8 + 4 + 4; 2265 } 2266 2267 unsigned SIInstrInfo::getBranchOpcode(SIInstrInfo::BranchPredicate Cond) { 2268 switch (Cond) { 2269 case SIInstrInfo::SCC_TRUE: 2270 return AMDGPU::S_CBRANCH_SCC1; 2271 case SIInstrInfo::SCC_FALSE: 2272 return AMDGPU::S_CBRANCH_SCC0; 2273 case SIInstrInfo::VCCNZ: 2274 return AMDGPU::S_CBRANCH_VCCNZ; 2275 case SIInstrInfo::VCCZ: 2276 return AMDGPU::S_CBRANCH_VCCZ; 2277 case SIInstrInfo::EXECNZ: 2278 return AMDGPU::S_CBRANCH_EXECNZ; 2279 case SIInstrInfo::EXECZ: 2280 return AMDGPU::S_CBRANCH_EXECZ; 2281 default: 2282 llvm_unreachable("invalid branch predicate"); 2283 } 2284 } 2285 2286 SIInstrInfo::BranchPredicate SIInstrInfo::getBranchPredicate(unsigned Opcode) { 2287 switch (Opcode) { 2288 case AMDGPU::S_CBRANCH_SCC0: 2289 return SCC_FALSE; 2290 case AMDGPU::S_CBRANCH_SCC1: 2291 return SCC_TRUE; 2292 case AMDGPU::S_CBRANCH_VCCNZ: 2293 return VCCNZ; 2294 case AMDGPU::S_CBRANCH_VCCZ: 2295 return VCCZ; 2296 case AMDGPU::S_CBRANCH_EXECNZ: 2297 return EXECNZ; 2298 case AMDGPU::S_CBRANCH_EXECZ: 2299 return EXECZ; 2300 default: 2301 return INVALID_BR; 2302 } 2303 } 2304 2305 bool SIInstrInfo::analyzeBranchImpl(MachineBasicBlock &MBB, 2306 MachineBasicBlock::iterator I, 2307 MachineBasicBlock *&TBB, 2308 MachineBasicBlock *&FBB, 2309 SmallVectorImpl<MachineOperand> &Cond, 2310 bool AllowModify) const { 2311 if (I->getOpcode() == AMDGPU::S_BRANCH) { 2312 // Unconditional Branch 2313 TBB = I->getOperand(0).getMBB(); 2314 return false; 2315 } 2316 2317 MachineBasicBlock *CondBB = nullptr; 2318 2319 if (I->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 2320 CondBB = I->getOperand(1).getMBB(); 2321 Cond.push_back(I->getOperand(0)); 2322 } else { 2323 BranchPredicate Pred = getBranchPredicate(I->getOpcode()); 2324 if (Pred == INVALID_BR) 2325 return true; 2326 2327 CondBB = I->getOperand(0).getMBB(); 2328 Cond.push_back(MachineOperand::CreateImm(Pred)); 2329 Cond.push_back(I->getOperand(1)); // Save the branch register. 2330 } 2331 ++I; 2332 2333 if (I == MBB.end()) { 2334 // Conditional branch followed by fall-through. 2335 TBB = CondBB; 2336 return false; 2337 } 2338 2339 if (I->getOpcode() == AMDGPU::S_BRANCH) { 2340 TBB = CondBB; 2341 FBB = I->getOperand(0).getMBB(); 2342 return false; 2343 } 2344 2345 return true; 2346 } 2347 2348 bool SIInstrInfo::analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, 2349 MachineBasicBlock *&FBB, 2350 SmallVectorImpl<MachineOperand> &Cond, 2351 bool AllowModify) const { 2352 MachineBasicBlock::iterator I = MBB.getFirstTerminator(); 2353 auto E = MBB.end(); 2354 if (I == E) 2355 return false; 2356 2357 // Skip over the instructions that are artificially terminators for special 2358 // exec management. 2359 while (I != E && !I->isBranch() && !I->isReturn() && 2360 I->getOpcode() != AMDGPU::SI_MASK_BRANCH) { 2361 switch (I->getOpcode()) { 2362 case AMDGPU::SI_MASK_BRANCH: 2363 case AMDGPU::S_MOV_B64_term: 2364 case AMDGPU::S_XOR_B64_term: 2365 case AMDGPU::S_OR_B64_term: 2366 case AMDGPU::S_ANDN2_B64_term: 2367 case AMDGPU::S_AND_B64_term: 2368 case AMDGPU::S_MOV_B32_term: 2369 case AMDGPU::S_XOR_B32_term: 2370 case AMDGPU::S_OR_B32_term: 2371 case AMDGPU::S_ANDN2_B32_term: 2372 case AMDGPU::S_AND_B32_term: 2373 break; 2374 case AMDGPU::SI_IF: 2375 case AMDGPU::SI_ELSE: 2376 case AMDGPU::SI_KILL_I1_TERMINATOR: 2377 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR: 2378 // FIXME: It's messy that these need to be considered here at all. 2379 return true; 2380 default: 2381 llvm_unreachable("unexpected non-branch terminator inst"); 2382 } 2383 2384 ++I; 2385 } 2386 2387 if (I == E) 2388 return false; 2389 2390 if (I->getOpcode() != AMDGPU::SI_MASK_BRANCH) 2391 return analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify); 2392 2393 ++I; 2394 2395 // TODO: Should be able to treat as fallthrough? 2396 if (I == MBB.end()) 2397 return true; 2398 2399 if (analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify)) 2400 return true; 2401 2402 MachineBasicBlock *MaskBrDest = I->getOperand(0).getMBB(); 2403 2404 // Specifically handle the case where the conditional branch is to the same 2405 // destination as the mask branch. e.g. 2406 // 2407 // si_mask_branch BB8 2408 // s_cbranch_execz BB8 2409 // s_cbranch BB9 2410 // 2411 // This is required to understand divergent loops which may need the branches 2412 // to be relaxed. 2413 if (TBB != MaskBrDest || Cond.empty()) 2414 return true; 2415 2416 auto Pred = Cond[0].getImm(); 2417 return (Pred != EXECZ && Pred != EXECNZ); 2418 } 2419 2420 unsigned SIInstrInfo::removeBranch(MachineBasicBlock &MBB, 2421 int *BytesRemoved) const { 2422 MachineBasicBlock::iterator I = MBB.getFirstTerminator(); 2423 2424 unsigned Count = 0; 2425 unsigned RemovedSize = 0; 2426 while (I != MBB.end()) { 2427 MachineBasicBlock::iterator Next = std::next(I); 2428 if (I->getOpcode() == AMDGPU::SI_MASK_BRANCH) { 2429 I = Next; 2430 continue; 2431 } 2432 2433 RemovedSize += getInstSizeInBytes(*I); 2434 I->eraseFromParent(); 2435 ++Count; 2436 I = Next; 2437 } 2438 2439 if (BytesRemoved) 2440 *BytesRemoved = RemovedSize; 2441 2442 return Count; 2443 } 2444 2445 // Copy the flags onto the implicit condition register operand. 2446 static void preserveCondRegFlags(MachineOperand &CondReg, 2447 const MachineOperand &OrigCond) { 2448 CondReg.setIsUndef(OrigCond.isUndef()); 2449 CondReg.setIsKill(OrigCond.isKill()); 2450 } 2451 2452 unsigned SIInstrInfo::insertBranch(MachineBasicBlock &MBB, 2453 MachineBasicBlock *TBB, 2454 MachineBasicBlock *FBB, 2455 ArrayRef<MachineOperand> Cond, 2456 const DebugLoc &DL, 2457 int *BytesAdded) const { 2458 if (!FBB && Cond.empty()) { 2459 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH)) 2460 .addMBB(TBB); 2461 if (BytesAdded) 2462 *BytesAdded = ST.hasOffset3fBug() ? 8 : 4; 2463 return 1; 2464 } 2465 2466 if(Cond.size() == 1 && Cond[0].isReg()) { 2467 BuildMI(&MBB, DL, get(AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO)) 2468 .add(Cond[0]) 2469 .addMBB(TBB); 2470 return 1; 2471 } 2472 2473 assert(TBB && Cond[0].isImm()); 2474 2475 unsigned Opcode 2476 = getBranchOpcode(static_cast<BranchPredicate>(Cond[0].getImm())); 2477 2478 if (!FBB) { 2479 Cond[1].isUndef(); 2480 MachineInstr *CondBr = 2481 BuildMI(&MBB, DL, get(Opcode)) 2482 .addMBB(TBB); 2483 2484 // Copy the flags onto the implicit condition register operand. 2485 preserveCondRegFlags(CondBr->getOperand(1), Cond[1]); 2486 fixImplicitOperands(*CondBr); 2487 2488 if (BytesAdded) 2489 *BytesAdded = ST.hasOffset3fBug() ? 8 : 4; 2490 return 1; 2491 } 2492 2493 assert(TBB && FBB); 2494 2495 MachineInstr *CondBr = 2496 BuildMI(&MBB, DL, get(Opcode)) 2497 .addMBB(TBB); 2498 fixImplicitOperands(*CondBr); 2499 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH)) 2500 .addMBB(FBB); 2501 2502 MachineOperand &CondReg = CondBr->getOperand(1); 2503 CondReg.setIsUndef(Cond[1].isUndef()); 2504 CondReg.setIsKill(Cond[1].isKill()); 2505 2506 if (BytesAdded) 2507 *BytesAdded = ST.hasOffset3fBug() ? 16 : 8; 2508 2509 return 2; 2510 } 2511 2512 bool SIInstrInfo::reverseBranchCondition( 2513 SmallVectorImpl<MachineOperand> &Cond) const { 2514 if (Cond.size() != 2) { 2515 return true; 2516 } 2517 2518 if (Cond[0].isImm()) { 2519 Cond[0].setImm(-Cond[0].getImm()); 2520 return false; 2521 } 2522 2523 return true; 2524 } 2525 2526 bool SIInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, 2527 ArrayRef<MachineOperand> Cond, 2528 Register DstReg, Register TrueReg, 2529 Register FalseReg, int &CondCycles, 2530 int &TrueCycles, int &FalseCycles) const { 2531 switch (Cond[0].getImm()) { 2532 case VCCNZ: 2533 case VCCZ: { 2534 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 2535 const TargetRegisterClass *RC = MRI.getRegClass(TrueReg); 2536 if (MRI.getRegClass(FalseReg) != RC) 2537 return false; 2538 2539 int NumInsts = AMDGPU::getRegBitWidth(RC->getID()) / 32; 2540 CondCycles = TrueCycles = FalseCycles = NumInsts; // ??? 2541 2542 // Limit to equal cost for branch vs. N v_cndmask_b32s. 2543 return RI.hasVGPRs(RC) && NumInsts <= 6; 2544 } 2545 case SCC_TRUE: 2546 case SCC_FALSE: { 2547 // FIXME: We could insert for VGPRs if we could replace the original compare 2548 // with a vector one. 2549 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 2550 const TargetRegisterClass *RC = MRI.getRegClass(TrueReg); 2551 if (MRI.getRegClass(FalseReg) != RC) 2552 return false; 2553 2554 int NumInsts = AMDGPU::getRegBitWidth(RC->getID()) / 32; 2555 2556 // Multiples of 8 can do s_cselect_b64 2557 if (NumInsts % 2 == 0) 2558 NumInsts /= 2; 2559 2560 CondCycles = TrueCycles = FalseCycles = NumInsts; // ??? 2561 return RI.isSGPRClass(RC); 2562 } 2563 default: 2564 return false; 2565 } 2566 } 2567 2568 void SIInstrInfo::insertSelect(MachineBasicBlock &MBB, 2569 MachineBasicBlock::iterator I, const DebugLoc &DL, 2570 Register DstReg, ArrayRef<MachineOperand> Cond, 2571 Register TrueReg, Register FalseReg) const { 2572 BranchPredicate Pred = static_cast<BranchPredicate>(Cond[0].getImm()); 2573 if (Pred == VCCZ || Pred == SCC_FALSE) { 2574 Pred = static_cast<BranchPredicate>(-Pred); 2575 std::swap(TrueReg, FalseReg); 2576 } 2577 2578 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 2579 const TargetRegisterClass *DstRC = MRI.getRegClass(DstReg); 2580 unsigned DstSize = RI.getRegSizeInBits(*DstRC); 2581 2582 if (DstSize == 32) { 2583 MachineInstr *Select; 2584 if (Pred == SCC_TRUE) { 2585 Select = BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B32), DstReg) 2586 .addReg(TrueReg) 2587 .addReg(FalseReg); 2588 } else { 2589 // Instruction's operands are backwards from what is expected. 2590 Select = BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e32), DstReg) 2591 .addReg(FalseReg) 2592 .addReg(TrueReg); 2593 } 2594 2595 preserveCondRegFlags(Select->getOperand(3), Cond[1]); 2596 return; 2597 } 2598 2599 if (DstSize == 64 && Pred == SCC_TRUE) { 2600 MachineInstr *Select = 2601 BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B64), DstReg) 2602 .addReg(TrueReg) 2603 .addReg(FalseReg); 2604 2605 preserveCondRegFlags(Select->getOperand(3), Cond[1]); 2606 return; 2607 } 2608 2609 static const int16_t Sub0_15[] = { 2610 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3, 2611 AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7, 2612 AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11, 2613 AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15, 2614 }; 2615 2616 static const int16_t Sub0_15_64[] = { 2617 AMDGPU::sub0_sub1, AMDGPU::sub2_sub3, 2618 AMDGPU::sub4_sub5, AMDGPU::sub6_sub7, 2619 AMDGPU::sub8_sub9, AMDGPU::sub10_sub11, 2620 AMDGPU::sub12_sub13, AMDGPU::sub14_sub15, 2621 }; 2622 2623 unsigned SelOp = AMDGPU::V_CNDMASK_B32_e32; 2624 const TargetRegisterClass *EltRC = &AMDGPU::VGPR_32RegClass; 2625 const int16_t *SubIndices = Sub0_15; 2626 int NElts = DstSize / 32; 2627 2628 // 64-bit select is only available for SALU. 2629 // TODO: Split 96-bit into 64-bit and 32-bit, not 3x 32-bit. 2630 if (Pred == SCC_TRUE) { 2631 if (NElts % 2) { 2632 SelOp = AMDGPU::S_CSELECT_B32; 2633 EltRC = &AMDGPU::SGPR_32RegClass; 2634 } else { 2635 SelOp = AMDGPU::S_CSELECT_B64; 2636 EltRC = &AMDGPU::SGPR_64RegClass; 2637 SubIndices = Sub0_15_64; 2638 NElts /= 2; 2639 } 2640 } 2641 2642 MachineInstrBuilder MIB = BuildMI( 2643 MBB, I, DL, get(AMDGPU::REG_SEQUENCE), DstReg); 2644 2645 I = MIB->getIterator(); 2646 2647 SmallVector<Register, 8> Regs; 2648 for (int Idx = 0; Idx != NElts; ++Idx) { 2649 Register DstElt = MRI.createVirtualRegister(EltRC); 2650 Regs.push_back(DstElt); 2651 2652 unsigned SubIdx = SubIndices[Idx]; 2653 2654 MachineInstr *Select; 2655 if (SelOp == AMDGPU::V_CNDMASK_B32_e32) { 2656 Select = 2657 BuildMI(MBB, I, DL, get(SelOp), DstElt) 2658 .addReg(FalseReg, 0, SubIdx) 2659 .addReg(TrueReg, 0, SubIdx); 2660 } else { 2661 Select = 2662 BuildMI(MBB, I, DL, get(SelOp), DstElt) 2663 .addReg(TrueReg, 0, SubIdx) 2664 .addReg(FalseReg, 0, SubIdx); 2665 } 2666 2667 preserveCondRegFlags(Select->getOperand(3), Cond[1]); 2668 fixImplicitOperands(*Select); 2669 2670 MIB.addReg(DstElt) 2671 .addImm(SubIdx); 2672 } 2673 } 2674 2675 bool SIInstrInfo::isFoldableCopy(const MachineInstr &MI) const { 2676 switch (MI.getOpcode()) { 2677 case AMDGPU::V_MOV_B32_e32: 2678 case AMDGPU::V_MOV_B32_e64: 2679 case AMDGPU::V_MOV_B64_PSEUDO: { 2680 // If there are additional implicit register operands, this may be used for 2681 // register indexing so the source register operand isn't simply copied. 2682 unsigned NumOps = MI.getDesc().getNumOperands() + 2683 MI.getDesc().getNumImplicitUses(); 2684 2685 return MI.getNumOperands() == NumOps; 2686 } 2687 case AMDGPU::S_MOV_B32: 2688 case AMDGPU::S_MOV_B64: 2689 case AMDGPU::COPY: 2690 case AMDGPU::V_ACCVGPR_WRITE_B32_e64: 2691 case AMDGPU::V_ACCVGPR_READ_B32_e64: 2692 case AMDGPU::V_ACCVGPR_MOV_B32: 2693 return true; 2694 default: 2695 return false; 2696 } 2697 } 2698 2699 unsigned SIInstrInfo::getAddressSpaceForPseudoSourceKind( 2700 unsigned Kind) const { 2701 switch(Kind) { 2702 case PseudoSourceValue::Stack: 2703 case PseudoSourceValue::FixedStack: 2704 return AMDGPUAS::PRIVATE_ADDRESS; 2705 case PseudoSourceValue::ConstantPool: 2706 case PseudoSourceValue::GOT: 2707 case PseudoSourceValue::JumpTable: 2708 case PseudoSourceValue::GlobalValueCallEntry: 2709 case PseudoSourceValue::ExternalSymbolCallEntry: 2710 case PseudoSourceValue::TargetCustom: 2711 return AMDGPUAS::CONSTANT_ADDRESS; 2712 } 2713 return AMDGPUAS::FLAT_ADDRESS; 2714 } 2715 2716 static void removeModOperands(MachineInstr &MI) { 2717 unsigned Opc = MI.getOpcode(); 2718 int Src0ModIdx = AMDGPU::getNamedOperandIdx(Opc, 2719 AMDGPU::OpName::src0_modifiers); 2720 int Src1ModIdx = AMDGPU::getNamedOperandIdx(Opc, 2721 AMDGPU::OpName::src1_modifiers); 2722 int Src2ModIdx = AMDGPU::getNamedOperandIdx(Opc, 2723 AMDGPU::OpName::src2_modifiers); 2724 2725 MI.RemoveOperand(Src2ModIdx); 2726 MI.RemoveOperand(Src1ModIdx); 2727 MI.RemoveOperand(Src0ModIdx); 2728 } 2729 2730 bool SIInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 2731 Register Reg, MachineRegisterInfo *MRI) const { 2732 if (!MRI->hasOneNonDBGUse(Reg)) 2733 return false; 2734 2735 switch (DefMI.getOpcode()) { 2736 default: 2737 return false; 2738 case AMDGPU::S_MOV_B64: 2739 // TODO: We could fold 64-bit immediates, but this get compilicated 2740 // when there are sub-registers. 2741 return false; 2742 2743 case AMDGPU::V_MOV_B32_e32: 2744 case AMDGPU::S_MOV_B32: 2745 case AMDGPU::V_ACCVGPR_WRITE_B32_e64: 2746 break; 2747 } 2748 2749 const MachineOperand *ImmOp = getNamedOperand(DefMI, AMDGPU::OpName::src0); 2750 assert(ImmOp); 2751 // FIXME: We could handle FrameIndex values here. 2752 if (!ImmOp->isImm()) 2753 return false; 2754 2755 unsigned Opc = UseMI.getOpcode(); 2756 if (Opc == AMDGPU::COPY) { 2757 Register DstReg = UseMI.getOperand(0).getReg(); 2758 bool Is16Bit = getOpSize(UseMI, 0) == 2; 2759 bool isVGPRCopy = RI.isVGPR(*MRI, DstReg); 2760 unsigned NewOpc = isVGPRCopy ? AMDGPU::V_MOV_B32_e32 : AMDGPU::S_MOV_B32; 2761 APInt Imm(32, ImmOp->getImm()); 2762 2763 if (UseMI.getOperand(1).getSubReg() == AMDGPU::hi16) 2764 Imm = Imm.ashr(16); 2765 2766 if (RI.isAGPR(*MRI, DstReg)) { 2767 if (!isInlineConstant(Imm)) 2768 return false; 2769 NewOpc = AMDGPU::V_ACCVGPR_WRITE_B32_e64; 2770 } 2771 2772 if (Is16Bit) { 2773 if (isVGPRCopy) 2774 return false; // Do not clobber vgpr_hi16 2775 2776 if (DstReg.isVirtual() && 2777 UseMI.getOperand(0).getSubReg() != AMDGPU::lo16) 2778 return false; 2779 2780 UseMI.getOperand(0).setSubReg(0); 2781 if (DstReg.isPhysical()) { 2782 DstReg = RI.get32BitRegister(DstReg); 2783 UseMI.getOperand(0).setReg(DstReg); 2784 } 2785 assert(UseMI.getOperand(1).getReg().isVirtual()); 2786 } 2787 2788 UseMI.setDesc(get(NewOpc)); 2789 UseMI.getOperand(1).ChangeToImmediate(Imm.getSExtValue()); 2790 UseMI.addImplicitDefUseOperands(*UseMI.getParent()->getParent()); 2791 return true; 2792 } 2793 2794 if (Opc == AMDGPU::V_MAD_F32_e64 || Opc == AMDGPU::V_MAC_F32_e64 || 2795 Opc == AMDGPU::V_MAD_F16_e64 || Opc == AMDGPU::V_MAC_F16_e64 || 2796 Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64 || 2797 Opc == AMDGPU::V_FMA_F16_e64 || Opc == AMDGPU::V_FMAC_F16_e64) { 2798 // Don't fold if we are using source or output modifiers. The new VOP2 2799 // instructions don't have them. 2800 if (hasAnyModifiersSet(UseMI)) 2801 return false; 2802 2803 // If this is a free constant, there's no reason to do this. 2804 // TODO: We could fold this here instead of letting SIFoldOperands do it 2805 // later. 2806 MachineOperand *Src0 = getNamedOperand(UseMI, AMDGPU::OpName::src0); 2807 2808 // Any src operand can be used for the legality check. 2809 if (isInlineConstant(UseMI, *Src0, *ImmOp)) 2810 return false; 2811 2812 bool IsF32 = Opc == AMDGPU::V_MAD_F32_e64 || Opc == AMDGPU::V_MAC_F32_e64 || 2813 Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64; 2814 bool IsFMA = Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64 || 2815 Opc == AMDGPU::V_FMA_F16_e64 || Opc == AMDGPU::V_FMAC_F16_e64; 2816 MachineOperand *Src1 = getNamedOperand(UseMI, AMDGPU::OpName::src1); 2817 MachineOperand *Src2 = getNamedOperand(UseMI, AMDGPU::OpName::src2); 2818 2819 // Multiplied part is the constant: Use v_madmk_{f16, f32}. 2820 // We should only expect these to be on src0 due to canonicalizations. 2821 if (Src0->isReg() && Src0->getReg() == Reg) { 2822 if (!Src1->isReg() || RI.isSGPRClass(MRI->getRegClass(Src1->getReg()))) 2823 return false; 2824 2825 if (!Src2->isReg() || RI.isSGPRClass(MRI->getRegClass(Src2->getReg()))) 2826 return false; 2827 2828 unsigned NewOpc = 2829 IsFMA ? (IsF32 ? AMDGPU::V_FMAMK_F32 : AMDGPU::V_FMAMK_F16) 2830 : (IsF32 ? AMDGPU::V_MADMK_F32 : AMDGPU::V_MADMK_F16); 2831 if (pseudoToMCOpcode(NewOpc) == -1) 2832 return false; 2833 2834 // We need to swap operands 0 and 1 since madmk constant is at operand 1. 2835 2836 const int64_t Imm = ImmOp->getImm(); 2837 2838 // FIXME: This would be a lot easier if we could return a new instruction 2839 // instead of having to modify in place. 2840 2841 // Remove these first since they are at the end. 2842 UseMI.RemoveOperand( 2843 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::omod)); 2844 UseMI.RemoveOperand( 2845 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp)); 2846 2847 Register Src1Reg = Src1->getReg(); 2848 unsigned Src1SubReg = Src1->getSubReg(); 2849 Src0->setReg(Src1Reg); 2850 Src0->setSubReg(Src1SubReg); 2851 Src0->setIsKill(Src1->isKill()); 2852 2853 if (Opc == AMDGPU::V_MAC_F32_e64 || 2854 Opc == AMDGPU::V_MAC_F16_e64 || 2855 Opc == AMDGPU::V_FMAC_F32_e64 || 2856 Opc == AMDGPU::V_FMAC_F16_e64) 2857 UseMI.untieRegOperand( 2858 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)); 2859 2860 Src1->ChangeToImmediate(Imm); 2861 2862 removeModOperands(UseMI); 2863 UseMI.setDesc(get(NewOpc)); 2864 2865 bool DeleteDef = MRI->hasOneNonDBGUse(Reg); 2866 if (DeleteDef) 2867 DefMI.eraseFromParent(); 2868 2869 return true; 2870 } 2871 2872 // Added part is the constant: Use v_madak_{f16, f32}. 2873 if (Src2->isReg() && Src2->getReg() == Reg) { 2874 // Not allowed to use constant bus for another operand. 2875 // We can however allow an inline immediate as src0. 2876 bool Src0Inlined = false; 2877 if (Src0->isReg()) { 2878 // Try to inline constant if possible. 2879 // If the Def moves immediate and the use is single 2880 // We are saving VGPR here. 2881 MachineInstr *Def = MRI->getUniqueVRegDef(Src0->getReg()); 2882 if (Def && Def->isMoveImmediate() && 2883 isInlineConstant(Def->getOperand(1)) && 2884 MRI->hasOneUse(Src0->getReg())) { 2885 Src0->ChangeToImmediate(Def->getOperand(1).getImm()); 2886 Src0Inlined = true; 2887 } else if ((Src0->getReg().isPhysical() && 2888 (ST.getConstantBusLimit(Opc) <= 1 && 2889 RI.isSGPRClass(RI.getPhysRegClass(Src0->getReg())))) || 2890 (Src0->getReg().isVirtual() && 2891 (ST.getConstantBusLimit(Opc) <= 1 && 2892 RI.isSGPRClass(MRI->getRegClass(Src0->getReg()))))) 2893 return false; 2894 // VGPR is okay as Src0 - fallthrough 2895 } 2896 2897 if (Src1->isReg() && !Src0Inlined ) { 2898 // We have one slot for inlinable constant so far - try to fill it 2899 MachineInstr *Def = MRI->getUniqueVRegDef(Src1->getReg()); 2900 if (Def && Def->isMoveImmediate() && 2901 isInlineConstant(Def->getOperand(1)) && 2902 MRI->hasOneUse(Src1->getReg()) && 2903 commuteInstruction(UseMI)) { 2904 Src0->ChangeToImmediate(Def->getOperand(1).getImm()); 2905 } else if ((Src1->getReg().isPhysical() && 2906 RI.isSGPRClass(RI.getPhysRegClass(Src1->getReg()))) || 2907 (Src1->getReg().isVirtual() && 2908 RI.isSGPRClass(MRI->getRegClass(Src1->getReg())))) 2909 return false; 2910 // VGPR is okay as Src1 - fallthrough 2911 } 2912 2913 unsigned NewOpc = 2914 IsFMA ? (IsF32 ? AMDGPU::V_FMAAK_F32 : AMDGPU::V_FMAAK_F16) 2915 : (IsF32 ? AMDGPU::V_MADAK_F32 : AMDGPU::V_MADAK_F16); 2916 if (pseudoToMCOpcode(NewOpc) == -1) 2917 return false; 2918 2919 const int64_t Imm = ImmOp->getImm(); 2920 2921 // FIXME: This would be a lot easier if we could return a new instruction 2922 // instead of having to modify in place. 2923 2924 // Remove these first since they are at the end. 2925 UseMI.RemoveOperand( 2926 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::omod)); 2927 UseMI.RemoveOperand( 2928 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp)); 2929 2930 if (Opc == AMDGPU::V_MAC_F32_e64 || 2931 Opc == AMDGPU::V_MAC_F16_e64 || 2932 Opc == AMDGPU::V_FMAC_F32_e64 || 2933 Opc == AMDGPU::V_FMAC_F16_e64) 2934 UseMI.untieRegOperand( 2935 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)); 2936 2937 // ChangingToImmediate adds Src2 back to the instruction. 2938 Src2->ChangeToImmediate(Imm); 2939 2940 // These come before src2. 2941 removeModOperands(UseMI); 2942 UseMI.setDesc(get(NewOpc)); 2943 // It might happen that UseMI was commuted 2944 // and we now have SGPR as SRC1. If so 2 inlined 2945 // constant and SGPR are illegal. 2946 legalizeOperands(UseMI); 2947 2948 bool DeleteDef = MRI->hasOneNonDBGUse(Reg); 2949 if (DeleteDef) 2950 DefMI.eraseFromParent(); 2951 2952 return true; 2953 } 2954 } 2955 2956 return false; 2957 } 2958 2959 static bool 2960 memOpsHaveSameBaseOperands(ArrayRef<const MachineOperand *> BaseOps1, 2961 ArrayRef<const MachineOperand *> BaseOps2) { 2962 if (BaseOps1.size() != BaseOps2.size()) 2963 return false; 2964 for (size_t I = 0, E = BaseOps1.size(); I < E; ++I) { 2965 if (!BaseOps1[I]->isIdenticalTo(*BaseOps2[I])) 2966 return false; 2967 } 2968 return true; 2969 } 2970 2971 static bool offsetsDoNotOverlap(int WidthA, int OffsetA, 2972 int WidthB, int OffsetB) { 2973 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB; 2974 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA; 2975 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; 2976 return LowOffset + LowWidth <= HighOffset; 2977 } 2978 2979 bool SIInstrInfo::checkInstOffsetsDoNotOverlap(const MachineInstr &MIa, 2980 const MachineInstr &MIb) const { 2981 SmallVector<const MachineOperand *, 4> BaseOps0, BaseOps1; 2982 int64_t Offset0, Offset1; 2983 unsigned Dummy0, Dummy1; 2984 bool Offset0IsScalable, Offset1IsScalable; 2985 if (!getMemOperandsWithOffsetWidth(MIa, BaseOps0, Offset0, Offset0IsScalable, 2986 Dummy0, &RI) || 2987 !getMemOperandsWithOffsetWidth(MIb, BaseOps1, Offset1, Offset1IsScalable, 2988 Dummy1, &RI)) 2989 return false; 2990 2991 if (!memOpsHaveSameBaseOperands(BaseOps0, BaseOps1)) 2992 return false; 2993 2994 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand()) { 2995 // FIXME: Handle ds_read2 / ds_write2. 2996 return false; 2997 } 2998 unsigned Width0 = MIa.memoperands().front()->getSize(); 2999 unsigned Width1 = MIb.memoperands().front()->getSize(); 3000 return offsetsDoNotOverlap(Width0, Offset0, Width1, Offset1); 3001 } 3002 3003 bool SIInstrInfo::areMemAccessesTriviallyDisjoint(const MachineInstr &MIa, 3004 const MachineInstr &MIb) const { 3005 assert(MIa.mayLoadOrStore() && 3006 "MIa must load from or modify a memory location"); 3007 assert(MIb.mayLoadOrStore() && 3008 "MIb must load from or modify a memory location"); 3009 3010 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects()) 3011 return false; 3012 3013 // XXX - Can we relax this between address spaces? 3014 if (MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef()) 3015 return false; 3016 3017 // TODO: Should we check the address space from the MachineMemOperand? That 3018 // would allow us to distinguish objects we know don't alias based on the 3019 // underlying address space, even if it was lowered to a different one, 3020 // e.g. private accesses lowered to use MUBUF instructions on a scratch 3021 // buffer. 3022 if (isDS(MIa)) { 3023 if (isDS(MIb)) 3024 return checkInstOffsetsDoNotOverlap(MIa, MIb); 3025 3026 return !isFLAT(MIb) || isSegmentSpecificFLAT(MIb); 3027 } 3028 3029 if (isMUBUF(MIa) || isMTBUF(MIa)) { 3030 if (isMUBUF(MIb) || isMTBUF(MIb)) 3031 return checkInstOffsetsDoNotOverlap(MIa, MIb); 3032 3033 return !isFLAT(MIb) && !isSMRD(MIb); 3034 } 3035 3036 if (isSMRD(MIa)) { 3037 if (isSMRD(MIb)) 3038 return checkInstOffsetsDoNotOverlap(MIa, MIb); 3039 3040 return !isFLAT(MIb) && !isMUBUF(MIb) && !isMTBUF(MIb); 3041 } 3042 3043 if (isFLAT(MIa)) { 3044 if (isFLAT(MIb)) 3045 return checkInstOffsetsDoNotOverlap(MIa, MIb); 3046 3047 return false; 3048 } 3049 3050 return false; 3051 } 3052 3053 static int64_t getFoldableImm(const MachineOperand* MO) { 3054 if (!MO->isReg()) 3055 return false; 3056 const MachineFunction *MF = MO->getParent()->getParent()->getParent(); 3057 const MachineRegisterInfo &MRI = MF->getRegInfo(); 3058 auto Def = MRI.getUniqueVRegDef(MO->getReg()); 3059 if (Def && Def->getOpcode() == AMDGPU::V_MOV_B32_e32 && 3060 Def->getOperand(1).isImm()) 3061 return Def->getOperand(1).getImm(); 3062 return AMDGPU::NoRegister; 3063 } 3064 3065 static void updateLiveVariables(LiveVariables *LV, MachineInstr &MI, 3066 MachineInstr &NewMI) { 3067 if (LV) { 3068 unsigned NumOps = MI.getNumOperands(); 3069 for (unsigned I = 1; I < NumOps; ++I) { 3070 MachineOperand &Op = MI.getOperand(I); 3071 if (Op.isReg() && Op.isKill()) 3072 LV->replaceKillInstruction(Op.getReg(), MI, NewMI); 3073 } 3074 } 3075 } 3076 3077 MachineInstr *SIInstrInfo::convertToThreeAddress(MachineFunction::iterator &MBB, 3078 MachineInstr &MI, 3079 LiveVariables *LV) const { 3080 unsigned Opc = MI.getOpcode(); 3081 bool IsF16 = false; 3082 bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e32 || Opc == AMDGPU::V_FMAC_F32_e64 || 3083 Opc == AMDGPU::V_FMAC_F16_e32 || Opc == AMDGPU::V_FMAC_F16_e64 || 3084 Opc == AMDGPU::V_FMAC_F64_e32 || Opc == AMDGPU::V_FMAC_F64_e64; 3085 bool IsF64 = Opc == AMDGPU::V_FMAC_F64_e32 || Opc == AMDGPU::V_FMAC_F64_e64; 3086 3087 switch (Opc) { 3088 default: 3089 return nullptr; 3090 case AMDGPU::V_MAC_F16_e64: 3091 case AMDGPU::V_FMAC_F16_e64: 3092 IsF16 = true; 3093 LLVM_FALLTHROUGH; 3094 case AMDGPU::V_MAC_F32_e64: 3095 case AMDGPU::V_FMAC_F32_e64: 3096 case AMDGPU::V_FMAC_F64_e64: 3097 break; 3098 case AMDGPU::V_MAC_F16_e32: 3099 case AMDGPU::V_FMAC_F16_e32: 3100 IsF16 = true; 3101 LLVM_FALLTHROUGH; 3102 case AMDGPU::V_MAC_F32_e32: 3103 case AMDGPU::V_FMAC_F32_e32: 3104 case AMDGPU::V_FMAC_F64_e32: { 3105 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), 3106 AMDGPU::OpName::src0); 3107 const MachineOperand *Src0 = &MI.getOperand(Src0Idx); 3108 if (!Src0->isReg() && !Src0->isImm()) 3109 return nullptr; 3110 3111 if (Src0->isImm() && !isInlineConstant(MI, Src0Idx, *Src0)) 3112 return nullptr; 3113 3114 break; 3115 } 3116 } 3117 3118 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 3119 const MachineOperand *Src0 = getNamedOperand(MI, AMDGPU::OpName::src0); 3120 const MachineOperand *Src0Mods = 3121 getNamedOperand(MI, AMDGPU::OpName::src0_modifiers); 3122 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1); 3123 const MachineOperand *Src1Mods = 3124 getNamedOperand(MI, AMDGPU::OpName::src1_modifiers); 3125 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2); 3126 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp); 3127 const MachineOperand *Omod = getNamedOperand(MI, AMDGPU::OpName::omod); 3128 MachineInstrBuilder MIB; 3129 3130 if (!Src0Mods && !Src1Mods && !Clamp && !Omod && !IsF64 && 3131 // If we have an SGPR input, we will violate the constant bus restriction. 3132 (ST.getConstantBusLimit(Opc) > 1 || !Src0->isReg() || 3133 !RI.isSGPRReg(MBB->getParent()->getRegInfo(), Src0->getReg()))) { 3134 if (auto Imm = getFoldableImm(Src2)) { 3135 unsigned NewOpc = 3136 IsFMA ? (IsF16 ? AMDGPU::V_FMAAK_F16 : AMDGPU::V_FMAAK_F32) 3137 : (IsF16 ? AMDGPU::V_MADAK_F16 : AMDGPU::V_MADAK_F32); 3138 if (pseudoToMCOpcode(NewOpc) != -1) { 3139 MIB = BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc)) 3140 .add(*Dst) 3141 .add(*Src0) 3142 .add(*Src1) 3143 .addImm(Imm); 3144 updateLiveVariables(LV, MI, *MIB); 3145 return MIB; 3146 } 3147 } 3148 unsigned NewOpc = IsFMA 3149 ? (IsF16 ? AMDGPU::V_FMAMK_F16 : AMDGPU::V_FMAMK_F32) 3150 : (IsF16 ? AMDGPU::V_MADMK_F16 : AMDGPU::V_MADMK_F32); 3151 if (auto Imm = getFoldableImm(Src1)) { 3152 if (pseudoToMCOpcode(NewOpc) != -1) { 3153 MIB = BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc)) 3154 .add(*Dst) 3155 .add(*Src0) 3156 .addImm(Imm) 3157 .add(*Src2); 3158 updateLiveVariables(LV, MI, *MIB); 3159 return MIB; 3160 } 3161 } 3162 if (auto Imm = getFoldableImm(Src0)) { 3163 if (pseudoToMCOpcode(NewOpc) != -1 && 3164 isOperandLegal( 3165 MI, AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::src0), 3166 Src1)) { 3167 MIB = BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc)) 3168 .add(*Dst) 3169 .add(*Src1) 3170 .addImm(Imm) 3171 .add(*Src2); 3172 updateLiveVariables(LV, MI, *MIB); 3173 return MIB; 3174 } 3175 } 3176 } 3177 3178 unsigned NewOpc = IsFMA ? (IsF16 ? AMDGPU::V_FMA_F16_e64 3179 : IsF64 ? AMDGPU::V_FMA_F64_e64 3180 : AMDGPU::V_FMA_F32_e64) 3181 : (IsF16 ? AMDGPU::V_MAD_F16_e64 : AMDGPU::V_MAD_F32_e64); 3182 if (pseudoToMCOpcode(NewOpc) == -1) 3183 return nullptr; 3184 3185 MIB = BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc)) 3186 .add(*Dst) 3187 .addImm(Src0Mods ? Src0Mods->getImm() : 0) 3188 .add(*Src0) 3189 .addImm(Src1Mods ? Src1Mods->getImm() : 0) 3190 .add(*Src1) 3191 .addImm(0) // Src mods 3192 .add(*Src2) 3193 .addImm(Clamp ? Clamp->getImm() : 0) 3194 .addImm(Omod ? Omod->getImm() : 0); 3195 updateLiveVariables(LV, MI, *MIB); 3196 return MIB; 3197 } 3198 3199 // It's not generally safe to move VALU instructions across these since it will 3200 // start using the register as a base index rather than directly. 3201 // XXX - Why isn't hasSideEffects sufficient for these? 3202 static bool changesVGPRIndexingMode(const MachineInstr &MI) { 3203 switch (MI.getOpcode()) { 3204 case AMDGPU::S_SET_GPR_IDX_ON: 3205 case AMDGPU::S_SET_GPR_IDX_MODE: 3206 case AMDGPU::S_SET_GPR_IDX_OFF: 3207 return true; 3208 default: 3209 return false; 3210 } 3211 } 3212 3213 bool SIInstrInfo::isSchedulingBoundary(const MachineInstr &MI, 3214 const MachineBasicBlock *MBB, 3215 const MachineFunction &MF) const { 3216 // Skipping the check for SP writes in the base implementation. The reason it 3217 // was added was apparently due to compile time concerns. 3218 // 3219 // TODO: Do we really want this barrier? It triggers unnecessary hazard nops 3220 // but is probably avoidable. 3221 3222 // Copied from base implementation. 3223 // Terminators and labels can't be scheduled around. 3224 if (MI.isTerminator() || MI.isPosition()) 3225 return true; 3226 3227 // INLINEASM_BR can jump to another block 3228 if (MI.getOpcode() == TargetOpcode::INLINEASM_BR) 3229 return true; 3230 3231 // Target-independent instructions do not have an implicit-use of EXEC, even 3232 // when they operate on VGPRs. Treating EXEC modifications as scheduling 3233 // boundaries prevents incorrect movements of such instructions. 3234 return MI.modifiesRegister(AMDGPU::EXEC, &RI) || 3235 MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 || 3236 MI.getOpcode() == AMDGPU::S_SETREG_B32 || 3237 changesVGPRIndexingMode(MI); 3238 } 3239 3240 bool SIInstrInfo::isAlwaysGDS(uint16_t Opcode) const { 3241 return Opcode == AMDGPU::DS_ORDERED_COUNT || 3242 Opcode == AMDGPU::DS_GWS_INIT || 3243 Opcode == AMDGPU::DS_GWS_SEMA_V || 3244 Opcode == AMDGPU::DS_GWS_SEMA_BR || 3245 Opcode == AMDGPU::DS_GWS_SEMA_P || 3246 Opcode == AMDGPU::DS_GWS_SEMA_RELEASE_ALL || 3247 Opcode == AMDGPU::DS_GWS_BARRIER; 3248 } 3249 3250 bool SIInstrInfo::modifiesModeRegister(const MachineInstr &MI) { 3251 // Skip the full operand and register alias search modifiesRegister 3252 // does. There's only a handful of instructions that touch this, it's only an 3253 // implicit def, and doesn't alias any other registers. 3254 if (const MCPhysReg *ImpDef = MI.getDesc().getImplicitDefs()) { 3255 for (; ImpDef && *ImpDef; ++ImpDef) { 3256 if (*ImpDef == AMDGPU::MODE) 3257 return true; 3258 } 3259 } 3260 3261 return false; 3262 } 3263 3264 bool SIInstrInfo::hasUnwantedEffectsWhenEXECEmpty(const MachineInstr &MI) const { 3265 unsigned Opcode = MI.getOpcode(); 3266 3267 if (MI.mayStore() && isSMRD(MI)) 3268 return true; // scalar store or atomic 3269 3270 // This will terminate the function when other lanes may need to continue. 3271 if (MI.isReturn()) 3272 return true; 3273 3274 // These instructions cause shader I/O that may cause hardware lockups 3275 // when executed with an empty EXEC mask. 3276 // 3277 // Note: exp with VM = DONE = 0 is automatically skipped by hardware when 3278 // EXEC = 0, but checking for that case here seems not worth it 3279 // given the typical code patterns. 3280 if (Opcode == AMDGPU::S_SENDMSG || Opcode == AMDGPU::S_SENDMSGHALT || 3281 isEXP(Opcode) || 3282 Opcode == AMDGPU::DS_ORDERED_COUNT || Opcode == AMDGPU::S_TRAP || 3283 Opcode == AMDGPU::DS_GWS_INIT || Opcode == AMDGPU::DS_GWS_BARRIER) 3284 return true; 3285 3286 if (MI.isCall() || MI.isInlineAsm()) 3287 return true; // conservative assumption 3288 3289 // A mode change is a scalar operation that influences vector instructions. 3290 if (modifiesModeRegister(MI)) 3291 return true; 3292 3293 // These are like SALU instructions in terms of effects, so it's questionable 3294 // whether we should return true for those. 3295 // 3296 // However, executing them with EXEC = 0 causes them to operate on undefined 3297 // data, which we avoid by returning true here. 3298 if (Opcode == AMDGPU::V_READFIRSTLANE_B32 || 3299 Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32) 3300 return true; 3301 3302 return false; 3303 } 3304 3305 bool SIInstrInfo::mayReadEXEC(const MachineRegisterInfo &MRI, 3306 const MachineInstr &MI) const { 3307 if (MI.isMetaInstruction()) 3308 return false; 3309 3310 // This won't read exec if this is an SGPR->SGPR copy. 3311 if (MI.isCopyLike()) { 3312 if (!RI.isSGPRReg(MRI, MI.getOperand(0).getReg())) 3313 return true; 3314 3315 // Make sure this isn't copying exec as a normal operand 3316 return MI.readsRegister(AMDGPU::EXEC, &RI); 3317 } 3318 3319 // Make a conservative assumption about the callee. 3320 if (MI.isCall()) 3321 return true; 3322 3323 // Be conservative with any unhandled generic opcodes. 3324 if (!isTargetSpecificOpcode(MI.getOpcode())) 3325 return true; 3326 3327 return !isSALU(MI) || MI.readsRegister(AMDGPU::EXEC, &RI); 3328 } 3329 3330 bool SIInstrInfo::isInlineConstant(const APInt &Imm) const { 3331 switch (Imm.getBitWidth()) { 3332 case 1: // This likely will be a condition code mask. 3333 return true; 3334 3335 case 32: 3336 return AMDGPU::isInlinableLiteral32(Imm.getSExtValue(), 3337 ST.hasInv2PiInlineImm()); 3338 case 64: 3339 return AMDGPU::isInlinableLiteral64(Imm.getSExtValue(), 3340 ST.hasInv2PiInlineImm()); 3341 case 16: 3342 return ST.has16BitInsts() && 3343 AMDGPU::isInlinableLiteral16(Imm.getSExtValue(), 3344 ST.hasInv2PiInlineImm()); 3345 default: 3346 llvm_unreachable("invalid bitwidth"); 3347 } 3348 } 3349 3350 bool SIInstrInfo::isInlineConstant(const MachineOperand &MO, 3351 uint8_t OperandType) const { 3352 if (!MO.isImm() || 3353 OperandType < AMDGPU::OPERAND_SRC_FIRST || 3354 OperandType > AMDGPU::OPERAND_SRC_LAST) 3355 return false; 3356 3357 // MachineOperand provides no way to tell the true operand size, since it only 3358 // records a 64-bit value. We need to know the size to determine if a 32-bit 3359 // floating point immediate bit pattern is legal for an integer immediate. It 3360 // would be for any 32-bit integer operand, but would not be for a 64-bit one. 3361 3362 int64_t Imm = MO.getImm(); 3363 switch (OperandType) { 3364 case AMDGPU::OPERAND_REG_IMM_INT32: 3365 case AMDGPU::OPERAND_REG_IMM_FP32: 3366 case AMDGPU::OPERAND_REG_INLINE_C_INT32: 3367 case AMDGPU::OPERAND_REG_INLINE_C_FP32: 3368 case AMDGPU::OPERAND_REG_IMM_V2FP32: 3369 case AMDGPU::OPERAND_REG_INLINE_C_V2FP32: 3370 case AMDGPU::OPERAND_REG_IMM_V2INT32: 3371 case AMDGPU::OPERAND_REG_INLINE_C_V2INT32: 3372 case AMDGPU::OPERAND_REG_INLINE_AC_INT32: 3373 case AMDGPU::OPERAND_REG_INLINE_AC_FP32: { 3374 int32_t Trunc = static_cast<int32_t>(Imm); 3375 return AMDGPU::isInlinableLiteral32(Trunc, ST.hasInv2PiInlineImm()); 3376 } 3377 case AMDGPU::OPERAND_REG_IMM_INT64: 3378 case AMDGPU::OPERAND_REG_IMM_FP64: 3379 case AMDGPU::OPERAND_REG_INLINE_C_INT64: 3380 case AMDGPU::OPERAND_REG_INLINE_C_FP64: 3381 case AMDGPU::OPERAND_REG_INLINE_AC_FP64: 3382 return AMDGPU::isInlinableLiteral64(MO.getImm(), 3383 ST.hasInv2PiInlineImm()); 3384 case AMDGPU::OPERAND_REG_IMM_INT16: 3385 case AMDGPU::OPERAND_REG_INLINE_C_INT16: 3386 case AMDGPU::OPERAND_REG_INLINE_AC_INT16: 3387 // We would expect inline immediates to not be concerned with an integer/fp 3388 // distinction. However, in the case of 16-bit integer operations, the 3389 // "floating point" values appear to not work. It seems read the low 16-bits 3390 // of 32-bit immediates, which happens to always work for the integer 3391 // values. 3392 // 3393 // See llvm bugzilla 46302. 3394 // 3395 // TODO: Theoretically we could use op-sel to use the high bits of the 3396 // 32-bit FP values. 3397 return AMDGPU::isInlinableIntLiteral(Imm); 3398 case AMDGPU::OPERAND_REG_IMM_V2INT16: 3399 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16: 3400 case AMDGPU::OPERAND_REG_INLINE_AC_V2INT16: 3401 // This suffers the same problem as the scalar 16-bit cases. 3402 return AMDGPU::isInlinableIntLiteralV216(Imm); 3403 case AMDGPU::OPERAND_REG_IMM_FP16: 3404 case AMDGPU::OPERAND_REG_INLINE_C_FP16: 3405 case AMDGPU::OPERAND_REG_INLINE_AC_FP16: { 3406 if (isInt<16>(Imm) || isUInt<16>(Imm)) { 3407 // A few special case instructions have 16-bit operands on subtargets 3408 // where 16-bit instructions are not legal. 3409 // TODO: Do the 32-bit immediates work? We shouldn't really need to handle 3410 // constants in these cases 3411 int16_t Trunc = static_cast<int16_t>(Imm); 3412 return ST.has16BitInsts() && 3413 AMDGPU::isInlinableLiteral16(Trunc, ST.hasInv2PiInlineImm()); 3414 } 3415 3416 return false; 3417 } 3418 case AMDGPU::OPERAND_REG_IMM_V2FP16: 3419 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16: 3420 case AMDGPU::OPERAND_REG_INLINE_AC_V2FP16: { 3421 uint32_t Trunc = static_cast<uint32_t>(Imm); 3422 return AMDGPU::isInlinableLiteralV216(Trunc, ST.hasInv2PiInlineImm()); 3423 } 3424 default: 3425 llvm_unreachable("invalid bitwidth"); 3426 } 3427 } 3428 3429 bool SIInstrInfo::isLiteralConstantLike(const MachineOperand &MO, 3430 const MCOperandInfo &OpInfo) const { 3431 switch (MO.getType()) { 3432 case MachineOperand::MO_Register: 3433 return false; 3434 case MachineOperand::MO_Immediate: 3435 return !isInlineConstant(MO, OpInfo); 3436 case MachineOperand::MO_FrameIndex: 3437 case MachineOperand::MO_MachineBasicBlock: 3438 case MachineOperand::MO_ExternalSymbol: 3439 case MachineOperand::MO_GlobalAddress: 3440 case MachineOperand::MO_MCSymbol: 3441 return true; 3442 default: 3443 llvm_unreachable("unexpected operand type"); 3444 } 3445 } 3446 3447 static bool compareMachineOp(const MachineOperand &Op0, 3448 const MachineOperand &Op1) { 3449 if (Op0.getType() != Op1.getType()) 3450 return false; 3451 3452 switch (Op0.getType()) { 3453 case MachineOperand::MO_Register: 3454 return Op0.getReg() == Op1.getReg(); 3455 case MachineOperand::MO_Immediate: 3456 return Op0.getImm() == Op1.getImm(); 3457 default: 3458 llvm_unreachable("Didn't expect to be comparing these operand types"); 3459 } 3460 } 3461 3462 bool SIInstrInfo::isImmOperandLegal(const MachineInstr &MI, unsigned OpNo, 3463 const MachineOperand &MO) const { 3464 const MCInstrDesc &InstDesc = MI.getDesc(); 3465 const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpNo]; 3466 3467 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal()); 3468 3469 if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE) 3470 return true; 3471 3472 if (OpInfo.RegClass < 0) 3473 return false; 3474 3475 if (MO.isImm() && isInlineConstant(MO, OpInfo)) { 3476 if (isMAI(MI) && ST.hasMFMAInlineLiteralBug() && 3477 OpNo ==(unsigned)AMDGPU::getNamedOperandIdx(MI.getOpcode(), 3478 AMDGPU::OpName::src2)) 3479 return false; 3480 return RI.opCanUseInlineConstant(OpInfo.OperandType); 3481 } 3482 3483 if (!RI.opCanUseLiteralConstant(OpInfo.OperandType)) 3484 return false; 3485 3486 if (!isVOP3(MI) || !AMDGPU::isSISrcOperand(InstDesc, OpNo)) 3487 return true; 3488 3489 return ST.hasVOP3Literal(); 3490 } 3491 3492 bool SIInstrInfo::hasVALU32BitEncoding(unsigned Opcode) const { 3493 // GFX90A does not have V_MUL_LEGACY_F32_e32. 3494 if (Opcode == AMDGPU::V_MUL_LEGACY_F32_e64 && ST.hasGFX90AInsts()) 3495 return false; 3496 3497 int Op32 = AMDGPU::getVOPe32(Opcode); 3498 if (Op32 == -1) 3499 return false; 3500 3501 return pseudoToMCOpcode(Op32) != -1; 3502 } 3503 3504 bool SIInstrInfo::hasModifiers(unsigned Opcode) const { 3505 // The src0_modifier operand is present on all instructions 3506 // that have modifiers. 3507 3508 return AMDGPU::getNamedOperandIdx(Opcode, 3509 AMDGPU::OpName::src0_modifiers) != -1; 3510 } 3511 3512 bool SIInstrInfo::hasModifiersSet(const MachineInstr &MI, 3513 unsigned OpName) const { 3514 const MachineOperand *Mods = getNamedOperand(MI, OpName); 3515 return Mods && Mods->getImm(); 3516 } 3517 3518 bool SIInstrInfo::hasAnyModifiersSet(const MachineInstr &MI) const { 3519 return hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) || 3520 hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) || 3521 hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers) || 3522 hasModifiersSet(MI, AMDGPU::OpName::clamp) || 3523 hasModifiersSet(MI, AMDGPU::OpName::omod); 3524 } 3525 3526 bool SIInstrInfo::canShrink(const MachineInstr &MI, 3527 const MachineRegisterInfo &MRI) const { 3528 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2); 3529 // Can't shrink instruction with three operands. 3530 // FIXME: v_cndmask_b32 has 3 operands and is shrinkable, but we need to add 3531 // a special case for it. It can only be shrunk if the third operand 3532 // is vcc, and src0_modifiers and src1_modifiers are not set. 3533 // We should handle this the same way we handle vopc, by addding 3534 // a register allocation hint pre-regalloc and then do the shrinking 3535 // post-regalloc. 3536 if (Src2) { 3537 switch (MI.getOpcode()) { 3538 default: return false; 3539 3540 case AMDGPU::V_ADDC_U32_e64: 3541 case AMDGPU::V_SUBB_U32_e64: 3542 case AMDGPU::V_SUBBREV_U32_e64: { 3543 const MachineOperand *Src1 3544 = getNamedOperand(MI, AMDGPU::OpName::src1); 3545 if (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg())) 3546 return false; 3547 // Additional verification is needed for sdst/src2. 3548 return true; 3549 } 3550 case AMDGPU::V_MAC_F32_e64: 3551 case AMDGPU::V_MAC_F16_e64: 3552 case AMDGPU::V_FMAC_F32_e64: 3553 case AMDGPU::V_FMAC_F16_e64: 3554 case AMDGPU::V_FMAC_F64_e64: 3555 if (!Src2->isReg() || !RI.isVGPR(MRI, Src2->getReg()) || 3556 hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers)) 3557 return false; 3558 break; 3559 3560 case AMDGPU::V_CNDMASK_B32_e64: 3561 break; 3562 } 3563 } 3564 3565 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1); 3566 if (Src1 && (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg()) || 3567 hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers))) 3568 return false; 3569 3570 // We don't need to check src0, all input types are legal, so just make sure 3571 // src0 isn't using any modifiers. 3572 if (hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers)) 3573 return false; 3574 3575 // Can it be shrunk to a valid 32 bit opcode? 3576 if (!hasVALU32BitEncoding(MI.getOpcode())) 3577 return false; 3578 3579 // Check output modifiers 3580 return !hasModifiersSet(MI, AMDGPU::OpName::omod) && 3581 !hasModifiersSet(MI, AMDGPU::OpName::clamp); 3582 } 3583 3584 // Set VCC operand with all flags from \p Orig, except for setting it as 3585 // implicit. 3586 static void copyFlagsToImplicitVCC(MachineInstr &MI, 3587 const MachineOperand &Orig) { 3588 3589 for (MachineOperand &Use : MI.implicit_operands()) { 3590 if (Use.isUse() && 3591 (Use.getReg() == AMDGPU::VCC || Use.getReg() == AMDGPU::VCC_LO)) { 3592 Use.setIsUndef(Orig.isUndef()); 3593 Use.setIsKill(Orig.isKill()); 3594 return; 3595 } 3596 } 3597 } 3598 3599 MachineInstr *SIInstrInfo::buildShrunkInst(MachineInstr &MI, 3600 unsigned Op32) const { 3601 MachineBasicBlock *MBB = MI.getParent();; 3602 MachineInstrBuilder Inst32 = 3603 BuildMI(*MBB, MI, MI.getDebugLoc(), get(Op32)) 3604 .setMIFlags(MI.getFlags()); 3605 3606 // Add the dst operand if the 32-bit encoding also has an explicit $vdst. 3607 // For VOPC instructions, this is replaced by an implicit def of vcc. 3608 int Op32DstIdx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::vdst); 3609 if (Op32DstIdx != -1) { 3610 // dst 3611 Inst32.add(MI.getOperand(0)); 3612 } else { 3613 assert(((MI.getOperand(0).getReg() == AMDGPU::VCC) || 3614 (MI.getOperand(0).getReg() == AMDGPU::VCC_LO)) && 3615 "Unexpected case"); 3616 } 3617 3618 Inst32.add(*getNamedOperand(MI, AMDGPU::OpName::src0)); 3619 3620 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1); 3621 if (Src1) 3622 Inst32.add(*Src1); 3623 3624 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2); 3625 3626 if (Src2) { 3627 int Op32Src2Idx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2); 3628 if (Op32Src2Idx != -1) { 3629 Inst32.add(*Src2); 3630 } else { 3631 // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is 3632 // replaced with an implicit read of vcc or vcc_lo. The implicit read 3633 // of vcc was already added during the initial BuildMI, but we 3634 // 1) may need to change vcc to vcc_lo to preserve the original register 3635 // 2) have to preserve the original flags. 3636 fixImplicitOperands(*Inst32); 3637 copyFlagsToImplicitVCC(*Inst32, *Src2); 3638 } 3639 } 3640 3641 return Inst32; 3642 } 3643 3644 bool SIInstrInfo::usesConstantBus(const MachineRegisterInfo &MRI, 3645 const MachineOperand &MO, 3646 const MCOperandInfo &OpInfo) const { 3647 // Literal constants use the constant bus. 3648 //if (isLiteralConstantLike(MO, OpInfo)) 3649 // return true; 3650 if (MO.isImm()) 3651 return !isInlineConstant(MO, OpInfo); 3652 3653 if (!MO.isReg()) 3654 return true; // Misc other operands like FrameIndex 3655 3656 if (!MO.isUse()) 3657 return false; 3658 3659 if (MO.getReg().isVirtual()) 3660 return RI.isSGPRClass(MRI.getRegClass(MO.getReg())); 3661 3662 // Null is free 3663 if (MO.getReg() == AMDGPU::SGPR_NULL) 3664 return false; 3665 3666 // SGPRs use the constant bus 3667 if (MO.isImplicit()) { 3668 return MO.getReg() == AMDGPU::M0 || 3669 MO.getReg() == AMDGPU::VCC || 3670 MO.getReg() == AMDGPU::VCC_LO; 3671 } else { 3672 return AMDGPU::SReg_32RegClass.contains(MO.getReg()) || 3673 AMDGPU::SReg_64RegClass.contains(MO.getReg()); 3674 } 3675 } 3676 3677 static Register findImplicitSGPRRead(const MachineInstr &MI) { 3678 for (const MachineOperand &MO : MI.implicit_operands()) { 3679 // We only care about reads. 3680 if (MO.isDef()) 3681 continue; 3682 3683 switch (MO.getReg()) { 3684 case AMDGPU::VCC: 3685 case AMDGPU::VCC_LO: 3686 case AMDGPU::VCC_HI: 3687 case AMDGPU::M0: 3688 case AMDGPU::FLAT_SCR: 3689 return MO.getReg(); 3690 3691 default: 3692 break; 3693 } 3694 } 3695 3696 return AMDGPU::NoRegister; 3697 } 3698 3699 static bool shouldReadExec(const MachineInstr &MI) { 3700 if (SIInstrInfo::isVALU(MI)) { 3701 switch (MI.getOpcode()) { 3702 case AMDGPU::V_READLANE_B32: 3703 case AMDGPU::V_WRITELANE_B32: 3704 return false; 3705 } 3706 3707 return true; 3708 } 3709 3710 if (MI.isPreISelOpcode() || 3711 SIInstrInfo::isGenericOpcode(MI.getOpcode()) || 3712 SIInstrInfo::isSALU(MI) || 3713 SIInstrInfo::isSMRD(MI)) 3714 return false; 3715 3716 return true; 3717 } 3718 3719 static bool isSubRegOf(const SIRegisterInfo &TRI, 3720 const MachineOperand &SuperVec, 3721 const MachineOperand &SubReg) { 3722 if (SubReg.getReg().isPhysical()) 3723 return TRI.isSubRegister(SuperVec.getReg(), SubReg.getReg()); 3724 3725 return SubReg.getSubReg() != AMDGPU::NoSubRegister && 3726 SubReg.getReg() == SuperVec.getReg(); 3727 } 3728 3729 bool SIInstrInfo::verifyInstruction(const MachineInstr &MI, 3730 StringRef &ErrInfo) const { 3731 uint16_t Opcode = MI.getOpcode(); 3732 if (SIInstrInfo::isGenericOpcode(MI.getOpcode())) 3733 return true; 3734 3735 const MachineFunction *MF = MI.getParent()->getParent(); 3736 const MachineRegisterInfo &MRI = MF->getRegInfo(); 3737 3738 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0); 3739 int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1); 3740 int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2); 3741 3742 // Make sure the number of operands is correct. 3743 const MCInstrDesc &Desc = get(Opcode); 3744 if (!Desc.isVariadic() && 3745 Desc.getNumOperands() != MI.getNumExplicitOperands()) { 3746 ErrInfo = "Instruction has wrong number of operands."; 3747 return false; 3748 } 3749 3750 if (MI.isInlineAsm()) { 3751 // Verify register classes for inlineasm constraints. 3752 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = MI.getNumOperands(); 3753 I != E; ++I) { 3754 const TargetRegisterClass *RC = MI.getRegClassConstraint(I, this, &RI); 3755 if (!RC) 3756 continue; 3757 3758 const MachineOperand &Op = MI.getOperand(I); 3759 if (!Op.isReg()) 3760 continue; 3761 3762 Register Reg = Op.getReg(); 3763 if (!Reg.isVirtual() && !RC->contains(Reg)) { 3764 ErrInfo = "inlineasm operand has incorrect register class."; 3765 return false; 3766 } 3767 } 3768 3769 return true; 3770 } 3771 3772 if (isMIMG(MI) && MI.memoperands_empty() && MI.mayLoadOrStore()) { 3773 ErrInfo = "missing memory operand from MIMG instruction."; 3774 return false; 3775 } 3776 3777 // Make sure the register classes are correct. 3778 for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) { 3779 const MachineOperand &MO = MI.getOperand(i); 3780 if (MO.isFPImm()) { 3781 ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast " 3782 "all fp values to integers."; 3783 return false; 3784 } 3785 3786 int RegClass = Desc.OpInfo[i].RegClass; 3787 3788 switch (Desc.OpInfo[i].OperandType) { 3789 case MCOI::OPERAND_REGISTER: 3790 if (MI.getOperand(i).isImm() || MI.getOperand(i).isGlobal()) { 3791 ErrInfo = "Illegal immediate value for operand."; 3792 return false; 3793 } 3794 break; 3795 case AMDGPU::OPERAND_REG_IMM_INT32: 3796 case AMDGPU::OPERAND_REG_IMM_FP32: 3797 break; 3798 case AMDGPU::OPERAND_REG_INLINE_C_INT32: 3799 case AMDGPU::OPERAND_REG_INLINE_C_FP32: 3800 case AMDGPU::OPERAND_REG_INLINE_C_INT64: 3801 case AMDGPU::OPERAND_REG_INLINE_C_FP64: 3802 case AMDGPU::OPERAND_REG_INLINE_C_INT16: 3803 case AMDGPU::OPERAND_REG_INLINE_C_FP16: 3804 case AMDGPU::OPERAND_REG_INLINE_AC_INT32: 3805 case AMDGPU::OPERAND_REG_INLINE_AC_FP32: 3806 case AMDGPU::OPERAND_REG_INLINE_AC_INT16: 3807 case AMDGPU::OPERAND_REG_INLINE_AC_FP16: 3808 case AMDGPU::OPERAND_REG_INLINE_AC_FP64: { 3809 if (!MO.isReg() && (!MO.isImm() || !isInlineConstant(MI, i))) { 3810 ErrInfo = "Illegal immediate value for operand."; 3811 return false; 3812 } 3813 break; 3814 } 3815 case MCOI::OPERAND_IMMEDIATE: 3816 case AMDGPU::OPERAND_KIMM32: 3817 // Check if this operand is an immediate. 3818 // FrameIndex operands will be replaced by immediates, so they are 3819 // allowed. 3820 if (!MI.getOperand(i).isImm() && !MI.getOperand(i).isFI()) { 3821 ErrInfo = "Expected immediate, but got non-immediate"; 3822 return false; 3823 } 3824 LLVM_FALLTHROUGH; 3825 default: 3826 continue; 3827 } 3828 3829 if (!MO.isReg()) 3830 continue; 3831 Register Reg = MO.getReg(); 3832 if (!Reg) 3833 continue; 3834 3835 // FIXME: Ideally we would have separate instruction definitions with the 3836 // aligned register constraint. 3837 // FIXME: We do not verify inline asm operands, but custom inline asm 3838 // verification is broken anyway 3839 if (ST.needsAlignedVGPRs()) { 3840 const TargetRegisterClass *RC = RI.getRegClassForReg(MRI, Reg); 3841 const bool IsVGPR = RI.hasVGPRs(RC); 3842 const bool IsAGPR = !IsVGPR && RI.hasAGPRs(RC); 3843 if ((IsVGPR || IsAGPR) && MO.getSubReg()) { 3844 const TargetRegisterClass *SubRC = 3845 RI.getSubRegClass(RC, MO.getSubReg()); 3846 RC = RI.getCompatibleSubRegClass(RC, SubRC, MO.getSubReg()); 3847 if (RC) 3848 RC = SubRC; 3849 } 3850 3851 // Check that this is the aligned version of the class. 3852 if (!RC || ((IsVGPR && !RC->hasSuperClassEq(RI.getVGPRClassForBitWidth( 3853 RI.getRegSizeInBits(*RC)))) || 3854 (IsAGPR && !RC->hasSuperClassEq(RI.getAGPRClassForBitWidth( 3855 RI.getRegSizeInBits(*RC)))))) { 3856 ErrInfo = "Subtarget requires even aligned vector registers"; 3857 return false; 3858 } 3859 } 3860 3861 if (RegClass != -1) { 3862 if (Reg.isVirtual()) 3863 continue; 3864 3865 const TargetRegisterClass *RC = RI.getRegClass(RegClass); 3866 if (!RC->contains(Reg)) { 3867 ErrInfo = "Operand has incorrect register class."; 3868 return false; 3869 } 3870 } 3871 } 3872 3873 // Verify SDWA 3874 if (isSDWA(MI)) { 3875 if (!ST.hasSDWA()) { 3876 ErrInfo = "SDWA is not supported on this target"; 3877 return false; 3878 } 3879 3880 int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst); 3881 3882 const int OpIndicies[] = { DstIdx, Src0Idx, Src1Idx, Src2Idx }; 3883 3884 for (int OpIdx: OpIndicies) { 3885 if (OpIdx == -1) 3886 continue; 3887 const MachineOperand &MO = MI.getOperand(OpIdx); 3888 3889 if (!ST.hasSDWAScalar()) { 3890 // Only VGPRS on VI 3891 if (!MO.isReg() || !RI.hasVGPRs(RI.getRegClassForReg(MRI, MO.getReg()))) { 3892 ErrInfo = "Only VGPRs allowed as operands in SDWA instructions on VI"; 3893 return false; 3894 } 3895 } else { 3896 // No immediates on GFX9 3897 if (!MO.isReg()) { 3898 ErrInfo = 3899 "Only reg allowed as operands in SDWA instructions on GFX9+"; 3900 return false; 3901 } 3902 } 3903 } 3904 3905 if (!ST.hasSDWAOmod()) { 3906 // No omod allowed on VI 3907 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod); 3908 if (OMod != nullptr && 3909 (!OMod->isImm() || OMod->getImm() != 0)) { 3910 ErrInfo = "OMod not allowed in SDWA instructions on VI"; 3911 return false; 3912 } 3913 } 3914 3915 uint16_t BasicOpcode = AMDGPU::getBasicFromSDWAOp(Opcode); 3916 if (isVOPC(BasicOpcode)) { 3917 if (!ST.hasSDWASdst() && DstIdx != -1) { 3918 // Only vcc allowed as dst on VI for VOPC 3919 const MachineOperand &Dst = MI.getOperand(DstIdx); 3920 if (!Dst.isReg() || Dst.getReg() != AMDGPU::VCC) { 3921 ErrInfo = "Only VCC allowed as dst in SDWA instructions on VI"; 3922 return false; 3923 } 3924 } else if (!ST.hasSDWAOutModsVOPC()) { 3925 // No clamp allowed on GFX9 for VOPC 3926 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp); 3927 if (Clamp && (!Clamp->isImm() || Clamp->getImm() != 0)) { 3928 ErrInfo = "Clamp not allowed in VOPC SDWA instructions on VI"; 3929 return false; 3930 } 3931 3932 // No omod allowed on GFX9 for VOPC 3933 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod); 3934 if (OMod && (!OMod->isImm() || OMod->getImm() != 0)) { 3935 ErrInfo = "OMod not allowed in VOPC SDWA instructions on VI"; 3936 return false; 3937 } 3938 } 3939 } 3940 3941 const MachineOperand *DstUnused = getNamedOperand(MI, AMDGPU::OpName::dst_unused); 3942 if (DstUnused && DstUnused->isImm() && 3943 DstUnused->getImm() == AMDGPU::SDWA::UNUSED_PRESERVE) { 3944 const MachineOperand &Dst = MI.getOperand(DstIdx); 3945 if (!Dst.isReg() || !Dst.isTied()) { 3946 ErrInfo = "Dst register should have tied register"; 3947 return false; 3948 } 3949 3950 const MachineOperand &TiedMO = 3951 MI.getOperand(MI.findTiedOperandIdx(DstIdx)); 3952 if (!TiedMO.isReg() || !TiedMO.isImplicit() || !TiedMO.isUse()) { 3953 ErrInfo = 3954 "Dst register should be tied to implicit use of preserved register"; 3955 return false; 3956 } else if (TiedMO.getReg().isPhysical() && 3957 Dst.getReg() != TiedMO.getReg()) { 3958 ErrInfo = "Dst register should use same physical register as preserved"; 3959 return false; 3960 } 3961 } 3962 } 3963 3964 // Verify MIMG 3965 if (isMIMG(MI.getOpcode()) && !MI.mayStore()) { 3966 // Ensure that the return type used is large enough for all the options 3967 // being used TFE/LWE require an extra result register. 3968 const MachineOperand *DMask = getNamedOperand(MI, AMDGPU::OpName::dmask); 3969 if (DMask) { 3970 uint64_t DMaskImm = DMask->getImm(); 3971 uint32_t RegCount = 3972 isGather4(MI.getOpcode()) ? 4 : countPopulation(DMaskImm); 3973 const MachineOperand *TFE = getNamedOperand(MI, AMDGPU::OpName::tfe); 3974 const MachineOperand *LWE = getNamedOperand(MI, AMDGPU::OpName::lwe); 3975 const MachineOperand *D16 = getNamedOperand(MI, AMDGPU::OpName::d16); 3976 3977 // Adjust for packed 16 bit values 3978 if (D16 && D16->getImm() && !ST.hasUnpackedD16VMem()) 3979 RegCount >>= 1; 3980 3981 // Adjust if using LWE or TFE 3982 if ((LWE && LWE->getImm()) || (TFE && TFE->getImm())) 3983 RegCount += 1; 3984 3985 const uint32_t DstIdx = 3986 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata); 3987 const MachineOperand &Dst = MI.getOperand(DstIdx); 3988 if (Dst.isReg()) { 3989 const TargetRegisterClass *DstRC = getOpRegClass(MI, DstIdx); 3990 uint32_t DstSize = RI.getRegSizeInBits(*DstRC) / 32; 3991 if (RegCount > DstSize) { 3992 ErrInfo = "MIMG instruction returns too many registers for dst " 3993 "register class"; 3994 return false; 3995 } 3996 } 3997 } 3998 } 3999 4000 // Verify VOP*. Ignore multiple sgpr operands on writelane. 4001 if (Desc.getOpcode() != AMDGPU::V_WRITELANE_B32 4002 && (isVOP1(MI) || isVOP2(MI) || isVOP3(MI) || isVOPC(MI) || isSDWA(MI))) { 4003 // Only look at the true operands. Only a real operand can use the constant 4004 // bus, and we don't want to check pseudo-operands like the source modifier 4005 // flags. 4006 const int OpIndices[] = { Src0Idx, Src1Idx, Src2Idx }; 4007 4008 unsigned ConstantBusCount = 0; 4009 unsigned LiteralCount = 0; 4010 4011 if (AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm) != -1) 4012 ++ConstantBusCount; 4013 4014 SmallVector<Register, 2> SGPRsUsed; 4015 Register SGPRUsed; 4016 4017 for (int OpIdx : OpIndices) { 4018 if (OpIdx == -1) 4019 break; 4020 const MachineOperand &MO = MI.getOperand(OpIdx); 4021 if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) { 4022 if (MO.isReg()) { 4023 SGPRUsed = MO.getReg(); 4024 if (llvm::all_of(SGPRsUsed, [SGPRUsed](unsigned SGPR) { 4025 return SGPRUsed != SGPR; 4026 })) { 4027 ++ConstantBusCount; 4028 SGPRsUsed.push_back(SGPRUsed); 4029 } 4030 } else { 4031 ++ConstantBusCount; 4032 ++LiteralCount; 4033 } 4034 } 4035 } 4036 4037 SGPRUsed = findImplicitSGPRRead(MI); 4038 if (SGPRUsed != AMDGPU::NoRegister) { 4039 // Implicit uses may safely overlap true overands 4040 if (llvm::all_of(SGPRsUsed, [this, SGPRUsed](unsigned SGPR) { 4041 return !RI.regsOverlap(SGPRUsed, SGPR); 4042 })) { 4043 ++ConstantBusCount; 4044 SGPRsUsed.push_back(SGPRUsed); 4045 } 4046 } 4047 4048 // v_writelane_b32 is an exception from constant bus restriction: 4049 // vsrc0 can be sgpr, const or m0 and lane select sgpr, m0 or inline-const 4050 if (ConstantBusCount > ST.getConstantBusLimit(Opcode) && 4051 Opcode != AMDGPU::V_WRITELANE_B32) { 4052 ErrInfo = "VOP* instruction violates constant bus restriction"; 4053 return false; 4054 } 4055 4056 if (isVOP3(MI) && LiteralCount) { 4057 if (!ST.hasVOP3Literal()) { 4058 ErrInfo = "VOP3 instruction uses literal"; 4059 return false; 4060 } 4061 if (LiteralCount > 1) { 4062 ErrInfo = "VOP3 instruction uses more than one literal"; 4063 return false; 4064 } 4065 } 4066 } 4067 4068 // Special case for writelane - this can break the multiple constant bus rule, 4069 // but still can't use more than one SGPR register 4070 if (Desc.getOpcode() == AMDGPU::V_WRITELANE_B32) { 4071 unsigned SGPRCount = 0; 4072 Register SGPRUsed = AMDGPU::NoRegister; 4073 4074 for (int OpIdx : {Src0Idx, Src1Idx, Src2Idx}) { 4075 if (OpIdx == -1) 4076 break; 4077 4078 const MachineOperand &MO = MI.getOperand(OpIdx); 4079 4080 if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) { 4081 if (MO.isReg() && MO.getReg() != AMDGPU::M0) { 4082 if (MO.getReg() != SGPRUsed) 4083 ++SGPRCount; 4084 SGPRUsed = MO.getReg(); 4085 } 4086 } 4087 if (SGPRCount > ST.getConstantBusLimit(Opcode)) { 4088 ErrInfo = "WRITELANE instruction violates constant bus restriction"; 4089 return false; 4090 } 4091 } 4092 } 4093 4094 // Verify misc. restrictions on specific instructions. 4095 if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32_e64 || 4096 Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64_e64) { 4097 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 4098 const MachineOperand &Src1 = MI.getOperand(Src1Idx); 4099 const MachineOperand &Src2 = MI.getOperand(Src2Idx); 4100 if (Src0.isReg() && Src1.isReg() && Src2.isReg()) { 4101 if (!compareMachineOp(Src0, Src1) && 4102 !compareMachineOp(Src0, Src2)) { 4103 ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2"; 4104 return false; 4105 } 4106 } 4107 if ((getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm() & 4108 SISrcMods::ABS) || 4109 (getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm() & 4110 SISrcMods::ABS) || 4111 (getNamedOperand(MI, AMDGPU::OpName::src2_modifiers)->getImm() & 4112 SISrcMods::ABS)) { 4113 ErrInfo = "ABS not allowed in VOP3B instructions"; 4114 return false; 4115 } 4116 } 4117 4118 if (isSOP2(MI) || isSOPC(MI)) { 4119 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 4120 const MachineOperand &Src1 = MI.getOperand(Src1Idx); 4121 unsigned Immediates = 0; 4122 4123 if (!Src0.isReg() && 4124 !isInlineConstant(Src0, Desc.OpInfo[Src0Idx].OperandType)) 4125 Immediates++; 4126 if (!Src1.isReg() && 4127 !isInlineConstant(Src1, Desc.OpInfo[Src1Idx].OperandType)) 4128 Immediates++; 4129 4130 if (Immediates > 1) { 4131 ErrInfo = "SOP2/SOPC instruction requires too many immediate constants"; 4132 return false; 4133 } 4134 } 4135 4136 if (isSOPK(MI)) { 4137 auto Op = getNamedOperand(MI, AMDGPU::OpName::simm16); 4138 if (Desc.isBranch()) { 4139 if (!Op->isMBB()) { 4140 ErrInfo = "invalid branch target for SOPK instruction"; 4141 return false; 4142 } 4143 } else { 4144 uint64_t Imm = Op->getImm(); 4145 if (sopkIsZext(MI)) { 4146 if (!isUInt<16>(Imm)) { 4147 ErrInfo = "invalid immediate for SOPK instruction"; 4148 return false; 4149 } 4150 } else { 4151 if (!isInt<16>(Imm)) { 4152 ErrInfo = "invalid immediate for SOPK instruction"; 4153 return false; 4154 } 4155 } 4156 } 4157 } 4158 4159 if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 || 4160 Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 || 4161 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 4162 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) { 4163 const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 4164 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64; 4165 4166 const unsigned StaticNumOps = Desc.getNumOperands() + 4167 Desc.getNumImplicitUses(); 4168 const unsigned NumImplicitOps = IsDst ? 2 : 1; 4169 4170 // Allow additional implicit operands. This allows a fixup done by the post 4171 // RA scheduler where the main implicit operand is killed and implicit-defs 4172 // are added for sub-registers that remain live after this instruction. 4173 if (MI.getNumOperands() < StaticNumOps + NumImplicitOps) { 4174 ErrInfo = "missing implicit register operands"; 4175 return false; 4176 } 4177 4178 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 4179 if (IsDst) { 4180 if (!Dst->isUse()) { 4181 ErrInfo = "v_movreld_b32 vdst should be a use operand"; 4182 return false; 4183 } 4184 4185 unsigned UseOpIdx; 4186 if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) || 4187 UseOpIdx != StaticNumOps + 1) { 4188 ErrInfo = "movrel implicit operands should be tied"; 4189 return false; 4190 } 4191 } 4192 4193 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 4194 const MachineOperand &ImpUse 4195 = MI.getOperand(StaticNumOps + NumImplicitOps - 1); 4196 if (!ImpUse.isReg() || !ImpUse.isUse() || 4197 !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) { 4198 ErrInfo = "src0 should be subreg of implicit vector use"; 4199 return false; 4200 } 4201 } 4202 4203 // Make sure we aren't losing exec uses in the td files. This mostly requires 4204 // being careful when using let Uses to try to add other use registers. 4205 if (shouldReadExec(MI)) { 4206 if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) { 4207 ErrInfo = "VALU instruction does not implicitly read exec mask"; 4208 return false; 4209 } 4210 } 4211 4212 if (isSMRD(MI)) { 4213 if (MI.mayStore()) { 4214 // The register offset form of scalar stores may only use m0 as the 4215 // soffset register. 4216 const MachineOperand *Soff = getNamedOperand(MI, AMDGPU::OpName::soff); 4217 if (Soff && Soff->getReg() != AMDGPU::M0) { 4218 ErrInfo = "scalar stores must use m0 as offset register"; 4219 return false; 4220 } 4221 } 4222 } 4223 4224 if (isFLAT(MI) && !ST.hasFlatInstOffsets()) { 4225 const MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 4226 if (Offset->getImm() != 0) { 4227 ErrInfo = "subtarget does not support offsets in flat instructions"; 4228 return false; 4229 } 4230 } 4231 4232 if (isMIMG(MI)) { 4233 const MachineOperand *DimOp = getNamedOperand(MI, AMDGPU::OpName::dim); 4234 if (DimOp) { 4235 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opcode, 4236 AMDGPU::OpName::vaddr0); 4237 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); 4238 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(Opcode); 4239 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode = 4240 AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode); 4241 const AMDGPU::MIMGDimInfo *Dim = 4242 AMDGPU::getMIMGDimInfoByEncoding(DimOp->getImm()); 4243 4244 if (!Dim) { 4245 ErrInfo = "dim is out of range"; 4246 return false; 4247 } 4248 4249 bool IsA16 = false; 4250 if (ST.hasR128A16()) { 4251 const MachineOperand *R128A16 = getNamedOperand(MI, AMDGPU::OpName::r128); 4252 IsA16 = R128A16->getImm() != 0; 4253 } else if (ST.hasGFX10A16()) { 4254 const MachineOperand *A16 = getNamedOperand(MI, AMDGPU::OpName::a16); 4255 IsA16 = A16->getImm() != 0; 4256 } 4257 4258 bool PackDerivatives = IsA16 || BaseOpcode->G16; 4259 bool IsNSA = SRsrcIdx - VAddr0Idx > 1; 4260 4261 unsigned AddrWords = BaseOpcode->NumExtraArgs; 4262 unsigned AddrComponents = (BaseOpcode->Coordinates ? Dim->NumCoords : 0) + 4263 (BaseOpcode->LodOrClampOrMip ? 1 : 0); 4264 if (IsA16) 4265 AddrWords += (AddrComponents + 1) / 2; 4266 else 4267 AddrWords += AddrComponents; 4268 4269 if (BaseOpcode->Gradients) { 4270 if (PackDerivatives) 4271 // There are two gradients per coordinate, we pack them separately. 4272 // For the 3d case, we get (dy/du, dx/du) (-, dz/du) (dy/dv, dx/dv) (-, dz/dv) 4273 AddrWords += (Dim->NumGradients / 2 + 1) / 2 * 2; 4274 else 4275 AddrWords += Dim->NumGradients; 4276 } 4277 4278 unsigned VAddrWords; 4279 if (IsNSA) { 4280 VAddrWords = SRsrcIdx - VAddr0Idx; 4281 } else { 4282 const TargetRegisterClass *RC = getOpRegClass(MI, VAddr0Idx); 4283 VAddrWords = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC) / 32; 4284 if (AddrWords > 8) 4285 AddrWords = 16; 4286 else if (AddrWords > 4) 4287 AddrWords = 8; 4288 else if (AddrWords == 4) 4289 AddrWords = 4; 4290 else if (AddrWords == 3) 4291 AddrWords = 3; 4292 } 4293 4294 if (VAddrWords != AddrWords) { 4295 LLVM_DEBUG(dbgs() << "bad vaddr size, expected " << AddrWords 4296 << " but got " << VAddrWords << "\n"); 4297 ErrInfo = "bad vaddr size"; 4298 return false; 4299 } 4300 } 4301 } 4302 4303 const MachineOperand *DppCt = getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl); 4304 if (DppCt) { 4305 using namespace AMDGPU::DPP; 4306 4307 unsigned DC = DppCt->getImm(); 4308 if (DC == DppCtrl::DPP_UNUSED1 || DC == DppCtrl::DPP_UNUSED2 || 4309 DC == DppCtrl::DPP_UNUSED3 || DC > DppCtrl::DPP_LAST || 4310 (DC >= DppCtrl::DPP_UNUSED4_FIRST && DC <= DppCtrl::DPP_UNUSED4_LAST) || 4311 (DC >= DppCtrl::DPP_UNUSED5_FIRST && DC <= DppCtrl::DPP_UNUSED5_LAST) || 4312 (DC >= DppCtrl::DPP_UNUSED6_FIRST && DC <= DppCtrl::DPP_UNUSED6_LAST) || 4313 (DC >= DppCtrl::DPP_UNUSED7_FIRST && DC <= DppCtrl::DPP_UNUSED7_LAST) || 4314 (DC >= DppCtrl::DPP_UNUSED8_FIRST && DC <= DppCtrl::DPP_UNUSED8_LAST)) { 4315 ErrInfo = "Invalid dpp_ctrl value"; 4316 return false; 4317 } 4318 if (DC >= DppCtrl::WAVE_SHL1 && DC <= DppCtrl::WAVE_ROR1 && 4319 ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 4320 ErrInfo = "Invalid dpp_ctrl value: " 4321 "wavefront shifts are not supported on GFX10+"; 4322 return false; 4323 } 4324 if (DC >= DppCtrl::BCAST15 && DC <= DppCtrl::BCAST31 && 4325 ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 4326 ErrInfo = "Invalid dpp_ctrl value: " 4327 "broadcasts are not supported on GFX10+"; 4328 return false; 4329 } 4330 if (DC >= DppCtrl::ROW_SHARE_FIRST && DC <= DppCtrl::ROW_XMASK_LAST && 4331 ST.getGeneration() < AMDGPUSubtarget::GFX10) { 4332 if (DC >= DppCtrl::ROW_NEWBCAST_FIRST && 4333 DC <= DppCtrl::ROW_NEWBCAST_LAST && 4334 !ST.hasGFX90AInsts()) { 4335 ErrInfo = "Invalid dpp_ctrl value: " 4336 "row_newbroadcast/row_share is not supported before " 4337 "GFX90A/GFX10"; 4338 return false; 4339 } else if (DC > DppCtrl::ROW_NEWBCAST_LAST || !ST.hasGFX90AInsts()) { 4340 ErrInfo = "Invalid dpp_ctrl value: " 4341 "row_share and row_xmask are not supported before GFX10"; 4342 return false; 4343 } 4344 } 4345 4346 int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst); 4347 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0); 4348 4349 if (Opcode != AMDGPU::V_MOV_B64_DPP_PSEUDO && 4350 ((DstIdx >= 0 && 4351 (Desc.OpInfo[DstIdx].RegClass == AMDGPU::VReg_64RegClassID || 4352 Desc.OpInfo[DstIdx].RegClass == AMDGPU::VReg_64_Align2RegClassID)) || 4353 ((Src0Idx >= 0 && 4354 (Desc.OpInfo[Src0Idx].RegClass == AMDGPU::VReg_64RegClassID || 4355 Desc.OpInfo[Src0Idx].RegClass == 4356 AMDGPU::VReg_64_Align2RegClassID)))) && 4357 !AMDGPU::isLegal64BitDPPControl(DC)) { 4358 ErrInfo = "Invalid dpp_ctrl value: " 4359 "64 bit dpp only support row_newbcast"; 4360 return false; 4361 } 4362 } 4363 4364 if ((MI.mayStore() || MI.mayLoad()) && !isVGPRSpill(MI)) { 4365 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 4366 uint16_t DataNameIdx = isDS(Opcode) ? AMDGPU::OpName::data0 4367 : AMDGPU::OpName::vdata; 4368 const MachineOperand *Data = getNamedOperand(MI, DataNameIdx); 4369 const MachineOperand *Data2 = getNamedOperand(MI, AMDGPU::OpName::data1); 4370 if (Data && !Data->isReg()) 4371 Data = nullptr; 4372 4373 if (ST.hasGFX90AInsts()) { 4374 if (Dst && Data && 4375 (RI.isAGPR(MRI, Dst->getReg()) != RI.isAGPR(MRI, Data->getReg()))) { 4376 ErrInfo = "Invalid register class: " 4377 "vdata and vdst should be both VGPR or AGPR"; 4378 return false; 4379 } 4380 if (Data && Data2 && 4381 (RI.isAGPR(MRI, Data->getReg()) != RI.isAGPR(MRI, Data2->getReg()))) { 4382 ErrInfo = "Invalid register class: " 4383 "both data operands should be VGPR or AGPR"; 4384 return false; 4385 } 4386 } else { 4387 if ((Dst && RI.isAGPR(MRI, Dst->getReg())) || 4388 (Data && RI.isAGPR(MRI, Data->getReg())) || 4389 (Data2 && RI.isAGPR(MRI, Data2->getReg()))) { 4390 ErrInfo = "Invalid register class: " 4391 "agpr loads and stores not supported on this GPU"; 4392 return false; 4393 } 4394 } 4395 } 4396 4397 return true; 4398 } 4399 4400 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) const { 4401 switch (MI.getOpcode()) { 4402 default: return AMDGPU::INSTRUCTION_LIST_END; 4403 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE; 4404 case AMDGPU::COPY: return AMDGPU::COPY; 4405 case AMDGPU::PHI: return AMDGPU::PHI; 4406 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG; 4407 case AMDGPU::WQM: return AMDGPU::WQM; 4408 case AMDGPU::SOFT_WQM: return AMDGPU::SOFT_WQM; 4409 case AMDGPU::WWM: return AMDGPU::WWM; 4410 case AMDGPU::S_MOV_B32: { 4411 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 4412 return MI.getOperand(1).isReg() || 4413 RI.isAGPR(MRI, MI.getOperand(0).getReg()) ? 4414 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32; 4415 } 4416 case AMDGPU::S_ADD_I32: 4417 return ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_CO_U32_e32; 4418 case AMDGPU::S_ADDC_U32: 4419 return AMDGPU::V_ADDC_U32_e32; 4420 case AMDGPU::S_SUB_I32: 4421 return ST.hasAddNoCarry() ? AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_CO_U32_e32; 4422 // FIXME: These are not consistently handled, and selected when the carry is 4423 // used. 4424 case AMDGPU::S_ADD_U32: 4425 return AMDGPU::V_ADD_CO_U32_e32; 4426 case AMDGPU::S_SUB_U32: 4427 return AMDGPU::V_SUB_CO_U32_e32; 4428 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32; 4429 case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_U32_e64; 4430 case AMDGPU::S_MUL_HI_U32: return AMDGPU::V_MUL_HI_U32_e64; 4431 case AMDGPU::S_MUL_HI_I32: return AMDGPU::V_MUL_HI_I32_e64; 4432 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64; 4433 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64; 4434 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64; 4435 case AMDGPU::S_XNOR_B32: 4436 return ST.hasDLInsts() ? AMDGPU::V_XNOR_B32_e64 : AMDGPU::INSTRUCTION_LIST_END; 4437 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64; 4438 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64; 4439 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64; 4440 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64; 4441 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32; 4442 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64_e64; 4443 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32; 4444 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64_e64; 4445 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32; 4446 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64_e64; 4447 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32_e64; 4448 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32_e64; 4449 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32_e64; 4450 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32_e64; 4451 case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64; 4452 case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32; 4453 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32; 4454 case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32; 4455 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e32; 4456 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e32; 4457 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e32; 4458 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e32; 4459 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e32; 4460 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e32; 4461 case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e32; 4462 case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e32; 4463 case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e32; 4464 case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e32; 4465 case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e32; 4466 case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e32; 4467 case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e32; 4468 case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e32; 4469 case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64; 4470 case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32; 4471 case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32; 4472 case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64; 4473 case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ; 4474 case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ; 4475 } 4476 llvm_unreachable( 4477 "Unexpected scalar opcode without corresponding vector one!"); 4478 } 4479 4480 static unsigned adjustAllocatableRegClass(const GCNSubtarget &ST, 4481 const MachineRegisterInfo &MRI, 4482 const MCInstrDesc &TID, 4483 unsigned RCID, 4484 bool IsAllocatable) { 4485 if ((IsAllocatable || !ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) && 4486 (TID.mayLoad() || TID.mayStore() || 4487 (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::MIMG)))) { 4488 switch (RCID) { 4489 case AMDGPU::AV_32RegClassID: return AMDGPU::VGPR_32RegClassID; 4490 case AMDGPU::AV_64RegClassID: return AMDGPU::VReg_64RegClassID; 4491 case AMDGPU::AV_96RegClassID: return AMDGPU::VReg_96RegClassID; 4492 case AMDGPU::AV_128RegClassID: return AMDGPU::VReg_128RegClassID; 4493 case AMDGPU::AV_160RegClassID: return AMDGPU::VReg_160RegClassID; 4494 default: 4495 break; 4496 } 4497 } 4498 return RCID; 4499 } 4500 4501 const TargetRegisterClass *SIInstrInfo::getRegClass(const MCInstrDesc &TID, 4502 unsigned OpNum, const TargetRegisterInfo *TRI, 4503 const MachineFunction &MF) 4504 const { 4505 if (OpNum >= TID.getNumOperands()) 4506 return nullptr; 4507 auto RegClass = TID.OpInfo[OpNum].RegClass; 4508 bool IsAllocatable = false; 4509 if (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::FLAT)) { 4510 // vdst and vdata should be both VGPR or AGPR, same for the DS instructions 4511 // with two data operands. Request register class constainted to VGPR only 4512 // of both operands present as Machine Copy Propagation can not check this 4513 // constraint and possibly other passes too. 4514 // 4515 // The check is limited to FLAT and DS because atomics in non-flat encoding 4516 // have their vdst and vdata tied to be the same register. 4517 const int VDstIdx = AMDGPU::getNamedOperandIdx(TID.Opcode, 4518 AMDGPU::OpName::vdst); 4519 const int DataIdx = AMDGPU::getNamedOperandIdx(TID.Opcode, 4520 (TID.TSFlags & SIInstrFlags::DS) ? AMDGPU::OpName::data0 4521 : AMDGPU::OpName::vdata); 4522 if (DataIdx != -1) { 4523 IsAllocatable = VDstIdx != -1 || 4524 AMDGPU::getNamedOperandIdx(TID.Opcode, 4525 AMDGPU::OpName::data1) != -1; 4526 } 4527 } 4528 RegClass = adjustAllocatableRegClass(ST, MF.getRegInfo(), TID, RegClass, 4529 IsAllocatable); 4530 return RI.getRegClass(RegClass); 4531 } 4532 4533 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI, 4534 unsigned OpNo) const { 4535 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 4536 const MCInstrDesc &Desc = get(MI.getOpcode()); 4537 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() || 4538 Desc.OpInfo[OpNo].RegClass == -1) { 4539 Register Reg = MI.getOperand(OpNo).getReg(); 4540 4541 if (Reg.isVirtual()) 4542 return MRI.getRegClass(Reg); 4543 return RI.getPhysRegClass(Reg); 4544 } 4545 4546 unsigned RCID = Desc.OpInfo[OpNo].RegClass; 4547 RCID = adjustAllocatableRegClass(ST, MRI, Desc, RCID, true); 4548 return RI.getRegClass(RCID); 4549 } 4550 4551 void SIInstrInfo::legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const { 4552 MachineBasicBlock::iterator I = MI; 4553 MachineBasicBlock *MBB = MI.getParent(); 4554 MachineOperand &MO = MI.getOperand(OpIdx); 4555 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 4556 unsigned RCID = get(MI.getOpcode()).OpInfo[OpIdx].RegClass; 4557 const TargetRegisterClass *RC = RI.getRegClass(RCID); 4558 unsigned Size = RI.getRegSizeInBits(*RC); 4559 unsigned Opcode = (Size == 64) ? AMDGPU::V_MOV_B64_PSEUDO : AMDGPU::V_MOV_B32_e32; 4560 if (MO.isReg()) 4561 Opcode = AMDGPU::COPY; 4562 else if (RI.isSGPRClass(RC)) 4563 Opcode = (Size == 64) ? AMDGPU::S_MOV_B64 : AMDGPU::S_MOV_B32; 4564 4565 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC); 4566 const TargetRegisterClass *VRC64 = RI.getVGPR64Class(); 4567 if (RI.getCommonSubClass(VRC64, VRC)) 4568 VRC = VRC64; 4569 else 4570 VRC = &AMDGPU::VGPR_32RegClass; 4571 4572 Register Reg = MRI.createVirtualRegister(VRC); 4573 DebugLoc DL = MBB->findDebugLoc(I); 4574 BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).add(MO); 4575 MO.ChangeToRegister(Reg, false); 4576 } 4577 4578 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI, 4579 MachineRegisterInfo &MRI, 4580 MachineOperand &SuperReg, 4581 const TargetRegisterClass *SuperRC, 4582 unsigned SubIdx, 4583 const TargetRegisterClass *SubRC) 4584 const { 4585 MachineBasicBlock *MBB = MI->getParent(); 4586 DebugLoc DL = MI->getDebugLoc(); 4587 Register SubReg = MRI.createVirtualRegister(SubRC); 4588 4589 if (SuperReg.getSubReg() == AMDGPU::NoSubRegister) { 4590 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 4591 .addReg(SuperReg.getReg(), 0, SubIdx); 4592 return SubReg; 4593 } 4594 4595 // Just in case the super register is itself a sub-register, copy it to a new 4596 // value so we don't need to worry about merging its subreg index with the 4597 // SubIdx passed to this function. The register coalescer should be able to 4598 // eliminate this extra copy. 4599 Register NewSuperReg = MRI.createVirtualRegister(SuperRC); 4600 4601 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg) 4602 .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg()); 4603 4604 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 4605 .addReg(NewSuperReg, 0, SubIdx); 4606 4607 return SubReg; 4608 } 4609 4610 MachineOperand SIInstrInfo::buildExtractSubRegOrImm( 4611 MachineBasicBlock::iterator MII, 4612 MachineRegisterInfo &MRI, 4613 MachineOperand &Op, 4614 const TargetRegisterClass *SuperRC, 4615 unsigned SubIdx, 4616 const TargetRegisterClass *SubRC) const { 4617 if (Op.isImm()) { 4618 if (SubIdx == AMDGPU::sub0) 4619 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm())); 4620 if (SubIdx == AMDGPU::sub1) 4621 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32)); 4622 4623 llvm_unreachable("Unhandled register index for immediate"); 4624 } 4625 4626 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC, 4627 SubIdx, SubRC); 4628 return MachineOperand::CreateReg(SubReg, false); 4629 } 4630 4631 // Change the order of operands from (0, 1, 2) to (0, 2, 1) 4632 void SIInstrInfo::swapOperands(MachineInstr &Inst) const { 4633 assert(Inst.getNumExplicitOperands() == 3); 4634 MachineOperand Op1 = Inst.getOperand(1); 4635 Inst.RemoveOperand(1); 4636 Inst.addOperand(Op1); 4637 } 4638 4639 bool SIInstrInfo::isLegalRegOperand(const MachineRegisterInfo &MRI, 4640 const MCOperandInfo &OpInfo, 4641 const MachineOperand &MO) const { 4642 if (!MO.isReg()) 4643 return false; 4644 4645 Register Reg = MO.getReg(); 4646 4647 const TargetRegisterClass *DRC = RI.getRegClass(OpInfo.RegClass); 4648 if (Reg.isPhysical()) 4649 return DRC->contains(Reg); 4650 4651 const TargetRegisterClass *RC = MRI.getRegClass(Reg); 4652 4653 if (MO.getSubReg()) { 4654 const MachineFunction *MF = MO.getParent()->getParent()->getParent(); 4655 const TargetRegisterClass *SuperRC = RI.getLargestLegalSuperClass(RC, *MF); 4656 if (!SuperRC) 4657 return false; 4658 4659 DRC = RI.getMatchingSuperRegClass(SuperRC, DRC, MO.getSubReg()); 4660 if (!DRC) 4661 return false; 4662 } 4663 return RC->hasSuperClassEq(DRC); 4664 } 4665 4666 bool SIInstrInfo::isLegalVSrcOperand(const MachineRegisterInfo &MRI, 4667 const MCOperandInfo &OpInfo, 4668 const MachineOperand &MO) const { 4669 if (MO.isReg()) 4670 return isLegalRegOperand(MRI, OpInfo, MO); 4671 4672 // Handle non-register types that are treated like immediates. 4673 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal()); 4674 return true; 4675 } 4676 4677 bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx, 4678 const MachineOperand *MO) const { 4679 const MachineFunction &MF = *MI.getParent()->getParent(); 4680 const MachineRegisterInfo &MRI = MF.getRegInfo(); 4681 const MCInstrDesc &InstDesc = MI.getDesc(); 4682 const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx]; 4683 const TargetRegisterClass *DefinedRC = 4684 OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr; 4685 if (!MO) 4686 MO = &MI.getOperand(OpIdx); 4687 4688 int ConstantBusLimit = ST.getConstantBusLimit(MI.getOpcode()); 4689 int VOP3LiteralLimit = ST.hasVOP3Literal() ? 1 : 0; 4690 if (isVALU(MI) && usesConstantBus(MRI, *MO, OpInfo)) { 4691 if (isVOP3(MI) && isLiteralConstantLike(*MO, OpInfo) && !VOP3LiteralLimit--) 4692 return false; 4693 4694 SmallDenseSet<RegSubRegPair> SGPRsUsed; 4695 if (MO->isReg()) 4696 SGPRsUsed.insert(RegSubRegPair(MO->getReg(), MO->getSubReg())); 4697 4698 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 4699 if (i == OpIdx) 4700 continue; 4701 const MachineOperand &Op = MI.getOperand(i); 4702 if (Op.isReg()) { 4703 RegSubRegPair SGPR(Op.getReg(), Op.getSubReg()); 4704 if (!SGPRsUsed.count(SGPR) && 4705 usesConstantBus(MRI, Op, InstDesc.OpInfo[i])) { 4706 if (--ConstantBusLimit <= 0) 4707 return false; 4708 SGPRsUsed.insert(SGPR); 4709 } 4710 } else if (InstDesc.OpInfo[i].OperandType == AMDGPU::OPERAND_KIMM32) { 4711 if (--ConstantBusLimit <= 0) 4712 return false; 4713 } else if (isVOP3(MI) && AMDGPU::isSISrcOperand(InstDesc, i) && 4714 isLiteralConstantLike(Op, InstDesc.OpInfo[i])) { 4715 if (!VOP3LiteralLimit--) 4716 return false; 4717 if (--ConstantBusLimit <= 0) 4718 return false; 4719 } 4720 } 4721 } 4722 4723 if (MO->isReg()) { 4724 assert(DefinedRC); 4725 if (!isLegalRegOperand(MRI, OpInfo, *MO)) 4726 return false; 4727 bool IsAGPR = RI.isAGPR(MRI, MO->getReg()); 4728 if (IsAGPR && !ST.hasMAIInsts()) 4729 return false; 4730 unsigned Opc = MI.getOpcode(); 4731 if (IsAGPR && 4732 (!ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) && 4733 (MI.mayLoad() || MI.mayStore() || isDS(Opc) || isMIMG(Opc))) 4734 return false; 4735 // Atomics should have both vdst and vdata either vgpr or agpr. 4736 const int VDstIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 4737 const int DataIdx = AMDGPU::getNamedOperandIdx(Opc, 4738 isDS(Opc) ? AMDGPU::OpName::data0 : AMDGPU::OpName::vdata); 4739 if ((int)OpIdx == VDstIdx && DataIdx != -1 && 4740 MI.getOperand(DataIdx).isReg() && 4741 RI.isAGPR(MRI, MI.getOperand(DataIdx).getReg()) != IsAGPR) 4742 return false; 4743 if ((int)OpIdx == DataIdx) { 4744 if (VDstIdx != -1 && 4745 RI.isAGPR(MRI, MI.getOperand(VDstIdx).getReg()) != IsAGPR) 4746 return false; 4747 // DS instructions with 2 src operands also must have tied RC. 4748 const int Data1Idx = AMDGPU::getNamedOperandIdx(Opc, 4749 AMDGPU::OpName::data1); 4750 if (Data1Idx != -1 && MI.getOperand(Data1Idx).isReg() && 4751 RI.isAGPR(MRI, MI.getOperand(Data1Idx).getReg()) != IsAGPR) 4752 return false; 4753 } 4754 if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64 && 4755 (int)OpIdx == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) && 4756 RI.isSGPRReg(MRI, MO->getReg())) 4757 return false; 4758 return true; 4759 } 4760 4761 // Handle non-register types that are treated like immediates. 4762 assert(MO->isImm() || MO->isTargetIndex() || MO->isFI() || MO->isGlobal()); 4763 4764 if (!DefinedRC) { 4765 // This operand expects an immediate. 4766 return true; 4767 } 4768 4769 return isImmOperandLegal(MI, OpIdx, *MO); 4770 } 4771 4772 void SIInstrInfo::legalizeOperandsVOP2(MachineRegisterInfo &MRI, 4773 MachineInstr &MI) const { 4774 unsigned Opc = MI.getOpcode(); 4775 const MCInstrDesc &InstrDesc = get(Opc); 4776 4777 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 4778 MachineOperand &Src0 = MI.getOperand(Src0Idx); 4779 4780 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 4781 MachineOperand &Src1 = MI.getOperand(Src1Idx); 4782 4783 // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32 4784 // we need to only have one constant bus use before GFX10. 4785 bool HasImplicitSGPR = findImplicitSGPRRead(MI) != AMDGPU::NoRegister; 4786 if (HasImplicitSGPR && ST.getConstantBusLimit(Opc) <= 1 && 4787 Src0.isReg() && (RI.isSGPRReg(MRI, Src0.getReg()) || 4788 isLiteralConstantLike(Src0, InstrDesc.OpInfo[Src0Idx]))) 4789 legalizeOpWithMove(MI, Src0Idx); 4790 4791 // Special case: V_WRITELANE_B32 accepts only immediate or SGPR operands for 4792 // both the value to write (src0) and lane select (src1). Fix up non-SGPR 4793 // src0/src1 with V_READFIRSTLANE. 4794 if (Opc == AMDGPU::V_WRITELANE_B32) { 4795 const DebugLoc &DL = MI.getDebugLoc(); 4796 if (Src0.isReg() && RI.isVGPR(MRI, Src0.getReg())) { 4797 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4798 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 4799 .add(Src0); 4800 Src0.ChangeToRegister(Reg, false); 4801 } 4802 if (Src1.isReg() && RI.isVGPR(MRI, Src1.getReg())) { 4803 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4804 const DebugLoc &DL = MI.getDebugLoc(); 4805 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 4806 .add(Src1); 4807 Src1.ChangeToRegister(Reg, false); 4808 } 4809 return; 4810 } 4811 4812 // No VOP2 instructions support AGPRs. 4813 if (Src0.isReg() && RI.isAGPR(MRI, Src0.getReg())) 4814 legalizeOpWithMove(MI, Src0Idx); 4815 4816 if (Src1.isReg() && RI.isAGPR(MRI, Src1.getReg())) 4817 legalizeOpWithMove(MI, Src1Idx); 4818 4819 // VOP2 src0 instructions support all operand types, so we don't need to check 4820 // their legality. If src1 is already legal, we don't need to do anything. 4821 if (isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src1)) 4822 return; 4823 4824 // Special case: V_READLANE_B32 accepts only immediate or SGPR operands for 4825 // lane select. Fix up using V_READFIRSTLANE, since we assume that the lane 4826 // select is uniform. 4827 if (Opc == AMDGPU::V_READLANE_B32 && Src1.isReg() && 4828 RI.isVGPR(MRI, Src1.getReg())) { 4829 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4830 const DebugLoc &DL = MI.getDebugLoc(); 4831 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 4832 .add(Src1); 4833 Src1.ChangeToRegister(Reg, false); 4834 return; 4835 } 4836 4837 // We do not use commuteInstruction here because it is too aggressive and will 4838 // commute if it is possible. We only want to commute here if it improves 4839 // legality. This can be called a fairly large number of times so don't waste 4840 // compile time pointlessly swapping and checking legality again. 4841 if (HasImplicitSGPR || !MI.isCommutable()) { 4842 legalizeOpWithMove(MI, Src1Idx); 4843 return; 4844 } 4845 4846 // If src0 can be used as src1, commuting will make the operands legal. 4847 // Otherwise we have to give up and insert a move. 4848 // 4849 // TODO: Other immediate-like operand kinds could be commuted if there was a 4850 // MachineOperand::ChangeTo* for them. 4851 if ((!Src1.isImm() && !Src1.isReg()) || 4852 !isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src0)) { 4853 legalizeOpWithMove(MI, Src1Idx); 4854 return; 4855 } 4856 4857 int CommutedOpc = commuteOpcode(MI); 4858 if (CommutedOpc == -1) { 4859 legalizeOpWithMove(MI, Src1Idx); 4860 return; 4861 } 4862 4863 MI.setDesc(get(CommutedOpc)); 4864 4865 Register Src0Reg = Src0.getReg(); 4866 unsigned Src0SubReg = Src0.getSubReg(); 4867 bool Src0Kill = Src0.isKill(); 4868 4869 if (Src1.isImm()) 4870 Src0.ChangeToImmediate(Src1.getImm()); 4871 else if (Src1.isReg()) { 4872 Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill()); 4873 Src0.setSubReg(Src1.getSubReg()); 4874 } else 4875 llvm_unreachable("Should only have register or immediate operands"); 4876 4877 Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill); 4878 Src1.setSubReg(Src0SubReg); 4879 fixImplicitOperands(MI); 4880 } 4881 4882 // Legalize VOP3 operands. All operand types are supported for any operand 4883 // but only one literal constant and only starting from GFX10. 4884 void SIInstrInfo::legalizeOperandsVOP3(MachineRegisterInfo &MRI, 4885 MachineInstr &MI) const { 4886 unsigned Opc = MI.getOpcode(); 4887 4888 int VOP3Idx[3] = { 4889 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0), 4890 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1), 4891 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2) 4892 }; 4893 4894 if (Opc == AMDGPU::V_PERMLANE16_B32_e64 || 4895 Opc == AMDGPU::V_PERMLANEX16_B32_e64) { 4896 // src1 and src2 must be scalar 4897 MachineOperand &Src1 = MI.getOperand(VOP3Idx[1]); 4898 MachineOperand &Src2 = MI.getOperand(VOP3Idx[2]); 4899 const DebugLoc &DL = MI.getDebugLoc(); 4900 if (Src1.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) { 4901 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4902 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 4903 .add(Src1); 4904 Src1.ChangeToRegister(Reg, false); 4905 } 4906 if (Src2.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src2.getReg()))) { 4907 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4908 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 4909 .add(Src2); 4910 Src2.ChangeToRegister(Reg, false); 4911 } 4912 } 4913 4914 // Find the one SGPR operand we are allowed to use. 4915 int ConstantBusLimit = ST.getConstantBusLimit(Opc); 4916 int LiteralLimit = ST.hasVOP3Literal() ? 1 : 0; 4917 SmallDenseSet<unsigned> SGPRsUsed; 4918 Register SGPRReg = findUsedSGPR(MI, VOP3Idx); 4919 if (SGPRReg != AMDGPU::NoRegister) { 4920 SGPRsUsed.insert(SGPRReg); 4921 --ConstantBusLimit; 4922 } 4923 4924 for (unsigned i = 0; i < 3; ++i) { 4925 int Idx = VOP3Idx[i]; 4926 if (Idx == -1) 4927 break; 4928 MachineOperand &MO = MI.getOperand(Idx); 4929 4930 if (!MO.isReg()) { 4931 if (!isLiteralConstantLike(MO, get(Opc).OpInfo[Idx])) 4932 continue; 4933 4934 if (LiteralLimit > 0 && ConstantBusLimit > 0) { 4935 --LiteralLimit; 4936 --ConstantBusLimit; 4937 continue; 4938 } 4939 4940 --LiteralLimit; 4941 --ConstantBusLimit; 4942 legalizeOpWithMove(MI, Idx); 4943 continue; 4944 } 4945 4946 if (RI.hasAGPRs(MRI.getRegClass(MO.getReg())) && 4947 !isOperandLegal(MI, Idx, &MO)) { 4948 legalizeOpWithMove(MI, Idx); 4949 continue; 4950 } 4951 4952 if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg()))) 4953 continue; // VGPRs are legal 4954 4955 // We can use one SGPR in each VOP3 instruction prior to GFX10 4956 // and two starting from GFX10. 4957 if (SGPRsUsed.count(MO.getReg())) 4958 continue; 4959 if (ConstantBusLimit > 0) { 4960 SGPRsUsed.insert(MO.getReg()); 4961 --ConstantBusLimit; 4962 continue; 4963 } 4964 4965 // If we make it this far, then the operand is not legal and we must 4966 // legalize it. 4967 legalizeOpWithMove(MI, Idx); 4968 } 4969 } 4970 4971 Register SIInstrInfo::readlaneVGPRToSGPR(Register SrcReg, MachineInstr &UseMI, 4972 MachineRegisterInfo &MRI) const { 4973 const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg); 4974 const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC); 4975 Register DstReg = MRI.createVirtualRegister(SRC); 4976 unsigned SubRegs = RI.getRegSizeInBits(*VRC) / 32; 4977 4978 if (RI.hasAGPRs(VRC)) { 4979 VRC = RI.getEquivalentVGPRClass(VRC); 4980 Register NewSrcReg = MRI.createVirtualRegister(VRC); 4981 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 4982 get(TargetOpcode::COPY), NewSrcReg) 4983 .addReg(SrcReg); 4984 SrcReg = NewSrcReg; 4985 } 4986 4987 if (SubRegs == 1) { 4988 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 4989 get(AMDGPU::V_READFIRSTLANE_B32), DstReg) 4990 .addReg(SrcReg); 4991 return DstReg; 4992 } 4993 4994 SmallVector<unsigned, 8> SRegs; 4995 for (unsigned i = 0; i < SubRegs; ++i) { 4996 Register SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 4997 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 4998 get(AMDGPU::V_READFIRSTLANE_B32), SGPR) 4999 .addReg(SrcReg, 0, RI.getSubRegFromChannel(i)); 5000 SRegs.push_back(SGPR); 5001 } 5002 5003 MachineInstrBuilder MIB = 5004 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5005 get(AMDGPU::REG_SEQUENCE), DstReg); 5006 for (unsigned i = 0; i < SubRegs; ++i) { 5007 MIB.addReg(SRegs[i]); 5008 MIB.addImm(RI.getSubRegFromChannel(i)); 5009 } 5010 return DstReg; 5011 } 5012 5013 void SIInstrInfo::legalizeOperandsSMRD(MachineRegisterInfo &MRI, 5014 MachineInstr &MI) const { 5015 5016 // If the pointer is store in VGPRs, then we need to move them to 5017 // SGPRs using v_readfirstlane. This is safe because we only select 5018 // loads with uniform pointers to SMRD instruction so we know the 5019 // pointer value is uniform. 5020 MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase); 5021 if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) { 5022 Register SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI); 5023 SBase->setReg(SGPR); 5024 } 5025 MachineOperand *SOff = getNamedOperand(MI, AMDGPU::OpName::soff); 5026 if (SOff && !RI.isSGPRClass(MRI.getRegClass(SOff->getReg()))) { 5027 Register SGPR = readlaneVGPRToSGPR(SOff->getReg(), MI, MRI); 5028 SOff->setReg(SGPR); 5029 } 5030 } 5031 5032 // FIXME: Remove this when SelectionDAG is obsoleted. 5033 void SIInstrInfo::legalizeOperandsFLAT(MachineRegisterInfo &MRI, 5034 MachineInstr &MI) const { 5035 if (!isSegmentSpecificFLAT(MI)) 5036 return; 5037 5038 // Fixup SGPR operands in VGPRs. We only select these when the DAG divergence 5039 // thinks they are uniform, so a readfirstlane should be valid. 5040 MachineOperand *SAddr = getNamedOperand(MI, AMDGPU::OpName::saddr); 5041 if (!SAddr || RI.isSGPRClass(MRI.getRegClass(SAddr->getReg()))) 5042 return; 5043 5044 Register ToSGPR = readlaneVGPRToSGPR(SAddr->getReg(), MI, MRI); 5045 SAddr->setReg(ToSGPR); 5046 } 5047 5048 void SIInstrInfo::legalizeGenericOperand(MachineBasicBlock &InsertMBB, 5049 MachineBasicBlock::iterator I, 5050 const TargetRegisterClass *DstRC, 5051 MachineOperand &Op, 5052 MachineRegisterInfo &MRI, 5053 const DebugLoc &DL) const { 5054 Register OpReg = Op.getReg(); 5055 unsigned OpSubReg = Op.getSubReg(); 5056 5057 const TargetRegisterClass *OpRC = RI.getSubClassWithSubReg( 5058 RI.getRegClassForReg(MRI, OpReg), OpSubReg); 5059 5060 // Check if operand is already the correct register class. 5061 if (DstRC == OpRC) 5062 return; 5063 5064 Register DstReg = MRI.createVirtualRegister(DstRC); 5065 MachineInstr *Copy = 5066 BuildMI(InsertMBB, I, DL, get(AMDGPU::COPY), DstReg).add(Op); 5067 5068 Op.setReg(DstReg); 5069 Op.setSubReg(0); 5070 5071 MachineInstr *Def = MRI.getVRegDef(OpReg); 5072 if (!Def) 5073 return; 5074 5075 // Try to eliminate the copy if it is copying an immediate value. 5076 if (Def->isMoveImmediate() && DstRC != &AMDGPU::VReg_1RegClass) 5077 FoldImmediate(*Copy, *Def, OpReg, &MRI); 5078 5079 bool ImpDef = Def->isImplicitDef(); 5080 while (!ImpDef && Def && Def->isCopy()) { 5081 if (Def->getOperand(1).getReg().isPhysical()) 5082 break; 5083 Def = MRI.getUniqueVRegDef(Def->getOperand(1).getReg()); 5084 ImpDef = Def && Def->isImplicitDef(); 5085 } 5086 if (!RI.isSGPRClass(DstRC) && !Copy->readsRegister(AMDGPU::EXEC, &RI) && 5087 !ImpDef) 5088 Copy->addOperand(MachineOperand::CreateReg(AMDGPU::EXEC, false, true)); 5089 } 5090 5091 // Emit the actual waterfall loop, executing the wrapped instruction for each 5092 // unique value of \p Rsrc across all lanes. In the best case we execute 1 5093 // iteration, in the worst case we execute 64 (once per lane). 5094 static void 5095 emitLoadSRsrcFromVGPRLoop(const SIInstrInfo &TII, MachineRegisterInfo &MRI, 5096 MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB, 5097 const DebugLoc &DL, MachineOperand &Rsrc) { 5098 MachineFunction &MF = *OrigBB.getParent(); 5099 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 5100 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 5101 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 5102 unsigned SaveExecOpc = 5103 ST.isWave32() ? AMDGPU::S_AND_SAVEEXEC_B32 : AMDGPU::S_AND_SAVEEXEC_B64; 5104 unsigned XorTermOpc = 5105 ST.isWave32() ? AMDGPU::S_XOR_B32_term : AMDGPU::S_XOR_B64_term; 5106 unsigned AndOpc = 5107 ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64; 5108 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 5109 5110 MachineBasicBlock::iterator I = LoopBB.begin(); 5111 5112 SmallVector<Register, 8> ReadlanePieces; 5113 Register CondReg = AMDGPU::NoRegister; 5114 5115 Register VRsrc = Rsrc.getReg(); 5116 unsigned VRsrcUndef = getUndefRegState(Rsrc.isUndef()); 5117 5118 unsigned RegSize = TRI->getRegSizeInBits(Rsrc.getReg(), MRI); 5119 unsigned NumSubRegs = RegSize / 32; 5120 assert(NumSubRegs % 2 == 0 && NumSubRegs <= 32 && "Unhandled register size"); 5121 5122 for (unsigned Idx = 0; Idx < NumSubRegs; Idx += 2) { 5123 5124 Register CurRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5125 Register CurRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5126 5127 // Read the next variant <- also loop target. 5128 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegLo) 5129 .addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx)); 5130 5131 // Read the next variant <- also loop target. 5132 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegHi) 5133 .addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx + 1)); 5134 5135 ReadlanePieces.push_back(CurRegLo); 5136 ReadlanePieces.push_back(CurRegHi); 5137 5138 // Comparison is to be done as 64-bit. 5139 Register CurReg = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass); 5140 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), CurReg) 5141 .addReg(CurRegLo) 5142 .addImm(AMDGPU::sub0) 5143 .addReg(CurRegHi) 5144 .addImm(AMDGPU::sub1); 5145 5146 Register NewCondReg = MRI.createVirtualRegister(BoolXExecRC); 5147 auto Cmp = 5148 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64), NewCondReg) 5149 .addReg(CurReg); 5150 if (NumSubRegs <= 2) 5151 Cmp.addReg(VRsrc); 5152 else 5153 Cmp.addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx, 2)); 5154 5155 // Combine the comparision results with AND. 5156 if (CondReg == AMDGPU::NoRegister) // First. 5157 CondReg = NewCondReg; 5158 else { // If not the first, we create an AND. 5159 Register AndReg = MRI.createVirtualRegister(BoolXExecRC); 5160 BuildMI(LoopBB, I, DL, TII.get(AndOpc), AndReg) 5161 .addReg(CondReg) 5162 .addReg(NewCondReg); 5163 CondReg = AndReg; 5164 } 5165 } // End for loop. 5166 5167 auto SRsrcRC = TRI->getEquivalentSGPRClass(MRI.getRegClass(VRsrc)); 5168 Register SRsrc = MRI.createVirtualRegister(SRsrcRC); 5169 5170 // Build scalar Rsrc. 5171 auto Merge = BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), SRsrc); 5172 unsigned Channel = 0; 5173 for (Register Piece : ReadlanePieces) { 5174 Merge.addReg(Piece) 5175 .addImm(TRI->getSubRegFromChannel(Channel++)); 5176 } 5177 5178 // Update Rsrc operand to use the SGPR Rsrc. 5179 Rsrc.setReg(SRsrc); 5180 Rsrc.setIsKill(true); 5181 5182 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC); 5183 MRI.setSimpleHint(SaveExec, CondReg); 5184 5185 // Update EXEC to matching lanes, saving original to SaveExec. 5186 BuildMI(LoopBB, I, DL, TII.get(SaveExecOpc), SaveExec) 5187 .addReg(CondReg, RegState::Kill); 5188 5189 // The original instruction is here; we insert the terminators after it. 5190 I = LoopBB.end(); 5191 5192 // Update EXEC, switch all done bits to 0 and all todo bits to 1. 5193 BuildMI(LoopBB, I, DL, TII.get(XorTermOpc), Exec) 5194 .addReg(Exec) 5195 .addReg(SaveExec); 5196 5197 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::S_CBRANCH_EXECNZ)).addMBB(&LoopBB); 5198 } 5199 5200 // Build a waterfall loop around \p MI, replacing the VGPR \p Rsrc register 5201 // with SGPRs by iterating over all unique values across all lanes. 5202 // Returns the loop basic block that now contains \p MI. 5203 static MachineBasicBlock * 5204 loadSRsrcFromVGPR(const SIInstrInfo &TII, MachineInstr &MI, 5205 MachineOperand &Rsrc, MachineDominatorTree *MDT, 5206 MachineBasicBlock::iterator Begin = nullptr, 5207 MachineBasicBlock::iterator End = nullptr) { 5208 MachineBasicBlock &MBB = *MI.getParent(); 5209 MachineFunction &MF = *MBB.getParent(); 5210 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 5211 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 5212 MachineRegisterInfo &MRI = MF.getRegInfo(); 5213 if (!Begin.isValid()) 5214 Begin = &MI; 5215 if (!End.isValid()) { 5216 End = &MI; 5217 ++End; 5218 } 5219 const DebugLoc &DL = MI.getDebugLoc(); 5220 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 5221 unsigned MovExecOpc = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64; 5222 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 5223 5224 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC); 5225 5226 // Save the EXEC mask 5227 BuildMI(MBB, Begin, DL, TII.get(MovExecOpc), SaveExec).addReg(Exec); 5228 5229 // Killed uses in the instruction we are waterfalling around will be 5230 // incorrect due to the added control-flow. 5231 MachineBasicBlock::iterator AfterMI = MI; 5232 ++AfterMI; 5233 for (auto I = Begin; I != AfterMI; I++) { 5234 for (auto &MO : I->uses()) { 5235 if (MO.isReg() && MO.isUse()) { 5236 MRI.clearKillFlags(MO.getReg()); 5237 } 5238 } 5239 } 5240 5241 // To insert the loop we need to split the block. Move everything after this 5242 // point to a new block, and insert a new empty block between the two. 5243 MachineBasicBlock *LoopBB = MF.CreateMachineBasicBlock(); 5244 MachineBasicBlock *RemainderBB = MF.CreateMachineBasicBlock(); 5245 MachineFunction::iterator MBBI(MBB); 5246 ++MBBI; 5247 5248 MF.insert(MBBI, LoopBB); 5249 MF.insert(MBBI, RemainderBB); 5250 5251 LoopBB->addSuccessor(LoopBB); 5252 LoopBB->addSuccessor(RemainderBB); 5253 5254 // Move Begin to MI to the LoopBB, and the remainder of the block to 5255 // RemainderBB. 5256 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); 5257 RemainderBB->splice(RemainderBB->begin(), &MBB, End, MBB.end()); 5258 LoopBB->splice(LoopBB->begin(), &MBB, Begin, MBB.end()); 5259 5260 MBB.addSuccessor(LoopBB); 5261 5262 // Update dominators. We know that MBB immediately dominates LoopBB, that 5263 // LoopBB immediately dominates RemainderBB, and that RemainderBB immediately 5264 // dominates all of the successors transferred to it from MBB that MBB used 5265 // to properly dominate. 5266 if (MDT) { 5267 MDT->addNewBlock(LoopBB, &MBB); 5268 MDT->addNewBlock(RemainderBB, LoopBB); 5269 for (auto &Succ : RemainderBB->successors()) { 5270 if (MDT->properlyDominates(&MBB, Succ)) { 5271 MDT->changeImmediateDominator(Succ, RemainderBB); 5272 } 5273 } 5274 } 5275 5276 emitLoadSRsrcFromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, Rsrc); 5277 5278 // Restore the EXEC mask 5279 MachineBasicBlock::iterator First = RemainderBB->begin(); 5280 BuildMI(*RemainderBB, First, DL, TII.get(MovExecOpc), Exec).addReg(SaveExec); 5281 return LoopBB; 5282 } 5283 5284 // Extract pointer from Rsrc and return a zero-value Rsrc replacement. 5285 static std::tuple<unsigned, unsigned> 5286 extractRsrcPtr(const SIInstrInfo &TII, MachineInstr &MI, MachineOperand &Rsrc) { 5287 MachineBasicBlock &MBB = *MI.getParent(); 5288 MachineFunction &MF = *MBB.getParent(); 5289 MachineRegisterInfo &MRI = MF.getRegInfo(); 5290 5291 // Extract the ptr from the resource descriptor. 5292 unsigned RsrcPtr = 5293 TII.buildExtractSubReg(MI, MRI, Rsrc, &AMDGPU::VReg_128RegClass, 5294 AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass); 5295 5296 // Create an empty resource descriptor 5297 Register Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 5298 Register SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5299 Register SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5300 Register NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SGPR_128RegClass); 5301 uint64_t RsrcDataFormat = TII.getDefaultRsrcDataFormat(); 5302 5303 // Zero64 = 0 5304 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B64), Zero64) 5305 .addImm(0); 5306 5307 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0} 5308 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatLo) 5309 .addImm(RsrcDataFormat & 0xFFFFFFFF); 5310 5311 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32} 5312 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatHi) 5313 .addImm(RsrcDataFormat >> 32); 5314 5315 // NewSRsrc = {Zero64, SRsrcFormat} 5316 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::REG_SEQUENCE), NewSRsrc) 5317 .addReg(Zero64) 5318 .addImm(AMDGPU::sub0_sub1) 5319 .addReg(SRsrcFormatLo) 5320 .addImm(AMDGPU::sub2) 5321 .addReg(SRsrcFormatHi) 5322 .addImm(AMDGPU::sub3); 5323 5324 return std::make_tuple(RsrcPtr, NewSRsrc); 5325 } 5326 5327 MachineBasicBlock * 5328 SIInstrInfo::legalizeOperands(MachineInstr &MI, 5329 MachineDominatorTree *MDT) const { 5330 MachineFunction &MF = *MI.getParent()->getParent(); 5331 MachineRegisterInfo &MRI = MF.getRegInfo(); 5332 MachineBasicBlock *CreatedBB = nullptr; 5333 5334 // Legalize VOP2 5335 if (isVOP2(MI) || isVOPC(MI)) { 5336 legalizeOperandsVOP2(MRI, MI); 5337 return CreatedBB; 5338 } 5339 5340 // Legalize VOP3 5341 if (isVOP3(MI)) { 5342 legalizeOperandsVOP3(MRI, MI); 5343 return CreatedBB; 5344 } 5345 5346 // Legalize SMRD 5347 if (isSMRD(MI)) { 5348 legalizeOperandsSMRD(MRI, MI); 5349 return CreatedBB; 5350 } 5351 5352 // Legalize FLAT 5353 if (isFLAT(MI)) { 5354 legalizeOperandsFLAT(MRI, MI); 5355 return CreatedBB; 5356 } 5357 5358 // Legalize REG_SEQUENCE and PHI 5359 // The register class of the operands much be the same type as the register 5360 // class of the output. 5361 if (MI.getOpcode() == AMDGPU::PHI) { 5362 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr; 5363 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { 5364 if (!MI.getOperand(i).isReg() || !MI.getOperand(i).getReg().isVirtual()) 5365 continue; 5366 const TargetRegisterClass *OpRC = 5367 MRI.getRegClass(MI.getOperand(i).getReg()); 5368 if (RI.hasVectorRegisters(OpRC)) { 5369 VRC = OpRC; 5370 } else { 5371 SRC = OpRC; 5372 } 5373 } 5374 5375 // If any of the operands are VGPR registers, then they all most be 5376 // otherwise we will create illegal VGPR->SGPR copies when legalizing 5377 // them. 5378 if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) { 5379 if (!VRC) { 5380 assert(SRC); 5381 if (getOpRegClass(MI, 0) == &AMDGPU::VReg_1RegClass) { 5382 VRC = &AMDGPU::VReg_1RegClass; 5383 } else 5384 VRC = RI.hasAGPRs(getOpRegClass(MI, 0)) 5385 ? RI.getEquivalentAGPRClass(SRC) 5386 : RI.getEquivalentVGPRClass(SRC); 5387 } else { 5388 VRC = RI.hasAGPRs(getOpRegClass(MI, 0)) 5389 ? RI.getEquivalentAGPRClass(VRC) 5390 : RI.getEquivalentVGPRClass(VRC); 5391 } 5392 RC = VRC; 5393 } else { 5394 RC = SRC; 5395 } 5396 5397 // Update all the operands so they have the same type. 5398 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 5399 MachineOperand &Op = MI.getOperand(I); 5400 if (!Op.isReg() || !Op.getReg().isVirtual()) 5401 continue; 5402 5403 // MI is a PHI instruction. 5404 MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB(); 5405 MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator(); 5406 5407 // Avoid creating no-op copies with the same src and dst reg class. These 5408 // confuse some of the machine passes. 5409 legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc()); 5410 } 5411 } 5412 5413 // REG_SEQUENCE doesn't really require operand legalization, but if one has a 5414 // VGPR dest type and SGPR sources, insert copies so all operands are 5415 // VGPRs. This seems to help operand folding / the register coalescer. 5416 if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) { 5417 MachineBasicBlock *MBB = MI.getParent(); 5418 const TargetRegisterClass *DstRC = getOpRegClass(MI, 0); 5419 if (RI.hasVGPRs(DstRC)) { 5420 // Update all the operands so they are VGPR register classes. These may 5421 // not be the same register class because REG_SEQUENCE supports mixing 5422 // subregister index types e.g. sub0_sub1 + sub2 + sub3 5423 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 5424 MachineOperand &Op = MI.getOperand(I); 5425 if (!Op.isReg() || !Op.getReg().isVirtual()) 5426 continue; 5427 5428 const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg()); 5429 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC); 5430 if (VRC == OpRC) 5431 continue; 5432 5433 legalizeGenericOperand(*MBB, MI, VRC, Op, MRI, MI.getDebugLoc()); 5434 Op.setIsKill(); 5435 } 5436 } 5437 5438 return CreatedBB; 5439 } 5440 5441 // Legalize INSERT_SUBREG 5442 // src0 must have the same register class as dst 5443 if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) { 5444 Register Dst = MI.getOperand(0).getReg(); 5445 Register Src0 = MI.getOperand(1).getReg(); 5446 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst); 5447 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0); 5448 if (DstRC != Src0RC) { 5449 MachineBasicBlock *MBB = MI.getParent(); 5450 MachineOperand &Op = MI.getOperand(1); 5451 legalizeGenericOperand(*MBB, MI, DstRC, Op, MRI, MI.getDebugLoc()); 5452 } 5453 return CreatedBB; 5454 } 5455 5456 // Legalize SI_INIT_M0 5457 if (MI.getOpcode() == AMDGPU::SI_INIT_M0) { 5458 MachineOperand &Src = MI.getOperand(0); 5459 if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg()))) 5460 Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI)); 5461 return CreatedBB; 5462 } 5463 5464 // Legalize MIMG and MUBUF/MTBUF for shaders. 5465 // 5466 // Shaders only generate MUBUF/MTBUF instructions via intrinsics or via 5467 // scratch memory access. In both cases, the legalization never involves 5468 // conversion to the addr64 form. 5469 if (isMIMG(MI) || (AMDGPU::isGraphics(MF.getFunction().getCallingConv()) && 5470 (isMUBUF(MI) || isMTBUF(MI)))) { 5471 MachineOperand *SRsrc = getNamedOperand(MI, AMDGPU::OpName::srsrc); 5472 if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg()))) 5473 CreatedBB = loadSRsrcFromVGPR(*this, MI, *SRsrc, MDT); 5474 5475 MachineOperand *SSamp = getNamedOperand(MI, AMDGPU::OpName::ssamp); 5476 if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg()))) 5477 CreatedBB = loadSRsrcFromVGPR(*this, MI, *SSamp, MDT); 5478 5479 return CreatedBB; 5480 } 5481 5482 // Legalize SI_CALL 5483 if (MI.getOpcode() == AMDGPU::SI_CALL_ISEL) { 5484 MachineOperand *Dest = &MI.getOperand(0); 5485 if (!RI.isSGPRClass(MRI.getRegClass(Dest->getReg()))) { 5486 // Move everything between ADJCALLSTACKUP and ADJCALLSTACKDOWN and 5487 // following copies, we also need to move copies from and to physical 5488 // registers into the loop block. 5489 unsigned FrameSetupOpcode = getCallFrameSetupOpcode(); 5490 unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode(); 5491 5492 // Also move the copies to physical registers into the loop block 5493 MachineBasicBlock &MBB = *MI.getParent(); 5494 MachineBasicBlock::iterator Start(&MI); 5495 while (Start->getOpcode() != FrameSetupOpcode) 5496 --Start; 5497 MachineBasicBlock::iterator End(&MI); 5498 while (End->getOpcode() != FrameDestroyOpcode) 5499 ++End; 5500 // Also include following copies of the return value 5501 ++End; 5502 while (End != MBB.end() && End->isCopy() && End->getOperand(1).isReg() && 5503 MI.definesRegister(End->getOperand(1).getReg())) 5504 ++End; 5505 CreatedBB = loadSRsrcFromVGPR(*this, MI, *Dest, MDT, Start, End); 5506 } 5507 } 5508 5509 // Legalize MUBUF* instructions. 5510 int RsrcIdx = 5511 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc); 5512 if (RsrcIdx != -1) { 5513 // We have an MUBUF instruction 5514 MachineOperand *Rsrc = &MI.getOperand(RsrcIdx); 5515 unsigned RsrcRC = get(MI.getOpcode()).OpInfo[RsrcIdx].RegClass; 5516 if (RI.getCommonSubClass(MRI.getRegClass(Rsrc->getReg()), 5517 RI.getRegClass(RsrcRC))) { 5518 // The operands are legal. 5519 // FIXME: We may need to legalize operands besided srsrc. 5520 return CreatedBB; 5521 } 5522 5523 // Legalize a VGPR Rsrc. 5524 // 5525 // If the instruction is _ADDR64, we can avoid a waterfall by extracting 5526 // the base pointer from the VGPR Rsrc, adding it to the VAddr, then using 5527 // a zero-value SRsrc. 5528 // 5529 // If the instruction is _OFFSET (both idxen and offen disabled), and we 5530 // support ADDR64 instructions, we can convert to ADDR64 and do the same as 5531 // above. 5532 // 5533 // Otherwise we are on non-ADDR64 hardware, and/or we have 5534 // idxen/offen/bothen and we fall back to a waterfall loop. 5535 5536 MachineBasicBlock &MBB = *MI.getParent(); 5537 5538 MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 5539 if (VAddr && AMDGPU::getIfAddr64Inst(MI.getOpcode()) != -1) { 5540 // This is already an ADDR64 instruction so we need to add the pointer 5541 // extracted from the resource descriptor to the current value of VAddr. 5542 Register NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 5543 Register NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 5544 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 5545 5546 const auto *BoolXExecRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 5547 Register CondReg0 = MRI.createVirtualRegister(BoolXExecRC); 5548 Register CondReg1 = MRI.createVirtualRegister(BoolXExecRC); 5549 5550 unsigned RsrcPtr, NewSRsrc; 5551 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc); 5552 5553 // NewVaddrLo = RsrcPtr:sub0 + VAddr:sub0 5554 const DebugLoc &DL = MI.getDebugLoc(); 5555 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_CO_U32_e64), NewVAddrLo) 5556 .addDef(CondReg0) 5557 .addReg(RsrcPtr, 0, AMDGPU::sub0) 5558 .addReg(VAddr->getReg(), 0, AMDGPU::sub0) 5559 .addImm(0); 5560 5561 // NewVaddrHi = RsrcPtr:sub1 + VAddr:sub1 5562 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e64), NewVAddrHi) 5563 .addDef(CondReg1, RegState::Dead) 5564 .addReg(RsrcPtr, 0, AMDGPU::sub1) 5565 .addReg(VAddr->getReg(), 0, AMDGPU::sub1) 5566 .addReg(CondReg0, RegState::Kill) 5567 .addImm(0); 5568 5569 // NewVaddr = {NewVaddrHi, NewVaddrLo} 5570 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr) 5571 .addReg(NewVAddrLo) 5572 .addImm(AMDGPU::sub0) 5573 .addReg(NewVAddrHi) 5574 .addImm(AMDGPU::sub1); 5575 5576 VAddr->setReg(NewVAddr); 5577 Rsrc->setReg(NewSRsrc); 5578 } else if (!VAddr && ST.hasAddr64()) { 5579 // This instructions is the _OFFSET variant, so we need to convert it to 5580 // ADDR64. 5581 assert(ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS && 5582 "FIXME: Need to emit flat atomics here"); 5583 5584 unsigned RsrcPtr, NewSRsrc; 5585 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc); 5586 5587 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 5588 MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata); 5589 MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 5590 MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset); 5591 unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode()); 5592 5593 // Atomics rith return have have an additional tied operand and are 5594 // missing some of the special bits. 5595 MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in); 5596 MachineInstr *Addr64; 5597 5598 if (!VDataIn) { 5599 // Regular buffer load / store. 5600 MachineInstrBuilder MIB = 5601 BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 5602 .add(*VData) 5603 .addReg(NewVAddr) 5604 .addReg(NewSRsrc) 5605 .add(*SOffset) 5606 .add(*Offset); 5607 5608 // Atomics do not have this operand. 5609 if (const MachineOperand *GLC = 5610 getNamedOperand(MI, AMDGPU::OpName::glc)) { 5611 MIB.addImm(GLC->getImm()); 5612 } 5613 if (const MachineOperand *DLC = 5614 getNamedOperand(MI, AMDGPU::OpName::dlc)) { 5615 MIB.addImm(DLC->getImm()); 5616 } 5617 if (const MachineOperand *SCCB = 5618 getNamedOperand(MI, AMDGPU::OpName::sccb)) { 5619 MIB.addImm(SCCB->getImm()); 5620 } 5621 5622 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::slc)); 5623 5624 if (const MachineOperand *TFE = 5625 getNamedOperand(MI, AMDGPU::OpName::tfe)) { 5626 MIB.addImm(TFE->getImm()); 5627 } 5628 5629 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::swz)); 5630 5631 MIB.cloneMemRefs(MI); 5632 Addr64 = MIB; 5633 } else { 5634 // Atomics with return. 5635 Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 5636 .add(*VData) 5637 .add(*VDataIn) 5638 .addReg(NewVAddr) 5639 .addReg(NewSRsrc) 5640 .add(*SOffset) 5641 .add(*Offset) 5642 .addImm(getNamedImmOperand(MI, AMDGPU::OpName::slc)) 5643 .cloneMemRefs(MI); 5644 } 5645 5646 MI.removeFromParent(); 5647 5648 // NewVaddr = {NewVaddrHi, NewVaddrLo} 5649 BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), 5650 NewVAddr) 5651 .addReg(RsrcPtr, 0, AMDGPU::sub0) 5652 .addImm(AMDGPU::sub0) 5653 .addReg(RsrcPtr, 0, AMDGPU::sub1) 5654 .addImm(AMDGPU::sub1); 5655 } else { 5656 // This is another variant; legalize Rsrc with waterfall loop from VGPRs 5657 // to SGPRs. 5658 CreatedBB = loadSRsrcFromVGPR(*this, MI, *Rsrc, MDT); 5659 return CreatedBB; 5660 } 5661 } 5662 return CreatedBB; 5663 } 5664 5665 MachineBasicBlock *SIInstrInfo::moveToVALU(MachineInstr &TopInst, 5666 MachineDominatorTree *MDT) const { 5667 SetVectorType Worklist; 5668 Worklist.insert(&TopInst); 5669 MachineBasicBlock *CreatedBB = nullptr; 5670 MachineBasicBlock *CreatedBBTmp = nullptr; 5671 5672 while (!Worklist.empty()) { 5673 MachineInstr &Inst = *Worklist.pop_back_val(); 5674 MachineBasicBlock *MBB = Inst.getParent(); 5675 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 5676 5677 unsigned Opcode = Inst.getOpcode(); 5678 unsigned NewOpcode = getVALUOp(Inst); 5679 5680 // Handle some special cases 5681 switch (Opcode) { 5682 default: 5683 break; 5684 case AMDGPU::S_ADD_U64_PSEUDO: 5685 case AMDGPU::S_SUB_U64_PSEUDO: 5686 splitScalar64BitAddSub(Worklist, Inst, MDT); 5687 Inst.eraseFromParent(); 5688 continue; 5689 case AMDGPU::S_ADD_I32: 5690 case AMDGPU::S_SUB_I32: { 5691 // FIXME: The u32 versions currently selected use the carry. 5692 bool Changed; 5693 std::tie(Changed, CreatedBBTmp) = moveScalarAddSub(Worklist, Inst, MDT); 5694 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 5695 CreatedBB = CreatedBBTmp; 5696 if (Changed) 5697 continue; 5698 5699 // Default handling 5700 break; 5701 } 5702 case AMDGPU::S_AND_B64: 5703 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32, MDT); 5704 Inst.eraseFromParent(); 5705 continue; 5706 5707 case AMDGPU::S_OR_B64: 5708 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32, MDT); 5709 Inst.eraseFromParent(); 5710 continue; 5711 5712 case AMDGPU::S_XOR_B64: 5713 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32, MDT); 5714 Inst.eraseFromParent(); 5715 continue; 5716 5717 case AMDGPU::S_NAND_B64: 5718 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NAND_B32, MDT); 5719 Inst.eraseFromParent(); 5720 continue; 5721 5722 case AMDGPU::S_NOR_B64: 5723 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NOR_B32, MDT); 5724 Inst.eraseFromParent(); 5725 continue; 5726 5727 case AMDGPU::S_XNOR_B64: 5728 if (ST.hasDLInsts()) 5729 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XNOR_B32, MDT); 5730 else 5731 splitScalar64BitXnor(Worklist, Inst, MDT); 5732 Inst.eraseFromParent(); 5733 continue; 5734 5735 case AMDGPU::S_ANDN2_B64: 5736 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ANDN2_B32, MDT); 5737 Inst.eraseFromParent(); 5738 continue; 5739 5740 case AMDGPU::S_ORN2_B64: 5741 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ORN2_B32, MDT); 5742 Inst.eraseFromParent(); 5743 continue; 5744 5745 case AMDGPU::S_BREV_B64: 5746 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_BREV_B32, true); 5747 Inst.eraseFromParent(); 5748 continue; 5749 5750 case AMDGPU::S_NOT_B64: 5751 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32); 5752 Inst.eraseFromParent(); 5753 continue; 5754 5755 case AMDGPU::S_BCNT1_I32_B64: 5756 splitScalar64BitBCNT(Worklist, Inst); 5757 Inst.eraseFromParent(); 5758 continue; 5759 5760 case AMDGPU::S_BFE_I64: 5761 splitScalar64BitBFE(Worklist, Inst); 5762 Inst.eraseFromParent(); 5763 continue; 5764 5765 case AMDGPU::S_LSHL_B32: 5766 if (ST.hasOnlyRevVALUShifts()) { 5767 NewOpcode = AMDGPU::V_LSHLREV_B32_e64; 5768 swapOperands(Inst); 5769 } 5770 break; 5771 case AMDGPU::S_ASHR_I32: 5772 if (ST.hasOnlyRevVALUShifts()) { 5773 NewOpcode = AMDGPU::V_ASHRREV_I32_e64; 5774 swapOperands(Inst); 5775 } 5776 break; 5777 case AMDGPU::S_LSHR_B32: 5778 if (ST.hasOnlyRevVALUShifts()) { 5779 NewOpcode = AMDGPU::V_LSHRREV_B32_e64; 5780 swapOperands(Inst); 5781 } 5782 break; 5783 case AMDGPU::S_LSHL_B64: 5784 if (ST.hasOnlyRevVALUShifts()) { 5785 NewOpcode = AMDGPU::V_LSHLREV_B64_e64; 5786 swapOperands(Inst); 5787 } 5788 break; 5789 case AMDGPU::S_ASHR_I64: 5790 if (ST.hasOnlyRevVALUShifts()) { 5791 NewOpcode = AMDGPU::V_ASHRREV_I64_e64; 5792 swapOperands(Inst); 5793 } 5794 break; 5795 case AMDGPU::S_LSHR_B64: 5796 if (ST.hasOnlyRevVALUShifts()) { 5797 NewOpcode = AMDGPU::V_LSHRREV_B64_e64; 5798 swapOperands(Inst); 5799 } 5800 break; 5801 5802 case AMDGPU::S_ABS_I32: 5803 lowerScalarAbs(Worklist, Inst); 5804 Inst.eraseFromParent(); 5805 continue; 5806 5807 case AMDGPU::S_CBRANCH_SCC0: 5808 case AMDGPU::S_CBRANCH_SCC1: 5809 // Clear unused bits of vcc 5810 if (ST.isWave32()) 5811 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(AMDGPU::S_AND_B32), 5812 AMDGPU::VCC_LO) 5813 .addReg(AMDGPU::EXEC_LO) 5814 .addReg(AMDGPU::VCC_LO); 5815 else 5816 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(AMDGPU::S_AND_B64), 5817 AMDGPU::VCC) 5818 .addReg(AMDGPU::EXEC) 5819 .addReg(AMDGPU::VCC); 5820 break; 5821 5822 case AMDGPU::S_BFE_U64: 5823 case AMDGPU::S_BFM_B64: 5824 llvm_unreachable("Moving this op to VALU not implemented"); 5825 5826 case AMDGPU::S_PACK_LL_B32_B16: 5827 case AMDGPU::S_PACK_LH_B32_B16: 5828 case AMDGPU::S_PACK_HH_B32_B16: 5829 movePackToVALU(Worklist, MRI, Inst); 5830 Inst.eraseFromParent(); 5831 continue; 5832 5833 case AMDGPU::S_XNOR_B32: 5834 lowerScalarXnor(Worklist, Inst); 5835 Inst.eraseFromParent(); 5836 continue; 5837 5838 case AMDGPU::S_NAND_B32: 5839 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_AND_B32); 5840 Inst.eraseFromParent(); 5841 continue; 5842 5843 case AMDGPU::S_NOR_B32: 5844 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_OR_B32); 5845 Inst.eraseFromParent(); 5846 continue; 5847 5848 case AMDGPU::S_ANDN2_B32: 5849 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_AND_B32); 5850 Inst.eraseFromParent(); 5851 continue; 5852 5853 case AMDGPU::S_ORN2_B32: 5854 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_OR_B32); 5855 Inst.eraseFromParent(); 5856 continue; 5857 5858 // TODO: remove as soon as everything is ready 5859 // to replace VGPR to SGPR copy with V_READFIRSTLANEs. 5860 // S_ADD/SUB_CO_PSEUDO as well as S_UADDO/USUBO_PSEUDO 5861 // can only be selected from the uniform SDNode. 5862 case AMDGPU::S_ADD_CO_PSEUDO: 5863 case AMDGPU::S_SUB_CO_PSEUDO: { 5864 unsigned Opc = (Inst.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO) 5865 ? AMDGPU::V_ADDC_U32_e64 5866 : AMDGPU::V_SUBB_U32_e64; 5867 const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 5868 5869 Register CarryInReg = Inst.getOperand(4).getReg(); 5870 if (!MRI.constrainRegClass(CarryInReg, CarryRC)) { 5871 Register NewCarryReg = MRI.createVirtualRegister(CarryRC); 5872 BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(AMDGPU::COPY), NewCarryReg) 5873 .addReg(CarryInReg); 5874 } 5875 5876 Register CarryOutReg = Inst.getOperand(1).getReg(); 5877 5878 Register DestReg = MRI.createVirtualRegister(RI.getEquivalentVGPRClass( 5879 MRI.getRegClass(Inst.getOperand(0).getReg()))); 5880 MachineInstr *CarryOp = 5881 BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(Opc), DestReg) 5882 .addReg(CarryOutReg, RegState::Define) 5883 .add(Inst.getOperand(2)) 5884 .add(Inst.getOperand(3)) 5885 .addReg(CarryInReg) 5886 .addImm(0); 5887 CreatedBBTmp = legalizeOperands(*CarryOp); 5888 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 5889 CreatedBB = CreatedBBTmp; 5890 MRI.replaceRegWith(Inst.getOperand(0).getReg(), DestReg); 5891 addUsersToMoveToVALUWorklist(DestReg, MRI, Worklist); 5892 Inst.eraseFromParent(); 5893 } 5894 continue; 5895 case AMDGPU::S_UADDO_PSEUDO: 5896 case AMDGPU::S_USUBO_PSEUDO: { 5897 const DebugLoc &DL = Inst.getDebugLoc(); 5898 MachineOperand &Dest0 = Inst.getOperand(0); 5899 MachineOperand &Dest1 = Inst.getOperand(1); 5900 MachineOperand &Src0 = Inst.getOperand(2); 5901 MachineOperand &Src1 = Inst.getOperand(3); 5902 5903 unsigned Opc = (Inst.getOpcode() == AMDGPU::S_UADDO_PSEUDO) 5904 ? AMDGPU::V_ADD_CO_U32_e64 5905 : AMDGPU::V_SUB_CO_U32_e64; 5906 const TargetRegisterClass *NewRC = 5907 RI.getEquivalentVGPRClass(MRI.getRegClass(Dest0.getReg())); 5908 Register DestReg = MRI.createVirtualRegister(NewRC); 5909 MachineInstr *NewInstr = BuildMI(*MBB, &Inst, DL, get(Opc), DestReg) 5910 .addReg(Dest1.getReg(), RegState::Define) 5911 .add(Src0) 5912 .add(Src1) 5913 .addImm(0); // clamp bit 5914 5915 CreatedBBTmp = legalizeOperands(*NewInstr, MDT); 5916 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 5917 CreatedBB = CreatedBBTmp; 5918 5919 MRI.replaceRegWith(Dest0.getReg(), DestReg); 5920 addUsersToMoveToVALUWorklist(NewInstr->getOperand(0).getReg(), MRI, 5921 Worklist); 5922 Inst.eraseFromParent(); 5923 } 5924 continue; 5925 5926 case AMDGPU::S_CSELECT_B32: 5927 case AMDGPU::S_CSELECT_B64: 5928 lowerSelect(Worklist, Inst, MDT); 5929 Inst.eraseFromParent(); 5930 continue; 5931 } 5932 5933 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) { 5934 // We cannot move this instruction to the VALU, so we should try to 5935 // legalize its operands instead. 5936 CreatedBBTmp = legalizeOperands(Inst, MDT); 5937 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 5938 CreatedBB = CreatedBBTmp; 5939 continue; 5940 } 5941 5942 // Use the new VALU Opcode. 5943 const MCInstrDesc &NewDesc = get(NewOpcode); 5944 Inst.setDesc(NewDesc); 5945 5946 // Remove any references to SCC. Vector instructions can't read from it, and 5947 // We're just about to add the implicit use / defs of VCC, and we don't want 5948 // both. 5949 for (unsigned i = Inst.getNumOperands() - 1; i > 0; --i) { 5950 MachineOperand &Op = Inst.getOperand(i); 5951 if (Op.isReg() && Op.getReg() == AMDGPU::SCC) { 5952 // Only propagate through live-def of SCC. 5953 if (Op.isDef() && !Op.isDead()) 5954 addSCCDefUsersToVALUWorklist(Op, Inst, Worklist); 5955 Inst.RemoveOperand(i); 5956 } 5957 } 5958 5959 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) { 5960 // We are converting these to a BFE, so we need to add the missing 5961 // operands for the size and offset. 5962 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16; 5963 Inst.addOperand(MachineOperand::CreateImm(0)); 5964 Inst.addOperand(MachineOperand::CreateImm(Size)); 5965 5966 } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) { 5967 // The VALU version adds the second operand to the result, so insert an 5968 // extra 0 operand. 5969 Inst.addOperand(MachineOperand::CreateImm(0)); 5970 } 5971 5972 Inst.addImplicitDefUseOperands(*Inst.getParent()->getParent()); 5973 fixImplicitOperands(Inst); 5974 5975 if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) { 5976 const MachineOperand &OffsetWidthOp = Inst.getOperand(2); 5977 // If we need to move this to VGPRs, we need to unpack the second operand 5978 // back into the 2 separate ones for bit offset and width. 5979 assert(OffsetWidthOp.isImm() && 5980 "Scalar BFE is only implemented for constant width and offset"); 5981 uint32_t Imm = OffsetWidthOp.getImm(); 5982 5983 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 5984 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 5985 Inst.RemoveOperand(2); // Remove old immediate. 5986 Inst.addOperand(MachineOperand::CreateImm(Offset)); 5987 Inst.addOperand(MachineOperand::CreateImm(BitWidth)); 5988 } 5989 5990 bool HasDst = Inst.getOperand(0).isReg() && Inst.getOperand(0).isDef(); 5991 unsigned NewDstReg = AMDGPU::NoRegister; 5992 if (HasDst) { 5993 Register DstReg = Inst.getOperand(0).getReg(); 5994 if (DstReg.isPhysical()) 5995 continue; 5996 5997 // Update the destination register class. 5998 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst); 5999 if (!NewDstRC) 6000 continue; 6001 6002 if (Inst.isCopy() && Inst.getOperand(1).getReg().isVirtual() && 6003 NewDstRC == RI.getRegClassForReg(MRI, Inst.getOperand(1).getReg())) { 6004 // Instead of creating a copy where src and dst are the same register 6005 // class, we just replace all uses of dst with src. These kinds of 6006 // copies interfere with the heuristics MachineSink uses to decide 6007 // whether or not to split a critical edge. Since the pass assumes 6008 // that copies will end up as machine instructions and not be 6009 // eliminated. 6010 addUsersToMoveToVALUWorklist(DstReg, MRI, Worklist); 6011 MRI.replaceRegWith(DstReg, Inst.getOperand(1).getReg()); 6012 MRI.clearKillFlags(Inst.getOperand(1).getReg()); 6013 Inst.getOperand(0).setReg(DstReg); 6014 6015 // Make sure we don't leave around a dead VGPR->SGPR copy. Normally 6016 // these are deleted later, but at -O0 it would leave a suspicious 6017 // looking illegal copy of an undef register. 6018 for (unsigned I = Inst.getNumOperands() - 1; I != 0; --I) 6019 Inst.RemoveOperand(I); 6020 Inst.setDesc(get(AMDGPU::IMPLICIT_DEF)); 6021 continue; 6022 } 6023 6024 NewDstReg = MRI.createVirtualRegister(NewDstRC); 6025 MRI.replaceRegWith(DstReg, NewDstReg); 6026 } 6027 6028 // Legalize the operands 6029 CreatedBBTmp = legalizeOperands(Inst, MDT); 6030 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6031 CreatedBB = CreatedBBTmp; 6032 6033 if (HasDst) 6034 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist); 6035 } 6036 return CreatedBB; 6037 } 6038 6039 // Add/sub require special handling to deal with carry outs. 6040 std::pair<bool, MachineBasicBlock *> 6041 SIInstrInfo::moveScalarAddSub(SetVectorType &Worklist, MachineInstr &Inst, 6042 MachineDominatorTree *MDT) const { 6043 if (ST.hasAddNoCarry()) { 6044 // Assume there is no user of scc since we don't select this in that case. 6045 // Since scc isn't used, it doesn't really matter if the i32 or u32 variant 6046 // is used. 6047 6048 MachineBasicBlock &MBB = *Inst.getParent(); 6049 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6050 6051 Register OldDstReg = Inst.getOperand(0).getReg(); 6052 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6053 6054 unsigned Opc = Inst.getOpcode(); 6055 assert(Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32); 6056 6057 unsigned NewOpc = Opc == AMDGPU::S_ADD_I32 ? 6058 AMDGPU::V_ADD_U32_e64 : AMDGPU::V_SUB_U32_e64; 6059 6060 assert(Inst.getOperand(3).getReg() == AMDGPU::SCC); 6061 Inst.RemoveOperand(3); 6062 6063 Inst.setDesc(get(NewOpc)); 6064 Inst.addOperand(MachineOperand::CreateImm(0)); // clamp bit 6065 Inst.addImplicitDefUseOperands(*MBB.getParent()); 6066 MRI.replaceRegWith(OldDstReg, ResultReg); 6067 MachineBasicBlock *NewBB = legalizeOperands(Inst, MDT); 6068 6069 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6070 return std::make_pair(true, NewBB); 6071 } 6072 6073 return std::make_pair(false, nullptr); 6074 } 6075 6076 void SIInstrInfo::lowerSelect(SetVectorType &Worklist, MachineInstr &Inst, 6077 MachineDominatorTree *MDT) const { 6078 6079 MachineBasicBlock &MBB = *Inst.getParent(); 6080 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6081 MachineBasicBlock::iterator MII = Inst; 6082 DebugLoc DL = Inst.getDebugLoc(); 6083 6084 MachineOperand &Dest = Inst.getOperand(0); 6085 MachineOperand &Src0 = Inst.getOperand(1); 6086 MachineOperand &Src1 = Inst.getOperand(2); 6087 MachineOperand &Cond = Inst.getOperand(3); 6088 6089 Register SCCSource = Cond.getReg(); 6090 // Find SCC def, and if that is a copy (SCC = COPY reg) then use reg instead. 6091 if (!Cond.isUndef()) { 6092 for (MachineInstr &CandI : 6093 make_range(std::next(MachineBasicBlock::reverse_iterator(Inst)), 6094 Inst.getParent()->rend())) { 6095 if (CandI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != 6096 -1) { 6097 if (CandI.isCopy() && CandI.getOperand(0).getReg() == AMDGPU::SCC) { 6098 SCCSource = CandI.getOperand(1).getReg(); 6099 } 6100 break; 6101 } 6102 } 6103 } 6104 6105 // If this is a trivial select where the condition is effectively not SCC 6106 // (SCCSource is a source of copy to SCC), then the select is semantically 6107 // equivalent to copying SCCSource. Hence, there is no need to create 6108 // V_CNDMASK, we can just use that and bail out. 6109 if ((SCCSource != AMDGPU::SCC) && Src0.isImm() && (Src0.getImm() == -1) && 6110 Src1.isImm() && (Src1.getImm() == 0)) { 6111 MRI.replaceRegWith(Dest.getReg(), SCCSource); 6112 return; 6113 } 6114 6115 const TargetRegisterClass *TC = ST.getWavefrontSize() == 64 6116 ? &AMDGPU::SReg_64_XEXECRegClass 6117 : &AMDGPU::SReg_32_XM0_XEXECRegClass; 6118 Register CopySCC = MRI.createVirtualRegister(TC); 6119 6120 if (SCCSource == AMDGPU::SCC) { 6121 // Insert a trivial select instead of creating a copy, because a copy from 6122 // SCC would semantically mean just copying a single bit, but we may need 6123 // the result to be a vector condition mask that needs preserving. 6124 unsigned Opcode = (ST.getWavefrontSize() == 64) ? AMDGPU::S_CSELECT_B64 6125 : AMDGPU::S_CSELECT_B32; 6126 auto NewSelect = 6127 BuildMI(MBB, MII, DL, get(Opcode), CopySCC).addImm(-1).addImm(0); 6128 NewSelect->getOperand(3).setIsUndef(Cond.isUndef()); 6129 } else { 6130 BuildMI(MBB, MII, DL, get(AMDGPU::COPY), CopySCC).addReg(SCCSource); 6131 } 6132 6133 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6134 6135 auto UpdatedInst = 6136 BuildMI(MBB, MII, DL, get(AMDGPU::V_CNDMASK_B32_e64), ResultReg) 6137 .addImm(0) 6138 .add(Src1) // False 6139 .addImm(0) 6140 .add(Src0) // True 6141 .addReg(CopySCC); 6142 6143 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6144 legalizeOperands(*UpdatedInst, MDT); 6145 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6146 } 6147 6148 void SIInstrInfo::lowerScalarAbs(SetVectorType &Worklist, 6149 MachineInstr &Inst) const { 6150 MachineBasicBlock &MBB = *Inst.getParent(); 6151 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6152 MachineBasicBlock::iterator MII = Inst; 6153 DebugLoc DL = Inst.getDebugLoc(); 6154 6155 MachineOperand &Dest = Inst.getOperand(0); 6156 MachineOperand &Src = Inst.getOperand(1); 6157 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6158 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6159 6160 unsigned SubOp = ST.hasAddNoCarry() ? 6161 AMDGPU::V_SUB_U32_e32 : AMDGPU::V_SUB_CO_U32_e32; 6162 6163 BuildMI(MBB, MII, DL, get(SubOp), TmpReg) 6164 .addImm(0) 6165 .addReg(Src.getReg()); 6166 6167 BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg) 6168 .addReg(Src.getReg()) 6169 .addReg(TmpReg); 6170 6171 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6172 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6173 } 6174 6175 void SIInstrInfo::lowerScalarXnor(SetVectorType &Worklist, 6176 MachineInstr &Inst) const { 6177 MachineBasicBlock &MBB = *Inst.getParent(); 6178 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6179 MachineBasicBlock::iterator MII = Inst; 6180 const DebugLoc &DL = Inst.getDebugLoc(); 6181 6182 MachineOperand &Dest = Inst.getOperand(0); 6183 MachineOperand &Src0 = Inst.getOperand(1); 6184 MachineOperand &Src1 = Inst.getOperand(2); 6185 6186 if (ST.hasDLInsts()) { 6187 Register NewDest = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6188 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src0, MRI, DL); 6189 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src1, MRI, DL); 6190 6191 BuildMI(MBB, MII, DL, get(AMDGPU::V_XNOR_B32_e64), NewDest) 6192 .add(Src0) 6193 .add(Src1); 6194 6195 MRI.replaceRegWith(Dest.getReg(), NewDest); 6196 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6197 } else { 6198 // Using the identity !(x ^ y) == (!x ^ y) == (x ^ !y), we can 6199 // invert either source and then perform the XOR. If either source is a 6200 // scalar register, then we can leave the inversion on the scalar unit to 6201 // acheive a better distrubution of scalar and vector instructions. 6202 bool Src0IsSGPR = Src0.isReg() && 6203 RI.isSGPRClass(MRI.getRegClass(Src0.getReg())); 6204 bool Src1IsSGPR = Src1.isReg() && 6205 RI.isSGPRClass(MRI.getRegClass(Src1.getReg())); 6206 MachineInstr *Xor; 6207 Register Temp = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6208 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6209 6210 // Build a pair of scalar instructions and add them to the work list. 6211 // The next iteration over the work list will lower these to the vector 6212 // unit as necessary. 6213 if (Src0IsSGPR) { 6214 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src0); 6215 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest) 6216 .addReg(Temp) 6217 .add(Src1); 6218 } else if (Src1IsSGPR) { 6219 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src1); 6220 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest) 6221 .add(Src0) 6222 .addReg(Temp); 6223 } else { 6224 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), Temp) 6225 .add(Src0) 6226 .add(Src1); 6227 MachineInstr *Not = 6228 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest).addReg(Temp); 6229 Worklist.insert(Not); 6230 } 6231 6232 MRI.replaceRegWith(Dest.getReg(), NewDest); 6233 6234 Worklist.insert(Xor); 6235 6236 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6237 } 6238 } 6239 6240 void SIInstrInfo::splitScalarNotBinop(SetVectorType &Worklist, 6241 MachineInstr &Inst, 6242 unsigned Opcode) const { 6243 MachineBasicBlock &MBB = *Inst.getParent(); 6244 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6245 MachineBasicBlock::iterator MII = Inst; 6246 const DebugLoc &DL = Inst.getDebugLoc(); 6247 6248 MachineOperand &Dest = Inst.getOperand(0); 6249 MachineOperand &Src0 = Inst.getOperand(1); 6250 MachineOperand &Src1 = Inst.getOperand(2); 6251 6252 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6253 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6254 6255 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), Interm) 6256 .add(Src0) 6257 .add(Src1); 6258 6259 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest) 6260 .addReg(Interm); 6261 6262 Worklist.insert(&Op); 6263 Worklist.insert(&Not); 6264 6265 MRI.replaceRegWith(Dest.getReg(), NewDest); 6266 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6267 } 6268 6269 void SIInstrInfo::splitScalarBinOpN2(SetVectorType& Worklist, 6270 MachineInstr &Inst, 6271 unsigned Opcode) const { 6272 MachineBasicBlock &MBB = *Inst.getParent(); 6273 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6274 MachineBasicBlock::iterator MII = Inst; 6275 const DebugLoc &DL = Inst.getDebugLoc(); 6276 6277 MachineOperand &Dest = Inst.getOperand(0); 6278 MachineOperand &Src0 = Inst.getOperand(1); 6279 MachineOperand &Src1 = Inst.getOperand(2); 6280 6281 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 6282 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 6283 6284 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Interm) 6285 .add(Src1); 6286 6287 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), NewDest) 6288 .add(Src0) 6289 .addReg(Interm); 6290 6291 Worklist.insert(&Not); 6292 Worklist.insert(&Op); 6293 6294 MRI.replaceRegWith(Dest.getReg(), NewDest); 6295 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6296 } 6297 6298 void SIInstrInfo::splitScalar64BitUnaryOp( 6299 SetVectorType &Worklist, MachineInstr &Inst, 6300 unsigned Opcode, bool Swap) const { 6301 MachineBasicBlock &MBB = *Inst.getParent(); 6302 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6303 6304 MachineOperand &Dest = Inst.getOperand(0); 6305 MachineOperand &Src0 = Inst.getOperand(1); 6306 DebugLoc DL = Inst.getDebugLoc(); 6307 6308 MachineBasicBlock::iterator MII = Inst; 6309 6310 const MCInstrDesc &InstDesc = get(Opcode); 6311 const TargetRegisterClass *Src0RC = Src0.isReg() ? 6312 MRI.getRegClass(Src0.getReg()) : 6313 &AMDGPU::SGPR_32RegClass; 6314 6315 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 6316 6317 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6318 AMDGPU::sub0, Src0SubRC); 6319 6320 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 6321 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 6322 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 6323 6324 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 6325 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0).add(SrcReg0Sub0); 6326 6327 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6328 AMDGPU::sub1, Src0SubRC); 6329 6330 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 6331 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1).add(SrcReg0Sub1); 6332 6333 if (Swap) 6334 std::swap(DestSub0, DestSub1); 6335 6336 Register FullDestReg = MRI.createVirtualRegister(NewDestRC); 6337 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 6338 .addReg(DestSub0) 6339 .addImm(AMDGPU::sub0) 6340 .addReg(DestSub1) 6341 .addImm(AMDGPU::sub1); 6342 6343 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 6344 6345 Worklist.insert(&LoHalf); 6346 Worklist.insert(&HiHalf); 6347 6348 // We don't need to legalizeOperands here because for a single operand, src0 6349 // will support any kind of input. 6350 6351 // Move all users of this moved value. 6352 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 6353 } 6354 6355 void SIInstrInfo::splitScalar64BitAddSub(SetVectorType &Worklist, 6356 MachineInstr &Inst, 6357 MachineDominatorTree *MDT) const { 6358 bool IsAdd = (Inst.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO); 6359 6360 MachineBasicBlock &MBB = *Inst.getParent(); 6361 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6362 const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 6363 6364 Register FullDestReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 6365 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6366 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6367 6368 Register CarryReg = MRI.createVirtualRegister(CarryRC); 6369 Register DeadCarryReg = MRI.createVirtualRegister(CarryRC); 6370 6371 MachineOperand &Dest = Inst.getOperand(0); 6372 MachineOperand &Src0 = Inst.getOperand(1); 6373 MachineOperand &Src1 = Inst.getOperand(2); 6374 const DebugLoc &DL = Inst.getDebugLoc(); 6375 MachineBasicBlock::iterator MII = Inst; 6376 6377 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0.getReg()); 6378 const TargetRegisterClass *Src1RC = MRI.getRegClass(Src1.getReg()); 6379 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 6380 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); 6381 6382 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6383 AMDGPU::sub0, Src0SubRC); 6384 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6385 AMDGPU::sub0, Src1SubRC); 6386 6387 6388 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6389 AMDGPU::sub1, Src0SubRC); 6390 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6391 AMDGPU::sub1, Src1SubRC); 6392 6393 unsigned LoOpc = IsAdd ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_SUB_CO_U32_e64; 6394 MachineInstr *LoHalf = 6395 BuildMI(MBB, MII, DL, get(LoOpc), DestSub0) 6396 .addReg(CarryReg, RegState::Define) 6397 .add(SrcReg0Sub0) 6398 .add(SrcReg1Sub0) 6399 .addImm(0); // clamp bit 6400 6401 unsigned HiOpc = IsAdd ? AMDGPU::V_ADDC_U32_e64 : AMDGPU::V_SUBB_U32_e64; 6402 MachineInstr *HiHalf = 6403 BuildMI(MBB, MII, DL, get(HiOpc), DestSub1) 6404 .addReg(DeadCarryReg, RegState::Define | RegState::Dead) 6405 .add(SrcReg0Sub1) 6406 .add(SrcReg1Sub1) 6407 .addReg(CarryReg, RegState::Kill) 6408 .addImm(0); // clamp bit 6409 6410 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 6411 .addReg(DestSub0) 6412 .addImm(AMDGPU::sub0) 6413 .addReg(DestSub1) 6414 .addImm(AMDGPU::sub1); 6415 6416 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 6417 6418 // Try to legalize the operands in case we need to swap the order to keep it 6419 // valid. 6420 legalizeOperands(*LoHalf, MDT); 6421 legalizeOperands(*HiHalf, MDT); 6422 6423 // Move all users of this moved vlaue. 6424 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 6425 } 6426 6427 void SIInstrInfo::splitScalar64BitBinaryOp(SetVectorType &Worklist, 6428 MachineInstr &Inst, unsigned Opcode, 6429 MachineDominatorTree *MDT) const { 6430 MachineBasicBlock &MBB = *Inst.getParent(); 6431 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6432 6433 MachineOperand &Dest = Inst.getOperand(0); 6434 MachineOperand &Src0 = Inst.getOperand(1); 6435 MachineOperand &Src1 = Inst.getOperand(2); 6436 DebugLoc DL = Inst.getDebugLoc(); 6437 6438 MachineBasicBlock::iterator MII = Inst; 6439 6440 const MCInstrDesc &InstDesc = get(Opcode); 6441 const TargetRegisterClass *Src0RC = Src0.isReg() ? 6442 MRI.getRegClass(Src0.getReg()) : 6443 &AMDGPU::SGPR_32RegClass; 6444 6445 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 6446 const TargetRegisterClass *Src1RC = Src1.isReg() ? 6447 MRI.getRegClass(Src1.getReg()) : 6448 &AMDGPU::SGPR_32RegClass; 6449 6450 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); 6451 6452 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6453 AMDGPU::sub0, Src0SubRC); 6454 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6455 AMDGPU::sub0, Src1SubRC); 6456 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6457 AMDGPU::sub1, Src0SubRC); 6458 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6459 AMDGPU::sub1, Src1SubRC); 6460 6461 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 6462 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 6463 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 6464 6465 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 6466 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0) 6467 .add(SrcReg0Sub0) 6468 .add(SrcReg1Sub0); 6469 6470 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 6471 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1) 6472 .add(SrcReg0Sub1) 6473 .add(SrcReg1Sub1); 6474 6475 Register FullDestReg = MRI.createVirtualRegister(NewDestRC); 6476 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 6477 .addReg(DestSub0) 6478 .addImm(AMDGPU::sub0) 6479 .addReg(DestSub1) 6480 .addImm(AMDGPU::sub1); 6481 6482 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 6483 6484 Worklist.insert(&LoHalf); 6485 Worklist.insert(&HiHalf); 6486 6487 // Move all users of this moved vlaue. 6488 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 6489 } 6490 6491 void SIInstrInfo::splitScalar64BitXnor(SetVectorType &Worklist, 6492 MachineInstr &Inst, 6493 MachineDominatorTree *MDT) const { 6494 MachineBasicBlock &MBB = *Inst.getParent(); 6495 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6496 6497 MachineOperand &Dest = Inst.getOperand(0); 6498 MachineOperand &Src0 = Inst.getOperand(1); 6499 MachineOperand &Src1 = Inst.getOperand(2); 6500 const DebugLoc &DL = Inst.getDebugLoc(); 6501 6502 MachineBasicBlock::iterator MII = Inst; 6503 6504 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 6505 6506 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 6507 6508 MachineOperand* Op0; 6509 MachineOperand* Op1; 6510 6511 if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) { 6512 Op0 = &Src0; 6513 Op1 = &Src1; 6514 } else { 6515 Op0 = &Src1; 6516 Op1 = &Src0; 6517 } 6518 6519 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B64), Interm) 6520 .add(*Op0); 6521 6522 Register NewDest = MRI.createVirtualRegister(DestRC); 6523 6524 MachineInstr &Xor = *BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B64), NewDest) 6525 .addReg(Interm) 6526 .add(*Op1); 6527 6528 MRI.replaceRegWith(Dest.getReg(), NewDest); 6529 6530 Worklist.insert(&Xor); 6531 } 6532 6533 void SIInstrInfo::splitScalar64BitBCNT( 6534 SetVectorType &Worklist, MachineInstr &Inst) const { 6535 MachineBasicBlock &MBB = *Inst.getParent(); 6536 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6537 6538 MachineBasicBlock::iterator MII = Inst; 6539 const DebugLoc &DL = Inst.getDebugLoc(); 6540 6541 MachineOperand &Dest = Inst.getOperand(0); 6542 MachineOperand &Src = Inst.getOperand(1); 6543 6544 const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64); 6545 const TargetRegisterClass *SrcRC = Src.isReg() ? 6546 MRI.getRegClass(Src.getReg()) : 6547 &AMDGPU::SGPR_32RegClass; 6548 6549 Register MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6550 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6551 6552 const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0); 6553 6554 MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 6555 AMDGPU::sub0, SrcSubRC); 6556 MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 6557 AMDGPU::sub1, SrcSubRC); 6558 6559 BuildMI(MBB, MII, DL, InstDesc, MidReg).add(SrcRegSub0).addImm(0); 6560 6561 BuildMI(MBB, MII, DL, InstDesc, ResultReg).add(SrcRegSub1).addReg(MidReg); 6562 6563 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6564 6565 // We don't need to legalize operands here. src0 for etiher instruction can be 6566 // an SGPR, and the second input is unused or determined here. 6567 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6568 } 6569 6570 void SIInstrInfo::splitScalar64BitBFE(SetVectorType &Worklist, 6571 MachineInstr &Inst) const { 6572 MachineBasicBlock &MBB = *Inst.getParent(); 6573 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6574 MachineBasicBlock::iterator MII = Inst; 6575 const DebugLoc &DL = Inst.getDebugLoc(); 6576 6577 MachineOperand &Dest = Inst.getOperand(0); 6578 uint32_t Imm = Inst.getOperand(2).getImm(); 6579 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 6580 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 6581 6582 (void) Offset; 6583 6584 // Only sext_inreg cases handled. 6585 assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 && 6586 Offset == 0 && "Not implemented"); 6587 6588 if (BitWidth < 32) { 6589 Register MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6590 Register MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6591 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 6592 6593 BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32_e64), MidRegLo) 6594 .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0) 6595 .addImm(0) 6596 .addImm(BitWidth); 6597 6598 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi) 6599 .addImm(31) 6600 .addReg(MidRegLo); 6601 6602 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 6603 .addReg(MidRegLo) 6604 .addImm(AMDGPU::sub0) 6605 .addReg(MidRegHi) 6606 .addImm(AMDGPU::sub1); 6607 6608 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6609 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6610 return; 6611 } 6612 6613 MachineOperand &Src = Inst.getOperand(1); 6614 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6615 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 6616 6617 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg) 6618 .addImm(31) 6619 .addReg(Src.getReg(), 0, AMDGPU::sub0); 6620 6621 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 6622 .addReg(Src.getReg(), 0, AMDGPU::sub0) 6623 .addImm(AMDGPU::sub0) 6624 .addReg(TmpReg) 6625 .addImm(AMDGPU::sub1); 6626 6627 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6628 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6629 } 6630 6631 void SIInstrInfo::addUsersToMoveToVALUWorklist( 6632 Register DstReg, 6633 MachineRegisterInfo &MRI, 6634 SetVectorType &Worklist) const { 6635 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg), 6636 E = MRI.use_end(); I != E;) { 6637 MachineInstr &UseMI = *I->getParent(); 6638 6639 unsigned OpNo = 0; 6640 6641 switch (UseMI.getOpcode()) { 6642 case AMDGPU::COPY: 6643 case AMDGPU::WQM: 6644 case AMDGPU::SOFT_WQM: 6645 case AMDGPU::WWM: 6646 case AMDGPU::REG_SEQUENCE: 6647 case AMDGPU::PHI: 6648 case AMDGPU::INSERT_SUBREG: 6649 break; 6650 default: 6651 OpNo = I.getOperandNo(); 6652 break; 6653 } 6654 6655 if (!RI.hasVectorRegisters(getOpRegClass(UseMI, OpNo))) { 6656 Worklist.insert(&UseMI); 6657 6658 do { 6659 ++I; 6660 } while (I != E && I->getParent() == &UseMI); 6661 } else { 6662 ++I; 6663 } 6664 } 6665 } 6666 6667 void SIInstrInfo::movePackToVALU(SetVectorType &Worklist, 6668 MachineRegisterInfo &MRI, 6669 MachineInstr &Inst) const { 6670 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6671 MachineBasicBlock *MBB = Inst.getParent(); 6672 MachineOperand &Src0 = Inst.getOperand(1); 6673 MachineOperand &Src1 = Inst.getOperand(2); 6674 const DebugLoc &DL = Inst.getDebugLoc(); 6675 6676 switch (Inst.getOpcode()) { 6677 case AMDGPU::S_PACK_LL_B32_B16: { 6678 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6679 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6680 6681 // FIXME: Can do a lot better if we know the high bits of src0 or src1 are 6682 // 0. 6683 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 6684 .addImm(0xffff); 6685 6686 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_B32_e64), TmpReg) 6687 .addReg(ImmReg, RegState::Kill) 6688 .add(Src0); 6689 6690 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32_e64), ResultReg) 6691 .add(Src1) 6692 .addImm(16) 6693 .addReg(TmpReg, RegState::Kill); 6694 break; 6695 } 6696 case AMDGPU::S_PACK_LH_B32_B16: { 6697 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6698 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 6699 .addImm(0xffff); 6700 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_BFI_B32_e64), ResultReg) 6701 .addReg(ImmReg, RegState::Kill) 6702 .add(Src0) 6703 .add(Src1); 6704 break; 6705 } 6706 case AMDGPU::S_PACK_HH_B32_B16: { 6707 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6708 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6709 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg) 6710 .addImm(16) 6711 .add(Src0); 6712 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 6713 .addImm(0xffff0000); 6714 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_OR_B32_e64), ResultReg) 6715 .add(Src1) 6716 .addReg(ImmReg, RegState::Kill) 6717 .addReg(TmpReg, RegState::Kill); 6718 break; 6719 } 6720 default: 6721 llvm_unreachable("unhandled s_pack_* instruction"); 6722 } 6723 6724 MachineOperand &Dest = Inst.getOperand(0); 6725 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6726 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6727 } 6728 6729 void SIInstrInfo::addSCCDefUsersToVALUWorklist(MachineOperand &Op, 6730 MachineInstr &SCCDefInst, 6731 SetVectorType &Worklist) const { 6732 bool SCCUsedImplicitly = false; 6733 6734 // Ensure that def inst defines SCC, which is still live. 6735 assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isDef() && 6736 !Op.isDead() && Op.getParent() == &SCCDefInst); 6737 SmallVector<MachineInstr *, 4> CopyToDelete; 6738 // This assumes that all the users of SCC are in the same block 6739 // as the SCC def. 6740 for (MachineInstr &MI : // Skip the def inst itself. 6741 make_range(std::next(MachineBasicBlock::iterator(SCCDefInst)), 6742 SCCDefInst.getParent()->end())) { 6743 // Check if SCC is used first. 6744 if (MI.findRegisterUseOperandIdx(AMDGPU::SCC, false, &RI) != -1) { 6745 if (MI.isCopy()) { 6746 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 6747 Register DestReg = MI.getOperand(0).getReg(); 6748 6749 for (auto &User : MRI.use_nodbg_instructions(DestReg)) { 6750 if ((User.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO) || 6751 (User.getOpcode() == AMDGPU::S_SUB_CO_PSEUDO)) { 6752 User.getOperand(4).setReg(RI.getVCC()); 6753 Worklist.insert(&User); 6754 } else if (User.getOpcode() == AMDGPU::V_CNDMASK_B32_e64) { 6755 User.getOperand(5).setReg(RI.getVCC()); 6756 // No need to add to Worklist. 6757 } 6758 } 6759 CopyToDelete.push_back(&MI); 6760 } else { 6761 if (MI.getOpcode() == AMDGPU::S_CSELECT_B32 || 6762 MI.getOpcode() == AMDGPU::S_CSELECT_B64) { 6763 // This is an implicit use of SCC and it is really expected by 6764 // the SCC users to handle. 6765 // We cannot preserve the edge to the user so add the explicit 6766 // copy: SCC = COPY VCC. 6767 // The copy will be cleaned up during the processing of the user 6768 // in lowerSelect. 6769 SCCUsedImplicitly = true; 6770 } 6771 6772 Worklist.insert(&MI); 6773 } 6774 } 6775 // Exit if we find another SCC def. 6776 if (MI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != -1) 6777 break; 6778 } 6779 for (auto &Copy : CopyToDelete) 6780 Copy->eraseFromParent(); 6781 6782 if (SCCUsedImplicitly) { 6783 BuildMI(*SCCDefInst.getParent(), std::next(SCCDefInst.getIterator()), 6784 SCCDefInst.getDebugLoc(), get(AMDGPU::COPY), AMDGPU::SCC) 6785 .addReg(RI.getVCC()); 6786 } 6787 } 6788 6789 const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass( 6790 const MachineInstr &Inst) const { 6791 const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0); 6792 6793 switch (Inst.getOpcode()) { 6794 // For target instructions, getOpRegClass just returns the virtual register 6795 // class associated with the operand, so we need to find an equivalent VGPR 6796 // register class in order to move the instruction to the VALU. 6797 case AMDGPU::COPY: 6798 case AMDGPU::PHI: 6799 case AMDGPU::REG_SEQUENCE: 6800 case AMDGPU::INSERT_SUBREG: 6801 case AMDGPU::WQM: 6802 case AMDGPU::SOFT_WQM: 6803 case AMDGPU::WWM: { 6804 const TargetRegisterClass *SrcRC = getOpRegClass(Inst, 1); 6805 if (RI.hasAGPRs(SrcRC)) { 6806 if (RI.hasAGPRs(NewDstRC)) 6807 return nullptr; 6808 6809 switch (Inst.getOpcode()) { 6810 case AMDGPU::PHI: 6811 case AMDGPU::REG_SEQUENCE: 6812 case AMDGPU::INSERT_SUBREG: 6813 NewDstRC = RI.getEquivalentAGPRClass(NewDstRC); 6814 break; 6815 default: 6816 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 6817 } 6818 6819 if (!NewDstRC) 6820 return nullptr; 6821 } else { 6822 if (RI.hasVGPRs(NewDstRC) || NewDstRC == &AMDGPU::VReg_1RegClass) 6823 return nullptr; 6824 6825 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 6826 if (!NewDstRC) 6827 return nullptr; 6828 } 6829 6830 return NewDstRC; 6831 } 6832 default: 6833 return NewDstRC; 6834 } 6835 } 6836 6837 // Find the one SGPR operand we are allowed to use. 6838 Register SIInstrInfo::findUsedSGPR(const MachineInstr &MI, 6839 int OpIndices[3]) const { 6840 const MCInstrDesc &Desc = MI.getDesc(); 6841 6842 // Find the one SGPR operand we are allowed to use. 6843 // 6844 // First we need to consider the instruction's operand requirements before 6845 // legalizing. Some operands are required to be SGPRs, such as implicit uses 6846 // of VCC, but we are still bound by the constant bus requirement to only use 6847 // one. 6848 // 6849 // If the operand's class is an SGPR, we can never move it. 6850 6851 Register SGPRReg = findImplicitSGPRRead(MI); 6852 if (SGPRReg != AMDGPU::NoRegister) 6853 return SGPRReg; 6854 6855 Register UsedSGPRs[3] = { AMDGPU::NoRegister }; 6856 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 6857 6858 for (unsigned i = 0; i < 3; ++i) { 6859 int Idx = OpIndices[i]; 6860 if (Idx == -1) 6861 break; 6862 6863 const MachineOperand &MO = MI.getOperand(Idx); 6864 if (!MO.isReg()) 6865 continue; 6866 6867 // Is this operand statically required to be an SGPR based on the operand 6868 // constraints? 6869 const TargetRegisterClass *OpRC = RI.getRegClass(Desc.OpInfo[Idx].RegClass); 6870 bool IsRequiredSGPR = RI.isSGPRClass(OpRC); 6871 if (IsRequiredSGPR) 6872 return MO.getReg(); 6873 6874 // If this could be a VGPR or an SGPR, Check the dynamic register class. 6875 Register Reg = MO.getReg(); 6876 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg); 6877 if (RI.isSGPRClass(RegRC)) 6878 UsedSGPRs[i] = Reg; 6879 } 6880 6881 // We don't have a required SGPR operand, so we have a bit more freedom in 6882 // selecting operands to move. 6883 6884 // Try to select the most used SGPR. If an SGPR is equal to one of the 6885 // others, we choose that. 6886 // 6887 // e.g. 6888 // V_FMA_F32 v0, s0, s0, s0 -> No moves 6889 // V_FMA_F32 v0, s0, s1, s0 -> Move s1 6890 6891 // TODO: If some of the operands are 64-bit SGPRs and some 32, we should 6892 // prefer those. 6893 6894 if (UsedSGPRs[0] != AMDGPU::NoRegister) { 6895 if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2]) 6896 SGPRReg = UsedSGPRs[0]; 6897 } 6898 6899 if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) { 6900 if (UsedSGPRs[1] == UsedSGPRs[2]) 6901 SGPRReg = UsedSGPRs[1]; 6902 } 6903 6904 return SGPRReg; 6905 } 6906 6907 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI, 6908 unsigned OperandName) const { 6909 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName); 6910 if (Idx == -1) 6911 return nullptr; 6912 6913 return &MI.getOperand(Idx); 6914 } 6915 6916 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const { 6917 if (ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 6918 return (AMDGPU::MTBUFFormat::UFMT_32_FLOAT << 44) | 6919 (1ULL << 56) | // RESOURCE_LEVEL = 1 6920 (3ULL << 60); // OOB_SELECT = 3 6921 } 6922 6923 uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT; 6924 if (ST.isAmdHsaOS()) { 6925 // Set ATC = 1. GFX9 doesn't have this bit. 6926 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) 6927 RsrcDataFormat |= (1ULL << 56); 6928 6929 // Set MTYPE = 2 (MTYPE_UC = uncached). GFX9 doesn't have this. 6930 // BTW, it disables TC L2 and therefore decreases performance. 6931 if (ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) 6932 RsrcDataFormat |= (2ULL << 59); 6933 } 6934 6935 return RsrcDataFormat; 6936 } 6937 6938 uint64_t SIInstrInfo::getScratchRsrcWords23() const { 6939 uint64_t Rsrc23 = getDefaultRsrcDataFormat() | 6940 AMDGPU::RSRC_TID_ENABLE | 6941 0xffffffff; // Size; 6942 6943 // GFX9 doesn't have ELEMENT_SIZE. 6944 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 6945 uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize(true)) - 1; 6946 Rsrc23 |= EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT; 6947 } 6948 6949 // IndexStride = 64 / 32. 6950 uint64_t IndexStride = ST.getWavefrontSize() == 64 ? 3 : 2; 6951 Rsrc23 |= IndexStride << AMDGPU::RSRC_INDEX_STRIDE_SHIFT; 6952 6953 // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17]. 6954 // Clear them unless we want a huge stride. 6955 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS && 6956 ST.getGeneration() <= AMDGPUSubtarget::GFX9) 6957 Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT; 6958 6959 return Rsrc23; 6960 } 6961 6962 bool SIInstrInfo::isLowLatencyInstruction(const MachineInstr &MI) const { 6963 unsigned Opc = MI.getOpcode(); 6964 6965 return isSMRD(Opc); 6966 } 6967 6968 bool SIInstrInfo::isHighLatencyDef(int Opc) const { 6969 return get(Opc).mayLoad() && 6970 (isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc) || isFLAT(Opc)); 6971 } 6972 6973 unsigned SIInstrInfo::isStackAccess(const MachineInstr &MI, 6974 int &FrameIndex) const { 6975 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 6976 if (!Addr || !Addr->isFI()) 6977 return AMDGPU::NoRegister; 6978 6979 assert(!MI.memoperands_empty() && 6980 (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS); 6981 6982 FrameIndex = Addr->getIndex(); 6983 return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg(); 6984 } 6985 6986 unsigned SIInstrInfo::isSGPRStackAccess(const MachineInstr &MI, 6987 int &FrameIndex) const { 6988 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr); 6989 assert(Addr && Addr->isFI()); 6990 FrameIndex = Addr->getIndex(); 6991 return getNamedOperand(MI, AMDGPU::OpName::data)->getReg(); 6992 } 6993 6994 unsigned SIInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 6995 int &FrameIndex) const { 6996 if (!MI.mayLoad()) 6997 return AMDGPU::NoRegister; 6998 6999 if (isMUBUF(MI) || isVGPRSpill(MI)) 7000 return isStackAccess(MI, FrameIndex); 7001 7002 if (isSGPRSpill(MI)) 7003 return isSGPRStackAccess(MI, FrameIndex); 7004 7005 return AMDGPU::NoRegister; 7006 } 7007 7008 unsigned SIInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 7009 int &FrameIndex) const { 7010 if (!MI.mayStore()) 7011 return AMDGPU::NoRegister; 7012 7013 if (isMUBUF(MI) || isVGPRSpill(MI)) 7014 return isStackAccess(MI, FrameIndex); 7015 7016 if (isSGPRSpill(MI)) 7017 return isSGPRStackAccess(MI, FrameIndex); 7018 7019 return AMDGPU::NoRegister; 7020 } 7021 7022 unsigned SIInstrInfo::getInstBundleSize(const MachineInstr &MI) const { 7023 unsigned Size = 0; 7024 MachineBasicBlock::const_instr_iterator I = MI.getIterator(); 7025 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 7026 while (++I != E && I->isInsideBundle()) { 7027 assert(!I->isBundle() && "No nested bundle!"); 7028 Size += getInstSizeInBytes(*I); 7029 } 7030 7031 return Size; 7032 } 7033 7034 unsigned SIInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 7035 unsigned Opc = MI.getOpcode(); 7036 const MCInstrDesc &Desc = getMCOpcodeFromPseudo(Opc); 7037 unsigned DescSize = Desc.getSize(); 7038 7039 // If we have a definitive size, we can use it. Otherwise we need to inspect 7040 // the operands to know the size. 7041 if (isFixedSize(MI)) { 7042 unsigned Size = DescSize; 7043 7044 // If we hit the buggy offset, an extra nop will be inserted in MC so 7045 // estimate the worst case. 7046 if (MI.isBranch() && ST.hasOffset3fBug()) 7047 Size += 4; 7048 7049 return Size; 7050 } 7051 7052 // 4-byte instructions may have a 32-bit literal encoded after them. Check 7053 // operands that coud ever be literals. 7054 if (isVALU(MI) || isSALU(MI)) { 7055 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 7056 if (Src0Idx == -1) 7057 return DescSize; // No operands. 7058 7059 if (isLiteralConstantLike(MI.getOperand(Src0Idx), Desc.OpInfo[Src0Idx])) 7060 return isVOP3(MI) ? 12 : (DescSize + 4); 7061 7062 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 7063 if (Src1Idx == -1) 7064 return DescSize; 7065 7066 if (isLiteralConstantLike(MI.getOperand(Src1Idx), Desc.OpInfo[Src1Idx])) 7067 return isVOP3(MI) ? 12 : (DescSize + 4); 7068 7069 int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); 7070 if (Src2Idx == -1) 7071 return DescSize; 7072 7073 if (isLiteralConstantLike(MI.getOperand(Src2Idx), Desc.OpInfo[Src2Idx])) 7074 return isVOP3(MI) ? 12 : (DescSize + 4); 7075 7076 return DescSize; 7077 } 7078 7079 // Check whether we have extra NSA words. 7080 if (isMIMG(MI)) { 7081 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0); 7082 if (VAddr0Idx < 0) 7083 return 8; 7084 7085 int RSrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc); 7086 return 8 + 4 * ((RSrcIdx - VAddr0Idx + 2) / 4); 7087 } 7088 7089 switch (Opc) { 7090 case TargetOpcode::IMPLICIT_DEF: 7091 case TargetOpcode::KILL: 7092 case TargetOpcode::DBG_VALUE: 7093 case TargetOpcode::EH_LABEL: 7094 return 0; 7095 case TargetOpcode::BUNDLE: 7096 return getInstBundleSize(MI); 7097 case TargetOpcode::INLINEASM: 7098 case TargetOpcode::INLINEASM_BR: { 7099 const MachineFunction *MF = MI.getParent()->getParent(); 7100 const char *AsmStr = MI.getOperand(0).getSymbolName(); 7101 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo(), &ST); 7102 } 7103 default: 7104 return DescSize; 7105 } 7106 } 7107 7108 bool SIInstrInfo::mayAccessFlatAddressSpace(const MachineInstr &MI) const { 7109 if (!isFLAT(MI)) 7110 return false; 7111 7112 if (MI.memoperands_empty()) 7113 return true; 7114 7115 for (const MachineMemOperand *MMO : MI.memoperands()) { 7116 if (MMO->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS) 7117 return true; 7118 } 7119 return false; 7120 } 7121 7122 bool SIInstrInfo::isNonUniformBranchInstr(MachineInstr &Branch) const { 7123 return Branch.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO; 7124 } 7125 7126 void SIInstrInfo::convertNonUniformIfRegion(MachineBasicBlock *IfEntry, 7127 MachineBasicBlock *IfEnd) const { 7128 MachineBasicBlock::iterator TI = IfEntry->getFirstTerminator(); 7129 assert(TI != IfEntry->end()); 7130 7131 MachineInstr *Branch = &(*TI); 7132 MachineFunction *MF = IfEntry->getParent(); 7133 MachineRegisterInfo &MRI = IfEntry->getParent()->getRegInfo(); 7134 7135 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 7136 Register DstReg = MRI.createVirtualRegister(RI.getBoolRC()); 7137 MachineInstr *SIIF = 7138 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_IF), DstReg) 7139 .add(Branch->getOperand(0)) 7140 .add(Branch->getOperand(1)); 7141 MachineInstr *SIEND = 7142 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_END_CF)) 7143 .addReg(DstReg); 7144 7145 IfEntry->erase(TI); 7146 IfEntry->insert(IfEntry->end(), SIIF); 7147 IfEnd->insert(IfEnd->getFirstNonPHI(), SIEND); 7148 } 7149 } 7150 7151 void SIInstrInfo::convertNonUniformLoopRegion( 7152 MachineBasicBlock *LoopEntry, MachineBasicBlock *LoopEnd) const { 7153 MachineBasicBlock::iterator TI = LoopEnd->getFirstTerminator(); 7154 // We expect 2 terminators, one conditional and one unconditional. 7155 assert(TI != LoopEnd->end()); 7156 7157 MachineInstr *Branch = &(*TI); 7158 MachineFunction *MF = LoopEnd->getParent(); 7159 MachineRegisterInfo &MRI = LoopEnd->getParent()->getRegInfo(); 7160 7161 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 7162 7163 Register DstReg = MRI.createVirtualRegister(RI.getBoolRC()); 7164 Register BackEdgeReg = MRI.createVirtualRegister(RI.getBoolRC()); 7165 MachineInstrBuilder HeaderPHIBuilder = 7166 BuildMI(*(MF), Branch->getDebugLoc(), get(TargetOpcode::PHI), DstReg); 7167 for (MachineBasicBlock::pred_iterator PI = LoopEntry->pred_begin(), 7168 E = LoopEntry->pred_end(); 7169 PI != E; ++PI) { 7170 if (*PI == LoopEnd) { 7171 HeaderPHIBuilder.addReg(BackEdgeReg); 7172 } else { 7173 MachineBasicBlock *PMBB = *PI; 7174 Register ZeroReg = MRI.createVirtualRegister(RI.getBoolRC()); 7175 materializeImmediate(*PMBB, PMBB->getFirstTerminator(), DebugLoc(), 7176 ZeroReg, 0); 7177 HeaderPHIBuilder.addReg(ZeroReg); 7178 } 7179 HeaderPHIBuilder.addMBB(*PI); 7180 } 7181 MachineInstr *HeaderPhi = HeaderPHIBuilder; 7182 MachineInstr *SIIFBREAK = BuildMI(*(MF), Branch->getDebugLoc(), 7183 get(AMDGPU::SI_IF_BREAK), BackEdgeReg) 7184 .addReg(DstReg) 7185 .add(Branch->getOperand(0)); 7186 MachineInstr *SILOOP = 7187 BuildMI(*(MF), Branch->getDebugLoc(), get(AMDGPU::SI_LOOP)) 7188 .addReg(BackEdgeReg) 7189 .addMBB(LoopEntry); 7190 7191 LoopEntry->insert(LoopEntry->begin(), HeaderPhi); 7192 LoopEnd->erase(TI); 7193 LoopEnd->insert(LoopEnd->end(), SIIFBREAK); 7194 LoopEnd->insert(LoopEnd->end(), SILOOP); 7195 } 7196 } 7197 7198 ArrayRef<std::pair<int, const char *>> 7199 SIInstrInfo::getSerializableTargetIndices() const { 7200 static const std::pair<int, const char *> TargetIndices[] = { 7201 {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"}, 7202 {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"}, 7203 {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"}, 7204 {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"}, 7205 {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}}; 7206 return makeArrayRef(TargetIndices); 7207 } 7208 7209 /// This is used by the post-RA scheduler (SchedulePostRAList.cpp). The 7210 /// post-RA version of misched uses CreateTargetMIHazardRecognizer. 7211 ScheduleHazardRecognizer * 7212 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 7213 const ScheduleDAG *DAG) const { 7214 return new GCNHazardRecognizer(DAG->MF); 7215 } 7216 7217 /// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer 7218 /// pass. 7219 ScheduleHazardRecognizer * 7220 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const { 7221 return new GCNHazardRecognizer(MF); 7222 } 7223 7224 std::pair<unsigned, unsigned> 7225 SIInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 7226 return std::make_pair(TF & MO_MASK, TF & ~MO_MASK); 7227 } 7228 7229 ArrayRef<std::pair<unsigned, const char *>> 7230 SIInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 7231 static const std::pair<unsigned, const char *> TargetFlags[] = { 7232 { MO_GOTPCREL, "amdgpu-gotprel" }, 7233 { MO_GOTPCREL32_LO, "amdgpu-gotprel32-lo" }, 7234 { MO_GOTPCREL32_HI, "amdgpu-gotprel32-hi" }, 7235 { MO_REL32_LO, "amdgpu-rel32-lo" }, 7236 { MO_REL32_HI, "amdgpu-rel32-hi" }, 7237 { MO_ABS32_LO, "amdgpu-abs32-lo" }, 7238 { MO_ABS32_HI, "amdgpu-abs32-hi" }, 7239 }; 7240 7241 return makeArrayRef(TargetFlags); 7242 } 7243 7244 bool SIInstrInfo::isBasicBlockPrologue(const MachineInstr &MI) const { 7245 return !MI.isTerminator() && MI.getOpcode() != AMDGPU::COPY && 7246 MI.modifiesRegister(AMDGPU::EXEC, &RI); 7247 } 7248 7249 MachineInstrBuilder 7250 SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB, 7251 MachineBasicBlock::iterator I, 7252 const DebugLoc &DL, 7253 Register DestReg) const { 7254 if (ST.hasAddNoCarry()) 7255 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e64), DestReg); 7256 7257 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7258 Register UnusedCarry = MRI.createVirtualRegister(RI.getBoolRC()); 7259 MRI.setRegAllocationHint(UnusedCarry, 0, RI.getVCC()); 7260 7261 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg) 7262 .addReg(UnusedCarry, RegState::Define | RegState::Dead); 7263 } 7264 7265 MachineInstrBuilder SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB, 7266 MachineBasicBlock::iterator I, 7267 const DebugLoc &DL, 7268 Register DestReg, 7269 RegScavenger &RS) const { 7270 if (ST.hasAddNoCarry()) 7271 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e32), DestReg); 7272 7273 // If available, prefer to use vcc. 7274 Register UnusedCarry = !RS.isRegUsed(AMDGPU::VCC) 7275 ? Register(RI.getVCC()) 7276 : RS.scavengeRegister(RI.getBoolRC(), I, 0, false); 7277 7278 // TODO: Users need to deal with this. 7279 if (!UnusedCarry.isValid()) 7280 return MachineInstrBuilder(); 7281 7282 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg) 7283 .addReg(UnusedCarry, RegState::Define | RegState::Dead); 7284 } 7285 7286 bool SIInstrInfo::isKillTerminator(unsigned Opcode) { 7287 switch (Opcode) { 7288 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR: 7289 case AMDGPU::SI_KILL_I1_TERMINATOR: 7290 return true; 7291 default: 7292 return false; 7293 } 7294 } 7295 7296 const MCInstrDesc &SIInstrInfo::getKillTerminatorFromPseudo(unsigned Opcode) const { 7297 switch (Opcode) { 7298 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO: 7299 return get(AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR); 7300 case AMDGPU::SI_KILL_I1_PSEUDO: 7301 return get(AMDGPU::SI_KILL_I1_TERMINATOR); 7302 default: 7303 llvm_unreachable("invalid opcode, expected SI_KILL_*_PSEUDO"); 7304 } 7305 } 7306 7307 void SIInstrInfo::fixImplicitOperands(MachineInstr &MI) const { 7308 if (!ST.isWave32()) 7309 return; 7310 7311 for (auto &Op : MI.implicit_operands()) { 7312 if (Op.isReg() && Op.getReg() == AMDGPU::VCC) 7313 Op.setReg(AMDGPU::VCC_LO); 7314 } 7315 } 7316 7317 bool SIInstrInfo::isBufferSMRD(const MachineInstr &MI) const { 7318 if (!isSMRD(MI)) 7319 return false; 7320 7321 // Check that it is using a buffer resource. 7322 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sbase); 7323 if (Idx == -1) // e.g. s_memtime 7324 return false; 7325 7326 const auto RCID = MI.getDesc().OpInfo[Idx].RegClass; 7327 return RI.getRegClass(RCID)->hasSubClassEq(&AMDGPU::SGPR_128RegClass); 7328 } 7329 7330 bool SIInstrInfo::isLegalFLATOffset(int64_t Offset, unsigned AddrSpace, 7331 bool Signed) const { 7332 // TODO: Should 0 be special cased? 7333 if (!ST.hasFlatInstOffsets()) 7334 return false; 7335 7336 if (ST.hasFlatSegmentOffsetBug() && AddrSpace == AMDGPUAS::FLAT_ADDRESS) 7337 return false; 7338 7339 unsigned N = AMDGPU::getNumFlatOffsetBits(ST, Signed); 7340 return Signed ? isIntN(N, Offset) : isUIntN(N, Offset); 7341 } 7342 7343 std::pair<int64_t, int64_t> SIInstrInfo::splitFlatOffset(int64_t COffsetVal, 7344 unsigned AddrSpace, 7345 bool IsSigned) const { 7346 int64_t RemainderOffset = COffsetVal; 7347 int64_t ImmField = 0; 7348 const unsigned NumBits = AMDGPU::getNumFlatOffsetBits(ST, IsSigned); 7349 if (IsSigned) { 7350 // Use signed division by a power of two to truncate towards 0. 7351 int64_t D = 1LL << (NumBits - 1); 7352 RemainderOffset = (COffsetVal / D) * D; 7353 ImmField = COffsetVal - RemainderOffset; 7354 } else if (COffsetVal >= 0) { 7355 ImmField = COffsetVal & maskTrailingOnes<uint64_t>(NumBits); 7356 RemainderOffset = COffsetVal - ImmField; 7357 } 7358 7359 assert(isLegalFLATOffset(ImmField, AddrSpace, IsSigned)); 7360 assert(RemainderOffset + ImmField == COffsetVal); 7361 return {ImmField, RemainderOffset}; 7362 } 7363 7364 // This must be kept in sync with the SIEncodingFamily class in SIInstrInfo.td 7365 enum SIEncodingFamily { 7366 SI = 0, 7367 VI = 1, 7368 SDWA = 2, 7369 SDWA9 = 3, 7370 GFX80 = 4, 7371 GFX9 = 5, 7372 GFX10 = 6, 7373 SDWA10 = 7, 7374 GFX90A = 8 7375 }; 7376 7377 static SIEncodingFamily subtargetEncodingFamily(const GCNSubtarget &ST) { 7378 switch (ST.getGeneration()) { 7379 default: 7380 break; 7381 case AMDGPUSubtarget::SOUTHERN_ISLANDS: 7382 case AMDGPUSubtarget::SEA_ISLANDS: 7383 return SIEncodingFamily::SI; 7384 case AMDGPUSubtarget::VOLCANIC_ISLANDS: 7385 case AMDGPUSubtarget::GFX9: 7386 return SIEncodingFamily::VI; 7387 case AMDGPUSubtarget::GFX10: 7388 return SIEncodingFamily::GFX10; 7389 } 7390 llvm_unreachable("Unknown subtarget generation!"); 7391 } 7392 7393 bool SIInstrInfo::isAsmOnlyOpcode(int MCOp) const { 7394 switch(MCOp) { 7395 // These opcodes use indirect register addressing so 7396 // they need special handling by codegen (currently missing). 7397 // Therefore it is too risky to allow these opcodes 7398 // to be selected by dpp combiner or sdwa peepholer. 7399 case AMDGPU::V_MOVRELS_B32_dpp_gfx10: 7400 case AMDGPU::V_MOVRELS_B32_sdwa_gfx10: 7401 case AMDGPU::V_MOVRELD_B32_dpp_gfx10: 7402 case AMDGPU::V_MOVRELD_B32_sdwa_gfx10: 7403 case AMDGPU::V_MOVRELSD_B32_dpp_gfx10: 7404 case AMDGPU::V_MOVRELSD_B32_sdwa_gfx10: 7405 case AMDGPU::V_MOVRELSD_2_B32_dpp_gfx10: 7406 case AMDGPU::V_MOVRELSD_2_B32_sdwa_gfx10: 7407 return true; 7408 default: 7409 return false; 7410 } 7411 } 7412 7413 int SIInstrInfo::pseudoToMCOpcode(int Opcode) const { 7414 SIEncodingFamily Gen = subtargetEncodingFamily(ST); 7415 7416 if ((get(Opcode).TSFlags & SIInstrFlags::renamedInGFX9) != 0 && 7417 ST.getGeneration() == AMDGPUSubtarget::GFX9) 7418 Gen = SIEncodingFamily::GFX9; 7419 7420 // Adjust the encoding family to GFX80 for D16 buffer instructions when the 7421 // subtarget has UnpackedD16VMem feature. 7422 // TODO: remove this when we discard GFX80 encoding. 7423 if (ST.hasUnpackedD16VMem() && (get(Opcode).TSFlags & SIInstrFlags::D16Buf)) 7424 Gen = SIEncodingFamily::GFX80; 7425 7426 if (get(Opcode).TSFlags & SIInstrFlags::SDWA) { 7427 switch (ST.getGeneration()) { 7428 default: 7429 Gen = SIEncodingFamily::SDWA; 7430 break; 7431 case AMDGPUSubtarget::GFX9: 7432 Gen = SIEncodingFamily::SDWA9; 7433 break; 7434 case AMDGPUSubtarget::GFX10: 7435 Gen = SIEncodingFamily::SDWA10; 7436 break; 7437 } 7438 } 7439 7440 int MCOp = AMDGPU::getMCOpcode(Opcode, Gen); 7441 7442 // -1 means that Opcode is already a native instruction. 7443 if (MCOp == -1) 7444 return Opcode; 7445 7446 if (ST.hasGFX90AInsts()) { 7447 uint16_t NMCOp = (uint16_t)-1; 7448 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX90A); 7449 if (NMCOp == (uint16_t)-1) 7450 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX9); 7451 if (NMCOp != (uint16_t)-1) 7452 MCOp = NMCOp; 7453 } 7454 7455 // (uint16_t)-1 means that Opcode is a pseudo instruction that has 7456 // no encoding in the given subtarget generation. 7457 if (MCOp == (uint16_t)-1) 7458 return -1; 7459 7460 if (isAsmOnlyOpcode(MCOp)) 7461 return -1; 7462 7463 return MCOp; 7464 } 7465 7466 static 7467 TargetInstrInfo::RegSubRegPair getRegOrUndef(const MachineOperand &RegOpnd) { 7468 assert(RegOpnd.isReg()); 7469 return RegOpnd.isUndef() ? TargetInstrInfo::RegSubRegPair() : 7470 getRegSubRegPair(RegOpnd); 7471 } 7472 7473 TargetInstrInfo::RegSubRegPair 7474 llvm::getRegSequenceSubReg(MachineInstr &MI, unsigned SubReg) { 7475 assert(MI.isRegSequence()); 7476 for (unsigned I = 0, E = (MI.getNumOperands() - 1)/ 2; I < E; ++I) 7477 if (MI.getOperand(1 + 2 * I + 1).getImm() == SubReg) { 7478 auto &RegOp = MI.getOperand(1 + 2 * I); 7479 return getRegOrUndef(RegOp); 7480 } 7481 return TargetInstrInfo::RegSubRegPair(); 7482 } 7483 7484 // Try to find the definition of reg:subreg in subreg-manipulation pseudos 7485 // Following a subreg of reg:subreg isn't supported 7486 static bool followSubRegDef(MachineInstr &MI, 7487 TargetInstrInfo::RegSubRegPair &RSR) { 7488 if (!RSR.SubReg) 7489 return false; 7490 switch (MI.getOpcode()) { 7491 default: break; 7492 case AMDGPU::REG_SEQUENCE: 7493 RSR = getRegSequenceSubReg(MI, RSR.SubReg); 7494 return true; 7495 // EXTRACT_SUBREG ins't supported as this would follow a subreg of subreg 7496 case AMDGPU::INSERT_SUBREG: 7497 if (RSR.SubReg == (unsigned)MI.getOperand(3).getImm()) 7498 // inserted the subreg we're looking for 7499 RSR = getRegOrUndef(MI.getOperand(2)); 7500 else { // the subreg in the rest of the reg 7501 auto R1 = getRegOrUndef(MI.getOperand(1)); 7502 if (R1.SubReg) // subreg of subreg isn't supported 7503 return false; 7504 RSR.Reg = R1.Reg; 7505 } 7506 return true; 7507 } 7508 return false; 7509 } 7510 7511 MachineInstr *llvm::getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P, 7512 MachineRegisterInfo &MRI) { 7513 assert(MRI.isSSA()); 7514 if (!P.Reg.isVirtual()) 7515 return nullptr; 7516 7517 auto RSR = P; 7518 auto *DefInst = MRI.getVRegDef(RSR.Reg); 7519 while (auto *MI = DefInst) { 7520 DefInst = nullptr; 7521 switch (MI->getOpcode()) { 7522 case AMDGPU::COPY: 7523 case AMDGPU::V_MOV_B32_e32: { 7524 auto &Op1 = MI->getOperand(1); 7525 if (Op1.isReg() && Op1.getReg().isVirtual()) { 7526 if (Op1.isUndef()) 7527 return nullptr; 7528 RSR = getRegSubRegPair(Op1); 7529 DefInst = MRI.getVRegDef(RSR.Reg); 7530 } 7531 break; 7532 } 7533 default: 7534 if (followSubRegDef(*MI, RSR)) { 7535 if (!RSR.Reg) 7536 return nullptr; 7537 DefInst = MRI.getVRegDef(RSR.Reg); 7538 } 7539 } 7540 if (!DefInst) 7541 return MI; 7542 } 7543 return nullptr; 7544 } 7545 7546 bool llvm::execMayBeModifiedBeforeUse(const MachineRegisterInfo &MRI, 7547 Register VReg, 7548 const MachineInstr &DefMI, 7549 const MachineInstr &UseMI) { 7550 assert(MRI.isSSA() && "Must be run on SSA"); 7551 7552 auto *TRI = MRI.getTargetRegisterInfo(); 7553 auto *DefBB = DefMI.getParent(); 7554 7555 // Don't bother searching between blocks, although it is possible this block 7556 // doesn't modify exec. 7557 if (UseMI.getParent() != DefBB) 7558 return true; 7559 7560 const int MaxInstScan = 20; 7561 int NumInst = 0; 7562 7563 // Stop scan at the use. 7564 auto E = UseMI.getIterator(); 7565 for (auto I = std::next(DefMI.getIterator()); I != E; ++I) { 7566 if (I->isDebugInstr()) 7567 continue; 7568 7569 if (++NumInst > MaxInstScan) 7570 return true; 7571 7572 if (I->modifiesRegister(AMDGPU::EXEC, TRI)) 7573 return true; 7574 } 7575 7576 return false; 7577 } 7578 7579 bool llvm::execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo &MRI, 7580 Register VReg, 7581 const MachineInstr &DefMI) { 7582 assert(MRI.isSSA() && "Must be run on SSA"); 7583 7584 auto *TRI = MRI.getTargetRegisterInfo(); 7585 auto *DefBB = DefMI.getParent(); 7586 7587 const int MaxUseScan = 10; 7588 int NumUse = 0; 7589 7590 for (auto &Use : MRI.use_nodbg_operands(VReg)) { 7591 auto &UseInst = *Use.getParent(); 7592 // Don't bother searching between blocks, although it is possible this block 7593 // doesn't modify exec. 7594 if (UseInst.getParent() != DefBB) 7595 return true; 7596 7597 if (++NumUse > MaxUseScan) 7598 return true; 7599 } 7600 7601 if (NumUse == 0) 7602 return false; 7603 7604 const int MaxInstScan = 20; 7605 int NumInst = 0; 7606 7607 // Stop scan when we have seen all the uses. 7608 for (auto I = std::next(DefMI.getIterator()); ; ++I) { 7609 assert(I != DefBB->end()); 7610 7611 if (I->isDebugInstr()) 7612 continue; 7613 7614 if (++NumInst > MaxInstScan) 7615 return true; 7616 7617 for (const MachineOperand &Op : I->operands()) { 7618 // We don't check reg masks here as they're used only on calls: 7619 // 1. EXEC is only considered const within one BB 7620 // 2. Call should be a terminator instruction if present in a BB 7621 7622 if (!Op.isReg()) 7623 continue; 7624 7625 Register Reg = Op.getReg(); 7626 if (Op.isUse()) { 7627 if (Reg == VReg && --NumUse == 0) 7628 return false; 7629 } else if (TRI->regsOverlap(Reg, AMDGPU::EXEC)) 7630 return true; 7631 } 7632 } 7633 } 7634 7635 MachineInstr *SIInstrInfo::createPHIDestinationCopy( 7636 MachineBasicBlock &MBB, MachineBasicBlock::iterator LastPHIIt, 7637 const DebugLoc &DL, Register Src, Register Dst) const { 7638 auto Cur = MBB.begin(); 7639 if (Cur != MBB.end()) 7640 do { 7641 if (!Cur->isPHI() && Cur->readsRegister(Dst)) 7642 return BuildMI(MBB, Cur, DL, get(TargetOpcode::COPY), Dst).addReg(Src); 7643 ++Cur; 7644 } while (Cur != MBB.end() && Cur != LastPHIIt); 7645 7646 return TargetInstrInfo::createPHIDestinationCopy(MBB, LastPHIIt, DL, Src, 7647 Dst); 7648 } 7649 7650 MachineInstr *SIInstrInfo::createPHISourceCopy( 7651 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt, 7652 const DebugLoc &DL, Register Src, unsigned SrcSubReg, Register Dst) const { 7653 if (InsPt != MBB.end() && 7654 (InsPt->getOpcode() == AMDGPU::SI_IF || 7655 InsPt->getOpcode() == AMDGPU::SI_ELSE || 7656 InsPt->getOpcode() == AMDGPU::SI_IF_BREAK) && 7657 InsPt->definesRegister(Src)) { 7658 InsPt++; 7659 return BuildMI(MBB, InsPt, DL, 7660 get(ST.isWave32() ? AMDGPU::S_MOV_B32_term 7661 : AMDGPU::S_MOV_B64_term), 7662 Dst) 7663 .addReg(Src, 0, SrcSubReg) 7664 .addReg(AMDGPU::EXEC, RegState::Implicit); 7665 } 7666 return TargetInstrInfo::createPHISourceCopy(MBB, InsPt, DL, Src, SrcSubReg, 7667 Dst); 7668 } 7669 7670 bool llvm::SIInstrInfo::isWave32() const { return ST.isWave32(); } 7671 7672 MachineInstr *SIInstrInfo::foldMemoryOperandImpl( 7673 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 7674 MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS, 7675 VirtRegMap *VRM) const { 7676 // This is a bit of a hack (copied from AArch64). Consider this instruction: 7677 // 7678 // %0:sreg_32 = COPY $m0 7679 // 7680 // We explicitly chose SReg_32 for the virtual register so such a copy might 7681 // be eliminated by RegisterCoalescer. However, that may not be possible, and 7682 // %0 may even spill. We can't spill $m0 normally (it would require copying to 7683 // a numbered SGPR anyway), and since it is in the SReg_32 register class, 7684 // TargetInstrInfo::foldMemoryOperand() is going to try. 7685 // A similar issue also exists with spilling and reloading $exec registers. 7686 // 7687 // To prevent that, constrain the %0 register class here. 7688 if (MI.isFullCopy()) { 7689 Register DstReg = MI.getOperand(0).getReg(); 7690 Register SrcReg = MI.getOperand(1).getReg(); 7691 if ((DstReg.isVirtual() || SrcReg.isVirtual()) && 7692 (DstReg.isVirtual() != SrcReg.isVirtual())) { 7693 MachineRegisterInfo &MRI = MF.getRegInfo(); 7694 Register VirtReg = DstReg.isVirtual() ? DstReg : SrcReg; 7695 const TargetRegisterClass *RC = MRI.getRegClass(VirtReg); 7696 if (RC->hasSuperClassEq(&AMDGPU::SReg_32RegClass)) { 7697 MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_32_XM0_XEXECRegClass); 7698 return nullptr; 7699 } else if (RC->hasSuperClassEq(&AMDGPU::SReg_64RegClass)) { 7700 MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_64_XEXECRegClass); 7701 return nullptr; 7702 } 7703 } 7704 } 7705 7706 return nullptr; 7707 } 7708 7709 unsigned SIInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 7710 const MachineInstr &MI, 7711 unsigned *PredCost) const { 7712 if (MI.isBundle()) { 7713 MachineBasicBlock::const_instr_iterator I(MI.getIterator()); 7714 MachineBasicBlock::const_instr_iterator E(MI.getParent()->instr_end()); 7715 unsigned Lat = 0, Count = 0; 7716 for (++I; I != E && I->isBundledWithPred(); ++I) { 7717 ++Count; 7718 Lat = std::max(Lat, SchedModel.computeInstrLatency(&*I)); 7719 } 7720 return Lat + Count - 1; 7721 } 7722 7723 return SchedModel.computeInstrLatency(&MI); 7724 } 7725 7726 unsigned SIInstrInfo::getDSShaderTypeValue(const MachineFunction &MF) { 7727 switch (MF.getFunction().getCallingConv()) { 7728 case CallingConv::AMDGPU_PS: 7729 return 1; 7730 case CallingConv::AMDGPU_VS: 7731 return 2; 7732 case CallingConv::AMDGPU_GS: 7733 return 3; 7734 case CallingConv::AMDGPU_HS: 7735 case CallingConv::AMDGPU_LS: 7736 case CallingConv::AMDGPU_ES: 7737 report_fatal_error("ds_ordered_count unsupported for this calling conv"); 7738 case CallingConv::AMDGPU_CS: 7739 case CallingConv::AMDGPU_KERNEL: 7740 case CallingConv::C: 7741 case CallingConv::Fast: 7742 default: 7743 // Assume other calling conventions are various compute callable functions 7744 return 0; 7745 } 7746 } 7747