blob: 707f1cee8709be4be9d0bd8e92a6706989e3c5a9 [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"
Chris Lattner5124aec2002-12-25 05:04:20 +00009#include "llvm/CodeGen/SSARegMap.h"
Misha Brukmandd46e2a2002-12-04 23:58:08 +000010#include "llvm/Target/MachineInstrInfo.h"
Misha Brukman07218672002-11-22 22:44:32 +000011#include "llvm/Target/TargetMachine.h"
Misha Brukman07218672002-11-22 22:44:32 +000012#include "Support/Statistic.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000013#include <iostream>
Chris Lattnerda7e4532002-12-15 20:36:09 +000014#include <set>
Misha Brukman07218672002-11-22 22:44:32 +000015
Misha Brukman59b3eed2002-12-13 10:42:31 +000016namespace {
Chris Lattnerda7e4532002-12-15 20:36:09 +000017 Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
18 Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
19
20 class RegAllocSimple : public FunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000021 TargetMachine &TM;
Misha Brukman07218672002-11-22 22:44:32 +000022 MachineFunction *MF;
Misha Brukman07218672002-11-22 22:44:32 +000023 const MRegisterInfo *RegInfo;
Chris Lattner9593fb12002-12-15 19:07:34 +000024 unsigned NumBytesAllocated;
Misha Brukman07218672002-11-22 22:44:32 +000025
26 // Maps SSA Regs => offsets on the stack where these values are stored
Chris Lattnerad44bd92002-12-15 18:15:24 +000027 std::map<unsigned, unsigned> VirtReg2OffsetMap;
Misha Brukman07218672002-11-22 22:44:32 +000028
Chris Lattnerda7e4532002-12-15 20:36:09 +000029 // RegsUsed - Keep track of what registers are currently in use.
30 std::set<unsigned> RegsUsed;
31
32 // RegClassIdx - Maps RegClass => which index we can take a register
33 // from. Since this is a simple register allocator, when we need a register
34 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000035 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
36
Chris Lattnerda7e4532002-12-15 20:36:09 +000037 public:
38
Chris Lattnerc2db1a92002-12-15 19:51:14 +000039 RegAllocSimple(TargetMachine &tm)
Chris Lattner9f366d72002-12-15 22:19:19 +000040 : TM(tm), RegInfo(tm.getRegisterInfo()) {
Chris Lattnerda7e4532002-12-15 20:36:09 +000041 RegsUsed.insert(RegInfo->getFramePointer());
42 RegsUsed.insert(RegInfo->getStackPointer());
Misha Brukmancea22452002-12-13 04:34:02 +000043
44 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +000045 }
46
Chris Lattnerda7e4532002-12-15 20:36:09 +000047 bool runOnFunction(Function &Fn) {
48 return runOnMachineFunction(MachineFunction::get(&Fn));
49 }
50
Chris Lattner8233e2f2002-12-15 21:13:12 +000051 virtual const char *getPassName() const {
52 return "Simple Register Allocator";
53 }
54
Chris Lattnerda7e4532002-12-15 20:36:09 +000055 private:
56 /// runOnMachineFunction - Register allocate the whole function
57 bool runOnMachineFunction(MachineFunction &Fn);
58
59 /// AllocateBasicBlock - Register allocate the specified basic block.
60 void AllocateBasicBlock(MachineBasicBlock &MBB);
61
62 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
63 /// in predecessor basic blocks.
64 void EliminatePHINodes(MachineBasicBlock &MBB);
65
Chris Lattnere7d361d2002-12-17 04:19:40 +000066 /// EmitPrologue/EmitEpilogue - Use the register info object to add a
67 /// prologue/epilogue to the function and save/restore any callee saved
68 /// registers we are responsible for.
69 ///
70 void EmitPrologue();
71 void EmitEpilogue(MachineBasicBlock &MBB);
72
Chris Lattnerda7e4532002-12-15 20:36:09 +000073
Chris Lattner9f366d72002-12-15 22:19:19 +000074 /// getStackSpaceFor - This returns the offset of the specified virtual
75 /// register on the stack, allocating space if neccesary.
76 unsigned getStackSpaceFor(unsigned VirtReg,
77 const TargetRegisterClass *regClass);
Misha Brukman07218672002-11-22 22:44:32 +000078
Chris Lattner9f366d72002-12-15 22:19:19 +000079 /// Given a virtual register, return a compatible physical register that is
80 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000081 ///
Misha Brukman07218672002-11-22 22:44:32 +000082 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000083 ///
Misha Brukman07218672002-11-22 22:44:32 +000084 unsigned getFreeReg(unsigned virtualReg);
85
86 /// Returns all `borrowed' registers back to the free pool
87 void clearAllRegs() {
Chris Lattnerc2db1a92002-12-15 19:51:14 +000088 RegClassIdx.clear();
Misha Brukman07218672002-11-22 22:44:32 +000089 }
90
Misha Brukman972b03f2002-12-13 11:33:22 +000091 /// Invalidates any references, real or implicit, to physical registers
92 ///
93 void invalidatePhysRegs(const MachineInstr *MI) {
94 unsigned Opcode = MI->getOpcode();
Chris Lattnerda7e4532002-12-15 20:36:09 +000095 const MachineInstrDescriptor &Desc = TM.getInstrInfo().get(Opcode);
Chris Lattneraed967c2002-12-18 01:11:14 +000096 if (const unsigned *regs = Desc.ImplicitUses)
97 while (*regs)
98 RegsUsed.insert(*regs++);
Misha Brukman972b03f2002-12-13 11:33:22 +000099
Chris Lattneraed967c2002-12-18 01:11:14 +0000100 if (const unsigned *regs = Desc.ImplicitDefs)
101 while (*regs)
102 RegsUsed.insert(*regs++);
Misha Brukman972b03f2002-12-13 11:33:22 +0000103 }
104
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000105 void cleanupAfterFunction() {
Chris Lattnerad44bd92002-12-15 18:15:24 +0000106 VirtReg2OffsetMap.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000107 NumBytesAllocated = 4; // FIXME: This is X86 specific
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000108 }
109
Misha Brukman07218672002-11-22 22:44:32 +0000110 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +0000111 unsigned reloadVirtReg(MachineBasicBlock &MBB,
112 MachineBasicBlock::iterator &I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000113
114 /// Saves reg value on the stack (maps virtual register to stack value)
Chris Lattnerb167c042002-12-15 23:01:26 +0000115 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
116 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000117 };
118
Misha Brukman59b3eed2002-12-13 10:42:31 +0000119}
Misha Brukman07218672002-11-22 22:44:32 +0000120
Chris Lattner9f366d72002-12-15 22:19:19 +0000121/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000122/// register to be held on the stack.
Chris Lattner9f366d72002-12-15 22:19:19 +0000123unsigned RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
124 const TargetRegisterClass *regClass) {
125 // Find the location VirtReg would belong...
126 std::map<unsigned, unsigned>::iterator I =
127 VirtReg2OffsetMap.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +0000128
Chris Lattner9f366d72002-12-15 22:19:19 +0000129 if (I != VirtReg2OffsetMap.end() && I->first == VirtReg)
130 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +0000131
Chris Lattner9f366d72002-12-15 22:19:19 +0000132 unsigned RegSize = regClass->getDataSize();
Chris Lattner9593fb12002-12-15 19:07:34 +0000133
Chris Lattner9f366d72002-12-15 22:19:19 +0000134 // Align NumBytesAllocated. We should be using TargetData alignment stuff
135 // to determine this, but we don't know the LLVM type associated with the
136 // virtual register. Instead, just align to a multiple of the size for now.
137 NumBytesAllocated += RegSize-1;
138 NumBytesAllocated = NumBytesAllocated/RegSize*RegSize;
139
140 // Assign the slot...
141 VirtReg2OffsetMap.insert(I, std::make_pair(VirtReg, NumBytesAllocated));
142
143 // Reserve the space!
144 NumBytesAllocated += RegSize;
145 return NumBytesAllocated-RegSize;
Misha Brukmanf514d512002-12-02 21:11:58 +0000146}
147
Misha Brukman07218672002-11-22 22:44:32 +0000148unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000149 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(virtualReg);
Chris Lattner9f366d72002-12-15 22:19:19 +0000150
Chris Lattner5124aec2002-12-25 05:04:20 +0000151 unsigned regIdx = RegClassIdx[RC]++;
152 assert(regIdx < RC->getNumRegs() && "Not enough registers!");
153 unsigned physReg = RC->getRegister(regIdx);
Misha Brukman07218672002-11-22 22:44:32 +0000154
Chris Lattner9f366d72002-12-15 22:19:19 +0000155 if (RegsUsed.find(physReg) == RegsUsed.end())
Misha Brukman07218672002-11-22 22:44:32 +0000156 return physReg;
Chris Lattnerda7e4532002-12-15 20:36:09 +0000157 else
Misha Brukman07218672002-11-22 22:44:32 +0000158 return getFreeReg(virtualReg);
Misha Brukman07218672002-11-22 22:44:32 +0000159}
160
Chris Lattnerb167c042002-12-15 23:01:26 +0000161unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
162 MachineBasicBlock::iterator &I,
163 unsigned VirtReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000164 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
165 unsigned stackOffset = getStackSpaceFor(VirtReg, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000166 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000167
Misha Brukmanf514d512002-12-02 21:11:58 +0000168 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000169 ++NumReloaded;
Chris Lattner5124aec2002-12-25 05:04:20 +0000170 RegInfo->loadRegOffset2Reg(MBB, I, PhysReg, RegInfo->getFramePointer(),
171 -stackOffset, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000172 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000173}
174
Chris Lattnerb167c042002-12-15 23:01:26 +0000175void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
176 MachineBasicBlock::iterator &I,
177 unsigned VirtReg, unsigned PhysReg)
Misha Brukman07218672002-11-22 22:44:32 +0000178{
Chris Lattner5124aec2002-12-25 05:04:20 +0000179 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
180 unsigned stackOffset = getStackSpaceFor(VirtReg, RC);
Misha Brukmanf514d512002-12-02 21:11:58 +0000181
Misha Brukman07218672002-11-22 22:44:32 +0000182 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000183 ++NumSpilled;
Chris Lattner5124aec2002-12-25 05:04:20 +0000184 RegInfo->storeReg2RegOffset(MBB, I, PhysReg, RegInfo->getFramePointer(),
185 -stackOffset, RC);
Misha Brukman07218672002-11-22 22:44:32 +0000186}
187
Misha Brukmandc2ec002002-12-03 23:15:19 +0000188
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000189/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
190/// predecessor basic blocks.
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000191///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000192void RegAllocSimple::EliminatePHINodes(MachineBasicBlock &MBB) {
Chris Lattnerda7e4532002-12-15 20:36:09 +0000193 const MachineInstrInfo &MII = TM.getInstrInfo();
194
Chris Lattner9f366d72002-12-15 22:19:19 +0000195 while (MBB.front()->getOpcode() == MachineInstrInfo::PHI) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000196 MachineInstr *MI = MBB.front();
Chris Lattner9f366d72002-12-15 22:19:19 +0000197 // Unlink the PHI node from the basic block... but don't delete the PHI yet
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000198 MBB.erase(MBB.begin());
199
Chris Lattner9f366d72002-12-15 22:19:19 +0000200 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
201 assert(MI->getOperand(0).isVirtualRegister() &&
202 "PHI node doesn't write virt reg?");
203
Chris Lattner9f366d72002-12-15 22:19:19 +0000204 unsigned virtualReg = MI->getOperand(0).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000205
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000206 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
207 MachineOperand &opVal = MI->getOperand(i-1);
208
209 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
210 // source path the phi
211 MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000212
Chris Lattner3f91ad72002-12-15 20:48:03 +0000213 // Check to make sure we haven't already emitted the copy for this block.
214 // This can happen because PHI nodes may have multiple entries for the
215 // same basic block. It doesn't matter which entry we use though, because
216 // all incoming values are guaranteed to be the same for a particular bb.
217 //
218 // Note that this is N^2 in the number of phi node entries, but since the
219 // # of entries is tiny, this is not a problem.
220 //
221 bool HaveNotEmitted = true;
222 for (int op = MI->getNumOperands() - 1; op != i; op -= 2)
223 if (&opBlock == MI->getOperand(op).getMachineBasicBlock()) {
224 HaveNotEmitted = false;
225 break;
226 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000227
Chris Lattner3f91ad72002-12-15 20:48:03 +0000228 if (HaveNotEmitted) {
229 MachineBasicBlock::iterator opI = opBlock.end();
230 MachineInstr *opMI = *--opI;
231
232 // must backtrack over ALL the branches in the previous block
233 while (MII.isBranch(opMI->getOpcode()) && opI != opBlock.begin())
234 opMI = *--opI;
235
236 // move back to the first branch instruction so new instructions
237 // are inserted right in front of it and not in front of a non-branch
Chris Lattner5124aec2002-12-25 05:04:20 +0000238 //
Chris Lattner3f91ad72002-12-15 20:48:03 +0000239 if (!MII.isBranch(opMI->getOpcode()))
240 ++opI;
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000241
Chris Lattner5124aec2002-12-25 05:04:20 +0000242 const TargetRegisterClass *RC =
243 MF->getSSARegMap()->getRegClass(virtualReg);
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000244
Chris Lattner3f91ad72002-12-15 20:48:03 +0000245 // Retrieve the constant value from this op, move it to target
246 // register of the phi
247 if (opVal.isImmediate()) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000248 RegInfo->moveImm2Reg(opBlock, opI, virtualReg,
249 (unsigned) opVal.getImmedValue(), RC);
Chris Lattner3f91ad72002-12-15 20:48:03 +0000250 } else {
Chris Lattner5124aec2002-12-25 05:04:20 +0000251 RegInfo->moveReg2Reg(opBlock, opI, virtualReg,
252 opVal.getAllocatedRegNum(), RC);
Chris Lattner3f91ad72002-12-15 20:48:03 +0000253 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000254 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000255 }
256
Chris Lattner9f366d72002-12-15 22:19:19 +0000257 // really delete the PHI instruction now!
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000258 delete MI;
259 }
260}
261
262
263void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000264 // loop over each instruction
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000265 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000266 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000267 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000268
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000269 MachineInstr *MI = *I;
270
271 // a preliminary pass that will invalidate any registers that
272 // are used by the instruction (including implicit uses)
273 invalidatePhysRegs(MI);
274
275 // Loop over uses, move from memory into registers
276 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
277 MachineOperand &op = MI->getOperand(i);
278
Chris Lattnerda7e4532002-12-15 20:36:09 +0000279 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000280 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
281 DEBUG(std::cerr << "op: " << op << "\n");
282 DEBUG(std::cerr << "\t inst[" << i << "]: ";
283 MI->print(std::cerr, TM));
284
285 // make sure the same virtual register maps to the same physical
286 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000287 unsigned physReg = Virt2PhysRegMap[virtualReg];
288 if (physReg == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000289 if (op.opIsDef()) {
290 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
291 // must be same register number as the first operand
292 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000293 assert(MI->getOperand(1).isRegister() &&
294 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner9f366d72002-12-15 22:19:19 +0000295 MI->getOperand(1).opIsUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000296 "Two address instruction invalid!");
297
Chris Lattnerda7e4532002-12-15 20:36:09 +0000298 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000299 } else {
300 physReg = getFreeReg(virtualReg);
301 }
Chris Lattnerb167c042002-12-15 23:01:26 +0000302 ++I;
303 spillVirtReg(MBB, I, virtualReg, physReg);
304 --I;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000305 } else {
Chris Lattnerb167c042002-12-15 23:01:26 +0000306 physReg = reloadVirtReg(MBB, I, virtualReg);
307 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000308 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000309 }
310 MI->SetMachineOperandReg(i, physReg);
311 DEBUG(std::cerr << "virt: " << virtualReg <<
312 ", phys: " << op.getAllocatedRegNum() << "\n");
313 }
314 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000315 clearAllRegs();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000316 }
317}
318
Chris Lattnere7d361d2002-12-17 04:19:40 +0000319
320/// EmitPrologue - Use the register info object to add a prologue to the
321/// function and save any callee saved registers we are responsible for.
322///
323void RegAllocSimple::EmitPrologue() {
324 // Get a list of the callee saved registers, so that we can save them on entry
325 // to the function.
326 //
327 MachineBasicBlock &MBB = MF->front(); // Prolog goes in entry BB
328 MachineBasicBlock::iterator I = MBB.begin();
329
330 const unsigned *CSRegs = RegInfo->getCalleeSaveRegs();
331 for (unsigned i = 0; CSRegs[i]; ++i) {
332 const TargetRegisterClass *RegClass = RegInfo->getRegClass(CSRegs[i]);
333 unsigned Offset = getStackSpaceFor(CSRegs[i], RegClass);
334
335 // Insert the spill to the stack frame...
Chris Lattner5124aec2002-12-25 05:04:20 +0000336 RegInfo->storeReg2RegOffset(MBB, I,CSRegs[i],RegInfo->getFramePointer(),
337 -Offset, RegClass);
Chris Lattnere7d361d2002-12-17 04:19:40 +0000338 ++NumSpilled;
339 }
340
341 // Add prologue to the function...
342 RegInfo->emitPrologue(*MF, NumBytesAllocated);
343}
344
345
346/// EmitEpilogue - Use the register info object to add a epilogue to the
347/// function and restore any callee saved registers we are responsible for.
348///
349void RegAllocSimple::EmitEpilogue(MachineBasicBlock &MBB) {
350 // Insert instructions before the return.
Chris Lattnere5008642002-12-23 23:44:04 +0000351 MachineBasicBlock::iterator I = MBB.end()-1;
Chris Lattnere7d361d2002-12-17 04:19:40 +0000352
353 const unsigned *CSRegs = RegInfo->getCalleeSaveRegs();
354 for (unsigned i = 0; CSRegs[i]; ++i) {
355 const TargetRegisterClass *RegClass = RegInfo->getRegClass(CSRegs[i]);
356 unsigned Offset = getStackSpaceFor(CSRegs[i], RegClass);
357
Chris Lattner5124aec2002-12-25 05:04:20 +0000358 RegInfo->loadRegOffset2Reg(MBB, I, CSRegs[i],RegInfo->getFramePointer(),
359 -Offset, RegClass);
Chris Lattnere7d361d2002-12-17 04:19:40 +0000360 --I; // Insert in reverse order
361 ++NumReloaded;
362 }
363
364 RegInfo->emitEpilogue(MBB, NumBytesAllocated);
365}
366
367
Chris Lattnerda7e4532002-12-15 20:36:09 +0000368/// runOnMachineFunction - Register allocate the whole function
369///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000370bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000371 DEBUG(std::cerr << "Machine Function " << "\n");
372 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000373
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000374 // First pass: eliminate PHI instructions by inserting copies into predecessor
375 // blocks.
376 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
377 MBB != MBBe; ++MBB)
378 EliminatePHINodes(*MBB);
379
Chris Lattner9f366d72002-12-15 22:19:19 +0000380 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000381 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
382 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000383 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000384
Chris Lattner69c19882002-12-16 17:42:40 +0000385 // Round stack allocation up to a nice alignment to keep the stack aligned
386 // FIXME: This is X86 specific! Move to frame manager
387 NumBytesAllocated = (NumBytesAllocated + 3) & ~3;
388
Chris Lattnere7d361d2002-12-17 04:19:40 +0000389 // Emit a prologue for the function...
390 EmitPrologue();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000391
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000392 const MachineInstrInfo &MII = TM.getInstrInfo();
393
Chris Lattner9f366d72002-12-15 22:19:19 +0000394 // Add epilogue to restore the callee-save registers in each exiting block
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000395 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000396 MBB != MBBe; ++MBB) {
Chris Lattner9f366d72002-12-15 22:19:19 +0000397 // If last instruction is a return instruction, add an epilogue
398 if (MII.isReturn(MBB->back()->getOpcode()))
Chris Lattnere7d361d2002-12-17 04:19:40 +0000399 EmitEpilogue(*MBB);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000400 }
Misha Brukman07218672002-11-22 22:44:32 +0000401
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000402 cleanupAfterFunction();
Chris Lattner9f366d72002-12-15 22:19:19 +0000403 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000404}
405
Chris Lattner1d62cea2002-12-16 14:37:00 +0000406Pass *createSimpleRegisterAllocator(TargetMachine &TM) {
Misha Brukman07218672002-11-22 22:44:32 +0000407 return new RegAllocSimple(TM);
408}