summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/APInt.cpp
diff options
context:
space:
mode:
authorCraig Topper <craig.topper@gmail.com>2017-05-10 18:15:24 +0000
committerCraig Topper <craig.topper@gmail.com>2017-05-10 18:15:24 +0000
commitecb97da1081225c0014177db63ec1f698d597c68 (patch)
tree86112466d070b143b5cb94b9f953fec9d6cdb1cb /llvm/lib/Support/APInt.cpp
parent6271bc71466d84239e2234af31fa3ba2f32e27a1 (diff)
downloadbcm5719-llvm-ecb97da1081225c0014177db63ec1f698d597c68.tar.gz
bcm5719-llvm-ecb97da1081225c0014177db63ec1f698d597c68.zip
[APInt] Make toString use udivrem instead of calling the divide helper method directly. Do a better job of reusing allocations while looping. NFCI
This lets toString take advantage of the degenerate case checks in udivrem and is just generally cleaner. One minor downside of this is that the divisor APInt now needs to be the same size as Tmp which requires an additional allocation. But we were doing a poor job of reusing allocations before so the new code should still be an improvement. llvm-svn: 302704
Diffstat (limited to 'llvm/lib/Support/APInt.cpp')
-rw-r--r--llvm/lib/Support/APInt.cpp17
1 files changed, 9 insertions, 8 deletions
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 90aba5b3624..227cfbd2e2e 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -1948,22 +1948,23 @@ void APInt::toString(SmallVectorImpl<char> &Str, unsigned Radix,
unsigned ShiftAmt = (Radix == 16 ? 4 : (Radix == 8 ? 3 : 1));
unsigned MaskAmt = Radix - 1;
- while (Tmp != 0) {
+ while (Tmp.getBoolValue()) {
unsigned Digit = unsigned(Tmp.getRawData()[0]) & MaskAmt;
Str.push_back(Digits[Digit]);
Tmp.lshrInPlace(ShiftAmt);
}
} else {
- APInt divisor(Radix == 10? 4 : 8, Radix);
- while (Tmp != 0) {
- APInt APdigit(1, 0);
- APInt tmp2(Tmp.getBitWidth(), 0);
- divide(Tmp, Tmp.getNumWords(), divisor, divisor.getNumWords(), &tmp2,
- &APdigit);
+ APInt divisor(Tmp.getBitWidth(), Radix);
+ APInt APdigit;
+ APInt tmp2(Tmp.getBitWidth(), 0);
+ while (Tmp.getBoolValue()) {
+ udivrem(Tmp, divisor, tmp2, APdigit);
unsigned Digit = (unsigned)APdigit.getZExtValue();
assert(Digit < Radix && "divide failed");
Str.push_back(Digits[Digit]);
- Tmp = tmp2;
+ // Move the quotient into Tmp and move the old allocation of Tmp into
+ // tmp2 to be used on the next loop iteration.
+ std::swap(Tmp, tmp2);
}
}
OpenPOWER on IntegriCloud