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" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/IR/Attributes.h" 17 #include "llvm/IR/DiagnosticInfo.h" 18 #include "llvm/IR/Module.h" 19 #include <algorithm> 20 using namespace llvm; 21 22 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) 23 : TheTrueVal(nullptr), TheFalseVal(nullptr), 24 VoidTy(C, Type::VoidTyID), 25 LabelTy(C, Type::LabelTyID), 26 HalfTy(C, Type::HalfTyID), 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), 33 X86_MMXTy(C, Type::X86_MMXTyID), 34 Int1Ty(C, 1), 35 Int8Ty(C, 8), 36 Int16Ty(C, 16), 37 Int32Ty(C, 32), 38 Int64Ty(C, 64) { 39 InlineAsmDiagHandler = nullptr; 40 InlineAsmDiagContext = nullptr; 41 DiagnosticHandler = nullptr; 42 DiagnosticContext = nullptr; 43 YieldCallback = nullptr; 44 YieldOpaqueHandle = nullptr; 45 NamedStructTypesUniqueID = 0; 46 } 47 48 namespace { 49 struct DropReferences { 50 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' 51 // is a Constant*. 52 template <typename PairT> void operator()(const PairT &P) { 53 P.second->dropAllReferences(); 54 } 55 }; 56 57 // Temporary - drops pair.first instead of second. 58 struct DropFirst { 59 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' 60 // is a Constant*. 61 template<typename PairT> 62 void operator()(const PairT &P) { 63 P.first->dropAllReferences(); 64 } 65 }; 66 } 67 68 LLVMContextImpl::~LLVMContextImpl() { 69 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor 70 // will call LLVMContextImpl::removeModule, thus invalidating iterators into 71 // the container. Avoid iterators during this operation: 72 while (!OwnedModules.empty()) 73 delete *OwnedModules.begin(); 74 75 // Free the constants. This is important to do here to ensure that they are 76 // freed before the LeakDetector is torn down. 77 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), 78 DropReferences()); 79 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), 80 DropFirst()); 81 std::for_each(StructConstants.map_begin(), StructConstants.map_end(), 82 DropFirst()); 83 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(), 84 DropFirst()); 85 ExprConstants.freeConstants(); 86 ArrayConstants.freeConstants(); 87 StructConstants.freeConstants(); 88 VectorConstants.freeConstants(); 89 DeleteContainerSeconds(CAZConstants); 90 DeleteContainerSeconds(CPNConstants); 91 DeleteContainerSeconds(UVConstants); 92 InlineAsms.freeConstants(); 93 DeleteContainerSeconds(IntConstants); 94 DeleteContainerSeconds(FPConstants); 95 96 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(), 97 E = CDSConstants.end(); I != E; ++I) 98 delete I->second; 99 CDSConstants.clear(); 100 101 // Destroy attributes. 102 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(), 103 E = AttrsSet.end(); I != E; ) { 104 FoldingSetIterator<AttributeImpl> Elem = I++; 105 delete &*Elem; 106 } 107 108 // Destroy attribute lists. 109 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(), 110 E = AttrsLists.end(); I != E; ) { 111 FoldingSetIterator<AttributeSetImpl> Elem = I++; 112 delete &*Elem; 113 } 114 115 // Destroy attribute node lists. 116 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(), 117 E = AttrsSetNodes.end(); I != E; ) { 118 FoldingSetIterator<AttributeSetNode> Elem = I++; 119 delete &*Elem; 120 } 121 122 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet 123 // and the NonUniquedMDNodes sets, so copy the values out first. 124 SmallVector<MDNode*, 8> MDNodes; 125 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size()); 126 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end(); 127 I != E; ++I) 128 MDNodes.push_back(&*I); 129 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end()); 130 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(), 131 E = MDNodes.end(); I != E; ++I) 132 (*I)->destroy(); 133 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() && 134 "Destroying all MDNodes didn't empty the Context's sets."); 135 136 // Destroy MDStrings. 137 DeleteContainerSeconds(MDStringCache); 138 } 139 140 // ConstantsContext anchors 141 void UnaryConstantExpr::anchor() { } 142 143 void BinaryConstantExpr::anchor() { } 144 145 void SelectConstantExpr::anchor() { } 146 147 void ExtractElementConstantExpr::anchor() { } 148 149 void InsertElementConstantExpr::anchor() { } 150 151 void ShuffleVectorConstantExpr::anchor() { } 152 153 void ExtractValueConstantExpr::anchor() { } 154 155 void InsertValueConstantExpr::anchor() { } 156 157 void GetElementPtrConstantExpr::anchor() { } 158 159 void CompareConstantExpr::anchor() { } 160