[llvm] [APInt] Slightly simplify APInt::ashrSlowCase. NFC (PR #111220)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 4 16:33:41 PDT 2024


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/111220

Use an arithmetic shift for the last word copy when BitShift!=0. This avoids an explicit sign extend after the shift.

>From ecc091f771a338fd2d92036f8883c9f49ac3f608 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Fri, 4 Oct 2024 16:23:32 -0700
Subject: [PATCH] [APInt] Slightly simplify APInt::ashrSlowCase. NFC

Use an arithmetic shift for the last word copy when BitShift!=0.
This avoids an explicit sign extend after the shift.
---
 llvm/lib/Support/APInt.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 2348a4c9b795e1..23e365f16d8fea 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -1055,11 +1055,10 @@ void APInt::ashrSlowCase(unsigned ShiftAmt) {
         U.pVal[i] = (U.pVal[i + WordShift] >> BitShift) |
                     (U.pVal[i + WordShift + 1] << (APINT_BITS_PER_WORD - BitShift));
 
-      // Handle the last word which has no high bits to copy.
-      U.pVal[WordsToMove - 1] = U.pVal[WordShift + WordsToMove - 1] >> BitShift;
-      // Sign extend one more time.
+      // Handle the last word which has no high bits to copy. Use an arithmetic
+      // shift to preserve the sign bit.
       U.pVal[WordsToMove - 1] =
-          SignExtend64(U.pVal[WordsToMove - 1], APINT_BITS_PER_WORD - BitShift);
+          (int64_t)U.pVal[WordShift + WordsToMove - 1] >> BitShift;
     }
   }
 



More information about the llvm-commits mailing list