SubtargetFeature.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //===- SubtargetFeature.cpp - CPU characteristics 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. /// \file Implements the SubtargetFeature interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/MC/SubtargetFeature.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/ADT/Triple.h"
  19. #include "llvm/Config/llvm-config.h"
  20. #include "llvm/Support/Compiler.h"
  21. #include "llvm/Support/Debug.h"
  22. #include "llvm/Support/Format.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include <algorithm>
  25. #include <cassert>
  26. #include <cstddef>
  27. #include <cstring>
  28. #include <iterator>
  29. #include <string>
  30. #include <vector>
  31. using namespace llvm;
  32. /// Determine if a feature has a flag; '+' or '-'
  33. static inline bool hasFlag(StringRef Feature) {
  34. assert(!Feature.empty() && "Empty string");
  35. // Get first character
  36. char Ch = Feature[0];
  37. // Check if first character is '+' or '-' flag
  38. return Ch == '+' || Ch =='-';
  39. }
  40. /// Return string stripped of flag.
  41. static inline std::string StripFlag(StringRef Feature) {
  42. return hasFlag(Feature) ? Feature.substr(1) : Feature;
  43. }
  44. /// Return true if enable flag; '+'.
  45. static inline bool isEnabled(StringRef Feature) {
  46. assert(!Feature.empty() && "Empty string");
  47. // Get first character
  48. char Ch = Feature[0];
  49. // Check if first character is '+' for enabled
  50. return Ch == '+';
  51. }
  52. /// Splits a string of comma separated items in to a vector of strings.
  53. static void Split(std::vector<std::string> &V, StringRef S) {
  54. SmallVector<StringRef, 3> Tmp;
  55. S.split(Tmp, ',', -1, false /* KeepEmpty */);
  56. V.assign(Tmp.begin(), Tmp.end());
  57. }
  58. void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
  59. // Don't add empty features.
  60. if (!String.empty())
  61. // Convert to lowercase, prepend flag if we don't already have a flag.
  62. Features.push_back(hasFlag(String) ? String.lower()
  63. : (Enable ? "+" : "-") + String.lower());
  64. }
  65. /// Find KV in array using binary search.
  66. static const SubtargetFeatureKV *Find(StringRef S,
  67. ArrayRef<SubtargetFeatureKV> A) {
  68. // Binary search the array
  69. auto F = std::lower_bound(A.begin(), A.end(), S);
  70. // If not found then return NULL
  71. if (F == A.end() || StringRef(F->Key) != S) return nullptr;
  72. // Return the found array item
  73. return F;
  74. }
  75. /// Return the length of the longest entry in the table.
  76. static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
  77. size_t MaxLen = 0;
  78. for (auto &I : Table)
  79. MaxLen = std::max(MaxLen, std::strlen(I.Key));
  80. return MaxLen;
  81. }
  82. /// Display help for feature choices.
  83. static void Help(ArrayRef<SubtargetFeatureKV> CPUTable,
  84. ArrayRef<SubtargetFeatureKV> FeatTable) {
  85. // Determine the length of the longest CPU and Feature entries.
  86. unsigned MaxCPULen = getLongestEntryLength(CPUTable);
  87. unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
  88. // Print the CPU table.
  89. errs() << "Available CPUs for this target:\n\n";
  90. for (auto &CPU : CPUTable)
  91. errs() << format(" %-*s - %s.\n", MaxCPULen, CPU.Key, CPU.Desc);
  92. errs() << '\n';
  93. // Print the Feature table.
  94. errs() << "Available features for this target:\n\n";
  95. for (auto &Feature : FeatTable)
  96. errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
  97. errs() << '\n';
  98. errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
  99. "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
  100. }
  101. SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
  102. // Break up string into separate features
  103. Split(Features, Initial);
  104. }
  105. std::string SubtargetFeatures::getString() const {
  106. return join(Features.begin(), Features.end(), ",");
  107. }
  108. /// For each feature that is (transitively) implied by this feature, set it.
  109. static
  110. void SetImpliedBits(FeatureBitset &Bits, const SubtargetFeatureKV &FeatureEntry,
  111. ArrayRef<SubtargetFeatureKV> FeatureTable) {
  112. for (const SubtargetFeatureKV &FE : FeatureTable) {
  113. if (FeatureEntry.Value == FE.Value) continue;
  114. if ((FeatureEntry.Implies & FE.Value).any()) {
  115. Bits |= FE.Value;
  116. SetImpliedBits(Bits, FE, FeatureTable);
  117. }
  118. }
  119. }
  120. /// For each feature that (transitively) implies this feature, clear it.
  121. static
  122. void ClearImpliedBits(FeatureBitset &Bits,
  123. const SubtargetFeatureKV &FeatureEntry,
  124. ArrayRef<SubtargetFeatureKV> FeatureTable) {
  125. for (const SubtargetFeatureKV &FE : FeatureTable) {
  126. if (FeatureEntry.Value == FE.Value) continue;
  127. if ((FE.Implies & FeatureEntry.Value).any()) {
  128. Bits &= ~FE.Value;
  129. ClearImpliedBits(Bits, FE, FeatureTable);
  130. }
  131. }
  132. }
  133. void
  134. SubtargetFeatures::ToggleFeature(FeatureBitset &Bits, StringRef Feature,
  135. ArrayRef<SubtargetFeatureKV> FeatureTable) {
  136. // Find feature in table.
  137. const SubtargetFeatureKV *FeatureEntry =
  138. Find(StripFlag(Feature), FeatureTable);
  139. // If there is a match
  140. if (FeatureEntry) {
  141. if ((Bits & FeatureEntry->Value) == FeatureEntry->Value) {
  142. Bits &= ~FeatureEntry->Value;
  143. // For each feature that implies this, clear it.
  144. ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
  145. } else {
  146. Bits |= FeatureEntry->Value;
  147. // For each feature that this implies, set it.
  148. SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
  149. }
  150. } else {
  151. errs() << "'" << Feature << "' is not a recognized feature for this target"
  152. << " (ignoring feature)\n";
  153. }
  154. }
  155. void SubtargetFeatures::ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
  156. ArrayRef<SubtargetFeatureKV> FeatureTable) {
  157. assert(hasFlag(Feature));
  158. // Find feature in table.
  159. const SubtargetFeatureKV *FeatureEntry =
  160. Find(StripFlag(Feature), FeatureTable);
  161. // If there is a match
  162. if (FeatureEntry) {
  163. // Enable/disable feature in bits
  164. if (isEnabled(Feature)) {
  165. Bits |= FeatureEntry->Value;
  166. // For each feature that this implies, set it.
  167. SetImpliedBits(Bits, *FeatureEntry, FeatureTable);
  168. } else {
  169. Bits &= ~FeatureEntry->Value;
  170. // For each feature that implies this, clear it.
  171. ClearImpliedBits(Bits, *FeatureEntry, FeatureTable);
  172. }
  173. } else {
  174. errs() << "'" << Feature << "' is not a recognized feature for this target"
  175. << " (ignoring feature)\n";
  176. }
  177. }
  178. FeatureBitset
  179. SubtargetFeatures::getFeatureBits(StringRef CPU,
  180. ArrayRef<SubtargetFeatureKV> CPUTable,
  181. ArrayRef<SubtargetFeatureKV> FeatureTable) {
  182. if (CPUTable.empty() || FeatureTable.empty())
  183. return FeatureBitset();
  184. assert(std::is_sorted(std::begin(CPUTable), std::end(CPUTable)) &&
  185. "CPU table is not sorted");
  186. assert(std::is_sorted(std::begin(FeatureTable), std::end(FeatureTable)) &&
  187. "CPU features table is not sorted");
  188. // Resulting bits
  189. FeatureBitset Bits;
  190. // Check if help is needed
  191. if (CPU == "help")
  192. Help(CPUTable, FeatureTable);
  193. // Find CPU entry if CPU name is specified.
  194. else if (!CPU.empty()) {
  195. const SubtargetFeatureKV *CPUEntry = Find(CPU, CPUTable);
  196. // If there is a match
  197. if (CPUEntry) {
  198. // Set base feature bits
  199. Bits = CPUEntry->Value;
  200. // Set the feature implied by this CPU feature, if any.
  201. for (auto &FE : FeatureTable) {
  202. if ((CPUEntry->Value & FE.Value).any())
  203. SetImpliedBits(Bits, FE, FeatureTable);
  204. }
  205. } else {
  206. errs() << "'" << CPU << "' is not a recognized processor for this target"
  207. << " (ignoring processor)\n";
  208. }
  209. }
  210. // Iterate through each feature
  211. for (const std::string &Feature : Features) {
  212. // Check for help
  213. if (Feature == "+help")
  214. Help(CPUTable, FeatureTable);
  215. ApplyFeatureFlag(Bits, Feature, FeatureTable);
  216. }
  217. return Bits;
  218. }
  219. void SubtargetFeatures::print(raw_ostream &OS) const {
  220. for (auto &F : Features)
  221. OS << F << " ";
  222. OS << "\n";
  223. }
  224. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  225. LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
  226. print(dbgs());
  227. }
  228. #endif
  229. void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
  230. // FIXME: This is an inelegant way of specifying the features of a
  231. // subtarget. It would be better if we could encode this information
  232. // into the IR. See <rdar://5972456>.
  233. if (Triple.getVendor() == Triple::Apple) {
  234. if (Triple.getArch() == Triple::ppc) {
  235. // powerpc-apple-*
  236. AddFeature("altivec");
  237. } else if (Triple.getArch() == Triple::ppc64) {
  238. // powerpc64-apple-*
  239. AddFeature("64bit");
  240. AddFeature("altivec");
  241. }
  242. }
  243. }