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//===----------------------------------------------------------------------===//
8
9#include "SubtargetFeatureInfo.h"
10#include "Basic/PredicateExpanderDag.h"
11#include "Types.h"
12#include "llvm/Config/llvm-config.h"
13#include "llvm/TableGen/Error.h"
14#include "llvm/TableGen/Record.h"
15
16using namespace llvm;
17
18#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
19LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const {
20 errs() << getEnumName() << " " << Index << "\n" << *TheDef;
21}
22#endif
23
24SubtargetFeaturesInfoVec
25SubtargetFeatureInfo::getAll(const RecordKeeper &Records) {
26 SubtargetFeaturesInfoVec SubtargetFeatures;
27 for (const Record *Pred : Records.getAllDerivedDefinitions(ClassName: "Predicate")) {
28 // Ignore predicates that are not intended for the assembler.
29 //
30 // The "AssemblerMatcherPredicate" string should be promoted to an argument
31 // if we re-use the machinery for non-assembler purposes in future.
32 if (!Pred->getValueAsBit(FieldName: "AssemblerMatcherPredicate"))
33 continue;
34
35 if (Pred->getName().empty())
36 PrintFatalError(ErrorLoc: Pred->getLoc(), Msg: "Predicate has no name!");
37
38 // Ignore always true predicates.
39 if (Pred->getValueAsString(FieldName: "CondString").empty())
40 continue;
41
42 SubtargetFeatures.emplace_back(
43 args&: Pred, args: SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));
44 }
45 return SubtargetFeatures;
46}
47
48void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
49 const SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
50 const std::map<std::string, unsigned> *HwModes) {
51 OS << "// Bits for subtarget features that participate in "
52 << "instruction matching.\n";
53 unsigned Size = SubtargetFeatures.size();
54 if (HwModes)
55 Size += HwModes->size();
56
57 OS << "enum SubtargetFeatureBits : " << getMinimalTypeForRange(Range: Size)
58 << " {\n";
59 for (const auto &SF : SubtargetFeatures) {
60 const SubtargetFeatureInfo &SFI = SF.second;
61 OS << " " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n";
62 }
63
64 if (HwModes) {
65 unsigned Offset = SubtargetFeatures.size();
66 for (const auto &M : *HwModes) {
67 OS << " Feature_HwMode" << M.second << "Bit = " << (M.second + Offset)
68 << ",\n";
69 }
70 }
71
72 OS << "};\n\n";
73}
74
75void SubtargetFeatureInfo::emitNameTable(
76 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
77 // Need to sort the name table so that lookup by the log of the enum value
78 // gives the proper name. More specifically, for a feature of value 1<<n,
79 // SubtargetFeatureNames[n] should be the name of the feature.
80 uint64_t IndexUB = 0;
81 for (const auto &SF : SubtargetFeatures)
82 if (IndexUB <= SF.second.Index)
83 IndexUB = SF.second.Index + 1;
84
85 std::vector<std::string> Names;
86 if (IndexUB > 0)
87 Names.resize(new_size: IndexUB);
88 for (const auto &SF : SubtargetFeatures)
89 Names[SF.second.Index] = SF.second.getEnumName();
90
91 OS << "static const char *SubtargetFeatureNames[] = {\n";
92 for (uint64_t I = 0; I < IndexUB; ++I)
93 OS << " \"" << Names[I] << "\",\n";
94
95 // A small number of targets have no predicates. Null terminate the array to
96 // avoid a zero-length array.
97 OS << " nullptr\n"
98 << "};\n\n";
99}
100
101void SubtargetFeatureInfo::emitComputeAvailableFeatures(
102 StringRef TargetName, StringRef ClassName, StringRef FuncName,
103 const SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
104 StringRef ExtraParams, const std::map<std::string, unsigned> *HwModes) {
105 OS << "PredicateBitset " << ClassName << "::\n"
106 << FuncName << "(const " << TargetName << "Subtarget *Subtarget";
107 if (!ExtraParams.empty())
108 OS << ", " << ExtraParams;
109 OS << ") const {\n";
110 OS << " PredicateBitset Features{};\n";
111 for (const auto &SF : SubtargetFeatures) {
112 const SubtargetFeatureInfo &SFI = SF.second;
113 StringRef CondStr = SFI.TheDef->getValueAsString(FieldName: "CondString");
114 assert(!CondStr.empty() && "true predicate should have been filtered");
115
116 OS << " if (" << CondStr << ")\n";
117 OS << " Features.set(" << SFI.getEnumBitName() << ");\n";
118 }
119
120 if (HwModes) {
121 for (const auto &M : *HwModes) {
122 OS << " if (" << M.first << ")\n";
123 OS << " Features.set(Feature_HwMode" << M.second << "Bit);\n";
124 }
125 }
126
127 OS << " return Features;\n";
128 OS << "}\n\n";
129}
130
131// Emit a feature-bit test leaf: FB[<TargetName>::<feature>]. Returns true on
132// error (the leaf is not a SubtargetFeature).
133static bool emitFeatureBitLeaf(StringRef TargetName, const Init &Val,
134 raw_ostream &OS) {
135 const auto *D = dyn_cast<DefInit>(Val: &Val);
136 if (!D || !D->getDef()->isSubClassOf(Name: "SubtargetFeature"))
137 return true;
138 OS << "FB[" << TargetName << "::" << D->getAsString() << ']';
139 return false;
140}
141
142// If ParenIfBinOp is true, print a surrounding () if Val uses && or ||.
143// Structural errors in the dag are diagnosed against \p Owner by the walker;
144// a leaf that is not a SubtargetFeature is reported here via the true return.
145static bool emitFeaturesAux(const Record *Owner, StringRef TargetName,
146 const Init &Val, bool ParenIfBinOp,
147 raw_ostream &OS) {
148 return emitPredicateDag(Owner, Val, ParenIfBinOp, OS,
149 EmitLeaf: [&](const Init &Leaf, raw_ostream &OS) {
150 return emitFeatureBitLeaf(TargetName, Val: Leaf, OS);
151 });
152}
153
154void SubtargetFeatureInfo::emitPredicateCheck(
155 raw_ostream &OS, ArrayRef<const Record *> Predicates) {
156 ListSeparator LS(" && ");
157 for (const Record *R : Predicates) {
158 StringRef CondString = R->getValueAsString(FieldName: "CondString");
159 if (CondString.empty())
160 continue;
161 OS << LS << '(' << CondString << ')';
162 }
163}
164
165void SubtargetFeatureInfo::emitMCPredicateCheck(
166 raw_ostream &OS, StringRef TargetName,
167 ArrayRef<const Record *> Predicates) {
168 auto MCPredicates = make_filter_range(Range&: Predicates, Pred: [](const Record *R) {
169 return R->getValueAsBit(FieldName: "AssemblerMatcherPredicate");
170 });
171
172 if (MCPredicates.empty()) {
173 OS << "false";
174 return;
175 }
176
177 ListSeparator LS(" && ");
178 bool ParenIfBinOp = range_size(Range&: MCPredicates) > 1;
179 for (const Record *R : MCPredicates) {
180 OS << LS;
181 if (emitFeaturesAux(Owner: R, TargetName, Val: *R->getValueAsDag(FieldName: "AssemblerCondDag"),
182 ParenIfBinOp, OS))
183 PrintFatalError(Rec: R, Msg: "Invalid AssemblerCondDag!");
184 }
185}
186
187void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
188 StringRef TargetName, StringRef ClassName, StringRef FuncName,
189 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
190 OS << "FeatureBitset ";
191 if (!ClassName.empty())
192 OS << TargetName << ClassName << "::\n";
193 OS << FuncName << "(const FeatureBitset &FB) ";
194 if (!ClassName.empty())
195 OS << "const ";
196 OS << "{\n";
197 OS << " FeatureBitset Features;\n";
198 for (const SubtargetFeatureInfo &SFI : make_second_range(c&: SubtargetFeatures)) {
199 const Record *Def = SFI.TheDef;
200
201 OS << " if (";
202 if (emitFeaturesAux(Owner: Def, TargetName,
203 Val: *Def->getValueAsDag(FieldName: "AssemblerCondDag"),
204 /*ParenIfBinOp=*/false, OS))
205 PrintFatalError(Rec: Def, Msg: "Invalid AssemblerCondDag!");
206
207 OS << ")\n";
208 OS << " Features.set(" << SFI.getEnumBitName() << ");\n";
209 }
210 OS << " return Features;\n";
211 OS << "}\n\n";
212}
213