InfoByHwMode.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //===--- InfoByHwMode.cpp -------------------------------------------------===//
  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. #include "CodeGenTarget.h"
  14. #include "InfoByHwMode.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/Twine.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include <set>
  20. #include <string>
  21. using namespace llvm;
  22. std::string llvm::getModeName(unsigned Mode) {
  23. if (Mode == DefaultMode)
  24. return "*";
  25. return (Twine('m') + Twine(Mode)).str();
  26. }
  27. ValueTypeByHwMode::ValueTypeByHwMode(Record *R, const CodeGenHwModes &CGH) {
  28. const HwModeSelect &MS = CGH.getHwModeSelect(R);
  29. for (const HwModeSelect::PairType &P : MS.Items) {
  30. auto I = Map.insert({P.first, MVT(llvm::getValueType(P.second))});
  31. assert(I.second && "Duplicate entry?");
  32. (void)I;
  33. }
  34. }
  35. ValueTypeByHwMode::ValueTypeByHwMode(Record *R, MVT T) : ValueTypeByHwMode(T) {
  36. if (R->isSubClassOf("PtrValueType"))
  37. PtrAddrSpace = R->getValueAsInt("AddrSpace");
  38. }
  39. bool ValueTypeByHwMode::operator== (const ValueTypeByHwMode &T) const {
  40. assert(isValid() && T.isValid() && "Invalid type in assignment");
  41. bool Simple = isSimple();
  42. if (Simple != T.isSimple())
  43. return false;
  44. if (Simple)
  45. return getSimple() == T.getSimple();
  46. return Map == T.Map;
  47. }
  48. bool ValueTypeByHwMode::operator< (const ValueTypeByHwMode &T) const {
  49. assert(isValid() && T.isValid() && "Invalid type in comparison");
  50. // Default order for maps.
  51. return Map < T.Map;
  52. }
  53. MVT &ValueTypeByHwMode::getOrCreateTypeForMode(unsigned Mode, MVT Type) {
  54. auto F = Map.find(Mode);
  55. if (F != Map.end())
  56. return F->second;
  57. // If Mode is not in the map, look up the default mode. If it exists,
  58. // make a copy of it for Mode and return it.
  59. auto D = Map.find(DefaultMode);
  60. if (D != Map.end())
  61. return Map.insert(std::make_pair(Mode, D->second)).first->second;
  62. // If default mode is not present either, use provided Type.
  63. return Map.insert(std::make_pair(Mode, Type)).first->second;
  64. }
  65. StringRef ValueTypeByHwMode::getMVTName(MVT T) {
  66. StringRef N = llvm::getEnumName(T.SimpleTy);
  67. N.consume_front("MVT::");
  68. return N;
  69. }
  70. void ValueTypeByHwMode::writeToStream(raw_ostream &OS) const {
  71. if (isSimple()) {
  72. OS << getMVTName(getSimple());
  73. return;
  74. }
  75. std::vector<const PairType*> Pairs;
  76. for (const auto &P : Map)
  77. Pairs.push_back(&P);
  78. llvm::sort(Pairs, deref<std::less<PairType>>());
  79. OS << '{';
  80. for (unsigned i = 0, e = Pairs.size(); i != e; ++i) {
  81. const PairType *P = Pairs[i];
  82. OS << '(' << getModeName(P->first)
  83. << ':' << getMVTName(P->second).str() << ')';
  84. if (i != e-1)
  85. OS << ',';
  86. }
  87. OS << '}';
  88. }
  89. LLVM_DUMP_METHOD
  90. void ValueTypeByHwMode::dump() const {
  91. dbgs() << *this << '\n';
  92. }
  93. ValueTypeByHwMode llvm::getValueTypeByHwMode(Record *Rec,
  94. const CodeGenHwModes &CGH) {
  95. #ifndef NDEBUG
  96. if (!Rec->isSubClassOf("ValueType"))
  97. Rec->dump();
  98. #endif
  99. assert(Rec->isSubClassOf("ValueType") &&
  100. "Record must be derived from ValueType");
  101. if (Rec->isSubClassOf("HwModeSelect"))
  102. return ValueTypeByHwMode(Rec, CGH);
  103. return ValueTypeByHwMode(Rec, llvm::getValueType(Rec));
  104. }
  105. RegSizeInfo::RegSizeInfo(Record *R, const CodeGenHwModes &CGH) {
  106. RegSize = R->getValueAsInt("RegSize");
  107. SpillSize = R->getValueAsInt("SpillSize");
  108. SpillAlignment = R->getValueAsInt("SpillAlignment");
  109. }
  110. bool RegSizeInfo::operator< (const RegSizeInfo &I) const {
  111. return std::tie(RegSize, SpillSize, SpillAlignment) <
  112. std::tie(I.RegSize, I.SpillSize, I.SpillAlignment);
  113. }
  114. bool RegSizeInfo::isSubClassOf(const RegSizeInfo &I) const {
  115. return RegSize <= I.RegSize &&
  116. SpillAlignment && I.SpillAlignment % SpillAlignment == 0 &&
  117. SpillSize <= I.SpillSize;
  118. }
  119. void RegSizeInfo::writeToStream(raw_ostream &OS) const {
  120. OS << "[R=" << RegSize << ",S=" << SpillSize
  121. << ",A=" << SpillAlignment << ']';
  122. }
  123. RegSizeInfoByHwMode::RegSizeInfoByHwMode(Record *R,
  124. const CodeGenHwModes &CGH) {
  125. const HwModeSelect &MS = CGH.getHwModeSelect(R);
  126. for (const HwModeSelect::PairType &P : MS.Items) {
  127. auto I = Map.insert({P.first, RegSizeInfo(P.second, CGH)});
  128. assert(I.second && "Duplicate entry?");
  129. (void)I;
  130. }
  131. }
  132. bool RegSizeInfoByHwMode::operator< (const RegSizeInfoByHwMode &I) const {
  133. unsigned M0 = Map.begin()->first;
  134. return get(M0) < I.get(M0);
  135. }
  136. bool RegSizeInfoByHwMode::operator== (const RegSizeInfoByHwMode &I) const {
  137. unsigned M0 = Map.begin()->first;
  138. return get(M0) == I.get(M0);
  139. }
  140. bool RegSizeInfoByHwMode::isSubClassOf(const RegSizeInfoByHwMode &I) const {
  141. unsigned M0 = Map.begin()->first;
  142. return get(M0).isSubClassOf(I.get(M0));
  143. }
  144. bool RegSizeInfoByHwMode::hasStricterSpillThan(const RegSizeInfoByHwMode &I)
  145. const {
  146. unsigned M0 = Map.begin()->first;
  147. const RegSizeInfo &A0 = get(M0);
  148. const RegSizeInfo &B0 = I.get(M0);
  149. return std::tie(A0.SpillSize, A0.SpillAlignment) >
  150. std::tie(B0.SpillSize, B0.SpillAlignment);
  151. }
  152. void RegSizeInfoByHwMode::writeToStream(raw_ostream &OS) const {
  153. typedef typename decltype(Map)::value_type PairType;
  154. std::vector<const PairType*> Pairs;
  155. for (const auto &P : Map)
  156. Pairs.push_back(&P);
  157. llvm::sort(Pairs, deref<std::less<PairType>>());
  158. OS << '{';
  159. for (unsigned i = 0, e = Pairs.size(); i != e; ++i) {
  160. const PairType *P = Pairs[i];
  161. OS << '(' << getModeName(P->first) << ':' << P->second << ')';
  162. if (i != e-1)
  163. OS << ',';
  164. }
  165. OS << '}';
  166. }
  167. EncodingInfoByHwMode::EncodingInfoByHwMode(Record *R, const CodeGenHwModes &CGH) {
  168. const HwModeSelect &MS = CGH.getHwModeSelect(R);
  169. for (const HwModeSelect::PairType &P : MS.Items) {
  170. assert(P.second && P.second->isSubClassOf("InstructionEncoding") &&
  171. "Encoding must subclass InstructionEncoding");
  172. auto I = Map.insert({P.first, P.second});
  173. assert(I.second && "Duplicate entry?");
  174. (void)I;
  175. }
  176. }
  177. namespace llvm {
  178. raw_ostream &operator<<(raw_ostream &OS, const ValueTypeByHwMode &T) {
  179. T.writeToStream(OS);
  180. return OS;
  181. }
  182. raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfo &T) {
  183. T.writeToStream(OS);
  184. return OS;
  185. }
  186. raw_ostream &operator<<(raw_ostream &OS, const RegSizeInfoByHwMode &T) {
  187. T.writeToStream(OS);
  188. return OS;
  189. }
  190. }