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