blob: 104d042de09dbed7b435d581ef2d1305f14d84a1 [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 Lattner80a04782003-01-13 00:26:08 +000010#include "llvm/CodeGen/Passes.h"
Chris Lattner600dee42002-12-28 20:42:14 +000011#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000012#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner5124aec2002-12-25 05:04:20 +000013#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000014#include "llvm/CodeGen/MachineFrameInfo.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/TargetMachine.h"
Misha Brukman07218672002-11-22 22:44:32 +000017#include "Support/Statistic.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000018#include <iostream>
Misha Brukman07218672002-11-22 22:44:32 +000019
Misha Brukman59b3eed2002-12-13 10:42:31 +000020namespace {
Chris Lattnerda7e4532002-12-15 20:36:09 +000021 Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
22 Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
23
Chris Lattner600dee42002-12-28 20:42:14 +000024 class RegAllocSimple : public MachineFunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000025 MachineFunction *MF;
Chris Lattner600dee42002-12-28 20:42:14 +000026 const TargetMachine *TM;
Misha Brukman07218672002-11-22 22:44:32 +000027 const MRegisterInfo *RegInfo;
Misha Brukman07218672002-11-22 22:44:32 +000028
Chris Lattner600dee42002-12-28 20:42:14 +000029 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
30 // these values are spilled
31 std::map<unsigned, int> StackSlotForVirtReg;
Misha Brukman07218672002-11-22 22:44:32 +000032
Chris Lattner600dee42002-12-28 20:42:14 +000033 // RegsUsed - Keep track of what registers are currently in use. This is a
34 // bitset.
35 std::vector<bool> RegsUsed;
Chris Lattnerda7e4532002-12-15 20:36:09 +000036
37 // RegClassIdx - Maps RegClass => which index we can take a register
38 // from. Since this is a simple register allocator, when we need a register
39 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000040 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
41
Chris Lattnerda7e4532002-12-15 20:36:09 +000042 public:
Chris Lattner8233e2f2002-12-15 21:13:12 +000043 virtual const char *getPassName() const {
44 return "Simple Register Allocator";
45 }
46
Chris Lattnerda7e4532002-12-15 20:36:09 +000047 /// runOnMachineFunction - Register allocate the whole function
48 bool runOnMachineFunction(MachineFunction &Fn);
49
Chris Lattner80a04782003-01-13 00:26:08 +000050 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51 AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
52 MachineFunctionPass::getAnalysisUsage(AU);
53 }
Chris Lattner600dee42002-12-28 20:42:14 +000054 private:
Chris Lattnerda7e4532002-12-15 20:36:09 +000055 /// AllocateBasicBlock - Register allocate the specified basic block.
56 void AllocateBasicBlock(MachineBasicBlock &MBB);
57
Chris Lattner9f366d72002-12-15 22:19:19 +000058 /// getStackSpaceFor - This returns the offset of the specified virtual
59 /// register on the stack, allocating space if neccesary.
Chris Lattner600dee42002-12-28 20:42:14 +000060 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Misha Brukman07218672002-11-22 22:44:32 +000061
Chris Lattner9f366d72002-12-15 22:19:19 +000062 /// Given a virtual register, return a compatible physical register that is
63 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000064 ///
Misha Brukman07218672002-11-22 22:44:32 +000065 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000066 ///
Misha Brukman07218672002-11-22 22:44:32 +000067 unsigned getFreeReg(unsigned virtualReg);
68
Misha Brukman07218672002-11-22 22:44:32 +000069 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +000070 unsigned reloadVirtReg(MachineBasicBlock &MBB,
71 MachineBasicBlock::iterator &I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +000072
73 /// Saves reg value on the stack (maps virtual register to stack value)
Chris Lattnerb167c042002-12-15 23:01:26 +000074 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
75 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +000076 };
77
Misha Brukman59b3eed2002-12-13 10:42:31 +000078}
Misha Brukman07218672002-11-22 22:44:32 +000079
Chris Lattner9f366d72002-12-15 22:19:19 +000080/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +000081/// register to be held on the stack.
Chris Lattner600dee42002-12-28 20:42:14 +000082int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
83 const TargetRegisterClass *RC) {
Chris Lattner9f366d72002-12-15 22:19:19 +000084 // Find the location VirtReg would belong...
Chris Lattner600dee42002-12-28 20:42:14 +000085 std::map<unsigned, int>::iterator I =
86 StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +000087
Chris Lattner600dee42002-12-28 20:42:14 +000088 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattner9f366d72002-12-15 22:19:19 +000089 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +000090
Chris Lattner600dee42002-12-28 20:42:14 +000091 // Allocate a new stack object for this spill location...
Chris Lattner80a04782003-01-13 00:26:08 +000092 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC);
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 +0000140void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000141 // loop over each instruction
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000142 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000143 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000144 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000145
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000146 MachineInstr *MI = *I;
Chris Lattner600dee42002-12-28 20:42:14 +0000147
148 RegsUsed.resize(MRegisterInfo::FirstVirtualRegister);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000149
150 // a preliminary pass that will invalidate any registers that
151 // are used by the instruction (including implicit uses)
Chris Lattner600dee42002-12-28 20:42:14 +0000152 unsigned Opcode = MI->getOpcode();
153 const MachineInstrDescriptor &Desc = TM->getInstrInfo().get(Opcode);
154 if (const unsigned *Regs = Desc.ImplicitUses)
155 while (*Regs)
156 RegsUsed[*Regs++] = true;
157
158 if (const unsigned *Regs = Desc.ImplicitDefs)
159 while (*Regs)
160 RegsUsed[*Regs++] = true;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000161
162 // Loop over uses, move from memory into registers
163 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
164 MachineOperand &op = MI->getOperand(i);
165
Chris Lattnerda7e4532002-12-15 20:36:09 +0000166 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000167 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
168 DEBUG(std::cerr << "op: " << op << "\n");
169 DEBUG(std::cerr << "\t inst[" << i << "]: ";
Chris Lattner600dee42002-12-28 20:42:14 +0000170 MI->print(std::cerr, *TM));
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000171
172 // make sure the same virtual register maps to the same physical
173 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000174 unsigned physReg = Virt2PhysRegMap[virtualReg];
175 if (physReg == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000176 if (op.opIsDef()) {
Chris Lattner600dee42002-12-28 20:42:14 +0000177 if (TM->getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000178 // must be same register number as the first operand
179 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000180 assert(MI->getOperand(1).isRegister() &&
181 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner9f366d72002-12-15 22:19:19 +0000182 MI->getOperand(1).opIsUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000183 "Two address instruction invalid!");
184
Chris Lattnerda7e4532002-12-15 20:36:09 +0000185 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000186 } else {
187 physReg = getFreeReg(virtualReg);
188 }
Chris Lattnerb167c042002-12-15 23:01:26 +0000189 ++I;
190 spillVirtReg(MBB, I, virtualReg, physReg);
191 --I;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000192 } else {
Chris Lattnerb167c042002-12-15 23:01:26 +0000193 physReg = reloadVirtReg(MBB, I, virtualReg);
194 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000195 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000196 }
197 MI->SetMachineOperandReg(i, physReg);
198 DEBUG(std::cerr << "virt: " << virtualReg <<
199 ", phys: " << op.getAllocatedRegNum() << "\n");
200 }
201 }
Chris Lattner600dee42002-12-28 20:42:14 +0000202 RegClassIdx.clear();
203 RegsUsed.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000204 }
205}
206
Chris Lattnere7d361d2002-12-17 04:19:40 +0000207
Chris Lattnerda7e4532002-12-15 20:36:09 +0000208/// runOnMachineFunction - Register allocate the whole function
209///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000210bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000211 DEBUG(std::cerr << "Machine Function " << "\n");
212 MF = &Fn;
Chris Lattner600dee42002-12-28 20:42:14 +0000213 TM = &MF->getTarget();
214 RegInfo = TM->getRegisterInfo();
Misha Brukmandc2ec002002-12-03 23:15:19 +0000215
Chris Lattner9f366d72002-12-15 22:19:19 +0000216 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000217 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
218 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000219 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000220
Chris Lattner600dee42002-12-28 20:42:14 +0000221 StackSlotForVirtReg.clear();
Chris Lattner9f366d72002-12-15 22:19:19 +0000222 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000223}
224
Chris Lattner600dee42002-12-28 20:42:14 +0000225Pass *createSimpleRegisterAllocator() {
226 return new RegAllocSimple();
Misha Brukman07218672002-11-22 22:44:32 +0000227}