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