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