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