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
|
|
36 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
|
223
|
37 : DiagHandler(std::make_unique<DiagnosticHandler>()),
|
|
38 VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID),
|
|
39 HalfTy(C, Type::HalfTyID), BFloatTy(C, Type::BFloatTyID),
|
|
40 FloatTy(C, Type::FloatTyID), DoubleTy(C, Type::DoubleTyID),
|
|
41 MetadataTy(C, Type::MetadataTyID), TokenTy(C, Type::TokenTyID),
|
|
42 X86_FP80Ty(C, Type::X86_FP80TyID), FP128Ty(C, Type::FP128TyID),
|
|
43 PPC_FP128Ty(C, Type::PPC_FP128TyID), X86_MMXTy(C, Type::X86_MMXTyID),
|
240
|
44 #ifndef noCbC
|
|
45 X86_AMXTy(C, Type::X86_AMXTyID), __CodeTy(C, Type::__CodeTyID), Int1Ty(C, 1), Int8Ty(C, 8),
|
|
46 #else
|
223
|
47 X86_AMXTy(C, Type::X86_AMXTyID), Int1Ty(C, 1), Int8Ty(C, 8),
|
240
|
48 #endif
|
252
|
49 Int16Ty(C, 16), Int32Ty(C, 32), Int64Ty(C, 64), Int128Ty(C, 128) {}
|
150
|
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 #ifndef NDEBUG
|
207
|
59 // Check for metadata references from leaked Values.
|
|
60 for (auto &Pair : ValueMetadata)
|
150
|
61 Pair.first->dump();
|
207
|
62 assert(ValueMetadata.empty() && "Values with metadata have been leaked");
|
150
|
63 #endif
|
|
64
|
|
65 // Drop references for MDNodes. Do this before Values get deleted to avoid
|
|
66 // unnecessary RAUW when nodes are still unresolved.
|
236
|
67 for (auto *I : DistinctMDNodes) {
|
|
68 // We may have DIArgList that were uniqued, and as it has a custom
|
|
69 // implementation of dropAllReferences, it needs to be explicitly invoked.
|
|
70 if (auto *AL = dyn_cast<DIArgList>(I)) {
|
|
71 AL->dropAllReferences();
|
|
72 continue;
|
|
73 }
|
150
|
74 I->dropAllReferences();
|
236
|
75 }
|
150
|
76 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
|
|
77 for (auto *I : CLASS##s) \
|
|
78 I->dropAllReferences();
|
|
79 #include "llvm/IR/Metadata.def"
|
|
80
|
|
81 // Also drop references that come from the Value bridges.
|
|
82 for (auto &Pair : ValuesAsMetadata)
|
|
83 Pair.second->dropUsers();
|
|
84 for (auto &Pair : MetadataAsValues)
|
|
85 Pair.second->dropUse();
|
|
86
|
|
87 // Destroy MDNodes.
|
|
88 for (MDNode *I : DistinctMDNodes)
|
|
89 I->deleteAsSubclass();
|
|
90 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
|
|
91 for (CLASS * I : CLASS##s) \
|
|
92 delete I;
|
|
93 #include "llvm/IR/Metadata.def"
|
|
94
|
|
95 // Free the constants.
|
|
96 for (auto *I : ExprConstants)
|
|
97 I->dropAllReferences();
|
|
98 for (auto *I : ArrayConstants)
|
|
99 I->dropAllReferences();
|
|
100 for (auto *I : StructConstants)
|
|
101 I->dropAllReferences();
|
|
102 for (auto *I : VectorConstants)
|
|
103 I->dropAllReferences();
|
|
104 ExprConstants.freeConstants();
|
|
105 ArrayConstants.freeConstants();
|
|
106 StructConstants.freeConstants();
|
|
107 VectorConstants.freeConstants();
|
|
108 InlineAsms.freeConstants();
|
|
109
|
|
110 CAZConstants.clear();
|
|
111 CPNConstants.clear();
|
252
|
112 CTNConstants.clear();
|
150
|
113 UVConstants.clear();
|
207
|
114 PVConstants.clear();
|
252
|
115 IntZeroConstants.clear();
|
|
116 IntOneConstants.clear();
|
150
|
117 IntConstants.clear();
|
|
118 FPConstants.clear();
|
|
119 CDSConstants.clear();
|
|
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
|
|
144 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
|
207
|
145 SmallSetVector<ConstantArray *, 4> WorkList;
|
|
146
|
|
147 // When ArrayConstants are of substantial size and only a few in them are
|
|
148 // dead, starting WorkList with all elements of ArrayConstants can be
|
|
149 // wasteful. Instead, starting WorkList with only elements that have empty
|
|
150 // uses.
|
|
151 for (ConstantArray *C : ArrayConstants)
|
|
152 if (C->use_empty())
|
|
153 WorkList.insert(C);
|
150
|
154
|
|
155 while (!WorkList.empty()) {
|
|
156 ConstantArray *C = WorkList.pop_back_val();
|
|
157 if (C->use_empty()) {
|
|
158 for (const Use &Op : C->operands()) {
|
|
159 if (auto *COp = dyn_cast<ConstantArray>(Op))
|
|
160 WorkList.insert(COp);
|
|
161 }
|
|
162 C->destroyConstant();
|
|
163 }
|
|
164 }
|
|
165 }
|
|
166
|
|
167 void Module::dropTriviallyDeadConstantArrays() {
|
|
168 Context.pImpl->dropTriviallyDeadConstantArrays();
|
|
169 }
|
|
170
|
|
171 namespace llvm {
|
|
172
|
|
173 /// Make MDOperand transparent for hashing.
|
|
174 ///
|
|
175 /// This overload of an implementation detail of the hashing library makes
|
|
176 /// MDOperand hash to the same value as a \a Metadata pointer.
|
|
177 ///
|
|
178 /// Note that overloading \a hash_value() as follows:
|
|
179 ///
|
|
180 /// \code
|
|
181 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
|
|
182 /// \endcode
|
|
183 ///
|
|
184 /// does not cause MDOperand to be transparent. In particular, a bare pointer
|
|
185 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
|
|
186 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
|
|
187
|
|
188 } // end namespace llvm
|
|
189
|
|
190 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
|
|
191 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
|
|
192 #ifndef NDEBUG
|
|
193 {
|
207
|
194 SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset));
|
150
|
195 unsigned RawHash = calculateHash(MDs);
|
|
196 assert(Hash == RawHash &&
|
|
197 "Expected hash of MDOperand to equal hash of Metadata*");
|
|
198 }
|
|
199 #endif
|
|
200 return Hash;
|
|
201 }
|
|
202
|
|
203 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
|
|
204 return hash_combine_range(Ops.begin(), Ops.end());
|
|
205 }
|
|
206
|
|
207 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
|
|
208 uint32_t NewIdx = BundleTagCache.size();
|
|
209 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
|
|
210 }
|
|
211
|
|
212 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
|
|
213 Tags.resize(BundleTagCache.size());
|
|
214 for (const auto &T : BundleTagCache)
|
|
215 Tags[T.second] = T.first();
|
|
216 }
|
|
217
|
|
218 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
|
|
219 auto I = BundleTagCache.find(Tag);
|
|
220 assert(I != BundleTagCache.end() && "Unknown tag!");
|
|
221 return I->second;
|
|
222 }
|
|
223
|
|
224 SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
|
|
225 auto NewSSID = SSC.size();
|
|
226 assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
|
|
227 "Hit the maximum number of synchronization scopes allowed!");
|
|
228 return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
|
|
229 }
|
|
230
|
|
231 void LLVMContextImpl::getSyncScopeNames(
|
|
232 SmallVectorImpl<StringRef> &SSNs) const {
|
|
233 SSNs.resize(SSC.size());
|
|
234 for (const auto &SSE : SSC)
|
|
235 SSNs[SSE.second] = SSE.first();
|
|
236 }
|
|
237
|
207
|
238 /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
|
|
239 /// singleton OptBisect if not explicitly set.
|
150
|
240 OptPassGate &LLVMContextImpl::getOptPassGate() const {
|
|
241 if (!OPG)
|
252
|
242 OPG = &getGlobalPassGate();
|
150
|
243 return *OPG;
|
|
244 }
|
|
245
|
|
246 void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
|
|
247 this->OPG = &OPG;
|
|
248 }
|