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
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 // Returns the function address associated with this BBAddrMap, which is
166 // stored as the `BaseAddress` of its first BBRangeEntry.
168 assert(!BBRanges.empty());
169 return BBRanges.front().BaseAddress;
170 }
171
172 // Returns the total number of bb entries in all bb ranges.
173 size_t getNumBBEntries() const {
174 size_t NumBBEntries = 0;
175 for (const auto &BBR : BBRanges)
176 NumBBEntries += BBR.BBEntries.size();
177 return NumBBEntries;
178 }
179
180 // Returns the index of the bb range with the given base address, or
181 // `std::nullopt` if no such range exists.
182 std::optional<size_t>
184 for (size_t I = 0; I < BBRanges.size(); ++I)
185 if (BBRanges[I].BaseAddress == BaseAddress)
186 return I;
187 return {};
188 }
189
190 // Returns bb entries in the first range.
191 const std::vector<BBEntry> &getBBEntries() const {
192 return BBRanges.front().BBEntries;
193 }
194
195 const std::vector<BBRangeEntry> &getBBRanges() const { return BBRanges; }
196
197 // Equality operator for unit testing.
198 bool operator==(const BBAddrMap &Other) const {
199 return BBRanges == Other.BBRanges;
200 }
201};
202
203/// A feature extension of BBAddrMap that holds information relevant to PGO.
205 /// Extra basic block data with fields for block frequency and branch
206 /// probability.
207 struct PGOBBEntry {
208 /// Single successor of a given basic block that contains the tag and branch
209 /// probability associated with it.
211 /// Unique ID of this successor basic block.
213 /// Branch Probability of the edge to this successor taken from MBPI.
215 /// Raw edge count from the post link profile (e.g., from bolt or
216 /// propeller).
218
219 bool operator==(const SuccessorEntry &Other) const {
220 return std::tie(ID, Prob, PostLinkFreq) ==
221 std::tie(Other.ID, Other.Prob, Other.PostLinkFreq);
222 }
223 };
224
225 /// Block frequency taken from MBFI
227 /// Raw block count taken from the post link profile (e.g., from bolt or
228 /// propeller).
230 /// List of successors of the current block
232
233 bool operator==(const PGOBBEntry &Other) const {
234 return std::tie(BlockFreq, PostLinkBlockFreq, Successors) ==
235 std::tie(Other.BlockFreq, Other.PostLinkBlockFreq,
236 Other.Successors);
237 }
238 };
239
240 uint64_t FuncEntryCount; // Prof count from IR function
241 std::vector<PGOBBEntry> BBEntries; // Extended basic block entries
242
243 // Flags to indicate if each PGO related info was enabled in this function
245
246 bool operator==(const PGOAnalysisMap &Other) const {
247 return std::tie(FuncEntryCount, BBEntries, FeatEnable) ==
248 std::tie(Other.FuncEntryCount, Other.BBEntries, Other.FeatEnable);
249 }
250};
251
252/// Extracts addresses from a data stream.
253/// The base implementation reads the address directly.
254/// Subclasses can override to handle format-specific details such as relocation
255/// resolution.
257 const DataExtractor &Data;
258 unsigned AddressSize;
259
260public:
261 AddressExtractor(const DataExtractor &Data, unsigned AddressSize)
262 : Data(Data), AddressSize(AddressSize) {}
263
264 virtual ~AddressExtractor() = default;
265
266 const DataExtractor &getDataExtractor() const { return Data; }
267
268 /// Extract and resolve an address at the current \p Cur position.
270 uint64_t Address = Data.getUnsigned(Cur, AddressSize);
271 if (!Cur)
272 return Cur.takeError();
273 return Address;
274 }
275};
276
277/// Decodes one BB address map section payload.
278///
279/// \p Extractor provides address extraction and the underlying DataExtractor.
280/// \p PGOAnalyses if non-null, receives the decoded PGO analysis data. On
281/// error, \p PGOAnalyses may be partially populated.
283decodeBBAddrMapPayload(AddressExtractor &Extractor,
284 std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr);
285
286} // end namespace object.
287} // end namespace llvm.
288
289#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.
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:269
const DataExtractor & getDataExtractor() const
Definition BBAddrMap.h:266
AddressExtractor(const DataExtractor &Data, unsigned AddressSize)
Definition BBAddrMap.h:261
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:1916
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:874
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:195
std::vector< BBRangeEntry > BBRanges
Definition BBAddrMap.h:163
size_t getNumBBEntries() const
Definition BBAddrMap.h:173
bool operator==(const BBAddrMap &Other) const
Definition BBAddrMap.h:198
const std::vector< BBEntry > & getBBEntries() const
Definition BBAddrMap.h:191
uint64_t getFunctionAddress() const
Definition BBAddrMap.h:167
std::optional< size_t > getBBRangeIndexForBaseAddress(uint64_t BaseAddress) const
Definition BBAddrMap.h:183
Single successor of a given basic block that contains the tag and branch probability associated with ...
Definition BBAddrMap.h:210
uint32_t ID
Unique ID of this successor basic block.
Definition BBAddrMap.h:212
BranchProbability Prob
Branch Probability of the edge to this successor taken from MBPI.
Definition BBAddrMap.h:214
bool operator==(const SuccessorEntry &Other) const
Definition BBAddrMap.h:219
uint64_t PostLinkFreq
Raw edge count from the post link profile (e.g., from bolt or propeller).
Definition BBAddrMap.h:217
Extra basic block data with fields for block frequency and branch probability.
Definition BBAddrMap.h:207
bool operator==(const PGOBBEntry &Other) const
Definition BBAddrMap.h:233
uint64_t PostLinkBlockFreq
Raw block count taken from the post link profile (e.g., from bolt or propeller).
Definition BBAddrMap.h:229
llvm::SmallVector< SuccessorEntry, 2 > Successors
List of successors of the current block.
Definition BBAddrMap.h:231
BlockFrequency BlockFreq
Block frequency taken from MBFI.
Definition BBAddrMap.h:226
A feature extension of BBAddrMap that holds information relevant to PGO.
Definition BBAddrMap.h:204
bool operator==(const PGOAnalysisMap &Other) const
Definition BBAddrMap.h:246
std::vector< PGOBBEntry > BBEntries
Definition BBAddrMap.h:241
BBAddrMap::Features FeatEnable
Definition BBAddrMap.h:244