blob: ba571e4116e4692fe3e22d5972d117838018b43c [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 Lattner4cc662b2003-08-03 21:47:31 +000010#define DEBUG_TYPE "regalloc"
Chris Lattner80a04782003-01-13 00:26:08 +000011#include "llvm/CodeGen/Passes.h"
Chris Lattner600dee42002-12-28 20:42:14 +000012#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000013#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner5124aec2002-12-25 05:04:20 +000014#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000015#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000016#include "llvm/Target/TargetInstrInfo.h"
Misha Brukman07218672002-11-22 22:44:32 +000017#include "llvm/Target/TargetMachine.h"
Chris Lattnera11136b2003-08-01 22:21:34 +000018#include "Support/Debug.h"
Misha Brukman07218672002-11-22 22:44:32 +000019#include "Support/Statistic.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000020#include <iostream>
Misha Brukman07218672002-11-22 22:44:32 +000021
Misha Brukman59b3eed2002-12-13 10:42:31 +000022namespace {
Chris Lattnerda7e4532002-12-15 20:36:09 +000023 Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
24 Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
25
Chris Lattner600dee42002-12-28 20:42:14 +000026 class RegAllocSimple : public MachineFunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000027 MachineFunction *MF;
Chris Lattner600dee42002-12-28 20:42:14 +000028 const TargetMachine *TM;
Misha Brukman07218672002-11-22 22:44:32 +000029 const MRegisterInfo *RegInfo;
Misha Brukman07218672002-11-22 22:44:32 +000030
Chris Lattner600dee42002-12-28 20:42:14 +000031 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
32 // these values are spilled
33 std::map<unsigned, int> StackSlotForVirtReg;
Misha Brukman07218672002-11-22 22:44:32 +000034
Chris Lattner600dee42002-12-28 20:42:14 +000035 // RegsUsed - Keep track of what registers are currently in use. This is a
36 // bitset.
37 std::vector<bool> RegsUsed;
Chris Lattnerda7e4532002-12-15 20:36:09 +000038
39 // RegClassIdx - Maps RegClass => which index we can take a register
40 // from. Since this is a simple register allocator, when we need a register
41 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000042 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
43
Chris Lattnerda7e4532002-12-15 20:36:09 +000044 public:
Chris Lattner8233e2f2002-12-15 21:13:12 +000045 virtual const char *getPassName() const {
46 return "Simple Register Allocator";
47 }
48
Chris Lattnerda7e4532002-12-15 20:36:09 +000049 /// runOnMachineFunction - Register allocate the whole function
50 bool runOnMachineFunction(MachineFunction &Fn);
51
Chris Lattner80a04782003-01-13 00:26:08 +000052 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
54 MachineFunctionPass::getAnalysisUsage(AU);
55 }
Chris Lattner600dee42002-12-28 20:42:14 +000056 private:
Chris Lattnerda7e4532002-12-15 20:36:09 +000057 /// AllocateBasicBlock - Register allocate the specified basic block.
58 void AllocateBasicBlock(MachineBasicBlock &MBB);
59
Chris Lattner9f366d72002-12-15 22:19:19 +000060 /// getStackSpaceFor - This returns the offset of the specified virtual
61 /// register on the stack, allocating space if neccesary.
Chris Lattner600dee42002-12-28 20:42:14 +000062 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Misha Brukman07218672002-11-22 22:44:32 +000063
Chris Lattner9f366d72002-12-15 22:19:19 +000064 /// Given a virtual register, return a compatible physical register that is
65 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000066 ///
Misha Brukman07218672002-11-22 22:44:32 +000067 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000068 ///
Misha Brukman07218672002-11-22 22:44:32 +000069 unsigned getFreeReg(unsigned virtualReg);
70
Misha Brukman07218672002-11-22 22:44:32 +000071 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +000072 unsigned reloadVirtReg(MachineBasicBlock &MBB,
73 MachineBasicBlock::iterator &I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +000074
75 /// Saves reg value on the stack (maps virtual register to stack value)
Chris Lattnerb167c042002-12-15 23:01:26 +000076 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
77 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +000078 };
79
Misha Brukman59b3eed2002-12-13 10:42:31 +000080}
Misha Brukman07218672002-11-22 22:44:32 +000081
Chris Lattner9f366d72002-12-15 22:19:19 +000082/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +000083/// register to be held on the stack.
Chris Lattner600dee42002-12-28 20:42:14 +000084int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
85 const TargetRegisterClass *RC) {
Chris Lattner9f366d72002-12-15 22:19:19 +000086 // Find the location VirtReg would belong...
Chris Lattner600dee42002-12-28 20:42:14 +000087 std::map<unsigned, int>::iterator I =
88 StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +000089
Chris Lattner600dee42002-12-28 20:42:14 +000090 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattner9f366d72002-12-15 22:19:19 +000091 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +000092
Chris Lattner600dee42002-12-28 20:42:14 +000093 // Allocate a new stack object for this spill location...
Chris Lattner80a04782003-01-13 00:26:08 +000094 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC);
Chris Lattner9f366d72002-12-15 22:19:19 +000095
96 // Assign the slot...
Chris Lattner600dee42002-12-28 20:42:14 +000097 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
98
99 return FrameIdx;
Misha Brukmanf514d512002-12-02 21:11:58 +0000100}
101
Misha Brukman07218672002-11-22 22:44:32 +0000102unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000103 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(virtualReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000104 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
105 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Misha Brukman07218672002-11-22 22:44:32 +0000106
Chris Lattner600dee42002-12-28 20:42:14 +0000107 while (1) {
108 unsigned regIdx = RegClassIdx[RC]++;
109 assert(RI+regIdx != RE && "Not enough registers!");
110 unsigned PhysReg = *(RI+regIdx);
111
112 if (!RegsUsed[PhysReg])
113 return PhysReg;
114 }
Misha Brukman07218672002-11-22 22:44:32 +0000115}
116
Chris Lattnerb167c042002-12-15 23:01:26 +0000117unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
118 MachineBasicBlock::iterator &I,
119 unsigned VirtReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000120 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000121 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000122 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000123
Misha Brukmanf514d512002-12-02 21:11:58 +0000124 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000125 ++NumReloaded;
Chris Lattner600dee42002-12-28 20:42:14 +0000126 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000127 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000128}
129
Chris Lattnerb167c042002-12-15 23:01:26 +0000130void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
131 MachineBasicBlock::iterator &I,
Chris Lattner600dee42002-12-28 20:42:14 +0000132 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000133 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000134 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Misha Brukmanf514d512002-12-02 21:11:58 +0000135
Misha Brukman07218672002-11-22 22:44:32 +0000136 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000137 ++NumSpilled;
Chris Lattner600dee42002-12-28 20:42:14 +0000138 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Misha Brukman07218672002-11-22 22:44:32 +0000139}
140
Misha Brukmandc2ec002002-12-03 23:15:19 +0000141
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000142void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000143 // loop over each instruction
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000144 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000145 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000146 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000147
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000148 MachineInstr *MI = *I;
Chris Lattner600dee42002-12-28 20:42:14 +0000149
150 RegsUsed.resize(MRegisterInfo::FirstVirtualRegister);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000151
152 // a preliminary pass that will invalidate any registers that
153 // are used by the instruction (including implicit uses)
Chris Lattner600dee42002-12-28 20:42:14 +0000154 unsigned Opcode = MI->getOpcode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000155 const TargetInstrDescriptor &Desc = TM->getInstrInfo().get(Opcode);
Chris Lattner600dee42002-12-28 20:42:14 +0000156 if (const unsigned *Regs = Desc.ImplicitUses)
157 while (*Regs)
158 RegsUsed[*Regs++] = true;
159
160 if (const unsigned *Regs = Desc.ImplicitDefs)
161 while (*Regs)
162 RegsUsed[*Regs++] = true;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000163
164 // Loop over uses, move from memory into registers
165 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
166 MachineOperand &op = MI->getOperand(i);
167
Chris Lattnerda7e4532002-12-15 20:36:09 +0000168 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000169 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
170 DEBUG(std::cerr << "op: " << op << "\n");
171 DEBUG(std::cerr << "\t inst[" << i << "]: ";
Chris Lattner600dee42002-12-28 20:42:14 +0000172 MI->print(std::cerr, *TM));
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000173
174 // make sure the same virtual register maps to the same physical
175 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000176 unsigned physReg = Virt2PhysRegMap[virtualReg];
177 if (physReg == 0) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000178 if (op.opIsDefOnly() || op.opIsDefAndUse()) {
Chris Lattner600dee42002-12-28 20:42:14 +0000179 if (TM->getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000180 // must be same register number as the first operand
181 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000182 assert(MI->getOperand(1).isRegister() &&
183 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner9f366d72002-12-15 22:19:19 +0000184 MI->getOperand(1).opIsUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000185 "Two address instruction invalid!");
186
Chris Lattnerda7e4532002-12-15 20:36:09 +0000187 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000188 } else {
189 physReg = getFreeReg(virtualReg);
190 }
Chris Lattnerb167c042002-12-15 23:01:26 +0000191 ++I;
192 spillVirtReg(MBB, I, virtualReg, physReg);
193 --I;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000194 } else {
Chris Lattnerb167c042002-12-15 23:01:26 +0000195 physReg = reloadVirtReg(MBB, I, virtualReg);
196 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000197 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000198 }
199 MI->SetMachineOperandReg(i, physReg);
200 DEBUG(std::cerr << "virt: " << virtualReg <<
201 ", phys: " << op.getAllocatedRegNum() << "\n");
202 }
203 }
Chris Lattner600dee42002-12-28 20:42:14 +0000204 RegClassIdx.clear();
205 RegsUsed.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000206 }
207}
208
Chris Lattnere7d361d2002-12-17 04:19:40 +0000209
Chris Lattnerda7e4532002-12-15 20:36:09 +0000210/// runOnMachineFunction - Register allocate the whole function
211///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000212bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000213 DEBUG(std::cerr << "Machine Function " << "\n");
214 MF = &Fn;
Chris Lattner600dee42002-12-28 20:42:14 +0000215 TM = &MF->getTarget();
216 RegInfo = TM->getRegisterInfo();
Misha Brukmandc2ec002002-12-03 23:15:19 +0000217
Chris Lattner9f366d72002-12-15 22:19:19 +0000218 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000219 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
220 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000221 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000222
Chris Lattner600dee42002-12-28 20:42:14 +0000223 StackSlotForVirtReg.clear();
Chris Lattner9f366d72002-12-15 22:19:19 +0000224 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000225}
226
Brian Gaeke19df3872003-08-13 18:18:15 +0000227FunctionPass *createSimpleRegisterAllocator() {
Chris Lattner600dee42002-12-28 20:42:14 +0000228 return new RegAllocSimple();
Misha Brukman07218672002-11-22 22:44:32 +0000229}