summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/APInt.cpp
diff options
context:
space:
mode:
authorCraig Topper <craig.topper@gmail.com>2017-05-12 21:45:50 +0000
committerCraig Topper <craig.topper@gmail.com>2017-05-12 21:45:50 +0000
commitb1a71cac4bd5798b9ebc106ef886f46eb4ae648a (patch)
tree63dfcc5ad30d0a0ec3498f3626aeab00b71f679b /llvm/lib/Support/APInt.cpp
parent2579c7c69f08330951d947c51f6f6938f0fdc737 (diff)
downloadbcm5719-llvm-b1a71cac4bd5798b9ebc106ef886f46eb4ae648a.tar.gz
bcm5719-llvm-b1a71cac4bd5798b9ebc106ef886f46eb4ae648a.zip
[APInt] Add early outs for a division by 1 to udiv/urem/udivrem
We already counted the number of bits in the RHS so its pretty cheap to just check if the RHS is 1. Differential Revision: https://reviews.llvm.org/D33154 llvm-svn: 302953
Diffstat (limited to 'llvm/lib/Support/APInt.cpp')
-rw-r--r--llvm/lib/Support/APInt.cpp22
1 files changed, 18 insertions, 4 deletions
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 25d39771a08..369ded7cf14 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -1565,14 +1565,18 @@ APInt APInt::udiv(const APInt& RHS) const {
}
// Get some facts about the LHS and RHS number of bits and words
- unsigned rhsWords = getNumWords(RHS.getActiveBits());
- assert(rhsWords && "Divided by zero???");
unsigned lhsWords = getNumWords(getActiveBits());
+ unsigned rhsBits = RHS.getActiveBits();
+ unsigned rhsWords = getNumWords(rhsBits);
+ assert(rhsWords && "Divided by zero???");
// Deal with some degenerate cases
if (!lhsWords)
// 0 / X ===> 0
return APInt(BitWidth, 0);
+ if (rhsBits == 1)
+ // X / 1 ===> X
+ return *this;
if (lhsWords < rhsWords || this->ult(RHS))
// X / Y ===> 0, iff X < Y
return APInt(BitWidth, 0);
@@ -1611,13 +1615,17 @@ APInt APInt::urem(const APInt& RHS) const {
unsigned lhsWords = getNumWords(getActiveBits());
// Get some facts about the RHS
- unsigned rhsWords = getNumWords(RHS.getActiveBits());
+ unsigned rhsBits = RHS.getActiveBits();
+ unsigned rhsWords = getNumWords(rhsBits);
assert(rhsWords && "Performing remainder operation by zero ???");
// Check the degenerate cases
if (lhsWords == 0)
// 0 % Y ===> 0
return APInt(BitWidth, 0);
+ if (rhsBits == 1)
+ // X % 1 ===> 0
+ return APInt(BitWidth, 0);
if (lhsWords < rhsWords || this->ult(RHS))
// X % Y ===> X, iff X < Y
return *this;
@@ -1662,7 +1670,8 @@ void APInt::udivrem(const APInt &LHS, const APInt &RHS,
// Get some size facts about the dividend and divisor
unsigned lhsWords = getNumWords(LHS.getActiveBits());
- unsigned rhsWords = getNumWords(RHS.getActiveBits());
+ unsigned rhsBits = RHS.getActiveBits();
+ unsigned rhsWords = getNumWords(rhsBits);
assert(rhsWords && "Performing divrem operation by zero ???");
// Check the degenerate cases
@@ -1672,6 +1681,11 @@ void APInt::udivrem(const APInt &LHS, const APInt &RHS,
return;
}
+ if (rhsBits == 1) {
+ Quotient = LHS; // X / 1 ===> X
+ Remainder = 0; // X % 1 ===> 0
+ }
+
if (lhsWords < rhsWords || LHS.ult(RHS)) {
Remainder = LHS; // X % Y ===> X, iff X < Y
Quotient = 0; // X / Y ===> 0, iff X < Y
OpenPOWER on IntegriCloud