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