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" |
Philip Reames | 2b45395 | 2015-01-16 20:07:33 +0000 | [diff] [blame] | 18 | #include "llvm/IR/GCStrategy.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 22 | |
| 23 | LLVMContextImpl::LLVMContextImpl(LLVMContext &C) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 24 | : TheTrueVal(nullptr), TheFalseVal(nullptr), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 25 | VoidTy(C, Type::VoidTyID), |
| 26 | LabelTy(C, Type::LabelTyID), |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 27 | HalfTy(C, Type::HalfTyID), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 28 | FloatTy(C, Type::FloatTyID), |
| 29 | DoubleTy(C, Type::DoubleTyID), |
| 30 | MetadataTy(C, Type::MetadataTyID), |
| 31 | X86_FP80Ty(C, Type::X86_FP80TyID), |
| 32 | FP128Ty(C, Type::FP128TyID), |
| 33 | PPC_FP128Ty(C, Type::PPC_FP128TyID), |
Dale Johannesen | baa5d04 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 34 | X86_MMXTy(C, Type::X86_MMXTyID), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 35 | Int1Ty(C, 1), |
| 36 | Int8Ty(C, 8), |
| 37 | Int16Ty(C, 16), |
| 38 | Int32Ty(C, 32), |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 39 | Int64Ty(C, 64) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 40 | InlineAsmDiagHandler = nullptr; |
| 41 | InlineAsmDiagContext = nullptr; |
| 42 | DiagnosticHandler = nullptr; |
| 43 | DiagnosticContext = nullptr; |
Duncan P. N. Exon Smith | 30c9242 | 2014-10-01 18:36:03 +0000 | [diff] [blame] | 44 | RespectDiagnosticFilters = false; |
Juergen Ributzka | 34390c7 | 2014-05-16 02:33:15 +0000 | [diff] [blame] | 45 | YieldCallback = nullptr; |
| 46 | YieldOpaqueHandle = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 47 | NamedStructTypesUniqueID = 0; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | struct DropReferences { |
| 52 | // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' |
| 53 | // is a Constant*. |
Diego Novillo | 7f8af8b | 2014-05-22 14:19:46 +0000 | [diff] [blame] | 54 | template <typename PairT> void operator()(const PairT &P) { |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 55 | P.second->dropAllReferences(); |
| 56 | } |
| 57 | }; |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 58 | |
| 59 | // Temporary - drops pair.first instead of second. |
| 60 | struct DropFirst { |
| 61 | // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' |
| 62 | // is a Constant*. |
| 63 | template<typename PairT> |
| 64 | void operator()(const PairT &P) { |
| 65 | P.first->dropAllReferences(); |
| 66 | } |
| 67 | }; |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 70 | LLVMContextImpl::~LLVMContextImpl() { |
David Blaikie | 4c82a80 | 2014-04-21 21:27:19 +0000 | [diff] [blame] | 71 | // NOTE: We need to delete the contents of OwnedModules, but Module's dtor |
| 72 | // will call LLVMContextImpl::removeModule, thus invalidating iterators into |
| 73 | // the container. Avoid iterators during this operation: |
| 74 | while (!OwnedModules.empty()) |
| 75 | delete *OwnedModules.begin(); |
Duncan P. N. Exon Smith | e16d587 | 2015-01-14 21:58:17 +0000 | [diff] [blame] | 76 | |
| 77 | // Drop references for MDNodes. Do this before Values get deleted to avoid |
| 78 | // unnecessary RAUW when nodes are still unresolved. |
| 79 | for (auto *I : DistinctMDNodes) |
| 80 | I->dropAllReferences(); |
| 81 | for (auto *I : MDTuples) |
| 82 | I->dropAllReferences(); |
| 83 | for (auto *I : MDLocations) |
| 84 | I->dropAllReferences(); |
| 85 | |
| 86 | // Also drop references that come from the Value bridges. |
| 87 | for (auto &Pair : ValuesAsMetadata) |
| 88 | Pair.second->dropUsers(); |
| 89 | for (auto &Pair : MetadataAsValues) |
| 90 | Pair.second->dropUse(); |
| 91 | |
| 92 | // Destroy MDNodes. |
| 93 | for (UniquableMDNode *I : DistinctMDNodes) |
| 94 | I->deleteAsSubclass(); |
| 95 | for (MDTuple *I : MDTuples) |
| 96 | delete I; |
| 97 | for (MDLocation *I : MDLocations) |
| 98 | delete I; |
| 99 | |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 100 | // Free the constants. This is important to do here to ensure that they are |
| 101 | // freed before the LeakDetector is torn down. |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 102 | std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), |
Duncan P. N. Exon Smith | 317c139 | 2014-08-19 16:39:58 +0000 | [diff] [blame] | 103 | DropFirst()); |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 104 | std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 105 | DropFirst()); |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 106 | std::for_each(StructConstants.map_begin(), StructConstants.map_end(), |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 107 | DropFirst()); |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 108 | std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(), |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 109 | DropFirst()); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 110 | ExprConstants.freeConstants(); |
| 111 | ArrayConstants.freeConstants(); |
| 112 | StructConstants.freeConstants(); |
| 113 | VectorConstants.freeConstants(); |
David Blaikie | cb2818f | 2014-11-25 02:26:22 +0000 | [diff] [blame] | 114 | DeleteContainerSeconds(CAZConstants); |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 115 | DeleteContainerSeconds(CPNConstants); |
| 116 | DeleteContainerSeconds(UVConstants); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 117 | InlineAsms.freeConstants(); |
Nick Lewycky | e9bb9a0 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 118 | DeleteContainerSeconds(IntConstants); |
| 119 | DeleteContainerSeconds(FPConstants); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 121 | for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(), |
| 122 | E = CDSConstants.end(); I != E; ++I) |
| 123 | delete I->second; |
| 124 | CDSConstants.clear(); |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 125 | |
| 126 | // Destroy attributes. |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 127 | for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(), |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 128 | E = AttrsSet.end(); I != E; ) { |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 129 | FoldingSetIterator<AttributeImpl> Elem = I++; |
Benjamin Kramer | 6bbdf70 | 2012-10-14 08:48:40 +0000 | [diff] [blame] | 130 | delete &*Elem; |
| 131 | } |
| 132 | |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 133 | // Destroy attribute lists. |
Bill Wendling | 6848e38 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 134 | for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(), |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 135 | E = AttrsLists.end(); I != E; ) { |
Bill Wendling | 6848e38 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 136 | FoldingSetIterator<AttributeSetImpl> Elem = I++; |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 137 | delete &*Elem; |
| 138 | } |
| 139 | |
Bill Wendling | 164a4fb | 2013-01-24 00:14:46 +0000 | [diff] [blame] | 140 | // Destroy attribute node lists. |
| 141 | for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(), |
| 142 | E = AttrsSetNodes.end(); I != E; ) { |
| 143 | FoldingSetIterator<AttributeSetNode> Elem = I++; |
| 144 | delete &*Elem; |
| 145 | } |
| 146 | |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 147 | // Destroy MetadataAsValues. |
| 148 | { |
| 149 | SmallVector<MetadataAsValue *, 8> MDVs; |
| 150 | MDVs.reserve(MetadataAsValues.size()); |
| 151 | for (auto &Pair : MetadataAsValues) |
| 152 | MDVs.push_back(Pair.second); |
| 153 | MetadataAsValues.clear(); |
| 154 | for (auto *V : MDVs) |
| 155 | delete V; |
| 156 | } |
| 157 | |
| 158 | // Destroy ValuesAsMetadata. |
| 159 | for (auto &Pair : ValuesAsMetadata) |
| 160 | delete Pair.second; |
| 161 | |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 162 | // Destroy MDStrings. |
Duncan P. N. Exon Smith | f17e740 | 2014-11-14 01:17:09 +0000 | [diff] [blame] | 163 | MDStringCache.clear(); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 164 | } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 165 | |
Duncan P. N. Exon Smith | 93e983e | 2015-01-19 22:53:18 +0000 | [diff] [blame^] | 166 | namespace llvm { |
| 167 | /// \brief Make MDOperand transparent for hashing. |
| 168 | /// |
| 169 | /// This overload of an implementation detail of the hashing library makes |
| 170 | /// MDOperand hash to the same value as a \a Metadata pointer. |
| 171 | /// |
| 172 | /// Note that overloading \a hash_value() as follows: |
| 173 | /// |
| 174 | /// \code |
| 175 | /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); } |
| 176 | /// \endcode |
| 177 | /// |
| 178 | /// does not cause MDOperand to be transparent. In particular, a bare pointer |
| 179 | /// doesn't get hashed before it's combined, whereas \a MDOperand would. |
| 180 | static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); } |
| 181 | } |
| 182 | |
| 183 | unsigned MDNodeOpsKey::calculateHash(MDNode *N) { |
| 184 | unsigned Hash = hash_combine_range(N->op_begin(), N->op_end()); |
| 185 | #ifndef NDEBUG |
| 186 | { |
| 187 | SmallVector<Metadata *, 8> MDs(N->op_begin(), N->op_end()); |
| 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 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 200 | // ConstantsContext anchors |
| 201 | void UnaryConstantExpr::anchor() { } |
| 202 | |
| 203 | void BinaryConstantExpr::anchor() { } |
| 204 | |
| 205 | void SelectConstantExpr::anchor() { } |
| 206 | |
| 207 | void ExtractElementConstantExpr::anchor() { } |
| 208 | |
| 209 | void InsertElementConstantExpr::anchor() { } |
| 210 | |
| 211 | void ShuffleVectorConstantExpr::anchor() { } |
| 212 | |
| 213 | void ExtractValueConstantExpr::anchor() { } |
| 214 | |
| 215 | void InsertValueConstantExpr::anchor() { } |
| 216 | |
| 217 | void GetElementPtrConstantExpr::anchor() { } |
| 218 | |
| 219 | void CompareConstantExpr::anchor() { } |
Philip Reames | 2b45395 | 2015-01-16 20:07:33 +0000 | [diff] [blame] | 220 | |
| 221 | GCStrategy *LLVMContextImpl::getGCStrategy(const StringRef Name) { |
| 222 | // TODO: Arguably, just doing a linear search would be faster for small N |
| 223 | auto NMI = GCStrategyMap.find(Name); |
| 224 | if (NMI != GCStrategyMap.end()) |
| 225 | return NMI->getValue(); |
| 226 | |
| 227 | for (auto& Entry : GCRegistry::entries()) { |
| 228 | if (Name == Entry.getName()) { |
| 229 | std::unique_ptr<GCStrategy> S = Entry.instantiate(); |
| 230 | S->Name = Name; |
| 231 | GCStrategyMap[Name] = S.get(); |
| 232 | GCStrategyList.push_back(std::move(S)); |
| 233 | return GCStrategyList.back().get(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // No GCStrategy found for that name, error reporting is the job of our |
| 238 | // callers. |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
| 242 | |