blob: 5abeb7c1b879934090947b09656f04cf38ad5ac4 [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"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Bill Wendlingcbac7882009-08-22 20:40:21 +000027#include "llvm/Support/Debug.h"
28#include "llvm/Support/raw_ostream.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000029#include "llvm/ADT/Statistic.h"
30#include "llvm/ADT/STLExtras.h"
Dan Gohmanc9235d22008-03-21 23:51:57 +000031#include <map>
Chris Lattner5aaf1d22004-02-15 21:38:28 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattnercd3245a2006-12-19 22:41:21 +000034STATISTIC(NumStores, "Number of stores added");
35STATISTIC(NumLoads , "Number of loads added");
Chris Lattnerda7e4532002-12-15 20:36:09 +000036
Chris Lattnercd3245a2006-12-19 22:41:21 +000037namespace {
Jim Laskey13ec7022006-08-01 14:21:23 +000038 static RegisterRegAlloc
Dan Gohmanb8cab922008-10-14 20:25:08 +000039 simpleRegAlloc("simple", "simple register allocator",
Jim Laskey13ec7022006-08-01 14:21:23 +000040 createSimpleRegisterAllocator);
41
Chris Lattnerf8c68f62006-06-28 22:17:39 +000042 class VISIBILITY_HIDDEN RegAllocSimple : public MachineFunctionPass {
Devang Patel794fd752007-05-01 21:15:47 +000043 public:
Devang Patel19974732007-05-03 01:11:54 +000044 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000045 RegAllocSimple() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000046 private:
Misha Brukman07218672002-11-22 22:44:32 +000047 MachineFunction *MF;
Chris Lattner600dee42002-12-28 20:42:14 +000048 const TargetMachine *TM;
Dan Gohman6f0d0242008-02-10 18:45:23 +000049 const TargetRegisterInfo *TRI;
Dan Gohman88cef242008-07-09 19:56:01 +000050 const TargetInstrInfo *TII;
Misha Brukmanedf128a2005-04-21 22:36:52 +000051
Chris Lattner600dee42002-12-28 20:42:14 +000052 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
53 // these values are spilled
54 std::map<unsigned, int> StackSlotForVirtReg;
Misha Brukman07218672002-11-22 22:44:32 +000055
Chris Lattner600dee42002-12-28 20:42:14 +000056 // RegsUsed - Keep track of what registers are currently in use. This is a
57 // bitset.
58 std::vector<bool> RegsUsed;
Chris Lattnerda7e4532002-12-15 20:36:09 +000059
60 // RegClassIdx - Maps RegClass => which index we can take a register
61 // from. Since this is a simple register allocator, when we need a register
62 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000063 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
64
Chris Lattnerda7e4532002-12-15 20:36:09 +000065 public:
Chris Lattner8233e2f2002-12-15 21:13:12 +000066 virtual const char *getPassName() const {
67 return "Simple Register Allocator";
68 }
69
Chris Lattnerda7e4532002-12-15 20:36:09 +000070 /// runOnMachineFunction - Register allocate the whole function
71 bool runOnMachineFunction(MachineFunction &Fn);
72
Chris Lattner80a04782003-01-13 00:26:08 +000073 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000074 AU.setPreservesCFG();
Chris Lattner80a04782003-01-13 00:26:08 +000075 AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
76 MachineFunctionPass::getAnalysisUsage(AU);
77 }
Chris Lattner600dee42002-12-28 20:42:14 +000078 private:
Chris Lattnerda7e4532002-12-15 20:36:09 +000079 /// AllocateBasicBlock - Register allocate the specified basic block.
80 void AllocateBasicBlock(MachineBasicBlock &MBB);
81
Chris Lattner9f366d72002-12-15 22:19:19 +000082 /// getStackSpaceFor - This returns the offset of the specified virtual
Misha Brukman5560c9d2003-08-18 14:43:39 +000083 /// register on the stack, allocating space if necessary.
Chris Lattner600dee42002-12-28 20:42:14 +000084 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Misha Brukman07218672002-11-22 22:44:32 +000085
Chris Lattner9f366d72002-12-15 22:19:19 +000086 /// Given a virtual register, return a compatible physical register that is
87 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000088 ///
Misha Brukman07218672002-11-22 22:44:32 +000089 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000090 ///
Misha Brukman07218672002-11-22 22:44:32 +000091 unsigned getFreeReg(unsigned virtualReg);
92
Misha Brukman07218672002-11-22 22:44:32 +000093 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +000094 unsigned reloadVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +000095 MachineBasicBlock::iterator I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +000096
97 /// Saves reg value on the stack (maps virtual register to stack value)
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +000098 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Chris Lattnerb167c042002-12-15 23:01:26 +000099 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +0000100 };
Devang Patel19974732007-05-03 01:11:54 +0000101 char RegAllocSimple::ID = 0;
Misha Brukman59b3eed2002-12-13 10:42:31 +0000102}
Misha Brukman07218672002-11-22 22:44:32 +0000103
Chris Lattner9f366d72002-12-15 22:19:19 +0000104/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000105/// register to be held on the stack.
Chris Lattner600dee42002-12-28 20:42:14 +0000106int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000107 const TargetRegisterClass *RC) {
Chris Lattner9f366d72002-12-15 22:19:19 +0000108 // Find the location VirtReg would belong...
Dan Gohman0383bc02008-07-09 19:51:00 +0000109 std::map<unsigned, int>::iterator I = StackSlotForVirtReg.find(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +0000110
Dan Gohman0383bc02008-07-09 19:51:00 +0000111 if (I != StackSlotForVirtReg.end())
Chris Lattner9f366d72002-12-15 22:19:19 +0000112 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +0000113
Chris Lattner600dee42002-12-28 20:42:14 +0000114 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000115 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
116 RC->getAlignment());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000117
Chris Lattner9f366d72002-12-15 22:19:19 +0000118 // Assign the slot...
Chris Lattner600dee42002-12-28 20:42:14 +0000119 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
120
121 return FrameIdx;
Misha Brukmanf514d512002-12-02 21:11:58 +0000122}
123
Misha Brukman07218672002-11-22 22:44:32 +0000124unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000125 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtualReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000126 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
Devang Patel27558962008-12-23 21:55:04 +0000127#ifndef NDEBUG
Chris Lattner600dee42002-12-28 20:42:14 +0000128 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Devang Patel27558962008-12-23 21:55:04 +0000129#endif
Misha Brukman07218672002-11-22 22:44:32 +0000130
Chris Lattner600dee42002-12-28 20:42:14 +0000131 while (1) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000132 unsigned regIdx = RegClassIdx[RC]++;
Chris Lattner600dee42002-12-28 20:42:14 +0000133 assert(RI+regIdx != RE && "Not enough registers!");
134 unsigned PhysReg = *(RI+regIdx);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000135
Chris Lattner78611632005-01-23 22:55:45 +0000136 if (!RegsUsed[PhysReg]) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000137 MF->getRegInfo().setPhysRegUsed(PhysReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000138 return PhysReg;
Chris Lattner78611632005-01-23 22:55:45 +0000139 }
Chris Lattner600dee42002-12-28 20:42:14 +0000140 }
Misha Brukman07218672002-11-22 22:44:32 +0000141}
142
Chris Lattnerb167c042002-12-15 23:01:26 +0000143unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000144 MachineBasicBlock::iterator I,
Chris Lattnerb167c042002-12-15 23:01:26 +0000145 unsigned VirtReg) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000146 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000147 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000148 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000149
Misha Brukmanf514d512002-12-02 21:11:58 +0000150 // Add move instruction(s)
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000151 ++NumLoads;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000152 TII->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000153 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000154}
155
Chris Lattnerb167c042002-12-15 23:01:26 +0000156void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000157 MachineBasicBlock::iterator I,
Chris Lattner600dee42002-12-28 20:42:14 +0000158 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000159 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +0000160
Chris Lattner600dee42002-12-28 20:42:14 +0000161 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Misha Brukmanf514d512002-12-02 21:11:58 +0000162
Misha Brukman07218672002-11-22 22:44:32 +0000163 // Add move instruction(s)
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000164 ++NumStores;
Owen Andersonf6372aa2008-01-01 21:11:32 +0000165 TII->storeRegToStackSlot(MBB, I, PhysReg, true, FrameIdx, RC);
Misha Brukman07218672002-11-22 22:44:32 +0000166}
167
Misha Brukmandc2ec002002-12-03 23:15:19 +0000168
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000169void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000170 // loop over each instruction
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000171 for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000172 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000173 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000174
Dan Gohman6f0d0242008-02-10 18:45:23 +0000175 RegsUsed.resize(TRI->getNumRegs());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000176
Chris Lattner78611632005-01-23 22:55:45 +0000177 // This is a preliminary pass that will invalidate any registers that are
178 // used by the instruction (including implicit uses).
Chris Lattner749c6f62008-01-07 07:27:27 +0000179 const TargetInstrDesc &Desc = MI->getDesc();
Chris Lattner78611632005-01-23 22:55:45 +0000180 const unsigned *Regs;
Jim Laskeycd4317e2006-07-21 21:15:20 +0000181 if (Desc.ImplicitUses) {
182 for (Regs = Desc.ImplicitUses; *Regs; ++Regs)
183 RegsUsed[*Regs] = true;
184 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000185
Jim Laskeycd4317e2006-07-21 21:15:20 +0000186 if (Desc.ImplicitDefs) {
187 for (Regs = Desc.ImplicitDefs; *Regs; ++Regs) {
188 RegsUsed[*Regs] = true;
Chris Lattner84bc5422007-12-31 04:13:23 +0000189 MF->getRegInfo().setPhysRegUsed(*Regs);
Jim Laskeycd4317e2006-07-21 21:15:20 +0000190 }
Chris Lattner78611632005-01-23 22:55:45 +0000191 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000192
Chris Lattner78611632005-01-23 22:55:45 +0000193 // Loop over uses, move from memory into registers.
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000194 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
Dan Gohman85e68152008-07-09 20:12:26 +0000195 MachineOperand &MO = MI->getOperand(i);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000196
Dan Gohmand735b802008-10-03 15:45:36 +0000197 if (MO.isReg() && MO.getReg() &&
Dan Gohman85e68152008-07-09 20:12:26 +0000198 TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
199 unsigned virtualReg = (unsigned) MO.getReg();
Bill Wendlingcbac7882009-08-22 20:40:21 +0000200 DEBUG({
201 errs() << "op: " << MO << "\n" << "\t inst[" << i << "]: ";
202 MI->print(errs(), TM);
203 });
Misha Brukmanedf128a2005-04-21 22:36:52 +0000204
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000205 // make sure the same virtual register maps to the same physical
206 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000207 unsigned physReg = Virt2PhysRegMap[virtualReg];
208 if (physReg == 0) {
Dan Gohman85e68152008-07-09 20:12:26 +0000209 if (MO.isDef()) {
Bob Wilsond9df5012009-04-09 17:16:43 +0000210 unsigned TiedOp;
211 if (!MI->isRegTiedToUseOperand(i, &TiedOp)) {
Chris Lattner5aaf1d22004-02-15 21:38:28 +0000212 physReg = getFreeReg(virtualReg);
213 } else {
Evan Cheng360c2dd2006-11-01 23:06:55 +0000214 // must be same register number as the source operand that is
215 // tied to. This maps a = b + c into b = b + c, and saves b into
216 // a's spot.
Dan Gohmand735b802008-10-03 15:45:36 +0000217 assert(MI->getOperand(TiedOp).isReg() &&
Evan Cheng360c2dd2006-11-01 23:06:55 +0000218 MI->getOperand(TiedOp).getReg() &&
219 MI->getOperand(TiedOp).isUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000220 "Two address instruction invalid!");
221
Evan Cheng360c2dd2006-11-01 23:06:55 +0000222 physReg = MI->getOperand(TiedOp).getReg();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000223 }
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000224 spillVirtReg(MBB, next(MI), virtualReg, physReg);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000225 } else {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000226 physReg = reloadVirtReg(MBB, MI, virtualReg);
Chris Lattnerb167c042002-12-15 23:01:26 +0000227 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000228 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000229 }
Dan Gohman85e68152008-07-09 20:12:26 +0000230 MO.setReg(physReg);
Bill Wendlingcbac7882009-08-22 20:40:21 +0000231 DEBUG(errs() << "virt: " << virtualReg
232 << ", phys: " << MO.getReg() << "\n");
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000233 }
234 }
Chris Lattner600dee42002-12-28 20:42:14 +0000235 RegClassIdx.clear();
236 RegsUsed.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000237 }
238}
239
Chris Lattnere7d361d2002-12-17 04:19:40 +0000240
Chris Lattnerda7e4532002-12-15 20:36:09 +0000241/// runOnMachineFunction - Register allocate the whole function
242///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000243bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Bill Wendlingcbac7882009-08-22 20:40:21 +0000244 DEBUG(errs() << "Machine Function\n");
Misha Brukman07218672002-11-22 22:44:32 +0000245 MF = &Fn;
Chris Lattner600dee42002-12-28 20:42:14 +0000246 TM = &MF->getTarget();
Dan Gohman6f0d0242008-02-10 18:45:23 +0000247 TRI = TM->getRegisterInfo();
Dan Gohman88cef242008-07-09 19:56:01 +0000248 TII = TM->getInstrInfo();
Misha Brukmandc2ec002002-12-03 23:15:19 +0000249
Chris Lattner9f366d72002-12-15 22:19:19 +0000250 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000251 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
252 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000253 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000254
Chris Lattner600dee42002-12-28 20:42:14 +0000255 StackSlotForVirtReg.clear();
Chris Lattner9f366d72002-12-15 22:19:19 +0000256 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000257}
258
Chris Lattner5aaf1d22004-02-15 21:38:28 +0000259FunctionPass *llvm::createSimpleRegisterAllocator() {
Chris Lattner600dee42002-12-28 20:42:14 +0000260 return new RegAllocSimple();
Misha Brukman07218672002-11-22 22:44:32 +0000261}