]> 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     SubtargetFeatures.emplace_back(
42         Pred, SubtargetFeatureInfo(Pred, SubtargetFeatures.size()));
43   }
44   return SubtargetFeatures;
45 }
46
47 void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
48     SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
49   OS << "// Bits for subtarget features that participate in "
50      << "instruction matching.\n";
51   OS << "enum SubtargetFeatureBits : "
52      << getMinimalTypeForRange(SubtargetFeatures.size()) << " {\n";
53   for (const auto &SF : SubtargetFeatures) {
54     const SubtargetFeatureInfo &SFI = SF.second;
55     OS << "  " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n";
56   }
57   OS << "};\n\n";
58 }
59
60 void SubtargetFeatureInfo::emitNameTable(
61     SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
62   // Need to sort the name table so that lookup by the log of the enum value
63   // gives the proper name. More specifically, for a feature of value 1<<n,
64   // SubtargetFeatureNames[n] should be the name of the feature.
65   uint64_t IndexUB = 0;
66   for (const auto &SF : SubtargetFeatures)
67     if (IndexUB <= SF.second.Index)
68       IndexUB = SF.second.Index+1;
69
70   std::vector<std::string> Names;
71   if (IndexUB > 0)
72     Names.resize(IndexUB);
73   for (const auto &SF : SubtargetFeatures)
74     Names[SF.second.Index] = SF.second.getEnumName();
75
76   OS << "static const char *SubtargetFeatureNames[] = {\n";
77   for (uint64_t I = 0; I < IndexUB; ++I)
78     OS << "  \"" << Names[I] << "\",\n";
79
80   // A small number of targets have no predicates. Null terminate the array to
81   // avoid a zero-length array.
82   OS << "  nullptr\n"
83      << "};\n\n";
84 }
85
86 void SubtargetFeatureInfo::emitComputeAvailableFeatures(
87     StringRef TargetName, StringRef ClassName, StringRef FuncName,
88     SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS,
89     StringRef ExtraParams) {
90   OS << "PredicateBitset " << TargetName << ClassName << "::\n"
91      << FuncName << "(const " << TargetName << "Subtarget *Subtarget";
92   if (!ExtraParams.empty())
93     OS << ", " << ExtraParams;
94   OS << ") const {\n";
95   OS << "  PredicateBitset Features;\n";
96   for (const auto &SF : SubtargetFeatures) {
97     const SubtargetFeatureInfo &SFI = SF.second;
98
99     OS << "  if (" << SFI.TheDef->getValueAsString("CondString") << ")\n";
100     OS << "    Features[" << SFI.getEnumBitName() << "] = 1;\n";
101   }
102   OS << "  return Features;\n";
103   OS << "}\n\n";
104 }
105
106 void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
107     StringRef TargetName, StringRef ClassName, StringRef FuncName,
108     SubtargetFeatureInfoMap &SubtargetFeatures, raw_ostream &OS) {
109   OS << "FeatureBitset " << TargetName << ClassName << "::\n"
110      << FuncName << "(const FeatureBitset& FB) const {\n";
111   OS << "  FeatureBitset Features;\n";
112   for (const auto &SF : SubtargetFeatures) {
113     const SubtargetFeatureInfo &SFI = SF.second;
114
115     OS << "  if (";
116     std::string CondStorage =
117         SFI.TheDef->getValueAsString("AssemblerCondString");
118     StringRef Conds = CondStorage;
119     std::pair<StringRef, StringRef> Comma = Conds.split(',');
120     bool First = true;
121     do {
122       if (!First)
123         OS << " && ";
124
125       bool Neg = false;
126       StringRef Cond = Comma.first;
127       if (Cond[0] == '!') {
128         Neg = true;
129         Cond = Cond.substr(1);
130       }
131
132       OS << "(";
133       if (Neg)
134         OS << "!";
135       OS << "FB[" << TargetName << "::" << Cond << "])";
136
137       if (Comma.second.empty())
138         break;
139
140       First = false;
141       Comma = Comma.second.split(',');
142     } while (true);
143
144     OS << ")\n";
145     OS << "    Features[" << SFI.getEnumBitName() << "] = 1;\n";
146   }
147   OS << "  return Features;\n";
148   OS << "}\n\n";
149 }