blob: 978e927c36ed702e3c565645b3de1e163fb21925 [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>
Misha Brukman07218672002-11-22 22:44:32 +000013
Chris Lattnerdd444f92002-12-15 18:38:59 +000014/// PhysRegClassMap - Construct a mapping of physical register numbers to their
15/// register classes.
16///
17/// NOTE: This class will eventually be pulled out to somewhere shared.
18///
19class PhysRegClassMap {
20 std::map<unsigned, const TargetRegisterClass*> PhysReg2RegClassMap;
21public:
22 PhysRegClassMap(const MRegisterInfo *RI) {
23 for (MRegisterInfo::const_iterator I = RI->regclass_begin(),
24 E = RI->regclass_end(); I != E; ++I)
25 for (unsigned i=0; i < (*I)->getNumRegs(); ++i)
26 PhysReg2RegClassMap[(*I)->getRegister(i)] = *I;
27 }
28
29 const TargetRegisterClass *operator[](unsigned Reg) {
30 assert(PhysReg2RegClassMap[Reg] && "Register is not a known physreg!");
31 return PhysReg2RegClassMap[Reg];
32 }
33
34 const TargetRegisterClass *get(unsigned Reg) { return operator[](Reg); }
35};
36
37
Misha Brukman59b3eed2002-12-13 10:42:31 +000038namespace {
Misha Brukman07218672002-11-22 22:44:32 +000039 struct RegAllocSimple : public FunctionPass {
40 TargetMachine &TM;
Misha Brukman07218672002-11-22 22:44:32 +000041 MachineFunction *MF;
Misha Brukman07218672002-11-22 22:44:32 +000042 const MRegisterInfo *RegInfo;
Chris Lattner9593fb12002-12-15 19:07:34 +000043 unsigned NumBytesAllocated;
Misha Brukman07218672002-11-22 22:44:32 +000044
45 // Maps SSA Regs => offsets on the stack where these values are stored
Chris Lattnerad44bd92002-12-15 18:15:24 +000046 std::map<unsigned, unsigned> VirtReg2OffsetMap;
Misha Brukman07218672002-11-22 22:44:32 +000047
48 // Maps SSA Regs => physical regs
49 std::map<unsigned, unsigned> SSA2PhysRegMap;
Misha Brukmandc2ec002002-12-03 23:15:19 +000050
51 // Maps physical register to their register classes
Chris Lattnerdd444f92002-12-15 18:38:59 +000052 PhysRegClassMap PhysRegClasses;
Misha Brukmand1bedcc2002-12-12 23:20:31 +000053
54 // Made to combat the incorrect allocation of r2 = add r1, r1
55 std::map<unsigned, unsigned> VirtReg2PhysRegMap;
Misha Brukman07218672002-11-22 22:44:32 +000056
57 // Maps RegClass => which index we can take a register from. Since this is a
58 // simple register allocator, when we need a register of a certain class, we
59 // just take the next available one.
60 std::map<unsigned, unsigned> RegsUsed;
61 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
62
Chris Lattner9593fb12002-12-15 19:07:34 +000063 RegAllocSimple(TargetMachine &tm) : TM(tm),
Misha Brukman07218672002-11-22 22:44:32 +000064 RegInfo(tm.getRegisterInfo()),
Chris Lattnerdd444f92002-12-15 18:38:59 +000065 PhysRegClasses(RegInfo)
Misha Brukman07218672002-11-22 22:44:32 +000066 {
67 RegsUsed[RegInfo->getFramePointer()] = 1;
68 RegsUsed[RegInfo->getStackPointer()] = 1;
Misha Brukmancea22452002-12-13 04:34:02 +000069
70 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +000071 }
72
73 bool isAvailableReg(unsigned Reg) {
74 // assert(Reg < MRegisterInfo::FirstVirtualReg && "...");
75 return RegsUsed.find(Reg) == RegsUsed.end();
76 }
77
Misha Brukmanf514d512002-12-02 21:11:58 +000078 ///
79 unsigned allocateStackSpaceFor(unsigned VirtReg,
80 const TargetRegisterClass *regClass);
81
Misha Brukman07218672002-11-22 22:44:32 +000082 /// Given size (in bytes), returns a register that is currently unused
83 /// Side effect: marks that register as being used until manually cleared
84 unsigned getFreeReg(unsigned virtualReg);
85
86 /// Returns all `borrowed' registers back to the free pool
87 void clearAllRegs() {
88 RegClassIdx.clear();
89 }
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();
95 const MachineInstrInfo &MII = TM.getInstrInfo();
96 const MachineInstrDescriptor &Desc = MII.get(Opcode);
97 const unsigned *regs = Desc.ImplicitUses;
98 while (*regs)
99 RegsUsed[*regs++] = 1;
100
101 regs = Desc.ImplicitDefs;
102 while (*regs)
103 RegsUsed[*regs++] = 1;
Misha Brukman972b03f2002-12-13 11:33:22 +0000104 }
105
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000106 void cleanupAfterFunction() {
Chris Lattnerad44bd92002-12-15 18:15:24 +0000107 VirtReg2OffsetMap.clear();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000108 SSA2PhysRegMap.clear();
Chris Lattner9593fb12002-12-15 19:07:34 +0000109 NumBytesAllocated = 4; /* FIXME: This is X86 specific */
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000110 }
111
Misha Brukman07218672002-11-22 22:44:32 +0000112 /// Moves value from memory into that register
113 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000114 moveUseToReg (MachineBasicBlock *MBB,
115 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukman07218672002-11-22 22:44:32 +0000116 unsigned &PhysReg);
117
118 /// Saves reg value on the stack (maps virtual register to stack value)
119 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000120 saveVirtRegToStack (MachineBasicBlock *MBB,
121 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000122 unsigned PhysReg);
123
124 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000125 savePhysRegToStack (MachineBasicBlock *MBB,
126 MachineBasicBlock::iterator I, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000127
128 /// runOnFunction - Top level implementation of instruction selection for
129 /// the entire function.
130 ///
131 bool runOnMachineFunction(MachineFunction &Fn);
132
133 bool runOnFunction(Function &Fn) {
134 return runOnMachineFunction(MachineFunction::get(&Fn));
135 }
136 };
137
Misha Brukman59b3eed2002-12-13 10:42:31 +0000138}
Misha Brukman07218672002-11-22 22:44:32 +0000139
Misha Brukmanf514d512002-12-02 21:11:58 +0000140unsigned RegAllocSimple::allocateStackSpaceFor(unsigned VirtReg,
141 const TargetRegisterClass *regClass)
142{
Chris Lattnerad44bd92002-12-15 18:15:24 +0000143 if (VirtReg2OffsetMap.find(VirtReg) == VirtReg2OffsetMap.end()) {
Chris Lattner9593fb12002-12-15 19:07:34 +0000144 unsigned RegSize = regClass->getDataSize();
145
146 // Align NumBytesAllocated. We should be using TargetData alignment stuff
147 // to determine this, but we don't know the LLVM type associated with the
148 // virtual register. Instead, just align to a multiple of the size for now.
149 NumBytesAllocated += RegSize-1;
150 NumBytesAllocated = NumBytesAllocated/RegSize*RegSize;
151
152 // Assign the slot...
Chris Lattnerad44bd92002-12-15 18:15:24 +0000153 VirtReg2OffsetMap[VirtReg] = NumBytesAllocated;
Chris Lattner9593fb12002-12-15 19:07:34 +0000154
155 // Reserve the space!
156 NumBytesAllocated += RegSize;
Misha Brukmanf514d512002-12-02 21:11:58 +0000157 }
Chris Lattnerad44bd92002-12-15 18:15:24 +0000158 return VirtReg2OffsetMap[VirtReg];
Misha Brukmanf514d512002-12-02 21:11:58 +0000159}
160
Misha Brukman07218672002-11-22 22:44:32 +0000161unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
162 const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
163 unsigned physReg;
164 assert(regClass);
165 if (RegClassIdx.find(regClass) != RegClassIdx.end()) {
166 unsigned regIdx = RegClassIdx[regClass]++;
167 assert(regIdx < regClass->getNumRegs() && "Not enough registers!");
168 physReg = regClass->getRegister(regIdx);
169 } else {
170 physReg = regClass->getRegister(0);
171 // assert(physReg < regClass->getNumRegs() && "No registers in class!");
172 RegClassIdx[regClass] = 1;
173 }
174
175 if (isAvailableReg(physReg))
176 return physReg;
177 else {
178 return getFreeReg(virtualReg);
179 }
180}
181
182MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000183RegAllocSimple::moveUseToReg (MachineBasicBlock *MBB,
184 MachineBasicBlock::iterator I,
Misha Brukman07218672002-11-22 22:44:32 +0000185 unsigned VirtReg, unsigned &PhysReg)
186{
187 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
188 assert(regClass);
189
Misha Brukmanf514d512002-12-02 21:11:58 +0000190 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000191 PhysReg = getFreeReg(VirtReg);
192
Misha Brukmanf514d512002-12-02 21:11:58 +0000193 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000194 return RegInfo->loadRegOffset2Reg(MBB, I, PhysReg,
Misha Brukmanf514d512002-12-02 21:11:58 +0000195 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000196 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000197}
198
199MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000200RegAllocSimple::saveVirtRegToStack (MachineBasicBlock *MBB,
201 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000202 unsigned VirtReg, unsigned PhysReg)
Misha Brukman07218672002-11-22 22:44:32 +0000203{
204 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
205 assert(regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000206
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000207 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukmanf514d512002-12-02 21:11:58 +0000208
Misha Brukman07218672002-11-22 22:44:32 +0000209 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000210 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukman07218672002-11-22 22:44:32 +0000211 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000212 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000213}
214
Misha Brukmandc2ec002002-12-03 23:15:19 +0000215MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000216RegAllocSimple::savePhysRegToStack (MachineBasicBlock *MBB,
217 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000218 unsigned PhysReg)
219{
220 const TargetRegisterClass* regClass = MF->getRegClass(PhysReg);
221 assert(regClass);
222
223 unsigned offset = allocateStackSpaceFor(PhysReg, regClass);
224
225 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000226 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000227 RegInfo->getFramePointer(),
228 offset, regClass->getDataSize());
229}
230
Misha Brukman07218672002-11-22 22:44:32 +0000231bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000232 cleanupAfterFunction();
233
Misha Brukman07218672002-11-22 22:44:32 +0000234 unsigned virtualReg, physReg;
235 DEBUG(std::cerr << "Machine Function " << "\n");
236 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000237
Misha Brukman07218672002-11-22 22:44:32 +0000238 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
239 MBB != MBBe; ++MBB)
240 {
Chris Lattner9593fb12002-12-15 19:07:34 +0000241 MachineBasicBlock *CurrMBB = &(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000242
Misha Brukman203b7692002-12-13 09:54:36 +0000243 // Handle PHI instructions specially: add moves to each pred block
244 while (MBB->front()->getOpcode() == 0) {
245 MachineInstr *MI = MBB->front();
246 // get rid of the phi
247 MBB->erase(MBB->begin());
248
Misha Brukman972b03f2002-12-13 11:33:22 +0000249 // a preliminary pass that will invalidate any registers that
250 // are used by the instruction (including implicit uses)
251 invalidatePhysRegs(MI);
252
253 DEBUG(std::cerr << "num invalid regs: " << RegsUsed.size() << "\n");
254
Misha Brukman203b7692002-12-13 09:54:36 +0000255 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
256 MachineOperand &targetReg = MI->getOperand(0);
257
258 // If it's a virtual register, allocate a physical one
259 // otherwise, just use whatever register is there now
260 // note: it MUST be a register -- we're assigning to it
261 virtualReg = (unsigned) targetReg.getAllocatedRegNum();
262 if (targetReg.isVirtualRegister()) {
263 physReg = getFreeReg(virtualReg);
264 } else {
Misha Brukman08686672002-12-13 12:33:31 +0000265 physReg = virtualReg;
Misha Brukman203b7692002-12-13 09:54:36 +0000266 }
267
268 // Find the register class of the target register: should be the
269 // same as the values we're trying to store there
Chris Lattnerdd444f92002-12-15 18:38:59 +0000270 const TargetRegisterClass* regClass = PhysRegClasses[physReg];
Misha Brukman203b7692002-12-13 09:54:36 +0000271 assert(regClass && "Target register class not found!");
272 unsigned dataSize = regClass->getDataSize();
273
274 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
275 MachineOperand &opVal = MI->getOperand(i-1);
276
277 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
278 // source path the phi
Chris Lattner71c83722002-12-15 08:02:51 +0000279 MachineBasicBlock *opBlock = MI->getOperand(i).getMachineBasicBlock();
Misha Brukman203b7692002-12-13 09:54:36 +0000280 MachineBasicBlock::iterator opI = opBlock->end();
281 MachineInstr *opMI = *(--opI);
282 const MachineInstrInfo &MII = TM.getInstrInfo();
Misha Brukman74676da2002-12-13 11:55:59 +0000283 // must backtrack over ALL the branches in the previous block, until no more
284 while ((MII.isBranch(opMI->getOpcode()) || MII.isReturn(opMI->getOpcode()))
285 && opI != opBlock->begin())
286 {
287 opMI = *(--opI);
288 }
289 // move back to the first branch instruction so new instructions
290 // are inserted right in front of it and not in front of a non-branch
291 ++opI;
292
Misha Brukman203b7692002-12-13 09:54:36 +0000293
Misha Brukman08686672002-12-13 12:33:31 +0000294 // Retrieve the constant value from this op, move it to target
295 // register of the phi
296 if (opVal.getType() == MachineOperand::MO_SignExtendedImmed ||
297 opVal.getType() == MachineOperand::MO_UnextendedImmed)
Misha Brukman203b7692002-12-13 09:54:36 +0000298 {
Misha Brukman08686672002-12-13 12:33:31 +0000299 opI = RegInfo->moveImm2Reg(opBlock, opI, physReg,
300 (unsigned) opVal.getImmedValue(),
301 dataSize);
302 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
303 } else {
304 // Allocate a physical register and add a move in the BB
305 unsigned opVirtualReg = (unsigned) opVal.getAllocatedRegNum();
306 unsigned opPhysReg; // = getFreeReg(opVirtualReg);
307 opI = moveUseToReg(opBlock, opI, opVirtualReg, physReg);
308 //opI = RegInfo->moveReg2Reg(opBlock, opI, physReg, opPhysReg,
309 // dataSize);
310 // Save that register value to the stack of the TARGET REG
311 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
312 }
Misha Brukman972b03f2002-12-13 11:33:22 +0000313
314 // make regs available to other instructions
315 clearAllRegs();
Misha Brukman203b7692002-12-13 09:54:36 +0000316 }
317
318 // really delete the instruction
319 delete MI;
320 }
321
Misha Brukman07218672002-11-22 22:44:32 +0000322 //loop over each basic block
323 for (MachineBasicBlock::iterator I = MBB->begin(); I != MBB->end(); ++I)
324 {
325 MachineInstr *MI = *I;
326
Misha Brukman972b03f2002-12-13 11:33:22 +0000327 // a preliminary pass that will invalidate any registers that
Misha Brukmandc2ec002002-12-03 23:15:19 +0000328 // are used by the instruction (including implicit uses)
Misha Brukman972b03f2002-12-13 11:33:22 +0000329 invalidatePhysRegs(MI);
Misha Brukmandc2ec002002-12-03 23:15:19 +0000330
Misha Brukman203b7692002-12-13 09:54:36 +0000331 // Loop over uses, move from memory into registers
Misha Brukman07218672002-11-22 22:44:32 +0000332 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
333 MachineOperand &op = MI->getOperand(i);
334
335 if (op.getType() == MachineOperand::MO_SignExtendedImmed ||
336 op.getType() == MachineOperand::MO_UnextendedImmed)
337 {
338 DEBUG(std::cerr << "const\n");
339 } else if (op.isVirtualRegister()) {
340 virtualReg = (unsigned) op.getAllocatedRegNum();
Misha Brukman07218672002-11-22 22:44:32 +0000341 DEBUG(std::cerr << "op: " << op << "\n");
342 DEBUG(std::cerr << "\t inst[" << i << "]: ";
343 MI->print(std::cerr, TM));
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000344
345 // make sure the same virtual register maps to the same physical
346 // register in any given instruction
347 if (VirtReg2PhysRegMap.find(virtualReg) != VirtReg2PhysRegMap.end()) {
348 physReg = VirtReg2PhysRegMap[virtualReg];
Misha Brukmanf514d512002-12-02 21:11:58 +0000349 } else {
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000350 if (op.opIsDef()) {
351 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
352 // must be same register number as the first operand
353 // This maps a = b + c into b += c, and saves b into a's spot
354 physReg = (unsigned) MI->getOperand(1).getAllocatedRegNum();
355 } else {
356 physReg = getFreeReg(virtualReg);
357 }
358 MachineBasicBlock::iterator J = I;
Misha Brukman203b7692002-12-13 09:54:36 +0000359 J = saveVirtRegToStack(CurrMBB, ++J, virtualReg, physReg);
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000360 I = --J;
361 } else {
Misha Brukman203b7692002-12-13 09:54:36 +0000362 I = moveUseToReg(CurrMBB, I, virtualReg, physReg);
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000363 }
364 VirtReg2PhysRegMap[virtualReg] = physReg;
Misha Brukmanf514d512002-12-02 21:11:58 +0000365 }
366 MI->SetMachineOperandReg(i, physReg);
Misha Brukman07218672002-11-22 22:44:32 +0000367 DEBUG(std::cerr << "virt: " << virtualReg <<
368 ", phys: " << op.getAllocatedRegNum() << "\n");
369 }
370 }
371
372 clearAllRegs();
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000373 VirtReg2PhysRegMap.clear();
Misha Brukman07218672002-11-22 22:44:32 +0000374 }
375
376 }
377
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000378 // add prologue we should preserve callee-save registers...
379 MachineFunction::iterator Fi = Fn.begin();
380 MachineBasicBlock *MBB = Fi;
381 MachineBasicBlock::iterator MBBi = MBB->begin();
382 RegInfo->emitPrologue(MBB, MBBi, NumBytesAllocated);
383
384 // add epilogue to restore the callee-save registers
385 // loop over the basic block
386 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
387 MBB != MBBe; ++MBB)
388 {
389 // check if last instruction is a RET
390 MachineBasicBlock::iterator I = (*MBB).end();
391 MachineInstr *MI = *(--I);
392 const MachineInstrInfo &MII = TM.getInstrInfo();
393 if (MII.isReturn(MI->getOpcode())) {
394 // this block has a return instruction, add epilogue
395 RegInfo->emitEpilogue(MBB, I, NumBytesAllocated);
396 }
397 }
Misha Brukman07218672002-11-22 22:44:32 +0000398
399 return false; // We never modify the LLVM itself.
400}
401
402Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) {
403 return new RegAllocSimple(TM);
404}