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 // Ignore always true predicates.
42 if (Pred
->getValueAsString("CondString").empty())
45 SubtargetFeatures
.emplace_back(
46 Pred
, SubtargetFeatureInfo(Pred
, SubtargetFeatures
.size()));
48 return SubtargetFeatures
;
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";
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.
70 for (const auto &SF
: SubtargetFeatures
)
71 if (IndexUB
<= SF
.second
.Index
)
72 IndexUB
= SF
.second
.Index
+1;
74 std::vector
<std::string
> Names
;
76 Names
.resize(IndexUB
);
77 for (const auto &SF
: SubtargetFeatures
)
78 Names
[SF
.second
.Index
] = SF
.second
.getEnumName();
80 OS
<< "static const char *SubtargetFeatureNames[] = {\n";
81 for (uint64_t I
= 0; I
< IndexUB
; ++I
)
82 OS
<< " \"" << Names
[I
] << "\",\n";
84 // A small number of targets have no predicates. Null terminate the array to
85 // avoid a zero-length array.
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
;
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");
105 OS
<< " if (" << CondStr
<< ")\n";
106 OS
<< " Features.set(" << SFI
.getEnumBitName() << ");\n";
108 OS
<< " return Features;\n";
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
;
122 std::string CondStorage
=
123 SFI
.TheDef
->getValueAsString("AssemblerCondString");
124 StringRef Conds
= CondStorage
;
125 std::pair
<StringRef
, StringRef
> Comma
= Conds
.split(',');
132 StringRef Cond
= Comma
.first
;
133 if (Cond
[0] == '!') {
135 Cond
= Cond
.substr(1);
141 OS
<< "FB[" << TargetName
<< "::" << Cond
<< "])";
143 if (Comma
.second
.empty())
147 Comma
= Comma
.second
.split(',');
151 OS
<< " Features.set(" << SFI
.getEnumBitName() << ");\n";
153 OS
<< " return Features;\n";