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 GET_INSTRINFO_CTOR_DTOR 67 #include "AMDGPUGenInstrInfo.inc" 68 69 namespace llvm { 70 namespace AMDGPU { 71 #define GET_D16ImageDimIntrinsics_IMPL 72 #define GET_ImageDimIntrinsicTable_IMPL 73 #define GET_RsrcIntrinsics_IMPL 74 #include "AMDGPUGenSearchableTables.inc" 75 } 76 } 77 78 79 // Must be at least 4 to be able to branch over minimum unconditional branch 80 // code. This is only for making it possible to write reasonably small tests for 81 // long branches. 82 static cl::opt<unsigned> 83 BranchOffsetBits("amdgpu-s-branch-bits", cl::ReallyHidden, cl::init(16), 84 cl::desc("Restrict range of branch instructions (DEBUG)")); 85 86 SIInstrInfo::SIInstrInfo(const GCNSubtarget &ST) 87 : AMDGPUGenInstrInfo(AMDGPU::ADJCALLSTACKUP, AMDGPU::ADJCALLSTACKDOWN), 88 RI(ST), ST(ST) {} 89 90 //===----------------------------------------------------------------------===// 91 // TargetInstrInfo callbacks 92 //===----------------------------------------------------------------------===// 93 94 static unsigned getNumOperandsNoGlue(SDNode *Node) { 95 unsigned N = Node->getNumOperands(); 96 while (N && Node->getOperand(N - 1).getValueType() == MVT::Glue) 97 --N; 98 return N; 99 } 100 101 static SDValue findChainOperand(SDNode *Load) { 102 SDValue LastOp = Load->getOperand(getNumOperandsNoGlue(Load) - 1); 103 assert(LastOp.getValueType() == MVT::Other && "Chain missing from load node"); 104 return LastOp; 105 } 106 107 /// Returns true if both nodes have the same value for the given 108 /// operand \p Op, or if both nodes do not have this operand. 109 static bool nodesHaveSameOperandValue(SDNode *N0, SDNode* N1, unsigned OpName) { 110 unsigned Opc0 = N0->getMachineOpcode(); 111 unsigned Opc1 = N1->getMachineOpcode(); 112 113 int Op0Idx = AMDGPU::getNamedOperandIdx(Opc0, OpName); 114 int Op1Idx = AMDGPU::getNamedOperandIdx(Opc1, OpName); 115 116 if (Op0Idx == -1 && Op1Idx == -1) 117 return true; 118 119 120 if ((Op0Idx == -1 && Op1Idx != -1) || 121 (Op1Idx == -1 && Op0Idx != -1)) 122 return false; 123 124 // getNamedOperandIdx returns the index for the MachineInstr's operands, 125 // which includes the result as the first operand. We are indexing into the 126 // MachineSDNode's operands, so we need to skip the result operand to get 127 // the real index. 128 --Op0Idx; 129 --Op1Idx; 130 131 return N0->getOperand(Op0Idx) == N1->getOperand(Op1Idx); 132 } 133 134 bool SIInstrInfo::isReallyTriviallyReMaterializable(const MachineInstr &MI, 135 AliasAnalysis *AA) const { 136 // TODO: The generic check fails for VALU instructions that should be 137 // rematerializable due to implicit reads of exec. We really want all of the 138 // generic logic for this except for this. 139 switch (MI.getOpcode()) { 140 case AMDGPU::V_MOV_B32_e32: 141 case AMDGPU::V_MOV_B32_e64: 142 case AMDGPU::V_MOV_B64_PSEUDO: 143 return true; 144 default: 145 return false; 146 } 147 } 148 149 bool SIInstrInfo::areLoadsFromSameBasePtr(SDNode *Load0, SDNode *Load1, 150 int64_t &Offset0, 151 int64_t &Offset1) const { 152 if (!Load0->isMachineOpcode() || !Load1->isMachineOpcode()) 153 return false; 154 155 unsigned Opc0 = Load0->getMachineOpcode(); 156 unsigned Opc1 = Load1->getMachineOpcode(); 157 158 // Make sure both are actually loads. 159 if (!get(Opc0).mayLoad() || !get(Opc1).mayLoad()) 160 return false; 161 162 if (isDS(Opc0) && isDS(Opc1)) { 163 164 // FIXME: Handle this case: 165 if (getNumOperandsNoGlue(Load0) != getNumOperandsNoGlue(Load1)) 166 return false; 167 168 // Check base reg. 169 if (Load0->getOperand(1) != Load1->getOperand(1)) 170 return false; 171 172 // Check chain. 173 if (findChainOperand(Load0) != findChainOperand(Load1)) 174 return false; 175 176 // Skip read2 / write2 variants for simplicity. 177 // TODO: We should report true if the used offsets are adjacent (excluded 178 // st64 versions). 179 if (AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::data1) != -1 || 180 AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::data1) != -1) 181 return false; 182 183 Offset0 = cast<ConstantSDNode>(Load0->getOperand(2))->getZExtValue(); 184 Offset1 = cast<ConstantSDNode>(Load1->getOperand(2))->getZExtValue(); 185 return true; 186 } 187 188 if (isSMRD(Opc0) && isSMRD(Opc1)) { 189 // Skip time and cache invalidation instructions. 190 if (AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::sbase) == -1 || 191 AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::sbase) == -1) 192 return false; 193 194 assert(getNumOperandsNoGlue(Load0) == getNumOperandsNoGlue(Load1)); 195 196 // Check base reg. 197 if (Load0->getOperand(0) != Load1->getOperand(0)) 198 return false; 199 200 const ConstantSDNode *Load0Offset = 201 dyn_cast<ConstantSDNode>(Load0->getOperand(1)); 202 const ConstantSDNode *Load1Offset = 203 dyn_cast<ConstantSDNode>(Load1->getOperand(1)); 204 205 if (!Load0Offset || !Load1Offset) 206 return false; 207 208 // Check chain. 209 if (findChainOperand(Load0) != findChainOperand(Load1)) 210 return false; 211 212 Offset0 = Load0Offset->getZExtValue(); 213 Offset1 = Load1Offset->getZExtValue(); 214 return true; 215 } 216 217 // MUBUF and MTBUF can access the same addresses. 218 if ((isMUBUF(Opc0) || isMTBUF(Opc0)) && (isMUBUF(Opc1) || isMTBUF(Opc1))) { 219 220 // MUBUF and MTBUF have vaddr at different indices. 221 if (!nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::soffset) || 222 findChainOperand(Load0) != findChainOperand(Load1) || 223 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::vaddr) || 224 !nodesHaveSameOperandValue(Load0, Load1, AMDGPU::OpName::srsrc)) 225 return false; 226 227 int OffIdx0 = AMDGPU::getNamedOperandIdx(Opc0, AMDGPU::OpName::offset); 228 int OffIdx1 = AMDGPU::getNamedOperandIdx(Opc1, AMDGPU::OpName::offset); 229 230 if (OffIdx0 == -1 || OffIdx1 == -1) 231 return false; 232 233 // getNamedOperandIdx returns the index for MachineInstrs. Since they 234 // inlcude the output in the operand list, but SDNodes don't, we need to 235 // subtract the index by one. 236 --OffIdx0; 237 --OffIdx1; 238 239 SDValue Off0 = Load0->getOperand(OffIdx0); 240 SDValue Off1 = Load1->getOperand(OffIdx1); 241 242 // The offset might be a FrameIndexSDNode. 243 if (!isa<ConstantSDNode>(Off0) || !isa<ConstantSDNode>(Off1)) 244 return false; 245 246 Offset0 = cast<ConstantSDNode>(Off0)->getZExtValue(); 247 Offset1 = cast<ConstantSDNode>(Off1)->getZExtValue(); 248 return true; 249 } 250 251 return false; 252 } 253 254 static bool isStride64(unsigned Opc) { 255 switch (Opc) { 256 case AMDGPU::DS_READ2ST64_B32: 257 case AMDGPU::DS_READ2ST64_B64: 258 case AMDGPU::DS_WRITE2ST64_B32: 259 case AMDGPU::DS_WRITE2ST64_B64: 260 return true; 261 default: 262 return false; 263 } 264 } 265 266 bool SIInstrInfo::getMemOperandWithOffset(MachineInstr &LdSt, 267 MachineOperand *&BaseOp, 268 int64_t &Offset, 269 const TargetRegisterInfo *TRI) const { 270 unsigned Opc = LdSt.getOpcode(); 271 272 if (isDS(LdSt)) { 273 const MachineOperand *OffsetImm = 274 getNamedOperand(LdSt, AMDGPU::OpName::offset); 275 if (OffsetImm) { 276 // Normal, single offset LDS instruction. 277 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::addr); 278 Offset = OffsetImm->getImm(); 279 assert(BaseOp->isReg() && "getMemOperandWithOffset only supports base " 280 "operands of type register."); 281 return true; 282 } 283 284 // The 2 offset instructions use offset0 and offset1 instead. We can treat 285 // these as a load with a single offset if the 2 offsets are consecutive. We 286 // will use this for some partially aligned loads. 287 const MachineOperand *Offset0Imm = 288 getNamedOperand(LdSt, AMDGPU::OpName::offset0); 289 const MachineOperand *Offset1Imm = 290 getNamedOperand(LdSt, AMDGPU::OpName::offset1); 291 292 uint8_t Offset0 = Offset0Imm->getImm(); 293 uint8_t Offset1 = Offset1Imm->getImm(); 294 295 if (Offset1 > Offset0 && Offset1 - Offset0 == 1) { 296 // Each of these offsets is in element sized units, so we need to convert 297 // to bytes of the individual reads. 298 299 unsigned EltSize; 300 if (LdSt.mayLoad()) 301 EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, 0)) / 16; 302 else { 303 assert(LdSt.mayStore()); 304 int Data0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::data0); 305 EltSize = TRI->getRegSizeInBits(*getOpRegClass(LdSt, Data0Idx)) / 8; 306 } 307 308 if (isStride64(Opc)) 309 EltSize *= 64; 310 311 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::addr); 312 Offset = EltSize * Offset0; 313 assert(BaseOp->isReg() && "getMemOperandWithOffset only supports base " 314 "operands of type register."); 315 return true; 316 } 317 318 return false; 319 } 320 321 if (isMUBUF(LdSt) || isMTBUF(LdSt)) { 322 const MachineOperand *SOffset = getNamedOperand(LdSt, AMDGPU::OpName::soffset); 323 if (SOffset && SOffset->isReg()) 324 return false; 325 326 MachineOperand *AddrReg = getNamedOperand(LdSt, AMDGPU::OpName::vaddr); 327 if (!AddrReg) 328 return false; 329 330 const MachineOperand *OffsetImm = 331 getNamedOperand(LdSt, AMDGPU::OpName::offset); 332 BaseOp = AddrReg; 333 Offset = OffsetImm->getImm(); 334 335 if (SOffset) // soffset can be an inline immediate. 336 Offset += SOffset->getImm(); 337 338 assert(BaseOp->isReg() && "getMemOperandWithOffset only supports base " 339 "operands of type register."); 340 return true; 341 } 342 343 if (isSMRD(LdSt)) { 344 const MachineOperand *OffsetImm = 345 getNamedOperand(LdSt, AMDGPU::OpName::offset); 346 if (!OffsetImm) 347 return false; 348 349 MachineOperand *SBaseReg = getNamedOperand(LdSt, AMDGPU::OpName::sbase); 350 BaseOp = SBaseReg; 351 Offset = OffsetImm->getImm(); 352 assert(BaseOp->isReg() && "getMemOperandWithOffset only supports base " 353 "operands of type register."); 354 return true; 355 } 356 357 if (isFLAT(LdSt)) { 358 MachineOperand *VAddr = getNamedOperand(LdSt, AMDGPU::OpName::vaddr); 359 if (VAddr) { 360 // Can't analyze 2 offsets. 361 if (getNamedOperand(LdSt, AMDGPU::OpName::saddr)) 362 return false; 363 364 BaseOp = VAddr; 365 } else { 366 // scratch instructions have either vaddr or saddr. 367 BaseOp = getNamedOperand(LdSt, AMDGPU::OpName::saddr); 368 } 369 370 Offset = getNamedOperand(LdSt, AMDGPU::OpName::offset)->getImm(); 371 assert(BaseOp->isReg() && "getMemOperandWithOffset only supports base " 372 "operands of type register."); 373 return true; 374 } 375 376 return false; 377 } 378 379 static bool memOpsHaveSameBasePtr(const MachineInstr &MI1, 380 const MachineOperand &BaseOp1, 381 const MachineInstr &MI2, 382 const MachineOperand &BaseOp2) { 383 // Support only base operands with base registers. 384 // Note: this could be extended to support FI operands. 385 if (!BaseOp1.isReg() || !BaseOp2.isReg()) 386 return false; 387 388 if (BaseOp1.isIdenticalTo(BaseOp2)) 389 return true; 390 391 if (!MI1.hasOneMemOperand() || !MI2.hasOneMemOperand()) 392 return false; 393 394 auto MO1 = *MI1.memoperands_begin(); 395 auto MO2 = *MI2.memoperands_begin(); 396 if (MO1->getAddrSpace() != MO2->getAddrSpace()) 397 return false; 398 399 auto Base1 = MO1->getValue(); 400 auto Base2 = MO2->getValue(); 401 if (!Base1 || !Base2) 402 return false; 403 const MachineFunction &MF = *MI1.getParent()->getParent(); 404 const DataLayout &DL = MF.getFunction().getParent()->getDataLayout(); 405 Base1 = GetUnderlyingObject(Base1, DL); 406 Base2 = GetUnderlyingObject(Base1, DL); 407 408 if (isa<UndefValue>(Base1) || isa<UndefValue>(Base2)) 409 return false; 410 411 return Base1 == Base2; 412 } 413 414 bool SIInstrInfo::shouldClusterMemOps(MachineOperand &BaseOp1, 415 MachineOperand &BaseOp2, 416 unsigned NumLoads) const { 417 MachineInstr &FirstLdSt = *BaseOp1.getParent(); 418 MachineInstr &SecondLdSt = *BaseOp2.getParent(); 419 420 if (!memOpsHaveSameBasePtr(FirstLdSt, BaseOp1, SecondLdSt, BaseOp2)) 421 return false; 422 423 const MachineOperand *FirstDst = nullptr; 424 const MachineOperand *SecondDst = nullptr; 425 426 if ((isMUBUF(FirstLdSt) && isMUBUF(SecondLdSt)) || 427 (isMTBUF(FirstLdSt) && isMTBUF(SecondLdSt)) || 428 (isFLAT(FirstLdSt) && isFLAT(SecondLdSt))) { 429 const unsigned MaxGlobalLoadCluster = 6; 430 if (NumLoads > MaxGlobalLoadCluster) 431 return false; 432 433 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::vdata); 434 if (!FirstDst) 435 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::vdst); 436 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::vdata); 437 if (!SecondDst) 438 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::vdst); 439 } else if (isSMRD(FirstLdSt) && isSMRD(SecondLdSt)) { 440 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::sdst); 441 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::sdst); 442 } else if (isDS(FirstLdSt) && isDS(SecondLdSt)) { 443 FirstDst = getNamedOperand(FirstLdSt, AMDGPU::OpName::vdst); 444 SecondDst = getNamedOperand(SecondLdSt, AMDGPU::OpName::vdst); 445 } 446 447 if (!FirstDst || !SecondDst) 448 return false; 449 450 // Try to limit clustering based on the total number of bytes loaded 451 // rather than the number of instructions. This is done to help reduce 452 // register pressure. The method used is somewhat inexact, though, 453 // because it assumes that all loads in the cluster will load the 454 // same number of bytes as FirstLdSt. 455 456 // The unit of this value is bytes. 457 // FIXME: This needs finer tuning. 458 unsigned LoadClusterThreshold = 16; 459 460 const MachineRegisterInfo &MRI = 461 FirstLdSt.getParent()->getParent()->getRegInfo(); 462 const TargetRegisterClass *DstRC = MRI.getRegClass(FirstDst->getReg()); 463 464 return (NumLoads * (RI.getRegSizeInBits(*DstRC) / 8)) <= LoadClusterThreshold; 465 } 466 467 // FIXME: This behaves strangely. If, for example, you have 32 load + stores, 468 // the first 16 loads will be interleaved with the stores, and the next 16 will 469 // be clustered as expected. It should really split into 2 16 store batches. 470 // 471 // Loads are clustered until this returns false, rather than trying to schedule 472 // groups of stores. This also means we have to deal with saying different 473 // address space loads should be clustered, and ones which might cause bank 474 // conflicts. 475 // 476 // This might be deprecated so it might not be worth that much effort to fix. 477 bool SIInstrInfo::shouldScheduleLoadsNear(SDNode *Load0, SDNode *Load1, 478 int64_t Offset0, int64_t Offset1, 479 unsigned NumLoads) const { 480 assert(Offset1 > Offset0 && 481 "Second offset should be larger than first offset!"); 482 // If we have less than 16 loads in a row, and the offsets are within 64 483 // bytes, then schedule together. 484 485 // A cacheline is 64 bytes (for global memory). 486 return (NumLoads <= 16 && (Offset1 - Offset0) < 64); 487 } 488 489 static void reportIllegalCopy(const SIInstrInfo *TII, MachineBasicBlock &MBB, 490 MachineBasicBlock::iterator MI, 491 const DebugLoc &DL, unsigned DestReg, 492 unsigned SrcReg, bool KillSrc) { 493 MachineFunction *MF = MBB.getParent(); 494 DiagnosticInfoUnsupported IllegalCopy(MF->getFunction(), 495 "illegal SGPR to VGPR copy", 496 DL, DS_Error); 497 LLVMContext &C = MF->getFunction().getContext(); 498 C.diagnose(IllegalCopy); 499 500 BuildMI(MBB, MI, DL, TII->get(AMDGPU::SI_ILLEGAL_COPY), DestReg) 501 .addReg(SrcReg, getKillRegState(KillSrc)); 502 } 503 504 void SIInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 505 MachineBasicBlock::iterator MI, 506 const DebugLoc &DL, unsigned DestReg, 507 unsigned SrcReg, bool KillSrc) const { 508 const TargetRegisterClass *RC = RI.getPhysRegClass(DestReg); 509 510 if (RC == &AMDGPU::VGPR_32RegClass) { 511 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg) || 512 AMDGPU::SReg_32RegClass.contains(SrcReg)); 513 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg) 514 .addReg(SrcReg, getKillRegState(KillSrc)); 515 return; 516 } 517 518 if (RC == &AMDGPU::SReg_32_XM0RegClass || 519 RC == &AMDGPU::SReg_32RegClass) { 520 if (SrcReg == AMDGPU::SCC) { 521 BuildMI(MBB, MI, DL, get(AMDGPU::S_CSELECT_B32), DestReg) 522 .addImm(-1) 523 .addImm(0); 524 return; 525 } 526 527 if (!AMDGPU::SReg_32RegClass.contains(SrcReg)) { 528 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 529 return; 530 } 531 532 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg) 533 .addReg(SrcReg, getKillRegState(KillSrc)); 534 return; 535 } 536 537 if (RC == &AMDGPU::SReg_64RegClass) { 538 if (DestReg == AMDGPU::VCC) { 539 if (AMDGPU::SReg_64RegClass.contains(SrcReg)) { 540 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), AMDGPU::VCC) 541 .addReg(SrcReg, getKillRegState(KillSrc)); 542 } else { 543 // FIXME: Hack until VReg_1 removed. 544 assert(AMDGPU::VGPR_32RegClass.contains(SrcReg)); 545 BuildMI(MBB, MI, DL, get(AMDGPU::V_CMP_NE_U32_e32)) 546 .addImm(0) 547 .addReg(SrcReg, getKillRegState(KillSrc)); 548 } 549 550 return; 551 } 552 553 if (!AMDGPU::SReg_64RegClass.contains(SrcReg)) { 554 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 555 return; 556 } 557 558 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg) 559 .addReg(SrcReg, getKillRegState(KillSrc)); 560 return; 561 } 562 563 if (DestReg == AMDGPU::SCC) { 564 assert(AMDGPU::SReg_32RegClass.contains(SrcReg)); 565 BuildMI(MBB, MI, DL, get(AMDGPU::S_CMP_LG_U32)) 566 .addReg(SrcReg, getKillRegState(KillSrc)) 567 .addImm(0); 568 return; 569 } 570 571 unsigned EltSize = 4; 572 unsigned Opcode = AMDGPU::V_MOV_B32_e32; 573 if (RI.isSGPRClass(RC)) { 574 if (RI.getRegSizeInBits(*RC) > 32) { 575 Opcode = AMDGPU::S_MOV_B64; 576 EltSize = 8; 577 } else { 578 Opcode = AMDGPU::S_MOV_B32; 579 EltSize = 4; 580 } 581 582 if (!RI.isSGPRClass(RI.getPhysRegClass(SrcReg))) { 583 reportIllegalCopy(this, MBB, MI, DL, DestReg, SrcReg, KillSrc); 584 return; 585 } 586 } 587 588 ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RC, EltSize); 589 bool Forward = RI.getHWRegIndex(DestReg) <= RI.getHWRegIndex(SrcReg); 590 591 for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) { 592 unsigned SubIdx; 593 if (Forward) 594 SubIdx = SubIndices[Idx]; 595 else 596 SubIdx = SubIndices[SubIndices.size() - Idx - 1]; 597 598 MachineInstrBuilder Builder = BuildMI(MBB, MI, DL, 599 get(Opcode), RI.getSubReg(DestReg, SubIdx)); 600 601 Builder.addReg(RI.getSubReg(SrcReg, SubIdx)); 602 603 if (Idx == 0) 604 Builder.addReg(DestReg, RegState::Define | RegState::Implicit); 605 606 bool UseKill = KillSrc && Idx == SubIndices.size() - 1; 607 Builder.addReg(SrcReg, getKillRegState(UseKill) | RegState::Implicit); 608 } 609 } 610 611 int SIInstrInfo::commuteOpcode(unsigned Opcode) const { 612 int NewOpc; 613 614 // Try to map original to commuted opcode 615 NewOpc = AMDGPU::getCommuteRev(Opcode); 616 if (NewOpc != -1) 617 // Check if the commuted (REV) opcode exists on the target. 618 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1; 619 620 // Try to map commuted to original opcode 621 NewOpc = AMDGPU::getCommuteOrig(Opcode); 622 if (NewOpc != -1) 623 // Check if the original (non-REV) opcode exists on the target. 624 return pseudoToMCOpcode(NewOpc) != -1 ? NewOpc : -1; 625 626 return Opcode; 627 } 628 629 void SIInstrInfo::materializeImmediate(MachineBasicBlock &MBB, 630 MachineBasicBlock::iterator MI, 631 const DebugLoc &DL, unsigned DestReg, 632 int64_t Value) const { 633 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 634 const TargetRegisterClass *RegClass = MRI.getRegClass(DestReg); 635 if (RegClass == &AMDGPU::SReg_32RegClass || 636 RegClass == &AMDGPU::SGPR_32RegClass || 637 RegClass == &AMDGPU::SReg_32_XM0RegClass || 638 RegClass == &AMDGPU::SReg_32_XM0_XEXECRegClass) { 639 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B32), DestReg) 640 .addImm(Value); 641 return; 642 } 643 644 if (RegClass == &AMDGPU::SReg_64RegClass || 645 RegClass == &AMDGPU::SGPR_64RegClass || 646 RegClass == &AMDGPU::SReg_64_XEXECRegClass) { 647 BuildMI(MBB, MI, DL, get(AMDGPU::S_MOV_B64), DestReg) 648 .addImm(Value); 649 return; 650 } 651 652 if (RegClass == &AMDGPU::VGPR_32RegClass) { 653 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DestReg) 654 .addImm(Value); 655 return; 656 } 657 if (RegClass == &AMDGPU::VReg_64RegClass) { 658 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO), DestReg) 659 .addImm(Value); 660 return; 661 } 662 663 unsigned EltSize = 4; 664 unsigned Opcode = AMDGPU::V_MOV_B32_e32; 665 if (RI.isSGPRClass(RegClass)) { 666 if (RI.getRegSizeInBits(*RegClass) > 32) { 667 Opcode = AMDGPU::S_MOV_B64; 668 EltSize = 8; 669 } else { 670 Opcode = AMDGPU::S_MOV_B32; 671 EltSize = 4; 672 } 673 } 674 675 ArrayRef<int16_t> SubIndices = RI.getRegSplitParts(RegClass, EltSize); 676 for (unsigned Idx = 0; Idx < SubIndices.size(); ++Idx) { 677 int64_t IdxValue = Idx == 0 ? Value : 0; 678 679 MachineInstrBuilder Builder = BuildMI(MBB, MI, DL, 680 get(Opcode), RI.getSubReg(DestReg, Idx)); 681 Builder.addImm(IdxValue); 682 } 683 } 684 685 const TargetRegisterClass * 686 SIInstrInfo::getPreferredSelectRegClass(unsigned Size) const { 687 return &AMDGPU::VGPR_32RegClass; 688 } 689 690 void SIInstrInfo::insertVectorSelect(MachineBasicBlock &MBB, 691 MachineBasicBlock::iterator I, 692 const DebugLoc &DL, unsigned DstReg, 693 ArrayRef<MachineOperand> Cond, 694 unsigned TrueReg, 695 unsigned FalseReg) const { 696 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 697 assert(MRI.getRegClass(DstReg) == &AMDGPU::VGPR_32RegClass && 698 "Not a VGPR32 reg"); 699 700 if (Cond.size() == 1) { 701 unsigned SReg = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 702 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg) 703 .add(Cond[0]); 704 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 705 .addReg(FalseReg) 706 .addReg(TrueReg) 707 .addReg(SReg); 708 } else if (Cond.size() == 2) { 709 assert(Cond[0].isImm() && "Cond[0] is not an immediate"); 710 switch (Cond[0].getImm()) { 711 case SIInstrInfo::SCC_TRUE: { 712 unsigned SReg = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 713 BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B64), SReg) 714 .addImm(-1) 715 .addImm(0); 716 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 717 .addReg(FalseReg) 718 .addReg(TrueReg) 719 .addReg(SReg); 720 break; 721 } 722 case SIInstrInfo::SCC_FALSE: { 723 unsigned SReg = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 724 BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B64), SReg) 725 .addImm(0) 726 .addImm(-1); 727 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 728 .addReg(FalseReg) 729 .addReg(TrueReg) 730 .addReg(SReg); 731 break; 732 } 733 case SIInstrInfo::VCCNZ: { 734 MachineOperand RegOp = Cond[1]; 735 RegOp.setImplicit(false); 736 unsigned SReg = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 737 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg) 738 .add(RegOp); 739 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 740 .addReg(FalseReg) 741 .addReg(TrueReg) 742 .addReg(SReg); 743 break; 744 } 745 case SIInstrInfo::VCCZ: { 746 MachineOperand RegOp = Cond[1]; 747 RegOp.setImplicit(false); 748 unsigned SReg = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 749 BuildMI(MBB, I, DL, get(AMDGPU::COPY), SReg) 750 .add(RegOp); 751 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 752 .addReg(TrueReg) 753 .addReg(FalseReg) 754 .addReg(SReg); 755 break; 756 } 757 case SIInstrInfo::EXECNZ: { 758 unsigned SReg = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 759 unsigned SReg2 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 760 BuildMI(MBB, I, DL, get(AMDGPU::S_OR_SAVEEXEC_B64), SReg2) 761 .addImm(0); 762 BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B64), SReg) 763 .addImm(-1) 764 .addImm(0); 765 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 766 .addReg(FalseReg) 767 .addReg(TrueReg) 768 .addReg(SReg); 769 break; 770 } 771 case SIInstrInfo::EXECZ: { 772 unsigned SReg = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 773 unsigned SReg2 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 774 BuildMI(MBB, I, DL, get(AMDGPU::S_OR_SAVEEXEC_B64), SReg2) 775 .addImm(0); 776 BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B64), SReg) 777 .addImm(0) 778 .addImm(-1); 779 BuildMI(MBB, I, DL, get(AMDGPU::V_CNDMASK_B32_e64), DstReg) 780 .addReg(FalseReg) 781 .addReg(TrueReg) 782 .addReg(SReg); 783 llvm_unreachable("Unhandled branch predicate EXECZ"); 784 break; 785 } 786 default: 787 llvm_unreachable("invalid branch predicate"); 788 } 789 } else { 790 llvm_unreachable("Can only handle Cond size 1 or 2"); 791 } 792 } 793 794 unsigned SIInstrInfo::insertEQ(MachineBasicBlock *MBB, 795 MachineBasicBlock::iterator I, 796 const DebugLoc &DL, 797 unsigned SrcReg, int Value) const { 798 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 799 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 800 BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_EQ_I32_e64), Reg) 801 .addImm(Value) 802 .addReg(SrcReg); 803 804 return Reg; 805 } 806 807 unsigned SIInstrInfo::insertNE(MachineBasicBlock *MBB, 808 MachineBasicBlock::iterator I, 809 const DebugLoc &DL, 810 unsigned SrcReg, int Value) const { 811 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 812 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 813 BuildMI(*MBB, I, DL, get(AMDGPU::V_CMP_NE_I32_e64), Reg) 814 .addImm(Value) 815 .addReg(SrcReg); 816 817 return Reg; 818 } 819 820 unsigned SIInstrInfo::getMovOpcode(const TargetRegisterClass *DstRC) const { 821 822 if (RI.getRegSizeInBits(*DstRC) == 32) { 823 return RI.isSGPRClass(DstRC) ? AMDGPU::S_MOV_B32 : AMDGPU::V_MOV_B32_e32; 824 } else if (RI.getRegSizeInBits(*DstRC) == 64 && RI.isSGPRClass(DstRC)) { 825 return AMDGPU::S_MOV_B64; 826 } else if (RI.getRegSizeInBits(*DstRC) == 64 && !RI.isSGPRClass(DstRC)) { 827 return AMDGPU::V_MOV_B64_PSEUDO; 828 } 829 return AMDGPU::COPY; 830 } 831 832 static unsigned getSGPRSpillSaveOpcode(unsigned Size) { 833 switch (Size) { 834 case 4: 835 return AMDGPU::SI_SPILL_S32_SAVE; 836 case 8: 837 return AMDGPU::SI_SPILL_S64_SAVE; 838 case 16: 839 return AMDGPU::SI_SPILL_S128_SAVE; 840 case 32: 841 return AMDGPU::SI_SPILL_S256_SAVE; 842 case 64: 843 return AMDGPU::SI_SPILL_S512_SAVE; 844 default: 845 llvm_unreachable("unknown register size"); 846 } 847 } 848 849 static unsigned getVGPRSpillSaveOpcode(unsigned Size) { 850 switch (Size) { 851 case 4: 852 return AMDGPU::SI_SPILL_V32_SAVE; 853 case 8: 854 return AMDGPU::SI_SPILL_V64_SAVE; 855 case 12: 856 return AMDGPU::SI_SPILL_V96_SAVE; 857 case 16: 858 return AMDGPU::SI_SPILL_V128_SAVE; 859 case 32: 860 return AMDGPU::SI_SPILL_V256_SAVE; 861 case 64: 862 return AMDGPU::SI_SPILL_V512_SAVE; 863 default: 864 llvm_unreachable("unknown register size"); 865 } 866 } 867 868 void SIInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 869 MachineBasicBlock::iterator MI, 870 unsigned SrcReg, bool isKill, 871 int FrameIndex, 872 const TargetRegisterClass *RC, 873 const TargetRegisterInfo *TRI) const { 874 MachineFunction *MF = MBB.getParent(); 875 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 876 MachineFrameInfo &FrameInfo = MF->getFrameInfo(); 877 const DebugLoc &DL = MBB.findDebugLoc(MI); 878 879 unsigned Size = FrameInfo.getObjectSize(FrameIndex); 880 unsigned Align = FrameInfo.getObjectAlignment(FrameIndex); 881 MachinePointerInfo PtrInfo 882 = MachinePointerInfo::getFixedStack(*MF, FrameIndex); 883 MachineMemOperand *MMO 884 = MF->getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore, 885 Size, Align); 886 unsigned SpillSize = TRI->getSpillSize(*RC); 887 888 if (RI.isSGPRClass(RC)) { 889 MFI->setHasSpilledSGPRs(); 890 891 // We are only allowed to create one new instruction when spilling 892 // registers, so we need to use pseudo instruction for spilling SGPRs. 893 const MCInstrDesc &OpDesc = get(getSGPRSpillSaveOpcode(SpillSize)); 894 895 // The SGPR spill/restore instructions only work on number sgprs, so we need 896 // to make sure we are using the correct register class. 897 if (TargetRegisterInfo::isVirtualRegister(SrcReg) && SpillSize == 4) { 898 MachineRegisterInfo &MRI = MF->getRegInfo(); 899 MRI.constrainRegClass(SrcReg, &AMDGPU::SReg_32_XM0RegClass); 900 } 901 902 MachineInstrBuilder Spill = BuildMI(MBB, MI, DL, OpDesc) 903 .addReg(SrcReg, getKillRegState(isKill)) // data 904 .addFrameIndex(FrameIndex) // addr 905 .addMemOperand(MMO) 906 .addReg(MFI->getScratchRSrcReg(), RegState::Implicit) 907 .addReg(MFI->getFrameOffsetReg(), RegState::Implicit); 908 // Add the scratch resource registers as implicit uses because we may end up 909 // needing them, and need to ensure that the reserved registers are 910 // correctly handled. 911 912 FrameInfo.setStackID(FrameIndex, SIStackID::SGPR_SPILL); 913 if (ST.hasScalarStores()) { 914 // m0 is used for offset to scalar stores if used to spill. 915 Spill.addReg(AMDGPU::M0, RegState::ImplicitDefine | RegState::Dead); 916 } 917 918 return; 919 } 920 921 assert(RI.hasVGPRs(RC) && "Only VGPR spilling expected"); 922 923 unsigned Opcode = getVGPRSpillSaveOpcode(SpillSize); 924 MFI->setHasSpilledVGPRs(); 925 BuildMI(MBB, MI, DL, get(Opcode)) 926 .addReg(SrcReg, getKillRegState(isKill)) // data 927 .addFrameIndex(FrameIndex) // addr 928 .addReg(MFI->getScratchRSrcReg()) // scratch_rsrc 929 .addReg(MFI->getFrameOffsetReg()) // scratch_offset 930 .addImm(0) // offset 931 .addMemOperand(MMO); 932 } 933 934 static unsigned getSGPRSpillRestoreOpcode(unsigned Size) { 935 switch (Size) { 936 case 4: 937 return AMDGPU::SI_SPILL_S32_RESTORE; 938 case 8: 939 return AMDGPU::SI_SPILL_S64_RESTORE; 940 case 16: 941 return AMDGPU::SI_SPILL_S128_RESTORE; 942 case 32: 943 return AMDGPU::SI_SPILL_S256_RESTORE; 944 case 64: 945 return AMDGPU::SI_SPILL_S512_RESTORE; 946 default: 947 llvm_unreachable("unknown register size"); 948 } 949 } 950 951 static unsigned getVGPRSpillRestoreOpcode(unsigned Size) { 952 switch (Size) { 953 case 4: 954 return AMDGPU::SI_SPILL_V32_RESTORE; 955 case 8: 956 return AMDGPU::SI_SPILL_V64_RESTORE; 957 case 12: 958 return AMDGPU::SI_SPILL_V96_RESTORE; 959 case 16: 960 return AMDGPU::SI_SPILL_V128_RESTORE; 961 case 32: 962 return AMDGPU::SI_SPILL_V256_RESTORE; 963 case 64: 964 return AMDGPU::SI_SPILL_V512_RESTORE; 965 default: 966 llvm_unreachable("unknown register size"); 967 } 968 } 969 970 void SIInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 971 MachineBasicBlock::iterator MI, 972 unsigned DestReg, int FrameIndex, 973 const TargetRegisterClass *RC, 974 const TargetRegisterInfo *TRI) const { 975 MachineFunction *MF = MBB.getParent(); 976 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 977 MachineFrameInfo &FrameInfo = MF->getFrameInfo(); 978 const DebugLoc &DL = MBB.findDebugLoc(MI); 979 unsigned Align = FrameInfo.getObjectAlignment(FrameIndex); 980 unsigned Size = FrameInfo.getObjectSize(FrameIndex); 981 unsigned SpillSize = TRI->getSpillSize(*RC); 982 983 MachinePointerInfo PtrInfo 984 = MachinePointerInfo::getFixedStack(*MF, FrameIndex); 985 986 MachineMemOperand *MMO = MF->getMachineMemOperand( 987 PtrInfo, MachineMemOperand::MOLoad, Size, Align); 988 989 if (RI.isSGPRClass(RC)) { 990 MFI->setHasSpilledSGPRs(); 991 992 // FIXME: Maybe this should not include a memoperand because it will be 993 // lowered to non-memory instructions. 994 const MCInstrDesc &OpDesc = get(getSGPRSpillRestoreOpcode(SpillSize)); 995 if (TargetRegisterInfo::isVirtualRegister(DestReg) && SpillSize == 4) { 996 MachineRegisterInfo &MRI = MF->getRegInfo(); 997 MRI.constrainRegClass(DestReg, &AMDGPU::SReg_32_XM0RegClass); 998 } 999 1000 FrameInfo.setStackID(FrameIndex, SIStackID::SGPR_SPILL); 1001 MachineInstrBuilder Spill = BuildMI(MBB, MI, DL, OpDesc, DestReg) 1002 .addFrameIndex(FrameIndex) // addr 1003 .addMemOperand(MMO) 1004 .addReg(MFI->getScratchRSrcReg(), RegState::Implicit) 1005 .addReg(MFI->getFrameOffsetReg(), RegState::Implicit); 1006 1007 if (ST.hasScalarStores()) { 1008 // m0 is used for offset to scalar stores if used to spill. 1009 Spill.addReg(AMDGPU::M0, RegState::ImplicitDefine | RegState::Dead); 1010 } 1011 1012 return; 1013 } 1014 1015 assert(RI.hasVGPRs(RC) && "Only VGPR spilling expected"); 1016 1017 unsigned Opcode = getVGPRSpillRestoreOpcode(SpillSize); 1018 BuildMI(MBB, MI, DL, get(Opcode), DestReg) 1019 .addFrameIndex(FrameIndex) // vaddr 1020 .addReg(MFI->getScratchRSrcReg()) // scratch_rsrc 1021 .addReg(MFI->getFrameOffsetReg()) // scratch_offset 1022 .addImm(0) // offset 1023 .addMemOperand(MMO); 1024 } 1025 1026 /// \param @Offset Offset in bytes of the FrameIndex being spilled 1027 unsigned SIInstrInfo::calculateLDSSpillAddress( 1028 MachineBasicBlock &MBB, MachineInstr &MI, RegScavenger *RS, unsigned TmpReg, 1029 unsigned FrameOffset, unsigned Size) const { 1030 MachineFunction *MF = MBB.getParent(); 1031 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 1032 const GCNSubtarget &ST = MF->getSubtarget<GCNSubtarget>(); 1033 const DebugLoc &DL = MBB.findDebugLoc(MI); 1034 unsigned WorkGroupSize = MFI->getMaxFlatWorkGroupSize(); 1035 unsigned WavefrontSize = ST.getWavefrontSize(); 1036 1037 unsigned TIDReg = MFI->getTIDReg(); 1038 if (!MFI->hasCalculatedTID()) { 1039 MachineBasicBlock &Entry = MBB.getParent()->front(); 1040 MachineBasicBlock::iterator Insert = Entry.front(); 1041 const DebugLoc &DL = Insert->getDebugLoc(); 1042 1043 TIDReg = RI.findUnusedRegister(MF->getRegInfo(), &AMDGPU::VGPR_32RegClass, 1044 *MF); 1045 if (TIDReg == AMDGPU::NoRegister) 1046 return TIDReg; 1047 1048 if (!AMDGPU::isShader(MF->getFunction().getCallingConv()) && 1049 WorkGroupSize > WavefrontSize) { 1050 unsigned TIDIGXReg 1051 = MFI->getPreloadedReg(AMDGPUFunctionArgInfo::WORKGROUP_ID_X); 1052 unsigned TIDIGYReg 1053 = MFI->getPreloadedReg(AMDGPUFunctionArgInfo::WORKGROUP_ID_Y); 1054 unsigned TIDIGZReg 1055 = MFI->getPreloadedReg(AMDGPUFunctionArgInfo::WORKGROUP_ID_Z); 1056 unsigned InputPtrReg = 1057 MFI->getPreloadedReg(AMDGPUFunctionArgInfo::KERNARG_SEGMENT_PTR); 1058 for (unsigned Reg : {TIDIGXReg, TIDIGYReg, TIDIGZReg}) { 1059 if (!Entry.isLiveIn(Reg)) 1060 Entry.addLiveIn(Reg); 1061 } 1062 1063 RS->enterBasicBlock(Entry); 1064 // FIXME: Can we scavenge an SReg_64 and access the subregs? 1065 unsigned STmp0 = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, 0); 1066 unsigned STmp1 = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, 0); 1067 BuildMI(Entry, Insert, DL, get(AMDGPU::S_LOAD_DWORD_IMM), STmp0) 1068 .addReg(InputPtrReg) 1069 .addImm(SI::KernelInputOffsets::NGROUPS_Z); 1070 BuildMI(Entry, Insert, DL, get(AMDGPU::S_LOAD_DWORD_IMM), STmp1) 1071 .addReg(InputPtrReg) 1072 .addImm(SI::KernelInputOffsets::NGROUPS_Y); 1073 1074 // NGROUPS.X * NGROUPS.Y 1075 BuildMI(Entry, Insert, DL, get(AMDGPU::S_MUL_I32), STmp1) 1076 .addReg(STmp1) 1077 .addReg(STmp0); 1078 // (NGROUPS.X * NGROUPS.Y) * TIDIG.X 1079 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MUL_U32_U24_e32), TIDReg) 1080 .addReg(STmp1) 1081 .addReg(TIDIGXReg); 1082 // NGROUPS.Z * TIDIG.Y + (NGROUPS.X * NGROPUS.Y * TIDIG.X) 1083 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MAD_U32_U24), TIDReg) 1084 .addReg(STmp0) 1085 .addReg(TIDIGYReg) 1086 .addReg(TIDReg); 1087 // (NGROUPS.Z * TIDIG.Y + (NGROUPS.X * NGROPUS.Y * TIDIG.X)) + TIDIG.Z 1088 getAddNoCarry(Entry, Insert, DL, TIDReg) 1089 .addReg(TIDReg) 1090 .addReg(TIDIGZReg); 1091 } else { 1092 // Get the wave id 1093 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MBCNT_LO_U32_B32_e64), 1094 TIDReg) 1095 .addImm(-1) 1096 .addImm(0); 1097 1098 BuildMI(Entry, Insert, DL, get(AMDGPU::V_MBCNT_HI_U32_B32_e64), 1099 TIDReg) 1100 .addImm(-1) 1101 .addReg(TIDReg); 1102 } 1103 1104 BuildMI(Entry, Insert, DL, get(AMDGPU::V_LSHLREV_B32_e32), 1105 TIDReg) 1106 .addImm(2) 1107 .addReg(TIDReg); 1108 MFI->setTIDReg(TIDReg); 1109 } 1110 1111 // Add FrameIndex to LDS offset 1112 unsigned LDSOffset = MFI->getLDSSize() + (FrameOffset * WorkGroupSize); 1113 getAddNoCarry(MBB, MI, DL, TmpReg) 1114 .addImm(LDSOffset) 1115 .addReg(TIDReg); 1116 1117 return TmpReg; 1118 } 1119 1120 void SIInstrInfo::insertWaitStates(MachineBasicBlock &MBB, 1121 MachineBasicBlock::iterator MI, 1122 int Count) const { 1123 DebugLoc DL = MBB.findDebugLoc(MI); 1124 while (Count > 0) { 1125 int Arg; 1126 if (Count >= 8) 1127 Arg = 7; 1128 else 1129 Arg = Count - 1; 1130 Count -= 8; 1131 BuildMI(MBB, MI, DL, get(AMDGPU::S_NOP)) 1132 .addImm(Arg); 1133 } 1134 } 1135 1136 void SIInstrInfo::insertNoop(MachineBasicBlock &MBB, 1137 MachineBasicBlock::iterator MI) const { 1138 insertWaitStates(MBB, MI, 1); 1139 } 1140 1141 void SIInstrInfo::insertReturn(MachineBasicBlock &MBB) const { 1142 auto MF = MBB.getParent(); 1143 SIMachineFunctionInfo *Info = MF->getInfo<SIMachineFunctionInfo>(); 1144 1145 assert(Info->isEntryFunction()); 1146 1147 if (MBB.succ_empty()) { 1148 bool HasNoTerminator = MBB.getFirstTerminator() == MBB.end(); 1149 if (HasNoTerminator) 1150 BuildMI(MBB, MBB.end(), DebugLoc(), 1151 get(Info->returnsVoid() ? AMDGPU::S_ENDPGM : AMDGPU::SI_RETURN_TO_EPILOG)); 1152 } 1153 } 1154 1155 unsigned SIInstrInfo::getNumWaitStates(const MachineInstr &MI) { 1156 switch (MI.getOpcode()) { 1157 default: return 1; // FIXME: Do wait states equal cycles? 1158 1159 case AMDGPU::S_NOP: 1160 return MI.getOperand(0).getImm() + 1; 1161 } 1162 } 1163 1164 bool SIInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { 1165 MachineBasicBlock &MBB = *MI.getParent(); 1166 DebugLoc DL = MBB.findDebugLoc(MI); 1167 switch (MI.getOpcode()) { 1168 default: return TargetInstrInfo::expandPostRAPseudo(MI); 1169 case AMDGPU::S_MOV_B64_term: 1170 // This is only a terminator to get the correct spill code placement during 1171 // register allocation. 1172 MI.setDesc(get(AMDGPU::S_MOV_B64)); 1173 break; 1174 1175 case AMDGPU::S_XOR_B64_term: 1176 // This is only a terminator to get the correct spill code placement during 1177 // register allocation. 1178 MI.setDesc(get(AMDGPU::S_XOR_B64)); 1179 break; 1180 1181 case AMDGPU::S_ANDN2_B64_term: 1182 // This is only a terminator to get the correct spill code placement during 1183 // register allocation. 1184 MI.setDesc(get(AMDGPU::S_ANDN2_B64)); 1185 break; 1186 1187 case AMDGPU::V_MOV_B64_PSEUDO: { 1188 unsigned Dst = MI.getOperand(0).getReg(); 1189 unsigned DstLo = RI.getSubReg(Dst, AMDGPU::sub0); 1190 unsigned DstHi = RI.getSubReg(Dst, AMDGPU::sub1); 1191 1192 const MachineOperand &SrcOp = MI.getOperand(1); 1193 // FIXME: Will this work for 64-bit floating point immediates? 1194 assert(!SrcOp.isFPImm()); 1195 if (SrcOp.isImm()) { 1196 APInt Imm(64, SrcOp.getImm()); 1197 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo) 1198 .addImm(Imm.getLoBits(32).getZExtValue()) 1199 .addReg(Dst, RegState::Implicit | RegState::Define); 1200 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi) 1201 .addImm(Imm.getHiBits(32).getZExtValue()) 1202 .addReg(Dst, RegState::Implicit | RegState::Define); 1203 } else { 1204 assert(SrcOp.isReg()); 1205 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstLo) 1206 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub0)) 1207 .addReg(Dst, RegState::Implicit | RegState::Define); 1208 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), DstHi) 1209 .addReg(RI.getSubReg(SrcOp.getReg(), AMDGPU::sub1)) 1210 .addReg(Dst, RegState::Implicit | RegState::Define); 1211 } 1212 MI.eraseFromParent(); 1213 break; 1214 } 1215 case AMDGPU::V_SET_INACTIVE_B32: { 1216 BuildMI(MBB, MI, DL, get(AMDGPU::S_NOT_B64), AMDGPU::EXEC) 1217 .addReg(AMDGPU::EXEC); 1218 BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B32_e32), MI.getOperand(0).getReg()) 1219 .add(MI.getOperand(2)); 1220 BuildMI(MBB, MI, DL, get(AMDGPU::S_NOT_B64), AMDGPU::EXEC) 1221 .addReg(AMDGPU::EXEC); 1222 MI.eraseFromParent(); 1223 break; 1224 } 1225 case AMDGPU::V_SET_INACTIVE_B64: { 1226 BuildMI(MBB, MI, DL, get(AMDGPU::S_NOT_B64), AMDGPU::EXEC) 1227 .addReg(AMDGPU::EXEC); 1228 MachineInstr *Copy = BuildMI(MBB, MI, DL, get(AMDGPU::V_MOV_B64_PSEUDO), 1229 MI.getOperand(0).getReg()) 1230 .add(MI.getOperand(2)); 1231 expandPostRAPseudo(*Copy); 1232 BuildMI(MBB, MI, DL, get(AMDGPU::S_NOT_B64), AMDGPU::EXEC) 1233 .addReg(AMDGPU::EXEC); 1234 MI.eraseFromParent(); 1235 break; 1236 } 1237 case AMDGPU::V_MOVRELD_B32_V1: 1238 case AMDGPU::V_MOVRELD_B32_V2: 1239 case AMDGPU::V_MOVRELD_B32_V4: 1240 case AMDGPU::V_MOVRELD_B32_V8: 1241 case AMDGPU::V_MOVRELD_B32_V16: { 1242 const MCInstrDesc &MovRelDesc = get(AMDGPU::V_MOVRELD_B32_e32); 1243 unsigned VecReg = MI.getOperand(0).getReg(); 1244 bool IsUndef = MI.getOperand(1).isUndef(); 1245 unsigned SubReg = AMDGPU::sub0 + MI.getOperand(3).getImm(); 1246 assert(VecReg == MI.getOperand(1).getReg()); 1247 1248 MachineInstr *MovRel = 1249 BuildMI(MBB, MI, DL, MovRelDesc) 1250 .addReg(RI.getSubReg(VecReg, SubReg), RegState::Undef) 1251 .add(MI.getOperand(2)) 1252 .addReg(VecReg, RegState::ImplicitDefine) 1253 .addReg(VecReg, 1254 RegState::Implicit | (IsUndef ? RegState::Undef : 0)); 1255 1256 const int ImpDefIdx = 1257 MovRelDesc.getNumOperands() + MovRelDesc.getNumImplicitUses(); 1258 const int ImpUseIdx = ImpDefIdx + 1; 1259 MovRel->tieOperands(ImpDefIdx, ImpUseIdx); 1260 1261 MI.eraseFromParent(); 1262 break; 1263 } 1264 case AMDGPU::SI_PC_ADD_REL_OFFSET: { 1265 MachineFunction &MF = *MBB.getParent(); 1266 unsigned Reg = MI.getOperand(0).getReg(); 1267 unsigned RegLo = RI.getSubReg(Reg, AMDGPU::sub0); 1268 unsigned RegHi = RI.getSubReg(Reg, AMDGPU::sub1); 1269 1270 // Create a bundle so these instructions won't be re-ordered by the 1271 // post-RA scheduler. 1272 MIBundleBuilder Bundler(MBB, MI); 1273 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_GETPC_B64), Reg)); 1274 1275 // Add 32-bit offset from this instruction to the start of the 1276 // constant data. 1277 Bundler.append(BuildMI(MF, DL, get(AMDGPU::S_ADD_U32), RegLo) 1278 .addReg(RegLo) 1279 .add(MI.getOperand(1))); 1280 1281 MachineInstrBuilder MIB = BuildMI(MF, DL, get(AMDGPU::S_ADDC_U32), RegHi) 1282 .addReg(RegHi); 1283 if (MI.getOperand(2).getTargetFlags() == SIInstrInfo::MO_NONE) 1284 MIB.addImm(0); 1285 else 1286 MIB.add(MI.getOperand(2)); 1287 1288 Bundler.append(MIB); 1289 finalizeBundle(MBB, Bundler.begin()); 1290 1291 MI.eraseFromParent(); 1292 break; 1293 } 1294 case AMDGPU::EXIT_WWM: { 1295 // This only gets its own opcode so that SIFixWWMLiveness can tell when WWM 1296 // is exited. 1297 MI.setDesc(get(AMDGPU::S_MOV_B64)); 1298 break; 1299 } 1300 case TargetOpcode::BUNDLE: { 1301 if (!MI.mayLoad()) 1302 return false; 1303 1304 // If it is a load it must be a memory clause 1305 for (MachineBasicBlock::instr_iterator I = MI.getIterator(); 1306 I->isBundledWithSucc(); ++I) { 1307 I->unbundleFromSucc(); 1308 for (MachineOperand &MO : I->operands()) 1309 if (MO.isReg()) 1310 MO.setIsInternalRead(false); 1311 } 1312 1313 MI.eraseFromParent(); 1314 break; 1315 } 1316 } 1317 return true; 1318 } 1319 1320 bool SIInstrInfo::swapSourceModifiers(MachineInstr &MI, 1321 MachineOperand &Src0, 1322 unsigned Src0OpName, 1323 MachineOperand &Src1, 1324 unsigned Src1OpName) const { 1325 MachineOperand *Src0Mods = getNamedOperand(MI, Src0OpName); 1326 if (!Src0Mods) 1327 return false; 1328 1329 MachineOperand *Src1Mods = getNamedOperand(MI, Src1OpName); 1330 assert(Src1Mods && 1331 "All commutable instructions have both src0 and src1 modifiers"); 1332 1333 int Src0ModsVal = Src0Mods->getImm(); 1334 int Src1ModsVal = Src1Mods->getImm(); 1335 1336 Src1Mods->setImm(Src0ModsVal); 1337 Src0Mods->setImm(Src1ModsVal); 1338 return true; 1339 } 1340 1341 static MachineInstr *swapRegAndNonRegOperand(MachineInstr &MI, 1342 MachineOperand &RegOp, 1343 MachineOperand &NonRegOp) { 1344 unsigned Reg = RegOp.getReg(); 1345 unsigned SubReg = RegOp.getSubReg(); 1346 bool IsKill = RegOp.isKill(); 1347 bool IsDead = RegOp.isDead(); 1348 bool IsUndef = RegOp.isUndef(); 1349 bool IsDebug = RegOp.isDebug(); 1350 1351 if (NonRegOp.isImm()) 1352 RegOp.ChangeToImmediate(NonRegOp.getImm()); 1353 else if (NonRegOp.isFI()) 1354 RegOp.ChangeToFrameIndex(NonRegOp.getIndex()); 1355 else 1356 return nullptr; 1357 1358 NonRegOp.ChangeToRegister(Reg, false, false, IsKill, IsDead, IsUndef, IsDebug); 1359 NonRegOp.setSubReg(SubReg); 1360 1361 return &MI; 1362 } 1363 1364 MachineInstr *SIInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, 1365 unsigned Src0Idx, 1366 unsigned Src1Idx) const { 1367 assert(!NewMI && "this should never be used"); 1368 1369 unsigned Opc = MI.getOpcode(); 1370 int CommutedOpcode = commuteOpcode(Opc); 1371 if (CommutedOpcode == -1) 1372 return nullptr; 1373 1374 assert(AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0) == 1375 static_cast<int>(Src0Idx) && 1376 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1) == 1377 static_cast<int>(Src1Idx) && 1378 "inconsistency with findCommutedOpIndices"); 1379 1380 MachineOperand &Src0 = MI.getOperand(Src0Idx); 1381 MachineOperand &Src1 = MI.getOperand(Src1Idx); 1382 1383 MachineInstr *CommutedMI = nullptr; 1384 if (Src0.isReg() && Src1.isReg()) { 1385 if (isOperandLegal(MI, Src1Idx, &Src0)) { 1386 // Be sure to copy the source modifiers to the right place. 1387 CommutedMI 1388 = TargetInstrInfo::commuteInstructionImpl(MI, NewMI, Src0Idx, Src1Idx); 1389 } 1390 1391 } else if (Src0.isReg() && !Src1.isReg()) { 1392 // src0 should always be able to support any operand type, so no need to 1393 // check operand legality. 1394 CommutedMI = swapRegAndNonRegOperand(MI, Src0, Src1); 1395 } else if (!Src0.isReg() && Src1.isReg()) { 1396 if (isOperandLegal(MI, Src1Idx, &Src0)) 1397 CommutedMI = swapRegAndNonRegOperand(MI, Src1, Src0); 1398 } else { 1399 // FIXME: Found two non registers to commute. This does happen. 1400 return nullptr; 1401 } 1402 1403 if (CommutedMI) { 1404 swapSourceModifiers(MI, Src0, AMDGPU::OpName::src0_modifiers, 1405 Src1, AMDGPU::OpName::src1_modifiers); 1406 1407 CommutedMI->setDesc(get(CommutedOpcode)); 1408 } 1409 1410 return CommutedMI; 1411 } 1412 1413 // This needs to be implemented because the source modifiers may be inserted 1414 // between the true commutable operands, and the base 1415 // TargetInstrInfo::commuteInstruction uses it. 1416 bool SIInstrInfo::findCommutedOpIndices(MachineInstr &MI, unsigned &SrcOpIdx0, 1417 unsigned &SrcOpIdx1) const { 1418 return findCommutedOpIndices(MI.getDesc(), SrcOpIdx0, SrcOpIdx1); 1419 } 1420 1421 bool SIInstrInfo::findCommutedOpIndices(MCInstrDesc Desc, unsigned &SrcOpIdx0, 1422 unsigned &SrcOpIdx1) const { 1423 if (!Desc.isCommutable()) 1424 return false; 1425 1426 unsigned Opc = Desc.getOpcode(); 1427 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 1428 if (Src0Idx == -1) 1429 return false; 1430 1431 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 1432 if (Src1Idx == -1) 1433 return false; 1434 1435 return fixCommutedOpIndices(SrcOpIdx0, SrcOpIdx1, Src0Idx, Src1Idx); 1436 } 1437 1438 bool SIInstrInfo::isBranchOffsetInRange(unsigned BranchOp, 1439 int64_t BrOffset) const { 1440 // BranchRelaxation should never have to check s_setpc_b64 because its dest 1441 // block is unanalyzable. 1442 assert(BranchOp != AMDGPU::S_SETPC_B64); 1443 1444 // Convert to dwords. 1445 BrOffset /= 4; 1446 1447 // The branch instructions do PC += signext(SIMM16 * 4) + 4, so the offset is 1448 // from the next instruction. 1449 BrOffset -= 1; 1450 1451 return isIntN(BranchOffsetBits, BrOffset); 1452 } 1453 1454 MachineBasicBlock *SIInstrInfo::getBranchDestBlock( 1455 const MachineInstr &MI) const { 1456 if (MI.getOpcode() == AMDGPU::S_SETPC_B64) { 1457 // This would be a difficult analysis to perform, but can always be legal so 1458 // there's no need to analyze it. 1459 return nullptr; 1460 } 1461 1462 return MI.getOperand(0).getMBB(); 1463 } 1464 1465 unsigned SIInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB, 1466 MachineBasicBlock &DestBB, 1467 const DebugLoc &DL, 1468 int64_t BrOffset, 1469 RegScavenger *RS) const { 1470 assert(RS && "RegScavenger required for long branching"); 1471 assert(MBB.empty() && 1472 "new block should be inserted for expanding unconditional branch"); 1473 assert(MBB.pred_size() == 1); 1474 1475 MachineFunction *MF = MBB.getParent(); 1476 MachineRegisterInfo &MRI = MF->getRegInfo(); 1477 1478 // FIXME: Virtual register workaround for RegScavenger not working with empty 1479 // blocks. 1480 unsigned PCReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 1481 1482 auto I = MBB.end(); 1483 1484 // We need to compute the offset relative to the instruction immediately after 1485 // s_getpc_b64. Insert pc arithmetic code before last terminator. 1486 MachineInstr *GetPC = BuildMI(MBB, I, DL, get(AMDGPU::S_GETPC_B64), PCReg); 1487 1488 // TODO: Handle > 32-bit block address. 1489 if (BrOffset >= 0) { 1490 BuildMI(MBB, I, DL, get(AMDGPU::S_ADD_U32)) 1491 .addReg(PCReg, RegState::Define, AMDGPU::sub0) 1492 .addReg(PCReg, 0, AMDGPU::sub0) 1493 .addMBB(&DestBB, AMDGPU::TF_LONG_BRANCH_FORWARD); 1494 BuildMI(MBB, I, DL, get(AMDGPU::S_ADDC_U32)) 1495 .addReg(PCReg, RegState::Define, AMDGPU::sub1) 1496 .addReg(PCReg, 0, AMDGPU::sub1) 1497 .addImm(0); 1498 } else { 1499 // Backwards branch. 1500 BuildMI(MBB, I, DL, get(AMDGPU::S_SUB_U32)) 1501 .addReg(PCReg, RegState::Define, AMDGPU::sub0) 1502 .addReg(PCReg, 0, AMDGPU::sub0) 1503 .addMBB(&DestBB, AMDGPU::TF_LONG_BRANCH_BACKWARD); 1504 BuildMI(MBB, I, DL, get(AMDGPU::S_SUBB_U32)) 1505 .addReg(PCReg, RegState::Define, AMDGPU::sub1) 1506 .addReg(PCReg, 0, AMDGPU::sub1) 1507 .addImm(0); 1508 } 1509 1510 // Insert the indirect branch after the other terminator. 1511 BuildMI(&MBB, DL, get(AMDGPU::S_SETPC_B64)) 1512 .addReg(PCReg); 1513 1514 // FIXME: If spilling is necessary, this will fail because this scavenger has 1515 // no emergency stack slots. It is non-trivial to spill in this situation, 1516 // because the restore code needs to be specially placed after the 1517 // jump. BranchRelaxation then needs to be made aware of the newly inserted 1518 // block. 1519 // 1520 // If a spill is needed for the pc register pair, we need to insert a spill 1521 // restore block right before the destination block, and insert a short branch 1522 // into the old destination block's fallthrough predecessor. 1523 // e.g.: 1524 // 1525 // s_cbranch_scc0 skip_long_branch: 1526 // 1527 // long_branch_bb: 1528 // spill s[8:9] 1529 // s_getpc_b64 s[8:9] 1530 // s_add_u32 s8, s8, restore_bb 1531 // s_addc_u32 s9, s9, 0 1532 // s_setpc_b64 s[8:9] 1533 // 1534 // skip_long_branch: 1535 // foo; 1536 // 1537 // ..... 1538 // 1539 // dest_bb_fallthrough_predecessor: 1540 // bar; 1541 // s_branch dest_bb 1542 // 1543 // restore_bb: 1544 // restore s[8:9] 1545 // fallthrough dest_bb 1546 /// 1547 // dest_bb: 1548 // buzz; 1549 1550 RS->enterBasicBlockEnd(MBB); 1551 unsigned Scav = RS->scavengeRegisterBackwards( 1552 AMDGPU::SReg_64RegClass, 1553 MachineBasicBlock::iterator(GetPC), false, 0); 1554 MRI.replaceRegWith(PCReg, Scav); 1555 MRI.clearVirtRegs(); 1556 RS->setRegUsed(Scav); 1557 1558 return 4 + 8 + 4 + 4; 1559 } 1560 1561 unsigned SIInstrInfo::getBranchOpcode(SIInstrInfo::BranchPredicate Cond) { 1562 switch (Cond) { 1563 case SIInstrInfo::SCC_TRUE: 1564 return AMDGPU::S_CBRANCH_SCC1; 1565 case SIInstrInfo::SCC_FALSE: 1566 return AMDGPU::S_CBRANCH_SCC0; 1567 case SIInstrInfo::VCCNZ: 1568 return AMDGPU::S_CBRANCH_VCCNZ; 1569 case SIInstrInfo::VCCZ: 1570 return AMDGPU::S_CBRANCH_VCCZ; 1571 case SIInstrInfo::EXECNZ: 1572 return AMDGPU::S_CBRANCH_EXECNZ; 1573 case SIInstrInfo::EXECZ: 1574 return AMDGPU::S_CBRANCH_EXECZ; 1575 default: 1576 llvm_unreachable("invalid branch predicate"); 1577 } 1578 } 1579 1580 SIInstrInfo::BranchPredicate SIInstrInfo::getBranchPredicate(unsigned Opcode) { 1581 switch (Opcode) { 1582 case AMDGPU::S_CBRANCH_SCC0: 1583 return SCC_FALSE; 1584 case AMDGPU::S_CBRANCH_SCC1: 1585 return SCC_TRUE; 1586 case AMDGPU::S_CBRANCH_VCCNZ: 1587 return VCCNZ; 1588 case AMDGPU::S_CBRANCH_VCCZ: 1589 return VCCZ; 1590 case AMDGPU::S_CBRANCH_EXECNZ: 1591 return EXECNZ; 1592 case AMDGPU::S_CBRANCH_EXECZ: 1593 return EXECZ; 1594 default: 1595 return INVALID_BR; 1596 } 1597 } 1598 1599 bool SIInstrInfo::analyzeBranchImpl(MachineBasicBlock &MBB, 1600 MachineBasicBlock::iterator I, 1601 MachineBasicBlock *&TBB, 1602 MachineBasicBlock *&FBB, 1603 SmallVectorImpl<MachineOperand> &Cond, 1604 bool AllowModify) const { 1605 if (I->getOpcode() == AMDGPU::S_BRANCH) { 1606 // Unconditional Branch 1607 TBB = I->getOperand(0).getMBB(); 1608 return false; 1609 } 1610 1611 MachineBasicBlock *CondBB = nullptr; 1612 1613 if (I->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 1614 CondBB = I->getOperand(1).getMBB(); 1615 Cond.push_back(I->getOperand(0)); 1616 } else { 1617 BranchPredicate Pred = getBranchPredicate(I->getOpcode()); 1618 if (Pred == INVALID_BR) 1619 return true; 1620 1621 CondBB = I->getOperand(0).getMBB(); 1622 Cond.push_back(MachineOperand::CreateImm(Pred)); 1623 Cond.push_back(I->getOperand(1)); // Save the branch register. 1624 } 1625 ++I; 1626 1627 if (I == MBB.end()) { 1628 // Conditional branch followed by fall-through. 1629 TBB = CondBB; 1630 return false; 1631 } 1632 1633 if (I->getOpcode() == AMDGPU::S_BRANCH) { 1634 TBB = CondBB; 1635 FBB = I->getOperand(0).getMBB(); 1636 return false; 1637 } 1638 1639 return true; 1640 } 1641 1642 bool SIInstrInfo::analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, 1643 MachineBasicBlock *&FBB, 1644 SmallVectorImpl<MachineOperand> &Cond, 1645 bool AllowModify) const { 1646 MachineBasicBlock::iterator I = MBB.getFirstTerminator(); 1647 auto E = MBB.end(); 1648 if (I == E) 1649 return false; 1650 1651 // Skip over the instructions that are artificially terminators for special 1652 // exec management. 1653 while (I != E && !I->isBranch() && !I->isReturn() && 1654 I->getOpcode() != AMDGPU::SI_MASK_BRANCH) { 1655 switch (I->getOpcode()) { 1656 case AMDGPU::SI_MASK_BRANCH: 1657 case AMDGPU::S_MOV_B64_term: 1658 case AMDGPU::S_XOR_B64_term: 1659 case AMDGPU::S_ANDN2_B64_term: 1660 break; 1661 case AMDGPU::SI_IF: 1662 case AMDGPU::SI_ELSE: 1663 case AMDGPU::SI_KILL_I1_TERMINATOR: 1664 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR: 1665 // FIXME: It's messy that these need to be considered here at all. 1666 return true; 1667 default: 1668 llvm_unreachable("unexpected non-branch terminator inst"); 1669 } 1670 1671 ++I; 1672 } 1673 1674 if (I == E) 1675 return false; 1676 1677 if (I->getOpcode() != AMDGPU::SI_MASK_BRANCH) 1678 return analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify); 1679 1680 ++I; 1681 1682 // TODO: Should be able to treat as fallthrough? 1683 if (I == MBB.end()) 1684 return true; 1685 1686 if (analyzeBranchImpl(MBB, I, TBB, FBB, Cond, AllowModify)) 1687 return true; 1688 1689 MachineBasicBlock *MaskBrDest = I->getOperand(0).getMBB(); 1690 1691 // Specifically handle the case where the conditional branch is to the same 1692 // destination as the mask branch. e.g. 1693 // 1694 // si_mask_branch BB8 1695 // s_cbranch_execz BB8 1696 // s_cbranch BB9 1697 // 1698 // This is required to understand divergent loops which may need the branches 1699 // to be relaxed. 1700 if (TBB != MaskBrDest || Cond.empty()) 1701 return true; 1702 1703 auto Pred = Cond[0].getImm(); 1704 return (Pred != EXECZ && Pred != EXECNZ); 1705 } 1706 1707 unsigned SIInstrInfo::removeBranch(MachineBasicBlock &MBB, 1708 int *BytesRemoved) const { 1709 MachineBasicBlock::iterator I = MBB.getFirstTerminator(); 1710 1711 unsigned Count = 0; 1712 unsigned RemovedSize = 0; 1713 while (I != MBB.end()) { 1714 MachineBasicBlock::iterator Next = std::next(I); 1715 if (I->getOpcode() == AMDGPU::SI_MASK_BRANCH) { 1716 I = Next; 1717 continue; 1718 } 1719 1720 RemovedSize += getInstSizeInBytes(*I); 1721 I->eraseFromParent(); 1722 ++Count; 1723 I = Next; 1724 } 1725 1726 if (BytesRemoved) 1727 *BytesRemoved = RemovedSize; 1728 1729 return Count; 1730 } 1731 1732 // Copy the flags onto the implicit condition register operand. 1733 static void preserveCondRegFlags(MachineOperand &CondReg, 1734 const MachineOperand &OrigCond) { 1735 CondReg.setIsUndef(OrigCond.isUndef()); 1736 CondReg.setIsKill(OrigCond.isKill()); 1737 } 1738 1739 unsigned SIInstrInfo::insertBranch(MachineBasicBlock &MBB, 1740 MachineBasicBlock *TBB, 1741 MachineBasicBlock *FBB, 1742 ArrayRef<MachineOperand> Cond, 1743 const DebugLoc &DL, 1744 int *BytesAdded) const { 1745 if (!FBB && Cond.empty()) { 1746 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH)) 1747 .addMBB(TBB); 1748 if (BytesAdded) 1749 *BytesAdded = 4; 1750 return 1; 1751 } 1752 1753 if(Cond.size() == 1 && Cond[0].isReg()) { 1754 BuildMI(&MBB, DL, get(AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO)) 1755 .add(Cond[0]) 1756 .addMBB(TBB); 1757 return 1; 1758 } 1759 1760 assert(TBB && Cond[0].isImm()); 1761 1762 unsigned Opcode 1763 = getBranchOpcode(static_cast<BranchPredicate>(Cond[0].getImm())); 1764 1765 if (!FBB) { 1766 Cond[1].isUndef(); 1767 MachineInstr *CondBr = 1768 BuildMI(&MBB, DL, get(Opcode)) 1769 .addMBB(TBB); 1770 1771 // Copy the flags onto the implicit condition register operand. 1772 preserveCondRegFlags(CondBr->getOperand(1), Cond[1]); 1773 1774 if (BytesAdded) 1775 *BytesAdded = 4; 1776 return 1; 1777 } 1778 1779 assert(TBB && FBB); 1780 1781 MachineInstr *CondBr = 1782 BuildMI(&MBB, DL, get(Opcode)) 1783 .addMBB(TBB); 1784 BuildMI(&MBB, DL, get(AMDGPU::S_BRANCH)) 1785 .addMBB(FBB); 1786 1787 MachineOperand &CondReg = CondBr->getOperand(1); 1788 CondReg.setIsUndef(Cond[1].isUndef()); 1789 CondReg.setIsKill(Cond[1].isKill()); 1790 1791 if (BytesAdded) 1792 *BytesAdded = 8; 1793 1794 return 2; 1795 } 1796 1797 bool SIInstrInfo::reverseBranchCondition( 1798 SmallVectorImpl<MachineOperand> &Cond) const { 1799 if (Cond.size() != 2) { 1800 return true; 1801 } 1802 1803 if (Cond[0].isImm()) { 1804 Cond[0].setImm(-Cond[0].getImm()); 1805 return false; 1806 } 1807 1808 return true; 1809 } 1810 1811 bool SIInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, 1812 ArrayRef<MachineOperand> Cond, 1813 unsigned TrueReg, unsigned FalseReg, 1814 int &CondCycles, 1815 int &TrueCycles, int &FalseCycles) const { 1816 switch (Cond[0].getImm()) { 1817 case VCCNZ: 1818 case VCCZ: { 1819 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1820 const TargetRegisterClass *RC = MRI.getRegClass(TrueReg); 1821 assert(MRI.getRegClass(FalseReg) == RC); 1822 1823 int NumInsts = AMDGPU::getRegBitWidth(RC->getID()) / 32; 1824 CondCycles = TrueCycles = FalseCycles = NumInsts; // ??? 1825 1826 // Limit to equal cost for branch vs. N v_cndmask_b32s. 1827 return !RI.isSGPRClass(RC) && NumInsts <= 6; 1828 } 1829 case SCC_TRUE: 1830 case SCC_FALSE: { 1831 // FIXME: We could insert for VGPRs if we could replace the original compare 1832 // with a vector one. 1833 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1834 const TargetRegisterClass *RC = MRI.getRegClass(TrueReg); 1835 assert(MRI.getRegClass(FalseReg) == RC); 1836 1837 int NumInsts = AMDGPU::getRegBitWidth(RC->getID()) / 32; 1838 1839 // Multiples of 8 can do s_cselect_b64 1840 if (NumInsts % 2 == 0) 1841 NumInsts /= 2; 1842 1843 CondCycles = TrueCycles = FalseCycles = NumInsts; // ??? 1844 return RI.isSGPRClass(RC); 1845 } 1846 default: 1847 return false; 1848 } 1849 } 1850 1851 void SIInstrInfo::insertSelect(MachineBasicBlock &MBB, 1852 MachineBasicBlock::iterator I, const DebugLoc &DL, 1853 unsigned DstReg, ArrayRef<MachineOperand> Cond, 1854 unsigned TrueReg, unsigned FalseReg) const { 1855 BranchPredicate Pred = static_cast<BranchPredicate>(Cond[0].getImm()); 1856 if (Pred == VCCZ || Pred == SCC_FALSE) { 1857 Pred = static_cast<BranchPredicate>(-Pred); 1858 std::swap(TrueReg, FalseReg); 1859 } 1860 1861 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 1862 const TargetRegisterClass *DstRC = MRI.getRegClass(DstReg); 1863 unsigned DstSize = RI.getRegSizeInBits(*DstRC); 1864 1865 if (DstSize == 32) { 1866 unsigned SelOp = Pred == SCC_TRUE ? 1867 AMDGPU::S_CSELECT_B32 : AMDGPU::V_CNDMASK_B32_e32; 1868 1869 // Instruction's operands are backwards from what is expected. 1870 MachineInstr *Select = 1871 BuildMI(MBB, I, DL, get(SelOp), DstReg) 1872 .addReg(FalseReg) 1873 .addReg(TrueReg); 1874 1875 preserveCondRegFlags(Select->getOperand(3), Cond[1]); 1876 return; 1877 } 1878 1879 if (DstSize == 64 && Pred == SCC_TRUE) { 1880 MachineInstr *Select = 1881 BuildMI(MBB, I, DL, get(AMDGPU::S_CSELECT_B64), DstReg) 1882 .addReg(FalseReg) 1883 .addReg(TrueReg); 1884 1885 preserveCondRegFlags(Select->getOperand(3), Cond[1]); 1886 return; 1887 } 1888 1889 static const int16_t Sub0_15[] = { 1890 AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3, 1891 AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7, 1892 AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11, 1893 AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15, 1894 }; 1895 1896 static const int16_t Sub0_15_64[] = { 1897 AMDGPU::sub0_sub1, AMDGPU::sub2_sub3, 1898 AMDGPU::sub4_sub5, AMDGPU::sub6_sub7, 1899 AMDGPU::sub8_sub9, AMDGPU::sub10_sub11, 1900 AMDGPU::sub12_sub13, AMDGPU::sub14_sub15, 1901 }; 1902 1903 unsigned SelOp = AMDGPU::V_CNDMASK_B32_e32; 1904 const TargetRegisterClass *EltRC = &AMDGPU::VGPR_32RegClass; 1905 const int16_t *SubIndices = Sub0_15; 1906 int NElts = DstSize / 32; 1907 1908 // 64-bit select is only avaialble for SALU. 1909 if (Pred == SCC_TRUE) { 1910 SelOp = AMDGPU::S_CSELECT_B64; 1911 EltRC = &AMDGPU::SGPR_64RegClass; 1912 SubIndices = Sub0_15_64; 1913 1914 assert(NElts % 2 == 0); 1915 NElts /= 2; 1916 } 1917 1918 MachineInstrBuilder MIB = BuildMI( 1919 MBB, I, DL, get(AMDGPU::REG_SEQUENCE), DstReg); 1920 1921 I = MIB->getIterator(); 1922 1923 SmallVector<unsigned, 8> Regs; 1924 for (int Idx = 0; Idx != NElts; ++Idx) { 1925 unsigned DstElt = MRI.createVirtualRegister(EltRC); 1926 Regs.push_back(DstElt); 1927 1928 unsigned SubIdx = SubIndices[Idx]; 1929 1930 MachineInstr *Select = 1931 BuildMI(MBB, I, DL, get(SelOp), DstElt) 1932 .addReg(FalseReg, 0, SubIdx) 1933 .addReg(TrueReg, 0, SubIdx); 1934 preserveCondRegFlags(Select->getOperand(3), Cond[1]); 1935 1936 MIB.addReg(DstElt) 1937 .addImm(SubIdx); 1938 } 1939 } 1940 1941 bool SIInstrInfo::isFoldableCopy(const MachineInstr &MI) const { 1942 switch (MI.getOpcode()) { 1943 case AMDGPU::V_MOV_B32_e32: 1944 case AMDGPU::V_MOV_B32_e64: 1945 case AMDGPU::V_MOV_B64_PSEUDO: { 1946 // If there are additional implicit register operands, this may be used for 1947 // register indexing so the source register operand isn't simply copied. 1948 unsigned NumOps = MI.getDesc().getNumOperands() + 1949 MI.getDesc().getNumImplicitUses(); 1950 1951 return MI.getNumOperands() == NumOps; 1952 } 1953 case AMDGPU::S_MOV_B32: 1954 case AMDGPU::S_MOV_B64: 1955 case AMDGPU::COPY: 1956 return true; 1957 default: 1958 return false; 1959 } 1960 } 1961 1962 unsigned SIInstrInfo::getAddressSpaceForPseudoSourceKind( 1963 unsigned Kind) const { 1964 switch(Kind) { 1965 case PseudoSourceValue::Stack: 1966 case PseudoSourceValue::FixedStack: 1967 return AMDGPUAS::PRIVATE_ADDRESS; 1968 case PseudoSourceValue::ConstantPool: 1969 case PseudoSourceValue::GOT: 1970 case PseudoSourceValue::JumpTable: 1971 case PseudoSourceValue::GlobalValueCallEntry: 1972 case PseudoSourceValue::ExternalSymbolCallEntry: 1973 case PseudoSourceValue::TargetCustom: 1974 return AMDGPUAS::CONSTANT_ADDRESS; 1975 } 1976 return AMDGPUAS::FLAT_ADDRESS; 1977 } 1978 1979 static void removeModOperands(MachineInstr &MI) { 1980 unsigned Opc = MI.getOpcode(); 1981 int Src0ModIdx = AMDGPU::getNamedOperandIdx(Opc, 1982 AMDGPU::OpName::src0_modifiers); 1983 int Src1ModIdx = AMDGPU::getNamedOperandIdx(Opc, 1984 AMDGPU::OpName::src1_modifiers); 1985 int Src2ModIdx = AMDGPU::getNamedOperandIdx(Opc, 1986 AMDGPU::OpName::src2_modifiers); 1987 1988 MI.RemoveOperand(Src2ModIdx); 1989 MI.RemoveOperand(Src1ModIdx); 1990 MI.RemoveOperand(Src0ModIdx); 1991 } 1992 1993 bool SIInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, 1994 unsigned Reg, MachineRegisterInfo *MRI) const { 1995 if (!MRI->hasOneNonDBGUse(Reg)) 1996 return false; 1997 1998 switch (DefMI.getOpcode()) { 1999 default: 2000 return false; 2001 case AMDGPU::S_MOV_B64: 2002 // TODO: We could fold 64-bit immediates, but this get compilicated 2003 // when there are sub-registers. 2004 return false; 2005 2006 case AMDGPU::V_MOV_B32_e32: 2007 case AMDGPU::S_MOV_B32: 2008 break; 2009 } 2010 2011 const MachineOperand *ImmOp = getNamedOperand(DefMI, AMDGPU::OpName::src0); 2012 assert(ImmOp); 2013 // FIXME: We could handle FrameIndex values here. 2014 if (!ImmOp->isImm()) 2015 return false; 2016 2017 unsigned Opc = UseMI.getOpcode(); 2018 if (Opc == AMDGPU::COPY) { 2019 bool isVGPRCopy = RI.isVGPR(*MRI, UseMI.getOperand(0).getReg()); 2020 unsigned NewOpc = isVGPRCopy ? AMDGPU::V_MOV_B32_e32 : AMDGPU::S_MOV_B32; 2021 UseMI.setDesc(get(NewOpc)); 2022 UseMI.getOperand(1).ChangeToImmediate(ImmOp->getImm()); 2023 UseMI.addImplicitDefUseOperands(*UseMI.getParent()->getParent()); 2024 return true; 2025 } 2026 2027 if (Opc == AMDGPU::V_MAD_F32 || Opc == AMDGPU::V_MAC_F32_e64 || 2028 Opc == AMDGPU::V_MAD_F16 || Opc == AMDGPU::V_MAC_F16_e64) { 2029 // Don't fold if we are using source or output modifiers. The new VOP2 2030 // instructions don't have them. 2031 if (hasAnyModifiersSet(UseMI)) 2032 return false; 2033 2034 // If this is a free constant, there's no reason to do this. 2035 // TODO: We could fold this here instead of letting SIFoldOperands do it 2036 // later. 2037 MachineOperand *Src0 = getNamedOperand(UseMI, AMDGPU::OpName::src0); 2038 2039 // Any src operand can be used for the legality check. 2040 if (isInlineConstant(UseMI, *Src0, *ImmOp)) 2041 return false; 2042 2043 bool IsF32 = Opc == AMDGPU::V_MAD_F32 || Opc == AMDGPU::V_MAC_F32_e64; 2044 MachineOperand *Src1 = getNamedOperand(UseMI, AMDGPU::OpName::src1); 2045 MachineOperand *Src2 = getNamedOperand(UseMI, AMDGPU::OpName::src2); 2046 2047 // Multiplied part is the constant: Use v_madmk_{f16, f32}. 2048 // We should only expect these to be on src0 due to canonicalizations. 2049 if (Src0->isReg() && Src0->getReg() == Reg) { 2050 if (!Src1->isReg() || RI.isSGPRClass(MRI->getRegClass(Src1->getReg()))) 2051 return false; 2052 2053 if (!Src2->isReg() || RI.isSGPRClass(MRI->getRegClass(Src2->getReg()))) 2054 return false; 2055 2056 // We need to swap operands 0 and 1 since madmk constant is at operand 1. 2057 2058 const int64_t Imm = ImmOp->getImm(); 2059 2060 // FIXME: This would be a lot easier if we could return a new instruction 2061 // instead of having to modify in place. 2062 2063 // Remove these first since they are at the end. 2064 UseMI.RemoveOperand( 2065 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::omod)); 2066 UseMI.RemoveOperand( 2067 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp)); 2068 2069 unsigned Src1Reg = Src1->getReg(); 2070 unsigned Src1SubReg = Src1->getSubReg(); 2071 Src0->setReg(Src1Reg); 2072 Src0->setSubReg(Src1SubReg); 2073 Src0->setIsKill(Src1->isKill()); 2074 2075 if (Opc == AMDGPU::V_MAC_F32_e64 || 2076 Opc == AMDGPU::V_MAC_F16_e64) 2077 UseMI.untieRegOperand( 2078 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)); 2079 2080 Src1->ChangeToImmediate(Imm); 2081 2082 removeModOperands(UseMI); 2083 UseMI.setDesc(get(IsF32 ? AMDGPU::V_MADMK_F32 : AMDGPU::V_MADMK_F16)); 2084 2085 bool DeleteDef = MRI->hasOneNonDBGUse(Reg); 2086 if (DeleteDef) 2087 DefMI.eraseFromParent(); 2088 2089 return true; 2090 } 2091 2092 // Added part is the constant: Use v_madak_{f16, f32}. 2093 if (Src2->isReg() && Src2->getReg() == Reg) { 2094 // Not allowed to use constant bus for another operand. 2095 // We can however allow an inline immediate as src0. 2096 bool Src0Inlined = false; 2097 if (Src0->isReg()) { 2098 // Try to inline constant if possible. 2099 // If the Def moves immediate and the use is single 2100 // We are saving VGPR here. 2101 MachineInstr *Def = MRI->getUniqueVRegDef(Src0->getReg()); 2102 if (Def && Def->isMoveImmediate() && 2103 isInlineConstant(Def->getOperand(1)) && 2104 MRI->hasOneUse(Src0->getReg())) { 2105 Src0->ChangeToImmediate(Def->getOperand(1).getImm()); 2106 Src0Inlined = true; 2107 } else if ((RI.isPhysicalRegister(Src0->getReg()) && 2108 RI.isSGPRClass(RI.getPhysRegClass(Src0->getReg()))) || 2109 (RI.isVirtualRegister(Src0->getReg()) && 2110 RI.isSGPRClass(MRI->getRegClass(Src0->getReg())))) 2111 return false; 2112 // VGPR is okay as Src0 - fallthrough 2113 } 2114 2115 if (Src1->isReg() && !Src0Inlined ) { 2116 // We have one slot for inlinable constant so far - try to fill it 2117 MachineInstr *Def = MRI->getUniqueVRegDef(Src1->getReg()); 2118 if (Def && Def->isMoveImmediate() && 2119 isInlineConstant(Def->getOperand(1)) && 2120 MRI->hasOneUse(Src1->getReg()) && 2121 commuteInstruction(UseMI)) { 2122 Src0->ChangeToImmediate(Def->getOperand(1).getImm()); 2123 } else if ((RI.isPhysicalRegister(Src1->getReg()) && 2124 RI.isSGPRClass(RI.getPhysRegClass(Src1->getReg()))) || 2125 (RI.isVirtualRegister(Src1->getReg()) && 2126 RI.isSGPRClass(MRI->getRegClass(Src1->getReg())))) 2127 return false; 2128 // VGPR is okay as Src1 - fallthrough 2129 } 2130 2131 const int64_t Imm = ImmOp->getImm(); 2132 2133 // FIXME: This would be a lot easier if we could return a new instruction 2134 // instead of having to modify in place. 2135 2136 // Remove these first since they are at the end. 2137 UseMI.RemoveOperand( 2138 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::omod)); 2139 UseMI.RemoveOperand( 2140 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::clamp)); 2141 2142 if (Opc == AMDGPU::V_MAC_F32_e64 || 2143 Opc == AMDGPU::V_MAC_F16_e64) 2144 UseMI.untieRegOperand( 2145 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2)); 2146 2147 // ChangingToImmediate adds Src2 back to the instruction. 2148 Src2->ChangeToImmediate(Imm); 2149 2150 // These come before src2. 2151 removeModOperands(UseMI); 2152 UseMI.setDesc(get(IsF32 ? AMDGPU::V_MADAK_F32 : AMDGPU::V_MADAK_F16)); 2153 2154 bool DeleteDef = MRI->hasOneNonDBGUse(Reg); 2155 if (DeleteDef) 2156 DefMI.eraseFromParent(); 2157 2158 return true; 2159 } 2160 } 2161 2162 return false; 2163 } 2164 2165 static bool offsetsDoNotOverlap(int WidthA, int OffsetA, 2166 int WidthB, int OffsetB) { 2167 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB; 2168 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA; 2169 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; 2170 return LowOffset + LowWidth <= HighOffset; 2171 } 2172 2173 bool SIInstrInfo::checkInstOffsetsDoNotOverlap(MachineInstr &MIa, 2174 MachineInstr &MIb) const { 2175 MachineOperand *BaseOp0, *BaseOp1; 2176 int64_t Offset0, Offset1; 2177 2178 if (getMemOperandWithOffset(MIa, BaseOp0, Offset0, &RI) && 2179 getMemOperandWithOffset(MIb, BaseOp1, Offset1, &RI)) { 2180 if (!BaseOp0->isIdenticalTo(*BaseOp1)) 2181 return false; 2182 2183 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand()) { 2184 // FIXME: Handle ds_read2 / ds_write2. 2185 return false; 2186 } 2187 unsigned Width0 = (*MIa.memoperands_begin())->getSize(); 2188 unsigned Width1 = (*MIb.memoperands_begin())->getSize(); 2189 if (offsetsDoNotOverlap(Width0, Offset0, Width1, Offset1)) { 2190 return true; 2191 } 2192 } 2193 2194 return false; 2195 } 2196 2197 bool SIInstrInfo::areMemAccessesTriviallyDisjoint(MachineInstr &MIa, 2198 MachineInstr &MIb, 2199 AliasAnalysis *AA) const { 2200 assert((MIa.mayLoad() || MIa.mayStore()) && 2201 "MIa must load from or modify a memory location"); 2202 assert((MIb.mayLoad() || MIb.mayStore()) && 2203 "MIb must load from or modify a memory location"); 2204 2205 if (MIa.hasUnmodeledSideEffects() || MIb.hasUnmodeledSideEffects()) 2206 return false; 2207 2208 // XXX - Can we relax this between address spaces? 2209 if (MIa.hasOrderedMemoryRef() || MIb.hasOrderedMemoryRef()) 2210 return false; 2211 2212 if (AA && MIa.hasOneMemOperand() && MIb.hasOneMemOperand()) { 2213 const MachineMemOperand *MMOa = *MIa.memoperands_begin(); 2214 const MachineMemOperand *MMOb = *MIb.memoperands_begin(); 2215 if (MMOa->getValue() && MMOb->getValue()) { 2216 MemoryLocation LocA(MMOa->getValue(), MMOa->getSize(), MMOa->getAAInfo()); 2217 MemoryLocation LocB(MMOb->getValue(), MMOb->getSize(), MMOb->getAAInfo()); 2218 if (!AA->alias(LocA, LocB)) 2219 return true; 2220 } 2221 } 2222 2223 // TODO: Should we check the address space from the MachineMemOperand? That 2224 // would allow us to distinguish objects we know don't alias based on the 2225 // underlying address space, even if it was lowered to a different one, 2226 // e.g. private accesses lowered to use MUBUF instructions on a scratch 2227 // buffer. 2228 if (isDS(MIa)) { 2229 if (isDS(MIb)) 2230 return checkInstOffsetsDoNotOverlap(MIa, MIb); 2231 2232 return !isFLAT(MIb) || isSegmentSpecificFLAT(MIb); 2233 } 2234 2235 if (isMUBUF(MIa) || isMTBUF(MIa)) { 2236 if (isMUBUF(MIb) || isMTBUF(MIb)) 2237 return checkInstOffsetsDoNotOverlap(MIa, MIb); 2238 2239 return !isFLAT(MIb) && !isSMRD(MIb); 2240 } 2241 2242 if (isSMRD(MIa)) { 2243 if (isSMRD(MIb)) 2244 return checkInstOffsetsDoNotOverlap(MIa, MIb); 2245 2246 return !isFLAT(MIb) && !isMUBUF(MIa) && !isMTBUF(MIa); 2247 } 2248 2249 if (isFLAT(MIa)) { 2250 if (isFLAT(MIb)) 2251 return checkInstOffsetsDoNotOverlap(MIa, MIb); 2252 2253 return false; 2254 } 2255 2256 return false; 2257 } 2258 2259 static int64_t getFoldableImm(const MachineOperand* MO) { 2260 if (!MO->isReg()) 2261 return false; 2262 const MachineFunction *MF = MO->getParent()->getParent()->getParent(); 2263 const MachineRegisterInfo &MRI = MF->getRegInfo(); 2264 auto Def = MRI.getUniqueVRegDef(MO->getReg()); 2265 if (Def && Def->getOpcode() == AMDGPU::V_MOV_B32_e32 && 2266 Def->getOperand(1).isImm()) 2267 return Def->getOperand(1).getImm(); 2268 return AMDGPU::NoRegister; 2269 } 2270 2271 MachineInstr *SIInstrInfo::convertToThreeAddress(MachineFunction::iterator &MBB, 2272 MachineInstr &MI, 2273 LiveVariables *LV) const { 2274 unsigned Opc = MI.getOpcode(); 2275 bool IsF16 = false; 2276 bool IsFMA = Opc == AMDGPU::V_FMAC_F32_e32 || Opc == AMDGPU::V_FMAC_F32_e64; 2277 2278 switch (Opc) { 2279 default: 2280 return nullptr; 2281 case AMDGPU::V_MAC_F16_e64: 2282 IsF16 = true; 2283 LLVM_FALLTHROUGH; 2284 case AMDGPU::V_MAC_F32_e64: 2285 case AMDGPU::V_FMAC_F32_e64: 2286 break; 2287 case AMDGPU::V_MAC_F16_e32: 2288 IsF16 = true; 2289 LLVM_FALLTHROUGH; 2290 case AMDGPU::V_MAC_F32_e32: 2291 case AMDGPU::V_FMAC_F32_e32: { 2292 int Src0Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), 2293 AMDGPU::OpName::src0); 2294 const MachineOperand *Src0 = &MI.getOperand(Src0Idx); 2295 if (!Src0->isReg() && !Src0->isImm()) 2296 return nullptr; 2297 2298 if (Src0->isImm() && !isInlineConstant(MI, Src0Idx, *Src0)) 2299 return nullptr; 2300 2301 break; 2302 } 2303 } 2304 2305 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 2306 const MachineOperand *Src0 = getNamedOperand(MI, AMDGPU::OpName::src0); 2307 const MachineOperand *Src0Mods = 2308 getNamedOperand(MI, AMDGPU::OpName::src0_modifiers); 2309 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1); 2310 const MachineOperand *Src1Mods = 2311 getNamedOperand(MI, AMDGPU::OpName::src1_modifiers); 2312 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2); 2313 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp); 2314 const MachineOperand *Omod = getNamedOperand(MI, AMDGPU::OpName::omod); 2315 2316 if (!IsFMA && !Src0Mods && !Src1Mods && !Clamp && !Omod && 2317 // If we have an SGPR input, we will violate the constant bus restriction. 2318 (!Src0->isReg() || !RI.isSGPRReg(MBB->getParent()->getRegInfo(), Src0->getReg()))) { 2319 if (auto Imm = getFoldableImm(Src2)) { 2320 return BuildMI(*MBB, MI, MI.getDebugLoc(), 2321 get(IsF16 ? AMDGPU::V_MADAK_F16 : AMDGPU::V_MADAK_F32)) 2322 .add(*Dst) 2323 .add(*Src0) 2324 .add(*Src1) 2325 .addImm(Imm); 2326 } 2327 if (auto Imm = getFoldableImm(Src1)) { 2328 return BuildMI(*MBB, MI, MI.getDebugLoc(), 2329 get(IsF16 ? AMDGPU::V_MADMK_F16 : AMDGPU::V_MADMK_F32)) 2330 .add(*Dst) 2331 .add(*Src0) 2332 .addImm(Imm) 2333 .add(*Src2); 2334 } 2335 if (auto Imm = getFoldableImm(Src0)) { 2336 if (isOperandLegal(MI, AMDGPU::getNamedOperandIdx(AMDGPU::V_MADMK_F32, 2337 AMDGPU::OpName::src0), Src1)) 2338 return BuildMI(*MBB, MI, MI.getDebugLoc(), 2339 get(IsF16 ? AMDGPU::V_MADMK_F16 : AMDGPU::V_MADMK_F32)) 2340 .add(*Dst) 2341 .add(*Src1) 2342 .addImm(Imm) 2343 .add(*Src2); 2344 } 2345 } 2346 2347 assert((!IsFMA || !IsF16) && "fmac only expected with f32"); 2348 unsigned NewOpc = IsFMA ? AMDGPU::V_FMA_F32 : 2349 (IsF16 ? AMDGPU::V_MAD_F16 : AMDGPU::V_MAD_F32); 2350 return BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpc)) 2351 .add(*Dst) 2352 .addImm(Src0Mods ? Src0Mods->getImm() : 0) 2353 .add(*Src0) 2354 .addImm(Src1Mods ? Src1Mods->getImm() : 0) 2355 .add(*Src1) 2356 .addImm(0) // Src mods 2357 .add(*Src2) 2358 .addImm(Clamp ? Clamp->getImm() : 0) 2359 .addImm(Omod ? Omod->getImm() : 0); 2360 } 2361 2362 // It's not generally safe to move VALU instructions across these since it will 2363 // start using the register as a base index rather than directly. 2364 // XXX - Why isn't hasSideEffects sufficient for these? 2365 static bool changesVGPRIndexingMode(const MachineInstr &MI) { 2366 switch (MI.getOpcode()) { 2367 case AMDGPU::S_SET_GPR_IDX_ON: 2368 case AMDGPU::S_SET_GPR_IDX_MODE: 2369 case AMDGPU::S_SET_GPR_IDX_OFF: 2370 return true; 2371 default: 2372 return false; 2373 } 2374 } 2375 2376 bool SIInstrInfo::isSchedulingBoundary(const MachineInstr &MI, 2377 const MachineBasicBlock *MBB, 2378 const MachineFunction &MF) const { 2379 // XXX - Do we want the SP check in the base implementation? 2380 2381 // Target-independent instructions do not have an implicit-use of EXEC, even 2382 // when they operate on VGPRs. Treating EXEC modifications as scheduling 2383 // boundaries prevents incorrect movements of such instructions. 2384 return TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF) || 2385 MI.modifiesRegister(AMDGPU::EXEC, &RI) || 2386 MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 || 2387 MI.getOpcode() == AMDGPU::S_SETREG_B32 || 2388 changesVGPRIndexingMode(MI); 2389 } 2390 2391 bool SIInstrInfo::isAlwaysGDS(uint16_t Opcode) const { 2392 return Opcode == AMDGPU::DS_ORDERED_COUNT || 2393 Opcode == AMDGPU::DS_GWS_INIT || 2394 Opcode == AMDGPU::DS_GWS_SEMA_V || 2395 Opcode == AMDGPU::DS_GWS_SEMA_BR || 2396 Opcode == AMDGPU::DS_GWS_SEMA_P || 2397 Opcode == AMDGPU::DS_GWS_SEMA_RELEASE_ALL || 2398 Opcode == AMDGPU::DS_GWS_BARRIER; 2399 } 2400 2401 bool SIInstrInfo::hasUnwantedEffectsWhenEXECEmpty(const MachineInstr &MI) const { 2402 unsigned Opcode = MI.getOpcode(); 2403 2404 if (MI.mayStore() && isSMRD(MI)) 2405 return true; // scalar store or atomic 2406 2407 // These instructions cause shader I/O that may cause hardware lockups 2408 // when executed with an empty EXEC mask. 2409 // 2410 // Note: exp with VM = DONE = 0 is automatically skipped by hardware when 2411 // EXEC = 0, but checking for that case here seems not worth it 2412 // given the typical code patterns. 2413 if (Opcode == AMDGPU::S_SENDMSG || Opcode == AMDGPU::S_SENDMSGHALT || 2414 Opcode == AMDGPU::EXP || Opcode == AMDGPU::EXP_DONE || 2415 Opcode == AMDGPU::DS_ORDERED_COUNT) 2416 return true; 2417 2418 if (MI.isInlineAsm()) 2419 return true; // conservative assumption 2420 2421 // These are like SALU instructions in terms of effects, so it's questionable 2422 // whether we should return true for those. 2423 // 2424 // However, executing them with EXEC = 0 causes them to operate on undefined 2425 // data, which we avoid by returning true here. 2426 if (Opcode == AMDGPU::V_READFIRSTLANE_B32 || Opcode == AMDGPU::V_READLANE_B32) 2427 return true; 2428 2429 return false; 2430 } 2431 2432 bool SIInstrInfo::isInlineConstant(const APInt &Imm) const { 2433 switch (Imm.getBitWidth()) { 2434 case 32: 2435 return AMDGPU::isInlinableLiteral32(Imm.getSExtValue(), 2436 ST.hasInv2PiInlineImm()); 2437 case 64: 2438 return AMDGPU::isInlinableLiteral64(Imm.getSExtValue(), 2439 ST.hasInv2PiInlineImm()); 2440 case 16: 2441 return ST.has16BitInsts() && 2442 AMDGPU::isInlinableLiteral16(Imm.getSExtValue(), 2443 ST.hasInv2PiInlineImm()); 2444 default: 2445 llvm_unreachable("invalid bitwidth"); 2446 } 2447 } 2448 2449 bool SIInstrInfo::isInlineConstant(const MachineOperand &MO, 2450 uint8_t OperandType) const { 2451 if (!MO.isImm() || 2452 OperandType < AMDGPU::OPERAND_SRC_FIRST || 2453 OperandType > AMDGPU::OPERAND_SRC_LAST) 2454 return false; 2455 2456 // MachineOperand provides no way to tell the true operand size, since it only 2457 // records a 64-bit value. We need to know the size to determine if a 32-bit 2458 // floating point immediate bit pattern is legal for an integer immediate. It 2459 // would be for any 32-bit integer operand, but would not be for a 64-bit one. 2460 2461 int64_t Imm = MO.getImm(); 2462 switch (OperandType) { 2463 case AMDGPU::OPERAND_REG_IMM_INT32: 2464 case AMDGPU::OPERAND_REG_IMM_FP32: 2465 case AMDGPU::OPERAND_REG_INLINE_C_INT32: 2466 case AMDGPU::OPERAND_REG_INLINE_C_FP32: { 2467 int32_t Trunc = static_cast<int32_t>(Imm); 2468 return AMDGPU::isInlinableLiteral32(Trunc, ST.hasInv2PiInlineImm()); 2469 } 2470 case AMDGPU::OPERAND_REG_IMM_INT64: 2471 case AMDGPU::OPERAND_REG_IMM_FP64: 2472 case AMDGPU::OPERAND_REG_INLINE_C_INT64: 2473 case AMDGPU::OPERAND_REG_INLINE_C_FP64: 2474 return AMDGPU::isInlinableLiteral64(MO.getImm(), 2475 ST.hasInv2PiInlineImm()); 2476 case AMDGPU::OPERAND_REG_IMM_INT16: 2477 case AMDGPU::OPERAND_REG_IMM_FP16: 2478 case AMDGPU::OPERAND_REG_INLINE_C_INT16: 2479 case AMDGPU::OPERAND_REG_INLINE_C_FP16: { 2480 if (isInt<16>(Imm) || isUInt<16>(Imm)) { 2481 // A few special case instructions have 16-bit operands on subtargets 2482 // where 16-bit instructions are not legal. 2483 // TODO: Do the 32-bit immediates work? We shouldn't really need to handle 2484 // constants in these cases 2485 int16_t Trunc = static_cast<int16_t>(Imm); 2486 return ST.has16BitInsts() && 2487 AMDGPU::isInlinableLiteral16(Trunc, ST.hasInv2PiInlineImm()); 2488 } 2489 2490 return false; 2491 } 2492 case AMDGPU::OPERAND_REG_INLINE_C_V2INT16: 2493 case AMDGPU::OPERAND_REG_INLINE_C_V2FP16: { 2494 if (isUInt<16>(Imm)) { 2495 int16_t Trunc = static_cast<int16_t>(Imm); 2496 return ST.has16BitInsts() && 2497 AMDGPU::isInlinableLiteral16(Trunc, ST.hasInv2PiInlineImm()); 2498 } 2499 if (!(Imm & 0xffff)) { 2500 return ST.has16BitInsts() && 2501 AMDGPU::isInlinableLiteral16(Imm >> 16, ST.hasInv2PiInlineImm()); 2502 } 2503 uint32_t Trunc = static_cast<uint32_t>(Imm); 2504 return AMDGPU::isInlinableLiteralV216(Trunc, ST.hasInv2PiInlineImm()); 2505 } 2506 default: 2507 llvm_unreachable("invalid bitwidth"); 2508 } 2509 } 2510 2511 bool SIInstrInfo::isLiteralConstantLike(const MachineOperand &MO, 2512 const MCOperandInfo &OpInfo) const { 2513 switch (MO.getType()) { 2514 case MachineOperand::MO_Register: 2515 return false; 2516 case MachineOperand::MO_Immediate: 2517 return !isInlineConstant(MO, OpInfo); 2518 case MachineOperand::MO_FrameIndex: 2519 case MachineOperand::MO_MachineBasicBlock: 2520 case MachineOperand::MO_ExternalSymbol: 2521 case MachineOperand::MO_GlobalAddress: 2522 case MachineOperand::MO_MCSymbol: 2523 return true; 2524 default: 2525 llvm_unreachable("unexpected operand type"); 2526 } 2527 } 2528 2529 static bool compareMachineOp(const MachineOperand &Op0, 2530 const MachineOperand &Op1) { 2531 if (Op0.getType() != Op1.getType()) 2532 return false; 2533 2534 switch (Op0.getType()) { 2535 case MachineOperand::MO_Register: 2536 return Op0.getReg() == Op1.getReg(); 2537 case MachineOperand::MO_Immediate: 2538 return Op0.getImm() == Op1.getImm(); 2539 default: 2540 llvm_unreachable("Didn't expect to be comparing these operand types"); 2541 } 2542 } 2543 2544 bool SIInstrInfo::isImmOperandLegal(const MachineInstr &MI, unsigned OpNo, 2545 const MachineOperand &MO) const { 2546 const MCOperandInfo &OpInfo = get(MI.getOpcode()).OpInfo[OpNo]; 2547 2548 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI()); 2549 2550 if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE) 2551 return true; 2552 2553 if (OpInfo.RegClass < 0) 2554 return false; 2555 2556 if (MO.isImm() && isInlineConstant(MO, OpInfo)) 2557 return RI.opCanUseInlineConstant(OpInfo.OperandType); 2558 2559 return RI.opCanUseLiteralConstant(OpInfo.OperandType); 2560 } 2561 2562 bool SIInstrInfo::hasVALU32BitEncoding(unsigned Opcode) const { 2563 int Op32 = AMDGPU::getVOPe32(Opcode); 2564 if (Op32 == -1) 2565 return false; 2566 2567 return pseudoToMCOpcode(Op32) != -1; 2568 } 2569 2570 bool SIInstrInfo::hasModifiers(unsigned Opcode) const { 2571 // The src0_modifier operand is present on all instructions 2572 // that have modifiers. 2573 2574 return AMDGPU::getNamedOperandIdx(Opcode, 2575 AMDGPU::OpName::src0_modifiers) != -1; 2576 } 2577 2578 bool SIInstrInfo::hasModifiersSet(const MachineInstr &MI, 2579 unsigned OpName) const { 2580 const MachineOperand *Mods = getNamedOperand(MI, OpName); 2581 return Mods && Mods->getImm(); 2582 } 2583 2584 bool SIInstrInfo::hasAnyModifiersSet(const MachineInstr &MI) const { 2585 return hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers) || 2586 hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers) || 2587 hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers) || 2588 hasModifiersSet(MI, AMDGPU::OpName::clamp) || 2589 hasModifiersSet(MI, AMDGPU::OpName::omod); 2590 } 2591 2592 bool SIInstrInfo::canShrink(const MachineInstr &MI, 2593 const MachineRegisterInfo &MRI) const { 2594 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2); 2595 // Can't shrink instruction with three operands. 2596 // FIXME: v_cndmask_b32 has 3 operands and is shrinkable, but we need to add 2597 // a special case for it. It can only be shrunk if the third operand 2598 // is vcc. We should handle this the same way we handle vopc, by addding 2599 // a register allocation hint pre-regalloc and then do the shrinking 2600 // post-regalloc. 2601 if (Src2) { 2602 switch (MI.getOpcode()) { 2603 default: return false; 2604 2605 case AMDGPU::V_ADDC_U32_e64: 2606 case AMDGPU::V_SUBB_U32_e64: 2607 case AMDGPU::V_SUBBREV_U32_e64: { 2608 const MachineOperand *Src1 2609 = getNamedOperand(MI, AMDGPU::OpName::src1); 2610 if (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg())) 2611 return false; 2612 // Additional verification is needed for sdst/src2. 2613 return true; 2614 } 2615 case AMDGPU::V_MAC_F32_e64: 2616 case AMDGPU::V_MAC_F16_e64: 2617 case AMDGPU::V_FMAC_F32_e64: 2618 if (!Src2->isReg() || !RI.isVGPR(MRI, Src2->getReg()) || 2619 hasModifiersSet(MI, AMDGPU::OpName::src2_modifiers)) 2620 return false; 2621 break; 2622 2623 case AMDGPU::V_CNDMASK_B32_e64: 2624 break; 2625 } 2626 } 2627 2628 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1); 2629 if (Src1 && (!Src1->isReg() || !RI.isVGPR(MRI, Src1->getReg()) || 2630 hasModifiersSet(MI, AMDGPU::OpName::src1_modifiers))) 2631 return false; 2632 2633 // We don't need to check src0, all input types are legal, so just make sure 2634 // src0 isn't using any modifiers. 2635 if (hasModifiersSet(MI, AMDGPU::OpName::src0_modifiers)) 2636 return false; 2637 2638 // Can it be shrunk to a valid 32 bit opcode? 2639 if (!hasVALU32BitEncoding(MI.getOpcode())) 2640 return false; 2641 2642 // Check output modifiers 2643 return !hasModifiersSet(MI, AMDGPU::OpName::omod) && 2644 !hasModifiersSet(MI, AMDGPU::OpName::clamp); 2645 } 2646 2647 // Set VCC operand with all flags from \p Orig, except for setting it as 2648 // implicit. 2649 static void copyFlagsToImplicitVCC(MachineInstr &MI, 2650 const MachineOperand &Orig) { 2651 2652 for (MachineOperand &Use : MI.implicit_operands()) { 2653 if (Use.isUse() && Use.getReg() == AMDGPU::VCC) { 2654 Use.setIsUndef(Orig.isUndef()); 2655 Use.setIsKill(Orig.isKill()); 2656 return; 2657 } 2658 } 2659 } 2660 2661 MachineInstr *SIInstrInfo::buildShrunkInst(MachineInstr &MI, 2662 unsigned Op32) const { 2663 MachineBasicBlock *MBB = MI.getParent();; 2664 MachineInstrBuilder Inst32 = 2665 BuildMI(*MBB, MI, MI.getDebugLoc(), get(Op32)); 2666 2667 // Add the dst operand if the 32-bit encoding also has an explicit $vdst. 2668 // For VOPC instructions, this is replaced by an implicit def of vcc. 2669 int Op32DstIdx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::vdst); 2670 if (Op32DstIdx != -1) { 2671 // dst 2672 Inst32.add(MI.getOperand(0)); 2673 } else { 2674 assert(MI.getOperand(0).getReg() == AMDGPU::VCC && 2675 "Unexpected case"); 2676 } 2677 2678 Inst32.add(*getNamedOperand(MI, AMDGPU::OpName::src0)); 2679 2680 const MachineOperand *Src1 = getNamedOperand(MI, AMDGPU::OpName::src1); 2681 if (Src1) 2682 Inst32.add(*Src1); 2683 2684 const MachineOperand *Src2 = getNamedOperand(MI, AMDGPU::OpName::src2); 2685 2686 if (Src2) { 2687 int Op32Src2Idx = AMDGPU::getNamedOperandIdx(Op32, AMDGPU::OpName::src2); 2688 if (Op32Src2Idx != -1) { 2689 Inst32.add(*Src2); 2690 } else { 2691 // In the case of V_CNDMASK_B32_e32, the explicit operand src2 is 2692 // replaced with an implicit read of vcc. This was already added 2693 // during the initial BuildMI, so find it to preserve the flags. 2694 copyFlagsToImplicitVCC(*Inst32, *Src2); 2695 } 2696 } 2697 2698 return Inst32; 2699 } 2700 2701 bool SIInstrInfo::usesConstantBus(const MachineRegisterInfo &MRI, 2702 const MachineOperand &MO, 2703 const MCOperandInfo &OpInfo) const { 2704 // Literal constants use the constant bus. 2705 //if (isLiteralConstantLike(MO, OpInfo)) 2706 // return true; 2707 if (MO.isImm()) 2708 return !isInlineConstant(MO, OpInfo); 2709 2710 if (!MO.isReg()) 2711 return true; // Misc other operands like FrameIndex 2712 2713 if (!MO.isUse()) 2714 return false; 2715 2716 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) 2717 return RI.isSGPRClass(MRI.getRegClass(MO.getReg())); 2718 2719 // FLAT_SCR is just an SGPR pair. 2720 if (!MO.isImplicit() && (MO.getReg() == AMDGPU::FLAT_SCR)) 2721 return true; 2722 2723 // EXEC register uses the constant bus. 2724 if (!MO.isImplicit() && MO.getReg() == AMDGPU::EXEC) 2725 return true; 2726 2727 // SGPRs use the constant bus 2728 return (MO.getReg() == AMDGPU::VCC || MO.getReg() == AMDGPU::M0 || 2729 (!MO.isImplicit() && 2730 (AMDGPU::SGPR_32RegClass.contains(MO.getReg()) || 2731 AMDGPU::SGPR_64RegClass.contains(MO.getReg())))); 2732 } 2733 2734 static unsigned findImplicitSGPRRead(const MachineInstr &MI) { 2735 for (const MachineOperand &MO : MI.implicit_operands()) { 2736 // We only care about reads. 2737 if (MO.isDef()) 2738 continue; 2739 2740 switch (MO.getReg()) { 2741 case AMDGPU::VCC: 2742 case AMDGPU::M0: 2743 case AMDGPU::FLAT_SCR: 2744 return MO.getReg(); 2745 2746 default: 2747 break; 2748 } 2749 } 2750 2751 return AMDGPU::NoRegister; 2752 } 2753 2754 static bool shouldReadExec(const MachineInstr &MI) { 2755 if (SIInstrInfo::isVALU(MI)) { 2756 switch (MI.getOpcode()) { 2757 case AMDGPU::V_READLANE_B32: 2758 case AMDGPU::V_READLANE_B32_si: 2759 case AMDGPU::V_READLANE_B32_vi: 2760 case AMDGPU::V_WRITELANE_B32: 2761 case AMDGPU::V_WRITELANE_B32_si: 2762 case AMDGPU::V_WRITELANE_B32_vi: 2763 return false; 2764 } 2765 2766 return true; 2767 } 2768 2769 if (SIInstrInfo::isGenericOpcode(MI.getOpcode()) || 2770 SIInstrInfo::isSALU(MI) || 2771 SIInstrInfo::isSMRD(MI)) 2772 return false; 2773 2774 return true; 2775 } 2776 2777 static bool isSubRegOf(const SIRegisterInfo &TRI, 2778 const MachineOperand &SuperVec, 2779 const MachineOperand &SubReg) { 2780 if (TargetRegisterInfo::isPhysicalRegister(SubReg.getReg())) 2781 return TRI.isSubRegister(SuperVec.getReg(), SubReg.getReg()); 2782 2783 return SubReg.getSubReg() != AMDGPU::NoSubRegister && 2784 SubReg.getReg() == SuperVec.getReg(); 2785 } 2786 2787 bool SIInstrInfo::verifyInstruction(const MachineInstr &MI, 2788 StringRef &ErrInfo) const { 2789 uint16_t Opcode = MI.getOpcode(); 2790 if (SIInstrInfo::isGenericOpcode(MI.getOpcode())) 2791 return true; 2792 2793 const MachineFunction *MF = MI.getParent()->getParent(); 2794 const MachineRegisterInfo &MRI = MF->getRegInfo(); 2795 2796 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0); 2797 int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1); 2798 int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2); 2799 2800 // Make sure the number of operands is correct. 2801 const MCInstrDesc &Desc = get(Opcode); 2802 if (!Desc.isVariadic() && 2803 Desc.getNumOperands() != MI.getNumExplicitOperands()) { 2804 ErrInfo = "Instruction has wrong number of operands."; 2805 return false; 2806 } 2807 2808 if (MI.isInlineAsm()) { 2809 // Verify register classes for inlineasm constraints. 2810 for (unsigned I = InlineAsm::MIOp_FirstOperand, E = MI.getNumOperands(); 2811 I != E; ++I) { 2812 const TargetRegisterClass *RC = MI.getRegClassConstraint(I, this, &RI); 2813 if (!RC) 2814 continue; 2815 2816 const MachineOperand &Op = MI.getOperand(I); 2817 if (!Op.isReg()) 2818 continue; 2819 2820 unsigned Reg = Op.getReg(); 2821 if (!TargetRegisterInfo::isVirtualRegister(Reg) && !RC->contains(Reg)) { 2822 ErrInfo = "inlineasm operand has incorrect register class."; 2823 return false; 2824 } 2825 } 2826 2827 return true; 2828 } 2829 2830 // Make sure the register classes are correct. 2831 for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) { 2832 if (MI.getOperand(i).isFPImm()) { 2833 ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast " 2834 "all fp values to integers."; 2835 return false; 2836 } 2837 2838 int RegClass = Desc.OpInfo[i].RegClass; 2839 2840 switch (Desc.OpInfo[i].OperandType) { 2841 case MCOI::OPERAND_REGISTER: 2842 if (MI.getOperand(i).isImm()) { 2843 ErrInfo = "Illegal immediate value for operand."; 2844 return false; 2845 } 2846 break; 2847 case AMDGPU::OPERAND_REG_IMM_INT32: 2848 case AMDGPU::OPERAND_REG_IMM_FP32: 2849 break; 2850 case AMDGPU::OPERAND_REG_INLINE_C_INT32: 2851 case AMDGPU::OPERAND_REG_INLINE_C_FP32: 2852 case AMDGPU::OPERAND_REG_INLINE_C_INT64: 2853 case AMDGPU::OPERAND_REG_INLINE_C_FP64: 2854 case AMDGPU::OPERAND_REG_INLINE_C_INT16: 2855 case AMDGPU::OPERAND_REG_INLINE_C_FP16: { 2856 const MachineOperand &MO = MI.getOperand(i); 2857 if (!MO.isReg() && (!MO.isImm() || !isInlineConstant(MI, i))) { 2858 ErrInfo = "Illegal immediate value for operand."; 2859 return false; 2860 } 2861 break; 2862 } 2863 case MCOI::OPERAND_IMMEDIATE: 2864 case AMDGPU::OPERAND_KIMM32: 2865 // Check if this operand is an immediate. 2866 // FrameIndex operands will be replaced by immediates, so they are 2867 // allowed. 2868 if (!MI.getOperand(i).isImm() && !MI.getOperand(i).isFI()) { 2869 ErrInfo = "Expected immediate, but got non-immediate"; 2870 return false; 2871 } 2872 LLVM_FALLTHROUGH; 2873 default: 2874 continue; 2875 } 2876 2877 if (!MI.getOperand(i).isReg()) 2878 continue; 2879 2880 if (RegClass != -1) { 2881 unsigned Reg = MI.getOperand(i).getReg(); 2882 if (Reg == AMDGPU::NoRegister || 2883 TargetRegisterInfo::isVirtualRegister(Reg)) 2884 continue; 2885 2886 const TargetRegisterClass *RC = RI.getRegClass(RegClass); 2887 if (!RC->contains(Reg)) { 2888 ErrInfo = "Operand has incorrect register class."; 2889 return false; 2890 } 2891 } 2892 } 2893 2894 // Verify SDWA 2895 if (isSDWA(MI)) { 2896 if (!ST.hasSDWA()) { 2897 ErrInfo = "SDWA is not supported on this target"; 2898 return false; 2899 } 2900 2901 int DstIdx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::vdst); 2902 2903 const int OpIndicies[] = { DstIdx, Src0Idx, Src1Idx, Src2Idx }; 2904 2905 for (int OpIdx: OpIndicies) { 2906 if (OpIdx == -1) 2907 continue; 2908 const MachineOperand &MO = MI.getOperand(OpIdx); 2909 2910 if (!ST.hasSDWAScalar()) { 2911 // Only VGPRS on VI 2912 if (!MO.isReg() || !RI.hasVGPRs(RI.getRegClassForReg(MRI, MO.getReg()))) { 2913 ErrInfo = "Only VGPRs allowed as operands in SDWA instructions on VI"; 2914 return false; 2915 } 2916 } else { 2917 // No immediates on GFX9 2918 if (!MO.isReg()) { 2919 ErrInfo = "Only reg allowed as operands in SDWA instructions on GFX9"; 2920 return false; 2921 } 2922 } 2923 } 2924 2925 if (!ST.hasSDWAOmod()) { 2926 // No omod allowed on VI 2927 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod); 2928 if (OMod != nullptr && 2929 (!OMod->isImm() || OMod->getImm() != 0)) { 2930 ErrInfo = "OMod not allowed in SDWA instructions on VI"; 2931 return false; 2932 } 2933 } 2934 2935 uint16_t BasicOpcode = AMDGPU::getBasicFromSDWAOp(Opcode); 2936 if (isVOPC(BasicOpcode)) { 2937 if (!ST.hasSDWASdst() && DstIdx != -1) { 2938 // Only vcc allowed as dst on VI for VOPC 2939 const MachineOperand &Dst = MI.getOperand(DstIdx); 2940 if (!Dst.isReg() || Dst.getReg() != AMDGPU::VCC) { 2941 ErrInfo = "Only VCC allowed as dst in SDWA instructions on VI"; 2942 return false; 2943 } 2944 } else if (!ST.hasSDWAOutModsVOPC()) { 2945 // No clamp allowed on GFX9 for VOPC 2946 const MachineOperand *Clamp = getNamedOperand(MI, AMDGPU::OpName::clamp); 2947 if (Clamp && (!Clamp->isImm() || Clamp->getImm() != 0)) { 2948 ErrInfo = "Clamp not allowed in VOPC SDWA instructions on VI"; 2949 return false; 2950 } 2951 2952 // No omod allowed on GFX9 for VOPC 2953 const MachineOperand *OMod = getNamedOperand(MI, AMDGPU::OpName::omod); 2954 if (OMod && (!OMod->isImm() || OMod->getImm() != 0)) { 2955 ErrInfo = "OMod not allowed in VOPC SDWA instructions on VI"; 2956 return false; 2957 } 2958 } 2959 } 2960 2961 const MachineOperand *DstUnused = getNamedOperand(MI, AMDGPU::OpName::dst_unused); 2962 if (DstUnused && DstUnused->isImm() && 2963 DstUnused->getImm() == AMDGPU::SDWA::UNUSED_PRESERVE) { 2964 const MachineOperand &Dst = MI.getOperand(DstIdx); 2965 if (!Dst.isReg() || !Dst.isTied()) { 2966 ErrInfo = "Dst register should have tied register"; 2967 return false; 2968 } 2969 2970 const MachineOperand &TiedMO = 2971 MI.getOperand(MI.findTiedOperandIdx(DstIdx)); 2972 if (!TiedMO.isReg() || !TiedMO.isImplicit() || !TiedMO.isUse()) { 2973 ErrInfo = 2974 "Dst register should be tied to implicit use of preserved register"; 2975 return false; 2976 } else if (TargetRegisterInfo::isPhysicalRegister(TiedMO.getReg()) && 2977 Dst.getReg() != TiedMO.getReg()) { 2978 ErrInfo = "Dst register should use same physical register as preserved"; 2979 return false; 2980 } 2981 } 2982 } 2983 2984 // Verify MIMG 2985 if (isMIMG(MI.getOpcode()) && !MI.mayStore()) { 2986 // Ensure that the return type used is large enough for all the options 2987 // being used TFE/LWE require an extra result register. 2988 const MachineOperand *DMask = getNamedOperand(MI, AMDGPU::OpName::dmask); 2989 if (DMask) { 2990 uint64_t DMaskImm = DMask->getImm(); 2991 uint32_t RegCount = 2992 isGather4(MI.getOpcode()) ? 4 : countPopulation(DMaskImm); 2993 const MachineOperand *TFE = getNamedOperand(MI, AMDGPU::OpName::tfe); 2994 const MachineOperand *LWE = getNamedOperand(MI, AMDGPU::OpName::lwe); 2995 const MachineOperand *D16 = getNamedOperand(MI, AMDGPU::OpName::d16); 2996 2997 // Adjust for packed 16 bit values 2998 if (D16 && D16->getImm() && !ST.hasUnpackedD16VMem()) 2999 RegCount >>= 1; 3000 3001 // Adjust if using LWE or TFE 3002 if ((LWE && LWE->getImm()) || (TFE && TFE->getImm())) 3003 RegCount += 1; 3004 3005 const uint32_t DstIdx = 3006 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::vdata); 3007 const MachineOperand &Dst = MI.getOperand(DstIdx); 3008 if (Dst.isReg()) { 3009 const TargetRegisterClass *DstRC = getOpRegClass(MI, DstIdx); 3010 uint32_t DstSize = RI.getRegSizeInBits(*DstRC) / 32; 3011 if (RegCount > DstSize) { 3012 ErrInfo = "MIMG instruction returns too many registers for dst " 3013 "register class"; 3014 return false; 3015 } 3016 } 3017 } 3018 } 3019 3020 // Verify VOP*. Ignore multiple sgpr operands on writelane. 3021 if (Desc.getOpcode() != AMDGPU::V_WRITELANE_B32 3022 && (isVOP1(MI) || isVOP2(MI) || isVOP3(MI) || isVOPC(MI) || isSDWA(MI))) { 3023 // Only look at the true operands. Only a real operand can use the constant 3024 // bus, and we don't want to check pseudo-operands like the source modifier 3025 // flags. 3026 const int OpIndices[] = { Src0Idx, Src1Idx, Src2Idx }; 3027 3028 unsigned ConstantBusCount = 0; 3029 unsigned LiteralCount = 0; 3030 3031 if (AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm) != -1) 3032 ++ConstantBusCount; 3033 3034 unsigned SGPRUsed = findImplicitSGPRRead(MI); 3035 if (SGPRUsed != AMDGPU::NoRegister) 3036 ++ConstantBusCount; 3037 3038 for (int OpIdx : OpIndices) { 3039 if (OpIdx == -1) 3040 break; 3041 const MachineOperand &MO = MI.getOperand(OpIdx); 3042 if (usesConstantBus(MRI, MO, MI.getDesc().OpInfo[OpIdx])) { 3043 if (MO.isReg()) { 3044 if (MO.getReg() != SGPRUsed) 3045 ++ConstantBusCount; 3046 SGPRUsed = MO.getReg(); 3047 } else { 3048 ++ConstantBusCount; 3049 ++LiteralCount; 3050 } 3051 } 3052 } 3053 if (ConstantBusCount > 1) { 3054 ErrInfo = "VOP* instruction uses the constant bus more than once"; 3055 return false; 3056 } 3057 3058 if (isVOP3(MI) && LiteralCount) { 3059 ErrInfo = "VOP3 instruction uses literal"; 3060 return false; 3061 } 3062 } 3063 3064 // Verify misc. restrictions on specific instructions. 3065 if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32 || 3066 Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64) { 3067 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 3068 const MachineOperand &Src1 = MI.getOperand(Src1Idx); 3069 const MachineOperand &Src2 = MI.getOperand(Src2Idx); 3070 if (Src0.isReg() && Src1.isReg() && Src2.isReg()) { 3071 if (!compareMachineOp(Src0, Src1) && 3072 !compareMachineOp(Src0, Src2)) { 3073 ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2"; 3074 return false; 3075 } 3076 } 3077 } 3078 3079 if (isSOPK(MI)) { 3080 int64_t Imm = getNamedOperand(MI, AMDGPU::OpName::simm16)->getImm(); 3081 if (sopkIsZext(MI)) { 3082 if (!isUInt<16>(Imm)) { 3083 ErrInfo = "invalid immediate for SOPK instruction"; 3084 return false; 3085 } 3086 } else { 3087 if (!isInt<16>(Imm)) { 3088 ErrInfo = "invalid immediate for SOPK instruction"; 3089 return false; 3090 } 3091 } 3092 } 3093 3094 if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 || 3095 Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 || 3096 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 3097 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) { 3098 const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 3099 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64; 3100 3101 const unsigned StaticNumOps = Desc.getNumOperands() + 3102 Desc.getNumImplicitUses(); 3103 const unsigned NumImplicitOps = IsDst ? 2 : 1; 3104 3105 // Allow additional implicit operands. This allows a fixup done by the post 3106 // RA scheduler where the main implicit operand is killed and implicit-defs 3107 // are added for sub-registers that remain live after this instruction. 3108 if (MI.getNumOperands() < StaticNumOps + NumImplicitOps) { 3109 ErrInfo = "missing implicit register operands"; 3110 return false; 3111 } 3112 3113 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 3114 if (IsDst) { 3115 if (!Dst->isUse()) { 3116 ErrInfo = "v_movreld_b32 vdst should be a use operand"; 3117 return false; 3118 } 3119 3120 unsigned UseOpIdx; 3121 if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) || 3122 UseOpIdx != StaticNumOps + 1) { 3123 ErrInfo = "movrel implicit operands should be tied"; 3124 return false; 3125 } 3126 } 3127 3128 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 3129 const MachineOperand &ImpUse 3130 = MI.getOperand(StaticNumOps + NumImplicitOps - 1); 3131 if (!ImpUse.isReg() || !ImpUse.isUse() || 3132 !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) { 3133 ErrInfo = "src0 should be subreg of implicit vector use"; 3134 return false; 3135 } 3136 } 3137 3138 // Make sure we aren't losing exec uses in the td files. This mostly requires 3139 // being careful when using let Uses to try to add other use registers. 3140 if (shouldReadExec(MI)) { 3141 if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) { 3142 ErrInfo = "VALU instruction does not implicitly read exec mask"; 3143 return false; 3144 } 3145 } 3146 3147 if (isSMRD(MI)) { 3148 if (MI.mayStore()) { 3149 // The register offset form of scalar stores may only use m0 as the 3150 // soffset register. 3151 const MachineOperand *Soff = getNamedOperand(MI, AMDGPU::OpName::soff); 3152 if (Soff && Soff->getReg() != AMDGPU::M0) { 3153 ErrInfo = "scalar stores must use m0 as offset register"; 3154 return false; 3155 } 3156 } 3157 } 3158 3159 if (isFLAT(MI) && !MF->getSubtarget<GCNSubtarget>().hasFlatInstOffsets()) { 3160 const MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 3161 if (Offset->getImm() != 0) { 3162 ErrInfo = "subtarget does not support offsets in flat instructions"; 3163 return false; 3164 } 3165 } 3166 3167 const MachineOperand *DppCt = getNamedOperand(MI, AMDGPU::OpName::dpp_ctrl); 3168 if (DppCt) { 3169 using namespace AMDGPU::DPP; 3170 3171 unsigned DC = DppCt->getImm(); 3172 if (DC == DppCtrl::DPP_UNUSED1 || DC == DppCtrl::DPP_UNUSED2 || 3173 DC == DppCtrl::DPP_UNUSED3 || DC > DppCtrl::DPP_LAST || 3174 (DC >= DppCtrl::DPP_UNUSED4_FIRST && DC <= DppCtrl::DPP_UNUSED4_LAST) || 3175 (DC >= DppCtrl::DPP_UNUSED5_FIRST && DC <= DppCtrl::DPP_UNUSED5_LAST) || 3176 (DC >= DppCtrl::DPP_UNUSED6_FIRST && DC <= DppCtrl::DPP_UNUSED6_LAST) || 3177 (DC >= DppCtrl::DPP_UNUSED7_FIRST && DC <= DppCtrl::DPP_UNUSED7_LAST)) { 3178 ErrInfo = "Invalid dpp_ctrl value"; 3179 return false; 3180 } 3181 } 3182 3183 return true; 3184 } 3185 3186 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) const { 3187 switch (MI.getOpcode()) { 3188 default: return AMDGPU::INSTRUCTION_LIST_END; 3189 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE; 3190 case AMDGPU::COPY: return AMDGPU::COPY; 3191 case AMDGPU::PHI: return AMDGPU::PHI; 3192 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG; 3193 case AMDGPU::WQM: return AMDGPU::WQM; 3194 case AMDGPU::WWM: return AMDGPU::WWM; 3195 case AMDGPU::S_MOV_B32: 3196 return MI.getOperand(1).isReg() ? 3197 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32; 3198 case AMDGPU::S_ADD_I32: 3199 return ST.hasAddNoCarry() ? AMDGPU::V_ADD_U32_e64 : AMDGPU::V_ADD_I32_e32; 3200 case AMDGPU::S_ADDC_U32: 3201 return AMDGPU::V_ADDC_U32_e32; 3202 case AMDGPU::S_SUB_I32: 3203 return ST.hasAddNoCarry() ? AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_I32_e32; 3204 // FIXME: These are not consistently handled, and selected when the carry is 3205 // used. 3206 case AMDGPU::S_ADD_U32: 3207 return AMDGPU::V_ADD_I32_e32; 3208 case AMDGPU::S_SUB_U32: 3209 return AMDGPU::V_SUB_I32_e32; 3210 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32; 3211 case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_I32; 3212 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64; 3213 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64; 3214 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64; 3215 case AMDGPU::S_XNOR_B32: 3216 return ST.hasDLInsts() ? AMDGPU::V_XNOR_B32_e64 : AMDGPU::INSTRUCTION_LIST_END; 3217 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64; 3218 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64; 3219 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64; 3220 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64; 3221 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32; 3222 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64; 3223 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32; 3224 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64; 3225 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32; 3226 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64; 3227 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32; 3228 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32; 3229 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32; 3230 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32; 3231 case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64; 3232 case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32; 3233 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32; 3234 case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32; 3235 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e32; 3236 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e32; 3237 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e32; 3238 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e32; 3239 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e32; 3240 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e32; 3241 case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e32; 3242 case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e32; 3243 case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e32; 3244 case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e32; 3245 case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e32; 3246 case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e32; 3247 case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e32; 3248 case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e32; 3249 case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64; 3250 case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32; 3251 case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32; 3252 case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64; 3253 case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ; 3254 case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ; 3255 } 3256 } 3257 3258 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI, 3259 unsigned OpNo) const { 3260 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 3261 const MCInstrDesc &Desc = get(MI.getOpcode()); 3262 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() || 3263 Desc.OpInfo[OpNo].RegClass == -1) { 3264 unsigned Reg = MI.getOperand(OpNo).getReg(); 3265 3266 if (TargetRegisterInfo::isVirtualRegister(Reg)) 3267 return MRI.getRegClass(Reg); 3268 return RI.getPhysRegClass(Reg); 3269 } 3270 3271 unsigned RCID = Desc.OpInfo[OpNo].RegClass; 3272 return RI.getRegClass(RCID); 3273 } 3274 3275 bool SIInstrInfo::canReadVGPR(const MachineInstr &MI, unsigned OpNo) const { 3276 switch (MI.getOpcode()) { 3277 case AMDGPU::COPY: 3278 case AMDGPU::REG_SEQUENCE: 3279 case AMDGPU::PHI: 3280 case AMDGPU::INSERT_SUBREG: 3281 return RI.hasVGPRs(getOpRegClass(MI, 0)); 3282 default: 3283 return RI.hasVGPRs(getOpRegClass(MI, OpNo)); 3284 } 3285 } 3286 3287 void SIInstrInfo::legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const { 3288 MachineBasicBlock::iterator I = MI; 3289 MachineBasicBlock *MBB = MI.getParent(); 3290 MachineOperand &MO = MI.getOperand(OpIdx); 3291 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 3292 unsigned RCID = get(MI.getOpcode()).OpInfo[OpIdx].RegClass; 3293 const TargetRegisterClass *RC = RI.getRegClass(RCID); 3294 unsigned Opcode = AMDGPU::V_MOV_B32_e32; 3295 if (MO.isReg()) 3296 Opcode = AMDGPU::COPY; 3297 else if (RI.isSGPRClass(RC)) 3298 Opcode = AMDGPU::S_MOV_B32; 3299 3300 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC); 3301 if (RI.getCommonSubClass(&AMDGPU::VReg_64RegClass, VRC)) 3302 VRC = &AMDGPU::VReg_64RegClass; 3303 else 3304 VRC = &AMDGPU::VGPR_32RegClass; 3305 3306 unsigned Reg = MRI.createVirtualRegister(VRC); 3307 DebugLoc DL = MBB->findDebugLoc(I); 3308 BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).add(MO); 3309 MO.ChangeToRegister(Reg, false); 3310 } 3311 3312 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI, 3313 MachineRegisterInfo &MRI, 3314 MachineOperand &SuperReg, 3315 const TargetRegisterClass *SuperRC, 3316 unsigned SubIdx, 3317 const TargetRegisterClass *SubRC) 3318 const { 3319 MachineBasicBlock *MBB = MI->getParent(); 3320 DebugLoc DL = MI->getDebugLoc(); 3321 unsigned SubReg = MRI.createVirtualRegister(SubRC); 3322 3323 if (SuperReg.getSubReg() == AMDGPU::NoSubRegister) { 3324 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 3325 .addReg(SuperReg.getReg(), 0, SubIdx); 3326 return SubReg; 3327 } 3328 3329 // Just in case the super register is itself a sub-register, copy it to a new 3330 // value so we don't need to worry about merging its subreg index with the 3331 // SubIdx passed to this function. The register coalescer should be able to 3332 // eliminate this extra copy. 3333 unsigned NewSuperReg = MRI.createVirtualRegister(SuperRC); 3334 3335 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg) 3336 .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg()); 3337 3338 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 3339 .addReg(NewSuperReg, 0, SubIdx); 3340 3341 return SubReg; 3342 } 3343 3344 MachineOperand SIInstrInfo::buildExtractSubRegOrImm( 3345 MachineBasicBlock::iterator MII, 3346 MachineRegisterInfo &MRI, 3347 MachineOperand &Op, 3348 const TargetRegisterClass *SuperRC, 3349 unsigned SubIdx, 3350 const TargetRegisterClass *SubRC) const { 3351 if (Op.isImm()) { 3352 if (SubIdx == AMDGPU::sub0) 3353 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm())); 3354 if (SubIdx == AMDGPU::sub1) 3355 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32)); 3356 3357 llvm_unreachable("Unhandled register index for immediate"); 3358 } 3359 3360 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC, 3361 SubIdx, SubRC); 3362 return MachineOperand::CreateReg(SubReg, false); 3363 } 3364 3365 // Change the order of operands from (0, 1, 2) to (0, 2, 1) 3366 void SIInstrInfo::swapOperands(MachineInstr &Inst) const { 3367 assert(Inst.getNumExplicitOperands() == 3); 3368 MachineOperand Op1 = Inst.getOperand(1); 3369 Inst.RemoveOperand(1); 3370 Inst.addOperand(Op1); 3371 } 3372 3373 bool SIInstrInfo::isLegalRegOperand(const MachineRegisterInfo &MRI, 3374 const MCOperandInfo &OpInfo, 3375 const MachineOperand &MO) const { 3376 if (!MO.isReg()) 3377 return false; 3378 3379 unsigned Reg = MO.getReg(); 3380 const TargetRegisterClass *RC = 3381 TargetRegisterInfo::isVirtualRegister(Reg) ? 3382 MRI.getRegClass(Reg) : 3383 RI.getPhysRegClass(Reg); 3384 3385 const SIRegisterInfo *TRI = 3386 static_cast<const SIRegisterInfo*>(MRI.getTargetRegisterInfo()); 3387 RC = TRI->getSubRegClass(RC, MO.getSubReg()); 3388 3389 // In order to be legal, the common sub-class must be equal to the 3390 // class of the current operand. For example: 3391 // 3392 // v_mov_b32 s0 ; Operand defined as vsrc_b32 3393 // ; RI.getCommonSubClass(s0,vsrc_b32) = sgpr ; LEGAL 3394 // 3395 // s_sendmsg 0, s0 ; Operand defined as m0reg 3396 // ; RI.getCommonSubClass(s0,m0reg) = m0reg ; NOT LEGAL 3397 3398 return RI.getCommonSubClass(RC, RI.getRegClass(OpInfo.RegClass)) == RC; 3399 } 3400 3401 bool SIInstrInfo::isLegalVSrcOperand(const MachineRegisterInfo &MRI, 3402 const MCOperandInfo &OpInfo, 3403 const MachineOperand &MO) const { 3404 if (MO.isReg()) 3405 return isLegalRegOperand(MRI, OpInfo, MO); 3406 3407 // Handle non-register types that are treated like immediates. 3408 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI()); 3409 return true; 3410 } 3411 3412 bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx, 3413 const MachineOperand *MO) const { 3414 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 3415 const MCInstrDesc &InstDesc = MI.getDesc(); 3416 const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx]; 3417 const TargetRegisterClass *DefinedRC = 3418 OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr; 3419 if (!MO) 3420 MO = &MI.getOperand(OpIdx); 3421 3422 if (isVALU(MI) && usesConstantBus(MRI, *MO, OpInfo)) { 3423 3424 RegSubRegPair SGPRUsed; 3425 if (MO->isReg()) 3426 SGPRUsed = RegSubRegPair(MO->getReg(), MO->getSubReg()); 3427 3428 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 3429 if (i == OpIdx) 3430 continue; 3431 const MachineOperand &Op = MI.getOperand(i); 3432 if (Op.isReg()) { 3433 if ((Op.getReg() != SGPRUsed.Reg || Op.getSubReg() != SGPRUsed.SubReg) && 3434 usesConstantBus(MRI, Op, InstDesc.OpInfo[i])) { 3435 return false; 3436 } 3437 } else if (InstDesc.OpInfo[i].OperandType == AMDGPU::OPERAND_KIMM32) { 3438 return false; 3439 } 3440 } 3441 } 3442 3443 if (MO->isReg()) { 3444 assert(DefinedRC); 3445 return isLegalRegOperand(MRI, OpInfo, *MO); 3446 } 3447 3448 // Handle non-register types that are treated like immediates. 3449 assert(MO->isImm() || MO->isTargetIndex() || MO->isFI()); 3450 3451 if (!DefinedRC) { 3452 // This operand expects an immediate. 3453 return true; 3454 } 3455 3456 return isImmOperandLegal(MI, OpIdx, *MO); 3457 } 3458 3459 void SIInstrInfo::legalizeOperandsVOP2(MachineRegisterInfo &MRI, 3460 MachineInstr &MI) const { 3461 unsigned Opc = MI.getOpcode(); 3462 const MCInstrDesc &InstrDesc = get(Opc); 3463 3464 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 3465 MachineOperand &Src1 = MI.getOperand(Src1Idx); 3466 3467 // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32 3468 // we need to only have one constant bus use. 3469 // 3470 // Note we do not need to worry about literal constants here. They are 3471 // disabled for the operand type for instructions because they will always 3472 // violate the one constant bus use rule. 3473 bool HasImplicitSGPR = findImplicitSGPRRead(MI) != AMDGPU::NoRegister; 3474 if (HasImplicitSGPR) { 3475 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 3476 MachineOperand &Src0 = MI.getOperand(Src0Idx); 3477 3478 if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) 3479 legalizeOpWithMove(MI, Src0Idx); 3480 } 3481 3482 // Special case: V_WRITELANE_B32 accepts only immediate or SGPR operands for 3483 // both the value to write (src0) and lane select (src1). Fix up non-SGPR 3484 // src0/src1 with V_READFIRSTLANE. 3485 if (Opc == AMDGPU::V_WRITELANE_B32) { 3486 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 3487 MachineOperand &Src0 = MI.getOperand(Src0Idx); 3488 const DebugLoc &DL = MI.getDebugLoc(); 3489 if (Src0.isReg() && RI.isVGPR(MRI, Src0.getReg())) { 3490 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 3491 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 3492 .add(Src0); 3493 Src0.ChangeToRegister(Reg, false); 3494 } 3495 if (Src1.isReg() && RI.isVGPR(MRI, Src1.getReg())) { 3496 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 3497 const DebugLoc &DL = MI.getDebugLoc(); 3498 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 3499 .add(Src1); 3500 Src1.ChangeToRegister(Reg, false); 3501 } 3502 return; 3503 } 3504 3505 // VOP2 src0 instructions support all operand types, so we don't need to check 3506 // their legality. If src1 is already legal, we don't need to do anything. 3507 if (isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src1)) 3508 return; 3509 3510 // Special case: V_READLANE_B32 accepts only immediate or SGPR operands for 3511 // lane select. Fix up using V_READFIRSTLANE, since we assume that the lane 3512 // select is uniform. 3513 if (Opc == AMDGPU::V_READLANE_B32 && Src1.isReg() && 3514 RI.isVGPR(MRI, Src1.getReg())) { 3515 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 3516 const DebugLoc &DL = MI.getDebugLoc(); 3517 BuildMI(*MI.getParent(), MI, DL, get(AMDGPU::V_READFIRSTLANE_B32), Reg) 3518 .add(Src1); 3519 Src1.ChangeToRegister(Reg, false); 3520 return; 3521 } 3522 3523 // We do not use commuteInstruction here because it is too aggressive and will 3524 // commute if it is possible. We only want to commute here if it improves 3525 // legality. This can be called a fairly large number of times so don't waste 3526 // compile time pointlessly swapping and checking legality again. 3527 if (HasImplicitSGPR || !MI.isCommutable()) { 3528 legalizeOpWithMove(MI, Src1Idx); 3529 return; 3530 } 3531 3532 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 3533 MachineOperand &Src0 = MI.getOperand(Src0Idx); 3534 3535 // If src0 can be used as src1, commuting will make the operands legal. 3536 // Otherwise we have to give up and insert a move. 3537 // 3538 // TODO: Other immediate-like operand kinds could be commuted if there was a 3539 // MachineOperand::ChangeTo* for them. 3540 if ((!Src1.isImm() && !Src1.isReg()) || 3541 !isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src0)) { 3542 legalizeOpWithMove(MI, Src1Idx); 3543 return; 3544 } 3545 3546 int CommutedOpc = commuteOpcode(MI); 3547 if (CommutedOpc == -1) { 3548 legalizeOpWithMove(MI, Src1Idx); 3549 return; 3550 } 3551 3552 MI.setDesc(get(CommutedOpc)); 3553 3554 unsigned Src0Reg = Src0.getReg(); 3555 unsigned Src0SubReg = Src0.getSubReg(); 3556 bool Src0Kill = Src0.isKill(); 3557 3558 if (Src1.isImm()) 3559 Src0.ChangeToImmediate(Src1.getImm()); 3560 else if (Src1.isReg()) { 3561 Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill()); 3562 Src0.setSubReg(Src1.getSubReg()); 3563 } else 3564 llvm_unreachable("Should only have register or immediate operands"); 3565 3566 Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill); 3567 Src1.setSubReg(Src0SubReg); 3568 } 3569 3570 // Legalize VOP3 operands. Because all operand types are supported for any 3571 // operand, and since literal constants are not allowed and should never be 3572 // seen, we only need to worry about inserting copies if we use multiple SGPR 3573 // operands. 3574 void SIInstrInfo::legalizeOperandsVOP3(MachineRegisterInfo &MRI, 3575 MachineInstr &MI) const { 3576 unsigned Opc = MI.getOpcode(); 3577 3578 int VOP3Idx[3] = { 3579 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0), 3580 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1), 3581 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2) 3582 }; 3583 3584 // Find the one SGPR operand we are allowed to use. 3585 unsigned SGPRReg = findUsedSGPR(MI, VOP3Idx); 3586 3587 for (unsigned i = 0; i < 3; ++i) { 3588 int Idx = VOP3Idx[i]; 3589 if (Idx == -1) 3590 break; 3591 MachineOperand &MO = MI.getOperand(Idx); 3592 3593 // We should never see a VOP3 instruction with an illegal immediate operand. 3594 if (!MO.isReg()) 3595 continue; 3596 3597 if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg()))) 3598 continue; // VGPRs are legal 3599 3600 if (SGPRReg == AMDGPU::NoRegister || SGPRReg == MO.getReg()) { 3601 SGPRReg = MO.getReg(); 3602 // We can use one SGPR in each VOP3 instruction. 3603 continue; 3604 } 3605 3606 // If we make it this far, then the operand is not legal and we must 3607 // legalize it. 3608 legalizeOpWithMove(MI, Idx); 3609 } 3610 } 3611 3612 unsigned SIInstrInfo::readlaneVGPRToSGPR(unsigned SrcReg, MachineInstr &UseMI, 3613 MachineRegisterInfo &MRI) const { 3614 const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg); 3615 const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC); 3616 unsigned DstReg = MRI.createVirtualRegister(SRC); 3617 unsigned SubRegs = RI.getRegSizeInBits(*VRC) / 32; 3618 3619 if (SubRegs == 1) { 3620 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 3621 get(AMDGPU::V_READFIRSTLANE_B32), DstReg) 3622 .addReg(SrcReg); 3623 return DstReg; 3624 } 3625 3626 SmallVector<unsigned, 8> SRegs; 3627 for (unsigned i = 0; i < SubRegs; ++i) { 3628 unsigned SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3629 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 3630 get(AMDGPU::V_READFIRSTLANE_B32), SGPR) 3631 .addReg(SrcReg, 0, RI.getSubRegFromChannel(i)); 3632 SRegs.push_back(SGPR); 3633 } 3634 3635 MachineInstrBuilder MIB = 3636 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 3637 get(AMDGPU::REG_SEQUENCE), DstReg); 3638 for (unsigned i = 0; i < SubRegs; ++i) { 3639 MIB.addReg(SRegs[i]); 3640 MIB.addImm(RI.getSubRegFromChannel(i)); 3641 } 3642 return DstReg; 3643 } 3644 3645 void SIInstrInfo::legalizeOperandsSMRD(MachineRegisterInfo &MRI, 3646 MachineInstr &MI) const { 3647 3648 // If the pointer is store in VGPRs, then we need to move them to 3649 // SGPRs using v_readfirstlane. This is safe because we only select 3650 // loads with uniform pointers to SMRD instruction so we know the 3651 // pointer value is uniform. 3652 MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase); 3653 if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) { 3654 unsigned SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI); 3655 SBase->setReg(SGPR); 3656 } 3657 MachineOperand *SOff = getNamedOperand(MI, AMDGPU::OpName::soff); 3658 if (SOff && !RI.isSGPRClass(MRI.getRegClass(SOff->getReg()))) { 3659 unsigned SGPR = readlaneVGPRToSGPR(SOff->getReg(), MI, MRI); 3660 SOff->setReg(SGPR); 3661 } 3662 } 3663 3664 void SIInstrInfo::legalizeGenericOperand(MachineBasicBlock &InsertMBB, 3665 MachineBasicBlock::iterator I, 3666 const TargetRegisterClass *DstRC, 3667 MachineOperand &Op, 3668 MachineRegisterInfo &MRI, 3669 const DebugLoc &DL) const { 3670 unsigned OpReg = Op.getReg(); 3671 unsigned OpSubReg = Op.getSubReg(); 3672 3673 const TargetRegisterClass *OpRC = RI.getSubClassWithSubReg( 3674 RI.getRegClassForReg(MRI, OpReg), OpSubReg); 3675 3676 // Check if operand is already the correct register class. 3677 if (DstRC == OpRC) 3678 return; 3679 3680 unsigned DstReg = MRI.createVirtualRegister(DstRC); 3681 MachineInstr *Copy = 3682 BuildMI(InsertMBB, I, DL, get(AMDGPU::COPY), DstReg).add(Op); 3683 3684 Op.setReg(DstReg); 3685 Op.setSubReg(0); 3686 3687 MachineInstr *Def = MRI.getVRegDef(OpReg); 3688 if (!Def) 3689 return; 3690 3691 // Try to eliminate the copy if it is copying an immediate value. 3692 if (Def->isMoveImmediate()) 3693 FoldImmediate(*Copy, *Def, OpReg, &MRI); 3694 } 3695 3696 // Emit the actual waterfall loop, executing the wrapped instruction for each 3697 // unique value of \p Rsrc across all lanes. In the best case we execute 1 3698 // iteration, in the worst case we execute 64 (once per lane). 3699 static void 3700 emitLoadSRsrcFromVGPRLoop(const SIInstrInfo &TII, MachineRegisterInfo &MRI, 3701 MachineBasicBlock &OrigBB, MachineBasicBlock &LoopBB, 3702 const DebugLoc &DL, MachineOperand &Rsrc) { 3703 MachineBasicBlock::iterator I = LoopBB.begin(); 3704 3705 unsigned VRsrc = Rsrc.getReg(); 3706 unsigned VRsrcUndef = getUndefRegState(Rsrc.isUndef()); 3707 3708 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 3709 unsigned CondReg0 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 3710 unsigned CondReg1 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 3711 unsigned AndCond = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 3712 unsigned SRsrcSub0 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3713 unsigned SRsrcSub1 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3714 unsigned SRsrcSub2 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3715 unsigned SRsrcSub3 = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3716 unsigned SRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass); 3717 3718 // Beginning of the loop, read the next Rsrc variant. 3719 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), SRsrcSub0) 3720 .addReg(VRsrc, VRsrcUndef, AMDGPU::sub0); 3721 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), SRsrcSub1) 3722 .addReg(VRsrc, VRsrcUndef, AMDGPU::sub1); 3723 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), SRsrcSub2) 3724 .addReg(VRsrc, VRsrcUndef, AMDGPU::sub2); 3725 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_READFIRSTLANE_B32), SRsrcSub3) 3726 .addReg(VRsrc, VRsrcUndef, AMDGPU::sub3); 3727 3728 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::REG_SEQUENCE), SRsrc) 3729 .addReg(SRsrcSub0) 3730 .addImm(AMDGPU::sub0) 3731 .addReg(SRsrcSub1) 3732 .addImm(AMDGPU::sub1) 3733 .addReg(SRsrcSub2) 3734 .addImm(AMDGPU::sub2) 3735 .addReg(SRsrcSub3) 3736 .addImm(AMDGPU::sub3); 3737 3738 // Update Rsrc operand to use the SGPR Rsrc. 3739 Rsrc.setReg(SRsrc); 3740 Rsrc.setIsKill(true); 3741 3742 // Identify all lanes with identical Rsrc operands in their VGPRs. 3743 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64), CondReg0) 3744 .addReg(SRsrc, 0, AMDGPU::sub0_sub1) 3745 .addReg(VRsrc, 0, AMDGPU::sub0_sub1); 3746 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::V_CMP_EQ_U64_e64), CondReg1) 3747 .addReg(SRsrc, 0, AMDGPU::sub2_sub3) 3748 .addReg(VRsrc, 0, AMDGPU::sub2_sub3); 3749 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::S_AND_B64), AndCond) 3750 .addReg(CondReg0) 3751 .addReg(CondReg1); 3752 3753 MRI.setSimpleHint(SaveExec, AndCond); 3754 3755 // Update EXEC to matching lanes, saving original to SaveExec. 3756 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::S_AND_SAVEEXEC_B64), SaveExec) 3757 .addReg(AndCond, RegState::Kill); 3758 3759 // The original instruction is here; we insert the terminators after it. 3760 I = LoopBB.end(); 3761 3762 // Update EXEC, switch all done bits to 0 and all todo bits to 1. 3763 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::S_XOR_B64_term), AMDGPU::EXEC) 3764 .addReg(AMDGPU::EXEC) 3765 .addReg(SaveExec); 3766 BuildMI(LoopBB, I, DL, TII.get(AMDGPU::S_CBRANCH_EXECNZ)).addMBB(&LoopBB); 3767 } 3768 3769 // Build a waterfall loop around \p MI, replacing the VGPR \p Rsrc register 3770 // with SGPRs by iterating over all unique values across all lanes. 3771 static void loadSRsrcFromVGPR(const SIInstrInfo &TII, MachineInstr &MI, 3772 MachineOperand &Rsrc, MachineDominatorTree *MDT) { 3773 MachineBasicBlock &MBB = *MI.getParent(); 3774 MachineFunction &MF = *MBB.getParent(); 3775 MachineRegisterInfo &MRI = MF.getRegInfo(); 3776 MachineBasicBlock::iterator I(&MI); 3777 const DebugLoc &DL = MI.getDebugLoc(); 3778 3779 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 3780 3781 // Save the EXEC mask 3782 BuildMI(MBB, I, DL, TII.get(AMDGPU::S_MOV_B64), SaveExec) 3783 .addReg(AMDGPU::EXEC); 3784 3785 // Killed uses in the instruction we are waterfalling around will be 3786 // incorrect due to the added control-flow. 3787 for (auto &MO : MI.uses()) { 3788 if (MO.isReg() && MO.isUse()) { 3789 MRI.clearKillFlags(MO.getReg()); 3790 } 3791 } 3792 3793 // To insert the loop we need to split the block. Move everything after this 3794 // point to a new block, and insert a new empty block between the two. 3795 MachineBasicBlock *LoopBB = MF.CreateMachineBasicBlock(); 3796 MachineBasicBlock *RemainderBB = MF.CreateMachineBasicBlock(); 3797 MachineFunction::iterator MBBI(MBB); 3798 ++MBBI; 3799 3800 MF.insert(MBBI, LoopBB); 3801 MF.insert(MBBI, RemainderBB); 3802 3803 LoopBB->addSuccessor(LoopBB); 3804 LoopBB->addSuccessor(RemainderBB); 3805 3806 // Move MI to the LoopBB, and the remainder of the block to RemainderBB. 3807 MachineBasicBlock::iterator J = I++; 3808 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB); 3809 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end()); 3810 LoopBB->splice(LoopBB->begin(), &MBB, J); 3811 3812 MBB.addSuccessor(LoopBB); 3813 3814 // Update dominators. We know that MBB immediately dominates LoopBB, that 3815 // LoopBB immediately dominates RemainderBB, and that RemainderBB immediately 3816 // dominates all of the successors transferred to it from MBB that MBB used 3817 // to dominate. 3818 if (MDT) { 3819 MDT->addNewBlock(LoopBB, &MBB); 3820 MDT->addNewBlock(RemainderBB, LoopBB); 3821 for (auto &Succ : RemainderBB->successors()) { 3822 if (MDT->dominates(&MBB, Succ)) { 3823 MDT->changeImmediateDominator(Succ, RemainderBB); 3824 } 3825 } 3826 } 3827 3828 emitLoadSRsrcFromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, Rsrc); 3829 3830 // Restore the EXEC mask 3831 MachineBasicBlock::iterator First = RemainderBB->begin(); 3832 BuildMI(*RemainderBB, First, DL, TII.get(AMDGPU::S_MOV_B64), AMDGPU::EXEC) 3833 .addReg(SaveExec); 3834 } 3835 3836 // Extract pointer from Rsrc and return a zero-value Rsrc replacement. 3837 static std::tuple<unsigned, unsigned> 3838 extractRsrcPtr(const SIInstrInfo &TII, MachineInstr &MI, MachineOperand &Rsrc) { 3839 MachineBasicBlock &MBB = *MI.getParent(); 3840 MachineFunction &MF = *MBB.getParent(); 3841 MachineRegisterInfo &MRI = MF.getRegInfo(); 3842 3843 // Extract the ptr from the resource descriptor. 3844 unsigned RsrcPtr = 3845 TII.buildExtractSubReg(MI, MRI, Rsrc, &AMDGPU::VReg_128RegClass, 3846 AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass); 3847 3848 // Create an empty resource descriptor 3849 unsigned Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 3850 unsigned SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3851 unsigned SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 3852 unsigned NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass); 3853 uint64_t RsrcDataFormat = TII.getDefaultRsrcDataFormat(); 3854 3855 // Zero64 = 0 3856 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B64), Zero64) 3857 .addImm(0); 3858 3859 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0} 3860 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatLo) 3861 .addImm(RsrcDataFormat & 0xFFFFFFFF); 3862 3863 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32} 3864 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::S_MOV_B32), SRsrcFormatHi) 3865 .addImm(RsrcDataFormat >> 32); 3866 3867 // NewSRsrc = {Zero64, SRsrcFormat} 3868 BuildMI(MBB, MI, MI.getDebugLoc(), TII.get(AMDGPU::REG_SEQUENCE), NewSRsrc) 3869 .addReg(Zero64) 3870 .addImm(AMDGPU::sub0_sub1) 3871 .addReg(SRsrcFormatLo) 3872 .addImm(AMDGPU::sub2) 3873 .addReg(SRsrcFormatHi) 3874 .addImm(AMDGPU::sub3); 3875 3876 return std::make_tuple(RsrcPtr, NewSRsrc); 3877 } 3878 3879 void SIInstrInfo::legalizeOperands(MachineInstr &MI, 3880 MachineDominatorTree *MDT) const { 3881 MachineFunction &MF = *MI.getParent()->getParent(); 3882 MachineRegisterInfo &MRI = MF.getRegInfo(); 3883 3884 // Legalize VOP2 3885 if (isVOP2(MI) || isVOPC(MI)) { 3886 legalizeOperandsVOP2(MRI, MI); 3887 return; 3888 } 3889 3890 // Legalize VOP3 3891 if (isVOP3(MI)) { 3892 legalizeOperandsVOP3(MRI, MI); 3893 return; 3894 } 3895 3896 // Legalize SMRD 3897 if (isSMRD(MI)) { 3898 legalizeOperandsSMRD(MRI, MI); 3899 return; 3900 } 3901 3902 // Legalize REG_SEQUENCE and PHI 3903 // The register class of the operands much be the same type as the register 3904 // class of the output. 3905 if (MI.getOpcode() == AMDGPU::PHI) { 3906 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr; 3907 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { 3908 if (!MI.getOperand(i).isReg() || 3909 !TargetRegisterInfo::isVirtualRegister(MI.getOperand(i).getReg())) 3910 continue; 3911 const TargetRegisterClass *OpRC = 3912 MRI.getRegClass(MI.getOperand(i).getReg()); 3913 if (RI.hasVGPRs(OpRC)) { 3914 VRC = OpRC; 3915 } else { 3916 SRC = OpRC; 3917 } 3918 } 3919 3920 // If any of the operands are VGPR registers, then they all most be 3921 // otherwise we will create illegal VGPR->SGPR copies when legalizing 3922 // them. 3923 if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) { 3924 if (!VRC) { 3925 assert(SRC); 3926 VRC = RI.getEquivalentVGPRClass(SRC); 3927 } 3928 RC = VRC; 3929 } else { 3930 RC = SRC; 3931 } 3932 3933 // Update all the operands so they have the same type. 3934 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 3935 MachineOperand &Op = MI.getOperand(I); 3936 if (!Op.isReg() || !TargetRegisterInfo::isVirtualRegister(Op.getReg())) 3937 continue; 3938 3939 // MI is a PHI instruction. 3940 MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB(); 3941 MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator(); 3942 3943 // Avoid creating no-op copies with the same src and dst reg class. These 3944 // confuse some of the machine passes. 3945 legalizeGenericOperand(*InsertBB, Insert, RC, Op, MRI, MI.getDebugLoc()); 3946 } 3947 } 3948 3949 // REG_SEQUENCE doesn't really require operand legalization, but if one has a 3950 // VGPR dest type and SGPR sources, insert copies so all operands are 3951 // VGPRs. This seems to help operand folding / the register coalescer. 3952 if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) { 3953 MachineBasicBlock *MBB = MI.getParent(); 3954 const TargetRegisterClass *DstRC = getOpRegClass(MI, 0); 3955 if (RI.hasVGPRs(DstRC)) { 3956 // Update all the operands so they are VGPR register classes. These may 3957 // not be the same register class because REG_SEQUENCE supports mixing 3958 // subregister index types e.g. sub0_sub1 + sub2 + sub3 3959 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 3960 MachineOperand &Op = MI.getOperand(I); 3961 if (!Op.isReg() || !TargetRegisterInfo::isVirtualRegister(Op.getReg())) 3962 continue; 3963 3964 const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg()); 3965 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC); 3966 if (VRC == OpRC) 3967 continue; 3968 3969 legalizeGenericOperand(*MBB, MI, VRC, Op, MRI, MI.getDebugLoc()); 3970 Op.setIsKill(); 3971 } 3972 } 3973 3974 return; 3975 } 3976 3977 // Legalize INSERT_SUBREG 3978 // src0 must have the same register class as dst 3979 if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) { 3980 unsigned Dst = MI.getOperand(0).getReg(); 3981 unsigned Src0 = MI.getOperand(1).getReg(); 3982 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst); 3983 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0); 3984 if (DstRC != Src0RC) { 3985 MachineBasicBlock *MBB = MI.getParent(); 3986 MachineOperand &Op = MI.getOperand(1); 3987 legalizeGenericOperand(*MBB, MI, DstRC, Op, MRI, MI.getDebugLoc()); 3988 } 3989 return; 3990 } 3991 3992 // Legalize SI_INIT_M0 3993 if (MI.getOpcode() == AMDGPU::SI_INIT_M0) { 3994 MachineOperand &Src = MI.getOperand(0); 3995 if (Src.isReg() && RI.hasVGPRs(MRI.getRegClass(Src.getReg()))) 3996 Src.setReg(readlaneVGPRToSGPR(Src.getReg(), MI, MRI)); 3997 return; 3998 } 3999 4000 // Legalize MIMG and MUBUF/MTBUF for shaders. 4001 // 4002 // Shaders only generate MUBUF/MTBUF instructions via intrinsics or via 4003 // scratch memory access. In both cases, the legalization never involves 4004 // conversion to the addr64 form. 4005 if (isMIMG(MI) || 4006 (AMDGPU::isShader(MF.getFunction().getCallingConv()) && 4007 (isMUBUF(MI) || isMTBUF(MI)))) { 4008 MachineOperand *SRsrc = getNamedOperand(MI, AMDGPU::OpName::srsrc); 4009 if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg()))) { 4010 unsigned SGPR = readlaneVGPRToSGPR(SRsrc->getReg(), MI, MRI); 4011 SRsrc->setReg(SGPR); 4012 } 4013 4014 MachineOperand *SSamp = getNamedOperand(MI, AMDGPU::OpName::ssamp); 4015 if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg()))) { 4016 unsigned SGPR = readlaneVGPRToSGPR(SSamp->getReg(), MI, MRI); 4017 SSamp->setReg(SGPR); 4018 } 4019 return; 4020 } 4021 4022 // Legalize MUBUF* instructions. 4023 int RsrcIdx = 4024 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc); 4025 if (RsrcIdx != -1) { 4026 // We have an MUBUF instruction 4027 MachineOperand *Rsrc = &MI.getOperand(RsrcIdx); 4028 unsigned RsrcRC = get(MI.getOpcode()).OpInfo[RsrcIdx].RegClass; 4029 if (RI.getCommonSubClass(MRI.getRegClass(Rsrc->getReg()), 4030 RI.getRegClass(RsrcRC))) { 4031 // The operands are legal. 4032 // FIXME: We may need to legalize operands besided srsrc. 4033 return; 4034 } 4035 4036 // Legalize a VGPR Rsrc. 4037 // 4038 // If the instruction is _ADDR64, we can avoid a waterfall by extracting 4039 // the base pointer from the VGPR Rsrc, adding it to the VAddr, then using 4040 // a zero-value SRsrc. 4041 // 4042 // If the instruction is _OFFSET (both idxen and offen disabled), and we 4043 // support ADDR64 instructions, we can convert to ADDR64 and do the same as 4044 // above. 4045 // 4046 // Otherwise we are on non-ADDR64 hardware, and/or we have 4047 // idxen/offen/bothen and we fall back to a waterfall loop. 4048 4049 MachineBasicBlock &MBB = *MI.getParent(); 4050 4051 MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 4052 if (VAddr && AMDGPU::getIfAddr64Inst(MI.getOpcode()) != -1) { 4053 // This is already an ADDR64 instruction so we need to add the pointer 4054 // extracted from the resource descriptor to the current value of VAddr. 4055 unsigned NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4056 unsigned NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4057 unsigned NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 4058 4059 unsigned RsrcPtr, NewSRsrc; 4060 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc); 4061 4062 // NewVaddrLo = RsrcPtr:sub0 + VAddr:sub0 4063 DebugLoc DL = MI.getDebugLoc(); 4064 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_I32_e32), NewVAddrLo) 4065 .addReg(RsrcPtr, 0, AMDGPU::sub0) 4066 .addReg(VAddr->getReg(), 0, AMDGPU::sub0); 4067 4068 // NewVaddrHi = RsrcPtr:sub1 + VAddr:sub1 4069 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e32), NewVAddrHi) 4070 .addReg(RsrcPtr, 0, AMDGPU::sub1) 4071 .addReg(VAddr->getReg(), 0, AMDGPU::sub1); 4072 4073 // NewVaddr = {NewVaddrHi, NewVaddrLo} 4074 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr) 4075 .addReg(NewVAddrLo) 4076 .addImm(AMDGPU::sub0) 4077 .addReg(NewVAddrHi) 4078 .addImm(AMDGPU::sub1); 4079 4080 VAddr->setReg(NewVAddr); 4081 Rsrc->setReg(NewSRsrc); 4082 } else if (!VAddr && ST.hasAddr64()) { 4083 // This instructions is the _OFFSET variant, so we need to convert it to 4084 // ADDR64. 4085 assert(MBB.getParent()->getSubtarget<GCNSubtarget>().getGeneration() 4086 < AMDGPUSubtarget::VOLCANIC_ISLANDS && 4087 "FIXME: Need to emit flat atomics here"); 4088 4089 unsigned RsrcPtr, NewSRsrc; 4090 std::tie(RsrcPtr, NewSRsrc) = extractRsrcPtr(*this, MI, *Rsrc); 4091 4092 unsigned NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 4093 MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata); 4094 MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 4095 MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset); 4096 unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode()); 4097 4098 // Atomics rith return have have an additional tied operand and are 4099 // missing some of the special bits. 4100 MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in); 4101 MachineInstr *Addr64; 4102 4103 if (!VDataIn) { 4104 // Regular buffer load / store. 4105 MachineInstrBuilder MIB = 4106 BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 4107 .add(*VData) 4108 .addReg(NewVAddr) 4109 .addReg(NewSRsrc) 4110 .add(*SOffset) 4111 .add(*Offset); 4112 4113 // Atomics do not have this operand. 4114 if (const MachineOperand *GLC = 4115 getNamedOperand(MI, AMDGPU::OpName::glc)) { 4116 MIB.addImm(GLC->getImm()); 4117 } 4118 4119 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::slc)); 4120 4121 if (const MachineOperand *TFE = 4122 getNamedOperand(MI, AMDGPU::OpName::tfe)) { 4123 MIB.addImm(TFE->getImm()); 4124 } 4125 4126 MIB.cloneMemRefs(MI); 4127 Addr64 = MIB; 4128 } else { 4129 // Atomics with return. 4130 Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 4131 .add(*VData) 4132 .add(*VDataIn) 4133 .addReg(NewVAddr) 4134 .addReg(NewSRsrc) 4135 .add(*SOffset) 4136 .add(*Offset) 4137 .addImm(getNamedImmOperand(MI, AMDGPU::OpName::slc)) 4138 .cloneMemRefs(MI); 4139 } 4140 4141 MI.removeFromParent(); 4142 4143 // NewVaddr = {NewVaddrHi, NewVaddrLo} 4144 BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), 4145 NewVAddr) 4146 .addReg(RsrcPtr, 0, AMDGPU::sub0) 4147 .addImm(AMDGPU::sub0) 4148 .addReg(RsrcPtr, 0, AMDGPU::sub1) 4149 .addImm(AMDGPU::sub1); 4150 } else { 4151 // This is another variant; legalize Rsrc with waterfall loop from VGPRs 4152 // to SGPRs. 4153 loadSRsrcFromVGPR(*this, MI, *Rsrc, MDT); 4154 } 4155 } 4156 } 4157 4158 void SIInstrInfo::moveToVALU(MachineInstr &TopInst, 4159 MachineDominatorTree *MDT) const { 4160 SetVectorType Worklist; 4161 Worklist.insert(&TopInst); 4162 4163 while (!Worklist.empty()) { 4164 MachineInstr &Inst = *Worklist.pop_back_val(); 4165 MachineBasicBlock *MBB = Inst.getParent(); 4166 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 4167 4168 unsigned Opcode = Inst.getOpcode(); 4169 unsigned NewOpcode = getVALUOp(Inst); 4170 4171 // Handle some special cases 4172 switch (Opcode) { 4173 default: 4174 break; 4175 case AMDGPU::S_ADD_U64_PSEUDO: 4176 case AMDGPU::S_SUB_U64_PSEUDO: 4177 splitScalar64BitAddSub(Worklist, Inst, MDT); 4178 Inst.eraseFromParent(); 4179 continue; 4180 case AMDGPU::S_ADD_I32: 4181 case AMDGPU::S_SUB_I32: 4182 // FIXME: The u32 versions currently selected use the carry. 4183 if (moveScalarAddSub(Worklist, Inst, MDT)) 4184 continue; 4185 4186 // Default handling 4187 break; 4188 case AMDGPU::S_AND_B64: 4189 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_AND_B32, MDT); 4190 Inst.eraseFromParent(); 4191 continue; 4192 4193 case AMDGPU::S_OR_B64: 4194 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_OR_B32, MDT); 4195 Inst.eraseFromParent(); 4196 continue; 4197 4198 case AMDGPU::S_XOR_B64: 4199 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XOR_B32, MDT); 4200 Inst.eraseFromParent(); 4201 continue; 4202 4203 case AMDGPU::S_NAND_B64: 4204 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NAND_B32, MDT); 4205 Inst.eraseFromParent(); 4206 continue; 4207 4208 case AMDGPU::S_NOR_B64: 4209 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_NOR_B32, MDT); 4210 Inst.eraseFromParent(); 4211 continue; 4212 4213 case AMDGPU::S_XNOR_B64: 4214 if (ST.hasDLInsts()) 4215 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_XNOR_B32, MDT); 4216 else 4217 splitScalar64BitXnor(Worklist, Inst, MDT); 4218 Inst.eraseFromParent(); 4219 continue; 4220 4221 case AMDGPU::S_ANDN2_B64: 4222 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ANDN2_B32, MDT); 4223 Inst.eraseFromParent(); 4224 continue; 4225 4226 case AMDGPU::S_ORN2_B64: 4227 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::S_ORN2_B32, MDT); 4228 Inst.eraseFromParent(); 4229 continue; 4230 4231 case AMDGPU::S_NOT_B64: 4232 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::S_NOT_B32); 4233 Inst.eraseFromParent(); 4234 continue; 4235 4236 case AMDGPU::S_BCNT1_I32_B64: 4237 splitScalar64BitBCNT(Worklist, Inst); 4238 Inst.eraseFromParent(); 4239 continue; 4240 4241 case AMDGPU::S_BFE_I64: 4242 splitScalar64BitBFE(Worklist, Inst); 4243 Inst.eraseFromParent(); 4244 continue; 4245 4246 case AMDGPU::S_LSHL_B32: 4247 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 4248 NewOpcode = AMDGPU::V_LSHLREV_B32_e64; 4249 swapOperands(Inst); 4250 } 4251 break; 4252 case AMDGPU::S_ASHR_I32: 4253 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 4254 NewOpcode = AMDGPU::V_ASHRREV_I32_e64; 4255 swapOperands(Inst); 4256 } 4257 break; 4258 case AMDGPU::S_LSHR_B32: 4259 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 4260 NewOpcode = AMDGPU::V_LSHRREV_B32_e64; 4261 swapOperands(Inst); 4262 } 4263 break; 4264 case AMDGPU::S_LSHL_B64: 4265 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 4266 NewOpcode = AMDGPU::V_LSHLREV_B64; 4267 swapOperands(Inst); 4268 } 4269 break; 4270 case AMDGPU::S_ASHR_I64: 4271 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 4272 NewOpcode = AMDGPU::V_ASHRREV_I64; 4273 swapOperands(Inst); 4274 } 4275 break; 4276 case AMDGPU::S_LSHR_B64: 4277 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 4278 NewOpcode = AMDGPU::V_LSHRREV_B64; 4279 swapOperands(Inst); 4280 } 4281 break; 4282 4283 case AMDGPU::S_ABS_I32: 4284 lowerScalarAbs(Worklist, Inst); 4285 Inst.eraseFromParent(); 4286 continue; 4287 4288 case AMDGPU::S_CBRANCH_SCC0: 4289 case AMDGPU::S_CBRANCH_SCC1: 4290 // Clear unused bits of vcc 4291 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(AMDGPU::S_AND_B64), 4292 AMDGPU::VCC) 4293 .addReg(AMDGPU::EXEC) 4294 .addReg(AMDGPU::VCC); 4295 break; 4296 4297 case AMDGPU::S_BFE_U64: 4298 case AMDGPU::S_BFM_B64: 4299 llvm_unreachable("Moving this op to VALU not implemented"); 4300 4301 case AMDGPU::S_PACK_LL_B32_B16: 4302 case AMDGPU::S_PACK_LH_B32_B16: 4303 case AMDGPU::S_PACK_HH_B32_B16: 4304 movePackToVALU(Worklist, MRI, Inst); 4305 Inst.eraseFromParent(); 4306 continue; 4307 4308 case AMDGPU::S_XNOR_B32: 4309 lowerScalarXnor(Worklist, Inst); 4310 Inst.eraseFromParent(); 4311 continue; 4312 4313 case AMDGPU::S_NAND_B32: 4314 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_AND_B32); 4315 Inst.eraseFromParent(); 4316 continue; 4317 4318 case AMDGPU::S_NOR_B32: 4319 splitScalarNotBinop(Worklist, Inst, AMDGPU::S_OR_B32); 4320 Inst.eraseFromParent(); 4321 continue; 4322 4323 case AMDGPU::S_ANDN2_B32: 4324 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_AND_B32); 4325 Inst.eraseFromParent(); 4326 continue; 4327 4328 case AMDGPU::S_ORN2_B32: 4329 splitScalarBinOpN2(Worklist, Inst, AMDGPU::S_OR_B32); 4330 Inst.eraseFromParent(); 4331 continue; 4332 } 4333 4334 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) { 4335 // We cannot move this instruction to the VALU, so we should try to 4336 // legalize its operands instead. 4337 legalizeOperands(Inst, MDT); 4338 continue; 4339 } 4340 4341 // Use the new VALU Opcode. 4342 const MCInstrDesc &NewDesc = get(NewOpcode); 4343 Inst.setDesc(NewDesc); 4344 4345 // Remove any references to SCC. Vector instructions can't read from it, and 4346 // We're just about to add the implicit use / defs of VCC, and we don't want 4347 // both. 4348 for (unsigned i = Inst.getNumOperands() - 1; i > 0; --i) { 4349 MachineOperand &Op = Inst.getOperand(i); 4350 if (Op.isReg() && Op.getReg() == AMDGPU::SCC) { 4351 Inst.RemoveOperand(i); 4352 addSCCDefUsersToVALUWorklist(Inst, Worklist); 4353 } 4354 } 4355 4356 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) { 4357 // We are converting these to a BFE, so we need to add the missing 4358 // operands for the size and offset. 4359 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16; 4360 Inst.addOperand(MachineOperand::CreateImm(0)); 4361 Inst.addOperand(MachineOperand::CreateImm(Size)); 4362 4363 } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) { 4364 // The VALU version adds the second operand to the result, so insert an 4365 // extra 0 operand. 4366 Inst.addOperand(MachineOperand::CreateImm(0)); 4367 } 4368 4369 Inst.addImplicitDefUseOperands(*Inst.getParent()->getParent()); 4370 4371 if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) { 4372 const MachineOperand &OffsetWidthOp = Inst.getOperand(2); 4373 // If we need to move this to VGPRs, we need to unpack the second operand 4374 // back into the 2 separate ones for bit offset and width. 4375 assert(OffsetWidthOp.isImm() && 4376 "Scalar BFE is only implemented for constant width and offset"); 4377 uint32_t Imm = OffsetWidthOp.getImm(); 4378 4379 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 4380 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 4381 Inst.RemoveOperand(2); // Remove old immediate. 4382 Inst.addOperand(MachineOperand::CreateImm(Offset)); 4383 Inst.addOperand(MachineOperand::CreateImm(BitWidth)); 4384 } 4385 4386 bool HasDst = Inst.getOperand(0).isReg() && Inst.getOperand(0).isDef(); 4387 unsigned NewDstReg = AMDGPU::NoRegister; 4388 if (HasDst) { 4389 unsigned DstReg = Inst.getOperand(0).getReg(); 4390 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) 4391 continue; 4392 4393 // Update the destination register class. 4394 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst); 4395 if (!NewDstRC) 4396 continue; 4397 4398 if (Inst.isCopy() && 4399 TargetRegisterInfo::isVirtualRegister(Inst.getOperand(1).getReg()) && 4400 NewDstRC == RI.getRegClassForReg(MRI, Inst.getOperand(1).getReg())) { 4401 // Instead of creating a copy where src and dst are the same register 4402 // class, we just replace all uses of dst with src. These kinds of 4403 // copies interfere with the heuristics MachineSink uses to decide 4404 // whether or not to split a critical edge. Since the pass assumes 4405 // that copies will end up as machine instructions and not be 4406 // eliminated. 4407 addUsersToMoveToVALUWorklist(DstReg, MRI, Worklist); 4408 MRI.replaceRegWith(DstReg, Inst.getOperand(1).getReg()); 4409 MRI.clearKillFlags(Inst.getOperand(1).getReg()); 4410 Inst.getOperand(0).setReg(DstReg); 4411 4412 // Make sure we don't leave around a dead VGPR->SGPR copy. Normally 4413 // these are deleted later, but at -O0 it would leave a suspicious 4414 // looking illegal copy of an undef register. 4415 for (unsigned I = Inst.getNumOperands() - 1; I != 0; --I) 4416 Inst.RemoveOperand(I); 4417 Inst.setDesc(get(AMDGPU::IMPLICIT_DEF)); 4418 continue; 4419 } 4420 4421 NewDstReg = MRI.createVirtualRegister(NewDstRC); 4422 MRI.replaceRegWith(DstReg, NewDstReg); 4423 } 4424 4425 // Legalize the operands 4426 legalizeOperands(Inst, MDT); 4427 4428 if (HasDst) 4429 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist); 4430 } 4431 } 4432 4433 // Add/sub require special handling to deal with carry outs. 4434 bool SIInstrInfo::moveScalarAddSub(SetVectorType &Worklist, MachineInstr &Inst, 4435 MachineDominatorTree *MDT) const { 4436 if (ST.hasAddNoCarry()) { 4437 // Assume there is no user of scc since we don't select this in that case. 4438 // Since scc isn't used, it doesn't really matter if the i32 or u32 variant 4439 // is used. 4440 4441 MachineBasicBlock &MBB = *Inst.getParent(); 4442 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4443 4444 unsigned OldDstReg = Inst.getOperand(0).getReg(); 4445 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4446 4447 unsigned Opc = Inst.getOpcode(); 4448 assert(Opc == AMDGPU::S_ADD_I32 || Opc == AMDGPU::S_SUB_I32); 4449 4450 unsigned NewOpc = Opc == AMDGPU::S_ADD_I32 ? 4451 AMDGPU::V_ADD_U32_e64 : AMDGPU::V_SUB_U32_e64; 4452 4453 assert(Inst.getOperand(3).getReg() == AMDGPU::SCC); 4454 Inst.RemoveOperand(3); 4455 4456 Inst.setDesc(get(NewOpc)); 4457 Inst.addImplicitDefUseOperands(*MBB.getParent()); 4458 MRI.replaceRegWith(OldDstReg, ResultReg); 4459 legalizeOperands(Inst, MDT); 4460 4461 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 4462 return true; 4463 } 4464 4465 return false; 4466 } 4467 4468 void SIInstrInfo::lowerScalarAbs(SetVectorType &Worklist, 4469 MachineInstr &Inst) const { 4470 MachineBasicBlock &MBB = *Inst.getParent(); 4471 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4472 MachineBasicBlock::iterator MII = Inst; 4473 DebugLoc DL = Inst.getDebugLoc(); 4474 4475 MachineOperand &Dest = Inst.getOperand(0); 4476 MachineOperand &Src = Inst.getOperand(1); 4477 unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4478 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4479 4480 unsigned SubOp = ST.hasAddNoCarry() ? 4481 AMDGPU::V_SUB_U32_e32 : AMDGPU::V_SUB_I32_e32; 4482 4483 BuildMI(MBB, MII, DL, get(SubOp), TmpReg) 4484 .addImm(0) 4485 .addReg(Src.getReg()); 4486 4487 BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg) 4488 .addReg(Src.getReg()) 4489 .addReg(TmpReg); 4490 4491 MRI.replaceRegWith(Dest.getReg(), ResultReg); 4492 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 4493 } 4494 4495 void SIInstrInfo::lowerScalarXnor(SetVectorType &Worklist, 4496 MachineInstr &Inst) const { 4497 MachineBasicBlock &MBB = *Inst.getParent(); 4498 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4499 MachineBasicBlock::iterator MII = Inst; 4500 const DebugLoc &DL = Inst.getDebugLoc(); 4501 4502 MachineOperand &Dest = Inst.getOperand(0); 4503 MachineOperand &Src0 = Inst.getOperand(1); 4504 MachineOperand &Src1 = Inst.getOperand(2); 4505 4506 if (ST.hasDLInsts()) { 4507 unsigned NewDest = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4508 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src0, MRI, DL); 4509 legalizeGenericOperand(MBB, MII, &AMDGPU::VGPR_32RegClass, Src1, MRI, DL); 4510 4511 BuildMI(MBB, MII, DL, get(AMDGPU::V_XNOR_B32_e64), NewDest) 4512 .add(Src0) 4513 .add(Src1); 4514 4515 MRI.replaceRegWith(Dest.getReg(), NewDest); 4516 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 4517 } else { 4518 // Using the identity !(x ^ y) == (!x ^ y) == (x ^ !y), we can 4519 // invert either source and then perform the XOR. If either source is a 4520 // scalar register, then we can leave the inversion on the scalar unit to 4521 // acheive a better distrubution of scalar and vector instructions. 4522 bool Src0IsSGPR = Src0.isReg() && 4523 RI.isSGPRClass(MRI.getRegClass(Src0.getReg())); 4524 bool Src1IsSGPR = Src1.isReg() && 4525 RI.isSGPRClass(MRI.getRegClass(Src1.getReg())); 4526 MachineInstr *Not = nullptr; 4527 MachineInstr *Xor = nullptr; 4528 unsigned Temp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4529 unsigned NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4530 4531 // Build a pair of scalar instructions and add them to the work list. 4532 // The next iteration over the work list will lower these to the vector 4533 // unit as necessary. 4534 if (Src0IsSGPR) { 4535 Not = BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp) 4536 .add(Src0); 4537 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest) 4538 .addReg(Temp) 4539 .add(Src1); 4540 } else if (Src1IsSGPR) { 4541 Not = BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Temp) 4542 .add(Src1); 4543 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), NewDest) 4544 .add(Src0) 4545 .addReg(Temp); 4546 } else { 4547 Xor = BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B32), Temp) 4548 .add(Src0) 4549 .add(Src1); 4550 Not = BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest) 4551 .addReg(Temp); 4552 Worklist.insert(Not); 4553 } 4554 4555 MRI.replaceRegWith(Dest.getReg(), NewDest); 4556 4557 Worklist.insert(Xor); 4558 4559 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 4560 } 4561 } 4562 4563 void SIInstrInfo::splitScalarNotBinop(SetVectorType &Worklist, 4564 MachineInstr &Inst, 4565 unsigned Opcode) const { 4566 MachineBasicBlock &MBB = *Inst.getParent(); 4567 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4568 MachineBasicBlock::iterator MII = Inst; 4569 const DebugLoc &DL = Inst.getDebugLoc(); 4570 4571 MachineOperand &Dest = Inst.getOperand(0); 4572 MachineOperand &Src0 = Inst.getOperand(1); 4573 MachineOperand &Src1 = Inst.getOperand(2); 4574 4575 unsigned NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4576 unsigned Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4577 4578 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), Interm) 4579 .add(Src0) 4580 .add(Src1); 4581 4582 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), NewDest) 4583 .addReg(Interm); 4584 4585 Worklist.insert(&Op); 4586 Worklist.insert(&Not); 4587 4588 MRI.replaceRegWith(Dest.getReg(), NewDest); 4589 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 4590 } 4591 4592 void SIInstrInfo::splitScalarBinOpN2(SetVectorType& Worklist, 4593 MachineInstr &Inst, 4594 unsigned Opcode) const { 4595 MachineBasicBlock &MBB = *Inst.getParent(); 4596 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4597 MachineBasicBlock::iterator MII = Inst; 4598 const DebugLoc &DL = Inst.getDebugLoc(); 4599 4600 MachineOperand &Dest = Inst.getOperand(0); 4601 MachineOperand &Src0 = Inst.getOperand(1); 4602 MachineOperand &Src1 = Inst.getOperand(2); 4603 4604 unsigned NewDest = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4605 unsigned Interm = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass); 4606 4607 MachineInstr &Not = *BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B32), Interm) 4608 .add(Src1); 4609 4610 MachineInstr &Op = *BuildMI(MBB, MII, DL, get(Opcode), NewDest) 4611 .add(Src0) 4612 .addReg(Interm); 4613 4614 Worklist.insert(&Not); 4615 Worklist.insert(&Op); 4616 4617 MRI.replaceRegWith(Dest.getReg(), NewDest); 4618 addUsersToMoveToVALUWorklist(NewDest, MRI, Worklist); 4619 } 4620 4621 void SIInstrInfo::splitScalar64BitUnaryOp( 4622 SetVectorType &Worklist, MachineInstr &Inst, 4623 unsigned Opcode) const { 4624 MachineBasicBlock &MBB = *Inst.getParent(); 4625 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4626 4627 MachineOperand &Dest = Inst.getOperand(0); 4628 MachineOperand &Src0 = Inst.getOperand(1); 4629 DebugLoc DL = Inst.getDebugLoc(); 4630 4631 MachineBasicBlock::iterator MII = Inst; 4632 4633 const MCInstrDesc &InstDesc = get(Opcode); 4634 const TargetRegisterClass *Src0RC = Src0.isReg() ? 4635 MRI.getRegClass(Src0.getReg()) : 4636 &AMDGPU::SGPR_32RegClass; 4637 4638 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 4639 4640 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 4641 AMDGPU::sub0, Src0SubRC); 4642 4643 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 4644 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 4645 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 4646 4647 unsigned DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 4648 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0).add(SrcReg0Sub0); 4649 4650 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 4651 AMDGPU::sub1, Src0SubRC); 4652 4653 unsigned DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 4654 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1).add(SrcReg0Sub1); 4655 4656 unsigned FullDestReg = MRI.createVirtualRegister(NewDestRC); 4657 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 4658 .addReg(DestSub0) 4659 .addImm(AMDGPU::sub0) 4660 .addReg(DestSub1) 4661 .addImm(AMDGPU::sub1); 4662 4663 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 4664 4665 Worklist.insert(&LoHalf); 4666 Worklist.insert(&HiHalf); 4667 4668 // We don't need to legalizeOperands here because for a single operand, src0 4669 // will support any kind of input. 4670 4671 // Move all users of this moved value. 4672 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 4673 } 4674 4675 void SIInstrInfo::splitScalar64BitAddSub(SetVectorType &Worklist, 4676 MachineInstr &Inst, 4677 MachineDominatorTree *MDT) const { 4678 bool IsAdd = (Inst.getOpcode() == AMDGPU::S_ADD_U64_PSEUDO); 4679 4680 MachineBasicBlock &MBB = *Inst.getParent(); 4681 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4682 4683 unsigned FullDestReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 4684 unsigned DestSub0 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4685 unsigned DestSub1 = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4686 4687 unsigned CarryReg = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 4688 unsigned DeadCarryReg = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass); 4689 4690 MachineOperand &Dest = Inst.getOperand(0); 4691 MachineOperand &Src0 = Inst.getOperand(1); 4692 MachineOperand &Src1 = Inst.getOperand(2); 4693 const DebugLoc &DL = Inst.getDebugLoc(); 4694 MachineBasicBlock::iterator MII = Inst; 4695 4696 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0.getReg()); 4697 const TargetRegisterClass *Src1RC = MRI.getRegClass(Src1.getReg()); 4698 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 4699 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); 4700 4701 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 4702 AMDGPU::sub0, Src0SubRC); 4703 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 4704 AMDGPU::sub0, Src1SubRC); 4705 4706 4707 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 4708 AMDGPU::sub1, Src0SubRC); 4709 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 4710 AMDGPU::sub1, Src1SubRC); 4711 4712 unsigned LoOpc = IsAdd ? AMDGPU::V_ADD_I32_e64 : AMDGPU::V_SUB_I32_e64; 4713 MachineInstr *LoHalf = 4714 BuildMI(MBB, MII, DL, get(LoOpc), DestSub0) 4715 .addReg(CarryReg, RegState::Define) 4716 .add(SrcReg0Sub0) 4717 .add(SrcReg1Sub0); 4718 4719 unsigned HiOpc = IsAdd ? AMDGPU::V_ADDC_U32_e64 : AMDGPU::V_SUBB_U32_e64; 4720 MachineInstr *HiHalf = 4721 BuildMI(MBB, MII, DL, get(HiOpc), DestSub1) 4722 .addReg(DeadCarryReg, RegState::Define | RegState::Dead) 4723 .add(SrcReg0Sub1) 4724 .add(SrcReg1Sub1) 4725 .addReg(CarryReg, RegState::Kill); 4726 4727 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 4728 .addReg(DestSub0) 4729 .addImm(AMDGPU::sub0) 4730 .addReg(DestSub1) 4731 .addImm(AMDGPU::sub1); 4732 4733 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 4734 4735 // Try to legalize the operands in case we need to swap the order to keep it 4736 // valid. 4737 legalizeOperands(*LoHalf, MDT); 4738 legalizeOperands(*HiHalf, MDT); 4739 4740 // Move all users of this moved vlaue. 4741 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 4742 } 4743 4744 void SIInstrInfo::splitScalar64BitBinaryOp(SetVectorType &Worklist, 4745 MachineInstr &Inst, unsigned Opcode, 4746 MachineDominatorTree *MDT) const { 4747 MachineBasicBlock &MBB = *Inst.getParent(); 4748 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4749 4750 MachineOperand &Dest = Inst.getOperand(0); 4751 MachineOperand &Src0 = Inst.getOperand(1); 4752 MachineOperand &Src1 = Inst.getOperand(2); 4753 DebugLoc DL = Inst.getDebugLoc(); 4754 4755 MachineBasicBlock::iterator MII = Inst; 4756 4757 const MCInstrDesc &InstDesc = get(Opcode); 4758 const TargetRegisterClass *Src0RC = Src0.isReg() ? 4759 MRI.getRegClass(Src0.getReg()) : 4760 &AMDGPU::SGPR_32RegClass; 4761 4762 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 4763 const TargetRegisterClass *Src1RC = Src1.isReg() ? 4764 MRI.getRegClass(Src1.getReg()) : 4765 &AMDGPU::SGPR_32RegClass; 4766 4767 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); 4768 4769 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 4770 AMDGPU::sub0, Src0SubRC); 4771 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 4772 AMDGPU::sub0, Src1SubRC); 4773 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 4774 AMDGPU::sub1, Src0SubRC); 4775 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 4776 AMDGPU::sub1, Src1SubRC); 4777 4778 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 4779 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 4780 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 4781 4782 unsigned DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 4783 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0) 4784 .add(SrcReg0Sub0) 4785 .add(SrcReg1Sub0); 4786 4787 unsigned DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 4788 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1) 4789 .add(SrcReg0Sub1) 4790 .add(SrcReg1Sub1); 4791 4792 unsigned FullDestReg = MRI.createVirtualRegister(NewDestRC); 4793 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 4794 .addReg(DestSub0) 4795 .addImm(AMDGPU::sub0) 4796 .addReg(DestSub1) 4797 .addImm(AMDGPU::sub1); 4798 4799 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 4800 4801 Worklist.insert(&LoHalf); 4802 Worklist.insert(&HiHalf); 4803 4804 // Move all users of this moved vlaue. 4805 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 4806 } 4807 4808 void SIInstrInfo::splitScalar64BitXnor(SetVectorType &Worklist, 4809 MachineInstr &Inst, 4810 MachineDominatorTree *MDT) const { 4811 MachineBasicBlock &MBB = *Inst.getParent(); 4812 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4813 4814 MachineOperand &Dest = Inst.getOperand(0); 4815 MachineOperand &Src0 = Inst.getOperand(1); 4816 MachineOperand &Src1 = Inst.getOperand(2); 4817 const DebugLoc &DL = Inst.getDebugLoc(); 4818 4819 MachineBasicBlock::iterator MII = Inst; 4820 4821 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 4822 4823 unsigned Interm = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 4824 4825 MachineOperand* Op0; 4826 MachineOperand* Op1; 4827 4828 if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) { 4829 Op0 = &Src0; 4830 Op1 = &Src1; 4831 } else { 4832 Op0 = &Src1; 4833 Op1 = &Src0; 4834 } 4835 4836 BuildMI(MBB, MII, DL, get(AMDGPU::S_NOT_B64), Interm) 4837 .add(*Op0); 4838 4839 unsigned NewDest = MRI.createVirtualRegister(DestRC); 4840 4841 MachineInstr &Xor = *BuildMI(MBB, MII, DL, get(AMDGPU::S_XOR_B64), NewDest) 4842 .addReg(Interm) 4843 .add(*Op1); 4844 4845 MRI.replaceRegWith(Dest.getReg(), NewDest); 4846 4847 Worklist.insert(&Xor); 4848 } 4849 4850 void SIInstrInfo::splitScalar64BitBCNT( 4851 SetVectorType &Worklist, MachineInstr &Inst) const { 4852 MachineBasicBlock &MBB = *Inst.getParent(); 4853 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4854 4855 MachineBasicBlock::iterator MII = Inst; 4856 const DebugLoc &DL = Inst.getDebugLoc(); 4857 4858 MachineOperand &Dest = Inst.getOperand(0); 4859 MachineOperand &Src = Inst.getOperand(1); 4860 4861 const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64); 4862 const TargetRegisterClass *SrcRC = Src.isReg() ? 4863 MRI.getRegClass(Src.getReg()) : 4864 &AMDGPU::SGPR_32RegClass; 4865 4866 unsigned MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4867 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4868 4869 const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0); 4870 4871 MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 4872 AMDGPU::sub0, SrcSubRC); 4873 MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 4874 AMDGPU::sub1, SrcSubRC); 4875 4876 BuildMI(MBB, MII, DL, InstDesc, MidReg).add(SrcRegSub0).addImm(0); 4877 4878 BuildMI(MBB, MII, DL, InstDesc, ResultReg).add(SrcRegSub1).addReg(MidReg); 4879 4880 MRI.replaceRegWith(Dest.getReg(), ResultReg); 4881 4882 // We don't need to legalize operands here. src0 for etiher instruction can be 4883 // an SGPR, and the second input is unused or determined here. 4884 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 4885 } 4886 4887 void SIInstrInfo::splitScalar64BitBFE(SetVectorType &Worklist, 4888 MachineInstr &Inst) const { 4889 MachineBasicBlock &MBB = *Inst.getParent(); 4890 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 4891 MachineBasicBlock::iterator MII = Inst; 4892 const DebugLoc &DL = Inst.getDebugLoc(); 4893 4894 MachineOperand &Dest = Inst.getOperand(0); 4895 uint32_t Imm = Inst.getOperand(2).getImm(); 4896 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 4897 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 4898 4899 (void) Offset; 4900 4901 // Only sext_inreg cases handled. 4902 assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 && 4903 Offset == 0 && "Not implemented"); 4904 4905 if (BitWidth < 32) { 4906 unsigned MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4907 unsigned MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4908 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 4909 4910 BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32), MidRegLo) 4911 .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0) 4912 .addImm(0) 4913 .addImm(BitWidth); 4914 4915 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi) 4916 .addImm(31) 4917 .addReg(MidRegLo); 4918 4919 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 4920 .addReg(MidRegLo) 4921 .addImm(AMDGPU::sub0) 4922 .addReg(MidRegHi) 4923 .addImm(AMDGPU::sub1); 4924 4925 MRI.replaceRegWith(Dest.getReg(), ResultReg); 4926 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 4927 return; 4928 } 4929 4930 MachineOperand &Src = Inst.getOperand(1); 4931 unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4932 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 4933 4934 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg) 4935 .addImm(31) 4936 .addReg(Src.getReg(), 0, AMDGPU::sub0); 4937 4938 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 4939 .addReg(Src.getReg(), 0, AMDGPU::sub0) 4940 .addImm(AMDGPU::sub0) 4941 .addReg(TmpReg) 4942 .addImm(AMDGPU::sub1); 4943 4944 MRI.replaceRegWith(Dest.getReg(), ResultReg); 4945 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 4946 } 4947 4948 void SIInstrInfo::addUsersToMoveToVALUWorklist( 4949 unsigned DstReg, 4950 MachineRegisterInfo &MRI, 4951 SetVectorType &Worklist) const { 4952 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg), 4953 E = MRI.use_end(); I != E;) { 4954 MachineInstr &UseMI = *I->getParent(); 4955 if (!canReadVGPR(UseMI, I.getOperandNo())) { 4956 Worklist.insert(&UseMI); 4957 4958 do { 4959 ++I; 4960 } while (I != E && I->getParent() == &UseMI); 4961 } else { 4962 ++I; 4963 } 4964 } 4965 } 4966 4967 void SIInstrInfo::movePackToVALU(SetVectorType &Worklist, 4968 MachineRegisterInfo &MRI, 4969 MachineInstr &Inst) const { 4970 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4971 MachineBasicBlock *MBB = Inst.getParent(); 4972 MachineOperand &Src0 = Inst.getOperand(1); 4973 MachineOperand &Src1 = Inst.getOperand(2); 4974 const DebugLoc &DL = Inst.getDebugLoc(); 4975 4976 switch (Inst.getOpcode()) { 4977 case AMDGPU::S_PACK_LL_B32_B16: { 4978 unsigned ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4979 unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4980 4981 // FIXME: Can do a lot better if we know the high bits of src0 or src1 are 4982 // 0. 4983 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 4984 .addImm(0xffff); 4985 4986 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_B32_e64), TmpReg) 4987 .addReg(ImmReg, RegState::Kill) 4988 .add(Src0); 4989 4990 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHL_OR_B32), ResultReg) 4991 .add(Src1) 4992 .addImm(16) 4993 .addReg(TmpReg, RegState::Kill); 4994 break; 4995 } 4996 case AMDGPU::S_PACK_LH_B32_B16: { 4997 unsigned ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 4998 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 4999 .addImm(0xffff); 5000 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_BFI_B32), ResultReg) 5001 .addReg(ImmReg, RegState::Kill) 5002 .add(Src0) 5003 .add(Src1); 5004 break; 5005 } 5006 case AMDGPU::S_PACK_HH_B32_B16: { 5007 unsigned ImmReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 5008 unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 5009 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_LSHRREV_B32_e64), TmpReg) 5010 .addImm(16) 5011 .add(Src0); 5012 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_MOV_B32_e32), ImmReg) 5013 .addImm(0xffff0000); 5014 BuildMI(*MBB, Inst, DL, get(AMDGPU::V_AND_OR_B32), ResultReg) 5015 .add(Src1) 5016 .addReg(ImmReg, RegState::Kill) 5017 .addReg(TmpReg, RegState::Kill); 5018 break; 5019 } 5020 default: 5021 llvm_unreachable("unhandled s_pack_* instruction"); 5022 } 5023 5024 MachineOperand &Dest = Inst.getOperand(0); 5025 MRI.replaceRegWith(Dest.getReg(), ResultReg); 5026 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 5027 } 5028 5029 void SIInstrInfo::addSCCDefUsersToVALUWorklist( 5030 MachineInstr &SCCDefInst, SetVectorType &Worklist) const { 5031 // This assumes that all the users of SCC are in the same block 5032 // as the SCC def. 5033 for (MachineInstr &MI : 5034 make_range(MachineBasicBlock::iterator(SCCDefInst), 5035 SCCDefInst.getParent()->end())) { 5036 // Exit if we find another SCC def. 5037 if (MI.findRegisterDefOperandIdx(AMDGPU::SCC, false, false, &RI) != -1) 5038 return; 5039 5040 if (MI.findRegisterUseOperandIdx(AMDGPU::SCC, false, &RI) != -1) 5041 Worklist.insert(&MI); 5042 } 5043 } 5044 5045 const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass( 5046 const MachineInstr &Inst) const { 5047 const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0); 5048 5049 switch (Inst.getOpcode()) { 5050 // For target instructions, getOpRegClass just returns the virtual register 5051 // class associated with the operand, so we need to find an equivalent VGPR 5052 // register class in order to move the instruction to the VALU. 5053 case AMDGPU::COPY: 5054 case AMDGPU::PHI: 5055 case AMDGPU::REG_SEQUENCE: 5056 case AMDGPU::INSERT_SUBREG: 5057 case AMDGPU::WQM: 5058 case AMDGPU::WWM: 5059 if (RI.hasVGPRs(NewDstRC)) 5060 return nullptr; 5061 5062 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 5063 if (!NewDstRC) 5064 return nullptr; 5065 return NewDstRC; 5066 default: 5067 return NewDstRC; 5068 } 5069 } 5070 5071 // Find the one SGPR operand we are allowed to use. 5072 unsigned SIInstrInfo::findUsedSGPR(const MachineInstr &MI, 5073 int OpIndices[3]) const { 5074 const MCInstrDesc &Desc = MI.getDesc(); 5075 5076 // Find the one SGPR operand we are allowed to use. 5077 // 5078 // First we need to consider the instruction's operand requirements before 5079 // legalizing. Some operands are required to be SGPRs, such as implicit uses 5080 // of VCC, but we are still bound by the constant bus requirement to only use 5081 // one. 5082 // 5083 // If the operand's class is an SGPR, we can never move it. 5084 5085 unsigned SGPRReg = findImplicitSGPRRead(MI); 5086 if (SGPRReg != AMDGPU::NoRegister) 5087 return SGPRReg; 5088 5089 unsigned UsedSGPRs[3] = { AMDGPU::NoRegister }; 5090 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 5091 5092 for (unsigned i = 0; i < 3; ++i) { 5093 int Idx = OpIndices[i]; 5094 if (Idx == -1) 5095 break; 5096 5097 const MachineOperand &MO = MI.getOperand(Idx); 5098 if (!MO.isReg()) 5099 continue; 5100 5101 // Is this operand statically required to be an SGPR based on the operand 5102 // constraints? 5103 const TargetRegisterClass *OpRC = RI.getRegClass(Desc.OpInfo[Idx].RegClass); 5104 bool IsRequiredSGPR = RI.isSGPRClass(OpRC); 5105 if (IsRequiredSGPR) 5106 return MO.getReg(); 5107 5108 // If this could be a VGPR or an SGPR, Check the dynamic register class. 5109 unsigned Reg = MO.getReg(); 5110 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg); 5111 if (RI.isSGPRClass(RegRC)) 5112 UsedSGPRs[i] = Reg; 5113 } 5114 5115 // We don't have a required SGPR operand, so we have a bit more freedom in 5116 // selecting operands to move. 5117 5118 // Try to select the most used SGPR. If an SGPR is equal to one of the 5119 // others, we choose that. 5120 // 5121 // e.g. 5122 // V_FMA_F32 v0, s0, s0, s0 -> No moves 5123 // V_FMA_F32 v0, s0, s1, s0 -> Move s1 5124 5125 // TODO: If some of the operands are 64-bit SGPRs and some 32, we should 5126 // prefer those. 5127 5128 if (UsedSGPRs[0] != AMDGPU::NoRegister) { 5129 if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2]) 5130 SGPRReg = UsedSGPRs[0]; 5131 } 5132 5133 if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) { 5134 if (UsedSGPRs[1] == UsedSGPRs[2]) 5135 SGPRReg = UsedSGPRs[1]; 5136 } 5137 5138 return SGPRReg; 5139 } 5140 5141 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI, 5142 unsigned OperandName) const { 5143 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName); 5144 if (Idx == -1) 5145 return nullptr; 5146 5147 return &MI.getOperand(Idx); 5148 } 5149 5150 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const { 5151 uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT; 5152 if (ST.isAmdHsaOS()) { 5153 // Set ATC = 1. GFX9 doesn't have this bit. 5154 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) 5155 RsrcDataFormat |= (1ULL << 56); 5156 5157 // Set MTYPE = 2 (MTYPE_UC = uncached). GFX9 doesn't have this. 5158 // BTW, it disables TC L2 and therefore decreases performance. 5159 if (ST.getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) 5160 RsrcDataFormat |= (2ULL << 59); 5161 } 5162 5163 return RsrcDataFormat; 5164 } 5165 5166 uint64_t SIInstrInfo::getScratchRsrcWords23() const { 5167 uint64_t Rsrc23 = getDefaultRsrcDataFormat() | 5168 AMDGPU::RSRC_TID_ENABLE | 5169 0xffffffff; // Size; 5170 5171 // GFX9 doesn't have ELEMENT_SIZE. 5172 if (ST.getGeneration() <= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 5173 uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize()) - 1; 5174 Rsrc23 |= EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT; 5175 } 5176 5177 // IndexStride = 64. 5178 Rsrc23 |= UINT64_C(3) << AMDGPU::RSRC_INDEX_STRIDE_SHIFT; 5179 5180 // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17]. 5181 // Clear them unless we want a huge stride. 5182 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 5183 Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT; 5184 5185 return Rsrc23; 5186 } 5187 5188 bool SIInstrInfo::isLowLatencyInstruction(const MachineInstr &MI) const { 5189 unsigned Opc = MI.getOpcode(); 5190 5191 return isSMRD(Opc); 5192 } 5193 5194 bool SIInstrInfo::isHighLatencyInstruction(const MachineInstr &MI) const { 5195 unsigned Opc = MI.getOpcode(); 5196 5197 return isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc); 5198 } 5199 5200 unsigned SIInstrInfo::isStackAccess(const MachineInstr &MI, 5201 int &FrameIndex) const { 5202 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 5203 if (!Addr || !Addr->isFI()) 5204 return AMDGPU::NoRegister; 5205 5206 assert(!MI.memoperands_empty() && 5207 (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS); 5208 5209 FrameIndex = Addr->getIndex(); 5210 return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg(); 5211 } 5212 5213 unsigned SIInstrInfo::isSGPRStackAccess(const MachineInstr &MI, 5214 int &FrameIndex) const { 5215 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr); 5216 assert(Addr && Addr->isFI()); 5217 FrameIndex = Addr->getIndex(); 5218 return getNamedOperand(MI, AMDGPU::OpName::data)->getReg(); 5219 } 5220 5221 unsigned SIInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 5222 int &FrameIndex) const { 5223 if (!MI.mayLoad()) 5224 return AMDGPU::NoRegister; 5225 5226 if (isMUBUF(MI) || isVGPRSpill(MI)) 5227 return isStackAccess(MI, FrameIndex); 5228 5229 if (isSGPRSpill(MI)) 5230 return isSGPRStackAccess(MI, FrameIndex); 5231 5232 return AMDGPU::NoRegister; 5233 } 5234 5235 unsigned SIInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 5236 int &FrameIndex) const { 5237 if (!MI.mayStore()) 5238 return AMDGPU::NoRegister; 5239 5240 if (isMUBUF(MI) || isVGPRSpill(MI)) 5241 return isStackAccess(MI, FrameIndex); 5242 5243 if (isSGPRSpill(MI)) 5244 return isSGPRStackAccess(MI, FrameIndex); 5245 5246 return AMDGPU::NoRegister; 5247 } 5248 5249 unsigned SIInstrInfo::getInstBundleSize(const MachineInstr &MI) const { 5250 unsigned Size = 0; 5251 MachineBasicBlock::const_instr_iterator I = MI.getIterator(); 5252 MachineBasicBlock::const_instr_iterator E = MI.getParent()->instr_end(); 5253 while (++I != E && I->isInsideBundle()) { 5254 assert(!I->isBundle() && "No nested bundle!"); 5255 Size += getInstSizeInBytes(*I); 5256 } 5257 5258 return Size; 5259 } 5260 5261 unsigned SIInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 5262 unsigned Opc = MI.getOpcode(); 5263 const MCInstrDesc &Desc = getMCOpcodeFromPseudo(Opc); 5264 unsigned DescSize = Desc.getSize(); 5265 5266 // If we have a definitive size, we can use it. Otherwise we need to inspect 5267 // the operands to know the size. 5268 if (isFixedSize(MI)) 5269 return DescSize; 5270 5271 // 4-byte instructions may have a 32-bit literal encoded after them. Check 5272 // operands that coud ever be literals. 5273 if (isVALU(MI) || isSALU(MI)) { 5274 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 5275 if (Src0Idx == -1) 5276 return DescSize; // No operands. 5277 5278 if (isLiteralConstantLike(MI.getOperand(Src0Idx), Desc.OpInfo[Src0Idx])) 5279 return DescSize + 4; 5280 5281 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 5282 if (Src1Idx == -1) 5283 return DescSize; 5284 5285 if (isLiteralConstantLike(MI.getOperand(Src1Idx), Desc.OpInfo[Src1Idx])) 5286 return DescSize + 4; 5287 5288 int Src2Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2); 5289 if (Src2Idx == -1) 5290 return DescSize; 5291 5292 if (isLiteralConstantLike(MI.getOperand(Src2Idx), Desc.OpInfo[Src2Idx])) 5293 return DescSize + 4; 5294 5295 return DescSize; 5296 } 5297 5298 switch (Opc) { 5299 case TargetOpcode::IMPLICIT_DEF: 5300 case TargetOpcode::KILL: 5301 case TargetOpcode::DBG_VALUE: 5302 case TargetOpcode::EH_LABEL: 5303 return 0; 5304 case TargetOpcode::BUNDLE: 5305 return getInstBundleSize(MI); 5306 case TargetOpcode::INLINEASM: { 5307 const MachineFunction *MF = MI.getParent()->getParent(); 5308 const char *AsmStr = MI.getOperand(0).getSymbolName(); 5309 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); 5310 } 5311 default: 5312 return DescSize; 5313 } 5314 } 5315 5316 bool SIInstrInfo::mayAccessFlatAddressSpace(const MachineInstr &MI) const { 5317 if (!isFLAT(MI)) 5318 return false; 5319 5320 if (MI.memoperands_empty()) 5321 return true; 5322 5323 for (const MachineMemOperand *MMO : MI.memoperands()) { 5324 if (MMO->getAddrSpace() == AMDGPUAS::FLAT_ADDRESS) 5325 return true; 5326 } 5327 return false; 5328 } 5329 5330 bool SIInstrInfo::isNonUniformBranchInstr(MachineInstr &Branch) const { 5331 return Branch.getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO; 5332 } 5333 5334 void SIInstrInfo::convertNonUniformIfRegion(MachineBasicBlock *IfEntry, 5335 MachineBasicBlock *IfEnd) const { 5336 MachineBasicBlock::iterator TI = IfEntry->getFirstTerminator(); 5337 assert(TI != IfEntry->end()); 5338 5339 MachineInstr *Branch = &(*TI); 5340 MachineFunction *MF = IfEntry->getParent(); 5341 MachineRegisterInfo &MRI = IfEntry->getParent()->getRegInfo(); 5342 5343 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 5344 unsigned DstReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 5345 MachineInstr *SIIF = 5346 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_IF), DstReg) 5347 .add(Branch->getOperand(0)) 5348 .add(Branch->getOperand(1)); 5349 MachineInstr *SIEND = 5350 BuildMI(*MF, Branch->getDebugLoc(), get(AMDGPU::SI_END_CF)) 5351 .addReg(DstReg); 5352 5353 IfEntry->erase(TI); 5354 IfEntry->insert(IfEntry->end(), SIIF); 5355 IfEnd->insert(IfEnd->getFirstNonPHI(), SIEND); 5356 } 5357 } 5358 5359 void SIInstrInfo::convertNonUniformLoopRegion( 5360 MachineBasicBlock *LoopEntry, MachineBasicBlock *LoopEnd) const { 5361 MachineBasicBlock::iterator TI = LoopEnd->getFirstTerminator(); 5362 // We expect 2 terminators, one conditional and one unconditional. 5363 assert(TI != LoopEnd->end()); 5364 5365 MachineInstr *Branch = &(*TI); 5366 MachineFunction *MF = LoopEnd->getParent(); 5367 MachineRegisterInfo &MRI = LoopEnd->getParent()->getRegInfo(); 5368 5369 if (Branch->getOpcode() == AMDGPU::SI_NON_UNIFORM_BRCOND_PSEUDO) { 5370 5371 unsigned DstReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 5372 unsigned BackEdgeReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 5373 MachineInstrBuilder HeaderPHIBuilder = 5374 BuildMI(*(MF), Branch->getDebugLoc(), get(TargetOpcode::PHI), DstReg); 5375 for (MachineBasicBlock::pred_iterator PI = LoopEntry->pred_begin(), 5376 E = LoopEntry->pred_end(); 5377 PI != E; ++PI) { 5378 if (*PI == LoopEnd) { 5379 HeaderPHIBuilder.addReg(BackEdgeReg); 5380 } else { 5381 MachineBasicBlock *PMBB = *PI; 5382 unsigned ZeroReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 5383 materializeImmediate(*PMBB, PMBB->getFirstTerminator(), DebugLoc(), 5384 ZeroReg, 0); 5385 HeaderPHIBuilder.addReg(ZeroReg); 5386 } 5387 HeaderPHIBuilder.addMBB(*PI); 5388 } 5389 MachineInstr *HeaderPhi = HeaderPHIBuilder; 5390 MachineInstr *SIIFBREAK = BuildMI(*(MF), Branch->getDebugLoc(), 5391 get(AMDGPU::SI_IF_BREAK), BackEdgeReg) 5392 .addReg(DstReg) 5393 .add(Branch->getOperand(0)); 5394 MachineInstr *SILOOP = 5395 BuildMI(*(MF), Branch->getDebugLoc(), get(AMDGPU::SI_LOOP)) 5396 .addReg(BackEdgeReg) 5397 .addMBB(LoopEntry); 5398 5399 LoopEntry->insert(LoopEntry->begin(), HeaderPhi); 5400 LoopEnd->erase(TI); 5401 LoopEnd->insert(LoopEnd->end(), SIIFBREAK); 5402 LoopEnd->insert(LoopEnd->end(), SILOOP); 5403 } 5404 } 5405 5406 ArrayRef<std::pair<int, const char *>> 5407 SIInstrInfo::getSerializableTargetIndices() const { 5408 static const std::pair<int, const char *> TargetIndices[] = { 5409 {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"}, 5410 {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"}, 5411 {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"}, 5412 {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"}, 5413 {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}}; 5414 return makeArrayRef(TargetIndices); 5415 } 5416 5417 /// This is used by the post-RA scheduler (SchedulePostRAList.cpp). The 5418 /// post-RA version of misched uses CreateTargetMIHazardRecognizer. 5419 ScheduleHazardRecognizer * 5420 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 5421 const ScheduleDAG *DAG) const { 5422 return new GCNHazardRecognizer(DAG->MF); 5423 } 5424 5425 /// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer 5426 /// pass. 5427 ScheduleHazardRecognizer * 5428 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const { 5429 return new GCNHazardRecognizer(MF); 5430 } 5431 5432 std::pair<unsigned, unsigned> 5433 SIInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const { 5434 return std::make_pair(TF & MO_MASK, TF & ~MO_MASK); 5435 } 5436 5437 ArrayRef<std::pair<unsigned, const char *>> 5438 SIInstrInfo::getSerializableDirectMachineOperandTargetFlags() const { 5439 static const std::pair<unsigned, const char *> TargetFlags[] = { 5440 { MO_GOTPCREL, "amdgpu-gotprel" }, 5441 { MO_GOTPCREL32_LO, "amdgpu-gotprel32-lo" }, 5442 { MO_GOTPCREL32_HI, "amdgpu-gotprel32-hi" }, 5443 { MO_REL32_LO, "amdgpu-rel32-lo" }, 5444 { MO_REL32_HI, "amdgpu-rel32-hi" } 5445 }; 5446 5447 return makeArrayRef(TargetFlags); 5448 } 5449 5450 bool SIInstrInfo::isBasicBlockPrologue(const MachineInstr &MI) const { 5451 return !MI.isTerminator() && MI.getOpcode() != AMDGPU::COPY && 5452 MI.modifiesRegister(AMDGPU::EXEC, &RI); 5453 } 5454 5455 MachineInstrBuilder 5456 SIInstrInfo::getAddNoCarry(MachineBasicBlock &MBB, 5457 MachineBasicBlock::iterator I, 5458 const DebugLoc &DL, 5459 unsigned DestReg) const { 5460 if (ST.hasAddNoCarry()) 5461 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_U32_e64), DestReg); 5462 5463 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 5464 unsigned UnusedCarry = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 5465 MRI.setRegAllocationHint(UnusedCarry, 0, AMDGPU::VCC); 5466 5467 return BuildMI(MBB, I, DL, get(AMDGPU::V_ADD_I32_e64), DestReg) 5468 .addReg(UnusedCarry, RegState::Define | RegState::Dead); 5469 } 5470 5471 bool SIInstrInfo::isKillTerminator(unsigned Opcode) { 5472 switch (Opcode) { 5473 case AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR: 5474 case AMDGPU::SI_KILL_I1_TERMINATOR: 5475 return true; 5476 default: 5477 return false; 5478 } 5479 } 5480 5481 const MCInstrDesc &SIInstrInfo::getKillTerminatorFromPseudo(unsigned Opcode) const { 5482 switch (Opcode) { 5483 case AMDGPU::SI_KILL_F32_COND_IMM_PSEUDO: 5484 return get(AMDGPU::SI_KILL_F32_COND_IMM_TERMINATOR); 5485 case AMDGPU::SI_KILL_I1_PSEUDO: 5486 return get(AMDGPU::SI_KILL_I1_TERMINATOR); 5487 default: 5488 llvm_unreachable("invalid opcode, expected SI_KILL_*_PSEUDO"); 5489 } 5490 } 5491 5492 bool SIInstrInfo::isBufferSMRD(const MachineInstr &MI) const { 5493 if (!isSMRD(MI)) 5494 return false; 5495 5496 // Check that it is using a buffer resource. 5497 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::sbase); 5498 if (Idx == -1) // e.g. s_memtime 5499 return false; 5500 5501 const auto RCID = MI.getDesc().OpInfo[Idx].RegClass; 5502 return RCID == AMDGPU::SReg_128RegClassID; 5503 } 5504 5505 // This must be kept in sync with the SIEncodingFamily class in SIInstrInfo.td 5506 enum SIEncodingFamily { 5507 SI = 0, 5508 VI = 1, 5509 SDWA = 2, 5510 SDWA9 = 3, 5511 GFX80 = 4, 5512 GFX9 = 5 5513 }; 5514 5515 static SIEncodingFamily subtargetEncodingFamily(const GCNSubtarget &ST) { 5516 switch (ST.getGeneration()) { 5517 default: 5518 break; 5519 case AMDGPUSubtarget::SOUTHERN_ISLANDS: 5520 case AMDGPUSubtarget::SEA_ISLANDS: 5521 return SIEncodingFamily::SI; 5522 case AMDGPUSubtarget::VOLCANIC_ISLANDS: 5523 case AMDGPUSubtarget::GFX9: 5524 return SIEncodingFamily::VI; 5525 } 5526 llvm_unreachable("Unknown subtarget generation!"); 5527 } 5528 5529 int SIInstrInfo::pseudoToMCOpcode(int Opcode) const { 5530 SIEncodingFamily Gen = subtargetEncodingFamily(ST); 5531 5532 if ((get(Opcode).TSFlags & SIInstrFlags::renamedInGFX9) != 0 && 5533 ST.getGeneration() >= AMDGPUSubtarget::GFX9) 5534 Gen = SIEncodingFamily::GFX9; 5535 5536 if (get(Opcode).TSFlags & SIInstrFlags::SDWA) 5537 Gen = ST.getGeneration() == AMDGPUSubtarget::GFX9 ? SIEncodingFamily::SDWA9 5538 : SIEncodingFamily::SDWA; 5539 // Adjust the encoding family to GFX80 for D16 buffer instructions when the 5540 // subtarget has UnpackedD16VMem feature. 5541 // TODO: remove this when we discard GFX80 encoding. 5542 if (ST.hasUnpackedD16VMem() && (get(Opcode).TSFlags & SIInstrFlags::D16Buf)) 5543 Gen = SIEncodingFamily::GFX80; 5544 5545 int MCOp = AMDGPU::getMCOpcode(Opcode, Gen); 5546 5547 // -1 means that Opcode is already a native instruction. 5548 if (MCOp == -1) 5549 return Opcode; 5550 5551 // (uint16_t)-1 means that Opcode is a pseudo instruction that has 5552 // no encoding in the given subtarget generation. 5553 if (MCOp == (uint16_t)-1) 5554 return -1; 5555 5556 return MCOp; 5557 } 5558 5559 static 5560 TargetInstrInfo::RegSubRegPair getRegOrUndef(const MachineOperand &RegOpnd) { 5561 assert(RegOpnd.isReg()); 5562 return RegOpnd.isUndef() ? TargetInstrInfo::RegSubRegPair() : 5563 getRegSubRegPair(RegOpnd); 5564 } 5565 5566 TargetInstrInfo::RegSubRegPair 5567 llvm::getRegSequenceSubReg(MachineInstr &MI, unsigned SubReg) { 5568 assert(MI.isRegSequence()); 5569 for (unsigned I = 0, E = (MI.getNumOperands() - 1)/ 2; I < E; ++I) 5570 if (MI.getOperand(1 + 2 * I + 1).getImm() == SubReg) { 5571 auto &RegOp = MI.getOperand(1 + 2 * I); 5572 return getRegOrUndef(RegOp); 5573 } 5574 return TargetInstrInfo::RegSubRegPair(); 5575 } 5576 5577 // Try to find the definition of reg:subreg in subreg-manipulation pseudos 5578 // Following a subreg of reg:subreg isn't supported 5579 static bool followSubRegDef(MachineInstr &MI, 5580 TargetInstrInfo::RegSubRegPair &RSR) { 5581 if (!RSR.SubReg) 5582 return false; 5583 switch (MI.getOpcode()) { 5584 default: break; 5585 case AMDGPU::REG_SEQUENCE: 5586 RSR = getRegSequenceSubReg(MI, RSR.SubReg); 5587 return true; 5588 // EXTRACT_SUBREG ins't supported as this would follow a subreg of subreg 5589 case AMDGPU::INSERT_SUBREG: 5590 if (RSR.SubReg == (unsigned)MI.getOperand(3).getImm()) 5591 // inserted the subreg we're looking for 5592 RSR = getRegOrUndef(MI.getOperand(2)); 5593 else { // the subreg in the rest of the reg 5594 auto R1 = getRegOrUndef(MI.getOperand(1)); 5595 if (R1.SubReg) // subreg of subreg isn't supported 5596 return false; 5597 RSR.Reg = R1.Reg; 5598 } 5599 return true; 5600 } 5601 return false; 5602 } 5603 5604 MachineInstr *llvm::getVRegSubRegDef(const TargetInstrInfo::RegSubRegPair &P, 5605 MachineRegisterInfo &MRI) { 5606 assert(MRI.isSSA()); 5607 if (!TargetRegisterInfo::isVirtualRegister(P.Reg)) 5608 return nullptr; 5609 5610 auto RSR = P; 5611 auto *DefInst = MRI.getVRegDef(RSR.Reg); 5612 while (auto *MI = DefInst) { 5613 DefInst = nullptr; 5614 switch (MI->getOpcode()) { 5615 case AMDGPU::COPY: 5616 case AMDGPU::V_MOV_B32_e32: { 5617 auto &Op1 = MI->getOperand(1); 5618 if (Op1.isReg() && 5619 TargetRegisterInfo::isVirtualRegister(Op1.getReg())) { 5620 if (Op1.isUndef()) 5621 return nullptr; 5622 RSR = getRegSubRegPair(Op1); 5623 DefInst = MRI.getVRegDef(RSR.Reg); 5624 } 5625 break; 5626 } 5627 default: 5628 if (followSubRegDef(*MI, RSR)) { 5629 if (!RSR.Reg) 5630 return nullptr; 5631 DefInst = MRI.getVRegDef(RSR.Reg); 5632 } 5633 } 5634 if (!DefInst) 5635 return MI; 5636 } 5637 return nullptr; 5638 } 5639