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("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("AssemblerMatcherPredicate"))
34       continue;
35 
36     if (Pred->getName().empty())
37       PrintFatalError(Pred->getLoc(), "Predicate has no name!");
38 
39     // Ignore always true predicates.
40     if (Pred->getValueAsString("CondString").empty())
41       continue;
42 
43     SubtargetFeatures.emplace_back(
44         Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));
45   }
46   return SubtargetFeatures;
47 }
48 
49 void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
50     SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
51   OS << "// Bits for subtarget features that participate in "
52      << "instruction matching.\n";
53   OS << "enum SubtargetFeatureBits : "
54      << getMinimalTypeForRange(SubtargetFeatures.size()) << " {\n";
55   for (const auto &SF : SubtargetFeatures) {
56     const SubtargetFeatureInfo &SFI = SF.second;
57     OS << "  " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n";
58   }
59   OS << "};\n\n";
60 }
61 
62 void SubtargetFeatureInfo::emitNameTable(
63     SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
64   // Need to sort the name table so that lookup by the log of the enum value
65   // gives the proper name. More specifically, for a feature of value 1<<n,
66   // SubtargetFeatureNames[n] should be the name of the feature.
67   uint64_t IndexUB = 0;
68   for (const auto &SF : SubtargetFeatures)
69     if (IndexUB <= SF.second.Index)
70       IndexUB = SF.second.Index+1;
71 
72   std::vector<std::string> Names;
73   if (IndexUB > 0)
74     Names.resize(IndexUB);
75   for (const auto &SF : SubtargetFeatures)
76     Names[SF.second.Index] = SF.second.getEnumName();
77 
78   OS << "static const char *SubtargetFeatureNames[] = {\n";
79   for (uint64_t I = 0; I < IndexUB; ++I)
80     OS << "  \"" << Names[I] << "\",\n";
81 
82   // A small number of targets have no predicates. Null terminate the array to
83   // avoid a zero-length array.
84   OS << "  nullptr\n"
85      << "};\n\n";
86 }
87 
88 void SubtargetFeatureInfo::emitComputeAvailableFeatures(
89     StringRef TargetName, StringRef ClassName, StringRef FuncName,
90     SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
91     StringRef ExtraParams) {
92   OS << "PredicateBitset " << ClassName << "::\n"
93      << FuncName << "(const " << TargetName << "Subtarget *Subtarget";
94   if (!ExtraParams.empty())
95     OS << ", " << ExtraParams;
96   OS << ") const {\n";
97   OS << "  PredicateBitset Features;\n";
98   for (const auto &SF : SubtargetFeatures) {
99     const SubtargetFeatureInfo &SFI = SF.second;
100     StringRef CondStr = SFI.TheDef->getValueAsString("CondString");
101     assert(!CondStr.empty() && "true predicate should have been filtered");
102 
103     OS << "  if (" << CondStr << ")\n";
104     OS << "    Features.set(" << SFI.getEnumBitName() << ");\n";
105   }
106   OS << "  return Features;\n";
107   OS << "}\n\n";
108 }
109 
110 // If ParenIfBinOp is true, print a surrounding () if Val uses && or ||.
111 static bool emitFeaturesAux(StringRef TargetName, const Init &Val,
112                             bool ParenIfBinOp, raw_ostream &OS) {
113   if (auto *D = dyn_cast<DefInit>(&Val)) {
114     if (!D->getDef()->isSubClassOf("SubtargetFeature"))
115       return true;
116     OS << "FB[" << TargetName << "::" << D->getAsString() << "]";
117     return false;
118   }
119   if (auto *D = dyn_cast<DagInit>(&Val)) {
120     auto *Op = dyn_cast<DefInit>(D->getOperator());
121     if (!Op)
122       return true;
123     StringRef OpName = Op->getDef()->getName();
124     if (OpName == "not" && D->getNumArgs() == 1) {
125       OS << '!';
126       return emitFeaturesAux(TargetName, *D->getArg(0), true, OS);
127     }
128     if ((OpName == "any_of" || OpName == "all_of") && D->getNumArgs() > 0) {
129       bool Paren = D->getNumArgs() > 1 && std::exchange(ParenIfBinOp, true);
130       if (Paren)
131         OS << '(';
132       ListSeparator LS(OpName == "any_of" ? " || " : " && ");
133       for (auto *Arg : D->getArgs()) {
134         OS << LS;
135         if (emitFeaturesAux(TargetName, *Arg, ParenIfBinOp, OS))
136           return true;
137       }
138       if (Paren)
139         OS << ')';
140       return false;
141     }
142   }
143   return true;
144 }
145 
146 void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
147     StringRef TargetName, StringRef ClassName, StringRef FuncName,
148     SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
149   OS << "FeatureBitset ";
150   if (!ClassName.empty())
151     OS << TargetName << ClassName << "::\n";
152   OS << FuncName << "(const FeatureBitset &FB) ";
153   if (!ClassName.empty())
154     OS << "const ";
155   OS << "{\n";
156   OS << "  FeatureBitset Features;\n";
157   for (const auto &SF : SubtargetFeatures) {
158     const SubtargetFeatureInfo &SFI = SF.second;
159 
160     OS << "  if (";
161     emitFeaturesAux(TargetName, *SFI.TheDef->getValueAsDag("AssemblerCondDag"),
162                     /*ParenIfBinOp=*/false, OS);
163     OS << ")\n";
164     OS << "    Features.set(" << SFI.getEnumBitName() << ");\n";
165   }
166   OS << "  return Features;\n";
167   OS << "}\n\n";
168 }
169