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