1 //===-- LLVMContextImpl.cpp - Implement LLVMContextImpl -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the opaque LLVMContextImpl.
12 //===----------------------------------------------------------------------===//
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"
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),
40 InlineAsmDiagHandler = nullptr;
41 InlineAsmDiagContext = nullptr;
42 DiagnosticHandler = nullptr;
43 DiagnosticContext = nullptr;
44 NamedStructTypesUniqueID = 0;
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;
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;
62 struct PassRemarksOpt {
63 void operator=(const std::string &Val) const {
64 // Create a regexp object to match pass names for emitOptimizationRemark.
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,
80 static PassRemarksOpt PassRemarksOptLoc;
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,
94 LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const {
95 return OptimizationRemarkPattern &&
96 OptimizationRemarkPattern->match(PassName);
101 struct DropReferences {
102 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
104 template<typename PairT>
105 void operator()(const PairT &P) {
106 P.second->dropAllReferences();
110 // Temporary - drops pair.first instead of second.
112 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
114 template<typename PairT>
115 void operator()(const PairT &P) {
116 P.first->dropAllReferences();
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();
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(),
132 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
134 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
136 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
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);
149 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
150 E = CDSConstants.end(); I != E; ++I)
152 CDSConstants.clear();
154 // Destroy attributes.
155 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
156 E = AttrsSet.end(); I != E; ) {
157 FoldingSetIterator<AttributeImpl> Elem = I++;
161 // Destroy attribute lists.
162 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
163 E = AttrsLists.end(); I != E; ) {
164 FoldingSetIterator<AttributeSetImpl> Elem = I++;
168 // Destroy attribute node lists.
169 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
170 E = AttrsSetNodes.end(); I != E; ) {
171 FoldingSetIterator<AttributeSetNode> Elem = I++;
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();
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)
186 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
187 "Destroying all MDNodes didn't empty the Context's sets.");
189 // Destroy MDStrings.
190 DeleteContainerSeconds(MDStringCache);
193 // ConstantsContext anchors
194 void UnaryConstantExpr::anchor() { }
196 void BinaryConstantExpr::anchor() { }
198 void SelectConstantExpr::anchor() { }
200 void ExtractElementConstantExpr::anchor() { }
202 void InsertElementConstantExpr::anchor() { }
204 void ShuffleVectorConstantExpr::anchor() { }
206 void ExtractValueConstantExpr::anchor() { }
208 void InsertValueConstantExpr::anchor() { }
210 void GetElementPtrConstantExpr::anchor() { }
212 void CompareConstantExpr::anchor() { }