LLVM 23.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"
21#include "llvm/MC/MCSchedule.h"
25#include <cassert>
26#include <cstdint>
27#include <optional>
28#include <string>
29
30namespace llvm {
31
32class MCInst;
33
34//===----------------------------------------------------------------------===//
35
36/// Used to provide key value pairs for feature and CPU bit flags.
40 unsigned Value; ///< K-V integer value
41 FeatureBitArray Implies; ///< K-V bit mask
42
47
48 // Because of relative string offsets, this type is not copyable.
51
52 const char *key() const {
53 return reinterpret_cast<const char *>(this) + KeyStrOff;
54 }
55
56 const char *desc() const {
57 return reinterpret_cast<const char *>(this) + DescStrOff;
58 }
59
60 /// Compare routine for std::lower_bound
61 bool operator<(StringRef S) const { return StringRef(key()) < S; }
62
63 /// Compare routine for std::is_sorted.
64 bool operator<(const SubtargetFeatureKV &Other) const {
65 return StringRef(key()) < StringRef(Other.key());
66 }
67};
68
69template <size_t NumFeatures, size_t FeatureStrTabSize>
72 char Strings[FeatureStrTabSize];
73};
74
75//===----------------------------------------------------------------------===//
76
77/// Used to provide key value pairs for feature and CPU bit flags.
80 unsigned SchedModelIdx;
81 FeatureBitArray Implies; ///< K-V bit mask
82 FeatureBitArray TuneImplies; ///< K-V bit mask
83
89
90 // Because of relative string offsets, this type is not copyable.
93
94 const char *key() const {
95 return reinterpret_cast<const char *>(this) + KeyStrOff;
96 }
97
98 /// Compare routine for std::lower_bound
99 bool operator<(StringRef S) const { return StringRef(key()) < S; }
100
101 /// Compare routine for std::is_sorted.
102 bool operator<(const SubtargetSubTypeKV &Other) const {
103 return StringRef(key()) < StringRef(Other.key());
104 }
105};
106
107template <size_t NumSubTypes, size_t SubTypeStrTabSize>
110 char Strings[SubTypeStrTabSize];
111};
112
113//===----------------------------------------------------------------------===//
114///
115/// Generic base class for all target subtargets.
116///
118 Triple TargetTriple;
119 std::string CPU; // CPU being targeted.
120 std::string TuneCPU; // CPU being tuned for.
121 StringTable ProcNames; // Processor list, including aliases
122 ArrayRef<SubtargetFeatureKV> ProcFeatures; // Processor feature list
123 ArrayRef<SubtargetSubTypeKV> ProcDesc; // Processor descriptions
124 const MCSchedModel *ProcSchedModels; ///< Processor scheduling models.
125
126 // Scheduler machine model
127 const MCWriteProcResEntry *WriteProcResTable;
128 const MCWriteLatencyEntry *WriteLatencyTable;
129 const MCReadAdvanceEntry *ReadAdvanceTable;
130 const MCSchedModel *CPUSchedModel;
131
132 const InstrStage *Stages; // Instruction itinerary stages
133 const unsigned *OperandCycles; // Itinerary operand cycles
134 const unsigned *ForwardingPaths;
135 FeatureBitset FeatureBits; // Feature bits for current CPU + FS
136 std::string FeatureString; // Feature string
137
138public:
140 MCSubtargetInfo(const Triple &TT, StringRef CPU, StringRef TuneCPU,
143 const MCWriteProcResEntry *WPR, const MCWriteLatencyEntry *WL,
144 const MCReadAdvanceEntry *RA, const InstrStage *IS,
145 const unsigned *OC, const unsigned *FP);
146 MCSubtargetInfo() = delete;
149 virtual ~MCSubtargetInfo() = default;
150
151 const Triple &getTargetTriple() const { return TargetTriple; }
152 StringRef getCPU() const { return CPU; }
153 StringRef getTuneCPU() const { return TuneCPU; }
154
155 const FeatureBitset& getFeatureBits() const { return FeatureBits; }
156 void setFeatureBits(const FeatureBitset &FeatureBits_) {
157 FeatureBits = FeatureBits_;
158 }
159
160 StringRef getFeatureString() const { return FeatureString; }
161
162 bool hasFeature(unsigned Feature) const {
163 return FeatureBits[Feature];
164 }
165
166protected:
167 /// Initialize the scheduling model and feature bits.
168 ///
169 /// FIXME: Find a way to stick this in the constructor, since it should only
170 /// be called during initialization.
171 void InitMCProcessorInfo(StringRef CPU, StringRef TuneCPU, StringRef FS);
172
173public:
174 /// Set the features to the default for the given CPU and TuneCPU, with ano
175 /// appended feature string.
176 void setDefaultFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
177
178 /// Toggle a feature and return the re-computed feature bits.
179 /// This version does not change the implied bits.
180 const FeatureBitset &ToggleFeature(uint64_t FB);
181
182 /// Toggle a feature and return the re-computed feature bits.
183 /// This version does not change the implied bits.
184 const FeatureBitset &ToggleFeature(const FeatureBitset &FB);
185
186 /// Toggle a set of features and return the re-computed feature bits.
187 /// This version will also change all implied bits.
188 const FeatureBitset &ToggleFeature(StringRef FS);
189
190 /// Apply a feature flag and return the re-computed feature bits, including
191 /// all feature bits implied by the flag.
193
194 /// Set/clear additional feature bits, including all other bits they imply.
195 const FeatureBitset &SetFeatureBitsTransitively(const FeatureBitset &FB);
196 const FeatureBitset &ClearFeatureBitsTransitively(const FeatureBitset &FB);
197
198 /// Check whether the subtarget features are enabled/disabled as per
199 /// the provided string, ignoring all other features.
200 bool checkFeatures(StringRef FS) const;
201
202 /// Check whether the current subtarget satisfies a target feature expression.
203 /// The expression uses feature names from the target's subtarget feature
204 /// table. Comma means AND, | means OR, comma has higher precedence than |,
205 /// and parentheses group expressions.
206 bool checkFeatureExpression(StringRef FeatureExpr) const;
207
208 /// Get the machine model of a CPU.
209 const MCSchedModel &getSchedModelForCPU(StringRef CPU) const;
210
211 /// Get the machine model for this subtarget's CPU.
212 const MCSchedModel &getSchedModel() const { return *CPUSchedModel; }
213
214 /// Return an iterator at the first process resource consumed by the given
215 /// scheduling class.
217 const MCSchedClassDesc *SC) const {
218 return &WriteProcResTable[SC->WriteProcResIdx];
219 }
224
226 unsigned DefIdx) const {
227 assert(DefIdx < SC->NumWriteLatencyEntries &&
228 "MachineModel does not specify a WriteResource for DefIdx");
229
230 return &WriteLatencyTable[SC->WriteLatencyIdx + DefIdx];
231 }
232
233 int getReadAdvanceCycles(const MCSchedClassDesc *SC, unsigned UseIdx,
234 unsigned WriteResID) const {
235 // TODO: The number of read advance entries in a class can be significant
236 // (~50). Consider compressing the WriteID into a dense ID of those that are
237 // used by ReadAdvance and representing them as a bitset.
238 for (const MCReadAdvanceEntry *I = &ReadAdvanceTable[SC->ReadAdvanceIdx],
239 *E = I + SC->NumReadAdvanceEntries; I != E; ++I) {
240 if (I->UseIdx < UseIdx)
241 continue;
242 if (I->UseIdx > UseIdx)
243 break;
244 // Find the first WriteResIdx match, which has the highest cycle count.
245 if (!I->WriteResourceID || I->WriteResourceID == WriteResID) {
246 return I->Cycles;
247 }
248 }
249 return 0;
250 }
251
252 /// Return the set of ReadAdvance entries declared by the scheduling class
253 /// descriptor in input.
256 if (!SC.NumReadAdvanceEntries)
258 return ArrayRef<MCReadAdvanceEntry>(&ReadAdvanceTable[SC.ReadAdvanceIdx],
260 }
261
262 /// Get scheduling itinerary of a CPU.
263 InstrItineraryData getInstrItineraryForCPU(StringRef CPU) const;
264
265 /// Initialize an InstrItineraryData instance.
266 void initInstrItins(InstrItineraryData &InstrItins) const;
267
268 /// Resolve a variant scheduling class for the given MCInst and CPU.
269 virtual unsigned resolveVariantSchedClass(unsigned SchedClass,
270 const MCInst *MI,
271 const MCInstrInfo *MCII,
272 unsigned CPUID) const {
273 return 0;
274 }
275
276 /// Check whether the CPU string is valid.
277 virtual bool isCPUStringValid(StringRef CPU) const {
278 auto Found = llvm::lower_bound(ProcDesc, CPU);
279 return Found != ProcDesc.end() && StringRef(Found->key()) == CPU;
280 }
281
282 /// Return processor descriptions.
284 return ProcDesc;
285 }
286
287 /// Return processor features.
289 return ProcFeatures;
290 }
291
292 /// Return the list of processor features currently enabled.
293 std::vector<const SubtargetFeatureKV *> getEnabledProcessorFeatures() const;
294
295 /// HwMode IDs are stored and accessed in a bit set format, enabling
296 /// users to efficiently retrieve specific IDs, such as the RegInfo
297 /// HwMode ID, from the set as required. Using this approach, various
298 /// types of HwMode IDs can be added to a subtarget to manage different
299 /// attributes within that subtarget, significantly enhancing the
300 /// scalability and usability of HwMode. Moreover, to ensure compatibility,
301 /// this method also supports controlling multiple attributes with a single
302 /// HwMode ID, just as was done previously.
304 HwMode_Default, // Return the smallest HwMode ID of current subtarget.
305 HwMode_ValueType, // Return the HwMode ID that controls the ValueType.
306 HwMode_RegInfo, // Return the HwMode ID that controls the RegSizeInfo,
307 // SubRegRange, and RegisterClass.
308 HwMode_EncodingInfo // Return the HwMode ID that controls the EncodingInfo.
309 };
310
311 /// Return a bit set containing all HwMode IDs of the current subtarget.
312 virtual unsigned getHwModeSet() const { return 0; }
313
314 /// HwMode ID corresponding to the 'type' parameter is retrieved from the
315 /// HwMode bit set of the current subtarget. It’s important to note that if
316 /// the current subtarget possesses two HwMode IDs and both control a single
317 /// attribute (such as RegInfo), this interface will result in an error.
318 virtual unsigned getHwMode(enum HwModeType type = HwMode_Default) const {
319 return 0;
320 }
321
322 /// Return the cache size in bytes for the given level of cache.
323 /// Level is zero-based, so a value of zero means the first level of
324 /// cache.
325 ///
326 virtual std::optional<unsigned> getCacheSize(unsigned Level) const;
327
328 /// Return the cache associatvity for the given level of cache.
329 /// Level is zero-based, so a value of zero means the first level of
330 /// cache.
331 ///
332 virtual std::optional<unsigned> getCacheAssociativity(unsigned Level) const;
333
334 /// Return the target cache line size in bytes at a given level.
335 ///
336 virtual std::optional<unsigned> getCacheLineSize(unsigned Level) const;
337
338 /// Return the target cache line size in bytes. By default, return
339 /// the line size for the bottom-most level of cache. This provides
340 /// a more convenient interface for the common case where all cache
341 /// levels have the same line size. Return zero if there is no
342 /// cache model.
343 ///
344 virtual unsigned getCacheLineSize() const {
345 std::optional<unsigned> Size = getCacheLineSize(0);
346 if (Size)
347 return *Size;
348
349 return 0;
350 }
351
352 /// Return the preferred prefetch distance in terms of instructions.
353 ///
354 virtual unsigned getPrefetchDistance() const;
355
356 /// Return the maximum prefetch distance in terms of loop
357 /// iterations.
358 ///
359 virtual unsigned getMaxPrefetchIterationsAhead() const;
360
361 /// \return True if prefetching should also be done for writes.
362 ///
363 virtual bool enableWritePrefetching() const;
364
365 /// Return the minimum stride necessary to trigger software
366 /// prefetching.
367 ///
368 virtual unsigned getMinPrefetchStride(unsigned NumMemAccesses,
369 unsigned NumStridedMemAccesses,
370 unsigned NumPrefetches,
371 bool HasCall) const;
372
373 /// \return if target want to issue a prefetch in address space \p AS.
374 virtual bool shouldPrefetchAddressSpace(unsigned AS) const;
375};
376
377} // end namespace llvm
378
379#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:215
IRTranslator LLVM IR MI
static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature, ArrayRef< SubtargetFeatureKV > FeatureTable)
#define I(x, y, z)
Definition MD5.cpp:57
SI optimize exec mask operations pre RA
This file contains some templates that are useful if you are working with the STL at all.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
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
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
virtual std::optional< unsigned > getCacheLineSize(unsigned Level) const
Return the target cache line size in bytes at a given level.
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
A table of densely packed, null-terminated strings indexed by offset.
Definition StringTable.h:34
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
This is an optimization pass for GlobalISel generic memory operations.
@ Other
Any other memory.
Definition ModRef.h:68
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:2052
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:114
Summarize the scheduling resources required for an instruction of a particular scheduling class.
Definition MCSchedule.h:129
uint16_t NumReadAdvanceEntries
Definition MCSchedule.h:145
uint16_t NumWriteProcResEntries
Definition MCSchedule.h:141
Machine model for scheduling, bundling, and heuristics.
Definition MCSchedule.h:264
Specify the latency in cpu cycles for a particular scheduling class and def index.
Definition MCSchedule.h:97
Identify one of the processor resource kinds consumed by a particular scheduling class for the specif...
Definition MCSchedule.h:74
SubtargetFeatureKV Features[NumFeatures]
char Strings[FeatureStrTabSize]
Used to provide key value pairs for feature and CPU bit flags.
const char * key() const
SubtargetFeatureKV(const SubtargetFeatureKV &)=delete
bool operator<(StringRef S) const
Compare routine for std::lower_bound.
bool operator<(const SubtargetFeatureKV &Other) const
Compare routine for std::is_sorted.
constexpr SubtargetFeatureKV(uint16_t KeyStrOff, uint16_t DescStrOff, unsigned Value, FeatureBitArray Implies)
const char * desc() const
unsigned Value
K-V integer value.
FeatureBitArray Implies
K-V bit mask.
SubtargetFeatureKV & operator=(const SubtargetFeatureKV &)=delete
char Strings[SubTypeStrTabSize]
SubtargetSubTypeKV SubTypes[NumSubTypes]
Used to provide key value pairs for feature and CPU bit flags.
SubtargetSubTypeKV(const SubtargetSubTypeKV &)=delete
const char * key() const
FeatureBitArray Implies
K-V bit mask.
FeatureBitArray TuneImplies
K-V bit mask.
bool operator<(const SubtargetSubTypeKV &Other) const
Compare routine for std::is_sorted.
SubtargetSubTypeKV & operator=(const SubtargetSubTypeKV &)=delete
constexpr SubtargetSubTypeKV(uint16_t KeyStrOff, FeatureBitArray Implies, FeatureBitArray TuneImplies, unsigned SchedModelIdx)
bool operator<(StringRef S) const
Compare routine for std::lower_bound.