Skip to content

Commit 6c53b96

Browse files
committed
[Analysis] Move DomConditionCache::findAffectedValues to a new file; NFC
1 parent c9b1f1c commit 6c53b96

File tree

2 files changed

+86
-64
lines changed

2 files changed

+86
-64
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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
10+
addValueAffectedByCondition(Value *V,
11+
function_ref<void(Value *)> InsertAffected) {
12+
using namespace llvm::PatternMatch;
13+
assert(V != nullptr);
14+
if (isa<Argument>(V) || isa<GlobalValue>(V)) {
15+
InsertAffected(V);
16+
} else if (auto *I = dyn_cast<Instruction>(V)) {
17+
InsertAffected(V);
18+
19+
// Peek through unary operators to find the source of the condition.
20+
Value *Op;
21+
if (match(I, m_PtrToInt(m_Value(Op)))) {
22+
if (isa<Instruction>(Op) || isa<Argument>(Op))
23+
InsertAffected(Op);
24+
}
25+
}
26+
}
27+
28+
static void
29+
findValuesAffectedByCondition(Value *Cond, bool IsAssume,
30+
function_ref<void(Value *)> InsertAffected) {
31+
using namespace llvm::PatternMatch;
32+
auto AddAffected = [&InsertAffected](Value *V) {
33+
addValueAffectedByCondition(V, InsertAffected);
34+
};
35+
36+
assert(!IsAssume);
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+
InsertAffected(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+
}
79+
}
80+
81+
} // namespace llvm
82+
83+
#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) { Affected.push_back(V); };
17+
findValuesAffectedByCondition(Cond, /*IsAssume*/ false, InsertAffected);
7918
}
8019

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

0 commit comments

Comments
 (0)