blob: e901fe6c2e65f04dbe45670ab4ec1300d87b05ca [file] [log] [blame]
Jakub Staszak49993f22011-07-25 22:24:51 +00001//====--------------- 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
Cong Houc536bd92015-09-10 23:10:42 +000021BlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) {
Duncan P. N. Exon Smith547183b2014-04-29 16:31:29 +000022 Frequency = Prob.scale(Frequency);
Jakub Staszak49993f22011-07-25 22:24:51 +000023 return *this;
24}
25
26const BlockFrequency
Cong Houc536bd92015-09-10 23:10:42 +000027BlockFrequency::operator*(BranchProbability Prob) const {
Jakub Staszak49993f22011-07-25 22:24:51 +000028 BlockFrequency Freq(Frequency);
29 Freq *= Prob;
30 return Freq;
31}
32
Cong Houc536bd92015-09-10 23:10:42 +000033BlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) {
Duncan P. N. Exon Smith547183b2014-04-29 16:31:29 +000034 Frequency = Prob.scaleByInverse(Frequency);
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +000035 return *this;
36}
37
Cong Houc536bd92015-09-10 23:10:42 +000038BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const {
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +000039 BlockFrequency Freq(Frequency);
40 Freq /= Prob;
41 return Freq;
42}
43
Jakub Staszak49993f22011-07-25 22:24:51 +000044BlockFrequency &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
Michael Gottesman8f17dcc2013-12-14 02:24:22 +000062BlockFrequency &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}