Copy the liveins for the first block. PR859
[oota-llvm.git] / lib / CodeGen / PrologEpilogInserter.cpp
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       RegsToSave.clear();
75       StackSlots.clear();
76       return true;
77     }
78
79   private:
80     std::vector<std::pair<unsigned, const TargetRegisterClass*> > RegsToSave;
81     std::vector<int> StackSlots;
82
83     void calculateCallerSavedRegisters(MachineFunction &Fn);
84     void saveCallerSavedRegisters(MachineFunction &Fn);
85     void calculateFrameObjectOffsets(MachineFunction &Fn);
86     void replaceFrameIndices(MachineFunction &Fn);
87     void insertPrologEpilogCode(MachineFunction &Fn);
88   };
89 }
90
91
92 /// createPrologEpilogCodeInserter - This function returns a pass that inserts
93 /// prolog and epilog code, and eliminates abstract frame references.
94 ///
95 FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
96
97
98 /// calculateCallerSavedRegisters - Scan the function for modified caller saved
99 /// registers.  Also calculate the MaxCallFrameSize and HasCalls variables for
100 /// the function's frame information and eliminates call frame pseudo
101 /// instructions.
102 ///
103 void PEI::calculateCallerSavedRegisters(MachineFunction &Fn) {
104   const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
105   const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
106
107   // Get the callee saved register list...
108   const unsigned *CSRegs = RegInfo->getCalleeSaveRegs();
109
110   // Get the function call frame set-up and tear-down instruction opcode
111   int FrameSetupOpcode   = RegInfo->getCallFrameSetupOpcode();
112   int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
113
114   // Early exit for targets which have no callee saved registers and no call
115   // frame setup/destroy pseudo instructions.
116   if ((CSRegs == 0 || CSRegs[0] == 0) &&
117       FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
118     return;
119
120   unsigned MaxCallFrameSize = 0;
121   bool HasCalls = false;
122
123   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
124     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); )
125       if (I->getOpcode() == FrameSetupOpcode ||
126           I->getOpcode() == FrameDestroyOpcode) {
127         assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
128                " instructions should have a single immediate argument!");
129         unsigned Size = I->getOperand(0).getImmedValue();
130         if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
131         HasCalls = true;
132         RegInfo->eliminateCallFramePseudoInstr(Fn, *BB, I++);
133       } else {
134         ++I;
135       }
136
137   MachineFrameInfo *FFI = Fn.getFrameInfo();
138   FFI->setHasCalls(HasCalls);
139   FFI->setMaxCallFrameSize(MaxCallFrameSize);
140
141   // Now figure out which *callee saved* registers are modified by the current
142   // function, thus needing to be saved and restored in the prolog/epilog.
143   //
144   const bool *PhysRegsUsed = Fn.getUsedPhysregs();
145   const TargetRegisterClass* const *CSRegClasses =
146     RegInfo->getCalleeSaveRegClasses();
147   for (unsigned i = 0; CSRegs[i]; ++i) {
148     unsigned Reg = CSRegs[i];
149     if (PhysRegsUsed[Reg]) {
150         // If the reg is modified, save it!
151       RegsToSave.push_back(std::make_pair(Reg, CSRegClasses[i]));
152     } else {
153       for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
154            *AliasSet; ++AliasSet) {  // Check alias registers too.
155         if (PhysRegsUsed[*AliasSet]) {
156           RegsToSave.push_back(std::make_pair(Reg, CSRegClasses[i]));
157           break;
158         }
159       }
160     }
161   }
162
163   if (RegsToSave.empty())
164     return;   // Early exit if no caller saved registers are modified!
165
166   unsigned NumFixedSpillSlots;
167   const std::pair<unsigned,int> *FixedSpillSlots =
168     TFI->getCalleeSaveSpillSlots(NumFixedSpillSlots);
169
170   // Now that we know which registers need to be saved and restored, allocate
171   // stack slots for them.
172   for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) {
173     unsigned Reg = RegsToSave[i].first;
174     const TargetRegisterClass *RC = RegsToSave[i].second;
175
176     // Check to see if this physreg must be spilled to a particular stack slot
177     // on this target.
178     const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
179     while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
180            FixedSlot->first != Reg)
181       ++FixedSlot;
182
183     int FrameIdx;
184     if (FixedSlot == FixedSpillSlots+NumFixedSpillSlots) {
185       // Nope, just spill it anywhere convenient.
186       FrameIdx = FFI->CreateStackObject(RC->getSize(), RC->getAlignment());
187     } else {
188       // Spill it to the stack where we must.
189       FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->second);
190     }
191     StackSlots.push_back(FrameIdx);
192   }
193 }
194
195 /// saveCallerSavedRegisters -  Insert spill code for any caller saved registers
196 /// that are modified in the function.
197 ///
198 void PEI::saveCallerSavedRegisters(MachineFunction &Fn) {
199   // Early exit if no caller saved registers are modified!
200   if (RegsToSave.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 = RegsToSave.size(); i != e; ++i) {
210     // Insert the spill to the stack frame.
211     RegInfo->storeRegToStackSlot(*MBB, I, RegsToSave[i].first, StackSlots[i],
212                                  RegsToSave[i].second);
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 = RegsToSave.size(); i != e; ++i) {
237         RegInfo->loadRegFromStackSlot(*MBB, I, RegsToSave[i].first,
238                                       StackSlots[i], RegsToSave[i].second);
239         assert(I != MBB->begin() &&
240                "loadRegFromStackSlot didn't insert any code!");
241         // Insert in reverse order.  loadRegFromStackSlot can insert multiple
242         // instructions.
243         if (AtStart)
244           I = MBB->begin();
245         else {
246           I = BeforeI;
247           ++I;
248         }
249       }
250     }
251 }
252
253
254 /// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
255 /// abstract stack objects.
256 ///
257 void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
258   const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
259
260   bool StackGrowsDown =
261     TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
262
263   // Loop over all of the stack objects, assigning sequential addresses...
264   MachineFrameInfo *FFI = Fn.getFrameInfo();
265
266   unsigned StackAlignment = TFI.getStackAlignment();
267   unsigned MaxAlign = 0;
268
269   // Start at the beginning of the local area.
270   // The Offset is the distance from the stack top in the direction
271   // of stack growth -- so it's always positive.
272   int Offset = TFI.getOffsetOfLocalArea();
273   if (StackGrowsDown)
274     Offset = -Offset;
275   assert(Offset >= 0
276          && "Local area offset should be in direction of stack growth");
277
278   // If there are fixed sized objects that are preallocated in the local area,
279   // non-fixed objects can't be allocated right at the start of local area.
280   // We currently don't support filling in holes in between fixed sized objects,
281   // so we adjust 'Offset' to point to the end of last fixed sized
282   // preallocated object.
283   for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
284     int FixedOff;
285     if (StackGrowsDown) {
286       // The maximum distance from the stack pointer is at lower address of
287       // the object -- which is given by offset. For down growing stack
288       // the offset is negative, so we negate the offset to get the distance.
289       FixedOff = -FFI->getObjectOffset(i);
290     } else {
291       // The maximum distance from the start pointer is at the upper
292       // address of the object.
293       FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
294     }
295     if (FixedOff > Offset) Offset = FixedOff;
296   }
297
298   for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
299     // If stack grows down, we need to add size of find the lowest
300     // address of the object.
301     if (StackGrowsDown)
302       Offset += FFI->getObjectSize(i);
303
304     unsigned Align = FFI->getObjectAlignment(i);
305     // If the alignment of this object is greater than that of the stack, then
306     // increase the stack alignment to match.
307     MaxAlign = std::max(MaxAlign, Align);
308     // Adjust to alignment boundary
309     Offset = (Offset+Align-1)/Align*Align;
310
311     if (StackGrowsDown) {
312       FFI->setObjectOffset(i, -Offset);        // Set the computed offset
313     } else {
314       FFI->setObjectOffset(i, Offset);
315       Offset += FFI->getObjectSize(i);
316     }
317   }
318
319   // Align the final stack pointer offset, but only if there are calls in the
320   // function.  This ensures that any calls to subroutines have their stack
321   // frames suitable aligned.
322   if (FFI->hasCalls())
323     Offset = (Offset+StackAlignment-1)/StackAlignment*StackAlignment;
324
325   // Set the final value of the stack pointer...
326   FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
327
328   // Remember the required stack alignment in case targets need it to perform
329   // dynamic stack alignment.
330   assert(FFI->getMaxAlignment() == MaxAlign &&
331          "Stack alignment calculation broken!");
332 }
333
334
335 /// insertPrologEpilogCode - Scan the function for modified caller saved
336 /// registers, insert spill code for these caller saved registers, then add
337 /// prolog and epilog code to the function.
338 ///
339 void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
340   // Add prologue to the function...
341   Fn.getTarget().getRegisterInfo()->emitPrologue(Fn);
342
343   // Add epilogue to restore the callee-save registers in each exiting block
344   const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
345   for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
346     // If last instruction is a return instruction, add an epilogue
347     if (!I->empty() && TII.isReturn(I->back().getOpcode()))
348       Fn.getTarget().getRegisterInfo()->emitEpilogue(Fn, *I);
349   }
350 }
351
352
353 /// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
354 /// register references and actual offsets.
355 ///
356 void PEI::replaceFrameIndices(MachineFunction &Fn) {
357   if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
358
359   const TargetMachine &TM = Fn.getTarget();
360   assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
361   const MRegisterInfo &MRI = *TM.getRegisterInfo();
362
363   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
364     for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
365       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
366         if (I->getOperand(i).isFrameIndex()) {
367           // If this instruction has a FrameIndex operand, we need to use that
368           // target machine register info object to eliminate it.
369           MRI.eliminateFrameIndex(I);
370           break;
371         }
372 }