Skip to content

Commit 64f0bab

Browse files
committed
[Analysis] Move DomConditionCache::findAffectedValues to a new file; NFC
1 parent 69b8372 commit 64f0bab

File tree

2 files changed

+80
-65
lines changed

2 files changed

+80
-65
lines changed

llvm/lib/Analysis/DomConditionCache.cpp

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

99
#include "llvm/Analysis/DomConditionCache.h"
10-
#include "llvm/IR/PatternMatch.h"
11-
10+
#include "llvm/IR/InstrTypes.h"
11+
#include "llvm/IR/Instruction.h"
12+
#include "llvm/IR/Instructions.h"
1213
using namespace llvm;
13-
using namespace llvm::PatternMatch;
1414

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.
15+
// Implemented in ValueTracking.cpp
16+
void findValuesAffectedByCondition(Value *Cond, bool IsAssume,
17+
function_ref<void(Value *)> InsertAffected);
18+
2019
static void findAffectedValues(Value *Cond,
2120
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-
}
21+
auto InsertAffected = [&Affected](Value *V) { Affected.push_back(V); };
22+
findValuesAffectedByCondition(Cond, /*IsAssume*/ false, InsertAffected);
7923
}
8024

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

llvm/lib/Analysis/ValueTracking.cpp

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

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

0 commit comments

Comments
 (0)