1 //===- llvm/MC/MCSubtargetInfo.h - Subtarget Information --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file describes the subtarget options of a Target machine.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_MC_MCSUBTARGETINFO_H
15 #define LLVM_MC_MCSUBTARGETINFO_H
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/MC/MCInstrItineraries.h"
21 #include "llvm/MC/MCSchedule.h"
22 #include "llvm/MC/SubtargetFeature.h"
32 //===----------------------------------------------------------------------===//
34 /// MCSubtargetInfo - Generic base class for all target subtargets.
36 class MCSubtargetInfo {
37 Triple TargetTriple; // Target triple
38 std::string CPU; // CPU being targeted.
39 ArrayRef<SubtargetFeatureKV> ProcFeatures; // Processor feature list
40 ArrayRef<SubtargetFeatureKV> ProcDesc; // Processor descriptions
42 // Scheduler machine model
43 const SubtargetInfoKV *ProcSchedModels;
44 const MCWriteProcResEntry *WriteProcResTable;
45 const MCWriteLatencyEntry *WriteLatencyTable;
46 const MCReadAdvanceEntry *ReadAdvanceTable;
47 const MCSchedModel *CPUSchedModel;
49 const InstrStage *Stages; // Instruction itinerary stages
50 const unsigned *OperandCycles; // Itinerary operand cycles
51 const unsigned *ForwardingPaths; // Forwarding paths
52 FeatureBitset FeatureBits; // Feature bits for current CPU + FS
55 MCSubtargetInfo(const MCSubtargetInfo &) = default;
56 MCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef FS,
57 ArrayRef<SubtargetFeatureKV> PF,
58 ArrayRef<SubtargetFeatureKV> PD,
59 const SubtargetInfoKV *ProcSched,
60 const MCWriteProcResEntry *WPR, const MCWriteLatencyEntry *WL,
61 const MCReadAdvanceEntry *RA, const InstrStage *IS,
62 const unsigned *OC, const unsigned *FP);
63 MCSubtargetInfo() = delete;
64 MCSubtargetInfo &operator=(const MCSubtargetInfo &) = delete;
65 MCSubtargetInfo &operator=(MCSubtargetInfo &&) = delete;
67 virtual ~MCSubtargetInfo() {}
69 /// getTargetTriple - Return the target triple string.
70 const Triple &getTargetTriple() const { return TargetTriple; }
72 /// getCPU - Return the CPU string.
73 StringRef getCPU() const {
77 /// getFeatureBits - Return the feature bits.
79 const FeatureBitset& getFeatureBits() const {
83 /// setFeatureBits - Set the feature bits.
85 void setFeatureBits(const FeatureBitset &FeatureBits_) {
86 FeatureBits = FeatureBits_;
89 bool hasFeature(unsigned Feature) const {
90 return FeatureBits[Feature];
94 /// Initialize the scheduling model and feature bits.
96 /// FIXME: Find a way to stick this in the constructor, since it should only
97 /// be called during initialization.
98 void InitMCProcessorInfo(StringRef CPU, StringRef FS);
101 /// Set the features to the default for the given CPU with an appended feature
103 void setDefaultFeatures(StringRef CPU, StringRef FS);
105 /// ToggleFeature - Toggle a feature and returns the re-computed feature
106 /// bits. This version does not change the implied bits.
107 FeatureBitset ToggleFeature(uint64_t FB);
109 /// ToggleFeature - Toggle a feature and returns the re-computed feature
110 /// bits. This version does not change the implied bits.
111 FeatureBitset ToggleFeature(const FeatureBitset& FB);
113 /// ToggleFeature - Toggle a set of features and returns the re-computed
114 /// feature bits. This version will also change all implied bits.
115 FeatureBitset ToggleFeature(StringRef FS);
117 /// Apply a feature flag and return the re-computed feature bits, including
118 /// all feature bits implied by the flag.
119 FeatureBitset ApplyFeatureFlag(StringRef FS);
121 /// getSchedModelForCPU - Get the machine model of a CPU.
123 const MCSchedModel &getSchedModelForCPU(StringRef CPU) const;
125 /// Get the machine model for this subtarget's CPU.
126 const MCSchedModel &getSchedModel() const { return *CPUSchedModel; }
128 /// Return an iterator at the first process resource consumed by the given
129 /// scheduling class.
130 const MCWriteProcResEntry *getWriteProcResBegin(
131 const MCSchedClassDesc *SC) const {
132 return &WriteProcResTable[SC->WriteProcResIdx];
134 const MCWriteProcResEntry *getWriteProcResEnd(
135 const MCSchedClassDesc *SC) const {
136 return getWriteProcResBegin(SC) + SC->NumWriteProcResEntries;
139 const MCWriteLatencyEntry *getWriteLatencyEntry(const MCSchedClassDesc *SC,
140 unsigned DefIdx) const {
141 assert(DefIdx < SC->NumWriteLatencyEntries &&
142 "MachineModel does not specify a WriteResource for DefIdx");
144 return &WriteLatencyTable[SC->WriteLatencyIdx + DefIdx];
147 int getReadAdvanceCycles(const MCSchedClassDesc *SC, unsigned UseIdx,
148 unsigned WriteResID) const {
149 // TODO: The number of read advance entries in a class can be significant
150 // (~50). Consider compressing the WriteID into a dense ID of those that are
151 // used by ReadAdvance and representing them as a bitset.
152 for (const MCReadAdvanceEntry *I = &ReadAdvanceTable[SC->ReadAdvanceIdx],
153 *E = I + SC->NumReadAdvanceEntries; I != E; ++I) {
154 if (I->UseIdx < UseIdx)
156 if (I->UseIdx > UseIdx)
158 // Find the first WriteResIdx match, which has the highest cycle count.
159 if (!I->WriteResourceID || I->WriteResourceID == WriteResID) {
166 /// getInstrItineraryForCPU - Get scheduling itinerary of a CPU.
168 InstrItineraryData getInstrItineraryForCPU(StringRef CPU) const;
170 /// Initialize an InstrItineraryData instance.
171 void initInstrItins(InstrItineraryData &InstrItins) const;
173 /// Check whether the CPU string is valid.
174 bool isCPUStringValid(StringRef CPU) const {
175 auto Found = std::lower_bound(ProcDesc.begin(), ProcDesc.end(), CPU);
176 return Found != ProcDesc.end() && StringRef(Found->Key) == CPU;
179 /// Returns string representation of scheduler comment
180 virtual std::string getSchedInfoStr(const MachineInstr &MI) const {
181 return std::string();
184 virtual std::string getSchedInfoStr(MCInst const &MCI) const {
185 return std::string();
189 } // end namespace llvm
191 #endif // LLVM_MC_MCSUBTARGETINFO_H