[llvm] 012afbb - [NFC][SupportTests] Adjust `UnsignedDivideUsingMagic()` for readability

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 25 05:48:26 PST 2022


Author: Roman Lebedev
Date: 2022-12-25T16:47:55+03:00
New Revision: 012afbbab48194b07ac46cf95e5684d2df57db47

URL: https://github.com/llvm/llvm-project/commit/012afbbab48194b07ac46cf95e5684d2df57db47
DIFF: https://github.com/llvm/llvm-project/commit/012afbbab48194b07ac46cf95e5684d2df57db47.diff

LOG: [NFC][SupportTests] Adjust `UnsignedDivideUsingMagic()` for readability

Added: 
    

Modified: 
    llvm/unittests/Support/DivisionByConstantTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/Support/DivisionByConstantTest.cpp b/llvm/unittests/Support/DivisionByConstantTest.cpp
index fa8492bd9a66..ab4c52c1e74f 100644
--- a/llvm/unittests/Support/DivisionByConstantTest.cpp
+++ b/llvm/unittests/Support/DivisionByConstantTest.cpp
@@ -102,9 +102,7 @@ APInt UnsignedDivideUsingMagic(APInt Numerator, APInt Divisor,
                                UnsignedDivisionByConstantInfo Magics) {
   unsigned Bits = Numerator.getBitWidth();
 
-  bool UseNPQ = false;
-  unsigned PreShift = 0, PostShift = 0;
-
+  unsigned PreShift = 0;
   if (AllowEvenDivisorOptimization) {
     // If the divisor is even, we can avoid using the expensive fixup by
     // shifting the divided value upfront.
@@ -117,6 +115,8 @@ APInt UnsignedDivideUsingMagic(APInt Numerator, APInt Divisor,
     }
   }
 
+  unsigned PostShift = 0;
+  bool UseNPQ = false;
   if (!Magics.IsAdd || Divisor.isOne()) {
     assert(Magics.ShiftAmount < Divisor.getBitWidth() &&
            "We shouldn't generate an undefined shift!");
@@ -124,11 +124,13 @@ APInt UnsignedDivideUsingMagic(APInt Numerator, APInt Divisor,
     UseNPQ = false;
   } else {
     PostShift = Magics.ShiftAmount - 1;
+    assert(PostShift < Divisor.getBitWidth() &&
+           "We shouldn't generate an undefined shift!");
     UseNPQ = true;
   }
 
   APInt NPQFactor =
-      UseNPQ ? APInt::getOneBitSet(Bits, Bits - 1) : APInt::getZero(Bits);
+      UseNPQ ? APInt::getSignedMinValue(Bits) : APInt::getZero(Bits);
 
   APInt Q = Numerator.lshr(PreShift);
 


        


More information about the llvm-commits mailing list