blob: e1f10bf498c1510709ec280299d6e9cbe9c1a881 [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
Misha Brukmandc2ec002002-12-03 23:15:19 +000052 // Maps physical register to their register classes
Chris Lattnerdd444f92002-12-15 18:38:59 +000053 PhysRegClassMap PhysRegClasses;
Misha Brukmand1bedcc2002-12-12 23:20:31 +000054
Chris Lattnerda7e4532002-12-15 20:36:09 +000055 // RegsUsed - Keep track of what registers are currently in use.
56 std::set<unsigned> RegsUsed;
57
58 // RegClassIdx - Maps RegClass => which index we can take a register
59 // from. Since this is a simple register allocator, when we need a register
60 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000061 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
62
Chris Lattnerda7e4532002-12-15 20:36:09 +000063 public:
64
Chris Lattnerc2db1a92002-12-15 19:51:14 +000065 RegAllocSimple(TargetMachine &tm)
66 : TM(tm), RegInfo(tm.getRegisterInfo()), PhysRegClasses(RegInfo) {
Chris Lattnerda7e4532002-12-15 20:36:09 +000067 RegsUsed.insert(RegInfo->getFramePointer());
68 RegsUsed.insert(RegInfo->getStackPointer());
Misha Brukmancea22452002-12-13 04:34:02 +000069
70 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +000071 }
72
Chris Lattnerda7e4532002-12-15 20:36:09 +000073 bool runOnFunction(Function &Fn) {
74 return runOnMachineFunction(MachineFunction::get(&Fn));
75 }
76
Chris Lattner8233e2f2002-12-15 21:13:12 +000077 virtual const char *getPassName() const {
78 return "Simple Register Allocator";
79 }
80
Chris Lattnerda7e4532002-12-15 20:36:09 +000081 private:
82 /// runOnMachineFunction - Register allocate the whole function
83 bool runOnMachineFunction(MachineFunction &Fn);
84
85 /// AllocateBasicBlock - Register allocate the specified basic block.
86 void AllocateBasicBlock(MachineBasicBlock &MBB);
87
88 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
89 /// in predecessor basic blocks.
90 void EliminatePHINodes(MachineBasicBlock &MBB);
91
92
Misha Brukman07218672002-11-22 22:44:32 +000093 bool isAvailableReg(unsigned Reg) {
94 // assert(Reg < MRegisterInfo::FirstVirtualReg && "...");
95 return RegsUsed.find(Reg) == RegsUsed.end();
96 }
97
Chris Lattnerc2db1a92002-12-15 19:51:14 +000098 /// allocateStackSpaceFor - This allocates space for the specified virtual
99 /// register to be held on the stack.
Misha Brukmanf514d512002-12-02 21:11:58 +0000100 unsigned allocateStackSpaceFor(unsigned VirtReg,
101 const TargetRegisterClass *regClass);
102
Chris Lattnerda7e4532002-12-15 20:36:09 +0000103 /// Given a virtual register, returns a physical register that is currently
104 /// unused.
105 ///
Misha Brukman07218672002-11-22 22:44:32 +0000106 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +0000107 ///
Misha Brukman07218672002-11-22 22:44:32 +0000108 unsigned getFreeReg(unsigned virtualReg);
109
110 /// Returns all `borrowed' registers back to the free pool
111 void clearAllRegs() {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000112 RegClassIdx.clear();
Misha Brukman07218672002-11-22 22:44:32 +0000113 }
114
Misha Brukman972b03f2002-12-13 11:33:22 +0000115 /// Invalidates any references, real or implicit, to physical registers
116 ///
117 void invalidatePhysRegs(const MachineInstr *MI) {
118 unsigned Opcode = MI->getOpcode();
Chris Lattnerda7e4532002-12-15 20:36:09 +0000119 const MachineInstrDescriptor &Desc = TM.getInstrInfo().get(Opcode);
Misha Brukman972b03f2002-12-13 11:33:22 +0000120 const unsigned *regs = Desc.ImplicitUses;
121 while (*regs)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000122 RegsUsed.insert(*regs++);
Misha Brukman972b03f2002-12-13 11:33:22 +0000123
124 regs = Desc.ImplicitDefs;
125 while (*regs)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000126 RegsUsed.insert(*regs++);
Misha Brukman972b03f2002-12-13 11:33:22 +0000127 }
128
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000129 void cleanupAfterFunction() {
Chris Lattnerad44bd92002-12-15 18:15:24 +0000130 VirtReg2OffsetMap.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000131 NumBytesAllocated = 4; // FIXME: This is X86 specific
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000132 }
133
Misha Brukman07218672002-11-22 22:44:32 +0000134 /// Moves value from memory into that register
135 MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000136 moveUseToReg (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000137 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukman07218672002-11-22 22:44:32 +0000138 unsigned &PhysReg);
139
140 /// Saves reg value on the stack (maps virtual register to stack value)
141 MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000142 saveVirtRegToStack (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000143 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000144 unsigned PhysReg);
145
146 MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000147 savePhysRegToStack (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000148 MachineBasicBlock::iterator I, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000149 };
150
Misha Brukman59b3eed2002-12-13 10:42:31 +0000151}
Misha Brukman07218672002-11-22 22:44:32 +0000152
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000153/// allocateStackSpaceFor - This allocates space for the specified virtual
154/// register to be held on the stack.
Misha Brukmanf514d512002-12-02 21:11:58 +0000155unsigned RegAllocSimple::allocateStackSpaceFor(unsigned VirtReg,
156 const TargetRegisterClass *regClass)
157{
Chris Lattnerad44bd92002-12-15 18:15:24 +0000158 if (VirtReg2OffsetMap.find(VirtReg) == VirtReg2OffsetMap.end()) {
Chris Lattner9593fb12002-12-15 19:07:34 +0000159 unsigned RegSize = regClass->getDataSize();
160
161 // Align NumBytesAllocated. We should be using TargetData alignment stuff
162 // to determine this, but we don't know the LLVM type associated with the
163 // virtual register. Instead, just align to a multiple of the size for now.
164 NumBytesAllocated += RegSize-1;
165 NumBytesAllocated = NumBytesAllocated/RegSize*RegSize;
166
167 // Assign the slot...
Chris Lattnerad44bd92002-12-15 18:15:24 +0000168 VirtReg2OffsetMap[VirtReg] = NumBytesAllocated;
Chris Lattner9593fb12002-12-15 19:07:34 +0000169
170 // Reserve the space!
171 NumBytesAllocated += RegSize;
Misha Brukmanf514d512002-12-02 21:11:58 +0000172 }
Chris Lattnerad44bd92002-12-15 18:15:24 +0000173 return VirtReg2OffsetMap[VirtReg];
Misha Brukmanf514d512002-12-02 21:11:58 +0000174}
175
Misha Brukman07218672002-11-22 22:44:32 +0000176unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
177 const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
178 unsigned physReg;
179 assert(regClass);
180 if (RegClassIdx.find(regClass) != RegClassIdx.end()) {
181 unsigned regIdx = RegClassIdx[regClass]++;
182 assert(regIdx < regClass->getNumRegs() && "Not enough registers!");
183 physReg = regClass->getRegister(regIdx);
184 } else {
185 physReg = regClass->getRegister(0);
186 // assert(physReg < regClass->getNumRegs() && "No registers in class!");
187 RegClassIdx[regClass] = 1;
188 }
189
190 if (isAvailableReg(physReg))
191 return physReg;
Chris Lattnerda7e4532002-12-15 20:36:09 +0000192 else
Misha Brukman07218672002-11-22 22:44:32 +0000193 return getFreeReg(virtualReg);
Misha Brukman07218672002-11-22 22:44:32 +0000194}
195
196MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000197RegAllocSimple::moveUseToReg (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000198 MachineBasicBlock::iterator I,
Misha Brukman07218672002-11-22 22:44:32 +0000199 unsigned VirtReg, unsigned &PhysReg)
200{
201 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
202 assert(regClass);
203
Misha Brukmanf514d512002-12-02 21:11:58 +0000204 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000205 PhysReg = getFreeReg(VirtReg);
206
Misha Brukmanf514d512002-12-02 21:11:58 +0000207 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000208 ++NumReloaded;
Chris Lattner198ab642002-12-15 20:06:35 +0000209 return RegInfo->loadRegOffset2Reg(MBB, I, PhysReg,
Misha Brukmanf514d512002-12-02 21:11:58 +0000210 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000211 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000212}
213
214MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000215RegAllocSimple::saveVirtRegToStack (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000216 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000217 unsigned VirtReg, unsigned PhysReg)
Misha Brukman07218672002-11-22 22:44:32 +0000218{
219 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
220 assert(regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000221
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000222 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukmanf514d512002-12-02 21:11:58 +0000223
Misha Brukman07218672002-11-22 22:44:32 +0000224 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000225 ++NumSpilled;
Chris Lattner198ab642002-12-15 20:06:35 +0000226 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukman07218672002-11-22 22:44:32 +0000227 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000228 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000229}
230
Misha Brukmandc2ec002002-12-03 23:15:19 +0000231MachineBasicBlock::iterator
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000232RegAllocSimple::savePhysRegToStack (MachineBasicBlock &MBB,
Misha Brukman203b7692002-12-13 09:54:36 +0000233 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000234 unsigned PhysReg)
235{
236 const TargetRegisterClass* regClass = MF->getRegClass(PhysReg);
237 assert(regClass);
238
239 unsigned offset = allocateStackSpaceFor(PhysReg, regClass);
240
241 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000242 ++NumSpilled;
Chris Lattner198ab642002-12-15 20:06:35 +0000243 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000244 RegInfo->getFramePointer(),
245 offset, regClass->getDataSize());
246}
247
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000248/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
249/// predecessor basic blocks.
250void RegAllocSimple::EliminatePHINodes(MachineBasicBlock &MBB) {
Chris Lattnerda7e4532002-12-15 20:36:09 +0000251 const MachineInstrInfo &MII = TM.getInstrInfo();
252
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000253 while (MBB.front()->getOpcode() == 0) {
254 MachineInstr *MI = MBB.front();
Chris Lattnerda7e4532002-12-15 20:36:09 +0000255 // Unlink the PHI node from the basic block... but don't delete the PHI
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000256 MBB.erase(MBB.begin());
257
258 // a preliminary pass that will invalidate any registers that
259 // are used by the instruction (including implicit uses)
260 invalidatePhysRegs(MI);
261
262 DEBUG(std::cerr << "num invalid regs: " << RegsUsed.size() << "\n");
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000263 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
264 MachineOperand &targetReg = MI->getOperand(0);
265
Chris Lattnerda7e4532002-12-15 20:36:09 +0000266 // If it's a virtual register, allocate a physical one otherwise, just use
267 // whatever register is there now note: it MUST be a register -- we're
268 // assigning to it!
269 //
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000270 unsigned virtualReg = (unsigned) targetReg.getAllocatedRegNum();
271 unsigned physReg;
272 if (targetReg.isVirtualRegister()) {
273 physReg = getFreeReg(virtualReg);
274 } else {
275 physReg = virtualReg;
276 }
277
278 // Find the register class of the target register: should be the
279 // same as the values we're trying to store there
280 const TargetRegisterClass* regClass = PhysRegClasses[physReg];
281 assert(regClass && "Target register class not found!");
282 unsigned dataSize = regClass->getDataSize();
Chris Lattner3f91ad72002-12-15 20:48:03 +0000283
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000284 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
285 MachineOperand &opVal = MI->getOperand(i-1);
286
287 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
288 // source path the phi
289 MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000290
Chris Lattner3f91ad72002-12-15 20:48:03 +0000291 // Check to make sure we haven't already emitted the copy for this block.
292 // This can happen because PHI nodes may have multiple entries for the
293 // same basic block. It doesn't matter which entry we use though, because
294 // all incoming values are guaranteed to be the same for a particular bb.
295 //
296 // Note that this is N^2 in the number of phi node entries, but since the
297 // # of entries is tiny, this is not a problem.
298 //
299 bool HaveNotEmitted = true;
300 for (int op = MI->getNumOperands() - 1; op != i; op -= 2)
301 if (&opBlock == MI->getOperand(op).getMachineBasicBlock()) {
302 HaveNotEmitted = false;
303 break;
304 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000305
Chris Lattner3f91ad72002-12-15 20:48:03 +0000306 if (HaveNotEmitted) {
307 MachineBasicBlock::iterator opI = opBlock.end();
308 MachineInstr *opMI = *--opI;
309
310 // must backtrack over ALL the branches in the previous block
311 while (MII.isBranch(opMI->getOpcode()) && opI != opBlock.begin())
312 opMI = *--opI;
313
314 // move back to the first branch instruction so new instructions
315 // are inserted right in front of it and not in front of a non-branch
316 if (!MII.isBranch(opMI->getOpcode()))
317 ++opI;
318
319 // Retrieve the constant value from this op, move it to target
320 // register of the phi
321 if (opVal.isImmediate()) {
322 opI = RegInfo->moveImm2Reg(opBlock, opI, physReg,
323 (unsigned) opVal.getImmedValue(),
324 dataSize);
325 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
326 } else {
327 // Allocate a physical register and add a move in the BB
Chris Lattner8233e2f2002-12-15 21:13:12 +0000328 unsigned opVirtualReg = opVal.getAllocatedRegNum();
Chris Lattner3f91ad72002-12-15 20:48:03 +0000329 unsigned opPhysReg;
330 opI = moveUseToReg(opBlock, opI, opVirtualReg, physReg);
331
332 // Save that register value to the stack of the TARGET REG
333 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
334 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000335 }
Chris Lattner3f91ad72002-12-15 20:48:03 +0000336
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000337 // make regs available to other instructions
338 clearAllRegs();
339 }
340
341 // really delete the instruction
342 delete MI;
343 }
344}
345
346
347void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
348 // Handle PHI instructions specially: add moves to each pred block
349 EliminatePHINodes(MBB);
350
351 //loop over each basic block
352 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000353 // Made to combat the incorrect allocation of r2 = add r1, r1
354 std::map<unsigned, unsigned> VirtReg2PhysRegMap;
355
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000356 MachineInstr *MI = *I;
357
358 // a preliminary pass that will invalidate any registers that
359 // are used by the instruction (including implicit uses)
360 invalidatePhysRegs(MI);
361
362 // Loop over uses, move from memory into registers
363 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
364 MachineOperand &op = MI->getOperand(i);
365
Chris Lattnerda7e4532002-12-15 20:36:09 +0000366 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000367 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
368 DEBUG(std::cerr << "op: " << op << "\n");
369 DEBUG(std::cerr << "\t inst[" << i << "]: ";
370 MI->print(std::cerr, TM));
371
372 // make sure the same virtual register maps to the same physical
373 // register in any given instruction
374 unsigned physReg;
375 if (VirtReg2PhysRegMap.find(virtualReg) != VirtReg2PhysRegMap.end()) {
376 physReg = VirtReg2PhysRegMap[virtualReg];
377 } else {
378 if (op.opIsDef()) {
379 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
380 // must be same register number as the first operand
381 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000382 assert(MI->getOperand(1).isRegister() &&
383 MI->getOperand(1).getAllocatedRegNum() &&
384 MF->getRegClass(virtualReg) ==
385 PhysRegClasses[MI->getOperand(1).getAllocatedRegNum()] &&
386 "Two address instruction invalid!");
387
Chris Lattnerda7e4532002-12-15 20:36:09 +0000388 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000389 } else {
390 physReg = getFreeReg(virtualReg);
391 }
392 MachineBasicBlock::iterator J = I;
393 J = saveVirtRegToStack(MBB, ++J, virtualReg, physReg);
394 I = --J;
395 } else {
396 I = moveUseToReg(MBB, I, virtualReg, physReg);
397 }
398 VirtReg2PhysRegMap[virtualReg] = physReg;
399 }
400 MI->SetMachineOperandReg(i, physReg);
401 DEBUG(std::cerr << "virt: " << virtualReg <<
402 ", phys: " << op.getAllocatedRegNum() << "\n");
403 }
404 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000405 clearAllRegs();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000406 }
407}
408
Chris Lattnerda7e4532002-12-15 20:36:09 +0000409/// runOnMachineFunction - Register allocate the whole function
410///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000411bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000412 DEBUG(std::cerr << "Machine Function " << "\n");
413 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000414
Misha Brukman07218672002-11-22 22:44:32 +0000415 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
416 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000417 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000418
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000419 // add prologue we should preserve callee-save registers...
Chris Lattner198ab642002-12-15 20:06:35 +0000420 RegInfo->emitPrologue(Fn, NumBytesAllocated);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000421
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000422 const MachineInstrInfo &MII = TM.getInstrInfo();
423
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000424 // add epilogue to restore the callee-save registers
425 // loop over the basic block
426 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000427 MBB != MBBe; ++MBB) {
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000428 // check if last instruction is a RET
Chris Lattnerda7e4532002-12-15 20:36:09 +0000429 if (MII.isReturn(MBB->back()->getOpcode())) {
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000430 // this block has a return instruction, add epilogue
Chris Lattner198ab642002-12-15 20:06:35 +0000431 RegInfo->emitEpilogue(*MBB, NumBytesAllocated);
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000432 }
433 }
Misha Brukman07218672002-11-22 22:44:32 +0000434
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000435 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +0000436 return false; // We never modify the LLVM itself.
437}
438
439Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) {
440 return new RegAllocSimple(TM);
441}