xref: /freebsd/contrib/llvm-project/llvm/lib/Support/BlockFrequency.cpp (revision 53120fbb68952b7d620c2c0e1cf05c5017fc1b27)
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 
17 using namespace llvm;
18 
19 BlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) {
20   Frequency = Prob.scale(Frequency);
21   return *this;
22 }
23 
24 BlockFrequency BlockFrequency::operator*(BranchProbability Prob) const {
25   BlockFrequency Freq(Frequency);
26   Freq *= Prob;
27   return Freq;
28 }
29 
30 BlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) {
31   Frequency = Prob.scaleByInverse(Frequency);
32   return *this;
33 }
34 
35 BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const {
36   BlockFrequency Freq(Frequency);
37   Freq /= Prob;
38   return Freq;
39 }
40 
41 std::optional<BlockFrequency> BlockFrequency::mul(uint64_t Factor) const {
42   bool Overflow;
43   uint64_t ResultFrequency = SaturatingMultiply(Frequency, Factor, &Overflow);
44   if (Overflow)
45     return {};
46   return BlockFrequency(ResultFrequency);
47 }
48