LLVM 24.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
21#include "llvm/Support/Error.h"
23
24namespace llvm {
25namespace object {
26
27// Struct representing the BBAddrMap for one function.
28struct BBAddrMap {
29
30 // Bitfield of optional features to control the extra information
31 // emitted/encoded in the section. The feature list lives in BBAddrMap.def.
32 struct Features {
33 enum {
34#define HANDLE_BB_ADDR_MAP_FEATURE(Name) Name##Bit,
35#include "llvm/Object/BBAddrMap.def"
37 };
38 static_assert(NumBits <= 16,
39 "BBAddrMap::Features is encoded as a uint16_t");
40
41#define HANDLE_BB_ADDR_MAP_FEATURE(Name) bool Name : 1;
42#include "llvm/Object/BBAddrMap.def"
43
44 static constexpr uint16_t KnownMask =
45 (static_cast<uint16_t>(1) << NumBits) - 1;
46
47 bool hasPGOAnalysis() const { return FuncEntryCount || BBFreq || BrProb; }
48
49 bool hasPGOAnalysisBBData() const { return BBFreq || BrProb; }
50
51 // Encodes to minimum bit width representation.
52 uint16_t encode() const {
53 uint16_t V = 0;
54#define HANDLE_BB_ADDR_MAP_FEATURE(Name) \
55 V |= static_cast<uint16_t>(Name) << Name##Bit;
56#include "llvm/Object/BBAddrMap.def"
57 return V;
58 }
59
60 // Decodes from minimum bit width representation and validates no
61 // unnecessary bits are used.
63 Features Feat{
64#define HANDLE_BB_ADDR_MAP_FEATURE(Name) \
65 static_cast<bool>(Val & (uint16_t{1} << Name##Bit)),
66#include "llvm/Object/BBAddrMap.def"
67 };
68 if (Feat.encode() != Val)
69 return createStringError(
70 "invalid encoding for BBAddrMap::Features: 0x%x", Val);
71 return Feat;
72 }
73
74 bool operator==(const Features &Other) const {
75 return encode() == Other.encode();
76 }
77 };
78
79 // Struct representing the BBAddrMap information for one basic block.
80 struct BBEntry {
81 struct Metadata {
82 enum {
83#define HANDLE_BB_ADDR_MAP_BB_METADATA(Name) Name##Bit,
84#include "llvm/Object/BBAddrMap.def"
86 };
87 static_assert(NumBits <= 32,
88 "BBAddrMap::BBEntry::Metadata is encoded as a uint32_t");
89
90#define HANDLE_BB_ADDR_MAP_BB_METADATA(Name) bool Name : 1;
91#include "llvm/Object/BBAddrMap.def"
92
93 bool operator==(const Metadata &Other) const {
94 return encode() == Other.encode();
95 }
96
97 // Encodes this struct as a uint32_t value.
98 uint32_t encode() const {
99 uint32_t V = 0;
100#define HANDLE_BB_ADDR_MAP_BB_METADATA(Name) \
101 V |= static_cast<uint32_t>(Name) << Name##Bit;
102#include "llvm/Object/BBAddrMap.def"
103 return V;
104 }
105
106 // Decodes and returns a Metadata struct from a uint32_t value.
108 Metadata MD{
109#define HANDLE_BB_ADDR_MAP_BB_METADATA(Name) \
110 static_cast<bool>(V & (uint32_t{1} << Name##Bit)),
111#include "llvm/Object/BBAddrMap.def"
112 };
113 if (MD.encode() != V)
114 return createStringError(
115 "invalid encoding for BBEntry::Metadata: 0x%x", V);
116 return MD;
117 }
118 };
119
120 uint32_t ID = 0; // Unique ID of this basic block.
121 uint32_t Offset = 0; // Offset of basic block relative to the base address.
122 uint32_t Size = 0; // Size of the basic block.
123 Metadata MD = {false, false, false, false,
124 false}; // Metadata for this basic block.
125 // Offsets of end of call instructions, relative to the basic block start.
127 uint64_t Hash = 0; // Hash for this basic block.
128
133
134 UniqueBBID getID() const { return {ID, 0}; }
135
136 bool operator==(const BBEntry &Other) const {
137 return ID == Other.ID && Offset == Other.Offset && Size == Other.Size &&
138 MD == Other.MD && CallsiteEndOffsets == Other.CallsiteEndOffsets &&
139 Hash == Other.Hash;
140 }
141
142 bool hasReturn() const { return MD.HasReturn; }
143 bool hasTailCall() const { return MD.HasTailCall; }
144 bool isEHPad() const { return MD.IsEHPad; }
145 bool canFallThrough() const { return MD.CanFallThrough; }
146 bool hasIndirectBranch() const { return MD.HasIndirectBranch; }
147 };
148
149 // Struct representing the BBAddrMap information for a contiguous range of
150 // basic blocks (a function or a basic block section).
152 uint64_t BaseAddress = 0; // Base address of the range.
153 std::vector<BBEntry> BBEntries; // Basic block entries for this range.
154
155 // Equality operator for unit testing.
156 bool operator==(const BBRangeEntry &Other) const {
157 return BaseAddress == Other.BaseAddress && BBEntries == Other.BBEntries;
158 }
159 };
160
161 // All ranges for this function. Cannot be empty. The first range always
162 // corresponds to the function entry.
163 std::vector<BBRangeEntry> BBRanges;
164
165 // GUID of the function (as computed by `Function::getGUID`). Only valid when
166 // the `FuncGUID` feature is enabled; zero otherwise.
168
169 // Returns the function address associated with this BBAddrMap, which is
170 // stored as the `BaseAddress` of its first BBRangeEntry.
172 assert(!BBRanges.empty());
173 return BBRanges.front().BaseAddress;
174 }
175
176 // Returns the total number of bb entries in all bb ranges.
177 size_t getNumBBEntries() const {
178 size_t NumBBEntries = 0;
179 for (const auto &BBR : BBRanges)
180 NumBBEntries += BBR.BBEntries.size();
181 return NumBBEntries;
182 }
183
184 // Returns the index of the bb range with the given base address, or
185 // `std::nullopt` if no such range exists.
186 std::optional<size_t>
188 for (size_t I = 0; I < BBRanges.size(); ++I)
189 if (BBRanges[I].BaseAddress == BaseAddress)
190 return I;
191 return {};
192 }
193
194 // Returns bb entries in the first range.
195 const std::vector<BBEntry> &getBBEntries() const {
196 return BBRanges.front().BBEntries;
197 }
198
199 const std::vector<BBRangeEntry> &getBBRanges() const { return BBRanges; }
200
201 // Equality operator for unit testing.
202 bool operator==(const BBAddrMap &Other) const {
203 return BBRanges == Other.BBRanges && FuncGUID == Other.FuncGUID;
204 }
205};
206
207/// A feature extension of BBAddrMap that holds information relevant to PGO.
209 /// Extra basic block data with fields for block frequency and branch
210 /// probability.
211 struct PGOBBEntry {
212 /// Single successor of a given basic block that contains the tag and branch
213 /// probability associated with it.
215 /// Unique ID of this successor basic block.
217 /// Branch Probability of the edge to this successor taken from MBPI.
219 /// Raw edge count from the post link profile (e.g., from bolt or
220 /// propeller).
222
223 bool operator==(const SuccessorEntry &Other) const {
224 return std::tie(ID, Prob, PostLinkFreq) ==
225 std::tie(Other.ID, Other.Prob, Other.PostLinkFreq);
226 }
227 };
228
229 /// Block frequency taken from MBFI
231 /// Raw block count taken from the post link profile (e.g., from bolt or
232 /// propeller).
234 /// List of successors of the current block
236
237 bool operator==(const PGOBBEntry &Other) const {
238 return std::tie(BlockFreq, PostLinkBlockFreq, Successors) ==
239 std::tie(Other.BlockFreq, Other.PostLinkBlockFreq,
240 Other.Successors);
241 }
242 };
243
244 uint64_t FuncEntryCount; // Prof count from IR function
245 std::vector<PGOBBEntry> BBEntries; // Extended basic block entries
246
247 // Flags to indicate if each PGO related info was enabled in this function
249
250 bool operator==(const PGOAnalysisMap &Other) const {
251 return std::tie(FuncEntryCount, BBEntries, FeatEnable) ==
252 std::tie(Other.FuncEntryCount, Other.BBEntries, Other.FeatEnable);
253 }
254};
255
256/// Extracts addresses from a data stream.
257/// The base implementation reads the address directly.
258/// Subclasses can override to handle format-specific details such as relocation
259/// resolution.
261 const DataExtractor &Data;
262 unsigned AddressSize;
263
264public:
265 AddressExtractor(const DataExtractor &Data, unsigned AddressSize)
266 : Data(Data), AddressSize(AddressSize) {}
267
268 virtual ~AddressExtractor() = default;
269
270 const DataExtractor &getDataExtractor() const { return Data; }
271
272 /// Extract and resolve an address at the current \p Cur position.
274 uint64_t Address = Data.getUnsigned(Cur, AddressSize);
275 if (!Cur)
276 return Cur.takeError();
277 return Address;
278 }
279};
280
281/// Decodes one BB address map section payload.
282///
283/// \p Extractor provides address extraction and the underlying DataExtractor.
284/// \p PGOAnalyses if non-null, receives the decoded PGO analysis data. On
285/// error, \p PGOAnalyses may be partially populated.
287decodeBBAddrMapPayload(AddressExtractor &Extractor,
288 std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr);
289
290} // end namespace object.
291} // end namespace llvm.
292
293#endif // LLVM_OBJECT_BBADDRMAP_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
#define LLVM_ABI
Definition Compiler.h:211
#define I(x, y, z)
Definition MD5.cpp:57
This file defines the SmallVector class.
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Error takeError()
Return error contained inside this Cursor, if any.
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.
virtual ~AddressExtractor()=default
virtual Expected< uint64_t > extractAddress(DataExtractor::Cursor &Cur)
Extract and resolve an address at the current Cur position.
Definition BBAddrMap.h:273
const DataExtractor & getDataExtractor() const
Definition BBAddrMap.h:270
AddressExtractor(const DataExtractor &Data, unsigned AddressSize)
Definition BBAddrMap.h:265
LLVM_ABI Expected< std::vector< BBAddrMap > > decodeBBAddrMapPayload(AddressExtractor &Extractor, std::vector< PGOAnalysisMap > *PGOAnalyses=nullptr)
Decodes one BB address map section payload.
Definition BBAddrMap.cpp:47
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
@ 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:1933
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
static Expected< Metadata > decode(uint32_t V)
Definition BBAddrMap.h:107
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:129
SmallVector< uint32_t, 1 > CallsiteEndOffsets
Definition BBAddrMap.h:126
bool operator==(const BBEntry &Other) const
Definition BBAddrMap.h:136
bool operator==(const BBRangeEntry &Other) const
Definition BBAddrMap.h:156
std::vector< BBEntry > BBEntries
Definition BBAddrMap.h:153
bool operator==(const Features &Other) const
Definition BBAddrMap.h:74
static constexpr uint16_t KnownMask
Definition BBAddrMap.h:44
static Expected< Features > decode(uint16_t Val)
Definition BBAddrMap.h:62
const std::vector< BBRangeEntry > & getBBRanges() const
Definition BBAddrMap.h:199
std::vector< BBRangeEntry > BBRanges
Definition BBAddrMap.h:163
size_t getNumBBEntries() const
Definition BBAddrMap.h:177
bool operator==(const BBAddrMap &Other) const
Definition BBAddrMap.h:202
const std::vector< BBEntry > & getBBEntries() const
Definition BBAddrMap.h:195
uint64_t getFunctionAddress() const
Definition BBAddrMap.h:171
std::optional< size_t > getBBRangeIndexForBaseAddress(uint64_t BaseAddress) const
Definition BBAddrMap.h:187
Single successor of a given basic block that contains the tag and branch probability associated with ...
Definition BBAddrMap.h:214
uint32_t ID
Unique ID of this successor basic block.
Definition BBAddrMap.h:216
BranchProbability Prob
Branch Probability of the edge to this successor taken from MBPI.
Definition BBAddrMap.h:218
bool operator==(const SuccessorEntry &Other) const
Definition BBAddrMap.h:223
uint64_t PostLinkFreq
Raw edge count from the post link profile (e.g., from bolt or propeller).
Definition BBAddrMap.h:221
Extra basic block data with fields for block frequency and branch probability.
Definition BBAddrMap.h:211
bool operator==(const PGOBBEntry &Other) const
Definition BBAddrMap.h:237
uint64_t PostLinkBlockFreq
Raw block count taken from the post link profile (e.g., from bolt or propeller).
Definition BBAddrMap.h:233
llvm::SmallVector< SuccessorEntry, 2 > Successors
List of successors of the current block.
Definition BBAddrMap.h:235
BlockFrequency BlockFreq
Block frequency taken from MBFI.
Definition BBAddrMap.h:230
A feature extension of BBAddrMap that holds information relevant to PGO.
Definition BBAddrMap.h:208
bool operator==(const PGOAnalysisMap &Other) const
Definition BBAddrMap.h:250
std::vector< PGOBBEntry > BBEntries
Definition BBAddrMap.h:245
BBAddrMap::Features FeatEnable
Definition BBAddrMap.h:248