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