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::emitSubtargetFeatureFlagEnumeration(
48 SubtargetFeatureInfoMap
&SubtargetFeatures
, raw_ostream
&OS
) {
49 OS
<< "// Flags for subtarget features that participate in "
50 << "instruction matching.\n";
51 OS
<< "enum SubtargetFeatureFlag : "
52 << getMinimalTypeForEnumBitfield(SubtargetFeatures
.size()) << " {\n";
53 for (const auto &SF
: SubtargetFeatures
) {
54 const SubtargetFeatureInfo
&SFI
= SF
.second
;
55 OS
<< " " << SFI
.getEnumName() << " = (1ULL << " << SFI
.Index
<< "),\n";
57 OS
<< " Feature_None = 0\n";
61 void SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(
62 SubtargetFeatureInfoMap
&SubtargetFeatures
, raw_ostream
&OS
) {
63 OS
<< "// Bits for subtarget features that participate in "
64 << "instruction matching.\n";
65 OS
<< "enum SubtargetFeatureBits : "
66 << getMinimalTypeForRange(SubtargetFeatures
.size()) << " {\n";
67 for (const auto &SF
: SubtargetFeatures
) {
68 const SubtargetFeatureInfo
&SFI
= SF
.second
;
69 OS
<< " " << SFI
.getEnumBitName() << " = " << SFI
.Index
<< ",\n";
74 void SubtargetFeatureInfo::emitNameTable(
75 SubtargetFeatureInfoMap
&SubtargetFeatures
, raw_ostream
&OS
) {
76 // Need to sort the name table so that lookup by the log of the enum value
77 // gives the proper name. More specifically, for a feature of value 1<<n,
78 // SubtargetFeatureNames[n] should be the name of the feature.
80 for (const auto &SF
: SubtargetFeatures
)
81 if (IndexUB
<= SF
.second
.Index
)
82 IndexUB
= SF
.second
.Index
+1;
84 std::vector
<std::string
> Names
;
86 Names
.resize(IndexUB
);
87 for (const auto &SF
: SubtargetFeatures
)
88 Names
[SF
.second
.Index
] = SF
.second
.getEnumName();
90 OS
<< "static const char *SubtargetFeatureNames[] = {\n";
91 for (uint64_t I
= 0; I
< IndexUB
; ++I
)
92 OS
<< " \"" << Names
[I
] << "\",\n";
94 // A small number of targets have no predicates. Null terminate the array to
95 // avoid a zero-length array.
100 void SubtargetFeatureInfo::emitComputeAvailableFeatures(
101 StringRef TargetName
, StringRef ClassName
, StringRef FuncName
,
102 SubtargetFeatureInfoMap
&SubtargetFeatures
, raw_ostream
&OS
,
103 StringRef ExtraParams
) {
104 OS
<< "PredicateBitset " << TargetName
<< ClassName
<< "::\n"
105 << FuncName
<< "(const " << TargetName
<< "Subtarget *Subtarget";
106 if (!ExtraParams
.empty())
107 OS
<< ", " << ExtraParams
;
109 OS
<< " PredicateBitset Features;\n";
110 for (const auto &SF
: SubtargetFeatures
) {
111 const SubtargetFeatureInfo
&SFI
= SF
.second
;
113 OS
<< " if (" << SFI
.TheDef
->getValueAsString("CondString") << ")\n";
114 OS
<< " Features[" << SFI
.getEnumBitName() << "] = 1;\n";
116 OS
<< " return Features;\n";
120 void SubtargetFeatureInfo::emitComputeAssemblerAvailableFeatures(
121 StringRef TargetName
, StringRef ClassName
, StringRef FuncName
,
122 SubtargetFeatureInfoMap
&SubtargetFeatures
, raw_ostream
&OS
) {
123 OS
<< "uint64_t " << TargetName
<< ClassName
<< "::\n"
124 << FuncName
<< "(const FeatureBitset& FB) const {\n";
125 OS
<< " uint64_t Features = 0;\n";
126 for (const auto &SF
: SubtargetFeatures
) {
127 const SubtargetFeatureInfo
&SFI
= SF
.second
;
130 std::string CondStorage
=
131 SFI
.TheDef
->getValueAsString("AssemblerCondString");
132 StringRef Conds
= CondStorage
;
133 std::pair
<StringRef
, StringRef
> Comma
= Conds
.split(',');
140 StringRef Cond
= Comma
.first
;
141 if (Cond
[0] == '!') {
143 Cond
= Cond
.substr(1);
149 OS
<< "FB[" << TargetName
<< "::" << Cond
<< "])";
151 if (Comma
.second
.empty())
155 Comma
= Comma
.second
.split(',');
159 OS
<< " Features |= " << SFI
.getEnumName() << ";\n";
161 OS
<< " return Features;\n";