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 "AMDGPUSubtarget.h"
20 #include "SIInstrInfo.h"
21 #include "SIMachineFunctionInfo.h"
22 #include "llvm/CodeGen/LiveIntervals.h"
23 #include "llvm/CodeGen/MachineBasicBlock.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineInstr.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineOperand.h"
29 #include "llvm/CodeGen/VirtRegMap.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/Target/TargetMachine.h"
32 
33 using namespace llvm;
34 
35 #define DEBUG_TYPE "si-lower-sgpr-spills"
36 
37 using MBBVector = SmallVector<MachineBasicBlock *, 4>;
38 
39 namespace {
40 
41 static cl::opt<bool> EnableSpillVGPRToAGPR(
42   "amdgpu-spill-vgpr-to-agpr",
43   cl::desc("Enable spilling VGPRs to AGPRs"),
44   cl::ReallyHidden,
45   cl::init(true));
46 
47 class SILowerSGPRSpills : public MachineFunctionPass {
48 private:
49   const SIRegisterInfo *TRI = nullptr;
50   const SIInstrInfo *TII = nullptr;
51   VirtRegMap *VRM = nullptr;
52   LiveIntervals *LIS = nullptr;
53 
54   // Save and Restore blocks of the current function. Typically there is a
55   // single save block, unless Windows EH funclets are involved.
56   MBBVector SaveBlocks;
57   MBBVector RestoreBlocks;
58 
59 public:
60   static char ID;
61 
62   SILowerSGPRSpills() : MachineFunctionPass(ID) {}
63 
64   void calculateSaveRestoreBlocks(MachineFunction &MF);
65   bool spillCalleeSavedRegs(MachineFunction &MF);
66 
67   bool runOnMachineFunction(MachineFunction &MF) override;
68 
69   void getAnalysisUsage(AnalysisUsage &AU) const override {
70     AU.setPreservesAll();
71     MachineFunctionPass::getAnalysisUsage(AU);
72   }
73 };
74 
75 } // end anonymous namespace
76 
77 char SILowerSGPRSpills::ID = 0;
78 
79 INITIALIZE_PASS_BEGIN(SILowerSGPRSpills, DEBUG_TYPE,
80                       "SI lower SGPR spill instructions", false, false)
81 INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
82 INITIALIZE_PASS_END(SILowerSGPRSpills, DEBUG_TYPE,
83                     "SI lower SGPR spill instructions", false, false)
84 
85 char &llvm::SILowerSGPRSpillsID = SILowerSGPRSpills::ID;
86 
87 /// Insert restore code for the callee-saved registers used in the function.
88 static void insertCSRSaves(MachineBasicBlock &SaveBlock,
89                            ArrayRef<CalleeSavedInfo> CSI,
90                            LiveIntervals *LIS) {
91   MachineFunction &MF = *SaveBlock.getParent();
92   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
93   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
94   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
95 
96   MachineBasicBlock::iterator I = SaveBlock.begin();
97   if (!TFI->spillCalleeSavedRegisters(SaveBlock, I, CSI, TRI)) {
98     for (const CalleeSavedInfo &CS : CSI) {
99       // Insert the spill to the stack frame.
100       unsigned Reg = CS.getReg();
101 
102       MachineInstrSpan MIS(I, &SaveBlock);
103       const TargetRegisterClass *RC =
104         TRI->getMinimalPhysRegClass(Reg, MVT::i32);
105 
106       TII.storeRegToStackSlot(SaveBlock, I, Reg, true, CS.getFrameIdx(), RC,
107                               TRI);
108 
109       if (LIS) {
110         assert(std::distance(MIS.begin(), I) == 1);
111         MachineInstr &Inst = *std::prev(I);
112 
113         LIS->InsertMachineInstrInMaps(Inst);
114         LIS->removeAllRegUnitsForPhysReg(Reg);
115       }
116     }
117   }
118 }
119 
120 /// Insert restore code for the callee-saved registers used in the function.
121 static void insertCSRRestores(MachineBasicBlock &RestoreBlock,
122                               MutableArrayRef<CalleeSavedInfo> CSI,
123                               LiveIntervals *LIS) {
124   MachineFunction &MF = *RestoreBlock.getParent();
125   const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
126   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
127   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
128 
129   // Restore all registers immediately before the return and any
130   // terminators that precede it.
131   MachineBasicBlock::iterator I = RestoreBlock.getFirstTerminator();
132 
133   // FIXME: Just emit the readlane/writelane directly
134   if (!TFI->restoreCalleeSavedRegisters(RestoreBlock, I, CSI, TRI)) {
135     for (const CalleeSavedInfo &CI : reverse(CSI)) {
136       unsigned Reg = CI.getReg();
137       const TargetRegisterClass *RC =
138         TRI->getMinimalPhysRegClass(Reg, MVT::i32);
139 
140       TII.loadRegFromStackSlot(RestoreBlock, I, Reg, CI.getFrameIdx(), RC, TRI);
141       assert(I != RestoreBlock.begin() &&
142              "loadRegFromStackSlot didn't insert any code!");
143       // Insert in reverse order.  loadRegFromStackSlot can insert
144       // multiple instructions.
145 
146       if (LIS) {
147         MachineInstr &Inst = *std::prev(I);
148         LIS->InsertMachineInstrInMaps(Inst);
149         LIS->removeAllRegUnitsForPhysReg(Reg);
150       }
151     }
152   }
153 }
154 
155 /// Compute the sets of entry and return blocks for saving and restoring
156 /// callee-saved registers, and placing prolog and epilog code.
157 void SILowerSGPRSpills::calculateSaveRestoreBlocks(MachineFunction &MF) {
158   const MachineFrameInfo &MFI = MF.getFrameInfo();
159 
160   // Even when we do not change any CSR, we still want to insert the
161   // prologue and epilogue of the function.
162   // So set the save points for those.
163 
164   // Use the points found by shrink-wrapping, if any.
165   if (MFI.getSavePoint()) {
166     SaveBlocks.push_back(MFI.getSavePoint());
167     assert(MFI.getRestorePoint() && "Both restore and save must be set");
168     MachineBasicBlock *RestoreBlock = MFI.getRestorePoint();
169     // If RestoreBlock does not have any successor and is not a return block
170     // then the end point is unreachable and we do not need to insert any
171     // epilogue.
172     if (!RestoreBlock->succ_empty() || RestoreBlock->isReturnBlock())
173       RestoreBlocks.push_back(RestoreBlock);
174     return;
175   }
176 
177   // Save refs to entry and return blocks.
178   SaveBlocks.push_back(&MF.front());
179   for (MachineBasicBlock &MBB : MF) {
180     if (MBB.isEHFuncletEntry())
181       SaveBlocks.push_back(&MBB);
182     if (MBB.isReturnBlock())
183       RestoreBlocks.push_back(&MBB);
184   }
185 }
186 
187 bool SILowerSGPRSpills::spillCalleeSavedRegs(MachineFunction &MF) {
188   MachineRegisterInfo &MRI = MF.getRegInfo();
189   const Function &F = MF.getFunction();
190   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
191   const SIFrameLowering *TFI = ST.getFrameLowering();
192   MachineFrameInfo &MFI = MF.getFrameInfo();
193   RegScavenger *RS = nullptr;
194 
195   // Determine which of the registers in the callee save list should be saved.
196   BitVector SavedRegs;
197   TFI->determineCalleeSavesSGPR(MF, SavedRegs, RS);
198 
199   // Add the code to save and restore the callee saved registers.
200   if (!F.hasFnAttribute(Attribute::Naked)) {
201     // FIXME: This is a lie. The CalleeSavedInfo is incomplete, but this is
202     // necessary for verifier liveness checks.
203     MFI.setCalleeSavedInfoValid(true);
204 
205     std::vector<CalleeSavedInfo> CSI;
206     const MCPhysReg *CSRegs = MRI.getCalleeSavedRegs();
207 
208     for (unsigned I = 0; CSRegs[I]; ++I) {
209       unsigned Reg = CSRegs[I];
210       if (SavedRegs.test(Reg)) {
211         const TargetRegisterClass *RC =
212           TRI->getMinimalPhysRegClass(Reg, MVT::i32);
213         int JunkFI = MFI.CreateStackObject(TRI->getSpillSize(*RC),
214                                            TRI->getSpillAlign(*RC), true);
215 
216         CSI.push_back(CalleeSavedInfo(Reg, JunkFI));
217       }
218     }
219 
220     if (!CSI.empty()) {
221       for (MachineBasicBlock *SaveBlock : SaveBlocks)
222         insertCSRSaves(*SaveBlock, CSI, LIS);
223 
224       for (MachineBasicBlock *RestoreBlock : RestoreBlocks)
225         insertCSRRestores(*RestoreBlock, CSI, LIS);
226       return true;
227     }
228   }
229 
230   return false;
231 }
232 
233 // Find lowest available VGPR and use it as VGPR reserved for SGPR spills.
234 static bool lowerShiftReservedVGPR(MachineFunction &MF,
235                                    const GCNSubtarget &ST) {
236   SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
237   const Register PreReservedVGPR = FuncInfo->VGPRReservedForSGPRSpill;
238   // Early out if pre-reservation of a VGPR for SGPR spilling is disabled.
239   if (!PreReservedVGPR)
240     return false;
241 
242   // If there are no free lower VGPRs available, default to using the
243   // pre-reserved register instead.
244   Register LowestAvailableVGPR = PreReservedVGPR;
245 
246   MachineRegisterInfo &MRI = MF.getRegInfo();
247   MachineFrameInfo &FrameInfo = MF.getFrameInfo();
248   ArrayRef<MCPhysReg> AllVGPR32s = ST.getRegisterInfo()->getAllVGPR32(MF);
249   for (MCPhysReg Reg : AllVGPR32s) {
250     if (MRI.isAllocatable(Reg) && !MRI.isPhysRegUsed(Reg)) {
251       LowestAvailableVGPR = Reg;
252       break;
253     }
254   }
255 
256   const MCPhysReg *CSRegs = MF.getRegInfo().getCalleeSavedRegs();
257   Optional<int> FI;
258   // Check if we are reserving a CSR. Create a stack object for a possible spill
259   // in the function prologue.
260   if (FuncInfo->isCalleeSavedReg(CSRegs, LowestAvailableVGPR))
261     FI = FrameInfo.CreateSpillStackObject(4, Align(4));
262 
263   // Find saved info about the pre-reserved register.
264   const auto *ReservedVGPRInfoItr =
265       std::find_if(FuncInfo->getSGPRSpillVGPRs().begin(),
266                    FuncInfo->getSGPRSpillVGPRs().end(),
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     MBB.addLiveIn(LowestAvailableVGPR);
279     MBB.sortUniqueLiveIns();
280   }
281 
282   return true;
283 }
284 
285 bool SILowerSGPRSpills::runOnMachineFunction(MachineFunction &MF) {
286   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
287   TII = ST.getInstrInfo();
288   TRI = &TII->getRegisterInfo();
289 
290   VRM = getAnalysisIfAvailable<VirtRegMap>();
291 
292   assert(SaveBlocks.empty() && RestoreBlocks.empty());
293 
294   // First, expose any CSR SGPR spills. This is mostly the same as what PEI
295   // does, but somewhat simpler.
296   calculateSaveRestoreBlocks(MF);
297   bool HasCSRs = spillCalleeSavedRegs(MF);
298 
299   MachineFrameInfo &MFI = MF.getFrameInfo();
300   if (!MFI.hasStackObjects() && !HasCSRs) {
301     SaveBlocks.clear();
302     RestoreBlocks.clear();
303     return false;
304   }
305 
306   MachineRegisterInfo &MRI = MF.getRegInfo();
307   SIMachineFunctionInfo *FuncInfo = MF.getInfo<SIMachineFunctionInfo>();
308   const bool SpillVGPRToAGPR = ST.hasMAIInsts() && FuncInfo->hasSpilledVGPRs()
309     && EnableSpillVGPRToAGPR;
310 
311   bool MadeChange = false;
312 
313   const bool SpillToAGPR = EnableSpillVGPRToAGPR && ST.hasMAIInsts();
314 
315   // TODO: CSR VGPRs will never be spilled to AGPRs. These can probably be
316   // handled as SpilledToReg in regular PrologEpilogInserter.
317   if ((TRI->spillSGPRToVGPR() && (HasCSRs || FuncInfo->hasSpilledSGPRs())) ||
318       SpillVGPRToAGPR) {
319     // Process all SGPR spills before frame offsets are finalized. Ideally SGPRs
320     // are spilled to VGPRs, in which case we can eliminate the stack usage.
321     //
322     // This operates under the assumption that only other SGPR spills are users
323     // of the frame index.
324 
325     lowerShiftReservedVGPR(MF, ST);
326 
327     for (MachineBasicBlock &MBB : MF) {
328       MachineBasicBlock::iterator Next;
329       for (auto I = MBB.begin(), E = MBB.end(); I != E; I = Next) {
330         MachineInstr &MI = *I;
331         Next = std::next(I);
332 
333         if (SpillToAGPR && TII->isVGPRSpill(MI)) {
334           // Try to eliminate stack used by VGPR spills before frame
335           // finalization.
336           unsigned FIOp = AMDGPU::getNamedOperandIdx(MI.getOpcode(),
337                                                      AMDGPU::OpName::vaddr);
338           int FI = MI.getOperand(FIOp).getIndex();
339           Register VReg =
340               TII->getNamedOperand(MI, AMDGPU::OpName::vdata)->getReg();
341           if (FuncInfo->allocateVGPRSpillToAGPR(MF, FI,
342                                                 TRI->isAGPR(MRI, VReg))) {
343             TRI->eliminateFrameIndex(MI, 0, FIOp, nullptr);
344             continue;
345           }
346         }
347 
348         if (!TII->isSGPRSpill(MI))
349           continue;
350 
351         int FI = TII->getNamedOperand(MI, AMDGPU::OpName::addr)->getIndex();
352         assert(MFI.getStackID(FI) == TargetStackID::SGPRSpill);
353         if (FuncInfo->allocateSGPRSpillToVGPR(MF, FI)) {
354           bool Spilled = TRI->eliminateSGPRToVGPRSpillFrameIndex(MI, FI, nullptr);
355           (void)Spilled;
356           assert(Spilled && "failed to spill SGPR to VGPR when allocated");
357         }
358       }
359     }
360 
361     for (MachineBasicBlock &MBB : MF) {
362       for (auto SSpill : FuncInfo->getSGPRSpillVGPRs())
363         MBB.addLiveIn(SSpill.VGPR);
364 
365       for (MCPhysReg Reg : FuncInfo->getVGPRSpillAGPRs())
366         MBB.addLiveIn(Reg);
367 
368       for (MCPhysReg Reg : FuncInfo->getAGPRSpillVGPRs())
369         MBB.addLiveIn(Reg);
370 
371       MBB.sortUniqueLiveIns();
372     }
373 
374     MadeChange = true;
375   } else if (FuncInfo->VGPRReservedForSGPRSpill) {
376     FuncInfo->removeVGPRForSGPRSpill(FuncInfo->VGPRReservedForSGPRSpill, MF);
377   }
378 
379   SaveBlocks.clear();
380   RestoreBlocks.clear();
381 
382   return MadeChange;
383 }
384