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 operator *=(const BranchProbability & Prob)21BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) { 22 Frequency = Prob.scale(Frequency); 23 return *this; 24 } 25 26 const BlockFrequency operator *(const BranchProbability & Prob) const27BlockFrequency::operator*(const BranchProbability &Prob) const { 28 BlockFrequency Freq(Frequency); 29 Freq *= Prob; 30 return Freq; 31 } 32 operator /=(const BranchProbability & Prob)33BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) { 34 Frequency = Prob.scaleByInverse(Frequency); 35 return *this; 36 } 37 operator /(const BranchProbability & Prob) const38BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const { 39 BlockFrequency Freq(Frequency); 40 Freq /= Prob; 41 return Freq; 42 } 43 operator +=(const BlockFrequency & Freq)44BlockFrequency &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 operator +(const BlockFrequency & Prob) const56BlockFrequency::operator+(const BlockFrequency &Prob) const { 57 BlockFrequency Freq(Frequency); 58 Freq += Prob; 59 return Freq; 60 } 61 operator >>=(const unsigned count)62BlockFrequency &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