-
Notifications
You must be signed in to change notification settings - Fork 14.3k
ConstRange: factor and introduce splitPosNeg (NFC) #126528
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
Conversation
Factor out some code that splits a ConstantRange into positive and negative components, introducing ConstantRange::splitPosNeg.
@llvm/pr-subscribers-llvm-ir Author: Ramkumar Ramachandra (artagnon) ChangesFactor out some code that splits a ConstantRange into positive and negative components, introducing ConstantRange::splitPosNeg. Full diff: https://github.com/llvm/llvm-project/pull/126528.diff 2 Files Affected:
diff --git a/llvm/include/llvm/IR/ConstantRange.h b/llvm/include/llvm/IR/ConstantRange.h
index d086c25390fd22..82af38fcd0b7c4 100644
--- a/llvm/include/llvm/IR/ConstantRange.h
+++ b/llvm/include/llvm/IR/ConstantRange.h
@@ -92,6 +92,9 @@ class [[nodiscard]] ConstantRange {
/// unsigned domain.
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned);
+ /// Split the ConstantRange into positive and negative components.
+ std::pair<ConstantRange, ConstantRange> splitPosNeg() const;
+
/// Produce the smallest range such that all values that may satisfy the given
/// predicate with any value contained within Other is contained in the
/// returned range. Formally, this returns a superset of
diff --git a/llvm/lib/IR/ConstantRange.cpp b/llvm/lib/IR/ConstantRange.cpp
index 35664353989929..d04eb81eed05c7 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);
+ 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(APInt(BW, 1), 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())
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems reasonable on the surface, but I don't understand how this is supposed to help with #125899.
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/27/builds/6177 Here is the relevant piece of the build log for the reference
|
Factor out some code that splits a ConstantRange into positive and negative components, introducing ConstantRange::splitPosNeg.
Factor out some code that splits a ConstantRange into positive and negative components, introducing ConstantRange::splitPosNeg.