LLVM  5.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/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  : VoidTy(C, Type::VoidTyID),
26  LabelTy(C, Type::LabelTyID),
27  HalfTy(C, Type::HalfTyID),
28  FloatTy(C, Type::FloatTyID),
29  DoubleTy(C, Type::DoubleTyID),
30  MetadataTy(C, Type::MetadataTyID),
31  TokenTy(C, Type::TokenTyID),
32  X86_FP80Ty(C, Type::X86_FP80TyID),
33  FP128Ty(C, Type::FP128TyID),
34  PPC_FP128Ty(C, Type::PPC_FP128TyID),
35  X86_MMXTy(C, Type::X86_MMXTyID),
36  Int1Ty(C, 1),
37  Int8Ty(C, 8),
38  Int16Ty(C, 16),
39  Int32Ty(C, 32),
40  Int64Ty(C, 64),
41  Int128Ty(C, 128) {}
42 
44  // 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();
49 
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();
54 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
55  for (auto *I : CLASS##s) \
56  I->dropAllReferences();
57 #include "llvm/IR/Metadata.def"
58 
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.
66  for (MDNode *I : DistinctMDNodes)
67  I->deleteAsSubclass();
68 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
69  for (CLASS * I : CLASS##s) \
70  delete I;
71 #include "llvm/IR/Metadata.def"
72 
73  // Free the constants.
74  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();
82  ExprConstants.freeConstants();
83  ArrayConstants.freeConstants();
84  StructConstants.freeConstants();
85  VectorConstants.freeConstants();
86  InlineAsms.freeConstants();
87 
88  CAZConstants.clear();
89  CPNConstants.clear();
90  UVConstants.clear();
93 
94  for (auto &CDSConstant : CDSConstants)
95  delete CDSConstant.second;
96  CDSConstants.clear();
97 
98  // Destroy attributes.
100  E = AttrsSet.end(); I != E; ) {
102  delete &*Elem;
103  }
104 
105  // Destroy attribute lists.
107  E = AttrsLists.end();
108  I != E;) {
110  delete &*Elem;
111  }
112 
113  // Destroy attribute node lists.
115  E = AttrsSetNodes.end(); I != E; ) {
117  delete &*Elem;
118  }
119 
120  // Destroy MetadataAsValues.
121  {
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;
134 }
135 
137  bool Changed;
138  do {
139  Changed = false;
140 
141  for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
142  auto *C = *I++;
143  if (C->use_empty()) {
144  Changed = true;
145  C->destroyConstant();
146  }
147  }
148  } while (Changed);
149 }
150 
153 }
154 
155 namespace llvm {
156 
157 /// \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.
170 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
171 
172 } // end namespace llvm
173 
175  unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
176 #ifndef NDEBUG
177  {
179  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 
188  return hash_combine_range(Ops.begin(), Ops.end());
189 }
190 
192  uint32_t NewIdx = BundleTagCache.size();
193  return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
194 }
195 
197  Tags.resize(BundleTagCache.size());
198  for (const auto &T : BundleTagCache)
199  Tags[T.second] = T.first();
200 }
201 
203  auto I = BundleTagCache.find(Tag);
204  assert(I != BundleTagCache.end() && "Unknown tag!");
205  return I->second;
206 }
207 
209  auto NewSSID = SSC.size();
211  "Hit the maximum number of synchronization scopes allowed!");
212  return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
213 }
214 
216  SmallVectorImpl<StringRef> &SSNs) const {
217  SSNs.resize(SSC.size());
218  for (const auto &SSE : SSC)
219  SSNs[SSE.second] = SSE.first();
220 }
221 
222 /// Singleton instance of the OptBisect class.
223 ///
224 /// This singleton is accessed via the LLVMContext::getOptBisect() function. It
225 /// provides a mechanism to disable passes and individual optimizations at
226 /// compile time based on a command line option (-opt-bisect-limit) in order to
227 /// perform a bisecting search for optimization-related problems.
228 ///
229 /// Even if multiple LLVMContext objects are created, they will all return the
230 /// same instance of OptBisect in order to provide a single bisect count. Any
231 /// code that uses the OptBisect object should be serialized when bisection is
232 /// enabled in order to enable a consistent bisect count.
234 
236  return *OptBisector;
237 }
static unsigned calculateHash(MDNode *N, unsigned Offset=0)
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:709
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
FoldingSet< AttributeImpl > AttrsSet
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
iterator end() const
Definition: ArrayRef.h:138
iterator find(StringRef Key)
Definition: StringMap.h:333
op_iterator op_begin() const
Definition: Metadata.h:1057
Metadata node.
Definition: Metadata.h:862
static const Metadata * get_hashable_data(const MDOperand &X)
Make MDOperand transparent for hashing.
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:1061
MapTy::iterator begin()
Metadata * get() const
Definition: Metadata.h:720
OptBisect & getOptBisect()
Access the object which manages optimization bisection for failure analysis.
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
StringMap< SyncScope::ID > SSC
A set of interned synchronization scopes.
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:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef Tag)
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
unsigned size() const
Definition: StringMap.h:111
StringMap< uint32_t > BundleTagCache
A set of interned tags for operand bundles.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
iterator begin() const
Definition: ArrayRef.h:137
iterator begin() const
Definition: SmallPtrSet.h:398
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:92
uint32_t getOperandBundleTagID(StringRef Tag) const
VectorConstantsTy VectorConstants
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:70
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
#define E
Definition: LargeTest.cpp:27
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.
const size_t N
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:480
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID...
#define C
Definition: LargeTest.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
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
void getOperandBundleTags(SmallVectorImpl< StringRef > &Tags) const
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
iterator end()
Definition: StringMap.h:318
IntegerType * Int32Ty
void resize(size_type N)
Definition: SmallVector.h:352