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