1 //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the opaque LLVMContextImpl.
12 //===----------------------------------------------------------------------===//
14 #include "LLVMContextImpl.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 : 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();
50 // Drop references for MDNodes. Do this before Values get deleted to avoid
51 // unnecessary RAUW when nodes are still unresolved.
52 for (auto *I : DistinctMDNodes)
53 I->dropAllReferences();
54 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
55 for (auto *I : CLASS##s) \
56 I->dropAllReferences();
57 #include "llvm/IR/Metadata.def"
59 // Also drop references that come from the Value bridges.
60 for (auto &Pair : ValuesAsMetadata)
61 Pair.second->dropUsers();
62 for (auto &Pair : MetadataAsValues)
63 Pair.second->dropUse();
66 for (MDNode *I : DistinctMDNodes)
67 I->deleteAsSubclass();
68 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
69 for (CLASS * I : CLASS##s) \
71 #include "llvm/IR/Metadata.def"
73 // Free the constants.
74 for (auto *I : ExprConstants)
75 I->dropAllReferences();
76 for (auto *I : ArrayConstants)
77 I->dropAllReferences();
78 for (auto *I : StructConstants)
79 I->dropAllReferences();
80 for (auto *I : VectorConstants)
81 I->dropAllReferences();
82 ExprConstants.freeConstants();
83 ArrayConstants.freeConstants();
84 StructConstants.freeConstants();
85 VectorConstants.freeConstants();
86 InlineAsms.freeConstants();
94 for (auto &CDSConstant : CDSConstants)
95 delete CDSConstant.second;
98 // Destroy attributes.
99 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
100 E = AttrsSet.end(); I != E; ) {
101 FoldingSetIterator<AttributeImpl> Elem = I++;
105 // Destroy attribute lists.
106 for (FoldingSetIterator<AttributeListImpl> I = AttrsLists.begin(),
107 E = AttrsLists.end();
109 FoldingSetIterator<AttributeListImpl> Elem = I++;
113 // Destroy attribute node lists.
114 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
115 E = AttrsSetNodes.end(); I != E; ) {
116 FoldingSetIterator<AttributeSetNode> Elem = I++;
120 // Destroy MetadataAsValues.
122 SmallVector<MetadataAsValue *, 8> MDVs;
123 MDVs.reserve(MetadataAsValues.size());
124 for (auto &Pair : MetadataAsValues)
125 MDVs.push_back(Pair.second);
126 MetadataAsValues.clear();
131 // Destroy ValuesAsMetadata.
132 for (auto &Pair : ValuesAsMetadata)
136 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
141 for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
143 if (C->use_empty()) {
145 C->destroyConstant();
151 void Module::dropTriviallyDeadConstantArrays() {
152 Context.pImpl->dropTriviallyDeadConstantArrays();
157 /// \brief Make MDOperand transparent for hashing.
159 /// This overload of an implementation detail of the hashing library makes
160 /// MDOperand hash to the same value as a \a Metadata pointer.
162 /// Note that overloading \a hash_value() as follows:
165 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
168 /// does not cause MDOperand to be transparent. In particular, a bare pointer
169 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
170 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
172 } // end namespace llvm
174 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
175 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
178 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
179 unsigned RawHash = calculateHash(MDs);
180 assert(Hash == RawHash &&
181 "Expected hash of MDOperand to equal hash of Metadata*");
187 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
188 return hash_combine_range(Ops.begin(), Ops.end());
191 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
192 uint32_t NewIdx = BundleTagCache.size();
193 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
196 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
197 Tags.resize(BundleTagCache.size());
198 for (const auto &T : BundleTagCache)
199 Tags[T.second] = T.first();
202 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
203 auto I = BundleTagCache.find(Tag);
204 assert(I != BundleTagCache.end() && "Unknown tag!");
208 /// Singleton instance of the OptBisect class.
210 /// This singleton is accessed via the LLVMContext::getOptBisect() function. It
211 /// provides a mechanism to disable passes and individual optimizations at
212 /// compile time based on a command line option (-opt-bisect-limit) in order to
213 /// perform a bisecting search for optimization-related problems.
215 /// Even if multiple LLVMContext objects are created, they will all return the
216 /// same instance of OptBisect in order to provide a single bisect count. Any
217 /// code that uses the OptBisect object should be serialized when bisection is
218 /// enabled in order to enable a consistent bisect count.
219 static ManagedStatic<OptBisect> OptBisector;
221 OptBisect &LLVMContextImpl::getOptBisect() {