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/PassSupport.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Regex.h"
24 /// Notify that we finished running a pass.
25 void LLVMContextImpl::notifyPassRun(LLVMContext *C, Pass *P, Module *M,
26 Function *F, BasicBlock *BB) {
27 for (auto const &L : RunListeners)
28 L->passRun(C, P, M, F, BB);
30 /// Register the given PassRunListener to receive notifyPassRun()
31 /// callbacks whenever a pass ran.
32 void LLVMContextImpl::addRunListener(PassRunListener *L) {
33 RunListeners.push_back(L);
35 /// Unregister a PassRunListener so that it no longer receives
36 /// notifyPassRun() callbacks.
37 void LLVMContextImpl::removeRunListener(PassRunListener *L) {
38 auto I = std::find(RunListeners.begin(), RunListeners.end(), L);
39 assert(I != RunListeners.end() && "RunListener not registered!");
41 RunListeners.erase(I);
44 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
45 : TheTrueVal(nullptr), TheFalseVal(nullptr),
46 VoidTy(C, Type::VoidTyID),
47 LabelTy(C, Type::LabelTyID),
48 HalfTy(C, Type::HalfTyID),
49 FloatTy(C, Type::FloatTyID),
50 DoubleTy(C, Type::DoubleTyID),
51 MetadataTy(C, Type::MetadataTyID),
52 X86_FP80Ty(C, Type::X86_FP80TyID),
53 FP128Ty(C, Type::FP128TyID),
54 PPC_FP128Ty(C, Type::PPC_FP128TyID),
55 X86_MMXTy(C, Type::X86_MMXTyID),
61 InlineAsmDiagHandler = nullptr;
62 InlineAsmDiagContext = nullptr;
63 DiagnosticHandler = nullptr;
64 DiagnosticContext = nullptr;
65 NamedStructTypesUniqueID = 0;
70 /// \brief Regular expression corresponding to the value given in the
71 /// command line flag -pass-remarks. Passes whose name matches this
72 /// regexp will emit a diagnostic when calling
73 /// LLVMContext::emitOptimizationRemark.
74 static Regex *OptimizationRemarkPattern = nullptr;
76 struct PassRemarksOpt {
77 void operator=(const std::string &Val) const {
78 // Create a regexp object to match pass names for emitOptimizationRemark.
80 delete OptimizationRemarkPattern;
81 OptimizationRemarkPattern = new Regex(Val);
82 std::string RegexError;
83 if (!OptimizationRemarkPattern->isValid(RegexError))
84 report_fatal_error("Invalid regular expression '" + Val +
85 "' in -pass-remarks: " + RegexError,
91 static PassRemarksOpt PassRemarksOptLoc;
94 // Command line flag to enable LLVMContext::emitOptimizationRemark()
95 // and LLVMContext::emitOptimizationNote() calls.
96 static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
97 PassRemarks("pass-remarks", cl::value_desc("pattern"),
98 cl::desc("Enable optimization remarks from passes whose name match "
99 "the given regular expression"),
100 cl::Hidden, cl::location(PassRemarksOptLoc), cl::ValueRequired,
105 LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const {
106 return OptimizationRemarkPattern &&
107 OptimizationRemarkPattern->match(PassName);
112 struct DropReferences {
113 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
115 template<typename PairT>
116 void operator()(const PairT &P) {
117 P.second->dropAllReferences();
121 // Temporary - drops pair.first instead of second.
123 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
125 template<typename PairT>
126 void operator()(const PairT &P) {
127 P.first->dropAllReferences();
132 LLVMContextImpl::~LLVMContextImpl() {
133 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
134 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
135 // the container. Avoid iterators during this operation:
136 while (!OwnedModules.empty())
137 delete *OwnedModules.begin();
139 // Free the constants. This is important to do here to ensure that they are
140 // freed before the LeakDetector is torn down.
141 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
143 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
145 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
147 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
149 ExprConstants.freeConstants();
150 ArrayConstants.freeConstants();
151 StructConstants.freeConstants();
152 VectorConstants.freeConstants();
153 DeleteContainerSeconds(CAZConstants);
154 DeleteContainerSeconds(CPNConstants);
155 DeleteContainerSeconds(UVConstants);
156 InlineAsms.freeConstants();
157 DeleteContainerSeconds(IntConstants);
158 DeleteContainerSeconds(FPConstants);
160 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
161 E = CDSConstants.end(); I != E; ++I)
163 CDSConstants.clear();
165 // Destroy attributes.
166 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
167 E = AttrsSet.end(); I != E; ) {
168 FoldingSetIterator<AttributeImpl> Elem = I++;
172 // Destroy attribute lists.
173 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
174 E = AttrsLists.end(); I != E; ) {
175 FoldingSetIterator<AttributeSetImpl> Elem = I++;
179 // Destroy attribute node lists.
180 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
181 E = AttrsSetNodes.end(); I != E; ) {
182 FoldingSetIterator<AttributeSetNode> Elem = I++;
186 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
187 // and the NonUniquedMDNodes sets, so copy the values out first.
188 SmallVector<MDNode*, 8> MDNodes;
189 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
190 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
192 MDNodes.push_back(&*I);
193 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
194 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
195 E = MDNodes.end(); I != E; ++I)
197 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
198 "Destroying all MDNodes didn't empty the Context's sets.");
200 // Destroy MDStrings.
201 DeleteContainerSeconds(MDStringCache);
203 // Destroy all run listeners.
204 for (auto &L : RunListeners)
206 RunListeners.clear();
209 // ConstantsContext anchors
210 void UnaryConstantExpr::anchor() { }
212 void BinaryConstantExpr::anchor() { }
214 void SelectConstantExpr::anchor() { }
216 void ExtractElementConstantExpr::anchor() { }
218 void InsertElementConstantExpr::anchor() { }
220 void ShuffleVectorConstantExpr::anchor() { }
222 void ExtractValueConstantExpr::anchor() { }
224 void InsertValueConstantExpr::anchor() { }
226 void GetElementPtrConstantExpr::anchor() { }
228 void CompareConstantExpr::anchor() { }