1 //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements Block Frequency class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/BranchProbability.h"
15 #include "llvm/Support/BlockFrequency.h"
16 #include "llvm/Support/raw_ostream.h"
21 /// Multiply FREQ by N and store result in W array.
22 static void mult96bit(uint64_t freq, uint32_t N, uint32_t W[3]) {
23 uint64_t u0 = freq & UINT32_MAX;
24 uint64_t u1 = freq >> 32;
26 // Represent 96-bit value as W[2]:W[1]:W[0];
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 };
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.
51 for (int i = 2; i >= 0; --i) {
52 uint64_t PartialD = uint64_t(R) << 32 | W[i];
56 } else if (PartialD < D) {
58 R = uint32_t(PartialD);
59 } else if (PartialD == D) {
63 Q[i] = uint32_t(PartialD / D);
64 R = uint32_t(PartialD - (Q[i] * D));
68 // If Q[2] is non-zero, then we overflowed.
74 // Form the final uint64_t result, avoiding endianness issues.
75 Result = uint64_t(Q[0]) | (uint64_t(Q[1]) << 32);
84 uint32_t BlockFrequency::scale(uint32_t N, uint32_t D) {
85 assert(D != 0 && "Division by zero");
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;
92 // If the product fits in 64 bits, just use built-in division.
93 if (MulHi <= UINT32_MAX && MulRes >= MulLo) {
94 Frequency = MulRes / D;
98 // Product overflowed, use 96-bit operations.
99 // 96-bit value represented as W[2]:W[1]:W[0].
102 mult96bit(Frequency, N, W);
103 Frequency = divrem96bit(W, D, &R);
107 BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
108 scale(Prob.getNumerator(), Prob.getDenominator());
113 BlockFrequency::operator*(const BranchProbability &Prob) const {
114 BlockFrequency Freq(Frequency);
119 BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) {
120 scale(Prob.getDenominator(), Prob.getNumerator());
124 BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const {
125 BlockFrequency Freq(Frequency);
130 BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
131 uint64_t Before = Freq.Frequency;
132 Frequency += Freq.Frequency;
134 // If overflow, set frequency to the maximum value.
135 if (Frequency < Before)
136 Frequency = UINT64_MAX;
142 BlockFrequency::operator+(const BlockFrequency &Prob) const {
143 BlockFrequency Freq(Frequency);
148 uint32_t BlockFrequency::scale(const BranchProbability &Prob) {
149 return scale(Prob.getNumerator(), Prob.getDenominator());
152 void BlockFrequency::print(raw_ostream &OS) const {
153 // Convert fixed-point number to decimal.
154 OS << Frequency / getEntryFrequency() << ".";
155 uint64_t Rem = Frequency % getEntryFrequency();
160 OS << Rem / getEntryFrequency();
161 Rem = Rem % getEntryFrequency();
162 } while (Rem >= Eps/2);
167 raw_ostream &operator<<(raw_ostream &OS, const BlockFrequency &Freq) {