blob: 506b0e3ac5abad6c7bc19a2c8ae1596260abeb32 [file] [log] [blame]
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +00001//===-- 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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Attributes.h"
17#include "llvm/IR/Module.h"
Diego Novilloc6574c12014-04-08 16:42:38 +000018#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/Regex.h"
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000020#include <algorithm>
Dan Gohmanb29cda92010-04-15 17:08:50 +000021using namespace llvm;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000022
23LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
Craig Topperc6207612014-04-09 06:08:46 +000024 : TheTrueVal(nullptr), TheFalseVal(nullptr),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000025 VoidTy(C, Type::VoidTyID),
26 LabelTy(C, Type::LabelTyID),
Dan Gohman518cda42011-12-17 00:04:22 +000027 HalfTy(C, Type::HalfTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000028 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),
Dale Johannesenbaa5d042010-09-10 20:55:01 +000034 X86_MMXTy(C, Type::X86_MMXTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000035 Int1Ty(C, 1),
36 Int8Ty(C, 8),
37 Int16Ty(C, 16),
38 Int32Ty(C, 32),
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000039 Int64Ty(C, 64) {
Craig Topperc6207612014-04-09 06:08:46 +000040 InlineAsmDiagHandler = nullptr;
41 InlineAsmDiagContext = nullptr;
42 DiagnosticHandler = nullptr;
43 DiagnosticContext = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000044 NamedStructTypesUniqueID = 0;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000045}
46
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000047namespace {
Diego Novilloc6574c12014-04-08 16:42:38 +000048
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.
Craig Topperc6207612014-04-09 06:08:46 +000053static Regex *OptimizationRemarkPattern = nullptr;
Diego Novilloc6574c12014-04-08 16:42:38 +000054
55/// \brief String to hold all the values passed via -pass-remarks. Every
56/// instance of -pass-remarks on the command line will be concatenated
57/// to this string. Values are stored inside braces and concatenated with
58/// the '|' operator. This implements the expected semantics that multiple
59/// -pass-remarks are additive.
60static std::string OptimizationRemarkExpr;
61
62struct PassRemarksOpt {
63 void operator=(const std::string &Val) const {
64 // Create a regexp object to match pass names for emitOptimizationRemark.
65 if (!Val.empty()) {
66 if (!OptimizationRemarkExpr.empty())
67 OptimizationRemarkExpr += "|";
68 OptimizationRemarkExpr += "(" + Val + ")";
69 delete OptimizationRemarkPattern;
70 OptimizationRemarkPattern = new Regex(OptimizationRemarkExpr);
71 std::string RegexError;
72 if (!OptimizationRemarkPattern->isValid(RegexError))
73 report_fatal_error("Invalid regular expression '" + Val +
74 "' in -pass-remarks: " + RegexError,
75 false);
76 }
77 };
78};
79
80static PassRemarksOpt PassRemarksOptLoc;
81
82// -pass-remarks
83// Command line flag to enable LLVMContext::emitOptimizationRemark()
84// and LLVMContext::emitOptimizationNote() calls.
85static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
86PassRemarks("pass-remarks", cl::value_desc("pattern"),
87 cl::desc("Enable optimization remarks from passes whose name match "
88 "the given regular expression"),
89 cl::Hidden, cl::location(PassRemarksOptLoc), cl::ValueRequired,
90 cl::ZeroOrMore);
91}
92
93bool
94LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const {
95 return OptimizationRemarkPattern &&
96 OptimizationRemarkPattern->match(PassName);
97}
98
99
100namespace {
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000101struct DropReferences {
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.second->dropAllReferences();
107 }
108};
Talin46e9b442012-02-05 20:54:10 +0000109
110// Temporary - drops pair.first instead of second.
111struct DropFirst {
112 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
113 // is a Constant*.
114 template<typename PairT>
115 void operator()(const PairT &P) {
116 P.first->dropAllReferences();
117 }
118};
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000119}
120
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000121LLVMContextImpl::~LLVMContextImpl() {
Owen Anderson8e89e412010-09-08 18:03:32 +0000122 // NOTE: We need to delete the contents of OwnedModules, but we have to
123 // duplicate it into a temporary vector, because the destructor of Module
124 // will try to remove itself from OwnedModules set. This would cause
125 // iterator invalidation if we iterated on the set directly.
126 std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end());
Nick Lewyckye9bb9a02011-07-12 00:26:08 +0000127 DeleteContainerPointers(Modules);
Owen Anderson8e89e412010-09-08 18:03:32 +0000128
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000129 // Free the constants. This is important to do here to ensure that they are
130 // freed before the LeakDetector is torn down.
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000131 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
132 DropReferences());
133 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +0000134 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000135 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +0000136 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000137 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +0000138 DropFirst());
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000139 ExprConstants.freeConstants();
140 ArrayConstants.freeConstants();
141 StructConstants.freeConstants();
142 VectorConstants.freeConstants();
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000143 DeleteContainerSeconds(CAZConstants);
144 DeleteContainerSeconds(CPNConstants);
145 DeleteContainerSeconds(UVConstants);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000146 InlineAsms.freeConstants();
Nick Lewyckye9bb9a02011-07-12 00:26:08 +0000147 DeleteContainerSeconds(IntConstants);
148 DeleteContainerSeconds(FPConstants);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000149
Chris Lattner3756b912012-01-23 22:57:10 +0000150 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
151 E = CDSConstants.end(); I != E; ++I)
152 delete I->second;
153 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +0000154
155 // Destroy attributes.
Bill Wendling4607f4b2012-12-20 01:36:59 +0000156 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000157 E = AttrsSet.end(); I != E; ) {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000158 FoldingSetIterator<AttributeImpl> Elem = I++;
Benjamin Kramer6bbdf702012-10-14 08:48:40 +0000159 delete &*Elem;
160 }
161
Bill Wendlingf86efb92012-11-20 05:09:20 +0000162 // Destroy attribute lists.
Bill Wendling6848e382012-12-19 22:42:22 +0000163 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000164 E = AttrsLists.end(); I != E; ) {
Bill Wendling6848e382012-12-19 22:42:22 +0000165 FoldingSetIterator<AttributeSetImpl> Elem = I++;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000166 delete &*Elem;
167 }
168
Bill Wendling164a4fb2013-01-24 00:14:46 +0000169 // Destroy attribute node lists.
170 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
171 E = AttrsSetNodes.end(); I != E; ) {
172 FoldingSetIterator<AttributeSetNode> Elem = I++;
173 delete &*Elem;
174 }
175
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000176 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
177 // and the NonUniquedMDNodes sets, so copy the values out first.
178 SmallVector<MDNode*, 8> MDNodes;
179 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
180 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
Chris Lattner68ef6942011-07-13 04:22:39 +0000181 I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000182 MDNodes.push_back(&*I);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000183 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
Dan Gohman060d5ba2010-10-12 00:15:27 +0000184 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
Chris Lattner68ef6942011-07-13 04:22:39 +0000185 E = MDNodes.end(); I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000186 (*I)->destroy();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000187 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
188 "Destroying all MDNodes didn't empty the Context's sets.");
Bill Wendlinge38b8042012-09-26 21:07:29 +0000189
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000190 // Destroy MDStrings.
Nick Lewyckye9bb9a02011-07-12 00:26:08 +0000191 DeleteContainerSeconds(MDStringCache);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000192}
David Blaikiea379b1812011-12-20 02:50:00 +0000193
194// ConstantsContext anchors
195void UnaryConstantExpr::anchor() { }
196
197void BinaryConstantExpr::anchor() { }
198
199void SelectConstantExpr::anchor() { }
200
201void ExtractElementConstantExpr::anchor() { }
202
203void InsertElementConstantExpr::anchor() { }
204
205void ShuffleVectorConstantExpr::anchor() { }
206
207void ExtractValueConstantExpr::anchor() { }
208
209void InsertValueConstantExpr::anchor() { }
210
211void GetElementPtrConstantExpr::anchor() { }
212
213void CompareConstantExpr::anchor() { }