InfoByHwMode.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //===--- InfoByHwMode.h -----------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // Classes that implement data parameterized by HW modes for instruction
  9. // selection. Currently it is ValueTypeByHwMode (parameterized ValueType),
  10. // and RegSizeInfoByHwMode (parameterized register/spill size and alignment
  11. // data).
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_UTILS_TABLEGEN_INFOBYHWMODE_H
  14. #define LLVM_UTILS_TABLEGEN_INFOBYHWMODE_H
  15. #include "CodeGenHwModes.h"
  16. #include "llvm/Support/MachineValueType.h"
  17. #include <map>
  18. #include <set>
  19. #include <string>
  20. #include <vector>
  21. namespace llvm {
  22. struct CodeGenHwModes;
  23. class Record;
  24. class raw_ostream;
  25. template <typename InfoT> struct InfoByHwMode;
  26. std::string getModeName(unsigned Mode);
  27. enum : unsigned {
  28. DefaultMode = CodeGenHwModes::DefaultMode,
  29. };
  30. template <typename InfoT>
  31. std::vector<unsigned> union_modes(const InfoByHwMode<InfoT> &A,
  32. const InfoByHwMode<InfoT> &B) {
  33. std::vector<unsigned> V;
  34. std::set<unsigned> U;
  35. for (const auto &P : A)
  36. U.insert(P.first);
  37. for (const auto &P : B)
  38. U.insert(P.first);
  39. // Make sure that the default mode is last on the list.
  40. bool HasDefault = false;
  41. for (unsigned M : U)
  42. if (M != DefaultMode)
  43. V.push_back(M);
  44. else
  45. HasDefault = true;
  46. if (HasDefault)
  47. V.push_back(DefaultMode);
  48. return V;
  49. }
  50. template <typename InfoT>
  51. struct InfoByHwMode {
  52. typedef std::map<unsigned,InfoT> MapType;
  53. typedef typename MapType::value_type PairType;
  54. typedef typename MapType::iterator iterator;
  55. typedef typename MapType::const_iterator const_iterator;
  56. InfoByHwMode() = default;
  57. InfoByHwMode(const MapType &M) : Map(M) {}
  58. LLVM_ATTRIBUTE_ALWAYS_INLINE
  59. iterator begin() { return Map.begin(); }
  60. LLVM_ATTRIBUTE_ALWAYS_INLINE
  61. iterator end() { return Map.end(); }
  62. LLVM_ATTRIBUTE_ALWAYS_INLINE
  63. const_iterator begin() const { return Map.begin(); }
  64. LLVM_ATTRIBUTE_ALWAYS_INLINE
  65. const_iterator end() const { return Map.end(); }
  66. LLVM_ATTRIBUTE_ALWAYS_INLINE
  67. bool empty() const { return Map.empty(); }
  68. LLVM_ATTRIBUTE_ALWAYS_INLINE
  69. bool hasMode(unsigned M) const { return Map.find(M) != Map.end(); }
  70. LLVM_ATTRIBUTE_ALWAYS_INLINE
  71. bool hasDefault() const { return hasMode(DefaultMode); }
  72. InfoT &get(unsigned Mode) {
  73. if (!hasMode(Mode)) {
  74. assert(hasMode(DefaultMode));
  75. Map.insert({Mode, Map.at(DefaultMode)});
  76. }
  77. return Map.at(Mode);
  78. }
  79. const InfoT &get(unsigned Mode) const {
  80. auto F = Map.find(Mode);
  81. if (Mode != DefaultMode && F == Map.end())
  82. F = Map.find(DefaultMode);
  83. assert(F != Map.end());
  84. return F->second;
  85. }
  86. LLVM_ATTRIBUTE_ALWAYS_INLINE
  87. bool isSimple() const {
  88. return Map.size() == 1 && Map.begin()->first == DefaultMode;
  89. }
  90. LLVM_ATTRIBUTE_ALWAYS_INLINE
  91. InfoT getSimple() const {
  92. assert(isSimple());
  93. return Map.begin()->second;
  94. }
  95. void makeSimple(unsigned Mode) {
  96. assert(hasMode(Mode) || hasDefault());
  97. InfoT I = get(Mode);
  98. Map.clear();
  99. Map.insert(std::make_pair(DefaultMode, I));
  100. }
  101. MapType Map;
  102. };
  103. struct ValueTypeByHwMode : public InfoByHwMode<MVT> {
  104. ValueTypeByHwMode(Record *R, const CodeGenHwModes &CGH);
  105. ValueTypeByHwMode(Record *R, MVT T);
  106. ValueTypeByHwMode(MVT T) { Map.insert({DefaultMode,T}); }
  107. ValueTypeByHwMode() = default;
  108. bool operator== (const ValueTypeByHwMode &T) const;
  109. bool operator< (const ValueTypeByHwMode &T) const;
  110. bool isValid() const {
  111. return !Map.empty();
  112. }
  113. MVT getType(unsigned Mode) const { return get(Mode); }
  114. MVT &getOrCreateTypeForMode(unsigned Mode, MVT Type);
  115. static StringRef getMVTName(MVT T);
  116. void writeToStream(raw_ostream &OS) const;
  117. void dump() const;
  118. unsigned PtrAddrSpace = std::numeric_limits<unsigned>::max();
  119. bool isPointer() const {
  120. return PtrAddrSpace != std::numeric_limits<unsigned>::max();
  121. }
  122. };
  123. ValueTypeByHwMode getValueTypeByHwMode(Record *Rec,
  124. const CodeGenHwModes &CGH);
  125. struct RegSizeInfo {
  126. unsigned RegSize;
  127. unsigned SpillSize;
  128. unsigned SpillAlignment;
  129. RegSizeInfo(Record *R, const CodeGenHwModes &CGH);
  130. RegSizeInfo() = default;
  131. bool operator< (const RegSizeInfo &I) const;
  132. bool operator== (const RegSizeInfo &I) const {
  133. return std::tie(RegSize, SpillSize, SpillAlignment) ==
  134. std::tie(I.RegSize, I.SpillSize, I.SpillAlignment);
  135. }
  136. bool operator!= (const RegSizeInfo &I) const {
  137. return !(*this == I);
  138. }
  139. bool isSubClassOf(const RegSizeInfo &I) const;
  140. void writeToStream(raw_ostream &OS) const;
  141. };
  142. struct RegSizeInfoByHwMode : public InfoByHwMode<RegSizeInfo> {
  143. RegSizeInfoByHwMode(Record *R, const CodeGenHwModes &CGH);
  144. RegSizeInfoByHwMode() = default;
  145. bool operator< (const RegSizeInfoByHwMode &VI) const;
  146. bool operator== (const RegSizeInfoByHwMode &VI) const;
  147. bool operator!= (const RegSizeInfoByHwMode &VI) const {
  148. return !(*this == VI);
  149. }
  150. bool isSubClassOf(const RegSizeInfoByHwMode &I) const;
  151. bool hasStricterSpillThan(const RegSizeInfoByHwMode &I) const;
  152. void writeToStream(raw_ostream &OS) const;
  153. };
  154. raw_ostream &operator<<(raw_ostream &OS, const ValueTypeByHwMode &T);
  155. raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfo &T);
  156. raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfoByHwMode &T);
  157. struct EncodingInfoByHwMode : public InfoByHwMode<Record*> {
  158. EncodingInfoByHwMode(Record *R, const CodeGenHwModes &CGH);
  159. EncodingInfoByHwMode() = default;
  160. };
  161. } // namespace llvm
  162. #endif // LLVM_UTILS_TABLEGEN_INFOBYHWMODE_H