LLVM 23.0.0git
BBAddrMap.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This file declares common types and utilities for basic-block address maps.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_OBJECT_BBADDRMAP_H
15#define LLVM_OBJECT_BBADDRMAP_H
16
20#include "llvm/Support/Error.h"
22
23namespace llvm {
24namespace object {
25
26// Struct representing the BBAddrMap for one function.
27struct BBAddrMap {
28
29 // Bitfield of optional features to control the extra information
30 // emitted/encoded in the section.
31 struct Features {
33 bool BBFreq : 1;
34 bool BrProb : 1;
35 bool MultiBBRange : 1;
36 bool OmitBBEntries : 1;
38 bool BBHash : 1;
39 bool PostLinkCfg : 1;
40
41 bool hasPGOAnalysis() const { return FuncEntryCount || BBFreq || BrProb; }
42
43 bool hasPGOAnalysisBBData() const { return BBFreq || BrProb; }
44
45 // Encodes to minimum bit width representation.
46 uint16_t encode() const {
47 return (static_cast<uint16_t>(FuncEntryCount) << 0) |
48 (static_cast<uint16_t>(BBFreq) << 1) |
49 (static_cast<uint16_t>(BrProb) << 2) |
50 (static_cast<uint16_t>(MultiBBRange) << 3) |
51 (static_cast<uint16_t>(OmitBBEntries) << 4) |
52 (static_cast<uint16_t>(CallsiteEndOffsets) << 5) |
53 (static_cast<uint16_t>(BBHash) << 6) |
54 (static_cast<uint16_t>(PostLinkCfg) << 7);
55 }
56
57 // Decodes from minimum bit width representation and validates no
58 // unnecessary bits are used.
60 Features Feat{
61 static_cast<bool>(Val & (1 << 0)), static_cast<bool>(Val & (1 << 1)),
62 static_cast<bool>(Val & (1 << 2)), static_cast<bool>(Val & (1 << 3)),
63 static_cast<bool>(Val & (1 << 4)), static_cast<bool>(Val & (1 << 5)),
64 static_cast<bool>(Val & (1 << 6)), static_cast<bool>(Val & (1 << 7))};
65 if (Feat.encode() != Val)
66 return createStringError(
67 std::error_code(), "invalid encoding for BBAddrMap::Features: 0x%x",
68 Val);
69 return Feat;
70 }
71
72 bool operator==(const Features &Other) const {
73 return std::tie(FuncEntryCount, BBFreq, BrProb, MultiBBRange,
75 std::tie(Other.FuncEntryCount, Other.BBFreq, Other.BrProb,
76 Other.MultiBBRange, Other.OmitBBEntries,
77 Other.CallsiteEndOffsets, Other.BBHash,
78 Other.PostLinkCfg);
79 }
80 };
81
82 // Struct representing the BBAddrMap information for one basic block.
83 struct BBEntry {
84 struct Metadata {
85 bool HasReturn : 1; // If this block ends with a return (or tail
86 // call).
87 bool HasTailCall : 1; // If this block ends with a tail call.
88 bool IsEHPad : 1; // If this is an exception handling block.
89 bool CanFallThrough : 1; // If this block can fall through to its next.
90 bool HasIndirectBranch : 1; // If this block ends with an indirect branch
91 // (branch via a register).
92
93 bool operator==(const Metadata &Other) const {
94 return HasReturn == Other.HasReturn &&
95 HasTailCall == Other.HasTailCall && IsEHPad == Other.IsEHPad &&
96 CanFallThrough == Other.CanFallThrough &&
97 HasIndirectBranch == Other.HasIndirectBranch;
98 }
99
100 // Encodes this struct as a uint32_t value.
101 uint32_t encode() const {
102 return static_cast<uint32_t>(HasReturn) |
103 (static_cast<uint32_t>(HasTailCall) << 1) |
104 (static_cast<uint32_t>(IsEHPad) << 2) |
105 (static_cast<uint32_t>(CanFallThrough) << 3) |
106 (static_cast<uint32_t>(HasIndirectBranch) << 4);
107 }
108
109 // Decodes and returns a Metadata struct from a uint32_t value.
111 Metadata MD{/*HasReturn=*/static_cast<bool>(V & 1),
112 /*HasTailCall=*/static_cast<bool>(V & (1 << 1)),
113 /*IsEHPad=*/static_cast<bool>(V & (1 << 2)),
114 /*CanFallThrough=*/static_cast<bool>(V & (1 << 3)),
115 /*HasIndirectBranch=*/static_cast<bool>(V & (1 << 4))};
116 if (MD.encode() != V)
117 return createStringError(
118 std::error_code(), "invalid encoding for BBEntry::Metadata: 0x%x",
119 V);
120 return MD;
121 }
122 };
123
124 uint32_t ID = 0; // Unique ID of this basic block.
125 uint32_t Offset = 0; // Offset of basic block relative to the base address.
126 uint32_t Size = 0; // Size of the basic block.
127 Metadata MD = {false, false, false, false,
128 false}; // Metadata for this basic block.
129 // Offsets of end of call instructions, relative to the basic block start.
131 uint64_t Hash = 0; // Hash for this basic block.
132
137
138 UniqueBBID getID() const { return {ID, 0}; }
139
140 bool operator==(const BBEntry &Other) const {
141 return ID == Other.ID && Offset == Other.Offset && Size == Other.Size &&
142 MD == Other.MD && CallsiteEndOffsets == Other.CallsiteEndOffsets &&
143 Hash == Other.Hash;
144 }
145
146 bool hasReturn() const { return MD.HasReturn; }
147 bool hasTailCall() const { return MD.HasTailCall; }
148 bool isEHPad() const { return MD.IsEHPad; }
149 bool canFallThrough() const { return MD.CanFallThrough; }
150 bool hasIndirectBranch() const { return MD.HasIndirectBranch; }
151 };
152
153 // Struct representing the BBAddrMap information for a contiguous range of
154 // basic blocks (a function or a basic block section).
156 uint64_t BaseAddress = 0; // Base address of the range.
157 std::vector<BBEntry> BBEntries; // Basic block entries for this range.
158
159 // Equality operator for unit testing.
160 bool operator==(const BBRangeEntry &Other) const {
161 return BaseAddress == Other.BaseAddress &&
162 std::equal(BBEntries.begin(), BBEntries.end(),
163 Other.BBEntries.begin());
164 }
165 };
166
167 // All ranges for this function. Cannot be empty. The first range always
168 // corresponds to the function entry.
169 std::vector<BBRangeEntry> BBRanges;
170
171 // Returns the function address associated with this BBAddrMap, which is
172 // stored as the `BaseAddress` of its first BBRangeEntry.
174 assert(!BBRanges.empty());
175 return BBRanges.front().BaseAddress;
176 }
177
178 // Returns the total number of bb entries in all bb ranges.
179 size_t getNumBBEntries() const {
180 size_t NumBBEntries = 0;
181 for (const auto &BBR : BBRanges)
182 NumBBEntries += BBR.BBEntries.size();
183 return NumBBEntries;
184 }
185
186 // Returns the index of the bb range with the given base address, or
187 // `std::nullopt` if no such range exists.
188 std::optional<size_t>
190 for (size_t I = 0; I < BBRanges.size(); ++I)
191 if (BBRanges[I].BaseAddress == BaseAddress)
192 return I;
193 return {};
194 }
195
196 // Returns bb entries in the first range.
197 const std::vector<BBEntry> &getBBEntries() const {
198 return BBRanges.front().BBEntries;
199 }
200
201 const std::vector<BBRangeEntry> &getBBRanges() const { return BBRanges; }
202
203 // Equality operator for unit testing.
204 bool operator==(const BBAddrMap &Other) const {
205 return std::equal(BBRanges.begin(), BBRanges.end(), Other.BBRanges.begin());
206 }
207};
208
209/// A feature extension of BBAddrMap that holds information relevant to PGO.
211 /// Extra basic block data with fields for block frequency and branch
212 /// probability.
213 struct PGOBBEntry {
214 /// Single successor of a given basic block that contains the tag and branch
215 /// probability associated with it.
217 /// Unique ID of this successor basic block.
219 /// Branch Probability of the edge to this successor taken from MBPI.
221 /// Raw edge count from the post link profile (e.g., from bolt or
222 /// propeller).
224
225 bool operator==(const SuccessorEntry &Other) const {
226 return std::tie(ID, Prob, PostLinkFreq) ==
227 std::tie(Other.ID, Other.Prob, Other.PostLinkFreq);
228 }
229 };
230
231 /// Block frequency taken from MBFI
233 /// Raw block count taken from the post link profile (e.g., from bolt or
234 /// propeller).
236 /// List of successors of the current block
238
239 bool operator==(const PGOBBEntry &Other) const {
240 return std::tie(BlockFreq, PostLinkBlockFreq, Successors) ==
241 std::tie(Other.BlockFreq, PostLinkBlockFreq, Other.Successors);
242 }
243 };
244
245 uint64_t FuncEntryCount; // Prof count from IR function
246 std::vector<PGOBBEntry> BBEntries; // Extended basic block entries
247
248 // Flags to indicate if each PGO related info was enabled in this function
250
251 bool operator==(const PGOAnalysisMap &Other) const {
252 return std::tie(FuncEntryCount, BBEntries, FeatEnable) ==
253 std::tie(Other.FuncEntryCount, Other.BBEntries, Other.FeatEnable);
254 }
255};
256
257} // end namespace object.
258} // end namespace llvm.
259
260#endif // LLVM_OBJECT_BBADDRMAP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define I(x, y, z)
Definition MD5.cpp:57
This file defines the SmallVector class.
Tagged union holding either a T or a Error.
Definition Error.h:485
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
@ Other
Any other memory.
Definition ModRef.h:68
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
static Expected< Metadata > decode(uint32_t V)
Definition BBAddrMap.h:110
bool operator==(const Metadata &Other) const
Definition BBAddrMap.h:93
BBEntry(uint32_t ID, uint32_t Offset, uint32_t Size, Metadata MD, SmallVector< uint32_t, 1 > CallsiteEndOffsets, uint64_t Hash)
Definition BBAddrMap.h:133
SmallVector< uint32_t, 1 > CallsiteEndOffsets
Definition BBAddrMap.h:130
bool operator==(const BBEntry &Other) const
Definition BBAddrMap.h:140
bool operator==(const BBRangeEntry &Other) const
Definition BBAddrMap.h:160
std::vector< BBEntry > BBEntries
Definition BBAddrMap.h:157
bool operator==(const Features &Other) const
Definition BBAddrMap.h:72
static Expected< Features > decode(uint16_t Val)
Definition BBAddrMap.h:59
const std::vector< BBRangeEntry > & getBBRanges() const
Definition BBAddrMap.h:201
std::vector< BBRangeEntry > BBRanges
Definition BBAddrMap.h:169
size_t getNumBBEntries() const
Definition BBAddrMap.h:179
bool operator==(const BBAddrMap &Other) const
Definition BBAddrMap.h:204
const std::vector< BBEntry > & getBBEntries() const
Definition BBAddrMap.h:197
uint64_t getFunctionAddress() const
Definition BBAddrMap.h:173
std::optional< size_t > getBBRangeIndexForBaseAddress(uint64_t BaseAddress) const
Definition BBAddrMap.h:189
Single successor of a given basic block that contains the tag and branch probability associated with ...
Definition BBAddrMap.h:216
uint32_t ID
Unique ID of this successor basic block.
Definition BBAddrMap.h:218
BranchProbability Prob
Branch Probability of the edge to this successor taken from MBPI.
Definition BBAddrMap.h:220
bool operator==(const SuccessorEntry &Other) const
Definition BBAddrMap.h:225
uint64_t PostLinkFreq
Raw edge count from the post link profile (e.g., from bolt or propeller).
Definition BBAddrMap.h:223
Extra basic block data with fields for block frequency and branch probability.
Definition BBAddrMap.h:213
bool operator==(const PGOBBEntry &Other) const
Definition BBAddrMap.h:239
uint64_t PostLinkBlockFreq
Raw block count taken from the post link profile (e.g., from bolt or propeller).
Definition BBAddrMap.h:235
llvm::SmallVector< SuccessorEntry, 2 > Successors
List of successors of the current block.
Definition BBAddrMap.h:237
BlockFrequency BlockFreq
Block frequency taken from MBFI.
Definition BBAddrMap.h:232
A feature extension of BBAddrMap that holds information relevant to PGO.
Definition BBAddrMap.h:210
bool operator==(const PGOAnalysisMap &Other) const
Definition BBAddrMap.h:251
std::vector< PGOBBEntry > BBEntries
Definition BBAddrMap.h:246
BBAddrMap::Features FeatEnable
Definition BBAddrMap.h:249