xref: /freebsd-src/contrib/llvm-project/llvm/lib/Support/DivisionByConstantInfo.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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) {
22*bdd1243dSDimitry Andric   assert(!D.isZero() && "Precondition violation.");
23*bdd1243dSDimitry Andric 
24*bdd1243dSDimitry Andric   // We'd be endlessly stuck in the loop.
25*bdd1243dSDimitry Andric   assert(D.getBitWidth() >= 3 && "Does not work at smaller bitwidths.");
26*bdd1243dSDimitry Andric 
27*bdd1243dSDimitry Andric   APInt Delta;
28349cc55cSDimitry Andric   APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
29349cc55cSDimitry Andric   struct SignedDivisionByConstantInfo Retval;
30349cc55cSDimitry Andric 
31*bdd1243dSDimitry Andric   APInt AD = D.abs();
32*bdd1243dSDimitry Andric   APInt T = SignedMin + (D.lshr(D.getBitWidth() - 1));
33*bdd1243dSDimitry Andric   APInt ANC = T - 1 - T.urem(AD);   // absolute value of NC
34*bdd1243dSDimitry Andric   unsigned P = D.getBitWidth() - 1; // initialize P
35*bdd1243dSDimitry Andric   APInt Q1, R1, Q2, R2;
36*bdd1243dSDimitry Andric   // initialize Q1 = 2P/abs(NC); R1 = rem(2P,abs(NC))
37*bdd1243dSDimitry Andric   APInt::udivrem(SignedMin, ANC, Q1, R1);
38*bdd1243dSDimitry Andric   // initialize Q2 = 2P/abs(D); R2 = rem(2P,abs(D))
39*bdd1243dSDimitry Andric   APInt::udivrem(SignedMin, AD, Q2, R2);
40349cc55cSDimitry Andric   do {
41349cc55cSDimitry Andric     P = P + 1;
42*bdd1243dSDimitry Andric     Q1 <<= 1;      // update Q1 = 2P/abs(NC)
43*bdd1243dSDimitry Andric     R1 <<= 1;      // update R1 = rem(2P/abs(NC))
44349cc55cSDimitry Andric     if (R1.uge(ANC)) { // must be unsigned comparison
45*bdd1243dSDimitry Andric       ++Q1;
46*bdd1243dSDimitry Andric       R1 -= ANC;
47349cc55cSDimitry Andric     }
48*bdd1243dSDimitry Andric     Q2 <<= 1;     // update Q2 = 2P/abs(D)
49*bdd1243dSDimitry Andric     R2 <<= 1;     // update R2 = rem(2P/abs(D))
50349cc55cSDimitry Andric     if (R2.uge(AD)) { // must be unsigned comparison
51*bdd1243dSDimitry Andric       ++Q2;
52*bdd1243dSDimitry Andric       R2 -= AD;
53349cc55cSDimitry Andric     }
54*bdd1243dSDimitry Andric     // Delta = AD - R2
55*bdd1243dSDimitry Andric     Delta = AD;
56*bdd1243dSDimitry Andric     Delta -= R2;
57*bdd1243dSDimitry Andric   } while (Q1.ult(Delta) || (Q1 == Delta && R1.isZero()));
58349cc55cSDimitry Andric 
59*bdd1243dSDimitry Andric   Retval.Magic = std::move(Q2);
60*bdd1243dSDimitry Andric   ++Retval.Magic;
61349cc55cSDimitry Andric   if (D.isNegative())
62*bdd1243dSDimitry 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
74*bdd1243dSDimitry Andric UnsignedDivisionByConstantInfo::get(const APInt &D, unsigned LeadingZeros,
75*bdd1243dSDimitry Andric                                     bool AllowEvenDivisorOptimization) {
76*bdd1243dSDimitry Andric   assert(!D.isZero() && !D.isOne() && "Precondition violation.");
77*bdd1243dSDimitry Andric   assert(D.getBitWidth() > 1 && "Does not work at smaller bitwidths.");
78*bdd1243dSDimitry Andric 
79*bdd1243dSDimitry Andric   APInt Delta;
80fcaf7f86SDimitry Andric   struct UnsignedDivisionByConstantInfo Retval;
8104eeddc0SDimitry Andric   Retval.IsAdd = false; // initialize "add" indicator
82349cc55cSDimitry Andric   APInt AllOnes = APInt::getAllOnes(D.getBitWidth()).lshr(LeadingZeros);
83349cc55cSDimitry Andric   APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
84349cc55cSDimitry Andric   APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth());
85349cc55cSDimitry Andric 
86*bdd1243dSDimitry Andric   // Calculate NC, the largest dividend such that NC.urem(D) == D-1.
87*bdd1243dSDimitry Andric   APInt NC = AllOnes - (AllOnes + 1 - D).urem(D);
88*bdd1243dSDimitry Andric   assert(NC.urem(D) == D - 1 && "Unexpected NC value");
89*bdd1243dSDimitry Andric   unsigned P = D.getBitWidth() - 1; // initialize P
90*bdd1243dSDimitry Andric   APInt Q1, R1, Q2, R2;
91*bdd1243dSDimitry Andric   // initialize Q1 = 2P/NC; R1 = rem(2P,NC)
92*bdd1243dSDimitry Andric   APInt::udivrem(SignedMin, NC, Q1, R1);
93*bdd1243dSDimitry Andric   // initialize Q2 = (2P-1)/D; R2 = rem((2P-1),D)
94*bdd1243dSDimitry Andric   APInt::udivrem(SignedMax, D, Q2, R2);
95349cc55cSDimitry Andric   do {
96349cc55cSDimitry Andric     P = P + 1;
97349cc55cSDimitry Andric     if (R1.uge(NC - R1)) {
98*bdd1243dSDimitry Andric       // update Q1
99*bdd1243dSDimitry Andric       Q1 <<= 1;
100*bdd1243dSDimitry Andric       ++Q1;
101*bdd1243dSDimitry Andric       // update R1
102*bdd1243dSDimitry Andric       R1 <<= 1;
103*bdd1243dSDimitry Andric       R1 -= NC;
104349cc55cSDimitry Andric     } else {
105*bdd1243dSDimitry Andric       Q1 <<= 1; // update Q1
106*bdd1243dSDimitry Andric       R1 <<= 1; // update R1
107349cc55cSDimitry Andric     }
108349cc55cSDimitry Andric     if ((R2 + 1).uge(D - R2)) {
109349cc55cSDimitry Andric       if (Q2.uge(SignedMax))
11004eeddc0SDimitry Andric         Retval.IsAdd = true;
111*bdd1243dSDimitry Andric       // update Q2
112*bdd1243dSDimitry Andric       Q2 <<= 1;
113*bdd1243dSDimitry Andric       ++Q2;
114*bdd1243dSDimitry Andric       // update R2
115*bdd1243dSDimitry Andric       R2 <<= 1;
116*bdd1243dSDimitry Andric       ++R2;
117*bdd1243dSDimitry Andric       R2 -= D;
118349cc55cSDimitry Andric     } else {
119349cc55cSDimitry Andric       if (Q2.uge(SignedMin))
12004eeddc0SDimitry Andric         Retval.IsAdd = true;
121*bdd1243dSDimitry Andric       // update Q2
122*bdd1243dSDimitry Andric       Q2 <<= 1;
123*bdd1243dSDimitry Andric       // update R2
124*bdd1243dSDimitry Andric       R2 <<= 1;
125*bdd1243dSDimitry Andric       ++R2;
126349cc55cSDimitry Andric     }
127*bdd1243dSDimitry Andric     // Delta = D - 1 - R2
128*bdd1243dSDimitry Andric     Delta = D;
129*bdd1243dSDimitry Andric     --Delta;
130*bdd1243dSDimitry Andric     Delta -= R2;
131349cc55cSDimitry Andric   } while (P < D.getBitWidth() * 2 &&
132*bdd1243dSDimitry Andric            (Q1.ult(Delta) || (Q1 == Delta && R1.isZero())));
133*bdd1243dSDimitry Andric 
134*bdd1243dSDimitry Andric   if (Retval.IsAdd && !D[0] && AllowEvenDivisorOptimization) {
135*bdd1243dSDimitry Andric     unsigned PreShift = D.countTrailingZeros();
136*bdd1243dSDimitry Andric     APInt ShiftedD = D.lshr(PreShift);
137*bdd1243dSDimitry Andric     Retval =
138*bdd1243dSDimitry Andric         UnsignedDivisionByConstantInfo::get(ShiftedD, LeadingZeros + PreShift);
139*bdd1243dSDimitry Andric     assert(Retval.IsAdd == 0 && Retval.PreShift == 0);
140*bdd1243dSDimitry Andric     Retval.PreShift = PreShift;
141*bdd1243dSDimitry Andric     return Retval;
142*bdd1243dSDimitry Andric   }
143*bdd1243dSDimitry Andric 
144*bdd1243dSDimitry Andric   Retval.Magic = std::move(Q2);             // resulting magic number
145*bdd1243dSDimitry Andric   ++Retval.Magic;
146*bdd1243dSDimitry Andric   Retval.PostShift = P - D.getBitWidth(); // resulting shift
147*bdd1243dSDimitry Andric   // Reduce shift amount for IsAdd.
148*bdd1243dSDimitry Andric   if (Retval.IsAdd) {
149*bdd1243dSDimitry Andric     assert(Retval.PostShift > 0 && "Unexpected shift");
150*bdd1243dSDimitry Andric     Retval.PostShift -= 1;
151*bdd1243dSDimitry Andric   }
152*bdd1243dSDimitry Andric   Retval.PreShift = 0;
153349cc55cSDimitry Andric   return Retval;
154349cc55cSDimitry Andric }
155