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