]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/utils/TableGen/SubtargetFeatureInfo.cpp
Merge lld trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / utils / TableGen / SubtargetFeatureInfo.cpp
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/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::emitSubtargetFeatureFlagEnumeration(
48     std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
49     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
62 void SubtargetFeatureInfo::emitNameTable(
63     std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
64     raw_ostream &OS) {
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.
68   uint64_t IndexUB = 0;
69   for (const auto &SF : SubtargetFeatures)
70     if (IndexUB <= SF.second.Index)
71       IndexUB = SF.second.Index+1;
72
73   std::vector<std::string> Names;
74   if (IndexUB > 0)
75     Names.resize(IndexUB);
76   for (const auto &SF : SubtargetFeatures)
77     Names[SF.second.Index] = SF.second.getEnumName();
78
79   OS << "static const char *SubtargetFeatureNames[] = {\n";
80   for (uint64_t I = 0; I < IndexUB; ++I)
81     OS << "  \"" << Names[I] << "\",\n";
82
83   // A small number of targets have no predicates. Null terminate the array to
84   // avoid a zero-length array.
85   OS << "  nullptr\n"
86      << "};\n\n";
87 }
88
89 void SubtargetFeatureInfo::emitComputeAvailableFeatures(
90     StringRef TargetName, StringRef ClassName, StringRef FuncName,
91     std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
92     raw_ostream &OS) {
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;
98
99     OS << "  if (";
100     std::string CondStorage =
101         SFI.TheDef->getValueAsString("AssemblerCondString");
102     StringRef Conds = CondStorage;
103     std::pair<StringRef, StringRef> Comma = Conds.split(',');
104     bool First = true;
105     do {
106       if (!First)
107         OS << " && ";
108
109       bool Neg = false;
110       StringRef Cond = Comma.first;
111       if (Cond[0] == '!') {
112         Neg = true;
113         Cond = Cond.substr(1);
114       }
115
116       OS << "(";
117       if (Neg)
118         OS << "!";
119       OS << "FB[" << TargetName << "::" << Cond << "])";
120
121       if (Comma.second.empty())
122         break;
123
124       First = false;
125       Comma = Comma.second.split(',');
126     } while (true);
127
128     OS << ")\n";
129     OS << "    Features |= " << SFI.getEnumName() << ";\n";
130   }
131   OS << "  return Features;\n";
132   OS << "}\n\n";
133 }