DivisionByConstantInfo.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //===----- DivisonByConstantInfo.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. unsigned P;
  20. APInt AD, ANC, Delta, Q1, R1, Q2, R2, T;
  21. APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
  22. struct SignedDivisionByConstantInfo Retval;
  23. AD = D.abs();
  24. T = SignedMin + (D.lshr(D.getBitWidth() - 1));
  25. ANC = T - 1 - T.urem(AD); // absolute value of NC
  26. P = D.getBitWidth() - 1; // initialize P
  27. Q1 = SignedMin.udiv(ANC); // initialize Q1 = 2P/abs(NC)
  28. R1 = SignedMin - Q1 * ANC; // initialize R1 = rem(2P,abs(NC))
  29. Q2 = SignedMin.udiv(AD); // initialize Q2 = 2P/abs(D)
  30. R2 = SignedMin - Q2 * AD; // initialize R2 = rem(2P,abs(D))
  31. do {
  32. P = P + 1;
  33. Q1 = Q1 << 1; // update Q1 = 2P/abs(NC)
  34. R1 = R1 << 1; // update R1 = rem(2P/abs(NC))
  35. if (R1.uge(ANC)) { // must be unsigned comparison
  36. Q1 = Q1 + 1;
  37. R1 = R1 - ANC;
  38. }
  39. Q2 = Q2 << 1; // update Q2 = 2P/abs(D)
  40. R2 = R2 << 1; // update R2 = rem(2P/abs(D))
  41. if (R2.uge(AD)) { // must be unsigned comparison
  42. Q2 = Q2 + 1;
  43. R2 = R2 - AD;
  44. }
  45. Delta = AD - R2;
  46. } while (Q1.ult(Delta) || (Q1 == Delta && R1 == 0));
  47. Retval.Magic = Q2 + 1;
  48. if (D.isNegative())
  49. Retval.Magic = -Retval.Magic; // resulting magic number
  50. Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift
  51. return Retval;
  52. }
  53. /// Calculate the magic numbers required to implement an unsigned integer
  54. /// division by a constant as a sequence of multiplies, adds and shifts.
  55. /// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry
  56. /// S. Warren, Jr., chapter 10.
  57. /// LeadingZeros can be used to simplify the calculation if the upper bits
  58. /// of the divided value are known zero.
  59. UnsignedDivisonByConstantInfo
  60. UnsignedDivisonByConstantInfo::get(const APInt &D, unsigned LeadingZeros) {
  61. unsigned P;
  62. APInt NC, Delta, Q1, R1, Q2, R2;
  63. struct UnsignedDivisonByConstantInfo Retval;
  64. Retval.IsAdd = false; // initialize "add" indicator
  65. APInt AllOnes = APInt::getAllOnes(D.getBitWidth()).lshr(LeadingZeros);
  66. APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
  67. APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth());
  68. NC = AllOnes - (AllOnes - D).urem(D);
  69. P = D.getBitWidth() - 1; // initialize P
  70. Q1 = SignedMin.udiv(NC); // initialize Q1 = 2P/NC
  71. R1 = SignedMin - Q1 * NC; // initialize R1 = rem(2P,NC)
  72. Q2 = SignedMax.udiv(D); // initialize Q2 = (2P-1)/D
  73. R2 = SignedMax - Q2 * D; // initialize R2 = rem((2P-1),D)
  74. do {
  75. P = P + 1;
  76. if (R1.uge(NC - R1)) {
  77. Q1 = Q1 + Q1 + 1; // update Q1
  78. R1 = R1 + R1 - NC; // update R1
  79. } else {
  80. Q1 = Q1 + Q1; // update Q1
  81. R1 = R1 + R1; // update R1
  82. }
  83. if ((R2 + 1).uge(D - R2)) {
  84. if (Q2.uge(SignedMax))
  85. Retval.IsAdd = true;
  86. Q2 = Q2 + Q2 + 1; // update Q2
  87. R2 = R2 + R2 + 1 - D; // update R2
  88. } else {
  89. if (Q2.uge(SignedMin))
  90. Retval.IsAdd = true;
  91. Q2 = Q2 + Q2; // update Q2
  92. R2 = R2 + R2 + 1; // update R2
  93. }
  94. Delta = D - 1 - R2;
  95. } while (P < D.getBitWidth() * 2 &&
  96. (Q1.ult(Delta) || (Q1 == Delta && R1 == 0)));
  97. Retval.Magic = Q2 + 1; // resulting magic number
  98. Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift
  99. return Retval;
  100. }