LLVM API Documentation

LiveRegMatrix.cpp
Go to the documentation of this file.
00001 //===-- LiveRegMatrix.cpp - Track register interference -------------------===//
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 LiveRegMatrix analysis pass.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/CodeGen/LiveRegMatrix.h"
00015 #include "RegisterCoalescer.h"
00016 #include "llvm/ADT/Statistic.h"
00017 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
00018 #include "llvm/CodeGen/MachineRegisterInfo.h"
00019 #include "llvm/CodeGen/VirtRegMap.h"
00020 #include "llvm/Support/Debug.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 #include "llvm/Target/TargetMachine.h"
00023 #include "llvm/Target/TargetRegisterInfo.h"
00024 
00025 using namespace llvm;
00026 
00027 #define DEBUG_TYPE "regalloc"
00028 
00029 STATISTIC(NumAssigned   , "Number of registers assigned");
00030 STATISTIC(NumUnassigned , "Number of registers unassigned");
00031 
00032 char LiveRegMatrix::ID = 0;
00033 INITIALIZE_PASS_BEGIN(LiveRegMatrix, "liveregmatrix",
00034                       "Live Register Matrix", false, false)
00035 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
00036 INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
00037 INITIALIZE_PASS_END(LiveRegMatrix, "liveregmatrix",
00038                     "Live Register Matrix", false, false)
00039 
00040 LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID),
00041   UserTag(0), RegMaskTag(0), RegMaskVirtReg(0) {}
00042 
00043 void LiveRegMatrix::getAnalysisUsage(AnalysisUsage &AU) const {
00044   AU.setPreservesAll();
00045   AU.addRequiredTransitive<LiveIntervals>();
00046   AU.addRequiredTransitive<VirtRegMap>();
00047   MachineFunctionPass::getAnalysisUsage(AU);
00048 }
00049 
00050 bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) {
00051   TRI = MF.getSubtarget().getRegisterInfo();
00052   MRI = &MF.getRegInfo();
00053   LIS = &getAnalysis<LiveIntervals>();
00054   VRM = &getAnalysis<VirtRegMap>();
00055 
00056   unsigned NumRegUnits = TRI->getNumRegUnits();
00057   if (NumRegUnits != Matrix.size())
00058     Queries.reset(new LiveIntervalUnion::Query[NumRegUnits]);
00059   Matrix.init(LIUAlloc, NumRegUnits);
00060 
00061   // Make sure no stale queries get reused.
00062   invalidateVirtRegs();
00063   return false;
00064 }
00065 
00066 void LiveRegMatrix::releaseMemory() {
00067   for (unsigned i = 0, e = Matrix.size(); i != e; ++i) {
00068     Matrix[i].clear();
00069     // No need to clear Queries here, since LiveIntervalUnion::Query doesn't
00070     // have anything important to clear and LiveRegMatrix's runOnFunction()
00071     // does a std::unique_ptr::reset anyways.
00072   }
00073 }
00074 
00075 void LiveRegMatrix::assign(LiveInterval &VirtReg, unsigned PhysReg) {
00076   DEBUG(dbgs() << "assigning " << PrintReg(VirtReg.reg, TRI)
00077                << " to " << PrintReg(PhysReg, TRI) << ':');
00078   assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
00079   VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
00080   MRI->setPhysRegUsed(PhysReg);
00081   for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
00082     DEBUG(dbgs() << ' ' << PrintRegUnit(*Units, TRI));
00083     Matrix[*Units].unify(VirtReg);
00084   }
00085   ++NumAssigned;
00086   DEBUG(dbgs() << '\n');
00087 }
00088 
00089 void LiveRegMatrix::unassign(LiveInterval &VirtReg) {
00090   unsigned PhysReg = VRM->getPhys(VirtReg.reg);
00091   DEBUG(dbgs() << "unassigning " << PrintReg(VirtReg.reg, TRI)
00092                << " from " << PrintReg(PhysReg, TRI) << ':');
00093   VRM->clearVirt(VirtReg.reg);
00094   for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
00095     DEBUG(dbgs() << ' ' << PrintRegUnit(*Units, TRI));
00096     Matrix[*Units].extract(VirtReg);
00097   }
00098   ++NumUnassigned;
00099   DEBUG(dbgs() << '\n');
00100 }
00101 
00102 bool LiveRegMatrix::checkRegMaskInterference(LiveInterval &VirtReg,
00103                                              unsigned PhysReg) {
00104   // Check if the cached information is valid.
00105   // The same BitVector can be reused for all PhysRegs.
00106   // We could cache multiple VirtRegs if it becomes necessary.
00107   if (RegMaskVirtReg != VirtReg.reg || RegMaskTag != UserTag) {
00108     RegMaskVirtReg = VirtReg.reg;
00109     RegMaskTag = UserTag;
00110     RegMaskUsable.clear();
00111     LIS->checkRegMaskInterference(VirtReg, RegMaskUsable);
00112   }
00113 
00114   // The BitVector is indexed by PhysReg, not register unit.
00115   // Regmask interference is more fine grained than regunits.
00116   // For example, a Win64 call can clobber %ymm8 yet preserve %xmm8.
00117   return !RegMaskUsable.empty() && (!PhysReg || !RegMaskUsable.test(PhysReg));
00118 }
00119 
00120 bool LiveRegMatrix::checkRegUnitInterference(LiveInterval &VirtReg,
00121                                              unsigned PhysReg) {
00122   if (VirtReg.empty())
00123     return false;
00124   CoalescerPair CP(VirtReg.reg, PhysReg, *TRI);
00125   for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
00126     const LiveRange &UnitRange = LIS->getRegUnit(*Units);
00127     if (VirtReg.overlaps(UnitRange, CP, *LIS->getSlotIndexes()))
00128       return true;
00129   }
00130   return false;
00131 }
00132 
00133 LiveIntervalUnion::Query &LiveRegMatrix::query(LiveInterval &VirtReg,
00134                                                unsigned RegUnit) {
00135   LiveIntervalUnion::Query &Q = Queries[RegUnit];
00136   Q.init(UserTag, &VirtReg, &Matrix[RegUnit]);
00137   return Q;
00138 }
00139 
00140 LiveRegMatrix::InterferenceKind
00141 LiveRegMatrix::checkInterference(LiveInterval &VirtReg, unsigned PhysReg) {
00142   if (VirtReg.empty())
00143     return IK_Free;
00144 
00145   // Regmask interference is the fastest check.
00146   if (checkRegMaskInterference(VirtReg, PhysReg))
00147     return IK_RegMask;
00148 
00149   // Check for fixed interference.
00150   if (checkRegUnitInterference(VirtReg, PhysReg))
00151     return IK_RegUnit;
00152 
00153   // Check the matrix for virtual register interference.
00154   for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units)
00155     if (query(VirtReg, *Units).checkInterference())
00156       return IK_VirtReg;
00157 
00158   return IK_Free;
00159 }