[llvm] [ValueTracking] Handle not in assume argument in isEphemeralValueOf. (PR #127140)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 15 00:36:20 PST 2025


https://github.com/andjo403 updated https://github.com/llvm/llvm-project/pull/127140

>From 1f5a2457b1fde722ad77bc808c66b4ec43f9afbe Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Tue, 11 Feb 2025 22:17:44 +0100
Subject: [PATCH] [ValueTracking] Handle not cond to assume.

---
 llvm/lib/Analysis/ValueTracking.cpp        | 38 +++++++++++++---------
 llvm/test/Transforms/InstCombine/assume.ll |  3 +-
 2 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 2a49a10447e0b..3f94a82d08417 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -441,9 +441,6 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
 }
 
 static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
-  SmallVector<const Value *, 16> WorkSet(1, I);
-  SmallPtrSet<const Value *, 32> Visited;
-  SmallPtrSet<const Value *, 16> EphValues;
 
   // The instruction defining an assumption's condition itself is always
   // considered ephemeral to that assumption (even if it has other
@@ -451,6 +448,21 @@ static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
   if (is_contained(I->operands(), E))
     return true;
 
+  SmallPtrSet<const Value *, 32> Visited{I};
+  SmallPtrSet<const Value *, 16> EphValues{I};
+
+  Value *X;
+  if (match(I, m_Intrinsic<Intrinsic::assume>(m_Not(m_Value(X))))) {
+    if (X == E)
+      return true;
+    auto *Not = cast<Instruction>(I->getOperand(0));
+    Visited.insert(Not);
+    EphValues.insert(Not);
+    I = Not;
+  }
+
+  SmallVector<const Value *, 16> WorkSet(I->operands());
+
   while (!WorkSet.empty()) {
     const Value *V = WorkSet.pop_back_val();
     if (!Visited.insert(V).second)
@@ -463,13 +475,11 @@ static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
       if (V == E)
         return true;
 
-      if (V == I || (isa<Instruction>(V) &&
-                     !cast<Instruction>(V)->mayHaveSideEffects() &&
-                     !cast<Instruction>(V)->isTerminator())) {
-       EphValues.insert(V);
-       if (const User *U = dyn_cast<User>(V))
-         append_range(WorkSet, U->operands());
-      }
+      if (const auto *II = dyn_cast<Instruction>(V))
+        if (!II->mayHaveSideEffects() && !II->isTerminator()) {
+          EphValues.insert(V);
+          append_range(WorkSet, II->operands());
+        }
     }
   }
 
@@ -10258,11 +10268,8 @@ void llvm::findValuesAffectedByCondition(
     CmpPredicate Pred;
     Value *A, *B, *X;
 
-    if (IsAssume) {
+    if (IsAssume)
       AddAffected(V);
-      if (match(V, m_Not(m_Value(X))))
-        AddAffected(X);
-    }
 
     if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
       // assume(A && B) is split to -> assume(A); assume(B);
@@ -10347,8 +10354,7 @@ void llvm::findValuesAffectedByCondition(
       // Assume is checked here as X is already added above for assumes in
       // addValueAffectedByCondition
       AddAffected(X);
-    } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
-      // Assume is checked here to avoid issues with ephemeral values
+    } else if (match(V, m_Not(m_Value(X)))) {
       Worklist.push_back(X);
     }
   }
diff --git a/llvm/test/Transforms/InstCombine/assume.ll b/llvm/test/Transforms/InstCombine/assume.ll
index 0007cc1518730..ed133342d1775 100644
--- a/llvm/test/Transforms/InstCombine/assume.ll
+++ b/llvm/test/Transforms/InstCombine/assume.ll
@@ -983,8 +983,7 @@ define i1 @not_cond_use(i8 %x) {
 ; CHECK-NEXT:    tail call void @use(i1 [[CMP]])
 ; CHECK-NEXT:    [[NOT:%.*]] = xor i1 [[CMP]], true
 ; CHECK-NEXT:    tail call void @llvm.assume(i1 [[NOT]])
-; CHECK-NEXT:    [[RVAL:%.*]] = icmp eq i8 [[X]], 0
-; CHECK-NEXT:    ret i1 [[RVAL]]
+; CHECK-NEXT:    ret i1 false
 ;
   %cmp = icmp eq i8 %x, 0
   tail call void @use(i1 %cmp)



More information about the llvm-commits mailing list