LLVM  5.0.0
SIRegisterInfo.cpp
Go to the documentation of this file.
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 "AMDGPUSubtarget.h"
17 #include "SIInstrInfo.h"
18 #include "SIMachineFunctionInfo.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/LLVMContext.h"
24 
25 using namespace llvm;
26 
27 static bool hasPressureSet(const int *PSets, unsigned PSetID) {
28  for (unsigned i = 0; PSets[i] != -1; ++i) {
29  if (PSets[i] == (int)PSetID)
30  return true;
31  }
32  return false;
33 }
34 
35 void SIRegisterInfo::classifyPressureSet(unsigned PSetID, unsigned Reg,
36  BitVector &PressureSets) const {
37  for (MCRegUnitIterator U(Reg, this); U.isValid(); ++U) {
38  const int *PSets = getRegUnitPressureSets(*U);
39  if (hasPressureSet(PSets, PSetID)) {
40  PressureSets.set(PSetID);
41  break;
42  }
43  }
44 }
45 
47  "amdgpu-spill-sgpr-to-smem",
48  cl::desc("Use scalar stores to spill SGPRs if supported by subtarget"),
49  cl::init(false));
50 
52  "amdgpu-spill-sgpr-to-vgpr",
53  cl::desc("Enable spilling VGPRs to SGPRs"),
55  cl::init(true));
56 
59  SGPRPressureSets(getNumRegPressureSets()),
60  VGPRPressureSets(getNumRegPressureSets()),
61  SpillSGPRToVGPR(false),
62  SpillSGPRToSMEM(false) {
64  SpillSGPRToSMEM = true;
65  else if (EnableSpillSGPRToVGPR)
66  SpillSGPRToVGPR = true;
67 
68  unsigned NumRegPressureSets = getNumRegPressureSets();
69 
70  SGPRSetID = NumRegPressureSets;
71  VGPRSetID = NumRegPressureSets;
72 
73  for (unsigned i = 0; i < NumRegPressureSets; ++i) {
74  classifyPressureSet(i, AMDGPU::SGPR0, SGPRPressureSets);
75  classifyPressureSet(i, AMDGPU::VGPR0, VGPRPressureSets);
76  }
77 
78  // Determine the number of reg units for each pressure set.
79  std::vector<unsigned> PressureSetRegUnits(NumRegPressureSets, 0);
80  for (unsigned i = 0, e = getNumRegUnits(); i != e; ++i) {
81  const int *PSets = getRegUnitPressureSets(i);
82  for (unsigned j = 0; PSets[j] != -1; ++j) {
83  ++PressureSetRegUnits[PSets[j]];
84  }
85  }
86 
87  unsigned VGPRMax = 0, SGPRMax = 0;
88  for (unsigned i = 0; i < NumRegPressureSets; ++i) {
89  if (isVGPRPressureSet(i) && PressureSetRegUnits[i] > VGPRMax) {
90  VGPRSetID = i;
91  VGPRMax = PressureSetRegUnits[i];
92  continue;
93  }
94  if (isSGPRPressureSet(i) && PressureSetRegUnits[i] > SGPRMax) {
95  SGPRSetID = i;
96  SGPRMax = PressureSetRegUnits[i];
97  }
98  }
99 
100  assert(SGPRSetID < NumRegPressureSets &&
101  VGPRSetID < NumRegPressureSets);
102 }
103 
104 void SIRegisterInfo::reserveRegisterTuples(BitVector &Reserved, unsigned Reg) const {
105  MCRegAliasIterator R(Reg, this, true);
106 
107  for (; R.isValid(); ++R)
108  Reserved.set(*R);
109 }
110 
112  const MachineFunction &MF) const {
113 
114  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
115  unsigned BaseIdx = alignDown(ST.getMaxNumSGPRs(MF), 4) - 4;
116  unsigned BaseReg(AMDGPU::SGPR_32RegClass.getRegister(BaseIdx));
117  return getMatchingSuperReg(BaseReg, AMDGPU::sub0, &AMDGPU::SReg_128RegClass);
118 }
119 
120 static unsigned findPrivateSegmentWaveByteOffsetRegIndex(unsigned RegCount) {
121  unsigned Reg;
122 
123  // Try to place it in a hole after PrivateSegmentBufferReg.
124  if (RegCount & 3) {
125  // We cannot put the segment buffer in (Idx - 4) ... (Idx - 1) due to
126  // alignment constraints, so we have a hole where can put the wave offset.
127  Reg = RegCount - 1;
128  } else {
129  // We can put the segment buffer in (Idx - 4) ... (Idx - 1) and put the
130  // wave offset before it.
131  Reg = RegCount - 5;
132  }
133 
134  return Reg;
135 }
136 
138  const MachineFunction &MF) const {
139  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
141  return AMDGPU::SGPR_32RegClass.getRegister(Reg);
142 }
143 
145  const MachineFunction &MF) const {
146  return AMDGPU::SGPR32;
147 }
148 
150  BitVector Reserved(getNumRegs());
151  Reserved.set(AMDGPU::INDIRECT_BASE_ADDR);
152 
153  // EXEC_LO and EXEC_HI could be allocated and used as regular register, but
154  // this seems likely to result in bugs, so I'm marking them as reserved.
155  reserveRegisterTuples(Reserved, AMDGPU::EXEC);
156  reserveRegisterTuples(Reserved, AMDGPU::FLAT_SCR);
157 
158  // M0 has to be reserved so that llvm accepts it as a live-in into a block.
159  reserveRegisterTuples(Reserved, AMDGPU::M0);
160 
161  // Reserve the memory aperture registers.
162  reserveRegisterTuples(Reserved, AMDGPU::SRC_SHARED_BASE);
163  reserveRegisterTuples(Reserved, AMDGPU::SRC_SHARED_LIMIT);
164  reserveRegisterTuples(Reserved, AMDGPU::SRC_PRIVATE_BASE);
165  reserveRegisterTuples(Reserved, AMDGPU::SRC_PRIVATE_LIMIT);
166 
167  // Reserve Trap Handler registers - support is not implemented in Codegen.
168  reserveRegisterTuples(Reserved, AMDGPU::TBA);
169  reserveRegisterTuples(Reserved, AMDGPU::TMA);
170  reserveRegisterTuples(Reserved, AMDGPU::TTMP0_TTMP1);
171  reserveRegisterTuples(Reserved, AMDGPU::TTMP2_TTMP3);
172  reserveRegisterTuples(Reserved, AMDGPU::TTMP4_TTMP5);
173  reserveRegisterTuples(Reserved, AMDGPU::TTMP6_TTMP7);
174  reserveRegisterTuples(Reserved, AMDGPU::TTMP8_TTMP9);
175  reserveRegisterTuples(Reserved, AMDGPU::TTMP10_TTMP11);
176 
177  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
178 
179  unsigned MaxNumSGPRs = ST.getMaxNumSGPRs(MF);
180  unsigned TotalNumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
181  for (unsigned i = MaxNumSGPRs; i < TotalNumSGPRs; ++i) {
182  unsigned Reg = AMDGPU::SGPR_32RegClass.getRegister(i);
183  reserveRegisterTuples(Reserved, Reg);
184  }
185 
186  unsigned MaxNumVGPRs = ST.getMaxNumVGPRs(MF);
187  unsigned TotalNumVGPRs = AMDGPU::VGPR_32RegClass.getNumRegs();
188  for (unsigned i = MaxNumVGPRs; i < TotalNumVGPRs; ++i) {
189  unsigned Reg = AMDGPU::VGPR_32RegClass.getRegister(i);
190  reserveRegisterTuples(Reserved, Reg);
191  }
192 
194 
195  unsigned ScratchWaveOffsetReg = MFI->getScratchWaveOffsetReg();
196  if (ScratchWaveOffsetReg != AMDGPU::NoRegister) {
197  // Reserve 1 SGPR for scratch wave offset in case we need to spill.
198  reserveRegisterTuples(Reserved, ScratchWaveOffsetReg);
199  }
200 
201  unsigned ScratchRSrcReg = MFI->getScratchRSrcReg();
202  if (ScratchRSrcReg != AMDGPU::NoRegister) {
203  // Reserve 4 SGPRs for the scratch buffer resource descriptor in case we need
204  // to spill.
205  // TODO: May need to reserve a VGPR if doing LDS spilling.
206  reserveRegisterTuples(Reserved, ScratchRSrcReg);
207  assert(!isSubRegister(ScratchRSrcReg, ScratchWaveOffsetReg));
208  }
209 
210  // We have to assume the SP is needed in case there are calls in the function,
211  // which is detected after the function is lowered. If we aren't really going
212  // to need SP, don't bother reserving it.
213  unsigned StackPtrReg = MFI->getStackPtrOffsetReg();
214 
215  if (StackPtrReg != AMDGPU::NoRegister) {
216  reserveRegisterTuples(Reserved, StackPtrReg);
217  assert(!isSubRegister(ScratchRSrcReg, StackPtrReg));
218  }
219 
220  unsigned FrameReg = MFI->getFrameOffsetReg();
221  if (FrameReg != AMDGPU::NoRegister) {
222  reserveRegisterTuples(Reserved, FrameReg);
223  assert(!isSubRegister(ScratchRSrcReg, FrameReg));
224  }
225 
226  return Reserved;
227 }
228 
231  if (Info->isEntryFunction()) {
232  const MachineFrameInfo &MFI = Fn.getFrameInfo();
233  return MFI.hasStackObjects() || MFI.hasCalls();
234  }
235 
236  // May need scavenger for dealing with callee saved registers.
237  return true;
238 }
239 
241  return MF.getFrameInfo().hasStackObjects();
242 }
243 
245  const MachineFunction &MF) const {
246  // m0 is needed for the scalar store offset. m0 is unallocatable, so we can't
247  // create a virtual register for it during frame index elimination, so the
248  // scavenger is directly needed.
249  return MF.getFrameInfo().hasStackObjects() &&
250  MF.getSubtarget<SISubtarget>().hasScalarStores() &&
251  MF.getInfo<SIMachineFunctionInfo>()->hasSpilledSGPRs();
252 }
253 
255  const MachineFunction &) const {
256  // There are no special dedicated stack or frame pointers.
257  return true;
258 }
259 
261  // This helps catch bugs as verifier errors.
262  return true;
263 }
264 
267 
268  int OffIdx = AMDGPU::getNamedOperandIdx(MI->getOpcode(),
269  AMDGPU::OpName::offset);
270  return MI->getOperand(OffIdx).getImm();
271 }
272 
274  int Idx) const {
275  if (!SIInstrInfo::isMUBUF(*MI))
276  return 0;
277 
279  AMDGPU::OpName::vaddr) &&
280  "Should never see frame index on non-address operand");
281 
282  return getMUBUFInstrOffset(MI);
283 }
284 
286  if (!MI->mayLoadOrStore())
287  return false;
288 
289  int64_t FullOffset = Offset + getMUBUFInstrOffset(MI);
290 
291  return !isUInt<12>(FullOffset);
292 }
293 
295  unsigned BaseReg,
296  int FrameIdx,
297  int64_t Offset) const {
299  DebugLoc DL; // Defaults to "unknown"
300 
301  if (Ins != MBB->end())
302  DL = Ins->getDebugLoc();
303 
304  MachineFunction *MF = MBB->getParent();
305  const SISubtarget &Subtarget = MF->getSubtarget<SISubtarget>();
306  const SIInstrInfo *TII = Subtarget.getInstrInfo();
307 
308  if (Offset == 0) {
309  BuildMI(*MBB, Ins, DL, TII->get(AMDGPU::V_MOV_B32_e32), BaseReg)
310  .addFrameIndex(FrameIdx);
311  return;
312  }
313 
315  unsigned OffsetReg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
316 
317  unsigned FIReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
318 
319  BuildMI(*MBB, Ins, DL, TII->get(AMDGPU::S_MOV_B32), OffsetReg)
320  .addImm(Offset);
321  BuildMI(*MBB, Ins, DL, TII->get(AMDGPU::V_MOV_B32_e32), FIReg)
322  .addFrameIndex(FrameIdx);
323 
324  TII->getAddNoCarry(*MBB, Ins, DL, BaseReg)
325  .addReg(OffsetReg, RegState::Kill)
326  .addReg(FIReg);
327 }
328 
330  int64_t Offset) const {
331 
332  MachineBasicBlock *MBB = MI.getParent();
333  MachineFunction *MF = MBB->getParent();
334  const SISubtarget &Subtarget = MF->getSubtarget<SISubtarget>();
335  const SIInstrInfo *TII = Subtarget.getInstrInfo();
336 
337 #ifndef NDEBUG
338  // FIXME: Is it possible to be storing a frame index to itself?
339  bool SeenFI = false;
340  for (const MachineOperand &MO: MI.operands()) {
341  if (MO.isFI()) {
342  if (SeenFI)
343  llvm_unreachable("should not see multiple frame indices");
344 
345  SeenFI = true;
346  }
347  }
348 #endif
349 
350  MachineOperand *FIOp = TII->getNamedOperand(MI, AMDGPU::OpName::vaddr);
351  assert(FIOp && FIOp->isFI() && "frame index must be address operand");
352  assert(TII->isMUBUF(MI));
353  assert(TII->getNamedOperand(MI, AMDGPU::OpName::soffset)->getReg() ==
354  MF->getInfo<SIMachineFunctionInfo>()->getFrameOffsetReg() &&
355  "should only be seeing frame offset relative FrameIndex");
356 
357 
358  MachineOperand *OffsetOp = TII->getNamedOperand(MI, AMDGPU::OpName::offset);
359  int64_t NewOffset = OffsetOp->getImm() + Offset;
360  assert(isUInt<12>(NewOffset) && "offset should be legal");
361 
362  FIOp->ChangeToRegister(BaseReg, false);
363  OffsetOp->setImm(NewOffset);
364 }
365 
367  unsigned BaseReg,
368  int64_t Offset) const {
369  if (!SIInstrInfo::isMUBUF(*MI))
370  return false;
371 
372  int64_t NewOffset = Offset + getMUBUFInstrOffset(MI);
373 
374  return isUInt<12>(NewOffset);
375 }
376 
378  const MachineFunction &MF, unsigned Kind) const {
379  // This is inaccurate. It depends on the instruction and address space. The
380  // only place where we should hit this is for dealing with frame indexes /
381  // private accesses, so this is correct in that case.
382  return &AMDGPU::VGPR_32RegClass;
383 }
384 
385 static unsigned getNumSubRegsForSpillOp(unsigned Op) {
386 
387  switch (Op) {
388  case AMDGPU::SI_SPILL_S512_SAVE:
389  case AMDGPU::SI_SPILL_S512_RESTORE:
390  case AMDGPU::SI_SPILL_V512_SAVE:
391  case AMDGPU::SI_SPILL_V512_RESTORE:
392  return 16;
393  case AMDGPU::SI_SPILL_S256_SAVE:
394  case AMDGPU::SI_SPILL_S256_RESTORE:
395  case AMDGPU::SI_SPILL_V256_SAVE:
396  case AMDGPU::SI_SPILL_V256_RESTORE:
397  return 8;
398  case AMDGPU::SI_SPILL_S128_SAVE:
399  case AMDGPU::SI_SPILL_S128_RESTORE:
400  case AMDGPU::SI_SPILL_V128_SAVE:
401  case AMDGPU::SI_SPILL_V128_RESTORE:
402  return 4;
403  case AMDGPU::SI_SPILL_V96_SAVE:
404  case AMDGPU::SI_SPILL_V96_RESTORE:
405  return 3;
406  case AMDGPU::SI_SPILL_S64_SAVE:
407  case AMDGPU::SI_SPILL_S64_RESTORE:
408  case AMDGPU::SI_SPILL_V64_SAVE:
409  case AMDGPU::SI_SPILL_V64_RESTORE:
410  return 2;
411  case AMDGPU::SI_SPILL_S32_SAVE:
412  case AMDGPU::SI_SPILL_S32_RESTORE:
413  case AMDGPU::SI_SPILL_V32_SAVE:
414  case AMDGPU::SI_SPILL_V32_RESTORE:
415  return 1;
416  default: llvm_unreachable("Invalid spill opcode");
417  }
418 }
419 
420 static int getOffsetMUBUFStore(unsigned Opc) {
421  switch (Opc) {
422  case AMDGPU::BUFFER_STORE_DWORD_OFFEN:
423  return AMDGPU::BUFFER_STORE_DWORD_OFFSET;
424  case AMDGPU::BUFFER_STORE_BYTE_OFFEN:
425  return AMDGPU::BUFFER_STORE_BYTE_OFFSET;
426  case AMDGPU::BUFFER_STORE_SHORT_OFFEN:
427  return AMDGPU::BUFFER_STORE_SHORT_OFFSET;
428  case AMDGPU::BUFFER_STORE_DWORDX2_OFFEN:
429  return AMDGPU::BUFFER_STORE_DWORDX2_OFFSET;
430  case AMDGPU::BUFFER_STORE_DWORDX4_OFFEN:
431  return AMDGPU::BUFFER_STORE_DWORDX4_OFFSET;
432  default:
433  return -1;
434  }
435 }
436 
437 static int getOffsetMUBUFLoad(unsigned Opc) {
438  switch (Opc) {
439  case AMDGPU::BUFFER_LOAD_DWORD_OFFEN:
440  return AMDGPU::BUFFER_LOAD_DWORD_OFFSET;
441  case AMDGPU::BUFFER_LOAD_UBYTE_OFFEN:
442  return AMDGPU::BUFFER_LOAD_UBYTE_OFFSET;
443  case AMDGPU::BUFFER_LOAD_SBYTE_OFFEN:
444  return AMDGPU::BUFFER_LOAD_SBYTE_OFFSET;
445  case AMDGPU::BUFFER_LOAD_USHORT_OFFEN:
446  return AMDGPU::BUFFER_LOAD_USHORT_OFFSET;
447  case AMDGPU::BUFFER_LOAD_SSHORT_OFFEN:
448  return AMDGPU::BUFFER_LOAD_SSHORT_OFFSET;
449  case AMDGPU::BUFFER_LOAD_DWORDX2_OFFEN:
450  return AMDGPU::BUFFER_LOAD_DWORDX2_OFFSET;
451  case AMDGPU::BUFFER_LOAD_DWORDX4_OFFEN:
452  return AMDGPU::BUFFER_LOAD_DWORDX4_OFFSET;
453  default:
454  return -1;
455  }
456 }
457 
458 // This differs from buildSpillLoadStore by only scavenging a VGPR. It does not
459 // need to handle the case where an SGPR may need to be spilled while spilling.
461  MachineFrameInfo &MFI,
463  int Index,
464  int64_t Offset) {
465  MachineBasicBlock *MBB = MI->getParent();
466  const DebugLoc &DL = MI->getDebugLoc();
467  bool IsStore = MI->mayStore();
468 
469  unsigned Opc = MI->getOpcode();
470  int LoadStoreOp = IsStore ?
472  if (LoadStoreOp == -1)
473  return false;
474 
475  unsigned Reg = TII->getNamedOperand(*MI, AMDGPU::OpName::vdata)->getReg();
476 
477  BuildMI(*MBB, MI, DL, TII->get(LoadStoreOp))
478  .addReg(Reg, getDefRegState(!IsStore))
479  .add(*TII->getNamedOperand(*MI, AMDGPU::OpName::srsrc))
480  .add(*TII->getNamedOperand(*MI, AMDGPU::OpName::soffset))
481  .addImm(Offset)
482  .addImm(0) // glc
483  .addImm(0) // slc
484  .addImm(0) // tfe
485  .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
486  return true;
487 }
488 
489 void SIRegisterInfo::buildSpillLoadStore(MachineBasicBlock::iterator MI,
490  unsigned LoadStoreOp,
491  int Index,
492  unsigned ValueReg,
493  bool IsKill,
494  unsigned ScratchRsrcReg,
495  unsigned ScratchOffsetReg,
496  int64_t InstOffset,
497  MachineMemOperand *MMO,
498  RegScavenger *RS) const {
499  MachineBasicBlock *MBB = MI->getParent();
500  MachineFunction *MF = MI->getParent()->getParent();
501  const SISubtarget &ST = MF->getSubtarget<SISubtarget>();
502  const SIInstrInfo *TII = ST.getInstrInfo();
503  const MachineFrameInfo &MFI = MF->getFrameInfo();
504 
505  const MCInstrDesc &Desc = TII->get(LoadStoreOp);
506  const DebugLoc &DL = MI->getDebugLoc();
507  bool IsStore = Desc.mayStore();
508 
509  bool RanOutOfSGPRs = false;
510  bool Scavenged = false;
511  unsigned SOffset = ScratchOffsetReg;
512 
513  const TargetRegisterClass *RC = getRegClassForReg(MF->getRegInfo(), ValueReg);
514  unsigned NumSubRegs = AMDGPU::getRegBitWidth(RC->getID()) / 32;
515  unsigned Size = NumSubRegs * 4;
516  int64_t Offset = InstOffset + MFI.getObjectOffset(Index);
517  const int64_t OriginalImmOffset = Offset;
518 
519  unsigned Align = MFI.getObjectAlignment(Index);
520  const MachinePointerInfo &BasePtrInfo = MMO->getPointerInfo();
521 
522  if (!isUInt<12>(Offset + Size)) {
523  SOffset = AMDGPU::NoRegister;
524 
525  // We don't have access to the register scavenger if this function is called
526  // during PEI::scavengeFrameVirtualRegs().
527  if (RS)
528  SOffset = RS->FindUnusedReg(&AMDGPU::SGPR_32RegClass);
529 
530  if (SOffset == AMDGPU::NoRegister) {
531  // There are no free SGPRs, and since we are in the process of spilling
532  // VGPRs too. Since we need a VGPR in order to spill SGPRs (this is true
533  // on SI/CI and on VI it is true until we implement spilling using scalar
534  // stores), we have no way to free up an SGPR. Our solution here is to
535  // add the offset directly to the ScratchOffset register, and then
536  // subtract the offset after the spill to return ScratchOffset to it's
537  // original value.
538  RanOutOfSGPRs = true;
539  SOffset = ScratchOffsetReg;
540  } else {
541  Scavenged = true;
542  }
543 
544  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::S_ADD_U32), SOffset)
545  .addReg(ScratchOffsetReg)
546  .addImm(Offset);
547 
548  Offset = 0;
549  }
550 
551  const unsigned EltSize = 4;
552 
553  for (unsigned i = 0, e = NumSubRegs; i != e; ++i, Offset += EltSize) {
554  unsigned SubReg = NumSubRegs == 1 ?
555  ValueReg : getSubReg(ValueReg, getSubRegFromChannel(i));
556 
557  unsigned SOffsetRegState = 0;
558  unsigned SrcDstRegState = getDefRegState(!IsStore);
559  if (i + 1 == e) {
560  SOffsetRegState |= getKillRegState(Scavenged);
561  // The last implicit use carries the "Kill" flag.
562  SrcDstRegState |= getKillRegState(IsKill);
563  }
564 
565  MachinePointerInfo PInfo = BasePtrInfo.getWithOffset(EltSize * i);
566  MachineMemOperand *NewMMO
567  = MF->getMachineMemOperand(PInfo, MMO->getFlags(),
568  EltSize, MinAlign(Align, EltSize * i));
569 
570  auto MIB = BuildMI(*MBB, MI, DL, Desc)
571  .addReg(SubReg, getDefRegState(!IsStore) | getKillRegState(IsKill))
572  .addReg(ScratchRsrcReg)
573  .addReg(SOffset, SOffsetRegState)
574  .addImm(Offset)
575  .addImm(0) // glc
576  .addImm(0) // slc
577  .addImm(0) // tfe
578  .addMemOperand(NewMMO);
579 
580  if (NumSubRegs > 1)
581  MIB.addReg(ValueReg, RegState::Implicit | SrcDstRegState);
582  }
583 
584  if (RanOutOfSGPRs) {
585  // Subtract the offset we added to the ScratchOffset register.
586  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::S_SUB_U32), ScratchOffsetReg)
587  .addReg(ScratchOffsetReg)
588  .addImm(OriginalImmOffset);
589  }
590 }
591 
592 static std::pair<unsigned, unsigned> getSpillEltSize(unsigned SuperRegSize,
593  bool Store) {
594  if (SuperRegSize % 16 == 0) {
595  return { 16, Store ? AMDGPU::S_BUFFER_STORE_DWORDX4_SGPR :
596  AMDGPU::S_BUFFER_LOAD_DWORDX4_SGPR };
597  }
598 
599  if (SuperRegSize % 8 == 0) {
600  return { 8, Store ? AMDGPU::S_BUFFER_STORE_DWORDX2_SGPR :
601  AMDGPU::S_BUFFER_LOAD_DWORDX2_SGPR };
602  }
603 
604  return { 4, Store ? AMDGPU::S_BUFFER_STORE_DWORD_SGPR :
605  AMDGPU::S_BUFFER_LOAD_DWORD_SGPR};
606 }
607 
609  int Index,
610  RegScavenger *RS,
611  bool OnlyToVGPR) const {
612  MachineBasicBlock *MBB = MI->getParent();
613  MachineFunction *MF = MBB->getParent();
615 
617  = MFI->getSGPRToVGPRSpills(Index);
618  bool SpillToVGPR = !VGPRSpills.empty();
619  if (OnlyToVGPR && !SpillToVGPR)
620  return false;
621 
623  const SISubtarget &ST = MF->getSubtarget<SISubtarget>();
624  const SIInstrInfo *TII = ST.getInstrInfo();
625 
626  unsigned SuperReg = MI->getOperand(0).getReg();
627  bool IsKill = MI->getOperand(0).isKill();
628  const DebugLoc &DL = MI->getDebugLoc();
629 
630  MachineFrameInfo &FrameInfo = MF->getFrameInfo();
631 
632  bool SpillToSMEM = spillSGPRToSMEM();
633  if (SpillToSMEM && OnlyToVGPR)
634  return false;
635 
636  assert(SuperReg != AMDGPU::M0 && "m0 should never spill");
637 
638  unsigned OffsetReg = AMDGPU::M0;
639  unsigned M0CopyReg = AMDGPU::NoRegister;
640 
641  if (SpillToSMEM) {
642  if (RS->isRegUsed(AMDGPU::M0)) {
643  M0CopyReg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
644  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::COPY), M0CopyReg)
645  .addReg(AMDGPU::M0);
646  }
647  }
648 
649  unsigned ScalarStoreOp;
650  unsigned EltSize = 4;
651  const TargetRegisterClass *RC = getPhysRegClass(SuperReg);
652  if (SpillToSMEM && isSGPRClass(RC)) {
653  // XXX - if private_element_size is larger than 4 it might be useful to be
654  // able to spill wider vmem spills.
655  std::tie(EltSize, ScalarStoreOp) =
656  getSpillEltSize(getRegSizeInBits(*RC) / 8, true);
657  }
658 
659  ArrayRef<int16_t> SplitParts = getRegSplitParts(RC, EltSize);
660  unsigned NumSubRegs = SplitParts.empty() ? 1 : SplitParts.size();
661 
662  // SubReg carries the "Kill" flag when SubReg == SuperReg.
663  unsigned SubKillState = getKillRegState((NumSubRegs == 1) && IsKill);
664  for (unsigned i = 0, e = NumSubRegs; i < e; ++i) {
665  unsigned SubReg = NumSubRegs == 1 ?
666  SuperReg : getSubReg(SuperReg, SplitParts[i]);
667 
668  if (SpillToSMEM) {
669  int64_t FrOffset = FrameInfo.getObjectOffset(Index);
670 
671  // The allocated memory size is really the wavefront size * the frame
672  // index size. The widest register class is 64 bytes, so a 4-byte scratch
673  // allocation is enough to spill this in a single stack object.
674  //
675  // FIXME: Frame size/offsets are computed earlier than this, so the extra
676  // space is still unnecessarily allocated.
677 
678  unsigned Align = FrameInfo.getObjectAlignment(Index);
679  MachinePointerInfo PtrInfo
680  = MachinePointerInfo::getFixedStack(*MF, Index, EltSize * i);
681  MachineMemOperand *MMO
683  EltSize, MinAlign(Align, EltSize * i));
684 
685  // SMEM instructions only support a single offset, so increment the wave
686  // offset.
687 
688  int64_t Offset = (ST.getWavefrontSize() * FrOffset) + (EltSize * i);
689  if (Offset != 0) {
690  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::S_ADD_U32), OffsetReg)
691  .addReg(MFI->getFrameOffsetReg())
692  .addImm(Offset);
693  } else {
694  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::S_MOV_B32), OffsetReg)
695  .addReg(MFI->getFrameOffsetReg());
696  }
697 
698  BuildMI(*MBB, MI, DL, TII->get(ScalarStoreOp))
699  .addReg(SubReg, getKillRegState(IsKill)) // sdata
700  .addReg(MFI->getScratchRSrcReg()) // sbase
701  .addReg(OffsetReg, RegState::Kill) // soff
702  .addImm(0) // glc
703  .addMemOperand(MMO);
704 
705  continue;
706  }
707 
708  if (SpillToVGPR) {
710 
711  BuildMI(*MBB, MI, DL,
712  TII->getMCOpcodeFromPseudo(AMDGPU::V_WRITELANE_B32),
713  Spill.VGPR)
714  .addReg(SubReg, getKillRegState(IsKill))
715  .addImm(Spill.Lane);
716 
717  // FIXME: Since this spills to another register instead of an actual
718  // frame index, we should delete the frame index when all references to
719  // it are fixed.
720  } else {
721  // XXX - Can to VGPR spill fail for some subregisters but not others?
722  if (OnlyToVGPR)
723  return false;
724 
725  // Spill SGPR to a frame index.
726  // TODO: Should VI try to spill to VGPR and then spill to SMEM?
727  unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
728  // TODO: Should VI try to spill to VGPR and then spill to SMEM?
729 
731  = BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_MOV_B32_e32), TmpReg)
732  .addReg(SubReg, SubKillState);
733 
734 
735  // There could be undef components of a spilled super register.
736  // TODO: Can we detect this and skip the spill?
737  if (NumSubRegs > 1) {
738  // The last implicit use of the SuperReg carries the "Kill" flag.
739  unsigned SuperKillState = 0;
740  if (i + 1 == e)
741  SuperKillState |= getKillRegState(IsKill);
742  Mov.addReg(SuperReg, RegState::Implicit | SuperKillState);
743  }
744 
745  unsigned Align = FrameInfo.getObjectAlignment(Index);
746  MachinePointerInfo PtrInfo
747  = MachinePointerInfo::getFixedStack(*MF, Index, EltSize * i);
748  MachineMemOperand *MMO
750  EltSize, MinAlign(Align, EltSize * i));
751  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::SI_SPILL_V32_SAVE))
752  .addReg(TmpReg, RegState::Kill) // src
753  .addFrameIndex(Index) // vaddr
754  .addReg(MFI->getScratchRSrcReg()) // srrsrc
755  .addReg(MFI->getFrameOffsetReg()) // soffset
756  .addImm(i * 4) // offset
757  .addMemOperand(MMO);
758  }
759  }
760 
761  if (M0CopyReg != AMDGPU::NoRegister) {
762  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::COPY), AMDGPU::M0)
763  .addReg(M0CopyReg, RegState::Kill);
764  }
765 
766  MI->eraseFromParent();
767  MFI->addToSpilledSGPRs(NumSubRegs);
768  return true;
769 }
770 
772  int Index,
773  RegScavenger *RS,
774  bool OnlyToVGPR) const {
775  MachineFunction *MF = MI->getParent()->getParent();
777  MachineBasicBlock *MBB = MI->getParent();
779 
781  = MFI->getSGPRToVGPRSpills(Index);
782  bool SpillToVGPR = !VGPRSpills.empty();
783  if (OnlyToVGPR && !SpillToVGPR)
784  return false;
785 
786  MachineFrameInfo &FrameInfo = MF->getFrameInfo();
787  const SISubtarget &ST = MF->getSubtarget<SISubtarget>();
788  const SIInstrInfo *TII = ST.getInstrInfo();
789  const DebugLoc &DL = MI->getDebugLoc();
790 
791  unsigned SuperReg = MI->getOperand(0).getReg();
792  bool SpillToSMEM = spillSGPRToSMEM();
793  if (SpillToSMEM && OnlyToVGPR)
794  return false;
795 
796  assert(SuperReg != AMDGPU::M0 && "m0 should never spill");
797 
798  unsigned OffsetReg = AMDGPU::M0;
799  unsigned M0CopyReg = AMDGPU::NoRegister;
800 
801  if (SpillToSMEM) {
802  if (RS->isRegUsed(AMDGPU::M0)) {
803  M0CopyReg = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
804  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::COPY), M0CopyReg)
805  .addReg(AMDGPU::M0);
806  }
807  }
808 
809  unsigned EltSize = 4;
810  unsigned ScalarLoadOp;
811 
812  const TargetRegisterClass *RC = getPhysRegClass(SuperReg);
813  if (SpillToSMEM && isSGPRClass(RC)) {
814  // XXX - if private_element_size is larger than 4 it might be useful to be
815  // able to spill wider vmem spills.
816  std::tie(EltSize, ScalarLoadOp) =
817  getSpillEltSize(getRegSizeInBits(*RC) / 8, false);
818  }
819 
820  ArrayRef<int16_t> SplitParts = getRegSplitParts(RC, EltSize);
821  unsigned NumSubRegs = SplitParts.empty() ? 1 : SplitParts.size();
822 
823  // SubReg carries the "Kill" flag when SubReg == SuperReg.
824  int64_t FrOffset = FrameInfo.getObjectOffset(Index);
825 
826  for (unsigned i = 0, e = NumSubRegs; i < e; ++i) {
827  unsigned SubReg = NumSubRegs == 1 ?
828  SuperReg : getSubReg(SuperReg, SplitParts[i]);
829 
830  if (SpillToSMEM) {
831  // FIXME: Size may be > 4 but extra bytes wasted.
832  unsigned Align = FrameInfo.getObjectAlignment(Index);
833  MachinePointerInfo PtrInfo
834  = MachinePointerInfo::getFixedStack(*MF, Index, EltSize * i);
835  MachineMemOperand *MMO
837  EltSize, MinAlign(Align, EltSize * i));
838 
839  // Add i * 4 offset
840  int64_t Offset = (ST.getWavefrontSize() * FrOffset) + (EltSize * i);
841  if (Offset != 0) {
842  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::S_ADD_U32), OffsetReg)
843  .addReg(MFI->getFrameOffsetReg())
844  .addImm(Offset);
845  } else {
846  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::S_MOV_B32), OffsetReg)
847  .addReg(MFI->getFrameOffsetReg());
848  }
849 
850  auto MIB =
851  BuildMI(*MBB, MI, DL, TII->get(ScalarLoadOp), SubReg)
852  .addReg(MFI->getScratchRSrcReg()) // sbase
853  .addReg(OffsetReg, RegState::Kill) // soff
854  .addImm(0) // glc
855  .addMemOperand(MMO);
856 
857  if (NumSubRegs > 1)
858  MIB.addReg(SuperReg, RegState::ImplicitDefine);
859 
860  continue;
861  }
862 
863  if (SpillToVGPR) {
865  auto MIB =
866  BuildMI(*MBB, MI, DL, TII->getMCOpcodeFromPseudo(AMDGPU::V_READLANE_B32),
867  SubReg)
868  .addReg(Spill.VGPR)
869  .addImm(Spill.Lane);
870 
871  if (NumSubRegs > 1)
872  MIB.addReg(SuperReg, RegState::ImplicitDefine);
873  } else {
874  if (OnlyToVGPR)
875  return false;
876 
877  // Restore SGPR from a stack slot.
878  // FIXME: We should use S_LOAD_DWORD here for VI.
879  unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
880  unsigned Align = FrameInfo.getObjectAlignment(Index);
881 
882  MachinePointerInfo PtrInfo
883  = MachinePointerInfo::getFixedStack(*MF, Index, EltSize * i);
884 
885  MachineMemOperand *MMO = MF->getMachineMemOperand(PtrInfo,
886  MachineMemOperand::MOLoad, EltSize,
887  MinAlign(Align, EltSize * i));
888 
889  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::SI_SPILL_V32_RESTORE), TmpReg)
890  .addFrameIndex(Index) // vaddr
891  .addReg(MFI->getScratchRSrcReg()) // srsrc
892  .addReg(MFI->getFrameOffsetReg()) // soffset
893  .addImm(i * 4) // offset
894  .addMemOperand(MMO);
895 
896  auto MIB =
897  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), SubReg)
898  .addReg(TmpReg, RegState::Kill);
899 
900  if (NumSubRegs > 1)
901  MIB.addReg(MI->getOperand(0).getReg(), RegState::ImplicitDefine);
902  }
903  }
904 
905  if (M0CopyReg != AMDGPU::NoRegister) {
906  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::COPY), AMDGPU::M0)
907  .addReg(M0CopyReg, RegState::Kill);
908  }
909 
910  MI->eraseFromParent();
911  return true;
912 }
913 
914 /// Special case of eliminateFrameIndex. Returns true if the SGPR was spilled to
915 /// a VGPR and the stack slot can be safely eliminated when all other users are
916 /// handled.
919  int FI,
920  RegScavenger *RS) const {
921  switch (MI->getOpcode()) {
922  case AMDGPU::SI_SPILL_S512_SAVE:
923  case AMDGPU::SI_SPILL_S256_SAVE:
924  case AMDGPU::SI_SPILL_S128_SAVE:
925  case AMDGPU::SI_SPILL_S64_SAVE:
926  case AMDGPU::SI_SPILL_S32_SAVE:
927  return spillSGPR(MI, FI, RS, true);
928  case AMDGPU::SI_SPILL_S512_RESTORE:
929  case AMDGPU::SI_SPILL_S256_RESTORE:
930  case AMDGPU::SI_SPILL_S128_RESTORE:
931  case AMDGPU::SI_SPILL_S64_RESTORE:
932  case AMDGPU::SI_SPILL_S32_RESTORE:
933  return restoreSGPR(MI, FI, RS, true);
934  default:
935  llvm_unreachable("not an SGPR spill instruction");
936  }
937 }
938 
940  int SPAdj, unsigned FIOperandNum,
941  RegScavenger *RS) const {
942  MachineFunction *MF = MI->getParent()->getParent();
944  MachineBasicBlock *MBB = MI->getParent();
946  MachineFrameInfo &FrameInfo = MF->getFrameInfo();
947  const SISubtarget &ST = MF->getSubtarget<SISubtarget>();
948  const SIInstrInfo *TII = ST.getInstrInfo();
949  DebugLoc DL = MI->getDebugLoc();
950 
951  MachineOperand &FIOp = MI->getOperand(FIOperandNum);
952  int Index = MI->getOperand(FIOperandNum).getIndex();
953 
954  switch (MI->getOpcode()) {
955  // SGPR register spill
956  case AMDGPU::SI_SPILL_S512_SAVE:
957  case AMDGPU::SI_SPILL_S256_SAVE:
958  case AMDGPU::SI_SPILL_S128_SAVE:
959  case AMDGPU::SI_SPILL_S64_SAVE:
960  case AMDGPU::SI_SPILL_S32_SAVE: {
961  spillSGPR(MI, Index, RS);
962  break;
963  }
964 
965  // SGPR register restore
966  case AMDGPU::SI_SPILL_S512_RESTORE:
967  case AMDGPU::SI_SPILL_S256_RESTORE:
968  case AMDGPU::SI_SPILL_S128_RESTORE:
969  case AMDGPU::SI_SPILL_S64_RESTORE:
970  case AMDGPU::SI_SPILL_S32_RESTORE: {
971  restoreSGPR(MI, Index, RS);
972  break;
973  }
974 
975  // VGPR register spill
976  case AMDGPU::SI_SPILL_V512_SAVE:
977  case AMDGPU::SI_SPILL_V256_SAVE:
978  case AMDGPU::SI_SPILL_V128_SAVE:
979  case AMDGPU::SI_SPILL_V96_SAVE:
980  case AMDGPU::SI_SPILL_V64_SAVE:
981  case AMDGPU::SI_SPILL_V32_SAVE: {
982  const MachineOperand *VData = TII->getNamedOperand(*MI,
983  AMDGPU::OpName::vdata);
984  buildSpillLoadStore(MI, AMDGPU::BUFFER_STORE_DWORD_OFFSET,
985  Index,
986  VData->getReg(), VData->isKill(),
987  TII->getNamedOperand(*MI, AMDGPU::OpName::srsrc)->getReg(),
988  TII->getNamedOperand(*MI, AMDGPU::OpName::soffset)->getReg(),
989  TII->getNamedOperand(*MI, AMDGPU::OpName::offset)->getImm(),
990  *MI->memoperands_begin(),
991  RS);
992  MFI->addToSpilledVGPRs(getNumSubRegsForSpillOp(MI->getOpcode()));
993  MI->eraseFromParent();
994  break;
995  }
996  case AMDGPU::SI_SPILL_V32_RESTORE:
997  case AMDGPU::SI_SPILL_V64_RESTORE:
998  case AMDGPU::SI_SPILL_V96_RESTORE:
999  case AMDGPU::SI_SPILL_V128_RESTORE:
1000  case AMDGPU::SI_SPILL_V256_RESTORE:
1001  case AMDGPU::SI_SPILL_V512_RESTORE: {
1002  const MachineOperand *VData = TII->getNamedOperand(*MI,
1003  AMDGPU::OpName::vdata);
1004 
1005  buildSpillLoadStore(MI, AMDGPU::BUFFER_LOAD_DWORD_OFFSET,
1006  Index,
1007  VData->getReg(), VData->isKill(),
1008  TII->getNamedOperand(*MI, AMDGPU::OpName::srsrc)->getReg(),
1009  TII->getNamedOperand(*MI, AMDGPU::OpName::soffset)->getReg(),
1010  TII->getNamedOperand(*MI, AMDGPU::OpName::offset)->getImm(),
1011  *MI->memoperands_begin(),
1012  RS);
1013  MI->eraseFromParent();
1014  break;
1015  }
1016 
1017  default: {
1018  const DebugLoc &DL = MI->getDebugLoc();
1019  bool IsMUBUF = TII->isMUBUF(*MI);
1020 
1021  if (!IsMUBUF &&
1022  MFI->getFrameOffsetReg() != MFI->getScratchWaveOffsetReg()) {
1023  // Convert to an absolute stack address by finding the offset from the
1024  // scratch wave base and scaling by the wave size.
1025  //
1026  // In an entry function/kernel the stack address is already the absolute
1027  // address relative to the the scratch wave offset.
1028 
1029  unsigned DiffReg
1030  = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
1031 
1032  bool IsCopy = MI->getOpcode() == AMDGPU::V_MOV_B32_e32;
1033  unsigned ResultReg = IsCopy ?
1034  MI->getOperand(0).getReg() :
1035  MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1036 
1037  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::S_SUB_U32), DiffReg)
1038  .addReg(MFI->getFrameOffsetReg())
1039  .addReg(MFI->getScratchWaveOffsetReg());
1040 
1041  int64_t Offset = FrameInfo.getObjectOffset(Index);
1042  if (Offset == 0) {
1043  // XXX - This never happens because of emergency scavenging slot at 0?
1044  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_LSHRREV_B32_e64), ResultReg)
1045  .addImm(Log2_32(ST.getWavefrontSize()))
1046  .addReg(DiffReg);
1047  } else {
1048  unsigned CarryOut
1049  = MRI.createVirtualRegister(&AMDGPU::SReg_64_XEXECRegClass);
1050  unsigned ScaledReg
1051  = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1052 
1053  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_LSHRREV_B32_e64), ScaledReg)
1054  .addImm(Log2_32(ST.getWavefrontSize()))
1055  .addReg(DiffReg, RegState::Kill);
1056 
1057  // TODO: Fold if use instruction is another add of a constant.
1059  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_ADD_I32_e64), ResultReg)
1060  .addReg(CarryOut, RegState::Define | RegState::Dead)
1061  .addImm(Offset)
1062  .addReg(ScaledReg, RegState::Kill);
1063  } else {
1064  unsigned ConstOffsetReg
1065  = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
1066 
1067  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::S_MOV_B32), ConstOffsetReg)
1068  .addImm(Offset);
1069  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_ADD_I32_e64), ResultReg)
1070  .addReg(CarryOut, RegState::Define | RegState::Dead)
1071  .addReg(ConstOffsetReg, RegState::Kill)
1072  .addReg(ScaledReg, RegState::Kill);
1073  }
1074 
1075  MRI.setRegAllocationHint(CarryOut, 0, AMDGPU::VCC);
1076  }
1077 
1078  // Don't introduce an extra copy if we're just materializing in a mov.
1079  if (IsCopy)
1080  MI->eraseFromParent();
1081  else
1082  FIOp.ChangeToRegister(ResultReg, false, false, true);
1083  return;
1084  }
1085 
1086  if (IsMUBUF) {
1087  // Disable offen so we don't need a 0 vgpr base.
1088  assert(static_cast<int>(FIOperandNum) ==
1089  AMDGPU::getNamedOperandIdx(MI->getOpcode(),
1090  AMDGPU::OpName::vaddr));
1091 
1092  assert(TII->getNamedOperand(*MI, AMDGPU::OpName::soffset)->getReg()
1093  == MFI->getFrameOffsetReg());
1094 
1095  int64_t Offset = FrameInfo.getObjectOffset(Index);
1096  int64_t OldImm
1097  = TII->getNamedOperand(*MI, AMDGPU::OpName::offset)->getImm();
1098  int64_t NewOffset = OldImm + Offset;
1099 
1100  if (isUInt<12>(NewOffset) &&
1101  buildMUBUFOffsetLoadStore(TII, FrameInfo, MI, Index, NewOffset)) {
1102  MI->eraseFromParent();
1103  return;
1104  }
1105  }
1106 
1107  // If the offset is simply too big, don't convert to a scratch wave offset
1108  // relative index.
1109 
1110  int64_t Offset = FrameInfo.getObjectOffset(Index);
1111  FIOp.ChangeToImmediate(Offset);
1112  if (!TII->isImmOperandLegal(*MI, FIOperandNum, FIOp)) {
1113  unsigned TmpReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1114  BuildMI(*MBB, MI, DL, TII->get(AMDGPU::V_MOV_B32_e32), TmpReg)
1115  .addImm(Offset);
1116  FIOp.ChangeToRegister(TmpReg, false, false, true);
1117  }
1118  }
1119  }
1120 }
1121 
1123  #define AMDGPU_REG_ASM_NAMES
1124  #include "AMDGPURegAsmNames.inc.cpp"
1125 
1126  #define REG_RANGE(BeginReg, EndReg, RegTable) \
1127  if (Reg >= BeginReg && Reg <= EndReg) { \
1128  unsigned Index = Reg - BeginReg; \
1129  assert(Index < array_lengthof(RegTable)); \
1130  return RegTable[Index]; \
1131  }
1132 
1133  REG_RANGE(AMDGPU::VGPR0, AMDGPU::VGPR255, VGPR32RegNames);
1134  REG_RANGE(AMDGPU::SGPR0, AMDGPU::SGPR103, SGPR32RegNames);
1135  REG_RANGE(AMDGPU::VGPR0_VGPR1, AMDGPU::VGPR254_VGPR255, VGPR64RegNames);
1136  REG_RANGE(AMDGPU::SGPR0_SGPR1, AMDGPU::SGPR102_SGPR103, SGPR64RegNames);
1137  REG_RANGE(AMDGPU::VGPR0_VGPR1_VGPR2, AMDGPU::VGPR253_VGPR254_VGPR255,
1138  VGPR96RegNames);
1139 
1140  REG_RANGE(AMDGPU::VGPR0_VGPR1_VGPR2_VGPR3,
1141  AMDGPU::VGPR252_VGPR253_VGPR254_VGPR255,
1142  VGPR128RegNames);
1143  REG_RANGE(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3,
1144  AMDGPU::SGPR100_SGPR101_SGPR102_SGPR103,
1145  SGPR128RegNames);
1146 
1147  REG_RANGE(AMDGPU::VGPR0_VGPR1_VGPR2_VGPR3_VGPR4_VGPR5_VGPR6_VGPR7,
1148  AMDGPU::VGPR248_VGPR249_VGPR250_VGPR251_VGPR252_VGPR253_VGPR254_VGPR255,
1149  VGPR256RegNames);
1150 
1151  REG_RANGE(
1152  AMDGPU::VGPR0_VGPR1_VGPR2_VGPR3_VGPR4_VGPR5_VGPR6_VGPR7_VGPR8_VGPR9_VGPR10_VGPR11_VGPR12_VGPR13_VGPR14_VGPR15,
1153  AMDGPU::VGPR240_VGPR241_VGPR242_VGPR243_VGPR244_VGPR245_VGPR246_VGPR247_VGPR248_VGPR249_VGPR250_VGPR251_VGPR252_VGPR253_VGPR254_VGPR255,
1154  VGPR512RegNames);
1155 
1156  REG_RANGE(AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3_SGPR4_SGPR5_SGPR6_SGPR7,
1157  AMDGPU::SGPR96_SGPR97_SGPR98_SGPR99_SGPR100_SGPR101_SGPR102_SGPR103,
1158  SGPR256RegNames);
1159 
1160  REG_RANGE(
1161  AMDGPU::SGPR0_SGPR1_SGPR2_SGPR3_SGPR4_SGPR5_SGPR6_SGPR7_SGPR8_SGPR9_SGPR10_SGPR11_SGPR12_SGPR13_SGPR14_SGPR15,
1162  AMDGPU::SGPR88_SGPR89_SGPR90_SGPR91_SGPR92_SGPR93_SGPR94_SGPR95_SGPR96_SGPR97_SGPR98_SGPR99_SGPR100_SGPR101_SGPR102_SGPR103,
1163  SGPR512RegNames
1164  );
1165 
1166 #undef REG_RANGE
1167 
1168  // FIXME: Rename flat_scr so we don't need to special case this.
1169  switch (Reg) {
1170  case AMDGPU::FLAT_SCR:
1171  return "flat_scratch";
1172  case AMDGPU::FLAT_SCR_LO:
1173  return "flat_scratch_lo";
1174  case AMDGPU::FLAT_SCR_HI:
1175  return "flat_scratch_hi";
1176  default:
1177  // For the special named registers the default is fine.
1179  }
1180 }
1181 
1182 // FIXME: This is very slow. It might be worth creating a map from physreg to
1183 // register class.
1186 
1187  static const TargetRegisterClass *const BaseClasses[] = {
1188  &AMDGPU::VGPR_32RegClass,
1189  &AMDGPU::SReg_32RegClass,
1190  &AMDGPU::VReg_64RegClass,
1191  &AMDGPU::SReg_64RegClass,
1192  &AMDGPU::VReg_96RegClass,
1193  &AMDGPU::VReg_128RegClass,
1194  &AMDGPU::SReg_128RegClass,
1195  &AMDGPU::VReg_256RegClass,
1196  &AMDGPU::SReg_256RegClass,
1197  &AMDGPU::VReg_512RegClass,
1198  &AMDGPU::SReg_512RegClass,
1199  &AMDGPU::SCC_CLASSRegClass,
1200  };
1201 
1202  for (const TargetRegisterClass *BaseClass : BaseClasses) {
1203  if (BaseClass->contains(Reg)) {
1204  return BaseClass;
1205  }
1206  }
1207  return nullptr;
1208 }
1209 
1210 // TODO: It might be helpful to have some target specific flags in
1211 // TargetRegisterClass to mark which classes are VGPRs to make this trivial.
1213  unsigned Size = getRegSizeInBits(*RC);
1214  if (Size < 32)
1215  return false;
1216  switch (Size) {
1217  case 32:
1218  return getCommonSubClass(&AMDGPU::VGPR_32RegClass, RC) != nullptr;
1219  case 64:
1220  return getCommonSubClass(&AMDGPU::VReg_64RegClass, RC) != nullptr;
1221  case 96:
1222  return getCommonSubClass(&AMDGPU::VReg_96RegClass, RC) != nullptr;
1223  case 128:
1224  return getCommonSubClass(&AMDGPU::VReg_128RegClass, RC) != nullptr;
1225  case 256:
1226  return getCommonSubClass(&AMDGPU::VReg_256RegClass, RC) != nullptr;
1227  case 512:
1228  return getCommonSubClass(&AMDGPU::VReg_512RegClass, RC) != nullptr;
1229  default:
1230  llvm_unreachable("Invalid register class size");
1231  }
1232 }
1233 
1235  const TargetRegisterClass *SRC) const {
1236  switch (getRegSizeInBits(*SRC)) {
1237  case 32:
1238  return &AMDGPU::VGPR_32RegClass;
1239  case 64:
1240  return &AMDGPU::VReg_64RegClass;
1241  case 96:
1242  return &AMDGPU::VReg_96RegClass;
1243  case 128:
1244  return &AMDGPU::VReg_128RegClass;
1245  case 256:
1246  return &AMDGPU::VReg_256RegClass;
1247  case 512:
1248  return &AMDGPU::VReg_512RegClass;
1249  default:
1250  llvm_unreachable("Invalid register class size");
1251  }
1252 }
1253 
1255  const TargetRegisterClass *VRC) const {
1256  switch (getRegSizeInBits(*VRC)) {
1257  case 32:
1258  return &AMDGPU::SGPR_32RegClass;
1259  case 64:
1260  return &AMDGPU::SReg_64RegClass;
1261  case 128:
1262  return &AMDGPU::SReg_128RegClass;
1263  case 256:
1264  return &AMDGPU::SReg_256RegClass;
1265  case 512:
1266  return &AMDGPU::SReg_512RegClass;
1267  default:
1268  llvm_unreachable("Invalid register class size");
1269  }
1270 }
1271 
1273  const TargetRegisterClass *RC, unsigned SubIdx) const {
1274  if (SubIdx == AMDGPU::NoSubRegister)
1275  return RC;
1276 
1277  // We can assume that each lane corresponds to one 32-bit register.
1278  LaneBitmask::Type Mask = getSubRegIndexLaneMask(SubIdx).getAsInteger();
1279  unsigned Count = countPopulation(Mask);
1280  if (isSGPRClass(RC)) {
1281  switch (Count) {
1282  case 1:
1283  return &AMDGPU::SGPR_32RegClass;
1284  case 2:
1285  return &AMDGPU::SReg_64RegClass;
1286  case 4:
1287  return &AMDGPU::SReg_128RegClass;
1288  case 8:
1289  return &AMDGPU::SReg_256RegClass;
1290  case 16: /* fall-through */
1291  default:
1292  llvm_unreachable("Invalid sub-register class size");
1293  }
1294  } else {
1295  switch (Count) {
1296  case 1:
1297  return &AMDGPU::VGPR_32RegClass;
1298  case 2:
1299  return &AMDGPU::VReg_64RegClass;
1300  case 3:
1301  return &AMDGPU::VReg_96RegClass;
1302  case 4:
1303  return &AMDGPU::VReg_128RegClass;
1304  case 8:
1305  return &AMDGPU::VReg_256RegClass;
1306  case 16: /* fall-through */
1307  default:
1308  llvm_unreachable("Invalid sub-register class size");
1309  }
1310  }
1311 }
1312 
1314  const TargetRegisterClass *DefRC,
1315  unsigned DefSubReg,
1316  const TargetRegisterClass *SrcRC,
1317  unsigned SrcSubReg) const {
1318  // We want to prefer the smallest register class possible, so we don't want to
1319  // stop and rewrite on anything that looks like a subregister
1320  // extract. Operations mostly don't care about the super register class, so we
1321  // only want to stop on the most basic of copies between the same register
1322  // class.
1323  //
1324  // e.g. if we have something like
1325  // vreg0 = ...
1326  // vreg1 = ...
1327  // vreg2 = REG_SEQUENCE vreg0, sub0, vreg1, sub1, vreg2, sub2
1328  // vreg3 = COPY vreg2, sub0
1329  //
1330  // We want to look through the COPY to find:
1331  // => vreg3 = COPY vreg0
1332 
1333  // Plain copy.
1334  return getCommonSubClass(DefRC, SrcRC) != nullptr;
1335 }
1336 
1337 // FIXME: Most of these are flexible with HSA and we don't need to reserve them
1338 // as input registers if unused. Whether the dispatch ptr is necessary should be
1339 // easy to detect from used intrinsics. Scratch setup is harder to know.
1341  enum PreloadedValue Value) const {
1342 
1344  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
1345  (void)ST;
1346  switch (Value) {
1348  assert(MFI->hasWorkGroupIDX());
1349  return MFI->WorkGroupIDXSystemSGPR;
1351  assert(MFI->hasWorkGroupIDY());
1352  return MFI->WorkGroupIDYSystemSGPR;
1354  assert(MFI->hasWorkGroupIDZ());
1355  return MFI->WorkGroupIDZSystemSGPR;
1357  return MFI->PrivateSegmentWaveByteOffsetSystemSGPR;
1360  return MFI->PrivateSegmentBufferUserSGPR;
1362  assert(MFI->hasImplicitBufferPtr());
1363  return MFI->ImplicitBufferPtrUserSGPR;
1365  assert(MFI->hasKernargSegmentPtr());
1366  return MFI->KernargSegmentPtrUserSGPR;
1368  assert(MFI->hasDispatchID());
1369  return MFI->DispatchIDUserSGPR;
1371  assert(MFI->hasFlatScratchInit());
1372  return MFI->FlatScratchInitUserSGPR;
1374  assert(MFI->hasDispatchPtr());
1375  return MFI->DispatchPtrUserSGPR;
1377  assert(MFI->hasQueuePtr());
1378  return MFI->QueuePtrUserSGPR;
1380  assert(MFI->hasWorkItemIDX());
1381  return AMDGPU::VGPR0;
1383  assert(MFI->hasWorkItemIDY());
1384  return AMDGPU::VGPR1;
1386  assert(MFI->hasWorkItemIDZ());
1387  return AMDGPU::VGPR2;
1388  }
1389  llvm_unreachable("unexpected preloaded value type");
1390 }
1391 
1392 /// \brief Returns a register that is not used at any point in the function.
1393 /// If all registers are used, then this function will return
1394 // AMDGPU::NoRegister.
1395 unsigned
1397  const TargetRegisterClass *RC,
1398  const MachineFunction &MF) const {
1399 
1400  for (unsigned Reg : *RC)
1401  if (MRI.isAllocatable(Reg) && !MRI.isPhysRegUsed(Reg))
1402  return Reg;
1403  return AMDGPU::NoRegister;
1404 }
1405 
1407  unsigned EltSize) const {
1408  if (EltSize == 4) {
1409  static const int16_t Sub0_15[] = {
1410  AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
1411  AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7,
1412  AMDGPU::sub8, AMDGPU::sub9, AMDGPU::sub10, AMDGPU::sub11,
1413  AMDGPU::sub12, AMDGPU::sub13, AMDGPU::sub14, AMDGPU::sub15,
1414  };
1415 
1416  static const int16_t Sub0_7[] = {
1417  AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
1418  AMDGPU::sub4, AMDGPU::sub5, AMDGPU::sub6, AMDGPU::sub7,
1419  };
1420 
1421  static const int16_t Sub0_3[] = {
1422  AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2, AMDGPU::sub3,
1423  };
1424 
1425  static const int16_t Sub0_2[] = {
1426  AMDGPU::sub0, AMDGPU::sub1, AMDGPU::sub2,
1427  };
1428 
1429  static const int16_t Sub0_1[] = {
1430  AMDGPU::sub0, AMDGPU::sub1,
1431  };
1432 
1433  switch (AMDGPU::getRegBitWidth(*RC->MC)) {
1434  case 32:
1435  return {};
1436  case 64:
1437  return makeArrayRef(Sub0_1);
1438  case 96:
1439  return makeArrayRef(Sub0_2);
1440  case 128:
1441  return makeArrayRef(Sub0_3);
1442  case 256:
1443  return makeArrayRef(Sub0_7);
1444  case 512:
1445  return makeArrayRef(Sub0_15);
1446  default:
1447  llvm_unreachable("unhandled register size");
1448  }
1449  }
1450 
1451  if (EltSize == 8) {
1452  static const int16_t Sub0_15_64[] = {
1453  AMDGPU::sub0_sub1, AMDGPU::sub2_sub3,
1454  AMDGPU::sub4_sub5, AMDGPU::sub6_sub7,
1455  AMDGPU::sub8_sub9, AMDGPU::sub10_sub11,
1456  AMDGPU::sub12_sub13, AMDGPU::sub14_sub15
1457  };
1458 
1459  static const int16_t Sub0_7_64[] = {
1460  AMDGPU::sub0_sub1, AMDGPU::sub2_sub3,
1461  AMDGPU::sub4_sub5, AMDGPU::sub6_sub7
1462  };
1463 
1464 
1465  static const int16_t Sub0_3_64[] = {
1466  AMDGPU::sub0_sub1, AMDGPU::sub2_sub3
1467  };
1468 
1469  switch (AMDGPU::getRegBitWidth(*RC->MC)) {
1470  case 64:
1471  return {};
1472  case 128:
1473  return makeArrayRef(Sub0_3_64);
1474  case 256:
1475  return makeArrayRef(Sub0_7_64);
1476  case 512:
1477  return makeArrayRef(Sub0_15_64);
1478  default:
1479  llvm_unreachable("unhandled register size");
1480  }
1481  }
1482 
1483  assert(EltSize == 16 && "unhandled register spill split size");
1484 
1485  static const int16_t Sub0_15_128[] = {
1486  AMDGPU::sub0_sub1_sub2_sub3,
1487  AMDGPU::sub4_sub5_sub6_sub7,
1488  AMDGPU::sub8_sub9_sub10_sub11,
1489  AMDGPU::sub12_sub13_sub14_sub15
1490  };
1491 
1492  static const int16_t Sub0_7_128[] = {
1493  AMDGPU::sub0_sub1_sub2_sub3,
1494  AMDGPU::sub4_sub5_sub6_sub7
1495  };
1496 
1497  switch (AMDGPU::getRegBitWidth(*RC->MC)) {
1498  case 128:
1499  return {};
1500  case 256:
1501  return makeArrayRef(Sub0_7_128);
1502  case 512:
1503  return makeArrayRef(Sub0_15_128);
1504  default:
1505  llvm_unreachable("unhandled register size");
1506  }
1507 }
1508 
1509 const TargetRegisterClass*
1511  unsigned Reg) const {
1513  return MRI.getRegClass(Reg);
1514 
1515  return getPhysRegClass(Reg);
1516 }
1517 
1519  unsigned Reg) const {
1520  return hasVGPRs(getRegClassForReg(MRI, Reg));
1521 }
1522 
1524  const TargetRegisterClass *SrcRC,
1525  unsigned SubReg,
1526  const TargetRegisterClass *DstRC,
1527  unsigned DstSubReg,
1528  const TargetRegisterClass *NewRC) const {
1529  unsigned SrcSize = getRegSizeInBits(*SrcRC);
1530  unsigned DstSize = getRegSizeInBits(*DstRC);
1531  unsigned NewSize = getRegSizeInBits(*NewRC);
1532 
1533  // Do not increase size of registers beyond dword, we would need to allocate
1534  // adjacent registers and constraint regalloc more than needed.
1535 
1536  // Always allow dword coalescing.
1537  if (SrcSize <= 32 || DstSize <= 32)
1538  return true;
1539 
1540  return NewSize <= DstSize || NewSize <= SrcSize;
1541 }
1542 
1544  MachineFunction &MF) const {
1545 
1546  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
1548 
1549  unsigned Occupancy = ST.getOccupancyWithLocalMemSize(MFI->getLDSSize(),
1550  *MF.getFunction());
1551  switch (RC->getID()) {
1552  default:
1553  return AMDGPURegisterInfo::getRegPressureLimit(RC, MF);
1554  case AMDGPU::VGPR_32RegClassID:
1555  return std::min(ST.getMaxNumVGPRs(Occupancy), ST.getMaxNumVGPRs(MF));
1556  case AMDGPU::SGPR_32RegClassID:
1557  return std::min(ST.getMaxNumSGPRs(Occupancy, true), ST.getMaxNumSGPRs(MF));
1558  }
1559 }
1560 
1562  unsigned Idx) const {
1563  if (Idx == getVGPRPressureSet())
1564  return getRegPressureLimit(&AMDGPU::VGPR_32RegClass,
1565  const_cast<MachineFunction &>(MF));
1566 
1567  if (Idx == getSGPRPressureSet())
1568  return getRegPressureLimit(&AMDGPU::SGPR_32RegClass,
1569  const_cast<MachineFunction &>(MF));
1570 
1571  return AMDGPURegisterInfo::getRegPressureSetLimit(MF, Idx);
1572 }
1573 
1574 const int *SIRegisterInfo::getRegUnitPressureSets(unsigned RegUnit) const {
1575  static const int Empty[] = { -1 };
1576 
1577  if (hasRegUnit(AMDGPU::M0, RegUnit))
1578  return Empty;
1579  return AMDGPURegisterInfo::getRegUnitPressureSets(RegUnit);
1580 }
void materializeFrameBaseRegister(MachineBasicBlock *MBB, unsigned BaseReg, int FrameIdx, int64_t Offset) const override
bool eliminateSGPRToVGPRSpillFrameIndex(MachineBasicBlock::iterator MI, int FI, RegScavenger *RS) const
Special case of eliminateFrameIndex.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
bool requiresRegisterScavenging(const MachineFunction &Fn) const override
BitVector & set()
Definition: BitVector.h:398
unsigned reservedStackPtrOffsetReg(const MachineFunction &MF) const
Interface definition for SIRegisterInfo.
bool spillSGPRToSMEM() const
SIRegisterInfo(const SISubtarget &ST)
ArrayRef< int16_t > getRegSplitParts(const TargetRegisterClass *RC, unsigned EltSize) const
Flags getFlags() const
Return the raw flags of the source value,.
AMDGPU specific subclass of TargetSubtarget.
bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const override
virtual StringRef getRegAsmName(unsigned Reg) const
Return the assembly name for Reg.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
bool isVGPR(const MachineRegisterInfo &MRI, unsigned Reg) const
void ChangeToRegister(unsigned Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isDebug=false)
ChangeToRegister - Replace this operand with a new register operand of the specified value...
unsigned getRegPressureLimit(const TargetRegisterClass *RC, MachineFunction &MF) const override
bool isFrameOffsetLegal(const MachineInstr *MI, unsigned BaseReg, int64_t Offset) const override
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
static int getOffsetMUBUFLoad(unsigned Opc)
StringRef getRegAsmName(unsigned Reg) const override
bool isSGPRPressureSet(unsigned SetID) const
unsigned getMaxNumVGPRs(unsigned WavesPerEU) const
bool mayStore() const
Return true if this instruction could possibly modify memory.
Definition: MCInstrDesc.h:393
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:163
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
DataLayout DL
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
static std::pair< unsigned, unsigned > getSpillEltSize(unsigned SuperRegSize, bool Store)
const SIInstrInfo * getInstrInfo() const override
const TargetRegisterClass * getRegClassForReg(const MachineRegisterInfo &MRI, unsigned Reg) const
unsigned getRegBitWidth(unsigned RCID)
Get the size in bits of a register from the register class RC.
unsigned getScratchWaveOffsetReg() const
unsigned getID() const
Return the register class ID number.
bool requiresVirtualBaseRegisters(const MachineFunction &Fn) const override
static unsigned findPrivateSegmentWaveByteOffsetRegIndex(unsigned RegCount)
A debug info location.
Definition: DebugLoc.h:34
bool isSGPRClass(const TargetRegisterClass *RC) const
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
const MachinePointerInfo & getPointerInfo() const
iterator_range< mop_iterator > operands()
Definition: MachineInstr.h:306
int64_t getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const override
void resolveFrameIndex(MachineInstr &MI, unsigned BaseReg, int64_t Offset) const override
static unsigned getNumSubRegsForSpillOp(unsigned Op)
unsigned getScratchRSrcReg() const
Returns the physical register reserved for use as the resource descriptor for scratch accesses...
const TargetRegisterClass * getSubRegClass(const TargetRegisterClass *RC, unsigned SubIdx) const
bool requiresFrameIndexScavenging(const MachineFunction &MF) const override
LLVM_READONLY int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the largest uint64_t less than or equal to Value and is Skew mod Align.
Definition: MathExtras.h:705
unsigned reservedPrivateSegmentWaveByteOffsetReg(const MachineFunction &MF) const
Return the end register initially reserved for the scratch wave offset in case spilling is needed...
A description of a memory reference used in the backend.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
bool isPhysRegUsed(unsigned PhysReg) const
Return true if the specified register is modified or read in this function.
const HexagonInstrInfo * TII
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const override
unsigned SubReg
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, unsigned OperandName) const
Returns the operand named Op.
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
Reg
All possible values of the reg field in the ModR/M byte.
MachinePointerInfo getWithOffset(int64_t O) const
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, unsigned base_alignment, const AAMDNodes &AAInfo=AAMDNodes(), const MDNode *Ranges=nullptr, SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
static bool hasPressureSet(const int *PSets, unsigned PSetID)
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
bool isKill() const
bool shouldRewriteCopySrc(const TargetRegisterClass *DefRC, unsigned DefSubReg, const TargetRegisterClass *SrcRC, unsigned SrcSubReg) const override
static bool isMUBUF(const MachineInstr &MI)
Definition: SIInstrInfo.h:371
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
bool hasStackObjects() const
Return true if there are any stack objects in this function.
int64_t getMUBUFInstrOffset(const MachineInstr *MI) const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
int64_t getImm() const
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
unsigned getKillRegState(bool B)
bool requiresFrameIndexReplacementScavenging(const MachineFunction &MF) const override
unsigned getVGPRPressureSet() const
const int * getRegUnitPressureSets(unsigned RegUnit) const override
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:279
void ChangeToImmediate(int64_t ImmVal)
ChangeToImmediate - Replace this operand with a new immediate operand of the specified value...
#define REG_RANGE(BeginReg, EndReg, RegTable)
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:139
bool restoreSGPR(MachineBasicBlock::iterator MI, int FI, RegScavenger *RS, bool OnlyToVGPR=false) const
unsigned getDefRegState(bool B)
constexpr uint64_t MinAlign(uint64_t A, uint64_t B)
A and B are either alignments or offsets.
Definition: MathExtras.h:602
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Function Alias Analysis false
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
This file declares the machine register scavenger class.
const TargetRegisterClass * getEquivalentVGPRClass(const TargetRegisterClass *SRC) const
unsigned const MachineRegisterInfo * MRI
bool hasVGPRs(const TargetRegisterClass *RC) const
unsigned getPreloadedValue(const MachineFunction &MF, enum PreloadedValue Value) const
Returns the physical register that Value is stored in.
bool shouldCoalesce(MachineInstr *MI, const TargetRegisterClass *SrcRC, unsigned SubReg, const TargetRegisterClass *DstRC, unsigned DstSubReg, const TargetRegisterClass *NewRC) const override
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
static cl::opt< bool > EnableSpillSGPRToSMEM("amdgpu-spill-sgpr-to-smem", cl::desc("Use scalar stores to spill SGPRs if supported by subtarget"), cl::init(false))
static bool buildMUBUFOffsetLoadStore(const SIInstrInfo *TII, MachineFrameInfo &MFI, MachineBasicBlock::iterator MI, int Index, int64_t Offset)
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:284
const TargetRegisterClass * getEquivalentSGPRClass(const TargetRegisterClass *VRC) const
unsigned getSubRegFromChannel(unsigned Channel) const
MCRegAliasIterator enumerates all registers aliasing Reg.
bool hasScalarStores() const
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
void eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, unsigned FIOperandNum, RegScavenger *RS) const override
unsigned getOccupancyWithLocalMemSize(uint32_t Bytes, const Function &) const
Inverse of getMaxLocalMemWithWaveCount.
BitVector getReservedRegs(const MachineFunction &MF) const override
bool hasInv2PiInlineImm() const
This class contains a discriminated union of information about pointers in memory operands...
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
The memory access writes data.
bool hasCalls() const
Return true if the current function has any function calls.
unsigned countPopulation(T Value)
Count the number of set bits in a value.
Definition: MathExtras.h:512
unsigned getRegPressureSetLimit(const MachineFunction &MF, unsigned Idx) const override
MachineOperand class - Representation of each machine instruction operand.
unsigned getObjectAlignment(int ObjectIdx) const
Return the alignment of the specified stack object.
void setRegAllocationHint(unsigned VReg, unsigned Type, unsigned PrefReg)
setRegAllocationHint - Specify a register allocation hint for the specified virtual register...
bool isRegUsed(unsigned Reg, bool includeReserved=true) const
Return if a specific register is currently used.
ArrayRef< SpilledReg > getSGPRToVGPRSpills(int FrameIndex) const
bool isAllocatable(unsigned PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn't been...
static cl::opt< bool > EnableSpillSGPRToVGPR("amdgpu-spill-sgpr-to-vgpr", cl::desc("Enable spilling VGPRs to SGPRs"), cl::ReallyHidden, cl::init(true))
const MachineInstrBuilder & addFrameIndex(int Idx) const
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:531
const MCRegisterClass * MC
const TargetRegisterClass * getPhysRegClass(unsigned Reg) const
Return the 'base' register class for this register.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
The memory access reads data.
static MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1164
Representation of each machine instruction.
Definition: MachineInstr.h:59
unsigned findUnusedRegister(const MachineRegisterInfo &MRI, const TargetRegisterClass *RC, const MachineFunction &MF) const
Returns a register that is not used at any point in the function.
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
Interface definition for SIInstrInfo.
unsigned FindUnusedReg(const TargetRegisterClass *RegClass) const
Find an unused register of the specified register class.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
Definition: MachineInstr.h:623
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
const TargetRegisterClass * getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const override
unsigned getWavefrontSize() const
static int getOffsetMUBUFStore(unsigned Opc)
unsigned getSGPRPressureSet() const
const unsigned Kind
unsigned getReg() const
getReg - Returns the register number.
bool isVGPRPressureSet(unsigned SetID) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool spillSGPR(MachineBasicBlock::iterator MI, int FI, RegScavenger *RS, bool OnlyToVGPR=false) const
If OnlyToVGPR is true, this will only succeed if this.
bool isInlinableLiteral32(int32_t Literal, bool HasInv2Pi)
LLVM Value Representation.
Definition: Value.h:73
unsigned reservedPrivateSegmentBufferReg(const MachineFunction &MF) const
Return the end register initially reserved for the scratch buffer in case spilling is needed...
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
unsigned getMaxNumSGPRs(unsigned WavesPerEU, bool Addressable) const
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.