]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/IR/LLVMContextImpl.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306325, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / IR / LLVMContextImpl.cpp
1 //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the opaque LLVMContextImpl.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "LLVMContextImpl.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/IR/OptBisect.h"
17 #include "llvm/IR/Type.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include <cassert>
20 #include <utility>
21
22 using namespace llvm;
23
24 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
25   : VoidTy(C, Type::VoidTyID),
26     LabelTy(C, Type::LabelTyID),
27     HalfTy(C, Type::HalfTyID),
28     FloatTy(C, Type::FloatTyID),
29     DoubleTy(C, Type::DoubleTyID),
30     MetadataTy(C, Type::MetadataTyID),
31     TokenTy(C, Type::TokenTyID),
32     X86_FP80Ty(C, Type::X86_FP80TyID),
33     FP128Ty(C, Type::FP128TyID),
34     PPC_FP128Ty(C, Type::PPC_FP128TyID),
35     X86_MMXTy(C, Type::X86_MMXTyID),
36     Int1Ty(C, 1),
37     Int8Ty(C, 8),
38     Int16Ty(C, 16),
39     Int32Ty(C, 32),
40     Int64Ty(C, 64),
41     Int128Ty(C, 128) {}
42
43 LLVMContextImpl::~LLVMContextImpl() {
44   // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
45   // will call LLVMContextImpl::removeModule, thus invalidating iterators into
46   // the container. Avoid iterators during this operation:
47   while (!OwnedModules.empty())
48     delete *OwnedModules.begin();
49
50   // Drop references for MDNodes.  Do this before Values get deleted to avoid
51   // unnecessary RAUW when nodes are still unresolved.
52   for (auto *I : DistinctMDNodes)
53     I->dropAllReferences();
54 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
55   for (auto *I : CLASS##s)                                                     \
56     I->dropAllReferences();
57 #include "llvm/IR/Metadata.def"
58
59   // Also drop references that come from the Value bridges.
60   for (auto &Pair : ValuesAsMetadata)
61     Pair.second->dropUsers();
62   for (auto &Pair : MetadataAsValues)
63     Pair.second->dropUse();
64
65   // Destroy MDNodes.
66   for (MDNode *I : DistinctMDNodes)
67     I->deleteAsSubclass();
68 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
69   for (CLASS * I : CLASS##s)                                                   \
70     delete I;
71 #include "llvm/IR/Metadata.def"
72
73   // Free the constants.
74   for (auto *I : ExprConstants)
75     I->dropAllReferences();
76   for (auto *I : ArrayConstants)
77     I->dropAllReferences();
78   for (auto *I : StructConstants)
79     I->dropAllReferences();
80   for (auto *I : VectorConstants)
81     I->dropAllReferences();
82   ExprConstants.freeConstants();
83   ArrayConstants.freeConstants();
84   StructConstants.freeConstants();
85   VectorConstants.freeConstants();
86   InlineAsms.freeConstants();
87
88   CAZConstants.clear();
89   CPNConstants.clear();
90   UVConstants.clear();
91   IntConstants.clear();
92   FPConstants.clear();
93
94   for (auto &CDSConstant : CDSConstants)
95     delete CDSConstant.second;
96   CDSConstants.clear();
97
98   // Destroy attributes.
99   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
100          E = AttrsSet.end(); I != E; ) {
101     FoldingSetIterator<AttributeImpl> Elem = I++;
102     delete &*Elem;
103   }
104
105   // Destroy attribute lists.
106   for (FoldingSetIterator<AttributeListImpl> I = AttrsLists.begin(),
107                                              E = AttrsLists.end();
108        I != E;) {
109     FoldingSetIterator<AttributeListImpl> Elem = I++;
110     delete &*Elem;
111   }
112
113   // Destroy attribute node lists.
114   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
115          E = AttrsSetNodes.end(); I != E; ) {
116     FoldingSetIterator<AttributeSetNode> Elem = I++;
117     delete &*Elem;
118   }
119
120   // Destroy MetadataAsValues.
121   {
122     SmallVector<MetadataAsValue *, 8> MDVs;
123     MDVs.reserve(MetadataAsValues.size());
124     for (auto &Pair : MetadataAsValues)
125       MDVs.push_back(Pair.second);
126     MetadataAsValues.clear();
127     for (auto *V : MDVs)
128       delete V;
129   }
130
131   // Destroy ValuesAsMetadata.
132   for (auto &Pair : ValuesAsMetadata)
133     delete Pair.second;
134 }
135
136 void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
137   bool Changed;
138   do {
139     Changed = false;
140
141     for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
142       auto *C = *I++;
143       if (C->use_empty()) {
144         Changed = true;
145         C->destroyConstant();
146       }
147     }
148   } while (Changed);
149 }
150
151 void Module::dropTriviallyDeadConstantArrays() {
152   Context.pImpl->dropTriviallyDeadConstantArrays();
153 }
154
155 namespace llvm {
156
157 /// \brief Make MDOperand transparent for hashing.
158 ///
159 /// This overload of an implementation detail of the hashing library makes
160 /// MDOperand hash to the same value as a \a Metadata pointer.
161 ///
162 /// Note that overloading \a hash_value() as follows:
163 ///
164 /// \code
165 ///     size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
166 /// \endcode
167 ///
168 /// does not cause MDOperand to be transparent.  In particular, a bare pointer
169 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
170 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
171
172 } // end namespace llvm
173
174 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
175   unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
176 #ifndef NDEBUG
177   {
178     SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
179     unsigned RawHash = calculateHash(MDs);
180     assert(Hash == RawHash &&
181            "Expected hash of MDOperand to equal hash of Metadata*");
182   }
183 #endif
184   return Hash;
185 }
186
187 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
188   return hash_combine_range(Ops.begin(), Ops.end());
189 }
190
191 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
192   uint32_t NewIdx = BundleTagCache.size();
193   return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
194 }
195
196 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
197   Tags.resize(BundleTagCache.size());
198   for (const auto &T : BundleTagCache)
199     Tags[T.second] = T.first();
200 }
201
202 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
203   auto I = BundleTagCache.find(Tag);
204   assert(I != BundleTagCache.end() && "Unknown tag!");
205   return I->second;
206 }
207
208 /// Singleton instance of the OptBisect class.
209 ///
210 /// This singleton is accessed via the LLVMContext::getOptBisect() function.  It
211 /// provides a mechanism to disable passes and individual optimizations at
212 /// compile time based on a command line option (-opt-bisect-limit) in order to
213 /// perform a bisecting search for optimization-related problems.
214 ///
215 /// Even if multiple LLVMContext objects are created, they will all return the
216 /// same instance of OptBisect in order to provide a single bisect count.  Any
217 /// code that uses the OptBisect object should be serialized when bisection is
218 /// enabled in order to enable a consistent bisect count.
219 static ManagedStatic<OptBisect> OptBisector;
220
221 OptBisect &LLVMContextImpl::getOptBisect() {
222   return *OptBisector;
223 }