blob: b84d2b8ea49b996e0a512757008db3478612fc77 [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
7#include "llvm/Function.h"
8#include "llvm/iTerminators.h"
9#include "llvm/Type.h"
10#include "llvm/Constants.h"
11#include "llvm/Pass.h"
Misha Brukman07218672002-11-22 22:44:32 +000012#include "llvm/CodeGen/MachineFunction.h"
13#include "llvm/CodeGen/MachineInstrBuilder.h"
Misha Brukmandd46e2a2002-12-04 23:58:08 +000014#include "llvm/Target/MachineInstrInfo.h"
Misha Brukman07218672002-11-22 22:44:32 +000015#include "llvm/Target/MRegisterInfo.h"
Misha Brukman07218672002-11-22 22:44:32 +000016#include "llvm/Target/TargetMachine.h"
17#include "llvm/Support/InstVisitor.h"
18#include "Support/Statistic.h"
Misha Brukman07218672002-11-22 22:44:32 +000019
Misha Brukman59b3eed2002-12-13 10:42:31 +000020namespace {
Misha Brukman07218672002-11-22 22:44:32 +000021 struct RegAllocSimple : public FunctionPass {
22 TargetMachine &TM;
23 MachineBasicBlock *CurrMBB;
24 MachineFunction *MF;
25 unsigned maxOffset;
26 const MRegisterInfo *RegInfo;
27 unsigned NumBytesAllocated, ByteAlignment;
28
29 // Maps SSA Regs => offsets on the stack where these values are stored
Chris Lattnerad44bd92002-12-15 18:15:24 +000030 std::map<unsigned, unsigned> VirtReg2OffsetMap;
Misha Brukman07218672002-11-22 22:44:32 +000031
32 // Maps SSA Regs => physical regs
33 std::map<unsigned, unsigned> SSA2PhysRegMap;
Misha Brukmandc2ec002002-12-03 23:15:19 +000034
35 // Maps physical register to their register classes
36 std::map<unsigned, const TargetRegisterClass*> PhysReg2RegClassMap;
Misha Brukmand1bedcc2002-12-12 23:20:31 +000037
38 // Made to combat the incorrect allocation of r2 = add r1, r1
39 std::map<unsigned, unsigned> VirtReg2PhysRegMap;
Misha Brukman07218672002-11-22 22:44:32 +000040
41 // Maps RegClass => which index we can take a register from. Since this is a
42 // simple register allocator, when we need a register of a certain class, we
43 // just take the next available one.
44 std::map<unsigned, unsigned> RegsUsed;
45 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
46
47 RegAllocSimple(TargetMachine &tm) : TM(tm), CurrMBB(0), maxOffset(0),
48 RegInfo(tm.getRegisterInfo()),
Misha Brukmancea22452002-12-13 04:34:02 +000049 ByteAlignment(4)
Misha Brukman07218672002-11-22 22:44:32 +000050 {
Misha Brukmandc2ec002002-12-03 23:15:19 +000051 // build reverse mapping for physReg -> register class
52 RegInfo->buildReg2RegClassMap(PhysReg2RegClassMap);
53
Misha Brukman07218672002-11-22 22:44:32 +000054 RegsUsed[RegInfo->getFramePointer()] = 1;
55 RegsUsed[RegInfo->getStackPointer()] = 1;
Misha Brukmancea22452002-12-13 04:34:02 +000056
57 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +000058 }
59
60 bool isAvailableReg(unsigned Reg) {
61 // assert(Reg < MRegisterInfo::FirstVirtualReg && "...");
62 return RegsUsed.find(Reg) == RegsUsed.end();
63 }
64
Misha Brukmanf514d512002-12-02 21:11:58 +000065 ///
66 unsigned allocateStackSpaceFor(unsigned VirtReg,
67 const TargetRegisterClass *regClass);
68
Misha Brukman07218672002-11-22 22:44:32 +000069 /// Given size (in bytes), returns a register that is currently unused
70 /// Side effect: marks that register as being used until manually cleared
71 unsigned getFreeReg(unsigned virtualReg);
72
73 /// Returns all `borrowed' registers back to the free pool
74 void clearAllRegs() {
75 RegClassIdx.clear();
76 }
77
Misha Brukman972b03f2002-12-13 11:33:22 +000078 /// Invalidates any references, real or implicit, to physical registers
79 ///
80 void invalidatePhysRegs(const MachineInstr *MI) {
81 unsigned Opcode = MI->getOpcode();
82 const MachineInstrInfo &MII = TM.getInstrInfo();
83 const MachineInstrDescriptor &Desc = MII.get(Opcode);
84 const unsigned *regs = Desc.ImplicitUses;
85 while (*regs)
86 RegsUsed[*regs++] = 1;
87
88 regs = Desc.ImplicitDefs;
89 while (*regs)
90 RegsUsed[*regs++] = 1;
Misha Brukman972b03f2002-12-13 11:33:22 +000091 }
92
Misha Brukmandd46e2a2002-12-04 23:58:08 +000093 void cleanupAfterFunction() {
Chris Lattnerad44bd92002-12-15 18:15:24 +000094 VirtReg2OffsetMap.clear();
Misha Brukmandd46e2a2002-12-04 23:58:08 +000095 SSA2PhysRegMap.clear();
Misha Brukman203b7692002-12-13 09:54:36 +000096 NumBytesAllocated = ByteAlignment;
Misha Brukmandd46e2a2002-12-04 23:58:08 +000097 }
98
Misha Brukman07218672002-11-22 22:44:32 +000099 /// Moves value from memory into that register
100 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000101 moveUseToReg (MachineBasicBlock *MBB,
102 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukman07218672002-11-22 22:44:32 +0000103 unsigned &PhysReg);
104
105 /// Saves reg value on the stack (maps virtual register to stack value)
106 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000107 saveVirtRegToStack (MachineBasicBlock *MBB,
108 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000109 unsigned PhysReg);
110
111 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000112 savePhysRegToStack (MachineBasicBlock *MBB,
113 MachineBasicBlock::iterator I, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000114
115 /// runOnFunction - Top level implementation of instruction selection for
116 /// the entire function.
117 ///
118 bool runOnMachineFunction(MachineFunction &Fn);
119
120 bool runOnFunction(Function &Fn) {
121 return runOnMachineFunction(MachineFunction::get(&Fn));
122 }
123 };
124
Misha Brukman59b3eed2002-12-13 10:42:31 +0000125}
Misha Brukman07218672002-11-22 22:44:32 +0000126
Misha Brukmanf514d512002-12-02 21:11:58 +0000127unsigned RegAllocSimple::allocateStackSpaceFor(unsigned VirtReg,
128 const TargetRegisterClass *regClass)
129{
Chris Lattnerad44bd92002-12-15 18:15:24 +0000130 if (VirtReg2OffsetMap.find(VirtReg) == VirtReg2OffsetMap.end()) {
Misha Brukmancea22452002-12-13 04:34:02 +0000131#if 0
Misha Brukmanf514d512002-12-02 21:11:58 +0000132 unsigned size = regClass->getDataSize();
133 unsigned over = NumBytesAllocated - (NumBytesAllocated % ByteAlignment);
134 if (size >= ByteAlignment - over) {
135 // need to pad by (ByteAlignment - over)
136 NumBytesAllocated += ByteAlignment - over;
137 }
Chris Lattnerad44bd92002-12-15 18:15:24 +0000138 VirtReg2OffsetMap[VirtReg] = NumBytesAllocated;
Misha Brukmanf514d512002-12-02 21:11:58 +0000139 NumBytesAllocated += size;
Misha Brukmancea22452002-12-13 04:34:02 +0000140#endif
141 // FIXME: forcing each arg to take 4 bytes on the stack
Chris Lattnerad44bd92002-12-15 18:15:24 +0000142 VirtReg2OffsetMap[VirtReg] = NumBytesAllocated;
Misha Brukman203b7692002-12-13 09:54:36 +0000143 NumBytesAllocated += ByteAlignment;
Misha Brukmanf514d512002-12-02 21:11:58 +0000144 }
Chris Lattnerad44bd92002-12-15 18:15:24 +0000145 return VirtReg2OffsetMap[VirtReg];
Misha Brukmanf514d512002-12-02 21:11:58 +0000146}
147
Misha Brukman07218672002-11-22 22:44:32 +0000148unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
149 const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
150 unsigned physReg;
151 assert(regClass);
152 if (RegClassIdx.find(regClass) != RegClassIdx.end()) {
153 unsigned regIdx = RegClassIdx[regClass]++;
154 assert(regIdx < regClass->getNumRegs() && "Not enough registers!");
155 physReg = regClass->getRegister(regIdx);
156 } else {
157 physReg = regClass->getRegister(0);
158 // assert(physReg < regClass->getNumRegs() && "No registers in class!");
159 RegClassIdx[regClass] = 1;
160 }
161
162 if (isAvailableReg(physReg))
163 return physReg;
164 else {
165 return getFreeReg(virtualReg);
166 }
167}
168
169MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000170RegAllocSimple::moveUseToReg (MachineBasicBlock *MBB,
171 MachineBasicBlock::iterator I,
Misha Brukman07218672002-11-22 22:44:32 +0000172 unsigned VirtReg, unsigned &PhysReg)
173{
174 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
175 assert(regClass);
176
Misha Brukmanf514d512002-12-02 21:11:58 +0000177 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000178 PhysReg = getFreeReg(VirtReg);
179
Misha Brukmanf514d512002-12-02 21:11:58 +0000180 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000181 return RegInfo->loadRegOffset2Reg(MBB, I, PhysReg,
Misha Brukmanf514d512002-12-02 21:11:58 +0000182 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000183 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000184}
185
186MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000187RegAllocSimple::saveVirtRegToStack (MachineBasicBlock *MBB,
188 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000189 unsigned VirtReg, unsigned PhysReg)
Misha Brukman07218672002-11-22 22:44:32 +0000190{
191 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
192 assert(regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000193
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000194 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukmanf514d512002-12-02 21:11:58 +0000195
Misha Brukman07218672002-11-22 22:44:32 +0000196 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000197 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukman07218672002-11-22 22:44:32 +0000198 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000199 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000200}
201
Misha Brukmandc2ec002002-12-03 23:15:19 +0000202MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000203RegAllocSimple::savePhysRegToStack (MachineBasicBlock *MBB,
204 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000205 unsigned PhysReg)
206{
207 const TargetRegisterClass* regClass = MF->getRegClass(PhysReg);
208 assert(regClass);
209
210 unsigned offset = allocateStackSpaceFor(PhysReg, regClass);
211
212 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000213 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000214 RegInfo->getFramePointer(),
215 offset, regClass->getDataSize());
216}
217
Misha Brukman07218672002-11-22 22:44:32 +0000218bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000219 cleanupAfterFunction();
220
Misha Brukman07218672002-11-22 22:44:32 +0000221 unsigned virtualReg, physReg;
222 DEBUG(std::cerr << "Machine Function " << "\n");
223 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000224
Misha Brukman07218672002-11-22 22:44:32 +0000225 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
226 MBB != MBBe; ++MBB)
227 {
228 CurrMBB = &(*MBB);
229
Misha Brukman203b7692002-12-13 09:54:36 +0000230 // Handle PHI instructions specially: add moves to each pred block
231 while (MBB->front()->getOpcode() == 0) {
232 MachineInstr *MI = MBB->front();
233 // get rid of the phi
234 MBB->erase(MBB->begin());
235
Misha Brukman972b03f2002-12-13 11:33:22 +0000236 // a preliminary pass that will invalidate any registers that
237 // are used by the instruction (including implicit uses)
238 invalidatePhysRegs(MI);
239
240 DEBUG(std::cerr << "num invalid regs: " << RegsUsed.size() << "\n");
241
Misha Brukman203b7692002-12-13 09:54:36 +0000242 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
243 MachineOperand &targetReg = MI->getOperand(0);
244
245 // If it's a virtual register, allocate a physical one
246 // otherwise, just use whatever register is there now
247 // note: it MUST be a register -- we're assigning to it
248 virtualReg = (unsigned) targetReg.getAllocatedRegNum();
249 if (targetReg.isVirtualRegister()) {
250 physReg = getFreeReg(virtualReg);
251 } else {
Misha Brukman08686672002-12-13 12:33:31 +0000252 physReg = virtualReg;
Misha Brukman203b7692002-12-13 09:54:36 +0000253 }
254
255 // Find the register class of the target register: should be the
256 // same as the values we're trying to store there
257 const TargetRegisterClass* regClass = PhysReg2RegClassMap[physReg];
258 assert(regClass && "Target register class not found!");
259 unsigned dataSize = regClass->getDataSize();
260
261 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
262 MachineOperand &opVal = MI->getOperand(i-1);
263
264 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
265 // source path the phi
Chris Lattner71c83722002-12-15 08:02:51 +0000266 MachineBasicBlock *opBlock = MI->getOperand(i).getMachineBasicBlock();
Misha Brukman203b7692002-12-13 09:54:36 +0000267 MachineBasicBlock::iterator opI = opBlock->end();
268 MachineInstr *opMI = *(--opI);
269 const MachineInstrInfo &MII = TM.getInstrInfo();
Misha Brukman74676da2002-12-13 11:55:59 +0000270 // must backtrack over ALL the branches in the previous block, until no more
271 while ((MII.isBranch(opMI->getOpcode()) || MII.isReturn(opMI->getOpcode()))
272 && opI != opBlock->begin())
273 {
274 opMI = *(--opI);
275 }
276 // move back to the first branch instruction so new instructions
277 // are inserted right in front of it and not in front of a non-branch
278 ++opI;
279
Misha Brukman203b7692002-12-13 09:54:36 +0000280
Misha Brukman08686672002-12-13 12:33:31 +0000281 // Retrieve the constant value from this op, move it to target
282 // register of the phi
283 if (opVal.getType() == MachineOperand::MO_SignExtendedImmed ||
284 opVal.getType() == MachineOperand::MO_UnextendedImmed)
Misha Brukman203b7692002-12-13 09:54:36 +0000285 {
Misha Brukman08686672002-12-13 12:33:31 +0000286 opI = RegInfo->moveImm2Reg(opBlock, opI, physReg,
287 (unsigned) opVal.getImmedValue(),
288 dataSize);
289 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
290 } else {
291 // Allocate a physical register and add a move in the BB
292 unsigned opVirtualReg = (unsigned) opVal.getAllocatedRegNum();
293 unsigned opPhysReg; // = getFreeReg(opVirtualReg);
294 opI = moveUseToReg(opBlock, opI, opVirtualReg, physReg);
295 //opI = RegInfo->moveReg2Reg(opBlock, opI, physReg, opPhysReg,
296 // dataSize);
297 // Save that register value to the stack of the TARGET REG
298 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
299 }
Misha Brukman972b03f2002-12-13 11:33:22 +0000300
301 // make regs available to other instructions
302 clearAllRegs();
Misha Brukman203b7692002-12-13 09:54:36 +0000303 }
304
305 // really delete the instruction
306 delete MI;
307 }
308
Misha Brukman07218672002-11-22 22:44:32 +0000309 //loop over each basic block
310 for (MachineBasicBlock::iterator I = MBB->begin(); I != MBB->end(); ++I)
311 {
312 MachineInstr *MI = *I;
313
Misha Brukman972b03f2002-12-13 11:33:22 +0000314 // a preliminary pass that will invalidate any registers that
Misha Brukmandc2ec002002-12-03 23:15:19 +0000315 // are used by the instruction (including implicit uses)
Misha Brukman972b03f2002-12-13 11:33:22 +0000316 invalidatePhysRegs(MI);
Misha Brukmandc2ec002002-12-03 23:15:19 +0000317
Misha Brukman203b7692002-12-13 09:54:36 +0000318 // Loop over uses, move from memory into registers
Misha Brukman07218672002-11-22 22:44:32 +0000319 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
320 MachineOperand &op = MI->getOperand(i);
321
322 if (op.getType() == MachineOperand::MO_SignExtendedImmed ||
323 op.getType() == MachineOperand::MO_UnextendedImmed)
324 {
325 DEBUG(std::cerr << "const\n");
326 } else if (op.isVirtualRegister()) {
327 virtualReg = (unsigned) op.getAllocatedRegNum();
Misha Brukman07218672002-11-22 22:44:32 +0000328 DEBUG(std::cerr << "op: " << op << "\n");
329 DEBUG(std::cerr << "\t inst[" << i << "]: ";
330 MI->print(std::cerr, TM));
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000331
332 // make sure the same virtual register maps to the same physical
333 // register in any given instruction
334 if (VirtReg2PhysRegMap.find(virtualReg) != VirtReg2PhysRegMap.end()) {
335 physReg = VirtReg2PhysRegMap[virtualReg];
Misha Brukmanf514d512002-12-02 21:11:58 +0000336 } else {
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000337 if (op.opIsDef()) {
338 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
339 // must be same register number as the first operand
340 // This maps a = b + c into b += c, and saves b into a's spot
341 physReg = (unsigned) MI->getOperand(1).getAllocatedRegNum();
342 } else {
343 physReg = getFreeReg(virtualReg);
344 }
345 MachineBasicBlock::iterator J = I;
Misha Brukman203b7692002-12-13 09:54:36 +0000346 J = saveVirtRegToStack(CurrMBB, ++J, virtualReg, physReg);
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000347 I = --J;
348 } else {
Misha Brukman203b7692002-12-13 09:54:36 +0000349 I = moveUseToReg(CurrMBB, I, virtualReg, physReg);
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000350 }
351 VirtReg2PhysRegMap[virtualReg] = physReg;
Misha Brukmanf514d512002-12-02 21:11:58 +0000352 }
353 MI->SetMachineOperandReg(i, physReg);
Misha Brukman07218672002-11-22 22:44:32 +0000354 DEBUG(std::cerr << "virt: " << virtualReg <<
355 ", phys: " << op.getAllocatedRegNum() << "\n");
356 }
357 }
358
359 clearAllRegs();
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000360 VirtReg2PhysRegMap.clear();
Misha Brukman07218672002-11-22 22:44:32 +0000361 }
362
363 }
364
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000365 // add prologue we should preserve callee-save registers...
366 MachineFunction::iterator Fi = Fn.begin();
367 MachineBasicBlock *MBB = Fi;
368 MachineBasicBlock::iterator MBBi = MBB->begin();
369 RegInfo->emitPrologue(MBB, MBBi, NumBytesAllocated);
370
371 // add epilogue to restore the callee-save registers
372 // loop over the basic block
373 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
374 MBB != MBBe; ++MBB)
375 {
376 // check if last instruction is a RET
377 MachineBasicBlock::iterator I = (*MBB).end();
378 MachineInstr *MI = *(--I);
379 const MachineInstrInfo &MII = TM.getInstrInfo();
380 if (MII.isReturn(MI->getOpcode())) {
381 // this block has a return instruction, add epilogue
382 RegInfo->emitEpilogue(MBB, I, NumBytesAllocated);
383 }
384 }
Misha Brukman07218672002-11-22 22:44:32 +0000385
386 return false; // We never modify the LLVM itself.
387}
388
389Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) {
390 return new RegAllocSimple(TM);
391}