[clang][bytecode] Fix reporting failed local constexpr initializers (#123588)
[llvm-project.git] / llvm / lib / Support / BlockFrequency.cpp
blob7d5498e7cb9974ba0562405434425d19c4670f47
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.
11 //===----------------------------------------------------------------------===//
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"
19 using namespace llvm;
21 BlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) {
22 Frequency = Prob.scale(Frequency);
23 return *this;
26 BlockFrequency BlockFrequency::operator*(BranchProbability Prob) const {
27 BlockFrequency Freq(Frequency);
28 Freq *= Prob;
29 return Freq;
32 BlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) {
33 Frequency = Prob.scaleByInverse(Frequency);
34 return *this;
37 BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const {
38 BlockFrequency Freq(Frequency);
39 Freq /= Prob;
40 return Freq;
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);
51 void llvm::printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq,
52 BlockFrequency Freq) {
53 if (Freq == BlockFrequency(0)) {
54 OS << "0";
55 return;
57 if (EntryFreq == BlockFrequency(0)) {
58 OS << "<invalid BFI>";
59 return;
61 ScaledNumber<uint64_t> Block(Freq.getFrequency(), 0);
62 ScaledNumber<uint64_t> Entry(EntryFreq.getFrequency(), 0);
63 OS << Block / Entry;