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