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