50740a0c348265bc78e44f7e386e7ee828849ed6
[oota-llvm.git] / lib / IR / LLVMContextImpl.cpp
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/PassSupport.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "llvm/Support/Regex.h"
21 #include <algorithm>
22 using namespace llvm;
23
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);
29 }
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);
34 }
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!");
40   delete *I;
41   RunListeners.erase(I);
42 }
43
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),
56     Int1Ty(C, 1),
57     Int8Ty(C, 8),
58     Int16Ty(C, 16),
59     Int32Ty(C, 32),
60     Int64Ty(C, 64) {
61   InlineAsmDiagHandler = nullptr;
62   InlineAsmDiagContext = nullptr;
63   DiagnosticHandler = nullptr;
64   DiagnosticContext = nullptr;
65   NamedStructTypesUniqueID = 0;
66 }
67
68 namespace {
69
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;
75
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;
82
83 struct PassRemarksOpt {
84   void operator=(const std::string &Val) const {
85     // Create a regexp object to match pass names for emitOptimizationRemark.
86     if (!Val.empty()) {
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,
96                            false);
97     }
98   };
99 };
100
101 static PassRemarksOpt PassRemarksOptLoc;
102
103 // -pass-remarks
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,
111             cl::ZeroOrMore);
112 }
113
114 bool
115 LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const {
116   return OptimizationRemarkPattern &&
117          OptimizationRemarkPattern->match(PassName);
118 }
119
120
121 namespace {
122 struct DropReferences {
123   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
124   // is a Constant*.
125   template<typename PairT>
126   void operator()(const PairT &P) {
127     P.second->dropAllReferences();
128   }
129 };
130
131 // Temporary - drops pair.first instead of second.
132 struct DropFirst {
133   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
134   // is a Constant*.
135   template<typename PairT>
136   void operator()(const PairT &P) {
137     P.first->dropAllReferences();
138   }
139 };
140 }
141
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();
148   
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(),
152                 DropReferences());
153   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
154                 DropFirst());
155   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
156                 DropFirst());
157   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
158                 DropFirst());
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);
169   
170   for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
171        E = CDSConstants.end(); I != E; ++I)
172     delete I->second;
173   CDSConstants.clear();
174
175   // Destroy attributes.
176   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
177          E = AttrsSet.end(); I != E; ) {
178     FoldingSetIterator<AttributeImpl> Elem = I++;
179     delete &*Elem;
180   }
181
182   // Destroy attribute lists.
183   for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
184          E = AttrsLists.end(); I != E; ) {
185     FoldingSetIterator<AttributeSetImpl> Elem = I++;
186     delete &*Elem;
187   }
188
189   // Destroy attribute node lists.
190   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
191          E = AttrsSetNodes.end(); I != E; ) {
192     FoldingSetIterator<AttributeSetNode> Elem = I++;
193     delete &*Elem;
194   }
195
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();
201        I != E; ++I)
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)
206     (*I)->destroy();
207   assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
208          "Destroying all MDNodes didn't empty the Context's sets.");
209
210   // Destroy MDStrings.
211   DeleteContainerSeconds(MDStringCache);
212
213   // Destroy all run listeners.
214   for (auto &L : RunListeners)
215     delete L;
216   RunListeners.clear();
217 }
218
219 // ConstantsContext anchors
220 void UnaryConstantExpr::anchor() { }
221
222 void BinaryConstantExpr::anchor() { }
223
224 void SelectConstantExpr::anchor() { }
225
226 void ExtractElementConstantExpr::anchor() { }
227
228 void InsertElementConstantExpr::anchor() { }
229
230 void ShuffleVectorConstantExpr::anchor() { }
231
232 void ExtractValueConstantExpr::anchor() { }
233
234 void InsertValueConstantExpr::anchor() { }
235
236 void GetElementPtrConstantExpr::anchor() { }
237
238 void CompareConstantExpr::anchor() { }