-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ValueTracking] Infer NonEqual from dominating conditions/assumptions #117442
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 |
---|---|---|
|
@@ -3857,6 +3857,50 @@ static bool isKnownNonEqual(const Value *V1, const Value *V2, | |
match(V2, m_PtrToIntSameSize(Q.DL, m_Value(B)))) | ||
return isKnownNonEqual(A, B, DemandedElts, Depth + 1, Q); | ||
|
||
if (!Q.CxtI) | ||
return false; | ||
|
||
// Try to infer NonEqual based on information from dominating conditions. | ||
if (Q.DC && Q.DT) { | ||
for (BranchInst *BI : Q.DC->conditionsFor(V1)) { | ||
Value *Cond = BI->getCondition(); | ||
BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0)); | ||
if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) && | ||
isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL, | ||
/*LHSIsTrue=*/true, Depth) | ||
.value_or(false)) | ||
return true; | ||
|
||
BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1)); | ||
if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) && | ||
isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL, | ||
/*LHSIsTrue=*/false, Depth) | ||
.value_or(false)) | ||
return true; | ||
} | ||
} | ||
|
||
if (!Q.AC) | ||
return false; | ||
|
||
// Try to infer NonEqual based on information from assumptions. | ||
for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) { | ||
if (!AssumeVH) | ||
continue; | ||
CallInst *I = cast<CallInst>(AssumeVH); | ||
|
||
assert(I->getFunction() == Q.CxtI->getFunction() && | ||
"Got assumption for the wrong function!"); | ||
assert(I->getIntrinsicID() == Intrinsic::assume && | ||
"must be an assume intrinsic"); | ||
|
||
if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL, | ||
/*LHSIsTrue=*/true, Depth) | ||
.value_or(false) && | ||
isValidAssumeForContext(I, Q.CxtI, Q.DT)) | ||
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. Check that assume is valid before recursive function? 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. It seems slower than current implementation: https://llvm-compile-time-tracker.com/compare.php?from=8b5145c718a41f1cbfe463def3ad88a89afb17e8&to=eb913e9188a5ae48bdf48d714a95f0f75731e21f&stat=instructions:u |
||
return true; | ||
} | ||
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. nit: Maybe nicer to have a helper |
||
|
||
return false; | ||
} | ||
|
||
|
@@ -10231,10 +10275,10 @@ void llvm::findValuesAffectedByCondition( | |
Worklist.push_back(B); | ||
} | ||
} else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) { | ||
AddCmpOperands(A, B); | ||
|
||
bool HasRHSC = match(B, m_ConstantInt()); | ||
if (ICmpInst::isEquality(Pred)) { | ||
AddAffected(A); | ||
AddAffected(B); | ||
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. 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. Is the impact here mainly from adding more affected values or from using them in isKnownNonEqual? Something we could do is add a bitfield for what the affected value is for (e.g. knownbits, etc), so we can quickly skip ones that are not relevant. 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. It is mainly from adding more affected values. 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.
Are you working on this? 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. No, I'm not working on this. 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. I have implemented a prototype (see #118493). But the compile-time impact looks worse: http://llvm-compile-time-tracker.com/compare.php?from=8f3cf204bb91ce09fb3651b8a7e770de7e603b2f&to=338d7fc5b72e20f1606a28f740d5a0068c95d94b&stat=instructions%3Au |
||
if (HasRHSC) { | ||
Value *Y; | ||
// (X & C) or (X | C). | ||
|
@@ -10248,6 +10292,7 @@ void llvm::findValuesAffectedByCondition( | |
} | ||
} | ||
} else { | ||
AddCmpOperands(A, B); | ||
if (HasRHSC) { | ||
// Handle (A + C1) u< C2, which is the canonical form of | ||
// A > C3 && A < C4. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and above, any value in iterating through V2 as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We add both
V1
andV2
infindValuesAffectedByCondition
. So it is ok to iterate on conditions related toV1
.