blob: a210790b8dd77f0313d02be7fae97e4c9ac01c23 [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"
Chris Lattnera11136b2003-08-01 22:21:34 +000025#include "Support/Debug.h"
Misha Brukman07218672002-11-22 22:44:32 +000026#include "Support/Statistic.h"
Chris Lattnerabe8dd52002-12-15 18:19:24 +000027#include <iostream>
Misha Brukman07218672002-11-22 22:44:32 +000028
Misha Brukman59b3eed2002-12-13 10:42:31 +000029namespace {
Chris Lattnerda7e4532002-12-15 20:36:09 +000030 Statistic<> NumSpilled ("ra-simple", "Number of registers spilled");
31 Statistic<> NumReloaded("ra-simple", "Number of registers reloaded");
32
Chris Lattner600dee42002-12-28 20:42:14 +000033 class RegAllocSimple : public MachineFunctionPass {
Misha Brukman07218672002-11-22 22:44:32 +000034 MachineFunction *MF;
Chris Lattner600dee42002-12-28 20:42:14 +000035 const TargetMachine *TM;
Misha Brukman07218672002-11-22 22:44:32 +000036 const MRegisterInfo *RegInfo;
Misha Brukman07218672002-11-22 22:44:32 +000037
Chris Lattner600dee42002-12-28 20:42:14 +000038 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where
39 // these values are spilled
40 std::map<unsigned, int> StackSlotForVirtReg;
Misha Brukman07218672002-11-22 22:44:32 +000041
Chris Lattner600dee42002-12-28 20:42:14 +000042 // RegsUsed - Keep track of what registers are currently in use. This is a
43 // bitset.
44 std::vector<bool> RegsUsed;
Chris Lattnerda7e4532002-12-15 20:36:09 +000045
46 // RegClassIdx - Maps RegClass => which index we can take a register
47 // from. Since this is a simple register allocator, when we need a register
48 // of a certain class, we just take the next available one.
Misha Brukman07218672002-11-22 22:44:32 +000049 std::map<const TargetRegisterClass*, unsigned> RegClassIdx;
50
Chris Lattnerda7e4532002-12-15 20:36:09 +000051 public:
Chris Lattner8233e2f2002-12-15 21:13:12 +000052 virtual const char *getPassName() const {
53 return "Simple Register Allocator";
54 }
55
Chris Lattnerda7e4532002-12-15 20:36:09 +000056 /// runOnMachineFunction - Register allocate the whole function
57 bool runOnMachineFunction(MachineFunction &Fn);
58
Chris Lattner80a04782003-01-13 00:26:08 +000059 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes
61 MachineFunctionPass::getAnalysisUsage(AU);
62 }
Chris Lattner600dee42002-12-28 20:42:14 +000063 private:
Chris Lattnerda7e4532002-12-15 20:36:09 +000064 /// AllocateBasicBlock - Register allocate the specified basic block.
65 void AllocateBasicBlock(MachineBasicBlock &MBB);
66
Chris Lattner9f366d72002-12-15 22:19:19 +000067 /// getStackSpaceFor - This returns the offset of the specified virtual
Misha Brukman5560c9d2003-08-18 14:43:39 +000068 /// register on the stack, allocating space if necessary.
Chris Lattner600dee42002-12-28 20:42:14 +000069 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC);
Misha Brukman07218672002-11-22 22:44:32 +000070
Chris Lattner9f366d72002-12-15 22:19:19 +000071 /// Given a virtual register, return a compatible physical register that is
72 /// currently unused.
Chris Lattnerda7e4532002-12-15 20:36:09 +000073 ///
Misha Brukman07218672002-11-22 22:44:32 +000074 /// Side effect: marks that register as being used until manually cleared
Chris Lattnerda7e4532002-12-15 20:36:09 +000075 ///
Misha Brukman07218672002-11-22 22:44:32 +000076 unsigned getFreeReg(unsigned virtualReg);
77
Misha Brukman07218672002-11-22 22:44:32 +000078 /// Moves value from memory into that register
Chris Lattnerb167c042002-12-15 23:01:26 +000079 unsigned reloadVirtReg(MachineBasicBlock &MBB,
80 MachineBasicBlock::iterator &I, unsigned VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +000081
82 /// Saves reg value on the stack (maps virtual register to stack value)
Chris Lattnerb167c042002-12-15 23:01:26 +000083 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator &I,
84 unsigned VirtReg, unsigned PhysReg);
Misha Brukman07218672002-11-22 22:44:32 +000085 };
86
Misha Brukman59b3eed2002-12-13 10:42:31 +000087}
Misha Brukman07218672002-11-22 22:44:32 +000088
Chris Lattner9f366d72002-12-15 22:19:19 +000089/// getStackSpaceFor - This allocates space for the specified virtual
Chris Lattnerc2db1a92002-12-15 19:51:14 +000090/// register to be held on the stack.
Chris Lattner600dee42002-12-28 20:42:14 +000091int RegAllocSimple::getStackSpaceFor(unsigned VirtReg,
92 const TargetRegisterClass *RC) {
Chris Lattner9f366d72002-12-15 22:19:19 +000093 // Find the location VirtReg would belong...
Chris Lattner600dee42002-12-28 20:42:14 +000094 std::map<unsigned, int>::iterator I =
95 StackSlotForVirtReg.lower_bound(VirtReg);
Chris Lattner9593fb12002-12-15 19:07:34 +000096
Chris Lattner600dee42002-12-28 20:42:14 +000097 if (I != StackSlotForVirtReg.end() && I->first == VirtReg)
Chris Lattner9f366d72002-12-15 22:19:19 +000098 return I->second; // Already has space allocated?
Chris Lattner9593fb12002-12-15 19:07:34 +000099
Chris Lattner600dee42002-12-28 20:42:14 +0000100 // Allocate a new stack object for this spill location...
Chris Lattner80a04782003-01-13 00:26:08 +0000101 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC);
Chris Lattner9f366d72002-12-15 22:19:19 +0000102
103 // Assign the slot...
Chris Lattner600dee42002-12-28 20:42:14 +0000104 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx));
105
106 return FrameIdx;
Misha Brukmanf514d512002-12-02 21:11:58 +0000107}
108
Misha Brukman07218672002-11-22 22:44:32 +0000109unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000110 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(virtualReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000111 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF);
112 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF);
Misha Brukman07218672002-11-22 22:44:32 +0000113
Chris Lattner600dee42002-12-28 20:42:14 +0000114 while (1) {
115 unsigned regIdx = RegClassIdx[RC]++;
116 assert(RI+regIdx != RE && "Not enough registers!");
117 unsigned PhysReg = *(RI+regIdx);
118
119 if (!RegsUsed[PhysReg])
120 return PhysReg;
121 }
Misha Brukman07218672002-11-22 22:44:32 +0000122}
123
Chris Lattnerb167c042002-12-15 23:01:26 +0000124unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB,
125 MachineBasicBlock::iterator &I,
126 unsigned VirtReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000127 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000128 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000129 unsigned PhysReg = getFreeReg(VirtReg);
Misha Brukman07218672002-11-22 22:44:32 +0000130
Misha Brukmanf514d512002-12-02 21:11:58 +0000131 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000132 ++NumReloaded;
Chris Lattner600dee42002-12-28 20:42:14 +0000133 RegInfo->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Chris Lattnerb167c042002-12-15 23:01:26 +0000134 return PhysReg;
Misha Brukman07218672002-11-22 22:44:32 +0000135}
136
Chris Lattnerb167c042002-12-15 23:01:26 +0000137void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB,
138 MachineBasicBlock::iterator &I,
Chris Lattner600dee42002-12-28 20:42:14 +0000139 unsigned VirtReg, unsigned PhysReg) {
Chris Lattner5124aec2002-12-25 05:04:20 +0000140 const TargetRegisterClass* RC = MF->getSSARegMap()->getRegClass(VirtReg);
Chris Lattner600dee42002-12-28 20:42:14 +0000141 int FrameIdx = getStackSpaceFor(VirtReg, RC);
Misha Brukmanf514d512002-12-02 21:11:58 +0000142
Misha Brukman07218672002-11-22 22:44:32 +0000143 // Add move instruction(s)
Chris Lattnerda7e4532002-12-15 20:36:09 +0000144 ++NumSpilled;
Chris Lattner600dee42002-12-28 20:42:14 +0000145 RegInfo->storeRegToStackSlot(MBB, I, PhysReg, FrameIdx, RC);
Misha Brukman07218672002-11-22 22:44:32 +0000146}
147
Misha Brukmandc2ec002002-12-03 23:15:19 +0000148
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000149void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerf6050552002-12-15 21:33:51 +0000150 // loop over each instruction
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000151 for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
Chris Lattner01b08c52002-12-15 21:24:30 +0000152 // Made to combat the incorrect allocation of r2 = add r1, r1
Chris Lattner9f366d72002-12-15 22:19:19 +0000153 std::map<unsigned, unsigned> Virt2PhysRegMap;
Chris Lattner01b08c52002-12-15 21:24:30 +0000154
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000155 MachineInstr *MI = *I;
Chris Lattner600dee42002-12-28 20:42:14 +0000156
157 RegsUsed.resize(MRegisterInfo::FirstVirtualRegister);
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000158
159 // a preliminary pass that will invalidate any registers that
160 // are used by the instruction (including implicit uses)
Chris Lattner600dee42002-12-28 20:42:14 +0000161 unsigned Opcode = MI->getOpcode();
Chris Lattner3501fea2003-01-14 22:00:31 +0000162 const TargetInstrDescriptor &Desc = TM->getInstrInfo().get(Opcode);
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000163 const unsigned *Regs = Desc.ImplicitUses;
164 while (*Regs)
165 RegsUsed[*Regs++] = true;
Chris Lattner600dee42002-12-28 20:42:14 +0000166
Alkis Evlogimenos73ff5122003-10-08 05:20:08 +0000167 Regs = Desc.ImplicitDefs;
168 while (*Regs)
169 RegsUsed[*Regs++] = true;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000170
171 // Loop over uses, move from memory into registers
172 for (int i = MI->getNumOperands() - 1; i >= 0; --i) {
173 MachineOperand &op = MI->getOperand(i);
174
Chris Lattnerda7e4532002-12-15 20:36:09 +0000175 if (op.isVirtualRegister()) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000176 unsigned virtualReg = (unsigned) op.getAllocatedRegNum();
177 DEBUG(std::cerr << "op: " << op << "\n");
178 DEBUG(std::cerr << "\t inst[" << i << "]: ";
Chris Lattner600dee42002-12-28 20:42:14 +0000179 MI->print(std::cerr, *TM));
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000180
181 // make sure the same virtual register maps to the same physical
182 // register in any given instruction
Chris Lattner9f366d72002-12-15 22:19:19 +0000183 unsigned physReg = Virt2PhysRegMap[virtualReg];
184 if (physReg == 0) {
Vikram S. Adve5f2180c2003-05-27 00:05:23 +0000185 if (op.opIsDefOnly() || op.opIsDefAndUse()) {
Chris Lattner600dee42002-12-28 20:42:14 +0000186 if (TM->getInstrInfo().isTwoAddrInstr(MI->getOpcode()) && i == 0) {
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000187 // must be same register number as the first operand
188 // This maps a = b + c into b += c, and saves b into a's spot
Chris Lattner15f96db2002-12-15 21:02:20 +0000189 assert(MI->getOperand(1).isRegister() &&
190 MI->getOperand(1).getAllocatedRegNum() &&
Chris Lattner9f366d72002-12-15 22:19:19 +0000191 MI->getOperand(1).opIsUse() &&
Chris Lattner15f96db2002-12-15 21:02:20 +0000192 "Two address instruction invalid!");
193
Chris Lattnerda7e4532002-12-15 20:36:09 +0000194 physReg = MI->getOperand(1).getAllocatedRegNum();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000195 } else {
196 physReg = getFreeReg(virtualReg);
197 }
Chris Lattnerb167c042002-12-15 23:01:26 +0000198 ++I;
199 spillVirtReg(MBB, I, virtualReg, physReg);
200 --I;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000201 } else {
Chris Lattnerb167c042002-12-15 23:01:26 +0000202 physReg = reloadVirtReg(MBB, I, virtualReg);
203 Virt2PhysRegMap[virtualReg] = physReg;
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000204 }
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000205 }
206 MI->SetMachineOperandReg(i, physReg);
207 DEBUG(std::cerr << "virt: " << virtualReg <<
208 ", phys: " << op.getAllocatedRegNum() << "\n");
209 }
210 }
Chris Lattner600dee42002-12-28 20:42:14 +0000211 RegClassIdx.clear();
212 RegsUsed.clear();
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000213 }
214}
215
Chris Lattnere7d361d2002-12-17 04:19:40 +0000216
Chris Lattnerda7e4532002-12-15 20:36:09 +0000217/// runOnMachineFunction - Register allocate the whole function
218///
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000219bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) {
Misha Brukman07218672002-11-22 22:44:32 +0000220 DEBUG(std::cerr << "Machine Function " << "\n");
221 MF = &Fn;
Chris Lattner600dee42002-12-28 20:42:14 +0000222 TM = &MF->getTarget();
223 RegInfo = TM->getRegisterInfo();
Misha Brukmandc2ec002002-12-03 23:15:19 +0000224
Chris Lattner9f366d72002-12-15 22:19:19 +0000225 // Loop over all of the basic blocks, eliminating virtual register references
Misha Brukman07218672002-11-22 22:44:32 +0000226 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end();
227 MBB != MBBe; ++MBB)
Chris Lattnerc2db1a92002-12-15 19:51:14 +0000228 AllocateBasicBlock(*MBB);
Misha Brukman07218672002-11-22 22:44:32 +0000229
Chris Lattner600dee42002-12-28 20:42:14 +0000230 StackSlotForVirtReg.clear();
Chris Lattner9f366d72002-12-15 22:19:19 +0000231 return true;
Misha Brukman07218672002-11-22 22:44:32 +0000232}
233
Brian Gaeke19df3872003-08-13 18:18:15 +0000234FunctionPass *createSimpleRegisterAllocator() {
Chris Lattner600dee42002-12-28 20:42:14 +0000235 return new RegAllocSimple();
Misha Brukman07218672002-11-22 22:44:32 +0000236}