blob: 5e45e46cf9743d73bb5f44f221dba936aa7f8571 [file] [log] [blame]
Jakub Staszaka26ec882011-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 Olesena45b3752013-06-28 18:33:19 +000021/// Multiply FREQ by N and store result in W array.
22static void mult96bit(uint64_t freq, uint32_t N, uint64_t W[2]) {
Jakub Staszaka26ec882011-07-25 22:24:51 +000023 uint64_t u0 = freq & UINT32_MAX;
24 uint64_t u1 = freq >> 32;
25
26 // Represent 96-bit value as w[2]:w[1]:w[0];
27 uint32_t w[3] = { 0, 0, 0 };
28
29 uint64_t t = u0 * N;
30 uint64_t k = t >> 32;
31 w[0] = t;
32 t = u1 * N + k;
33 w[1] = t;
34 w[2] = t >> 32;
35
36 // W[1] - higher bits.
37 // W[0] - lower bits.
38 W[0] = w[0] + ((uint64_t) w[1] << 32);
39 W[1] = w[2];
40}
41
42
Jakob Stoklund Olesena45b3752013-06-28 18:33:19 +000043/// Divide 96-bit value stored in W array by D.
Jakob Stoklund Olesend7648ff2013-06-28 18:23:42 +000044/// Return 64-bit quotient, saturated to UINT64_MAX on overflow.
Jakob Stoklund Olesena45b3752013-06-28 18:33:19 +000045static uint64_t div96bit(uint64_t W[2], uint32_t D) {
Jakub Staszaka26ec882011-07-25 22:24:51 +000046 uint64_t y = W[0];
47 uint64_t x = W[1];
Jakob Stoklund Olesena45b3752013-06-28 18:33:19 +000048 unsigned i;
Jakub Staszaka26ec882011-07-25 22:24:51 +000049
Jakob Stoklund Olesen5d3257e2013-06-28 21:51:18 +000050 assert(x != 0 && "This is really a 64-bit division");
Jakob Stoklund Olesene1875122013-06-28 21:10:25 +000051
Jakob Stoklund Olesend7648ff2013-06-28 18:23:42 +000052 // This long division algorithm automatically saturates on overflow.
Jakob Stoklund Olesena45b3752013-06-28 18:33:19 +000053 for (i = 0; i < 64 && x; ++i) {
Jakob Stoklund Olesene1875122013-06-28 21:10:25 +000054 uint32_t t = -((x >> 31) & 1); // Splat bit 31 to bits 0-31.
Jakub Staszaka26ec882011-07-25 22:24:51 +000055 x = (x << 1) | (y >> 63);
56 y = y << 1;
57 if ((x | t) >= D) {
58 x -= D;
59 ++y;
60 }
61 }
62
Jakob Stoklund Olesena45b3752013-06-28 18:33:19 +000063 return y << (64 - i);
Jakub Staszaka26ec882011-07-25 22:24:51 +000064}
65
Jakub Staszak636a02b2011-07-27 15:51:51 +000066
Jakob Stoklund Olesend7648ff2013-06-28 18:23:42 +000067void BlockFrequency::scale(uint32_t N, uint32_t D) {
68 assert(D != 0 && "Division by zero");
Jakub Staszak636a02b2011-07-27 15:51:51 +000069
Jakob Stoklund Olesend7648ff2013-06-28 18:23:42 +000070 // Calculate Frequency * N.
71 uint64_t MulLo = (Frequency & UINT32_MAX) * N;
72 uint64_t MulHi = (Frequency >> 32) * N;
73 uint64_t MulRes = (MulHi << 32) + MulLo;
Jakub Staszaka26ec882011-07-25 22:24:51 +000074
Jakob Stoklund Olesend7648ff2013-06-28 18:23:42 +000075 // If the product fits in 64 bits, just use built-in division.
Jakob Stoklund Olesen5d3257e2013-06-28 21:51:18 +000076 if (MulHi <= UINT32_MAX && MulRes >= MulLo) {
Jakob Stoklund Olesend7648ff2013-06-28 18:23:42 +000077 Frequency = MulRes / D;
78 return;
Jakub Staszaka26ec882011-07-25 22:24:51 +000079 }
80
Jakob Stoklund Olesend7648ff2013-06-28 18:23:42 +000081 // Product overflowed, use 96-bit operations.
82 // 96-bit value represented as W[1]:W[0].
83 uint64_t W[2];
84 mult96bit(Frequency, N, W);
85 Frequency = div96bit(W, D);
86 return;
87}
88
89BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
90 scale(Prob.getNumerator(), Prob.getDenominator());
Jakub Staszaka26ec882011-07-25 22:24:51 +000091 return *this;
92}
93
94const BlockFrequency
95BlockFrequency::operator*(const BranchProbability &Prob) const {
96 BlockFrequency Freq(Frequency);
97 Freq *= Prob;
98 return Freq;
99}
100
Jakob Stoklund Olesend7648ff2013-06-28 18:23:42 +0000101BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) {
102 scale(Prob.getDenominator(), Prob.getNumerator());
103 return *this;
104}
105
106BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const {
107 BlockFrequency Freq(Frequency);
108 Freq /= Prob;
109 return Freq;
110}
111
Jakub Staszaka26ec882011-07-25 22:24:51 +0000112BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
113 uint64_t Before = Freq.Frequency;
114 Frequency += Freq.Frequency;
115
116 // If overflow, set frequency to the maximum value.
117 if (Frequency < Before)
118 Frequency = UINT64_MAX;
119
120 return *this;
121}
122
123const BlockFrequency
124BlockFrequency::operator+(const BlockFrequency &Prob) const {
125 BlockFrequency Freq(Frequency);
126 Freq += Prob;
127 return Freq;
128}
129
130void BlockFrequency::print(raw_ostream &OS) const {
Jakob Stoklund Olesenb1c0cc22013-06-25 21:57:38 +0000131 // Convert fixed-point number to decimal.
132 OS << Frequency / getEntryFrequency() << ".";
133 uint64_t Rem = Frequency % getEntryFrequency();
134 uint64_t Eps = 1;
135 do {
136 Rem *= 10;
137 Eps *= 10;
138 OS << Rem / getEntryFrequency();
139 Rem = Rem % getEntryFrequency();
140 } while (Rem >= Eps/2);
Jakub Staszaka26ec882011-07-25 22:24:51 +0000141}
142
143namespace llvm {
144
145raw_ostream &operator<<(raw_ostream &OS, const BlockFrequency &Freq) {
146 Freq.print(OS);
147 return OS;
148}
149
150}