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/BranchProbability.h" 156122f3e6SDimitry Andric #include "llvm/Support/BlockFrequency.h" 166122f3e6SDimitry Andric #include "llvm/Support/raw_ostream.h" 176122f3e6SDimitry Andric #include <cassert> 186122f3e6SDimitry Andric 196122f3e6SDimitry Andric using namespace llvm; 206122f3e6SDimitry Andric 216122f3e6SDimitry Andric BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) { 22*91bc56edSDimitry Andric Frequency = Prob.scale(Frequency); 236122f3e6SDimitry Andric return *this; 246122f3e6SDimitry Andric } 256122f3e6SDimitry Andric 266122f3e6SDimitry Andric const BlockFrequency 276122f3e6SDimitry Andric BlockFrequency::operator*(const BranchProbability &Prob) const { 286122f3e6SDimitry Andric BlockFrequency Freq(Frequency); 296122f3e6SDimitry Andric Freq *= Prob; 306122f3e6SDimitry Andric return Freq; 316122f3e6SDimitry Andric } 326122f3e6SDimitry Andric 33f785676fSDimitry Andric BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) { 34*91bc56edSDimitry Andric Frequency = Prob.scaleByInverse(Frequency); 35f785676fSDimitry Andric return *this; 36f785676fSDimitry Andric } 37f785676fSDimitry Andric 38f785676fSDimitry Andric BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const { 39f785676fSDimitry Andric BlockFrequency Freq(Frequency); 40f785676fSDimitry Andric Freq /= Prob; 41f785676fSDimitry Andric return Freq; 42f785676fSDimitry Andric } 43f785676fSDimitry Andric 446122f3e6SDimitry Andric BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) { 456122f3e6SDimitry Andric uint64_t Before = Freq.Frequency; 466122f3e6SDimitry Andric Frequency += Freq.Frequency; 476122f3e6SDimitry Andric 486122f3e6SDimitry Andric // If overflow, set frequency to the maximum value. 496122f3e6SDimitry Andric if (Frequency < Before) 506122f3e6SDimitry Andric Frequency = UINT64_MAX; 516122f3e6SDimitry Andric 526122f3e6SDimitry Andric return *this; 536122f3e6SDimitry Andric } 546122f3e6SDimitry Andric 556122f3e6SDimitry Andric const BlockFrequency 566122f3e6SDimitry Andric BlockFrequency::operator+(const BlockFrequency &Prob) const { 576122f3e6SDimitry Andric BlockFrequency Freq(Frequency); 586122f3e6SDimitry Andric Freq += Prob; 596122f3e6SDimitry Andric return Freq; 606122f3e6SDimitry Andric } 616122f3e6SDimitry Andric 62*91bc56edSDimitry Andric BlockFrequency &BlockFrequency::operator>>=(const unsigned count) { 63*91bc56edSDimitry Andric // Frequency can never be 0 by design. 64*91bc56edSDimitry Andric assert(Frequency != 0); 65f785676fSDimitry Andric 66*91bc56edSDimitry Andric // Shift right by count. 67*91bc56edSDimitry Andric Frequency >>= count; 686122f3e6SDimitry Andric 69*91bc56edSDimitry Andric // Saturate to 1 if we are 0. 70*91bc56edSDimitry Andric Frequency |= Frequency == 0; 71*91bc56edSDimitry Andric return *this; 726122f3e6SDimitry Andric } 73