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