e0430c199eb86e2b9fec33224d8a1521805598b3
[oota-llvm.git] / lib / Support / BlockFrequency.cpp
1 //====--------------- 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/BlockFrequency.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include <cassert>
17
18 using namespace llvm;
19
20 BlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) {
21   Frequency = Prob.scale(Frequency);
22   return *this;
23 }
24
25 BlockFrequency BlockFrequency::operator*(BranchProbability Prob) const {
26   BlockFrequency Freq(Frequency);
27   Freq *= Prob;
28   return Freq;
29 }
30
31 BlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) {
32   Frequency = Prob.scaleByInverse(Frequency);
33   return *this;
34 }
35
36 BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const {
37   BlockFrequency Freq(Frequency);
38   Freq /= Prob;
39   return Freq;
40 }
41
42 BlockFrequency &BlockFrequency::operator+=(BlockFrequency Freq) {
43   uint64_t Before = Freq.Frequency;
44   Frequency += Freq.Frequency;
45
46   // If overflow, set frequency to the maximum value.
47   if (Frequency < Before)
48     Frequency = UINT64_MAX;
49
50   return *this;
51 }
52
53 BlockFrequency BlockFrequency::operator+(BlockFrequency Freq) const {
54   BlockFrequency NewFreq(Frequency);
55   NewFreq += Freq;
56   return NewFreq;
57 }
58
59 BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
60   // Frequency can never be 0 by design.
61   assert(Frequency != 0);
62
63   // Shift right by count.
64   Frequency >>= count;
65
66   // Saturate to 1 if we are 0.
67   Frequency |= Frequency == 0;
68   return *this;
69 }