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