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/ADT/STLExtras.h"
16 #include "llvm/IR/Attributes.h"
17 #include "llvm/IR/DiagnosticInfo.h"
18 #include "llvm/IR/Module.h"
22 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
23 : TheTrueVal(nullptr), TheFalseVal(nullptr),
24 TheNoneToken(nullptr),
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),
42 InlineAsmDiagHandler = nullptr;
43 InlineAsmDiagContext = nullptr;
44 DiagnosticHandler = nullptr;
45 DiagnosticContext = nullptr;
46 RespectDiagnosticFilters = false;
47 YieldCallback = nullptr;
48 YieldOpaqueHandle = nullptr;
49 NamedStructTypesUniqueID = 0;
53 struct DropReferences {
54 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
56 template <typename PairT> void operator()(const PairT &P) {
57 P.second->dropAllReferences();
61 // Temporary - drops pair.first instead of second.
63 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
65 template<typename PairT>
66 void operator()(const PairT &P) {
67 P.first->dropAllReferences();
72 LLVMContextImpl::~LLVMContextImpl() {
73 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
74 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
75 // the container. Avoid iterators during this operation:
76 while (!OwnedModules.empty())
77 delete *OwnedModules.begin();
79 // Drop references for MDNodes. Do this before Values get deleted to avoid
80 // unnecessary RAUW when nodes are still unresolved.
81 for (auto *I : DistinctMDNodes)
82 I->dropAllReferences();
83 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
84 for (auto *I : CLASS##s) \
85 I->dropAllReferences();
86 #include "llvm/IR/Metadata.def"
88 // Also drop references that come from the Value bridges.
89 for (auto &Pair : ValuesAsMetadata)
90 Pair.second->dropUsers();
91 for (auto &Pair : MetadataAsValues)
92 Pair.second->dropUse();
95 for (MDNode *I : DistinctMDNodes)
96 I->deleteAsSubclass();
97 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
98 for (CLASS * I : CLASS##s) \
100 #include "llvm/IR/Metadata.def"
102 // Free the constants.
103 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
105 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
107 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
109 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
111 ExprConstants.freeConstants();
112 ArrayConstants.freeConstants();
113 StructConstants.freeConstants();
114 VectorConstants.freeConstants();
115 DeleteContainerSeconds(CAZConstants);
116 DeleteContainerSeconds(CPNConstants);
117 DeleteContainerSeconds(UVConstants);
118 InlineAsms.freeConstants();
119 DeleteContainerSeconds(IntConstants);
120 DeleteContainerSeconds(FPConstants);
122 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
123 E = CDSConstants.end(); I != E; ++I)
125 CDSConstants.clear();
127 // Destroy attributes.
128 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
129 E = AttrsSet.end(); I != E; ) {
130 FoldingSetIterator<AttributeImpl> Elem = I++;
134 // Destroy attribute lists.
135 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
136 E = AttrsLists.end(); I != E; ) {
137 FoldingSetIterator<AttributeSetImpl> Elem = I++;
141 // Destroy attribute node lists.
142 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
143 E = AttrsSetNodes.end(); I != E; ) {
144 FoldingSetIterator<AttributeSetNode> Elem = I++;
148 // Destroy MetadataAsValues.
150 SmallVector<MetadataAsValue *, 8> MDVs;
151 MDVs.reserve(MetadataAsValues.size());
152 for (auto &Pair : MetadataAsValues)
153 MDVs.push_back(Pair.second);
154 MetadataAsValues.clear();
159 // Destroy ValuesAsMetadata.
160 for (auto &Pair : ValuesAsMetadata)
163 // Destroy MDStrings.
164 MDStringCache.clear();
167 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
172 for (auto I = ArrayConstants.map_begin(), E = ArrayConstants.map_end();
176 if (C->use_empty()) {
178 C->destroyConstant();
185 void Module::dropTriviallyDeadConstantArrays() {
186 Context.pImpl->dropTriviallyDeadConstantArrays();
190 /// \brief Make MDOperand transparent for hashing.
192 /// This overload of an implementation detail of the hashing library makes
193 /// MDOperand hash to the same value as a \a Metadata pointer.
195 /// Note that overloading \a hash_value() as follows:
198 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
201 /// does not cause MDOperand to be transparent. In particular, a bare pointer
202 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
203 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
206 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
207 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
210 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
211 unsigned RawHash = calculateHash(MDs);
212 assert(Hash == RawHash &&
213 "Expected hash of MDOperand to equal hash of Metadata*");
219 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
220 return hash_combine_range(Ops.begin(), Ops.end());
223 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
224 uint32_t NewIdx = BundleTagCache.size();
225 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
228 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
229 Tags.resize(BundleTagCache.size());
230 for (const auto &T : BundleTagCache)
231 Tags[T.second] = T.first();
234 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
235 auto I = BundleTagCache.find(Tag);
236 assert(I != BundleTagCache.end() && "Unknown tag!");
240 // ConstantsContext anchors
241 void UnaryConstantExpr::anchor() { }
243 void BinaryConstantExpr::anchor() { }
245 void SelectConstantExpr::anchor() { }
247 void ExtractElementConstantExpr::anchor() { }
249 void InsertElementConstantExpr::anchor() { }
251 void ShuffleVectorConstantExpr::anchor() { }
253 void ExtractValueConstantExpr::anchor() { }
255 void InsertValueConstantExpr::anchor() { }
257 void GetElementPtrConstantExpr::anchor() { }
259 void CompareConstantExpr::anchor() { }