xref: /minix3/external/bsd/llvm/dist/llvm/lib/Support/BlockFrequency.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements Block Frequency class.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "llvm/Support/BranchProbability.h"
15f4a2713aSLionel Sambuc #include "llvm/Support/BlockFrequency.h"
16f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
17f4a2713aSLionel Sambuc #include <cassert>
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc using namespace llvm;
20f4a2713aSLionel Sambuc 
operator *=(const BranchProbability & Prob)21f4a2713aSLionel Sambuc BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
22*0a6a1f1dSLionel Sambuc   Frequency = Prob.scale(Frequency);
23f4a2713aSLionel Sambuc   return *this;
24f4a2713aSLionel Sambuc }
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc const BlockFrequency
operator *(const BranchProbability & Prob) const27f4a2713aSLionel Sambuc BlockFrequency::operator*(const BranchProbability &Prob) const {
28f4a2713aSLionel Sambuc   BlockFrequency Freq(Frequency);
29f4a2713aSLionel Sambuc   Freq *= Prob;
30f4a2713aSLionel Sambuc   return Freq;
31f4a2713aSLionel Sambuc }
32f4a2713aSLionel Sambuc 
operator /=(const BranchProbability & Prob)33f4a2713aSLionel Sambuc BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) {
34*0a6a1f1dSLionel Sambuc   Frequency = Prob.scaleByInverse(Frequency);
35f4a2713aSLionel Sambuc   return *this;
36f4a2713aSLionel Sambuc }
37f4a2713aSLionel Sambuc 
operator /(const BranchProbability & Prob) const38f4a2713aSLionel Sambuc BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const {
39f4a2713aSLionel Sambuc   BlockFrequency Freq(Frequency);
40f4a2713aSLionel Sambuc   Freq /= Prob;
41f4a2713aSLionel Sambuc   return Freq;
42f4a2713aSLionel Sambuc }
43f4a2713aSLionel Sambuc 
operator +=(const BlockFrequency & Freq)44f4a2713aSLionel Sambuc BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
45f4a2713aSLionel Sambuc   uint64_t Before = Freq.Frequency;
46f4a2713aSLionel Sambuc   Frequency += Freq.Frequency;
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc   // If overflow, set frequency to the maximum value.
49f4a2713aSLionel Sambuc   if (Frequency < Before)
50f4a2713aSLionel Sambuc     Frequency = UINT64_MAX;
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc   return *this;
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc 
55f4a2713aSLionel Sambuc const BlockFrequency
operator +(const BlockFrequency & Prob) const56f4a2713aSLionel Sambuc BlockFrequency::operator+(const BlockFrequency &Prob) const {
57f4a2713aSLionel Sambuc   BlockFrequency Freq(Frequency);
58f4a2713aSLionel Sambuc   Freq += Prob;
59f4a2713aSLionel Sambuc   return Freq;
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc 
operator >>=(const unsigned count)62*0a6a1f1dSLionel Sambuc BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
63*0a6a1f1dSLionel Sambuc   // Frequency can never be 0 by design.
64*0a6a1f1dSLionel Sambuc   assert(Frequency != 0);
65f4a2713aSLionel Sambuc 
66*0a6a1f1dSLionel Sambuc   // Shift right by count.
67*0a6a1f1dSLionel Sambuc   Frequency >>= count;
68f4a2713aSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc   // Saturate to 1 if we are 0.
70*0a6a1f1dSLionel Sambuc   Frequency |= Frequency == 0;
71*0a6a1f1dSLionel Sambuc   return *this;
72f4a2713aSLionel Sambuc }
73