[llvm] 855bc46 - [InstCombine] Fold trunc nuw/nsw X to i1 -> true IFF X != 0 (#119131)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 8 13:28:18 PST 2024
Author: Andreas Jonson
Date: 2024-12-08T22:28:16+01:00
New Revision: 855bc46bc810c4ae97ea9f427429a7efd8b9cc15
URL: https://github.com/llvm/llvm-project/commit/855bc46bc810c4ae97ea9f427429a7efd8b9cc15
DIFF: https://github.com/llvm/llvm-project/commit/855bc46bc810c4ae97ea9f427429a7efd8b9cc15.diff
LOG: [InstCombine] Fold trunc nuw/nsw X to i1 -> true IFF X != 0 (#119131)
proof https://alive2.llvm.org/ce/z/prpPex
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/test/Transforms/InstCombine/trunc.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 102b784169ca7d..0b9379965f4249 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -934,6 +934,11 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
}
}
+ if (DestWidth == 1 &&
+ (Trunc.hasNoUnsignedWrap() || Trunc.hasNoSignedWrap()) &&
+ isKnownNonZero(Src, SQ.getWithInstruction(&Trunc)))
+ return replaceInstUsesWith(Trunc, ConstantInt::getTrue(DestTy));
+
bool Changed = false;
if (!Trunc.hasNoSignedWrap() &&
ComputeMaxSignificantBits(Src, /*Depth=*/0, &Trunc) <= DestWidth) {
diff --git a/llvm/test/Transforms/InstCombine/trunc.ll b/llvm/test/Transforms/InstCombine/trunc.ll
index fda99dd5ac3e3b..a85ce716fbdfab 100644
--- a/llvm/test/Transforms/InstCombine/trunc.ll
+++ b/llvm/test/Transforms/InstCombine/trunc.ll
@@ -1129,8 +1129,7 @@ define i1 @trunc_nuw_i1_non_zero(i8 %1) {
; CHECK-LABEL: @trunc_nuw_i1_non_zero(
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i8 [[TMP0:%.*]], 0
; CHECK-NEXT: tail call void @llvm.assume(i1 [[TMP2]])
-; CHECK-NEXT: [[RET:%.*]] = trunc nuw i8 [[TMP0]] to i1
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: ret i1 true
;
%3 = icmp ne i8 %1, 0
tail call void @llvm.assume(i1 %3)
@@ -1177,8 +1176,7 @@ define i1 @trunc_nsw_i1_non_zero(i8 %1) {
; CHECK-LABEL: @trunc_nsw_i1_non_zero(
; CHECK-NEXT: [[TMP2:%.*]] = icmp ne i8 [[TMP0:%.*]], 0
; CHECK-NEXT: tail call void @llvm.assume(i1 [[TMP2]])
-; CHECK-NEXT: [[RET:%.*]] = trunc nsw i8 [[TMP0]] to i1
-; CHECK-NEXT: ret i1 [[RET]]
+; CHECK-NEXT: ret i1 true
;
%3 = icmp ne i8 %1, 0
tail call void @llvm.assume(i1 %3)
More information about the llvm-commits
mailing list