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