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