LLVM 18.0.0git
BlockFrequency.h
Go to the documentation of this file.
1//===-------- BlockFrequency.h - Block Frequency Wrapper --------*- C++ -*-===//
2//
3// 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
6//
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 <cassert>
17#include <cstdint>
18#include <optional>
19
20namespace llvm {
21
22class BranchProbability;
23
24// This class represents Block Frequency as a 64-bit value.
26 uint64_t Frequency;
27
28public:
29 BlockFrequency(uint64_t Freq = 0) : Frequency(Freq) { }
30
31 /// Returns the maximum possible frequency, the saturation value.
32 static uint64_t getMaxFrequency() { return UINT64_MAX; }
33
34 /// Returns the frequency as a fixpoint number scaled by the entry
35 /// frequency.
36 uint64_t getFrequency() const { return Frequency; }
37
38 /// Multiplies with a branch probability. The computation will never
39 /// overflow.
42
43 /// Divide by a non-zero branch probability using saturating
44 /// arithmetic.
47
48 /// Adds another block frequency using saturating arithmetic.
50 uint64_t Before = Freq.Frequency;
51 Frequency += Freq.Frequency;
52
53 // If overflow, set frequency to the maximum value.
54 if (Frequency < Before)
55 Frequency = UINT64_MAX;
56
57 return *this;
58 }
60 BlockFrequency NewFreq(Frequency);
61 NewFreq += Freq;
62 return NewFreq;
63 }
64
65 /// Subtracts another block frequency using saturating arithmetic.
67 // If underflow, set frequency to 0.
68 if (Frequency <= Freq.Frequency)
69 Frequency = 0;
70 else
71 Frequency -= Freq.Frequency;
72 return *this;
73 }
75 BlockFrequency NewFreq(Frequency);
76 NewFreq -= Freq;
77 return NewFreq;
78 }
79
80 /// Multiplies frequency with `Factor`. Returns `nullopt` in case of overflow.
81 std::optional<BlockFrequency> mul(uint64_t Factor) const;
82
83 /// Shift block frequency to the right by count digits saturating to 1.
84 BlockFrequency &operator>>=(const unsigned count) {
85 // Frequency can never be 0 by design.
86 assert(Frequency != 0);
87
88 // Shift right by count.
89 Frequency >>= count;
90
91 // Saturate to 1 if we are 0.
92 Frequency |= Frequency == 0;
93 return *this;
94 }
95
97 return Frequency < RHS.Frequency;
98 }
99
101 return Frequency <= RHS.Frequency;
102 }
103
105 return Frequency > RHS.Frequency;
106 }
107
109 return Frequency >= RHS.Frequency;
110 }
111
113 return Frequency == RHS.Frequency;
114 }
115};
116
117} // namespace llvm
118
119#endif
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * RHS
BlockFrequency operator+(BlockFrequency Freq) const
BlockFrequency operator/(BranchProbability Prob) const
bool operator>=(BlockFrequency RHS) const
BlockFrequency & operator-=(BlockFrequency Freq)
Subtracts another block frequency using saturating arithmetic.
BlockFrequency(uint64_t Freq=0)
std::optional< BlockFrequency > mul(uint64_t Factor) const
Multiplies frequency with Factor. Returns nullopt in case of overflow.
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
bool operator<(BlockFrequency RHS) const
bool operator==(BlockFrequency RHS) const
BlockFrequency & operator+=(BlockFrequency Freq)
Adds another block frequency using saturating arithmetic.
BlockFrequency operator*(BranchProbability Prob) const
static uint64_t getMaxFrequency()
Returns the maximum possible frequency, the saturation value.
bool operator>(BlockFrequency RHS) const
bool operator<=(BlockFrequency RHS) const
BlockFrequency & operator>>=(const unsigned count)
Shift block frequency to the right by count digits saturating to 1.
BlockFrequency & operator*=(BranchProbability Prob)
Multiplies with a branch probability.
BlockFrequency operator-(BlockFrequency Freq) const
BlockFrequency & operator/=(BranchProbability Prob)
Divide by a non-zero branch probability using saturating arithmetic.
#define UINT64_MAX
Definition: DataTypes.h:77
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:1919