1//====--------------- lib/Support/BlockFrequency.cpp -----------*- C++ -*-====//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Block Frequency class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Support/BranchProbability.h"
15#include "llvm/Support/BlockFrequency.h"
16#include "llvm/Support/raw_ostream.h"
17#include <cassert>
18
19using namespace llvm;
20
21BlockFrequency &BlockFrequency::operator*=(const BranchProbability &Prob) {
22  Frequency = Prob.scale(Frequency);
23  return *this;
24}
25
26const BlockFrequency
27BlockFrequency::operator*(const BranchProbability &Prob) const {
28  BlockFrequency Freq(Frequency);
29  Freq *= Prob;
30  return Freq;
31}
32
33BlockFrequency &BlockFrequency::operator/=(const BranchProbability &Prob) {
34  Frequency = Prob.scaleByInverse(Frequency);
35  return *this;
36}
37
38BlockFrequency BlockFrequency::operator/(const BranchProbability &Prob) const {
39  BlockFrequency Freq(Frequency);
40  Freq /= Prob;
41  return Freq;
42}
43
44BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
45  uint64_t Before = Freq.Frequency;
46  Frequency += Freq.Frequency;
47
48  // If overflow, set frequency to the maximum value.
49  if (Frequency < Before)
50    Frequency = UINT64_MAX;
51
52  return *this;
53}
54
55const BlockFrequency
56BlockFrequency::operator+(const BlockFrequency &Prob) const {
57  BlockFrequency Freq(Frequency);
58  Freq += Prob;
59  return Freq;
60}
61
62BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
63  // Frequency can never be 0 by design.
64  assert(Frequency != 0);
65
66  // Shift right by count.
67  Frequency >>= count;
68
69  // Saturate to 1 if we are 0.
70  Frequency |= Frequency == 0;
71  return *this;
72}
73