10b57cec5SDimitry Andric //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements Block Frequency class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/Support/BlockFrequency.h"
1404eeddc0SDimitry Andric #include "llvm/Support/BranchProbability.h"
15*c9157d92SDimitry Andric #include "llvm/Support/MathExtras.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric using namespace llvm;
180b57cec5SDimitry Andric 
operator *=(BranchProbability Prob)190b57cec5SDimitry Andric BlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) {
200b57cec5SDimitry Andric   Frequency = Prob.scale(Frequency);
210b57cec5SDimitry Andric   return *this;
220b57cec5SDimitry Andric }
230b57cec5SDimitry Andric 
operator *(BranchProbability Prob) const240b57cec5SDimitry Andric BlockFrequency BlockFrequency::operator*(BranchProbability Prob) const {
250b57cec5SDimitry Andric   BlockFrequency Freq(Frequency);
260b57cec5SDimitry Andric   Freq *= Prob;
270b57cec5SDimitry Andric   return Freq;
280b57cec5SDimitry Andric }
290b57cec5SDimitry Andric 
operator /=(BranchProbability Prob)300b57cec5SDimitry Andric BlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) {
310b57cec5SDimitry Andric   Frequency = Prob.scaleByInverse(Frequency);
320b57cec5SDimitry Andric   return *this;
330b57cec5SDimitry Andric }
340b57cec5SDimitry Andric 
operator /(BranchProbability Prob) const350b57cec5SDimitry Andric BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const {
360b57cec5SDimitry Andric   BlockFrequency Freq(Frequency);
370b57cec5SDimitry Andric   Freq /= Prob;
380b57cec5SDimitry Andric   return Freq;
390b57cec5SDimitry Andric }
40*c9157d92SDimitry Andric 
mul(uint64_t Factor) const41*c9157d92SDimitry Andric std::optional<BlockFrequency> BlockFrequency::mul(uint64_t Factor) const {
42*c9157d92SDimitry Andric   bool Overflow;
43*c9157d92SDimitry Andric   uint64_t ResultFrequency = SaturatingMultiply(Frequency, Factor, &Overflow);
44*c9157d92SDimitry Andric   if (Overflow)
45*c9157d92SDimitry Andric     return {};
46*c9157d92SDimitry Andric   return BlockFrequency(ResultFrequency);
47*c9157d92SDimitry Andric }
48