1*a55ff6aaSCraig Topper //===----- DivisionByConstantInfo.cpp - division by constant -*- C++ -*----===//
23077bc90SChristopher Tetreault //
33077bc90SChristopher Tetreault // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43077bc90SChristopher Tetreault // See https://llvm.org/LICENSE.txt for license information.
53077bc90SChristopher Tetreault // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63077bc90SChristopher Tetreault //
73077bc90SChristopher Tetreault //===----------------------------------------------------------------------===//
83077bc90SChristopher Tetreault ///
93077bc90SChristopher Tetreault /// This file implements support for optimizing divisions by a constant
103077bc90SChristopher Tetreault ///
113077bc90SChristopher Tetreault //===----------------------------------------------------------------------===//
123077bc90SChristopher Tetreault
133077bc90SChristopher Tetreault #include "llvm/Support/DivisionByConstantInfo.h"
143077bc90SChristopher Tetreault
153077bc90SChristopher Tetreault using namespace llvm;
163077bc90SChristopher Tetreault
173077bc90SChristopher Tetreault /// Calculate the magic numbers required to implement a signed integer division
183077bc90SChristopher Tetreault /// by a constant as a sequence of multiplies, adds and shifts. Requires that
193077bc90SChristopher Tetreault /// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S.
203077bc90SChristopher Tetreault /// Warren, Jr., Chapter 10.
get(const APInt & D)213077bc90SChristopher Tetreault SignedDivisionByConstantInfo SignedDivisionByConstantInfo::get(const APInt &D) {
223077bc90SChristopher Tetreault unsigned P;
233077bc90SChristopher Tetreault APInt AD, ANC, Delta, Q1, R1, Q2, R2, T;
243077bc90SChristopher Tetreault APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
253077bc90SChristopher Tetreault struct SignedDivisionByConstantInfo Retval;
263077bc90SChristopher Tetreault
273077bc90SChristopher Tetreault AD = D.abs();
283077bc90SChristopher Tetreault T = SignedMin + (D.lshr(D.getBitWidth() - 1));
293077bc90SChristopher Tetreault ANC = T - 1 - T.urem(AD); // absolute value of NC
303077bc90SChristopher Tetreault P = D.getBitWidth() - 1; // initialize P
313077bc90SChristopher Tetreault Q1 = SignedMin.udiv(ANC); // initialize Q1 = 2P/abs(NC)
323077bc90SChristopher Tetreault R1 = SignedMin - Q1 * ANC; // initialize R1 = rem(2P,abs(NC))
333077bc90SChristopher Tetreault Q2 = SignedMin.udiv(AD); // initialize Q2 = 2P/abs(D)
343077bc90SChristopher Tetreault R2 = SignedMin - Q2 * AD; // initialize R2 = rem(2P,abs(D))
353077bc90SChristopher Tetreault do {
363077bc90SChristopher Tetreault P = P + 1;
373077bc90SChristopher Tetreault Q1 = Q1 << 1; // update Q1 = 2P/abs(NC)
383077bc90SChristopher Tetreault R1 = R1 << 1; // update R1 = rem(2P/abs(NC))
393077bc90SChristopher Tetreault if (R1.uge(ANC)) { // must be unsigned comparison
403077bc90SChristopher Tetreault Q1 = Q1 + 1;
413077bc90SChristopher Tetreault R1 = R1 - ANC;
423077bc90SChristopher Tetreault }
433077bc90SChristopher Tetreault Q2 = Q2 << 1; // update Q2 = 2P/abs(D)
443077bc90SChristopher Tetreault R2 = R2 << 1; // update R2 = rem(2P/abs(D))
453077bc90SChristopher Tetreault if (R2.uge(AD)) { // must be unsigned comparison
463077bc90SChristopher Tetreault Q2 = Q2 + 1;
473077bc90SChristopher Tetreault R2 = R2 - AD;
483077bc90SChristopher Tetreault }
493077bc90SChristopher Tetreault Delta = AD - R2;
503077bc90SChristopher Tetreault } while (Q1.ult(Delta) || (Q1 == Delta && R1 == 0));
513077bc90SChristopher Tetreault
523077bc90SChristopher Tetreault Retval.Magic = Q2 + 1;
533077bc90SChristopher Tetreault if (D.isNegative())
543077bc90SChristopher Tetreault Retval.Magic = -Retval.Magic; // resulting magic number
553077bc90SChristopher Tetreault Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift
563077bc90SChristopher Tetreault return Retval;
573077bc90SChristopher Tetreault }
583077bc90SChristopher Tetreault
593077bc90SChristopher Tetreault /// Calculate the magic numbers required to implement an unsigned integer
603077bc90SChristopher Tetreault /// division by a constant as a sequence of multiplies, adds and shifts.
613077bc90SChristopher Tetreault /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry
623077bc90SChristopher Tetreault /// S. Warren, Jr., chapter 10.
633077bc90SChristopher Tetreault /// LeadingZeros can be used to simplify the calculation if the upper bits
643077bc90SChristopher Tetreault /// of the divided value are known zero.
65*a55ff6aaSCraig Topper UnsignedDivisionByConstantInfo
get(const APInt & D,unsigned LeadingZeros)66*a55ff6aaSCraig Topper UnsignedDivisionByConstantInfo::get(const APInt &D, unsigned LeadingZeros) {
673077bc90SChristopher Tetreault unsigned P;
683077bc90SChristopher Tetreault APInt NC, Delta, Q1, R1, Q2, R2;
69*a55ff6aaSCraig Topper struct UnsignedDivisionByConstantInfo Retval;
702aed0813SKazu Hirata Retval.IsAdd = false; // initialize "add" indicator
713077bc90SChristopher Tetreault APInt AllOnes = APInt::getAllOnes(D.getBitWidth()).lshr(LeadingZeros);
723077bc90SChristopher Tetreault APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
733077bc90SChristopher Tetreault APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth());
743077bc90SChristopher Tetreault
753077bc90SChristopher Tetreault NC = AllOnes - (AllOnes - D).urem(D);
763077bc90SChristopher Tetreault P = D.getBitWidth() - 1; // initialize P
773077bc90SChristopher Tetreault Q1 = SignedMin.udiv(NC); // initialize Q1 = 2P/NC
783077bc90SChristopher Tetreault R1 = SignedMin - Q1 * NC; // initialize R1 = rem(2P,NC)
793077bc90SChristopher Tetreault Q2 = SignedMax.udiv(D); // initialize Q2 = (2P-1)/D
803077bc90SChristopher Tetreault R2 = SignedMax - Q2 * D; // initialize R2 = rem((2P-1),D)
813077bc90SChristopher Tetreault do {
823077bc90SChristopher Tetreault P = P + 1;
833077bc90SChristopher Tetreault if (R1.uge(NC - R1)) {
843077bc90SChristopher Tetreault Q1 = Q1 + Q1 + 1; // update Q1
853077bc90SChristopher Tetreault R1 = R1 + R1 - NC; // update R1
863077bc90SChristopher Tetreault } else {
873077bc90SChristopher Tetreault Q1 = Q1 + Q1; // update Q1
883077bc90SChristopher Tetreault R1 = R1 + R1; // update R1
893077bc90SChristopher Tetreault }
903077bc90SChristopher Tetreault if ((R2 + 1).uge(D - R2)) {
913077bc90SChristopher Tetreault if (Q2.uge(SignedMax))
922aed0813SKazu Hirata Retval.IsAdd = true;
933077bc90SChristopher Tetreault Q2 = Q2 + Q2 + 1; // update Q2
943077bc90SChristopher Tetreault R2 = R2 + R2 + 1 - D; // update R2
953077bc90SChristopher Tetreault } else {
963077bc90SChristopher Tetreault if (Q2.uge(SignedMin))
972aed0813SKazu Hirata Retval.IsAdd = true;
983077bc90SChristopher Tetreault Q2 = Q2 + Q2; // update Q2
993077bc90SChristopher Tetreault R2 = R2 + R2 + 1; // update R2
1003077bc90SChristopher Tetreault }
1013077bc90SChristopher Tetreault Delta = D - 1 - R2;
1023077bc90SChristopher Tetreault } while (P < D.getBitWidth() * 2 &&
1033077bc90SChristopher Tetreault (Q1.ult(Delta) || (Q1 == Delta && R1 == 0)));
1043077bc90SChristopher Tetreault Retval.Magic = Q2 + 1; // resulting magic number
1053077bc90SChristopher Tetreault Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift
1063077bc90SChristopher Tetreault return Retval;
1073077bc90SChristopher Tetreault }
108