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"
23 /// mult96bit - Multiply FREQ by N and store result in W array.
24 void mult96bit(uint64_t freq, uint32_t N, uint64_t W[2]) {
25 uint64_t u0 = freq & UINT32_MAX;
26 uint64_t u1 = freq >> 32;
28 // Represent 96-bit value as w[2]:w[1]:w[0];
29 uint32_t w[3] = { 0, 0, 0 };
38 // W[1] - higher bits.
40 W[0] = w[0] + ((uint64_t) w[1] << 32);
45 /// div96bit - Divide 96-bit value stored in W array by D. Return 64-bit frequency.
46 uint64_t div96bit(uint64_t W[2], uint32_t D) {
51 for (i = 1; i <= 64 && x; ++i) {
52 uint32_t t = (int)x >> 31;
53 x = (x << 1) | (y >> 63);
61 return y << (64 - i + 1);
67 BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
71 uint32_t n = Prob.getNumerator();
72 uint32_t d = Prob.getDenominator();
74 assert(n <= d && "Probability must be less or equal to 1.");
76 // Calculate Frequency * n.
77 uint64_t mulLo = (Frequency & UINT32_MAX) * n;
78 uint64_t mulHi = (Frequency >> 32) * n;
79 uint64_t mulRes = (mulHi << 32) + mulLo;
81 // If there was overflow use 96-bit operations.
82 if (mulHi > UINT32_MAX || mulRes < mulLo) {
83 // 96-bit value represented as W[1]:W[0].
86 // Probability is less or equal to 1 which means that results must fit
88 mult96bit(Frequency, n, W);
89 Frequency = div96bit(W, d);
92 Frequency = mulRes / d;
95 // Limit the result to 1; 0 is a sentinel value. This keeps BlockFrequencyInfo
96 // from getting stuck at zero frequencies just because a value became too
97 // small to be represented as a BlockFrequency.
98 Frequency = (n == 0 || Frequency != 0) ? Frequency : 1;
103 BlockFrequency::operator*(const BranchProbability &Prob) const {
104 BlockFrequency Freq(Frequency);
109 BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
110 uint64_t Before = Freq.Frequency;
111 Frequency += Freq.Frequency;
113 // If overflow, set frequency to the maximum value.
114 if (Frequency < Before)
115 Frequency = UINT64_MAX;
121 BlockFrequency::operator+(const BlockFrequency &Prob) const {
122 BlockFrequency Freq(Frequency);
127 void BlockFrequency::print(raw_ostream &OS) const {
133 raw_ostream &operator<<(raw_ostream &OS, const BlockFrequency &Freq) {