Do not make -pass-remarks additive.
[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 struct PassRemarksOpt {
77   void operator=(const std::string &Val) const {
78     // Create a regexp object to match pass names for emitOptimizationRemark.
79     if (!Val.empty()) {
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,
86                            false);
87     }
88   };
89 };
90
91 static PassRemarksOpt PassRemarksOptLoc;
92
93 // -pass-remarks
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,
101             cl::ZeroOrMore);
102 }
103
104 bool
105 LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const {
106   return OptimizationRemarkPattern &&
107          OptimizationRemarkPattern->match(PassName);
108 }
109
110
111 namespace {
112 struct DropReferences {
113   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
114   // is a Constant*.
115   template<typename PairT>
116   void operator()(const PairT &P) {
117     P.second->dropAllReferences();
118   }
119 };
120
121 // Temporary - drops pair.first instead of second.
122 struct DropFirst {
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.first->dropAllReferences();
128   }
129 };
130 }
131
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();
138   
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(),
142                 DropReferences());
143   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
144                 DropFirst());
145   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
146                 DropFirst());
147   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
148                 DropFirst());
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);
159   
160   for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
161        E = CDSConstants.end(); I != E; ++I)
162     delete I->second;
163   CDSConstants.clear();
164
165   // Destroy attributes.
166   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
167          E = AttrsSet.end(); I != E; ) {
168     FoldingSetIterator<AttributeImpl> Elem = I++;
169     delete &*Elem;
170   }
171
172   // Destroy attribute lists.
173   for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
174          E = AttrsLists.end(); I != E; ) {
175     FoldingSetIterator<AttributeSetImpl> Elem = I++;
176     delete &*Elem;
177   }
178
179   // Destroy attribute node lists.
180   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
181          E = AttrsSetNodes.end(); I != E; ) {
182     FoldingSetIterator<AttributeSetNode> Elem = I++;
183     delete &*Elem;
184   }
185
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();
191        I != E; ++I)
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)
196     (*I)->destroy();
197   assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
198          "Destroying all MDNodes didn't empty the Context's sets.");
199
200   // Destroy MDStrings.
201   DeleteContainerSeconds(MDStringCache);
202
203   // Destroy all run listeners.
204   for (auto &L : RunListeners)
205     delete L;
206   RunListeners.clear();
207 }
208
209 // ConstantsContext anchors
210 void UnaryConstantExpr::anchor() { }
211
212 void BinaryConstantExpr::anchor() { }
213
214 void SelectConstantExpr::anchor() { }
215
216 void ExtractElementConstantExpr::anchor() { }
217
218 void InsertElementConstantExpr::anchor() { }
219
220 void ShuffleVectorConstantExpr::anchor() { }
221
222 void ExtractValueConstantExpr::anchor() { }
223
224 void InsertValueConstantExpr::anchor() { }
225
226 void GetElementPtrConstantExpr::anchor() { }
227
228 void CompareConstantExpr::anchor() { }