Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 1 | //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements Block Frequency class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/BranchProbability.h" |
| 15 | #include "llvm/Support/BlockFrequency.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include <cassert> |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
Jakob Stoklund Olesen | ff9a5c2 | 2013-06-28 18:33:19 +0000 | [diff] [blame] | 21 | /// Multiply FREQ by N and store result in W array. |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 22 | static void mult96bit(uint64_t freq, uint32_t N, uint32_t W[3]) { |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 23 | uint64_t u0 = freq & UINT32_MAX; |
| 24 | uint64_t u1 = freq >> 32; |
| 25 | |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 26 | // Represent 96-bit value as W[2]:W[1]:W[0]; |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 27 | uint64_t t = u0 * N; |
| 28 | uint64_t k = t >> 32; |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 29 | W[0] = t; |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 30 | t = u1 * N + k; |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 31 | W[1] = t; |
| 32 | W[2] = t >> 32; |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 35 | /// Divide 96-bit value stored in W[2]:W[1]:W[0] by D. Since our word size is a |
| 36 | /// 32 bit unsigned integer, we can use a short division algorithm. |
| 37 | static uint64_t divrem96bit(uint32_t W[3], uint32_t D, uint32_t *Rout) { |
| 38 | // We assume that W[2] is non-zero since if W[2] is not then the user should |
| 39 | // just use hardware division. |
| 40 | assert(W[2] && "This routine assumes that W[2] is non-zero since if W[2] is " |
| 41 | "zero, the caller should just use 64/32 hardware."); |
| 42 | uint32_t Q[3] = { 0, 0, 0 }; |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 43 | |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 44 | // The generalized short division algorithm sets i to m + n - 1, where n is |
| 45 | // the number of words in the divisior and m is the number of words by which |
| 46 | // the divident exceeds the divisor (i.e. m + n == the length of the dividend |
| 47 | // in words). Due to our assumption that W[2] is non-zero, we know that the |
| 48 | // dividend is of length 3 implying since n is 1 that m = 2. Thus we set i to |
| 49 | // m + n - 1 = 2 + 1 - 1 = 2. |
| 50 | uint32_t R = 0; |
| 51 | for (int i = 2; i >= 0; --i) { |
| 52 | uint64_t PartialD = uint64_t(R) << 32 | W[i]; |
| 53 | if (PartialD == 0) { |
| 54 | Q[i] = 0; |
| 55 | R = 0; |
| 56 | } else if (PartialD < D) { |
| 57 | Q[i] = 0; |
| 58 | R = uint32_t(PartialD); |
| 59 | } else if (PartialD == D) { |
| 60 | Q[i] = 1; |
| 61 | R = 0; |
| 62 | } else { |
| 63 | Q[i] = uint32_t(PartialD / D); |
| 64 | R = uint32_t(PartialD - (Q[i] * D)); |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 68 | // If Q[2] is non-zero, then we overflowed. |
| 69 | uint64_t Result; |
| 70 | if (Q[2]) { |
| 71 | Result = UINT64_MAX; |
| 72 | R = D; |
| 73 | } else { |
| 74 | // Form the final uint64_t result, avoiding endianness issues. |
| 75 | Result = uint64_t(Q[0]) | (uint64_t(Q[1]) << 32); |
| 76 | } |
| 77 | |
| 78 | if (Rout) |
| 79 | *Rout = R; |
| 80 | |
| 81 | return Result; |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 84 | uint32_t BlockFrequency::scale(uint32_t N, uint32_t D) { |
Jakob Stoklund Olesen | c506e5d | 2013-06-28 18:23:42 +0000 | [diff] [blame] | 85 | assert(D != 0 && "Division by zero"); |
Jakub Staszak | a9e8aa0 | 2011-07-27 15:51:51 +0000 | [diff] [blame] | 86 | |
Jakob Stoklund Olesen | c506e5d | 2013-06-28 18:23:42 +0000 | [diff] [blame] | 87 | // Calculate Frequency * N. |
| 88 | uint64_t MulLo = (Frequency & UINT32_MAX) * N; |
| 89 | uint64_t MulHi = (Frequency >> 32) * N; |
| 90 | uint64_t MulRes = (MulHi << 32) + MulLo; |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 91 | |
Jakob Stoklund Olesen | c506e5d | 2013-06-28 18:23:42 +0000 | [diff] [blame] | 92 | // If the product fits in 64 bits, just use built-in division. |
Jakob Stoklund Olesen | 3192b2f | 2013-06-28 21:51:18 +0000 | [diff] [blame] | 93 | if (MulHi <= UINT32_MAX && MulRes >= MulLo) { |
Jakob Stoklund Olesen | c506e5d | 2013-06-28 18:23:42 +0000 | [diff] [blame] | 94 | Frequency = MulRes / D; |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 95 | return MulRes % D; |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Jakob Stoklund Olesen | c506e5d | 2013-06-28 18:23:42 +0000 | [diff] [blame] | 98 | // Product overflowed, use 96-bit operations. |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 99 | // 96-bit value represented as W[2]:W[1]:W[0]. |
| 100 | uint32_t W[3]; |
| 101 | uint32_t R; |
Jakob Stoklund Olesen | c506e5d | 2013-06-28 18:23:42 +0000 | [diff] [blame] | 102 | mult96bit(Frequency, N, W); |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 103 | Frequency = divrem96bit(W, D, &R); |
| 104 | return R; |
Jakob Stoklund Olesen | c506e5d | 2013-06-28 18:23:42 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) { |
| 108 | scale(Prob.getNumerator(), Prob.getDenominator()); |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 109 | return *this; |
| 110 | } |
| 111 | |
| 112 | const BlockFrequency |
| 113 | BlockFrequency::operator*(const BranchProbability &Prob) const { |
| 114 | BlockFrequency Freq(Frequency); |
| 115 | Freq *= Prob; |
| 116 | return Freq; |
| 117 | } |
| 118 | |
Jakob Stoklund Olesen | c506e5d | 2013-06-28 18:23:42 +0000 | [diff] [blame] | 119 | BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) { |
| 120 | scale(Prob.getDenominator(), Prob.getNumerator()); |
| 121 | return *this; |
| 122 | } |
| 123 | |
| 124 | BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const { |
| 125 | BlockFrequency Freq(Frequency); |
| 126 | Freq /= Prob; |
| 127 | return Freq; |
| 128 | } |
| 129 | |
Jakub Staszak | 49993f2 | 2011-07-25 22:24:51 +0000 | [diff] [blame] | 130 | BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) { |
| 131 | uint64_t Before = Freq.Frequency; |
| 132 | Frequency += Freq.Frequency; |
| 133 | |
| 134 | // If overflow, set frequency to the maximum value. |
| 135 | if (Frequency < Before) |
| 136 | Frequency = UINT64_MAX; |
| 137 | |
| 138 | return *this; |
| 139 | } |
| 140 | |
| 141 | const BlockFrequency |
| 142 | BlockFrequency::operator+(const BlockFrequency &Prob) const { |
| 143 | BlockFrequency Freq(Frequency); |
| 144 | Freq += Prob; |
| 145 | return Freq; |
| 146 | } |
| 147 | |
Michael Gottesman | 8f17dcc | 2013-12-14 02:24:22 +0000 | [diff] [blame] | 148 | BlockFrequency &BlockFrequency::operator>>=(const unsigned count) { |
| 149 | // Frequency can never be 0 by design. |
| 150 | assert(Frequency != 0); |
| 151 | |
| 152 | // Shift right by count. |
| 153 | Frequency >>= count; |
| 154 | |
| 155 | // Saturate to 1 if we are 0. |
| 156 | Frequency |= Frequency == 0; |
| 157 | return *this; |
| 158 | } |
| 159 | |
Michael Gottesman | 4d078a3 | 2013-11-17 03:25:24 +0000 | [diff] [blame] | 160 | uint32_t BlockFrequency::scale(const BranchProbability &Prob) { |
| 161 | return scale(Prob.getNumerator(), Prob.getDenominator()); |
| 162 | } |
| 163 | |