blob: 2fa6780e569491bc91b60176ba0a40d25255a9dd [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"
Diego Novillo7f8af8b2014-05-22 14:19:46 +000017#include "llvm/IR/DiagnosticInfo.h"
Philip Reames2b453952015-01-16 20:07:33 +000018#include "llvm/IR/GCStrategy.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Module.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;
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000044 RespectDiagnosticFilters = false;
Juergen Ributzka34390c72014-05-16 02:33:15 +000045 YieldCallback = nullptr;
46 YieldOpaqueHandle = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000047 NamedStructTypesUniqueID = 0;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000048}
49
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000050namespace {
51struct DropReferences {
52 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
53 // is a Constant*.
Diego Novillo7f8af8b2014-05-22 14:19:46 +000054 template <typename PairT> void operator()(const PairT &P) {
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000055 P.second->dropAllReferences();
56 }
57};
Talin46e9b442012-02-05 20:54:10 +000058
59// Temporary - drops pair.first instead of second.
60struct DropFirst {
61 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
62 // is a Constant*.
63 template<typename PairT>
64 void operator()(const PairT &P) {
65 P.first->dropAllReferences();
66 }
67};
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000068}
69
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000070LLVMContextImpl::~LLVMContextImpl() {
David Blaikie4c82a802014-04-21 21:27:19 +000071 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
72 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
73 // the container. Avoid iterators during this operation:
74 while (!OwnedModules.empty())
75 delete *OwnedModules.begin();
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000076
77 // Drop references for MDNodes. Do this before Values get deleted to avoid
78 // unnecessary RAUW when nodes are still unresolved.
79 for (auto *I : DistinctMDNodes)
80 I->dropAllReferences();
81 for (auto *I : MDTuples)
82 I->dropAllReferences();
83 for (auto *I : MDLocations)
84 I->dropAllReferences();
85
86 // Also drop references that come from the Value bridges.
87 for (auto &Pair : ValuesAsMetadata)
88 Pair.second->dropUsers();
89 for (auto &Pair : MetadataAsValues)
90 Pair.second->dropUse();
91
92 // Destroy MDNodes.
93 for (UniquableMDNode *I : DistinctMDNodes)
94 I->deleteAsSubclass();
95 for (MDTuple *I : MDTuples)
96 delete I;
97 for (MDLocation *I : MDLocations)
98 delete I;
99
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000100 // Free the constants. This is important to do here to ensure that they are
101 // freed before the LeakDetector is torn down.
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000102 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000103 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000104 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +0000105 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000106 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +0000107 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000108 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +0000109 DropFirst());
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000110 ExprConstants.freeConstants();
111 ArrayConstants.freeConstants();
112 StructConstants.freeConstants();
113 VectorConstants.freeConstants();
David Blaikiecb2818f2014-11-25 02:26:22 +0000114 DeleteContainerSeconds(CAZConstants);
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000115 DeleteContainerSeconds(CPNConstants);
116 DeleteContainerSeconds(UVConstants);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000117 InlineAsms.freeConstants();
Nick Lewyckye9bb9a02011-07-12 00:26:08 +0000118 DeleteContainerSeconds(IntConstants);
119 DeleteContainerSeconds(FPConstants);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000120
Chris Lattner3756b912012-01-23 22:57:10 +0000121 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
122 E = CDSConstants.end(); I != E; ++I)
123 delete I->second;
124 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +0000125
126 // Destroy attributes.
Bill Wendling4607f4b2012-12-20 01:36:59 +0000127 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000128 E = AttrsSet.end(); I != E; ) {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000129 FoldingSetIterator<AttributeImpl> Elem = I++;
Benjamin Kramer6bbdf702012-10-14 08:48:40 +0000130 delete &*Elem;
131 }
132
Bill Wendlingf86efb92012-11-20 05:09:20 +0000133 // Destroy attribute lists.
Bill Wendling6848e382012-12-19 22:42:22 +0000134 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000135 E = AttrsLists.end(); I != E; ) {
Bill Wendling6848e382012-12-19 22:42:22 +0000136 FoldingSetIterator<AttributeSetImpl> Elem = I++;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000137 delete &*Elem;
138 }
139
Bill Wendling164a4fb2013-01-24 00:14:46 +0000140 // Destroy attribute node lists.
141 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
142 E = AttrsSetNodes.end(); I != E; ) {
143 FoldingSetIterator<AttributeSetNode> Elem = I++;
144 delete &*Elem;
145 }
146
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000147 // Destroy MetadataAsValues.
148 {
149 SmallVector<MetadataAsValue *, 8> MDVs;
150 MDVs.reserve(MetadataAsValues.size());
151 for (auto &Pair : MetadataAsValues)
152 MDVs.push_back(Pair.second);
153 MetadataAsValues.clear();
154 for (auto *V : MDVs)
155 delete V;
156 }
157
158 // Destroy ValuesAsMetadata.
159 for (auto &Pair : ValuesAsMetadata)
160 delete Pair.second;
161
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000162 // Destroy MDStrings.
Duncan P. N. Exon Smithf17e7402014-11-14 01:17:09 +0000163 MDStringCache.clear();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000164}
David Blaikiea379b1812011-12-20 02:50:00 +0000165
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000166namespace llvm {
167/// \brief Make MDOperand transparent for hashing.
168///
169/// This overload of an implementation detail of the hashing library makes
170/// MDOperand hash to the same value as a \a Metadata pointer.
171///
172/// Note that overloading \a hash_value() as follows:
173///
174/// \code
175/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
176/// \endcode
177///
178/// does not cause MDOperand to be transparent. In particular, a bare pointer
179/// doesn't get hashed before it's combined, whereas \a MDOperand would.
180static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
181}
182
183unsigned MDNodeOpsKey::calculateHash(MDNode *N) {
184 unsigned Hash = hash_combine_range(N->op_begin(), N->op_end());
185#ifndef NDEBUG
186 {
187 SmallVector<Metadata *, 8> MDs(N->op_begin(), N->op_end());
188 unsigned RawHash = calculateHash(MDs);
189 assert(Hash == RawHash &&
190 "Expected hash of MDOperand to equal hash of Metadata*");
191 }
192#endif
193 return Hash;
194}
195
196unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
197 return hash_combine_range(Ops.begin(), Ops.end());
198}
199
David Blaikiea379b1812011-12-20 02:50:00 +0000200// ConstantsContext anchors
201void UnaryConstantExpr::anchor() { }
202
203void BinaryConstantExpr::anchor() { }
204
205void SelectConstantExpr::anchor() { }
206
207void ExtractElementConstantExpr::anchor() { }
208
209void InsertElementConstantExpr::anchor() { }
210
211void ShuffleVectorConstantExpr::anchor() { }
212
213void ExtractValueConstantExpr::anchor() { }
214
215void InsertValueConstantExpr::anchor() { }
216
217void GetElementPtrConstantExpr::anchor() { }
218
219void CompareConstantExpr::anchor() { }
Philip Reames2b453952015-01-16 20:07:33 +0000220
221GCStrategy *LLVMContextImpl::getGCStrategy(const StringRef Name) {
222 // TODO: Arguably, just doing a linear search would be faster for small N
223 auto NMI = GCStrategyMap.find(Name);
224 if (NMI != GCStrategyMap.end())
225 return NMI->getValue();
226
227 for (auto& Entry : GCRegistry::entries()) {
228 if (Name == Entry.getName()) {
229 std::unique_ptr<GCStrategy> S = Entry.instantiate();
230 S->Name = Name;
231 GCStrategyMap[Name] = S.get();
232 GCStrategyList.push_back(std::move(S));
233 return GCStrategyList.back().get();
234 }
235 }
236
237 // No GCStrategy found for that name, error reporting is the job of our
238 // callers.
239 return nullptr;
240}
241
242