annotate llvm/lib/IR/LLVMContextImpl.cpp @ 221:79ff65ed7e25

LLVM12 Original
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:15:29 +0900
parents 0572611fdcc8
children dd44ba33042e 5f17cb93ff66
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),
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
38 X86_AMXTy(C, Type::X86_AMXTyID),
150
anatofuz
parents:
diff changeset
39 Int1Ty(C, 1),
anatofuz
parents:
diff changeset
40 Int8Ty(C, 8),
anatofuz
parents:
diff changeset
41 Int16Ty(C, 16),
anatofuz
parents:
diff changeset
42 Int32Ty(C, 32),
anatofuz
parents:
diff changeset
43 Int64Ty(C, 64),
anatofuz
parents:
diff changeset
44 Int128Ty(C, 128) {}
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 LLVMContextImpl::~LLVMContextImpl() {
anatofuz
parents:
diff changeset
47 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
anatofuz
parents:
diff changeset
48 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
anatofuz
parents:
diff changeset
49 // the container. Avoid iterators during this operation:
anatofuz
parents:
diff changeset
50 while (!OwnedModules.empty())
anatofuz
parents:
diff changeset
51 delete *OwnedModules.begin();
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 #ifndef NDEBUG
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
54 // Check for metadata references from leaked Values.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
55 for (auto &Pair : ValueMetadata)
150
anatofuz
parents:
diff changeset
56 Pair.first->dump();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
57 assert(ValueMetadata.empty() && "Values with metadata have been leaked");
150
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();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
101 PVConstants.clear();
150
anatofuz
parents:
diff changeset
102 IntConstants.clear();
anatofuz
parents:
diff changeset
103 FPConstants.clear();
anatofuz
parents:
diff changeset
104 CDSConstants.clear();
anatofuz
parents:
diff changeset
105
anatofuz
parents:
diff changeset
106 // Destroy attribute node lists.
anatofuz
parents:
diff changeset
107 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
anatofuz
parents:
diff changeset
108 E = AttrsSetNodes.end(); I != E; ) {
anatofuz
parents:
diff changeset
109 FoldingSetIterator<AttributeSetNode> Elem = I++;
anatofuz
parents:
diff changeset
110 delete &*Elem;
anatofuz
parents:
diff changeset
111 }
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 // Destroy MetadataAsValues.
anatofuz
parents:
diff changeset
114 {
anatofuz
parents:
diff changeset
115 SmallVector<MetadataAsValue *, 8> MDVs;
anatofuz
parents:
diff changeset
116 MDVs.reserve(MetadataAsValues.size());
anatofuz
parents:
diff changeset
117 for (auto &Pair : MetadataAsValues)
anatofuz
parents:
diff changeset
118 MDVs.push_back(Pair.second);
anatofuz
parents:
diff changeset
119 MetadataAsValues.clear();
anatofuz
parents:
diff changeset
120 for (auto *V : MDVs)
anatofuz
parents:
diff changeset
121 delete V;
anatofuz
parents:
diff changeset
122 }
anatofuz
parents:
diff changeset
123
anatofuz
parents:
diff changeset
124 // Destroy ValuesAsMetadata.
anatofuz
parents:
diff changeset
125 for (auto &Pair : ValuesAsMetadata)
anatofuz
parents:
diff changeset
126 delete Pair.second;
anatofuz
parents:
diff changeset
127 }
anatofuz
parents:
diff changeset
128
anatofuz
parents:
diff changeset
129 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
130 SmallSetVector<ConstantArray *, 4> WorkList;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
131
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
132 // When ArrayConstants are of substantial size and only a few in them are
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
133 // dead, starting WorkList with all elements of ArrayConstants can be
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
134 // wasteful. Instead, starting WorkList with only elements that have empty
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
135 // uses.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
136 for (ConstantArray *C : ArrayConstants)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
137 if (C->use_empty())
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
138 WorkList.insert(C);
150
anatofuz
parents:
diff changeset
139
anatofuz
parents:
diff changeset
140 while (!WorkList.empty()) {
anatofuz
parents:
diff changeset
141 ConstantArray *C = WorkList.pop_back_val();
anatofuz
parents:
diff changeset
142 if (C->use_empty()) {
anatofuz
parents:
diff changeset
143 for (const Use &Op : C->operands()) {
anatofuz
parents:
diff changeset
144 if (auto *COp = dyn_cast<ConstantArray>(Op))
anatofuz
parents:
diff changeset
145 WorkList.insert(COp);
anatofuz
parents:
diff changeset
146 }
anatofuz
parents:
diff changeset
147 C->destroyConstant();
anatofuz
parents:
diff changeset
148 }
anatofuz
parents:
diff changeset
149 }
anatofuz
parents:
diff changeset
150 }
anatofuz
parents:
diff changeset
151
anatofuz
parents:
diff changeset
152 void Module::dropTriviallyDeadConstantArrays() {
anatofuz
parents:
diff changeset
153 Context.pImpl->dropTriviallyDeadConstantArrays();
anatofuz
parents:
diff changeset
154 }
anatofuz
parents:
diff changeset
155
anatofuz
parents:
diff changeset
156 namespace llvm {
anatofuz
parents:
diff changeset
157
anatofuz
parents:
diff changeset
158 /// Make MDOperand transparent for hashing.
anatofuz
parents:
diff changeset
159 ///
anatofuz
parents:
diff changeset
160 /// This overload of an implementation detail of the hashing library makes
anatofuz
parents:
diff changeset
161 /// MDOperand hash to the same value as a \a Metadata pointer.
anatofuz
parents:
diff changeset
162 ///
anatofuz
parents:
diff changeset
163 /// Note that overloading \a hash_value() as follows:
anatofuz
parents:
diff changeset
164 ///
anatofuz
parents:
diff changeset
165 /// \code
anatofuz
parents:
diff changeset
166 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
anatofuz
parents:
diff changeset
167 /// \endcode
anatofuz
parents:
diff changeset
168 ///
anatofuz
parents:
diff changeset
169 /// does not cause MDOperand to be transparent. In particular, a bare pointer
anatofuz
parents:
diff changeset
170 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
anatofuz
parents:
diff changeset
171 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
anatofuz
parents:
diff changeset
172
anatofuz
parents:
diff changeset
173 } // end namespace llvm
anatofuz
parents:
diff changeset
174
anatofuz
parents:
diff changeset
175 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
anatofuz
parents:
diff changeset
176 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
anatofuz
parents:
diff changeset
177 #ifndef NDEBUG
anatofuz
parents:
diff changeset
178 {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
179 SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset));
150
anatofuz
parents:
diff changeset
180 unsigned RawHash = calculateHash(MDs);
anatofuz
parents:
diff changeset
181 assert(Hash == RawHash &&
anatofuz
parents:
diff changeset
182 "Expected hash of MDOperand to equal hash of Metadata*");
anatofuz
parents:
diff changeset
183 }
anatofuz
parents:
diff changeset
184 #endif
anatofuz
parents:
diff changeset
185 return Hash;
anatofuz
parents:
diff changeset
186 }
anatofuz
parents:
diff changeset
187
anatofuz
parents:
diff changeset
188 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
anatofuz
parents:
diff changeset
189 return hash_combine_range(Ops.begin(), Ops.end());
anatofuz
parents:
diff changeset
190 }
anatofuz
parents:
diff changeset
191
anatofuz
parents:
diff changeset
192 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
anatofuz
parents:
diff changeset
193 uint32_t NewIdx = BundleTagCache.size();
anatofuz
parents:
diff changeset
194 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
anatofuz
parents:
diff changeset
195 }
anatofuz
parents:
diff changeset
196
anatofuz
parents:
diff changeset
197 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
anatofuz
parents:
diff changeset
198 Tags.resize(BundleTagCache.size());
anatofuz
parents:
diff changeset
199 for (const auto &T : BundleTagCache)
anatofuz
parents:
diff changeset
200 Tags[T.second] = T.first();
anatofuz
parents:
diff changeset
201 }
anatofuz
parents:
diff changeset
202
anatofuz
parents:
diff changeset
203 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
anatofuz
parents:
diff changeset
204 auto I = BundleTagCache.find(Tag);
anatofuz
parents:
diff changeset
205 assert(I != BundleTagCache.end() && "Unknown tag!");
anatofuz
parents:
diff changeset
206 return I->second;
anatofuz
parents:
diff changeset
207 }
anatofuz
parents:
diff changeset
208
anatofuz
parents:
diff changeset
209 SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
anatofuz
parents:
diff changeset
210 auto NewSSID = SSC.size();
anatofuz
parents:
diff changeset
211 assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
anatofuz
parents:
diff changeset
212 "Hit the maximum number of synchronization scopes allowed!");
anatofuz
parents:
diff changeset
213 return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
anatofuz
parents:
diff changeset
214 }
anatofuz
parents:
diff changeset
215
anatofuz
parents:
diff changeset
216 void LLVMContextImpl::getSyncScopeNames(
anatofuz
parents:
diff changeset
217 SmallVectorImpl<StringRef> &SSNs) const {
anatofuz
parents:
diff changeset
218 SSNs.resize(SSC.size());
anatofuz
parents:
diff changeset
219 for (const auto &SSE : SSC)
anatofuz
parents:
diff changeset
220 SSNs[SSE.second] = SSE.first();
anatofuz
parents:
diff changeset
221 }
anatofuz
parents:
diff changeset
222
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
223 /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
224 /// singleton OptBisect if not explicitly set.
150
anatofuz
parents:
diff changeset
225 OptPassGate &LLVMContextImpl::getOptPassGate() const {
anatofuz
parents:
diff changeset
226 if (!OPG)
anatofuz
parents:
diff changeset
227 OPG = &(*OptBisector);
anatofuz
parents:
diff changeset
228 return *OPG;
anatofuz
parents:
diff changeset
229 }
anatofuz
parents:
diff changeset
230
anatofuz
parents:
diff changeset
231 void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
anatofuz
parents:
diff changeset
232 this->OPG = &OPG;
anatofuz
parents:
diff changeset
233 }