Skip to content
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

[ValueTracking] Handle not in dominating condition. #126423

Merged
merged 1 commit into from
Feb 10, 2025

Conversation

andjo403
Copy link
Contributor

@andjo403 andjo403 commented Feb 9, 2025

General handling of not in dominating condition.
Split out from #126414

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

@llvmbot
Copy link
Member

llvmbot commented Feb 9, 2025

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-llvm-transforms

Author: Andreas Jonson (andjo403)

Changes

General handling of not split out from #126414

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


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

3 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+10)
  • (modified) llvm/test/Transforms/InstCombine/fpclass-from-dom-cond.ll (+1-2)
  • (modified) llvm/test/Transforms/InstCombine/known-bits.ll (+1-2)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 8a9ad55366ee703..6163d6453db3514 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -801,6 +801,9 @@ static void computeKnownBitsFromCond(const Value *V, Value *Cond,
 
   if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
     computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
+
+  if (Depth < MaxAnalysisRecursionDepth && match(Cond, m_Not(m_Value(A))))
+    computeKnownBitsFromCond(V, A, Known, Depth + 1, SQ, !Invert);
 }
 
 void llvm::computeKnownBitsFromContext(const Value *V, KnownBits &Known,
@@ -4934,6 +4937,11 @@ static void computeKnownFPClassFromCond(const Value *V, Value *Cond,
                                 KnownFromContext);
     return;
   }
+  if (Depth < MaxAnalysisRecursionDepth && match(Cond, m_Not(m_Value(A)))) {
+    computeKnownFPClassFromCond(V, A, Depth + 1, !CondIsTrue, CxtI,
+                                KnownFromContext);
+    return;
+  }
   CmpPredicate Pred;
   Value *LHS;
   uint64_t ClassVal = 0;
@@ -10272,6 +10280,8 @@ void llvm::findValuesAffectedByCondition(
                                                            m_Value()))) {
       // Handle patterns that computeKnownFPClass() support.
       AddAffected(A);
+    } else if (match(V, m_Not(m_Value(X)))) {
+      Worklist.push_back(X);
     }
   }
 }
diff --git a/llvm/test/Transforms/InstCombine/fpclass-from-dom-cond.ll b/llvm/test/Transforms/InstCombine/fpclass-from-dom-cond.ll
index e6df7fab356b4a5..934852d1ca8bc42 100644
--- a/llvm/test/Transforms/InstCombine/fpclass-from-dom-cond.ll
+++ b/llvm/test/Transforms/InstCombine/fpclass-from-dom-cond.ll
@@ -528,8 +528,7 @@ define i1 @test_inv_and(float %x, i1 %cond2) {
 ; CHECK-NEXT:    [[AND:%.*]] = and i1 [[COND2]], [[NOT]]
 ; CHECK-NEXT:    br i1 [[AND]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
 ; CHECK:       if.then:
-; CHECK-NEXT:    [[RET1:%.*]] = fcmp oeq float [[X]], 0x7FF0000000000000
-; CHECK-NEXT:    ret i1 [[RET1]]
+; CHECK-NEXT:    ret i1 false
 ; CHECK:       if.else:
 ; CHECK-NEXT:    ret i1 false
 ;
diff --git a/llvm/test/Transforms/InstCombine/known-bits.ll b/llvm/test/Transforms/InstCombine/known-bits.ll
index 7563a63f607f04d..b729cbd971accce 100644
--- a/llvm/test/Transforms/InstCombine/known-bits.ll
+++ b/llvm/test/Transforms/InstCombine/known-bits.ll
@@ -2374,8 +2374,7 @@ define i8 @test_inv_cond_and(i8 %x, i1 %c) {
 ; CHECK-NEXT:    [[COND:%.*]] = and i1 [[C:%.*]], [[NOT]]
 ; CHECK-NEXT:    br i1 [[COND]], label [[IF:%.*]], label [[EXIT:%.*]]
 ; CHECK:       if:
-; CHECK-NEXT:    [[OR1:%.*]] = or i8 [[X]], -4
-; CHECK-NEXT:    ret i8 [[OR1]]
+; CHECK-NEXT:    ret i8 -4
 ; CHECK:       exit:
 ; CHECK-NEXT:    [[OR2:%.*]] = or i8 [[X]], -4
 ; CHECK-NEXT:    ret i8 [[OR2]]

@andjo403 andjo403 force-pushed the notCondInDominatingCond branch from dd461ed to ee741c0 Compare February 9, 2025 19:32
@@ -10272,6 +10280,8 @@ void llvm::findValuesAffectedByCondition(
m_Value()))) {
// Handle patterns that computeKnownFPClass() support.
AddAffected(A);
} else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we drop the !IsAssume here and instead drop the m_Not special case for assumes above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to be able to do that isEphemeralValueOf need to be updated to handle that.
example of test that will fail as the assume is optimized away.

define void @pr47496(i8 %x) {
; CHECK-LABEL: @pr47496(
; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0
; CHECK-NEXT: [[XOR:%.*]] = xor i1 [[CMP]], true
; CHECK-NEXT: call void @llvm.assume(i1 [[XOR]])
; CHECK-NEXT: call void @use(i1 false)
; CHECK-NEXT: ret void
;
%cmp = icmp slt i8 %x, 0
%xor = xor i1 %cmp, true
call void @llvm.assume(i1 %xor)
call void @use(i1 %cmp)
ret void
}

Maybe all instructions with boolean results building up the condition for the assume shall be Ephemeral.

Copy link
Contributor Author

@andjo403 andjo403 Feb 9, 2025

Choose a reason for hiding this comment

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

Have some follow up changes that update isEphemeralValueOf here main...andjo403:llvm-project:assumeNotCond but probably better as multiple PRs if that is something that we want

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 force-pushed the notCondInDominatingCond branch from ee741c0 to 394ba6d Compare February 10, 2025 17:12
@andjo403 andjo403 merged commit 9e0077c into llvm:main Feb 10, 2025
5 of 6 checks passed
@andjo403 andjo403 deleted the notCondInDominatingCond branch February 10, 2025 17:16
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
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