blob: 3a58b5abeedaf044db8c8e2f8965917e4f8cdc39 [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"
12#include "llvm/CodeGen/MachineInstr.h"
13#include "llvm/CodeGen/MachineFunction.h"
14#include "llvm/CodeGen/MachineInstrBuilder.h"
Misha Brukmandd46e2a2002-12-04 23:58:08 +000015#include "llvm/Target/MachineInstrInfo.h"
Misha Brukman07218672002-11-22 22:44:32 +000016#include "llvm/Target/MRegisterInfo.h"
17#include "llvm/Target/MachineRegInfo.h"
18#include "llvm/Target/TargetMachine.h"
19#include "llvm/Support/InstVisitor.h"
20#include "Support/Statistic.h"
21#include <map>
22
Misha Brukman59b3eed2002-12-13 10:42:31 +000023namespace {
Misha Brukman07218672002-11-22 22:44:32 +000024 struct RegAllocSimple : public FunctionPass {
25 TargetMachine &TM;
26 MachineBasicBlock *CurrMBB;
27 MachineFunction *MF;
28 unsigned maxOffset;
29 const MRegisterInfo *RegInfo;
30 unsigned NumBytesAllocated, ByteAlignment;
31
32 // Maps SSA Regs => offsets on the stack where these values are stored
Misha Brukman06f8aec2002-12-04 19:24:45 +000033 // FIXME: change name to VirtReg2OffsetMap
34 std::map<unsigned, unsigned> RegMap;
Misha Brukman07218672002-11-22 22:44:32 +000035
36 // Maps SSA Regs => physical regs
37 std::map<unsigned, unsigned> SSA2PhysRegMap;
Misha Brukmandc2ec002002-12-03 23:15:19 +000038
39 // Maps physical register to their register classes
40 std::map<unsigned, const TargetRegisterClass*> PhysReg2RegClassMap;
Misha Brukmand1bedcc2002-12-12 23:20:31 +000041
42 // Made to combat the incorrect allocation of r2 = add r1, r1
43 std::map<unsigned, unsigned> VirtReg2PhysRegMap;
Misha Brukman07218672002-11-22 22:44:32 +000044
45 // Maps RegClass => which index we can take a register from. Since this is a
46 // simple register allocator, when we need a register of a certain class, we
47 // just take the next available one.
48 std::map<unsigned, unsigned> RegsUsed;
49 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
50
51 RegAllocSimple(TargetMachine &tm) : TM(tm), CurrMBB(0), maxOffset(0),
52 RegInfo(tm.getRegisterInfo()),
Misha Brukmancea22452002-12-13 04:34:02 +000053 ByteAlignment(4)
Misha Brukman07218672002-11-22 22:44:32 +000054 {
Misha Brukmandc2ec002002-12-03 23:15:19 +000055 // build reverse mapping for physReg -> register class
56 RegInfo->buildReg2RegClassMap(PhysReg2RegClassMap);
57
Misha Brukman07218672002-11-22 22:44:32 +000058 RegsUsed[RegInfo->getFramePointer()] = 1;
59 RegsUsed[RegInfo->getStackPointer()] = 1;
Misha Brukmancea22452002-12-13 04:34:02 +000060
61 cleanupAfterFunction();
Misha Brukman07218672002-11-22 22:44:32 +000062 }
63
64 bool isAvailableReg(unsigned Reg) {
65 // assert(Reg < MRegisterInfo::FirstVirtualReg && "...");
66 return RegsUsed.find(Reg) == RegsUsed.end();
67 }
68
Misha Brukmanf514d512002-12-02 21:11:58 +000069 ///
70 unsigned allocateStackSpaceFor(unsigned VirtReg,
71 const TargetRegisterClass *regClass);
72
Misha Brukman07218672002-11-22 22:44:32 +000073 /// Given size (in bytes), returns a register that is currently unused
74 /// Side effect: marks that register as being used until manually cleared
75 unsigned getFreeReg(unsigned virtualReg);
76
77 /// Returns all `borrowed' registers back to the free pool
78 void clearAllRegs() {
79 RegClassIdx.clear();
80 }
81
Misha Brukman972b03f2002-12-13 11:33:22 +000082 /// Invalidates any references, real or implicit, to physical registers
83 ///
84 void invalidatePhysRegs(const MachineInstr *MI) {
85 unsigned Opcode = MI->getOpcode();
86 const MachineInstrInfo &MII = TM.getInstrInfo();
87 const MachineInstrDescriptor &Desc = MII.get(Opcode);
88 const unsigned *regs = Desc.ImplicitUses;
89 while (*regs)
90 RegsUsed[*regs++] = 1;
91
92 regs = Desc.ImplicitDefs;
93 while (*regs)
94 RegsUsed[*regs++] = 1;
95
96
97 /*
98 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
99 const MachineOperand &op = MI->getOperand(i);
100 if (op.isMachineRegister())
101 RegsUsed[op.getAllocatedRegNum()] = 1;
102 }
103
104 for (int i = MI->getNumImplicitRefs() - 1; i >= 0; --i) {
105 const MachineOperand &op = MI->getImplicitOp(i);
106 if (op.isMachineRegister())
107 RegsUsed[op.getAllocatedRegNum()] = 1;
108 }
109 */
110 }
111
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000112 void cleanupAfterFunction() {
113 RegMap.clear();
114 SSA2PhysRegMap.clear();
Misha Brukman203b7692002-12-13 09:54:36 +0000115 NumBytesAllocated = ByteAlignment;
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000116 }
117
Misha Brukman07218672002-11-22 22:44:32 +0000118 /// Moves value from memory into that register
119 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000120 moveUseToReg (MachineBasicBlock *MBB,
121 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukman07218672002-11-22 22:44:32 +0000122 unsigned &PhysReg);
123
124 /// Saves reg value on the stack (maps virtual register to stack value)
125 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000126 saveVirtRegToStack (MachineBasicBlock *MBB,
127 MachineBasicBlock::iterator I, unsigned VirtReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000128 unsigned PhysReg);
129
130 MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000131 savePhysRegToStack (MachineBasicBlock *MBB,
132 MachineBasicBlock::iterator I, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000133
134 /// runOnFunction - Top level implementation of instruction selection for
135 /// the entire function.
136 ///
137 bool runOnMachineFunction(MachineFunction &Fn);
138
139 bool runOnFunction(Function &Fn) {
140 return runOnMachineFunction(MachineFunction::get(&Fn));
141 }
142 };
143
Misha Brukman59b3eed2002-12-13 10:42:31 +0000144}
Misha Brukman07218672002-11-22 22:44:32 +0000145
Misha Brukmanf514d512002-12-02 21:11:58 +0000146unsigned RegAllocSimple::allocateStackSpaceFor(unsigned VirtReg,
147 const TargetRegisterClass *regClass)
148{
149 if (RegMap.find(VirtReg) == RegMap.end()) {
Misha Brukmancea22452002-12-13 04:34:02 +0000150#if 0
Misha Brukmanf514d512002-12-02 21:11:58 +0000151 unsigned size = regClass->getDataSize();
152 unsigned over = NumBytesAllocated - (NumBytesAllocated % ByteAlignment);
153 if (size >= ByteAlignment - over) {
154 // need to pad by (ByteAlignment - over)
155 NumBytesAllocated += ByteAlignment - over;
156 }
157 RegMap[VirtReg] = NumBytesAllocated;
158 NumBytesAllocated += size;
Misha Brukmancea22452002-12-13 04:34:02 +0000159#endif
160 // FIXME: forcing each arg to take 4 bytes on the stack
161 RegMap[VirtReg] = NumBytesAllocated;
Misha Brukman203b7692002-12-13 09:54:36 +0000162 NumBytesAllocated += ByteAlignment;
Misha Brukmanf514d512002-12-02 21:11:58 +0000163 }
164 return RegMap[VirtReg];
165}
166
Misha Brukman07218672002-11-22 22:44:32 +0000167unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
168 const TargetRegisterClass* regClass = MF->getRegClass(virtualReg);
169 unsigned physReg;
170 assert(regClass);
171 if (RegClassIdx.find(regClass) != RegClassIdx.end()) {
172 unsigned regIdx = RegClassIdx[regClass]++;
173 assert(regIdx < regClass->getNumRegs() && "Not enough registers!");
174 physReg = regClass->getRegister(regIdx);
175 } else {
176 physReg = regClass->getRegister(0);
177 // assert(physReg < regClass->getNumRegs() && "No registers in class!");
178 RegClassIdx[regClass] = 1;
179 }
180
181 if (isAvailableReg(physReg))
182 return physReg;
183 else {
184 return getFreeReg(virtualReg);
185 }
186}
187
188MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000189RegAllocSimple::moveUseToReg (MachineBasicBlock *MBB,
190 MachineBasicBlock::iterator I,
Misha Brukman07218672002-11-22 22:44:32 +0000191 unsigned VirtReg, unsigned &PhysReg)
192{
193 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
194 assert(regClass);
195
Misha Brukmanf514d512002-12-02 21:11:58 +0000196 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000197 PhysReg = getFreeReg(VirtReg);
198
Misha Brukmanf514d512002-12-02 21:11:58 +0000199 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000200 return RegInfo->loadRegOffset2Reg(MBB, I, PhysReg,
Misha Brukmanf514d512002-12-02 21:11:58 +0000201 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000202 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000203}
204
205MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000206RegAllocSimple::saveVirtRegToStack (MachineBasicBlock *MBB,
207 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000208 unsigned VirtReg, unsigned PhysReg)
Misha Brukman07218672002-11-22 22:44:32 +0000209{
210 const TargetRegisterClass* regClass = MF->getRegClass(VirtReg);
211 assert(regClass);
Misha Brukman07218672002-11-22 22:44:32 +0000212
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000213 unsigned stackOffset = allocateStackSpaceFor(VirtReg, regClass);
Misha Brukmanf514d512002-12-02 21:11:58 +0000214
Misha Brukman07218672002-11-22 22:44:32 +0000215 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000216 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukman07218672002-11-22 22:44:32 +0000217 RegInfo->getFramePointer(),
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000218 -stackOffset, regClass->getDataSize());
Misha Brukman07218672002-11-22 22:44:32 +0000219}
220
Misha Brukmandc2ec002002-12-03 23:15:19 +0000221MachineBasicBlock::iterator
Misha Brukman203b7692002-12-13 09:54:36 +0000222RegAllocSimple::savePhysRegToStack (MachineBasicBlock *MBB,
223 MachineBasicBlock::iterator I,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000224 unsigned PhysReg)
225{
226 const TargetRegisterClass* regClass = MF->getRegClass(PhysReg);
227 assert(regClass);
228
229 unsigned offset = allocateStackSpaceFor(PhysReg, regClass);
230
231 // Add move instruction(s)
Misha Brukman203b7692002-12-13 09:54:36 +0000232 return RegInfo->storeReg2RegOffset(MBB, I, PhysReg,
Misha Brukmandc2ec002002-12-03 23:15:19 +0000233 RegInfo->getFramePointer(),
234 offset, regClass->getDataSize());
235}
236
Misha Brukman07218672002-11-22 22:44:32 +0000237bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000238 cleanupAfterFunction();
239
Misha Brukman07218672002-11-22 22:44:32 +0000240 unsigned virtualReg, physReg;
241 DEBUG(std::cerr << "Machine Function " << "\n");
242 MF = &Fn;
Misha Brukmandc2ec002002-12-03 23:15:19 +0000243
Misha Brukman07218672002-11-22 22:44:32 +0000244 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
245 MBB != MBBe; ++MBB)
246 {
247 CurrMBB = &(*MBB);
248
Misha Brukman203b7692002-12-13 09:54:36 +0000249 // Handle PHI instructions specially: add moves to each pred block
250 while (MBB->front()->getOpcode() == 0) {
251 MachineInstr *MI = MBB->front();
252 // get rid of the phi
253 MBB->erase(MBB->begin());
254
Misha Brukman972b03f2002-12-13 11:33:22 +0000255 // a preliminary pass that will invalidate any registers that
256 // are used by the instruction (including implicit uses)
257 invalidatePhysRegs(MI);
258
259 DEBUG(std::cerr << "num invalid regs: " << RegsUsed.size() << "\n");
260
Misha Brukman203b7692002-12-13 09:54:36 +0000261 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
262 MachineOperand &targetReg = MI->getOperand(0);
263
264 // If it's a virtual register, allocate a physical one
265 // otherwise, just use whatever register is there now
266 // note: it MUST be a register -- we're assigning to it
267 virtualReg = (unsigned) targetReg.getAllocatedRegNum();
268 if (targetReg.isVirtualRegister()) {
269 physReg = getFreeReg(virtualReg);
270 } else {
271 physReg = targetReg.getAllocatedRegNum();
272 }
273
274 // Find the register class of the target register: should be the
275 // same as the values we're trying to store there
276 const TargetRegisterClass* regClass = PhysReg2RegClassMap[physReg];
277 assert(regClass && "Target register class not found!");
278 unsigned dataSize = regClass->getDataSize();
279
280 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
281 MachineOperand &opVal = MI->getOperand(i-1);
282
283 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
284 // source path the phi
285 BasicBlock *opBB =
286 cast<BasicBlock>(MI->getOperand(i).getVRegValue());
287 MachineBasicBlock *opBlock = NULL;
288 for (MachineFunction::iterator opFi = Fn.begin(), opFe = Fn.end();
289 opFi != opFe; ++opFi)
290 {
291 if (opFi->getBasicBlock() == opBB) {
292 opBlock = opFi; break;
293 }
294 }
295 assert(opBlock && "MachineBasicBlock object not found for specified block!");
296
297 MachineBasicBlock::iterator opI = opBlock->end();
298 MachineInstr *opMI = *(--opI);
299 const MachineInstrInfo &MII = TM.getInstrInfo();
300
301 // insert the move just before the return/branch
302 if (MII.isReturn(opMI->getOpcode()) || MII.isBranch(opMI->getOpcode()))
303 {
304 // Retrieve the constant value from this op, move it to target
305 // register of the phi
306 if (opVal.getType() == MachineOperand::MO_SignExtendedImmed ||
307 opVal.getType() == MachineOperand::MO_UnextendedImmed)
308 {
309 opI = RegInfo->moveImm2Reg(opBlock, opI, physReg,
310 (unsigned) opVal.getImmedValue(),
311 dataSize);
312 saveVirtRegToStack(opBlock, opI, virtualReg, physReg);
313 } else {
314 // Allocate a physical register and add a move in the BB
315 unsigned opVirtualReg = (unsigned) opVal.getAllocatedRegNum();
316 unsigned opPhysReg; // = getFreeReg(opVirtualReg);
317 opI = moveUseToReg(opBlock, opI, opVirtualReg, opPhysReg);
318 opI = RegInfo->moveReg2Reg(opBlock, opI, physReg, opPhysReg,
319 dataSize);
320 // Save that register value to the stack of the TARGET REG
321 saveVirtRegToStack(opBlock, opI, virtualReg, opPhysReg);
322 }
323 }
Misha Brukman972b03f2002-12-13 11:33:22 +0000324
325 // make regs available to other instructions
326 clearAllRegs();
Misha Brukman203b7692002-12-13 09:54:36 +0000327 }
328
329 // really delete the instruction
330 delete MI;
331 }
332
Misha Brukman07218672002-11-22 22:44:32 +0000333 //loop over each basic block
334 for (MachineBasicBlock::iterator I = MBB->begin(); I != MBB->end(); ++I)
335 {
336 MachineInstr *MI = *I;
337
Misha Brukman972b03f2002-12-13 11:33:22 +0000338 // a preliminary pass that will invalidate any registers that
Misha Brukmandc2ec002002-12-03 23:15:19 +0000339 // are used by the instruction (including implicit uses)
Misha Brukman972b03f2002-12-13 11:33:22 +0000340 invalidatePhysRegs(MI);
Misha Brukmandc2ec002002-12-03 23:15:19 +0000341
Misha Brukman203b7692002-12-13 09:54:36 +0000342 // Loop over uses, move from memory into registers
Misha Brukman07218672002-11-22 22:44:32 +0000343 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
344 MachineOperand &op = MI->getOperand(i);
345
346 if (op.getType() == MachineOperand::MO_SignExtendedImmed ||
347 op.getType() == MachineOperand::MO_UnextendedImmed)
348 {
349 DEBUG(std::cerr << "const\n");
350 } else if (op.isVirtualRegister()) {
351 virtualReg = (unsigned) op.getAllocatedRegNum();
Misha Brukman07218672002-11-22 22:44:32 +0000352 DEBUG(std::cerr << "op: " << op << "\n");
353 DEBUG(std::cerr << "\t inst[" << i << "]: ";
354 MI->print(std::cerr, TM));
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000355
356 // make sure the same virtual register maps to the same physical
357 // register in any given instruction
358 if (VirtReg2PhysRegMap.find(virtualReg) != VirtReg2PhysRegMap.end()) {
359 physReg = VirtReg2PhysRegMap[virtualReg];
Misha Brukmanf514d512002-12-02 21:11:58 +0000360 } else {
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000361 if (op.opIsDef()) {
362 if (TM.getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
363 // must be same register number as the first operand
364 // This maps a = b + c into b += c, and saves b into a's spot
365 physReg = (unsigned) MI->getOperand(1).getAllocatedRegNum();
366 } else {
367 physReg = getFreeReg(virtualReg);
368 }
369 MachineBasicBlock::iterator J = I;
Misha Brukman203b7692002-12-13 09:54:36 +0000370 J = saveVirtRegToStack(CurrMBB, ++J, virtualReg, physReg);
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000371 I = --J;
372 } else {
Misha Brukman203b7692002-12-13 09:54:36 +0000373 I = moveUseToReg(CurrMBB, I, virtualReg, physReg);
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000374 }
375 VirtReg2PhysRegMap[virtualReg] = physReg;
Misha Brukmanf514d512002-12-02 21:11:58 +0000376 }
377 MI->SetMachineOperandReg(i, physReg);
Misha Brukman07218672002-11-22 22:44:32 +0000378 DEBUG(std::cerr << "virt: " << virtualReg <<
379 ", phys: " << op.getAllocatedRegNum() << "\n");
380 }
381 }
382
383 clearAllRegs();
Misha Brukmand1bedcc2002-12-12 23:20:31 +0000384 VirtReg2PhysRegMap.clear();
Misha Brukman07218672002-11-22 22:44:32 +0000385 }
386
387 }
388
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000389 // add prologue we should preserve callee-save registers...
390 MachineFunction::iterator Fi = Fn.begin();
391 MachineBasicBlock *MBB = Fi;
392 MachineBasicBlock::iterator MBBi = MBB->begin();
393 RegInfo->emitPrologue(MBB, MBBi, NumBytesAllocated);
394
395 // add epilogue to restore the callee-save registers
396 // loop over the basic block
397 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
398 MBB != MBBe; ++MBB)
399 {
400 // check if last instruction is a RET
401 MachineBasicBlock::iterator I = (*MBB).end();
402 MachineInstr *MI = *(--I);
403 const MachineInstrInfo &MII = TM.getInstrInfo();
404 if (MII.isReturn(MI->getOpcode())) {
405 // this block has a return instruction, add epilogue
406 RegInfo->emitEpilogue(MBB, I, NumBytesAllocated);
407 }
408 }
Misha Brukman07218672002-11-22 22:44:32 +0000409
410 return false; // We never modify the LLVM itself.
411}
412
413Pass *createSimpleX86RegisterAllocator(TargetMachine &TM) {
414 return new RegAllocSimple(TM);
415}