1 //===-- LLVMContextImpl.cpp - Implement LLVMContextImpl -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the opaque LLVMContextImpl.
12 //===----------------------------------------------------------------------===//
14 #include "LLVMContextImpl.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/IR/Attributes.h"
17 #include "llvm/IR/DiagnosticInfo.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/OptBisect.h"
20 #include "llvm/Support/ManagedStatic.h"
24 LLVMContextImpl::LLVMContextImpl(LLVMContext
&C
)
25 : TheTrueVal(nullptr), TheFalseVal(nullptr),
26 VoidTy(C
, Type::VoidTyID
),
27 LabelTy(C
, Type::LabelTyID
),
28 HalfTy(C
, Type::HalfTyID
),
29 FloatTy(C
, Type::FloatTyID
),
30 DoubleTy(C
, Type::DoubleTyID
),
31 MetadataTy(C
, Type::MetadataTyID
),
32 TokenTy(C
, Type::TokenTyID
),
33 X86_FP80Ty(C
, Type::X86_FP80TyID
),
34 FP128Ty(C
, Type::FP128TyID
),
35 PPC_FP128Ty(C
, Type::PPC_FP128TyID
),
36 X86_MMXTy(C
, Type::X86_MMXTyID
),
43 InlineAsmDiagHandler
= nullptr;
44 InlineAsmDiagContext
= nullptr;
45 DiagnosticHandler
= nullptr;
46 DiagnosticContext
= nullptr;
47 RespectDiagnosticFilters
= false;
48 DiagnosticHotnessRequested
= false;
49 YieldCallback
= nullptr;
50 YieldOpaqueHandle
= nullptr;
51 NamedStructTypesUniqueID
= 0;
54 LLVMContextImpl::~LLVMContextImpl() {
55 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
56 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
57 // the container. Avoid iterators during this operation:
58 while (!OwnedModules
.empty())
59 delete *OwnedModules
.begin();
61 // Drop references for MDNodes. Do this before Values get deleted to avoid
62 // unnecessary RAUW when nodes are still unresolved.
63 for (auto *I
: DistinctMDNodes
)
64 I
->dropAllReferences();
65 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
66 for (auto *I : CLASS##s) \
67 I->dropAllReferences();
68 #include "llvm/IR/Metadata.def"
70 // Also drop references that come from the Value bridges.
71 for (auto &Pair
: ValuesAsMetadata
)
72 Pair
.second
->dropUsers();
73 for (auto &Pair
: MetadataAsValues
)
74 Pair
.second
->dropUse();
77 for (MDNode
*I
: DistinctMDNodes
)
78 I
->deleteAsSubclass();
79 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
80 for (CLASS * I : CLASS##s) \
82 #include "llvm/IR/Metadata.def"
84 // Free the constants.
85 for (auto *I
: ExprConstants
)
86 I
->dropAllReferences();
87 for (auto *I
: ArrayConstants
)
88 I
->dropAllReferences();
89 for (auto *I
: StructConstants
)
90 I
->dropAllReferences();
91 for (auto *I
: VectorConstants
)
92 I
->dropAllReferences();
93 ExprConstants
.freeConstants();
94 ArrayConstants
.freeConstants();
95 StructConstants
.freeConstants();
96 VectorConstants
.freeConstants();
97 InlineAsms
.freeConstants();
100 CPNConstants
.clear();
102 IntConstants
.clear();
105 for (auto &CDSConstant
: CDSConstants
)
106 delete CDSConstant
.second
;
107 CDSConstants
.clear();
109 // Destroy attributes.
110 for (FoldingSetIterator
<AttributeImpl
> I
= AttrsSet
.begin(),
111 E
= AttrsSet
.end(); I
!= E
; ) {
112 FoldingSetIterator
<AttributeImpl
> Elem
= I
++;
116 // Destroy attribute lists.
117 for (FoldingSetIterator
<AttributeListImpl
> I
= AttrsLists
.begin(),
118 E
= AttrsLists
.end();
120 FoldingSetIterator
<AttributeListImpl
> Elem
= I
++;
124 // Destroy attribute node lists.
125 for (FoldingSetIterator
<AttributeSetNode
> I
= AttrsSetNodes
.begin(),
126 E
= AttrsSetNodes
.end(); I
!= E
; ) {
127 FoldingSetIterator
<AttributeSetNode
> Elem
= I
++;
131 // Destroy MetadataAsValues.
133 SmallVector
<MetadataAsValue
*, 8> MDVs
;
134 MDVs
.reserve(MetadataAsValues
.size());
135 for (auto &Pair
: MetadataAsValues
)
136 MDVs
.push_back(Pair
.second
);
137 MetadataAsValues
.clear();
142 // Destroy ValuesAsMetadata.
143 for (auto &Pair
: ValuesAsMetadata
)
147 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
152 for (auto I
= ArrayConstants
.begin(), E
= ArrayConstants
.end(); I
!= E
;) {
154 if (C
->use_empty()) {
156 C
->destroyConstant();
163 void Module::dropTriviallyDeadConstantArrays() {
164 Context
.pImpl
->dropTriviallyDeadConstantArrays();
168 /// \brief Make MDOperand transparent for hashing.
170 /// This overload of an implementation detail of the hashing library makes
171 /// MDOperand hash to the same value as a \a Metadata pointer.
173 /// Note that overloading \a hash_value() as follows:
176 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
179 /// does not cause MDOperand to be transparent. In particular, a bare pointer
180 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
181 static const Metadata
*get_hashable_data(const MDOperand
&X
) { return X
.get(); }
184 unsigned MDNodeOpsKey::calculateHash(MDNode
*N
, unsigned Offset
) {
185 unsigned Hash
= hash_combine_range(N
->op_begin() + Offset
, N
->op_end());
188 SmallVector
<Metadata
*, 8> MDs(N
->op_begin() + Offset
, N
->op_end());
189 unsigned RawHash
= calculateHash(MDs
);
190 assert(Hash
== RawHash
&&
191 "Expected hash of MDOperand to equal hash of Metadata*");
197 unsigned MDNodeOpsKey::calculateHash(ArrayRef
<Metadata
*> Ops
) {
198 return hash_combine_range(Ops
.begin(), Ops
.end());
201 StringMapEntry
<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag
) {
202 uint32_t NewIdx
= BundleTagCache
.size();
203 return &*(BundleTagCache
.insert(std::make_pair(Tag
, NewIdx
)).first
);
206 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl
<StringRef
> &Tags
) const {
207 Tags
.resize(BundleTagCache
.size());
208 for (const auto &T
: BundleTagCache
)
209 Tags
[T
.second
] = T
.first();
212 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag
) const {
213 auto I
= BundleTagCache
.find(Tag
);
214 assert(I
!= BundleTagCache
.end() && "Unknown tag!");
218 // ConstantsContext anchors
219 void UnaryConstantExpr::anchor() { }
221 void BinaryConstantExpr::anchor() { }
223 void SelectConstantExpr::anchor() { }
225 void ExtractElementConstantExpr::anchor() { }
227 void InsertElementConstantExpr::anchor() { }
229 void ShuffleVectorConstantExpr::anchor() { }
231 void ExtractValueConstantExpr::anchor() { }
233 void InsertValueConstantExpr::anchor() { }
235 void GetElementPtrConstantExpr::anchor() { }
237 void CompareConstantExpr::anchor() { }
239 /// Singleton instance of the OptBisect class.
241 /// This singleton is accessed via the LLVMContext::getOptBisect() function. It
242 /// provides a mechanism to disable passes and individual optimizations at
243 /// compile time based on a command line option (-opt-bisect-limit) in order to
244 /// perform a bisecting search for optimization-related problems.
246 /// Even if multiple LLVMContext objects are created, they will all return the
247 /// same instance of OptBisect in order to provide a single bisect count. Any
248 /// code that uses the OptBisect object should be serialized when bisection is
249 /// enabled in order to enable a consistent bisect count.
250 static ManagedStatic
<OptBisect
> OptBisector
;
252 OptBisect
&LLVMContextImpl::getOptBisect() {