48df5ba62f049f6b8d21aea42e9d33fb20bba8e5
[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     TheNoneToken(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     TokenTy(C, Type::TokenTyID),
32     X86_FP80Ty(C, Type::X86_FP80TyID),
33     FP128Ty(C, Type::FP128TyID),
34     PPC_FP128Ty(C, Type::PPC_FP128TyID),
35     X86_MMXTy(C, Type::X86_MMXTyID),
36     Int1Ty(C, 1),
37     Int8Ty(C, 8),
38     Int16Ty(C, 16),
39     Int32Ty(C, 32),
40     Int64Ty(C, 64),
41     Int128Ty(C, 128) {
42   InlineAsmDiagHandler = nullptr;
43   InlineAsmDiagContext = nullptr;
44   DiagnosticHandler = nullptr;
45   DiagnosticContext = nullptr;
46   RespectDiagnosticFilters = false;
47   YieldCallback = nullptr;
48   YieldOpaqueHandle = nullptr;
49   NamedStructTypesUniqueID = 0;
50 }
51
52 namespace {
53 struct DropReferences {
54   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
55   // is a Constant*.
56   template <typename PairT> void operator()(const PairT &P) {
57     P.second->dropAllReferences();
58   }
59 };
60
61 // Temporary - drops pair.first instead of second.
62 struct DropFirst {
63   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
64   // is a Constant*.
65   template<typename PairT>
66   void operator()(const PairT &P) {
67     P.first->dropAllReferences();
68   }
69 };
70 }
71
72 LLVMContextImpl::~LLVMContextImpl() {
73   // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
74   // will call LLVMContextImpl::removeModule, thus invalidating iterators into
75   // the container. Avoid iterators during this operation:
76   while (!OwnedModules.empty())
77     delete *OwnedModules.begin();
78
79   // Drop references for MDNodes.  Do this before Values get deleted to avoid
80   // unnecessary RAUW when nodes are still unresolved.
81   for (auto *I : DistinctMDNodes)
82     I->dropAllReferences();
83 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
84   for (auto *I : CLASS##s)                                                     \
85     I->dropAllReferences();
86 #include "llvm/IR/Metadata.def"
87
88   // Also drop references that come from the Value bridges.
89   for (auto &Pair : ValuesAsMetadata)
90     Pair.second->dropUsers();
91   for (auto &Pair : MetadataAsValues)
92     Pair.second->dropUse();
93
94   // Destroy MDNodes.
95   for (MDNode *I : DistinctMDNodes)
96     I->deleteAsSubclass();
97 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
98   for (CLASS * I : CLASS##s)                                                   \
99     delete I;
100 #include "llvm/IR/Metadata.def"
101
102   // Free the constants.
103   std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
104                 DropFirst());
105   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
106                 DropFirst());
107   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
108                 DropFirst());
109   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
110                 DropFirst());
111   ExprConstants.freeConstants();
112   ArrayConstants.freeConstants();
113   StructConstants.freeConstants();
114   VectorConstants.freeConstants();
115   DeleteContainerSeconds(CAZConstants);
116   DeleteContainerSeconds(CPNConstants);
117   DeleteContainerSeconds(UVConstants);
118   InlineAsms.freeConstants();
119   DeleteContainerSeconds(IntConstants);
120   DeleteContainerSeconds(FPConstants);
121   
122   for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
123        E = CDSConstants.end(); I != E; ++I)
124     delete I->second;
125   CDSConstants.clear();
126
127   // Destroy attributes.
128   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
129          E = AttrsSet.end(); I != E; ) {
130     FoldingSetIterator<AttributeImpl> Elem = I++;
131     delete &*Elem;
132   }
133
134   // Destroy attribute lists.
135   for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
136          E = AttrsLists.end(); I != E; ) {
137     FoldingSetIterator<AttributeSetImpl> Elem = I++;
138     delete &*Elem;
139   }
140
141   // Destroy attribute node lists.
142   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
143          E = AttrsSetNodes.end(); I != E; ) {
144     FoldingSetIterator<AttributeSetNode> Elem = I++;
145     delete &*Elem;
146   }
147
148   // Destroy MetadataAsValues.
149   {
150     SmallVector<MetadataAsValue *, 8> MDVs;
151     MDVs.reserve(MetadataAsValues.size());
152     for (auto &Pair : MetadataAsValues)
153       MDVs.push_back(Pair.second);
154     MetadataAsValues.clear();
155     for (auto *V : MDVs)
156       delete V;
157   }
158
159   // Destroy ValuesAsMetadata.
160   for (auto &Pair : ValuesAsMetadata)
161     delete Pair.second;
162
163   // Destroy MDStrings.
164   MDStringCache.clear();
165 }
166
167 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
168   bool Changed;
169   do {
170     Changed = false;
171
172     for (auto I = ArrayConstants.map_begin(), E = ArrayConstants.map_end();
173          I != E; ) {
174       auto *C = I->first;
175       I++;
176       if (C->use_empty()) {
177         Changed = true;
178         C->destroyConstant();
179       }
180     }
181
182   } while (Changed);
183 }
184
185 void Module::dropTriviallyDeadConstantArrays() {
186   Context.pImpl->dropTriviallyDeadConstantArrays();
187 }
188
189 namespace llvm {
190 /// \brief Make MDOperand transparent for hashing.
191 ///
192 /// This overload of an implementation detail of the hashing library makes
193 /// MDOperand hash to the same value as a \a Metadata pointer.
194 ///
195 /// Note that overloading \a hash_value() as follows:
196 ///
197 /// \code
198 ///     size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
199 /// \endcode
200 ///
201 /// does not cause MDOperand to be transparent.  In particular, a bare pointer
202 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
203 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
204 }
205
206 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
207   unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
208 #ifndef NDEBUG
209   {
210     SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
211     unsigned RawHash = calculateHash(MDs);
212     assert(Hash == RawHash &&
213            "Expected hash of MDOperand to equal hash of Metadata*");
214   }
215 #endif
216   return Hash;
217 }
218
219 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
220   return hash_combine_range(Ops.begin(), Ops.end());
221 }
222
223 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
224   uint32_t NewIdx = BundleTagCache.size();
225   return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
226 }
227
228 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
229   Tags.resize(BundleTagCache.size());
230   for (const auto &T : BundleTagCache)
231     Tags[T.second] = T.first();
232 }
233
234 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
235   auto I = BundleTagCache.find(Tag);
236   assert(I != BundleTagCache.end() && "Unknown tag!");
237   return I->second;
238 }
239
240 // ConstantsContext anchors
241 void UnaryConstantExpr::anchor() { }
242
243 void BinaryConstantExpr::anchor() { }
244
245 void SelectConstantExpr::anchor() { }
246
247 void ExtractElementConstantExpr::anchor() { }
248
249 void InsertElementConstantExpr::anchor() { }
250
251 void ShuffleVectorConstantExpr::anchor() { }
252
253 void ExtractValueConstantExpr::anchor() { }
254
255 void InsertValueConstantExpr::anchor() { }
256
257 void GetElementPtrConstantExpr::anchor() { }
258
259 void CompareConstantExpr::anchor() { }
260