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