1 //===-- SILowerSGPRSPills.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Handle SGPR spills. This pass takes the place of PrologEpilogInserter for all
10 // SGPR spills, so must insert CSR SGPR spills as well as expand them.
11 //
12 // This pass must never create new SGPR virtual registers.
13 //
14 // FIXME: Must stop RegScavenger spills in later passes.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 #include "AMDGPU.h"
19 #include "GCNSubtarget.h"
20 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
21 #include "SIMachineFunctionInfo.h"
22 #include "llvm/CodeGen/LiveIntervals.h"
23 #include "llvm/CodeGen/RegisterScavenging.h"
24 #include "llvm/InitializePasses.h"
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "si-lower-sgpr-spills"
29 
30 using MBBVector = SmallVector<MachineBasicBlock *, 4>;
31 
32 namespace {
33 
34 class SILowerSGPRSpills : public MachineFunctionPass {
35 private:
36   const SIRegisterInfo *TRI = nullptr;
37   const SIInstrInfo *TII = nullptr;
38   LiveIntervals *LIS = nullptr;
39 
40   // Save and Restore blocks of the current function. Typically there is a
41   // single save block, unless Windows EH funclets are involved.
42   MBBVector SaveBlocks;
43   MBBVector RestoreBlocks;
44 
45 public:
46   static char ID;
47 
48   SILowerSGPRSpills() : MachineFunctionPass(ID) {}
49 
50   void calculateSaveRestoreBlocks(MachineFunction &MF);
51   bool spillCalleeSavedRegs(MachineFunction &MF);
52 
53   bool runOnMachineFunction(MachineFunction &MF) override;
54 
55   void getAnalysisUsage(AnalysisUsage &AU) const override {
56     AU.setPreservesAll();
57     MachineFunctionPass::getAnalysisUsage(AU);
58   }
59 };
60 
61 } // end anonymous namespace
62 
63 char SILowerSGPRSpills::ID = 0;
64 
65 INITIALIZE_PASS_BEGIN(SILowerSGPRSpills, DEBUG_TYPE,
66                       "SI lower SGPR spill instructions", false, false)
67 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
68 INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
69 INITIALIZE_PASS_END(SILowerSGPRSpills, DEBUG_TYPE,
70                     "SI lower SGPR spill instructions", false, false)
71 
72 char &llvm::SILowerSGPRSpillsID = SILowerSGPRSpills::ID;
73 
74 /// Insert restore code for the callee-saved registers used in the function.
75 static void insertCSRSaves(MachineBasicBlock &SaveBlock,
76                            ArrayRef<CalleeSavedInfo> CSI,
77                            LiveIntervals *LIS) {
78   MachineFunction &MF = *SaveBlock.getParent();
79   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
80   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
81   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
82 
83   MachineBasicBlock::iterator I = SaveBlock.begin();
84   if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) {
85     const MachineRegisterInfo &MRI = MF.getRegInfo();
86 
87     for (const CalleeSavedInfo &CS : CSI) {
88       // Insert the spill to the stack frame.
89       MCRegister Reg = CS.getReg();
90 
91       MachineInstrSpan MIS(I, &SaveBlock);
92       const TargetRegisterClass *RC =
93         TRI->getMinimalPhysRegClass(Reg, MVT::i32);
94 
95       // If this value was already livein, we probably have a direct use of the
96       // incoming register value, so don't kill at the spill point. This happens
97       // since we pass some special inputs (workgroup IDs) in the callee saved
98       // range.
99       const bool IsLiveIn = MRI.isLiveIn(Reg);
100       TII.storeRegToStackSlot(SaveBlock, I, Reg, !IsLiveIn, CS.getFrameIdx(),
101                               RC, TRI);
102 
103       if (LIS) {
104         assert(std::distance(MIS.begin(), I) == 1);
105         MachineInstr &Inst = *std::prev(I);
106 
107         LIS->InsertMachineInstrInMaps(Inst);
108         LIS->removeAllRegUnitsForPhysReg(Reg);
109       }
110     }
111   }
112 }
113 
114 /// Insert restore code for the callee-saved registers used in the function.
115 static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
116                               MutableArrayRef<CalleeSavedInfo> CSI,
117                               LiveIntervals *LIS) {
118   MachineFunction &MF = *RestoreBlock.getParent();
119   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
120   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
121   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
122 
123   // Restore all registers immediately before the return and any
124   // terminators that precede it.
125   MachineBasicBlock::iterator I = RestoreBlock.getFirstTerminator();
126 
127   // FIXME: Just emit the readlane/writelane directly
128   if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) {
129     for (const CalleeSavedInfo &CI : reverse(CSI)) {
130       unsigned Reg = CI.getReg();
131       const TargetRegisterClass *RC =
132         TRI->getMinimalPhysRegClass(Reg, MVT::i32);
133 
134       TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI);
135       assert(I != RestoreBlock.begin() &&
136              "loadRegFromStackSlot didn't insert any code!");
137       // Insert in reverse order.  loadRegFromStackSlot can insert
138       // multiple instructions.
139 
140       if (LIS) {
141         MachineInstr &Inst = *std::prev(I);
142         LIS->InsertMachineInstrInMaps(Inst);
143         LIS->removeAllRegUnitsForPhysReg(Reg);
144       }
145     }
146   }
147 }
148 
149 /// Compute the sets of entry and return blocks for saving and restoring
150 /// callee-saved registers, and placing prolog and epilog code.
151 void SILowerSGPRSpills::calculateSaveRestoreBlocks(MachineFunction &MF) {
152   const MachineFrameInfo &MFI = MF.getFrameInfo();
153 
154   // Even when we do not change any CSR, we still want to insert the
155   // prologue and epilogue of the function.
156   // So set the save points for those.
157 
158   // Use the points found by shrink-wrapping, if any.
159   if (MFI.getSavePoint()) {
160     SaveBlocks.push_back(MFI.getSavePoint());
161     assert(MFI.getRestorePoint() && "Both restore and save must be set");
162     MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
163     // If RestoreBlock does not have any successor and is not a return block
164     // then the end point is unreachable and we do not need to insert any
165     // epilogue.
166     if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
167       RestoreBlocks.push_back(RestoreBlock);
168     return;
169   }
170 
171   // Save refs to entry and return blocks.
172   SaveBlocks.push_back(&MF.front());
173   for (MachineBasicBlock &MBB : MF) {
174     if (MBB.isEHFuncletEntry())
175       SaveBlocks.push_back(&MBB);
176     if (MBB.isReturnBlock())
177       RestoreBlocks.push_back(&MBB);
178   }
179 }
180 
181 // TODO: To support shrink wrapping, this would need to copy
182 // PrologEpilogInserter's updateLiveness.
183 static void updateLiveness(MachineFunction &MF, ArrayRef<CalleeSavedInfo> CSI) {
184   MachineBasicBlock &EntryBB = MF.front();
185 
186   for (const CalleeSavedInfo &CSIReg : CSI)
187     EntryBB.addLiveIn(CSIReg.getReg());
188   EntryBB.sortUniqueLiveIns();
189 }
190 
191 bool SILowerSGPRSpills::spillCalleeSavedRegs(MachineFunction &MF) {
192   MachineRegisterInfo &MRI = MF.getRegInfo();
193   const Function &F = MF.getFunction();
194   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
195   const SIFrameLowering *TFI = ST.getFrameLowering();
196   MachineFrameInfo &MFI = MF.getFrameInfo();
197   RegScavenger *RS = nullptr;
198 
199   // Determine which of the registers in the callee save list should be saved.
200   BitVector SavedRegs;
201   TFI->determineCalleeSavesSGPR(MF, SavedRegs, RS);
202 
203   // Add the code to save and restore the callee saved registers.
204   if (!F.hasFnAttribute(Attribute::Naked)) {
205     // FIXME: This is a lie. The CalleeSavedInfo is incomplete, but this is
206     // necessary for verifier liveness checks.
207     MFI.setCalleeSavedInfoValid(true);
208 
209     std::vector<CalleeSavedInfo> CSI;
210     const MCPhysReg *CSRegs = MRI.getCalleeSavedRegs();
211 
212     for (unsigned I = 0; CSRegs[I]; ++I) {
213       MCRegister Reg = CSRegs[I];
214 
215       if (SavedRegs.test(Reg)) {
216         const TargetRegisterClass *RC =
217           TRI->getMinimalPhysRegClass(Reg, MVT::i32);
218         int JunkFI = MFI.CreateStackObject(TRI->getSpillSize(*RC),
219                                            TRI->getSpillAlign(*RC), true);
220 
221         CSI.push_back(CalleeSavedInfo(Reg, JunkFI));
222       }
223     }
224 
225     if (!CSI.empty()) {
226       for (MachineBasicBlock *SaveBlock : SaveBlocks)
227         insertCSRSaves(*SaveBlock, CSI, LIS);
228 
229       // Add live ins to save blocks.
230       assert(SaveBlocks.size() == 1 && "shrink wrapping not fully implemented");
231       updateLiveness(MF, CSI);
232 
233       for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
234         insertCSRRestores(*RestoreBlock, CSI, LIS);
235       return true;
236     }
237   }
238 
239   return false;
240 }
241 
242 // Find lowest available VGPR and use it as VGPR reserved for SGPR spills.
243 static bool lowerShiftReservedVGPR(MachineFunction &MF,
244                                    const GCNSubtarget &ST) {
245   SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
246   const Register PreReservedVGPR = FuncInfo->VGPRReservedForSGPRSpill;
247   // Early out if pre-reservation of a VGPR for SGPR spilling is disabled.
248   if (!PreReservedVGPR)
249     return false;
250 
251   // If there are no free lower VGPRs available, default to using the
252   // pre-reserved register instead.
253   const SIRegisterInfo *TRI = ST.getRegisterInfo();
254   Register LowestAvailableVGPR =
255       TRI->findUnusedRegister(MF.getRegInfo(), &AMDGPU::VGPR_32RegClass, MF);
256   if (!LowestAvailableVGPR)
257     LowestAvailableVGPR = PreReservedVGPR;
258 
259   MachineFrameInfo &FrameInfo = MF.getFrameInfo();
260   // Create a stack object for a possible spill in the function prologue.
261   // Note Non-CSR VGPR also need this as we may overwrite inactive lanes.
262   Optional<int> FI = FrameInfo.CreateSpillStackObject(4, Align(4));
263 
264   // Find saved info about the pre-reserved register.
265   const auto *ReservedVGPRInfoItr =
266       llvm::find_if(FuncInfo->getSGPRSpillVGPRs(),
267                     [PreReservedVGPR](const auto &SpillRegInfo) {
268                       return SpillRegInfo.VGPR == PreReservedVGPR;
269                     });
270 
271   assert(ReservedVGPRInfoItr != FuncInfo->getSGPRSpillVGPRs().end());
272   auto Index =
273       std::distance(FuncInfo->getSGPRSpillVGPRs().begin(), ReservedVGPRInfoItr);
274 
275   FuncInfo->setSGPRSpillVGPRs(LowestAvailableVGPR, FI, Index);
276 
277   for (MachineBasicBlock &MBB : MF) {
278     assert(LowestAvailableVGPR.isValid() && "Did not find an available VGPR");
279     MBB.addLiveIn(LowestAvailableVGPR);
280     MBB.sortUniqueLiveIns();
281   }
282 
283   return true;
284 }
285 
286 bool SILowerSGPRSpills::runOnMachineFunction(MachineFunction &MF) {
287   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
288   TII = ST.getInstrInfo();
289   TRI = &TII->getRegisterInfo();
290 
291   LIS = getAnalysisIfAvailable<LiveIntervals>();
292 
293   assert(SaveBlocks.empty() && RestoreBlocks.empty());
294 
295   // First, expose any CSR SGPR spills. This is mostly the same as what PEI
296   // does, but somewhat simpler.
297   calculateSaveRestoreBlocks(MF);
298   bool HasCSRs = spillCalleeSavedRegs(MF);
299 
300   MachineFrameInfo &MFI = MF.getFrameInfo();
301   MachineRegisterInfo &MRI = MF.getRegInfo();
302   SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
303 
304   if (!MFI.hasStackObjects() && !HasCSRs) {
305     SaveBlocks.clear();
306     RestoreBlocks.clear();
307     if (FuncInfo->VGPRReservedForSGPRSpill) {
308       // Free the reserved VGPR for later possible use by frame lowering.
309       FuncInfo->removeVGPRForSGPRSpill(FuncInfo->VGPRReservedForSGPRSpill, MF);
310       MRI.freezeReservedRegs(MF);
311     }
312     return false;
313   }
314 
315   bool MadeChange = false;
316   bool NewReservedRegs = false;
317 
318   // TODO: CSR VGPRs will never be spilled to AGPRs. These can probably be
319   // handled as SpilledToReg in regular PrologEpilogInserter.
320   const bool HasSGPRSpillToVGPR = TRI->spillSGPRToVGPR() &&
321                                   (HasCSRs || FuncInfo->hasSpilledSGPRs());
322   if (HasSGPRSpillToVGPR) {
323     // Process all SGPR spills before frame offsets are finalized. Ideally SGPRs
324     // are spilled to VGPRs, in which case we can eliminate the stack usage.
325     //
326     // This operates under the assumption that only other SGPR spills are users
327     // of the frame index.
328 
329     lowerShiftReservedVGPR(MF, ST);
330 
331     // To track the spill frame indices handled in this pass.
332     BitVector SpillFIs(MFI.getObjectIndexEnd(), false);
333 
334     for (MachineBasicBlock &MBB : MF) {
335       for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
336         if (!TII->isSGPRSpill(MI))
337           continue;
338 
339         int FI = TII->getNamedOperand(MI, AMDGPU::OpName::addr)->getIndex();
340         assert(MFI.getStackID(FI) == TargetStackID::SGPRSpill);
341         if (FuncInfo->allocateSGPRSpillToVGPR(MF, FI)) {
342           NewReservedRegs = true;
343           bool Spilled = TRI->eliminateSGPRToVGPRSpillFrameIndex(MI, FI,
344                                                                  nullptr, LIS);
345           (void)Spilled;
346           assert(Spilled && "failed to spill SGPR to VGPR when allocated");
347           SpillFIs.set(FI);
348         }
349       }
350     }
351 
352     // FIXME: Adding to live-ins redundant with reserving registers.
353     for (MachineBasicBlock &MBB : MF) {
354       for (auto SSpill : FuncInfo->getSGPRSpillVGPRs())
355         MBB.addLiveIn(SSpill.VGPR);
356       MBB.sortUniqueLiveIns();
357 
358       // FIXME: The dead frame indices are replaced with a null register from
359       // the debug value instructions. We should instead, update it with the
360       // correct register value. But not sure the register value alone is
361       // adequate to lower the DIExpression. It should be worked out later.
362       for (MachineInstr &MI : MBB) {
363         if (MI.isDebugValue() && MI.getOperand(0).isFI() &&
364             SpillFIs[MI.getOperand(0).getIndex()]) {
365           MI.getOperand(0).ChangeToRegister(Register(), false /*isDef*/);
366         }
367       }
368     }
369 
370     // All those frame indices which are dead by now should be removed from the
371     // function frame. Otherwise, there is a side effect such as re-mapping of
372     // free frame index ids by the later pass(es) like "stack slot coloring"
373     // which in turn could mess-up with the book keeping of "frame index to VGPR
374     // lane".
375     FuncInfo->removeDeadFrameIndices(MFI);
376 
377     MadeChange = true;
378   } else if (FuncInfo->VGPRReservedForSGPRSpill) {
379     FuncInfo->removeVGPRForSGPRSpill(FuncInfo->VGPRReservedForSGPRSpill, MF);
380   }
381 
382   SaveBlocks.clear();
383   RestoreBlocks.clear();
384 
385   // Updated the reserved registers with any VGPRs added for SGPR spills.
386   if (NewReservedRegs)
387     MRI.freezeReservedRegs(MF);
388 
389   return MadeChange;
390 }
391