DivisionByConstantInfo.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //===----- DivisionByConstantInfo.cpp - division by constant -*- C++ -*----===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// This file implements support for optimizing divisions by a constant
  10. ///
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Support/DivisionByConstantInfo.h"
  13. using namespace llvm;
  14. /// Calculate the magic numbers required to implement a signed integer division
  15. /// by a constant as a sequence of multiplies, adds and shifts. Requires that
  16. /// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S.
  17. /// Warren, Jr., Chapter 10.
  18. SignedDivisionByConstantInfo SignedDivisionByConstantInfo::get(const APInt &D) {
  19. assert(!D.isZero() && "Precondition violation.");
  20. // We'd be endlessly stuck in the loop.
  21. assert(D.getBitWidth() >= 3 && "Does not work at smaller bitwidths.");
  22. APInt Delta;
  23. APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
  24. struct SignedDivisionByConstantInfo Retval;
  25. APInt AD = D.abs();
  26. APInt T = SignedMin + (D.lshr(D.getBitWidth() - 1));
  27. APInt ANC = T - 1 - T.urem(AD); // absolute value of NC
  28. unsigned P = D.getBitWidth() - 1; // initialize P
  29. APInt Q1, R1, Q2, R2;
  30. // initialize Q1 = 2P/abs(NC); R1 = rem(2P,abs(NC))
  31. APInt::udivrem(SignedMin, ANC, Q1, R1);
  32. // initialize Q2 = 2P/abs(D); R2 = rem(2P,abs(D))
  33. APInt::udivrem(SignedMin, AD, Q2, R2);
  34. do {
  35. P = P + 1;
  36. Q1 <<= 1; // update Q1 = 2P/abs(NC)
  37. R1 <<= 1; // update R1 = rem(2P/abs(NC))
  38. if (R1.uge(ANC)) { // must be unsigned comparison
  39. ++Q1;
  40. R1 -= ANC;
  41. }
  42. Q2 <<= 1; // update Q2 = 2P/abs(D)
  43. R2 <<= 1; // update R2 = rem(2P/abs(D))
  44. if (R2.uge(AD)) { // must be unsigned comparison
  45. ++Q2;
  46. R2 -= AD;
  47. }
  48. // Delta = AD - R2
  49. Delta = AD;
  50. Delta -= R2;
  51. } while (Q1.ult(Delta) || (Q1 == Delta && R1.isZero()));
  52. Retval.Magic = std::move(Q2);
  53. ++Retval.Magic;
  54. if (D.isNegative())
  55. Retval.Magic.negate(); // resulting magic number
  56. Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift
  57. return Retval;
  58. }
  59. /// Calculate the magic numbers required to implement an unsigned integer
  60. /// division by a constant as a sequence of multiplies, adds and shifts.
  61. /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry
  62. /// S. Warren, Jr., chapter 10.
  63. /// LeadingZeros can be used to simplify the calculation if the upper bits
  64. /// of the divided value are known zero.
  65. UnsignedDivisionByConstantInfo
  66. UnsignedDivisionByConstantInfo::get(const APInt &D, unsigned LeadingZeros,
  67. bool AllowEvenDivisorOptimization) {
  68. assert(!D.isZero() && !D.isOne() && "Precondition violation.");
  69. assert(D.getBitWidth() > 1 && "Does not work at smaller bitwidths.");
  70. APInt Delta;
  71. struct UnsignedDivisionByConstantInfo Retval;
  72. Retval.IsAdd = false; // initialize "add" indicator
  73. APInt AllOnes = APInt::getAllOnes(D.getBitWidth()).lshr(LeadingZeros);
  74. APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
  75. APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth());
  76. // Calculate NC, the largest dividend such that NC.urem(D) == D-1.
  77. APInt NC = AllOnes - (AllOnes + 1 - D).urem(D);
  78. assert(NC.urem(D) == D - 1 && "Unexpected NC value");
  79. unsigned P = D.getBitWidth() - 1; // initialize P
  80. APInt Q1, R1, Q2, R2;
  81. // initialize Q1 = 2P/NC; R1 = rem(2P,NC)
  82. APInt::udivrem(SignedMin, NC, Q1, R1);
  83. // initialize Q2 = (2P-1)/D; R2 = rem((2P-1),D)
  84. APInt::udivrem(SignedMax, D, Q2, R2);
  85. do {
  86. P = P + 1;
  87. if (R1.uge(NC - R1)) {
  88. // update Q1
  89. Q1 <<= 1;
  90. ++Q1;
  91. // update R1
  92. R1 <<= 1;
  93. R1 -= NC;
  94. } else {
  95. Q1 <<= 1; // update Q1
  96. R1 <<= 1; // update R1
  97. }
  98. if ((R2 + 1).uge(D - R2)) {
  99. if (Q2.uge(SignedMax))
  100. Retval.IsAdd = true;
  101. // update Q2
  102. Q2 <<= 1;
  103. ++Q2;
  104. // update R2
  105. R2 <<= 1;
  106. ++R2;
  107. R2 -= D;
  108. } else {
  109. if (Q2.uge(SignedMin))
  110. Retval.IsAdd = true;
  111. // update Q2
  112. Q2 <<= 1;
  113. // update R2
  114. R2 <<= 1;
  115. ++R2;
  116. }
  117. // Delta = D - 1 - R2
  118. Delta = D;
  119. --Delta;
  120. Delta -= R2;
  121. } while (P < D.getBitWidth() * 2 &&
  122. (Q1.ult(Delta) || (Q1 == Delta && R1.isZero())));
  123. if (Retval.IsAdd && !D[0] && AllowEvenDivisorOptimization) {
  124. unsigned PreShift = D.countTrailingZeros();
  125. APInt ShiftedD = D.lshr(PreShift);
  126. Retval =
  127. UnsignedDivisionByConstantInfo::get(ShiftedD, LeadingZeros + PreShift);
  128. assert(Retval.IsAdd == 0 && Retval.PreShift == 0);
  129. Retval.PreShift = PreShift;
  130. return Retval;
  131. }
  132. Retval.Magic = std::move(Q2); // resulting magic number
  133. ++Retval.Magic;
  134. Retval.PostShift = P - D.getBitWidth(); // resulting shift
  135. // Reduce shift amount for IsAdd.
  136. if (Retval.IsAdd) {
  137. assert(Retval.PostShift > 0 && "Unexpected shift");
  138. Retval.PostShift -= 1;
  139. }
  140. Retval.PreShift = 0;
  141. return Retval;
  142. }