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