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