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 "Types.h"
11#include "llvm/Config/llvm-config.h"
12#include "llvm/TableGen/Error.h"
13#include "llvm/TableGen/Record.h"
14
15using namespace llvm;
16
17#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
18LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const {
19 errs() << getEnumName() << " " << Index << "\n" << *TheDef;
20}
21#endif
22
23SubtargetFeaturesInfoVec
24SubtargetFeatureInfo::getAll(const RecordKeeper &Records) {
25 SubtargetFeaturesInfoVec SubtargetFeatures;
26 for (const Record *Pred : Records.getAllDerivedDefinitions(ClassName: "Predicate")) {
27 // Ignore predicates that are not intended for the assembler.
28 //
29 // The "AssemblerMatcherPredicate" string should be promoted to an argument
30 // if we re-use the machinery for non-assembler purposes in future.
31 if (!Pred->getValueAsBit(FieldName: "AssemblerMatcherPredicate"))
32 continue;
33
34 if (Pred->getName().empty())
35 PrintFatalError(ErrorLoc: Pred->getLoc(), Msg: "Predicate has no name!");
36
37 // Ignore always true predicates.
38 if (Pred->getValueAsString(FieldName: "CondString").empty())
39 continue;
40
41 SubtargetFeatures.emplace_back(
42 args&: Pred, args: SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));
43 }
44 return SubtargetFeatures;
45}
46
47void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
48 const SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
49 const std::map<std::string, unsigned> *HwModes) {
50 OS << "// Bits for subtarget features that participate in "
51 << "instruction matching.\n";
52 unsigned Size = SubtargetFeatures.size();
53 if (HwModes)
54 Size += HwModes->size();
55
56 OS << "enum SubtargetFeatureBits : " << getMinimalTypeForRange(Range: Size)
57 << " {\n";
58 for (const auto &SF : SubtargetFeatures) {
59 const SubtargetFeatureInfo &SFI = SF.second;
60 OS << " " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n";
61 }
62
63 if (HwModes) {
64 unsigned Offset = SubtargetFeatures.size();
65 for (const auto &M : *HwModes) {
66 OS << " Feature_HwMode" << M.second << "Bit = " << (M.second + Offset)
67 << ",\n";
68 }
69 }
70
71 OS << "};\n\n";
72}
73
74void 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;
83
84 std::vector<std::string> Names;
85 if (IndexUB > 0)
86 Names.resize(new_size: IndexUB);
87 for (const auto &SF : SubtargetFeatures)
88 Names[SF.second.Index] = SF.second.getEnumName();
89
90 OS << "static const char *SubtargetFeatureNames[] = {\n";
91 for (uint64_t I = 0; I < IndexUB; ++I)
92 OS << " \"" << Names[I] << "\",\n";
93
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";
98}
99
100void SubtargetFeatureInfo::emitComputeAvailableFeatures(
101 StringRef TargetName, StringRef ClassName, StringRef FuncName,
102 const SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
103 StringRef ExtraParams, const std::map<std::string, unsigned> *HwModes) {
104 OS << "PredicateBitset " << 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;
112 StringRef CondStr = SFI.TheDef->getValueAsString(FieldName: "CondString");
113 assert(!CondStr.empty() && "true predicate should have been filtered");
114
115 OS << " if (" << CondStr << ")\n";
116 OS << " Features.set(" << SFI.getEnumBitName() << ");\n";
117 }
118
119 if (HwModes) {
120 for (const auto &M : *HwModes) {
121 OS << " if (" << M.first << ")\n";
122 OS << " Features.set(Feature_HwMode" << M.second << "Bit);\n";
123 }
124 }
125
126 OS << " return Features;\n";
127 OS << "}\n\n";
128}
129
130// If ParenIfBinOp is true, print a surrounding () if Val uses && or ||.
131static bool emitFeaturesAux(StringRef TargetName, const Init &Val,
132 bool ParenIfBinOp, raw_ostream &OS) {
133 if (auto *D = dyn_cast<DefInit>(Val: &Val)) {
134 if (!D->getDef()->isSubClassOf(Name: "SubtargetFeature"))
135 return true;
136 OS << "FB[" << TargetName << "::" << D->getAsString() << "]";
137 return false;
138 }
139 if (auto *D = dyn_cast<DagInit>(Val: &Val)) {
140 auto *Op = dyn_cast<DefInit>(Val: D->getOperator());
141 if (!Op)
142 return true;
143 StringRef OpName = Op->getDef()->getName();
144 if (OpName == "not" && D->getNumArgs() == 1) {
145 OS << '!';
146 return emitFeaturesAux(TargetName, Val: *D->getArg(Num: 0), ParenIfBinOp: true, OS);
147 }
148 if ((OpName == "any_of" || OpName == "all_of") && D->getNumArgs() > 0) {
149 bool Paren = D->getNumArgs() > 1 && std::exchange(obj&: ParenIfBinOp, new_val: true);
150 if (Paren)
151 OS << '(';
152 ListSeparator LS(OpName == "any_of" ? " || " : " && ");
153 for (auto *Arg : D->getArgs()) {
154 OS << LS;
155 if (emitFeaturesAux(TargetName, Val: *Arg, ParenIfBinOp, OS))
156 return true;
157 }
158 if (Paren)
159 OS << ')';
160 return false;
161 }
162 }
163 return true;
164}
165
166void SubtargetFeatureInfo::emitPredicateCheck(
167 raw_ostream &OS, ArrayRef<const Record *> Predicates) {
168 ListSeparator LS(" && ");
169 for (const Record *R : Predicates) {
170 StringRef CondString = R->getValueAsString(FieldName: "CondString");
171 if (CondString.empty())
172 continue;
173 OS << LS << '(' << CondString << ')';
174 }
175}
176
177void SubtargetFeatureInfo::emitMCPredicateCheck(
178 raw_ostream &OS, StringRef TargetName,
179 ArrayRef<const Record *> Predicates) {
180 auto MCPredicates = make_filter_range(Range&: Predicates, Pred: [](const Record *R) {
181 return R->getValueAsBit(FieldName: "AssemblerMatcherPredicate");
182 });
183
184 if (MCPredicates.empty()) {
185 OS << "false";
186 return;
187 }
188
189 ListSeparator LS(" && ");
190 bool ParenIfBinOp = range_size(Range&: MCPredicates) > 1;
191 for (const Record *R : MCPredicates) {
192 OS << LS;
193 if (emitFeaturesAux(TargetName, Val: *R->getValueAsDag(FieldName: "AssemblerCondDag"),
194 ParenIfBinOp, OS))
195 PrintFatalError(Rec: R, Msg: "Invalid AssemblerCondDag!");
196 }
197}
198
199void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
200 StringRef TargetName, StringRef ClassName, StringRef FuncName,
201 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
202 OS << "FeatureBitset ";
203 if (!ClassName.empty())
204 OS << TargetName << ClassName << "::\n";
205 OS << FuncName << "(const FeatureBitset &FB) ";
206 if (!ClassName.empty())
207 OS << "const ";
208 OS << "{\n";
209 OS << " FeatureBitset Features;\n";
210 for (const SubtargetFeatureInfo &SFI : make_second_range(c&: SubtargetFeatures)) {
211 const Record *Def = SFI.TheDef;
212
213 OS << " if (";
214 if (emitFeaturesAux(TargetName, Val: *Def->getValueAsDag(FieldName: "AssemblerCondDag"),
215 /*ParenIfBinOp=*/false, OS))
216 PrintFatalError(Rec: Def, Msg: "Invalid AssemblerCondDag!");
217
218 OS << ")\n";
219 OS << " Features.set(" << SFI.getEnumBitName() << ");\n";
220 }
221 OS << " return Features;\n";
222 OS << "}\n\n";
223}
224