annotate llvm/lib/IR/LLVMContextImpl.cpp @ 266:00f31e85ec16 default tip

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