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