Skip to content

Commit 4b0feb4

Browse files
committed
[ValueTracking] Adjust hash table structure
1 parent 6328e0f commit 4b0feb4

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

llvm/include/llvm/Analysis/DomConditionCache.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum class DomConditionFlag : uint8_t {
3434
KnownFPClass = 1 << 1,
3535
PowerOfTwo = 1 << 2,
3636
ICmp = 1 << 3,
37+
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/ICmp),
3738
};
3839

3940
LLVM_DECLARE_ENUM_AS_BITMASK(
@@ -43,23 +44,26 @@ LLVM_DECLARE_ENUM_AS_BITMASK(
4344
class DomConditionCache {
4445
private:
4546
/// A map of values about which a branch might be providing information.
46-
using AffectedValuesMap =
47-
DenseMap<Value *,
48-
SmallVector<std::pair<BranchInst *, DomConditionFlag>, 1>>;
49-
AffectedValuesMap AffectedValues;
47+
using AffectedValuesMap = DenseMap<Value *, SmallVector<BranchInst *, 1>>;
48+
AffectedValuesMap AffectedValues[BitWidth<DomConditionFlag>];
5049

5150
public:
5251
/// Add a branch condition to the cache.
5352
void registerBranch(BranchInst *BI);
5453

5554
/// Remove a value from the cache, e.g. because it will be erased.
56-
void removeValue(Value *V) { AffectedValues.erase(V); }
55+
void removeValue(Value *V) {
56+
for (auto &Table : AffectedValues)
57+
Table.erase(V);
58+
}
5759

5860
/// Access the list of branches which affect this value.
59-
ArrayRef<std::pair<BranchInst *, DomConditionFlag>>
60-
conditionsFor(const Value *V) const {
61-
auto AVI = AffectedValues.find_as(const_cast<Value *>(V));
62-
if (AVI == AffectedValues.end())
61+
ArrayRef<BranchInst *> conditionsFor(const Value *V,
62+
DomConditionFlag Filter) const {
63+
assert(has_single_bit(to_underlying(Filter)));
64+
auto &Values = AffectedValues[countr_zero(to_underlying(Filter))];
65+
auto AVI = Values.find_as(const_cast<Value *>(V));
66+
if (AVI == Values.end())
6367
return {};
6468

6569
return AVI->second;

llvm/lib/Analysis/DomConditionCache.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@ void DomConditionCache::registerBranch(BranchInst *BI) {
2424
SmallVector<std::pair<Value *, DomConditionFlag>, 16> Affected;
2525
findAffectedValues(BI->getCondition(), Affected);
2626
for (auto [V, Flags] : Affected) {
27-
auto &AV = AffectedValues[V];
28-
bool Exist = false;
29-
for (auto &[OtherBI, OtherFlags] : AV) {
30-
if (OtherBI == BI) {
31-
OtherFlags |= Flags;
32-
Exist = true;
33-
break;
34-
}
27+
uint32_t Underlying = to_underlying(Flags);
28+
while (Underlying) {
29+
uint32_t LSB = Underlying & -Underlying;
30+
auto &AV = AffectedValues[countr_zero(LSB)][V];
31+
if (llvm::none_of(AV, [&](BranchInst *Elem) { return Elem == BI; }))
32+
AV.push_back(BI);
33+
Underlying -= LSB;
3534
}
36-
if (!Exist)
37-
AV.push_back({BI, Flags});
3835
}
3936
}

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -790,9 +790,7 @@ void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,
790790

791791
if (Q.DC && Q.DT) {
792792
// Handle dominating conditions.
793-
for (auto [BI, Flag] : Q.DC->conditionsFor(V)) {
794-
if (!any(Flag & DomConditionFlag::KnownBits))
795-
continue;
793+
for (BranchInst *BI : Q.DC->conditionsFor(V, DomConditionFlag::KnownBits)) {
796794
BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
797795
if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
798796
computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
@@ -2301,9 +2299,8 @@ bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
23012299

23022300
// Handle dominating conditions.
23032301
if (Q.DC && Q.CxtI && Q.DT) {
2304-
for (auto [BI, Flag] : Q.DC->conditionsFor(V)) {
2305-
if (!any(Flag & DomConditionFlag::PowerOfTwo))
2306-
continue;
2302+
for (BranchInst *BI :
2303+
Q.DC->conditionsFor(V, DomConditionFlag::PowerOfTwo)) {
23072304
Value *Cond = BI->getCondition();
23082305

23092306
BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
@@ -4934,9 +4931,8 @@ static KnownFPClass computeKnownFPClassFromContext(const Value *V,
49344931

49354932
if (Q.DC && Q.DT) {
49364933
// Handle dominating conditions.
4937-
for (auto [BI, Flag] : Q.DC->conditionsFor(V)) {
4938-
if (!any(Flag & DomConditionFlag::KnownFPClass))
4939-
continue;
4934+
for (BranchInst *BI :
4935+
Q.DC->conditionsFor(V, DomConditionFlag::KnownFPClass)) {
49404936
Value *Cond = BI->getCondition();
49414937

49424938
BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,9 +1385,7 @@ Instruction *InstCombinerImpl::foldICmpWithDominatingICmp(ICmpInst &Cmp) {
13851385
return nullptr;
13861386
};
13871387

1388-
for (auto [BI, Flags] : DC.conditionsFor(X)) {
1389-
if (!any(Flags & DomConditionFlag::ICmp))
1390-
continue;
1388+
for (BranchInst *BI : DC.conditionsFor(X, DomConditionFlag::ICmp)) {
13911389
ICmpInst::Predicate DomPred;
13921390
const APInt *DomC;
13931391
if (!match(BI->getCondition(),

0 commit comments

Comments
 (0)