blob: 4b44a6b69cad1e7146830784d8612b84aa03fd2a [file] [log] [blame]
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001//===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +00002//
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 Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Module.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000016#include "llvm/IR/OptBisect.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000017#include "llvm/IR/Type.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000018#include "llvm/Support/ManagedStatic.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000019#include <cassert>
20#include <utility>
21
Dan Gohmanb29cda92010-04-15 17:08:50 +000022using namespace llvm;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000023
24LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
Vivek Pandyab5ab8952017-09-15 20:10:09 +000025 : DiagHandler(llvm::make_unique<DiagnosticHandler>()),
26 VoidTy(C, Type::VoidTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000027 LabelTy(C, Type::LabelTyID),
Dan Gohman518cda42011-12-17 00:04:22 +000028 HalfTy(C, Type::HalfTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000029 FloatTy(C, Type::FloatTyID),
30 DoubleTy(C, Type::DoubleTyID),
31 MetadataTy(C, Type::MetadataTyID),
David Majnemerb611e3f2015-08-14 05:09:07 +000032 TokenTy(C, Type::TokenTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000033 X86_FP80Ty(C, Type::X86_FP80TyID),
34 FP128Ty(C, Type::FP128TyID),
35 PPC_FP128Ty(C, Type::PPC_FP128TyID),
Dale Johannesenbaa5d042010-09-10 20:55:01 +000036 X86_MMXTy(C, Type::X86_MMXTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000037 Int1Ty(C, 1),
38 Int8Ty(C, 8),
39 Int16Ty(C, 16),
40 Int32Ty(C, 32),
Kit Barton72918022015-04-17 15:32:15 +000041 Int64Ty(C, 64),
Eugene Zelenkode6cce22017-06-19 22:05:08 +000042 Int128Ty(C, 128) {}
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000043
44LLVMContextImpl::~LLVMContextImpl() {
David Blaikie4c82a802014-04-21 21:27:19 +000045 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
46 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
47 // the container. Avoid iterators during this operation:
48 while (!OwnedModules.empty())
49 delete *OwnedModules.begin();
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000050
51 // Drop references for MDNodes. Do this before Values get deleted to avoid
52 // unnecessary RAUW when nodes are still unresolved.
53 for (auto *I : DistinctMDNodes)
54 I->dropAllReferences();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000055#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000056 for (auto *I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000057 I->dropAllReferences();
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000058#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000059
60 // Also drop references that come from the Value bridges.
61 for (auto &Pair : ValuesAsMetadata)
62 Pair.second->dropUsers();
63 for (auto &Pair : MetadataAsValues)
64 Pair.second->dropUse();
65
66 // Destroy MDNodes.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +000067 for (MDNode *I : DistinctMDNodes)
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000068 I->deleteAsSubclass();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000069#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
70 for (CLASS * I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000071 delete I;
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000072#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000073
Benjamin Kramercb36bec2015-01-22 21:43:01 +000074 // Free the constants.
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +000075 for (auto *I : ExprConstants)
76 I->dropAllReferences();
77 for (auto *I : ArrayConstants)
78 I->dropAllReferences();
79 for (auto *I : StructConstants)
80 I->dropAllReferences();
81 for (auto *I : VectorConstants)
82 I->dropAllReferences();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000083 ExprConstants.freeConstants();
84 ArrayConstants.freeConstants();
85 StructConstants.freeConstants();
86 VectorConstants.freeConstants();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000087 InlineAsms.freeConstants();
Justin Lebar611c5c22016-10-10 16:26:13 +000088
89 CAZConstants.clear();
90 CPNConstants.clear();
91 UVConstants.clear();
92 IntConstants.clear();
93 FPConstants.clear();
Benjamin Krameraf28e7d2016-06-26 14:10:56 +000094
95 for (auto &CDSConstant : CDSConstants)
96 delete CDSConstant.second;
Chris Lattner3756b912012-01-23 22:57:10 +000097 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +000098
99 // Destroy attributes.
Bill Wendling4607f4b2012-12-20 01:36:59 +0000100 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000101 E = AttrsSet.end(); I != E; ) {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000102 FoldingSetIterator<AttributeImpl> Elem = I++;
Benjamin Kramer6bbdf702012-10-14 08:48:40 +0000103 delete &*Elem;
104 }
105
Bill Wendlingf86efb92012-11-20 05:09:20 +0000106 // Destroy attribute lists.
Reid Klecknerb5180542017-03-21 16:57:19 +0000107 for (FoldingSetIterator<AttributeListImpl> I = AttrsLists.begin(),
108 E = AttrsLists.end();
109 I != E;) {
110 FoldingSetIterator<AttributeListImpl> Elem = I++;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000111 delete &*Elem;
112 }
113
Bill Wendling164a4fb2013-01-24 00:14:46 +0000114 // Destroy attribute node lists.
115 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
116 E = AttrsSetNodes.end(); I != E; ) {
117 FoldingSetIterator<AttributeSetNode> Elem = I++;
118 delete &*Elem;
119 }
120
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000121 // Destroy MetadataAsValues.
122 {
123 SmallVector<MetadataAsValue *, 8> MDVs;
124 MDVs.reserve(MetadataAsValues.size());
125 for (auto &Pair : MetadataAsValues)
126 MDVs.push_back(Pair.second);
127 MetadataAsValues.clear();
128 for (auto *V : MDVs)
129 delete V;
130 }
131
132 // Destroy ValuesAsMetadata.
133 for (auto &Pair : ValuesAsMetadata)
134 delete Pair.second;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000135}
David Blaikiea379b1812011-12-20 02:50:00 +0000136
Manman Rendab999d2015-01-20 19:24:59 +0000137void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
138 bool Changed;
139 do {
140 Changed = false;
141
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000142 for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
143 auto *C = *I++;
Manman Rendab999d2015-01-20 19:24:59 +0000144 if (C->use_empty()) {
145 Changed = true;
146 C->destroyConstant();
147 }
148 }
Manman Rendab999d2015-01-20 19:24:59 +0000149 } while (Changed);
150}
151
152void Module::dropTriviallyDeadConstantArrays() {
153 Context.pImpl->dropTriviallyDeadConstantArrays();
154}
155
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000156namespace llvm {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000157
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000158/// \brief Make MDOperand transparent for hashing.
159///
160/// This overload of an implementation detail of the hashing library makes
161/// MDOperand hash to the same value as a \a Metadata pointer.
162///
163/// Note that overloading \a hash_value() as follows:
164///
165/// \code
166/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
167/// \endcode
168///
169/// does not cause MDOperand to be transparent. In particular, a bare pointer
170/// doesn't get hashed before it's combined, whereas \a MDOperand would.
171static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000172
173} // end namespace llvm
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000174
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000175unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
176 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000177#ifndef NDEBUG
178 {
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000179 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000180 unsigned RawHash = calculateHash(MDs);
181 assert(Hash == RawHash &&
182 "Expected hash of MDOperand to equal hash of Metadata*");
183 }
184#endif
185 return Hash;
186}
187
188unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
189 return hash_combine_range(Ops.begin(), Ops.end());
190}
191
Sanjoy Das9303c242015-09-24 19:14:18 +0000192StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
193 uint32_t NewIdx = BundleTagCache.size();
194 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
195}
196
197void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
198 Tags.resize(BundleTagCache.size());
199 for (const auto &T : BundleTagCache)
200 Tags[T.second] = T.first();
201}
202
203uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
204 auto I = BundleTagCache.find(Tag);
205 assert(I != BundleTagCache.end() && "Unknown tag!");
206 return I->second;
207}
208
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000209SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
210 auto NewSSID = SSC.size();
211 assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
212 "Hit the maximum number of synchronization scopes allowed!");
213 return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
214}
215
216void LLVMContextImpl::getSyncScopeNames(
217 SmallVectorImpl<StringRef> &SSNs) const {
218 SSNs.resize(SSC.size());
219 for (const auto &SSE : SSC)
220 SSNs[SSE.second] = SSE.first();
221}
222
Andrew Kayloraa641a52016-04-22 22:06:11 +0000223/// Singleton instance of the OptBisect class.
224///
225/// This singleton is accessed via the LLVMContext::getOptBisect() function. It
226/// provides a mechanism to disable passes and individual optimizations at
227/// compile time based on a command line option (-opt-bisect-limit) in order to
228/// perform a bisecting search for optimization-related problems.
229///
230/// Even if multiple LLVMContext objects are created, they will all return the
231/// same instance of OptBisect in order to provide a single bisect count. Any
232/// code that uses the OptBisect object should be serialized when bisection is
233/// enabled in order to enable a consistent bisect count.
234static ManagedStatic<OptBisect> OptBisector;
235
236OptBisect &LLVMContextImpl::getOptBisect() {
237 return *OptBisector;
238}