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