blob: c19e1be44fdc7903722e459d24f30fcb687218f4 [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)
Eugene Zelenkode6cce22017-06-19 22:05:08 +000025 : VoidTy(C, Type::VoidTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000026 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),
David Majnemerb611e3f2015-08-14 05:09:07 +000031 TokenTy(C, Type::TokenTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000032 X86_FP80Ty(C, Type::X86_FP80TyID),
33 FP128Ty(C, Type::FP128TyID),
34 PPC_FP128Ty(C, Type::PPC_FP128TyID),
Dale Johannesenbaa5d042010-09-10 20:55:01 +000035 X86_MMXTy(C, Type::X86_MMXTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000036 Int1Ty(C, 1),
37 Int8Ty(C, 8),
38 Int16Ty(C, 16),
39 Int32Ty(C, 32),
Kit Barton72918022015-04-17 15:32:15 +000040 Int64Ty(C, 64),
Eugene Zelenkode6cce22017-06-19 22:05:08 +000041 Int128Ty(C, 128) {}
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000042
43LLVMContextImpl::~LLVMContextImpl() {
David Blaikie4c82a802014-04-21 21:27:19 +000044 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
45 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
46 // the container. Avoid iterators during this operation:
47 while (!OwnedModules.empty())
48 delete *OwnedModules.begin();
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000049
50 // Drop references for MDNodes. Do this before Values get deleted to avoid
51 // unnecessary RAUW when nodes are still unresolved.
52 for (auto *I : DistinctMDNodes)
53 I->dropAllReferences();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000054#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000055 for (auto *I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000056 I->dropAllReferences();
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000057#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000058
59 // Also drop references that come from the Value bridges.
60 for (auto &Pair : ValuesAsMetadata)
61 Pair.second->dropUsers();
62 for (auto &Pair : MetadataAsValues)
63 Pair.second->dropUse();
64
65 // Destroy MDNodes.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +000066 for (MDNode *I : DistinctMDNodes)
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000067 I->deleteAsSubclass();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000068#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
69 for (CLASS * I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000070 delete I;
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000071#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000072
Benjamin Kramercb36bec2015-01-22 21:43:01 +000073 // Free the constants.
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +000074 for (auto *I : ExprConstants)
75 I->dropAllReferences();
76 for (auto *I : ArrayConstants)
77 I->dropAllReferences();
78 for (auto *I : StructConstants)
79 I->dropAllReferences();
80 for (auto *I : VectorConstants)
81 I->dropAllReferences();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000082 ExprConstants.freeConstants();
83 ArrayConstants.freeConstants();
84 StructConstants.freeConstants();
85 VectorConstants.freeConstants();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000086 InlineAsms.freeConstants();
Justin Lebar611c5c22016-10-10 16:26:13 +000087
88 CAZConstants.clear();
89 CPNConstants.clear();
90 UVConstants.clear();
91 IntConstants.clear();
92 FPConstants.clear();
Benjamin Krameraf28e7d2016-06-26 14:10:56 +000093
94 for (auto &CDSConstant : CDSConstants)
95 delete CDSConstant.second;
Chris Lattner3756b912012-01-23 22:57:10 +000096 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +000097
98 // Destroy attributes.
Bill Wendling4607f4b2012-12-20 01:36:59 +000099 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000100 E = AttrsSet.end(); I != E; ) {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000101 FoldingSetIterator<AttributeImpl> Elem = I++;
Benjamin Kramer6bbdf702012-10-14 08:48:40 +0000102 delete &*Elem;
103 }
104
Bill Wendlingf86efb92012-11-20 05:09:20 +0000105 // Destroy attribute lists.
Reid Klecknerb5180542017-03-21 16:57:19 +0000106 for (FoldingSetIterator<AttributeListImpl> I = AttrsLists.begin(),
107 E = AttrsLists.end();
108 I != E;) {
109 FoldingSetIterator<AttributeListImpl> Elem = I++;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000110 delete &*Elem;
111 }
112
Bill Wendling164a4fb2013-01-24 00:14:46 +0000113 // Destroy attribute node lists.
114 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
115 E = AttrsSetNodes.end(); I != E; ) {
116 FoldingSetIterator<AttributeSetNode> Elem = I++;
117 delete &*Elem;
118 }
119
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000120 // Destroy MetadataAsValues.
121 {
122 SmallVector<MetadataAsValue *, 8> MDVs;
123 MDVs.reserve(MetadataAsValues.size());
124 for (auto &Pair : MetadataAsValues)
125 MDVs.push_back(Pair.second);
126 MetadataAsValues.clear();
127 for (auto *V : MDVs)
128 delete V;
129 }
130
131 // Destroy ValuesAsMetadata.
132 for (auto &Pair : ValuesAsMetadata)
133 delete Pair.second;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000134}
David Blaikiea379b1812011-12-20 02:50:00 +0000135
Manman Rendab999d2015-01-20 19:24:59 +0000136void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
137 bool Changed;
138 do {
139 Changed = false;
140
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000141 for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
142 auto *C = *I++;
Manman Rendab999d2015-01-20 19:24:59 +0000143 if (C->use_empty()) {
144 Changed = true;
145 C->destroyConstant();
146 }
147 }
Manman Rendab999d2015-01-20 19:24:59 +0000148 } while (Changed);
149}
150
151void Module::dropTriviallyDeadConstantArrays() {
152 Context.pImpl->dropTriviallyDeadConstantArrays();
153}
154
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000155namespace llvm {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000156
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000157/// \brief Make MDOperand transparent for hashing.
158///
159/// This overload of an implementation detail of the hashing library makes
160/// MDOperand hash to the same value as a \a Metadata pointer.
161///
162/// Note that overloading \a hash_value() as follows:
163///
164/// \code
165/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
166/// \endcode
167///
168/// does not cause MDOperand to be transparent. In particular, a bare pointer
169/// doesn't get hashed before it's combined, whereas \a MDOperand would.
170static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000171
172} // end namespace llvm
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000173
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000174unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
175 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000176#ifndef NDEBUG
177 {
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000178 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000179 unsigned RawHash = calculateHash(MDs);
180 assert(Hash == RawHash &&
181 "Expected hash of MDOperand to equal hash of Metadata*");
182 }
183#endif
184 return Hash;
185}
186
187unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
188 return hash_combine_range(Ops.begin(), Ops.end());
189}
190
Sanjoy Das9303c242015-09-24 19:14:18 +0000191StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
192 uint32_t NewIdx = BundleTagCache.size();
193 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
194}
195
196void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
197 Tags.resize(BundleTagCache.size());
198 for (const auto &T : BundleTagCache)
199 Tags[T.second] = T.first();
200}
201
202uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
203 auto I = BundleTagCache.find(Tag);
204 assert(I != BundleTagCache.end() && "Unknown tag!");
205 return I->second;
206}
207
Andrew Kayloraa641a52016-04-22 22:06:11 +0000208/// Singleton instance of the OptBisect class.
209///
210/// This singleton is accessed via the LLVMContext::getOptBisect() function. It
211/// provides a mechanism to disable passes and individual optimizations at
212/// compile time based on a command line option (-opt-bisect-limit) in order to
213/// perform a bisecting search for optimization-related problems.
214///
215/// Even if multiple LLVMContext objects are created, they will all return the
216/// same instance of OptBisect in order to provide a single bisect count. Any
217/// code that uses the OptBisect object should be serialized when bisection is
218/// enabled in order to enable a consistent bisect count.
219static ManagedStatic<OptBisect> OptBisector;
220
221OptBisect &LLVMContextImpl::getOptBisect() {
222 return *OptBisector;
223}