LLVM  4.0.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 "llvm/IR/OptBisect.h"
21 #include <algorithm>
22 using namespace llvm;
23 
25  : TheTrueVal(nullptr), TheFalseVal(nullptr),
26  VoidTy(C, Type::VoidTyID),
27  LabelTy(C, Type::LabelTyID),
28  HalfTy(C, Type::HalfTyID),
29  FloatTy(C, Type::FloatTyID),
30  DoubleTy(C, Type::DoubleTyID),
31  MetadataTy(C, Type::MetadataTyID),
32  TokenTy(C, Type::TokenTyID),
33  X86_FP80Ty(C, Type::X86_FP80TyID),
34  FP128Ty(C, Type::FP128TyID),
35  PPC_FP128Ty(C, Type::PPC_FP128TyID),
36  X86_MMXTy(C, Type::X86_MMXTyID),
37  Int1Ty(C, 1),
38  Int8Ty(C, 8),
39  Int16Ty(C, 16),
40  Int32Ty(C, 32),
41  Int64Ty(C, 64),
42  Int128Ty(C, 128) {
43  InlineAsmDiagHandler = nullptr;
44  InlineAsmDiagContext = nullptr;
45  DiagnosticHandler = nullptr;
46  DiagnosticContext = nullptr;
49  YieldCallback = nullptr;
50  YieldOpaqueHandle = nullptr;
52 }
53 
55  // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
56  // will call LLVMContextImpl::removeModule, thus invalidating iterators into
57  // the container. Avoid iterators during this operation:
58  while (!OwnedModules.empty())
59  delete *OwnedModules.begin();
60 
61  // Drop references for MDNodes. Do this before Values get deleted to avoid
62  // unnecessary RAUW when nodes are still unresolved.
63  for (auto *I : DistinctMDNodes)
64  I->dropAllReferences();
65 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
66  for (auto *I : CLASS##s) \
67  I->dropAllReferences();
68 #include "llvm/IR/Metadata.def"
69 
70  // Also drop references that come from the Value bridges.
71  for (auto &Pair : ValuesAsMetadata)
72  Pair.second->dropUsers();
73  for (auto &Pair : MetadataAsValues)
74  Pair.second->dropUse();
75 
76  // Destroy MDNodes.
77  for (MDNode *I : DistinctMDNodes)
78  I->deleteAsSubclass();
79 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
80  for (CLASS * I : CLASS##s) \
81  delete I;
82 #include "llvm/IR/Metadata.def"
83 
84  // Free the constants.
85  for (auto *I : ExprConstants)
86  I->dropAllReferences();
87  for (auto *I : ArrayConstants)
88  I->dropAllReferences();
89  for (auto *I : StructConstants)
90  I->dropAllReferences();
91  for (auto *I : VectorConstants)
92  I->dropAllReferences();
93  ExprConstants.freeConstants();
94  ArrayConstants.freeConstants();
95  StructConstants.freeConstants();
96  VectorConstants.freeConstants();
97  InlineAsms.freeConstants();
98 
99  CAZConstants.clear();
100  CPNConstants.clear();
101  UVConstants.clear();
103  FPConstants.clear();
104 
105  for (auto &CDSConstant : CDSConstants)
106  delete CDSConstant.second;
107  CDSConstants.clear();
108 
109  // Destroy attributes.
111  E = AttrsSet.end(); I != E; ) {
113  delete &*Elem;
114  }
115 
116  // Destroy attribute lists.
118  E = AttrsLists.end(); I != E; ) {
120  delete &*Elem;
121  }
122 
123  // Destroy attribute node lists.
125  E = AttrsSetNodes.end(); I != E; ) {
127  delete &*Elem;
128  }
129 
130  // Destroy MetadataAsValues.
131  {
133  MDVs.reserve(MetadataAsValues.size());
134  for (auto &Pair : MetadataAsValues)
135  MDVs.push_back(Pair.second);
136  MetadataAsValues.clear();
137  for (auto *V : MDVs)
138  delete V;
139  }
140 
141  // Destroy ValuesAsMetadata.
142  for (auto &Pair : ValuesAsMetadata)
143  delete Pair.second;
144 }
145 
147  bool Changed;
148  do {
149  Changed = false;
150 
151  for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
152  auto *C = *I++;
153  if (C->use_empty()) {
154  Changed = true;
155  C->destroyConstant();
156  }
157  }
158 
159  } while (Changed);
160 }
161 
164 }
165 
166 namespace llvm {
167 /// \brief Make MDOperand transparent for hashing.
168 ///
169 /// This overload of an implementation detail of the hashing library makes
170 /// MDOperand hash to the same value as a \a Metadata pointer.
171 ///
172 /// Note that overloading \a hash_value() as follows:
173 ///
174 /// \code
175 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
176 /// \endcode
177 ///
178 /// does not cause MDOperand to be transparent. In particular, a bare pointer
179 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
180 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
181 }
182 
184  unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
185 #ifndef NDEBUG
186  {
188  unsigned RawHash = calculateHash(MDs);
189  assert(Hash == RawHash &&
190  "Expected hash of MDOperand to equal hash of Metadata*");
191  }
192 #endif
193  return Hash;
194 }
195 
197  return hash_combine_range(Ops.begin(), Ops.end());
198 }
199 
201  uint32_t NewIdx = BundleTagCache.size();
202  return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
203 }
204 
206  Tags.resize(BundleTagCache.size());
207  for (const auto &T : BundleTagCache)
208  Tags[T.second] = T.first();
209 }
210 
212  auto I = BundleTagCache.find(Tag);
213  assert(I != BundleTagCache.end() && "Unknown tag!");
214  return I->second;
215 }
216 
217 // ConstantsContext anchors
218 void UnaryConstantExpr::anchor() { }
219 
220 void BinaryConstantExpr::anchor() { }
221 
222 void SelectConstantExpr::anchor() { }
223 
224 void ExtractElementConstantExpr::anchor() { }
225 
226 void InsertElementConstantExpr::anchor() { }
227 
228 void ShuffleVectorConstantExpr::anchor() { }
229 
230 void ExtractValueConstantExpr::anchor() { }
231 
232 void InsertValueConstantExpr::anchor() { }
233 
234 void GetElementPtrConstantExpr::anchor() { }
235 
236 void CompareConstantExpr::anchor() { }
237 
238 /// Singleton instance of the OptBisect class.
239 ///
240 /// This singleton is accessed via the LLVMContext::getOptBisect() function. It
241 /// provides a mechanism to disable passes and individual optimizations at
242 /// compile time based on a command line option (-opt-bisect-limit) in order to
243 /// perform a bisecting search for optimization-related problems.
244 ///
245 /// Even if multiple LLVMContext objects are created, they will all return the
246 /// same instance of OptBisect in order to provide a single bisect count. Any
247 /// code that uses the OptBisect object should be serialized when bisection is
248 /// enabled in order to enable a consistent bisect count.
250 
252  return *OptBisector;
253 }
static unsigned calculateHash(MDNode *N, unsigned Offset=0)
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:679
LLVMContext & Context
FoldingSet< AttributeImpl > AttrsSet
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
iterator end() const
Definition: ArrayRef.h:130
iterator find(StringRef Key)
Definition: StringMap.h:315
op_iterator op_begin() const
Definition: Metadata.h:1024
Metadata node.
Definition: Metadata.h:830
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:377
void dropTriviallyDeadConstantArrays()
Destroy the ConstantArrays if they are not used.
op_iterator op_end() const
Definition: Metadata.h:1028
MapTy::iterator begin()
Metadata * get() const
Definition: Metadata.h:690
OptBisect & getOptBisect()
Access the object which manages optimization bisection for failure analysis.
FoldingSet< AttributeSetImpl > AttrsLists
This file contains the simple types necessary to represent the attributes associated with functions a...
std::vector< MDNode * > DistinctMDNodes
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: APInt.h:33
ArrayConstantsTy ArrayConstants
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
LLVMContext::YieldCallbackTy YieldCallback
DenseMap< PointerType *, std::unique_ptr< ConstantPointerNull > > CPNConstants
This class implements a mechanism to disable passes and individual optimizations at compile time base...
Definition: OptBisect.h:28
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:48
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef Tag)
unsigned size() const
Definition: StringMap.h:114
StringMap< uint32_t > BundleTagCache
A set of interned tags for operand bundles.
uint32_t Offset
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
iterator begin() const
Definition: ArrayRef.h:129
iterator begin() const
Definition: SmallPtrSet.h:398
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:98
uint32_t getOperandBundleTagID(StringRef Tag) const
VectorConstantsTy VectorConstants
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:50
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:843
Module.h This file contains the declarations for the Module class.
This file declares the interface for bisecting optimizations.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:348
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
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:480
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
DenseMap< Type *, std::unique_ptr< ConstantAggregateZero > > CAZConstants
ConstantUniqueMap< ConstantExpr > ExprConstants
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const Type::TypeID DoubleTyID
StructConstantsTy StructConstants
DenseMap< Type *, std::unique_ptr< UndefValue > > UVConstants
static ManagedStatic< OptBisect > OptBisector
Singleton instance of the OptBisect class.
FoldingSet< AttributeSetNode > AttrsSetNodes
LLVMContext::DiagnosticHandlerTy DiagnosticHandler
void getOperandBundleTags(SmallVectorImpl< StringRef > &Tags) const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:63
StringMap< ConstantDataSequential * > CDSConstants
Root of the metadata hierarchy.
Definition: Metadata.h:55
iterator end()
Definition: StringMap.h:305
IntegerType * Int32Ty
void resize(size_type N)
Definition: SmallVector.h:352