Path: blob/main/contrib/llvm-project/llvm/lib/Support/DivisionByConstantInfo.cpp
35233 views
//===----- DivisionByConstantInfo.cpp - division by constant -*- C++ -*----===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// This file implements support for optimizing divisions by a constant9///10//===----------------------------------------------------------------------===//1112#include "llvm/Support/DivisionByConstantInfo.h"1314using namespace llvm;1516/// Calculate the magic numbers required to implement a signed integer division17/// by a constant as a sequence of multiplies, adds and shifts. Requires that18/// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S.19/// Warren, Jr., Chapter 10.20SignedDivisionByConstantInfo SignedDivisionByConstantInfo::get(const APInt &D) {21assert(!D.isZero() && "Precondition violation.");2223// We'd be endlessly stuck in the loop.24assert(D.getBitWidth() >= 3 && "Does not work at smaller bitwidths.");2526APInt Delta;27APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());28struct SignedDivisionByConstantInfo Retval;2930APInt AD = D.abs();31APInt T = SignedMin + (D.lshr(D.getBitWidth() - 1));32APInt ANC = T - 1 - T.urem(AD); // absolute value of NC33unsigned P = D.getBitWidth() - 1; // initialize P34APInt Q1, R1, Q2, R2;35// initialize Q1 = 2P/abs(NC); R1 = rem(2P,abs(NC))36APInt::udivrem(SignedMin, ANC, Q1, R1);37// initialize Q2 = 2P/abs(D); R2 = rem(2P,abs(D))38APInt::udivrem(SignedMin, AD, Q2, R2);39do {40P = P + 1;41Q1 <<= 1; // update Q1 = 2P/abs(NC)42R1 <<= 1; // update R1 = rem(2P/abs(NC))43if (R1.uge(ANC)) { // must be unsigned comparison44++Q1;45R1 -= ANC;46}47Q2 <<= 1; // update Q2 = 2P/abs(D)48R2 <<= 1; // update R2 = rem(2P/abs(D))49if (R2.uge(AD)) { // must be unsigned comparison50++Q2;51R2 -= AD;52}53// Delta = AD - R254Delta = AD;55Delta -= R2;56} while (Q1.ult(Delta) || (Q1 == Delta && R1.isZero()));5758Retval.Magic = std::move(Q2);59++Retval.Magic;60if (D.isNegative())61Retval.Magic.negate(); // resulting magic number62Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift63return Retval;64}6566/// Calculate the magic numbers required to implement an unsigned integer67/// division by a constant as a sequence of multiplies, adds and shifts.68/// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry69/// S. Warren, Jr., chapter 10.70/// LeadingZeros can be used to simplify the calculation if the upper bits71/// of the divided value are known zero.72UnsignedDivisionByConstantInfo73UnsignedDivisionByConstantInfo::get(const APInt &D, unsigned LeadingZeros,74bool AllowEvenDivisorOptimization) {75assert(!D.isZero() && !D.isOne() && "Precondition violation.");76assert(D.getBitWidth() > 1 && "Does not work at smaller bitwidths.");7778APInt Delta;79struct UnsignedDivisionByConstantInfo Retval;80Retval.IsAdd = false; // initialize "add" indicator81APInt AllOnes =82APInt::getLowBitsSet(D.getBitWidth(), D.getBitWidth() - LeadingZeros);83APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());84APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth());8586// Calculate NC, the largest dividend such that NC.urem(D) == D-1.87APInt NC = AllOnes - (AllOnes + 1 - D).urem(D);88assert(NC.urem(D) == D - 1 && "Unexpected NC value");89unsigned P = D.getBitWidth() - 1; // initialize P90APInt Q1, R1, Q2, R2;91// initialize Q1 = 2P/NC; R1 = rem(2P,NC)92APInt::udivrem(SignedMin, NC, Q1, R1);93// initialize Q2 = (2P-1)/D; R2 = rem((2P-1),D)94APInt::udivrem(SignedMax, D, Q2, R2);95do {96P = P + 1;97if (R1.uge(NC - R1)) {98// update Q199Q1 <<= 1;100++Q1;101// update R1102R1 <<= 1;103R1 -= NC;104} else {105Q1 <<= 1; // update Q1106R1 <<= 1; // update R1107}108if ((R2 + 1).uge(D - R2)) {109if (Q2.uge(SignedMax))110Retval.IsAdd = true;111// update Q2112Q2 <<= 1;113++Q2;114// update R2115R2 <<= 1;116++R2;117R2 -= D;118} else {119if (Q2.uge(SignedMin))120Retval.IsAdd = true;121// update Q2122Q2 <<= 1;123// update R2124R2 <<= 1;125++R2;126}127// Delta = D - 1 - R2128Delta = D;129--Delta;130Delta -= R2;131} while (P < D.getBitWidth() * 2 &&132(Q1.ult(Delta) || (Q1 == Delta && R1.isZero())));133134if (Retval.IsAdd && !D[0] && AllowEvenDivisorOptimization) {135unsigned PreShift = D.countr_zero();136APInt ShiftedD = D.lshr(PreShift);137Retval =138UnsignedDivisionByConstantInfo::get(ShiftedD, LeadingZeros + PreShift);139assert(Retval.IsAdd == 0 && Retval.PreShift == 0);140Retval.PreShift = PreShift;141return Retval;142}143144Retval.Magic = std::move(Q2); // resulting magic number145++Retval.Magic;146Retval.PostShift = P - D.getBitWidth(); // resulting shift147// Reduce shift amount for IsAdd.148if (Retval.IsAdd) {149assert(Retval.PostShift > 0 && "Unexpected shift");150Retval.PostShift -= 1;151}152Retval.PreShift = 0;153return Retval;154}155156157