blob: 84a993e3e5b6fd6a54dad76dc982b2c7ced36cf1 [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
Jakub Staszak636a02b2011-07-27 15:51:51 +000021namespace {
22
Jakub Staszaka26ec882011-07-25 22:24:51 +000023/// mult96bit - Multiply FREQ by N and store result in W array.
Jakub Staszak636a02b2011-07-27 15:51:51 +000024void mult96bit(uint64_t freq, uint32_t N, uint64_t W[2]) {
Jakub Staszaka26ec882011-07-25 22:24:51 +000025 uint64_t u0 = freq & UINT32_MAX;
26 uint64_t u1 = freq >> 32;
27
28 // Represent 96-bit value as w[2]:w[1]:w[0];
29 uint32_t w[3] = { 0, 0, 0 };
30
31 uint64_t t = u0 * N;
32 uint64_t k = t >> 32;
33 w[0] = t;
34 t = u1 * N + k;
35 w[1] = t;
36 w[2] = t >> 32;
37
38 // W[1] - higher bits.
39 // W[0] - lower bits.
40 W[0] = w[0] + ((uint64_t) w[1] << 32);
41 W[1] = w[2];
42}
43
44
45/// div96bit - Divide 96-bit value stored in W array by D. Return 64-bit frequency.
Jakub Staszak636a02b2011-07-27 15:51:51 +000046uint64_t div96bit(uint64_t W[2], uint32_t D) {
Jakub Staszaka26ec882011-07-25 22:24:51 +000047 uint64_t y = W[0];
48 uint64_t x = W[1];
Jakub Staszakffcc2a52011-07-27 16:00:40 +000049 int i;
Jakub Staszaka26ec882011-07-25 22:24:51 +000050
Jakub Staszakffcc2a52011-07-27 16:00:40 +000051 for (i = 1; i <= 64 && x; ++i) {
Jakub Staszaka26ec882011-07-25 22:24:51 +000052 uint32_t t = (int)x >> 31;
53 x = (x << 1) | (y >> 63);
54 y = y << 1;
55 if ((x | t) >= D) {
56 x -= D;
57 ++y;
58 }
59 }
60
Jakub Staszakffcc2a52011-07-27 16:00:40 +000061 return y << (64 - i + 1);
Jakub Staszaka26ec882011-07-25 22:24:51 +000062}
63
Jakub Staszak636a02b2011-07-27 15:51:51 +000064}
65
66
Jakub Staszaka26ec882011-07-25 22:24:51 +000067BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
68 uint32_t n = Prob.getNumerator();
69 uint32_t d = Prob.getDenominator();
70
71 assert(n <= d && "Probability must be less or equal to 1.");
72
Benjamin Kramerbc1430a2011-10-27 16:38:50 +000073 // Calculate Frequency * n.
74 uint64_t mulLo = (Frequency & UINT32_MAX) * n;
75 uint64_t mulHi = (Frequency >> 32) * n;
76 uint64_t mulRes = (mulHi << 32) + mulLo;
77
78 // If there was overflow use 96-bit operations.
79 if (mulHi > UINT32_MAX || mulRes < mulLo) {
Jakub Staszaka26ec882011-07-25 22:24:51 +000080 // 96-bit value represented as W[1]:W[0].
81 uint64_t W[2];
82
83 // Probability is less or equal to 1 which means that results must fit
84 // 64-bit.
85 mult96bit(Frequency, n, W);
86 Frequency = div96bit(W, d);
87 return *this;
88 }
89
Benjamin Kramerbc1430a2011-10-27 16:38:50 +000090 Frequency = mulRes / d;
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
101BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
102 uint64_t Before = Freq.Frequency;
103 Frequency += Freq.Frequency;
104
105 // If overflow, set frequency to the maximum value.
106 if (Frequency < Before)
107 Frequency = UINT64_MAX;
108
109 return *this;
110}
111
112const BlockFrequency
113BlockFrequency::operator+(const BlockFrequency &Prob) const {
114 BlockFrequency Freq(Frequency);
115 Freq += Prob;
116 return Freq;
117}
118
119void BlockFrequency::print(raw_ostream &OS) const {
120 OS << Frequency;
121}
122
123namespace llvm {
124
125raw_ostream &operator<<(raw_ostream &OS, const BlockFrequency &Freq) {
126 Freq.print(OS);
127 return OS;
128}
129
130}