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.
32 struct Features {
34 bool BBFreq : 1;
35 bool BrProb : 1;
36 bool MultiBBRange : 1;
37 bool OmitBBEntries : 1;
39 bool BBHash : 1;
40 bool PostLinkCfg : 1;
41
42 bool hasPGOAnalysis() const { return FuncEntryCount || BBFreq || BrProb; }
43
44 bool hasPGOAnalysisBBData() const { return BBFreq || BrProb; }
45
46 // Encodes to minimum bit width representation.
47 uint16_t encode() const {
48 return (static_cast<uint16_t>(FuncEntryCount) << 0) |
49 (static_cast<uint16_t>(BBFreq) << 1) |
50 (static_cast<uint16_t>(BrProb) << 2) |
51 (static_cast<uint16_t>(MultiBBRange) << 3) |
52 (static_cast<uint16_t>(OmitBBEntries) << 4) |
53 (static_cast<uint16_t>(CallsiteEndOffsets) << 5) |
54 (static_cast<uint16_t>(BBHash) << 6) |
55 (static_cast<uint16_t>(PostLinkCfg) << 7);
56 }
57
58 // Decodes from minimum bit width representation and validates no
59 // unnecessary bits are used.
61 Features Feat{
62 static_cast<bool>(Val & (1 << 0)), static_cast<bool>(Val & (1 << 1)),
63 static_cast<bool>(Val & (1 << 2)), static_cast<bool>(Val & (1 << 3)),
64 static_cast<bool>(Val & (1 << 4)), static_cast<bool>(Val & (1 << 5)),
65 static_cast<bool>(Val & (1 << 6)), static_cast<bool>(Val & (1 << 7))};
66 if (Feat.encode() != Val)
67 return createStringError(
68 "invalid encoding for BBAddrMap::Features: 0x%x", 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 "invalid encoding for BBEntry::Metadata: 0x%x", V);
119 return MD;
120 }
121 };
122
123 uint32_t ID = 0; // Unique ID of this basic block.
124 uint32_t Offset = 0; // Offset of basic block relative to the base address.
125 uint32_t Size = 0; // Size of the basic block.
126 Metadata MD = {false, false, false, false,
127 false}; // Metadata for this basic block.
128 // Offsets of end of call instructions, relative to the basic block start.
130 uint64_t Hash = 0; // Hash for this basic block.
131
136
137 UniqueBBID getID() const { return {ID, 0}; }
138
139 bool operator==(const BBEntry &Other) const {
140 return ID == Other.ID && Offset == Other.Offset && Size == Other.Size &&
141 MD == Other.MD && CallsiteEndOffsets == Other.CallsiteEndOffsets &&
142 Hash == Other.Hash;
143 }
144
145 bool hasReturn() const { return MD.HasReturn; }
146 bool hasTailCall() const { return MD.HasTailCall; }
147 bool isEHPad() const { return MD.IsEHPad; }
148 bool canFallThrough() const { return MD.CanFallThrough; }
149 bool hasIndirectBranch() const { return MD.HasIndirectBranch; }
150 };
151
152 // Struct representing the BBAddrMap information for a contiguous range of
153 // basic blocks (a function or a basic block section).
155 uint64_t BaseAddress = 0; // Base address of the range.
156 std::vector<BBEntry> BBEntries; // Basic block entries for this range.
157
158 // Equality operator for unit testing.
159 bool operator==(const BBRangeEntry &Other) const {
160 return BaseAddress == Other.BaseAddress && BBEntries == Other.BBEntries;
161 }
162 };
163
164 // All ranges for this function. Cannot be empty. The first range always
165 // corresponds to the function entry.
166 std::vector<BBRangeEntry> BBRanges;
167
168 // Returns the function address associated with this BBAddrMap, which is
169 // stored as the `BaseAddress` of its first BBRangeEntry.
171 assert(!BBRanges.empty());
172 return BBRanges.front().BaseAddress;
173 }
174
175 // Returns the total number of bb entries in all bb ranges.
176 size_t getNumBBEntries() const {
177 size_t NumBBEntries = 0;
178 for (const auto &BBR : BBRanges)
179 NumBBEntries += BBR.BBEntries.size();
180 return NumBBEntries;
181 }
182
183 // Returns the index of the bb range with the given base address, or
184 // `std::nullopt` if no such range exists.
185 std::optional<size_t>
187 for (size_t I = 0; I < BBRanges.size(); ++I)
188 if (BBRanges[I].BaseAddress == BaseAddress)
189 return I;
190 return {};
191 }
192
193 // Returns bb entries in the first range.
194 const std::vector<BBEntry> &getBBEntries() const {
195 return BBRanges.front().BBEntries;
196 }
197
198 const std::vector<BBRangeEntry> &getBBRanges() const { return BBRanges; }
199
200 // Equality operator for unit testing.
201 bool operator==(const BBAddrMap &Other) const {
202 return BBRanges == Other.BBRanges;
203 }
204};
205
206/// A feature extension of BBAddrMap that holds information relevant to PGO.
208 /// Extra basic block data with fields for block frequency and branch
209 /// probability.
210 struct PGOBBEntry {
211 /// Single successor of a given basic block that contains the tag and branch
212 /// probability associated with it.
214 /// Unique ID of this successor basic block.
216 /// Branch Probability of the edge to this successor taken from MBPI.
218 /// Raw edge count from the post link profile (e.g., from bolt or
219 /// propeller).
221
222 bool operator==(const SuccessorEntry &Other) const {
223 return std::tie(ID, Prob, PostLinkFreq) ==
224 std::tie(Other.ID, Other.Prob, Other.PostLinkFreq);
225 }
226 };
227
228 /// Block frequency taken from MBFI
230 /// Raw block count taken from the post link profile (e.g., from bolt or
231 /// propeller).
233 /// List of successors of the current block
235
236 bool operator==(const PGOBBEntry &Other) const {
237 return std::tie(BlockFreq, PostLinkBlockFreq, Successors) ==
238 std::tie(Other.BlockFreq, Other.PostLinkBlockFreq,
239 Other.Successors);
240 }
241 };
242
243 uint64_t FuncEntryCount; // Prof count from IR function
244 std::vector<PGOBBEntry> BBEntries; // Extended basic block entries
245
246 // Flags to indicate if each PGO related info was enabled in this function
248
249 bool operator==(const PGOAnalysisMap &Other) const {
250 return std::tie(FuncEntryCount, BBEntries, FeatEnable) ==
251 std::tie(Other.FuncEntryCount, Other.BBEntries, Other.FeatEnable);
252 }
253};
254
255/// Extracts addresses from a data stream.
256/// The base implementation reads the address directly.
257/// Subclasses can override to handle format-specific details such as relocation
258/// resolution.
260 const DataExtractor &Data;
261
262public:
263 AddressExtractor(const DataExtractor &Data) : Data(Data) {}
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.getAddress(Cur);
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.
AddressExtractor(const DataExtractor &Data)
Definition BBAddrMap.h:263
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
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:1328
@ 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:132
SmallVector< uint32_t, 1 > CallsiteEndOffsets
Definition BBAddrMap.h:129
bool operator==(const BBEntry &Other) const
Definition BBAddrMap.h:139
bool operator==(const BBRangeEntry &Other) const
Definition BBAddrMap.h:159
std::vector< BBEntry > BBEntries
Definition BBAddrMap.h:156
bool operator==(const Features &Other) const
Definition BBAddrMap.h:72
static Expected< Features > decode(uint16_t Val)
Definition BBAddrMap.h:60
const std::vector< BBRangeEntry > & getBBRanges() const
Definition BBAddrMap.h:198
std::vector< BBRangeEntry > BBRanges
Definition BBAddrMap.h:166
size_t getNumBBEntries() const
Definition BBAddrMap.h:176
bool operator==(const BBAddrMap &Other) const
Definition BBAddrMap.h:201
const std::vector< BBEntry > & getBBEntries() const
Definition BBAddrMap.h:194
uint64_t getFunctionAddress() const
Definition BBAddrMap.h:170
std::optional< size_t > getBBRangeIndexForBaseAddress(uint64_t BaseAddress) const
Definition BBAddrMap.h:186
Single successor of a given basic block that contains the tag and branch probability associated with ...
Definition BBAddrMap.h:213
uint32_t ID
Unique ID of this successor basic block.
Definition BBAddrMap.h:215
BranchProbability Prob
Branch Probability of the edge to this successor taken from MBPI.
Definition BBAddrMap.h:217
bool operator==(const SuccessorEntry &Other) const
Definition BBAddrMap.h:222
uint64_t PostLinkFreq
Raw edge count from the post link profile (e.g., from bolt or propeller).
Definition BBAddrMap.h:220
Extra basic block data with fields for block frequency and branch probability.
Definition BBAddrMap.h:210
bool operator==(const PGOBBEntry &Other) const
Definition BBAddrMap.h:236
uint64_t PostLinkBlockFreq
Raw block count taken from the post link profile (e.g., from bolt or propeller).
Definition BBAddrMap.h:232
llvm::SmallVector< SuccessorEntry, 2 > Successors
List of successors of the current block.
Definition BBAddrMap.h:234
BlockFrequency BlockFreq
Block frequency taken from MBFI.
Definition BBAddrMap.h:229
A feature extension of BBAddrMap that holds information relevant to PGO.
Definition BBAddrMap.h:207
bool operator==(const PGOAnalysisMap &Other) const
Definition BBAddrMap.h:249
std::vector< PGOBBEntry > BBEntries
Definition BBAddrMap.h:244
BBAddrMap::Features FeatEnable
Definition BBAddrMap.h:247