Skip to content

Commit db5f904

Browse files
committed
[APInt] remove getClearedMemory and improve slow initialization
1 parent 647f892 commit db5f904

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

llvm/lib/Support/APInt.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ using namespace llvm;
3131

3232
#define DEBUG_TYPE "apint"
3333

34-
/// A utility function for allocating memory, checking for allocation failures,
35-
/// and ensuring the contents are zeroed.
36-
inline static uint64_t* getClearedMemory(unsigned numWords) {
37-
uint64_t *result = new uint64_t[numWords];
38-
memset(result, 0, numWords * sizeof(uint64_t));
39-
return result;
40-
}
41-
4234
/// A utility function for allocating memory and checking for allocation
4335
/// failure. The content is not zeroed.
4436
inline static uint64_t* getMemory(unsigned numWords) {
@@ -74,12 +66,15 @@ inline static unsigned getDigit(char cdigit, uint8_t radix) {
7466

7567

7668
void APInt::initSlowCase(uint64_t val, bool isSigned) {
77-
U.pVal = getClearedMemory(getNumWords());
78-
U.pVal[0] = val;
79-
if (isSigned && int64_t(val) < 0)
80-
for (unsigned i = 1; i < getNumWords(); ++i)
81-
U.pVal[i] = WORDTYPE_MAX;
82-
clearUnusedBits();
69+
if (isSigned && int64_t(val) < 0) {
70+
U.pVal = new WordType[getNumWords()];
71+
U.pVal[0] = val;
72+
memset(&U.pVal[1], 0xFF, APINT_WORD_SIZE * (getNumWords() - 1));
73+
clearUnusedBits();
74+
} else {
75+
U.pVal = new WordType[getNumWords()](/* default-initialization */);
76+
U.pVal[0] = val;
77+
}
8378
}
8479

8580
void APInt::initSlowCase(const APInt& that) {
@@ -93,7 +88,7 @@ void APInt::initFromArray(ArrayRef<uint64_t> bigVal) {
9388
U.VAL = bigVal[0];
9489
else {
9590
// Get memory, cleared to 0
96-
U.pVal = getClearedMemory(getNumWords());
91+
U.pVal = new WordType[getNumWords()](/* default-initialization */);
9792
// Calculate the number of words to copy
9893
unsigned words = std::min<unsigned>(bigVal.size(), getNumWords());
9994
// Copy the words from bigVal to pVal
@@ -2105,7 +2100,7 @@ void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) {
21052100
if (isSingleWord())
21062101
U.VAL = 0;
21072102
else
2108-
U.pVal = getClearedMemory(getNumWords());
2103+
U.pVal = new WordType[getNumWords()](/* default-initialization */);
21092104

21102105
// Figure out if we can shift instead of multiply
21112106
unsigned shift = (radix == 16 ? 4 : radix == 8 ? 3 : radix == 2 ? 1 : 0);

0 commit comments

Comments
 (0)