blob: 55172b563edb8e3c02b52225b305bf6a706a44a3 [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"
Evan Cheng49dd0642007-02-23 01:11:26 +000023#include "llvm/CodeGen/RegisterScavenging.h"
Chris Lattner58b33282002-12-28 20:43:30 +000024#include "llvm/Target/TargetMachine.h"
25#include "llvm/Target/MRegisterInfo.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000026#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000027#include "llvm/Target/TargetInstrInfo.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Evan Cheng8e334732007-05-01 09:01:42 +000029#include "llvm/ADT/STLExtras.h"
Evan Chengc2b4ec32006-09-28 00:10:27 +000030#include <climits>
Chris Lattner05d83502004-02-15 00:14:20 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner58b33282002-12-28 20:43:30 +000033namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000034 struct VISIBILITY_HIDDEN PEI : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000035 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000036 PEI() : MachineFunctionPass((intptr_t)&ID) {}
37
Chris Lattnerf00a3f92003-01-13 00:23:41 +000038 const char *getPassName() const {
39 return "Prolog/Epilog Insertion & Frame Finalization";
Chris Lattner58b33282002-12-28 20:43:30 +000040 }
41
42 /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
43 /// frame indexes with appropriate references.
44 ///
45 bool runOnMachineFunction(MachineFunction &Fn) {
Evan Cheng87f8bf62007-03-06 10:02:38 +000046 const MRegisterInfo *MRI = Fn.getTarget().getRegisterInfo();
47 RS = MRI->requiresRegisterScavenging(Fn) ? new RegScavenger() : NULL;
48
Jim Laskey44c3b9f2007-01-26 21:22:28 +000049 // Get MachineModuleInfo so that we can track the construction of the
Jim Laskey41886992006-04-07 16:34:46 +000050 // frame.
Jim Laskey44c3b9f2007-01-26 21:22:28 +000051 if (MachineModuleInfo *MMI = getAnalysisToUpdate<MachineModuleInfo>()) {
52 Fn.getFrameInfo()->setMachineModuleInfo(MMI);
Jim Laskey41886992006-04-07 16:34:46 +000053 }
Evan Chengc2b4ec32006-09-28 00:10:27 +000054
55 // Allow the target machine to make some adjustments to the function
56 // e.g. UsedPhysRegs before calculateCalleeSavedRegisters.
Evan Cheng87f8bf62007-03-06 10:02:38 +000057 MRI->processFunctionBeforeCalleeSavedScan(Fn, RS);
Evan Chengc2b4ec32006-09-28 00:10:27 +000058
Evan Cheng692d4e02006-09-26 22:29:31 +000059 // Scan the function for modified callee saved registers and insert spill
60 // code for any callee saved registers that are modified. Also calculate
Chris Lattner58b33282002-12-28 20:43:30 +000061 // the MaxCallFrameSize and HasCalls variables for the function's frame
62 // information and eliminates call frame pseudo instructions.
Evan Cheng692d4e02006-09-26 22:29:31 +000063 calculateCalleeSavedRegisters(Fn);
Chris Lattnerc330b682004-08-12 19:01:14 +000064
Evan Chengc2b4ec32006-09-28 00:10:27 +000065 // Add the code to save and restore the callee saved registers
66 saveCalleeSavedRegisters(Fn);
Chris Lattner58b33282002-12-28 20:43:30 +000067
68 // Allow the target machine to make final modifications to the function
69 // before the frame layout is finalized.
70 Fn.getTarget().getRegisterInfo()->processFunctionBeforeFrameFinalized(Fn);
71
72 // Calculate actual frame offsets for all of the abstract stack objects...
73 calculateFrameObjectOffsets(Fn);
74
Chris Lattnerc330b682004-08-12 19:01:14 +000075 // Add prolog and epilog code to the function. This function is required
76 // to align the stack frame as necessary for any stack variables or
Evan Cheng692d4e02006-09-26 22:29:31 +000077 // called functions. Because of this, calculateCalleeSavedRegisters
Chris Lattnerc330b682004-08-12 19:01:14 +000078 // must be called before this function in order to set the HasCalls
79 // and MaxCallFrameSize variables.
Chris Lattner4ac7d732003-01-15 22:52:34 +000080 insertPrologEpilogCode(Fn);
81
Chris Lattner58b33282002-12-28 20:43:30 +000082 // Replace all MO_FrameIndex operands with physical register references
83 // and actual offsets.
84 //
85 replaceFrameIndices(Fn);
Chris Lattnerc330b682004-08-12 19:01:14 +000086
Evan Cheng87f8bf62007-03-06 10:02:38 +000087 delete RS;
Chris Lattner58b33282002-12-28 20:43:30 +000088 return true;
89 }
Jim Laskeyf3e4f0e2006-08-25 19:45:51 +000090
Chris Lattner58b33282002-12-28 20:43:30 +000091 private:
Evan Cheng87f8bf62007-03-06 10:02:38 +000092 RegScavenger *RS;
93
Evan Chengad93d7f2007-01-02 21:31:15 +000094 // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
Evan Chengc2b4ec32006-09-28 00:10:27 +000095 // stack frame indexes.
96 unsigned MinCSFrameIndex, MaxCSFrameIndex;
97
Evan Cheng692d4e02006-09-26 22:29:31 +000098 void calculateCalleeSavedRegisters(MachineFunction &Fn);
Evan Chengc2b4ec32006-09-28 00:10:27 +000099 void saveCalleeSavedRegisters(MachineFunction &Fn);
Chris Lattner58b33282002-12-28 20:43:30 +0000100 void calculateFrameObjectOffsets(MachineFunction &Fn);
101 void replaceFrameIndices(MachineFunction &Fn);
102 void insertPrologEpilogCode(MachineFunction &Fn);
103 };
Devang Patel19974732007-05-03 01:11:54 +0000104 char PEI::ID = 0;
Chris Lattner58b33282002-12-28 20:43:30 +0000105}
106
Brian Gaeked0fde302003-11-11 22:41:34 +0000107
Chris Lattner58b33282002-12-28 20:43:30 +0000108/// createPrologEpilogCodeInserter - This function returns a pass that inserts
109/// prolog and epilog code, and eliminates abstract frame references.
110///
Chris Lattner05d83502004-02-15 00:14:20 +0000111FunctionPass *llvm::createPrologEpilogCodeInserter() { return new PEI(); }
Chris Lattner58b33282002-12-28 20:43:30 +0000112
113
Evan Chengc2b4ec32006-09-28 00:10:27 +0000114/// calculateCalleeSavedRegisters - Scan the function for modified callee saved
Chris Lattnerc330b682004-08-12 19:01:14 +0000115/// registers. Also calculate the MaxCallFrameSize and HasCalls variables for
Chris Lattner58b33282002-12-28 20:43:30 +0000116/// the function's frame information and eliminates call frame pseudo
117/// instructions.
118///
Evan Cheng692d4e02006-09-26 22:29:31 +0000119void PEI::calculateCalleeSavedRegisters(MachineFunction &Fn) {
Chris Lattner58b33282002-12-28 20:43:30 +0000120 const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
Chris Lattnerc330b682004-08-12 19:01:14 +0000121 const TargetFrameInfo *TFI = Fn.getTarget().getFrameInfo();
Chris Lattner58b33282002-12-28 20:43:30 +0000122
123 // Get the callee saved register list...
Evan Chengad93d7f2007-01-02 21:31:15 +0000124 const unsigned *CSRegs = RegInfo->getCalleeSavedRegs();
Chris Lattner58b33282002-12-28 20:43:30 +0000125
126 // Get the function call frame set-up and tear-down instruction opcode
127 int FrameSetupOpcode = RegInfo->getCallFrameSetupOpcode();
128 int FrameDestroyOpcode = RegInfo->getCallFrameDestroyOpcode();
129
Evan Cheng7d3223e2006-12-07 02:25:34 +0000130 // These are used to keep track the callee-save area. Initialize them.
131 MinCSFrameIndex = INT_MAX;
132 MaxCSFrameIndex = 0;
133
Chris Lattner58b33282002-12-28 20:43:30 +0000134 // Early exit for targets which have no callee saved registers and no call
135 // frame setup/destroy pseudo instructions.
136 if ((CSRegs == 0 || CSRegs[0] == 0) &&
137 FrameSetupOpcode == -1 && FrameDestroyOpcode == -1)
138 return;
139
Chris Lattner58b33282002-12-28 20:43:30 +0000140 unsigned MaxCallFrameSize = 0;
141 bool HasCalls = false;
142
Evan Cheng5c3885c2007-05-01 00:52:08 +0000143 std::vector<MachineBasicBlock::iterator> FrameSDOps;
Chris Lattner58b33282002-12-28 20:43:30 +0000144 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB)
Evan Cheng5c3885c2007-05-01 00:52:08 +0000145 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ++I)
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000146 if (I->getOpcode() == FrameSetupOpcode ||
Chris Lattnerd555da52004-08-07 07:07:57 +0000147 I->getOpcode() == FrameDestroyOpcode) {
Chris Lattner2a82ef32005-05-13 21:07:15 +0000148 assert(I->getNumOperands() >= 1 && "Call Frame Setup/Destroy Pseudo"
Chris Lattnerd555da52004-08-07 07:07:57 +0000149 " instructions should have a single immediate argument!");
150 unsigned Size = I->getOperand(0).getImmedValue();
151 if (Size > MaxCallFrameSize) MaxCallFrameSize = Size;
152 HasCalls = true;
Evan Cheng5c3885c2007-05-01 00:52:08 +0000153 FrameSDOps.push_back(I);
Chris Lattner58b33282002-12-28 20:43:30 +0000154 }
155
Chris Lattnereb24db92002-12-28 21:08:26 +0000156 MachineFrameInfo *FFI = Fn.getFrameInfo();
Chris Lattner58b33282002-12-28 20:43:30 +0000157 FFI->setHasCalls(HasCalls);
158 FFI->setMaxCallFrameSize(MaxCallFrameSize);
Evan Cheng8e334732007-05-01 09:01:42 +0000159
Evan Cheng5c3885c2007-05-01 00:52:08 +0000160 for (unsigned i = 0, e = FrameSDOps.size(); i != e; ++i) {
161 MachineBasicBlock::iterator I = FrameSDOps[i];
Evan Cheng8e334732007-05-01 09:01:42 +0000162 // If call frames are not being included as part of the stack frame,
163 // and there is no dynamic allocation (therefore referencing frame slots
164 // off sp), leave the pseudo ops alone. We'll eliminate them later.
165 if (RegInfo->hasReservedCallFrame(Fn) || RegInfo->hasFP(Fn))
166 RegInfo->eliminateCallFramePseudoInstr(Fn, *I->getParent(), I);
Evan Cheng5c3885c2007-05-01 00:52:08 +0000167 }
Chris Lattner58b33282002-12-28 20:43:30 +0000168
169 // Now figure out which *callee saved* registers are modified by the current
170 // function, thus needing to be saved and restored in the prolog/epilog.
171 //
Chris Lattner80a4f162005-09-30 16:59:07 +0000172 const TargetRegisterClass* const *CSRegClasses =
Evan Chengad93d7f2007-01-02 21:31:15 +0000173 RegInfo->getCalleeSavedRegClasses();
Jim Laskey08ede262006-08-25 22:56:30 +0000174 std::vector<CalleeSavedInfo> CSI;
Chris Lattner58b33282002-12-28 20:43:30 +0000175 for (unsigned i = 0; CSRegs[i]; ++i) {
176 unsigned Reg = CSRegs[i];
Evan Cheng6c087e52007-04-25 22:13:27 +0000177 if (Fn.isPhysRegUsed(Reg)) {
Chris Lattner80a4f162005-09-30 16:59:07 +0000178 // If the reg is modified, save it!
Jim Laskeyf3e4f0e2006-08-25 19:45:51 +0000179 CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000180 } else {
181 for (const unsigned *AliasSet = RegInfo->getAliasSet(Reg);
Chris Lattner35630152005-01-23 23:13:12 +0000182 *AliasSet; ++AliasSet) { // Check alias registers too.
Evan Cheng6c087e52007-04-25 22:13:27 +0000183 if (Fn.isPhysRegUsed(*AliasSet)) {
Jim Laskeyf3e4f0e2006-08-25 19:45:51 +0000184 CSI.push_back(CalleeSavedInfo(Reg, CSRegClasses[i]));
Chris Lattnerd555da52004-08-07 07:07:57 +0000185 break;
Chris Lattnerecf8afd2004-08-07 07:18:41 +0000186 }
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000187 }
188 }
Chris Lattner58b33282002-12-28 20:43:30 +0000189 }
190
Jim Laskeyf3e4f0e2006-08-25 19:45:51 +0000191 if (CSI.empty())
Evan Chengc2b4ec32006-09-28 00:10:27 +0000192 return; // Early exit if no callee saved registers are modified!
Chris Lattner58b33282002-12-28 20:43:30 +0000193
Chris Lattnerc330b682004-08-12 19:01:14 +0000194 unsigned NumFixedSpillSlots;
Alkis Evlogimenos8c9b4de2004-08-15 09:18:55 +0000195 const std::pair<unsigned,int> *FixedSpillSlots =
Evan Chengad93d7f2007-01-02 21:31:15 +0000196 TFI->getCalleeSavedSpillSlots(NumFixedSpillSlots);
Chris Lattnerc330b682004-08-12 19:01:14 +0000197
Chris Lattner58b33282002-12-28 20:43:30 +0000198 // Now that we know which registers need to be saved and restored, allocate
199 // stack slots for them.
Jim Laskeyf3e4f0e2006-08-25 19:45:51 +0000200 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
201 unsigned Reg = CSI[i].getReg();
202 const TargetRegisterClass *RC = CSI[i].getRegClass();
Chris Lattnerc330b682004-08-12 19:01:14 +0000203
204 // Check to see if this physreg must be spilled to a particular stack slot
205 // on this target.
Alkis Evlogimenos8c9b4de2004-08-15 09:18:55 +0000206 const std::pair<unsigned,int> *FixedSlot = FixedSpillSlots;
Chris Lattnerc330b682004-08-12 19:01:14 +0000207 while (FixedSlot != FixedSpillSlots+NumFixedSpillSlots &&
208 FixedSlot->first != Reg)
209 ++FixedSlot;
210
Chris Lattnerfa1face2004-08-21 19:45:10 +0000211 int FrameIdx;
Chris Lattnerc330b682004-08-12 19:01:14 +0000212 if (FixedSlot == FixedSpillSlots+NumFixedSpillSlots) {
213 // Nope, just spill it anywhere convenient.
Evan Cheng5feaa9a2006-09-28 18:52:32 +0000214 unsigned Align = RC->getAlignment();
215 unsigned StackAlign = TFI->getStackAlignment();
216 // We may not be able to sastify the desired alignment specification of
217 // the TargetRegisterClass if the stack alignment is smaller. Use the min.
218 Align = std::min(Align, StackAlign);
219 FrameIdx = FFI->CreateStackObject(RC->getSize(), Align);
Evan Chengc2b4ec32006-09-28 00:10:27 +0000220 if ((unsigned)FrameIdx < MinCSFrameIndex) MinCSFrameIndex = FrameIdx;
221 if ((unsigned)FrameIdx > MaxCSFrameIndex) MaxCSFrameIndex = FrameIdx;
Chris Lattnerc330b682004-08-12 19:01:14 +0000222 } else {
223 // Spill it to the stack where we must.
Chris Lattner8fb040e2005-09-30 17:19:22 +0000224 FrameIdx = FFI->CreateFixedObject(RC->getSize(), FixedSlot->second);
Chris Lattnerc330b682004-08-12 19:01:14 +0000225 }
Jim Laskeyf3e4f0e2006-08-25 19:45:51 +0000226 CSI[i].setFrameIdx(FrameIdx);
Chris Lattner58b33282002-12-28 20:43:30 +0000227 }
Jim Laskey08ede262006-08-25 22:56:30 +0000228
229 FFI->setCalleeSavedInfo(CSI);
Chris Lattnerc330b682004-08-12 19:01:14 +0000230}
231
Evan Chengc2b4ec32006-09-28 00:10:27 +0000232/// saveCalleeSavedRegisters - Insert spill code for any callee saved registers
Chris Lattnerc330b682004-08-12 19:01:14 +0000233/// that are modified in the function.
234///
Evan Chengc2b4ec32006-09-28 00:10:27 +0000235void PEI::saveCalleeSavedRegisters(MachineFunction &Fn) {
Jim Laskeyf3e4f0e2006-08-25 19:45:51 +0000236 // Get callee saved register information.
237 MachineFrameInfo *FFI = Fn.getFrameInfo();
Jim Laskey08ede262006-08-25 22:56:30 +0000238 const std::vector<CalleeSavedInfo> &CSI = FFI->getCalleeSavedInfo();
Jim Laskeyf3e4f0e2006-08-25 19:45:51 +0000239
Evan Chengc2b4ec32006-09-28 00:10:27 +0000240 // Early exit if no callee saved registers are modified!
Jim Laskeyf3e4f0e2006-08-25 19:45:51 +0000241 if (CSI.empty())
Misha Brukmanedf128a2005-04-21 22:36:52 +0000242 return;
Chris Lattnerc330b682004-08-12 19:01:14 +0000243
244 const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
Chris Lattner58b33282002-12-28 20:43:30 +0000245
246 // Now that we have a stack slot for each register to be saved, insert spill
Chris Lattner92b9fce2005-01-23 21:45:01 +0000247 // code into the entry block.
Chris Lattner58b33282002-12-28 20:43:30 +0000248 MachineBasicBlock *MBB = Fn.begin();
249 MachineBasicBlock::iterator I = MBB->begin();
Evan Chengad93d7f2007-01-02 21:31:15 +0000250 if (!RegInfo->spillCalleeSavedRegisters(*MBB, I, CSI)) {
251 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
Evan Cheng49dd0642007-02-23 01:11:26 +0000252 // Add the callee-saved register as live-in. It's killed at the spill.
253 MBB->addLiveIn(CSI[i].getReg());
254
Evan Chengad93d7f2007-01-02 21:31:15 +0000255 // Insert the spill to the stack frame.
256 RegInfo->storeRegToStackSlot(*MBB, I, CSI[i].getReg(),
Evan Cheng49dd0642007-02-23 01:11:26 +0000257 CSI[i].getFrameIdx(), CSI[i].getRegClass());
Evan Chengad93d7f2007-01-02 21:31:15 +0000258 }
Chris Lattner58b33282002-12-28 20:43:30 +0000259 }
260
261 // Add code to restore the callee-save registers in each exiting block.
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000262 const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
Chris Lattner92b9fce2005-01-23 21:45:01 +0000263 for (MachineFunction::iterator FI = Fn.begin(), E = Fn.end(); FI != E; ++FI)
264 // If last instruction is a return instruction, add an epilogue.
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000265 if (!FI->empty() && TII.isReturn(FI->back().getOpcode())) {
266 MBB = FI;
267 I = MBB->end(); --I;
Chris Lattner58b33282002-12-28 20:43:30 +0000268
Chris Lattner4fc99792005-05-15 03:09:58 +0000269 // Skip over all terminator instructions, which are part of the return
270 // sequence.
271 MachineBasicBlock::iterator I2 = I;
272 while (I2 != MBB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
273 I = I2;
274
Chris Lattnerdfd58702005-08-29 00:10:46 +0000275 bool AtStart = I == MBB->begin();
Chris Lattnered461e02005-08-26 22:18:32 +0000276 MachineBasicBlock::iterator BeforeI = I;
277 if (!AtStart)
278 --BeforeI;
279
Chris Lattner4fc99792005-05-15 03:09:58 +0000280 // Restore all registers immediately before the return and any terminators
281 // that preceed it.
Evan Chengad93d7f2007-01-02 21:31:15 +0000282 if (!RegInfo->restoreCalleeSavedRegisters(*MBB, I, CSI)) {
283 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
284 RegInfo->loadRegFromStackSlot(*MBB, I, CSI[i].getReg(),
285 CSI[i].getFrameIdx(),
286 CSI[i].getRegClass());
287 assert(I != MBB->begin() &&
288 "loadRegFromStackSlot didn't insert any code!");
289 // Insert in reverse order. loadRegFromStackSlot can insert multiple
290 // instructions.
291 if (AtStart)
292 I = MBB->begin();
293 else {
294 I = BeforeI;
295 ++I;
296 }
Chris Lattnered461e02005-08-26 22:18:32 +0000297 }
Chris Lattner58b33282002-12-28 20:43:30 +0000298 }
299 }
Chris Lattner58b33282002-12-28 20:43:30 +0000300}
301
302
303/// calculateFrameObjectOffsets - Calculate actual frame offsets for all of the
Chris Lattner92b9fce2005-01-23 21:45:01 +0000304/// abstract stack objects.
Chris Lattner58b33282002-12-28 20:43:30 +0000305///
306void PEI::calculateFrameObjectOffsets(MachineFunction &Fn) {
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000307 const TargetFrameInfo &TFI = *Fn.getTarget().getFrameInfo();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000308
Chris Lattner58b33282002-12-28 20:43:30 +0000309 bool StackGrowsDown =
310 TFI.getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000311
Chris Lattner58b33282002-12-28 20:43:30 +0000312 // Loop over all of the stack objects, assigning sequential addresses...
Chris Lattnereb24db92002-12-28 21:08:26 +0000313 MachineFrameInfo *FFI = Fn.getFrameInfo();
Chris Lattner58b33282002-12-28 20:43:30 +0000314
Chris Lattnercbef8ba2005-11-06 17:43:20 +0000315 unsigned MaxAlign = 0;
Chris Lattner78d6db52003-01-16 02:22:08 +0000316
Chris Lattner05d83502004-02-15 00:14:20 +0000317 // Start at the beginning of the local area.
Chris Lattner577aec12004-06-10 06:23:35 +0000318 // The Offset is the distance from the stack top in the direction
319 // of stack growth -- so it's always positive.
Chris Lattnera401b1e2007-04-25 04:20:54 +0000320 int64_t Offset = TFI.getOffsetOfLocalArea();
Chris Lattner577aec12004-06-10 06:23:35 +0000321 if (StackGrowsDown)
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000322 Offset = -Offset;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000323 assert(Offset >= 0
Chris Lattner577aec12004-06-10 06:23:35 +0000324 && "Local area offset should be in direction of stack growth");
Chris Lattner05d83502004-02-15 00:14:20 +0000325
Chris Lattner577aec12004-06-10 06:23:35 +0000326 // If there are fixed sized objects that are preallocated in the local area,
327 // non-fixed objects can't be allocated right at the start of local area.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000328 // We currently don't support filling in holes in between fixed sized objects,
Chris Lattner577aec12004-06-10 06:23:35 +0000329 // so we adjust 'Offset' to point to the end of last fixed sized
Chris Lattner05d83502004-02-15 00:14:20 +0000330 // preallocated object.
331 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000332 int64_t FixedOff;
Chris Lattner577aec12004-06-10 06:23:35 +0000333 if (StackGrowsDown) {
334 // The maximum distance from the stack pointer is at lower address of
335 // the object -- which is given by offset. For down growing stack
336 // the offset is negative, so we negate the offset to get the distance.
337 FixedOff = -FFI->getObjectOffset(i);
338 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000339 // The maximum distance from the start pointer is at the upper
Chris Lattner577aec12004-06-10 06:23:35 +0000340 // address of the object.
341 FixedOff = FFI->getObjectOffset(i) + FFI->getObjectSize(i);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000342 }
343 if (FixedOff > Offset) Offset = FixedOff;
Chris Lattner05d83502004-02-15 00:14:20 +0000344 }
345
Evan Chengc2b4ec32006-09-28 00:10:27 +0000346 // First assign frame offsets to stack objects that are used to spill
Evan Chengad93d7f2007-01-02 21:31:15 +0000347 // callee saved registers.
Evan Chengc2b4ec32006-09-28 00:10:27 +0000348 if (StackGrowsDown) {
Evan Cheng5c3885c2007-05-01 00:52:08 +0000349 for (unsigned i = MinCSFrameIndex; i <= MaxCSFrameIndex; ++i) {
Evan Chengc2b4ec32006-09-28 00:10:27 +0000350 // If stack grows down, we need to add size of find the lowest
351 // address of the object.
352 Offset += FFI->getObjectSize(i);
353
354 unsigned Align = FFI->getObjectAlignment(i);
355 // If the alignment of this object is greater than that of the stack, then
356 // increase the stack alignment to match.
357 MaxAlign = std::max(MaxAlign, Align);
358 // Adjust to alignment boundary
359 Offset = (Offset+Align-1)/Align*Align;
360
361 FFI->setObjectOffset(i, -Offset); // Set the computed offset
362 }
363 } else {
Evan Cheng5c3885c2007-05-01 00:52:08 +0000364 for (unsigned i = MaxCSFrameIndex; i >= MinCSFrameIndex; --i) {
Evan Chengc2b4ec32006-09-28 00:10:27 +0000365 unsigned Align = FFI->getObjectAlignment(i);
366 // If the alignment of this object is greater than that of the stack, then
367 // increase the stack alignment to match.
368 MaxAlign = std::max(MaxAlign, Align);
369 // Adjust to alignment boundary
370 Offset = (Offset+Align-1)/Align*Align;
371
372 FFI->setObjectOffset(i, Offset);
373 Offset += FFI->getObjectSize(i);
374 }
375 }
376
Evan Cheng87f8bf62007-03-06 10:02:38 +0000377 // Make sure the special register scavenging spill slot is closest to the
378 // frame pointer if a frame pointer is required.
379 const MRegisterInfo *RegInfo = Fn.getTarget().getRegisterInfo();
380 if (RS && RegInfo->hasFP(Fn)) {
381 int SFI = RS->getScavengingFrameIndex();
382 if (SFI >= 0) {
Evan Cheng5c3885c2007-05-01 00:52:08 +0000383 // If stack grows down, we need to add size of the lowest
Evan Cheng87f8bf62007-03-06 10:02:38 +0000384 // address of the object.
385 if (StackGrowsDown)
386 Offset += FFI->getObjectSize(SFI);
387
388 unsigned Align = FFI->getObjectAlignment(SFI);
389 // Adjust to alignment boundary
390 Offset = (Offset+Align-1)/Align*Align;
391
392 if (StackGrowsDown) {
393 FFI->setObjectOffset(SFI, -Offset); // Set the computed offset
394 } else {
395 FFI->setObjectOffset(SFI, Offset);
396 Offset += FFI->getObjectSize(SFI);
397 }
398 }
399 }
400
Evan Chengc2b4ec32006-09-28 00:10:27 +0000401 // Then assign frame offsets to stack objects that are not used to spill
Evan Chengad93d7f2007-01-02 21:31:15 +0000402 // callee saved registers.
Chris Lattner58b33282002-12-28 20:43:30 +0000403 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
Evan Chengc2b4ec32006-09-28 00:10:27 +0000404 if (i >= MinCSFrameIndex && i <= MaxCSFrameIndex)
405 continue;
Evan Cheng87f8bf62007-03-06 10:02:38 +0000406 if (RS && (int)i == RS->getScavengingFrameIndex())
407 continue;
Evan Chengc2b4ec32006-09-28 00:10:27 +0000408
Chris Lattner577aec12004-06-10 06:23:35 +0000409 // If stack grows down, we need to add size of find the lowest
410 // address of the object.
411 if (StackGrowsDown)
412 Offset += FFI->getObjectSize(i);
Chris Lattner58b33282002-12-28 20:43:30 +0000413
414 unsigned Align = FFI->getObjectAlignment(i);
Nate Begemanae232e72005-11-06 09:00:38 +0000415 // If the alignment of this object is greater than that of the stack, then
416 // increase the stack alignment to match.
417 MaxAlign = std::max(MaxAlign, Align);
418 // Adjust to alignment boundary
419 Offset = (Offset+Align-1)/Align*Align;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000420
Chris Lattner577aec12004-06-10 06:23:35 +0000421 if (StackGrowsDown) {
422 FFI->setObjectOffset(i, -Offset); // Set the computed offset
423 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000424 FFI->setObjectOffset(i, Offset);
Chris Lattner577aec12004-06-10 06:23:35 +0000425 Offset += FFI->getObjectSize(i);
426 }
Chris Lattner58b33282002-12-28 20:43:30 +0000427 }
428
Evan Cheng87f8bf62007-03-06 10:02:38 +0000429 // Make sure the special register scavenging spill slot is closest to the
430 // stack pointer.
431 if (RS) {
432 int SFI = RS->getScavengingFrameIndex();
433 if (SFI >= 0) {
434 // If stack grows down, we need to add size of find the lowest
435 // address of the object.
436 if (StackGrowsDown)
437 Offset += FFI->getObjectSize(SFI);
438
439 unsigned Align = FFI->getObjectAlignment(SFI);
440 // Adjust to alignment boundary
441 Offset = (Offset+Align-1)/Align*Align;
442
443 if (StackGrowsDown) {
444 FFI->setObjectOffset(SFI, -Offset); // Set the computed offset
445 } else {
446 FFI->setObjectOffset(SFI, Offset);
447 Offset += FFI->getObjectSize(SFI);
448 }
449 }
450 }
451
Evan Cheng367372a2007-01-23 09:38:11 +0000452 // Round up the size to a multiple of the alignment, but only if there are
453 // calls or alloca's in the function. This ensures that any calls to
454 // subroutines have their stack frames suitable aligned.
Evan Cheng02a20292007-01-25 22:12:41 +0000455 if (!RegInfo->targetHandlesStackFrameRounding() &&
456 (FFI->hasCalls() || FFI->hasVarSizedObjects())) {
Evan Cheng5c3885c2007-05-01 00:52:08 +0000457 // If we have reserved argument space for call sites in the function
458 // immediately on entry to the current function, count it as part of the
459 // overall stack size.
460 if (RegInfo->hasReservedCallFrame(Fn))
Evan Cheng367372a2007-01-23 09:38:11 +0000461 Offset += FFI->getMaxCallFrameSize();
462
463 unsigned AlignMask = TFI.getStackAlignment() - 1;
Chris Lattnerea84c5e2007-04-25 04:30:24 +0000464 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
Evan Cheng367372a2007-01-23 09:38:11 +0000465 }
466
467 // Update frame info to pretend that this is part of the stack...
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000468 FFI->setStackSize(Offset+TFI.getOffsetOfLocalArea());
Chris Lattnercbef8ba2005-11-06 17:43:20 +0000469
470 // Remember the required stack alignment in case targets need it to perform
471 // dynamic stack alignment.
Chris Lattner4672f712006-04-03 21:39:57 +0000472 assert(FFI->getMaxAlignment() == MaxAlign &&
473 "Stack alignment calculation broken!");
Chris Lattner4ac7d732003-01-15 22:52:34 +0000474}
475
476
Evan Chengc2b4ec32006-09-28 00:10:27 +0000477/// insertPrologEpilogCode - Scan the function for modified callee saved
478/// registers, insert spill code for these callee saved registers, then add
Chris Lattner4ac7d732003-01-15 22:52:34 +0000479/// prolog and epilog code to the function.
480///
481void PEI::insertPrologEpilogCode(MachineFunction &Fn) {
482 // Add prologue to the function...
483 Fn.getTarget().getRegisterInfo()->emitPrologue(Fn);
484
485 // Add epilogue to restore the callee-save registers in each exiting block
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000486 const TargetInstrInfo &TII = *Fn.getTarget().getInstrInfo();
Chris Lattner4ac7d732003-01-15 22:52:34 +0000487 for (MachineFunction::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I) {
488 // If last instruction is a return instruction, add an epilogue
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000489 if (!I->empty() && TII.isReturn(I->back().getOpcode()))
Chris Lattner4ac7d732003-01-15 22:52:34 +0000490 Fn.getTarget().getRegisterInfo()->emitEpilogue(Fn, *I);
491 }
Chris Lattner58b33282002-12-28 20:43:30 +0000492}
493
494
495/// replaceFrameIndices - Replace all MO_FrameIndex operands with physical
496/// register references and actual offsets.
497///
498void PEI::replaceFrameIndices(MachineFunction &Fn) {
499 if (!Fn.getFrameInfo()->hasStackObjects()) return; // Nothing to do?
500
501 const TargetMachine &TM = Fn.getTarget();
502 assert(TM.getRegisterInfo() && "TM::getRegisterInfo() must be implemented!");
503 const MRegisterInfo &MRI = *TM.getRegisterInfo();
Evan Cheng8e334732007-05-01 09:01:42 +0000504 const TargetFrameInfo *TFI = TM.getFrameInfo();
505 bool StackGrowsDown =
506 TFI->getStackGrowthDirection() == TargetFrameInfo::StackGrowsDown;
507 int FrameSetupOpcode = MRI.getCallFrameSetupOpcode();
508 int FrameDestroyOpcode = MRI.getCallFrameDestroyOpcode();
Chris Lattner58b33282002-12-28 20:43:30 +0000509
Evan Cheng49dd0642007-02-23 01:11:26 +0000510 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
Evan Cheng8e334732007-05-01 09:01:42 +0000511 int SPAdj = 0; // SP offset due to call frame setup / destroy.
Evan Cheng23322d12007-03-01 02:25:51 +0000512 if (RS) RS->enterBasicBlock(BB);
Chris Lattner0ebe9c12007-04-09 01:19:33 +0000513 for (MachineBasicBlock::iterator I = BB->begin(); I != BB->end(); ) {
Evan Cheng8e334732007-05-01 09:01:42 +0000514 MachineInstr *MI = I;
Chris Lattnerd2eae622007-04-09 00:46:10 +0000515
Evan Cheng8e334732007-05-01 09:01:42 +0000516 // Remember how much SP has been adjustment to create the call frame.
517 if (I->getOpcode() == FrameSetupOpcode ||
518 I->getOpcode() == FrameDestroyOpcode) {
519 int Size = I->getOperand(0).getImmedValue();
520 if ((!StackGrowsDown && I->getOpcode() == FrameSetupOpcode) ||
521 (StackGrowsDown && I->getOpcode() == FrameDestroyOpcode))
522 Size = -Size;
523 SPAdj += Size;
524 MachineBasicBlock::iterator PrevI = prior(I);
525 MRI.eliminateCallFramePseudoInstr(Fn, *BB, I);
526 // Visit the instructions created by eliminateCallFramePseudoInstr().
527 I = next(PrevI);
528 MI = NULL;
529 } else {
530 I++;
531 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
532 if (MI->getOperand(i).isFrameIndex()) {
533 // If this instruction has a FrameIndex operand, we need to use that
534 // target machine register info object to eliminate it.
535 MRI.eliminateFrameIndex(MI, SPAdj, RS);
536
537 // Revisit the instruction in full. Some instructions (e.g. inline
538 // asm instructions) can have multiple frame indices.
539 --I;
540 MI = 0;
541 break;
542 }
543 }
Evan Cheng49dd0642007-02-23 01:11:26 +0000544 // Update register states.
Chris Lattner0ebe9c12007-04-09 01:19:33 +0000545 if (RS && MI) RS->forward(MI);
Evan Cheng49dd0642007-02-23 01:11:26 +0000546 }
Evan Cheng8e334732007-05-01 09:01:42 +0000547 assert(SPAdj == 0 && "Unbalanced call frame setup / destroy pairs?");
Evan Cheng49dd0642007-02-23 01:11:26 +0000548 }
Chris Lattner58b33282002-12-28 20:43:30 +0000549}