1//===- MCSubtargetInfo.cpp - Subtarget Information ------------------------===//
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#include "llvm/MC/MCSubtargetInfo.h"
10#include "llvm/ADT/ArrayRef.h"
11#include "llvm/ADT/StringRef.h"
12#include "llvm/ADT/Twine.h"
13#include "llvm/MC/MCInstrItineraries.h"
14#include "llvm/MC/MCSchedule.h"
15#include "llvm/Support/Format.h"
16#include "llvm/Support/raw_ostream.h"
17#include "llvm/TargetParser/SubtargetFeature.h"
18#include <algorithm>
19#include <cassert>
20#include <cstring>
21#include <optional>
22
23using namespace llvm;
24
25/// Find KV in array using binary search.
26template <typename T>
27static const T *Find(StringRef S, ArrayRef<T> A) {
28 // Binary search the array
29 auto F = llvm::lower_bound(A, S);
30 // If not found then return NULL
31 if (F == A.end() || StringRef(F->key()) != S)
32 return nullptr;
33 // Return the found array item
34 return F;
35}
36
37/// For each feature that is (transitively) implied by this feature, set it.
38static void SetImpliedBits(FeatureBitset &Bits, FeatureBitset Implies,
39 ArrayRef<SubtargetFeatureKV> FeatureTable) {
40 // Transitively set all features implied. We don't assume that the features in
41 // Bits have already had their implied features set.
42 FeatureBitset NewBits = Implies;
43 while (Implies.any()) {
44 FeatureBitset Implied;
45 for (const SubtargetFeatureKV &FE : FeatureTable) {
46 if (Implies.test(I: FE.Value))
47 Implied |= FE.Implies.getAsBitset();
48 }
49
50 // Only continue for bits that haven't been set yet.
51 Implies = Implied & ~NewBits;
52 NewBits |= Implies;
53 }
54 Bits |= NewBits;
55}
56
57/// For each feature that (transitively) implies this feature, clear it.
58static
59void ClearImpliedBits(FeatureBitset &Bits, unsigned Value,
60 ArrayRef<SubtargetFeatureKV> FeatureTable) {
61 for (const SubtargetFeatureKV &FE : FeatureTable) {
62 if (FE.Implies.getAsBitset().test(I: Value)) {
63 Bits.reset(I: FE.Value);
64 ClearImpliedBits(Bits, Value: FE.Value, FeatureTable);
65 }
66 }
67}
68
69static void ApplyFeatureFlag(FeatureBitset &Bits, StringRef Feature,
70 ArrayRef<SubtargetFeatureKV> FeatureTable) {
71 assert(SubtargetFeatures::hasFlag(Feature) &&
72 "Feature flags should start with '+' or '-'");
73
74 // Find feature in table.
75 const SubtargetFeatureKV *FeatureEntry =
76 Find(S: SubtargetFeatures::StripFlag(Feature), A: FeatureTable);
77 // If there is a match
78 if (FeatureEntry) {
79 // Enable/disable feature in bits
80 if (SubtargetFeatures::isEnabled(Feature)) {
81 Bits.set(FeatureEntry->Value);
82
83 // For each feature that this implies, set it.
84 SetImpliedBits(Bits, Implies: FeatureEntry->Implies.getAsBitset(), FeatureTable);
85 } else {
86 Bits.reset(I: FeatureEntry->Value);
87
88 // For each feature that implies this, clear it.
89 ClearImpliedBits(Bits, Value: FeatureEntry->Value, FeatureTable);
90 }
91 } else {
92 errs() << "'" << Feature << "' is not a recognized feature for this target"
93 << " (ignoring feature)\n";
94 }
95}
96
97/// Return the length of the longest entry in the table.
98static size_t getLongestEntryLength(ArrayRef<SubtargetFeatureKV> Table) {
99 size_t MaxLen = 0;
100 for (auto &I : Table)
101 MaxLen = std::max(a: MaxLen, b: std::strlen(s: I.key()));
102 return MaxLen;
103}
104
105static size_t getLongestEntryLength(StringTable Table) {
106 size_t MaxLen = 0;
107 for (StringRef I : Table)
108 MaxLen = std::max(a: MaxLen, b: I.size());
109 return MaxLen;
110}
111
112/// Display help for feature and mcpu choices.
113static void Help(StringTable CPUNames, ArrayRef<SubtargetFeatureKV> FeatTable) {
114 // the static variable ensures that the help information only gets
115 // printed once even though a target machine creates multiple subtargets
116 static bool PrintOnce = false;
117 if (PrintOnce) {
118 return;
119 }
120
121 // Determine the length of the longest CPU and Feature entries.
122 unsigned MaxCPULen = getLongestEntryLength(Table: CPUNames);
123 unsigned MaxFeatLen = getLongestEntryLength(Table: FeatTable);
124
125 // Print the CPU table.
126 errs() << "Available CPUs for this target:\n\n";
127 for (auto &CPUName : drop_begin(RangeOrContainer&: CPUNames)) {
128 // Skip apple-latest, as that's only meant to be used in
129 // disassemblers/debuggers, and we don't want normal code to be built with
130 // it as an -mcpu=
131 if (CPUName == "apple-latest")
132 continue;
133 errs() << format(Fmt: " %-*s - Select the %s processor.\n", Vals: MaxCPULen,
134 Vals: CPUName.str().c_str(), Vals: CPUName.str().c_str());
135 }
136 errs() << '\n';
137
138 // Print the Feature table.
139 errs() << "Available features for this target:\n\n";
140 for (auto &Feature : FeatTable)
141 errs() << format(Fmt: " %-*s - %s.\n", Vals: MaxFeatLen, Vals: Feature.key(),
142 Vals: Feature.desc());
143 errs() << '\n';
144
145 errs() << "Use +feature to enable a feature, or -feature to disable it.\n"
146 "For example, llc -mcpu=mycpu -mattr=+feature1,-feature2\n";
147
148 PrintOnce = true;
149}
150
151/// Display help for mcpu choices only
152static void cpuHelp(StringTable CPUNames) {
153 // the static variable ensures that the help information only gets
154 // printed once even though a target machine creates multiple subtargets
155 static bool PrintOnce = false;
156 if (PrintOnce) {
157 return;
158 }
159
160 // Print the CPU table.
161 errs() << "Available CPUs for this target:\n\n";
162 for (auto &CPU : llvm::drop_begin(RangeOrContainer&: CPUNames)) {
163 // Skip apple-latest, as that's only meant to be used in
164 // disassemblers/debuggers, and we don't want normal code to be built with
165 // it as an -mcpu=
166 if (CPU == "apple-latest")
167 continue;
168 errs() << "\t" << CPU << "\n";
169 }
170 errs() << '\n';
171
172 errs() << "Use -mcpu or -mtune to specify the target's processor.\n"
173 "For example, clang --target=aarch64-unknown-linux-gnu "
174 "-mcpu=cortex-a35\n";
175
176 PrintOnce = true;
177}
178
179static FeatureBitset getFeatures(MCSubtargetInfo &STI, StringRef CPU,
180 StringRef TuneCPU, StringRef FS,
181 StringTable ProcNames,
182 ArrayRef<SubtargetSubTypeKV> ProcDesc,
183 ArrayRef<SubtargetFeatureKV> ProcFeatures) {
184 SubtargetFeatures Features(FS);
185
186 if (ProcDesc.empty() || ProcFeatures.empty())
187 return FeatureBitset();
188
189 assert(llvm::is_sorted(ProcDesc) && "CPU table is not sorted");
190 assert(llvm::is_sorted(ProcFeatures) && "CPU features table is not sorted");
191 // Resulting bits
192 FeatureBitset Bits;
193
194 // Check if help is needed
195 if (CPU == "help")
196 Help(CPUNames: ProcNames, FeatTable: ProcFeatures);
197
198 // Find CPU entry if CPU name is specified.
199 else if (!CPU.empty()) {
200 const SubtargetSubTypeKV *CPUEntry = Find(S: CPU, A: ProcDesc);
201
202 // If there is a match
203 if (CPUEntry) {
204 // Set the features implied by this CPU feature, if any.
205 SetImpliedBits(Bits, Implies: CPUEntry->Implies.getAsBitset(), FeatureTable: ProcFeatures);
206 } else {
207 errs() << "'" << CPU << "' is not a recognized processor for this target"
208 << " (ignoring processor)\n";
209 }
210 }
211
212 if (!TuneCPU.empty()) {
213 const SubtargetSubTypeKV *CPUEntry = Find(S: TuneCPU, A: ProcDesc);
214
215 // If there is a match
216 if (CPUEntry) {
217 // Set the features implied by this CPU feature, if any.
218 SetImpliedBits(Bits, Implies: CPUEntry->TuneImplies.getAsBitset(), FeatureTable: ProcFeatures);
219 } else if (TuneCPU != CPU) {
220 errs() << "'" << TuneCPU << "' is not a recognized processor for this "
221 << "target (ignoring processor)\n";
222 }
223 }
224
225 // Iterate through each feature
226 for (const std::string &Feature : Features.getFeatures()) {
227 // Check for help
228 if (Feature == "+help")
229 Help(CPUNames: ProcNames, FeatTable: ProcFeatures);
230 else if (Feature == "+cpuhelp")
231 cpuHelp(CPUNames: ProcNames);
232 else
233 ApplyFeatureFlag(Bits, Feature, FeatureTable: ProcFeatures);
234 }
235
236 return Bits;
237}
238
239void MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef TuneCPU,
240 StringRef FS) {
241 FeatureBits =
242 getFeatures(STI&: *this, CPU, TuneCPU, FS, ProcNames, ProcDesc, ProcFeatures);
243 FeatureString = std::string(FS);
244
245 if (!TuneCPU.empty())
246 CPUSchedModel = &getSchedModelForCPU(CPU: TuneCPU);
247 else
248 CPUSchedModel = &MCSchedModel::Default;
249}
250
251void MCSubtargetInfo::setDefaultFeatures(StringRef CPU, StringRef TuneCPU,
252 StringRef FS) {
253 FeatureBits =
254 getFeatures(STI&: *this, CPU, TuneCPU, FS, ProcNames, ProcDesc, ProcFeatures);
255 FeatureString = std::string(FS);
256}
257
258MCSubtargetInfo::MCSubtargetInfo(
259 const Triple &TT, StringRef C, StringRef TC, StringRef FS, StringTable PN,
260 ArrayRef<SubtargetFeatureKV> PF, ArrayRef<SubtargetSubTypeKV> PD,
261 const MCSchedModel *PSM, const MCWriteProcResEntry *WPR,
262 const MCWriteLatencyEntry *WL, const MCReadAdvanceEntry *RA,
263 const InstrStage *IS, const unsigned *OC, const unsigned *FP)
264 : TargetTriple(TT), CPU(std::string(C)), TuneCPU(std::string(TC)),
265 ProcNames(PN), ProcFeatures(PF), ProcDesc(PD), ProcSchedModels(PSM),
266 WriteProcResTable(WPR), WriteLatencyTable(WL), ReadAdvanceTable(RA),
267 Stages(IS), OperandCycles(OC), ForwardingPaths(FP) {
268 InitMCProcessorInfo(CPU, TuneCPU, FS);
269}
270
271const FeatureBitset &MCSubtargetInfo::ToggleFeature(uint64_t FB) {
272 FeatureBits.flip(I: FB);
273 return FeatureBits;
274}
275
276const FeatureBitset &MCSubtargetInfo::ToggleFeature(const FeatureBitset &FB) {
277 FeatureBits ^= FB;
278 return FeatureBits;
279}
280
281const FeatureBitset &
282MCSubtargetInfo::SetFeatureBitsTransitively(const FeatureBitset &FB) {
283 SetImpliedBits(Bits&: FeatureBits, Implies: FB, FeatureTable: ProcFeatures);
284 return FeatureBits;
285}
286
287const FeatureBitset &
288MCSubtargetInfo::ClearFeatureBitsTransitively(const FeatureBitset &FB) {
289 for (unsigned I = 0, E = FB.size(); I < E; I++) {
290 if (FB[I]) {
291 FeatureBits.reset(I);
292 ClearImpliedBits(Bits&: FeatureBits, Value: I, FeatureTable: ProcFeatures);
293 }
294 }
295 return FeatureBits;
296}
297
298const FeatureBitset &MCSubtargetInfo::ToggleFeature(StringRef Feature) {
299 // Find feature in table.
300 const SubtargetFeatureKV *FeatureEntry =
301 Find(S: SubtargetFeatures::StripFlag(Feature), A: ProcFeatures);
302 // If there is a match
303 if (FeatureEntry) {
304 if (FeatureBits.test(I: FeatureEntry->Value)) {
305 FeatureBits.reset(I: FeatureEntry->Value);
306 // For each feature that implies this, clear it.
307 ClearImpliedBits(Bits&: FeatureBits, Value: FeatureEntry->Value, FeatureTable: ProcFeatures);
308 } else {
309 FeatureBits.set(FeatureEntry->Value);
310
311 // For each feature that this implies, set it.
312 SetImpliedBits(Bits&: FeatureBits, Implies: FeatureEntry->Implies.getAsBitset(),
313 FeatureTable: ProcFeatures);
314 }
315 } else {
316 errs() << "'" << Feature << "' is not a recognized feature for this target"
317 << " (ignoring feature)\n";
318 }
319
320 return FeatureBits;
321}
322
323const FeatureBitset &MCSubtargetInfo::ApplyFeatureFlag(StringRef FS) {
324 ::ApplyFeatureFlag(Bits&: FeatureBits, Feature: FS, FeatureTable: ProcFeatures);
325 return FeatureBits;
326}
327
328bool MCSubtargetInfo::checkFeatures(StringRef FS) const {
329 SubtargetFeatures T(FS);
330 return all_of(Range: T.getFeatures(), P: [this](const std::string &F) {
331 assert(SubtargetFeatures::hasFlag(F) &&
332 "Feature flags should start with '+' or '-'");
333 const SubtargetFeatureKV *FeatureEntry =
334 Find(S: SubtargetFeatures::StripFlag(Feature: F), A: ProcFeatures);
335 if (!FeatureEntry) {
336 reportFatalInternalError(reason: Twine("'") + F +
337 "' is not a recognized feature for this target");
338 }
339
340 return FeatureBits.test(I: FeatureEntry->Value) ==
341 SubtargetFeatures::isEnabled(Feature: F);
342 });
343}
344
345static bool hasFeature(StringRef Feature, const FeatureBitset &FeatureBits,
346 ArrayRef<SubtargetFeatureKV> ProcFeatures) {
347 bool ShouldBeEnabled = true;
348 if (!Feature.consume_front(Prefix: "+") && Feature.consume_front(Prefix: "-"))
349 ShouldBeEnabled = false;
350
351 const SubtargetFeatureKV *FeatureEntry = Find(S: Feature, A: ProcFeatures);
352 if (!FeatureEntry) {
353 reportFatalInternalError(reason: Twine("'") + Feature +
354 "' is not a recognized feature for this target");
355 }
356
357 return FeatureBits.test(I: FeatureEntry->Value) == ShouldBeEnabled;
358}
359
360namespace {
361class FeatureExpressionParser {
362 StringRef Expr;
363 const FeatureBitset &FeatureBits;
364 ArrayRef<SubtargetFeatureKV> ProcFeatures;
365 size_t Pos = 0;
366
367public:
368 FeatureExpressionParser(StringRef Expr, const FeatureBitset &FeatureBits,
369 ArrayRef<SubtargetFeatureKV> ProcFeatures)
370 : Expr(Expr), FeatureBits(FeatureBits), ProcFeatures(ProcFeatures) {}
371
372 bool parse() {
373 bool Result = parseOr();
374 if (Pos != Expr.size())
375 reportFatalInternalError(reason: "malformed target feature expression");
376 return Result;
377 }
378
379private:
380 bool consume(char C) {
381 if (Pos == Expr.size() || Expr[Pos] != C)
382 return false;
383 ++Pos;
384 return true;
385 }
386
387 bool parseOr() {
388 bool Result = parseAnd();
389 while (consume(C: '|')) {
390 bool RHS = parseAnd();
391 Result |= RHS;
392 }
393 return Result;
394 }
395
396 bool parseAnd() {
397 bool Result = parsePrimary();
398 while (consume(C: ',')) {
399 bool RHS = parsePrimary();
400 Result &= RHS;
401 }
402 return Result;
403 }
404
405 bool parsePrimary() {
406 if (consume(C: '(')) {
407 bool Result = parseOr();
408 if (!consume(C: ')'))
409 reportFatalInternalError(reason: "malformed target feature expression");
410 return Result;
411 }
412
413 size_t Start = Pos;
414 Pos = Expr.find_first_of(Chars: ",|()", From: Pos);
415 if (Pos == StringRef::npos)
416 Pos = Expr.size();
417
418 if (Start == Pos)
419 reportFatalInternalError(reason: "malformed target feature expression");
420
421 return hasFeature(Feature: Expr.slice(Start, End: Pos), FeatureBits, ProcFeatures);
422 }
423};
424} // namespace
425
426bool MCSubtargetInfo::checkFeatureExpression(StringRef FeatureExpr) const {
427 if (FeatureExpr.empty())
428 return true;
429 if (FeatureExpr.contains(C: ' ')) {
430 reportFatalInternalError(
431 reason: "spaces are not allowed in target feature expressions");
432 }
433 FeatureExpressionParser Parser(FeatureExpr, FeatureBits, ProcFeatures);
434 return Parser.parse();
435}
436
437const MCSchedModel &MCSubtargetInfo::getSchedModelForCPU(StringRef CPU) const {
438 assert(llvm::is_sorted(ProcDesc) &&
439 "Processor machine model table is not sorted");
440
441 // Find entry
442 const SubtargetSubTypeKV *CPUEntry = Find(S: CPU, A: ProcDesc);
443
444 if (!CPUEntry) {
445 if (CPU != "help") // Don't error if the user asked for help.
446 errs() << "'" << CPU
447 << "' is not a recognized processor for this target"
448 << " (ignoring processor)\n";
449 return MCSchedModel::Default;
450 }
451 return ProcSchedModels[CPUEntry->SchedModelIdx];
452}
453
454InstrItineraryData
455MCSubtargetInfo::getInstrItineraryForCPU(StringRef CPU) const {
456 const MCSchedModel &SchedModel = getSchedModelForCPU(CPU);
457 return InstrItineraryData(SchedModel, Stages, OperandCycles, ForwardingPaths);
458}
459
460void MCSubtargetInfo::initInstrItins(InstrItineraryData &InstrItins) const {
461 InstrItins = InstrItineraryData(getSchedModel(), Stages, OperandCycles,
462 ForwardingPaths);
463}
464
465std::vector<const SubtargetFeatureKV *>
466MCSubtargetInfo::getEnabledProcessorFeatures() const {
467 std::vector<const SubtargetFeatureKV *> EnabledFeatures;
468 for (const SubtargetFeatureKV &FeatureKV : ProcFeatures)
469 if (FeatureBits.test(I: FeatureKV.Value))
470 EnabledFeatures.push_back(x: &FeatureKV);
471 return EnabledFeatures;
472}
473
474std::optional<unsigned> MCSubtargetInfo::getCacheSize(unsigned Level) const {
475 return std::nullopt;
476}
477
478std::optional<unsigned>
479MCSubtargetInfo::getCacheAssociativity(unsigned Level) const {
480 return std::nullopt;
481}
482
483std::optional<unsigned>
484MCSubtargetInfo::getCacheLineSize(unsigned Level) const {
485 return std::nullopt;
486}
487
488unsigned MCSubtargetInfo::getPrefetchDistance() const {
489 return 0;
490}
491
492unsigned MCSubtargetInfo::getMaxPrefetchIterationsAhead() const {
493 return UINT_MAX;
494}
495
496bool MCSubtargetInfo::enableWritePrefetching() const {
497 return false;
498}
499
500unsigned MCSubtargetInfo::getMinPrefetchStride(unsigned NumMemAccesses,
501 unsigned NumStridedMemAccesses,
502 unsigned NumPrefetches,
503 bool HasCall) const {
504 return 1;
505}
506
507bool MCSubtargetInfo::shouldPrefetchAddressSpace(unsigned AS) const {
508 return !AS;
509}
510