gn build: Merge r372706
[llvm-complete.git] / utils / TableGen / SubtargetFeatureInfo.cpp
blob5430f73d5e091edcd27f98b1029b7a9d556026e9
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 //===----------------------------------------------------------------------===//
9 #include "SubtargetFeatureInfo.h"
11 #include "Types.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/TableGen/Record.h"
15 #include <map>
17 using namespace llvm;
19 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
20 LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const {
21 errs() << getEnumName() << " " << Index << "\n" << *TheDef;
23 #endif
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"))
36 continue;
38 if (Pred->getName().empty())
39 PrintFatalError(Pred->getLoc(), "Predicate has no name!");
41 // Ignore always true predicates.
42 if (Pred->getValueAsString("CondString").empty())
43 continue;
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";
61 OS << "};\n\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.
69 uint64_t IndexUB = 0;
70 for (const auto &SF : SubtargetFeatures)
71 if (IndexUB <= SF.second.Index)
72 IndexUB = SF.second.Index+1;
74 std::vector<std::string> Names;
75 if (IndexUB > 0)
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.
86 OS << " nullptr\n"
87 << "};\n\n";
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;
98 OS << ") const {\n";
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";
109 OS << "}\n\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;
121 OS << " if (";
122 std::string CondStorage =
123 SFI.TheDef->getValueAsString("AssemblerCondString");
124 StringRef Conds = CondStorage;
125 std::pair<StringRef, StringRef> Comma = Conds.split(',');
126 bool First = true;
127 do {
128 if (!First)
129 OS << " && ";
131 bool Neg = false;
132 StringRef Cond = Comma.first;
133 if (Cond[0] == '!') {
134 Neg = true;
135 Cond = Cond.substr(1);
138 OS << "(";
139 if (Neg)
140 OS << "!";
141 OS << "FB[" << TargetName << "::" << Cond << "])";
143 if (Comma.second.empty())
144 break;
146 First = false;
147 Comma = Comma.second.split(',');
148 } while (true);
150 OS << ")\n";
151 OS << " Features.set(" << SFI.getEnumBitName() << ");\n";
153 OS << " return Features;\n";
154 OS << "}\n\n";