diff options
author | Yingwei Zheng <dtcxzyw2333@gmail.com> | 2024-02-14 15:33:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-14 15:33:08 +0800 |
commit | dc866ae49e9d461c836a113472ea3ff314bb4a80 (patch) | |
tree | 3183287e26fe4b1ab0052ecc973584657c65fd4a /llvm/lib/Analysis/ValueTracking.cpp | |
parent | b5d694ba14524e0161421b13c875747d5fa917de (diff) | |
download | llvm-dc866ae49e9d461c836a113472ea3ff314bb4a80.zip llvm-dc866ae49e9d461c836a113472ea3ff314bb4a80.tar.gz llvm-dc866ae49e9d461c836a113472ea3ff314bb4a80.tar.bz2 |
[ValueTracking] Move the `isSignBitCheck` helper into ValueTracking. NFC. (#81704)
This patch moves the `isSignBitCheck` helper into ValueTracking to reuse
the logic in ValueTracking/InstSimplify.
Addresses the comment
https://github.com/llvm/llvm-project/pull/80740#discussion_r1488440050.
Diffstat (limited to 'llvm/lib/Analysis/ValueTracking.cpp')
-rw-r--r-- | llvm/lib/Analysis/ValueTracking.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 92c9162..6c42fac 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -3810,6 +3810,45 @@ void KnownFPClass::propagateCanonicalizingSrc(const KnownFPClass &Src, propagateNaN(Src, /*PreserveSign=*/true); } +/// Given an exploded icmp instruction, return true if the comparison only +/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if +/// the result of the comparison is true when the input value is signed. +bool llvm::isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, + bool &TrueIfSigned) { + switch (Pred) { + case ICmpInst::ICMP_SLT: // True if LHS s< 0 + TrueIfSigned = true; + return RHS.isZero(); + case ICmpInst::ICMP_SLE: // True if LHS s<= -1 + TrueIfSigned = true; + return RHS.isAllOnes(); + case ICmpInst::ICMP_SGT: // True if LHS s> -1 + TrueIfSigned = false; + return RHS.isAllOnes(); + case ICmpInst::ICMP_SGE: // True if LHS s>= 0 + TrueIfSigned = false; + return RHS.isZero(); + case ICmpInst::ICMP_UGT: + // True if LHS u> RHS and RHS == sign-bit-mask - 1 + TrueIfSigned = true; + return RHS.isMaxSignedValue(); + case ICmpInst::ICMP_UGE: + // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc) + TrueIfSigned = true; + return RHS.isMinSignedValue(); + case ICmpInst::ICMP_ULT: + // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc) + TrueIfSigned = false; + return RHS.isMinSignedValue(); + case ICmpInst::ICMP_ULE: + // True if LHS u<= RHS and RHS == sign-bit-mask - 1 + TrueIfSigned = false; + return RHS.isMaxSignedValue(); + default: + return false; + } +} + /// Returns a pair of values, which if passed to llvm.is.fpclass, returns the /// same result as an fcmp with the given operands. std::pair<Value *, FPClassTest> llvm::fcmpToClassTest(FCmpInst::Predicate Pred, |