1 //====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements Block Frequency class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/BlockFrequency.h"
15 #include "llvm/Support/raw_ostream.h"
20 BlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) {
21 Frequency = Prob.scale(Frequency);
25 BlockFrequency BlockFrequency::operator*(BranchProbability Prob) const {
26 BlockFrequency Freq(Frequency);
31 BlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) {
32 Frequency = Prob.scaleByInverse(Frequency);
36 BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const {
37 BlockFrequency Freq(Frequency);
42 BlockFrequency &BlockFrequency::operator+=(BlockFrequency Freq) {
43 uint64_t Before = Freq.Frequency;
44 Frequency += Freq.Frequency;
46 // If overflow, set frequency to the maximum value.
47 if (Frequency < Before)
48 Frequency = UINT64_MAX;
53 BlockFrequency BlockFrequency::operator+(BlockFrequency Freq) const {
54 BlockFrequency NewFreq(Frequency);
59 BlockFrequency &BlockFrequency::operator-=(BlockFrequency Freq) {
60 // If underflow, set frequency to 0.
61 if (Frequency <= Freq.Frequency)
64 Frequency -= Freq.Frequency;
68 BlockFrequency BlockFrequency::operator-(BlockFrequency Freq) const {
69 BlockFrequency NewFreq(Frequency);
74 BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
75 // Frequency can never be 0 by design.
76 assert(Frequency != 0);
78 // Shift right by count.
81 // Saturate to 1 if we are 0.
82 Frequency |= Frequency == 0;