LLVM API Documentation
00001 //===-- RegAllocSimple.cpp - A simple generic register allocator ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a simple register allocator. *Very* simple: It immediate 00011 // spills every value right after it is computed, and it reloads all used 00012 // operands from the spill area to temporary registers before each instruction. 00013 // It does not keep values in registers across instructions. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #define DEBUG_TYPE "regalloc" 00018 #include "llvm/CodeGen/Passes.h" 00019 #include "llvm/CodeGen/MachineFunctionPass.h" 00020 #include "llvm/CodeGen/MachineInstr.h" 00021 #include "llvm/CodeGen/MachineFrameInfo.h" 00022 #include "llvm/CodeGen/MachineRegisterInfo.h" 00023 #include "llvm/CodeGen/RegAllocRegistry.h" 00024 #include "llvm/Target/TargetInstrInfo.h" 00025 #include "llvm/Target/TargetMachine.h" 00026 #include "llvm/Support/Debug.h" 00027 #include "llvm/Support/Compiler.h" 00028 #include "llvm/ADT/Statistic.h" 00029 #include "llvm/ADT/STLExtras.h" 00030 #include <map> 00031 using namespace llvm; 00032 00033 STATISTIC(NumStores, "Number of stores added"); 00034 STATISTIC(NumLoads , "Number of loads added"); 00035 00036 namespace { 00037 static RegisterRegAlloc 00038 simpleRegAlloc("simple", "simple register allocator", 00039 createSimpleRegisterAllocator); 00040 00041 class VISIBILITY_HIDDEN RegAllocSimple : public MachineFunctionPass { 00042 public: 00043 static char ID; 00044 RegAllocSimple() : MachineFunctionPass(&ID) {} 00045 private: 00046 MachineFunction *MF; 00047 const TargetMachine *TM; 00048 const TargetRegisterInfo *TRI; 00049 const TargetInstrInfo *TII; 00050 00051 // StackSlotForVirtReg - Maps SSA Regs => frame index on the stack where 00052 // these values are spilled 00053 std::map<unsigned, int> StackSlotForVirtReg; 00054 00055 // RegsUsed - Keep track of what registers are currently in use. This is a 00056 // bitset. 00057 std::vector<bool> RegsUsed; 00058 00059 // RegClassIdx - Maps RegClass => which index we can take a register 00060 // from. Since this is a simple register allocator, when we need a register 00061 // of a certain class, we just take the next available one. 00062 std::map<const TargetRegisterClass*, unsigned> RegClassIdx; 00063 00064 public: 00065 virtual const char *getPassName() const { 00066 return "Simple Register Allocator"; 00067 } 00068 00069 /// runOnMachineFunction - Register allocate the whole function 00070 bool runOnMachineFunction(MachineFunction &Fn); 00071 00072 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00073 AU.addRequiredID(PHIEliminationID); // Eliminate PHI nodes 00074 MachineFunctionPass::getAnalysisUsage(AU); 00075 } 00076 private: 00077 /// AllocateBasicBlock - Register allocate the specified basic block. 00078 void AllocateBasicBlock(MachineBasicBlock &MBB); 00079 00080 /// getStackSpaceFor - This returns the offset of the specified virtual 00081 /// register on the stack, allocating space if necessary. 00082 int getStackSpaceFor(unsigned VirtReg, const TargetRegisterClass *RC); 00083 00084 /// Given a virtual register, return a compatible physical register that is 00085 /// currently unused. 00086 /// 00087 /// Side effect: marks that register as being used until manually cleared 00088 /// 00089 unsigned getFreeReg(unsigned virtualReg); 00090 00091 /// Moves value from memory into that register 00092 unsigned reloadVirtReg(MachineBasicBlock &MBB, 00093 MachineBasicBlock::iterator I, unsigned VirtReg); 00094 00095 /// Saves reg value on the stack (maps virtual register to stack value) 00096 void spillVirtReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, 00097 unsigned VirtReg, unsigned PhysReg); 00098 }; 00099 char RegAllocSimple::ID = 0; 00100 } 00101 00102 /// getStackSpaceFor - This allocates space for the specified virtual 00103 /// register to be held on the stack. 00104 int RegAllocSimple::getStackSpaceFor(unsigned VirtReg, 00105 const TargetRegisterClass *RC) { 00106 // Find the location VirtReg would belong... 00107 std::map<unsigned, int>::iterator I = StackSlotForVirtReg.find(VirtReg); 00108 00109 if (I != StackSlotForVirtReg.end()) 00110 return I->second; // Already has space allocated? 00111 00112 // Allocate a new stack object for this spill location... 00113 int FrameIdx = MF->getFrameInfo()->CreateStackObject(RC->getSize(), 00114 RC->getAlignment()); 00115 00116 // Assign the slot... 00117 StackSlotForVirtReg.insert(I, std::make_pair(VirtReg, FrameIdx)); 00118 00119 return FrameIdx; 00120 } 00121 00122 unsigned RegAllocSimple::getFreeReg(unsigned virtualReg) { 00123 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(virtualReg); 00124 TargetRegisterClass::iterator RI = RC->allocation_order_begin(*MF); 00125 #ifndef NDEBUG 00126 TargetRegisterClass::iterator RE = RC->allocation_order_end(*MF); 00127 #endif 00128 00129 while (1) { 00130 unsigned regIdx = RegClassIdx[RC]++; 00131 assert(RI+regIdx != RE && "Not enough registers!"); 00132 unsigned PhysReg = *(RI+regIdx); 00133 00134 if (!RegsUsed[PhysReg]) { 00135 MF->getRegInfo().setPhysRegUsed(PhysReg); 00136 return PhysReg; 00137 } 00138 } 00139 } 00140 00141 unsigned RegAllocSimple::reloadVirtReg(MachineBasicBlock &MBB, 00142 MachineBasicBlock::iterator I, 00143 unsigned VirtReg) { 00144 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg); 00145 int FrameIdx = getStackSpaceFor(VirtReg, RC); 00146 unsigned PhysReg = getFreeReg(VirtReg); 00147 00148 // Add move instruction(s) 00149 ++NumLoads; 00150 TII->loadRegFromStackSlot(MBB, I, PhysReg, FrameIdx, RC); 00151 return PhysReg; 00152 } 00153 00154 void RegAllocSimple::spillVirtReg(MachineBasicBlock &MBB, 00155 MachineBasicBlock::iterator I, 00156 unsigned VirtReg, unsigned PhysReg) { 00157 const TargetRegisterClass* RC = MF->getRegInfo().getRegClass(VirtReg); 00158 00159 int FrameIdx = getStackSpaceFor(VirtReg, RC); 00160 00161 // Add move instruction(s) 00162 ++NumStores; 00163 TII->storeRegToStackSlot(MBB, I, PhysReg, true, FrameIdx, RC); 00164 } 00165 00166 00167 void RegAllocSimple::AllocateBasicBlock(MachineBasicBlock &MBB) { 00168 // loop over each instruction 00169 for (MachineBasicBlock::iterator MI = MBB.begin(); MI != MBB.end(); ++MI) { 00170 // Made to combat the incorrect allocation of r2 = add r1, r1 00171 std::map<unsigned, unsigned> Virt2PhysRegMap; 00172 00173 RegsUsed.resize(TRI->getNumRegs()); 00174 00175 // This is a preliminary pass that will invalidate any registers that are 00176 // used by the instruction (including implicit uses). 00177 const TargetInstrDesc &Desc = MI->getDesc(); 00178 const unsigned *Regs; 00179 if (Desc.ImplicitUses) { 00180 for (Regs = Desc.ImplicitUses; *Regs; ++Regs) 00181 RegsUsed[*Regs] = true; 00182 } 00183 00184 if (Desc.ImplicitDefs) { 00185 for (Regs = Desc.ImplicitDefs; *Regs; ++Regs) { 00186 RegsUsed[*Regs] = true; 00187 MF->getRegInfo().setPhysRegUsed(*Regs); 00188 } 00189 } 00190 00191 // Loop over uses, move from memory into registers. 00192 for (int i = MI->getNumOperands() - 1; i >= 0; --i) { 00193 MachineOperand &MO = MI->getOperand(i); 00194 00195 if (MO.isReg() && MO.getReg() && 00196 TargetRegisterInfo::isVirtualRegister(MO.getReg())) { 00197 unsigned virtualReg = (unsigned) MO.getReg(); 00198 DOUT << "op: " << MO << "\n"; 00199 DOUT << "\t inst[" << i << "]: "; 00200 DEBUG(MI->print(*cerr.stream(), TM)); 00201 00202 // make sure the same virtual register maps to the same physical 00203 // register in any given instruction 00204 unsigned physReg = Virt2PhysRegMap[virtualReg]; 00205 if (physReg == 0) { 00206 if (MO.isDef()) { 00207 int TiedOp = Desc.findTiedToSrcOperand(i); 00208 if (TiedOp == -1) { 00209 physReg = getFreeReg(virtualReg); 00210 } else { 00211 // must be same register number as the source operand that is 00212 // tied to. This maps a = b + c into b = b + c, and saves b into 00213 // a's spot. 00214 assert(MI->getOperand(TiedOp).isReg() && 00215 MI->getOperand(TiedOp).getReg() && 00216 MI->getOperand(TiedOp).isUse() && 00217 "Two address instruction invalid!"); 00218 00219 physReg = MI->getOperand(TiedOp).getReg(); 00220 } 00221 spillVirtReg(MBB, next(MI), virtualReg, physReg); 00222 } else { 00223 physReg = reloadVirtReg(MBB, MI, virtualReg); 00224 Virt2PhysRegMap[virtualReg] = physReg; 00225 } 00226 } 00227 MO.setReg(physReg); 00228 DOUT << "virt: " << virtualReg << ", phys: " << MO.getReg() << "\n"; 00229 } 00230 } 00231 RegClassIdx.clear(); 00232 RegsUsed.clear(); 00233 } 00234 } 00235 00236 00237 /// runOnMachineFunction - Register allocate the whole function 00238 /// 00239 bool RegAllocSimple::runOnMachineFunction(MachineFunction &Fn) { 00240 DOUT << "Machine Function\n"; 00241 MF = &Fn; 00242 TM = &MF->getTarget(); 00243 TRI = TM->getRegisterInfo(); 00244 TII = TM->getInstrInfo(); 00245 00246 // Loop over all of the basic blocks, eliminating virtual register references 00247 for (MachineFunction::iterator MBB = Fn.begin(), MBBe = Fn.end(); 00248 MBB != MBBe; ++MBB) 00249 AllocateBasicBlock(*MBB); 00250 00251 StackSlotForVirtReg.clear(); 00252 return true; 00253 } 00254 00255 FunctionPass *llvm::createSimpleRegisterAllocator() { 00256 return new RegAllocSimple(); 00257 }