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