@@ -95,6 +95,17 @@ KnownBits ConstantRange::toKnownBits() const {
9595 return Known;
9696}
9797
98+ std::pair<ConstantRange, ConstantRange> ConstantRange::splitPosNeg () const {
99+ uint32_t BW = getBitWidth ();
100+ APInt Zero = APInt::getZero (BW), One = APInt (BW, 1 );
101+ APInt SignedMin = APInt::getSignedMinValue (BW);
102+ // There are no positive 1-bit values. The 1 would get interpreted as -1.
103+ ConstantRange PosFilter =
104+ BW == 1 ? getEmpty () : ConstantRange (One, SignedMin);
105+ ConstantRange NegFilter (SignedMin, Zero);
106+ return {intersectWith (PosFilter), intersectWith (NegFilter)};
107+ }
108+
98109ConstantRange ConstantRange::makeAllowedICmpRegion (CmpInst::Predicate Pred,
99110 const ConstantRange &CR) {
100111 if (CR.isEmptySet ())
@@ -1356,20 +1367,14 @@ ConstantRange::udiv(const ConstantRange &RHS) const {
13561367}
13571368
13581369ConstantRange ConstantRange::sdiv (const ConstantRange &RHS) const {
1370+ APInt Zero = APInt::getZero (getBitWidth ());
1371+ APInt SignedMin = APInt::getSignedMinValue (getBitWidth ());
1372+
13591373 // We split up the LHS and RHS into positive and negative components
13601374 // and then also compute the positive and negative components of the result
13611375 // separately by combining division results with the appropriate signs.
1362- APInt Zero = APInt::getZero (getBitWidth ());
1363- APInt SignedMin = APInt::getSignedMinValue (getBitWidth ());
1364- // There are no positive 1-bit values. The 1 would get interpreted as -1.
1365- ConstantRange PosFilter =
1366- getBitWidth () == 1 ? getEmpty ()
1367- : ConstantRange (APInt (getBitWidth (), 1 ), SignedMin);
1368- ConstantRange NegFilter (SignedMin, Zero);
1369- ConstantRange PosL = intersectWith (PosFilter);
1370- ConstantRange NegL = intersectWith (NegFilter);
1371- ConstantRange PosR = RHS.intersectWith (PosFilter);
1372- ConstantRange NegR = RHS.intersectWith (NegFilter);
1376+ auto [PosL, NegL] = splitPosNeg ();
1377+ auto [PosR, NegR] = RHS.splitPosNeg ();
13731378
13741379 ConstantRange PosRes = getEmpty ();
13751380 if (!PosL.isEmptySet () && !PosR.isEmptySet ())
0 commit comments