Skip to content

[ValueTracking] Handle assume(trunc x to i1) in knownNonZero #135055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

andjo403
Copy link
Contributor

@andjo403 andjo403 commented Apr 9, 2025

@llvmbot
Copy link
Member

llvmbot commented Apr 9, 2025

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-transforms

Author: Andreas Jonson (andjo403)

Changes

Split out from #118406

proof: https://alive2.llvm.org/ce/z/zAspzb


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

3 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+6-1)
  • (modified) llvm/test/Transforms/Attributor/lvi-after-jumpthreading.ll (+1-4)
  • (modified) llvm/test/Transforms/InstSimplify/assume-non-zero.ll (+1-2)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index d9c55330f8664..900ada0e37cc4 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -646,11 +646,16 @@ static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
     // Warning: This loop can end up being somewhat performance sensitive.
     // We're running this loop for once for each value queried resulting in a
     // runtime of ~O(#assumes * #values).
+    Value *Arg = I->getArgOperand(0);
+    if (match(Arg, m_TruncOrSelf(m_Specific(V))) &&
+        isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
+      return true;
+    }
 
     Value *RHS;
     CmpPredicate Pred;
     auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
-    if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
+    if (!match(Arg, m_c_ICmp(Pred, m_V, m_Value(RHS))))
       continue;
 
     if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
diff --git a/llvm/test/Transforms/Attributor/lvi-after-jumpthreading.ll b/llvm/test/Transforms/Attributor/lvi-after-jumpthreading.ll
index 2aa95216a6656..fd9d2e4de61ba 100644
--- a/llvm/test/Transforms/Attributor/lvi-after-jumpthreading.ll
+++ b/llvm/test/Transforms/Attributor/lvi-after-jumpthreading.ll
@@ -130,8 +130,6 @@ exit2:
   ret i32 30
 }
 
-; FIXME: We should be able to merge cont into do.
-; FIXME: COND should be replaced with false. This will be fixed by improving LVI.
 define i32 @test4(i32 %i, i1 %f, i32 %n) {
 ; CHECK-LABEL: define {{[^@]+}}@test4
 ; CHECK-SAME: (i32 [[I:%.*]], i1 [[F:%.*]], i32 [[N:%.*]]) {
@@ -148,8 +146,7 @@ define i32 @test4(i32 %i, i1 %f, i32 %n) {
 ; CHECK-NEXT:    call void @dummy(i1 [[F]]) #[[ATTR2]]
 ; CHECK-NEXT:    [[CONSUME:%.*]] = call i32 @exit()
 ; CHECK-NEXT:    call void @llvm.assume(i1 noundef [[F]])
-; CHECK-NEXT:    [[COND:%.*]] = icmp eq i1 [[F]], false
-; CHECK-NEXT:    br i1 [[COND]], label [[EXIT]], label [[CONT:%.*]]
+; CHECK-NEXT:    br label [[CONT:%.*]]
 ; CHECK:       exit2:
 ; CHECK-NEXT:    ret i32 30
 ;
diff --git a/llvm/test/Transforms/InstSimplify/assume-non-zero.ll b/llvm/test/Transforms/InstSimplify/assume-non-zero.ll
index 22bdf374487da..331c9504f2da6 100644
--- a/llvm/test/Transforms/InstSimplify/assume-non-zero.ll
+++ b/llvm/test/Transforms/InstSimplify/assume-non-zero.ll
@@ -236,8 +236,7 @@ define i1 @nonnull_trunc_true(i8 %x) {
 ; CHECK-LABEL: @nonnull_trunc_true(
 ; CHECK-NEXT:    [[A:%.*]] = trunc i8 [[X:%.*]] to i1
 ; CHECK-NEXT:    call void @llvm.assume(i1 [[A]])
-; CHECK-NEXT:    [[Q:%.*]] = icmp ne i8 [[X]], 0
-; CHECK-NEXT:    ret i1 [[Q]]
+; CHECK-NEXT:    ret i1 true
 ;
   %a = trunc i8 %x to i1
   call void @llvm.assume(i1 %a)

@andjo403
Copy link
Contributor Author

@dtcxzyw I do not understand why the test do not fail for you when the code is removed #118406 (comment)

@dtcxzyw
Copy link
Member

dtcxzyw commented Apr 12, 2025

@dtcxzyw I do not understand why the test do not fail for you when the code is removed #118406 (comment)

They failed because they ran InstSimplify/Attributor. I mean InstCombine can optimize these cases without the change to is KnownNonZero .

@dtcxzyw
Copy link
Member

dtcxzyw commented Apr 12, 2025

No diff: dtcxzyw/llvm-opt-benchmark#2253

@andjo403
Copy link
Contributor Author

thanks for the explanation.
I also tested this after the fold icmp ne (and X, 1), 0 --> trunc X to i1 and also did not get any diff for llvm-opt-benchmark so seems like it make no change.

@andjo403 andjo403 closed this Apr 12, 2025
@andjo403 andjo403 deleted the truncAssumeNonZero branch April 12, 2025 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants