MCSubtargetInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //===- MCSubtargetInfo.cpp - Subtarget Information ------------------------===//
  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. #include "llvm/MC/MCSubtargetInfo.h"
  9. #include "llvm/ADT/ArrayRef.h"
  10. #include "llvm/ADT/StringRef.h"
  11. #include "llvm/MC/MCInstrItineraries.h"
  12. #include "llvm/MC/MCSchedule.h"
  13. #include "llvm/MC/SubtargetFeature.h"
  14. #include "llvm/Support/Format.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <algorithm>
  17. #include <cassert>
  18. #include <cstring>
  19. using namespace llvm;
  20. /// Find KV in array using binary search.
  21. template <typename T>
  22. static const T *Find(StringRef S, ArrayRef<T> A) {
  23. // Binary search the array
  24. auto F = llvm::lower_bound(A, S);
  25. // If not found then return NULL
  26. if (F == A.end() || StringRef(F->Key) != S) return nullptr;
  27. // Return the found array item
  28. return F;
  29. }
  30. /// For each feature that is (transitively) implied by this feature, set it.
  31. static
  32. void SetImpliedBits(FeatureBitset &Bits, const FeatureBitset &Implies,
  33. ArrayRef<SubtargetFeatureKV> FeatureTable) {
  34. // OR the Implies bits in outside the loop. This allows the Implies for CPUs
  35. // which might imply features not in FeatureTable to use this.
  36. Bits |= Implies;
  37. for (const SubtargetFeatureKV &FE : FeatureTable)
  38. if (Implies.test(FE.Value))
  39. SetImpliedBits(Bits, FE.Implies.getAsBitset(), FeatureTable);
  40. }
  41. /// For each feature that (transitively) implies this feature, clear it.
  42. static
  43. void ClearImpliedBits(FeatureBitset &Bits, unsigned Value,
  44. ArrayRef<SubtargetFeatureKV> FeatureTable) {
  45. for (const SubtargetFeatureKV &FE : FeatureTable) {
  46. if (FE.Implies.getAsBitset().test(Value)) {
  47. Bits.reset(FE.Value);
  48. ClearImpliedBits(Bits, FE.Value, FeatureTable);
  49. }
  50. }
  51. }
  52. static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
  53. ArrayRef<SubtargetFeatureKV> FeatureTable) {
  54. assert(SubtargetFeatures::hasFlag(Feature) &&
  55. "Feature flags should start with '+' or '-'");
  56. // Find feature in table.
  57. const SubtargetFeatureKV *FeatureEntry =
  58. Find(SubtargetFeatures::StripFlag(Feature), FeatureTable);
  59. // If there is a match
  60. if (FeatureEntry) {
  61. // Enable/disable feature in bits
  62. if (SubtargetFeatures::isEnabled(Feature)) {
  63. Bits.set(FeatureEntry->Value);
  64. // For each feature that this implies, set it.
  65. SetImpliedBits(Bits, FeatureEntry->Implies.getAsBitset(), FeatureTable);
  66. } else {
  67. Bits.reset(FeatureEntry->Value);
  68. // For each feature that implies this, clear it.
  69. ClearImpliedBits(Bits, FeatureEntry->Value, FeatureTable);
  70. }
  71. } else {
  72. errs() << "'" << Feature << "' is not a recognized feature for this target"
  73. << " (ignoring feature)\n";
  74. }
  75. }
  76. /// Return the length of the longest entry in the table.
  77. template <typename T>
  78. static size_t getLongestEntryLength(ArrayRef<T> Table) {
  79. size_t MaxLen = 0;
  80. for (auto &I : Table)
  81. MaxLen = std::max(MaxLen, std::strlen(I.Key));
  82. return MaxLen;
  83. }
  84. /// Display help for feature and mcpu choices.
  85. static void Help(ArrayRef<SubtargetSubTypeKV> CPUTable,
  86. ArrayRef<SubtargetFeatureKV> FeatTable) {
  87. // the static variable ensures that the help information only gets
  88. // printed once even though a target machine creates multiple subtargets
  89. static bool PrintOnce = false;
  90. if (PrintOnce) {
  91. return;
  92. }
  93. // Determine the length of the longest CPU and Feature entries.
  94. unsigned MaxCPULen = getLongestEntryLength(CPUTable);
  95. unsigned MaxFeatLen = getLongestEntryLength(FeatTable);
  96. // Print the CPU table.
  97. errs() << "Available CPUs for this target:\n\n";
  98. for (auto &CPU : CPUTable)
  99. errs() << format(" %-*s - Select the %s processor.\n", MaxCPULen, CPU.Key,
  100. CPU.Key);
  101. errs() << '\n';
  102. // Print the Feature table.
  103. errs() << "Available features for this target:\n\n";
  104. for (auto &Feature : FeatTable)
  105. errs() << format(" %-*s - %s.\n", MaxFeatLen, Feature.Key, Feature.Desc);
  106. errs() << '\n';
  107. errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
  108. "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
  109. PrintOnce = true;
  110. }
  111. /// Display help for mcpu choices only
  112. static void cpuHelp(ArrayRef<SubtargetSubTypeKV> CPUTable) {
  113. // the static variable ensures that the help information only gets
  114. // printed once even though a target machine creates multiple subtargets
  115. static bool PrintOnce = false;
  116. if (PrintOnce) {
  117. return;
  118. }
  119. // Print the CPU table.
  120. errs() << "Available CPUs for this target:\n\n";
  121. for (auto &CPU : CPUTable)
  122. errs() << "\t" << CPU.Key << "\n";
  123. errs() << '\n';
  124. errs() << "Use -mcpu or -mtune to specify the target's processor.\n"
  125. "For example, clang --target=aarch64-unknown-linux-gui "
  126. "-mcpu=cortex-a35\n";
  127. PrintOnce = true;
  128. }
  129. static FeatureBitset getFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS,
  130. ArrayRef<SubtargetSubTypeKV> ProcDesc,
  131. ArrayRef<SubtargetFeatureKV> ProcFeatures) {
  132. SubtargetFeatures Features(FS);
  133. if (ProcDesc.empty() || ProcFeatures.empty())
  134. return FeatureBitset();
  135. assert(llvm::is_sorted(ProcDesc) && "CPU table is not sorted");
  136. assert(llvm::is_sorted(ProcFeatures) && "CPU features table is not sorted");
  137. // Resulting bits
  138. FeatureBitset Bits;
  139. // Check if help is needed
  140. if (CPU == "help")
  141. Help(ProcDesc, ProcFeatures);
  142. // Find CPU entry if CPU name is specified.
  143. else if (!CPU.empty()) {
  144. const SubtargetSubTypeKV *CPUEntry = Find(CPU, ProcDesc);
  145. // If there is a match
  146. if (CPUEntry) {
  147. // Set the features implied by this CPU feature, if any.
  148. SetImpliedBits(Bits, CPUEntry->Implies.getAsBitset(), ProcFeatures);
  149. } else {
  150. errs() << "'" << CPU << "' is not a recognized processor for this target"
  151. << " (ignoring processor)\n";
  152. }
  153. }
  154. if (!TuneCPU.empty()) {
  155. const SubtargetSubTypeKV *CPUEntry = Find(TuneCPU, ProcDesc);
  156. // If there is a match
  157. if (CPUEntry) {
  158. // Set the features implied by this CPU feature, if any.
  159. SetImpliedBits(Bits, CPUEntry->TuneImplies.getAsBitset(), ProcFeatures);
  160. } else if (TuneCPU != CPU) {
  161. errs() << "'" << TuneCPU << "' is not a recognized processor for this "
  162. << "target (ignoring processor)\n";
  163. }
  164. }
  165. // Iterate through each feature
  166. for (const std::string &Feature : Features.getFeatures()) {
  167. // Check for help
  168. if (Feature == "+help")
  169. Help(ProcDesc, ProcFeatures);
  170. else if (Feature == "+cpuhelp")
  171. cpuHelp(ProcDesc);
  172. else
  173. ApplyFeatureFlag(Bits, Feature, ProcFeatures);
  174. }
  175. return Bits;
  176. }
  177. void MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef TuneCPU,
  178. StringRef FS) {
  179. FeatureBits = getFeatures(CPU, TuneCPU, FS, ProcDesc, ProcFeatures);
  180. FeatureString = std::string(FS);
  181. if (!TuneCPU.empty())
  182. CPUSchedModel = &getSchedModelForCPU(TuneCPU);
  183. else
  184. CPUSchedModel = &MCSchedModel::GetDefaultSchedModel();
  185. }
  186. void MCSubtargetInfo::setDefaultFeatures(StringRef CPU, StringRef TuneCPU,
  187. StringRef FS) {
  188. FeatureBits = getFeatures(CPU, TuneCPU, FS, ProcDesc, ProcFeatures);
  189. FeatureString = std::string(FS);
  190. }
  191. MCSubtargetInfo::MCSubtargetInfo(const Triple &TT, StringRef C, StringRef TC,
  192. StringRef FS, ArrayRef<SubtargetFeatureKV> PF,
  193. ArrayRef<SubtargetSubTypeKV> PD,
  194. const MCWriteProcResEntry *WPR,
  195. const MCWriteLatencyEntry *WL,
  196. const MCReadAdvanceEntry *RA,
  197. const InstrStage *IS, const unsigned *OC,
  198. const unsigned *FP)
  199. : TargetTriple(TT), CPU(std::string(C)), TuneCPU(std::string(TC)),
  200. ProcFeatures(PF), ProcDesc(PD), WriteProcResTable(WPR),
  201. WriteLatencyTable(WL), ReadAdvanceTable(RA), Stages(IS),
  202. OperandCycles(OC), ForwardingPaths(FP) {
  203. InitMCProcessorInfo(CPU, TuneCPU, FS);
  204. }
  205. FeatureBitset MCSubtargetInfo::ToggleFeature(uint64_t FB) {
  206. FeatureBits.flip(FB);
  207. return FeatureBits;
  208. }
  209. FeatureBitset MCSubtargetInfo::ToggleFeature(const FeatureBitset &FB) {
  210. FeatureBits ^= FB;
  211. return FeatureBits;
  212. }
  213. FeatureBitset MCSubtargetInfo::SetFeatureBitsTransitively(
  214. const FeatureBitset &FB) {
  215. SetImpliedBits(FeatureBits, FB, ProcFeatures);
  216. return FeatureBits;
  217. }
  218. FeatureBitset MCSubtargetInfo::ClearFeatureBitsTransitively(
  219. const FeatureBitset &FB) {
  220. for (unsigned I = 0, E = FB.size(); I < E; I++) {
  221. if (FB[I]) {
  222. FeatureBits.reset(I);
  223. ClearImpliedBits(FeatureBits, I, ProcFeatures);
  224. }
  225. }
  226. return FeatureBits;
  227. }
  228. FeatureBitset MCSubtargetInfo::ToggleFeature(StringRef Feature) {
  229. // Find feature in table.
  230. const SubtargetFeatureKV *FeatureEntry =
  231. Find(SubtargetFeatures::StripFlag(Feature), ProcFeatures);
  232. // If there is a match
  233. if (FeatureEntry) {
  234. if (FeatureBits.test(FeatureEntry->Value)) {
  235. FeatureBits.reset(FeatureEntry->Value);
  236. // For each feature that implies this, clear it.
  237. ClearImpliedBits(FeatureBits, FeatureEntry->Value, ProcFeatures);
  238. } else {
  239. FeatureBits.set(FeatureEntry->Value);
  240. // For each feature that this implies, set it.
  241. SetImpliedBits(FeatureBits, FeatureEntry->Implies.getAsBitset(),
  242. ProcFeatures);
  243. }
  244. } else {
  245. errs() << "'" << Feature << "' is not a recognized feature for this target"
  246. << " (ignoring feature)\n";
  247. }
  248. return FeatureBits;
  249. }
  250. FeatureBitset MCSubtargetInfo::ApplyFeatureFlag(StringRef FS) {
  251. ::ApplyFeatureFlag(FeatureBits, FS, ProcFeatures);
  252. return FeatureBits;
  253. }
  254. bool MCSubtargetInfo::checkFeatures(StringRef FS) const {
  255. SubtargetFeatures T(FS);
  256. FeatureBitset Set, All;
  257. for (std::string F : T.getFeatures()) {
  258. ::ApplyFeatureFlag(Set, F, ProcFeatures);
  259. if (F[0] == '-')
  260. F[0] = '+';
  261. ::ApplyFeatureFlag(All, F, ProcFeatures);
  262. }
  263. return (FeatureBits & All) == Set;
  264. }
  265. const MCSchedModel &MCSubtargetInfo::getSchedModelForCPU(StringRef CPU) const {
  266. assert(llvm::is_sorted(ProcDesc) &&
  267. "Processor machine model table is not sorted");
  268. // Find entry
  269. const SubtargetSubTypeKV *CPUEntry = Find(CPU, ProcDesc);
  270. if (!CPUEntry) {
  271. if (CPU != "help") // Don't error if the user asked for help.
  272. errs() << "'" << CPU
  273. << "' is not a recognized processor for this target"
  274. << " (ignoring processor)\n";
  275. return MCSchedModel::GetDefaultSchedModel();
  276. }
  277. assert(CPUEntry->SchedModel && "Missing processor SchedModel value");
  278. return *CPUEntry->SchedModel;
  279. }
  280. InstrItineraryData
  281. MCSubtargetInfo::getInstrItineraryForCPU(StringRef CPU) const {
  282. const MCSchedModel &SchedModel = getSchedModelForCPU(CPU);
  283. return InstrItineraryData(SchedModel, Stages, OperandCycles, ForwardingPaths);
  284. }
  285. void MCSubtargetInfo::initInstrItins(InstrItineraryData &InstrItins) const {
  286. InstrItins = InstrItineraryData(getSchedModel(), Stages, OperandCycles,
  287. ForwardingPaths);
  288. }
  289. Optional<unsigned> MCSubtargetInfo::getCacheSize(unsigned Level) const {
  290. return Optional<unsigned>();
  291. }
  292. Optional<unsigned>
  293. MCSubtargetInfo::getCacheAssociativity(unsigned Level) const {
  294. return Optional<unsigned>();
  295. }
  296. Optional<unsigned> MCSubtargetInfo::getCacheLineSize(unsigned Level) const {
  297. return Optional<unsigned>();
  298. }
  299. unsigned MCSubtargetInfo::getPrefetchDistance() const {
  300. return 0;
  301. }
  302. unsigned MCSubtargetInfo::getMaxPrefetchIterationsAhead() const {
  303. return UINT_MAX;
  304. }
  305. bool MCSubtargetInfo::enableWritePrefetching() const {
  306. return false;
  307. }
  308. unsigned MCSubtargetInfo::getMinPrefetchStride(unsigned NumMemAccesses,
  309. unsigned NumStridedMemAccesses,
  310. unsigned NumPrefetches,
  311. bool HasCall) const {
  312. return 1;
  313. }