TargetRegisterInfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //===- TargetRegisterInfo.cpp - Target Register Information Implementation ===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the TargetRegisterInfo interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Target/TargetRegisterInfo.h"
  14. #include "llvm/ADT/BitVector.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. #include "llvm/CodeGen/VirtRegMap.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. TargetRegisterInfo::TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
  22. regclass_iterator RCB, regclass_iterator RCE,
  23. const char *const *SRINames,
  24. const unsigned *SRILaneMasks,
  25. unsigned SRICoveringLanes)
  26. : InfoDesc(ID), SubRegIndexNames(SRINames),
  27. SubRegIndexLaneMasks(SRILaneMasks),
  28. RegClassBegin(RCB), RegClassEnd(RCE),
  29. CoveringLanes(SRICoveringLanes) {
  30. }
  31. TargetRegisterInfo::~TargetRegisterInfo() {}
  32. void PrintReg::print(raw_ostream &OS) const {
  33. if (!Reg)
  34. OS << "%noreg";
  35. else if (TargetRegisterInfo::isStackSlot(Reg))
  36. OS << "SS#" << TargetRegisterInfo::stackSlot2Index(Reg);
  37. else if (TargetRegisterInfo::isVirtualRegister(Reg))
  38. OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Reg);
  39. else if (TRI && Reg < TRI->getNumRegs())
  40. OS << '%' << TRI->getName(Reg);
  41. else
  42. OS << "%physreg" << Reg;
  43. if (SubIdx) {
  44. if (TRI)
  45. OS << ':' << TRI->getSubRegIndexName(SubIdx);
  46. else
  47. OS << ":sub(" << SubIdx << ')';
  48. }
  49. }
  50. void PrintRegUnit::print(raw_ostream &OS) const {
  51. // Generic printout when TRI is missing.
  52. if (!TRI) {
  53. OS << "Unit~" << Unit;
  54. return;
  55. }
  56. // Check for invalid register units.
  57. if (Unit >= TRI->getNumRegUnits()) {
  58. OS << "BadUnit~" << Unit;
  59. return;
  60. }
  61. // Normal units have at least one root.
  62. MCRegUnitRootIterator Roots(Unit, TRI);
  63. assert(Roots.isValid() && "Unit has no roots.");
  64. OS << TRI->getName(*Roots);
  65. for (++Roots; Roots.isValid(); ++Roots)
  66. OS << '~' << TRI->getName(*Roots);
  67. }
  68. void PrintVRegOrUnit::print(raw_ostream &OS) const {
  69. if (TRI && TRI->isVirtualRegister(Unit)) {
  70. OS << "%vreg" << TargetRegisterInfo::virtReg2Index(Unit);
  71. return;
  72. }
  73. PrintRegUnit::print(OS);
  74. }
  75. /// getAllocatableClass - Return the maximal subclass of the given register
  76. /// class that is alloctable, or NULL.
  77. const TargetRegisterClass *
  78. TargetRegisterInfo::getAllocatableClass(const TargetRegisterClass *RC) const {
  79. if (!RC || RC->isAllocatable())
  80. return RC;
  81. const unsigned *SubClass = RC->getSubClassMask();
  82. for (unsigned Base = 0, BaseE = getNumRegClasses();
  83. Base < BaseE; Base += 32) {
  84. unsigned Idx = Base;
  85. for (unsigned Mask = *SubClass++; Mask; Mask >>= 1) {
  86. unsigned Offset = countTrailingZeros(Mask);
  87. const TargetRegisterClass *SubRC = getRegClass(Idx + Offset);
  88. if (SubRC->isAllocatable())
  89. return SubRC;
  90. Mask >>= Offset;
  91. Idx += Offset + 1;
  92. }
  93. }
  94. return nullptr;
  95. }
  96. /// getMinimalPhysRegClass - Returns the Register Class of a physical
  97. /// register of the given type, picking the most sub register class of
  98. /// the right type that contains this physreg.
  99. const TargetRegisterClass *
  100. TargetRegisterInfo::getMinimalPhysRegClass(unsigned reg, MVT VT) const {
  101. assert(isPhysicalRegister(reg) && "reg must be a physical register");
  102. // Pick the most sub register class of the right type that contains
  103. // this physreg.
  104. const TargetRegisterClass* BestRC = nullptr;
  105. for (regclass_iterator I = regclass_begin(), E = regclass_end(); I != E; ++I){
  106. const TargetRegisterClass* RC = *I;
  107. if ((VT == MVT::Other || RC->hasType(VT)) && RC->contains(reg) &&
  108. (!BestRC || BestRC->hasSubClass(RC)))
  109. BestRC = RC;
  110. }
  111. assert(BestRC && "Couldn't find the register class");
  112. return BestRC;
  113. }
  114. /// getAllocatableSetForRC - Toggle the bits that represent allocatable
  115. /// registers for the specific register class.
  116. static void getAllocatableSetForRC(const MachineFunction &MF,
  117. const TargetRegisterClass *RC, BitVector &R){
  118. assert(RC->isAllocatable() && "invalid for nonallocatable sets");
  119. ArrayRef<MCPhysReg> Order = RC->getRawAllocationOrder(MF);
  120. for (unsigned i = 0; i != Order.size(); ++i)
  121. R.set(Order[i]);
  122. }
  123. BitVector TargetRegisterInfo::getAllocatableSet(const MachineFunction &MF,
  124. const TargetRegisterClass *RC) const {
  125. BitVector Allocatable(getNumRegs());
  126. if (RC) {
  127. // A register class with no allocatable subclass returns an empty set.
  128. const TargetRegisterClass *SubClass = getAllocatableClass(RC);
  129. if (SubClass)
  130. getAllocatableSetForRC(MF, SubClass, Allocatable);
  131. } else {
  132. for (TargetRegisterInfo::regclass_iterator I = regclass_begin(),
  133. E = regclass_end(); I != E; ++I)
  134. if ((*I)->isAllocatable())
  135. getAllocatableSetForRC(MF, *I, Allocatable);
  136. }
  137. // Mask out the reserved registers
  138. BitVector Reserved = getReservedRegs(MF);
  139. Allocatable &= Reserved.flip();
  140. return Allocatable;
  141. }
  142. static inline
  143. const TargetRegisterClass *firstCommonClass(const uint32_t *A,
  144. const uint32_t *B,
  145. const TargetRegisterInfo *TRI) {
  146. for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; I += 32)
  147. if (unsigned Common = *A++ & *B++)
  148. return TRI->getRegClass(I + countTrailingZeros(Common));
  149. return nullptr;
  150. }
  151. const TargetRegisterClass *
  152. TargetRegisterInfo::getCommonSubClass(const TargetRegisterClass *A,
  153. const TargetRegisterClass *B) const {
  154. // First take care of the trivial cases.
  155. if (A == B)
  156. return A;
  157. if (!A || !B)
  158. return nullptr;
  159. // Register classes are ordered topologically, so the largest common
  160. // sub-class it the common sub-class with the smallest ID.
  161. return firstCommonClass(A->getSubClassMask(), B->getSubClassMask(), this);
  162. }
  163. const TargetRegisterClass *
  164. TargetRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
  165. const TargetRegisterClass *B,
  166. unsigned Idx) const {
  167. assert(A && B && "Missing register class");
  168. assert(Idx && "Bad sub-register index");
  169. // Find Idx in the list of super-register indices.
  170. for (SuperRegClassIterator RCI(B, this); RCI.isValid(); ++RCI)
  171. if (RCI.getSubReg() == Idx)
  172. // The bit mask contains all register classes that are projected into B
  173. // by Idx. Find a class that is also a sub-class of A.
  174. return firstCommonClass(RCI.getMask(), A->getSubClassMask(), this);
  175. return nullptr;
  176. }
  177. const TargetRegisterClass *TargetRegisterInfo::
  178. getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
  179. const TargetRegisterClass *RCB, unsigned SubB,
  180. unsigned &PreA, unsigned &PreB) const {
  181. assert(RCA && SubA && RCB && SubB && "Invalid arguments");
  182. // Search all pairs of sub-register indices that project into RCA and RCB
  183. // respectively. This is quadratic, but usually the sets are very small. On
  184. // most targets like X86, there will only be a single sub-register index
  185. // (e.g., sub_16bit projecting into GR16).
  186. //
  187. // The worst case is a register class like DPR on ARM.
  188. // We have indices dsub_0..dsub_7 projecting into that class.
  189. //
  190. // It is very common that one register class is a sub-register of the other.
  191. // Arrange for RCA to be the larger register so the answer will be found in
  192. // the first iteration. This makes the search linear for the most common
  193. // case.
  194. const TargetRegisterClass *BestRC = nullptr;
  195. unsigned *BestPreA = &PreA;
  196. unsigned *BestPreB = &PreB;
  197. if (RCA->getSize() < RCB->getSize()) {
  198. std::swap(RCA, RCB);
  199. std::swap(SubA, SubB);
  200. std::swap(BestPreA, BestPreB);
  201. }
  202. // Also terminate the search one we have found a register class as small as
  203. // RCA.
  204. unsigned MinSize = RCA->getSize();
  205. for (SuperRegClassIterator IA(RCA, this, true); IA.isValid(); ++IA) {
  206. unsigned FinalA = composeSubRegIndices(IA.getSubReg(), SubA);
  207. for (SuperRegClassIterator IB(RCB, this, true); IB.isValid(); ++IB) {
  208. // Check if a common super-register class exists for this index pair.
  209. const TargetRegisterClass *RC =
  210. firstCommonClass(IA.getMask(), IB.getMask(), this);
  211. if (!RC || RC->getSize() < MinSize)
  212. continue;
  213. // The indexes must compose identically: PreA+SubA == PreB+SubB.
  214. unsigned FinalB = composeSubRegIndices(IB.getSubReg(), SubB);
  215. if (FinalA != FinalB)
  216. continue;
  217. // Is RC a better candidate than BestRC?
  218. if (BestRC && RC->getSize() >= BestRC->getSize())
  219. continue;
  220. // Yes, RC is the smallest super-register seen so far.
  221. BestRC = RC;
  222. *BestPreA = IA.getSubReg();
  223. *BestPreB = IB.getSubReg();
  224. // Bail early if we reached MinSize. We won't find a better candidate.
  225. if (BestRC->getSize() == MinSize)
  226. return BestRC;
  227. }
  228. }
  229. return BestRC;
  230. }
  231. // Compute target-independent register allocator hints to help eliminate copies.
  232. void
  233. TargetRegisterInfo::getRegAllocationHints(unsigned VirtReg,
  234. ArrayRef<MCPhysReg> Order,
  235. SmallVectorImpl<MCPhysReg> &Hints,
  236. const MachineFunction &MF,
  237. const VirtRegMap *VRM) const {
  238. const MachineRegisterInfo &MRI = MF.getRegInfo();
  239. std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg);
  240. // Hints with HintType != 0 were set by target-dependent code.
  241. // Such targets must provide their own implementation of
  242. // TRI::getRegAllocationHints to interpret those hint types.
  243. assert(Hint.first == 0 && "Target must implement TRI::getRegAllocationHints");
  244. // Target-independent hints are either a physical or a virtual register.
  245. unsigned Phys = Hint.second;
  246. if (VRM && isVirtualRegister(Phys))
  247. Phys = VRM->getPhys(Phys);
  248. // Check that Phys is a valid hint in VirtReg's register class.
  249. if (!isPhysicalRegister(Phys))
  250. return;
  251. if (MRI.isReserved(Phys))
  252. return;
  253. // Check that Phys is in the allocation order. We shouldn't heed hints
  254. // from VirtReg's register class if they aren't in the allocation order. The
  255. // target probably has a reason for removing the register.
  256. if (std::find(Order.begin(), Order.end(), Phys) == Order.end())
  257. return;
  258. // All clear, tell the register allocator to prefer this register.
  259. Hints.push_back(Phys);
  260. }
  261. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  262. void
  263. TargetRegisterInfo::dumpReg(unsigned Reg, unsigned SubRegIndex,
  264. const TargetRegisterInfo *TRI) {
  265. dbgs() << PrintReg(Reg, TRI, SubRegIndex) << "\n";
  266. }
  267. #endif