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