IR: Drop metadata references more aggressively during teardown
[oota-llvm.git] / lib / IR / LLVMContextImpl.cpp
1 //===-- LLVMContextImpl.cpp - Implement LLVMContextImpl -------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the opaque LLVMContextImpl.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
19 #include <algorithm>
20 using namespace llvm;
21
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),
34     Int1Ty(C, 1),
35     Int8Ty(C, 8),
36     Int16Ty(C, 16),
37     Int32Ty(C, 32),
38     Int64Ty(C, 64) {
39   InlineAsmDiagHandler = nullptr;
40   InlineAsmDiagContext = nullptr;
41   DiagnosticHandler = nullptr;
42   DiagnosticContext = nullptr;
43   RespectDiagnosticFilters = false;
44   YieldCallback = nullptr;
45   YieldOpaqueHandle = nullptr;
46   NamedStructTypesUniqueID = 0;
47 }
48
49 namespace {
50 struct DropReferences {
51   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
52   // is a Constant*.
53   template <typename PairT> void operator()(const PairT &P) {
54     P.second->dropAllReferences();
55   }
56 };
57
58 // Temporary - drops pair.first instead of second.
59 struct DropFirst {
60   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
61   // is a Constant*.
62   template<typename PairT>
63   void operator()(const PairT &P) {
64     P.first->dropAllReferences();
65   }
66 };
67 }
68
69 LLVMContextImpl::~LLVMContextImpl() {
70   // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
71   // will call LLVMContextImpl::removeModule, thus invalidating iterators into
72   // the container. Avoid iterators during this operation:
73   while (!OwnedModules.empty())
74     delete *OwnedModules.begin();
75
76   // Drop references for MDNodes.  Do this before Values get deleted to avoid
77   // unnecessary RAUW when nodes are still unresolved.
78   for (auto *I : DistinctMDNodes)
79     I->dropAllReferences();
80   for (auto *I : MDTuples)
81     I->dropAllReferences();
82   for (auto *I : MDLocations)
83     I->dropAllReferences();
84
85   // Also drop references that come from the Value bridges.
86   for (auto &Pair : ValuesAsMetadata)
87     Pair.second->dropUsers();
88   for (auto &Pair : MetadataAsValues)
89     Pair.second->dropUse();
90
91   // Destroy MDNodes.
92   for (UniquableMDNode *I : DistinctMDNodes)
93     I->deleteAsSubclass();
94   for (MDTuple *I : MDTuples)
95     delete I;
96   for (MDLocation *I : MDLocations)
97     delete I;
98
99   // Free the constants.  This is important to do here to ensure that they are
100   // freed before the LeakDetector is torn down.
101   std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
102                 DropFirst());
103   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
104                 DropFirst());
105   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
106                 DropFirst());
107   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
108                 DropFirst());
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);
119   
120   for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
121        E = CDSConstants.end(); I != E; ++I)
122     delete I->second;
123   CDSConstants.clear();
124
125   // Destroy attributes.
126   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
127          E = AttrsSet.end(); I != E; ) {
128     FoldingSetIterator<AttributeImpl> Elem = I++;
129     delete &*Elem;
130   }
131
132   // Destroy attribute lists.
133   for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
134          E = AttrsLists.end(); I != E; ) {
135     FoldingSetIterator<AttributeSetImpl> Elem = I++;
136     delete &*Elem;
137   }
138
139   // Destroy attribute node lists.
140   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
141          E = AttrsSetNodes.end(); I != E; ) {
142     FoldingSetIterator<AttributeSetNode> Elem = I++;
143     delete &*Elem;
144   }
145
146   // Destroy MetadataAsValues.
147   {
148     SmallVector<MetadataAsValue *, 8> MDVs;
149     MDVs.reserve(MetadataAsValues.size());
150     for (auto &Pair : MetadataAsValues)
151       MDVs.push_back(Pair.second);
152     MetadataAsValues.clear();
153     for (auto *V : MDVs)
154       delete V;
155   }
156
157   // Destroy ValuesAsMetadata.
158   for (auto &Pair : ValuesAsMetadata)
159     delete Pair.second;
160
161   // Destroy MDStrings.
162   MDStringCache.clear();
163 }
164
165 // ConstantsContext anchors
166 void UnaryConstantExpr::anchor() { }
167
168 void BinaryConstantExpr::anchor() { }
169
170 void SelectConstantExpr::anchor() { }
171
172 void ExtractElementConstantExpr::anchor() { }
173
174 void InsertElementConstantExpr::anchor() { }
175
176 void ShuffleVectorConstantExpr::anchor() { }
177
178 void ExtractValueConstantExpr::anchor() { }
179
180 void InsertValueConstantExpr::anchor() { }
181
182 void GetElementPtrConstantExpr::anchor() { }
183
184 void CompareConstantExpr::anchor() { }