LLVM  13.0.0git
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/ADT/SetVector.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(std::make_unique<DiagnosticHandler>()),
26  VoidTy(C, Type::VoidTyID),
27  LabelTy(C, Type::LabelTyID),
28  HalfTy(C, Type::HalfTyID),
29  BFloatTy(C, Type::BFloatTyID),
30  FloatTy(C, Type::FloatTyID),
31  DoubleTy(C, Type::DoubleTyID),
32  MetadataTy(C, Type::MetadataTyID),
33  TokenTy(C, Type::TokenTyID),
34  X86_FP80Ty(C, Type::X86_FP80TyID),
35  FP128Ty(C, Type::FP128TyID),
36  PPC_FP128Ty(C, Type::PPC_FP128TyID),
37  X86_MMXTy(C, Type::X86_MMXTyID),
38  X86_AMXTy(C, Type::X86_AMXTyID),
39  Int1Ty(C, 1),
40  Int8Ty(C, 8),
41  Int16Ty(C, 16),
42  Int32Ty(C, 32),
43  Int64Ty(C, 64),
44  Int128Ty(C, 128) {}
45 
47  // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
48  // will call LLVMContextImpl::removeModule, thus invalidating iterators into
49  // the container. Avoid iterators during this operation:
50  while (!OwnedModules.empty())
51  delete *OwnedModules.begin();
52 
53 #ifndef NDEBUG
54  // Check for metadata references from leaked Values.
55  for (auto &Pair : ValueMetadata)
56  Pair.first->dump();
57  assert(ValueMetadata.empty() && "Values with metadata have been leaked");
58 #endif
59 
60  // Drop references for MDNodes. Do this before Values get deleted to avoid
61  // unnecessary RAUW when nodes are still unresolved.
62  for (auto *I : DistinctMDNodes)
63  I->dropAllReferences();
64 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
65  for (auto *I : CLASS##s) \
66  I->dropAllReferences();
67 #include "llvm/IR/Metadata.def"
68 
69  // Also drop references that come from the Value bridges.
70  for (auto &Pair : ValuesAsMetadata)
71  Pair.second->dropUsers();
72  for (auto &Pair : MetadataAsValues)
73  Pair.second->dropUse();
74 
75  // Destroy MDNodes.
76  for (MDNode *I : DistinctMDNodes)
77  I->deleteAsSubclass();
78 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
79  for (CLASS * I : CLASS##s) \
80  delete I;
81 #include "llvm/IR/Metadata.def"
82 
83  // Free the constants.
84  for (auto *I : ExprConstants)
85  I->dropAllReferences();
86  for (auto *I : ArrayConstants)
87  I->dropAllReferences();
88  for (auto *I : StructConstants)
89  I->dropAllReferences();
90  for (auto *I : VectorConstants)
91  I->dropAllReferences();
92  ExprConstants.freeConstants();
96  InlineAsms.freeConstants();
97 
98  CAZConstants.clear();
99  CPNConstants.clear();
100  UVConstants.clear();
101  PVConstants.clear();
103  FPConstants.clear();
104  CDSConstants.clear();
105 
106  // Destroy attribute node lists.
108  E = AttrsSetNodes.end(); I != E; ) {
110  delete &*Elem;
111  }
112 
113  // Destroy MetadataAsValues.
114  {
116  MDVs.reserve(MetadataAsValues.size());
117  for (auto &Pair : MetadataAsValues)
118  MDVs.push_back(Pair.second);
119  MetadataAsValues.clear();
120  for (auto *V : MDVs)
121  delete V;
122  }
123 
124  // Destroy ValuesAsMetadata.
125  for (auto &Pair : ValuesAsMetadata)
126  delete Pair.second;
127 }
128 
131 
132  // When ArrayConstants are of substantial size and only a few in them are
133  // dead, starting WorkList with all elements of ArrayConstants can be
134  // wasteful. Instead, starting WorkList with only elements that have empty
135  // uses.
137  if (C->use_empty())
138  WorkList.insert(C);
139 
140  while (!WorkList.empty()) {
141  ConstantArray *C = WorkList.pop_back_val();
142  if (C->use_empty()) {
143  for (const Use &Op : C->operands()) {
144  if (auto *COp = dyn_cast<ConstantArray>(Op))
145  WorkList.insert(COp);
146  }
147  C->destroyConstant();
148  }
149  }
150 }
151 
154 }
155 
156 namespace llvm {
157 
158 /// Make MDOperand transparent for hashing.
159 ///
160 /// This overload of an implementation detail of the hashing library makes
161 /// MDOperand hash to the same value as a \a Metadata pointer.
162 ///
163 /// Note that overloading \a hash_value() as follows:
164 ///
165 /// \code
166 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
167 /// \endcode
168 ///
169 /// does not cause MDOperand to be transparent. In particular, a bare pointer
170 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
171 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
172 
173 } // end namespace llvm
174 
176  unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
177 #ifndef NDEBUG
178  {
179  SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset));
180  unsigned RawHash = calculateHash(MDs);
181  assert(Hash == RawHash &&
182  "Expected hash of MDOperand to equal hash of Metadata*");
183  }
184 #endif
185  return Hash;
186 }
187 
189  return hash_combine_range(Ops.begin(), Ops.end());
190 }
191 
193  uint32_t NewIdx = BundleTagCache.size();
194  return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
195 }
196 
198  Tags.resize(BundleTagCache.size());
199  for (const auto &T : BundleTagCache)
200  Tags[T.second] = T.first();
201 }
202 
204  auto I = BundleTagCache.find(Tag);
205  assert(I != BundleTagCache.end() && "Unknown tag!");
206  return I->second;
207 }
208 
210  auto NewSSID = SSC.size();
212  "Hit the maximum number of synchronization scopes allowed!");
213  return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
214 }
215 
217  SmallVectorImpl<StringRef> &SSNs) const {
218  SSNs.resize(SSC.size());
219  for (const auto &SSE : SSC)
220  SSNs[SSE.second] = SSE.first();
221 }
222 
223 /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
224 /// singleton OptBisect if not explicitly set.
226  if (!OPG)
227  OPG = &(*OptBisector);
228  return *OPG;
229 }
230 
232  this->OPG = &OPG;
233 }
llvm::LLVMContextImpl::OwnedModules
SmallPtrSet< Module *, 4 > OwnedModules
OwnedModules - The set of modules instantiated in this context, and which will be automatically delet...
Definition: LLVMContextImpl.h:1328
Int32Ty
IntegerType * Int32Ty
Definition: NVVMIntrRange.cpp:67
llvm::LLVMContext::pImpl
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:70
llvm::LLVMContextImpl::InlineAsms
ConstantUniqueMap< InlineAsm > InlineAsms
Definition: LLVMContextImpl.h:1421
llvm
Definition: AllocatorList.h:23
FloatTyID
const Type::TypeID FloatTyID
Definition: Mips16HardFloat.cpp:103
llvm::drop_begin
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:266
llvm::StringMapEntry< uint32_t >
llvm::LLVMContextImpl::FPConstants
FPMapTy FPConstants
Definition: LLVMContextImpl.h:1370
llvm::LLVMContextImpl::UVConstants
DenseMap< Type *, std::unique_ptr< UndefValue > > UVConstants
Definition: LLVMContextImpl.h:1408
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
ManagedStatic.h
llvm::LLVMContextImpl::ExprConstants
ConstantUniqueMap< ConstantExpr > ExprConstants
Definition: LLVMContextImpl.h:1419
llvm::LLVMContextImpl::setOptPassGate
void setOptPassGate(OptPassGate &)
Set the object which can disable optional passes and individual optimizations at compile time.
Definition: LLVMContextImpl.cpp:231
llvm::StringMap::end
iterator end()
Definition: StringMap.h:205
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
Module.h
llvm::DiagnosticHandler
This is the base class for diagnostic handling in LLVM.
Definition: DiagnosticHandler.h:24
OptBisect.h
T
#define T
Definition: Mips16ISelLowering.cpp:341
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::LLVMContextImpl::ValuesAsMetadata
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
Definition: LLVMContextImpl.h:1377
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:218
llvm::LLVMContextImpl::~LLVMContextImpl
~LLVMContextImpl()
Definition: LLVMContextImpl.cpp:46
llvm::LLVMContextImpl::getSyncScopeNames
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
Definition: LLVMContextImpl.cpp:216
llvm::LLVMContextImpl::ArrayConstants
ArrayConstantsTy ArrayConstants
Definition: LLVMContextImpl.h:1398
llvm::VFISAKind::SSE
@ SSE
llvm::LLVMContextImpl::dropTriviallyDeadConstantArrays
void dropTriviallyDeadConstantArrays()
Destroy the ConstantArrays if they are not used.
Definition: LLVMContextImpl.cpp:129
llvm::FoldingSetIterator
Definition: FoldingSet.h:367
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::LLVMContextImpl::getOperandBundleTagID
uint32_t getOperandBundleTagID(StringRef Tag) const
Definition: LLVMContextImpl.cpp:203
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::StringMap::insert
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:276
llvm::LLVMContextImpl::CAZConstants
DenseMap< Type *, std::unique_ptr< ConstantAggregateZero > > CAZConstants
Definition: LLVMContextImpl.h:1395
llvm::OptPassGate
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition: OptBisect.h:26
llvm::LLVMContextImpl::getOrInsertBundleTag
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef Tag)
Definition: LLVMContextImpl.cpp:192
llvm::ConstantArray
ConstantArray - Constant Array Declarations.
Definition: Constants.h:407
LLVMContextImpl.h
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:606
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::LLVMContextImpl::CDSConstants
StringMap< std::unique_ptr< ConstantDataSequential > > CDSConstants
Definition: LLVMContextImpl.h:1412
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::empty
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:72
Type.h
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
DoubleTyID
const Type::TypeID DoubleTyID
Definition: Mips16HardFloat.cpp:104
llvm::LLVMContextImpl::BundleTagCache
StringMap< uint32_t > BundleTagCache
A set of interned tags for operand bundles.
Definition: LLVMContextImpl.h:1478
llvm::LLVMContextImpl::ValueMetadata
DenseMap< const Value *, MDAttachments > ValueMetadata
Collection of metadata used in this context.
Definition: LLVMContextImpl.h:1461
llvm::LLVMContextImpl::CPNConstants
DenseMap< PointerType *, std::unique_ptr< ConstantPointerNull > > CPNConstants
Definition: LLVMContextImpl.h:1406
llvm::DenseMapBase::clear
void clear()
Definition: DenseMap.h:111
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::SyncScope::ID
uint8_t ID
Definition: LLVMContext.h:47
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::LLVMContextImpl::getOperandBundleTags
void getOperandBundleTags(SmallVectorImpl< StringRef > &Tags) const
Definition: LLVMContextImpl.cpp:197
llvm::SmallPtrSetImpl::begin
iterator begin() const
Definition: SmallPtrSet.h:402
llvm::LLVMContextImpl::OPG
OptPassGate * OPG
Definition: LLVMContextImpl.h:1515
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MDNode
Metadata node.
Definition: Metadata.h:897
llvm::StringMapImpl::size
unsigned size() const
Definition: StringMap.h:93
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
llvm::LLVMContextImpl::IntConstants
IntMapTy IntConstants
Definition: LLVMContextImpl.h:1366
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
DiagHandler
static void DiagHandler(const SMDiagnostic &Diag, void *Context)
Definition: TextStub.cpp:1095
llvm::LLVMContextImpl::getOptPassGate
OptPassGate & getOptPassGate() const
Access the object which can disable optional passes and individual optimizations at compile time.
Definition: LLVMContextImpl.cpp:225
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
uint32_t
llvm::LLVMContextImpl::StructConstants
StructConstantsTy StructConstants
Definition: LLVMContextImpl.h:1401
llvm::LLVMContextImpl::getOrInsertSyncScopeID
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID.
Definition: LLVMContextImpl.cpp:209
llvm::LLVMContextImpl::PVConstants
DenseMap< Type *, std::unique_ptr< PoisonValue > > PVConstants
Definition: LLVMContextImpl.h:1410
std
Definition: BitVector.h:838
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:314
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:151
llvm::LLVMContextImpl::AttrsSetNodes
FoldingSet< AttributeSetNode > AttrsSetNodes
Definition: LLVMContextImpl.h:1374
llvm::LLVMContextImpl::MetadataAsValues
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
Definition: LLVMContextImpl.h:1378
llvm::Module::dropTriviallyDeadConstantArrays
void dropTriviallyDeadConstantArrays()
Destroy ConstantArrays in LLVMContext if they are not used.
Definition: LLVMContextImpl.cpp:152
llvm::SmallPtrSetImplBase::empty
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:91
N
#define N
llvm::hash_combine_range
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition: Hashing.h:482
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::LLVMContextImpl::SSC
StringMap< SyncScope::ID > SSC
A set of interned synchronization scopes.
Definition: LLVMContextImpl.h:1486
llvm::SmallVectorImpl< StringRef >
llvm::LLVMContextImpl::VectorConstants
VectorConstantsTy VectorConstants
Definition: LLVMContextImpl.h:1404
llvm::SmallSetVector
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:307
llvm::MDNodeOpsKey::calculateHash
static unsigned calculateHash(MDNode *N, unsigned Offset=0)
Definition: LLVMContextImpl.cpp:175
llvm::LLVMContextImpl::DistinctMDNodes
std::vector< MDNode * > DistinctMDNodes
Definition: LLVMContextImpl.h:1393
llvm::get_hashable_data
static const Metadata * get_hashable_data(const MDOperand &X)
Make MDOperand transparent for hashing.
Definition: LLVMContextImpl.cpp:171
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:624
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::pop_back_val
LLVM_NODISCARD T pop_back_val()
Definition: SetVector.h:232
llvm::LLVMContextImpl::LLVMContextImpl
LLVMContextImpl(LLVMContext &C)
Definition: LLVMContextImpl.cpp:24
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:152
llvm::ConstantUniqueMap::freeConstants
void freeConstants()
Definition: ConstantsContext.h:708
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:744
SetVector.h
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44