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 "llvm/IR/OptBisect.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include <algorithm>
22 using namespace llvm;
23
LLVMContextImpl(LLVMContext & C)24 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
25 : TheTrueVal(nullptr), TheFalseVal(nullptr),
26 VoidTy(C, Type::VoidTyID),
27 LabelTy(C, Type::LabelTyID),
28 HalfTy(C, Type::HalfTyID),
29 FloatTy(C, Type::FloatTyID),
30 DoubleTy(C, Type::DoubleTyID),
31 MetadataTy(C, Type::MetadataTyID),
32 TokenTy(C, Type::TokenTyID),
33 X86_FP80Ty(C, Type::X86_FP80TyID),
34 FP128Ty(C, Type::FP128TyID),
35 PPC_FP128Ty(C, Type::PPC_FP128TyID),
36 X86_MMXTy(C, Type::X86_MMXTyID),
37 Int1Ty(C, 1),
38 Int8Ty(C, 8),
39 Int16Ty(C, 16),
40 Int32Ty(C, 32),
41 Int64Ty(C, 64),
42 Int128Ty(C, 128) {
43 InlineAsmDiagHandler = nullptr;
44 InlineAsmDiagContext = nullptr;
45 DiagnosticHandler = nullptr;
46 DiagnosticContext = nullptr;
47 RespectDiagnosticFilters = false;
48 YieldCallback = nullptr;
49 YieldOpaqueHandle = nullptr;
50 NamedStructTypesUniqueID = 0;
51 }
52
~LLVMContextImpl()53 LLVMContextImpl::~LLVMContextImpl() {
54 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
55 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
56 // the container. Avoid iterators during this operation:
57 while (!OwnedModules.empty())
58 delete *OwnedModules.begin();
59
60 // Drop references for MDNodes. Do this before Values get deleted to avoid
61 // unnecessary RAUW when nodes are still unresolved.
62 for (auto *I : DistinctMDNodes)
63 I->dropAllReferences();
64 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
65 for (auto *I : CLASS##s) \
66 I->dropAllReferences();
67 #include "llvm/IR/Metadata.def"
68
69 // Also drop references that come from the Value bridges.
70 for (auto &Pair : ValuesAsMetadata)
71 Pair.second->dropUsers();
72 for (auto &Pair : MetadataAsValues)
73 Pair.second->dropUse();
74
75 // Destroy MDNodes.
76 for (MDNode *I : DistinctMDNodes)
77 I->deleteAsSubclass();
78 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
79 for (CLASS * I : CLASS##s) \
80 delete I;
81 #include "llvm/IR/Metadata.def"
82
83 // Free the constants.
84 for (auto *I : ExprConstants)
85 I->dropAllReferences();
86 for (auto *I : ArrayConstants)
87 I->dropAllReferences();
88 for (auto *I : StructConstants)
89 I->dropAllReferences();
90 for (auto *I : VectorConstants)
91 I->dropAllReferences();
92 ExprConstants.freeConstants();
93 ArrayConstants.freeConstants();
94 StructConstants.freeConstants();
95 VectorConstants.freeConstants();
96 DeleteContainerSeconds(CAZConstants);
97 DeleteContainerSeconds(CPNConstants);
98 DeleteContainerSeconds(UVConstants);
99 InlineAsms.freeConstants();
100 DeleteContainerSeconds(IntConstants);
101 DeleteContainerSeconds(FPConstants);
102
103 for (auto &CDSConstant : CDSConstants)
104 delete CDSConstant.second;
105 CDSConstants.clear();
106
107 // Destroy attributes.
108 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
109 E = AttrsSet.end(); I != E; ) {
110 FoldingSetIterator<AttributeImpl> Elem = I++;
111 delete &*Elem;
112 }
113
114 // Destroy attribute lists.
115 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
116 E = AttrsLists.end(); I != E; ) {
117 FoldingSetIterator<AttributeSetImpl> Elem = I++;
118 delete &*Elem;
119 }
120
121 // Destroy attribute node lists.
122 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
123 E = AttrsSetNodes.end(); I != E; ) {
124 FoldingSetIterator<AttributeSetNode> Elem = I++;
125 delete &*Elem;
126 }
127
128 // Destroy MetadataAsValues.
129 {
130 SmallVector<MetadataAsValue *, 8> MDVs;
131 MDVs.reserve(MetadataAsValues.size());
132 for (auto &Pair : MetadataAsValues)
133 MDVs.push_back(Pair.second);
134 MetadataAsValues.clear();
135 for (auto *V : MDVs)
136 delete V;
137 }
138
139 // Destroy ValuesAsMetadata.
140 for (auto &Pair : ValuesAsMetadata)
141 delete Pair.second;
142 }
143
dropTriviallyDeadConstantArrays()144 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
145 bool Changed;
146 do {
147 Changed = false;
148
149 for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
150 auto *C = *I++;
151 if (C->use_empty()) {
152 Changed = true;
153 C->destroyConstant();
154 }
155 }
156
157 } while (Changed);
158 }
159
dropTriviallyDeadConstantArrays()160 void Module::dropTriviallyDeadConstantArrays() {
161 Context.pImpl->dropTriviallyDeadConstantArrays();
162 }
163
164 namespace llvm {
165 /// \brief Make MDOperand transparent for hashing.
166 ///
167 /// This overload of an implementation detail of the hashing library makes
168 /// MDOperand hash to the same value as a \a Metadata pointer.
169 ///
170 /// Note that overloading \a hash_value() as follows:
171 ///
172 /// \code
173 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
174 /// \endcode
175 ///
176 /// does not cause MDOperand to be transparent. In particular, a bare pointer
177 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
get_hashable_data(const MDOperand & X)178 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
179 }
180
calculateHash(MDNode * N,unsigned Offset)181 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
182 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
183 #ifndef NDEBUG
184 {
185 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
186 unsigned RawHash = calculateHash(MDs);
187 assert(Hash == RawHash &&
188 "Expected hash of MDOperand to equal hash of Metadata*");
189 }
190 #endif
191 return Hash;
192 }
193
calculateHash(ArrayRef<Metadata * > Ops)194 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
195 return hash_combine_range(Ops.begin(), Ops.end());
196 }
197
getOrInsertBundleTag(StringRef Tag)198 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
199 uint32_t NewIdx = BundleTagCache.size();
200 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
201 }
202
getOperandBundleTags(SmallVectorImpl<StringRef> & Tags) const203 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
204 Tags.resize(BundleTagCache.size());
205 for (const auto &T : BundleTagCache)
206 Tags[T.second] = T.first();
207 }
208
getOperandBundleTagID(StringRef Tag) const209 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
210 auto I = BundleTagCache.find(Tag);
211 assert(I != BundleTagCache.end() && "Unknown tag!");
212 return I->second;
213 }
214
215 // ConstantsContext anchors
anchor()216 void UnaryConstantExpr::anchor() { }
217
anchor()218 void BinaryConstantExpr::anchor() { }
219
anchor()220 void SelectConstantExpr::anchor() { }
221
anchor()222 void ExtractElementConstantExpr::anchor() { }
223
anchor()224 void InsertElementConstantExpr::anchor() { }
225
anchor()226 void ShuffleVectorConstantExpr::anchor() { }
227
anchor()228 void ExtractValueConstantExpr::anchor() { }
229
anchor()230 void InsertValueConstantExpr::anchor() { }
231
anchor()232 void GetElementPtrConstantExpr::anchor() { }
233
anchor()234 void CompareConstantExpr::anchor() { }
235
236 /// Singleton instance of the OptBisect class.
237 ///
238 /// This singleton is accessed via the LLVMContext::getOptBisect() function. It
239 /// provides a mechanism to disable passes and individual optimizations at
240 /// compile time based on a command line option (-opt-bisect-limit) in order to
241 /// perform a bisecting search for optimization-related problems.
242 ///
243 /// Even if multiple LLVMContext objects are created, they will all return the
244 /// same instance of OptBisect in order to provide a single bisect count. Any
245 /// code that uses the OptBisect object should be serialized when bisection is
246 /// enabled in order to enable a consistent bisect count.
247 static ManagedStatic<OptBisect> OptBisector;
248
getOptBisect()249 OptBisect &LLVMContextImpl::getOptBisect() {
250 return *OptBisector;
251 }
252