blob: 3e354b5e0cfef97e71ae095c9d7884acb11d6e82 [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"
Andrew Kayloraa641a52016-04-22 22:06:11 +000019#include "llvm/IR/OptBisect.h"
20#include "llvm/Support/ManagedStatic.h"
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000021#include <algorithm>
Dan Gohmanb29cda92010-04-15 17:08:50 +000022using namespace llvm;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000023
24LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
Craig Topperc6207612014-04-09 06:08:46 +000025 : TheTrueVal(nullptr), TheFalseVal(nullptr),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000026 VoidTy(C, Type::VoidTyID),
27 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),
42 Int128Ty(C, 128) {
Craig Topperc6207612014-04-09 06:08:46 +000043 InlineAsmDiagHandler = nullptr;
44 InlineAsmDiagContext = nullptr;
45 DiagnosticHandler = nullptr;
46 DiagnosticContext = nullptr;
Duncan P. N. Exon Smith30c92422014-10-01 18:36:03 +000047 RespectDiagnosticFilters = false;
Juergen Ributzka34390c72014-05-16 02:33:15 +000048 YieldCallback = nullptr;
49 YieldOpaqueHandle = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000050 NamedStructTypesUniqueID = 0;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000051}
52
53LLVMContextImpl::~LLVMContextImpl() {
David Blaikie4c82a802014-04-21 21:27:19 +000054 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
55 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
56 // the container. Avoid iterators during this operation:
57 while (!OwnedModules.empty())
58 delete *OwnedModules.begin();
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000059
60 // Drop references for MDNodes. Do this before Values get deleted to avoid
61 // unnecessary RAUW when nodes are still unresolved.
62 for (auto *I : DistinctMDNodes)
63 I->dropAllReferences();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000064#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000065 for (auto *I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000066 I->dropAllReferences();
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000067#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000068
69 // Also drop references that come from the Value bridges.
70 for (auto &Pair : ValuesAsMetadata)
71 Pair.second->dropUsers();
72 for (auto &Pair : MetadataAsValues)
73 Pair.second->dropUse();
74
75 // Destroy MDNodes.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +000076 for (MDNode *I : DistinctMDNodes)
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000077 I->deleteAsSubclass();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000078#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
79 for (CLASS * I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000080 delete I;
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000081#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000082
Benjamin Kramercb36bec2015-01-22 21:43:01 +000083 // Free the constants.
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +000084 for (auto *I : ExprConstants)
85 I->dropAllReferences();
86 for (auto *I : ArrayConstants)
87 I->dropAllReferences();
88 for (auto *I : StructConstants)
89 I->dropAllReferences();
90 for (auto *I : VectorConstants)
91 I->dropAllReferences();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000092 ExprConstants.freeConstants();
93 ArrayConstants.freeConstants();
94 StructConstants.freeConstants();
95 VectorConstants.freeConstants();
David Blaikiecb2818f2014-11-25 02:26:22 +000096 DeleteContainerSeconds(CAZConstants);
Chris Lattnerc7f9fd42012-01-23 15:20:12 +000097 DeleteContainerSeconds(CPNConstants);
98 DeleteContainerSeconds(UVConstants);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000099 InlineAsms.freeConstants();
Nick Lewyckye9bb9a02011-07-12 00:26:08 +0000100 DeleteContainerSeconds(IntConstants);
101 DeleteContainerSeconds(FPConstants);
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000102
103 for (auto &CDSConstant : CDSConstants)
104 delete CDSConstant.second;
Chris Lattner3756b912012-01-23 22:57:10 +0000105 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +0000106
107 // Destroy attributes.
Bill Wendling4607f4b2012-12-20 01:36:59 +0000108 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000109 E = AttrsSet.end(); I != E; ) {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000110 FoldingSetIterator<AttributeImpl> Elem = I++;
Benjamin Kramer6bbdf702012-10-14 08:48:40 +0000111 delete &*Elem;
112 }
113
Bill Wendlingf86efb92012-11-20 05:09:20 +0000114 // Destroy attribute lists.
Bill Wendling6848e382012-12-19 22:42:22 +0000115 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000116 E = AttrsLists.end(); I != E; ) {
Bill Wendling6848e382012-12-19 22:42:22 +0000117 FoldingSetIterator<AttributeSetImpl> Elem = I++;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000118 delete &*Elem;
119 }
120
Bill Wendling164a4fb2013-01-24 00:14:46 +0000121 // Destroy attribute node lists.
122 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
123 E = AttrsSetNodes.end(); I != E; ) {
124 FoldingSetIterator<AttributeSetNode> Elem = I++;
125 delete &*Elem;
126 }
127
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000128 // Destroy MetadataAsValues.
129 {
130 SmallVector<MetadataAsValue *, 8> MDVs;
131 MDVs.reserve(MetadataAsValues.size());
132 for (auto &Pair : MetadataAsValues)
133 MDVs.push_back(Pair.second);
134 MetadataAsValues.clear();
135 for (auto *V : MDVs)
136 delete V;
137 }
138
139 // Destroy ValuesAsMetadata.
140 for (auto &Pair : ValuesAsMetadata)
141 delete Pair.second;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000142}
David Blaikiea379b1812011-12-20 02:50:00 +0000143
Manman Rendab999d2015-01-20 19:24:59 +0000144void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
145 bool Changed;
146 do {
147 Changed = false;
148
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000149 for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
150 auto *C = *I++;
Manman Rendab999d2015-01-20 19:24:59 +0000151 if (C->use_empty()) {
152 Changed = true;
153 C->destroyConstant();
154 }
155 }
156
157 } while (Changed);
158}
159
160void Module::dropTriviallyDeadConstantArrays() {
161 Context.pImpl->dropTriviallyDeadConstantArrays();
162}
163
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000164namespace llvm {
165/// \brief Make MDOperand transparent for hashing.
166///
167/// This overload of an implementation detail of the hashing library makes
168/// MDOperand hash to the same value as a \a Metadata pointer.
169///
170/// Note that overloading \a hash_value() as follows:
171///
172/// \code
173/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
174/// \endcode
175///
176/// does not cause MDOperand to be transparent. In particular, a bare pointer
177/// doesn't get hashed before it's combined, whereas \a MDOperand would.
178static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000179}
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000180
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000181unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
182 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000183#ifndef NDEBUG
184 {
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000185 SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000186 unsigned RawHash = calculateHash(MDs);
187 assert(Hash == RawHash &&
188 "Expected hash of MDOperand to equal hash of Metadata*");
189 }
190#endif
191 return Hash;
192}
193
194unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
195 return hash_combine_range(Ops.begin(), Ops.end());
196}
197
Sanjoy Das9303c242015-09-24 19:14:18 +0000198StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
199 uint32_t NewIdx = BundleTagCache.size();
200 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
201}
202
203void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
204 Tags.resize(BundleTagCache.size());
205 for (const auto &T : BundleTagCache)
206 Tags[T.second] = T.first();
207}
208
209uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
210 auto I = BundleTagCache.find(Tag);
211 assert(I != BundleTagCache.end() && "Unknown tag!");
212 return I->second;
213}
214
David Blaikiea379b1812011-12-20 02:50:00 +0000215// ConstantsContext anchors
216void UnaryConstantExpr::anchor() { }
217
218void BinaryConstantExpr::anchor() { }
219
220void SelectConstantExpr::anchor() { }
221
222void ExtractElementConstantExpr::anchor() { }
223
224void InsertElementConstantExpr::anchor() { }
225
226void ShuffleVectorConstantExpr::anchor() { }
227
228void ExtractValueConstantExpr::anchor() { }
229
230void InsertValueConstantExpr::anchor() { }
231
232void GetElementPtrConstantExpr::anchor() { }
233
234void CompareConstantExpr::anchor() { }
Philip Reames2b453952015-01-16 20:07:33 +0000235
Andrew Kayloraa641a52016-04-22 22:06:11 +0000236/// Singleton instance of the OptBisect class.
237///
238/// This singleton is accessed via the LLVMContext::getOptBisect() function. It
239/// provides a mechanism to disable passes and individual optimizations at
240/// compile time based on a command line option (-opt-bisect-limit) in order to
241/// perform a bisecting search for optimization-related problems.
242///
243/// Even if multiple LLVMContext objects are created, they will all return the
244/// same instance of OptBisect in order to provide a single bisect count. Any
245/// code that uses the OptBisect object should be serialized when bisection is
246/// enabled in order to enable a consistent bisect count.
247static ManagedStatic<OptBisect> OptBisector;
248
249OptBisect &LLVMContextImpl::getOptBisect() {
250 return *OptBisector;
251}