blob: 202123ad106373c38de89a89ad9717cb21d3f524 [file] [log] [blame]
Chris Lattnerc63d63a2002-12-16 14:37:00 +00001//===-- RegAllocSimple.cpp - A simple generic register allocator ----------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Misha Brukman60286d02002-11-22 22:44:32 +00009//
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000010// 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.
Misha Brukman60286d02002-11-22 22:44:32 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner74e4e9b2003-08-03 21:47:31 +000017#define DEBUG_TYPE "regalloc"
Chris Lattnercbedb8b2003-01-13 00:26:08 +000018#include "llvm/CodeGen/Passes.h"
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnercf1955c2002-12-15 18:19:24 +000020#include "llvm/CodeGen/MachineInstr.h"
Chris Lattneree734502002-12-25 05:04:20 +000021#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerca4362f2002-12-28 21:08:26 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerb4d58d72003-01-14 22:00:31 +000023#include "llvm/Target/TargetInstrInfo.h"
Misha Brukman60286d02002-11-22 22:44:32 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattner1007f032003-08-01 22:21:34 +000025#include "Support/Debug.h"
Misha Brukman60286d02002-11-22 22:44:32 +000026#include "Support/Statistic.h"
Chris Lattnercf1955c2002-12-15 18:19:24 +000027#include <iostream>
Misha Brukman60286d02002-11-22 22:44:32 +000028
Brian Gaeke960707c2003-11-11 22:41:34 +000029namespace llvm {
30
Misha Brukman89ff3fb2002-12-13 10:42:31 +000031namespace {
Chris Lattnerdfa238f2002-12-15 20:36:09 +000032 Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
33 Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
34
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000035 class RegAllocSimple : public MachineFunctionPass {
Misha Brukman60286d02002-11-22 22:44:32 +000036 MachineFunction *MF;
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000037 const TargetMachine *TM;
Misha Brukman60286d02002-11-22 22:44:32 +000038 const MRegisterInfo *RegInfo;
Misha Brukman60286d02002-11-22 22:44:32 +000039
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000040 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
41 // these values are spilled
42 std::map<unsigned, int> StackSlotForVirtReg;
Misha Brukman60286d02002-11-22 22:44:32 +000043
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000044 // RegsUsed - Keep track of what registers are currently in use. This is a
45 // bitset.
46 std::vector<bool> RegsUsed;
Chris Lattnerdfa238f2002-12-15 20:36:09 +000047
48 // RegClassIdx - Maps RegClass => which index we can take a register
49 // from. Since this is a simple register allocator, when we need a register
50 // of a certain class, we just take the next available one.
Misha Brukman60286d02002-11-22 22:44:32 +000051 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
52
Chris Lattnerdfa238f2002-12-15 20:36:09 +000053 public:
Chris Lattner1499e5a2002-12-15 21:13:12 +000054 virtual const char *getPassName() const {
55 return "Simple Register Allocator";
56 }
57
Chris Lattnerdfa238f2002-12-15 20:36:09 +000058 /// runOnMachineFunction - Register allocate the whole function
59 bool runOnMachineFunction(MachineFunction &Fn);
60
Chris Lattnercbedb8b2003-01-13 00:26:08 +000061 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
62 AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
63 MachineFunctionPass::getAnalysisUsage(AU);
64 }
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000065 private:
Chris Lattnerdfa238f2002-12-15 20:36:09 +000066 /// AllocateBasicBlock - Register allocate the specified basic block.
67 void AllocateBasicBlock(MachineBasicBlock &MBB);
68
Chris Lattner292083a2002-12-15 22:19:19 +000069 /// getStackSpaceFor - This returns the offset of the specified virtual
Misha Brukman7eb05a12003-08-18 14:43:39 +000070 /// register on the stack, allocating space if necessary.
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000071 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Misha Brukman60286d02002-11-22 22:44:32 +000072
Chris Lattner292083a2002-12-15 22:19:19 +000073 /// Given a virtual register, return a compatible physical register that is
74 /// currently unused.
Chris Lattnerdfa238f2002-12-15 20:36:09 +000075 ///
Misha Brukman60286d02002-11-22 22:44:32 +000076 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerdfa238f2002-12-15 20:36:09 +000077 ///
Misha Brukman60286d02002-11-22 22:44:32 +000078 unsigned getFreeReg(unsigned virtualReg);
79
Misha Brukman60286d02002-11-22 22:44:32 +000080 /// Moves value from memory into that register
Chris Lattnerbc1e6702002-12-15 23:01:26 +000081 unsigned reloadVirtReg(MachineBasicBlock &MBB,
82 MachineBasicBlock::iterator &I, unsigned VirtReg);
Misha Brukman60286d02002-11-22 22:44:32 +000083
84 /// Saves reg value on the stack (maps virtual register to stack value)
Chris Lattnerbc1e6702002-12-15 23:01:26 +000085 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
86 unsigned VirtReg, unsigned PhysReg);
Misha Brukman60286d02002-11-22 22:44:32 +000087 };
88
Misha Brukman89ff3fb2002-12-13 10:42:31 +000089}
Misha Brukman60286d02002-11-22 22:44:32 +000090
Chris Lattner292083a2002-12-15 22:19:19 +000091/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc1c7cc22002-12-15 19:51:14 +000092/// register to be held on the stack.
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000093int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
94 const TargetRegisterClass *RC) {
Chris Lattner292083a2002-12-15 22:19:19 +000095 // Find the location VirtReg would belong...
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000096 std::map<unsigned, int>::iterator I =
97 StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattnerf2acd842002-12-15 19:07:34 +000098
Chris Lattnerbf9d12a2002-12-28 20:42:14 +000099 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattner292083a2002-12-15 22:19:19 +0000100 return I->second; // Already has space allocated?
Chris Lattnerf2acd842002-12-15 19:07:34 +0000101
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000102 // Allocate a new stack object for this spill location...
Chris Lattnercbedb8b2003-01-13 00:26:08 +0000103 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC);
Chris Lattner292083a2002-12-15 22:19:19 +0000104
105 // Assign the slot...
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000106 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
107
108 return FrameIdx;
Misha Brukman2e035d62002-12-02 21:11:58 +0000109}
110
Misha Brukman60286d02002-11-22 22:44:32 +0000111unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattneree734502002-12-25 05:04:20 +0000112 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(virtualReg);
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000113 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
114 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Misha Brukman60286d02002-11-22 22:44:32 +0000115
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000116 while (1) {
117 unsigned regIdx = RegClassIdx[RC]++;
118 assert(RI+regIdx != RE && "Not enough registers!");
119 unsigned PhysReg = *(RI+regIdx);
120
121 if (!RegsUsed[PhysReg])
122 return PhysReg;
123 }
Misha Brukman60286d02002-11-22 22:44:32 +0000124}
125
Chris Lattnerbc1e6702002-12-15 23:01:26 +0000126unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
127 MachineBasicBlock::iterator &I,
128 unsigned VirtReg) {
Chris Lattneree734502002-12-25 05:04:20 +0000129 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000130 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Chris Lattnerbc1e6702002-12-15 23:01:26 +0000131 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman60286d02002-11-22 22:44:32 +0000132
Misha Brukman2e035d62002-12-02 21:11:58 +0000133 // Add move instruction(s)
Chris Lattnerdfa238f2002-12-15 20:36:09 +0000134 ++NumReloaded;
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000135 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Chris Lattnerbc1e6702002-12-15 23:01:26 +0000136 return PhysReg;
Misha Brukman60286d02002-11-22 22:44:32 +0000137}
138
Chris Lattnerbc1e6702002-12-15 23:01:26 +0000139void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
140 MachineBasicBlock::iterator &I,
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000141 unsigned VirtReg, unsigned PhysReg) {
Chris Lattneree734502002-12-25 05:04:20 +0000142 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000143 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Misha Brukman2e035d62002-12-02 21:11:58 +0000144
Misha Brukman60286d02002-11-22 22:44:32 +0000145 // Add move instruction(s)
Chris Lattnerdfa238f2002-12-15 20:36:09 +0000146 ++NumSpilled;
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000147 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Misha Brukman60286d02002-11-22 22:44:32 +0000148}
149
Misha Brukmana8ad9322002-12-03 23:15:19 +0000150
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000151void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattner2af545d2002-12-15 21:33:51 +0000152 // loop over each instruction
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000153 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattnered594b62002-12-15 21:24:30 +0000154 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner292083a2002-12-15 22:19:19 +0000155 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattnered594b62002-12-15 21:24:30 +0000156
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000157 MachineInstr *MI = *I;
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000158
159 RegsUsed.resize(MRegisterInfo::FirstVirtualRegister);
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000160
161 // a preliminary pass that will invalidate any registers that
162 // are used by the instruction (including implicit uses)
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000163 unsigned Opcode = MI->getOpcode();
Chris Lattnerb4d58d72003-01-14 22:00:31 +0000164 const TargetInstrDescriptor &Desc = TM->getInstrInfo().get(Opcode);
Alkis Evlogimenos5f1f3372003-10-08 05:20:08 +0000165 const unsigned *Regs = Desc.ImplicitUses;
166 while (*Regs)
167 RegsUsed[*Regs++] = true;
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000168
Alkis Evlogimenos5f1f3372003-10-08 05:20:08 +0000169 Regs = Desc.ImplicitDefs;
170 while (*Regs)
171 RegsUsed[*Regs++] = true;
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000172
173 // Loop over uses, move from memory into registers
174 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
175 MachineOperand &op = MI->getOperand(i);
176
Chris Lattnerdfa238f2002-12-15 20:36:09 +0000177 if (op.isVirtualRegister()) {
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000178 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
179 DEBUG(std::cerr << "op: " << op << "\n");
180 DEBUG(std::cerr << "\t inst[" << i << "]: ";
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000181 MI->print(std::cerr, *TM));
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000182
183 // make sure the same virtual register maps to the same physical
184 // register in any given instruction
Chris Lattner292083a2002-12-15 22:19:19 +0000185 unsigned physReg = Virt2PhysRegMap[virtualReg];
186 if (physReg == 0) {
Vikram S. Adve7366fa12003-05-27 00:05:23 +0000187 if (op.opIsDefOnly() || op.opIsDefAndUse()) {
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000188 if (TM->getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000189 // must be same register number as the first operand
190 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner2979a852002-12-15 21:02:20 +0000191 assert(MI->getOperand(1).isRegister() &&
192 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner292083a2002-12-15 22:19:19 +0000193 MI->getOperand(1).opIsUse() &&
Chris Lattner2979a852002-12-15 21:02:20 +0000194 "Two address instruction invalid!");
195
Chris Lattnerdfa238f2002-12-15 20:36:09 +0000196 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000197 } else {
198 physReg = getFreeReg(virtualReg);
199 }
Chris Lattnerbc1e6702002-12-15 23:01:26 +0000200 ++I;
201 spillVirtReg(MBB, I, virtualReg, physReg);
202 --I;
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000203 } else {
Chris Lattnerbc1e6702002-12-15 23:01:26 +0000204 physReg = reloadVirtReg(MBB, I, virtualReg);
205 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000206 }
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000207 }
208 MI->SetMachineOperandReg(i, physReg);
209 DEBUG(std::cerr << "virt: " << virtualReg <<
210 ", phys: " << op.getAllocatedRegNum() << "\n");
211 }
212 }
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000213 RegClassIdx.clear();
214 RegsUsed.clear();
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000215 }
216}
217
Chris Lattnerac5f3b32002-12-17 04:19:40 +0000218
Chris Lattnerdfa238f2002-12-15 20:36:09 +0000219/// runOnMachineFunction - Register allocate the whole function
220///
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000221bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman60286d02002-11-22 22:44:32 +0000222 DEBUG(std::cerr << "Machine Function " << "\n");
223 MF = &Fn;
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000224 TM = &MF->getTarget();
225 RegInfo = TM->getRegisterInfo();
Misha Brukmana8ad9322002-12-03 23:15:19 +0000226
Chris Lattner292083a2002-12-15 22:19:19 +0000227 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman60286d02002-11-22 22:44:32 +0000228 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
229 MBB != MBBe; ++MBB)
Chris Lattnerc1c7cc22002-12-15 19:51:14 +0000230 AllocateBasicBlock(*MBB);
Misha Brukman60286d02002-11-22 22:44:32 +0000231
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000232 StackSlotForVirtReg.clear();
Chris Lattner292083a2002-12-15 22:19:19 +0000233 return true;
Misha Brukman60286d02002-11-22 22:44:32 +0000234}
235
Brian Gaeke89207942003-08-13 18:18:15 +0000236FunctionPass *createSimpleRegisterAllocator() {
Chris Lattnerbf9d12a2002-12-28 20:42:14 +0000237 return new RegAllocSimple();
Misha Brukman60286d02002-11-22 22:44:32 +0000238}
Brian Gaeke960707c2003-11-11 22:41:34 +0000239
240} // End llvm namespace