diff options
author | Yingwei Zheng <dtcxzyw2333@gmail.com> | 2023-11-06 19:17:39 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-06 19:17:39 +0800 |
commit | 6f1d3b97c76b26ab68853e28e97368ddddeaa63c (patch) | |
tree | 54efce26d0c01018e0963e4ece516b4917b584e3 /llvm/lib/IR/ConstantRange.cpp | |
parent | 333124cfd6dfa413294e80f9be75460b9a9314f9 (diff) | |
download | llvm-6f1d3b97c76b26ab68853e28e97368ddddeaa63c.zip llvm-6f1d3b97c76b26ab68853e28e97368ddddeaa63c.tar.gz llvm-6f1d3b97c76b26ab68853e28e97368ddddeaa63c.tar.bz2 |
[ConstantRange] Handle `Intrinsic::cttz` (#67917)
This patch adds support for `Intrinsic::cttz` in ConstantRange. It
calculates the range in O(1) with the LCP-based method.
Migrated from https://reviews.llvm.org/D153505.
Diffstat (limited to 'llvm/lib/IR/ConstantRange.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantRange.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp index af93b69..cbb64b2 100644 --- a/llvm/lib/IR/ConstantRange.cpp +++ b/llvm/lib/IR/ConstantRange.cpp @@ -949,6 +949,7 @@ bool ConstantRange::isIntrinsicSupported(Intrinsic::ID IntrinsicID) { case Intrinsic::smax: case Intrinsic::abs: case Intrinsic::ctlz: + case Intrinsic::cttz: case Intrinsic::ctpop: return true; default: @@ -987,6 +988,12 @@ ConstantRange ConstantRange::intrinsic(Intrinsic::ID IntrinsicID, assert(ZeroIsPoison->getBitWidth() == 1 && "Must be boolean"); return Ops[0].ctlz(ZeroIsPoison->getBoolValue()); } + case Intrinsic::cttz: { + const APInt *ZeroIsPoison = Ops[1].getSingleElement(); + assert(ZeroIsPoison && "Must be known (immarg)"); + assert(ZeroIsPoison->getBitWidth() == 1 && "Must be boolean"); + return Ops[0].cttz(ZeroIsPoison->getBoolValue()); + } case Intrinsic::ctpop: return Ops[0].ctpop(); default: @@ -1739,6 +1746,74 @@ ConstantRange ConstantRange::ctlz(bool ZeroIsPoison) const { APInt(getBitWidth(), getUnsignedMin().countl_zero() + 1)); } +static ConstantRange getUnsignedCountTrailingZerosRange(const APInt &Lower, + const APInt &Upper) { + assert(!ConstantRange(Lower, Upper).isWrappedSet() && + "Unexpected wrapped set."); + assert(Lower != Upper && "Unexpected empty set."); + unsigned BitWidth = Lower.getBitWidth(); + if (Lower + 1 == Upper) + return ConstantRange(APInt(BitWidth, Lower.countr_zero())); + if (Lower.isZero()) + return ConstantRange(APInt::getZero(BitWidth), + APInt(BitWidth, BitWidth + 1)); + + // Calculate longest common prefix. + unsigned LCPLength = (Lower ^ (Upper - 1)).countl_zero(); + // If Lower is {LCP, 000...}, the maximum is Lower.countr_zero(). + // Otherwise, the maximum is BitWidth - LCPLength - 1 ({LCP, 100...}). + return ConstantRange( + APInt::getZero(BitWidth), + APInt(BitWidth, + std::max(BitWidth - LCPLength - 1, Lower.countr_zero()) + 1)); +} + +ConstantRange ConstantRange::cttz(bool ZeroIsPoison) const { + if (isEmptySet()) + return getEmpty(); + + unsigned BitWidth = getBitWidth(); + APInt Zero = APInt::getZero(BitWidth); + if (ZeroIsPoison && contains(Zero)) { + // ZeroIsPoison is set, and zero is contained. We discern three cases, in + // which a zero can appear: + // 1) Lower is zero, handling cases of kind [0, 1), [0, 2), etc. + // 2) Upper is zero, wrapped set, handling cases of kind [3, 0], etc. + // 3) Zero contained in a wrapped set, e.g., [3, 2), [3, 1), etc. + + if (Lower.isZero()) { + if (Upper == 1) { + // We have in input interval of kind [0, 1). In this case we cannot + // really help but return empty-set. + return getEmpty(); + } + + // Compute the resulting range by excluding zero from Lower. + return getUnsignedCountTrailingZerosRange(APInt(BitWidth, 1), Upper); + } else if (Upper == 1) { + // Compute the resulting range by excluding zero from Upper. + return getUnsignedCountTrailingZerosRange(Lower, Zero); + } else { + ConstantRange CR1 = getUnsignedCountTrailingZerosRange(Lower, Zero); + ConstantRange CR2 = + getUnsignedCountTrailingZerosRange(APInt(BitWidth, 1), Upper); + return CR1.unionWith(CR2); + } + } + + if (isFullSet()) + return getNonEmpty(Zero, APInt(BitWidth, BitWidth + 1)); + if (!isWrappedSet()) + return getUnsignedCountTrailingZerosRange(Lower, Upper); + // The range is wrapped. We decompose it into two ranges, [0, Upper) and + // [Lower, 0). + // Handle [Lower, 0) + ConstantRange CR1 = getUnsignedCountTrailingZerosRange(Lower, Zero); + // Handle [0, Upper) + ConstantRange CR2 = getUnsignedCountTrailingZerosRange(Zero, Upper); + return CR1.unionWith(CR2); +} + static ConstantRange getUnsignedPopCountRange(const APInt &Lower, const APInt &Upper) { assert(!ConstantRange(Lower, Upper).isWrappedSet() && |