]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/utils/TableGen/SubtargetFeatureInfo.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / utils / TableGen / SubtargetFeatureInfo.cpp
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
11 #include "Types.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/TableGen/Record.h"
14
15 #include <map>
16
17 using namespace llvm;
18
19 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
20 LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const {
21   errs() << getEnumName() << " " << Index << "\n" << *TheDef;
22 }
23 #endif
24
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.
32     //
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;
37
38     if (Pred->getName().empty())
39       PrintFatalError(Pred->getLoc(), "Predicate has no name!");
40
41     // Ignore always true predicates.
42     if (Pred->getValueAsString("CondString").empty())
43       continue;
44
45     SubtargetFeatures.emplace_back(
46         Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));
47   }
48   return SubtargetFeatures;
49 }
50
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";
60   }
61   OS << "};\n\n";
62 }
63
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;
73
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();
79
80   OS << "static const char *SubtargetFeatureNames[] = {\n";
81   for (uint64_t I = 0; I < IndexUB; ++I)
82     OS << "  \"" << Names[I] << "\",\n";
83
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";
88 }
89
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");
104
105     OS << "  if (" << CondStr << ")\n";
106     OS << "    Features.set(" << SFI.getEnumBitName() << ");\n";
107   }
108   OS << "  return Features;\n";
109   OS << "}\n\n";
110 }
111
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;
120
121     OS << "  if (";
122
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";
130
131     if (IsOr)
132       OS << "(";
133
134     bool First = true;
135     for (auto *Arg : D->getArgs()) {
136       if (!First) {
137         if (IsOr)
138           OS << " || ";
139         else
140           OS << " && ";
141       }
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);
147         OS << "!";
148       }
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() << "]";
153
154       First = false;
155     }
156
157     if (IsOr)
158       OS << ")";
159
160     OS << ")\n";
161     OS << "    Features.set(" << SFI.getEnumBitName() << ");\n";
162   }
163   OS << "  return Features;\n";
164   OS << "}\n\n";
165 }