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