Skip to content

[ValueTracking] Refactor isKnownNonEqualFromContext #127388

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

Merged
merged 2 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 61 additions & 44 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3786,6 +3786,63 @@ static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B,
(StartOffset.sle(OffsetB) && StepOffset.isNegative()));
}

static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
unsigned Depth, const SimplifyQuery &Q) {
if (!Q.CxtI)
return false;

// Try to infer NonEqual based on information from dominating conditions.
if (Q.DC && Q.DT) {
auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
for (BranchInst *BI : Q.DC->conditionsFor(V)) {
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;
}

return false;
};

if (IsKnownNonEqualFromDominatingCondition(V1) ||
IsKnownNonEqualFromDominatingCondition(V2))
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))
return true;
}

return false;
}

/// Return true if it is known that V1 != V2.
static bool isKnownNonEqual(const Value *V1, const Value *V2,
const APInt &DemandedElts, unsigned Depth,
Expand Down Expand Up @@ -3857,49 +3914,8 @@ 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))
return true;
}
if (isKnownNonEqualFromContext(V1, V2, Depth, Q))
return true;

return false;
}
Expand Down Expand Up @@ -10278,7 +10294,8 @@ void llvm::findValuesAffectedByCondition(
bool HasRHSC = match(B, m_ConstantInt());
if (ICmpInst::isEquality(Pred)) {
AddAffected(A);
AddAffected(B);
if (IsAssume)
AddAffected(B);
if (HasRHSC) {
Value *Y;
// (X & C) or (X | C).
Expand Down
78 changes: 78 additions & 0 deletions llvm/test/Transforms/InstCombine/icmp-dom.ll
Original file line number Diff line number Diff line change
Expand Up @@ -733,4 +733,82 @@ entry:
ret i1 %cmp
}

; TODO: We can prove `%cond2` is always false
define void @test_nonequal_domcond_loop1(i32 %x0, i1 %x1) {
; CHECK-LABEL: @test_nonequal_domcond_loop1(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP_HEADER:%.*]]
; CHECK: loop.header:
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[X0:%.*]], [[LATCH:%.*]] ]
; CHECK-NEXT: br label [[LATCH]]
; CHECK: latch:
; CHECK-NEXT: br i1 [[X1:%.*]], label [[IF_THEN:%.*]], label [[LOOP_HEADER]]
; CHECK: if.then:
; CHECK-NEXT: [[AND:%.*]] = and i32 [[X0]], 1
; CHECK-NEXT: [[COND1:%.*]] = icmp eq i32 [[AND]], [[PHI]]
; CHECK-NEXT: br i1 [[COND1]], label [[IF_THEN2:%.*]], label [[LATCH]]
; CHECK: if.then2:
; CHECK-NEXT: br label [[BB:%.*]]
; CHECK: indirectbb:
; CHECK-NEXT: [[COND2:%.*]] = icmp eq i32 [[PHI]], 31
; CHECK-NEXT: br i1 [[COND2]], label [[EXIT:%.*]], label [[LATCH]]
; CHECK: exit:
; CHECK-NEXT: ret void
;
entry:
br label %loop.header

loop.header:
%phi = phi i32 [ 0, %entry ], [ %x0, %latch ]
br label %latch

latch:
br i1 %x1, label %if.then, label %loop.header

if.then:
%and = and i32 %x0, 1
%cond1 = icmp eq i32 %and, %phi
br i1 %cond1, label %if.then2, label %latch

if.then2:
br label %indirectbb

indirectbb:
%cond2 = icmp eq i32 %phi, 31
br i1 %cond2, label %exit, label %latch

exit:
ret void
}

define void @test_nonequal_domcond_loop2(ptr %p) {
; CHECK-LABEL: @test_nonequal_domcond_loop2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[LOAD1:%.*]] = load volatile i8, ptr [[P:%.*]], align 1
; CHECK-NEXT: br label [[WHILE_COND:%.*]]
; CHECK: while.cond:
; CHECK-NEXT: [[LOAD2:%.*]] = load volatile i8, ptr [[P]], align 1
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i8 [[LOAD2]], 0
; CHECK-NEXT: [[CMP2:%.*]] = icmp uge i8 [[LOAD2]], [[LOAD1]]
; CHECK-NEXT: [[OR:%.*]] = select i1 [[CMP1]], i1 true, i1 [[CMP2]]
; CHECK-NEXT: br i1 [[OR]], label [[WHILE_COND]], label [[FOR_BODY:%.*]]
; CHECK: for.body:
; CHECK-NEXT: br i1 false, label [[WHILE_COND]], label [[FOR_BODY]]
;
entry:
%load1 = load volatile i8, ptr %p, align 1
br label %while.cond

while.cond:
%load2 = load volatile i8, ptr %p, align 1
%cmp1 = icmp eq i8 %load2, 0
%cmp2 = icmp uge i8 %load2, %load1
%or = select i1 %cmp1, i1 true, i1 %cmp2
br i1 %or, label %while.cond, label %for.body

for.body:
%cond = icmp eq i8 %load1, %load2
br i1 %cond, label %while.cond, label %for.body
}

declare void @side_effect()