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 (UniquableMDNode *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();
166 // ConstantsContext anchors
167 void UnaryConstantExpr::anchor() { }
169 void BinaryConstantExpr::anchor() { }
171 void SelectConstantExpr::anchor() { }
173 void ExtractElementConstantExpr::anchor() { }
175 void InsertElementConstantExpr::anchor() { }
177 void ShuffleVectorConstantExpr::anchor() { }
179 void ExtractValueConstantExpr::anchor() { }
181 void InsertValueConstantExpr::anchor() { }
183 void GetElementPtrConstantExpr::anchor() { }
185 void CompareConstantExpr::anchor() { }
187 GCStrategy *LLVMContextImpl::getGCStrategy(const StringRef Name) {
188 // TODO: Arguably, just doing a linear search would be faster for small N
189 auto NMI = GCStrategyMap.find(Name);
190 if (NMI != GCStrategyMap.end())
191 return NMI->getValue();
193 for (auto& Entry : GCRegistry::entries()) {
194 if (Name == Entry.getName()) {
195 std::unique_ptr<GCStrategy> S = Entry.instantiate();
197 GCStrategyMap[Name] = S.get();
198 GCStrategyList.push_back(std::move(S));
199 return GCStrategyList.back().get();
203 // No GCStrategy found for that name, error reporting is the job of our