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