blob: 4e6e1d2705e1b2e2793e6f3344cec7609f69865c [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//
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.
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 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 Lattner2c2c6c62006-01-22 23:41:00 +000028#include <iostream>
Chris Lattner5aaf1d22004-02-15 21:38:28 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Misha Brukman59b3eed2002-12-13 10:42:31 +000031namespace {
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +000032 Statistic<> NumStores("ra-simple", "Number of stores added");
33 Statistic<> NumLoads ("ra-simple", "Number of loads added");
Chris Lattnerda7e4532002-12-15 20:36:09 +000034
Chris Lattner600dee42002-12-28 20:42:14 +000035 class RegAllocSimple : public MachineFunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000036 MachineFunction *MF;
Chris Lattner600dee42002-12-28 20:42:14 +000037 const TargetMachine *TM;
Misha Brukman07218672002-11-22 22:44:32 +000038 const MRegisterInfo *RegInfo;
Chris Lattner78611632005-01-23 22:55:45 +000039 bool *PhysRegsEverUsed;
Misha Brukmanedf128a2005-04-21 22:36:52 +000040
Chris Lattner600dee42002-12-28 20:42:14 +000041 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
42 // these values are spilled
43 std::map<unsigned, int> StackSlotForVirtReg;
Misha Brukman07218672002-11-22 22:44:32 +000044
Chris Lattner600dee42002-12-28 20:42:14 +000045 // RegsUsed - Keep track of what registers are currently in use. This is a
46 // bitset.
47 std::vector<bool> RegsUsed;
Chris Lattnerda7e4532002-12-15 20:36:09 +000048
49 // RegClassIdx - Maps RegClass => which index we can take a register
50 // from. Since this is a simple register allocator, when we need a register
51 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000052 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
53
Chris Lattnerda7e4532002-12-15 20:36:09 +000054 public:
Chris Lattner8233e2f2002-12-15 21:13:12 +000055 virtual const char *getPassName() const {
56 return "Simple Register Allocator";
57 }
58
Chris Lattnerda7e4532002-12-15 20:36:09 +000059 /// runOnMachineFunction - Register allocate the whole function
60 bool runOnMachineFunction(MachineFunction &Fn);
61
Chris Lattner80a04782003-01-13 00:26:08 +000062 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
63 AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
64 MachineFunctionPass::getAnalysisUsage(AU);
65 }
Chris Lattner600dee42002-12-28 20:42:14 +000066 private:
Chris Lattnerda7e4532002-12-15 20:36:09 +000067 /// AllocateBasicBlock - Register allocate the specified basic block.
68 void AllocateBasicBlock(MachineBasicBlock &MBB);
69
Chris Lattner9f366d72002-12-15 22:19:19 +000070 /// getStackSpaceFor - This returns the offset of the specified virtual
Misha Brukman5560c9d2003-08-18 14:43:39 +000071 /// register on the stack, allocating space if necessary.
Chris Lattner600dee42002-12-28 20:42:14 +000072 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Misha Brukman07218672002-11-22 22:44:32 +000073
Chris Lattner9f366d72002-12-15 22:19:19 +000074 /// Given a virtual register, return a compatible physical register that is
75 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000076 ///
Misha Brukman07218672002-11-22 22:44:32 +000077 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000078 ///
Misha Brukman07218672002-11-22 22:44:32 +000079 unsigned getFreeReg(unsigned virtualReg);
80
Misha Brukman07218672002-11-22 22:44:32 +000081 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +000082 unsigned reloadVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +000083 MachineBasicBlock::iterator I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +000084
85 /// Saves reg value on the stack (maps virtual register to stack value)
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +000086 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
Chris Lattnerb167c042002-12-15 23:01:26 +000087 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +000088 };
89
Misha Brukman59b3eed2002-12-13 10:42:31 +000090}
Misha Brukman07218672002-11-22 22:44:32 +000091
Chris Lattner9f366d72002-12-15 22:19:19 +000092/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +000093/// register to be held on the stack.
Chris Lattner600dee42002-12-28 20:42:14 +000094int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
Misha Brukmandedf2bd2005-04-22 04:01:18 +000095 const TargetRegisterClass *RC) {
Chris Lattner9f366d72002-12-15 22:19:19 +000096 // Find the location VirtReg would belong...
Chris Lattner600dee42002-12-28 20:42:14 +000097 std::map<unsigned, int>::iterator I =
98 StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +000099
Chris Lattner600dee42002-12-28 20:42:14 +0000100 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattner9f366d72002-12-15 22:19:19 +0000101 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +0000102
Chris Lattner600dee42002-12-28 20:42:14 +0000103 // Allocate a new stack object for this spill location...
Chris Lattner26eb14b2004-08-15 22:02:22 +0000104 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(),
105 RC->getAlignment());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000106
Chris Lattner9f366d72002-12-15 22:19:19 +0000107 // Assign the slot...
Chris Lattner600dee42002-12-28 20:42:14 +0000108 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
109
110 return FrameIdx;
Misha Brukmanf514d512002-12-02 21:11:58 +0000111}
112
Misha Brukman07218672002-11-22 22:44:32 +0000113unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000114 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(virtualReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000115 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
116 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Misha Brukman07218672002-11-22 22:44:32 +0000117
Chris Lattner600dee42002-12-28 20:42:14 +0000118 while (1) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000119 unsigned regIdx = RegClassIdx[RC]++;
Chris Lattner600dee42002-12-28 20:42:14 +0000120 assert(RI+regIdx != RE && "Not enough registers!");
121 unsigned PhysReg = *(RI+regIdx);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000122
Chris Lattner78611632005-01-23 22:55:45 +0000123 if (!RegsUsed[PhysReg]) {
124 PhysRegsEverUsed[PhysReg] = true;
Chris Lattner600dee42002-12-28 20:42:14 +0000125 return PhysReg;
Chris Lattner78611632005-01-23 22:55:45 +0000126 }
Chris Lattner600dee42002-12-28 20:42:14 +0000127 }
Misha Brukman07218672002-11-22 22:44:32 +0000128}
129
Chris Lattnerb167c042002-12-15 23:01:26 +0000130unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000131 MachineBasicBlock::iterator I,
Chris Lattnerb167c042002-12-15 23:01:26 +0000132 unsigned VirtReg) {
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);
Chris Lattnerb167c042002-12-15 23:01:26 +0000135 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000136
Misha Brukmanf514d512002-12-02 21:11:58 +0000137 // Add move instruction(s)
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000138 ++NumLoads;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000139 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000140 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000141}
142
Chris Lattnerb167c042002-12-15 23:01:26 +0000143void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000144 MachineBasicBlock::iterator I,
Chris Lattner600dee42002-12-28 20:42:14 +0000145 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000146 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000147 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Misha Brukmanf514d512002-12-02 21:11:58 +0000148
Misha Brukman07218672002-11-22 22:44:32 +0000149 // Add move instruction(s)
Alkis Evlogimenos2acef2d2004-02-19 06:19:09 +0000150 ++NumStores;
Chris Lattnerbf9716b2005-09-30 01:29:00 +0000151 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Misha Brukman07218672002-11-22 22:44:32 +0000152}
153
Misha Brukmandc2ec002002-12-03 23:15:19 +0000154
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000155void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000156 // loop over each instruction
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000157 for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000158 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000159 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000160
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000161 RegsUsed.resize(RegInfo->getNumRegs());
Misha Brukmanedf128a2005-04-21 22:36:52 +0000162
Chris Lattner78611632005-01-23 22:55:45 +0000163 // This is a preliminary pass that will invalidate any registers that are
164 // used by the instruction (including implicit uses).
Chris Lattner600dee42002-12-28 20:42:14 +0000165 unsigned Opcode = MI->getOpcode();
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000166 const TargetInstrDescriptor &Desc = TM->getInstrInfo()->get(Opcode);
Chris Lattner78611632005-01-23 22:55:45 +0000167 const unsigned *Regs;
168 for (Regs = Desc.ImplicitUses; *Regs; ++Regs)
169 RegsUsed[*Regs] = true;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000170
Chris Lattner78611632005-01-23 22:55:45 +0000171 for (Regs = Desc.ImplicitDefs; *Regs; ++Regs) {
172 RegsUsed[*Regs] = true;
173 PhysRegsEverUsed[*Regs] = true;
174 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000175
Chris Lattner78611632005-01-23 22:55:45 +0000176 // Loop over uses, move from memory into registers.
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000177 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
178 MachineOperand &op = MI->getOperand(i);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000179
Chris Lattner6ae9eb12004-03-16 01:45:55 +0000180 if (op.isRegister() && op.getReg() &&
181 MRegisterInfo::isVirtualRegister(op.getReg())) {
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000182 unsigned virtualReg = (unsigned) op.getReg();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000183 DEBUG(std::cerr << "op: " << op << "\n");
184 DEBUG(std::cerr << "\t inst[" << i << "]: ";
Tanya Lattnerb1407622004-06-25 00:13:11 +0000185 MI->print(std::cerr, TM));
Misha Brukmanedf128a2005-04-21 22:36:52 +0000186
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000187 // make sure the same virtual register maps to the same physical
188 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000189 unsigned physReg = Virt2PhysRegMap[virtualReg];
190 if (physReg == 0) {
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000191 if (op.isDef()) {
Chris Lattner9bcdcd12004-06-02 05:57:12 +0000192 if (!TM->getInstrInfo()->isTwoAddrInstr(MI->getOpcode()) || i) {
Chris Lattner5aaf1d22004-02-15 21:38:28 +0000193 physReg = getFreeReg(virtualReg);
194 } else {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000195 // must be same register number as the first operand
196 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000197 assert(MI->getOperand(1).isRegister() &&
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000198 MI->getOperand(1).getReg() &&
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000199 MI->getOperand(1).isUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000200 "Two address instruction invalid!");
201
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000202 physReg = MI->getOperand(1).getReg();
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000203 spillVirtReg(MBB, next(MI), virtualReg, physReg);
Chris Lattner5aaf1d22004-02-15 21:38:28 +0000204 MI->getOperand(1).setDef();
205 MI->RemoveOperand(0);
206 break; // This is the last operand to process
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000207 }
Alkis Evlogimenosfc2b4492004-02-23 04:12:30 +0000208 spillVirtReg(MBB, next(MI), virtualReg, physReg);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000209 } else {
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000210 physReg = reloadVirtReg(MBB, MI, virtualReg);
Chris Lattnerb167c042002-12-15 23:01:26 +0000211 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000212 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000213 }
Chris Lattnere53f4a02006-05-04 17:52:23 +0000214 MI->getOperand(i).setReg(physReg);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000215 DEBUG(std::cerr << "virt: " << virtualReg <<
Alkis Evlogimenosbe766c72004-02-13 21:01:20 +0000216 ", phys: " << op.getReg() << "\n");
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000217 }
218 }
Chris Lattner600dee42002-12-28 20:42:14 +0000219 RegClassIdx.clear();
220 RegsUsed.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000221 }
222}
223
Chris Lattnere7d361d2002-12-17 04:19:40 +0000224
Chris Lattnerda7e4532002-12-15 20:36:09 +0000225/// runOnMachineFunction - Register allocate the whole function
226///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000227bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000228 DEBUG(std::cerr << "Machine Function " << "\n");
229 MF = &Fn;
Chris Lattner600dee42002-12-28 20:42:14 +0000230 TM = &MF->getTarget();
231 RegInfo = TM->getRegisterInfo();
Misha Brukmandc2ec002002-12-03 23:15:19 +0000232
Chris Lattner78611632005-01-23 22:55:45 +0000233 PhysRegsEverUsed = new bool[RegInfo->getNumRegs()];
234 std::fill(PhysRegsEverUsed, PhysRegsEverUsed+RegInfo->getNumRegs(), false);
235 Fn.setUsedPhysRegs(PhysRegsEverUsed);
236
Chris Lattner9f366d72002-12-15 22:19:19 +0000237 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000238 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
239 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000240 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000241
Chris Lattner600dee42002-12-28 20:42:14 +0000242 StackSlotForVirtReg.clear();
Chris Lattner9f366d72002-12-15 22:19:19 +0000243 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000244}
245
Chris Lattner5aaf1d22004-02-15 21:38:28 +0000246FunctionPass *llvm::createSimpleRegisterAllocator() {
Chris Lattner600dee42002-12-28 20:42:14 +0000247 return new RegAllocSimple();
Misha Brukman07218672002-11-22 22:44:32 +0000248}