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