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