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/GCStrategy.h"
19 #include "llvm/IR/Module.h"
23 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
24 : TheTrueVal(nullptr), TheFalseVal(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 X86_FP80Ty(C, Type::X86_FP80TyID),
32 FP128Ty(C, Type::FP128TyID),
33 PPC_FP128Ty(C, Type::PPC_FP128TyID),
34 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 for (auto *I : MDTuples)
82 I->dropAllReferences();
83 for (auto *I : MDLocations)
84 I->dropAllReferences();
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 for (MDTuple *I : MDTuples)
97 for (MDLocation *I : MDLocations)
100 // Free the constants. This is important to do here to ensure that they are
101 // freed before the LeakDetector is torn down.
102 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
104 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
106 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
108 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
110 ExprConstants.freeConstants();
111 ArrayConstants.freeConstants();
112 StructConstants.freeConstants();
113 VectorConstants.freeConstants();
114 DeleteContainerSeconds(CAZConstants);
115 DeleteContainerSeconds(CPNConstants);
116 DeleteContainerSeconds(UVConstants);
117 InlineAsms.freeConstants();
118 DeleteContainerSeconds(IntConstants);
119 DeleteContainerSeconds(FPConstants);
121 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
122 E = CDSConstants.end(); I != E; ++I)
124 CDSConstants.clear();
126 // Destroy attributes.
127 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
128 E = AttrsSet.end(); I != E; ) {
129 FoldingSetIterator<AttributeImpl> Elem = I++;
133 // Destroy attribute lists.
134 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
135 E = AttrsLists.end(); I != E; ) {
136 FoldingSetIterator<AttributeSetImpl> Elem = I++;
140 // Destroy attribute node lists.
141 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
142 E = AttrsSetNodes.end(); I != E; ) {
143 FoldingSetIterator<AttributeSetNode> Elem = I++;
147 // Destroy MetadataAsValues.
149 SmallVector<MetadataAsValue *, 8> MDVs;
150 MDVs.reserve(MetadataAsValues.size());
151 for (auto &Pair : MetadataAsValues)
152 MDVs.push_back(Pair.second);
153 MetadataAsValues.clear();
158 // Destroy ValuesAsMetadata.
159 for (auto &Pair : ValuesAsMetadata)
162 // Destroy MDStrings.
163 MDStringCache.clear();
167 /// \brief Make MDOperand transparent for hashing.
169 /// This overload of an implementation detail of the hashing library makes
170 /// MDOperand hash to the same value as a \a Metadata pointer.
172 /// Note that overloading \a hash_value() as follows:
175 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
178 /// does not cause MDOperand to be transparent. In particular, a bare pointer
179 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
180 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
183 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
184 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
187 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
188 unsigned RawHash = calculateHash(MDs);
189 assert(Hash == RawHash &&
190 "Expected hash of MDOperand to equal hash of Metadata*");
196 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
197 return hash_combine_range(Ops.begin(), Ops.end());
200 // ConstantsContext anchors
201 void UnaryConstantExpr::anchor() { }
203 void BinaryConstantExpr::anchor() { }
205 void SelectConstantExpr::anchor() { }
207 void ExtractElementConstantExpr::anchor() { }
209 void InsertElementConstantExpr::anchor() { }
211 void ShuffleVectorConstantExpr::anchor() { }
213 void ExtractValueConstantExpr::anchor() { }
215 void InsertValueConstantExpr::anchor() { }
217 void GetElementPtrConstantExpr::anchor() { }
219 void CompareConstantExpr::anchor() { }
221 GCStrategy *LLVMContextImpl::getGCStrategy(const StringRef Name) {
222 // TODO: Arguably, just doing a linear search would be faster for small N
223 auto NMI = GCStrategyMap.find(Name);
224 if (NMI != GCStrategyMap.end())
225 return NMI->getValue();
227 for (auto& Entry : GCRegistry::entries()) {
228 if (Name == Entry.getName()) {
229 std::unique_ptr<GCStrategy> S = Entry.instantiate();
231 GCStrategyMap[Name] = S.get();
232 GCStrategyList.push_back(std::move(S));
233 return GCStrategyList.back().get();
237 // No GCStrategy found for that name, error reporting is the job of our