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