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