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