LLVM  8.0.0svn
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/IR/Module.h"
16 #include "llvm/IR/OptBisect.h"
17 #include "llvm/IR/Type.h"
19 #include <cassert>
20 #include <utility>
21 
22 using namespace llvm;
23 
25  : DiagHandler(llvm::make_unique<DiagnosticHandler>()),
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 
45  // 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();
50 
51 #ifndef NDEBUG
52  // Check for metadata references from leaked Instructions.
53  for (auto &Pair : InstructionMetadata)
54  Pair.first->dump();
55  assert(InstructionMetadata.empty() &&
56  "Instructions with metadata have been leaked");
57 #endif
58 
59  // Drop references for MDNodes. Do this before Values get deleted to avoid
60  // unnecessary RAUW when nodes are still unresolved.
61  for (auto *I : DistinctMDNodes)
62  I->dropAllReferences();
63 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
64  for (auto *I : CLASS##s) \
65  I->dropAllReferences();
66 #include "llvm/IR/Metadata.def"
67 
68  // Also drop references that come from the Value bridges.
69  for (auto &Pair : ValuesAsMetadata)
70  Pair.second->dropUsers();
71  for (auto &Pair : MetadataAsValues)
72  Pair.second->dropUse();
73 
74  // Destroy MDNodes.
75  for (MDNode *I : DistinctMDNodes)
76  I->deleteAsSubclass();
77 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
78  for (CLASS * I : CLASS##s) \
79  delete I;
80 #include "llvm/IR/Metadata.def"
81 
82  // Free the constants.
83  for (auto *I : ExprConstants)
84  I->dropAllReferences();
85  for (auto *I : ArrayConstants)
86  I->dropAllReferences();
87  for (auto *I : StructConstants)
88  I->dropAllReferences();
89  for (auto *I : VectorConstants)
90  I->dropAllReferences();
91  ExprConstants.freeConstants();
92  ArrayConstants.freeConstants();
93  StructConstants.freeConstants();
94  VectorConstants.freeConstants();
95  InlineAsms.freeConstants();
96 
97  CAZConstants.clear();
98  CPNConstants.clear();
99  UVConstants.clear();
101  FPConstants.clear();
102 
103  for (auto &CDSConstant : CDSConstants)
104  delete CDSConstant.second;
105  CDSConstants.clear();
106 
107  // Destroy attributes.
109  E = AttrsSet.end(); I != E; ) {
111  delete &*Elem;
112  }
113 
114  // Destroy attribute lists.
116  E = AttrsLists.end();
117  I != E;) {
119  delete &*Elem;
120  }
121 
122  // Destroy attribute node lists.
124  E = AttrsSetNodes.end(); I != E; ) {
126  delete &*Elem;
127  }
128 
129  // Destroy MetadataAsValues.
130  {
132  MDVs.reserve(MetadataAsValues.size());
133  for (auto &Pair : MetadataAsValues)
134  MDVs.push_back(Pair.second);
135  MetadataAsValues.clear();
136  for (auto *V : MDVs)
137  delete V;
138  }
139 
140  // Destroy ValuesAsMetadata.
141  for (auto &Pair : ValuesAsMetadata)
142  delete Pair.second;
143 }
144 
146  bool Changed;
147  do {
148  Changed = false;
149 
150  for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
151  auto *C = *I++;
152  if (C->use_empty()) {
153  Changed = true;
154  C->destroyConstant();
155  }
156  }
157  } while (Changed);
158 }
159 
162 }
163 
164 namespace llvm {
165 
166 /// Make MDOperand transparent for hashing.
167 ///
168 /// This overload of an implementation detail of the hashing library makes
169 /// MDOperand hash to the same value as a \a Metadata pointer.
170 ///
171 /// Note that overloading \a hash_value() as follows:
172 ///
173 /// \code
174 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
175 /// \endcode
176 ///
177 /// does not cause MDOperand to be transparent. In particular, a bare pointer
178 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
179 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
180 
181 } // end namespace llvm
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 
218  auto NewSSID = SSC.size();
220  "Hit the maximum number of synchronization scopes allowed!");
221  return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
222 }
223 
225  SmallVectorImpl<StringRef> &SSNs) const {
226  SSNs.resize(SSC.size());
227  for (const auto &SSE : SSC)
228  SSNs[SSE.second] = SSE.first();
229 }
230 
231 /// Singleton instance of the OptBisect class.
232 ///
233 /// This singleton is accessed via the LLVMContext::getOptPassGate() function.
234 /// It provides a mechanism to disable passes and individual optimizations at
235 /// compile time based on a command line option (-opt-bisect-limit) in order to
236 /// perform a bisecting search for optimization-related problems.
237 ///
238 /// Even if multiple LLVMContext objects are created, they will all return the
239 /// same instance of OptBisect in order to provide a single bisect count. Any
240 /// code that uses the OptBisect object should be serialized when bisection is
241 /// enabled in order to enable a consistent bisect count.
243 
245  if (!OPG)
246  OPG = &(*OptBisector);
247  return *OPG;
248 }
249 
251  this->OPG = &OPG;
252 }
static unsigned calculateHash(MDNode *N, unsigned Offset=0)
uint64_t CallInst * C
This is the base class for diagnostic handling in LLVM.
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:711
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
void getOperandBundleTags(SmallVectorImpl< StringRef > &Tags) const
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
LLVMContext & Context
FoldingSet< AttributeImpl > AttrsSet
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
iterator begin() const
Definition: ArrayRef.h:137
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition: OptBisect.h:32
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
uint32_t getOperandBundleTagID(StringRef Tag) const
iterator find(StringRef Key)
Definition: StringMap.h:333
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
Metadata node.
Definition: Metadata.h:864
static const Metadata * get_hashable_data(const MDOperand &X)
Make MDOperand transparent for hashing.
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&... args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:1205
void reserve(size_type N)
Definition: SmallVector.h:376
void dropTriviallyDeadConstantArrays()
Destroy the ConstantArrays if they are not used.
op_iterator op_end() const
Definition: Metadata.h:1063
MapTy::iterator begin()
DenseMap< const Instruction *, MDAttachmentMap > InstructionMetadata
Collection of per-instruction metadata used in this context.
unsigned size() const
Definition: StringMap.h:112
std::vector< MDNode * > DistinctMDNodes
ConstantUniqueMap< InlineAsm > InlineAsms
op_iterator op_begin() const
Definition: Metadata.h:1059
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
StringMap< SyncScope::ID > SSC
A set of interned synchronization scopes.
DenseMap< PointerType *, std::unique_ptr< ConstantPointerNull > > CPNConstants
void setOptPassGate(OptPassGate &)
Set the object which can disable optional passes and individual optimizations at compile time...
OptPassGate & getOptPassGate() const
Access the object which can disable optional passes and individual optimizations at compile time...
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef Tag)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:92
StringMap< uint32_t > BundleTagCache
A set of interned tags for operand bundles.
VectorConstantsTy VectorConstants
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:71
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
void dropTriviallyDeadConstantArrays()
Destroy ConstantArrays in LLVMContext if they are not used.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Module.h This file contains the declarations for the Module class.
iterator end() const
Definition: ArrayRef.h:138
FoldingSet< AttributeListImpl > AttrsLists
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:366
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:479
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID...
iterator begin() const
Definition: SmallPtrSet.h:397
#define I(x, y, z)
Definition: MD5.cpp:58
#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
Metadata * get() const
Definition: Metadata.h:722
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61
StringMap< ConstantDataSequential * > CDSConstants
Root of the metadata hierarchy.
Definition: Metadata.h:58
bool use_empty() const
Definition: Value.h:323
iterator end()
Definition: StringMap.h:318
IntegerType * Int32Ty
void resize(size_type N)
Definition: SmallVector.h:351