InfoByHwMode.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/ADT/SmallSet.h"
  17. #include "llvm/Support/MachineValueType.h"
  18. #include <map>
  19. #include <string>
  20. namespace llvm {
  21. class Record;
  22. class raw_ostream;
  23. template <typename InfoT> struct InfoByHwMode;
  24. std::string getModeName(unsigned Mode);
  25. enum : unsigned {
  26. DefaultMode = CodeGenHwModes::DefaultMode,
  27. };
  28. template <typename InfoT>
  29. void union_modes(const InfoByHwMode<InfoT> &A,
  30. const InfoByHwMode<InfoT> &B,
  31. SmallVectorImpl<unsigned> &Modes) {
  32. SmallSet<unsigned, 4> U;
  33. for (const auto &P : A)
  34. U.insert(P.first);
  35. for (const auto &P : B)
  36. U.insert(P.first);
  37. // Make sure that the default mode is last on the list.
  38. bool HasDefault = false;
  39. for (unsigned M : U)
  40. if (M != DefaultMode)
  41. Modes.push_back(M);
  42. else
  43. HasDefault = true;
  44. if (HasDefault)
  45. Modes.push_back(DefaultMode);
  46. }
  47. template <typename InfoT>
  48. struct InfoByHwMode {
  49. typedef std::map<unsigned,InfoT> MapType;
  50. typedef typename MapType::value_type PairType;
  51. typedef typename MapType::iterator iterator;
  52. typedef typename MapType::const_iterator const_iterator;
  53. InfoByHwMode() = default;
  54. InfoByHwMode(const MapType &M) : Map(M) {}
  55. LLVM_ATTRIBUTE_ALWAYS_INLINE
  56. iterator begin() { return Map.begin(); }
  57. LLVM_ATTRIBUTE_ALWAYS_INLINE
  58. iterator end() { return Map.end(); }
  59. LLVM_ATTRIBUTE_ALWAYS_INLINE
  60. const_iterator begin() const { return Map.begin(); }
  61. LLVM_ATTRIBUTE_ALWAYS_INLINE
  62. const_iterator end() const { return Map.end(); }
  63. LLVM_ATTRIBUTE_ALWAYS_INLINE
  64. bool empty() const { return Map.empty(); }
  65. LLVM_ATTRIBUTE_ALWAYS_INLINE
  66. bool hasMode(unsigned M) const { return Map.find(M) != Map.end(); }
  67. LLVM_ATTRIBUTE_ALWAYS_INLINE
  68. bool hasDefault() const { return hasMode(DefaultMode); }
  69. InfoT &get(unsigned Mode) {
  70. if (!hasMode(Mode)) {
  71. assert(hasMode(DefaultMode));
  72. Map.insert({Mode, Map.at(DefaultMode)});
  73. }
  74. return Map.at(Mode);
  75. }
  76. const InfoT &get(unsigned Mode) const {
  77. auto F = Map.find(Mode);
  78. if (Mode != DefaultMode && F == Map.end())
  79. F = Map.find(DefaultMode);
  80. assert(F != Map.end());
  81. return F->second;
  82. }
  83. LLVM_ATTRIBUTE_ALWAYS_INLINE
  84. bool isSimple() const {
  85. return Map.size() == 1 && Map.begin()->first == DefaultMode;
  86. }
  87. LLVM_ATTRIBUTE_ALWAYS_INLINE
  88. InfoT getSimple() const {
  89. assert(isSimple());
  90. return Map.begin()->second;
  91. }
  92. void makeSimple(unsigned Mode) {
  93. assert(hasMode(Mode) || hasDefault());
  94. InfoT I = get(Mode);
  95. Map.clear();
  96. Map.insert(std::make_pair(DefaultMode, I));
  97. }
  98. protected:
  99. MapType Map;
  100. };
  101. struct ValueTypeByHwMode : public InfoByHwMode<MVT> {
  102. ValueTypeByHwMode(Record *R, const CodeGenHwModes &CGH);
  103. ValueTypeByHwMode(Record *R, MVT T);
  104. ValueTypeByHwMode(MVT T) { Map.insert({DefaultMode,T}); }
  105. ValueTypeByHwMode() = default;
  106. bool operator== (const ValueTypeByHwMode &T) const;
  107. bool operator< (const ValueTypeByHwMode &T) const;
  108. bool isValid() const {
  109. return !Map.empty();
  110. }
  111. MVT getType(unsigned Mode) const { return get(Mode); }
  112. MVT &getOrCreateTypeForMode(unsigned Mode, MVT Type);
  113. static StringRef getMVTName(MVT T);
  114. void writeToStream(raw_ostream &OS) const;
  115. void dump() const;
  116. unsigned PtrAddrSpace = std::numeric_limits<unsigned>::max();
  117. bool isPointer() const {
  118. return PtrAddrSpace != std::numeric_limits<unsigned>::max();
  119. }
  120. };
  121. ValueTypeByHwMode getValueTypeByHwMode(Record *Rec,
  122. const CodeGenHwModes &CGH);
  123. struct RegSizeInfo {
  124. unsigned RegSize;
  125. unsigned SpillSize;
  126. unsigned SpillAlignment;
  127. RegSizeInfo(Record *R, const CodeGenHwModes &CGH);
  128. RegSizeInfo() = default;
  129. bool operator< (const RegSizeInfo &I) const;
  130. bool operator== (const RegSizeInfo &I) const {
  131. return std::tie(RegSize, SpillSize, SpillAlignment) ==
  132. std::tie(I.RegSize, I.SpillSize, I.SpillAlignment);
  133. }
  134. bool operator!= (const RegSizeInfo &I) const {
  135. return !(*this == I);
  136. }
  137. bool isSubClassOf(const RegSizeInfo &I) const;
  138. void writeToStream(raw_ostream &OS) const;
  139. };
  140. struct RegSizeInfoByHwMode : public InfoByHwMode<RegSizeInfo> {
  141. RegSizeInfoByHwMode(Record *R, const CodeGenHwModes &CGH);
  142. RegSizeInfoByHwMode() = default;
  143. bool operator< (const RegSizeInfoByHwMode &VI) const;
  144. bool operator== (const RegSizeInfoByHwMode &VI) const;
  145. bool operator!= (const RegSizeInfoByHwMode &VI) const {
  146. return !(*this == VI);
  147. }
  148. bool isSubClassOf(const RegSizeInfoByHwMode &I) const;
  149. bool hasStricterSpillThan(const RegSizeInfoByHwMode &I) const;
  150. void writeToStream(raw_ostream &OS) const;
  151. void insertRegSizeForMode(unsigned Mode, RegSizeInfo Info) {
  152. Map.insert(std::make_pair(Mode, Info));
  153. }
  154. };
  155. raw_ostream &operator<<(raw_ostream &OS, const ValueTypeByHwMode &T);
  156. raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfo &T);
  157. raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfoByHwMode &T);
  158. struct EncodingInfoByHwMode : public InfoByHwMode<Record*> {
  159. EncodingInfoByHwMode(Record *R, const CodeGenHwModes &CGH);
  160. EncodingInfoByHwMode() = default;
  161. };
  162. } // namespace llvm
  163. #endif // LLVM_UTILS_TABLEGEN_INFOBYHWMODE_H