blob: 61109ad1eab8543c05734e4a1dd3d2f192174214 [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.
215void RegAllocSimple::EliminatePHINodes(MachineBasicBlock &MBB) {
Chris Lattnerda7e4532002-12-15 20:36:09 +0000216 const MachineInstrInfo &MII = TM.getInstrInfo();
217
Chris Lattner9f366d72002-12-15 22:19:19 +0000218 while (MBB.front()->getOpcode() == MachineInstrInfo::PHI) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000219 MachineInstr *MI = MBB.front();
Chris Lattner9f366d72002-12-15 22:19:19 +0000220 // Unlink the PHI node from the basic block... but don't delete the PHI yet
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000221 MBB.erase(MBB.begin());
222
Chris Lattner9f366d72002-12-15 22:19:19 +0000223 DEBUG(std::cerr << "num invalid regs: " << RegsUsed.size() << "\n");
224 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
225 assert(MI->getOperand(0).isVirtualRegister() &&
226 "PHI node doesn't write virt reg?");
227
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000228 // a preliminary pass that will invalidate any registers that
229 // are used by the instruction (including implicit uses)
230 invalidatePhysRegs(MI);
231
Chris Lattner9f366d72002-12-15 22:19:19 +0000232 // Allocate a physical reg to hold this temporary.
Chris Lattnerda7e4532002-12-15 20:36:09 +0000233 //
Chris Lattner9f366d72002-12-15 22:19:19 +0000234 unsigned virtualReg = MI->getOperand(0).getAllocatedRegNum();
235 unsigned physReg = getFreeReg(virtualReg);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000236
237 // Find the register class of the target register: should be the
238 // same as the values we're trying to store there
Chris Lattner9f366d72002-12-15 22:19:19 +0000239 const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000240 assert(regClass && "Target register class not found!");
241 unsigned dataSize = regClass->getDataSize();
Chris Lattner3f91ad72002-12-15 20:48:03 +0000242
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000243 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
244 MachineOperand &opVal = MI->getOperand(i-1);
245
246 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
247 // source path the phi
248 MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000249
Chris Lattner3f91ad72002-12-15 20:48:03 +0000250 // Check to make sure we haven't already emitted the copy for this block.
251 // This can happen because PHI nodes may have multiple entries for the
252 // same basic block. It doesn't matter which entry we use though, because
253 // all incoming values are guaranteed to be the same for a particular bb.
254 //
255 // Note that this is N^2 in the number of phi node entries, but since the
256 // # of entries is tiny, this is not a problem.
257 //
258 bool HaveNotEmitted = true;
259 for (int op = MI->getNumOperands() - 1; op != i; op -= 2)
260 if (&opBlock == MI->getOperand(op).getMachineBasicBlock()) {
261 HaveNotEmitted = false;
262 break;
263 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000264
Chris Lattner3f91ad72002-12-15 20:48:03 +0000265 if (HaveNotEmitted) {
266 MachineBasicBlock::iterator opI = opBlock.end();
267 MachineInstr *opMI = *--opI;
268
269 // must backtrack over ALL the branches in the previous block
270 while (MII.isBranch(opMI->getOpcode()) && opI != opBlock.begin())
271 opMI = *--opI;
272
273 // move back to the first branch instruction so new instructions
274 // are inserted right in front of it and not in front of a non-branch
275 if (!MII.isBranch(opMI->getOpcode()))
276 ++opI;
277
278 // Retrieve the constant value from this op, move it to target
279 // register of the phi
280 if (opVal.isImmediate()) {
281 opI = RegInfo->moveImm2Reg(opBlock, opI, physReg,
282 (unsigned) opVal.getImmedValue(),
283 dataSize);
Chris Lattner3f91ad72002-12-15 20:48:03 +0000284 } else {
285 // Allocate a physical register and add a move in the BB
Chris Lattner8233e2f2002-12-15 21:13:12 +0000286 unsigned opVirtualReg = opVal.getAllocatedRegNum();
Chris Lattner3f91ad72002-12-15 20:48:03 +0000287 unsigned opPhysReg;
288 opI = moveUseToReg(opBlock, opI, opVirtualReg, physReg);
289
Chris Lattner3f91ad72002-12-15 20:48:03 +0000290 }
Chris Lattnerf6050552002-12-15 21:33:51 +0000291
292 // Save that register value to the stack of the TARGET REG
293 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000294 }
Chris Lattner3f91ad72002-12-15 20:48:03 +0000295
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000296 // make regs available to other instructions
297 clearAllRegs();
298 }
299
Chris Lattner9f366d72002-12-15 22:19:19 +0000300 // really delete the PHI instruction now!
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000301 delete MI;
302 }
303}
304
305
306void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
307 // Handle PHI instructions specially: add moves to each pred block
308 EliminatePHINodes(MBB);
309
Chris Lattnerf6050552002-12-15 21:33:51 +0000310 // loop over each instruction
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000311 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000312 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000313 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000314
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000315 MachineInstr *MI = *I;
316
317 // a preliminary pass that will invalidate any registers that
318 // are used by the instruction (including implicit uses)
319 invalidatePhysRegs(MI);
320
321 // Loop over uses, move from memory into registers
322 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
323 MachineOperand &op = MI->getOperand(i);
324
Chris Lattnerda7e4532002-12-15 20:36:09 +0000325 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000326 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
327 DEBUG(std::cerr << "op: " << op << "\n");
328 DEBUG(std::cerr << "\t inst[" << i << "]: ";
329 MI->print(std::cerr, TM));
330
331 // make sure the same virtual register maps to the same physical
332 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000333 unsigned physReg = Virt2PhysRegMap[virtualReg];
334 if (physReg == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000335 if (op.opIsDef()) {
336 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
337 // must be same register number as the first operand
338 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000339 assert(MI->getOperand(1).isRegister() &&
340 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner9f366d72002-12-15 22:19:19 +0000341 MI->getOperand(1).opIsUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000342 "Two address instruction invalid!");
343
Chris Lattnerda7e4532002-12-15 20:36:09 +0000344 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000345 } else {
346 physReg = getFreeReg(virtualReg);
347 }
Chris Lattner9f366d72002-12-15 22:19:19 +0000348 I = --saveVirtRegToStack(MBB, ++I, virtualReg, physReg);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000349 } else {
350 I = moveUseToReg(MBB, I, virtualReg, physReg);
351 }
Chris Lattner9f366d72002-12-15 22:19:19 +0000352 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000353 }
354 MI->SetMachineOperandReg(i, physReg);
355 DEBUG(std::cerr << "virt: " << virtualReg <<
356 ", phys: " << op.getAllocatedRegNum() << "\n");
357 }
358 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000359 clearAllRegs();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000360 }
361}
362
Chris Lattnerda7e4532002-12-15 20:36:09 +0000363/// runOnMachineFunction - Register allocate the whole function
364///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000365bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000366 DEBUG(std::cerr << "Machine Function " << "\n");
367 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000368
Chris Lattner9f366d72002-12-15 22:19:19 +0000369 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000370 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
371 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000372 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000373
Chris Lattner9f366d72002-12-15 22:19:19 +0000374 // Add prologue to the function...
Chris Lattner198ab642002-12-15 20:06:35 +0000375 RegInfo->emitPrologue(Fn, NumBytesAllocated);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000376
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000377 const MachineInstrInfo &MII = TM.getInstrInfo();
378
Chris Lattner9f366d72002-12-15 22:19:19 +0000379 // Add epilogue to restore the callee-save registers in each exiting block
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000380 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000381 MBB != MBBe; ++MBB) {
Chris Lattner9f366d72002-12-15 22:19:19 +0000382 // If last instruction is a return instruction, add an epilogue
383 if (MII.isReturn(MBB->back()->getOpcode()))
Chris Lattner198ab642002-12-15 20:06:35 +0000384 RegInfo->emitEpilogue(*MBB, NumBytesAllocated);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000385 }
Misha Brukman07218672002-11-22 22:44:32 +0000386
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000387 cleanupAfterFunction();
Chris Lattner9f366d72002-12-15 22:19:19 +0000388 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000389}
390
391Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) {
392 return new RegAllocSimple(TM);
393}