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