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