Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- RegAllocSimple.cpp - A simple generic register allocator ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // 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. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define DEBUG_TYPE "regalloc" |
| 18 | #include "llvm/CodeGen/Passes.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 20 | #include "llvm/CodeGen/MachineInstr.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/RegAllocRegistry.h" |
| 24 | #include "llvm/Target/TargetInstrInfo.h" |
| 25 | #include "llvm/Target/TargetMachine.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 540f0b7 | 2009-08-22 20:40:21 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include "llvm/ADT/STLExtras.h" |
Dan Gohman | 249ddbf | 2008-03-21 23:51:57 +0000 | [diff] [blame] | 31 | #include <map> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
| 34 | STATISTIC(NumStores, "Number of stores added"); |
| 35 | STATISTIC(NumLoads , "Number of loads added"); |
| 36 | |
| 37 | namespace { |
| 38 | static RegisterRegAlloc |
Dan Gohman | 669b9bf | 2008-10-14 20:25:08 +0000 | [diff] [blame] | 39 | simpleRegAlloc("simple", "simple register allocator", |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | createSimpleRegisterAllocator); |
| 41 | |
| 42 | class VISIBILITY_HIDDEN RegAllocSimple : public MachineFunctionPass { |
| 43 | public: |
| 44 | static char ID; |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 45 | RegAllocSimple() : MachineFunctionPass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 46 | private: |
| 47 | MachineFunction *MF; |
| 48 | const TargetMachine *TM; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 49 | const TargetRegisterInfo *TRI; |
Dan Gohman | ef83bfc | 2008-07-09 19:56:01 +0000 | [diff] [blame] | 50 | const TargetInstrInfo *TII; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | |
| 52 | // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where |
| 53 | // these values are spilled |
| 54 | std::map<unsigned, int> StackSlotForVirtReg; |
| 55 | |
| 56 | // RegsUsed - Keep track of what registers are currently in use. This is a |
| 57 | // bitset. |
| 58 | std::vector<bool> RegsUsed; |
| 59 | |
| 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. |
| 63 | std::map<const TargetRegisterClass*, unsigned> RegClassIdx; |
| 64 | |
| 65 | public: |
| 66 | virtual const char *getPassName() const { |
| 67 | return "Simple Register Allocator"; |
| 68 | } |
| 69 | |
| 70 | /// runOnMachineFunction - Register allocate the whole function |
| 71 | bool runOnMachineFunction(MachineFunction &Fn); |
| 72 | |
| 73 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | ecb436f | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 74 | AU.setPreservesCFG(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 75 | AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes |
| 76 | MachineFunctionPass::getAnalysisUsage(AU); |
| 77 | } |
| 78 | private: |
| 79 | /// AllocateBasicBlock - Register allocate the specified basic block. |
| 80 | void AllocateBasicBlock(MachineBasicBlock &MBB); |
| 81 | |
| 82 | /// getStackSpaceFor - This returns the offset of the specified virtual |
| 83 | /// register on the stack, allocating space if necessary. |
| 84 | int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC); |
| 85 | |
| 86 | /// Given a virtual register, return a compatible physical register that is |
| 87 | /// currently unused. |
| 88 | /// |
| 89 | /// Side effect: marks that register as being used until manually cleared |
| 90 | /// |
| 91 | unsigned getFreeReg(unsigned virtualReg); |
| 92 | |
| 93 | /// Moves value from memory into that register |
| 94 | unsigned reloadVirtReg(MachineBasicBlock &MBB, |
| 95 | MachineBasicBlock::iterator I, unsigned VirtReg); |
| 96 | |
| 97 | /// Saves reg value on the stack (maps virtual register to stack value) |
| 98 | void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, |
| 99 | unsigned VirtReg, unsigned PhysReg); |
| 100 | }; |
| 101 | char RegAllocSimple::ID = 0; |
| 102 | } |
| 103 | |
| 104 | /// getStackSpaceFor - This allocates space for the specified virtual |
| 105 | /// register to be held on the stack. |
| 106 | int RegAllocSimple::getStackSpaceFor(unsigned VirtReg, |
| 107 | const TargetRegisterClass *RC) { |
| 108 | // Find the location VirtReg would belong... |
Dan Gohman | 7fb3d54 | 2008-07-09 19:51:00 +0000 | [diff] [blame] | 109 | std::map<unsigned, int>::iterator I = StackSlotForVirtReg.find(VirtReg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | |
Dan Gohman | 7fb3d54 | 2008-07-09 19:51:00 +0000 | [diff] [blame] | 111 | if (I != StackSlotForVirtReg.end()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | return I->second; // Already has space allocated? |
| 113 | |
| 114 | // Allocate a new stack object for this spill location... |
| 115 | int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(), |
| 116 | RC->getAlignment()); |
| 117 | |
| 118 | // Assign the slot... |
| 119 | StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx)); |
| 120 | |
| 121 | return FrameIdx; |
| 122 | } |
| 123 | |
| 124 | unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) { |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 125 | const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtualReg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 126 | TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF); |
Devang Patel | e71304c | 2008-12-23 21:55:04 +0000 | [diff] [blame] | 127 | #ifndef NDEBUG |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 128 | TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF); |
Devang Patel | e71304c | 2008-12-23 21:55:04 +0000 | [diff] [blame] | 129 | #endif |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 130 | |
| 131 | while (1) { |
| 132 | unsigned regIdx = RegClassIdx[RC]++; |
| 133 | assert(RI+regIdx != RE && "Not enough registers!"); |
| 134 | unsigned PhysReg = *(RI+regIdx); |
| 135 | |
| 136 | if (!RegsUsed[PhysReg]) { |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 137 | MF->getRegInfo().setPhysRegUsed(PhysReg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 138 | return PhysReg; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB, |
| 144 | MachineBasicBlock::iterator I, |
| 145 | unsigned VirtReg) { |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 146 | const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 147 | int FrameIdx = getStackSpaceFor(VirtReg, RC); |
| 148 | unsigned PhysReg = getFreeReg(VirtReg); |
| 149 | |
| 150 | // Add move instruction(s) |
| 151 | ++NumLoads; |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 152 | TII->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 153 | return PhysReg; |
| 154 | } |
| 155 | |
| 156 | void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB, |
| 157 | MachineBasicBlock::iterator I, |
| 158 | unsigned VirtReg, unsigned PhysReg) { |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 159 | const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg); |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 160 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 161 | int FrameIdx = getStackSpaceFor(VirtReg, RC); |
| 162 | |
| 163 | // Add move instruction(s) |
| 164 | ++NumStores; |
Owen Anderson | 8187543 | 2008-01-01 21:11:32 +0000 | [diff] [blame] | 165 | TII->storeRegToStackSlot(MBB, I, PhysReg, true, FrameIdx, RC); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
| 169 | void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) { |
| 170 | // loop over each instruction |
| 171 | for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) { |
| 172 | // Made to combat the incorrect allocation of r2 = add r1, r1 |
| 173 | std::map<unsigned, unsigned> Virt2PhysRegMap; |
| 174 | |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 175 | RegsUsed.resize(TRI->getNumRegs()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 176 | |
| 177 | // This is a preliminary pass that will invalidate any registers that are |
| 178 | // used by the instruction (including implicit uses). |
Chris Lattner | 5b93037 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 179 | const TargetInstrDesc &Desc = MI->getDesc(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 180 | const unsigned *Regs; |
| 181 | if (Desc.ImplicitUses) { |
| 182 | for (Regs = Desc.ImplicitUses; *Regs; ++Regs) |
| 183 | RegsUsed[*Regs] = true; |
| 184 | } |
| 185 | |
| 186 | if (Desc.ImplicitDefs) { |
| 187 | for (Regs = Desc.ImplicitDefs; *Regs; ++Regs) { |
| 188 | RegsUsed[*Regs] = true; |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 189 | MF->getRegInfo().setPhysRegUsed(*Regs); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | // Loop over uses, move from memory into registers. |
| 194 | for (int i = MI->getNumOperands() - 1; i >= 0; --i) { |
Dan Gohman | 7f31037a | 2008-07-09 20:12:26 +0000 | [diff] [blame] | 195 | MachineOperand &MO = MI->getOperand(i); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 196 | |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 197 | if (MO.isReg() && MO.getReg() && |
Dan Gohman | 7f31037a | 2008-07-09 20:12:26 +0000 | [diff] [blame] | 198 | TargetRegisterInfo::isVirtualRegister(MO.getReg())) { |
| 199 | unsigned virtualReg = (unsigned) MO.getReg(); |
Bill Wendling | 540f0b7 | 2009-08-22 20:40:21 +0000 | [diff] [blame] | 200 | DEBUG({ |
| 201 | errs() << "op: " << MO << "\n" << "\t inst[" << i << "]: "; |
| 202 | MI->print(errs(), TM); |
| 203 | }); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 204 | |
| 205 | // make sure the same virtual register maps to the same physical |
| 206 | // register in any given instruction |
| 207 | unsigned physReg = Virt2PhysRegMap[virtualReg]; |
| 208 | if (physReg == 0) { |
Dan Gohman | 7f31037a | 2008-07-09 20:12:26 +0000 | [diff] [blame] | 209 | if (MO.isDef()) { |
Bob Wilson | aded995 | 2009-04-09 17:16:43 +0000 | [diff] [blame] | 210 | unsigned TiedOp; |
| 211 | if (!MI->isRegTiedToUseOperand(i, &TiedOp)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 212 | physReg = getFreeReg(virtualReg); |
| 213 | } else { |
| 214 | // 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 Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 217 | assert(MI->getOperand(TiedOp).isReg() && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 218 | MI->getOperand(TiedOp).getReg() && |
| 219 | MI->getOperand(TiedOp).isUse() && |
| 220 | "Two address instruction invalid!"); |
| 221 | |
| 222 | physReg = MI->getOperand(TiedOp).getReg(); |
| 223 | } |
| 224 | spillVirtReg(MBB, next(MI), virtualReg, physReg); |
| 225 | } else { |
| 226 | physReg = reloadVirtReg(MBB, MI, virtualReg); |
| 227 | Virt2PhysRegMap[virtualReg] = physReg; |
| 228 | } |
| 229 | } |
Dan Gohman | 7f31037a | 2008-07-09 20:12:26 +0000 | [diff] [blame] | 230 | MO.setReg(physReg); |
Bill Wendling | 540f0b7 | 2009-08-22 20:40:21 +0000 | [diff] [blame] | 231 | DEBUG(errs() << "virt: " << virtualReg |
| 232 | << ", phys: " << MO.getReg() << "\n"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | RegClassIdx.clear(); |
| 236 | RegsUsed.clear(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /// runOnMachineFunction - Register allocate the whole function |
| 242 | /// |
| 243 | bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) { |
Bill Wendling | 540f0b7 | 2009-08-22 20:40:21 +0000 | [diff] [blame] | 244 | DEBUG(errs() << "Machine Function\n"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 245 | MF = &Fn; |
| 246 | TM = &MF->getTarget(); |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 247 | TRI = TM->getRegisterInfo(); |
Dan Gohman | ef83bfc | 2008-07-09 19:56:01 +0000 | [diff] [blame] | 248 | TII = TM->getInstrInfo(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 249 | |
| 250 | // Loop over all of the basic blocks, eliminating virtual register references |
| 251 | for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); |
| 252 | MBB != MBBe; ++MBB) |
| 253 | AllocateBasicBlock(*MBB); |
| 254 | |
| 255 | StackSlotForVirtReg.clear(); |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | FunctionPass *llvm::createSimpleRegisterAllocator() { |
| 260 | return new RegAllocSimple(); |
| 261 | } |