[llvm] [ValueTracking] Add fast path to avoid second recursive call in `isKnownPositive`; NFC (PR #83638)

via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 1 16:51:35 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: None (goldsteinn)

<details>
<summary>Changes</summary>

Just a simple compile time improvement. This function isn't used much,
however, so its not particularly impactful.

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


1 Files Affected:

- (modified) llvm/lib/Analysis/ValueTracking.cpp (+5-3) 


``````````diff
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index e591ac504e9f05..3d25585a9f9353 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -293,9 +293,11 @@ bool llvm::isKnownPositive(const Value *V, const SimplifyQuery &SQ,
   if (auto *CI = dyn_cast<ConstantInt>(V))
     return CI->getValue().isStrictlyPositive();
 
-  // TODO: We'd doing two recursive queries here.  We should factor this such
-  // that only a single query is needed.
-  return isKnownNonNegative(V, SQ, Depth) && ::isKnownNonZero(V, Depth, SQ);
+  // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
+  // this updated.
+  KnownBits Known = computeKnownBits(V, Depth, SQ);
+  return Known.isNonNegative() &&
+         (Known.isNonZero() || ::isKnownNonZero(V, Depth, SQ));
 }
 
 bool llvm::isKnownNegative(const Value *V, const SimplifyQuery &SQ,

``````````

</details>


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


More information about the llvm-commits mailing list