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