blob: 0c363dcd1686505806a15bc2cf5f656cd06b0f7d [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
129 MachineBasicBlock::iterator
Chris Lattner9f366d72002-12-15 22:19:19 +0000130 moveUseToReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
131 unsigned VirtReg, unsigned &PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000132
133 /// Saves reg value on the stack (maps virtual register to stack value)
134 MachineBasicBlock::iterator
Chris Lattner9f366d72002-12-15 22:19:19 +0000135 saveVirtRegToStack(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
136 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000137 };
138
Misha Brukman59b3eed2002-12-13 10:42:31 +0000139}
Misha Brukman07218672002-11-22 22:44:32 +0000140
Chris Lattner9f366d72002-12-15 22:19:19 +0000141/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000142/// register to be held on the stack.
Chris Lattner9f366d72002-12-15 22:19:19 +0000143unsigned RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
144 const TargetRegisterClass *regClass) {
145 // Find the location VirtReg would belong...
146 std::map<unsigned, unsigned>::iterator I =
147 VirtReg2OffsetMap.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +0000148
Chris Lattner9f366d72002-12-15 22:19:19 +0000149 if (I != VirtReg2OffsetMap.end() && I->first == VirtReg)
150 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +0000151
Chris Lattner9f366d72002-12-15 22:19:19 +0000152 unsigned RegSize = regClass->getDataSize();
Chris Lattner9593fb12002-12-15 19:07:34 +0000153
Chris Lattner9f366d72002-12-15 22:19:19 +0000154 // Align NumBytesAllocated. We should be using TargetData alignment stuff
155 // to determine this, but we don't know the LLVM type associated with the
156 // virtual register. Instead, just align to a multiple of the size for now.
157 NumBytesAllocated += RegSize-1;
158 NumBytesAllocated = NumBytesAllocated/RegSize*RegSize;
159
160 // Assign the slot...
161 VirtReg2OffsetMap.insert(I, std::make_pair(VirtReg, NumBytesAllocated));
162
163 // Reserve the space!
164 NumBytesAllocated += RegSize;
165 return NumBytesAllocated-RegSize;
Misha Brukmanf514d512002-12-02 21:11:58 +0000166}
167
Misha Brukman07218672002-11-22 22:44:32 +0000168unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
169 const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
Chris Lattner9f366d72002-12-15 22:19:19 +0000170
171 unsigned regIdx = RegClassIdx[regClass]++;
172 assert(regIdx < regClass->getNumRegs() && "Not enough registers!");
173 unsigned physReg = regClass->getRegister(regIdx);
Misha Brukman07218672002-11-22 22:44:32 +0000174
Chris Lattner9f366d72002-12-15 22:19:19 +0000175 if (RegsUsed.find(physReg) == RegsUsed.end())
Misha Brukman07218672002-11-22 22:44:32 +0000176 return physReg;
Chris Lattnerda7e4532002-12-15 20:36:09 +0000177 else
Misha Brukman07218672002-11-22 22:44:32 +0000178 return getFreeReg(virtualReg);
Misha Brukman07218672002-11-22 22:44:32 +0000179}
180
181MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000182RegAllocSimple::moveUseToReg (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000183 MachineBasicBlock::iterator I,
Misha Brukman07218672002-11-22 22:44:32 +0000184 unsigned VirtReg, unsigned &PhysReg)
185{
186 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
Chris Lattner9f366d72002-12-15 22:19:19 +0000187 unsigned stackOffset = getStackSpaceFor(VirtReg, regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000188 PhysReg = getFreeReg(VirtReg);
189
Misha Brukmanf514d512002-12-02 21:11:58 +0000190 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000191 ++NumReloaded;
Chris Lattner198ab642002-12-15 20:06:35 +0000192 return RegInfo->loadRegOffset2Reg(MBB, I, PhysReg,
Misha Brukmanf514d512002-12-02 21:11:58 +0000193 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000194 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000195}
196
197MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000198RegAllocSimple::saveVirtRegToStack (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000199 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000200 unsigned VirtReg, unsigned PhysReg)
Misha Brukman07218672002-11-22 22:44:32 +0000201{
202 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
Chris Lattner9f366d72002-12-15 22:19:19 +0000203 unsigned stackOffset = getStackSpaceFor(VirtReg, regClass);
Misha Brukmanf514d512002-12-02 21:11:58 +0000204
Misha Brukman07218672002-11-22 22:44:32 +0000205 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000206 ++NumSpilled;
Chris Lattner198ab642002-12-15 20:06:35 +0000207 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukman07218672002-11-22 22:44:32 +0000208 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000209 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000210}
211
Misha Brukmandc2ec002002-12-03 23:15:19 +0000212
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000213/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
214/// predecessor basic blocks.
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000215///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000216void RegAllocSimple::EliminatePHINodes(MachineBasicBlock &MBB) {
Chris Lattnerda7e4532002-12-15 20:36:09 +0000217 const MachineInstrInfo &MII = TM.getInstrInfo();
218
Chris Lattner9f366d72002-12-15 22:19:19 +0000219 while (MBB.front()->getOpcode() == MachineInstrInfo::PHI) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000220 MachineInstr *MI = MBB.front();
Chris Lattner9f366d72002-12-15 22:19:19 +0000221 // Unlink the PHI node from the basic block... but don't delete the PHI yet
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000222 MBB.erase(MBB.begin());
223
Chris Lattner9f366d72002-12-15 22:19:19 +0000224 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
225 assert(MI->getOperand(0).isVirtualRegister() &&
226 "PHI node doesn't write virt reg?");
227
Chris Lattner9f366d72002-12-15 22:19:19 +0000228 unsigned virtualReg = MI->getOperand(0).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000229
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000230 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
231 MachineOperand &opVal = MI->getOperand(i-1);
232
233 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
234 // source path the phi
235 MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000236
Chris Lattner3f91ad72002-12-15 20:48:03 +0000237 // Check to make sure we haven't already emitted the copy for this block.
238 // This can happen because PHI nodes may have multiple entries for the
239 // same basic block. It doesn't matter which entry we use though, because
240 // all incoming values are guaranteed to be the same for a particular bb.
241 //
242 // Note that this is N^2 in the number of phi node entries, but since the
243 // # of entries is tiny, this is not a problem.
244 //
245 bool HaveNotEmitted = true;
246 for (int op = MI->getNumOperands() - 1; op != i; op -= 2)
247 if (&opBlock == MI->getOperand(op).getMachineBasicBlock()) {
248 HaveNotEmitted = false;
249 break;
250 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000251
Chris Lattner3f91ad72002-12-15 20:48:03 +0000252 if (HaveNotEmitted) {
253 MachineBasicBlock::iterator opI = opBlock.end();
254 MachineInstr *opMI = *--opI;
255
256 // must backtrack over ALL the branches in the previous block
257 while (MII.isBranch(opMI->getOpcode()) && opI != opBlock.begin())
258 opMI = *--opI;
259
260 // move back to the first branch instruction so new instructions
261 // are inserted right in front of it and not in front of a non-branch
262 if (!MII.isBranch(opMI->getOpcode()))
263 ++opI;
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000264
265 unsigned dataSize = MF->getRegClass(virtualReg)->getDataSize();
266
Chris Lattner3f91ad72002-12-15 20:48:03 +0000267 // Retrieve the constant value from this op, move it to target
268 // register of the phi
269 if (opVal.isImmediate()) {
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000270 opI = RegInfo->moveImm2Reg(opBlock, opI, virtualReg,
Chris Lattner3f91ad72002-12-15 20:48:03 +0000271 (unsigned) opVal.getImmedValue(),
272 dataSize);
Chris Lattner3f91ad72002-12-15 20:48:03 +0000273 } else {
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000274 opI = RegInfo->moveReg2Reg(opBlock, opI, virtualReg,
275 opVal.getAllocatedRegNum(), dataSize);
Chris Lattner3f91ad72002-12-15 20:48:03 +0000276 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000277 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000278 }
279
Chris Lattner9f366d72002-12-15 22:19:19 +0000280 // really delete the PHI instruction now!
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000281 delete MI;
282 }
283}
284
285
286void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000287 // loop over each instruction
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000288 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000289 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000290 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000291
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000292 MachineInstr *MI = *I;
293
294 // a preliminary pass that will invalidate any registers that
295 // are used by the instruction (including implicit uses)
296 invalidatePhysRegs(MI);
297
298 // Loop over uses, move from memory into registers
299 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
300 MachineOperand &op = MI->getOperand(i);
301
Chris Lattnerda7e4532002-12-15 20:36:09 +0000302 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000303 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
304 DEBUG(std::cerr << "op: " << op << "\n");
305 DEBUG(std::cerr << "\t inst[" << i << "]: ";
306 MI->print(std::cerr, TM));
307
308 // make sure the same virtual register maps to the same physical
309 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000310 unsigned physReg = Virt2PhysRegMap[virtualReg];
311 if (physReg == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000312 if (op.opIsDef()) {
313 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
314 // must be same register number as the first operand
315 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000316 assert(MI->getOperand(1).isRegister() &&
317 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner9f366d72002-12-15 22:19:19 +0000318 MI->getOperand(1).opIsUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000319 "Two address instruction invalid!");
320
Chris Lattnerda7e4532002-12-15 20:36:09 +0000321 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000322 } else {
323 physReg = getFreeReg(virtualReg);
324 }
Chris Lattner9f366d72002-12-15 22:19:19 +0000325 I = --saveVirtRegToStack(MBB, ++I, virtualReg, physReg);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000326 } else {
327 I = moveUseToReg(MBB, I, virtualReg, physReg);
328 }
Chris Lattner9f366d72002-12-15 22:19:19 +0000329 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000330 }
331 MI->SetMachineOperandReg(i, physReg);
332 DEBUG(std::cerr << "virt: " << virtualReg <<
333 ", phys: " << op.getAllocatedRegNum() << "\n");
334 }
335 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000336 clearAllRegs();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000337 }
338}
339
Chris Lattnerda7e4532002-12-15 20:36:09 +0000340/// runOnMachineFunction - Register allocate the whole function
341///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000342bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000343 DEBUG(std::cerr << "Machine Function " << "\n");
344 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000345
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000346 // First pass: eliminate PHI instructions by inserting copies into predecessor
347 // blocks.
348 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
349 MBB != MBBe; ++MBB)
350 EliminatePHINodes(*MBB);
351
Chris Lattner9f366d72002-12-15 22:19:19 +0000352 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000353 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
354 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000355 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000356
Chris Lattner9f366d72002-12-15 22:19:19 +0000357 // Add prologue to the function...
Chris Lattner198ab642002-12-15 20:06:35 +0000358 RegInfo->emitPrologue(Fn, NumBytesAllocated);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000359
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000360 const MachineInstrInfo &MII = TM.getInstrInfo();
361
Chris Lattner9f366d72002-12-15 22:19:19 +0000362 // Add epilogue to restore the callee-save registers in each exiting block
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000363 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000364 MBB != MBBe; ++MBB) {
Chris Lattner9f366d72002-12-15 22:19:19 +0000365 // If last instruction is a return instruction, add an epilogue
366 if (MII.isReturn(MBB->back()->getOpcode()))
Chris Lattner198ab642002-12-15 20:06:35 +0000367 RegInfo->emitEpilogue(*MBB, NumBytesAllocated);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000368 }
Misha Brukman07218672002-11-22 22:44:32 +0000369
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000370 cleanupAfterFunction();
Chris Lattner9f366d72002-12-15 22:19:19 +0000371 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000372}
373
374Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) {
375 return new RegAllocSimple(TM);
376}