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 4370 if (!Src0.isReg() && !Src1.isReg() && 4371 !isInlineConstant(Src0, Desc.OpInfo[Src0Idx].OperandType) && 4372 !isInlineConstant(Src1, Desc.OpInfo[Src1Idx].OperandType) && 4373 !Src0.isIdenticalTo(Src1)) { 4374 ErrInfo = "SOP2/SOPC instruction requires too many immediate constants"; 4375 return false; 4376 } 4377 } 4378 4379 if (isSOPK(MI)) { 4380 auto Op = getNamedOperand(MI, AMDGPU::OpName::simm16); 4381 if (Desc.isBranch()) { 4382 if (!Op->isMBB()) { 4383 ErrInfo = "invalid branch target for SOPK instruction"; 4384 return false; 4385 } 4386 } else { 4387 uint64_t Imm = Op->getImm(); 4388 if (sopkIsZext(MI)) { 4389 if (!isUInt<16>(Imm)) { 4390 ErrInfo = "invalid immediate for SOPK instruction"; 4391 return false; 4392 } 4393 } else { 4394 if (!isInt<16>(Imm)) { 4395 ErrInfo = "invalid immediate for SOPK instruction"; 4396 return false; 4397 } 4398 } 4399 } 4400 } 4401 4402 if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 || 4403 Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 || 4404 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 4405 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) { 4406 const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 4407 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64; 4408 4409 const unsigned StaticNumOps = Desc.getNumOperands() + 4410 Desc.getNumImplicitUses(); 4411 const unsigned NumImplicitOps = IsDst ? 2 : 1; 4412 4413 // Allow additional implicit operands. This allows a fixup done by the post 4414 // RA scheduler where the main implicit operand is killed and implicit-defs 4415 // are added for sub-registers that remain live after this instruction. 4416 if (MI.getNumOperands() < StaticNumOps + NumImplicitOps) { 4417 ErrInfo = "missing implicit register operands"; 4418 return false; 4419 } 4420 4421 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 4422 if (IsDst) { 4423 if (!Dst->isUse()) { 4424 ErrInfo = "v_movreld_b32 vdst should be a use operand"; 4425 return false; 4426 } 4427 4428 unsigned UseOpIdx; 4429 if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) || 4430 UseOpIdx != StaticNumOps + 1) { 4431 ErrInfo = "movrel implicit operands should be tied"; 4432 return false; 4433 } 4434 } 4435 4436 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 4437 const MachineOperand &ImpUse 4438 = MI.getOperand(StaticNumOps + NumImplicitOps - 1); 4439 if (!ImpUse.isReg() || !ImpUse.isUse() || 4440 !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) { 4441 ErrInfo = "src0 should be subreg of implicit vector use"; 4442 return false; 4443 } 4444 } 4445 4446 // Make sure we aren't losing exec uses in the td files. This mostly requires 4447 // being careful when using let Uses to try to add other use registers. 4448 if (shouldReadExec(MI)) { 4449 if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) { 4450 ErrInfo = "VALU instruction does not implicitly read exec mask"; 4451 return false; 4452 } 4453 } 4454 4455 if (isSMRD(MI)) { 4456 if (MI.mayStore()) { 4457 // The register offset form of scalar stores may only use m0 as the 4458 // soffset register. 4459 const MachineOperand *Soff = getNamedOperand(MI, AMDGPU::OpName::soffset); 4460 if (Soff && Soff->getReg() != AMDGPU::M0) { 4461 ErrInfo = "scalar stores must use m0 as offset register"; 4462 return false; 4463 } 4464 } 4465 } 4466 4467 if (isFLAT(MI) && !ST.hasFlatInstOffsets()) { 4468 const MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 4469 if (Offset->getImm() != 0) { 4470 ErrInfo = "subtarget does not support offsets in flat instructions"; 4471 return false; 4472 } 4473 } 4474 4475 if (isMIMG(MI)) { 4476 const MachineOperand *DimOp = getNamedOperand(MI, AMDGPU::OpName::dim); 4477 if (DimOp) { 4478 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opcode, 4479 AMDGPU::OpName::vaddr0); 4480 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); 4481 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(Opcode); 4482 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode = 4483 AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode); 4484 const AMDGPU::MIMGDimInfo *Dim = 4485 AMDGPU::getMIMGDimInfoByEncoding(DimOp->getImm()); 4486 4487 if (!Dim) { 4488 ErrInfo = "dim is out of range"; 4489 return false; 4490 } 4491 4492 bool IsA16 = false; 4493 if (ST.hasR128A16()) { 4494 const MachineOperand *R128A16 = getNamedOperand(MI, AMDGPU::OpName::r128); 4495 IsA16 = R128A16->getImm() != 0; 4496 } else if (ST.hasGFX10A16()) { 4497 const MachineOperand *A16 = getNamedOperand(MI, AMDGPU::OpName::a16); 4498 IsA16 = A16->getImm() != 0; 4499 } 4500 4501 bool IsNSA = SRsrcIdx - VAddr0Idx > 1; 4502 4503 unsigned AddrWords = 4504 AMDGPU::getAddrSizeMIMGOp(BaseOpcode, Dim, IsA16, ST.hasG16()); 4505 4506 unsigned VAddrWords; 4507 if (IsNSA) { 4508 VAddrWords = SRsrcIdx - VAddr0Idx; 4509 } else { 4510 const TargetRegisterClass *RC = getOpRegClass(MI, VAddr0Idx); 4511 VAddrWords = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC) / 32; 4512 if (AddrWords > 8) 4513 AddrWords = 16; 4514 } 4515 4516 if (VAddrWords != AddrWords) { 4517 LLVM_DEBUG(dbgs() << "bad vaddr size, expected " << AddrWords 4518 << " but got " << VAddrWords << "\n"); 4519 ErrInfo = "bad vaddr size"; 4520 return false; 4521 } 4522 } 4523 } 4524 4525 const MachineOperand *DppCt = getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl); 4526 if (DppCt) { 4527 using namespace AMDGPU::DPP; 4528 4529 unsigned DC = DppCt->getImm(); 4530 if (DC == DppCtrl::DPP_UNUSED1 || DC == DppCtrl::DPP_UNUSED2 || 4531 DC == DppCtrl::DPP_UNUSED3 || DC > DppCtrl::DPP_LAST || 4532 (DC >= DppCtrl::DPP_UNUSED4_FIRST && DC <= DppCtrl::DPP_UNUSED4_LAST) || 4533 (DC >= DppCtrl::DPP_UNUSED5_FIRST && DC <= DppCtrl::DPP_UNUSED5_LAST) || 4534 (DC >= DppCtrl::DPP_UNUSED6_FIRST && DC <= DppCtrl::DPP_UNUSED6_LAST) || 4535 (DC >= DppCtrl::DPP_UNUSED7_FIRST && DC <= DppCtrl::DPP_UNUSED7_LAST) || 4536 (DC >= DppCtrl::DPP_UNUSED8_FIRST && DC <= DppCtrl::DPP_UNUSED8_LAST)) { 4537 ErrInfo = "Invalid dpp_ctrl value"; 4538 return false; 4539 } 4540 if (DC >= DppCtrl::WAVE_SHL1 && DC <= DppCtrl::WAVE_ROR1 && 4541 ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 4542 ErrInfo = "Invalid dpp_ctrl value: " 4543 "wavefront shifts are not supported on GFX10+"; 4544 return false; 4545 } 4546 if (DC >= DppCtrl::BCAST15 && DC <= DppCtrl::BCAST31 && 4547 ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 4548 ErrInfo = "Invalid dpp_ctrl value: " 4549 "broadcasts are not supported on GFX10+"; 4550 return false; 4551 } 4552 if (DC >= DppCtrl::ROW_SHARE_FIRST && DC <= DppCtrl::ROW_XMASK_LAST && 4553 ST.getGeneration() < AMDGPUSubtarget::GFX10) { 4554 if (DC >= DppCtrl::ROW_NEWBCAST_FIRST && 4555 DC <= DppCtrl::ROW_NEWBCAST_LAST && 4556 !ST.hasGFX90AInsts()) { 4557 ErrInfo = "Invalid dpp_ctrl value: " 4558 "row_newbroadcast/row_share is not supported before " 4559 "GFX90A/GFX10"; 4560 return false; 4561 } else if (DC > DppCtrl::ROW_NEWBCAST_LAST || !ST.hasGFX90AInsts()) { 4562 ErrInfo = "Invalid dpp_ctrl value: " 4563 "row_share and row_xmask are not supported before GFX10"; 4564 return false; 4565 } 4566 } 4567 4568 int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst); 4569 4570 if (Opcode != AMDGPU::V_MOV_B64_DPP_PSEUDO && 4571 ((DstIdx >= 0 && 4572 (Desc.OpInfo[DstIdx].RegClass == AMDGPU::VReg_64RegClassID || 4573 Desc.OpInfo[DstIdx].RegClass == AMDGPU::VReg_64_Align2RegClassID)) || 4574 ((Src0Idx >= 0 && 4575 (Desc.OpInfo[Src0Idx].RegClass == AMDGPU::VReg_64RegClassID || 4576 Desc.OpInfo[Src0Idx].RegClass == 4577 AMDGPU::VReg_64_Align2RegClassID)))) && 4578 !AMDGPU::isLegal64BitDPPControl(DC)) { 4579 ErrInfo = "Invalid dpp_ctrl value: " 4580 "64 bit dpp only support row_newbcast"; 4581 return false; 4582 } 4583 } 4584 4585 if ((MI.mayStore() || MI.mayLoad()) && !isVGPRSpill(MI)) { 4586 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 4587 uint16_t DataNameIdx = isDS(Opcode) ? AMDGPU::OpName::data0 4588 : AMDGPU::OpName::vdata; 4589 const MachineOperand *Data = getNamedOperand(MI, DataNameIdx); 4590 const MachineOperand *Data2 = getNamedOperand(MI, AMDGPU::OpName::data1); 4591 if (Data && !Data->isReg()) 4592 Data = nullptr; 4593 4594 if (ST.hasGFX90AInsts()) { 4595 if (Dst && Data && 4596 (RI.isAGPR(MRI, Dst->getReg()) != RI.isAGPR(MRI, Data->getReg()))) { 4597 ErrInfo = "Invalid register class: " 4598 "vdata and vdst should be both VGPR or AGPR"; 4599 return false; 4600 } 4601 if (Data && Data2 && 4602 (RI.isAGPR(MRI, Data->getReg()) != RI.isAGPR(MRI, Data2->getReg()))) { 4603 ErrInfo = "Invalid register class: " 4604 "both data operands should be VGPR or AGPR"; 4605 return false; 4606 } 4607 } else { 4608 if ((Dst && RI.isAGPR(MRI, Dst->getReg())) || 4609 (Data && RI.isAGPR(MRI, Data->getReg())) || 4610 (Data2 && RI.isAGPR(MRI, Data2->getReg()))) { 4611 ErrInfo = "Invalid register class: " 4612 "agpr loads and stores not supported on this GPU"; 4613 return false; 4614 } 4615 } 4616 } 4617 4618 if (ST.needsAlignedVGPRs() && 4619 (MI.getOpcode() == AMDGPU::DS_GWS_INIT || 4620 MI.getOpcode() == AMDGPU::DS_GWS_SEMA_BR || 4621 MI.getOpcode() == AMDGPU::DS_GWS_BARRIER)) { 4622 const MachineOperand *Op = getNamedOperand(MI, AMDGPU::OpName::data0); 4623 Register Reg = Op->getReg(); 4624 bool Aligned = true; 4625 if (Reg.isPhysical()) { 4626 Aligned = !(RI.getHWRegIndex(Reg) & 1); 4627 } else { 4628 const TargetRegisterClass &RC = *MRI.getRegClass(Reg); 4629 Aligned = RI.getRegSizeInBits(RC) > 32 && RI.isProperlyAlignedRC(RC) && 4630 !(RI.getChannelFromSubReg(Op->getSubReg()) & 1); 4631 } 4632 4633 if (!Aligned) { 4634 ErrInfo = "Subtarget requires even aligned vector registers " 4635 "for DS_GWS instructions"; 4636 return false; 4637 } 4638 } 4639 4640 if (MI.getOpcode() == AMDGPU::V_ACCVGPR_WRITE_B32_e64 && 4641 !ST.hasGFX90AInsts()) { 4642 const MachineOperand *Src = getNamedOperand(MI, AMDGPU::OpName::src0); 4643 if (Src->isReg() && RI.isSGPRReg(MRI, Src->getReg())) { 4644 ErrInfo = "Invalid register class: " 4645 "v_accvgpr_write with an SGPR is not supported on this GPU"; 4646 return false; 4647 } 4648 } 4649 4650 if (Desc.getOpcode() == AMDGPU::G_AMDGPU_WAVE_ADDRESS) { 4651 const MachineOperand &SrcOp = MI.getOperand(1); 4652 if (!SrcOp.isReg() || SrcOp.getReg().isVirtual()) { 4653 ErrInfo = "pseudo expects only physical SGPRs"; 4654 return false; 4655 } 4656 } 4657 4658 return true; 4659 } 4660 4661 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) const { 4662 switch (MI.getOpcode()) { 4663 default: return AMDGPU::INSTRUCTION_LIST_END; 4664 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE; 4665 case AMDGPU::COPY: return AMDGPU::COPY; 4666 case AMDGPU::PHI: return AMDGPU::PHI; 4667 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG; 4668 case AMDGPU::WQM: return AMDGPU::WQM; 4669 case AMDGPU::SOFT_WQM: return AMDGPU::SOFT_WQM; 4670 case AMDGPU::STRICT_WWM: return AMDGPU::STRICT_WWM; 4671 case AMDGPU::STRICT_WQM: return AMDGPU::STRICT_WQM; 4672 case AMDGPU::S_MOV_B32: { 4673 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 4674 return MI.getOperand(1).isReg() || 4675 RI.isAGPR(MRI, MI.getOperand(0).getReg()) ? 4676 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32; 4677 } 4678 case AMDGPU::S_ADD_I32: 4679 return ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_CO_U32_e32; 4680 case AMDGPU::S_ADDC_U32: 4681 return AMDGPU::V_ADDC_U32_e32; 4682 case AMDGPU::S_SUB_I32: 4683 return ST.hasAddNoCarry() ? AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_CO_U32_e32; 4684 // FIXME: These are not consistently handled, and selected when the carry is 4685 // used. 4686 case AMDGPU::S_ADD_U32: 4687 return AMDGPU::V_ADD_CO_U32_e32; 4688 case AMDGPU::S_SUB_U32: 4689 return AMDGPU::V_SUB_CO_U32_e32; 4690 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32; 4691 case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_U32_e64; 4692 case AMDGPU::S_MUL_HI_U32: return AMDGPU::V_MUL_HI_U32_e64; 4693 case AMDGPU::S_MUL_HI_I32: return AMDGPU::V_MUL_HI_I32_e64; 4694 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64; 4695 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64; 4696 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64; 4697 case AMDGPU::S_XNOR_B32: 4698 return ST.hasDLInsts() ? AMDGPU::V_XNOR_B32_e64 : AMDGPU::INSTRUCTION_LIST_END; 4699 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64; 4700 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64; 4701 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64; 4702 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64; 4703 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32; 4704 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64_e64; 4705 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32; 4706 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64_e64; 4707 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32; 4708 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64_e64; 4709 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32_e64; 4710 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32_e64; 4711 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32_e64; 4712 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32_e64; 4713 case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64; 4714 case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32; 4715 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32; 4716 case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32; 4717 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e64; 4718 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e64; 4719 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e64; 4720 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e64; 4721 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e64; 4722 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e64; 4723 case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e64; 4724 case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e64; 4725 case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e64; 4726 case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e64; 4727 case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e64; 4728 case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e64; 4729 case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e64; 4730 case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e64; 4731 case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64; 4732 case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32; 4733 case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32; 4734 case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64; 4735 case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ; 4736 case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ; 4737 } 4738 llvm_unreachable( 4739 "Unexpected scalar opcode without corresponding vector one!"); 4740 } 4741 4742 static const TargetRegisterClass * 4743 adjustAllocatableRegClass(const GCNSubtarget &ST, const SIRegisterInfo &RI, 4744 const MachineRegisterInfo &MRI, 4745 const MCInstrDesc &TID, unsigned RCID, 4746 bool IsAllocatable) { 4747 if ((IsAllocatable || !ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) && 4748 (((TID.mayLoad() || TID.mayStore()) && 4749 !(TID.TSFlags & SIInstrFlags::VGPRSpill)) || 4750 (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::MIMG)))) { 4751 switch (RCID) { 4752 case AMDGPU::AV_32RegClassID: 4753 RCID = AMDGPU::VGPR_32RegClassID; 4754 break; 4755 case AMDGPU::AV_64RegClassID: 4756 RCID = AMDGPU::VReg_64RegClassID; 4757 break; 4758 case AMDGPU::AV_96RegClassID: 4759 RCID = AMDGPU::VReg_96RegClassID; 4760 break; 4761 case AMDGPU::AV_128RegClassID: 4762 RCID = AMDGPU::VReg_128RegClassID; 4763 break; 4764 case AMDGPU::AV_160RegClassID: 4765 RCID = AMDGPU::VReg_160RegClassID; 4766 break; 4767 case AMDGPU::AV_512RegClassID: 4768 RCID = AMDGPU::VReg_512RegClassID; 4769 break; 4770 default: 4771 break; 4772 } 4773 } 4774 4775 return RI.getProperlyAlignedRC(RI.getRegClass(RCID)); 4776 } 4777 4778 const TargetRegisterClass *SIInstrInfo::getRegClass(const MCInstrDesc &TID, 4779 unsigned OpNum, const TargetRegisterInfo *TRI, 4780 const MachineFunction &MF) 4781 const { 4782 if (OpNum >= TID.getNumOperands()) 4783 return nullptr; 4784 auto RegClass = TID.OpInfo[OpNum].RegClass; 4785 bool IsAllocatable = false; 4786 if (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::FLAT)) { 4787 // vdst and vdata should be both VGPR or AGPR, same for the DS instructions 4788 // with two data operands. Request register class constrained to VGPR only 4789 // of both operands present as Machine Copy Propagation can not check this 4790 // constraint and possibly other passes too. 4791 // 4792 // The check is limited to FLAT and DS because atomics in non-flat encoding 4793 // have their vdst and vdata tied to be the same register. 4794 const int VDstIdx = AMDGPU::getNamedOperandIdx(TID.Opcode, 4795 AMDGPU::OpName::vdst); 4796 const int DataIdx = AMDGPU::getNamedOperandIdx(TID.Opcode, 4797 (TID.TSFlags & SIInstrFlags::DS) ? AMDGPU::OpName::data0 4798 : AMDGPU::OpName::vdata); 4799 if (DataIdx != -1) { 4800 IsAllocatable = VDstIdx != -1 || 4801 AMDGPU::getNamedOperandIdx(TID.Opcode, 4802 AMDGPU::OpName::data1) != -1; 4803 } 4804 } 4805 return adjustAllocatableRegClass(ST, RI, MF.getRegInfo(), TID, RegClass, 4806 IsAllocatable); 4807 } 4808 4809 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI, 4810 unsigned OpNo) const { 4811 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 4812 const MCInstrDesc &Desc = get(MI.getOpcode()); 4813 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() || 4814 Desc.OpInfo[OpNo].RegClass == -1) { 4815 Register Reg = MI.getOperand(OpNo).getReg(); 4816 4817 if (Reg.isVirtual()) 4818 return MRI.getRegClass(Reg); 4819 return RI.getPhysRegClass(Reg); 4820 } 4821 4822 unsigned RCID = Desc.OpInfo[OpNo].RegClass; 4823 return adjustAllocatableRegClass(ST, RI, MRI, Desc, RCID, true); 4824 } 4825 4826 void SIInstrInfo::legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const { 4827 MachineBasicBlock::iterator I = MI; 4828 MachineBasicBlock *MBB = MI.getParent(); 4829 MachineOperand &MO = MI.getOperand(OpIdx); 4830 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 4831 unsigned RCID = get(MI.getOpcode()).OpInfo[OpIdx].RegClass; 4832 const TargetRegisterClass *RC = RI.getRegClass(RCID); 4833 unsigned Size = RI.getRegSizeInBits(*RC); 4834 unsigned Opcode = (Size == 64) ? AMDGPU::V_MOV_B64_PSEUDO : AMDGPU::V_MOV_B32_e32; 4835 if (MO.isReg()) 4836 Opcode = AMDGPU::COPY; 4837 else if (RI.isSGPRClass(RC)) 4838 Opcode = (Size == 64) ? AMDGPU::S_MOV_B64 : AMDGPU::S_MOV_B32; 4839 4840 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC); 4841 const TargetRegisterClass *VRC64 = RI.getVGPR64Class(); 4842 if (RI.getCommonSubClass(VRC64, VRC)) 4843 VRC = VRC64; 4844 else 4845 VRC = &AMDGPU::VGPR_32RegClass; 4846 4847 Register Reg = MRI.createVirtualRegister(VRC); 4848 DebugLoc DL = MBB->findDebugLoc(I); 4849 BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).add(MO); 4850 MO.ChangeToRegister(Reg, false); 4851 } 4852 4853 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI, 4854 MachineRegisterInfo &MRI, 4855 MachineOperand &SuperReg, 4856 const TargetRegisterClass *SuperRC, 4857 unsigned SubIdx, 4858 const TargetRegisterClass *SubRC) 4859 const { 4860 MachineBasicBlock *MBB = MI->getParent(); 4861 DebugLoc DL = MI->getDebugLoc(); 4862 Register SubReg = MRI.createVirtualRegister(SubRC); 4863 4864 if (SuperReg.getSubReg() == AMDGPU::NoSubRegister) { 4865 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 4866 .addReg(SuperReg.getReg(), 0, SubIdx); 4867 return SubReg; 4868 } 4869 4870 // Just in case the super register is itself a sub-register, copy it to a new 4871 // value so we don't need to worry about merging its subreg index with the 4872 // SubIdx passed to this function. The register coalescer should be able to 4873 // eliminate this extra copy. 4874 Register NewSuperReg = MRI.createVirtualRegister(SuperRC); 4875 4876 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg) 4877 .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg()); 4878 4879 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 4880 .addReg(NewSuperReg, 0, SubIdx); 4881 4882 return SubReg; 4883 } 4884 4885 MachineOperand SIInstrInfo::buildExtractSubRegOrImm( 4886 MachineBasicBlock::iterator MII, 4887 MachineRegisterInfo &MRI, 4888 MachineOperand &Op, 4889 const TargetRegisterClass *SuperRC, 4890 unsigned SubIdx, 4891 const TargetRegisterClass *SubRC) const { 4892 if (Op.isImm()) { 4893 if (SubIdx == AMDGPU::sub0) 4894 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm())); 4895 if (SubIdx == AMDGPU::sub1) 4896 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32)); 4897 4898 llvm_unreachable("Unhandled register index for immediate"); 4899 } 4900 4901 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC, 4902 SubIdx, SubRC); 4903 return MachineOperand::CreateReg(SubReg, false); 4904 } 4905 4906 // Change the order of operands from (0, 1, 2) to (0, 2, 1) 4907 void SIInstrInfo::swapOperands(MachineInstr &Inst) const { 4908 assert(Inst.getNumExplicitOperands() == 3); 4909 MachineOperand Op1 = Inst.getOperand(1); 4910 Inst.removeOperand(1); 4911 Inst.addOperand(Op1); 4912 } 4913 4914 bool SIInstrInfo::isLegalRegOperand(const MachineRegisterInfo &MRI, 4915 const MCOperandInfo &OpInfo, 4916 const MachineOperand &MO) const { 4917 if (!MO.isReg()) 4918 return false; 4919 4920 Register Reg = MO.getReg(); 4921 4922 const TargetRegisterClass *DRC = RI.getRegClass(OpInfo.RegClass); 4923 if (Reg.isPhysical()) 4924 return DRC->contains(Reg); 4925 4926 const TargetRegisterClass *RC = MRI.getRegClass(Reg); 4927 4928 if (MO.getSubReg()) { 4929 const MachineFunction *MF = MO.getParent()->getParent()->getParent(); 4930 const TargetRegisterClass *SuperRC = RI.getLargestLegalSuperClass(RC, *MF); 4931 if (!SuperRC) 4932 return false; 4933 4934 DRC = RI.getMatchingSuperRegClass(SuperRC, DRC, MO.getSubReg()); 4935 if (!DRC) 4936 return false; 4937 } 4938 return RC->hasSuperClassEq(DRC); 4939 } 4940 4941 bool SIInstrInfo::isLegalVSrcOperand(const MachineRegisterInfo &MRI, 4942 const MCOperandInfo &OpInfo, 4943 const MachineOperand &MO) const { 4944 if (MO.isReg()) 4945 return isLegalRegOperand(MRI, OpInfo, MO); 4946 4947 // Handle non-register types that are treated like immediates. 4948 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal()); 4949 return true; 4950 } 4951 4952 bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx, 4953 const MachineOperand *MO) const { 4954 const MachineFunction &MF = *MI.getParent()->getParent(); 4955 const MachineRegisterInfo &MRI = MF.getRegInfo(); 4956 const MCInstrDesc &InstDesc = MI.getDesc(); 4957 const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx]; 4958 const TargetRegisterClass *DefinedRC = 4959 OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr; 4960 if (!MO) 4961 MO = &MI.getOperand(OpIdx); 4962 4963 int ConstantBusLimit = ST.getConstantBusLimit(MI.getOpcode()); 4964 int VOP3LiteralLimit = ST.hasVOP3Literal() ? 1 : 0; 4965 if (isVALU(MI) && usesConstantBus(MRI, *MO, OpInfo)) { 4966 if (isVOP3(MI) && isLiteralConstantLike(*MO, OpInfo) && !VOP3LiteralLimit--) 4967 return false; 4968 4969 SmallDenseSet<RegSubRegPair> SGPRsUsed; 4970 if (MO->isReg()) 4971 SGPRsUsed.insert(RegSubRegPair(MO->getReg(), MO->getSubReg())); 4972 4973 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 4974 if (i == OpIdx) 4975 continue; 4976 const MachineOperand &Op = MI.getOperand(i); 4977 if (Op.isReg()) { 4978 RegSubRegPair SGPR(Op.getReg(), Op.getSubReg()); 4979 if (!SGPRsUsed.count(SGPR) && 4980 usesConstantBus(MRI, Op, InstDesc.OpInfo[i])) { 4981 if (--ConstantBusLimit <= 0) 4982 return false; 4983 SGPRsUsed.insert(SGPR); 4984 } 4985 } else if (InstDesc.OpInfo[i].OperandType == AMDGPU::OPERAND_KIMM32) { 4986 if (--ConstantBusLimit <= 0) 4987 return false; 4988 } else if (isVOP3(MI) && AMDGPU::isSISrcOperand(InstDesc, i) && 4989 isLiteralConstantLike(Op, InstDesc.OpInfo[i])) { 4990 if (!VOP3LiteralLimit--) 4991 return false; 4992 if (--ConstantBusLimit <= 0) 4993 return false; 4994 } 4995 } 4996 } 4997 4998 if (MO->isReg()) { 4999 if (!DefinedRC) { 5000 // This operand allows any register. 5001 return true; 5002 } 5003 if (!isLegalRegOperand(MRI, OpInfo, *MO)) 5004 return false; 5005 bool IsAGPR = RI.isAGPR(MRI, MO->getReg()); 5006 if (IsAGPR && !ST.hasMAIInsts()) 5007 return false; 5008 unsigned Opc = MI.getOpcode(); 5009 if (IsAGPR && 5010 (!ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) && 5011 (MI.mayLoad() || MI.mayStore() || isDS(Opc) || isMIMG(Opc))) 5012 return false; 5013 // Atomics should have both vdst and vdata either vgpr or agpr. 5014 const int VDstIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 5015 const int DataIdx = AMDGPU::getNamedOperandIdx(Opc, 5016 isDS(Opc) ? AMDGPU::OpName::data0 : AMDGPU::OpName::vdata); 5017 if ((int)OpIdx == VDstIdx && DataIdx != -1 && 5018 MI.getOperand(DataIdx).isReg() && 5019 RI.isAGPR(MRI, MI.getOperand(DataIdx).getReg()) != IsAGPR) 5020 return false; 5021 if ((int)OpIdx == DataIdx) { 5022 if (VDstIdx != -1 && 5023 RI.isAGPR(MRI, MI.getOperand(VDstIdx).getReg()) != IsAGPR) 5024 return false; 5025 // DS instructions with 2 src operands also must have tied RC. 5026 const int Data1Idx = AMDGPU::getNamedOperandIdx(Opc, 5027 AMDGPU::OpName::data1); 5028 if (Data1Idx != -1 && MI.getOperand(Data1Idx).isReg() && 5029 RI.isAGPR(MRI, MI.getOperand(Data1Idx).getReg()) != IsAGPR) 5030 return false; 5031 } 5032 if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64 && !ST.hasGFX90AInsts() && 5033 (int)OpIdx == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) && 5034 RI.isSGPRReg(MRI, MO->getReg())) 5035 return false; 5036 return true; 5037 } 5038 5039 // Handle non-register types that are treated like immediates. 5040 assert(MO->isImm() || MO->isTargetIndex() || MO->isFI() || MO->isGlobal()); 5041 5042 if (!DefinedRC) { 5043 // This operand expects an immediate. 5044 return true; 5045 } 5046 5047 return isImmOperandLegal(MI, OpIdx, *MO); 5048 } 5049 5050 void SIInstrInfo::legalizeOperandsVOP2(MachineRegisterInfo &MRI, 5051 MachineInstr &MI) const { 5052 unsigned Opc = MI.getOpcode(); 5053 const MCInstrDesc &InstrDesc = get(Opc); 5054 5055 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 5056 MachineOperand &Src0 = MI.getOperand(Src0Idx); 5057 5058 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 5059 MachineOperand &Src1 = MI.getOperand(Src1Idx); 5060 5061 // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32 5062 // we need to only have one constant bus use before GFX10. 5063 bool HasImplicitSGPR = findImplicitSGPRRead(MI) != AMDGPU::NoRegister; 5064 if (HasImplicitSGPR && ST.getConstantBusLimit(Opc) <= 1 && 5065 Src0.isReg() && (RI.isSGPRReg(MRI, Src0.getReg()) || 5066 isLiteralConstantLike(Src0, InstrDesc.OpInfo[Src0Idx]))) 5067 legalizeOpWithMove(MI, Src0Idx); 5068 5069 // Special case: V_WRITELANE_B32 accepts only immediate or SGPR operands for 5070 // both the value to write (src0) and lane select (src1). Fix up non-SGPR 5071 // src0/src1 with V_READFIRSTLANE. 5072 if (Opc == AMDGPU::V_WRITELANE_B32) { 5073 const DebugLoc &DL = MI.getDebugLoc(); 5074 if (Src0.isReg() && RI.isVGPR(MRI, Src0.getReg())) { 5075 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5076 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5077 .add(Src0); 5078 Src0.ChangeToRegister(Reg, false); 5079 } 5080 if (Src1.isReg() && RI.isVGPR(MRI, Src1.getReg())) { 5081 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5082 const DebugLoc &DL = MI.getDebugLoc(); 5083 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5084 .add(Src1); 5085 Src1.ChangeToRegister(Reg, false); 5086 } 5087 return; 5088 } 5089 5090 // No VOP2 instructions support AGPRs. 5091 if (Src0.isReg() && RI.isAGPR(MRI, Src0.getReg())) 5092 legalizeOpWithMove(MI, Src0Idx); 5093 5094 if (Src1.isReg() && RI.isAGPR(MRI, Src1.getReg())) 5095 legalizeOpWithMove(MI, Src1Idx); 5096 5097 // VOP2 src0 instructions support all operand types, so we don't need to check 5098 // their legality. If src1 is already legal, we don't need to do anything. 5099 if (isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src1)) 5100 return; 5101 5102 // Special case: V_READLANE_B32 accepts only immediate or SGPR operands for 5103 // lane select. Fix up using V_READFIRSTLANE, since we assume that the lane 5104 // select is uniform. 5105 if (Opc == AMDGPU::V_READLANE_B32 && Src1.isReg() && 5106 RI.isVGPR(MRI, Src1.getReg())) { 5107 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5108 const DebugLoc &DL = MI.getDebugLoc(); 5109 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5110 .add(Src1); 5111 Src1.ChangeToRegister(Reg, false); 5112 return; 5113 } 5114 5115 // We do not use commuteInstruction here because it is too aggressive and will 5116 // commute if it is possible. We only want to commute here if it improves 5117 // legality. This can be called a fairly large number of times so don't waste 5118 // compile time pointlessly swapping and checking legality again. 5119 if (HasImplicitSGPR || !MI.isCommutable()) { 5120 legalizeOpWithMove(MI, Src1Idx); 5121 return; 5122 } 5123 5124 // If src0 can be used as src1, commuting will make the operands legal. 5125 // Otherwise we have to give up and insert a move. 5126 // 5127 // TODO: Other immediate-like operand kinds could be commuted if there was a 5128 // MachineOperand::ChangeTo* for them. 5129 if ((!Src1.isImm() && !Src1.isReg()) || 5130 !isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src0)) { 5131 legalizeOpWithMove(MI, Src1Idx); 5132 return; 5133 } 5134 5135 int CommutedOpc = commuteOpcode(MI); 5136 if (CommutedOpc == -1) { 5137 legalizeOpWithMove(MI, Src1Idx); 5138 return; 5139 } 5140 5141 MI.setDesc(get(CommutedOpc)); 5142 5143 Register Src0Reg = Src0.getReg(); 5144 unsigned Src0SubReg = Src0.getSubReg(); 5145 bool Src0Kill = Src0.isKill(); 5146 5147 if (Src1.isImm()) 5148 Src0.ChangeToImmediate(Src1.getImm()); 5149 else if (Src1.isReg()) { 5150 Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill()); 5151 Src0.setSubReg(Src1.getSubReg()); 5152 } else 5153 llvm_unreachable("Should only have register or immediate operands"); 5154 5155 Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill); 5156 Src1.setSubReg(Src0SubReg); 5157 fixImplicitOperands(MI); 5158 } 5159 5160 // Legalize VOP3 operands. All operand types are supported for any operand 5161 // but only one literal constant and only starting from GFX10. 5162 void SIInstrInfo::legalizeOperandsVOP3(MachineRegisterInfo &MRI, 5163 MachineInstr &MI) const { 5164 unsigned Opc = MI.getOpcode(); 5165 5166 int VOP3Idx[3] = { 5167 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0), 5168 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1), 5169 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2) 5170 }; 5171 5172 if (Opc == AMDGPU::V_PERMLANE16_B32_e64 || 5173 Opc == AMDGPU::V_PERMLANEX16_B32_e64) { 5174 // src1 and src2 must be scalar 5175 MachineOperand &Src1 = MI.getOperand(VOP3Idx[1]); 5176 MachineOperand &Src2 = MI.getOperand(VOP3Idx[2]); 5177 const DebugLoc &DL = MI.getDebugLoc(); 5178 if (Src1.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) { 5179 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5180 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5181 .add(Src1); 5182 Src1.ChangeToRegister(Reg, false); 5183 } 5184 if (Src2.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src2.getReg()))) { 5185 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5186 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5187 .add(Src2); 5188 Src2.ChangeToRegister(Reg, false); 5189 } 5190 } 5191 5192 // Find the one SGPR operand we are allowed to use. 5193 int ConstantBusLimit = ST.getConstantBusLimit(Opc); 5194 int LiteralLimit = ST.hasVOP3Literal() ? 1 : 0; 5195 SmallDenseSet<unsigned> SGPRsUsed; 5196 Register SGPRReg = findUsedSGPR(MI, VOP3Idx); 5197 if (SGPRReg != AMDGPU::NoRegister) { 5198 SGPRsUsed.insert(SGPRReg); 5199 --ConstantBusLimit; 5200 } 5201 5202 for (int Idx : VOP3Idx) { 5203 if (Idx == -1) 5204 break; 5205 MachineOperand &MO = MI.getOperand(Idx); 5206 5207 if (!MO.isReg()) { 5208 if (!isLiteralConstantLike(MO, get(Opc).OpInfo[Idx])) 5209 continue; 5210 5211 if (LiteralLimit > 0 && ConstantBusLimit > 0) { 5212 --LiteralLimit; 5213 --ConstantBusLimit; 5214 continue; 5215 } 5216 5217 --LiteralLimit; 5218 --ConstantBusLimit; 5219 legalizeOpWithMove(MI, Idx); 5220 continue; 5221 } 5222 5223 if (RI.hasAGPRs(RI.getRegClassForReg(MRI, MO.getReg())) && 5224 !isOperandLegal(MI, Idx, &MO)) { 5225 legalizeOpWithMove(MI, Idx); 5226 continue; 5227 } 5228 5229 if (!RI.isSGPRClass(RI.getRegClassForReg(MRI, MO.getReg()))) 5230 continue; // VGPRs are legal 5231 5232 // We can use one SGPR in each VOP3 instruction prior to GFX10 5233 // and two starting from GFX10. 5234 if (SGPRsUsed.count(MO.getReg())) 5235 continue; 5236 if (ConstantBusLimit > 0) { 5237 SGPRsUsed.insert(MO.getReg()); 5238 --ConstantBusLimit; 5239 continue; 5240 } 5241 5242 // If we make it this far, then the operand is not legal and we must 5243 // legalize it. 5244 legalizeOpWithMove(MI, Idx); 5245 } 5246 } 5247 5248 Register SIInstrInfo::readlaneVGPRToSGPR(Register SrcReg, MachineInstr &UseMI, 5249 MachineRegisterInfo &MRI) const { 5250 const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg); 5251 const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC); 5252 Register DstReg = MRI.createVirtualRegister(SRC); 5253 unsigned SubRegs = RI.getRegSizeInBits(*VRC) / 32; 5254 5255 if (RI.hasAGPRs(VRC)) { 5256 VRC = RI.getEquivalentVGPRClass(VRC); 5257 Register NewSrcReg = MRI.createVirtualRegister(VRC); 5258 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5259 get(TargetOpcode::COPY), NewSrcReg) 5260 .addReg(SrcReg); 5261 SrcReg = NewSrcReg; 5262 } 5263 5264 if (SubRegs == 1) { 5265 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5266 get(AMDGPU::V_READFIRSTLANE_B32), DstReg) 5267 .addReg(SrcReg); 5268 return DstReg; 5269 } 5270 5271 SmallVector<unsigned, 8> SRegs; 5272 for (unsigned i = 0; i < SubRegs; ++i) { 5273 Register SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5274 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5275 get(AMDGPU::V_READFIRSTLANE_B32), SGPR) 5276 .addReg(SrcReg, 0, RI.getSubRegFromChannel(i)); 5277 SRegs.push_back(SGPR); 5278 } 5279 5280 MachineInstrBuilder MIB = 5281 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5282 get(AMDGPU::REG_SEQUENCE), DstReg); 5283 for (unsigned i = 0; i < SubRegs; ++i) { 5284 MIB.addReg(SRegs[i]); 5285 MIB.addImm(RI.getSubRegFromChannel(i)); 5286 } 5287 return DstReg; 5288 } 5289 5290 void SIInstrInfo::legalizeOperandsSMRD(MachineRegisterInfo &MRI, 5291 MachineInstr &MI) const { 5292 5293 // If the pointer is store in VGPRs, then we need to move them to 5294 // SGPRs using v_readfirstlane. This is safe because we only select 5295 // loads with uniform pointers to SMRD instruction so we know the 5296 // pointer value is uniform. 5297 MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase); 5298 if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) { 5299 Register SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI); 5300 SBase->setReg(SGPR); 5301 } 5302 MachineOperand *SOff = getNamedOperand(MI, AMDGPU::OpName::soffset); 5303 if (SOff && !RI.isSGPRClass(MRI.getRegClass(SOff->getReg()))) { 5304 Register SGPR = readlaneVGPRToSGPR(SOff->getReg(), MI, MRI); 5305 SOff->setReg(SGPR); 5306 } 5307 } 5308 5309 bool SIInstrInfo::moveFlatAddrToVGPR(MachineInstr &Inst) const { 5310 unsigned Opc = Inst.getOpcode(); 5311 int OldSAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::saddr); 5312 if (OldSAddrIdx < 0) 5313 return false; 5314 5315 assert(isSegmentSpecificFLAT(Inst)); 5316 5317 int NewOpc = AMDGPU::getGlobalVaddrOp(Opc); 5318 if (NewOpc < 0) 5319 NewOpc = AMDGPU::getFlatScratchInstSVfromSS(Opc); 5320 if (NewOpc < 0) 5321 return false; 5322 5323 MachineRegisterInfo &MRI = Inst.getMF()->getRegInfo(); 5324 MachineOperand &SAddr = Inst.getOperand(OldSAddrIdx); 5325 if (RI.isSGPRReg(MRI, SAddr.getReg())) 5326 return false; 5327 5328 int NewVAddrIdx = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vaddr); 5329 if (NewVAddrIdx < 0) 5330 return false; 5331 5332 int OldVAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr); 5333 5334 // Check vaddr, it shall be zero or absent. 5335 MachineInstr *VAddrDef = nullptr; 5336 if (OldVAddrIdx >= 0) { 5337 MachineOperand &VAddr = Inst.getOperand(OldVAddrIdx); 5338 VAddrDef = MRI.getUniqueVRegDef(VAddr.getReg()); 5339 if (!VAddrDef || VAddrDef->getOpcode() != AMDGPU::V_MOV_B32_e32 || 5340 !VAddrDef->getOperand(1).isImm() || 5341 VAddrDef->getOperand(1).getImm() != 0) 5342 return false; 5343 } 5344 5345 const MCInstrDesc &NewDesc = get(NewOpc); 5346 Inst.setDesc(NewDesc); 5347 5348 // Callers expect iterator to be valid after this call, so modify the 5349 // instruction in place. 5350 if (OldVAddrIdx == NewVAddrIdx) { 5351 MachineOperand &NewVAddr = Inst.getOperand(NewVAddrIdx); 5352 // Clear use list from the old vaddr holding a zero register. 5353 MRI.removeRegOperandFromUseList(&NewVAddr); 5354 MRI.moveOperands(&NewVAddr, &SAddr, 1); 5355 Inst.removeOperand(OldSAddrIdx); 5356 // Update the use list with the pointer we have just moved from vaddr to 5357 // saddr position. Otherwise new vaddr will be missing from the use list. 5358 MRI.removeRegOperandFromUseList(&NewVAddr); 5359 MRI.addRegOperandToUseList(&NewVAddr); 5360 } else { 5361 assert(OldSAddrIdx == NewVAddrIdx); 5362 5363 if (OldVAddrIdx >= 0) { 5364 int NewVDstIn = AMDGPU::getNamedOperandIdx(NewOpc, 5365 AMDGPU::OpName::vdst_in); 5366 5367 // removeOperand doesn't try to fixup tied operand indexes at it goes, so 5368 // it asserts. Untie the operands for now and retie them afterwards. 5369 if (NewVDstIn != -1) { 5370 int OldVDstIn = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst_in); 5371 Inst.untieRegOperand(OldVDstIn); 5372 } 5373 5374 Inst.removeOperand(OldVAddrIdx); 5375 5376 if (NewVDstIn != -1) { 5377 int NewVDst = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vdst); 5378 Inst.tieOperands(NewVDst, NewVDstIn); 5379 } 5380 } 5381 } 5382 5383 if (VAddrDef && MRI.use_nodbg_empty(VAddrDef->getOperand(0).getReg())) 5384 VAddrDef->eraseFromParent(); 5385 5386 return true; 5387 } 5388 5389 // FIXME: Remove this when SelectionDAG is obsoleted. 5390 void SIInstrInfo::legalizeOperandsFLAT(MachineRegisterInfo &MRI, 5391 MachineInstr &MI) const { 5392 if (!isSegmentSpecificFLAT(MI)) 5393 return; 5394 5395 // Fixup SGPR operands in VGPRs. We only select these when the DAG divergence 5396 // thinks they are uniform, so a readfirstlane should be valid. 5397 MachineOperand *SAddr = getNamedOperand(MI, AMDGPU::OpName::saddr); 5398 if (!SAddr || RI.isSGPRClass(MRI.getRegClass(SAddr->getReg()))) 5399 return; 5400 5401 if (moveFlatAddrToVGPR(MI)) 5402 return; 5403 5404 Register ToSGPR = readlaneVGPRToSGPR(SAddr->getReg(), MI, MRI); 5405 SAddr->setReg(ToSGPR); 5406 } 5407 5408 void SIInstrInfo::legalizeGenericOperand(MachineBasicBlock &InsertMBB, 5409 MachineBasicBlock::iterator I, 5410 const TargetRegisterClass *DstRC, 5411 MachineOperand &Op, 5412 MachineRegisterInfo &MRI, 5413 const DebugLoc &DL) const { 5414 Register OpReg = Op.getReg(); 5415 unsigned OpSubReg = Op.getSubReg(); 5416 5417 const TargetRegisterClass *OpRC = RI.getSubClassWithSubReg( 5418 RI.getRegClassForReg(MRI, OpReg), OpSubReg); 5419 5420 // Check if operand is already the correct register class. 5421 if (DstRC == OpRC) 5422 return; 5423 5424 Register DstReg = MRI.createVirtualRegister(DstRC); 5425 auto Copy = BuildMI(InsertMBB, I, DL, get(AMDGPU::COPY), DstReg).add(Op); 5426 5427 Op.setReg(DstReg); 5428 Op.setSubReg(0); 5429 5430 MachineInstr *Def = MRI.getVRegDef(OpReg); 5431 if (!Def) 5432 return; 5433 5434 // Try to eliminate the copy if it is copying an immediate value. 5435 if (Def->isMoveImmediate() && DstRC != &AMDGPU::VReg_1RegClass) 5436 FoldImmediate(*Copy, *Def, OpReg, &MRI); 5437 5438 bool ImpDef = Def->isImplicitDef(); 5439 while (!ImpDef && Def && Def->isCopy()) { 5440 if (Def->getOperand(1).getReg().isPhysical()) 5441 break; 5442 Def = MRI.getUniqueVRegDef(Def->getOperand(1).getReg()); 5443 ImpDef = Def && Def->isImplicitDef(); 5444 } 5445 if (!RI.isSGPRClass(DstRC) && !Copy->readsRegister(AMDGPU::EXEC, &RI) && 5446 !ImpDef) 5447 Copy.addReg(AMDGPU::EXEC, RegState::Implicit); 5448 } 5449 5450 // Emit the actual waterfall loop, executing the wrapped instruction for each 5451 // unique value of \p Rsrc across all lanes. In the best case we execute 1 5452 // iteration, in the worst case we execute 64 (once per lane). 5453 static void 5454 emitLoadSRsrcFromVGPRLoop(const SIInstrInfo &TII, MachineRegisterInfo &MRI, 5455 MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB, 5456 MachineBasicBlock &BodyBB, const DebugLoc &DL, 5457 MachineOperand &Rsrc) { 5458 MachineFunction &MF = *OrigBB.getParent(); 5459 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 5460 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 5461 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 5462 unsigned SaveExecOpc = 5463 ST.isWave32() ? AMDGPU::S_AND_SAVEEXEC_B32 : AMDGPU::S_AND_SAVEEXEC_B64; 5464 unsigned XorTermOpc = 5465 ST.isWave32() ? AMDGPU::S_XOR_B32_term : AMDGPU::S_XOR_B64_term; 5466 unsigned AndOpc = 5467 ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64; 5468 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 5469 5470 MachineBasicBlock::iterator I = LoopBB.begin(); 5471 5472 SmallVector<Register, 8> ReadlanePieces; 5473 Register CondReg = AMDGPU::NoRegister; 5474 5475 Register VRsrc = Rsrc.getReg(); 5476 unsigned VRsrcUndef = getUndefRegState(Rsrc.isUndef()); 5477 5478 unsigned RegSize = TRI->getRegSizeInBits(Rsrc.getReg(), MRI); 5479 unsigned NumSubRegs = RegSize / 32; 5480 assert(NumSubRegs % 2 == 0 && NumSubRegs <= 32 && "Unhandled register size"); 5481 5482 for (unsigned Idx = 0; Idx < NumSubRegs; Idx += 2) { 5483 5484 Register CurRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5485 Register CurRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5486 5487 // Read the next variant <- also loop target. 5488 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegLo) 5489 .addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx)); 5490 5491 // Read the next variant <- also loop target. 5492 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegHi) 5493 .addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx + 1)); 5494 5495 ReadlanePieces.push_back(CurRegLo); 5496 ReadlanePieces.push_back(CurRegHi); 5497 5498 // Comparison is to be done as 64-bit. 5499 Register CurReg = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass); 5500 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), CurReg) 5501 .addReg(CurRegLo) 5502 .addImm(AMDGPU::sub0) 5503 .addReg(CurRegHi) 5504 .addImm(AMDGPU::sub1); 5505 5506 Register NewCondReg = MRI.createVirtualRegister(BoolXExecRC); 5507 auto Cmp = 5508 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64), NewCondReg) 5509 .addReg(CurReg); 5510 if (NumSubRegs <= 2) 5511 Cmp.addReg(VRsrc); 5512 else 5513 Cmp.addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx, 2)); 5514 5515 // Combine the comparison results with AND. 5516 if (CondReg == AMDGPU::NoRegister) // First. 5517 CondReg = NewCondReg; 5518 else { // If not the first, we create an AND. 5519 Register AndReg = MRI.createVirtualRegister(BoolXExecRC); 5520 BuildMI(LoopBB, I, DL, TII.get(AndOpc), AndReg) 5521 .addReg(CondReg) 5522 .addReg(NewCondReg); 5523 CondReg = AndReg; 5524 } 5525 } // End for loop. 5526 5527 auto SRsrcRC = TRI->getEquivalentSGPRClass(MRI.getRegClass(VRsrc)); 5528 Register SRsrc = MRI.createVirtualRegister(SRsrcRC); 5529 5530 // Build scalar Rsrc. 5531 auto Merge = BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), SRsrc); 5532 unsigned Channel = 0; 5533 for (Register Piece : ReadlanePieces) { 5534 Merge.addReg(Piece) 5535 .addImm(TRI->getSubRegFromChannel(Channel++)); 5536 } 5537 5538 // Update Rsrc operand to use the SGPR Rsrc. 5539 Rsrc.setReg(SRsrc); 5540 Rsrc.setIsKill(true); 5541 5542 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC); 5543 MRI.setSimpleHint(SaveExec, CondReg); 5544 5545 // Update EXEC to matching lanes, saving original to SaveExec. 5546 BuildMI(LoopBB, I, DL, TII.get(SaveExecOpc), SaveExec) 5547 .addReg(CondReg, RegState::Kill); 5548 5549 // The original instruction is here; we insert the terminators after it. 5550 I = BodyBB.end(); 5551 5552 // Update EXEC, switch all done bits to 0 and all todo bits to 1. 5553 BuildMI(BodyBB, I, DL, TII.get(XorTermOpc), Exec) 5554 .addReg(Exec) 5555 .addReg(SaveExec); 5556 5557 BuildMI(BodyBB, I, DL, TII.get(AMDGPU::SI_WATERFALL_LOOP)).addMBB(&LoopBB); 5558 } 5559 5560 // Build a waterfall loop around \p MI, replacing the VGPR \p Rsrc register 5561 // with SGPRs by iterating over all unique values across all lanes. 5562 // Returns the loop basic block that now contains \p MI. 5563 static MachineBasicBlock * 5564 loadSRsrcFromVGPR(const SIInstrInfo &TII, MachineInstr &MI, 5565 MachineOperand &Rsrc, MachineDominatorTree *MDT, 5566 MachineBasicBlock::iterator Begin = nullptr, 5567 MachineBasicBlock::iterator End = nullptr) { 5568 MachineBasicBlock &MBB = *MI.getParent(); 5569 MachineFunction &MF = *MBB.getParent(); 5570 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 5571 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 5572 MachineRegisterInfo &MRI = MF.getRegInfo(); 5573 if (!Begin.isValid()) 5574 Begin = &MI; 5575 if (!End.isValid()) { 5576 End = &MI; 5577 ++End; 5578 } 5579 const DebugLoc &DL = MI.getDebugLoc(); 5580 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 5581 unsigned MovExecOpc = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64; 5582 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 5583 5584 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC); 5585 5586 // Save the EXEC mask 5587 BuildMI(MBB, Begin, DL, TII.get(MovExecOpc), SaveExec).addReg(Exec); 5588 5589 // Killed uses in the instruction we are waterfalling around will be 5590 // incorrect due to the added control-flow. 5591 MachineBasicBlock::iterator AfterMI = MI; 5592 ++AfterMI; 5593 for (auto I = Begin; I != AfterMI; I++) { 5594 for (auto &MO : I->uses()) { 5595 if (MO.isReg() && MO.isUse()) { 5596 MRI.clearKillFlags(MO.getReg()); 5597 } 5598 } 5599 } 5600 5601 // To insert the loop we need to split the block. Move everything after this 5602 // point to a new block, and insert a new empty block between the two. 5603 MachineBasicBlock *LoopBB = MF.CreateMachineBasicBlock(); 5604 MachineBasicBlock *BodyBB = MF.CreateMachineBasicBlock(); 5605 MachineBasicBlock *RemainderBB = MF.CreateMachineBasicBlock(); 5606 MachineFunction::iterator MBBI(MBB); 5607 ++MBBI; 5608 5609 MF.insert(MBBI, LoopBB); 5610 MF.insert(MBBI, BodyBB); 5611 MF.insert(MBBI, RemainderBB); 5612 5613 LoopBB->addSuccessor(BodyBB); 5614 BodyBB->addSuccessor(LoopBB); 5615 BodyBB->addSuccessor(RemainderBB); 5616 5617 // Move Begin to MI to the BodyBB, and the remainder of the block to 5618 // RemainderBB. 5619 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); 5620 RemainderBB->splice(RemainderBB->begin(), &MBB, End, MBB.end()); 5621 BodyBB->splice(BodyBB->begin(), &MBB, Begin, MBB.end()); 5622 5623 MBB.addSuccessor(LoopBB); 5624 5625 // Update dominators. We know that MBB immediately dominates LoopBB, that 5626 // LoopBB immediately dominates BodyBB, and BodyBB immediately dominates 5627 // RemainderBB. RemainderBB immediately dominates all of the successors 5628 // transferred to it from MBB that MBB used to properly dominate. 5629 if (MDT) { 5630 MDT->addNewBlock(LoopBB, &MBB); 5631 MDT->addNewBlock(BodyBB, LoopBB); 5632 MDT->addNewBlock(RemainderBB, BodyBB); 5633 for (auto &Succ : RemainderBB->successors()) { 5634 if (MDT->properlyDominates(&MBB, Succ)) { 5635 MDT->changeImmediateDominator(Succ, RemainderBB); 5636 } 5637 } 5638 } 5639 5640 emitLoadSRsrcFromVGPRLoop(TII, MRI, MBB, *LoopBB, *BodyBB, DL, Rsrc); 5641 5642 // Restore the EXEC mask 5643 MachineBasicBlock::iterator First = RemainderBB->begin(); 5644 BuildMI(*RemainderBB, First, DL, TII.get(MovExecOpc), Exec).addReg(SaveExec); 5645 return BodyBB; 5646 } 5647 5648 // Extract pointer from Rsrc and return a zero-value Rsrc replacement. 5649 static std::tuple<unsigned, unsigned> 5650 extractRsrcPtr(const SIInstrInfo &TII, MachineInstr &MI, MachineOperand &Rsrc) { 5651 MachineBasicBlock &MBB = *MI.getParent(); 5652 MachineFunction &MF = *MBB.getParent(); 5653 MachineRegisterInfo &MRI = MF.getRegInfo(); 5654 5655 // Extract the ptr from the resource descriptor. 5656 unsigned RsrcPtr = 5657 TII.buildExtractSubReg(MI, MRI, Rsrc, &AMDGPU::VReg_128RegClass, 5658 AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass); 5659 5660 // Create an empty resource descriptor 5661 Register Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 5662 Register SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5663 Register SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5664 Register NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SGPR_128RegClass); 5665 uint64_t RsrcDataFormat = TII.getDefaultRsrcDataFormat(); 5666 5667 // Zero64 = 0 5668 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B64), Zero64) 5669 .addImm(0); 5670 5671 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0} 5672 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatLo) 5673 .addImm(RsrcDataFormat & 0xFFFFFFFF); 5674 5675 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32} 5676 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatHi) 5677 .addImm(RsrcDataFormat >> 32); 5678 5679 // NewSRsrc = {Zero64, SRsrcFormat} 5680 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::REG_SEQUENCE), NewSRsrc) 5681 .addReg(Zero64) 5682 .addImm(AMDGPU::sub0_sub1) 5683 .addReg(SRsrcFormatLo) 5684 .addImm(AMDGPU::sub2) 5685 .addReg(SRsrcFormatHi) 5686 .addImm(AMDGPU::sub3); 5687 5688 return std::make_tuple(RsrcPtr, NewSRsrc); 5689 } 5690 5691 MachineBasicBlock * 5692 SIInstrInfo::legalizeOperands(MachineInstr &MI, 5693 MachineDominatorTree *MDT) const { 5694 MachineFunction &MF = *MI.getParent()->getParent(); 5695 MachineRegisterInfo &MRI = MF.getRegInfo(); 5696 MachineBasicBlock *CreatedBB = nullptr; 5697 5698 // Legalize VOP2 5699 if (isVOP2(MI) || isVOPC(MI)) { 5700 legalizeOperandsVOP2(MRI, MI); 5701 return CreatedBB; 5702 } 5703 5704 // Legalize VOP3 5705 if (isVOP3(MI)) { 5706 legalizeOperandsVOP3(MRI, MI); 5707 return CreatedBB; 5708 } 5709 5710 // Legalize SMRD 5711 if (isSMRD(MI)) { 5712 legalizeOperandsSMRD(MRI, MI); 5713 return CreatedBB; 5714 } 5715 5716 // Legalize FLAT 5717 if (isFLAT(MI)) { 5718 legalizeOperandsFLAT(MRI, MI); 5719 return CreatedBB; 5720 } 5721 5722 // Legalize REG_SEQUENCE and PHI 5723 // The register class of the operands much be the same type as the register 5724 // class of the output. 5725 if (MI.getOpcode() == AMDGPU::PHI) { 5726 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr; 5727 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { 5728 if (!MI.getOperand(i).isReg() || !MI.getOperand(i).getReg().isVirtual()) 5729 continue; 5730 const TargetRegisterClass *OpRC = 5731 MRI.getRegClass(MI.getOperand(i).getReg()); 5732 if (RI.hasVectorRegisters(OpRC)) { 5733 VRC = OpRC; 5734 } else { 5735 SRC = OpRC; 5736 } 5737 } 5738 5739 // If any of the operands are VGPR registers, then they all most be 5740 // otherwise we will create illegal VGPR->SGPR copies when legalizing 5741 // them. 5742 if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) { 5743 if (!VRC) { 5744 assert(SRC); 5745 if (getOpRegClass(MI, 0) == &AMDGPU::VReg_1RegClass) { 5746 VRC = &AMDGPU::VReg_1RegClass; 5747 } else 5748 VRC = RI.isAGPRClass(getOpRegClass(MI, 0)) 5749 ? RI.getEquivalentAGPRClass(SRC) 5750 : RI.getEquivalentVGPRClass(SRC); 5751 } else { 5752 VRC = RI.isAGPRClass(getOpRegClass(MI, 0)) 5753 ? RI.getEquivalentAGPRClass(VRC) 5754 : RI.getEquivalentVGPRClass(VRC); 5755 } 5756 RC = VRC; 5757 } else { 5758 RC = SRC; 5759 } 5760 5761 // Update all the operands so they have the same type. 5762 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 5763 MachineOperand &Op = MI.getOperand(I); 5764 if (!Op.isReg() || !Op.getReg().isVirtual()) 5765 continue; 5766 5767 // MI is a PHI instruction. 5768 MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB(); 5769 MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator(); 5770 5771 // Avoid creating no-op copies with the same src and dst reg class. These 5772 // confuse some of the machine passes. 5773 legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc()); 5774 } 5775 } 5776 5777 // REG_SEQUENCE doesn't really require operand legalization, but if one has a 5778 // VGPR dest type and SGPR sources, insert copies so all operands are 5779 // VGPRs. This seems to help operand folding / the register coalescer. 5780 if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) { 5781 MachineBasicBlock *MBB = MI.getParent(); 5782 const TargetRegisterClass *DstRC = getOpRegClass(MI, 0); 5783 if (RI.hasVGPRs(DstRC)) { 5784 // Update all the operands so they are VGPR register classes. These may 5785 // not be the same register class because REG_SEQUENCE supports mixing 5786 // subregister index types e.g. sub0_sub1 + sub2 + sub3 5787 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 5788 MachineOperand &Op = MI.getOperand(I); 5789 if (!Op.isReg() || !Op.getReg().isVirtual()) 5790 continue; 5791 5792 const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg()); 5793 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC); 5794 if (VRC == OpRC) 5795 continue; 5796 5797 legalizeGenericOperand(*MBB, MI, VRC, Op, MRI, MI.getDebugLoc()); 5798 Op.setIsKill(); 5799 } 5800 } 5801 5802 return CreatedBB; 5803 } 5804 5805 // Legalize INSERT_SUBREG 5806 // src0 must have the same register class as dst 5807 if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) { 5808 Register Dst = MI.getOperand(0).getReg(); 5809 Register Src0 = MI.getOperand(1).getReg(); 5810 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst); 5811 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0); 5812 if (DstRC != Src0RC) { 5813 MachineBasicBlock *MBB = MI.getParent(); 5814 MachineOperand &Op = MI.getOperand(1); 5815 legalizeGenericOperand(*MBB, MI, DstRC, Op, MRI, MI.getDebugLoc()); 5816 } 5817 return CreatedBB; 5818 } 5819 5820 // Legalize SI_INIT_M0 5821 if (MI.getOpcode() == AMDGPU::SI_INIT_M0) { 5822 MachineOperand &Src = MI.getOperand(0); 5823 if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg()))) 5824 Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI)); 5825 return CreatedBB; 5826 } 5827 5828 // Legalize MIMG and MUBUF/MTBUF for shaders. 5829 // 5830 // Shaders only generate MUBUF/MTBUF instructions via intrinsics or via 5831 // scratch memory access. In both cases, the legalization never involves 5832 // conversion to the addr64 form. 5833 if (isMIMG(MI) || (AMDGPU::isGraphics(MF.getFunction().getCallingConv()) && 5834 (isMUBUF(MI) || isMTBUF(MI)))) { 5835 MachineOperand *SRsrc = getNamedOperand(MI, AMDGPU::OpName::srsrc); 5836 if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg()))) 5837 CreatedBB = loadSRsrcFromVGPR(*this, MI, *SRsrc, MDT); 5838 5839 MachineOperand *SSamp = getNamedOperand(MI, AMDGPU::OpName::ssamp); 5840 if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg()))) 5841 CreatedBB = loadSRsrcFromVGPR(*this, MI, *SSamp, MDT); 5842 5843 return CreatedBB; 5844 } 5845 5846 // Legalize SI_CALL 5847 if (MI.getOpcode() == AMDGPU::SI_CALL_ISEL) { 5848 MachineOperand *Dest = &MI.getOperand(0); 5849 if (!RI.isSGPRClass(MRI.getRegClass(Dest->getReg()))) { 5850 // Move everything between ADJCALLSTACKUP and ADJCALLSTACKDOWN and 5851 // following copies, we also need to move copies from and to physical 5852 // registers into the loop block. 5853 unsigned FrameSetupOpcode = getCallFrameSetupOpcode(); 5854 unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode(); 5855 5856 // Also move the copies to physical registers into the loop block 5857 MachineBasicBlock &MBB = *MI.getParent(); 5858 MachineBasicBlock::iterator Start(&MI); 5859 while (Start->getOpcode() != FrameSetupOpcode) 5860 --Start; 5861 MachineBasicBlock::iterator End(&MI); 5862 while (End->getOpcode() != FrameDestroyOpcode) 5863 ++End; 5864 // Also include following copies of the return value 5865 ++End; 5866 while (End != MBB.end() && End->isCopy() && End->getOperand(1).isReg() && 5867 MI.definesRegister(End->getOperand(1).getReg())) 5868 ++End; 5869 CreatedBB = loadSRsrcFromVGPR(*this, MI, *Dest, MDT, Start, End); 5870 } 5871 } 5872 5873 // Legalize MUBUF* instructions. 5874 int RsrcIdx = 5875 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc); 5876 if (RsrcIdx != -1) { 5877 // We have an MUBUF instruction 5878 MachineOperand *Rsrc = &MI.getOperand(RsrcIdx); 5879 unsigned RsrcRC = get(MI.getOpcode()).OpInfo[RsrcIdx].RegClass; 5880 if (RI.getCommonSubClass(MRI.getRegClass(Rsrc->getReg()), 5881 RI.getRegClass(RsrcRC))) { 5882 // The operands are legal. 5883 // FIXME: We may need to legalize operands besides srsrc. 5884 return CreatedBB; 5885 } 5886 5887 // Legalize a VGPR Rsrc. 5888 // 5889 // If the instruction is _ADDR64, we can avoid a waterfall by extracting 5890 // the base pointer from the VGPR Rsrc, adding it to the VAddr, then using 5891 // a zero-value SRsrc. 5892 // 5893 // If the instruction is _OFFSET (both idxen and offen disabled), and we 5894 // support ADDR64 instructions, we can convert to ADDR64 and do the same as 5895 // above. 5896 // 5897 // Otherwise we are on non-ADDR64 hardware, and/or we have 5898 // idxen/offen/bothen and we fall back to a waterfall loop. 5899 5900 MachineBasicBlock &MBB = *MI.getParent(); 5901 5902 MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 5903 if (VAddr && AMDGPU::getIfAddr64Inst(MI.getOpcode()) != -1) { 5904 // This is already an ADDR64 instruction so we need to add the pointer 5905 // extracted from the resource descriptor to the current value of VAddr. 5906 Register NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 5907 Register NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 5908 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 5909 5910 const auto *BoolXExecRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 5911 Register CondReg0 = MRI.createVirtualRegister(BoolXExecRC); 5912 Register CondReg1 = MRI.createVirtualRegister(BoolXExecRC); 5913 5914 unsigned RsrcPtr, NewSRsrc; 5915 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc); 5916 5917 // NewVaddrLo = RsrcPtr:sub0 + VAddr:sub0 5918 const DebugLoc &DL = MI.getDebugLoc(); 5919 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_CO_U32_e64), NewVAddrLo) 5920 .addDef(CondReg0) 5921 .addReg(RsrcPtr, 0, AMDGPU::sub0) 5922 .addReg(VAddr->getReg(), 0, AMDGPU::sub0) 5923 .addImm(0); 5924 5925 // NewVaddrHi = RsrcPtr:sub1 + VAddr:sub1 5926 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e64), NewVAddrHi) 5927 .addDef(CondReg1, RegState::Dead) 5928 .addReg(RsrcPtr, 0, AMDGPU::sub1) 5929 .addReg(VAddr->getReg(), 0, AMDGPU::sub1) 5930 .addReg(CondReg0, RegState::Kill) 5931 .addImm(0); 5932 5933 // NewVaddr = {NewVaddrHi, NewVaddrLo} 5934 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr) 5935 .addReg(NewVAddrLo) 5936 .addImm(AMDGPU::sub0) 5937 .addReg(NewVAddrHi) 5938 .addImm(AMDGPU::sub1); 5939 5940 VAddr->setReg(NewVAddr); 5941 Rsrc->setReg(NewSRsrc); 5942 } else if (!VAddr && ST.hasAddr64()) { 5943 // This instructions is the _OFFSET variant, so we need to convert it to 5944 // ADDR64. 5945 assert(ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS && 5946 "FIXME: Need to emit flat atomics here"); 5947 5948 unsigned RsrcPtr, NewSRsrc; 5949 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc); 5950 5951 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 5952 MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata); 5953 MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 5954 MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset); 5955 unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode()); 5956 5957 // Atomics with return have an additional tied operand and are 5958 // missing some of the special bits. 5959 MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in); 5960 MachineInstr *Addr64; 5961 5962 if (!VDataIn) { 5963 // Regular buffer load / store. 5964 MachineInstrBuilder MIB = 5965 BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 5966 .add(*VData) 5967 .addReg(NewVAddr) 5968 .addReg(NewSRsrc) 5969 .add(*SOffset) 5970 .add(*Offset); 5971 5972 if (const MachineOperand *CPol = 5973 getNamedOperand(MI, AMDGPU::OpName::cpol)) { 5974 MIB.addImm(CPol->getImm()); 5975 } 5976 5977 if (const MachineOperand *TFE = 5978 getNamedOperand(MI, AMDGPU::OpName::tfe)) { 5979 MIB.addImm(TFE->getImm()); 5980 } 5981 5982 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::swz)); 5983 5984 MIB.cloneMemRefs(MI); 5985 Addr64 = MIB; 5986 } else { 5987 // Atomics with return. 5988 Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 5989 .add(*VData) 5990 .add(*VDataIn) 5991 .addReg(NewVAddr) 5992 .addReg(NewSRsrc) 5993 .add(*SOffset) 5994 .add(*Offset) 5995 .addImm(getNamedImmOperand(MI, AMDGPU::OpName::cpol)) 5996 .cloneMemRefs(MI); 5997 } 5998 5999 MI.removeFromParent(); 6000 6001 // NewVaddr = {NewVaddrHi, NewVaddrLo} 6002 BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), 6003 NewVAddr) 6004 .addReg(RsrcPtr, 0, AMDGPU::sub0) 6005 .addImm(AMDGPU::sub0) 6006 .addReg(RsrcPtr, 0, AMDGPU::sub1) 6007 .addImm(AMDGPU::sub1); 6008 } else { 6009 // This is another variant; legalize Rsrc with waterfall loop from VGPRs 6010 // to SGPRs. 6011 CreatedBB = loadSRsrcFromVGPR(*this, MI, *Rsrc, MDT); 6012 return CreatedBB; 6013 } 6014 } 6015 return CreatedBB; 6016 } 6017 6018 MachineBasicBlock *SIInstrInfo::moveToVALU(MachineInstr &TopInst, 6019 MachineDominatorTree *MDT) const { 6020 SetVectorType Worklist; 6021 Worklist.insert(&TopInst); 6022 MachineBasicBlock *CreatedBB = nullptr; 6023 MachineBasicBlock *CreatedBBTmp = nullptr; 6024 6025 while (!Worklist.empty()) { 6026 MachineInstr &Inst = *Worklist.pop_back_val(); 6027 MachineBasicBlock *MBB = Inst.getParent(); 6028 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 6029 6030 unsigned Opcode = Inst.getOpcode(); 6031 unsigned NewOpcode = getVALUOp(Inst); 6032 6033 // Handle some special cases 6034 switch (Opcode) { 6035 default: 6036 break; 6037 case AMDGPU::S_ADD_U64_PSEUDO: 6038 case AMDGPU::S_SUB_U64_PSEUDO: 6039 splitScalar64BitAddSub(Worklist, Inst, MDT); 6040 Inst.eraseFromParent(); 6041 continue; 6042 case AMDGPU::S_ADD_I32: 6043 case AMDGPU::S_SUB_I32: { 6044 // FIXME: The u32 versions currently selected use the carry. 6045 bool Changed; 6046 std::tie(Changed, CreatedBBTmp) = moveScalarAddSub(Worklist, Inst, MDT); 6047 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6048 CreatedBB = CreatedBBTmp; 6049 if (Changed) 6050 continue; 6051 6052 // Default handling 6053 break; 6054 } 6055 case AMDGPU::S_AND_B64: 6056 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32, MDT); 6057 Inst.eraseFromParent(); 6058 continue; 6059 6060 case AMDGPU::S_OR_B64: 6061 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32, MDT); 6062 Inst.eraseFromParent(); 6063 continue; 6064 6065 case AMDGPU::S_XOR_B64: 6066 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32, MDT); 6067 Inst.eraseFromParent(); 6068 continue; 6069 6070 case AMDGPU::S_NAND_B64: 6071 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NAND_B32, MDT); 6072 Inst.eraseFromParent(); 6073 continue; 6074 6075 case AMDGPU::S_NOR_B64: 6076 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NOR_B32, MDT); 6077 Inst.eraseFromParent(); 6078 continue; 6079 6080 case AMDGPU::S_XNOR_B64: 6081 if (ST.hasDLInsts()) 6082 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XNOR_B32, MDT); 6083 else 6084 splitScalar64BitXnor(Worklist, Inst, MDT); 6085 Inst.eraseFromParent(); 6086 continue; 6087 6088 case AMDGPU::S_ANDN2_B64: 6089 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ANDN2_B32, MDT); 6090 Inst.eraseFromParent(); 6091 continue; 6092 6093 case AMDGPU::S_ORN2_B64: 6094 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ORN2_B32, MDT); 6095 Inst.eraseFromParent(); 6096 continue; 6097 6098 case AMDGPU::S_BREV_B64: 6099 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_BREV_B32, true); 6100 Inst.eraseFromParent(); 6101 continue; 6102 6103 case AMDGPU::S_NOT_B64: 6104 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32); 6105 Inst.eraseFromParent(); 6106 continue; 6107 6108 case AMDGPU::S_BCNT1_I32_B64: 6109 splitScalar64BitBCNT(Worklist, Inst); 6110 Inst.eraseFromParent(); 6111 continue; 6112 6113 case AMDGPU::S_BFE_I64: 6114 splitScalar64BitBFE(Worklist, Inst); 6115 Inst.eraseFromParent(); 6116 continue; 6117 6118 case AMDGPU::S_LSHL_B32: 6119 if (ST.hasOnlyRevVALUShifts()) { 6120 NewOpcode = AMDGPU::V_LSHLREV_B32_e64; 6121 swapOperands(Inst); 6122 } 6123 break; 6124 case AMDGPU::S_ASHR_I32: 6125 if (ST.hasOnlyRevVALUShifts()) { 6126 NewOpcode = AMDGPU::V_ASHRREV_I32_e64; 6127 swapOperands(Inst); 6128 } 6129 break; 6130 case AMDGPU::S_LSHR_B32: 6131 if (ST.hasOnlyRevVALUShifts()) { 6132 NewOpcode = AMDGPU::V_LSHRREV_B32_e64; 6133 swapOperands(Inst); 6134 } 6135 break; 6136 case AMDGPU::S_LSHL_B64: 6137 if (ST.hasOnlyRevVALUShifts()) { 6138 NewOpcode = AMDGPU::V_LSHLREV_B64_e64; 6139 swapOperands(Inst); 6140 } 6141 break; 6142 case AMDGPU::S_ASHR_I64: 6143 if (ST.hasOnlyRevVALUShifts()) { 6144 NewOpcode = AMDGPU::V_ASHRREV_I64_e64; 6145 swapOperands(Inst); 6146 } 6147 break; 6148 case AMDGPU::S_LSHR_B64: 6149 if (ST.hasOnlyRevVALUShifts()) { 6150 NewOpcode = AMDGPU::V_LSHRREV_B64_e64; 6151 swapOperands(Inst); 6152 } 6153 break; 6154 6155 case AMDGPU::S_ABS_I32: 6156 lowerScalarAbs(Worklist, Inst); 6157 Inst.eraseFromParent(); 6158 continue; 6159 6160 case AMDGPU::S_CBRANCH_SCC0: 6161 case AMDGPU::S_CBRANCH_SCC1: { 6162 // Clear unused bits of vcc 6163 Register CondReg = Inst.getOperand(1).getReg(); 6164 bool IsSCC = CondReg == AMDGPU::SCC; 6165 Register VCC = RI.getVCC(); 6166 Register EXEC = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 6167 unsigned Opc = ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64; 6168 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(Opc), VCC) 6169 .addReg(EXEC) 6170 .addReg(IsSCC ? VCC : CondReg); 6171 Inst.removeOperand(1); 6172 } 6173 break; 6174 6175 case AMDGPU::S_BFE_U64: 6176 case AMDGPU::S_BFM_B64: 6177 llvm_unreachable("Moving this op to VALU not implemented"); 6178 6179 case AMDGPU::S_PACK_LL_B32_B16: 6180 case AMDGPU::S_PACK_LH_B32_B16: 6181 case AMDGPU::S_PACK_HH_B32_B16: 6182 movePackToVALU(Worklist, MRI, Inst); 6183 Inst.eraseFromParent(); 6184 continue; 6185 6186 case AMDGPU::S_XNOR_B32: 6187 lowerScalarXnor(Worklist, Inst); 6188 Inst.eraseFromParent(); 6189 continue; 6190 6191 case AMDGPU::S_NAND_B32: 6192 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_AND_B32); 6193 Inst.eraseFromParent(); 6194 continue; 6195 6196 case AMDGPU::S_NOR_B32: 6197 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_OR_B32); 6198 Inst.eraseFromParent(); 6199 continue; 6200 6201 case AMDGPU::S_ANDN2_B32: 6202 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_AND_B32); 6203 Inst.eraseFromParent(); 6204 continue; 6205 6206 case AMDGPU::S_ORN2_B32: 6207 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_OR_B32); 6208 Inst.eraseFromParent(); 6209 continue; 6210 6211 // TODO: remove as soon as everything is ready 6212 // to replace VGPR to SGPR copy with V_READFIRSTLANEs. 6213 // S_ADD/SUB_CO_PSEUDO as well as S_UADDO/USUBO_PSEUDO 6214 // can only be selected from the uniform SDNode. 6215 case AMDGPU::S_ADD_CO_PSEUDO: 6216 case AMDGPU::S_SUB_CO_PSEUDO: { 6217 unsigned Opc = (Inst.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO) 6218 ? AMDGPU::V_ADDC_U32_e64 6219 : AMDGPU::V_SUBB_U32_e64; 6220 const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 6221 6222 Register CarryInReg = Inst.getOperand(4).getReg(); 6223 if (!MRI.constrainRegClass(CarryInReg, CarryRC)) { 6224 Register NewCarryReg = MRI.createVirtualRegister(CarryRC); 6225 BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(AMDGPU::COPY), NewCarryReg) 6226 .addReg(CarryInReg); 6227 } 6228 6229 Register CarryOutReg = Inst.getOperand(1).getReg(); 6230 6231 Register DestReg = MRI.createVirtualRegister(RI.getEquivalentVGPRClass( 6232 MRI.getRegClass(Inst.getOperand(0).getReg()))); 6233 MachineInstr *CarryOp = 6234 BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(Opc), DestReg) 6235 .addReg(CarryOutReg, RegState::Define) 6236 .add(Inst.getOperand(2)) 6237 .add(Inst.getOperand(3)) 6238 .addReg(CarryInReg) 6239 .addImm(0); 6240 CreatedBBTmp = legalizeOperands(*CarryOp); 6241 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6242 CreatedBB = CreatedBBTmp; 6243 MRI.replaceRegWith(Inst.getOperand(0).getReg(), DestReg); 6244 addUsersToMoveToVALUWorklist(DestReg, MRI, Worklist); 6245 Inst.eraseFromParent(); 6246 } 6247 continue; 6248 case AMDGPU::S_UADDO_PSEUDO: 6249 case AMDGPU::S_USUBO_PSEUDO: { 6250 const DebugLoc &DL = Inst.getDebugLoc(); 6251 MachineOperand &Dest0 = Inst.getOperand(0); 6252 MachineOperand &Dest1 = Inst.getOperand(1); 6253 MachineOperand &Src0 = Inst.getOperand(2); 6254 MachineOperand &Src1 = Inst.getOperand(3); 6255 6256 unsigned Opc = (Inst.getOpcode() == AMDGPU::S_UADDO_PSEUDO) 6257 ? AMDGPU::V_ADD_CO_U32_e64 6258 : AMDGPU::V_SUB_CO_U32_e64; 6259 const TargetRegisterClass *NewRC = 6260 RI.getEquivalentVGPRClass(MRI.getRegClass(Dest0.getReg())); 6261 Register DestReg = MRI.createVirtualRegister(NewRC); 6262 MachineInstr *NewInstr = BuildMI(*MBB, &Inst, DL, get(Opc), DestReg) 6263 .addReg(Dest1.getReg(), RegState::Define) 6264 .add(Src0) 6265 .add(Src1) 6266 .addImm(0); // clamp bit 6267 6268 CreatedBBTmp = legalizeOperands(*NewInstr, MDT); 6269 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6270 CreatedBB = CreatedBBTmp; 6271 6272 MRI.replaceRegWith(Dest0.getReg(), DestReg); 6273 addUsersToMoveToVALUWorklist(NewInstr->getOperand(0).getReg(), MRI, 6274 Worklist); 6275 Inst.eraseFromParent(); 6276 } 6277 continue; 6278 6279 case AMDGPU::S_CSELECT_B32: 6280 case AMDGPU::S_CSELECT_B64: 6281 lowerSelect(Worklist, Inst, MDT); 6282 Inst.eraseFromParent(); 6283 continue; 6284 case AMDGPU::S_CMP_EQ_I32: 6285 case AMDGPU::S_CMP_LG_I32: 6286 case AMDGPU::S_CMP_GT_I32: 6287 case AMDGPU::S_CMP_GE_I32: 6288 case AMDGPU::S_CMP_LT_I32: 6289 case AMDGPU::S_CMP_LE_I32: 6290 case AMDGPU::S_CMP_EQ_U32: 6291 case AMDGPU::S_CMP_LG_U32: 6292 case AMDGPU::S_CMP_GT_U32: 6293 case AMDGPU::S_CMP_GE_U32: 6294 case AMDGPU::S_CMP_LT_U32: 6295 case AMDGPU::S_CMP_LE_U32: 6296 case AMDGPU::S_CMP_EQ_U64: 6297 case AMDGPU::S_CMP_LG_U64: { 6298 const MCInstrDesc &NewDesc = get(NewOpcode); 6299 Register CondReg = MRI.createVirtualRegister(RI.getWaveMaskRegClass()); 6300 MachineInstr *NewInstr = 6301 BuildMI(*MBB, Inst, Inst.getDebugLoc(), NewDesc, CondReg) 6302 .add(Inst.getOperand(0)) 6303 .add(Inst.getOperand(1)); 6304 legalizeOperands(*NewInstr, MDT); 6305 int SCCIdx = Inst.findRegisterDefOperandIdx(AMDGPU::SCC); 6306 MachineOperand SCCOp = Inst.getOperand(SCCIdx); 6307 addSCCDefUsersToVALUWorklist(SCCOp, Inst, Worklist, CondReg); 6308 Inst.eraseFromParent(); 6309 } 6310 continue; 6311 } 6312 6313 6314 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) { 6315 // We cannot move this instruction to the VALU, so we should try to 6316 // legalize its operands instead. 6317 CreatedBBTmp = legalizeOperands(Inst, MDT); 6318 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6319 CreatedBB = CreatedBBTmp; 6320 continue; 6321 } 6322 6323 // Use the new VALU Opcode. 6324 const MCInstrDesc &NewDesc = get(NewOpcode); 6325 Inst.setDesc(NewDesc); 6326 6327 // Remove any references to SCC. Vector instructions can't read from it, and 6328 // We're just about to add the implicit use / defs of VCC, and we don't want 6329 // both. 6330 for (unsigned i = Inst.getNumOperands() - 1; i > 0; --i) { 6331 MachineOperand &Op = Inst.getOperand(i); 6332 if (Op.isReg() && Op.getReg() == AMDGPU::SCC) { 6333 // Only propagate through live-def of SCC. 6334 if (Op.isDef() && !Op.isDead()) 6335 addSCCDefUsersToVALUWorklist(Op, Inst, Worklist); 6336 if (Op.isUse()) 6337 addSCCDefsToVALUWorklist(Op, Worklist); 6338 Inst.removeOperand(i); 6339 } 6340 } 6341 6342 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) { 6343 // We are converting these to a BFE, so we need to add the missing 6344 // operands for the size and offset. 6345 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16; 6346 Inst.addOperand(MachineOperand::CreateImm(0)); 6347 Inst.addOperand(MachineOperand::CreateImm(Size)); 6348 6349 } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) { 6350 // The VALU version adds the second operand to the result, so insert an 6351 // extra 0 operand. 6352 Inst.addOperand(MachineOperand::CreateImm(0)); 6353 } 6354 6355 Inst.addImplicitDefUseOperands(*Inst.getParent()->getParent()); 6356 fixImplicitOperands(Inst); 6357 6358 if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) { 6359 const MachineOperand &OffsetWidthOp = Inst.getOperand(2); 6360 // If we need to move this to VGPRs, we need to unpack the second operand 6361 // back into the 2 separate ones for bit offset and width. 6362 assert(OffsetWidthOp.isImm() && 6363 "Scalar BFE is only implemented for constant width and offset"); 6364 uint32_t Imm = OffsetWidthOp.getImm(); 6365 6366 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 6367 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 6368 Inst.removeOperand(2); // Remove old immediate. 6369 Inst.addOperand(MachineOperand::CreateImm(Offset)); 6370 Inst.addOperand(MachineOperand::CreateImm(BitWidth)); 6371 } 6372 6373 bool HasDst = Inst.getOperand(0).isReg() && Inst.getOperand(0).isDef(); 6374 unsigned NewDstReg = AMDGPU::NoRegister; 6375 if (HasDst) { 6376 Register DstReg = Inst.getOperand(0).getReg(); 6377 if (DstReg.isPhysical()) 6378 continue; 6379 6380 // Update the destination register class. 6381 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst); 6382 if (!NewDstRC) 6383 continue; 6384 6385 if (Inst.isCopy() && Inst.getOperand(1).getReg().isVirtual() && 6386 NewDstRC == RI.getRegClassForReg(MRI, Inst.getOperand(1).getReg())) { 6387 // Instead of creating a copy where src and dst are the same register 6388 // class, we just replace all uses of dst with src. These kinds of 6389 // copies interfere with the heuristics MachineSink uses to decide 6390 // whether or not to split a critical edge. Since the pass assumes 6391 // that copies will end up as machine instructions and not be 6392 // eliminated. 6393 addUsersToMoveToVALUWorklist(DstReg, MRI, Worklist); 6394 MRI.replaceRegWith(DstReg, Inst.getOperand(1).getReg()); 6395 MRI.clearKillFlags(Inst.getOperand(1).getReg()); 6396 Inst.getOperand(0).setReg(DstReg); 6397 6398 // Make sure we don't leave around a dead VGPR->SGPR copy. Normally 6399 // these are deleted later, but at -O0 it would leave a suspicious 6400 // looking illegal copy of an undef register. 6401 for (unsigned I = Inst.getNumOperands() - 1; I != 0; --I) 6402 Inst.removeOperand(I); 6403 Inst.setDesc(get(AMDGPU::IMPLICIT_DEF)); 6404 continue; 6405 } 6406 6407 NewDstReg = MRI.createVirtualRegister(NewDstRC); 6408 MRI.replaceRegWith(DstReg, NewDstReg); 6409 } 6410 6411 // Legalize the operands 6412 CreatedBBTmp = legalizeOperands(Inst, MDT); 6413 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6414 CreatedBB = CreatedBBTmp; 6415 6416 if (HasDst) 6417 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist); 6418 } 6419 return CreatedBB; 6420 } 6421 6422 // Add/sub require special handling to deal with carry outs. 6423 std::pair<bool, MachineBasicBlock *> 6424 SIInstrInfo::moveScalarAddSub(SetVectorType &Worklist, MachineInstr &Inst, 6425 MachineDominatorTree *MDT) const { 6426 if (ST.hasAddNoCarry()) { 6427 // Assume there is no user of scc since we don't select this in that case. 6428 // Since scc isn't used, it doesn't really matter if the i32 or u32 variant 6429 // is used. 6430 6431 MachineBasicBlock &MBB = *Inst.getParent(); 6432 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6433 6434 Register OldDstReg = Inst.getOperand(0).getReg(); 6435 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6436 6437 unsigned Opc = Inst.getOpcode(); 6438 assert(Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32); 6439 6440 unsigned NewOpc = Opc == AMDGPU::S_ADD_I32 ? 6441 AMDGPU::V_ADD_U32_e64 : AMDGPU::V_SUB_U32_e64; 6442 6443 assert(Inst.getOperand(3).getReg() == AMDGPU::SCC); 6444 Inst.removeOperand(3); 6445 6446 Inst.setDesc(get(NewOpc)); 6447 Inst.addOperand(MachineOperand::CreateImm(0)); // clamp bit 6448 Inst.addImplicitDefUseOperands(*MBB.getParent()); 6449 MRI.replaceRegWith(OldDstReg, ResultReg); 6450 MachineBasicBlock *NewBB = legalizeOperands(Inst, MDT); 6451 6452 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6453 return std::make_pair(true, NewBB); 6454 } 6455 6456 return std::make_pair(false, nullptr); 6457 } 6458 6459 void SIInstrInfo::lowerSelect(SetVectorType &Worklist, MachineInstr &Inst, 6460 MachineDominatorTree *MDT) const { 6461 6462 MachineBasicBlock &MBB = *Inst.getParent(); 6463 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6464 MachineBasicBlock::iterator MII = Inst; 6465 DebugLoc DL = Inst.getDebugLoc(); 6466 6467 MachineOperand &Dest = Inst.getOperand(0); 6468 MachineOperand &Src0 = Inst.getOperand(1); 6469 MachineOperand &Src1 = Inst.getOperand(2); 6470 MachineOperand &Cond = Inst.getOperand(3); 6471 6472 Register SCCSource = Cond.getReg(); 6473 bool IsSCC = (SCCSource == AMDGPU::SCC); 6474 6475 // If this is a trivial select where the condition is effectively not SCC 6476 // (SCCSource is a source of copy to SCC), then the select is semantically 6477 // equivalent to copying SCCSource. Hence, there is no need to create 6478 // V_CNDMASK, we can just use that and bail out. 6479 if (!IsSCC && Src0.isImm() && (Src0.getImm() == -1) && Src1.isImm() && 6480 (Src1.getImm() == 0)) { 6481 MRI.replaceRegWith(Dest.getReg(), SCCSource); 6482 return; 6483 } 6484 6485 const TargetRegisterClass *TC = 6486 RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 6487 6488 Register CopySCC = MRI.createVirtualRegister(TC); 6489 6490 if (IsSCC) { 6491 // Now look for the closest SCC def if it is a copy 6492 // replacing the SCCSource with the COPY source register 6493 bool CopyFound = false; 6494 for (MachineInstr &CandI : 6495 make_range(std::next(MachineBasicBlock::reverse_iterator(Inst)), 6496 Inst.getParent()->rend())) { 6497 if (CandI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != 6498 -1) { 6499 if (CandI.isCopy() && CandI.getOperand(0).getReg() == AMDGPU::SCC) { 6500 BuildMI(MBB, MII, DL, get(AMDGPU::COPY), CopySCC) 6501 .addReg(CandI.getOperand(1).getReg()); 6502 CopyFound = true; 6503 } 6504 break; 6505 } 6506 } 6507 if (!CopyFound) { 6508 // SCC def is not a copy 6509 // Insert a trivial select instead of creating a copy, because a copy from 6510 // SCC would semantically mean just copying a single bit, but we may need 6511 // the result to be a vector condition mask that needs preserving. 6512 unsigned Opcode = (ST.getWavefrontSize() == 64) ? AMDGPU::S_CSELECT_B64 6513 : AMDGPU::S_CSELECT_B32; 6514 auto NewSelect = 6515 BuildMI(MBB, MII, DL, get(Opcode), CopySCC).addImm(-1).addImm(0); 6516 NewSelect->getOperand(3).setIsUndef(Cond.isUndef()); 6517 } 6518 } 6519 6520 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6521 6522 auto UpdatedInst = 6523 BuildMI(MBB, MII, DL, get(AMDGPU::V_CNDMASK_B32_e64), ResultReg) 6524 .addImm(0) 6525 .add(Src1) // False 6526 .addImm(0) 6527 .add(Src0) // True 6528 .addReg(IsSCC ? CopySCC : SCCSource); 6529 6530 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6531 legalizeOperands(*UpdatedInst, MDT); 6532 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6533 } 6534 6535 void SIInstrInfo::lowerScalarAbs(SetVectorType &Worklist, 6536 MachineInstr &Inst) const { 6537 MachineBasicBlock &MBB = *Inst.getParent(); 6538 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6539 MachineBasicBlock::iterator MII = Inst; 6540 DebugLoc DL = Inst.getDebugLoc(); 6541 6542 MachineOperand &Dest = Inst.getOperand(0); 6543 MachineOperand &Src = Inst.getOperand(1); 6544 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6545 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6546 6547 unsigned SubOp = ST.hasAddNoCarry() ? 6548 AMDGPU::V_SUB_U32_e32 : AMDGPU::V_SUB_CO_U32_e32; 6549 6550 BuildMI(MBB, MII, DL, get(SubOp), TmpReg) 6551 .addImm(0) 6552 .addReg(Src.getReg()); 6553 6554 BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg) 6555 .addReg(Src.getReg()) 6556 .addReg(TmpReg); 6557 6558 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6559 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6560 } 6561 6562 void SIInstrInfo::lowerScalarXnor(SetVectorType &Worklist, 6563 MachineInstr &Inst) const { 6564 MachineBasicBlock &MBB = *Inst.getParent(); 6565 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6566 MachineBasicBlock::iterator MII = Inst; 6567 const DebugLoc &DL = Inst.getDebugLoc(); 6568 6569 MachineOperand &Dest = Inst.getOperand(0); 6570 MachineOperand &Src0 = Inst.getOperand(1); 6571 MachineOperand &Src1 = Inst.getOperand(2); 6572 6573 if (ST.hasDLInsts()) { 6574 Register NewDest = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6575 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src0, MRI, DL); 6576 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src1, MRI, DL); 6577 6578 BuildMI(MBB, MII, DL, get(AMDGPU::V_XNOR_B32_e64), NewDest) 6579 .add(Src0) 6580 .add(Src1); 6581 6582 MRI.replaceRegWith(Dest.getReg(), NewDest); 6583 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6584 } else { 6585 // Using the identity !(x ^ y) == (!x ^ y) == (x ^ !y), we can 6586 // invert either source and then perform the XOR. If either source is a 6587 // scalar register, then we can leave the inversion on the scalar unit to 6588 // achieve a better distribution of scalar and vector instructions. 6589 bool Src0IsSGPR = Src0.isReg() && 6590 RI.isSGPRClass(MRI.getRegClass(Src0.getReg())); 6591 bool Src1IsSGPR = Src1.isReg() && 6592 RI.isSGPRClass(MRI.getRegClass(Src1.getReg())); 6593 MachineInstr *Xor; 6594 Register Temp = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6595 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6596 6597 // Build a pair of scalar instructions and add them to the work list. 6598 // The next iteration over the work list will lower these to the vector 6599 // unit as necessary. 6600 if (Src0IsSGPR) { 6601 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src0); 6602 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest) 6603 .addReg(Temp) 6604 .add(Src1); 6605 } else if (Src1IsSGPR) { 6606 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src1); 6607 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest) 6608 .add(Src0) 6609 .addReg(Temp); 6610 } else { 6611 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), Temp) 6612 .add(Src0) 6613 .add(Src1); 6614 MachineInstr *Not = 6615 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest).addReg(Temp); 6616 Worklist.insert(Not); 6617 } 6618 6619 MRI.replaceRegWith(Dest.getReg(), NewDest); 6620 6621 Worklist.insert(Xor); 6622 6623 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6624 } 6625 } 6626 6627 void SIInstrInfo::splitScalarNotBinop(SetVectorType &Worklist, 6628 MachineInstr &Inst, 6629 unsigned Opcode) const { 6630 MachineBasicBlock &MBB = *Inst.getParent(); 6631 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6632 MachineBasicBlock::iterator MII = Inst; 6633 const DebugLoc &DL = Inst.getDebugLoc(); 6634 6635 MachineOperand &Dest = Inst.getOperand(0); 6636 MachineOperand &Src0 = Inst.getOperand(1); 6637 MachineOperand &Src1 = Inst.getOperand(2); 6638 6639 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6640 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6641 6642 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), Interm) 6643 .add(Src0) 6644 .add(Src1); 6645 6646 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest) 6647 .addReg(Interm); 6648 6649 Worklist.insert(&Op); 6650 Worklist.insert(&Not); 6651 6652 MRI.replaceRegWith(Dest.getReg(), NewDest); 6653 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6654 } 6655 6656 void SIInstrInfo::splitScalarBinOpN2(SetVectorType& Worklist, 6657 MachineInstr &Inst, 6658 unsigned Opcode) const { 6659 MachineBasicBlock &MBB = *Inst.getParent(); 6660 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6661 MachineBasicBlock::iterator MII = Inst; 6662 const DebugLoc &DL = Inst.getDebugLoc(); 6663 6664 MachineOperand &Dest = Inst.getOperand(0); 6665 MachineOperand &Src0 = Inst.getOperand(1); 6666 MachineOperand &Src1 = Inst.getOperand(2); 6667 6668 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 6669 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 6670 6671 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Interm) 6672 .add(Src1); 6673 6674 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), NewDest) 6675 .add(Src0) 6676 .addReg(Interm); 6677 6678 Worklist.insert(&Not); 6679 Worklist.insert(&Op); 6680 6681 MRI.replaceRegWith(Dest.getReg(), NewDest); 6682 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6683 } 6684 6685 void SIInstrInfo::splitScalar64BitUnaryOp( 6686 SetVectorType &Worklist, MachineInstr &Inst, 6687 unsigned Opcode, bool Swap) const { 6688 MachineBasicBlock &MBB = *Inst.getParent(); 6689 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6690 6691 MachineOperand &Dest = Inst.getOperand(0); 6692 MachineOperand &Src0 = Inst.getOperand(1); 6693 DebugLoc DL = Inst.getDebugLoc(); 6694 6695 MachineBasicBlock::iterator MII = Inst; 6696 6697 const MCInstrDesc &InstDesc = get(Opcode); 6698 const TargetRegisterClass *Src0RC = Src0.isReg() ? 6699 MRI.getRegClass(Src0.getReg()) : 6700 &AMDGPU::SGPR_32RegClass; 6701 6702 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 6703 6704 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6705 AMDGPU::sub0, Src0SubRC); 6706 6707 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 6708 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 6709 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 6710 6711 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 6712 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0).add(SrcReg0Sub0); 6713 6714 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6715 AMDGPU::sub1, Src0SubRC); 6716 6717 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 6718 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1).add(SrcReg0Sub1); 6719 6720 if (Swap) 6721 std::swap(DestSub0, DestSub1); 6722 6723 Register FullDestReg = MRI.createVirtualRegister(NewDestRC); 6724 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 6725 .addReg(DestSub0) 6726 .addImm(AMDGPU::sub0) 6727 .addReg(DestSub1) 6728 .addImm(AMDGPU::sub1); 6729 6730 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 6731 6732 Worklist.insert(&LoHalf); 6733 Worklist.insert(&HiHalf); 6734 6735 // We don't need to legalizeOperands here because for a single operand, src0 6736 // will support any kind of input. 6737 6738 // Move all users of this moved value. 6739 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 6740 } 6741 6742 void SIInstrInfo::splitScalar64BitAddSub(SetVectorType &Worklist, 6743 MachineInstr &Inst, 6744 MachineDominatorTree *MDT) const { 6745 bool IsAdd = (Inst.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO); 6746 6747 MachineBasicBlock &MBB = *Inst.getParent(); 6748 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6749 const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 6750 6751 Register FullDestReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 6752 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6753 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6754 6755 Register CarryReg = MRI.createVirtualRegister(CarryRC); 6756 Register DeadCarryReg = MRI.createVirtualRegister(CarryRC); 6757 6758 MachineOperand &Dest = Inst.getOperand(0); 6759 MachineOperand &Src0 = Inst.getOperand(1); 6760 MachineOperand &Src1 = Inst.getOperand(2); 6761 const DebugLoc &DL = Inst.getDebugLoc(); 6762 MachineBasicBlock::iterator MII = Inst; 6763 6764 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0.getReg()); 6765 const TargetRegisterClass *Src1RC = MRI.getRegClass(Src1.getReg()); 6766 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 6767 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); 6768 6769 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6770 AMDGPU::sub0, Src0SubRC); 6771 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6772 AMDGPU::sub0, Src1SubRC); 6773 6774 6775 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6776 AMDGPU::sub1, Src0SubRC); 6777 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6778 AMDGPU::sub1, Src1SubRC); 6779 6780 unsigned LoOpc = IsAdd ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_SUB_CO_U32_e64; 6781 MachineInstr *LoHalf = 6782 BuildMI(MBB, MII, DL, get(LoOpc), DestSub0) 6783 .addReg(CarryReg, RegState::Define) 6784 .add(SrcReg0Sub0) 6785 .add(SrcReg1Sub0) 6786 .addImm(0); // clamp bit 6787 6788 unsigned HiOpc = IsAdd ? AMDGPU::V_ADDC_U32_e64 : AMDGPU::V_SUBB_U32_e64; 6789 MachineInstr *HiHalf = 6790 BuildMI(MBB, MII, DL, get(HiOpc), DestSub1) 6791 .addReg(DeadCarryReg, RegState::Define | RegState::Dead) 6792 .add(SrcReg0Sub1) 6793 .add(SrcReg1Sub1) 6794 .addReg(CarryReg, RegState::Kill) 6795 .addImm(0); // clamp bit 6796 6797 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 6798 .addReg(DestSub0) 6799 .addImm(AMDGPU::sub0) 6800 .addReg(DestSub1) 6801 .addImm(AMDGPU::sub1); 6802 6803 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 6804 6805 // Try to legalize the operands in case we need to swap the order to keep it 6806 // valid. 6807 legalizeOperands(*LoHalf, MDT); 6808 legalizeOperands(*HiHalf, MDT); 6809 6810 // Move all users of this moved value. 6811 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 6812 } 6813 6814 void SIInstrInfo::splitScalar64BitBinaryOp(SetVectorType &Worklist, 6815 MachineInstr &Inst, unsigned Opcode, 6816 MachineDominatorTree *MDT) const { 6817 MachineBasicBlock &MBB = *Inst.getParent(); 6818 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6819 6820 MachineOperand &Dest = Inst.getOperand(0); 6821 MachineOperand &Src0 = Inst.getOperand(1); 6822 MachineOperand &Src1 = Inst.getOperand(2); 6823 DebugLoc DL = Inst.getDebugLoc(); 6824 6825 MachineBasicBlock::iterator MII = Inst; 6826 6827 const MCInstrDesc &InstDesc = get(Opcode); 6828 const TargetRegisterClass *Src0RC = Src0.isReg() ? 6829 MRI.getRegClass(Src0.getReg()) : 6830 &AMDGPU::SGPR_32RegClass; 6831 6832 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 6833 const TargetRegisterClass *Src1RC = Src1.isReg() ? 6834 MRI.getRegClass(Src1.getReg()) : 6835 &AMDGPU::SGPR_32RegClass; 6836 6837 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); 6838 6839 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6840 AMDGPU::sub0, Src0SubRC); 6841 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6842 AMDGPU::sub0, Src1SubRC); 6843 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6844 AMDGPU::sub1, Src0SubRC); 6845 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6846 AMDGPU::sub1, Src1SubRC); 6847 6848 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 6849 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 6850 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 6851 6852 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 6853 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0) 6854 .add(SrcReg0Sub0) 6855 .add(SrcReg1Sub0); 6856 6857 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 6858 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1) 6859 .add(SrcReg0Sub1) 6860 .add(SrcReg1Sub1); 6861 6862 Register FullDestReg = MRI.createVirtualRegister(NewDestRC); 6863 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 6864 .addReg(DestSub0) 6865 .addImm(AMDGPU::sub0) 6866 .addReg(DestSub1) 6867 .addImm(AMDGPU::sub1); 6868 6869 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 6870 6871 Worklist.insert(&LoHalf); 6872 Worklist.insert(&HiHalf); 6873 6874 // Move all users of this moved value. 6875 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 6876 } 6877 6878 void SIInstrInfo::splitScalar64BitXnor(SetVectorType &Worklist, 6879 MachineInstr &Inst, 6880 MachineDominatorTree *MDT) const { 6881 MachineBasicBlock &MBB = *Inst.getParent(); 6882 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6883 6884 MachineOperand &Dest = Inst.getOperand(0); 6885 MachineOperand &Src0 = Inst.getOperand(1); 6886 MachineOperand &Src1 = Inst.getOperand(2); 6887 const DebugLoc &DL = Inst.getDebugLoc(); 6888 6889 MachineBasicBlock::iterator MII = Inst; 6890 6891 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 6892 6893 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 6894 6895 MachineOperand* Op0; 6896 MachineOperand* Op1; 6897 6898 if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) { 6899 Op0 = &Src0; 6900 Op1 = &Src1; 6901 } else { 6902 Op0 = &Src1; 6903 Op1 = &Src0; 6904 } 6905 6906 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B64), Interm) 6907 .add(*Op0); 6908 6909 Register NewDest = MRI.createVirtualRegister(DestRC); 6910 6911 MachineInstr &Xor = *BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B64), NewDest) 6912 .addReg(Interm) 6913 .add(*Op1); 6914 6915 MRI.replaceRegWith(Dest.getReg(), NewDest); 6916 6917 Worklist.insert(&Xor); 6918 } 6919 6920 void SIInstrInfo::splitScalar64BitBCNT( 6921 SetVectorType &Worklist, MachineInstr &Inst) const { 6922 MachineBasicBlock &MBB = *Inst.getParent(); 6923 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6924 6925 MachineBasicBlock::iterator MII = Inst; 6926 const DebugLoc &DL = Inst.getDebugLoc(); 6927 6928 MachineOperand &Dest = Inst.getOperand(0); 6929 MachineOperand &Src = Inst.getOperand(1); 6930 6931 const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64); 6932 const TargetRegisterClass *SrcRC = Src.isReg() ? 6933 MRI.getRegClass(Src.getReg()) : 6934 &AMDGPU::SGPR_32RegClass; 6935 6936 Register MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6937 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6938 6939 const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0); 6940 6941 MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 6942 AMDGPU::sub0, SrcSubRC); 6943 MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 6944 AMDGPU::sub1, SrcSubRC); 6945 6946 BuildMI(MBB, MII, DL, InstDesc, MidReg).add(SrcRegSub0).addImm(0); 6947 6948 BuildMI(MBB, MII, DL, InstDesc, ResultReg).add(SrcRegSub1).addReg(MidReg); 6949 6950 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6951 6952 // We don't need to legalize operands here. src0 for either instruction can be 6953 // an SGPR, and the second input is unused or determined here. 6954 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6955 } 6956 6957 void SIInstrInfo::splitScalar64BitBFE(SetVectorType &Worklist, 6958 MachineInstr &Inst) const { 6959 MachineBasicBlock &MBB = *Inst.getParent(); 6960 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6961 MachineBasicBlock::iterator MII = Inst; 6962 const DebugLoc &DL = Inst.getDebugLoc(); 6963 6964 MachineOperand &Dest = Inst.getOperand(0); 6965 uint32_t Imm = Inst.getOperand(2).getImm(); 6966 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 6967 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 6968 6969 (void) Offset; 6970 6971 // Only sext_inreg cases handled. 6972 assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 && 6973 Offset == 0 && "Not implemented"); 6974 6975 if (BitWidth < 32) { 6976 Register MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6977 Register MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6978 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 6979 6980 BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32_e64), MidRegLo) 6981 .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0) 6982 .addImm(0) 6983 .addImm(BitWidth); 6984 6985 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi) 6986 .addImm(31) 6987 .addReg(MidRegLo); 6988 6989 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 6990 .addReg(MidRegLo) 6991 .addImm(AMDGPU::sub0) 6992 .addReg(MidRegHi) 6993 .addImm(AMDGPU::sub1); 6994 6995 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6996 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6997 return; 6998 } 6999 7000 MachineOperand &Src = Inst.getOperand(1); 7001 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7002 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 7003 7004 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg) 7005 .addImm(31) 7006 .addReg(Src.getReg(), 0, AMDGPU::sub0); 7007 7008 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 7009 .addReg(Src.getReg(), 0, AMDGPU::sub0) 7010 .addImm(AMDGPU::sub0) 7011 .addReg(TmpReg) 7012 .addImm(AMDGPU::sub1); 7013 7014 MRI.replaceRegWith(Dest.getReg(), ResultReg); 7015 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 7016 } 7017 7018 void SIInstrInfo::addUsersToMoveToVALUWorklist( 7019 Register DstReg, 7020 MachineRegisterInfo &MRI, 7021 SetVectorType &Worklist) const { 7022 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg), 7023 E = MRI.use_end(); I != E;) { 7024 MachineInstr &UseMI = *I->getParent(); 7025 7026 unsigned OpNo = 0; 7027 7028 switch (UseMI.getOpcode()) { 7029 case AMDGPU::COPY: 7030 case AMDGPU::WQM: 7031 case AMDGPU::SOFT_WQM: 7032 case AMDGPU::STRICT_WWM: 7033 case AMDGPU::STRICT_WQM: 7034 case AMDGPU::REG_SEQUENCE: 7035 case AMDGPU::PHI: 7036 case AMDGPU::INSERT_SUBREG: 7037 break; 7038 default: 7039 OpNo = I.getOperandNo(); 7040 break; 7041 } 7042 7043 if (!RI.hasVectorRegisters(getOpRegClass(UseMI, OpNo))) { 7044 Worklist.insert(&UseMI); 7045 7046 do { 7047 ++I; 7048 } while (I != E && I->getParent() == &UseMI); 7049 } else { 7050 ++I; 7051 } 7052 } 7053 } 7054 7055 void SIInstrInfo::movePackToVALU(SetVectorType &Worklist, 7056 MachineRegisterInfo &MRI, 7057 MachineInstr &Inst) const { 7058 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7059 MachineBasicBlock *MBB = Inst.getParent(); 7060 MachineOperand &Src0 = Inst.getOperand(1); 7061 MachineOperand &Src1 = Inst.getOperand(2); 7062 const DebugLoc &DL = Inst.getDebugLoc(); 7063 7064 switch (Inst.getOpcode()) { 7065 case AMDGPU::S_PACK_LL_B32_B16: { 7066 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7067 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7068 7069 // FIXME: Can do a lot better if we know the high bits of src0 or src1 are 7070 // 0. 7071 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 7072 .addImm(0xffff); 7073 7074 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_B32_e64), TmpReg) 7075 .addReg(ImmReg, RegState::Kill) 7076 .add(Src0); 7077 7078 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32_e64), ResultReg) 7079 .add(Src1) 7080 .addImm(16) 7081 .addReg(TmpReg, RegState::Kill); 7082 break; 7083 } 7084 case AMDGPU::S_PACK_LH_B32_B16: { 7085 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7086 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 7087 .addImm(0xffff); 7088 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_BFI_B32_e64), ResultReg) 7089 .addReg(ImmReg, RegState::Kill) 7090 .add(Src0) 7091 .add(Src1); 7092 break; 7093 } 7094 case AMDGPU::S_PACK_HH_B32_B16: { 7095 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7096 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7097 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg) 7098 .addImm(16) 7099 .add(Src0); 7100 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 7101 .addImm(0xffff0000); 7102 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_OR_B32_e64), ResultReg) 7103 .add(Src1) 7104 .addReg(ImmReg, RegState::Kill) 7105 .addReg(TmpReg, RegState::Kill); 7106 break; 7107 } 7108 default: 7109 llvm_unreachable("unhandled s_pack_* instruction"); 7110 } 7111 7112 MachineOperand &Dest = Inst.getOperand(0); 7113 MRI.replaceRegWith(Dest.getReg(), ResultReg); 7114 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 7115 } 7116 7117 void SIInstrInfo::addSCCDefUsersToVALUWorklist(MachineOperand &Op, 7118 MachineInstr &SCCDefInst, 7119 SetVectorType &Worklist, 7120 Register NewCond) const { 7121 7122 // Ensure that def inst defines SCC, which is still live. 7123 assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isDef() && 7124 !Op.isDead() && Op.getParent() == &SCCDefInst); 7125 SmallVector<MachineInstr *, 4> CopyToDelete; 7126 // This assumes that all the users of SCC are in the same block 7127 // as the SCC def. 7128 for (MachineInstr &MI : // Skip the def inst itself. 7129 make_range(std::next(MachineBasicBlock::iterator(SCCDefInst)), 7130 SCCDefInst.getParent()->end())) { 7131 // Check if SCC is used first. 7132 int SCCIdx = MI.findRegisterUseOperandIdx(AMDGPU::SCC, false, &RI); 7133 if (SCCIdx != -1) { 7134 if (MI.isCopy()) { 7135 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 7136 Register DestReg = MI.getOperand(0).getReg(); 7137 7138 MRI.replaceRegWith(DestReg, NewCond); 7139 CopyToDelete.push_back(&MI); 7140 } else { 7141 7142 if (NewCond.isValid()) 7143 MI.getOperand(SCCIdx).setReg(NewCond); 7144 7145 Worklist.insert(&MI); 7146 } 7147 } 7148 // Exit if we find another SCC def. 7149 if (MI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != -1) 7150 break; 7151 } 7152 for (auto &Copy : CopyToDelete) 7153 Copy->eraseFromParent(); 7154 } 7155 7156 // Instructions that use SCC may be converted to VALU instructions. When that 7157 // happens, the SCC register is changed to VCC_LO. The instruction that defines 7158 // SCC must be changed to an instruction that defines VCC. This function makes 7159 // sure that the instruction that defines SCC is added to the moveToVALU 7160 // worklist. 7161 void SIInstrInfo::addSCCDefsToVALUWorklist(MachineOperand &Op, 7162 SetVectorType &Worklist) const { 7163 assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isUse()); 7164 7165 MachineInstr *SCCUseInst = Op.getParent(); 7166 // Look for a preceding instruction that either defines VCC or SCC. If VCC 7167 // then there is nothing to do because the defining instruction has been 7168 // converted to a VALU already. If SCC then that instruction needs to be 7169 // converted to a VALU. 7170 for (MachineInstr &MI : 7171 make_range(std::next(MachineBasicBlock::reverse_iterator(SCCUseInst)), 7172 SCCUseInst->getParent()->rend())) { 7173 if (MI.modifiesRegister(AMDGPU::VCC, &RI)) 7174 break; 7175 if (MI.definesRegister(AMDGPU::SCC, &RI)) { 7176 Worklist.insert(&MI); 7177 break; 7178 } 7179 } 7180 } 7181 7182 const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass( 7183 const MachineInstr &Inst) const { 7184 const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0); 7185 7186 switch (Inst.getOpcode()) { 7187 // For target instructions, getOpRegClass just returns the virtual register 7188 // class associated with the operand, so we need to find an equivalent VGPR 7189 // register class in order to move the instruction to the VALU. 7190 case AMDGPU::COPY: 7191 case AMDGPU::PHI: 7192 case AMDGPU::REG_SEQUENCE: 7193 case AMDGPU::INSERT_SUBREG: 7194 case AMDGPU::WQM: 7195 case AMDGPU::SOFT_WQM: 7196 case AMDGPU::STRICT_WWM: 7197 case AMDGPU::STRICT_WQM: { 7198 const TargetRegisterClass *SrcRC = getOpRegClass(Inst, 1); 7199 if (RI.isAGPRClass(SrcRC)) { 7200 if (RI.isAGPRClass(NewDstRC)) 7201 return nullptr; 7202 7203 switch (Inst.getOpcode()) { 7204 case AMDGPU::PHI: 7205 case AMDGPU::REG_SEQUENCE: 7206 case AMDGPU::INSERT_SUBREG: 7207 NewDstRC = RI.getEquivalentAGPRClass(NewDstRC); 7208 break; 7209 default: 7210 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 7211 } 7212 7213 if (!NewDstRC) 7214 return nullptr; 7215 } else { 7216 if (RI.isVGPRClass(NewDstRC) || NewDstRC == &AMDGPU::VReg_1RegClass) 7217 return nullptr; 7218 7219 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 7220 if (!NewDstRC) 7221 return nullptr; 7222 } 7223 7224 return NewDstRC; 7225 } 7226 default: 7227 return NewDstRC; 7228 } 7229 } 7230 7231 // Find the one SGPR operand we are allowed to use. 7232 Register SIInstrInfo::findUsedSGPR(const MachineInstr &MI, 7233 int OpIndices[3]) const { 7234 const MCInstrDesc &Desc = MI.getDesc(); 7235 7236 // Find the one SGPR operand we are allowed to use. 7237 // 7238 // First we need to consider the instruction's operand requirements before 7239 // legalizing. Some operands are required to be SGPRs, such as implicit uses 7240 // of VCC, but we are still bound by the constant bus requirement to only use 7241 // one. 7242 // 7243 // If the operand's class is an SGPR, we can never move it. 7244 7245 Register SGPRReg = findImplicitSGPRRead(MI); 7246 if (SGPRReg != AMDGPU::NoRegister) 7247 return SGPRReg; 7248 7249 Register UsedSGPRs[3] = { AMDGPU::NoRegister }; 7250 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 7251 7252 for (unsigned i = 0; i < 3; ++i) { 7253 int Idx = OpIndices[i]; 7254 if (Idx == -1) 7255 break; 7256 7257 const MachineOperand &MO = MI.getOperand(Idx); 7258 if (!MO.isReg()) 7259 continue; 7260 7261 // Is this operand statically required to be an SGPR based on the operand 7262 // constraints? 7263 const TargetRegisterClass *OpRC = RI.getRegClass(Desc.OpInfo[Idx].RegClass); 7264 bool IsRequiredSGPR = RI.isSGPRClass(OpRC); 7265 if (IsRequiredSGPR) 7266 return MO.getReg(); 7267 7268 // If this could be a VGPR or an SGPR, Check the dynamic register class. 7269 Register Reg = MO.getReg(); 7270 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg); 7271 if (RI.isSGPRClass(RegRC)) 7272 UsedSGPRs[i] = Reg; 7273 } 7274 7275 // We don't have a required SGPR operand, so we have a bit more freedom in 7276 // selecting operands to move. 7277 7278 // Try to select the most used SGPR. If an SGPR is equal to one of the 7279 // others, we choose that. 7280 // 7281 // e.g. 7282 // V_FMA_F32 v0, s0, s0, s0 -> No moves 7283 // V_FMA_F32 v0, s0, s1, s0 -> Move s1 7284 7285 // TODO: If some of the operands are 64-bit SGPRs and some 32, we should 7286 // prefer those. 7287 7288 if (UsedSGPRs[0] != AMDGPU::NoRegister) { 7289 if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2]) 7290 SGPRReg = UsedSGPRs[0]; 7291 } 7292 7293 if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) { 7294 if (UsedSGPRs[1] == UsedSGPRs[2]) 7295 SGPRReg = UsedSGPRs[1]; 7296 } 7297 7298 return SGPRReg; 7299 } 7300 7301 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI, 7302 unsigned OperandName) const { 7303 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName); 7304 if (Idx == -1) 7305 return nullptr; 7306 7307 return &MI.getOperand(Idx); 7308 } 7309 7310 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const { 7311 if (ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 7312 int64_t Format = ST.getGeneration() >= AMDGPUSubtarget::GFX11 ? 7313 AMDGPU::UfmtGFX11::UFMT_32_FLOAT : 7314 AMDGPU::UfmtGFX10::UFMT_32_FLOAT; 7315 return (Format << 44) | 7316 (1ULL << 56) | // RESOURCE_LEVEL = 1 7317 (3ULL << 60); // OOB_SELECT = 3 7318 } 7319 7320 uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT; 7321 if (ST.isAmdHsaOS()) { 7322 // Set ATC = 1. GFX9 doesn't have this bit. 7323 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) 7324 RsrcDataFormat |= (1ULL << 56); 7325 7326 // Set MTYPE = 2 (MTYPE_UC = uncached). GFX9 doesn't have this. 7327 // BTW, it disables TC L2 and therefore decreases performance. 7328 if (ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) 7329 RsrcDataFormat |= (2ULL << 59); 7330 } 7331 7332 return RsrcDataFormat; 7333 } 7334 7335 uint64_t SIInstrInfo::getScratchRsrcWords23() const { 7336 uint64_t Rsrc23 = getDefaultRsrcDataFormat() | 7337 AMDGPU::RSRC_TID_ENABLE | 7338 0xffffffff; // Size; 7339 7340 // GFX9 doesn't have ELEMENT_SIZE. 7341 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 7342 uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize(true)) - 1; 7343 Rsrc23 |= EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT; 7344 } 7345 7346 // IndexStride = 64 / 32. 7347 uint64_t IndexStride = ST.getWavefrontSize() == 64 ? 3 : 2; 7348 Rsrc23 |= IndexStride << AMDGPU::RSRC_INDEX_STRIDE_SHIFT; 7349 7350 // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17]. 7351 // Clear them unless we want a huge stride. 7352 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS && 7353 ST.getGeneration() <= AMDGPUSubtarget::GFX9) 7354 Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT; 7355 7356 return Rsrc23; 7357 } 7358 7359 bool SIInstrInfo::isLowLatencyInstruction(const MachineInstr &MI) const { 7360 unsigned Opc = MI.getOpcode(); 7361 7362 return isSMRD(Opc); 7363 } 7364 7365 bool SIInstrInfo::isHighLatencyDef(int Opc) const { 7366 return get(Opc).mayLoad() && 7367 (isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc) || isFLAT(Opc)); 7368 } 7369 7370 unsigned SIInstrInfo::isStackAccess(const MachineInstr &MI, 7371 int &FrameIndex) const { 7372 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 7373 if (!Addr || !Addr->isFI()) 7374 return AMDGPU::NoRegister; 7375 7376 assert(!MI.memoperands_empty() && 7377 (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS); 7378 7379 FrameIndex = Addr->getIndex(); 7380 return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg(); 7381 } 7382 7383 unsigned SIInstrInfo::isSGPRStackAccess(const MachineInstr &MI, 7384 int &FrameIndex) const { 7385 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr); 7386 assert(Addr && Addr->isFI()); 7387 FrameIndex = Addr->getIndex(); 7388 return getNamedOperand(MI, AMDGPU::OpName::data)->getReg(); 7389 } 7390 7391 unsigned SIInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 7392 int &FrameIndex) const { 7393 if (!MI.mayLoad()) 7394 return AMDGPU::NoRegister; 7395 7396 if (isMUBUF(MI) || isVGPRSpill(MI)) 7397 return isStackAccess(MI, FrameIndex); 7398 7399 if (isSGPRSpill(MI)) 7400 return isSGPRStackAccess(MI, FrameIndex); 7401 7402 return AMDGPU::NoRegister; 7403 } 7404 7405 unsigned SIInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 7406 int &FrameIndex) const { 7407 if (!MI.mayStore()) 7408 return AMDGPU::NoRegister; 7409 7410 if (isMUBUF(MI) || isVGPRSpill(MI)) 7411 return isStackAccess(MI, FrameIndex); 7412 7413 if (isSGPRSpill(MI)) 7414 return isSGPRStackAccess(MI, FrameIndex); 7415 7416 return AMDGPU::NoRegister; 7417 } 7418 7419 unsigned SIInstrInfo::getInstBundleSize(const MachineInstr &MI) const { 7420 unsigned Size = 0; 7421 MachineBasicBlock::const_instr_iterator I = MI.getIterator(); 7422 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 7423 while (++I != E && I->isInsideBundle()) { 7424 assert(!I->isBundle() && "No nested bundle!"); 7425 Size += getInstSizeInBytes(*I); 7426 } 7427 7428 return Size; 7429 } 7430 7431 unsigned SIInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 7432 unsigned Opc = MI.getOpcode(); 7433 const MCInstrDesc &Desc = getMCOpcodeFromPseudo(Opc); 7434 unsigned DescSize = Desc.getSize(); 7435 7436 // If we have a definitive size, we can use it. Otherwise we need to inspect 7437 // the operands to know the size. 7438 if (isFixedSize(MI)) { 7439 unsigned Size = DescSize; 7440 7441 // If we hit the buggy offset, an extra nop will be inserted in MC so 7442 // estimate the worst case. 7443 if (MI.isBranch() && ST.hasOffset3fBug()) 7444 Size += 4; 7445 7446 return Size; 7447 } 7448 7449 // Instructions may have a 32-bit literal encoded after them. Check 7450 // operands that could ever be literals. 7451 if (isVALU(MI) || isSALU(MI)) { 7452 if (isDPP(MI)) 7453 return DescSize; 7454 bool HasLiteral = false; 7455 for (int I = 0, E = MI.getNumExplicitOperands(); I != E; ++I) { 7456 const MachineOperand &Op = MI.getOperand(I); 7457 const MCOperandInfo &OpInfo = Desc.OpInfo[I]; 7458 if (isLiteralConstantLike(Op, OpInfo)) { 7459 HasLiteral = true; 7460 break; 7461 } 7462 } 7463 return HasLiteral ? DescSize + 4 : DescSize; 7464 } 7465 7466 // Check whether we have extra NSA words. 7467 if (isMIMG(MI)) { 7468 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0); 7469 if (VAddr0Idx < 0) 7470 return 8; 7471 7472 int RSrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc); 7473 return 8 + 4 * ((RSrcIdx - VAddr0Idx + 2) / 4); 7474 } 7475 7476 switch (Opc) { 7477 case TargetOpcode::BUNDLE: 7478 return getInstBundleSize(MI); 7479 case TargetOpcode::INLINEASM: 7480 case TargetOpcode::INLINEASM_BR: { 7481 const MachineFunction *MF = MI.getParent()->getParent(); 7482 const char *AsmStr = MI.getOperand(0).getSymbolName(); 7483 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo(), &ST); 7484 } 7485 default: 7486 if (MI.isMetaInstruction()) 7487 return 0; 7488 return DescSize; 7489 } 7490 } 7491 7492 bool SIInstrInfo::mayAccessFlatAddressSpace(const MachineInstr &MI) const { 7493 if (!isFLAT(MI)) 7494 return false; 7495 7496 if (MI.memoperands_empty()) 7497 return true; 7498 7499 for (const MachineMemOperand *MMO : MI.memoperands()) { 7500 if (MMO->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS) 7501 return true; 7502 } 7503 return false; 7504 } 7505 7506 bool SIInstrInfo::isNonUniformBranchInstr(MachineInstr &Branch) const { 7507 return Branch.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO; 7508 } 7509 7510 void SIInstrInfo::convertNonUniformIfRegion(MachineBasicBlock *IfEntry, 7511 MachineBasicBlock *IfEnd) const { 7512 MachineBasicBlock::iterator TI = IfEntry->getFirstTerminator(); 7513 assert(TI != IfEntry->end()); 7514 7515 MachineInstr *Branch = &(*TI); 7516 MachineFunction *MF = IfEntry->getParent(); 7517 MachineRegisterInfo &MRI = IfEntry->getParent()->getRegInfo(); 7518 7519 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 7520 Register DstReg = MRI.createVirtualRegister(RI.getBoolRC()); 7521 MachineInstr *SIIF = 7522 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_IF), DstReg) 7523 .add(Branch->getOperand(0)) 7524 .add(Branch->getOperand(1)); 7525 MachineInstr *SIEND = 7526 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_END_CF)) 7527 .addReg(DstReg); 7528 7529 IfEntry->erase(TI); 7530 IfEntry->insert(IfEntry->end(), SIIF); 7531 IfEnd->insert(IfEnd->getFirstNonPHI(), SIEND); 7532 } 7533 } 7534 7535 void SIInstrInfo::convertNonUniformLoopRegion( 7536 MachineBasicBlock *LoopEntry, MachineBasicBlock *LoopEnd) const { 7537 MachineBasicBlock::iterator TI = LoopEnd->getFirstTerminator(); 7538 // We expect 2 terminators, one conditional and one unconditional. 7539 assert(TI != LoopEnd->end()); 7540 7541 MachineInstr *Branch = &(*TI); 7542 MachineFunction *MF = LoopEnd->getParent(); 7543 MachineRegisterInfo &MRI = LoopEnd->getParent()->getRegInfo(); 7544 7545 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 7546 7547 Register DstReg = MRI.createVirtualRegister(RI.getBoolRC()); 7548 Register BackEdgeReg = MRI.createVirtualRegister(RI.getBoolRC()); 7549 MachineInstrBuilder HeaderPHIBuilder = 7550 BuildMI(*(MF), Branch->getDebugLoc(), get(TargetOpcode::PHI), DstReg); 7551 for (MachineBasicBlock *PMBB : LoopEntry->predecessors()) { 7552 if (PMBB == LoopEnd) { 7553 HeaderPHIBuilder.addReg(BackEdgeReg); 7554 } else { 7555 Register ZeroReg = MRI.createVirtualRegister(RI.getBoolRC()); 7556 materializeImmediate(*PMBB, PMBB->getFirstTerminator(), DebugLoc(), 7557 ZeroReg, 0); 7558 HeaderPHIBuilder.addReg(ZeroReg); 7559 } 7560 HeaderPHIBuilder.addMBB(PMBB); 7561 } 7562 MachineInstr *HeaderPhi = HeaderPHIBuilder; 7563 MachineInstr *SIIFBREAK = BuildMI(*(MF), Branch->getDebugLoc(), 7564 get(AMDGPU::SI_IF_BREAK), BackEdgeReg) 7565 .addReg(DstReg) 7566 .add(Branch->getOperand(0)); 7567 MachineInstr *SILOOP = 7568 BuildMI(*(MF), Branch->getDebugLoc(), get(AMDGPU::SI_LOOP)) 7569 .addReg(BackEdgeReg) 7570 .addMBB(LoopEntry); 7571 7572 LoopEntry->insert(LoopEntry->begin(), HeaderPhi); 7573 LoopEnd->erase(TI); 7574 LoopEnd->insert(LoopEnd->end(), SIIFBREAK); 7575 LoopEnd->insert(LoopEnd->end(), SILOOP); 7576 } 7577 } 7578 7579 ArrayRef<std::pair<int, const char *>> 7580 SIInstrInfo::getSerializableTargetIndices() const { 7581 static const std::pair<int, const char *> TargetIndices[] = { 7582 {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"}, 7583 {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"}, 7584 {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"}, 7585 {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"}, 7586 {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}}; 7587 return makeArrayRef(TargetIndices); 7588 } 7589 7590 /// This is used by the post-RA scheduler (SchedulePostRAList.cpp). The 7591 /// post-RA version of misched uses CreateTargetMIHazardRecognizer. 7592 ScheduleHazardRecognizer * 7593 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 7594 const ScheduleDAG *DAG) const { 7595 return new GCNHazardRecognizer(DAG->MF); 7596 } 7597 7598 /// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer 7599 /// pass. 7600 ScheduleHazardRecognizer * 7601 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const { 7602 return new GCNHazardRecognizer(MF); 7603 } 7604 7605 // Called during: 7606 // - pre-RA scheduling and post-RA scheduling 7607 ScheduleHazardRecognizer * 7608 SIInstrInfo::CreateTargetMIHazardRecognizer(const InstrItineraryData *II, 7609 const ScheduleDAGMI *DAG) const { 7610 // Borrowed from Arm Target 7611 // We would like to restrict this hazard recognizer to only 7612 // post-RA scheduling; we can tell that we're post-RA because we don't 7613 // track VRegLiveness. 7614 if (!DAG->hasVRegLiveness()) 7615 return new GCNHazardRecognizer(DAG->MF); 7616 return TargetInstrInfo::CreateTargetMIHazardRecognizer(II, DAG); 7617 } 7618 7619 std::pair<unsigned, unsigned> 7620 SIInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 7621 return std::make_pair(TF & MO_MASK, TF & ~MO_MASK); 7622 } 7623 7624 ArrayRef<std::pair<unsigned, const char *>> 7625 SIInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 7626 static const std::pair<unsigned, const char *> TargetFlags[] = { 7627 { MO_GOTPCREL, "amdgpu-gotprel" }, 7628 { MO_GOTPCREL32_LO, "amdgpu-gotprel32-lo" }, 7629 { MO_GOTPCREL32_HI, "amdgpu-gotprel32-hi" }, 7630 { MO_REL32_LO, "amdgpu-rel32-lo" }, 7631 { MO_REL32_HI, "amdgpu-rel32-hi" }, 7632 { MO_ABS32_LO, "amdgpu-abs32-lo" }, 7633 { MO_ABS32_HI, "amdgpu-abs32-hi" }, 7634 }; 7635 7636 return makeArrayRef(TargetFlags); 7637 } 7638 7639 ArrayRef<std::pair<MachineMemOperand::Flags, const char *>> 7640 SIInstrInfo::getSerializableMachineMemOperandTargetFlags() const { 7641 static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] = 7642 { 7643 {MONoClobber, "amdgpu-noclobber"}, 7644 }; 7645 7646 return makeArrayRef(TargetFlags); 7647 } 7648 7649 bool SIInstrInfo::isBasicBlockPrologue(const MachineInstr &MI) const { 7650 return !MI.isTerminator() && MI.getOpcode() != AMDGPU::COPY && 7651 MI.modifiesRegister(AMDGPU::EXEC, &RI); 7652 } 7653 7654 MachineInstrBuilder 7655 SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB, 7656 MachineBasicBlock::iterator I, 7657 const DebugLoc &DL, 7658 Register DestReg) const { 7659 if (ST.hasAddNoCarry()) 7660 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e64), DestReg); 7661 7662 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7663 Register UnusedCarry = MRI.createVirtualRegister(RI.getBoolRC()); 7664 MRI.setRegAllocationHint(UnusedCarry, 0, RI.getVCC()); 7665 7666 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg) 7667 .addReg(UnusedCarry, RegState::Define | RegState::Dead); 7668 } 7669 7670 MachineInstrBuilder SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB, 7671 MachineBasicBlock::iterator I, 7672 const DebugLoc &DL, 7673 Register DestReg, 7674 RegScavenger &RS) const { 7675 if (ST.hasAddNoCarry()) 7676 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e32), DestReg); 7677 7678 // If available, prefer to use vcc. 7679 Register UnusedCarry = !RS.isRegUsed(AMDGPU::VCC) 7680 ? Register(RI.getVCC()) 7681 : RS.scavengeRegister(RI.getBoolRC(), I, 0, false); 7682 7683 // TODO: Users need to deal with this. 7684 if (!UnusedCarry.isValid()) 7685 return MachineInstrBuilder(); 7686 7687 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg) 7688 .addReg(UnusedCarry, RegState::Define | RegState::Dead); 7689 } 7690 7691 bool SIInstrInfo::isKillTerminator(unsigned Opcode) { 7692 switch (Opcode) { 7693 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR: 7694 case AMDGPU::SI_KILL_I1_TERMINATOR: 7695 return true; 7696 default: 7697 return false; 7698 } 7699 } 7700 7701 const MCInstrDesc &SIInstrInfo::getKillTerminatorFromPseudo(unsigned Opcode) const { 7702 switch (Opcode) { 7703 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO: 7704 return get(AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR); 7705 case AMDGPU::SI_KILL_I1_PSEUDO: 7706 return get(AMDGPU::SI_KILL_I1_TERMINATOR); 7707 default: 7708 llvm_unreachable("invalid opcode, expected SI_KILL_*_PSEUDO"); 7709 } 7710 } 7711 7712 void SIInstrInfo::fixImplicitOperands(MachineInstr &MI) const { 7713 if (!ST.isWave32()) 7714 return; 7715 7716 for (auto &Op : MI.implicit_operands()) { 7717 if (Op.isReg() && Op.getReg() == AMDGPU::VCC) 7718 Op.setReg(AMDGPU::VCC_LO); 7719 } 7720 } 7721 7722 bool SIInstrInfo::isBufferSMRD(const MachineInstr &MI) const { 7723 if (!isSMRD(MI)) 7724 return false; 7725 7726 // Check that it is using a buffer resource. 7727 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sbase); 7728 if (Idx == -1) // e.g. s_memtime 7729 return false; 7730 7731 const auto RCID = MI.getDesc().OpInfo[Idx].RegClass; 7732 return RI.getRegClass(RCID)->hasSubClassEq(&AMDGPU::SGPR_128RegClass); 7733 } 7734 7735 // Depending on the used address space and instructions, some immediate offsets 7736 // are allowed and some are not. 7737 // In general, flat instruction offsets can only be non-negative, global and 7738 // scratch instruction offsets can also be negative. 7739 // 7740 // There are several bugs related to these offsets: 7741 // On gfx10.1, flat instructions that go into the global address space cannot 7742 // use an offset. 7743 // 7744 // For scratch instructions, the address can be either an SGPR or a VGPR. 7745 // The following offsets can be used, depending on the architecture (x means 7746 // cannot be used): 7747 // +----------------------------+------+------+ 7748 // | Address-Mode | SGPR | VGPR | 7749 // +----------------------------+------+------+ 7750 // | gfx9 | | | 7751 // | negative, 4-aligned offset | x | ok | 7752 // | negative, unaligned offset | x | ok | 7753 // +----------------------------+------+------+ 7754 // | gfx10 | | | 7755 // | negative, 4-aligned offset | ok | ok | 7756 // | negative, unaligned offset | ok | x | 7757 // +----------------------------+------+------+ 7758 // | gfx10.3 | | | 7759 // | negative, 4-aligned offset | ok | ok | 7760 // | negative, unaligned offset | ok | ok | 7761 // +----------------------------+------+------+ 7762 // 7763 // This function ignores the addressing mode, so if an offset cannot be used in 7764 // one addressing mode, it is considered illegal. 7765 bool SIInstrInfo::isLegalFLATOffset(int64_t Offset, unsigned AddrSpace, 7766 uint64_t FlatVariant) const { 7767 // TODO: Should 0 be special cased? 7768 if (!ST.hasFlatInstOffsets()) 7769 return false; 7770 7771 if (ST.hasFlatSegmentOffsetBug() && FlatVariant == SIInstrFlags::FLAT && 7772 (AddrSpace == AMDGPUAS::FLAT_ADDRESS || 7773 AddrSpace == AMDGPUAS::GLOBAL_ADDRESS)) 7774 return false; 7775 7776 bool Signed = FlatVariant != SIInstrFlags::FLAT; 7777 if (ST.hasNegativeScratchOffsetBug() && 7778 FlatVariant == SIInstrFlags::FlatScratch) 7779 Signed = false; 7780 if (ST.hasNegativeUnalignedScratchOffsetBug() && 7781 FlatVariant == SIInstrFlags::FlatScratch && Offset < 0 && 7782 (Offset % 4) != 0) { 7783 return false; 7784 } 7785 7786 unsigned N = AMDGPU::getNumFlatOffsetBits(ST, Signed); 7787 return Signed ? isIntN(N, Offset) : isUIntN(N, Offset); 7788 } 7789 7790 // See comment on SIInstrInfo::isLegalFLATOffset for what is legal and what not. 7791 std::pair<int64_t, int64_t> 7792 SIInstrInfo::splitFlatOffset(int64_t COffsetVal, unsigned AddrSpace, 7793 uint64_t FlatVariant) const { 7794 int64_t RemainderOffset = COffsetVal; 7795 int64_t ImmField = 0; 7796 bool Signed = FlatVariant != SIInstrFlags::FLAT; 7797 if (ST.hasNegativeScratchOffsetBug() && 7798 FlatVariant == SIInstrFlags::FlatScratch) 7799 Signed = false; 7800 7801 const unsigned NumBits = AMDGPU::getNumFlatOffsetBits(ST, Signed); 7802 if (Signed) { 7803 // Use signed division by a power of two to truncate towards 0. 7804 int64_t D = 1LL << (NumBits - 1); 7805 RemainderOffset = (COffsetVal / D) * D; 7806 ImmField = COffsetVal - RemainderOffset; 7807 7808 if (ST.hasNegativeUnalignedScratchOffsetBug() && 7809 FlatVariant == SIInstrFlags::FlatScratch && ImmField < 0 && 7810 (ImmField % 4) != 0) { 7811 // Make ImmField a multiple of 4 7812 RemainderOffset += ImmField % 4; 7813 ImmField -= ImmField % 4; 7814 } 7815 } else if (COffsetVal >= 0) { 7816 ImmField = COffsetVal & maskTrailingOnes<uint64_t>(NumBits); 7817 RemainderOffset = COffsetVal - ImmField; 7818 } 7819 7820 assert(isLegalFLATOffset(ImmField, AddrSpace, FlatVariant)); 7821 assert(RemainderOffset + ImmField == COffsetVal); 7822 return {ImmField, RemainderOffset}; 7823 } 7824 7825 // This must be kept in sync with the SIEncodingFamily class in SIInstrInfo.td 7826 // and the columns of the getMCOpcodeGen table. 7827 enum SIEncodingFamily { 7828 SI = 0, 7829 VI = 1, 7830 SDWA = 2, 7831 SDWA9 = 3, 7832 GFX80 = 4, 7833 GFX9 = 5, 7834 GFX10 = 6, 7835 SDWA10 = 7, 7836 GFX90A = 8, 7837 GFX940 = 9, 7838 GFX11 = 10, 7839 }; 7840 7841 static SIEncodingFamily subtargetEncodingFamily(const GCNSubtarget &ST) { 7842 switch (ST.getGeneration()) { 7843 default: 7844 break; 7845 case AMDGPUSubtarget::SOUTHERN_ISLANDS: 7846 case AMDGPUSubtarget::SEA_ISLANDS: 7847 return SIEncodingFamily::SI; 7848 case AMDGPUSubtarget::VOLCANIC_ISLANDS: 7849 case AMDGPUSubtarget::GFX9: 7850 return SIEncodingFamily::VI; 7851 case AMDGPUSubtarget::GFX10: 7852 return SIEncodingFamily::GFX10; 7853 case AMDGPUSubtarget::GFX11: 7854 return SIEncodingFamily::GFX11; 7855 } 7856 llvm_unreachable("Unknown subtarget generation!"); 7857 } 7858 7859 bool SIInstrInfo::isAsmOnlyOpcode(int MCOp) const { 7860 switch(MCOp) { 7861 // These opcodes use indirect register addressing so 7862 // they need special handling by codegen (currently missing). 7863 // Therefore it is too risky to allow these opcodes 7864 // to be selected by dpp combiner or sdwa peepholer. 7865 case AMDGPU::V_MOVRELS_B32_dpp_gfx10: 7866 case AMDGPU::V_MOVRELS_B32_sdwa_gfx10: 7867 case AMDGPU::V_MOVRELD_B32_dpp_gfx10: 7868 case AMDGPU::V_MOVRELD_B32_sdwa_gfx10: 7869 case AMDGPU::V_MOVRELSD_B32_dpp_gfx10: 7870 case AMDGPU::V_MOVRELSD_B32_sdwa_gfx10: 7871 case AMDGPU::V_MOVRELSD_2_B32_dpp_gfx10: 7872 case AMDGPU::V_MOVRELSD_2_B32_sdwa_gfx10: 7873 return true; 7874 default: 7875 return false; 7876 } 7877 } 7878 7879 int SIInstrInfo::pseudoToMCOpcode(int Opcode) const { 7880 SIEncodingFamily Gen = subtargetEncodingFamily(ST); 7881 7882 if ((get(Opcode).TSFlags & SIInstrFlags::renamedInGFX9) != 0 && 7883 ST.getGeneration() == AMDGPUSubtarget::GFX9) 7884 Gen = SIEncodingFamily::GFX9; 7885 7886 // Adjust the encoding family to GFX80 for D16 buffer instructions when the 7887 // subtarget has UnpackedD16VMem feature. 7888 // TODO: remove this when we discard GFX80 encoding. 7889 if (ST.hasUnpackedD16VMem() && (get(Opcode).TSFlags & SIInstrFlags::D16Buf)) 7890 Gen = SIEncodingFamily::GFX80; 7891 7892 if (get(Opcode).TSFlags & SIInstrFlags::SDWA) { 7893 switch (ST.getGeneration()) { 7894 default: 7895 Gen = SIEncodingFamily::SDWA; 7896 break; 7897 case AMDGPUSubtarget::GFX9: 7898 Gen = SIEncodingFamily::SDWA9; 7899 break; 7900 case AMDGPUSubtarget::GFX10: 7901 Gen = SIEncodingFamily::SDWA10; 7902 break; 7903 } 7904 } 7905 7906 if (isMAI(Opcode)) { 7907 int MFMAOp = AMDGPU::getMFMAEarlyClobberOp(Opcode); 7908 if (MFMAOp != -1) 7909 Opcode = MFMAOp; 7910 } 7911 7912 int MCOp = AMDGPU::getMCOpcode(Opcode, Gen); 7913 7914 // -1 means that Opcode is already a native instruction. 7915 if (MCOp == -1) 7916 return Opcode; 7917 7918 if (ST.hasGFX90AInsts()) { 7919 uint16_t NMCOp = (uint16_t)-1; 7920 if (ST.hasGFX940Insts()) 7921 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX940); 7922 if (NMCOp == (uint16_t)-1) 7923 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX90A); 7924 if (NMCOp == (uint16_t)-1) 7925 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX9); 7926 if (NMCOp != (uint16_t)-1) 7927 MCOp = NMCOp; 7928 } 7929 7930 // (uint16_t)-1 means that Opcode is a pseudo instruction that has 7931 // no encoding in the given subtarget generation. 7932 if (MCOp == (uint16_t)-1) 7933 return -1; 7934 7935 if (isAsmOnlyOpcode(MCOp)) 7936 return -1; 7937 7938 return MCOp; 7939 } 7940 7941 static 7942 TargetInstrInfo::RegSubRegPair getRegOrUndef(const MachineOperand &RegOpnd) { 7943 assert(RegOpnd.isReg()); 7944 return RegOpnd.isUndef() ? TargetInstrInfo::RegSubRegPair() : 7945 getRegSubRegPair(RegOpnd); 7946 } 7947 7948 TargetInstrInfo::RegSubRegPair 7949 llvm::getRegSequenceSubReg(MachineInstr &MI, unsigned SubReg) { 7950 assert(MI.isRegSequence()); 7951 for (unsigned I = 0, E = (MI.getNumOperands() - 1)/ 2; I < E; ++I) 7952 if (MI.getOperand(1 + 2 * I + 1).getImm() == SubReg) { 7953 auto &RegOp = MI.getOperand(1 + 2 * I); 7954 return getRegOrUndef(RegOp); 7955 } 7956 return TargetInstrInfo::RegSubRegPair(); 7957 } 7958 7959 // Try to find the definition of reg:subreg in subreg-manipulation pseudos 7960 // Following a subreg of reg:subreg isn't supported 7961 static bool followSubRegDef(MachineInstr &MI, 7962 TargetInstrInfo::RegSubRegPair &RSR) { 7963 if (!RSR.SubReg) 7964 return false; 7965 switch (MI.getOpcode()) { 7966 default: break; 7967 case AMDGPU::REG_SEQUENCE: 7968 RSR = getRegSequenceSubReg(MI, RSR.SubReg); 7969 return true; 7970 // EXTRACT_SUBREG ins't supported as this would follow a subreg of subreg 7971 case AMDGPU::INSERT_SUBREG: 7972 if (RSR.SubReg == (unsigned)MI.getOperand(3).getImm()) 7973 // inserted the subreg we're looking for 7974 RSR = getRegOrUndef(MI.getOperand(2)); 7975 else { // the subreg in the rest of the reg 7976 auto R1 = getRegOrUndef(MI.getOperand(1)); 7977 if (R1.SubReg) // subreg of subreg isn't supported 7978 return false; 7979 RSR.Reg = R1.Reg; 7980 } 7981 return true; 7982 } 7983 return false; 7984 } 7985 7986 MachineInstr *llvm::getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P, 7987 MachineRegisterInfo &MRI) { 7988 assert(MRI.isSSA()); 7989 if (!P.Reg.isVirtual()) 7990 return nullptr; 7991 7992 auto RSR = P; 7993 auto *DefInst = MRI.getVRegDef(RSR.Reg); 7994 while (auto *MI = DefInst) { 7995 DefInst = nullptr; 7996 switch (MI->getOpcode()) { 7997 case AMDGPU::COPY: 7998 case AMDGPU::V_MOV_B32_e32: { 7999 auto &Op1 = MI->getOperand(1); 8000 if (Op1.isReg() && Op1.getReg().isVirtual()) { 8001 if (Op1.isUndef()) 8002 return nullptr; 8003 RSR = getRegSubRegPair(Op1); 8004 DefInst = MRI.getVRegDef(RSR.Reg); 8005 } 8006 break; 8007 } 8008 default: 8009 if (followSubRegDef(*MI, RSR)) { 8010 if (!RSR.Reg) 8011 return nullptr; 8012 DefInst = MRI.getVRegDef(RSR.Reg); 8013 } 8014 } 8015 if (!DefInst) 8016 return MI; 8017 } 8018 return nullptr; 8019 } 8020 8021 bool llvm::execMayBeModifiedBeforeUse(const MachineRegisterInfo &MRI, 8022 Register VReg, 8023 const MachineInstr &DefMI, 8024 const MachineInstr &UseMI) { 8025 assert(MRI.isSSA() && "Must be run on SSA"); 8026 8027 auto *TRI = MRI.getTargetRegisterInfo(); 8028 auto *DefBB = DefMI.getParent(); 8029 8030 // Don't bother searching between blocks, although it is possible this block 8031 // doesn't modify exec. 8032 if (UseMI.getParent() != DefBB) 8033 return true; 8034 8035 const int MaxInstScan = 20; 8036 int NumInst = 0; 8037 8038 // Stop scan at the use. 8039 auto E = UseMI.getIterator(); 8040 for (auto I = std::next(DefMI.getIterator()); I != E; ++I) { 8041 if (I->isDebugInstr()) 8042 continue; 8043 8044 if (++NumInst > MaxInstScan) 8045 return true; 8046 8047 if (I->modifiesRegister(AMDGPU::EXEC, TRI)) 8048 return true; 8049 } 8050 8051 return false; 8052 } 8053 8054 bool llvm::execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo &MRI, 8055 Register VReg, 8056 const MachineInstr &DefMI) { 8057 assert(MRI.isSSA() && "Must be run on SSA"); 8058 8059 auto *TRI = MRI.getTargetRegisterInfo(); 8060 auto *DefBB = DefMI.getParent(); 8061 8062 const int MaxUseScan = 10; 8063 int NumUse = 0; 8064 8065 for (auto &Use : MRI.use_nodbg_operands(VReg)) { 8066 auto &UseInst = *Use.getParent(); 8067 // Don't bother searching between blocks, although it is possible this block 8068 // doesn't modify exec. 8069 if (UseInst.getParent() != DefBB || UseInst.isPHI()) 8070 return true; 8071 8072 if (++NumUse > MaxUseScan) 8073 return true; 8074 } 8075 8076 if (NumUse == 0) 8077 return false; 8078 8079 const int MaxInstScan = 20; 8080 int NumInst = 0; 8081 8082 // Stop scan when we have seen all the uses. 8083 for (auto I = std::next(DefMI.getIterator()); ; ++I) { 8084 assert(I != DefBB->end()); 8085 8086 if (I->isDebugInstr()) 8087 continue; 8088 8089 if (++NumInst > MaxInstScan) 8090 return true; 8091 8092 for (const MachineOperand &Op : I->operands()) { 8093 // We don't check reg masks here as they're used only on calls: 8094 // 1. EXEC is only considered const within one BB 8095 // 2. Call should be a terminator instruction if present in a BB 8096 8097 if (!Op.isReg()) 8098 continue; 8099 8100 Register Reg = Op.getReg(); 8101 if (Op.isUse()) { 8102 if (Reg == VReg && --NumUse == 0) 8103 return false; 8104 } else if (TRI->regsOverlap(Reg, AMDGPU::EXEC)) 8105 return true; 8106 } 8107 } 8108 } 8109 8110 MachineInstr *SIInstrInfo::createPHIDestinationCopy( 8111 MachineBasicBlock &MBB, MachineBasicBlock::iterator LastPHIIt, 8112 const DebugLoc &DL, Register Src, Register Dst) const { 8113 auto Cur = MBB.begin(); 8114 if (Cur != MBB.end()) 8115 do { 8116 if (!Cur->isPHI() && Cur->readsRegister(Dst)) 8117 return BuildMI(MBB, Cur, DL, get(TargetOpcode::COPY), Dst).addReg(Src); 8118 ++Cur; 8119 } while (Cur != MBB.end() && Cur != LastPHIIt); 8120 8121 return TargetInstrInfo::createPHIDestinationCopy(MBB, LastPHIIt, DL, Src, 8122 Dst); 8123 } 8124 8125 MachineInstr *SIInstrInfo::createPHISourceCopy( 8126 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt, 8127 const DebugLoc &DL, Register Src, unsigned SrcSubReg, Register Dst) const { 8128 if (InsPt != MBB.end() && 8129 (InsPt->getOpcode() == AMDGPU::SI_IF || 8130 InsPt->getOpcode() == AMDGPU::SI_ELSE || 8131 InsPt->getOpcode() == AMDGPU::SI_IF_BREAK) && 8132 InsPt->definesRegister(Src)) { 8133 InsPt++; 8134 return BuildMI(MBB, InsPt, DL, 8135 get(ST.isWave32() ? AMDGPU::S_MOV_B32_term 8136 : AMDGPU::S_MOV_B64_term), 8137 Dst) 8138 .addReg(Src, 0, SrcSubReg) 8139 .addReg(AMDGPU::EXEC, RegState::Implicit); 8140 } 8141 return TargetInstrInfo::createPHISourceCopy(MBB, InsPt, DL, Src, SrcSubReg, 8142 Dst); 8143 } 8144 8145 bool llvm::SIInstrInfo::isWave32() const { return ST.isWave32(); } 8146 8147 MachineInstr *SIInstrInfo::foldMemoryOperandImpl( 8148 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 8149 MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS, 8150 VirtRegMap *VRM) const { 8151 // This is a bit of a hack (copied from AArch64). Consider this instruction: 8152 // 8153 // %0:sreg_32 = COPY $m0 8154 // 8155 // We explicitly chose SReg_32 for the virtual register so such a copy might 8156 // be eliminated by RegisterCoalescer. However, that may not be possible, and 8157 // %0 may even spill. We can't spill $m0 normally (it would require copying to 8158 // a numbered SGPR anyway), and since it is in the SReg_32 register class, 8159 // TargetInstrInfo::foldMemoryOperand() is going to try. 8160 // A similar issue also exists with spilling and reloading $exec registers. 8161 // 8162 // To prevent that, constrain the %0 register class here. 8163 if (MI.isFullCopy()) { 8164 Register DstReg = MI.getOperand(0).getReg(); 8165 Register SrcReg = MI.getOperand(1).getReg(); 8166 if ((DstReg.isVirtual() || SrcReg.isVirtual()) && 8167 (DstReg.isVirtual() != SrcReg.isVirtual())) { 8168 MachineRegisterInfo &MRI = MF.getRegInfo(); 8169 Register VirtReg = DstReg.isVirtual() ? DstReg : SrcReg; 8170 const TargetRegisterClass *RC = MRI.getRegClass(VirtReg); 8171 if (RC->hasSuperClassEq(&AMDGPU::SReg_32RegClass)) { 8172 MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_32_XM0_XEXECRegClass); 8173 return nullptr; 8174 } else if (RC->hasSuperClassEq(&AMDGPU::SReg_64RegClass)) { 8175 MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_64_XEXECRegClass); 8176 return nullptr; 8177 } 8178 } 8179 } 8180 8181 return nullptr; 8182 } 8183 8184 unsigned SIInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 8185 const MachineInstr &MI, 8186 unsigned *PredCost) const { 8187 if (MI.isBundle()) { 8188 MachineBasicBlock::const_instr_iterator I(MI.getIterator()); 8189 MachineBasicBlock::const_instr_iterator E(MI.getParent()->instr_end()); 8190 unsigned Lat = 0, Count = 0; 8191 for (++I; I != E && I->isBundledWithPred(); ++I) { 8192 ++Count; 8193 Lat = std::max(Lat, SchedModel.computeInstrLatency(&*I)); 8194 } 8195 return Lat + Count - 1; 8196 } 8197 8198 return SchedModel.computeInstrLatency(&MI); 8199 } 8200 8201 unsigned SIInstrInfo::getDSShaderTypeValue(const MachineFunction &MF) { 8202 switch (MF.getFunction().getCallingConv()) { 8203 case CallingConv::AMDGPU_PS: 8204 return 1; 8205 case CallingConv::AMDGPU_VS: 8206 return 2; 8207 case CallingConv::AMDGPU_GS: 8208 return 3; 8209 case CallingConv::AMDGPU_HS: 8210 case CallingConv::AMDGPU_LS: 8211 case CallingConv::AMDGPU_ES: 8212 report_fatal_error("ds_ordered_count unsupported for this calling conv"); 8213 case CallingConv::AMDGPU_CS: 8214 case CallingConv::AMDGPU_KERNEL: 8215 case CallingConv::C: 8216 case CallingConv::Fast: 8217 default: 8218 // Assume other calling conventions are various compute callable functions 8219 return 0; 8220 } 8221 } 8222 8223 bool SIInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg, 8224 Register &SrcReg2, int64_t &CmpMask, 8225 int64_t &CmpValue) const { 8226 if (!MI.getOperand(0).isReg() || MI.getOperand(0).getSubReg()) 8227 return false; 8228 8229 switch (MI.getOpcode()) { 8230 default: 8231 break; 8232 case AMDGPU::S_CMP_EQ_U32: 8233 case AMDGPU::S_CMP_EQ_I32: 8234 case AMDGPU::S_CMP_LG_U32: 8235 case AMDGPU::S_CMP_LG_I32: 8236 case AMDGPU::S_CMP_LT_U32: 8237 case AMDGPU::S_CMP_LT_I32: 8238 case AMDGPU::S_CMP_GT_U32: 8239 case AMDGPU::S_CMP_GT_I32: 8240 case AMDGPU::S_CMP_LE_U32: 8241 case AMDGPU::S_CMP_LE_I32: 8242 case AMDGPU::S_CMP_GE_U32: 8243 case AMDGPU::S_CMP_GE_I32: 8244 case AMDGPU::S_CMP_EQ_U64: 8245 case AMDGPU::S_CMP_LG_U64: 8246 SrcReg = MI.getOperand(0).getReg(); 8247 if (MI.getOperand(1).isReg()) { 8248 if (MI.getOperand(1).getSubReg()) 8249 return false; 8250 SrcReg2 = MI.getOperand(1).getReg(); 8251 CmpValue = 0; 8252 } else if (MI.getOperand(1).isImm()) { 8253 SrcReg2 = Register(); 8254 CmpValue = MI.getOperand(1).getImm(); 8255 } else { 8256 return false; 8257 } 8258 CmpMask = ~0; 8259 return true; 8260 case AMDGPU::S_CMPK_EQ_U32: 8261 case AMDGPU::S_CMPK_EQ_I32: 8262 case AMDGPU::S_CMPK_LG_U32: 8263 case AMDGPU::S_CMPK_LG_I32: 8264 case AMDGPU::S_CMPK_LT_U32: 8265 case AMDGPU::S_CMPK_LT_I32: 8266 case AMDGPU::S_CMPK_GT_U32: 8267 case AMDGPU::S_CMPK_GT_I32: 8268 case AMDGPU::S_CMPK_LE_U32: 8269 case AMDGPU::S_CMPK_LE_I32: 8270 case AMDGPU::S_CMPK_GE_U32: 8271 case AMDGPU::S_CMPK_GE_I32: 8272 SrcReg = MI.getOperand(0).getReg(); 8273 SrcReg2 = Register(); 8274 CmpValue = MI.getOperand(1).getImm(); 8275 CmpMask = ~0; 8276 return true; 8277 } 8278 8279 return false; 8280 } 8281 8282 bool SIInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg, 8283 Register SrcReg2, int64_t CmpMask, 8284 int64_t CmpValue, 8285 const MachineRegisterInfo *MRI) const { 8286 if (!SrcReg || SrcReg.isPhysical()) 8287 return false; 8288 8289 if (SrcReg2 && !getFoldableImm(SrcReg2, *MRI, CmpValue)) 8290 return false; 8291 8292 const auto optimizeCmpAnd = [&CmpInstr, SrcReg, CmpValue, MRI, 8293 this](int64_t ExpectedValue, unsigned SrcSize, 8294 bool IsReversible, bool IsSigned) -> bool { 8295 // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 8296 // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 8297 // s_cmp_ge_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 8298 // s_cmp_ge_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 8299 // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 1 << n => s_and_b64 $src, 1 << n 8300 // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 8301 // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 8302 // s_cmp_gt_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 8303 // s_cmp_gt_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 8304 // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 0 => s_and_b64 $src, 1 << n 8305 // 8306 // Signed ge/gt are not used for the sign bit. 8307 // 8308 // If result of the AND is unused except in the compare: 8309 // s_and_b(32|64) $src, 1 << n => s_bitcmp1_b(32|64) $src, n 8310 // 8311 // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n 8312 // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n 8313 // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 0 => s_bitcmp0_b64 $src, n 8314 // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n 8315 // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n 8316 // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 1 << n => s_bitcmp0_b64 $src, n 8317 8318 MachineInstr *Def = MRI->getUniqueVRegDef(SrcReg); 8319 if (!Def || Def->getParent() != CmpInstr.getParent()) 8320 return false; 8321 8322 if (Def->getOpcode() != AMDGPU::S_AND_B32 && 8323 Def->getOpcode() != AMDGPU::S_AND_B64) 8324 return false; 8325 8326 int64_t Mask; 8327 const auto isMask = [&Mask, SrcSize](const MachineOperand *MO) -> bool { 8328 if (MO->isImm()) 8329 Mask = MO->getImm(); 8330 else if (!getFoldableImm(MO, Mask)) 8331 return false; 8332 Mask &= maxUIntN(SrcSize); 8333 return isPowerOf2_64(Mask); 8334 }; 8335 8336 MachineOperand *SrcOp = &Def->getOperand(1); 8337 if (isMask(SrcOp)) 8338 SrcOp = &Def->getOperand(2); 8339 else if (isMask(&Def->getOperand(2))) 8340 SrcOp = &Def->getOperand(1); 8341 else 8342 return false; 8343 8344 unsigned BitNo = countTrailingZeros((uint64_t)Mask); 8345 if (IsSigned && BitNo == SrcSize - 1) 8346 return false; 8347 8348 ExpectedValue <<= BitNo; 8349 8350 bool IsReversedCC = false; 8351 if (CmpValue != ExpectedValue) { 8352 if (!IsReversible) 8353 return false; 8354 IsReversedCC = CmpValue == (ExpectedValue ^ Mask); 8355 if (!IsReversedCC) 8356 return false; 8357 } 8358 8359 Register DefReg = Def->getOperand(0).getReg(); 8360 if (IsReversedCC && !MRI->hasOneNonDBGUse(DefReg)) 8361 return false; 8362 8363 for (auto I = std::next(Def->getIterator()), E = CmpInstr.getIterator(); 8364 I != E; ++I) { 8365 if (I->modifiesRegister(AMDGPU::SCC, &RI) || 8366 I->killsRegister(AMDGPU::SCC, &RI)) 8367 return false; 8368 } 8369 8370 MachineOperand *SccDef = Def->findRegisterDefOperand(AMDGPU::SCC); 8371 SccDef->setIsDead(false); 8372 CmpInstr.eraseFromParent(); 8373 8374 if (!MRI->use_nodbg_empty(DefReg)) { 8375 assert(!IsReversedCC); 8376 return true; 8377 } 8378 8379 // Replace AND with unused result with a S_BITCMP. 8380 MachineBasicBlock *MBB = Def->getParent(); 8381 8382 unsigned NewOpc = (SrcSize == 32) ? IsReversedCC ? AMDGPU::S_BITCMP0_B32 8383 : AMDGPU::S_BITCMP1_B32 8384 : IsReversedCC ? AMDGPU::S_BITCMP0_B64 8385 : AMDGPU::S_BITCMP1_B64; 8386 8387 BuildMI(*MBB, Def, Def->getDebugLoc(), get(NewOpc)) 8388 .add(*SrcOp) 8389 .addImm(BitNo); 8390 Def->eraseFromParent(); 8391 8392 return true; 8393 }; 8394 8395 switch (CmpInstr.getOpcode()) { 8396 default: 8397 break; 8398 case AMDGPU::S_CMP_EQ_U32: 8399 case AMDGPU::S_CMP_EQ_I32: 8400 case AMDGPU::S_CMPK_EQ_U32: 8401 case AMDGPU::S_CMPK_EQ_I32: 8402 return optimizeCmpAnd(1, 32, true, false); 8403 case AMDGPU::S_CMP_GE_U32: 8404 case AMDGPU::S_CMPK_GE_U32: 8405 return optimizeCmpAnd(1, 32, false, false); 8406 case AMDGPU::S_CMP_GE_I32: 8407 case AMDGPU::S_CMPK_GE_I32: 8408 return optimizeCmpAnd(1, 32, false, true); 8409 case AMDGPU::S_CMP_EQ_U64: 8410 return optimizeCmpAnd(1, 64, true, false); 8411 case AMDGPU::S_CMP_LG_U32: 8412 case AMDGPU::S_CMP_LG_I32: 8413 case AMDGPU::S_CMPK_LG_U32: 8414 case AMDGPU::S_CMPK_LG_I32: 8415 return optimizeCmpAnd(0, 32, true, false); 8416 case AMDGPU::S_CMP_GT_U32: 8417 case AMDGPU::S_CMPK_GT_U32: 8418 return optimizeCmpAnd(0, 32, false, false); 8419 case AMDGPU::S_CMP_GT_I32: 8420 case AMDGPU::S_CMPK_GT_I32: 8421 return optimizeCmpAnd(0, 32, false, true); 8422 case AMDGPU::S_CMP_LG_U64: 8423 return optimizeCmpAnd(0, 64, true, false); 8424 } 8425 8426 return false; 8427 } 8428