[llvm] [InstCombine] Fold trunc nuw X to i1 -> true IFF X != 0 (PR #119131)
Andreas Jonson via llvm-commits
llvm-commits at lists.llvm.org
Sun Dec 8 08:54:20 PST 2024
https://github.com/andjo403 created https://github.com/llvm/llvm-project/pull/119131
This is a fairly common pattern when I tried it on llvm-opt-benchmark over 100 files updated.
proof https://alive2.llvm.org/ce/z/prpPex
>From ea351b08d326232bd3e1a6c894afaa86a295185e Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Sun, 8 Dec 2024 15:05:36 +0100
Subject: [PATCH] [InstCombine] Fold trunc nuw X to i1 -> true IFF X != 0
---
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp | 4 ++++
llvm/test/Transforms/InstCombine/trunc.ll | 3 +--
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 102b784169ca7d..e15d87289bb97f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -934,6 +934,10 @@ Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) {
}
}
+ if (DestWidth == 1 && Trunc.hasNoUnsignedWrap() &&
+ 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 f29ecb8c313738..22cb66d1916662 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)
More information about the llvm-commits
mailing list