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