blob: 4d286d9952ebe381029e8de9caa75210bab4df90 [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"
Chris Lattner3501fea2003-01-14 22:00:31 +000015#include "llvm/Target/TargetInstrInfo.h"
Misha Brukman07218672002-11-22 22:44:32 +000016#include "llvm/Target/TargetMachine.h"
Chris Lattnera11136b2003-08-01 22:21:34 +000017#include "Support/Debug.h"
Misha Brukman07218672002-11-22 22:44:32 +000018#include "Support/Statistic.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000019#include <iostream>
Misha Brukman07218672002-11-22 22:44:32 +000020
Misha Brukman59b3eed2002-12-13 10:42:31 +000021namespace {
Chris Lattnerda7e4532002-12-15 20:36:09 +000022 Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
23 Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
24
Chris Lattner600dee42002-12-28 20:42:14 +000025 class RegAllocSimple : public MachineFunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000026 MachineFunction *MF;
Chris Lattner600dee42002-12-28 20:42:14 +000027 const TargetMachine *TM;
Misha Brukman07218672002-11-22 22:44:32 +000028 const MRegisterInfo *RegInfo;
Misha Brukman07218672002-11-22 22:44:32 +000029
Chris Lattner600dee42002-12-28 20:42:14 +000030 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
31 // these values are spilled
32 std::map<unsigned, int> StackSlotForVirtReg;
Misha Brukman07218672002-11-22 22:44:32 +000033
Chris Lattner600dee42002-12-28 20:42:14 +000034 // RegsUsed - Keep track of what registers are currently in use. This is a
35 // bitset.
36 std::vector<bool> RegsUsed;
Chris Lattnerda7e4532002-12-15 20:36:09 +000037
38 // RegClassIdx - Maps RegClass => which index we can take a register
39 // from. Since this is a simple register allocator, when we need a register
40 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000041 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
42
Chris Lattnerda7e4532002-12-15 20:36:09 +000043 public:
Chris Lattner8233e2f2002-12-15 21:13:12 +000044 virtual const char *getPassName() const {
45 return "Simple Register Allocator";
46 }
47
Chris Lattnerda7e4532002-12-15 20:36:09 +000048 /// runOnMachineFunction - Register allocate the whole function
49 bool runOnMachineFunction(MachineFunction &Fn);
50
Chris Lattner80a04782003-01-13 00:26:08 +000051 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
53 MachineFunctionPass::getAnalysisUsage(AU);
54 }
Chris Lattner600dee42002-12-28 20:42:14 +000055 private:
Chris Lattnerda7e4532002-12-15 20:36:09 +000056 /// AllocateBasicBlock - Register allocate the specified basic block.
57 void AllocateBasicBlock(MachineBasicBlock &MBB);
58
Chris Lattner9f366d72002-12-15 22:19:19 +000059 /// getStackSpaceFor - This returns the offset of the specified virtual
60 /// register on the stack, allocating space if neccesary.
Chris Lattner600dee42002-12-28 20:42:14 +000061 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Misha Brukman07218672002-11-22 22:44:32 +000062
Chris Lattner9f366d72002-12-15 22:19:19 +000063 /// Given a virtual register, return a compatible physical register that is
64 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000065 ///
Misha Brukman07218672002-11-22 22:44:32 +000066 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000067 ///
Misha Brukman07218672002-11-22 22:44:32 +000068 unsigned getFreeReg(unsigned virtualReg);
69
Misha Brukman07218672002-11-22 22:44:32 +000070 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +000071 unsigned reloadVirtReg(MachineBasicBlock &MBB,
72 MachineBasicBlock::iterator &I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +000073
74 /// Saves reg value on the stack (maps virtual register to stack value)
Chris Lattnerb167c042002-12-15 23:01:26 +000075 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
76 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +000077 };
78
Misha Brukman59b3eed2002-12-13 10:42:31 +000079}
Misha Brukman07218672002-11-22 22:44:32 +000080
Chris Lattner9f366d72002-12-15 22:19:19 +000081/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +000082/// register to be held on the stack.
Chris Lattner600dee42002-12-28 20:42:14 +000083int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
84 const TargetRegisterClass *RC) {
Chris Lattner9f366d72002-12-15 22:19:19 +000085 // Find the location VirtReg would belong...
Chris Lattner600dee42002-12-28 20:42:14 +000086 std::map<unsigned, int>::iterator I =
87 StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +000088
Chris Lattner600dee42002-12-28 20:42:14 +000089 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattner9f366d72002-12-15 22:19:19 +000090 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +000091
Chris Lattner600dee42002-12-28 20:42:14 +000092 // Allocate a new stack object for this spill location...
Chris Lattner80a04782003-01-13 00:26:08 +000093 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC);
Chris Lattner9f366d72002-12-15 22:19:19 +000094
95 // Assign the slot...
Chris Lattner600dee42002-12-28 20:42:14 +000096 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
97
98 return FrameIdx;
Misha Brukmanf514d512002-12-02 21:11:58 +000099}
100
Misha Brukman07218672002-11-22 22:44:32 +0000101unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000102 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(virtualReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000103 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
104 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Misha Brukman07218672002-11-22 22:44:32 +0000105
Chris Lattner600dee42002-12-28 20:42:14 +0000106 while (1) {
107 unsigned regIdx = RegClassIdx[RC]++;
108 assert(RI+regIdx != RE && "Not enough registers!");
109 unsigned PhysReg = *(RI+regIdx);
110
111 if (!RegsUsed[PhysReg])
112 return PhysReg;
113 }
Misha Brukman07218672002-11-22 22:44:32 +0000114}
115
Chris Lattnerb167c042002-12-15 23:01:26 +0000116unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
117 MachineBasicBlock::iterator &I,
118 unsigned VirtReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000119 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000120 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000121 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000122
Misha Brukmanf514d512002-12-02 21:11:58 +0000123 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000124 ++NumReloaded;
Chris Lattner600dee42002-12-28 20:42:14 +0000125 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000126 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000127}
128
Chris Lattnerb167c042002-12-15 23:01:26 +0000129void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
130 MachineBasicBlock::iterator &I,
Chris Lattner600dee42002-12-28 20:42:14 +0000131 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000132 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000133 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Misha Brukmanf514d512002-12-02 21:11:58 +0000134
Misha Brukman07218672002-11-22 22:44:32 +0000135 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000136 ++NumSpilled;
Chris Lattner600dee42002-12-28 20:42:14 +0000137 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Misha Brukman07218672002-11-22 22:44:32 +0000138}
139
Misha Brukmandc2ec002002-12-03 23:15:19 +0000140
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000141void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000142 // loop over each instruction
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000143 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000144 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000145 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000146
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000147 MachineInstr *MI = *I;
Chris Lattner600dee42002-12-28 20:42:14 +0000148
149 RegsUsed.resize(MRegisterInfo::FirstVirtualRegister);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000150
151 // a preliminary pass that will invalidate any registers that
152 // are used by the instruction (including implicit uses)
Chris Lattner600dee42002-12-28 20:42:14 +0000153 unsigned Opcode = MI->getOpcode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000154 const TargetInstrDescriptor &Desc = TM->getInstrInfo().get(Opcode);
Chris Lattner600dee42002-12-28 20:42:14 +0000155 if (const unsigned *Regs = Desc.ImplicitUses)
156 while (*Regs)
157 RegsUsed[*Regs++] = true;
158
159 if (const unsigned *Regs = Desc.ImplicitDefs)
160 while (*Regs)
161 RegsUsed[*Regs++] = true;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000162
163 // Loop over uses, move from memory into registers
164 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
165 MachineOperand &op = MI->getOperand(i);
166
Chris Lattnerda7e4532002-12-15 20:36:09 +0000167 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000168 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
169 DEBUG(std::cerr << "op: " << op << "\n");
170 DEBUG(std::cerr << "\t inst[" << i << "]: ";
Chris Lattner600dee42002-12-28 20:42:14 +0000171 MI->print(std::cerr, *TM));
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000172
173 // make sure the same virtual register maps to the same physical
174 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000175 unsigned physReg = Virt2PhysRegMap[virtualReg];
176 if (physReg == 0) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000177 if (op.opIsDefOnly() || op.opIsDefAndUse()) {
Chris Lattner600dee42002-12-28 20:42:14 +0000178 if (TM->getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000179 // must be same register number as the first operand
180 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000181 assert(MI->getOperand(1).isRegister() &&
182 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner9f366d72002-12-15 22:19:19 +0000183 MI->getOperand(1).opIsUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000184 "Two address instruction invalid!");
185
Chris Lattnerda7e4532002-12-15 20:36:09 +0000186 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000187 } else {
188 physReg = getFreeReg(virtualReg);
189 }
Chris Lattnerb167c042002-12-15 23:01:26 +0000190 ++I;
191 spillVirtReg(MBB, I, virtualReg, physReg);
192 --I;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000193 } else {
Chris Lattnerb167c042002-12-15 23:01:26 +0000194 physReg = reloadVirtReg(MBB, I, virtualReg);
195 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000196 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000197 }
198 MI->SetMachineOperandReg(i, physReg);
199 DEBUG(std::cerr << "virt: " << virtualReg <<
200 ", phys: " << op.getAllocatedRegNum() << "\n");
201 }
202 }
Chris Lattner600dee42002-12-28 20:42:14 +0000203 RegClassIdx.clear();
204 RegsUsed.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000205 }
206}
207
Chris Lattnere7d361d2002-12-17 04:19:40 +0000208
Chris Lattnerda7e4532002-12-15 20:36:09 +0000209/// runOnMachineFunction - Register allocate the whole function
210///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000211bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000212 DEBUG(std::cerr << "Machine Function " << "\n");
213 MF = &Fn;
Chris Lattner600dee42002-12-28 20:42:14 +0000214 TM = &MF->getTarget();
215 RegInfo = TM->getRegisterInfo();
Misha Brukmandc2ec002002-12-03 23:15:19 +0000216
Chris Lattner9f366d72002-12-15 22:19:19 +0000217 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000218 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
219 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000220 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000221
Chris Lattner600dee42002-12-28 20:42:14 +0000222 StackSlotForVirtReg.clear();
Chris Lattner9f366d72002-12-15 22:19:19 +0000223 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000224}
225
Chris Lattner600dee42002-12-28 20:42:14 +0000226Pass *createSimpleRegisterAllocator() {
227 return new RegAllocSimple();
Misha Brukman07218672002-11-22 22:44:32 +0000228}