blob: ffb6e97b19d1e06265e59ed9bf81717ec460340e [file] [log] [blame]
Chris Lattner1d62cea2002-12-16 14:37:00 +00001//===-- RegAllocSimple.cpp - A simple generic register allocator ----------===//
Misha Brukman07218672002-11-22 22:44:32 +00002//
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
Misha Brukman59b3eed2002-12-13 10:42:31 +000015namespace {
Chris Lattnerda7e4532002-12-15 20:36:09 +000016 Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
17 Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
18
19 class RegAllocSimple : public FunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000020 TargetMachine &TM;
Misha Brukman07218672002-11-22 22:44:32 +000021 MachineFunction *MF;
Misha Brukman07218672002-11-22 22:44:32 +000022 const MRegisterInfo *RegInfo;
Chris Lattner9593fb12002-12-15 19:07:34 +000023 unsigned NumBytesAllocated;
Misha Brukman07218672002-11-22 22:44:32 +000024
25 // Maps SSA Regs => offsets on the stack where these values are stored
Chris Lattnerad44bd92002-12-15 18:15:24 +000026 std::map<unsigned, unsigned> VirtReg2OffsetMap;
Misha Brukman07218672002-11-22 22:44:32 +000027
Chris Lattnerda7e4532002-12-15 20:36:09 +000028 // RegsUsed - Keep track of what registers are currently in use.
29 std::set<unsigned> RegsUsed;
30
31 // RegClassIdx - Maps RegClass => which index we can take a register
32 // from. Since this is a simple register allocator, when we need a register
33 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000034 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
35
Chris Lattnerda7e4532002-12-15 20:36:09 +000036 public:
37
Chris Lattnerc2db1a92002-12-15 19:51:14 +000038 RegAllocSimple(TargetMachine &tm)
Chris Lattner9f366d72002-12-15 22:19:19 +000039 : TM(tm), RegInfo(tm.getRegisterInfo()) {
Chris Lattnerda7e4532002-12-15 20:36:09 +000040 RegsUsed.insert(RegInfo->getFramePointer());
41 RegsUsed.insert(RegInfo->getStackPointer());
Misha Brukmancea22452002-12-13 04:34:02 +000042
43 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +000044 }
45
Chris Lattnerda7e4532002-12-15 20:36:09 +000046 bool runOnFunction(Function &Fn) {
47 return runOnMachineFunction(MachineFunction::get(&Fn));
48 }
49
Chris Lattner8233e2f2002-12-15 21:13:12 +000050 virtual const char *getPassName() const {
51 return "Simple Register Allocator";
52 }
53
Chris Lattnerda7e4532002-12-15 20:36:09 +000054 private:
55 /// runOnMachineFunction - Register allocate the whole function
56 bool runOnMachineFunction(MachineFunction &Fn);
57
58 /// AllocateBasicBlock - Register allocate the specified basic block.
59 void AllocateBasicBlock(MachineBasicBlock &MBB);
60
61 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
62 /// in predecessor basic blocks.
63 void EliminatePHINodes(MachineBasicBlock &MBB);
64
Chris Lattnere7d361d2002-12-17 04:19:40 +000065 /// EmitPrologue/EmitEpilogue - Use the register info object to add a
66 /// prologue/epilogue to the function and save/restore any callee saved
67 /// registers we are responsible for.
68 ///
69 void EmitPrologue();
70 void EmitEpilogue(MachineBasicBlock &MBB);
71
Chris Lattnerda7e4532002-12-15 20:36:09 +000072
Chris Lattner9f366d72002-12-15 22:19:19 +000073 /// getStackSpaceFor - This returns the offset of the specified virtual
74 /// register on the stack, allocating space if neccesary.
75 unsigned getStackSpaceFor(unsigned VirtReg,
76 const TargetRegisterClass *regClass);
Misha Brukman07218672002-11-22 22:44:32 +000077
Chris Lattner9f366d72002-12-15 22:19:19 +000078 /// Given a virtual register, return a compatible physical register that is
79 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000080 ///
Misha Brukman07218672002-11-22 22:44:32 +000081 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000082 ///
Misha Brukman07218672002-11-22 22:44:32 +000083 unsigned getFreeReg(unsigned virtualReg);
84
85 /// Returns all `borrowed' registers back to the free pool
86 void clearAllRegs() {
Chris Lattnerc2db1a92002-12-15 19:51:14 +000087 RegClassIdx.clear();
Misha Brukman07218672002-11-22 22:44:32 +000088 }
89
Misha Brukman972b03f2002-12-13 11:33:22 +000090 /// Invalidates any references, real or implicit, to physical registers
91 ///
92 void invalidatePhysRegs(const MachineInstr *MI) {
93 unsigned Opcode = MI->getOpcode();
Chris Lattnerda7e4532002-12-15 20:36:09 +000094 const MachineInstrDescriptor &Desc = TM.getInstrInfo().get(Opcode);
Chris Lattneraed967c2002-12-18 01:11:14 +000095 if (const unsigned *regs = Desc.ImplicitUses)
96 while (*regs)
97 RegsUsed.insert(*regs++);
Misha Brukman972b03f2002-12-13 11:33:22 +000098
Chris Lattneraed967c2002-12-18 01:11:14 +000099 if (const unsigned *regs = Desc.ImplicitDefs)
100 while (*regs)
101 RegsUsed.insert(*regs++);
Misha Brukman972b03f2002-12-13 11:33:22 +0000102 }
103
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000104 void cleanupAfterFunction() {
Chris Lattnerad44bd92002-12-15 18:15:24 +0000105 VirtReg2OffsetMap.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000106 NumBytesAllocated = 4; // FIXME: This is X86 specific
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000107 }
108
Misha Brukman07218672002-11-22 22:44:32 +0000109 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +0000110 unsigned reloadVirtReg(MachineBasicBlock &MBB,
111 MachineBasicBlock::iterator &I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000112
113 /// Saves reg value on the stack (maps virtual register to stack value)
Chris Lattnerb167c042002-12-15 23:01:26 +0000114 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
115 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000116 };
117
Misha Brukman59b3eed2002-12-13 10:42:31 +0000118}
Misha Brukman07218672002-11-22 22:44:32 +0000119
Chris Lattner9f366d72002-12-15 22:19:19 +0000120/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000121/// register to be held on the stack.
Chris Lattner9f366d72002-12-15 22:19:19 +0000122unsigned RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
123 const TargetRegisterClass *regClass) {
124 // Find the location VirtReg would belong...
125 std::map<unsigned, unsigned>::iterator I =
126 VirtReg2OffsetMap.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +0000127
Chris Lattner9f366d72002-12-15 22:19:19 +0000128 if (I != VirtReg2OffsetMap.end() && I->first == VirtReg)
129 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +0000130
Chris Lattner9f366d72002-12-15 22:19:19 +0000131 unsigned RegSize = regClass->getDataSize();
Chris Lattner9593fb12002-12-15 19:07:34 +0000132
Chris Lattner9f366d72002-12-15 22:19:19 +0000133 // Align NumBytesAllocated. We should be using TargetData alignment stuff
134 // to determine this, but we don't know the LLVM type associated with the
135 // virtual register. Instead, just align to a multiple of the size for now.
136 NumBytesAllocated += RegSize-1;
137 NumBytesAllocated = NumBytesAllocated/RegSize*RegSize;
138
139 // Assign the slot...
140 VirtReg2OffsetMap.insert(I, std::make_pair(VirtReg, NumBytesAllocated));
141
142 // Reserve the space!
143 NumBytesAllocated += RegSize;
144 return NumBytesAllocated-RegSize;
Misha Brukmanf514d512002-12-02 21:11:58 +0000145}
146
Misha Brukman07218672002-11-22 22:44:32 +0000147unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
148 const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
Chris Lattner9f366d72002-12-15 22:19:19 +0000149
150 unsigned regIdx = RegClassIdx[regClass]++;
151 assert(regIdx < regClass->getNumRegs() && "Not enough registers!");
152 unsigned physReg = regClass->getRegister(regIdx);
Misha Brukman07218672002-11-22 22:44:32 +0000153
Chris Lattner9f366d72002-12-15 22:19:19 +0000154 if (RegsUsed.find(physReg) == RegsUsed.end())
Misha Brukman07218672002-11-22 22:44:32 +0000155 return physReg;
Chris Lattnerda7e4532002-12-15 20:36:09 +0000156 else
Misha Brukman07218672002-11-22 22:44:32 +0000157 return getFreeReg(virtualReg);
Misha Brukman07218672002-11-22 22:44:32 +0000158}
159
Chris Lattnerb167c042002-12-15 23:01:26 +0000160unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
161 MachineBasicBlock::iterator &I,
162 unsigned VirtReg) {
Misha Brukman07218672002-11-22 22:44:32 +0000163 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
Chris Lattner9f366d72002-12-15 22:19:19 +0000164 unsigned stackOffset = getStackSpaceFor(VirtReg, regClass);
Chris Lattnerb167c042002-12-15 23:01:26 +0000165 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000166
Misha Brukmanf514d512002-12-02 21:11:58 +0000167 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000168 ++NumReloaded;
Chris Lattnerb167c042002-12-15 23:01:26 +0000169 I = RegInfo->loadRegOffset2Reg(MBB, I, PhysReg, RegInfo->getFramePointer(),
170 -stackOffset, regClass->getDataSize());
171 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000172}
173
Chris Lattnerb167c042002-12-15 23:01:26 +0000174void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
175 MachineBasicBlock::iterator &I,
176 unsigned VirtReg, unsigned PhysReg)
Misha Brukman07218672002-11-22 22:44:32 +0000177{
178 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
Chris Lattner9f366d72002-12-15 22:19:19 +0000179 unsigned stackOffset = getStackSpaceFor(VirtReg, regClass);
Misha Brukmanf514d512002-12-02 21:11:58 +0000180
Misha Brukman07218672002-11-22 22:44:32 +0000181 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000182 ++NumSpilled;
Chris Lattnerb167c042002-12-15 23:01:26 +0000183 I = RegInfo->storeReg2RegOffset(MBB, I, PhysReg, RegInfo->getFramePointer(),
184 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000185}
186
Misha Brukmandc2ec002002-12-03 23:15:19 +0000187
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000188/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
189/// predecessor basic blocks.
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000190///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000191void RegAllocSimple::EliminatePHINodes(MachineBasicBlock &MBB) {
Chris Lattnerda7e4532002-12-15 20:36:09 +0000192 const MachineInstrInfo &MII = TM.getInstrInfo();
193
Chris Lattner9f366d72002-12-15 22:19:19 +0000194 while (MBB.front()->getOpcode() == MachineInstrInfo::PHI) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000195 MachineInstr *MI = MBB.front();
Chris Lattner9f366d72002-12-15 22:19:19 +0000196 // Unlink the PHI node from the basic block... but don't delete the PHI yet
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000197 MBB.erase(MBB.begin());
198
Chris Lattner9f366d72002-12-15 22:19:19 +0000199 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
200 assert(MI->getOperand(0).isVirtualRegister() &&
201 "PHI node doesn't write virt reg?");
202
Chris Lattner9f366d72002-12-15 22:19:19 +0000203 unsigned virtualReg = MI->getOperand(0).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000204
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000205 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
206 MachineOperand &opVal = MI->getOperand(i-1);
207
208 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
209 // source path the phi
210 MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000211
Chris Lattner3f91ad72002-12-15 20:48:03 +0000212 // Check to make sure we haven't already emitted the copy for this block.
213 // This can happen because PHI nodes may have multiple entries for the
214 // same basic block. It doesn't matter which entry we use though, because
215 // all incoming values are guaranteed to be the same for a particular bb.
216 //
217 // Note that this is N^2 in the number of phi node entries, but since the
218 // # of entries is tiny, this is not a problem.
219 //
220 bool HaveNotEmitted = true;
221 for (int op = MI->getNumOperands() - 1; op != i; op -= 2)
222 if (&opBlock == MI->getOperand(op).getMachineBasicBlock()) {
223 HaveNotEmitted = false;
224 break;
225 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000226
Chris Lattner3f91ad72002-12-15 20:48:03 +0000227 if (HaveNotEmitted) {
228 MachineBasicBlock::iterator opI = opBlock.end();
229 MachineInstr *opMI = *--opI;
230
231 // must backtrack over ALL the branches in the previous block
232 while (MII.isBranch(opMI->getOpcode()) && opI != opBlock.begin())
233 opMI = *--opI;
234
235 // move back to the first branch instruction so new instructions
236 // are inserted right in front of it and not in front of a non-branch
237 if (!MII.isBranch(opMI->getOpcode()))
238 ++opI;
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000239
240 unsigned dataSize = MF->getRegClass(virtualReg)->getDataSize();
241
Chris Lattner3f91ad72002-12-15 20:48:03 +0000242 // Retrieve the constant value from this op, move it to target
243 // register of the phi
244 if (opVal.isImmediate()) {
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000245 opI = RegInfo->moveImm2Reg(opBlock, opI, virtualReg,
Chris Lattner3f91ad72002-12-15 20:48:03 +0000246 (unsigned) opVal.getImmedValue(),
247 dataSize);
Chris Lattner3f91ad72002-12-15 20:48:03 +0000248 } else {
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000249 opI = RegInfo->moveReg2Reg(opBlock, opI, virtualReg,
250 opVal.getAllocatedRegNum(), dataSize);
Chris Lattner3f91ad72002-12-15 20:48:03 +0000251 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000252 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000253 }
254
Chris Lattner9f366d72002-12-15 22:19:19 +0000255 // really delete the PHI instruction now!
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000256 delete MI;
257 }
258}
259
260
261void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000262 // loop over each instruction
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000263 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000264 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000265 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000266
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000267 MachineInstr *MI = *I;
268
269 // a preliminary pass that will invalidate any registers that
270 // are used by the instruction (including implicit uses)
271 invalidatePhysRegs(MI);
272
273 // Loop over uses, move from memory into registers
274 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
275 MachineOperand &op = MI->getOperand(i);
276
Chris Lattnerda7e4532002-12-15 20:36:09 +0000277 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000278 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
279 DEBUG(std::cerr << "op: " << op << "\n");
280 DEBUG(std::cerr << "\t inst[" << i << "]: ";
281 MI->print(std::cerr, TM));
282
283 // make sure the same virtual register maps to the same physical
284 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000285 unsigned physReg = Virt2PhysRegMap[virtualReg];
286 if (physReg == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000287 if (op.opIsDef()) {
288 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
289 // must be same register number as the first operand
290 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000291 assert(MI->getOperand(1).isRegister() &&
292 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner9f366d72002-12-15 22:19:19 +0000293 MI->getOperand(1).opIsUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000294 "Two address instruction invalid!");
295
Chris Lattnerda7e4532002-12-15 20:36:09 +0000296 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000297 } else {
298 physReg = getFreeReg(virtualReg);
299 }
Chris Lattnerb167c042002-12-15 23:01:26 +0000300 ++I;
301 spillVirtReg(MBB, I, virtualReg, physReg);
302 --I;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000303 } else {
Chris Lattnerb167c042002-12-15 23:01:26 +0000304 physReg = reloadVirtReg(MBB, I, virtualReg);
305 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000306 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000307 }
308 MI->SetMachineOperandReg(i, physReg);
309 DEBUG(std::cerr << "virt: " << virtualReg <<
310 ", phys: " << op.getAllocatedRegNum() << "\n");
311 }
312 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000313 clearAllRegs();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000314 }
315}
316
Chris Lattnere7d361d2002-12-17 04:19:40 +0000317
318/// EmitPrologue - Use the register info object to add a prologue to the
319/// function and save any callee saved registers we are responsible for.
320///
321void RegAllocSimple::EmitPrologue() {
322 // Get a list of the callee saved registers, so that we can save them on entry
323 // to the function.
324 //
325 MachineBasicBlock &MBB = MF->front(); // Prolog goes in entry BB
326 MachineBasicBlock::iterator I = MBB.begin();
327
328 const unsigned *CSRegs = RegInfo->getCalleeSaveRegs();
329 for (unsigned i = 0; CSRegs[i]; ++i) {
330 const TargetRegisterClass *RegClass = RegInfo->getRegClass(CSRegs[i]);
331 unsigned Offset = getStackSpaceFor(CSRegs[i], RegClass);
332
333 // Insert the spill to the stack frame...
334 I = RegInfo->storeReg2RegOffset(MBB, I,CSRegs[i],RegInfo->getFramePointer(),
335 -Offset, RegClass->getDataSize());
336 ++NumSpilled;
337 }
338
339 // Add prologue to the function...
340 RegInfo->emitPrologue(*MF, NumBytesAllocated);
341}
342
343
344/// EmitEpilogue - Use the register info object to add a epilogue to the
345/// function and restore any callee saved registers we are responsible for.
346///
347void RegAllocSimple::EmitEpilogue(MachineBasicBlock &MBB) {
348 // Insert instructions before the return.
349 MachineBasicBlock::iterator I = --MBB.end();
350
351 const unsigned *CSRegs = RegInfo->getCalleeSaveRegs();
352 for (unsigned i = 0; CSRegs[i]; ++i) {
353 const TargetRegisterClass *RegClass = RegInfo->getRegClass(CSRegs[i]);
354 unsigned Offset = getStackSpaceFor(CSRegs[i], RegClass);
355
356 I = RegInfo->loadRegOffset2Reg(MBB, I, CSRegs[i],RegInfo->getFramePointer(),
357 -Offset, RegClass->getDataSize());
358 --I; // Insert in reverse order
359 ++NumReloaded;
360 }
361
362 RegInfo->emitEpilogue(MBB, NumBytesAllocated);
363}
364
365
Chris Lattnerda7e4532002-12-15 20:36:09 +0000366/// runOnMachineFunction - Register allocate the whole function
367///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000368bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000369 DEBUG(std::cerr << "Machine Function " << "\n");
370 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000371
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000372 // First pass: eliminate PHI instructions by inserting copies into predecessor
373 // blocks.
374 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
375 MBB != MBBe; ++MBB)
376 EliminatePHINodes(*MBB);
377
Chris Lattner9f366d72002-12-15 22:19:19 +0000378 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000379 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
380 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000381 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000382
Chris Lattner69c19882002-12-16 17:42:40 +0000383 // Round stack allocation up to a nice alignment to keep the stack aligned
384 // FIXME: This is X86 specific! Move to frame manager
385 NumBytesAllocated = (NumBytesAllocated + 3) & ~3;
386
Chris Lattnere7d361d2002-12-17 04:19:40 +0000387 // Emit a prologue for the function...
388 EmitPrologue();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000389
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000390 const MachineInstrInfo &MII = TM.getInstrInfo();
391
Chris Lattner9f366d72002-12-15 22:19:19 +0000392 // Add epilogue to restore the callee-save registers in each exiting block
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000393 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000394 MBB != MBBe; ++MBB) {
Chris Lattner9f366d72002-12-15 22:19:19 +0000395 // If last instruction is a return instruction, add an epilogue
396 if (MII.isReturn(MBB->back()->getOpcode()))
Chris Lattnere7d361d2002-12-17 04:19:40 +0000397 EmitEpilogue(*MBB);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000398 }
Misha Brukman07218672002-11-22 22:44:32 +0000399
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000400 cleanupAfterFunction();
Chris Lattner9f366d72002-12-15 22:19:19 +0000401 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000402}
403
Chris Lattner1d62cea2002-12-16 14:37:00 +0000404Pass *createSimpleRegisterAllocator(TargetMachine &TM) {
Misha Brukman07218672002-11-22 22:44:32 +0000405 return new RegAllocSimple(TM);
406}