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