[llvm] b7ae853 - [ValueTracking] Handle trunc to i1 condition in isKnownNonNullFromDominatingCondition (#230925)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 11 03:34:38 PDT 2026
Author: Andreas Jonson
Date: 2026-10-11T12:34:33+02:00
New Revision: b7ae8532ff1738bcd449bb3f82dab5135effdbbe
URL: https://github.com/llvm/llvm-project/commit/b7ae8532ff1738bcd449bb3f82dab5135effdbbe
DIFF: https://github.com/llvm/llvm-project/commit/b7ae8532ff1738bcd449bb3f82dab5135effdbbe.diff
LOG: [ValueTracking] Handle trunc to i1 condition in isKnownNonNullFromDominatingCondition (#230925)
Proof: https://alive2.llvm.org/ce/z/hnLV-e
Added:
Modified:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/InstSimplify/known-non-zero.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index f75ec6e622f0a..cf0e04d671cfc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -3120,15 +3120,18 @@ static bool isKnownNonNullFromDominatingCondition(const Value *V,
// Consider only compare instructions uniquely controlling a branch
Value *RHS;
CmpPredicate Pred;
- if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
- continue;
-
bool NonNullIfTrue;
- if (cmpExcludesZero(Pred, RHS))
+ if (match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS)))) {
+ if (cmpExcludesZero(Pred, RHS))
+ NonNullIfTrue = true;
+ else if (cmpExcludesZero(CmpInst::getInversePredicate(Pred), RHS))
+ NonNullIfTrue = false;
+ else
+ continue;
+ } else if (UI->getType()->isIntegerTy(1) &&
+ match(UI, m_Trunc(m_Specific(V)))) {
NonNullIfTrue = true;
- else if (cmpExcludesZero(CmpInst::getInversePredicate(Pred), RHS))
- NonNullIfTrue = false;
- else
+ } else
continue;
SmallVector<const User *, 4> WorkList;
diff --git a/llvm/test/Transforms/InstSimplify/known-non-zero.ll b/llvm/test/Transforms/InstSimplify/known-non-zero.ll
index 1445402d1feb2..4946b7192ee72 100644
--- a/llvm/test/Transforms/InstSimplify/known-non-zero.ll
+++ b/llvm/test/Transforms/InstSimplify/known-non-zero.ll
@@ -583,3 +583,44 @@ define i1 @src_x_uadd.sat_c1_fail(i8 %x, i1 %c1) {
ret i1 %r
}
+define i1 @trunc_dom_cond(i64 %x) {
+; CHECK-LABEL: @trunc_dom_cond(
+; CHECK-NEXT: [[ISZERO:%.*]] = trunc i64 [[X:%.*]] to i1
+; CHECK-NEXT: br i1 [[ISZERO]], label [[NON_ZERO:%.*]], label [[ZERO:%.*]]
+; CHECK: non_zero:
+; CHECK-NEXT: ret i1 false
+; CHECK: zero:
+; CHECK-NEXT: ret i1 false
+;
+ %iszero = trunc i64 %x to i1
+ br i1 %iszero, label %non_zero, label %zero
+
+non_zero:
+ %result = icmp eq i64 %x, 0
+ ret i1 %result
+
+zero:
+ ret i1 false
+}
+
+define i1 @neg_trunc_dom_cond(i64 %x) {
+; CHECK-LABEL: @neg_trunc_dom_cond(
+; CHECK-NEXT: [[ISZERO:%.*]] = trunc i64 [[X:%.*]] to i1
+; CHECK-NEXT: br i1 [[ISZERO]], label [[NON_ZERO:%.*]], label [[ZERO:%.*]]
+; CHECK: non_zero:
+; CHECK-NEXT: ret i1 false
+; CHECK: zero:
+; CHECK-NEXT: [[RESULT:%.*]] = icmp eq i64 [[X]], 0
+; CHECK-NEXT: ret i1 [[RESULT]]
+;
+ %iszero = trunc i64 %x to i1
+ br i1 %iszero, label %non_zero, label %zero
+
+non_zero:
+ ret i1 false
+
+zero:
+ %result = icmp eq i64 %x, 0
+ ret i1 %result
+}
+
More information about the llvm-commits
mailing list