Skip to content

[ValueTracking] Add fast path to avoid second recursive call in isKnownPositive; NFC #83638

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

Closed
wants to merge 1 commit into from

Conversation

goldsteinn
Copy link
Contributor

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

…ownPositive`; NFC

Just a simple compile time improvement. This function isn't used much,
however, so its not particularly impactful.
@goldsteinn goldsteinn requested a review from nikic as a code owner March 2, 2024 00:51
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Mar 2, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 2, 2024

@llvm/pr-subscribers-llvm-analysis

Author: None (goldsteinn)

Changes

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 --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,

@goldsteinn goldsteinn requested a review from dtcxzyw March 2, 2024 00:51
@dtcxzyw
Copy link
Member

dtcxzyw commented Mar 2, 2024

@goldsteinn
Copy link
Contributor Author

goldsteinn commented Mar 2, 2024

FYI https://dtcxzyw.github.io/llvm-opt-benchmark/coverage/home/dtcxzyw/llvm-project/llvm/lib/Analysis/ValueTracking.cpp.html#L291

Neat :)
so it will save maybe a few calls, but not much.

Edit: I'm not so attached this patch, just saw it and figured why not, but either way...

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

@goldsteinn goldsteinn closed this in c5aacb0 Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants