130815c53SDimitry Andric //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====// 230815c53SDimitry Andric // 3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 630815c53SDimitry Andric // 730815c53SDimitry Andric //===----------------------------------------------------------------------===// 830815c53SDimitry Andric // 930815c53SDimitry Andric // This file implements Block Frequency class. 1030815c53SDimitry Andric // 1130815c53SDimitry Andric //===----------------------------------------------------------------------===// 1230815c53SDimitry Andric 1330815c53SDimitry Andric #include "llvm/Support/BlockFrequency.h" 146f8fc217SDimitry Andric #include "llvm/Support/BranchProbability.h" 15b1c73532SDimitry Andric #include "llvm/Support/MathExtras.h" 16ac9a064cSDimitry Andric #include "llvm/Support/ScaledNumber.h" 17ac9a064cSDimitry Andric #include "llvm/Support/raw_ostream.h" 1830815c53SDimitry Andric 1930815c53SDimitry Andric using namespace llvm; 2030815c53SDimitry Andric operator *=(BranchProbability Prob)21dd58ef01SDimitry AndricBlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) { 225ca98fd9SDimitry Andric Frequency = Prob.scale(Frequency); 2330815c53SDimitry Andric return *this; 2430815c53SDimitry Andric } 2530815c53SDimitry Andric operator *(BranchProbability Prob) const26dd58ef01SDimitry AndricBlockFrequency BlockFrequency::operator*(BranchProbability Prob) const { 2730815c53SDimitry Andric BlockFrequency Freq(Frequency); 2830815c53SDimitry Andric Freq *= Prob; 2930815c53SDimitry Andric return Freq; 3030815c53SDimitry Andric } 3130815c53SDimitry Andric operator /=(BranchProbability Prob)32dd58ef01SDimitry AndricBlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) { 335ca98fd9SDimitry Andric Frequency = Prob.scaleByInverse(Frequency); 34f8af5cf6SDimitry Andric return *this; 35f8af5cf6SDimitry Andric } 36f8af5cf6SDimitry Andric operator /(BranchProbability Prob) const37dd58ef01SDimitry AndricBlockFrequency BlockFrequency::operator/(BranchProbability Prob) const { 38f8af5cf6SDimitry Andric BlockFrequency Freq(Frequency); 39f8af5cf6SDimitry Andric Freq /= Prob; 40f8af5cf6SDimitry Andric return Freq; 41f8af5cf6SDimitry Andric } 42b1c73532SDimitry Andric mul(uint64_t Factor) const43b1c73532SDimitry Andricstd::optional<BlockFrequency> BlockFrequency::mul(uint64_t Factor) const { 44b1c73532SDimitry Andric bool Overflow; 45b1c73532SDimitry Andric uint64_t ResultFrequency = SaturatingMultiply(Frequency, Factor, &Overflow); 46b1c73532SDimitry Andric if (Overflow) 47b1c73532SDimitry Andric return {}; 48b1c73532SDimitry Andric return BlockFrequency(ResultFrequency); 49b1c73532SDimitry Andric } 50ac9a064cSDimitry Andric printRelativeBlockFreq(raw_ostream & OS,BlockFrequency EntryFreq,BlockFrequency Freq)51ac9a064cSDimitry Andricvoid llvm::printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq, 52ac9a064cSDimitry Andric BlockFrequency Freq) { 53ac9a064cSDimitry Andric if (Freq == BlockFrequency(0)) { 54ac9a064cSDimitry Andric OS << "0"; 55ac9a064cSDimitry Andric return; 56ac9a064cSDimitry Andric } 57ac9a064cSDimitry Andric if (EntryFreq == BlockFrequency(0)) { 58ac9a064cSDimitry Andric OS << "<invalid BFI>"; 59ac9a064cSDimitry Andric return; 60ac9a064cSDimitry Andric } 61ac9a064cSDimitry Andric ScaledNumber<uint64_t> Block(Freq.getFrequency(), 0); 62ac9a064cSDimitry Andric ScaledNumber<uint64_t> Entry(EntryFreq.getFrequency(), 0); 63ac9a064cSDimitry Andric OS << Block / Entry; 64ac9a064cSDimitry Andric } 65