[llvm] 1490796 - [Support] Fix what I think is an off by 1 bug in UnsignedDivisionByConstantInfo.
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 29 09:42:59 PST 2022
Author: Craig Topper
Date: 2022-12-29T09:42:50-08:00
New Revision: 1490796dd28c80e987a0a9306104141c52022982
URL: https://github.com/llvm/llvm-project/commit/1490796dd28c80e987a0a9306104141c52022982
DIFF: https://github.com/llvm/llvm-project/commit/1490796dd28c80e987a0a9306104141c52022982.diff
LOG: [Support] Fix what I think is an off by 1 bug in UnsignedDivisionByConstantInfo.
The code in Hacker's Delight says
`nc = -1 - (-d)%d;`
But we have
`NC = AllOnes - (AllOnes-D)%D`
The Hacker's Delight code is written for the LeadingZeros==0 case.
`AllOnes - D` is not the same as `-d` from Hacker's Delight.
This patch changes the code to
`NC = AllOnes - (AllOnes+1-D)%D`
This will increment AllOnes to 0 in the LeadingZeros==0 case. This
will make it equivalent to -D. I believe this is also correct for
LeadingZeros>0.
At least for i8, i16, and i32 the only divisor that changes is
((1 << (BitWidth-1)) | 1). Or 127 for i8, 32769 for i16, and 2147483649
for i32. These are all large enough that the quotient is 0 or 1 so
InstCombine replaces them with an icmp and zext before SelectionDAG.
Reviewed By: lebedev.ri
Differential Revision: https://reviews.llvm.org/D140636
Added:
Modified:
llvm/lib/Support/DivisionByConstantInfo.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/DivisionByConstantInfo.cpp b/llvm/lib/Support/DivisionByConstantInfo.cpp
index c84224069cbc..b29076037111 100644
--- a/llvm/lib/Support/DivisionByConstantInfo.cpp
+++ b/llvm/lib/Support/DivisionByConstantInfo.cpp
@@ -82,7 +82,9 @@ UnsignedDivisionByConstantInfo::get(const APInt &D, unsigned LeadingZeros) {
APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth());
- APInt NC = AllOnes - (AllOnes - D).urem(D);
+ // Calculate NC, the largest dividend such that NC.urem(D) == D-1.
+ APInt NC = AllOnes - (AllOnes + 1 - D).urem(D);
+ assert(NC.urem(D) == D - 1 && "Unexpected NC value");
unsigned P = D.getBitWidth() - 1; // initialize P
APInt Q1, R1, Q2, R2;
// initialize Q1 = 2P/NC; R1 = rem(2P,NC)
More information about the llvm-commits
mailing list