blob: 18fb60e1904b3558b09836286b37644b90b1617c [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-------- BlockFrequency.h - Block Frequency Wrapper --------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements Block Frequency class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_SUPPORT_BLOCKFREQUENCY_H
14#define LLVM_SUPPORT_BLOCKFREQUENCY_H
15
16#include "llvm/Support/BranchProbability.h"
17#include "llvm/Support/DataTypes.h"
18
19namespace llvm {
20
21class raw_ostream;
22
23// This class represents Block Frequency as a 64-bit value.
24class BlockFrequency {
25 uint64_t Frequency;
26
27public:
28 BlockFrequency(uint64_t Freq = 0) : Frequency(Freq) { }
29
Andrew Scullcdfcccc2018-10-05 20:58:37 +010030 /// Returns the maximum possible frequency, the saturation value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031 static uint64_t getMaxFrequency() { return -1ULL; }
32
Andrew Scullcdfcccc2018-10-05 20:58:37 +010033 /// Returns the frequency as a fixpoint number scaled by the entry
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034 /// frequency.
35 uint64_t getFrequency() const { return Frequency; }
36
Andrew Scullcdfcccc2018-10-05 20:58:37 +010037 /// Multiplies with a branch probability. The computation will never
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010038 /// overflow.
39 BlockFrequency &operator*=(BranchProbability Prob);
40 BlockFrequency operator*(BranchProbability Prob) const;
41
Andrew Scullcdfcccc2018-10-05 20:58:37 +010042 /// Divide by a non-zero branch probability using saturating
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010043 /// arithmetic.
44 BlockFrequency &operator/=(BranchProbability Prob);
45 BlockFrequency operator/(BranchProbability Prob) const;
46
Andrew Scullcdfcccc2018-10-05 20:58:37 +010047 /// Adds another block frequency using saturating arithmetic.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048 BlockFrequency &operator+=(BlockFrequency Freq);
49 BlockFrequency operator+(BlockFrequency Freq) const;
50
Andrew Scullcdfcccc2018-10-05 20:58:37 +010051 /// Subtracts another block frequency using saturating arithmetic.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010052 BlockFrequency &operator-=(BlockFrequency Freq);
53 BlockFrequency operator-(BlockFrequency Freq) const;
54
Andrew Scullcdfcccc2018-10-05 20:58:37 +010055 /// Shift block frequency to the right by count digits saturating to 1.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010056 BlockFrequency &operator>>=(const unsigned count);
57
58 bool operator<(BlockFrequency RHS) const {
59 return Frequency < RHS.Frequency;
60 }
61
62 bool operator<=(BlockFrequency RHS) const {
63 return Frequency <= RHS.Frequency;
64 }
65
66 bool operator>(BlockFrequency RHS) const {
67 return Frequency > RHS.Frequency;
68 }
69
70 bool operator>=(BlockFrequency RHS) const {
71 return Frequency >= RHS.Frequency;
72 }
73
74 bool operator==(BlockFrequency RHS) const {
75 return Frequency == RHS.Frequency;
76 }
77};
78
79}
80
81#endif