MCSubtargetInfo.cpp 12 KB

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