Skip to content

[ValueTracking] Handle not in dominating condition. #126423

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 1 commit into from
Feb 10, 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
11 changes: 11 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,9 @@ static void computeKnownBitsFromCond(const Value *V, Value *Cond,

if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);

if (Depth < MaxAnalysisRecursionDepth && match(Cond, m_Not(m_Value(A))))
computeKnownBitsFromCond(V, A, Known, Depth + 1, SQ, !Invert);
}

void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,
Expand Down Expand Up @@ -4934,6 +4937,11 @@ static void computeKnownFPClassFromCond(const Value *V, Value *Cond,
KnownFromContext);
return;
}
if (Depth < MaxAnalysisRecursionDepth && match(Cond, m_Not(m_Value(A)))) {
computeKnownFPClassFromCond(V, A, Depth + 1, !CondIsTrue, CxtI,
KnownFromContext);
return;
}
CmpPredicate Pred;
Value *LHS;
uint64_t ClassVal = 0;
Expand Down Expand Up @@ -10272,6 +10280,9 @@ void llvm::findValuesAffectedByCondition(
m_Value()))) {
// Handle patterns that computeKnownFPClass() support.
AddAffected(A);
} else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we drop the !IsAssume here and instead drop the m_Not special case for assumes above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be able to do that isEphemeralValueOf need to be updated to handle that.
example of test that will fail as the assume is optimized away.

define void @pr47496(i8 %x) {
; CHECK-LABEL: @pr47496(
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
; CHECK-NEXT: [[XOR:%.*]] = xor i1 [[CMP]], true
; CHECK-NEXT: call void @llvm.assume(i1 [[XOR]])
; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: ret void
;
%cmp = icmp slt i8 %x, 0
%xor = xor i1 %cmp, true
call void @llvm.assume(i1 %xor)
call void @use(i1 %cmp)
ret void
}

Maybe all instructions with boolean results building up the condition for the assume shall be Ephemeral.

Copy link
Contributor Author

@andjo403 andjo403 Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have some follow up changes that update isEphemeralValueOf here main...andjo403:llvm-project:assumeNotCond but probably better as multiple PRs if that is something that we want

// Assume is checked here to avoid issues with ephemeral values
Worklist.push_back(X);
}
}
}
3 changes: 1 addition & 2 deletions llvm/test/Transforms/InstCombine/fpclass-from-dom-cond.ll
Original file line number Diff line number Diff line change
Expand Up @@ -528,8 +528,7 @@ define i1 @test_inv_and(float %x, i1 %cond2) {
; CHECK-NEXT: [[AND:%.*]] = and i1 [[COND2]], [[NOT]]
; CHECK-NEXT: br i1 [[AND]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[RET1:%.*]] = fcmp oeq float [[X]], 0x7FF0000000000000
; CHECK-NEXT: ret i1 [[RET1]]
; CHECK-NEXT: ret i1 false
; CHECK: if.else:
; CHECK-NEXT: ret i1 false
;
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/InstCombine/known-bits.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2374,8 +2374,7 @@ define i8 @test_inv_cond_and(i8 %x, i1 %c) {
; CHECK-NEXT: [[COND:%.*]] = and i1 [[C:%.*]], [[NOT]]
; CHECK-NEXT: br i1 [[COND]], label [[IF:%.*]], label [[EXIT:%.*]]
; CHECK: if:
; CHECK-NEXT: [[OR1:%.*]] = or i8 [[X]], -4
; CHECK-NEXT: ret i8 [[OR1]]
; CHECK-NEXT: ret i8 -4
; CHECK: exit:
; CHECK-NEXT: [[OR2:%.*]] = or i8 [[X]], -4
; CHECK-NEXT: ret i8 [[OR2]]
Expand Down