150
|
1 //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
|
|
2 //
|
|
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
4 // See https://llvm.org/LICENSE.txt for license information.
|
|
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
6 //
|
|
7 //===----------------------------------------------------------------------===//
|
|
8 //
|
|
9 // This file implements the opaque LLVMContextImpl.
|
|
10 //
|
|
11 //===----------------------------------------------------------------------===//
|
|
12
|
|
13 #include "LLVMContextImpl.h"
|
236
|
14 #include "AttributeImpl.h"
|
150
|
15 #include "llvm/ADT/SetVector.h"
|
236
|
16 #include "llvm/ADT/StringMapEntry.h"
|
|
17 #include "llvm/ADT/iterator.h"
|
|
18 #include "llvm/ADT/iterator_range.h"
|
|
19 #include "llvm/IR/DiagnosticHandler.h"
|
|
20 #include "llvm/IR/LLVMRemarkStreamer.h"
|
150
|
21 #include "llvm/IR/Module.h"
|
|
22 #include "llvm/IR/OptBisect.h"
|
|
23 #include "llvm/IR/Type.h"
|
236
|
24 #include "llvm/IR/Use.h"
|
|
25 #include "llvm/IR/User.h"
|
|
26 #include "llvm/Remarks/RemarkStreamer.h"
|
223
|
27 #include "llvm/Support/CommandLine.h"
|
236
|
28 #include "llvm/Support/Compiler.h"
|
|
29 #include "llvm/Support/ErrorHandling.h"
|
|
30 #include "llvm/Support/TypeSize.h"
|
150
|
31 #include <cassert>
|
|
32 #include <utility>
|
|
33
|
|
34 using namespace llvm;
|
|
35
|
223
|
36 static cl::opt<bool>
|
236
|
37 OpaquePointersCL("opaque-pointers", cl::desc("Use opaque pointers"),
|
|
38 cl::init(true));
|
223
|
39
|
150
|
40 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
|
223
|
41 : DiagHandler(std::make_unique<DiagnosticHandler>()),
|
|
42 VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID),
|
|
43 HalfTy(C, Type::HalfTyID), BFloatTy(C, Type::BFloatTyID),
|
|
44 FloatTy(C, Type::FloatTyID), DoubleTy(C, Type::DoubleTyID),
|
|
45 MetadataTy(C, Type::MetadataTyID), TokenTy(C, Type::TokenTyID),
|
|
46 X86_FP80Ty(C, Type::X86_FP80TyID), FP128Ty(C, Type::FP128TyID),
|
|
47 PPC_FP128Ty(C, Type::PPC_FP128TyID), X86_MMXTy(C, Type::X86_MMXTyID),
|
240
|
48 #ifndef noCbC
|
|
49 X86_AMXTy(C, Type::X86_AMXTyID), __CodeTy(C, Type::__CodeTyID), Int1Ty(C, 1), Int8Ty(C, 8),
|
|
50 #else
|
223
|
51 X86_AMXTy(C, Type::X86_AMXTyID), Int1Ty(C, 1), Int8Ty(C, 8),
|
240
|
52 #endif
|
236
|
53 Int16Ty(C, 16), Int32Ty(C, 32), Int64Ty(C, 64), Int128Ty(C, 128) {
|
|
54 if (OpaquePointersCL.getNumOccurrences()) {
|
|
55 OpaquePointers = OpaquePointersCL;
|
|
56 }
|
|
57 }
|
150
|
58
|
|
59 LLVMContextImpl::~LLVMContextImpl() {
|
|
60 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
|
|
61 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
|
|
62 // the container. Avoid iterators during this operation:
|
|
63 while (!OwnedModules.empty())
|
|
64 delete *OwnedModules.begin();
|
|
65
|
|
66 #ifndef NDEBUG
|
207
|
67 // Check for metadata references from leaked Values.
|
|
68 for (auto &Pair : ValueMetadata)
|
150
|
69 Pair.first->dump();
|
207
|
70 assert(ValueMetadata.empty() && "Values with metadata have been leaked");
|
150
|
71 #endif
|
|
72
|
|
73 // Drop references for MDNodes. Do this before Values get deleted to avoid
|
|
74 // unnecessary RAUW when nodes are still unresolved.
|
236
|
75 for (auto *I : DistinctMDNodes) {
|
|
76 // We may have DIArgList that were uniqued, and as it has a custom
|
|
77 // implementation of dropAllReferences, it needs to be explicitly invoked.
|
|
78 if (auto *AL = dyn_cast<DIArgList>(I)) {
|
|
79 AL->dropAllReferences();
|
|
80 continue;
|
|
81 }
|
150
|
82 I->dropAllReferences();
|
236
|
83 }
|
150
|
84 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
|
|
85 for (auto *I : CLASS##s) \
|
|
86 I->dropAllReferences();
|
|
87 #include "llvm/IR/Metadata.def"
|
|
88
|
|
89 // Also drop references that come from the Value bridges.
|
|
90 for (auto &Pair : ValuesAsMetadata)
|
|
91 Pair.second->dropUsers();
|
|
92 for (auto &Pair : MetadataAsValues)
|
|
93 Pair.second->dropUse();
|
|
94
|
|
95 // Destroy MDNodes.
|
|
96 for (MDNode *I : DistinctMDNodes)
|
|
97 I->deleteAsSubclass();
|
|
98 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
|
|
99 for (CLASS * I : CLASS##s) \
|
|
100 delete I;
|
|
101 #include "llvm/IR/Metadata.def"
|
|
102
|
|
103 // Free the constants.
|
|
104 for (auto *I : ExprConstants)
|
|
105 I->dropAllReferences();
|
|
106 for (auto *I : ArrayConstants)
|
|
107 I->dropAllReferences();
|
|
108 for (auto *I : StructConstants)
|
|
109 I->dropAllReferences();
|
|
110 for (auto *I : VectorConstants)
|
|
111 I->dropAllReferences();
|
|
112 ExprConstants.freeConstants();
|
|
113 ArrayConstants.freeConstants();
|
|
114 StructConstants.freeConstants();
|
|
115 VectorConstants.freeConstants();
|
|
116 InlineAsms.freeConstants();
|
|
117
|
|
118 CAZConstants.clear();
|
|
119 CPNConstants.clear();
|
|
120 UVConstants.clear();
|
207
|
121 PVConstants.clear();
|
150
|
122 IntConstants.clear();
|
|
123 FPConstants.clear();
|
|
124 CDSConstants.clear();
|
|
125
|
|
126 // Destroy attribute node lists.
|
|
127 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
|
|
128 E = AttrsSetNodes.end(); I != E; ) {
|
|
129 FoldingSetIterator<AttributeSetNode> Elem = I++;
|
|
130 delete &*Elem;
|
|
131 }
|
|
132
|
|
133 // Destroy MetadataAsValues.
|
|
134 {
|
|
135 SmallVector<MetadataAsValue *, 8> MDVs;
|
|
136 MDVs.reserve(MetadataAsValues.size());
|
|
137 for (auto &Pair : MetadataAsValues)
|
|
138 MDVs.push_back(Pair.second);
|
|
139 MetadataAsValues.clear();
|
|
140 for (auto *V : MDVs)
|
|
141 delete V;
|
|
142 }
|
|
143
|
|
144 // Destroy ValuesAsMetadata.
|
|
145 for (auto &Pair : ValuesAsMetadata)
|
|
146 delete Pair.second;
|
|
147 }
|
|
148
|
|
149 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
|
207
|
150 SmallSetVector<ConstantArray *, 4> WorkList;
|
|
151
|
|
152 // When ArrayConstants are of substantial size and only a few in them are
|
|
153 // dead, starting WorkList with all elements of ArrayConstants can be
|
|
154 // wasteful. Instead, starting WorkList with only elements that have empty
|
|
155 // uses.
|
|
156 for (ConstantArray *C : ArrayConstants)
|
|
157 if (C->use_empty())
|
|
158 WorkList.insert(C);
|
150
|
159
|
|
160 while (!WorkList.empty()) {
|
|
161 ConstantArray *C = WorkList.pop_back_val();
|
|
162 if (C->use_empty()) {
|
|
163 for (const Use &Op : C->operands()) {
|
|
164 if (auto *COp = dyn_cast<ConstantArray>(Op))
|
|
165 WorkList.insert(COp);
|
|
166 }
|
|
167 C->destroyConstant();
|
|
168 }
|
|
169 }
|
|
170 }
|
|
171
|
|
172 void Module::dropTriviallyDeadConstantArrays() {
|
|
173 Context.pImpl->dropTriviallyDeadConstantArrays();
|
|
174 }
|
|
175
|
|
176 namespace llvm {
|
|
177
|
|
178 /// Make MDOperand transparent for hashing.
|
|
179 ///
|
|
180 /// This overload of an implementation detail of the hashing library makes
|
|
181 /// MDOperand hash to the same value as a \a Metadata pointer.
|
|
182 ///
|
|
183 /// Note that overloading \a hash_value() as follows:
|
|
184 ///
|
|
185 /// \code
|
|
186 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
|
|
187 /// \endcode
|
|
188 ///
|
|
189 /// does not cause MDOperand to be transparent. In particular, a bare pointer
|
|
190 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
|
|
191 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
|
|
192
|
|
193 } // end namespace llvm
|
|
194
|
|
195 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
|
|
196 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
|
|
197 #ifndef NDEBUG
|
|
198 {
|
207
|
199 SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset));
|
150
|
200 unsigned RawHash = calculateHash(MDs);
|
|
201 assert(Hash == RawHash &&
|
|
202 "Expected hash of MDOperand to equal hash of Metadata*");
|
|
203 }
|
|
204 #endif
|
|
205 return Hash;
|
|
206 }
|
|
207
|
|
208 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
|
|
209 return hash_combine_range(Ops.begin(), Ops.end());
|
|
210 }
|
|
211
|
|
212 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
|
|
213 uint32_t NewIdx = BundleTagCache.size();
|
|
214 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
|
|
215 }
|
|
216
|
|
217 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
|
|
218 Tags.resize(BundleTagCache.size());
|
|
219 for (const auto &T : BundleTagCache)
|
|
220 Tags[T.second] = T.first();
|
|
221 }
|
|
222
|
|
223 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
|
|
224 auto I = BundleTagCache.find(Tag);
|
|
225 assert(I != BundleTagCache.end() && "Unknown tag!");
|
|
226 return I->second;
|
|
227 }
|
|
228
|
|
229 SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
|
|
230 auto NewSSID = SSC.size();
|
|
231 assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
|
|
232 "Hit the maximum number of synchronization scopes allowed!");
|
|
233 return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
|
|
234 }
|
|
235
|
|
236 void LLVMContextImpl::getSyncScopeNames(
|
|
237 SmallVectorImpl<StringRef> &SSNs) const {
|
|
238 SSNs.resize(SSC.size());
|
|
239 for (const auto &SSE : SSC)
|
|
240 SSNs[SSE.second] = SSE.first();
|
|
241 }
|
|
242
|
207
|
243 /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
|
|
244 /// singleton OptBisect if not explicitly set.
|
150
|
245 OptPassGate &LLVMContextImpl::getOptPassGate() const {
|
|
246 if (!OPG)
|
236
|
247 OPG = &getOptBisector();
|
150
|
248 return *OPG;
|
|
249 }
|
|
250
|
|
251 void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
|
|
252 this->OPG = &OPG;
|
|
253 }
|
236
|
254
|
|
255 bool LLVMContextImpl::hasOpaquePointersValue() {
|
|
256 return OpaquePointers.has_value();
|
|
257 }
|
|
258
|
|
259 bool LLVMContextImpl::getOpaquePointers() {
|
|
260 if (LLVM_UNLIKELY(!OpaquePointers))
|
|
261 OpaquePointers = OpaquePointersCL;
|
|
262 return *OpaquePointers;
|
|
263 }
|
|
264
|
|
265 void LLVMContextImpl::setOpaquePointers(bool OP) {
|
|
266 assert((!OpaquePointers || OpaquePointers.value() == OP) &&
|
|
267 "Cannot change opaque pointers mode once set");
|
|
268 OpaquePointers = OP;
|
|
269 }
|