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