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