blob: dbd790cec3a869ec2df3cc936660f4af31133e84 [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
Jakub Staszak49993f22011-07-25 22:24:51 +000014#include "llvm/Support/BlockFrequency.h"
15#include "llvm/Support/raw_ostream.h"
16#include <cassert>
17
18using namespace llvm;
19
Cong Houc536bd92015-09-10 23:10:42 +000020BlockFrequency &BlockFrequency::operator*=(BranchProbability Prob) {
Duncan P. N. Exon Smith547183b2014-04-29 16:31:29 +000021 Frequency = Prob.scale(Frequency);
Jakub Staszak49993f22011-07-25 22:24:51 +000022 return *this;
23}
24
25const BlockFrequency
Cong Houc536bd92015-09-10 23:10:42 +000026BlockFrequency::operator*(BranchProbability Prob) const {
Jakub Staszak49993f22011-07-25 22:24:51 +000027 BlockFrequency Freq(Frequency);
28 Freq *= Prob;
29 return Freq;
30}
31
Cong Houc536bd92015-09-10 23:10:42 +000032BlockFrequency &BlockFrequency::operator/=(BranchProbability Prob) {
Duncan P. N. Exon Smith547183b2014-04-29 16:31:29 +000033 Frequency = Prob.scaleByInverse(Frequency);
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +000034 return *this;
35}
36
Cong Houc536bd92015-09-10 23:10:42 +000037BlockFrequency BlockFrequency::operator/(BranchProbability Prob) const {
Jakob Stoklund Olesenc506e5d2013-06-28 18:23:42 +000038 BlockFrequency Freq(Frequency);
39 Freq /= Prob;
40 return Freq;
41}
42
Jakub Staszak49993f22011-07-25 22:24:51 +000043BlockFrequency &BlockFrequency::operator+=(const BlockFrequency &Freq) {
44 uint64_t Before = Freq.Frequency;
45 Frequency += Freq.Frequency;
46
47 // If overflow, set frequency to the maximum value.
48 if (Frequency < Before)
49 Frequency = UINT64_MAX;
50
51 return *this;
52}
53
54const BlockFrequency
55BlockFrequency::operator+(const BlockFrequency &Prob) const {
56 BlockFrequency Freq(Frequency);
57 Freq += Prob;
58 return Freq;
59}
60
Michael Gottesman8f17dcc2013-12-14 02:24:22 +000061BlockFrequency &BlockFrequency::operator>>=(const unsigned count) {
62 // Frequency can never be 0 by design.
63 assert(Frequency != 0);
64
65 // Shift right by count.
66 Frequency >>= count;
67
68 // Saturate to 1 if we are 0.
69 Frequency |= Frequency == 0;
70 return *this;
71}