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