annotate llvm/lib/IR/LLVMContextImpl.cpp @ 223:5f17cb93ff66 llvm-original

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