/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/IR/LLVMContextImpl.cpp
Line | Count | Source (jump to first uncovered line) |
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/IR/Module.h" |
15 | | #include "llvm/IR/OptBisect.h" |
16 | | #include "llvm/IR/Type.h" |
17 | | #include "llvm/Support/ManagedStatic.h" |
18 | | #include <cassert> |
19 | | #include <utility> |
20 | | |
21 | | using namespace llvm; |
22 | | |
23 | | LLVMContextImpl::LLVMContextImpl(LLVMContext &C) |
24 | | : DiagHandler(llvm::make_unique<DiagnosticHandler>()), |
25 | | VoidTy(C, Type::VoidTyID), |
26 | | LabelTy(C, Type::LabelTyID), |
27 | | HalfTy(C, Type::HalfTyID), |
28 | | FloatTy(C, Type::FloatTyID), |
29 | | DoubleTy(C, Type::DoubleTyID), |
30 | | MetadataTy(C, Type::MetadataTyID), |
31 | | TokenTy(C, Type::TokenTyID), |
32 | | X86_FP80Ty(C, Type::X86_FP80TyID), |
33 | | FP128Ty(C, Type::FP128TyID), |
34 | | PPC_FP128Ty(C, Type::PPC_FP128TyID), |
35 | | X86_MMXTy(C, Type::X86_MMXTyID), |
36 | | Int1Ty(C, 1), |
37 | | Int8Ty(C, 8), |
38 | | Int16Ty(C, 16), |
39 | | Int32Ty(C, 32), |
40 | | Int64Ty(C, 64), |
41 | 107k | Int128Ty(C, 128) {} |
42 | | |
43 | 94.3k | LLVMContextImpl::~LLVMContextImpl() { |
44 | 94.3k | // NOTE: We need to delete the contents of OwnedModules, but Module's dtor |
45 | 94.3k | // will call LLVMContextImpl::removeModule, thus invalidating iterators into |
46 | 94.3k | // the container. Avoid iterators during this operation: |
47 | 94.4k | while (!OwnedModules.empty()) |
48 | 20 | delete *OwnedModules.begin(); |
49 | 94.3k | |
50 | | #ifndef NDEBUG |
51 | | // Check for metadata references from leaked Instructions. |
52 | | for (auto &Pair : InstructionMetadata) |
53 | | Pair.first->dump(); |
54 | | assert(InstructionMetadata.empty() && |
55 | | "Instructions with metadata have been leaked"); |
56 | | #endif |
57 | | |
58 | 94.3k | // Drop references for MDNodes. Do this before Values get deleted to avoid |
59 | 94.3k | // unnecessary RAUW when nodes are still unresolved. |
60 | 94.3k | for (auto *I : DistinctMDNodes) |
61 | 43.3k | I->dropAllReferences(); |
62 | 94.3k | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
63 | 2.54M | for (auto *I : CLASS##s) \ |
64 | 2.54M | I->dropAllReferences()186k ; |
65 | 94.3k | #include "llvm/IR/Metadata.def" |
66 | 94.3k | |
67 | 94.3k | // Also drop references that come from the Value bridges. |
68 | 94.3k | for (auto &Pair : ValuesAsMetadata) |
69 | 82.8k | Pair.second->dropUsers(); |
70 | 94.3k | for (auto &Pair : MetadataAsValues) |
71 | 21.4k | Pair.second->dropUse(); |
72 | 94.3k | |
73 | 94.3k | // Destroy MDNodes. |
74 | 94.3k | for (MDNode *I : DistinctMDNodes) |
75 | 43.3k | I->deleteAsSubclass(); |
76 | 94.3k | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
77 | 2.54M | for (CLASS * I : CLASS##s) \ |
78 | 2.54M | delete I186k ; |
79 | 94.3k | #include "llvm/IR/Metadata.def" |
80 | 94.3k | |
81 | 94.3k | // Free the constants. |
82 | 94.3k | for (auto *I : ExprConstants) |
83 | 4.44k | I->dropAllReferences(); |
84 | 94.3k | for (auto *I : ArrayConstants) |
85 | 551 | I->dropAllReferences(); |
86 | 94.3k | for (auto *I : StructConstants) |
87 | 4.19k | I->dropAllReferences(); |
88 | 94.3k | for (auto *I : VectorConstants) |
89 | 19.3k | I->dropAllReferences(); |
90 | 94.3k | ExprConstants.freeConstants(); |
91 | 94.3k | ArrayConstants.freeConstants(); |
92 | 94.3k | StructConstants.freeConstants(); |
93 | 94.3k | VectorConstants.freeConstants(); |
94 | 94.3k | InlineAsms.freeConstants(); |
95 | 94.3k | |
96 | 94.3k | CAZConstants.clear(); |
97 | 94.3k | CPNConstants.clear(); |
98 | 94.3k | UVConstants.clear(); |
99 | 94.3k | IntConstants.clear(); |
100 | 94.3k | FPConstants.clear(); |
101 | 94.3k | |
102 | 94.3k | for (auto &CDSConstant : CDSConstants) |
103 | 84.9k | delete CDSConstant.second; |
104 | 94.3k | CDSConstants.clear(); |
105 | 94.3k | |
106 | 94.3k | // Destroy attributes. |
107 | 94.3k | for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(), |
108 | 394k | E = AttrsSet.end(); I != E; ) { |
109 | 299k | FoldingSetIterator<AttributeImpl> Elem = I++; |
110 | 299k | delete &*Elem; |
111 | 299k | } |
112 | 94.3k | |
113 | 94.3k | // Destroy attribute lists. |
114 | 94.3k | for (FoldingSetIterator<AttributeListImpl> I = AttrsLists.begin(), |
115 | 94.3k | E = AttrsLists.end(); |
116 | 396k | I != E;) { |
117 | 302k | FoldingSetIterator<AttributeListImpl> Elem = I++; |
118 | 302k | delete &*Elem; |
119 | 302k | } |
120 | 94.3k | |
121 | 94.3k | // Destroy attribute node lists. |
122 | 94.3k | for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(), |
123 | 314k | E = AttrsSetNodes.end(); I != E; ) { |
124 | 219k | FoldingSetIterator<AttributeSetNode> Elem = I++; |
125 | 219k | delete &*Elem; |
126 | 219k | } |
127 | 94.3k | |
128 | 94.3k | // Destroy MetadataAsValues. |
129 | 94.3k | { |
130 | 94.3k | SmallVector<MetadataAsValue *, 8> MDVs; |
131 | 94.3k | MDVs.reserve(MetadataAsValues.size()); |
132 | 94.3k | for (auto &Pair : MetadataAsValues) |
133 | 21.4k | MDVs.push_back(Pair.second); |
134 | 94.3k | MetadataAsValues.clear(); |
135 | 94.3k | for (auto *V : MDVs) |
136 | 21.4k | delete V; |
137 | 94.3k | } |
138 | 94.3k | |
139 | 94.3k | // Destroy ValuesAsMetadata. |
140 | 94.3k | for (auto &Pair : ValuesAsMetadata) |
141 | 0 | delete Pair.second; |
142 | 94.3k | } |
143 | | |
144 | 1.07k | void LLVMContextImpl::dropTriviallyDeadConstantArrays() { |
145 | 1.07k | bool Changed; |
146 | 1.08k | do { |
147 | 1.08k | Changed = false; |
148 | 1.08k | |
149 | 1.29k | for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) { |
150 | 212 | auto *C = *I++; |
151 | 212 | if (C->use_empty()) { |
152 | 10 | Changed = true; |
153 | 10 | C->destroyConstant(); |
154 | 10 | } |
155 | 212 | } |
156 | 1.08k | } while (Changed); |
157 | 1.07k | } |
158 | | |
159 | 1.07k | void Module::dropTriviallyDeadConstantArrays() { |
160 | 1.07k | Context.pImpl->dropTriviallyDeadConstantArrays(); |
161 | 1.07k | } |
162 | | |
163 | | namespace llvm { |
164 | | |
165 | | /// 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. |
178 | 73.5M | static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); } |
179 | | |
180 | | } // end namespace llvm |
181 | | |
182 | 50.3k | unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) { |
183 | 50.3k | unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end()); |
184 | | #ifndef NDEBUG |
185 | | { |
186 | | SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end()); |
187 | | unsigned RawHash = calculateHash(MDs); |
188 | | assert(Hash == RawHash && |
189 | | "Expected hash of MDOperand to equal hash of Metadata*"); |
190 | | } |
191 | | #endif |
192 | | return Hash; |
193 | 50.3k | } |
194 | | |
195 | 1.50M | unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) { |
196 | 1.50M | return hash_combine_range(Ops.begin(), Ops.end()); |
197 | 1.50M | } |
198 | | |
199 | 325k | StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) { |
200 | 325k | uint32_t NewIdx = BundleTagCache.size(); |
201 | 325k | return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first); |
202 | 325k | } |
203 | | |
204 | 4.96k | void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { |
205 | 4.96k | Tags.resize(BundleTagCache.size()); |
206 | 4.96k | for (const auto &T : BundleTagCache) |
207 | 14.9k | Tags[T.second] = T.first(); |
208 | 4.96k | } |
209 | | |
210 | 70 | uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const { |
211 | 70 | auto I = BundleTagCache.find(Tag); |
212 | 70 | assert(I != BundleTagCache.end() && "Unknown tag!"); |
213 | 70 | return I->second; |
214 | 70 | } |
215 | | |
216 | 244k | SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) { |
217 | 244k | auto NewSSID = SSC.size(); |
218 | 244k | assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() && |
219 | 244k | "Hit the maximum number of synchronization scopes allowed!"); |
220 | 244k | return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second; |
221 | 244k | } |
222 | | |
223 | | void LLVMContextImpl::getSyncScopeNames( |
224 | 5.11k | SmallVectorImpl<StringRef> &SSNs) const { |
225 | 5.11k | SSNs.resize(SSC.size()); |
226 | 5.11k | for (const auto &SSE : SSC) |
227 | 11.0k | SSNs[SSE.second] = SSE.first(); |
228 | 5.11k | } |
229 | | |
230 | | /// Singleton instance of the OptBisect class. |
231 | | /// |
232 | | /// This singleton is accessed via the LLVMContext::getOptPassGate() function. |
233 | | /// It provides a mechanism to disable passes and individual optimizations at |
234 | | /// compile time based on a command line option (-opt-bisect-limit) in order to |
235 | | /// perform a bisecting search for optimization-related problems. |
236 | | /// |
237 | | /// Even if multiple LLVMContext objects are created, they will all return the |
238 | | /// same instance of OptBisect in order to provide a single bisect count. Any |
239 | | /// code that uses the OptBisect object should be serialized when bisection is |
240 | | /// enabled in order to enable a consistent bisect count. |
241 | | static ManagedStatic<OptBisect> OptBisector; |
242 | | |
243 | 43.7M | OptPassGate &LLVMContextImpl::getOptPassGate() const { |
244 | 43.7M | if (!OPG) |
245 | 42.5k | OPG = &(*OptBisector); |
246 | 43.7M | return *OPG; |
247 | 43.7M | } |
248 | | |
249 | 2 | void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) { |
250 | 2 | this->OPG = &OPG; |
251 | 2 | } |