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

via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 13 14:39:39 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Andreas Jonson (andjo403)

<details>
<summary>Changes</summary>

Addresses this comment https://github.com/llvm/llvm-project/pull/126423#discussion_r1948227236.
Also solves the ephemeral issue from https://github.com/llvm/llvm-project/pull/118406 for known bits of x in `assume (not trunc x to i1)`

is it enough with the test in https://github.com/llvm/llvm-project/blob/3d140004c70e2bc79416825e43207e8b711c56d9/llvm/test/Transforms/GVN/assume.ll ?

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


1 Files Affected:

- (modified) llvm/lib/Analysis/ValueTracking.cpp (+22-16) 


``````````diff
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 28d7e1ce401e4..1ac37603172a1 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());
+        }
     }
   }
 
@@ -10214,11 +10224,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);
@@ -10302,8 +10309,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);
     }
   }

``````````

</details>


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


More information about the llvm-commits mailing list