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 VoidTy(C, Type::VoidTyID),
25 LabelTy(C, Type::LabelTyID),
26 HalfTy(C, Type::HalfTyID),
27 FloatTy(C, Type::FloatTyID),
28 DoubleTy(C, Type::DoubleTyID),
29 MetadataTy(C, Type::MetadataTyID),
30 X86_FP80Ty(C, Type::X86_FP80TyID),
31 FP128Ty(C, Type::FP128TyID),
32 PPC_FP128Ty(C, Type::PPC_FP128TyID),
33 X86_MMXTy(C, Type::X86_MMXTyID),
40 InlineAsmDiagHandler = nullptr;
41 InlineAsmDiagContext = nullptr;
42 DiagnosticHandler = nullptr;
43 DiagnosticContext = nullptr;
44 RespectDiagnosticFilters = false;
45 YieldCallback = nullptr;
46 YieldOpaqueHandle = nullptr;
47 NamedStructTypesUniqueID = 0;
51 struct DropReferences {
52 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
54 template <typename PairT> void operator()(const PairT &P) {
55 P.second->dropAllReferences();
59 // Temporary - drops pair.first instead of second.
61 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
63 template<typename PairT>
64 void operator()(const PairT &P) {
65 P.first->dropAllReferences();
70 LLVMContextImpl::~LLVMContextImpl() {
71 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
72 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
73 // the container. Avoid iterators during this operation:
74 while (!OwnedModules.empty())
75 delete *OwnedModules.begin();
77 // Drop references for MDNodes. Do this before Values get deleted to avoid
78 // unnecessary RAUW when nodes are still unresolved.
79 for (auto *I : DistinctMDNodes)
80 I->dropAllReferences();
81 #define HANDLE_MDNODE_LEAF(CLASS) \
82 for (auto *I : CLASS##s) \
83 I->dropAllReferences();
84 #include "llvm/IR/Metadata.def"
86 // Also drop references that come from the Value bridges.
87 for (auto &Pair : ValuesAsMetadata)
88 Pair.second->dropUsers();
89 for (auto &Pair : MetadataAsValues)
90 Pair.second->dropUse();
93 for (MDNode *I : DistinctMDNodes)
94 I->deleteAsSubclass();
95 #define HANDLE_MDNODE_LEAF(CLASS) \
96 for (CLASS *I : CLASS##s) \
98 #include "llvm/IR/Metadata.def"
100 // Free the constants.
101 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
103 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
105 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
107 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
109 ExprConstants.freeConstants();
110 ArrayConstants.freeConstants();
111 StructConstants.freeConstants();
112 VectorConstants.freeConstants();
113 DeleteContainerSeconds(CAZConstants);
114 DeleteContainerSeconds(CPNConstants);
115 DeleteContainerSeconds(UVConstants);
116 InlineAsms.freeConstants();
117 DeleteContainerSeconds(IntConstants);
118 DeleteContainerSeconds(FPConstants);
120 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
121 E = CDSConstants.end(); I != E; ++I)
123 CDSConstants.clear();
125 // Destroy attributes.
126 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
127 E = AttrsSet.end(); I != E; ) {
128 FoldingSetIterator<AttributeImpl> Elem = I++;
132 // Destroy attribute lists.
133 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
134 E = AttrsLists.end(); I != E; ) {
135 FoldingSetIterator<AttributeSetImpl> Elem = I++;
139 // Destroy attribute node lists.
140 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
141 E = AttrsSetNodes.end(); I != E; ) {
142 FoldingSetIterator<AttributeSetNode> Elem = I++;
146 // Destroy MetadataAsValues.
148 SmallVector<MetadataAsValue *, 8> MDVs;
149 MDVs.reserve(MetadataAsValues.size());
150 for (auto &Pair : MetadataAsValues)
151 MDVs.push_back(Pair.second);
152 MetadataAsValues.clear();
157 // Destroy ValuesAsMetadata.
158 for (auto &Pair : ValuesAsMetadata)
161 // Destroy MDStrings.
162 MDStringCache.clear();
165 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
170 for (auto I = ArrayConstants.map_begin(), E = ArrayConstants.map_end();
174 if (C->use_empty()) {
176 C->destroyConstant();
183 void Module::dropTriviallyDeadConstantArrays() {
184 Context.pImpl->dropTriviallyDeadConstantArrays();
188 /// \brief Make MDOperand transparent for hashing.
190 /// This overload of an implementation detail of the hashing library makes
191 /// MDOperand hash to the same value as a \a Metadata pointer.
193 /// Note that overloading \a hash_value() as follows:
196 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
199 /// does not cause MDOperand to be transparent. In particular, a bare pointer
200 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
201 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
204 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
205 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
208 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
209 unsigned RawHash = calculateHash(MDs);
210 assert(Hash == RawHash &&
211 "Expected hash of MDOperand to equal hash of Metadata*");
217 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
218 return hash_combine_range(Ops.begin(), Ops.end());
221 // ConstantsContext anchors
222 void UnaryConstantExpr::anchor() { }
224 void BinaryConstantExpr::anchor() { }
226 void SelectConstantExpr::anchor() { }
228 void ExtractElementConstantExpr::anchor() { }
230 void InsertElementConstantExpr::anchor() { }
232 void ShuffleVectorConstantExpr::anchor() { }
234 void ExtractValueConstantExpr::anchor() { }
236 void InsertValueConstantExpr::anchor() { }
238 void GetElementPtrConstantExpr::anchor() { }
240 void CompareConstantExpr::anchor() { }