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