LLVM API Documentation

RegAllocBase.cpp
Go to the documentation of this file.
00001 //===-- RegAllocBase.cpp - Register Allocator Base Class ------------------===//
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 defines the RegAllocBase class which provides common functionality
00011 // for LiveIntervalUnion-based register allocators.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "RegAllocBase.h"
00016 #include "Spiller.h"
00017 #include "llvm/ADT/Statistic.h"
00018 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
00019 #include "llvm/CodeGen/LiveRangeEdit.h"
00020 #include "llvm/CodeGen/LiveRegMatrix.h"
00021 #include "llvm/CodeGen/MachineInstr.h"
00022 #include "llvm/CodeGen/MachineRegisterInfo.h"
00023 #include "llvm/CodeGen/VirtRegMap.h"
00024 #include "llvm/Target/TargetMachine.h"
00025 #include "llvm/Target/TargetRegisterInfo.h"
00026 #ifndef NDEBUG
00027 #include "llvm/ADT/SparseBitVector.h"
00028 #endif
00029 #include "llvm/Support/CommandLine.h"
00030 #include "llvm/Support/Debug.h"
00031 #include "llvm/Support/ErrorHandling.h"
00032 #include "llvm/Support/raw_ostream.h"
00033 #include "llvm/Support/Timer.h"
00034 
00035 using namespace llvm;
00036 
00037 #define DEBUG_TYPE "regalloc"
00038 
00039 STATISTIC(NumNewQueued    , "Number of new live ranges queued");
00040 
00041 // Temporary verification option until we can put verification inside
00042 // MachineVerifier.
00043 static cl::opt<bool, true>
00044 VerifyRegAlloc("verify-regalloc", cl::location(RegAllocBase::VerifyEnabled),
00045                cl::desc("Verify during register allocation"));
00046 
00047 const char RegAllocBase::TimerGroupName[] = "Register Allocation";
00048 bool RegAllocBase::VerifyEnabled = false;
00049 
00050 //===----------------------------------------------------------------------===//
00051 //                         RegAllocBase Implementation
00052 //===----------------------------------------------------------------------===//
00053 
00054 // Pin the vtable to this file.
00055 void RegAllocBase::anchor() {}
00056 
00057 void RegAllocBase::init(VirtRegMap &vrm,
00058                         LiveIntervals &lis,
00059                         LiveRegMatrix &mat) {
00060   TRI = &vrm.getTargetRegInfo();
00061   MRI = &vrm.getRegInfo();
00062   VRM = &vrm;
00063   LIS = &lis;
00064   Matrix = &mat;
00065   MRI->freezeReservedRegs(vrm.getMachineFunction());
00066   RegClassInfo.runOnMachineFunction(vrm.getMachineFunction());
00067 }
00068 
00069 // Visit all the live registers. If they are already assigned to a physical
00070 // register, unify them with the corresponding LiveIntervalUnion, otherwise push
00071 // them on the priority queue for later assignment.
00072 void RegAllocBase::seedLiveRegs() {
00073   NamedRegionTimer T("Seed Live Regs", TimerGroupName, TimePassesIsEnabled);
00074   for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
00075     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
00076     if (MRI->reg_nodbg_empty(Reg))
00077       continue;
00078     enqueue(&LIS->getInterval(Reg));
00079   }
00080 }
00081 
00082 // Top-level driver to manage the queue of unassigned VirtRegs and call the
00083 // selectOrSplit implementation.
00084 void RegAllocBase::allocatePhysRegs() {
00085   seedLiveRegs();
00086 
00087   // Continue assigning vregs one at a time to available physical registers.
00088   while (LiveInterval *VirtReg = dequeue()) {
00089     assert(!VRM->hasPhys(VirtReg->reg) && "Register already assigned");
00090 
00091     // Unused registers can appear when the spiller coalesces snippets.
00092     if (MRI->reg_nodbg_empty(VirtReg->reg)) {
00093       DEBUG(dbgs() << "Dropping unused " << *VirtReg << '\n');
00094       LIS->removeInterval(VirtReg->reg);
00095       continue;
00096     }
00097 
00098     // Invalidate all interference queries, live ranges could have changed.
00099     Matrix->invalidateVirtRegs();
00100 
00101     // selectOrSplit requests the allocator to return an available physical
00102     // register if possible and populate a list of new live intervals that
00103     // result from splitting.
00104     DEBUG(dbgs() << "\nselectOrSplit "
00105           << MRI->getRegClass(VirtReg->reg)->getName()
00106           << ':' << *VirtReg << " w=" << VirtReg->weight << '\n');
00107     typedef SmallVector<unsigned, 4> VirtRegVec;
00108     VirtRegVec SplitVRegs;
00109     unsigned AvailablePhysReg = selectOrSplit(*VirtReg, SplitVRegs);
00110 
00111     if (AvailablePhysReg == ~0u) {
00112       // selectOrSplit failed to find a register!
00113       // Probably caused by an inline asm.
00114       MachineInstr *MI = nullptr;
00115       for (MachineRegisterInfo::reg_instr_iterator
00116            I = MRI->reg_instr_begin(VirtReg->reg), E = MRI->reg_instr_end();
00117            I != E; ) {
00118         MachineInstr *TmpMI = &*(I++);
00119         if (TmpMI->isInlineAsm()) {
00120           MI = TmpMI;
00121           break;
00122         }
00123       }
00124       if (MI)
00125         MI->emitError("inline assembly requires more registers than available");
00126       else
00127         report_fatal_error("ran out of registers during register allocation");
00128       // Keep going after reporting the error.
00129       VRM->assignVirt2Phys(VirtReg->reg,
00130                  RegClassInfo.getOrder(MRI->getRegClass(VirtReg->reg)).front());
00131       continue;
00132     }
00133 
00134     if (AvailablePhysReg)
00135       Matrix->assign(*VirtReg, AvailablePhysReg);
00136 
00137     for (VirtRegVec::iterator I = SplitVRegs.begin(), E = SplitVRegs.end();
00138          I != E; ++I) {
00139       LiveInterval *SplitVirtReg = &LIS->getInterval(*I);
00140       assert(!VRM->hasPhys(SplitVirtReg->reg) && "Register already assigned");
00141       if (MRI->reg_nodbg_empty(SplitVirtReg->reg)) {
00142         DEBUG(dbgs() << "not queueing unused  " << *SplitVirtReg << '\n');
00143         LIS->removeInterval(SplitVirtReg->reg);
00144         continue;
00145       }
00146       DEBUG(dbgs() << "queuing new interval: " << *SplitVirtReg << "\n");
00147       assert(TargetRegisterInfo::isVirtualRegister(SplitVirtReg->reg) &&
00148              "expect split value in virtual register");
00149       enqueue(SplitVirtReg);
00150       ++NumNewQueued;
00151     }
00152   }
00153 }