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