1 //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the opaque LLVMContextImpl.
11 //===----------------------------------------------------------------------===//
13 #include "LLVMContextImpl.h"
14 #include "AttributeImpl.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/ADT/StringMapEntry.h"
17 #include "llvm/ADT/iterator.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/IR/DiagnosticHandler.h"
20 #include "llvm/IR/LLVMRemarkStreamer.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/OptBisect.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/IR/Use.h"
25 #include "llvm/IR/User.h"
26 #include "llvm/Remarks/RemarkStreamer.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/TypeSize.h"
36 LLVMContextImpl::LLVMContextImpl(LLVMContext
&C
)
37 : DiagHandler(std::make_unique
<DiagnosticHandler
>()),
38 VoidTy(C
, Type::VoidTyID
), LabelTy(C
, Type::LabelTyID
),
39 HalfTy(C
, Type::HalfTyID
), BFloatTy(C
, Type::BFloatTyID
),
40 FloatTy(C
, Type::FloatTyID
), DoubleTy(C
, Type::DoubleTyID
),
41 MetadataTy(C
, Type::MetadataTyID
), TokenTy(C
, Type::TokenTyID
),
42 X86_FP80Ty(C
, Type::X86_FP80TyID
), FP128Ty(C
, Type::FP128TyID
),
43 PPC_FP128Ty(C
, Type::PPC_FP128TyID
), X86_MMXTy(C
, Type::X86_MMXTyID
),
44 X86_AMXTy(C
, Type::X86_AMXTyID
), Int1Ty(C
, 1), Int8Ty(C
, 8),
45 Int16Ty(C
, 16), Int32Ty(C
, 32), Int64Ty(C
, 64), Int128Ty(C
, 128) {}
47 LLVMContextImpl::~LLVMContextImpl() {
49 // Check that any variable location records that fell off the end of a block
50 // when it's terminator was removed were eventually replaced. This assertion
51 // firing indicates that DPValues went missing during the lifetime of the
53 assert(TrailingDPValues
.empty() && "DPValue records in blocks not cleaned");
56 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
57 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
58 // the container. Avoid iterators during this operation:
59 while (!OwnedModules
.empty())
60 delete *OwnedModules
.begin();
63 // Check for metadata references from leaked Values.
64 for (auto &Pair
: ValueMetadata
)
66 assert(ValueMetadata
.empty() && "Values with metadata have been leaked");
69 // Drop references for MDNodes. Do this before Values get deleted to avoid
70 // unnecessary RAUW when nodes are still unresolved.
71 for (auto *I
: DistinctMDNodes
)
72 I
->dropAllReferences();
73 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
74 for (auto *I : CLASS##s) \
75 I->dropAllReferences();
76 #include "llvm/IR/Metadata.def"
78 // Also drop references that come from the Value bridges.
79 for (auto &Pair
: ValuesAsMetadata
)
80 Pair
.second
->dropUsers();
81 for (auto &Pair
: MetadataAsValues
)
82 Pair
.second
->dropUse();
83 // Do not untrack ValueAsMetadata references for DIArgLists, as they have
84 // already been more efficiently untracked above.
85 for (DIArgList
*AL
: DIArgLists
) {
86 AL
->dropAllReferences(/* Untrack */ false);
92 for (MDNode
*I
: DistinctMDNodes
)
93 I
->deleteAsSubclass();
94 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
95 for (CLASS * I : CLASS##s) \
97 #include "llvm/IR/Metadata.def"
99 // Free the constants.
100 for (auto *I
: ExprConstants
)
101 I
->dropAllReferences();
102 for (auto *I
: ArrayConstants
)
103 I
->dropAllReferences();
104 for (auto *I
: StructConstants
)
105 I
->dropAllReferences();
106 for (auto *I
: VectorConstants
)
107 I
->dropAllReferences();
108 ExprConstants
.freeConstants();
109 ArrayConstants
.freeConstants();
110 StructConstants
.freeConstants();
111 VectorConstants
.freeConstants();
112 InlineAsms
.freeConstants();
114 CAZConstants
.clear();
115 CPNConstants
.clear();
116 CTNConstants
.clear();
119 IntZeroConstants
.clear();
120 IntOneConstants
.clear();
121 IntConstants
.clear();
123 CDSConstants
.clear();
125 // Destroy attribute node lists.
126 for (FoldingSetIterator
<AttributeSetNode
> I
= AttrsSetNodes
.begin(),
127 E
= AttrsSetNodes
.end(); I
!= E
; ) {
128 FoldingSetIterator
<AttributeSetNode
> Elem
= I
++;
132 // Destroy MetadataAsValues.
134 SmallVector
<MetadataAsValue
*, 8> MDVs
;
135 MDVs
.reserve(MetadataAsValues
.size());
136 for (auto &Pair
: MetadataAsValues
)
137 MDVs
.push_back(Pair
.second
);
138 MetadataAsValues
.clear();
143 // Destroy ValuesAsMetadata.
144 for (auto &Pair
: ValuesAsMetadata
)
148 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
149 SmallSetVector
<ConstantArray
*, 4> WorkList
;
151 // When ArrayConstants are of substantial size and only a few in them are
152 // dead, starting WorkList with all elements of ArrayConstants can be
153 // wasteful. Instead, starting WorkList with only elements that have empty
155 for (ConstantArray
*C
: ArrayConstants
)
159 while (!WorkList
.empty()) {
160 ConstantArray
*C
= WorkList
.pop_back_val();
161 if (C
->use_empty()) {
162 for (const Use
&Op
: C
->operands()) {
163 if (auto *COp
= dyn_cast
<ConstantArray
>(Op
))
164 WorkList
.insert(COp
);
166 C
->destroyConstant();
171 void Module::dropTriviallyDeadConstantArrays() {
172 Context
.pImpl
->dropTriviallyDeadConstantArrays();
177 /// Make MDOperand transparent for hashing.
179 /// This overload of an implementation detail of the hashing library makes
180 /// MDOperand hash to the same value as a \a Metadata pointer.
182 /// Note that overloading \a hash_value() as follows:
185 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
188 /// does not cause MDOperand to be transparent. In particular, a bare pointer
189 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
190 static const Metadata
*get_hashable_data(const MDOperand
&X
) { return X
.get(); }
192 } // end namespace llvm
194 unsigned MDNodeOpsKey::calculateHash(MDNode
*N
, unsigned Offset
) {
195 unsigned Hash
= hash_combine_range(N
->op_begin() + Offset
, N
->op_end());
198 SmallVector
<Metadata
*, 8> MDs(drop_begin(N
->operands(), Offset
));
199 unsigned RawHash
= calculateHash(MDs
);
200 assert(Hash
== RawHash
&&
201 "Expected hash of MDOperand to equal hash of Metadata*");
207 unsigned MDNodeOpsKey::calculateHash(ArrayRef
<Metadata
*> Ops
) {
208 return hash_combine_range(Ops
.begin(), Ops
.end());
211 StringMapEntry
<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag
) {
212 uint32_t NewIdx
= BundleTagCache
.size();
213 return &*(BundleTagCache
.insert(std::make_pair(Tag
, NewIdx
)).first
);
216 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl
<StringRef
> &Tags
) const {
217 Tags
.resize(BundleTagCache
.size());
218 for (const auto &T
: BundleTagCache
)
219 Tags
[T
.second
] = T
.first();
222 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag
) const {
223 auto I
= BundleTagCache
.find(Tag
);
224 assert(I
!= BundleTagCache
.end() && "Unknown tag!");
228 SyncScope::ID
LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN
) {
229 auto NewSSID
= SSC
.size();
230 assert(NewSSID
< std::numeric_limits
<SyncScope::ID
>::max() &&
231 "Hit the maximum number of synchronization scopes allowed!");
232 return SSC
.insert(std::make_pair(SSN
, SyncScope::ID(NewSSID
))).first
->second
;
235 void LLVMContextImpl::getSyncScopeNames(
236 SmallVectorImpl
<StringRef
> &SSNs
) const {
237 SSNs
.resize(SSC
.size());
238 for (const auto &SSE
: SSC
)
239 SSNs
[SSE
.second
] = SSE
.first();
242 /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
243 /// singleton OptBisect if not explicitly set.
244 OptPassGate
&LLVMContextImpl::getOptPassGate() const {
246 OPG
= &getGlobalPassGate();
250 void LLVMContextImpl::setOptPassGate(OptPassGate
& OPG
) {