blob: 4745e1113c534940ea7423ccee762512d6e442c1 [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;
41 MachineBasicBlock *CurrMBB;
42 MachineFunction *MF;
43 unsigned maxOffset;
44 const MRegisterInfo *RegInfo;
45 unsigned NumBytesAllocated, ByteAlignment;
46
47 // Maps SSA Regs => offsets on the stack where these values are stored
Chris Lattnerad44bd92002-12-15 18:15:24 +000048 std::map<unsigned, unsigned> VirtReg2OffsetMap;
Misha Brukman07218672002-11-22 22:44:32 +000049
50 // Maps SSA Regs => physical regs
51 std::map<unsigned, unsigned> SSA2PhysRegMap;
Misha Brukmandc2ec002002-12-03 23:15:19 +000052
53 // Maps physical register to their register classes
Chris Lattnerdd444f92002-12-15 18:38:59 +000054 PhysRegClassMap PhysRegClasses;
Misha Brukmand1bedcc2002-12-12 23:20:31 +000055
56 // Made to combat the incorrect allocation of r2 = add r1, r1
57 std::map<unsigned, unsigned> VirtReg2PhysRegMap;
Misha Brukman07218672002-11-22 22:44:32 +000058
59 // Maps RegClass => which index we can take a register from. Since this is a
60 // simple register allocator, when we need a register of a certain class, we
61 // just take the next available one.
62 std::map<unsigned, unsigned> RegsUsed;
63 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
64
65 RegAllocSimple(TargetMachine &tm) : TM(tm), CurrMBB(0), maxOffset(0),
66 RegInfo(tm.getRegisterInfo()),
Chris Lattnerdd444f92002-12-15 18:38:59 +000067 ByteAlignment(4),
68 PhysRegClasses(RegInfo)
Misha Brukman07218672002-11-22 22:44:32 +000069 {
70 RegsUsed[RegInfo->getFramePointer()] = 1;
71 RegsUsed[RegInfo->getStackPointer()] = 1;
Misha Brukmancea22452002-12-13 04:34:02 +000072
73 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +000074 }
75
76 bool isAvailableReg(unsigned Reg) {
77 // assert(Reg < MRegisterInfo::FirstVirtualReg && "...");
78 return RegsUsed.find(Reg) == RegsUsed.end();
79 }
80
Misha Brukmanf514d512002-12-02 21:11:58 +000081 ///
82 unsigned allocateStackSpaceFor(unsigned VirtReg,
83 const TargetRegisterClass *regClass);
84
Misha Brukman07218672002-11-22 22:44:32 +000085 /// Given size (in bytes), returns a register that is currently unused
86 /// Side effect: marks that register as being used until manually cleared
87 unsigned getFreeReg(unsigned virtualReg);
88
89 /// Returns all `borrowed' registers back to the free pool
90 void clearAllRegs() {
91 RegClassIdx.clear();
92 }
93
Misha Brukman972b03f2002-12-13 11:33:22 +000094 /// Invalidates any references, real or implicit, to physical registers
95 ///
96 void invalidatePhysRegs(const MachineInstr *MI) {
97 unsigned Opcode = MI->getOpcode();
98 const MachineInstrInfo &MII = TM.getInstrInfo();
99 const MachineInstrDescriptor &Desc = MII.get(Opcode);
100 const unsigned *regs = Desc.ImplicitUses;
101 while (*regs)
102 RegsUsed[*regs++] = 1;
103
104 regs = Desc.ImplicitDefs;
105 while (*regs)
106 RegsUsed[*regs++] = 1;
Misha Brukman972b03f2002-12-13 11:33:22 +0000107 }
108
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000109 void cleanupAfterFunction() {
Chris Lattnerad44bd92002-12-15 18:15:24 +0000110 VirtReg2OffsetMap.clear();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000111 SSA2PhysRegMap.clear();
Misha Brukman203b7692002-12-13 09:54:36 +0000112 NumBytesAllocated = ByteAlignment;
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000113 }
114
Misha Brukman07218672002-11-22 22:44:32 +0000115 /// Moves value from memory into that register
116 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000117 moveUseToReg (MachineBasicBlock *MBB,
118 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukman07218672002-11-22 22:44:32 +0000119 unsigned &PhysReg);
120
121 /// Saves reg value on the stack (maps virtual register to stack value)
122 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000123 saveVirtRegToStack (MachineBasicBlock *MBB,
124 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000125 unsigned PhysReg);
126
127 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000128 savePhysRegToStack (MachineBasicBlock *MBB,
129 MachineBasicBlock::iterator I, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000130
131 /// runOnFunction - Top level implementation of instruction selection for
132 /// the entire function.
133 ///
134 bool runOnMachineFunction(MachineFunction &Fn);
135
136 bool runOnFunction(Function &Fn) {
137 return runOnMachineFunction(MachineFunction::get(&Fn));
138 }
139 };
140
Misha Brukman59b3eed2002-12-13 10:42:31 +0000141}
Misha Brukman07218672002-11-22 22:44:32 +0000142
Misha Brukmanf514d512002-12-02 21:11:58 +0000143unsigned RegAllocSimple::allocateStackSpaceFor(unsigned VirtReg,
144 const TargetRegisterClass *regClass)
145{
Chris Lattnerad44bd92002-12-15 18:15:24 +0000146 if (VirtReg2OffsetMap.find(VirtReg) == VirtReg2OffsetMap.end()) {
Misha Brukmancea22452002-12-13 04:34:02 +0000147#if 0
Misha Brukmanf514d512002-12-02 21:11:58 +0000148 unsigned size = regClass->getDataSize();
149 unsigned over = NumBytesAllocated - (NumBytesAllocated % ByteAlignment);
150 if (size >= ByteAlignment - over) {
151 // need to pad by (ByteAlignment - over)
152 NumBytesAllocated += ByteAlignment - over;
153 }
Chris Lattnerad44bd92002-12-15 18:15:24 +0000154 VirtReg2OffsetMap[VirtReg] = NumBytesAllocated;
Misha Brukmanf514d512002-12-02 21:11:58 +0000155 NumBytesAllocated += size;
Misha Brukmancea22452002-12-13 04:34:02 +0000156#endif
157 // FIXME: forcing each arg to take 4 bytes on the stack
Chris Lattnerad44bd92002-12-15 18:15:24 +0000158 VirtReg2OffsetMap[VirtReg] = NumBytesAllocated;
Misha Brukman203b7692002-12-13 09:54:36 +0000159 NumBytesAllocated += ByteAlignment;
Misha Brukmanf514d512002-12-02 21:11:58 +0000160 }
Chris Lattnerad44bd92002-12-15 18:15:24 +0000161 return VirtReg2OffsetMap[VirtReg];
Misha Brukmanf514d512002-12-02 21:11:58 +0000162}
163
Misha Brukman07218672002-11-22 22:44:32 +0000164unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
165 const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
166 unsigned physReg;
167 assert(regClass);
168 if (RegClassIdx.find(regClass) != RegClassIdx.end()) {
169 unsigned regIdx = RegClassIdx[regClass]++;
170 assert(regIdx < regClass->getNumRegs() && "Not enough registers!");
171 physReg = regClass->getRegister(regIdx);
172 } else {
173 physReg = regClass->getRegister(0);
174 // assert(physReg < regClass->getNumRegs() && "No registers in class!");
175 RegClassIdx[regClass] = 1;
176 }
177
178 if (isAvailableReg(physReg))
179 return physReg;
180 else {
181 return getFreeReg(virtualReg);
182 }
183}
184
185MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000186RegAllocSimple::moveUseToReg (MachineBasicBlock *MBB,
187 MachineBasicBlock::iterator I,
Misha Brukman07218672002-11-22 22:44:32 +0000188 unsigned VirtReg, unsigned &PhysReg)
189{
190 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
191 assert(regClass);
192
Misha Brukmanf514d512002-12-02 21:11:58 +0000193 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000194 PhysReg = getFreeReg(VirtReg);
195
Misha Brukmanf514d512002-12-02 21:11:58 +0000196 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000197 return RegInfo->loadRegOffset2Reg(MBB, I, PhysReg,
Misha Brukmanf514d512002-12-02 21:11:58 +0000198 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000199 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000200}
201
202MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000203RegAllocSimple::saveVirtRegToStack (MachineBasicBlock *MBB,
204 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000205 unsigned VirtReg, unsigned PhysReg)
Misha Brukman07218672002-11-22 22:44:32 +0000206{
207 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
208 assert(regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000209
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000210 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukmanf514d512002-12-02 21:11:58 +0000211
Misha Brukman07218672002-11-22 22:44:32 +0000212 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000213 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukman07218672002-11-22 22:44:32 +0000214 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000215 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000216}
217
Misha Brukmandc2ec002002-12-03 23:15:19 +0000218MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000219RegAllocSimple::savePhysRegToStack (MachineBasicBlock *MBB,
220 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000221 unsigned PhysReg)
222{
223 const TargetRegisterClass* regClass = MF->getRegClass(PhysReg);
224 assert(regClass);
225
226 unsigned offset = allocateStackSpaceFor(PhysReg, regClass);
227
228 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000229 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000230 RegInfo->getFramePointer(),
231 offset, regClass->getDataSize());
232}
233
Misha Brukman07218672002-11-22 22:44:32 +0000234bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000235 cleanupAfterFunction();
236
Misha Brukman07218672002-11-22 22:44:32 +0000237 unsigned virtualReg, physReg;
238 DEBUG(std::cerr << "Machine Function " << "\n");
239 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000240
Misha Brukman07218672002-11-22 22:44:32 +0000241 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
242 MBB != MBBe; ++MBB)
243 {
244 CurrMBB = &(*MBB);
245
Misha Brukman203b7692002-12-13 09:54:36 +0000246 // Handle PHI instructions specially: add moves to each pred block
247 while (MBB->front()->getOpcode() == 0) {
248 MachineInstr *MI = MBB->front();
249 // get rid of the phi
250 MBB->erase(MBB->begin());
251
Misha Brukman972b03f2002-12-13 11:33:22 +0000252 // a preliminary pass that will invalidate any registers that
253 // are used by the instruction (including implicit uses)
254 invalidatePhysRegs(MI);
255
256 DEBUG(std::cerr << "num invalid regs: " << RegsUsed.size() << "\n");
257
Misha Brukman203b7692002-12-13 09:54:36 +0000258 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
259 MachineOperand &targetReg = MI->getOperand(0);
260
261 // If it's a virtual register, allocate a physical one
262 // otherwise, just use whatever register is there now
263 // note: it MUST be a register -- we're assigning to it
264 virtualReg = (unsigned) targetReg.getAllocatedRegNum();
265 if (targetReg.isVirtualRegister()) {
266 physReg = getFreeReg(virtualReg);
267 } else {
Misha Brukman08686672002-12-13 12:33:31 +0000268 physReg = virtualReg;
Misha Brukman203b7692002-12-13 09:54:36 +0000269 }
270
271 // Find the register class of the target register: should be the
272 // same as the values we're trying to store there
Chris Lattnerdd444f92002-12-15 18:38:59 +0000273 const TargetRegisterClass* regClass = PhysRegClasses[physReg];
Misha Brukman203b7692002-12-13 09:54:36 +0000274 assert(regClass && "Target register class not found!");
275 unsigned dataSize = regClass->getDataSize();
276
277 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
278 MachineOperand &opVal = MI->getOperand(i-1);
279
280 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
281 // source path the phi
Chris Lattner71c83722002-12-15 08:02:51 +0000282 MachineBasicBlock *opBlock = MI->getOperand(i).getMachineBasicBlock();
Misha Brukman203b7692002-12-13 09:54:36 +0000283 MachineBasicBlock::iterator opI = opBlock->end();
284 MachineInstr *opMI = *(--opI);
285 const MachineInstrInfo &MII = TM.getInstrInfo();
Misha Brukman74676da2002-12-13 11:55:59 +0000286 // must backtrack over ALL the branches in the previous block, until no more
287 while ((MII.isBranch(opMI->getOpcode()) || MII.isReturn(opMI->getOpcode()))
288 && opI != opBlock->begin())
289 {
290 opMI = *(--opI);
291 }
292 // move back to the first branch instruction so new instructions
293 // are inserted right in front of it and not in front of a non-branch
294 ++opI;
295
Misha Brukman203b7692002-12-13 09:54:36 +0000296
Misha Brukman08686672002-12-13 12:33:31 +0000297 // Retrieve the constant value from this op, move it to target
298 // register of the phi
299 if (opVal.getType() == MachineOperand::MO_SignExtendedImmed ||
300 opVal.getType() == MachineOperand::MO_UnextendedImmed)
Misha Brukman203b7692002-12-13 09:54:36 +0000301 {
Misha Brukman08686672002-12-13 12:33:31 +0000302 opI = RegInfo->moveImm2Reg(opBlock, opI, physReg,
303 (unsigned) opVal.getImmedValue(),
304 dataSize);
305 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
306 } else {
307 // Allocate a physical register and add a move in the BB
308 unsigned opVirtualReg = (unsigned) opVal.getAllocatedRegNum();
309 unsigned opPhysReg; // = getFreeReg(opVirtualReg);
310 opI = moveUseToReg(opBlock, opI, opVirtualReg, physReg);
311 //opI = RegInfo->moveReg2Reg(opBlock, opI, physReg, opPhysReg,
312 // dataSize);
313 // Save that register value to the stack of the TARGET REG
314 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
315 }
Misha Brukman972b03f2002-12-13 11:33:22 +0000316
317 // make regs available to other instructions
318 clearAllRegs();
Misha Brukman203b7692002-12-13 09:54:36 +0000319 }
320
321 // really delete the instruction
322 delete MI;
323 }
324
Misha Brukman07218672002-11-22 22:44:32 +0000325 //loop over each basic block
326 for (MachineBasicBlock::iterator I = MBB->begin(); I != MBB->end(); ++I)
327 {
328 MachineInstr *MI = *I;
329
Misha Brukman972b03f2002-12-13 11:33:22 +0000330 // a preliminary pass that will invalidate any registers that
Misha Brukmandc2ec002002-12-03 23:15:19 +0000331 // are used by the instruction (including implicit uses)
Misha Brukman972b03f2002-12-13 11:33:22 +0000332 invalidatePhysRegs(MI);
Misha Brukmandc2ec002002-12-03 23:15:19 +0000333
Misha Brukman203b7692002-12-13 09:54:36 +0000334 // Loop over uses, move from memory into registers
Misha Brukman07218672002-11-22 22:44:32 +0000335 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
336 MachineOperand &op = MI->getOperand(i);
337
338 if (op.getType() == MachineOperand::MO_SignExtendedImmed ||
339 op.getType() == MachineOperand::MO_UnextendedImmed)
340 {
341 DEBUG(std::cerr << "const\n");
342 } else if (op.isVirtualRegister()) {
343 virtualReg = (unsigned) op.getAllocatedRegNum();
Misha Brukman07218672002-11-22 22:44:32 +0000344 DEBUG(std::cerr << "op: " << op << "\n");
345 DEBUG(std::cerr << "\t inst[" << i << "]: ";
346 MI->print(std::cerr, TM));
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000347
348 // make sure the same virtual register maps to the same physical
349 // register in any given instruction
350 if (VirtReg2PhysRegMap.find(virtualReg) != VirtReg2PhysRegMap.end()) {
351 physReg = VirtReg2PhysRegMap[virtualReg];
Misha Brukmanf514d512002-12-02 21:11:58 +0000352 } else {
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000353 if (op.opIsDef()) {
354 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
355 // must be same register number as the first operand
356 // This maps a = b + c into b += c, and saves b into a's spot
357 physReg = (unsigned) MI->getOperand(1).getAllocatedRegNum();
358 } else {
359 physReg = getFreeReg(virtualReg);
360 }
361 MachineBasicBlock::iterator J = I;
Misha Brukman203b7692002-12-13 09:54:36 +0000362 J = saveVirtRegToStack(CurrMBB, ++J, virtualReg, physReg);
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000363 I = --J;
364 } else {
Misha Brukman203b7692002-12-13 09:54:36 +0000365 I = moveUseToReg(CurrMBB, I, virtualReg, physReg);
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000366 }
367 VirtReg2PhysRegMap[virtualReg] = physReg;
Misha Brukmanf514d512002-12-02 21:11:58 +0000368 }
369 MI->SetMachineOperandReg(i, physReg);
Misha Brukman07218672002-11-22 22:44:32 +0000370 DEBUG(std::cerr << "virt: " << virtualReg <<
371 ", phys: " << op.getAllocatedRegNum() << "\n");
372 }
373 }
374
375 clearAllRegs();
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000376 VirtReg2PhysRegMap.clear();
Misha Brukman07218672002-11-22 22:44:32 +0000377 }
378
379 }
380
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000381 // add prologue we should preserve callee-save registers...
382 MachineFunction::iterator Fi = Fn.begin();
383 MachineBasicBlock *MBB = Fi;
384 MachineBasicBlock::iterator MBBi = MBB->begin();
385 RegInfo->emitPrologue(MBB, MBBi, NumBytesAllocated);
386
387 // add epilogue to restore the callee-save registers
388 // loop over the basic block
389 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
390 MBB != MBBe; ++MBB)
391 {
392 // check if last instruction is a RET
393 MachineBasicBlock::iterator I = (*MBB).end();
394 MachineInstr *MI = *(--I);
395 const MachineInstrInfo &MII = TM.getInstrInfo();
396 if (MII.isReturn(MI->getOpcode())) {
397 // this block has a return instruction, add epilogue
398 RegInfo->emitEpilogue(MBB, I, NumBytesAllocated);
399 }
400 }
Misha Brukman07218672002-11-22 22:44:32 +0000401
402 return false; // We never modify the LLVM itself.
403}
404
405Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) {
406 return new RegAllocSimple(TM);
407}