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