1 //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements Block Frequency class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Support/BlockFrequency.h" 14 #include "llvm/Support/BranchProbability.h" 15 #include "llvm/Support/MathExtras.h" 16 #include "llvm/Support/ScaledNumber.h" 17 #include "llvm/Support/raw_ostream.h" 18 19 using namespace llvm; 20 21 BlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) { 22 Frequency = Prob.scale(Frequency); 23 return *this; 24 } 25 26 BlockFrequency BlockFrequency::operator*(BranchProbability Prob) const { 27 BlockFrequency Freq(Frequency); 28 Freq *= Prob; 29 return Freq; 30 } 31 32 BlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) { 33 Frequency = Prob.scaleByInverse(Frequency); 34 return *this; 35 } 36 37 BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const { 38 BlockFrequency Freq(Frequency); 39 Freq /= Prob; 40 return Freq; 41 } 42 43 std::optional<BlockFrequency> BlockFrequency::mul(uint64_t Factor) const { 44 bool Overflow; 45 uint64_t ResultFrequency = SaturatingMultiply(Frequency, Factor, &Overflow); 46 if (Overflow) 47 return {}; 48 return BlockFrequency(ResultFrequency); 49 } 50 51 void llvm::printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq, 52 BlockFrequency Freq) { 53 if (Freq == BlockFrequency(0)) { 54 OS << "0"; 55 return; 56 } 57 if (EntryFreq == BlockFrequency(0)) { 58 OS << "<invalid BFI>"; 59 return; 60 } 61 ScaledNumber<uint64_t> Block(Freq.getFrequency(), 0); 62 ScaledNumber<uint64_t> Entry(EntryFreq.getFrequency(), 0); 63 OS << Block / Entry; 64 } 65