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