MCSubtargetInfo.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/MC/MCSubtargetInfo.h - Subtarget Information --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file describes the subtarget options of a Target machine.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_MC_MCSUBTARGETINFO_H
  18. #define LLVM_MC_MCSUBTARGETINFO_H
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/ADT/STLExtras.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/ADT/Triple.h"
  23. #include "llvm/MC/MCInstrItineraries.h"
  24. #include "llvm/MC/MCSchedule.h"
  25. #include "llvm/MC/SubtargetFeature.h"
  26. #include <cassert>
  27. #include <cstdint>
  28. #include <optional>
  29. #include <string>
  30. namespace llvm {
  31. class MCInst;
  32. //===----------------------------------------------------------------------===//
  33. /// Used to provide key value pairs for feature and CPU bit flags.
  34. struct SubtargetFeatureKV {
  35. const char *Key; ///< K-V key string
  36. const char *Desc; ///< Help descriptor
  37. unsigned Value; ///< K-V integer value
  38. FeatureBitArray Implies; ///< K-V bit mask
  39. /// Compare routine for std::lower_bound
  40. bool operator<(StringRef S) const {
  41. return StringRef(Key) < S;
  42. }
  43. /// Compare routine for std::is_sorted.
  44. bool operator<(const SubtargetFeatureKV &Other) const {
  45. return StringRef(Key) < StringRef(Other.Key);
  46. }
  47. };
  48. //===----------------------------------------------------------------------===//
  49. /// Used to provide key value pairs for feature and CPU bit flags.
  50. struct SubtargetSubTypeKV {
  51. const char *Key; ///< K-V key string
  52. FeatureBitArray Implies; ///< K-V bit mask
  53. FeatureBitArray TuneImplies; ///< K-V bit mask
  54. const MCSchedModel *SchedModel;
  55. /// Compare routine for std::lower_bound
  56. bool operator<(StringRef S) const {
  57. return StringRef(Key) < S;
  58. }
  59. /// Compare routine for std::is_sorted.
  60. bool operator<(const SubtargetSubTypeKV &Other) const {
  61. return StringRef(Key) < StringRef(Other.Key);
  62. }
  63. };
  64. //===----------------------------------------------------------------------===//
  65. ///
  66. /// Generic base class for all target subtargets.
  67. ///
  68. class MCSubtargetInfo {
  69. Triple TargetTriple;
  70. std::string CPU; // CPU being targeted.
  71. std::string TuneCPU; // CPU being tuned for.
  72. ArrayRef<SubtargetFeatureKV> ProcFeatures; // Processor feature list
  73. ArrayRef<SubtargetSubTypeKV> ProcDesc; // Processor descriptions
  74. // Scheduler machine model
  75. const MCWriteProcResEntry *WriteProcResTable;
  76. const MCWriteLatencyEntry *WriteLatencyTable;
  77. const MCReadAdvanceEntry *ReadAdvanceTable;
  78. const MCSchedModel *CPUSchedModel;
  79. const InstrStage *Stages; // Instruction itinerary stages
  80. const unsigned *OperandCycles; // Itinerary operand cycles
  81. const unsigned *ForwardingPaths;
  82. FeatureBitset FeatureBits; // Feature bits for current CPU + FS
  83. std::string FeatureString; // Feature string
  84. public:
  85. MCSubtargetInfo(const MCSubtargetInfo &) = default;
  86. MCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef TuneCPU,
  87. StringRef FS, ArrayRef<SubtargetFeatureKV> PF,
  88. ArrayRef<SubtargetSubTypeKV> PD,
  89. const MCWriteProcResEntry *WPR, const MCWriteLatencyEntry *WL,
  90. const MCReadAdvanceEntry *RA, const InstrStage *IS,
  91. const unsigned *OC, const unsigned *FP);
  92. MCSubtargetInfo() = delete;
  93. MCSubtargetInfo &operator=(const MCSubtargetInfo &) = delete;
  94. MCSubtargetInfo &operator=(MCSubtargetInfo &&) = delete;
  95. virtual ~MCSubtargetInfo() = default;
  96. const Triple &getTargetTriple() const { return TargetTriple; }
  97. StringRef getCPU() const { return CPU; }
  98. StringRef getTuneCPU() const { return TuneCPU; }
  99. const FeatureBitset& getFeatureBits() const { return FeatureBits; }
  100. void setFeatureBits(const FeatureBitset &FeatureBits_) {
  101. FeatureBits = FeatureBits_;
  102. }
  103. StringRef getFeatureString() const { return FeatureString; }
  104. bool hasFeature(unsigned Feature) const {
  105. return FeatureBits[Feature];
  106. }
  107. protected:
  108. /// Initialize the scheduling model and feature bits.
  109. ///
  110. /// FIXME: Find a way to stick this in the constructor, since it should only
  111. /// be called during initialization.
  112. void InitMCProcessorInfo(StringRef CPU, StringRef TuneCPU, StringRef FS);
  113. public:
  114. /// Set the features to the default for the given CPU and TuneCPU, with ano
  115. /// appended feature string.
  116. void setDefaultFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
  117. /// Toggle a feature and return the re-computed feature bits.
  118. /// This version does not change the implied bits.
  119. FeatureBitset ToggleFeature(uint64_t FB);
  120. /// Toggle a feature and return the re-computed feature bits.
  121. /// This version does not change the implied bits.
  122. FeatureBitset ToggleFeature(const FeatureBitset& FB);
  123. /// Toggle a set of features and return the re-computed feature bits.
  124. /// This version will also change all implied bits.
  125. FeatureBitset ToggleFeature(StringRef FS);
  126. /// Apply a feature flag and return the re-computed feature bits, including
  127. /// all feature bits implied by the flag.
  128. FeatureBitset ApplyFeatureFlag(StringRef FS);
  129. /// Set/clear additional feature bits, including all other bits they imply.
  130. FeatureBitset SetFeatureBitsTransitively(const FeatureBitset& FB);
  131. FeatureBitset ClearFeatureBitsTransitively(const FeatureBitset &FB);
  132. /// Check whether the subtarget features are enabled/disabled as per
  133. /// the provided string, ignoring all other features.
  134. bool checkFeatures(StringRef FS) const;
  135. /// Get the machine model of a CPU.
  136. const MCSchedModel &getSchedModelForCPU(StringRef CPU) const;
  137. /// Get the machine model for this subtarget's CPU.
  138. const MCSchedModel &getSchedModel() const { return *CPUSchedModel; }
  139. /// Return an iterator at the first process resource consumed by the given
  140. /// scheduling class.
  141. const MCWriteProcResEntry *getWriteProcResBegin(
  142. const MCSchedClassDesc *SC) const {
  143. return &WriteProcResTable[SC->WriteProcResIdx];
  144. }
  145. const MCWriteProcResEntry *getWriteProcResEnd(
  146. const MCSchedClassDesc *SC) const {
  147. return getWriteProcResBegin(SC) + SC->NumWriteProcResEntries;
  148. }
  149. const MCWriteLatencyEntry *getWriteLatencyEntry(const MCSchedClassDesc *SC,
  150. unsigned DefIdx) const {
  151. assert(DefIdx < SC->NumWriteLatencyEntries &&
  152. "MachineModel does not specify a WriteResource for DefIdx");
  153. return &WriteLatencyTable[SC->WriteLatencyIdx + DefIdx];
  154. }
  155. int getReadAdvanceCycles(const MCSchedClassDesc *SC, unsigned UseIdx,
  156. unsigned WriteResID) const {
  157. // TODO: The number of read advance entries in a class can be significant
  158. // (~50). Consider compressing the WriteID into a dense ID of those that are
  159. // used by ReadAdvance and representing them as a bitset.
  160. for (const MCReadAdvanceEntry *I = &ReadAdvanceTable[SC->ReadAdvanceIdx],
  161. *E = I + SC->NumReadAdvanceEntries; I != E; ++I) {
  162. if (I->UseIdx < UseIdx)
  163. continue;
  164. if (I->UseIdx > UseIdx)
  165. break;
  166. // Find the first WriteResIdx match, which has the highest cycle count.
  167. if (!I->WriteResourceID || I->WriteResourceID == WriteResID) {
  168. return I->Cycles;
  169. }
  170. }
  171. return 0;
  172. }
  173. /// Return the set of ReadAdvance entries declared by the scheduling class
  174. /// descriptor in input.
  175. ArrayRef<MCReadAdvanceEntry>
  176. getReadAdvanceEntries(const MCSchedClassDesc &SC) const {
  177. if (!SC.NumReadAdvanceEntries)
  178. return ArrayRef<MCReadAdvanceEntry>();
  179. return ArrayRef<MCReadAdvanceEntry>(&ReadAdvanceTable[SC.ReadAdvanceIdx],
  180. SC.NumReadAdvanceEntries);
  181. }
  182. /// Get scheduling itinerary of a CPU.
  183. InstrItineraryData getInstrItineraryForCPU(StringRef CPU) const;
  184. /// Initialize an InstrItineraryData instance.
  185. void initInstrItins(InstrItineraryData &InstrItins) const;
  186. /// Resolve a variant scheduling class for the given MCInst and CPU.
  187. virtual unsigned resolveVariantSchedClass(unsigned SchedClass,
  188. const MCInst *MI,
  189. const MCInstrInfo *MCII,
  190. unsigned CPUID) const {
  191. return 0;
  192. }
  193. /// Check whether the CPU string is valid.
  194. bool isCPUStringValid(StringRef CPU) const {
  195. auto Found = llvm::lower_bound(ProcDesc, CPU);
  196. return Found != ProcDesc.end() && StringRef(Found->Key) == CPU;
  197. }
  198. virtual unsigned getHwMode() const { return 0; }
  199. /// Return the cache size in bytes for the given level of cache.
  200. /// Level is zero-based, so a value of zero means the first level of
  201. /// cache.
  202. ///
  203. virtual std::optional<unsigned> getCacheSize(unsigned Level) const;
  204. /// Return the cache associatvity for the given level of cache.
  205. /// Level is zero-based, so a value of zero means the first level of
  206. /// cache.
  207. ///
  208. virtual std::optional<unsigned> getCacheAssociativity(unsigned Level) const;
  209. /// Return the target cache line size in bytes at a given level.
  210. ///
  211. virtual std::optional<unsigned> getCacheLineSize(unsigned Level) const;
  212. /// Return the target cache line size in bytes. By default, return
  213. /// the line size for the bottom-most level of cache. This provides
  214. /// a more convenient interface for the common case where all cache
  215. /// levels have the same line size. Return zero if there is no
  216. /// cache model.
  217. ///
  218. virtual unsigned getCacheLineSize() const {
  219. std::optional<unsigned> Size = getCacheLineSize(0);
  220. if (Size)
  221. return *Size;
  222. return 0;
  223. }
  224. /// Return the preferred prefetch distance in terms of instructions.
  225. ///
  226. virtual unsigned getPrefetchDistance() const;
  227. /// Return the maximum prefetch distance in terms of loop
  228. /// iterations.
  229. ///
  230. virtual unsigned getMaxPrefetchIterationsAhead() const;
  231. /// \return True if prefetching should also be done for writes.
  232. ///
  233. virtual bool enableWritePrefetching() const;
  234. /// Return the minimum stride necessary to trigger software
  235. /// prefetching.
  236. ///
  237. virtual unsigned getMinPrefetchStride(unsigned NumMemAccesses,
  238. unsigned NumStridedMemAccesses,
  239. unsigned NumPrefetches,
  240. bool HasCall) const;
  241. /// \return if target want to issue a prefetch in address space \p AS.
  242. virtual bool shouldPrefetchAddressSpace(unsigned AS) const;
  243. };
  244. } // end namespace llvm
  245. #endif // LLVM_MC_MCSUBTARGETINFO_H
  246. #ifdef __GNUC__
  247. #pragma GCC diagnostic pop
  248. #endif