LLVM 24.0.0git
WholeProgramDevirt.h
Go to the documentation of this file.
1//===- WholeProgramDevirt.h - Whole-program devirt pass ---------*- C++ -*-===//
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// This file defines parts of the whole-program devirtualization pass
10// implementation that may be usefully unit tested.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TRANSFORMS_IPO_WHOLEPROGRAMDEVIRT_H
15#define LLVM_TRANSFORMS_IPO_WHOLEPROGRAMDEVIRT_H
16
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/IR/GlobalValue.h"
19#include "llvm/IR/PassManager.h"
21#include <cassert>
22#include <cstdint>
23#include <map>
24#include <set>
25#include <utility>
26#include <vector>
27
28namespace llvm {
29class Module;
30
31template <typename T> class ArrayRef;
32template <typename T> class MutableArrayRef;
33class GlobalVariable;
35struct ValueInfo;
36namespace cl {
38}
39
41
42// A bit vector that keeps track of which bits are used. We use this to
43// pack constant values compactly before and after each virtual table.
45 std::vector<uint8_t> Bytes;
46
47 // Bits in BytesUsed[I] are 1 if matching bit in Bytes[I] is used, 0 if not.
48 std::vector<uint8_t> BytesUsed;
49
50 std::pair<uint8_t *, uint8_t *> getPtrToData(uint64_t Pos, uint8_t Size) {
51 if (Bytes.size() < Pos + Size) {
52 Bytes.resize(Pos + Size);
53 BytesUsed.resize(Pos + Size);
54 }
55 return std::make_pair(Bytes.data() + Pos, BytesUsed.data() + Pos);
56 }
57
58 // Set little-endian value Val with size Size at bit position Pos,
59 // and mark bytes as used.
61 assert(Pos % 8 == 0);
62 auto DataUsed = getPtrToData(Pos / 8, Size);
63 for (unsigned I = 0; I != Size; ++I) {
64 DataUsed.first[I] = Val >> (I * 8);
65 assert(!DataUsed.second[I]);
66 DataUsed.second[I] = 0xff;
67 }
68 }
69
70 // Set big-endian value Val with size Size at bit position Pos,
71 // and mark bytes as used.
73 assert(Pos % 8 == 0);
74 auto DataUsed = getPtrToData(Pos / 8, Size);
75 for (unsigned I = 0; I != Size; ++I) {
76 DataUsed.first[Size - I - 1] = Val >> (I * 8);
77 assert(!DataUsed.second[Size - I - 1]);
78 DataUsed.second[Size - I - 1] = 0xff;
79 }
80 }
81
82 // Set bit at bit position Pos to b and mark bit as used.
83 void setBit(uint64_t Pos, bool b) {
84 auto DataUsed = getPtrToData(Pos / 8, 1);
85 if (b)
86 *DataUsed.first |= 1 << (Pos % 8);
87 assert(!(*DataUsed.second & (1 << Pos % 8)));
88 *DataUsed.second |= 1 << (Pos % 8);
89 }
90};
91
92// The bits that will be stored before and after a particular vtable.
93struct VTableBits {
94 // The vtable global.
96
97 // Cache of the vtable's size in bytes.
99
100 // The bit vector that will be laid out before the vtable. Note that these
101 // bytes are stored in reverse order until the globals are rebuilt. This means
102 // that any values in the array must be stored using the opposite endianness
103 // from the target.
105
106 // The bit vector that will be laid out after the vtable.
108};
109
110// Information about a member of a particular type identifier.
112 // The VTableBits for the vtable.
114
115 // The offset in bytes from the start of the vtable (i.e. the address point).
117
118 bool operator<(const TypeMemberInfo &other) const {
119 return std::tie(Bits, Offset) < std::tie(other.Bits, other.Offset);
120 }
121};
122
123// A virtual call target, i.e. an entry in a particular vtable.
126
127 // For testing only.
130
131 // The function (or an alias to a function) stored in the vtable.
133
134 // A pointer to the type identifier member through which the pointer to Fn is
135 // accessed.
137
138 // When doing virtual constant propagation, this stores the return value for
139 // the function when passed the currently considered argument list.
141
142 // Whether the target is big endian.
144
145 // Whether at least one call site to the target was devirtualized.
147
148 // The minimum byte offset before the address point. This covers the bytes in
149 // the vtable object before the address point (e.g. RTTI, access-to-top,
150 // vtables for other base classes) and is equal to the offset from the start
151 // of the vtable object to the address point.
152 uint64_t minBeforeBytes() const { return TM->Offset; }
153
154 // The minimum byte offset after the address point. This covers the bytes in
155 // the vtable object after the address point (e.g. the vtable for the current
156 // class and any later base classes) and is equal to the size of the vtable
157 // object minus the offset from the start of the vtable object to the address
158 // point.
159 uint64_t minAfterBytes() const { return TM->Bits->ObjectSize - TM->Offset; }
160
161 // The number of bytes allocated (for the vtable plus the byte array) before
162 // the address point.
164 return minBeforeBytes() + TM->Bits->Before.Bytes.size();
165 }
166
167 // The number of bytes allocated (for the vtable plus the byte array) after
168 // the address point.
170 return minAfterBytes() + TM->Bits->After.Bytes.size();
171 }
172
173 // Set the bit at position Pos before the address point to RetVal.
175 assert(Pos >= 8 * minBeforeBytes());
176 TM->Bits->Before.setBit(Pos - 8 * minBeforeBytes(), RetVal);
177 }
178
179 // Set the bit at position Pos after the address point to RetVal.
181 assert(Pos >= 8 * minAfterBytes());
182 TM->Bits->After.setBit(Pos - 8 * minAfterBytes(), RetVal);
183 }
184
185 // Set the bytes at position Pos before the address point to RetVal.
186 // Because the bytes in Before are stored in reverse order, we use the
187 // opposite endianness to the target.
189 assert(Pos >= 8 * minBeforeBytes());
190 if (IsBigEndian)
191 TM->Bits->Before.setLE(Pos - 8 * minBeforeBytes(), RetVal, Size);
192 else
193 TM->Bits->Before.setBE(Pos - 8 * minBeforeBytes(), RetVal, Size);
194 }
195
196 // Set the bytes at position Pos after the address point to RetVal.
198 assert(Pos >= 8 * minAfterBytes());
199 if (IsBigEndian)
200 TM->Bits->After.setBE(Pos - 8 * minAfterBytes(), RetVal, Size);
201 else
202 TM->Bits->After.setLE(Pos - 8 * minAfterBytes(), RetVal, Size);
203 }
204};
205
206// Find the minimum offset that we may store a value of size Size bits at. If
207// IsAfter is set, look for an offset before the object, otherwise look for an
208// offset after the object.
210 bool IsAfter, uint64_t Size);
211
212// Set the stored value in each of Targets to VirtualCallTarget::RetVal at the
213// given allocation offset before the vtable address. Stores the computed
214// byte/bit offset to OffsetByte/OffsetBit.
216 uint64_t AllocBefore, unsigned BitWidth,
217 int64_t &OffsetByte, uint64_t &OffsetBit);
218
219// Set the stored value in each of Targets to VirtualCallTarget::RetVal at the
220// given allocation offset after the vtable address. Stores the computed
221// byte/bit offset to OffsetByte/OffsetBit.
223 uint64_t AllocAfter, unsigned BitWidth,
224 int64_t &OffsetByte, uint64_t &OffsetBit);
225
226} // end namespace wholeprogramdevirt
227
245
250LLVM_ABI bool
252 bool WholeProgramVisibilityEnabledInLTO);
253/// Convert llvm.public.type.test calls to llvm.type.test when whole program
254/// visibility is available, or otherwise drop them unless speculative
255/// devirtualization is enabled, in which case WPD still needs them to identify
256/// the call sites. Speculative devirtualization is enabled either via the
257/// option, or when \p DevirtSpeculativelyInIndex is true, i.e. it was recorded
258/// in the combined index by the thin link. The latter is only needed by
259/// (distributed) ThinLTO backends; regular LTO runs in the same process as the
260/// option so it can rely on the default.
261LLVM_ABI void
262updatePublicTypeTestCalls(Module &M, bool WholeProgramVisibilityEnabledInLTO,
263 bool DevirtSpeculativelyInIndex = false);
265 Module &M, bool WholeProgramVisibilityEnabledInLTO,
266 const DenseSet<GlobalValue::GUID> &DynamicExportSymbols,
267 bool ValidateAllVtablesHaveTypeInfos,
268 function_ref<bool(StringRef)> IsVisibleToRegularObj);
270 const cl::LibraryOptionsContext &LibraryOptions, ModuleSummaryIndex &Index,
271 bool WholeProgramVisibilityEnabledInLTO,
272 const DenseSet<GlobalValue::GUID> &DynamicExportSymbols,
273 const DenseSet<GlobalValue::GUID> &VisibleToRegularObjSymbols);
274
276 ModuleSummaryIndex &Index,
277 DenseSet<GlobalValue::GUID> &VisibleToRegularObjSymbols,
278 function_ref<bool(StringRef)> IsVisibleToRegularObj);
279
280/// Perform index-based whole program devirtualization on the \p Summary
281/// index. Any devirtualized targets used by a type test in another module
282/// are added to the \p ExportedGUIDs set. For any local devirtualized targets
283/// only used within the defining module, the information necessary for
284/// locating the corresponding WPD resolution is recorded for the ValueInfo
285/// in case it is exported by cross module importing (in which case the
286/// devirtualized target name will need adjustment).
288 const cl::LibraryOptionsContext &LibraryOptions,
289 ModuleSummaryIndex &Summary, std::set<GlobalValue::GUID> &ExportedGUIDs,
290 std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap,
291 DenseSet<StringRef> *ExternallyVisibleSymbolNamesPtr);
292
293/// Call after cross-module importing to update the recorded single impl
294/// devirt target names for any locals that were exported.
296 ModuleSummaryIndex &Summary,
297 function_ref<bool(StringRef, ValueInfo)> isExported,
298 std::map<ValueInfo, std::vector<VTableSlotSummary>> &LocalWPDTargetsMap,
299 DenseSet<StringRef> *ExternallyVisibleSymbolNamesPtr = nullptr);
300
301} // end namespace llvm
302
303#endif // LLVM_TRANSFORMS_IPO_WHOLEPROGRAMDEVIRT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
#define LLVM_ABI
Definition Compiler.h:211
This file defines the DenseSet and SmallDenseSet classes.
This header defines various interfaces for pass management in LLVM.
#define I(x, y, z)
Definition MD5.cpp:57
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Implements a dense probed hash-table based set.
Definition DenseSet.h:283
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An efficient, type-erasing, non-owning reference to a callable.
This namespace contains all of the command line option processing machinery.
LLVM_ABI uint64_t findLowestOffset(ArrayRef< VirtualCallTarget > Targets, bool IsAfter, uint64_t Size)
LLVM_ABI void setAfterReturnValues(MutableArrayRef< VirtualCallTarget > Targets, uint64_t AllocAfter, unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit)
LLVM_ABI void setBeforeReturnValues(MutableArrayRef< VirtualCallTarget > Targets, uint64_t AllocBefore, unsigned BitWidth, int64_t &OffsetByte, uint64_t &OffsetBit)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void updatePublicTypeTestCalls(Module &M, bool WholeProgramVisibilityEnabledInLTO, bool DevirtSpeculativelyInIndex=false)
Convert llvm.public.type.test calls to llvm.type.test when whole program visibility is available,...
LLVM_ABI void updateIndexWPDForExports(ModuleSummaryIndex &Summary, function_ref< bool(StringRef, ValueInfo)> isExported, std::map< ValueInfo, std::vector< VTableSlotSummary > > &LocalWPDTargetsMap, DenseSet< StringRef > *ExternallyVisibleSymbolNamesPtr=nullptr)
Call after cross-module importing to update the recorded single impl devirt target names for any loca...
LLVM_ABI void getVisibleToRegularObjVtableGUIDs(ModuleSummaryIndex &Index, DenseSet< GlobalValue::GUID > &VisibleToRegularObjSymbols, function_ref< bool(StringRef)> IsVisibleToRegularObj)
Based on typeID string, get all associated vtable GUIDS that are visible to regular objects.
constexpr unsigned BitWidth
LLVM_ABI bool hasWholeProgramVisibility(const cl::LibraryOptionsContext &LibraryOptions, bool WholeProgramVisibilityEnabledInLTO)
LLVM_ABI void runWholeProgramDevirtOnIndex(const cl::LibraryOptionsContext &LibraryOptions, ModuleSummaryIndex &Summary, std::set< GlobalValue::GUID > &ExportedGUIDs, std::map< ValueInfo, std::vector< VTableSlotSummary > > &LocalWPDTargetsMap, DenseSet< StringRef > *ExternallyVisibleSymbolNamesPtr)
Perform index-based whole program devirtualization on the Summary index.
LLVM_ABI void updateVCallVisibilityInModule(Module &M, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols, bool ValidateAllVtablesHaveTypeInfos, function_ref< bool(StringRef)> IsVisibleToRegularObj)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI void updateVCallVisibilityInIndex(const cl::LibraryOptionsContext &LibraryOptions, ModuleSummaryIndex &Index, bool WholeProgramVisibilityEnabledInLTO, const DenseSet< GlobalValue::GUID > &DynamicExportSymbols, const DenseSet< GlobalValue::GUID > &VisibleToRegularObjSymbols)
If whole program visibility asserted, then upgrade all public vcall visibility metadata on vtable def...
A CRTP mix-in for passes that can be skipped.
Struct that holds a reference to a particular GUID in a global value summary.
WholeProgramDevirtPass(ModuleSummaryIndex *ExportSummary, const ModuleSummaryIndex *ImportSummary, bool DevirtSpeculatively=false)
const ModuleSummaryIndex * ImportSummary
ModuleSummaryIndex * ExportSummary
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
void setBE(uint64_t Pos, uint64_t Val, uint8_t Size)
void setLE(uint64_t Pos, uint64_t Val, uint8_t Size)
std::pair< uint8_t *, uint8_t * > getPtrToData(uint64_t Pos, uint8_t Size)
bool operator<(const TypeMemberInfo &other) const
VirtualCallTarget(const TypeMemberInfo *TM, bool IsBigEndian)
LLVM_ABI VirtualCallTarget(GlobalValue *Fn, const TypeMemberInfo *TM)
void setBeforeBytes(uint64_t Pos, uint8_t Size)
void setAfterBytes(uint64_t Pos, uint8_t Size)