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/ADT/SetVector.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/OptBisect.h"
17 #include "llvm/IR/Type.h"
18 #include "llvm/Support/ManagedStatic.h"
24 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
25 : DiagHandler(std::make_unique<DiagnosticHandler>()),
26 VoidTy(C, Type::VoidTyID),
27 LabelTy(C, Type::LabelTyID),
28 HalfTy(C, Type::HalfTyID),
29 BFloatTy(C, Type::BFloatTyID),
30 FloatTy(C, Type::FloatTyID),
31 DoubleTy(C, Type::DoubleTyID),
32 MetadataTy(C, Type::MetadataTyID),
33 TokenTy(C, Type::TokenTyID),
34 X86_FP80Ty(C, Type::X86_FP80TyID),
35 FP128Ty(C, Type::FP128TyID),
36 PPC_FP128Ty(C, Type::PPC_FP128TyID),
37 X86_MMXTy(C, Type::X86_MMXTyID),
45 LLVMContextImpl::~LLVMContextImpl() {
46 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
47 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
48 // the container. Avoid iterators during this operation:
49 while (!OwnedModules.empty())
50 delete *OwnedModules.begin();
53 // Check for metadata references from leaked Instructions.
54 for (auto &Pair : InstructionMetadata)
56 assert(InstructionMetadata.empty() &&
57 "Instructions with metadata have been leaked");
60 // Drop references for MDNodes. Do this before Values get deleted to avoid
61 // unnecessary RAUW when nodes are still unresolved.
62 for (auto *I : DistinctMDNodes)
63 I->dropAllReferences();
64 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
65 for (auto *I : CLASS##s) \
66 I->dropAllReferences();
67 #include "llvm/IR/Metadata.def"
69 // Also drop references that come from the Value bridges.
70 for (auto &Pair : ValuesAsMetadata)
71 Pair.second->dropUsers();
72 for (auto &Pair : MetadataAsValues)
73 Pair.second->dropUse();
76 for (MDNode *I : DistinctMDNodes)
77 I->deleteAsSubclass();
78 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
79 for (CLASS * I : CLASS##s) \
81 #include "llvm/IR/Metadata.def"
83 // Free the constants.
84 for (auto *I : ExprConstants)
85 I->dropAllReferences();
86 for (auto *I : ArrayConstants)
87 I->dropAllReferences();
88 for (auto *I : StructConstants)
89 I->dropAllReferences();
90 for (auto *I : VectorConstants)
91 I->dropAllReferences();
92 ExprConstants.freeConstants();
93 ArrayConstants.freeConstants();
94 StructConstants.freeConstants();
95 VectorConstants.freeConstants();
96 InlineAsms.freeConstants();
101 IntConstants.clear();
104 for (auto &CDSConstant : CDSConstants)
105 delete CDSConstant.second;
106 CDSConstants.clear();
108 // Destroy attribute node lists.
109 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
110 E = AttrsSetNodes.end(); I != E; ) {
111 FoldingSetIterator<AttributeSetNode> Elem = I++;
115 // Destroy MetadataAsValues.
117 SmallVector<MetadataAsValue *, 8> MDVs;
118 MDVs.reserve(MetadataAsValues.size());
119 for (auto &Pair : MetadataAsValues)
120 MDVs.push_back(Pair.second);
121 MetadataAsValues.clear();
126 // Destroy ValuesAsMetadata.
127 for (auto &Pair : ValuesAsMetadata)
131 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
132 SmallSetVector<ConstantArray *, 4> WorkList(ArrayConstants.begin(),
133 ArrayConstants.end());
135 while (!WorkList.empty()) {
136 ConstantArray *C = WorkList.pop_back_val();
137 if (C->use_empty()) {
138 for (const Use &Op : C->operands()) {
139 if (auto *COp = dyn_cast<ConstantArray>(Op))
140 WorkList.insert(COp);
142 C->destroyConstant();
147 void Module::dropTriviallyDeadConstantArrays() {
148 Context.pImpl->dropTriviallyDeadConstantArrays();
153 /// Make MDOperand transparent for hashing.
155 /// This overload of an implementation detail of the hashing library makes
156 /// MDOperand hash to the same value as a \a Metadata pointer.
158 /// Note that overloading \a hash_value() as follows:
161 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
164 /// does not cause MDOperand to be transparent. In particular, a bare pointer
165 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
166 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
168 } // end namespace llvm
170 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
171 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
174 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
175 unsigned RawHash = calculateHash(MDs);
176 assert(Hash == RawHash &&
177 "Expected hash of MDOperand to equal hash of Metadata*");
183 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
184 return hash_combine_range(Ops.begin(), Ops.end());
187 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
188 uint32_t NewIdx = BundleTagCache.size();
189 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
192 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
193 Tags.resize(BundleTagCache.size());
194 for (const auto &T : BundleTagCache)
195 Tags[T.second] = T.first();
198 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
199 auto I = BundleTagCache.find(Tag);
200 assert(I != BundleTagCache.end() && "Unknown tag!");
204 SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
205 auto NewSSID = SSC.size();
206 assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
207 "Hit the maximum number of synchronization scopes allowed!");
208 return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
211 void LLVMContextImpl::getSyncScopeNames(
212 SmallVectorImpl<StringRef> &SSNs) const {
213 SSNs.resize(SSC.size());
214 for (const auto &SSE : SSC)
215 SSNs[SSE.second] = SSE.first();
218 /// Singleton instance of the OptBisect class.
220 /// This singleton is accessed via the LLVMContext::getOptPassGate() function.
221 /// It provides a mechanism to disable passes and individual optimizations at
222 /// compile time based on a command line option (-opt-bisect-limit) in order to
223 /// perform a bisecting search for optimization-related problems.
225 /// Even if multiple LLVMContext objects are created, they will all return the
226 /// same instance of OptBisect in order to provide a single bisect count. Any
227 /// code that uses the OptBisect object should be serialized when bisection is
228 /// enabled in order to enable a consistent bisect count.
229 static ManagedStatic<OptBisect> OptBisector;
231 OptPassGate &LLVMContextImpl::getOptPassGate() const {
233 OPG = &(*OptBisector);
237 void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {