LLVM API Documentation

RegisterClassInfo.cpp
Go to the documentation of this file.
00001 //===-- RegisterClassInfo.cpp - Dynamic Register Class Info ---------------===//
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 the RegisterClassInfo class which provides dynamic
00011 // information about target register classes. Callee-saved vs. caller-saved and
00012 // reserved registers depend on calling conventions and other dynamic
00013 // information, so some things cannot be determined statically.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #include "llvm/CodeGen/RegisterClassInfo.h"
00018 #include "llvm/CodeGen/MachineFunction.h"
00019 #include "llvm/CodeGen/MachineRegisterInfo.h"
00020 #include "llvm/Support/CommandLine.h"
00021 #include "llvm/Support/Debug.h"
00022 #include "llvm/Support/raw_ostream.h"
00023 #include "llvm/Target/TargetMachine.h"
00024 
00025 using namespace llvm;
00026 
00027 #define DEBUG_TYPE "regalloc"
00028 
00029 static cl::opt<unsigned>
00030 StressRA("stress-regalloc", cl::Hidden, cl::init(0), cl::value_desc("N"),
00031          cl::desc("Limit all regclasses to N registers"));
00032 
00033 RegisterClassInfo::RegisterClassInfo()
00034   : Tag(0), MF(nullptr), TRI(nullptr), CalleeSaved(nullptr) {}
00035 
00036 void RegisterClassInfo::runOnMachineFunction(const MachineFunction &mf) {
00037   bool Update = false;
00038   MF = &mf;
00039 
00040   // Allocate new array the first time we see a new target.
00041   if (MF->getSubtarget().getRegisterInfo() != TRI) {
00042     TRI = MF->getSubtarget().getRegisterInfo();
00043     RegClass.reset(new RCInfo[TRI->getNumRegClasses()]);
00044     unsigned NumPSets = TRI->getNumRegPressureSets();
00045     PSetLimits.reset(new unsigned[NumPSets]);
00046     std::fill(&PSetLimits[0], &PSetLimits[NumPSets], 0);
00047     Update = true;
00048   }
00049 
00050   // Does this MF have different CSRs?
00051   const MCPhysReg *CSR = TRI->getCalleeSavedRegs(MF);
00052   if (Update || CSR != CalleeSaved) {
00053     // Build a CSRNum map. Every CSR alias gets an entry pointing to the last
00054     // overlapping CSR.
00055     CSRNum.clear();
00056     CSRNum.resize(TRI->getNumRegs(), 0);
00057     for (unsigned N = 0; unsigned Reg = CSR[N]; ++N)
00058       for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
00059         CSRNum[*AI] = N + 1; // 0 means no CSR, 1 means CalleeSaved[0], ...
00060     Update = true;
00061   }
00062   CalleeSaved = CSR;
00063 
00064   // Different reserved registers?
00065   const BitVector &RR = MF->getRegInfo().getReservedRegs();
00066   if (Reserved.size() != RR.size() || RR != Reserved) {
00067     Update = true;
00068     Reserved = RR;
00069   }
00070 
00071   // Invalidate cached information from previous function.
00072   if (Update)
00073     ++Tag;
00074 }
00075 
00076 /// compute - Compute the preferred allocation order for RC with reserved
00077 /// registers filtered out. Volatile registers come first followed by CSR
00078 /// aliases ordered according to the CSR order specified by the target.
00079 void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
00080   RCInfo &RCI = RegClass[RC->getID()];
00081 
00082   // Raw register count, including all reserved regs.
00083   unsigned NumRegs = RC->getNumRegs();
00084 
00085   if (!RCI.Order)
00086     RCI.Order.reset(new MCPhysReg[NumRegs]);
00087 
00088   unsigned N = 0;
00089   SmallVector<MCPhysReg, 16> CSRAlias;
00090   unsigned MinCost = 0xff;
00091   unsigned LastCost = ~0u;
00092   unsigned LastCostChange = 0;
00093 
00094   // FIXME: Once targets reserve registers instead of removing them from the
00095   // allocation order, we can simply use begin/end here.
00096   ArrayRef<MCPhysReg> RawOrder = RC->getRawAllocationOrder(*MF);
00097   for (unsigned i = 0; i != RawOrder.size(); ++i) {
00098     unsigned PhysReg = RawOrder[i];
00099     // Remove reserved registers from the allocation order.
00100     if (Reserved.test(PhysReg))
00101       continue;
00102     unsigned Cost = TRI->getCostPerUse(PhysReg);
00103     MinCost = std::min(MinCost, Cost);
00104 
00105     if (CSRNum[PhysReg])
00106       // PhysReg aliases a CSR, save it for later.
00107       CSRAlias.push_back(PhysReg);
00108     else {
00109       if (Cost != LastCost)
00110         LastCostChange = N;
00111       RCI.Order[N++] = PhysReg;
00112       LastCost = Cost;
00113     }
00114   }
00115   RCI.NumRegs = N + CSRAlias.size();
00116   assert (RCI.NumRegs <= NumRegs && "Allocation order larger than regclass");
00117 
00118   // CSR aliases go after the volatile registers, preserve the target's order.
00119   for (unsigned i = 0, e = CSRAlias.size(); i != e; ++i) {
00120     unsigned PhysReg = CSRAlias[i];
00121     unsigned Cost = TRI->getCostPerUse(PhysReg);
00122     if (Cost != LastCost)
00123       LastCostChange = N;
00124     RCI.Order[N++] = PhysReg;
00125     LastCost = Cost;
00126   }
00127 
00128   // Register allocator stress test.  Clip register class to N registers.
00129   if (StressRA && RCI.NumRegs > StressRA)
00130     RCI.NumRegs = StressRA;
00131 
00132   // Check if RC is a proper sub-class.
00133   if (const TargetRegisterClass *Super = TRI->getLargestLegalSuperClass(RC))
00134     if (Super != RC && getNumAllocatableRegs(Super) > RCI.NumRegs)
00135       RCI.ProperSubClass = true;
00136 
00137   RCI.MinCost = uint8_t(MinCost);
00138   RCI.LastCostChange = LastCostChange;
00139 
00140   DEBUG({
00141     dbgs() << "AllocationOrder(" << RC->getName() << ") = [";
00142     for (unsigned I = 0; I != RCI.NumRegs; ++I)
00143       dbgs() << ' ' << PrintReg(RCI.Order[I], TRI);
00144     dbgs() << (RCI.ProperSubClass ? " ] (sub-class)\n" : " ]\n");
00145   });
00146 
00147   // RCI is now up-to-date.
00148   RCI.Tag = Tag;
00149 }
00150 
00151 /// This is not accurate because two overlapping register sets may have some
00152 /// nonoverlapping reserved registers. However, computing the allocation order
00153 /// for all register classes would be too expensive.
00154 unsigned RegisterClassInfo::computePSetLimit(unsigned Idx) const {
00155   const TargetRegisterClass *RC = nullptr;
00156   unsigned NumRCUnits = 0;
00157   for (TargetRegisterInfo::regclass_iterator
00158          RI = TRI->regclass_begin(), RE = TRI->regclass_end(); RI != RE; ++RI) {
00159     const int *PSetID = TRI->getRegClassPressureSets(*RI);
00160     for (; *PSetID != -1; ++PSetID) {
00161       if ((unsigned)*PSetID == Idx)
00162         break;
00163     }
00164     if (*PSetID == -1)
00165       continue;
00166 
00167     // Found a register class that counts against this pressure set.
00168     // For efficiency, only compute the set order for the largest set.
00169     unsigned NUnits = TRI->getRegClassWeight(*RI).WeightLimit;
00170     if (!RC || NUnits > NumRCUnits) {
00171       RC = *RI;
00172       NumRCUnits = NUnits;
00173     }
00174   }
00175   compute(RC);
00176   unsigned NReserved = RC->getNumRegs() - getNumAllocatableRegs(RC);
00177   return TRI->getRegPressureSetLimit(Idx)
00178     - TRI->getRegClassWeight(RC).RegWeight * NReserved;
00179 }