1 //===- SubtargetFeatureInfo.cpp - Helpers for subtarget features ----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "SubtargetFeatureInfo.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/TableGen/Record.h"
19 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
20 LLVM_DUMP_METHOD
void SubtargetFeatureInfo::dump() const {
21 errs() << getEnumName() << " " << Index
<< "\n" << *TheDef
;
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"))
38 if (Pred
->getName().empty())
39 PrintFatalError(Pred
->getLoc(), "Predicate has no name!");
41 SubtargetFeatures
.emplace_back(
42 Pred
, SubtargetFeatureInfo(Pred
, SubtargetFeatures
.size()));
44 return SubtargetFeatures
;
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";
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.
66 for (const auto &SF
: SubtargetFeatures
)
67 if (IndexUB
<= SF
.second
.Index
)
68 IndexUB
= SF
.second
.Index
+1;
70 std::vector
<std::string
> Names
;
72 Names
.resize(IndexUB
);
73 for (const auto &SF
: SubtargetFeatures
)
74 Names
[SF
.second
.Index
] = SF
.second
.getEnumName();
76 OS
<< "static const char *SubtargetFeatureNames[] = {\n";
77 for (uint64_t I
= 0; I
< IndexUB
; ++I
)
78 OS
<< " \"" << Names
[I
] << "\",\n";
80 // A small number of targets have no predicates. Null terminate the array to
81 // avoid a zero-length array.
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
;
95 OS
<< " PredicateBitset Features;\n";
96 for (const auto &SF
: SubtargetFeatures
) {
97 const SubtargetFeatureInfo
&SFI
= SF
.second
;
99 OS
<< " if (" << SFI
.TheDef
->getValueAsString("CondString") << ")\n";
100 OS
<< " Features[" << SFI
.getEnumBitName() << "] = 1;\n";
102 OS
<< " return Features;\n";
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
;
116 std::string CondStorage
=
117 SFI
.TheDef
->getValueAsString("AssemblerCondString");
118 StringRef Conds
= CondStorage
;
119 std::pair
<StringRef
, StringRef
> Comma
= Conds
.split(',');
126 StringRef Cond
= Comma
.first
;
127 if (Cond
[0] == '!') {
129 Cond
= Cond
.substr(1);
135 OS
<< "FB[" << TargetName
<< "::" << Cond
<< "])";
137 if (Comma
.second
.empty())
141 Comma
= Comma
.second
.split(',');
145 OS
<< " Features[" << SFI
.getEnumBitName() << "] = 1;\n";
147 OS
<< " return Features;\n";