blob: 1e2080770fcd596da00cb62d50ea4a4a9c79d483 [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),
30 X86_FP80Ty(C, Type::X86_FP80TyID),
31 FP128Ty(C, Type::FP128TyID),
32 PPC_FP128Ty(C, Type::PPC_FP128TyID),
Dale Johannesenbaa5d042010-09-10 20:55:01 +000033 X86_MMXTy(C, Type::X86_MMXTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000034 Int1Ty(C, 1),
35 Int8Ty(C, 8),
36 Int16Ty(C, 16),
37 Int32Ty(C, 32),
Kit Barton72918022015-04-17 15:32:15 +000038 Int64Ty(C, 64),
39 Int128Ty(C, 128) {
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};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000068}
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000069
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();
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000081#define HANDLE_MDNODE_LEAF(CLASS) \
82 for (auto *I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000083 I->dropAllReferences();
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000084#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000085
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.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +000093 for (MDNode *I : DistinctMDNodes)
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000094 I->deleteAsSubclass();
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000095#define HANDLE_MDNODE_LEAF(CLASS) \
96 for (CLASS *I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000097 delete I;
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000098#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000099
Benjamin Kramercb36bec2015-01-22 21:43:01 +0000100 // Free the constants.
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000101 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +0000102 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000103 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +0000104 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000105 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +0000106 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +0000107 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +0000108 DropFirst());
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000109 ExprConstants.freeConstants();
110 ArrayConstants.freeConstants();
111 StructConstants.freeConstants();
112 VectorConstants.freeConstants();
David Blaikiecb2818f2014-11-25 02:26:22 +0000113 DeleteContainerSeconds(CAZConstants);
Chris Lattnerc7f9fd42012-01-23 15:20:12 +0000114 DeleteContainerSeconds(CPNConstants);
115 DeleteContainerSeconds(UVConstants);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000116 InlineAsms.freeConstants();
Nick Lewyckye9bb9a02011-07-12 00:26:08 +0000117 DeleteContainerSeconds(IntConstants);
118 DeleteContainerSeconds(FPConstants);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000119
Chris Lattner3756b912012-01-23 22:57:10 +0000120 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
121 E = CDSConstants.end(); I != E; ++I)
122 delete I->second;
123 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +0000124
125 // Destroy attributes.
Bill Wendling4607f4b2012-12-20 01:36:59 +0000126 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000127 E = AttrsSet.end(); I != E; ) {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000128 FoldingSetIterator<AttributeImpl> Elem = I++;
Benjamin Kramer6bbdf702012-10-14 08:48:40 +0000129 delete &*Elem;
130 }
131
Bill Wendlingf86efb92012-11-20 05:09:20 +0000132 // Destroy attribute lists.
Bill Wendling6848e382012-12-19 22:42:22 +0000133 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000134 E = AttrsLists.end(); I != E; ) {
Bill Wendling6848e382012-12-19 22:42:22 +0000135 FoldingSetIterator<AttributeSetImpl> Elem = I++;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000136 delete &*Elem;
137 }
138
Bill Wendling164a4fb2013-01-24 00:14:46 +0000139 // Destroy attribute node lists.
140 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
141 E = AttrsSetNodes.end(); I != E; ) {
142 FoldingSetIterator<AttributeSetNode> Elem = I++;
143 delete &*Elem;
144 }
145
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000146 // Destroy MetadataAsValues.
147 {
148 SmallVector<MetadataAsValue *, 8> MDVs;
149 MDVs.reserve(MetadataAsValues.size());
150 for (auto &Pair : MetadataAsValues)
151 MDVs.push_back(Pair.second);
152 MetadataAsValues.clear();
153 for (auto *V : MDVs)
154 delete V;
155 }
156
157 // Destroy ValuesAsMetadata.
158 for (auto &Pair : ValuesAsMetadata)
159 delete Pair.second;
160
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000161 // Destroy MDStrings.
Duncan P. N. Exon Smithf17e7402014-11-14 01:17:09 +0000162 MDStringCache.clear();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000163}
David Blaikiea379b1812011-12-20 02:50:00 +0000164
Manman Rendab999d2015-01-20 19:24:59 +0000165void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
166 bool Changed;
167 do {
168 Changed = false;
169
170 for (auto I = ArrayConstants.map_begin(), E = ArrayConstants.map_end();
171 I != E; ) {
172 auto *C = I->first;
173 I++;
174 if (C->use_empty()) {
175 Changed = true;
176 C->destroyConstant();
177 }
178 }
179
180 } while (Changed);
181}
182
183void Module::dropTriviallyDeadConstantArrays() {
184 Context.pImpl->dropTriviallyDeadConstantArrays();
185}
186
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000187namespace llvm {
188/// \brief Make MDOperand transparent for hashing.
189///
190/// This overload of an implementation detail of the hashing library makes
191/// MDOperand hash to the same value as a \a Metadata pointer.
192///
193/// Note that overloading \a hash_value() as follows:
194///
195/// \code
196/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
197/// \endcode
198///
199/// does not cause MDOperand to be transparent. In particular, a bare pointer
200/// doesn't get hashed before it's combined, whereas \a MDOperand would.
201static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000202}
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000203
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000204unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
205 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000206#ifndef NDEBUG
207 {
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000208 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000209 unsigned RawHash = calculateHash(MDs);
210 assert(Hash == RawHash &&
211 "Expected hash of MDOperand to equal hash of Metadata*");
212 }
213#endif
214 return Hash;
215}
216
217unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
218 return hash_combine_range(Ops.begin(), Ops.end());
219}
220
David Blaikiea379b1812011-12-20 02:50:00 +0000221// ConstantsContext anchors
222void UnaryConstantExpr::anchor() { }
223
224void BinaryConstantExpr::anchor() { }
225
226void SelectConstantExpr::anchor() { }
227
228void ExtractElementConstantExpr::anchor() { }
229
230void InsertElementConstantExpr::anchor() { }
231
232void ShuffleVectorConstantExpr::anchor() { }
233
234void ExtractValueConstantExpr::anchor() { }
235
236void InsertValueConstantExpr::anchor() { }
237
238void GetElementPtrConstantExpr::anchor() { }
239
240void CompareConstantExpr::anchor() { }
Philip Reames2b453952015-01-16 20:07:33 +0000241