Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Attributes.h" |
| 17 | #include "llvm/IR/Module.h" |
Diego Novillo | c6574c1 | 2014-04-08 16:42:38 +0000 | [diff] [blame] | 18 | #include "llvm/Support/CommandLine.h" |
| 19 | #include "llvm/Support/Regex.h" |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Dan Gohman | b29cda9 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 22 | |
| 23 | LLVMContextImpl::LLVMContextImpl(LLVMContext &C) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame^] | 24 | : TheTrueVal(nullptr), TheFalseVal(nullptr), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 25 | VoidTy(C, Type::VoidTyID), |
| 26 | LabelTy(C, Type::LabelTyID), |
Dan Gohman | 518cda4 | 2011-12-17 00:04:22 +0000 | [diff] [blame] | 27 | HalfTy(C, Type::HalfTyID), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 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), |
Dale Johannesen | baa5d04 | 2010-09-10 20:55:01 +0000 | [diff] [blame] | 34 | X86_MMXTy(C, Type::X86_MMXTyID), |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 35 | Int1Ty(C, 1), |
| 36 | Int8Ty(C, 8), |
| 37 | Int16Ty(C, 16), |
| 38 | Int32Ty(C, 32), |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 39 | Int64Ty(C, 64) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame^] | 40 | InlineAsmDiagHandler = nullptr; |
| 41 | InlineAsmDiagContext = nullptr; |
| 42 | DiagnosticHandler = nullptr; |
| 43 | DiagnosticContext = nullptr; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 44 | NamedStructTypesUniqueID = 0; |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 47 | namespace { |
Diego Novillo | c6574c1 | 2014-04-08 16:42:38 +0000 | [diff] [blame] | 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. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame^] | 53 | static Regex *OptimizationRemarkPattern = nullptr; |
Diego Novillo | c6574c1 | 2014-04-08 16:42:38 +0000 | [diff] [blame] | 54 | |
| 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. |
| 60 | static std::string OptimizationRemarkExpr; |
| 61 | |
| 62 | struct 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 | |
| 80 | static PassRemarksOpt PassRemarksOptLoc; |
| 81 | |
| 82 | // -pass-remarks |
| 83 | // Command line flag to enable LLVMContext::emitOptimizationRemark() |
| 84 | // and LLVMContext::emitOptimizationNote() calls. |
| 85 | static cl::opt<PassRemarksOpt, true, cl::parser<std::string>> |
| 86 | PassRemarks("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 | |
| 93 | bool |
| 94 | LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const { |
| 95 | return OptimizationRemarkPattern && |
| 96 | OptimizationRemarkPattern->match(PassName); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | namespace { |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 101 | struct 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 | }; |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 109 | |
| 110 | // Temporary - drops pair.first instead of second. |
| 111 | struct 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 Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 121 | LLVMContextImpl::~LLVMContextImpl() { |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 122 | // 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 Lewycky | e9bb9a0 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 127 | DeleteContainerPointers(Modules); |
Owen Anderson | 8e89e41 | 2010-09-08 18:03:32 +0000 | [diff] [blame] | 128 | |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 129 | // Free the constants. This is important to do here to ensure that they are |
| 130 | // freed before the LeakDetector is torn down. |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 131 | std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(), |
| 132 | DropReferences()); |
| 133 | std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(), |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 134 | DropFirst()); |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 135 | std::for_each(StructConstants.map_begin(), StructConstants.map_end(), |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 136 | DropFirst()); |
Jeffrey Yasskin | a6eedc3 | 2010-03-22 05:23:37 +0000 | [diff] [blame] | 137 | std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(), |
Talin | 46e9b44 | 2012-02-05 20:54:10 +0000 | [diff] [blame] | 138 | DropFirst()); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 139 | ExprConstants.freeConstants(); |
| 140 | ArrayConstants.freeConstants(); |
| 141 | StructConstants.freeConstants(); |
| 142 | VectorConstants.freeConstants(); |
Chris Lattner | c7f9fd4 | 2012-01-23 15:20:12 +0000 | [diff] [blame] | 143 | DeleteContainerSeconds(CAZConstants); |
| 144 | DeleteContainerSeconds(CPNConstants); |
| 145 | DeleteContainerSeconds(UVConstants); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 146 | InlineAsms.freeConstants(); |
Nick Lewycky | e9bb9a0 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 147 | DeleteContainerSeconds(IntConstants); |
| 148 | DeleteContainerSeconds(FPConstants); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 3756b91 | 2012-01-23 22:57:10 +0000 | [diff] [blame] | 150 | for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(), |
| 151 | E = CDSConstants.end(); I != E; ++I) |
| 152 | delete I->second; |
| 153 | CDSConstants.clear(); |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 154 | |
| 155 | // Destroy attributes. |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 156 | for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(), |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 157 | E = AttrsSet.end(); I != E; ) { |
Bill Wendling | 4607f4b | 2012-12-20 01:36:59 +0000 | [diff] [blame] | 158 | FoldingSetIterator<AttributeImpl> Elem = I++; |
Benjamin Kramer | 6bbdf70 | 2012-10-14 08:48:40 +0000 | [diff] [blame] | 159 | delete &*Elem; |
| 160 | } |
| 161 | |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 162 | // Destroy attribute lists. |
Bill Wendling | 6848e38 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 163 | for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(), |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 164 | E = AttrsLists.end(); I != E; ) { |
Bill Wendling | 6848e38 | 2012-12-19 22:42:22 +0000 | [diff] [blame] | 165 | FoldingSetIterator<AttributeSetImpl> Elem = I++; |
Bill Wendling | f86efb9 | 2012-11-20 05:09:20 +0000 | [diff] [blame] | 166 | delete &*Elem; |
| 167 | } |
| 168 | |
Bill Wendling | 164a4fb | 2013-01-24 00:14:46 +0000 | [diff] [blame] | 169 | // 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 Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 176 | // 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 Lattner | 68ef694 | 2011-07-13 04:22:39 +0000 | [diff] [blame] | 181 | I != E; ++I) |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 182 | MDNodes.push_back(&*I); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 183 | MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end()); |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 184 | for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(), |
Chris Lattner | 68ef694 | 2011-07-13 04:22:39 +0000 | [diff] [blame] | 185 | E = MDNodes.end(); I != E; ++I) |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 186 | (*I)->destroy(); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 187 | assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() && |
| 188 | "Destroying all MDNodes didn't empty the Context's sets."); |
Bill Wendling | e38b804 | 2012-09-26 21:07:29 +0000 | [diff] [blame] | 189 | |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 190 | // Destroy MDStrings. |
Nick Lewycky | e9bb9a0 | 2011-07-12 00:26:08 +0000 | [diff] [blame] | 191 | DeleteContainerSeconds(MDStringCache); |
Jeffrey Yasskin | 4cfb3a7 | 2010-03-21 21:17:34 +0000 | [diff] [blame] | 192 | } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 193 | |
| 194 | // ConstantsContext anchors |
| 195 | void UnaryConstantExpr::anchor() { } |
| 196 | |
| 197 | void BinaryConstantExpr::anchor() { } |
| 198 | |
| 199 | void SelectConstantExpr::anchor() { } |
| 200 | |
| 201 | void ExtractElementConstantExpr::anchor() { } |
| 202 | |
| 203 | void InsertElementConstantExpr::anchor() { } |
| 204 | |
| 205 | void ShuffleVectorConstantExpr::anchor() { } |
| 206 | |
| 207 | void ExtractValueConstantExpr::anchor() { } |
| 208 | |
| 209 | void InsertValueConstantExpr::anchor() { } |
| 210 | |
| 211 | void GetElementPtrConstantExpr::anchor() { } |
| 212 | |
| 213 | void CompareConstantExpr::anchor() { } |