LLVM  9.0.0svn
LLVMContextImpl.cpp
Go to the documentation of this file.
1 //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the opaque LLVMContextImpl.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "LLVMContextImpl.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/OptBisect.h"
16 #include "llvm/IR/Type.h"
18 #include <cassert>
19 #include <utility>
20 
21 using namespace llvm;
22 
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 #ifndef NDEBUG
51  // Check for metadata references from leaked Instructions.
52  for (auto &Pair : InstructionMetadata)
53  Pair.first->dump();
54  assert(InstructionMetadata.empty() &&
55  "Instructions with metadata have been leaked");
56 #endif
57 
58  // Drop references for MDNodes. Do this before Values get deleted to avoid
59  // unnecessary RAUW when nodes are still unresolved.
60  for (auto *I : DistinctMDNodes)
61  I->dropAllReferences();
62 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
63  for (auto *I : CLASS##s) \
64  I->dropAllReferences();
65 #include "llvm/IR/Metadata.def"
66 
67  // Also drop references that come from the Value bridges.
68  for (auto &Pair : ValuesAsMetadata)
69  Pair.second->dropUsers();
70  for (auto &Pair : MetadataAsValues)
71  Pair.second->dropUse();
72 
73  // Destroy MDNodes.
74  for (MDNode *I : DistinctMDNodes)
75  I->deleteAsSubclass();
76 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
77  for (CLASS * I : CLASS##s) \
78  delete I;
79 #include "llvm/IR/Metadata.def"
80 
81  // Free the constants.
82  for (auto *I : ExprConstants)
83  I->dropAllReferences();
84  for (auto *I : ArrayConstants)
85  I->dropAllReferences();
86  for (auto *I : StructConstants)
87  I->dropAllReferences();
88  for (auto *I : VectorConstants)
89  I->dropAllReferences();
90  ExprConstants.freeConstants();
91  ArrayConstants.freeConstants();
92  StructConstants.freeConstants();
93  VectorConstants.freeConstants();
94  InlineAsms.freeConstants();
95 
96  CAZConstants.clear();
97  CPNConstants.clear();
98  UVConstants.clear();
100  FPConstants.clear();
101 
102  for (auto &CDSConstant : CDSConstants)
103  delete CDSConstant.second;
104  CDSConstants.clear();
105 
106  // Destroy attributes.
108  E = AttrsSet.end(); I != E; ) {
110  delete &*Elem;
111  }
112 
113  // Destroy attribute lists.
115  E = AttrsLists.end();
116  I != E;) {
118  delete &*Elem;
119  }
120 
121  // Destroy attribute node lists.
123  E = AttrsSetNodes.end(); I != E; ) {
125  delete &*Elem;
126  }
127 
128  // Destroy MetadataAsValues.
129  {
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;
142 }
143 
145  bool Changed;
146  do {
147  Changed = false;
148 
149  for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
150  auto *C = *I++;
151  if (C->use_empty()) {
152  Changed = true;
153  C->destroyConstant();
154  }
155  }
156  } while (Changed);
157 }
158 
161 }
162 
163 namespace llvm {
164 
165 /// 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.
178 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
179 
180 } // end namespace llvm
181 
183  unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
184 #ifndef NDEBUG
185  {
187  unsigned RawHash = calculateHash(MDs);
188  assert(Hash == RawHash &&
189  "Expected hash of MDOperand to equal hash of Metadata*");
190  }
191 #endif
192  return Hash;
193 }
194 
196  return hash_combine_range(Ops.begin(), Ops.end());
197 }
198 
200  uint32_t NewIdx = BundleTagCache.size();
201  return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
202 }
203 
205  Tags.resize(BundleTagCache.size());
206  for (const auto &T : BundleTagCache)
207  Tags[T.second] = T.first();
208 }
209 
211  auto I = BundleTagCache.find(Tag);
212  assert(I != BundleTagCache.end() && "Unknown tag!");
213  return I->second;
214 }
215 
217  auto NewSSID = SSC.size();
219  "Hit the maximum number of synchronization scopes allowed!");
220  return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
221 }
222 
224  SmallVectorImpl<StringRef> &SSNs) const {
225  SSNs.resize(SSC.size());
226  for (const auto &SSE : SSC)
227  SSNs[SSE.second] = SSE.first();
228 }
229 
230 /// Singleton instance of the OptBisect class.
231 ///
232 /// This singleton is accessed via the LLVMContext::getOptPassGate() function.
233 /// It provides a mechanism to disable passes and individual optimizations at
234 /// compile time based on a command line option (-opt-bisect-limit) in order to
235 /// perform a bisecting search for optimization-related problems.
236 ///
237 /// Even if multiple LLVMContext objects are created, they will all return the
238 /// same instance of OptBisect in order to provide a single bisect count. Any
239 /// code that uses the OptBisect object should be serialized when bisection is
240 /// enabled in order to enable a consistent bisect count.
242 
244  if (!OPG)
245  OPG = &(*OptBisector);
246  return *OPG;
247 }
248 
250  this->OPG = &OPG;
251 }
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:710
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
This class represents lattice values for constants.
Definition: AllocatorList.h:23
iterator begin() const
Definition: ArrayRef.h:136
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition: OptBisect.h:25
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
uint32_t getOperandBundleTagID(StringRef Tag) const
iterator find(StringRef Key)
Definition: StringMap.h:332
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
Metadata node.
Definition: Metadata.h:863
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:1405
void reserve(size_type N)
Definition: SmallVector.h:369
void dropTriviallyDeadConstantArrays()
Destroy the ConstantArrays if they are not used.
op_iterator op_end() const
Definition: Metadata.h:1062
MapTy::iterator begin()
DenseMap< const Instruction *, MDAttachmentMap > InstructionMetadata
Collection of per-instruction metadata used in this context.
unsigned size() const
Definition: StringMap.h:111
std::vector< MDNode * > DistinctMDNodes
static void DiagHandler(const SMDiagnostic &Diag, void *Context)
Definition: TextStub.cpp:611
ConstantUniqueMap< InlineAsm > InlineAsms
op_iterator op_begin() const
Definition: Metadata.h:1058
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:32
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:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef Tag)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:91
StringMap< uint32_t > BundleTagCache
A set of interned tags for operand bundles.
VectorConstantsTy VectorConstants
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:66
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:837
Module.h This file contains the declarations for the Module class.
iterator end() const
Definition: ArrayRef.h:137
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:370
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:478
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID...
iterator begin() const
Definition: SmallPtrSet.h:396
#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:721
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:83
StringMap< ConstantDataSequential * > CDSConstants
Root of the metadata hierarchy.
Definition: Metadata.h:57
bool use_empty() const
Definition: Value.h:322
iterator end()
Definition: StringMap.h:317
IntegerType * Int32Ty
void resize(size_type N)
Definition: SmallVector.h:344