1 //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the opaque LLVMContextImpl.
11 //===----------------------------------------------------------------------===//
13 #include "LLVMContextImpl.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/OptBisect.h"
16 #include "llvm/IR/Type.h"
17 #include "llvm/Support/ManagedStatic.h"
23 LLVMContextImpl::LLVMContextImpl(LLVMContext
&C
)
24 : DiagHandler(llvm::make_unique
<DiagnosticHandler
>()),
25 VoidTy(C
, Type::VoidTyID
),
26 LabelTy(C
, Type::LabelTyID
),
27 HalfTy(C
, Type::HalfTyID
),
28 FloatTy(C
, Type::FloatTyID
),
29 DoubleTy(C
, Type::DoubleTyID
),
30 MetadataTy(C
, Type::MetadataTyID
),
31 TokenTy(C
, Type::TokenTyID
),
32 X86_FP80Ty(C
, Type::X86_FP80TyID
),
33 FP128Ty(C
, Type::FP128TyID
),
34 PPC_FP128Ty(C
, Type::PPC_FP128TyID
),
35 X86_MMXTy(C
, Type::X86_MMXTyID
),
43 LLVMContextImpl::~LLVMContextImpl() {
44 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
45 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
46 // the container. Avoid iterators during this operation:
47 while (!OwnedModules
.empty())
48 delete *OwnedModules
.begin();
51 // Check for metadata references from leaked Instructions.
52 for (auto &Pair
: InstructionMetadata
)
54 assert(InstructionMetadata
.empty() &&
55 "Instructions with metadata have been leaked");
58 // Drop references for MDNodes. Do this before Values get deleted to avoid
59 // unnecessary RAUW when nodes are still unresolved.
60 for (auto *I
: DistinctMDNodes
)
61 I
->dropAllReferences();
62 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
63 for (auto *I : CLASS##s) \
64 I->dropAllReferences();
65 #include "llvm/IR/Metadata.def"
67 // Also drop references that come from the Value bridges.
68 for (auto &Pair
: ValuesAsMetadata
)
69 Pair
.second
->dropUsers();
70 for (auto &Pair
: MetadataAsValues
)
71 Pair
.second
->dropUse();
74 for (MDNode
*I
: DistinctMDNodes
)
75 I
->deleteAsSubclass();
76 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
77 for (CLASS * I : CLASS##s) \
79 #include "llvm/IR/Metadata.def"
81 // Free the constants.
82 for (auto *I
: ExprConstants
)
83 I
->dropAllReferences();
84 for (auto *I
: ArrayConstants
)
85 I
->dropAllReferences();
86 for (auto *I
: StructConstants
)
87 I
->dropAllReferences();
88 for (auto *I
: VectorConstants
)
89 I
->dropAllReferences();
90 ExprConstants
.freeConstants();
91 ArrayConstants
.freeConstants();
92 StructConstants
.freeConstants();
93 VectorConstants
.freeConstants();
94 InlineAsms
.freeConstants();
102 for (auto &CDSConstant
: CDSConstants
)
103 delete CDSConstant
.second
;
104 CDSConstants
.clear();
106 // Destroy attributes.
107 for (FoldingSetIterator
<AttributeImpl
> I
= AttrsSet
.begin(),
108 E
= AttrsSet
.end(); I
!= E
; ) {
109 FoldingSetIterator
<AttributeImpl
> Elem
= I
++;
113 // Destroy attribute lists.
114 for (FoldingSetIterator
<AttributeListImpl
> I
= AttrsLists
.begin(),
115 E
= AttrsLists
.end();
117 FoldingSetIterator
<AttributeListImpl
> Elem
= I
++;
121 // Destroy attribute node lists.
122 for (FoldingSetIterator
<AttributeSetNode
> I
= AttrsSetNodes
.begin(),
123 E
= AttrsSetNodes
.end(); I
!= E
; ) {
124 FoldingSetIterator
<AttributeSetNode
> Elem
= I
++;
128 // Destroy MetadataAsValues.
130 SmallVector
<MetadataAsValue
*, 8> MDVs
;
131 MDVs
.reserve(MetadataAsValues
.size());
132 for (auto &Pair
: MetadataAsValues
)
133 MDVs
.push_back(Pair
.second
);
134 MetadataAsValues
.clear();
139 // Destroy ValuesAsMetadata.
140 for (auto &Pair
: ValuesAsMetadata
)
144 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
149 for (auto I
= ArrayConstants
.begin(), E
= ArrayConstants
.end(); I
!= E
;) {
151 if (C
->use_empty()) {
153 C
->destroyConstant();
159 void Module::dropTriviallyDeadConstantArrays() {
160 Context
.pImpl
->dropTriviallyDeadConstantArrays();
165 /// Make MDOperand transparent for hashing.
167 /// This overload of an implementation detail of the hashing library makes
168 /// MDOperand hash to the same value as a \a Metadata pointer.
170 /// Note that overloading \a hash_value() as follows:
173 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
176 /// does not cause MDOperand to be transparent. In particular, a bare pointer
177 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
178 static const Metadata
*get_hashable_data(const MDOperand
&X
) { return X
.get(); }
180 } // end namespace llvm
182 unsigned MDNodeOpsKey::calculateHash(MDNode
*N
, unsigned Offset
) {
183 unsigned Hash
= hash_combine_range(N
->op_begin() + Offset
, N
->op_end());
186 SmallVector
<Metadata
*, 8> MDs(N
->op_begin() + Offset
, N
->op_end());
187 unsigned RawHash
= calculateHash(MDs
);
188 assert(Hash
== RawHash
&&
189 "Expected hash of MDOperand to equal hash of Metadata*");
195 unsigned MDNodeOpsKey::calculateHash(ArrayRef
<Metadata
*> Ops
) {
196 return hash_combine_range(Ops
.begin(), Ops
.end());
199 StringMapEntry
<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag
) {
200 uint32_t NewIdx
= BundleTagCache
.size();
201 return &*(BundleTagCache
.insert(std::make_pair(Tag
, NewIdx
)).first
);
204 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl
<StringRef
> &Tags
) const {
205 Tags
.resize(BundleTagCache
.size());
206 for (const auto &T
: BundleTagCache
)
207 Tags
[T
.second
] = T
.first();
210 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag
) const {
211 auto I
= BundleTagCache
.find(Tag
);
212 assert(I
!= BundleTagCache
.end() && "Unknown tag!");
216 SyncScope::ID
LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN
) {
217 auto NewSSID
= SSC
.size();
218 assert(NewSSID
< std::numeric_limits
<SyncScope::ID
>::max() &&
219 "Hit the maximum number of synchronization scopes allowed!");
220 return SSC
.insert(std::make_pair(SSN
, SyncScope::ID(NewSSID
))).first
->second
;
223 void LLVMContextImpl::getSyncScopeNames(
224 SmallVectorImpl
<StringRef
> &SSNs
) const {
225 SSNs
.resize(SSC
.size());
226 for (const auto &SSE
: SSC
)
227 SSNs
[SSE
.second
] = SSE
.first();
230 /// Singleton instance of the OptBisect class.
232 /// This singleton is accessed via the LLVMContext::getOptPassGate() function.
233 /// It provides a mechanism to disable passes and individual optimizations at
234 /// compile time based on a command line option (-opt-bisect-limit) in order to
235 /// perform a bisecting search for optimization-related problems.
237 /// Even if multiple LLVMContext objects are created, they will all return the
238 /// same instance of OptBisect in order to provide a single bisect count. Any
239 /// code that uses the OptBisect object should be serialized when bisection is
240 /// enabled in order to enable a consistent bisect count.
241 static ManagedStatic
<OptBisect
> OptBisector
;
243 OptPassGate
&LLVMContextImpl::getOptPassGate() const {
245 OPG
= &(*OptBisector
);
249 void LLVMContextImpl::setOptPassGate(OptPassGate
& OPG
) {