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