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" |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
Nick Lewycky | e9bb9a0 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 17 | #include <algorithm> |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 18 | using namespace llvm; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 19 | |
| 20 | LLVMContextImpl::LLVMContextImpl(LLVMContext &C) |
| 21 | : TheTrueVal(0), TheFalseVal(0), |
| 22 | VoidTy(C, Type::VoidTyID), |
| 23 | LabelTy(C, Type::LabelTyID), |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 24 | HalfTy(C, Type::HalfTyID), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 25 | FloatTy(C, Type::FloatTyID), |
| 26 | DoubleTy(C, Type::DoubleTyID), |
| 27 | MetadataTy(C, Type::MetadataTyID), |
| 28 | X86_FP80Ty(C, Type::X86_FP80TyID), |
| 29 | FP128Ty(C, Type::FP128TyID), |
| 30 | PPC_FP128Ty(C, Type::PPC_FP128TyID), |
Dale Johannesen | baa5d04 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 31 | X86_MMXTy(C, Type::X86_MMXTyID), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 32 | Int1Ty(C, 1), |
| 33 | Int8Ty(C, 8), |
| 34 | Int16Ty(C, 16), |
| 35 | Int32Ty(C, 32), |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 36 | Int64Ty(C, 64) { |
Chris Lattner | 60955d4 | 2010-04-06 00:44:45 +0000 | [diff] [blame] | 37 | InlineAsmDiagHandler = 0; |
| 38 | InlineAsmDiagContext = 0; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 39 | NamedStructTypesUniqueID = 0; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 42 | namespace { |
| 43 | struct DropReferences { |
| 44 | // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' |
| 45 | // is a Constant*. |
| 46 | template<typename PairT> |
| 47 | void operator()(const PairT &P) { |
| 48 | P.second->dropAllReferences(); |
| 49 | } |
| 50 | }; |
| 51 | } |
| 52 | |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 53 | LLVMContextImpl::~LLVMContextImpl() { |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 54 | // NOTE: We need to delete the contents of OwnedModules, but we have to |
| 55 | // duplicate it into a temporary vector, because the destructor of Module |
| 56 | // will try to remove itself from OwnedModules set. This would cause |
| 57 | // iterator invalidation if we iterated on the set directly. |
| 58 | std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end()); |
Nick Lewycky | e9bb9a0 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 59 | DeleteContainerPointers(Modules); |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 60 | |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame^] | 61 | // Free the constants. This is important to do here to ensure that they are |
| 62 | // freed before the LeakDetector is torn down. |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 63 | std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), |
| 64 | DropReferences()); |
| 65 | std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), |
| 66 | DropReferences()); |
| 67 | std::for_each(StructConstants.map_begin(), StructConstants.map_end(), |
| 68 | DropReferences()); |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 69 | std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(), |
| 70 | DropReferences()); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 71 | ExprConstants.freeConstants(); |
| 72 | ArrayConstants.freeConstants(); |
| 73 | StructConstants.freeConstants(); |
| 74 | VectorConstants.freeConstants(); |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame^] | 75 | DeleteContainerSeconds(CAZConstants); |
| 76 | DeleteContainerSeconds(CPNConstants); |
| 77 | DeleteContainerSeconds(UVConstants); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 78 | InlineAsms.freeConstants(); |
Nick Lewycky | e9bb9a0 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 79 | DeleteContainerSeconds(IntConstants); |
| 80 | DeleteContainerSeconds(FPConstants); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 81 | |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 82 | // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet |
| 83 | // and the NonUniquedMDNodes sets, so copy the values out first. |
| 84 | SmallVector<MDNode*, 8> MDNodes; |
| 85 | MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size()); |
| 86 | for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end(); |
Chris Lattner | 68ef694 | 2011-07-13 04:22:39 +0000 | [diff] [blame] | 87 | I != E; ++I) |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 88 | MDNodes.push_back(&*I); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 89 | MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end()); |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 90 | for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(), |
Chris Lattner | 68ef694 | 2011-07-13 04:22:39 +0000 | [diff] [blame] | 91 | E = MDNodes.end(); I != E; ++I) |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 92 | (*I)->destroy(); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 93 | assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() && |
| 94 | "Destroying all MDNodes didn't empty the Context's sets."); |
| 95 | // Destroy MDStrings. |
Nick Lewycky | e9bb9a0 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 96 | DeleteContainerSeconds(MDStringCache); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 97 | } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 98 | |
| 99 | // ConstantsContext anchors |
| 100 | void UnaryConstantExpr::anchor() { } |
| 101 | |
| 102 | void BinaryConstantExpr::anchor() { } |
| 103 | |
| 104 | void SelectConstantExpr::anchor() { } |
| 105 | |
| 106 | void ExtractElementConstantExpr::anchor() { } |
| 107 | |
| 108 | void InsertElementConstantExpr::anchor() { } |
| 109 | |
| 110 | void ShuffleVectorConstantExpr::anchor() { } |
| 111 | |
| 112 | void ExtractValueConstantExpr::anchor() { } |
| 113 | |
| 114 | void InsertValueConstantExpr::anchor() { } |
| 115 | |
| 116 | void GetElementPtrConstantExpr::anchor() { } |
| 117 | |
| 118 | void CompareConstantExpr::anchor() { } |