[APInt] Optimize umul_ov

Change two costly udiv() calls to lshr(1)*RHS + left-shift + plus

On one 64-bit umul_ov benchmark, I measured an obvious improvement: 12.8129s -> 3.6257s

Note, there may be some value to special case 64-bit (the most common
case) with __builtin_umulll_overflow().

Differential Revision: https://reviews.llvm.org/D60669

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358730 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Fangrui Song
2019-04-19 02:06:06 +00:00
parent 667d4f4107
commit f238d27073
2 changed files with 48 additions and 5 deletions
+12 -5
View File
@@ -1914,12 +1914,19 @@ APInt APInt::smul_ov(const APInt &RHS, bool &Overflow) const {
}
APInt APInt::umul_ov(const APInt &RHS, bool &Overflow) const {
APInt Res = *this * RHS;
if (countLeadingZeros() + RHS.countLeadingZeros() + 2 <= BitWidth) {
Overflow = true;
return *this * RHS;
}
if (*this != 0 && RHS != 0)
Overflow = Res.udiv(RHS) != *this || Res.udiv(*this) != RHS;
else
Overflow = false;
APInt Res = lshr(1) * RHS;
Overflow = Res.isNegative();
Res <<= 1;
if ((*this)[0]) {
Res += RHS;
if (Res.ult(RHS))
Overflow = true;
}
return Res;
}