1*349cc55cSDimitry Andric //===----- DivisonByConstantInfo.cpp - division by constant -*- C++ -*-----===// 2*349cc55cSDimitry Andric // 3*349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*349cc55cSDimitry Andric // 7*349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 8*349cc55cSDimitry Andric /// 9*349cc55cSDimitry Andric /// This file implements support for optimizing divisions by a constant 10*349cc55cSDimitry Andric /// 11*349cc55cSDimitry Andric //===----------------------------------------------------------------------===// 12*349cc55cSDimitry Andric 13*349cc55cSDimitry Andric #include "llvm/Support/DivisionByConstantInfo.h" 14*349cc55cSDimitry Andric 15*349cc55cSDimitry Andric using namespace llvm; 16*349cc55cSDimitry Andric 17*349cc55cSDimitry Andric /// Calculate the magic numbers required to implement a signed integer division 18*349cc55cSDimitry Andric /// by a constant as a sequence of multiplies, adds and shifts. Requires that 19*349cc55cSDimitry Andric /// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S. 20*349cc55cSDimitry Andric /// Warren, Jr., Chapter 10. 21*349cc55cSDimitry Andric SignedDivisionByConstantInfo SignedDivisionByConstantInfo::get(const APInt &D) { 22*349cc55cSDimitry Andric unsigned P; 23*349cc55cSDimitry Andric APInt AD, ANC, Delta, Q1, R1, Q2, R2, T; 24*349cc55cSDimitry Andric APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth()); 25*349cc55cSDimitry Andric struct SignedDivisionByConstantInfo Retval; 26*349cc55cSDimitry Andric 27*349cc55cSDimitry Andric AD = D.abs(); 28*349cc55cSDimitry Andric T = SignedMin + (D.lshr(D.getBitWidth() - 1)); 29*349cc55cSDimitry Andric ANC = T - 1 - T.urem(AD); // absolute value of NC 30*349cc55cSDimitry Andric P = D.getBitWidth() - 1; // initialize P 31*349cc55cSDimitry Andric Q1 = SignedMin.udiv(ANC); // initialize Q1 = 2P/abs(NC) 32*349cc55cSDimitry Andric R1 = SignedMin - Q1 * ANC; // initialize R1 = rem(2P,abs(NC)) 33*349cc55cSDimitry Andric Q2 = SignedMin.udiv(AD); // initialize Q2 = 2P/abs(D) 34*349cc55cSDimitry Andric R2 = SignedMin - Q2 * AD; // initialize R2 = rem(2P,abs(D)) 35*349cc55cSDimitry Andric do { 36*349cc55cSDimitry Andric P = P + 1; 37*349cc55cSDimitry Andric Q1 = Q1 << 1; // update Q1 = 2P/abs(NC) 38*349cc55cSDimitry Andric R1 = R1 << 1; // update R1 = rem(2P/abs(NC)) 39*349cc55cSDimitry Andric if (R1.uge(ANC)) { // must be unsigned comparison 40*349cc55cSDimitry Andric Q1 = Q1 + 1; 41*349cc55cSDimitry Andric R1 = R1 - ANC; 42*349cc55cSDimitry Andric } 43*349cc55cSDimitry Andric Q2 = Q2 << 1; // update Q2 = 2P/abs(D) 44*349cc55cSDimitry Andric R2 = R2 << 1; // update R2 = rem(2P/abs(D)) 45*349cc55cSDimitry Andric if (R2.uge(AD)) { // must be unsigned comparison 46*349cc55cSDimitry Andric Q2 = Q2 + 1; 47*349cc55cSDimitry Andric R2 = R2 - AD; 48*349cc55cSDimitry Andric } 49*349cc55cSDimitry Andric Delta = AD - R2; 50*349cc55cSDimitry Andric } while (Q1.ult(Delta) || (Q1 == Delta && R1 == 0)); 51*349cc55cSDimitry Andric 52*349cc55cSDimitry Andric Retval.Magic = Q2 + 1; 53*349cc55cSDimitry Andric if (D.isNegative()) 54*349cc55cSDimitry Andric Retval.Magic = -Retval.Magic; // resulting magic number 55*349cc55cSDimitry Andric Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift 56*349cc55cSDimitry Andric return Retval; 57*349cc55cSDimitry Andric } 58*349cc55cSDimitry Andric 59*349cc55cSDimitry Andric /// Calculate the magic numbers required to implement an unsigned integer 60*349cc55cSDimitry Andric /// division by a constant as a sequence of multiplies, adds and shifts. 61*349cc55cSDimitry Andric /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry 62*349cc55cSDimitry Andric /// S. Warren, Jr., chapter 10. 63*349cc55cSDimitry Andric /// LeadingZeros can be used to simplify the calculation if the upper bits 64*349cc55cSDimitry Andric /// of the divided value are known zero. 65*349cc55cSDimitry Andric UnsignedDivisonByConstantInfo 66*349cc55cSDimitry Andric UnsignedDivisonByConstantInfo::get(const APInt &D, unsigned LeadingZeros) { 67*349cc55cSDimitry Andric unsigned P; 68*349cc55cSDimitry Andric APInt NC, Delta, Q1, R1, Q2, R2; 69*349cc55cSDimitry Andric struct UnsignedDivisonByConstantInfo Retval; 70*349cc55cSDimitry Andric Retval.IsAdd = 0; // initialize "add" indicator 71*349cc55cSDimitry Andric APInt AllOnes = APInt::getAllOnes(D.getBitWidth()).lshr(LeadingZeros); 72*349cc55cSDimitry Andric APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth()); 73*349cc55cSDimitry Andric APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth()); 74*349cc55cSDimitry Andric 75*349cc55cSDimitry Andric NC = AllOnes - (AllOnes - D).urem(D); 76*349cc55cSDimitry Andric P = D.getBitWidth() - 1; // initialize P 77*349cc55cSDimitry Andric Q1 = SignedMin.udiv(NC); // initialize Q1 = 2P/NC 78*349cc55cSDimitry Andric R1 = SignedMin - Q1 * NC; // initialize R1 = rem(2P,NC) 79*349cc55cSDimitry Andric Q2 = SignedMax.udiv(D); // initialize Q2 = (2P-1)/D 80*349cc55cSDimitry Andric R2 = SignedMax - Q2 * D; // initialize R2 = rem((2P-1),D) 81*349cc55cSDimitry Andric do { 82*349cc55cSDimitry Andric P = P + 1; 83*349cc55cSDimitry Andric if (R1.uge(NC - R1)) { 84*349cc55cSDimitry Andric Q1 = Q1 + Q1 + 1; // update Q1 85*349cc55cSDimitry Andric R1 = R1 + R1 - NC; // update R1 86*349cc55cSDimitry Andric } else { 87*349cc55cSDimitry Andric Q1 = Q1 + Q1; // update Q1 88*349cc55cSDimitry Andric R1 = R1 + R1; // update R1 89*349cc55cSDimitry Andric } 90*349cc55cSDimitry Andric if ((R2 + 1).uge(D - R2)) { 91*349cc55cSDimitry Andric if (Q2.uge(SignedMax)) 92*349cc55cSDimitry Andric Retval.IsAdd = 1; 93*349cc55cSDimitry Andric Q2 = Q2 + Q2 + 1; // update Q2 94*349cc55cSDimitry Andric R2 = R2 + R2 + 1 - D; // update R2 95*349cc55cSDimitry Andric } else { 96*349cc55cSDimitry Andric if (Q2.uge(SignedMin)) 97*349cc55cSDimitry Andric Retval.IsAdd = 1; 98*349cc55cSDimitry Andric Q2 = Q2 + Q2; // update Q2 99*349cc55cSDimitry Andric R2 = R2 + R2 + 1; // update R2 100*349cc55cSDimitry Andric } 101*349cc55cSDimitry Andric Delta = D - 1 - R2; 102*349cc55cSDimitry Andric } while (P < D.getBitWidth() * 2 && 103*349cc55cSDimitry Andric (Q1.ult(Delta) || (Q1 == Delta && R1 == 0))); 104*349cc55cSDimitry Andric Retval.Magic = Q2 + 1; // resulting magic number 105*349cc55cSDimitry Andric Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift 106*349cc55cSDimitry Andric return Retval; 107*349cc55cSDimitry Andric } 108