diff options
author | Nikita Popov <npopov@redhat.com> | 2022-05-16 16:04:11 +0200 |
---|---|---|
committer | Nikita Popov <npopov@redhat.com> | 2022-05-16 16:12:25 +0200 |
commit | 8ab819ad90d647b96bb4b6842a742d2552ba9e9c (patch) | |
tree | b62a5e3a62c7d63f801d09af8ffa46ab19905c1d /llvm/lib/IR/ConstantRange.cpp | |
parent | c70259405c61b203682e2a03c4688c6d6bc89856 (diff) | |
download | llvm-8ab819ad90d647b96bb4b6842a742d2552ba9e9c.zip llvm-8ab819ad90d647b96bb4b6842a742d2552ba9e9c.tar.gz llvm-8ab819ad90d647b96bb4b6842a742d2552ba9e9c.tar.bz2 |
[ConstantRange] Add toKnownBits() method
Add toKnownBits() method to mirror fromKnownBits(). We know the
top bits that are constant between min and max.
The return value for an empty range is chosen to be conservative.
Diffstat (limited to 'llvm/lib/IR/ConstantRange.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantRange.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp index a0f2179..16eb4d0 100644 --- a/llvm/lib/IR/ConstantRange.cpp +++ b/llvm/lib/IR/ConstantRange.cpp @@ -75,6 +75,24 @@ ConstantRange ConstantRange::fromKnownBits(const KnownBits &Known, return ConstantRange(Lower, Upper + 1); } +KnownBits ConstantRange::toKnownBits() const { + // TODO: We could return conflicting known bits here, but consumers are + // likely not prepared for that. + if (isEmptySet()) + return KnownBits(getBitWidth()); + + // We can only retain the top bits that are the same between min and max. + APInt Min = getUnsignedMin(); + APInt Max = getUnsignedMax(); + KnownBits Known = KnownBits::makeConstant(Min); + if (Optional<unsigned> DifferentBit = + APIntOps::GetMostSignificantDifferentBit(Min, Max)) { + Known.Zero.clearLowBits(*DifferentBit + 1); + Known.One.clearLowBits(*DifferentBit + 1); + } + return Known; +} + ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &CR) { if (CR.isEmptySet()) |