blob: dc367fee88423a72abc99628b5049363a757f5dc [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"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000019#include <algorithm>
Dan Gohmanb29cda92010-04-15 17:08:50 +000020using namespace llvm;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000021
22LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
Craig Topperc6207612014-04-09 06:08:46 +000023 : TheTrueVal(nullptr), TheFalseVal(nullptr),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000024 VoidTy(C, Type::VoidTyID),
25 LabelTy(C, Type::LabelTyID),
Dan Gohman518cda42011-12-17 00:04:22 +000026 HalfTy(C, Type::HalfTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000027 FloatTy(C, Type::FloatTyID),
28 DoubleTy(C, Type::DoubleTyID),
29 MetadataTy(C, Type::MetadataTyID),
David Majnemerb611e3f2015-08-14 05:09:07 +000030 TokenTy(C, Type::TokenTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000031 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),
Kit Barton72918022015-04-17 15:32:15 +000039 Int64Ty(C, 64),
40 Int128Ty(C, 128) {
Craig Topperc6207612014-04-09 06:08:46 +000041 InlineAsmDiagHandler = nullptr;
42 InlineAsmDiagContext = nullptr;
43 DiagnosticHandler = nullptr;
44 DiagnosticContext = nullptr;
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000045 RespectDiagnosticFilters = false;
Juergen Ributzka34390c72014-05-16 02:33:15 +000046 YieldCallback = nullptr;
47 YieldOpaqueHandle = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000048 NamedStructTypesUniqueID = 0;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000049}
50
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000051namespace {
52struct DropReferences {
53 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
54 // is a Constant*.
Diego Novillo7f8af8b2014-05-22 14:19:46 +000055 template <typename PairT> void operator()(const PairT &P) {
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000056 P.second->dropAllReferences();
57 }
58};
Talin46e9b442012-02-05 20:54:10 +000059
60// Temporary - drops pair.first instead of second.
61struct DropFirst {
62 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
63 // is a Constant*.
64 template<typename PairT>
65 void operator()(const PairT &P) {
66 P.first->dropAllReferences();
67 }
68};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000069}
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000070
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000071LLVMContextImpl::~LLVMContextImpl() {
David Blaikie4c82a802014-04-21 21:27:19 +000072 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
73 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
74 // the container. Avoid iterators during this operation:
75 while (!OwnedModules.empty())
76 delete *OwnedModules.begin();
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000077
78 // Drop references for MDNodes. Do this before Values get deleted to avoid
79 // unnecessary RAUW when nodes are still unresolved.
80 for (auto *I : DistinctMDNodes)
81 I->dropAllReferences();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000082#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000083 for (auto *I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000084 I->dropAllReferences();
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000085#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000086
87 // Also drop references that come from the Value bridges.
88 for (auto &Pair : ValuesAsMetadata)
89 Pair.second->dropUsers();
90 for (auto &Pair : MetadataAsValues)
91 Pair.second->dropUse();
92
93 // Destroy MDNodes.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +000094 for (MDNode *I : DistinctMDNodes)
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000095 I->deleteAsSubclass();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000096#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
97 for (CLASS * I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000098 delete I;
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000099#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +0000100
Benjamin Kramercb36bec2015-01-22 21:43:01 +0000101 // Free the constants.
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
Manman Rendab999d2015-01-20 19:24:59 +0000166void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
167 bool Changed;
168 do {
169 Changed = false;
170
171 for (auto I = ArrayConstants.map_begin(), E = ArrayConstants.map_end();
172 I != E; ) {
173 auto *C = I->first;
174 I++;
175 if (C->use_empty()) {
176 Changed = true;
177 C->destroyConstant();
178 }
179 }
180
181 } while (Changed);
182}
183
184void Module::dropTriviallyDeadConstantArrays() {
185 Context.pImpl->dropTriviallyDeadConstantArrays();
186}
187
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000188namespace llvm {
189/// \brief Make MDOperand transparent for hashing.
190///
191/// This overload of an implementation detail of the hashing library makes
192/// MDOperand hash to the same value as a \a Metadata pointer.
193///
194/// Note that overloading \a hash_value() as follows:
195///
196/// \code
197/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
198/// \endcode
199///
200/// does not cause MDOperand to be transparent. In particular, a bare pointer
201/// doesn't get hashed before it's combined, whereas \a MDOperand would.
202static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000203}
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000204
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000205unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
206 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000207#ifndef NDEBUG
208 {
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000209 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000210 unsigned RawHash = calculateHash(MDs);
211 assert(Hash == RawHash &&
212 "Expected hash of MDOperand to equal hash of Metadata*");
213 }
214#endif
215 return Hash;
216}
217
218unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
219 return hash_combine_range(Ops.begin(), Ops.end());
220}
221
David Blaikiea379b1812011-12-20 02:50:00 +0000222// ConstantsContext anchors
223void UnaryConstantExpr::anchor() { }
224
225void BinaryConstantExpr::anchor() { }
226
227void SelectConstantExpr::anchor() { }
228
229void ExtractElementConstantExpr::anchor() { }
230
231void InsertElementConstantExpr::anchor() { }
232
233void ShuffleVectorConstantExpr::anchor() { }
234
235void ExtractValueConstantExpr::anchor() { }
236
237void InsertValueConstantExpr::anchor() { }
238
239void GetElementPtrConstantExpr::anchor() { }
240
241void CompareConstantExpr::anchor() { }
Philip Reames2b453952015-01-16 20:07:33 +0000242