1 //===-- SIRegisterInfo.cpp - SI Register 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 the TargetRegisterInfo class. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "SIRegisterInfo.h" 16 #include "SIInstrInfo.h" 17 #include "SIMachineFunctionInfo.h" 18 #include "llvm/CodeGen/MachineFrameInfo.h" 19 #include "llvm/CodeGen/MachineInstrBuilder.h" 20 #include "llvm/CodeGen/RegisterScavenging.h" 21 #include "llvm/IR/Function.h" 22 #include "llvm/IR/LLVMContext.h" 23 24 using namespace llvm; 25 26 SIRegisterInfo::SIRegisterInfo() : AMDGPURegisterInfo() { 27 unsigned NumRegPressureSets = getNumRegPressureSets(); 28 29 SGPR32SetID = NumRegPressureSets; 30 VGPR32SetID = NumRegPressureSets; 31 for (unsigned i = 0; i < NumRegPressureSets; ++i) { 32 if (strncmp("SGPR_32", getRegPressureSetName(i), 7) == 0) 33 SGPR32SetID = i; 34 else if (strncmp("VGPR_32", getRegPressureSetName(i), 7) == 0) 35 VGPR32SetID = i; 36 } 37 assert(SGPR32SetID < NumRegPressureSets && 38 VGPR32SetID < NumRegPressureSets); 39 } 40 41 void SIRegisterInfo::reserveRegisterTuples(BitVector &Reserved, unsigned Reg) const { 42 MCRegAliasIterator R(Reg, this, true); 43 44 for (; R.isValid(); ++R) 45 Reserved.set(*R); 46 } 47 48 unsigned SIRegisterInfo::reservedPrivateSegmentBufferReg( 49 const MachineFunction &MF) const { 50 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>(); 51 if (ST.hasSGPRInitBug()) { 52 // Leave space for flat_scr, xnack_mask, vcc, and alignment 53 unsigned BaseIdx = AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG - 8 - 4; 54 unsigned BaseReg(AMDGPU::SGPR_32RegClass.getRegister(BaseIdx)); 55 return getMatchingSuperReg(BaseReg, AMDGPU::sub0, &AMDGPU::SReg_128RegClass); 56 } 57 58 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 59 // 96/97 need to be reserved for flat_scr, 98/99 for xnack_mask, and 60 // 100/101 for vcc. This is the next sgpr128 down. 61 return AMDGPU::SGPR92_SGPR93_SGPR94_SGPR95; 62 } 63 64 return AMDGPU::SGPR96_SGPR97_SGPR98_SGPR99; 65 } 66 67 unsigned SIRegisterInfo::reservedPrivateSegmentWaveByteOffsetReg( 68 const MachineFunction &MF) const { 69 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>(); 70 if (ST.hasSGPRInitBug()) { 71 unsigned Idx = AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG - 6 - 1; 72 return AMDGPU::SGPR_32RegClass.getRegister(Idx); 73 } 74 75 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 76 // Next register before reservations for flat_scr, xnack_mask, vcc, 77 // and scratch resource. 78 return AMDGPU::SGPR91; 79 } 80 81 return AMDGPU::SGPR95; 82 } 83 84 BitVector SIRegisterInfo::getReservedRegs(const MachineFunction &MF) const { 85 BitVector Reserved(getNumRegs()); 86 Reserved.set(AMDGPU::INDIRECT_BASE_ADDR); 87 88 // EXEC_LO and EXEC_HI could be allocated and used as regular register, but 89 // this seems likely to result in bugs, so I'm marking them as reserved. 90 reserveRegisterTuples(Reserved, AMDGPU::EXEC); 91 reserveRegisterTuples(Reserved, AMDGPU::FLAT_SCR); 92 93 // Reserve the last 2 registers so we will always have at least 2 more that 94 // will physically contain VCC. 95 reserveRegisterTuples(Reserved, AMDGPU::SGPR102_SGPR103); 96 97 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>(); 98 99 if (ST.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 100 // SI/CI have 104 SGPRs. VI has 102. We need to shift down the reservation 101 // for VCC/XNACK_MASK/FLAT_SCR. 102 // 103 // TODO The SGPRs that alias to XNACK_MASK could be used as general purpose 104 // SGPRs when the XNACK feature is not used. This is currently not done 105 // because the code that counts SGPRs cannot account for such holes. 106 reserveRegisterTuples(Reserved, AMDGPU::SGPR96_SGPR97); 107 reserveRegisterTuples(Reserved, AMDGPU::SGPR98_SGPR99); 108 reserveRegisterTuples(Reserved, AMDGPU::SGPR100_SGPR101); 109 } 110 111 // Tonga and Iceland can only allocate a fixed number of SGPRs due 112 // to a hw bug. 113 if (ST.hasSGPRInitBug()) { 114 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs(); 115 // Reserve some SGPRs for FLAT_SCRATCH, XNACK_MASK, and VCC (6 SGPRs). 116 unsigned Limit = AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG - 6; 117 118 for (unsigned i = Limit; i < NumSGPRs; ++i) { 119 unsigned Reg = AMDGPU::SGPR_32RegClass.getRegister(i); 120 reserveRegisterTuples(Reserved, Reg); 121 } 122 } 123 124 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 125 126 unsigned ScratchWaveOffsetReg = MFI->getScratchWaveOffsetReg(); 127 if (ScratchWaveOffsetReg != AMDGPU::NoRegister) { 128 // Reserve 1 SGPR for scratch wave offset in case we need to spill. 129 reserveRegisterTuples(Reserved, ScratchWaveOffsetReg); 130 } 131 132 unsigned ScratchRSrcReg = MFI->getScratchRSrcReg(); 133 if (ScratchRSrcReg != AMDGPU::NoRegister) { 134 // Reserve 4 SGPRs for the scratch buffer resource descriptor in case we need 135 // to spill. 136 // TODO: May need to reserve a VGPR if doing LDS spilling. 137 reserveRegisterTuples(Reserved, ScratchRSrcReg); 138 assert(!isSubRegister(ScratchRSrcReg, ScratchWaveOffsetReg)); 139 } 140 141 return Reserved; 142 } 143 144 unsigned SIRegisterInfo::getRegPressureSetLimit(const MachineFunction &MF, 145 unsigned Idx) const { 146 const AMDGPUSubtarget &STI = MF.getSubtarget<AMDGPUSubtarget>(); 147 // FIXME: We should adjust the max number of waves based on LDS size. 148 unsigned SGPRLimit = getNumSGPRsAllowed(STI.getGeneration(), 149 STI.getMaxWavesPerCU()); 150 unsigned VGPRLimit = getNumVGPRsAllowed(STI.getMaxWavesPerCU()); 151 152 unsigned VSLimit = SGPRLimit + VGPRLimit; 153 154 for (regclass_iterator I = regclass_begin(), E = regclass_end(); 155 I != E; ++I) { 156 const TargetRegisterClass *RC = *I; 157 158 unsigned NumSubRegs = std::max((int)RC->getSize() / 4, 1); 159 unsigned Limit; 160 161 if (isPseudoRegClass(RC)) { 162 // FIXME: This is a hack. We should never be considering the pressure of 163 // these since no virtual register should ever have this class. 164 Limit = VSLimit; 165 } else if (isSGPRClass(RC)) { 166 Limit = SGPRLimit / NumSubRegs; 167 } else { 168 Limit = VGPRLimit / NumSubRegs; 169 } 170 171 const int *Sets = getRegClassPressureSets(RC); 172 assert(Sets); 173 for (unsigned i = 0; Sets[i] != -1; ++i) { 174 if (Sets[i] == (int)Idx) 175 return Limit; 176 } 177 } 178 return 256; 179 } 180 181 bool SIRegisterInfo::requiresRegisterScavenging(const MachineFunction &Fn) const { 182 return Fn.getFrameInfo()->hasStackObjects(); 183 } 184 185 static unsigned getNumSubRegsForSpillOp(unsigned Op) { 186 187 switch (Op) { 188 case AMDGPU::SI_SPILL_S512_SAVE: 189 case AMDGPU::SI_SPILL_S512_RESTORE: 190 case AMDGPU::SI_SPILL_V512_SAVE: 191 case AMDGPU::SI_SPILL_V512_RESTORE: 192 return 16; 193 case AMDGPU::SI_SPILL_S256_SAVE: 194 case AMDGPU::SI_SPILL_S256_RESTORE: 195 case AMDGPU::SI_SPILL_V256_SAVE: 196 case AMDGPU::SI_SPILL_V256_RESTORE: 197 return 8; 198 case AMDGPU::SI_SPILL_S128_SAVE: 199 case AMDGPU::SI_SPILL_S128_RESTORE: 200 case AMDGPU::SI_SPILL_V128_SAVE: 201 case AMDGPU::SI_SPILL_V128_RESTORE: 202 return 4; 203 case AMDGPU::SI_SPILL_V96_SAVE: 204 case AMDGPU::SI_SPILL_V96_RESTORE: 205 return 3; 206 case AMDGPU::SI_SPILL_S64_SAVE: 207 case AMDGPU::SI_SPILL_S64_RESTORE: 208 case AMDGPU::SI_SPILL_V64_SAVE: 209 case AMDGPU::SI_SPILL_V64_RESTORE: 210 return 2; 211 case AMDGPU::SI_SPILL_S32_SAVE: 212 case AMDGPU::SI_SPILL_S32_RESTORE: 213 case AMDGPU::SI_SPILL_V32_SAVE: 214 case AMDGPU::SI_SPILL_V32_RESTORE: 215 return 1; 216 default: llvm_unreachable("Invalid spill opcode"); 217 } 218 } 219 220 void SIRegisterInfo::buildScratchLoadStore(MachineBasicBlock::iterator MI, 221 unsigned LoadStoreOp, 222 unsigned Value, 223 unsigned ScratchRsrcReg, 224 unsigned ScratchOffset, 225 int64_t Offset, 226 RegScavenger *RS) const { 227 228 MachineBasicBlock *MBB = MI->getParent(); 229 const MachineFunction *MF = MI->getParent()->getParent(); 230 const SIInstrInfo *TII = 231 static_cast<const SIInstrInfo *>(MF->getSubtarget().getInstrInfo()); 232 LLVMContext &Ctx = MF->getFunction()->getContext(); 233 DebugLoc DL = MI->getDebugLoc(); 234 bool IsLoad = TII->get(LoadStoreOp).mayLoad(); 235 236 bool RanOutOfSGPRs = false; 237 bool Scavenged = false; 238 unsigned SOffset = ScratchOffset; 239 240 unsigned NumSubRegs = getNumSubRegsForSpillOp(MI->getOpcode()); 241 unsigned Size = NumSubRegs * 4; 242 243 if (!isUInt<12>(Offset + Size)) { 244 SOffset = RS->scavengeRegister(&AMDGPU::SGPR_32RegClass, MI, 0); 245 if (SOffset == AMDGPU::NoRegister) { 246 RanOutOfSGPRs = true; 247 SOffset = AMDGPU::SGPR0; 248 } else { 249 Scavenged = true; 250 } 251 BuildMI(*MBB, MI, DL, TII->get(AMDGPU::S_ADD_U32), SOffset) 252 .addReg(ScratchOffset) 253 .addImm(Offset); 254 Offset = 0; 255 } 256 257 if (RanOutOfSGPRs) 258 Ctx.emitError("Ran out of SGPRs for spilling VGPRS"); 259 260 for (unsigned i = 0, e = NumSubRegs; i != e; ++i, Offset += 4) { 261 unsigned SubReg = NumSubRegs > 1 ? 262 getPhysRegSubReg(Value, &AMDGPU::VGPR_32RegClass, i) : 263 Value; 264 265 unsigned SOffsetRegState = 0; 266 if (i + 1 == e && Scavenged) 267 SOffsetRegState |= RegState::Kill; 268 269 BuildMI(*MBB, MI, DL, TII->get(LoadStoreOp)) 270 .addReg(SubReg, getDefRegState(IsLoad)) 271 .addReg(ScratchRsrcReg) 272 .addReg(SOffset, SOffsetRegState) 273 .addImm(Offset) 274 .addImm(0) // glc 275 .addImm(0) // slc 276 .addImm(0) // tfe 277 .addReg(Value, RegState::Implicit | getDefRegState(IsLoad)) 278 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end()); 279 } 280 } 281 282 void SIRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator MI, 283 int SPAdj, unsigned FIOperandNum, 284 RegScavenger *RS) const { 285 MachineFunction *MF = MI->getParent()->getParent(); 286 MachineBasicBlock *MBB = MI->getParent(); 287 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); 288 MachineFrameInfo *FrameInfo = MF->getFrameInfo(); 289 const SIInstrInfo *TII = 290 static_cast<const SIInstrInfo *>(MF->getSubtarget().getInstrInfo()); 291 DebugLoc DL = MI->getDebugLoc(); 292 293 MachineOperand &FIOp = MI->getOperand(FIOperandNum); 294 int Index = MI->getOperand(FIOperandNum).getIndex(); 295 296 switch (MI->getOpcode()) { 297 // SGPR register spill 298 case AMDGPU::SI_SPILL_S512_SAVE: 299 case AMDGPU::SI_SPILL_S256_SAVE: 300 case AMDGPU::SI_SPILL_S128_SAVE: 301 case AMDGPU::SI_SPILL_S64_SAVE: 302 case AMDGPU::SI_SPILL_S32_SAVE: { 303 unsigned NumSubRegs = getNumSubRegsForSpillOp(MI->getOpcode()); 304 305 for (unsigned i = 0, e = NumSubRegs; i < e; ++i) { 306 unsigned SubReg = getPhysRegSubReg(MI->getOperand(0).getReg(), 307 &AMDGPU::SGPR_32RegClass, i); 308 struct SIMachineFunctionInfo::SpilledReg Spill = 309 MFI->getSpilledReg(MF, Index, i); 310 311 BuildMI(*MBB, MI, DL, 312 TII->getMCOpcodeFromPseudo(AMDGPU::V_WRITELANE_B32), 313 Spill.VGPR) 314 .addReg(SubReg) 315 .addImm(Spill.Lane); 316 317 // FIXME: Since this spills to another register instead of an actual 318 // frame index, we should delete the frame index when all references to 319 // it are fixed. 320 } 321 MI->eraseFromParent(); 322 break; 323 } 324 325 // SGPR register restore 326 case AMDGPU::SI_SPILL_S512_RESTORE: 327 case AMDGPU::SI_SPILL_S256_RESTORE: 328 case AMDGPU::SI_SPILL_S128_RESTORE: 329 case AMDGPU::SI_SPILL_S64_RESTORE: 330 case AMDGPU::SI_SPILL_S32_RESTORE: { 331 unsigned NumSubRegs = getNumSubRegsForSpillOp(MI->getOpcode()); 332 333 for (unsigned i = 0, e = NumSubRegs; i < e; ++i) { 334 unsigned SubReg = getPhysRegSubReg(MI->getOperand(0).getReg(), 335 &AMDGPU::SGPR_32RegClass, i); 336 struct SIMachineFunctionInfo::SpilledReg Spill = 337 MFI->getSpilledReg(MF, Index, i); 338 339 BuildMI(*MBB, MI, DL, 340 TII->getMCOpcodeFromPseudo(AMDGPU::V_READLANE_B32), 341 SubReg) 342 .addReg(Spill.VGPR) 343 .addImm(Spill.Lane) 344 .addReg(MI->getOperand(0).getReg(), RegState::ImplicitDefine); 345 } 346 347 // TODO: only do this when it is needed 348 switch (MF->getSubtarget<AMDGPUSubtarget>().getGeneration()) { 349 case AMDGPUSubtarget::SOUTHERN_ISLANDS: 350 // "VALU writes SGPR" -> "SMRD reads that SGPR" needs 4 wait states 351 // ("S_NOP 3") on SI 352 TII->insertWaitStates(MI, 4); 353 break; 354 case AMDGPUSubtarget::SEA_ISLANDS: 355 break; 356 default: // VOLCANIC_ISLANDS and later 357 // "VALU writes SGPR -> VMEM reads that SGPR" needs 5 wait states 358 // ("S_NOP 4") on VI and later. This also applies to VALUs which write 359 // VCC, but we're unlikely to see VMEM use VCC. 360 TII->insertWaitStates(MI, 5); 361 } 362 363 MI->eraseFromParent(); 364 break; 365 } 366 367 // VGPR register spill 368 case AMDGPU::SI_SPILL_V512_SAVE: 369 case AMDGPU::SI_SPILL_V256_SAVE: 370 case AMDGPU::SI_SPILL_V128_SAVE: 371 case AMDGPU::SI_SPILL_V96_SAVE: 372 case AMDGPU::SI_SPILL_V64_SAVE: 373 case AMDGPU::SI_SPILL_V32_SAVE: 374 buildScratchLoadStore(MI, AMDGPU::BUFFER_STORE_DWORD_OFFSET, 375 TII->getNamedOperand(*MI, AMDGPU::OpName::src)->getReg(), 376 TII->getNamedOperand(*MI, AMDGPU::OpName::scratch_rsrc)->getReg(), 377 TII->getNamedOperand(*MI, AMDGPU::OpName::scratch_offset)->getReg(), 378 FrameInfo->getObjectOffset(Index), RS); 379 MI->eraseFromParent(); 380 break; 381 case AMDGPU::SI_SPILL_V32_RESTORE: 382 case AMDGPU::SI_SPILL_V64_RESTORE: 383 case AMDGPU::SI_SPILL_V96_RESTORE: 384 case AMDGPU::SI_SPILL_V128_RESTORE: 385 case AMDGPU::SI_SPILL_V256_RESTORE: 386 case AMDGPU::SI_SPILL_V512_RESTORE: { 387 buildScratchLoadStore(MI, AMDGPU::BUFFER_LOAD_DWORD_OFFSET, 388 TII->getNamedOperand(*MI, AMDGPU::OpName::dst)->getReg(), 389 TII->getNamedOperand(*MI, AMDGPU::OpName::scratch_rsrc)->getReg(), 390 TII->getNamedOperand(*MI, AMDGPU::OpName::scratch_offset)->getReg(), 391 FrameInfo->getObjectOffset(Index), RS); 392 MI->eraseFromParent(); 393 break; 394 } 395 396 default: { 397 int64_t Offset = FrameInfo->getObjectOffset(Index); 398 FIOp.ChangeToImmediate(Offset); 399 if (!TII->isImmOperandLegal(MI, FIOperandNum, FIOp)) { 400 unsigned TmpReg = RS->scavengeRegister(&AMDGPU::VGPR_32RegClass, MI, SPAdj); 401 BuildMI(*MBB, MI, MI->getDebugLoc(), 402 TII->get(AMDGPU::V_MOV_B32_e32), TmpReg) 403 .addImm(Offset); 404 FIOp.ChangeToRegister(TmpReg, false, false, true); 405 } 406 } 407 } 408 } 409 410 unsigned SIRegisterInfo::getHWRegIndex(unsigned Reg) const { 411 return getEncodingValue(Reg) & 0xff; 412 } 413 414 // FIXME: This is very slow. It might be worth creating a map from physreg to 415 // register class. 416 const TargetRegisterClass *SIRegisterInfo::getPhysRegClass(unsigned Reg) const { 417 assert(!TargetRegisterInfo::isVirtualRegister(Reg)); 418 419 static const TargetRegisterClass *const BaseClasses[] = { 420 &AMDGPU::VGPR_32RegClass, 421 &AMDGPU::SReg_32RegClass, 422 &AMDGPU::VReg_64RegClass, 423 &AMDGPU::SReg_64RegClass, 424 &AMDGPU::VReg_96RegClass, 425 &AMDGPU::VReg_128RegClass, 426 &AMDGPU::SReg_128RegClass, 427 &AMDGPU::VReg_256RegClass, 428 &AMDGPU::SReg_256RegClass, 429 &AMDGPU::VReg_512RegClass, 430 &AMDGPU::SReg_512RegClass, 431 &AMDGPU::SCC_CLASSRegClass, 432 }; 433 434 for (const TargetRegisterClass *BaseClass : BaseClasses) { 435 if (BaseClass->contains(Reg)) { 436 return BaseClass; 437 } 438 } 439 return nullptr; 440 } 441 442 // TODO: It might be helpful to have some target specific flags in 443 // TargetRegisterClass to mark which classes are VGPRs to make this trivial. 444 bool SIRegisterInfo::hasVGPRs(const TargetRegisterClass *RC) const { 445 switch (RC->getSize()) { 446 case 0: return false; 447 case 1: return false; 448 case 4: 449 return getCommonSubClass(&AMDGPU::VGPR_32RegClass, RC) != nullptr; 450 case 8: 451 return getCommonSubClass(&AMDGPU::VReg_64RegClass, RC) != nullptr; 452 case 12: 453 return getCommonSubClass(&AMDGPU::VReg_96RegClass, RC) != nullptr; 454 case 16: 455 return getCommonSubClass(&AMDGPU::VReg_128RegClass, RC) != nullptr; 456 case 32: 457 return getCommonSubClass(&AMDGPU::VReg_256RegClass, RC) != nullptr; 458 case 64: 459 return getCommonSubClass(&AMDGPU::VReg_512RegClass, RC) != nullptr; 460 default: 461 llvm_unreachable("Invalid register class size"); 462 } 463 } 464 465 const TargetRegisterClass *SIRegisterInfo::getEquivalentVGPRClass( 466 const TargetRegisterClass *SRC) const { 467 switch (SRC->getSize()) { 468 case 4: 469 return &AMDGPU::VGPR_32RegClass; 470 case 8: 471 return &AMDGPU::VReg_64RegClass; 472 case 12: 473 return &AMDGPU::VReg_96RegClass; 474 case 16: 475 return &AMDGPU::VReg_128RegClass; 476 case 32: 477 return &AMDGPU::VReg_256RegClass; 478 case 64: 479 return &AMDGPU::VReg_512RegClass; 480 default: 481 llvm_unreachable("Invalid register class size"); 482 } 483 } 484 485 const TargetRegisterClass *SIRegisterInfo::getEquivalentSGPRClass( 486 const TargetRegisterClass *VRC) const { 487 switch (VRC->getSize()) { 488 case 4: 489 return &AMDGPU::SGPR_32RegClass; 490 case 8: 491 return &AMDGPU::SReg_64RegClass; 492 case 16: 493 return &AMDGPU::SReg_128RegClass; 494 case 32: 495 return &AMDGPU::SReg_256RegClass; 496 case 64: 497 return &AMDGPU::SReg_512RegClass; 498 default: 499 llvm_unreachable("Invalid register class size"); 500 } 501 } 502 503 const TargetRegisterClass *SIRegisterInfo::getSubRegClass( 504 const TargetRegisterClass *RC, unsigned SubIdx) const { 505 if (SubIdx == AMDGPU::NoSubRegister) 506 return RC; 507 508 // We can assume that each lane corresponds to one 32-bit register. 509 unsigned Count = countPopulation(getSubRegIndexLaneMask(SubIdx)); 510 if (isSGPRClass(RC)) { 511 switch (Count) { 512 case 1: 513 return &AMDGPU::SGPR_32RegClass; 514 case 2: 515 return &AMDGPU::SReg_64RegClass; 516 case 4: 517 return &AMDGPU::SReg_128RegClass; 518 case 8: 519 return &AMDGPU::SReg_256RegClass; 520 case 16: /* fall-through */ 521 default: 522 llvm_unreachable("Invalid sub-register class size"); 523 } 524 } else { 525 switch (Count) { 526 case 1: 527 return &AMDGPU::VGPR_32RegClass; 528 case 2: 529 return &AMDGPU::VReg_64RegClass; 530 case 3: 531 return &AMDGPU::VReg_96RegClass; 532 case 4: 533 return &AMDGPU::VReg_128RegClass; 534 case 8: 535 return &AMDGPU::VReg_256RegClass; 536 case 16: /* fall-through */ 537 default: 538 llvm_unreachable("Invalid sub-register class size"); 539 } 540 } 541 } 542 543 bool SIRegisterInfo::shouldRewriteCopySrc( 544 const TargetRegisterClass *DefRC, 545 unsigned DefSubReg, 546 const TargetRegisterClass *SrcRC, 547 unsigned SrcSubReg) const { 548 // We want to prefer the smallest register class possible, so we don't want to 549 // stop and rewrite on anything that looks like a subregister 550 // extract. Operations mostly don't care about the super register class, so we 551 // only want to stop on the most basic of copies between the smae register 552 // class. 553 // 554 // e.g. if we have something like 555 // vreg0 = ... 556 // vreg1 = ... 557 // vreg2 = REG_SEQUENCE vreg0, sub0, vreg1, sub1, vreg2, sub2 558 // vreg3 = COPY vreg2, sub0 559 // 560 // We want to look through the COPY to find: 561 // => vreg3 = COPY vreg0 562 563 // Plain copy. 564 return getCommonSubClass(DefRC, SrcRC) != nullptr; 565 } 566 567 unsigned SIRegisterInfo::getPhysRegSubReg(unsigned Reg, 568 const TargetRegisterClass *SubRC, 569 unsigned Channel) const { 570 571 switch (Reg) { 572 case AMDGPU::VCC: 573 switch(Channel) { 574 case 0: return AMDGPU::VCC_LO; 575 case 1: return AMDGPU::VCC_HI; 576 default: llvm_unreachable("Invalid SubIdx for VCC"); 577 } 578 579 case AMDGPU::FLAT_SCR: 580 switch (Channel) { 581 case 0: 582 return AMDGPU::FLAT_SCR_LO; 583 case 1: 584 return AMDGPU::FLAT_SCR_HI; 585 default: 586 llvm_unreachable("Invalid SubIdx for FLAT_SCR"); 587 } 588 break; 589 590 case AMDGPU::EXEC: 591 switch (Channel) { 592 case 0: 593 return AMDGPU::EXEC_LO; 594 case 1: 595 return AMDGPU::EXEC_HI; 596 default: 597 llvm_unreachable("Invalid SubIdx for EXEC"); 598 } 599 break; 600 } 601 602 const TargetRegisterClass *RC = getPhysRegClass(Reg); 603 // 32-bit registers don't have sub-registers, so we can just return the 604 // Reg. We need to have this check here, because the calculation below 605 // using getHWRegIndex() will fail with special 32-bit registers like 606 // VCC_LO, VCC_HI, EXEC_LO, EXEC_HI and M0. 607 if (RC->getSize() == 4) { 608 assert(Channel == 0); 609 return Reg; 610 } 611 612 unsigned Index = getHWRegIndex(Reg); 613 return SubRC->getRegister(Index + Channel); 614 } 615 616 bool SIRegisterInfo::opCanUseLiteralConstant(unsigned OpType) const { 617 return OpType == AMDGPU::OPERAND_REG_IMM32; 618 } 619 620 bool SIRegisterInfo::opCanUseInlineConstant(unsigned OpType) const { 621 if (opCanUseLiteralConstant(OpType)) 622 return true; 623 624 return OpType == AMDGPU::OPERAND_REG_INLINE_C; 625 } 626 627 // FIXME: Most of these are flexible with HSA and we don't need to reserve them 628 // as input registers if unused. Whether the dispatch ptr is necessary should be 629 // easy to detect from used intrinsics. Scratch setup is harder to know. 630 unsigned SIRegisterInfo::getPreloadedValue(const MachineFunction &MF, 631 enum PreloadedValue Value) const { 632 633 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); 634 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>(); 635 (void)ST; 636 switch (Value) { 637 case SIRegisterInfo::WORKGROUP_ID_X: 638 assert(MFI->hasWorkGroupIDX()); 639 return MFI->WorkGroupIDXSystemSGPR; 640 case SIRegisterInfo::WORKGROUP_ID_Y: 641 assert(MFI->hasWorkGroupIDY()); 642 return MFI->WorkGroupIDYSystemSGPR; 643 case SIRegisterInfo::WORKGROUP_ID_Z: 644 assert(MFI->hasWorkGroupIDZ()); 645 return MFI->WorkGroupIDZSystemSGPR; 646 case SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET: 647 return MFI->PrivateSegmentWaveByteOffsetSystemSGPR; 648 case SIRegisterInfo::PRIVATE_SEGMENT_BUFFER: 649 assert(ST.isAmdHsaOS() && "Non-HSA ABI currently uses relocations"); 650 assert(MFI->hasPrivateSegmentBuffer()); 651 return MFI->PrivateSegmentBufferUserSGPR; 652 case SIRegisterInfo::KERNARG_SEGMENT_PTR: 653 assert(MFI->hasKernargSegmentPtr()); 654 return MFI->KernargSegmentPtrUserSGPR; 655 case SIRegisterInfo::DISPATCH_ID: 656 llvm_unreachable("unimplemented"); 657 case SIRegisterInfo::FLAT_SCRATCH_INIT: 658 assert(MFI->hasFlatScratchInit()); 659 return MFI->FlatScratchInitUserSGPR; 660 case SIRegisterInfo::DISPATCH_PTR: 661 assert(MFI->hasDispatchPtr()); 662 return MFI->DispatchPtrUserSGPR; 663 case SIRegisterInfo::QUEUE_PTR: 664 llvm_unreachable("not implemented"); 665 case SIRegisterInfo::WORKITEM_ID_X: 666 assert(MFI->hasWorkItemIDX()); 667 return AMDGPU::VGPR0; 668 case SIRegisterInfo::WORKITEM_ID_Y: 669 assert(MFI->hasWorkItemIDY()); 670 return AMDGPU::VGPR1; 671 case SIRegisterInfo::WORKITEM_ID_Z: 672 assert(MFI->hasWorkItemIDZ()); 673 return AMDGPU::VGPR2; 674 } 675 llvm_unreachable("unexpected preloaded value type"); 676 } 677 678 /// \brief Returns a register that is not used at any point in the function. 679 /// If all registers are used, then this function will return 680 // AMDGPU::NoRegister. 681 unsigned SIRegisterInfo::findUnusedRegister(const MachineRegisterInfo &MRI, 682 const TargetRegisterClass *RC) const { 683 for (unsigned Reg : *RC) 684 if (!MRI.isPhysRegUsed(Reg)) 685 return Reg; 686 return AMDGPU::NoRegister; 687 } 688 689 unsigned SIRegisterInfo::getNumVGPRsAllowed(unsigned WaveCount) const { 690 switch(WaveCount) { 691 case 10: return 24; 692 case 9: return 28; 693 case 8: return 32; 694 case 7: return 36; 695 case 6: return 40; 696 case 5: return 48; 697 case 4: return 64; 698 case 3: return 84; 699 case 2: return 128; 700 default: return 256; 701 } 702 } 703 704 unsigned SIRegisterInfo::getNumSGPRsAllowed(AMDGPUSubtarget::Generation gen, 705 unsigned WaveCount) const { 706 if (gen >= AMDGPUSubtarget::VOLCANIC_ISLANDS) { 707 switch (WaveCount) { 708 case 10: return 80; 709 case 9: return 80; 710 case 8: return 96; 711 default: return 102; 712 } 713 } else { 714 switch(WaveCount) { 715 case 10: return 48; 716 case 9: return 56; 717 case 8: return 64; 718 case 7: return 72; 719 case 6: return 80; 720 case 5: return 96; 721 default: return 103; 722 } 723 } 724 } 725