Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Attributes.h" |
Diego Novillo | 7f8af8b | 2014-05-22 14:19:46 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Module.h" |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 19 | #include "llvm/IR/OptBisect.h" |
| 20 | #include "llvm/Support/ManagedStatic.h" |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 23 | |
| 24 | LLVMContextImpl::LLVMContextImpl(LLVMContext &C) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 25 | : TheTrueVal(nullptr), TheFalseVal(nullptr), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 26 | VoidTy(C, Type::VoidTyID), |
| 27 | LabelTy(C, Type::LabelTyID), |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 28 | HalfTy(C, Type::HalfTyID), |
Jeffrey Yasskin | 4cfb3a7 | 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 | b611e3f | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 32 | TokenTy(C, Type::TokenTyID), |
Jeffrey Yasskin | 4cfb3a7 | 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 | baa5d04 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 36 | X86_MMXTy(C, Type::X86_MMXTyID), |
Jeffrey Yasskin | 4cfb3a7 | 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 | 7291802 | 2015-04-17 15:32:15 +0000 | [diff] [blame] | 41 | Int64Ty(C, 64), |
| 42 | Int128Ty(C, 128) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 43 | InlineAsmDiagHandler = nullptr; |
| 44 | InlineAsmDiagContext = nullptr; |
| 45 | DiagnosticHandler = nullptr; |
| 46 | DiagnosticContext = nullptr; |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 47 | RespectDiagnosticFilters = false; |
Adam Nemet | aad8160 | 2016-07-15 17:23:20 +0000 | [diff] [blame^] | 48 | DiagnosticHotnessRequested = false; |
Juergen Ributzka | 34390c7 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 49 | YieldCallback = nullptr; |
| 50 | YieldOpaqueHandle = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 51 | NamedStructTypesUniqueID = 0; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | LLVMContextImpl::~LLVMContextImpl() { |
David Blaikie | 4c82a80 | 2014-04-21 21:27:19 +0000 | [diff] [blame] | 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(); |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 60 | |
| 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(); |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 65 | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
Duncan P. N. Exon Smith | 408f5a2 | 2015-01-20 01:18:32 +0000 | [diff] [blame] | 66 | for (auto *I : CLASS##s) \ |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 67 | I->dropAllReferences(); |
Duncan P. N. Exon Smith | 408f5a2 | 2015-01-20 01:18:32 +0000 | [diff] [blame] | 68 | #include "llvm/IR/Metadata.def" |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 69 | |
| 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(); |
| 75 | |
| 76 | // Destroy MDNodes. |
Duncan P. N. Exon Smith | 2bc00f4 | 2015-01-19 23:13:14 +0000 | [diff] [blame] | 77 | for (MDNode *I : DistinctMDNodes) |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 78 | I->deleteAsSubclass(); |
Duncan P. N. Exon Smith | 55ca964 | 2015-08-03 17:26:41 +0000 | [diff] [blame] | 79 | #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ |
| 80 | for (CLASS * I : CLASS##s) \ |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 81 | delete I; |
Duncan P. N. Exon Smith | 408f5a2 | 2015-01-20 01:18:32 +0000 | [diff] [blame] | 82 | #include "llvm/IR/Metadata.def" |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 83 | |
Benjamin Kramer | cb36bec | 2015-01-22 21:43:01 +0000 | [diff] [blame] | 84 | // Free the constants. |
Duncan P. N. Exon Smith | ef06d44 | 2016-04-06 17:56:08 +0000 | [diff] [blame] | 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(); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 93 | ExprConstants.freeConstants(); |
| 94 | ArrayConstants.freeConstants(); |
| 95 | StructConstants.freeConstants(); |
| 96 | VectorConstants.freeConstants(); |
David Blaikie | cb2818f | 2014-11-25 02:26:22 +0000 | [diff] [blame] | 97 | DeleteContainerSeconds(CAZConstants); |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 98 | DeleteContainerSeconds(CPNConstants); |
| 99 | DeleteContainerSeconds(UVConstants); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 100 | InlineAsms.freeConstants(); |
Nick Lewycky | e9bb9a0 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 101 | DeleteContainerSeconds(IntConstants); |
| 102 | DeleteContainerSeconds(FPConstants); |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 103 | |
| 104 | for (auto &CDSConstant : CDSConstants) |
| 105 | delete CDSConstant.second; |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 106 | CDSConstants.clear(); |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 107 | |
| 108 | // Destroy attributes. |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 109 | for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(), |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 110 | E = AttrsSet.end(); I != E; ) { |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 111 | FoldingSetIterator<AttributeImpl> Elem = I++; |
Benjamin Kramer | 6bbdf70 | 2012-10-14 08:48:40 +0000 | [diff] [blame] | 112 | delete &*Elem; |
| 113 | } |
| 114 | |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 115 | // Destroy attribute lists. |
Bill Wendling | 6848e38 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 116 | for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(), |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 117 | E = AttrsLists.end(); I != E; ) { |
Bill Wendling | 6848e38 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 118 | FoldingSetIterator<AttributeSetImpl> Elem = I++; |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 119 | delete &*Elem; |
| 120 | } |
| 121 | |
Bill Wendling | 164a4fb | 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 | 5bf8fef | 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 | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 143 | } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 144 | |
Manman Ren | dab999d | 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 | ef06d44 | 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 | dab999d | 2015-01-20 19:24:59 +0000 | [diff] [blame] | 152 | if (C->use_empty()) { |
| 153 | Changed = true; |
| 154 | C->destroyConstant(); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | } while (Changed); |
| 159 | } |
| 160 | |
| 161 | void Module::dropTriviallyDeadConstantArrays() { |
| 162 | Context.pImpl->dropTriviallyDeadConstantArrays(); |
| 163 | } |
| 164 | |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 165 | namespace llvm { |
| 166 | /// \brief Make MDOperand transparent for hashing. |
| 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(); } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 180 | } |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 181 | |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 182 | unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) { |
| 183 | unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end()); |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 184 | #ifndef NDEBUG |
| 185 | { |
Duncan P. N. Exon Smith | fed199a | 2015-01-20 00:01:43 +0000 | [diff] [blame] | 186 | SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end()); |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame] | 187 | unsigned RawHash = calculateHash(MDs); |
| 188 | assert(Hash == RawHash && |
| 189 | "Expected hash of MDOperand to equal hash of Metadata*"); |
| 190 | } |
| 191 | #endif |
| 192 | return Hash; |
| 193 | } |
| 194 | |
| 195 | unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) { |
| 196 | return hash_combine_range(Ops.begin(), Ops.end()); |
| 197 | } |
| 198 | |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame] | 199 | StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) { |
| 200 | uint32_t NewIdx = BundleTagCache.size(); |
| 201 | return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first); |
| 202 | } |
| 203 | |
| 204 | void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { |
| 205 | Tags.resize(BundleTagCache.size()); |
| 206 | for (const auto &T : BundleTagCache) |
| 207 | Tags[T.second] = T.first(); |
| 208 | } |
| 209 | |
| 210 | uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const { |
| 211 | auto I = BundleTagCache.find(Tag); |
| 212 | assert(I != BundleTagCache.end() && "Unknown tag!"); |
| 213 | return I->second; |
| 214 | } |
| 215 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 216 | // ConstantsContext anchors |
| 217 | void UnaryConstantExpr::anchor() { } |
| 218 | |
| 219 | void BinaryConstantExpr::anchor() { } |
| 220 | |
| 221 | void SelectConstantExpr::anchor() { } |
| 222 | |
| 223 | void ExtractElementConstantExpr::anchor() { } |
| 224 | |
| 225 | void InsertElementConstantExpr::anchor() { } |
| 226 | |
| 227 | void ShuffleVectorConstantExpr::anchor() { } |
| 228 | |
| 229 | void ExtractValueConstantExpr::anchor() { } |
| 230 | |
| 231 | void InsertValueConstantExpr::anchor() { } |
| 232 | |
| 233 | void GetElementPtrConstantExpr::anchor() { } |
| 234 | |
| 235 | void CompareConstantExpr::anchor() { } |
Philip Reames | 2b45395 | 2015-01-16 20:07:33 +0000 | [diff] [blame] | 236 | |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 237 | /// Singleton instance of the OptBisect class. |
| 238 | /// |
| 239 | /// This singleton is accessed via the LLVMContext::getOptBisect() function. It |
| 240 | /// provides a mechanism to disable passes and individual optimizations at |
| 241 | /// compile time based on a command line option (-opt-bisect-limit) in order to |
| 242 | /// perform a bisecting search for optimization-related problems. |
| 243 | /// |
| 244 | /// Even if multiple LLVMContext objects are created, they will all return the |
| 245 | /// same instance of OptBisect in order to provide a single bisect count. Any |
| 246 | /// code that uses the OptBisect object should be serialized when bisection is |
| 247 | /// enabled in order to enable a consistent bisect count. |
| 248 | static ManagedStatic<OptBisect> OptBisector; |
| 249 | |
| 250 | OptBisect &LLVMContextImpl::getOptBisect() { |
| 251 | return *OptBisector; |
| 252 | } |