1 //===- SubtargetFeatureInfo.cpp - Helpers for subtarget features ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "SubtargetFeatureInfo.h"
11
12 #include "Types.h"
13 #include "llvm/Config/llvm-config.h"
14 #include "llvm/TableGen/Record.h"
15
16 #include <map>
17
18 using namespace llvm;
19
20 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const21 LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const {
22 errs() << getEnumName() << " " << Index << "\n" << *TheDef;
23 }
24 #endif
25
26 std::vector<std::pair<Record *, SubtargetFeatureInfo>>
getAll(const RecordKeeper & Records)27 SubtargetFeatureInfo::getAll(const RecordKeeper &Records) {
28 std::vector<std::pair<Record *, SubtargetFeatureInfo>> SubtargetFeatures;
29 std::vector<Record *> AllPredicates =
30 Records.getAllDerivedDefinitions("Predicate");
31 for (Record *Pred : AllPredicates) {
32 // Ignore predicates that are not intended for the assembler.
33 //
34 // The "AssemblerMatcherPredicate" string should be promoted to an argument
35 // if we re-use the machinery for non-assembler purposes in future.
36 if (!Pred->getValueAsBit("AssemblerMatcherPredicate"))
37 continue;
38
39 if (Pred->getName().empty())
40 PrintFatalError(Pred->getLoc(), "Predicate has no name!");
41
42 SubtargetFeatures.emplace_back(
43 Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));
44 }
45 return SubtargetFeatures;
46 }
47
emitSubtargetFeatureFlagEnumeration(SubtargetFeatureInfoMap & SubtargetFeatures,raw_ostream & OS)48 void SubtargetFeatureInfo::emitSubtargetFeatureFlagEnumeration(
49 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
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";
57 }
58 OS << " Feature_None = 0\n";
59 OS << "};\n\n";
60 }
61
emitSubtargetFeatureBitEnumeration(SubtargetFeatureInfoMap & SubtargetFeatures,raw_ostream & OS)62 void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
63 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
64 OS << "// Bits for subtarget features that participate in "
65 << "instruction matching.\n";
66 OS << "enum SubtargetFeatureBits : "
67 << getMinimalTypeForRange(SubtargetFeatures.size()) << " {\n";
68 for (const auto &SF : SubtargetFeatures) {
69 const SubtargetFeatureInfo &SFI = SF.second;
70 OS << " " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n";
71 }
72 OS << "};\n\n";
73 }
74
emitNameTable(SubtargetFeatureInfoMap & SubtargetFeatures,raw_ostream & OS)75 void SubtargetFeatureInfo::emitNameTable(
76 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
77 // Need to sort the name table so that lookup by the log of the enum value
78 // gives the proper name. More specifically, for a feature of value 1<<n,
79 // SubtargetFeatureNames[n] should be the name of the feature.
80 uint64_t IndexUB = 0;
81 for (const auto &SF : SubtargetFeatures)
82 if (IndexUB <= SF.second.Index)
83 IndexUB = SF.second.Index+1;
84
85 std::vector<std::string> Names;
86 if (IndexUB > 0)
87 Names.resize(IndexUB);
88 for (const auto &SF : SubtargetFeatures)
89 Names[SF.second.Index] = SF.second.getEnumName();
90
91 OS << "static const char *SubtargetFeatureNames[] = {\n";
92 for (uint64_t I = 0; I < IndexUB; ++I)
93 OS << " \"" << Names[I] << "\",\n";
94
95 // A small number of targets have no predicates. Null terminate the array to
96 // avoid a zero-length array.
97 OS << " nullptr\n"
98 << "};\n\n";
99 }
100
emitComputeAvailableFeatures(StringRef TargetName,StringRef ClassName,StringRef FuncName,SubtargetFeatureInfoMap & SubtargetFeatures,raw_ostream & OS,StringRef ExtraParams)101 void SubtargetFeatureInfo::emitComputeAvailableFeatures(
102 StringRef TargetName, StringRef ClassName, StringRef FuncName,
103 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
104 StringRef ExtraParams) {
105 OS << "PredicateBitset " << TargetName << ClassName << "::\n"
106 << FuncName << "(const " << TargetName << "Subtarget *Subtarget";
107 if (!ExtraParams.empty())
108 OS << ", " << ExtraParams;
109 OS << ") const {\n";
110 OS << " PredicateBitset Features;\n";
111 for (const auto &SF : SubtargetFeatures) {
112 const SubtargetFeatureInfo &SFI = SF.second;
113
114 OS << " if (" << SFI.TheDef->getValueAsString("CondString") << ")\n";
115 OS << " Features[" << SFI.getEnumBitName() << "] = 1;\n";
116 }
117 OS << " return Features;\n";
118 OS << "}\n\n";
119 }
120
emitComputeAssemblerAvailableFeatures(StringRef TargetName,StringRef ClassName,StringRef FuncName,SubtargetFeatureInfoMap & SubtargetFeatures,raw_ostream & OS)121 void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
122 StringRef TargetName, StringRef ClassName, StringRef FuncName,
123 SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
124 OS << "uint64_t " << TargetName << ClassName << "::\n"
125 << FuncName << "(const FeatureBitset& FB) const {\n";
126 OS << " uint64_t Features = 0;\n";
127 for (const auto &SF : SubtargetFeatures) {
128 const SubtargetFeatureInfo &SFI = SF.second;
129
130 OS << " if (";
131 std::string CondStorage =
132 SFI.TheDef->getValueAsString("AssemblerCondString");
133 StringRef Conds = CondStorage;
134 std::pair<StringRef, StringRef> Comma = Conds.split(',');
135 bool First = true;
136 do {
137 if (!First)
138 OS << " && ";
139
140 bool Neg = false;
141 StringRef Cond = Comma.first;
142 if (Cond[0] == '!') {
143 Neg = true;
144 Cond = Cond.substr(1);
145 }
146
147 OS << "(";
148 if (Neg)
149 OS << "!";
150 OS << "FB[" << TargetName << "::" << Cond << "])";
151
152 if (Comma.second.empty())
153 break;
154
155 First = false;
156 Comma = Comma.second.split(',');
157 } while (true);
158
159 OS << ")\n";
160 OS << " Features |= " << SFI.getEnumName() << ";\n";
161 }
162 OS << " return Features;\n";
163 OS << "}\n\n";
164 }
165