blob: 00f3180a137a39e02aeed9184d1f014b7d8f3413 [file] [log] [blame]
Chris Lattner1d62cea2002-12-16 14:37:00 +00001//===-- RegAllocSimple.cpp - A simple generic register allocator ----------===//
John Criswellb576c942003-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 Brukman07218672002-11-22 22:44:32 +00009//
Chris Lattner600dee42002-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 Brukman07218672002-11-22 22:44:32 +000014//
15//===----------------------------------------------------------------------===//
16
Chris Lattner4cc662b2003-08-03 21:47:31 +000017#define DEBUG_TYPE "regalloc"
Chris Lattner80a04782003-01-13 00:26:08 +000018#include "llvm/CodeGen/Passes.h"
Chris Lattner600dee42002-12-28 20:42:14 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000020#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner5124aec2002-12-25 05:04:20 +000021#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000023#include "llvm/Target/TargetInstrInfo.h"
Misha Brukman07218672002-11-22 22:44:32 +000024#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000025#include "llvm/Support/Debug.h"
26#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/STLExtras.h"
Chris Lattner5aaf1d22004-02-15 21:38:28 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Misha Brukman59b3eed2002-12-13 10:42:31 +000030namespace {
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +000031 Statistic<> NumStores("ra-simple", "Number of stores added");
32 Statistic<> NumLoads ("ra-simple", "Number of loads added");
Chris Lattnerda7e4532002-12-15 20:36:09 +000033
Chris Lattner600dee42002-12-28 20:42:14 +000034 class RegAllocSimple : public MachineFunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000035 MachineFunction *MF;
Chris Lattner600dee42002-12-28 20:42:14 +000036 const TargetMachine *TM;
Misha Brukman07218672002-11-22 22:44:32 +000037 const MRegisterInfo *RegInfo;
Chris Lattner78611632005-01-23 22:55:45 +000038 bool *PhysRegsEverUsed;
Misha Brukman07218672002-11-22 22:44:32 +000039
Chris Lattner600dee42002-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 Brukman07218672002-11-22 22:44:32 +000043
Chris Lattner600dee42002-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 Lattnerda7e4532002-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 Brukman07218672002-11-22 22:44:32 +000051 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
52
Chris Lattnerda7e4532002-12-15 20:36:09 +000053 public:
Chris Lattner8233e2f2002-12-15 21:13:12 +000054 virtual const char *getPassName() const {
55 return "Simple Register Allocator";
56 }
57
Chris Lattnerda7e4532002-12-15 20:36:09 +000058 /// runOnMachineFunction - Register allocate the whole function
59 bool runOnMachineFunction(MachineFunction &Fn);
60
Chris Lattner80a04782003-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 Lattner600dee42002-12-28 20:42:14 +000065 private:
Chris Lattnerda7e4532002-12-15 20:36:09 +000066 /// AllocateBasicBlock - Register allocate the specified basic block.
67 void AllocateBasicBlock(MachineBasicBlock &MBB);
68
Chris Lattner9f366d72002-12-15 22:19:19 +000069 /// getStackSpaceFor - This returns the offset of the specified virtual
Misha Brukman5560c9d2003-08-18 14:43:39 +000070 /// register on the stack, allocating space if necessary.
Chris Lattner600dee42002-12-28 20:42:14 +000071 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Misha Brukman07218672002-11-22 22:44:32 +000072
Chris Lattner9f366d72002-12-15 22:19:19 +000073 /// Given a virtual register, return a compatible physical register that is
74 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000075 ///
Misha Brukman07218672002-11-22 22:44:32 +000076 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000077 ///
Misha Brukman07218672002-11-22 22:44:32 +000078 unsigned getFreeReg(unsigned virtualReg);
79
Misha Brukman07218672002-11-22 22:44:32 +000080 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +000081 unsigned reloadVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +000082 MachineBasicBlock::iterator I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +000083
84 /// Saves reg value on the stack (maps virtual register to stack value)
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +000085 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Chris Lattnerb167c042002-12-15 23:01:26 +000086 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +000087 };
88
Misha Brukman59b3eed2002-12-13 10:42:31 +000089}
Misha Brukman07218672002-11-22 22:44:32 +000090
Chris Lattner9f366d72002-12-15 22:19:19 +000091/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +000092/// register to be held on the stack.
Chris Lattner600dee42002-12-28 20:42:14 +000093int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
94 const TargetRegisterClass *RC) {
Chris Lattner9f366d72002-12-15 22:19:19 +000095 // Find the location VirtReg would belong...
Chris Lattner600dee42002-12-28 20:42:14 +000096 std::map<unsigned, int>::iterator I =
97 StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +000098
Chris Lattner600dee42002-12-28 20:42:14 +000099 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattner9f366d72002-12-15 22:19:19 +0000100 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +0000101
Chris Lattner600dee42002-12-28 20:42:14 +0000102 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000103 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
104 RC->getAlignment());
Chris Lattner9f366d72002-12-15 22:19:19 +0000105
106 // Assign the slot...
Chris Lattner600dee42002-12-28 20:42:14 +0000107 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
108
109 return FrameIdx;
Misha Brukmanf514d512002-12-02 21:11:58 +0000110}
111
Misha Brukman07218672002-11-22 22:44:32 +0000112unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000113 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(virtualReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000114 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
115 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Misha Brukman07218672002-11-22 22:44:32 +0000116
Chris Lattner600dee42002-12-28 20:42:14 +0000117 while (1) {
118 unsigned regIdx = RegClassIdx[RC]++;
119 assert(RI+regIdx != RE && "Not enough registers!");
120 unsigned PhysReg = *(RI+regIdx);
121
Chris Lattner78611632005-01-23 22:55:45 +0000122 if (!RegsUsed[PhysReg]) {
123 PhysRegsEverUsed[PhysReg] = true;
Chris Lattner600dee42002-12-28 20:42:14 +0000124 return PhysReg;
Chris Lattner78611632005-01-23 22:55:45 +0000125 }
Chris Lattner600dee42002-12-28 20:42:14 +0000126 }
Misha Brukman07218672002-11-22 22:44:32 +0000127}
128
Chris Lattnerb167c042002-12-15 23:01:26 +0000129unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000130 MachineBasicBlock::iterator I,
Chris Lattnerb167c042002-12-15 23:01:26 +0000131 unsigned VirtReg) {
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);
Chris Lattnerb167c042002-12-15 23:01:26 +0000134 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000135
Misha Brukmanf514d512002-12-02 21:11:58 +0000136 // Add move instruction(s)
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000137 ++NumLoads;
Chris Lattner57f1b672004-08-15 21:56:44 +0000138 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx);
Chris Lattnerb167c042002-12-15 23:01:26 +0000139 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000140}
141
Chris Lattnerb167c042002-12-15 23:01:26 +0000142void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000143 MachineBasicBlock::iterator I,
Chris Lattner600dee42002-12-28 20:42:14 +0000144 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000145 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000146 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Misha Brukmanf514d512002-12-02 21:11:58 +0000147
Misha Brukman07218672002-11-22 22:44:32 +0000148 // Add move instruction(s)
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000149 ++NumStores;
Chris Lattner57f1b672004-08-15 21:56:44 +0000150 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIdx);
Misha Brukman07218672002-11-22 22:44:32 +0000151}
152
Misha Brukmandc2ec002002-12-03 23:15:19 +0000153
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000154void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000155 // loop over each instruction
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000156 for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000157 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000158 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000159
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000160 RegsUsed.resize(RegInfo->getNumRegs());
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000161
Chris Lattner78611632005-01-23 22:55:45 +0000162 // This is a preliminary pass that will invalidate any registers that are
163 // used by the instruction (including implicit uses).
Chris Lattner600dee42002-12-28 20:42:14 +0000164 unsigned Opcode = MI->getOpcode();
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000165 const TargetInstrDescriptor &Desc = TM->getInstrInfo()->get(Opcode);
Chris Lattner78611632005-01-23 22:55:45 +0000166 const unsigned *Regs;
167 for (Regs = Desc.ImplicitUses; *Regs; ++Regs)
168 RegsUsed[*Regs] = true;
Chris Lattner600dee42002-12-28 20:42:14 +0000169
Chris Lattner78611632005-01-23 22:55:45 +0000170 for (Regs = Desc.ImplicitDefs; *Regs; ++Regs) {
171 RegsUsed[*Regs] = true;
172 PhysRegsEverUsed[*Regs] = true;
173 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000174
Chris Lattner78611632005-01-23 22:55:45 +0000175 // Loop over uses, move from memory into registers.
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000176 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
177 MachineOperand &op = MI->getOperand(i);
178
Chris Lattner6ae9eb12004-03-16 01:45:55 +0000179 if (op.isRegister() && op.getReg() &&
180 MRegisterInfo::isVirtualRegister(op.getReg())) {
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000181 unsigned virtualReg = (unsigned) op.getReg();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000182 DEBUG(std::cerr << "op: " << op << "\n");
183 DEBUG(std::cerr << "\t inst[" << i << "]: ";
Tanya Lattnerb1407622004-06-25 00:13:11 +0000184 MI->print(std::cerr, TM));
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000185
186 // make sure the same virtual register maps to the same physical
187 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000188 unsigned physReg = Virt2PhysRegMap[virtualReg];
189 if (physReg == 0) {
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000190 if (op.isDef()) {
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000191 if (!TM->getInstrInfo()->isTwoAddrInstr(MI->getOpcode()) || i) {
Chris Lattner5aaf1d22004-02-15 21:38:28 +0000192 physReg = getFreeReg(virtualReg);
193 } else {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000194 // must be same register number as the first operand
195 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000196 assert(MI->getOperand(1).isRegister() &&
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000197 MI->getOperand(1).getReg() &&
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000198 MI->getOperand(1).isUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000199 "Two address instruction invalid!");
200
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000201 physReg = MI->getOperand(1).getReg();
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000202 spillVirtReg(MBB, next(MI), virtualReg, physReg);
Chris Lattner5aaf1d22004-02-15 21:38:28 +0000203 MI->getOperand(1).setDef();
204 MI->RemoveOperand(0);
205 break; // This is the last operand to process
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000206 }
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000207 spillVirtReg(MBB, next(MI), virtualReg, physReg);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000208 } else {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000209 physReg = reloadVirtReg(MBB, MI, virtualReg);
Chris Lattnerb167c042002-12-15 23:01:26 +0000210 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000211 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000212 }
213 MI->SetMachineOperandReg(i, physReg);
214 DEBUG(std::cerr << "virt: " << virtualReg <<
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000215 ", phys: " << op.getReg() << "\n");
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000216 }
217 }
Chris Lattner600dee42002-12-28 20:42:14 +0000218 RegClassIdx.clear();
219 RegsUsed.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000220 }
221}
222
Chris Lattnere7d361d2002-12-17 04:19:40 +0000223
Chris Lattnerda7e4532002-12-15 20:36:09 +0000224/// runOnMachineFunction - Register allocate the whole function
225///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000226bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000227 DEBUG(std::cerr << "Machine Function " << "\n");
228 MF = &Fn;
Chris Lattner600dee42002-12-28 20:42:14 +0000229 TM = &MF->getTarget();
230 RegInfo = TM->getRegisterInfo();
Misha Brukmandc2ec002002-12-03 23:15:19 +0000231
Chris Lattner78611632005-01-23 22:55:45 +0000232 PhysRegsEverUsed = new bool[RegInfo->getNumRegs()];
233 std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false);
234 Fn.setUsedPhysRegs(PhysRegsEverUsed);
235
Chris Lattner9f366d72002-12-15 22:19:19 +0000236 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000237 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
238 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000239 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000240
Chris Lattner600dee42002-12-28 20:42:14 +0000241 StackSlotForVirtReg.clear();
Chris Lattner9f366d72002-12-15 22:19:19 +0000242 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000243}
244
Chris Lattner5aaf1d22004-02-15 21:38:28 +0000245FunctionPass *llvm::createSimpleRegisterAllocator() {
Chris Lattner600dee42002-12-28 20:42:14 +0000246 return new RegAllocSimple();
Misha Brukman07218672002-11-22 22:44:32 +0000247}