blob: 00cf75bd5cf3b3decc4a9d33f8d6cd5800390ddb [file] [log] [blame]
Jakub Staszak49993f22011-07-25 22:24:51 +00001//====--------------- 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
19using namespace llvm;
20
Jakob Stoklund Olesenff9a5c22013-06-28 18:33:19 +000021/// Multiply FREQ by N and store result in W array.
Michael Gottesman4d078a32013-11-17 03:25:24 +000022static void mult96bit(uint64_t freq, uint32_t N, uint32_t W[3]) {
Jakub Staszak49993f22011-07-25 22:24:51 +000023 uint64_t u0 = freq & UINT32_MAX;
24 uint64_t u1 = freq >> 32;
25
Michael Gottesman4d078a32013-11-17 03:25:24 +000026 // Represent 96-bit value as W[2]:W[1]:W[0];
Jakub Staszak49993f22011-07-25 22:24:51 +000027 uint64_t t = u0 * N;
28 uint64_t k = t >> 32;
Michael Gottesman4d078a32013-11-17 03:25:24 +000029 W[0] = t;
Jakub Staszak49993f22011-07-25 22:24:51 +000030 t = u1 * N + k;
Michael Gottesman4d078a32013-11-17 03:25:24 +000031 W[1] = t;
32 W[2] = t >> 32;
Jakub Staszak49993f22011-07-25 22:24:51 +000033}
34
Michael Gottesman4d078a32013-11-17 03:25:24 +000035/// 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.
37static 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 Staszak49993f22011-07-25 22:24:51 +000043
Michael Gottesman4d078a32013-11-17 03:25:24 +000044 // 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 Staszak49993f22011-07-25 22:24:51 +000065 }
66 }
67
Michael Gottesman4d078a32013-11-17 03:25:24 +000068 // 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 Staszak49993f22011-07-25 22:24:51 +000082}
83
Michael Gottesman4d078a32013-11-17 03:25:24 +000084uint32_t BlockFrequency::scale(uint32_t N, uint32_t D) {
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +000085 assert(D != 0 && "Division by zero");
Jakub Staszaka9e8aa02011-07-27 15:51:51 +000086
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +000087 // 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 Staszak49993f22011-07-25 22:24:51 +000091
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +000092 // If the product fits in 64 bits, just use built-in division.
Jakob Stoklund Olesen3192b2f2013-06-28 21:51:18 +000093 if (MulHi <= UINT32_MAX && MulRes >= MulLo) {
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +000094 Frequency = MulRes / D;
Michael Gottesman4d078a32013-11-17 03:25:24 +000095 return MulRes % D;
Jakub Staszak49993f22011-07-25 22:24:51 +000096 }
97
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +000098 // Product overflowed, use 96-bit operations.
Michael Gottesman4d078a32013-11-17 03:25:24 +000099 // 96-bit value represented as W[2]:W[1]:W[0].
100 uint32_t W[3];
101 uint32_t R;
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +0000102 mult96bit(Frequency, N, W);
Michael Gottesman4d078a32013-11-17 03:25:24 +0000103 Frequency = divrem96bit(W, D, &R);
104 return R;
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +0000105}
106
107BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
108 scale(Prob.getNumerator(), Prob.getDenominator());
Jakub Staszak49993f22011-07-25 22:24:51 +0000109 return *this;
110}
111
112const BlockFrequency
113BlockFrequency::operator*(const BranchProbability &Prob) const {
114 BlockFrequency Freq(Frequency);
115 Freq *= Prob;
116 return Freq;
117}
118
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +0000119BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) {
120 scale(Prob.getDenominator(), Prob.getNumerator());
121 return *this;
122}
123
124BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const {
125 BlockFrequency Freq(Frequency);
126 Freq /= Prob;
127 return Freq;
128}
129
Jakub Staszak49993f22011-07-25 22:24:51 +0000130BlockFrequency &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
141const BlockFrequency
142BlockFrequency::operator+(const BlockFrequency &Prob) const {
143 BlockFrequency Freq(Frequency);
144 Freq += Prob;
145 return Freq;
146}
147
Michael Gottesman8f17dcc2013-12-14 02:24:22 +0000148BlockFrequency &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 Gottesman4d078a32013-11-17 03:25:24 +0000160uint32_t BlockFrequency::scale(const BranchProbability &Prob) {
161 return scale(Prob.getNumerator(), Prob.getDenominator());
162}
163