Eugene Zelenko | f193400 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 1 | //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===// |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 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" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Module.h" |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 16 | #include "llvm/IR/OptBisect.h" |
Eugene Zelenko | f193400 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Type.h" |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ManagedStatic.h" |
Eugene Zelenko | f193400 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 19 | #include <cassert> |
| 20 | #include <utility> |
| 21 | |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 23 | |
| 24 | LLVMContextImpl::LLVMContextImpl(LLVMContext &C) |
Vivek Pandya | 18b4c37 | 2017-09-15 20:10:09 +0000 | [diff] [blame] | 25 | : DiagHandler(llvm::make_unique<DiagnosticHandler>()), |
| 26 | VoidTy(C, Type::VoidTyID), |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 27 | LabelTy(C, Type::LabelTyID), |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 28 | HalfTy(C, Type::HalfTyID), |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 29 | FloatTy(C, Type::FloatTyID), |
| 30 | DoubleTy(C, Type::DoubleTyID), |
| 31 | MetadataTy(C, Type::MetadataTyID), |
David Majnemer | 2dacece | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 32 | TokenTy(C, Type::TokenTyID), |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 33 | X86_FP80Ty(C, Type::X86_FP80TyID), |
| 34 | FP128Ty(C, Type::FP128TyID), |
| 35 | PPC_FP128Ty(C, Type::PPC_FP128TyID), |
Dale Johannesen | bb811a2 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 36 | X86_MMXTy(C, Type::X86_MMXTyID), |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 37 | Int1Ty(C, 1), |
| 38 | Int8Ty(C, 8), |
| 39 | Int16Ty(C, 16), |
| 40 | Int32Ty(C, 32), |
Kit Barton | fbfd58a | 2015-04-17 15:32:15 +0000 | [diff] [blame] | 41 | Int64Ty(C, 64), |
Eugene Zelenko | f193400 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 42 | Int128Ty(C, 128) {} |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 43 | |
| 44 | LLVMContextImpl::~LLVMContextImpl() { |
David Blaikie | b86fbcb | 2014-04-21 21:27:19 +0000 | [diff] [blame] | 45 | // NOTE: We need to delete the contents of OwnedModules, but Module's dtor |
| 46 | // will call LLVMContextImpl::removeModule, thus invalidating iterators into |
| 47 | // the container. Avoid iterators during this operation: |
| 48 | while (!OwnedModules.empty()) |
| 49 | delete *OwnedModules.begin(); |
Duncan P. N. Exon Smith | 0294f63 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 50 | |
Vedant Kumar | c55ef47 | 2018-06-29 20:13:13 +0000 | [diff] [blame] | 51 | #ifndef NDEBUG |
| 52 | // Check for metadata references from leaked Instructions. |
| 53 | for (auto &Pair : InstructionMetadata) |
| 54 | Pair.first->dump(); |
| 55 | assert(InstructionMetadata.empty() && |
| 56 | "Instructions with metadata have been leaked"); |
| 57 | #endif |
| 58 | |
Duncan P. N. Exon Smith | 0294f63 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 59 | // Drop references for MDNodes. Do this before Values get deleted to avoid |
| 60 | // unnecessary RAUW when nodes are still unresolved. |
| 61 | for (auto *I : DistinctMDNodes) |
| 62 | I->dropAllReferences(); |
Duncan P. N. Exon Smith | c61bc48 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 63 | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
Duncan P. N. Exon Smith | 47ddb76 | 2015-01-20 01:18:32 +0000 | [diff] [blame] | 64 | for (auto *I : CLASS##s) \ |
Duncan P. N. Exon Smith | 0294f63 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 65 | I->dropAllReferences(); |
Duncan P. N. Exon Smith | 47ddb76 | 2015-01-20 01:18:32 +0000 | [diff] [blame] | 66 | #include "llvm/IR/Metadata.def" |
Duncan P. N. Exon Smith | 0294f63 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 67 | |
| 68 | // Also drop references that come from the Value bridges. |
| 69 | for (auto &Pair : ValuesAsMetadata) |
| 70 | Pair.second->dropUsers(); |
| 71 | for (auto &Pair : MetadataAsValues) |
| 72 | Pair.second->dropUse(); |
| 73 | |
| 74 | // Destroy MDNodes. |
Duncan P. N. Exon Smith | b061786 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 75 | for (MDNode *I : DistinctMDNodes) |
Duncan P. N. Exon Smith | 0294f63 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 76 | I->deleteAsSubclass(); |
Duncan P. N. Exon Smith | c61bc48 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 77 | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
| 78 | for (CLASS * I : CLASS##s) \ |
Duncan P. N. Exon Smith | 0294f63 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 79 | delete I; |
Duncan P. N. Exon Smith | 47ddb76 | 2015-01-20 01:18:32 +0000 | [diff] [blame] | 80 | #include "llvm/IR/Metadata.def" |
Duncan P. N. Exon Smith | 0294f63 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 81 | |
Benjamin Kramer | 1552e50 | 2015-01-22 21:43:01 +0000 | [diff] [blame] | 82 | // Free the constants. |
Duncan P. N. Exon Smith | a15c0e9 | 2016-04-06 17:56:08 +0000 | [diff] [blame] | 83 | for (auto *I : ExprConstants) |
| 84 | I->dropAllReferences(); |
| 85 | for (auto *I : ArrayConstants) |
| 86 | I->dropAllReferences(); |
| 87 | for (auto *I : StructConstants) |
| 88 | I->dropAllReferences(); |
| 89 | for (auto *I : VectorConstants) |
| 90 | I->dropAllReferences(); |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 91 | ExprConstants.freeConstants(); |
| 92 | ArrayConstants.freeConstants(); |
| 93 | StructConstants.freeConstants(); |
| 94 | VectorConstants.freeConstants(); |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 95 | InlineAsms.freeConstants(); |
Justin Lebar | a2e3c5a | 2016-10-10 16:26:13 +0000 | [diff] [blame] | 96 | |
| 97 | CAZConstants.clear(); |
| 98 | CPNConstants.clear(); |
| 99 | UVConstants.clear(); |
| 100 | IntConstants.clear(); |
| 101 | FPConstants.clear(); |
Benjamin Kramer | e96e21f | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 102 | |
| 103 | for (auto &CDSConstant : CDSConstants) |
| 104 | delete CDSConstant.second; |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 105 | CDSConstants.clear(); |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 106 | |
| 107 | // Destroy attributes. |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 108 | for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(), |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 109 | E = AttrsSet.end(); I != E; ) { |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 110 | FoldingSetIterator<AttributeImpl> Elem = I++; |
Benjamin Kramer | fd8d62c | 2012-10-14 08:48:40 +0000 | [diff] [blame] | 111 | delete &*Elem; |
| 112 | } |
| 113 | |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 114 | // Destroy attribute lists. |
Reid Kleckner | 6707770 | 2017-03-21 16:57:19 +0000 | [diff] [blame] | 115 | for (FoldingSetIterator<AttributeListImpl> I = AttrsLists.begin(), |
| 116 | E = AttrsLists.end(); |
| 117 | I != E;) { |
| 118 | FoldingSetIterator<AttributeListImpl> Elem = I++; |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 119 | delete &*Elem; |
| 120 | } |
| 121 | |
Bill Wendling | 8b1f2f3 | 2013-01-24 00:14:46 +0000 | [diff] [blame] | 122 | // Destroy attribute node lists. |
| 123 | for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(), |
| 124 | E = AttrsSetNodes.end(); I != E; ) { |
| 125 | FoldingSetIterator<AttributeSetNode> Elem = I++; |
| 126 | delete &*Elem; |
| 127 | } |
| 128 | |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 129 | // Destroy MetadataAsValues. |
| 130 | { |
| 131 | SmallVector<MetadataAsValue *, 8> MDVs; |
| 132 | MDVs.reserve(MetadataAsValues.size()); |
| 133 | for (auto &Pair : MetadataAsValues) |
| 134 | MDVs.push_back(Pair.second); |
| 135 | MetadataAsValues.clear(); |
| 136 | for (auto *V : MDVs) |
| 137 | delete V; |
| 138 | } |
| 139 | |
| 140 | // Destroy ValuesAsMetadata. |
| 141 | for (auto &Pair : ValuesAsMetadata) |
| 142 | delete Pair.second; |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 143 | } |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 144 | |
Manman Ren | 0d12d4e | 2015-01-20 19:24:59 +0000 | [diff] [blame] | 145 | void LLVMContextImpl::dropTriviallyDeadConstantArrays() { |
| 146 | bool Changed; |
| 147 | do { |
| 148 | Changed = false; |
| 149 | |
Duncan P. N. Exon Smith | a15c0e9 | 2016-04-06 17:56:08 +0000 | [diff] [blame] | 150 | for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) { |
| 151 | auto *C = *I++; |
Manman Ren | 0d12d4e | 2015-01-20 19:24:59 +0000 | [diff] [blame] | 152 | if (C->use_empty()) { |
| 153 | Changed = true; |
| 154 | C->destroyConstant(); |
| 155 | } |
| 156 | } |
Manman Ren | 0d12d4e | 2015-01-20 19:24:59 +0000 | [diff] [blame] | 157 | } while (Changed); |
| 158 | } |
| 159 | |
| 160 | void Module::dropTriviallyDeadConstantArrays() { |
| 161 | Context.pImpl->dropTriviallyDeadConstantArrays(); |
| 162 | } |
| 163 | |
Duncan P. N. Exon Smith | fce53dd | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 164 | namespace llvm { |
Eugene Zelenko | f193400 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 165 | |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 166 | /// Make MDOperand transparent for hashing. |
Duncan P. N. Exon Smith | fce53dd | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 167 | /// |
| 168 | /// This overload of an implementation detail of the hashing library makes |
| 169 | /// MDOperand hash to the same value as a \a Metadata pointer. |
| 170 | /// |
| 171 | /// Note that overloading \a hash_value() as follows: |
| 172 | /// |
| 173 | /// \code |
| 174 | /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); } |
| 175 | /// \endcode |
| 176 | /// |
| 177 | /// does not cause MDOperand to be transparent. In particular, a bare pointer |
| 178 | /// doesn't get hashed before it's combined, whereas \a MDOperand would. |
| 179 | static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); } |
Eugene Zelenko | f193400 | 2017-06-19 22:05:08 +0000 | [diff] [blame] | 180 | |
| 181 | } // end namespace llvm |
Duncan P. N. Exon Smith | fce53dd | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 182 | |
Duncan P. N. Exon Smith | 0a9f921 | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 183 | unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) { |
| 184 | unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end()); |
Duncan P. N. Exon Smith | fce53dd | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 185 | #ifndef NDEBUG |
| 186 | { |
Duncan P. N. Exon Smith | 0a9f921 | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 187 | SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end()); |
Duncan P. N. Exon Smith | fce53dd | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 188 | unsigned RawHash = calculateHash(MDs); |
| 189 | assert(Hash == RawHash && |
| 190 | "Expected hash of MDOperand to equal hash of Metadata*"); |
| 191 | } |
| 192 | #endif |
| 193 | return Hash; |
| 194 | } |
| 195 | |
| 196 | unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) { |
| 197 | return hash_combine_range(Ops.begin(), Ops.end()); |
| 198 | } |
| 199 | |
Sanjoy Das | 5b674c0 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 200 | StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) { |
| 201 | uint32_t NewIdx = BundleTagCache.size(); |
| 202 | return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first); |
| 203 | } |
| 204 | |
| 205 | void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { |
| 206 | Tags.resize(BundleTagCache.size()); |
| 207 | for (const auto &T : BundleTagCache) |
| 208 | Tags[T.second] = T.first(); |
| 209 | } |
| 210 | |
| 211 | uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const { |
| 212 | auto I = BundleTagCache.find(Tag); |
| 213 | assert(I != BundleTagCache.end() && "Unknown tag!"); |
| 214 | return I->second; |
| 215 | } |
| 216 | |
Konstantin Zhuravlyov | 8f85685 | 2017-07-11 22:23:00 +0000 | [diff] [blame] | 217 | SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) { |
| 218 | auto NewSSID = SSC.size(); |
| 219 | assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() && |
| 220 | "Hit the maximum number of synchronization scopes allowed!"); |
| 221 | return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second; |
| 222 | } |
| 223 | |
| 224 | void LLVMContextImpl::getSyncScopeNames( |
| 225 | SmallVectorImpl<StringRef> &SSNs) const { |
| 226 | SSNs.resize(SSC.size()); |
| 227 | for (const auto &SSE : SSC) |
| 228 | SSNs[SSE.second] = SSE.first(); |
| 229 | } |
| 230 | |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 231 | /// Singleton instance of the OptBisect class. |
| 232 | /// |
Fedor Sergeev | 558f76f | 2018-03-27 16:57:20 +0000 | [diff] [blame] | 233 | /// This singleton is accessed via the LLVMContext::getOptPassGate() function. |
| 234 | /// It provides a mechanism to disable passes and individual optimizations at |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 235 | /// compile time based on a command line option (-opt-bisect-limit) in order to |
| 236 | /// perform a bisecting search for optimization-related problems. |
| 237 | /// |
| 238 | /// Even if multiple LLVMContext objects are created, they will all return the |
| 239 | /// same instance of OptBisect in order to provide a single bisect count. Any |
| 240 | /// code that uses the OptBisect object should be serialized when bisection is |
| 241 | /// enabled in order to enable a consistent bisect count. |
| 242 | static ManagedStatic<OptBisect> OptBisector; |
| 243 | |
Fedor Sergeev | 46d5e2b | 2018-04-05 10:29:37 +0000 | [diff] [blame] | 244 | OptPassGate &LLVMContextImpl::getOptPassGate() const { |
| 245 | if (!OPG) |
| 246 | OPG = &(*OptBisector); |
| 247 | return *OPG; |
| 248 | } |
| 249 | |
| 250 | void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) { |
| 251 | this->OPG = &OPG; |
Andrew Kaylor | 1e455c5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 252 | } |