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