Jeffrey Yasskin | 2f1efd6 | 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 | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Attributes.h" |
| 17 | #include "llvm/IR/Module.h" |
Jeffrey Yasskin | c1dc067 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 20 | |
| 21 | LLVMContextImpl::LLVMContextImpl(LLVMContext &C) |
| 22 | : TheTrueVal(0), TheFalseVal(0), |
| 23 | VoidTy(C, Type::VoidTyID), |
| 24 | LabelTy(C, Type::LabelTyID), |
Dan Gohman | ce16339 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 25 | HalfTy(C, Type::HalfTyID), |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 26 | FloatTy(C, Type::FloatTyID), |
| 27 | DoubleTy(C, Type::DoubleTyID), |
| 28 | MetadataTy(C, Type::MetadataTyID), |
| 29 | X86_FP80Ty(C, Type::X86_FP80TyID), |
| 30 | FP128Ty(C, Type::FP128TyID), |
| 31 | PPC_FP128Ty(C, Type::PPC_FP128TyID), |
Dale Johannesen | bb811a2 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 32 | X86_MMXTy(C, Type::X86_MMXTyID), |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 33 | Int1Ty(C, 1), |
| 34 | Int8Ty(C, 8), |
| 35 | Int16Ty(C, 16), |
| 36 | Int32Ty(C, 32), |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 37 | Int64Ty(C, 64) { |
Bob Wilson | f64c889 | 2013-02-11 05:37:07 +0000 | [diff] [blame] | 38 | InlineAsmDiagHandler = 0; |
| 39 | InlineAsmDiagContext = 0; |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 40 | NamedStructTypesUniqueID = 0; |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Jeffrey Yasskin | c1dc067 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | struct DropReferences { |
| 45 | // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' |
| 46 | // is a Constant*. |
| 47 | template<typename PairT> |
| 48 | void operator()(const PairT &P) { |
| 49 | P.second->dropAllReferences(); |
| 50 | } |
| 51 | }; |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 52 | |
| 53 | // Temporary - drops pair.first instead of second. |
| 54 | struct DropFirst { |
| 55 | // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' |
| 56 | // is a Constant*. |
| 57 | template<typename PairT> |
| 58 | void operator()(const PairT &P) { |
| 59 | P.first->dropAllReferences(); |
| 60 | } |
| 61 | }; |
Jeffrey Yasskin | c1dc067 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 64 | LLVMContextImpl::~LLVMContextImpl() { |
Owen Anderson | 30268be | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 65 | // NOTE: We need to delete the contents of OwnedModules, but we have to |
| 66 | // duplicate it into a temporary vector, because the destructor of Module |
| 67 | // will try to remove itself from OwnedModules set. This would cause |
| 68 | // iterator invalidation if we iterated on the set directly. |
| 69 | std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end()); |
Nick Lewycky | 1852e21 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 70 | DeleteContainerPointers(Modules); |
Owen Anderson | 30268be | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 72 | // Free the constants. This is important to do here to ensure that they are |
| 73 | // freed before the LeakDetector is torn down. |
Jeffrey Yasskin | c1dc067 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 74 | std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), |
| 75 | DropReferences()); |
| 76 | std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 77 | DropFirst()); |
Jeffrey Yasskin | c1dc067 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 78 | std::for_each(StructConstants.map_begin(), StructConstants.map_end(), |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 79 | DropFirst()); |
Jeffrey Yasskin | c1dc067 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 80 | std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(), |
Talin | 2cb395e | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 81 | DropFirst()); |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 82 | ExprConstants.freeConstants(); |
| 83 | ArrayConstants.freeConstants(); |
| 84 | StructConstants.freeConstants(); |
| 85 | VectorConstants.freeConstants(); |
Chris Lattner | 9df0fb4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 86 | DeleteContainerSeconds(CAZConstants); |
| 87 | DeleteContainerSeconds(CPNConstants); |
| 88 | DeleteContainerSeconds(UVConstants); |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 89 | InlineAsms.freeConstants(); |
Nick Lewycky | 1852e21 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 90 | DeleteContainerSeconds(IntConstants); |
| 91 | DeleteContainerSeconds(FPConstants); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 27dd9cf | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 93 | for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(), |
| 94 | E = CDSConstants.end(); I != E; ++I) |
| 95 | delete I->second; |
| 96 | CDSConstants.clear(); |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 97 | |
| 98 | // Destroy attributes. |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 99 | for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(), |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 100 | E = AttrsSet.end(); I != E; ) { |
Bill Wendling | f667072 | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 101 | FoldingSetIterator<AttributeImpl> Elem = I++; |
Benjamin Kramer | fd8d62c | 2012-10-14 08:48:40 +0000 | [diff] [blame] | 102 | delete &*Elem; |
| 103 | } |
| 104 | |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 105 | // Destroy attribute lists. |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 106 | for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(), |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 107 | E = AttrsLists.end(); I != E; ) { |
Bill Wendling | 18e7211 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 108 | FoldingSetIterator<AttributeSetImpl> Elem = I++; |
Bill Wendling | 0976e00 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 109 | delete &*Elem; |
| 110 | } |
| 111 | |
Bill Wendling | 8b1f2f3 | 2013-01-24 00:14:46 +0000 | [diff] [blame] | 112 | // Destroy attribute node lists. |
| 113 | for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(), |
| 114 | E = AttrsSetNodes.end(); I != E; ) { |
| 115 | FoldingSetIterator<AttributeSetNode> Elem = I++; |
| 116 | delete &*Elem; |
| 117 | } |
| 118 | |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 119 | // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet |
| 120 | // and the NonUniquedMDNodes sets, so copy the values out first. |
| 121 | SmallVector<MDNode*, 8> MDNodes; |
| 122 | MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size()); |
| 123 | for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end(); |
Chris Lattner | c09ef37 | 2011-07-13 04:22:39 +0000 | [diff] [blame] | 124 | I != E; ++I) |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 125 | MDNodes.push_back(&*I); |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 126 | MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end()); |
Dan Gohman | 9b0e47e | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 127 | for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(), |
Chris Lattner | c09ef37 | 2011-07-13 04:22:39 +0000 | [diff] [blame] | 128 | E = MDNodes.end(); I != E; ++I) |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 129 | (*I)->destroy(); |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 130 | assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() && |
| 131 | "Destroying all MDNodes didn't empty the Context's sets."); |
Bill Wendling | 2c79ecb | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 132 | |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 133 | // Destroy MDStrings. |
Nick Lewycky | 1852e21 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 134 | DeleteContainerSeconds(MDStringCache); |
Jeffrey Yasskin | 2f1efd6 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 135 | } |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 136 | |
| 137 | // ConstantsContext anchors |
| 138 | void UnaryConstantExpr::anchor() { } |
| 139 | |
| 140 | void BinaryConstantExpr::anchor() { } |
| 141 | |
| 142 | void SelectConstantExpr::anchor() { } |
| 143 | |
| 144 | void ExtractElementConstantExpr::anchor() { } |
| 145 | |
| 146 | void InsertElementConstantExpr::anchor() { } |
| 147 | |
| 148 | void ShuffleVectorConstantExpr::anchor() { } |
| 149 | |
| 150 | void ExtractValueConstantExpr::anchor() { } |
| 151 | |
| 152 | void InsertValueConstantExpr::anchor() { } |
| 153 | |
| 154 | void GetElementPtrConstantExpr::anchor() { } |
| 155 | |
| 156 | void CompareConstantExpr::anchor() { } |