1*0b57cec5SDimitry Andric //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements Block Frequency class.
10*0b57cec5SDimitry Andric //
11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*0b57cec5SDimitry Andric 
13*0b57cec5SDimitry Andric #include "llvm/Support/BlockFrequency.h"
14*0b57cec5SDimitry Andric #include <cassert>
15*0b57cec5SDimitry Andric 
16*0b57cec5SDimitry Andric using namespace llvm;
17*0b57cec5SDimitry Andric 
operator *=(BranchProbability Prob)18*0b57cec5SDimitry Andric BlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) {
19*0b57cec5SDimitry Andric   Frequency = Prob.scale(Frequency);
20*0b57cec5SDimitry Andric   return *this;
21*0b57cec5SDimitry Andric }
22*0b57cec5SDimitry Andric 
operator *(BranchProbability Prob) const23*0b57cec5SDimitry Andric BlockFrequency BlockFrequency::operator*(BranchProbability Prob) const {
24*0b57cec5SDimitry Andric   BlockFrequency Freq(Frequency);
25*0b57cec5SDimitry Andric   Freq *= Prob;
26*0b57cec5SDimitry Andric   return Freq;
27*0b57cec5SDimitry Andric }
28*0b57cec5SDimitry Andric 
operator /=(BranchProbability Prob)29*0b57cec5SDimitry Andric BlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) {
30*0b57cec5SDimitry Andric   Frequency = Prob.scaleByInverse(Frequency);
31*0b57cec5SDimitry Andric   return *this;
32*0b57cec5SDimitry Andric }
33*0b57cec5SDimitry Andric 
operator /(BranchProbability Prob) const34*0b57cec5SDimitry Andric BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const {
35*0b57cec5SDimitry Andric   BlockFrequency Freq(Frequency);
36*0b57cec5SDimitry Andric   Freq /= Prob;
37*0b57cec5SDimitry Andric   return Freq;
38*0b57cec5SDimitry Andric }
39*0b57cec5SDimitry Andric 
operator +=(BlockFrequency Freq)40*0b57cec5SDimitry Andric BlockFrequency &BlockFrequency::operator+=(BlockFrequency Freq) {
41*0b57cec5SDimitry Andric   uint64_t Before = Freq.Frequency;
42*0b57cec5SDimitry Andric   Frequency += Freq.Frequency;
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric   // If overflow, set frequency to the maximum value.
45*0b57cec5SDimitry Andric   if (Frequency < Before)
46*0b57cec5SDimitry Andric     Frequency = UINT64_MAX;
47*0b57cec5SDimitry Andric 
48*0b57cec5SDimitry Andric   return *this;
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric 
operator +(BlockFrequency Freq) const51*0b57cec5SDimitry Andric BlockFrequency BlockFrequency::operator+(BlockFrequency Freq) const {
52*0b57cec5SDimitry Andric   BlockFrequency NewFreq(Frequency);
53*0b57cec5SDimitry Andric   NewFreq += Freq;
54*0b57cec5SDimitry Andric   return NewFreq;
55*0b57cec5SDimitry Andric }
56*0b57cec5SDimitry Andric 
operator -=(BlockFrequency Freq)57*0b57cec5SDimitry Andric BlockFrequency &BlockFrequency::operator-=(BlockFrequency Freq) {
58*0b57cec5SDimitry Andric   // If underflow, set frequency to 0.
59*0b57cec5SDimitry Andric   if (Frequency <= Freq.Frequency)
60*0b57cec5SDimitry Andric     Frequency = 0;
61*0b57cec5SDimitry Andric   else
62*0b57cec5SDimitry Andric     Frequency -= Freq.Frequency;
63*0b57cec5SDimitry Andric   return *this;
64*0b57cec5SDimitry Andric }
65*0b57cec5SDimitry Andric 
operator -(BlockFrequency Freq) const66*0b57cec5SDimitry Andric BlockFrequency BlockFrequency::operator-(BlockFrequency Freq) const {
67*0b57cec5SDimitry Andric   BlockFrequency NewFreq(Frequency);
68*0b57cec5SDimitry Andric   NewFreq -= Freq;
69*0b57cec5SDimitry Andric   return NewFreq;
70*0b57cec5SDimitry Andric }
71*0b57cec5SDimitry Andric 
operator >>=(const unsigned count)72*0b57cec5SDimitry Andric BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
73*0b57cec5SDimitry Andric   // Frequency can never be 0 by design.
74*0b57cec5SDimitry Andric   assert(Frequency != 0);
75*0b57cec5SDimitry Andric 
76*0b57cec5SDimitry Andric   // Shift right by count.
77*0b57cec5SDimitry Andric   Frequency >>= count;
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric   // Saturate to 1 if we are 0.
80*0b57cec5SDimitry Andric   Frequency |= Frequency == 0;
81*0b57cec5SDimitry Andric   return *this;
82*0b57cec5SDimitry Andric }
83