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