Revert "[PM] Add pass run listeners to the pass manager."
[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/Support/CommandLine.h"
19 #include "llvm/Support/Regex.h"
20 #include <algorithm>
21 using namespace llvm;
22
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),
35     Int1Ty(C, 1),
36     Int8Ty(C, 8),
37     Int16Ty(C, 16),
38     Int32Ty(C, 32),
39     Int64Ty(C, 64) {
40   InlineAsmDiagHandler = nullptr;
41   InlineAsmDiagContext = nullptr;
42   DiagnosticHandler = nullptr;
43   DiagnosticContext = nullptr;
44   NamedStructTypesUniqueID = 0;
45 }
46
47 namespace {
48
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;
54
55 struct PassRemarksOpt {
56   void operator=(const std::string &Val) const {
57     // Create a regexp object to match pass names for emitOptimizationRemark.
58     if (!Val.empty()) {
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,
65                            false);
66     }
67   };
68 };
69
70 static PassRemarksOpt PassRemarksOptLoc;
71
72 // -pass-remarks
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,
80             cl::ZeroOrMore);
81 }
82
83 bool
84 LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const {
85   return OptimizationRemarkPattern &&
86          OptimizationRemarkPattern->match(PassName);
87 }
88
89
90 namespace {
91 struct DropReferences {
92   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
93   // is a Constant*.
94   template<typename PairT>
95   void operator()(const PairT &P) {
96     P.second->dropAllReferences();
97   }
98 };
99
100 // Temporary - drops pair.first instead of second.
101 struct DropFirst {
102   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
103   // is a Constant*.
104   template<typename PairT>
105   void operator()(const PairT &P) {
106     P.first->dropAllReferences();
107   }
108 };
109 }
110
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();
117   
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(),
121                 DropReferences());
122   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
123                 DropFirst());
124   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
125                 DropFirst());
126   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
127                 DropFirst());
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);
138   
139   for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
140        E = CDSConstants.end(); I != E; ++I)
141     delete I->second;
142   CDSConstants.clear();
143
144   // Destroy attributes.
145   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
146          E = AttrsSet.end(); I != E; ) {
147     FoldingSetIterator<AttributeImpl> Elem = I++;
148     delete &*Elem;
149   }
150
151   // Destroy attribute lists.
152   for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
153          E = AttrsLists.end(); I != E; ) {
154     FoldingSetIterator<AttributeSetImpl> Elem = I++;
155     delete &*Elem;
156   }
157
158   // Destroy attribute node lists.
159   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
160          E = AttrsSetNodes.end(); I != E; ) {
161     FoldingSetIterator<AttributeSetNode> Elem = I++;
162     delete &*Elem;
163   }
164
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();
170        I != E; ++I)
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)
175     (*I)->destroy();
176   assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
177          "Destroying all MDNodes didn't empty the Context's sets.");
178
179   // Destroy MDStrings.
180   DeleteContainerSeconds(MDStringCache);
181 }
182
183 // ConstantsContext anchors
184 void UnaryConstantExpr::anchor() { }
185
186 void BinaryConstantExpr::anchor() { }
187
188 void SelectConstantExpr::anchor() { }
189
190 void ExtractElementConstantExpr::anchor() { }
191
192 void InsertElementConstantExpr::anchor() { }
193
194 void ShuffleVectorConstantExpr::anchor() { }
195
196 void ExtractValueConstantExpr::anchor() { }
197
198 void InsertValueConstantExpr::anchor() { }
199
200 void GetElementPtrConstantExpr::anchor() { }
201
202 void CompareConstantExpr::anchor() { }