Move ownership of GCStrategy objects to LLVMContext
[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/GCStrategy.h"
19 #include "llvm/IR/Module.h"
20 #include <algorithm>
21 using namespace llvm;
22
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),
35     Int1Ty(C, 1),
36     Int8Ty(C, 8),
37     Int16Ty(C, 16),
38     Int32Ty(C, 32),
39     Int64Ty(C, 64) {
40   InlineAsmDiagHandler = nullptr;
41   InlineAsmDiagContext = nullptr;
42   DiagnosticHandler = nullptr;
43   DiagnosticContext = nullptr;
44   RespectDiagnosticFilters = false;
45   YieldCallback = nullptr;
46   YieldOpaqueHandle = nullptr;
47   NamedStructTypesUniqueID = 0;
48 }
49
50 namespace {
51 struct DropReferences {
52   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
53   // is a Constant*.
54   template <typename PairT> void operator()(const PairT &P) {
55     P.second->dropAllReferences();
56   }
57 };
58
59 // Temporary - drops pair.first instead of second.
60 struct DropFirst {
61   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
62   // is a Constant*.
63   template<typename PairT>
64   void operator()(const PairT &P) {
65     P.first->dropAllReferences();
66   }
67 };
68 }
69
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();
76
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();
85
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();
91
92   // Destroy MDNodes.
93   for (UniquableMDNode *I : DistinctMDNodes)
94     I->deleteAsSubclass();
95   for (MDTuple *I : MDTuples)
96     delete I;
97   for (MDLocation *I : MDLocations)
98     delete I;
99
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(),
103                 DropFirst());
104   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
105                 DropFirst());
106   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
107                 DropFirst());
108   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
109                 DropFirst());
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);
120   
121   for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
122        E = CDSConstants.end(); I != E; ++I)
123     delete I->second;
124   CDSConstants.clear();
125
126   // Destroy attributes.
127   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
128          E = AttrsSet.end(); I != E; ) {
129     FoldingSetIterator<AttributeImpl> Elem = I++;
130     delete &*Elem;
131   }
132
133   // Destroy attribute lists.
134   for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
135          E = AttrsLists.end(); I != E; ) {
136     FoldingSetIterator<AttributeSetImpl> Elem = I++;
137     delete &*Elem;
138   }
139
140   // Destroy attribute node lists.
141   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
142          E = AttrsSetNodes.end(); I != E; ) {
143     FoldingSetIterator<AttributeSetNode> Elem = I++;
144     delete &*Elem;
145   }
146
147   // Destroy MetadataAsValues.
148   {
149     SmallVector<MetadataAsValue *, 8> MDVs;
150     MDVs.reserve(MetadataAsValues.size());
151     for (auto &Pair : MetadataAsValues)
152       MDVs.push_back(Pair.second);
153     MetadataAsValues.clear();
154     for (auto *V : MDVs)
155       delete V;
156   }
157
158   // Destroy ValuesAsMetadata.
159   for (auto &Pair : ValuesAsMetadata)
160     delete Pair.second;
161
162   // Destroy MDStrings.
163   MDStringCache.clear();
164 }
165
166 // ConstantsContext anchors
167 void UnaryConstantExpr::anchor() { }
168
169 void BinaryConstantExpr::anchor() { }
170
171 void SelectConstantExpr::anchor() { }
172
173 void ExtractElementConstantExpr::anchor() { }
174
175 void InsertElementConstantExpr::anchor() { }
176
177 void ShuffleVectorConstantExpr::anchor() { }
178
179 void ExtractValueConstantExpr::anchor() { }
180
181 void InsertValueConstantExpr::anchor() { }
182
183 void GetElementPtrConstantExpr::anchor() { }
184
185 void CompareConstantExpr::anchor() { }
186
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();
192   
193   for (auto& Entry : GCRegistry::entries()) {
194     if (Name == Entry.getName()) {
195       std::unique_ptr<GCStrategy> S = Entry.instantiate();
196       S->Name = Name;
197       GCStrategyMap[Name] = S.get();
198       GCStrategyList.push_back(std::move(S));
199       return GCStrategyList.back().get();
200     }
201   }
202
203   // No GCStrategy found for that name, error reporting is the job of our
204   // callers. 
205   return nullptr;
206 }
207
208