MCSubtargetInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. if (!TuneCPU.empty())
  181. CPUSchedModel = &getSchedModelForCPU(TuneCPU);
  182. else
  183. CPUSchedModel = &MCSchedModel::GetDefaultSchedModel();
  184. }
  185. void MCSubtargetInfo::setDefaultFeatures(StringRef CPU, StringRef TuneCPU,
  186. StringRef FS) {
  187. FeatureBits = getFeatures(CPU, TuneCPU, FS, ProcDesc, ProcFeatures);
  188. }
  189. MCSubtargetInfo::MCSubtargetInfo(const Triple &TT, StringRef C, StringRef TC,
  190. StringRef FS, ArrayRef<SubtargetFeatureKV> PF,
  191. ArrayRef<SubtargetSubTypeKV> PD,
  192. const MCWriteProcResEntry *WPR,
  193. const MCWriteLatencyEntry *WL,
  194. const MCReadAdvanceEntry *RA,
  195. const InstrStage *IS, const unsigned *OC,
  196. const unsigned *FP)
  197. : TargetTriple(TT), CPU(std::string(C)), TuneCPU(std::string(TC)),
  198. ProcFeatures(PF), ProcDesc(PD), WriteProcResTable(WPR),
  199. WriteLatencyTable(WL), ReadAdvanceTable(RA), Stages(IS),
  200. OperandCycles(OC), ForwardingPaths(FP) {
  201. InitMCProcessorInfo(CPU, TuneCPU, FS);
  202. }
  203. FeatureBitset MCSubtargetInfo::ToggleFeature(uint64_t FB) {
  204. FeatureBits.flip(FB);
  205. return FeatureBits;
  206. }
  207. FeatureBitset MCSubtargetInfo::ToggleFeature(const FeatureBitset &FB) {
  208. FeatureBits ^= FB;
  209. return FeatureBits;
  210. }
  211. FeatureBitset MCSubtargetInfo::SetFeatureBitsTransitively(
  212. const FeatureBitset &FB) {
  213. SetImpliedBits(FeatureBits, FB, ProcFeatures);
  214. return FeatureBits;
  215. }
  216. FeatureBitset MCSubtargetInfo::ClearFeatureBitsTransitively(
  217. const FeatureBitset &FB) {
  218. for (unsigned I = 0, E = FB.size(); I < E; I++) {
  219. if (FB[I]) {
  220. FeatureBits.reset(I);
  221. ClearImpliedBits(FeatureBits, I, ProcFeatures);
  222. }
  223. }
  224. return FeatureBits;
  225. }
  226. FeatureBitset MCSubtargetInfo::ToggleFeature(StringRef Feature) {
  227. // Find feature in table.
  228. const SubtargetFeatureKV *FeatureEntry =
  229. Find(SubtargetFeatures::StripFlag(Feature), ProcFeatures);
  230. // If there is a match
  231. if (FeatureEntry) {
  232. if (FeatureBits.test(FeatureEntry->Value)) {
  233. FeatureBits.reset(FeatureEntry->Value);
  234. // For each feature that implies this, clear it.
  235. ClearImpliedBits(FeatureBits, FeatureEntry->Value, ProcFeatures);
  236. } else {
  237. FeatureBits.set(FeatureEntry->Value);
  238. // For each feature that this implies, set it.
  239. SetImpliedBits(FeatureBits, FeatureEntry->Implies.getAsBitset(),
  240. ProcFeatures);
  241. }
  242. } else {
  243. errs() << "'" << Feature << "' is not a recognized feature for this target"
  244. << " (ignoring feature)\n";
  245. }
  246. return FeatureBits;
  247. }
  248. FeatureBitset MCSubtargetInfo::ApplyFeatureFlag(StringRef FS) {
  249. ::ApplyFeatureFlag(FeatureBits, FS, ProcFeatures);
  250. return FeatureBits;
  251. }
  252. bool MCSubtargetInfo::checkFeatures(StringRef FS) const {
  253. SubtargetFeatures T(FS);
  254. FeatureBitset Set, All;
  255. for (std::string F : T.getFeatures()) {
  256. ::ApplyFeatureFlag(Set, F, ProcFeatures);
  257. if (F[0] == '-')
  258. F[0] = '+';
  259. ::ApplyFeatureFlag(All, F, ProcFeatures);
  260. }
  261. return (FeatureBits & All) == Set;
  262. }
  263. const MCSchedModel &MCSubtargetInfo::getSchedModelForCPU(StringRef CPU) const {
  264. assert(llvm::is_sorted(ProcDesc) &&
  265. "Processor machine model table is not sorted");
  266. // Find entry
  267. const SubtargetSubTypeKV *CPUEntry = Find(CPU, ProcDesc);
  268. if (!CPUEntry) {
  269. if (CPU != "help") // Don't error if the user asked for help.
  270. errs() << "'" << CPU
  271. << "' is not a recognized processor for this target"
  272. << " (ignoring processor)\n";
  273. return MCSchedModel::GetDefaultSchedModel();
  274. }
  275. assert(CPUEntry->SchedModel && "Missing processor SchedModel value");
  276. return *CPUEntry->SchedModel;
  277. }
  278. InstrItineraryData
  279. MCSubtargetInfo::getInstrItineraryForCPU(StringRef CPU) const {
  280. const MCSchedModel &SchedModel = getSchedModelForCPU(CPU);
  281. return InstrItineraryData(SchedModel, Stages, OperandCycles, ForwardingPaths);
  282. }
  283. void MCSubtargetInfo::initInstrItins(InstrItineraryData &InstrItins) const {
  284. InstrItins = InstrItineraryData(getSchedModel(), Stages, OperandCycles,
  285. ForwardingPaths);
  286. }
  287. Optional<unsigned> MCSubtargetInfo::getCacheSize(unsigned Level) const {
  288. return Optional<unsigned>();
  289. }
  290. Optional<unsigned>
  291. MCSubtargetInfo::getCacheAssociativity(unsigned Level) const {
  292. return Optional<unsigned>();
  293. }
  294. Optional<unsigned> MCSubtargetInfo::getCacheLineSize(unsigned Level) const {
  295. return Optional<unsigned>();
  296. }
  297. unsigned MCSubtargetInfo::getPrefetchDistance() const {
  298. return 0;
  299. }
  300. unsigned MCSubtargetInfo::getMaxPrefetchIterationsAhead() const {
  301. return UINT_MAX;
  302. }
  303. bool MCSubtargetInfo::enableWritePrefetching() const {
  304. return false;
  305. }
  306. unsigned MCSubtargetInfo::getMinPrefetchStride(unsigned NumMemAccesses,
  307. unsigned NumStridedMemAccesses,
  308. unsigned NumPrefetches,
  309. bool HasCall) const {
  310. return 1;
  311. }