LLVM 22.0.0git
MCSubtargetInfo.h
Go to the documentation of this file.
1//===- llvm/MC/MCSubtargetInfo.h - Subtarget Information --------*- C++ -*-===//
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//
9// This file describes the subtarget options of a Target machine.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_MC_MCSUBTARGETINFO_H
14#define LLVM_MC_MCSUBTARGETINFO_H
15
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/StringRef.h"
20#include "llvm/MC/MCSchedule.h"
24#include <cassert>
25#include <cstdint>
26#include <optional>
27#include <string>
28
29namespace llvm {
30
31class MCInst;
32
33//===----------------------------------------------------------------------===//
34
35/// Used to provide key value pairs for feature and CPU bit flags.
37 const char *Key; ///< K-V key string
38 const char *Desc; ///< Help descriptor
39 unsigned Value; ///< K-V integer value
40 FeatureBitArray Implies; ///< K-V bit mask
41
42 /// Compare routine for std::lower_bound
43 bool operator<(StringRef S) const {
44 return StringRef(Key) < S;
45 }
46
47 /// Compare routine for std::is_sorted.
48 bool operator<(const SubtargetFeatureKV &Other) const {
49 return StringRef(Key) < StringRef(Other.Key);
50 }
51};
52
53//===----------------------------------------------------------------------===//
54
55/// Used to provide key value pairs for feature and CPU bit flags.
57 const char *Key; ///< K-V key string
58 FeatureBitArray Implies; ///< K-V bit mask
59 FeatureBitArray TuneImplies; ///< K-V bit mask
61
62 /// Compare routine for std::lower_bound
63 bool operator<(StringRef S) const {
64 return StringRef(Key) < S;
65 }
66
67 /// Compare routine for std::is_sorted.
68 bool operator<(const SubtargetSubTypeKV &Other) const {
69 return StringRef(Key) < StringRef(Other.Key);
70 }
71};
72
73//===----------------------------------------------------------------------===//
74///
75/// Generic base class for all target subtargets.
76///
78 Triple TargetTriple;
79 std::string CPU; // CPU being targeted.
80 std::string TuneCPU; // CPU being tuned for.
81 ArrayRef<StringRef> ProcNames; // Processor list, including aliases
82 ArrayRef<SubtargetFeatureKV> ProcFeatures; // Processor feature list
83 ArrayRef<SubtargetSubTypeKV> ProcDesc; // Processor descriptions
84
85 // Scheduler machine model
86 const MCWriteProcResEntry *WriteProcResTable;
87 const MCWriteLatencyEntry *WriteLatencyTable;
88 const MCReadAdvanceEntry *ReadAdvanceTable;
89 const MCSchedModel *CPUSchedModel;
90
91 const InstrStage *Stages; // Instruction itinerary stages
92 const unsigned *OperandCycles; // Itinerary operand cycles
93 const unsigned *ForwardingPaths;
94 FeatureBitset FeatureBits; // Feature bits for current CPU + FS
95 std::string FeatureString; // Feature string
96
97public:
98 MCSubtargetInfo(const MCSubtargetInfo &) = default;
99 MCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef TuneCPU,
103 const MCWriteProcResEntry *WPR, const MCWriteLatencyEntry *WL,
104 const MCReadAdvanceEntry *RA, const InstrStage *IS,
105 const unsigned *OC, const unsigned *FP);
106 MCSubtargetInfo() = delete;
109 virtual ~MCSubtargetInfo() = default;
110
111 const Triple &getTargetTriple() const { return TargetTriple; }
112 StringRef getCPU() const { return CPU; }
113 StringRef getTuneCPU() const { return TuneCPU; }
114
115 const FeatureBitset& getFeatureBits() const { return FeatureBits; }
116 void setFeatureBits(const FeatureBitset &FeatureBits_) {
117 FeatureBits = FeatureBits_;
118 }
119
120 StringRef getFeatureString() const { return FeatureString; }
121
122 bool hasFeature(unsigned Feature) const {
123 return FeatureBits[Feature];
124 }
125
126protected:
127 /// Initialize the scheduling model and feature bits.
128 ///
129 /// FIXME: Find a way to stick this in the constructor, since it should only
130 /// be called during initialization.
131 void InitMCProcessorInfo(StringRef CPU, StringRef TuneCPU, StringRef FS);
132
133public:
134 /// Set the features to the default for the given CPU and TuneCPU, with ano
135 /// appended feature string.
136 void setDefaultFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
137
138 /// Toggle a feature and return the re-computed feature bits.
139 /// This version does not change the implied bits.
140 FeatureBitset ToggleFeature(uint64_t FB);
141
142 /// Toggle a feature and return the re-computed feature bits.
143 /// This version does not change the implied bits.
144 FeatureBitset ToggleFeature(const FeatureBitset& FB);
145
146 /// Toggle a set of features and return the re-computed feature bits.
147 /// This version will also change all implied bits.
148 FeatureBitset ToggleFeature(StringRef FS);
149
150 /// Apply a feature flag and return the re-computed feature bits, including
151 /// all feature bits implied by the flag.
153
154 /// Set/clear additional feature bits, including all other bits they imply.
155 FeatureBitset SetFeatureBitsTransitively(const FeatureBitset& FB);
156 FeatureBitset ClearFeatureBitsTransitively(const FeatureBitset &FB);
157
158 /// Check whether the subtarget features are enabled/disabled as per
159 /// the provided string, ignoring all other features.
160 bool checkFeatures(StringRef FS) const;
161
162 /// Get the machine model of a CPU.
163 const MCSchedModel &getSchedModelForCPU(StringRef CPU) const;
164
165 /// Get the machine model for this subtarget's CPU.
166 const MCSchedModel &getSchedModel() const { return *CPUSchedModel; }
167
168 /// Return an iterator at the first process resource consumed by the given
169 /// scheduling class.
171 const MCSchedClassDesc *SC) const {
172 return &WriteProcResTable[SC->WriteProcResIdx];
173 }
175 const MCSchedClassDesc *SC) const {
176 return getWriteProcResBegin(SC) + SC->NumWriteProcResEntries;
177 }
178
180 unsigned DefIdx) const {
181 assert(DefIdx < SC->NumWriteLatencyEntries &&
182 "MachineModel does not specify a WriteResource for DefIdx");
183
184 return &WriteLatencyTable[SC->WriteLatencyIdx + DefIdx];
185 }
186
187 int getReadAdvanceCycles(const MCSchedClassDesc *SC, unsigned UseIdx,
188 unsigned WriteResID) const {
189 // TODO: The number of read advance entries in a class can be significant
190 // (~50). Consider compressing the WriteID into a dense ID of those that are
191 // used by ReadAdvance and representing them as a bitset.
192 for (const MCReadAdvanceEntry *I = &ReadAdvanceTable[SC->ReadAdvanceIdx],
193 *E = I + SC->NumReadAdvanceEntries; I != E; ++I) {
194 if (I->UseIdx < UseIdx)
195 continue;
196 if (I->UseIdx > UseIdx)
197 break;
198 // Find the first WriteResIdx match, which has the highest cycle count.
199 if (!I->WriteResourceID || I->WriteResourceID == WriteResID) {
200 return I->Cycles;
201 }
202 }
203 return 0;
204 }
205
206 /// Return the set of ReadAdvance entries declared by the scheduling class
207 /// descriptor in input.
210 if (!SC.NumReadAdvanceEntries)
212 return ArrayRef<MCReadAdvanceEntry>(&ReadAdvanceTable[SC.ReadAdvanceIdx],
214 }
215
216 /// Get scheduling itinerary of a CPU.
217 InstrItineraryData getInstrItineraryForCPU(StringRef CPU) const;
218
219 /// Initialize an InstrItineraryData instance.
220 void initInstrItins(InstrItineraryData &InstrItins) const;
221
222 /// Resolve a variant scheduling class for the given MCInst and CPU.
223 virtual unsigned resolveVariantSchedClass(unsigned SchedClass,
224 const MCInst *MI,
225 const MCInstrInfo *MCII,
226 unsigned CPUID) const {
227 return 0;
228 }
229
230 /// Check whether the CPU string is valid.
231 virtual bool isCPUStringValid(StringRef CPU) const {
232 auto Found = llvm::lower_bound(ProcDesc, CPU);
233 return Found != ProcDesc.end() && StringRef(Found->Key) == CPU;
234 }
235
236 /// Return processor descriptions.
238 return ProcDesc;
239 }
240
241 /// Return processor features.
243 return ProcFeatures;
244 }
245
246 /// Return the list of processor features currently enabled.
247 std::vector<SubtargetFeatureKV> getEnabledProcessorFeatures() const;
248
249 /// HwMode IDs are stored and accessed in a bit set format, enabling
250 /// users to efficiently retrieve specific IDs, such as the RegInfo
251 /// HwMode ID, from the set as required. Using this approach, various
252 /// types of HwMode IDs can be added to a subtarget to manage different
253 /// attributes within that subtarget, significantly enhancing the
254 /// scalability and usability of HwMode. Moreover, to ensure compatibility,
255 /// this method also supports controlling multiple attributes with a single
256 /// HwMode ID, just as was done previously.
258 HwMode_Default, // Return the smallest HwMode ID of current subtarget.
259 HwMode_ValueType, // Return the HwMode ID that controls the ValueType.
260 HwMode_RegInfo, // Return the HwMode ID that controls the RegSizeInfo and
261 // SubRegRange.
262 HwMode_EncodingInfo // Return the HwMode ID that controls the EncodingInfo.
263 };
264
265 /// Return a bit set containing all HwMode IDs of the current subtarget.
266 virtual unsigned getHwModeSet() const { return 0; }
267
268 /// HwMode ID corresponding to the 'type' parameter is retrieved from the
269 /// HwMode bit set of the current subtarget. It’s important to note that if
270 /// the current subtarget possesses two HwMode IDs and both control a single
271 /// attribute (such as RegInfo), this interface will result in an error.
272 virtual unsigned getHwMode(enum HwModeType type = HwMode_Default) const {
273 return 0;
274 }
275
276 /// Return the cache size in bytes for the given level of cache.
277 /// Level is zero-based, so a value of zero means the first level of
278 /// cache.
279 ///
280 virtual std::optional<unsigned> getCacheSize(unsigned Level) const;
281
282 /// Return the cache associatvity for the given level of cache.
283 /// Level is zero-based, so a value of zero means the first level of
284 /// cache.
285 ///
286 virtual std::optional<unsigned> getCacheAssociativity(unsigned Level) const;
287
288 /// Return the target cache line size in bytes at a given level.
289 ///
290 virtual std::optional<unsigned> getCacheLineSize(unsigned Level) const;
291
292 /// Return the target cache line size in bytes. By default, return
293 /// the line size for the bottom-most level of cache. This provides
294 /// a more convenient interface for the common case where all cache
295 /// levels have the same line size. Return zero if there is no
296 /// cache model.
297 ///
298 virtual unsigned getCacheLineSize() const {
299 std::optional<unsigned> Size = getCacheLineSize(0);
300 if (Size)
301 return *Size;
302
303 return 0;
304 }
305
306 /// Return the preferred prefetch distance in terms of instructions.
307 ///
308 virtual unsigned getPrefetchDistance() const;
309
310 /// Return the maximum prefetch distance in terms of loop
311 /// iterations.
312 ///
313 virtual unsigned getMaxPrefetchIterationsAhead() const;
314
315 /// \return True if prefetching should also be done for writes.
316 ///
317 virtual bool enableWritePrefetching() const;
318
319 /// Return the minimum stride necessary to trigger software
320 /// prefetching.
321 ///
322 virtual unsigned getMinPrefetchStride(unsigned NumMemAccesses,
323 unsigned NumStridedMemAccesses,
324 unsigned NumPrefetches,
325 bool HasCall) const;
326
327 /// \return if target want to issue a prefetch in address space \p AS.
328 virtual bool shouldPrefetchAddressSpace(unsigned AS) const;
329};
330
331} // end namespace llvm
332
333#endif // LLVM_MC_MCSUBTARGETINFO_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition: Compiler.h:213
uint64_t Size
IRTranslator LLVM IR MI
static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature, ArrayRef< SubtargetFeatureKV > FeatureTable)
#define I(x, y, z)
Definition: MD5.cpp:58
SI optimize exec mask operations pre RA
This file contains some templates that are useful if you are working with the STL at all.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:136
Class used to store the subtarget bits in the tables created by tablegen.
Container class for subtarget features.
Itinerary data supplied by a subtarget to be used by a target.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
Interface to description of machine instruction set.
Definition: MCInstrInfo.h:27
Generic base class for all target subtargets.
virtual unsigned getCacheLineSize() const
Return the target cache line size in bytes.
const MCWriteProcResEntry * getWriteProcResEnd(const MCSchedClassDesc *SC) const
bool hasFeature(unsigned Feature) const
int getReadAdvanceCycles(const MCSchedClassDesc *SC, unsigned UseIdx, unsigned WriteResID) const
StringRef getFeatureString() const
void setFeatureBits(const FeatureBitset &FeatureBits_)
virtual unsigned resolveVariantSchedClass(unsigned SchedClass, const MCInst *MI, const MCInstrInfo *MCII, unsigned CPUID) const
Resolve a variant scheduling class for the given MCInst and CPU.
const Triple & getTargetTriple() const
ArrayRef< SubtargetSubTypeKV > getAllProcessorDescriptions() const
Return processor descriptions.
ArrayRef< MCReadAdvanceEntry > getReadAdvanceEntries(const MCSchedClassDesc &SC) const
Return the set of ReadAdvance entries declared by the scheduling class descriptor in input.
const MCWriteLatencyEntry * getWriteLatencyEntry(const MCSchedClassDesc *SC, unsigned DefIdx) const
MCSubtargetInfo & operator=(const MCSubtargetInfo &)=delete
MCSubtargetInfo & operator=(MCSubtargetInfo &&)=delete
const FeatureBitset & getFeatureBits() const
HwModeType
HwMode IDs are stored and accessed in a bit set format, enabling users to efficiently retrieve specif...
StringRef getCPU() const
ArrayRef< SubtargetFeatureKV > getAllProcessorFeatures() const
Return processor features.
virtual ~MCSubtargetInfo()=default
StringRef getTuneCPU() const
MCSubtargetInfo(const MCSubtargetInfo &)=default
virtual bool isCPUStringValid(StringRef CPU) const
Check whether the CPU string is valid.
virtual unsigned getHwModeSet() const
Return a bit set containing all HwMode IDs of the current subtarget.
const MCWriteProcResEntry * getWriteProcResBegin(const MCSchedClassDesc *SC) const
Return an iterator at the first process resource consumed by the given scheduling class.
virtual unsigned getHwMode(enum HwModeType type=HwMode_Default) const
HwMode ID corresponding to the 'type' parameter is retrieved from the HwMode bit set of the current s...
const MCSchedModel & getSchedModel() const
Get the machine model for this subtarget's CPU.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Other
Any other memory.
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:2013
These values represent a non-pipelined step in the execution of an instruction.
Specify the number of cycles allowed after instruction issue before a particular use operand reads it...
Definition: MCSchedule.h:108
Summarize the scheduling resources required for an instruction of a particular scheduling class.
Definition: MCSchedule.h:123
uint16_t NumReadAdvanceEntries
Definition: MCSchedule.h:139
uint16_t NumWriteProcResEntries
Definition: MCSchedule.h:135
Machine model for scheduling, bundling, and heuristics.
Definition: MCSchedule.h:258
Specify the latency in cpu cycles for a particular scheduling class and def index.
Definition: MCSchedule.h:91
Identify one of the processor resource kinds consumed by a particular scheduling class for the specif...
Definition: MCSchedule.h:68
Used to provide key value pairs for feature and CPU bit flags.
bool operator<(StringRef S) const
Compare routine for std::lower_bound.
bool operator<(const SubtargetFeatureKV &Other) const
Compare routine for std::is_sorted.
unsigned Value
K-V integer value.
const char * Key
K-V key string.
const char * Desc
Help descriptor.
FeatureBitArray Implies
K-V bit mask.
Used to provide key value pairs for feature and CPU bit flags.
const MCSchedModel * SchedModel
FeatureBitArray Implies
K-V bit mask.
const char * Key
K-V key string.
FeatureBitArray TuneImplies
K-V bit mask.
bool operator<(const SubtargetSubTypeKV &Other) const
Compare routine for std::is_sorted.
bool operator<(StringRef S) const
Compare routine for std::lower_bound.