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 case AMDGPU::OPERAND_REG_IMM_V2FP32: 4055 break; 4056 case AMDGPU::OPERAND_REG_INLINE_C_INT32: 4057 case AMDGPU::OPERAND_REG_INLINE_C_FP32: 4058 case AMDGPU::OPERAND_REG_INLINE_C_INT64: 4059 case AMDGPU::OPERAND_REG_INLINE_C_FP64: 4060 case AMDGPU::OPERAND_REG_INLINE_C_INT16: 4061 case AMDGPU::OPERAND_REG_INLINE_C_FP16: 4062 case AMDGPU::OPERAND_REG_INLINE_AC_INT32: 4063 case AMDGPU::OPERAND_REG_INLINE_AC_FP32: 4064 case AMDGPU::OPERAND_REG_INLINE_AC_INT16: 4065 case AMDGPU::OPERAND_REG_INLINE_AC_FP16: 4066 case AMDGPU::OPERAND_REG_INLINE_AC_FP64: { 4067 if (!MO.isReg() && (!MO.isImm() || !isInlineConstant(MI, i))) { 4068 ErrInfo = "Illegal immediate value for operand."; 4069 return false; 4070 } 4071 break; 4072 } 4073 case MCOI::OPERAND_IMMEDIATE: 4074 case AMDGPU::OPERAND_KIMM32: 4075 // Check if this operand is an immediate. 4076 // FrameIndex operands will be replaced by immediates, so they are 4077 // allowed. 4078 if (!MI.getOperand(i).isImm() && !MI.getOperand(i).isFI()) { 4079 ErrInfo = "Expected immediate, but got non-immediate"; 4080 return false; 4081 } 4082 LLVM_FALLTHROUGH; 4083 default: 4084 continue; 4085 } 4086 4087 if (!MO.isReg()) 4088 continue; 4089 Register Reg = MO.getReg(); 4090 if (!Reg) 4091 continue; 4092 4093 // FIXME: Ideally we would have separate instruction definitions with the 4094 // aligned register constraint. 4095 // FIXME: We do not verify inline asm operands, but custom inline asm 4096 // verification is broken anyway 4097 if (ST.needsAlignedVGPRs()) { 4098 const TargetRegisterClass *RC = RI.getRegClassForReg(MRI, Reg); 4099 if (RI.hasVectorRegisters(RC) && MO.getSubReg()) { 4100 const TargetRegisterClass *SubRC = 4101 RI.getSubRegClass(RC, MO.getSubReg()); 4102 RC = RI.getCompatibleSubRegClass(RC, SubRC, MO.getSubReg()); 4103 if (RC) 4104 RC = SubRC; 4105 } 4106 4107 // Check that this is the aligned version of the class. 4108 if (!RC || !RI.isProperlyAlignedRC(*RC)) { 4109 ErrInfo = "Subtarget requires even aligned vector registers"; 4110 return false; 4111 } 4112 } 4113 4114 if (RegClass != -1) { 4115 if (Reg.isVirtual()) 4116 continue; 4117 4118 const TargetRegisterClass *RC = RI.getRegClass(RegClass); 4119 if (!RC->contains(Reg)) { 4120 ErrInfo = "Operand has incorrect register class."; 4121 return false; 4122 } 4123 } 4124 } 4125 4126 // Verify SDWA 4127 if (isSDWA(MI)) { 4128 if (!ST.hasSDWA()) { 4129 ErrInfo = "SDWA is not supported on this target"; 4130 return false; 4131 } 4132 4133 int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst); 4134 4135 const int OpIndices[] = {DstIdx, Src0Idx, Src1Idx, Src2Idx}; 4136 4137 for (int OpIdx : OpIndices) { 4138 if (OpIdx == -1) 4139 continue; 4140 const MachineOperand &MO = MI.getOperand(OpIdx); 4141 4142 if (!ST.hasSDWAScalar()) { 4143 // Only VGPRS on VI 4144 if (!MO.isReg() || !RI.hasVGPRs(RI.getRegClassForReg(MRI, MO.getReg()))) { 4145 ErrInfo = "Only VGPRs allowed as operands in SDWA instructions on VI"; 4146 return false; 4147 } 4148 } else { 4149 // No immediates on GFX9 4150 if (!MO.isReg()) { 4151 ErrInfo = 4152 "Only reg allowed as operands in SDWA instructions on GFX9+"; 4153 return false; 4154 } 4155 } 4156 } 4157 4158 if (!ST.hasSDWAOmod()) { 4159 // No omod allowed on VI 4160 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod); 4161 if (OMod != nullptr && 4162 (!OMod->isImm() || OMod->getImm() != 0)) { 4163 ErrInfo = "OMod not allowed in SDWA instructions on VI"; 4164 return false; 4165 } 4166 } 4167 4168 uint16_t BasicOpcode = AMDGPU::getBasicFromSDWAOp(Opcode); 4169 if (isVOPC(BasicOpcode)) { 4170 if (!ST.hasSDWASdst() && DstIdx != -1) { 4171 // Only vcc allowed as dst on VI for VOPC 4172 const MachineOperand &Dst = MI.getOperand(DstIdx); 4173 if (!Dst.isReg() || Dst.getReg() != AMDGPU::VCC) { 4174 ErrInfo = "Only VCC allowed as dst in SDWA instructions on VI"; 4175 return false; 4176 } 4177 } else if (!ST.hasSDWAOutModsVOPC()) { 4178 // No clamp allowed on GFX9 for VOPC 4179 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp); 4180 if (Clamp && (!Clamp->isImm() || Clamp->getImm() != 0)) { 4181 ErrInfo = "Clamp not allowed in VOPC SDWA instructions on VI"; 4182 return false; 4183 } 4184 4185 // No omod allowed on GFX9 for VOPC 4186 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod); 4187 if (OMod && (!OMod->isImm() || OMod->getImm() != 0)) { 4188 ErrInfo = "OMod not allowed in VOPC SDWA instructions on VI"; 4189 return false; 4190 } 4191 } 4192 } 4193 4194 const MachineOperand *DstUnused = getNamedOperand(MI, AMDGPU::OpName::dst_unused); 4195 if (DstUnused && DstUnused->isImm() && 4196 DstUnused->getImm() == AMDGPU::SDWA::UNUSED_PRESERVE) { 4197 const MachineOperand &Dst = MI.getOperand(DstIdx); 4198 if (!Dst.isReg() || !Dst.isTied()) { 4199 ErrInfo = "Dst register should have tied register"; 4200 return false; 4201 } 4202 4203 const MachineOperand &TiedMO = 4204 MI.getOperand(MI.findTiedOperandIdx(DstIdx)); 4205 if (!TiedMO.isReg() || !TiedMO.isImplicit() || !TiedMO.isUse()) { 4206 ErrInfo = 4207 "Dst register should be tied to implicit use of preserved register"; 4208 return false; 4209 } else if (TiedMO.getReg().isPhysical() && 4210 Dst.getReg() != TiedMO.getReg()) { 4211 ErrInfo = "Dst register should use same physical register as preserved"; 4212 return false; 4213 } 4214 } 4215 } 4216 4217 // Verify MIMG 4218 if (isMIMG(MI.getOpcode()) && !MI.mayStore()) { 4219 // Ensure that the return type used is large enough for all the options 4220 // being used TFE/LWE require an extra result register. 4221 const MachineOperand *DMask = getNamedOperand(MI, AMDGPU::OpName::dmask); 4222 if (DMask) { 4223 uint64_t DMaskImm = DMask->getImm(); 4224 uint32_t RegCount = 4225 isGather4(MI.getOpcode()) ? 4 : countPopulation(DMaskImm); 4226 const MachineOperand *TFE = getNamedOperand(MI, AMDGPU::OpName::tfe); 4227 const MachineOperand *LWE = getNamedOperand(MI, AMDGPU::OpName::lwe); 4228 const MachineOperand *D16 = getNamedOperand(MI, AMDGPU::OpName::d16); 4229 4230 // Adjust for packed 16 bit values 4231 if (D16 && D16->getImm() && !ST.hasUnpackedD16VMem()) 4232 RegCount >>= 1; 4233 4234 // Adjust if using LWE or TFE 4235 if ((LWE && LWE->getImm()) || (TFE && TFE->getImm())) 4236 RegCount += 1; 4237 4238 const uint32_t DstIdx = 4239 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata); 4240 const MachineOperand &Dst = MI.getOperand(DstIdx); 4241 if (Dst.isReg()) { 4242 const TargetRegisterClass *DstRC = getOpRegClass(MI, DstIdx); 4243 uint32_t DstSize = RI.getRegSizeInBits(*DstRC) / 32; 4244 if (RegCount > DstSize) { 4245 ErrInfo = "MIMG instruction returns too many registers for dst " 4246 "register class"; 4247 return false; 4248 } 4249 } 4250 } 4251 } 4252 4253 // Verify VOP*. Ignore multiple sgpr operands on writelane. 4254 if (Desc.getOpcode() != AMDGPU::V_WRITELANE_B32 4255 && (isVOP1(MI) || isVOP2(MI) || isVOP3(MI) || isVOPC(MI) || isSDWA(MI))) { 4256 // Only look at the true operands. Only a real operand can use the constant 4257 // bus, and we don't want to check pseudo-operands like the source modifier 4258 // flags. 4259 const int OpIndices[] = { Src0Idx, Src1Idx, Src2Idx }; 4260 4261 unsigned ConstantBusCount = 0; 4262 bool UsesLiteral = false; 4263 const MachineOperand *LiteralVal = nullptr; 4264 4265 if (AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm) != -1) 4266 ++ConstantBusCount; 4267 4268 SmallVector<Register, 2> SGPRsUsed; 4269 Register SGPRUsed; 4270 4271 for (int OpIdx : OpIndices) { 4272 if (OpIdx == -1) 4273 break; 4274 const MachineOperand &MO = MI.getOperand(OpIdx); 4275 if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) { 4276 if (MO.isReg()) { 4277 SGPRUsed = MO.getReg(); 4278 if (llvm::all_of(SGPRsUsed, [SGPRUsed](unsigned SGPR) { 4279 return SGPRUsed != SGPR; 4280 })) { 4281 ++ConstantBusCount; 4282 SGPRsUsed.push_back(SGPRUsed); 4283 } 4284 } else { 4285 if (!UsesLiteral) { 4286 ++ConstantBusCount; 4287 UsesLiteral = true; 4288 LiteralVal = &MO; 4289 } else if (!MO.isIdenticalTo(*LiteralVal)) { 4290 assert(isVOP3(MI)); 4291 ErrInfo = "VOP3 instruction uses more than one literal"; 4292 return false; 4293 } 4294 } 4295 } 4296 } 4297 4298 SGPRUsed = findImplicitSGPRRead(MI); 4299 if (SGPRUsed != AMDGPU::NoRegister) { 4300 // Implicit uses may safely overlap true operands 4301 if (llvm::all_of(SGPRsUsed, [this, SGPRUsed](unsigned SGPR) { 4302 return !RI.regsOverlap(SGPRUsed, SGPR); 4303 })) { 4304 ++ConstantBusCount; 4305 SGPRsUsed.push_back(SGPRUsed); 4306 } 4307 } 4308 4309 // v_writelane_b32 is an exception from constant bus restriction: 4310 // vsrc0 can be sgpr, const or m0 and lane select sgpr, m0 or inline-const 4311 if (ConstantBusCount > ST.getConstantBusLimit(Opcode) && 4312 Opcode != AMDGPU::V_WRITELANE_B32) { 4313 ErrInfo = "VOP* instruction violates constant bus restriction"; 4314 return false; 4315 } 4316 4317 if (isVOP3(MI) && UsesLiteral && !ST.hasVOP3Literal()) { 4318 ErrInfo = "VOP3 instruction uses literal"; 4319 return false; 4320 } 4321 } 4322 4323 // Special case for writelane - this can break the multiple constant bus rule, 4324 // but still can't use more than one SGPR register 4325 if (Desc.getOpcode() == AMDGPU::V_WRITELANE_B32) { 4326 unsigned SGPRCount = 0; 4327 Register SGPRUsed = AMDGPU::NoRegister; 4328 4329 for (int OpIdx : {Src0Idx, Src1Idx, Src2Idx}) { 4330 if (OpIdx == -1) 4331 break; 4332 4333 const MachineOperand &MO = MI.getOperand(OpIdx); 4334 4335 if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) { 4336 if (MO.isReg() && MO.getReg() != AMDGPU::M0) { 4337 if (MO.getReg() != SGPRUsed) 4338 ++SGPRCount; 4339 SGPRUsed = MO.getReg(); 4340 } 4341 } 4342 if (SGPRCount > ST.getConstantBusLimit(Opcode)) { 4343 ErrInfo = "WRITELANE instruction violates constant bus restriction"; 4344 return false; 4345 } 4346 } 4347 } 4348 4349 // Verify misc. restrictions on specific instructions. 4350 if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32_e64 || 4351 Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64_e64) { 4352 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 4353 const MachineOperand &Src1 = MI.getOperand(Src1Idx); 4354 const MachineOperand &Src2 = MI.getOperand(Src2Idx); 4355 if (Src0.isReg() && Src1.isReg() && Src2.isReg()) { 4356 if (!compareMachineOp(Src0, Src1) && 4357 !compareMachineOp(Src0, Src2)) { 4358 ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2"; 4359 return false; 4360 } 4361 } 4362 if ((getNamedOperand(MI, AMDGPU::OpName::src0_modifiers)->getImm() & 4363 SISrcMods::ABS) || 4364 (getNamedOperand(MI, AMDGPU::OpName::src1_modifiers)->getImm() & 4365 SISrcMods::ABS) || 4366 (getNamedOperand(MI, AMDGPU::OpName::src2_modifiers)->getImm() & 4367 SISrcMods::ABS)) { 4368 ErrInfo = "ABS not allowed in VOP3B instructions"; 4369 return false; 4370 } 4371 } 4372 4373 if (isSOP2(MI) || isSOPC(MI)) { 4374 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 4375 const MachineOperand &Src1 = MI.getOperand(Src1Idx); 4376 unsigned Immediates = 0; 4377 4378 if (!Src0.isReg() && 4379 !isInlineConstant(Src0, Desc.OpInfo[Src0Idx].OperandType)) 4380 Immediates++; 4381 if (!Src1.isReg() && 4382 !isInlineConstant(Src1, Desc.OpInfo[Src1Idx].OperandType)) 4383 Immediates++; 4384 4385 if (Immediates > 1) { 4386 ErrInfo = "SOP2/SOPC instruction requires too many immediate constants"; 4387 return false; 4388 } 4389 } 4390 4391 if (isSOPK(MI)) { 4392 auto Op = getNamedOperand(MI, AMDGPU::OpName::simm16); 4393 if (Desc.isBranch()) { 4394 if (!Op->isMBB()) { 4395 ErrInfo = "invalid branch target for SOPK instruction"; 4396 return false; 4397 } 4398 } else { 4399 uint64_t Imm = Op->getImm(); 4400 if (sopkIsZext(MI)) { 4401 if (!isUInt<16>(Imm)) { 4402 ErrInfo = "invalid immediate for SOPK instruction"; 4403 return false; 4404 } 4405 } else { 4406 if (!isInt<16>(Imm)) { 4407 ErrInfo = "invalid immediate for SOPK instruction"; 4408 return false; 4409 } 4410 } 4411 } 4412 } 4413 4414 if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 || 4415 Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 || 4416 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 4417 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) { 4418 const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 4419 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64; 4420 4421 const unsigned StaticNumOps = Desc.getNumOperands() + 4422 Desc.getNumImplicitUses(); 4423 const unsigned NumImplicitOps = IsDst ? 2 : 1; 4424 4425 // Allow additional implicit operands. This allows a fixup done by the post 4426 // RA scheduler where the main implicit operand is killed and implicit-defs 4427 // are added for sub-registers that remain live after this instruction. 4428 if (MI.getNumOperands() < StaticNumOps + NumImplicitOps) { 4429 ErrInfo = "missing implicit register operands"; 4430 return false; 4431 } 4432 4433 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 4434 if (IsDst) { 4435 if (!Dst->isUse()) { 4436 ErrInfo = "v_movreld_b32 vdst should be a use operand"; 4437 return false; 4438 } 4439 4440 unsigned UseOpIdx; 4441 if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) || 4442 UseOpIdx != StaticNumOps + 1) { 4443 ErrInfo = "movrel implicit operands should be tied"; 4444 return false; 4445 } 4446 } 4447 4448 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 4449 const MachineOperand &ImpUse 4450 = MI.getOperand(StaticNumOps + NumImplicitOps - 1); 4451 if (!ImpUse.isReg() || !ImpUse.isUse() || 4452 !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) { 4453 ErrInfo = "src0 should be subreg of implicit vector use"; 4454 return false; 4455 } 4456 } 4457 4458 // Make sure we aren't losing exec uses in the td files. This mostly requires 4459 // being careful when using let Uses to try to add other use registers. 4460 if (shouldReadExec(MI)) { 4461 if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) { 4462 ErrInfo = "VALU instruction does not implicitly read exec mask"; 4463 return false; 4464 } 4465 } 4466 4467 if (isSMRD(MI)) { 4468 if (MI.mayStore()) { 4469 // The register offset form of scalar stores may only use m0 as the 4470 // soffset register. 4471 const MachineOperand *Soff = getNamedOperand(MI, AMDGPU::OpName::soff); 4472 if (Soff && Soff->getReg() != AMDGPU::M0) { 4473 ErrInfo = "scalar stores must use m0 as offset register"; 4474 return false; 4475 } 4476 } 4477 } 4478 4479 if (isFLAT(MI) && !ST.hasFlatInstOffsets()) { 4480 const MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 4481 if (Offset->getImm() != 0) { 4482 ErrInfo = "subtarget does not support offsets in flat instructions"; 4483 return false; 4484 } 4485 } 4486 4487 if (isMIMG(MI)) { 4488 const MachineOperand *DimOp = getNamedOperand(MI, AMDGPU::OpName::dim); 4489 if (DimOp) { 4490 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opcode, 4491 AMDGPU::OpName::vaddr0); 4492 int SRsrcIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::srsrc); 4493 const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(Opcode); 4494 const AMDGPU::MIMGBaseOpcodeInfo *BaseOpcode = 4495 AMDGPU::getMIMGBaseOpcodeInfo(Info->BaseOpcode); 4496 const AMDGPU::MIMGDimInfo *Dim = 4497 AMDGPU::getMIMGDimInfoByEncoding(DimOp->getImm()); 4498 4499 if (!Dim) { 4500 ErrInfo = "dim is out of range"; 4501 return false; 4502 } 4503 4504 bool IsA16 = false; 4505 if (ST.hasR128A16()) { 4506 const MachineOperand *R128A16 = getNamedOperand(MI, AMDGPU::OpName::r128); 4507 IsA16 = R128A16->getImm() != 0; 4508 } else if (ST.hasGFX10A16()) { 4509 const MachineOperand *A16 = getNamedOperand(MI, AMDGPU::OpName::a16); 4510 IsA16 = A16->getImm() != 0; 4511 } 4512 4513 bool IsNSA = SRsrcIdx - VAddr0Idx > 1; 4514 4515 unsigned AddrWords = 4516 AMDGPU::getAddrSizeMIMGOp(BaseOpcode, Dim, IsA16, ST.hasG16()); 4517 4518 unsigned VAddrWords; 4519 if (IsNSA) { 4520 VAddrWords = SRsrcIdx - VAddr0Idx; 4521 } else { 4522 const TargetRegisterClass *RC = getOpRegClass(MI, VAddr0Idx); 4523 VAddrWords = MRI.getTargetRegisterInfo()->getRegSizeInBits(*RC) / 32; 4524 if (AddrWords > 8) 4525 AddrWords = 16; 4526 } 4527 4528 if (VAddrWords != AddrWords) { 4529 LLVM_DEBUG(dbgs() << "bad vaddr size, expected " << AddrWords 4530 << " but got " << VAddrWords << "\n"); 4531 ErrInfo = "bad vaddr size"; 4532 return false; 4533 } 4534 } 4535 } 4536 4537 const MachineOperand *DppCt = getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl); 4538 if (DppCt) { 4539 using namespace AMDGPU::DPP; 4540 4541 unsigned DC = DppCt->getImm(); 4542 if (DC == DppCtrl::DPP_UNUSED1 || DC == DppCtrl::DPP_UNUSED2 || 4543 DC == DppCtrl::DPP_UNUSED3 || DC > DppCtrl::DPP_LAST || 4544 (DC >= DppCtrl::DPP_UNUSED4_FIRST && DC <= DppCtrl::DPP_UNUSED4_LAST) || 4545 (DC >= DppCtrl::DPP_UNUSED5_FIRST && DC <= DppCtrl::DPP_UNUSED5_LAST) || 4546 (DC >= DppCtrl::DPP_UNUSED6_FIRST && DC <= DppCtrl::DPP_UNUSED6_LAST) || 4547 (DC >= DppCtrl::DPP_UNUSED7_FIRST && DC <= DppCtrl::DPP_UNUSED7_LAST) || 4548 (DC >= DppCtrl::DPP_UNUSED8_FIRST && DC <= DppCtrl::DPP_UNUSED8_LAST)) { 4549 ErrInfo = "Invalid dpp_ctrl value"; 4550 return false; 4551 } 4552 if (DC >= DppCtrl::WAVE_SHL1 && DC <= DppCtrl::WAVE_ROR1 && 4553 ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 4554 ErrInfo = "Invalid dpp_ctrl value: " 4555 "wavefront shifts are not supported on GFX10+"; 4556 return false; 4557 } 4558 if (DC >= DppCtrl::BCAST15 && DC <= DppCtrl::BCAST31 && 4559 ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 4560 ErrInfo = "Invalid dpp_ctrl value: " 4561 "broadcasts are not supported on GFX10+"; 4562 return false; 4563 } 4564 if (DC >= DppCtrl::ROW_SHARE_FIRST && DC <= DppCtrl::ROW_XMASK_LAST && 4565 ST.getGeneration() < AMDGPUSubtarget::GFX10) { 4566 if (DC >= DppCtrl::ROW_NEWBCAST_FIRST && 4567 DC <= DppCtrl::ROW_NEWBCAST_LAST && 4568 !ST.hasGFX90AInsts()) { 4569 ErrInfo = "Invalid dpp_ctrl value: " 4570 "row_newbroadcast/row_share is not supported before " 4571 "GFX90A/GFX10"; 4572 return false; 4573 } else if (DC > DppCtrl::ROW_NEWBCAST_LAST || !ST.hasGFX90AInsts()) { 4574 ErrInfo = "Invalid dpp_ctrl value: " 4575 "row_share and row_xmask are not supported before GFX10"; 4576 return false; 4577 } 4578 } 4579 4580 int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst); 4581 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0); 4582 4583 if (Opcode != AMDGPU::V_MOV_B64_DPP_PSEUDO && 4584 ((DstIdx >= 0 && 4585 (Desc.OpInfo[DstIdx].RegClass == AMDGPU::VReg_64RegClassID || 4586 Desc.OpInfo[DstIdx].RegClass == AMDGPU::VReg_64_Align2RegClassID)) || 4587 ((Src0Idx >= 0 && 4588 (Desc.OpInfo[Src0Idx].RegClass == AMDGPU::VReg_64RegClassID || 4589 Desc.OpInfo[Src0Idx].RegClass == 4590 AMDGPU::VReg_64_Align2RegClassID)))) && 4591 !AMDGPU::isLegal64BitDPPControl(DC)) { 4592 ErrInfo = "Invalid dpp_ctrl value: " 4593 "64 bit dpp only support row_newbcast"; 4594 return false; 4595 } 4596 } 4597 4598 if ((MI.mayStore() || MI.mayLoad()) && !isVGPRSpill(MI)) { 4599 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 4600 uint16_t DataNameIdx = isDS(Opcode) ? AMDGPU::OpName::data0 4601 : AMDGPU::OpName::vdata; 4602 const MachineOperand *Data = getNamedOperand(MI, DataNameIdx); 4603 const MachineOperand *Data2 = getNamedOperand(MI, AMDGPU::OpName::data1); 4604 if (Data && !Data->isReg()) 4605 Data = nullptr; 4606 4607 if (ST.hasGFX90AInsts()) { 4608 if (Dst && Data && 4609 (RI.isAGPR(MRI, Dst->getReg()) != RI.isAGPR(MRI, Data->getReg()))) { 4610 ErrInfo = "Invalid register class: " 4611 "vdata and vdst should be both VGPR or AGPR"; 4612 return false; 4613 } 4614 if (Data && Data2 && 4615 (RI.isAGPR(MRI, Data->getReg()) != RI.isAGPR(MRI, Data2->getReg()))) { 4616 ErrInfo = "Invalid register class: " 4617 "both data operands should be VGPR or AGPR"; 4618 return false; 4619 } 4620 } else { 4621 if ((Dst && RI.isAGPR(MRI, Dst->getReg())) || 4622 (Data && RI.isAGPR(MRI, Data->getReg())) || 4623 (Data2 && RI.isAGPR(MRI, Data2->getReg()))) { 4624 ErrInfo = "Invalid register class: " 4625 "agpr loads and stores not supported on this GPU"; 4626 return false; 4627 } 4628 } 4629 } 4630 4631 if (ST.needsAlignedVGPRs() && 4632 (MI.getOpcode() == AMDGPU::DS_GWS_INIT || 4633 MI.getOpcode() == AMDGPU::DS_GWS_SEMA_BR || 4634 MI.getOpcode() == AMDGPU::DS_GWS_BARRIER)) { 4635 const MachineOperand *Op = getNamedOperand(MI, AMDGPU::OpName::data0); 4636 Register Reg = Op->getReg(); 4637 bool Aligned = true; 4638 if (Reg.isPhysical()) { 4639 Aligned = !(RI.getHWRegIndex(Reg) & 1); 4640 } else { 4641 const TargetRegisterClass &RC = *MRI.getRegClass(Reg); 4642 Aligned = RI.getRegSizeInBits(RC) > 32 && RI.isProperlyAlignedRC(RC) && 4643 !(RI.getChannelFromSubReg(Op->getSubReg()) & 1); 4644 } 4645 4646 if (!Aligned) { 4647 ErrInfo = "Subtarget requires even aligned vector registers " 4648 "for DS_GWS instructions"; 4649 return false; 4650 } 4651 } 4652 4653 if (Desc.getOpcode() == AMDGPU::G_AMDGPU_WAVE_ADDRESS) { 4654 const MachineOperand &SrcOp = MI.getOperand(1); 4655 if (!SrcOp.isReg() || SrcOp.getReg().isVirtual()) { 4656 ErrInfo = "pseudo expects only physical SGPRs"; 4657 return false; 4658 } 4659 } 4660 4661 return true; 4662 } 4663 4664 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) const { 4665 switch (MI.getOpcode()) { 4666 default: return AMDGPU::INSTRUCTION_LIST_END; 4667 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE; 4668 case AMDGPU::COPY: return AMDGPU::COPY; 4669 case AMDGPU::PHI: return AMDGPU::PHI; 4670 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG; 4671 case AMDGPU::WQM: return AMDGPU::WQM; 4672 case AMDGPU::SOFT_WQM: return AMDGPU::SOFT_WQM; 4673 case AMDGPU::STRICT_WWM: return AMDGPU::STRICT_WWM; 4674 case AMDGPU::STRICT_WQM: return AMDGPU::STRICT_WQM; 4675 case AMDGPU::S_MOV_B32: { 4676 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 4677 return MI.getOperand(1).isReg() || 4678 RI.isAGPR(MRI, MI.getOperand(0).getReg()) ? 4679 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32; 4680 } 4681 case AMDGPU::S_ADD_I32: 4682 return ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_CO_U32_e32; 4683 case AMDGPU::S_ADDC_U32: 4684 return AMDGPU::V_ADDC_U32_e32; 4685 case AMDGPU::S_SUB_I32: 4686 return ST.hasAddNoCarry() ? AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_CO_U32_e32; 4687 // FIXME: These are not consistently handled, and selected when the carry is 4688 // used. 4689 case AMDGPU::S_ADD_U32: 4690 return AMDGPU::V_ADD_CO_U32_e32; 4691 case AMDGPU::S_SUB_U32: 4692 return AMDGPU::V_SUB_CO_U32_e32; 4693 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32; 4694 case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_U32_e64; 4695 case AMDGPU::S_MUL_HI_U32: return AMDGPU::V_MUL_HI_U32_e64; 4696 case AMDGPU::S_MUL_HI_I32: return AMDGPU::V_MUL_HI_I32_e64; 4697 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64; 4698 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64; 4699 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64; 4700 case AMDGPU::S_XNOR_B32: 4701 return ST.hasDLInsts() ? AMDGPU::V_XNOR_B32_e64 : AMDGPU::INSTRUCTION_LIST_END; 4702 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64; 4703 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64; 4704 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64; 4705 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64; 4706 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32; 4707 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64_e64; 4708 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32; 4709 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64_e64; 4710 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32; 4711 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64_e64; 4712 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32_e64; 4713 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32_e64; 4714 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32_e64; 4715 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32_e64; 4716 case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64; 4717 case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32; 4718 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32; 4719 case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32; 4720 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e64; 4721 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e64; 4722 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e64; 4723 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e64; 4724 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e64; 4725 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e64; 4726 case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e64; 4727 case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e64; 4728 case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e64; 4729 case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e64; 4730 case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e64; 4731 case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e64; 4732 case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e64; 4733 case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e64; 4734 case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64; 4735 case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32; 4736 case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32; 4737 case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64; 4738 case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ; 4739 case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ; 4740 } 4741 llvm_unreachable( 4742 "Unexpected scalar opcode without corresponding vector one!"); 4743 } 4744 4745 static const TargetRegisterClass * 4746 adjustAllocatableRegClass(const GCNSubtarget &ST, const SIRegisterInfo &RI, 4747 const MachineRegisterInfo &MRI, 4748 const MCInstrDesc &TID, unsigned RCID, 4749 bool IsAllocatable) { 4750 if ((IsAllocatable || !ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) && 4751 (((TID.mayLoad() || TID.mayStore()) && 4752 !(TID.TSFlags & SIInstrFlags::VGPRSpill)) || 4753 (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::MIMG)))) { 4754 switch (RCID) { 4755 case AMDGPU::AV_32RegClassID: 4756 RCID = AMDGPU::VGPR_32RegClassID; 4757 break; 4758 case AMDGPU::AV_64RegClassID: 4759 RCID = AMDGPU::VReg_64RegClassID; 4760 break; 4761 case AMDGPU::AV_96RegClassID: 4762 RCID = AMDGPU::VReg_96RegClassID; 4763 break; 4764 case AMDGPU::AV_128RegClassID: 4765 RCID = AMDGPU::VReg_128RegClassID; 4766 break; 4767 case AMDGPU::AV_160RegClassID: 4768 RCID = AMDGPU::VReg_160RegClassID; 4769 break; 4770 default: 4771 break; 4772 } 4773 } 4774 4775 return RI.getProperlyAlignedRC(RI.getRegClass(RCID)); 4776 } 4777 4778 const TargetRegisterClass *SIInstrInfo::getRegClass(const MCInstrDesc &TID, 4779 unsigned OpNum, const TargetRegisterInfo *TRI, 4780 const MachineFunction &MF) 4781 const { 4782 if (OpNum >= TID.getNumOperands()) 4783 return nullptr; 4784 auto RegClass = TID.OpInfo[OpNum].RegClass; 4785 bool IsAllocatable = false; 4786 if (TID.TSFlags & (SIInstrFlags::DS | SIInstrFlags::FLAT)) { 4787 // vdst and vdata should be both VGPR or AGPR, same for the DS instructions 4788 // with two data operands. Request register class constrained to VGPR only 4789 // of both operands present as Machine Copy Propagation can not check this 4790 // constraint and possibly other passes too. 4791 // 4792 // The check is limited to FLAT and DS because atomics in non-flat encoding 4793 // have their vdst and vdata tied to be the same register. 4794 const int VDstIdx = AMDGPU::getNamedOperandIdx(TID.Opcode, 4795 AMDGPU::OpName::vdst); 4796 const int DataIdx = AMDGPU::getNamedOperandIdx(TID.Opcode, 4797 (TID.TSFlags & SIInstrFlags::DS) ? AMDGPU::OpName::data0 4798 : AMDGPU::OpName::vdata); 4799 if (DataIdx != -1) { 4800 IsAllocatable = VDstIdx != -1 || 4801 AMDGPU::getNamedOperandIdx(TID.Opcode, 4802 AMDGPU::OpName::data1) != -1; 4803 } 4804 } 4805 return adjustAllocatableRegClass(ST, RI, MF.getRegInfo(), TID, RegClass, 4806 IsAllocatable); 4807 } 4808 4809 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI, 4810 unsigned OpNo) const { 4811 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 4812 const MCInstrDesc &Desc = get(MI.getOpcode()); 4813 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() || 4814 Desc.OpInfo[OpNo].RegClass == -1) { 4815 Register Reg = MI.getOperand(OpNo).getReg(); 4816 4817 if (Reg.isVirtual()) 4818 return MRI.getRegClass(Reg); 4819 return RI.getPhysRegClass(Reg); 4820 } 4821 4822 unsigned RCID = Desc.OpInfo[OpNo].RegClass; 4823 return adjustAllocatableRegClass(ST, RI, MRI, Desc, RCID, true); 4824 } 4825 4826 void SIInstrInfo::legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const { 4827 MachineBasicBlock::iterator I = MI; 4828 MachineBasicBlock *MBB = MI.getParent(); 4829 MachineOperand &MO = MI.getOperand(OpIdx); 4830 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 4831 unsigned RCID = get(MI.getOpcode()).OpInfo[OpIdx].RegClass; 4832 const TargetRegisterClass *RC = RI.getRegClass(RCID); 4833 unsigned Size = RI.getRegSizeInBits(*RC); 4834 unsigned Opcode = (Size == 64) ? AMDGPU::V_MOV_B64_PSEUDO : AMDGPU::V_MOV_B32_e32; 4835 if (MO.isReg()) 4836 Opcode = AMDGPU::COPY; 4837 else if (RI.isSGPRClass(RC)) 4838 Opcode = (Size == 64) ? AMDGPU::S_MOV_B64 : AMDGPU::S_MOV_B32; 4839 4840 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC); 4841 const TargetRegisterClass *VRC64 = RI.getVGPR64Class(); 4842 if (RI.getCommonSubClass(VRC64, VRC)) 4843 VRC = VRC64; 4844 else 4845 VRC = &AMDGPU::VGPR_32RegClass; 4846 4847 Register Reg = MRI.createVirtualRegister(VRC); 4848 DebugLoc DL = MBB->findDebugLoc(I); 4849 BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).add(MO); 4850 MO.ChangeToRegister(Reg, false); 4851 } 4852 4853 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI, 4854 MachineRegisterInfo &MRI, 4855 MachineOperand &SuperReg, 4856 const TargetRegisterClass *SuperRC, 4857 unsigned SubIdx, 4858 const TargetRegisterClass *SubRC) 4859 const { 4860 MachineBasicBlock *MBB = MI->getParent(); 4861 DebugLoc DL = MI->getDebugLoc(); 4862 Register SubReg = MRI.createVirtualRegister(SubRC); 4863 4864 if (SuperReg.getSubReg() == AMDGPU::NoSubRegister) { 4865 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 4866 .addReg(SuperReg.getReg(), 0, SubIdx); 4867 return SubReg; 4868 } 4869 4870 // Just in case the super register is itself a sub-register, copy it to a new 4871 // value so we don't need to worry about merging its subreg index with the 4872 // SubIdx passed to this function. The register coalescer should be able to 4873 // eliminate this extra copy. 4874 Register NewSuperReg = MRI.createVirtualRegister(SuperRC); 4875 4876 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg) 4877 .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg()); 4878 4879 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 4880 .addReg(NewSuperReg, 0, SubIdx); 4881 4882 return SubReg; 4883 } 4884 4885 MachineOperand SIInstrInfo::buildExtractSubRegOrImm( 4886 MachineBasicBlock::iterator MII, 4887 MachineRegisterInfo &MRI, 4888 MachineOperand &Op, 4889 const TargetRegisterClass *SuperRC, 4890 unsigned SubIdx, 4891 const TargetRegisterClass *SubRC) const { 4892 if (Op.isImm()) { 4893 if (SubIdx == AMDGPU::sub0) 4894 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm())); 4895 if (SubIdx == AMDGPU::sub1) 4896 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32)); 4897 4898 llvm_unreachable("Unhandled register index for immediate"); 4899 } 4900 4901 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC, 4902 SubIdx, SubRC); 4903 return MachineOperand::CreateReg(SubReg, false); 4904 } 4905 4906 // Change the order of operands from (0, 1, 2) to (0, 2, 1) 4907 void SIInstrInfo::swapOperands(MachineInstr &Inst) const { 4908 assert(Inst.getNumExplicitOperands() == 3); 4909 MachineOperand Op1 = Inst.getOperand(1); 4910 Inst.removeOperand(1); 4911 Inst.addOperand(Op1); 4912 } 4913 4914 bool SIInstrInfo::isLegalRegOperand(const MachineRegisterInfo &MRI, 4915 const MCOperandInfo &OpInfo, 4916 const MachineOperand &MO) const { 4917 if (!MO.isReg()) 4918 return false; 4919 4920 Register Reg = MO.getReg(); 4921 4922 const TargetRegisterClass *DRC = RI.getRegClass(OpInfo.RegClass); 4923 if (Reg.isPhysical()) 4924 return DRC->contains(Reg); 4925 4926 const TargetRegisterClass *RC = MRI.getRegClass(Reg); 4927 4928 if (MO.getSubReg()) { 4929 const MachineFunction *MF = MO.getParent()->getParent()->getParent(); 4930 const TargetRegisterClass *SuperRC = RI.getLargestLegalSuperClass(RC, *MF); 4931 if (!SuperRC) 4932 return false; 4933 4934 DRC = RI.getMatchingSuperRegClass(SuperRC, DRC, MO.getSubReg()); 4935 if (!DRC) 4936 return false; 4937 } 4938 return RC->hasSuperClassEq(DRC); 4939 } 4940 4941 bool SIInstrInfo::isLegalVSrcOperand(const MachineRegisterInfo &MRI, 4942 const MCOperandInfo &OpInfo, 4943 const MachineOperand &MO) const { 4944 if (MO.isReg()) 4945 return isLegalRegOperand(MRI, OpInfo, MO); 4946 4947 // Handle non-register types that are treated like immediates. 4948 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI() || MO.isGlobal()); 4949 return true; 4950 } 4951 4952 bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx, 4953 const MachineOperand *MO) const { 4954 const MachineFunction &MF = *MI.getParent()->getParent(); 4955 const MachineRegisterInfo &MRI = MF.getRegInfo(); 4956 const MCInstrDesc &InstDesc = MI.getDesc(); 4957 const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx]; 4958 const TargetRegisterClass *DefinedRC = 4959 OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr; 4960 if (!MO) 4961 MO = &MI.getOperand(OpIdx); 4962 4963 int ConstantBusLimit = ST.getConstantBusLimit(MI.getOpcode()); 4964 int VOP3LiteralLimit = ST.hasVOP3Literal() ? 1 : 0; 4965 if (isVALU(MI) && usesConstantBus(MRI, *MO, OpInfo)) { 4966 if (isVOP3(MI) && isLiteralConstantLike(*MO, OpInfo) && !VOP3LiteralLimit--) 4967 return false; 4968 4969 SmallDenseSet<RegSubRegPair> SGPRsUsed; 4970 if (MO->isReg()) 4971 SGPRsUsed.insert(RegSubRegPair(MO->getReg(), MO->getSubReg())); 4972 4973 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 4974 if (i == OpIdx) 4975 continue; 4976 const MachineOperand &Op = MI.getOperand(i); 4977 if (Op.isReg()) { 4978 RegSubRegPair SGPR(Op.getReg(), Op.getSubReg()); 4979 if (!SGPRsUsed.count(SGPR) && 4980 usesConstantBus(MRI, Op, InstDesc.OpInfo[i])) { 4981 if (--ConstantBusLimit <= 0) 4982 return false; 4983 SGPRsUsed.insert(SGPR); 4984 } 4985 } else if (InstDesc.OpInfo[i].OperandType == AMDGPU::OPERAND_KIMM32) { 4986 if (--ConstantBusLimit <= 0) 4987 return false; 4988 } else if (isVOP3(MI) && AMDGPU::isSISrcOperand(InstDesc, i) && 4989 isLiteralConstantLike(Op, InstDesc.OpInfo[i])) { 4990 if (!VOP3LiteralLimit--) 4991 return false; 4992 if (--ConstantBusLimit <= 0) 4993 return false; 4994 } 4995 } 4996 } 4997 4998 if (MO->isReg()) { 4999 assert(DefinedRC); 5000 if (!isLegalRegOperand(MRI, OpInfo, *MO)) 5001 return false; 5002 bool IsAGPR = RI.isAGPR(MRI, MO->getReg()); 5003 if (IsAGPR && !ST.hasMAIInsts()) 5004 return false; 5005 unsigned Opc = MI.getOpcode(); 5006 if (IsAGPR && 5007 (!ST.hasGFX90AInsts() || !MRI.reservedRegsFrozen()) && 5008 (MI.mayLoad() || MI.mayStore() || isDS(Opc) || isMIMG(Opc))) 5009 return false; 5010 // Atomics should have both vdst and vdata either vgpr or agpr. 5011 const int VDstIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst); 5012 const int DataIdx = AMDGPU::getNamedOperandIdx(Opc, 5013 isDS(Opc) ? AMDGPU::OpName::data0 : AMDGPU::OpName::vdata); 5014 if ((int)OpIdx == VDstIdx && DataIdx != -1 && 5015 MI.getOperand(DataIdx).isReg() && 5016 RI.isAGPR(MRI, MI.getOperand(DataIdx).getReg()) != IsAGPR) 5017 return false; 5018 if ((int)OpIdx == DataIdx) { 5019 if (VDstIdx != -1 && 5020 RI.isAGPR(MRI, MI.getOperand(VDstIdx).getReg()) != IsAGPR) 5021 return false; 5022 // DS instructions with 2 src operands also must have tied RC. 5023 const int Data1Idx = AMDGPU::getNamedOperandIdx(Opc, 5024 AMDGPU::OpName::data1); 5025 if (Data1Idx != -1 && MI.getOperand(Data1Idx).isReg() && 5026 RI.isAGPR(MRI, MI.getOperand(Data1Idx).getReg()) != IsAGPR) 5027 return false; 5028 } 5029 if (Opc == AMDGPU::V_ACCVGPR_WRITE_B32_e64 && 5030 (int)OpIdx == AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) && 5031 RI.isSGPRReg(MRI, MO->getReg())) 5032 return false; 5033 return true; 5034 } 5035 5036 // Handle non-register types that are treated like immediates. 5037 assert(MO->isImm() || MO->isTargetIndex() || MO->isFI() || MO->isGlobal()); 5038 5039 if (!DefinedRC) { 5040 // This operand expects an immediate. 5041 return true; 5042 } 5043 5044 return isImmOperandLegal(MI, OpIdx, *MO); 5045 } 5046 5047 void SIInstrInfo::legalizeOperandsVOP2(MachineRegisterInfo &MRI, 5048 MachineInstr &MI) const { 5049 unsigned Opc = MI.getOpcode(); 5050 const MCInstrDesc &InstrDesc = get(Opc); 5051 5052 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 5053 MachineOperand &Src0 = MI.getOperand(Src0Idx); 5054 5055 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 5056 MachineOperand &Src1 = MI.getOperand(Src1Idx); 5057 5058 // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32 5059 // we need to only have one constant bus use before GFX10. 5060 bool HasImplicitSGPR = findImplicitSGPRRead(MI) != AMDGPU::NoRegister; 5061 if (HasImplicitSGPR && ST.getConstantBusLimit(Opc) <= 1 && 5062 Src0.isReg() && (RI.isSGPRReg(MRI, Src0.getReg()) || 5063 isLiteralConstantLike(Src0, InstrDesc.OpInfo[Src0Idx]))) 5064 legalizeOpWithMove(MI, Src0Idx); 5065 5066 // Special case: V_WRITELANE_B32 accepts only immediate or SGPR operands for 5067 // both the value to write (src0) and lane select (src1). Fix up non-SGPR 5068 // src0/src1 with V_READFIRSTLANE. 5069 if (Opc == AMDGPU::V_WRITELANE_B32) { 5070 const DebugLoc &DL = MI.getDebugLoc(); 5071 if (Src0.isReg() && RI.isVGPR(MRI, Src0.getReg())) { 5072 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5073 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5074 .add(Src0); 5075 Src0.ChangeToRegister(Reg, false); 5076 } 5077 if (Src1.isReg() && RI.isVGPR(MRI, Src1.getReg())) { 5078 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5079 const DebugLoc &DL = MI.getDebugLoc(); 5080 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5081 .add(Src1); 5082 Src1.ChangeToRegister(Reg, false); 5083 } 5084 return; 5085 } 5086 5087 // No VOP2 instructions support AGPRs. 5088 if (Src0.isReg() && RI.isAGPR(MRI, Src0.getReg())) 5089 legalizeOpWithMove(MI, Src0Idx); 5090 5091 if (Src1.isReg() && RI.isAGPR(MRI, Src1.getReg())) 5092 legalizeOpWithMove(MI, Src1Idx); 5093 5094 // VOP2 src0 instructions support all operand types, so we don't need to check 5095 // their legality. If src1 is already legal, we don't need to do anything. 5096 if (isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src1)) 5097 return; 5098 5099 // Special case: V_READLANE_B32 accepts only immediate or SGPR operands for 5100 // lane select. Fix up using V_READFIRSTLANE, since we assume that the lane 5101 // select is uniform. 5102 if (Opc == AMDGPU::V_READLANE_B32 && Src1.isReg() && 5103 RI.isVGPR(MRI, Src1.getReg())) { 5104 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5105 const DebugLoc &DL = MI.getDebugLoc(); 5106 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5107 .add(Src1); 5108 Src1.ChangeToRegister(Reg, false); 5109 return; 5110 } 5111 5112 // We do not use commuteInstruction here because it is too aggressive and will 5113 // commute if it is possible. We only want to commute here if it improves 5114 // legality. This can be called a fairly large number of times so don't waste 5115 // compile time pointlessly swapping and checking legality again. 5116 if (HasImplicitSGPR || !MI.isCommutable()) { 5117 legalizeOpWithMove(MI, Src1Idx); 5118 return; 5119 } 5120 5121 // If src0 can be used as src1, commuting will make the operands legal. 5122 // Otherwise we have to give up and insert a move. 5123 // 5124 // TODO: Other immediate-like operand kinds could be commuted if there was a 5125 // MachineOperand::ChangeTo* for them. 5126 if ((!Src1.isImm() && !Src1.isReg()) || 5127 !isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src0)) { 5128 legalizeOpWithMove(MI, Src1Idx); 5129 return; 5130 } 5131 5132 int CommutedOpc = commuteOpcode(MI); 5133 if (CommutedOpc == -1) { 5134 legalizeOpWithMove(MI, Src1Idx); 5135 return; 5136 } 5137 5138 MI.setDesc(get(CommutedOpc)); 5139 5140 Register Src0Reg = Src0.getReg(); 5141 unsigned Src0SubReg = Src0.getSubReg(); 5142 bool Src0Kill = Src0.isKill(); 5143 5144 if (Src1.isImm()) 5145 Src0.ChangeToImmediate(Src1.getImm()); 5146 else if (Src1.isReg()) { 5147 Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill()); 5148 Src0.setSubReg(Src1.getSubReg()); 5149 } else 5150 llvm_unreachable("Should only have register or immediate operands"); 5151 5152 Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill); 5153 Src1.setSubReg(Src0SubReg); 5154 fixImplicitOperands(MI); 5155 } 5156 5157 // Legalize VOP3 operands. All operand types are supported for any operand 5158 // but only one literal constant and only starting from GFX10. 5159 void SIInstrInfo::legalizeOperandsVOP3(MachineRegisterInfo &MRI, 5160 MachineInstr &MI) const { 5161 unsigned Opc = MI.getOpcode(); 5162 5163 int VOP3Idx[3] = { 5164 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0), 5165 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1), 5166 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2) 5167 }; 5168 5169 if (Opc == AMDGPU::V_PERMLANE16_B32_e64 || 5170 Opc == AMDGPU::V_PERMLANEX16_B32_e64) { 5171 // src1 and src2 must be scalar 5172 MachineOperand &Src1 = MI.getOperand(VOP3Idx[1]); 5173 MachineOperand &Src2 = MI.getOperand(VOP3Idx[2]); 5174 const DebugLoc &DL = MI.getDebugLoc(); 5175 if (Src1.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src1.getReg()))) { 5176 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5177 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5178 .add(Src1); 5179 Src1.ChangeToRegister(Reg, false); 5180 } 5181 if (Src2.isReg() && !RI.isSGPRClass(MRI.getRegClass(Src2.getReg()))) { 5182 Register Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 5183 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 5184 .add(Src2); 5185 Src2.ChangeToRegister(Reg, false); 5186 } 5187 } 5188 5189 // Find the one SGPR operand we are allowed to use. 5190 int ConstantBusLimit = ST.getConstantBusLimit(Opc); 5191 int LiteralLimit = ST.hasVOP3Literal() ? 1 : 0; 5192 SmallDenseSet<unsigned> SGPRsUsed; 5193 Register SGPRReg = findUsedSGPR(MI, VOP3Idx); 5194 if (SGPRReg != AMDGPU::NoRegister) { 5195 SGPRsUsed.insert(SGPRReg); 5196 --ConstantBusLimit; 5197 } 5198 5199 for (int Idx : VOP3Idx) { 5200 if (Idx == -1) 5201 break; 5202 MachineOperand &MO = MI.getOperand(Idx); 5203 5204 if (!MO.isReg()) { 5205 if (!isLiteralConstantLike(MO, get(Opc).OpInfo[Idx])) 5206 continue; 5207 5208 if (LiteralLimit > 0 && ConstantBusLimit > 0) { 5209 --LiteralLimit; 5210 --ConstantBusLimit; 5211 continue; 5212 } 5213 5214 --LiteralLimit; 5215 --ConstantBusLimit; 5216 legalizeOpWithMove(MI, Idx); 5217 continue; 5218 } 5219 5220 if (RI.hasAGPRs(RI.getRegClassForReg(MRI, MO.getReg())) && 5221 !isOperandLegal(MI, Idx, &MO)) { 5222 legalizeOpWithMove(MI, Idx); 5223 continue; 5224 } 5225 5226 if (!RI.isSGPRClass(RI.getRegClassForReg(MRI, MO.getReg()))) 5227 continue; // VGPRs are legal 5228 5229 // We can use one SGPR in each VOP3 instruction prior to GFX10 5230 // and two starting from GFX10. 5231 if (SGPRsUsed.count(MO.getReg())) 5232 continue; 5233 if (ConstantBusLimit > 0) { 5234 SGPRsUsed.insert(MO.getReg()); 5235 --ConstantBusLimit; 5236 continue; 5237 } 5238 5239 // If we make it this far, then the operand is not legal and we must 5240 // legalize it. 5241 legalizeOpWithMove(MI, Idx); 5242 } 5243 } 5244 5245 Register SIInstrInfo::readlaneVGPRToSGPR(Register SrcReg, MachineInstr &UseMI, 5246 MachineRegisterInfo &MRI) const { 5247 const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg); 5248 const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC); 5249 Register DstReg = MRI.createVirtualRegister(SRC); 5250 unsigned SubRegs = RI.getRegSizeInBits(*VRC) / 32; 5251 5252 if (RI.hasAGPRs(VRC)) { 5253 VRC = RI.getEquivalentVGPRClass(VRC); 5254 Register NewSrcReg = MRI.createVirtualRegister(VRC); 5255 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5256 get(TargetOpcode::COPY), NewSrcReg) 5257 .addReg(SrcReg); 5258 SrcReg = NewSrcReg; 5259 } 5260 5261 if (SubRegs == 1) { 5262 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5263 get(AMDGPU::V_READFIRSTLANE_B32), DstReg) 5264 .addReg(SrcReg); 5265 return DstReg; 5266 } 5267 5268 SmallVector<unsigned, 8> SRegs; 5269 for (unsigned i = 0; i < SubRegs; ++i) { 5270 Register SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5271 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5272 get(AMDGPU::V_READFIRSTLANE_B32), SGPR) 5273 .addReg(SrcReg, 0, RI.getSubRegFromChannel(i)); 5274 SRegs.push_back(SGPR); 5275 } 5276 5277 MachineInstrBuilder MIB = 5278 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 5279 get(AMDGPU::REG_SEQUENCE), DstReg); 5280 for (unsigned i = 0; i < SubRegs; ++i) { 5281 MIB.addReg(SRegs[i]); 5282 MIB.addImm(RI.getSubRegFromChannel(i)); 5283 } 5284 return DstReg; 5285 } 5286 5287 void SIInstrInfo::legalizeOperandsSMRD(MachineRegisterInfo &MRI, 5288 MachineInstr &MI) const { 5289 5290 // If the pointer is store in VGPRs, then we need to move them to 5291 // SGPRs using v_readfirstlane. This is safe because we only select 5292 // loads with uniform pointers to SMRD instruction so we know the 5293 // pointer value is uniform. 5294 MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase); 5295 if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) { 5296 Register SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI); 5297 SBase->setReg(SGPR); 5298 } 5299 MachineOperand *SOff = getNamedOperand(MI, AMDGPU::OpName::soff); 5300 if (SOff && !RI.isSGPRClass(MRI.getRegClass(SOff->getReg()))) { 5301 Register SGPR = readlaneVGPRToSGPR(SOff->getReg(), MI, MRI); 5302 SOff->setReg(SGPR); 5303 } 5304 } 5305 5306 bool SIInstrInfo::moveFlatAddrToVGPR(MachineInstr &Inst) const { 5307 unsigned Opc = Inst.getOpcode(); 5308 int OldSAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::saddr); 5309 if (OldSAddrIdx < 0) 5310 return false; 5311 5312 assert(isSegmentSpecificFLAT(Inst)); 5313 5314 int NewOpc = AMDGPU::getGlobalVaddrOp(Opc); 5315 if (NewOpc < 0) 5316 NewOpc = AMDGPU::getFlatScratchInstSVfromSS(Opc); 5317 if (NewOpc < 0) 5318 return false; 5319 5320 MachineRegisterInfo &MRI = Inst.getMF()->getRegInfo(); 5321 MachineOperand &SAddr = Inst.getOperand(OldSAddrIdx); 5322 if (RI.isSGPRReg(MRI, SAddr.getReg())) 5323 return false; 5324 5325 int NewVAddrIdx = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vaddr); 5326 if (NewVAddrIdx < 0) 5327 return false; 5328 5329 int OldVAddrIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr); 5330 5331 // Check vaddr, it shall be zero or absent. 5332 MachineInstr *VAddrDef = nullptr; 5333 if (OldVAddrIdx >= 0) { 5334 MachineOperand &VAddr = Inst.getOperand(OldVAddrIdx); 5335 VAddrDef = MRI.getUniqueVRegDef(VAddr.getReg()); 5336 if (!VAddrDef || VAddrDef->getOpcode() != AMDGPU::V_MOV_B32_e32 || 5337 !VAddrDef->getOperand(1).isImm() || 5338 VAddrDef->getOperand(1).getImm() != 0) 5339 return false; 5340 } 5341 5342 const MCInstrDesc &NewDesc = get(NewOpc); 5343 Inst.setDesc(NewDesc); 5344 5345 // Callers expect iterator to be valid after this call, so modify the 5346 // instruction in place. 5347 if (OldVAddrIdx == NewVAddrIdx) { 5348 MachineOperand &NewVAddr = Inst.getOperand(NewVAddrIdx); 5349 // Clear use list from the old vaddr holding a zero register. 5350 MRI.removeRegOperandFromUseList(&NewVAddr); 5351 MRI.moveOperands(&NewVAddr, &SAddr, 1); 5352 Inst.removeOperand(OldSAddrIdx); 5353 // Update the use list with the pointer we have just moved from vaddr to 5354 // saddr position. Otherwise new vaddr will be missing from the use list. 5355 MRI.removeRegOperandFromUseList(&NewVAddr); 5356 MRI.addRegOperandToUseList(&NewVAddr); 5357 } else { 5358 assert(OldSAddrIdx == NewVAddrIdx); 5359 5360 if (OldVAddrIdx >= 0) { 5361 int NewVDstIn = AMDGPU::getNamedOperandIdx(NewOpc, 5362 AMDGPU::OpName::vdst_in); 5363 5364 // removeOperand doesn't try to fixup tied operand indexes at it goes, so 5365 // it asserts. Untie the operands for now and retie them afterwards. 5366 if (NewVDstIn != -1) { 5367 int OldVDstIn = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vdst_in); 5368 Inst.untieRegOperand(OldVDstIn); 5369 } 5370 5371 Inst.removeOperand(OldVAddrIdx); 5372 5373 if (NewVDstIn != -1) { 5374 int NewVDst = AMDGPU::getNamedOperandIdx(NewOpc, AMDGPU::OpName::vdst); 5375 Inst.tieOperands(NewVDst, NewVDstIn); 5376 } 5377 } 5378 } 5379 5380 if (VAddrDef && MRI.use_nodbg_empty(VAddrDef->getOperand(0).getReg())) 5381 VAddrDef->eraseFromParent(); 5382 5383 return true; 5384 } 5385 5386 // FIXME: Remove this when SelectionDAG is obsoleted. 5387 void SIInstrInfo::legalizeOperandsFLAT(MachineRegisterInfo &MRI, 5388 MachineInstr &MI) const { 5389 if (!isSegmentSpecificFLAT(MI)) 5390 return; 5391 5392 // Fixup SGPR operands in VGPRs. We only select these when the DAG divergence 5393 // thinks they are uniform, so a readfirstlane should be valid. 5394 MachineOperand *SAddr = getNamedOperand(MI, AMDGPU::OpName::saddr); 5395 if (!SAddr || RI.isSGPRClass(MRI.getRegClass(SAddr->getReg()))) 5396 return; 5397 5398 if (moveFlatAddrToVGPR(MI)) 5399 return; 5400 5401 Register ToSGPR = readlaneVGPRToSGPR(SAddr->getReg(), MI, MRI); 5402 SAddr->setReg(ToSGPR); 5403 } 5404 5405 void SIInstrInfo::legalizeGenericOperand(MachineBasicBlock &InsertMBB, 5406 MachineBasicBlock::iterator I, 5407 const TargetRegisterClass *DstRC, 5408 MachineOperand &Op, 5409 MachineRegisterInfo &MRI, 5410 const DebugLoc &DL) const { 5411 Register OpReg = Op.getReg(); 5412 unsigned OpSubReg = Op.getSubReg(); 5413 5414 const TargetRegisterClass *OpRC = RI.getSubClassWithSubReg( 5415 RI.getRegClassForReg(MRI, OpReg), OpSubReg); 5416 5417 // Check if operand is already the correct register class. 5418 if (DstRC == OpRC) 5419 return; 5420 5421 Register DstReg = MRI.createVirtualRegister(DstRC); 5422 auto Copy = BuildMI(InsertMBB, I, DL, get(AMDGPU::COPY), DstReg).add(Op); 5423 5424 Op.setReg(DstReg); 5425 Op.setSubReg(0); 5426 5427 MachineInstr *Def = MRI.getVRegDef(OpReg); 5428 if (!Def) 5429 return; 5430 5431 // Try to eliminate the copy if it is copying an immediate value. 5432 if (Def->isMoveImmediate() && DstRC != &AMDGPU::VReg_1RegClass) 5433 FoldImmediate(*Copy, *Def, OpReg, &MRI); 5434 5435 bool ImpDef = Def->isImplicitDef(); 5436 while (!ImpDef && Def && Def->isCopy()) { 5437 if (Def->getOperand(1).getReg().isPhysical()) 5438 break; 5439 Def = MRI.getUniqueVRegDef(Def->getOperand(1).getReg()); 5440 ImpDef = Def && Def->isImplicitDef(); 5441 } 5442 if (!RI.isSGPRClass(DstRC) && !Copy->readsRegister(AMDGPU::EXEC, &RI) && 5443 !ImpDef) 5444 Copy.addReg(AMDGPU::EXEC, RegState::Implicit); 5445 } 5446 5447 // Emit the actual waterfall loop, executing the wrapped instruction for each 5448 // unique value of \p Rsrc across all lanes. In the best case we execute 1 5449 // iteration, in the worst case we execute 64 (once per lane). 5450 static void 5451 emitLoadSRsrcFromVGPRLoop(const SIInstrInfo &TII, MachineRegisterInfo &MRI, 5452 MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB, 5453 const DebugLoc &DL, MachineOperand &Rsrc) { 5454 MachineFunction &MF = *OrigBB.getParent(); 5455 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 5456 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 5457 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 5458 unsigned SaveExecOpc = 5459 ST.isWave32() ? AMDGPU::S_AND_SAVEEXEC_B32 : AMDGPU::S_AND_SAVEEXEC_B64; 5460 unsigned XorTermOpc = 5461 ST.isWave32() ? AMDGPU::S_XOR_B32_term : AMDGPU::S_XOR_B64_term; 5462 unsigned AndOpc = 5463 ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64; 5464 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 5465 5466 MachineBasicBlock::iterator I = LoopBB.begin(); 5467 5468 SmallVector<Register, 8> ReadlanePieces; 5469 Register CondReg = AMDGPU::NoRegister; 5470 5471 Register VRsrc = Rsrc.getReg(); 5472 unsigned VRsrcUndef = getUndefRegState(Rsrc.isUndef()); 5473 5474 unsigned RegSize = TRI->getRegSizeInBits(Rsrc.getReg(), MRI); 5475 unsigned NumSubRegs = RegSize / 32; 5476 assert(NumSubRegs % 2 == 0 && NumSubRegs <= 32 && "Unhandled register size"); 5477 5478 for (unsigned Idx = 0; Idx < NumSubRegs; Idx += 2) { 5479 5480 Register CurRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5481 Register CurRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5482 5483 // Read the next variant <- also loop target. 5484 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegLo) 5485 .addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx)); 5486 5487 // Read the next variant <- also loop target. 5488 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), CurRegHi) 5489 .addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx + 1)); 5490 5491 ReadlanePieces.push_back(CurRegLo); 5492 ReadlanePieces.push_back(CurRegHi); 5493 5494 // Comparison is to be done as 64-bit. 5495 Register CurReg = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass); 5496 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), CurReg) 5497 .addReg(CurRegLo) 5498 .addImm(AMDGPU::sub0) 5499 .addReg(CurRegHi) 5500 .addImm(AMDGPU::sub1); 5501 5502 Register NewCondReg = MRI.createVirtualRegister(BoolXExecRC); 5503 auto Cmp = 5504 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64), NewCondReg) 5505 .addReg(CurReg); 5506 if (NumSubRegs <= 2) 5507 Cmp.addReg(VRsrc); 5508 else 5509 Cmp.addReg(VRsrc, VRsrcUndef, TRI->getSubRegFromChannel(Idx, 2)); 5510 5511 // Combine the comparison results with AND. 5512 if (CondReg == AMDGPU::NoRegister) // First. 5513 CondReg = NewCondReg; 5514 else { // If not the first, we create an AND. 5515 Register AndReg = MRI.createVirtualRegister(BoolXExecRC); 5516 BuildMI(LoopBB, I, DL, TII.get(AndOpc), AndReg) 5517 .addReg(CondReg) 5518 .addReg(NewCondReg); 5519 CondReg = AndReg; 5520 } 5521 } // End for loop. 5522 5523 auto SRsrcRC = TRI->getEquivalentSGPRClass(MRI.getRegClass(VRsrc)); 5524 Register SRsrc = MRI.createVirtualRegister(SRsrcRC); 5525 5526 // Build scalar Rsrc. 5527 auto Merge = BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), SRsrc); 5528 unsigned Channel = 0; 5529 for (Register Piece : ReadlanePieces) { 5530 Merge.addReg(Piece) 5531 .addImm(TRI->getSubRegFromChannel(Channel++)); 5532 } 5533 5534 // Update Rsrc operand to use the SGPR Rsrc. 5535 Rsrc.setReg(SRsrc); 5536 Rsrc.setIsKill(true); 5537 5538 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC); 5539 MRI.setSimpleHint(SaveExec, CondReg); 5540 5541 // Update EXEC to matching lanes, saving original to SaveExec. 5542 BuildMI(LoopBB, I, DL, TII.get(SaveExecOpc), SaveExec) 5543 .addReg(CondReg, RegState::Kill); 5544 5545 // The original instruction is here; we insert the terminators after it. 5546 I = LoopBB.end(); 5547 5548 // Update EXEC, switch all done bits to 0 and all todo bits to 1. 5549 BuildMI(LoopBB, I, DL, TII.get(XorTermOpc), Exec) 5550 .addReg(Exec) 5551 .addReg(SaveExec); 5552 5553 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::SI_WATERFALL_LOOP)).addMBB(&LoopBB); 5554 } 5555 5556 // Build a waterfall loop around \p MI, replacing the VGPR \p Rsrc register 5557 // with SGPRs by iterating over all unique values across all lanes. 5558 // Returns the loop basic block that now contains \p MI. 5559 static MachineBasicBlock * 5560 loadSRsrcFromVGPR(const SIInstrInfo &TII, MachineInstr &MI, 5561 MachineOperand &Rsrc, MachineDominatorTree *MDT, 5562 MachineBasicBlock::iterator Begin = nullptr, 5563 MachineBasicBlock::iterator End = nullptr) { 5564 MachineBasicBlock &MBB = *MI.getParent(); 5565 MachineFunction &MF = *MBB.getParent(); 5566 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); 5567 const SIRegisterInfo *TRI = ST.getRegisterInfo(); 5568 MachineRegisterInfo &MRI = MF.getRegInfo(); 5569 if (!Begin.isValid()) 5570 Begin = &MI; 5571 if (!End.isValid()) { 5572 End = &MI; 5573 ++End; 5574 } 5575 const DebugLoc &DL = MI.getDebugLoc(); 5576 unsigned Exec = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 5577 unsigned MovExecOpc = ST.isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64; 5578 const auto *BoolXExecRC = TRI->getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 5579 5580 Register SaveExec = MRI.createVirtualRegister(BoolXExecRC); 5581 5582 // Save the EXEC mask 5583 BuildMI(MBB, Begin, DL, TII.get(MovExecOpc), SaveExec).addReg(Exec); 5584 5585 // Killed uses in the instruction we are waterfalling around will be 5586 // incorrect due to the added control-flow. 5587 MachineBasicBlock::iterator AfterMI = MI; 5588 ++AfterMI; 5589 for (auto I = Begin; I != AfterMI; I++) { 5590 for (auto &MO : I->uses()) { 5591 if (MO.isReg() && MO.isUse()) { 5592 MRI.clearKillFlags(MO.getReg()); 5593 } 5594 } 5595 } 5596 5597 // To insert the loop we need to split the block. Move everything after this 5598 // point to a new block, and insert a new empty block between the two. 5599 MachineBasicBlock *LoopBB = MF.CreateMachineBasicBlock(); 5600 MachineBasicBlock *RemainderBB = MF.CreateMachineBasicBlock(); 5601 MachineFunction::iterator MBBI(MBB); 5602 ++MBBI; 5603 5604 MF.insert(MBBI, LoopBB); 5605 MF.insert(MBBI, RemainderBB); 5606 5607 LoopBB->addSuccessor(LoopBB); 5608 LoopBB->addSuccessor(RemainderBB); 5609 5610 // Move Begin to MI to the LoopBB, and the remainder of the block to 5611 // RemainderBB. 5612 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); 5613 RemainderBB->splice(RemainderBB->begin(), &MBB, End, MBB.end()); 5614 LoopBB->splice(LoopBB->begin(), &MBB, Begin, MBB.end()); 5615 5616 MBB.addSuccessor(LoopBB); 5617 5618 // Update dominators. We know that MBB immediately dominates LoopBB, that 5619 // LoopBB immediately dominates RemainderBB, and that RemainderBB immediately 5620 // dominates all of the successors transferred to it from MBB that MBB used 5621 // to properly dominate. 5622 if (MDT) { 5623 MDT->addNewBlock(LoopBB, &MBB); 5624 MDT->addNewBlock(RemainderBB, LoopBB); 5625 for (auto &Succ : RemainderBB->successors()) { 5626 if (MDT->properlyDominates(&MBB, Succ)) { 5627 MDT->changeImmediateDominator(Succ, RemainderBB); 5628 } 5629 } 5630 } 5631 5632 emitLoadSRsrcFromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, Rsrc); 5633 5634 // Restore the EXEC mask 5635 MachineBasicBlock::iterator First = RemainderBB->begin(); 5636 BuildMI(*RemainderBB, First, DL, TII.get(MovExecOpc), Exec).addReg(SaveExec); 5637 return LoopBB; 5638 } 5639 5640 // Extract pointer from Rsrc and return a zero-value Rsrc replacement. 5641 static std::tuple<unsigned, unsigned> 5642 extractRsrcPtr(const SIInstrInfo &TII, MachineInstr &MI, MachineOperand &Rsrc) { 5643 MachineBasicBlock &MBB = *MI.getParent(); 5644 MachineFunction &MF = *MBB.getParent(); 5645 MachineRegisterInfo &MRI = MF.getRegInfo(); 5646 5647 // Extract the ptr from the resource descriptor. 5648 unsigned RsrcPtr = 5649 TII.buildExtractSubReg(MI, MRI, Rsrc, &AMDGPU::VReg_128RegClass, 5650 AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass); 5651 5652 // Create an empty resource descriptor 5653 Register Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 5654 Register SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5655 Register SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 5656 Register NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SGPR_128RegClass); 5657 uint64_t RsrcDataFormat = TII.getDefaultRsrcDataFormat(); 5658 5659 // Zero64 = 0 5660 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B64), Zero64) 5661 .addImm(0); 5662 5663 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0} 5664 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatLo) 5665 .addImm(RsrcDataFormat & 0xFFFFFFFF); 5666 5667 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32} 5668 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatHi) 5669 .addImm(RsrcDataFormat >> 32); 5670 5671 // NewSRsrc = {Zero64, SRsrcFormat} 5672 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::REG_SEQUENCE), NewSRsrc) 5673 .addReg(Zero64) 5674 .addImm(AMDGPU::sub0_sub1) 5675 .addReg(SRsrcFormatLo) 5676 .addImm(AMDGPU::sub2) 5677 .addReg(SRsrcFormatHi) 5678 .addImm(AMDGPU::sub3); 5679 5680 return std::make_tuple(RsrcPtr, NewSRsrc); 5681 } 5682 5683 MachineBasicBlock * 5684 SIInstrInfo::legalizeOperands(MachineInstr &MI, 5685 MachineDominatorTree *MDT) const { 5686 MachineFunction &MF = *MI.getParent()->getParent(); 5687 MachineRegisterInfo &MRI = MF.getRegInfo(); 5688 MachineBasicBlock *CreatedBB = nullptr; 5689 5690 // Legalize VOP2 5691 if (isVOP2(MI) || isVOPC(MI)) { 5692 legalizeOperandsVOP2(MRI, MI); 5693 return CreatedBB; 5694 } 5695 5696 // Legalize VOP3 5697 if (isVOP3(MI)) { 5698 legalizeOperandsVOP3(MRI, MI); 5699 return CreatedBB; 5700 } 5701 5702 // Legalize SMRD 5703 if (isSMRD(MI)) { 5704 legalizeOperandsSMRD(MRI, MI); 5705 return CreatedBB; 5706 } 5707 5708 // Legalize FLAT 5709 if (isFLAT(MI)) { 5710 legalizeOperandsFLAT(MRI, MI); 5711 return CreatedBB; 5712 } 5713 5714 // Legalize REG_SEQUENCE and PHI 5715 // The register class of the operands much be the same type as the register 5716 // class of the output. 5717 if (MI.getOpcode() == AMDGPU::PHI) { 5718 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr; 5719 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { 5720 if (!MI.getOperand(i).isReg() || !MI.getOperand(i).getReg().isVirtual()) 5721 continue; 5722 const TargetRegisterClass *OpRC = 5723 MRI.getRegClass(MI.getOperand(i).getReg()); 5724 if (RI.hasVectorRegisters(OpRC)) { 5725 VRC = OpRC; 5726 } else { 5727 SRC = OpRC; 5728 } 5729 } 5730 5731 // If any of the operands are VGPR registers, then they all most be 5732 // otherwise we will create illegal VGPR->SGPR copies when legalizing 5733 // them. 5734 if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) { 5735 if (!VRC) { 5736 assert(SRC); 5737 if (getOpRegClass(MI, 0) == &AMDGPU::VReg_1RegClass) { 5738 VRC = &AMDGPU::VReg_1RegClass; 5739 } else 5740 VRC = RI.isAGPRClass(getOpRegClass(MI, 0)) 5741 ? RI.getEquivalentAGPRClass(SRC) 5742 : RI.getEquivalentVGPRClass(SRC); 5743 } else { 5744 VRC = RI.isAGPRClass(getOpRegClass(MI, 0)) 5745 ? RI.getEquivalentAGPRClass(VRC) 5746 : RI.getEquivalentVGPRClass(VRC); 5747 } 5748 RC = VRC; 5749 } else { 5750 RC = SRC; 5751 } 5752 5753 // Update all the operands so they have the same type. 5754 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 5755 MachineOperand &Op = MI.getOperand(I); 5756 if (!Op.isReg() || !Op.getReg().isVirtual()) 5757 continue; 5758 5759 // MI is a PHI instruction. 5760 MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB(); 5761 MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator(); 5762 5763 // Avoid creating no-op copies with the same src and dst reg class. These 5764 // confuse some of the machine passes. 5765 legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc()); 5766 } 5767 } 5768 5769 // REG_SEQUENCE doesn't really require operand legalization, but if one has a 5770 // VGPR dest type and SGPR sources, insert copies so all operands are 5771 // VGPRs. This seems to help operand folding / the register coalescer. 5772 if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) { 5773 MachineBasicBlock *MBB = MI.getParent(); 5774 const TargetRegisterClass *DstRC = getOpRegClass(MI, 0); 5775 if (RI.hasVGPRs(DstRC)) { 5776 // Update all the operands so they are VGPR register classes. These may 5777 // not be the same register class because REG_SEQUENCE supports mixing 5778 // subregister index types e.g. sub0_sub1 + sub2 + sub3 5779 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 5780 MachineOperand &Op = MI.getOperand(I); 5781 if (!Op.isReg() || !Op.getReg().isVirtual()) 5782 continue; 5783 5784 const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg()); 5785 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC); 5786 if (VRC == OpRC) 5787 continue; 5788 5789 legalizeGenericOperand(*MBB, MI, VRC, Op, MRI, MI.getDebugLoc()); 5790 Op.setIsKill(); 5791 } 5792 } 5793 5794 return CreatedBB; 5795 } 5796 5797 // Legalize INSERT_SUBREG 5798 // src0 must have the same register class as dst 5799 if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) { 5800 Register Dst = MI.getOperand(0).getReg(); 5801 Register Src0 = MI.getOperand(1).getReg(); 5802 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst); 5803 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0); 5804 if (DstRC != Src0RC) { 5805 MachineBasicBlock *MBB = MI.getParent(); 5806 MachineOperand &Op = MI.getOperand(1); 5807 legalizeGenericOperand(*MBB, MI, DstRC, Op, MRI, MI.getDebugLoc()); 5808 } 5809 return CreatedBB; 5810 } 5811 5812 // Legalize SI_INIT_M0 5813 if (MI.getOpcode() == AMDGPU::SI_INIT_M0) { 5814 MachineOperand &Src = MI.getOperand(0); 5815 if (Src.isReg() && RI.hasVectorRegisters(MRI.getRegClass(Src.getReg()))) 5816 Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI)); 5817 return CreatedBB; 5818 } 5819 5820 // Legalize MIMG and MUBUF/MTBUF for shaders. 5821 // 5822 // Shaders only generate MUBUF/MTBUF instructions via intrinsics or via 5823 // scratch memory access. In both cases, the legalization never involves 5824 // conversion to the addr64 form. 5825 if (isMIMG(MI) || (AMDGPU::isGraphics(MF.getFunction().getCallingConv()) && 5826 (isMUBUF(MI) || isMTBUF(MI)))) { 5827 MachineOperand *SRsrc = getNamedOperand(MI, AMDGPU::OpName::srsrc); 5828 if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg()))) 5829 CreatedBB = loadSRsrcFromVGPR(*this, MI, *SRsrc, MDT); 5830 5831 MachineOperand *SSamp = getNamedOperand(MI, AMDGPU::OpName::ssamp); 5832 if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg()))) 5833 CreatedBB = loadSRsrcFromVGPR(*this, MI, *SSamp, MDT); 5834 5835 return CreatedBB; 5836 } 5837 5838 // Legalize SI_CALL 5839 if (MI.getOpcode() == AMDGPU::SI_CALL_ISEL) { 5840 MachineOperand *Dest = &MI.getOperand(0); 5841 if (!RI.isSGPRClass(MRI.getRegClass(Dest->getReg()))) { 5842 // Move everything between ADJCALLSTACKUP and ADJCALLSTACKDOWN and 5843 // following copies, we also need to move copies from and to physical 5844 // registers into the loop block. 5845 unsigned FrameSetupOpcode = getCallFrameSetupOpcode(); 5846 unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode(); 5847 5848 // Also move the copies to physical registers into the loop block 5849 MachineBasicBlock &MBB = *MI.getParent(); 5850 MachineBasicBlock::iterator Start(&MI); 5851 while (Start->getOpcode() != FrameSetupOpcode) 5852 --Start; 5853 MachineBasicBlock::iterator End(&MI); 5854 while (End->getOpcode() != FrameDestroyOpcode) 5855 ++End; 5856 // Also include following copies of the return value 5857 ++End; 5858 while (End != MBB.end() && End->isCopy() && End->getOperand(1).isReg() && 5859 MI.definesRegister(End->getOperand(1).getReg())) 5860 ++End; 5861 CreatedBB = loadSRsrcFromVGPR(*this, MI, *Dest, MDT, Start, End); 5862 } 5863 } 5864 5865 // Legalize MUBUF* instructions. 5866 int RsrcIdx = 5867 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc); 5868 if (RsrcIdx != -1) { 5869 // We have an MUBUF instruction 5870 MachineOperand *Rsrc = &MI.getOperand(RsrcIdx); 5871 unsigned RsrcRC = get(MI.getOpcode()).OpInfo[RsrcIdx].RegClass; 5872 if (RI.getCommonSubClass(MRI.getRegClass(Rsrc->getReg()), 5873 RI.getRegClass(RsrcRC))) { 5874 // The operands are legal. 5875 // FIXME: We may need to legalize operands besides srsrc. 5876 return CreatedBB; 5877 } 5878 5879 // Legalize a VGPR Rsrc. 5880 // 5881 // If the instruction is _ADDR64, we can avoid a waterfall by extracting 5882 // the base pointer from the VGPR Rsrc, adding it to the VAddr, then using 5883 // a zero-value SRsrc. 5884 // 5885 // If the instruction is _OFFSET (both idxen and offen disabled), and we 5886 // support ADDR64 instructions, we can convert to ADDR64 and do the same as 5887 // above. 5888 // 5889 // Otherwise we are on non-ADDR64 hardware, and/or we have 5890 // idxen/offen/bothen and we fall back to a waterfall loop. 5891 5892 MachineBasicBlock &MBB = *MI.getParent(); 5893 5894 MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 5895 if (VAddr && AMDGPU::getIfAddr64Inst(MI.getOpcode()) != -1) { 5896 // This is already an ADDR64 instruction so we need to add the pointer 5897 // extracted from the resource descriptor to the current value of VAddr. 5898 Register NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 5899 Register NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 5900 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 5901 5902 const auto *BoolXExecRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 5903 Register CondReg0 = MRI.createVirtualRegister(BoolXExecRC); 5904 Register CondReg1 = MRI.createVirtualRegister(BoolXExecRC); 5905 5906 unsigned RsrcPtr, NewSRsrc; 5907 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc); 5908 5909 // NewVaddrLo = RsrcPtr:sub0 + VAddr:sub0 5910 const DebugLoc &DL = MI.getDebugLoc(); 5911 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_CO_U32_e64), NewVAddrLo) 5912 .addDef(CondReg0) 5913 .addReg(RsrcPtr, 0, AMDGPU::sub0) 5914 .addReg(VAddr->getReg(), 0, AMDGPU::sub0) 5915 .addImm(0); 5916 5917 // NewVaddrHi = RsrcPtr:sub1 + VAddr:sub1 5918 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e64), NewVAddrHi) 5919 .addDef(CondReg1, RegState::Dead) 5920 .addReg(RsrcPtr, 0, AMDGPU::sub1) 5921 .addReg(VAddr->getReg(), 0, AMDGPU::sub1) 5922 .addReg(CondReg0, RegState::Kill) 5923 .addImm(0); 5924 5925 // NewVaddr = {NewVaddrHi, NewVaddrLo} 5926 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr) 5927 .addReg(NewVAddrLo) 5928 .addImm(AMDGPU::sub0) 5929 .addReg(NewVAddrHi) 5930 .addImm(AMDGPU::sub1); 5931 5932 VAddr->setReg(NewVAddr); 5933 Rsrc->setReg(NewSRsrc); 5934 } else if (!VAddr && ST.hasAddr64()) { 5935 // This instructions is the _OFFSET variant, so we need to convert it to 5936 // ADDR64. 5937 assert(ST.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS && 5938 "FIXME: Need to emit flat atomics here"); 5939 5940 unsigned RsrcPtr, NewSRsrc; 5941 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc); 5942 5943 Register NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 5944 MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata); 5945 MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 5946 MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset); 5947 unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode()); 5948 5949 // Atomics with return have an additional tied operand and are 5950 // missing some of the special bits. 5951 MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in); 5952 MachineInstr *Addr64; 5953 5954 if (!VDataIn) { 5955 // Regular buffer load / store. 5956 MachineInstrBuilder MIB = 5957 BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 5958 .add(*VData) 5959 .addReg(NewVAddr) 5960 .addReg(NewSRsrc) 5961 .add(*SOffset) 5962 .add(*Offset); 5963 5964 if (const MachineOperand *CPol = 5965 getNamedOperand(MI, AMDGPU::OpName::cpol)) { 5966 MIB.addImm(CPol->getImm()); 5967 } 5968 5969 if (const MachineOperand *TFE = 5970 getNamedOperand(MI, AMDGPU::OpName::tfe)) { 5971 MIB.addImm(TFE->getImm()); 5972 } 5973 5974 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::swz)); 5975 5976 MIB.cloneMemRefs(MI); 5977 Addr64 = MIB; 5978 } else { 5979 // Atomics with return. 5980 Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 5981 .add(*VData) 5982 .add(*VDataIn) 5983 .addReg(NewVAddr) 5984 .addReg(NewSRsrc) 5985 .add(*SOffset) 5986 .add(*Offset) 5987 .addImm(getNamedImmOperand(MI, AMDGPU::OpName::cpol)) 5988 .cloneMemRefs(MI); 5989 } 5990 5991 MI.removeFromParent(); 5992 5993 // NewVaddr = {NewVaddrHi, NewVaddrLo} 5994 BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), 5995 NewVAddr) 5996 .addReg(RsrcPtr, 0, AMDGPU::sub0) 5997 .addImm(AMDGPU::sub0) 5998 .addReg(RsrcPtr, 0, AMDGPU::sub1) 5999 .addImm(AMDGPU::sub1); 6000 } else { 6001 // This is another variant; legalize Rsrc with waterfall loop from VGPRs 6002 // to SGPRs. 6003 CreatedBB = loadSRsrcFromVGPR(*this, MI, *Rsrc, MDT); 6004 return CreatedBB; 6005 } 6006 } 6007 return CreatedBB; 6008 } 6009 6010 MachineBasicBlock *SIInstrInfo::moveToVALU(MachineInstr &TopInst, 6011 MachineDominatorTree *MDT) const { 6012 SetVectorType Worklist; 6013 Worklist.insert(&TopInst); 6014 MachineBasicBlock *CreatedBB = nullptr; 6015 MachineBasicBlock *CreatedBBTmp = nullptr; 6016 6017 while (!Worklist.empty()) { 6018 MachineInstr &Inst = *Worklist.pop_back_val(); 6019 MachineBasicBlock *MBB = Inst.getParent(); 6020 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 6021 6022 unsigned Opcode = Inst.getOpcode(); 6023 unsigned NewOpcode = getVALUOp(Inst); 6024 6025 // Handle some special cases 6026 switch (Opcode) { 6027 default: 6028 break; 6029 case AMDGPU::S_ADD_U64_PSEUDO: 6030 case AMDGPU::S_SUB_U64_PSEUDO: 6031 splitScalar64BitAddSub(Worklist, Inst, MDT); 6032 Inst.eraseFromParent(); 6033 continue; 6034 case AMDGPU::S_ADD_I32: 6035 case AMDGPU::S_SUB_I32: { 6036 // FIXME: The u32 versions currently selected use the carry. 6037 bool Changed; 6038 std::tie(Changed, CreatedBBTmp) = moveScalarAddSub(Worklist, Inst, MDT); 6039 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6040 CreatedBB = CreatedBBTmp; 6041 if (Changed) 6042 continue; 6043 6044 // Default handling 6045 break; 6046 } 6047 case AMDGPU::S_AND_B64: 6048 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32, MDT); 6049 Inst.eraseFromParent(); 6050 continue; 6051 6052 case AMDGPU::S_OR_B64: 6053 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32, MDT); 6054 Inst.eraseFromParent(); 6055 continue; 6056 6057 case AMDGPU::S_XOR_B64: 6058 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32, MDT); 6059 Inst.eraseFromParent(); 6060 continue; 6061 6062 case AMDGPU::S_NAND_B64: 6063 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NAND_B32, MDT); 6064 Inst.eraseFromParent(); 6065 continue; 6066 6067 case AMDGPU::S_NOR_B64: 6068 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NOR_B32, MDT); 6069 Inst.eraseFromParent(); 6070 continue; 6071 6072 case AMDGPU::S_XNOR_B64: 6073 if (ST.hasDLInsts()) 6074 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XNOR_B32, MDT); 6075 else 6076 splitScalar64BitXnor(Worklist, Inst, MDT); 6077 Inst.eraseFromParent(); 6078 continue; 6079 6080 case AMDGPU::S_ANDN2_B64: 6081 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ANDN2_B32, MDT); 6082 Inst.eraseFromParent(); 6083 continue; 6084 6085 case AMDGPU::S_ORN2_B64: 6086 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ORN2_B32, MDT); 6087 Inst.eraseFromParent(); 6088 continue; 6089 6090 case AMDGPU::S_BREV_B64: 6091 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_BREV_B32, true); 6092 Inst.eraseFromParent(); 6093 continue; 6094 6095 case AMDGPU::S_NOT_B64: 6096 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32); 6097 Inst.eraseFromParent(); 6098 continue; 6099 6100 case AMDGPU::S_BCNT1_I32_B64: 6101 splitScalar64BitBCNT(Worklist, Inst); 6102 Inst.eraseFromParent(); 6103 continue; 6104 6105 case AMDGPU::S_BFE_I64: 6106 splitScalar64BitBFE(Worklist, Inst); 6107 Inst.eraseFromParent(); 6108 continue; 6109 6110 case AMDGPU::S_LSHL_B32: 6111 if (ST.hasOnlyRevVALUShifts()) { 6112 NewOpcode = AMDGPU::V_LSHLREV_B32_e64; 6113 swapOperands(Inst); 6114 } 6115 break; 6116 case AMDGPU::S_ASHR_I32: 6117 if (ST.hasOnlyRevVALUShifts()) { 6118 NewOpcode = AMDGPU::V_ASHRREV_I32_e64; 6119 swapOperands(Inst); 6120 } 6121 break; 6122 case AMDGPU::S_LSHR_B32: 6123 if (ST.hasOnlyRevVALUShifts()) { 6124 NewOpcode = AMDGPU::V_LSHRREV_B32_e64; 6125 swapOperands(Inst); 6126 } 6127 break; 6128 case AMDGPU::S_LSHL_B64: 6129 if (ST.hasOnlyRevVALUShifts()) { 6130 NewOpcode = AMDGPU::V_LSHLREV_B64_e64; 6131 swapOperands(Inst); 6132 } 6133 break; 6134 case AMDGPU::S_ASHR_I64: 6135 if (ST.hasOnlyRevVALUShifts()) { 6136 NewOpcode = AMDGPU::V_ASHRREV_I64_e64; 6137 swapOperands(Inst); 6138 } 6139 break; 6140 case AMDGPU::S_LSHR_B64: 6141 if (ST.hasOnlyRevVALUShifts()) { 6142 NewOpcode = AMDGPU::V_LSHRREV_B64_e64; 6143 swapOperands(Inst); 6144 } 6145 break; 6146 6147 case AMDGPU::S_ABS_I32: 6148 lowerScalarAbs(Worklist, Inst); 6149 Inst.eraseFromParent(); 6150 continue; 6151 6152 case AMDGPU::S_CBRANCH_SCC0: 6153 case AMDGPU::S_CBRANCH_SCC1: { 6154 // Clear unused bits of vcc 6155 Register CondReg = Inst.getOperand(1).getReg(); 6156 bool IsSCC = CondReg == AMDGPU::SCC; 6157 Register VCC = RI.getVCC(); 6158 Register EXEC = ST.isWave32() ? AMDGPU::EXEC_LO : AMDGPU::EXEC; 6159 unsigned Opc = ST.isWave32() ? AMDGPU::S_AND_B32 : AMDGPU::S_AND_B64; 6160 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(Opc), VCC) 6161 .addReg(EXEC) 6162 .addReg(IsSCC ? VCC : CondReg); 6163 Inst.removeOperand(1); 6164 } 6165 break; 6166 6167 case AMDGPU::S_BFE_U64: 6168 case AMDGPU::S_BFM_B64: 6169 llvm_unreachable("Moving this op to VALU not implemented"); 6170 6171 case AMDGPU::S_PACK_LL_B32_B16: 6172 case AMDGPU::S_PACK_LH_B32_B16: 6173 case AMDGPU::S_PACK_HH_B32_B16: 6174 movePackToVALU(Worklist, MRI, Inst); 6175 Inst.eraseFromParent(); 6176 continue; 6177 6178 case AMDGPU::S_XNOR_B32: 6179 lowerScalarXnor(Worklist, Inst); 6180 Inst.eraseFromParent(); 6181 continue; 6182 6183 case AMDGPU::S_NAND_B32: 6184 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_AND_B32); 6185 Inst.eraseFromParent(); 6186 continue; 6187 6188 case AMDGPU::S_NOR_B32: 6189 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_OR_B32); 6190 Inst.eraseFromParent(); 6191 continue; 6192 6193 case AMDGPU::S_ANDN2_B32: 6194 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_AND_B32); 6195 Inst.eraseFromParent(); 6196 continue; 6197 6198 case AMDGPU::S_ORN2_B32: 6199 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_OR_B32); 6200 Inst.eraseFromParent(); 6201 continue; 6202 6203 // TODO: remove as soon as everything is ready 6204 // to replace VGPR to SGPR copy with V_READFIRSTLANEs. 6205 // S_ADD/SUB_CO_PSEUDO as well as S_UADDO/USUBO_PSEUDO 6206 // can only be selected from the uniform SDNode. 6207 case AMDGPU::S_ADD_CO_PSEUDO: 6208 case AMDGPU::S_SUB_CO_PSEUDO: { 6209 unsigned Opc = (Inst.getOpcode() == AMDGPU::S_ADD_CO_PSEUDO) 6210 ? AMDGPU::V_ADDC_U32_e64 6211 : AMDGPU::V_SUBB_U32_e64; 6212 const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 6213 6214 Register CarryInReg = Inst.getOperand(4).getReg(); 6215 if (!MRI.constrainRegClass(CarryInReg, CarryRC)) { 6216 Register NewCarryReg = MRI.createVirtualRegister(CarryRC); 6217 BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(AMDGPU::COPY), NewCarryReg) 6218 .addReg(CarryInReg); 6219 } 6220 6221 Register CarryOutReg = Inst.getOperand(1).getReg(); 6222 6223 Register DestReg = MRI.createVirtualRegister(RI.getEquivalentVGPRClass( 6224 MRI.getRegClass(Inst.getOperand(0).getReg()))); 6225 MachineInstr *CarryOp = 6226 BuildMI(*MBB, &Inst, Inst.getDebugLoc(), get(Opc), DestReg) 6227 .addReg(CarryOutReg, RegState::Define) 6228 .add(Inst.getOperand(2)) 6229 .add(Inst.getOperand(3)) 6230 .addReg(CarryInReg) 6231 .addImm(0); 6232 CreatedBBTmp = legalizeOperands(*CarryOp); 6233 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6234 CreatedBB = CreatedBBTmp; 6235 MRI.replaceRegWith(Inst.getOperand(0).getReg(), DestReg); 6236 addUsersToMoveToVALUWorklist(DestReg, MRI, Worklist); 6237 Inst.eraseFromParent(); 6238 } 6239 continue; 6240 case AMDGPU::S_UADDO_PSEUDO: 6241 case AMDGPU::S_USUBO_PSEUDO: { 6242 const DebugLoc &DL = Inst.getDebugLoc(); 6243 MachineOperand &Dest0 = Inst.getOperand(0); 6244 MachineOperand &Dest1 = Inst.getOperand(1); 6245 MachineOperand &Src0 = Inst.getOperand(2); 6246 MachineOperand &Src1 = Inst.getOperand(3); 6247 6248 unsigned Opc = (Inst.getOpcode() == AMDGPU::S_UADDO_PSEUDO) 6249 ? AMDGPU::V_ADD_CO_U32_e64 6250 : AMDGPU::V_SUB_CO_U32_e64; 6251 const TargetRegisterClass *NewRC = 6252 RI.getEquivalentVGPRClass(MRI.getRegClass(Dest0.getReg())); 6253 Register DestReg = MRI.createVirtualRegister(NewRC); 6254 MachineInstr *NewInstr = BuildMI(*MBB, &Inst, DL, get(Opc), DestReg) 6255 .addReg(Dest1.getReg(), RegState::Define) 6256 .add(Src0) 6257 .add(Src1) 6258 .addImm(0); // clamp bit 6259 6260 CreatedBBTmp = legalizeOperands(*NewInstr, MDT); 6261 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6262 CreatedBB = CreatedBBTmp; 6263 6264 MRI.replaceRegWith(Dest0.getReg(), DestReg); 6265 addUsersToMoveToVALUWorklist(NewInstr->getOperand(0).getReg(), MRI, 6266 Worklist); 6267 Inst.eraseFromParent(); 6268 } 6269 continue; 6270 6271 case AMDGPU::S_CSELECT_B32: 6272 case AMDGPU::S_CSELECT_B64: 6273 lowerSelect(Worklist, Inst, MDT); 6274 Inst.eraseFromParent(); 6275 continue; 6276 case AMDGPU::S_CMP_EQ_I32: 6277 case AMDGPU::S_CMP_LG_I32: 6278 case AMDGPU::S_CMP_GT_I32: 6279 case AMDGPU::S_CMP_GE_I32: 6280 case AMDGPU::S_CMP_LT_I32: 6281 case AMDGPU::S_CMP_LE_I32: 6282 case AMDGPU::S_CMP_EQ_U32: 6283 case AMDGPU::S_CMP_LG_U32: 6284 case AMDGPU::S_CMP_GT_U32: 6285 case AMDGPU::S_CMP_GE_U32: 6286 case AMDGPU::S_CMP_LT_U32: 6287 case AMDGPU::S_CMP_LE_U32: 6288 case AMDGPU::S_CMP_EQ_U64: 6289 case AMDGPU::S_CMP_LG_U64: { 6290 const MCInstrDesc &NewDesc = get(NewOpcode); 6291 Register CondReg = MRI.createVirtualRegister(RI.getWaveMaskRegClass()); 6292 MachineInstr *NewInstr = 6293 BuildMI(*MBB, Inst, Inst.getDebugLoc(), NewDesc, CondReg) 6294 .add(Inst.getOperand(0)) 6295 .add(Inst.getOperand(1)); 6296 legalizeOperands(*NewInstr, MDT); 6297 int SCCIdx = Inst.findRegisterDefOperandIdx(AMDGPU::SCC); 6298 MachineOperand SCCOp = Inst.getOperand(SCCIdx); 6299 addSCCDefUsersToVALUWorklist(SCCOp, Inst, Worklist, CondReg); 6300 Inst.eraseFromParent(); 6301 } 6302 continue; 6303 } 6304 6305 6306 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) { 6307 // We cannot move this instruction to the VALU, so we should try to 6308 // legalize its operands instead. 6309 CreatedBBTmp = legalizeOperands(Inst, MDT); 6310 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6311 CreatedBB = CreatedBBTmp; 6312 continue; 6313 } 6314 6315 // Use the new VALU Opcode. 6316 const MCInstrDesc &NewDesc = get(NewOpcode); 6317 Inst.setDesc(NewDesc); 6318 6319 // Remove any references to SCC. Vector instructions can't read from it, and 6320 // We're just about to add the implicit use / defs of VCC, and we don't want 6321 // both. 6322 for (unsigned i = Inst.getNumOperands() - 1; i > 0; --i) { 6323 MachineOperand &Op = Inst.getOperand(i); 6324 if (Op.isReg() && Op.getReg() == AMDGPU::SCC) { 6325 // Only propagate through live-def of SCC. 6326 if (Op.isDef() && !Op.isDead()) 6327 addSCCDefUsersToVALUWorklist(Op, Inst, Worklist); 6328 if (Op.isUse()) 6329 addSCCDefsToVALUWorklist(Op, Worklist); 6330 Inst.removeOperand(i); 6331 } 6332 } 6333 6334 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) { 6335 // We are converting these to a BFE, so we need to add the missing 6336 // operands for the size and offset. 6337 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16; 6338 Inst.addOperand(MachineOperand::CreateImm(0)); 6339 Inst.addOperand(MachineOperand::CreateImm(Size)); 6340 6341 } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) { 6342 // The VALU version adds the second operand to the result, so insert an 6343 // extra 0 operand. 6344 Inst.addOperand(MachineOperand::CreateImm(0)); 6345 } 6346 6347 Inst.addImplicitDefUseOperands(*Inst.getParent()->getParent()); 6348 fixImplicitOperands(Inst); 6349 6350 if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) { 6351 const MachineOperand &OffsetWidthOp = Inst.getOperand(2); 6352 // If we need to move this to VGPRs, we need to unpack the second operand 6353 // back into the 2 separate ones for bit offset and width. 6354 assert(OffsetWidthOp.isImm() && 6355 "Scalar BFE is only implemented for constant width and offset"); 6356 uint32_t Imm = OffsetWidthOp.getImm(); 6357 6358 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 6359 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 6360 Inst.removeOperand(2); // Remove old immediate. 6361 Inst.addOperand(MachineOperand::CreateImm(Offset)); 6362 Inst.addOperand(MachineOperand::CreateImm(BitWidth)); 6363 } 6364 6365 bool HasDst = Inst.getOperand(0).isReg() && Inst.getOperand(0).isDef(); 6366 unsigned NewDstReg = AMDGPU::NoRegister; 6367 if (HasDst) { 6368 Register DstReg = Inst.getOperand(0).getReg(); 6369 if (DstReg.isPhysical()) 6370 continue; 6371 6372 // Update the destination register class. 6373 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst); 6374 if (!NewDstRC) 6375 continue; 6376 6377 if (Inst.isCopy() && Inst.getOperand(1).getReg().isVirtual() && 6378 NewDstRC == RI.getRegClassForReg(MRI, Inst.getOperand(1).getReg())) { 6379 // Instead of creating a copy where src and dst are the same register 6380 // class, we just replace all uses of dst with src. These kinds of 6381 // copies interfere with the heuristics MachineSink uses to decide 6382 // whether or not to split a critical edge. Since the pass assumes 6383 // that copies will end up as machine instructions and not be 6384 // eliminated. 6385 addUsersToMoveToVALUWorklist(DstReg, MRI, Worklist); 6386 MRI.replaceRegWith(DstReg, Inst.getOperand(1).getReg()); 6387 MRI.clearKillFlags(Inst.getOperand(1).getReg()); 6388 Inst.getOperand(0).setReg(DstReg); 6389 6390 // Make sure we don't leave around a dead VGPR->SGPR copy. Normally 6391 // these are deleted later, but at -O0 it would leave a suspicious 6392 // looking illegal copy of an undef register. 6393 for (unsigned I = Inst.getNumOperands() - 1; I != 0; --I) 6394 Inst.removeOperand(I); 6395 Inst.setDesc(get(AMDGPU::IMPLICIT_DEF)); 6396 continue; 6397 } 6398 6399 NewDstReg = MRI.createVirtualRegister(NewDstRC); 6400 MRI.replaceRegWith(DstReg, NewDstReg); 6401 } 6402 6403 // Legalize the operands 6404 CreatedBBTmp = legalizeOperands(Inst, MDT); 6405 if (CreatedBBTmp && TopInst.getParent() == CreatedBBTmp) 6406 CreatedBB = CreatedBBTmp; 6407 6408 if (HasDst) 6409 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist); 6410 } 6411 return CreatedBB; 6412 } 6413 6414 // Add/sub require special handling to deal with carry outs. 6415 std::pair<bool, MachineBasicBlock *> 6416 SIInstrInfo::moveScalarAddSub(SetVectorType &Worklist, MachineInstr &Inst, 6417 MachineDominatorTree *MDT) const { 6418 if (ST.hasAddNoCarry()) { 6419 // Assume there is no user of scc since we don't select this in that case. 6420 // Since scc isn't used, it doesn't really matter if the i32 or u32 variant 6421 // is used. 6422 6423 MachineBasicBlock &MBB = *Inst.getParent(); 6424 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6425 6426 Register OldDstReg = Inst.getOperand(0).getReg(); 6427 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6428 6429 unsigned Opc = Inst.getOpcode(); 6430 assert(Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32); 6431 6432 unsigned NewOpc = Opc == AMDGPU::S_ADD_I32 ? 6433 AMDGPU::V_ADD_U32_e64 : AMDGPU::V_SUB_U32_e64; 6434 6435 assert(Inst.getOperand(3).getReg() == AMDGPU::SCC); 6436 Inst.removeOperand(3); 6437 6438 Inst.setDesc(get(NewOpc)); 6439 Inst.addOperand(MachineOperand::CreateImm(0)); // clamp bit 6440 Inst.addImplicitDefUseOperands(*MBB.getParent()); 6441 MRI.replaceRegWith(OldDstReg, ResultReg); 6442 MachineBasicBlock *NewBB = legalizeOperands(Inst, MDT); 6443 6444 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6445 return std::make_pair(true, NewBB); 6446 } 6447 6448 return std::make_pair(false, nullptr); 6449 } 6450 6451 void SIInstrInfo::lowerSelect(SetVectorType &Worklist, MachineInstr &Inst, 6452 MachineDominatorTree *MDT) const { 6453 6454 MachineBasicBlock &MBB = *Inst.getParent(); 6455 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6456 MachineBasicBlock::iterator MII = Inst; 6457 DebugLoc DL = Inst.getDebugLoc(); 6458 6459 MachineOperand &Dest = Inst.getOperand(0); 6460 MachineOperand &Src0 = Inst.getOperand(1); 6461 MachineOperand &Src1 = Inst.getOperand(2); 6462 MachineOperand &Cond = Inst.getOperand(3); 6463 6464 Register SCCSource = Cond.getReg(); 6465 bool IsSCC = (SCCSource == AMDGPU::SCC); 6466 6467 // If this is a trivial select where the condition is effectively not SCC 6468 // (SCCSource is a source of copy to SCC), then the select is semantically 6469 // equivalent to copying SCCSource. Hence, there is no need to create 6470 // V_CNDMASK, we can just use that and bail out. 6471 if (!IsSCC && Src0.isImm() && (Src0.getImm() == -1) && Src1.isImm() && 6472 (Src1.getImm() == 0)) { 6473 MRI.replaceRegWith(Dest.getReg(), SCCSource); 6474 return; 6475 } 6476 6477 const TargetRegisterClass *TC = 6478 RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 6479 6480 Register CopySCC = MRI.createVirtualRegister(TC); 6481 6482 if (IsSCC) { 6483 // Now look for the closest SCC def if it is a copy 6484 // replacing the SCCSource with the COPY source register 6485 bool CopyFound = false; 6486 for (MachineInstr &CandI : 6487 make_range(std::next(MachineBasicBlock::reverse_iterator(Inst)), 6488 Inst.getParent()->rend())) { 6489 if (CandI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != 6490 -1) { 6491 if (CandI.isCopy() && CandI.getOperand(0).getReg() == AMDGPU::SCC) { 6492 BuildMI(MBB, MII, DL, get(AMDGPU::COPY), CopySCC) 6493 .addReg(CandI.getOperand(1).getReg()); 6494 CopyFound = true; 6495 } 6496 break; 6497 } 6498 } 6499 if (!CopyFound) { 6500 // SCC def is not a copy 6501 // Insert a trivial select instead of creating a copy, because a copy from 6502 // SCC would semantically mean just copying a single bit, but we may need 6503 // the result to be a vector condition mask that needs preserving. 6504 unsigned Opcode = (ST.getWavefrontSize() == 64) ? AMDGPU::S_CSELECT_B64 6505 : AMDGPU::S_CSELECT_B32; 6506 auto NewSelect = 6507 BuildMI(MBB, MII, DL, get(Opcode), CopySCC).addImm(-1).addImm(0); 6508 NewSelect->getOperand(3).setIsUndef(Cond.isUndef()); 6509 } 6510 } 6511 6512 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6513 6514 auto UpdatedInst = 6515 BuildMI(MBB, MII, DL, get(AMDGPU::V_CNDMASK_B32_e64), ResultReg) 6516 .addImm(0) 6517 .add(Src1) // False 6518 .addImm(0) 6519 .add(Src0) // True 6520 .addReg(IsSCC ? CopySCC : SCCSource); 6521 6522 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6523 legalizeOperands(*UpdatedInst, MDT); 6524 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6525 } 6526 6527 void SIInstrInfo::lowerScalarAbs(SetVectorType &Worklist, 6528 MachineInstr &Inst) const { 6529 MachineBasicBlock &MBB = *Inst.getParent(); 6530 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6531 MachineBasicBlock::iterator MII = Inst; 6532 DebugLoc DL = Inst.getDebugLoc(); 6533 6534 MachineOperand &Dest = Inst.getOperand(0); 6535 MachineOperand &Src = Inst.getOperand(1); 6536 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6537 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6538 6539 unsigned SubOp = ST.hasAddNoCarry() ? 6540 AMDGPU::V_SUB_U32_e32 : AMDGPU::V_SUB_CO_U32_e32; 6541 6542 BuildMI(MBB, MII, DL, get(SubOp), TmpReg) 6543 .addImm(0) 6544 .addReg(Src.getReg()); 6545 6546 BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg) 6547 .addReg(Src.getReg()) 6548 .addReg(TmpReg); 6549 6550 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6551 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6552 } 6553 6554 void SIInstrInfo::lowerScalarXnor(SetVectorType &Worklist, 6555 MachineInstr &Inst) const { 6556 MachineBasicBlock &MBB = *Inst.getParent(); 6557 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6558 MachineBasicBlock::iterator MII = Inst; 6559 const DebugLoc &DL = Inst.getDebugLoc(); 6560 6561 MachineOperand &Dest = Inst.getOperand(0); 6562 MachineOperand &Src0 = Inst.getOperand(1); 6563 MachineOperand &Src1 = Inst.getOperand(2); 6564 6565 if (ST.hasDLInsts()) { 6566 Register NewDest = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6567 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src0, MRI, DL); 6568 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src1, MRI, DL); 6569 6570 BuildMI(MBB, MII, DL, get(AMDGPU::V_XNOR_B32_e64), NewDest) 6571 .add(Src0) 6572 .add(Src1); 6573 6574 MRI.replaceRegWith(Dest.getReg(), NewDest); 6575 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6576 } else { 6577 // Using the identity !(x ^ y) == (!x ^ y) == (x ^ !y), we can 6578 // invert either source and then perform the XOR. If either source is a 6579 // scalar register, then we can leave the inversion on the scalar unit to 6580 // achieve a better distribution of scalar and vector instructions. 6581 bool Src0IsSGPR = Src0.isReg() && 6582 RI.isSGPRClass(MRI.getRegClass(Src0.getReg())); 6583 bool Src1IsSGPR = Src1.isReg() && 6584 RI.isSGPRClass(MRI.getRegClass(Src1.getReg())); 6585 MachineInstr *Xor; 6586 Register Temp = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6587 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6588 6589 // Build a pair of scalar instructions and add them to the work list. 6590 // The next iteration over the work list will lower these to the vector 6591 // unit as necessary. 6592 if (Src0IsSGPR) { 6593 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src0); 6594 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest) 6595 .addReg(Temp) 6596 .add(Src1); 6597 } else if (Src1IsSGPR) { 6598 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp).add(Src1); 6599 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest) 6600 .add(Src0) 6601 .addReg(Temp); 6602 } else { 6603 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), Temp) 6604 .add(Src0) 6605 .add(Src1); 6606 MachineInstr *Not = 6607 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest).addReg(Temp); 6608 Worklist.insert(Not); 6609 } 6610 6611 MRI.replaceRegWith(Dest.getReg(), NewDest); 6612 6613 Worklist.insert(Xor); 6614 6615 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6616 } 6617 } 6618 6619 void SIInstrInfo::splitScalarNotBinop(SetVectorType &Worklist, 6620 MachineInstr &Inst, 6621 unsigned Opcode) const { 6622 MachineBasicBlock &MBB = *Inst.getParent(); 6623 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6624 MachineBasicBlock::iterator MII = Inst; 6625 const DebugLoc &DL = Inst.getDebugLoc(); 6626 6627 MachineOperand &Dest = Inst.getOperand(0); 6628 MachineOperand &Src0 = Inst.getOperand(1); 6629 MachineOperand &Src1 = Inst.getOperand(2); 6630 6631 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6632 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass); 6633 6634 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), Interm) 6635 .add(Src0) 6636 .add(Src1); 6637 6638 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest) 6639 .addReg(Interm); 6640 6641 Worklist.insert(&Op); 6642 Worklist.insert(&Not); 6643 6644 MRI.replaceRegWith(Dest.getReg(), NewDest); 6645 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6646 } 6647 6648 void SIInstrInfo::splitScalarBinOpN2(SetVectorType& Worklist, 6649 MachineInstr &Inst, 6650 unsigned Opcode) const { 6651 MachineBasicBlock &MBB = *Inst.getParent(); 6652 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6653 MachineBasicBlock::iterator MII = Inst; 6654 const DebugLoc &DL = Inst.getDebugLoc(); 6655 6656 MachineOperand &Dest = Inst.getOperand(0); 6657 MachineOperand &Src0 = Inst.getOperand(1); 6658 MachineOperand &Src1 = Inst.getOperand(2); 6659 6660 Register NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 6661 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 6662 6663 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Interm) 6664 .add(Src1); 6665 6666 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), NewDest) 6667 .add(Src0) 6668 .addReg(Interm); 6669 6670 Worklist.insert(&Not); 6671 Worklist.insert(&Op); 6672 6673 MRI.replaceRegWith(Dest.getReg(), NewDest); 6674 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 6675 } 6676 6677 void SIInstrInfo::splitScalar64BitUnaryOp( 6678 SetVectorType &Worklist, MachineInstr &Inst, 6679 unsigned Opcode, bool Swap) const { 6680 MachineBasicBlock &MBB = *Inst.getParent(); 6681 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6682 6683 MachineOperand &Dest = Inst.getOperand(0); 6684 MachineOperand &Src0 = Inst.getOperand(1); 6685 DebugLoc DL = Inst.getDebugLoc(); 6686 6687 MachineBasicBlock::iterator MII = Inst; 6688 6689 const MCInstrDesc &InstDesc = get(Opcode); 6690 const TargetRegisterClass *Src0RC = Src0.isReg() ? 6691 MRI.getRegClass(Src0.getReg()) : 6692 &AMDGPU::SGPR_32RegClass; 6693 6694 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 6695 6696 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6697 AMDGPU::sub0, Src0SubRC); 6698 6699 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 6700 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 6701 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 6702 6703 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 6704 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0).add(SrcReg0Sub0); 6705 6706 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6707 AMDGPU::sub1, Src0SubRC); 6708 6709 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 6710 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1).add(SrcReg0Sub1); 6711 6712 if (Swap) 6713 std::swap(DestSub0, DestSub1); 6714 6715 Register FullDestReg = MRI.createVirtualRegister(NewDestRC); 6716 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 6717 .addReg(DestSub0) 6718 .addImm(AMDGPU::sub0) 6719 .addReg(DestSub1) 6720 .addImm(AMDGPU::sub1); 6721 6722 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 6723 6724 Worklist.insert(&LoHalf); 6725 Worklist.insert(&HiHalf); 6726 6727 // We don't need to legalizeOperands here because for a single operand, src0 6728 // will support any kind of input. 6729 6730 // Move all users of this moved value. 6731 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 6732 } 6733 6734 void SIInstrInfo::splitScalar64BitAddSub(SetVectorType &Worklist, 6735 MachineInstr &Inst, 6736 MachineDominatorTree *MDT) const { 6737 bool IsAdd = (Inst.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO); 6738 6739 MachineBasicBlock &MBB = *Inst.getParent(); 6740 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6741 const auto *CarryRC = RI.getRegClass(AMDGPU::SReg_1_XEXECRegClassID); 6742 6743 Register FullDestReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 6744 Register DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6745 Register DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6746 6747 Register CarryReg = MRI.createVirtualRegister(CarryRC); 6748 Register DeadCarryReg = MRI.createVirtualRegister(CarryRC); 6749 6750 MachineOperand &Dest = Inst.getOperand(0); 6751 MachineOperand &Src0 = Inst.getOperand(1); 6752 MachineOperand &Src1 = Inst.getOperand(2); 6753 const DebugLoc &DL = Inst.getDebugLoc(); 6754 MachineBasicBlock::iterator MII = Inst; 6755 6756 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0.getReg()); 6757 const TargetRegisterClass *Src1RC = MRI.getRegClass(Src1.getReg()); 6758 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 6759 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); 6760 6761 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6762 AMDGPU::sub0, Src0SubRC); 6763 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6764 AMDGPU::sub0, Src1SubRC); 6765 6766 6767 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6768 AMDGPU::sub1, Src0SubRC); 6769 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6770 AMDGPU::sub1, Src1SubRC); 6771 6772 unsigned LoOpc = IsAdd ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_SUB_CO_U32_e64; 6773 MachineInstr *LoHalf = 6774 BuildMI(MBB, MII, DL, get(LoOpc), DestSub0) 6775 .addReg(CarryReg, RegState::Define) 6776 .add(SrcReg0Sub0) 6777 .add(SrcReg1Sub0) 6778 .addImm(0); // clamp bit 6779 6780 unsigned HiOpc = IsAdd ? AMDGPU::V_ADDC_U32_e64 : AMDGPU::V_SUBB_U32_e64; 6781 MachineInstr *HiHalf = 6782 BuildMI(MBB, MII, DL, get(HiOpc), DestSub1) 6783 .addReg(DeadCarryReg, RegState::Define | RegState::Dead) 6784 .add(SrcReg0Sub1) 6785 .add(SrcReg1Sub1) 6786 .addReg(CarryReg, RegState::Kill) 6787 .addImm(0); // clamp bit 6788 6789 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 6790 .addReg(DestSub0) 6791 .addImm(AMDGPU::sub0) 6792 .addReg(DestSub1) 6793 .addImm(AMDGPU::sub1); 6794 6795 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 6796 6797 // Try to legalize the operands in case we need to swap the order to keep it 6798 // valid. 6799 legalizeOperands(*LoHalf, MDT); 6800 legalizeOperands(*HiHalf, MDT); 6801 6802 // Move all users of this moved value. 6803 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 6804 } 6805 6806 void SIInstrInfo::splitScalar64BitBinaryOp(SetVectorType &Worklist, 6807 MachineInstr &Inst, unsigned Opcode, 6808 MachineDominatorTree *MDT) const { 6809 MachineBasicBlock &MBB = *Inst.getParent(); 6810 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6811 6812 MachineOperand &Dest = Inst.getOperand(0); 6813 MachineOperand &Src0 = Inst.getOperand(1); 6814 MachineOperand &Src1 = Inst.getOperand(2); 6815 DebugLoc DL = Inst.getDebugLoc(); 6816 6817 MachineBasicBlock::iterator MII = Inst; 6818 6819 const MCInstrDesc &InstDesc = get(Opcode); 6820 const TargetRegisterClass *Src0RC = Src0.isReg() ? 6821 MRI.getRegClass(Src0.getReg()) : 6822 &AMDGPU::SGPR_32RegClass; 6823 6824 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 6825 const TargetRegisterClass *Src1RC = Src1.isReg() ? 6826 MRI.getRegClass(Src1.getReg()) : 6827 &AMDGPU::SGPR_32RegClass; 6828 6829 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); 6830 6831 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6832 AMDGPU::sub0, Src0SubRC); 6833 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6834 AMDGPU::sub0, Src1SubRC); 6835 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 6836 AMDGPU::sub1, Src0SubRC); 6837 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 6838 AMDGPU::sub1, Src1SubRC); 6839 6840 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 6841 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 6842 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 6843 6844 Register DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 6845 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0) 6846 .add(SrcReg0Sub0) 6847 .add(SrcReg1Sub0); 6848 6849 Register DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 6850 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1) 6851 .add(SrcReg0Sub1) 6852 .add(SrcReg1Sub1); 6853 6854 Register FullDestReg = MRI.createVirtualRegister(NewDestRC); 6855 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 6856 .addReg(DestSub0) 6857 .addImm(AMDGPU::sub0) 6858 .addReg(DestSub1) 6859 .addImm(AMDGPU::sub1); 6860 6861 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 6862 6863 Worklist.insert(&LoHalf); 6864 Worklist.insert(&HiHalf); 6865 6866 // Move all users of this moved value. 6867 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 6868 } 6869 6870 void SIInstrInfo::splitScalar64BitXnor(SetVectorType &Worklist, 6871 MachineInstr &Inst, 6872 MachineDominatorTree *MDT) const { 6873 MachineBasicBlock &MBB = *Inst.getParent(); 6874 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6875 6876 MachineOperand &Dest = Inst.getOperand(0); 6877 MachineOperand &Src0 = Inst.getOperand(1); 6878 MachineOperand &Src1 = Inst.getOperand(2); 6879 const DebugLoc &DL = Inst.getDebugLoc(); 6880 6881 MachineBasicBlock::iterator MII = Inst; 6882 6883 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 6884 6885 Register Interm = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 6886 6887 MachineOperand* Op0; 6888 MachineOperand* Op1; 6889 6890 if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) { 6891 Op0 = &Src0; 6892 Op1 = &Src1; 6893 } else { 6894 Op0 = &Src1; 6895 Op1 = &Src0; 6896 } 6897 6898 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B64), Interm) 6899 .add(*Op0); 6900 6901 Register NewDest = MRI.createVirtualRegister(DestRC); 6902 6903 MachineInstr &Xor = *BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B64), NewDest) 6904 .addReg(Interm) 6905 .add(*Op1); 6906 6907 MRI.replaceRegWith(Dest.getReg(), NewDest); 6908 6909 Worklist.insert(&Xor); 6910 } 6911 6912 void SIInstrInfo::splitScalar64BitBCNT( 6913 SetVectorType &Worklist, MachineInstr &Inst) const { 6914 MachineBasicBlock &MBB = *Inst.getParent(); 6915 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6916 6917 MachineBasicBlock::iterator MII = Inst; 6918 const DebugLoc &DL = Inst.getDebugLoc(); 6919 6920 MachineOperand &Dest = Inst.getOperand(0); 6921 MachineOperand &Src = Inst.getOperand(1); 6922 6923 const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64); 6924 const TargetRegisterClass *SrcRC = Src.isReg() ? 6925 MRI.getRegClass(Src.getReg()) : 6926 &AMDGPU::SGPR_32RegClass; 6927 6928 Register MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6929 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6930 6931 const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0); 6932 6933 MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 6934 AMDGPU::sub0, SrcSubRC); 6935 MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 6936 AMDGPU::sub1, SrcSubRC); 6937 6938 BuildMI(MBB, MII, DL, InstDesc, MidReg).add(SrcRegSub0).addImm(0); 6939 6940 BuildMI(MBB, MII, DL, InstDesc, ResultReg).add(SrcRegSub1).addReg(MidReg); 6941 6942 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6943 6944 // We don't need to legalize operands here. src0 for either instruction can be 6945 // an SGPR, and the second input is unused or determined here. 6946 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6947 } 6948 6949 void SIInstrInfo::splitScalar64BitBFE(SetVectorType &Worklist, 6950 MachineInstr &Inst) const { 6951 MachineBasicBlock &MBB = *Inst.getParent(); 6952 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 6953 MachineBasicBlock::iterator MII = Inst; 6954 const DebugLoc &DL = Inst.getDebugLoc(); 6955 6956 MachineOperand &Dest = Inst.getOperand(0); 6957 uint32_t Imm = Inst.getOperand(2).getImm(); 6958 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 6959 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 6960 6961 (void) Offset; 6962 6963 // Only sext_inreg cases handled. 6964 assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 && 6965 Offset == 0 && "Not implemented"); 6966 6967 if (BitWidth < 32) { 6968 Register MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6969 Register MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6970 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 6971 6972 BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32_e64), MidRegLo) 6973 .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0) 6974 .addImm(0) 6975 .addImm(BitWidth); 6976 6977 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi) 6978 .addImm(31) 6979 .addReg(MidRegLo); 6980 6981 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 6982 .addReg(MidRegLo) 6983 .addImm(AMDGPU::sub0) 6984 .addReg(MidRegHi) 6985 .addImm(AMDGPU::sub1); 6986 6987 MRI.replaceRegWith(Dest.getReg(), ResultReg); 6988 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 6989 return; 6990 } 6991 6992 MachineOperand &Src = Inst.getOperand(1); 6993 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 6994 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 6995 6996 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg) 6997 .addImm(31) 6998 .addReg(Src.getReg(), 0, AMDGPU::sub0); 6999 7000 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 7001 .addReg(Src.getReg(), 0, AMDGPU::sub0) 7002 .addImm(AMDGPU::sub0) 7003 .addReg(TmpReg) 7004 .addImm(AMDGPU::sub1); 7005 7006 MRI.replaceRegWith(Dest.getReg(), ResultReg); 7007 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 7008 } 7009 7010 void SIInstrInfo::addUsersToMoveToVALUWorklist( 7011 Register DstReg, 7012 MachineRegisterInfo &MRI, 7013 SetVectorType &Worklist) const { 7014 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg), 7015 E = MRI.use_end(); I != E;) { 7016 MachineInstr &UseMI = *I->getParent(); 7017 7018 unsigned OpNo = 0; 7019 7020 switch (UseMI.getOpcode()) { 7021 case AMDGPU::COPY: 7022 case AMDGPU::WQM: 7023 case AMDGPU::SOFT_WQM: 7024 case AMDGPU::STRICT_WWM: 7025 case AMDGPU::STRICT_WQM: 7026 case AMDGPU::REG_SEQUENCE: 7027 case AMDGPU::PHI: 7028 case AMDGPU::INSERT_SUBREG: 7029 break; 7030 default: 7031 OpNo = I.getOperandNo(); 7032 break; 7033 } 7034 7035 if (!RI.hasVectorRegisters(getOpRegClass(UseMI, OpNo))) { 7036 Worklist.insert(&UseMI); 7037 7038 do { 7039 ++I; 7040 } while (I != E && I->getParent() == &UseMI); 7041 } else { 7042 ++I; 7043 } 7044 } 7045 } 7046 7047 void SIInstrInfo::movePackToVALU(SetVectorType &Worklist, 7048 MachineRegisterInfo &MRI, 7049 MachineInstr &Inst) const { 7050 Register ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7051 MachineBasicBlock *MBB = Inst.getParent(); 7052 MachineOperand &Src0 = Inst.getOperand(1); 7053 MachineOperand &Src1 = Inst.getOperand(2); 7054 const DebugLoc &DL = Inst.getDebugLoc(); 7055 7056 switch (Inst.getOpcode()) { 7057 case AMDGPU::S_PACK_LL_B32_B16: { 7058 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7059 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7060 7061 // FIXME: Can do a lot better if we know the high bits of src0 or src1 are 7062 // 0. 7063 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 7064 .addImm(0xffff); 7065 7066 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_B32_e64), TmpReg) 7067 .addReg(ImmReg, RegState::Kill) 7068 .add(Src0); 7069 7070 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32_e64), ResultReg) 7071 .add(Src1) 7072 .addImm(16) 7073 .addReg(TmpReg, RegState::Kill); 7074 break; 7075 } 7076 case AMDGPU::S_PACK_LH_B32_B16: { 7077 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7078 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 7079 .addImm(0xffff); 7080 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_BFI_B32_e64), ResultReg) 7081 .addReg(ImmReg, RegState::Kill) 7082 .add(Src0) 7083 .add(Src1); 7084 break; 7085 } 7086 case AMDGPU::S_PACK_HH_B32_B16: { 7087 Register ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7088 Register TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 7089 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg) 7090 .addImm(16) 7091 .add(Src0); 7092 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 7093 .addImm(0xffff0000); 7094 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_OR_B32_e64), ResultReg) 7095 .add(Src1) 7096 .addReg(ImmReg, RegState::Kill) 7097 .addReg(TmpReg, RegState::Kill); 7098 break; 7099 } 7100 default: 7101 llvm_unreachable("unhandled s_pack_* instruction"); 7102 } 7103 7104 MachineOperand &Dest = Inst.getOperand(0); 7105 MRI.replaceRegWith(Dest.getReg(), ResultReg); 7106 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 7107 } 7108 7109 void SIInstrInfo::addSCCDefUsersToVALUWorklist(MachineOperand &Op, 7110 MachineInstr &SCCDefInst, 7111 SetVectorType &Worklist, 7112 Register NewCond) const { 7113 7114 // Ensure that def inst defines SCC, which is still live. 7115 assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isDef() && 7116 !Op.isDead() && Op.getParent() == &SCCDefInst); 7117 SmallVector<MachineInstr *, 4> CopyToDelete; 7118 // This assumes that all the users of SCC are in the same block 7119 // as the SCC def. 7120 for (MachineInstr &MI : // Skip the def inst itself. 7121 make_range(std::next(MachineBasicBlock::iterator(SCCDefInst)), 7122 SCCDefInst.getParent()->end())) { 7123 // Check if SCC is used first. 7124 int SCCIdx = MI.findRegisterUseOperandIdx(AMDGPU::SCC, false, &RI); 7125 if (SCCIdx != -1) { 7126 if (MI.isCopy()) { 7127 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 7128 Register DestReg = MI.getOperand(0).getReg(); 7129 7130 MRI.replaceRegWith(DestReg, NewCond); 7131 CopyToDelete.push_back(&MI); 7132 } else { 7133 7134 if (NewCond.isValid()) 7135 MI.getOperand(SCCIdx).setReg(NewCond); 7136 7137 Worklist.insert(&MI); 7138 } 7139 } 7140 // Exit if we find another SCC def. 7141 if (MI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != -1) 7142 break; 7143 } 7144 for (auto &Copy : CopyToDelete) 7145 Copy->eraseFromParent(); 7146 } 7147 7148 // Instructions that use SCC may be converted to VALU instructions. When that 7149 // happens, the SCC register is changed to VCC_LO. The instruction that defines 7150 // SCC must be changed to an instruction that defines VCC. This function makes 7151 // sure that the instruction that defines SCC is added to the moveToVALU 7152 // worklist. 7153 void SIInstrInfo::addSCCDefsToVALUWorklist(MachineOperand &Op, 7154 SetVectorType &Worklist) const { 7155 assert(Op.isReg() && Op.getReg() == AMDGPU::SCC && Op.isUse()); 7156 7157 MachineInstr *SCCUseInst = Op.getParent(); 7158 // Look for a preceding instruction that either defines VCC or SCC. If VCC 7159 // then there is nothing to do because the defining instruction has been 7160 // converted to a VALU already. If SCC then that instruction needs to be 7161 // converted to a VALU. 7162 for (MachineInstr &MI : 7163 make_range(std::next(MachineBasicBlock::reverse_iterator(SCCUseInst)), 7164 SCCUseInst->getParent()->rend())) { 7165 if (MI.modifiesRegister(AMDGPU::VCC, &RI)) 7166 break; 7167 if (MI.definesRegister(AMDGPU::SCC, &RI)) { 7168 Worklist.insert(&MI); 7169 break; 7170 } 7171 } 7172 } 7173 7174 const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass( 7175 const MachineInstr &Inst) const { 7176 const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0); 7177 7178 switch (Inst.getOpcode()) { 7179 // For target instructions, getOpRegClass just returns the virtual register 7180 // class associated with the operand, so we need to find an equivalent VGPR 7181 // register class in order to move the instruction to the VALU. 7182 case AMDGPU::COPY: 7183 case AMDGPU::PHI: 7184 case AMDGPU::REG_SEQUENCE: 7185 case AMDGPU::INSERT_SUBREG: 7186 case AMDGPU::WQM: 7187 case AMDGPU::SOFT_WQM: 7188 case AMDGPU::STRICT_WWM: 7189 case AMDGPU::STRICT_WQM: { 7190 const TargetRegisterClass *SrcRC = getOpRegClass(Inst, 1); 7191 if (RI.isAGPRClass(SrcRC)) { 7192 if (RI.isAGPRClass(NewDstRC)) 7193 return nullptr; 7194 7195 switch (Inst.getOpcode()) { 7196 case AMDGPU::PHI: 7197 case AMDGPU::REG_SEQUENCE: 7198 case AMDGPU::INSERT_SUBREG: 7199 NewDstRC = RI.getEquivalentAGPRClass(NewDstRC); 7200 break; 7201 default: 7202 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 7203 } 7204 7205 if (!NewDstRC) 7206 return nullptr; 7207 } else { 7208 if (RI.isVGPRClass(NewDstRC) || NewDstRC == &AMDGPU::VReg_1RegClass) 7209 return nullptr; 7210 7211 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 7212 if (!NewDstRC) 7213 return nullptr; 7214 } 7215 7216 return NewDstRC; 7217 } 7218 default: 7219 return NewDstRC; 7220 } 7221 } 7222 7223 // Find the one SGPR operand we are allowed to use. 7224 Register SIInstrInfo::findUsedSGPR(const MachineInstr &MI, 7225 int OpIndices[3]) const { 7226 const MCInstrDesc &Desc = MI.getDesc(); 7227 7228 // Find the one SGPR operand we are allowed to use. 7229 // 7230 // First we need to consider the instruction's operand requirements before 7231 // legalizing. Some operands are required to be SGPRs, such as implicit uses 7232 // of VCC, but we are still bound by the constant bus requirement to only use 7233 // one. 7234 // 7235 // If the operand's class is an SGPR, we can never move it. 7236 7237 Register SGPRReg = findImplicitSGPRRead(MI); 7238 if (SGPRReg != AMDGPU::NoRegister) 7239 return SGPRReg; 7240 7241 Register UsedSGPRs[3] = { AMDGPU::NoRegister }; 7242 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 7243 7244 for (unsigned i = 0; i < 3; ++i) { 7245 int Idx = OpIndices[i]; 7246 if (Idx == -1) 7247 break; 7248 7249 const MachineOperand &MO = MI.getOperand(Idx); 7250 if (!MO.isReg()) 7251 continue; 7252 7253 // Is this operand statically required to be an SGPR based on the operand 7254 // constraints? 7255 const TargetRegisterClass *OpRC = RI.getRegClass(Desc.OpInfo[Idx].RegClass); 7256 bool IsRequiredSGPR = RI.isSGPRClass(OpRC); 7257 if (IsRequiredSGPR) 7258 return MO.getReg(); 7259 7260 // If this could be a VGPR or an SGPR, Check the dynamic register class. 7261 Register Reg = MO.getReg(); 7262 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg); 7263 if (RI.isSGPRClass(RegRC)) 7264 UsedSGPRs[i] = Reg; 7265 } 7266 7267 // We don't have a required SGPR operand, so we have a bit more freedom in 7268 // selecting operands to move. 7269 7270 // Try to select the most used SGPR. If an SGPR is equal to one of the 7271 // others, we choose that. 7272 // 7273 // e.g. 7274 // V_FMA_F32 v0, s0, s0, s0 -> No moves 7275 // V_FMA_F32 v0, s0, s1, s0 -> Move s1 7276 7277 // TODO: If some of the operands are 64-bit SGPRs and some 32, we should 7278 // prefer those. 7279 7280 if (UsedSGPRs[0] != AMDGPU::NoRegister) { 7281 if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2]) 7282 SGPRReg = UsedSGPRs[0]; 7283 } 7284 7285 if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) { 7286 if (UsedSGPRs[1] == UsedSGPRs[2]) 7287 SGPRReg = UsedSGPRs[1]; 7288 } 7289 7290 return SGPRReg; 7291 } 7292 7293 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI, 7294 unsigned OperandName) const { 7295 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName); 7296 if (Idx == -1) 7297 return nullptr; 7298 7299 return &MI.getOperand(Idx); 7300 } 7301 7302 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const { 7303 if (ST.getGeneration() >= AMDGPUSubtarget::GFX10) { 7304 return (AMDGPU::MTBUFFormat::UFMT_32_FLOAT << 44) | 7305 (1ULL << 56) | // RESOURCE_LEVEL = 1 7306 (3ULL << 60); // OOB_SELECT = 3 7307 } 7308 7309 uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT; 7310 if (ST.isAmdHsaOS()) { 7311 // Set ATC = 1. GFX9 doesn't have this bit. 7312 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) 7313 RsrcDataFormat |= (1ULL << 56); 7314 7315 // Set MTYPE = 2 (MTYPE_UC = uncached). GFX9 doesn't have this. 7316 // BTW, it disables TC L2 and therefore decreases performance. 7317 if (ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) 7318 RsrcDataFormat |= (2ULL << 59); 7319 } 7320 7321 return RsrcDataFormat; 7322 } 7323 7324 uint64_t SIInstrInfo::getScratchRsrcWords23() const { 7325 uint64_t Rsrc23 = getDefaultRsrcDataFormat() | 7326 AMDGPU::RSRC_TID_ENABLE | 7327 0xffffffff; // Size; 7328 7329 // GFX9 doesn't have ELEMENT_SIZE. 7330 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 7331 uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize(true)) - 1; 7332 Rsrc23 |= EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT; 7333 } 7334 7335 // IndexStride = 64 / 32. 7336 uint64_t IndexStride = ST.getWavefrontSize() == 64 ? 3 : 2; 7337 Rsrc23 |= IndexStride << AMDGPU::RSRC_INDEX_STRIDE_SHIFT; 7338 7339 // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17]. 7340 // Clear them unless we want a huge stride. 7341 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS && 7342 ST.getGeneration() <= AMDGPUSubtarget::GFX9) 7343 Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT; 7344 7345 return Rsrc23; 7346 } 7347 7348 bool SIInstrInfo::isLowLatencyInstruction(const MachineInstr &MI) const { 7349 unsigned Opc = MI.getOpcode(); 7350 7351 return isSMRD(Opc); 7352 } 7353 7354 bool SIInstrInfo::isHighLatencyDef(int Opc) const { 7355 return get(Opc).mayLoad() && 7356 (isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc) || isFLAT(Opc)); 7357 } 7358 7359 unsigned SIInstrInfo::isStackAccess(const MachineInstr &MI, 7360 int &FrameIndex) const { 7361 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 7362 if (!Addr || !Addr->isFI()) 7363 return AMDGPU::NoRegister; 7364 7365 assert(!MI.memoperands_empty() && 7366 (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS); 7367 7368 FrameIndex = Addr->getIndex(); 7369 return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg(); 7370 } 7371 7372 unsigned SIInstrInfo::isSGPRStackAccess(const MachineInstr &MI, 7373 int &FrameIndex) const { 7374 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr); 7375 assert(Addr && Addr->isFI()); 7376 FrameIndex = Addr->getIndex(); 7377 return getNamedOperand(MI, AMDGPU::OpName::data)->getReg(); 7378 } 7379 7380 unsigned SIInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 7381 int &FrameIndex) const { 7382 if (!MI.mayLoad()) 7383 return AMDGPU::NoRegister; 7384 7385 if (isMUBUF(MI) || isVGPRSpill(MI)) 7386 return isStackAccess(MI, FrameIndex); 7387 7388 if (isSGPRSpill(MI)) 7389 return isSGPRStackAccess(MI, FrameIndex); 7390 7391 return AMDGPU::NoRegister; 7392 } 7393 7394 unsigned SIInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 7395 int &FrameIndex) const { 7396 if (!MI.mayStore()) 7397 return AMDGPU::NoRegister; 7398 7399 if (isMUBUF(MI) || isVGPRSpill(MI)) 7400 return isStackAccess(MI, FrameIndex); 7401 7402 if (isSGPRSpill(MI)) 7403 return isSGPRStackAccess(MI, FrameIndex); 7404 7405 return AMDGPU::NoRegister; 7406 } 7407 7408 unsigned SIInstrInfo::getInstBundleSize(const MachineInstr &MI) const { 7409 unsigned Size = 0; 7410 MachineBasicBlock::const_instr_iterator I = MI.getIterator(); 7411 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 7412 while (++I != E && I->isInsideBundle()) { 7413 assert(!I->isBundle() && "No nested bundle!"); 7414 Size += getInstSizeInBytes(*I); 7415 } 7416 7417 return Size; 7418 } 7419 7420 unsigned SIInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 7421 unsigned Opc = MI.getOpcode(); 7422 const MCInstrDesc &Desc = getMCOpcodeFromPseudo(Opc); 7423 unsigned DescSize = Desc.getSize(); 7424 7425 // If we have a definitive size, we can use it. Otherwise we need to inspect 7426 // the operands to know the size. 7427 if (isFixedSize(MI)) { 7428 unsigned Size = DescSize; 7429 7430 // If we hit the buggy offset, an extra nop will be inserted in MC so 7431 // estimate the worst case. 7432 if (MI.isBranch() && ST.hasOffset3fBug()) 7433 Size += 4; 7434 7435 return Size; 7436 } 7437 7438 // Instructions may have a 32-bit literal encoded after them. Check 7439 // operands that could ever be literals. 7440 if (isVALU(MI) || isSALU(MI)) { 7441 if (isDPP(MI)) 7442 return DescSize; 7443 bool HasLiteral = false; 7444 for (int I = 0, E = MI.getNumExplicitOperands(); I != E; ++I) { 7445 if (isLiteralConstant(MI, I)) { 7446 HasLiteral = true; 7447 break; 7448 } 7449 } 7450 return HasLiteral ? DescSize + 4 : DescSize; 7451 } 7452 7453 // Check whether we have extra NSA words. 7454 if (isMIMG(MI)) { 7455 int VAddr0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::vaddr0); 7456 if (VAddr0Idx < 0) 7457 return 8; 7458 7459 int RSrcIdx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::srsrc); 7460 return 8 + 4 * ((RSrcIdx - VAddr0Idx + 2) / 4); 7461 } 7462 7463 switch (Opc) { 7464 case TargetOpcode::BUNDLE: 7465 return getInstBundleSize(MI); 7466 case TargetOpcode::INLINEASM: 7467 case TargetOpcode::INLINEASM_BR: { 7468 const MachineFunction *MF = MI.getParent()->getParent(); 7469 const char *AsmStr = MI.getOperand(0).getSymbolName(); 7470 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo(), &ST); 7471 } 7472 default: 7473 if (MI.isMetaInstruction()) 7474 return 0; 7475 return DescSize; 7476 } 7477 } 7478 7479 bool SIInstrInfo::mayAccessFlatAddressSpace(const MachineInstr &MI) const { 7480 if (!isFLAT(MI)) 7481 return false; 7482 7483 if (MI.memoperands_empty()) 7484 return true; 7485 7486 for (const MachineMemOperand *MMO : MI.memoperands()) { 7487 if (MMO->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS) 7488 return true; 7489 } 7490 return false; 7491 } 7492 7493 bool SIInstrInfo::isNonUniformBranchInstr(MachineInstr &Branch) const { 7494 return Branch.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO; 7495 } 7496 7497 void SIInstrInfo::convertNonUniformIfRegion(MachineBasicBlock *IfEntry, 7498 MachineBasicBlock *IfEnd) const { 7499 MachineBasicBlock::iterator TI = IfEntry->getFirstTerminator(); 7500 assert(TI != IfEntry->end()); 7501 7502 MachineInstr *Branch = &(*TI); 7503 MachineFunction *MF = IfEntry->getParent(); 7504 MachineRegisterInfo &MRI = IfEntry->getParent()->getRegInfo(); 7505 7506 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 7507 Register DstReg = MRI.createVirtualRegister(RI.getBoolRC()); 7508 MachineInstr *SIIF = 7509 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_IF), DstReg) 7510 .add(Branch->getOperand(0)) 7511 .add(Branch->getOperand(1)); 7512 MachineInstr *SIEND = 7513 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_END_CF)) 7514 .addReg(DstReg); 7515 7516 IfEntry->erase(TI); 7517 IfEntry->insert(IfEntry->end(), SIIF); 7518 IfEnd->insert(IfEnd->getFirstNonPHI(), SIEND); 7519 } 7520 } 7521 7522 void SIInstrInfo::convertNonUniformLoopRegion( 7523 MachineBasicBlock *LoopEntry, MachineBasicBlock *LoopEnd) const { 7524 MachineBasicBlock::iterator TI = LoopEnd->getFirstTerminator(); 7525 // We expect 2 terminators, one conditional and one unconditional. 7526 assert(TI != LoopEnd->end()); 7527 7528 MachineInstr *Branch = &(*TI); 7529 MachineFunction *MF = LoopEnd->getParent(); 7530 MachineRegisterInfo &MRI = LoopEnd->getParent()->getRegInfo(); 7531 7532 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 7533 7534 Register DstReg = MRI.createVirtualRegister(RI.getBoolRC()); 7535 Register BackEdgeReg = MRI.createVirtualRegister(RI.getBoolRC()); 7536 MachineInstrBuilder HeaderPHIBuilder = 7537 BuildMI(*(MF), Branch->getDebugLoc(), get(TargetOpcode::PHI), DstReg); 7538 for (MachineBasicBlock *PMBB : LoopEntry->predecessors()) { 7539 if (PMBB == LoopEnd) { 7540 HeaderPHIBuilder.addReg(BackEdgeReg); 7541 } else { 7542 Register ZeroReg = MRI.createVirtualRegister(RI.getBoolRC()); 7543 materializeImmediate(*PMBB, PMBB->getFirstTerminator(), DebugLoc(), 7544 ZeroReg, 0); 7545 HeaderPHIBuilder.addReg(ZeroReg); 7546 } 7547 HeaderPHIBuilder.addMBB(PMBB); 7548 } 7549 MachineInstr *HeaderPhi = HeaderPHIBuilder; 7550 MachineInstr *SIIFBREAK = BuildMI(*(MF), Branch->getDebugLoc(), 7551 get(AMDGPU::SI_IF_BREAK), BackEdgeReg) 7552 .addReg(DstReg) 7553 .add(Branch->getOperand(0)); 7554 MachineInstr *SILOOP = 7555 BuildMI(*(MF), Branch->getDebugLoc(), get(AMDGPU::SI_LOOP)) 7556 .addReg(BackEdgeReg) 7557 .addMBB(LoopEntry); 7558 7559 LoopEntry->insert(LoopEntry->begin(), HeaderPhi); 7560 LoopEnd->erase(TI); 7561 LoopEnd->insert(LoopEnd->end(), SIIFBREAK); 7562 LoopEnd->insert(LoopEnd->end(), SILOOP); 7563 } 7564 } 7565 7566 ArrayRef<std::pair<int, const char *>> 7567 SIInstrInfo::getSerializableTargetIndices() const { 7568 static const std::pair<int, const char *> TargetIndices[] = { 7569 {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"}, 7570 {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"}, 7571 {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"}, 7572 {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"}, 7573 {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}}; 7574 return makeArrayRef(TargetIndices); 7575 } 7576 7577 /// This is used by the post-RA scheduler (SchedulePostRAList.cpp). The 7578 /// post-RA version of misched uses CreateTargetMIHazardRecognizer. 7579 ScheduleHazardRecognizer * 7580 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 7581 const ScheduleDAG *DAG) const { 7582 return new GCNHazardRecognizer(DAG->MF); 7583 } 7584 7585 /// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer 7586 /// pass. 7587 ScheduleHazardRecognizer * 7588 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const { 7589 return new GCNHazardRecognizer(MF); 7590 } 7591 7592 // Called during: 7593 // - pre-RA scheduling and post-RA scheduling 7594 ScheduleHazardRecognizer * 7595 SIInstrInfo::CreateTargetMIHazardRecognizer(const InstrItineraryData *II, 7596 const ScheduleDAGMI *DAG) const { 7597 // Borrowed from Arm Target 7598 // We would like to restrict this hazard recognizer to only 7599 // post-RA scheduling; we can tell that we're post-RA because we don't 7600 // track VRegLiveness. 7601 if (!DAG->hasVRegLiveness()) 7602 return new GCNHazardRecognizer(DAG->MF); 7603 return TargetInstrInfo::CreateTargetMIHazardRecognizer(II, DAG); 7604 } 7605 7606 std::pair<unsigned, unsigned> 7607 SIInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 7608 return std::make_pair(TF & MO_MASK, TF & ~MO_MASK); 7609 } 7610 7611 ArrayRef<std::pair<unsigned, const char *>> 7612 SIInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 7613 static const std::pair<unsigned, const char *> TargetFlags[] = { 7614 { MO_GOTPCREL, "amdgpu-gotprel" }, 7615 { MO_GOTPCREL32_LO, "amdgpu-gotprel32-lo" }, 7616 { MO_GOTPCREL32_HI, "amdgpu-gotprel32-hi" }, 7617 { MO_REL32_LO, "amdgpu-rel32-lo" }, 7618 { MO_REL32_HI, "amdgpu-rel32-hi" }, 7619 { MO_ABS32_LO, "amdgpu-abs32-lo" }, 7620 { MO_ABS32_HI, "amdgpu-abs32-hi" }, 7621 }; 7622 7623 return makeArrayRef(TargetFlags); 7624 } 7625 7626 ArrayRef<std::pair<MachineMemOperand::Flags, const char *>> 7627 SIInstrInfo::getSerializableMachineMemOperandTargetFlags() const { 7628 static const std::pair<MachineMemOperand::Flags, const char *> TargetFlags[] = 7629 { 7630 {MONoClobber, "amdgpu-noclobber"}, 7631 }; 7632 7633 return makeArrayRef(TargetFlags); 7634 } 7635 7636 bool SIInstrInfo::isBasicBlockPrologue(const MachineInstr &MI) const { 7637 return !MI.isTerminator() && MI.getOpcode() != AMDGPU::COPY && 7638 MI.modifiesRegister(AMDGPU::EXEC, &RI); 7639 } 7640 7641 MachineInstrBuilder 7642 SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB, 7643 MachineBasicBlock::iterator I, 7644 const DebugLoc &DL, 7645 Register DestReg) const { 7646 if (ST.hasAddNoCarry()) 7647 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e64), DestReg); 7648 7649 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 7650 Register UnusedCarry = MRI.createVirtualRegister(RI.getBoolRC()); 7651 MRI.setRegAllocationHint(UnusedCarry, 0, RI.getVCC()); 7652 7653 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg) 7654 .addReg(UnusedCarry, RegState::Define | RegState::Dead); 7655 } 7656 7657 MachineInstrBuilder SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB, 7658 MachineBasicBlock::iterator I, 7659 const DebugLoc &DL, 7660 Register DestReg, 7661 RegScavenger &RS) const { 7662 if (ST.hasAddNoCarry()) 7663 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e32), DestReg); 7664 7665 // If available, prefer to use vcc. 7666 Register UnusedCarry = !RS.isRegUsed(AMDGPU::VCC) 7667 ? Register(RI.getVCC()) 7668 : RS.scavengeRegister(RI.getBoolRC(), I, 0, false); 7669 7670 // TODO: Users need to deal with this. 7671 if (!UnusedCarry.isValid()) 7672 return MachineInstrBuilder(); 7673 7674 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_CO_U32_e64), DestReg) 7675 .addReg(UnusedCarry, RegState::Define | RegState::Dead); 7676 } 7677 7678 bool SIInstrInfo::isKillTerminator(unsigned Opcode) { 7679 switch (Opcode) { 7680 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR: 7681 case AMDGPU::SI_KILL_I1_TERMINATOR: 7682 return true; 7683 default: 7684 return false; 7685 } 7686 } 7687 7688 const MCInstrDesc &SIInstrInfo::getKillTerminatorFromPseudo(unsigned Opcode) const { 7689 switch (Opcode) { 7690 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO: 7691 return get(AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR); 7692 case AMDGPU::SI_KILL_I1_PSEUDO: 7693 return get(AMDGPU::SI_KILL_I1_TERMINATOR); 7694 default: 7695 llvm_unreachable("invalid opcode, expected SI_KILL_*_PSEUDO"); 7696 } 7697 } 7698 7699 void SIInstrInfo::fixImplicitOperands(MachineInstr &MI) const { 7700 if (!ST.isWave32()) 7701 return; 7702 7703 for (auto &Op : MI.implicit_operands()) { 7704 if (Op.isReg() && Op.getReg() == AMDGPU::VCC) 7705 Op.setReg(AMDGPU::VCC_LO); 7706 } 7707 } 7708 7709 bool SIInstrInfo::isBufferSMRD(const MachineInstr &MI) const { 7710 if (!isSMRD(MI)) 7711 return false; 7712 7713 // Check that it is using a buffer resource. 7714 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sbase); 7715 if (Idx == -1) // e.g. s_memtime 7716 return false; 7717 7718 const auto RCID = MI.getDesc().OpInfo[Idx].RegClass; 7719 return RI.getRegClass(RCID)->hasSubClassEq(&AMDGPU::SGPR_128RegClass); 7720 } 7721 7722 // Depending on the used address space and instructions, some immediate offsets 7723 // are allowed and some are not. 7724 // In general, flat instruction offsets can only be non-negative, global and 7725 // scratch instruction offsets can also be negative. 7726 // 7727 // There are several bugs related to these offsets: 7728 // On gfx10.1, flat instructions that go into the global address space cannot 7729 // use an offset. 7730 // 7731 // For scratch instructions, the address can be either an SGPR or a VGPR. 7732 // The following offsets can be used, depending on the architecture (x means 7733 // cannot be used): 7734 // +----------------------------+------+------+ 7735 // | Address-Mode | SGPR | VGPR | 7736 // +----------------------------+------+------+ 7737 // | gfx9 | | | 7738 // | negative, 4-aligned offset | x | ok | 7739 // | negative, unaligned offset | x | ok | 7740 // +----------------------------+------+------+ 7741 // | gfx10 | | | 7742 // | negative, 4-aligned offset | ok | ok | 7743 // | negative, unaligned offset | ok | x | 7744 // +----------------------------+------+------+ 7745 // | gfx10.3 | | | 7746 // | negative, 4-aligned offset | ok | ok | 7747 // | negative, unaligned offset | ok | ok | 7748 // +----------------------------+------+------+ 7749 // 7750 // This function ignores the addressing mode, so if an offset cannot be used in 7751 // one addressing mode, it is considered illegal. 7752 bool SIInstrInfo::isLegalFLATOffset(int64_t Offset, unsigned AddrSpace, 7753 uint64_t FlatVariant) const { 7754 // TODO: Should 0 be special cased? 7755 if (!ST.hasFlatInstOffsets()) 7756 return false; 7757 7758 if (ST.hasFlatSegmentOffsetBug() && FlatVariant == SIInstrFlags::FLAT && 7759 (AddrSpace == AMDGPUAS::FLAT_ADDRESS || 7760 AddrSpace == AMDGPUAS::GLOBAL_ADDRESS)) 7761 return false; 7762 7763 bool Signed = FlatVariant != SIInstrFlags::FLAT; 7764 if (ST.hasNegativeScratchOffsetBug() && 7765 FlatVariant == SIInstrFlags::FlatScratch) 7766 Signed = false; 7767 if (ST.hasNegativeUnalignedScratchOffsetBug() && 7768 FlatVariant == SIInstrFlags::FlatScratch && Offset < 0 && 7769 (Offset % 4) != 0) { 7770 return false; 7771 } 7772 7773 unsigned N = AMDGPU::getNumFlatOffsetBits(ST, Signed); 7774 return Signed ? isIntN(N, Offset) : isUIntN(N, Offset); 7775 } 7776 7777 // See comment on SIInstrInfo::isLegalFLATOffset for what is legal and what not. 7778 std::pair<int64_t, int64_t> 7779 SIInstrInfo::splitFlatOffset(int64_t COffsetVal, unsigned AddrSpace, 7780 uint64_t FlatVariant) const { 7781 int64_t RemainderOffset = COffsetVal; 7782 int64_t ImmField = 0; 7783 bool Signed = FlatVariant != SIInstrFlags::FLAT; 7784 if (ST.hasNegativeScratchOffsetBug() && 7785 FlatVariant == SIInstrFlags::FlatScratch) 7786 Signed = false; 7787 7788 const unsigned NumBits = AMDGPU::getNumFlatOffsetBits(ST, Signed); 7789 if (Signed) { 7790 // Use signed division by a power of two to truncate towards 0. 7791 int64_t D = 1LL << (NumBits - 1); 7792 RemainderOffset = (COffsetVal / D) * D; 7793 ImmField = COffsetVal - RemainderOffset; 7794 7795 if (ST.hasNegativeUnalignedScratchOffsetBug() && 7796 FlatVariant == SIInstrFlags::FlatScratch && ImmField < 0 && 7797 (ImmField % 4) != 0) { 7798 // Make ImmField a multiple of 4 7799 RemainderOffset += ImmField % 4; 7800 ImmField -= ImmField % 4; 7801 } 7802 } else if (COffsetVal >= 0) { 7803 ImmField = COffsetVal & maskTrailingOnes<uint64_t>(NumBits); 7804 RemainderOffset = COffsetVal - ImmField; 7805 } 7806 7807 assert(isLegalFLATOffset(ImmField, AddrSpace, FlatVariant)); 7808 assert(RemainderOffset + ImmField == COffsetVal); 7809 return {ImmField, RemainderOffset}; 7810 } 7811 7812 // This must be kept in sync with the SIEncodingFamily class in SIInstrInfo.td 7813 enum SIEncodingFamily { 7814 SI = 0, 7815 VI = 1, 7816 SDWA = 2, 7817 SDWA9 = 3, 7818 GFX80 = 4, 7819 GFX9 = 5, 7820 GFX10 = 6, 7821 SDWA10 = 7, 7822 GFX90A = 8, 7823 GFX940 = 9 7824 }; 7825 7826 static SIEncodingFamily subtargetEncodingFamily(const GCNSubtarget &ST) { 7827 switch (ST.getGeneration()) { 7828 default: 7829 break; 7830 case AMDGPUSubtarget::SOUTHERN_ISLANDS: 7831 case AMDGPUSubtarget::SEA_ISLANDS: 7832 return SIEncodingFamily::SI; 7833 case AMDGPUSubtarget::VOLCANIC_ISLANDS: 7834 case AMDGPUSubtarget::GFX9: 7835 return SIEncodingFamily::VI; 7836 case AMDGPUSubtarget::GFX10: 7837 return SIEncodingFamily::GFX10; 7838 } 7839 llvm_unreachable("Unknown subtarget generation!"); 7840 } 7841 7842 bool SIInstrInfo::isAsmOnlyOpcode(int MCOp) const { 7843 switch(MCOp) { 7844 // These opcodes use indirect register addressing so 7845 // they need special handling by codegen (currently missing). 7846 // Therefore it is too risky to allow these opcodes 7847 // to be selected by dpp combiner or sdwa peepholer. 7848 case AMDGPU::V_MOVRELS_B32_dpp_gfx10: 7849 case AMDGPU::V_MOVRELS_B32_sdwa_gfx10: 7850 case AMDGPU::V_MOVRELD_B32_dpp_gfx10: 7851 case AMDGPU::V_MOVRELD_B32_sdwa_gfx10: 7852 case AMDGPU::V_MOVRELSD_B32_dpp_gfx10: 7853 case AMDGPU::V_MOVRELSD_B32_sdwa_gfx10: 7854 case AMDGPU::V_MOVRELSD_2_B32_dpp_gfx10: 7855 case AMDGPU::V_MOVRELSD_2_B32_sdwa_gfx10: 7856 return true; 7857 default: 7858 return false; 7859 } 7860 } 7861 7862 int SIInstrInfo::pseudoToMCOpcode(int Opcode) const { 7863 SIEncodingFamily Gen = subtargetEncodingFamily(ST); 7864 7865 if ((get(Opcode).TSFlags & SIInstrFlags::renamedInGFX9) != 0 && 7866 ST.getGeneration() == AMDGPUSubtarget::GFX9) 7867 Gen = SIEncodingFamily::GFX9; 7868 7869 // Adjust the encoding family to GFX80 for D16 buffer instructions when the 7870 // subtarget has UnpackedD16VMem feature. 7871 // TODO: remove this when we discard GFX80 encoding. 7872 if (ST.hasUnpackedD16VMem() && (get(Opcode).TSFlags & SIInstrFlags::D16Buf)) 7873 Gen = SIEncodingFamily::GFX80; 7874 7875 if (get(Opcode).TSFlags & SIInstrFlags::SDWA) { 7876 switch (ST.getGeneration()) { 7877 default: 7878 Gen = SIEncodingFamily::SDWA; 7879 break; 7880 case AMDGPUSubtarget::GFX9: 7881 Gen = SIEncodingFamily::SDWA9; 7882 break; 7883 case AMDGPUSubtarget::GFX10: 7884 Gen = SIEncodingFamily::SDWA10; 7885 break; 7886 } 7887 } 7888 7889 if (isMAI(Opcode)) { 7890 int MFMAOp = AMDGPU::getMFMAEarlyClobberOp(Opcode); 7891 if (MFMAOp != -1) 7892 Opcode = MFMAOp; 7893 } 7894 7895 int MCOp = AMDGPU::getMCOpcode(Opcode, Gen); 7896 7897 // -1 means that Opcode is already a native instruction. 7898 if (MCOp == -1) 7899 return Opcode; 7900 7901 if (ST.hasGFX90AInsts()) { 7902 uint16_t NMCOp = (uint16_t)-1; 7903 if (ST.hasGFX940Insts()) 7904 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX940); 7905 if (NMCOp == (uint16_t)-1) 7906 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX90A); 7907 if (NMCOp == (uint16_t)-1) 7908 NMCOp = AMDGPU::getMCOpcode(Opcode, SIEncodingFamily::GFX9); 7909 if (NMCOp != (uint16_t)-1) 7910 MCOp = NMCOp; 7911 } 7912 7913 // (uint16_t)-1 means that Opcode is a pseudo instruction that has 7914 // no encoding in the given subtarget generation. 7915 if (MCOp == (uint16_t)-1) 7916 return -1; 7917 7918 if (isAsmOnlyOpcode(MCOp)) 7919 return -1; 7920 7921 return MCOp; 7922 } 7923 7924 static 7925 TargetInstrInfo::RegSubRegPair getRegOrUndef(const MachineOperand &RegOpnd) { 7926 assert(RegOpnd.isReg()); 7927 return RegOpnd.isUndef() ? TargetInstrInfo::RegSubRegPair() : 7928 getRegSubRegPair(RegOpnd); 7929 } 7930 7931 TargetInstrInfo::RegSubRegPair 7932 llvm::getRegSequenceSubReg(MachineInstr &MI, unsigned SubReg) { 7933 assert(MI.isRegSequence()); 7934 for (unsigned I = 0, E = (MI.getNumOperands() - 1)/ 2; I < E; ++I) 7935 if (MI.getOperand(1 + 2 * I + 1).getImm() == SubReg) { 7936 auto &RegOp = MI.getOperand(1 + 2 * I); 7937 return getRegOrUndef(RegOp); 7938 } 7939 return TargetInstrInfo::RegSubRegPair(); 7940 } 7941 7942 // Try to find the definition of reg:subreg in subreg-manipulation pseudos 7943 // Following a subreg of reg:subreg isn't supported 7944 static bool followSubRegDef(MachineInstr &MI, 7945 TargetInstrInfo::RegSubRegPair &RSR) { 7946 if (!RSR.SubReg) 7947 return false; 7948 switch (MI.getOpcode()) { 7949 default: break; 7950 case AMDGPU::REG_SEQUENCE: 7951 RSR = getRegSequenceSubReg(MI, RSR.SubReg); 7952 return true; 7953 // EXTRACT_SUBREG ins't supported as this would follow a subreg of subreg 7954 case AMDGPU::INSERT_SUBREG: 7955 if (RSR.SubReg == (unsigned)MI.getOperand(3).getImm()) 7956 // inserted the subreg we're looking for 7957 RSR = getRegOrUndef(MI.getOperand(2)); 7958 else { // the subreg in the rest of the reg 7959 auto R1 = getRegOrUndef(MI.getOperand(1)); 7960 if (R1.SubReg) // subreg of subreg isn't supported 7961 return false; 7962 RSR.Reg = R1.Reg; 7963 } 7964 return true; 7965 } 7966 return false; 7967 } 7968 7969 MachineInstr *llvm::getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P, 7970 MachineRegisterInfo &MRI) { 7971 assert(MRI.isSSA()); 7972 if (!P.Reg.isVirtual()) 7973 return nullptr; 7974 7975 auto RSR = P; 7976 auto *DefInst = MRI.getVRegDef(RSR.Reg); 7977 while (auto *MI = DefInst) { 7978 DefInst = nullptr; 7979 switch (MI->getOpcode()) { 7980 case AMDGPU::COPY: 7981 case AMDGPU::V_MOV_B32_e32: { 7982 auto &Op1 = MI->getOperand(1); 7983 if (Op1.isReg() && Op1.getReg().isVirtual()) { 7984 if (Op1.isUndef()) 7985 return nullptr; 7986 RSR = getRegSubRegPair(Op1); 7987 DefInst = MRI.getVRegDef(RSR.Reg); 7988 } 7989 break; 7990 } 7991 default: 7992 if (followSubRegDef(*MI, RSR)) { 7993 if (!RSR.Reg) 7994 return nullptr; 7995 DefInst = MRI.getVRegDef(RSR.Reg); 7996 } 7997 } 7998 if (!DefInst) 7999 return MI; 8000 } 8001 return nullptr; 8002 } 8003 8004 bool llvm::execMayBeModifiedBeforeUse(const MachineRegisterInfo &MRI, 8005 Register VReg, 8006 const MachineInstr &DefMI, 8007 const MachineInstr &UseMI) { 8008 assert(MRI.isSSA() && "Must be run on SSA"); 8009 8010 auto *TRI = MRI.getTargetRegisterInfo(); 8011 auto *DefBB = DefMI.getParent(); 8012 8013 // Don't bother searching between blocks, although it is possible this block 8014 // doesn't modify exec. 8015 if (UseMI.getParent() != DefBB) 8016 return true; 8017 8018 const int MaxInstScan = 20; 8019 int NumInst = 0; 8020 8021 // Stop scan at the use. 8022 auto E = UseMI.getIterator(); 8023 for (auto I = std::next(DefMI.getIterator()); I != E; ++I) { 8024 if (I->isDebugInstr()) 8025 continue; 8026 8027 if (++NumInst > MaxInstScan) 8028 return true; 8029 8030 if (I->modifiesRegister(AMDGPU::EXEC, TRI)) 8031 return true; 8032 } 8033 8034 return false; 8035 } 8036 8037 bool llvm::execMayBeModifiedBeforeAnyUse(const MachineRegisterInfo &MRI, 8038 Register VReg, 8039 const MachineInstr &DefMI) { 8040 assert(MRI.isSSA() && "Must be run on SSA"); 8041 8042 auto *TRI = MRI.getTargetRegisterInfo(); 8043 auto *DefBB = DefMI.getParent(); 8044 8045 const int MaxUseScan = 10; 8046 int NumUse = 0; 8047 8048 for (auto &Use : MRI.use_nodbg_operands(VReg)) { 8049 auto &UseInst = *Use.getParent(); 8050 // Don't bother searching between blocks, although it is possible this block 8051 // doesn't modify exec. 8052 if (UseInst.getParent() != DefBB) 8053 return true; 8054 8055 if (++NumUse > MaxUseScan) 8056 return true; 8057 } 8058 8059 if (NumUse == 0) 8060 return false; 8061 8062 const int MaxInstScan = 20; 8063 int NumInst = 0; 8064 8065 // Stop scan when we have seen all the uses. 8066 for (auto I = std::next(DefMI.getIterator()); ; ++I) { 8067 assert(I != DefBB->end()); 8068 8069 if (I->isDebugInstr()) 8070 continue; 8071 8072 if (++NumInst > MaxInstScan) 8073 return true; 8074 8075 for (const MachineOperand &Op : I->operands()) { 8076 // We don't check reg masks here as they're used only on calls: 8077 // 1. EXEC is only considered const within one BB 8078 // 2. Call should be a terminator instruction if present in a BB 8079 8080 if (!Op.isReg()) 8081 continue; 8082 8083 Register Reg = Op.getReg(); 8084 if (Op.isUse()) { 8085 if (Reg == VReg && --NumUse == 0) 8086 return false; 8087 } else if (TRI->regsOverlap(Reg, AMDGPU::EXEC)) 8088 return true; 8089 } 8090 } 8091 } 8092 8093 MachineInstr *SIInstrInfo::createPHIDestinationCopy( 8094 MachineBasicBlock &MBB, MachineBasicBlock::iterator LastPHIIt, 8095 const DebugLoc &DL, Register Src, Register Dst) const { 8096 auto Cur = MBB.begin(); 8097 if (Cur != MBB.end()) 8098 do { 8099 if (!Cur->isPHI() && Cur->readsRegister(Dst)) 8100 return BuildMI(MBB, Cur, DL, get(TargetOpcode::COPY), Dst).addReg(Src); 8101 ++Cur; 8102 } while (Cur != MBB.end() && Cur != LastPHIIt); 8103 8104 return TargetInstrInfo::createPHIDestinationCopy(MBB, LastPHIIt, DL, Src, 8105 Dst); 8106 } 8107 8108 MachineInstr *SIInstrInfo::createPHISourceCopy( 8109 MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt, 8110 const DebugLoc &DL, Register Src, unsigned SrcSubReg, Register Dst) const { 8111 if (InsPt != MBB.end() && 8112 (InsPt->getOpcode() == AMDGPU::SI_IF || 8113 InsPt->getOpcode() == AMDGPU::SI_ELSE || 8114 InsPt->getOpcode() == AMDGPU::SI_IF_BREAK) && 8115 InsPt->definesRegister(Src)) { 8116 InsPt++; 8117 return BuildMI(MBB, InsPt, DL, 8118 get(ST.isWave32() ? AMDGPU::S_MOV_B32_term 8119 : AMDGPU::S_MOV_B64_term), 8120 Dst) 8121 .addReg(Src, 0, SrcSubReg) 8122 .addReg(AMDGPU::EXEC, RegState::Implicit); 8123 } 8124 return TargetInstrInfo::createPHISourceCopy(MBB, InsPt, DL, Src, SrcSubReg, 8125 Dst); 8126 } 8127 8128 bool llvm::SIInstrInfo::isWave32() const { return ST.isWave32(); } 8129 8130 MachineInstr *SIInstrInfo::foldMemoryOperandImpl( 8131 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, 8132 MachineBasicBlock::iterator InsertPt, int FrameIndex, LiveIntervals *LIS, 8133 VirtRegMap *VRM) const { 8134 // This is a bit of a hack (copied from AArch64). Consider this instruction: 8135 // 8136 // %0:sreg_32 = COPY $m0 8137 // 8138 // We explicitly chose SReg_32 for the virtual register so such a copy might 8139 // be eliminated by RegisterCoalescer. However, that may not be possible, and 8140 // %0 may even spill. We can't spill $m0 normally (it would require copying to 8141 // a numbered SGPR anyway), and since it is in the SReg_32 register class, 8142 // TargetInstrInfo::foldMemoryOperand() is going to try. 8143 // A similar issue also exists with spilling and reloading $exec registers. 8144 // 8145 // To prevent that, constrain the %0 register class here. 8146 if (MI.isFullCopy()) { 8147 Register DstReg = MI.getOperand(0).getReg(); 8148 Register SrcReg = MI.getOperand(1).getReg(); 8149 if ((DstReg.isVirtual() || SrcReg.isVirtual()) && 8150 (DstReg.isVirtual() != SrcReg.isVirtual())) { 8151 MachineRegisterInfo &MRI = MF.getRegInfo(); 8152 Register VirtReg = DstReg.isVirtual() ? DstReg : SrcReg; 8153 const TargetRegisterClass *RC = MRI.getRegClass(VirtReg); 8154 if (RC->hasSuperClassEq(&AMDGPU::SReg_32RegClass)) { 8155 MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_32_XM0_XEXECRegClass); 8156 return nullptr; 8157 } else if (RC->hasSuperClassEq(&AMDGPU::SReg_64RegClass)) { 8158 MRI.constrainRegClass(VirtReg, &AMDGPU::SReg_64_XEXECRegClass); 8159 return nullptr; 8160 } 8161 } 8162 } 8163 8164 return nullptr; 8165 } 8166 8167 unsigned SIInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 8168 const MachineInstr &MI, 8169 unsigned *PredCost) const { 8170 if (MI.isBundle()) { 8171 MachineBasicBlock::const_instr_iterator I(MI.getIterator()); 8172 MachineBasicBlock::const_instr_iterator E(MI.getParent()->instr_end()); 8173 unsigned Lat = 0, Count = 0; 8174 for (++I; I != E && I->isBundledWithPred(); ++I) { 8175 ++Count; 8176 Lat = std::max(Lat, SchedModel.computeInstrLatency(&*I)); 8177 } 8178 return Lat + Count - 1; 8179 } 8180 8181 return SchedModel.computeInstrLatency(&MI); 8182 } 8183 8184 unsigned SIInstrInfo::getDSShaderTypeValue(const MachineFunction &MF) { 8185 switch (MF.getFunction().getCallingConv()) { 8186 case CallingConv::AMDGPU_PS: 8187 return 1; 8188 case CallingConv::AMDGPU_VS: 8189 return 2; 8190 case CallingConv::AMDGPU_GS: 8191 return 3; 8192 case CallingConv::AMDGPU_HS: 8193 case CallingConv::AMDGPU_LS: 8194 case CallingConv::AMDGPU_ES: 8195 report_fatal_error("ds_ordered_count unsupported for this calling conv"); 8196 case CallingConv::AMDGPU_CS: 8197 case CallingConv::AMDGPU_KERNEL: 8198 case CallingConv::C: 8199 case CallingConv::Fast: 8200 default: 8201 // Assume other calling conventions are various compute callable functions 8202 return 0; 8203 } 8204 } 8205 8206 bool SIInstrInfo::analyzeCompare(const MachineInstr &MI, Register &SrcReg, 8207 Register &SrcReg2, int64_t &CmpMask, 8208 int64_t &CmpValue) const { 8209 if (!MI.getOperand(0).isReg() || MI.getOperand(0).getSubReg()) 8210 return false; 8211 8212 switch (MI.getOpcode()) { 8213 default: 8214 break; 8215 case AMDGPU::S_CMP_EQ_U32: 8216 case AMDGPU::S_CMP_EQ_I32: 8217 case AMDGPU::S_CMP_LG_U32: 8218 case AMDGPU::S_CMP_LG_I32: 8219 case AMDGPU::S_CMP_LT_U32: 8220 case AMDGPU::S_CMP_LT_I32: 8221 case AMDGPU::S_CMP_GT_U32: 8222 case AMDGPU::S_CMP_GT_I32: 8223 case AMDGPU::S_CMP_LE_U32: 8224 case AMDGPU::S_CMP_LE_I32: 8225 case AMDGPU::S_CMP_GE_U32: 8226 case AMDGPU::S_CMP_GE_I32: 8227 case AMDGPU::S_CMP_EQ_U64: 8228 case AMDGPU::S_CMP_LG_U64: 8229 SrcReg = MI.getOperand(0).getReg(); 8230 if (MI.getOperand(1).isReg()) { 8231 if (MI.getOperand(1).getSubReg()) 8232 return false; 8233 SrcReg2 = MI.getOperand(1).getReg(); 8234 CmpValue = 0; 8235 } else if (MI.getOperand(1).isImm()) { 8236 SrcReg2 = Register(); 8237 CmpValue = MI.getOperand(1).getImm(); 8238 } else { 8239 return false; 8240 } 8241 CmpMask = ~0; 8242 return true; 8243 case AMDGPU::S_CMPK_EQ_U32: 8244 case AMDGPU::S_CMPK_EQ_I32: 8245 case AMDGPU::S_CMPK_LG_U32: 8246 case AMDGPU::S_CMPK_LG_I32: 8247 case AMDGPU::S_CMPK_LT_U32: 8248 case AMDGPU::S_CMPK_LT_I32: 8249 case AMDGPU::S_CMPK_GT_U32: 8250 case AMDGPU::S_CMPK_GT_I32: 8251 case AMDGPU::S_CMPK_LE_U32: 8252 case AMDGPU::S_CMPK_LE_I32: 8253 case AMDGPU::S_CMPK_GE_U32: 8254 case AMDGPU::S_CMPK_GE_I32: 8255 SrcReg = MI.getOperand(0).getReg(); 8256 SrcReg2 = Register(); 8257 CmpValue = MI.getOperand(1).getImm(); 8258 CmpMask = ~0; 8259 return true; 8260 } 8261 8262 return false; 8263 } 8264 8265 bool SIInstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg, 8266 Register SrcReg2, int64_t CmpMask, 8267 int64_t CmpValue, 8268 const MachineRegisterInfo *MRI) const { 8269 if (!SrcReg || SrcReg.isPhysical()) 8270 return false; 8271 8272 if (SrcReg2 && !getFoldableImm(SrcReg2, *MRI, CmpValue)) 8273 return false; 8274 8275 const auto optimizeCmpAnd = [&CmpInstr, SrcReg, CmpValue, MRI, 8276 this](int64_t ExpectedValue, unsigned SrcSize, 8277 bool IsReversible, bool IsSigned) -> bool { 8278 // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 8279 // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 8280 // s_cmp_ge_u32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 8281 // s_cmp_ge_i32 (s_and_b32 $src, 1 << n), 1 << n => s_and_b32 $src, 1 << n 8282 // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 1 << n => s_and_b64 $src, 1 << n 8283 // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 8284 // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 8285 // s_cmp_gt_u32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 8286 // s_cmp_gt_i32 (s_and_b32 $src, 1 << n), 0 => s_and_b32 $src, 1 << n 8287 // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 0 => s_and_b64 $src, 1 << n 8288 // 8289 // Signed ge/gt are not used for the sign bit. 8290 // 8291 // If result of the AND is unused except in the compare: 8292 // s_and_b(32|64) $src, 1 << n => s_bitcmp1_b(32|64) $src, n 8293 // 8294 // s_cmp_eq_u32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n 8295 // s_cmp_eq_i32 (s_and_b32 $src, 1 << n), 0 => s_bitcmp0_b32 $src, n 8296 // s_cmp_eq_u64 (s_and_b64 $src, 1 << n), 0 => s_bitcmp0_b64 $src, n 8297 // s_cmp_lg_u32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n 8298 // s_cmp_lg_i32 (s_and_b32 $src, 1 << n), 1 << n => s_bitcmp0_b32 $src, n 8299 // s_cmp_lg_u64 (s_and_b64 $src, 1 << n), 1 << n => s_bitcmp0_b64 $src, n 8300 8301 MachineInstr *Def = MRI->getUniqueVRegDef(SrcReg); 8302 if (!Def || Def->getParent() != CmpInstr.getParent()) 8303 return false; 8304 8305 if (Def->getOpcode() != AMDGPU::S_AND_B32 && 8306 Def->getOpcode() != AMDGPU::S_AND_B64) 8307 return false; 8308 8309 int64_t Mask; 8310 const auto isMask = [&Mask, SrcSize](const MachineOperand *MO) -> bool { 8311 if (MO->isImm()) 8312 Mask = MO->getImm(); 8313 else if (!getFoldableImm(MO, Mask)) 8314 return false; 8315 Mask &= maxUIntN(SrcSize); 8316 return isPowerOf2_64(Mask); 8317 }; 8318 8319 MachineOperand *SrcOp = &Def->getOperand(1); 8320 if (isMask(SrcOp)) 8321 SrcOp = &Def->getOperand(2); 8322 else if (isMask(&Def->getOperand(2))) 8323 SrcOp = &Def->getOperand(1); 8324 else 8325 return false; 8326 8327 unsigned BitNo = countTrailingZeros((uint64_t)Mask); 8328 if (IsSigned && BitNo == SrcSize - 1) 8329 return false; 8330 8331 ExpectedValue <<= BitNo; 8332 8333 bool IsReversedCC = false; 8334 if (CmpValue != ExpectedValue) { 8335 if (!IsReversible) 8336 return false; 8337 IsReversedCC = CmpValue == (ExpectedValue ^ Mask); 8338 if (!IsReversedCC) 8339 return false; 8340 } 8341 8342 Register DefReg = Def->getOperand(0).getReg(); 8343 if (IsReversedCC && !MRI->hasOneNonDBGUse(DefReg)) 8344 return false; 8345 8346 for (auto I = std::next(Def->getIterator()), E = CmpInstr.getIterator(); 8347 I != E; ++I) { 8348 if (I->modifiesRegister(AMDGPU::SCC, &RI) || 8349 I->killsRegister(AMDGPU::SCC, &RI)) 8350 return false; 8351 } 8352 8353 MachineOperand *SccDef = Def->findRegisterDefOperand(AMDGPU::SCC); 8354 SccDef->setIsDead(false); 8355 CmpInstr.eraseFromParent(); 8356 8357 if (!MRI->use_nodbg_empty(DefReg)) { 8358 assert(!IsReversedCC); 8359 return true; 8360 } 8361 8362 // Replace AND with unused result with a S_BITCMP. 8363 MachineBasicBlock *MBB = Def->getParent(); 8364 8365 unsigned NewOpc = (SrcSize == 32) ? IsReversedCC ? AMDGPU::S_BITCMP0_B32 8366 : AMDGPU::S_BITCMP1_B32 8367 : IsReversedCC ? AMDGPU::S_BITCMP0_B64 8368 : AMDGPU::S_BITCMP1_B64; 8369 8370 BuildMI(*MBB, Def, Def->getDebugLoc(), get(NewOpc)) 8371 .add(*SrcOp) 8372 .addImm(BitNo); 8373 Def->eraseFromParent(); 8374 8375 return true; 8376 }; 8377 8378 switch (CmpInstr.getOpcode()) { 8379 default: 8380 break; 8381 case AMDGPU::S_CMP_EQ_U32: 8382 case AMDGPU::S_CMP_EQ_I32: 8383 case AMDGPU::S_CMPK_EQ_U32: 8384 case AMDGPU::S_CMPK_EQ_I32: 8385 return optimizeCmpAnd(1, 32, true, false); 8386 case AMDGPU::S_CMP_GE_U32: 8387 case AMDGPU::S_CMPK_GE_U32: 8388 return optimizeCmpAnd(1, 32, false, false); 8389 case AMDGPU::S_CMP_GE_I32: 8390 case AMDGPU::S_CMPK_GE_I32: 8391 return optimizeCmpAnd(1, 32, false, true); 8392 case AMDGPU::S_CMP_EQ_U64: 8393 return optimizeCmpAnd(1, 64, true, false); 8394 case AMDGPU::S_CMP_LG_U32: 8395 case AMDGPU::S_CMP_LG_I32: 8396 case AMDGPU::S_CMPK_LG_U32: 8397 case AMDGPU::S_CMPK_LG_I32: 8398 return optimizeCmpAnd(0, 32, true, false); 8399 case AMDGPU::S_CMP_GT_U32: 8400 case AMDGPU::S_CMPK_GT_U32: 8401 return optimizeCmpAnd(0, 32, false, false); 8402 case AMDGPU::S_CMP_GT_I32: 8403 case AMDGPU::S_CMPK_GT_I32: 8404 return optimizeCmpAnd(0, 32, false, true); 8405 case AMDGPU::S_CMP_LG_U64: 8406 return optimizeCmpAnd(0, 64, true, false); 8407 } 8408 8409 return false; 8410 } 8411