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