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