blob: 73c193ba5b897ba48e6d01ca3517c056b8efa313 [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 Lattnerdd444f92002-12-15 18:38:59 +000015/// PhysRegClassMap - Construct a mapping of physical register numbers to their
16/// register classes.
17///
18/// NOTE: This class will eventually be pulled out to somewhere shared.
19///
20class PhysRegClassMap {
21 std::map<unsigned, const TargetRegisterClass*> PhysReg2RegClassMap;
22public:
23 PhysRegClassMap(const MRegisterInfo *RI) {
24 for (MRegisterInfo::const_iterator I = RI->regclass_begin(),
25 E = RI->regclass_end(); I != E; ++I)
26 for (unsigned i=0; i < (*I)->getNumRegs(); ++i)
27 PhysReg2RegClassMap[(*I)->getRegister(i)] = *I;
28 }
29
30 const TargetRegisterClass *operator[](unsigned Reg) {
31 assert(PhysReg2RegClassMap[Reg] && "Register is not a known physreg!");
32 return PhysReg2RegClassMap[Reg];
33 }
34
35 const TargetRegisterClass *get(unsigned Reg) { return operator[](Reg); }
36};
37
38
Misha Brukman59b3eed2002-12-13 10:42:31 +000039namespace {
Chris Lattnerda7e4532002-12-15 20:36:09 +000040 Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
41 Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
42
43 class RegAllocSimple : public FunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000044 TargetMachine &TM;
Misha Brukman07218672002-11-22 22:44:32 +000045 MachineFunction *MF;
Misha Brukman07218672002-11-22 22:44:32 +000046 const MRegisterInfo *RegInfo;
Chris Lattner9593fb12002-12-15 19:07:34 +000047 unsigned NumBytesAllocated;
Misha Brukman07218672002-11-22 22:44:32 +000048
49 // Maps SSA Regs => offsets on the stack where these values are stored
Chris Lattnerad44bd92002-12-15 18:15:24 +000050 std::map<unsigned, unsigned> VirtReg2OffsetMap;
Misha Brukman07218672002-11-22 22:44:32 +000051
52 // Maps SSA Regs => physical regs
53 std::map<unsigned, unsigned> SSA2PhysRegMap;
Misha Brukmandc2ec002002-12-03 23:15:19 +000054
55 // Maps physical register to their register classes
Chris Lattnerdd444f92002-12-15 18:38:59 +000056 PhysRegClassMap PhysRegClasses;
Misha Brukmand1bedcc2002-12-12 23:20:31 +000057
58 // Made to combat the incorrect allocation of r2 = add r1, r1
59 std::map<unsigned, unsigned> VirtReg2PhysRegMap;
Misha Brukman07218672002-11-22 22:44:32 +000060
Chris Lattnerda7e4532002-12-15 20:36:09 +000061 // RegsUsed - Keep track of what registers are currently in use.
62 std::set<unsigned> RegsUsed;
63
64 // RegClassIdx - Maps RegClass => which index we can take a register
65 // from. Since this is a simple register allocator, when we need a register
66 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000067 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
68
Chris Lattnerda7e4532002-12-15 20:36:09 +000069 public:
70
Chris Lattnerc2db1a92002-12-15 19:51:14 +000071 RegAllocSimple(TargetMachine &tm)
72 : TM(tm), RegInfo(tm.getRegisterInfo()), PhysRegClasses(RegInfo) {
Chris Lattnerda7e4532002-12-15 20:36:09 +000073 RegsUsed.insert(RegInfo->getFramePointer());
74 RegsUsed.insert(RegInfo->getStackPointer());
Misha Brukmancea22452002-12-13 04:34:02 +000075
76 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +000077 }
78
Chris Lattnerda7e4532002-12-15 20:36:09 +000079 bool runOnFunction(Function &Fn) {
80 return runOnMachineFunction(MachineFunction::get(&Fn));
81 }
82
Chris Lattner8233e2f2002-12-15 21:13:12 +000083 virtual const char *getPassName() const {
84 return "Simple Register Allocator";
85 }
86
Chris Lattnerda7e4532002-12-15 20:36:09 +000087 private:
88 /// runOnMachineFunction - Register allocate the whole function
89 bool runOnMachineFunction(MachineFunction &Fn);
90
91 /// AllocateBasicBlock - Register allocate the specified basic block.
92 void AllocateBasicBlock(MachineBasicBlock &MBB);
93
94 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
95 /// in predecessor basic blocks.
96 void EliminatePHINodes(MachineBasicBlock &MBB);
97
98
Misha Brukman07218672002-11-22 22:44:32 +000099 bool isAvailableReg(unsigned Reg) {
100 // assert(Reg < MRegisterInfo::FirstVirtualReg && "...");
101 return RegsUsed.find(Reg) == RegsUsed.end();
102 }
103
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000104 /// allocateStackSpaceFor - This allocates space for the specified virtual
105 /// register to be held on the stack.
Misha Brukmanf514d512002-12-02 21:11:58 +0000106 unsigned allocateStackSpaceFor(unsigned VirtReg,
107 const TargetRegisterClass *regClass);
108
Chris Lattnerda7e4532002-12-15 20:36:09 +0000109 /// Given a virtual register, returns a physical register that is currently
110 /// unused.
111 ///
Misha Brukman07218672002-11-22 22:44:32 +0000112 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +0000113 ///
Misha Brukman07218672002-11-22 22:44:32 +0000114 unsigned getFreeReg(unsigned virtualReg);
115
116 /// Returns all `borrowed' registers back to the free pool
117 void clearAllRegs() {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000118 RegClassIdx.clear();
Misha Brukman07218672002-11-22 22:44:32 +0000119 }
120
Misha Brukman972b03f2002-12-13 11:33:22 +0000121 /// Invalidates any references, real or implicit, to physical registers
122 ///
123 void invalidatePhysRegs(const MachineInstr *MI) {
124 unsigned Opcode = MI->getOpcode();
Chris Lattnerda7e4532002-12-15 20:36:09 +0000125 const MachineInstrDescriptor &Desc = TM.getInstrInfo().get(Opcode);
Misha Brukman972b03f2002-12-13 11:33:22 +0000126 const unsigned *regs = Desc.ImplicitUses;
127 while (*regs)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000128 RegsUsed.insert(*regs++);
Misha Brukman972b03f2002-12-13 11:33:22 +0000129
130 regs = Desc.ImplicitDefs;
131 while (*regs)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000132 RegsUsed.insert(*regs++);
Misha Brukman972b03f2002-12-13 11:33:22 +0000133 }
134
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000135 void cleanupAfterFunction() {
Chris Lattnerad44bd92002-12-15 18:15:24 +0000136 VirtReg2OffsetMap.clear();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000137 SSA2PhysRegMap.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000138 NumBytesAllocated = 4; // FIXME: This is X86 specific
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000139 }
140
Misha Brukman07218672002-11-22 22:44:32 +0000141 /// Moves value from memory into that register
142 MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000143 moveUseToReg (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000144 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukman07218672002-11-22 22:44:32 +0000145 unsigned &PhysReg);
146
147 /// Saves reg value on the stack (maps virtual register to stack value)
148 MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000149 saveVirtRegToStack (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000150 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000151 unsigned PhysReg);
152
153 MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000154 savePhysRegToStack (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000155 MachineBasicBlock::iterator I, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000156 };
157
Misha Brukman59b3eed2002-12-13 10:42:31 +0000158}
Misha Brukman07218672002-11-22 22:44:32 +0000159
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000160/// allocateStackSpaceFor - This allocates space for the specified virtual
161/// register to be held on the stack.
Misha Brukmanf514d512002-12-02 21:11:58 +0000162unsigned RegAllocSimple::allocateStackSpaceFor(unsigned VirtReg,
163 const TargetRegisterClass *regClass)
164{
Chris Lattnerad44bd92002-12-15 18:15:24 +0000165 if (VirtReg2OffsetMap.find(VirtReg) == VirtReg2OffsetMap.end()) {
Chris Lattner9593fb12002-12-15 19:07:34 +0000166 unsigned RegSize = regClass->getDataSize();
167
168 // Align NumBytesAllocated. We should be using TargetData alignment stuff
169 // to determine this, but we don't know the LLVM type associated with the
170 // virtual register. Instead, just align to a multiple of the size for now.
171 NumBytesAllocated += RegSize-1;
172 NumBytesAllocated = NumBytesAllocated/RegSize*RegSize;
173
174 // Assign the slot...
Chris Lattnerad44bd92002-12-15 18:15:24 +0000175 VirtReg2OffsetMap[VirtReg] = NumBytesAllocated;
Chris Lattner9593fb12002-12-15 19:07:34 +0000176
177 // Reserve the space!
178 NumBytesAllocated += RegSize;
Misha Brukmanf514d512002-12-02 21:11:58 +0000179 }
Chris Lattnerad44bd92002-12-15 18:15:24 +0000180 return VirtReg2OffsetMap[VirtReg];
Misha Brukmanf514d512002-12-02 21:11:58 +0000181}
182
Misha Brukman07218672002-11-22 22:44:32 +0000183unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
184 const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
185 unsigned physReg;
186 assert(regClass);
187 if (RegClassIdx.find(regClass) != RegClassIdx.end()) {
188 unsigned regIdx = RegClassIdx[regClass]++;
189 assert(regIdx < regClass->getNumRegs() && "Not enough registers!");
190 physReg = regClass->getRegister(regIdx);
191 } else {
192 physReg = regClass->getRegister(0);
193 // assert(physReg < regClass->getNumRegs() && "No registers in class!");
194 RegClassIdx[regClass] = 1;
195 }
196
197 if (isAvailableReg(physReg))
198 return physReg;
Chris Lattnerda7e4532002-12-15 20:36:09 +0000199 else
Misha Brukman07218672002-11-22 22:44:32 +0000200 return getFreeReg(virtualReg);
Misha Brukman07218672002-11-22 22:44:32 +0000201}
202
203MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000204RegAllocSimple::moveUseToReg (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000205 MachineBasicBlock::iterator I,
Misha Brukman07218672002-11-22 22:44:32 +0000206 unsigned VirtReg, unsigned &PhysReg)
207{
208 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
209 assert(regClass);
210
Misha Brukmanf514d512002-12-02 21:11:58 +0000211 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000212 PhysReg = getFreeReg(VirtReg);
213
Misha Brukmanf514d512002-12-02 21:11:58 +0000214 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000215 ++NumReloaded;
Chris Lattner198ab642002-12-15 20:06:35 +0000216 return RegInfo->loadRegOffset2Reg(MBB, I, PhysReg,
Misha Brukmanf514d512002-12-02 21:11:58 +0000217 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000218 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000219}
220
221MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000222RegAllocSimple::saveVirtRegToStack (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000223 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000224 unsigned VirtReg, unsigned PhysReg)
Misha Brukman07218672002-11-22 22:44:32 +0000225{
226 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
227 assert(regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000228
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000229 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukmanf514d512002-12-02 21:11:58 +0000230
Misha Brukman07218672002-11-22 22:44:32 +0000231 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000232 ++NumSpilled;
Chris Lattner198ab642002-12-15 20:06:35 +0000233 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukman07218672002-11-22 22:44:32 +0000234 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000235 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000236}
237
Misha Brukmandc2ec002002-12-03 23:15:19 +0000238MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000239RegAllocSimple::savePhysRegToStack (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000240 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000241 unsigned PhysReg)
242{
243 const TargetRegisterClass* regClass = MF->getRegClass(PhysReg);
244 assert(regClass);
245
246 unsigned offset = allocateStackSpaceFor(PhysReg, regClass);
247
248 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000249 ++NumSpilled;
Chris Lattner198ab642002-12-15 20:06:35 +0000250 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000251 RegInfo->getFramePointer(),
252 offset, regClass->getDataSize());
253}
254
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000255/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
256/// predecessor basic blocks.
257void RegAllocSimple::EliminatePHINodes(MachineBasicBlock &MBB) {
Chris Lattnerda7e4532002-12-15 20:36:09 +0000258 const MachineInstrInfo &MII = TM.getInstrInfo();
259
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000260 while (MBB.front()->getOpcode() == 0) {
261 MachineInstr *MI = MBB.front();
Chris Lattnerda7e4532002-12-15 20:36:09 +0000262 // Unlink the PHI node from the basic block... but don't delete the PHI
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000263 MBB.erase(MBB.begin());
264
265 // a preliminary pass that will invalidate any registers that
266 // are used by the instruction (including implicit uses)
267 invalidatePhysRegs(MI);
268
269 DEBUG(std::cerr << "num invalid regs: " << RegsUsed.size() << "\n");
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000270 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
271 MachineOperand &targetReg = MI->getOperand(0);
272
Chris Lattnerda7e4532002-12-15 20:36:09 +0000273 // If it's a virtual register, allocate a physical one otherwise, just use
274 // whatever register is there now note: it MUST be a register -- we're
275 // assigning to it!
276 //
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000277 unsigned virtualReg = (unsigned) targetReg.getAllocatedRegNum();
278 unsigned physReg;
279 if (targetReg.isVirtualRegister()) {
280 physReg = getFreeReg(virtualReg);
281 } else {
282 physReg = virtualReg;
283 }
284
285 // Find the register class of the target register: should be the
286 // same as the values we're trying to store there
287 const TargetRegisterClass* regClass = PhysRegClasses[physReg];
288 assert(regClass && "Target register class not found!");
289 unsigned dataSize = regClass->getDataSize();
Chris Lattner3f91ad72002-12-15 20:48:03 +0000290
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000291 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
292 MachineOperand &opVal = MI->getOperand(i-1);
293
294 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
295 // source path the phi
296 MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000297
Chris Lattner3f91ad72002-12-15 20:48:03 +0000298 // Check to make sure we haven't already emitted the copy for this block.
299 // This can happen because PHI nodes may have multiple entries for the
300 // same basic block. It doesn't matter which entry we use though, because
301 // all incoming values are guaranteed to be the same for a particular bb.
302 //
303 // Note that this is N^2 in the number of phi node entries, but since the
304 // # of entries is tiny, this is not a problem.
305 //
306 bool HaveNotEmitted = true;
307 for (int op = MI->getNumOperands() - 1; op != i; op -= 2)
308 if (&opBlock == MI->getOperand(op).getMachineBasicBlock()) {
309 HaveNotEmitted = false;
310 break;
311 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000312
Chris Lattner3f91ad72002-12-15 20:48:03 +0000313 if (HaveNotEmitted) {
314 MachineBasicBlock::iterator opI = opBlock.end();
315 MachineInstr *opMI = *--opI;
316
317 // must backtrack over ALL the branches in the previous block
318 while (MII.isBranch(opMI->getOpcode()) && opI != opBlock.begin())
319 opMI = *--opI;
320
321 // move back to the first branch instruction so new instructions
322 // are inserted right in front of it and not in front of a non-branch
323 if (!MII.isBranch(opMI->getOpcode()))
324 ++opI;
325
326 // Retrieve the constant value from this op, move it to target
327 // register of the phi
328 if (opVal.isImmediate()) {
329 opI = RegInfo->moveImm2Reg(opBlock, opI, physReg,
330 (unsigned) opVal.getImmedValue(),
331 dataSize);
332 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
333 } else {
334 // Allocate a physical register and add a move in the BB
Chris Lattner8233e2f2002-12-15 21:13:12 +0000335 unsigned opVirtualReg = opVal.getAllocatedRegNum();
Chris Lattner3f91ad72002-12-15 20:48:03 +0000336 unsigned opPhysReg;
337 opI = moveUseToReg(opBlock, opI, opVirtualReg, physReg);
338
339 // Save that register value to the stack of the TARGET REG
340 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
341 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000342 }
Chris Lattner3f91ad72002-12-15 20:48:03 +0000343
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000344 // make regs available to other instructions
345 clearAllRegs();
346 }
347
348 // really delete the instruction
349 delete MI;
350 }
351}
352
353
354void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
355 // Handle PHI instructions specially: add moves to each pred block
356 EliminatePHINodes(MBB);
357
358 //loop over each basic block
359 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
360 MachineInstr *MI = *I;
361
362 // a preliminary pass that will invalidate any registers that
363 // are used by the instruction (including implicit uses)
364 invalidatePhysRegs(MI);
365
366 // Loop over uses, move from memory into registers
367 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
368 MachineOperand &op = MI->getOperand(i);
369
Chris Lattnerda7e4532002-12-15 20:36:09 +0000370 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000371 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
372 DEBUG(std::cerr << "op: " << op << "\n");
373 DEBUG(std::cerr << "\t inst[" << i << "]: ";
374 MI->print(std::cerr, TM));
375
376 // make sure the same virtual register maps to the same physical
377 // register in any given instruction
378 unsigned physReg;
379 if (VirtReg2PhysRegMap.find(virtualReg) != VirtReg2PhysRegMap.end()) {
380 physReg = VirtReg2PhysRegMap[virtualReg];
381 } else {
382 if (op.opIsDef()) {
383 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
384 // must be same register number as the first operand
385 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000386 assert(MI->getOperand(1).isRegister() &&
387 MI->getOperand(1).getAllocatedRegNum() &&
388 MF->getRegClass(virtualReg) ==
389 PhysRegClasses[MI->getOperand(1).getAllocatedRegNum()] &&
390 "Two address instruction invalid!");
391
Chris Lattnerda7e4532002-12-15 20:36:09 +0000392 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000393 } else {
394 physReg = getFreeReg(virtualReg);
395 }
396 MachineBasicBlock::iterator J = I;
397 J = saveVirtRegToStack(MBB, ++J, virtualReg, physReg);
398 I = --J;
399 } else {
400 I = moveUseToReg(MBB, I, virtualReg, physReg);
401 }
402 VirtReg2PhysRegMap[virtualReg] = physReg;
403 }
404 MI->SetMachineOperandReg(i, physReg);
405 DEBUG(std::cerr << "virt: " << virtualReg <<
406 ", phys: " << op.getAllocatedRegNum() << "\n");
407 }
408 }
409
410 clearAllRegs();
411 VirtReg2PhysRegMap.clear();
412 }
413}
414
Chris Lattnerda7e4532002-12-15 20:36:09 +0000415/// runOnMachineFunction - Register allocate the whole function
416///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000417bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000418 DEBUG(std::cerr << "Machine Function " << "\n");
419 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000420
Misha Brukman07218672002-11-22 22:44:32 +0000421 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
422 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000423 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000424
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000425 // add prologue we should preserve callee-save registers...
Chris Lattner198ab642002-12-15 20:06:35 +0000426 RegInfo->emitPrologue(Fn, NumBytesAllocated);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000427
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000428 const MachineInstrInfo &MII = TM.getInstrInfo();
429
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000430 // add epilogue to restore the callee-save registers
431 // loop over the basic block
432 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000433 MBB != MBBe; ++MBB) {
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000434 // check if last instruction is a RET
Chris Lattnerda7e4532002-12-15 20:36:09 +0000435 if (MII.isReturn(MBB->back()->getOpcode())) {
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000436 // this block has a return instruction, add epilogue
Chris Lattner198ab642002-12-15 20:06:35 +0000437 RegInfo->emitEpilogue(*MBB, NumBytesAllocated);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000438 }
439 }
Misha Brukman07218672002-11-22 22:44:32 +0000440
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000441 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +0000442 return false; // We never modify the LLVM itself.
443}
444
445Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) {
446 return new RegAllocSimple(TM);
447}