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