LLVM  5.0.0
SIFrameLowering.cpp
Go to the documentation of this file.
1 //===----------------------- SIFrameLowering.cpp --------------------------===//
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 #include "SIFrameLowering.h"
11 #include "AMDGPUSubtarget.h"
12 #include "SIInstrInfo.h"
13 #include "SIMachineFunctionInfo.h"
14 #include "SIRegisterInfo.h"
15 
20 
21 using namespace llvm;
22 
23 
25  const MachineFunction &MF) {
26  return makeArrayRef(AMDGPU::SGPR_128RegClass.begin(),
27  ST.getMaxNumSGPRs(MF) / 4);
28 }
29 
31  const MachineFunction &MF) {
32  return makeArrayRef(AMDGPU::SGPR_32RegClass.begin(),
33  ST.getMaxNumSGPRs(MF));
34 }
35 
36 void SIFrameLowering::emitFlatScratchInit(const SISubtarget &ST,
37  MachineFunction &MF,
38  MachineBasicBlock &MBB) const {
39  const SIInstrInfo *TII = ST.getInstrInfo();
40  const SIRegisterInfo* TRI = &TII->getRegisterInfo();
41 
42  // We don't need this if we only have spills since there is no user facing
43  // scratch.
44 
45  // TODO: If we know we don't have flat instructions earlier, we can omit
46  // this from the input registers.
47  //
48  // TODO: We only need to know if we access scratch space through a flat
49  // pointer. Because we only detect if flat instructions are used at all,
50  // this will be used more often than necessary on VI.
51 
52  // Debug location must be unknown since the first debug location is used to
53  // determine the end of the prologue.
54  DebugLoc DL;
56 
57  unsigned FlatScratchInitReg
59 
61  MRI.addLiveIn(FlatScratchInitReg);
62  MBB.addLiveIn(FlatScratchInitReg);
63 
64  unsigned FlatScrInitLo = TRI->getSubReg(FlatScratchInitReg, AMDGPU::sub0);
65  unsigned FlatScrInitHi = TRI->getSubReg(FlatScratchInitReg, AMDGPU::sub1);
66 
68  unsigned ScratchWaveOffsetReg = MFI->getScratchWaveOffsetReg();
69 
70  // Do a 64-bit pointer add.
71  if (ST.flatScratchIsPointer()) {
72  BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADD_U32), AMDGPU::FLAT_SCR_LO)
73  .addReg(FlatScrInitLo)
74  .addReg(ScratchWaveOffsetReg);
75  BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADDC_U32), AMDGPU::FLAT_SCR_HI)
76  .addReg(FlatScrInitHi)
77  .addImm(0);
78 
79  return;
80  }
81 
82  // Copy the size in bytes.
83  BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), AMDGPU::FLAT_SCR_LO)
84  .addReg(FlatScrInitHi, RegState::Kill);
85 
86  // Add wave offset in bytes to private base offset.
87  // See comment in AMDKernelCodeT.h for enable_sgpr_flat_scratch_init.
88  BuildMI(MBB, I, DL, TII->get(AMDGPU::S_ADD_U32), FlatScrInitLo)
89  .addReg(FlatScrInitLo)
90  .addReg(ScratchWaveOffsetReg);
91 
92  // Convert offset to 256-byte units.
93  BuildMI(MBB, I, DL, TII->get(AMDGPU::S_LSHR_B32), AMDGPU::FLAT_SCR_HI)
94  .addReg(FlatScrInitLo, RegState::Kill)
95  .addImm(8);
96 }
97 
98 unsigned SIFrameLowering::getReservedPrivateSegmentBufferReg(
99  const SISubtarget &ST,
100  const SIInstrInfo *TII,
101  const SIRegisterInfo *TRI,
103  MachineFunction &MF) const {
104  MachineRegisterInfo &MRI = MF.getRegInfo();
105 
106  // We need to insert initialization of the scratch resource descriptor.
107  unsigned ScratchRsrcReg = MFI->getScratchRSrcReg();
108  if (ScratchRsrcReg == AMDGPU::NoRegister ||
109  !MRI.isPhysRegUsed(ScratchRsrcReg))
110  return AMDGPU::NoRegister;
111 
112  if (ST.hasSGPRInitBug() ||
113  ScratchRsrcReg != TRI->reservedPrivateSegmentBufferReg(MF))
114  return ScratchRsrcReg;
115 
116  // We reserved the last registers for this. Shift it down to the end of those
117  // which were actually used.
118  //
119  // FIXME: It might be safer to use a pseudoregister before replacement.
120 
121  // FIXME: We should be able to eliminate unused input registers. We only
122  // cannot do this for the resources required for scratch access. For now we
123  // skip over user SGPRs and may leave unused holes.
124 
125  // We find the resource first because it has an alignment requirement.
126 
127  unsigned NumPreloaded = (MFI->getNumPreloadedSGPRs() + 3) / 4;
128  ArrayRef<MCPhysReg> AllSGPR128s = getAllSGPR128(ST, MF);
129  AllSGPR128s = AllSGPR128s.slice(std::min(static_cast<unsigned>(AllSGPR128s.size()), NumPreloaded));
130 
131  // Skip the last N reserved elements because they should have already been
132  // reserved for VCC etc.
133  for (MCPhysReg Reg : AllSGPR128s) {
134  // Pick the first unallocated one. Make sure we don't clobber the other
135  // reserved input we needed.
136  if (!MRI.isPhysRegUsed(Reg) && MRI.isAllocatable(Reg)) {
137  MRI.replaceRegWith(ScratchRsrcReg, Reg);
138  MFI->setScratchRSrcReg(Reg);
139  return Reg;
140  }
141  }
142 
143  return ScratchRsrcReg;
144 }
145 
146 // Shift down registers reserved for the scratch wave offset and stack pointer
147 // SGPRs.
148 std::pair<unsigned, unsigned>
149 SIFrameLowering::getReservedPrivateSegmentWaveByteOffsetReg(
150  const SISubtarget &ST,
151  const SIInstrInfo *TII,
152  const SIRegisterInfo *TRI,
154  MachineFunction &MF) const {
155  MachineRegisterInfo &MRI = MF.getRegInfo();
156  unsigned ScratchWaveOffsetReg = MFI->getScratchWaveOffsetReg();
157 
158  // No replacement necessary.
159  if (ScratchWaveOffsetReg == AMDGPU::NoRegister ||
160  !MRI.isPhysRegUsed(ScratchWaveOffsetReg)) {
161  assert(MFI->getStackPtrOffsetReg() == AMDGPU::SP_REG);
162  return std::make_pair(AMDGPU::NoRegister, AMDGPU::NoRegister);
163  }
164 
165  unsigned SPReg = MFI->getStackPtrOffsetReg();
166  if (ST.hasSGPRInitBug())
167  return std::make_pair(ScratchWaveOffsetReg, SPReg);
168 
169  unsigned NumPreloaded = MFI->getNumPreloadedSGPRs();
170 
171  ArrayRef<MCPhysReg> AllSGPRs = getAllSGPRs(ST, MF);
172  if (NumPreloaded > AllSGPRs.size())
173  return std::make_pair(ScratchWaveOffsetReg, SPReg);
174 
175  AllSGPRs = AllSGPRs.slice(NumPreloaded);
176 
177  // We need to drop register from the end of the list that we cannot use
178  // for the scratch wave offset.
179  // + 2 s102 and s103 do not exist on VI.
180  // + 2 for vcc
181  // + 2 for xnack_mask
182  // + 2 for flat_scratch
183  // + 4 for registers reserved for scratch resource register
184  // + 1 for register reserved for scratch wave offset. (By exluding this
185  // register from the list to consider, it means that when this
186  // register is being used for the scratch wave offset and there
187  // are no other free SGPRs, then the value will stay in this register.
188  // + 1 if stack pointer is used.
189  // ----
190  // 13 (+1)
191  unsigned ReservedRegCount = 13;
192 
193  if (AllSGPRs.size() < ReservedRegCount)
194  return std::make_pair(ScratchWaveOffsetReg, SPReg);
195 
196  bool HandledScratchWaveOffsetReg =
197  ScratchWaveOffsetReg != TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
198 
199  for (MCPhysReg Reg : AllSGPRs.drop_back(ReservedRegCount)) {
200  // Pick the first unallocated SGPR. Be careful not to pick an alias of the
201  // scratch descriptor, since we haven’t added its uses yet.
202  if (!MRI.isPhysRegUsed(Reg) && MRI.isAllocatable(Reg)) {
203  if (!HandledScratchWaveOffsetReg) {
204  HandledScratchWaveOffsetReg = true;
205 
206  MRI.replaceRegWith(ScratchWaveOffsetReg, Reg);
208  ScratchWaveOffsetReg = Reg;
209  break;
210  }
211  }
212  }
213 
214  return std::make_pair(ScratchWaveOffsetReg, SPReg);
215 }
216 
218  MachineBasicBlock &MBB) const {
219  // Emit debugger prologue if "amdgpu-debugger-emit-prologue" attribute was
220  // specified.
221  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
222  auto AMDGPUASI = ST.getAMDGPUAS();
223  if (ST.debuggerEmitPrologue())
224  emitDebuggerPrologue(MF, MBB);
225 
226  assert(&MF.front() == &MBB && "Shrink-wrapping not yet supported");
227 
229 
230  // If we only have SGPR spills, we won't actually be using scratch memory
231  // since these spill to VGPRs.
232  //
233  // FIXME: We should be cleaning up these unused SGPR spill frame indices
234  // somewhere.
235 
236  const SIInstrInfo *TII = ST.getInstrInfo();
237  const SIRegisterInfo *TRI = &TII->getRegisterInfo();
238  MachineRegisterInfo &MRI = MF.getRegInfo();
239 
240  // We need to do the replacement of the private segment buffer and wave offset
241  // register even if there are no stack objects. There could be stores to undef
242  // or a constant without an associated object.
243 
244  // FIXME: We still have implicit uses on SGPR spill instructions in case they
245  // need to spill to vector memory. It's likely that will not happen, but at
246  // this point it appears we need the setup. This part of the prolog should be
247  // emitted after frame indices are eliminated.
248 
249  if (MFI->hasFlatScratchInit())
250  emitFlatScratchInit(ST, MF, MBB);
251 
252  unsigned SPReg = MFI->getStackPtrOffsetReg();
253  if (SPReg != AMDGPU::SP_REG) {
254  assert(MRI.isReserved(SPReg) && "SPReg used but not reserved");
255 
256  DebugLoc DL;
257  const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
258  int64_t StackSize = FrameInfo.getStackSize();
259 
260  if (StackSize == 0) {
261  BuildMI(MBB, MBB.begin(), DL, TII->get(AMDGPU::COPY), SPReg)
262  .addReg(MFI->getScratchWaveOffsetReg());
263  } else {
264  BuildMI(MBB, MBB.begin(), DL, TII->get(AMDGPU::S_ADD_U32), SPReg)
265  .addReg(MFI->getScratchWaveOffsetReg())
266  .addImm(StackSize * ST.getWavefrontSize());
267  }
268  }
269 
270  unsigned ScratchRsrcReg
271  = getReservedPrivateSegmentBufferReg(ST, TII, TRI, MFI, MF);
272 
273  unsigned ScratchWaveOffsetReg;
274  std::tie(ScratchWaveOffsetReg, SPReg)
275  = getReservedPrivateSegmentWaveByteOffsetReg(ST, TII, TRI, MFI, MF);
276 
277  // It's possible to have uses of only ScratchWaveOffsetReg without
278  // ScratchRsrcReg if it's only used for the initialization of flat_scratch,
279  // but the inverse is not true.
280  if (ScratchWaveOffsetReg == AMDGPU::NoRegister) {
281  assert(ScratchRsrcReg == AMDGPU::NoRegister);
282  return;
283  }
284 
285  // We need to insert initialization of the scratch resource descriptor.
286  unsigned PreloadedScratchWaveOffsetReg = TRI->getPreloadedValue(
288 
289  unsigned PreloadedPrivateBufferReg = AMDGPU::NoRegister;
290  if (ST.isAmdCodeObjectV2(MF)) {
291  PreloadedPrivateBufferReg = TRI->getPreloadedValue(
293  }
294 
295  bool OffsetRegUsed = MRI.isPhysRegUsed(ScratchWaveOffsetReg);
296  bool ResourceRegUsed = ScratchRsrcReg != AMDGPU::NoRegister &&
297  MRI.isPhysRegUsed(ScratchRsrcReg);
298 
299  // We added live-ins during argument lowering, but since they were not used
300  // they were deleted. We're adding the uses now, so add them back.
301  if (OffsetRegUsed) {
302  assert(PreloadedScratchWaveOffsetReg != AMDGPU::NoRegister &&
303  "scratch wave offset input is required");
304  MRI.addLiveIn(PreloadedScratchWaveOffsetReg);
305  MBB.addLiveIn(PreloadedScratchWaveOffsetReg);
306  }
307 
308  if (ResourceRegUsed && PreloadedPrivateBufferReg != AMDGPU::NoRegister) {
309  assert(ST.isAmdCodeObjectV2(MF) || ST.isMesaGfxShader(MF));
310  MRI.addLiveIn(PreloadedPrivateBufferReg);
311  MBB.addLiveIn(PreloadedPrivateBufferReg);
312  }
313 
314  // Make the register selected live throughout the function.
315  for (MachineBasicBlock &OtherBB : MF) {
316  if (&OtherBB == &MBB)
317  continue;
318 
319  if (OffsetRegUsed)
320  OtherBB.addLiveIn(ScratchWaveOffsetReg);
321 
322  if (ResourceRegUsed)
323  OtherBB.addLiveIn(ScratchRsrcReg);
324  }
325 
326  DebugLoc DL;
328 
329  // If we reserved the original input registers, we don't need to copy to the
330  // reserved registers.
331 
332  bool CopyBuffer = ResourceRegUsed &&
333  PreloadedPrivateBufferReg != AMDGPU::NoRegister &&
334  ST.isAmdCodeObjectV2(MF) &&
335  ScratchRsrcReg != PreloadedPrivateBufferReg;
336 
337  // This needs to be careful of the copying order to avoid overwriting one of
338  // the input registers before it's been copied to it's final
339  // destination. Usually the offset should be copied first.
340  bool CopyBufferFirst = TRI->isSubRegisterEq(PreloadedPrivateBufferReg,
341  ScratchWaveOffsetReg);
342  if (CopyBuffer && CopyBufferFirst) {
343  BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), ScratchRsrcReg)
344  .addReg(PreloadedPrivateBufferReg, RegState::Kill);
345  }
346 
347  if (OffsetRegUsed &&
348  PreloadedScratchWaveOffsetReg != ScratchWaveOffsetReg) {
349  BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), ScratchWaveOffsetReg)
350  .addReg(PreloadedScratchWaveOffsetReg,
351  MRI.isPhysRegUsed(ScratchWaveOffsetReg) ? 0 : RegState::Kill);
352  }
353 
354  if (CopyBuffer && !CopyBufferFirst) {
355  BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), ScratchRsrcReg)
356  .addReg(PreloadedPrivateBufferReg, RegState::Kill);
357  }
358 
359  if (ResourceRegUsed && (ST.isMesaGfxShader(MF) || (PreloadedPrivateBufferReg == AMDGPU::NoRegister))) {
360  assert(!ST.isAmdCodeObjectV2(MF));
361  const MCInstrDesc &SMovB32 = TII->get(AMDGPU::S_MOV_B32);
362 
363  unsigned Rsrc2 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub2);
364  unsigned Rsrc3 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub3);
365 
366  // Use relocations to get the pointer, and setup the other bits manually.
367  uint64_t Rsrc23 = TII->getScratchRsrcWords23();
368 
369  if (MFI->hasImplicitBufferPtr()) {
370  unsigned Rsrc01 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0_sub1);
371 
372  if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
373  const MCInstrDesc &Mov64 = TII->get(AMDGPU::S_MOV_B64);
374 
375  BuildMI(MBB, I, DL, Mov64, Rsrc01)
376  .addReg(MFI->getImplicitBufferPtrUserSGPR())
377  .addReg(ScratchRsrcReg, RegState::ImplicitDefine);
378  } else {
379  const MCInstrDesc &LoadDwordX2 = TII->get(AMDGPU::S_LOAD_DWORDX2_IMM);
380 
381  PointerType *PtrTy =
382  PointerType::get(Type::getInt64Ty(MF.getFunction()->getContext()),
383  AMDGPUASI.CONSTANT_ADDRESS);
384  MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
385  auto MMO = MF.getMachineMemOperand(PtrInfo,
389  0, 0);
390  BuildMI(MBB, I, DL, LoadDwordX2, Rsrc01)
391  .addReg(MFI->getImplicitBufferPtrUserSGPR())
392  .addImm(0) // offset
393  .addImm(0) // glc
394  .addMemOperand(MMO)
395  .addReg(ScratchRsrcReg, RegState::ImplicitDefine);
396  }
397  } else {
398  unsigned Rsrc0 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub0);
399  unsigned Rsrc1 = TRI->getSubReg(ScratchRsrcReg, AMDGPU::sub1);
400 
401  BuildMI(MBB, I, DL, SMovB32, Rsrc0)
402  .addExternalSymbol("SCRATCH_RSRC_DWORD0")
403  .addReg(ScratchRsrcReg, RegState::ImplicitDefine);
404 
405  BuildMI(MBB, I, DL, SMovB32, Rsrc1)
406  .addExternalSymbol("SCRATCH_RSRC_DWORD1")
407  .addReg(ScratchRsrcReg, RegState::ImplicitDefine);
408 
409  }
410 
411  BuildMI(MBB, I, DL, SMovB32, Rsrc2)
412  .addImm(Rsrc23 & 0xffffffff)
413  .addReg(ScratchRsrcReg, RegState::ImplicitDefine);
414 
415  BuildMI(MBB, I, DL, SMovB32, Rsrc3)
416  .addImm(Rsrc23 >> 32)
417  .addReg(ScratchRsrcReg, RegState::ImplicitDefine);
418  }
419 }
420 
422  MachineBasicBlock &MBB) const {
423  const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
424  if (FuncInfo->isEntryFunction()) {
425  emitEntryFunctionPrologue(MF, MBB);
426  return;
427  }
428 
429  const MachineFrameInfo &MFI = MF.getFrameInfo();
430  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
431  const SIInstrInfo *TII = ST.getInstrInfo();
432 
433  unsigned StackPtrReg = FuncInfo->getStackPtrOffsetReg();
434  unsigned FramePtrReg = FuncInfo->getFrameOffsetReg();
435 
436  MachineBasicBlock::iterator MBBI = MBB.begin();
437  DebugLoc DL;
438 
439  bool NeedFP = hasFP(MF);
440  if (NeedFP) {
441  // If we need a base pointer, set it up here. It's whatever the value of
442  // the stack pointer is at this point. Any variable size objects will be
443  // allocated after this, so we can still use the base pointer to reference
444  // locals.
445  BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::COPY), FramePtrReg)
446  .addReg(StackPtrReg)
448  }
449 
450  uint32_t NumBytes = MFI.getStackSize();
451  if (NumBytes != 0 && hasSP(MF)) {
452  BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_ADD_U32), StackPtrReg)
453  .addReg(StackPtrReg)
454  .addImm(NumBytes * ST.getWavefrontSize())
455  .setMIFlag(MachineInstr::FrameSetup);
456  }
457 }
458 
460  MachineBasicBlock &MBB) const {
461  const SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
462  if (FuncInfo->isEntryFunction())
463  return;
464 
465  unsigned StackPtrReg = FuncInfo->getStackPtrOffsetReg();
466  if (StackPtrReg == AMDGPU::NoRegister)
467  return;
468 
469  const MachineFrameInfo &MFI = MF.getFrameInfo();
470  uint32_t NumBytes = MFI.getStackSize();
471 
472  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
473  const SIInstrInfo *TII = ST.getInstrInfo();
475  DebugLoc DL;
476 
477  // FIXME: Clarify distinction between no set SP and SP. For callee functions,
478  // it's really whether we need SP to be accurate or not.
479 
480  if (NumBytes != 0 && hasSP(MF)) {
481  BuildMI(MBB, MBBI, DL, TII->get(AMDGPU::S_SUB_U32), StackPtrReg)
482  .addReg(StackPtrReg)
483  .addImm(NumBytes * ST.getWavefrontSize())
484  .setMIFlag(MachineInstr::FrameDestroy);
485  }
486 }
487 
488 static bool allStackObjectsAreDead(const MachineFrameInfo &MFI) {
489  for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd();
490  I != E; ++I) {
491  if (!MFI.isDeadObjectIndex(I))
492  return false;
493  }
494 
495  return true;
496 }
497 
499  unsigned &FrameReg) const {
500  const SIRegisterInfo *RI = MF.getSubtarget<SISubtarget>().getRegisterInfo();
501 
502  FrameReg = RI->getFrameRegister(MF);
503  return MF.getFrameInfo().getObjectOffset(FI);
504 }
505 
507  MachineFunction &MF,
508  RegScavenger *RS) const {
509  MachineFrameInfo &MFI = MF.getFrameInfo();
510 
511  if (!MFI.hasStackObjects())
512  return;
513 
514  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
515  const SIInstrInfo *TII = ST.getInstrInfo();
516  const SIRegisterInfo &TRI = TII->getRegisterInfo();
518  bool AllSGPRSpilledToVGPRs = false;
519 
520  if (TRI.spillSGPRToVGPR() && FuncInfo->hasSpilledSGPRs()) {
521  AllSGPRSpilledToVGPRs = true;
522 
523  // Process all SGPR spills before frame offsets are finalized. Ideally SGPRs
524  // are spilled to VGPRs, in which case we can eliminate the stack usage.
525  //
526  // XXX - This operates under the assumption that only other SGPR spills are
527  // users of the frame index. I'm not 100% sure this is correct. The
528  // StackColoring pass has a comment saying a future improvement would be to
529  // merging of allocas with spill slots, but for now according to
530  // MachineFrameInfo isSpillSlot can't alias any other object.
531  for (MachineBasicBlock &MBB : MF) {
533  for (auto I = MBB.begin(), E = MBB.end(); I != E; I = Next) {
534  MachineInstr &MI = *I;
535  Next = std::next(I);
536 
537  if (TII->isSGPRSpill(MI)) {
538  int FI = TII->getNamedOperand(MI, AMDGPU::OpName::addr)->getIndex();
539  if (FuncInfo->allocateSGPRSpillToVGPR(MF, FI)) {
540  bool Spilled = TRI.eliminateSGPRToVGPRSpillFrameIndex(MI, FI, RS);
541  (void)Spilled;
542  assert(Spilled && "failed to spill SGPR to VGPR when allocated");
543  } else
544  AllSGPRSpilledToVGPRs = false;
545  }
546  }
547  }
548 
549  FuncInfo->removeSGPRToVGPRFrameIndices(MFI);
550  }
551 
552  // FIXME: The other checks should be redundant with allStackObjectsAreDead,
553  // but currently hasNonSpillStackObjects is set only from source
554  // allocas. Stack temps produced from legalization are not counted currently.
555  if (FuncInfo->hasNonSpillStackObjects() || FuncInfo->hasSpilledVGPRs() ||
556  !AllSGPRSpilledToVGPRs || !allStackObjectsAreDead(MFI)) {
557  assert(RS && "RegScavenger required if spilling");
558 
559  // We force this to be at offset 0 so no user object ever has 0 as an
560  // address, so we may use 0 as an invalid pointer value. This is because
561  // LLVM assumes 0 is an invalid pointer in address space 0. Because alloca
562  // is required to be address space 0, we are forced to accept this for
563  // now. Ideally we could have the stack in another address space with 0 as a
564  // valid pointer, and -1 as the null value.
565  //
566  // This will also waste additional space when user stack objects require > 4
567  // byte alignment.
568  //
569  // The main cost here is losing the offset for addressing modes. However
570  // this also ensures we shouldn't need a register for the offset when
571  // emergency scavenging.
572  int ScavengeFI = MFI.CreateFixedObject(
573  TRI.getSpillSize(AMDGPU::SGPR_32RegClass), 0, false);
574  RS->addScavengingFrameIndex(ScavengeFI);
575  }
576 }
577 
578 void SIFrameLowering::emitDebuggerPrologue(MachineFunction &MF,
579  MachineBasicBlock &MBB) const {
580  const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
581  const SIInstrInfo *TII = ST.getInstrInfo();
582  const SIRegisterInfo *TRI = &TII->getRegisterInfo();
584 
586  DebugLoc DL;
587 
588  // For each dimension:
589  for (unsigned i = 0; i < 3; ++i) {
590  // Get work group ID SGPR, and make it live-in again.
591  unsigned WorkGroupIDSGPR = MFI->getWorkGroupIDSGPR(i);
592  MF.getRegInfo().addLiveIn(WorkGroupIDSGPR);
593  MBB.addLiveIn(WorkGroupIDSGPR);
594 
595  // Since SGPRs are spilled into VGPRs, copy work group ID SGPR to VGPR in
596  // order to spill it to scratch.
597  unsigned WorkGroupIDVGPR =
598  MF.getRegInfo().createVirtualRegister(&AMDGPU::VGPR_32RegClass);
599  BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), WorkGroupIDVGPR)
600  .addReg(WorkGroupIDSGPR);
601 
602  // Spill work group ID.
603  int WorkGroupIDObjectIdx = MFI->getDebuggerWorkGroupIDStackObjectIndex(i);
604  TII->storeRegToStackSlot(MBB, I, WorkGroupIDVGPR, false,
605  WorkGroupIDObjectIdx, &AMDGPU::VGPR_32RegClass, TRI);
606 
607  // Get work item ID VGPR, and make it live-in again.
608  unsigned WorkItemIDVGPR = MFI->getWorkItemIDVGPR(i);
609  MF.getRegInfo().addLiveIn(WorkItemIDVGPR);
610  MBB.addLiveIn(WorkItemIDVGPR);
611 
612  // Spill work item ID.
613  int WorkItemIDObjectIdx = MFI->getDebuggerWorkItemIDStackObjectIndex(i);
614  TII->storeRegToStackSlot(MBB, I, WorkItemIDVGPR, false,
615  WorkItemIDObjectIdx, &AMDGPU::VGPR_32RegClass, TRI);
616  }
617 }
618 
620  // All stack operations are relative to the frame offset SGPR.
621  // TODO: Still want to eliminate sometimes.
622  const MachineFrameInfo &MFI = MF.getFrameInfo();
623 
624  // XXX - Is this only called after frame is finalized? Should be able to check
625  // frame size.
626  return MFI.hasStackObjects() && !allStackObjectsAreDead(MFI);
627 }
628 
630  // All stack operations are relative to the frame offset SGPR.
631  const MachineFrameInfo &MFI = MF.getFrameInfo();
632  return MFI.hasCalls() || MFI.hasVarSizedObjects();
633 }
int getFrameIndexReference(const MachineFunction &MF, int FI, unsigned &FrameReg) const override
getFrameIndexReference - This method should return the base register and offset used to reference a f...
bool eliminateSGPRToVGPRSpillFrameIndex(MachineBasicBlock::iterator MI, int FI, RegScavenger *RS) const
Special case of eliminateFrameIndex.
Interface definition for SIRegisterInfo.
int getDebuggerWorkGroupIDStackObjectIndex(unsigned Dim) const
AMDGPU specific subclass of TargetSubtarget.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:234
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
void addLiveIn(unsigned Reg, unsigned vreg=0)
addLiveIn - Add the specified register as a live-in.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:163
void processFunctionBeforeFrameFinalized(MachineFunction &MF, RegScavenger *RS=nullptr) const override
processFunctionBeforeFrameFinalized - This method is called immediately before the specified function...
DataLayout DL
void removeSGPRToVGPRFrameIndices(MachineFrameInfo &MFI)
const SIInstrInfo * getInstrInfo() const override
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space...
Definition: Type.cpp:617
unsigned getScratchWaveOffsetReg() const
A debug info location.
Definition: DebugLoc.h:34
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:177
bool isAmdCodeObjectV2(const MachineFunction &MF) const
unsigned getScratchRSrcReg() const
Returns the physical register reserved for use as the resource descriptor for scratch accesses...
bool spillSGPRToVGPR() const
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects...
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:179
unsigned reservedPrivateSegmentWaveByteOffsetReg(const MachineFunction &MF) const
Return the end register initially reserved for the scratch wave offset in case spilling is needed...
void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override
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 flatScratchIsPointer() const
static bool allStackObjectsAreDead(const MachineFrameInfo &MFI)
Reg
All possible values of the reg field in the ModR/M byte.
The memory access is dereferenceable (i.e., doesn't trap).
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
bool isMesaGfxShader(const MachineFunction &MF) const
unsigned getWorkItemIDVGPR(unsigned Dim) const
const MachineBasicBlock & front() const
int getObjectIndexBegin() const
Return the minimum frame object index.
bool hasStackObjects() const
Return true if there are any stack objects in this function.
int getDebuggerWorkItemIDStackObjectIndex(unsigned Dim) const
unsigned getWorkGroupIDSGPR(unsigned Dim) const
static ArrayRef< MCPhysReg > getAllSGPR128(const SISubtarget &ST, const MachineFunction &MF)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
const SIRegisterInfo & getRegisterInfo() const
Definition: SIInstrInfo.h:125
bool allocateSGPRSpillToVGPR(MachineFunction &MF, int FI)
Reserve a slice of a VGPR to support spilling for FrameIndex FI.
Class to represent pointers.
Definition: DerivedTypes.h:467
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
bool hasSGPRInitBug() const
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool isCompute(CallingConv::ID cc)
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
This file declares the machine register scavenger class.
unsigned const MachineRegisterInfo * MRI
unsigned getFrameRegister(const MachineFunction &MF) const override
unsigned getPreloadedValue(const MachineFunction &MF, enum PreloadedValue Value) const
Returns the physical register that Value is stored in.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
ArrayRef< T > drop_back(size_t N=1) const
Drop the last N elements of the array.
Definition: ArrayRef.h:194
bool isReserved(unsigned PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
int CreateFixedObject(uint64_t Size, int64_t SPOffset, bool Immutable, bool isAliased=false)
Create a new object at a fixed location on the stack.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1320
void setScratchWaveOffsetReg(unsigned Reg)
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.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
bool hasCalls() const
Return true if the current function has any function calls.
#define E
Definition: LargeTest.cpp:27
static ArrayRef< MCPhysReg > getAllSGPRs(const SISubtarget &ST, const MachineFunction &MF)
void addScavengingFrameIndex(int FI)
Add a scavenging frame index.
AMDGPUAS getAMDGPUAS() const
bool isAllocatable(unsigned PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn't been...
void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override
emitProlog/emitEpilog - These methods insert prolog and epilog code into the function.
void replaceRegWith(unsigned FromReg, unsigned ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
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 MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1164
Representation of each machine instruction.
Definition: MachineInstr.h:59
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
Interface definition for SIInstrInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const MachineInstrBuilder & addExternalSymbol(const char *FnName, unsigned char TargetFlags=0) const
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
#define I(x, y, z)
Definition: MD5.cpp:58
void emitEntryFunctionPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const
The memory access always returns the same value (or traps).
unsigned getWavefrontSize() const
bool hasVarSizedObjects() const
This method may be called any time after instruction selection is complete to determine if the stack ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned reservedPrivateSegmentBufferReg(const MachineFunction &MF) const
Return the end register initially reserved for the scratch buffer in case spilling is needed...
unsigned getMaxNumSGPRs(unsigned WavesPerEU, bool Addressable) const
IRTranslator LLVM IR MI
bool hasSP(const MachineFunction &MF) const
const MachineInstrBuilder & setMIFlag(MachineInstr::MIFlag Flag) const
int getObjectIndexEnd() const
Return one past the maximum frame object index.
const MachineInstrBuilder & addReg(unsigned RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
bool debuggerEmitPrologue() const
bool hasFP(const MachineFunction &MF) const override
hasFP - Return true if the specified function should have a dedicated frame pointer register...