MCSubtargetInfo.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //==-- llvm/MC/MCSubtargetInfo.h - Subtarget Information ---------*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file describes the subtarget options of a Target machine.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCSUBTARGETINFO_H
  14. #define LLVM_MC_MCSUBTARGETINFO_H
  15. #include "llvm/MC/MCInstrItineraries.h"
  16. #include "llvm/MC/SubtargetFeature.h"
  17. #include <string>
  18. namespace llvm {
  19. class StringRef;
  20. // //
  21. ///////////////////////////////////////////////////////////////////////////////
  22. ///
  23. /// MCSubtargetInfo - Generic base class for all target subtargets.
  24. ///
  25. class MCSubtargetInfo {
  26. Triple TargetTriple; // Target triple
  27. std::string CPU; // CPU being targeted.
  28. ArrayRef<SubtargetFeatureKV> ProcFeatures; // Processor feature list
  29. ArrayRef<SubtargetFeatureKV> ProcDesc; // Processor descriptions
  30. // Scheduler machine model
  31. const SubtargetInfoKV *ProcSchedModels;
  32. const MCWriteProcResEntry *WriteProcResTable;
  33. const MCWriteLatencyEntry *WriteLatencyTable;
  34. const MCReadAdvanceEntry *ReadAdvanceTable;
  35. const MCSchedModel *CPUSchedModel;
  36. const InstrStage *Stages; // Instruction itinerary stages
  37. const unsigned *OperandCycles; // Itinerary operand cycles
  38. const unsigned *ForwardingPaths; // Forwarding paths
  39. FeatureBitset FeatureBits; // Feature bits for current CPU + FS
  40. MCSubtargetInfo() = delete;
  41. MCSubtargetInfo &operator=(MCSubtargetInfo &&) = delete;
  42. MCSubtargetInfo &operator=(const MCSubtargetInfo &) = delete;
  43. public:
  44. MCSubtargetInfo(const MCSubtargetInfo &) = default;
  45. MCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS,
  46. ArrayRef<SubtargetFeatureKV> PF,
  47. ArrayRef<SubtargetFeatureKV> PD,
  48. const SubtargetInfoKV *ProcSched,
  49. const MCWriteProcResEntry *WPR, const MCWriteLatencyEntry *WL,
  50. const MCReadAdvanceEntry *RA, const InstrStage *IS,
  51. const unsigned *OC, const unsigned *FP);
  52. /// getTargetTriple - Return the target triple string.
  53. const Triple &getTargetTriple() const { return TargetTriple; }
  54. /// getCPU - Return the CPU string.
  55. StringRef getCPU() const {
  56. return CPU;
  57. }
  58. /// getFeatureBits - Return the feature bits.
  59. ///
  60. const FeatureBitset& getFeatureBits() const {
  61. return FeatureBits;
  62. }
  63. /// setFeatureBits - Set the feature bits.
  64. ///
  65. void setFeatureBits(const FeatureBitset &FeatureBits_) {
  66. FeatureBits = FeatureBits_;
  67. }
  68. protected:
  69. /// Initialize the scheduling model and feature bits.
  70. ///
  71. /// FIXME: Find a way to stick this in the constructor, since it should only
  72. /// be called during initialization.
  73. void InitMCProcessorInfo(StringRef CPU, StringRef FS);
  74. public:
  75. /// Set the features to the default for the given CPU.
  76. void setDefaultFeatures(StringRef CPU);
  77. /// ToggleFeature - Toggle a feature and returns the re-computed feature
  78. /// bits. This version does not change the implied bits.
  79. FeatureBitset ToggleFeature(uint64_t FB);
  80. /// ToggleFeature - Toggle a feature and returns the re-computed feature
  81. /// bits. This version does not change the implied bits.
  82. FeatureBitset ToggleFeature(const FeatureBitset& FB);
  83. /// ToggleFeature - Toggle a set of features and returns the re-computed
  84. /// feature bits. This version will also change all implied bits.
  85. FeatureBitset ToggleFeature(StringRef FS);
  86. /// Apply a feature flag and return the re-computed feature bits, including
  87. /// all feature bits implied by the flag.
  88. FeatureBitset ApplyFeatureFlag(StringRef FS);
  89. /// getSchedModelForCPU - Get the machine model of a CPU.
  90. ///
  91. const MCSchedModel &getSchedModelForCPU(StringRef CPU) const;
  92. /// Get the machine model for this subtarget's CPU.
  93. const MCSchedModel &getSchedModel() const { return *CPUSchedModel; }
  94. /// Return an iterator at the first process resource consumed by the given
  95. /// scheduling class.
  96. const MCWriteProcResEntry *getWriteProcResBegin(
  97. const MCSchedClassDesc *SC) const {
  98. return &WriteProcResTable[SC->WriteProcResIdx];
  99. }
  100. const MCWriteProcResEntry *getWriteProcResEnd(
  101. const MCSchedClassDesc *SC) const {
  102. return getWriteProcResBegin(SC) + SC->NumWriteProcResEntries;
  103. }
  104. const MCWriteLatencyEntry *getWriteLatencyEntry(const MCSchedClassDesc *SC,
  105. unsigned DefIdx) const {
  106. assert(DefIdx < SC->NumWriteLatencyEntries &&
  107. "MachineModel does not specify a WriteResource for DefIdx");
  108. return &WriteLatencyTable[SC->WriteLatencyIdx + DefIdx];
  109. }
  110. int getReadAdvanceCycles(const MCSchedClassDesc *SC, unsigned UseIdx,
  111. unsigned WriteResID) const {
  112. // TODO: The number of read advance entries in a class can be significant
  113. // (~50). Consider compressing the WriteID into a dense ID of those that are
  114. // used by ReadAdvance and representing them as a bitset.
  115. for (const MCReadAdvanceEntry *I = &ReadAdvanceTable[SC->ReadAdvanceIdx],
  116. *E = I + SC->NumReadAdvanceEntries; I != E; ++I) {
  117. if (I->UseIdx < UseIdx)
  118. continue;
  119. if (I->UseIdx > UseIdx)
  120. break;
  121. // Find the first WriteResIdx match, which has the highest cycle count.
  122. if (!I->WriteResourceID || I->WriteResourceID == WriteResID) {
  123. return I->Cycles;
  124. }
  125. }
  126. return 0;
  127. }
  128. /// getInstrItineraryForCPU - Get scheduling itinerary of a CPU.
  129. ///
  130. InstrItineraryData getInstrItineraryForCPU(StringRef CPU) const;
  131. /// Initialize an InstrItineraryData instance.
  132. void initInstrItins(InstrItineraryData &InstrItins) const;
  133. /// Check whether the CPU string is valid.
  134. bool isCPUStringValid(StringRef CPU) const {
  135. auto Found = std::find_if(ProcDesc.begin(), ProcDesc.end(),
  136. [=](const SubtargetFeatureKV &KV) {
  137. return CPU == KV.Key;
  138. });
  139. return Found != ProcDesc.end();
  140. }
  141. };
  142. } // End llvm namespace
  143. #endif