[Demangle] Add a few more options to the microsoft demangler
[llvm-complete.git] / lib / IR / LLVMContextImpl.cpp
blob5f978271417059630ae57fc7cb736e03056ece09
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.
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"
18 #include <cassert>
19 #include <utility>
21 using namespace llvm;
23 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
24 : DiagHandler(std::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),
36 Int1Ty(C, 1),
37 Int8Ty(C, 8),
38 Int16Ty(C, 16),
39 Int32Ty(C, 32),
40 Int64Ty(C, 64),
41 Int128Ty(C, 128) {}
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();
50 #ifndef NDEBUG
51 // Check for metadata references from leaked Instructions.
52 for (auto &Pair : InstructionMetadata)
53 Pair.first->dump();
54 assert(InstructionMetadata.empty() &&
55 "Instructions with metadata have been leaked");
56 #endif
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();
73 // Destroy MDNodes.
74 for (MDNode *I : DistinctMDNodes)
75 I->deleteAsSubclass();
76 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
77 for (CLASS * I : CLASS##s) \
78 delete I;
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();
96 CAZConstants.clear();
97 CPNConstants.clear();
98 UVConstants.clear();
99 IntConstants.clear();
100 FPConstants.clear();
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++;
110 delete &*Elem;
113 // Destroy attribute lists.
114 for (FoldingSetIterator<AttributeListImpl> I = AttrsLists.begin(),
115 E = AttrsLists.end();
116 I != E;) {
117 FoldingSetIterator<AttributeListImpl> Elem = I++;
118 delete &*Elem;
121 // Destroy attribute node lists.
122 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
123 E = AttrsSetNodes.end(); I != E; ) {
124 FoldingSetIterator<AttributeSetNode> Elem = I++;
125 delete &*Elem;
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();
135 for (auto *V : MDVs)
136 delete V;
139 // Destroy ValuesAsMetadata.
140 for (auto &Pair : ValuesAsMetadata)
141 delete Pair.second;
144 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
145 bool Changed;
146 do {
147 Changed = false;
149 for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
150 auto *C = *I++;
151 if (C->use_empty()) {
152 Changed = true;
153 C->destroyConstant();
156 } while (Changed);
159 void Module::dropTriviallyDeadConstantArrays() {
160 Context.pImpl->dropTriviallyDeadConstantArrays();
163 namespace llvm {
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:
172 /// \code
173 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
174 /// \endcode
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());
184 #ifndef NDEBUG
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*");
191 #endif
192 return Hash;
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!");
213 return I->second;
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 {
244 if (!OPG)
245 OPG = &(*OptBisector);
246 return *OPG;
249 void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
250 this->OPG = &OPG;