LLVM  14.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"
20 #include <cassert>
21 #include <utility>
22 
23 using namespace llvm;
24 
25 static cl::opt<bool>
26  OpaquePointersCL("opaque-pointers", cl::desc("Use opaque pointers"),
27  cl::init(false));
28 
30  : DiagHandler(std::make_unique<DiagnosticHandler>()),
31  VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID),
32  HalfTy(C, Type::HalfTyID), BFloatTy(C, Type::BFloatTyID),
33  FloatTy(C, Type::FloatTyID), DoubleTy(C, Type::DoubleTyID),
34  MetadataTy(C, Type::MetadataTyID), TokenTy(C, Type::TokenTyID),
35  X86_FP80Ty(C, Type::X86_FP80TyID), FP128Ty(C, Type::FP128TyID),
36  PPC_FP128Ty(C, Type::PPC_FP128TyID), X86_MMXTy(C, Type::X86_MMXTyID),
37  X86_AMXTy(C, Type::X86_AMXTyID), Int1Ty(C, 1), Int8Ty(C, 8),
38  Int16Ty(C, 16), Int32Ty(C, 32), Int64Ty(C, 64), Int128Ty(C, 128) {}
39 
41  // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
42  // will call LLVMContextImpl::removeModule, thus invalidating iterators into
43  // the container. Avoid iterators during this operation:
44  while (!OwnedModules.empty())
45  delete *OwnedModules.begin();
46 
47 #ifndef NDEBUG
48  // Check for metadata references from leaked Values.
49  for (auto &Pair : ValueMetadata)
50  Pair.first->dump();
51  assert(ValueMetadata.empty() && "Values with metadata have been leaked");
52 #endif
53 
54  // Drop references for MDNodes. Do this before Values get deleted to avoid
55  // unnecessary RAUW when nodes are still unresolved.
56  for (auto *I : DistinctMDNodes) {
57  // We may have DIArgList that were uniqued, and as it has a custom
58  // implementation of dropAllReferences, it needs to be explicitly invoked.
59  if (auto *AL = dyn_cast<DIArgList>(I)) {
60  AL->dropAllReferences();
61  continue;
62  }
63  I->dropAllReferences();
64  }
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();
97  InlineAsms.freeConstants();
98 
99  CAZConstants.clear();
100  CPNConstants.clear();
101  UVConstants.clear();
102  PVConstants.clear();
104  FPConstants.clear();
105  CDSConstants.clear();
106 
107  // Destroy attribute node lists.
109  E = AttrsSetNodes.end(); I != E; ) {
111  delete &*Elem;
112  }
113 
114  // Destroy MetadataAsValues.
115  {
117  MDVs.reserve(MetadataAsValues.size());
118  for (auto &Pair : MetadataAsValues)
119  MDVs.push_back(Pair.second);
120  MetadataAsValues.clear();
121  for (auto *V : MDVs)
122  delete V;
123  }
124 
125  // Destroy ValuesAsMetadata.
126  for (auto &Pair : ValuesAsMetadata)
127  delete Pair.second;
128 }
129 
132 
133  // When ArrayConstants are of substantial size and only a few in them are
134  // dead, starting WorkList with all elements of ArrayConstants can be
135  // wasteful. Instead, starting WorkList with only elements that have empty
136  // uses.
138  if (C->use_empty())
139  WorkList.insert(C);
140 
141  while (!WorkList.empty()) {
142  ConstantArray *C = WorkList.pop_back_val();
143  if (C->use_empty()) {
144  for (const Use &Op : C->operands()) {
145  if (auto *COp = dyn_cast<ConstantArray>(Op))
146  WorkList.insert(COp);
147  }
148  C->destroyConstant();
149  }
150  }
151 }
152 
155 }
156 
157 namespace llvm {
158 
159 /// Make MDOperand transparent for hashing.
160 ///
161 /// This overload of an implementation detail of the hashing library makes
162 /// MDOperand hash to the same value as a \a Metadata pointer.
163 ///
164 /// Note that overloading \a hash_value() as follows:
165 ///
166 /// \code
167 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
168 /// \endcode
169 ///
170 /// does not cause MDOperand to be transparent. In particular, a bare pointer
171 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
172 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
173 
174 } // end namespace llvm
175 
177  unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
178 #ifndef NDEBUG
179  {
180  SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset));
181  unsigned RawHash = calculateHash(MDs);
182  assert(Hash == RawHash &&
183  "Expected hash of MDOperand to equal hash of Metadata*");
184  }
185 #endif
186  return Hash;
187 }
188 
190  return hash_combine_range(Ops.begin(), Ops.end());
191 }
192 
194  uint32_t NewIdx = BundleTagCache.size();
195  return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
196 }
197 
199  Tags.resize(BundleTagCache.size());
200  for (const auto &T : BundleTagCache)
201  Tags[T.second] = T.first();
202 }
203 
205  auto I = BundleTagCache.find(Tag);
206  assert(I != BundleTagCache.end() && "Unknown tag!");
207  return I->second;
208 }
209 
211  auto NewSSID = SSC.size();
213  "Hit the maximum number of synchronization scopes allowed!");
214  return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
215 }
216 
218  SmallVectorImpl<StringRef> &SSNs) const {
219  SSNs.resize(SSC.size());
220  for (const auto &SSE : SSC)
221  SSNs[SSE.second] = SSE.first();
222 }
223 
224 /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
225 /// singleton OptBisect if not explicitly set.
227  if (!OPG)
228  OPG = &(*OptBisector);
229  return *OPG;
230 }
231 
233  this->OPG = &OPG;
234 }
235 
237  if (LLVM_UNLIKELY(!(OpaquePointers.hasValue())))
238  OpaquePointers = OpaquePointersCL;
239  return *OpaquePointers;
240 }
241 
242 void LLVMContextImpl::setOpaquePointers(bool OP) { OpaquePointers = OP; }
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:1434
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:22
FloatTyID
const Type::TypeID FloatTyID
Definition: Mips16HardFloat.cpp:103
llvm::AArch64CC::AL
@ AL
Definition: AArch64BaseInfo.h:269
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:264
llvm::StringMapEntry< uint32_t >
llvm::LLVMContextImpl::FPConstants
FPMapTy FPConstants
Definition: LLVMContextImpl.h:1381
T
llvm::LLVMContextImpl::UVConstants
DenseMap< Type *, std::unique_ptr< UndefValue > > UVConstants
Definition: LLVMContextImpl.h:1419
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1176
ManagedStatic.h
llvm::LLVMContextImpl::ExprConstants
ConstantUniqueMap< ConstantExpr > ExprConstants
Definition: LLVMContextImpl.h:1432
llvm::LLVMContextImpl::setOptPassGate
void setOptPassGate(OptPassGate &)
Set the object which can disable optional passes and individual optimizations at compile time.
Definition: LLVMContextImpl.cpp:232
llvm::StringMap::end
iterator end()
Definition: StringMap.h:204
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::DiagnosticHandler
This is the base class for diagnostic handling in LLVM.
Definition: DiagnosticHandler.h:24
OptBisect.h
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:80
llvm::LLVMContextImpl::ValuesAsMetadata
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
Definition: LLVMContextImpl.h:1388
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:217
llvm::LLVMContextImpl::~LLVMContextImpl
~LLVMContextImpl()
Definition: LLVMContextImpl.cpp:40
llvm::LLVMContextImpl::getSyncScopeNames
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
Definition: LLVMContextImpl.cpp:217
llvm::LLVMContextImpl::ArrayConstants
ArrayConstantsTy ArrayConstants
Definition: LLVMContextImpl.h:1409
llvm::Optional::hasValue
constexpr bool hasValue() const
Definition: Optional.h:288
llvm::VFISAKind::SSE
@ SSE
CommandLine.h
llvm::LLVMContextImpl::dropTriviallyDeadConstantArrays
void dropTriviallyDeadConstantArrays()
Destroy the ConstantArrays if they are not used.
Definition: LLVMContextImpl.cpp:130
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:204
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:275
llvm::LLVMContextImpl::CAZConstants
DenseMap< Type *, std::unique_ptr< ConstantAggregateZero > > CAZConstants
Definition: LLVMContextImpl.h:1406
llvm::OptPassGate
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition: OptBisect.h:27
llvm::LLVMContextImpl::getOrInsertBundleTag
StringMapEntry< uint32_t > * getOrInsertBundleTag(StringRef Tag)
Definition: LLVMContextImpl.cpp:193
llvm::ConstantArray
ConstantArray - Constant Array Declarations.
Definition: Constants.h:409
LLVMContextImpl.h
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:1339
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:608
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::LLVMContextImpl::CDSConstants
StringMap< std::unique_ptr< ConstantDataSequential > > CDSConstants
Definition: LLVMContextImpl.h:1423
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::empty
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:71
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:1491
llvm::LLVMContextImpl::ValueMetadata
DenseMap< const Value *, MDAttachments > ValueMetadata
Collection of metadata used in this context.
Definition: LLVMContextImpl.h:1474
llvm::cl::opt< bool >
llvm::LLVMContextImpl::setOpaquePointers
void setOpaquePointers(bool OP)
Definition: LLVMContextImpl.cpp:242
llvm::LLVMContextImpl::CPNConstants
DenseMap< PointerType *, std::unique_ptr< ConstantPointerNull > > CPNConstants
Definition: LLVMContextImpl.h:1417
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:58
llvm::LLVMContextImpl::getOperandBundleTags
void getOperandBundleTags(SmallVectorImpl< StringRef > &Tags) const
Definition: LLVMContextImpl.cpp:198
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:441
llvm::SmallPtrSetImpl::begin
iterator begin() const
Definition: SmallPtrSet.h:402
llvm::LLVMContextImpl::OPG
OptPassGate * OPG
Definition: LLVMContextImpl.h:1528
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MDNode
Metadata node.
Definition: Metadata.h:906
OP
#define OP(n)
Definition: regex2.h:73
llvm::StringMapImpl::size
unsigned size() const
Definition: StringMap.h:94
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:140
llvm::LLVMContextImpl::IntConstants
IntMapTy IntConstants
Definition: LLVMContextImpl.h:1377
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:1094
llvm::LLVMContextImpl::getOptPassGate
OptPassGate & getOptPassGate() const
Access the object which can disable optional passes and individual optimizations at compile time.
Definition: LLVMContextImpl.cpp:226
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::LLVMContextImpl::getOpaquePointers
bool getOpaquePointers()
Definition: LLVMContextImpl.cpp:236
uint32_t
llvm::LLVMContextImpl::StructConstants
StructConstantsTy StructConstants
Definition: LLVMContextImpl.h:1412
llvm::LLVMContextImpl::getOrInsertSyncScopeID
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID.
Definition: LLVMContextImpl.cpp:210
llvm::LLVMContextImpl::PVConstants
DenseMap< Type *, std::unique_ptr< PoisonValue > > PVConstants
Definition: LLVMContextImpl.h:1421
std
Definition: BitVector.h:850
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:325
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:151
llvm::LLVMContextImpl::AttrsSetNodes
FoldingSet< AttributeSetNode > AttrsSetNodes
Definition: LLVMContextImpl.h:1385
llvm::LLVMContextImpl::MetadataAsValues
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
Definition: LLVMContextImpl.h:1389
llvm::Module::dropTriviallyDeadConstantArrays
void dropTriviallyDeadConstantArrays()
Destroy ConstantArrays in LLVMContext if they are not used.
Definition: LLVMContextImpl.cpp:153
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:483
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:1499
llvm::SmallVectorImpl< StringRef >
llvm::LLVMContextImpl::VectorConstants
VectorConstantsTy VectorConstants
Definition: LLVMContextImpl.h:1415
llvm::SmallSetVector
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:306
llvm::MDNodeOpsKey::calculateHash
static unsigned calculateHash(MDNode *N, unsigned Offset=0)
Definition: LLVMContextImpl.cpp:176
llvm::LLVMContextImpl::DistinctMDNodes
std::vector< MDNode * > DistinctMDNodes
Definition: LLVMContextImpl.h:1404
llvm::cl::desc
Definition: CommandLine.h:412
LLVM_UNLIKELY
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:227
llvm::get_hashable_data
static const Metadata * get_hashable_data(const MDOperand &X)
Make MDOperand transparent for hashing.
Definition: LLVMContextImpl.cpp:172
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:633
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::pop_back_val
LLVM_NODISCARD T pop_back_val()
Definition: SetVector.h:231
llvm::LLVMContextImpl::LLVMContextImpl
LLVMContextImpl(LLVMContext &C)
Definition: LLVMContextImpl.cpp:29
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:152
llvm::ConstantUniqueMap::freeConstants
void freeConstants()
Definition: ConstantsContext.h:703
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:753
SetVector.h
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44
OpaquePointersCL
static cl::opt< bool > OpaquePointersCL("opaque-pointers", cl::desc("Use opaque pointers"), cl::init(false))