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 "invalid encoding for BBAddrMap::Features: 0x%x", Val);
68 return Feat;
69 }
70
71 bool operator==(const Features &Other) const {
72 return std::tie(FuncEntryCount, BBFreq, BrProb, MultiBBRange,
74 std::tie(Other.FuncEntryCount, Other.BBFreq, Other.BrProb,
75 Other.MultiBBRange, Other.OmitBBEntries,
76 Other.CallsiteEndOffsets, Other.BBHash,
77 Other.PostLinkCfg);
78 }
79 };
80
81 // Struct representing the BBAddrMap information for one basic block.
82 struct BBEntry {
83 struct Metadata {
84 bool HasReturn : 1; // If this block ends with a return (or tail
85 // call).
86 bool HasTailCall : 1; // If this block ends with a tail call.
87 bool IsEHPad : 1; // If this is an exception handling block.
88 bool CanFallThrough : 1; // If this block can fall through to its next.
89 bool HasIndirectBranch : 1; // If this block ends with an indirect branch
90 // (branch via a register).
91
92 bool operator==(const Metadata &Other) const {
93 return HasReturn == Other.HasReturn &&
94 HasTailCall == Other.HasTailCall && IsEHPad == Other.IsEHPad &&
95 CanFallThrough == Other.CanFallThrough &&
96 HasIndirectBranch == Other.HasIndirectBranch;
97 }
98
99 // Encodes this struct as a uint32_t value.
100 uint32_t encode() const {
101 return static_cast<uint32_t>(HasReturn) |
102 (static_cast<uint32_t>(HasTailCall) << 1) |
103 (static_cast<uint32_t>(IsEHPad) << 2) |
104 (static_cast<uint32_t>(CanFallThrough) << 3) |
105 (static_cast<uint32_t>(HasIndirectBranch) << 4);
106 }
107
108 // Decodes and returns a Metadata struct from a uint32_t value.
110 Metadata MD{/*HasReturn=*/static_cast<bool>(V & 1),
111 /*HasTailCall=*/static_cast<bool>(V & (1 << 1)),
112 /*IsEHPad=*/static_cast<bool>(V & (1 << 2)),
113 /*CanFallThrough=*/static_cast<bool>(V & (1 << 3)),
114 /*HasIndirectBranch=*/static_cast<bool>(V & (1 << 4))};
115 if (MD.encode() != V)
116 return createStringError(
117 "invalid encoding for BBEntry::Metadata: 0x%x", V);
118 return MD;
119 }
120 };
121
122 uint32_t ID = 0; // Unique ID of this basic block.
123 uint32_t Offset = 0; // Offset of basic block relative to the base address.
124 uint32_t Size = 0; // Size of the basic block.
125 Metadata MD = {false, false, false, false,
126 false}; // Metadata for this basic block.
127 // Offsets of end of call instructions, relative to the basic block start.
129 uint64_t Hash = 0; // Hash for this basic block.
130
135
136 UniqueBBID getID() const { return {ID, 0}; }
137
138 bool operator==(const BBEntry &Other) const {
139 return ID == Other.ID && Offset == Other.Offset && Size == Other.Size &&
140 MD == Other.MD && CallsiteEndOffsets == Other.CallsiteEndOffsets &&
141 Hash == Other.Hash;
142 }
143
144 bool hasReturn() const { return MD.HasReturn; }
145 bool hasTailCall() const { return MD.HasTailCall; }
146 bool isEHPad() const { return MD.IsEHPad; }
147 bool canFallThrough() const { return MD.CanFallThrough; }
148 bool hasIndirectBranch() const { return MD.HasIndirectBranch; }
149 };
150
151 // Struct representing the BBAddrMap information for a contiguous range of
152 // basic blocks (a function or a basic block section).
154 uint64_t BaseAddress = 0; // Base address of the range.
155 std::vector<BBEntry> BBEntries; // Basic block entries for this range.
156
157 // Equality operator for unit testing.
158 bool operator==(const BBRangeEntry &Other) const {
159 return BaseAddress == Other.BaseAddress && BBEntries == Other.BBEntries;
160 }
161 };
162
163 // All ranges for this function. Cannot be empty. The first range always
164 // corresponds to the function entry.
165 std::vector<BBRangeEntry> BBRanges;
166
167 // Returns the function address associated with this BBAddrMap, which is
168 // stored as the `BaseAddress` of its first BBRangeEntry.
170 assert(!BBRanges.empty());
171 return BBRanges.front().BaseAddress;
172 }
173
174 // Returns the total number of bb entries in all bb ranges.
175 size_t getNumBBEntries() const {
176 size_t NumBBEntries = 0;
177 for (const auto &BBR : BBRanges)
178 NumBBEntries += BBR.BBEntries.size();
179 return NumBBEntries;
180 }
181
182 // Returns the index of the bb range with the given base address, or
183 // `std::nullopt` if no such range exists.
184 std::optional<size_t>
186 for (size_t I = 0; I < BBRanges.size(); ++I)
187 if (BBRanges[I].BaseAddress == BaseAddress)
188 return I;
189 return {};
190 }
191
192 // Returns bb entries in the first range.
193 const std::vector<BBEntry> &getBBEntries() const {
194 return BBRanges.front().BBEntries;
195 }
196
197 const std::vector<BBRangeEntry> &getBBRanges() const { return BBRanges; }
198
199 // Equality operator for unit testing.
200 bool operator==(const BBAddrMap &Other) const {
201 return BBRanges == Other.BBRanges;
202 }
203};
204
205/// A feature extension of BBAddrMap that holds information relevant to PGO.
207 /// Extra basic block data with fields for block frequency and branch
208 /// probability.
209 struct PGOBBEntry {
210 /// Single successor of a given basic block that contains the tag and branch
211 /// probability associated with it.
213 /// Unique ID of this successor basic block.
215 /// Branch Probability of the edge to this successor taken from MBPI.
217 /// Raw edge count from the post link profile (e.g., from bolt or
218 /// propeller).
220
221 bool operator==(const SuccessorEntry &Other) const {
222 return std::tie(ID, Prob, PostLinkFreq) ==
223 std::tie(Other.ID, Other.Prob, Other.PostLinkFreq);
224 }
225 };
226
227 /// Block frequency taken from MBFI
229 /// Raw block count taken from the post link profile (e.g., from bolt or
230 /// propeller).
232 /// List of successors of the current block
234
235 bool operator==(const PGOBBEntry &Other) const {
236 return std::tie(BlockFreq, PostLinkBlockFreq, Successors) ==
237 std::tie(Other.BlockFreq, Other.PostLinkBlockFreq,
238 Other.Successors);
239 }
240 };
241
242 uint64_t FuncEntryCount; // Prof count from IR function
243 std::vector<PGOBBEntry> BBEntries; // Extended basic block entries
244
245 // Flags to indicate if each PGO related info was enabled in this function
247
248 bool operator==(const PGOAnalysisMap &Other) const {
249 return std::tie(FuncEntryCount, BBEntries, FeatEnable) ==
250 std::tie(Other.FuncEntryCount, Other.BBEntries, Other.FeatEnable);
251 }
252};
253
254} // end namespace object.
255} // end namespace llvm.
256
257#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:109
bool operator==(const Metadata &Other) const
Definition BBAddrMap.h:92
BBEntry(uint32_t ID, uint32_t Offset, uint32_t Size, Metadata MD, SmallVector< uint32_t, 1 > CallsiteEndOffsets, uint64_t Hash)
Definition BBAddrMap.h:131
SmallVector< uint32_t, 1 > CallsiteEndOffsets
Definition BBAddrMap.h:128
bool operator==(const BBEntry &Other) const
Definition BBAddrMap.h:138
bool operator==(const BBRangeEntry &Other) const
Definition BBAddrMap.h:158
std::vector< BBEntry > BBEntries
Definition BBAddrMap.h:155
bool operator==(const Features &Other) const
Definition BBAddrMap.h:71
static Expected< Features > decode(uint16_t Val)
Definition BBAddrMap.h:59
const std::vector< BBRangeEntry > & getBBRanges() const
Definition BBAddrMap.h:197
std::vector< BBRangeEntry > BBRanges
Definition BBAddrMap.h:165
size_t getNumBBEntries() const
Definition BBAddrMap.h:175
bool operator==(const BBAddrMap &Other) const
Definition BBAddrMap.h:200
const std::vector< BBEntry > & getBBEntries() const
Definition BBAddrMap.h:193
uint64_t getFunctionAddress() const
Definition BBAddrMap.h:169
std::optional< size_t > getBBRangeIndexForBaseAddress(uint64_t BaseAddress) const
Definition BBAddrMap.h:185
Single successor of a given basic block that contains the tag and branch probability associated with ...
Definition BBAddrMap.h:212
uint32_t ID
Unique ID of this successor basic block.
Definition BBAddrMap.h:214
BranchProbability Prob
Branch Probability of the edge to this successor taken from MBPI.
Definition BBAddrMap.h:216
bool operator==(const SuccessorEntry &Other) const
Definition BBAddrMap.h:221
uint64_t PostLinkFreq
Raw edge count from the post link profile (e.g., from bolt or propeller).
Definition BBAddrMap.h:219
Extra basic block data with fields for block frequency and branch probability.
Definition BBAddrMap.h:209
bool operator==(const PGOBBEntry &Other) const
Definition BBAddrMap.h:235
uint64_t PostLinkBlockFreq
Raw block count taken from the post link profile (e.g., from bolt or propeller).
Definition BBAddrMap.h:231
llvm::SmallVector< SuccessorEntry, 2 > Successors
List of successors of the current block.
Definition BBAddrMap.h:233
BlockFrequency BlockFreq
Block frequency taken from MBFI.
Definition BBAddrMap.h:228
A feature extension of BBAddrMap that holds information relevant to PGO.
Definition BBAddrMap.h:206
bool operator==(const PGOAnalysisMap &Other) const
Definition BBAddrMap.h:248
std::vector< PGOBBEntry > BBEntries
Definition BBAddrMap.h:243
BBAddrMap::Features FeatEnable
Definition BBAddrMap.h:246