aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/ConstantRange.cpp
diff options
context:
space:
mode:
authorRamkumar Ramachandra <ramkumar.ramachandra@codasip.com>2025-02-15 17:25:03 +0000
committerGitHub <noreply@github.com>2025-02-15 17:25:03 +0000
commitb4030040359656ed20cb29de7b3912b6b249e98e (patch)
treef1f3b07d266e3e75c3ce11107723227429b3256e /llvm/lib/IR/ConstantRange.cpp
parentbfdf30e9b3d0b49344a651a5c7cd87be31d255c4 (diff)
downloadllvm-b4030040359656ed20cb29de7b3912b6b249e98e.zip
llvm-b4030040359656ed20cb29de7b3912b6b249e98e.tar.gz
llvm-b4030040359656ed20cb29de7b3912b6b249e98e.tar.bz2
ConstRange: factor and introduce splitPosNeg (NFC) (#126528)
Factor out some code that splits a ConstantRange into positive and negative components, introducing ConstantRange::splitPosNeg.
Diffstat (limited to 'llvm/lib/IR/ConstantRange.cpp')
-rw-r--r--llvm/lib/IR/ConstantRange.cpp27
1 files changed, 16 insertions, 11 deletions
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index 3566435..41e40cd 100644
--- a/llvm/lib/IR/ConstantRange.cpp
+++ b/llvm/lib/IR/ConstantRange.cpp
@@ -95,6 +95,17 @@ KnownBits ConstantRange::toKnownBits() const {
return Known;
}
+std::pair<ConstantRange, ConstantRange> ConstantRange::splitPosNeg() const {
+ uint32_t BW = getBitWidth();
+ APInt Zero = APInt::getZero(BW), One = APInt(BW, 1);
+ APInt SignedMin = APInt::getSignedMinValue(BW);
+ // There are no positive 1-bit values. The 1 would get interpreted as -1.
+ ConstantRange PosFilter =
+ BW == 1 ? getEmpty() : ConstantRange(One, SignedMin);
+ ConstantRange NegFilter(SignedMin, Zero);
+ return {intersectWith(PosFilter), intersectWith(NegFilter)};
+}
+
ConstantRange ConstantRange::makeAllowedICmpRegion(CmpInst::Predicate Pred,
const ConstantRange &CR) {
if (CR.isEmptySet())
@@ -1356,20 +1367,14 @@ ConstantRange::udiv(const ConstantRange &RHS) const {
}
ConstantRange ConstantRange::sdiv(const ConstantRange &RHS) const {
+ APInt Zero = APInt::getZero(getBitWidth());
+ APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
+
// We split up the LHS and RHS into positive and negative components
// and then also compute the positive and negative components of the result
// separately by combining division results with the appropriate signs.
- APInt Zero = APInt::getZero(getBitWidth());
- APInt SignedMin = APInt::getSignedMinValue(getBitWidth());
- // There are no positive 1-bit values. The 1 would get interpreted as -1.
- ConstantRange PosFilter =
- getBitWidth() == 1 ? getEmpty()
- : ConstantRange(APInt(getBitWidth(), 1), SignedMin);
- ConstantRange NegFilter(SignedMin, Zero);
- ConstantRange PosL = intersectWith(PosFilter);
- ConstantRange NegL = intersectWith(NegFilter);
- ConstantRange PosR = RHS.intersectWith(PosFilter);
- ConstantRange NegR = RHS.intersectWith(NegFilter);
+ auto [PosL, NegL] = splitPosNeg();
+ auto [PosR, NegR] = RHS.splitPosNeg();
ConstantRange PosRes = getEmpty();
if (!PosL.isEmptySet() && !PosR.isEmptySet())