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-=(BlockFrequency Freq) { 60 // If underflow, set frequency to 0. 61 if (Frequency <= Freq.Frequency) 62 Frequency = 0; 63 else 64 Frequency -= Freq.Frequency; 65 return *this; 66 } 67 68 BlockFrequency BlockFrequency::operator-(BlockFrequency Freq) const { 69 BlockFrequency NewFreq(Frequency); 70 NewFreq -= Freq; 71 return NewFreq; 72 } 73 74 BlockFrequency &BlockFrequency::operator>>=(const unsigned count) { 75 // Frequency can never be 0 by design. 76 assert(Frequency != 0); 77 78 // Shift right by count. 79 Frequency >>= count; 80 81 // Saturate to 1 if we are 0. 82 Frequency |= Frequency == 0; 83 return *this; 84 } 85