blob: e98815a5d88e5b7d2eea0881a093c5a7b442978b [file] [log] [blame]
Chris Lattner58b33282002-12-28 20:43:30 +00001//===-- PrologEpilogInserter.cpp - Insert Prolog/Epilog code in function --===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner58b33282002-12-28 20:43:30 +00009//
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
Chris Lattnerf00a3f92003-01-13 00:23:41 +000019#include "llvm/CodeGen/Passes.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner58b33282002-12-28 20:43:30 +000021#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner58b33282002-12-28 20:43:30 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Target/MRegisterInfo.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000025#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000026#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnerf8c68f62006-06-28 22:17:39 +000027#include "llvm/Support/Visibility.h"
Chris Lattner05d83502004-02-15 00:14:20 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner58b33282002-12-28 20:43:30 +000030namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000031 struct VISIBILITY_HIDDEN PEI : public MachineFunctionPass {
Chris Lattnerf00a3f92003-01-13 00:23:41 +000032 const char *getPassName() const {
33 return "Prolog/Epilog Insertion & Frame Finalization";
Chris Lattner58b33282002-12-28 20:43:30 +000034 }
35
36 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
37 /// frame indexes with appropriate references.
38 ///
39 bool runOnMachineFunction(MachineFunction &Fn) {
Jim Laskey41886992006-04-07 16:34:46 +000040 // 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
Chris Lattner58b33282002-12-28 20:43:30 +000046 // 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.
Chris Lattnerc330b682004-08-12 19:01:14 +000050 calculateCallerSavedRegisters(Fn);
51
52 // Add the code to save and restore the caller saved registers
Chris Lattner58b33282002-12-28 20:43:30 +000053 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
Chris Lattnerc330b682004-08-12 19:01:14 +000062 // 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.
Chris Lattner4ac7d732003-01-15 22:52:34 +000067 insertPrologEpilogCode(Fn);
68
Chris Lattner58b33282002-12-28 20:43:30 +000069 // Replace all MO_FrameIndex operands with physical register references
70 // and actual offsets.
71 //
72 replaceFrameIndices(Fn);
Chris Lattnerc330b682004-08-12 19:01:14 +000073
74 RegsToSave.clear();
75 StackSlots.clear();
Chris Lattner58b33282002-12-28 20:43:30 +000076 return true;
77 }
78
79 private:
Chris Lattner80a4f162005-09-30 16:59:07 +000080 std::vector<std::pair<unsigned, const TargetRegisterClass*> > RegsToSave;
Chris Lattnerc330b682004-08-12 19:01:14 +000081 std::vector<int> StackSlots;
82
83 void calculateCallerSavedRegisters(MachineFunction &Fn);
Chris Lattner58b33282002-12-28 20:43:30 +000084 void saveCallerSavedRegisters(MachineFunction &Fn);
85 void calculateFrameObjectOffsets(MachineFunction &Fn);
86 void replaceFrameIndices(MachineFunction &Fn);
87 void insertPrologEpilogCode(MachineFunction &Fn);
88 };
89}
90
Brian Gaeked0fde302003-11-11 22:41:34 +000091
Chris Lattner58b33282002-12-28 20:43:30 +000092/// createPrologEpilogCodeInserter - This function returns a pass that inserts
93/// prolog and epilog code, and eliminates abstract frame references.
94///
Chris Lattner05d83502004-02-15 00:14:20 +000095FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
Chris Lattner58b33282002-12-28 20:43:30 +000096
97
Chris Lattnerc330b682004-08-12 19:01:14 +000098/// calculateCallerSavedRegisters - Scan the function for modified caller saved
99/// registers. Also calculate the MaxCallFrameSize and HasCalls variables for
Chris Lattner58b33282002-12-28 20:43:30 +0000100/// the function's frame information and eliminates call frame pseudo
101/// instructions.
102///
Chris Lattnerc330b682004-08-12 19:01:14 +0000103void PEI::calculateCallerSavedRegisters(MachineFunction &Fn) {
Chris Lattner58b33282002-12-28 20:43:30 +0000104 const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
Chris Lattnerc330b682004-08-12 19:01:14 +0000105 const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
Chris Lattner58b33282002-12-28 20:43:30 +0000106
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
Chris Lattner58b33282002-12-28 20:43:30 +0000120 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(); )
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000125 if (I->getOpcode() == FrameSetupOpcode ||
Chris Lattnerd555da52004-08-07 07:07:57 +0000126 I->getOpcode() == FrameDestroyOpcode) {
Chris Lattner2a82ef32005-05-13 21:07:15 +0000127 assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
Chris Lattnerd555da52004-08-07 07:07:57 +0000128 " 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++);
Chris Lattner58b33282002-12-28 20:43:30 +0000133 } else {
Chris Lattnerd555da52004-08-07 07:07:57 +0000134 ++I;
Chris Lattner58b33282002-12-28 20:43:30 +0000135 }
136
Chris Lattnereb24db92002-12-28 21:08:26 +0000137 MachineFrameInfo *FFI = Fn.getFrameInfo();
Chris Lattner58b33282002-12-28 20:43:30 +0000138 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 //
Chris Lattner35630152005-01-23 23:13:12 +0000144 const bool *PhysRegsUsed = Fn.getUsedPhysregs();
Chris Lattner80a4f162005-09-30 16:59:07 +0000145 const TargetRegisterClass* const *CSRegClasses =
146 RegInfo->getCalleeSaveRegClasses();
Chris Lattner58b33282002-12-28 20:43:30 +0000147 for (unsigned i = 0; CSRegs[i]; ++i) {
148 unsigned Reg = CSRegs[i];
Chris Lattner35630152005-01-23 23:13:12 +0000149 if (PhysRegsUsed[Reg]) {
Chris Lattner80a4f162005-09-30 16:59:07 +0000150 // If the reg is modified, save it!
151 RegsToSave.push_back(std::make_pair(Reg, CSRegClasses[i]));
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000152 } else {
153 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
Chris Lattner35630152005-01-23 23:13:12 +0000154 *AliasSet; ++AliasSet) { // Check alias registers too.
155 if (PhysRegsUsed[*AliasSet]) {
Chris Lattner80a4f162005-09-30 16:59:07 +0000156 RegsToSave.push_back(std::make_pair(Reg, CSRegClasses[i]));
Chris Lattnerd555da52004-08-07 07:07:57 +0000157 break;
Chris Lattnerecf8afd2004-08-07 07:18:41 +0000158 }
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000159 }
160 }
Chris Lattner58b33282002-12-28 20:43:30 +0000161 }
162
163 if (RegsToSave.empty())
164 return; // Early exit if no caller saved registers are modified!
165
Chris Lattnerc330b682004-08-12 19:01:14 +0000166 unsigned NumFixedSpillSlots;
Alkis Evlogimenos8c9b4de2004-08-15 09:18:55 +0000167 const std::pair<unsigned,int> *FixedSpillSlots =
Chris Lattnerc330b682004-08-12 19:01:14 +0000168 TFI->getCalleeSaveSpillSlots(NumFixedSpillSlots);
169
Chris Lattner58b33282002-12-28 20:43:30 +0000170 // Now that we know which registers need to be saved and restored, allocate
171 // stack slots for them.
Chris Lattner58b33282002-12-28 20:43:30 +0000172 for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) {
Chris Lattner80a4f162005-09-30 16:59:07 +0000173 unsigned Reg = RegsToSave[i].first;
Chris Lattner8fb040e2005-09-30 17:19:22 +0000174 const TargetRegisterClass *RC = RegsToSave[i].second;
Chris Lattnerc330b682004-08-12 19:01:14 +0000175
176 // Check to see if this physreg must be spilled to a particular stack slot
177 // on this target.
Alkis Evlogimenos8c9b4de2004-08-15 09:18:55 +0000178 const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
Chris Lattnerc330b682004-08-12 19:01:14 +0000179 while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
180 FixedSlot->first != Reg)
181 ++FixedSlot;
182
Chris Lattnerfa1face2004-08-21 19:45:10 +0000183 int FrameIdx;
Chris Lattnerc330b682004-08-12 19:01:14 +0000184 if (FixedSlot == FixedSpillSlots+NumFixedSpillSlots) {
185 // Nope, just spill it anywhere convenient.
Chris Lattner8fb040e2005-09-30 17:19:22 +0000186 FrameIdx = FFI->CreateStackObject(RC->getSize(), RC->getAlignment());
Chris Lattnerc330b682004-08-12 19:01:14 +0000187 } else {
188 // Spill it to the stack where we must.
Chris Lattner8fb040e2005-09-30 17:19:22 +0000189 FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->second);
Chris Lattnerc330b682004-08-12 19:01:14 +0000190 }
Chris Lattner58b33282002-12-28 20:43:30 +0000191 StackSlots.push_back(FrameIdx);
192 }
Chris Lattnerc330b682004-08-12 19:01:14 +0000193}
194
195/// saveCallerSavedRegisters - Insert spill code for any caller saved registers
196/// that are modified in the function.
197///
198void PEI::saveCallerSavedRegisters(MachineFunction &Fn) {
199 // Early exit if no caller saved registers are modified!
200 if (RegsToSave.empty())
Misha Brukmanedf128a2005-04-21 22:36:52 +0000201 return;
Chris Lattnerc330b682004-08-12 19:01:14 +0000202
203 const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
Chris Lattner58b33282002-12-28 20:43:30 +0000204
205 // Now that we have a stack slot for each register to be saved, insert spill
Chris Lattner92b9fce2005-01-23 21:45:01 +0000206 // code into the entry block.
Chris Lattner58b33282002-12-28 20:43:30 +0000207 MachineBasicBlock *MBB = Fn.begin();
208 MachineBasicBlock::iterator I = MBB->begin();
209 for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) {
Chris Lattner57f1b672004-08-15 21:56:44 +0000210 // Insert the spill to the stack frame.
Chris Lattner80a4f162005-09-30 16:59:07 +0000211 RegInfo->storeRegToStackSlot(*MBB, I, RegsToSave[i].first, StackSlots[i],
212 RegsToSave[i].second);
Chris Lattner58b33282002-12-28 20:43:30 +0000213 }
214
215 // Add code to restore the callee-save registers in each exiting block.
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000216 const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
Chris Lattner92b9fce2005-01-23 21:45:01 +0000217 for (MachineFunction::iterator FI = Fn.begin(), E = Fn.end(); FI != E; ++FI)
218 // If last instruction is a return instruction, add an epilogue.
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000219 if (!FI->empty() && TII.isReturn(FI->back().getOpcode())) {
220 MBB = FI;
221 I = MBB->end(); --I;
Chris Lattner58b33282002-12-28 20:43:30 +0000222
Chris Lattner4fc99792005-05-15 03:09:58 +0000223 // 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
Chris Lattnerdfd58702005-08-29 00:10:46 +0000229 bool AtStart = I == MBB->begin();
Chris Lattnered461e02005-08-26 22:18:32 +0000230 MachineBasicBlock::iterator BeforeI = I;
231 if (!AtStart)
232 --BeforeI;
233
Chris Lattner4fc99792005-05-15 03:09:58 +0000234 // Restore all registers immediately before the return and any terminators
235 // that preceed it.
Chris Lattner58b33282002-12-28 20:43:30 +0000236 for (unsigned i = 0, e = RegsToSave.size(); i != e; ++i) {
Chris Lattner80a4f162005-09-30 16:59:07 +0000237 RegInfo->loadRegFromStackSlot(*MBB, I, RegsToSave[i].first,
238 StackSlots[i], RegsToSave[i].second);
Chris Lattner3ca6a2c2005-01-19 21:32:07 +0000239 assert(I != MBB->begin() &&
240 "loadRegFromStackSlot didn't insert any code!");
Chris Lattnered461e02005-08-26 22:18:32 +0000241 // 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 }
Chris Lattner58b33282002-12-28 20:43:30 +0000249 }
250 }
Chris Lattner58b33282002-12-28 20:43:30 +0000251}
252
253
254/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
Chris Lattner92b9fce2005-01-23 21:45:01 +0000255/// abstract stack objects.
Chris Lattner58b33282002-12-28 20:43:30 +0000256///
257void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000258 const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000259
Chris Lattner58b33282002-12-28 20:43:30 +0000260 bool StackGrowsDown =
261 TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000262
Chris Lattner58b33282002-12-28 20:43:30 +0000263 // Loop over all of the stack objects, assigning sequential addresses...
Chris Lattnereb24db92002-12-28 21:08:26 +0000264 MachineFrameInfo *FFI = Fn.getFrameInfo();
Chris Lattner58b33282002-12-28 20:43:30 +0000265
Chris Lattnerf85249c2003-01-16 02:24:20 +0000266 unsigned StackAlignment = TFI.getStackAlignment();
Chris Lattnercbef8ba2005-11-06 17:43:20 +0000267 unsigned MaxAlign = 0;
Chris Lattner78d6db52003-01-16 02:22:08 +0000268
Chris Lattner05d83502004-02-15 00:14:20 +0000269 // Start at the beginning of the local area.
Chris Lattner577aec12004-06-10 06:23:35 +0000270 // The Offset is the distance from the stack top in the direction
271 // of stack growth -- so it's always positive.
Chris Lattner4ac7d732003-01-15 22:52:34 +0000272 int Offset = TFI.getOffsetOfLocalArea();
Chris Lattner577aec12004-06-10 06:23:35 +0000273 if (StackGrowsDown)
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000274 Offset = -Offset;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000275 assert(Offset >= 0
Chris Lattner577aec12004-06-10 06:23:35 +0000276 && "Local area offset should be in direction of stack growth");
Chris Lattner05d83502004-02-15 00:14:20 +0000277
Chris Lattner577aec12004-06-10 06:23:35 +0000278 // 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.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000280 // We currently don't support filling in holes in between fixed sized objects,
Chris Lattner577aec12004-06-10 06:23:35 +0000281 // so we adjust 'Offset' to point to the end of last fixed sized
Chris Lattner05d83502004-02-15 00:14:20 +0000282 // preallocated object.
283 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
Chris Lattner577aec12004-06-10 06:23:35 +0000284 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 {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000291 // The maximum distance from the start pointer is at the upper
Chris Lattner577aec12004-06-10 06:23:35 +0000292 // address of the object.
293 FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000294 }
295 if (FixedOff > Offset) Offset = FixedOff;
Chris Lattner05d83502004-02-15 00:14:20 +0000296 }
297
Chris Lattner58b33282002-12-28 20:43:30 +0000298 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
Chris Lattner577aec12004-06-10 06:23:35 +0000299 // 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);
Chris Lattner58b33282002-12-28 20:43:30 +0000303
304 unsigned Align = FFI->getObjectAlignment(i);
Nate Begemanae232e72005-11-06 09:00:38 +0000305 // 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;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000310
Chris Lattner577aec12004-06-10 06:23:35 +0000311 if (StackGrowsDown) {
312 FFI->setObjectOffset(i, -Offset); // Set the computed offset
313 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000314 FFI->setObjectOffset(i, Offset);
Chris Lattner577aec12004-06-10 06:23:35 +0000315 Offset += FFI->getObjectSize(i);
316 }
Chris Lattner58b33282002-12-28 20:43:30 +0000317 }
318
Chris Lattner93799292004-02-14 20:10:59 +0000319 // 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;
Chris Lattner58b33282002-12-28 20:43:30 +0000324
325 // Set the final value of the stack pointer...
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000326 FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
Chris Lattnercbef8ba2005-11-06 17:43:20 +0000327
328 // Remember the required stack alignment in case targets need it to perform
329 // dynamic stack alignment.
Chris Lattner4672f712006-04-03 21:39:57 +0000330 assert(FFI->getMaxAlignment() == MaxAlign &&
331 "Stack alignment calculation broken!");
Chris Lattner4ac7d732003-01-15 22:52:34 +0000332}
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///
339void 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
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000344 const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
Chris Lattner4ac7d732003-01-15 22:52:34 +0000345 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
346 // If last instruction is a return instruction, add an epilogue
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000347 if (!I->empty() && TII.isReturn(I->back().getOpcode()))
Chris Lattner4ac7d732003-01-15 22:52:34 +0000348 Fn.getTarget().getRegisterInfo()->emitEpilogue(Fn, *I);
349 }
Chris Lattner58b33282002-12-28 20:43:30 +0000350}
351
352
353/// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
354/// register references and actual offsets.
355///
356void 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)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000365 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
Chris Lattnerecf8afd2004-08-07 07:18:41 +0000366 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.
Nate Begeman5de0f7a2004-08-14 22:00:10 +0000369 MRI.eliminateFrameIndex(I);
Chris Lattnerecf8afd2004-08-07 07:18:41 +0000370 break;
371 }
Chris Lattner58b33282002-12-28 20:43:30 +0000372}