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 bool SIInstrInfo::isSchedulingBoundary(const MachineInstr &MI, 1649 const MachineBasicBlock *MBB, 1650 const MachineFunction &MF) const { 1651 // XXX - Do we want the SP check in the base implementation? 1652 1653 // Target-independent instructions do not have an implicit-use of EXEC, even 1654 // when they operate on VGPRs. Treating EXEC modifications as scheduling 1655 // boundaries prevents incorrect movements of such instructions. 1656 return TargetInstrInfo::isSchedulingBoundary(MI, MBB, MF) || 1657 MI.modifiesRegister(AMDGPU::EXEC, &RI); 1658 } 1659 1660 bool SIInstrInfo::isInlineConstant(const APInt &Imm) const { 1661 int64_t SVal = Imm.getSExtValue(); 1662 if (SVal >= -16 && SVal <= 64) 1663 return true; 1664 1665 if (Imm.getBitWidth() == 64) { 1666 uint64_t Val = Imm.getZExtValue(); 1667 return (DoubleToBits(0.0) == Val) || 1668 (DoubleToBits(1.0) == Val) || 1669 (DoubleToBits(-1.0) == Val) || 1670 (DoubleToBits(0.5) == Val) || 1671 (DoubleToBits(-0.5) == Val) || 1672 (DoubleToBits(2.0) == Val) || 1673 (DoubleToBits(-2.0) == Val) || 1674 (DoubleToBits(4.0) == Val) || 1675 (DoubleToBits(-4.0) == Val); 1676 } 1677 1678 // The actual type of the operand does not seem to matter as long 1679 // as the bits match one of the inline immediate values. For example: 1680 // 1681 // -nan has the hexadecimal encoding of 0xfffffffe which is -2 in decimal, 1682 // so it is a legal inline immediate. 1683 // 1684 // 1065353216 has the hexadecimal encoding 0x3f800000 which is 1.0f in 1685 // floating-point, so it is a legal inline immediate. 1686 uint32_t Val = Imm.getZExtValue(); 1687 1688 return (FloatToBits(0.0f) == Val) || 1689 (FloatToBits(1.0f) == Val) || 1690 (FloatToBits(-1.0f) == Val) || 1691 (FloatToBits(0.5f) == Val) || 1692 (FloatToBits(-0.5f) == Val) || 1693 (FloatToBits(2.0f) == Val) || 1694 (FloatToBits(-2.0f) == Val) || 1695 (FloatToBits(4.0f) == Val) || 1696 (FloatToBits(-4.0f) == Val); 1697 } 1698 1699 bool SIInstrInfo::isInlineConstant(const MachineOperand &MO, 1700 unsigned OpSize) const { 1701 if (MO.isImm()) { 1702 // MachineOperand provides no way to tell the true operand size, since it 1703 // only records a 64-bit value. We need to know the size to determine if a 1704 // 32-bit floating point immediate bit pattern is legal for an integer 1705 // immediate. It would be for any 32-bit integer operand, but would not be 1706 // for a 64-bit one. 1707 1708 unsigned BitSize = 8 * OpSize; 1709 return isInlineConstant(APInt(BitSize, MO.getImm(), true)); 1710 } 1711 1712 return false; 1713 } 1714 1715 bool SIInstrInfo::isLiteralConstant(const MachineOperand &MO, 1716 unsigned OpSize) const { 1717 return MO.isImm() && !isInlineConstant(MO, OpSize); 1718 } 1719 1720 bool SIInstrInfo::isLiteralConstantLike(const MachineOperand &MO, 1721 unsigned OpSize) const { 1722 switch (MO.getType()) { 1723 case MachineOperand::MO_Register: 1724 return false; 1725 case MachineOperand::MO_Immediate: 1726 return !isInlineConstant(MO, OpSize); 1727 case MachineOperand::MO_FrameIndex: 1728 case MachineOperand::MO_MachineBasicBlock: 1729 case MachineOperand::MO_ExternalSymbol: 1730 case MachineOperand::MO_GlobalAddress: 1731 case MachineOperand::MO_MCSymbol: 1732 return true; 1733 default: 1734 llvm_unreachable("unexpected operand type"); 1735 } 1736 } 1737 1738 static bool compareMachineOp(const MachineOperand &Op0, 1739 const MachineOperand &Op1) { 1740 if (Op0.getType() != Op1.getType()) 1741 return false; 1742 1743 switch (Op0.getType()) { 1744 case MachineOperand::MO_Register: 1745 return Op0.getReg() == Op1.getReg(); 1746 case MachineOperand::MO_Immediate: 1747 return Op0.getImm() == Op1.getImm(); 1748 default: 1749 llvm_unreachable("Didn't expect to be comparing these operand types"); 1750 } 1751 } 1752 1753 bool SIInstrInfo::isImmOperandLegal(const MachineInstr &MI, unsigned OpNo, 1754 const MachineOperand &MO) const { 1755 const MCOperandInfo &OpInfo = get(MI.getOpcode()).OpInfo[OpNo]; 1756 1757 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI()); 1758 1759 if (OpInfo.OperandType == MCOI::OPERAND_IMMEDIATE) 1760 return true; 1761 1762 if (OpInfo.RegClass < 0) 1763 return false; 1764 1765 unsigned OpSize = RI.getRegClass(OpInfo.RegClass)->getSize(); 1766 if (isLiteralConstant(MO, OpSize)) 1767 return RI.opCanUseLiteralConstant(OpInfo.OperandType); 1768 1769 return RI.opCanUseInlineConstant(OpInfo.OperandType); 1770 } 1771 1772 bool SIInstrInfo::hasVALU32BitEncoding(unsigned Opcode) const { 1773 int Op32 = AMDGPU::getVOPe32(Opcode); 1774 if (Op32 == -1) 1775 return false; 1776 1777 return pseudoToMCOpcode(Op32) != -1; 1778 } 1779 1780 bool SIInstrInfo::hasModifiers(unsigned Opcode) const { 1781 // The src0_modifier operand is present on all instructions 1782 // that have modifiers. 1783 1784 return AMDGPU::getNamedOperandIdx(Opcode, 1785 AMDGPU::OpName::src0_modifiers) != -1; 1786 } 1787 1788 bool SIInstrInfo::hasModifiersSet(const MachineInstr &MI, 1789 unsigned OpName) const { 1790 const MachineOperand *Mods = getNamedOperand(MI, OpName); 1791 return Mods && Mods->getImm(); 1792 } 1793 1794 bool SIInstrInfo::usesConstantBus(const MachineRegisterInfo &MRI, 1795 const MachineOperand &MO, 1796 unsigned OpSize) const { 1797 // Literal constants use the constant bus. 1798 if (isLiteralConstant(MO, OpSize)) 1799 return true; 1800 1801 if (!MO.isReg() || !MO.isUse()) 1802 return false; 1803 1804 if (TargetRegisterInfo::isVirtualRegister(MO.getReg())) 1805 return RI.isSGPRClass(MRI.getRegClass(MO.getReg())); 1806 1807 // FLAT_SCR is just an SGPR pair. 1808 if (!MO.isImplicit() && (MO.getReg() == AMDGPU::FLAT_SCR)) 1809 return true; 1810 1811 // EXEC register uses the constant bus. 1812 if (!MO.isImplicit() && MO.getReg() == AMDGPU::EXEC) 1813 return true; 1814 1815 // SGPRs use the constant bus 1816 return (MO.getReg() == AMDGPU::VCC || MO.getReg() == AMDGPU::M0 || 1817 (!MO.isImplicit() && 1818 (AMDGPU::SGPR_32RegClass.contains(MO.getReg()) || 1819 AMDGPU::SGPR_64RegClass.contains(MO.getReg())))); 1820 } 1821 1822 static unsigned findImplicitSGPRRead(const MachineInstr &MI) { 1823 for (const MachineOperand &MO : MI.implicit_operands()) { 1824 // We only care about reads. 1825 if (MO.isDef()) 1826 continue; 1827 1828 switch (MO.getReg()) { 1829 case AMDGPU::VCC: 1830 case AMDGPU::M0: 1831 case AMDGPU::FLAT_SCR: 1832 return MO.getReg(); 1833 1834 default: 1835 break; 1836 } 1837 } 1838 1839 return AMDGPU::NoRegister; 1840 } 1841 1842 static bool shouldReadExec(const MachineInstr &MI) { 1843 if (SIInstrInfo::isVALU(MI)) { 1844 switch (MI.getOpcode()) { 1845 case AMDGPU::V_READLANE_B32: 1846 case AMDGPU::V_READLANE_B32_si: 1847 case AMDGPU::V_READLANE_B32_vi: 1848 case AMDGPU::V_WRITELANE_B32: 1849 case AMDGPU::V_WRITELANE_B32_si: 1850 case AMDGPU::V_WRITELANE_B32_vi: 1851 return false; 1852 } 1853 1854 return true; 1855 } 1856 1857 if (SIInstrInfo::isGenericOpcode(MI.getOpcode()) || 1858 SIInstrInfo::isSALU(MI) || 1859 SIInstrInfo::isSMRD(MI)) 1860 return false; 1861 1862 return true; 1863 } 1864 1865 static bool isSubRegOf(const SIRegisterInfo &TRI, 1866 const MachineOperand &SuperVec, 1867 const MachineOperand &SubReg) { 1868 if (TargetRegisterInfo::isPhysicalRegister(SubReg.getReg())) 1869 return TRI.isSubRegister(SuperVec.getReg(), SubReg.getReg()); 1870 1871 return SubReg.getSubReg() != AMDGPU::NoSubRegister && 1872 SubReg.getReg() == SuperVec.getReg(); 1873 } 1874 1875 bool SIInstrInfo::verifyInstruction(const MachineInstr &MI, 1876 StringRef &ErrInfo) const { 1877 uint16_t Opcode = MI.getOpcode(); 1878 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 1879 int Src0Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src0); 1880 int Src1Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src1); 1881 int Src2Idx = AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::src2); 1882 1883 // Make sure the number of operands is correct. 1884 const MCInstrDesc &Desc = get(Opcode); 1885 if (!Desc.isVariadic() && 1886 Desc.getNumOperands() != MI.getNumExplicitOperands()) { 1887 ErrInfo = "Instruction has wrong number of operands."; 1888 return false; 1889 } 1890 1891 // Make sure the register classes are correct. 1892 for (int i = 0, e = Desc.getNumOperands(); i != e; ++i) { 1893 if (MI.getOperand(i).isFPImm()) { 1894 ErrInfo = "FPImm Machine Operands are not supported. ISel should bitcast " 1895 "all fp values to integers."; 1896 return false; 1897 } 1898 1899 int RegClass = Desc.OpInfo[i].RegClass; 1900 1901 switch (Desc.OpInfo[i].OperandType) { 1902 case MCOI::OPERAND_REGISTER: 1903 if (MI.getOperand(i).isImm()) { 1904 ErrInfo = "Illegal immediate value for operand."; 1905 return false; 1906 } 1907 break; 1908 case AMDGPU::OPERAND_REG_IMM32_INT: 1909 case AMDGPU::OPERAND_REG_IMM32_FP: 1910 break; 1911 case AMDGPU::OPERAND_REG_INLINE_C_INT: 1912 case AMDGPU::OPERAND_REG_INLINE_C_FP: 1913 if (isLiteralConstant(MI.getOperand(i), 1914 RI.getRegClass(RegClass)->getSize())) { 1915 ErrInfo = "Illegal immediate value for operand."; 1916 return false; 1917 } 1918 break; 1919 case MCOI::OPERAND_IMMEDIATE: 1920 case AMDGPU::OPERAND_KIMM32: 1921 // Check if this operand is an immediate. 1922 // FrameIndex operands will be replaced by immediates, so they are 1923 // allowed. 1924 if (!MI.getOperand(i).isImm() && !MI.getOperand(i).isFI()) { 1925 ErrInfo = "Expected immediate, but got non-immediate"; 1926 return false; 1927 } 1928 LLVM_FALLTHROUGH; 1929 default: 1930 continue; 1931 } 1932 1933 if (!MI.getOperand(i).isReg()) 1934 continue; 1935 1936 if (RegClass != -1) { 1937 unsigned Reg = MI.getOperand(i).getReg(); 1938 if (Reg == AMDGPU::NoRegister || 1939 TargetRegisterInfo::isVirtualRegister(Reg)) 1940 continue; 1941 1942 const TargetRegisterClass *RC = RI.getRegClass(RegClass); 1943 if (!RC->contains(Reg)) { 1944 ErrInfo = "Operand has incorrect register class."; 1945 return false; 1946 } 1947 } 1948 } 1949 1950 // Verify VOP* 1951 if (isVOP1(MI) || isVOP2(MI) || isVOP3(MI) || isVOPC(MI)) { 1952 // Only look at the true operands. Only a real operand can use the constant 1953 // bus, and we don't want to check pseudo-operands like the source modifier 1954 // flags. 1955 const int OpIndices[] = { Src0Idx, Src1Idx, Src2Idx }; 1956 1957 unsigned ConstantBusCount = 0; 1958 1959 if (AMDGPU::getNamedOperandIdx(Opcode, AMDGPU::OpName::imm) != -1) 1960 ++ConstantBusCount; 1961 1962 unsigned SGPRUsed = findImplicitSGPRRead(MI); 1963 if (SGPRUsed != AMDGPU::NoRegister) 1964 ++ConstantBusCount; 1965 1966 for (int OpIdx : OpIndices) { 1967 if (OpIdx == -1) 1968 break; 1969 const MachineOperand &MO = MI.getOperand(OpIdx); 1970 if (usesConstantBus(MRI, MO, getOpSize(Opcode, OpIdx))) { 1971 if (MO.isReg()) { 1972 if (MO.getReg() != SGPRUsed) 1973 ++ConstantBusCount; 1974 SGPRUsed = MO.getReg(); 1975 } else { 1976 ++ConstantBusCount; 1977 } 1978 } 1979 } 1980 if (ConstantBusCount > 1) { 1981 ErrInfo = "VOP* instruction uses the constant bus more than once"; 1982 return false; 1983 } 1984 } 1985 1986 // Verify misc. restrictions on specific instructions. 1987 if (Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F32 || 1988 Desc.getOpcode() == AMDGPU::V_DIV_SCALE_F64) { 1989 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 1990 const MachineOperand &Src1 = MI.getOperand(Src1Idx); 1991 const MachineOperand &Src2 = MI.getOperand(Src2Idx); 1992 if (Src0.isReg() && Src1.isReg() && Src2.isReg()) { 1993 if (!compareMachineOp(Src0, Src1) && 1994 !compareMachineOp(Src0, Src2)) { 1995 ErrInfo = "v_div_scale_{f32|f64} require src0 = src1 or src2"; 1996 return false; 1997 } 1998 } 1999 } 2000 2001 if (isSOPK(MI)) { 2002 int64_t Imm = getNamedOperand(MI, AMDGPU::OpName::simm16)->getImm(); 2003 if (sopkIsZext(MI)) { 2004 if (!isUInt<16>(Imm)) { 2005 ErrInfo = "invalid immediate for SOPK instruction"; 2006 return false; 2007 } 2008 } else { 2009 if (!isInt<16>(Imm)) { 2010 ErrInfo = "invalid immediate for SOPK instruction"; 2011 return false; 2012 } 2013 } 2014 } 2015 2016 if (Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e32 || 2017 Desc.getOpcode() == AMDGPU::V_MOVRELS_B32_e64 || 2018 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 2019 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64) { 2020 const bool IsDst = Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e32 || 2021 Desc.getOpcode() == AMDGPU::V_MOVRELD_B32_e64; 2022 2023 const unsigned StaticNumOps = Desc.getNumOperands() + 2024 Desc.getNumImplicitUses(); 2025 const unsigned NumImplicitOps = IsDst ? 2 : 1; 2026 2027 if (MI.getNumOperands() != StaticNumOps + NumImplicitOps) { 2028 ErrInfo = "missing implicit register operands"; 2029 return false; 2030 } 2031 2032 const MachineOperand *Dst = getNamedOperand(MI, AMDGPU::OpName::vdst); 2033 if (IsDst) { 2034 if (!Dst->isUse()) { 2035 ErrInfo = "v_movreld_b32 vdst should be a use operand"; 2036 return false; 2037 } 2038 2039 unsigned UseOpIdx; 2040 if (!MI.isRegTiedToUseOperand(StaticNumOps, &UseOpIdx) || 2041 UseOpIdx != StaticNumOps + 1) { 2042 ErrInfo = "movrel implicit operands should be tied"; 2043 return false; 2044 } 2045 } 2046 2047 const MachineOperand &Src0 = MI.getOperand(Src0Idx); 2048 const MachineOperand &ImpUse 2049 = MI.getOperand(StaticNumOps + NumImplicitOps - 1); 2050 if (!ImpUse.isReg() || !ImpUse.isUse() || 2051 !isSubRegOf(RI, ImpUse, IsDst ? *Dst : Src0)) { 2052 ErrInfo = "src0 should be subreg of implicit vector use"; 2053 return false; 2054 } 2055 } 2056 2057 // Make sure we aren't losing exec uses in the td files. This mostly requires 2058 // being careful when using let Uses to try to add other use registers. 2059 if (shouldReadExec(MI)) { 2060 if (!MI.hasRegisterImplicitUseOperand(AMDGPU::EXEC)) { 2061 ErrInfo = "VALU instruction does not implicitly read exec mask"; 2062 return false; 2063 } 2064 } 2065 2066 return true; 2067 } 2068 2069 unsigned SIInstrInfo::getVALUOp(const MachineInstr &MI) { 2070 switch (MI.getOpcode()) { 2071 default: return AMDGPU::INSTRUCTION_LIST_END; 2072 case AMDGPU::REG_SEQUENCE: return AMDGPU::REG_SEQUENCE; 2073 case AMDGPU::COPY: return AMDGPU::COPY; 2074 case AMDGPU::PHI: return AMDGPU::PHI; 2075 case AMDGPU::INSERT_SUBREG: return AMDGPU::INSERT_SUBREG; 2076 case AMDGPU::S_MOV_B32: 2077 return MI.getOperand(1).isReg() ? 2078 AMDGPU::COPY : AMDGPU::V_MOV_B32_e32; 2079 case AMDGPU::S_ADD_I32: 2080 case AMDGPU::S_ADD_U32: return AMDGPU::V_ADD_I32_e32; 2081 case AMDGPU::S_ADDC_U32: return AMDGPU::V_ADDC_U32_e32; 2082 case AMDGPU::S_SUB_I32: 2083 case AMDGPU::S_SUB_U32: return AMDGPU::V_SUB_I32_e32; 2084 case AMDGPU::S_SUBB_U32: return AMDGPU::V_SUBB_U32_e32; 2085 case AMDGPU::S_MUL_I32: return AMDGPU::V_MUL_LO_I32; 2086 case AMDGPU::S_AND_B32: return AMDGPU::V_AND_B32_e64; 2087 case AMDGPU::S_OR_B32: return AMDGPU::V_OR_B32_e64; 2088 case AMDGPU::S_XOR_B32: return AMDGPU::V_XOR_B32_e64; 2089 case AMDGPU::S_MIN_I32: return AMDGPU::V_MIN_I32_e64; 2090 case AMDGPU::S_MIN_U32: return AMDGPU::V_MIN_U32_e64; 2091 case AMDGPU::S_MAX_I32: return AMDGPU::V_MAX_I32_e64; 2092 case AMDGPU::S_MAX_U32: return AMDGPU::V_MAX_U32_e64; 2093 case AMDGPU::S_ASHR_I32: return AMDGPU::V_ASHR_I32_e32; 2094 case AMDGPU::S_ASHR_I64: return AMDGPU::V_ASHR_I64; 2095 case AMDGPU::S_LSHL_B32: return AMDGPU::V_LSHL_B32_e32; 2096 case AMDGPU::S_LSHL_B64: return AMDGPU::V_LSHL_B64; 2097 case AMDGPU::S_LSHR_B32: return AMDGPU::V_LSHR_B32_e32; 2098 case AMDGPU::S_LSHR_B64: return AMDGPU::V_LSHR_B64; 2099 case AMDGPU::S_SEXT_I32_I8: return AMDGPU::V_BFE_I32; 2100 case AMDGPU::S_SEXT_I32_I16: return AMDGPU::V_BFE_I32; 2101 case AMDGPU::S_BFE_U32: return AMDGPU::V_BFE_U32; 2102 case AMDGPU::S_BFE_I32: return AMDGPU::V_BFE_I32; 2103 case AMDGPU::S_BFM_B32: return AMDGPU::V_BFM_B32_e64; 2104 case AMDGPU::S_BREV_B32: return AMDGPU::V_BFREV_B32_e32; 2105 case AMDGPU::S_NOT_B32: return AMDGPU::V_NOT_B32_e32; 2106 case AMDGPU::S_NOT_B64: return AMDGPU::V_NOT_B32_e32; 2107 case AMDGPU::S_CMP_EQ_I32: return AMDGPU::V_CMP_EQ_I32_e32; 2108 case AMDGPU::S_CMP_LG_I32: return AMDGPU::V_CMP_NE_I32_e32; 2109 case AMDGPU::S_CMP_GT_I32: return AMDGPU::V_CMP_GT_I32_e32; 2110 case AMDGPU::S_CMP_GE_I32: return AMDGPU::V_CMP_GE_I32_e32; 2111 case AMDGPU::S_CMP_LT_I32: return AMDGPU::V_CMP_LT_I32_e32; 2112 case AMDGPU::S_CMP_LE_I32: return AMDGPU::V_CMP_LE_I32_e32; 2113 case AMDGPU::S_CMP_EQ_U32: return AMDGPU::V_CMP_EQ_U32_e32; 2114 case AMDGPU::S_CMP_LG_U32: return AMDGPU::V_CMP_NE_U32_e32; 2115 case AMDGPU::S_CMP_GT_U32: return AMDGPU::V_CMP_GT_U32_e32; 2116 case AMDGPU::S_CMP_GE_U32: return AMDGPU::V_CMP_GE_U32_e32; 2117 case AMDGPU::S_CMP_LT_U32: return AMDGPU::V_CMP_LT_U32_e32; 2118 case AMDGPU::S_CMP_LE_U32: return AMDGPU::V_CMP_LE_U32_e32; 2119 case AMDGPU::S_CMP_EQ_U64: return AMDGPU::V_CMP_EQ_U64_e32; 2120 case AMDGPU::S_CMP_LG_U64: return AMDGPU::V_CMP_NE_U64_e32; 2121 case AMDGPU::S_BCNT1_I32_B32: return AMDGPU::V_BCNT_U32_B32_e64; 2122 case AMDGPU::S_FF1_I32_B32: return AMDGPU::V_FFBL_B32_e32; 2123 case AMDGPU::S_FLBIT_I32_B32: return AMDGPU::V_FFBH_U32_e32; 2124 case AMDGPU::S_FLBIT_I32: return AMDGPU::V_FFBH_I32_e64; 2125 case AMDGPU::S_CBRANCH_SCC0: return AMDGPU::S_CBRANCH_VCCZ; 2126 case AMDGPU::S_CBRANCH_SCC1: return AMDGPU::S_CBRANCH_VCCNZ; 2127 } 2128 } 2129 2130 bool SIInstrInfo::isSALUOpSupportedOnVALU(const MachineInstr &MI) const { 2131 return getVALUOp(MI) != AMDGPU::INSTRUCTION_LIST_END; 2132 } 2133 2134 const TargetRegisterClass *SIInstrInfo::getOpRegClass(const MachineInstr &MI, 2135 unsigned OpNo) const { 2136 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 2137 const MCInstrDesc &Desc = get(MI.getOpcode()); 2138 if (MI.isVariadic() || OpNo >= Desc.getNumOperands() || 2139 Desc.OpInfo[OpNo].RegClass == -1) { 2140 unsigned Reg = MI.getOperand(OpNo).getReg(); 2141 2142 if (TargetRegisterInfo::isVirtualRegister(Reg)) 2143 return MRI.getRegClass(Reg); 2144 return RI.getPhysRegClass(Reg); 2145 } 2146 2147 unsigned RCID = Desc.OpInfo[OpNo].RegClass; 2148 return RI.getRegClass(RCID); 2149 } 2150 2151 bool SIInstrInfo::canReadVGPR(const MachineInstr &MI, unsigned OpNo) const { 2152 switch (MI.getOpcode()) { 2153 case AMDGPU::COPY: 2154 case AMDGPU::REG_SEQUENCE: 2155 case AMDGPU::PHI: 2156 case AMDGPU::INSERT_SUBREG: 2157 return RI.hasVGPRs(getOpRegClass(MI, 0)); 2158 default: 2159 return RI.hasVGPRs(getOpRegClass(MI, OpNo)); 2160 } 2161 } 2162 2163 void SIInstrInfo::legalizeOpWithMove(MachineInstr &MI, unsigned OpIdx) const { 2164 MachineBasicBlock::iterator I = MI; 2165 MachineBasicBlock *MBB = MI.getParent(); 2166 MachineOperand &MO = MI.getOperand(OpIdx); 2167 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 2168 unsigned RCID = get(MI.getOpcode()).OpInfo[OpIdx].RegClass; 2169 const TargetRegisterClass *RC = RI.getRegClass(RCID); 2170 unsigned Opcode = AMDGPU::V_MOV_B32_e32; 2171 if (MO.isReg()) 2172 Opcode = AMDGPU::COPY; 2173 else if (RI.isSGPRClass(RC)) 2174 Opcode = AMDGPU::S_MOV_B32; 2175 2176 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(RC); 2177 if (RI.getCommonSubClass(&AMDGPU::VReg_64RegClass, VRC)) 2178 VRC = &AMDGPU::VReg_64RegClass; 2179 else 2180 VRC = &AMDGPU::VGPR_32RegClass; 2181 2182 unsigned Reg = MRI.createVirtualRegister(VRC); 2183 DebugLoc DL = MBB->findDebugLoc(I); 2184 BuildMI(*MI.getParent(), I, DL, get(Opcode), Reg).addOperand(MO); 2185 MO.ChangeToRegister(Reg, false); 2186 } 2187 2188 unsigned SIInstrInfo::buildExtractSubReg(MachineBasicBlock::iterator MI, 2189 MachineRegisterInfo &MRI, 2190 MachineOperand &SuperReg, 2191 const TargetRegisterClass *SuperRC, 2192 unsigned SubIdx, 2193 const TargetRegisterClass *SubRC) 2194 const { 2195 MachineBasicBlock *MBB = MI->getParent(); 2196 DebugLoc DL = MI->getDebugLoc(); 2197 unsigned SubReg = MRI.createVirtualRegister(SubRC); 2198 2199 if (SuperReg.getSubReg() == AMDGPU::NoSubRegister) { 2200 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 2201 .addReg(SuperReg.getReg(), 0, SubIdx); 2202 return SubReg; 2203 } 2204 2205 // Just in case the super register is itself a sub-register, copy it to a new 2206 // value so we don't need to worry about merging its subreg index with the 2207 // SubIdx passed to this function. The register coalescer should be able to 2208 // eliminate this extra copy. 2209 unsigned NewSuperReg = MRI.createVirtualRegister(SuperRC); 2210 2211 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), NewSuperReg) 2212 .addReg(SuperReg.getReg(), 0, SuperReg.getSubReg()); 2213 2214 BuildMI(*MBB, MI, DL, get(TargetOpcode::COPY), SubReg) 2215 .addReg(NewSuperReg, 0, SubIdx); 2216 2217 return SubReg; 2218 } 2219 2220 MachineOperand SIInstrInfo::buildExtractSubRegOrImm( 2221 MachineBasicBlock::iterator MII, 2222 MachineRegisterInfo &MRI, 2223 MachineOperand &Op, 2224 const TargetRegisterClass *SuperRC, 2225 unsigned SubIdx, 2226 const TargetRegisterClass *SubRC) const { 2227 if (Op.isImm()) { 2228 if (SubIdx == AMDGPU::sub0) 2229 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm())); 2230 if (SubIdx == AMDGPU::sub1) 2231 return MachineOperand::CreateImm(static_cast<int32_t>(Op.getImm() >> 32)); 2232 2233 llvm_unreachable("Unhandled register index for immediate"); 2234 } 2235 2236 unsigned SubReg = buildExtractSubReg(MII, MRI, Op, SuperRC, 2237 SubIdx, SubRC); 2238 return MachineOperand::CreateReg(SubReg, false); 2239 } 2240 2241 // Change the order of operands from (0, 1, 2) to (0, 2, 1) 2242 void SIInstrInfo::swapOperands(MachineInstr &Inst) const { 2243 assert(Inst.getNumExplicitOperands() == 3); 2244 MachineOperand Op1 = Inst.getOperand(1); 2245 Inst.RemoveOperand(1); 2246 Inst.addOperand(Op1); 2247 } 2248 2249 bool SIInstrInfo::isLegalRegOperand(const MachineRegisterInfo &MRI, 2250 const MCOperandInfo &OpInfo, 2251 const MachineOperand &MO) const { 2252 if (!MO.isReg()) 2253 return false; 2254 2255 unsigned Reg = MO.getReg(); 2256 const TargetRegisterClass *RC = 2257 TargetRegisterInfo::isVirtualRegister(Reg) ? 2258 MRI.getRegClass(Reg) : 2259 RI.getPhysRegClass(Reg); 2260 2261 const SIRegisterInfo *TRI = 2262 static_cast<const SIRegisterInfo*>(MRI.getTargetRegisterInfo()); 2263 RC = TRI->getSubRegClass(RC, MO.getSubReg()); 2264 2265 // In order to be legal, the common sub-class must be equal to the 2266 // class of the current operand. For example: 2267 // 2268 // v_mov_b32 s0 ; Operand defined as vsrc_b32 2269 // ; RI.getCommonSubClass(s0,vsrc_b32) = sgpr ; LEGAL 2270 // 2271 // s_sendmsg 0, s0 ; Operand defined as m0reg 2272 // ; RI.getCommonSubClass(s0,m0reg) = m0reg ; NOT LEGAL 2273 2274 return RI.getCommonSubClass(RC, RI.getRegClass(OpInfo.RegClass)) == RC; 2275 } 2276 2277 bool SIInstrInfo::isLegalVSrcOperand(const MachineRegisterInfo &MRI, 2278 const MCOperandInfo &OpInfo, 2279 const MachineOperand &MO) const { 2280 if (MO.isReg()) 2281 return isLegalRegOperand(MRI, OpInfo, MO); 2282 2283 // Handle non-register types that are treated like immediates. 2284 assert(MO.isImm() || MO.isTargetIndex() || MO.isFI()); 2285 return true; 2286 } 2287 2288 bool SIInstrInfo::isOperandLegal(const MachineInstr &MI, unsigned OpIdx, 2289 const MachineOperand *MO) const { 2290 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 2291 const MCInstrDesc &InstDesc = MI.getDesc(); 2292 const MCOperandInfo &OpInfo = InstDesc.OpInfo[OpIdx]; 2293 const TargetRegisterClass *DefinedRC = 2294 OpInfo.RegClass != -1 ? RI.getRegClass(OpInfo.RegClass) : nullptr; 2295 if (!MO) 2296 MO = &MI.getOperand(OpIdx); 2297 2298 if (isVALU(MI) && usesConstantBus(MRI, *MO, DefinedRC->getSize())) { 2299 2300 RegSubRegPair SGPRUsed; 2301 if (MO->isReg()) 2302 SGPRUsed = RegSubRegPair(MO->getReg(), MO->getSubReg()); 2303 2304 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) { 2305 if (i == OpIdx) 2306 continue; 2307 const MachineOperand &Op = MI.getOperand(i); 2308 if (Op.isReg()) { 2309 if ((Op.getReg() != SGPRUsed.Reg || Op.getSubReg() != SGPRUsed.SubReg) && 2310 usesConstantBus(MRI, Op, getOpSize(MI, i))) { 2311 return false; 2312 } 2313 } else if (InstDesc.OpInfo[i].OperandType == AMDGPU::OPERAND_KIMM32) { 2314 return false; 2315 } 2316 } 2317 } 2318 2319 if (MO->isReg()) { 2320 assert(DefinedRC); 2321 return isLegalRegOperand(MRI, OpInfo, *MO); 2322 } 2323 2324 // Handle non-register types that are treated like immediates. 2325 assert(MO->isImm() || MO->isTargetIndex() || MO->isFI()); 2326 2327 if (!DefinedRC) { 2328 // This operand expects an immediate. 2329 return true; 2330 } 2331 2332 return isImmOperandLegal(MI, OpIdx, *MO); 2333 } 2334 2335 void SIInstrInfo::legalizeOperandsVOP2(MachineRegisterInfo &MRI, 2336 MachineInstr &MI) const { 2337 unsigned Opc = MI.getOpcode(); 2338 const MCInstrDesc &InstrDesc = get(Opc); 2339 2340 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 2341 MachineOperand &Src1 = MI.getOperand(Src1Idx); 2342 2343 // If there is an implicit SGPR use such as VCC use for v_addc_u32/v_subb_u32 2344 // we need to only have one constant bus use. 2345 // 2346 // Note we do not need to worry about literal constants here. They are 2347 // disabled for the operand type for instructions because they will always 2348 // violate the one constant bus use rule. 2349 bool HasImplicitSGPR = findImplicitSGPRRead(MI) != AMDGPU::NoRegister; 2350 if (HasImplicitSGPR) { 2351 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 2352 MachineOperand &Src0 = MI.getOperand(Src0Idx); 2353 2354 if (Src0.isReg() && RI.isSGPRReg(MRI, Src0.getReg())) 2355 legalizeOpWithMove(MI, Src0Idx); 2356 } 2357 2358 // VOP2 src0 instructions support all operand types, so we don't need to check 2359 // their legality. If src1 is already legal, we don't need to do anything. 2360 if (isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src1)) 2361 return; 2362 2363 // We do not use commuteInstruction here because it is too aggressive and will 2364 // commute if it is possible. We only want to commute here if it improves 2365 // legality. This can be called a fairly large number of times so don't waste 2366 // compile time pointlessly swapping and checking legality again. 2367 if (HasImplicitSGPR || !MI.isCommutable()) { 2368 legalizeOpWithMove(MI, Src1Idx); 2369 return; 2370 } 2371 2372 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 2373 MachineOperand &Src0 = MI.getOperand(Src0Idx); 2374 2375 // If src0 can be used as src1, commuting will make the operands legal. 2376 // Otherwise we have to give up and insert a move. 2377 // 2378 // TODO: Other immediate-like operand kinds could be commuted if there was a 2379 // MachineOperand::ChangeTo* for them. 2380 if ((!Src1.isImm() && !Src1.isReg()) || 2381 !isLegalRegOperand(MRI, InstrDesc.OpInfo[Src1Idx], Src0)) { 2382 legalizeOpWithMove(MI, Src1Idx); 2383 return; 2384 } 2385 2386 int CommutedOpc = commuteOpcode(MI); 2387 if (CommutedOpc == -1) { 2388 legalizeOpWithMove(MI, Src1Idx); 2389 return; 2390 } 2391 2392 MI.setDesc(get(CommutedOpc)); 2393 2394 unsigned Src0Reg = Src0.getReg(); 2395 unsigned Src0SubReg = Src0.getSubReg(); 2396 bool Src0Kill = Src0.isKill(); 2397 2398 if (Src1.isImm()) 2399 Src0.ChangeToImmediate(Src1.getImm()); 2400 else if (Src1.isReg()) { 2401 Src0.ChangeToRegister(Src1.getReg(), false, false, Src1.isKill()); 2402 Src0.setSubReg(Src1.getSubReg()); 2403 } else 2404 llvm_unreachable("Should only have register or immediate operands"); 2405 2406 Src1.ChangeToRegister(Src0Reg, false, false, Src0Kill); 2407 Src1.setSubReg(Src0SubReg); 2408 } 2409 2410 // Legalize VOP3 operands. Because all operand types are supported for any 2411 // operand, and since literal constants are not allowed and should never be 2412 // seen, we only need to worry about inserting copies if we use multiple SGPR 2413 // operands. 2414 void SIInstrInfo::legalizeOperandsVOP3(MachineRegisterInfo &MRI, 2415 MachineInstr &MI) const { 2416 unsigned Opc = MI.getOpcode(); 2417 2418 int VOP3Idx[3] = { 2419 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0), 2420 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1), 2421 AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src2) 2422 }; 2423 2424 // Find the one SGPR operand we are allowed to use. 2425 unsigned SGPRReg = findUsedSGPR(MI, VOP3Idx); 2426 2427 for (unsigned i = 0; i < 3; ++i) { 2428 int Idx = VOP3Idx[i]; 2429 if (Idx == -1) 2430 break; 2431 MachineOperand &MO = MI.getOperand(Idx); 2432 2433 // We should never see a VOP3 instruction with an illegal immediate operand. 2434 if (!MO.isReg()) 2435 continue; 2436 2437 if (!RI.isSGPRClass(MRI.getRegClass(MO.getReg()))) 2438 continue; // VGPRs are legal 2439 2440 if (SGPRReg == AMDGPU::NoRegister || SGPRReg == MO.getReg()) { 2441 SGPRReg = MO.getReg(); 2442 // We can use one SGPR in each VOP3 instruction. 2443 continue; 2444 } 2445 2446 // If we make it this far, then the operand is not legal and we must 2447 // legalize it. 2448 legalizeOpWithMove(MI, Idx); 2449 } 2450 } 2451 2452 unsigned SIInstrInfo::readlaneVGPRToSGPR(unsigned SrcReg, MachineInstr &UseMI, 2453 MachineRegisterInfo &MRI) const { 2454 const TargetRegisterClass *VRC = MRI.getRegClass(SrcReg); 2455 const TargetRegisterClass *SRC = RI.getEquivalentSGPRClass(VRC); 2456 unsigned DstReg = MRI.createVirtualRegister(SRC); 2457 unsigned SubRegs = VRC->getSize() / 4; 2458 2459 SmallVector<unsigned, 8> SRegs; 2460 for (unsigned i = 0; i < SubRegs; ++i) { 2461 unsigned SGPR = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 2462 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 2463 get(AMDGPU::V_READFIRSTLANE_B32), SGPR) 2464 .addReg(SrcReg, 0, RI.getSubRegFromChannel(i)); 2465 SRegs.push_back(SGPR); 2466 } 2467 2468 MachineInstrBuilder MIB = 2469 BuildMI(*UseMI.getParent(), UseMI, UseMI.getDebugLoc(), 2470 get(AMDGPU::REG_SEQUENCE), DstReg); 2471 for (unsigned i = 0; i < SubRegs; ++i) { 2472 MIB.addReg(SRegs[i]); 2473 MIB.addImm(RI.getSubRegFromChannel(i)); 2474 } 2475 return DstReg; 2476 } 2477 2478 void SIInstrInfo::legalizeOperandsSMRD(MachineRegisterInfo &MRI, 2479 MachineInstr &MI) const { 2480 2481 // If the pointer is store in VGPRs, then we need to move them to 2482 // SGPRs using v_readfirstlane. This is safe because we only select 2483 // loads with uniform pointers to SMRD instruction so we know the 2484 // pointer value is uniform. 2485 MachineOperand *SBase = getNamedOperand(MI, AMDGPU::OpName::sbase); 2486 if (SBase && !RI.isSGPRClass(MRI.getRegClass(SBase->getReg()))) { 2487 unsigned SGPR = readlaneVGPRToSGPR(SBase->getReg(), MI, MRI); 2488 SBase->setReg(SGPR); 2489 } 2490 } 2491 2492 void SIInstrInfo::legalizeOperands(MachineInstr &MI) const { 2493 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 2494 2495 // Legalize VOP2 2496 if (isVOP2(MI) || isVOPC(MI)) { 2497 legalizeOperandsVOP2(MRI, MI); 2498 return; 2499 } 2500 2501 // Legalize VOP3 2502 if (isVOP3(MI)) { 2503 legalizeOperandsVOP3(MRI, MI); 2504 return; 2505 } 2506 2507 // Legalize SMRD 2508 if (isSMRD(MI)) { 2509 legalizeOperandsSMRD(MRI, MI); 2510 return; 2511 } 2512 2513 // Legalize REG_SEQUENCE and PHI 2514 // The register class of the operands much be the same type as the register 2515 // class of the output. 2516 if (MI.getOpcode() == AMDGPU::PHI) { 2517 const TargetRegisterClass *RC = nullptr, *SRC = nullptr, *VRC = nullptr; 2518 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { 2519 if (!MI.getOperand(i).isReg() || 2520 !TargetRegisterInfo::isVirtualRegister(MI.getOperand(i).getReg())) 2521 continue; 2522 const TargetRegisterClass *OpRC = 2523 MRI.getRegClass(MI.getOperand(i).getReg()); 2524 if (RI.hasVGPRs(OpRC)) { 2525 VRC = OpRC; 2526 } else { 2527 SRC = OpRC; 2528 } 2529 } 2530 2531 // If any of the operands are VGPR registers, then they all most be 2532 // otherwise we will create illegal VGPR->SGPR copies when legalizing 2533 // them. 2534 if (VRC || !RI.isSGPRClass(getOpRegClass(MI, 0))) { 2535 if (!VRC) { 2536 assert(SRC); 2537 VRC = RI.getEquivalentVGPRClass(SRC); 2538 } 2539 RC = VRC; 2540 } else { 2541 RC = SRC; 2542 } 2543 2544 // Update all the operands so they have the same type. 2545 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 2546 MachineOperand &Op = MI.getOperand(I); 2547 if (!Op.isReg() || !TargetRegisterInfo::isVirtualRegister(Op.getReg())) 2548 continue; 2549 unsigned DstReg = MRI.createVirtualRegister(RC); 2550 2551 // MI is a PHI instruction. 2552 MachineBasicBlock *InsertBB = MI.getOperand(I + 1).getMBB(); 2553 MachineBasicBlock::iterator Insert = InsertBB->getFirstTerminator(); 2554 2555 BuildMI(*InsertBB, Insert, MI.getDebugLoc(), get(AMDGPU::COPY), DstReg) 2556 .addOperand(Op); 2557 Op.setReg(DstReg); 2558 } 2559 } 2560 2561 // REG_SEQUENCE doesn't really require operand legalization, but if one has a 2562 // VGPR dest type and SGPR sources, insert copies so all operands are 2563 // VGPRs. This seems to help operand folding / the register coalescer. 2564 if (MI.getOpcode() == AMDGPU::REG_SEQUENCE) { 2565 MachineBasicBlock *MBB = MI.getParent(); 2566 const TargetRegisterClass *DstRC = getOpRegClass(MI, 0); 2567 if (RI.hasVGPRs(DstRC)) { 2568 // Update all the operands so they are VGPR register classes. These may 2569 // not be the same register class because REG_SEQUENCE supports mixing 2570 // subregister index types e.g. sub0_sub1 + sub2 + sub3 2571 for (unsigned I = 1, E = MI.getNumOperands(); I != E; I += 2) { 2572 MachineOperand &Op = MI.getOperand(I); 2573 if (!Op.isReg() || !TargetRegisterInfo::isVirtualRegister(Op.getReg())) 2574 continue; 2575 2576 const TargetRegisterClass *OpRC = MRI.getRegClass(Op.getReg()); 2577 const TargetRegisterClass *VRC = RI.getEquivalentVGPRClass(OpRC); 2578 if (VRC == OpRC) 2579 continue; 2580 2581 unsigned DstReg = MRI.createVirtualRegister(VRC); 2582 2583 BuildMI(*MBB, MI, MI.getDebugLoc(), get(AMDGPU::COPY), DstReg) 2584 .addOperand(Op); 2585 2586 Op.setReg(DstReg); 2587 Op.setIsKill(); 2588 } 2589 } 2590 2591 return; 2592 } 2593 2594 // Legalize INSERT_SUBREG 2595 // src0 must have the same register class as dst 2596 if (MI.getOpcode() == AMDGPU::INSERT_SUBREG) { 2597 unsigned Dst = MI.getOperand(0).getReg(); 2598 unsigned Src0 = MI.getOperand(1).getReg(); 2599 const TargetRegisterClass *DstRC = MRI.getRegClass(Dst); 2600 const TargetRegisterClass *Src0RC = MRI.getRegClass(Src0); 2601 if (DstRC != Src0RC) { 2602 MachineBasicBlock &MBB = *MI.getParent(); 2603 unsigned NewSrc0 = MRI.createVirtualRegister(DstRC); 2604 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::COPY), NewSrc0) 2605 .addReg(Src0); 2606 MI.getOperand(1).setReg(NewSrc0); 2607 } 2608 return; 2609 } 2610 2611 // Legalize MIMG 2612 if (isMIMG(MI)) { 2613 MachineOperand *SRsrc = getNamedOperand(MI, AMDGPU::OpName::srsrc); 2614 if (SRsrc && !RI.isSGPRClass(MRI.getRegClass(SRsrc->getReg()))) { 2615 unsigned SGPR = readlaneVGPRToSGPR(SRsrc->getReg(), MI, MRI); 2616 SRsrc->setReg(SGPR); 2617 } 2618 2619 MachineOperand *SSamp = getNamedOperand(MI, AMDGPU::OpName::ssamp); 2620 if (SSamp && !RI.isSGPRClass(MRI.getRegClass(SSamp->getReg()))) { 2621 unsigned SGPR = readlaneVGPRToSGPR(SSamp->getReg(), MI, MRI); 2622 SSamp->setReg(SGPR); 2623 } 2624 return; 2625 } 2626 2627 // Legalize MUBUF* instructions 2628 // FIXME: If we start using the non-addr64 instructions for compute, we 2629 // may need to legalize them here. 2630 int SRsrcIdx = 2631 AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::srsrc); 2632 if (SRsrcIdx != -1) { 2633 // We have an MUBUF instruction 2634 MachineOperand *SRsrc = &MI.getOperand(SRsrcIdx); 2635 unsigned SRsrcRC = get(MI.getOpcode()).OpInfo[SRsrcIdx].RegClass; 2636 if (RI.getCommonSubClass(MRI.getRegClass(SRsrc->getReg()), 2637 RI.getRegClass(SRsrcRC))) { 2638 // The operands are legal. 2639 // FIXME: We may need to legalize operands besided srsrc. 2640 return; 2641 } 2642 2643 MachineBasicBlock &MBB = *MI.getParent(); 2644 2645 // Extract the ptr from the resource descriptor. 2646 unsigned SRsrcPtr = buildExtractSubReg(MI, MRI, *SRsrc, 2647 &AMDGPU::VReg_128RegClass, AMDGPU::sub0_sub1, &AMDGPU::VReg_64RegClass); 2648 2649 // Create an empty resource descriptor 2650 unsigned Zero64 = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass); 2651 unsigned SRsrcFormatLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 2652 unsigned SRsrcFormatHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass); 2653 unsigned NewSRsrc = MRI.createVirtualRegister(&AMDGPU::SReg_128RegClass); 2654 uint64_t RsrcDataFormat = getDefaultRsrcDataFormat(); 2655 2656 // Zero64 = 0 2657 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::S_MOV_B64), Zero64) 2658 .addImm(0); 2659 2660 // SRsrcFormatLo = RSRC_DATA_FORMAT{31-0} 2661 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::S_MOV_B32), SRsrcFormatLo) 2662 .addImm(RsrcDataFormat & 0xFFFFFFFF); 2663 2664 // SRsrcFormatHi = RSRC_DATA_FORMAT{63-32} 2665 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::S_MOV_B32), SRsrcFormatHi) 2666 .addImm(RsrcDataFormat >> 32); 2667 2668 // NewSRsrc = {Zero64, SRsrcFormat} 2669 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewSRsrc) 2670 .addReg(Zero64) 2671 .addImm(AMDGPU::sub0_sub1) 2672 .addReg(SRsrcFormatLo) 2673 .addImm(AMDGPU::sub2) 2674 .addReg(SRsrcFormatHi) 2675 .addImm(AMDGPU::sub3); 2676 2677 MachineOperand *VAddr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 2678 unsigned NewVAddr = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 2679 if (VAddr) { 2680 // This is already an ADDR64 instruction so we need to add the pointer 2681 // extracted from the resource descriptor to the current value of VAddr. 2682 unsigned NewVAddrLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 2683 unsigned NewVAddrHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 2684 2685 // NewVaddrLo = SRsrcPtr:sub0 + VAddr:sub0 2686 DebugLoc DL = MI.getDebugLoc(); 2687 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADD_I32_e32), NewVAddrLo) 2688 .addReg(SRsrcPtr, 0, AMDGPU::sub0) 2689 .addReg(VAddr->getReg(), 0, AMDGPU::sub0); 2690 2691 // NewVaddrHi = SRsrcPtr:sub1 + VAddr:sub1 2692 BuildMI(MBB, MI, DL, get(AMDGPU::V_ADDC_U32_e32), NewVAddrHi) 2693 .addReg(SRsrcPtr, 0, AMDGPU::sub1) 2694 .addReg(VAddr->getReg(), 0, AMDGPU::sub1); 2695 2696 // NewVaddr = {NewVaddrHi, NewVaddrLo} 2697 BuildMI(MBB, MI, MI.getDebugLoc(), get(AMDGPU::REG_SEQUENCE), NewVAddr) 2698 .addReg(NewVAddrLo) 2699 .addImm(AMDGPU::sub0) 2700 .addReg(NewVAddrHi) 2701 .addImm(AMDGPU::sub1); 2702 } else { 2703 // This instructions is the _OFFSET variant, so we need to convert it to 2704 // ADDR64. 2705 assert(MBB.getParent()->getSubtarget<SISubtarget>().getGeneration() 2706 < SISubtarget::VOLCANIC_ISLANDS && 2707 "FIXME: Need to emit flat atomics here"); 2708 2709 MachineOperand *VData = getNamedOperand(MI, AMDGPU::OpName::vdata); 2710 MachineOperand *Offset = getNamedOperand(MI, AMDGPU::OpName::offset); 2711 MachineOperand *SOffset = getNamedOperand(MI, AMDGPU::OpName::soffset); 2712 unsigned Addr64Opcode = AMDGPU::getAddr64Inst(MI.getOpcode()); 2713 2714 // Atomics rith return have have an additional tied operand and are 2715 // missing some of the special bits. 2716 MachineOperand *VDataIn = getNamedOperand(MI, AMDGPU::OpName::vdata_in); 2717 MachineInstr *Addr64; 2718 2719 if (!VDataIn) { 2720 // Regular buffer load / store. 2721 MachineInstrBuilder MIB = 2722 BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 2723 .addOperand(*VData) 2724 .addReg(AMDGPU::NoRegister) // Dummy value for vaddr. 2725 // This will be replaced later 2726 // with the new value of vaddr. 2727 .addOperand(*SRsrc) 2728 .addOperand(*SOffset) 2729 .addOperand(*Offset); 2730 2731 // Atomics do not have this operand. 2732 if (const MachineOperand *GLC = 2733 getNamedOperand(MI, AMDGPU::OpName::glc)) { 2734 MIB.addImm(GLC->getImm()); 2735 } 2736 2737 MIB.addImm(getNamedImmOperand(MI, AMDGPU::OpName::slc)); 2738 2739 if (const MachineOperand *TFE = 2740 getNamedOperand(MI, AMDGPU::OpName::tfe)) { 2741 MIB.addImm(TFE->getImm()); 2742 } 2743 2744 MIB.setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 2745 Addr64 = MIB; 2746 } else { 2747 // Atomics with return. 2748 Addr64 = BuildMI(MBB, MI, MI.getDebugLoc(), get(Addr64Opcode)) 2749 .addOperand(*VData) 2750 .addOperand(*VDataIn) 2751 .addReg(AMDGPU::NoRegister) // Dummy value for vaddr. 2752 // This will be replaced later 2753 // with the new value of vaddr. 2754 .addOperand(*SRsrc) 2755 .addOperand(*SOffset) 2756 .addOperand(*Offset) 2757 .addImm(getNamedImmOperand(MI, AMDGPU::OpName::slc)) 2758 .setMemRefs(MI.memoperands_begin(), MI.memoperands_end()); 2759 } 2760 2761 MI.removeFromParent(); 2762 2763 // NewVaddr = {NewVaddrHi, NewVaddrLo} 2764 BuildMI(MBB, Addr64, Addr64->getDebugLoc(), get(AMDGPU::REG_SEQUENCE), 2765 NewVAddr) 2766 .addReg(SRsrcPtr, 0, AMDGPU::sub0) 2767 .addImm(AMDGPU::sub0) 2768 .addReg(SRsrcPtr, 0, AMDGPU::sub1) 2769 .addImm(AMDGPU::sub1); 2770 2771 VAddr = getNamedOperand(*Addr64, AMDGPU::OpName::vaddr); 2772 SRsrc = getNamedOperand(*Addr64, AMDGPU::OpName::srsrc); 2773 } 2774 2775 // Update the instruction to use NewVaddr 2776 VAddr->setReg(NewVAddr); 2777 // Update the instruction to use NewSRsrc 2778 SRsrc->setReg(NewSRsrc); 2779 } 2780 } 2781 2782 void SIInstrInfo::moveToVALU(MachineInstr &TopInst) const { 2783 SmallVector<MachineInstr *, 128> Worklist; 2784 Worklist.push_back(&TopInst); 2785 2786 while (!Worklist.empty()) { 2787 MachineInstr &Inst = *Worklist.pop_back_val(); 2788 MachineBasicBlock *MBB = Inst.getParent(); 2789 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); 2790 2791 unsigned Opcode = Inst.getOpcode(); 2792 unsigned NewOpcode = getVALUOp(Inst); 2793 2794 // Handle some special cases 2795 switch (Opcode) { 2796 default: 2797 break; 2798 case AMDGPU::S_AND_B64: 2799 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::V_AND_B32_e64); 2800 Inst.eraseFromParent(); 2801 continue; 2802 2803 case AMDGPU::S_OR_B64: 2804 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::V_OR_B32_e64); 2805 Inst.eraseFromParent(); 2806 continue; 2807 2808 case AMDGPU::S_XOR_B64: 2809 splitScalar64BitBinaryOp(Worklist, Inst, AMDGPU::V_XOR_B32_e64); 2810 Inst.eraseFromParent(); 2811 continue; 2812 2813 case AMDGPU::S_NOT_B64: 2814 splitScalar64BitUnaryOp(Worklist, Inst, AMDGPU::V_NOT_B32_e32); 2815 Inst.eraseFromParent(); 2816 continue; 2817 2818 case AMDGPU::S_BCNT1_I32_B64: 2819 splitScalar64BitBCNT(Worklist, Inst); 2820 Inst.eraseFromParent(); 2821 continue; 2822 2823 case AMDGPU::S_BFE_I64: { 2824 splitScalar64BitBFE(Worklist, Inst); 2825 Inst.eraseFromParent(); 2826 continue; 2827 } 2828 2829 case AMDGPU::S_LSHL_B32: 2830 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2831 NewOpcode = AMDGPU::V_LSHLREV_B32_e64; 2832 swapOperands(Inst); 2833 } 2834 break; 2835 case AMDGPU::S_ASHR_I32: 2836 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2837 NewOpcode = AMDGPU::V_ASHRREV_I32_e64; 2838 swapOperands(Inst); 2839 } 2840 break; 2841 case AMDGPU::S_LSHR_B32: 2842 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2843 NewOpcode = AMDGPU::V_LSHRREV_B32_e64; 2844 swapOperands(Inst); 2845 } 2846 break; 2847 case AMDGPU::S_LSHL_B64: 2848 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2849 NewOpcode = AMDGPU::V_LSHLREV_B64; 2850 swapOperands(Inst); 2851 } 2852 break; 2853 case AMDGPU::S_ASHR_I64: 2854 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2855 NewOpcode = AMDGPU::V_ASHRREV_I64; 2856 swapOperands(Inst); 2857 } 2858 break; 2859 case AMDGPU::S_LSHR_B64: 2860 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) { 2861 NewOpcode = AMDGPU::V_LSHRREV_B64; 2862 swapOperands(Inst); 2863 } 2864 break; 2865 2866 case AMDGPU::S_ABS_I32: 2867 lowerScalarAbs(Worklist, Inst); 2868 Inst.eraseFromParent(); 2869 continue; 2870 2871 case AMDGPU::S_CBRANCH_SCC0: 2872 case AMDGPU::S_CBRANCH_SCC1: 2873 // Clear unused bits of vcc 2874 BuildMI(*MBB, Inst, Inst.getDebugLoc(), get(AMDGPU::S_AND_B64), 2875 AMDGPU::VCC) 2876 .addReg(AMDGPU::EXEC) 2877 .addReg(AMDGPU::VCC); 2878 break; 2879 2880 case AMDGPU::S_BFE_U64: 2881 case AMDGPU::S_BFM_B64: 2882 llvm_unreachable("Moving this op to VALU not implemented"); 2883 } 2884 2885 if (NewOpcode == AMDGPU::INSTRUCTION_LIST_END) { 2886 // We cannot move this instruction to the VALU, so we should try to 2887 // legalize its operands instead. 2888 legalizeOperands(Inst); 2889 continue; 2890 } 2891 2892 // Use the new VALU Opcode. 2893 const MCInstrDesc &NewDesc = get(NewOpcode); 2894 Inst.setDesc(NewDesc); 2895 2896 // Remove any references to SCC. Vector instructions can't read from it, and 2897 // We're just about to add the implicit use / defs of VCC, and we don't want 2898 // both. 2899 for (unsigned i = Inst.getNumOperands() - 1; i > 0; --i) { 2900 MachineOperand &Op = Inst.getOperand(i); 2901 if (Op.isReg() && Op.getReg() == AMDGPU::SCC) { 2902 Inst.RemoveOperand(i); 2903 addSCCDefUsersToVALUWorklist(Inst, Worklist); 2904 } 2905 } 2906 2907 if (Opcode == AMDGPU::S_SEXT_I32_I8 || Opcode == AMDGPU::S_SEXT_I32_I16) { 2908 // We are converting these to a BFE, so we need to add the missing 2909 // operands for the size and offset. 2910 unsigned Size = (Opcode == AMDGPU::S_SEXT_I32_I8) ? 8 : 16; 2911 Inst.addOperand(MachineOperand::CreateImm(0)); 2912 Inst.addOperand(MachineOperand::CreateImm(Size)); 2913 2914 } else if (Opcode == AMDGPU::S_BCNT1_I32_B32) { 2915 // The VALU version adds the second operand to the result, so insert an 2916 // extra 0 operand. 2917 Inst.addOperand(MachineOperand::CreateImm(0)); 2918 } 2919 2920 Inst.addImplicitDefUseOperands(*Inst.getParent()->getParent()); 2921 2922 if (Opcode == AMDGPU::S_BFE_I32 || Opcode == AMDGPU::S_BFE_U32) { 2923 const MachineOperand &OffsetWidthOp = Inst.getOperand(2); 2924 // If we need to move this to VGPRs, we need to unpack the second operand 2925 // back into the 2 separate ones for bit offset and width. 2926 assert(OffsetWidthOp.isImm() && 2927 "Scalar BFE is only implemented for constant width and offset"); 2928 uint32_t Imm = OffsetWidthOp.getImm(); 2929 2930 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 2931 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 2932 Inst.RemoveOperand(2); // Remove old immediate. 2933 Inst.addOperand(MachineOperand::CreateImm(Offset)); 2934 Inst.addOperand(MachineOperand::CreateImm(BitWidth)); 2935 } 2936 2937 bool HasDst = Inst.getOperand(0).isReg() && Inst.getOperand(0).isDef(); 2938 unsigned NewDstReg = AMDGPU::NoRegister; 2939 if (HasDst) { 2940 // Update the destination register class. 2941 const TargetRegisterClass *NewDstRC = getDestEquivalentVGPRClass(Inst); 2942 if (!NewDstRC) 2943 continue; 2944 2945 unsigned DstReg = Inst.getOperand(0).getReg(); 2946 NewDstReg = MRI.createVirtualRegister(NewDstRC); 2947 MRI.replaceRegWith(DstReg, NewDstReg); 2948 } 2949 2950 // Legalize the operands 2951 legalizeOperands(Inst); 2952 2953 if (HasDst) 2954 addUsersToMoveToVALUWorklist(NewDstReg, MRI, Worklist); 2955 } 2956 } 2957 2958 void SIInstrInfo::lowerScalarAbs(SmallVectorImpl<MachineInstr *> &Worklist, 2959 MachineInstr &Inst) const { 2960 MachineBasicBlock &MBB = *Inst.getParent(); 2961 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 2962 MachineBasicBlock::iterator MII = Inst; 2963 DebugLoc DL = Inst.getDebugLoc(); 2964 2965 MachineOperand &Dest = Inst.getOperand(0); 2966 MachineOperand &Src = Inst.getOperand(1); 2967 unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 2968 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 2969 2970 BuildMI(MBB, MII, DL, get(AMDGPU::V_SUB_I32_e32), TmpReg) 2971 .addImm(0) 2972 .addReg(Src.getReg()); 2973 2974 BuildMI(MBB, MII, DL, get(AMDGPU::V_MAX_I32_e64), ResultReg) 2975 .addReg(Src.getReg()) 2976 .addReg(TmpReg); 2977 2978 MRI.replaceRegWith(Dest.getReg(), ResultReg); 2979 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 2980 } 2981 2982 void SIInstrInfo::splitScalar64BitUnaryOp( 2983 SmallVectorImpl<MachineInstr *> &Worklist, MachineInstr &Inst, 2984 unsigned Opcode) const { 2985 MachineBasicBlock &MBB = *Inst.getParent(); 2986 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 2987 2988 MachineOperand &Dest = Inst.getOperand(0); 2989 MachineOperand &Src0 = Inst.getOperand(1); 2990 DebugLoc DL = Inst.getDebugLoc(); 2991 2992 MachineBasicBlock::iterator MII = Inst; 2993 2994 const MCInstrDesc &InstDesc = get(Opcode); 2995 const TargetRegisterClass *Src0RC = Src0.isReg() ? 2996 MRI.getRegClass(Src0.getReg()) : 2997 &AMDGPU::SGPR_32RegClass; 2998 2999 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 3000 3001 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 3002 AMDGPU::sub0, Src0SubRC); 3003 3004 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 3005 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 3006 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 3007 3008 unsigned DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 3009 BuildMI(MBB, MII, DL, InstDesc, DestSub0) 3010 .addOperand(SrcReg0Sub0); 3011 3012 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 3013 AMDGPU::sub1, Src0SubRC); 3014 3015 unsigned DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 3016 BuildMI(MBB, MII, DL, InstDesc, DestSub1) 3017 .addOperand(SrcReg0Sub1); 3018 3019 unsigned FullDestReg = MRI.createVirtualRegister(NewDestRC); 3020 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 3021 .addReg(DestSub0) 3022 .addImm(AMDGPU::sub0) 3023 .addReg(DestSub1) 3024 .addImm(AMDGPU::sub1); 3025 3026 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 3027 3028 // We don't need to legalizeOperands here because for a single operand, src0 3029 // will support any kind of input. 3030 3031 // Move all users of this moved value. 3032 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 3033 } 3034 3035 void SIInstrInfo::splitScalar64BitBinaryOp( 3036 SmallVectorImpl<MachineInstr *> &Worklist, MachineInstr &Inst, 3037 unsigned Opcode) const { 3038 MachineBasicBlock &MBB = *Inst.getParent(); 3039 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3040 3041 MachineOperand &Dest = Inst.getOperand(0); 3042 MachineOperand &Src0 = Inst.getOperand(1); 3043 MachineOperand &Src1 = Inst.getOperand(2); 3044 DebugLoc DL = Inst.getDebugLoc(); 3045 3046 MachineBasicBlock::iterator MII = Inst; 3047 3048 const MCInstrDesc &InstDesc = get(Opcode); 3049 const TargetRegisterClass *Src0RC = Src0.isReg() ? 3050 MRI.getRegClass(Src0.getReg()) : 3051 &AMDGPU::SGPR_32RegClass; 3052 3053 const TargetRegisterClass *Src0SubRC = RI.getSubRegClass(Src0RC, AMDGPU::sub0); 3054 const TargetRegisterClass *Src1RC = Src1.isReg() ? 3055 MRI.getRegClass(Src1.getReg()) : 3056 &AMDGPU::SGPR_32RegClass; 3057 3058 const TargetRegisterClass *Src1SubRC = RI.getSubRegClass(Src1RC, AMDGPU::sub0); 3059 3060 MachineOperand SrcReg0Sub0 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 3061 AMDGPU::sub0, Src0SubRC); 3062 MachineOperand SrcReg1Sub0 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 3063 AMDGPU::sub0, Src1SubRC); 3064 3065 const TargetRegisterClass *DestRC = MRI.getRegClass(Dest.getReg()); 3066 const TargetRegisterClass *NewDestRC = RI.getEquivalentVGPRClass(DestRC); 3067 const TargetRegisterClass *NewDestSubRC = RI.getSubRegClass(NewDestRC, AMDGPU::sub0); 3068 3069 unsigned DestSub0 = MRI.createVirtualRegister(NewDestSubRC); 3070 MachineInstr &LoHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub0) 3071 .addOperand(SrcReg0Sub0) 3072 .addOperand(SrcReg1Sub0); 3073 3074 MachineOperand SrcReg0Sub1 = buildExtractSubRegOrImm(MII, MRI, Src0, Src0RC, 3075 AMDGPU::sub1, Src0SubRC); 3076 MachineOperand SrcReg1Sub1 = buildExtractSubRegOrImm(MII, MRI, Src1, Src1RC, 3077 AMDGPU::sub1, Src1SubRC); 3078 3079 unsigned DestSub1 = MRI.createVirtualRegister(NewDestSubRC); 3080 MachineInstr &HiHalf = *BuildMI(MBB, MII, DL, InstDesc, DestSub1) 3081 .addOperand(SrcReg0Sub1) 3082 .addOperand(SrcReg1Sub1); 3083 3084 unsigned FullDestReg = MRI.createVirtualRegister(NewDestRC); 3085 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), FullDestReg) 3086 .addReg(DestSub0) 3087 .addImm(AMDGPU::sub0) 3088 .addReg(DestSub1) 3089 .addImm(AMDGPU::sub1); 3090 3091 MRI.replaceRegWith(Dest.getReg(), FullDestReg); 3092 3093 // Try to legalize the operands in case we need to swap the order to keep it 3094 // valid. 3095 legalizeOperands(LoHalf); 3096 legalizeOperands(HiHalf); 3097 3098 // Move all users of this moved vlaue. 3099 addUsersToMoveToVALUWorklist(FullDestReg, MRI, Worklist); 3100 } 3101 3102 void SIInstrInfo::splitScalar64BitBCNT( 3103 SmallVectorImpl<MachineInstr *> &Worklist, MachineInstr &Inst) const { 3104 MachineBasicBlock &MBB = *Inst.getParent(); 3105 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3106 3107 MachineBasicBlock::iterator MII = Inst; 3108 DebugLoc DL = Inst.getDebugLoc(); 3109 3110 MachineOperand &Dest = Inst.getOperand(0); 3111 MachineOperand &Src = Inst.getOperand(1); 3112 3113 const MCInstrDesc &InstDesc = get(AMDGPU::V_BCNT_U32_B32_e64); 3114 const TargetRegisterClass *SrcRC = Src.isReg() ? 3115 MRI.getRegClass(Src.getReg()) : 3116 &AMDGPU::SGPR_32RegClass; 3117 3118 unsigned MidReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3119 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3120 3121 const TargetRegisterClass *SrcSubRC = RI.getSubRegClass(SrcRC, AMDGPU::sub0); 3122 3123 MachineOperand SrcRegSub0 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 3124 AMDGPU::sub0, SrcSubRC); 3125 MachineOperand SrcRegSub1 = buildExtractSubRegOrImm(MII, MRI, Src, SrcRC, 3126 AMDGPU::sub1, SrcSubRC); 3127 3128 BuildMI(MBB, MII, DL, InstDesc, MidReg) 3129 .addOperand(SrcRegSub0) 3130 .addImm(0); 3131 3132 BuildMI(MBB, MII, DL, InstDesc, ResultReg) 3133 .addOperand(SrcRegSub1) 3134 .addReg(MidReg); 3135 3136 MRI.replaceRegWith(Dest.getReg(), ResultReg); 3137 3138 // We don't need to legalize operands here. src0 for etiher instruction can be 3139 // an SGPR, and the second input is unused or determined here. 3140 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 3141 } 3142 3143 void SIInstrInfo::splitScalar64BitBFE(SmallVectorImpl<MachineInstr *> &Worklist, 3144 MachineInstr &Inst) const { 3145 MachineBasicBlock &MBB = *Inst.getParent(); 3146 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); 3147 MachineBasicBlock::iterator MII = Inst; 3148 DebugLoc DL = Inst.getDebugLoc(); 3149 3150 MachineOperand &Dest = Inst.getOperand(0); 3151 uint32_t Imm = Inst.getOperand(2).getImm(); 3152 uint32_t Offset = Imm & 0x3f; // Extract bits [5:0]. 3153 uint32_t BitWidth = (Imm & 0x7f0000) >> 16; // Extract bits [22:16]. 3154 3155 (void) Offset; 3156 3157 // Only sext_inreg cases handled. 3158 assert(Inst.getOpcode() == AMDGPU::S_BFE_I64 && BitWidth <= 32 && 3159 Offset == 0 && "Not implemented"); 3160 3161 if (BitWidth < 32) { 3162 unsigned MidRegLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3163 unsigned MidRegHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3164 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 3165 3166 BuildMI(MBB, MII, DL, get(AMDGPU::V_BFE_I32), MidRegLo) 3167 .addReg(Inst.getOperand(1).getReg(), 0, AMDGPU::sub0) 3168 .addImm(0) 3169 .addImm(BitWidth); 3170 3171 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e32), MidRegHi) 3172 .addImm(31) 3173 .addReg(MidRegLo); 3174 3175 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 3176 .addReg(MidRegLo) 3177 .addImm(AMDGPU::sub0) 3178 .addReg(MidRegHi) 3179 .addImm(AMDGPU::sub1); 3180 3181 MRI.replaceRegWith(Dest.getReg(), ResultReg); 3182 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 3183 return; 3184 } 3185 3186 MachineOperand &Src = Inst.getOperand(1); 3187 unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass); 3188 unsigned ResultReg = MRI.createVirtualRegister(&AMDGPU::VReg_64RegClass); 3189 3190 BuildMI(MBB, MII, DL, get(AMDGPU::V_ASHRREV_I32_e64), TmpReg) 3191 .addImm(31) 3192 .addReg(Src.getReg(), 0, AMDGPU::sub0); 3193 3194 BuildMI(MBB, MII, DL, get(TargetOpcode::REG_SEQUENCE), ResultReg) 3195 .addReg(Src.getReg(), 0, AMDGPU::sub0) 3196 .addImm(AMDGPU::sub0) 3197 .addReg(TmpReg) 3198 .addImm(AMDGPU::sub1); 3199 3200 MRI.replaceRegWith(Dest.getReg(), ResultReg); 3201 addUsersToMoveToVALUWorklist(ResultReg, MRI, Worklist); 3202 } 3203 3204 void SIInstrInfo::addUsersToMoveToVALUWorklist( 3205 unsigned DstReg, 3206 MachineRegisterInfo &MRI, 3207 SmallVectorImpl<MachineInstr *> &Worklist) const { 3208 for (MachineRegisterInfo::use_iterator I = MRI.use_begin(DstReg), 3209 E = MRI.use_end(); I != E; ++I) { 3210 MachineInstr &UseMI = *I->getParent(); 3211 if (!canReadVGPR(UseMI, I.getOperandNo())) { 3212 Worklist.push_back(&UseMI); 3213 } 3214 } 3215 } 3216 3217 void SIInstrInfo::addSCCDefUsersToVALUWorklist( 3218 MachineInstr &SCCDefInst, SmallVectorImpl<MachineInstr *> &Worklist) const { 3219 // This assumes that all the users of SCC are in the same block 3220 // as the SCC def. 3221 for (MachineInstr &MI : 3222 llvm::make_range(MachineBasicBlock::iterator(SCCDefInst), 3223 SCCDefInst.getParent()->end())) { 3224 // Exit if we find another SCC def. 3225 if (MI.findRegisterDefOperandIdx(AMDGPU::SCC) != -1) 3226 return; 3227 3228 if (MI.findRegisterUseOperandIdx(AMDGPU::SCC) != -1) 3229 Worklist.push_back(&MI); 3230 } 3231 } 3232 3233 const TargetRegisterClass *SIInstrInfo::getDestEquivalentVGPRClass( 3234 const MachineInstr &Inst) const { 3235 const TargetRegisterClass *NewDstRC = getOpRegClass(Inst, 0); 3236 3237 switch (Inst.getOpcode()) { 3238 // For target instructions, getOpRegClass just returns the virtual register 3239 // class associated with the operand, so we need to find an equivalent VGPR 3240 // register class in order to move the instruction to the VALU. 3241 case AMDGPU::COPY: 3242 case AMDGPU::PHI: 3243 case AMDGPU::REG_SEQUENCE: 3244 case AMDGPU::INSERT_SUBREG: 3245 if (RI.hasVGPRs(NewDstRC)) 3246 return nullptr; 3247 3248 NewDstRC = RI.getEquivalentVGPRClass(NewDstRC); 3249 if (!NewDstRC) 3250 return nullptr; 3251 return NewDstRC; 3252 default: 3253 return NewDstRC; 3254 } 3255 } 3256 3257 // Find the one SGPR operand we are allowed to use. 3258 unsigned SIInstrInfo::findUsedSGPR(const MachineInstr &MI, 3259 int OpIndices[3]) const { 3260 const MCInstrDesc &Desc = MI.getDesc(); 3261 3262 // Find the one SGPR operand we are allowed to use. 3263 // 3264 // First we need to consider the instruction's operand requirements before 3265 // legalizing. Some operands are required to be SGPRs, such as implicit uses 3266 // of VCC, but we are still bound by the constant bus requirement to only use 3267 // one. 3268 // 3269 // If the operand's class is an SGPR, we can never move it. 3270 3271 unsigned SGPRReg = findImplicitSGPRRead(MI); 3272 if (SGPRReg != AMDGPU::NoRegister) 3273 return SGPRReg; 3274 3275 unsigned UsedSGPRs[3] = { AMDGPU::NoRegister }; 3276 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo(); 3277 3278 for (unsigned i = 0; i < 3; ++i) { 3279 int Idx = OpIndices[i]; 3280 if (Idx == -1) 3281 break; 3282 3283 const MachineOperand &MO = MI.getOperand(Idx); 3284 if (!MO.isReg()) 3285 continue; 3286 3287 // Is this operand statically required to be an SGPR based on the operand 3288 // constraints? 3289 const TargetRegisterClass *OpRC = RI.getRegClass(Desc.OpInfo[Idx].RegClass); 3290 bool IsRequiredSGPR = RI.isSGPRClass(OpRC); 3291 if (IsRequiredSGPR) 3292 return MO.getReg(); 3293 3294 // If this could be a VGPR or an SGPR, Check the dynamic register class. 3295 unsigned Reg = MO.getReg(); 3296 const TargetRegisterClass *RegRC = MRI.getRegClass(Reg); 3297 if (RI.isSGPRClass(RegRC)) 3298 UsedSGPRs[i] = Reg; 3299 } 3300 3301 // We don't have a required SGPR operand, so we have a bit more freedom in 3302 // selecting operands to move. 3303 3304 // Try to select the most used SGPR. If an SGPR is equal to one of the 3305 // others, we choose that. 3306 // 3307 // e.g. 3308 // V_FMA_F32 v0, s0, s0, s0 -> No moves 3309 // V_FMA_F32 v0, s0, s1, s0 -> Move s1 3310 3311 // TODO: If some of the operands are 64-bit SGPRs and some 32, we should 3312 // prefer those. 3313 3314 if (UsedSGPRs[0] != AMDGPU::NoRegister) { 3315 if (UsedSGPRs[0] == UsedSGPRs[1] || UsedSGPRs[0] == UsedSGPRs[2]) 3316 SGPRReg = UsedSGPRs[0]; 3317 } 3318 3319 if (SGPRReg == AMDGPU::NoRegister && UsedSGPRs[1] != AMDGPU::NoRegister) { 3320 if (UsedSGPRs[1] == UsedSGPRs[2]) 3321 SGPRReg = UsedSGPRs[1]; 3322 } 3323 3324 return SGPRReg; 3325 } 3326 3327 MachineOperand *SIInstrInfo::getNamedOperand(MachineInstr &MI, 3328 unsigned OperandName) const { 3329 int Idx = AMDGPU::getNamedOperandIdx(MI.getOpcode(), OperandName); 3330 if (Idx == -1) 3331 return nullptr; 3332 3333 return &MI.getOperand(Idx); 3334 } 3335 3336 uint64_t SIInstrInfo::getDefaultRsrcDataFormat() const { 3337 uint64_t RsrcDataFormat = AMDGPU::RSRC_DATA_FORMAT; 3338 if (ST.isAmdHsaOS()) { 3339 RsrcDataFormat |= (1ULL << 56); 3340 3341 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 3342 // Set MTYPE = 2 3343 RsrcDataFormat |= (2ULL << 59); 3344 } 3345 3346 return RsrcDataFormat; 3347 } 3348 3349 uint64_t SIInstrInfo::getScratchRsrcWords23() const { 3350 uint64_t Rsrc23 = getDefaultRsrcDataFormat() | 3351 AMDGPU::RSRC_TID_ENABLE | 3352 0xffffffff; // Size; 3353 3354 uint64_t EltSizeValue = Log2_32(ST.getMaxPrivateElementSize()) - 1; 3355 3356 Rsrc23 |= (EltSizeValue << AMDGPU::RSRC_ELEMENT_SIZE_SHIFT) | 3357 // IndexStride = 64 3358 (UINT64_C(3) << AMDGPU::RSRC_INDEX_STRIDE_SHIFT); 3359 3360 // If TID_ENABLE is set, DATA_FORMAT specifies stride bits [14:17]. 3361 // Clear them unless we want a huge stride. 3362 if (ST.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) 3363 Rsrc23 &= ~AMDGPU::RSRC_DATA_FORMAT; 3364 3365 return Rsrc23; 3366 } 3367 3368 bool SIInstrInfo::isLowLatencyInstruction(const MachineInstr &MI) const { 3369 unsigned Opc = MI.getOpcode(); 3370 3371 return isSMRD(Opc); 3372 } 3373 3374 bool SIInstrInfo::isHighLatencyInstruction(const MachineInstr &MI) const { 3375 unsigned Opc = MI.getOpcode(); 3376 3377 return isMUBUF(Opc) || isMTBUF(Opc) || isMIMG(Opc); 3378 } 3379 3380 unsigned SIInstrInfo::isStackAccess(const MachineInstr &MI, 3381 int &FrameIndex) const { 3382 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::vaddr); 3383 if (!Addr || !Addr->isFI()) 3384 return AMDGPU::NoRegister; 3385 3386 assert(!MI.memoperands_empty() && 3387 (*MI.memoperands_begin())->getAddrSpace() == AMDGPUAS::PRIVATE_ADDRESS); 3388 3389 FrameIndex = Addr->getIndex(); 3390 return getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg(); 3391 } 3392 3393 unsigned SIInstrInfo::isSGPRStackAccess(const MachineInstr &MI, 3394 int &FrameIndex) const { 3395 const MachineOperand *Addr = getNamedOperand(MI, AMDGPU::OpName::addr); 3396 assert(Addr && Addr->isFI()); 3397 FrameIndex = Addr->getIndex(); 3398 return getNamedOperand(MI, AMDGPU::OpName::data)->getReg(); 3399 } 3400 3401 unsigned SIInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, 3402 int &FrameIndex) const { 3403 3404 if (!MI.mayLoad()) 3405 return AMDGPU::NoRegister; 3406 3407 if (isMUBUF(MI) || isVGPRSpill(MI)) 3408 return isStackAccess(MI, FrameIndex); 3409 3410 if (isSGPRSpill(MI)) 3411 return isSGPRStackAccess(MI, FrameIndex); 3412 3413 return AMDGPU::NoRegister; 3414 } 3415 3416 unsigned SIInstrInfo::isStoreToStackSlot(const MachineInstr &MI, 3417 int &FrameIndex) const { 3418 if (!MI.mayStore()) 3419 return AMDGPU::NoRegister; 3420 3421 if (isMUBUF(MI) || isVGPRSpill(MI)) 3422 return isStackAccess(MI, FrameIndex); 3423 3424 if (isSGPRSpill(MI)) 3425 return isSGPRStackAccess(MI, FrameIndex); 3426 3427 return AMDGPU::NoRegister; 3428 } 3429 3430 unsigned SIInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { 3431 unsigned Opc = MI.getOpcode(); 3432 const MCInstrDesc &Desc = getMCOpcodeFromPseudo(Opc); 3433 unsigned DescSize = Desc.getSize(); 3434 3435 // If we have a definitive size, we can use it. Otherwise we need to inspect 3436 // the operands to know the size. 3437 if (DescSize != 0) 3438 return DescSize; 3439 3440 // 4-byte instructions may have a 32-bit literal encoded after them. Check 3441 // operands that coud ever be literals. 3442 if (isVALU(MI) || isSALU(MI)) { 3443 int Src0Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src0); 3444 if (Src0Idx == -1) 3445 return 4; // No operands. 3446 3447 if (isLiteralConstantLike(MI.getOperand(Src0Idx), getOpSize(MI, Src0Idx))) 3448 return 8; 3449 3450 int Src1Idx = AMDGPU::getNamedOperandIdx(Opc, AMDGPU::OpName::src1); 3451 if (Src1Idx == -1) 3452 return 4; 3453 3454 if (isLiteralConstantLike(MI.getOperand(Src1Idx), getOpSize(MI, Src1Idx))) 3455 return 8; 3456 3457 return 4; 3458 } 3459 3460 switch (Opc) { 3461 case TargetOpcode::IMPLICIT_DEF: 3462 case TargetOpcode::KILL: 3463 case TargetOpcode::DBG_VALUE: 3464 case TargetOpcode::BUNDLE: 3465 case TargetOpcode::EH_LABEL: 3466 return 0; 3467 case TargetOpcode::INLINEASM: { 3468 const MachineFunction *MF = MI.getParent()->getParent(); 3469 const char *AsmStr = MI.getOperand(0).getSymbolName(); 3470 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); 3471 } 3472 default: 3473 llvm_unreachable("unable to find instruction size"); 3474 } 3475 } 3476 3477 ArrayRef<std::pair<int, const char *>> 3478 SIInstrInfo::getSerializableTargetIndices() const { 3479 static const std::pair<int, const char *> TargetIndices[] = { 3480 {AMDGPU::TI_CONSTDATA_START, "amdgpu-constdata-start"}, 3481 {AMDGPU::TI_SCRATCH_RSRC_DWORD0, "amdgpu-scratch-rsrc-dword0"}, 3482 {AMDGPU::TI_SCRATCH_RSRC_DWORD1, "amdgpu-scratch-rsrc-dword1"}, 3483 {AMDGPU::TI_SCRATCH_RSRC_DWORD2, "amdgpu-scratch-rsrc-dword2"}, 3484 {AMDGPU::TI_SCRATCH_RSRC_DWORD3, "amdgpu-scratch-rsrc-dword3"}}; 3485 return makeArrayRef(TargetIndices); 3486 } 3487 3488 /// This is used by the post-RA scheduler (SchedulePostRAList.cpp). The 3489 /// post-RA version of misched uses CreateTargetMIHazardRecognizer. 3490 ScheduleHazardRecognizer * 3491 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 3492 const ScheduleDAG *DAG) const { 3493 return new GCNHazardRecognizer(DAG->MF); 3494 } 3495 3496 /// This is the hazard recognizer used at -O0 by the PostRAHazardRecognizer 3497 /// pass. 3498 ScheduleHazardRecognizer * 3499 SIInstrInfo::CreateTargetPostRAHazardRecognizer(const MachineFunction &MF) const { 3500 return new GCNHazardRecognizer(MF); 3501 } 3502