Skip to content

Commit 316b7e2

Browse files
author
翁锦瑞
committed
[MC][TargetParser] Optimize SetImpliedBits.
1 parent 655de70 commit 316b7e2

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

llvm/include/llvm/TargetParser/SubtargetFeature.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/ArrayRef.h"
2121
#include "llvm/ADT/STLExtras.h"
2222
#include "llvm/ADT/StringRef.h"
23+
#include "llvm/ADT/bit.h"
2324
#include "llvm/Support/Compiler.h"
2425
#include "llvm/Support/MathExtras.h"
2526
#include <array>
@@ -151,6 +152,18 @@ class FeatureBitset {
151152
}
152153
return false;
153154
}
155+
156+
template <typename Func>
157+
constexpr void forEachBit(Func func) const {
158+
for (unsigned I = 0; I < MAX_SUBTARGET_WORDS; ++I) {
159+
uint64_t Word = Bits[I];
160+
while (Word) {
161+
unsigned Bit = llvm::countr_zero(Word);
162+
Word &= Word - 1;
163+
func(I * 64 + Bit);
164+
}
165+
}
166+
}
154167
};
155168

156169
/// Class used to store the subtarget bits in the tables created by tablegen.

llvm/lib/MC/MCSubtargetInfo.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,32 @@ static const T *Find(StringRef S, ArrayRef<T> A) {
3434
}
3535

3636
/// For each feature that is (transitively) implied by this feature, set it.
37-
static
38-
void SetImpliedBits(FeatureBitset &Bits, const FeatureBitset &Implies,
39-
ArrayRef<SubtargetFeatureKV> FeatureTable) {
40-
// OR the Implies bits in outside the loop. This allows the Implies for CPUs
41-
// which might imply features not in FeatureTable to use this.
42-
Bits |= Implies;
43-
for (const SubtargetFeatureKV &FE : FeatureTable)
44-
if (Implies.test(FE.Value))
45-
SetImpliedBits(Bits, FE.Implies.getAsBitset(), FeatureTable);
37+
static void SetImpliedBits(FeatureBitset &Bits, const FeatureBitset &Implies,
38+
ArrayRef<SubtargetFeatureKV> FeatureTable) {
39+
std::array<uint16_t, MAX_SUBTARGET_FEATURES> featureMap;
40+
FeatureBitset Mask;
41+
uint16_t idx = 0;
42+
for (const auto &FE : FeatureTable) {
43+
Mask.set(FE.Value);
44+
featureMap[FE.Value] = idx++;
45+
}
46+
47+
FeatureBitset impl(Implies);
48+
while (true) {
49+
Bits |= impl;
50+
auto newImplies = Mask & impl;
51+
if (newImplies.none()) {
52+
break;
53+
}
54+
55+
Mask ^= newImplies;
56+
impl = FeatureBitset();
57+
58+
newImplies.forEachBit([&](unsigned Bit) {
59+
unsigned idx = featureMap[Bit];
60+
impl |= FeatureTable[idx].Implies.getAsBitset();
61+
});
62+
}
4663
}
4764

4865
/// For each feature that (transitively) implies this feature, clear it.

0 commit comments

Comments
 (0)