xref: /freebsd-src/contrib/llvm-project/llvm/lib/Support/DivisionByConstantInfo.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1fcaf7f86SDimitry Andric //===----- DivisionByConstantInfo.cpp - division by constant -*- C++ -*----===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric ///
9349cc55cSDimitry Andric /// This file implements support for optimizing divisions by a constant
10349cc55cSDimitry Andric ///
11349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
12349cc55cSDimitry Andric 
13349cc55cSDimitry Andric #include "llvm/Support/DivisionByConstantInfo.h"
14349cc55cSDimitry Andric 
15349cc55cSDimitry Andric using namespace llvm;
16349cc55cSDimitry Andric 
17349cc55cSDimitry Andric /// Calculate the magic numbers required to implement a signed integer division
18349cc55cSDimitry Andric /// by a constant as a sequence of multiplies, adds and shifts.  Requires that
19349cc55cSDimitry Andric /// the divisor not be 0, 1, or -1.  Taken from "Hacker's Delight", Henry S.
20349cc55cSDimitry Andric /// Warren, Jr., Chapter 10.
21349cc55cSDimitry Andric SignedDivisionByConstantInfo SignedDivisionByConstantInfo::get(const APInt &D) {
22bdd1243dSDimitry Andric   assert(!D.isZero() && "Precondition violation.");
23bdd1243dSDimitry Andric 
24bdd1243dSDimitry Andric   // We'd be endlessly stuck in the loop.
25bdd1243dSDimitry Andric   assert(D.getBitWidth() >= 3 && "Does not work at smaller bitwidths.");
26bdd1243dSDimitry Andric 
27bdd1243dSDimitry Andric   APInt Delta;
28349cc55cSDimitry Andric   APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
29349cc55cSDimitry Andric   struct SignedDivisionByConstantInfo Retval;
30349cc55cSDimitry Andric 
31bdd1243dSDimitry Andric   APInt AD = D.abs();
32bdd1243dSDimitry Andric   APInt T = SignedMin + (D.lshr(D.getBitWidth() - 1));
33bdd1243dSDimitry Andric   APInt ANC = T - 1 - T.urem(AD);   // absolute value of NC
34bdd1243dSDimitry Andric   unsigned P = D.getBitWidth() - 1; // initialize P
35bdd1243dSDimitry Andric   APInt Q1, R1, Q2, R2;
36bdd1243dSDimitry Andric   // initialize Q1 = 2P/abs(NC); R1 = rem(2P,abs(NC))
37bdd1243dSDimitry Andric   APInt::udivrem(SignedMin, ANC, Q1, R1);
38bdd1243dSDimitry Andric   // initialize Q2 = 2P/abs(D); R2 = rem(2P,abs(D))
39bdd1243dSDimitry Andric   APInt::udivrem(SignedMin, AD, Q2, R2);
40349cc55cSDimitry Andric   do {
41349cc55cSDimitry Andric     P = P + 1;
42bdd1243dSDimitry Andric     Q1 <<= 1;      // update Q1 = 2P/abs(NC)
43bdd1243dSDimitry Andric     R1 <<= 1;      // update R1 = rem(2P/abs(NC))
44349cc55cSDimitry Andric     if (R1.uge(ANC)) { // must be unsigned comparison
45bdd1243dSDimitry Andric       ++Q1;
46bdd1243dSDimitry Andric       R1 -= ANC;
47349cc55cSDimitry Andric     }
48bdd1243dSDimitry Andric     Q2 <<= 1;     // update Q2 = 2P/abs(D)
49bdd1243dSDimitry Andric     R2 <<= 1;     // update R2 = rem(2P/abs(D))
50349cc55cSDimitry Andric     if (R2.uge(AD)) { // must be unsigned comparison
51bdd1243dSDimitry Andric       ++Q2;
52bdd1243dSDimitry Andric       R2 -= AD;
53349cc55cSDimitry Andric     }
54bdd1243dSDimitry Andric     // Delta = AD - R2
55bdd1243dSDimitry Andric     Delta = AD;
56bdd1243dSDimitry Andric     Delta -= R2;
57bdd1243dSDimitry Andric   } while (Q1.ult(Delta) || (Q1 == Delta && R1.isZero()));
58349cc55cSDimitry Andric 
59bdd1243dSDimitry Andric   Retval.Magic = std::move(Q2);
60bdd1243dSDimitry Andric   ++Retval.Magic;
61349cc55cSDimitry Andric   if (D.isNegative())
62bdd1243dSDimitry Andric     Retval.Magic.negate();                  // resulting magic number
63349cc55cSDimitry Andric   Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift
64349cc55cSDimitry Andric   return Retval;
65349cc55cSDimitry Andric }
66349cc55cSDimitry Andric 
67349cc55cSDimitry Andric /// Calculate the magic numbers required to implement an unsigned integer
68349cc55cSDimitry Andric /// division by a constant as a sequence of multiplies, adds and shifts.
69349cc55cSDimitry Andric /// Requires that the divisor not be 0.  Taken from "Hacker's Delight", Henry
70349cc55cSDimitry Andric /// S. Warren, Jr., chapter 10.
71349cc55cSDimitry Andric /// LeadingZeros can be used to simplify the calculation if the upper bits
72349cc55cSDimitry Andric /// of the divided value are known zero.
73fcaf7f86SDimitry Andric UnsignedDivisionByConstantInfo
74bdd1243dSDimitry Andric UnsignedDivisionByConstantInfo::get(const APInt &D, unsigned LeadingZeros,
75bdd1243dSDimitry Andric                                     bool AllowEvenDivisorOptimization) {
76bdd1243dSDimitry Andric   assert(!D.isZero() && !D.isOne() && "Precondition violation.");
77bdd1243dSDimitry Andric   assert(D.getBitWidth() > 1 && "Does not work at smaller bitwidths.");
78bdd1243dSDimitry Andric 
79bdd1243dSDimitry Andric   APInt Delta;
80fcaf7f86SDimitry Andric   struct UnsignedDivisionByConstantInfo Retval;
8104eeddc0SDimitry Andric   Retval.IsAdd = false; // initialize "add" indicator
82*0fca6ea1SDimitry Andric   APInt AllOnes =
83*0fca6ea1SDimitry Andric       APInt::getLowBitsSet(D.getBitWidth(), D.getBitWidth() - LeadingZeros);
84349cc55cSDimitry Andric   APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
85349cc55cSDimitry Andric   APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth());
86349cc55cSDimitry Andric 
87bdd1243dSDimitry Andric   // Calculate NC, the largest dividend such that NC.urem(D) == D-1.
88bdd1243dSDimitry Andric   APInt NC = AllOnes - (AllOnes + 1 - D).urem(D);
89bdd1243dSDimitry Andric   assert(NC.urem(D) == D - 1 && "Unexpected NC value");
90bdd1243dSDimitry Andric   unsigned P = D.getBitWidth() - 1; // initialize P
91bdd1243dSDimitry Andric   APInt Q1, R1, Q2, R2;
92bdd1243dSDimitry Andric   // initialize Q1 = 2P/NC; R1 = rem(2P,NC)
93bdd1243dSDimitry Andric   APInt::udivrem(SignedMin, NC, Q1, R1);
94bdd1243dSDimitry Andric   // initialize Q2 = (2P-1)/D; R2 = rem((2P-1),D)
95bdd1243dSDimitry Andric   APInt::udivrem(SignedMax, D, Q2, R2);
96349cc55cSDimitry Andric   do {
97349cc55cSDimitry Andric     P = P + 1;
98349cc55cSDimitry Andric     if (R1.uge(NC - R1)) {
99bdd1243dSDimitry Andric       // update Q1
100bdd1243dSDimitry Andric       Q1 <<= 1;
101bdd1243dSDimitry Andric       ++Q1;
102bdd1243dSDimitry Andric       // update R1
103bdd1243dSDimitry Andric       R1 <<= 1;
104bdd1243dSDimitry Andric       R1 -= NC;
105349cc55cSDimitry Andric     } else {
106bdd1243dSDimitry Andric       Q1 <<= 1; // update Q1
107bdd1243dSDimitry Andric       R1 <<= 1; // update R1
108349cc55cSDimitry Andric     }
109349cc55cSDimitry Andric     if ((R2 + 1).uge(D - R2)) {
110349cc55cSDimitry Andric       if (Q2.uge(SignedMax))
11104eeddc0SDimitry Andric         Retval.IsAdd = true;
112bdd1243dSDimitry Andric       // update Q2
113bdd1243dSDimitry Andric       Q2 <<= 1;
114bdd1243dSDimitry Andric       ++Q2;
115bdd1243dSDimitry Andric       // update R2
116bdd1243dSDimitry Andric       R2 <<= 1;
117bdd1243dSDimitry Andric       ++R2;
118bdd1243dSDimitry Andric       R2 -= D;
119349cc55cSDimitry Andric     } else {
120349cc55cSDimitry Andric       if (Q2.uge(SignedMin))
12104eeddc0SDimitry Andric         Retval.IsAdd = true;
122bdd1243dSDimitry Andric       // update Q2
123bdd1243dSDimitry Andric       Q2 <<= 1;
124bdd1243dSDimitry Andric       // update R2
125bdd1243dSDimitry Andric       R2 <<= 1;
126bdd1243dSDimitry Andric       ++R2;
127349cc55cSDimitry Andric     }
128bdd1243dSDimitry Andric     // Delta = D - 1 - R2
129bdd1243dSDimitry Andric     Delta = D;
130bdd1243dSDimitry Andric     --Delta;
131bdd1243dSDimitry Andric     Delta -= R2;
132349cc55cSDimitry Andric   } while (P < D.getBitWidth() * 2 &&
133bdd1243dSDimitry Andric            (Q1.ult(Delta) || (Q1 == Delta && R1.isZero())));
134bdd1243dSDimitry Andric 
135bdd1243dSDimitry Andric   if (Retval.IsAdd && !D[0] && AllowEvenDivisorOptimization) {
13606c3fb27SDimitry Andric     unsigned PreShift = D.countr_zero();
137bdd1243dSDimitry Andric     APInt ShiftedD = D.lshr(PreShift);
138bdd1243dSDimitry Andric     Retval =
139bdd1243dSDimitry Andric         UnsignedDivisionByConstantInfo::get(ShiftedD, LeadingZeros + PreShift);
140bdd1243dSDimitry Andric     assert(Retval.IsAdd == 0 && Retval.PreShift == 0);
141bdd1243dSDimitry Andric     Retval.PreShift = PreShift;
142bdd1243dSDimitry Andric     return Retval;
143bdd1243dSDimitry Andric   }
144bdd1243dSDimitry Andric 
145bdd1243dSDimitry Andric   Retval.Magic = std::move(Q2);             // resulting magic number
146bdd1243dSDimitry Andric   ++Retval.Magic;
147bdd1243dSDimitry Andric   Retval.PostShift = P - D.getBitWidth(); // resulting shift
148bdd1243dSDimitry Andric   // Reduce shift amount for IsAdd.
149bdd1243dSDimitry Andric   if (Retval.IsAdd) {
150bdd1243dSDimitry Andric     assert(Retval.PostShift > 0 && "Unexpected shift");
151bdd1243dSDimitry Andric     Retval.PostShift -= 1;
152bdd1243dSDimitry Andric   }
153bdd1243dSDimitry Andric   Retval.PreShift = 0;
154349cc55cSDimitry Andric   return Retval;
155349cc55cSDimitry Andric }
156