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"
22 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
23 : TheTrueVal(nullptr), TheFalseVal(nullptr),
24 VoidTy(C, Type::VoidTyID),
25 LabelTy(C, Type::LabelTyID),
26 HalfTy(C, Type::HalfTyID),
27 FloatTy(C, Type::FloatTyID),
28 DoubleTy(C, Type::DoubleTyID),
29 MetadataTy(C, Type::MetadataTyID),
30 TokenTy(C, Type::TokenTyID),
31 X86_FP80Ty(C, Type::X86_FP80TyID),
32 FP128Ty(C, Type::FP128TyID),
33 PPC_FP128Ty(C, Type::PPC_FP128TyID),
34 X86_MMXTy(C, Type::X86_MMXTyID),
41 InlineAsmDiagHandler = nullptr;
42 InlineAsmDiagContext = nullptr;
43 DiagnosticHandler = nullptr;
44 DiagnosticContext = nullptr;
45 RespectDiagnosticFilters = false;
46 YieldCallback = nullptr;
47 YieldOpaqueHandle = nullptr;
48 NamedStructTypesUniqueID = 0;
52 struct DropReferences {
53 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
55 template <typename PairT> void operator()(const PairT &P) {
56 P.second->dropAllReferences();
60 // Temporary - drops pair.first instead of second.
62 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
64 template<typename PairT>
65 void operator()(const PairT &P) {
66 P.first->dropAllReferences();
71 LLVMContextImpl::~LLVMContextImpl() {
72 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
73 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
74 // the container. Avoid iterators during this operation:
75 while (!OwnedModules.empty())
76 delete *OwnedModules.begin();
78 // Drop references for MDNodes. Do this before Values get deleted to avoid
79 // unnecessary RAUW when nodes are still unresolved.
80 for (auto *I : DistinctMDNodes)
81 I->dropAllReferences();
82 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
83 for (auto *I : CLASS##s) \
84 I->dropAllReferences();
85 #include "llvm/IR/Metadata.def"
87 // Also drop references that come from the Value bridges.
88 for (auto &Pair : ValuesAsMetadata)
89 Pair.second->dropUsers();
90 for (auto &Pair : MetadataAsValues)
91 Pair.second->dropUse();
94 for (MDNode *I : DistinctMDNodes)
95 I->deleteAsSubclass();
96 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
97 for (CLASS * I : CLASS##s) \
99 #include "llvm/IR/Metadata.def"
101 // Free the constants.
102 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
104 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
106 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
108 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
110 ExprConstants.freeConstants();
111 ArrayConstants.freeConstants();
112 StructConstants.freeConstants();
113 VectorConstants.freeConstants();
114 DeleteContainerSeconds(CAZConstants);
115 DeleteContainerSeconds(CPNConstants);
116 DeleteContainerSeconds(UVConstants);
117 InlineAsms.freeConstants();
118 DeleteContainerSeconds(IntConstants);
119 DeleteContainerSeconds(FPConstants);
121 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
122 E = CDSConstants.end(); I != E; ++I)
124 CDSConstants.clear();
126 // Destroy attributes.
127 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
128 E = AttrsSet.end(); I != E; ) {
129 FoldingSetIterator<AttributeImpl> Elem = I++;
133 // Destroy attribute lists.
134 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
135 E = AttrsLists.end(); I != E; ) {
136 FoldingSetIterator<AttributeSetImpl> Elem = I++;
140 // Destroy attribute node lists.
141 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
142 E = AttrsSetNodes.end(); I != E; ) {
143 FoldingSetIterator<AttributeSetNode> Elem = I++;
147 // Destroy MetadataAsValues.
149 SmallVector<MetadataAsValue *, 8> MDVs;
150 MDVs.reserve(MetadataAsValues.size());
151 for (auto &Pair : MetadataAsValues)
152 MDVs.push_back(Pair.second);
153 MetadataAsValues.clear();
158 // Destroy ValuesAsMetadata.
159 for (auto &Pair : ValuesAsMetadata)
162 // Destroy MDStrings.
163 MDStringCache.clear();
166 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
171 for (auto I = ArrayConstants.map_begin(), E = ArrayConstants.map_end();
175 if (C->use_empty()) {
177 C->destroyConstant();
184 void Module::dropTriviallyDeadConstantArrays() {
185 Context.pImpl->dropTriviallyDeadConstantArrays();
189 /// \brief Make MDOperand transparent for hashing.
191 /// This overload of an implementation detail of the hashing library makes
192 /// MDOperand hash to the same value as a \a Metadata pointer.
194 /// Note that overloading \a hash_value() as follows:
197 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
200 /// does not cause MDOperand to be transparent. In particular, a bare pointer
201 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
202 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
205 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
206 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
209 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
210 unsigned RawHash = calculateHash(MDs);
211 assert(Hash == RawHash &&
212 "Expected hash of MDOperand to equal hash of Metadata*");
218 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
219 return hash_combine_range(Ops.begin(), Ops.end());
222 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
223 uint32_t NewIdx = BundleTagCache.size();
224 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
227 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
228 Tags.resize(BundleTagCache.size());
229 for (const auto &T : BundleTagCache)
230 Tags[T.second] = T.first();
233 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
234 auto I = BundleTagCache.find(Tag);
235 assert(I != BundleTagCache.end() && "Unknown tag!");
239 // ConstantsContext anchors
240 void UnaryConstantExpr::anchor() { }
242 void BinaryConstantExpr::anchor() { }
244 void SelectConstantExpr::anchor() { }
246 void ExtractElementConstantExpr::anchor() { }
248 void InsertElementConstantExpr::anchor() { }
250 void ShuffleVectorConstantExpr::anchor() { }
252 void ExtractValueConstantExpr::anchor() { }
254 void InsertValueConstantExpr::anchor() { }
256 void GetElementPtrConstantExpr::anchor() { }
258 void CompareConstantExpr::anchor() { }