LLVM 24.0.0git
LLVMContextImpl.cpp
Go to the documentation of this file.
1//===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
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 implements the opaque LLVMContextImpl.
10//
11//===----------------------------------------------------------------------===//
12
13#include "LLVMContextImpl.h"
14#include "AttributeImpl.h"
18#include "llvm/IR/Module.h"
19#include "llvm/IR/OptBisect.h"
20#include "llvm/IR/Type.h"
21#include "llvm/IR/User.h"
25#include <cassert>
26
27using namespace llvm;
28
40
42 SmallVectorImpl<MDNode *> &Nodes) const {
43 Nodes.append(DistinctMDNodes.begin(), DistinctMDNodes.end());
44#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
45 Nodes.append(CLASS##s.begin(), CLASS##s.end());
46#include "llvm/IR/Metadata.def"
47 Nodes.append(TemporaryMDNodes.begin(), TemporaryMDNodes.end());
48}
49
51#ifndef NDEBUG
52 // Check that any variable location records that fell off the end of a block
53 // when it's terminator was removed were eventually replaced. This assertion
54 // firing indicates that DbgVariableRecords went missing during the lifetime
55 // of the LLVMContext.
56 assert(TrailingDbgRecords.empty() && "DbgRecords in blocks not cleaned");
57#endif
58
59 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
60 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
61 // the container. Avoid iterators during this operation:
62 while (!OwnedModules.empty())
63 delete *OwnedModules.begin();
64
65#ifndef NDEBUG
66 // Check for metadata references from leaked Values.
67 assert((Metadatas.empty() || MetadataRecycleSize + 1 == Metadatas.size()) &&
68 "Values with metadata have been leaked");
69#endif
70
71 // Drop references for MDNodes. Do this before Values get deleted to avoid
72 // unnecessary RAUW when nodes are still unresolved.
73 for (auto *I : DistinctMDNodes)
74 I->dropAllReferences();
75#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
76 for (auto *I : CLASS##s) \
77 I->dropAllReferences();
78#include "llvm/IR/Metadata.def"
79
80 // Also drop references that come from the Value bridges.
81 for (auto &Pair : ValuesAsMetadata)
82 Pair.second->dropUsers();
83 for (auto &Pair : MetadataAsValues)
84 Pair.second->dropUse();
85 // Do not untrack ValueAsMetadata references for DIArgLists, as they have
86 // already been more efficiently untracked above.
87 for (DIArgList *AL : DIArgLists) {
88 AL->dropAllReferences(/* Untrack */ false);
89 delete AL;
90 }
91 DIArgLists.clear();
92
93 // Destroy MDNodes.
94 for (MDNode *I : DistinctMDNodes)
95 I->deleteAsSubclass();
96
97 for (auto *ConstantRangeListAttribute : ConstantRangeListAttributes)
98 ConstantRangeListAttribute->~ConstantRangeListAttributeImpl();
99#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
100 for (CLASS * I : CLASS##s) \
101 delete I;
102#include "llvm/IR/Metadata.def"
103
104 // Free the constants.
105 for (auto *I : ExprConstants)
106 I->dropAllReferences();
107 for (auto *I : ArrayConstants)
108 I->dropAllReferences();
109 for (auto *I : StructConstants)
110 I->dropAllReferences();
111 for (auto *I : VectorConstants)
112 I->dropAllReferences();
113 ExprConstants.freeConstants();
114 ArrayConstants.freeConstants();
115 StructConstants.freeConstants();
116 VectorConstants.freeConstants();
117 ConstantPtrAuths.freeConstants();
118 InlineAsms.freeConstants();
119
120 CAZConstants.clear();
121 CPNConstants.clear();
122 CTNConstants.clear();
123 UVConstants.clear();
124 PVConstants.clear();
125 IntZeroConstants.clear();
126 IntOneConstants.clear();
127 IntConstants.clear();
128 IntSplatConstants.clear();
129 ByteZeroConstants.clear();
130 ByteOneConstants.clear();
131 ByteConstants.clear();
132 ByteSplatConstants.clear();
133 FPConstants.clear();
134 FPSplatConstants.clear();
135 CDSConstants.clear();
136
137 // Destroy attribute node lists.
139 E = AttrsSetNodes.end(); I != E; ) {
141 delete &*Elem;
142 }
143
144 // Destroy MetadataAsValues.
145 {
147 MDVs.reserve(MetadataAsValues.size());
148 for (auto &Pair : MetadataAsValues)
149 MDVs.push_back(Pair.second);
150 MetadataAsValues.clear();
151 for (auto *V : MDVs)
152 delete V;
153 }
154
155 // Destroy ValuesAsMetadata.
156 for (auto &Pair : ValuesAsMetadata)
157 delete Pair.second;
158}
159
160namespace llvm {
161
162/// Make MDOperand transparent for hashing.
163///
164/// This overload of an implementation detail of the hashing library makes
165/// MDOperand hash to the same value as a \a Metadata pointer.
166///
167/// Note that overloading \a hash_value() as follows:
168///
169/// \code
170/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
171/// \endcode
172///
173/// does not cause MDOperand to be transparent. In particular, a bare pointer
174/// doesn't get hashed before it's combined, whereas \a MDOperand would.
175static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
176
177} // end namespace llvm
178
180 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
181#ifndef NDEBUG
182 {
184 unsigned RawHash = calculateHash(MDs);
185 assert(Hash == RawHash &&
186 "Expected hash of MDOperand to equal hash of Metadata*");
187 }
188#endif
189 return Hash;
190}
191
193 return hash_combine_range(Ops);
194}
195
197 uint32_t NewIdx = BundleTagCache.size();
198 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
199}
200
202 Tags.resize(BundleTagCache.size());
203 for (const auto &T : BundleTagCache)
204 Tags[T.second] = T.first();
205}
206
208 auto I = BundleTagCache.find(Tag);
209 assert(I != BundleTagCache.end() && "Unknown tag!");
210 return I->second;
211}
212
214 auto NewSSID = SSC.size();
215 assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
216 "Hit the maximum number of synchronization scopes allowed!");
217 return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
218}
219
221 SmallVectorImpl<StringRef> &SSNs) const {
222 SSNs.resize(SSC.size());
223 for (const auto &SSE : SSC)
224 SSNs[SSE.second] = SSE.first();
225}
226
227std::optional<StringRef>
229 for (const auto &SSE : SSC) {
230 if (SSE.second != Id)
231 continue;
232 return SSE.first();
233 }
234 return std::nullopt;
235}
236
237/// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
238/// singleton OptBisect if not explicitly set.
240 if (!OPG)
242 return *OPG;
243}
244
246 this->OPG = &OPG;
247}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMapEntry class - it is intended to be a low dependency implementation det...
This file defines various helper methods and classes used by LLVMContextImpl for creating and managin...
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
Module.h This file contains the declarations for the Module class.
@ X86_FP80TyID
80-bit floating point type (X87)
@ X86_AMXTyID
AMX vectors (8192 bits, X86 specific)
@ FP128TyID
128-bit floating point type (112-bit significand)
@ LabelTyID
Labels.
@ TokenTyID
Tokens.
@ PPC_FP128TyID
128-bit floating point type (two 64-bits, PowerPC)
@ DoubleTyID
64-bit floating point type
@ HalfTyID
16-bit floating point type
@ BFloatTyID
16-bit floating point type (7-bit significand)
@ FloatTyID
32-bit floating point type
@ VoidTyID
type with no size
@ MetadataTyID
Metadata.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define I(x, y, z)
Definition MD5.cpp:57
#define T
This file declares the interface for bisecting optimizations.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
List of ValueAsMetadata, to be used as an argument to a dbg.value intrinsic.
Forward iterator for FoldingSet and ContextualFoldingSet.
Definition FoldingSet.h:323
DenseMap< unsigned, std::unique_ptr< ConstantInt > > IntOneConstants
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
DenseMap< unsigned, std::unique_ptr< ConstantInt > > IntZeroConstants
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
SmallVector< MDAttachment, 0 > Metadatas
Collection of metadata attachments in this context.
DenseMap< Type *, std::unique_ptr< ConstantPointerNull > > CPNConstants
DenseMap< std::pair< ElementCount, APInt >, std::unique_ptr< ConstantByte > > ByteSplatConstants
DenseMap< APFloat, std::unique_ptr< ConstantFP > > FPConstants
SmallPtrSet< Module *, 4 > OwnedModules
OwnedModules - The set of modules instantiated in this context, and which will be automatically delet...
DenseMap< Type *, std::unique_ptr< ConstantAggregateZero > > CAZConstants
DenseMap< Type *, std::unique_ptr< PoisonValue > > PVConstants
DenseMap< APInt, std::unique_ptr< ConstantInt > > IntConstants
std::vector< MDNode * > DistinctMDNodes
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID.
void setOptPassGate(OptPassGate &)
Set the object which can disable optional passes and individual optimizations at compile time.
VectorConstantsTy VectorConstants
DenseMap< Type *, std::unique_ptr< UndefValue > > UVConstants
OptPassGate & getOptPassGate() const
Access the object which can disable optional passes and individual optimizations at compile time.
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef Tag)
std::unique_ptr< DiagnosticHandler > DiagHandler
StringMap< uint32_t > BundleTagCache
A set of interned tags for operand bundles.
StringMap< std::unique_ptr< ConstantDataSequential > > CDSConstants
StructConstantsTy StructConstants
void getOperandBundleTags(SmallVectorImpl< StringRef > &Tags) const
void getAllMetadataNodes(SmallVectorImpl< MDNode * > &Nodes) const
FoldingSet< AttributeSetNode > AttrsSetNodes
ConstantUniqueMap< ConstantPtrAuth > ConstantPtrAuths
DenseMap< TargetExtType *, std::unique_ptr< ConstantTargetNone > > CTNConstants
ConstantUniqueMap< ConstantExpr > ExprConstants
uint32_t getOperandBundleTagID(StringRef Tag) const
StringMap< SyncScope::ID > SSC
A set of interned synchronization scopes.
DenseMap< std::pair< ElementCount, APInt >, std::unique_ptr< ConstantInt > > IntSplatConstants
DenseMap< unsigned, std::unique_ptr< ConstantByte > > ByteOneConstants
std::vector< ConstantRangeListAttributeImpl * > ConstantRangeListAttributes
DenseSet< DIArgList *, DIArgListInfo > DIArgLists
std::optional< StringRef > getSyncScopeName(SyncScope::ID Id) const
getSyncScopeName - Returns the name of a SyncScope::ID registered with LLVMContext,...
ArrayConstantsTy ArrayConstants
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
ConstantUniqueMap< InlineAsm > InlineAsms
DenseSet< MDNode * > TemporaryMDNodes
LLVMContextImpl(LLVMContext &C)
DenseMap< unsigned, std::unique_ptr< ConstantByte > > ByteZeroConstants
DenseMap< APInt, std::unique_ptr< ConstantByte > > ByteConstants
SmallDenseMap< BasicBlock *, DbgMarker * > TrailingDbgRecords
Mapping of blocks to collections of "trailing" DbgVariableRecords.
DenseMap< std::pair< ElementCount, APFloat >, std::unique_ptr< ConstantFP > > FPSplatConstants
unsigned MetadataRecycleSize
Number of currently unused metadata entries.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static unsigned calculateHash(MDNode *N, unsigned Offset=0)
Metadata node.
Definition Metadata.h:1069
Tracking metadata reference owned by Metadata.
Definition Metadata.h:891
Root of the metadata hierarchy.
Definition Metadata.h:64
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition OptBisect.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
@ Offset
Definition DWP.cpp:577
static const Metadata * get_hashable_data(const MDOperand &X)
Make MDOperand transparent for hashing.
LLVM_ABI OptPassGate & getGlobalPassGate()
Singleton instance of the OptPassGate class, so multiple pass managers don't need to coordinate their...
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:285
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
#define N
This is the base class for diagnostic handling in LLVM.