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