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/Module.h" 18 #include "llvm/Support/CommandLine.h" 19 #include "llvm/Support/Regex.h" 20 #include <algorithm> 21 using namespace llvm; 22 23 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) 24 : TheTrueVal(nullptr), TheFalseVal(nullptr), 25 VoidTy(C, Type::VoidTyID), 26 LabelTy(C, Type::LabelTyID), 27 HalfTy(C, Type::HalfTyID), 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), 34 X86_MMXTy(C, Type::X86_MMXTyID), 35 Int1Ty(C, 1), 36 Int8Ty(C, 8), 37 Int16Ty(C, 16), 38 Int32Ty(C, 32), 39 Int64Ty(C, 64) { 40 InlineAsmDiagHandler = nullptr; 41 InlineAsmDiagContext = nullptr; 42 DiagnosticHandler = nullptr; 43 DiagnosticContext = nullptr; 44 NamedStructTypesUniqueID = 0; 45 } 46 47 namespace { 48 49 /// \brief Regular expression corresponding to the value given in the 50 /// command line flag -pass-remarks. Passes whose name matches this 51 /// regexp will emit a diagnostic when calling 52 /// LLVMContext::emitOptimizationRemark. 53 static Regex *OptimizationRemarkPattern = nullptr; 54 55 /// \brief String to hold all the values passed via -pass-remarks. Every 56 /// instance of -pass-remarks on the command line will be concatenated 57 /// to this string. Values are stored inside braces and concatenated with 58 /// the '|' operator. This implements the expected semantics that multiple 59 /// -pass-remarks are additive. 60 static std::string OptimizationRemarkExpr; 61 62 struct PassRemarksOpt { 63 void operator=(const std::string &Val) const { 64 // Create a regexp object to match pass names for emitOptimizationRemark. 65 if (!Val.empty()) { 66 if (!OptimizationRemarkExpr.empty()) 67 OptimizationRemarkExpr += "|"; 68 OptimizationRemarkExpr += "(" + Val + ")"; 69 delete OptimizationRemarkPattern; 70 OptimizationRemarkPattern = new Regex(OptimizationRemarkExpr); 71 std::string RegexError; 72 if (!OptimizationRemarkPattern->isValid(RegexError)) 73 report_fatal_error("Invalid regular expression '" + Val + 74 "' in -pass-remarks: " + RegexError, 75 false); 76 } 77 }; 78 }; 79 80 static PassRemarksOpt PassRemarksOptLoc; 81 82 // -pass-remarks 83 // Command line flag to enable LLVMContext::emitOptimizationRemark() 84 // and LLVMContext::emitOptimizationNote() calls. 85 static cl::opt<PassRemarksOpt, true, cl::parser<std::string>> 86 PassRemarks("pass-remarks", cl::value_desc("pattern"), 87 cl::desc("Enable optimization remarks from passes whose name match " 88 "the given regular expression"), 89 cl::Hidden, cl::location(PassRemarksOptLoc), cl::ValueRequired, 90 cl::ZeroOrMore); 91 } 92 93 bool 94 LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const { 95 return OptimizationRemarkPattern && 96 OptimizationRemarkPattern->match(PassName); 97 } 98 99 100 namespace { 101 struct DropReferences { 102 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' 103 // is a Constant*. 104 template<typename PairT> 105 void operator()(const PairT &P) { 106 P.second->dropAllReferences(); 107 } 108 }; 109 110 // Temporary - drops pair.first instead of second. 111 struct DropFirst { 112 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second' 113 // is a Constant*. 114 template<typename PairT> 115 void operator()(const PairT &P) { 116 P.first->dropAllReferences(); 117 } 118 }; 119 } 120 121 LLVMContextImpl::~LLVMContextImpl() { 122 // NOTE: We need to delete the contents of OwnedModules, but we have to 123 // duplicate it into a temporary vector, because the destructor of Module 124 // will try to remove itself from OwnedModules set. This would cause 125 // iterator invalidation if we iterated on the set directly. 126 std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end()); 127 DeleteContainerPointers(Modules); 128 129 // Free the constants. This is important to do here to ensure that they are 130 // freed before the LeakDetector is torn down. 131 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), 132 DropReferences()); 133 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), 134 DropFirst()); 135 std::for_each(StructConstants.map_begin(), StructConstants.map_end(), 136 DropFirst()); 137 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(), 138 DropFirst()); 139 ExprConstants.freeConstants(); 140 ArrayConstants.freeConstants(); 141 StructConstants.freeConstants(); 142 VectorConstants.freeConstants(); 143 DeleteContainerSeconds(CAZConstants); 144 DeleteContainerSeconds(CPNConstants); 145 DeleteContainerSeconds(UVConstants); 146 InlineAsms.freeConstants(); 147 DeleteContainerSeconds(IntConstants); 148 DeleteContainerSeconds(FPConstants); 149 150 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(), 151 E = CDSConstants.end(); I != E; ++I) 152 delete I->second; 153 CDSConstants.clear(); 154 155 // Destroy attributes. 156 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(), 157 E = AttrsSet.end(); I != E; ) { 158 FoldingSetIterator<AttributeImpl> Elem = I++; 159 delete &*Elem; 160 } 161 162 // Destroy attribute lists. 163 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(), 164 E = AttrsLists.end(); I != E; ) { 165 FoldingSetIterator<AttributeSetImpl> Elem = I++; 166 delete &*Elem; 167 } 168 169 // Destroy attribute node lists. 170 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(), 171 E = AttrsSetNodes.end(); I != E; ) { 172 FoldingSetIterator<AttributeSetNode> Elem = I++; 173 delete &*Elem; 174 } 175 176 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet 177 // and the NonUniquedMDNodes sets, so copy the values out first. 178 SmallVector<MDNode*, 8> MDNodes; 179 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size()); 180 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end(); 181 I != E; ++I) 182 MDNodes.push_back(&*I); 183 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end()); 184 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(), 185 E = MDNodes.end(); I != E; ++I) 186 (*I)->destroy(); 187 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() && 188 "Destroying all MDNodes didn't empty the Context's sets."); 189 190 // Destroy MDStrings. 191 DeleteContainerSeconds(MDStringCache); 192 } 193 194 // ConstantsContext anchors 195 void UnaryConstantExpr::anchor() { } 196 197 void BinaryConstantExpr::anchor() { } 198 199 void SelectConstantExpr::anchor() { } 200 201 void ExtractElementConstantExpr::anchor() { } 202 203 void InsertElementConstantExpr::anchor() { } 204 205 void ShuffleVectorConstantExpr::anchor() { } 206 207 void ExtractValueConstantExpr::anchor() { } 208 209 void InsertValueConstantExpr::anchor() { } 210 211 void GetElementPtrConstantExpr::anchor() { } 212 213 void CompareConstantExpr::anchor() { } 214