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