1 //===- SIInstrInfo.cpp - SI Instruction Information ----------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// SI Implementation of TargetInstrInfo. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "SIInstrInfo.h" 15 #include "AMDGPU.h" 16 #include "AMDGPUInstrInfo.h" 17 #include "GCNHazardRecognizer.h" 18 #include "GCNSubtarget.h" 19 #include "SIMachineFunctionInfo.h" 20 #include "Utils/AMDGPUBaseInfo.h" 21 #include "llvm/Analysis/ValueTracking.h" 22 #include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h" 23 #include "llvm/CodeGen/LiveIntervals.h" 24 #include "llvm/CodeGen/LiveVariables.h" 25 #include "llvm/CodeGen/MachineDominators.h" 26 #include "llvm/CodeGen/MachineFrameInfo.h" 27 #include "llvm/CodeGen/MachineScheduler.h" 28 #include "llvm/CodeGen/RegisterScavenging.h" 29 #include "llvm/CodeGen/ScheduleDAG.h" 30 #include "llvm/IR/DiagnosticInfo.h" 31 #include "llvm/IR/IntrinsicsAMDGPU.h" 32 #include "llvm/MC/MCContext.h" 33 #include "llvm/Support/CommandLine.h" 34 #include "llvm/Target/TargetMachine.h" 35 36 using namespace llvm; 37 38 #define DEBUG_TYPE "si-instr-info" 39 40 #define GET_INSTRINFO_CTOR_DTOR 41 #include "AMDGPUGenInstrInfo.inc" 42 43 namespace llvm { 44 namespace AMDGPU { 45 #define GET_D16ImageDimIntrinsics_IMPL 46 #define GET_ImageDimIntrinsicTable_IMPL 47 #define GET_RsrcIntrinsics_IMPL 48 #include "AMDGPUGenSearchableTables.inc" 49 } 50 } 51 52 53 // Must be at least 4 to be able to branch over minimum unconditional branch 54 // code. This is only for making it possible to write reasonably small tests for 55 // long branches. 56 static cl::opt<unsigned> 57 BranchOffsetBits("amdgpu-s-branch-bits", cl::ReallyHidden, cl::init(16), 58 cl::desc("Restrict range of branch instructions (DEBUG)")); 59 60 static cl::opt<bool> Fix16BitCopies( 61 "amdgpu-fix-16-bit-physreg-copies", 62 cl::desc("Fix copies between 32 and 16 bit registers by extending to 32 bit"), 63 cl::init(true), 64 cl::ReallyHidden); 65 66 SIInstrInfo::SIInstrInfo(const GCNSubtarget &ST) 67 : AMDGPUGenInstrInfo(AMDGPU::ADJCALLSTACKUP, AMDGPU::ADJCALLSTACKDOWN), 68 RI(ST), ST(ST) { 69 SchedModel.init(&ST); 70 } 71 72 //===----------------------------------------------------------------------===// 73 // TargetInstrInfo callbacks 74 //===----------------------------------------------------------------------===// 75 76 static unsigned getNumOperandsNoGlue(SDNode *Node) { 77 unsigned N = Node->getNumOperands(); 78 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue) 79 --N; 80 return N; 81 } 82 83 /// Returns true if both nodes have the same value for the given 84 /// operand \p Op, or if both nodes do not have this operand. 85 static bool nodesHaveSameOperandValue(SDNode *N0, SDNode* N1, unsigned OpName) { 86 unsigned Opc0 = N0->getMachineOpcode(); 87 unsigned Opc1 = N1->getMachineOpcode(); 88 89 int Op0Idx = AMDGPU::getNamedOperandIdx(Opc0, OpName); 90 int Op1Idx = AMDGPU::getNamedOperandIdx(Opc1, OpName); 91 92 if (Op0Idx == -1 && Op1Idx == -1) 93 return true; 94 95 96 if ((Op0Idx == -1 && Op1Idx != -1) || 97 (Op1Idx == -1 && Op0Idx != -1)) 98 return false; 99 100 // getNamedOperandIdx returns the index for the MachineInstr's operands, 101 // which includes the result as the first operand. We are indexing into the 102 // MachineSDNode's operands, so we need to skip the result operand to get 103 // the real index. 104 --Op0Idx; 105 --Op1Idx; 106 107 return N0->getOperand(Op0Idx) == N1->getOperand(Op1Idx); 108 } 109 110 static bool canRemat(const MachineInstr &MI) { 111 112 if (SIInstrInfo::isVOP1(MI) || SIInstrInfo::isVOP2(MI) || 113 SIInstrInfo::isVOP3(MI) || SIInstrInfo::isSDWA(MI) || 114 SIInstrInfo::isSALU(MI)) 115 return true; 116 117 if (SIInstrInfo::isSMRD(MI)) { 118 return !MI.memoperands_empty() && 119 llvm::all_of(MI.memoperands(), [](const MachineMemOperand *MMO) { 120 return MMO->isLoad() && MMO->isInvariant(); 121 }); 122 } 123 124 return false; 125 } 126 127 bool SIInstrInfo::isReallyTriviallyReMaterializable( 128 const MachineInstr &MI) const { 129 130 if (canRemat(MI)) { 131 // Normally VALU use of exec would block the rematerialization, but that 132 // is OK in this case to have an implicit exec read as all VALU do. 133 // We really want all of the generic logic for this except for this. 134 135 // Another potential implicit use is mode register. The core logic of 136 // the RA will not attempt rematerialization if mode is set anywhere 137 // in the function, otherwise it is safe since mode is not changed. 138 139 // There is difference to generic method which does not allow 140 // rematerialization if there are virtual register uses. We allow this, 141 // therefore this method includes SOP instructions as well. 142 if (!MI.hasImplicitDef() && 143 MI.getNumImplicitOperands() == MI.getDesc().implicit_uses().size() && 144 !MI.mayRaiseFPException()) 145 return true; 146 } 147 148 return TargetInstrInfo::isReallyTriviallyReMaterializable(MI); 149 } 150 151 // Returns true if the scalar result of a VALU instruction depends on exec. 152 static bool resultDependsOnExec(const MachineInstr &MI) { 153 // Ignore comparisons which are only used masked with exec. 154 // This allows some hoisting/sinking of VALU comparisons. 155 if (MI.isCompare()) { 156 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 157 Register DstReg = MI.getOperand(0).getReg(); 158 if (!DstReg.isVirtual()) 159 return true; 160 for (MachineInstr &Use : MRI.use_nodbg_instructions(DstReg)) { 161 switch (Use.getOpcode()) { 162 case AMDGPU::S_AND_SAVEEXEC_B32: 163 case AMDGPU::S_AND_SAVEEXEC_B64: 164 break; 165 case AMDGPU::S_AND_B32: 166 case AMDGPU::S_AND_B64: 167 if (!Use.readsRegister(AMDGPU::EXEC)) 168 return true; 169 break; 170 default: 171 return true; 172 } 173 } 174 return false; 175 } 176 177 switch (MI.getOpcode()) { 178 default: 179 break; 180 case AMDGPU::V_READFIRSTLANE_B32: 181 return true; 182 } 183 184 return false; 185 } 186 187 bool SIInstrInfo::isIgnorableUse(const MachineOperand &MO) const { 188 // Any implicit use of exec by VALU is not a real register read. 189 return MO.getReg() == AMDGPU::EXEC && MO.isImplicit() && 190 isVALU(*MO.getParent()) && !resultDependsOnExec(*MO.getParent()); 191 } 192 193 bool SIInstrInfo::isSafeToSink(MachineInstr &MI, 194 MachineBasicBlock *SuccToSinkTo, 195 MachineCycleInfo *CI) const { 196 // Allow sinking if MI edits lane mask (divergent i1 in sgpr). 197 if (MI.getOpcode() == AMDGPU::SI_IF_BREAK) 198 return true; 199 200 MachineRegisterInfo &MRI = MI.getMF()->getRegInfo(); 201 // Check if sinking of MI would create temporal divergent use. 202 for (auto Op : MI.uses()) { 203 if (Op.isReg() && Op.getReg().isVirtual() && 204 RI.isSGPRClass(MRI.getRegClass(Op.getReg()))) { 205 MachineInstr *SgprDef = MRI.getVRegDef(Op.getReg()); 206 207 // SgprDef defined inside cycle 208 MachineCycle *FromCycle = CI->getCycle(SgprDef->getParent()); 209 if (FromCycle == nullptr) 210 continue; 211 212 MachineCycle *ToCycle = CI->getCycle(SuccToSinkTo); 213 // Check if there is a FromCycle that contains SgprDef's basic block but 214 // does not contain SuccToSinkTo and also has divergent exit condition. 215 while (FromCycle && !FromCycle->contains(ToCycle)) { 216 // After structurize-cfg, there should be exactly one cycle exit. 217 SmallVector<MachineBasicBlock *, 1> ExitBlocks; 218 FromCycle->getExitBlocks(ExitBlocks); 219 assert(ExitBlocks.size() == 1); 220 assert(ExitBlocks[0]->getSinglePredecessor()); 221 222 // FromCycle has divergent exit condition. 223 if (hasDivergentBranch(ExitBlocks[0]->getSinglePredecessor())) { 224 return false; 225 } 226 227 FromCycle = FromCycle->getParentCycle(); 228 } 229 } 230 } 231 232 return true; 233 } 234 235 bool SIInstrInfo::areLoadsFromSameBasePtr(SDNode *Load0, SDNode *Load1, 236 int64_t &Offset0, 237 int64_t &Offset1) const { 238 if (!Load0->isMachineOpcode() || !Load1->isMachineOpcode()) 239 return false; 240 241 unsigned Opc0 = Load0->getMachineOpcode(); 242 unsigned Opc1 = Load1->getMachineOpcode(); 243 244 // Make sure both are actually loads. 245 if (!get(Opc0).mayLoad() || !get(Opc1).mayLoad()) 246 return false; 247 248 // A mayLoad instruction without a def is not a load. Likely a prefetch. 249 if (!get(Opc0).getNumDefs() || !get(Opc1).getNumDefs()) 250 return false; 251 252 if (isDS(Opc0) && isDS(Opc1)) { 253 254 // FIXME: Handle this case: 255 if (getNumOperandsNoGlue(Load0) != getNumOperandsNoGlue(Load1)) 256 return false; 257 258 // Check base reg. 259 if (Load0->getOperand(0) != Load1->getOperand(0)) 260 return false; 261 262 // Skip read2 / write2 variants for simplicity. 263 // TODO: We should report true if the used offsets are adjacent (excluded 264 // st64 versions). 265 int Offset0Idx = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset); 266 int Offset1Idx = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset); 267 if (Offset0Idx == -1 || Offset1Idx == -1) 268 return false; 269 270 // XXX - be careful of dataless loads 271 // getNamedOperandIdx returns the index for MachineInstrs. Since they 272 // include the output in the operand list, but SDNodes don't, we need to 273 // subtract the index by one. 274 Offset0Idx -= get(Opc0).NumDefs; 275 Offset1Idx -= get(Opc1).NumDefs; 276 Offset0 = cast<ConstantSDNode>(Load0->getOperand(Offset0Idx))->getZExtValue(); 277 Offset1 = cast<ConstantSDNode>(Load1->getOperand(Offset1Idx))->getZExtValue(); 278 return true; 279 } 280 281 if (isSMRD(Opc0) && isSMRD(Opc1)) { 282 // Skip time and cache invalidation instructions. 283 if (!AMDGPU::hasNamedOperand(Opc0, AMDGPU::OpName::sbase) || 284 !AMDGPU::hasNamedOperand(Opc1, AMDGPU::OpName::sbase)) 285 return false; 286 287 unsigned NumOps = getNumOperandsNoGlue(Load0); 288 if (NumOps != getNumOperandsNoGlue(Load1)) 289 return false; 290 291 // Check base reg. 292 if (Load0->getOperand(0) != Load1->getOperand(0)) 293 return false; 294 295 // Match register offsets, if both register and immediate offsets present. 296 assert(NumOps == 4 || NumOps == 5); 297 if (NumOps == 5 && Load0->getOperand(1) != Load1->getOperand(1)) 298 return false; 299 300 const ConstantSDNode *Load0Offset = 301 dyn_cast<ConstantSDNode>(Load0->getOperand(NumOps - 3)); 302 const ConstantSDNode *Load1Offset = 303 dyn_cast<ConstantSDNode>(Load1->getOperand(NumOps - 3)); 304 305 if (!Load0Offset || !Load1Offset) 306 return false; 307 308 Offset0 = Load0Offset->getZExtValue(); 309 Offset1 = Load1Offset->getZExtValue(); 310 return true; 311 } 312 313 // MUBUF and MTBUF can access the same addresses. 314 if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1))) { 315 316 // MUBUF and MTBUF have vaddr at different indices. 317 if (!nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::soffset) || 318 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::vaddr) || 319 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::srsrc)) 320 return false; 321 322 int OffIdx0 = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset); 323 int OffIdx1 = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset); 324 325 if (OffIdx0 == -1 || OffIdx1 == -1) 326 return false; 327 328 // getNamedOperandIdx returns the index for MachineInstrs. Since they 329 // include the output in the operand list, but SDNodes don't, we need to 330 // subtract the index by one. 331 OffIdx0 -= get(Opc0).NumDefs; 332 OffIdx1 -= get(Opc1).NumDefs; 333 334 SDValue Off0 = Load0->getOperand(OffIdx0); 335 SDValue Off1 = Load1->getOperand(OffIdx1); 336 337 // The offset might be a FrameIndexSDNode. 338 if (!isa<ConstantSDNode>(Off0) || !isa<ConstantSDNode>(Off1)) 339 return false; 340 341 Offset0 = cast<ConstantSDNode>(Off0)->getZExtValue(); 342 Offset1 = cast<ConstantSDNode>(Off1)->getZExtValue(); 343 return true; 344 } 345 346 return false; 347 } 348 349 static bool isStride64(unsigned Opc) { 350 switch (Opc) { 351 case AMDGPU::DS_READ2ST64_B32: 352 case AMDGPU::DS_READ2ST64_B64: 353 case AMDGPU::DS_WRITE2ST64_B32: 354 case AMDGPU::DS_WRITE2ST64_B64: 355 return true; 356 default: 357 return false; 358 } 359 } 360 361 bool SIInstrInfo::getMemOperandsWithOffsetWidth( 362 const MachineInstr &LdSt, SmallVectorImpl<const MachineOperand *> &BaseOps, 363 int64_t &Offset, bool &OffsetIsScalable, unsigned &Width, 364 const TargetRegisterInfo *TRI) const { 365 if (!LdSt.mayLoadOrStore()) 366 return false; 367 368 unsigned Opc = LdSt.getOpcode(); 369 OffsetIsScalable = false; 370 const MachineOperand *BaseOp, *OffsetOp; 371 int DataOpIdx; 372 373 if (isDS(LdSt)) { 374 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::addr); 375 OffsetOp = getNamedOperand(LdSt, AMDGPU::OpName::offset); 376 if (OffsetOp) { 377 // Normal, single offset LDS instruction. 378 if (!BaseOp) { 379 // DS_CONSUME/DS_APPEND use M0 for the base address. 380 // TODO: find the implicit use operand for M0 and use that as BaseOp? 381 return false; 382 } 383 BaseOps.push_back(BaseOp); 384 Offset = OffsetOp->getImm(); 385 // Get appropriate operand, and compute width accordingly. 386 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 387 if (DataOpIdx == -1) 388 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0); 389 Width = getOpSize(LdSt, DataOpIdx); 390 } else { 391 // The 2 offset instructions use offset0 and offset1 instead. We can treat 392 // these as a load with a single offset if the 2 offsets are consecutive. 393 // We will use this for some partially aligned loads. 394 const MachineOperand *Offset0Op = 395 getNamedOperand(LdSt, AMDGPU::OpName::offset0); 396 const MachineOperand *Offset1Op = 397 getNamedOperand(LdSt, AMDGPU::OpName::offset1); 398 399 unsigned Offset0 = Offset0Op->getImm() & 0xff; 400 unsigned Offset1 = Offset1Op->getImm() & 0xff; 401 if (Offset0 + 1 != Offset1) 402 return false; 403 404 // Each of these offsets is in element sized units, so we need to convert 405 // to bytes of the individual reads. 406 407 unsigned EltSize; 408 if (LdSt.mayLoad()) 409 EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, 0)) / 16; 410 else { 411 assert(LdSt.mayStore()); 412 int Data0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0); 413 EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, Data0Idx)) / 8; 414 } 415 416 if (isStride64(Opc)) 417 EltSize *= 64; 418 419 BaseOps.push_back(BaseOp); 420 Offset = EltSize * Offset0; 421 // Get appropriate operand(s), and compute width accordingly. 422 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 423 if (DataOpIdx == -1) { 424 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0); 425 Width = getOpSize(LdSt, DataOpIdx); 426 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data1); 427 Width += getOpSize(LdSt, DataOpIdx); 428 } else { 429 Width = getOpSize(LdSt, DataOpIdx); 430 } 431 } 432 return true; 433 } 434 435 if (isMUBUF(LdSt) || isMTBUF(LdSt)) { 436 const MachineOperand *RSrc = getNamedOperand(LdSt, AMDGPU::OpName::srsrc); 437 if (!RSrc) // e.g. BUFFER_WBINVL1_VOL 438 return false; 439 BaseOps.push_back(RSrc); 440 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::vaddr); 441 if (BaseOp && !BaseOp->isFI()) 442 BaseOps.push_back(BaseOp); 443 const MachineOperand *OffsetImm = 444 getNamedOperand(LdSt, AMDGPU::OpName::offset); 445 Offset = OffsetImm->getImm(); 446 const MachineOperand *SOffset = 447 getNamedOperand(LdSt, AMDGPU::OpName::soffset); 448 if (SOffset) { 449 if (SOffset->isReg()) 450 BaseOps.push_back(SOffset); 451 else 452 Offset += SOffset->getImm(); 453 } 454 // Get appropriate operand, and compute width accordingly. 455 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 456 if (DataOpIdx == -1) 457 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata); 458 if (DataOpIdx == -1) // LDS DMA 459 return false; 460 Width = getOpSize(LdSt, DataOpIdx); 461 return true; 462 } 463 464 if (isMIMG(LdSt)) { 465 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc); 466 BaseOps.push_back(&LdSt.getOperand(SRsrcIdx)); 467 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0); 468 if (VAddr0Idx >= 0) { 469 // GFX10 possible NSA encoding. 470 for (int I = VAddr0Idx; I < SRsrcIdx; ++I) 471 BaseOps.push_back(&LdSt.getOperand(I)); 472 } else { 473 BaseOps.push_back(getNamedOperand(LdSt, AMDGPU::OpName::vaddr)); 474 } 475 Offset = 0; 476 // Get appropriate operand, and compute width accordingly. 477 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata); 478 Width = getOpSize(LdSt, DataOpIdx); 479 return true; 480 } 481 482 if (isSMRD(LdSt)) { 483 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::sbase); 484 if (!BaseOp) // e.g. S_MEMTIME 485 return false; 486 BaseOps.push_back(BaseOp); 487 OffsetOp = getNamedOperand(LdSt, AMDGPU::OpName::offset); 488 Offset = OffsetOp ? OffsetOp->getImm() : 0; 489 // Get appropriate operand, and compute width accordingly. 490 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::sdst); 491 if (DataOpIdx == -1) 492 return false; 493 Width = getOpSize(LdSt, DataOpIdx); 494 return true; 495 } 496 497 if (isFLAT(LdSt)) { 498 // Instructions have either vaddr or saddr or both or none. 499 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::vaddr); 500 if (BaseOp) 501 BaseOps.push_back(BaseOp); 502 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::saddr); 503 if (BaseOp) 504 BaseOps.push_back(BaseOp); 505 Offset = getNamedOperand(LdSt, AMDGPU::OpName::offset)->getImm(); 506 // Get appropriate operand, and compute width accordingly. 507 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 508 if (DataOpIdx == -1) 509 DataOpIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdata); 510 if (DataOpIdx == -1) // LDS DMA 511 return false; 512 Width = getOpSize(LdSt, DataOpIdx); 513 return true; 514 } 515 516 return false; 517 } 518 519 static bool memOpsHaveSameBasePtr(const MachineInstr &MI1, 520 ArrayRef<const MachineOperand *> BaseOps1, 521 const MachineInstr &MI2, 522 ArrayRef<const MachineOperand *> BaseOps2) { 523 // Only examine the first "base" operand of each instruction, on the 524 // assumption that it represents the real base address of the memory access. 525 // Other operands are typically offsets or indices from this base address. 526 if (BaseOps1.front()->isIdenticalTo(*BaseOps2.front())) 527 return true; 528 529 if (!MI1.hasOneMemOperand() || !MI2.hasOneMemOperand()) 530 return false; 531 532 auto MO1 = *MI1.memoperands_begin(); 533 auto MO2 = *MI2.memoperands_begin(); 534 if (MO1->getAddrSpace() != MO2->getAddrSpace()) 535 return false; 536 537 auto Base1 = MO1->getValue(); 538 auto Base2 = MO2->getValue(); 539 if (!Base1 || !Base2) 540 return false; 541 Base1 = getUnderlyingObject(Base1); 542 Base2 = getUnderlyingObject(Base2); 543 544 if (isa<UndefValue>(Base1) || isa<UndefValue>(Base2)) 545 return false; 546 547 return Base1 == Base2; 548 } 549 550 bool SIInstrInfo::shouldClusterMemOps(ArrayRef<const MachineOperand *> BaseOps1, 551 int64_t Offset1, bool OffsetIsScalable1, 552 ArrayRef<const MachineOperand *> BaseOps2, 553 int64_t Offset2, bool OffsetIsScalable2, 554 unsigned ClusterSize, 555 unsigned NumBytes) const { 556 // If the mem ops (to be clustered) do not have the same base ptr, then they 557 // should not be clustered 558 if (!BaseOps1.empty() && !BaseOps2.empty()) { 559 const MachineInstr &FirstLdSt = *BaseOps1.front()->getParent(); 560 const MachineInstr &SecondLdSt = *BaseOps2.front()->getParent(); 561 if (!memOpsHaveSameBasePtr(FirstLdSt, BaseOps1, SecondLdSt, BaseOps2)) 562 return false; 563 } else if (!BaseOps1.empty() || !BaseOps2.empty()) { 564 // If only one base op is empty, they do not have the same base ptr 565 return false; 566 } 567 568 // In order to avoid register pressure, on an average, the number of DWORDS 569 // loaded together by all clustered mem ops should not exceed 8. This is an 570 // empirical value based on certain observations and performance related 571 // experiments. 572 // The good thing about this heuristic is - it avoids clustering of too many 573 // sub-word loads, and also avoids clustering of wide loads. Below is the 574 // brief summary of how the heuristic behaves for various `LoadSize`. 575 // (1) 1 <= LoadSize <= 4: cluster at max 8 mem ops 576 // (2) 5 <= LoadSize <= 8: cluster at max 4 mem ops 577 // (3) 9 <= LoadSize <= 12: cluster at max 2 mem ops 578 // (4) 13 <= LoadSize <= 16: cluster at max 2 mem ops 579 // (5) LoadSize >= 17: do not cluster 580 const unsigned LoadSize = NumBytes / ClusterSize; 581 const unsigned NumDWORDs = ((LoadSize + 3) / 4) * ClusterSize; 582 return NumDWORDs <= 8; 583 } 584 585 // FIXME: This behaves strangely. If, for example, you have 32 load + stores, 586 // the first 16 loads will be interleaved with the stores, and the next 16 will 587 // be clustered as expected. It should really split into 2 16 store batches. 588 // 589 // Loads are clustered until this returns false, rather than trying to schedule 590 // groups of stores. This also means we have to deal with saying different 591 // address space loads should be clustered, and ones which might cause bank 592 // conflicts. 593 // 594 // This might be deprecated so it might not be worth that much effort to fix. 595 bool SIInstrInfo::shouldScheduleLoadsNear(SDNode *Load0, SDNode *Load1, 596 int64_t Offset0, int64_t Offset1, 597 unsigned NumLoads) const { 598 assert(Offset1 > Offset0 && 599 "Second offset should be larger than first offset!"); 600 // If we have less than 16 loads in a row, and the offsets are within 64 601 // bytes, then schedule together. 602 603 // A cacheline is 64 bytes (for global memory). 604 return (NumLoads <= 16 && (Offset1 - Offset0) < 64); 605 } 606 607 static void reportIllegalCopy(const SIInstrInfo *TII, MachineBasicBlock &MBB, 608 MachineBasicBlock::iterator MI, 609 const DebugLoc &DL, MCRegister DestReg, 610 MCRegister SrcReg, bool KillSrc, 611 const char *Msg = "illegal VGPR to SGPR copy") { 612 MachineFunction *MF = MBB.getParent(); 613 DiagnosticInfoUnsupported IllegalCopy(MF->getFunction(), Msg, DL, DS_Error); 614 LLVMContext &C = MF->getFunction().getContext(); 615 C.diagnose(IllegalCopy); 616 617 BuildMI(MBB, MI, DL, TII->get(AMDGPU::SI_ILLEGAL_COPY), DestReg) 618 .addReg(SrcReg, getKillRegState(KillSrc)); 619 } 620 621 /// Handle copying from SGPR to AGPR, or from AGPR to AGPR on GFX908. It is not 622 /// possible to have a direct copy in these cases on GFX908, so an intermediate 623 /// VGPR copy is required. 624 static void indirectCopyToAGPR(const SIInstrInfo &TII, 625 MachineBasicBlock &MBB, 626 MachineBasicBlock::iterator MI, 627 const DebugLoc &DL, MCRegister DestReg, 628 MCRegister SrcReg, bool KillSrc, 629 RegScavenger &RS, bool RegsOverlap, 630 Register ImpDefSuperReg = Register(), 631 Register ImpUseSuperReg = Register()) { 632 assert((TII.getSubtarget().hasMAIInsts() && 633 !TII.getSubtarget().hasGFX90AInsts()) && 634 "Expected GFX908 subtarget."); 635 636 assert((AMDGPU::SReg_32RegClass.contains(SrcReg) || 637 AMDGPU::AGPR_32RegClass.contains(SrcReg)) && 638 "Source register of the copy should be either an SGPR or an AGPR."); 639 640 assert(AMDGPU::AGPR_32RegClass.contains(DestReg) && 641 "Destination register of the copy should be an AGPR."); 642 643 const SIRegisterInfo &RI = TII.getRegisterInfo(); 644 645 // First try to find defining accvgpr_write to avoid temporary registers. 646 // In the case of copies of overlapping AGPRs, we conservatively do not 647 // reuse previous accvgpr_writes. Otherwise, we may incorrectly pick up 648 // an accvgpr_write used for this same copy due to implicit-defs 649 if (!RegsOverlap) { 650 for (auto Def = MI, E = MBB.begin(); Def != E; ) { 651 --Def; 652 653 if (!Def->modifiesRegister(SrcReg, &RI)) 654 continue; 655 656 if (Def->getOpcode() != AMDGPU::V_ACCVGPR_WRITE_B32_e64 || 657 Def->getOperand(0).getReg() != SrcReg) 658 break; 659 660 MachineOperand &DefOp = Def->getOperand(1); 661 assert(DefOp.isReg() || DefOp.isImm()); 662 663 if (DefOp.isReg()) { 664 bool SafeToPropagate = true; 665 // Check that register source operand is not clobbered before MI. 666 // Immediate operands are always safe to propagate. 667 for (auto I = Def; I != MI && SafeToPropagate; ++I) 668 if (I->modifiesRegister(DefOp.getReg(), &RI)) 669 SafeToPropagate = false; 670 671 if (!SafeToPropagate) 672 break; 673 674 DefOp.setIsKill(false); 675 } 676 677 MachineInstrBuilder Builder = 678 BuildMI(MBB, MI, DL, TII.get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg) 679 .add(DefOp); 680 if (ImpDefSuperReg) 681 Builder.addReg(ImpDefSuperReg, RegState::Define | RegState::Implicit); 682 683 if (ImpUseSuperReg) { 684 Builder.addReg(ImpUseSuperReg, 685 getKillRegState(KillSrc) | RegState::Implicit); 686 } 687 688 return; 689 } 690 } 691 692 RS.enterBasicBlockEnd(MBB); 693 RS.backward(std::next(MI)); 694 695 // Ideally we want to have three registers for a long reg_sequence copy 696 // to hide 2 waitstates between v_mov_b32 and accvgpr_write. 697 unsigned MaxVGPRs = RI.getRegPressureLimit(&AMDGPU::VGPR_32RegClass, 698 *MBB.getParent()); 699 700 // Registers in the sequence are allocated contiguously so we can just 701 // use register number to pick one of three round-robin temps. 702 unsigned RegNo = (DestReg - AMDGPU::AGPR0) % 3; 703 Register Tmp = 704 MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getVGPRForAGPRCopy(); 705 assert(MBB.getParent()->getRegInfo().isReserved(Tmp) && 706 "VGPR used for an intermediate copy should have been reserved."); 707 708 // Only loop through if there are any free registers left. We don't want to 709 // spill. 710 while (RegNo--) { 711 Register Tmp2 = RS.scavengeRegisterBackwards(AMDGPU::VGPR_32RegClass, MI, 712 /* RestoreAfter */ false, 0, 713 /* AllowSpill */ false); 714 if (!Tmp2 || RI.getHWRegIndex(Tmp2) >= MaxVGPRs) 715 break; 716 Tmp = Tmp2; 717 RS.setRegUsed(Tmp); 718 } 719 720 // Insert copy to temporary VGPR. 721 unsigned TmpCopyOp = AMDGPU::V_MOV_B32_e32; 722 if (AMDGPU::AGPR_32RegClass.contains(SrcReg)) { 723 TmpCopyOp = AMDGPU::V_ACCVGPR_READ_B32_e64; 724 } else { 725 assert(AMDGPU::SReg_32RegClass.contains(SrcReg)); 726 } 727 728 MachineInstrBuilder UseBuilder = BuildMI(MBB, MI, DL, TII.get(TmpCopyOp), Tmp) 729 .addReg(SrcReg, getKillRegState(KillSrc)); 730 if (ImpUseSuperReg) { 731 UseBuilder.addReg(ImpUseSuperReg, 732 getKillRegState(KillSrc) | RegState::Implicit); 733 } 734 735 MachineInstrBuilder DefBuilder 736 = BuildMI(MBB, MI, DL, TII.get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg) 737 .addReg(Tmp, RegState::Kill); 738 739 if (ImpDefSuperReg) 740 DefBuilder.addReg(ImpDefSuperReg, RegState::Define | RegState::Implicit); 741 } 742 743 static void expandSGPRCopy(const SIInstrInfo &TII, MachineBasicBlock &MBB, 744 MachineBasicBlock::iterator MI, const DebugLoc &DL, 745 MCRegister DestReg, MCRegister SrcReg, bool KillSrc, 746 const TargetRegisterClass *RC, bool Forward) { 747 const SIRegisterInfo &RI = TII.getRegisterInfo(); 748 ArrayRef<int16_t> BaseIndices = RI.getRegSplitParts(RC, 4); 749 MachineBasicBlock::iterator I = MI; 750 MachineInstr *FirstMI = nullptr, *LastMI = nullptr; 751 752 for (unsigned Idx = 0; Idx < BaseIndices.size(); ++Idx) { 753 int16_t SubIdx = BaseIndices[Idx]; 754 Register DestSubReg = RI.getSubReg(DestReg, SubIdx); 755 Register SrcSubReg = RI.getSubReg(SrcReg, SubIdx); 756 assert(DestSubReg && SrcSubReg && "Failed to find subregs!"); 757 unsigned Opcode = AMDGPU::S_MOV_B32; 758 759 // Is SGPR aligned? If so try to combine with next. 760 bool AlignedDest = ((DestSubReg - AMDGPU::SGPR0) % 2) == 0; 761 bool AlignedSrc = ((SrcSubReg - AMDGPU::SGPR0) % 2) == 0; 762 if (AlignedDest && AlignedSrc && (Idx + 1 < BaseIndices.size())) { 763 // Can use SGPR64 copy 764 unsigned Channel = RI.getChannelFromSubReg(SubIdx); 765 SubIdx = RI.getSubRegFromChannel(Channel, 2); 766 DestSubReg = RI.getSubReg(DestReg, SubIdx); 767 SrcSubReg = RI.getSubReg(SrcReg, SubIdx); 768 assert(DestSubReg && SrcSubReg && "Failed to find subregs!"); 769 Opcode = AMDGPU::S_MOV_B64; 770 Idx++; 771 } 772 773 LastMI = BuildMI(MBB, I, DL, TII.get(Opcode), DestSubReg) 774 .addReg(SrcSubReg) 775 .addReg(SrcReg, RegState::Implicit); 776 777 if (!FirstMI) 778 FirstMI = LastMI; 779 780 if (!Forward) 781 I--; 782 } 783 784 assert(FirstMI && LastMI); 785 if (!Forward) 786 std::swap(FirstMI, LastMI); 787 788 FirstMI->addOperand( 789 MachineOperand::CreateReg(DestReg, true /*IsDef*/, true /*IsImp*/)); 790 791 if (KillSrc) 792 LastMI->addRegisterKilled(SrcReg, &RI); 793 } 794 795 void SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 796 MachineBasicBlock::iterator MI, 797 const DebugLoc &DL, MCRegister DestReg, 798 MCRegister SrcReg, bool KillSrc) const { 799 const TargetRegisterClass *RC = RI.getPhysRegBaseClass(DestReg); 800 unsigned Size = RI.getRegSizeInBits(*RC); 801 const TargetRegisterClass *SrcRC = RI.getPhysRegBaseClass(SrcReg); 802 unsigned SrcSize = RI.getRegSizeInBits(*SrcRC); 803 804 // The rest of copyPhysReg assumes Src and Dst size are the same size. 805 // TODO-GFX11_16BIT If all true 16 bit instruction patterns are completed can 806 // we remove Fix16BitCopies and this code block? 807 if (Fix16BitCopies) { 808 if (((Size == 16) != (SrcSize == 16))) { 809 // Non-VGPR Src and Dst will later be expanded back to 32 bits. 810 assert(ST.hasTrue16BitInsts()); 811 MCRegister &RegToFix = (Size == 32) ? DestReg : SrcReg; 812 MCRegister SubReg = RI.getSubReg(RegToFix, AMDGPU::lo16); 813 RegToFix = SubReg; 814 815 if (DestReg == SrcReg) { 816 // Identity copy. Insert empty bundle since ExpandPostRA expects an 817 // instruction here. 818 BuildMI(MBB, MI, DL, get(AMDGPU::BUNDLE)); 819 return; 820 } 821 RC = RI.getPhysRegBaseClass(DestReg); 822 Size = RI.getRegSizeInBits(*RC); 823 SrcRC = RI.getPhysRegBaseClass(SrcReg); 824 SrcSize = RI.getRegSizeInBits(*SrcRC); 825 } 826 } 827 828 if (RC == &AMDGPU::VGPR_32RegClass) { 829 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) || 830 AMDGPU::SReg_32RegClass.contains(SrcReg) || 831 AMDGPU::AGPR_32RegClass.contains(SrcReg)); 832 unsigned Opc = AMDGPU::AGPR_32RegClass.contains(SrcReg) ? 833 AMDGPU::V_ACCVGPR_READ_B32_e64 : AMDGPU::V_MOV_B32_e32; 834 BuildMI(MBB, MI, DL, get(Opc), DestReg) 835 .addReg(SrcReg, getKillRegState(KillSrc)); 836 return; 837 } 838 839 if (RC == &AMDGPU::SReg_32_XM0RegClass || 840 RC == &AMDGPU::SReg_32RegClass) { 841 if (SrcReg == AMDGPU::SCC) { 842 BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B32), DestReg) 843 .addImm(1) 844 .addImm(0); 845 return; 846 } 847 848 if (DestReg == AMDGPU::VCC_LO) { 849 if (AMDGPU::SReg_32RegClass.contains(SrcReg)) { 850 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), AMDGPU::VCC_LO) 851 .addReg(SrcReg, getKillRegState(KillSrc)); 852 } else { 853 // FIXME: Hack until VReg_1 removed. 854 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg)); 855 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32)) 856 .addImm(0) 857 .addReg(SrcReg, getKillRegState(KillSrc)); 858 } 859 860 return; 861 } 862 863 if (!AMDGPU::SReg_32RegClass.contains(SrcReg)) { 864 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 865 return; 866 } 867 868 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg) 869 .addReg(SrcReg, getKillRegState(KillSrc)); 870 return; 871 } 872 873 if (RC == &AMDGPU::SReg_64RegClass) { 874 if (SrcReg == AMDGPU::SCC) { 875 BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B64), DestReg) 876 .addImm(1) 877 .addImm(0); 878 return; 879 } 880 881 if (DestReg == AMDGPU::VCC) { 882 if (AMDGPU::SReg_64RegClass.contains(SrcReg)) { 883 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), AMDGPU::VCC) 884 .addReg(SrcReg, getKillRegState(KillSrc)); 885 } else { 886 // FIXME: Hack until VReg_1 removed. 887 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg)); 888 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32)) 889 .addImm(0) 890 .addReg(SrcReg, getKillRegState(KillSrc)); 891 } 892 893 return; 894 } 895 896 if (!AMDGPU::SReg_64RegClass.contains(SrcReg)) { 897 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 898 return; 899 } 900 901 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg) 902 .addReg(SrcReg, getKillRegState(KillSrc)); 903 return; 904 } 905 906 if (DestReg == AMDGPU::SCC) { 907 // Copying 64-bit or 32-bit sources to SCC barely makes sense, 908 // but SelectionDAG emits such copies for i1 sources. 909 if (AMDGPU::SReg_64RegClass.contains(SrcReg)) { 910 // This copy can only be produced by patterns 911 // with explicit SCC, which are known to be enabled 912 // only for subtargets with S_CMP_LG_U64 present. 913 assert(ST.hasScalarCompareEq64()); 914 BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U64)) 915 .addReg(SrcReg, getKillRegState(KillSrc)) 916 .addImm(0); 917 } else { 918 assert(AMDGPU::SReg_32RegClass.contains(SrcReg)); 919 BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U32)) 920 .addReg(SrcReg, getKillRegState(KillSrc)) 921 .addImm(0); 922 } 923 924 return; 925 } 926 927 if (RC == &AMDGPU::AGPR_32RegClass) { 928 if (AMDGPU::VGPR_32RegClass.contains(SrcReg) || 929 (ST.hasGFX90AInsts() && AMDGPU::SReg_32RegClass.contains(SrcReg))) { 930 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_WRITE_B32_e64), DestReg) 931 .addReg(SrcReg, getKillRegState(KillSrc)); 932 return; 933 } 934 935 if (AMDGPU::AGPR_32RegClass.contains(SrcReg) && ST.hasGFX90AInsts()) { 936 BuildMI(MBB, MI, DL, get(AMDGPU::V_ACCVGPR_MOV_B32), DestReg) 937 .addReg(SrcReg, getKillRegState(KillSrc)); 938 return; 939 } 940 941 // FIXME: Pass should maintain scavenger to avoid scan through the block on 942 // every AGPR spill. 943 RegScavenger RS; 944 const bool Overlap = RI.regsOverlap(SrcReg, DestReg); 945 indirectCopyToAGPR(*this, MBB, MI, DL, DestReg, SrcReg, KillSrc, RS, Overlap); 946 return; 947 } 948 949 if (Size == 16) { 950 assert(AMDGPU::VGPR_16RegClass.contains(SrcReg) || 951 AMDGPU::SReg_LO16RegClass.contains(SrcReg) || 952 AMDGPU::AGPR_LO16RegClass.contains(SrcReg)); 953 954 bool IsSGPRDst = AMDGPU::SReg_LO16RegClass.contains(DestReg); 955 bool IsSGPRSrc = AMDGPU::SReg_LO16RegClass.contains(SrcReg); 956 bool IsAGPRDst = AMDGPU::AGPR_LO16RegClass.contains(DestReg); 957 bool IsAGPRSrc = AMDGPU::AGPR_LO16RegClass.contains(SrcReg); 958 bool DstLow = AMDGPU::VGPR_LO16RegClass.contains(DestReg) || 959 AMDGPU::SReg_LO16RegClass.contains(DestReg) || 960 AMDGPU::AGPR_LO16RegClass.contains(DestReg); 961 bool SrcLow = AMDGPU::VGPR_LO16RegClass.contains(SrcReg) || 962 AMDGPU::SReg_LO16RegClass.contains(SrcReg) || 963 AMDGPU::AGPR_LO16RegClass.contains(SrcReg); 964 MCRegister NewDestReg = RI.get32BitRegister(DestReg); 965 MCRegister NewSrcReg = RI.get32BitRegister(SrcReg); 966 967 if (IsSGPRDst) { 968 if (!IsSGPRSrc) { 969 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 970 return; 971 } 972 973 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), NewDestReg) 974 .addReg(NewSrcReg, getKillRegState(KillSrc)); 975 return; 976 } 977 978 if (IsAGPRDst || IsAGPRSrc) { 979 if (!DstLow || !SrcLow) { 980 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc, 981 "Cannot use hi16 subreg with an AGPR!"); 982 } 983 984 copyPhysReg(MBB, MI, DL, NewDestReg, NewSrcReg, KillSrc); 985 return; 986 } 987 988 if (ST.hasTrue16BitInsts()) { 989 if (IsSGPRSrc) { 990 assert(SrcLow); 991 SrcReg = NewSrcReg; 992 } 993 // Use the smaller instruction encoding if possible. 994 if (AMDGPU::VGPR_16_Lo128RegClass.contains(DestReg) && 995 (IsSGPRSrc || AMDGPU::VGPR_16_Lo128RegClass.contains(SrcReg))) { 996 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B16_t16_e32), DestReg) 997 .addReg(SrcReg); 998 } else { 999 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B16_t16_e64), DestReg) 1000 .addImm(0) // src0_modifiers 1001 .addReg(SrcReg) 1002 .addImm(0); // op_sel 1003 } 1004 return; 1005 } 1006 1007 if (IsSGPRSrc && !ST.hasSDWAScalar()) { 1008 if (!DstLow || !SrcLow) { 1009 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc, 1010 "Cannot use hi16 subreg on VI!"); 1011 } 1012 1013 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), NewDestReg) 1014 .addReg(NewSrcReg, getKillRegState(KillSrc)); 1015 return; 1016 } 1017 1018 auto MIB = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_sdwa), NewDestReg) 1019 .addImm(0) // src0_modifiers 1020 .addReg(NewSrcReg) 1021 .addImm(0) // clamp 1022 .addImm(DstLow ? AMDGPU::SDWA::SdwaSel::WORD_0 1023 : AMDGPU::SDWA::SdwaSel::WORD_1) 1024 .addImm(AMDGPU::SDWA::DstUnused::UNUSED_PRESERVE) 1025 .addImm(SrcLow ? AMDGPU::SDWA::SdwaSel::WORD_0 1026 : AMDGPU::SDWA::SdwaSel::WORD_1) 1027 .addReg(NewDestReg, RegState::Implicit | RegState::Undef); 1028 // First implicit operand is $exec. 1029 MIB->tieOperands(0, MIB->getNumOperands() - 1); 1030 return; 1031 } 1032 1033 if (RC == RI.getVGPR64Class() && (SrcRC == RC || RI.isSGPRClass(SrcRC))) { 1034 if (ST.hasMovB64()) { 1035 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_e32), DestReg) 1036 .addReg(SrcReg, getKillRegState(KillSrc)); 1037 return; 1038 } 1039 if (ST.hasPkMovB32()) { 1040 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), DestReg) 1041 .addImm(SISrcMods::OP_SEL_1) 1042 .addReg(SrcReg) 1043 .addImm(SISrcMods::OP_SEL_0 | SISrcMods::OP_SEL_1) 1044 .addReg(SrcReg) 1045 .addImm(0) // op_sel_lo 1046 .addImm(0) // op_sel_hi 1047 .addImm(0) // neg_lo 1048 .addImm(0) // neg_hi 1049 .addImm(0) // clamp 1050 .addReg(SrcReg, getKillRegState(KillSrc) | RegState::Implicit); 1051 return; 1052 } 1053 } 1054 1055 const bool Forward = RI.getHWRegIndex(DestReg) <= RI.getHWRegIndex(SrcReg); 1056 if (RI.isSGPRClass(RC)) { 1057 if (!RI.isSGPRClass(SrcRC)) { 1058 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 1059 return; 1060 } 1061 const bool CanKillSuperReg = KillSrc && !RI.regsOverlap(SrcReg, DestReg); 1062 expandSGPRCopy(*this, MBB, MI, DL, DestReg, SrcReg, CanKillSuperReg, RC, 1063 Forward); 1064 return; 1065 } 1066 1067 unsigned EltSize = 4; 1068 unsigned Opcode = AMDGPU::V_MOV_B32_e32; 1069 if (RI.isAGPRClass(RC)) { 1070 if (ST.hasGFX90AInsts() && RI.isAGPRClass(SrcRC)) 1071 Opcode = AMDGPU::V_ACCVGPR_MOV_B32; 1072 else if (RI.hasVGPRs(SrcRC) || 1073 (ST.hasGFX90AInsts() && RI.isSGPRClass(SrcRC))) 1074 Opcode = AMDGPU::V_ACCVGPR_WRITE_B32_e64; 1075 else 1076 Opcode = AMDGPU::INSTRUCTION_LIST_END; 1077 } else if (RI.hasVGPRs(RC) && RI.isAGPRClass(SrcRC)) { 1078 Opcode = AMDGPU::V_ACCVGPR_READ_B32_e64; 1079 } else if ((Size % 64 == 0) && RI.hasVGPRs(RC) && 1080 (RI.isProperlyAlignedRC(*RC) && 1081 (SrcRC == RC || RI.isSGPRClass(SrcRC)))) { 1082 // TODO: In 96-bit case, could do a 64-bit mov and then a 32-bit mov. 1083 if (ST.hasMovB64()) { 1084 Opcode = AMDGPU::V_MOV_B64_e32; 1085 EltSize = 8; 1086 } else if (ST.hasPkMovB32()) { 1087 Opcode = AMDGPU::V_PK_MOV_B32; 1088 EltSize = 8; 1089 } 1090 } 1091 1092 // For the cases where we need an intermediate instruction/temporary register 1093 // (destination is an AGPR), we need a scavenger. 1094 // 1095 // FIXME: The pass should maintain this for us so we don't have to re-scan the 1096 // whole block for every handled copy. 1097 std::unique_ptr<RegScavenger> RS; 1098 if (Opcode == AMDGPU::INSTRUCTION_LIST_END) 1099 RS.reset(new RegScavenger()); 1100 1101 ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RC, EltSize); 1102 1103 // If there is an overlap, we can't kill the super-register on the last 1104 // instruction, since it will also kill the components made live by this def. 1105 const bool Overlap = RI.regsOverlap(SrcReg, DestReg); 1106 const bool CanKillSuperReg = KillSrc && !Overlap; 1107 1108 for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) { 1109 unsigned SubIdx; 1110 if (Forward) 1111 SubIdx = SubIndices[Idx]; 1112 else 1113 SubIdx = SubIndices[SubIndices.size() - Idx - 1]; 1114 Register DestSubReg = RI.getSubReg(DestReg, SubIdx); 1115 Register SrcSubReg = RI.getSubReg(SrcReg, SubIdx); 1116 assert(DestSubReg && SrcSubReg && "Failed to find subregs!"); 1117 1118 bool IsFirstSubreg = Idx == 0; 1119 bool UseKill = CanKillSuperReg && Idx == SubIndices.size() - 1; 1120 1121 if (Opcode == AMDGPU::INSTRUCTION_LIST_END) { 1122 Register ImpDefSuper = IsFirstSubreg ? Register(DestReg) : Register(); 1123 Register ImpUseSuper = SrcReg; 1124 indirectCopyToAGPR(*this, MBB, MI, DL, DestSubReg, SrcSubReg, UseKill, 1125 *RS, Overlap, ImpDefSuper, ImpUseSuper); 1126 } else if (Opcode == AMDGPU::V_PK_MOV_B32) { 1127 MachineInstrBuilder MIB = 1128 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), DestSubReg) 1129 .addImm(SISrcMods::OP_SEL_1) 1130 .addReg(SrcSubReg) 1131 .addImm(SISrcMods::OP_SEL_0 | SISrcMods::OP_SEL_1) 1132 .addReg(SrcSubReg) 1133 .addImm(0) // op_sel_lo 1134 .addImm(0) // op_sel_hi 1135 .addImm(0) // neg_lo 1136 .addImm(0) // neg_hi 1137 .addImm(0) // clamp 1138 .addReg(SrcReg, getKillRegState(UseKill) | RegState::Implicit); 1139 if (IsFirstSubreg) 1140 MIB.addReg(DestReg, RegState::Define | RegState::Implicit); 1141 } else { 1142 MachineInstrBuilder Builder = 1143 BuildMI(MBB, MI, DL, get(Opcode), DestSubReg).addReg(SrcSubReg); 1144 if (IsFirstSubreg) 1145 Builder.addReg(DestReg, RegState::Define | RegState::Implicit); 1146 1147 Builder.addReg(SrcReg, getKillRegState(UseKill) | RegState::Implicit); 1148 } 1149 } 1150 } 1151 1152 int SIInstrInfo::commuteOpcode(unsigned Opcode) const { 1153 int NewOpc; 1154 1155 // Try to map original to commuted opcode 1156 NewOpc = AMDGPU::getCommuteRev(Opcode); 1157 if (NewOpc != -1) 1158 // Check if the commuted (REV) opcode exists on the target. 1159 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1; 1160 1161 // Try to map commuted to original opcode 1162 NewOpc = AMDGPU::getCommuteOrig(Opcode); 1163 if (NewOpc != -1) 1164 // Check if the original (non-REV) opcode exists on the target. 1165 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1; 1166 1167 return Opcode; 1168 } 1169 1170 void SIInstrInfo::materializeImmediate(MachineBasicBlock &MBB, 1171 MachineBasicBlock::iterator MI, 1172 const DebugLoc &DL, Register DestReg, 1173 int64_t Value) const { 1174 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1175 const TargetRegisterClass *RegClass = MRI.getRegClass(DestReg); 1176 if (RegClass == &AMDGPU::SReg_32RegClass || 1177 RegClass == &AMDGPU::SGPR_32RegClass || 1178 RegClass == &AMDGPU::SReg_32_XM0RegClass || 1179 RegClass == &AMDGPU::SReg_32_XM0_XEXECRegClass) { 1180 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg) 1181 .addImm(Value); 1182 return; 1183 } 1184 1185 if (RegClass == &AMDGPU::SReg_64RegClass || 1186 RegClass == &AMDGPU::SGPR_64RegClass || 1187 RegClass == &AMDGPU::SReg_64_XEXECRegClass) { 1188 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg) 1189 .addImm(Value); 1190 return; 1191 } 1192 1193 if (RegClass == &AMDGPU::VGPR_32RegClass) { 1194 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg) 1195 .addImm(Value); 1196 return; 1197 } 1198 if (RegClass->hasSuperClassEq(&AMDGPU::VReg_64RegClass)) { 1199 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO), DestReg) 1200 .addImm(Value); 1201 return; 1202 } 1203 1204 unsigned EltSize = 4; 1205 unsigned Opcode = AMDGPU::V_MOV_B32_e32; 1206 if (RI.isSGPRClass(RegClass)) { 1207 if (RI.getRegSizeInBits(*RegClass) > 32) { 1208 Opcode = AMDGPU::S_MOV_B64; 1209 EltSize = 8; 1210 } else { 1211 Opcode = AMDGPU::S_MOV_B32; 1212 EltSize = 4; 1213 } 1214 } 1215 1216 ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RegClass, EltSize); 1217 for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) { 1218 int64_t IdxValue = Idx == 0 ? Value : 0; 1219 1220 MachineInstrBuilder Builder = BuildMI(MBB, MI, DL, 1221 get(Opcode), RI.getSubReg(DestReg, SubIndices[Idx])); 1222 Builder.addImm(IdxValue); 1223 } 1224 } 1225 1226 const TargetRegisterClass * 1227 SIInstrInfo::getPreferredSelectRegClass(unsigned Size) const { 1228 return &AMDGPU::VGPR_32RegClass; 1229 } 1230 1231 void SIInstrInfo::insertVectorSelect(MachineBasicBlock &MBB, 1232 MachineBasicBlock::iterator I, 1233 const DebugLoc &DL, Register DstReg, 1234 ArrayRef<MachineOperand> Cond, 1235 Register TrueReg, 1236 Register FalseReg) const { 1237 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1238 const TargetRegisterClass *BoolXExecRC = 1239 RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 1240 assert(MRI.getRegClass(DstReg) == &AMDGPU::VGPR_32RegClass && 1241 "Not a VGPR32 reg"); 1242 1243 if (Cond.size() == 1) { 1244 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1245 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg) 1246 .add(Cond[0]); 1247 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1248 .addImm(0) 1249 .addReg(FalseReg) 1250 .addImm(0) 1251 .addReg(TrueReg) 1252 .addReg(SReg); 1253 } else if (Cond.size() == 2) { 1254 assert(Cond[0].isImm() && "Cond[0] is not an immediate"); 1255 switch (Cond[0].getImm()) { 1256 case SIInstrInfo::SCC_TRUE: { 1257 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1258 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32 1259 : AMDGPU::S_CSELECT_B64), SReg) 1260 .addImm(1) 1261 .addImm(0); 1262 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1263 .addImm(0) 1264 .addReg(FalseReg) 1265 .addImm(0) 1266 .addReg(TrueReg) 1267 .addReg(SReg); 1268 break; 1269 } 1270 case SIInstrInfo::SCC_FALSE: { 1271 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1272 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32 1273 : AMDGPU::S_CSELECT_B64), SReg) 1274 .addImm(0) 1275 .addImm(1); 1276 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1277 .addImm(0) 1278 .addReg(FalseReg) 1279 .addImm(0) 1280 .addReg(TrueReg) 1281 .addReg(SReg); 1282 break; 1283 } 1284 case SIInstrInfo::VCCNZ: { 1285 MachineOperand RegOp = Cond[1]; 1286 RegOp.setImplicit(false); 1287 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1288 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg) 1289 .add(RegOp); 1290 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1291 .addImm(0) 1292 .addReg(FalseReg) 1293 .addImm(0) 1294 .addReg(TrueReg) 1295 .addReg(SReg); 1296 break; 1297 } 1298 case SIInstrInfo::VCCZ: { 1299 MachineOperand RegOp = Cond[1]; 1300 RegOp.setImplicit(false); 1301 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1302 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg) 1303 .add(RegOp); 1304 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1305 .addImm(0) 1306 .addReg(TrueReg) 1307 .addImm(0) 1308 .addReg(FalseReg) 1309 .addReg(SReg); 1310 break; 1311 } 1312 case SIInstrInfo::EXECNZ: { 1313 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1314 Register SReg2 = MRI.createVirtualRegister(RI.getBoolRC()); 1315 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32 1316 : AMDGPU::S_OR_SAVEEXEC_B64), SReg2) 1317 .addImm(0); 1318 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32 1319 : AMDGPU::S_CSELECT_B64), SReg) 1320 .addImm(1) 1321 .addImm(0); 1322 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1323 .addImm(0) 1324 .addReg(FalseReg) 1325 .addImm(0) 1326 .addReg(TrueReg) 1327 .addReg(SReg); 1328 break; 1329 } 1330 case SIInstrInfo::EXECZ: { 1331 Register SReg = MRI.createVirtualRegister(BoolXExecRC); 1332 Register SReg2 = MRI.createVirtualRegister(RI.getBoolRC()); 1333 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32 1334 : AMDGPU::S_OR_SAVEEXEC_B64), SReg2) 1335 .addImm(0); 1336 BuildMI(MBB, I, DL, get(ST.isWave32() ? AMDGPU::S_CSELECT_B32 1337 : AMDGPU::S_CSELECT_B64), SReg) 1338 .addImm(0) 1339 .addImm(1); 1340 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 1341 .addImm(0) 1342 .addReg(FalseReg) 1343 .addImm(0) 1344 .addReg(TrueReg) 1345 .addReg(SReg); 1346 llvm_unreachable("Unhandled branch predicate EXECZ"); 1347 break; 1348 } 1349 default: 1350 llvm_unreachable("invalid branch predicate"); 1351 } 1352 } else { 1353 llvm_unreachable("Can only handle Cond size 1 or 2"); 1354 } 1355 } 1356 1357 Register SIInstrInfo::insertEQ(MachineBasicBlock *MBB, 1358 MachineBasicBlock::iterator I, 1359 const DebugLoc &DL, 1360 Register SrcReg, int Value) const { 1361 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 1362 Register Reg = MRI.createVirtualRegister(RI.getBoolRC()); 1363 BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_EQ_I32_e64), Reg) 1364 .addImm(Value) 1365 .addReg(SrcReg); 1366 1367 return Reg; 1368 } 1369 1370 Register SIInstrInfo::insertNE(MachineBasicBlock *MBB, 1371 MachineBasicBlock::iterator I, 1372 const DebugLoc &DL, 1373 Register SrcReg, int Value) const { 1374 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 1375 Register Reg = MRI.createVirtualRegister(RI.getBoolRC()); 1376 BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_NE_I32_e64), Reg) 1377 .addImm(Value) 1378 .addReg(SrcReg); 1379 1380 return Reg; 1381 } 1382 1383 unsigned SIInstrInfo::getMovOpcode(const TargetRegisterClass *DstRC) const { 1384 1385 if (RI.isAGPRClass(DstRC)) 1386 return AMDGPU::COPY; 1387 if (RI.getRegSizeInBits(*DstRC) == 16) { 1388 // Assume hi bits are unneeded. Only _e64 true16 instructions are legal 1389 // before RA. 1390 return RI.isSGPRClass(DstRC) ? AMDGPU::COPY : AMDGPU::V_MOV_B16_t16_e64; 1391 } else if (RI.getRegSizeInBits(*DstRC) == 32) { 1392 return RI.isSGPRClass(DstRC) ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32; 1393 } else if (RI.getRegSizeInBits(*DstRC) == 64 && RI.isSGPRClass(DstRC)) { 1394 return AMDGPU::S_MOV_B64; 1395 } else if (RI.getRegSizeInBits(*DstRC) == 64 && !RI.isSGPRClass(DstRC)) { 1396 return AMDGPU::V_MOV_B64_PSEUDO; 1397 } 1398 return AMDGPU::COPY; 1399 } 1400 1401 const MCInstrDesc & 1402 SIInstrInfo::getIndirectGPRIDXPseudo(unsigned VecSize, 1403 bool IsIndirectSrc) const { 1404 if (IsIndirectSrc) { 1405 if (VecSize <= 32) // 4 bytes 1406 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V1); 1407 if (VecSize <= 64) // 8 bytes 1408 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V2); 1409 if (VecSize <= 96) // 12 bytes 1410 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V3); 1411 if (VecSize <= 128) // 16 bytes 1412 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V4); 1413 if (VecSize <= 160) // 20 bytes 1414 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V5); 1415 if (VecSize <= 256) // 32 bytes 1416 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V8); 1417 if (VecSize <= 288) // 36 bytes 1418 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V9); 1419 if (VecSize <= 320) // 40 bytes 1420 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V10); 1421 if (VecSize <= 352) // 44 bytes 1422 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V11); 1423 if (VecSize <= 384) // 48 bytes 1424 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V12); 1425 if (VecSize <= 512) // 64 bytes 1426 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V16); 1427 if (VecSize <= 1024) // 128 bytes 1428 return get(AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V32); 1429 1430 llvm_unreachable("unsupported size for IndirectRegReadGPRIDX pseudos"); 1431 } 1432 1433 if (VecSize <= 32) // 4 bytes 1434 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V1); 1435 if (VecSize <= 64) // 8 bytes 1436 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V2); 1437 if (VecSize <= 96) // 12 bytes 1438 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V3); 1439 if (VecSize <= 128) // 16 bytes 1440 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V4); 1441 if (VecSize <= 160) // 20 bytes 1442 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V5); 1443 if (VecSize <= 256) // 32 bytes 1444 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V8); 1445 if (VecSize <= 288) // 36 bytes 1446 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V9); 1447 if (VecSize <= 320) // 40 bytes 1448 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V10); 1449 if (VecSize <= 352) // 44 bytes 1450 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V11); 1451 if (VecSize <= 384) // 48 bytes 1452 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V12); 1453 if (VecSize <= 512) // 64 bytes 1454 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V16); 1455 if (VecSize <= 1024) // 128 bytes 1456 return get(AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V32); 1457 1458 llvm_unreachable("unsupported size for IndirectRegWriteGPRIDX pseudos"); 1459 } 1460 1461 static unsigned getIndirectVGPRWriteMovRelPseudoOpc(unsigned VecSize) { 1462 if (VecSize <= 32) // 4 bytes 1463 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V1; 1464 if (VecSize <= 64) // 8 bytes 1465 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V2; 1466 if (VecSize <= 96) // 12 bytes 1467 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V3; 1468 if (VecSize <= 128) // 16 bytes 1469 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V4; 1470 if (VecSize <= 160) // 20 bytes 1471 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V5; 1472 if (VecSize <= 256) // 32 bytes 1473 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V8; 1474 if (VecSize <= 288) // 36 bytes 1475 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V9; 1476 if (VecSize <= 320) // 40 bytes 1477 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V10; 1478 if (VecSize <= 352) // 44 bytes 1479 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V11; 1480 if (VecSize <= 384) // 48 bytes 1481 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V12; 1482 if (VecSize <= 512) // 64 bytes 1483 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V16; 1484 if (VecSize <= 1024) // 128 bytes 1485 return AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V32; 1486 1487 llvm_unreachable("unsupported size for IndirectRegWrite pseudos"); 1488 } 1489 1490 static unsigned getIndirectSGPRWriteMovRelPseudo32(unsigned VecSize) { 1491 if (VecSize <= 32) // 4 bytes 1492 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V1; 1493 if (VecSize <= 64) // 8 bytes 1494 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V2; 1495 if (VecSize <= 96) // 12 bytes 1496 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V3; 1497 if (VecSize <= 128) // 16 bytes 1498 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V4; 1499 if (VecSize <= 160) // 20 bytes 1500 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V5; 1501 if (VecSize <= 256) // 32 bytes 1502 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V8; 1503 if (VecSize <= 288) // 36 bytes 1504 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V9; 1505 if (VecSize <= 320) // 40 bytes 1506 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V10; 1507 if (VecSize <= 352) // 44 bytes 1508 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V11; 1509 if (VecSize <= 384) // 48 bytes 1510 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V12; 1511 if (VecSize <= 512) // 64 bytes 1512 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V16; 1513 if (VecSize <= 1024) // 128 bytes 1514 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V32; 1515 1516 llvm_unreachable("unsupported size for IndirectRegWrite pseudos"); 1517 } 1518 1519 static unsigned getIndirectSGPRWriteMovRelPseudo64(unsigned VecSize) { 1520 if (VecSize <= 64) // 8 bytes 1521 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V1; 1522 if (VecSize <= 128) // 16 bytes 1523 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V2; 1524 if (VecSize <= 256) // 32 bytes 1525 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V4; 1526 if (VecSize <= 512) // 64 bytes 1527 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V8; 1528 if (VecSize <= 1024) // 128 bytes 1529 return AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V16; 1530 1531 llvm_unreachable("unsupported size for IndirectRegWrite pseudos"); 1532 } 1533 1534 const MCInstrDesc & 1535 SIInstrInfo::getIndirectRegWriteMovRelPseudo(unsigned VecSize, unsigned EltSize, 1536 bool IsSGPR) const { 1537 if (IsSGPR) { 1538 switch (EltSize) { 1539 case 32: 1540 return get(getIndirectSGPRWriteMovRelPseudo32(VecSize)); 1541 case 64: 1542 return get(getIndirectSGPRWriteMovRelPseudo64(VecSize)); 1543 default: 1544 llvm_unreachable("invalid reg indexing elt size"); 1545 } 1546 } 1547 1548 assert(EltSize == 32 && "invalid reg indexing elt size"); 1549 return get(getIndirectVGPRWriteMovRelPseudoOpc(VecSize)); 1550 } 1551 1552 static unsigned getSGPRSpillSaveOpcode(unsigned Size) { 1553 switch (Size) { 1554 case 4: 1555 return AMDGPU::SI_SPILL_S32_SAVE; 1556 case 8: 1557 return AMDGPU::SI_SPILL_S64_SAVE; 1558 case 12: 1559 return AMDGPU::SI_SPILL_S96_SAVE; 1560 case 16: 1561 return AMDGPU::SI_SPILL_S128_SAVE; 1562 case 20: 1563 return AMDGPU::SI_SPILL_S160_SAVE; 1564 case 24: 1565 return AMDGPU::SI_SPILL_S192_SAVE; 1566 case 28: 1567 return AMDGPU::SI_SPILL_S224_SAVE; 1568 case 32: 1569 return AMDGPU::SI_SPILL_S256_SAVE; 1570 case 36: 1571 return AMDGPU::SI_SPILL_S288_SAVE; 1572 case 40: 1573 return AMDGPU::SI_SPILL_S320_SAVE; 1574 case 44: 1575 return AMDGPU::SI_SPILL_S352_SAVE; 1576 case 48: 1577 return AMDGPU::SI_SPILL_S384_SAVE; 1578 case 64: 1579 return AMDGPU::SI_SPILL_S512_SAVE; 1580 case 128: 1581 return AMDGPU::SI_SPILL_S1024_SAVE; 1582 default: 1583 llvm_unreachable("unknown register size"); 1584 } 1585 } 1586 1587 static unsigned getVGPRSpillSaveOpcode(unsigned Size) { 1588 switch (Size) { 1589 case 4: 1590 return AMDGPU::SI_SPILL_V32_SAVE; 1591 case 8: 1592 return AMDGPU::SI_SPILL_V64_SAVE; 1593 case 12: 1594 return AMDGPU::SI_SPILL_V96_SAVE; 1595 case 16: 1596 return AMDGPU::SI_SPILL_V128_SAVE; 1597 case 20: 1598 return AMDGPU::SI_SPILL_V160_SAVE; 1599 case 24: 1600 return AMDGPU::SI_SPILL_V192_SAVE; 1601 case 28: 1602 return AMDGPU::SI_SPILL_V224_SAVE; 1603 case 32: 1604 return AMDGPU::SI_SPILL_V256_SAVE; 1605 case 36: 1606 return AMDGPU::SI_SPILL_V288_SAVE; 1607 case 40: 1608 return AMDGPU::SI_SPILL_V320_SAVE; 1609 case 44: 1610 return AMDGPU::SI_SPILL_V352_SAVE; 1611 case 48: 1612 return AMDGPU::SI_SPILL_V384_SAVE; 1613 case 64: 1614 return AMDGPU::SI_SPILL_V512_SAVE; 1615 case 128: 1616 return AMDGPU::SI_SPILL_V1024_SAVE; 1617 default: 1618 llvm_unreachable("unknown register size"); 1619 } 1620 } 1621 1622 static unsigned getAGPRSpillSaveOpcode(unsigned Size) { 1623 switch (Size) { 1624 case 4: 1625 return AMDGPU::SI_SPILL_A32_SAVE; 1626 case 8: 1627 return AMDGPU::SI_SPILL_A64_SAVE; 1628 case 12: 1629 return AMDGPU::SI_SPILL_A96_SAVE; 1630 case 16: 1631 return AMDGPU::SI_SPILL_A128_SAVE; 1632 case 20: 1633 return AMDGPU::SI_SPILL_A160_SAVE; 1634 case 24: 1635 return AMDGPU::SI_SPILL_A192_SAVE; 1636 case 28: 1637 return AMDGPU::SI_SPILL_A224_SAVE; 1638 case 32: 1639 return AMDGPU::SI_SPILL_A256_SAVE; 1640 case 36: 1641 return AMDGPU::SI_SPILL_A288_SAVE; 1642 case 40: 1643 return AMDGPU::SI_SPILL_A320_SAVE; 1644 case 44: 1645 return AMDGPU::SI_SPILL_A352_SAVE; 1646 case 48: 1647 return AMDGPU::SI_SPILL_A384_SAVE; 1648 case 64: 1649 return AMDGPU::SI_SPILL_A512_SAVE; 1650 case 128: 1651 return AMDGPU::SI_SPILL_A1024_SAVE; 1652 default: 1653 llvm_unreachable("unknown register size"); 1654 } 1655 } 1656 1657 static unsigned getAVSpillSaveOpcode(unsigned Size) { 1658 switch (Size) { 1659 case 4: 1660 return AMDGPU::SI_SPILL_AV32_SAVE; 1661 case 8: 1662 return AMDGPU::SI_SPILL_AV64_SAVE; 1663 case 12: 1664 return AMDGPU::SI_SPILL_AV96_SAVE; 1665 case 16: 1666 return AMDGPU::SI_SPILL_AV128_SAVE; 1667 case 20: 1668 return AMDGPU::SI_SPILL_AV160_SAVE; 1669 case 24: 1670 return AMDGPU::SI_SPILL_AV192_SAVE; 1671 case 28: 1672 return AMDGPU::SI_SPILL_AV224_SAVE; 1673 case 32: 1674 return AMDGPU::SI_SPILL_AV256_SAVE; 1675 case 36: 1676 return AMDGPU::SI_SPILL_AV288_SAVE; 1677 case 40: 1678 return AMDGPU::SI_SPILL_AV320_SAVE; 1679 case 44: 1680 return AMDGPU::SI_SPILL_AV352_SAVE; 1681 case 48: 1682 return AMDGPU::SI_SPILL_AV384_SAVE; 1683 case 64: 1684 return AMDGPU::SI_SPILL_AV512_SAVE; 1685 case 128: 1686 return AMDGPU::SI_SPILL_AV1024_SAVE; 1687 default: 1688 llvm_unreachable("unknown register size"); 1689 } 1690 } 1691 1692 static unsigned getWWMRegSpillSaveOpcode(unsigned Size, 1693 bool IsVectorSuperClass) { 1694 // Currently, there is only 32-bit WWM register spills needed. 1695 if (Size != 4) 1696 llvm_unreachable("unknown wwm register spill size"); 1697 1698 if (IsVectorSuperClass) 1699 return AMDGPU::SI_SPILL_WWM_AV32_SAVE; 1700 1701 return AMDGPU::SI_SPILL_WWM_V32_SAVE; 1702 } 1703 1704 static unsigned getVectorRegSpillSaveOpcode(Register Reg, 1705 const TargetRegisterClass *RC, 1706 unsigned Size, 1707 const SIRegisterInfo &TRI, 1708 const SIMachineFunctionInfo &MFI) { 1709 bool IsVectorSuperClass = TRI.isVectorSuperClass(RC); 1710 1711 // Choose the right opcode if spilling a WWM register. 1712 if (MFI.checkFlag(Reg, AMDGPU::VirtRegFlag::WWM_REG)) 1713 return getWWMRegSpillSaveOpcode(Size, IsVectorSuperClass); 1714 1715 if (IsVectorSuperClass) 1716 return getAVSpillSaveOpcode(Size); 1717 1718 return TRI.isAGPRClass(RC) ? getAGPRSpillSaveOpcode(Size) 1719 : getVGPRSpillSaveOpcode(Size); 1720 } 1721 1722 void SIInstrInfo::storeRegToStackSlot( 1723 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, Register SrcReg, 1724 bool isKill, int FrameIndex, const TargetRegisterClass *RC, 1725 const TargetRegisterInfo *TRI, Register VReg) const { 1726 MachineFunction *MF = MBB.getParent(); 1727 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 1728 MachineFrameInfo &FrameInfo = MF->getFrameInfo(); 1729 const DebugLoc &DL = MBB.findDebugLoc(MI); 1730 1731 MachinePointerInfo PtrInfo 1732 = MachinePointerInfo::getFixedStack(*MF, FrameIndex); 1733 MachineMemOperand *MMO = MF->getMachineMemOperand( 1734 PtrInfo, MachineMemOperand::MOStore, FrameInfo.getObjectSize(FrameIndex), 1735 FrameInfo.getObjectAlign(FrameIndex)); 1736 unsigned SpillSize = TRI->getSpillSize(*RC); 1737 1738 MachineRegisterInfo &MRI = MF->getRegInfo(); 1739 if (RI.isSGPRClass(RC)) { 1740 MFI->setHasSpilledSGPRs(); 1741 assert(SrcReg != AMDGPU::M0 && "m0 should not be spilled"); 1742 assert(SrcReg != AMDGPU::EXEC_LO && SrcReg != AMDGPU::EXEC_HI && 1743 SrcReg != AMDGPU::EXEC && "exec should not be spilled"); 1744 1745 // We are only allowed to create one new instruction when spilling 1746 // registers, so we need to use pseudo instruction for spilling SGPRs. 1747 const MCInstrDesc &OpDesc = get(getSGPRSpillSaveOpcode(SpillSize)); 1748 1749 // The SGPR spill/restore instructions only work on number sgprs, so we need 1750 // to make sure we are using the correct register class. 1751 if (SrcReg.isVirtual() && SpillSize == 4) { 1752 MRI.constrainRegClass(SrcReg, &AMDGPU::SReg_32_XM0_XEXECRegClass); 1753 } 1754 1755 BuildMI(MBB, MI, DL, OpDesc) 1756 .addReg(SrcReg, getKillRegState(isKill)) // data 1757 .addFrameIndex(FrameIndex) // addr 1758 .addMemOperand(MMO) 1759 .addReg(MFI->getStackPtrOffsetReg(), RegState::Implicit); 1760 1761 if (RI.spillSGPRToVGPR()) 1762 FrameInfo.setStackID(FrameIndex, TargetStackID::SGPRSpill); 1763 return; 1764 } 1765 1766 unsigned Opcode = getVectorRegSpillSaveOpcode(VReg ? VReg : SrcReg, RC, 1767 SpillSize, RI, *MFI); 1768 MFI->setHasSpilledVGPRs(); 1769 1770 BuildMI(MBB, MI, DL, get(Opcode)) 1771 .addReg(SrcReg, getKillRegState(isKill)) // data 1772 .addFrameIndex(FrameIndex) // addr 1773 .addReg(MFI->getStackPtrOffsetReg()) // scratch_offset 1774 .addImm(0) // offset 1775 .addMemOperand(MMO); 1776 } 1777 1778 static unsigned getSGPRSpillRestoreOpcode(unsigned Size) { 1779 switch (Size) { 1780 case 4: 1781 return AMDGPU::SI_SPILL_S32_RESTORE; 1782 case 8: 1783 return AMDGPU::SI_SPILL_S64_RESTORE; 1784 case 12: 1785 return AMDGPU::SI_SPILL_S96_RESTORE; 1786 case 16: 1787 return AMDGPU::SI_SPILL_S128_RESTORE; 1788 case 20: 1789 return AMDGPU::SI_SPILL_S160_RESTORE; 1790 case 24: 1791 return AMDGPU::SI_SPILL_S192_RESTORE; 1792 case 28: 1793 return AMDGPU::SI_SPILL_S224_RESTORE; 1794 case 32: 1795 return AMDGPU::SI_SPILL_S256_RESTORE; 1796 case 36: 1797 return AMDGPU::SI_SPILL_S288_RESTORE; 1798 case 40: 1799 return AMDGPU::SI_SPILL_S320_RESTORE; 1800 case 44: 1801 return AMDGPU::SI_SPILL_S352_RESTORE; 1802 case 48: 1803 return AMDGPU::SI_SPILL_S384_RESTORE; 1804 case 64: 1805 return AMDGPU::SI_SPILL_S512_RESTORE; 1806 case 128: 1807 return AMDGPU::SI_SPILL_S1024_RESTORE; 1808 default: 1809 llvm_unreachable("unknown register size"); 1810 } 1811 } 1812 1813 static unsigned getVGPRSpillRestoreOpcode(unsigned Size) { 1814 switch (Size) { 1815 case 4: 1816 return AMDGPU::SI_SPILL_V32_RESTORE; 1817 case 8: 1818 return AMDGPU::SI_SPILL_V64_RESTORE; 1819 case 12: 1820 return AMDGPU::SI_SPILL_V96_RESTORE; 1821 case 16: 1822 return AMDGPU::SI_SPILL_V128_RESTORE; 1823 case 20: 1824 return AMDGPU::SI_SPILL_V160_RESTORE; 1825 case 24: 1826 return AMDGPU::SI_SPILL_V192_RESTORE; 1827 case 28: 1828 return AMDGPU::SI_SPILL_V224_RESTORE; 1829 case 32: 1830 return AMDGPU::SI_SPILL_V256_RESTORE; 1831 case 36: 1832 return AMDGPU::SI_SPILL_V288_RESTORE; 1833 case 40: 1834 return AMDGPU::SI_SPILL_V320_RESTORE; 1835 case 44: 1836 return AMDGPU::SI_SPILL_V352_RESTORE; 1837 case 48: 1838 return AMDGPU::SI_SPILL_V384_RESTORE; 1839 case 64: 1840 return AMDGPU::SI_SPILL_V512_RESTORE; 1841 case 128: 1842 return AMDGPU::SI_SPILL_V1024_RESTORE; 1843 default: 1844 llvm_unreachable("unknown register size"); 1845 } 1846 } 1847 1848 static unsigned getAGPRSpillRestoreOpcode(unsigned Size) { 1849 switch (Size) { 1850 case 4: 1851 return AMDGPU::SI_SPILL_A32_RESTORE; 1852 case 8: 1853 return AMDGPU::SI_SPILL_A64_RESTORE; 1854 case 12: 1855 return AMDGPU::SI_SPILL_A96_RESTORE; 1856 case 16: 1857 return AMDGPU::SI_SPILL_A128_RESTORE; 1858 case 20: 1859 return AMDGPU::SI_SPILL_A160_RESTORE; 1860 case 24: 1861 return AMDGPU::SI_SPILL_A192_RESTORE; 1862 case 28: 1863 return AMDGPU::SI_SPILL_A224_RESTORE; 1864 case 32: 1865 return AMDGPU::SI_SPILL_A256_RESTORE; 1866 case 36: 1867 return AMDGPU::SI_SPILL_A288_RESTORE; 1868 case 40: 1869 return AMDGPU::SI_SPILL_A320_RESTORE; 1870 case 44: 1871 return AMDGPU::SI_SPILL_A352_RESTORE; 1872 case 48: 1873 return AMDGPU::SI_SPILL_A384_RESTORE; 1874 case 64: 1875 return AMDGPU::SI_SPILL_A512_RESTORE; 1876 case 128: 1877 return AMDGPU::SI_SPILL_A1024_RESTORE; 1878 default: 1879 llvm_unreachable("unknown register size"); 1880 } 1881 } 1882 1883 static unsigned getAVSpillRestoreOpcode(unsigned Size) { 1884 switch (Size) { 1885 case 4: 1886 return AMDGPU::SI_SPILL_AV32_RESTORE; 1887 case 8: 1888 return AMDGPU::SI_SPILL_AV64_RESTORE; 1889 case 12: 1890 return AMDGPU::SI_SPILL_AV96_RESTORE; 1891 case 16: 1892 return AMDGPU::SI_SPILL_AV128_RESTORE; 1893 case 20: 1894 return AMDGPU::SI_SPILL_AV160_RESTORE; 1895 case 24: 1896 return AMDGPU::SI_SPILL_AV192_RESTORE; 1897 case 28: 1898 return AMDGPU::SI_SPILL_AV224_RESTORE; 1899 case 32: 1900 return AMDGPU::SI_SPILL_AV256_RESTORE; 1901 case 36: 1902 return AMDGPU::SI_SPILL_AV288_RESTORE; 1903 case 40: 1904 return AMDGPU::SI_SPILL_AV320_RESTORE; 1905 case 44: 1906 return AMDGPU::SI_SPILL_AV352_RESTORE; 1907 case 48: 1908 return AMDGPU::SI_SPILL_AV384_RESTORE; 1909 case 64: 1910 return AMDGPU::SI_SPILL_AV512_RESTORE; 1911 case 128: 1912 return AMDGPU::SI_SPILL_AV1024_RESTORE; 1913 default: 1914 llvm_unreachable("unknown register size"); 1915 } 1916 } 1917 1918 static unsigned getWWMRegSpillRestoreOpcode(unsigned Size, 1919 bool IsVectorSuperClass) { 1920 // Currently, there is only 32-bit WWM register spills needed. 1921 if (Size != 4) 1922 llvm_unreachable("unknown wwm register spill size"); 1923 1924 if (IsVectorSuperClass) 1925 return AMDGPU::SI_SPILL_WWM_AV32_RESTORE; 1926 1927 return AMDGPU::SI_SPILL_WWM_V32_RESTORE; 1928 } 1929 1930 static unsigned 1931 getVectorRegSpillRestoreOpcode(Register Reg, const TargetRegisterClass *RC, 1932 unsigned Size, const SIRegisterInfo &TRI, 1933 const SIMachineFunctionInfo &MFI) { 1934 bool IsVectorSuperClass = TRI.isVectorSuperClass(RC); 1935 1936 // Choose the right opcode if restoring a WWM register. 1937 if (MFI.checkFlag(Reg, AMDGPU::VirtRegFlag::WWM_REG)) 1938 return getWWMRegSpillRestoreOpcode(Size, IsVectorSuperClass); 1939 1940 if (IsVectorSuperClass) 1941 return getAVSpillRestoreOpcode(Size); 1942 1943 return TRI.isAGPRClass(RC) ? getAGPRSpillRestoreOpcode(Size) 1944 : getVGPRSpillRestoreOpcode(Size); 1945 } 1946 1947 void SIInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 1948 MachineBasicBlock::iterator MI, 1949 Register DestReg, int FrameIndex, 1950 const TargetRegisterClass *RC, 1951 const TargetRegisterInfo *TRI, 1952 Register VReg) const { 1953 MachineFunction *MF = MBB.getParent(); 1954 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 1955 MachineFrameInfo &FrameInfo = MF->getFrameInfo(); 1956 const DebugLoc &DL = MBB.findDebugLoc(MI); 1957 unsigned SpillSize = TRI->getSpillSize(*RC); 1958 1959 MachinePointerInfo PtrInfo 1960 = MachinePointerInfo::getFixedStack(*MF, FrameIndex); 1961 1962 MachineMemOperand *MMO = MF->getMachineMemOperand( 1963 PtrInfo, MachineMemOperand::MOLoad, FrameInfo.getObjectSize(FrameIndex), 1964 FrameInfo.getObjectAlign(FrameIndex)); 1965 1966 if (RI.isSGPRClass(RC)) { 1967 MFI->setHasSpilledSGPRs(); 1968 assert(DestReg != AMDGPU::M0 && "m0 should not be reloaded into"); 1969 assert(DestReg != AMDGPU::EXEC_LO && DestReg != AMDGPU::EXEC_HI && 1970 DestReg != AMDGPU::EXEC && "exec should not be spilled"); 1971 1972 // FIXME: Maybe this should not include a memoperand because it will be 1973 // lowered to non-memory instructions. 1974 const MCInstrDesc &OpDesc = get(getSGPRSpillRestoreOpcode(SpillSize)); 1975 if (DestReg.isVirtual() && SpillSize == 4) { 1976 MachineRegisterInfo &MRI = MF->getRegInfo(); 1977 MRI.constrainRegClass(DestReg, &AMDGPU::SReg_32_XM0_XEXECRegClass); 1978 } 1979 1980 if (RI.spillSGPRToVGPR()) 1981 FrameInfo.setStackID(FrameIndex, TargetStackID::SGPRSpill); 1982 BuildMI(MBB, MI, DL, OpDesc, DestReg) 1983 .addFrameIndex(FrameIndex) // addr 1984 .addMemOperand(MMO) 1985 .addReg(MFI->getStackPtrOffsetReg(), RegState::Implicit); 1986 1987 return; 1988 } 1989 1990 unsigned Opcode = getVectorRegSpillRestoreOpcode(VReg ? VReg : DestReg, RC, 1991 SpillSize, RI, *MFI); 1992 BuildMI(MBB, MI, DL, get(Opcode), DestReg) 1993 .addFrameIndex(FrameIndex) // vaddr 1994 .addReg(MFI->getStackPtrOffsetReg()) // scratch_offset 1995 .addImm(0) // offset 1996 .addMemOperand(MMO); 1997 } 1998 1999 void SIInstrInfo::insertNoop(MachineBasicBlock &MBB, 2000 MachineBasicBlock::iterator MI) const { 2001 insertNoops(MBB, MI, 1); 2002 } 2003 2004 void SIInstrInfo::insertNoops(MachineBasicBlock &MBB, 2005 MachineBasicBlock::iterator MI, 2006 unsigned Quantity) const { 2007 DebugLoc DL = MBB.findDebugLoc(MI); 2008 while (Quantity > 0) { 2009 unsigned Arg = std::min(Quantity, 8u); 2010 Quantity -= Arg; 2011 BuildMI(MBB, MI, DL, get(AMDGPU::S_NOP)).addImm(Arg - 1); 2012 } 2013 } 2014 2015 void SIInstrInfo::insertReturn(MachineBasicBlock &MBB) const { 2016 auto MF = MBB.getParent(); 2017 SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>(); 2018 2019 assert(Info->isEntryFunction()); 2020 2021 if (MBB.succ_empty()) { 2022 bool HasNoTerminator = MBB.getFirstTerminator() == MBB.end(); 2023 if (HasNoTerminator) { 2024 if (Info->returnsVoid()) { 2025 BuildMI(MBB, MBB.end(), DebugLoc(), get(AMDGPU::S_ENDPGM)).addImm(0); 2026 } else { 2027 BuildMI(MBB, MBB.end(), DebugLoc(), get(AMDGPU::SI_RETURN_TO_EPILOG)); 2028 } 2029 } 2030 } 2031 } 2032 2033 unsigned SIInstrInfo::getNumWaitStates(const MachineInstr &MI) { 2034 switch (MI.getOpcode()) { 2035 default: 2036 if (MI.isMetaInstruction()) 2037 return 0; 2038 return 1; // FIXME: Do wait states equal cycles? 2039 2040 case AMDGPU::S_NOP: 2041 return MI.getOperand(0).getImm() + 1; 2042 // SI_RETURN_TO_EPILOG is a fallthrough to code outside of the function. The 2043 // hazard, even if one exist, won't really be visible. Should we handle it? 2044 } 2045 } 2046 2047 bool SIInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 2048 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 2049 MachineBasicBlock &MBB = *MI.getParent(); 2050 DebugLoc DL = MBB.findDebugLoc(MI); 2051 switch (MI.getOpcode()) { 2052 default: return TargetInstrInfo::expandPostRAPseudo(MI); 2053 case AMDGPU::S_MOV_B64_term: 2054 // This is only a terminator to get the correct spill code placement during 2055 // register allocation. 2056 MI.setDesc(get(AMDGPU::S_MOV_B64)); 2057 break; 2058 2059 case AMDGPU::S_MOV_B32_term: 2060 // This is only a terminator to get the correct spill code placement during 2061 // register allocation. 2062 MI.setDesc(get(AMDGPU::S_MOV_B32)); 2063 break; 2064 2065 case AMDGPU::S_XOR_B64_term: 2066 // This is only a terminator to get the correct spill code placement during 2067 // register allocation. 2068 MI.setDesc(get(AMDGPU::S_XOR_B64)); 2069 break; 2070 2071 case AMDGPU::S_XOR_B32_term: 2072 // This is only a terminator to get the correct spill code placement during 2073 // register allocation. 2074 MI.setDesc(get(AMDGPU::S_XOR_B32)); 2075 break; 2076 case AMDGPU::S_OR_B64_term: 2077 // This is only a terminator to get the correct spill code placement during 2078 // register allocation. 2079 MI.setDesc(get(AMDGPU::S_OR_B64)); 2080 break; 2081 case AMDGPU::S_OR_B32_term: 2082 // This is only a terminator to get the correct spill code placement during 2083 // register allocation. 2084 MI.setDesc(get(AMDGPU::S_OR_B32)); 2085 break; 2086 2087 case AMDGPU::S_ANDN2_B64_term: 2088 // This is only a terminator to get the correct spill code placement during 2089 // register allocation. 2090 MI.setDesc(get(AMDGPU::S_ANDN2_B64)); 2091 break; 2092 2093 case AMDGPU::S_ANDN2_B32_term: 2094 // This is only a terminator to get the correct spill code placement during 2095 // register allocation. 2096 MI.setDesc(get(AMDGPU::S_ANDN2_B32)); 2097 break; 2098 2099 case AMDGPU::S_AND_B64_term: 2100 // This is only a terminator to get the correct spill code placement during 2101 // register allocation. 2102 MI.setDesc(get(AMDGPU::S_AND_B64)); 2103 break; 2104 2105 case AMDGPU::S_AND_B32_term: 2106 // This is only a terminator to get the correct spill code placement during 2107 // register allocation. 2108 MI.setDesc(get(AMDGPU::S_AND_B32)); 2109 break; 2110 2111 case AMDGPU::S_AND_SAVEEXEC_B64_term: 2112 // This is only a terminator to get the correct spill code placement during 2113 // register allocation. 2114 MI.setDesc(get(AMDGPU::S_AND_SAVEEXEC_B64)); 2115 break; 2116 2117 case AMDGPU::S_AND_SAVEEXEC_B32_term: 2118 // This is only a terminator to get the correct spill code placement during 2119 // register allocation. 2120 MI.setDesc(get(AMDGPU::S_AND_SAVEEXEC_B32)); 2121 break; 2122 2123 case AMDGPU::SI_SPILL_S32_TO_VGPR: 2124 MI.setDesc(get(AMDGPU::V_WRITELANE_B32)); 2125 break; 2126 2127 case AMDGPU::SI_RESTORE_S32_FROM_VGPR: 2128 MI.setDesc(get(AMDGPU::V_READLANE_B32)); 2129 break; 2130 2131 case AMDGPU::V_MOV_B64_PSEUDO: { 2132 Register Dst = MI.getOperand(0).getReg(); 2133 Register DstLo = RI.getSubReg(Dst, AMDGPU::sub0); 2134 Register DstHi = RI.getSubReg(Dst, AMDGPU::sub1); 2135 2136 const MachineOperand &SrcOp = MI.getOperand(1); 2137 // FIXME: Will this work for 64-bit floating point immediates? 2138 assert(!SrcOp.isFPImm()); 2139 if (ST.hasMovB64()) { 2140 MI.setDesc(get(AMDGPU::V_MOV_B64_e32)); 2141 if (SrcOp.isReg() || isInlineConstant(MI, 1) || 2142 isUInt<32>(SrcOp.getImm())) 2143 break; 2144 } 2145 if (SrcOp.isImm()) { 2146 APInt Imm(64, SrcOp.getImm()); 2147 APInt Lo(32, Imm.getLoBits(32).getZExtValue()); 2148 APInt Hi(32, Imm.getHiBits(32).getZExtValue()); 2149 if (ST.hasPkMovB32() && Lo == Hi && isInlineConstant(Lo)) { 2150 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), Dst) 2151 .addImm(SISrcMods::OP_SEL_1) 2152 .addImm(Lo.getSExtValue()) 2153 .addImm(SISrcMods::OP_SEL_1) 2154 .addImm(Lo.getSExtValue()) 2155 .addImm(0) // op_sel_lo 2156 .addImm(0) // op_sel_hi 2157 .addImm(0) // neg_lo 2158 .addImm(0) // neg_hi 2159 .addImm(0); // clamp 2160 } else { 2161 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo) 2162 .addImm(Lo.getSExtValue()) 2163 .addReg(Dst, RegState::Implicit | RegState::Define); 2164 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi) 2165 .addImm(Hi.getSExtValue()) 2166 .addReg(Dst, RegState::Implicit | RegState::Define); 2167 } 2168 } else { 2169 assert(SrcOp.isReg()); 2170 if (ST.hasPkMovB32() && 2171 !RI.isAGPR(MBB.getParent()->getRegInfo(), SrcOp.getReg())) { 2172 BuildMI(MBB, MI, DL, get(AMDGPU::V_PK_MOV_B32), Dst) 2173 .addImm(SISrcMods::OP_SEL_1) // src0_mod 2174 .addReg(SrcOp.getReg()) 2175 .addImm(SISrcMods::OP_SEL_0 | SISrcMods::OP_SEL_1) // src1_mod 2176 .addReg(SrcOp.getReg()) 2177 .addImm(0) // op_sel_lo 2178 .addImm(0) // op_sel_hi 2179 .addImm(0) // neg_lo 2180 .addImm(0) // neg_hi 2181 .addImm(0); // clamp 2182 } else { 2183 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo) 2184 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub0)) 2185 .addReg(Dst, RegState::Implicit | RegState::Define); 2186 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi) 2187 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub1)) 2188 .addReg(Dst, RegState::Implicit | RegState::Define); 2189 } 2190 } 2191 MI.eraseFromParent(); 2192 break; 2193 } 2194 case AMDGPU::V_MOV_B64_DPP_PSEUDO: { 2195 expandMovDPP64(MI); 2196 break; 2197 } 2198 case AMDGPU::S_MOV_B64_IMM_PSEUDO: { 2199 const MachineOperand &SrcOp = MI.getOperand(1); 2200 assert(!SrcOp.isFPImm()); 2201 APInt Imm(64, SrcOp.getImm()); 2202 if (Imm.isIntN(32) || isInlineConstant(Imm)) { 2203 MI.setDesc(get(AMDGPU::S_MOV_B64)); 2204 break; 2205 } 2206 2207 Register Dst = MI.getOperand(0).getReg(); 2208 Register DstLo = RI.getSubReg(Dst, AMDGPU::sub0); 2209 Register DstHi = RI.getSubReg(Dst, AMDGPU::sub1); 2210 2211 APInt Lo(32, Imm.getLoBits(32).getZExtValue()); 2212 APInt Hi(32, Imm.getHiBits(32).getZExtValue()); 2213 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DstLo) 2214 .addImm(Lo.getSExtValue()) 2215 .addReg(Dst, RegState::Implicit | RegState::Define); 2216 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DstHi) 2217 .addImm(Hi.getSExtValue()) 2218 .addReg(Dst, RegState::Implicit | RegState::Define); 2219 MI.eraseFromParent(); 2220 break; 2221 } 2222 case AMDGPU::V_SET_INACTIVE_B32: { 2223 unsigned NotOpc = ST.isWave32() ? AMDGPU::S_NOT_B32 : AMDGPU::S_NOT_B64; 2224 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 2225 // FIXME: We may possibly optimize the COPY once we find ways to make LLVM 2226 // optimizations (mainly Register Coalescer) aware of WWM register liveness. 2227 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), MI.getOperand(0).getReg()) 2228 .add(MI.getOperand(1)); 2229 auto FirstNot = BuildMI(MBB, MI, DL, get(NotOpc), Exec).addReg(Exec); 2230 FirstNot->addRegisterDead(AMDGPU::SCC, TRI); // SCC is overwritten 2231 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), MI.getOperand(0).getReg()) 2232 .add(MI.getOperand(2)); 2233 BuildMI(MBB, MI, DL, get(NotOpc), Exec) 2234 .addReg(Exec); 2235 MI.eraseFromParent(); 2236 break; 2237 } 2238 case AMDGPU::V_SET_INACTIVE_B64: { 2239 unsigned NotOpc = ST.isWave32() ? AMDGPU::S_NOT_B32 : AMDGPU::S_NOT_B64; 2240 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 2241 MachineInstr *Copy = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO), 2242 MI.getOperand(0).getReg()) 2243 .add(MI.getOperand(1)); 2244 expandPostRAPseudo(*Copy); 2245 auto FirstNot = BuildMI(MBB, MI, DL, get(NotOpc), Exec).addReg(Exec); 2246 FirstNot->addRegisterDead(AMDGPU::SCC, TRI); // SCC is overwritten 2247 Copy = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO), 2248 MI.getOperand(0).getReg()) 2249 .add(MI.getOperand(2)); 2250 expandPostRAPseudo(*Copy); 2251 BuildMI(MBB, MI, DL, get(NotOpc), Exec) 2252 .addReg(Exec); 2253 MI.eraseFromParent(); 2254 break; 2255 } 2256 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V1: 2257 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V2: 2258 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V3: 2259 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V4: 2260 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V5: 2261 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V8: 2262 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V9: 2263 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V10: 2264 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V11: 2265 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V12: 2266 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V16: 2267 case AMDGPU::V_INDIRECT_REG_WRITE_MOVREL_B32_V32: 2268 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V1: 2269 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V2: 2270 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V3: 2271 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V4: 2272 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V5: 2273 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V8: 2274 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V9: 2275 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V10: 2276 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V11: 2277 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V12: 2278 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V16: 2279 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B32_V32: 2280 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V1: 2281 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V2: 2282 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V4: 2283 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V8: 2284 case AMDGPU::S_INDIRECT_REG_WRITE_MOVREL_B64_V16: { 2285 const TargetRegisterClass *EltRC = getOpRegClass(MI, 2); 2286 2287 unsigned Opc; 2288 if (RI.hasVGPRs(EltRC)) { 2289 Opc = AMDGPU::V_MOVRELD_B32_e32; 2290 } else { 2291 Opc = RI.getRegSizeInBits(*EltRC) == 64 ? AMDGPU::S_MOVRELD_B64 2292 : AMDGPU::S_MOVRELD_B32; 2293 } 2294 2295 const MCInstrDesc &OpDesc = get(Opc); 2296 Register VecReg = MI.getOperand(0).getReg(); 2297 bool IsUndef = MI.getOperand(1).isUndef(); 2298 unsigned SubReg = MI.getOperand(3).getImm(); 2299 assert(VecReg == MI.getOperand(1).getReg()); 2300 2301 MachineInstrBuilder MIB = 2302 BuildMI(MBB, MI, DL, OpDesc) 2303 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef) 2304 .add(MI.getOperand(2)) 2305 .addReg(VecReg, RegState::ImplicitDefine) 2306 .addReg(VecReg, RegState::Implicit | (IsUndef ? RegState::Undef : 0)); 2307 2308 const int ImpDefIdx = 2309 OpDesc.getNumOperands() + OpDesc.implicit_uses().size(); 2310 const int ImpUseIdx = ImpDefIdx + 1; 2311 MIB->tieOperands(ImpDefIdx, ImpUseIdx); 2312 MI.eraseFromParent(); 2313 break; 2314 } 2315 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V1: 2316 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V2: 2317 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V3: 2318 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V4: 2319 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V5: 2320 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V8: 2321 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V9: 2322 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V10: 2323 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V11: 2324 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V12: 2325 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V16: 2326 case AMDGPU::V_INDIRECT_REG_WRITE_GPR_IDX_B32_V32: { 2327 assert(ST.useVGPRIndexMode()); 2328 Register VecReg = MI.getOperand(0).getReg(); 2329 bool IsUndef = MI.getOperand(1).isUndef(); 2330 Register Idx = MI.getOperand(3).getReg(); 2331 Register SubReg = MI.getOperand(4).getImm(); 2332 2333 MachineInstr *SetOn = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_ON)) 2334 .addReg(Idx) 2335 .addImm(AMDGPU::VGPRIndexMode::DST_ENABLE); 2336 SetOn->getOperand(3).setIsUndef(); 2337 2338 const MCInstrDesc &OpDesc = get(AMDGPU::V_MOV_B32_indirect_write); 2339 MachineInstrBuilder MIB = 2340 BuildMI(MBB, MI, DL, OpDesc) 2341 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef) 2342 .add(MI.getOperand(2)) 2343 .addReg(VecReg, RegState::ImplicitDefine) 2344 .addReg(VecReg, 2345 RegState::Implicit | (IsUndef ? RegState::Undef : 0)); 2346 2347 const int ImpDefIdx = 2348 OpDesc.getNumOperands() + OpDesc.implicit_uses().size(); 2349 const int ImpUseIdx = ImpDefIdx + 1; 2350 MIB->tieOperands(ImpDefIdx, ImpUseIdx); 2351 2352 MachineInstr *SetOff = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_OFF)); 2353 2354 finalizeBundle(MBB, SetOn->getIterator(), std::next(SetOff->getIterator())); 2355 2356 MI.eraseFromParent(); 2357 break; 2358 } 2359 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V1: 2360 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V2: 2361 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V3: 2362 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V4: 2363 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V5: 2364 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V8: 2365 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V9: 2366 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V10: 2367 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V11: 2368 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V12: 2369 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V16: 2370 case AMDGPU::V_INDIRECT_REG_READ_GPR_IDX_B32_V32: { 2371 assert(ST.useVGPRIndexMode()); 2372 Register Dst = MI.getOperand(0).getReg(); 2373 Register VecReg = MI.getOperand(1).getReg(); 2374 bool IsUndef = MI.getOperand(1).isUndef(); 2375 Register Idx = MI.getOperand(2).getReg(); 2376 Register SubReg = MI.getOperand(3).getImm(); 2377 2378 MachineInstr *SetOn = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_ON)) 2379 .addReg(Idx) 2380 .addImm(AMDGPU::VGPRIndexMode::SRC0_ENABLE); 2381 SetOn->getOperand(3).setIsUndef(); 2382 2383 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_indirect_read)) 2384 .addDef(Dst) 2385 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef) 2386 .addReg(VecReg, RegState::Implicit | (IsUndef ? RegState::Undef : 0)); 2387 2388 MachineInstr *SetOff = BuildMI(MBB, MI, DL, get(AMDGPU::S_SET_GPR_IDX_OFF)); 2389 2390 finalizeBundle(MBB, SetOn->getIterator(), std::next(SetOff->getIterator())); 2391 2392 MI.eraseFromParent(); 2393 break; 2394 } 2395 case AMDGPU::SI_PC_ADD_REL_OFFSET: { 2396 MachineFunction &MF = *MBB.getParent(); 2397 Register Reg = MI.getOperand(0).getReg(); 2398 Register RegLo = RI.getSubReg(Reg, AMDGPU::sub0); 2399 Register RegHi = RI.getSubReg(Reg, AMDGPU::sub1); 2400 MachineOperand OpLo = MI.getOperand(1); 2401 MachineOperand OpHi = MI.getOperand(2); 2402 2403 // Create a bundle so these instructions won't be re-ordered by the 2404 // post-RA scheduler. 2405 MIBundleBuilder Bundler(MBB, MI); 2406 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_GETPC_B64), Reg)); 2407 2408 // What we want here is an offset from the value returned by s_getpc (which 2409 // is the address of the s_add_u32 instruction) to the global variable, but 2410 // since the encoding of $symbol starts 4 bytes after the start of the 2411 // s_add_u32 instruction, we end up with an offset that is 4 bytes too 2412 // small. This requires us to add 4 to the global variable offset in order 2413 // to compute the correct address. Similarly for the s_addc_u32 instruction, 2414 // the encoding of $symbol starts 12 bytes after the start of the s_add_u32 2415 // instruction. 2416 2417 if (OpLo.isGlobal()) 2418 OpLo.setOffset(OpLo.getOffset() + 4); 2419 Bundler.append( 2420 BuildMI(MF, DL, get(AMDGPU::S_ADD_U32), RegLo).addReg(RegLo).add(OpLo)); 2421 2422 if (OpHi.isGlobal()) 2423 OpHi.setOffset(OpHi.getOffset() + 12); 2424 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_ADDC_U32), RegHi) 2425 .addReg(RegHi) 2426 .add(OpHi)); 2427 2428 finalizeBundle(MBB, Bundler.begin()); 2429 2430 MI.eraseFromParent(); 2431 break; 2432 } 2433 case AMDGPU::ENTER_STRICT_WWM: { 2434 // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when 2435 // Whole Wave Mode is entered. 2436 MI.setDesc(get(ST.isWave32() ? AMDGPU::S_OR_SAVEEXEC_B32 2437 : AMDGPU::S_OR_SAVEEXEC_B64)); 2438 break; 2439 } 2440 case AMDGPU::ENTER_STRICT_WQM: { 2441 // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when 2442 // STRICT_WQM is entered. 2443 const unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 2444 const unsigned WQMOp = ST.isWave32() ? AMDGPU::S_WQM_B32 : AMDGPU::S_WQM_B64; 2445 const unsigned MovOp = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64; 2446 BuildMI(MBB, MI, DL, get(MovOp), MI.getOperand(0).getReg()).addReg(Exec); 2447 BuildMI(MBB, MI, DL, get(WQMOp), Exec).addReg(Exec); 2448 2449 MI.eraseFromParent(); 2450 break; 2451 } 2452 case AMDGPU::EXIT_STRICT_WWM: 2453 case AMDGPU::EXIT_STRICT_WQM: { 2454 // This only gets its own opcode so that SIPreAllocateWWMRegs can tell when 2455 // WWM/STICT_WQM is exited. 2456 MI.setDesc(get(ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64)); 2457 break; 2458 } 2459 case AMDGPU::ENTER_PSEUDO_WM: 2460 case AMDGPU::EXIT_PSEUDO_WM: { 2461 // These do nothing. 2462 MI.eraseFromParent(); 2463 break; 2464 } 2465 case AMDGPU::SI_RETURN: { 2466 const MachineFunction *MF = MBB.getParent(); 2467 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 2468 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 2469 // Hiding the return address use with SI_RETURN may lead to extra kills in 2470 // the function and missing live-ins. We are fine in practice because callee 2471 // saved register handling ensures the register value is restored before 2472 // RET, but we need the undef flag here to appease the MachineVerifier 2473 // liveness checks. 2474 MachineInstrBuilder MIB = 2475 BuildMI(MBB, MI, DL, get(AMDGPU::S_SETPC_B64_return)) 2476 .addReg(TRI->getReturnAddressReg(*MF), RegState::Undef); 2477 2478 MIB.copyImplicitOps(MI); 2479 MI.eraseFromParent(); 2480 break; 2481 } 2482 } 2483 return true; 2484 } 2485 2486 void SIInstrInfo::reMaterialize(MachineBasicBlock &MBB, 2487 MachineBasicBlock::iterator I, Register DestReg, 2488 unsigned SubIdx, const MachineInstr &Orig, 2489 const TargetRegisterInfo &RI) const { 2490 2491 // Try shrinking the instruction to remat only the part needed for current 2492 // context. 2493 // TODO: Handle more cases. 2494 unsigned Opcode = Orig.getOpcode(); 2495 switch (Opcode) { 2496 case AMDGPU::S_LOAD_DWORDX16_IMM: 2497 case AMDGPU::S_LOAD_DWORDX8_IMM: { 2498 if (SubIdx != 0) 2499 break; 2500 2501 if (I == MBB.end()) 2502 break; 2503 2504 if (I->isBundled()) 2505 break; 2506 2507 // Look for a single use of the register that is also a subreg. 2508 Register RegToFind = Orig.getOperand(0).getReg(); 2509 MachineOperand *UseMO = nullptr; 2510 for (auto &CandMO : I->operands()) { 2511 if (!CandMO.isReg() || CandMO.getReg() != RegToFind || CandMO.isDef()) 2512 continue; 2513 if (UseMO) { 2514 UseMO = nullptr; 2515 break; 2516 } 2517 UseMO = &CandMO; 2518 } 2519 if (!UseMO || UseMO->getSubReg() == AMDGPU::NoSubRegister) 2520 break; 2521 2522 unsigned Offset = RI.getSubRegIdxOffset(UseMO->getSubReg()); 2523 unsigned SubregSize = RI.getSubRegIdxSize(UseMO->getSubReg()); 2524 2525 MachineFunction *MF = MBB.getParent(); 2526 MachineRegisterInfo &MRI = MF->getRegInfo(); 2527 assert(MRI.use_nodbg_empty(DestReg) && "DestReg should have no users yet."); 2528 2529 unsigned NewOpcode = -1; 2530 if (SubregSize == 256) 2531 NewOpcode = AMDGPU::S_LOAD_DWORDX8_IMM; 2532 else if (SubregSize == 128) 2533 NewOpcode = AMDGPU::S_LOAD_DWORDX4_IMM; 2534 else 2535 break; 2536 2537 const MCInstrDesc &TID = get(NewOpcode); 2538 const TargetRegisterClass *NewRC = 2539 RI.getAllocatableClass(getRegClass(TID, 0, &RI, *MF)); 2540 MRI.setRegClass(DestReg, NewRC); 2541 2542 UseMO->setReg(DestReg); 2543 UseMO->setSubReg(AMDGPU::NoSubRegister); 2544 2545 // Use a smaller load with the desired size, possibly with updated offset. 2546 MachineInstr *MI = MF->CloneMachineInstr(&Orig); 2547 MI->setDesc(TID); 2548 MI->getOperand(0).setReg(DestReg); 2549 MI->getOperand(0).setSubReg(AMDGPU::NoSubRegister); 2550 if (Offset) { 2551 MachineOperand *OffsetMO = getNamedOperand(*MI, AMDGPU::OpName::offset); 2552 int64_t FinalOffset = OffsetMO->getImm() + Offset / 8; 2553 OffsetMO->setImm(FinalOffset); 2554 } 2555 SmallVector<MachineMemOperand *> NewMMOs; 2556 for (const MachineMemOperand *MemOp : Orig.memoperands()) 2557 NewMMOs.push_back(MF->getMachineMemOperand(MemOp, MemOp->getPointerInfo(), 2558 SubregSize / 8)); 2559 MI->setMemRefs(*MF, NewMMOs); 2560 2561 MBB.insert(I, MI); 2562 return; 2563 } 2564 2565 default: 2566 break; 2567 } 2568 2569 TargetInstrInfo::reMaterialize(MBB, I, DestReg, SubIdx, Orig, RI); 2570 } 2571 2572 std::pair<MachineInstr*, MachineInstr*> 2573 SIInstrInfo::expandMovDPP64(MachineInstr &MI) const { 2574 assert (MI.getOpcode() == AMDGPU::V_MOV_B64_DPP_PSEUDO); 2575 2576 if (ST.hasMovB64() && 2577 AMDGPU::isLegalDPALU_DPPControl( 2578 getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl)->getImm())) { 2579 MI.setDesc(get(AMDGPU::V_MOV_B64_dpp)); 2580 return std::pair(&MI, nullptr); 2581 } 2582 2583 MachineBasicBlock &MBB = *MI.getParent(); 2584 DebugLoc DL = MBB.findDebugLoc(MI); 2585 MachineFunction *MF = MBB.getParent(); 2586 MachineRegisterInfo &MRI = MF->getRegInfo(); 2587 Register Dst = MI.getOperand(0).getReg(); 2588 unsigned Part = 0; 2589 MachineInstr *Split[2]; 2590 2591 for (auto Sub : { AMDGPU::sub0, AMDGPU::sub1 }) { 2592 auto MovDPP = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_dpp)); 2593 if (Dst.isPhysical()) { 2594 MovDPP.addDef(RI.getSubReg(Dst, Sub)); 2595 } else { 2596 assert(MRI.isSSA()); 2597 auto Tmp = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 2598 MovDPP.addDef(Tmp); 2599 } 2600 2601 for (unsigned I = 1; I <= 2; ++I) { // old and src operands. 2602 const MachineOperand &SrcOp = MI.getOperand(I); 2603 assert(!SrcOp.isFPImm()); 2604 if (SrcOp.isImm()) { 2605 APInt Imm(64, SrcOp.getImm()); 2606 Imm.ashrInPlace(Part * 32); 2607 MovDPP.addImm(Imm.getLoBits(32).getZExtValue()); 2608 } else { 2609 assert(SrcOp.isReg()); 2610 Register Src = SrcOp.getReg(); 2611 if (Src.isPhysical()) 2612 MovDPP.addReg(RI.getSubReg(Src, Sub)); 2613 else 2614 MovDPP.addReg(Src, SrcOp.isUndef() ? RegState::Undef : 0, Sub); 2615 } 2616 } 2617 2618 for (const MachineOperand &MO : llvm::drop_begin(MI.explicit_operands(), 3)) 2619 MovDPP.addImm(MO.getImm()); 2620 2621 Split[Part] = MovDPP; 2622 ++Part; 2623 } 2624 2625 if (Dst.isVirtual()) 2626 BuildMI(MBB, MI, DL, get(AMDGPU::REG_SEQUENCE), Dst) 2627 .addReg(Split[0]->getOperand(0).getReg()) 2628 .addImm(AMDGPU::sub0) 2629 .addReg(Split[1]->getOperand(0).getReg()) 2630 .addImm(AMDGPU::sub1); 2631 2632 MI.eraseFromParent(); 2633 return std::pair(Split[0], Split[1]); 2634 } 2635 2636 std::optional<DestSourcePair> 2637 SIInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const { 2638 if (MI.getOpcode() == AMDGPU::WWM_COPY) 2639 return DestSourcePair{MI.getOperand(0), MI.getOperand(1)}; 2640 2641 return std::nullopt; 2642 } 2643 2644 bool SIInstrInfo::swapSourceModifiers(MachineInstr &MI, 2645 MachineOperand &Src0, 2646 unsigned Src0OpName, 2647 MachineOperand &Src1, 2648 unsigned Src1OpName) const { 2649 MachineOperand *Src0Mods = getNamedOperand(MI, Src0OpName); 2650 if (!Src0Mods) 2651 return false; 2652 2653 MachineOperand *Src1Mods = getNamedOperand(MI, Src1OpName); 2654 assert(Src1Mods && 2655 "All commutable instructions have both src0 and src1 modifiers"); 2656 2657 int Src0ModsVal = Src0Mods->getImm(); 2658 int Src1ModsVal = Src1Mods->getImm(); 2659 2660 Src1Mods->setImm(Src0ModsVal); 2661 Src0Mods->setImm(Src1ModsVal); 2662 return true; 2663 } 2664 2665 static MachineInstr *swapRegAndNonRegOperand(MachineInstr &MI, 2666 MachineOperand &RegOp, 2667 MachineOperand &NonRegOp) { 2668 Register Reg = RegOp.getReg(); 2669 unsigned SubReg = RegOp.getSubReg(); 2670 bool IsKill = RegOp.isKill(); 2671 bool IsDead = RegOp.isDead(); 2672 bool IsUndef = RegOp.isUndef(); 2673 bool IsDebug = RegOp.isDebug(); 2674 2675 if (NonRegOp.isImm()) 2676 RegOp.ChangeToImmediate(NonRegOp.getImm()); 2677 else if (NonRegOp.isFI()) 2678 RegOp.ChangeToFrameIndex(NonRegOp.getIndex()); 2679 else if (NonRegOp.isGlobal()) { 2680 RegOp.ChangeToGA(NonRegOp.getGlobal(), NonRegOp.getOffset(), 2681 NonRegOp.getTargetFlags()); 2682 } else 2683 return nullptr; 2684 2685 // Make sure we don't reinterpret a subreg index in the target flags. 2686 RegOp.setTargetFlags(NonRegOp.getTargetFlags()); 2687 2688 NonRegOp.ChangeToRegister(Reg, false, false, IsKill, IsDead, IsUndef, IsDebug); 2689 NonRegOp.setSubReg(SubReg); 2690 2691 return &MI; 2692 } 2693 2694 MachineInstr *SIInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, 2695 unsigned Src0Idx, 2696 unsigned Src1Idx) const { 2697 assert(!NewMI && "this should never be used"); 2698 2699 unsigned Opc = MI.getOpcode(); 2700 int CommutedOpcode = commuteOpcode(Opc); 2701 if (CommutedOpcode == -1) 2702 return nullptr; 2703 2704 if (Src0Idx > Src1Idx) 2705 std::swap(Src0Idx, Src1Idx); 2706 2707 assert(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) == 2708 static_cast<int>(Src0Idx) && 2709 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1) == 2710 static_cast<int>(Src1Idx) && 2711 "inconsistency with findCommutedOpIndices"); 2712 2713 MachineOperand &Src0 = MI.getOperand(Src0Idx); 2714 MachineOperand &Src1 = MI.getOperand(Src1Idx); 2715 2716 MachineInstr *CommutedMI = nullptr; 2717 if (Src0.isReg() && Src1.isReg()) { 2718 if (isOperandLegal(MI, Src1Idx, &Src0)) { 2719 // Be sure to copy the source modifiers to the right place. 2720 CommutedMI 2721 = TargetInstrInfo::commuteInstructionImpl(MI, NewMI, Src0Idx, Src1Idx); 2722 } 2723 2724 } else if (Src0.isReg() && !Src1.isReg()) { 2725 // src0 should always be able to support any operand type, so no need to 2726 // check operand legality. 2727 CommutedMI = swapRegAndNonRegOperand(MI, Src0, Src1); 2728 } else if (!Src0.isReg() && Src1.isReg()) { 2729 if (isOperandLegal(MI, Src1Idx, &Src0)) 2730 CommutedMI = swapRegAndNonRegOperand(MI, Src1, Src0); 2731 } else { 2732 // FIXME: Found two non registers to commute. This does happen. 2733 return nullptr; 2734 } 2735 2736 if (CommutedMI) { 2737 swapSourceModifiers(MI, Src0, AMDGPU::OpName::src0_modifiers, 2738 Src1, AMDGPU::OpName::src1_modifiers); 2739 2740 CommutedMI->setDesc(get(CommutedOpcode)); 2741 } 2742 2743 return CommutedMI; 2744 } 2745 2746 // This needs to be implemented because the source modifiers may be inserted 2747 // between the true commutable operands, and the base 2748 // TargetInstrInfo::commuteInstruction uses it. 2749 bool SIInstrInfo::findCommutedOpIndices(const MachineInstr &MI, 2750 unsigned &SrcOpIdx0, 2751 unsigned &SrcOpIdx1) const { 2752 return findCommutedOpIndices(MI.getDesc(), SrcOpIdx0, SrcOpIdx1); 2753 } 2754 2755 bool SIInstrInfo::findCommutedOpIndices(const MCInstrDesc &Desc, 2756 unsigned &SrcOpIdx0, 2757 unsigned &SrcOpIdx1) const { 2758 if (!Desc.isCommutable()) 2759 return false; 2760 2761 unsigned Opc = Desc.getOpcode(); 2762 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 2763 if (Src0Idx == -1) 2764 return false; 2765 2766 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 2767 if (Src1Idx == -1) 2768 return false; 2769 2770 return fixCommutedOpIndices(SrcOpIdx0, SrcOpIdx1, Src0Idx, Src1Idx); 2771 } 2772 2773 bool SIInstrInfo::isBranchOffsetInRange(unsigned BranchOp, 2774 int64_t BrOffset) const { 2775 // BranchRelaxation should never have to check s_setpc_b64 because its dest 2776 // block is unanalyzable. 2777 assert(BranchOp != AMDGPU::S_SETPC_B64); 2778 2779 // Convert to dwords. 2780 BrOffset /= 4; 2781 2782 // The branch instructions do PC += signext(SIMM16 * 4) + 4, so the offset is 2783 // from the next instruction. 2784 BrOffset -= 1; 2785 2786 return isIntN(BranchOffsetBits, BrOffset); 2787 } 2788 2789 MachineBasicBlock * 2790 SIInstrInfo::getBranchDestBlock(const MachineInstr &MI) const { 2791 return MI.getOperand(0).getMBB(); 2792 } 2793 2794 bool SIInstrInfo::hasDivergentBranch(const MachineBasicBlock *MBB) const { 2795 for (const MachineInstr &MI : MBB->terminators()) { 2796 if (MI.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO || 2797 MI.getOpcode() == AMDGPU::SI_IF || MI.getOpcode() == AMDGPU::SI_ELSE || 2798 MI.getOpcode() == AMDGPU::SI_LOOP) 2799 return true; 2800 } 2801 return false; 2802 } 2803 2804 void SIInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB, 2805 MachineBasicBlock &DestBB, 2806 MachineBasicBlock &RestoreBB, 2807 const DebugLoc &DL, int64_t BrOffset, 2808 RegScavenger *RS) const { 2809 assert(RS && "RegScavenger required for long branching"); 2810 assert(MBB.empty() && 2811 "new block should be inserted for expanding unconditional branch"); 2812 assert(MBB.pred_size() == 1); 2813 assert(RestoreBB.empty() && 2814 "restore block should be inserted for restoring clobbered registers"); 2815 2816 MachineFunction *MF = MBB.getParent(); 2817 MachineRegisterInfo &MRI = MF->getRegInfo(); 2818 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 2819 2820 // FIXME: Virtual register workaround for RegScavenger not working with empty 2821 // blocks. 2822 Register PCReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 2823 2824 auto I = MBB.end(); 2825 2826 // We need to compute the offset relative to the instruction immediately after 2827 // s_getpc_b64. Insert pc arithmetic code before last terminator. 2828 MachineInstr *GetPC = BuildMI(MBB, I, DL, get(AMDGPU::S_GETPC_B64), PCReg); 2829 2830 auto &MCCtx = MF->getContext(); 2831 MCSymbol *PostGetPCLabel = 2832 MCCtx.createTempSymbol("post_getpc", /*AlwaysAddSuffix=*/true); 2833 GetPC->setPostInstrSymbol(*MF, PostGetPCLabel); 2834 2835 MCSymbol *OffsetLo = 2836 MCCtx.createTempSymbol("offset_lo", /*AlwaysAddSuffix=*/true); 2837 MCSymbol *OffsetHi = 2838 MCCtx.createTempSymbol("offset_hi", /*AlwaysAddSuffix=*/true); 2839 BuildMI(MBB, I, DL, get(AMDGPU::S_ADD_U32)) 2840 .addReg(PCReg, RegState::Define, AMDGPU::sub0) 2841 .addReg(PCReg, 0, AMDGPU::sub0) 2842 .addSym(OffsetLo, MO_FAR_BRANCH_OFFSET); 2843 BuildMI(MBB, I, DL, get(AMDGPU::S_ADDC_U32)) 2844 .addReg(PCReg, RegState::Define, AMDGPU::sub1) 2845 .addReg(PCReg, 0, AMDGPU::sub1) 2846 .addSym(OffsetHi, MO_FAR_BRANCH_OFFSET); 2847 2848 // Insert the indirect branch after the other terminator. 2849 BuildMI(&MBB, DL, get(AMDGPU::S_SETPC_B64)) 2850 .addReg(PCReg); 2851 2852 // If a spill is needed for the pc register pair, we need to insert a spill 2853 // restore block right before the destination block, and insert a short branch 2854 // into the old destination block's fallthrough predecessor. 2855 // e.g.: 2856 // 2857 // s_cbranch_scc0 skip_long_branch: 2858 // 2859 // long_branch_bb: 2860 // spill s[8:9] 2861 // s_getpc_b64 s[8:9] 2862 // s_add_u32 s8, s8, restore_bb 2863 // s_addc_u32 s9, s9, 0 2864 // s_setpc_b64 s[8:9] 2865 // 2866 // skip_long_branch: 2867 // foo; 2868 // 2869 // ..... 2870 // 2871 // dest_bb_fallthrough_predecessor: 2872 // bar; 2873 // s_branch dest_bb 2874 // 2875 // restore_bb: 2876 // restore s[8:9] 2877 // fallthrough dest_bb 2878 /// 2879 // dest_bb: 2880 // buzz; 2881 2882 Register LongBranchReservedReg = MFI->getLongBranchReservedReg(); 2883 Register Scav; 2884 2885 // If we've previously reserved a register for long branches 2886 // avoid running the scavenger and just use those registers 2887 if (LongBranchReservedReg) { 2888 RS->enterBasicBlock(MBB); 2889 Scav = LongBranchReservedReg; 2890 } else { 2891 RS->enterBasicBlockEnd(MBB); 2892 Scav = RS->scavengeRegisterBackwards( 2893 AMDGPU::SReg_64RegClass, MachineBasicBlock::iterator(GetPC), 2894 /* RestoreAfter */ false, 0, /* AllowSpill */ false); 2895 } 2896 if (Scav) { 2897 RS->setRegUsed(Scav); 2898 MRI.replaceRegWith(PCReg, Scav); 2899 MRI.clearVirtRegs(); 2900 } else { 2901 // As SGPR needs VGPR to be spilled, we reuse the slot of temporary VGPR for 2902 // SGPR spill. 2903 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 2904 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 2905 TRI->spillEmergencySGPR(GetPC, RestoreBB, AMDGPU::SGPR0_SGPR1, RS); 2906 MRI.replaceRegWith(PCReg, AMDGPU::SGPR0_SGPR1); 2907 MRI.clearVirtRegs(); 2908 } 2909 2910 MCSymbol *DestLabel = Scav ? DestBB.getSymbol() : RestoreBB.getSymbol(); 2911 // Now, the distance could be defined. 2912 auto *Offset = MCBinaryExpr::createSub( 2913 MCSymbolRefExpr::create(DestLabel, MCCtx), 2914 MCSymbolRefExpr::create(PostGetPCLabel, MCCtx), MCCtx); 2915 // Add offset assignments. 2916 auto *Mask = MCConstantExpr::create(0xFFFFFFFFULL, MCCtx); 2917 OffsetLo->setVariableValue(MCBinaryExpr::createAnd(Offset, Mask, MCCtx)); 2918 auto *ShAmt = MCConstantExpr::create(32, MCCtx); 2919 OffsetHi->setVariableValue(MCBinaryExpr::createAShr(Offset, ShAmt, MCCtx)); 2920 } 2921 2922 unsigned SIInstrInfo::getBranchOpcode(SIInstrInfo::BranchPredicate Cond) { 2923 switch (Cond) { 2924 case SIInstrInfo::SCC_TRUE: 2925 return AMDGPU::S_CBRANCH_SCC1; 2926 case SIInstrInfo::SCC_FALSE: 2927 return AMDGPU::S_CBRANCH_SCC0; 2928 case SIInstrInfo::VCCNZ: 2929 return AMDGPU::S_CBRANCH_VCCNZ; 2930 case SIInstrInfo::VCCZ: 2931 return AMDGPU::S_CBRANCH_VCCZ; 2932 case SIInstrInfo::EXECNZ: 2933 return AMDGPU::S_CBRANCH_EXECNZ; 2934 case SIInstrInfo::EXECZ: 2935 return AMDGPU::S_CBRANCH_EXECZ; 2936 default: 2937 llvm_unreachable("invalid branch predicate"); 2938 } 2939 } 2940 2941 SIInstrInfo::BranchPredicate SIInstrInfo::getBranchPredicate(unsigned Opcode) { 2942 switch (Opcode) { 2943 case AMDGPU::S_CBRANCH_SCC0: 2944 return SCC_FALSE; 2945 case AMDGPU::S_CBRANCH_SCC1: 2946 return SCC_TRUE; 2947 case AMDGPU::S_CBRANCH_VCCNZ: 2948 return VCCNZ; 2949 case AMDGPU::S_CBRANCH_VCCZ: 2950 return VCCZ; 2951 case AMDGPU::S_CBRANCH_EXECNZ: 2952 return EXECNZ; 2953 case AMDGPU::S_CBRANCH_EXECZ: 2954 return EXECZ; 2955 default: 2956 return INVALID_BR; 2957 } 2958 } 2959 2960 bool SIInstrInfo::analyzeBranchImpl(MachineBasicBlock &MBB, 2961 MachineBasicBlock::iterator I, 2962 MachineBasicBlock *&TBB, 2963 MachineBasicBlock *&FBB, 2964 SmallVectorImpl<MachineOperand> &Cond, 2965 bool AllowModify) const { 2966 if (I->getOpcode() == AMDGPU::S_BRANCH) { 2967 // Unconditional Branch 2968 TBB = I->getOperand(0).getMBB(); 2969 return false; 2970 } 2971 2972 MachineBasicBlock *CondBB = nullptr; 2973 2974 if (I->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 2975 CondBB = I->getOperand(1).getMBB(); 2976 Cond.push_back(I->getOperand(0)); 2977 } else { 2978 BranchPredicate Pred = getBranchPredicate(I->getOpcode()); 2979 if (Pred == INVALID_BR) 2980 return true; 2981 2982 CondBB = I->getOperand(0).getMBB(); 2983 Cond.push_back(MachineOperand::CreateImm(Pred)); 2984 Cond.push_back(I->getOperand(1)); // Save the branch register. 2985 } 2986 ++I; 2987 2988 if (I == MBB.end()) { 2989 // Conditional branch followed by fall-through. 2990 TBB = CondBB; 2991 return false; 2992 } 2993 2994 if (I->getOpcode() == AMDGPU::S_BRANCH) { 2995 TBB = CondBB; 2996 FBB = I->getOperand(0).getMBB(); 2997 return false; 2998 } 2999 3000 return true; 3001 } 3002 3003 bool SIInstrInfo::analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, 3004 MachineBasicBlock *&FBB, 3005 SmallVectorImpl<MachineOperand> &Cond, 3006 bool AllowModify) const { 3007 MachineBasicBlock::iterator I = MBB.getFirstTerminator(); 3008 auto E = MBB.end(); 3009 if (I == E) 3010 return false; 3011 3012 // Skip over the instructions that are artificially terminators for special 3013 // exec management. 3014 while (I != E && !I->isBranch() && !I->isReturn()) { 3015 switch (I->getOpcode()) { 3016 case AMDGPU::S_MOV_B64_term: 3017 case AMDGPU::S_XOR_B64_term: 3018 case AMDGPU::S_OR_B64_term: 3019 case AMDGPU::S_ANDN2_B64_term: 3020 case AMDGPU::S_AND_B64_term: 3021 case AMDGPU::S_AND_SAVEEXEC_B64_term: 3022 case AMDGPU::S_MOV_B32_term: 3023 case AMDGPU::S_XOR_B32_term: 3024 case AMDGPU::S_OR_B32_term: 3025 case AMDGPU::S_ANDN2_B32_term: 3026 case AMDGPU::S_AND_B32_term: 3027 case AMDGPU::S_AND_SAVEEXEC_B32_term: 3028 break; 3029 case AMDGPU::SI_IF: 3030 case AMDGPU::SI_ELSE: 3031 case AMDGPU::SI_KILL_I1_TERMINATOR: 3032 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR: 3033 // FIXME: It's messy that these need to be considered here at all. 3034 return true; 3035 default: 3036 llvm_unreachable("unexpected non-branch terminator inst"); 3037 } 3038 3039 ++I; 3040 } 3041 3042 if (I == E) 3043 return false; 3044 3045 return analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify); 3046 } 3047 3048 unsigned SIInstrInfo::removeBranch(MachineBasicBlock &MBB, 3049 int *BytesRemoved) const { 3050 unsigned Count = 0; 3051 unsigned RemovedSize = 0; 3052 for (MachineInstr &MI : llvm::make_early_inc_range(MBB.terminators())) { 3053 // Skip over artificial terminators when removing instructions. 3054 if (MI.isBranch() || MI.isReturn()) { 3055 RemovedSize += getInstSizeInBytes(MI); 3056 MI.eraseFromParent(); 3057 ++Count; 3058 } 3059 } 3060 3061 if (BytesRemoved) 3062 *BytesRemoved = RemovedSize; 3063 3064 return Count; 3065 } 3066 3067 // Copy the flags onto the implicit condition register operand. 3068 static void preserveCondRegFlags(MachineOperand &CondReg, 3069 const MachineOperand &OrigCond) { 3070 CondReg.setIsUndef(OrigCond.isUndef()); 3071 CondReg.setIsKill(OrigCond.isKill()); 3072 } 3073 3074 unsigned SIInstrInfo::insertBranch(MachineBasicBlock &MBB, 3075 MachineBasicBlock *TBB, 3076 MachineBasicBlock *FBB, 3077 ArrayRef<MachineOperand> Cond, 3078 const DebugLoc &DL, 3079 int *BytesAdded) const { 3080 if (!FBB && Cond.empty()) { 3081 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH)) 3082 .addMBB(TBB); 3083 if (BytesAdded) 3084 *BytesAdded = ST.hasOffset3fBug() ? 8 : 4; 3085 return 1; 3086 } 3087 3088 if(Cond.size() == 1 && Cond[0].isReg()) { 3089 BuildMI(&MBB, DL, get(AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO)) 3090 .add(Cond[0]) 3091 .addMBB(TBB); 3092 return 1; 3093 } 3094 3095 assert(TBB && Cond[0].isImm()); 3096 3097 unsigned Opcode 3098 = getBranchOpcode(static_cast<BranchPredicate>(Cond[0].getImm())); 3099 3100 if (!FBB) { 3101 MachineInstr *CondBr = 3102 BuildMI(&MBB, DL, get(Opcode)) 3103 .addMBB(TBB); 3104 3105 // Copy the flags onto the implicit condition register operand. 3106 preserveCondRegFlags(CondBr->getOperand(1), Cond[1]); 3107 fixImplicitOperands(*CondBr); 3108 3109 if (BytesAdded) 3110 *BytesAdded = ST.hasOffset3fBug() ? 8 : 4; 3111 return 1; 3112 } 3113 3114 assert(TBB && FBB); 3115 3116 MachineInstr *CondBr = 3117 BuildMI(&MBB, DL, get(Opcode)) 3118 .addMBB(TBB); 3119 fixImplicitOperands(*CondBr); 3120 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH)) 3121 .addMBB(FBB); 3122 3123 MachineOperand &CondReg = CondBr->getOperand(1); 3124 CondReg.setIsUndef(Cond[1].isUndef()); 3125 CondReg.setIsKill(Cond[1].isKill()); 3126 3127 if (BytesAdded) 3128 *BytesAdded = ST.hasOffset3fBug() ? 16 : 8; 3129 3130 return 2; 3131 } 3132 3133 bool SIInstrInfo::reverseBranchCondition( 3134 SmallVectorImpl<MachineOperand> &Cond) const { 3135 if (Cond.size() != 2) { 3136 return true; 3137 } 3138 3139 if (Cond[0].isImm()) { 3140 Cond[0].setImm(-Cond[0].getImm()); 3141 return false; 3142 } 3143 3144 return true; 3145 } 3146 3147 bool SIInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, 3148 ArrayRef<MachineOperand> Cond, 3149 Register DstReg, Register TrueReg, 3150 Register FalseReg, int &CondCycles, 3151 int &TrueCycles, int &FalseCycles) const { 3152 switch (Cond[0].getImm()) { 3153 case VCCNZ: 3154 case VCCZ: { 3155 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3156 const TargetRegisterClass *RC = MRI.getRegClass(TrueReg); 3157 if (MRI.getRegClass(FalseReg) != RC) 3158 return false; 3159 3160 int NumInsts = AMDGPU::getRegBitWidth(*RC) / 32; 3161 CondCycles = TrueCycles = FalseCycles = NumInsts; // ??? 3162 3163 // Limit to equal cost for branch vs. N v_cndmask_b32s. 3164 return RI.hasVGPRs(RC) && NumInsts <= 6; 3165 } 3166 case SCC_TRUE: 3167 case SCC_FALSE: { 3168 // FIXME: We could insert for VGPRs if we could replace the original compare 3169 // with a vector one. 3170 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3171 const TargetRegisterClass *RC = MRI.getRegClass(TrueReg); 3172 if (MRI.getRegClass(FalseReg) != RC) 3173 return false; 3174 3175 int NumInsts = AMDGPU::getRegBitWidth(*RC) / 32; 3176 3177 // Multiples of 8 can do s_cselect_b64 3178 if (NumInsts % 2 == 0) 3179 NumInsts /= 2; 3180 3181 CondCycles = TrueCycles = FalseCycles = NumInsts; // ??? 3182 return RI.isSGPRClass(RC); 3183 } 3184 default: 3185 return false; 3186 } 3187 } 3188 3189 void SIInstrInfo::insertSelect(MachineBasicBlock &MBB, 3190 MachineBasicBlock::iterator I, const DebugLoc &DL, 3191 Register DstReg, ArrayRef<MachineOperand> Cond, 3192 Register TrueReg, Register FalseReg) const { 3193 BranchPredicate Pred = static_cast<BranchPredicate>(Cond[0].getImm()); 3194 if (Pred == VCCZ || Pred == SCC_FALSE) { 3195 Pred = static_cast<BranchPredicate>(-Pred); 3196 std::swap(TrueReg, FalseReg); 3197 } 3198 3199 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3200 const TargetRegisterClass *DstRC = MRI.getRegClass(DstReg); 3201 unsigned DstSize = RI.getRegSizeInBits(*DstRC); 3202 3203 if (DstSize == 32) { 3204 MachineInstr *Select; 3205 if (Pred == SCC_TRUE) { 3206 Select = BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B32), DstReg) 3207 .addReg(TrueReg) 3208 .addReg(FalseReg); 3209 } else { 3210 // Instruction's operands are backwards from what is expected. 3211 Select = BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e32), DstReg) 3212 .addReg(FalseReg) 3213 .addReg(TrueReg); 3214 } 3215 3216 preserveCondRegFlags(Select->getOperand(3), Cond[1]); 3217 return; 3218 } 3219 3220 if (DstSize == 64 && Pred == SCC_TRUE) { 3221 MachineInstr *Select = 3222 BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B64), DstReg) 3223 .addReg(TrueReg) 3224 .addReg(FalseReg); 3225 3226 preserveCondRegFlags(Select->getOperand(3), Cond[1]); 3227 return; 3228 } 3229 3230 static const int16_t Sub0_15[] = { 3231 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3, 3232 AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7, 3233 AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11, 3234 AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15, 3235 }; 3236 3237 static const int16_t Sub0_15_64[] = { 3238 AMDGPU::sub0_sub1, AMDGPU::sub2_sub3, 3239 AMDGPU::sub4_sub5, AMDGPU::sub6_sub7, 3240 AMDGPU::sub8_sub9, AMDGPU::sub10_sub11, 3241 AMDGPU::sub12_sub13, AMDGPU::sub14_sub15, 3242 }; 3243 3244 unsigned SelOp = AMDGPU::V_CNDMASK_B32_e32; 3245 const TargetRegisterClass *EltRC = &AMDGPU::VGPR_32RegClass; 3246 const int16_t *SubIndices = Sub0_15; 3247 int NElts = DstSize / 32; 3248 3249 // 64-bit select is only available for SALU. 3250 // TODO: Split 96-bit into 64-bit and 32-bit, not 3x 32-bit. 3251 if (Pred == SCC_TRUE) { 3252 if (NElts % 2) { 3253 SelOp = AMDGPU::S_CSELECT_B32; 3254 EltRC = &AMDGPU::SGPR_32RegClass; 3255 } else { 3256 SelOp = AMDGPU::S_CSELECT_B64; 3257 EltRC = &AMDGPU::SGPR_64RegClass; 3258 SubIndices = Sub0_15_64; 3259 NElts /= 2; 3260 } 3261 } 3262 3263 MachineInstrBuilder MIB = BuildMI( 3264 MBB, I, DL, get(AMDGPU::REG_SEQUENCE), DstReg); 3265 3266 I = MIB->getIterator(); 3267 3268 SmallVector<Register, 8> Regs; 3269 for (int Idx = 0; Idx != NElts; ++Idx) { 3270 Register DstElt = MRI.createVirtualRegister(EltRC); 3271 Regs.push_back(DstElt); 3272 3273 unsigned SubIdx = SubIndices[Idx]; 3274 3275 MachineInstr *Select; 3276 if (SelOp == AMDGPU::V_CNDMASK_B32_e32) { 3277 Select = 3278 BuildMI(MBB, I, DL, get(SelOp), DstElt) 3279 .addReg(FalseReg, 0, SubIdx) 3280 .addReg(TrueReg, 0, SubIdx); 3281 } else { 3282 Select = 3283 BuildMI(MBB, I, DL, get(SelOp), DstElt) 3284 .addReg(TrueReg, 0, SubIdx) 3285 .addReg(FalseReg, 0, SubIdx); 3286 } 3287 3288 preserveCondRegFlags(Select->getOperand(3), Cond[1]); 3289 fixImplicitOperands(*Select); 3290 3291 MIB.addReg(DstElt) 3292 .addImm(SubIdx); 3293 } 3294 } 3295 3296 bool SIInstrInfo::isFoldableCopy(const MachineInstr &MI) { 3297 switch (MI.getOpcode()) { 3298 case AMDGPU::V_MOV_B32_e32: 3299 case AMDGPU::V_MOV_B32_e64: 3300 case AMDGPU::V_MOV_B64_PSEUDO: 3301 case AMDGPU::V_MOV_B64_e32: 3302 case AMDGPU::V_MOV_B64_e64: 3303 case AMDGPU::S_MOV_B32: 3304 case AMDGPU::S_MOV_B64: 3305 case AMDGPU::S_MOV_B64_IMM_PSEUDO: 3306 case AMDGPU::COPY: 3307 case AMDGPU::WWM_COPY: 3308 case AMDGPU::V_ACCVGPR_WRITE_B32_e64: 3309 case AMDGPU::V_ACCVGPR_READ_B32_e64: 3310 case AMDGPU::V_ACCVGPR_MOV_B32: 3311 return true; 3312 default: 3313 return false; 3314 } 3315 } 3316 3317 static constexpr unsigned ModifierOpNames[] = { 3318 AMDGPU::OpName::src0_modifiers, AMDGPU::OpName::src1_modifiers, 3319 AMDGPU::OpName::src2_modifiers, AMDGPU::OpName::clamp, 3320 AMDGPU::OpName::omod, AMDGPU::OpName::op_sel}; 3321 3322 void SIInstrInfo::removeModOperands(MachineInstr &MI) const { 3323 unsigned Opc = MI.getOpcode(); 3324 for (unsigned Name : reverse(ModifierOpNames)) { 3325 int Idx = AMDGPU::getNamedOperandIdx(Opc, Name); 3326 if (Idx >= 0) 3327 MI.removeOperand(Idx); 3328 } 3329 } 3330 3331 bool SIInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 3332 Register Reg, MachineRegisterInfo *MRI) const { 3333 if (!MRI->hasOneNonDBGUse(Reg)) 3334 return false; 3335 3336 switch (DefMI.getOpcode()) { 3337 default: 3338 return false; 3339 case AMDGPU::V_MOV_B64_e32: 3340 case AMDGPU::S_MOV_B64: 3341 case AMDGPU::V_MOV_B64_PSEUDO: 3342 case AMDGPU::S_MOV_B64_IMM_PSEUDO: 3343 case AMDGPU::V_MOV_B32_e32: 3344 case AMDGPU::S_MOV_B32: 3345 case AMDGPU::V_ACCVGPR_WRITE_B32_e64: 3346 break; 3347 } 3348 3349 const MachineOperand *ImmOp = getNamedOperand(DefMI, AMDGPU::OpName::src0); 3350 assert(ImmOp); 3351 // FIXME: We could handle FrameIndex values here. 3352 if (!ImmOp->isImm()) 3353 return false; 3354 3355 auto getImmFor = [ImmOp](const MachineOperand &UseOp) -> int64_t { 3356 int64_t Imm = ImmOp->getImm(); 3357 switch (UseOp.getSubReg()) { 3358 default: 3359 return Imm; 3360 case AMDGPU::sub0: 3361 return Lo_32(Imm); 3362 case AMDGPU::sub1: 3363 return Hi_32(Imm); 3364 case AMDGPU::lo16: 3365 return APInt(16, Imm).getSExtValue(); 3366 case AMDGPU::hi16: 3367 return APInt(32, Imm).ashr(16).getSExtValue(); 3368 case AMDGPU::sub1_lo16: 3369 return APInt(16, Hi_32(Imm)).getSExtValue(); 3370 case AMDGPU::sub1_hi16: 3371 return APInt(32, Hi_32(Imm)).ashr(16).getSExtValue(); 3372 } 3373 }; 3374 3375 assert(!DefMI.getOperand(0).getSubReg() && "Expected SSA form"); 3376 3377 unsigned Opc = UseMI.getOpcode(); 3378 if (Opc == AMDGPU::COPY) { 3379 assert(!UseMI.getOperand(0).getSubReg() && "Expected SSA form"); 3380 3381 Register DstReg = UseMI.getOperand(0).getReg(); 3382 unsigned OpSize = getOpSize(UseMI, 0); 3383 bool Is16Bit = OpSize == 2; 3384 bool Is64Bit = OpSize == 8; 3385 bool isVGPRCopy = RI.isVGPR(*MRI, DstReg); 3386 unsigned NewOpc = isVGPRCopy ? Is64Bit ? AMDGPU::V_MOV_B64_PSEUDO 3387 : AMDGPU::V_MOV_B32_e32 3388 : Is64Bit ? AMDGPU::S_MOV_B64_IMM_PSEUDO 3389 : AMDGPU::S_MOV_B32; 3390 APInt Imm(Is64Bit ? 64 : 32, getImmFor(UseMI.getOperand(1))); 3391 3392 if (RI.isAGPR(*MRI, DstReg)) { 3393 if (Is64Bit || !isInlineConstant(Imm)) 3394 return false; 3395 NewOpc = AMDGPU::V_ACCVGPR_WRITE_B32_e64; 3396 } 3397 3398 if (Is16Bit) { 3399 if (isVGPRCopy) 3400 return false; // Do not clobber vgpr_hi16 3401 3402 if (DstReg.isVirtual() && UseMI.getOperand(0).getSubReg() != AMDGPU::lo16) 3403 return false; 3404 3405 UseMI.getOperand(0).setSubReg(0); 3406 if (DstReg.isPhysical()) { 3407 DstReg = RI.get32BitRegister(DstReg); 3408 UseMI.getOperand(0).setReg(DstReg); 3409 } 3410 assert(UseMI.getOperand(1).getReg().isVirtual()); 3411 } 3412 3413 const MCInstrDesc &NewMCID = get(NewOpc); 3414 if (DstReg.isPhysical() && 3415 !RI.getRegClass(NewMCID.operands()[0].RegClass)->contains(DstReg)) 3416 return false; 3417 3418 UseMI.setDesc(NewMCID); 3419 UseMI.getOperand(1).ChangeToImmediate(Imm.getSExtValue()); 3420 UseMI.addImplicitDefUseOperands(*UseMI.getParent()->getParent()); 3421 return true; 3422 } 3423 3424 if (Opc == AMDGPU::V_MAD_F32_e64 || Opc == AMDGPU::V_MAC_F32_e64 || 3425 Opc == AMDGPU::V_MAD_F16_e64 || Opc == AMDGPU::V_MAC_F16_e64 || 3426 Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64 || 3427 Opc == AMDGPU::V_FMA_F16_e64 || Opc == AMDGPU::V_FMAC_F16_e64 || 3428 Opc == AMDGPU::V_FMAC_F16_t16_e64) { 3429 // Don't fold if we are using source or output modifiers. The new VOP2 3430 // instructions don't have them. 3431 if (hasAnyModifiersSet(UseMI)) 3432 return false; 3433 3434 // If this is a free constant, there's no reason to do this. 3435 // TODO: We could fold this here instead of letting SIFoldOperands do it 3436 // later. 3437 MachineOperand *Src0 = getNamedOperand(UseMI, AMDGPU::OpName::src0); 3438 3439 // Any src operand can be used for the legality check. 3440 if (isInlineConstant(UseMI, *Src0, *ImmOp)) 3441 return false; 3442 3443 bool IsF32 = Opc == AMDGPU::V_MAD_F32_e64 || Opc == AMDGPU::V_MAC_F32_e64 || 3444 Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64; 3445 bool IsFMA = 3446 Opc == AMDGPU::V_FMA_F32_e64 || Opc == AMDGPU::V_FMAC_F32_e64 || 3447 Opc == AMDGPU::V_FMA_F16_e64 || Opc == AMDGPU::V_FMAC_F16_e64 || 3448 Opc == AMDGPU::V_FMAC_F16_t16_e64; 3449 MachineOperand *Src1 = getNamedOperand(UseMI, AMDGPU::OpName::src1); 3450 MachineOperand *Src2 = getNamedOperand(UseMI, AMDGPU::OpName::src2); 3451 3452 // Multiplied part is the constant: Use v_madmk_{f16, f32}. 3453 if ((Src0->isReg() && Src0->getReg() == Reg) || 3454 (Src1->isReg() && Src1->getReg() == Reg)) { 3455 MachineOperand *RegSrc = 3456 Src1->isReg() && Src1->getReg() == Reg ? Src0 : Src1; 3457 if (!RegSrc->isReg()) 3458 return false; 3459 if (RI.isSGPRClass(MRI->getRegClass(RegSrc->getReg())) && 3460 ST.getConstantBusLimit(Opc) < 2) 3461 return false; 3462 3463 if (!Src2->isReg() || RI.isSGPRClass(MRI->getRegClass(Src2->getReg()))) 3464 return false; 3465 3466 // If src2 is also a literal constant then we have to choose which one to 3467 // fold. In general it is better to choose madak so that the other literal 3468 // can be materialized in an sgpr instead of a vgpr: 3469 // s_mov_b32 s0, literal 3470 // v_madak_f32 v0, s0, v0, literal 3471 // Instead of: 3472 // v_mov_b32 v1, literal 3473 // v_madmk_f32 v0, v0, literal, v1 3474 MachineInstr *Def = MRI->getUniqueVRegDef(Src2->getReg()); 3475 if (Def && Def->isMoveImmediate() && 3476 !isInlineConstant(Def->getOperand(1))) 3477 return false; 3478 3479 unsigned NewOpc = 3480 IsFMA ? (IsF32 ? AMDGPU::V_FMAMK_F32 3481 : ST.hasTrue16BitInsts() ? AMDGPU::V_FMAMK_F16_t16 3482 : AMDGPU::V_FMAMK_F16) 3483 : (IsF32 ? AMDGPU::V_MADMK_F32 : AMDGPU::V_MADMK_F16); 3484 if (pseudoToMCOpcode(NewOpc) == -1) 3485 return false; 3486 3487 // V_FMAMK_F16_t16 takes VGPR_32_Lo128 operands, so the rewrite 3488 // would also require restricting their register classes. For now 3489 // just bail out. 3490 if (NewOpc == AMDGPU::V_FMAMK_F16_t16) 3491 return false; 3492 3493 const int64_t Imm = getImmFor(RegSrc == Src1 ? *Src0 : *Src1); 3494 3495 // FIXME: This would be a lot easier if we could return a new instruction 3496 // instead of having to modify in place. 3497 3498 Register SrcReg = RegSrc->getReg(); 3499 unsigned SrcSubReg = RegSrc->getSubReg(); 3500 Src0->setReg(SrcReg); 3501 Src0->setSubReg(SrcSubReg); 3502 Src0->setIsKill(RegSrc->isKill()); 3503 3504 if (Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64 || 3505 Opc == AMDGPU::V_FMAC_F32_e64 || Opc == AMDGPU::V_FMAC_F16_t16_e64 || 3506 Opc == AMDGPU::V_FMAC_F16_e64) 3507 UseMI.untieRegOperand( 3508 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)); 3509 3510 Src1->ChangeToImmediate(Imm); 3511 3512 removeModOperands(UseMI); 3513 UseMI.setDesc(get(NewOpc)); 3514 3515 bool DeleteDef = MRI->use_nodbg_empty(Reg); 3516 if (DeleteDef) 3517 DefMI.eraseFromParent(); 3518 3519 return true; 3520 } 3521 3522 // Added part is the constant: Use v_madak_{f16, f32}. 3523 if (Src2->isReg() && Src2->getReg() == Reg) { 3524 if (ST.getConstantBusLimit(Opc) < 2) { 3525 // Not allowed to use constant bus for another operand. 3526 // We can however allow an inline immediate as src0. 3527 bool Src0Inlined = false; 3528 if (Src0->isReg()) { 3529 // Try to inline constant if possible. 3530 // If the Def moves immediate and the use is single 3531 // We are saving VGPR here. 3532 MachineInstr *Def = MRI->getUniqueVRegDef(Src0->getReg()); 3533 if (Def && Def->isMoveImmediate() && 3534 isInlineConstant(Def->getOperand(1)) && 3535 MRI->hasOneUse(Src0->getReg())) { 3536 Src0->ChangeToImmediate(Def->getOperand(1).getImm()); 3537 Src0Inlined = true; 3538 } else if (ST.getConstantBusLimit(Opc) <= 1 && 3539 RI.isSGPRReg(*MRI, Src0->getReg())) { 3540 return false; 3541 } 3542 // VGPR is okay as Src0 - fallthrough 3543 } 3544 3545 if (Src1->isReg() && !Src0Inlined) { 3546 // We have one slot for inlinable constant so far - try to fill it 3547 MachineInstr *Def = MRI->getUniqueVRegDef(Src1->getReg()); 3548 if (Def && Def->isMoveImmediate() && 3549 isInlineConstant(Def->getOperand(1)) && 3550 MRI->hasOneUse(Src1->getReg()) && commuteInstruction(UseMI)) 3551 Src0->ChangeToImmediate(Def->getOperand(1).getImm()); 3552 else if (RI.isSGPRReg(*MRI, Src1->getReg())) 3553 return false; 3554 // VGPR is okay as Src1 - fallthrough 3555 } 3556 } 3557 3558 unsigned NewOpc = 3559 IsFMA ? (IsF32 ? AMDGPU::V_FMAAK_F32 3560 : ST.hasTrue16BitInsts() ? AMDGPU::V_FMAAK_F16_t16 3561 : AMDGPU::V_FMAAK_F16) 3562 : (IsF32 ? AMDGPU::V_MADAK_F32 : AMDGPU::V_MADAK_F16); 3563 if (pseudoToMCOpcode(NewOpc) == -1) 3564 return false; 3565 3566 // V_FMAAK_F16_t16 takes VGPR_32_Lo128 operands, so the rewrite 3567 // would also require restricting their register classes. For now 3568 // just bail out. 3569 if (NewOpc == AMDGPU::V_FMAAK_F16_t16) 3570 return false; 3571 3572 // FIXME: This would be a lot easier if we could return a new instruction 3573 // instead of having to modify in place. 3574 3575 if (Opc == AMDGPU::V_MAC_F32_e64 || Opc == AMDGPU::V_MAC_F16_e64 || 3576 Opc == AMDGPU::V_FMAC_F32_e64 || Opc == AMDGPU::V_FMAC_F16_t16_e64 || 3577 Opc == AMDGPU::V_FMAC_F16_e64) 3578 UseMI.untieRegOperand( 3579 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)); 3580 3581 // ChangingToImmediate adds Src2 back to the instruction. 3582 Src2->ChangeToImmediate(getImmFor(*Src2)); 3583 3584 // These come before src2. 3585 removeModOperands(UseMI); 3586 UseMI.setDesc(get(NewOpc)); 3587 // It might happen that UseMI was commuted 3588 // and we now have SGPR as SRC1. If so 2 inlined 3589 // constant and SGPR are illegal. 3590 legalizeOperands(UseMI); 3591 3592 bool DeleteDef = MRI->use_nodbg_empty(Reg); 3593 if (DeleteDef) 3594 DefMI.eraseFromParent(); 3595 3596 return true; 3597 } 3598 } 3599 3600 return false; 3601 } 3602 3603 static bool 3604 memOpsHaveSameBaseOperands(ArrayRef<const MachineOperand *> BaseOps1, 3605 ArrayRef<const MachineOperand *> BaseOps2) { 3606 if (BaseOps1.size() != BaseOps2.size()) 3607 return false; 3608 for (size_t I = 0, E = BaseOps1.size(); I < E; ++I) { 3609 if (!BaseOps1[I]->isIdenticalTo(*BaseOps2[I])) 3610 return false; 3611 } 3612 return true; 3613 } 3614 3615 static bool offsetsDoNotOverlap(int WidthA, int OffsetA, 3616 int WidthB, int OffsetB) { 3617 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB; 3618 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA; 3619 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; 3620 return LowOffset + LowWidth <= HighOffset; 3621 } 3622 3623 bool SIInstrInfo::checkInstOffsetsDoNotOverlap(const MachineInstr &MIa, 3624 const MachineInstr &MIb) const { 3625 SmallVector<const MachineOperand *, 4> BaseOps0, BaseOps1; 3626 int64_t Offset0, Offset1; 3627 unsigned Dummy0, Dummy1; 3628 bool Offset0IsScalable, Offset1IsScalable; 3629 if (!getMemOperandsWithOffsetWidth(MIa, BaseOps0, Offset0, Offset0IsScalable, 3630 Dummy0, &RI) || 3631 !getMemOperandsWithOffsetWidth(MIb, BaseOps1, Offset1, Offset1IsScalable, 3632 Dummy1, &RI)) 3633 return false; 3634 3635 if (!memOpsHaveSameBaseOperands(BaseOps0, BaseOps1)) 3636 return false; 3637 3638 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand()) { 3639 // FIXME: Handle ds_read2 / ds_write2. 3640 return false; 3641 } 3642 unsigned Width0 = MIa.memoperands().front()->getSize(); 3643 unsigned Width1 = MIb.memoperands().front()->getSize(); 3644 return offsetsDoNotOverlap(Width0, Offset0, Width1, Offset1); 3645 } 3646 3647 bool SIInstrInfo::areMemAccessesTriviallyDisjoint(const MachineInstr &MIa, 3648 const MachineInstr &MIb) const { 3649 assert(MIa.mayLoadOrStore() && 3650 "MIa must load from or modify a memory location"); 3651 assert(MIb.mayLoadOrStore() && 3652 "MIb must load from or modify a memory location"); 3653 3654 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects()) 3655 return false; 3656 3657 // XXX - Can we relax this between address spaces? 3658 if (MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef()) 3659 return false; 3660 3661 if (isLDSDMA(MIa) || isLDSDMA(MIb)) 3662 return false; 3663 3664 // TODO: Should we check the address space from the MachineMemOperand? That 3665 // would allow us to distinguish objects we know don't alias based on the 3666 // underlying address space, even if it was lowered to a different one, 3667 // e.g. private accesses lowered to use MUBUF instructions on a scratch 3668 // buffer. 3669 if (isDS(MIa)) { 3670 if (isDS(MIb)) 3671 return checkInstOffsetsDoNotOverlap(MIa, MIb); 3672 3673 return !isFLAT(MIb) || isSegmentSpecificFLAT(MIb); 3674 } 3675 3676 if (isMUBUF(MIa) || isMTBUF(MIa)) { 3677 if (isMUBUF(MIb) || isMTBUF(MIb)) 3678 return checkInstOffsetsDoNotOverlap(MIa, MIb); 3679 3680 if (isFLAT(MIb)) 3681 return isFLATScratch(MIb); 3682 3683 return !isSMRD(MIb); 3684 } 3685 3686 if (isSMRD(MIa)) { 3687 if (isSMRD(MIb)) 3688 return checkInstOffsetsDoNotOverlap(MIa, MIb); 3689 3690 if (isFLAT(MIb)) 3691 return isFLATScratch(MIb); 3692 3693 return !isMUBUF(MIb) && !isMTBUF(MIb); 3694 } 3695 3696 if (isFLAT(MIa)) { 3697 if (isFLAT(MIb)) { 3698 if ((isFLATScratch(MIa) && isFLATGlobal(MIb)) || 3699 (isFLATGlobal(MIa) && isFLATScratch(MIb))) 3700 return true; 3701 3702 return checkInstOffsetsDoNotOverlap(MIa, MIb); 3703 } 3704 3705 return false; 3706 } 3707 3708 return false; 3709 } 3710 3711 static bool getFoldableImm(Register Reg, const MachineRegisterInfo &MRI, 3712 int64_t &Imm, MachineInstr **DefMI = nullptr) { 3713 if (Reg.isPhysical()) 3714 return false; 3715 auto *Def = MRI.getUniqueVRegDef(Reg); 3716 if (Def && SIInstrInfo::isFoldableCopy(*Def) && Def->getOperand(1).isImm()) { 3717 Imm = Def->getOperand(1).getImm(); 3718 if (DefMI) 3719 *DefMI = Def; 3720 return true; 3721 } 3722 return false; 3723 } 3724 3725 static bool getFoldableImm(const MachineOperand *MO, int64_t &Imm, 3726 MachineInstr **DefMI = nullptr) { 3727 if (!MO->isReg()) 3728 return false; 3729 const MachineFunction *MF = MO->getParent()->getParent()->getParent(); 3730 const MachineRegisterInfo &MRI = MF->getRegInfo(); 3731 return getFoldableImm(MO->getReg(), MRI, Imm, DefMI); 3732 } 3733 3734 static void updateLiveVariables(LiveVariables *LV, MachineInstr &MI, 3735 MachineInstr &NewMI) { 3736 if (LV) { 3737 unsigned NumOps = MI.getNumOperands(); 3738 for (unsigned I = 1; I < NumOps; ++I) { 3739 MachineOperand &Op = MI.getOperand(I); 3740 if (Op.isReg() && Op.isKill()) 3741 LV->replaceKillInstruction(Op.getReg(), MI, NewMI); 3742 } 3743 } 3744 } 3745 3746 MachineInstr *SIInstrInfo::convertToThreeAddress(MachineInstr &MI, 3747 LiveVariables *LV, 3748 LiveIntervals *LIS) const { 3749 MachineBasicBlock &MBB = *MI.getParent(); 3750 unsigned Opc = MI.getOpcode(); 3751 3752 // Handle MFMA. 3753 int NewMFMAOpc = AMDGPU::getMFMAEarlyClobberOp(Opc); 3754 if (NewMFMAOpc != -1) { 3755 MachineInstrBuilder MIB = 3756 BuildMI(MBB, MI, MI.getDebugLoc(), get(NewMFMAOpc)); 3757 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) 3758 MIB.add(MI.getOperand(I)); 3759 updateLiveVariables(LV, MI, *MIB); 3760 if (LIS) 3761 LIS->ReplaceMachineInstrInMaps(MI, *MIB); 3762 return MIB; 3763 } 3764 3765 if (SIInstrInfo::isWMMA(MI)) { 3766 unsigned NewOpc = AMDGPU::mapWMMA2AddrTo3AddrOpcode(MI.getOpcode()); 3767 MachineInstrBuilder MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc)) 3768 .setMIFlags(MI.getFlags()); 3769 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) 3770 MIB->addOperand(MI.getOperand(I)); 3771 3772 updateLiveVariables(LV, MI, *MIB); 3773 if (LIS) 3774 LIS->ReplaceMachineInstrInMaps(MI, *MIB); 3775 3776 return MIB; 3777 } 3778 3779 assert(Opc != AMDGPU::V_FMAC_F16_t16_e32 && 3780 "V_FMAC_F16_t16_e32 is not supported and not expected to be present " 3781 "pre-RA"); 3782 3783 // Handle MAC/FMAC. 3784 bool IsF16 = Opc == AMDGPU::V_MAC_F16_e32 || Opc == AMDGPU::V_MAC_F16_e64 || 3785 Opc == AMDGPU::V_FMAC_F16_e32 || Opc == AMDGPU::V_FMAC_F16_e64 || 3786 Opc == AMDGPU::V_FMAC_F16_t16_e64; 3787 bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e32 || Opc == AMDGPU::V_FMAC_F32_e64 || 3788 Opc == AMDGPU::V_FMAC_LEGACY_F32_e32 || 3789 Opc == AMDGPU::V_FMAC_LEGACY_F32_e64 || 3790 Opc == AMDGPU::V_FMAC_F16_e32 || Opc == AMDGPU::V_FMAC_F16_e64 || 3791 Opc == AMDGPU::V_FMAC_F16_t16_e64 || 3792 Opc == AMDGPU::V_FMAC_F64_e32 || Opc == AMDGPU::V_FMAC_F64_e64; 3793 bool IsF64 = Opc == AMDGPU::V_FMAC_F64_e32 || Opc == AMDGPU::V_FMAC_F64_e64; 3794 bool IsLegacy = Opc == AMDGPU::V_MAC_LEGACY_F32_e32 || 3795 Opc == AMDGPU::V_MAC_LEGACY_F32_e64 || 3796 Opc == AMDGPU::V_FMAC_LEGACY_F32_e32 || 3797 Opc == AMDGPU::V_FMAC_LEGACY_F32_e64; 3798 bool Src0Literal = false; 3799 3800 switch (Opc) { 3801 default: 3802 return nullptr; 3803 case AMDGPU::V_MAC_F16_e64: 3804 case AMDGPU::V_FMAC_F16_e64: 3805 case AMDGPU::V_FMAC_F16_t16_e64: 3806 case AMDGPU::V_MAC_F32_e64: 3807 case AMDGPU::V_MAC_LEGACY_F32_e64: 3808 case AMDGPU::V_FMAC_F32_e64: 3809 case AMDGPU::V_FMAC_LEGACY_F32_e64: 3810 case AMDGPU::V_FMAC_F64_e64: 3811 break; 3812 case AMDGPU::V_MAC_F16_e32: 3813 case AMDGPU::V_FMAC_F16_e32: 3814 case AMDGPU::V_MAC_F32_e32: 3815 case AMDGPU::V_MAC_LEGACY_F32_e32: 3816 case AMDGPU::V_FMAC_F32_e32: 3817 case AMDGPU::V_FMAC_LEGACY_F32_e32: 3818 case AMDGPU::V_FMAC_F64_e32: { 3819 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), 3820 AMDGPU::OpName::src0); 3821 const MachineOperand *Src0 = &MI.getOperand(Src0Idx); 3822 if (!Src0->isReg() && !Src0->isImm()) 3823 return nullptr; 3824 3825 if (Src0->isImm() && !isInlineConstant(MI, Src0Idx, *Src0)) 3826 Src0Literal = true; 3827 3828 break; 3829 } 3830 } 3831 3832 MachineInstrBuilder MIB; 3833 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 3834 const MachineOperand *Src0 = getNamedOperand(MI, AMDGPU::OpName::src0); 3835 const MachineOperand *Src0Mods = 3836 getNamedOperand(MI, AMDGPU::OpName::src0_modifiers); 3837 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1); 3838 const MachineOperand *Src1Mods = 3839 getNamedOperand(MI, AMDGPU::OpName::src1_modifiers); 3840 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2); 3841 const MachineOperand *Src2Mods = 3842 getNamedOperand(MI, AMDGPU::OpName::src2_modifiers); 3843 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp); 3844 const MachineOperand *Omod = getNamedOperand(MI, AMDGPU::OpName::omod); 3845 const MachineOperand *OpSel = getNamedOperand(MI, AMDGPU::OpName::op_sel); 3846 3847 if (!Src0Mods && !Src1Mods && !Src2Mods && !Clamp && !Omod && !IsF64 && 3848 !IsLegacy && 3849 // If we have an SGPR input, we will violate the constant bus restriction. 3850 (ST.getConstantBusLimit(Opc) > 1 || !Src0->isReg() || 3851 !RI.isSGPRReg(MBB.getParent()->getRegInfo(), Src0->getReg()))) { 3852 MachineInstr *DefMI; 3853 const auto killDef = [&]() -> void { 3854 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3855 // The only user is the instruction which will be killed. 3856 Register DefReg = DefMI->getOperand(0).getReg(); 3857 if (!MRI.hasOneNonDBGUse(DefReg)) 3858 return; 3859 // We cannot just remove the DefMI here, calling pass will crash. 3860 DefMI->setDesc(get(AMDGPU::IMPLICIT_DEF)); 3861 for (unsigned I = DefMI->getNumOperands() - 1; I != 0; --I) 3862 DefMI->removeOperand(I); 3863 if (LV) 3864 LV->getVarInfo(DefReg).AliveBlocks.clear(); 3865 }; 3866 3867 int64_t Imm; 3868 if (!Src0Literal && getFoldableImm(Src2, Imm, &DefMI)) { 3869 unsigned NewOpc = 3870 IsFMA ? (IsF16 ? (ST.hasTrue16BitInsts() ? AMDGPU::V_FMAAK_F16_t16 3871 : AMDGPU::V_FMAAK_F16) 3872 : AMDGPU::V_FMAAK_F32) 3873 : (IsF16 ? AMDGPU::V_MADAK_F16 : AMDGPU::V_MADAK_F32); 3874 if (pseudoToMCOpcode(NewOpc) != -1) { 3875 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc)) 3876 .add(*Dst) 3877 .add(*Src0) 3878 .add(*Src1) 3879 .addImm(Imm); 3880 updateLiveVariables(LV, MI, *MIB); 3881 if (LIS) 3882 LIS->ReplaceMachineInstrInMaps(MI, *MIB); 3883 killDef(); 3884 return MIB; 3885 } 3886 } 3887 unsigned NewOpc = 3888 IsFMA ? (IsF16 ? (ST.hasTrue16BitInsts() ? AMDGPU::V_FMAMK_F16_t16 3889 : AMDGPU::V_FMAMK_F16) 3890 : AMDGPU::V_FMAMK_F32) 3891 : (IsF16 ? AMDGPU::V_MADMK_F16 : AMDGPU::V_MADMK_F32); 3892 if (!Src0Literal && getFoldableImm(Src1, Imm, &DefMI)) { 3893 if (pseudoToMCOpcode(NewOpc) != -1) { 3894 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc)) 3895 .add(*Dst) 3896 .add(*Src0) 3897 .addImm(Imm) 3898 .add(*Src2); 3899 updateLiveVariables(LV, MI, *MIB); 3900 if (LIS) 3901 LIS->ReplaceMachineInstrInMaps(MI, *MIB); 3902 killDef(); 3903 return MIB; 3904 } 3905 } 3906 if (Src0Literal || getFoldableImm(Src0, Imm, &DefMI)) { 3907 if (Src0Literal) { 3908 Imm = Src0->getImm(); 3909 DefMI = nullptr; 3910 } 3911 if (pseudoToMCOpcode(NewOpc) != -1 && 3912 isOperandLegal( 3913 MI, AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::src0), 3914 Src1)) { 3915 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc)) 3916 .add(*Dst) 3917 .add(*Src1) 3918 .addImm(Imm) 3919 .add(*Src2); 3920 updateLiveVariables(LV, MI, *MIB); 3921 if (LIS) 3922 LIS->ReplaceMachineInstrInMaps(MI, *MIB); 3923 if (DefMI) 3924 killDef(); 3925 return MIB; 3926 } 3927 } 3928 } 3929 3930 // VOP2 mac/fmac with a literal operand cannot be converted to VOP3 mad/fma 3931 // if VOP3 does not allow a literal operand. 3932 if (Src0Literal && !ST.hasVOP3Literal()) 3933 return nullptr; 3934 3935 unsigned NewOpc = IsFMA ? IsF16 ? AMDGPU::V_FMA_F16_gfx9_e64 3936 : IsF64 ? AMDGPU::V_FMA_F64_e64 3937 : IsLegacy 3938 ? AMDGPU::V_FMA_LEGACY_F32_e64 3939 : AMDGPU::V_FMA_F32_e64 3940 : IsF16 ? AMDGPU::V_MAD_F16_e64 3941 : IsLegacy ? AMDGPU::V_MAD_LEGACY_F32_e64 3942 : AMDGPU::V_MAD_F32_e64; 3943 if (pseudoToMCOpcode(NewOpc) == -1) 3944 return nullptr; 3945 3946 MIB = BuildMI(MBB, MI, MI.getDebugLoc(), get(NewOpc)) 3947 .add(*Dst) 3948 .addImm(Src0Mods ? Src0Mods->getImm() : 0) 3949 .add(*Src0) 3950 .addImm(Src1Mods ? Src1Mods->getImm() : 0) 3951 .add(*Src1) 3952 .addImm(Src2Mods ? Src2Mods->getImm() : 0) 3953 .add(*Src2) 3954 .addImm(Clamp ? Clamp->getImm() : 0) 3955 .addImm(Omod ? Omod->getImm() : 0); 3956 if (AMDGPU::hasNamedOperand(NewOpc, AMDGPU::OpName::op_sel)) 3957 MIB.addImm(OpSel ? OpSel->getImm() : 0); 3958 updateLiveVariables(LV, MI, *MIB); 3959 if (LIS) 3960 LIS->ReplaceMachineInstrInMaps(MI, *MIB); 3961 return MIB; 3962 } 3963 3964 // It's not generally safe to move VALU instructions across these since it will 3965 // start using the register as a base index rather than directly. 3966 // XXX - Why isn't hasSideEffects sufficient for these? 3967 static bool changesVGPRIndexingMode(const MachineInstr &MI) { 3968 switch (MI.getOpcode()) { 3969 case AMDGPU::S_SET_GPR_IDX_ON: 3970 case AMDGPU::S_SET_GPR_IDX_MODE: 3971 case AMDGPU::S_SET_GPR_IDX_OFF: 3972 return true; 3973 default: 3974 return false; 3975 } 3976 } 3977 3978 bool SIInstrInfo::isSchedulingBoundary(const MachineInstr &MI, 3979 const MachineBasicBlock *MBB, 3980 const MachineFunction &MF) const { 3981 // Skipping the check for SP writes in the base implementation. The reason it 3982 // was added was apparently due to compile time concerns. 3983 // 3984 // TODO: Do we really want this barrier? It triggers unnecessary hazard nops 3985 // but is probably avoidable. 3986 3987 // Copied from base implementation. 3988 // Terminators and labels can't be scheduled around. 3989 if (MI.isTerminator() || MI.isPosition()) 3990 return true; 3991 3992 // INLINEASM_BR can jump to another block 3993 if (MI.getOpcode() == TargetOpcode::INLINEASM_BR) 3994 return true; 3995 3996 if (MI.getOpcode() == AMDGPU::SCHED_BARRIER && MI.getOperand(0).getImm() == 0) 3997 return true; 3998 3999 // Target-independent instructions do not have an implicit-use of EXEC, even 4000 // when they operate on VGPRs. Treating EXEC modifications as scheduling 4001 // boundaries prevents incorrect movements of such instructions. 4002 return MI.modifiesRegister(AMDGPU::EXEC, &RI) || 4003 MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 || 4004 MI.getOpcode() == AMDGPU::S_SETREG_B32 || 4005 MI.getOpcode() == AMDGPU::S_SETPRIO || 4006 changesVGPRIndexingMode(MI); 4007 } 4008 4009 bool SIInstrInfo::isAlwaysGDS(uint16_t Opcode) const { 4010 return Opcode == AMDGPU::DS_ORDERED_COUNT || isGWS(Opcode); 4011 } 4012 4013 bool SIInstrInfo::modifiesModeRegister(const MachineInstr &MI) { 4014 // Skip the full operand and register alias search modifiesRegister 4015 // does. There's only a handful of instructions that touch this, it's only an 4016 // implicit def, and doesn't alias any other registers. 4017 return is_contained(MI.getDesc().implicit_defs(), AMDGPU::MODE); 4018 } 4019 4020 bool SIInstrInfo::hasUnwantedEffectsWhenEXECEmpty(const MachineInstr &MI) const { 4021 unsigned Opcode = MI.getOpcode(); 4022 4023 if (MI.mayStore() && isSMRD(MI)) 4024 return true; // scalar store or atomic 4025 4026 // This will terminate the function when other lanes may need to continue. 4027 if (MI.isReturn()) 4028 return true; 4029 4030 // These instructions cause shader I/O that may cause hardware lockups 4031 // when executed with an empty EXEC mask. 4032 // 4033 // Note: exp with VM = DONE = 0 is automatically skipped by hardware when 4034 // EXEC = 0, but checking for that case here seems not worth it 4035 // given the typical code patterns. 4036 if (Opcode == AMDGPU::S_SENDMSG || Opcode == AMDGPU::S_SENDMSGHALT || 4037 isEXP(Opcode) || 4038 Opcode == AMDGPU::DS_ORDERED_COUNT || Opcode == AMDGPU::S_TRAP || 4039 Opcode == AMDGPU::DS_GWS_INIT || Opcode == AMDGPU::DS_GWS_BARRIER) 4040 return true; 4041 4042 if (MI.isCall() || MI.isInlineAsm()) 4043 return true; // conservative assumption 4044 4045 // A mode change is a scalar operation that influences vector instructions. 4046 if (modifiesModeRegister(MI)) 4047 return true; 4048 4049 // These are like SALU instructions in terms of effects, so it's questionable 4050 // whether we should return true for those. 4051 // 4052 // However, executing them with EXEC = 0 causes them to operate on undefined 4053 // data, which we avoid by returning true here. 4054 if (Opcode == AMDGPU::V_READFIRSTLANE_B32 || 4055 Opcode == AMDGPU::V_READLANE_B32 || Opcode == AMDGPU::V_WRITELANE_B32 || 4056 Opcode == AMDGPU::SI_RESTORE_S32_FROM_VGPR || 4057 Opcode == AMDGPU::SI_SPILL_S32_TO_VGPR) 4058 return true; 4059 4060 return false; 4061 } 4062 4063 bool SIInstrInfo::mayReadEXEC(const MachineRegisterInfo &MRI, 4064 const MachineInstr &MI) const { 4065 if (MI.isMetaInstruction()) 4066 return false; 4067 4068 // This won't read exec if this is an SGPR->SGPR copy. 4069 if (MI.isCopyLike()) { 4070 if (!RI.isSGPRReg(MRI, MI.getOperand(0).getReg())) 4071 return true; 4072 4073 // Make sure this isn't copying exec as a normal operand 4074 return MI.readsRegister(AMDGPU::EXEC, &RI); 4075 } 4076 4077 // Make a conservative assumption about the callee. 4078 if (MI.isCall()) 4079 return true; 4080 4081 // Be conservative with any unhandled generic opcodes. 4082 if (!isTargetSpecificOpcode(MI.getOpcode())) 4083 return true; 4084 4085 return !isSALU(MI) || MI.readsRegister(AMDGPU::EXEC, &RI); 4086 } 4087 4088 bool SIInstrInfo::isInlineConstant(const APInt &Imm) const { 4089 switch (Imm.getBitWidth()) { 4090 case 1: // This likely will be a condition code mask. 4091 return true; 4092 4093 case 32: 4094 return AMDGPU::isInlinableLiteral32(Imm.getSExtValue(), 4095 ST.hasInv2PiInlineImm()); 4096 case 64: 4097 return AMDGPU::isInlinableLiteral64(Imm.getSExtValue(), 4098 ST.hasInv2PiInlineImm()); 4099 case 16: 4100 return ST.has16BitInsts() && 4101 AMDGPU::isInlinableLiteral16(Imm.getSExtValue(), 4102 ST.hasInv2PiInlineImm()); 4103 default: 4104 llvm_unreachable("invalid bitwidth"); 4105 } 4106 } 4107 4108 bool SIInstrInfo::isInlineConstant(const MachineOperand &MO, 4109 uint8_t OperandType) const { 4110 assert(!MO.isReg() && "isInlineConstant called on register operand!"); 4111 if (!MO.isImm()) 4112 return false; 4113 4114 // MachineOperand provides no way to tell the true operand size, since it only 4115 // records a 64-bit value. We need to know the size to determine if a 32-bit 4116 // floating point immediate bit pattern is legal for an integer immediate. It 4117 // would be for any 32-bit integer operand, but would not be for a 64-bit one. 4118 4119 int64_t Imm = MO.getImm(); 4120 switch (OperandType) { 4121 case AMDGPU::OPERAND_REG_IMM_INT32: 4122 case AMDGPU::OPERAND_REG_IMM_FP32: 4123 case AMDGPU::OPERAND_REG_IMM_FP32_DEFERRED: 4124 case AMDGPU::OPERAND_REG_INLINE_C_INT32: 4125 case AMDGPU::OPERAND_REG_INLINE_C_FP32: 4126 case AMDGPU::OPERAND_REG_IMM_V2FP32: 4127 case AMDGPU::OPERAND_REG_INLINE_C_V2FP32: 4128 case AMDGPU::OPERAND_REG_IMM_V2INT32: 4129 case AMDGPU::OPERAND_REG_INLINE_C_V2INT32: 4130 case AMDGPU::OPERAND_REG_INLINE_AC_INT32: 4131 case AMDGPU::OPERAND_REG_INLINE_AC_FP32: 4132 case AMDGPU::OPERAND_INLINE_SPLIT_BARRIER_INT32: { 4133 int32_t Trunc = static_cast<int32_t>(Imm); 4134 return AMDGPU::isInlinableLiteral32(Trunc, ST.hasInv2PiInlineImm()); 4135 } 4136 case AMDGPU::OPERAND_REG_IMM_INT64: 4137 case AMDGPU::OPERAND_REG_IMM_FP64: 4138 case AMDGPU::OPERAND_REG_INLINE_C_INT64: 4139 case AMDGPU::OPERAND_REG_INLINE_C_FP64: 4140 case AMDGPU::OPERAND_REG_INLINE_AC_FP64: 4141 return AMDGPU::isInlinableLiteral64(MO.getImm(), 4142 ST.hasInv2PiInlineImm()); 4143 case AMDGPU::OPERAND_REG_IMM_INT16: 4144 case AMDGPU::OPERAND_REG_INLINE_C_INT16: 4145 case AMDGPU::OPERAND_REG_INLINE_AC_INT16: 4146 // We would expect inline immediates to not be concerned with an integer/fp 4147 // distinction. However, in the case of 16-bit integer operations, the 4148 // "floating point" values appear to not work. It seems read the low 16-bits 4149 // of 32-bit immediates, which happens to always work for the integer 4150 // values. 4151 // 4152 // See llvm bugzilla 46302. 4153 // 4154 // TODO: Theoretically we could use op-sel to use the high bits of the 4155 // 32-bit FP values. 4156 return AMDGPU::isInlinableIntLiteral(Imm); 4157 case AMDGPU::OPERAND_REG_IMM_V2INT16: 4158 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16: 4159 case AMDGPU::OPERAND_REG_INLINE_AC_V2INT16: 4160 return (isInt<16>(Imm) || isUInt<16>(Imm)) && 4161 AMDGPU::isInlinableIntLiteral((int16_t)Imm); 4162 case AMDGPU::OPERAND_REG_IMM_FP16: 4163 case AMDGPU::OPERAND_REG_IMM_FP16_DEFERRED: 4164 case AMDGPU::OPERAND_REG_INLINE_C_FP16: 4165 case AMDGPU::OPERAND_REG_INLINE_AC_FP16: 4166 case AMDGPU::OPERAND_REG_IMM_V2FP16: 4167 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16: 4168 case AMDGPU::OPERAND_REG_INLINE_AC_V2FP16: { 4169 if (isInt<16>(Imm) || isUInt<16>(Imm)) { 4170 // A few special case instructions have 16-bit operands on subtargets 4171 // where 16-bit instructions are not legal. 4172 // TODO: Do the 32-bit immediates work? We shouldn't really need to handle 4173 // constants in these cases 4174 int16_t Trunc = static_cast<int16_t>(Imm); 4175 return ST.has16BitInsts() && 4176 AMDGPU::isInlinableLiteral16(Trunc, ST.hasInv2PiInlineImm()); 4177 } 4178 4179 return false; 4180 } 4181 case AMDGPU::OPERAND_KIMM32: 4182 case AMDGPU::OPERAND_KIMM16: 4183 return false; 4184 case AMDGPU::OPERAND_INPUT_MODS: 4185 case MCOI::OPERAND_IMMEDIATE: 4186 // Always embedded in the instruction for free. 4187 return true; 4188 case MCOI::OPERAND_UNKNOWN: 4189 case MCOI::OPERAND_REGISTER: 4190 case MCOI::OPERAND_PCREL: 4191 case MCOI::OPERAND_GENERIC_0: 4192 case MCOI::OPERAND_GENERIC_1: 4193 case MCOI::OPERAND_GENERIC_2: 4194 case MCOI::OPERAND_GENERIC_3: 4195 case MCOI::OPERAND_GENERIC_4: 4196 case MCOI::OPERAND_GENERIC_5: 4197 // Just ignore anything else. 4198 return true; 4199 default: 4200 llvm_unreachable("invalid operand type"); 4201 } 4202 } 4203 4204 static bool compareMachineOp(const MachineOperand &Op0, 4205 const MachineOperand &Op1) { 4206 if (Op0.getType() != Op1.getType()) 4207 return false; 4208 4209 switch (Op0.getType()) { 4210 case MachineOperand::MO_Register: 4211 return Op0.getReg() == Op1.getReg(); 4212 case MachineOperand::MO_Immediate: 4213 return Op0.getImm() == Op1.getImm(); 4214 default: 4215 llvm_unreachable("Didn't expect to be comparing these operand types"); 4216 } 4217 } 4218 4219 bool SIInstrInfo::isImmOperandLegal(const MachineInstr &MI, unsigned OpNo, 4220 const MachineOperand &MO) const { 4221 const MCInstrDesc &InstDesc = MI.getDesc(); 4222 const MCOperandInfo &OpInfo = InstDesc.operands()[OpNo]; 4223 4224 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal()); 4225 4226 if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE) 4227 return true; 4228 4229 if (OpInfo.RegClass < 0) 4230 return false; 4231 4232 if (MO.isImm() && isInlineConstant(MO, OpInfo)) { 4233 if (isMAI(MI) && ST.hasMFMAInlineLiteralBug() && 4234 OpNo ==(unsigned)AMDGPU::getNamedOperandIdx(MI.getOpcode(), 4235 AMDGPU::OpName::src2)) 4236 return false; 4237 return RI.opCanUseInlineConstant(OpInfo.OperandType); 4238 } 4239 4240 if (!RI.opCanUseLiteralConstant(OpInfo.OperandType)) 4241 return false; 4242 4243 if (!isVOP3(MI) || !AMDGPU::isSISrcOperand(InstDesc, OpNo)) 4244 return true; 4245 4246 return ST.hasVOP3Literal(); 4247 } 4248 4249 bool SIInstrInfo::hasVALU32BitEncoding(unsigned Opcode) const { 4250 // GFX90A does not have V_MUL_LEGACY_F32_e32. 4251 if (Opcode == AMDGPU::V_MUL_LEGACY_F32_e64 && ST.hasGFX90AInsts()) 4252 return false; 4253 4254 int Op32 = AMDGPU::getVOPe32(Opcode); 4255 if (Op32 == -1) 4256 return false; 4257 4258 return pseudoToMCOpcode(Op32) != -1; 4259 } 4260 4261 bool SIInstrInfo::hasModifiers(unsigned Opcode) const { 4262 // The src0_modifier operand is present on all instructions 4263 // that have modifiers. 4264 4265 return AMDGPU::hasNamedOperand(Opcode, AMDGPU::OpName::src0_modifiers); 4266 } 4267 4268 bool SIInstrInfo::hasModifiersSet(const MachineInstr &MI, 4269 unsigned OpName) const { 4270 const MachineOperand *Mods = getNamedOperand(MI, OpName); 4271 return Mods && Mods->getImm(); 4272 } 4273 4274 bool SIInstrInfo::hasAnyModifiersSet(const MachineInstr &MI) const { 4275 return any_of(ModifierOpNames, 4276 [&](unsigned Name) { return hasModifiersSet(MI, Name); }); 4277 } 4278 4279 bool SIInstrInfo::canShrink(const MachineInstr &MI, 4280 const MachineRegisterInfo &MRI) const { 4281 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2); 4282 // Can't shrink instruction with three operands. 4283 if (Src2) { 4284 switch (MI.getOpcode()) { 4285 default: return false; 4286 4287 case AMDGPU::V_ADDC_U32_e64: 4288 case AMDGPU::V_SUBB_U32_e64: 4289 case AMDGPU::V_SUBBREV_U32_e64: { 4290 const MachineOperand *Src1 4291 = getNamedOperand(MI, AMDGPU::OpName::src1); 4292 if (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg())) 4293 return false; 4294 // Additional verification is needed for sdst/src2. 4295 return true; 4296 } 4297 case AMDGPU::V_MAC_F16_e64: 4298 case AMDGPU::V_MAC_F32_e64: 4299 case AMDGPU::V_MAC_LEGACY_F32_e64: 4300 case AMDGPU::V_FMAC_F16_e64: 4301 case AMDGPU::V_FMAC_F16_t16_e64: 4302 case AMDGPU::V_FMAC_F32_e64: 4303 case AMDGPU::V_FMAC_F64_e64: 4304 case AMDGPU::V_FMAC_LEGACY_F32_e64: 4305 if (!Src2->isReg() || !RI.isVGPR(MRI, Src2->getReg()) || 4306 hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers)) 4307 return false; 4308 break; 4309 4310 case AMDGPU::V_CNDMASK_B32_e64: 4311 break; 4312 } 4313 } 4314 4315 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1); 4316 if (Src1 && (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg()) || 4317 hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers))) 4318 return false; 4319 4320 // We don't need to check src0, all input types are legal, so just make sure 4321 // src0 isn't using any modifiers. 4322 if (hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers)) 4323 return false; 4324 4325 // Can it be shrunk to a valid 32 bit opcode? 4326 if (!hasVALU32BitEncoding(MI.getOpcode())) 4327 return false; 4328 4329 // Check output modifiers 4330 return !hasModifiersSet(MI, AMDGPU::OpName::omod) && 4331 !hasModifiersSet(MI, AMDGPU::OpName::clamp); 4332 } 4333 4334 // Set VCC operand with all flags from \p Orig, except for setting it as 4335 // implicit. 4336 static void copyFlagsToImplicitVCC(MachineInstr &MI, 4337 const MachineOperand &Orig) { 4338 4339 for (MachineOperand &Use : MI.implicit_operands()) { 4340 if (Use.isUse() && 4341 (Use.getReg() == AMDGPU::VCC || Use.getReg() == AMDGPU::VCC_LO)) { 4342 Use.setIsUndef(Orig.isUndef()); 4343 Use.setIsKill(Orig.isKill()); 4344 return; 4345 } 4346 } 4347 } 4348 4349 MachineInstr *SIInstrInfo::buildShrunkInst(MachineInstr &MI, 4350 unsigned Op32) const { 4351 MachineBasicBlock *MBB = MI.getParent(); 4352 MachineInstrBuilder Inst32 = 4353 BuildMI(*MBB, MI, MI.getDebugLoc(), get(Op32)) 4354 .setMIFlags(MI.getFlags()); 4355 4356 // Add the dst operand if the 32-bit encoding also has an explicit $vdst. 4357 // For VOPC instructions, this is replaced by an implicit def of vcc. 4358 if (AMDGPU::hasNamedOperand(Op32, AMDGPU::OpName::vdst)) { 4359 // dst 4360 Inst32.add(MI.getOperand(0)); 4361 } else if (AMDGPU::hasNamedOperand(Op32, AMDGPU::OpName::sdst)) { 4362 // VOPCX instructions won't be writing to an explicit dst, so this should 4363 // not fail for these instructions. 4364 assert(((MI.getOperand(0).getReg() == AMDGPU::VCC) || 4365 (MI.getOperand(0).getReg() == AMDGPU::VCC_LO)) && 4366 "Unexpected case"); 4367 } 4368 4369 Inst32.add(*getNamedOperand(MI, AMDGPU::OpName::src0)); 4370 4371 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1); 4372 if (Src1) 4373 Inst32.add(*Src1); 4374 4375 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2); 4376 4377 if (Src2) { 4378 int Op32Src2Idx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2); 4379 if (Op32Src2Idx != -1) { 4380 Inst32.add(*Src2); 4381 } else { 4382 // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is 4383 // replaced with an implicit read of vcc or vcc_lo. The implicit read 4384 // of vcc was already added during the initial BuildMI, but we 4385 // 1) may need to change vcc to vcc_lo to preserve the original register 4386 // 2) have to preserve the original flags. 4387 fixImplicitOperands(*Inst32); 4388 copyFlagsToImplicitVCC(*Inst32, *Src2); 4389 } 4390 } 4391 4392 return Inst32; 4393 } 4394 4395 bool SIInstrInfo::usesConstantBus(const MachineRegisterInfo &MRI, 4396 const MachineOperand &MO, 4397 const MCOperandInfo &OpInfo) const { 4398 // Literal constants use the constant bus. 4399 if (!MO.isReg()) 4400 return !isInlineConstant(MO, OpInfo); 4401 4402 if (!MO.isUse()) 4403 return false; 4404 4405 if (MO.getReg().isVirtual()) 4406 return RI.isSGPRClass(MRI.getRegClass(MO.getReg())); 4407 4408 // Null is free 4409 if (MO.getReg() == AMDGPU::SGPR_NULL || MO.getReg() == AMDGPU::SGPR_NULL64) 4410 return false; 4411 4412 // SGPRs use the constant bus 4413 if (MO.isImplicit()) { 4414 return MO.getReg() == AMDGPU::M0 || 4415 MO.getReg() == AMDGPU::VCC || 4416 MO.getReg() == AMDGPU::VCC_LO; 4417 } else { 4418 return AMDGPU::SReg_32RegClass.contains(MO.getReg()) || 4419 AMDGPU::SReg_64RegClass.contains(MO.getReg()); 4420 } 4421 } 4422 4423 static Register findImplicitSGPRRead(const MachineInstr &MI) { 4424 for (const MachineOperand &MO : MI.implicit_operands()) { 4425 // We only care about reads. 4426 if (MO.isDef()) 4427 continue; 4428 4429 switch (MO.getReg()) { 4430 case AMDGPU::VCC: 4431 case AMDGPU::VCC_LO: 4432 case AMDGPU::VCC_HI: 4433 case AMDGPU::M0: 4434 case AMDGPU::FLAT_SCR: 4435 return MO.getReg(); 4436 4437 default: 4438 break; 4439 } 4440 } 4441 4442 return Register(); 4443 } 4444 4445 static bool shouldReadExec(const MachineInstr &MI) { 4446 if (SIInstrInfo::isVALU(MI)) { 4447 switch (MI.getOpcode()) { 4448 case AMDGPU::V_READLANE_B32: 4449 case AMDGPU::SI_RESTORE_S32_FROM_VGPR: 4450 case AMDGPU::V_WRITELANE_B32: 4451 case AMDGPU::SI_SPILL_S32_TO_VGPR: 4452 return false; 4453 } 4454 4455 return true; 4456 } 4457 4458 if (MI.isPreISelOpcode() || 4459 SIInstrInfo::isGenericOpcode(MI.getOpcode()) || 4460 SIInstrInfo::isSALU(MI) || 4461 SIInstrInfo::isSMRD(MI)) 4462 return false; 4463 4464 return true; 4465 } 4466 4467 static bool isSubRegOf(const SIRegisterInfo &TRI, 4468 const MachineOperand &SuperVec, 4469 const MachineOperand &SubReg) { 4470 if (SubReg.getReg().isPhysical()) 4471 return TRI.isSubRegister(SuperVec.getReg(), SubReg.getReg()); 4472 4473 return SubReg.getSubReg() != AMDGPU::NoSubRegister && 4474 SubReg.getReg() == SuperVec.getReg(); 4475 } 4476 4477 bool SIInstrInfo::verifyInstruction(const MachineInstr &MI, 4478 StringRef &ErrInfo) const { 4479 uint16_t Opcode = MI.getOpcode(); 4480 if (SIInstrInfo::isGenericOpcode(MI.getOpcode())) 4481 return true; 4482 4483 const MachineFunction *MF = MI.getParent()->getParent(); 4484 const MachineRegisterInfo &MRI = MF->getRegInfo(); 4485 4486 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0); 4487 int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1); 4488 int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2); 4489 int Src3Idx = -1; 4490 if (Src0Idx == -1) { 4491 // VOPD V_DUAL_* instructions use different operand names. 4492 Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0X); 4493 Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vsrc1X); 4494 Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0Y); 4495 Src3Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vsrc1Y); 4496 } 4497 4498 // Make sure the number of operands is correct. 4499 const MCInstrDesc &Desc = get(Opcode); 4500 if (!Desc.isVariadic() && 4501 Desc.getNumOperands() != MI.getNumExplicitOperands()) { 4502 ErrInfo = "Instruction has wrong number of operands."; 4503 return false; 4504 } 4505 4506 if (MI.isInlineAsm()) { 4507 // Verify register classes for inlineasm constraints. 4508 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = MI.getNumOperands(); 4509 I != E; ++I) { 4510 const TargetRegisterClass *RC = MI.getRegClassConstraint(I, this, &RI); 4511 if (!RC) 4512 continue; 4513 4514 const MachineOperand &Op = MI.getOperand(I); 4515 if (!Op.isReg()) 4516 continue; 4517 4518 Register Reg = Op.getReg(); 4519 if (!Reg.isVirtual() && !RC->contains(Reg)) { 4520 ErrInfo = "inlineasm operand has incorrect register class."; 4521 return false; 4522 } 4523 } 4524 4525 return true; 4526 } 4527 4528 if (isImage(MI) && MI.memoperands_empty() && MI.mayLoadOrStore()) { 4529 ErrInfo = "missing memory operand from image instruction."; 4530 return false; 4531 } 4532 4533 // Make sure the register classes are correct. 4534 for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) { 4535 const MachineOperand &MO = MI.getOperand(i); 4536 if (MO.isFPImm()) { 4537 ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast " 4538 "all fp values to integers."; 4539 return false; 4540 } 4541 4542 int RegClass = Desc.operands()[i].RegClass; 4543 4544 switch (Desc.operands()[i].OperandType) { 4545 case MCOI::OPERAND_REGISTER: 4546 if (MI.getOperand(i).isImm() || MI.getOperand(i).isGlobal()) { 4547 ErrInfo = "Illegal immediate value for operand."; 4548 return false; 4549 } 4550 break; 4551 case AMDGPU::OPERAND_REG_IMM_INT32: 4552 case AMDGPU::OPERAND_REG_IMM_FP32: 4553 case AMDGPU::OPERAND_REG_IMM_FP32_DEFERRED: 4554 case AMDGPU::OPERAND_REG_IMM_V2FP32: 4555 break; 4556 case AMDGPU::OPERAND_REG_INLINE_C_INT32: 4557 case AMDGPU::OPERAND_REG_INLINE_C_FP32: 4558 case AMDGPU::OPERAND_REG_INLINE_C_INT64: 4559 case AMDGPU::OPERAND_REG_INLINE_C_FP64: 4560 case AMDGPU::OPERAND_REG_INLINE_C_INT16: 4561 case AMDGPU::OPERAND_REG_INLINE_C_FP16: 4562 case AMDGPU::OPERAND_REG_INLINE_AC_INT32: 4563 case AMDGPU::OPERAND_REG_INLINE_AC_FP32: 4564 case AMDGPU::OPERAND_REG_INLINE_AC_INT16: 4565 case AMDGPU::OPERAND_REG_INLINE_AC_FP16: 4566 case AMDGPU::OPERAND_REG_INLINE_AC_FP64: { 4567 if (!MO.isReg() && (!MO.isImm() || !isInlineConstant(MI, i))) { 4568 ErrInfo = "Illegal immediate value for operand."; 4569 return false; 4570 } 4571 break; 4572 } 4573 case AMDGPU::OPERAND_INLINE_SPLIT_BARRIER_INT32: 4574 if (!MI.getOperand(i).isImm() || !isInlineConstant(MI, i)) { 4575 ErrInfo = "Expected inline constant for operand."; 4576 return false; 4577 } 4578 break; 4579 case MCOI::OPERAND_IMMEDIATE: 4580 case AMDGPU::OPERAND_KIMM32: 4581 // Check if this operand is an immediate. 4582 // FrameIndex operands will be replaced by immediates, so they are 4583 // allowed. 4584 if (!MI.getOperand(i).isImm() && !MI.getOperand(i).isFI()) { 4585 ErrInfo = "Expected immediate, but got non-immediate"; 4586 return false; 4587 } 4588 [[fallthrough]]; 4589 default: 4590 continue; 4591 } 4592 4593 if (!MO.isReg()) 4594 continue; 4595 Register Reg = MO.getReg(); 4596 if (!Reg) 4597 continue; 4598 4599 // FIXME: Ideally we would have separate instruction definitions with the 4600 // aligned register constraint. 4601 // FIXME: We do not verify inline asm operands, but custom inline asm 4602 // verification is broken anyway 4603 if (ST.needsAlignedVGPRs()) { 4604 const TargetRegisterClass *RC = RI.getRegClassForReg(MRI, Reg); 4605 if (RI.hasVectorRegisters(RC) && MO.getSubReg()) { 4606 const TargetRegisterClass *SubRC = 4607 RI.getSubRegisterClass(RC, MO.getSubReg()); 4608 RC = RI.getCompatibleSubRegClass(RC, SubRC, MO.getSubReg()); 4609 if (RC) 4610 RC = SubRC; 4611 } 4612 4613 // Check that this is the aligned version of the class. 4614 if (!RC || !RI.isProperlyAlignedRC(*RC)) { 4615 ErrInfo = "Subtarget requires even aligned vector registers"; 4616 return false; 4617 } 4618 } 4619 4620 if (RegClass != -1) { 4621 if (Reg.isVirtual()) 4622 continue; 4623 4624 const TargetRegisterClass *RC = RI.getRegClass(RegClass); 4625 if (!RC->contains(Reg)) { 4626 ErrInfo = "Operand has incorrect register class."; 4627 return false; 4628 } 4629 } 4630 } 4631 4632 // Verify SDWA 4633 if (isSDWA(MI)) { 4634 if (!ST.hasSDWA()) { 4635 ErrInfo = "SDWA is not supported on this target"; 4636 return false; 4637 } 4638 4639 int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst); 4640 4641 for (int OpIdx : {DstIdx, Src0Idx, Src1Idx, Src2Idx}) { 4642 if (OpIdx == -1) 4643 continue; 4644 const MachineOperand &MO = MI.getOperand(OpIdx); 4645 4646 if (!ST.hasSDWAScalar()) { 4647 // Only VGPRS on VI 4648 if (!MO.isReg() || !RI.hasVGPRs(RI.getRegClassForReg(MRI, MO.getReg()))) { 4649 ErrInfo = "Only VGPRs allowed as operands in SDWA instructions on VI"; 4650 return false; 4651 } 4652 } else { 4653 // No immediates on GFX9 4654 if (!MO.isReg()) { 4655 ErrInfo = 4656 "Only reg allowed as operands in SDWA instructions on GFX9+"; 4657 return false; 4658 } 4659 } 4660 } 4661 4662 if (!ST.hasSDWAOmod()) { 4663 // No omod allowed on VI 4664 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod); 4665 if (OMod != nullptr && 4666 (!OMod->isImm() || OMod->getImm() != 0)) { 4667 ErrInfo = "OMod not allowed in SDWA instructions on VI"; 4668 return false; 4669 } 4670 } 4671 4672 uint16_t BasicOpcode = AMDGPU::getBasicFromSDWAOp(Opcode); 4673 if (isVOPC(BasicOpcode)) { 4674 if (!ST.hasSDWASdst() && DstIdx != -1) { 4675 // Only vcc allowed as dst on VI for VOPC 4676 const MachineOperand &Dst = MI.getOperand(DstIdx); 4677 if (!Dst.isReg() || Dst.getReg() != AMDGPU::VCC) { 4678 ErrInfo = "Only VCC allowed as dst in SDWA instructions on VI"; 4679 return false; 4680 } 4681 } else if (!ST.hasSDWAOutModsVOPC()) { 4682 // No clamp allowed on GFX9 for VOPC 4683 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp); 4684 if (Clamp && (!Clamp->isImm() || Clamp->getImm() != 0)) { 4685 ErrInfo = "Clamp not allowed in VOPC SDWA instructions on VI"; 4686 return false; 4687 } 4688 4689 // No omod allowed on GFX9 for VOPC 4690 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod); 4691 if (OMod && (!OMod->isImm() || OMod->getImm() != 0)) { 4692 ErrInfo = "OMod not allowed in VOPC SDWA instructions on VI"; 4693 return false; 4694 } 4695 } 4696 } 4697 4698 const MachineOperand *DstUnused = getNamedOperand(MI, AMDGPU::OpName::dst_unused); 4699 if (DstUnused && DstUnused->isImm() && 4700 DstUnused->getImm() == AMDGPU::SDWA::UNUSED_PRESERVE) { 4701 const MachineOperand &Dst = MI.getOperand(DstIdx); 4702 if (!Dst.isReg() || !Dst.isTied()) { 4703 ErrInfo = "Dst register should have tied register"; 4704 return false; 4705 } 4706 4707 const MachineOperand &TiedMO = 4708 MI.getOperand(MI.findTiedOperandIdx(DstIdx)); 4709 if (!TiedMO.isReg() || !TiedMO.isImplicit() || !TiedMO.isUse()) { 4710 ErrInfo = 4711 "Dst register should be tied to implicit use of preserved register"; 4712 return false; 4713 } else if (TiedMO.getReg().isPhysical() && 4714 Dst.getReg() != TiedMO.getReg()) { 4715 ErrInfo = "Dst register should use same physical register as preserved"; 4716 return false; 4717 } 4718 } 4719 } 4720 4721 // Verify MIMG / VIMAGE / VSAMPLE 4722 if (isImage(MI.getOpcode()) && !MI.mayStore()) { 4723 // Ensure that the return type used is large enough for all the options 4724 // being used TFE/LWE require an extra result register. 4725 const MachineOperand *DMask = getNamedOperand(MI, AMDGPU::OpName::dmask); 4726 if (DMask) { 4727 uint64_t DMaskImm = DMask->getImm(); 4728 uint32_t RegCount = 4729 isGather4(MI.getOpcode()) ? 4 : llvm::popcount(DMaskImm); 4730 const MachineOperand *TFE = getNamedOperand(MI, AMDGPU::OpName::tfe); 4731 const MachineOperand *LWE = getNamedOperand(MI, AMDGPU::OpName::lwe); 4732 const MachineOperand *D16 = getNamedOperand(MI, AMDGPU::OpName::d16); 4733 4734 // Adjust for packed 16 bit values 4735 if (D16 && D16->getImm() && !ST.hasUnpackedD16VMem()) 4736 RegCount = divideCeil(RegCount, 2); 4737 4738 // Adjust if using LWE or TFE 4739 if ((LWE && LWE->getImm()) || (TFE && TFE->getImm())) 4740 RegCount += 1; 4741 4742 const uint32_t DstIdx = 4743 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata); 4744 const MachineOperand &Dst = MI.getOperand(DstIdx); 4745 if (Dst.isReg()) { 4746 const TargetRegisterClass *DstRC = getOpRegClass(MI, DstIdx); 4747 uint32_t DstSize = RI.getRegSizeInBits(*DstRC) / 32; 4748 if (RegCount > DstSize) { 4749 ErrInfo = "Image instruction returns too many registers for dst " 4750 "register class"; 4751 return false; 4752 } 4753 } 4754 } 4755 } 4756 4757 // Verify VOP*. Ignore multiple sgpr operands on writelane. 4758 if (isVALU(MI) && Desc.getOpcode() != AMDGPU::V_WRITELANE_B32) { 4759 unsigned ConstantBusCount = 0; 4760 bool UsesLiteral = false; 4761 const MachineOperand *LiteralVal = nullptr; 4762 4763 int ImmIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm); 4764 if (ImmIdx != -1) { 4765 ++ConstantBusCount; 4766 UsesLiteral = true; 4767 LiteralVal = &MI.getOperand(ImmIdx); 4768 } 4769 4770 SmallVector<Register, 2> SGPRsUsed; 4771 Register SGPRUsed; 4772 4773 // Only look at the true operands. Only a real operand can use the constant 4774 // bus, and we don't want to check pseudo-operands like the source modifier 4775 // flags. 4776 for (int OpIdx : {Src0Idx, Src1Idx, Src2Idx, Src3Idx}) { 4777 if (OpIdx == -1) 4778 continue; 4779 const MachineOperand &MO = MI.getOperand(OpIdx); 4780 if (usesConstantBus(MRI, MO, MI.getDesc().operands()[OpIdx])) { 4781 if (MO.isReg()) { 4782 SGPRUsed = MO.getReg(); 4783 if (!llvm::is_contained(SGPRsUsed, SGPRUsed)) { 4784 ++ConstantBusCount; 4785 SGPRsUsed.push_back(SGPRUsed); 4786 } 4787 } else { 4788 if (!UsesLiteral) { 4789 ++ConstantBusCount; 4790 UsesLiteral = true; 4791 LiteralVal = &MO; 4792 } else if (!MO.isIdenticalTo(*LiteralVal)) { 4793 assert(isVOP2(MI) || isVOP3(MI)); 4794 ErrInfo = "VOP2/VOP3 instruction uses more than one literal"; 4795 return false; 4796 } 4797 } 4798 } 4799 } 4800 4801 SGPRUsed = findImplicitSGPRRead(MI); 4802 if (SGPRUsed) { 4803 // Implicit uses may safely overlap true operands 4804 if (llvm::all_of(SGPRsUsed, [this, SGPRUsed](unsigned SGPR) { 4805 return !RI.regsOverlap(SGPRUsed, SGPR); 4806 })) { 4807 ++ConstantBusCount; 4808 SGPRsUsed.push_back(SGPRUsed); 4809 } 4810 } 4811 4812 // v_writelane_b32 is an exception from constant bus restriction: 4813 // vsrc0 can be sgpr, const or m0 and lane select sgpr, m0 or inline-const 4814 if (ConstantBusCount > ST.getConstantBusLimit(Opcode) && 4815 Opcode != AMDGPU::V_WRITELANE_B32) { 4816 ErrInfo = "VOP* instruction violates constant bus restriction"; 4817 return false; 4818 } 4819 4820 if (isVOP3(MI) && UsesLiteral && !ST.hasVOP3Literal()) { 4821 ErrInfo = "VOP3 instruction uses literal"; 4822 return false; 4823 } 4824 } 4825 4826 // Special case for writelane - this can break the multiple constant bus rule, 4827 // but still can't use more than one SGPR register 4828 if (Desc.getOpcode() == AMDGPU::V_WRITELANE_B32) { 4829 unsigned SGPRCount = 0; 4830 Register SGPRUsed; 4831 4832 for (int OpIdx : {Src0Idx, Src1Idx}) { 4833 if (OpIdx == -1) 4834 break; 4835 4836 const MachineOperand &MO = MI.getOperand(OpIdx); 4837 4838 if (usesConstantBus(MRI, MO, MI.getDesc().operands()[OpIdx])) { 4839 if (MO.isReg() && MO.getReg() != AMDGPU::M0) { 4840 if (MO.getReg() != SGPRUsed) 4841 ++SGPRCount; 4842 SGPRUsed = MO.getReg(); 4843 } 4844 } 4845 if (SGPRCount > ST.getConstantBusLimit(Opcode)) { 4846 ErrInfo = "WRITELANE instruction violates constant bus restriction"; 4847 return false; 4848 } 4849 } 4850 } 4851 4852 // Verify misc. restrictions on specific instructions. 4853 if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32_e64 || 4854 Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64_e64) { 4855 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 4856 const MachineOperand &Src1 = MI.getOperand(Src1Idx); 4857 const MachineOperand &Src2 = MI.getOperand(Src2Idx); 4858 if (Src0.isReg() && Src1.isReg() && Src2.isReg()) { 4859 if (!compareMachineOp(Src0, Src1) && 4860 !compareMachineOp(Src0, Src2)) { 4861 ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2"; 4862 return false; 4863 } 4864 } 4865 if ((getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm() & 4866 SISrcMods::ABS) || 4867 (getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm() & 4868 SISrcMods::ABS) || 4869 (getNamedOperand(MI, AMDGPU::OpName::src2_modifiers)->getImm() & 4870 SISrcMods::ABS)) { 4871 ErrInfo = "ABS not allowed in VOP3B instructions"; 4872 return false; 4873 } 4874 } 4875 4876 if (isSOP2(MI) || isSOPC(MI)) { 4877 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 4878 const MachineOperand &Src1 = MI.getOperand(Src1Idx); 4879 4880 if (!Src0.isReg() && !Src1.isReg() && 4881 !isInlineConstant(Src0, Desc.operands()[Src0Idx]) && 4882 !isInlineConstant(Src1, Desc.operands()[Src1Idx]) && 4883 !Src0.isIdenticalTo(Src1)) { 4884 ErrInfo = "SOP2/SOPC instruction requires too many immediate constants"; 4885 return false; 4886 } 4887 } 4888 4889 if (isSOPK(MI)) { 4890 auto Op = getNamedOperand(MI, AMDGPU::OpName::simm16); 4891 if (Desc.isBranch()) { 4892 if (!Op->isMBB()) { 4893 ErrInfo = "invalid branch target for SOPK instruction"; 4894 return false; 4895 } 4896 } else { 4897 uint64_t Imm = Op->getImm(); 4898 if (sopkIsZext(MI)) { 4899 if (!isUInt<16>(Imm)) { 4900 ErrInfo = "invalid immediate for SOPK instruction"; 4901 return false; 4902 } 4903 } else { 4904 if (!isInt<16>(Imm)) { 4905 ErrInfo = "invalid immediate for SOPK instruction"; 4906 return false; 4907 } 4908 } 4909 } 4910 } 4911 4912 if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 || 4913 Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 || 4914 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 4915 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) { 4916 const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 4917 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64; 4918 4919 const unsigned StaticNumOps = 4920 Desc.getNumOperands() + Desc.implicit_uses().size(); 4921 const unsigned NumImplicitOps = IsDst ? 2 : 1; 4922 4923 // Allow additional implicit operands. This allows a fixup done by the post 4924 // RA scheduler where the main implicit operand is killed and implicit-defs 4925 // are added for sub-registers that remain live after this instruction. 4926 if (MI.getNumOperands() < StaticNumOps + NumImplicitOps) { 4927 ErrInfo = "missing implicit register operands"; 4928 return false; 4929 } 4930 4931 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 4932 if (IsDst) { 4933 if (!Dst->isUse()) { 4934 ErrInfo = "v_movreld_b32 vdst should be a use operand"; 4935 return false; 4936 } 4937 4938 unsigned UseOpIdx; 4939 if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) || 4940 UseOpIdx != StaticNumOps + 1) { 4941 ErrInfo = "movrel implicit operands should be tied"; 4942 return false; 4943 } 4944 } 4945 4946 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 4947 const MachineOperand &ImpUse 4948 = MI.getOperand(StaticNumOps + NumImplicitOps - 1); 4949 if (!ImpUse.isReg() || !ImpUse.isUse() || 4950 !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) { 4951 ErrInfo = "src0 should be subreg of implicit vector use"; 4952 return false; 4953 } 4954 } 4955 4956 // Make sure we aren't losing exec uses in the td files. This mostly requires 4957 // being careful when using let Uses to try to add other use registers. 4958 if (shouldReadExec(MI)) { 4959 if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) { 4960 ErrInfo = "VALU instruction does not implicitly read exec mask"; 4961 return false; 4962 } 4963 } 4964 4965 if (isSMRD(MI)) { 4966 if (MI.mayStore() && 4967 ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) { 4968 // The register offset form of scalar stores may only use m0 as the 4969 // soffset register. 4970 const MachineOperand *Soff = getNamedOperand(MI, AMDGPU::OpName::soffset); 4971 if (Soff && Soff->getReg() != AMDGPU::M0) { 4972 ErrInfo = "scalar stores must use m0 as offset register"; 4973 return false; 4974 } 4975 } 4976 } 4977 4978 if (isFLAT(MI) && !ST.hasFlatInstOffsets()) { 4979 const MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 4980 if (Offset->getImm() != 0) { 4981 ErrInfo = "subtarget does not support offsets in flat instructions"; 4982 return false; 4983 } 4984 } 4985 4986 if (isDS(MI) && !ST.hasGDS()) { 4987 const MachineOperand *GDSOp = getNamedOperand(MI, AMDGPU::OpName::gds); 4988 if (GDSOp && GDSOp->getImm() != 0) { 4989 ErrInfo = "GDS is not supported on this subtarget"; 4990 return false; 4991 } 4992 } 4993 4994 if (isImage(MI)) { 4995 const MachineOperand *DimOp = getNamedOperand(MI, AMDGPU::OpName::dim); 4996 if (DimOp) { 4997 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opcode, 4998 AMDGPU::OpName::vaddr0); 4999 int RSrcOpName = 5000 isMIMG(MI) ? AMDGPU::OpName::srsrc : AMDGPU::OpName::rsrc; 5001 int RsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, RSrcOpName); 5002 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(Opcode); 5003 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode = 5004 AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode); 5005 const AMDGPU::MIMGDimInfo *Dim = 5006 AMDGPU::getMIMGDimInfoByEncoding(DimOp->getImm()); 5007 5008 if (!Dim) { 5009 ErrInfo = "dim is out of range"; 5010 return false; 5011 } 5012 5013 bool IsA16 = false; 5014 if (ST.hasR128A16()) { 5015 const MachineOperand *R128A16 = getNamedOperand(MI, AMDGPU::OpName::r128); 5016 IsA16 = R128A16->getImm() != 0; 5017 } else if (ST.hasA16()) { 5018 const MachineOperand *A16 = getNamedOperand(MI, AMDGPU::OpName::a16); 5019 IsA16 = A16->getImm() != 0; 5020 } 5021 5022 bool IsNSA = RsrcIdx - VAddr0Idx > 1; 5023 5024 unsigned AddrWords = 5025 AMDGPU::getAddrSizeMIMGOp(BaseOpcode, Dim, IsA16, ST.hasG16()); 5026 5027 unsigned VAddrWords; 5028 if (IsNSA) { 5029 VAddrWords = RsrcIdx - VAddr0Idx; 5030 if (ST.hasPartialNSAEncoding() && 5031 AddrWords > ST.getNSAMaxSize(isVSAMPLE(MI))) { 5032 unsigned LastVAddrIdx = RsrcIdx - 1; 5033 VAddrWords += getOpSize(MI, LastVAddrIdx) / 4 - 1; 5034 } 5035 } else { 5036 VAddrWords = getOpSize(MI, VAddr0Idx) / 4; 5037 if (AddrWords > 12) 5038 AddrWords = 16; 5039 } 5040 5041 if (VAddrWords != AddrWords) { 5042 LLVM_DEBUG(dbgs() << "bad vaddr size, expected " << AddrWords 5043 << " but got " << VAddrWords << "\n"); 5044 ErrInfo = "bad vaddr size"; 5045 return false; 5046 } 5047 } 5048 } 5049 5050 const MachineOperand *DppCt = getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl); 5051 if (DppCt) { 5052 using namespace AMDGPU::DPP; 5053 5054 unsigned DC = DppCt->getImm(); 5055 if (DC == DppCtrl::DPP_UNUSED1 || DC == DppCtrl::DPP_UNUSED2 || 5056 DC == DppCtrl::DPP_UNUSED3 || DC > DppCtrl::DPP_LAST || 5057 (DC >= DppCtrl::DPP_UNUSED4_FIRST && DC <= DppCtrl::DPP_UNUSED4_LAST) || 5058 (DC >= DppCtrl::DPP_UNUSED5_FIRST && DC <= DppCtrl::DPP_UNUSED5_LAST) || 5059 (DC >= DppCtrl::DPP_UNUSED6_FIRST && DC <= DppCtrl::DPP_UNUSED6_LAST) || 5060 (DC >= DppCtrl::DPP_UNUSED7_FIRST && DC <= DppCtrl::DPP_UNUSED7_LAST) || 5061 (DC >= DppCtrl::DPP_UNUSED8_FIRST && DC <= DppCtrl::DPP_UNUSED8_LAST)) { 5062 ErrInfo = "Invalid dpp_ctrl value"; 5063 return false; 5064 } 5065 if (DC >= DppCtrl::WAVE_SHL1 && DC <= DppCtrl::WAVE_ROR1 && 5066 ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 5067 ErrInfo = "Invalid dpp_ctrl value: " 5068 "wavefront shifts are not supported on GFX10+"; 5069 return false; 5070 } 5071 if (DC >= DppCtrl::BCAST15 && DC <= DppCtrl::BCAST31 && 5072 ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 5073 ErrInfo = "Invalid dpp_ctrl value: " 5074 "broadcasts are not supported on GFX10+"; 5075 return false; 5076 } 5077 if (DC >= DppCtrl::ROW_SHARE_FIRST && DC <= DppCtrl::ROW_XMASK_LAST && 5078 ST.getGeneration() < AMDGPUSubtarget::GFX10) { 5079 if (DC >= DppCtrl::ROW_NEWBCAST_FIRST && 5080 DC <= DppCtrl::ROW_NEWBCAST_LAST && 5081 !ST.hasGFX90AInsts()) { 5082 ErrInfo = "Invalid dpp_ctrl value: " 5083 "row_newbroadcast/row_share is not supported before " 5084 "GFX90A/GFX10"; 5085 return false; 5086 } else if (DC > DppCtrl::ROW_NEWBCAST_LAST || !ST.hasGFX90AInsts()) { 5087 ErrInfo = "Invalid dpp_ctrl value: " 5088 "row_share and row_xmask are not supported before GFX10"; 5089 return false; 5090 } 5091 } 5092 5093 if (Opcode != AMDGPU::V_MOV_B64_DPP_PSEUDO && 5094 !AMDGPU::isLegalDPALU_DPPControl(DC) && AMDGPU::isDPALU_DPP(Desc)) { 5095 ErrInfo = "Invalid dpp_ctrl value: " 5096 "DP ALU dpp only support row_newbcast"; 5097 return false; 5098 } 5099 } 5100 5101 if ((MI.mayStore() || MI.mayLoad()) && !isVGPRSpill(MI)) { 5102 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 5103 uint16_t DataNameIdx = isDS(Opcode) ? AMDGPU::OpName::data0 5104 : AMDGPU::OpName::vdata; 5105 const MachineOperand *Data = getNamedOperand(MI, DataNameIdx); 5106 const MachineOperand *Data2 = getNamedOperand(MI, AMDGPU::OpName::data1); 5107 if (Data && !Data->isReg()) 5108 Data = nullptr; 5109 5110 if (ST.hasGFX90AInsts()) { 5111 if (Dst && Data && 5112 (RI.isAGPR(MRI, Dst->getReg()) != RI.isAGPR(MRI, Data->getReg()))) { 5113 ErrInfo = "Invalid register class: " 5114 "vdata and vdst should be both VGPR or AGPR"; 5115 return false; 5116 } 5117 if (Data && Data2 && 5118 (RI.isAGPR(MRI, Data->getReg()) != RI.isAGPR(MRI, Data2->getReg()))) { 5119 ErrInfo = "Invalid register class: " 5120 "both data operands should be VGPR or AGPR"; 5121 return false; 5122 } 5123 } else { 5124 if ((Dst && RI.isAGPR(MRI, Dst->getReg())) || 5125 (Data && RI.isAGPR(MRI, Data->getReg())) || 5126 (Data2 && RI.isAGPR(MRI, Data2->getReg()))) { 5127 ErrInfo = "Invalid register class: " 5128 "agpr loads and stores not supported on this GPU"; 5129 return false; 5130 } 5131 } 5132 } 5133 5134 if (ST.needsAlignedVGPRs()) { 5135 const auto isAlignedReg = [&MI, &MRI, this](unsigned OpName) -> bool { 5136 const MachineOperand *Op = getNamedOperand(MI, OpName); 5137 if (!Op) 5138 return true; 5139 Register Reg = Op->getReg(); 5140 if (Reg.isPhysical()) 5141 return !(RI.getHWRegIndex(Reg) & 1); 5142 const TargetRegisterClass &RC = *MRI.getRegClass(Reg); 5143 return RI.getRegSizeInBits(RC) > 32 && RI.isProperlyAlignedRC(RC) && 5144 !(RI.getChannelFromSubReg(Op->getSubReg()) & 1); 5145 }; 5146 5147 if (MI.getOpcode() == AMDGPU::DS_GWS_INIT || 5148 MI.getOpcode() == AMDGPU::DS_GWS_SEMA_BR || 5149 MI.getOpcode() == AMDGPU::DS_GWS_BARRIER) { 5150 5151 if (!isAlignedReg(AMDGPU::OpName::data0)) { 5152 ErrInfo = "Subtarget requires even aligned vector registers " 5153 "for DS_GWS instructions"; 5154 return false; 5155 } 5156 } 5157 5158 if (isMIMG(MI)) { 5159 if (!isAlignedReg(AMDGPU::OpName::vaddr)) { 5160 ErrInfo = "Subtarget requires even aligned vector registers " 5161 "for vaddr operand of image instructions"; 5162 return false; 5163 } 5164 } 5165 } 5166 5167 if (MI.getOpcode() == AMDGPU::V_ACCVGPR_WRITE_B32_e64 && 5168 !ST.hasGFX90AInsts()) { 5169 const MachineOperand *Src = getNamedOperand(MI, AMDGPU::OpName::src0); 5170 if (Src->isReg() && RI.isSGPRReg(MRI, Src->getReg())) { 5171 ErrInfo = "Invalid register class: " 5172 "v_accvgpr_write with an SGPR is not supported on this GPU"; 5173 return false; 5174 } 5175 } 5176 5177 if (Desc.getOpcode() == AMDGPU::G_AMDGPU_WAVE_ADDRESS) { 5178 const MachineOperand &SrcOp = MI.getOperand(1); 5179 if (!SrcOp.isReg() || SrcOp.getReg().isVirtual()) { 5180 ErrInfo = "pseudo expects only physical SGPRs"; 5181 return false; 5182 } 5183 } 5184 5185 return true; 5186 } 5187 5188 // It is more readable to list mapped opcodes on the same line. 5189 // clang-format off 5190 5191 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) const { 5192 switch (MI.getOpcode()) { 5193 default: return AMDGPU::INSTRUCTION_LIST_END; 5194 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE; 5195 case AMDGPU::COPY: return AMDGPU::COPY; 5196 case AMDGPU::PHI: return AMDGPU::PHI; 5197 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG; 5198 case AMDGPU::WQM: return AMDGPU::WQM; 5199 case AMDGPU::SOFT_WQM: return AMDGPU::SOFT_WQM; 5200 case AMDGPU::STRICT_WWM: return AMDGPU::STRICT_WWM; 5201 case AMDGPU::STRICT_WQM: return AMDGPU::STRICT_WQM; 5202 case AMDGPU::S_MOV_B32: { 5203 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 5204 return MI.getOperand(1).isReg() || 5205 RI.isAGPR(MRI, MI.getOperand(0).getReg()) ? 5206 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32; 5207 } 5208 case AMDGPU::S_ADD_I32: 5209 return ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_CO_U32_e32; 5210 case AMDGPU::S_ADDC_U32: 5211 return AMDGPU::V_ADDC_U32_e32; 5212 case AMDGPU::S_SUB_I32: 5213 return ST.hasAddNoCarry() ? AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_CO_U32_e32; 5214 // FIXME: These are not consistently handled, and selected when the carry is 5215 // used. 5216 case AMDGPU::S_ADD_U32: 5217 return AMDGPU::V_ADD_CO_U32_e32; 5218 case AMDGPU::S_SUB_U32: 5219 return AMDGPU::V_SUB_CO_U32_e32; 5220 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32; 5221 case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_U32_e64; 5222 case AMDGPU::S_MUL_HI_U32: return AMDGPU::V_MUL_HI_U32_e64; 5223 case AMDGPU::S_MUL_HI_I32: return AMDGPU::V_MUL_HI_I32_e64; 5224 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64; 5225 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64; 5226 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64; 5227 case AMDGPU::S_XNOR_B32: 5228 return ST.hasDLInsts() ? AMDGPU::V_XNOR_B32_e64 : AMDGPU::INSTRUCTION_LIST_END; 5229 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64; 5230 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64; 5231 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64; 5232 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64; 5233 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32; 5234 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64_e64; 5235 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32; 5236 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64_e64; 5237 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32; 5238 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64_e64; 5239 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32_e64; 5240 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32_e64; 5241 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32_e64; 5242 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32_e64; 5243 case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64; 5244 case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32; 5245 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32; 5246 case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32; 5247 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e64; 5248 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e64; 5249 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e64; 5250 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e64; 5251 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e64; 5252 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e64; 5253 case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e64; 5254 case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e64; 5255 case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e64; 5256 case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e64; 5257 case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e64; 5258 case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e64; 5259 case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e64; 5260 case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e64; 5261 case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64; 5262 case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32; 5263 case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32; 5264 case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64; 5265 case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ; 5266 case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ; 5267 case AMDGPU::S_CVT_F32_I32: return AMDGPU::V_CVT_F32_I32_e64; 5268 case AMDGPU::S_CVT_F32_U32: return AMDGPU::V_CVT_F32_U32_e64; 5269 case AMDGPU::S_CVT_I32_F32: return AMDGPU::V_CVT_I32_F32_e64; 5270 case AMDGPU::S_CVT_U32_F32: return AMDGPU::V_CVT_U32_F32_e64; 5271 case AMDGPU::S_CVT_F32_F16: return AMDGPU::V_CVT_F32_F16_t16_e64; 5272 case AMDGPU::S_CVT_HI_F32_F16: return AMDGPU::V_CVT_F32_F16_t16_e64; 5273 case AMDGPU::S_CVT_F16_F32: return AMDGPU::V_CVT_F16_F32_t16_e64; 5274 case AMDGPU::S_CEIL_F32: return AMDGPU::V_CEIL_F32_e64; 5275 case AMDGPU::S_FLOOR_F32: return AMDGPU::V_FLOOR_F32_e64; 5276 case AMDGPU::S_TRUNC_F32: return AMDGPU::V_TRUNC_F32_e64; 5277 case AMDGPU::S_RNDNE_F32: return AMDGPU::V_RNDNE_F32_e64; 5278 case AMDGPU::S_CEIL_F16: return AMDGPU::V_CEIL_F16_t16_e64; 5279 case AMDGPU::S_FLOOR_F16: return AMDGPU::V_FLOOR_F16_t16_e64; 5280 case AMDGPU::S_TRUNC_F16: return AMDGPU::V_TRUNC_F16_t16_e64; 5281 case AMDGPU::S_RNDNE_F16: return AMDGPU::V_RNDNE_F16_t16_e64; 5282 case AMDGPU::S_ADD_F32: return AMDGPU::V_ADD_F32_e64; 5283 case AMDGPU::S_SUB_F32: return AMDGPU::V_SUB_F32_e64; 5284 case AMDGPU::S_MIN_F32: return AMDGPU::V_MIN_F32_e64; 5285 case AMDGPU::S_MAX_F32: return AMDGPU::V_MAX_F32_e64; 5286 case AMDGPU::S_MINIMUM_F32: return AMDGPU::V_MINIMUM_F32_e64; 5287 case AMDGPU::S_MAXIMUM_F32: return AMDGPU::V_MAXIMUM_F32_e64; 5288 case AMDGPU::S_MUL_F32: return AMDGPU::V_MUL_F32_e64; 5289 case AMDGPU::S_ADD_F16: return AMDGPU::V_ADD_F16_fake16_e64; 5290 case AMDGPU::S_SUB_F16: return AMDGPU::V_SUB_F16_fake16_e64; 5291 case AMDGPU::S_MIN_F16: return AMDGPU::V_MIN_F16_fake16_e64; 5292 case AMDGPU::S_MAX_F16: return AMDGPU::V_MAX_F16_fake16_e64; 5293 case AMDGPU::S_MINIMUM_F16: return AMDGPU::V_MINIMUM_F16_e64; 5294 case AMDGPU::S_MAXIMUM_F16: return AMDGPU::V_MAXIMUM_F16_e64; 5295 case AMDGPU::S_MUL_F16: return AMDGPU::V_MUL_F16_fake16_e64; 5296 case AMDGPU::S_CVT_PK_RTZ_F16_F32: return AMDGPU::V_CVT_PKRTZ_F16_F32_e64; 5297 case AMDGPU::S_FMAC_F32: return AMDGPU::V_FMAC_F32_e64; 5298 case AMDGPU::S_FMAC_F16: return AMDGPU::V_FMAC_F16_t16_e64; 5299 case AMDGPU::S_FMAMK_F32: return AMDGPU::V_FMAMK_F32; 5300 case AMDGPU::S_FMAAK_F32: return AMDGPU::V_FMAAK_F32; 5301 case AMDGPU::S_CMP_LT_F32: return AMDGPU::V_CMP_LT_F32_e64; 5302 case AMDGPU::S_CMP_EQ_F32: return AMDGPU::V_CMP_EQ_F32_e64; 5303 case AMDGPU::S_CMP_LE_F32: return AMDGPU::V_CMP_LE_F32_e64; 5304 case AMDGPU::S_CMP_GT_F32: return AMDGPU::V_CMP_GT_F32_e64; 5305 case AMDGPU::S_CMP_LG_F32: return AMDGPU::V_CMP_LG_F32_e64; 5306 case AMDGPU::S_CMP_GE_F32: return AMDGPU::V_CMP_GE_F32_e64; 5307 case AMDGPU::S_CMP_O_F32: return AMDGPU::V_CMP_O_F32_e64; 5308 case AMDGPU::S_CMP_U_F32: return AMDGPU::V_CMP_U_F32_e64; 5309 case AMDGPU::S_CMP_NGE_F32: return AMDGPU::V_CMP_NGE_F32_e64; 5310 case AMDGPU::S_CMP_NLG_F32: return AMDGPU::V_CMP_NLG_F32_e64; 5311 case AMDGPU::S_CMP_NGT_F32: return AMDGPU::V_CMP_NGT_F32_e64; 5312 case AMDGPU::S_CMP_NLE_F32: return AMDGPU::V_CMP_NLE_F32_e64; 5313 case AMDGPU::S_CMP_NEQ_F32: return AMDGPU::V_CMP_NEQ_F32_e64; 5314 case AMDGPU::S_CMP_NLT_F32: return AMDGPU::V_CMP_NLT_F32_e64; 5315 case AMDGPU::S_CMP_LT_F16: return AMDGPU::V_CMP_LT_F16_t16_e64; 5316 case AMDGPU::S_CMP_EQ_F16: return AMDGPU::V_CMP_EQ_F16_t16_e64; 5317 case AMDGPU::S_CMP_LE_F16: return AMDGPU::V_CMP_LE_F16_t16_e64; 5318 case AMDGPU::S_CMP_GT_F16: return AMDGPU::V_CMP_GT_F16_t16_e64; 5319 case AMDGPU::S_CMP_LG_F16: return AMDGPU::V_CMP_LG_F16_t16_e64; 5320 case AMDGPU::S_CMP_GE_F16: return AMDGPU::V_CMP_GE_F16_t16_e64; 5321 case AMDGPU::S_CMP_O_F16: return AMDGPU::V_CMP_O_F16_t16_e64; 5322 case AMDGPU::S_CMP_U_F16: return AMDGPU::V_CMP_U_F16_t16_e64; 5323 case AMDGPU::S_CMP_NGE_F16: return AMDGPU::V_CMP_NGE_F16_t16_e64; 5324 case AMDGPU::S_CMP_NLG_F16: return AMDGPU::V_CMP_NLG_F16_t16_e64; 5325 case AMDGPU::S_CMP_NGT_F16: return AMDGPU::V_CMP_NGT_F16_t16_e64; 5326 case AMDGPU::S_CMP_NLE_F16: return AMDGPU::V_CMP_NLE_F16_t16_e64; 5327 case AMDGPU::S_CMP_NEQ_F16: return AMDGPU::V_CMP_NEQ_F16_t16_e64; 5328 case AMDGPU::S_CMP_NLT_F16: return AMDGPU::V_CMP_NLT_F16_t16_e64; 5329 case AMDGPU::V_S_EXP_F32_e64: return AMDGPU::V_EXP_F32_e64; 5330 case AMDGPU::V_S_EXP_F16_e64: return AMDGPU::V_EXP_F16_t16_e64; 5331 case AMDGPU::V_S_LOG_F32_e64: return AMDGPU::V_LOG_F32_e64; 5332 case AMDGPU::V_S_LOG_F16_e64: return AMDGPU::V_LOG_F16_t16_e64; 5333 case AMDGPU::V_S_RCP_F32_e64: return AMDGPU::V_RCP_F32_e64; 5334 case AMDGPU::V_S_RCP_F16_e64: return AMDGPU::V_RCP_F16_t16_e64; 5335 case AMDGPU::V_S_RSQ_F32_e64: return AMDGPU::V_RSQ_F32_e64; 5336 case AMDGPU::V_S_RSQ_F16_e64: return AMDGPU::V_RSQ_F16_t16_e64; 5337 case AMDGPU::V_S_SQRT_F32_e64: return AMDGPU::V_SQRT_F32_e64; 5338 case AMDGPU::V_S_SQRT_F16_e64: return AMDGPU::V_SQRT_F16_t16_e64; 5339 } 5340 llvm_unreachable( 5341 "Unexpected scalar opcode without corresponding vector one!"); 5342 } 5343 5344 // clang-format on 5345 5346 void SIInstrInfo::insertScratchExecCopy(MachineFunction &MF, 5347 MachineBasicBlock &MBB, 5348 MachineBasicBlock::iterator MBBI, 5349 const DebugLoc &DL, Register Reg, 5350 bool IsSCCLive, 5351 SlotIndexes *Indexes) const { 5352 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 5353 const SIInstrInfo *TII = ST.getInstrInfo(); 5354 bool IsWave32 = ST.isWave32(); 5355 if (IsSCCLive) { 5356 // Insert two move instructions, one to save the original value of EXEC and 5357 // the other to turn on all bits in EXEC. This is required as we can't use 5358 // the single instruction S_OR_SAVEEXEC that clobbers SCC. 5359 unsigned MovOpc = IsWave32 ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64; 5360 MCRegister Exec = IsWave32 ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 5361 auto StoreExecMI = BuildMI(MBB, MBBI, DL, TII->get(MovOpc), Reg) 5362 .addReg(Exec, RegState::Kill); 5363 auto FlipExecMI = BuildMI(MBB, MBBI, DL, TII->get(MovOpc), Exec).addImm(-1); 5364 if (Indexes) { 5365 Indexes->insertMachineInstrInMaps(*StoreExecMI); 5366 Indexes->insertMachineInstrInMaps(*FlipExecMI); 5367 } 5368 } else { 5369 const unsigned OrSaveExec = 5370 IsWave32 ? AMDGPU::S_OR_SAVEEXEC_B32 : AMDGPU::S_OR_SAVEEXEC_B64; 5371 auto SaveExec = 5372 BuildMI(MBB, MBBI, DL, TII->get(OrSaveExec), Reg).addImm(-1); 5373 SaveExec->getOperand(3).setIsDead(); // Mark SCC as dead. 5374 if (Indexes) 5375 Indexes->insertMachineInstrInMaps(*SaveExec); 5376 } 5377 } 5378 5379 void SIInstrInfo::restoreExec(MachineFunction &MF, MachineBasicBlock &MBB, 5380 MachineBasicBlock::iterator MBBI, 5381 const DebugLoc &DL, Register Reg, 5382 SlotIndexes *Indexes) const { 5383 unsigned ExecMov = isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64; 5384 MCRegister Exec = isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 5385 auto ExecRestoreMI = 5386 BuildMI(MBB, MBBI, DL, get(ExecMov), Exec).addReg(Reg, RegState::Kill); 5387 if (Indexes) 5388 Indexes->insertMachineInstrInMaps(*ExecRestoreMI); 5389 } 5390 5391 static const TargetRegisterClass * 5392 adjustAllocatableRegClass(const GCNSubtarget &ST, const SIRegisterInfo &RI, 5393 const MachineRegisterInfo &MRI, 5394 const MCInstrDesc &TID, unsigned RCID, 5395 bool IsAllocatable) { 5396 if ((IsAllocatable || !ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) && 5397 (((TID.mayLoad() || TID.mayStore()) && 5398 !(TID.TSFlags & SIInstrFlags::VGPRSpill)) || 5399 (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::MIMG)))) { 5400 switch (RCID) { 5401 case AMDGPU::AV_32RegClassID: 5402 RCID = AMDGPU::VGPR_32RegClassID; 5403 break; 5404 case AMDGPU::AV_64RegClassID: 5405 RCID = AMDGPU::VReg_64RegClassID; 5406 break; 5407 case AMDGPU::AV_96RegClassID: 5408 RCID = AMDGPU::VReg_96RegClassID; 5409 break; 5410 case AMDGPU::AV_128RegClassID: 5411 RCID = AMDGPU::VReg_128RegClassID; 5412 break; 5413 case AMDGPU::AV_160RegClassID: 5414 RCID = AMDGPU::VReg_160RegClassID; 5415 break; 5416 case AMDGPU::AV_512RegClassID: 5417 RCID = AMDGPU::VReg_512RegClassID; 5418 break; 5419 default: 5420 break; 5421 } 5422 } 5423 5424 return RI.getProperlyAlignedRC(RI.getRegClass(RCID)); 5425 } 5426 5427 const TargetRegisterClass *SIInstrInfo::getRegClass(const MCInstrDesc &TID, 5428 unsigned OpNum, const TargetRegisterInfo *TRI, 5429 const MachineFunction &MF) 5430 const { 5431 if (OpNum >= TID.getNumOperands()) 5432 return nullptr; 5433 auto RegClass = TID.operands()[OpNum].RegClass; 5434 bool IsAllocatable = false; 5435 if (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::FLAT)) { 5436 // vdst and vdata should be both VGPR or AGPR, same for the DS instructions 5437 // with two data operands. Request register class constrained to VGPR only 5438 // of both operands present as Machine Copy Propagation can not check this 5439 // constraint and possibly other passes too. 5440 // 5441 // The check is limited to FLAT and DS because atomics in non-flat encoding 5442 // have their vdst and vdata tied to be the same register. 5443 const int VDstIdx = AMDGPU::getNamedOperandIdx(TID.Opcode, 5444 AMDGPU::OpName::vdst); 5445 const int DataIdx = AMDGPU::getNamedOperandIdx(TID.Opcode, 5446 (TID.TSFlags & SIInstrFlags::DS) ? AMDGPU::OpName::data0 5447 : AMDGPU::OpName::vdata); 5448 if (DataIdx != -1) { 5449 IsAllocatable = VDstIdx != -1 || AMDGPU::hasNamedOperand( 5450 TID.Opcode, AMDGPU::OpName::data1); 5451 } 5452 } 5453 return adjustAllocatableRegClass(ST, RI, MF.getRegInfo(), TID, RegClass, 5454 IsAllocatable); 5455 } 5456 5457 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI, 5458 unsigned OpNo) const { 5459 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 5460 const MCInstrDesc &Desc = get(MI.getOpcode()); 5461 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() || 5462 Desc.operands()[OpNo].RegClass == -1) { 5463 Register Reg = MI.getOperand(OpNo).getReg(); 5464 5465 if (Reg.isVirtual()) 5466 return MRI.getRegClass(Reg); 5467 return RI.getPhysRegBaseClass(Reg); 5468 } 5469 5470 unsigned RCID = Desc.operands()[OpNo].RegClass; 5471 return adjustAllocatableRegClass(ST, RI, MRI, Desc, RCID, true); 5472 } 5473 5474 void SIInstrInfo::legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const { 5475 MachineBasicBlock::iterator I = MI; 5476 MachineBasicBlock *MBB = MI.getParent(); 5477 MachineOperand &MO = MI.getOperand(OpIdx); 5478 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 5479 unsigned RCID = get(MI.getOpcode()).operands()[OpIdx].RegClass; 5480 const TargetRegisterClass *RC = RI.getRegClass(RCID); 5481 unsigned Size = RI.getRegSizeInBits(*RC); 5482 unsigned Opcode = (Size == 64) ? AMDGPU::V_MOV_B64_PSEUDO : AMDGPU::V_MOV_B32_e32; 5483 if (MO.isReg()) 5484 Opcode = AMDGPU::COPY; 5485 else if (RI.isSGPRClass(RC)) 5486 Opcode = (Size == 64) ? AMDGPU::S_MOV_B64 : AMDGPU::S_MOV_B32; 5487 5488 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC); 5489 Register Reg = MRI.createVirtualRegister(VRC); 5490 DebugLoc DL = MBB->findDebugLoc(I); 5491 BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).add(MO); 5492 MO.ChangeToRegister(Reg, false); 5493 } 5494 5495 unsigned SIInstrInfo::buildExtractSubReg( 5496 MachineBasicBlock::iterator MI, MachineRegisterInfo &MRI, 5497 const MachineOperand &SuperReg, const TargetRegisterClass *SuperRC, 5498 unsigned SubIdx, const TargetRegisterClass *SubRC) const { 5499 MachineBasicBlock *MBB = MI->getParent(); 5500 DebugLoc DL = MI->getDebugLoc(); 5501 Register SubReg = MRI.createVirtualRegister(SubRC); 5502 5503 if (SuperReg.getSubReg() == AMDGPU::NoSubRegister) { 5504 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 5505 .addReg(SuperReg.getReg(), 0, SubIdx); 5506 return SubReg; 5507 } 5508 5509 // Just in case the super register is itself a sub-register, copy it to a new 5510 // value so we don't need to worry about merging its subreg index with the 5511 // SubIdx passed to this function. The register coalescer should be able to 5512 // eliminate this extra copy. 5513 Register NewSuperReg = MRI.createVirtualRegister(SuperRC); 5514 5515 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg) 5516 .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg()); 5517 5518 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 5519 .addReg(NewSuperReg, 0, SubIdx); 5520 5521 return SubReg; 5522 } 5523 5524 MachineOperand SIInstrInfo::buildExtractSubRegOrImm( 5525 MachineBasicBlock::iterator MII, MachineRegisterInfo &MRI, 5526 const MachineOperand &Op, const TargetRegisterClass *SuperRC, 5527 unsigned SubIdx, const TargetRegisterClass *SubRC) const { 5528 if (Op.isImm()) { 5529 if (SubIdx == AMDGPU::sub0) 5530 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm())); 5531 if (SubIdx == AMDGPU::sub1) 5532 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32)); 5533 5534 llvm_unreachable("Unhandled register index for immediate"); 5535 } 5536 5537 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC, 5538 SubIdx, SubRC); 5539 return MachineOperand::CreateReg(SubReg, false); 5540 } 5541 5542 // Change the order of operands from (0, 1, 2) to (0, 2, 1) 5543 void SIInstrInfo::swapOperands(MachineInstr &Inst) const { 5544 assert(Inst.getNumExplicitOperands() == 3); 5545 MachineOperand Op1 = Inst.getOperand(1); 5546 Inst.removeOperand(1); 5547 Inst.addOperand(Op1); 5548 } 5549 5550 bool SIInstrInfo::isLegalRegOperand(const MachineRegisterInfo &MRI, 5551 const MCOperandInfo &OpInfo, 5552 const MachineOperand &MO) const { 5553 if (!MO.isReg()) 5554 return false; 5555 5556 Register Reg = MO.getReg(); 5557 5558 const TargetRegisterClass *DRC = RI.getRegClass(OpInfo.RegClass); 5559 if (Reg.isPhysical()) 5560 return DRC->contains(Reg); 5561 5562 const TargetRegisterClass *RC = MRI.getRegClass(Reg); 5563 5564 if (MO.getSubReg()) { 5565 const MachineFunction *MF = MO.getParent()->getParent()->getParent(); 5566 const TargetRegisterClass *SuperRC = RI.getLargestLegalSuperClass(RC, *MF); 5567 if (!SuperRC) 5568 return false; 5569 5570 DRC = RI.getMatchingSuperRegClass(SuperRC, DRC, MO.getSubReg()); 5571 if (!DRC) 5572 return false; 5573 } 5574 return RC->hasSuperClassEq(DRC); 5575 } 5576 5577 bool SIInstrInfo::isLegalVSrcOperand(const MachineRegisterInfo &MRI, 5578 const MCOperandInfo &OpInfo, 5579 const MachineOperand &MO) const { 5580 if (MO.isReg()) 5581 return isLegalRegOperand(MRI, OpInfo, MO); 5582 5583 // Handle non-register types that are treated like immediates. 5584 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal()); 5585 return true; 5586 } 5587 5588 bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx, 5589 const MachineOperand *MO) const { 5590 const MachineFunction &MF = *MI.getParent()->getParent(); 5591 const MachineRegisterInfo &MRI = MF.getRegInfo(); 5592 const MCInstrDesc &InstDesc = MI.getDesc(); 5593 const MCOperandInfo &OpInfo = InstDesc.operands()[OpIdx]; 5594 const TargetRegisterClass *DefinedRC = 5595 OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr; 5596 if (!MO) 5597 MO = &MI.getOperand(OpIdx); 5598 5599 int ConstantBusLimit = ST.getConstantBusLimit(MI.getOpcode()); 5600 int LiteralLimit = !isVOP3(MI) || ST.hasVOP3Literal() ? 1 : 0; 5601 if (isVALU(MI) && usesConstantBus(MRI, *MO, OpInfo)) { 5602 if (!MO->isReg() && !isInlineConstant(*MO, OpInfo) && !LiteralLimit--) 5603 return false; 5604 5605 SmallDenseSet<RegSubRegPair> SGPRsUsed; 5606 if (MO->isReg()) 5607 SGPRsUsed.insert(RegSubRegPair(MO->getReg(), MO->getSubReg())); 5608 5609 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 5610 if (i == OpIdx) 5611 continue; 5612 const MachineOperand &Op = MI.getOperand(i); 5613 if (Op.isReg()) { 5614 RegSubRegPair SGPR(Op.getReg(), Op.getSubReg()); 5615 if (!SGPRsUsed.count(SGPR) && 5616 // FIXME: This can access off the end of the operands() array. 5617 usesConstantBus(MRI, Op, InstDesc.operands().begin()[i])) { 5618 if (--ConstantBusLimit <= 0) 5619 return false; 5620 SGPRsUsed.insert(SGPR); 5621 } 5622 } else if (AMDGPU::isSISrcOperand(InstDesc, i) && 5623 !isInlineConstant(Op, InstDesc.operands()[i])) { 5624 if (!LiteralLimit--) 5625 return false; 5626 if (--ConstantBusLimit <= 0) 5627 return false; 5628 } 5629 } 5630 } 5631 5632 if (MO->isReg()) { 5633 if (!DefinedRC) 5634 return OpInfo.OperandType == MCOI::OPERAND_UNKNOWN; 5635 if (!isLegalRegOperand(MRI, OpInfo, *MO)) 5636 return false; 5637 bool IsAGPR = RI.isAGPR(MRI, MO->getReg()); 5638 if (IsAGPR && !ST.hasMAIInsts()) 5639 return false; 5640 unsigned Opc = MI.getOpcode(); 5641 if (IsAGPR && 5642 (!ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) && 5643 (MI.mayLoad() || MI.mayStore() || isDS(Opc) || isMIMG(Opc))) 5644 return false; 5645 // Atomics should have both vdst and vdata either vgpr or agpr. 5646 const int VDstIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 5647 const int DataIdx = AMDGPU::getNamedOperandIdx(Opc, 5648 isDS(Opc) ? AMDGPU::OpName::data0 : AMDGPU::OpName::vdata); 5649 if ((int)OpIdx == VDstIdx && DataIdx != -1 && 5650 MI.getOperand(DataIdx).isReg() && 5651 RI.isAGPR(MRI, MI.getOperand(DataIdx).getReg()) != IsAGPR) 5652 return false; 5653 if ((int)OpIdx == DataIdx) { 5654 if (VDstIdx != -1 && 5655 RI.isAGPR(MRI, MI.getOperand(VDstIdx).getReg()) != IsAGPR) 5656 return false; 5657 // DS instructions with 2 src operands also must have tied RC. 5658 const int Data1Idx = AMDGPU::getNamedOperandIdx(Opc, 5659 AMDGPU::OpName::data1); 5660 if (Data1Idx != -1 && MI.getOperand(Data1Idx).isReg() && 5661 RI.isAGPR(MRI, MI.getOperand(Data1Idx).getReg()) != IsAGPR) 5662 return false; 5663 } 5664 if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64 && !ST.hasGFX90AInsts() && 5665 (int)OpIdx == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) && 5666 RI.isSGPRReg(MRI, MO->getReg())) 5667 return false; 5668 return true; 5669 } 5670 5671 if (MO->isImm()) { 5672 uint64_t Imm = MO->getImm(); 5673 bool Is64BitFPOp = OpInfo.OperandType == AMDGPU::OPERAND_REG_IMM_FP64; 5674 bool Is64BitOp = Is64BitFPOp || 5675 OpInfo.OperandType == AMDGPU::OPERAND_REG_IMM_INT64 || 5676 OpInfo.OperandType == AMDGPU::OPERAND_REG_IMM_V2INT32 || 5677 OpInfo.OperandType == AMDGPU::OPERAND_REG_IMM_V2FP32; 5678 if (Is64BitOp && 5679 !AMDGPU::isInlinableLiteral64(Imm, ST.hasInv2PiInlineImm())) { 5680 if (!AMDGPU::isValid32BitLiteral(Imm, Is64BitFPOp)) 5681 return false; 5682 5683 // FIXME: We can use sign extended 64-bit literals, but only for signed 5684 // operands. At the moment we do not know if an operand is signed. 5685 // Such operand will be encoded as its low 32 bits and then either 5686 // correctly sign extended or incorrectly zero extended by HW. 5687 if (!Is64BitFPOp && (int32_t)Imm < 0) 5688 return false; 5689 } 5690 } 5691 5692 // Handle non-register types that are treated like immediates. 5693 assert(MO->isImm() || MO->isTargetIndex() || MO->isFI() || MO->isGlobal()); 5694 5695 if (!DefinedRC) { 5696 // This operand expects an immediate. 5697 return true; 5698 } 5699 5700 return isImmOperandLegal(MI, OpIdx, *MO); 5701 } 5702 5703 void SIInstrInfo::legalizeOperandsVOP2(MachineRegisterInfo &MRI, 5704 MachineInstr &MI) const { 5705 unsigned Opc = MI.getOpcode(); 5706 const MCInstrDesc &InstrDesc = get(Opc); 5707 5708 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 5709 MachineOperand &Src0 = MI.getOperand(Src0Idx); 5710 5711 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 5712 MachineOperand &Src1 = MI.getOperand(Src1Idx); 5713 5714 // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32 5715 // we need to only have one constant bus use before GFX10. 5716 bool HasImplicitSGPR = findImplicitSGPRRead(MI); 5717 if (HasImplicitSGPR && ST.getConstantBusLimit(Opc) <= 1 && Src0.isReg() && 5718 RI.isSGPRReg(MRI, Src0.getReg())) 5719 legalizeOpWithMove(MI, Src0Idx); 5720 5721 // Special case: V_WRITELANE_B32 accepts only immediate or SGPR operands for 5722 // both the value to write (src0) and lane select (src1). Fix up non-SGPR 5723 // src0/src1 with V_READFIRSTLANE. 5724 if (Opc == AMDGPU::V_WRITELANE_B32) { 5725 const DebugLoc &DL = MI.getDebugLoc(); 5726 if (Src0.isReg() && RI.isVGPR(MRI, Src0.getReg())) { 5727 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5728 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5729 .add(Src0); 5730 Src0.ChangeToRegister(Reg, false); 5731 } 5732 if (Src1.isReg() && RI.isVGPR(MRI, Src1.getReg())) { 5733 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5734 const DebugLoc &DL = MI.getDebugLoc(); 5735 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5736 .add(Src1); 5737 Src1.ChangeToRegister(Reg, false); 5738 } 5739 return; 5740 } 5741 5742 // No VOP2 instructions support AGPRs. 5743 if (Src0.isReg() && RI.isAGPR(MRI, Src0.getReg())) 5744 legalizeOpWithMove(MI, Src0Idx); 5745 5746 if (Src1.isReg() && RI.isAGPR(MRI, Src1.getReg())) 5747 legalizeOpWithMove(MI, Src1Idx); 5748 5749 // Special case: V_FMAC_F32 and V_FMAC_F16 have src2. 5750 if (Opc == AMDGPU::V_FMAC_F32_e32 || Opc == AMDGPU::V_FMAC_F16_e32) { 5751 int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); 5752 if (!RI.isVGPR(MRI, MI.getOperand(Src2Idx).getReg())) 5753 legalizeOpWithMove(MI, Src2Idx); 5754 } 5755 5756 // VOP2 src0 instructions support all operand types, so we don't need to check 5757 // their legality. If src1 is already legal, we don't need to do anything. 5758 if (isLegalRegOperand(MRI, InstrDesc.operands()[Src1Idx], Src1)) 5759 return; 5760 5761 // Special case: V_READLANE_B32 accepts only immediate or SGPR operands for 5762 // lane select. Fix up using V_READFIRSTLANE, since we assume that the lane 5763 // select is uniform. 5764 if (Opc == AMDGPU::V_READLANE_B32 && Src1.isReg() && 5765 RI.isVGPR(MRI, Src1.getReg())) { 5766 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5767 const DebugLoc &DL = MI.getDebugLoc(); 5768 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5769 .add(Src1); 5770 Src1.ChangeToRegister(Reg, false); 5771 return; 5772 } 5773 5774 // We do not use commuteInstruction here because it is too aggressive and will 5775 // commute if it is possible. We only want to commute here if it improves 5776 // legality. This can be called a fairly large number of times so don't waste 5777 // compile time pointlessly swapping and checking legality again. 5778 if (HasImplicitSGPR || !MI.isCommutable()) { 5779 legalizeOpWithMove(MI, Src1Idx); 5780 return; 5781 } 5782 5783 // If src0 can be used as src1, commuting will make the operands legal. 5784 // Otherwise we have to give up and insert a move. 5785 // 5786 // TODO: Other immediate-like operand kinds could be commuted if there was a 5787 // MachineOperand::ChangeTo* for them. 5788 if ((!Src1.isImm() && !Src1.isReg()) || 5789 !isLegalRegOperand(MRI, InstrDesc.operands()[Src1Idx], Src0)) { 5790 legalizeOpWithMove(MI, Src1Idx); 5791 return; 5792 } 5793 5794 int CommutedOpc = commuteOpcode(MI); 5795 if (CommutedOpc == -1) { 5796 legalizeOpWithMove(MI, Src1Idx); 5797 return; 5798 } 5799 5800 MI.setDesc(get(CommutedOpc)); 5801 5802 Register Src0Reg = Src0.getReg(); 5803 unsigned Src0SubReg = Src0.getSubReg(); 5804 bool Src0Kill = Src0.isKill(); 5805 5806 if (Src1.isImm()) 5807 Src0.ChangeToImmediate(Src1.getImm()); 5808 else if (Src1.isReg()) { 5809 Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill()); 5810 Src0.setSubReg(Src1.getSubReg()); 5811 } else 5812 llvm_unreachable("Should only have register or immediate operands"); 5813 5814 Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill); 5815 Src1.setSubReg(Src0SubReg); 5816 fixImplicitOperands(MI); 5817 } 5818 5819 // Legalize VOP3 operands. All operand types are supported for any operand 5820 // but only one literal constant and only starting from GFX10. 5821 void SIInstrInfo::legalizeOperandsVOP3(MachineRegisterInfo &MRI, 5822 MachineInstr &MI) const { 5823 unsigned Opc = MI.getOpcode(); 5824 5825 int VOP3Idx[3] = { 5826 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0), 5827 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1), 5828 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2) 5829 }; 5830 5831 if (Opc == AMDGPU::V_PERMLANE16_B32_e64 || 5832 Opc == AMDGPU::V_PERMLANEX16_B32_e64) { 5833 // src1 and src2 must be scalar 5834 MachineOperand &Src1 = MI.getOperand(VOP3Idx[1]); 5835 MachineOperand &Src2 = MI.getOperand(VOP3Idx[2]); 5836 const DebugLoc &DL = MI.getDebugLoc(); 5837 if (Src1.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) { 5838 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5839 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5840 .add(Src1); 5841 Src1.ChangeToRegister(Reg, false); 5842 } 5843 if (Src2.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src2.getReg()))) { 5844 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5845 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5846 .add(Src2); 5847 Src2.ChangeToRegister(Reg, false); 5848 } 5849 } 5850 5851 // Find the one SGPR operand we are allowed to use. 5852 int ConstantBusLimit = ST.getConstantBusLimit(Opc); 5853 int LiteralLimit = ST.hasVOP3Literal() ? 1 : 0; 5854 SmallDenseSet<unsigned> SGPRsUsed; 5855 Register SGPRReg = findUsedSGPR(MI, VOP3Idx); 5856 if (SGPRReg) { 5857 SGPRsUsed.insert(SGPRReg); 5858 --ConstantBusLimit; 5859 } 5860 5861 for (int Idx : VOP3Idx) { 5862 if (Idx == -1) 5863 break; 5864 MachineOperand &MO = MI.getOperand(Idx); 5865 5866 if (!MO.isReg()) { 5867 if (isInlineConstant(MO, get(Opc).operands()[Idx])) 5868 continue; 5869 5870 if (LiteralLimit > 0 && ConstantBusLimit > 0) { 5871 --LiteralLimit; 5872 --ConstantBusLimit; 5873 continue; 5874 } 5875 5876 --LiteralLimit; 5877 --ConstantBusLimit; 5878 legalizeOpWithMove(MI, Idx); 5879 continue; 5880 } 5881 5882 if (RI.hasAGPRs(RI.getRegClassForReg(MRI, MO.getReg())) && 5883 !isOperandLegal(MI, Idx, &MO)) { 5884 legalizeOpWithMove(MI, Idx); 5885 continue; 5886 } 5887 5888 if (!RI.isSGPRClass(RI.getRegClassForReg(MRI, MO.getReg()))) 5889 continue; // VGPRs are legal 5890 5891 // We can use one SGPR in each VOP3 instruction prior to GFX10 5892 // and two starting from GFX10. 5893 if (SGPRsUsed.count(MO.getReg())) 5894 continue; 5895 if (ConstantBusLimit > 0) { 5896 SGPRsUsed.insert(MO.getReg()); 5897 --ConstantBusLimit; 5898 continue; 5899 } 5900 5901 // If we make it this far, then the operand is not legal and we must 5902 // legalize it. 5903 legalizeOpWithMove(MI, Idx); 5904 } 5905 5906 // Special case: V_FMAC_F32 and V_FMAC_F16 have src2 tied to vdst. 5907 if ((Opc == AMDGPU::V_FMAC_F32_e64 || Opc == AMDGPU::V_FMAC_F16_e64) && 5908 !RI.isVGPR(MRI, MI.getOperand(VOP3Idx[2]).getReg())) 5909 legalizeOpWithMove(MI, VOP3Idx[2]); 5910 } 5911 5912 Register SIInstrInfo::readlaneVGPRToSGPR(Register SrcReg, MachineInstr &UseMI, 5913 MachineRegisterInfo &MRI) const { 5914 const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg); 5915 const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC); 5916 Register DstReg = MRI.createVirtualRegister(SRC); 5917 unsigned SubRegs = RI.getRegSizeInBits(*VRC) / 32; 5918 5919 if (RI.hasAGPRs(VRC)) { 5920 VRC = RI.getEquivalentVGPRClass(VRC); 5921 Register NewSrcReg = MRI.createVirtualRegister(VRC); 5922 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5923 get(TargetOpcode::COPY), NewSrcReg) 5924 .addReg(SrcReg); 5925 SrcReg = NewSrcReg; 5926 } 5927 5928 if (SubRegs == 1) { 5929 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5930 get(AMDGPU::V_READFIRSTLANE_B32), DstReg) 5931 .addReg(SrcReg); 5932 return DstReg; 5933 } 5934 5935 SmallVector<Register, 8> SRegs; 5936 for (unsigned i = 0; i < SubRegs; ++i) { 5937 Register SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5938 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5939 get(AMDGPU::V_READFIRSTLANE_B32), SGPR) 5940 .addReg(SrcReg, 0, RI.getSubRegFromChannel(i)); 5941 SRegs.push_back(SGPR); 5942 } 5943 5944 MachineInstrBuilder MIB = 5945 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5946 get(AMDGPU::REG_SEQUENCE), DstReg); 5947 for (unsigned i = 0; i < SubRegs; ++i) { 5948 MIB.addReg(SRegs[i]); 5949 MIB.addImm(RI.getSubRegFromChannel(i)); 5950 } 5951 return DstReg; 5952 } 5953 5954 void SIInstrInfo::legalizeOperandsSMRD(MachineRegisterInfo &MRI, 5955 MachineInstr &MI) const { 5956 5957 // If the pointer is store in VGPRs, then we need to move them to 5958 // SGPRs using v_readfirstlane. This is safe because we only select 5959 // loads with uniform pointers to SMRD instruction so we know the 5960 // pointer value is uniform. 5961 MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase); 5962 if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) { 5963 Register SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI); 5964 SBase->setReg(SGPR); 5965 } 5966 MachineOperand *SOff = getNamedOperand(MI, AMDGPU::OpName::soffset); 5967 if (SOff && !RI.isSGPRClass(MRI.getRegClass(SOff->getReg()))) { 5968 Register SGPR = readlaneVGPRToSGPR(SOff->getReg(), MI, MRI); 5969 SOff->setReg(SGPR); 5970 } 5971 } 5972 5973 bool SIInstrInfo::moveFlatAddrToVGPR(MachineInstr &Inst) const { 5974 unsigned Opc = Inst.getOpcode(); 5975 int OldSAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::saddr); 5976 if (OldSAddrIdx < 0) 5977 return false; 5978 5979 assert(isSegmentSpecificFLAT(Inst)); 5980 5981 int NewOpc = AMDGPU::getGlobalVaddrOp(Opc); 5982 if (NewOpc < 0) 5983 NewOpc = AMDGPU::getFlatScratchInstSVfromSS(Opc); 5984 if (NewOpc < 0) 5985 return false; 5986 5987 MachineRegisterInfo &MRI = Inst.getMF()->getRegInfo(); 5988 MachineOperand &SAddr = Inst.getOperand(OldSAddrIdx); 5989 if (RI.isSGPRReg(MRI, SAddr.getReg())) 5990 return false; 5991 5992 int NewVAddrIdx = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vaddr); 5993 if (NewVAddrIdx < 0) 5994 return false; 5995 5996 int OldVAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr); 5997 5998 // Check vaddr, it shall be zero or absent. 5999 MachineInstr *VAddrDef = nullptr; 6000 if (OldVAddrIdx >= 0) { 6001 MachineOperand &VAddr = Inst.getOperand(OldVAddrIdx); 6002 VAddrDef = MRI.getUniqueVRegDef(VAddr.getReg()); 6003 if (!VAddrDef || VAddrDef->getOpcode() != AMDGPU::V_MOV_B32_e32 || 6004 !VAddrDef->getOperand(1).isImm() || 6005 VAddrDef->getOperand(1).getImm() != 0) 6006 return false; 6007 } 6008 6009 const MCInstrDesc &NewDesc = get(NewOpc); 6010 Inst.setDesc(NewDesc); 6011 6012 // Callers expect iterator to be valid after this call, so modify the 6013 // instruction in place. 6014 if (OldVAddrIdx == NewVAddrIdx) { 6015 MachineOperand &NewVAddr = Inst.getOperand(NewVAddrIdx); 6016 // Clear use list from the old vaddr holding a zero register. 6017 MRI.removeRegOperandFromUseList(&NewVAddr); 6018 MRI.moveOperands(&NewVAddr, &SAddr, 1); 6019 Inst.removeOperand(OldSAddrIdx); 6020 // Update the use list with the pointer we have just moved from vaddr to 6021 // saddr position. Otherwise new vaddr will be missing from the use list. 6022 MRI.removeRegOperandFromUseList(&NewVAddr); 6023 MRI.addRegOperandToUseList(&NewVAddr); 6024 } else { 6025 assert(OldSAddrIdx == NewVAddrIdx); 6026 6027 if (OldVAddrIdx >= 0) { 6028 int NewVDstIn = AMDGPU::getNamedOperandIdx(NewOpc, 6029 AMDGPU::OpName::vdst_in); 6030 6031 // removeOperand doesn't try to fixup tied operand indexes at it goes, so 6032 // it asserts. Untie the operands for now and retie them afterwards. 6033 if (NewVDstIn != -1) { 6034 int OldVDstIn = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst_in); 6035 Inst.untieRegOperand(OldVDstIn); 6036 } 6037 6038 Inst.removeOperand(OldVAddrIdx); 6039 6040 if (NewVDstIn != -1) { 6041 int NewVDst = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vdst); 6042 Inst.tieOperands(NewVDst, NewVDstIn); 6043 } 6044 } 6045 } 6046 6047 if (VAddrDef && MRI.use_nodbg_empty(VAddrDef->getOperand(0).getReg())) 6048 VAddrDef->eraseFromParent(); 6049 6050 return true; 6051 } 6052 6053 // FIXME: Remove this when SelectionDAG is obsoleted. 6054 void SIInstrInfo::legalizeOperandsFLAT(MachineRegisterInfo &MRI, 6055 MachineInstr &MI) const { 6056 if (!isSegmentSpecificFLAT(MI)) 6057 return; 6058 6059 // Fixup SGPR operands in VGPRs. We only select these when the DAG divergence 6060 // thinks they are uniform, so a readfirstlane should be valid. 6061 MachineOperand *SAddr = getNamedOperand(MI, AMDGPU::OpName::saddr); 6062 if (!SAddr || RI.isSGPRClass(MRI.getRegClass(SAddr->getReg()))) 6063 return; 6064 6065 if (moveFlatAddrToVGPR(MI)) 6066 return; 6067 6068 Register ToSGPR = readlaneVGPRToSGPR(SAddr->getReg(), MI, MRI); 6069 SAddr->setReg(ToSGPR); 6070 } 6071 6072 void SIInstrInfo::legalizeGenericOperand(MachineBasicBlock &InsertMBB, 6073 MachineBasicBlock::iterator I, 6074 const TargetRegisterClass *DstRC, 6075 MachineOperand &Op, 6076 MachineRegisterInfo &MRI, 6077 const DebugLoc &DL) const { 6078 Register OpReg = Op.getReg(); 6079 unsigned OpSubReg = Op.getSubReg(); 6080 6081 const TargetRegisterClass *OpRC = RI.getSubClassWithSubReg( 6082 RI.getRegClassForReg(MRI, OpReg), OpSubReg); 6083 6084 // Check if operand is already the correct register class. 6085 if (DstRC == OpRC) 6086 return; 6087 6088 Register DstReg = MRI.createVirtualRegister(DstRC); 6089 auto Copy = BuildMI(InsertMBB, I, DL, get(AMDGPU::COPY), DstReg).add(Op); 6090 6091 Op.setReg(DstReg); 6092 Op.setSubReg(0); 6093 6094 MachineInstr *Def = MRI.getVRegDef(OpReg); 6095 if (!Def) 6096 return; 6097 6098 // Try to eliminate the copy if it is copying an immediate value. 6099 if (Def->isMoveImmediate() && DstRC != &AMDGPU::VReg_1RegClass) 6100 FoldImmediate(*Copy, *Def, OpReg, &MRI); 6101 6102 bool ImpDef = Def->isImplicitDef(); 6103 while (!ImpDef && Def && Def->isCopy()) { 6104 if (Def->getOperand(1).getReg().isPhysical()) 6105 break; 6106 Def = MRI.getUniqueVRegDef(Def->getOperand(1).getReg()); 6107 ImpDef = Def && Def->isImplicitDef(); 6108 } 6109 if (!RI.isSGPRClass(DstRC) && !Copy->readsRegister(AMDGPU::EXEC, &RI) && 6110 !ImpDef) 6111 Copy.addReg(AMDGPU::EXEC, RegState::Implicit); 6112 } 6113 6114 // Emit the actual waterfall loop, executing the wrapped instruction for each 6115 // unique value of \p ScalarOps across all lanes. In the best case we execute 1 6116 // iteration, in the worst case we execute 64 (once per lane). 6117 static void emitLoadScalarOpsFromVGPRLoop( 6118 const SIInstrInfo &TII, MachineRegisterInfo &MRI, MachineBasicBlock &OrigBB, 6119 MachineBasicBlock &LoopBB, MachineBasicBlock &BodyBB, const DebugLoc &DL, 6120 ArrayRef<MachineOperand *> ScalarOps) { 6121 MachineFunction &MF = *OrigBB.getParent(); 6122 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 6123 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 6124 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 6125 unsigned SaveExecOpc = 6126 ST.isWave32() ? AMDGPU::S_AND_SAVEEXEC_B32 : AMDGPU::S_AND_SAVEEXEC_B64; 6127 unsigned XorTermOpc = 6128 ST.isWave32() ? AMDGPU::S_XOR_B32_term : AMDGPU::S_XOR_B64_term; 6129 unsigned AndOpc = 6130 ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64; 6131 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 6132 6133 MachineBasicBlock::iterator I = LoopBB.begin(); 6134 6135 SmallVector<Register, 8> ReadlanePieces; 6136 Register CondReg; 6137 6138 for (MachineOperand *ScalarOp : ScalarOps) { 6139 unsigned RegSize = TRI->getRegSizeInBits(ScalarOp->getReg(), MRI); 6140 unsigned NumSubRegs = RegSize / 32; 6141 Register VScalarOp = ScalarOp->getReg(); 6142 6143 if (NumSubRegs == 1) { 6144 Register CurReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 6145 6146 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurReg) 6147 .addReg(VScalarOp); 6148 6149 Register NewCondReg = MRI.createVirtualRegister(BoolXExecRC); 6150 6151 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U32_e64), NewCondReg) 6152 .addReg(CurReg) 6153 .addReg(VScalarOp); 6154 6155 // Combine the comparison results with AND. 6156 if (!CondReg) // First. 6157 CondReg = NewCondReg; 6158 else { // If not the first, we create an AND. 6159 Register AndReg = MRI.createVirtualRegister(BoolXExecRC); 6160 BuildMI(LoopBB, I, DL, TII.get(AndOpc), AndReg) 6161 .addReg(CondReg) 6162 .addReg(NewCondReg); 6163 CondReg = AndReg; 6164 } 6165 6166 // Update ScalarOp operand to use the SGPR ScalarOp. 6167 ScalarOp->setReg(CurReg); 6168 ScalarOp->setIsKill(); 6169 } else { 6170 unsigned VScalarOpUndef = getUndefRegState(ScalarOp->isUndef()); 6171 assert(NumSubRegs % 2 == 0 && NumSubRegs <= 32 && 6172 "Unhandled register size"); 6173 6174 for (unsigned Idx = 0; Idx < NumSubRegs; Idx += 2) { 6175 Register CurRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 6176 Register CurRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 6177 6178 // Read the next variant <- also loop target. 6179 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegLo) 6180 .addReg(VScalarOp, VScalarOpUndef, TRI->getSubRegFromChannel(Idx)); 6181 6182 // Read the next variant <- also loop target. 6183 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegHi) 6184 .addReg(VScalarOp, VScalarOpUndef, 6185 TRI->getSubRegFromChannel(Idx + 1)); 6186 6187 ReadlanePieces.push_back(CurRegLo); 6188 ReadlanePieces.push_back(CurRegHi); 6189 6190 // Comparison is to be done as 64-bit. 6191 Register CurReg = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass); 6192 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), CurReg) 6193 .addReg(CurRegLo) 6194 .addImm(AMDGPU::sub0) 6195 .addReg(CurRegHi) 6196 .addImm(AMDGPU::sub1); 6197 6198 Register NewCondReg = MRI.createVirtualRegister(BoolXExecRC); 6199 auto Cmp = BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64), 6200 NewCondReg) 6201 .addReg(CurReg); 6202 if (NumSubRegs <= 2) 6203 Cmp.addReg(VScalarOp); 6204 else 6205 Cmp.addReg(VScalarOp, VScalarOpUndef, 6206 TRI->getSubRegFromChannel(Idx, 2)); 6207 6208 // Combine the comparison results with AND. 6209 if (!CondReg) // First. 6210 CondReg = NewCondReg; 6211 else { // If not the first, we create an AND. 6212 Register AndReg = MRI.createVirtualRegister(BoolXExecRC); 6213 BuildMI(LoopBB, I, DL, TII.get(AndOpc), AndReg) 6214 .addReg(CondReg) 6215 .addReg(NewCondReg); 6216 CondReg = AndReg; 6217 } 6218 } // End for loop. 6219 6220 auto SScalarOpRC = 6221 TRI->getEquivalentSGPRClass(MRI.getRegClass(VScalarOp)); 6222 Register SScalarOp = MRI.createVirtualRegister(SScalarOpRC); 6223 6224 // Build scalar ScalarOp. 6225 auto Merge = 6226 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), SScalarOp); 6227 unsigned Channel = 0; 6228 for (Register Piece : ReadlanePieces) { 6229 Merge.addReg(Piece).addImm(TRI->getSubRegFromChannel(Channel++)); 6230 } 6231 6232 // Update ScalarOp operand to use the SGPR ScalarOp. 6233 ScalarOp->setReg(SScalarOp); 6234 ScalarOp->setIsKill(); 6235 } 6236 } 6237 6238 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC); 6239 MRI.setSimpleHint(SaveExec, CondReg); 6240 6241 // Update EXEC to matching lanes, saving original to SaveExec. 6242 BuildMI(LoopBB, I, DL, TII.get(SaveExecOpc), SaveExec) 6243 .addReg(CondReg, RegState::Kill); 6244 6245 // The original instruction is here; we insert the terminators after it. 6246 I = BodyBB.end(); 6247 6248 // Update EXEC, switch all done bits to 0 and all todo bits to 1. 6249 BuildMI(BodyBB, I, DL, TII.get(XorTermOpc), Exec) 6250 .addReg(Exec) 6251 .addReg(SaveExec); 6252 6253 BuildMI(BodyBB, I, DL, TII.get(AMDGPU::SI_WATERFALL_LOOP)).addMBB(&LoopBB); 6254 } 6255 6256 // Build a waterfall loop around \p MI, replacing the VGPR \p ScalarOp register 6257 // with SGPRs by iterating over all unique values across all lanes. 6258 // Returns the loop basic block that now contains \p MI. 6259 static MachineBasicBlock * 6260 loadMBUFScalarOperandsFromVGPR(const SIInstrInfo &TII, MachineInstr &MI, 6261 ArrayRef<MachineOperand *> ScalarOps, 6262 MachineDominatorTree *MDT, 6263 MachineBasicBlock::iterator Begin = nullptr, 6264 MachineBasicBlock::iterator End = nullptr) { 6265 MachineBasicBlock &MBB = *MI.getParent(); 6266 MachineFunction &MF = *MBB.getParent(); 6267 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 6268 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 6269 MachineRegisterInfo &MRI = MF.getRegInfo(); 6270 if (!Begin.isValid()) 6271 Begin = &MI; 6272 if (!End.isValid()) { 6273 End = &MI; 6274 ++End; 6275 } 6276 const DebugLoc &DL = MI.getDebugLoc(); 6277 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 6278 unsigned MovExecOpc = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64; 6279 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 6280 6281 // Save SCC. Waterfall Loop may overwrite SCC. 6282 Register SaveSCCReg; 6283 bool SCCNotDead = (MBB.computeRegisterLiveness(TRI, AMDGPU::SCC, MI, 30) != 6284 MachineBasicBlock::LQR_Dead); 6285 if (SCCNotDead) { 6286 SaveSCCReg = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6287 BuildMI(MBB, Begin, DL, TII.get(AMDGPU::S_CSELECT_B32), SaveSCCReg) 6288 .addImm(1) 6289 .addImm(0); 6290 } 6291 6292 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC); 6293 6294 // Save the EXEC mask 6295 BuildMI(MBB, Begin, DL, TII.get(MovExecOpc), SaveExec).addReg(Exec); 6296 6297 // Killed uses in the instruction we are waterfalling around will be 6298 // incorrect due to the added control-flow. 6299 MachineBasicBlock::iterator AfterMI = MI; 6300 ++AfterMI; 6301 for (auto I = Begin; I != AfterMI; I++) { 6302 for (auto &MO : I->all_uses()) 6303 MRI.clearKillFlags(MO.getReg()); 6304 } 6305 6306 // To insert the loop we need to split the block. Move everything after this 6307 // point to a new block, and insert a new empty block between the two. 6308 MachineBasicBlock *LoopBB = MF.CreateMachineBasicBlock(); 6309 MachineBasicBlock *BodyBB = MF.CreateMachineBasicBlock(); 6310 MachineBasicBlock *RemainderBB = MF.CreateMachineBasicBlock(); 6311 MachineFunction::iterator MBBI(MBB); 6312 ++MBBI; 6313 6314 MF.insert(MBBI, LoopBB); 6315 MF.insert(MBBI, BodyBB); 6316 MF.insert(MBBI, RemainderBB); 6317 6318 LoopBB->addSuccessor(BodyBB); 6319 BodyBB->addSuccessor(LoopBB); 6320 BodyBB->addSuccessor(RemainderBB); 6321 6322 // Move Begin to MI to the BodyBB, and the remainder of the block to 6323 // RemainderBB. 6324 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); 6325 RemainderBB->splice(RemainderBB->begin(), &MBB, End, MBB.end()); 6326 BodyBB->splice(BodyBB->begin(), &MBB, Begin, MBB.end()); 6327 6328 MBB.addSuccessor(LoopBB); 6329 6330 // Update dominators. We know that MBB immediately dominates LoopBB, that 6331 // LoopBB immediately dominates BodyBB, and BodyBB immediately dominates 6332 // RemainderBB. RemainderBB immediately dominates all of the successors 6333 // transferred to it from MBB that MBB used to properly dominate. 6334 if (MDT) { 6335 MDT->addNewBlock(LoopBB, &MBB); 6336 MDT->addNewBlock(BodyBB, LoopBB); 6337 MDT->addNewBlock(RemainderBB, BodyBB); 6338 for (auto &Succ : RemainderBB->successors()) { 6339 if (MDT->properlyDominates(&MBB, Succ)) { 6340 MDT->changeImmediateDominator(Succ, RemainderBB); 6341 } 6342 } 6343 } 6344 6345 emitLoadScalarOpsFromVGPRLoop(TII, MRI, MBB, *LoopBB, *BodyBB, DL, ScalarOps); 6346 6347 MachineBasicBlock::iterator First = RemainderBB->begin(); 6348 // Restore SCC 6349 if (SCCNotDead) { 6350 BuildMI(*RemainderBB, First, DL, TII.get(AMDGPU::S_CMP_LG_U32)) 6351 .addReg(SaveSCCReg, RegState::Kill) 6352 .addImm(0); 6353 } 6354 6355 // Restore the EXEC mask 6356 BuildMI(*RemainderBB, First, DL, TII.get(MovExecOpc), Exec).addReg(SaveExec); 6357 return BodyBB; 6358 } 6359 6360 // Extract pointer from Rsrc and return a zero-value Rsrc replacement. 6361 static std::tuple<unsigned, unsigned> 6362 extractRsrcPtr(const SIInstrInfo &TII, MachineInstr &MI, MachineOperand &Rsrc) { 6363 MachineBasicBlock &MBB = *MI.getParent(); 6364 MachineFunction &MF = *MBB.getParent(); 6365 MachineRegisterInfo &MRI = MF.getRegInfo(); 6366 6367 // Extract the ptr from the resource descriptor. 6368 unsigned RsrcPtr = 6369 TII.buildExtractSubReg(MI, MRI, Rsrc, &AMDGPU::VReg_128RegClass, 6370 AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass); 6371 6372 // Create an empty resource descriptor 6373 Register Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 6374 Register SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 6375 Register SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 6376 Register NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SGPR_128RegClass); 6377 uint64_t RsrcDataFormat = TII.getDefaultRsrcDataFormat(); 6378 6379 // Zero64 = 0 6380 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B64), Zero64) 6381 .addImm(0); 6382 6383 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0} 6384 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatLo) 6385 .addImm(RsrcDataFormat & 0xFFFFFFFF); 6386 6387 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32} 6388 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatHi) 6389 .addImm(RsrcDataFormat >> 32); 6390 6391 // NewSRsrc = {Zero64, SRsrcFormat} 6392 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::REG_SEQUENCE), NewSRsrc) 6393 .addReg(Zero64) 6394 .addImm(AMDGPU::sub0_sub1) 6395 .addReg(SRsrcFormatLo) 6396 .addImm(AMDGPU::sub2) 6397 .addReg(SRsrcFormatHi) 6398 .addImm(AMDGPU::sub3); 6399 6400 return std::tuple(RsrcPtr, NewSRsrc); 6401 } 6402 6403 MachineBasicBlock * 6404 SIInstrInfo::legalizeOperands(MachineInstr &MI, 6405 MachineDominatorTree *MDT) const { 6406 MachineFunction &MF = *MI.getParent()->getParent(); 6407 MachineRegisterInfo &MRI = MF.getRegInfo(); 6408 MachineBasicBlock *CreatedBB = nullptr; 6409 6410 // Legalize VOP2 6411 if (isVOP2(MI) || isVOPC(MI)) { 6412 legalizeOperandsVOP2(MRI, MI); 6413 return CreatedBB; 6414 } 6415 6416 // Legalize VOP3 6417 if (isVOP3(MI)) { 6418 legalizeOperandsVOP3(MRI, MI); 6419 return CreatedBB; 6420 } 6421 6422 // Legalize SMRD 6423 if (isSMRD(MI)) { 6424 legalizeOperandsSMRD(MRI, MI); 6425 return CreatedBB; 6426 } 6427 6428 // Legalize FLAT 6429 if (isFLAT(MI)) { 6430 legalizeOperandsFLAT(MRI, MI); 6431 return CreatedBB; 6432 } 6433 6434 // Legalize REG_SEQUENCE and PHI 6435 // The register class of the operands much be the same type as the register 6436 // class of the output. 6437 if (MI.getOpcode() == AMDGPU::PHI) { 6438 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr; 6439 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { 6440 if (!MI.getOperand(i).isReg() || !MI.getOperand(i).getReg().isVirtual()) 6441 continue; 6442 const TargetRegisterClass *OpRC = 6443 MRI.getRegClass(MI.getOperand(i).getReg()); 6444 if (RI.hasVectorRegisters(OpRC)) { 6445 VRC = OpRC; 6446 } else { 6447 SRC = OpRC; 6448 } 6449 } 6450 6451 // If any of the operands are VGPR registers, then they all most be 6452 // otherwise we will create illegal VGPR->SGPR copies when legalizing 6453 // them. 6454 if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) { 6455 if (!VRC) { 6456 assert(SRC); 6457 if (getOpRegClass(MI, 0) == &AMDGPU::VReg_1RegClass) { 6458 VRC = &AMDGPU::VReg_1RegClass; 6459 } else 6460 VRC = RI.isAGPRClass(getOpRegClass(MI, 0)) 6461 ? RI.getEquivalentAGPRClass(SRC) 6462 : RI.getEquivalentVGPRClass(SRC); 6463 } else { 6464 VRC = RI.isAGPRClass(getOpRegClass(MI, 0)) 6465 ? RI.getEquivalentAGPRClass(VRC) 6466 : RI.getEquivalentVGPRClass(VRC); 6467 } 6468 RC = VRC; 6469 } else { 6470 RC = SRC; 6471 } 6472 6473 // Update all the operands so they have the same type. 6474 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 6475 MachineOperand &Op = MI.getOperand(I); 6476 if (!Op.isReg() || !Op.getReg().isVirtual()) 6477 continue; 6478 6479 // MI is a PHI instruction. 6480 MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB(); 6481 MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator(); 6482 6483 // Avoid creating no-op copies with the same src and dst reg class. These 6484 // confuse some of the machine passes. 6485 legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc()); 6486 } 6487 } 6488 6489 // REG_SEQUENCE doesn't really require operand legalization, but if one has a 6490 // VGPR dest type and SGPR sources, insert copies so all operands are 6491 // VGPRs. This seems to help operand folding / the register coalescer. 6492 if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) { 6493 MachineBasicBlock *MBB = MI.getParent(); 6494 const TargetRegisterClass *DstRC = getOpRegClass(MI, 0); 6495 if (RI.hasVGPRs(DstRC)) { 6496 // Update all the operands so they are VGPR register classes. These may 6497 // not be the same register class because REG_SEQUENCE supports mixing 6498 // subregister index types e.g. sub0_sub1 + sub2 + sub3 6499 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 6500 MachineOperand &Op = MI.getOperand(I); 6501 if (!Op.isReg() || !Op.getReg().isVirtual()) 6502 continue; 6503 6504 const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg()); 6505 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC); 6506 if (VRC == OpRC) 6507 continue; 6508 6509 legalizeGenericOperand(*MBB, MI, VRC, Op, MRI, MI.getDebugLoc()); 6510 Op.setIsKill(); 6511 } 6512 } 6513 6514 return CreatedBB; 6515 } 6516 6517 // Legalize INSERT_SUBREG 6518 // src0 must have the same register class as dst 6519 if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) { 6520 Register Dst = MI.getOperand(0).getReg(); 6521 Register Src0 = MI.getOperand(1).getReg(); 6522 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst); 6523 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0); 6524 if (DstRC != Src0RC) { 6525 MachineBasicBlock *MBB = MI.getParent(); 6526 MachineOperand &Op = MI.getOperand(1); 6527 legalizeGenericOperand(*MBB, MI, DstRC, Op, MRI, MI.getDebugLoc()); 6528 } 6529 return CreatedBB; 6530 } 6531 6532 // Legalize SI_INIT_M0 6533 if (MI.getOpcode() == AMDGPU::SI_INIT_M0) { 6534 MachineOperand &Src = MI.getOperand(0); 6535 if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg()))) 6536 Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI)); 6537 return CreatedBB; 6538 } 6539 6540 // Legalize S_BITREPLICATE, S_QUADMASK and S_WQM 6541 if (MI.getOpcode() == AMDGPU::S_BITREPLICATE_B64_B32 || 6542 MI.getOpcode() == AMDGPU::S_QUADMASK_B32 || 6543 MI.getOpcode() == AMDGPU::S_QUADMASK_B64 || 6544 MI.getOpcode() == AMDGPU::S_WQM_B32 || 6545 MI.getOpcode() == AMDGPU::S_WQM_B64) { 6546 MachineOperand &Src = MI.getOperand(1); 6547 if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg()))) 6548 Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI)); 6549 return CreatedBB; 6550 } 6551 6552 // Legalize MIMG/VIMAGE/VSAMPLE and MUBUF/MTBUF for shaders. 6553 // 6554 // Shaders only generate MUBUF/MTBUF instructions via intrinsics or via 6555 // scratch memory access. In both cases, the legalization never involves 6556 // conversion to the addr64 form. 6557 if (isImage(MI) || (AMDGPU::isGraphics(MF.getFunction().getCallingConv()) && 6558 (isMUBUF(MI) || isMTBUF(MI)))) { 6559 int RSrcOpName = (isVIMAGE(MI) || isVSAMPLE(MI)) ? AMDGPU::OpName::rsrc 6560 : AMDGPU::OpName::srsrc; 6561 MachineOperand *SRsrc = getNamedOperand(MI, RSrcOpName); 6562 if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg()))) 6563 CreatedBB = loadMBUFScalarOperandsFromVGPR(*this, MI, {SRsrc}, MDT); 6564 6565 int SampOpName = isMIMG(MI) ? AMDGPU::OpName::ssamp : AMDGPU::OpName::samp; 6566 MachineOperand *SSamp = getNamedOperand(MI, SampOpName); 6567 if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg()))) 6568 CreatedBB = loadMBUFScalarOperandsFromVGPR(*this, MI, {SSamp}, MDT); 6569 6570 return CreatedBB; 6571 } 6572 6573 // Legalize SI_CALL 6574 if (MI.getOpcode() == AMDGPU::SI_CALL_ISEL) { 6575 MachineOperand *Dest = &MI.getOperand(0); 6576 if (!RI.isSGPRClass(MRI.getRegClass(Dest->getReg()))) { 6577 // Move everything between ADJCALLSTACKUP and ADJCALLSTACKDOWN and 6578 // following copies, we also need to move copies from and to physical 6579 // registers into the loop block. 6580 unsigned FrameSetupOpcode = getCallFrameSetupOpcode(); 6581 unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode(); 6582 6583 // Also move the copies to physical registers into the loop block 6584 MachineBasicBlock &MBB = *MI.getParent(); 6585 MachineBasicBlock::iterator Start(&MI); 6586 while (Start->getOpcode() != FrameSetupOpcode) 6587 --Start; 6588 MachineBasicBlock::iterator End(&MI); 6589 while (End->getOpcode() != FrameDestroyOpcode) 6590 ++End; 6591 // Also include following copies of the return value 6592 ++End; 6593 while (End != MBB.end() && End->isCopy() && End->getOperand(1).isReg() && 6594 MI.definesRegister(End->getOperand(1).getReg())) 6595 ++End; 6596 CreatedBB = 6597 loadMBUFScalarOperandsFromVGPR(*this, MI, {Dest}, MDT, Start, End); 6598 } 6599 } 6600 6601 // Legalize s_sleep_var. 6602 if (MI.getOpcode() == AMDGPU::S_SLEEP_VAR) { 6603 const DebugLoc &DL = MI.getDebugLoc(); 6604 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 6605 int Src0Idx = 6606 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::src0); 6607 MachineOperand &Src0 = MI.getOperand(Src0Idx); 6608 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 6609 .add(Src0); 6610 Src0.ChangeToRegister(Reg, false); 6611 return nullptr; 6612 } 6613 6614 // Legalize MUBUF instructions. 6615 bool isSoffsetLegal = true; 6616 int SoffsetIdx = 6617 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::soffset); 6618 if (SoffsetIdx != -1) { 6619 MachineOperand *Soffset = &MI.getOperand(SoffsetIdx); 6620 if (Soffset->isReg() && Soffset->getReg().isVirtual() && 6621 !RI.isSGPRClass(MRI.getRegClass(Soffset->getReg()))) { 6622 isSoffsetLegal = false; 6623 } 6624 } 6625 6626 bool isRsrcLegal = true; 6627 int RsrcIdx = 6628 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc); 6629 if (RsrcIdx != -1) { 6630 MachineOperand *Rsrc = &MI.getOperand(RsrcIdx); 6631 if (Rsrc->isReg() && !RI.isSGPRClass(MRI.getRegClass(Rsrc->getReg()))) { 6632 isRsrcLegal = false; 6633 } 6634 } 6635 6636 // The operands are legal. 6637 if (isRsrcLegal && isSoffsetLegal) 6638 return CreatedBB; 6639 6640 if (!isRsrcLegal) { 6641 // Legalize a VGPR Rsrc 6642 // 6643 // If the instruction is _ADDR64, we can avoid a waterfall by extracting 6644 // the base pointer from the VGPR Rsrc, adding it to the VAddr, then using 6645 // a zero-value SRsrc. 6646 // 6647 // If the instruction is _OFFSET (both idxen and offen disabled), and we 6648 // support ADDR64 instructions, we can convert to ADDR64 and do the same as 6649 // above. 6650 // 6651 // Otherwise we are on non-ADDR64 hardware, and/or we have 6652 // idxen/offen/bothen and we fall back to a waterfall loop. 6653 6654 MachineOperand *Rsrc = &MI.getOperand(RsrcIdx); 6655 MachineBasicBlock &MBB = *MI.getParent(); 6656 6657 MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 6658 if (VAddr && AMDGPU::getIfAddr64Inst(MI.getOpcode()) != -1) { 6659 // This is already an ADDR64 instruction so we need to add the pointer 6660 // extracted from the resource descriptor to the current value of VAddr. 6661 Register NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6662 Register NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6663 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 6664 6665 const auto *BoolXExecRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 6666 Register CondReg0 = MRI.createVirtualRegister(BoolXExecRC); 6667 Register CondReg1 = MRI.createVirtualRegister(BoolXExecRC); 6668 6669 unsigned RsrcPtr, NewSRsrc; 6670 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc); 6671 6672 // NewVaddrLo = RsrcPtr:sub0 + VAddr:sub0 6673 const DebugLoc &DL = MI.getDebugLoc(); 6674 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_CO_U32_e64), NewVAddrLo) 6675 .addDef(CondReg0) 6676 .addReg(RsrcPtr, 0, AMDGPU::sub0) 6677 .addReg(VAddr->getReg(), 0, AMDGPU::sub0) 6678 .addImm(0); 6679 6680 // NewVaddrHi = RsrcPtr:sub1 + VAddr:sub1 6681 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e64), NewVAddrHi) 6682 .addDef(CondReg1, RegState::Dead) 6683 .addReg(RsrcPtr, 0, AMDGPU::sub1) 6684 .addReg(VAddr->getReg(), 0, AMDGPU::sub1) 6685 .addReg(CondReg0, RegState::Kill) 6686 .addImm(0); 6687 6688 // NewVaddr = {NewVaddrHi, NewVaddrLo} 6689 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr) 6690 .addReg(NewVAddrLo) 6691 .addImm(AMDGPU::sub0) 6692 .addReg(NewVAddrHi) 6693 .addImm(AMDGPU::sub1); 6694 6695 VAddr->setReg(NewVAddr); 6696 Rsrc->setReg(NewSRsrc); 6697 } else if (!VAddr && ST.hasAddr64()) { 6698 // This instructions is the _OFFSET variant, so we need to convert it to 6699 // ADDR64. 6700 assert(ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS && 6701 "FIXME: Need to emit flat atomics here"); 6702 6703 unsigned RsrcPtr, NewSRsrc; 6704 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc); 6705 6706 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 6707 MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata); 6708 MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 6709 MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset); 6710 unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode()); 6711 6712 // Atomics with return have an additional tied operand and are 6713 // missing some of the special bits. 6714 MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in); 6715 MachineInstr *Addr64; 6716 6717 if (!VDataIn) { 6718 // Regular buffer load / store. 6719 MachineInstrBuilder MIB = 6720 BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 6721 .add(*VData) 6722 .addReg(NewVAddr) 6723 .addReg(NewSRsrc) 6724 .add(*SOffset) 6725 .add(*Offset); 6726 6727 if (const MachineOperand *CPol = 6728 getNamedOperand(MI, AMDGPU::OpName::cpol)) { 6729 MIB.addImm(CPol->getImm()); 6730 } 6731 6732 if (const MachineOperand *TFE = 6733 getNamedOperand(MI, AMDGPU::OpName::tfe)) { 6734 MIB.addImm(TFE->getImm()); 6735 } 6736 6737 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::swz)); 6738 6739 MIB.cloneMemRefs(MI); 6740 Addr64 = MIB; 6741 } else { 6742 // Atomics with return. 6743 Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 6744 .add(*VData) 6745 .add(*VDataIn) 6746 .addReg(NewVAddr) 6747 .addReg(NewSRsrc) 6748 .add(*SOffset) 6749 .add(*Offset) 6750 .addImm(getNamedImmOperand(MI, AMDGPU::OpName::cpol)) 6751 .cloneMemRefs(MI); 6752 } 6753 6754 MI.removeFromParent(); 6755 6756 // NewVaddr = {NewVaddrHi, NewVaddrLo} 6757 BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), 6758 NewVAddr) 6759 .addReg(RsrcPtr, 0, AMDGPU::sub0) 6760 .addImm(AMDGPU::sub0) 6761 .addReg(RsrcPtr, 0, AMDGPU::sub1) 6762 .addImm(AMDGPU::sub1); 6763 } else { 6764 // Legalize a VGPR Rsrc and soffset together. 6765 if (!isSoffsetLegal) { 6766 MachineOperand *Soffset = getNamedOperand(MI, AMDGPU::OpName::soffset); 6767 CreatedBB = 6768 loadMBUFScalarOperandsFromVGPR(*this, MI, {Rsrc, Soffset}, MDT); 6769 return CreatedBB; 6770 } 6771 CreatedBB = loadMBUFScalarOperandsFromVGPR(*this, MI, {Rsrc}, MDT); 6772 return CreatedBB; 6773 } 6774 } 6775 6776 // Legalize a VGPR soffset. 6777 if (!isSoffsetLegal) { 6778 MachineOperand *Soffset = getNamedOperand(MI, AMDGPU::OpName::soffset); 6779 CreatedBB = loadMBUFScalarOperandsFromVGPR(*this, MI, {Soffset}, MDT); 6780 return CreatedBB; 6781 } 6782 return CreatedBB; 6783 } 6784 6785 void SIInstrWorklist::insert(MachineInstr *MI) { 6786 InstrList.insert(MI); 6787 // Add MBUF instructiosn to deferred list. 6788 int RsrcIdx = 6789 AMDGPU::getNamedOperandIdx(MI->getOpcode(), AMDGPU::OpName::srsrc); 6790 if (RsrcIdx != -1) { 6791 DeferredList.insert(MI); 6792 } 6793 } 6794 6795 bool SIInstrWorklist::isDeferred(MachineInstr *MI) { 6796 return DeferredList.contains(MI); 6797 } 6798 6799 void SIInstrInfo::moveToVALU(SIInstrWorklist &Worklist, 6800 MachineDominatorTree *MDT) const { 6801 6802 while (!Worklist.empty()) { 6803 MachineInstr &Inst = *Worklist.top(); 6804 Worklist.erase_top(); 6805 // Skip MachineInstr in the deferred list. 6806 if (Worklist.isDeferred(&Inst)) 6807 continue; 6808 moveToVALUImpl(Worklist, MDT, Inst); 6809 } 6810 6811 // Deferred list of instructions will be processed once 6812 // all the MachineInstr in the worklist are done. 6813 for (MachineInstr *Inst : Worklist.getDeferredList()) { 6814 moveToVALUImpl(Worklist, MDT, *Inst); 6815 assert(Worklist.empty() && 6816 "Deferred MachineInstr are not supposed to re-populate worklist"); 6817 } 6818 } 6819 6820 void SIInstrInfo::moveToVALUImpl(SIInstrWorklist &Worklist, 6821 MachineDominatorTree *MDT, 6822 MachineInstr &Inst) const { 6823 6824 MachineBasicBlock *MBB = Inst.getParent(); 6825 if (!MBB) 6826 return; 6827 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 6828 unsigned Opcode = Inst.getOpcode(); 6829 unsigned NewOpcode = getVALUOp(Inst); 6830 // Handle some special cases 6831 switch (Opcode) { 6832 default: 6833 break; 6834 case AMDGPU::S_ADD_U64_PSEUDO: 6835 NewOpcode = AMDGPU::V_ADD_U64_PSEUDO; 6836 break; 6837 case AMDGPU::S_SUB_U64_PSEUDO: 6838 NewOpcode = AMDGPU::V_SUB_U64_PSEUDO; 6839 break; 6840 case AMDGPU::S_ADD_I32: 6841 case AMDGPU::S_SUB_I32: { 6842 // FIXME: The u32 versions currently selected use the carry. 6843 bool Changed; 6844 MachineBasicBlock *CreatedBBTmp = nullptr; 6845 std::tie(Changed, CreatedBBTmp) = moveScalarAddSub(Worklist, Inst, MDT); 6846 if (Changed) 6847 return; 6848 6849 // Default handling 6850 break; 6851 } 6852 case AMDGPU::S_AND_B64: 6853 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32, MDT); 6854 Inst.eraseFromParent(); 6855 return; 6856 6857 case AMDGPU::S_OR_B64: 6858 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32, MDT); 6859 Inst.eraseFromParent(); 6860 return; 6861 6862 case AMDGPU::S_XOR_B64: 6863 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32, MDT); 6864 Inst.eraseFromParent(); 6865 return; 6866 6867 case AMDGPU::S_NAND_B64: 6868 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NAND_B32, MDT); 6869 Inst.eraseFromParent(); 6870 return; 6871 6872 case AMDGPU::S_NOR_B64: 6873 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NOR_B32, MDT); 6874 Inst.eraseFromParent(); 6875 return; 6876 6877 case AMDGPU::S_XNOR_B64: 6878 if (ST.hasDLInsts()) 6879 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XNOR_B32, MDT); 6880 else 6881 splitScalar64BitXnor(Worklist, Inst, MDT); 6882 Inst.eraseFromParent(); 6883 return; 6884 6885 case AMDGPU::S_ANDN2_B64: 6886 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ANDN2_B32, MDT); 6887 Inst.eraseFromParent(); 6888 return; 6889 6890 case AMDGPU::S_ORN2_B64: 6891 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ORN2_B32, MDT); 6892 Inst.eraseFromParent(); 6893 return; 6894 6895 case AMDGPU::S_BREV_B64: 6896 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_BREV_B32, true); 6897 Inst.eraseFromParent(); 6898 return; 6899 6900 case AMDGPU::S_NOT_B64: 6901 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32); 6902 Inst.eraseFromParent(); 6903 return; 6904 6905 case AMDGPU::S_BCNT1_I32_B64: 6906 splitScalar64BitBCNT(Worklist, Inst); 6907 Inst.eraseFromParent(); 6908 return; 6909 6910 case AMDGPU::S_BFE_I64: 6911 splitScalar64BitBFE(Worklist, Inst); 6912 Inst.eraseFromParent(); 6913 return; 6914 6915 case AMDGPU::S_FLBIT_I32_B64: 6916 splitScalar64BitCountOp(Worklist, Inst, AMDGPU::V_FFBH_U32_e32); 6917 Inst.eraseFromParent(); 6918 return; 6919 case AMDGPU::S_FF1_I32_B64: 6920 splitScalar64BitCountOp(Worklist, Inst, AMDGPU::V_FFBL_B32_e32); 6921 Inst.eraseFromParent(); 6922 return; 6923 6924 case AMDGPU::S_LSHL_B32: 6925 if (ST.hasOnlyRevVALUShifts()) { 6926 NewOpcode = AMDGPU::V_LSHLREV_B32_e64; 6927 swapOperands(Inst); 6928 } 6929 break; 6930 case AMDGPU::S_ASHR_I32: 6931 if (ST.hasOnlyRevVALUShifts()) { 6932 NewOpcode = AMDGPU::V_ASHRREV_I32_e64; 6933 swapOperands(Inst); 6934 } 6935 break; 6936 case AMDGPU::S_LSHR_B32: 6937 if (ST.hasOnlyRevVALUShifts()) { 6938 NewOpcode = AMDGPU::V_LSHRREV_B32_e64; 6939 swapOperands(Inst); 6940 } 6941 break; 6942 case AMDGPU::S_LSHL_B64: 6943 if (ST.hasOnlyRevVALUShifts()) { 6944 NewOpcode = ST.getGeneration() >= AMDGPUSubtarget::GFX12 6945 ? AMDGPU::V_LSHLREV_B64_pseudo_e64 6946 : AMDGPU::V_LSHLREV_B64_e64; 6947 swapOperands(Inst); 6948 } 6949 break; 6950 case AMDGPU::S_ASHR_I64: 6951 if (ST.hasOnlyRevVALUShifts()) { 6952 NewOpcode = AMDGPU::V_ASHRREV_I64_e64; 6953 swapOperands(Inst); 6954 } 6955 break; 6956 case AMDGPU::S_LSHR_B64: 6957 if (ST.hasOnlyRevVALUShifts()) { 6958 NewOpcode = AMDGPU::V_LSHRREV_B64_e64; 6959 swapOperands(Inst); 6960 } 6961 break; 6962 6963 case AMDGPU::S_ABS_I32: 6964 lowerScalarAbs(Worklist, Inst); 6965 Inst.eraseFromParent(); 6966 return; 6967 6968 case AMDGPU::S_CBRANCH_SCC0: 6969 case AMDGPU::S_CBRANCH_SCC1: { 6970 // Clear unused bits of vcc 6971 Register CondReg = Inst.getOperand(1).getReg(); 6972 bool IsSCC = CondReg == AMDGPU::SCC; 6973 Register VCC = RI.getVCC(); 6974 Register EXEC = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 6975 unsigned Opc = ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64; 6976 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(Opc), VCC) 6977 .addReg(EXEC) 6978 .addReg(IsSCC ? VCC : CondReg); 6979 Inst.removeOperand(1); 6980 } break; 6981 6982 case AMDGPU::S_BFE_U64: 6983 case AMDGPU::S_BFM_B64: 6984 llvm_unreachable("Moving this op to VALU not implemented"); 6985 6986 case AMDGPU::S_PACK_LL_B32_B16: 6987 case AMDGPU::S_PACK_LH_B32_B16: 6988 case AMDGPU::S_PACK_HL_B32_B16: 6989 case AMDGPU::S_PACK_HH_B32_B16: 6990 movePackToVALU(Worklist, MRI, Inst); 6991 Inst.eraseFromParent(); 6992 return; 6993 6994 case AMDGPU::S_XNOR_B32: 6995 lowerScalarXnor(Worklist, Inst); 6996 Inst.eraseFromParent(); 6997 return; 6998 6999 case AMDGPU::S_NAND_B32: 7000 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_AND_B32); 7001 Inst.eraseFromParent(); 7002 return; 7003 7004 case AMDGPU::S_NOR_B32: 7005 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_OR_B32); 7006 Inst.eraseFromParent(); 7007 return; 7008 7009 case AMDGPU::S_ANDN2_B32: 7010 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_AND_B32); 7011 Inst.eraseFromParent(); 7012 return; 7013 7014 case AMDGPU::S_ORN2_B32: 7015 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_OR_B32); 7016 Inst.eraseFromParent(); 7017 return; 7018 7019 // TODO: remove as soon as everything is ready 7020 // to replace VGPR to SGPR copy with V_READFIRSTLANEs. 7021 // S_ADD/SUB_CO_PSEUDO as well as S_UADDO/USUBO_PSEUDO 7022 // can only be selected from the uniform SDNode. 7023 case AMDGPU::S_ADD_CO_PSEUDO: 7024 case AMDGPU::S_SUB_CO_PSEUDO: { 7025 unsigned Opc = (Inst.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO) 7026 ? AMDGPU::V_ADDC_U32_e64 7027 : AMDGPU::V_SUBB_U32_e64; 7028 const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 7029 7030 Register CarryInReg = Inst.getOperand(4).getReg(); 7031 if (!MRI.constrainRegClass(CarryInReg, CarryRC)) { 7032 Register NewCarryReg = MRI.createVirtualRegister(CarryRC); 7033 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(AMDGPU::COPY), NewCarryReg) 7034 .addReg(CarryInReg); 7035 } 7036 7037 Register CarryOutReg = Inst.getOperand(1).getReg(); 7038 7039 Register DestReg = MRI.createVirtualRegister(RI.getEquivalentVGPRClass( 7040 MRI.getRegClass(Inst.getOperand(0).getReg()))); 7041 MachineInstr *CarryOp = 7042 BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(Opc), DestReg) 7043 .addReg(CarryOutReg, RegState::Define) 7044 .add(Inst.getOperand(2)) 7045 .add(Inst.getOperand(3)) 7046 .addReg(CarryInReg) 7047 .addImm(0); 7048 legalizeOperands(*CarryOp); 7049 MRI.replaceRegWith(Inst.getOperand(0).getReg(), DestReg); 7050 addUsersToMoveToVALUWorklist(DestReg, MRI, Worklist); 7051 Inst.eraseFromParent(); 7052 } 7053 return; 7054 case AMDGPU::S_UADDO_PSEUDO: 7055 case AMDGPU::S_USUBO_PSEUDO: { 7056 const DebugLoc &DL = Inst.getDebugLoc(); 7057 MachineOperand &Dest0 = Inst.getOperand(0); 7058 MachineOperand &Dest1 = Inst.getOperand(1); 7059 MachineOperand &Src0 = Inst.getOperand(2); 7060 MachineOperand &Src1 = Inst.getOperand(3); 7061 7062 unsigned Opc = (Inst.getOpcode() == AMDGPU::S_UADDO_PSEUDO) 7063 ? AMDGPU::V_ADD_CO_U32_e64 7064 : AMDGPU::V_SUB_CO_U32_e64; 7065 const TargetRegisterClass *NewRC = 7066 RI.getEquivalentVGPRClass(MRI.getRegClass(Dest0.getReg())); 7067 Register DestReg = MRI.createVirtualRegister(NewRC); 7068 MachineInstr *NewInstr = BuildMI(*MBB, &Inst, DL, get(Opc), DestReg) 7069 .addReg(Dest1.getReg(), RegState::Define) 7070 .add(Src0) 7071 .add(Src1) 7072 .addImm(0); // clamp bit 7073 7074 legalizeOperands(*NewInstr, MDT); 7075 MRI.replaceRegWith(Dest0.getReg(), DestReg); 7076 addUsersToMoveToVALUWorklist(NewInstr->getOperand(0).getReg(), MRI, 7077 Worklist); 7078 Inst.eraseFromParent(); 7079 } 7080 return; 7081 7082 case AMDGPU::S_CSELECT_B32: 7083 case AMDGPU::S_CSELECT_B64: 7084 lowerSelect(Worklist, Inst, MDT); 7085 Inst.eraseFromParent(); 7086 return; 7087 case AMDGPU::S_CMP_EQ_I32: 7088 case AMDGPU::S_CMP_LG_I32: 7089 case AMDGPU::S_CMP_GT_I32: 7090 case AMDGPU::S_CMP_GE_I32: 7091 case AMDGPU::S_CMP_LT_I32: 7092 case AMDGPU::S_CMP_LE_I32: 7093 case AMDGPU::S_CMP_EQ_U32: 7094 case AMDGPU::S_CMP_LG_U32: 7095 case AMDGPU::S_CMP_GT_U32: 7096 case AMDGPU::S_CMP_GE_U32: 7097 case AMDGPU::S_CMP_LT_U32: 7098 case AMDGPU::S_CMP_LE_U32: 7099 case AMDGPU::S_CMP_EQ_U64: 7100 case AMDGPU::S_CMP_LG_U64: 7101 case AMDGPU::S_CMP_LT_F32: 7102 case AMDGPU::S_CMP_EQ_F32: 7103 case AMDGPU::S_CMP_LE_F32: 7104 case AMDGPU::S_CMP_GT_F32: 7105 case AMDGPU::S_CMP_LG_F32: 7106 case AMDGPU::S_CMP_GE_F32: 7107 case AMDGPU::S_CMP_O_F32: 7108 case AMDGPU::S_CMP_U_F32: 7109 case AMDGPU::S_CMP_NGE_F32: 7110 case AMDGPU::S_CMP_NLG_F32: 7111 case AMDGPU::S_CMP_NGT_F32: 7112 case AMDGPU::S_CMP_NLE_F32: 7113 case AMDGPU::S_CMP_NEQ_F32: 7114 case AMDGPU::S_CMP_NLT_F32: 7115 case AMDGPU::S_CMP_LT_F16: 7116 case AMDGPU::S_CMP_EQ_F16: 7117 case AMDGPU::S_CMP_LE_F16: 7118 case AMDGPU::S_CMP_GT_F16: 7119 case AMDGPU::S_CMP_LG_F16: 7120 case AMDGPU::S_CMP_GE_F16: 7121 case AMDGPU::S_CMP_O_F16: 7122 case AMDGPU::S_CMP_U_F16: 7123 case AMDGPU::S_CMP_NGE_F16: 7124 case AMDGPU::S_CMP_NLG_F16: 7125 case AMDGPU::S_CMP_NGT_F16: 7126 case AMDGPU::S_CMP_NLE_F16: 7127 case AMDGPU::S_CMP_NEQ_F16: 7128 case AMDGPU::S_CMP_NLT_F16: { 7129 Register CondReg = MRI.createVirtualRegister(RI.getWaveMaskRegClass()); 7130 auto NewInstr = 7131 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(NewOpcode), CondReg) 7132 .setMIFlags(Inst.getFlags()); 7133 if (AMDGPU::getNamedOperandIdx(NewOpcode, 7134 AMDGPU::OpName::src0_modifiers) >= 0) { 7135 NewInstr 7136 .addImm(0) // src0_modifiers 7137 .add(Inst.getOperand(0)) // src0 7138 .addImm(0) // src1_modifiers 7139 .add(Inst.getOperand(1)) // src1 7140 .addImm(0); // clamp 7141 } else { 7142 NewInstr 7143 .add(Inst.getOperand(0)) 7144 .add(Inst.getOperand(1)); 7145 } 7146 legalizeOperands(*NewInstr, MDT); 7147 int SCCIdx = Inst.findRegisterDefOperandIdx(AMDGPU::SCC); 7148 MachineOperand SCCOp = Inst.getOperand(SCCIdx); 7149 addSCCDefUsersToVALUWorklist(SCCOp, Inst, Worklist, CondReg); 7150 Inst.eraseFromParent(); 7151 return; 7152 } 7153 case AMDGPU::S_CVT_HI_F32_F16: { 7154 const DebugLoc &DL = Inst.getDebugLoc(); 7155 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7156 Register NewDst = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7157 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg) 7158 .addImm(16) 7159 .add(Inst.getOperand(1)); 7160 BuildMI(*MBB, Inst, DL, get(NewOpcode), NewDst) 7161 .addImm(0) // src0_modifiers 7162 .addReg(TmpReg) 7163 .addImm(0) // clamp 7164 .addImm(0); // omod 7165 7166 MRI.replaceRegWith(Inst.getOperand(0).getReg(), NewDst); 7167 addUsersToMoveToVALUWorklist(NewDst, MRI, Worklist); 7168 Inst.eraseFromParent(); 7169 return; 7170 } 7171 case AMDGPU::S_MINIMUM_F32: 7172 case AMDGPU::S_MAXIMUM_F32: 7173 case AMDGPU::S_MINIMUM_F16: 7174 case AMDGPU::S_MAXIMUM_F16: { 7175 const DebugLoc &DL = Inst.getDebugLoc(); 7176 Register NewDst = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7177 MachineInstr *NewInstr = BuildMI(*MBB, Inst, DL, get(NewOpcode), NewDst) 7178 .addImm(0) // src0_modifiers 7179 .add(Inst.getOperand(1)) 7180 .addImm(0) // src1_modifiers 7181 .add(Inst.getOperand(2)) 7182 .addImm(0) // clamp 7183 .addImm(0); // omod 7184 MRI.replaceRegWith(Inst.getOperand(0).getReg(), NewDst); 7185 7186 legalizeOperands(*NewInstr, MDT); 7187 addUsersToMoveToVALUWorklist(NewDst, MRI, Worklist); 7188 Inst.eraseFromParent(); 7189 return; 7190 } 7191 } 7192 7193 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) { 7194 // We cannot move this instruction to the VALU, so we should try to 7195 // legalize its operands instead. 7196 legalizeOperands(Inst, MDT); 7197 return; 7198 } 7199 // Handle converting generic instructions like COPY-to-SGPR into 7200 // COPY-to-VGPR. 7201 if (NewOpcode == Opcode) { 7202 Register DstReg = Inst.getOperand(0).getReg(); 7203 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst); 7204 7205 if (Inst.isCopy() && Inst.getOperand(1).getReg().isVirtual() && 7206 NewDstRC == RI.getRegClassForReg(MRI, Inst.getOperand(1).getReg())) { 7207 // Instead of creating a copy where src and dst are the same register 7208 // class, we just replace all uses of dst with src. These kinds of 7209 // copies interfere with the heuristics MachineSink uses to decide 7210 // whether or not to split a critical edge. Since the pass assumes 7211 // that copies will end up as machine instructions and not be 7212 // eliminated. 7213 addUsersToMoveToVALUWorklist(DstReg, MRI, Worklist); 7214 MRI.replaceRegWith(DstReg, Inst.getOperand(1).getReg()); 7215 MRI.clearKillFlags(Inst.getOperand(1).getReg()); 7216 Inst.getOperand(0).setReg(DstReg); 7217 // Make sure we don't leave around a dead VGPR->SGPR copy. Normally 7218 // these are deleted later, but at -O0 it would leave a suspicious 7219 // looking illegal copy of an undef register. 7220 for (unsigned I = Inst.getNumOperands() - 1; I != 0; --I) 7221 Inst.removeOperand(I); 7222 Inst.setDesc(get(AMDGPU::IMPLICIT_DEF)); 7223 return; 7224 } 7225 Register NewDstReg = MRI.createVirtualRegister(NewDstRC); 7226 MRI.replaceRegWith(DstReg, NewDstReg); 7227 legalizeOperands(Inst, MDT); 7228 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist); 7229 return; 7230 } 7231 7232 // Use the new VALU Opcode. 7233 auto NewInstr = BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(NewOpcode)) 7234 .setMIFlags(Inst.getFlags()); 7235 if (isVOP3(NewOpcode) && !isVOP3(Opcode)) { 7236 // Intersperse VOP3 modifiers among the SALU operands. 7237 NewInstr->addOperand(Inst.getOperand(0)); 7238 if (AMDGPU::getNamedOperandIdx(NewOpcode, 7239 AMDGPU::OpName::src0_modifiers) >= 0) 7240 NewInstr.addImm(0); 7241 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::src0) >= 0) 7242 NewInstr->addOperand(Inst.getOperand(1)); 7243 7244 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) { 7245 // We are converting these to a BFE, so we need to add the missing 7246 // operands for the size and offset. 7247 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16; 7248 NewInstr.addImm(0); 7249 NewInstr.addImm(Size); 7250 } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) { 7251 // The VALU version adds the second operand to the result, so insert an 7252 // extra 0 operand. 7253 NewInstr.addImm(0); 7254 } else if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) { 7255 const MachineOperand &OffsetWidthOp = Inst.getOperand(2); 7256 // If we need to move this to VGPRs, we need to unpack the second 7257 // operand back into the 2 separate ones for bit offset and width. 7258 assert(OffsetWidthOp.isImm() && 7259 "Scalar BFE is only implemented for constant width and offset"); 7260 uint32_t Imm = OffsetWidthOp.getImm(); 7261 7262 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 7263 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 7264 NewInstr.addImm(Offset); 7265 NewInstr.addImm(BitWidth); 7266 } else { 7267 if (AMDGPU::getNamedOperandIdx(NewOpcode, 7268 AMDGPU::OpName::src1_modifiers) >= 0) 7269 NewInstr.addImm(0); 7270 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::src1) >= 0) 7271 NewInstr->addOperand(Inst.getOperand(2)); 7272 if (AMDGPU::getNamedOperandIdx(NewOpcode, 7273 AMDGPU::OpName::src2_modifiers) >= 0) 7274 NewInstr.addImm(0); 7275 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::src2) >= 0) 7276 NewInstr->addOperand(Inst.getOperand(3)); 7277 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::clamp) >= 0) 7278 NewInstr.addImm(0); 7279 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::omod) >= 0) 7280 NewInstr.addImm(0); 7281 if (AMDGPU::getNamedOperandIdx(NewOpcode, AMDGPU::OpName::op_sel) >= 0) 7282 NewInstr.addImm(0); 7283 } 7284 } else { 7285 // Just copy the SALU operands. 7286 for (const MachineOperand &Op : Inst.explicit_operands()) 7287 NewInstr->addOperand(Op); 7288 } 7289 7290 // Remove any references to SCC. Vector instructions can't read from it, and 7291 // We're just about to add the implicit use / defs of VCC, and we don't want 7292 // both. 7293 for (MachineOperand &Op : Inst.implicit_operands()) { 7294 if (Op.getReg() == AMDGPU::SCC) { 7295 // Only propagate through live-def of SCC. 7296 if (Op.isDef() && !Op.isDead()) 7297 addSCCDefUsersToVALUWorklist(Op, Inst, Worklist); 7298 if (Op.isUse()) 7299 addSCCDefsToVALUWorklist(NewInstr, Worklist); 7300 } 7301 } 7302 Inst.eraseFromParent(); 7303 Register NewDstReg; 7304 if (NewInstr->getOperand(0).isReg() && NewInstr->getOperand(0).isDef()) { 7305 Register DstReg = NewInstr->getOperand(0).getReg(); 7306 assert(DstReg.isVirtual()); 7307 // Update the destination register class. 7308 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(*NewInstr); 7309 assert(NewDstRC); 7310 NewDstReg = MRI.createVirtualRegister(NewDstRC); 7311 MRI.replaceRegWith(DstReg, NewDstReg); 7312 } 7313 fixImplicitOperands(*NewInstr); 7314 // Legalize the operands 7315 legalizeOperands(*NewInstr, MDT); 7316 if (NewDstReg) 7317 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist); 7318 } 7319 7320 // Add/sub require special handling to deal with carry outs. 7321 std::pair<bool, MachineBasicBlock *> 7322 SIInstrInfo::moveScalarAddSub(SIInstrWorklist &Worklist, MachineInstr &Inst, 7323 MachineDominatorTree *MDT) const { 7324 if (ST.hasAddNoCarry()) { 7325 // Assume there is no user of scc since we don't select this in that case. 7326 // Since scc isn't used, it doesn't really matter if the i32 or u32 variant 7327 // is used. 7328 7329 MachineBasicBlock &MBB = *Inst.getParent(); 7330 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7331 7332 Register OldDstReg = Inst.getOperand(0).getReg(); 7333 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7334 7335 unsigned Opc = Inst.getOpcode(); 7336 assert(Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32); 7337 7338 unsigned NewOpc = Opc == AMDGPU::S_ADD_I32 ? 7339 AMDGPU::V_ADD_U32_e64 : AMDGPU::V_SUB_U32_e64; 7340 7341 assert(Inst.getOperand(3).getReg() == AMDGPU::SCC); 7342 Inst.removeOperand(3); 7343 7344 Inst.setDesc(get(NewOpc)); 7345 Inst.addOperand(MachineOperand::CreateImm(0)); // clamp bit 7346 Inst.addImplicitDefUseOperands(*MBB.getParent()); 7347 MRI.replaceRegWith(OldDstReg, ResultReg); 7348 MachineBasicBlock *NewBB = legalizeOperands(Inst, MDT); 7349 7350 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 7351 return std::pair(true, NewBB); 7352 } 7353 7354 return std::pair(false, nullptr); 7355 } 7356 7357 void SIInstrInfo::lowerSelect(SIInstrWorklist &Worklist, MachineInstr &Inst, 7358 MachineDominatorTree *MDT) const { 7359 7360 MachineBasicBlock &MBB = *Inst.getParent(); 7361 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7362 MachineBasicBlock::iterator MII = Inst; 7363 DebugLoc DL = Inst.getDebugLoc(); 7364 7365 MachineOperand &Dest = Inst.getOperand(0); 7366 MachineOperand &Src0 = Inst.getOperand(1); 7367 MachineOperand &Src1 = Inst.getOperand(2); 7368 MachineOperand &Cond = Inst.getOperand(3); 7369 7370 Register CondReg = Cond.getReg(); 7371 bool IsSCC = (CondReg == AMDGPU::SCC); 7372 7373 // If this is a trivial select where the condition is effectively not SCC 7374 // (CondReg is a source of copy to SCC), then the select is semantically 7375 // equivalent to copying CondReg. Hence, there is no need to create 7376 // V_CNDMASK, we can just use that and bail out. 7377 if (!IsSCC && Src0.isImm() && (Src0.getImm() == -1) && Src1.isImm() && 7378 (Src1.getImm() == 0)) { 7379 MRI.replaceRegWith(Dest.getReg(), CondReg); 7380 return; 7381 } 7382 7383 Register NewCondReg = CondReg; 7384 if (IsSCC) { 7385 const TargetRegisterClass *TC = 7386 RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 7387 NewCondReg = MRI.createVirtualRegister(TC); 7388 7389 // Now look for the closest SCC def if it is a copy 7390 // replacing the CondReg with the COPY source register 7391 bool CopyFound = false; 7392 for (MachineInstr &CandI : 7393 make_range(std::next(MachineBasicBlock::reverse_iterator(Inst)), 7394 Inst.getParent()->rend())) { 7395 if (CandI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != 7396 -1) { 7397 if (CandI.isCopy() && CandI.getOperand(0).getReg() == AMDGPU::SCC) { 7398 BuildMI(MBB, MII, DL, get(AMDGPU::COPY), NewCondReg) 7399 .addReg(CandI.getOperand(1).getReg()); 7400 CopyFound = true; 7401 } 7402 break; 7403 } 7404 } 7405 if (!CopyFound) { 7406 // SCC def is not a copy 7407 // Insert a trivial select instead of creating a copy, because a copy from 7408 // SCC would semantically mean just copying a single bit, but we may need 7409 // the result to be a vector condition mask that needs preserving. 7410 unsigned Opcode = (ST.getWavefrontSize() == 64) ? AMDGPU::S_CSELECT_B64 7411 : AMDGPU::S_CSELECT_B32; 7412 auto NewSelect = 7413 BuildMI(MBB, MII, DL, get(Opcode), NewCondReg).addImm(-1).addImm(0); 7414 NewSelect->getOperand(3).setIsUndef(Cond.isUndef()); 7415 } 7416 } 7417 7418 Register NewDestReg = MRI.createVirtualRegister( 7419 RI.getEquivalentVGPRClass(MRI.getRegClass(Dest.getReg()))); 7420 MachineInstr *NewInst; 7421 if (Inst.getOpcode() == AMDGPU::S_CSELECT_B32) { 7422 NewInst = BuildMI(MBB, MII, DL, get(AMDGPU::V_CNDMASK_B32_e64), NewDestReg) 7423 .addImm(0) 7424 .add(Src1) // False 7425 .addImm(0) 7426 .add(Src0) // True 7427 .addReg(NewCondReg); 7428 } else { 7429 NewInst = 7430 BuildMI(MBB, MII, DL, get(AMDGPU::V_CNDMASK_B64_PSEUDO), NewDestReg) 7431 .add(Src1) // False 7432 .add(Src0) // True 7433 .addReg(NewCondReg); 7434 } 7435 MRI.replaceRegWith(Dest.getReg(), NewDestReg); 7436 legalizeOperands(*NewInst, MDT); 7437 addUsersToMoveToVALUWorklist(NewDestReg, MRI, Worklist); 7438 } 7439 7440 void SIInstrInfo::lowerScalarAbs(SIInstrWorklist &Worklist, 7441 MachineInstr &Inst) const { 7442 MachineBasicBlock &MBB = *Inst.getParent(); 7443 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7444 MachineBasicBlock::iterator MII = Inst; 7445 DebugLoc DL = Inst.getDebugLoc(); 7446 7447 MachineOperand &Dest = Inst.getOperand(0); 7448 MachineOperand &Src = Inst.getOperand(1); 7449 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7450 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7451 7452 unsigned SubOp = ST.hasAddNoCarry() ? 7453 AMDGPU::V_SUB_U32_e32 : AMDGPU::V_SUB_CO_U32_e32; 7454 7455 BuildMI(MBB, MII, DL, get(SubOp), TmpReg) 7456 .addImm(0) 7457 .addReg(Src.getReg()); 7458 7459 BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg) 7460 .addReg(Src.getReg()) 7461 .addReg(TmpReg); 7462 7463 MRI.replaceRegWith(Dest.getReg(), ResultReg); 7464 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 7465 } 7466 7467 void SIInstrInfo::lowerScalarXnor(SIInstrWorklist &Worklist, 7468 MachineInstr &Inst) const { 7469 MachineBasicBlock &MBB = *Inst.getParent(); 7470 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7471 MachineBasicBlock::iterator MII = Inst; 7472 const DebugLoc &DL = Inst.getDebugLoc(); 7473 7474 MachineOperand &Dest = Inst.getOperand(0); 7475 MachineOperand &Src0 = Inst.getOperand(1); 7476 MachineOperand &Src1 = Inst.getOperand(2); 7477 7478 if (ST.hasDLInsts()) { 7479 Register NewDest = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7480 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src0, MRI, DL); 7481 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src1, MRI, DL); 7482 7483 BuildMI(MBB, MII, DL, get(AMDGPU::V_XNOR_B32_e64), NewDest) 7484 .add(Src0) 7485 .add(Src1); 7486 7487 MRI.replaceRegWith(Dest.getReg(), NewDest); 7488 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 7489 } else { 7490 // Using the identity !(x ^ y) == (!x ^ y) == (x ^ !y), we can 7491 // invert either source and then perform the XOR. If either source is a 7492 // scalar register, then we can leave the inversion on the scalar unit to 7493 // achieve a better distribution of scalar and vector instructions. 7494 bool Src0IsSGPR = Src0.isReg() && 7495 RI.isSGPRClass(MRI.getRegClass(Src0.getReg())); 7496 bool Src1IsSGPR = Src1.isReg() && 7497 RI.isSGPRClass(MRI.getRegClass(Src1.getReg())); 7498 MachineInstr *Xor; 7499 Register Temp = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 7500 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 7501 7502 // Build a pair of scalar instructions and add them to the work list. 7503 // The next iteration over the work list will lower these to the vector 7504 // unit as necessary. 7505 if (Src0IsSGPR) { 7506 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src0); 7507 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest) 7508 .addReg(Temp) 7509 .add(Src1); 7510 } else if (Src1IsSGPR) { 7511 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src1); 7512 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest) 7513 .add(Src0) 7514 .addReg(Temp); 7515 } else { 7516 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), Temp) 7517 .add(Src0) 7518 .add(Src1); 7519 MachineInstr *Not = 7520 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest).addReg(Temp); 7521 Worklist.insert(Not); 7522 } 7523 7524 MRI.replaceRegWith(Dest.getReg(), NewDest); 7525 7526 Worklist.insert(Xor); 7527 7528 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 7529 } 7530 } 7531 7532 void SIInstrInfo::splitScalarNotBinop(SIInstrWorklist &Worklist, 7533 MachineInstr &Inst, 7534 unsigned Opcode) const { 7535 MachineBasicBlock &MBB = *Inst.getParent(); 7536 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7537 MachineBasicBlock::iterator MII = Inst; 7538 const DebugLoc &DL = Inst.getDebugLoc(); 7539 7540 MachineOperand &Dest = Inst.getOperand(0); 7541 MachineOperand &Src0 = Inst.getOperand(1); 7542 MachineOperand &Src1 = Inst.getOperand(2); 7543 7544 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 7545 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 7546 7547 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), Interm) 7548 .add(Src0) 7549 .add(Src1); 7550 7551 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest) 7552 .addReg(Interm); 7553 7554 Worklist.insert(&Op); 7555 Worklist.insert(&Not); 7556 7557 MRI.replaceRegWith(Dest.getReg(), NewDest); 7558 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 7559 } 7560 7561 void SIInstrInfo::splitScalarBinOpN2(SIInstrWorklist &Worklist, 7562 MachineInstr &Inst, 7563 unsigned Opcode) const { 7564 MachineBasicBlock &MBB = *Inst.getParent(); 7565 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7566 MachineBasicBlock::iterator MII = Inst; 7567 const DebugLoc &DL = Inst.getDebugLoc(); 7568 7569 MachineOperand &Dest = Inst.getOperand(0); 7570 MachineOperand &Src0 = Inst.getOperand(1); 7571 MachineOperand &Src1 = Inst.getOperand(2); 7572 7573 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 7574 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 7575 7576 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Interm) 7577 .add(Src1); 7578 7579 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), NewDest) 7580 .add(Src0) 7581 .addReg(Interm); 7582 7583 Worklist.insert(&Not); 7584 Worklist.insert(&Op); 7585 7586 MRI.replaceRegWith(Dest.getReg(), NewDest); 7587 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 7588 } 7589 7590 void SIInstrInfo::splitScalar64BitUnaryOp(SIInstrWorklist &Worklist, 7591 MachineInstr &Inst, unsigned Opcode, 7592 bool Swap) const { 7593 MachineBasicBlock &MBB = *Inst.getParent(); 7594 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7595 7596 MachineOperand &Dest = Inst.getOperand(0); 7597 MachineOperand &Src0 = Inst.getOperand(1); 7598 DebugLoc DL = Inst.getDebugLoc(); 7599 7600 MachineBasicBlock::iterator MII = Inst; 7601 7602 const MCInstrDesc &InstDesc = get(Opcode); 7603 const TargetRegisterClass *Src0RC = Src0.isReg() ? 7604 MRI.getRegClass(Src0.getReg()) : 7605 &AMDGPU::SGPR_32RegClass; 7606 7607 const TargetRegisterClass *Src0SubRC = 7608 RI.getSubRegisterClass(Src0RC, AMDGPU::sub0); 7609 7610 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 7611 AMDGPU::sub0, Src0SubRC); 7612 7613 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 7614 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 7615 const TargetRegisterClass *NewDestSubRC = 7616 RI.getSubRegisterClass(NewDestRC, AMDGPU::sub0); 7617 7618 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 7619 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0).add(SrcReg0Sub0); 7620 7621 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 7622 AMDGPU::sub1, Src0SubRC); 7623 7624 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 7625 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1).add(SrcReg0Sub1); 7626 7627 if (Swap) 7628 std::swap(DestSub0, DestSub1); 7629 7630 Register FullDestReg = MRI.createVirtualRegister(NewDestRC); 7631 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 7632 .addReg(DestSub0) 7633 .addImm(AMDGPU::sub0) 7634 .addReg(DestSub1) 7635 .addImm(AMDGPU::sub1); 7636 7637 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 7638 7639 Worklist.insert(&LoHalf); 7640 Worklist.insert(&HiHalf); 7641 7642 // We don't need to legalizeOperands here because for a single operand, src0 7643 // will support any kind of input. 7644 7645 // Move all users of this moved value. 7646 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 7647 } 7648 7649 void SIInstrInfo::splitScalar64BitBinaryOp(SIInstrWorklist &Worklist, 7650 MachineInstr &Inst, unsigned Opcode, 7651 MachineDominatorTree *MDT) const { 7652 MachineBasicBlock &MBB = *Inst.getParent(); 7653 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7654 7655 MachineOperand &Dest = Inst.getOperand(0); 7656 MachineOperand &Src0 = Inst.getOperand(1); 7657 MachineOperand &Src1 = Inst.getOperand(2); 7658 DebugLoc DL = Inst.getDebugLoc(); 7659 7660 MachineBasicBlock::iterator MII = Inst; 7661 7662 const MCInstrDesc &InstDesc = get(Opcode); 7663 const TargetRegisterClass *Src0RC = Src0.isReg() ? 7664 MRI.getRegClass(Src0.getReg()) : 7665 &AMDGPU::SGPR_32RegClass; 7666 7667 const TargetRegisterClass *Src0SubRC = 7668 RI.getSubRegisterClass(Src0RC, AMDGPU::sub0); 7669 const TargetRegisterClass *Src1RC = Src1.isReg() ? 7670 MRI.getRegClass(Src1.getReg()) : 7671 &AMDGPU::SGPR_32RegClass; 7672 7673 const TargetRegisterClass *Src1SubRC = 7674 RI.getSubRegisterClass(Src1RC, AMDGPU::sub0); 7675 7676 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 7677 AMDGPU::sub0, Src0SubRC); 7678 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 7679 AMDGPU::sub0, Src1SubRC); 7680 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 7681 AMDGPU::sub1, Src0SubRC); 7682 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 7683 AMDGPU::sub1, Src1SubRC); 7684 7685 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 7686 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 7687 const TargetRegisterClass *NewDestSubRC = 7688 RI.getSubRegisterClass(NewDestRC, AMDGPU::sub0); 7689 7690 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 7691 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0) 7692 .add(SrcReg0Sub0) 7693 .add(SrcReg1Sub0); 7694 7695 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 7696 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1) 7697 .add(SrcReg0Sub1) 7698 .add(SrcReg1Sub1); 7699 7700 Register FullDestReg = MRI.createVirtualRegister(NewDestRC); 7701 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 7702 .addReg(DestSub0) 7703 .addImm(AMDGPU::sub0) 7704 .addReg(DestSub1) 7705 .addImm(AMDGPU::sub1); 7706 7707 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 7708 7709 Worklist.insert(&LoHalf); 7710 Worklist.insert(&HiHalf); 7711 7712 // Move all users of this moved value. 7713 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 7714 } 7715 7716 void SIInstrInfo::splitScalar64BitXnor(SIInstrWorklist &Worklist, 7717 MachineInstr &Inst, 7718 MachineDominatorTree *MDT) const { 7719 MachineBasicBlock &MBB = *Inst.getParent(); 7720 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7721 7722 MachineOperand &Dest = Inst.getOperand(0); 7723 MachineOperand &Src0 = Inst.getOperand(1); 7724 MachineOperand &Src1 = Inst.getOperand(2); 7725 const DebugLoc &DL = Inst.getDebugLoc(); 7726 7727 MachineBasicBlock::iterator MII = Inst; 7728 7729 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 7730 7731 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 7732 7733 MachineOperand* Op0; 7734 MachineOperand* Op1; 7735 7736 if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) { 7737 Op0 = &Src0; 7738 Op1 = &Src1; 7739 } else { 7740 Op0 = &Src1; 7741 Op1 = &Src0; 7742 } 7743 7744 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B64), Interm) 7745 .add(*Op0); 7746 7747 Register NewDest = MRI.createVirtualRegister(DestRC); 7748 7749 MachineInstr &Xor = *BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B64), NewDest) 7750 .addReg(Interm) 7751 .add(*Op1); 7752 7753 MRI.replaceRegWith(Dest.getReg(), NewDest); 7754 7755 Worklist.insert(&Xor); 7756 } 7757 7758 void SIInstrInfo::splitScalar64BitBCNT(SIInstrWorklist &Worklist, 7759 MachineInstr &Inst) const { 7760 MachineBasicBlock &MBB = *Inst.getParent(); 7761 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7762 7763 MachineBasicBlock::iterator MII = Inst; 7764 const DebugLoc &DL = Inst.getDebugLoc(); 7765 7766 MachineOperand &Dest = Inst.getOperand(0); 7767 MachineOperand &Src = Inst.getOperand(1); 7768 7769 const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64); 7770 const TargetRegisterClass *SrcRC = Src.isReg() ? 7771 MRI.getRegClass(Src.getReg()) : 7772 &AMDGPU::SGPR_32RegClass; 7773 7774 Register MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7775 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7776 7777 const TargetRegisterClass *SrcSubRC = 7778 RI.getSubRegisterClass(SrcRC, AMDGPU::sub0); 7779 7780 MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 7781 AMDGPU::sub0, SrcSubRC); 7782 MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 7783 AMDGPU::sub1, SrcSubRC); 7784 7785 BuildMI(MBB, MII, DL, InstDesc, MidReg).add(SrcRegSub0).addImm(0); 7786 7787 BuildMI(MBB, MII, DL, InstDesc, ResultReg).add(SrcRegSub1).addReg(MidReg); 7788 7789 MRI.replaceRegWith(Dest.getReg(), ResultReg); 7790 7791 // We don't need to legalize operands here. src0 for either instruction can be 7792 // an SGPR, and the second input is unused or determined here. 7793 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 7794 } 7795 7796 void SIInstrInfo::splitScalar64BitBFE(SIInstrWorklist &Worklist, 7797 MachineInstr &Inst) const { 7798 MachineBasicBlock &MBB = *Inst.getParent(); 7799 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7800 MachineBasicBlock::iterator MII = Inst; 7801 const DebugLoc &DL = Inst.getDebugLoc(); 7802 7803 MachineOperand &Dest = Inst.getOperand(0); 7804 uint32_t Imm = Inst.getOperand(2).getImm(); 7805 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 7806 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 7807 7808 (void) Offset; 7809 7810 // Only sext_inreg cases handled. 7811 assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 && 7812 Offset == 0 && "Not implemented"); 7813 7814 if (BitWidth < 32) { 7815 Register MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7816 Register MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7817 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 7818 7819 BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32_e64), MidRegLo) 7820 .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0) 7821 .addImm(0) 7822 .addImm(BitWidth); 7823 7824 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi) 7825 .addImm(31) 7826 .addReg(MidRegLo); 7827 7828 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 7829 .addReg(MidRegLo) 7830 .addImm(AMDGPU::sub0) 7831 .addReg(MidRegHi) 7832 .addImm(AMDGPU::sub1); 7833 7834 MRI.replaceRegWith(Dest.getReg(), ResultReg); 7835 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 7836 return; 7837 } 7838 7839 MachineOperand &Src = Inst.getOperand(1); 7840 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7841 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 7842 7843 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg) 7844 .addImm(31) 7845 .addReg(Src.getReg(), 0, AMDGPU::sub0); 7846 7847 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 7848 .addReg(Src.getReg(), 0, AMDGPU::sub0) 7849 .addImm(AMDGPU::sub0) 7850 .addReg(TmpReg) 7851 .addImm(AMDGPU::sub1); 7852 7853 MRI.replaceRegWith(Dest.getReg(), ResultReg); 7854 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 7855 } 7856 7857 void SIInstrInfo::splitScalar64BitCountOp(SIInstrWorklist &Worklist, 7858 MachineInstr &Inst, unsigned Opcode, 7859 MachineDominatorTree *MDT) const { 7860 // (S_FLBIT_I32_B64 hi:lo) -> 7861 // -> (umin (V_FFBH_U32_e32 hi), (uaddsat (V_FFBH_U32_e32 lo), 32)) 7862 // (S_FF1_I32_B64 hi:lo) -> 7863 // ->(umin (uaddsat (V_FFBL_B32_e32 hi), 32) (V_FFBL_B32_e32 lo)) 7864 7865 MachineBasicBlock &MBB = *Inst.getParent(); 7866 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7867 MachineBasicBlock::iterator MII = Inst; 7868 const DebugLoc &DL = Inst.getDebugLoc(); 7869 7870 MachineOperand &Dest = Inst.getOperand(0); 7871 MachineOperand &Src = Inst.getOperand(1); 7872 7873 const MCInstrDesc &InstDesc = get(Opcode); 7874 7875 bool IsCtlz = Opcode == AMDGPU::V_FFBH_U32_e32; 7876 unsigned OpcodeAdd = 7877 ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_CO_U32_e32; 7878 7879 const TargetRegisterClass *SrcRC = 7880 Src.isReg() ? MRI.getRegClass(Src.getReg()) : &AMDGPU::SGPR_32RegClass; 7881 const TargetRegisterClass *SrcSubRC = 7882 RI.getSubRegisterClass(SrcRC, AMDGPU::sub0); 7883 7884 MachineOperand SrcRegSub0 = 7885 buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, AMDGPU::sub0, SrcSubRC); 7886 MachineOperand SrcRegSub1 = 7887 buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, AMDGPU::sub1, SrcSubRC); 7888 7889 Register MidReg1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7890 Register MidReg2 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7891 Register MidReg3 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7892 Register MidReg4 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7893 7894 BuildMI(MBB, MII, DL, InstDesc, MidReg1).add(SrcRegSub0); 7895 7896 BuildMI(MBB, MII, DL, InstDesc, MidReg2).add(SrcRegSub1); 7897 7898 BuildMI(MBB, MII, DL, get(OpcodeAdd), MidReg3) 7899 .addReg(IsCtlz ? MidReg1 : MidReg2) 7900 .addImm(32) 7901 .addImm(1); // enable clamp 7902 7903 BuildMI(MBB, MII, DL, get(AMDGPU::V_MIN_U32_e64), MidReg4) 7904 .addReg(MidReg3) 7905 .addReg(IsCtlz ? MidReg2 : MidReg1); 7906 7907 MRI.replaceRegWith(Dest.getReg(), MidReg4); 7908 7909 addUsersToMoveToVALUWorklist(MidReg4, MRI, Worklist); 7910 } 7911 7912 void SIInstrInfo::addUsersToMoveToVALUWorklist( 7913 Register DstReg, MachineRegisterInfo &MRI, 7914 SIInstrWorklist &Worklist) const { 7915 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg), 7916 E = MRI.use_end(); I != E;) { 7917 MachineInstr &UseMI = *I->getParent(); 7918 7919 unsigned OpNo = 0; 7920 7921 switch (UseMI.getOpcode()) { 7922 case AMDGPU::COPY: 7923 case AMDGPU::WQM: 7924 case AMDGPU::SOFT_WQM: 7925 case AMDGPU::STRICT_WWM: 7926 case AMDGPU::STRICT_WQM: 7927 case AMDGPU::REG_SEQUENCE: 7928 case AMDGPU::PHI: 7929 case AMDGPU::INSERT_SUBREG: 7930 break; 7931 default: 7932 OpNo = I.getOperandNo(); 7933 break; 7934 } 7935 7936 if (!RI.hasVectorRegisters(getOpRegClass(UseMI, OpNo))) { 7937 Worklist.insert(&UseMI); 7938 7939 do { 7940 ++I; 7941 } while (I != E && I->getParent() == &UseMI); 7942 } else { 7943 ++I; 7944 } 7945 } 7946 } 7947 7948 void SIInstrInfo::movePackToVALU(SIInstrWorklist &Worklist, 7949 MachineRegisterInfo &MRI, 7950 MachineInstr &Inst) const { 7951 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7952 MachineBasicBlock *MBB = Inst.getParent(); 7953 MachineOperand &Src0 = Inst.getOperand(1); 7954 MachineOperand &Src1 = Inst.getOperand(2); 7955 const DebugLoc &DL = Inst.getDebugLoc(); 7956 7957 switch (Inst.getOpcode()) { 7958 case AMDGPU::S_PACK_LL_B32_B16: { 7959 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7960 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7961 7962 // FIXME: Can do a lot better if we know the high bits of src0 or src1 are 7963 // 0. 7964 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 7965 .addImm(0xffff); 7966 7967 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_B32_e64), TmpReg) 7968 .addReg(ImmReg, RegState::Kill) 7969 .add(Src0); 7970 7971 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32_e64), ResultReg) 7972 .add(Src1) 7973 .addImm(16) 7974 .addReg(TmpReg, RegState::Kill); 7975 break; 7976 } 7977 case AMDGPU::S_PACK_LH_B32_B16: { 7978 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7979 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 7980 .addImm(0xffff); 7981 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_BFI_B32_e64), ResultReg) 7982 .addReg(ImmReg, RegState::Kill) 7983 .add(Src0) 7984 .add(Src1); 7985 break; 7986 } 7987 case AMDGPU::S_PACK_HL_B32_B16: { 7988 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7989 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg) 7990 .addImm(16) 7991 .add(Src0); 7992 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32_e64), ResultReg) 7993 .add(Src1) 7994 .addImm(16) 7995 .addReg(TmpReg, RegState::Kill); 7996 break; 7997 } 7998 case AMDGPU::S_PACK_HH_B32_B16: { 7999 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 8000 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 8001 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg) 8002 .addImm(16) 8003 .add(Src0); 8004 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 8005 .addImm(0xffff0000); 8006 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_OR_B32_e64), ResultReg) 8007 .add(Src1) 8008 .addReg(ImmReg, RegState::Kill) 8009 .addReg(TmpReg, RegState::Kill); 8010 break; 8011 } 8012 default: 8013 llvm_unreachable("unhandled s_pack_* instruction"); 8014 } 8015 8016 MachineOperand &Dest = Inst.getOperand(0); 8017 MRI.replaceRegWith(Dest.getReg(), ResultReg); 8018 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 8019 } 8020 8021 void SIInstrInfo::addSCCDefUsersToVALUWorklist(MachineOperand &Op, 8022 MachineInstr &SCCDefInst, 8023 SIInstrWorklist &Worklist, 8024 Register NewCond) const { 8025 8026 // Ensure that def inst defines SCC, which is still live. 8027 assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isDef() && 8028 !Op.isDead() && Op.getParent() == &SCCDefInst); 8029 SmallVector<MachineInstr *, 4> CopyToDelete; 8030 // This assumes that all the users of SCC are in the same block 8031 // as the SCC def. 8032 for (MachineInstr &MI : // Skip the def inst itself. 8033 make_range(std::next(MachineBasicBlock::iterator(SCCDefInst)), 8034 SCCDefInst.getParent()->end())) { 8035 // Check if SCC is used first. 8036 int SCCIdx = MI.findRegisterUseOperandIdx(AMDGPU::SCC, false, &RI); 8037 if (SCCIdx != -1) { 8038 if (MI.isCopy()) { 8039 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 8040 Register DestReg = MI.getOperand(0).getReg(); 8041 8042 MRI.replaceRegWith(DestReg, NewCond); 8043 CopyToDelete.push_back(&MI); 8044 } else { 8045 8046 if (NewCond.isValid()) 8047 MI.getOperand(SCCIdx).setReg(NewCond); 8048 8049 Worklist.insert(&MI); 8050 } 8051 } 8052 // Exit if we find another SCC def. 8053 if (MI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != -1) 8054 break; 8055 } 8056 for (auto &Copy : CopyToDelete) 8057 Copy->eraseFromParent(); 8058 } 8059 8060 // Instructions that use SCC may be converted to VALU instructions. When that 8061 // happens, the SCC register is changed to VCC_LO. The instruction that defines 8062 // SCC must be changed to an instruction that defines VCC. This function makes 8063 // sure that the instruction that defines SCC is added to the moveToVALU 8064 // worklist. 8065 void SIInstrInfo::addSCCDefsToVALUWorklist(MachineInstr *SCCUseInst, 8066 SIInstrWorklist &Worklist) const { 8067 // Look for a preceding instruction that either defines VCC or SCC. If VCC 8068 // then there is nothing to do because the defining instruction has been 8069 // converted to a VALU already. If SCC then that instruction needs to be 8070 // converted to a VALU. 8071 for (MachineInstr &MI : 8072 make_range(std::next(MachineBasicBlock::reverse_iterator(SCCUseInst)), 8073 SCCUseInst->getParent()->rend())) { 8074 if (MI.modifiesRegister(AMDGPU::VCC, &RI)) 8075 break; 8076 if (MI.definesRegister(AMDGPU::SCC, &RI)) { 8077 Worklist.insert(&MI); 8078 break; 8079 } 8080 } 8081 } 8082 8083 const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass( 8084 const MachineInstr &Inst) const { 8085 const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0); 8086 8087 switch (Inst.getOpcode()) { 8088 // For target instructions, getOpRegClass just returns the virtual register 8089 // class associated with the operand, so we need to find an equivalent VGPR 8090 // register class in order to move the instruction to the VALU. 8091 case AMDGPU::COPY: 8092 case AMDGPU::PHI: 8093 case AMDGPU::REG_SEQUENCE: 8094 case AMDGPU::INSERT_SUBREG: 8095 case AMDGPU::WQM: 8096 case AMDGPU::SOFT_WQM: 8097 case AMDGPU::STRICT_WWM: 8098 case AMDGPU::STRICT_WQM: { 8099 const TargetRegisterClass *SrcRC = getOpRegClass(Inst, 1); 8100 if (RI.isAGPRClass(SrcRC)) { 8101 if (RI.isAGPRClass(NewDstRC)) 8102 return nullptr; 8103 8104 switch (Inst.getOpcode()) { 8105 case AMDGPU::PHI: 8106 case AMDGPU::REG_SEQUENCE: 8107 case AMDGPU::INSERT_SUBREG: 8108 NewDstRC = RI.getEquivalentAGPRClass(NewDstRC); 8109 break; 8110 default: 8111 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 8112 } 8113 8114 if (!NewDstRC) 8115 return nullptr; 8116 } else { 8117 if (RI.isVGPRClass(NewDstRC) || NewDstRC == &AMDGPU::VReg_1RegClass) 8118 return nullptr; 8119 8120 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 8121 if (!NewDstRC) 8122 return nullptr; 8123 } 8124 8125 return NewDstRC; 8126 } 8127 default: 8128 return NewDstRC; 8129 } 8130 } 8131 8132 // Find the one SGPR operand we are allowed to use. 8133 Register SIInstrInfo::findUsedSGPR(const MachineInstr &MI, 8134 int OpIndices[3]) const { 8135 const MCInstrDesc &Desc = MI.getDesc(); 8136 8137 // Find the one SGPR operand we are allowed to use. 8138 // 8139 // First we need to consider the instruction's operand requirements before 8140 // legalizing. Some operands are required to be SGPRs, such as implicit uses 8141 // of VCC, but we are still bound by the constant bus requirement to only use 8142 // one. 8143 // 8144 // If the operand's class is an SGPR, we can never move it. 8145 8146 Register SGPRReg = findImplicitSGPRRead(MI); 8147 if (SGPRReg) 8148 return SGPRReg; 8149 8150 Register UsedSGPRs[3] = {Register()}; 8151 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 8152 8153 for (unsigned i = 0; i < 3; ++i) { 8154 int Idx = OpIndices[i]; 8155 if (Idx == -1) 8156 break; 8157 8158 const MachineOperand &MO = MI.getOperand(Idx); 8159 if (!MO.isReg()) 8160 continue; 8161 8162 // Is this operand statically required to be an SGPR based on the operand 8163 // constraints? 8164 const TargetRegisterClass *OpRC = 8165 RI.getRegClass(Desc.operands()[Idx].RegClass); 8166 bool IsRequiredSGPR = RI.isSGPRClass(OpRC); 8167 if (IsRequiredSGPR) 8168 return MO.getReg(); 8169 8170 // If this could be a VGPR or an SGPR, Check the dynamic register class. 8171 Register Reg = MO.getReg(); 8172 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg); 8173 if (RI.isSGPRClass(RegRC)) 8174 UsedSGPRs[i] = Reg; 8175 } 8176 8177 // We don't have a required SGPR operand, so we have a bit more freedom in 8178 // selecting operands to move. 8179 8180 // Try to select the most used SGPR. If an SGPR is equal to one of the 8181 // others, we choose that. 8182 // 8183 // e.g. 8184 // V_FMA_F32 v0, s0, s0, s0 -> No moves 8185 // V_FMA_F32 v0, s0, s1, s0 -> Move s1 8186 8187 // TODO: If some of the operands are 64-bit SGPRs and some 32, we should 8188 // prefer those. 8189 8190 if (UsedSGPRs[0]) { 8191 if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2]) 8192 SGPRReg = UsedSGPRs[0]; 8193 } 8194 8195 if (!SGPRReg && UsedSGPRs[1]) { 8196 if (UsedSGPRs[1] == UsedSGPRs[2]) 8197 SGPRReg = UsedSGPRs[1]; 8198 } 8199 8200 return SGPRReg; 8201 } 8202 8203 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI, 8204 unsigned OperandName) const { 8205 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName); 8206 if (Idx == -1) 8207 return nullptr; 8208 8209 return &MI.getOperand(Idx); 8210 } 8211 8212 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const { 8213 if (ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 8214 int64_t Format = ST.getGeneration() >= AMDGPUSubtarget::GFX11 8215 ? (int64_t)AMDGPU::UfmtGFX11::UFMT_32_FLOAT 8216 : (int64_t)AMDGPU::UfmtGFX10::UFMT_32_FLOAT; 8217 return (Format << 44) | 8218 (1ULL << 56) | // RESOURCE_LEVEL = 1 8219 (3ULL << 60); // OOB_SELECT = 3 8220 } 8221 8222 uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT; 8223 if (ST.isAmdHsaOS()) { 8224 // Set ATC = 1. GFX9 doesn't have this bit. 8225 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) 8226 RsrcDataFormat |= (1ULL << 56); 8227 8228 // Set MTYPE = 2 (MTYPE_UC = uncached). GFX9 doesn't have this. 8229 // BTW, it disables TC L2 and therefore decreases performance. 8230 if (ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) 8231 RsrcDataFormat |= (2ULL << 59); 8232 } 8233 8234 return RsrcDataFormat; 8235 } 8236 8237 uint64_t SIInstrInfo::getScratchRsrcWords23() const { 8238 uint64_t Rsrc23 = getDefaultRsrcDataFormat() | 8239 AMDGPU::RSRC_TID_ENABLE | 8240 0xffffffff; // Size; 8241 8242 // GFX9 doesn't have ELEMENT_SIZE. 8243 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 8244 uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize(true)) - 1; 8245 Rsrc23 |= EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT; 8246 } 8247 8248 // IndexStride = 64 / 32. 8249 uint64_t IndexStride = ST.getWavefrontSize() == 64 ? 3 : 2; 8250 Rsrc23 |= IndexStride << AMDGPU::RSRC_INDEX_STRIDE_SHIFT; 8251 8252 // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17]. 8253 // Clear them unless we want a huge stride. 8254 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS && 8255 ST.getGeneration() <= AMDGPUSubtarget::GFX9) 8256 Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT; 8257 8258 return Rsrc23; 8259 } 8260 8261 bool SIInstrInfo::isLowLatencyInstruction(const MachineInstr &MI) const { 8262 unsigned Opc = MI.getOpcode(); 8263 8264 return isSMRD(Opc); 8265 } 8266 8267 bool SIInstrInfo::isHighLatencyDef(int Opc) const { 8268 return get(Opc).mayLoad() && 8269 (isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc) || isFLAT(Opc)); 8270 } 8271 8272 unsigned SIInstrInfo::isStackAccess(const MachineInstr &MI, 8273 int &FrameIndex) const { 8274 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 8275 if (!Addr || !Addr->isFI()) 8276 return Register(); 8277 8278 assert(!MI.memoperands_empty() && 8279 (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS); 8280 8281 FrameIndex = Addr->getIndex(); 8282 return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg(); 8283 } 8284 8285 unsigned SIInstrInfo::isSGPRStackAccess(const MachineInstr &MI, 8286 int &FrameIndex) const { 8287 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr); 8288 assert(Addr && Addr->isFI()); 8289 FrameIndex = Addr->getIndex(); 8290 return getNamedOperand(MI, AMDGPU::OpName::data)->getReg(); 8291 } 8292 8293 unsigned SIInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 8294 int &FrameIndex) const { 8295 if (!MI.mayLoad()) 8296 return Register(); 8297 8298 if (isMUBUF(MI) || isVGPRSpill(MI)) 8299 return isStackAccess(MI, FrameIndex); 8300 8301 if (isSGPRSpill(MI)) 8302 return isSGPRStackAccess(MI, FrameIndex); 8303 8304 return Register(); 8305 } 8306 8307 unsigned SIInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 8308 int &FrameIndex) const { 8309 if (!MI.mayStore()) 8310 return Register(); 8311 8312 if (isMUBUF(MI) || isVGPRSpill(MI)) 8313 return isStackAccess(MI, FrameIndex); 8314 8315 if (isSGPRSpill(MI)) 8316 return isSGPRStackAccess(MI, FrameIndex); 8317 8318 return Register(); 8319 } 8320 8321 unsigned SIInstrInfo::getInstBundleSize(const MachineInstr &MI) const { 8322 unsigned Size = 0; 8323 MachineBasicBlock::const_instr_iterator I = MI.getIterator(); 8324 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 8325 while (++I != E && I->isInsideBundle()) { 8326 assert(!I->isBundle() && "No nested bundle!"); 8327 Size += getInstSizeInBytes(*I); 8328 } 8329 8330 return Size; 8331 } 8332 8333 unsigned SIInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 8334 unsigned Opc = MI.getOpcode(); 8335 const MCInstrDesc &Desc = getMCOpcodeFromPseudo(Opc); 8336 unsigned DescSize = Desc.getSize(); 8337 8338 // If we have a definitive size, we can use it. Otherwise we need to inspect 8339 // the operands to know the size. 8340 if (isFixedSize(MI)) { 8341 unsigned Size = DescSize; 8342 8343 // If we hit the buggy offset, an extra nop will be inserted in MC so 8344 // estimate the worst case. 8345 if (MI.isBranch() && ST.hasOffset3fBug()) 8346 Size += 4; 8347 8348 return Size; 8349 } 8350 8351 // Instructions may have a 32-bit literal encoded after them. Check 8352 // operands that could ever be literals. 8353 if (isVALU(MI) || isSALU(MI)) { 8354 if (isDPP(MI)) 8355 return DescSize; 8356 bool HasLiteral = false; 8357 for (int I = 0, E = MI.getNumExplicitOperands(); I != E; ++I) { 8358 const MachineOperand &Op = MI.getOperand(I); 8359 const MCOperandInfo &OpInfo = Desc.operands()[I]; 8360 if (!Op.isReg() && !isInlineConstant(Op, OpInfo)) { 8361 HasLiteral = true; 8362 break; 8363 } 8364 } 8365 return HasLiteral ? DescSize + 4 : DescSize; 8366 } 8367 8368 // Check whether we have extra NSA words. 8369 if (isMIMG(MI)) { 8370 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0); 8371 if (VAddr0Idx < 0) 8372 return 8; 8373 8374 int RSrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc); 8375 return 8 + 4 * ((RSrcIdx - VAddr0Idx + 2) / 4); 8376 } 8377 8378 switch (Opc) { 8379 case TargetOpcode::BUNDLE: 8380 return getInstBundleSize(MI); 8381 case TargetOpcode::INLINEASM: 8382 case TargetOpcode::INLINEASM_BR: { 8383 const MachineFunction *MF = MI.getParent()->getParent(); 8384 const char *AsmStr = MI.getOperand(0).getSymbolName(); 8385 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo(), &ST); 8386 } 8387 default: 8388 if (MI.isMetaInstruction()) 8389 return 0; 8390 return DescSize; 8391 } 8392 } 8393 8394 bool SIInstrInfo::mayAccessFlatAddressSpace(const MachineInstr &MI) const { 8395 if (!isFLAT(MI)) 8396 return false; 8397 8398 if (MI.memoperands_empty()) 8399 return true; 8400 8401 for (const MachineMemOperand *MMO : MI.memoperands()) { 8402 if (MMO->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS) 8403 return true; 8404 } 8405 return false; 8406 } 8407 8408 bool SIInstrInfo::isNonUniformBranchInstr(MachineInstr &Branch) const { 8409 return Branch.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO; 8410 } 8411 8412 void SIInstrInfo::convertNonUniformIfRegion(MachineBasicBlock *IfEntry, 8413 MachineBasicBlock *IfEnd) const { 8414 MachineBasicBlock::iterator TI = IfEntry->getFirstTerminator(); 8415 assert(TI != IfEntry->end()); 8416 8417 MachineInstr *Branch = &(*TI); 8418 MachineFunction *MF = IfEntry->getParent(); 8419 MachineRegisterInfo &MRI = IfEntry->getParent()->getRegInfo(); 8420 8421 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 8422 Register DstReg = MRI.createVirtualRegister(RI.getBoolRC()); 8423 MachineInstr *SIIF = 8424 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_IF), DstReg) 8425 .add(Branch->getOperand(0)) 8426 .add(Branch->getOperand(1)); 8427 MachineInstr *SIEND = 8428 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_END_CF)) 8429 .addReg(DstReg); 8430 8431 IfEntry->erase(TI); 8432 IfEntry->insert(IfEntry->end(), SIIF); 8433 IfEnd->insert(IfEnd->getFirstNonPHI(), SIEND); 8434 } 8435 } 8436 8437 void SIInstrInfo::convertNonUniformLoopRegion( 8438 MachineBasicBlock *LoopEntry, MachineBasicBlock *LoopEnd) const { 8439 MachineBasicBlock::iterator TI = LoopEnd->getFirstTerminator(); 8440 // We expect 2 terminators, one conditional and one unconditional. 8441 assert(TI != LoopEnd->end()); 8442 8443 MachineInstr *Branch = &(*TI); 8444 MachineFunction *MF = LoopEnd->getParent(); 8445 MachineRegisterInfo &MRI = LoopEnd->getParent()->getRegInfo(); 8446 8447 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 8448 8449 Register DstReg = MRI.createVirtualRegister(RI.getBoolRC()); 8450 Register BackEdgeReg = MRI.createVirtualRegister(RI.getBoolRC()); 8451 MachineInstrBuilder HeaderPHIBuilder = 8452 BuildMI(*(MF), Branch->getDebugLoc(), get(TargetOpcode::PHI), DstReg); 8453 for (MachineBasicBlock *PMBB : LoopEntry->predecessors()) { 8454 if (PMBB == LoopEnd) { 8455 HeaderPHIBuilder.addReg(BackEdgeReg); 8456 } else { 8457 Register ZeroReg = MRI.createVirtualRegister(RI.getBoolRC()); 8458 materializeImmediate(*PMBB, PMBB->getFirstTerminator(), DebugLoc(), 8459 ZeroReg, 0); 8460 HeaderPHIBuilder.addReg(ZeroReg); 8461 } 8462 HeaderPHIBuilder.addMBB(PMBB); 8463 } 8464 MachineInstr *HeaderPhi = HeaderPHIBuilder; 8465 MachineInstr *SIIFBREAK = BuildMI(*(MF), Branch->getDebugLoc(), 8466 get(AMDGPU::SI_IF_BREAK), BackEdgeReg) 8467 .addReg(DstReg) 8468 .add(Branch->getOperand(0)); 8469 MachineInstr *SILOOP = 8470 BuildMI(*(MF), Branch->getDebugLoc(), get(AMDGPU::SI_LOOP)) 8471 .addReg(BackEdgeReg) 8472 .addMBB(LoopEntry); 8473 8474 LoopEntry->insert(LoopEntry->begin(), HeaderPhi); 8475 LoopEnd->erase(TI); 8476 LoopEnd->insert(LoopEnd->end(), SIIFBREAK); 8477 LoopEnd->insert(LoopEnd->end(), SILOOP); 8478 } 8479 } 8480 8481 ArrayRef<std::pair<int, const char *>> 8482 SIInstrInfo::getSerializableTargetIndices() const { 8483 static const std::pair<int, const char *> TargetIndices[] = { 8484 {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"}, 8485 {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"}, 8486 {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"}, 8487 {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"}, 8488 {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}}; 8489 return ArrayRef(TargetIndices); 8490 } 8491 8492 /// This is used by the post-RA scheduler (SchedulePostRAList.cpp). The 8493 /// post-RA version of misched uses CreateTargetMIHazardRecognizer. 8494 ScheduleHazardRecognizer * 8495 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 8496 const ScheduleDAG *DAG) const { 8497 return new GCNHazardRecognizer(DAG->MF); 8498 } 8499 8500 /// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer 8501 /// pass. 8502 ScheduleHazardRecognizer * 8503 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const { 8504 return new GCNHazardRecognizer(MF); 8505 } 8506 8507 // Called during: 8508 // - pre-RA scheduling and post-RA scheduling 8509 ScheduleHazardRecognizer * 8510 SIInstrInfo::CreateTargetMIHazardRecognizer(const InstrItineraryData *II, 8511 const ScheduleDAGMI *DAG) const { 8512 // Borrowed from Arm Target 8513 // We would like to restrict this hazard recognizer to only 8514 // post-RA scheduling; we can tell that we're post-RA because we don't 8515 // track VRegLiveness. 8516 if (!DAG->hasVRegLiveness()) 8517 return new GCNHazardRecognizer(DAG->MF); 8518 return TargetInstrInfo::CreateTargetMIHazardRecognizer(II, DAG); 8519 } 8520 8521 std::pair<unsigned, unsigned> 8522 SIInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 8523 return std::pair(TF & MO_MASK, TF & ~MO_MASK); 8524 } 8525 8526 ArrayRef<std::pair<unsigned, const char *>> 8527 SIInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 8528 static const std::pair<unsigned, const char *> TargetFlags[] = { 8529 { MO_GOTPCREL, "amdgpu-gotprel" }, 8530 { MO_GOTPCREL32_LO, "amdgpu-gotprel32-lo" }, 8531 { MO_GOTPCREL32_HI, "amdgpu-gotprel32-hi" }, 8532 { MO_REL32_LO, "amdgpu-rel32-lo" }, 8533 { MO_REL32_HI, "amdgpu-rel32-hi" }, 8534 { MO_ABS32_LO, "amdgpu-abs32-lo" }, 8535 { MO_ABS32_HI, "amdgpu-abs32-hi" }, 8536 }; 8537 8538 return ArrayRef(TargetFlags); 8539 } 8540 8541 ArrayRef<std::pair<MachineMemOperand::Flags, const char *>> 8542 SIInstrInfo::getSerializableMachineMemOperandTargetFlags() const { 8543 static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] = 8544 { 8545 {MONoClobber, "amdgpu-noclobber"}, 8546 }; 8547 8548 return ArrayRef(TargetFlags); 8549 } 8550 8551 unsigned SIInstrInfo::getLiveRangeSplitOpcode(Register SrcReg, 8552 const MachineFunction &MF) const { 8553 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 8554 assert(SrcReg.isVirtual()); 8555 if (MFI->checkFlag(SrcReg, AMDGPU::VirtRegFlag::WWM_REG)) 8556 return AMDGPU::WWM_COPY; 8557 8558 return AMDGPU::COPY; 8559 } 8560 8561 bool SIInstrInfo::isBasicBlockPrologue(const MachineInstr &MI, 8562 Register Reg) const { 8563 // We need to handle instructions which may be inserted during register 8564 // allocation to handle the prolog. The initial prolog instruction may have 8565 // been separated from the start of the block by spills and copies inserted 8566 // needed by the prolog. However, the insertions for scalar registers can 8567 // always be placed at the BB top as they are independent of the exec mask 8568 // value. 8569 bool IsNullOrVectorRegister = true; 8570 if (Reg) { 8571 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 8572 IsNullOrVectorRegister = !RI.isSGPRClass(RI.getRegClassForReg(MRI, Reg)); 8573 } 8574 8575 uint16_t Opc = MI.getOpcode(); 8576 // FIXME: Copies inserted in the block prolog for live-range split should also 8577 // be included. 8578 return IsNullOrVectorRegister && 8579 (isSpillOpcode(Opc) || (!MI.isTerminator() && Opc != AMDGPU::COPY && 8580 MI.modifiesRegister(AMDGPU::EXEC, &RI))); 8581 } 8582 8583 MachineInstrBuilder 8584 SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB, 8585 MachineBasicBlock::iterator I, 8586 const DebugLoc &DL, 8587 Register DestReg) const { 8588 if (ST.hasAddNoCarry()) 8589 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e64), DestReg); 8590 8591 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 8592 Register UnusedCarry = MRI.createVirtualRegister(RI.getBoolRC()); 8593 MRI.setRegAllocationHint(UnusedCarry, 0, RI.getVCC()); 8594 8595 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg) 8596 .addReg(UnusedCarry, RegState::Define | RegState::Dead); 8597 } 8598 8599 MachineInstrBuilder SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB, 8600 MachineBasicBlock::iterator I, 8601 const DebugLoc &DL, 8602 Register DestReg, 8603 RegScavenger &RS) const { 8604 if (ST.hasAddNoCarry()) 8605 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e32), DestReg); 8606 8607 // If available, prefer to use vcc. 8608 Register UnusedCarry = !RS.isRegUsed(AMDGPU::VCC) 8609 ? Register(RI.getVCC()) 8610 : RS.scavengeRegisterBackwards( 8611 *RI.getBoolRC(), I, /* RestoreAfter */ false, 8612 0, /* AllowSpill */ false); 8613 8614 // TODO: Users need to deal with this. 8615 if (!UnusedCarry.isValid()) 8616 return MachineInstrBuilder(); 8617 8618 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg) 8619 .addReg(UnusedCarry, RegState::Define | RegState::Dead); 8620 } 8621 8622 bool SIInstrInfo::isKillTerminator(unsigned Opcode) { 8623 switch (Opcode) { 8624 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR: 8625 case AMDGPU::SI_KILL_I1_TERMINATOR: 8626 return true; 8627 default: 8628 return false; 8629 } 8630 } 8631 8632 const MCInstrDesc &SIInstrInfo::getKillTerminatorFromPseudo(unsigned Opcode) const { 8633 switch (Opcode) { 8634 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO: 8635 return get(AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR); 8636 case AMDGPU::SI_KILL_I1_PSEUDO: 8637 return get(AMDGPU::SI_KILL_I1_TERMINATOR); 8638 default: 8639 llvm_unreachable("invalid opcode, expected SI_KILL_*_PSEUDO"); 8640 } 8641 } 8642 8643 bool SIInstrInfo::isLegalMUBUFImmOffset(unsigned Imm) const { 8644 return Imm <= getMaxMUBUFImmOffset(ST); 8645 } 8646 8647 unsigned SIInstrInfo::getMaxMUBUFImmOffset(const GCNSubtarget &ST) { 8648 // GFX12 field is non-negative 24-bit signed byte offset. 8649 const unsigned OffsetBits = 8650 ST.getGeneration() >= AMDGPUSubtarget::GFX12 ? 23 : 12; 8651 return (1 << OffsetBits) - 1; 8652 } 8653 8654 void SIInstrInfo::fixImplicitOperands(MachineInstr &MI) const { 8655 if (!ST.isWave32()) 8656 return; 8657 8658 if (MI.isInlineAsm()) 8659 return; 8660 8661 for (auto &Op : MI.implicit_operands()) { 8662 if (Op.isReg() && Op.getReg() == AMDGPU::VCC) 8663 Op.setReg(AMDGPU::VCC_LO); 8664 } 8665 } 8666 8667 bool SIInstrInfo::isBufferSMRD(const MachineInstr &MI) const { 8668 if (!isSMRD(MI)) 8669 return false; 8670 8671 // Check that it is using a buffer resource. 8672 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sbase); 8673 if (Idx == -1) // e.g. s_memtime 8674 return false; 8675 8676 const auto RCID = MI.getDesc().operands()[Idx].RegClass; 8677 return RI.getRegClass(RCID)->hasSubClassEq(&AMDGPU::SGPR_128RegClass); 8678 } 8679 8680 // Given Imm, split it into the values to put into the SOffset and ImmOffset 8681 // fields in an MUBUF instruction. Return false if it is not possible (due to a 8682 // hardware bug needing a workaround). 8683 // 8684 // The required alignment ensures that individual address components remain 8685 // aligned if they are aligned to begin with. It also ensures that additional 8686 // offsets within the given alignment can be added to the resulting ImmOffset. 8687 bool SIInstrInfo::splitMUBUFOffset(uint32_t Imm, uint32_t &SOffset, 8688 uint32_t &ImmOffset, Align Alignment) const { 8689 const uint32_t MaxOffset = SIInstrInfo::getMaxMUBUFImmOffset(ST); 8690 const uint32_t MaxImm = alignDown(MaxOffset, Alignment.value()); 8691 uint32_t Overflow = 0; 8692 8693 if (Imm > MaxImm) { 8694 if (Imm <= MaxImm + 64) { 8695 // Use an SOffset inline constant for 4..64 8696 Overflow = Imm - MaxImm; 8697 Imm = MaxImm; 8698 } else { 8699 // Try to keep the same value in SOffset for adjacent loads, so that 8700 // the corresponding register contents can be re-used. 8701 // 8702 // Load values with all low-bits (except for alignment bits) set into 8703 // SOffset, so that a larger range of values can be covered using 8704 // s_movk_i32. 8705 // 8706 // Atomic operations fail to work correctly when individual address 8707 // components are unaligned, even if their sum is aligned. 8708 uint32_t High = (Imm + Alignment.value()) & ~MaxOffset; 8709 uint32_t Low = (Imm + Alignment.value()) & MaxOffset; 8710 Imm = Low; 8711 Overflow = High - Alignment.value(); 8712 } 8713 } 8714 8715 if (Overflow > 0) { 8716 // There is a hardware bug in SI and CI which prevents address clamping in 8717 // MUBUF instructions from working correctly with SOffsets. The immediate 8718 // offset is unaffected. 8719 if (ST.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) 8720 return false; 8721 8722 // It is not possible to set immediate in SOffset field on some targets. 8723 if (ST.hasRestrictedSOffset()) 8724 return false; 8725 } 8726 8727 ImmOffset = Imm; 8728 SOffset = Overflow; 8729 return true; 8730 } 8731 8732 // Depending on the used address space and instructions, some immediate offsets 8733 // are allowed and some are not. 8734 // In general, flat instruction offsets can only be non-negative, global and 8735 // scratch instruction offsets can also be negative. 8736 // 8737 // There are several bugs related to these offsets: 8738 // On gfx10.1, flat instructions that go into the global address space cannot 8739 // use an offset. 8740 // 8741 // For scratch instructions, the address can be either an SGPR or a VGPR. 8742 // The following offsets can be used, depending on the architecture (x means 8743 // cannot be used): 8744 // +----------------------------+------+------+ 8745 // | Address-Mode | SGPR | VGPR | 8746 // +----------------------------+------+------+ 8747 // | gfx9 | | | 8748 // | negative, 4-aligned offset | x | ok | 8749 // | negative, unaligned offset | x | ok | 8750 // +----------------------------+------+------+ 8751 // | gfx10 | | | 8752 // | negative, 4-aligned offset | ok | ok | 8753 // | negative, unaligned offset | ok | x | 8754 // +----------------------------+------+------+ 8755 // | gfx10.3 | | | 8756 // | negative, 4-aligned offset | ok | ok | 8757 // | negative, unaligned offset | ok | ok | 8758 // +----------------------------+------+------+ 8759 // 8760 // This function ignores the addressing mode, so if an offset cannot be used in 8761 // one addressing mode, it is considered illegal. 8762 bool SIInstrInfo::isLegalFLATOffset(int64_t Offset, unsigned AddrSpace, 8763 uint64_t FlatVariant) const { 8764 // TODO: Should 0 be special cased? 8765 if (!ST.hasFlatInstOffsets()) 8766 return false; 8767 8768 if (ST.hasFlatSegmentOffsetBug() && FlatVariant == SIInstrFlags::FLAT && 8769 (AddrSpace == AMDGPUAS::FLAT_ADDRESS || 8770 AddrSpace == AMDGPUAS::GLOBAL_ADDRESS)) 8771 return false; 8772 8773 if (ST.hasNegativeUnalignedScratchOffsetBug() && 8774 FlatVariant == SIInstrFlags::FlatScratch && Offset < 0 && 8775 (Offset % 4) != 0) { 8776 return false; 8777 } 8778 8779 bool AllowNegative = allowNegativeFlatOffset(FlatVariant); 8780 unsigned N = AMDGPU::getNumFlatOffsetBits(ST); 8781 return isIntN(N, Offset) && (AllowNegative || Offset >= 0); 8782 } 8783 8784 // See comment on SIInstrInfo::isLegalFLATOffset for what is legal and what not. 8785 std::pair<int64_t, int64_t> 8786 SIInstrInfo::splitFlatOffset(int64_t COffsetVal, unsigned AddrSpace, 8787 uint64_t FlatVariant) const { 8788 int64_t RemainderOffset = COffsetVal; 8789 int64_t ImmField = 0; 8790 8791 bool AllowNegative = allowNegativeFlatOffset(FlatVariant); 8792 const unsigned NumBits = AMDGPU::getNumFlatOffsetBits(ST) - 1; 8793 8794 if (AllowNegative) { 8795 // Use signed division by a power of two to truncate towards 0. 8796 int64_t D = 1LL << NumBits; 8797 RemainderOffset = (COffsetVal / D) * D; 8798 ImmField = COffsetVal - RemainderOffset; 8799 8800 if (ST.hasNegativeUnalignedScratchOffsetBug() && 8801 FlatVariant == SIInstrFlags::FlatScratch && ImmField < 0 && 8802 (ImmField % 4) != 0) { 8803 // Make ImmField a multiple of 4 8804 RemainderOffset += ImmField % 4; 8805 ImmField -= ImmField % 4; 8806 } 8807 } else if (COffsetVal >= 0) { 8808 ImmField = COffsetVal & maskTrailingOnes<uint64_t>(NumBits); 8809 RemainderOffset = COffsetVal - ImmField; 8810 } 8811 8812 assert(isLegalFLATOffset(ImmField, AddrSpace, FlatVariant)); 8813 assert(RemainderOffset + ImmField == COffsetVal); 8814 return {ImmField, RemainderOffset}; 8815 } 8816 8817 bool SIInstrInfo::allowNegativeFlatOffset(uint64_t FlatVariant) const { 8818 if (ST.hasNegativeScratchOffsetBug() && 8819 FlatVariant == SIInstrFlags::FlatScratch) 8820 return false; 8821 8822 return FlatVariant != SIInstrFlags::FLAT || AMDGPU::isGFX12Plus(ST); 8823 } 8824 8825 static unsigned subtargetEncodingFamily(const GCNSubtarget &ST) { 8826 switch (ST.getGeneration()) { 8827 default: 8828 break; 8829 case AMDGPUSubtarget::SOUTHERN_ISLANDS: 8830 case AMDGPUSubtarget::SEA_ISLANDS: 8831 return SIEncodingFamily::SI; 8832 case AMDGPUSubtarget::VOLCANIC_ISLANDS: 8833 case AMDGPUSubtarget::GFX9: 8834 return SIEncodingFamily::VI; 8835 case AMDGPUSubtarget::GFX10: 8836 return SIEncodingFamily::GFX10; 8837 case AMDGPUSubtarget::GFX11: 8838 return SIEncodingFamily::GFX11; 8839 case AMDGPUSubtarget::GFX12: 8840 return SIEncodingFamily::GFX12; 8841 } 8842 llvm_unreachable("Unknown subtarget generation!"); 8843 } 8844 8845 bool SIInstrInfo::isAsmOnlyOpcode(int MCOp) const { 8846 switch(MCOp) { 8847 // These opcodes use indirect register addressing so 8848 // they need special handling by codegen (currently missing). 8849 // Therefore it is too risky to allow these opcodes 8850 // to be selected by dpp combiner or sdwa peepholer. 8851 case AMDGPU::V_MOVRELS_B32_dpp_gfx10: 8852 case AMDGPU::V_MOVRELS_B32_sdwa_gfx10: 8853 case AMDGPU::V_MOVRELD_B32_dpp_gfx10: 8854 case AMDGPU::V_MOVRELD_B32_sdwa_gfx10: 8855 case AMDGPU::V_MOVRELSD_B32_dpp_gfx10: 8856 case AMDGPU::V_MOVRELSD_B32_sdwa_gfx10: 8857 case AMDGPU::V_MOVRELSD_2_B32_dpp_gfx10: 8858 case AMDGPU::V_MOVRELSD_2_B32_sdwa_gfx10: 8859 return true; 8860 default: 8861 return false; 8862 } 8863 } 8864 8865 int SIInstrInfo::pseudoToMCOpcode(int Opcode) const { 8866 if (SIInstrInfo::isSoftWaitcnt(Opcode)) 8867 Opcode = SIInstrInfo::getNonSoftWaitcntOpcode(Opcode); 8868 8869 unsigned Gen = subtargetEncodingFamily(ST); 8870 8871 if ((get(Opcode).TSFlags & SIInstrFlags::renamedInGFX9) != 0 && 8872 ST.getGeneration() == AMDGPUSubtarget::GFX9) 8873 Gen = SIEncodingFamily::GFX9; 8874 8875 // Adjust the encoding family to GFX80 for D16 buffer instructions when the 8876 // subtarget has UnpackedD16VMem feature. 8877 // TODO: remove this when we discard GFX80 encoding. 8878 if (ST.hasUnpackedD16VMem() && (get(Opcode).TSFlags & SIInstrFlags::D16Buf)) 8879 Gen = SIEncodingFamily::GFX80; 8880 8881 if (get(Opcode).TSFlags & SIInstrFlags::SDWA) { 8882 switch (ST.getGeneration()) { 8883 default: 8884 Gen = SIEncodingFamily::SDWA; 8885 break; 8886 case AMDGPUSubtarget::GFX9: 8887 Gen = SIEncodingFamily::SDWA9; 8888 break; 8889 case AMDGPUSubtarget::GFX10: 8890 Gen = SIEncodingFamily::SDWA10; 8891 break; 8892 } 8893 } 8894 8895 if (isMAI(Opcode)) { 8896 int MFMAOp = AMDGPU::getMFMAEarlyClobberOp(Opcode); 8897 if (MFMAOp != -1) 8898 Opcode = MFMAOp; 8899 } 8900 8901 int MCOp = AMDGPU::getMCOpcode(Opcode, Gen); 8902 8903 // TODO-GFX12: Remove this. 8904 // Hack to allow some GFX12 codegen tests to run before all the encodings are 8905 // implemented. 8906 if (MCOp == (uint16_t)-1 && Gen == SIEncodingFamily::GFX12) 8907 MCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX11); 8908 8909 // -1 means that Opcode is already a native instruction. 8910 if (MCOp == -1) 8911 return Opcode; 8912 8913 if (ST.hasGFX90AInsts()) { 8914 uint16_t NMCOp = (uint16_t)-1; 8915 if (ST.hasGFX940Insts()) 8916 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX940); 8917 if (NMCOp == (uint16_t)-1) 8918 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX90A); 8919 if (NMCOp == (uint16_t)-1) 8920 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX9); 8921 if (NMCOp != (uint16_t)-1) 8922 MCOp = NMCOp; 8923 } 8924 8925 // (uint16_t)-1 means that Opcode is a pseudo instruction that has 8926 // no encoding in the given subtarget generation. 8927 if (MCOp == (uint16_t)-1) 8928 return -1; 8929 8930 if (isAsmOnlyOpcode(MCOp)) 8931 return -1; 8932 8933 return MCOp; 8934 } 8935 8936 static 8937 TargetInstrInfo::RegSubRegPair getRegOrUndef(const MachineOperand &RegOpnd) { 8938 assert(RegOpnd.isReg()); 8939 return RegOpnd.isUndef() ? TargetInstrInfo::RegSubRegPair() : 8940 getRegSubRegPair(RegOpnd); 8941 } 8942 8943 TargetInstrInfo::RegSubRegPair 8944 llvm::getRegSequenceSubReg(MachineInstr &MI, unsigned SubReg) { 8945 assert(MI.isRegSequence()); 8946 for (unsigned I = 0, E = (MI.getNumOperands() - 1)/ 2; I < E; ++I) 8947 if (MI.getOperand(1 + 2 * I + 1).getImm() == SubReg) { 8948 auto &RegOp = MI.getOperand(1 + 2 * I); 8949 return getRegOrUndef(RegOp); 8950 } 8951 return TargetInstrInfo::RegSubRegPair(); 8952 } 8953 8954 // Try to find the definition of reg:subreg in subreg-manipulation pseudos 8955 // Following a subreg of reg:subreg isn't supported 8956 static bool followSubRegDef(MachineInstr &MI, 8957 TargetInstrInfo::RegSubRegPair &RSR) { 8958 if (!RSR.SubReg) 8959 return false; 8960 switch (MI.getOpcode()) { 8961 default: break; 8962 case AMDGPU::REG_SEQUENCE: 8963 RSR = getRegSequenceSubReg(MI, RSR.SubReg); 8964 return true; 8965 // EXTRACT_SUBREG ins't supported as this would follow a subreg of subreg 8966 case AMDGPU::INSERT_SUBREG: 8967 if (RSR.SubReg == (unsigned)MI.getOperand(3).getImm()) 8968 // inserted the subreg we're looking for 8969 RSR = getRegOrUndef(MI.getOperand(2)); 8970 else { // the subreg in the rest of the reg 8971 auto R1 = getRegOrUndef(MI.getOperand(1)); 8972 if (R1.SubReg) // subreg of subreg isn't supported 8973 return false; 8974 RSR.Reg = R1.Reg; 8975 } 8976 return true; 8977 } 8978 return false; 8979 } 8980 8981 MachineInstr *llvm::getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P, 8982 MachineRegisterInfo &MRI) { 8983 assert(MRI.isSSA()); 8984 if (!P.Reg.isVirtual()) 8985 return nullptr; 8986 8987 auto RSR = P; 8988 auto *DefInst = MRI.getVRegDef(RSR.Reg); 8989 while (auto *MI = DefInst) { 8990 DefInst = nullptr; 8991 switch (MI->getOpcode()) { 8992 case AMDGPU::COPY: 8993 case AMDGPU::V_MOV_B32_e32: { 8994 auto &Op1 = MI->getOperand(1); 8995 if (Op1.isReg() && Op1.getReg().isVirtual()) { 8996 if (Op1.isUndef()) 8997 return nullptr; 8998 RSR = getRegSubRegPair(Op1); 8999 DefInst = MRI.getVRegDef(RSR.Reg); 9000 } 9001 break; 9002 } 9003 default: 9004 if (followSubRegDef(*MI, RSR)) { 9005 if (!RSR.Reg) 9006 return nullptr; 9007 DefInst = MRI.getVRegDef(RSR.Reg); 9008 } 9009 } 9010 if (!DefInst) 9011 return MI; 9012 } 9013 return nullptr; 9014 } 9015 9016 bool llvm::execMayBeModifiedBeforeUse(const MachineRegisterInfo &MRI, 9017 Register VReg, 9018 const MachineInstr &DefMI, 9019 const MachineInstr &UseMI) { 9020 assert(MRI.isSSA() && "Must be run on SSA"); 9021 9022 auto *TRI = MRI.getTargetRegisterInfo(); 9023 auto *DefBB = DefMI.getParent(); 9024 9025 // Don't bother searching between blocks, although it is possible this block 9026 // doesn't modify exec. 9027 if (UseMI.getParent() != DefBB) 9028 return true; 9029 9030 const int MaxInstScan = 20; 9031 int NumInst = 0; 9032 9033 // Stop scan at the use. 9034 auto E = UseMI.getIterator(); 9035 for (auto I = std::next(DefMI.getIterator()); I != E; ++I) { 9036 if (I->isDebugInstr()) 9037 continue; 9038 9039 if (++NumInst > MaxInstScan) 9040 return true; 9041 9042 if (I->modifiesRegister(AMDGPU::EXEC, TRI)) 9043 return true; 9044 } 9045 9046 return false; 9047 } 9048 9049 bool llvm::execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo &MRI, 9050 Register VReg, 9051 const MachineInstr &DefMI) { 9052 assert(MRI.isSSA() && "Must be run on SSA"); 9053 9054 auto *TRI = MRI.getTargetRegisterInfo(); 9055 auto *DefBB = DefMI.getParent(); 9056 9057 const int MaxUseScan = 10; 9058 int NumUse = 0; 9059 9060 for (auto &Use : MRI.use_nodbg_operands(VReg)) { 9061 auto &UseInst = *Use.getParent(); 9062 // Don't bother searching between blocks, although it is possible this block 9063 // doesn't modify exec. 9064 if (UseInst.getParent() != DefBB || UseInst.isPHI()) 9065 return true; 9066 9067 if (++NumUse > MaxUseScan) 9068 return true; 9069 } 9070 9071 if (NumUse == 0) 9072 return false; 9073 9074 const int MaxInstScan = 20; 9075 int NumInst = 0; 9076 9077 // Stop scan when we have seen all the uses. 9078 for (auto I = std::next(DefMI.getIterator()); ; ++I) { 9079 assert(I != DefBB->end()); 9080 9081 if (I->isDebugInstr()) 9082 continue; 9083 9084 if (++NumInst > MaxInstScan) 9085 return true; 9086 9087 for (const MachineOperand &Op : I->operands()) { 9088 // We don't check reg masks here as they're used only on calls: 9089 // 1. EXEC is only considered const within one BB 9090 // 2. Call should be a terminator instruction if present in a BB 9091 9092 if (!Op.isReg()) 9093 continue; 9094 9095 Register Reg = Op.getReg(); 9096 if (Op.isUse()) { 9097 if (Reg == VReg && --NumUse == 0) 9098 return false; 9099 } else if (TRI->regsOverlap(Reg, AMDGPU::EXEC)) 9100 return true; 9101 } 9102 } 9103 } 9104 9105 MachineInstr *SIInstrInfo::createPHIDestinationCopy( 9106 MachineBasicBlock &MBB, MachineBasicBlock::iterator LastPHIIt, 9107 const DebugLoc &DL, Register Src, Register Dst) const { 9108 auto Cur = MBB.begin(); 9109 if (Cur != MBB.end()) 9110 do { 9111 if (!Cur->isPHI() && Cur->readsRegister(Dst)) 9112 return BuildMI(MBB, Cur, DL, get(TargetOpcode::COPY), Dst).addReg(Src); 9113 ++Cur; 9114 } while (Cur != MBB.end() && Cur != LastPHIIt); 9115 9116 return TargetInstrInfo::createPHIDestinationCopy(MBB, LastPHIIt, DL, Src, 9117 Dst); 9118 } 9119 9120 MachineInstr *SIInstrInfo::createPHISourceCopy( 9121 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt, 9122 const DebugLoc &DL, Register Src, unsigned SrcSubReg, Register Dst) const { 9123 if (InsPt != MBB.end() && 9124 (InsPt->getOpcode() == AMDGPU::SI_IF || 9125 InsPt->getOpcode() == AMDGPU::SI_ELSE || 9126 InsPt->getOpcode() == AMDGPU::SI_IF_BREAK) && 9127 InsPt->definesRegister(Src)) { 9128 InsPt++; 9129 return BuildMI(MBB, InsPt, DL, 9130 get(ST.isWave32() ? AMDGPU::S_MOV_B32_term 9131 : AMDGPU::S_MOV_B64_term), 9132 Dst) 9133 .addReg(Src, 0, SrcSubReg) 9134 .addReg(AMDGPU::EXEC, RegState::Implicit); 9135 } 9136 return TargetInstrInfo::createPHISourceCopy(MBB, InsPt, DL, Src, SrcSubReg, 9137 Dst); 9138 } 9139 9140 bool llvm::SIInstrInfo::isWave32() const { return ST.isWave32(); } 9141 9142 MachineInstr *SIInstrInfo::foldMemoryOperandImpl( 9143 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 9144 MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS, 9145 VirtRegMap *VRM) const { 9146 // This is a bit of a hack (copied from AArch64). Consider this instruction: 9147 // 9148 // %0:sreg_32 = COPY $m0 9149 // 9150 // We explicitly chose SReg_32 for the virtual register so such a copy might 9151 // be eliminated by RegisterCoalescer. However, that may not be possible, and 9152 // %0 may even spill. We can't spill $m0 normally (it would require copying to 9153 // a numbered SGPR anyway), and since it is in the SReg_32 register class, 9154 // TargetInstrInfo::foldMemoryOperand() is going to try. 9155 // A similar issue also exists with spilling and reloading $exec registers. 9156 // 9157 // To prevent that, constrain the %0 register class here. 9158 if (isFullCopyInstr(MI)) { 9159 Register DstReg = MI.getOperand(0).getReg(); 9160 Register SrcReg = MI.getOperand(1).getReg(); 9161 if ((DstReg.isVirtual() || SrcReg.isVirtual()) && 9162 (DstReg.isVirtual() != SrcReg.isVirtual())) { 9163 MachineRegisterInfo &MRI = MF.getRegInfo(); 9164 Register VirtReg = DstReg.isVirtual() ? DstReg : SrcReg; 9165 const TargetRegisterClass *RC = MRI.getRegClass(VirtReg); 9166 if (RC->hasSuperClassEq(&AMDGPU::SReg_32RegClass)) { 9167 MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_32_XM0_XEXECRegClass); 9168 return nullptr; 9169 } else if (RC->hasSuperClassEq(&AMDGPU::SReg_64RegClass)) { 9170 MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_64_XEXECRegClass); 9171 return nullptr; 9172 } 9173 } 9174 } 9175 9176 return nullptr; 9177 } 9178 9179 unsigned SIInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 9180 const MachineInstr &MI, 9181 unsigned *PredCost) const { 9182 if (MI.isBundle()) { 9183 MachineBasicBlock::const_instr_iterator I(MI.getIterator()); 9184 MachineBasicBlock::const_instr_iterator E(MI.getParent()->instr_end()); 9185 unsigned Lat = 0, Count = 0; 9186 for (++I; I != E && I->isBundledWithPred(); ++I) { 9187 ++Count; 9188 Lat = std::max(Lat, SchedModel.computeInstrLatency(&*I)); 9189 } 9190 return Lat + Count - 1; 9191 } 9192 9193 return SchedModel.computeInstrLatency(&MI); 9194 } 9195 9196 InstructionUniformity 9197 SIInstrInfo::getGenericInstructionUniformity(const MachineInstr &MI) const { 9198 unsigned opcode = MI.getOpcode(); 9199 if (auto *GI = dyn_cast<GIntrinsic>(&MI)) { 9200 auto IID = GI->getIntrinsicID(); 9201 if (AMDGPU::isIntrinsicSourceOfDivergence(IID)) 9202 return InstructionUniformity::NeverUniform; 9203 if (AMDGPU::isIntrinsicAlwaysUniform(IID)) 9204 return InstructionUniformity::AlwaysUniform; 9205 9206 switch (IID) { 9207 case Intrinsic::amdgcn_if: 9208 case Intrinsic::amdgcn_else: 9209 // FIXME: Uniform if second result 9210 break; 9211 } 9212 9213 return InstructionUniformity::Default; 9214 } 9215 9216 // Loads from the private and flat address spaces are divergent, because 9217 // threads can execute the load instruction with the same inputs and get 9218 // different results. 9219 // 9220 // All other loads are not divergent, because if threads issue loads with the 9221 // same arguments, they will always get the same result. 9222 if (opcode == AMDGPU::G_LOAD) { 9223 if (MI.memoperands_empty()) 9224 return InstructionUniformity::NeverUniform; // conservative assumption 9225 9226 if (llvm::any_of(MI.memoperands(), [](const MachineMemOperand *mmo) { 9227 return mmo->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS || 9228 mmo->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS; 9229 })) { 9230 // At least one MMO in a non-global address space. 9231 return InstructionUniformity::NeverUniform; 9232 } 9233 return InstructionUniformity::Default; 9234 } 9235 9236 if (SIInstrInfo::isGenericAtomicRMWOpcode(opcode) || 9237 opcode == AMDGPU::G_ATOMIC_CMPXCHG || 9238 opcode == AMDGPU::G_ATOMIC_CMPXCHG_WITH_SUCCESS || 9239 AMDGPU::isGenericAtomic(opcode)) { 9240 return InstructionUniformity::NeverUniform; 9241 } 9242 return InstructionUniformity::Default; 9243 } 9244 9245 InstructionUniformity 9246 SIInstrInfo::getInstructionUniformity(const MachineInstr &MI) const { 9247 9248 if (isNeverUniform(MI)) 9249 return InstructionUniformity::NeverUniform; 9250 9251 unsigned opcode = MI.getOpcode(); 9252 if (opcode == AMDGPU::V_READLANE_B32 || 9253 opcode == AMDGPU::V_READFIRSTLANE_B32 || 9254 opcode == AMDGPU::SI_RESTORE_S32_FROM_VGPR) 9255 return InstructionUniformity::AlwaysUniform; 9256 9257 if (isCopyInstr(MI)) { 9258 const MachineOperand &srcOp = MI.getOperand(1); 9259 if (srcOp.isReg() && srcOp.getReg().isPhysical()) { 9260 const TargetRegisterClass *regClass = 9261 RI.getPhysRegBaseClass(srcOp.getReg()); 9262 return RI.isSGPRClass(regClass) ? InstructionUniformity::AlwaysUniform 9263 : InstructionUniformity::NeverUniform; 9264 } 9265 return InstructionUniformity::Default; 9266 } 9267 9268 // GMIR handling 9269 if (MI.isPreISelOpcode()) 9270 return SIInstrInfo::getGenericInstructionUniformity(MI); 9271 9272 // Atomics are divergent because they are executed sequentially: when an 9273 // atomic operation refers to the same address in each thread, then each 9274 // thread after the first sees the value written by the previous thread as 9275 // original value. 9276 9277 if (isAtomic(MI)) 9278 return InstructionUniformity::NeverUniform; 9279 9280 // Loads from the private and flat address spaces are divergent, because 9281 // threads can execute the load instruction with the same inputs and get 9282 // different results. 9283 if (isFLAT(MI) && MI.mayLoad()) { 9284 if (MI.memoperands_empty()) 9285 return InstructionUniformity::NeverUniform; // conservative assumption 9286 9287 if (llvm::any_of(MI.memoperands(), [](const MachineMemOperand *mmo) { 9288 return mmo->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS || 9289 mmo->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS; 9290 })) { 9291 // At least one MMO in a non-global address space. 9292 return InstructionUniformity::NeverUniform; 9293 } 9294 9295 return InstructionUniformity::Default; 9296 } 9297 9298 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 9299 const AMDGPURegisterBankInfo *RBI = ST.getRegBankInfo(); 9300 9301 // FIXME: It's conceptually broken to report this for an instruction, and not 9302 // a specific def operand. For inline asm in particular, there could be mixed 9303 // uniform and divergent results. 9304 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { 9305 const MachineOperand &SrcOp = MI.getOperand(I); 9306 if (!SrcOp.isReg()) 9307 continue; 9308 9309 Register Reg = SrcOp.getReg(); 9310 if (!Reg || !SrcOp.readsReg()) 9311 continue; 9312 9313 // If RegBank is null, this is unassigned or an unallocatable special 9314 // register, which are all scalars. 9315 const RegisterBank *RegBank = RBI->getRegBank(Reg, MRI, RI); 9316 if (RegBank && RegBank->getID() != AMDGPU::SGPRRegBankID) 9317 return InstructionUniformity::NeverUniform; 9318 } 9319 9320 // TODO: Uniformity check condtions above can be rearranged for more 9321 // redability 9322 9323 // TODO: amdgcn.{ballot, [if]cmp} should be AlwaysUniform, but they are 9324 // currently turned into no-op COPYs by SelectionDAG ISel and are 9325 // therefore no longer recognizable. 9326 9327 return InstructionUniformity::Default; 9328 } 9329 9330 unsigned SIInstrInfo::getDSShaderTypeValue(const MachineFunction &MF) { 9331 switch (MF.getFunction().getCallingConv()) { 9332 case CallingConv::AMDGPU_PS: 9333 return 1; 9334 case CallingConv::AMDGPU_VS: 9335 return 2; 9336 case CallingConv::AMDGPU_GS: 9337 return 3; 9338 case CallingConv::AMDGPU_HS: 9339 case CallingConv::AMDGPU_LS: 9340 case CallingConv::AMDGPU_ES: 9341 report_fatal_error("ds_ordered_count unsupported for this calling conv"); 9342 case CallingConv::AMDGPU_CS: 9343 case CallingConv::AMDGPU_KERNEL: 9344 case CallingConv::C: 9345 case CallingConv::Fast: 9346 default: 9347 // Assume other calling conventions are various compute callable functions 9348 return 0; 9349 } 9350 } 9351 9352 bool SIInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg, 9353 Register &SrcReg2, int64_t &CmpMask, 9354 int64_t &CmpValue) const { 9355 if (!MI.getOperand(0).isReg() || MI.getOperand(0).getSubReg()) 9356 return false; 9357 9358 switch (MI.getOpcode()) { 9359 default: 9360 break; 9361 case AMDGPU::S_CMP_EQ_U32: 9362 case AMDGPU::S_CMP_EQ_I32: 9363 case AMDGPU::S_CMP_LG_U32: 9364 case AMDGPU::S_CMP_LG_I32: 9365 case AMDGPU::S_CMP_LT_U32: 9366 case AMDGPU::S_CMP_LT_I32: 9367 case AMDGPU::S_CMP_GT_U32: 9368 case AMDGPU::S_CMP_GT_I32: 9369 case AMDGPU::S_CMP_LE_U32: 9370 case AMDGPU::S_CMP_LE_I32: 9371 case AMDGPU::S_CMP_GE_U32: 9372 case AMDGPU::S_CMP_GE_I32: 9373 case AMDGPU::S_CMP_EQ_U64: 9374 case AMDGPU::S_CMP_LG_U64: 9375 SrcReg = MI.getOperand(0).getReg(); 9376 if (MI.getOperand(1).isReg()) { 9377 if (MI.getOperand(1).getSubReg()) 9378 return false; 9379 SrcReg2 = MI.getOperand(1).getReg(); 9380 CmpValue = 0; 9381 } else if (MI.getOperand(1).isImm()) { 9382 SrcReg2 = Register(); 9383 CmpValue = MI.getOperand(1).getImm(); 9384 } else { 9385 return false; 9386 } 9387 CmpMask = ~0; 9388 return true; 9389 case AMDGPU::S_CMPK_EQ_U32: 9390 case AMDGPU::S_CMPK_EQ_I32: 9391 case AMDGPU::S_CMPK_LG_U32: 9392 case AMDGPU::S_CMPK_LG_I32: 9393 case AMDGPU::S_CMPK_LT_U32: 9394 case AMDGPU::S_CMPK_LT_I32: 9395 case AMDGPU::S_CMPK_GT_U32: 9396 case AMDGPU::S_CMPK_GT_I32: 9397 case AMDGPU::S_CMPK_LE_U32: 9398 case AMDGPU::S_CMPK_LE_I32: 9399 case AMDGPU::S_CMPK_GE_U32: 9400 case AMDGPU::S_CMPK_GE_I32: 9401 SrcReg = MI.getOperand(0).getReg(); 9402 SrcReg2 = Register(); 9403 CmpValue = MI.getOperand(1).getImm(); 9404 CmpMask = ~0; 9405 return true; 9406 } 9407 9408 return false; 9409 } 9410 9411 bool SIInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg, 9412 Register SrcReg2, int64_t CmpMask, 9413 int64_t CmpValue, 9414 const MachineRegisterInfo *MRI) const { 9415 if (!SrcReg || SrcReg.isPhysical()) 9416 return false; 9417 9418 if (SrcReg2 && !getFoldableImm(SrcReg2, *MRI, CmpValue)) 9419 return false; 9420 9421 const auto optimizeCmpAnd = [&CmpInstr, SrcReg, CmpValue, MRI, 9422 this](int64_t ExpectedValue, unsigned SrcSize, 9423 bool IsReversible, bool IsSigned) -> bool { 9424 // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 9425 // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 9426 // s_cmp_ge_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 9427 // s_cmp_ge_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 9428 // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 1 << n => s_and_b64 $src, 1 << n 9429 // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 9430 // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 9431 // s_cmp_gt_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 9432 // s_cmp_gt_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 9433 // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 0 => s_and_b64 $src, 1 << n 9434 // 9435 // Signed ge/gt are not used for the sign bit. 9436 // 9437 // If result of the AND is unused except in the compare: 9438 // s_and_b(32|64) $src, 1 << n => s_bitcmp1_b(32|64) $src, n 9439 // 9440 // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n 9441 // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n 9442 // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 0 => s_bitcmp0_b64 $src, n 9443 // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n 9444 // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n 9445 // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 1 << n => s_bitcmp0_b64 $src, n 9446 9447 MachineInstr *Def = MRI->getUniqueVRegDef(SrcReg); 9448 if (!Def || Def->getParent() != CmpInstr.getParent()) 9449 return false; 9450 9451 if (Def->getOpcode() != AMDGPU::S_AND_B32 && 9452 Def->getOpcode() != AMDGPU::S_AND_B64) 9453 return false; 9454 9455 int64_t Mask; 9456 const auto isMask = [&Mask, SrcSize](const MachineOperand *MO) -> bool { 9457 if (MO->isImm()) 9458 Mask = MO->getImm(); 9459 else if (!getFoldableImm(MO, Mask)) 9460 return false; 9461 Mask &= maxUIntN(SrcSize); 9462 return isPowerOf2_64(Mask); 9463 }; 9464 9465 MachineOperand *SrcOp = &Def->getOperand(1); 9466 if (isMask(SrcOp)) 9467 SrcOp = &Def->getOperand(2); 9468 else if (isMask(&Def->getOperand(2))) 9469 SrcOp = &Def->getOperand(1); 9470 else 9471 return false; 9472 9473 unsigned BitNo = llvm::countr_zero((uint64_t)Mask); 9474 if (IsSigned && BitNo == SrcSize - 1) 9475 return false; 9476 9477 ExpectedValue <<= BitNo; 9478 9479 bool IsReversedCC = false; 9480 if (CmpValue != ExpectedValue) { 9481 if (!IsReversible) 9482 return false; 9483 IsReversedCC = CmpValue == (ExpectedValue ^ Mask); 9484 if (!IsReversedCC) 9485 return false; 9486 } 9487 9488 Register DefReg = Def->getOperand(0).getReg(); 9489 if (IsReversedCC && !MRI->hasOneNonDBGUse(DefReg)) 9490 return false; 9491 9492 for (auto I = std::next(Def->getIterator()), E = CmpInstr.getIterator(); 9493 I != E; ++I) { 9494 if (I->modifiesRegister(AMDGPU::SCC, &RI) || 9495 I->killsRegister(AMDGPU::SCC, &RI)) 9496 return false; 9497 } 9498 9499 MachineOperand *SccDef = Def->findRegisterDefOperand(AMDGPU::SCC); 9500 SccDef->setIsDead(false); 9501 CmpInstr.eraseFromParent(); 9502 9503 if (!MRI->use_nodbg_empty(DefReg)) { 9504 assert(!IsReversedCC); 9505 return true; 9506 } 9507 9508 // Replace AND with unused result with a S_BITCMP. 9509 MachineBasicBlock *MBB = Def->getParent(); 9510 9511 unsigned NewOpc = (SrcSize == 32) ? IsReversedCC ? AMDGPU::S_BITCMP0_B32 9512 : AMDGPU::S_BITCMP1_B32 9513 : IsReversedCC ? AMDGPU::S_BITCMP0_B64 9514 : AMDGPU::S_BITCMP1_B64; 9515 9516 BuildMI(*MBB, Def, Def->getDebugLoc(), get(NewOpc)) 9517 .add(*SrcOp) 9518 .addImm(BitNo); 9519 Def->eraseFromParent(); 9520 9521 return true; 9522 }; 9523 9524 switch (CmpInstr.getOpcode()) { 9525 default: 9526 break; 9527 case AMDGPU::S_CMP_EQ_U32: 9528 case AMDGPU::S_CMP_EQ_I32: 9529 case AMDGPU::S_CMPK_EQ_U32: 9530 case AMDGPU::S_CMPK_EQ_I32: 9531 return optimizeCmpAnd(1, 32, true, false); 9532 case AMDGPU::S_CMP_GE_U32: 9533 case AMDGPU::S_CMPK_GE_U32: 9534 return optimizeCmpAnd(1, 32, false, false); 9535 case AMDGPU::S_CMP_GE_I32: 9536 case AMDGPU::S_CMPK_GE_I32: 9537 return optimizeCmpAnd(1, 32, false, true); 9538 case AMDGPU::S_CMP_EQ_U64: 9539 return optimizeCmpAnd(1, 64, true, false); 9540 case AMDGPU::S_CMP_LG_U32: 9541 case AMDGPU::S_CMP_LG_I32: 9542 case AMDGPU::S_CMPK_LG_U32: 9543 case AMDGPU::S_CMPK_LG_I32: 9544 return optimizeCmpAnd(0, 32, true, false); 9545 case AMDGPU::S_CMP_GT_U32: 9546 case AMDGPU::S_CMPK_GT_U32: 9547 return optimizeCmpAnd(0, 32, false, false); 9548 case AMDGPU::S_CMP_GT_I32: 9549 case AMDGPU::S_CMPK_GT_I32: 9550 return optimizeCmpAnd(0, 32, false, true); 9551 case AMDGPU::S_CMP_LG_U64: 9552 return optimizeCmpAnd(0, 64, true, false); 9553 } 9554 9555 return false; 9556 } 9557 9558 void SIInstrInfo::enforceOperandRCAlignment(MachineInstr &MI, 9559 unsigned OpName) const { 9560 if (!ST.needsAlignedVGPRs()) 9561 return; 9562 9563 int OpNo = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OpName); 9564 if (OpNo < 0) 9565 return; 9566 MachineOperand &Op = MI.getOperand(OpNo); 9567 if (getOpSize(MI, OpNo) > 4) 9568 return; 9569 9570 // Add implicit aligned super-reg to force alignment on the data operand. 9571 const DebugLoc &DL = MI.getDebugLoc(); 9572 MachineBasicBlock *BB = MI.getParent(); 9573 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo(); 9574 Register DataReg = Op.getReg(); 9575 bool IsAGPR = RI.isAGPR(MRI, DataReg); 9576 Register Undef = MRI.createVirtualRegister( 9577 IsAGPR ? &AMDGPU::AGPR_32RegClass : &AMDGPU::VGPR_32RegClass); 9578 BuildMI(*BB, MI, DL, get(AMDGPU::IMPLICIT_DEF), Undef); 9579 Register NewVR = 9580 MRI.createVirtualRegister(IsAGPR ? &AMDGPU::AReg_64_Align2RegClass 9581 : &AMDGPU::VReg_64_Align2RegClass); 9582 BuildMI(*BB, MI, DL, get(AMDGPU::REG_SEQUENCE), NewVR) 9583 .addReg(DataReg, 0, Op.getSubReg()) 9584 .addImm(AMDGPU::sub0) 9585 .addReg(Undef) 9586 .addImm(AMDGPU::sub1); 9587 Op.setReg(NewVR); 9588 Op.setSubReg(AMDGPU::sub0); 9589 MI.addOperand(MachineOperand::CreateReg(NewVR, false, true)); 9590 } 9591