blob: b299ddd1bf9984988eed09a697157640d9a4778e [file] [log] [blame]
Misha Brukman07218672002-11-22 22:44:32 +00001//===-- RegAllocSimple.cpp - A simple generic register allocator --- ------===//
2//
3// This file implements a simple register allocator. *Very* simple.
4//
5//===----------------------------------------------------------------------===//
6
Misha Brukman07218672002-11-22 22:44:32 +00007#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +00008#include "llvm/CodeGen/MachineInstr.h"
Misha Brukmandd46e2a2002-12-04 23:58:08 +00009#include "llvm/Target/MachineInstrInfo.h"
Misha Brukman07218672002-11-22 22:44:32 +000010#include "llvm/Target/TargetMachine.h"
Misha Brukman07218672002-11-22 22:44:32 +000011#include "Support/Statistic.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000012#include <iostream>
Chris Lattnerda7e4532002-12-15 20:36:09 +000013#include <set>
Misha Brukman07218672002-11-22 22:44:32 +000014
Chris Lattner9f366d72002-12-15 22:19:19 +000015#if 0
Chris Lattnerdd444f92002-12-15 18:38:59 +000016/// PhysRegClassMap - Construct a mapping of physical register numbers to their
17/// register classes.
18///
19/// NOTE: This class will eventually be pulled out to somewhere shared.
20///
21class PhysRegClassMap {
22 std::map<unsigned, const TargetRegisterClass*> PhysReg2RegClassMap;
23public:
24 PhysRegClassMap(const MRegisterInfo *RI) {
25 for (MRegisterInfo::const_iterator I = RI->regclass_begin(),
26 E = RI->regclass_end(); I != E; ++I)
27 for (unsigned i=0; i < (*I)->getNumRegs(); ++i)
28 PhysReg2RegClassMap[(*I)->getRegister(i)] = *I;
29 }
30
31 const TargetRegisterClass *operator[](unsigned Reg) {
32 assert(PhysReg2RegClassMap[Reg] && "Register is not a known physreg!");
33 return PhysReg2RegClassMap[Reg];
34 }
35
36 const TargetRegisterClass *get(unsigned Reg) { return operator[](Reg); }
37};
Chris Lattner9f366d72002-12-15 22:19:19 +000038#endif
Chris Lattnerdd444f92002-12-15 18:38:59 +000039
40
Misha Brukman59b3eed2002-12-13 10:42:31 +000041namespace {
Chris Lattnerda7e4532002-12-15 20:36:09 +000042 Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
43 Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
44
45 class RegAllocSimple : public FunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000046 TargetMachine &TM;
Misha Brukman07218672002-11-22 22:44:32 +000047 MachineFunction *MF;
Misha Brukman07218672002-11-22 22:44:32 +000048 const MRegisterInfo *RegInfo;
Chris Lattner9593fb12002-12-15 19:07:34 +000049 unsigned NumBytesAllocated;
Misha Brukman07218672002-11-22 22:44:32 +000050
51 // Maps SSA Regs => offsets on the stack where these values are stored
Chris Lattnerad44bd92002-12-15 18:15:24 +000052 std::map<unsigned, unsigned> VirtReg2OffsetMap;
Misha Brukman07218672002-11-22 22:44:32 +000053
Chris Lattnerda7e4532002-12-15 20:36:09 +000054 // RegsUsed - Keep track of what registers are currently in use.
55 std::set<unsigned> RegsUsed;
56
57 // RegClassIdx - Maps RegClass => which index we can take a register
58 // from. Since this is a simple register allocator, when we need a register
59 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000060 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
61
Chris Lattnerda7e4532002-12-15 20:36:09 +000062 public:
63
Chris Lattnerc2db1a92002-12-15 19:51:14 +000064 RegAllocSimple(TargetMachine &tm)
Chris Lattner9f366d72002-12-15 22:19:19 +000065 : TM(tm), RegInfo(tm.getRegisterInfo()) {
Chris Lattnerda7e4532002-12-15 20:36:09 +000066 RegsUsed.insert(RegInfo->getFramePointer());
67 RegsUsed.insert(RegInfo->getStackPointer());
Misha Brukmancea22452002-12-13 04:34:02 +000068
69 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +000070 }
71
Chris Lattnerda7e4532002-12-15 20:36:09 +000072 bool runOnFunction(Function &Fn) {
73 return runOnMachineFunction(MachineFunction::get(&Fn));
74 }
75
Chris Lattner8233e2f2002-12-15 21:13:12 +000076 virtual const char *getPassName() const {
77 return "Simple Register Allocator";
78 }
79
Chris Lattnerda7e4532002-12-15 20:36:09 +000080 private:
81 /// runOnMachineFunction - Register allocate the whole function
82 bool runOnMachineFunction(MachineFunction &Fn);
83
84 /// AllocateBasicBlock - Register allocate the specified basic block.
85 void AllocateBasicBlock(MachineBasicBlock &MBB);
86
87 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
88 /// in predecessor basic blocks.
89 void EliminatePHINodes(MachineBasicBlock &MBB);
90
91
Chris Lattner9f366d72002-12-15 22:19:19 +000092 /// getStackSpaceFor - This returns the offset of the specified virtual
93 /// register on the stack, allocating space if neccesary.
94 unsigned getStackSpaceFor(unsigned VirtReg,
95 const TargetRegisterClass *regClass);
Misha Brukman07218672002-11-22 22:44:32 +000096
Chris Lattner9f366d72002-12-15 22:19:19 +000097 /// Given a virtual register, return a compatible physical register that is
98 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000099 ///
Misha Brukman07218672002-11-22 22:44:32 +0000100 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +0000101 ///
Misha Brukman07218672002-11-22 22:44:32 +0000102 unsigned getFreeReg(unsigned virtualReg);
103
104 /// Returns all `borrowed' registers back to the free pool
105 void clearAllRegs() {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000106 RegClassIdx.clear();
Misha Brukman07218672002-11-22 22:44:32 +0000107 }
108
Misha Brukman972b03f2002-12-13 11:33:22 +0000109 /// Invalidates any references, real or implicit, to physical registers
110 ///
111 void invalidatePhysRegs(const MachineInstr *MI) {
112 unsigned Opcode = MI->getOpcode();
Chris Lattnerda7e4532002-12-15 20:36:09 +0000113 const MachineInstrDescriptor &Desc = TM.getInstrInfo().get(Opcode);
Misha Brukman972b03f2002-12-13 11:33:22 +0000114 const unsigned *regs = Desc.ImplicitUses;
115 while (*regs)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000116 RegsUsed.insert(*regs++);
Misha Brukman972b03f2002-12-13 11:33:22 +0000117
118 regs = Desc.ImplicitDefs;
119 while (*regs)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000120 RegsUsed.insert(*regs++);
Misha Brukman972b03f2002-12-13 11:33:22 +0000121 }
122
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000123 void cleanupAfterFunction() {
Chris Lattnerad44bd92002-12-15 18:15:24 +0000124 VirtReg2OffsetMap.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000125 NumBytesAllocated = 4; // FIXME: This is X86 specific
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000126 }
127
Misha Brukman07218672002-11-22 22:44:32 +0000128 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +0000129 unsigned reloadVirtReg(MachineBasicBlock &MBB,
130 MachineBasicBlock::iterator &I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000131
132 /// Saves reg value on the stack (maps virtual register to stack value)
Chris Lattnerb167c042002-12-15 23:01:26 +0000133 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
134 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000135 };
136
Misha Brukman59b3eed2002-12-13 10:42:31 +0000137}
Misha Brukman07218672002-11-22 22:44:32 +0000138
Chris Lattner9f366d72002-12-15 22:19:19 +0000139/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000140/// register to be held on the stack.
Chris Lattner9f366d72002-12-15 22:19:19 +0000141unsigned RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
142 const TargetRegisterClass *regClass) {
143 // Find the location VirtReg would belong...
144 std::map<unsigned, unsigned>::iterator I =
145 VirtReg2OffsetMap.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +0000146
Chris Lattner9f366d72002-12-15 22:19:19 +0000147 if (I != VirtReg2OffsetMap.end() && I->first == VirtReg)
148 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +0000149
Chris Lattner9f366d72002-12-15 22:19:19 +0000150 unsigned RegSize = regClass->getDataSize();
Chris Lattner9593fb12002-12-15 19:07:34 +0000151
Chris Lattner9f366d72002-12-15 22:19:19 +0000152 // Align NumBytesAllocated. We should be using TargetData alignment stuff
153 // to determine this, but we don't know the LLVM type associated with the
154 // virtual register. Instead, just align to a multiple of the size for now.
155 NumBytesAllocated += RegSize-1;
156 NumBytesAllocated = NumBytesAllocated/RegSize*RegSize;
157
158 // Assign the slot...
159 VirtReg2OffsetMap.insert(I, std::make_pair(VirtReg, NumBytesAllocated));
160
161 // Reserve the space!
162 NumBytesAllocated += RegSize;
163 return NumBytesAllocated-RegSize;
Misha Brukmanf514d512002-12-02 21:11:58 +0000164}
165
Misha Brukman07218672002-11-22 22:44:32 +0000166unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
167 const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
Chris Lattner9f366d72002-12-15 22:19:19 +0000168
169 unsigned regIdx = RegClassIdx[regClass]++;
170 assert(regIdx < regClass->getNumRegs() && "Not enough registers!");
171 unsigned physReg = regClass->getRegister(regIdx);
Misha Brukman07218672002-11-22 22:44:32 +0000172
Chris Lattner9f366d72002-12-15 22:19:19 +0000173 if (RegsUsed.find(physReg) == RegsUsed.end())
Misha Brukman07218672002-11-22 22:44:32 +0000174 return physReg;
Chris Lattnerda7e4532002-12-15 20:36:09 +0000175 else
Misha Brukman07218672002-11-22 22:44:32 +0000176 return getFreeReg(virtualReg);
Misha Brukman07218672002-11-22 22:44:32 +0000177}
178
Chris Lattnerb167c042002-12-15 23:01:26 +0000179unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
180 MachineBasicBlock::iterator &I,
181 unsigned VirtReg) {
Misha Brukman07218672002-11-22 22:44:32 +0000182 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
Chris Lattner9f366d72002-12-15 22:19:19 +0000183 unsigned stackOffset = getStackSpaceFor(VirtReg, regClass);
Chris Lattnerb167c042002-12-15 23:01:26 +0000184 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000185
Misha Brukmanf514d512002-12-02 21:11:58 +0000186 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000187 ++NumReloaded;
Chris Lattnerb167c042002-12-15 23:01:26 +0000188 I = RegInfo->loadRegOffset2Reg(MBB, I, PhysReg, RegInfo->getFramePointer(),
189 -stackOffset, regClass->getDataSize());
190 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000191}
192
Chris Lattnerb167c042002-12-15 23:01:26 +0000193void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
194 MachineBasicBlock::iterator &I,
195 unsigned VirtReg, unsigned PhysReg)
Misha Brukman07218672002-11-22 22:44:32 +0000196{
197 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
Chris Lattner9f366d72002-12-15 22:19:19 +0000198 unsigned stackOffset = getStackSpaceFor(VirtReg, regClass);
Misha Brukmanf514d512002-12-02 21:11:58 +0000199
Misha Brukman07218672002-11-22 22:44:32 +0000200 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000201 ++NumSpilled;
Chris Lattnerb167c042002-12-15 23:01:26 +0000202 I = RegInfo->storeReg2RegOffset(MBB, I, PhysReg, RegInfo->getFramePointer(),
203 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000204}
205
Misha Brukmandc2ec002002-12-03 23:15:19 +0000206
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000207/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
208/// predecessor basic blocks.
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000209///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000210void RegAllocSimple::EliminatePHINodes(MachineBasicBlock &MBB) {
Chris Lattnerda7e4532002-12-15 20:36:09 +0000211 const MachineInstrInfo &MII = TM.getInstrInfo();
212
Chris Lattner9f366d72002-12-15 22:19:19 +0000213 while (MBB.front()->getOpcode() == MachineInstrInfo::PHI) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000214 MachineInstr *MI = MBB.front();
Chris Lattner9f366d72002-12-15 22:19:19 +0000215 // Unlink the PHI node from the basic block... but don't delete the PHI yet
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000216 MBB.erase(MBB.begin());
217
Chris Lattner9f366d72002-12-15 22:19:19 +0000218 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
219 assert(MI->getOperand(0).isVirtualRegister() &&
220 "PHI node doesn't write virt reg?");
221
Chris Lattner9f366d72002-12-15 22:19:19 +0000222 unsigned virtualReg = MI->getOperand(0).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000223
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000224 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
225 MachineOperand &opVal = MI->getOperand(i-1);
226
227 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
228 // source path the phi
229 MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000230
Chris Lattner3f91ad72002-12-15 20:48:03 +0000231 // Check to make sure we haven't already emitted the copy for this block.
232 // This can happen because PHI nodes may have multiple entries for the
233 // same basic block. It doesn't matter which entry we use though, because
234 // all incoming values are guaranteed to be the same for a particular bb.
235 //
236 // Note that this is N^2 in the number of phi node entries, but since the
237 // # of entries is tiny, this is not a problem.
238 //
239 bool HaveNotEmitted = true;
240 for (int op = MI->getNumOperands() - 1; op != i; op -= 2)
241 if (&opBlock == MI->getOperand(op).getMachineBasicBlock()) {
242 HaveNotEmitted = false;
243 break;
244 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000245
Chris Lattner3f91ad72002-12-15 20:48:03 +0000246 if (HaveNotEmitted) {
247 MachineBasicBlock::iterator opI = opBlock.end();
248 MachineInstr *opMI = *--opI;
249
250 // must backtrack over ALL the branches in the previous block
251 while (MII.isBranch(opMI->getOpcode()) && opI != opBlock.begin())
252 opMI = *--opI;
253
254 // move back to the first branch instruction so new instructions
255 // are inserted right in front of it and not in front of a non-branch
256 if (!MII.isBranch(opMI->getOpcode()))
257 ++opI;
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000258
259 unsigned dataSize = MF->getRegClass(virtualReg)->getDataSize();
260
Chris Lattner3f91ad72002-12-15 20:48:03 +0000261 // Retrieve the constant value from this op, move it to target
262 // register of the phi
263 if (opVal.isImmediate()) {
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000264 opI = RegInfo->moveImm2Reg(opBlock, opI, virtualReg,
Chris Lattner3f91ad72002-12-15 20:48:03 +0000265 (unsigned) opVal.getImmedValue(),
266 dataSize);
Chris Lattner3f91ad72002-12-15 20:48:03 +0000267 } else {
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000268 opI = RegInfo->moveReg2Reg(opBlock, opI, virtualReg,
269 opVal.getAllocatedRegNum(), dataSize);
Chris Lattner3f91ad72002-12-15 20:48:03 +0000270 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000271 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000272 }
273
Chris Lattner9f366d72002-12-15 22:19:19 +0000274 // really delete the PHI instruction now!
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000275 delete MI;
276 }
277}
278
279
280void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000281 // loop over each instruction
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000282 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000283 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000284 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000285
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000286 MachineInstr *MI = *I;
287
288 // a preliminary pass that will invalidate any registers that
289 // are used by the instruction (including implicit uses)
290 invalidatePhysRegs(MI);
291
292 // Loop over uses, move from memory into registers
293 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
294 MachineOperand &op = MI->getOperand(i);
295
Chris Lattnerda7e4532002-12-15 20:36:09 +0000296 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000297 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
298 DEBUG(std::cerr << "op: " << op << "\n");
299 DEBUG(std::cerr << "\t inst[" << i << "]: ";
300 MI->print(std::cerr, TM));
301
302 // make sure the same virtual register maps to the same physical
303 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000304 unsigned physReg = Virt2PhysRegMap[virtualReg];
305 if (physReg == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000306 if (op.opIsDef()) {
307 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
308 // must be same register number as the first operand
309 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000310 assert(MI->getOperand(1).isRegister() &&
311 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner9f366d72002-12-15 22:19:19 +0000312 MI->getOperand(1).opIsUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000313 "Two address instruction invalid!");
314
Chris Lattnerda7e4532002-12-15 20:36:09 +0000315 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000316 } else {
317 physReg = getFreeReg(virtualReg);
318 }
Chris Lattnerb167c042002-12-15 23:01:26 +0000319 ++I;
320 spillVirtReg(MBB, I, virtualReg, physReg);
321 --I;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000322 } else {
Chris Lattnerb167c042002-12-15 23:01:26 +0000323 physReg = reloadVirtReg(MBB, I, virtualReg);
324 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000325 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000326 }
327 MI->SetMachineOperandReg(i, physReg);
328 DEBUG(std::cerr << "virt: " << virtualReg <<
329 ", phys: " << op.getAllocatedRegNum() << "\n");
330 }
331 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000332 clearAllRegs();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000333 }
334}
335
Chris Lattnerda7e4532002-12-15 20:36:09 +0000336/// runOnMachineFunction - Register allocate the whole function
337///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000338bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000339 DEBUG(std::cerr << "Machine Function " << "\n");
340 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000341
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000342 // First pass: eliminate PHI instructions by inserting copies into predecessor
343 // blocks.
344 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
345 MBB != MBBe; ++MBB)
346 EliminatePHINodes(*MBB);
347
Chris Lattner9f366d72002-12-15 22:19:19 +0000348 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000349 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
350 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000351 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000352
Chris Lattner9f366d72002-12-15 22:19:19 +0000353 // Add prologue to the function...
Chris Lattner198ab642002-12-15 20:06:35 +0000354 RegInfo->emitPrologue(Fn, NumBytesAllocated);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000355
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000356 const MachineInstrInfo &MII = TM.getInstrInfo();
357
Chris Lattner9f366d72002-12-15 22:19:19 +0000358 // Add epilogue to restore the callee-save registers in each exiting block
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000359 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000360 MBB != MBBe; ++MBB) {
Chris Lattner9f366d72002-12-15 22:19:19 +0000361 // If last instruction is a return instruction, add an epilogue
362 if (MII.isReturn(MBB->back()->getOpcode()))
Chris Lattner198ab642002-12-15 20:06:35 +0000363 RegInfo->emitEpilogue(*MBB, NumBytesAllocated);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000364 }
Misha Brukman07218672002-11-22 22:44:32 +0000365
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000366 cleanupAfterFunction();
Chris Lattner9f366d72002-12-15 22:19:19 +0000367 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000368}
369
370Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) {
371 return new RegAllocSimple(TM);
372}