[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:06 PST 2024


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

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

>From ce3ab9687e1a7f7fcf72e0b13ccc5f4b4a332c58 Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Fri, 1 Mar 2024 18:48:45 -0600
Subject: [PATCH] [ValueTracking] Add fast path to avoid second recursive call
 in `isKnownPositive`; NFC

Just a simple compile time improvement. This function isn't used much,
however, so its not particularly impactful.
---
 llvm/lib/Analysis/ValueTracking.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

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,



More information about the llvm-commits mailing list