Skip to content

Commit 427e202

Browse files
authored
[APInt] improve initialization performance (#106945)
The purpose is to save an extra memset in both cases: 1. When `int64_t(val) < 0`, zeroing out is redundant as the subsequent for-loop will initialize to `val .. 0xFFFFF ....`. Instead we should only create an uninitialized buffer, and transform the slow for-loop into a memset to initialize the higher words to `0xFF`. 2. In the other case, first we create an uninitialized array (`new int64_t[]`) and _then_ we zero it out with `memset`. But this can be combined in one operation with `new int64_t[]()`, which default-initializes the array. On one example where use of APInt was heavy, this improved compile time by 1%.
1 parent 0628683 commit 427e202

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

llvm/lib/Support/APInt.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ using namespace llvm;
3434
/// A utility function for allocating memory, checking for allocation failures,
3535
/// and ensuring the contents are zeroed.
3636
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;
37+
return new uint64_t[numWords]();
4038
}
4139

4240
/// A utility function for allocating memory and checking for allocation
@@ -74,12 +72,15 @@ inline static unsigned getDigit(char cdigit, uint8_t radix) {
7472

7573

7674
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();
75+
if (isSigned && int64_t(val) < 0) {
76+
U.pVal = getMemory(getNumWords());
77+
U.pVal[0] = val;
78+
memset(&U.pVal[1], 0xFF, APINT_WORD_SIZE * (getNumWords() - 1));
79+
clearUnusedBits();
80+
} else {
81+
U.pVal = getClearedMemory(getNumWords());
82+
U.pVal[0] = val;
83+
}
8384
}
8485

8586
void APInt::initSlowCase(const APInt& that) {

0 commit comments

Comments
 (0)