[llvm] [APInt] remove getClearedMemory and improve slow initialization (PR #106945)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 2 01:14:56 PDT 2024


================
@@ -74,11 +66,14 @@ inline static unsigned getDigit(char cdigit, uint8_t radix) {
 
 
 void APInt::initSlowCase(uint64_t val, bool isSigned) {
-  U.pVal = getClearedMemory(getNumWords());
-  U.pVal[0] = val;
-  if (isSigned && int64_t(val) < 0)
-    for (unsigned i = 1; i < getNumWords(); ++i)
-      U.pVal[i] = WORDTYPE_MAX;
+  if (isSigned && int64_t(val) < 0) {
+    U.pVal = new WordType[getNumWords()];
+    U.pVal[0] = val;
+    memset(&U.pVal[1], 0xFF, sizeof(WordType) * (getNumWords() - 1));
+  } else {
+    U.pVal = new WordType[getNumWords()]();
+    U.pVal[0] = val;
----------------
nikic wrote:

Oh wow, so one of these lines is `new WordType[getNumWords()]` and the other is `new WordType[getNumWords()]()`. I completely missed that.

I agree that the code is correct, but it looks like an undesirable obfuscation to me. I'll leave it to other people to decide.

https://github.com/llvm/llvm-project/pull/106945


More information about the llvm-commits mailing list