Skip to content

Commit 3bc0ff2

Browse files
committed
[Analysis] Move DomConditionCache::findAffectedValues to a new file; NFC
1 parent 10ccde3 commit 3bc0ff2

File tree

3 files changed

+79
-65
lines changed

3 files changed

+79
-65
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,13 @@ std::optional<bool> isImpliedByDomCondition(CmpInst::Predicate Pred,
11951195
const Value *LHS, const Value *RHS,
11961196
const Instruction *ContextI,
11971197
const DataLayout &DL);
1198+
1199+
/// Call \p InsertAffected on all Values whose known bits / value may be
1200+
/// affected by the condition \p Cond. Used by AssumptionCache and
1201+
/// DomConditionCache.
1202+
void findValuesAffectedByCondition(Value *Cond, bool IsAssume,
1203+
function_ref<void(Value *)> InsertAffected);
1204+
11981205
} // end namespace llvm
11991206

12001207
#endif // LLVM_ANALYSIS_VALUETRACKING_H

llvm/lib/Analysis/DomConditionCache.cpp

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,75 +7,13 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/Analysis/DomConditionCache.h"
10-
#include "llvm/IR/PatternMatch.h"
11-
10+
#include "llvm/Analysis/ValueTracking.h"
1211
using namespace llvm;
13-
using namespace llvm::PatternMatch;
1412

15-
// TODO: This code is very similar to findAffectedValues() in
16-
// AssumptionCache, but currently specialized to just the patterns that
17-
// computeKnownBits() supports, and without the notion of result elem indices
18-
// that are AC specific. Deduplicate this code once we have a clearer picture
19-
// of how much they can be shared.
2013
static void findAffectedValues(Value *Cond,
2114
SmallVectorImpl<Value *> &Affected) {
22-
auto AddAffected = [&Affected](Value *V) {
23-
if (isa<Argument>(V) || isa<GlobalValue>(V)) {
24-
Affected.push_back(V);
25-
} else if (auto *I = dyn_cast<Instruction>(V)) {
26-
Affected.push_back(I);
27-
28-
// Peek through unary operators to find the source of the condition.
29-
Value *Op;
30-
if (match(I, m_PtrToInt(m_Value(Op)))) {
31-
if (isa<Instruction>(Op) || isa<Argument>(Op))
32-
Affected.push_back(Op);
33-
}
34-
}
35-
};
36-
37-
SmallVector<Value *, 8> Worklist;
38-
SmallPtrSet<Value *, 8> Visited;
39-
Worklist.push_back(Cond);
40-
while (!Worklist.empty()) {
41-
Value *V = Worklist.pop_back_val();
42-
if (!Visited.insert(V).second)
43-
continue;
44-
45-
CmpInst::Predicate Pred;
46-
Value *A, *B;
47-
if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
48-
Worklist.push_back(A);
49-
Worklist.push_back(B);
50-
} else if (match(V, m_ICmp(Pred, m_Value(A), m_Constant()))) {
51-
AddAffected(A);
52-
53-
if (ICmpInst::isEquality(Pred)) {
54-
Value *X;
55-
// (X & C) or (X | C) or (X ^ C).
56-
// (X << C) or (X >>_s C) or (X >>_u C).
57-
if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
58-
match(A, m_Shift(m_Value(X), m_ConstantInt())))
59-
AddAffected(X);
60-
} else {
61-
Value *X;
62-
// Handle (A + C1) u< C2, which is the canonical form of
63-
// A > C3 && A < C4.
64-
if (match(A, m_Add(m_Value(X), m_ConstantInt())))
65-
AddAffected(X);
66-
// Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported by
67-
// computeKnownFPClass().
68-
if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGT) &&
69-
match(A, m_ElementWiseBitCast(m_Value(X))))
70-
Affected.push_back(X);
71-
}
72-
} else if (match(Cond, m_CombineOr(m_FCmp(Pred, m_Value(A), m_Constant()),
73-
m_Intrinsic<Intrinsic::is_fpclass>(
74-
m_Value(A), m_Constant())))) {
75-
// Handle patterns that computeKnownFPClass() support.
76-
AddAffected(A);
77-
}
78-
}
15+
auto InsertAffected = [&Affected](Value *V) { Affected.push_back(V); };
16+
findValuesAffectedByCondition(Cond, /*IsAssume=*/false, InsertAffected);
7917
}
8018

8119
void DomConditionCache::registerBranch(BranchInst *BI) {

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9099,3 +9099,72 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
90999099

91009100
return CR;
91019101
}
9102+
9103+
static void
9104+
addValueAffectedByCondition(Value *V,
9105+
function_ref<void(Value *)> InsertAffected) {
9106+
assert(V != nullptr);
9107+
if (isa<Argument>(V) || isa<GlobalValue>(V)) {
9108+
InsertAffected(V);
9109+
} else if (auto *I = dyn_cast<Instruction>(V)) {
9110+
InsertAffected(V);
9111+
9112+
// Peek through unary operators to find the source of the condition.
9113+
Value *Op;
9114+
if (match(I, m_PtrToInt(m_Value(Op)))) {
9115+
if (isa<Instruction>(Op) || isa<Argument>(Op))
9116+
InsertAffected(Op);
9117+
}
9118+
}
9119+
}
9120+
9121+
void llvm::findValuesAffectedByCondition(
9122+
Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
9123+
auto AddAffected = [&InsertAffected](Value *V) {
9124+
addValueAffectedByCondition(V, InsertAffected);
9125+
};
9126+
9127+
assert(!IsAssume);
9128+
SmallVector<Value *, 8> Worklist;
9129+
SmallPtrSet<Value *, 8> Visited;
9130+
Worklist.push_back(Cond);
9131+
while (!Worklist.empty()) {
9132+
Value *V = Worklist.pop_back_val();
9133+
if (!Visited.insert(V).second)
9134+
continue;
9135+
9136+
CmpInst::Predicate Pred;
9137+
Value *A, *B;
9138+
if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
9139+
Worklist.push_back(A);
9140+
Worklist.push_back(B);
9141+
} else if (match(V, m_ICmp(Pred, m_Value(A), m_Constant()))) {
9142+
AddAffected(A);
9143+
9144+
if (ICmpInst::isEquality(Pred)) {
9145+
Value *X;
9146+
// (X & C) or (X | C) or (X ^ C).
9147+
// (X << C) or (X >>_s C) or (X >>_u C).
9148+
if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
9149+
match(A, m_Shift(m_Value(X), m_ConstantInt())))
9150+
AddAffected(X);
9151+
} else {
9152+
Value *X;
9153+
// Handle (A + C1) u< C2, which is the canonical form of
9154+
// A > C3 && A < C4.
9155+
if (match(A, m_Add(m_Value(X), m_ConstantInt())))
9156+
AddAffected(X);
9157+
// Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported by
9158+
// computeKnownFPClass().
9159+
if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGT) &&
9160+
match(A, m_ElementWiseBitCast(m_Value(X))))
9161+
InsertAffected(X);
9162+
}
9163+
} else if (match(Cond, m_CombineOr(m_FCmp(Pred, m_Value(A), m_Constant()),
9164+
m_Intrinsic<Intrinsic::is_fpclass>(
9165+
m_Value(A), m_Constant())))) {
9166+
// Handle patterns that computeKnownFPClass() support.
9167+
AddAffected(A);
9168+
}
9169+
}
9170+
}

0 commit comments

Comments
 (0)