xref: /llvm-project/llvm/lib/CodeGen/PrologEpilogInserter.cpp (revision 92206f940454edae755972aafa57d5258c4029c6)
1 //===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass is responsible for finalizing the functions frame layout, saving
11 // callee saved registers, and for emitting prolog & epilog code for the
12 // function.
13 //
14 // This pass must be run after register allocation.  After this pass is
15 // executed, it is illegal to construct MO_FrameIndex operands.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/MRegisterInfo.h"
25 #include "llvm/Target/TargetFrameInfo.h"
26 #include "llvm/Target/TargetInstrInfo.h"
27 #include "llvm/Support/Visibility.h"
28 using namespace llvm;
29 
30 namespace {
31   struct VISIBILITY_HIDDEN PEI : public MachineFunctionPass {
32     const char *getPassName() const {
33       return "Prolog/Epilog Insertion & Frame Finalization";
34     }
35 
36     /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
37     /// frame indexes with appropriate references.
38     ///
39     bool runOnMachineFunction(MachineFunction &Fn) {
40       // Get MachineDebugInfo so that we can track the construction of the
41       // frame.
42       if (MachineDebugInfo *DI = getAnalysisToUpdate<MachineDebugInfo>()) {
43         Fn.getFrameInfo()->setMachineDebugInfo(DI);
44       }
45 
46       // Scan the function for modified caller saved registers and insert spill
47       // code for any caller saved registers that are modified.  Also calculate
48       // the MaxCallFrameSize and HasCalls variables for the function's frame
49       // information and eliminates call frame pseudo instructions.
50       calculateCallerSavedRegisters(Fn);
51 
52       // Add the code to save and restore the caller saved registers
53       saveCallerSavedRegisters(Fn);
54 
55       // Allow the target machine to make final modifications to the function
56       // before the frame layout is finalized.
57       Fn.getTarget().getRegisterInfo()->processFunctionBeforeFrameFinalized(Fn);
58 
59       // Calculate actual frame offsets for all of the abstract stack objects...
60       calculateFrameObjectOffsets(Fn);
61 
62       // Add prolog and epilog code to the function.  This function is required
63       // to align the stack frame as necessary for any stack variables or
64       // called functions.  Because of this, calculateCallerSavedRegisters
65       // must be called before this function in order to set the HasCalls
66       // and MaxCallFrameSize variables.
67       insertPrologEpilogCode(Fn);
68 
69       // Replace all MO_FrameIndex operands with physical register references
70       // and actual offsets.
71       //
72       replaceFrameIndices(Fn);
73 
74       return true;
75     }
76 
77   private:
78     void calculateCallerSavedRegisters(MachineFunction &Fn);
79     void saveCallerSavedRegisters(MachineFunction &Fn);
80     void calculateFrameObjectOffsets(MachineFunction &Fn);
81     void replaceFrameIndices(MachineFunction &Fn);
82     void insertPrologEpilogCode(MachineFunction &Fn);
83   };
84 }
85 
86 
87 /// createPrologEpilogCodeInserter - This function returns a pass that inserts
88 /// prolog and epilog code, and eliminates abstract frame references.
89 ///
90 FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
91 
92 
93 /// calculateCallerSavedRegisters - Scan the function for modified caller saved
94 /// registers.  Also calculate the MaxCallFrameSize and HasCalls variables for
95 /// the function's frame information and eliminates call frame pseudo
96 /// instructions.
97 ///
98 void PEI::calculateCallerSavedRegisters(MachineFunction &Fn) {
99   const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
100   const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
101 
102   // Get the callee saved register list...
103   const unsigned *CSRegs = RegInfo->getCalleeSaveRegs();
104 
105   // Get the function call frame set-up and tear-down instruction opcode
106   int FrameSetupOpcode   = RegInfo->getCallFrameSetupOpcode();
107   int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
108 
109   // Early exit for targets which have no callee saved registers and no call
110   // frame setup/destroy pseudo instructions.
111   if ((CSRegs == 0 || CSRegs[0] == 0) &&
112       FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
113     return;
114 
115   unsigned MaxCallFrameSize = 0;
116   bool HasCalls = false;
117 
118   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
119     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); )
120       if (I->getOpcode() == FrameSetupOpcode ||
121           I->getOpcode() == FrameDestroyOpcode) {
122         assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
123                " instructions should have a single immediate argument!");
124         unsigned Size = I->getOperand(0).getImmedValue();
125         if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
126         HasCalls = true;
127         RegInfo->eliminateCallFramePseudoInstr(Fn, *BB, I++);
128       } else {
129         ++I;
130       }
131 
132   MachineFrameInfo *FFI = Fn.getFrameInfo();
133   FFI->setHasCalls(HasCalls);
134   FFI->setMaxCallFrameSize(MaxCallFrameSize);
135 
136   // Now figure out which *callee saved* registers are modified by the current
137   // function, thus needing to be saved and restored in the prolog/epilog.
138   //
139   const bool *PhysRegsUsed = Fn.getUsedPhysregs();
140   const TargetRegisterClass* const *CSRegClasses =
141     RegInfo->getCalleeSaveRegClasses();
142   std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
143   for (unsigned i = 0; CSRegs[i]; ++i) {
144     unsigned Reg = CSRegs[i];
145     if (PhysRegsUsed[Reg]) {
146         // If the reg is modified, save it!
147       CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
148     } else {
149       for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
150            *AliasSet; ++AliasSet) {  // Check alias registers too.
151         if (PhysRegsUsed[*AliasSet]) {
152           CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
153           break;
154         }
155       }
156     }
157   }
158 
159   if (CSI.empty())
160     return;   // Early exit if no caller saved registers are modified!
161 
162   unsigned NumFixedSpillSlots;
163   const std::pair<unsigned,int> *FixedSpillSlots =
164     TFI->getCalleeSaveSpillSlots(NumFixedSpillSlots);
165 
166   // Now that we know which registers need to be saved and restored, allocate
167   // stack slots for them.
168   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
169     unsigned Reg = CSI[i].getReg();
170     const TargetRegisterClass *RC = CSI[i].getRegClass();
171 
172     // Check to see if this physreg must be spilled to a particular stack slot
173     // on this target.
174     const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
175     while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
176            FixedSlot->first != Reg)
177       ++FixedSlot;
178 
179     int FrameIdx;
180     if (FixedSlot == FixedSpillSlots+NumFixedSpillSlots) {
181       // Nope, just spill it anywhere convenient.
182       FrameIdx = FFI->CreateStackObject(RC->getSize(), RC->getAlignment());
183     } else {
184       // Spill it to the stack where we must.
185       FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->second);
186     }
187     CSI[i].setFrameIdx(FrameIdx);
188   }
189 }
190 
191 /// saveCallerSavedRegisters -  Insert spill code for any caller saved registers
192 /// that are modified in the function.
193 ///
194 void PEI::saveCallerSavedRegisters(MachineFunction &Fn) {
195   // Get callee saved register information.
196   MachineFrameInfo *FFI = Fn.getFrameInfo();
197   std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
198 
199   // Early exit if no caller saved registers are modified!
200   if (CSI.empty())
201     return;
202 
203   const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
204 
205   // Now that we have a stack slot for each register to be saved, insert spill
206   // code into the entry block.
207   MachineBasicBlock *MBB = Fn.begin();
208   MachineBasicBlock::iterator I = MBB->begin();
209   for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
210     // Insert the spill to the stack frame.
211     RegInfo->storeRegToStackSlot(*MBB, I, CSI[i].getReg(), CSI[i].getFrameIdx(),
212                                  CSI[i].getRegClass());
213   }
214 
215   // Add code to restore the callee-save registers in each exiting block.
216   const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
217   for (MachineFunction::iterator FI = Fn.begin(), E = Fn.end(); FI != E; ++FI)
218     // If last instruction is a return instruction, add an epilogue.
219     if (!FI->empty() && TII.isReturn(FI->back().getOpcode())) {
220       MBB = FI;
221       I = MBB->end(); --I;
222 
223       // Skip over all terminator instructions, which are part of the return
224       // sequence.
225       MachineBasicBlock::iterator I2 = I;
226       while (I2 != MBB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
227         I = I2;
228 
229       bool AtStart = I == MBB->begin();
230       MachineBasicBlock::iterator BeforeI = I;
231       if (!AtStart)
232         --BeforeI;
233 
234       // Restore all registers immediately before the return and any terminators
235       // that preceed it.
236       for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
237         RegInfo->loadRegFromStackSlot(*MBB, I, CSI[i].getReg(),
238                                       CSI[i].getFrameIdx(),
239                                       CSI[i].getRegClass());
240         assert(I != MBB->begin() &&
241                "loadRegFromStackSlot didn't insert any code!");
242         // Insert in reverse order.  loadRegFromStackSlot can insert multiple
243         // instructions.
244         if (AtStart)
245           I = MBB->begin();
246         else {
247           I = BeforeI;
248           ++I;
249         }
250       }
251     }
252 }
253 
254 
255 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
256 /// abstract stack objects.
257 ///
258 void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
259   const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
260 
261   bool StackGrowsDown =
262     TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
263 
264   // Loop over all of the stack objects, assigning sequential addresses...
265   MachineFrameInfo *FFI = Fn.getFrameInfo();
266 
267   unsigned StackAlignment = TFI.getStackAlignment();
268   unsigned MaxAlign = 0;
269 
270   // Start at the beginning of the local area.
271   // The Offset is the distance from the stack top in the direction
272   // of stack growth -- so it's always positive.
273   int Offset = TFI.getOffsetOfLocalArea();
274   if (StackGrowsDown)
275     Offset = -Offset;
276   assert(Offset >= 0
277          && "Local area offset should be in direction of stack growth");
278 
279   // If there are fixed sized objects that are preallocated in the local area,
280   // non-fixed objects can't be allocated right at the start of local area.
281   // We currently don't support filling in holes in between fixed sized objects,
282   // so we adjust 'Offset' to point to the end of last fixed sized
283   // preallocated object.
284   for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
285     int FixedOff;
286     if (StackGrowsDown) {
287       // The maximum distance from the stack pointer is at lower address of
288       // the object -- which is given by offset. For down growing stack
289       // the offset is negative, so we negate the offset to get the distance.
290       FixedOff = -FFI->getObjectOffset(i);
291     } else {
292       // The maximum distance from the start pointer is at the upper
293       // address of the object.
294       FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
295     }
296     if (FixedOff > Offset) Offset = FixedOff;
297   }
298 
299   for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
300     // If stack grows down, we need to add size of find the lowest
301     // address of the object.
302     if (StackGrowsDown)
303       Offset += FFI->getObjectSize(i);
304 
305     unsigned Align = FFI->getObjectAlignment(i);
306     // If the alignment of this object is greater than that of the stack, then
307     // increase the stack alignment to match.
308     MaxAlign = std::max(MaxAlign, Align);
309     // Adjust to alignment boundary
310     Offset = (Offset+Align-1)/Align*Align;
311 
312     if (StackGrowsDown) {
313       FFI->setObjectOffset(i, -Offset);        // Set the computed offset
314     } else {
315       FFI->setObjectOffset(i, Offset);
316       Offset += FFI->getObjectSize(i);
317     }
318   }
319 
320   // Align the final stack pointer offset, but only if there are calls in the
321   // function.  This ensures that any calls to subroutines have their stack
322   // frames suitable aligned.
323   if (FFI->hasCalls())
324     Offset = (Offset+StackAlignment-1)/StackAlignment*StackAlignment;
325 
326   // Set the final value of the stack pointer...
327   FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
328 
329   // Remember the required stack alignment in case targets need it to perform
330   // dynamic stack alignment.
331   assert(FFI->getMaxAlignment() == MaxAlign &&
332          "Stack alignment calculation broken!");
333 }
334 
335 
336 /// insertPrologEpilogCode - Scan the function for modified caller saved
337 /// registers, insert spill code for these caller saved registers, then add
338 /// prolog and epilog code to the function.
339 ///
340 void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
341   // Add prologue to the function...
342   Fn.getTarget().getRegisterInfo()->emitPrologue(Fn);
343 
344   // Add epilogue to restore the callee-save registers in each exiting block
345   const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
346   for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
347     // If last instruction is a return instruction, add an epilogue
348     if (!I->empty() && TII.isReturn(I->back().getOpcode()))
349       Fn.getTarget().getRegisterInfo()->emitEpilogue(Fn, *I);
350   }
351 }
352 
353 
354 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
355 /// register references and actual offsets.
356 ///
357 void PEI::replaceFrameIndices(MachineFunction &Fn) {
358   if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
359 
360   const TargetMachine &TM = Fn.getTarget();
361   assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
362   const MRegisterInfo &MRI = *TM.getRegisterInfo();
363 
364   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
365     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
366       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
367         if (I->getOperand(i).isFrameIndex()) {
368           // If this instruction has a FrameIndex operand, we need to use that
369           // target machine register info object to eliminate it.
370           MRI.eliminateFrameIndex(I);
371           break;
372         }
373 }
374