Skip to content

[ValueTracking] Handle trunc to i1 as condition in dominating condition. #126414

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 11, 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
24 changes: 23 additions & 1 deletion llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,10 +797,28 @@ static void computeKnownBitsFromCond(const Value *V, Value *Cond,
else
Known2 = Known2.intersectWith(Known3);
Known = Known.unionWith(Known2);
return;
}

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

if (match(Cond, m_Trunc(m_Specific(V)))) {
KnownBits DstKnown(1);
if (Invert) {
DstKnown.setAllZero();
} else {
DstKnown.setAllOnes();
}
if (cast<TruncInst>(Cond)->hasNoUnsignedWrap()) {
Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
Copy link
Contributor

Choose a reason for hiding this comment

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

you can sext if you have trunc nsw

Copy link
Contributor Author

Choose a reason for hiding this comment

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

avoided sext as it was not handled i #125414 seems like it is uncommon to have sext for trunc to i1 only found 3 in llvm-opt-benchmark

return;
}
Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
return;
}

if (Depth < MaxAnalysisRecursionDepth && match(Cond, m_Not(m_Value(A))))
computeKnownBitsFromCond(V, A, Known, Depth + 1, SQ, !Invert);
Expand Down Expand Up @@ -10280,6 +10298,10 @@ void llvm::findValuesAffectedByCondition(
m_Value()))) {
// Handle patterns that computeKnownFPClass() support.
AddAffected(A);
} else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
// Assume is checked here as X is already added above for assumes in
// addValueAffectedByCondition
AddAffected(X);
} else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
// Assume is checked here to avoid issues with ephemeral values
Worklist.push_back(X);
Expand Down
36 changes: 12 additions & 24 deletions llvm/test/Transforms/InstCombine/known-bits.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2167,11 +2167,9 @@ define i8 @test_trunc_and_1(i8 %a) {
; CHECK-NEXT: [[CAST:%.*]] = trunc i8 [[A:%.*]] to i1
; CHECK-NEXT: br i1 [[CAST]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[B:%.*]] = and i8 [[A]], 1
; CHECK-NEXT: ret i8 [[B]]
; CHECK-NEXT: ret i8 1
; CHECK: if.else:
; CHECK-NEXT: [[C:%.*]] = and i8 [[A]], 1
; CHECK-NEXT: ret i8 [[C]]
; CHECK-NEXT: ret i8 0
;
entry:
%cast = trunc i8 %a to i1
Expand All @@ -2192,11 +2190,9 @@ define i8 @test_not_trunc_and_1(i8 %a) {
; CHECK-NEXT: [[CAST:%.*]] = trunc i8 [[A:%.*]] to i1
; CHECK-NEXT: br i1 [[CAST]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[B:%.*]] = and i8 [[A]], 1
; CHECK-NEXT: ret i8 [[B]]
; CHECK-NEXT: ret i8 0
; CHECK: if.else:
; CHECK-NEXT: [[C:%.*]] = and i8 [[A]], 1
; CHECK-NEXT: ret i8 [[C]]
; CHECK-NEXT: ret i8 1
;
entry:
%cast = trunc i8 %a to i1
Expand Down Expand Up @@ -2243,11 +2239,9 @@ define i8 @test_trunc_nuw_and_1(i8 %a) {
; CHECK-NEXT: [[CAST:%.*]] = trunc nuw i8 [[A:%.*]] to i1
; CHECK-NEXT: br i1 [[CAST]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[B:%.*]] = and i8 [[A]], 1
; CHECK-NEXT: ret i8 [[B]]
; CHECK-NEXT: ret i8 0
; CHECK: if.else:
; CHECK-NEXT: [[C:%.*]] = and i8 [[A]], 1
; CHECK-NEXT: ret i8 [[C]]
; CHECK-NEXT: ret i8 1
;
entry:
%cast = trunc nuw i8 %a to i1
Expand All @@ -2268,11 +2262,9 @@ define i8 @test_trunc_nuw_or_2(i8 %a) {
; CHECK-NEXT: [[CAST:%.*]] = trunc nuw i8 [[A:%.*]] to i1
; CHECK-NEXT: br i1 [[CAST]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[B:%.*]] = or i8 [[A]], 2
; CHECK-NEXT: ret i8 [[B]]
; CHECK-NEXT: ret i8 2
; CHECK: if.else:
; CHECK-NEXT: [[C:%.*]] = or i8 [[A]], 2
; CHECK-NEXT: ret i8 [[C]]
; CHECK-NEXT: ret i8 3
;
entry:
%cast = trunc nuw i8 %a to i1
Expand All @@ -2293,11 +2285,9 @@ define i8 @test_not_trunc_nuw_and_1(i8 %a) {
; CHECK-NEXT: [[CAST:%.*]] = trunc nuw i8 [[A:%.*]] to i1
; CHECK-NEXT: br i1 [[CAST]], label [[IF_ELSE:%.*]], label [[IF_THEN:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[B:%.*]] = and i8 [[A]], 1
; CHECK-NEXT: ret i8 [[B]]
; CHECK-NEXT: ret i8 0
; CHECK: if.else:
; CHECK-NEXT: [[C:%.*]] = and i8 [[A]], 1
; CHECK-NEXT: ret i8 [[C]]
; CHECK-NEXT: ret i8 1
;
entry:
%cast = trunc nuw i8 %a to i1
Expand All @@ -2319,8 +2309,7 @@ define i8 @test_trunc_cond_and(i8 %x, i1 %c) {
; CHECK-NEXT: [[COND:%.*]] = and i1 [[C:%.*]], [[CMP]]
; CHECK-NEXT: br i1 [[COND]], label [[IF:%.*]], label [[EXIT:%.*]]
; CHECK: if:
; CHECK-NEXT: [[OR1:%.*]] = or i8 [[X]], -2
; CHECK-NEXT: ret i8 [[OR1]]
; CHECK-NEXT: ret i8 -1
; CHECK: exit:
; CHECK-NEXT: [[OR2:%.*]] = or i8 [[X]], -2
; CHECK-NEXT: ret i8 [[OR2]]
Expand All @@ -2345,8 +2334,7 @@ define i8 @test_not_trunc_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]], -2
; CHECK-NEXT: ret i8 [[OR1]]
; CHECK-NEXT: ret i8 -2
; CHECK: exit:
; CHECK-NEXT: [[OR2:%.*]] = or i8 [[X]], -2
; CHECK-NEXT: ret i8 [[OR2]]
Expand Down