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