annotate llvm/lib/IR/LLVMContextImpl.cpp @ 173:0572611fdcc8 llvm10 llvm12

reorgnization done
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 11:55:54 +0900
parents 1d019706d866
children f935e5e0dbe7 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file implements the opaque LLVMContextImpl.
anatofuz
parents:
diff changeset
10 //
anatofuz
parents:
diff changeset
11 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 #include "LLVMContextImpl.h"
anatofuz
parents:
diff changeset
14 #include "llvm/ADT/SetVector.h"
anatofuz
parents:
diff changeset
15 #include "llvm/IR/Module.h"
anatofuz
parents:
diff changeset
16 #include "llvm/IR/OptBisect.h"
anatofuz
parents:
diff changeset
17 #include "llvm/IR/Type.h"
anatofuz
parents:
diff changeset
18 #include "llvm/Support/ManagedStatic.h"
anatofuz
parents:
diff changeset
19 #include <cassert>
anatofuz
parents:
diff changeset
20 #include <utility>
anatofuz
parents:
diff changeset
21
anatofuz
parents:
diff changeset
22 using namespace llvm;
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
anatofuz
parents:
diff changeset
25 : DiagHandler(std::make_unique<DiagnosticHandler>()),
anatofuz
parents:
diff changeset
26 VoidTy(C, Type::VoidTyID),
anatofuz
parents:
diff changeset
27 LabelTy(C, Type::LabelTyID),
anatofuz
parents:
diff changeset
28 HalfTy(C, Type::HalfTyID),
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
29 BFloatTy(C, Type::BFloatTyID),
150
anatofuz
parents:
diff changeset
30 FloatTy(C, Type::FloatTyID),
anatofuz
parents:
diff changeset
31 DoubleTy(C, Type::DoubleTyID),
anatofuz
parents:
diff changeset
32 MetadataTy(C, Type::MetadataTyID),
anatofuz
parents:
diff changeset
33 TokenTy(C, Type::TokenTyID),
anatofuz
parents:
diff changeset
34 X86_FP80Ty(C, Type::X86_FP80TyID),
anatofuz
parents:
diff changeset
35 FP128Ty(C, Type::FP128TyID),
anatofuz
parents:
diff changeset
36 PPC_FP128Ty(C, Type::PPC_FP128TyID),
anatofuz
parents:
diff changeset
37 X86_MMXTy(C, Type::X86_MMXTyID),
anatofuz
parents:
diff changeset
38 Int1Ty(C, 1),
anatofuz
parents:
diff changeset
39 Int8Ty(C, 8),
anatofuz
parents:
diff changeset
40 Int16Ty(C, 16),
anatofuz
parents:
diff changeset
41 Int32Ty(C, 32),
anatofuz
parents:
diff changeset
42 Int64Ty(C, 64),
anatofuz
parents:
diff changeset
43 Int128Ty(C, 128) {}
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 LLVMContextImpl::~LLVMContextImpl() {
anatofuz
parents:
diff changeset
46 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
anatofuz
parents:
diff changeset
47 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
anatofuz
parents:
diff changeset
48 // the container. Avoid iterators during this operation:
anatofuz
parents:
diff changeset
49 while (!OwnedModules.empty())
anatofuz
parents:
diff changeset
50 delete *OwnedModules.begin();
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 #ifndef NDEBUG
anatofuz
parents:
diff changeset
53 // Check for metadata references from leaked Instructions.
anatofuz
parents:
diff changeset
54 for (auto &Pair : InstructionMetadata)
anatofuz
parents:
diff changeset
55 Pair.first->dump();
anatofuz
parents:
diff changeset
56 assert(InstructionMetadata.empty() &&
anatofuz
parents:
diff changeset
57 "Instructions with metadata have been leaked");
anatofuz
parents:
diff changeset
58 #endif
anatofuz
parents:
diff changeset
59
anatofuz
parents:
diff changeset
60 // Drop references for MDNodes. Do this before Values get deleted to avoid
anatofuz
parents:
diff changeset
61 // unnecessary RAUW when nodes are still unresolved.
anatofuz
parents:
diff changeset
62 for (auto *I : DistinctMDNodes)
anatofuz
parents:
diff changeset
63 I->dropAllReferences();
anatofuz
parents:
diff changeset
64 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
anatofuz
parents:
diff changeset
65 for (auto *I : CLASS##s) \
anatofuz
parents:
diff changeset
66 I->dropAllReferences();
anatofuz
parents:
diff changeset
67 #include "llvm/IR/Metadata.def"
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 // Also drop references that come from the Value bridges.
anatofuz
parents:
diff changeset
70 for (auto &Pair : ValuesAsMetadata)
anatofuz
parents:
diff changeset
71 Pair.second->dropUsers();
anatofuz
parents:
diff changeset
72 for (auto &Pair : MetadataAsValues)
anatofuz
parents:
diff changeset
73 Pair.second->dropUse();
anatofuz
parents:
diff changeset
74
anatofuz
parents:
diff changeset
75 // Destroy MDNodes.
anatofuz
parents:
diff changeset
76 for (MDNode *I : DistinctMDNodes)
anatofuz
parents:
diff changeset
77 I->deleteAsSubclass();
anatofuz
parents:
diff changeset
78 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
anatofuz
parents:
diff changeset
79 for (CLASS * I : CLASS##s) \
anatofuz
parents:
diff changeset
80 delete I;
anatofuz
parents:
diff changeset
81 #include "llvm/IR/Metadata.def"
anatofuz
parents:
diff changeset
82
anatofuz
parents:
diff changeset
83 // Free the constants.
anatofuz
parents:
diff changeset
84 for (auto *I : ExprConstants)
anatofuz
parents:
diff changeset
85 I->dropAllReferences();
anatofuz
parents:
diff changeset
86 for (auto *I : ArrayConstants)
anatofuz
parents:
diff changeset
87 I->dropAllReferences();
anatofuz
parents:
diff changeset
88 for (auto *I : StructConstants)
anatofuz
parents:
diff changeset
89 I->dropAllReferences();
anatofuz
parents:
diff changeset
90 for (auto *I : VectorConstants)
anatofuz
parents:
diff changeset
91 I->dropAllReferences();
anatofuz
parents:
diff changeset
92 ExprConstants.freeConstants();
anatofuz
parents:
diff changeset
93 ArrayConstants.freeConstants();
anatofuz
parents:
diff changeset
94 StructConstants.freeConstants();
anatofuz
parents:
diff changeset
95 VectorConstants.freeConstants();
anatofuz
parents:
diff changeset
96 InlineAsms.freeConstants();
anatofuz
parents:
diff changeset
97
anatofuz
parents:
diff changeset
98 CAZConstants.clear();
anatofuz
parents:
diff changeset
99 CPNConstants.clear();
anatofuz
parents:
diff changeset
100 UVConstants.clear();
anatofuz
parents:
diff changeset
101 IntConstants.clear();
anatofuz
parents:
diff changeset
102 FPConstants.clear();
anatofuz
parents:
diff changeset
103
anatofuz
parents:
diff changeset
104 for (auto &CDSConstant : CDSConstants)
anatofuz
parents:
diff changeset
105 delete CDSConstant.second;
anatofuz
parents:
diff changeset
106 CDSConstants.clear();
anatofuz
parents:
diff changeset
107
anatofuz
parents:
diff changeset
108 // Destroy attribute node lists.
anatofuz
parents:
diff changeset
109 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
anatofuz
parents:
diff changeset
110 E = AttrsSetNodes.end(); I != E; ) {
anatofuz
parents:
diff changeset
111 FoldingSetIterator<AttributeSetNode> Elem = I++;
anatofuz
parents:
diff changeset
112 delete &*Elem;
anatofuz
parents:
diff changeset
113 }
anatofuz
parents:
diff changeset
114
anatofuz
parents:
diff changeset
115 // Destroy MetadataAsValues.
anatofuz
parents:
diff changeset
116 {
anatofuz
parents:
diff changeset
117 SmallVector<MetadataAsValue *, 8> MDVs;
anatofuz
parents:
diff changeset
118 MDVs.reserve(MetadataAsValues.size());
anatofuz
parents:
diff changeset
119 for (auto &Pair : MetadataAsValues)
anatofuz
parents:
diff changeset
120 MDVs.push_back(Pair.second);
anatofuz
parents:
diff changeset
121 MetadataAsValues.clear();
anatofuz
parents:
diff changeset
122 for (auto *V : MDVs)
anatofuz
parents:
diff changeset
123 delete V;
anatofuz
parents:
diff changeset
124 }
anatofuz
parents:
diff changeset
125
anatofuz
parents:
diff changeset
126 // Destroy ValuesAsMetadata.
anatofuz
parents:
diff changeset
127 for (auto &Pair : ValuesAsMetadata)
anatofuz
parents:
diff changeset
128 delete Pair.second;
anatofuz
parents:
diff changeset
129 }
anatofuz
parents:
diff changeset
130
anatofuz
parents:
diff changeset
131 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
anatofuz
parents:
diff changeset
132 SmallSetVector<ConstantArray *, 4> WorkList(ArrayConstants.begin(),
anatofuz
parents:
diff changeset
133 ArrayConstants.end());
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 while (!WorkList.empty()) {
anatofuz
parents:
diff changeset
136 ConstantArray *C = WorkList.pop_back_val();
anatofuz
parents:
diff changeset
137 if (C->use_empty()) {
anatofuz
parents:
diff changeset
138 for (const Use &Op : C->operands()) {
anatofuz
parents:
diff changeset
139 if (auto *COp = dyn_cast<ConstantArray>(Op))
anatofuz
parents:
diff changeset
140 WorkList.insert(COp);
anatofuz
parents:
diff changeset
141 }
anatofuz
parents:
diff changeset
142 C->destroyConstant();
anatofuz
parents:
diff changeset
143 }
anatofuz
parents:
diff changeset
144 }
anatofuz
parents:
diff changeset
145 }
anatofuz
parents:
diff changeset
146
anatofuz
parents:
diff changeset
147 void Module::dropTriviallyDeadConstantArrays() {
anatofuz
parents:
diff changeset
148 Context.pImpl->dropTriviallyDeadConstantArrays();
anatofuz
parents:
diff changeset
149 }
anatofuz
parents:
diff changeset
150
anatofuz
parents:
diff changeset
151 namespace llvm {
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 /// Make MDOperand transparent for hashing.
anatofuz
parents:
diff changeset
154 ///
anatofuz
parents:
diff changeset
155 /// This overload of an implementation detail of the hashing library makes
anatofuz
parents:
diff changeset
156 /// MDOperand hash to the same value as a \a Metadata pointer.
anatofuz
parents:
diff changeset
157 ///
anatofuz
parents:
diff changeset
158 /// Note that overloading \a hash_value() as follows:
anatofuz
parents:
diff changeset
159 ///
anatofuz
parents:
diff changeset
160 /// \code
anatofuz
parents:
diff changeset
161 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
anatofuz
parents:
diff changeset
162 /// \endcode
anatofuz
parents:
diff changeset
163 ///
anatofuz
parents:
diff changeset
164 /// does not cause MDOperand to be transparent. In particular, a bare pointer
anatofuz
parents:
diff changeset
165 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
anatofuz
parents:
diff changeset
166 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
anatofuz
parents:
diff changeset
167
anatofuz
parents:
diff changeset
168 } // end namespace llvm
anatofuz
parents:
diff changeset
169
anatofuz
parents:
diff changeset
170 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
anatofuz
parents:
diff changeset
171 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
anatofuz
parents:
diff changeset
172 #ifndef NDEBUG
anatofuz
parents:
diff changeset
173 {
anatofuz
parents:
diff changeset
174 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
anatofuz
parents:
diff changeset
175 unsigned RawHash = calculateHash(MDs);
anatofuz
parents:
diff changeset
176 assert(Hash == RawHash &&
anatofuz
parents:
diff changeset
177 "Expected hash of MDOperand to equal hash of Metadata*");
anatofuz
parents:
diff changeset
178 }
anatofuz
parents:
diff changeset
179 #endif
anatofuz
parents:
diff changeset
180 return Hash;
anatofuz
parents:
diff changeset
181 }
anatofuz
parents:
diff changeset
182
anatofuz
parents:
diff changeset
183 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
anatofuz
parents:
diff changeset
184 return hash_combine_range(Ops.begin(), Ops.end());
anatofuz
parents:
diff changeset
185 }
anatofuz
parents:
diff changeset
186
anatofuz
parents:
diff changeset
187 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
anatofuz
parents:
diff changeset
188 uint32_t NewIdx = BundleTagCache.size();
anatofuz
parents:
diff changeset
189 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
anatofuz
parents:
diff changeset
190 }
anatofuz
parents:
diff changeset
191
anatofuz
parents:
diff changeset
192 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
anatofuz
parents:
diff changeset
193 Tags.resize(BundleTagCache.size());
anatofuz
parents:
diff changeset
194 for (const auto &T : BundleTagCache)
anatofuz
parents:
diff changeset
195 Tags[T.second] = T.first();
anatofuz
parents:
diff changeset
196 }
anatofuz
parents:
diff changeset
197
anatofuz
parents:
diff changeset
198 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
anatofuz
parents:
diff changeset
199 auto I = BundleTagCache.find(Tag);
anatofuz
parents:
diff changeset
200 assert(I != BundleTagCache.end() && "Unknown tag!");
anatofuz
parents:
diff changeset
201 return I->second;
anatofuz
parents:
diff changeset
202 }
anatofuz
parents:
diff changeset
203
anatofuz
parents:
diff changeset
204 SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
anatofuz
parents:
diff changeset
205 auto NewSSID = SSC.size();
anatofuz
parents:
diff changeset
206 assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
anatofuz
parents:
diff changeset
207 "Hit the maximum number of synchronization scopes allowed!");
anatofuz
parents:
diff changeset
208 return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
anatofuz
parents:
diff changeset
209 }
anatofuz
parents:
diff changeset
210
anatofuz
parents:
diff changeset
211 void LLVMContextImpl::getSyncScopeNames(
anatofuz
parents:
diff changeset
212 SmallVectorImpl<StringRef> &SSNs) const {
anatofuz
parents:
diff changeset
213 SSNs.resize(SSC.size());
anatofuz
parents:
diff changeset
214 for (const auto &SSE : SSC)
anatofuz
parents:
diff changeset
215 SSNs[SSE.second] = SSE.first();
anatofuz
parents:
diff changeset
216 }
anatofuz
parents:
diff changeset
217
anatofuz
parents:
diff changeset
218 /// Singleton instance of the OptBisect class.
anatofuz
parents:
diff changeset
219 ///
anatofuz
parents:
diff changeset
220 /// This singleton is accessed via the LLVMContext::getOptPassGate() function.
anatofuz
parents:
diff changeset
221 /// It provides a mechanism to disable passes and individual optimizations at
anatofuz
parents:
diff changeset
222 /// compile time based on a command line option (-opt-bisect-limit) in order to
anatofuz
parents:
diff changeset
223 /// perform a bisecting search for optimization-related problems.
anatofuz
parents:
diff changeset
224 ///
anatofuz
parents:
diff changeset
225 /// Even if multiple LLVMContext objects are created, they will all return the
anatofuz
parents:
diff changeset
226 /// same instance of OptBisect in order to provide a single bisect count. Any
anatofuz
parents:
diff changeset
227 /// code that uses the OptBisect object should be serialized when bisection is
anatofuz
parents:
diff changeset
228 /// enabled in order to enable a consistent bisect count.
anatofuz
parents:
diff changeset
229 static ManagedStatic<OptBisect> OptBisector;
anatofuz
parents:
diff changeset
230
anatofuz
parents:
diff changeset
231 OptPassGate &LLVMContextImpl::getOptPassGate() const {
anatofuz
parents:
diff changeset
232 if (!OPG)
anatofuz
parents:
diff changeset
233 OPG = &(*OptBisector);
anatofuz
parents:
diff changeset
234 return *OPG;
anatofuz
parents:
diff changeset
235 }
anatofuz
parents:
diff changeset
236
anatofuz
parents:
diff changeset
237 void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
anatofuz
parents:
diff changeset
238 this->OPG = &OPG;
anatofuz
parents:
diff changeset
239 }