LLVM  3.7.0
LLVMContextImpl.cpp
Go to the documentation of this file.
1 //===-- 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"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/IR/Attributes.h"
17 #include "llvm/IR/DiagnosticInfo.h"
18 #include "llvm/IR/Module.h"
19 #include <algorithm>
20 using namespace llvm;
21 
23  : TheTrueVal(nullptr), TheFalseVal(nullptr),
24  VoidTy(C, Type::VoidTyID),
25  LabelTy(C, Type::LabelTyID),
26  HalfTy(C, Type::HalfTyID),
27  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),
33  X86_MMXTy(C, Type::X86_MMXTyID),
34  Int1Ty(C, 1),
35  Int8Ty(C, 8),
36  Int16Ty(C, 16),
37  Int32Ty(C, 32),
38  Int64Ty(C, 64),
39  Int128Ty(C, 128) {
40  InlineAsmDiagHandler = nullptr;
41  InlineAsmDiagContext = nullptr;
42  DiagnosticHandler = nullptr;
43  DiagnosticContext = nullptr;
45  YieldCallback = nullptr;
46  YieldOpaqueHandle = nullptr;
48 }
49 
50 namespace {
51 struct DropReferences {
52  // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
53  // is a Constant*.
54  template <typename PairT> void operator()(const PairT &P) {
55  P.second->dropAllReferences();
56  }
57 };
58 
59 // Temporary - drops pair.first instead of second.
60 struct 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 };
68 }
69 
71  // 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();
76 
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();
81 #define HANDLE_MDNODE_LEAF(CLASS) \
82  for (auto *I : CLASS##s) \
83  I->dropAllReferences();
84 #include "llvm/IR/Metadata.def"
85 
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.
93  for (MDNode *I : DistinctMDNodes)
94  I->deleteAsSubclass();
95 #define HANDLE_MDNODE_LEAF(CLASS) \
96  for (CLASS *I : CLASS##s) \
97  delete I;
98 #include "llvm/IR/Metadata.def"
99 
100  // Free the constants.
101  std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
102  DropFirst());
103  std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
104  DropFirst());
106  DropFirst());
108  DropFirst());
109  ExprConstants.freeConstants();
116  InlineAsms.freeConstants();
119 
121  E = CDSConstants.end(); I != E; ++I)
122  delete I->second;
123  CDSConstants.clear();
124 
125  // Destroy attributes.
127  E = AttrsSet.end(); I != E; ) {
129  delete &*Elem;
130  }
131 
132  // Destroy attribute lists.
134  E = AttrsLists.end(); I != E; ) {
136  delete &*Elem;
137  }
138 
139  // Destroy attribute node lists.
141  E = AttrsSetNodes.end(); I != E; ) {
143  delete &*Elem;
144  }
145 
146  // Destroy MetadataAsValues.
147  {
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 
161  // Destroy MDStrings.
162  MDStringCache.clear();
163 }
164 
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 
185 }
186 
187 namespace 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.
201 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
202 }
203 
204 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
205  unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
206 #ifndef NDEBUG
207  {
208  SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
209  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 
218  return hash_combine_range(Ops.begin(), Ops.end());
219 }
220 
221 // ConstantsContext anchors
222 void UnaryConstantExpr::anchor() { }
223 
224 void BinaryConstantExpr::anchor() { }
225 
226 void SelectConstantExpr::anchor() { }
227 
228 void ExtractElementConstantExpr::anchor() { }
229 
230 void InsertElementConstantExpr::anchor() { }
231 
232 void ShuffleVectorConstantExpr::anchor() { }
233 
234 void ExtractValueConstantExpr::anchor() { }
235 
236 void InsertValueConstantExpr::anchor() { }
237 
238 void GetElementPtrConstantExpr::anchor() { }
239 
240 void CompareConstantExpr::anchor() { }
241 
static unsigned calculateHash(MDNode *N, unsigned Offset=0)
void DeleteContainerSeconds(Container &C)
In a container of pairs (usually a map) whose second element is a pointer, deletes the second element...
Definition: STLExtras.h:325
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:597
FoldingSet< AttributeImpl > AttrsSet
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallPtrSet.h:78
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
iterator end() const
Definition: ArrayRef.h:123
op_iterator op_begin() const
Definition: Metadata.h:928
Metadata node.
Definition: Metadata.h:740
static const Metadata * get_hashable_data(const MDOperand &X)
Make MDOperand transparent for hashing.
LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler
void reserve(size_type N)
Definition: SmallVector.h:401
void dropTriviallyDeadConstantArrays()
Destroy the ConstantArrays if they are not used.
op_iterator op_end() const
Definition: Metadata.h:931
Metadata * get() const
Definition: Metadata.h:609
FoldingSet< AttributeSetImpl > AttrsLists
This file contains the simple types necessary to represent the attributes associated with functions a...
ConstantUniqueMap< InlineAsm > InlineAsms
LLVMContextImpl(LLVMContext &C)
const Type::TypeID FloatTyID
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
ArrayConstantsTy ArrayConstants
DenseMap< PointerType *, ConstantPointerNull * > CPNConstants
MapTy::iterator map_begin()
LLVMContext::YieldCallbackTy YieldCallback
DenseMap< Type *, ConstantAggregateZero * > CAZConstants
#define P(N)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
iterator begin() const
Definition: ArrayRef.h:122
iterator begin() const
Definition: SmallPtrSet.h:286
SmallPtrSet< MDNode *, 1 > DistinctMDNodes
VectorConstantsTy VectorConstants
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:43
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
void dropTriviallyDeadConstantArrays()
Destroy ConstantArrays in LLVMContext if they are not used.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
SmallPtrSet< Module *, 4 > OwnedModules
OwnedModules - The set of modules instantiated in this context, and which will be automatically delet...
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:481
DenseMap< Type *, UndefValue * > UVConstants
StringMap< MDString > MDStringCache
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
ConstantUniqueMap< ConstantExpr > ExprConstants
const Type::TypeID DoubleTyID
StructConstantsTy StructConstants
MapTy::iterator map_end()
FoldingSet< AttributeSetNode > AttrsSetNodes
LLVMContext::DiagnosticHandlerTy DiagnosticHandler
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
StringMap< ConstantDataSequential * > CDSConstants
Root of the metadata hierarchy.
Definition: Metadata.h:45