LLVM API Documentation

TargetRegisterInfo.cpp
Go to the documentation of this file.
00001 //===- TargetRegisterInfo.cpp - Target Register Information Implementation ===//
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 TargetRegisterInfo interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Target/TargetRegisterInfo.h"
00015 #include "llvm/ADT/BitVector.h"
00016 #include "llvm/CodeGen/MachineFunction.h"
00017 #include "llvm/CodeGen/MachineRegisterInfo.h"
00018 #include "llvm/CodeGen/VirtRegMap.h"
00019 #include "llvm/Support/raw_ostream.h"
00020 
00021 using namespace llvm;
00022 
00023 TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
00024                              regclass_iterator RCB, regclass_iterator RCE,
00025                              const char *const *SRINames,
00026                              const unsigned *SRILaneMasks,
00027                              unsigned SRICoveringLanes)
00028   : InfoDesc(ID), SubRegIndexNames(SRINames),
00029     SubRegIndexLaneMasks(SRILaneMasks),
00030     RegClassBegin(RCB), RegClassEnd(RCE),
00031     CoveringLanes(SRICoveringLanes) {
00032 }
00033 
00034 TargetRegisterInfo::~TargetRegisterInfo() {}
00035 
00036 void PrintReg::print(raw_ostream &OS) const {
00037   if (!Reg)
00038     OS << "%noreg";
00039   else if (TargetRegisterInfo::isStackSlot(Reg))
00040     OS << "SS#" << TargetRegisterInfo::stackSlot2Index(Reg);
00041   else if (TargetRegisterInfo::isVirtualRegister(Reg))
00042     OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Reg);
00043   else if (TRI && Reg < TRI->getNumRegs())
00044     OS << '%' << TRI->getName(Reg);
00045   else
00046     OS << "%physreg" << Reg;
00047   if (SubIdx) {
00048     if (TRI)
00049       OS << ':' << TRI->getSubRegIndexName(SubIdx);
00050     else
00051       OS << ":sub(" << SubIdx << ')';
00052   }
00053 }
00054 
00055 void PrintRegUnit::print(raw_ostream &OS) const {
00056   // Generic printout when TRI is missing.
00057   if (!TRI) {
00058     OS << "Unit~" << Unit;
00059     return;
00060   }
00061 
00062   // Check for invalid register units.
00063   if (Unit >= TRI->getNumRegUnits()) {
00064     OS << "BadUnit~" << Unit;
00065     return;
00066   }
00067 
00068   // Normal units have at least one root.
00069   MCRegUnitRootIterator Roots(Unit, TRI);
00070   assert(Roots.isValid() && "Unit has no roots.");
00071   OS << TRI->getName(*Roots);
00072   for (++Roots; Roots.isValid(); ++Roots)
00073     OS << '~' << TRI->getName(*Roots);
00074 }
00075 
00076 void PrintVRegOrUnit::print(raw_ostream &OS) const {
00077   if (TRI && TRI->isVirtualRegister(Unit)) {
00078     OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Unit);
00079     return;
00080   }
00081   PrintRegUnit::print(OS);
00082 }
00083 
00084 /// getAllocatableClass - Return the maximal subclass of the given register
00085 /// class that is alloctable, or NULL.
00086 const TargetRegisterClass *
00087 TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
00088   if (!RC || RC->isAllocatable())
00089     return RC;
00090 
00091   const unsigned *SubClass = RC->getSubClassMask();
00092   for (unsigned Base = 0, BaseE = getNumRegClasses();
00093        Base < BaseE; Base += 32) {
00094     unsigned Idx = Base;
00095     for (unsigned Mask = *SubClass++; Mask; Mask >>= 1) {
00096       unsigned Offset = countTrailingZeros(Mask);
00097       const TargetRegisterClass *SubRC = getRegClass(Idx + Offset);
00098       if (SubRC->isAllocatable())
00099         return SubRC;
00100       Mask >>= Offset;
00101       Idx += Offset + 1;
00102     }
00103   }
00104   return nullptr;
00105 }
00106 
00107 /// getMinimalPhysRegClass - Returns the Register Class of a physical
00108 /// register of the given type, picking the most sub register class of
00109 /// the right type that contains this physreg.
00110 const TargetRegisterClass *
00111 TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, EVT VT) const {
00112   assert(isPhysicalRegister(reg) && "reg must be a physical register");
00113 
00114   // Pick the most sub register class of the right type that contains
00115   // this physreg.
00116   const TargetRegisterClass* BestRC = nullptr;
00117   for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
00118     const TargetRegisterClass* RC = *I;
00119     if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) &&
00120         (!BestRC || BestRC->hasSubClass(RC)))
00121       BestRC = RC;
00122   }
00123 
00124   assert(BestRC && "Couldn't find the register class");
00125   return BestRC;
00126 }
00127 
00128 /// getAllocatableSetForRC - Toggle the bits that represent allocatable
00129 /// registers for the specific register class.
00130 static void getAllocatableSetForRC(const MachineFunction &MF,
00131                                    const TargetRegisterClass *RC, BitVector &R){
00132   assert(RC->isAllocatable() && "invalid for nonallocatable sets");
00133   ArrayRef<MCPhysReg> Order = RC->getRawAllocationOrder(MF);
00134   for (unsigned i = 0; i != Order.size(); ++i)
00135     R.set(Order[i]);
00136 }
00137 
00138 BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
00139                                           const TargetRegisterClass *RC) const {
00140   BitVector Allocatable(getNumRegs());
00141   if (RC) {
00142     // A register class with no allocatable subclass returns an empty set.
00143     const TargetRegisterClass *SubClass = getAllocatableClass(RC);
00144     if (SubClass)
00145       getAllocatableSetForRC(MF, SubClass, Allocatable);
00146   } else {
00147     for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
00148          E = regclass_end(); I != E; ++I)
00149       if ((*I)->isAllocatable())
00150         getAllocatableSetForRC(MF, *I, Allocatable);
00151   }
00152 
00153   // Mask out the reserved registers
00154   BitVector Reserved = getReservedRegs(MF);
00155   Allocatable &= Reserved.flip();
00156 
00157   return Allocatable;
00158 }
00159 
00160 static inline
00161 const TargetRegisterClass *firstCommonClass(const uint32_t *A,
00162                                             const uint32_t *B,
00163                                             const TargetRegisterInfo *TRI) {
00164   for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32)
00165     if (unsigned Common = *A++ & *B++)
00166       return TRI->getRegClass(I + countTrailingZeros(Common));
00167   return nullptr;
00168 }
00169 
00170 const TargetRegisterClass *
00171 TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
00172                                       const TargetRegisterClass *B) const {
00173   // First take care of the trivial cases.
00174   if (A == B)
00175     return A;
00176   if (!A || !B)
00177     return nullptr;
00178 
00179   // Register classes are ordered topologically, so the largest common
00180   // sub-class it the common sub-class with the smallest ID.
00181   return firstCommonClass(A->getSubClassMask(), B->getSubClassMask(), this);
00182 }
00183 
00184 const TargetRegisterClass *
00185 TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
00186                                              const TargetRegisterClass *B,
00187                                              unsigned Idx) const {
00188   assert(A && B && "Missing register class");
00189   assert(Idx && "Bad sub-register index");
00190 
00191   // Find Idx in the list of super-register indices.
00192   for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI)
00193     if (RCI.getSubReg() == Idx)
00194       // The bit mask contains all register classes that are projected into B
00195       // by Idx. Find a class that is also a sub-class of A.
00196       return firstCommonClass(RCI.getMask(), A->getSubClassMask(), this);
00197   return nullptr;
00198 }
00199 
00200 const TargetRegisterClass *TargetRegisterInfo::
00201 getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
00202                        const TargetRegisterClass *RCB, unsigned SubB,
00203                        unsigned &PreA, unsigned &PreB) const {
00204   assert(RCA && SubA && RCB && SubB && "Invalid arguments");
00205 
00206   // Search all pairs of sub-register indices that project into RCA and RCB
00207   // respectively. This is quadratic, but usually the sets are very small. On
00208   // most targets like X86, there will only be a single sub-register index
00209   // (e.g., sub_16bit projecting into GR16).
00210   //
00211   // The worst case is a register class like DPR on ARM.
00212   // We have indices dsub_0..dsub_7 projecting into that class.
00213   //
00214   // It is very common that one register class is a sub-register of the other.
00215   // Arrange for RCA to be the larger register so the answer will be found in
00216   // the first iteration. This makes the search linear for the most common
00217   // case.
00218   const TargetRegisterClass *BestRC = nullptr;
00219   unsigned *BestPreA = &PreA;
00220   unsigned *BestPreB = &PreB;
00221   if (RCA->getSize() < RCB->getSize()) {
00222     std::swap(RCA, RCB);
00223     std::swap(SubA, SubB);
00224     std::swap(BestPreA, BestPreB);
00225   }
00226 
00227   // Also terminate the search one we have found a register class as small as
00228   // RCA.
00229   unsigned MinSize = RCA->getSize();
00230 
00231   for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) {
00232     unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA);
00233     for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) {
00234       // Check if a common super-register class exists for this index pair.
00235       const TargetRegisterClass *RC =
00236         firstCommonClass(IA.getMask(), IB.getMask(), this);
00237       if (!RC || RC->getSize() < MinSize)
00238         continue;
00239 
00240       // The indexes must compose identically: PreA+SubA == PreB+SubB.
00241       unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB);
00242       if (FinalA != FinalB)
00243         continue;
00244 
00245       // Is RC a better candidate than BestRC?
00246       if (BestRC && RC->getSize() >= BestRC->getSize())
00247         continue;
00248 
00249       // Yes, RC is the smallest super-register seen so far.
00250       BestRC = RC;
00251       *BestPreA = IA.getSubReg();
00252       *BestPreB = IB.getSubReg();
00253 
00254       // Bail early if we reached MinSize. We won't find a better candidate.
00255       if (BestRC->getSize() == MinSize)
00256         return BestRC;
00257     }
00258   }
00259   return BestRC;
00260 }
00261 
00262 // Compute target-independent register allocator hints to help eliminate copies.
00263 void
00264 TargetRegisterInfo::getRegAllocationHints(unsigned VirtReg,
00265                                           ArrayRef<MCPhysReg> Order,
00266                                           SmallVectorImpl<MCPhysReg> &Hints,
00267                                           const MachineFunction &MF,
00268                                           const VirtRegMap *VRM) const {
00269   const MachineRegisterInfo &MRI = MF.getRegInfo();
00270   std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg);
00271 
00272   // Hints with HintType != 0 were set by target-dependent code.
00273   // Such targets must provide their own implementation of
00274   // TRI::getRegAllocationHints to interpret those hint types.
00275   assert(Hint.first == 0 && "Target must implement TRI::getRegAllocationHints");
00276 
00277   // Target-independent hints are either a physical or a virtual register.
00278   unsigned Phys = Hint.second;
00279   if (VRM && isVirtualRegister(Phys))
00280     Phys = VRM->getPhys(Phys);
00281 
00282   // Check that Phys is a valid hint in VirtReg's register class.
00283   if (!isPhysicalRegister(Phys))
00284     return;
00285   if (MRI.isReserved(Phys))
00286     return;
00287   // Check that Phys is in the allocation order. We shouldn't heed hints
00288   // from VirtReg's register class if they aren't in the allocation order. The
00289   // target probably has a reason for removing the register.
00290   if (std::find(Order.begin(), Order.end(), Phys) == Order.end())
00291     return;
00292 
00293   // All clear, tell the register allocator to prefer this register.
00294   Hints.push_back(Phys);
00295 }