Skip to content

Conversation

@andjo403
Copy link
Contributor

@andjo403 andjo403 commented Dec 8, 2024

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

@andjo403 andjo403 requested a review from dtcxzyw December 8, 2024 16:54
@andjo403 andjo403 requested a review from nikic as a code owner December 8, 2024 16:54
@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms labels Dec 8, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 8, 2024

@llvm/pr-subscribers-llvm-transforms

Author: Andreas Jonson (andjo403)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/119131.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp (+4)
  • (modified) llvm/test/Transforms/InstCombine/trunc.ll (+1-2)
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)

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

This also holds for trunc nsw: https://alive2.llvm.org/ce/z/5oGGAj

@andjo403 andjo403 force-pushed the foldTruncNuwI1NonZeroToTrue branch from ea351b0 to b118f6d Compare December 8, 2024 21:09
@andjo403 andjo403 changed the title [InstCombine] Fold trunc nuw X to i1 -> true IFF X != 0 [InstCombine] Fold trunc nuw/nsw X to i1 -> true IFF X != 0 Dec 8, 2024
@andjo403
Copy link
Contributor Author

andjo403 commented Dec 8, 2024

good catch fixed now

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@andjo403 andjo403 merged commit 855bc46 into llvm:main Dec 8, 2024
5 of 8 checks passed
@andjo403 andjo403 deleted the foldTruncNuwI1NonZeroToTrue branch December 8, 2024 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants