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