LLVM 20.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 raw_ostream;
23class BranchProbability;
24
25// This class represents Block Frequency as a 64-bit value.
27 uint64_t Frequency;
28
29public:
30 BlockFrequency() : Frequency(0) {}
31 explicit BlockFrequency(uint64_t Freq) : Frequency(Freq) {}
32
33 /// Returns the maximum possible frequency, the saturation value.
35
36 /// Returns the frequency as a fixpoint number scaled by the entry
37 /// frequency.
38 uint64_t getFrequency() const { return Frequency; }
39
40 /// Multiplies with a branch probability. The computation will never
41 /// overflow.
44
45 /// Divide by a non-zero branch probability using saturating
46 /// arithmetic.
49
50 /// Adds another block frequency using saturating arithmetic.
52 uint64_t Before = Freq.Frequency;
53 Frequency += Freq.Frequency;
54
55 // If overflow, set frequency to the maximum value.
56 if (Frequency < Before)
57 Frequency = UINT64_MAX;
58
59 return *this;
60 }
62 BlockFrequency NewFreq(Frequency);
63 NewFreq += Freq;
64 return NewFreq;
65 }
66
67 /// Subtracts another block frequency using saturating arithmetic.
69 // If underflow, set frequency to 0.
70 if (Frequency <= Freq.Frequency)
71 Frequency = 0;
72 else
73 Frequency -= Freq.Frequency;
74 return *this;
75 }
77 BlockFrequency NewFreq(Frequency);
78 NewFreq -= Freq;
79 return NewFreq;
80 }
81
82 /// Multiplies frequency with `Factor`. Returns `nullopt` in case of overflow.
83 std::optional<BlockFrequency> mul(uint64_t Factor) const;
84
85 /// Shift block frequency to the right by count digits saturating to 1.
86 BlockFrequency &operator>>=(const unsigned count) {
87 // Frequency can never be 0 by design.
88 assert(Frequency != 0);
89
90 // Shift right by count.
91 Frequency >>= count;
92
93 // Saturate to 1 if we are 0.
94 Frequency |= Frequency == 0;
95 return *this;
96 }
97
99 return Frequency < RHS.Frequency;
100 }
101
103 return Frequency <= RHS.Frequency;
104 }
105
107 return Frequency > RHS.Frequency;
108 }
109
111 return Frequency >= RHS.Frequency;
112 }
113
115 return Frequency == RHS.Frequency;
116 }
117
119 return Frequency != RHS.Frequency;
120 }
121};
122
123void printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq,
124 BlockFrequency Freq);
125
126} // namespace llvm
127
128#endif
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Value * RHS
bool operator!=(BlockFrequency RHS) const
BlockFrequency operator+(BlockFrequency Freq) const
BlockFrequency operator/(BranchProbability Prob) const
bool operator>=(BlockFrequency RHS) const
BlockFrequency(uint64_t Freq)
static BlockFrequency max()
Returns the maximum possible frequency, the saturation value.
BlockFrequency & operator-=(BlockFrequency Freq)
Subtracts another block frequency using saturating arithmetic.
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
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:1921
void printRelativeBlockFreq(raw_ostream &OS, BlockFrequency EntryFreq, BlockFrequency Freq)