RDFRegisters.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- RDFRegisters.h -------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_RDFREGISTERS_H
  14. #define LLVM_CODEGEN_RDFREGISTERS_H
  15. #include "llvm/ADT/BitVector.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/CodeGen/TargetRegisterInfo.h"
  18. #include "llvm/MC/LaneBitmask.h"
  19. #include <cassert>
  20. #include <cstdint>
  21. #include <map>
  22. #include <set>
  23. #include <vector>
  24. namespace llvm {
  25. class MachineFunction;
  26. class raw_ostream;
  27. namespace rdf {
  28. using RegisterId = uint32_t;
  29. // Template class for a map translating uint32_t into arbitrary types.
  30. // The map will act like an indexed set: upon insertion of a new object,
  31. // it will automatically assign a new index to it. Index of 0 is treated
  32. // as invalid and is never allocated.
  33. template <typename T, unsigned N = 32>
  34. struct IndexedSet {
  35. IndexedSet() { Map.reserve(N); }
  36. T get(uint32_t Idx) const {
  37. // Index Idx corresponds to Map[Idx-1].
  38. assert(Idx != 0 && !Map.empty() && Idx-1 < Map.size());
  39. return Map[Idx-1];
  40. }
  41. uint32_t insert(T Val) {
  42. // Linear search.
  43. auto F = llvm::find(Map, Val);
  44. if (F != Map.end())
  45. return F - Map.begin() + 1;
  46. Map.push_back(Val);
  47. return Map.size(); // Return actual_index + 1.
  48. }
  49. uint32_t find(T Val) const {
  50. auto F = llvm::find(Map, Val);
  51. assert(F != Map.end());
  52. return F - Map.begin() + 1;
  53. }
  54. uint32_t size() const { return Map.size(); }
  55. using const_iterator = typename std::vector<T>::const_iterator;
  56. const_iterator begin() const { return Map.begin(); }
  57. const_iterator end() const { return Map.end(); }
  58. private:
  59. std::vector<T> Map;
  60. };
  61. struct RegisterRef {
  62. RegisterId Reg = 0;
  63. LaneBitmask Mask = LaneBitmask::getNone();
  64. RegisterRef() = default;
  65. explicit RegisterRef(RegisterId R, LaneBitmask M = LaneBitmask::getAll())
  66. : Reg(R), Mask(R != 0 ? M : LaneBitmask::getNone()) {}
  67. operator bool() const {
  68. return Reg != 0 && Mask.any();
  69. }
  70. bool operator== (const RegisterRef &RR) const {
  71. return Reg == RR.Reg && Mask == RR.Mask;
  72. }
  73. bool operator!= (const RegisterRef &RR) const {
  74. return !operator==(RR);
  75. }
  76. bool operator< (const RegisterRef &RR) const {
  77. return Reg < RR.Reg || (Reg == RR.Reg && Mask < RR.Mask);
  78. }
  79. size_t hash() const {
  80. return std::hash<RegisterId>{}(Reg) ^
  81. std::hash<LaneBitmask::Type>{}(Mask.getAsInteger());
  82. }
  83. };
  84. struct PhysicalRegisterInfo {
  85. PhysicalRegisterInfo(const TargetRegisterInfo &tri,
  86. const MachineFunction &mf);
  87. static bool isRegMaskId(RegisterId R) {
  88. return Register::isStackSlot(R);
  89. }
  90. RegisterId getRegMaskId(const uint32_t *RM) const {
  91. return Register::index2StackSlot(RegMasks.find(RM));
  92. }
  93. const uint32_t *getRegMaskBits(RegisterId R) const {
  94. return RegMasks.get(Register::stackSlot2Index(R));
  95. }
  96. bool alias(RegisterRef RA, RegisterRef RB) const {
  97. if (!isRegMaskId(RA.Reg))
  98. return !isRegMaskId(RB.Reg) ? aliasRR(RA, RB) : aliasRM(RA, RB);
  99. return !isRegMaskId(RB.Reg) ? aliasRM(RB, RA) : aliasMM(RA, RB);
  100. }
  101. std::set<RegisterId> getAliasSet(RegisterId Reg) const;
  102. RegisterRef getRefForUnit(uint32_t U) const {
  103. return RegisterRef(UnitInfos[U].Reg, UnitInfos[U].Mask);
  104. }
  105. const BitVector &getMaskUnits(RegisterId MaskId) const {
  106. return MaskInfos[Register::stackSlot2Index(MaskId)].Units;
  107. }
  108. const BitVector &getUnitAliases(uint32_t U) const {
  109. return AliasInfos[U].Regs;
  110. }
  111. RegisterRef mapTo(RegisterRef RR, unsigned R) const;
  112. const TargetRegisterInfo &getTRI() const { return TRI; }
  113. private:
  114. struct RegInfo {
  115. const TargetRegisterClass *RegClass = nullptr;
  116. };
  117. struct UnitInfo {
  118. RegisterId Reg = 0;
  119. LaneBitmask Mask;
  120. };
  121. struct MaskInfo {
  122. BitVector Units;
  123. };
  124. struct AliasInfo {
  125. BitVector Regs;
  126. };
  127. const TargetRegisterInfo &TRI;
  128. IndexedSet<const uint32_t*> RegMasks;
  129. std::vector<RegInfo> RegInfos;
  130. std::vector<UnitInfo> UnitInfos;
  131. std::vector<MaskInfo> MaskInfos;
  132. std::vector<AliasInfo> AliasInfos;
  133. bool aliasRR(RegisterRef RA, RegisterRef RB) const;
  134. bool aliasRM(RegisterRef RR, RegisterRef RM) const;
  135. bool aliasMM(RegisterRef RM, RegisterRef RN) const;
  136. };
  137. struct RegisterAggr {
  138. RegisterAggr(const PhysicalRegisterInfo &pri)
  139. : Units(pri.getTRI().getNumRegUnits()), PRI(pri) {}
  140. RegisterAggr(const RegisterAggr &RG) = default;
  141. unsigned count() const { return Units.count(); }
  142. bool empty() const { return Units.none(); }
  143. bool hasAliasOf(RegisterRef RR) const;
  144. bool hasCoverOf(RegisterRef RR) const;
  145. bool operator==(const RegisterAggr &A) const {
  146. return DenseMapInfo<BitVector>::isEqual(Units, A.Units);
  147. }
  148. static bool isCoverOf(RegisterRef RA, RegisterRef RB,
  149. const PhysicalRegisterInfo &PRI) {
  150. return RegisterAggr(PRI).insert(RA).hasCoverOf(RB);
  151. }
  152. RegisterAggr &insert(RegisterRef RR);
  153. RegisterAggr &insert(const RegisterAggr &RG);
  154. RegisterAggr &intersect(RegisterRef RR);
  155. RegisterAggr &intersect(const RegisterAggr &RG);
  156. RegisterAggr &clear(RegisterRef RR);
  157. RegisterAggr &clear(const RegisterAggr &RG);
  158. RegisterRef intersectWith(RegisterRef RR) const;
  159. RegisterRef clearIn(RegisterRef RR) const;
  160. RegisterRef makeRegRef() const;
  161. size_t hash() const {
  162. return DenseMapInfo<BitVector>::getHashValue(Units);
  163. }
  164. void print(raw_ostream &OS) const;
  165. struct rr_iterator {
  166. using MapType = std::map<RegisterId, LaneBitmask>;
  167. private:
  168. MapType Masks;
  169. MapType::iterator Pos;
  170. unsigned Index;
  171. const RegisterAggr *Owner;
  172. public:
  173. rr_iterator(const RegisterAggr &RG, bool End);
  174. RegisterRef operator*() const {
  175. return RegisterRef(Pos->first, Pos->second);
  176. }
  177. rr_iterator &operator++() {
  178. ++Pos;
  179. ++Index;
  180. return *this;
  181. }
  182. bool operator==(const rr_iterator &I) const {
  183. assert(Owner == I.Owner);
  184. (void)Owner;
  185. return Index == I.Index;
  186. }
  187. bool operator!=(const rr_iterator &I) const {
  188. return !(*this == I);
  189. }
  190. };
  191. rr_iterator rr_begin() const {
  192. return rr_iterator(*this, false);
  193. }
  194. rr_iterator rr_end() const {
  195. return rr_iterator(*this, true);
  196. }
  197. private:
  198. BitVector Units;
  199. const PhysicalRegisterInfo &PRI;
  200. };
  201. // Optionally print the lane mask, if it is not ~0.
  202. struct PrintLaneMaskOpt {
  203. PrintLaneMaskOpt(LaneBitmask M) : Mask(M) {}
  204. LaneBitmask Mask;
  205. };
  206. raw_ostream &operator<< (raw_ostream &OS, const PrintLaneMaskOpt &P);
  207. raw_ostream &operator<< (raw_ostream &OS, const RegisterAggr &A);
  208. } // end namespace rdf
  209. } // end namespace llvm
  210. namespace std {
  211. template <> struct hash<llvm::rdf::RegisterRef> {
  212. size_t operator()(llvm::rdf::RegisterRef A) const {
  213. return A.hash();
  214. }
  215. };
  216. template <> struct hash<llvm::rdf::RegisterAggr> {
  217. size_t operator()(const llvm::rdf::RegisterAggr &A) const {
  218. return A.hash();
  219. }
  220. };
  221. template <> struct equal_to<llvm::rdf::RegisterAggr> {
  222. bool operator()(const llvm::rdf::RegisterAggr &A,
  223. const llvm::rdf::RegisterAggr &B) const {
  224. return A == B;
  225. }
  226. };
  227. }
  228. #endif // LLVM_CODEGEN_RDFREGISTERS_H
  229. #ifdef __GNUC__
  230. #pragma GCC diagnostic pop
  231. #endif