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 struct PassRemarksOpt {
56 void operator=(const std::string &Val) const {
57 // Create a regexp object to match pass names for emitOptimizationRemark.
59 delete OptimizationRemarkPattern;
60 OptimizationRemarkPattern = new Regex(Val);
61 std::string RegexError;
62 if (!OptimizationRemarkPattern->isValid(RegexError))
63 report_fatal_error("Invalid regular expression '" + Val +
64 "' in -pass-remarks: " + RegexError,
70 static PassRemarksOpt PassRemarksOptLoc;
73 // Command line flag to enable LLVMContext::emitOptimizationRemark()
74 // and LLVMContext::emitOptimizationNote() calls.
75 static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
76 PassRemarks("pass-remarks", cl::value_desc("pattern"),
77 cl::desc("Enable optimization remarks from passes whose name match "
78 "the given regular expression"),
79 cl::Hidden, cl::location(PassRemarksOptLoc), cl::ValueRequired,
84 LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const {
85 return OptimizationRemarkPattern &&
86 OptimizationRemarkPattern->match(PassName);
91 struct DropReferences {
92 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
94 template<typename PairT>
95 void operator()(const PairT &P) {
96 P.second->dropAllReferences();
100 // Temporary - drops pair.first instead of second.
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.first->dropAllReferences();
111 LLVMContextImpl::~LLVMContextImpl() {
112 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
113 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
114 // the container. Avoid iterators during this operation:
115 while (!OwnedModules.empty())
116 delete *OwnedModules.begin();
118 // Free the constants. This is important to do here to ensure that they are
119 // freed before the LeakDetector is torn down.
120 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
122 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
124 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
126 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
128 ExprConstants.freeConstants();
129 ArrayConstants.freeConstants();
130 StructConstants.freeConstants();
131 VectorConstants.freeConstants();
132 DeleteContainerSeconds(CAZConstants);
133 DeleteContainerSeconds(CPNConstants);
134 DeleteContainerSeconds(UVConstants);
135 InlineAsms.freeConstants();
136 DeleteContainerSeconds(IntConstants);
137 DeleteContainerSeconds(FPConstants);
139 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
140 E = CDSConstants.end(); I != E; ++I)
142 CDSConstants.clear();
144 // Destroy attributes.
145 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
146 E = AttrsSet.end(); I != E; ) {
147 FoldingSetIterator<AttributeImpl> Elem = I++;
151 // Destroy attribute lists.
152 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
153 E = AttrsLists.end(); I != E; ) {
154 FoldingSetIterator<AttributeSetImpl> Elem = I++;
158 // Destroy attribute node lists.
159 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
160 E = AttrsSetNodes.end(); I != E; ) {
161 FoldingSetIterator<AttributeSetNode> Elem = I++;
165 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
166 // and the NonUniquedMDNodes sets, so copy the values out first.
167 SmallVector<MDNode*, 8> MDNodes;
168 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
169 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
171 MDNodes.push_back(&*I);
172 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
173 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
174 E = MDNodes.end(); I != E; ++I)
176 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
177 "Destroying all MDNodes didn't empty the Context's sets.");
179 // Destroy MDStrings.
180 DeleteContainerSeconds(MDStringCache);
183 // ConstantsContext anchors
184 void UnaryConstantExpr::anchor() { }
186 void BinaryConstantExpr::anchor() { }
188 void SelectConstantExpr::anchor() { }
190 void ExtractElementConstantExpr::anchor() { }
192 void InsertElementConstantExpr::anchor() { }
194 void ShuffleVectorConstantExpr::anchor() { }
196 void ExtractValueConstantExpr::anchor() { }
198 void InsertValueConstantExpr::anchor() { }
200 void GetElementPtrConstantExpr::anchor() { }
202 void CompareConstantExpr::anchor() { }