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 Module's dtor 123 // will call LLVMContextImpl::removeModule, thus invalidating iterators into 124 // the container. Avoid iterators during this operation: 125 while (!OwnedModules.empty()) 126 delete *OwnedModules.begin(); 127 128 // Free the constants. This is important to do here to ensure that they are 129 // freed before the LeakDetector is torn down. 130 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), 131 DropReferences()); 132 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), 133 DropFirst()); 134 std::for_each(StructConstants.map_begin(), StructConstants.map_end(), 135 DropFirst()); 136 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(), 137 DropFirst()); 138 ExprConstants.freeConstants(); 139 ArrayConstants.freeConstants(); 140 StructConstants.freeConstants(); 141 VectorConstants.freeConstants(); 142 DeleteContainerSeconds(CAZConstants); 143 DeleteContainerSeconds(CPNConstants); 144 DeleteContainerSeconds(UVConstants); 145 InlineAsms.freeConstants(); 146 DeleteContainerSeconds(IntConstants); 147 DeleteContainerSeconds(FPConstants); 148 149 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(), 150 E = CDSConstants.end(); I != E; ++I) 151 delete I->second; 152 CDSConstants.clear(); 153 154 // Destroy attributes. 155 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(), 156 E = AttrsSet.end(); I != E; ) { 157 FoldingSetIterator<AttributeImpl> Elem = I++; 158 delete &*Elem; 159 } 160 161 // Destroy attribute lists. 162 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(), 163 E = AttrsLists.end(); I != E; ) { 164 FoldingSetIterator<AttributeSetImpl> Elem = I++; 165 delete &*Elem; 166 } 167 168 // Destroy attribute node lists. 169 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(), 170 E = AttrsSetNodes.end(); I != E; ) { 171 FoldingSetIterator<AttributeSetNode> Elem = I++; 172 delete &*Elem; 173 } 174 175 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet 176 // and the NonUniquedMDNodes sets, so copy the values out first. 177 SmallVector<MDNode*, 8> MDNodes; 178 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size()); 179 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end(); 180 I != E; ++I) 181 MDNodes.push_back(&*I); 182 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end()); 183 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(), 184 E = MDNodes.end(); I != E; ++I) 185 (*I)->destroy(); 186 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() && 187 "Destroying all MDNodes didn't empty the Context's sets."); 188 189 // Destroy MDStrings. 190 DeleteContainerSeconds(MDStringCache); 191 } 192 193 // ConstantsContext anchors 194 void UnaryConstantExpr::anchor() { } 195 196 void BinaryConstantExpr::anchor() { } 197 198 void SelectConstantExpr::anchor() { } 199 200 void ExtractElementConstantExpr::anchor() { } 201 202 void InsertElementConstantExpr::anchor() { } 203 204 void ShuffleVectorConstantExpr::anchor() { } 205 206 void ExtractValueConstantExpr::anchor() { } 207 208 void InsertValueConstantExpr::anchor() { } 209 210 void GetElementPtrConstantExpr::anchor() { } 211 212 void CompareConstantExpr::anchor() { } 213