1 //===-- SIInstrInfo.cpp - SI Instruction Information ---------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 /// \file 11 /// \brief SI Implementation of TargetInstrInfo. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "SIInstrInfo.h" 16 #include "AMDGPUTargetMachine.h" 17 #include "GCNHazardRecognizer.h" 18 #include "SIDefines.h" 19 #include "SIMachineFunctionInfo.h" 20 #include "llvm/CodeGen/MachineFrameInfo.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/MachineRegisterInfo.h" 23 #include "llvm/CodeGen/ScheduleDAG.h" 24 #include "llvm/IR/Function.h" 25 #include "llvm/CodeGen/RegisterScavenging.h" 26 #include "llvm/MC/MCInstrDesc.h" 27 #include "llvm/Support/Debug.h" 28 29 using namespace llvm; 30 31 // Must be at least 4 to be able to branch over minimum unconditional branch 32 // code. This is only for making it possible to write reasonably small tests for 33 // long branches. 34 static cl::opt<unsigned> 35 BranchOffsetBits("amdgpu-s-branch-bits", cl::ReallyHidden, cl::init(16), 36 cl::desc("Restrict range of branch instructions (DEBUG)")); 37 38 SIInstrInfo::SIInstrInfo(const SISubtarget &ST) 39 : AMDGPUInstrInfo(ST), RI(), ST(ST) {} 40 41 //===----------------------------------------------------------------------===// 42 // TargetInstrInfo callbacks 43 //===----------------------------------------------------------------------===// 44 45 static unsigned getNumOperandsNoGlue(SDNode *Node) { 46 unsigned N = Node->getNumOperands(); 47 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue) 48 --N; 49 return N; 50 } 51 52 static SDValue findChainOperand(SDNode *Load) { 53 SDValue LastOp = Load->getOperand(getNumOperandsNoGlue(Load) - 1); 54 assert(LastOp.getValueType() == MVT::Other && "Chain missing from load node"); 55 return LastOp; 56 } 57 58 /// \brief Returns true if both nodes have the same value for the given 59 /// operand \p Op, or if both nodes do not have this operand. 60 static bool nodesHaveSameOperandValue(SDNode *N0, SDNode* N1, unsigned OpName) { 61 unsigned Opc0 = N0->getMachineOpcode(); 62 unsigned Opc1 = N1->getMachineOpcode(); 63 64 int Op0Idx = AMDGPU::getNamedOperandIdx(Opc0, OpName); 65 int Op1Idx = AMDGPU::getNamedOperandIdx(Opc1, OpName); 66 67 if (Op0Idx == -1 && Op1Idx == -1) 68 return true; 69 70 71 if ((Op0Idx == -1 && Op1Idx != -1) || 72 (Op1Idx == -1 && Op0Idx != -1)) 73 return false; 74 75 // getNamedOperandIdx returns the index for the MachineInstr's operands, 76 // which includes the result as the first operand. We are indexing into the 77 // MachineSDNode's operands, so we need to skip the result operand to get 78 // the real index. 79 --Op0Idx; 80 --Op1Idx; 81 82 return N0->getOperand(Op0Idx) == N1->getOperand(Op1Idx); 83 } 84 85 bool SIInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI, 86 AliasAnalysis *AA) const { 87 // TODO: The generic check fails for VALU instructions that should be 88 // rematerializable due to implicit reads of exec. We really want all of the 89 // generic logic for this except for this. 90 switch (MI.getOpcode()) { 91 case AMDGPU::V_MOV_B32_e32: 92 case AMDGPU::V_MOV_B32_e64: 93 case AMDGPU::V_MOV_B64_PSEUDO: 94 return true; 95 default: 96 return false; 97 } 98 } 99 100 bool SIInstrInfo::areLoadsFromSameBasePtr(SDNode *Load0, SDNode *Load1, 101 int64_t &Offset0, 102 int64_t &Offset1) const { 103 if (!Load0->isMachineOpcode() || !Load1->isMachineOpcode()) 104 return false; 105 106 unsigned Opc0 = Load0->getMachineOpcode(); 107 unsigned Opc1 = Load1->getMachineOpcode(); 108 109 // Make sure both are actually loads. 110 if (!get(Opc0).mayLoad() || !get(Opc1).mayLoad()) 111 return false; 112 113 if (isDS(Opc0) && isDS(Opc1)) { 114 115 // FIXME: Handle this case: 116 if (getNumOperandsNoGlue(Load0) != getNumOperandsNoGlue(Load1)) 117 return false; 118 119 // Check base reg. 120 if (Load0->getOperand(1) != Load1->getOperand(1)) 121 return false; 122 123 // Check chain. 124 if (findChainOperand(Load0) != findChainOperand(Load1)) 125 return false; 126 127 // Skip read2 / write2 variants for simplicity. 128 // TODO: We should report true if the used offsets are adjacent (excluded 129 // st64 versions). 130 if (AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::data1) != -1 || 131 AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::data1) != -1) 132 return false; 133 134 Offset0 = cast<ConstantSDNode>(Load0->getOperand(2))->getZExtValue(); 135 Offset1 = cast<ConstantSDNode>(Load1->getOperand(2))->getZExtValue(); 136 return true; 137 } 138 139 if (isSMRD(Opc0) && isSMRD(Opc1)) { 140 assert(getNumOperandsNoGlue(Load0) == getNumOperandsNoGlue(Load1)); 141 142 // Check base reg. 143 if (Load0->getOperand(0) != Load1->getOperand(0)) 144 return false; 145 146 const ConstantSDNode *Load0Offset = 147 dyn_cast<ConstantSDNode>(Load0->getOperand(1)); 148 const ConstantSDNode *Load1Offset = 149 dyn_cast<ConstantSDNode>(Load1->getOperand(1)); 150 151 if (!Load0Offset || !Load1Offset) 152 return false; 153 154 // Check chain. 155 if (findChainOperand(Load0) != findChainOperand(Load1)) 156 return false; 157 158 Offset0 = Load0Offset->getZExtValue(); 159 Offset1 = Load1Offset->getZExtValue(); 160 return true; 161 } 162 163 // MUBUF and MTBUF can access the same addresses. 164 if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1))) { 165 166 // MUBUF and MTBUF have vaddr at different indices. 167 if (!nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::soffset) || 168 findChainOperand(Load0) != findChainOperand(Load1) || 169 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::vaddr) || 170 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::srsrc)) 171 return false; 172 173 int OffIdx0 = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset); 174 int OffIdx1 = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset); 175 176 if (OffIdx0 == -1 || OffIdx1 == -1) 177 return false; 178 179 // getNamedOperandIdx returns the index for MachineInstrs. Since they 180 // inlcude the output in the operand list, but SDNodes don't, we need to 181 // subtract the index by one. 182 --OffIdx0; 183 --OffIdx1; 184 185 SDValue Off0 = Load0->getOperand(OffIdx0); 186 SDValue Off1 = Load1->getOperand(OffIdx1); 187 188 // The offset might be a FrameIndexSDNode. 189 if (!isa<ConstantSDNode>(Off0) || !isa<ConstantSDNode>(Off1)) 190 return false; 191 192 Offset0 = cast<ConstantSDNode>(Off0)->getZExtValue(); 193 Offset1 = cast<ConstantSDNode>(Off1)->getZExtValue(); 194 return true; 195 } 196 197 return false; 198 } 199 200 static bool isStride64(unsigned Opc) { 201 switch (Opc) { 202 case AMDGPU::DS_READ2ST64_B32: 203 case AMDGPU::DS_READ2ST64_B64: 204 case AMDGPU::DS_WRITE2ST64_B32: 205 case AMDGPU::DS_WRITE2ST64_B64: 206 return true; 207 default: 208 return false; 209 } 210 } 211 212 bool SIInstrInfo::getMemOpBaseRegImmOfs(MachineInstr &LdSt, unsigned &BaseReg, 213 int64_t &Offset, 214 const TargetRegisterInfo *TRI) const { 215 unsigned Opc = LdSt.getOpcode(); 216 217 if (isDS(LdSt)) { 218 const MachineOperand *OffsetImm = 219 getNamedOperand(LdSt, AMDGPU::OpName::offset); 220 if (OffsetImm) { 221 // Normal, single offset LDS instruction. 222 const MachineOperand *AddrReg = 223 getNamedOperand(LdSt, AMDGPU::OpName::addr); 224 225 BaseReg = AddrReg->getReg(); 226 Offset = OffsetImm->getImm(); 227 return true; 228 } 229 230 // The 2 offset instructions use offset0 and offset1 instead. We can treat 231 // these as a load with a single offset if the 2 offsets are consecutive. We 232 // will use this for some partially aligned loads. 233 const MachineOperand *Offset0Imm = 234 getNamedOperand(LdSt, AMDGPU::OpName::offset0); 235 const MachineOperand *Offset1Imm = 236 getNamedOperand(LdSt, AMDGPU::OpName::offset1); 237 238 uint8_t Offset0 = Offset0Imm->getImm(); 239 uint8_t Offset1 = Offset1Imm->getImm(); 240 241 if (Offset1 > Offset0 && Offset1 - Offset0 == 1) { 242 // Each of these offsets is in element sized units, so we need to convert 243 // to bytes of the individual reads. 244 245 unsigned EltSize; 246 if (LdSt.mayLoad()) 247 EltSize = getOpRegClass(LdSt, 0)->getSize() / 2; 248 else { 249 assert(LdSt.mayStore()); 250 int Data0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0); 251 EltSize = getOpRegClass(LdSt, Data0Idx)->getSize(); 252 } 253 254 if (isStride64(Opc)) 255 EltSize *= 64; 256 257 const MachineOperand *AddrReg = 258 getNamedOperand(LdSt, AMDGPU::OpName::addr); 259 BaseReg = AddrReg->getReg(); 260 Offset = EltSize * Offset0; 261 return true; 262 } 263 264 return false; 265 } 266 267 if (isMUBUF(LdSt) || isMTBUF(LdSt)) { 268 const MachineOperand *SOffset = getNamedOperand(LdSt, AMDGPU::OpName::soffset); 269 if (SOffset && SOffset->isReg()) 270 return false; 271 272 const MachineOperand *AddrReg = 273 getNamedOperand(LdSt, AMDGPU::OpName::vaddr); 274 if (!AddrReg) 275 return false; 276 277 const MachineOperand *OffsetImm = 278 getNamedOperand(LdSt, AMDGPU::OpName::offset); 279 BaseReg = AddrReg->getReg(); 280 Offset = OffsetImm->getImm(); 281 282 if (SOffset) // soffset can be an inline immediate. 283 Offset += SOffset->getImm(); 284 285 return true; 286 } 287 288 if (isSMRD(LdSt)) { 289 const MachineOperand *OffsetImm = 290 getNamedOperand(LdSt, AMDGPU::OpName::offset); 291 if (!OffsetImm) 292 return false; 293 294 const MachineOperand *SBaseReg = 295 getNamedOperand(LdSt, AMDGPU::OpName::sbase); 296 BaseReg = SBaseReg->getReg(); 297 Offset = OffsetImm->getImm(); 298 return true; 299 } 300 301 if (isFLAT(LdSt)) { 302 const MachineOperand *AddrReg = getNamedOperand(LdSt, AMDGPU::OpName::vaddr); 303 BaseReg = AddrReg->getReg(); 304 Offset = 0; 305 return true; 306 } 307 308 return false; 309 } 310 311 bool SIInstrInfo::shouldClusterMemOps(MachineInstr &FirstLdSt, 312 MachineInstr &SecondLdSt, 313 unsigned NumLoads) const { 314 const MachineOperand *FirstDst = nullptr; 315 const MachineOperand *SecondDst = nullptr; 316 317 if ((isMUBUF(FirstLdSt) && isMUBUF(SecondLdSt)) || 318 (isMTBUF(FirstLdSt) && isMTBUF(SecondLdSt))) { 319 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::vdata); 320 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::vdata); 321 } else if (isSMRD(FirstLdSt) && isSMRD(SecondLdSt)) { 322 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::sdst); 323 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::sdst); 324 } else if (isDS(FirstLdSt) && isDS(SecondLdSt)) { 325 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::vdst); 326 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::vdst); 327 } 328 329 if (!FirstDst || !SecondDst) 330 return false; 331 332 // Try to limit clustering based on the total number of bytes loaded 333 // rather than the number of instructions. This is done to help reduce 334 // register pressure. The method used is somewhat inexact, though, 335 // because it assumes that all loads in the cluster will load the 336 // same number of bytes as FirstLdSt. 337 338 // The unit of this value is bytes. 339 // FIXME: This needs finer tuning. 340 unsigned LoadClusterThreshold = 16; 341 342 const MachineRegisterInfo &MRI = 343 FirstLdSt.getParent()->getParent()->getRegInfo(); 344 const TargetRegisterClass *DstRC = MRI.getRegClass(FirstDst->getReg()); 345 346 return (NumLoads * DstRC->getSize()) <= LoadClusterThreshold; 347 } 348 349 void SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 350 MachineBasicBlock::iterator MI, 351 const DebugLoc &DL, unsigned DestReg, 352 unsigned SrcReg, bool KillSrc) const { 353 const TargetRegisterClass *RC = RI.getPhysRegClass(DestReg); 354 355 if (RC == &AMDGPU::VGPR_32RegClass) { 356 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) || 357 AMDGPU::SReg_32RegClass.contains(SrcReg)); 358 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg) 359 .addReg(SrcReg, getKillRegState(KillSrc)); 360 return; 361 } 362 363 if (RC == &AMDGPU::SReg_32_XM0RegClass || 364 RC == &AMDGPU::SReg_32RegClass) { 365 if (SrcReg == AMDGPU::SCC) { 366 BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B32), DestReg) 367 .addImm(-1) 368 .addImm(0); 369 return; 370 } 371 372 assert(AMDGPU::SReg_32RegClass.contains(SrcReg)); 373 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg) 374 .addReg(SrcReg, getKillRegState(KillSrc)); 375 return; 376 } 377 378 if (RC == &AMDGPU::SReg_64RegClass) { 379 if (DestReg == AMDGPU::VCC) { 380 if (AMDGPU::SReg_64RegClass.contains(SrcReg)) { 381 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), AMDGPU::VCC) 382 .addReg(SrcReg, getKillRegState(KillSrc)); 383 } else { 384 // FIXME: Hack until VReg_1 removed. 385 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg)); 386 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32)) 387 .addImm(0) 388 .addReg(SrcReg, getKillRegState(KillSrc)); 389 } 390 391 return; 392 } 393 394 assert(AMDGPU::SReg_64RegClass.contains(SrcReg)); 395 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg) 396 .addReg(SrcReg, getKillRegState(KillSrc)); 397 return; 398 } 399 400 if (DestReg == AMDGPU::SCC) { 401 assert(AMDGPU::SReg_32RegClass.contains(SrcReg)); 402 BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U32)) 403 .addReg(SrcReg, getKillRegState(KillSrc)) 404 .addImm(0); 405 return; 406 } 407 408 unsigned EltSize = 4; 409 unsigned Opcode = AMDGPU::V_MOV_B32_e32; 410 if (RI.isSGPRClass(RC)) { 411 if (RC->getSize() > 4) { 412 Opcode = AMDGPU::S_MOV_B64; 413 EltSize = 8; 414 } else { 415 Opcode = AMDGPU::S_MOV_B32; 416 EltSize = 4; 417 } 418 } 419 420 ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RC, EltSize); 421 bool Forward = RI.getHWRegIndex(DestReg) <= RI.getHWRegIndex(SrcReg); 422 423 for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) { 424 unsigned SubIdx; 425 if (Forward) 426 SubIdx = SubIndices[Idx]; 427 else 428 SubIdx = SubIndices[SubIndices.size() - Idx - 1]; 429 430 MachineInstrBuilder Builder = BuildMI(MBB, MI, DL, 431 get(Opcode), RI.getSubReg(DestReg, SubIdx)); 432 433 Builder.addReg(RI.getSubReg(SrcReg, SubIdx)); 434 435 if (Idx == SubIndices.size() - 1) 436 Builder.addReg(SrcReg, getKillRegState(KillSrc) | RegState::Implicit); 437 438 if (Idx == 0) 439 Builder.addReg(DestReg, RegState::Define | RegState::Implicit); 440 441 Builder.addReg(SrcReg, RegState::Implicit); 442 } 443 } 444 445 int SIInstrInfo::commuteOpcode(unsigned Opcode) const { 446 int NewOpc; 447 448 // Try to map original to commuted opcode 449 NewOpc = AMDGPU::getCommuteRev(Opcode); 450 if (NewOpc != -1) 451 // Check if the commuted (REV) opcode exists on the target. 452 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1; 453 454 // Try to map commuted to original opcode 455 NewOpc = AMDGPU::getCommuteOrig(Opcode); 456 if (NewOpc != -1) 457 // Check if the original (non-REV) opcode exists on the target. 458 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1; 459 460 return Opcode; 461 } 462 463 unsigned SIInstrInfo::getMovOpcode(const TargetRegisterClass *DstRC) const { 464 465 if (DstRC->getSize() == 4) { 466 return RI.isSGPRClass(DstRC) ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32; 467 } else if (DstRC->getSize() == 8 && RI.isSGPRClass(DstRC)) { 468 return AMDGPU::S_MOV_B64; 469 } else if (DstRC->getSize() == 8 && !RI.isSGPRClass(DstRC)) { 470 return AMDGPU::V_MOV_B64_PSEUDO; 471 } 472 return AMDGPU::COPY; 473 } 474 475 static unsigned getSGPRSpillSaveOpcode(unsigned Size) { 476 switch (Size) { 477 case 4: 478 return AMDGPU::SI_SPILL_S32_SAVE; 479 case 8: 480 return AMDGPU::SI_SPILL_S64_SAVE; 481 case 16: 482 return AMDGPU::SI_SPILL_S128_SAVE; 483 case 32: 484 return AMDGPU::SI_SPILL_S256_SAVE; 485 case 64: 486 return AMDGPU::SI_SPILL_S512_SAVE; 487 default: 488 llvm_unreachable("unknown register size"); 489 } 490 } 491 492 static unsigned getVGPRSpillSaveOpcode(unsigned Size) { 493 switch (Size) { 494 case 4: 495 return AMDGPU::SI_SPILL_V32_SAVE; 496 case 8: 497 return AMDGPU::SI_SPILL_V64_SAVE; 498 case 12: 499 return AMDGPU::SI_SPILL_V96_SAVE; 500 case 16: 501 return AMDGPU::SI_SPILL_V128_SAVE; 502 case 32: 503 return AMDGPU::SI_SPILL_V256_SAVE; 504 case 64: 505 return AMDGPU::SI_SPILL_V512_SAVE; 506 default: 507 llvm_unreachable("unknown register size"); 508 } 509 } 510 511 void SIInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 512 MachineBasicBlock::iterator MI, 513 unsigned SrcReg, bool isKill, 514 int FrameIndex, 515 const TargetRegisterClass *RC, 516 const TargetRegisterInfo *TRI) const { 517 MachineFunction *MF = MBB.getParent(); 518 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 519 MachineFrameInfo &FrameInfo = MF->getFrameInfo(); 520 DebugLoc DL = MBB.findDebugLoc(MI); 521 522 unsigned Size = FrameInfo.getObjectSize(FrameIndex); 523 unsigned Align = FrameInfo.getObjectAlignment(FrameIndex); 524 MachinePointerInfo PtrInfo 525 = MachinePointerInfo::getFixedStack(*MF, FrameIndex); 526 MachineMemOperand *MMO 527 = MF->getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore, 528 Size, Align); 529 530 if (RI.isSGPRClass(RC)) { 531 MFI->setHasSpilledSGPRs(); 532 533 // We are only allowed to create one new instruction when spilling 534 // registers, so we need to use pseudo instruction for spilling SGPRs. 535 const MCInstrDesc &OpDesc = get(getSGPRSpillSaveOpcode(RC->getSize())); 536 537 // The SGPR spill/restore instructions only work on number sgprs, so we need 538 // to make sure we are using the correct register class. 539 if (TargetRegisterInfo::isVirtualRegister(SrcReg) && RC->getSize() == 4) { 540 MachineRegisterInfo &MRI = MF->getRegInfo(); 541 MRI.constrainRegClass(SrcReg, &AMDGPU::SReg_32_XM0RegClass); 542 } 543 544 MachineInstrBuilder Spill = BuildMI(MBB, MI, DL, OpDesc) 545 .addReg(SrcReg, getKillRegState(isKill)) // data 546 .addFrameIndex(FrameIndex) // addr 547 .addMemOperand(MMO) 548 .addReg(MFI->getScratchRSrcReg(), RegState::Implicit) 549 .addReg(MFI->getScratchWaveOffsetReg(), RegState::Implicit); 550 // Add the scratch resource registers as implicit uses because we may end up 551 // needing them, and need to ensure that the reserved registers are 552 // correctly handled. 553 554 if (ST.hasScalarStores()) { 555 // m0 is used for offset to scalar stores if used to spill. 556 Spill.addReg(AMDGPU::M0, RegState::ImplicitDefine); 557 } 558 559 return; 560 } 561 562 if (!ST.isVGPRSpillingEnabled(*MF->getFunction())) { 563 LLVMContext &Ctx = MF->getFunction()->getContext(); 564 Ctx.emitError("SIInstrInfo::storeRegToStackSlot - Do not know how to" 565 " spill register"); 566 BuildMI(MBB, MI, DL, get(AMDGPU::KILL)) 567 .addReg(SrcReg); 568 569 return; 570 } 571 572 assert(RI.hasVGPRs(RC) && "Only VGPR spilling expected"); 573 574 unsigned Opcode = getVGPRSpillSaveOpcode(RC->getSize()); 575 MFI->setHasSpilledVGPRs(); 576 BuildMI(MBB, MI, DL, get(Opcode)) 577 .addReg(SrcReg, getKillRegState(isKill)) // data 578 .addFrameIndex(FrameIndex) // addr 579 .addReg(MFI->getScratchRSrcReg()) // scratch_rsrc 580 .addReg(MFI->getScratchWaveOffsetReg()) // scratch_offset 581 .addImm(0) // offset 582 .addMemOperand(MMO); 583 } 584 585 static unsigned getSGPRSpillRestoreOpcode(unsigned Size) { 586 switch (Size) { 587 case 4: 588 return AMDGPU::SI_SPILL_S32_RESTORE; 589 case 8: 590 return AMDGPU::SI_SPILL_S64_RESTORE; 591 case 16: 592 return AMDGPU::SI_SPILL_S128_RESTORE; 593 case 32: 594 return AMDGPU::SI_SPILL_S256_RESTORE; 595 case 64: 596 return AMDGPU::SI_SPILL_S512_RESTORE; 597 default: 598 llvm_unreachable("unknown register size"); 599 } 600 } 601 602 static unsigned getVGPRSpillRestoreOpcode(unsigned Size) { 603 switch (Size) { 604 case 4: 605 return AMDGPU::SI_SPILL_V32_RESTORE; 606 case 8: 607 return AMDGPU::SI_SPILL_V64_RESTORE; 608 case 12: 609 return AMDGPU::SI_SPILL_V96_RESTORE; 610 case 16: 611 return AMDGPU::SI_SPILL_V128_RESTORE; 612 case 32: 613 return AMDGPU::SI_SPILL_V256_RESTORE; 614 case 64: 615 return AMDGPU::SI_SPILL_V512_RESTORE; 616 default: 617 llvm_unreachable("unknown register size"); 618 } 619 } 620 621 void SIInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 622 MachineBasicBlock::iterator MI, 623 unsigned DestReg, int FrameIndex, 624 const TargetRegisterClass *RC, 625 const TargetRegisterInfo *TRI) const { 626 MachineFunction *MF = MBB.getParent(); 627 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 628 MachineFrameInfo &FrameInfo = MF->getFrameInfo(); 629 DebugLoc DL = MBB.findDebugLoc(MI); 630 unsigned Align = FrameInfo.getObjectAlignment(FrameIndex); 631 unsigned Size = FrameInfo.getObjectSize(FrameIndex); 632 633 MachinePointerInfo PtrInfo 634 = MachinePointerInfo::getFixedStack(*MF, FrameIndex); 635 636 MachineMemOperand *MMO = MF->getMachineMemOperand( 637 PtrInfo, MachineMemOperand::MOLoad, Size, Align); 638 639 if (RI.isSGPRClass(RC)) { 640 // FIXME: Maybe this should not include a memoperand because it will be 641 // lowered to non-memory instructions. 642 const MCInstrDesc &OpDesc = get(getSGPRSpillRestoreOpcode(RC->getSize())); 643 if (TargetRegisterInfo::isVirtualRegister(DestReg) && RC->getSize() == 4) { 644 MachineRegisterInfo &MRI = MF->getRegInfo(); 645 MRI.constrainRegClass(DestReg, &AMDGPU::SReg_32_XM0RegClass); 646 } 647 648 MachineInstrBuilder Spill = BuildMI(MBB, MI, DL, OpDesc, DestReg) 649 .addFrameIndex(FrameIndex) // addr 650 .addMemOperand(MMO) 651 .addReg(MFI->getScratchRSrcReg(), RegState::Implicit) 652 .addReg(MFI->getScratchWaveOffsetReg(), RegState::Implicit); 653 654 if (ST.hasScalarStores()) { 655 // m0 is used for offset to scalar stores if used to spill. 656 Spill.addReg(AMDGPU::M0, RegState::ImplicitDefine); 657 } 658 659 return; 660 } 661 662 if (!ST.isVGPRSpillingEnabled(*MF->getFunction())) { 663 LLVMContext &Ctx = MF->getFunction()->getContext(); 664 Ctx.emitError("SIInstrInfo::loadRegFromStackSlot - Do not know how to" 665 " restore register"); 666 BuildMI(MBB, MI, DL, get(AMDGPU::IMPLICIT_DEF), DestReg); 667 668 return; 669 } 670 671 assert(RI.hasVGPRs(RC) && "Only VGPR spilling expected"); 672 673 unsigned Opcode = getVGPRSpillRestoreOpcode(RC->getSize()); 674 BuildMI(MBB, MI, DL, get(Opcode), DestReg) 675 .addFrameIndex(FrameIndex) // vaddr 676 .addReg(MFI->getScratchRSrcReg()) // scratch_rsrc 677 .addReg(MFI->getScratchWaveOffsetReg()) // scratch_offset 678 .addImm(0) // offset 679 .addMemOperand(MMO); 680 } 681 682 /// \param @Offset Offset in bytes of the FrameIndex being spilled 683 unsigned SIInstrInfo::calculateLDSSpillAddress( 684 MachineBasicBlock &MBB, MachineInstr &MI, RegScavenger *RS, unsigned TmpReg, 685 unsigned FrameOffset, unsigned Size) const { 686 MachineFunction *MF = MBB.getParent(); 687 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 688 const SISubtarget &ST = MF->getSubtarget<SISubtarget>(); 689 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 690 DebugLoc DL = MBB.findDebugLoc(MI); 691 unsigned WorkGroupSize = MFI->getMaxFlatWorkGroupSize(); 692 unsigned WavefrontSize = ST.getWavefrontSize(); 693 694 unsigned TIDReg = MFI->getTIDReg(); 695 if (!MFI->hasCalculatedTID()) { 696 MachineBasicBlock &Entry = MBB.getParent()->front(); 697 MachineBasicBlock::iterator Insert = Entry.front(); 698 DebugLoc DL = Insert->getDebugLoc(); 699 700 TIDReg = RI.findUnusedRegister(MF->getRegInfo(), &AMDGPU::VGPR_32RegClass, 701 *MF); 702 if (TIDReg == AMDGPU::NoRegister) 703 return TIDReg; 704 705 if (!AMDGPU::isShader(MF->getFunction()->getCallingConv()) && 706 WorkGroupSize > WavefrontSize) { 707 708 unsigned TIDIGXReg 709 = TRI->getPreloadedValue(*MF, SIRegisterInfo::WORKGROUP_ID_X); 710 unsigned TIDIGYReg 711 = TRI->getPreloadedValue(*MF, SIRegisterInfo::WORKGROUP_ID_Y); 712 unsigned TIDIGZReg 713 = TRI->getPreloadedValue(*MF, SIRegisterInfo::WORKGROUP_ID_Z); 714 unsigned InputPtrReg = 715 TRI->getPreloadedValue(*MF, SIRegisterInfo::KERNARG_SEGMENT_PTR); 716 for (unsigned Reg : {TIDIGXReg, TIDIGYReg, TIDIGZReg}) { 717 if (!Entry.isLiveIn(Reg)) 718 Entry.addLiveIn(Reg); 719 } 720 721 RS->enterBasicBlock(Entry); 722 // FIXME: Can we scavenge an SReg_64 and access the subregs? 723 unsigned STmp0 = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, 0); 724 unsigned STmp1 = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, 0); 725 BuildMI(Entry, Insert, DL, get(AMDGPU::S_LOAD_DWORD_IMM), STmp0) 726 .addReg(InputPtrReg) 727 .addImm(SI::KernelInputOffsets::NGROUPS_Z); 728 BuildMI(Entry, Insert, DL, get(AMDGPU::S_LOAD_DWORD_IMM), STmp1) 729 .addReg(InputPtrReg) 730 .addImm(SI::KernelInputOffsets::NGROUPS_Y); 731 732 // NGROUPS.X * NGROUPS.Y 733 BuildMI(Entry, Insert, DL, get(AMDGPU::S_MUL_I32), STmp1) 734 .addReg(STmp1) 735 .addReg(STmp0); 736 // (NGROUPS.X * NGROUPS.Y) * TIDIG.X 737 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MUL_U32_U24_e32), TIDReg) 738 .addReg(STmp1) 739 .addReg(TIDIGXReg); 740 // NGROUPS.Z * TIDIG.Y + (NGROUPS.X * NGROPUS.Y * TIDIG.X) 741 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MAD_U32_U24), TIDReg) 742 .addReg(STmp0) 743 .addReg(TIDIGYReg) 744 .addReg(TIDReg); 745 // (NGROUPS.Z * TIDIG.Y + (NGROUPS.X * NGROPUS.Y * TIDIG.X)) + TIDIG.Z 746 BuildMI(Entry, Insert, DL, get(AMDGPU::V_ADD_I32_e32), TIDReg) 747 .addReg(TIDReg) 748 .addReg(TIDIGZReg); 749 } else { 750 // Get the wave id 751 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MBCNT_LO_U32_B32_e64), 752 TIDReg) 753 .addImm(-1) 754 .addImm(0); 755 756 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MBCNT_HI_U32_B32_e64), 757 TIDReg) 758 .addImm(-1) 759 .addReg(TIDReg); 760 } 761 762 BuildMI(Entry, Insert, DL, get(AMDGPU::V_LSHLREV_B32_e32), 763 TIDReg) 764 .addImm(2) 765 .addReg(TIDReg); 766 MFI->setTIDReg(TIDReg); 767 } 768 769 // Add FrameIndex to LDS offset 770 unsigned LDSOffset = MFI->getLDSSize() + (FrameOffset * WorkGroupSize); 771 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_I32_e32), TmpReg) 772 .addImm(LDSOffset) 773 .addReg(TIDReg); 774 775 return TmpReg; 776 } 777 778 void SIInstrInfo::insertWaitStates(MachineBasicBlock &MBB, 779 MachineBasicBlock::iterator MI, 780 int Count) const { 781 DebugLoc DL = MBB.findDebugLoc(MI); 782 while (Count > 0) { 783 int Arg; 784 if (Count >= 8) 785 Arg = 7; 786 else 787 Arg = Count - 1; 788 Count -= 8; 789 BuildMI(MBB, MI, DL, get(AMDGPU::S_NOP)) 790 .addImm(Arg); 791 } 792 } 793 794 void SIInstrInfo::insertNoop(MachineBasicBlock &MBB, 795 MachineBasicBlock::iterator MI) const { 796 insertWaitStates(MBB, MI, 1); 797 } 798 799 unsigned SIInstrInfo::getNumWaitStates(const MachineInstr &MI) const { 800 switch (MI.getOpcode()) { 801 default: return 1; // FIXME: Do wait states equal cycles? 802 803 case AMDGPU::S_NOP: 804 return MI.getOperand(0).getImm() + 1; 805 } 806 } 807 808 bool SIInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 809 MachineBasicBlock &MBB = *MI.getParent(); 810 DebugLoc DL = MBB.findDebugLoc(MI); 811 switch (MI.getOpcode()) { 812 default: return AMDGPUInstrInfo::expandPostRAPseudo(MI); 813 case AMDGPU::S_MOV_B64_term: { 814 // This is only a terminator to get the correct spill code placement during 815 // register allocation. 816 MI.setDesc(get(AMDGPU::S_MOV_B64)); 817 break; 818 } 819 case AMDGPU::S_XOR_B64_term: { 820 // This is only a terminator to get the correct spill code placement during 821 // register allocation. 822 MI.setDesc(get(AMDGPU::S_XOR_B64)); 823 break; 824 } 825 case AMDGPU::S_ANDN2_B64_term: { 826 // This is only a terminator to get the correct spill code placement during 827 // register allocation. 828 MI.setDesc(get(AMDGPU::S_ANDN2_B64)); 829 break; 830 } 831 case AMDGPU::V_MOV_B64_PSEUDO: { 832 unsigned Dst = MI.getOperand(0).getReg(); 833 unsigned DstLo = RI.getSubReg(Dst, AMDGPU::sub0); 834 unsigned DstHi = RI.getSubReg(Dst, AMDGPU::sub1); 835 836 const MachineOperand &SrcOp = MI.getOperand(1); 837 // FIXME: Will this work for 64-bit floating point immediates? 838 assert(!SrcOp.isFPImm()); 839 if (SrcOp.isImm()) { 840 APInt Imm(64, SrcOp.getImm()); 841 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo) 842 .addImm(Imm.getLoBits(32).getZExtValue()) 843 .addReg(Dst, RegState::Implicit | RegState::Define); 844 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi) 845 .addImm(Imm.getHiBits(32).getZExtValue()) 846 .addReg(Dst, RegState::Implicit | RegState::Define); 847 } else { 848 assert(SrcOp.isReg()); 849 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo) 850 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub0)) 851 .addReg(Dst, RegState::Implicit | RegState::Define); 852 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi) 853 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub1)) 854 .addReg(Dst, RegState::Implicit | RegState::Define); 855 } 856 MI.eraseFromParent(); 857 break; 858 } 859 case AMDGPU::V_MOVRELD_B32_V1: 860 case AMDGPU::V_MOVRELD_B32_V2: 861 case AMDGPU::V_MOVRELD_B32_V4: 862 case AMDGPU::V_MOVRELD_B32_V8: 863 case AMDGPU::V_MOVRELD_B32_V16: { 864 const MCInstrDesc &MovRelDesc = get(AMDGPU::V_MOVRELD_B32_e32); 865 unsigned VecReg = MI.getOperand(0).getReg(); 866 bool IsUndef = MI.getOperand(1).isUndef(); 867 unsigned SubReg = AMDGPU::sub0 + MI.getOperand(3).getImm(); 868 assert(VecReg == MI.getOperand(1).getReg()); 869 870 MachineInstr *MovRel = 871 BuildMI(MBB, MI, DL, MovRelDesc) 872 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef) 873 .addOperand(MI.getOperand(2)) 874 .addReg(VecReg, RegState::ImplicitDefine) 875 .addReg(VecReg, RegState::Implicit | (IsUndef ? RegState::Undef : 0)); 876 877 const int ImpDefIdx = 878 MovRelDesc.getNumOperands() + MovRelDesc.getNumImplicitUses(); 879 const int ImpUseIdx = ImpDefIdx + 1; 880 MovRel->tieOperands(ImpDefIdx, ImpUseIdx); 881 882 MI.eraseFromParent(); 883 break; 884 } 885 case AMDGPU::SI_PC_ADD_REL_OFFSET: { 886 MachineFunction &MF = *MBB.getParent(); 887 unsigned Reg = MI.getOperand(0).getReg(); 888 unsigned RegLo = RI.getSubReg(Reg, AMDGPU::sub0); 889 unsigned RegHi = RI.getSubReg(Reg, AMDGPU::sub1); 890 891 // Create a bundle so these instructions won't be re-ordered by the 892 // post-RA scheduler. 893 MIBundleBuilder Bundler(MBB, MI); 894 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_GETPC_B64), Reg)); 895 896 // Add 32-bit offset from this instruction to the start of the 897 // constant data. 898 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_ADD_U32), RegLo) 899 .addReg(RegLo) 900 .addOperand(MI.getOperand(1))); 901 902 MachineInstrBuilder MIB = BuildMI(MF, DL, get(AMDGPU::S_ADDC_U32), RegHi) 903 .addReg(RegHi); 904 if (MI.getOperand(2).getTargetFlags() == SIInstrInfo::MO_NONE) 905 MIB.addImm(0); 906 else 907 MIB.addOperand(MI.getOperand(2)); 908 909 Bundler.append(MIB); 910 llvm::finalizeBundle(MBB, Bundler.begin()); 911 912 MI.eraseFromParent(); 913 break; 914 } 915 } 916 return true; 917 } 918 919 bool SIInstrInfo::swapSourceModifiers(MachineInstr &MI, 920 MachineOperand &Src0, 921 unsigned Src0OpName, 922 MachineOperand &Src1, 923 unsigned Src1OpName) const { 924 MachineOperand *Src0Mods = getNamedOperand(MI, Src0OpName); 925 if (!Src0Mods) 926 return false; 927 928 MachineOperand *Src1Mods = getNamedOperand(MI, Src1OpName); 929 assert(Src1Mods && 930 "All commutable instructions have both src0 and src1 modifiers"); 931 932 int Src0ModsVal = Src0Mods->getImm(); 933 int Src1ModsVal = Src1Mods->getImm(); 934 935 Src1Mods->setImm(Src0ModsVal); 936 Src0Mods->setImm(Src1ModsVal); 937 return true; 938 } 939 940 static MachineInstr *swapRegAndNonRegOperand(MachineInstr &MI, 941 MachineOperand &RegOp, 942 MachineOperand &NonRegOp) { 943 unsigned Reg = RegOp.getReg(); 944 unsigned SubReg = RegOp.getSubReg(); 945 bool IsKill = RegOp.isKill(); 946 bool IsDead = RegOp.isDead(); 947 bool IsUndef = RegOp.isUndef(); 948 bool IsDebug = RegOp.isDebug(); 949 950 if (NonRegOp.isImm()) 951 RegOp.ChangeToImmediate(NonRegOp.getImm()); 952 else if (NonRegOp.isFI()) 953 RegOp.ChangeToFrameIndex(NonRegOp.getIndex()); 954 else 955 return nullptr; 956 957 NonRegOp.ChangeToRegister(Reg, false, false, IsKill, IsDead, IsUndef, IsDebug); 958 NonRegOp.setSubReg(SubReg); 959 960 return &MI; 961 } 962 963 MachineInstr *SIInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, 964 unsigned Src0Idx, 965 unsigned Src1Idx) const { 966 assert(!NewMI && "this should never be used"); 967 968 unsigned Opc = MI.getOpcode(); 969 int CommutedOpcode = commuteOpcode(Opc); 970 if (CommutedOpcode == -1) 971 return nullptr; 972 973 assert(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) == 974 static_cast<int>(Src0Idx) && 975 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1) == 976 static_cast<int>(Src1Idx) && 977 "inconsistency with findCommutedOpIndices"); 978 979 MachineOperand &Src0 = MI.getOperand(Src0Idx); 980 MachineOperand &Src1 = MI.getOperand(Src1Idx); 981 982 MachineInstr *CommutedMI = nullptr; 983 if (Src0.isReg() && Src1.isReg()) { 984 if (isOperandLegal(MI, Src1Idx, &Src0)) { 985 // Be sure to copy the source modifiers to the right place. 986 CommutedMI 987 = TargetInstrInfo::commuteInstructionImpl(MI, NewMI, Src0Idx, Src1Idx); 988 } 989 990 } else if (Src0.isReg() && !Src1.isReg()) { 991 // src0 should always be able to support any operand type, so no need to 992 // check operand legality. 993 CommutedMI = swapRegAndNonRegOperand(MI, Src0, Src1); 994 } else if (!Src0.isReg() && Src1.isReg()) { 995 if (isOperandLegal(MI, Src1Idx, &Src0)) 996 CommutedMI = swapRegAndNonRegOperand(MI, Src1, Src0); 997 } else { 998 // FIXME: Found two non registers to commute. This does happen. 999 return nullptr; 1000 } 1001 1002 1003 if (CommutedMI) { 1004 swapSourceModifiers(MI, Src0, AMDGPU::OpName::src0_modifiers, 1005 Src1, AMDGPU::OpName::src1_modifiers); 1006 1007 CommutedMI->setDesc(get(CommutedOpcode)); 1008 } 1009 1010 return CommutedMI; 1011 } 1012 1013 // This needs to be implemented because the source modifiers may be inserted 1014 // between the true commutable operands, and the base 1015 // TargetInstrInfo::commuteInstruction uses it. 1016 bool SIInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx0, 1017 unsigned &SrcOpIdx1) const { 1018 if (!MI.isCommutable()) 1019 return false; 1020 1021 unsigned Opc = MI.getOpcode(); 1022 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 1023 if (Src0Idx == -1) 1024 return false; 1025 1026 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 1027 if (Src1Idx == -1) 1028 return false; 1029 1030 return fixCommutedOpIndices(SrcOpIdx0, SrcOpIdx1, Src0Idx, Src1Idx); 1031 } 1032 1033 bool SIInstrInfo::isBranchOffsetInRange(unsigned BranchOp, 1034 int64_t BrOffset) const { 1035 // BranchRelaxation should never have to check s_setpc_b64 because its dest 1036 // block is unanalyzable. 1037 assert(BranchOp != AMDGPU::S_SETPC_B64); 1038 1039 // Convert to dwords. 1040 BrOffset /= 4; 1041 1042 // The branch instructions do PC += signext(SIMM16 * 4) + 4, so the offset is 1043 // from the next instruction. 1044 BrOffset -= 1; 1045 1046 return isIntN(BranchOffsetBits, BrOffset); 1047 } 1048 1049 MachineBasicBlock *SIInstrInfo::getBranchDestBlock( 1050 const MachineInstr &MI) const { 1051 if (MI.getOpcode() == AMDGPU::S_SETPC_B64) { 1052 // This would be a difficult analysis to perform, but can always be legal so 1053 // there's no need to analyze it. 1054 return nullptr; 1055 } 1056 1057 return MI.getOperand(0).getMBB(); 1058 } 1059 1060 unsigned SIInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB, 1061 MachineBasicBlock &DestBB, 1062 const DebugLoc &DL, 1063 int64_t BrOffset, 1064 RegScavenger *RS) const { 1065 assert(RS && "RegScavenger required for long branching"); 1066 assert(MBB.empty() && 1067 "new block should be inserted for expanding unconditional branch"); 1068 assert(MBB.pred_size() == 1); 1069 1070 MachineFunction *MF = MBB.getParent(); 1071 MachineRegisterInfo &MRI = MF->getRegInfo(); 1072 1073 // FIXME: Virtual register workaround for RegScavenger not working with empty 1074 // blocks. 1075 unsigned PCReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1076 1077 auto I = MBB.end(); 1078 1079 // We need to compute the offset relative to the instruction immediately after 1080 // s_getpc_b64. Insert pc arithmetic code before last terminator. 1081 MachineInstr *GetPC = BuildMI(MBB, I, DL, get(AMDGPU::S_GETPC_B64), PCReg); 1082 1083 // TODO: Handle > 32-bit block address. 1084 if (BrOffset >= 0) { 1085 BuildMI(MBB, I, DL, get(AMDGPU::S_ADD_U32)) 1086 .addReg(PCReg, RegState::Define, AMDGPU::sub0) 1087 .addReg(PCReg, 0, AMDGPU::sub0) 1088 .addMBB(&DestBB, AMDGPU::TF_LONG_BRANCH_FORWARD); 1089 BuildMI(MBB, I, DL, get(AMDGPU::S_ADDC_U32)) 1090 .addReg(PCReg, RegState::Define, AMDGPU::sub1) 1091 .addReg(PCReg, 0, AMDGPU::sub1) 1092 .addImm(0); 1093 } else { 1094 // Backwards branch. 1095 BuildMI(MBB, I, DL, get(AMDGPU::S_SUB_U32)) 1096 .addReg(PCReg, RegState::Define, AMDGPU::sub0) 1097 .addReg(PCReg, 0, AMDGPU::sub0) 1098 .addMBB(&DestBB, AMDGPU::TF_LONG_BRANCH_BACKWARD); 1099 BuildMI(MBB, I, DL, get(AMDGPU::S_SUBB_U32)) 1100 .addReg(PCReg, RegState::Define, AMDGPU::sub1) 1101 .addReg(PCReg, 0, AMDGPU::sub1) 1102 .addImm(0); 1103 } 1104 1105 // Insert the indirect branch after the other terminator. 1106 BuildMI(&MBB, DL, get(AMDGPU::S_SETPC_B64)) 1107 .addReg(PCReg); 1108 1109 // FIXME: If spilling is necessary, this will fail because this scavenger has 1110 // no emergency stack slots. It is non-trivial to spill in this situation, 1111 // because the restore code needs to be specially placed after the 1112 // jump. BranchRelaxation then needs to be made aware of the newly inserted 1113 // block. 1114 // 1115 // If a spill is needed for the pc register pair, we need to insert a spill 1116 // restore block right before the destination block, and insert a short branch 1117 // into the old destination block's fallthrough predecessor. 1118 // e.g.: 1119 // 1120 // s_cbranch_scc0 skip_long_branch: 1121 // 1122 // long_branch_bb: 1123 // spill s[8:9] 1124 // s_getpc_b64 s[8:9] 1125 // s_add_u32 s8, s8, restore_bb 1126 // s_addc_u32 s9, s9, 0 1127 // s_setpc_b64 s[8:9] 1128 // 1129 // skip_long_branch: 1130 // foo; 1131 // 1132 // ..... 1133 // 1134 // dest_bb_fallthrough_predecessor: 1135 // bar; 1136 // s_branch dest_bb 1137 // 1138 // restore_bb: 1139 // restore s[8:9] 1140 // fallthrough dest_bb 1141 /// 1142 // dest_bb: 1143 // buzz; 1144 1145 RS->enterBasicBlockEnd(MBB); 1146 unsigned Scav = RS->scavengeRegister(&AMDGPU::SReg_64RegClass, 1147 MachineBasicBlock::iterator(GetPC), 0); 1148 MRI.replaceRegWith(PCReg, Scav); 1149 MRI.clearVirtRegs(); 1150 RS->setRegUsed(Scav); 1151 1152 return 4 + 8 + 4 + 4; 1153 } 1154 1155 unsigned SIInstrInfo::getBranchOpcode(SIInstrInfo::BranchPredicate Cond) { 1156 switch (Cond) { 1157 case SIInstrInfo::SCC_TRUE: 1158 return AMDGPU::S_CBRANCH_SCC1; 1159 case SIInstrInfo::SCC_FALSE: 1160 return AMDGPU::S_CBRANCH_SCC0; 1161 case SIInstrInfo::VCCNZ: 1162 return AMDGPU::S_CBRANCH_VCCNZ; 1163 case SIInstrInfo::VCCZ: 1164 return AMDGPU::S_CBRANCH_VCCZ; 1165 case SIInstrInfo::EXECNZ: 1166 return AMDGPU::S_CBRANCH_EXECNZ; 1167 case SIInstrInfo::EXECZ: 1168 return AMDGPU::S_CBRANCH_EXECZ; 1169 default: 1170 llvm_unreachable("invalid branch predicate"); 1171 } 1172 } 1173 1174 SIInstrInfo::BranchPredicate SIInstrInfo::getBranchPredicate(unsigned Opcode) { 1175 switch (Opcode) { 1176 case AMDGPU::S_CBRANCH_SCC0: 1177 return SCC_FALSE; 1178 case AMDGPU::S_CBRANCH_SCC1: 1179 return SCC_TRUE; 1180 case AMDGPU::S_CBRANCH_VCCNZ: 1181 return VCCNZ; 1182 case AMDGPU::S_CBRANCH_VCCZ: 1183 return VCCZ; 1184 case AMDGPU::S_CBRANCH_EXECNZ: 1185 return EXECNZ; 1186 case AMDGPU::S_CBRANCH_EXECZ: 1187 return EXECZ; 1188 default: 1189 return INVALID_BR; 1190 } 1191 } 1192 1193 bool SIInstrInfo::analyzeBranchImpl(MachineBasicBlock &MBB, 1194 MachineBasicBlock::iterator I, 1195 MachineBasicBlock *&TBB, 1196 MachineBasicBlock *&FBB, 1197 SmallVectorImpl<MachineOperand> &Cond, 1198 bool AllowModify) const { 1199 if (I->getOpcode() == AMDGPU::S_BRANCH) { 1200 // Unconditional Branch 1201 TBB = I->getOperand(0).getMBB(); 1202 return false; 1203 } 1204 1205 BranchPredicate Pred = getBranchPredicate(I->getOpcode()); 1206 if (Pred == INVALID_BR) 1207 return true; 1208 1209 MachineBasicBlock *CondBB = I->getOperand(0).getMBB(); 1210 Cond.push_back(MachineOperand::CreateImm(Pred)); 1211 Cond.push_back(I->getOperand(1)); // Save the branch register. 1212 1213 ++I; 1214 1215 if (I == MBB.end()) { 1216 // Conditional branch followed by fall-through. 1217 TBB = CondBB; 1218 return false; 1219 } 1220 1221 if (I->getOpcode() == AMDGPU::S_BRANCH) { 1222 TBB = CondBB; 1223 FBB = I->getOperand(0).getMBB(); 1224 return false; 1225 } 1226 1227 return true; 1228 } 1229 1230 bool SIInstrInfo::analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, 1231 MachineBasicBlock *&FBB, 1232 SmallVectorImpl<MachineOperand> &Cond, 1233 bool AllowModify) const { 1234 MachineBasicBlock::iterator I = MBB.getFirstTerminator(); 1235 if (I == MBB.end()) 1236 return false; 1237 1238 if (I->getOpcode() != AMDGPU::SI_MASK_BRANCH) 1239 return analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify); 1240 1241 ++I; 1242 1243 // TODO: Should be able to treat as fallthrough? 1244 if (I == MBB.end()) 1245 return true; 1246 1247 if (analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify)) 1248 return true; 1249 1250 MachineBasicBlock *MaskBrDest = I->getOperand(0).getMBB(); 1251 1252 // Specifically handle the case where the conditional branch is to the same 1253 // destination as the mask branch. e.g. 1254 // 1255 // si_mask_branch BB8 1256 // s_cbranch_execz BB8 1257 // s_cbranch BB9 1258 // 1259 // This is required to understand divergent loops which may need the branches 1260 // to be relaxed. 1261 if (TBB != MaskBrDest || Cond.empty()) 1262 return true; 1263 1264 auto Pred = Cond[0].getImm(); 1265 return (Pred != EXECZ && Pred != EXECNZ); 1266 } 1267 1268 unsigned SIInstrInfo::removeBranch(MachineBasicBlock &MBB, 1269 int *BytesRemoved) const { 1270 MachineBasicBlock::iterator I = MBB.getFirstTerminator(); 1271 1272 unsigned Count = 0; 1273 unsigned RemovedSize = 0; 1274 while (I != MBB.end()) { 1275 MachineBasicBlock::iterator Next = std::next(I); 1276 if (I->getOpcode() == AMDGPU::SI_MASK_BRANCH) { 1277 I = Next; 1278 continue; 1279 } 1280 1281 RemovedSize += getInstSizeInBytes(*I); 1282 I->eraseFromParent(); 1283 ++Count; 1284 I = Next; 1285 } 1286 1287 if (BytesRemoved) 1288 *BytesRemoved = RemovedSize; 1289 1290 return Count; 1291 } 1292 1293 unsigned SIInstrInfo::insertBranch(MachineBasicBlock &MBB, 1294 MachineBasicBlock *TBB, 1295 MachineBasicBlock *FBB, 1296 ArrayRef<MachineOperand> Cond, 1297 const DebugLoc &DL, 1298 int *BytesAdded) const { 1299 1300 if (!FBB && Cond.empty()) { 1301 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH)) 1302 .addMBB(TBB); 1303 if (BytesAdded) 1304 *BytesAdded = 4; 1305 return 1; 1306 } 1307 1308 assert(TBB && Cond[0].isImm()); 1309 1310 unsigned Opcode 1311 = getBranchOpcode(static_cast<BranchPredicate>(Cond[0].getImm())); 1312 1313 if (!FBB) { 1314 Cond[1].isUndef(); 1315 MachineInstr *CondBr = 1316 BuildMI(&MBB, DL, get(Opcode)) 1317 .addMBB(TBB); 1318 1319 // Copy the flags onto the implicit condition register operand. 1320 MachineOperand &CondReg = CondBr->getOperand(1); 1321 CondReg.setIsUndef(Cond[1].isUndef()); 1322 CondReg.setIsKill(Cond[1].isKill()); 1323 1324 if (BytesAdded) 1325 *BytesAdded = 4; 1326 return 1; 1327 } 1328 1329 assert(TBB && FBB); 1330 1331 MachineInstr *CondBr = 1332 BuildMI(&MBB, DL, get(Opcode)) 1333 .addMBB(TBB); 1334 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH)) 1335 .addMBB(FBB); 1336 1337 MachineOperand &CondReg = CondBr->getOperand(1); 1338 CondReg.setIsUndef(Cond[1].isUndef()); 1339 CondReg.setIsKill(Cond[1].isKill()); 1340 1341 if (BytesAdded) 1342 *BytesAdded = 8; 1343 1344 return 2; 1345 } 1346 1347 bool SIInstrInfo::reverseBranchCondition( 1348 SmallVectorImpl<MachineOperand> &Cond) const { 1349 assert(Cond.size() == 2); 1350 Cond[0].setImm(-Cond[0].getImm()); 1351 return false; 1352 } 1353 1354 static void removeModOperands(MachineInstr &MI) { 1355 unsigned Opc = MI.getOpcode(); 1356 int Src0ModIdx = AMDGPU::getNamedOperandIdx(Opc, 1357 AMDGPU::OpName::src0_modifiers); 1358 int Src1ModIdx = AMDGPU::getNamedOperandIdx(Opc, 1359 AMDGPU::OpName::src1_modifiers); 1360 int Src2ModIdx = AMDGPU::getNamedOperandIdx(Opc, 1361 AMDGPU::OpName::src2_modifiers); 1362 1363 MI.RemoveOperand(Src2ModIdx); 1364 MI.RemoveOperand(Src1ModIdx); 1365 MI.RemoveOperand(Src0ModIdx); 1366 } 1367 1368 bool SIInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 1369 unsigned Reg, MachineRegisterInfo *MRI) const { 1370 if (!MRI->hasOneNonDBGUse(Reg)) 1371 return false; 1372 1373 unsigned Opc = UseMI.getOpcode(); 1374 if (Opc == AMDGPU::COPY) { 1375 bool isVGPRCopy = RI.isVGPR(*MRI, UseMI.getOperand(0).getReg()); 1376 switch (DefMI.getOpcode()) { 1377 default: 1378 return false; 1379 case AMDGPU::S_MOV_B64: 1380 // TODO: We could fold 64-bit immediates, but this get compilicated 1381 // when there are sub-registers. 1382 return false; 1383 1384 case AMDGPU::V_MOV_B32_e32: 1385 case AMDGPU::S_MOV_B32: 1386 break; 1387 } 1388 unsigned NewOpc = isVGPRCopy ? AMDGPU::V_MOV_B32_e32 : AMDGPU::S_MOV_B32; 1389 const MachineOperand *ImmOp = getNamedOperand(DefMI, AMDGPU::OpName::src0); 1390 assert(ImmOp); 1391 // FIXME: We could handle FrameIndex values here. 1392 if (!ImmOp->isImm()) { 1393 return false; 1394 } 1395 UseMI.setDesc(get(NewOpc)); 1396 UseMI.getOperand(1).ChangeToImmediate(ImmOp->getImm()); 1397 UseMI.addImplicitDefUseOperands(*UseMI.getParent()->getParent()); 1398 return true; 1399 } 1400 1401 if (Opc == AMDGPU::V_MAD_F32 || Opc == AMDGPU::V_MAC_F32_e64 || 1402 Opc == AMDGPU::V_MAD_F16 || Opc == AMDGPU::V_MAC_F16_e64) { 1403 bool IsF32 = Opc == AMDGPU::V_MAD_F32 || Opc == AMDGPU::V_MAC_F32_e64; 1404 1405 // Don't fold if we are using source modifiers. The new VOP2 instructions 1406 // don't have them. 1407 if (hasModifiersSet(UseMI, AMDGPU::OpName::src0_modifiers) || 1408 hasModifiersSet(UseMI, AMDGPU::OpName::src1_modifiers) || 1409 hasModifiersSet(UseMI, AMDGPU::OpName::src2_modifiers)) { 1410 return false; 1411 } 1412 1413 const MachineOperand &ImmOp = DefMI.getOperand(1); 1414 1415 // If this is a free constant, there's no reason to do this. 1416 // TODO: We could fold this here instead of letting SIFoldOperands do it 1417 // later. 1418 if (isInlineConstant(ImmOp, 4)) 1419 return false; 1420 1421 MachineOperand *Src0 = getNamedOperand(UseMI, AMDGPU::OpName::src0); 1422 MachineOperand *Src1 = getNamedOperand(UseMI, AMDGPU::OpName::src1); 1423 MachineOperand *Src2 = getNamedOperand(UseMI, AMDGPU::OpName::src2); 1424 1425 // Multiplied part is the constant: Use v_madmk_{f16, f32}. 1426 // We should only expect these to be on src0 due to canonicalizations. 1427 if (Src0->isReg() && Src0->getReg() == Reg) { 1428 if (!Src1->isReg() || RI.isSGPRClass(MRI->getRegClass(Src1->getReg()))) 1429 return false; 1430 1431 if (!Src2->isReg() || RI.isSGPRClass(MRI->getRegClass(Src2->getReg()))) 1432 return false; 1433 1434 // We need to swap operands 0 and 1 since madmk constant is at operand 1. 1435 1436 const int64_t Imm = DefMI.getOperand(1).getImm(); 1437 1438 // FIXME: This would be a lot easier if we could return a new instruction 1439 // instead of having to modify in place. 1440 1441 // Remove these first since they are at the end. 1442 UseMI.RemoveOperand( 1443 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::omod)); 1444 UseMI.RemoveOperand( 1445 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp)); 1446 1447 unsigned Src1Reg = Src1->getReg(); 1448 unsigned Src1SubReg = Src1->getSubReg(); 1449 Src0->setReg(Src1Reg); 1450 Src0->setSubReg(Src1SubReg); 1451 Src0->setIsKill(Src1->isKill()); 1452 1453 if (Opc == AMDGPU::V_MAC_F32_e64 || 1454 Opc == AMDGPU::V_MAC_F16_e64) 1455 UseMI.untieRegOperand( 1456 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)); 1457 1458 Src1->ChangeToImmediate(Imm); 1459 1460 removeModOperands(UseMI); 1461 UseMI.setDesc(get(IsF32 ? AMDGPU::V_MADMK_F32 : AMDGPU::V_MADMK_F16)); 1462 1463 bool DeleteDef = MRI->hasOneNonDBGUse(Reg); 1464 if (DeleteDef) 1465 DefMI.eraseFromParent(); 1466 1467 return true; 1468 } 1469 1470 // Added part is the constant: Use v_madak_{f16, f32}. 1471 if (Src2->isReg() && Src2->getReg() == Reg) { 1472 // Not allowed to use constant bus for another operand. 1473 // We can however allow an inline immediate as src0. 1474 if (!Src0->isImm() && 1475 (Src0->isReg() && RI.isSGPRClass(MRI->getRegClass(Src0->getReg())))) 1476 return false; 1477 1478 if (!Src1->isReg() || RI.isSGPRClass(MRI->getRegClass(Src1->getReg()))) 1479 return false; 1480 1481 const int64_t Imm = DefMI.getOperand(1).getImm(); 1482 1483 // FIXME: This would be a lot easier if we could return a new instruction 1484 // instead of having to modify in place. 1485 1486 // Remove these first since they are at the end. 1487 UseMI.RemoveOperand( 1488 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::omod)); 1489 UseMI.RemoveOperand( 1490 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp)); 1491 1492 if (Opc == AMDGPU::V_MAC_F32_e64 || 1493 Opc == AMDGPU::V_MAC_F16_e64) 1494 UseMI.untieRegOperand( 1495 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)); 1496 1497 // ChangingToImmediate adds Src2 back to the instruction. 1498 Src2->ChangeToImmediate(Imm); 1499 1500 // These come before src2. 1501 removeModOperands(UseMI); 1502 UseMI.setDesc(get(IsF32 ? AMDGPU::V_MADAK_F32 : AMDGPU::V_MADAK_F16)); 1503 1504 bool DeleteDef = MRI->hasOneNonDBGUse(Reg); 1505 if (DeleteDef) 1506 DefMI.eraseFromParent(); 1507 1508 return true; 1509 } 1510 } 1511 1512 return false; 1513 } 1514 1515 static bool offsetsDoNotOverlap(int WidthA, int OffsetA, 1516 int WidthB, int OffsetB) { 1517 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB; 1518 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA; 1519 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; 1520 return LowOffset + LowWidth <= HighOffset; 1521 } 1522 1523 bool SIInstrInfo::checkInstOffsetsDoNotOverlap(MachineInstr &MIa, 1524 MachineInstr &MIb) const { 1525 unsigned BaseReg0, BaseReg1; 1526 int64_t Offset0, Offset1; 1527 1528 if (getMemOpBaseRegImmOfs(MIa, BaseReg0, Offset0, &RI) && 1529 getMemOpBaseRegImmOfs(MIb, BaseReg1, Offset1, &RI)) { 1530 1531 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand()) { 1532 // FIXME: Handle ds_read2 / ds_write2. 1533 return false; 1534 } 1535 unsigned Width0 = (*MIa.memoperands_begin())->getSize(); 1536 unsigned Width1 = (*MIb.memoperands_begin())->getSize(); 1537 if (BaseReg0 == BaseReg1 && 1538 offsetsDoNotOverlap(Width0, Offset0, Width1, Offset1)) { 1539 return true; 1540 } 1541 } 1542 1543 return false; 1544 } 1545 1546 bool SIInstrInfo::areMemAccessesTriviallyDisjoint(MachineInstr &MIa, 1547 MachineInstr &MIb, 1548 AliasAnalysis *AA) const { 1549 assert((MIa.mayLoad() || MIa.mayStore()) && 1550 "MIa must load from or modify a memory location"); 1551 assert((MIb.mayLoad() || MIb.mayStore()) && 1552 "MIb must load from or modify a memory location"); 1553 1554 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects()) 1555 return false; 1556 1557 // XXX - Can we relax this between address spaces? 1558 if (MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef()) 1559 return false; 1560 1561 if (AA && MIa.hasOneMemOperand() && MIb.hasOneMemOperand()) { 1562 const MachineMemOperand *MMOa = *MIa.memoperands_begin(); 1563 const MachineMemOperand *MMOb = *MIb.memoperands_begin(); 1564 if (MMOa->getValue() && MMOb->getValue()) { 1565 MemoryLocation LocA(MMOa->getValue(), MMOa->getSize(), MMOa->getAAInfo()); 1566 MemoryLocation LocB(MMOb->getValue(), MMOb->getSize(), MMOb->getAAInfo()); 1567 if (!AA->alias(LocA, LocB)) 1568 return true; 1569 } 1570 } 1571 1572 // TODO: Should we check the address space from the MachineMemOperand? That 1573 // would allow us to distinguish objects we know don't alias based on the 1574 // underlying address space, even if it was lowered to a different one, 1575 // e.g. private accesses lowered to use MUBUF instructions on a scratch 1576 // buffer. 1577 if (isDS(MIa)) { 1578 if (isDS(MIb)) 1579 return checkInstOffsetsDoNotOverlap(MIa, MIb); 1580 1581 return !isFLAT(MIb); 1582 } 1583 1584 if (isMUBUF(MIa) || isMTBUF(MIa)) { 1585 if (isMUBUF(MIb) || isMTBUF(MIb)) 1586 return checkInstOffsetsDoNotOverlap(MIa, MIb); 1587 1588 return !isFLAT(MIb) && !isSMRD(MIb); 1589 } 1590 1591 if (isSMRD(MIa)) { 1592 if (isSMRD(MIb)) 1593 return checkInstOffsetsDoNotOverlap(MIa, MIb); 1594 1595 return !isFLAT(MIb) && !isMUBUF(MIa) && !isMTBUF(MIa); 1596 } 1597 1598 if (isFLAT(MIa)) { 1599 if (isFLAT(MIb)) 1600 return checkInstOffsetsDoNotOverlap(MIa, MIb); 1601 1602 return false; 1603 } 1604 1605 return false; 1606 } 1607 1608 MachineInstr *SIInstrInfo::convertToThreeAddress(MachineFunction::iterator &MBB, 1609 MachineInstr &MI, 1610 LiveVariables *LV) const { 1611 bool IsF16 = false; 1612 1613 switch (MI.getOpcode()) { 1614 default: 1615 return nullptr; 1616 case AMDGPU::V_MAC_F16_e64: 1617 IsF16 = true; 1618 case AMDGPU::V_MAC_F32_e64: 1619 break; 1620 case AMDGPU::V_MAC_F16_e32: 1621 IsF16 = true; 1622 case AMDGPU::V_MAC_F32_e32: { 1623 const MachineOperand *Src0 = getNamedOperand(MI, AMDGPU::OpName::src0); 1624 if (Src0->isImm() && !isInlineConstant(*Src0, 4)) 1625 return nullptr; 1626 break; 1627 } 1628 } 1629 1630 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 1631 const MachineOperand *Src0 = getNamedOperand(MI, AMDGPU::OpName::src0); 1632 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1); 1633 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2); 1634 1635 return BuildMI(*MBB, MI, MI.getDebugLoc(), 1636 get(IsF16 ? AMDGPU::V_MAD_F16 : AMDGPU::V_MAD_F32)) 1637 .addOperand(*Dst) 1638 .addImm(0) // Src0 mods 1639 .addOperand(*Src0) 1640 .addImm(0) // Src1 mods 1641 .addOperand(*Src1) 1642 .addImm(0) // Src mods 1643 .addOperand(*Src2) 1644 .addImm(0) // clamp 1645 .addImm(0); // omod 1646 } 1647 1648 // It's not generally safe to move VALU instructions across these since it will 1649 // start using the register as a base index rather than directly. 1650 // XXX - Why isn't hasSideEffects sufficient for these? 1651 static bool changesVGPRIndexingMode(const MachineInstr &MI) { 1652 switch (MI.getOpcode()) { 1653 case AMDGPU::S_SET_GPR_IDX_ON: 1654 case AMDGPU::S_SET_GPR_IDX_MODE: 1655 case AMDGPU::S_SET_GPR_IDX_OFF: 1656 return true; 1657 default: 1658 return false; 1659 } 1660 } 1661 1662 bool SIInstrInfo::isSchedulingBoundary(const MachineInstr &MI, 1663 const MachineBasicBlock *MBB, 1664 const MachineFunction &MF) const { 1665 // XXX - Do we want the SP check in the base implementation? 1666 1667 // Target-independent instructions do not have an implicit-use of EXEC, even 1668 // when they operate on VGPRs. Treating EXEC modifications as scheduling 1669 // boundaries prevents incorrect movements of such instructions. 1670 return TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF) || 1671 MI.modifiesRegister(AMDGPU::EXEC, &RI) || 1672 MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 || 1673 MI.getOpcode() == AMDGPU::S_SETREG_B32 || 1674 changesVGPRIndexingMode(MI); 1675 } 1676 1677 bool SIInstrInfo::isInlineConstant(const APInt &Imm) const { 1678 switch (Imm.getBitWidth()) { 1679 case 32: 1680 return AMDGPU::isInlinableLiteral32(Imm.getSExtValue(), 1681 ST.hasInv2PiInlineImm()); 1682 case 64: 1683 return AMDGPU::isInlinableLiteral64(Imm.getSExtValue(), 1684 ST.hasInv2PiInlineImm()); 1685 default: 1686 llvm_unreachable("invalid bitwidth"); 1687 } 1688 } 1689 1690 bool SIInstrInfo::isInlineConstant(const MachineOperand &MO, 1691 unsigned OpSize) const { 1692 if (MO.isImm()) { 1693 // MachineOperand provides no way to tell the true operand size, since it 1694 // only records a 64-bit value. We need to know the size to determine if a 1695 // 32-bit floating point immediate bit pattern is legal for an integer 1696 // immediate. It would be for any 32-bit integer operand, but would not be 1697 // for a 64-bit one. 1698 switch (OpSize) { 1699 case 4: 1700 return AMDGPU::isInlinableLiteral32(static_cast<int32_t>(MO.getImm()), 1701 ST.hasInv2PiInlineImm()); 1702 case 8: 1703 return AMDGPU::isInlinableLiteral64(MO.getImm(), 1704 ST.hasInv2PiInlineImm()); 1705 default: 1706 llvm_unreachable("invalid bitwidth"); 1707 } 1708 } 1709 1710 return false; 1711 } 1712 1713 bool SIInstrInfo::isLiteralConstant(const MachineOperand &MO, 1714 unsigned OpSize) const { 1715 return MO.isImm() && !isInlineConstant(MO, OpSize); 1716 } 1717 1718 bool SIInstrInfo::isLiteralConstantLike(const MachineOperand &MO, 1719 unsigned OpSize) const { 1720 switch (MO.getType()) { 1721 case MachineOperand::MO_Register: 1722 return false; 1723 case MachineOperand::MO_Immediate: 1724 return !isInlineConstant(MO, OpSize); 1725 case MachineOperand::MO_FrameIndex: 1726 case MachineOperand::MO_MachineBasicBlock: 1727 case MachineOperand::MO_ExternalSymbol: 1728 case MachineOperand::MO_GlobalAddress: 1729 case MachineOperand::MO_MCSymbol: 1730 return true; 1731 default: 1732 llvm_unreachable("unexpected operand type"); 1733 } 1734 } 1735 1736 static bool compareMachineOp(const MachineOperand &Op0, 1737 const MachineOperand &Op1) { 1738 if (Op0.getType() != Op1.getType()) 1739 return false; 1740 1741 switch (Op0.getType()) { 1742 case MachineOperand::MO_Register: 1743 return Op0.getReg() == Op1.getReg(); 1744 case MachineOperand::MO_Immediate: 1745 return Op0.getImm() == Op1.getImm(); 1746 default: 1747 llvm_unreachable("Didn't expect to be comparing these operand types"); 1748 } 1749 } 1750 1751 bool SIInstrInfo::isImmOperandLegal(const MachineInstr &MI, unsigned OpNo, 1752 const MachineOperand &MO) const { 1753 const MCOperandInfo &OpInfo = get(MI.getOpcode()).OpInfo[OpNo]; 1754 1755 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI()); 1756 1757 if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE) 1758 return true; 1759 1760 if (OpInfo.RegClass < 0) 1761 return false; 1762 1763 unsigned OpSize = RI.getRegClass(OpInfo.RegClass)->getSize(); 1764 if (isLiteralConstant(MO, OpSize)) 1765 return RI.opCanUseLiteralConstant(OpInfo.OperandType); 1766 1767 return RI.opCanUseInlineConstant(OpInfo.OperandType); 1768 } 1769 1770 bool SIInstrInfo::hasVALU32BitEncoding(unsigned Opcode) const { 1771 int Op32 = AMDGPU::getVOPe32(Opcode); 1772 if (Op32 == -1) 1773 return false; 1774 1775 return pseudoToMCOpcode(Op32) != -1; 1776 } 1777 1778 bool SIInstrInfo::hasModifiers(unsigned Opcode) const { 1779 // The src0_modifier operand is present on all instructions 1780 // that have modifiers. 1781 1782 return AMDGPU::getNamedOperandIdx(Opcode, 1783 AMDGPU::OpName::src0_modifiers) != -1; 1784 } 1785 1786 bool SIInstrInfo::hasModifiersSet(const MachineInstr &MI, 1787 unsigned OpName) const { 1788 const MachineOperand *Mods = getNamedOperand(MI, OpName); 1789 return Mods && Mods->getImm(); 1790 } 1791 1792 bool SIInstrInfo::usesConstantBus(const MachineRegisterInfo &MRI, 1793 const MachineOperand &MO, 1794 unsigned OpSize) const { 1795 // Literal constants use the constant bus. 1796 if (isLiteralConstant(MO, OpSize)) 1797 return true; 1798 1799 if (!MO.isReg() || !MO.isUse()) 1800 return false; 1801 1802 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) 1803 return RI.isSGPRClass(MRI.getRegClass(MO.getReg())); 1804 1805 // FLAT_SCR is just an SGPR pair. 1806 if (!MO.isImplicit() && (MO.getReg() == AMDGPU::FLAT_SCR)) 1807 return true; 1808 1809 // EXEC register uses the constant bus. 1810 if (!MO.isImplicit() && MO.getReg() == AMDGPU::EXEC) 1811 return true; 1812 1813 // SGPRs use the constant bus 1814 return (MO.getReg() == AMDGPU::VCC || MO.getReg() == AMDGPU::M0 || 1815 (!MO.isImplicit() && 1816 (AMDGPU::SGPR_32RegClass.contains(MO.getReg()) || 1817 AMDGPU::SGPR_64RegClass.contains(MO.getReg())))); 1818 } 1819 1820 static unsigned findImplicitSGPRRead(const MachineInstr &MI) { 1821 for (const MachineOperand &MO : MI.implicit_operands()) { 1822 // We only care about reads. 1823 if (MO.isDef()) 1824 continue; 1825 1826 switch (MO.getReg()) { 1827 case AMDGPU::VCC: 1828 case AMDGPU::M0: 1829 case AMDGPU::FLAT_SCR: 1830 return MO.getReg(); 1831 1832 default: 1833 break; 1834 } 1835 } 1836 1837 return AMDGPU::NoRegister; 1838 } 1839 1840 static bool shouldReadExec(const MachineInstr &MI) { 1841 if (SIInstrInfo::isVALU(MI)) { 1842 switch (MI.getOpcode()) { 1843 case AMDGPU::V_READLANE_B32: 1844 case AMDGPU::V_READLANE_B32_si: 1845 case AMDGPU::V_READLANE_B32_vi: 1846 case AMDGPU::V_WRITELANE_B32: 1847 case AMDGPU::V_WRITELANE_B32_si: 1848 case AMDGPU::V_WRITELANE_B32_vi: 1849 return false; 1850 } 1851 1852 return true; 1853 } 1854 1855 if (SIInstrInfo::isGenericOpcode(MI.getOpcode()) || 1856 SIInstrInfo::isSALU(MI) || 1857 SIInstrInfo::isSMRD(MI)) 1858 return false; 1859 1860 return true; 1861 } 1862 1863 static bool isSubRegOf(const SIRegisterInfo &TRI, 1864 const MachineOperand &SuperVec, 1865 const MachineOperand &SubReg) { 1866 if (TargetRegisterInfo::isPhysicalRegister(SubReg.getReg())) 1867 return TRI.isSubRegister(SuperVec.getReg(), SubReg.getReg()); 1868 1869 return SubReg.getSubReg() != AMDGPU::NoSubRegister && 1870 SubReg.getReg() == SuperVec.getReg(); 1871 } 1872 1873 bool SIInstrInfo::verifyInstruction(const MachineInstr &MI, 1874 StringRef &ErrInfo) const { 1875 uint16_t Opcode = MI.getOpcode(); 1876 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 1877 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0); 1878 int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1); 1879 int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2); 1880 1881 // Make sure the number of operands is correct. 1882 const MCInstrDesc &Desc = get(Opcode); 1883 if (!Desc.isVariadic() && 1884 Desc.getNumOperands() != MI.getNumExplicitOperands()) { 1885 ErrInfo = "Instruction has wrong number of operands."; 1886 return false; 1887 } 1888 1889 if (MI.isInlineAsm()) { 1890 // Verify register classes for inlineasm constraints. 1891 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = MI.getNumOperands(); 1892 I != E; ++I) { 1893 const TargetRegisterClass *RC = MI.getRegClassConstraint(I, this, &RI); 1894 if (!RC) 1895 continue; 1896 1897 const MachineOperand &Op = MI.getOperand(I); 1898 if (!Op.isReg()) 1899 continue; 1900 1901 unsigned Reg = Op.getReg(); 1902 if (!TargetRegisterInfo::isVirtualRegister(Reg) && !RC->contains(Reg)) { 1903 ErrInfo = "inlineasm operand has incorrect register class."; 1904 return false; 1905 } 1906 } 1907 1908 return true; 1909 } 1910 1911 // Make sure the register classes are correct. 1912 for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) { 1913 if (MI.getOperand(i).isFPImm()) { 1914 ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast " 1915 "all fp values to integers."; 1916 return false; 1917 } 1918 1919 int RegClass = Desc.OpInfo[i].RegClass; 1920 1921 switch (Desc.OpInfo[i].OperandType) { 1922 case MCOI::OPERAND_REGISTER: 1923 if (MI.getOperand(i).isImm()) { 1924 ErrInfo = "Illegal immediate value for operand."; 1925 return false; 1926 } 1927 break; 1928 case AMDGPU::OPERAND_REG_IMM32_INT: 1929 case AMDGPU::OPERAND_REG_IMM32_FP: 1930 break; 1931 case AMDGPU::OPERAND_REG_INLINE_C_INT: 1932 case AMDGPU::OPERAND_REG_INLINE_C_FP: 1933 if (isLiteralConstant(MI.getOperand(i), 1934 RI.getRegClass(RegClass)->getSize())) { 1935 ErrInfo = "Illegal immediate value for operand."; 1936 return false; 1937 } 1938 break; 1939 case MCOI::OPERAND_IMMEDIATE: 1940 case AMDGPU::OPERAND_KIMM32: 1941 // Check if this operand is an immediate. 1942 // FrameIndex operands will be replaced by immediates, so they are 1943 // allowed. 1944 if (!MI.getOperand(i).isImm() && !MI.getOperand(i).isFI()) { 1945 ErrInfo = "Expected immediate, but got non-immediate"; 1946 return false; 1947 } 1948 LLVM_FALLTHROUGH; 1949 default: 1950 continue; 1951 } 1952 1953 if (!MI.getOperand(i).isReg()) 1954 continue; 1955 1956 if (RegClass != -1) { 1957 unsigned Reg = MI.getOperand(i).getReg(); 1958 if (Reg == AMDGPU::NoRegister || 1959 TargetRegisterInfo::isVirtualRegister(Reg)) 1960 continue; 1961 1962 const TargetRegisterClass *RC = RI.getRegClass(RegClass); 1963 if (!RC->contains(Reg)) { 1964 ErrInfo = "Operand has incorrect register class."; 1965 return false; 1966 } 1967 } 1968 } 1969 1970 // Verify VOP* 1971 if (isVOP1(MI) || isVOP2(MI) || isVOP3(MI) || isVOPC(MI)) { 1972 // Only look at the true operands. Only a real operand can use the constant 1973 // bus, and we don't want to check pseudo-operands like the source modifier 1974 // flags. 1975 const int OpIndices[] = { Src0Idx, Src1Idx, Src2Idx }; 1976 1977 unsigned ConstantBusCount = 0; 1978 1979 if (AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm) != -1) 1980 ++ConstantBusCount; 1981 1982 unsigned SGPRUsed = findImplicitSGPRRead(MI); 1983 if (SGPRUsed != AMDGPU::NoRegister) 1984 ++ConstantBusCount; 1985 1986 for (int OpIdx : OpIndices) { 1987 if (OpIdx == -1) 1988 break; 1989 const MachineOperand &MO = MI.getOperand(OpIdx); 1990 if (usesConstantBus(MRI, MO, getOpSize(Opcode, OpIdx))) { 1991 if (MO.isReg()) { 1992 if (MO.getReg() != SGPRUsed) 1993 ++ConstantBusCount; 1994 SGPRUsed = MO.getReg(); 1995 } else { 1996 ++ConstantBusCount; 1997 } 1998 } 1999 } 2000 if (ConstantBusCount > 1) { 2001 ErrInfo = "VOP* instruction uses the constant bus more than once"; 2002 return false; 2003 } 2004 } 2005 2006 // Verify misc. restrictions on specific instructions. 2007 if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32 || 2008 Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64) { 2009 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 2010 const MachineOperand &Src1 = MI.getOperand(Src1Idx); 2011 const MachineOperand &Src2 = MI.getOperand(Src2Idx); 2012 if (Src0.isReg() && Src1.isReg() && Src2.isReg()) { 2013 if (!compareMachineOp(Src0, Src1) && 2014 !compareMachineOp(Src0, Src2)) { 2015 ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2"; 2016 return false; 2017 } 2018 } 2019 } 2020 2021 if (isSOPK(MI)) { 2022 int64_t Imm = getNamedOperand(MI, AMDGPU::OpName::simm16)->getImm(); 2023 if (sopkIsZext(MI)) { 2024 if (!isUInt<16>(Imm)) { 2025 ErrInfo = "invalid immediate for SOPK instruction"; 2026 return false; 2027 } 2028 } else { 2029 if (!isInt<16>(Imm)) { 2030 ErrInfo = "invalid immediate for SOPK instruction"; 2031 return false; 2032 } 2033 } 2034 } 2035 2036 if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 || 2037 Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 || 2038 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 2039 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) { 2040 const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 2041 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64; 2042 2043 const unsigned StaticNumOps = Desc.getNumOperands() + 2044 Desc.getNumImplicitUses(); 2045 const unsigned NumImplicitOps = IsDst ? 2 : 1; 2046 2047 // Allow additional implicit operands. This allows a fixup done by the post 2048 // RA scheduler where the main implicit operand is killed and implicit-defs 2049 // are added for sub-registers that remain live after this instruction. 2050 if (MI.getNumOperands() < StaticNumOps + NumImplicitOps) { 2051 ErrInfo = "missing implicit register operands"; 2052 return false; 2053 } 2054 2055 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 2056 if (IsDst) { 2057 if (!Dst->isUse()) { 2058 ErrInfo = "v_movreld_b32 vdst should be a use operand"; 2059 return false; 2060 } 2061 2062 unsigned UseOpIdx; 2063 if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) || 2064 UseOpIdx != StaticNumOps + 1) { 2065 ErrInfo = "movrel implicit operands should be tied"; 2066 return false; 2067 } 2068 } 2069 2070 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 2071 const MachineOperand &ImpUse 2072 = MI.getOperand(StaticNumOps + NumImplicitOps - 1); 2073 if (!ImpUse.isReg() || !ImpUse.isUse() || 2074 !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) { 2075 ErrInfo = "src0 should be subreg of implicit vector use"; 2076 return false; 2077 } 2078 } 2079 2080 // Make sure we aren't losing exec uses in the td files. This mostly requires 2081 // being careful when using let Uses to try to add other use registers. 2082 if (shouldReadExec(MI)) { 2083 if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) { 2084 ErrInfo = "VALU instruction does not implicitly read exec mask"; 2085 return false; 2086 } 2087 } 2088 2089 if (isSMRD(MI)) { 2090 if (MI.mayStore()) { 2091 // The register offset form of scalar stores may only use m0 as the 2092 // soffset register. 2093 const MachineOperand *Soff = getNamedOperand(MI, AMDGPU::OpName::soff); 2094 if (Soff && Soff->getReg() != AMDGPU::M0) { 2095 ErrInfo = "scalar stores must use m0 as offset register"; 2096 return false; 2097 } 2098 } 2099 } 2100 2101 return true; 2102 } 2103 2104 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) { 2105 switch (MI.getOpcode()) { 2106 default: return AMDGPU::INSTRUCTION_LIST_END; 2107 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE; 2108 case AMDGPU::COPY: return AMDGPU::COPY; 2109 case AMDGPU::PHI: return AMDGPU::PHI; 2110 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG; 2111 case AMDGPU::S_MOV_B32: 2112 return MI.getOperand(1).isReg() ? 2113 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32; 2114 case AMDGPU::S_ADD_I32: 2115 case AMDGPU::S_ADD_U32: return AMDGPU::V_ADD_I32_e32; 2116 case AMDGPU::S_ADDC_U32: return AMDGPU::V_ADDC_U32_e32; 2117 case AMDGPU::S_SUB_I32: 2118 case AMDGPU::S_SUB_U32: return AMDGPU::V_SUB_I32_e32; 2119 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32; 2120 case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_I32; 2121 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64; 2122 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64; 2123 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64; 2124 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64; 2125 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64; 2126 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64; 2127 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64; 2128 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32; 2129 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64; 2130 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32; 2131 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64; 2132 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32; 2133 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64; 2134 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32; 2135 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32; 2136 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32; 2137 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32; 2138 case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64; 2139 case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32; 2140 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32; 2141 case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32; 2142 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e32; 2143 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e32; 2144 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e32; 2145 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e32; 2146 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e32; 2147 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e32; 2148 case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e32; 2149 case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e32; 2150 case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e32; 2151 case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e32; 2152 case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e32; 2153 case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e32; 2154 case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e32; 2155 case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e32; 2156 case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64; 2157 case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32; 2158 case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32; 2159 case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64; 2160 case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ; 2161 case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ; 2162 } 2163 } 2164 2165 bool SIInstrInfo::isSALUOpSupportedOnVALU(const MachineInstr &MI) const { 2166 return getVALUOp(MI) != AMDGPU::INSTRUCTION_LIST_END; 2167 } 2168 2169 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI, 2170 unsigned OpNo) const { 2171 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 2172 const MCInstrDesc &Desc = get(MI.getOpcode()); 2173 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() || 2174 Desc.OpInfo[OpNo].RegClass == -1) { 2175 unsigned Reg = MI.getOperand(OpNo).getReg(); 2176 2177 if (TargetRegisterInfo::isVirtualRegister(Reg)) 2178 return MRI.getRegClass(Reg); 2179 return RI.getPhysRegClass(Reg); 2180 } 2181 2182 unsigned RCID = Desc.OpInfo[OpNo].RegClass; 2183 return RI.getRegClass(RCID); 2184 } 2185 2186 bool SIInstrInfo::canReadVGPR(const MachineInstr &MI, unsigned OpNo) const { 2187 switch (MI.getOpcode()) { 2188 case AMDGPU::COPY: 2189 case AMDGPU::REG_SEQUENCE: 2190 case AMDGPU::PHI: 2191 case AMDGPU::INSERT_SUBREG: 2192 return RI.hasVGPRs(getOpRegClass(MI, 0)); 2193 default: 2194 return RI.hasVGPRs(getOpRegClass(MI, OpNo)); 2195 } 2196 } 2197 2198 void SIInstrInfo::legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const { 2199 MachineBasicBlock::iterator I = MI; 2200 MachineBasicBlock *MBB = MI.getParent(); 2201 MachineOperand &MO = MI.getOperand(OpIdx); 2202 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 2203 unsigned RCID = get(MI.getOpcode()).OpInfo[OpIdx].RegClass; 2204 const TargetRegisterClass *RC = RI.getRegClass(RCID); 2205 unsigned Opcode = AMDGPU::V_MOV_B32_e32; 2206 if (MO.isReg()) 2207 Opcode = AMDGPU::COPY; 2208 else if (RI.isSGPRClass(RC)) 2209 Opcode = AMDGPU::S_MOV_B32; 2210 2211 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC); 2212 if (RI.getCommonSubClass(&AMDGPU::VReg_64RegClass, VRC)) 2213 VRC = &AMDGPU::VReg_64RegClass; 2214 else 2215 VRC = &AMDGPU::VGPR_32RegClass; 2216 2217 unsigned Reg = MRI.createVirtualRegister(VRC); 2218 DebugLoc DL = MBB->findDebugLoc(I); 2219 BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).addOperand(MO); 2220 MO.ChangeToRegister(Reg, false); 2221 } 2222 2223 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI, 2224 MachineRegisterInfo &MRI, 2225 MachineOperand &SuperReg, 2226 const TargetRegisterClass *SuperRC, 2227 unsigned SubIdx, 2228 const TargetRegisterClass *SubRC) 2229 const { 2230 MachineBasicBlock *MBB = MI->getParent(); 2231 DebugLoc DL = MI->getDebugLoc(); 2232 unsigned SubReg = MRI.createVirtualRegister(SubRC); 2233 2234 if (SuperReg.getSubReg() == AMDGPU::NoSubRegister) { 2235 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 2236 .addReg(SuperReg.getReg(), 0, SubIdx); 2237 return SubReg; 2238 } 2239 2240 // Just in case the super register is itself a sub-register, copy it to a new 2241 // value so we don't need to worry about merging its subreg index with the 2242 // SubIdx passed to this function. The register coalescer should be able to 2243 // eliminate this extra copy. 2244 unsigned NewSuperReg = MRI.createVirtualRegister(SuperRC); 2245 2246 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg) 2247 .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg()); 2248 2249 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 2250 .addReg(NewSuperReg, 0, SubIdx); 2251 2252 return SubReg; 2253 } 2254 2255 MachineOperand SIInstrInfo::buildExtractSubRegOrImm( 2256 MachineBasicBlock::iterator MII, 2257 MachineRegisterInfo &MRI, 2258 MachineOperand &Op, 2259 const TargetRegisterClass *SuperRC, 2260 unsigned SubIdx, 2261 const TargetRegisterClass *SubRC) const { 2262 if (Op.isImm()) { 2263 if (SubIdx == AMDGPU::sub0) 2264 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm())); 2265 if (SubIdx == AMDGPU::sub1) 2266 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32)); 2267 2268 llvm_unreachable("Unhandled register index for immediate"); 2269 } 2270 2271 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC, 2272 SubIdx, SubRC); 2273 return MachineOperand::CreateReg(SubReg, false); 2274 } 2275 2276 // Change the order of operands from (0, 1, 2) to (0, 2, 1) 2277 void SIInstrInfo::swapOperands(MachineInstr &Inst) const { 2278 assert(Inst.getNumExplicitOperands() == 3); 2279 MachineOperand Op1 = Inst.getOperand(1); 2280 Inst.RemoveOperand(1); 2281 Inst.addOperand(Op1); 2282 } 2283 2284 bool SIInstrInfo::isLegalRegOperand(const MachineRegisterInfo &MRI, 2285 const MCOperandInfo &OpInfo, 2286 const MachineOperand &MO) const { 2287 if (!MO.isReg()) 2288 return false; 2289 2290 unsigned Reg = MO.getReg(); 2291 const TargetRegisterClass *RC = 2292 TargetRegisterInfo::isVirtualRegister(Reg) ? 2293 MRI.getRegClass(Reg) : 2294 RI.getPhysRegClass(Reg); 2295 2296 const SIRegisterInfo *TRI = 2297 static_cast<const SIRegisterInfo*>(MRI.getTargetRegisterInfo()); 2298 RC = TRI->getSubRegClass(RC, MO.getSubReg()); 2299 2300 // In order to be legal, the common sub-class must be equal to the 2301 // class of the current operand. For example: 2302 // 2303 // v_mov_b32 s0 ; Operand defined as vsrc_b32 2304 // ; RI.getCommonSubClass(s0,vsrc_b32) = sgpr ; LEGAL 2305 // 2306 // s_sendmsg 0, s0 ; Operand defined as m0reg 2307 // ; RI.getCommonSubClass(s0,m0reg) = m0reg ; NOT LEGAL 2308 2309 return RI.getCommonSubClass(RC, RI.getRegClass(OpInfo.RegClass)) == RC; 2310 } 2311 2312 bool SIInstrInfo::isLegalVSrcOperand(const MachineRegisterInfo &MRI, 2313 const MCOperandInfo &OpInfo, 2314 const MachineOperand &MO) const { 2315 if (MO.isReg()) 2316 return isLegalRegOperand(MRI, OpInfo, MO); 2317 2318 // Handle non-register types that are treated like immediates. 2319 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI()); 2320 return true; 2321 } 2322 2323 bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx, 2324 const MachineOperand *MO) const { 2325 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 2326 const MCInstrDesc &InstDesc = MI.getDesc(); 2327 const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx]; 2328 const TargetRegisterClass *DefinedRC = 2329 OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr; 2330 if (!MO) 2331 MO = &MI.getOperand(OpIdx); 2332 2333 if (isVALU(MI) && usesConstantBus(MRI, *MO, DefinedRC->getSize())) { 2334 2335 RegSubRegPair SGPRUsed; 2336 if (MO->isReg()) 2337 SGPRUsed = RegSubRegPair(MO->getReg(), MO->getSubReg()); 2338 2339 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 2340 if (i == OpIdx) 2341 continue; 2342 const MachineOperand &Op = MI.getOperand(i); 2343 if (Op.isReg()) { 2344 if ((Op.getReg() != SGPRUsed.Reg || Op.getSubReg() != SGPRUsed.SubReg) && 2345 usesConstantBus(MRI, Op, getOpSize(MI, i))) { 2346 return false; 2347 } 2348 } else if (InstDesc.OpInfo[i].OperandType == AMDGPU::OPERAND_KIMM32) { 2349 return false; 2350 } 2351 } 2352 } 2353 2354 if (MO->isReg()) { 2355 assert(DefinedRC); 2356 return isLegalRegOperand(MRI, OpInfo, *MO); 2357 } 2358 2359 // Handle non-register types that are treated like immediates. 2360 assert(MO->isImm() || MO->isTargetIndex() || MO->isFI()); 2361 2362 if (!DefinedRC) { 2363 // This operand expects an immediate. 2364 return true; 2365 } 2366 2367 return isImmOperandLegal(MI, OpIdx, *MO); 2368 } 2369 2370 void SIInstrInfo::legalizeOperandsVOP2(MachineRegisterInfo &MRI, 2371 MachineInstr &MI) const { 2372 unsigned Opc = MI.getOpcode(); 2373 const MCInstrDesc &InstrDesc = get(Opc); 2374 2375 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 2376 MachineOperand &Src1 = MI.getOperand(Src1Idx); 2377 2378 // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32 2379 // we need to only have one constant bus use. 2380 // 2381 // Note we do not need to worry about literal constants here. They are 2382 // disabled for the operand type for instructions because they will always 2383 // violate the one constant bus use rule. 2384 bool HasImplicitSGPR = findImplicitSGPRRead(MI) != AMDGPU::NoRegister; 2385 if (HasImplicitSGPR) { 2386 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 2387 MachineOperand &Src0 = MI.getOperand(Src0Idx); 2388 2389 if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) 2390 legalizeOpWithMove(MI, Src0Idx); 2391 } 2392 2393 // VOP2 src0 instructions support all operand types, so we don't need to check 2394 // their legality. If src1 is already legal, we don't need to do anything. 2395 if (isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src1)) 2396 return; 2397 2398 // We do not use commuteInstruction here because it is too aggressive and will 2399 // commute if it is possible. We only want to commute here if it improves 2400 // legality. This can be called a fairly large number of times so don't waste 2401 // compile time pointlessly swapping and checking legality again. 2402 if (HasImplicitSGPR || !MI.isCommutable()) { 2403 legalizeOpWithMove(MI, Src1Idx); 2404 return; 2405 } 2406 2407 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 2408 MachineOperand &Src0 = MI.getOperand(Src0Idx); 2409 2410 // If src0 can be used as src1, commuting will make the operands legal. 2411 // Otherwise we have to give up and insert a move. 2412 // 2413 // TODO: Other immediate-like operand kinds could be commuted if there was a 2414 // MachineOperand::ChangeTo* for them. 2415 if ((!Src1.isImm() && !Src1.isReg()) || 2416 !isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src0)) { 2417 legalizeOpWithMove(MI, Src1Idx); 2418 return; 2419 } 2420 2421 int CommutedOpc = commuteOpcode(MI); 2422 if (CommutedOpc == -1) { 2423 legalizeOpWithMove(MI, Src1Idx); 2424 return; 2425 } 2426 2427 MI.setDesc(get(CommutedOpc)); 2428 2429 unsigned Src0Reg = Src0.getReg(); 2430 unsigned Src0SubReg = Src0.getSubReg(); 2431 bool Src0Kill = Src0.isKill(); 2432 2433 if (Src1.isImm()) 2434 Src0.ChangeToImmediate(Src1.getImm()); 2435 else if (Src1.isReg()) { 2436 Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill()); 2437 Src0.setSubReg(Src1.getSubReg()); 2438 } else 2439 llvm_unreachable("Should only have register or immediate operands"); 2440 2441 Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill); 2442 Src1.setSubReg(Src0SubReg); 2443 } 2444 2445 // Legalize VOP3 operands. Because all operand types are supported for any 2446 // operand, and since literal constants are not allowed and should never be 2447 // seen, we only need to worry about inserting copies if we use multiple SGPR 2448 // operands. 2449 void SIInstrInfo::legalizeOperandsVOP3(MachineRegisterInfo &MRI, 2450 MachineInstr &MI) const { 2451 unsigned Opc = MI.getOpcode(); 2452 2453 int VOP3Idx[3] = { 2454 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0), 2455 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1), 2456 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2) 2457 }; 2458 2459 // Find the one SGPR operand we are allowed to use. 2460 unsigned SGPRReg = findUsedSGPR(MI, VOP3Idx); 2461 2462 for (unsigned i = 0; i < 3; ++i) { 2463 int Idx = VOP3Idx[i]; 2464 if (Idx == -1) 2465 break; 2466 MachineOperand &MO = MI.getOperand(Idx); 2467 2468 // We should never see a VOP3 instruction with an illegal immediate operand. 2469 if (!MO.isReg()) 2470 continue; 2471 2472 if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg()))) 2473 continue; // VGPRs are legal 2474 2475 if (SGPRReg == AMDGPU::NoRegister || SGPRReg == MO.getReg()) { 2476 SGPRReg = MO.getReg(); 2477 // We can use one SGPR in each VOP3 instruction. 2478 continue; 2479 } 2480 2481 // If we make it this far, then the operand is not legal and we must 2482 // legalize it. 2483 legalizeOpWithMove(MI, Idx); 2484 } 2485 } 2486 2487 unsigned SIInstrInfo::readlaneVGPRToSGPR(unsigned SrcReg, MachineInstr &UseMI, 2488 MachineRegisterInfo &MRI) const { 2489 const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg); 2490 const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC); 2491 unsigned DstReg = MRI.createVirtualRegister(SRC); 2492 unsigned SubRegs = VRC->getSize() / 4; 2493 2494 SmallVector<unsigned, 8> SRegs; 2495 for (unsigned i = 0; i < SubRegs; ++i) { 2496 unsigned SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 2497 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 2498 get(AMDGPU::V_READFIRSTLANE_B32), SGPR) 2499 .addReg(SrcReg, 0, RI.getSubRegFromChannel(i)); 2500 SRegs.push_back(SGPR); 2501 } 2502 2503 MachineInstrBuilder MIB = 2504 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 2505 get(AMDGPU::REG_SEQUENCE), DstReg); 2506 for (unsigned i = 0; i < SubRegs; ++i) { 2507 MIB.addReg(SRegs[i]); 2508 MIB.addImm(RI.getSubRegFromChannel(i)); 2509 } 2510 return DstReg; 2511 } 2512 2513 void SIInstrInfo::legalizeOperandsSMRD(MachineRegisterInfo &MRI, 2514 MachineInstr &MI) const { 2515 2516 // If the pointer is store in VGPRs, then we need to move them to 2517 // SGPRs using v_readfirstlane. This is safe because we only select 2518 // loads with uniform pointers to SMRD instruction so we know the 2519 // pointer value is uniform. 2520 MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase); 2521 if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) { 2522 unsigned SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI); 2523 SBase->setReg(SGPR); 2524 } 2525 } 2526 2527 void SIInstrInfo::legalizeGenericOperand(MachineBasicBlock &InsertMBB, 2528 MachineBasicBlock::iterator I, 2529 const TargetRegisterClass *DstRC, 2530 MachineOperand &Op, 2531 MachineRegisterInfo &MRI, 2532 const DebugLoc &DL) const { 2533 2534 unsigned OpReg = Op.getReg(); 2535 unsigned OpSubReg = Op.getSubReg(); 2536 2537 const TargetRegisterClass *OpRC = RI.getSubClassWithSubReg( 2538 RI.getRegClassForReg(MRI, OpReg), OpSubReg); 2539 2540 // Check if operand is already the correct register class. 2541 if (DstRC == OpRC) 2542 return; 2543 2544 unsigned DstReg = MRI.createVirtualRegister(DstRC); 2545 MachineInstr *Copy = BuildMI(InsertMBB, I, DL, get(AMDGPU::COPY), DstReg) 2546 .addOperand(Op); 2547 2548 Op.setReg(DstReg); 2549 Op.setSubReg(0); 2550 2551 MachineInstr *Def = MRI.getVRegDef(OpReg); 2552 if (!Def) 2553 return; 2554 2555 // Try to eliminate the copy if it is copying an immediate value. 2556 if (Def->isMoveImmediate()) 2557 FoldImmediate(*Copy, *Def, OpReg, &MRI); 2558 } 2559 2560 void SIInstrInfo::legalizeOperands(MachineInstr &MI) const { 2561 MachineFunction &MF = *MI.getParent()->getParent(); 2562 MachineRegisterInfo &MRI = MF.getRegInfo(); 2563 2564 // Legalize VOP2 2565 if (isVOP2(MI) || isVOPC(MI)) { 2566 legalizeOperandsVOP2(MRI, MI); 2567 return; 2568 } 2569 2570 // Legalize VOP3 2571 if (isVOP3(MI)) { 2572 legalizeOperandsVOP3(MRI, MI); 2573 return; 2574 } 2575 2576 // Legalize SMRD 2577 if (isSMRD(MI)) { 2578 legalizeOperandsSMRD(MRI, MI); 2579 return; 2580 } 2581 2582 // Legalize REG_SEQUENCE and PHI 2583 // The register class of the operands much be the same type as the register 2584 // class of the output. 2585 if (MI.getOpcode() == AMDGPU::PHI) { 2586 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr; 2587 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { 2588 if (!MI.getOperand(i).isReg() || 2589 !TargetRegisterInfo::isVirtualRegister(MI.getOperand(i).getReg())) 2590 continue; 2591 const TargetRegisterClass *OpRC = 2592 MRI.getRegClass(MI.getOperand(i).getReg()); 2593 if (RI.hasVGPRs(OpRC)) { 2594 VRC = OpRC; 2595 } else { 2596 SRC = OpRC; 2597 } 2598 } 2599 2600 // If any of the operands are VGPR registers, then they all most be 2601 // otherwise we will create illegal VGPR->SGPR copies when legalizing 2602 // them. 2603 if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) { 2604 if (!VRC) { 2605 assert(SRC); 2606 VRC = RI.getEquivalentVGPRClass(SRC); 2607 } 2608 RC = VRC; 2609 } else { 2610 RC = SRC; 2611 } 2612 2613 // Update all the operands so they have the same type. 2614 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 2615 MachineOperand &Op = MI.getOperand(I); 2616 if (!Op.isReg() || !TargetRegisterInfo::isVirtualRegister(Op.getReg())) 2617 continue; 2618 2619 // MI is a PHI instruction. 2620 MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB(); 2621 MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator(); 2622 2623 // Avoid creating no-op copies with the same src and dst reg class. These 2624 // confuse some of the machine passes. 2625 legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc()); 2626 } 2627 } 2628 2629 // REG_SEQUENCE doesn't really require operand legalization, but if one has a 2630 // VGPR dest type and SGPR sources, insert copies so all operands are 2631 // VGPRs. This seems to help operand folding / the register coalescer. 2632 if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) { 2633 MachineBasicBlock *MBB = MI.getParent(); 2634 const TargetRegisterClass *DstRC = getOpRegClass(MI, 0); 2635 if (RI.hasVGPRs(DstRC)) { 2636 // Update all the operands so they are VGPR register classes. These may 2637 // not be the same register class because REG_SEQUENCE supports mixing 2638 // subregister index types e.g. sub0_sub1 + sub2 + sub3 2639 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 2640 MachineOperand &Op = MI.getOperand(I); 2641 if (!Op.isReg() || !TargetRegisterInfo::isVirtualRegister(Op.getReg())) 2642 continue; 2643 2644 const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg()); 2645 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC); 2646 if (VRC == OpRC) 2647 continue; 2648 2649 legalizeGenericOperand(*MBB, MI, VRC, Op, MRI, MI.getDebugLoc()); 2650 Op.setIsKill(); 2651 } 2652 } 2653 2654 return; 2655 } 2656 2657 // Legalize INSERT_SUBREG 2658 // src0 must have the same register class as dst 2659 if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) { 2660 unsigned Dst = MI.getOperand(0).getReg(); 2661 unsigned Src0 = MI.getOperand(1).getReg(); 2662 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst); 2663 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0); 2664 if (DstRC != Src0RC) { 2665 MachineBasicBlock *MBB = MI.getParent(); 2666 MachineOperand &Op = MI.getOperand(1); 2667 legalizeGenericOperand(*MBB, MI, DstRC, Op, MRI, MI.getDebugLoc()); 2668 } 2669 return; 2670 } 2671 2672 // Legalize MIMG and MUBUF/MTBUF for shaders. 2673 // 2674 // Shaders only generate MUBUF/MTBUF instructions via intrinsics or via 2675 // scratch memory access. In both cases, the legalization never involves 2676 // conversion to the addr64 form. 2677 if (isMIMG(MI) || 2678 (AMDGPU::isShader(MF.getFunction()->getCallingConv()) && 2679 (isMUBUF(MI) || isMTBUF(MI)))) { 2680 MachineOperand *SRsrc = getNamedOperand(MI, AMDGPU::OpName::srsrc); 2681 if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg()))) { 2682 unsigned SGPR = readlaneVGPRToSGPR(SRsrc->getReg(), MI, MRI); 2683 SRsrc->setReg(SGPR); 2684 } 2685 2686 MachineOperand *SSamp = getNamedOperand(MI, AMDGPU::OpName::ssamp); 2687 if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg()))) { 2688 unsigned SGPR = readlaneVGPRToSGPR(SSamp->getReg(), MI, MRI); 2689 SSamp->setReg(SGPR); 2690 } 2691 return; 2692 } 2693 2694 // Legalize MUBUF* instructions by converting to addr64 form. 2695 // FIXME: If we start using the non-addr64 instructions for compute, we 2696 // may need to legalize them as above. This especially applies to the 2697 // buffer_load_format_* variants and variants with idxen (or bothen). 2698 int SRsrcIdx = 2699 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc); 2700 if (SRsrcIdx != -1) { 2701 // We have an MUBUF instruction 2702 MachineOperand *SRsrc = &MI.getOperand(SRsrcIdx); 2703 unsigned SRsrcRC = get(MI.getOpcode()).OpInfo[SRsrcIdx].RegClass; 2704 if (RI.getCommonSubClass(MRI.getRegClass(SRsrc->getReg()), 2705 RI.getRegClass(SRsrcRC))) { 2706 // The operands are legal. 2707 // FIXME: We may need to legalize operands besided srsrc. 2708 return; 2709 } 2710 2711 MachineBasicBlock &MBB = *MI.getParent(); 2712 2713 // Extract the ptr from the resource descriptor. 2714 unsigned SRsrcPtr = buildExtractSubReg(MI, MRI, *SRsrc, 2715 &AMDGPU::VReg_128RegClass, AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass); 2716 2717 // Create an empty resource descriptor 2718 unsigned Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 2719 unsigned SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 2720 unsigned SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 2721 unsigned NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass); 2722 uint64_t RsrcDataFormat = getDefaultRsrcDataFormat(); 2723 2724 // Zero64 = 0 2725 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::S_MOV_B64), Zero64) 2726 .addImm(0); 2727 2728 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0} 2729 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::S_MOV_B32), SRsrcFormatLo) 2730 .addImm(RsrcDataFormat & 0xFFFFFFFF); 2731 2732 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32} 2733 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::S_MOV_B32), SRsrcFormatHi) 2734 .addImm(RsrcDataFormat >> 32); 2735 2736 // NewSRsrc = {Zero64, SRsrcFormat} 2737 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewSRsrc) 2738 .addReg(Zero64) 2739 .addImm(AMDGPU::sub0_sub1) 2740 .addReg(SRsrcFormatLo) 2741 .addImm(AMDGPU::sub2) 2742 .addReg(SRsrcFormatHi) 2743 .addImm(AMDGPU::sub3); 2744 2745 MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 2746 unsigned NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 2747 if (VAddr) { 2748 // This is already an ADDR64 instruction so we need to add the pointer 2749 // extracted from the resource descriptor to the current value of VAddr. 2750 unsigned NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 2751 unsigned NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 2752 2753 // NewVaddrLo = SRsrcPtr:sub0 + VAddr:sub0 2754 DebugLoc DL = MI.getDebugLoc(); 2755 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_I32_e32), NewVAddrLo) 2756 .addReg(SRsrcPtr, 0, AMDGPU::sub0) 2757 .addReg(VAddr->getReg(), 0, AMDGPU::sub0); 2758 2759 // NewVaddrHi = SRsrcPtr:sub1 + VAddr:sub1 2760 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e32), NewVAddrHi) 2761 .addReg(SRsrcPtr, 0, AMDGPU::sub1) 2762 .addReg(VAddr->getReg(), 0, AMDGPU::sub1); 2763 2764 // NewVaddr = {NewVaddrHi, NewVaddrLo} 2765 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr) 2766 .addReg(NewVAddrLo) 2767 .addImm(AMDGPU::sub0) 2768 .addReg(NewVAddrHi) 2769 .addImm(AMDGPU::sub1); 2770 } else { 2771 // This instructions is the _OFFSET variant, so we need to convert it to 2772 // ADDR64. 2773 assert(MBB.getParent()->getSubtarget<SISubtarget>().getGeneration() 2774 < SISubtarget::VOLCANIC_ISLANDS && 2775 "FIXME: Need to emit flat atomics here"); 2776 2777 MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata); 2778 MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 2779 MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset); 2780 unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode()); 2781 2782 // Atomics rith return have have an additional tied operand and are 2783 // missing some of the special bits. 2784 MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in); 2785 MachineInstr *Addr64; 2786 2787 if (!VDataIn) { 2788 // Regular buffer load / store. 2789 MachineInstrBuilder MIB = 2790 BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 2791 .addOperand(*VData) 2792 .addReg(AMDGPU::NoRegister) // Dummy value for vaddr. 2793 // This will be replaced later 2794 // with the new value of vaddr. 2795 .addOperand(*SRsrc) 2796 .addOperand(*SOffset) 2797 .addOperand(*Offset); 2798 2799 // Atomics do not have this operand. 2800 if (const MachineOperand *GLC = 2801 getNamedOperand(MI, AMDGPU::OpName::glc)) { 2802 MIB.addImm(GLC->getImm()); 2803 } 2804 2805 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::slc)); 2806 2807 if (const MachineOperand *TFE = 2808 getNamedOperand(MI, AMDGPU::OpName::tfe)) { 2809 MIB.addImm(TFE->getImm()); 2810 } 2811 2812 MIB.setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 2813 Addr64 = MIB; 2814 } else { 2815 // Atomics with return. 2816 Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 2817 .addOperand(*VData) 2818 .addOperand(*VDataIn) 2819 .addReg(AMDGPU::NoRegister) // Dummy value for vaddr. 2820 // This will be replaced later 2821 // with the new value of vaddr. 2822 .addOperand(*SRsrc) 2823 .addOperand(*SOffset) 2824 .addOperand(*Offset) 2825 .addImm(getNamedImmOperand(MI, AMDGPU::OpName::slc)) 2826 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 2827 } 2828 2829 MI.removeFromParent(); 2830 2831 // NewVaddr = {NewVaddrHi, NewVaddrLo} 2832 BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), 2833 NewVAddr) 2834 .addReg(SRsrcPtr, 0, AMDGPU::sub0) 2835 .addImm(AMDGPU::sub0) 2836 .addReg(SRsrcPtr, 0, AMDGPU::sub1) 2837 .addImm(AMDGPU::sub1); 2838 2839 VAddr = getNamedOperand(*Addr64, AMDGPU::OpName::vaddr); 2840 SRsrc = getNamedOperand(*Addr64, AMDGPU::OpName::srsrc); 2841 } 2842 2843 // Update the instruction to use NewVaddr 2844 VAddr->setReg(NewVAddr); 2845 // Update the instruction to use NewSRsrc 2846 SRsrc->setReg(NewSRsrc); 2847 } 2848 } 2849 2850 void SIInstrInfo::moveToVALU(MachineInstr &TopInst) const { 2851 SmallVector<MachineInstr *, 128> Worklist; 2852 Worklist.push_back(&TopInst); 2853 2854 while (!Worklist.empty()) { 2855 MachineInstr &Inst = *Worklist.pop_back_val(); 2856 MachineBasicBlock *MBB = Inst.getParent(); 2857 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 2858 2859 unsigned Opcode = Inst.getOpcode(); 2860 unsigned NewOpcode = getVALUOp(Inst); 2861 2862 // Handle some special cases 2863 switch (Opcode) { 2864 default: 2865 break; 2866 case AMDGPU::S_AND_B64: 2867 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::V_AND_B32_e64); 2868 Inst.eraseFromParent(); 2869 continue; 2870 2871 case AMDGPU::S_OR_B64: 2872 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::V_OR_B32_e64); 2873 Inst.eraseFromParent(); 2874 continue; 2875 2876 case AMDGPU::S_XOR_B64: 2877 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::V_XOR_B32_e64); 2878 Inst.eraseFromParent(); 2879 continue; 2880 2881 case AMDGPU::S_NOT_B64: 2882 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::V_NOT_B32_e32); 2883 Inst.eraseFromParent(); 2884 continue; 2885 2886 case AMDGPU::S_BCNT1_I32_B64: 2887 splitScalar64BitBCNT(Worklist, Inst); 2888 Inst.eraseFromParent(); 2889 continue; 2890 2891 case AMDGPU::S_BFE_I64: { 2892 splitScalar64BitBFE(Worklist, Inst); 2893 Inst.eraseFromParent(); 2894 continue; 2895 } 2896 2897 case AMDGPU::S_LSHL_B32: 2898 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2899 NewOpcode = AMDGPU::V_LSHLREV_B32_e64; 2900 swapOperands(Inst); 2901 } 2902 break; 2903 case AMDGPU::S_ASHR_I32: 2904 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2905 NewOpcode = AMDGPU::V_ASHRREV_I32_e64; 2906 swapOperands(Inst); 2907 } 2908 break; 2909 case AMDGPU::S_LSHR_B32: 2910 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2911 NewOpcode = AMDGPU::V_LSHRREV_B32_e64; 2912 swapOperands(Inst); 2913 } 2914 break; 2915 case AMDGPU::S_LSHL_B64: 2916 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2917 NewOpcode = AMDGPU::V_LSHLREV_B64; 2918 swapOperands(Inst); 2919 } 2920 break; 2921 case AMDGPU::S_ASHR_I64: 2922 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2923 NewOpcode = AMDGPU::V_ASHRREV_I64; 2924 swapOperands(Inst); 2925 } 2926 break; 2927 case AMDGPU::S_LSHR_B64: 2928 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2929 NewOpcode = AMDGPU::V_LSHRREV_B64; 2930 swapOperands(Inst); 2931 } 2932 break; 2933 2934 case AMDGPU::S_ABS_I32: 2935 lowerScalarAbs(Worklist, Inst); 2936 Inst.eraseFromParent(); 2937 continue; 2938 2939 case AMDGPU::S_CBRANCH_SCC0: 2940 case AMDGPU::S_CBRANCH_SCC1: 2941 // Clear unused bits of vcc 2942 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(AMDGPU::S_AND_B64), 2943 AMDGPU::VCC) 2944 .addReg(AMDGPU::EXEC) 2945 .addReg(AMDGPU::VCC); 2946 break; 2947 2948 case AMDGPU::S_BFE_U64: 2949 case AMDGPU::S_BFM_B64: 2950 llvm_unreachable("Moving this op to VALU not implemented"); 2951 } 2952 2953 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) { 2954 // We cannot move this instruction to the VALU, so we should try to 2955 // legalize its operands instead. 2956 legalizeOperands(Inst); 2957 continue; 2958 } 2959 2960 // Use the new VALU Opcode. 2961 const MCInstrDesc &NewDesc = get(NewOpcode); 2962 Inst.setDesc(NewDesc); 2963 2964 // Remove any references to SCC. Vector instructions can't read from it, and 2965 // We're just about to add the implicit use / defs of VCC, and we don't want 2966 // both. 2967 for (unsigned i = Inst.getNumOperands() - 1; i > 0; --i) { 2968 MachineOperand &Op = Inst.getOperand(i); 2969 if (Op.isReg() && Op.getReg() == AMDGPU::SCC) { 2970 Inst.RemoveOperand(i); 2971 addSCCDefUsersToVALUWorklist(Inst, Worklist); 2972 } 2973 } 2974 2975 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) { 2976 // We are converting these to a BFE, so we need to add the missing 2977 // operands for the size and offset. 2978 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16; 2979 Inst.addOperand(MachineOperand::CreateImm(0)); 2980 Inst.addOperand(MachineOperand::CreateImm(Size)); 2981 2982 } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) { 2983 // The VALU version adds the second operand to the result, so insert an 2984 // extra 0 operand. 2985 Inst.addOperand(MachineOperand::CreateImm(0)); 2986 } 2987 2988 Inst.addImplicitDefUseOperands(*Inst.getParent()->getParent()); 2989 2990 if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) { 2991 const MachineOperand &OffsetWidthOp = Inst.getOperand(2); 2992 // If we need to move this to VGPRs, we need to unpack the second operand 2993 // back into the 2 separate ones for bit offset and width. 2994 assert(OffsetWidthOp.isImm() && 2995 "Scalar BFE is only implemented for constant width and offset"); 2996 uint32_t Imm = OffsetWidthOp.getImm(); 2997 2998 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 2999 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 3000 Inst.RemoveOperand(2); // Remove old immediate. 3001 Inst.addOperand(MachineOperand::CreateImm(Offset)); 3002 Inst.addOperand(MachineOperand::CreateImm(BitWidth)); 3003 } 3004 3005 bool HasDst = Inst.getOperand(0).isReg() && Inst.getOperand(0).isDef(); 3006 unsigned NewDstReg = AMDGPU::NoRegister; 3007 if (HasDst) { 3008 // Update the destination register class. 3009 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst); 3010 if (!NewDstRC) 3011 continue; 3012 3013 unsigned DstReg = Inst.getOperand(0).getReg(); 3014 if (Inst.isCopy() && 3015 TargetRegisterInfo::isVirtualRegister(Inst.getOperand(1).getReg()) && 3016 NewDstRC == RI.getRegClassForReg(MRI, Inst.getOperand(1).getReg())) { 3017 // Instead of creating a copy where src and dst are the same register 3018 // class, we just replace all uses of dst with src. These kinds of 3019 // copies interfere with the heuristics MachineSink uses to decide 3020 // whether or not to split a critical edge. Since the pass assumes 3021 // that copies will end up as machine instructions and not be 3022 // eliminated. 3023 addUsersToMoveToVALUWorklist(DstReg, MRI, Worklist); 3024 MRI.replaceRegWith(DstReg, Inst.getOperand(1).getReg()); 3025 MRI.clearKillFlags(Inst.getOperand(1).getReg()); 3026 Inst.getOperand(0).setReg(DstReg); 3027 continue; 3028 } 3029 3030 NewDstReg = MRI.createVirtualRegister(NewDstRC); 3031 MRI.replaceRegWith(DstReg, NewDstReg); 3032 } 3033 3034 // Legalize the operands 3035 legalizeOperands(Inst); 3036 3037 if (HasDst) 3038 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist); 3039 } 3040 } 3041 3042 void SIInstrInfo::lowerScalarAbs(SmallVectorImpl<MachineInstr *> &Worklist, 3043 MachineInstr &Inst) const { 3044 MachineBasicBlock &MBB = *Inst.getParent(); 3045 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3046 MachineBasicBlock::iterator MII = Inst; 3047 DebugLoc DL = Inst.getDebugLoc(); 3048 3049 MachineOperand &Dest = Inst.getOperand(0); 3050 MachineOperand &Src = Inst.getOperand(1); 3051 unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3052 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3053 3054 BuildMI(MBB, MII, DL, get(AMDGPU::V_SUB_I32_e32), TmpReg) 3055 .addImm(0) 3056 .addReg(Src.getReg()); 3057 3058 BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg) 3059 .addReg(Src.getReg()) 3060 .addReg(TmpReg); 3061 3062 MRI.replaceRegWith(Dest.getReg(), ResultReg); 3063 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 3064 } 3065 3066 void SIInstrInfo::splitScalar64BitUnaryOp( 3067 SmallVectorImpl<MachineInstr *> &Worklist, MachineInstr &Inst, 3068 unsigned Opcode) const { 3069 MachineBasicBlock &MBB = *Inst.getParent(); 3070 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3071 3072 MachineOperand &Dest = Inst.getOperand(0); 3073 MachineOperand &Src0 = Inst.getOperand(1); 3074 DebugLoc DL = Inst.getDebugLoc(); 3075 3076 MachineBasicBlock::iterator MII = Inst; 3077 3078 const MCInstrDesc &InstDesc = get(Opcode); 3079 const TargetRegisterClass *Src0RC = Src0.isReg() ? 3080 MRI.getRegClass(Src0.getReg()) : 3081 &AMDGPU::SGPR_32RegClass; 3082 3083 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 3084 3085 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 3086 AMDGPU::sub0, Src0SubRC); 3087 3088 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 3089 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 3090 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 3091 3092 unsigned DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 3093 BuildMI(MBB, MII, DL, InstDesc, DestSub0) 3094 .addOperand(SrcReg0Sub0); 3095 3096 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 3097 AMDGPU::sub1, Src0SubRC); 3098 3099 unsigned DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 3100 BuildMI(MBB, MII, DL, InstDesc, DestSub1) 3101 .addOperand(SrcReg0Sub1); 3102 3103 unsigned FullDestReg = MRI.createVirtualRegister(NewDestRC); 3104 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 3105 .addReg(DestSub0) 3106 .addImm(AMDGPU::sub0) 3107 .addReg(DestSub1) 3108 .addImm(AMDGPU::sub1); 3109 3110 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 3111 3112 // We don't need to legalizeOperands here because for a single operand, src0 3113 // will support any kind of input. 3114 3115 // Move all users of this moved value. 3116 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 3117 } 3118 3119 void SIInstrInfo::splitScalar64BitBinaryOp( 3120 SmallVectorImpl<MachineInstr *> &Worklist, MachineInstr &Inst, 3121 unsigned Opcode) const { 3122 MachineBasicBlock &MBB = *Inst.getParent(); 3123 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3124 3125 MachineOperand &Dest = Inst.getOperand(0); 3126 MachineOperand &Src0 = Inst.getOperand(1); 3127 MachineOperand &Src1 = Inst.getOperand(2); 3128 DebugLoc DL = Inst.getDebugLoc(); 3129 3130 MachineBasicBlock::iterator MII = Inst; 3131 3132 const MCInstrDesc &InstDesc = get(Opcode); 3133 const TargetRegisterClass *Src0RC = Src0.isReg() ? 3134 MRI.getRegClass(Src0.getReg()) : 3135 &AMDGPU::SGPR_32RegClass; 3136 3137 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 3138 const TargetRegisterClass *Src1RC = Src1.isReg() ? 3139 MRI.getRegClass(Src1.getReg()) : 3140 &AMDGPU::SGPR_32RegClass; 3141 3142 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); 3143 3144 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 3145 AMDGPU::sub0, Src0SubRC); 3146 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 3147 AMDGPU::sub0, Src1SubRC); 3148 3149 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 3150 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 3151 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 3152 3153 unsigned DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 3154 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0) 3155 .addOperand(SrcReg0Sub0) 3156 .addOperand(SrcReg1Sub0); 3157 3158 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 3159 AMDGPU::sub1, Src0SubRC); 3160 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 3161 AMDGPU::sub1, Src1SubRC); 3162 3163 unsigned DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 3164 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1) 3165 .addOperand(SrcReg0Sub1) 3166 .addOperand(SrcReg1Sub1); 3167 3168 unsigned FullDestReg = MRI.createVirtualRegister(NewDestRC); 3169 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 3170 .addReg(DestSub0) 3171 .addImm(AMDGPU::sub0) 3172 .addReg(DestSub1) 3173 .addImm(AMDGPU::sub1); 3174 3175 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 3176 3177 // Try to legalize the operands in case we need to swap the order to keep it 3178 // valid. 3179 legalizeOperands(LoHalf); 3180 legalizeOperands(HiHalf); 3181 3182 // Move all users of this moved vlaue. 3183 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 3184 } 3185 3186 void SIInstrInfo::splitScalar64BitBCNT( 3187 SmallVectorImpl<MachineInstr *> &Worklist, MachineInstr &Inst) const { 3188 MachineBasicBlock &MBB = *Inst.getParent(); 3189 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3190 3191 MachineBasicBlock::iterator MII = Inst; 3192 DebugLoc DL = Inst.getDebugLoc(); 3193 3194 MachineOperand &Dest = Inst.getOperand(0); 3195 MachineOperand &Src = Inst.getOperand(1); 3196 3197 const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64); 3198 const TargetRegisterClass *SrcRC = Src.isReg() ? 3199 MRI.getRegClass(Src.getReg()) : 3200 &AMDGPU::SGPR_32RegClass; 3201 3202 unsigned MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3203 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3204 3205 const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0); 3206 3207 MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 3208 AMDGPU::sub0, SrcSubRC); 3209 MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 3210 AMDGPU::sub1, SrcSubRC); 3211 3212 BuildMI(MBB, MII, DL, InstDesc, MidReg) 3213 .addOperand(SrcRegSub0) 3214 .addImm(0); 3215 3216 BuildMI(MBB, MII, DL, InstDesc, ResultReg) 3217 .addOperand(SrcRegSub1) 3218 .addReg(MidReg); 3219 3220 MRI.replaceRegWith(Dest.getReg(), ResultReg); 3221 3222 // We don't need to legalize operands here. src0 for etiher instruction can be 3223 // an SGPR, and the second input is unused or determined here. 3224 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 3225 } 3226 3227 void SIInstrInfo::splitScalar64BitBFE(SmallVectorImpl<MachineInstr *> &Worklist, 3228 MachineInstr &Inst) const { 3229 MachineBasicBlock &MBB = *Inst.getParent(); 3230 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3231 MachineBasicBlock::iterator MII = Inst; 3232 DebugLoc DL = Inst.getDebugLoc(); 3233 3234 MachineOperand &Dest = Inst.getOperand(0); 3235 uint32_t Imm = Inst.getOperand(2).getImm(); 3236 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 3237 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 3238 3239 (void) Offset; 3240 3241 // Only sext_inreg cases handled. 3242 assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 && 3243 Offset == 0 && "Not implemented"); 3244 3245 if (BitWidth < 32) { 3246 unsigned MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3247 unsigned MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3248 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 3249 3250 BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32), MidRegLo) 3251 .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0) 3252 .addImm(0) 3253 .addImm(BitWidth); 3254 3255 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi) 3256 .addImm(31) 3257 .addReg(MidRegLo); 3258 3259 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 3260 .addReg(MidRegLo) 3261 .addImm(AMDGPU::sub0) 3262 .addReg(MidRegHi) 3263 .addImm(AMDGPU::sub1); 3264 3265 MRI.replaceRegWith(Dest.getReg(), ResultReg); 3266 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 3267 return; 3268 } 3269 3270 MachineOperand &Src = Inst.getOperand(1); 3271 unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3272 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 3273 3274 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg) 3275 .addImm(31) 3276 .addReg(Src.getReg(), 0, AMDGPU::sub0); 3277 3278 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 3279 .addReg(Src.getReg(), 0, AMDGPU::sub0) 3280 .addImm(AMDGPU::sub0) 3281 .addReg(TmpReg) 3282 .addImm(AMDGPU::sub1); 3283 3284 MRI.replaceRegWith(Dest.getReg(), ResultReg); 3285 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 3286 } 3287 3288 void SIInstrInfo::addUsersToMoveToVALUWorklist( 3289 unsigned DstReg, 3290 MachineRegisterInfo &MRI, 3291 SmallVectorImpl<MachineInstr *> &Worklist) const { 3292 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg), 3293 E = MRI.use_end(); I != E; ++I) { 3294 MachineInstr &UseMI = *I->getParent(); 3295 if (!canReadVGPR(UseMI, I.getOperandNo())) { 3296 Worklist.push_back(&UseMI); 3297 } 3298 } 3299 } 3300 3301 void SIInstrInfo::addSCCDefUsersToVALUWorklist( 3302 MachineInstr &SCCDefInst, SmallVectorImpl<MachineInstr *> &Worklist) const { 3303 // This assumes that all the users of SCC are in the same block 3304 // as the SCC def. 3305 for (MachineInstr &MI : 3306 llvm::make_range(MachineBasicBlock::iterator(SCCDefInst), 3307 SCCDefInst.getParent()->end())) { 3308 // Exit if we find another SCC def. 3309 if (MI.findRegisterDefOperandIdx(AMDGPU::SCC) != -1) 3310 return; 3311 3312 if (MI.findRegisterUseOperandIdx(AMDGPU::SCC) != -1) 3313 Worklist.push_back(&MI); 3314 } 3315 } 3316 3317 const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass( 3318 const MachineInstr &Inst) const { 3319 const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0); 3320 3321 switch (Inst.getOpcode()) { 3322 // For target instructions, getOpRegClass just returns the virtual register 3323 // class associated with the operand, so we need to find an equivalent VGPR 3324 // register class in order to move the instruction to the VALU. 3325 case AMDGPU::COPY: 3326 case AMDGPU::PHI: 3327 case AMDGPU::REG_SEQUENCE: 3328 case AMDGPU::INSERT_SUBREG: 3329 if (RI.hasVGPRs(NewDstRC)) 3330 return nullptr; 3331 3332 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 3333 if (!NewDstRC) 3334 return nullptr; 3335 return NewDstRC; 3336 default: 3337 return NewDstRC; 3338 } 3339 } 3340 3341 // Find the one SGPR operand we are allowed to use. 3342 unsigned SIInstrInfo::findUsedSGPR(const MachineInstr &MI, 3343 int OpIndices[3]) const { 3344 const MCInstrDesc &Desc = MI.getDesc(); 3345 3346 // Find the one SGPR operand we are allowed to use. 3347 // 3348 // First we need to consider the instruction's operand requirements before 3349 // legalizing. Some operands are required to be SGPRs, such as implicit uses 3350 // of VCC, but we are still bound by the constant bus requirement to only use 3351 // one. 3352 // 3353 // If the operand's class is an SGPR, we can never move it. 3354 3355 unsigned SGPRReg = findImplicitSGPRRead(MI); 3356 if (SGPRReg != AMDGPU::NoRegister) 3357 return SGPRReg; 3358 3359 unsigned UsedSGPRs[3] = { AMDGPU::NoRegister }; 3360 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 3361 3362 for (unsigned i = 0; i < 3; ++i) { 3363 int Idx = OpIndices[i]; 3364 if (Idx == -1) 3365 break; 3366 3367 const MachineOperand &MO = MI.getOperand(Idx); 3368 if (!MO.isReg()) 3369 continue; 3370 3371 // Is this operand statically required to be an SGPR based on the operand 3372 // constraints? 3373 const TargetRegisterClass *OpRC = RI.getRegClass(Desc.OpInfo[Idx].RegClass); 3374 bool IsRequiredSGPR = RI.isSGPRClass(OpRC); 3375 if (IsRequiredSGPR) 3376 return MO.getReg(); 3377 3378 // If this could be a VGPR or an SGPR, Check the dynamic register class. 3379 unsigned Reg = MO.getReg(); 3380 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg); 3381 if (RI.isSGPRClass(RegRC)) 3382 UsedSGPRs[i] = Reg; 3383 } 3384 3385 // We don't have a required SGPR operand, so we have a bit more freedom in 3386 // selecting operands to move. 3387 3388 // Try to select the most used SGPR. If an SGPR is equal to one of the 3389 // others, we choose that. 3390 // 3391 // e.g. 3392 // V_FMA_F32 v0, s0, s0, s0 -> No moves 3393 // V_FMA_F32 v0, s0, s1, s0 -> Move s1 3394 3395 // TODO: If some of the operands are 64-bit SGPRs and some 32, we should 3396 // prefer those. 3397 3398 if (UsedSGPRs[0] != AMDGPU::NoRegister) { 3399 if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2]) 3400 SGPRReg = UsedSGPRs[0]; 3401 } 3402 3403 if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) { 3404 if (UsedSGPRs[1] == UsedSGPRs[2]) 3405 SGPRReg = UsedSGPRs[1]; 3406 } 3407 3408 return SGPRReg; 3409 } 3410 3411 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI, 3412 unsigned OperandName) const { 3413 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName); 3414 if (Idx == -1) 3415 return nullptr; 3416 3417 return &MI.getOperand(Idx); 3418 } 3419 3420 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const { 3421 uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT; 3422 if (ST.isAmdHsaOS()) { 3423 RsrcDataFormat |= (1ULL << 56); 3424 3425 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 3426 // Set MTYPE = 2 3427 RsrcDataFormat |= (2ULL << 59); 3428 } 3429 3430 return RsrcDataFormat; 3431 } 3432 3433 uint64_t SIInstrInfo::getScratchRsrcWords23() const { 3434 uint64_t Rsrc23 = getDefaultRsrcDataFormat() | 3435 AMDGPU::RSRC_TID_ENABLE | 3436 0xffffffff; // Size; 3437 3438 uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize()) - 1; 3439 3440 Rsrc23 |= (EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT) | 3441 // IndexStride = 64 3442 (UINT64_C(3) << AMDGPU::RSRC_INDEX_STRIDE_SHIFT); 3443 3444 // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17]. 3445 // Clear them unless we want a huge stride. 3446 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 3447 Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT; 3448 3449 return Rsrc23; 3450 } 3451 3452 bool SIInstrInfo::isLowLatencyInstruction(const MachineInstr &MI) const { 3453 unsigned Opc = MI.getOpcode(); 3454 3455 return isSMRD(Opc); 3456 } 3457 3458 bool SIInstrInfo::isHighLatencyInstruction(const MachineInstr &MI) const { 3459 unsigned Opc = MI.getOpcode(); 3460 3461 return isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc); 3462 } 3463 3464 unsigned SIInstrInfo::isStackAccess(const MachineInstr &MI, 3465 int &FrameIndex) const { 3466 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 3467 if (!Addr || !Addr->isFI()) 3468 return AMDGPU::NoRegister; 3469 3470 assert(!MI.memoperands_empty() && 3471 (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS); 3472 3473 FrameIndex = Addr->getIndex(); 3474 return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg(); 3475 } 3476 3477 unsigned SIInstrInfo::isSGPRStackAccess(const MachineInstr &MI, 3478 int &FrameIndex) const { 3479 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr); 3480 assert(Addr && Addr->isFI()); 3481 FrameIndex = Addr->getIndex(); 3482 return getNamedOperand(MI, AMDGPU::OpName::data)->getReg(); 3483 } 3484 3485 unsigned SIInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 3486 int &FrameIndex) const { 3487 3488 if (!MI.mayLoad()) 3489 return AMDGPU::NoRegister; 3490 3491 if (isMUBUF(MI) || isVGPRSpill(MI)) 3492 return isStackAccess(MI, FrameIndex); 3493 3494 if (isSGPRSpill(MI)) 3495 return isSGPRStackAccess(MI, FrameIndex); 3496 3497 return AMDGPU::NoRegister; 3498 } 3499 3500 unsigned SIInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 3501 int &FrameIndex) const { 3502 if (!MI.mayStore()) 3503 return AMDGPU::NoRegister; 3504 3505 if (isMUBUF(MI) || isVGPRSpill(MI)) 3506 return isStackAccess(MI, FrameIndex); 3507 3508 if (isSGPRSpill(MI)) 3509 return isSGPRStackAccess(MI, FrameIndex); 3510 3511 return AMDGPU::NoRegister; 3512 } 3513 3514 unsigned SIInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 3515 unsigned Opc = MI.getOpcode(); 3516 const MCInstrDesc &Desc = getMCOpcodeFromPseudo(Opc); 3517 unsigned DescSize = Desc.getSize(); 3518 3519 // If we have a definitive size, we can use it. Otherwise we need to inspect 3520 // the operands to know the size. 3521 // 3522 // FIXME: Instructions that have a base 32-bit encoding report their size as 3523 // 4, even though they are really 8 bytes if they have a literal operand. 3524 if (DescSize != 0 && DescSize != 4) 3525 return DescSize; 3526 3527 if (Opc == AMDGPU::WAVE_BARRIER) 3528 return 0; 3529 3530 // 4-byte instructions may have a 32-bit literal encoded after them. Check 3531 // operands that coud ever be literals. 3532 if (isVALU(MI) || isSALU(MI)) { 3533 if (isFixedSize(MI)) { 3534 assert(DescSize == 4); 3535 return DescSize; 3536 } 3537 3538 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 3539 if (Src0Idx == -1) 3540 return 4; // No operands. 3541 3542 if (isLiteralConstantLike(MI.getOperand(Src0Idx), getOpSize(MI, Src0Idx))) 3543 return 8; 3544 3545 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 3546 if (Src1Idx == -1) 3547 return 4; 3548 3549 if (isLiteralConstantLike(MI.getOperand(Src1Idx), getOpSize(MI, Src1Idx))) 3550 return 8; 3551 3552 return 4; 3553 } 3554 3555 if (DescSize == 4) 3556 return 4; 3557 3558 switch (Opc) { 3559 case AMDGPU::SI_MASK_BRANCH: 3560 case TargetOpcode::IMPLICIT_DEF: 3561 case TargetOpcode::KILL: 3562 case TargetOpcode::DBG_VALUE: 3563 case TargetOpcode::BUNDLE: 3564 case TargetOpcode::EH_LABEL: 3565 return 0; 3566 case TargetOpcode::INLINEASM: { 3567 const MachineFunction *MF = MI.getParent()->getParent(); 3568 const char *AsmStr = MI.getOperand(0).getSymbolName(); 3569 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); 3570 } 3571 default: 3572 llvm_unreachable("unable to find instruction size"); 3573 } 3574 } 3575 3576 bool SIInstrInfo::mayAccessFlatAddressSpace(const MachineInstr &MI) const { 3577 if (!isFLAT(MI)) 3578 return false; 3579 3580 if (MI.memoperands_empty()) 3581 return true; 3582 3583 for (const MachineMemOperand *MMO : MI.memoperands()) { 3584 if (MMO->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS) 3585 return true; 3586 } 3587 return false; 3588 } 3589 3590 ArrayRef<std::pair<int, const char *>> 3591 SIInstrInfo::getSerializableTargetIndices() const { 3592 static const std::pair<int, const char *> TargetIndices[] = { 3593 {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"}, 3594 {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"}, 3595 {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"}, 3596 {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"}, 3597 {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}}; 3598 return makeArrayRef(TargetIndices); 3599 } 3600 3601 /// This is used by the post-RA scheduler (SchedulePostRAList.cpp). The 3602 /// post-RA version of misched uses CreateTargetMIHazardRecognizer. 3603 ScheduleHazardRecognizer * 3604 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 3605 const ScheduleDAG *DAG) const { 3606 return new GCNHazardRecognizer(DAG->MF); 3607 } 3608 3609 /// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer 3610 /// pass. 3611 ScheduleHazardRecognizer * 3612 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const { 3613 return new GCNHazardRecognizer(MF); 3614 } 3615