blob: 3d1a32b27a09c9c22ec8a15a4f4287f578d3e1c2 [file] [log] [blame]
Chris Lattner1d62cea2002-12-16 14:37:00 +00001//===-- RegAllocSimple.cpp - A simple generic register allocator ----------===//
Misha Brukman07218672002-11-22 22:44:32 +00002//
Chris Lattner600dee42002-12-28 20:42:14 +00003// This file implements a simple register allocator. *Very* simple: It immediate
4// spills every value right after it is computed, and it reloads all used
5// operands from the spill area to temporary registers before each instruction.
6// It does not keep values in registers across instructions.
Misha Brukman07218672002-11-22 22:44:32 +00007//
8//===----------------------------------------------------------------------===//
9
Chris Lattner600dee42002-12-28 20:42:14 +000010#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000011#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner5124aec2002-12-25 05:04:20 +000012#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000013#include "llvm/CodeGen/MachineFrameInfo.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/TargetMachine.h"
Misha Brukman07218672002-11-22 22:44:32 +000016#include "Support/Statistic.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000017#include <iostream>
Misha Brukman07218672002-11-22 22:44:32 +000018
Misha Brukman59b3eed2002-12-13 10:42:31 +000019namespace {
Chris Lattnerda7e4532002-12-15 20:36:09 +000020 Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
21 Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
22
Chris Lattner600dee42002-12-28 20:42:14 +000023 class RegAllocSimple : public MachineFunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000024 MachineFunction *MF;
Chris Lattner600dee42002-12-28 20:42:14 +000025 const TargetMachine *TM;
Misha Brukman07218672002-11-22 22:44:32 +000026 const MRegisterInfo *RegInfo;
Misha Brukman07218672002-11-22 22:44:32 +000027
Chris Lattner600dee42002-12-28 20:42:14 +000028 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
29 // these values are spilled
30 std::map<unsigned, int> StackSlotForVirtReg;
Misha Brukman07218672002-11-22 22:44:32 +000031
Chris Lattner600dee42002-12-28 20:42:14 +000032 // RegsUsed - Keep track of what registers are currently in use. This is a
33 // bitset.
34 std::vector<bool> RegsUsed;
Chris Lattnerda7e4532002-12-15 20:36:09 +000035
36 // RegClassIdx - Maps RegClass => which index we can take a register
37 // from. Since this is a simple register allocator, when we need a register
38 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000039 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
40
Chris Lattnerda7e4532002-12-15 20:36:09 +000041 public:
Chris Lattner8233e2f2002-12-15 21:13:12 +000042 virtual const char *getPassName() const {
43 return "Simple Register Allocator";
44 }
45
Chris Lattnerda7e4532002-12-15 20:36:09 +000046 /// runOnMachineFunction - Register allocate the whole function
47 bool runOnMachineFunction(MachineFunction &Fn);
48
Chris Lattner600dee42002-12-28 20:42:14 +000049 private:
Chris Lattnerda7e4532002-12-15 20:36:09 +000050 /// AllocateBasicBlock - Register allocate the specified basic block.
51 void AllocateBasicBlock(MachineBasicBlock &MBB);
52
53 /// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
54 /// in predecessor basic blocks.
55 void EliminatePHINodes(MachineBasicBlock &MBB);
56
Chris Lattner9f366d72002-12-15 22:19:19 +000057 /// getStackSpaceFor - This returns the offset of the specified virtual
58 /// register on the stack, allocating space if neccesary.
Chris Lattner600dee42002-12-28 20:42:14 +000059 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Misha Brukman07218672002-11-22 22:44:32 +000060
Chris Lattner9f366d72002-12-15 22:19:19 +000061 /// Given a virtual register, return a compatible physical register that is
62 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000063 ///
Misha Brukman07218672002-11-22 22:44:32 +000064 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000065 ///
Misha Brukman07218672002-11-22 22:44:32 +000066 unsigned getFreeReg(unsigned virtualReg);
67
Misha Brukman07218672002-11-22 22:44:32 +000068 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +000069 unsigned reloadVirtReg(MachineBasicBlock &MBB,
70 MachineBasicBlock::iterator &I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +000071
72 /// Saves reg value on the stack (maps virtual register to stack value)
Chris Lattnerb167c042002-12-15 23:01:26 +000073 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
74 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +000075 };
76
Misha Brukman59b3eed2002-12-13 10:42:31 +000077}
Misha Brukman07218672002-11-22 22:44:32 +000078
Chris Lattner9f366d72002-12-15 22:19:19 +000079/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +000080/// register to be held on the stack.
Chris Lattner600dee42002-12-28 20:42:14 +000081int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
82 const TargetRegisterClass *RC) {
Chris Lattner9f366d72002-12-15 22:19:19 +000083 // Find the location VirtReg would belong...
Chris Lattner600dee42002-12-28 20:42:14 +000084 std::map<unsigned, int>::iterator I =
85 StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +000086
Chris Lattner600dee42002-12-28 20:42:14 +000087 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattner9f366d72002-12-15 22:19:19 +000088 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +000089
Chris Lattner600dee42002-12-28 20:42:14 +000090 // Allocate a new stack object for this spill location...
91 int FrameIdx =
92 MF->getFrameInfo()->CreateStackObject(RC->getSize(), RC->getAlignment());
Chris Lattner9f366d72002-12-15 22:19:19 +000093
94 // Assign the slot...
Chris Lattner600dee42002-12-28 20:42:14 +000095 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
96
97 return FrameIdx;
Misha Brukmanf514d512002-12-02 21:11:58 +000098}
99
Misha Brukman07218672002-11-22 22:44:32 +0000100unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000101 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(virtualReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000102 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
103 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Misha Brukman07218672002-11-22 22:44:32 +0000104
Chris Lattner600dee42002-12-28 20:42:14 +0000105 while (1) {
106 unsigned regIdx = RegClassIdx[RC]++;
107 assert(RI+regIdx != RE && "Not enough registers!");
108 unsigned PhysReg = *(RI+regIdx);
109
110 if (!RegsUsed[PhysReg])
111 return PhysReg;
112 }
Misha Brukman07218672002-11-22 22:44:32 +0000113}
114
Chris Lattnerb167c042002-12-15 23:01:26 +0000115unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
116 MachineBasicBlock::iterator &I,
117 unsigned VirtReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000118 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000119 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000120 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000121
Misha Brukmanf514d512002-12-02 21:11:58 +0000122 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000123 ++NumReloaded;
Chris Lattner600dee42002-12-28 20:42:14 +0000124 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000125 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000126}
127
Chris Lattnerb167c042002-12-15 23:01:26 +0000128void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
129 MachineBasicBlock::iterator &I,
Chris Lattner600dee42002-12-28 20:42:14 +0000130 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000131 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000132 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Misha Brukmanf514d512002-12-02 21:11:58 +0000133
Misha Brukman07218672002-11-22 22:44:32 +0000134 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000135 ++NumSpilled;
Chris Lattner600dee42002-12-28 20:42:14 +0000136 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Misha Brukman07218672002-11-22 22:44:32 +0000137}
138
Misha Brukmandc2ec002002-12-03 23:15:19 +0000139
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000140/// EliminatePHINodes - Eliminate phi nodes by inserting copy instructions in
141/// predecessor basic blocks.
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000142///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000143void RegAllocSimple::EliminatePHINodes(MachineBasicBlock &MBB) {
Chris Lattner600dee42002-12-28 20:42:14 +0000144 const MachineInstrInfo &MII = TM->getInstrInfo();
Chris Lattnerda7e4532002-12-15 20:36:09 +0000145
Chris Lattner9f366d72002-12-15 22:19:19 +0000146 while (MBB.front()->getOpcode() == MachineInstrInfo::PHI) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000147 MachineInstr *MI = MBB.front();
Chris Lattner9f366d72002-12-15 22:19:19 +0000148 // Unlink the PHI node from the basic block... but don't delete the PHI yet
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000149 MBB.erase(MBB.begin());
150
Chris Lattner9f366d72002-12-15 22:19:19 +0000151 DEBUG(std::cerr << "num ops: " << MI->getNumOperands() << "\n");
152 assert(MI->getOperand(0).isVirtualRegister() &&
153 "PHI node doesn't write virt reg?");
154
Chris Lattner9f366d72002-12-15 22:19:19 +0000155 unsigned virtualReg = MI->getOperand(0).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000156
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000157 for (int i = MI->getNumOperands() - 1; i >= 2; i-=2) {
158 MachineOperand &opVal = MI->getOperand(i-1);
159
160 // Get the MachineBasicBlock equivalent of the BasicBlock that is the
161 // source path the phi
162 MachineBasicBlock &opBlock = *MI->getOperand(i).getMachineBasicBlock();
Misha Brukmandd46e2a2002-12-04 23:58:08 +0000163
Chris Lattner3f91ad72002-12-15 20:48:03 +0000164 // Check to make sure we haven't already emitted the copy for this block.
165 // This can happen because PHI nodes may have multiple entries for the
166 // same basic block. It doesn't matter which entry we use though, because
167 // all incoming values are guaranteed to be the same for a particular bb.
168 //
169 // Note that this is N^2 in the number of phi node entries, but since the
170 // # of entries is tiny, this is not a problem.
171 //
172 bool HaveNotEmitted = true;
173 for (int op = MI->getNumOperands() - 1; op != i; op -= 2)
174 if (&opBlock == MI->getOperand(op).getMachineBasicBlock()) {
175 HaveNotEmitted = false;
176 break;
177 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000178
Chris Lattner3f91ad72002-12-15 20:48:03 +0000179 if (HaveNotEmitted) {
180 MachineBasicBlock::iterator opI = opBlock.end();
181 MachineInstr *opMI = *--opI;
182
183 // must backtrack over ALL the branches in the previous block
184 while (MII.isBranch(opMI->getOpcode()) && opI != opBlock.begin())
185 opMI = *--opI;
186
187 // move back to the first branch instruction so new instructions
188 // are inserted right in front of it and not in front of a non-branch
Chris Lattner5124aec2002-12-25 05:04:20 +0000189 //
Chris Lattner3f91ad72002-12-15 20:48:03 +0000190 if (!MII.isBranch(opMI->getOpcode()))
191 ++opI;
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000192
Chris Lattner5124aec2002-12-25 05:04:20 +0000193 const TargetRegisterClass *RC =
194 MF->getSSARegMap()->getRegClass(virtualReg);
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000195
Chris Lattner600dee42002-12-28 20:42:14 +0000196 assert(opVal.isVirtualRegister() &&
197 "Machine PHI Operands must all be virtual registers!");
198 RegInfo->copyRegToReg(opBlock, opI, virtualReg, opVal.getReg(), RC);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000199 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000200 }
201
Chris Lattner9f366d72002-12-15 22:19:19 +0000202 // really delete the PHI instruction now!
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000203 delete MI;
204 }
205}
206
207
208void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000209 // loop over each instruction
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000210 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000211 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000212 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000213
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000214 MachineInstr *MI = *I;
Chris Lattner600dee42002-12-28 20:42:14 +0000215
216 RegsUsed.resize(MRegisterInfo::FirstVirtualRegister);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000217
218 // a preliminary pass that will invalidate any registers that
219 // are used by the instruction (including implicit uses)
Chris Lattner600dee42002-12-28 20:42:14 +0000220 unsigned Opcode = MI->getOpcode();
221 const MachineInstrDescriptor &Desc = TM->getInstrInfo().get(Opcode);
222 if (const unsigned *Regs = Desc.ImplicitUses)
223 while (*Regs)
224 RegsUsed[*Regs++] = true;
225
226 if (const unsigned *Regs = Desc.ImplicitDefs)
227 while (*Regs)
228 RegsUsed[*Regs++] = true;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000229
230 // Loop over uses, move from memory into registers
231 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
232 MachineOperand &op = MI->getOperand(i);
233
Chris Lattnerda7e4532002-12-15 20:36:09 +0000234 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000235 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
236 DEBUG(std::cerr << "op: " << op << "\n");
237 DEBUG(std::cerr << "\t inst[" << i << "]: ";
Chris Lattner600dee42002-12-28 20:42:14 +0000238 MI->print(std::cerr, *TM));
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000239
240 // make sure the same virtual register maps to the same physical
241 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000242 unsigned physReg = Virt2PhysRegMap[virtualReg];
243 if (physReg == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000244 if (op.opIsDef()) {
Chris Lattner600dee42002-12-28 20:42:14 +0000245 if (TM->getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000246 // must be same register number as the first operand
247 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000248 assert(MI->getOperand(1).isRegister() &&
249 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner9f366d72002-12-15 22:19:19 +0000250 MI->getOperand(1).opIsUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000251 "Two address instruction invalid!");
252
Chris Lattnerda7e4532002-12-15 20:36:09 +0000253 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000254 } else {
255 physReg = getFreeReg(virtualReg);
256 }
Chris Lattnerb167c042002-12-15 23:01:26 +0000257 ++I;
258 spillVirtReg(MBB, I, virtualReg, physReg);
259 --I;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000260 } else {
Chris Lattnerb167c042002-12-15 23:01:26 +0000261 physReg = reloadVirtReg(MBB, I, virtualReg);
262 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000263 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000264 }
265 MI->SetMachineOperandReg(i, physReg);
266 DEBUG(std::cerr << "virt: " << virtualReg <<
267 ", phys: " << op.getAllocatedRegNum() << "\n");
268 }
269 }
Chris Lattner600dee42002-12-28 20:42:14 +0000270 RegClassIdx.clear();
271 RegsUsed.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000272 }
273}
274
Chris Lattnere7d361d2002-12-17 04:19:40 +0000275
Chris Lattnerda7e4532002-12-15 20:36:09 +0000276/// runOnMachineFunction - Register allocate the whole function
277///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000278bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000279 DEBUG(std::cerr << "Machine Function " << "\n");
280 MF = &Fn;
Chris Lattner600dee42002-12-28 20:42:14 +0000281 TM = &MF->getTarget();
282 RegInfo = TM->getRegisterInfo();
Misha Brukmandc2ec002002-12-03 23:15:19 +0000283
Chris Lattner8ed9eb52002-12-15 22:39:53 +0000284 // First pass: eliminate PHI instructions by inserting copies into predecessor
285 // blocks.
286 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
287 MBB != MBBe; ++MBB)
288 EliminatePHINodes(*MBB);
289
Chris Lattner9f366d72002-12-15 22:19:19 +0000290 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000291 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
292 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000293 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000294
Chris Lattner600dee42002-12-28 20:42:14 +0000295 StackSlotForVirtReg.clear();
Chris Lattner9f366d72002-12-15 22:19:19 +0000296 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000297}
298
Chris Lattner600dee42002-12-28 20:42:14 +0000299Pass *createSimpleRegisterAllocator() {
300 return new RegAllocSimple();
Misha Brukman07218672002-11-22 22:44:32 +0000301}