Skip to content

Commit ad4029b

Browse files
committed
[Analysis] Move DomConditionCache::findAffectedValues to a new file; NFC
1 parent 64422cf commit ad4029b

File tree

2 files changed

+85
-64
lines changed

2 files changed

+85
-64
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#ifndef LLVM_ANALYSIS_CONDITIONCACHEUTIL_H
2+
#define LLVM_ANALYSIS_CONDITIONCACHEUTIL_H
3+
4+
#include "llvm/IR/PatternMatch.h"
5+
#include <functional>
6+
7+
namespace llvm {
8+
9+
static void addValueAffectedByCondition(
10+
Value *V, function_ref<void(Value *, int)> InsertAffected, int Idx = -1) {
11+
using namespace llvm::PatternMatch;
12+
assert(V != nullptr);
13+
if (isa<Argument>(V) || isa<GlobalValue>(V)) {
14+
InsertAffected(V, Idx);
15+
} else if (auto *I = dyn_cast<Instruction>(V)) {
16+
InsertAffected(V, Idx);
17+
18+
// Peek through unary operators to find the source of the condition.
19+
Value *Op;
20+
if (match(I, m_PtrToInt(m_Value(Op)))) {
21+
if (isa<Instruction>(Op) || isa<Argument>(Op))
22+
InsertAffected(Op, Idx);
23+
}
24+
}
25+
}
26+
27+
static void
28+
findValuesAffectedByCondition(Value *Cond, bool IsAssume,
29+
function_ref<void(Value *, int)> InsertAffected) {
30+
using namespace llvm::PatternMatch;
31+
auto AddAffected = [&InsertAffected](Value *V) {
32+
addValueAffectedByCondition(V, InsertAffected);
33+
};
34+
35+
assert(!IsAssume);
36+
SmallVector<Value *, 8> Worklist;
37+
SmallPtrSet<Value *, 8> Visited;
38+
Worklist.push_back(Cond);
39+
while (!Worklist.empty()) {
40+
Value *V = Worklist.pop_back_val();
41+
if (!Visited.insert(V).second)
42+
continue;
43+
44+
CmpInst::Predicate Pred;
45+
Value *A, *B;
46+
if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
47+
Worklist.push_back(A);
48+
Worklist.push_back(B);
49+
} else if (match(V, m_ICmp(Pred, m_Value(A), m_Constant()))) {
50+
AddAffected(A);
51+
52+
if (ICmpInst::isEquality(Pred)) {
53+
Value *X;
54+
// (X & C) or (X | C) or (X ^ C).
55+
// (X << C) or (X >>_s C) or (X >>_u C).
56+
if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) ||
57+
match(A, m_Shift(m_Value(X), m_ConstantInt())))
58+
AddAffected(X);
59+
} else {
60+
Value *X;
61+
// Handle (A + C1) u< C2, which is the canonical form of
62+
// A > C3 && A < C4.
63+
if (match(A, m_Add(m_Value(X), m_ConstantInt())))
64+
AddAffected(X);
65+
// Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported by
66+
// computeKnownFPClass().
67+
if ((Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SGT) &&
68+
match(A, m_ElementWiseBitCast(m_Value(X))))
69+
InsertAffected(X, -1);
70+
}
71+
} else if (match(Cond, m_CombineOr(m_FCmp(Pred, m_Value(A), m_Constant()),
72+
m_Intrinsic<Intrinsic::is_fpclass>(
73+
m_Value(A), m_Constant())))) {
74+
// Handle patterns that computeKnownFPClass() support.
75+
AddAffected(A);
76+
}
77+
}
78+
}
79+
80+
} // namespace llvm
81+
82+
#endif // LLVM_ANALYSIS_CONDITIONCACHEUTIL_H

llvm/lib/Analysis/DomConditionCache.cpp

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

99
#include "llvm/Analysis/DomConditionCache.h"
10-
#include "llvm/IR/PatternMatch.h"
10+
#include "llvm/Analysis/ConditionCacheUtil.h"
1111

1212
using namespace llvm;
13-
using namespace llvm::PatternMatch;
1413

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.
2014
static void findAffectedValues(Value *Cond,
2115
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-
}
16+
auto InsertAffected = [&Affected](Value *V, int) { Affected.push_back(V); };
17+
findValuesAffectedByCondition(Cond, /*IsAssume*/ false, InsertAffected);
7918
}
8019

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

0 commit comments

Comments
 (0)