[llvm] 39562de - [ValueTracking] Handle assume(trunc x to i1) in ComputeKnownBits (#118406)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 10 10:29:32 PDT 2025
Author: Andreas Jonson
Date: 2025-04-10T19:29:28+02:00
New Revision: 39562de51007e9d6a2ce444475a7b78fba9fafcb
URL: https://github.com/llvm/llvm-project/commit/39562de51007e9d6a2ce444475a7b78fba9fafcb
DIFF: https://github.com/llvm/llvm-project/commit/39562de51007e9d6a2ce444475a7b78fba9fafcb.diff
LOG: [ValueTracking] Handle assume(trunc x to i1) in ComputeKnownBits (#118406)
proof: https://alive2.llvm.org/ce/z/zAspzb
Added:
Modified:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/InstCombine/assume.ll
llvm/test/Transforms/InstSimplify/shr-nop.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index d9c55330f8664..ff8f3b6ae3bc8 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -910,6 +910,16 @@ void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,
Known.setAllZero();
return;
}
+ auto *Trunc = dyn_cast<TruncInst>(Arg);
+ if (Trunc && Trunc->getOperand(0) == V &&
+ isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
+ if (Trunc->hasNoUnsignedWrap()) {
+ Known = KnownBits::makeConstant(APInt(BitWidth, 1));
+ return;
+ }
+ Known.One.setBit(0);
+ return;
+ }
// The remaining tests are all recursive, so bail out if we hit the limit.
if (Depth == MaxAnalysisRecursionDepth)
diff --git a/llvm/test/Transforms/InstCombine/assume.ll b/llvm/test/Transforms/InstCombine/assume.ll
index 1f8fa1716b2aa..e87a61a57ea47 100644
--- a/llvm/test/Transforms/InstCombine/assume.ll
+++ b/llvm/test/Transforms/InstCombine/assume.ll
@@ -1013,8 +1013,7 @@ define i1 @assume_trunc_nuw_eq_one(i8 %x) {
; CHECK-LABEL: @assume_trunc_nuw_eq_one(
; CHECK-NEXT: [[A:%.*]] = trunc nuw i8 [[X:%.*]] to i1
; CHECK-NEXT: call void @llvm.assume(i1 [[A]])
-; CHECK-NEXT: [[Q:%.*]] = icmp eq i8 [[X]], 1
-; CHECK-NEXT: ret i1 [[Q]]
+; CHECK-NEXT: ret i1 true
;
%a = trunc nuw i8 %x to i1
call void @llvm.assume(i1 %a)
diff --git a/llvm/test/Transforms/InstSimplify/shr-nop.ll b/llvm/test/Transforms/InstSimplify/shr-nop.ll
index 4b9292aab4bb3..b18a1c6d30e74 100644
--- a/llvm/test/Transforms/InstSimplify/shr-nop.ll
+++ b/llvm/test/Transforms/InstSimplify/shr-nop.ll
@@ -385,8 +385,7 @@ define i8 @exact_lshr_lowbit_set_assume_trunc(i8 %x) {
; CHECK-LABEL: @exact_lshr_lowbit_set_assume_trunc(
; CHECK-NEXT: [[COND:%.*]] = trunc i8 [[X:%.*]] to i1
; CHECK-NEXT: call void @llvm.assume(i1 [[COND]])
-; CHECK-NEXT: [[SHR:%.*]] = lshr exact i8 [[X]], 1
-; CHECK-NEXT: ret i8 [[SHR]]
+; CHECK-NEXT: ret i8 [[X]]
;
%cond = trunc i8 %x to i1
call void @llvm.assume(i1 %cond)
More information about the llvm-commits
mailing list