[llvm-exegesis] [NFC] Fixing typo.
[llvm-complete.git] / utils / TableGen / SubtargetFeatureInfo.cpp
blobb7ed23fff877de4160a2df1eda72e63ea5b769b2
1 //===- SubtargetFeatureInfo.cpp - Helpers for subtarget features ----------===//
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 //===----------------------------------------------------------------------===//
9 #include "SubtargetFeatureInfo.h"
11 #include "Types.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/TableGen/Record.h"
15 #include <map>
17 using namespace llvm;
19 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
20 LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const {
21 errs() << getEnumName() << " " << Index << "\n" << *TheDef;
23 #endif
25 std::vector<std::pair<Record *, SubtargetFeatureInfo>>
26 SubtargetFeatureInfo::getAll(const RecordKeeper &Records) {
27 std::vector<std::pair<Record *, SubtargetFeatureInfo>> SubtargetFeatures;
28 std::vector<Record *> AllPredicates =
29 Records.getAllDerivedDefinitions("Predicate");
30 for (Record *Pred : AllPredicates) {
31 // Ignore predicates that are not intended for the assembler.
33 // The "AssemblerMatcherPredicate" string should be promoted to an argument
34 // if we re-use the machinery for non-assembler purposes in future.
35 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
36 continue;
38 if (Pred->getName().empty())
39 PrintFatalError(Pred->getLoc(), "Predicate has no name!");
41 SubtargetFeatures.emplace_back(
42 Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));
44 return SubtargetFeatures;
47 void SubtargetFeatureInfo::emitSubtargetFeatureFlagEnumeration(
48 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
49 OS << "// Flags for subtarget features that participate in "
50 << "instruction matching.\n";
51 OS << "enum SubtargetFeatureFlag : "
52 << getMinimalTypeForEnumBitfield(SubtargetFeatures.size()) << " {\n";
53 for (const auto &SF : SubtargetFeatures) {
54 const SubtargetFeatureInfo &SFI = SF.second;
55 OS << " " << SFI.getEnumName() << " = (1ULL << " << SFI.Index << "),\n";
57 OS << " Feature_None = 0\n";
58 OS << "};\n\n";
61 void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
62 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
63 OS << "// Bits for subtarget features that participate in "
64 << "instruction matching.\n";
65 OS << "enum SubtargetFeatureBits : "
66 << getMinimalTypeForRange(SubtargetFeatures.size()) << " {\n";
67 for (const auto &SF : SubtargetFeatures) {
68 const SubtargetFeatureInfo &SFI = SF.second;
69 OS << " " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n";
71 OS << "};\n\n";
74 void SubtargetFeatureInfo::emitNameTable(
75 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
76 // Need to sort the name table so that lookup by the log of the enum value
77 // gives the proper name. More specifically, for a feature of value 1<<n,
78 // SubtargetFeatureNames[n] should be the name of the feature.
79 uint64_t IndexUB = 0;
80 for (const auto &SF : SubtargetFeatures)
81 if (IndexUB <= SF.second.Index)
82 IndexUB = SF.second.Index+1;
84 std::vector<std::string> Names;
85 if (IndexUB > 0)
86 Names.resize(IndexUB);
87 for (const auto &SF : SubtargetFeatures)
88 Names[SF.second.Index] = SF.second.getEnumName();
90 OS << "static const char *SubtargetFeatureNames[] = {\n";
91 for (uint64_t I = 0; I < IndexUB; ++I)
92 OS << " \"" << Names[I] << "\",\n";
94 // A small number of targets have no predicates. Null terminate the array to
95 // avoid a zero-length array.
96 OS << " nullptr\n"
97 << "};\n\n";
100 void SubtargetFeatureInfo::emitComputeAvailableFeatures(
101 StringRef TargetName, StringRef ClassName, StringRef FuncName,
102 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
103 StringRef ExtraParams) {
104 OS << "PredicateBitset " << TargetName << ClassName << "::\n"
105 << FuncName << "(const " << TargetName << "Subtarget *Subtarget";
106 if (!ExtraParams.empty())
107 OS << ", " << ExtraParams;
108 OS << ") const {\n";
109 OS << " PredicateBitset Features;\n";
110 for (const auto &SF : SubtargetFeatures) {
111 const SubtargetFeatureInfo &SFI = SF.second;
113 OS << " if (" << SFI.TheDef->getValueAsString("CondString") << ")\n";
114 OS << " Features[" << SFI.getEnumBitName() << "] = 1;\n";
116 OS << " return Features;\n";
117 OS << "}\n\n";
120 void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
121 StringRef TargetName, StringRef ClassName, StringRef FuncName,
122 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
123 OS << "uint64_t " << TargetName << ClassName << "::\n"
124 << FuncName << "(const FeatureBitset& FB) const {\n";
125 OS << " uint64_t Features = 0;\n";
126 for (const auto &SF : SubtargetFeatures) {
127 const SubtargetFeatureInfo &SFI = SF.second;
129 OS << " if (";
130 std::string CondStorage =
131 SFI.TheDef->getValueAsString("AssemblerCondString");
132 StringRef Conds = CondStorage;
133 std::pair<StringRef, StringRef> Comma = Conds.split(',');
134 bool First = true;
135 do {
136 if (!First)
137 OS << " && ";
139 bool Neg = false;
140 StringRef Cond = Comma.first;
141 if (Cond[0] == '!') {
142 Neg = true;
143 Cond = Cond.substr(1);
146 OS << "(";
147 if (Neg)
148 OS << "!";
149 OS << "FB[" << TargetName << "::" << Cond << "])";
151 if (Comma.second.empty())
152 break;
154 First = false;
155 Comma = Comma.second.split(',');
156 } while (true);
158 OS << ")\n";
159 OS << " Features |= " << SFI.getEnumName() << ";\n";
161 OS << " return Features;\n";
162 OS << "}\n\n";