-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[ValueTracking] Support dominating known bits condition in and/or #74728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,23 +34,39 @@ static void findAffectedValues(Value *Cond, | |
} | ||
}; | ||
|
||
ICmpInst::Predicate Pred; | ||
Value *A; | ||
if (match(Cond, m_ICmp(Pred, m_Value(A), m_Constant()))) { | ||
AddAffected(A); | ||
bool TopLevelIsAnd = match(Cond, m_LogicalAnd()); | ||
SmallVector<Value *, 8> Worklist; | ||
SmallPtrSet<Value *, 8> Visited; | ||
Worklist.push_back(Cond); | ||
while (!Worklist.empty()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the compile-time impact after this patch? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Value *V = Worklist.pop_back_val(); | ||
if (!Visited.insert(V).second) | ||
continue; | ||
|
||
if (ICmpInst::isEquality(Pred)) { | ||
Value *X; | ||
// (X & C) or (X | C) or (X ^ C). | ||
// (X << C) or (X >>_s C) or (X >>_u C). | ||
if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) || | ||
match(A, m_Shift(m_Value(X), m_ConstantInt()))) | ||
AddAffected(X); | ||
} else { | ||
Value *X; | ||
// Handle (A + C1) u< C2, which is the canonical form of A > C3 && A < C4. | ||
if (match(A, m_Add(m_Value(X), m_ConstantInt()))) | ||
AddAffected(X); | ||
ICmpInst::Predicate Pred; | ||
Value *A, *B; | ||
// Only recurse into and/or if it matches the top-level and/or type. | ||
if (TopLevelIsAnd ? match(V, m_LogicalAnd(m_Value(A), m_Value(B))) | ||
: match(V, m_LogicalOr(m_Value(A), m_Value(B)))) { | ||
Worklist.push_back(A); | ||
Worklist.push_back(B); | ||
} else if (match(V, m_ICmp(Pred, m_Value(A), m_Constant()))) { | ||
AddAffected(A); | ||
|
||
if (ICmpInst::isEquality(Pred)) { | ||
Value *X; | ||
// (X & C) or (X | C) or (X ^ C). | ||
// (X << C) or (X >>_s C) or (X >>_u C). | ||
if (match(A, m_BitwiseLogic(m_Value(X), m_ConstantInt())) || | ||
match(A, m_Shift(m_Value(X), m_ConstantInt()))) | ||
AddAffected(X); | ||
} else { | ||
Value *X; | ||
// Handle (A + C1) u< C2, which is the canonical form of | ||
// A > C3 && A < C4. | ||
if (match(A, m_Add(m_Value(X), m_ConstantInt()))) | ||
AddAffected(X); | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -706,28 +706,40 @@ static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, | |
} | ||
} | ||
|
||
static void computeKnownBitsFromCond(const Value *V, Value *Cond, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a template helper to avoid duplicating the decomposition?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do something like 07f0dde to allow reuse of the full logic for other ValueTracking helpers, e.g. #80941. Seems to add a little overhead, but not much: http://llvm-compile-time-tracker.com/compare.php?from=9ac82f0d3ecf6c13669b0c7940920460c037a292&to=07f0dde1783a0ce7ea17bcf29438989e9077b310&stat=instructions:u |
||
KnownBits &Known, unsigned Depth, | ||
const SimplifyQuery &SQ, bool Invert) { | ||
Value *A, *B; | ||
if (Depth < MaxAnalysisRecursionDepth && | ||
(Invert ? match(Cond, m_LogicalOr(m_Value(A), m_Value(B))) | ||
: match(Cond, m_LogicalAnd(m_Value(A), m_Value(B))))) { | ||
computeKnownBitsFromCond(V, A, Known, Depth + 1, SQ, Invert); | ||
computeKnownBitsFromCond(V, B, Known, Depth + 1, SQ, Invert); | ||
dtcxzyw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
dtcxzyw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) | ||
computeKnownBitsFromCmp( | ||
V, Invert ? Cmp->getInversePredicate() : Cmp->getPredicate(), | ||
Cmp->getOperand(0), Cmp->getOperand(1), Known, SQ); | ||
} | ||
|
||
void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known, | ||
unsigned Depth, const SimplifyQuery &Q) { | ||
unsigned Depth, const SimplifyQuery &Q) { | ||
if (!Q.CxtI) | ||
return; | ||
|
||
if (Q.DC && Q.DT) { | ||
// Handle dominating conditions. | ||
for (BranchInst *BI : Q.DC->conditionsFor(V)) { | ||
auto *Cmp = dyn_cast<ICmpInst>(BI->getCondition()); | ||
if (!Cmp) | ||
continue; | ||
|
||
BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0)); | ||
if (Q.DT->dominates(Edge0, Q.CxtI->getParent())) | ||
computeKnownBitsFromCmp(V, Cmp->getPredicate(), Cmp->getOperand(0), | ||
Cmp->getOperand(1), Known, Q); | ||
computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q, | ||
/*Invert*/ false); | ||
|
||
BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1)); | ||
if (Q.DT->dominates(Edge1, Q.CxtI->getParent())) | ||
computeKnownBitsFromCmp(V, Cmp->getInversePredicate(), | ||
Cmp->getOperand(0), Cmp->getOperand(1), Known, | ||
Q); | ||
computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q, | ||
/*Invert*/ true); | ||
} | ||
|
||
if (Known.hasConflict()) | ||
|
Uh oh!
There was an error while loading. Please reload this page.