16122f3e6SDimitry Andric //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====// 26122f3e6SDimitry Andric // 36122f3e6SDimitry Andric // The LLVM Compiler Infrastructure 46122f3e6SDimitry Andric // 56122f3e6SDimitry Andric // This file is distributed under the University of Illinois Open Source 66122f3e6SDimitry Andric // License. See LICENSE.TXT for details. 76122f3e6SDimitry Andric // 86122f3e6SDimitry Andric //===----------------------------------------------------------------------===// 96122f3e6SDimitry Andric // 106122f3e6SDimitry Andric // This file implements Block Frequency class. 116122f3e6SDimitry Andric // 126122f3e6SDimitry Andric //===----------------------------------------------------------------------===// 136122f3e6SDimitry Andric 146122f3e6SDimitry Andric #include "llvm/Support/BlockFrequency.h" 156122f3e6SDimitry Andric #include <cassert> 166122f3e6SDimitry Andric 176122f3e6SDimitry Andric using namespace llvm; 186122f3e6SDimitry Andric operator *=(BranchProbability Prob)19*7d523365SDimitry AndricBlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) { 2091bc56edSDimitry Andric Frequency = Prob.scale(Frequency); 216122f3e6SDimitry Andric return *this; 226122f3e6SDimitry Andric } 236122f3e6SDimitry Andric operator *(BranchProbability Prob) const24*7d523365SDimitry AndricBlockFrequency BlockFrequency::operator*(BranchProbability Prob) const { 256122f3e6SDimitry Andric BlockFrequency Freq(Frequency); 266122f3e6SDimitry Andric Freq *= Prob; 276122f3e6SDimitry Andric return Freq; 286122f3e6SDimitry Andric } 296122f3e6SDimitry Andric operator /=(BranchProbability Prob)30*7d523365SDimitry AndricBlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) { 3191bc56edSDimitry Andric Frequency = Prob.scaleByInverse(Frequency); 32f785676fSDimitry Andric return *this; 33f785676fSDimitry Andric } 34f785676fSDimitry Andric operator /(BranchProbability Prob) const35*7d523365SDimitry AndricBlockFrequency BlockFrequency::operator/(BranchProbability Prob) const { 36f785676fSDimitry Andric BlockFrequency Freq(Frequency); 37f785676fSDimitry Andric Freq /= Prob; 38f785676fSDimitry Andric return Freq; 39f785676fSDimitry Andric } 40f785676fSDimitry Andric operator +=(BlockFrequency Freq)41*7d523365SDimitry AndricBlockFrequency &BlockFrequency::operator+=(BlockFrequency Freq) { 426122f3e6SDimitry Andric uint64_t Before = Freq.Frequency; 436122f3e6SDimitry Andric Frequency += Freq.Frequency; 446122f3e6SDimitry Andric 456122f3e6SDimitry Andric // If overflow, set frequency to the maximum value. 466122f3e6SDimitry Andric if (Frequency < Before) 476122f3e6SDimitry Andric Frequency = UINT64_MAX; 486122f3e6SDimitry Andric 496122f3e6SDimitry Andric return *this; 506122f3e6SDimitry Andric } 516122f3e6SDimitry Andric operator +(BlockFrequency Freq) const52*7d523365SDimitry AndricBlockFrequency BlockFrequency::operator+(BlockFrequency Freq) const { 53*7d523365SDimitry Andric BlockFrequency NewFreq(Frequency); 54*7d523365SDimitry Andric NewFreq += Freq; 55*7d523365SDimitry Andric return NewFreq; 56*7d523365SDimitry Andric } 57*7d523365SDimitry Andric operator -=(BlockFrequency Freq)58*7d523365SDimitry AndricBlockFrequency &BlockFrequency::operator-=(BlockFrequency Freq) { 59*7d523365SDimitry Andric // If underflow, set frequency to 0. 60*7d523365SDimitry Andric if (Frequency <= Freq.Frequency) 61*7d523365SDimitry Andric Frequency = 0; 62*7d523365SDimitry Andric else 63*7d523365SDimitry Andric Frequency -= Freq.Frequency; 64*7d523365SDimitry Andric return *this; 65*7d523365SDimitry Andric } 66*7d523365SDimitry Andric operator -(BlockFrequency Freq) const67*7d523365SDimitry AndricBlockFrequency BlockFrequency::operator-(BlockFrequency Freq) const { 68*7d523365SDimitry Andric BlockFrequency NewFreq(Frequency); 69*7d523365SDimitry Andric NewFreq -= Freq; 70*7d523365SDimitry Andric return NewFreq; 716122f3e6SDimitry Andric } 726122f3e6SDimitry Andric operator >>=(const unsigned count)7391bc56edSDimitry AndricBlockFrequency &BlockFrequency::operator>>=(const unsigned count) { 7491bc56edSDimitry Andric // Frequency can never be 0 by design. 7591bc56edSDimitry Andric assert(Frequency != 0); 76f785676fSDimitry Andric 7791bc56edSDimitry Andric // Shift right by count. 7891bc56edSDimitry Andric Frequency >>= count; 796122f3e6SDimitry Andric 8091bc56edSDimitry Andric // Saturate to 1 if we are 0. 8191bc56edSDimitry Andric Frequency |= Frequency == 0; 8291bc56edSDimitry Andric return *this; 836122f3e6SDimitry Andric } 84