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 /// \brief String to hold all the values passed via -pass-remarks. Every
77 /// instance of -pass-remarks on the command line will be concatenated
78 /// to this string. Values are stored inside braces and concatenated with
79 /// the '|' operator. This implements the expected semantics that multiple
80 /// -pass-remarks are additive.
81 static std::string OptimizationRemarkExpr;
83 struct PassRemarksOpt {
84 void operator=(const std::string &Val) const {
85 // Create a regexp object to match pass names for emitOptimizationRemark.
87 if (!OptimizationRemarkExpr.empty())
88 OptimizationRemarkExpr += "|";
89 OptimizationRemarkExpr += "(" + Val + ")";
90 delete OptimizationRemarkPattern;
91 OptimizationRemarkPattern = new Regex(OptimizationRemarkExpr);
92 std::string RegexError;
93 if (!OptimizationRemarkPattern->isValid(RegexError))
94 report_fatal_error("Invalid regular expression '" + Val +
95 "' in -pass-remarks: " + RegexError,
101 static PassRemarksOpt PassRemarksOptLoc;
104 // Command line flag to enable LLVMContext::emitOptimizationRemark()
105 // and LLVMContext::emitOptimizationNote() calls.
106 static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
107 PassRemarks("pass-remarks", cl::value_desc("pattern"),
108 cl::desc("Enable optimization remarks from passes whose name match "
109 "the given regular expression"),
110 cl::Hidden, cl::location(PassRemarksOptLoc), cl::ValueRequired,
115 LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const {
116 return OptimizationRemarkPattern &&
117 OptimizationRemarkPattern->match(PassName);
122 struct DropReferences {
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.second->dropAllReferences();
131 // Temporary - drops pair.first instead of second.
133 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
135 template<typename PairT>
136 void operator()(const PairT &P) {
137 P.first->dropAllReferences();
142 LLVMContextImpl::~LLVMContextImpl() {
143 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
144 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
145 // the container. Avoid iterators during this operation:
146 while (!OwnedModules.empty())
147 delete *OwnedModules.begin();
149 // Free the constants. This is important to do here to ensure that they are
150 // freed before the LeakDetector is torn down.
151 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
153 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
155 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
157 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
159 ExprConstants.freeConstants();
160 ArrayConstants.freeConstants();
161 StructConstants.freeConstants();
162 VectorConstants.freeConstants();
163 DeleteContainerSeconds(CAZConstants);
164 DeleteContainerSeconds(CPNConstants);
165 DeleteContainerSeconds(UVConstants);
166 InlineAsms.freeConstants();
167 DeleteContainerSeconds(IntConstants);
168 DeleteContainerSeconds(FPConstants);
170 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
171 E = CDSConstants.end(); I != E; ++I)
173 CDSConstants.clear();
175 // Destroy attributes.
176 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
177 E = AttrsSet.end(); I != E; ) {
178 FoldingSetIterator<AttributeImpl> Elem = I++;
182 // Destroy attribute lists.
183 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
184 E = AttrsLists.end(); I != E; ) {
185 FoldingSetIterator<AttributeSetImpl> Elem = I++;
189 // Destroy attribute node lists.
190 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
191 E = AttrsSetNodes.end(); I != E; ) {
192 FoldingSetIterator<AttributeSetNode> Elem = I++;
196 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
197 // and the NonUniquedMDNodes sets, so copy the values out first.
198 SmallVector<MDNode*, 8> MDNodes;
199 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
200 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
202 MDNodes.push_back(&*I);
203 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
204 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
205 E = MDNodes.end(); I != E; ++I)
207 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
208 "Destroying all MDNodes didn't empty the Context's sets.");
210 // Destroy MDStrings.
211 DeleteContainerSeconds(MDStringCache);
213 // Destroy all run listeners.
214 for (auto &L : RunListeners)
216 RunListeners.clear();
219 // ConstantsContext anchors
220 void UnaryConstantExpr::anchor() { }
222 void BinaryConstantExpr::anchor() { }
224 void SelectConstantExpr::anchor() { }
226 void ExtractElementConstantExpr::anchor() { }
228 void InsertElementConstantExpr::anchor() { }
230 void ShuffleVectorConstantExpr::anchor() { }
232 void ExtractValueConstantExpr::anchor() { }
234 void InsertValueConstantExpr::anchor() { }
236 void GetElementPtrConstantExpr::anchor() { }
238 void CompareConstantExpr::anchor() { }