blob: 96418dc77d5547f8bc5095feddb8d15f9c0c245d [file] [log] [blame]
Daniel Sandersea6ef3d2016-11-15 09:51:02 +00001//===- 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
Daniel Sanders72db2a32016-11-19 13:05:44 +000012#include "Types.h"
Daniel Sandersea6ef3d2016-11-15 09:51:02 +000013#include "llvm/TableGen/Record.h"
14
15#include <map>
16
17using namespace llvm;
18
Matthias Braun25bcaba2017-01-28 02:47:46 +000019#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
20LLVM_DUMP_METHOD void SubtargetFeatureInfo::dump() const {
21 errs() << getEnumName() << " " << Index << "\n" << *TheDef;
Daniel Sandersea6ef3d2016-11-15 09:51:02 +000022}
Matthias Braun25bcaba2017-01-28 02:47:46 +000023#endif
Daniel Sandersea6ef3d2016-11-15 09:51:02 +000024
25std::vector<std::pair<Record *, SubtargetFeatureInfo>>
26SubtargetFeatureInfo::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
Daniel Sanders72db2a32016-11-19 13:05:44 +000047void 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
Daniel Sanderse7b0d662017-04-21 15:59:56 +000062void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
63 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
64 raw_ostream &OS) {
65 OS << "// Bits for subtarget features that participate in "
66 << "instruction matching.\n";
67 OS << "enum SubtargetFeatureBits : "
68 << getMinimalTypeForRange(SubtargetFeatures.size()) << " {\n";
69 for (const auto &SF : SubtargetFeatures) {
70 const SubtargetFeatureInfo &SFI = SF.second;
71 OS << " " << SFI.getEnumBitName() << " = " << SFI.Index << ",\n";
72 }
73 OS << "};\n\n";
74}
75
Daniel Sanders72db2a32016-11-19 13:05:44 +000076void SubtargetFeatureInfo::emitNameTable(
77 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
78 raw_ostream &OS) {
Krzysztof Parzyszekdd4f5c52017-03-06 21:26:49 +000079 // Need to sort the name table so that lookup by the log of the enum value
80 // gives the proper name. More specifically, for a feature of value 1<<n,
81 // SubtargetFeatureNames[n] should be the name of the feature.
82 uint64_t IndexUB = 0;
83 for (const auto &SF : SubtargetFeatures)
84 if (IndexUB <= SF.second.Index)
85 IndexUB = SF.second.Index+1;
86
87 std::vector<std::string> Names;
88 if (IndexUB > 0)
89 Names.resize(IndexUB);
90 for (const auto &SF : SubtargetFeatures)
91 Names[SF.second.Index] = SF.second.getEnumName();
92
Daniel Sanders72db2a32016-11-19 13:05:44 +000093 OS << "static const char *SubtargetFeatureNames[] = {\n";
Krzysztof Parzyszekdd4f5c52017-03-06 21:26:49 +000094 for (uint64_t I = 0; I < IndexUB; ++I)
95 OS << " \"" << Names[I] << "\",\n";
96
Daniel Sanders72db2a32016-11-19 13:05:44 +000097 // A small number of targets have no predicates. Null terminate the array to
98 // avoid a zero-length array.
99 OS << " nullptr\n"
100 << "};\n\n";
101}
102
Daniel Sandersea6ef3d2016-11-15 09:51:02 +0000103void SubtargetFeatureInfo::emitComputeAvailableFeatures(
Daniel Sanders72db2a32016-11-19 13:05:44 +0000104 StringRef TargetName, StringRef ClassName, StringRef FuncName,
Daniel Sandersea6ef3d2016-11-15 09:51:02 +0000105 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
106 raw_ostream &OS) {
Daniel Sanderse7b0d662017-04-21 15:59:56 +0000107 OS << "PredicateBitset " << TargetName << ClassName << "::\n"
108 << FuncName << "(const MachineFunction *MF, const " << TargetName
109 << "Subtarget *Subtarget) 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
121void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
122 StringRef TargetName, StringRef ClassName, StringRef FuncName,
123 std::map<Record *, SubtargetFeatureInfo, LessRecordByID> &SubtargetFeatures,
124 raw_ostream &OS) {
Daniel Sandersea6ef3d2016-11-15 09:51:02 +0000125 OS << "uint64_t " << TargetName << ClassName << "::\n"
Daniel Sanders72db2a32016-11-19 13:05:44 +0000126 << FuncName << "(const FeatureBitset& FB) const {\n";
Daniel Sandersea6ef3d2016-11-15 09:51:02 +0000127 OS << " uint64_t Features = 0;\n";
128 for (const auto &SF : SubtargetFeatures) {
129 const SubtargetFeatureInfo &SFI = SF.second;
130
131 OS << " if (";
132 std::string CondStorage =
133 SFI.TheDef->getValueAsString("AssemblerCondString");
134 StringRef Conds = CondStorage;
135 std::pair<StringRef, StringRef> Comma = Conds.split(',');
136 bool First = true;
137 do {
138 if (!First)
139 OS << " && ";
140
141 bool Neg = false;
142 StringRef Cond = Comma.first;
143 if (Cond[0] == '!') {
144 Neg = true;
145 Cond = Cond.substr(1);
146 }
147
148 OS << "(";
149 if (Neg)
150 OS << "!";
151 OS << "FB[" << TargetName << "::" << Cond << "])";
152
153 if (Comma.second.empty())
154 break;
155
156 First = false;
157 Comma = Comma.second.split(',');
158 } while (true);
159
160 OS << ")\n";
161 OS << " Features |= " << SFI.getEnumName() << ";\n";
162 }
163 OS << " return Features;\n";
164 OS << "}\n\n";
165}