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