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