1 //===- SubtargetFeatureInfo.cpp - Helpers for subtarget features ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "SubtargetFeatureInfo.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 SubtargetFeatures.emplace_back(
42 Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));
44 return SubtargetFeatures;
47 void SubtargetFeatureInfo::emitSubtargetFeatureFlagEnumeration(
48 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
50 OS << "// Flags for subtarget features that participate in "
51 << "instruction matching.\n";
52 OS << "enum SubtargetFeatureFlag : "
53 << getMinimalTypeForEnumBitfield(SubtargetFeatures.size()) << " {\n";
54 for (const auto &SF : SubtargetFeatures) {
55 const SubtargetFeatureInfo &SFI = SF.second;
56 OS << " " << SFI.getEnumName() << " = (1ULL << " << SFI.Index << "),\n";
58 OS << " Feature_None = 0\n";
62 void SubtargetFeatureInfo::emitNameTable(
63 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
65 // Need to sort the name table so that lookup by the log of the enum value
66 // gives the proper name. More specifically, for a feature of value 1<<n,
67 // SubtargetFeatureNames[n] should be the name of the feature.
69 for (const auto &SF : SubtargetFeatures)
70 if (IndexUB <= SF.second.Index)
71 IndexUB = SF.second.Index+1;
73 std::vector<std::string> Names;
75 Names.resize(IndexUB);
76 for (const auto &SF : SubtargetFeatures)
77 Names[SF.second.Index] = SF.second.getEnumName();
79 OS << "static const char *SubtargetFeatureNames[] = {\n";
80 for (uint64_t I = 0; I < IndexUB; ++I)
81 OS << " \"" << Names[I] << "\",\n";
83 // A small number of targets have no predicates. Null terminate the array to
84 // avoid a zero-length array.
89 void SubtargetFeatureInfo::emitComputeAvailableFeatures(
90 StringRef TargetName, StringRef ClassName, StringRef FuncName,
91 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
93 OS << "uint64_t " << TargetName << ClassName << "::\n"
94 << FuncName << "(const FeatureBitset& FB) const {\n";
95 OS << " uint64_t Features = 0;\n";
96 for (const auto &SF : SubtargetFeatures) {
97 const SubtargetFeatureInfo &SFI = SF.second;
100 std::string CondStorage =
101 SFI.TheDef->getValueAsString("AssemblerCondString");
102 StringRef Conds = CondStorage;
103 std::pair<StringRef, StringRef> Comma = Conds.split(',');
110 StringRef Cond = Comma.first;
111 if (Cond[0] == '!') {
113 Cond = Cond.substr(1);
119 OS << "FB[" << TargetName << "::" << Cond << "])";
121 if (Comma.second.empty())
125 Comma = Comma.second.split(',');
129 OS << " Features |= " << SFI.getEnumName() << ";\n";
131 OS << " return Features;\n";