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