Skip to content

Commit 88464ba

Browse files
committed
[ValueTracking] Refactor isKnownNonEqualFromContext
1 parent c6d95c4 commit 88464ba

File tree

1 file changed

+61
-44
lines changed

1 file changed

+61
-44
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3786,6 +3786,63 @@ static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,
37863786
(StartOffset.sle(OffsetB) && StepOffset.isNegative()));
37873787
}
37883788

3789+
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
3790+
unsigned Depth, const SimplifyQuery &Q) {
3791+
if (!Q.CxtI)
3792+
return false;
3793+
3794+
// Try to infer NonEqual based on information from dominating conditions.
3795+
if (Q.DC && Q.DT) {
3796+
auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
3797+
for (BranchInst *BI : Q.DC->conditionsFor(V)) {
3798+
Value *Cond = BI->getCondition();
3799+
BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
3800+
if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
3801+
isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL,
3802+
/*LHSIsTrue=*/true, Depth)
3803+
.value_or(false))
3804+
return true;
3805+
3806+
BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
3807+
if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
3808+
isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL,
3809+
/*LHSIsTrue=*/false, Depth)
3810+
.value_or(false))
3811+
return true;
3812+
}
3813+
3814+
return false;
3815+
};
3816+
3817+
if (IsKnownNonEqualFromDominatingCondition(V1) ||
3818+
IsKnownNonEqualFromDominatingCondition(V2))
3819+
return true;
3820+
}
3821+
3822+
if (!Q.AC)
3823+
return false;
3824+
3825+
// Try to infer NonEqual based on information from assumptions.
3826+
for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
3827+
if (!AssumeVH)
3828+
continue;
3829+
CallInst *I = cast<CallInst>(AssumeVH);
3830+
3831+
assert(I->getFunction() == Q.CxtI->getFunction() &&
3832+
"Got assumption for the wrong function!");
3833+
assert(I->getIntrinsicID() == Intrinsic::assume &&
3834+
"must be an assume intrinsic");
3835+
3836+
if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
3837+
/*LHSIsTrue=*/true, Depth)
3838+
.value_or(false) &&
3839+
isValidAssumeForContext(I, Q.CxtI, Q.DT))
3840+
return true;
3841+
}
3842+
3843+
return false;
3844+
}
3845+
37893846
/// Return true if it is known that V1 != V2.
37903847
static bool isKnownNonEqual(const Value *V1, const Value *V2,
37913848
const APInt &DemandedElts, unsigned Depth,
@@ -3857,49 +3914,8 @@ static bool isKnownNonEqual(const Value *V1, const Value *V2,
38573914
match(V2, m_PtrToIntSameSize(Q.DL, m_Value(B))))
38583915
return isKnownNonEqual(A, B, DemandedElts, Depth + 1, Q);
38593916

3860-
if (!Q.CxtI)
3861-
return false;
3862-
3863-
// Try to infer NonEqual based on information from dominating conditions.
3864-
if (Q.DC && Q.DT) {
3865-
for (BranchInst *BI : Q.DC->conditionsFor(V1)) {
3866-
Value *Cond = BI->getCondition();
3867-
BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
3868-
if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
3869-
isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL,
3870-
/*LHSIsTrue=*/true, Depth)
3871-
.value_or(false))
3872-
return true;
3873-
3874-
BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
3875-
if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
3876-
isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL,
3877-
/*LHSIsTrue=*/false, Depth)
3878-
.value_or(false))
3879-
return true;
3880-
}
3881-
}
3882-
3883-
if (!Q.AC)
3884-
return false;
3885-
3886-
// Try to infer NonEqual based on information from assumptions.
3887-
for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
3888-
if (!AssumeVH)
3889-
continue;
3890-
CallInst *I = cast<CallInst>(AssumeVH);
3891-
3892-
assert(I->getFunction() == Q.CxtI->getFunction() &&
3893-
"Got assumption for the wrong function!");
3894-
assert(I->getIntrinsicID() == Intrinsic::assume &&
3895-
"must be an assume intrinsic");
3896-
3897-
if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
3898-
/*LHSIsTrue=*/true, Depth)
3899-
.value_or(false) &&
3900-
isValidAssumeForContext(I, Q.CxtI, Q.DT))
3901-
return true;
3902-
}
3917+
if (isKnownNonEqualFromContext(V1, V2, Depth, Q))
3918+
return true;
39033919

39043920
return false;
39053921
}
@@ -10278,7 +10294,8 @@ void llvm::findValuesAffectedByCondition(
1027810294
bool HasRHSC = match(B, m_ConstantInt());
1027910295
if (ICmpInst::isEquality(Pred)) {
1028010296
AddAffected(A);
10281-
AddAffected(B);
10297+
if (IsAssume)
10298+
AddAffected(B);
1028210299
if (HasRHSC) {
1028310300
Value *Y;
1028410301
// (X & C) or (X | C).

0 commit comments

Comments
 (0)