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