1 //===- SubtargetFeatureInfo.cpp - Helpers for subtarget features ----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "SubtargetFeatureInfo.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/TableGen/Record.h"
19 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
20 LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const {
21 errs() << getEnumName() << " " << Index << "\n" << *TheDef;
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"))
38 if (Pred->getName().empty())
39 PrintFatalError(Pred->getLoc(), "Predicate has no name!");
41 // Ignore always true predicates.
42 if (Pred->getValueAsString("CondString").empty())
45 SubtargetFeatures.emplace_back(
46 Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));
48 return SubtargetFeatures;
51 void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
52 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
53 OS << "// Bits for subtarget features that participate in "
54 << "instruction matching.\n";
55 OS << "enum SubtargetFeatureBits : "
56 << getMinimalTypeForRange(SubtargetFeatures.size()) << " {\n";
57 for (const auto &SF : SubtargetFeatures) {
58 const SubtargetFeatureInfo &SFI = SF.second;
59 OS << " " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n";
64 void SubtargetFeatureInfo::emitNameTable(
65 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
66 // Need to sort the name table so that lookup by the log of the enum value
67 // gives the proper name. More specifically, for a feature of value 1<<n,
68 // SubtargetFeatureNames[n] should be the name of the feature.
70 for (const auto &SF : SubtargetFeatures)
71 if (IndexUB <= SF.second.Index)
72 IndexUB = SF.second.Index+1;
74 std::vector<std::string> Names;
76 Names.resize(IndexUB);
77 for (const auto &SF : SubtargetFeatures)
78 Names[SF.second.Index] = SF.second.getEnumName();
80 OS << "static const char *SubtargetFeatureNames[] = {\n";
81 for (uint64_t I = 0; I < IndexUB; ++I)
82 OS << " \"" << Names[I] << "\",\n";
84 // A small number of targets have no predicates. Null terminate the array to
85 // avoid a zero-length array.
90 void SubtargetFeatureInfo::emitComputeAvailableFeatures(
91 StringRef TargetName, StringRef ClassName, StringRef FuncName,
92 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
93 StringRef ExtraParams) {
94 OS << "PredicateBitset " << TargetName << ClassName << "::\n"
95 << FuncName << "(const " << TargetName << "Subtarget *Subtarget";
96 if (!ExtraParams.empty())
97 OS << ", " << ExtraParams;
99 OS << " PredicateBitset Features;\n";
100 for (const auto &SF : SubtargetFeatures) {
101 const SubtargetFeatureInfo &SFI = SF.second;
102 StringRef CondStr = SFI.TheDef->getValueAsString("CondString");
103 assert(!CondStr.empty() && "true predicate should have been filtered");
105 OS << " if (" << CondStr << ")\n";
106 OS << " Features.set(" << SFI.getEnumBitName() << ");\n";
108 OS << " return Features;\n";
112 void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
113 StringRef TargetName, StringRef ClassName, StringRef FuncName,
114 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
115 OS << "FeatureBitset " << TargetName << ClassName << "::\n"
116 << FuncName << "(const FeatureBitset& FB) const {\n";
117 OS << " FeatureBitset Features;\n";
118 for (const auto &SF : SubtargetFeatures) {
119 const SubtargetFeatureInfo &SFI = SF.second;
123 const DagInit *D = SFI.TheDef->getValueAsDag("AssemblerCondDag");
124 std::string CombineType = D->getOperator()->getAsString();
125 if (CombineType != "any_of" && CombineType != "all_of")
126 PrintFatalError(SFI.TheDef->getLoc(), "Invalid AssemblerCondDag!");
127 if (D->getNumArgs() == 0)
128 PrintFatalError(SFI.TheDef->getLoc(), "Invalid AssemblerCondDag!");
129 bool IsOr = CombineType == "any_of";
135 for (auto *Arg : D->getArgs()) {
142 if (auto *NotArg = dyn_cast<DagInit>(Arg)) {
143 if (NotArg->getOperator()->getAsString() != "not" ||
144 NotArg->getNumArgs() != 1)
145 PrintFatalError(SFI.TheDef->getLoc(), "Invalid AssemblerCondDag!");
146 Arg = NotArg->getArg(0);
149 if (!isa<DefInit>(Arg) ||
150 !cast<DefInit>(Arg)->getDef()->isSubClassOf("SubtargetFeature"))
151 PrintFatalError(SFI.TheDef->getLoc(), "Invalid AssemblerCondDag!");
152 OS << "FB[" << TargetName << "::" << Arg->getAsString() << "]";
161 OS << " Features.set(" << SFI.getEnumBitName() << ");\n";
163 OS << " return Features;\n";