Index: lib/IR/ConstantRange.cpp =================================================================== --- lib/IR/ConstantRange.cpp +++ lib/IR/ConstantRange.cpp @@ -567,7 +567,6 @@ return ConstantRange(DstTySize, /*isFullSet=*/true); APInt MaxValue = APInt::getLowBitsSet(getBitWidth(), DstTySize); - APInt MaxBitValue = APInt::getOneBitSet(getBitWidth(), DstTySize); APInt LowerDiv(Lower), UpperDiv(Upper); ConstantRange Union(DstTySize, /*isFullSet=*/false); @@ -591,9 +590,10 @@ // Chop off the most significant bits that are past the destination bitwidth. if (LowerDiv.uge(MaxValue)) { - APInt Div(getBitWidth(), 0); - APInt::udivrem(LowerDiv, MaxBitValue, Div, LowerDiv); - UpperDiv -= MaxBitValue * Div; + // Mask to just the signficant bits and subtract from LowerDiv/UpperDiv. + APInt Adjust = LowerDiv & ~MaxValue; + LowerDiv -= Adjust; + UpperDiv -= Adjust; } if (UpperDiv.ule(MaxValue)) @@ -601,7 +601,8 @@ UpperDiv.trunc(DstTySize)).unionWith(Union); // The truncated value wraps around. Check if we can do better than fullset. - UpperDiv -= MaxBitValue; + UpperDiv -= MaxValue; + UpperDiv -= 1; if (UpperDiv.ult(LowerDiv)) return ConstantRange(LowerDiv.trunc(DstTySize), UpperDiv.trunc(DstTySize)).unionWith(Union);