1 //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements Block Frequency class.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/BlockFrequency.h"
18 BlockFrequency
&BlockFrequency::operator*=(BranchProbability Prob
) {
19 Frequency
= Prob
.scale(Frequency
);
23 BlockFrequency
BlockFrequency::operator*(BranchProbability Prob
) const {
24 BlockFrequency
Freq(Frequency
);
29 BlockFrequency
&BlockFrequency::operator/=(BranchProbability Prob
) {
30 Frequency
= Prob
.scaleByInverse(Frequency
);
34 BlockFrequency
BlockFrequency::operator/(BranchProbability Prob
) const {
35 BlockFrequency
Freq(Frequency
);
40 BlockFrequency
&BlockFrequency::operator+=(BlockFrequency Freq
) {
41 uint64_t Before
= Freq
.Frequency
;
42 Frequency
+= Freq
.Frequency
;
44 // If overflow, set frequency to the maximum value.
45 if (Frequency
< Before
)
46 Frequency
= UINT64_MAX
;
51 BlockFrequency
BlockFrequency::operator+(BlockFrequency Freq
) const {
52 BlockFrequency
NewFreq(Frequency
);
57 BlockFrequency
&BlockFrequency::operator-=(BlockFrequency Freq
) {
58 // If underflow, set frequency to 0.
59 if (Frequency
<= Freq
.Frequency
)
62 Frequency
-= Freq
.Frequency
;
66 BlockFrequency
BlockFrequency::operator-(BlockFrequency Freq
) const {
67 BlockFrequency
NewFreq(Frequency
);
72 BlockFrequency
&BlockFrequency::operator>>=(const unsigned count
) {
73 // Frequency can never be 0 by design.
74 assert(Frequency
!= 0);
76 // Shift right by count.
79 // Saturate to 1 if we are 0.
80 Frequency
|= Frequency
== 0;