blob: f07f7f92be84a2e7ea16089b9131b86b72376bc1 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- RegAllocSimple.cpp - A simple generic register allocator ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements a simple register allocator. *Very* simple: It immediate
11// spills every value right after it is computed, and it reloads all used
12// operands from the spill area to temporary registers before each instruction.
13// It does not keep values in registers across instructions.
14//
15//===----------------------------------------------------------------------===//
16
17#define DEBUG_TYPE "regalloc"
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstr.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/RegAllocRegistry.h"
24#include "llvm/Target/TargetInstrInfo.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/Compiler.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/STLExtras.h"
Dan Gohman249ddbf2008-03-21 23:51:57 +000030#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031using namespace llvm;
32
33STATISTIC(NumStores, "Number of stores added");
34STATISTIC(NumLoads , "Number of loads added");
35
36namespace {
37 static RegisterRegAlloc
38 simpleRegAlloc("simple", " simple register allocator",
39 createSimpleRegisterAllocator);
40
41 class VISIBILITY_HIDDEN RegAllocSimple : public MachineFunctionPass {
42 public:
43 static char ID;
44 RegAllocSimple() : MachineFunctionPass((intptr_t)&ID) {}
45 private:
46 MachineFunction *MF;
47 const TargetMachine *TM;
Dan Gohman1e57df32008-02-10 18:45:23 +000048 const TargetRegisterInfo *TRI;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049
50 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
51 // these values are spilled
52 std::map<unsigned, int> StackSlotForVirtReg;
53
54 // RegsUsed - Keep track of what registers are currently in use. This is a
55 // bitset.
56 std::vector<bool> RegsUsed;
57
58 // RegClassIdx - Maps RegClass => which index we can take a register
59 // from. Since this is a simple register allocator, when we need a register
60 // of a certain class, we just take the next available one.
61 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
62
63 public:
64 virtual const char *getPassName() const {
65 return "Simple Register Allocator";
66 }
67
68 /// runOnMachineFunction - Register allocate the whole function
69 bool runOnMachineFunction(MachineFunction &Fn);
70
71 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
73 MachineFunctionPass::getAnalysisUsage(AU);
74 }
75 private:
76 /// AllocateBasicBlock - Register allocate the specified basic block.
77 void AllocateBasicBlock(MachineBasicBlock &MBB);
78
79 /// getStackSpaceFor - This returns the offset of the specified virtual
80 /// register on the stack, allocating space if necessary.
81 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
82
83 /// Given a virtual register, return a compatible physical register that is
84 /// currently unused.
85 ///
86 /// Side effect: marks that register as being used until manually cleared
87 ///
88 unsigned getFreeReg(unsigned virtualReg);
89
90 /// Moves value from memory into that register
91 unsigned reloadVirtReg(MachineBasicBlock &MBB,
92 MachineBasicBlock::iterator I, unsigned VirtReg);
93
94 /// Saves reg value on the stack (maps virtual register to stack value)
95 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
96 unsigned VirtReg, unsigned PhysReg);
97 };
98 char RegAllocSimple::ID = 0;
99}
100
101/// getStackSpaceFor - This allocates space for the specified virtual
102/// register to be held on the stack.
103int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
104 const TargetRegisterClass *RC) {
105 // Find the location VirtReg would belong...
Dan Gohman7fb3d542008-07-09 19:51:00 +0000106 std::map<unsigned, int>::iterator I = StackSlotForVirtReg.find(VirtReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107
Dan Gohman7fb3d542008-07-09 19:51:00 +0000108 if (I != StackSlotForVirtReg.end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 return I->second; // Already has space allocated?
110
111 // Allocate a new stack object for this spill location...
112 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
113 RC->getAlignment());
114
115 // Assign the slot...
116 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
117
118 return FrameIdx;
119}
120
121unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattner1b989192007-12-31 04:13:23 +0000122 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtualReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
124 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
125
126 while (1) {
127 unsigned regIdx = RegClassIdx[RC]++;
128 assert(RI+regIdx != RE && "Not enough registers!");
129 unsigned PhysReg = *(RI+regIdx);
130
131 if (!RegsUsed[PhysReg]) {
Chris Lattner1b989192007-12-31 04:13:23 +0000132 MF->getRegInfo().setPhysRegUsed(PhysReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 return PhysReg;
134 }
135 }
136}
137
138unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
139 MachineBasicBlock::iterator I,
140 unsigned VirtReg) {
Chris Lattner1b989192007-12-31 04:13:23 +0000141 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 int FrameIdx = getStackSpaceFor(VirtReg, RC);
143 unsigned PhysReg = getFreeReg(VirtReg);
144
145 // Add move instruction(s)
146 ++NumLoads;
Owen Anderson81875432008-01-01 21:11:32 +0000147 const TargetInstrInfo* TII = MBB.getParent()->getTarget().getInstrInfo();
148 TII->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 return PhysReg;
150}
151
152void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
153 MachineBasicBlock::iterator I,
154 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner1b989192007-12-31 04:13:23 +0000155 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg);
Owen Anderson81875432008-01-01 21:11:32 +0000156 const TargetInstrInfo* TII = MBB.getParent()->getTarget().getInstrInfo();
157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 int FrameIdx = getStackSpaceFor(VirtReg, RC);
159
160 // Add move instruction(s)
161 ++NumStores;
Owen Anderson81875432008-01-01 21:11:32 +0000162 TII->storeRegToStackSlot(MBB, I, PhysReg, true, FrameIdx, RC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163}
164
165
166void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
167 // loop over each instruction
168 for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) {
169 // Made to combat the incorrect allocation of r2 = add r1, r1
170 std::map<unsigned, unsigned> Virt2PhysRegMap;
171
Dan Gohman1e57df32008-02-10 18:45:23 +0000172 RegsUsed.resize(TRI->getNumRegs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173
174 // This is a preliminary pass that will invalidate any registers that are
175 // used by the instruction (including implicit uses).
Chris Lattner5b930372008-01-07 07:27:27 +0000176 const TargetInstrDesc &Desc = MI->getDesc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 const unsigned *Regs;
178 if (Desc.ImplicitUses) {
179 for (Regs = Desc.ImplicitUses; *Regs; ++Regs)
180 RegsUsed[*Regs] = true;
181 }
182
183 if (Desc.ImplicitDefs) {
184 for (Regs = Desc.ImplicitDefs; *Regs; ++Regs) {
185 RegsUsed[*Regs] = true;
Chris Lattner1b989192007-12-31 04:13:23 +0000186 MF->getRegInfo().setPhysRegUsed(*Regs);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 }
188 }
189
190 // Loop over uses, move from memory into registers.
191 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
192 MachineOperand &op = MI->getOperand(i);
193
194 if (op.isRegister() && op.getReg() &&
Dan Gohman1e57df32008-02-10 18:45:23 +0000195 TargetRegisterInfo::isVirtualRegister(op.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 unsigned virtualReg = (unsigned) op.getReg();
197 DOUT << "op: " << op << "\n";
198 DOUT << "\t inst[" << i << "]: ";
199 DEBUG(MI->print(*cerr.stream(), TM));
200
201 // make sure the same virtual register maps to the same physical
202 // register in any given instruction
203 unsigned physReg = Virt2PhysRegMap[virtualReg];
204 if (physReg == 0) {
205 if (op.isDef()) {
Chris Lattner5b930372008-01-07 07:27:27 +0000206 int TiedOp = Desc.findTiedToSrcOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 if (TiedOp == -1) {
208 physReg = getFreeReg(virtualReg);
209 } else {
210 // must be same register number as the source operand that is
211 // tied to. This maps a = b + c into b = b + c, and saves b into
212 // a's spot.
213 assert(MI->getOperand(TiedOp).isRegister() &&
214 MI->getOperand(TiedOp).getReg() &&
215 MI->getOperand(TiedOp).isUse() &&
216 "Two address instruction invalid!");
217
218 physReg = MI->getOperand(TiedOp).getReg();
219 }
220 spillVirtReg(MBB, next(MI), virtualReg, physReg);
221 } else {
222 physReg = reloadVirtReg(MBB, MI, virtualReg);
223 Virt2PhysRegMap[virtualReg] = physReg;
224 }
225 }
226 MI->getOperand(i).setReg(physReg);
227 DOUT << "virt: " << virtualReg << ", phys: " << op.getReg() << "\n";
228 }
229 }
230 RegClassIdx.clear();
231 RegsUsed.clear();
232 }
233}
234
235
236/// runOnMachineFunction - Register allocate the whole function
237///
238bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
239 DOUT << "Machine Function\n";
240 MF = &Fn;
241 TM = &MF->getTarget();
Dan Gohman1e57df32008-02-10 18:45:23 +0000242 TRI = TM->getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243
244 // Loop over all of the basic blocks, eliminating virtual register references
245 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
246 MBB != MBBe; ++MBB)
247 AllocateBasicBlock(*MBB);
248
249 StackSlotForVirtReg.clear();
250 return true;
251}
252
253FunctionPass *llvm::createSimpleRegisterAllocator() {
254 return new RegAllocSimple();
255}