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