1*fcaf7f86SDimitry 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) { 22349cc55cSDimitry Andric unsigned P; 23349cc55cSDimitry Andric APInt AD, ANC, Delta, Q1, R1, Q2, R2, T; 24349cc55cSDimitry Andric APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth()); 25349cc55cSDimitry Andric struct SignedDivisionByConstantInfo Retval; 26349cc55cSDimitry Andric 27349cc55cSDimitry Andric AD = D.abs(); 28349cc55cSDimitry Andric T = SignedMin + (D.lshr(D.getBitWidth() - 1)); 29349cc55cSDimitry Andric ANC = T - 1 - T.urem(AD); // absolute value of NC 30349cc55cSDimitry Andric P = D.getBitWidth() - 1; // initialize P 31349cc55cSDimitry Andric Q1 = SignedMin.udiv(ANC); // initialize Q1 = 2P/abs(NC) 32349cc55cSDimitry Andric R1 = SignedMin - Q1 * ANC; // initialize R1 = rem(2P,abs(NC)) 33349cc55cSDimitry Andric Q2 = SignedMin.udiv(AD); // initialize Q2 = 2P/abs(D) 34349cc55cSDimitry Andric R2 = SignedMin - Q2 * AD; // initialize R2 = rem(2P,abs(D)) 35349cc55cSDimitry Andric do { 36349cc55cSDimitry Andric P = P + 1; 37349cc55cSDimitry Andric Q1 = Q1 << 1; // update Q1 = 2P/abs(NC) 38349cc55cSDimitry Andric R1 = R1 << 1; // update R1 = rem(2P/abs(NC)) 39349cc55cSDimitry Andric if (R1.uge(ANC)) { // must be unsigned comparison 40349cc55cSDimitry Andric Q1 = Q1 + 1; 41349cc55cSDimitry Andric R1 = R1 - ANC; 42349cc55cSDimitry Andric } 43349cc55cSDimitry Andric Q2 = Q2 << 1; // update Q2 = 2P/abs(D) 44349cc55cSDimitry Andric R2 = R2 << 1; // update R2 = rem(2P/abs(D)) 45349cc55cSDimitry Andric if (R2.uge(AD)) { // must be unsigned comparison 46349cc55cSDimitry Andric Q2 = Q2 + 1; 47349cc55cSDimitry Andric R2 = R2 - AD; 48349cc55cSDimitry Andric } 49349cc55cSDimitry Andric Delta = AD - R2; 50349cc55cSDimitry Andric } while (Q1.ult(Delta) || (Q1 == Delta && R1 == 0)); 51349cc55cSDimitry Andric 52349cc55cSDimitry Andric Retval.Magic = Q2 + 1; 53349cc55cSDimitry Andric if (D.isNegative()) 54349cc55cSDimitry Andric Retval.Magic = -Retval.Magic; // resulting magic number 55349cc55cSDimitry Andric Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift 56349cc55cSDimitry Andric return Retval; 57349cc55cSDimitry Andric } 58349cc55cSDimitry Andric 59349cc55cSDimitry Andric /// Calculate the magic numbers required to implement an unsigned integer 60349cc55cSDimitry Andric /// division by a constant as a sequence of multiplies, adds and shifts. 61349cc55cSDimitry Andric /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry 62349cc55cSDimitry Andric /// S. Warren, Jr., chapter 10. 63349cc55cSDimitry Andric /// LeadingZeros can be used to simplify the calculation if the upper bits 64349cc55cSDimitry Andric /// of the divided value are known zero. 65*fcaf7f86SDimitry Andric UnsignedDivisionByConstantInfo 66*fcaf7f86SDimitry Andric UnsignedDivisionByConstantInfo::get(const APInt &D, unsigned LeadingZeros) { 67349cc55cSDimitry Andric unsigned P; 68349cc55cSDimitry Andric APInt NC, Delta, Q1, R1, Q2, R2; 69*fcaf7f86SDimitry Andric struct UnsignedDivisionByConstantInfo Retval; 7004eeddc0SDimitry Andric Retval.IsAdd = false; // initialize "add" indicator 71349cc55cSDimitry Andric APInt AllOnes = APInt::getAllOnes(D.getBitWidth()).lshr(LeadingZeros); 72349cc55cSDimitry Andric APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth()); 73349cc55cSDimitry Andric APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth()); 74349cc55cSDimitry Andric 75349cc55cSDimitry Andric NC = AllOnes - (AllOnes - D).urem(D); 76349cc55cSDimitry Andric P = D.getBitWidth() - 1; // initialize P 77349cc55cSDimitry Andric Q1 = SignedMin.udiv(NC); // initialize Q1 = 2P/NC 78349cc55cSDimitry Andric R1 = SignedMin - Q1 * NC; // initialize R1 = rem(2P,NC) 79349cc55cSDimitry Andric Q2 = SignedMax.udiv(D); // initialize Q2 = (2P-1)/D 80349cc55cSDimitry Andric R2 = SignedMax - Q2 * D; // initialize R2 = rem((2P-1),D) 81349cc55cSDimitry Andric do { 82349cc55cSDimitry Andric P = P + 1; 83349cc55cSDimitry Andric if (R1.uge(NC - R1)) { 84349cc55cSDimitry Andric Q1 = Q1 + Q1 + 1; // update Q1 85349cc55cSDimitry Andric R1 = R1 + R1 - NC; // update R1 86349cc55cSDimitry Andric } else { 87349cc55cSDimitry Andric Q1 = Q1 + Q1; // update Q1 88349cc55cSDimitry Andric R1 = R1 + R1; // update R1 89349cc55cSDimitry Andric } 90349cc55cSDimitry Andric if ((R2 + 1).uge(D - R2)) { 91349cc55cSDimitry Andric if (Q2.uge(SignedMax)) 9204eeddc0SDimitry Andric Retval.IsAdd = true; 93349cc55cSDimitry Andric Q2 = Q2 + Q2 + 1; // update Q2 94349cc55cSDimitry Andric R2 = R2 + R2 + 1 - D; // update R2 95349cc55cSDimitry Andric } else { 96349cc55cSDimitry Andric if (Q2.uge(SignedMin)) 9704eeddc0SDimitry Andric Retval.IsAdd = true; 98349cc55cSDimitry Andric Q2 = Q2 + Q2; // update Q2 99349cc55cSDimitry Andric R2 = R2 + R2 + 1; // update R2 100349cc55cSDimitry Andric } 101349cc55cSDimitry Andric Delta = D - 1 - R2; 102349cc55cSDimitry Andric } while (P < D.getBitWidth() * 2 && 103349cc55cSDimitry Andric (Q1.ult(Delta) || (Q1 == Delta && R1 == 0))); 104349cc55cSDimitry Andric Retval.Magic = Q2 + 1; // resulting magic number 105349cc55cSDimitry Andric Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift 106349cc55cSDimitry Andric return Retval; 107349cc55cSDimitry Andric } 108