blob: d5d9038d1e9d689010e8c999a74baff79a533fa7 [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
51LLVMContextImpl::~LLVMContextImpl() {
David Blaikie4c82a802014-04-21 21:27:19 +000052 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
53 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
54 // the container. Avoid iterators during this operation:
55 while (!OwnedModules.empty())
56 delete *OwnedModules.begin();
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000057
58 // Drop references for MDNodes. Do this before Values get deleted to avoid
59 // unnecessary RAUW when nodes are still unresolved.
60 for (auto *I : DistinctMDNodes)
61 I->dropAllReferences();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000062#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000063 for (auto *I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000064 I->dropAllReferences();
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000065#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000066
67 // Also drop references that come from the Value bridges.
68 for (auto &Pair : ValuesAsMetadata)
69 Pair.second->dropUsers();
70 for (auto &Pair : MetadataAsValues)
71 Pair.second->dropUse();
72
73 // Destroy MDNodes.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +000074 for (MDNode *I : DistinctMDNodes)
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000075 I->deleteAsSubclass();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000076#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
77 for (CLASS * I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000078 delete I;
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000079#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000080
Benjamin Kramercb36bec2015-01-22 21:43:01 +000081 // Free the constants.
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +000082 for (auto *I : ExprConstants)
83 I->dropAllReferences();
84 for (auto *I : ArrayConstants)
85 I->dropAllReferences();
86 for (auto *I : StructConstants)
87 I->dropAllReferences();
88 for (auto *I : VectorConstants)
89 I->dropAllReferences();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000090 ExprConstants.freeConstants();
91 ArrayConstants.freeConstants();
92 StructConstants.freeConstants();
93 VectorConstants.freeConstants();
David Blaikiecb2818f2014-11-25 02:26:22 +000094 DeleteContainerSeconds(CAZConstants);
Chris Lattnerc7f9fd42012-01-23 15:20:12 +000095 DeleteContainerSeconds(CPNConstants);
96 DeleteContainerSeconds(UVConstants);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000097 InlineAsms.freeConstants();
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000098 DeleteContainerSeconds(IntConstants);
99 DeleteContainerSeconds(FPConstants);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000100
Chris Lattner3756b912012-01-23 22:57:10 +0000101 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
102 E = CDSConstants.end(); I != E; ++I)
103 delete I->second;
104 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +0000105
106 // Destroy attributes.
Bill Wendling4607f4b2012-12-20 01:36:59 +0000107 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000108 E = AttrsSet.end(); I != E; ) {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000109 FoldingSetIterator<AttributeImpl> Elem = I++;
Benjamin Kramer6bbdf702012-10-14 08:48:40 +0000110 delete &*Elem;
111 }
112
Bill Wendlingf86efb92012-11-20 05:09:20 +0000113 // Destroy attribute lists.
Bill Wendling6848e382012-12-19 22:42:22 +0000114 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000115 E = AttrsLists.end(); I != E; ) {
Bill Wendling6848e382012-12-19 22:42:22 +0000116 FoldingSetIterator<AttributeSetImpl> Elem = I++;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000117 delete &*Elem;
118 }
119
Bill Wendling164a4fb2013-01-24 00:14:46 +0000120 // Destroy attribute node lists.
121 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
122 E = AttrsSetNodes.end(); I != E; ) {
123 FoldingSetIterator<AttributeSetNode> Elem = I++;
124 delete &*Elem;
125 }
126
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000127 // Destroy MetadataAsValues.
128 {
129 SmallVector<MetadataAsValue *, 8> MDVs;
130 MDVs.reserve(MetadataAsValues.size());
131 for (auto &Pair : MetadataAsValues)
132 MDVs.push_back(Pair.second);
133 MetadataAsValues.clear();
134 for (auto *V : MDVs)
135 delete V;
136 }
137
138 // Destroy ValuesAsMetadata.
139 for (auto &Pair : ValuesAsMetadata)
140 delete Pair.second;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000141}
David Blaikiea379b1812011-12-20 02:50:00 +0000142
Manman Rendab999d2015-01-20 19:24:59 +0000143void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
144 bool Changed;
145 do {
146 Changed = false;
147
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000148 for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
149 auto *C = *I++;
Manman Rendab999d2015-01-20 19:24:59 +0000150 if (C->use_empty()) {
151 Changed = true;
152 C->destroyConstant();
153 }
154 }
155
156 } while (Changed);
157}
158
159void Module::dropTriviallyDeadConstantArrays() {
160 Context.pImpl->dropTriviallyDeadConstantArrays();
161}
162
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000163namespace llvm {
164/// \brief Make MDOperand transparent for hashing.
165///
166/// This overload of an implementation detail of the hashing library makes
167/// MDOperand hash to the same value as a \a Metadata pointer.
168///
169/// Note that overloading \a hash_value() as follows:
170///
171/// \code
172/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
173/// \endcode
174///
175/// does not cause MDOperand to be transparent. In particular, a bare pointer
176/// doesn't get hashed before it's combined, whereas \a MDOperand would.
177static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000178}
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000179
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000180unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
181 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000182#ifndef NDEBUG
183 {
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000184 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000185 unsigned RawHash = calculateHash(MDs);
186 assert(Hash == RawHash &&
187 "Expected hash of MDOperand to equal hash of Metadata*");
188 }
189#endif
190 return Hash;
191}
192
193unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
194 return hash_combine_range(Ops.begin(), Ops.end());
195}
196
Sanjoy Das9303c242015-09-24 19:14:18 +0000197StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
198 uint32_t NewIdx = BundleTagCache.size();
199 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
200}
201
202void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
203 Tags.resize(BundleTagCache.size());
204 for (const auto &T : BundleTagCache)
205 Tags[T.second] = T.first();
206}
207
208uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
209 auto I = BundleTagCache.find(Tag);
210 assert(I != BundleTagCache.end() && "Unknown tag!");
211 return I->second;
212}
213
David Blaikiea379b1812011-12-20 02:50:00 +0000214// ConstantsContext anchors
215void UnaryConstantExpr::anchor() { }
216
217void BinaryConstantExpr::anchor() { }
218
219void SelectConstantExpr::anchor() { }
220
221void ExtractElementConstantExpr::anchor() { }
222
223void InsertElementConstantExpr::anchor() { }
224
225void ShuffleVectorConstantExpr::anchor() { }
226
227void ExtractValueConstantExpr::anchor() { }
228
229void InsertValueConstantExpr::anchor() { }
230
231void GetElementPtrConstantExpr::anchor() { }
232
233void CompareConstantExpr::anchor() { }
Philip Reames2b453952015-01-16 20:07:33 +0000234