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