MCSubtargetInfo.h 11 KB

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