blob: 447e54cf790bd179d5d7677626b4a3a5c7f9ce62 [file] [log] [blame]
Chris Lattner1d62cea2002-12-16 14:37:00 +00001//===-- RegAllocSimple.cpp - A simple generic register allocator ----------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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 Lattnereb24db92002-12-28 21:08:26 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Jim Laskeyeb577ba2006-08-02 12:30:23 +000023#include "llvm/CodeGen/RegAllocRegistry.h"
Chris Lattner3501fea2003-01-14 22:00:31 +000024#include "llvm/Target/TargetInstrInfo.h"
Misha Brukman07218672002-11-22 22:44:32 +000025#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/ADT/Statistic.h"
29#include "llvm/ADT/STLExtras.h"
Dan Gohmanc9235d22008-03-21 23:51:57 +000030#include <map>
Chris Lattner5aaf1d22004-02-15 21:38:28 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattnercd3245a2006-12-19 22:41:21 +000033STATISTIC(NumStores, "Number of stores added");
34STATISTIC(NumLoads , "Number of loads added");
Chris Lattnerda7e4532002-12-15 20:36:09 +000035
Chris Lattnercd3245a2006-12-19 22:41:21 +000036namespace {
Jim Laskey13ec7022006-08-01 14:21:23 +000037 static RegisterRegAlloc
Dan Gohmanb8cab922008-10-14 20:25:08 +000038 simpleRegAlloc("simple", "simple register allocator",
Jim Laskey13ec7022006-08-01 14:21:23 +000039 createSimpleRegisterAllocator);
40
Chris Lattnerf8c68f62006-06-28 22:17:39 +000041 class VISIBILITY_HIDDEN RegAllocSimple : public MachineFunctionPass {
Devang Patel794fd752007-05-01 21:15:47 +000042 public:
Devang Patel19974732007-05-03 01:11:54 +000043 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000044 RegAllocSimple() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000045 private:
Misha Brukman07218672002-11-22 22:44:32 +000046 MachineFunction *MF;
Chris Lattner600dee42002-12-28 20:42:14 +000047 const TargetMachine *TM;
Dan Gohman6f0d0242008-02-10 18:45:23 +000048 const TargetRegisterInfo *TRI;
Dan Gohman88cef242008-07-09 19:56:01 +000049 const TargetInstrInfo *TII;
Misha Brukmanedf128a2005-04-21 22:36:52 +000050
Chris Lattner600dee42002-12-28 20:42:14 +000051 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
52 // these values are spilled
53 std::map<unsigned, int> StackSlotForVirtReg;
Misha Brukman07218672002-11-22 22:44:32 +000054
Chris Lattner600dee42002-12-28 20:42:14 +000055 // RegsUsed - Keep track of what registers are currently in use. This is a
56 // bitset.
57 std::vector<bool> RegsUsed;
Chris Lattnerda7e4532002-12-15 20:36:09 +000058
59 // RegClassIdx - Maps RegClass => which index we can take a register
60 // from. Since this is a simple register allocator, when we need a register
61 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000062 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
63
Chris Lattnerda7e4532002-12-15 20:36:09 +000064 public:
Chris Lattner8233e2f2002-12-15 21:13:12 +000065 virtual const char *getPassName() const {
66 return "Simple Register Allocator";
67 }
68
Chris Lattnerda7e4532002-12-15 20:36:09 +000069 /// runOnMachineFunction - Register allocate the whole function
70 bool runOnMachineFunction(MachineFunction &Fn);
71
Chris Lattner80a04782003-01-13 00:26:08 +000072 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73 AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
74 MachineFunctionPass::getAnalysisUsage(AU);
75 }
Chris Lattner600dee42002-12-28 20:42:14 +000076 private:
Chris Lattnerda7e4532002-12-15 20:36:09 +000077 /// AllocateBasicBlock - Register allocate the specified basic block.
78 void AllocateBasicBlock(MachineBasicBlock &MBB);
79
Chris Lattner9f366d72002-12-15 22:19:19 +000080 /// getStackSpaceFor - This returns the offset of the specified virtual
Misha Brukman5560c9d2003-08-18 14:43:39 +000081 /// register on the stack, allocating space if necessary.
Chris Lattner600dee42002-12-28 20:42:14 +000082 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Misha Brukman07218672002-11-22 22:44:32 +000083
Chris Lattner9f366d72002-12-15 22:19:19 +000084 /// Given a virtual register, return a compatible physical register that is
85 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000086 ///
Misha Brukman07218672002-11-22 22:44:32 +000087 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000088 ///
Misha Brukman07218672002-11-22 22:44:32 +000089 unsigned getFreeReg(unsigned virtualReg);
90
Misha Brukman07218672002-11-22 22:44:32 +000091 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +000092 unsigned reloadVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +000093 MachineBasicBlock::iterator I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +000094
95 /// Saves reg value on the stack (maps virtual register to stack value)
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +000096 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Chris Lattnerb167c042002-12-15 23:01:26 +000097 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +000098 };
Devang Patel19974732007-05-03 01:11:54 +000099 char RegAllocSimple::ID = 0;
Misha Brukman59b3eed2002-12-13 10:42:31 +0000100}
Misha Brukman07218672002-11-22 22:44:32 +0000101
Chris Lattner9f366d72002-12-15 22:19:19 +0000102/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000103/// register to be held on the stack.
Chris Lattner600dee42002-12-28 20:42:14 +0000104int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000105 const TargetRegisterClass *RC) {
Chris Lattner9f366d72002-12-15 22:19:19 +0000106 // Find the location VirtReg would belong...
Dan Gohman0383bc02008-07-09 19:51:00 +0000107 std::map<unsigned, int>::iterator I = StackSlotForVirtReg.find(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +0000108
Dan Gohman0383bc02008-07-09 19:51:00 +0000109 if (I != StackSlotForVirtReg.end())
Chris Lattner9f366d72002-12-15 22:19:19 +0000110 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +0000111
Chris Lattner600dee42002-12-28 20:42:14 +0000112 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000113 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
114 RC->getAlignment());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000115
Chris Lattner9f366d72002-12-15 22:19:19 +0000116 // Assign the slot...
Chris Lattner600dee42002-12-28 20:42:14 +0000117 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
118
119 return FrameIdx;
Misha Brukmanf514d512002-12-02 21:11:58 +0000120}
121
Misha Brukman07218672002-11-22 22:44:32 +0000122unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000123 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtualReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000124 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
Devang Patel27558962008-12-23 21:55:04 +0000125#ifndef NDEBUG
Chris Lattner600dee42002-12-28 20:42:14 +0000126 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Devang Patel27558962008-12-23 21:55:04 +0000127#endif
Misha Brukman07218672002-11-22 22:44:32 +0000128
Chris Lattner600dee42002-12-28 20:42:14 +0000129 while (1) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000130 unsigned regIdx = RegClassIdx[RC]++;
Chris Lattner600dee42002-12-28 20:42:14 +0000131 assert(RI+regIdx != RE && "Not enough registers!");
132 unsigned PhysReg = *(RI+regIdx);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000133
Chris Lattner78611632005-01-23 22:55:45 +0000134 if (!RegsUsed[PhysReg]) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000135 MF->getRegInfo().setPhysRegUsed(PhysReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000136 return PhysReg;
Chris Lattner78611632005-01-23 22:55:45 +0000137 }
Chris Lattner600dee42002-12-28 20:42:14 +0000138 }
Misha Brukman07218672002-11-22 22:44:32 +0000139}
140
Chris Lattnerb167c042002-12-15 23:01:26 +0000141unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000142 MachineBasicBlock::iterator I,
Chris Lattnerb167c042002-12-15 23:01:26 +0000143 unsigned VirtReg) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000144 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000145 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000146 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000147
Misha Brukmanf514d512002-12-02 21:11:58 +0000148 // Add move instruction(s)
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000149 ++NumLoads;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000150 TII->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000151 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000152}
153
Chris Lattnerb167c042002-12-15 23:01:26 +0000154void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000155 MachineBasicBlock::iterator I,
Chris Lattner600dee42002-12-28 20:42:14 +0000156 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000157 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000158
Chris Lattner600dee42002-12-28 20:42:14 +0000159 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Misha Brukmanf514d512002-12-02 21:11:58 +0000160
Misha Brukman07218672002-11-22 22:44:32 +0000161 // Add move instruction(s)
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000162 ++NumStores;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000163 TII->storeRegToStackSlot(MBB, I, PhysReg, true, FrameIdx, RC);
Misha Brukman07218672002-11-22 22:44:32 +0000164}
165
Misha Brukmandc2ec002002-12-03 23:15:19 +0000166
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000167void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000168 // loop over each instruction
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000169 for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000170 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000171 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000172
Dan Gohman6f0d0242008-02-10 18:45:23 +0000173 RegsUsed.resize(TRI->getNumRegs());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000174
Chris Lattner78611632005-01-23 22:55:45 +0000175 // This is a preliminary pass that will invalidate any registers that are
176 // used by the instruction (including implicit uses).
Chris Lattner749c6f62008-01-07 07:27:27 +0000177 const TargetInstrDesc &Desc = MI->getDesc();
Chris Lattner78611632005-01-23 22:55:45 +0000178 const unsigned *Regs;
Jim Laskeycd4317e2006-07-21 21:15:20 +0000179 if (Desc.ImplicitUses) {
180 for (Regs = Desc.ImplicitUses; *Regs; ++Regs)
181 RegsUsed[*Regs] = true;
182 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000183
Jim Laskeycd4317e2006-07-21 21:15:20 +0000184 if (Desc.ImplicitDefs) {
185 for (Regs = Desc.ImplicitDefs; *Regs; ++Regs) {
186 RegsUsed[*Regs] = true;
Chris Lattner84bc5422007-12-31 04:13:23 +0000187 MF->getRegInfo().setPhysRegUsed(*Regs);
Jim Laskeycd4317e2006-07-21 21:15:20 +0000188 }
Chris Lattner78611632005-01-23 22:55:45 +0000189 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000190
Chris Lattner78611632005-01-23 22:55:45 +0000191 // Loop over uses, move from memory into registers.
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000192 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
Dan Gohman85e68152008-07-09 20:12:26 +0000193 MachineOperand &MO = MI->getOperand(i);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000194
Dan Gohmand735b802008-10-03 15:45:36 +0000195 if (MO.isReg() && MO.getReg() &&
Dan Gohman85e68152008-07-09 20:12:26 +0000196 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
197 unsigned virtualReg = (unsigned) MO.getReg();
198 DOUT << "op: " << MO << "\n";
Bill Wendlinga09362e2006-11-28 22:48:48 +0000199 DOUT << "\t inst[" << i << "]: ";
Bill Wendlingbcd24982006-12-07 20:28:15 +0000200 DEBUG(MI->print(*cerr.stream(), TM));
Misha Brukmanedf128a2005-04-21 22:36:52 +0000201
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000202 // make sure the same virtual register maps to the same physical
203 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000204 unsigned physReg = Virt2PhysRegMap[virtualReg];
205 if (physReg == 0) {
Dan Gohman85e68152008-07-09 20:12:26 +0000206 if (MO.isDef()) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000207 unsigned TiedOp;
208 if (!MI->isRegTiedToUseOperand(i, &TiedOp)) {
Chris Lattner5aaf1d22004-02-15 21:38:28 +0000209 physReg = getFreeReg(virtualReg);
210 } else {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000211 // must be same register number as the source operand that is
212 // tied to. This maps a = b + c into b = b + c, and saves b into
213 // a's spot.
Dan Gohmand735b802008-10-03 15:45:36 +0000214 assert(MI->getOperand(TiedOp).isReg() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000215 MI->getOperand(TiedOp).getReg() &&
216 MI->getOperand(TiedOp).isUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000217 "Two address instruction invalid!");
218
Evan Cheng360c2dd2006-11-01 23:06:55 +0000219 physReg = MI->getOperand(TiedOp).getReg();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000220 }
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000221 spillVirtReg(MBB, next(MI), virtualReg, physReg);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000222 } else {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000223 physReg = reloadVirtReg(MBB, MI, virtualReg);
Chris Lattnerb167c042002-12-15 23:01:26 +0000224 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000225 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000226 }
Dan Gohman85e68152008-07-09 20:12:26 +0000227 MO.setReg(physReg);
228 DOUT << "virt: " << virtualReg << ", phys: " << MO.getReg() << "\n";
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000229 }
230 }
Chris Lattner600dee42002-12-28 20:42:14 +0000231 RegClassIdx.clear();
232 RegsUsed.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000233 }
234}
235
Chris Lattnere7d361d2002-12-17 04:19:40 +0000236
Chris Lattnerda7e4532002-12-15 20:36:09 +0000237/// runOnMachineFunction - Register allocate the whole function
238///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000239bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Bill Wendlinga09362e2006-11-28 22:48:48 +0000240 DOUT << "Machine Function\n";
Misha Brukman07218672002-11-22 22:44:32 +0000241 MF = &Fn;
Chris Lattner600dee42002-12-28 20:42:14 +0000242 TM = &MF->getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000243 TRI = TM->getRegisterInfo();
Dan Gohman88cef242008-07-09 19:56:01 +0000244 TII = TM->getInstrInfo();
Misha Brukmandc2ec002002-12-03 23:15:19 +0000245
Chris Lattner9f366d72002-12-15 22:19:19 +0000246 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000247 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
248 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000249 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000250
Chris Lattner600dee42002-12-28 20:42:14 +0000251 StackSlotForVirtReg.clear();
Chris Lattner9f366d72002-12-15 22:19:19 +0000252 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000253}
254
Chris Lattner5aaf1d22004-02-15 21:38:28 +0000255FunctionPass *llvm::createSimpleRegisterAllocator() {
Chris Lattner600dee42002-12-28 20:42:14 +0000256 return new RegAllocSimple();
Misha Brukman07218672002-11-22 22:44:32 +0000257}