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