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