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 /// getAllocatableClass - Return the maximal subclass of the given register
00077 /// class that is alloctable, or NULL.
00078 const TargetRegisterClass *
00079 TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
00080   if (!RC || RC->isAllocatable())
00081     return RC;
00082 
00083   const unsigned *SubClass = RC->getSubClassMask();
00084   for (unsigned Base = 0, BaseE = getNumRegClasses();
00085        Base < BaseE; Base += 32) {
00086     unsigned Idx = Base;
00087     for (unsigned Mask = *SubClass++; Mask; Mask >>= 1) {
00088       unsigned Offset = countTrailingZeros(Mask);
00089       const TargetRegisterClass *SubRC = getRegClass(Idx + Offset);
00090       if (SubRC->isAllocatable())
00091         return SubRC;
00092       Mask >>= Offset;
00093       Idx += Offset + 1;
00094     }
00095   }
00096   return NULL;
00097 }
00098 
00099 /// getMinimalPhysRegClass - Returns the Register Class of a physical
00100 /// register of the given type, picking the most sub register class of
00101 /// the right type that contains this physreg.
00102 const TargetRegisterClass *
00103 TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, EVT VT) const {
00104   assert(isPhysicalRegister(reg) && "reg must be a physical register");
00105 
00106   // Pick the most sub register class of the right type that contains
00107   // this physreg.
00108   const TargetRegisterClass* BestRC = 0;
00109   for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
00110     const TargetRegisterClass* RC = *I;
00111     if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) &&
00112         (!BestRC || BestRC->hasSubClass(RC)))
00113       BestRC = RC;
00114   }
00115 
00116   assert(BestRC && "Couldn't find the register class");
00117   return BestRC;
00118 }
00119 
00120 /// getAllocatableSetForRC - Toggle the bits that represent allocatable
00121 /// registers for the specific register class.
00122 static void getAllocatableSetForRC(const MachineFunction &MF,
00123                                    const TargetRegisterClass *RC, BitVector &R){
00124   assert(RC->isAllocatable() && "invalid for nonallocatable sets");
00125   ArrayRef<uint16_t> Order = RC->getRawAllocationOrder(MF);
00126   for (unsigned i = 0; i != Order.size(); ++i)
00127     R.set(Order[i]);
00128 }
00129 
00130 BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
00131                                           const TargetRegisterClass *RC) const {
00132   BitVector Allocatable(getNumRegs());
00133   if (RC) {
00134     // A register class with no allocatable subclass returns an empty set.
00135     const TargetRegisterClass *SubClass = getAllocatableClass(RC);
00136     if (SubClass)
00137       getAllocatableSetForRC(MF, SubClass, Allocatable);
00138   } else {
00139     for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
00140          E = regclass_end(); I != E; ++I)
00141       if ((*I)->isAllocatable())
00142         getAllocatableSetForRC(MF, *I, Allocatable);
00143   }
00144 
00145   // Mask out the reserved registers
00146   BitVector Reserved = getReservedRegs(MF);
00147   Allocatable &= Reserved.flip();
00148 
00149   return Allocatable;
00150 }
00151 
00152 static inline
00153 const TargetRegisterClass *firstCommonClass(const uint32_t *A,
00154                                             const uint32_t *B,
00155                                             const TargetRegisterInfo *TRI) {
00156   for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32)
00157     if (unsigned Common = *A++ & *B++)
00158       return TRI->getRegClass(I + countTrailingZeros(Common));
00159   return 0;
00160 }
00161 
00162 const TargetRegisterClass *
00163 TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
00164                                       const TargetRegisterClass *B) const {
00165   // First take care of the trivial cases.
00166   if (A == B)
00167     return A;
00168   if (!A || !B)
00169     return 0;
00170 
00171   // Register classes are ordered topologically, so the largest common
00172   // sub-class it the common sub-class with the smallest ID.
00173   return firstCommonClass(A->getSubClassMask(), B->getSubClassMask(), this);
00174 }
00175 
00176 const TargetRegisterClass *
00177 TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
00178                                              const TargetRegisterClass *B,
00179                                              unsigned Idx) const {
00180   assert(A && B && "Missing register class");
00181   assert(Idx && "Bad sub-register index");
00182 
00183   // Find Idx in the list of super-register indices.
00184   for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI)
00185     if (RCI.getSubReg() == Idx)
00186       // The bit mask contains all register classes that are projected into B
00187       // by Idx. Find a class that is also a sub-class of A.
00188       return firstCommonClass(RCI.getMask(), A->getSubClassMask(), this);
00189   return 0;
00190 }
00191 
00192 const TargetRegisterClass *TargetRegisterInfo::
00193 getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
00194                        const TargetRegisterClass *RCB, unsigned SubB,
00195                        unsigned &PreA, unsigned &PreB) const {
00196   assert(RCA && SubA && RCB && SubB && "Invalid arguments");
00197 
00198   // Search all pairs of sub-register indices that project into RCA and RCB
00199   // respectively. This is quadratic, but usually the sets are very small. On
00200   // most targets like X86, there will only be a single sub-register index
00201   // (e.g., sub_16bit projecting into GR16).
00202   //
00203   // The worst case is a register class like DPR on ARM.
00204   // We have indices dsub_0..dsub_7 projecting into that class.
00205   //
00206   // It is very common that one register class is a sub-register of the other.
00207   // Arrange for RCA to be the larger register so the answer will be found in
00208   // the first iteration. This makes the search linear for the most common
00209   // case.
00210   const TargetRegisterClass *BestRC = 0;
00211   unsigned *BestPreA = &PreA;
00212   unsigned *BestPreB = &PreB;
00213   if (RCA->getSize() < RCB->getSize()) {
00214     std::swap(RCA, RCB);
00215     std::swap(SubA, SubB);
00216     std::swap(BestPreA, BestPreB);
00217   }
00218 
00219   // Also terminate the search one we have found a register class as small as
00220   // RCA.
00221   unsigned MinSize = RCA->getSize();
00222 
00223   for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) {
00224     unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA);
00225     for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) {
00226       // Check if a common super-register class exists for this index pair.
00227       const TargetRegisterClass *RC =
00228         firstCommonClass(IA.getMask(), IB.getMask(), this);
00229       if (!RC || RC->getSize() < MinSize)
00230         continue;
00231 
00232       // The indexes must compose identically: PreA+SubA == PreB+SubB.
00233       unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB);
00234       if (FinalA != FinalB)
00235         continue;
00236 
00237       // Is RC a better candidate than BestRC?
00238       if (BestRC && RC->getSize() >= BestRC->getSize())
00239         continue;
00240 
00241       // Yes, RC is the smallest super-register seen so far.
00242       BestRC = RC;
00243       *BestPreA = IA.getSubReg();
00244       *BestPreB = IB.getSubReg();
00245 
00246       // Bail early if we reached MinSize. We won't find a better candidate.
00247       if (BestRC->getSize() == MinSize)
00248         return BestRC;
00249     }
00250   }
00251   return BestRC;
00252 }
00253 
00254 // Compute target-independent register allocator hints to help eliminate copies.
00255 void
00256 TargetRegisterInfo::getRegAllocationHints(unsigned VirtReg,
00257                                           ArrayRef<MCPhysReg> Order,
00258                                           SmallVectorImpl<MCPhysReg> &Hints,
00259                                           const MachineFunction &MF,
00260                                           const VirtRegMap *VRM) const {
00261   const MachineRegisterInfo &MRI = MF.getRegInfo();
00262   std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg);
00263 
00264   // Hints with HintType != 0 were set by target-dependent code.
00265   // Such targets must provide their own implementation of
00266   // TRI::getRegAllocationHints to interpret those hint types.
00267   assert(Hint.first == 0 && "Target must implement TRI::getRegAllocationHints");
00268 
00269   // Target-independent hints are either a physical or a virtual register.
00270   unsigned Phys = Hint.second;
00271   if (VRM && isVirtualRegister(Phys))
00272     Phys = VRM->getPhys(Phys);
00273 
00274   // Check that Phys is a valid hint in VirtReg's register class.
00275   if (!isPhysicalRegister(Phys))
00276     return;
00277   if (MRI.isReserved(Phys))
00278     return;
00279   // Check that Phys is in the allocation order. We shouldn't heed hints
00280   // from VirtReg's register class if they aren't in the allocation order. The
00281   // target probably has a reason for removing the register.
00282   if (std::find(Order.begin(), Order.end(), Phys) == Order.end())
00283     return;
00284 
00285   // All clear, tell the register allocator to prefer this register.
00286   Hints.push_back(Phys);
00287 }