LLVM  17.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 "AttributeImpl.h"
15 #include "llvm/ADT/SetVector.h"
17 #include "llvm/ADT/iterator.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/IR/OptBisect.h"
23 #include "llvm/IR/Type.h"
24 #include "llvm/IR/Use.h"
25 #include "llvm/IR/User.h"
28 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/TypeSize.h"
31 #include <cassert>
32 #include <utility>
33 
34 using namespace llvm;
35 
36 static cl::opt<bool>
37  OpaquePointersCL("opaque-pointers", cl::desc("Use opaque pointers"),
38  cl::init(true));
39 
41  : DiagHandler(std::make_unique<DiagnosticHandler>()),
42  VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID),
43  HalfTy(C, Type::HalfTyID), BFloatTy(C, Type::BFloatTyID),
44  FloatTy(C, Type::FloatTyID), DoubleTy(C, Type::DoubleTyID),
45  MetadataTy(C, Type::MetadataTyID), TokenTy(C, Type::TokenTyID),
46  X86_FP80Ty(C, Type::X86_FP80TyID), FP128Ty(C, Type::FP128TyID),
47  PPC_FP128Ty(C, Type::PPC_FP128TyID), X86_MMXTy(C, Type::X86_MMXTyID),
48  X86_AMXTy(C, Type::X86_AMXTyID), Int1Ty(C, 1), Int8Ty(C, 8),
49  Int16Ty(C, 16), Int32Ty(C, 32), Int64Ty(C, 64), Int128Ty(C, 128) {
51  OpaquePointers = OpaquePointersCL;
52  }
53 }
54 
56  // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
57  // will call LLVMContextImpl::removeModule, thus invalidating iterators into
58  // the container. Avoid iterators during this operation:
59  while (!OwnedModules.empty())
60  delete *OwnedModules.begin();
61 
62 #ifndef NDEBUG
63  // Check for metadata references from leaked Values.
64  for (auto &Pair : ValueMetadata)
65  Pair.first->dump();
66  assert(ValueMetadata.empty() && "Values with metadata have been leaked");
67 #endif
68 
69  // Drop references for MDNodes. Do this before Values get deleted to avoid
70  // unnecessary RAUW when nodes are still unresolved.
71  for (auto *I : DistinctMDNodes) {
72  // We may have DIArgList that were uniqued, and as it has a custom
73  // implementation of dropAllReferences, it needs to be explicitly invoked.
74  if (auto *AL = dyn_cast<DIArgList>(I)) {
75  AL->dropAllReferences();
76  continue;
77  }
78  I->dropAllReferences();
79  }
80 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
81  for (auto *I : CLASS##s) \
82  I->dropAllReferences();
83 #include "llvm/IR/Metadata.def"
84 
85  // Also drop references that come from the Value bridges.
86  for (auto &Pair : ValuesAsMetadata)
87  Pair.second->dropUsers();
88  for (auto &Pair : MetadataAsValues)
89  Pair.second->dropUse();
90 
91  // Destroy MDNodes.
92  for (MDNode *I : DistinctMDNodes)
93  I->deleteAsSubclass();
94 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
95  for (CLASS * I : CLASS##s) \
96  delete I;
97 #include "llvm/IR/Metadata.def"
98 
99  // Free the constants.
100  for (auto *I : ExprConstants)
101  I->dropAllReferences();
102  for (auto *I : ArrayConstants)
103  I->dropAllReferences();
104  for (auto *I : StructConstants)
105  I->dropAllReferences();
106  for (auto *I : VectorConstants)
107  I->dropAllReferences();
108  ExprConstants.freeConstants();
112  InlineAsms.freeConstants();
113 
114  CAZConstants.clear();
115  CPNConstants.clear();
116  CTNConstants.clear();
117  UVConstants.clear();
118  PVConstants.clear();
120  FPConstants.clear();
121  CDSConstants.clear();
122 
123  // Destroy attribute node lists.
125  E = AttrsSetNodes.end(); I != E; ) {
127  delete &*Elem;
128  }
129 
130  // Destroy MetadataAsValues.
131  {
133  MDVs.reserve(MetadataAsValues.size());
134  for (auto &Pair : MetadataAsValues)
135  MDVs.push_back(Pair.second);
136  MetadataAsValues.clear();
137  for (auto *V : MDVs)
138  delete V;
139  }
140 
141  // Destroy ValuesAsMetadata.
142  for (auto &Pair : ValuesAsMetadata)
143  delete Pair.second;
144 }
145 
148 
149  // When ArrayConstants are of substantial size and only a few in them are
150  // dead, starting WorkList with all elements of ArrayConstants can be
151  // wasteful. Instead, starting WorkList with only elements that have empty
152  // uses.
154  if (C->use_empty())
155  WorkList.insert(C);
156 
157  while (!WorkList.empty()) {
158  ConstantArray *C = WorkList.pop_back_val();
159  if (C->use_empty()) {
160  for (const Use &Op : C->operands()) {
161  if (auto *COp = dyn_cast<ConstantArray>(Op))
162  WorkList.insert(COp);
163  }
164  C->destroyConstant();
165  }
166  }
167 }
168 
171 }
172 
173 namespace llvm {
174 
175 /// Make MDOperand transparent for hashing.
176 ///
177 /// This overload of an implementation detail of the hashing library makes
178 /// MDOperand hash to the same value as a \a Metadata pointer.
179 ///
180 /// Note that overloading \a hash_value() as follows:
181 ///
182 /// \code
183 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
184 /// \endcode
185 ///
186 /// does not cause MDOperand to be transparent. In particular, a bare pointer
187 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
188 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
189 
190 } // end namespace llvm
191 
193  unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
194 #ifndef NDEBUG
195  {
196  SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset));
197  unsigned RawHash = calculateHash(MDs);
198  assert(Hash == RawHash &&
199  "Expected hash of MDOperand to equal hash of Metadata*");
200  }
201 #endif
202  return Hash;
203 }
204 
206  return hash_combine_range(Ops.begin(), Ops.end());
207 }
208 
210  uint32_t NewIdx = BundleTagCache.size();
211  return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
212 }
213 
215  Tags.resize(BundleTagCache.size());
216  for (const auto &T : BundleTagCache)
217  Tags[T.second] = T.first();
218 }
219 
221  auto I = BundleTagCache.find(Tag);
222  assert(I != BundleTagCache.end() && "Unknown tag!");
223  return I->second;
224 }
225 
227  auto NewSSID = SSC.size();
229  "Hit the maximum number of synchronization scopes allowed!");
230  return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
231 }
232 
234  SmallVectorImpl<StringRef> &SSNs) const {
235  SSNs.resize(SSC.size());
236  for (const auto &SSE : SSC)
237  SSNs[SSE.second] = SSE.first();
238 }
239 
240 /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
241 /// singleton OptBisect if not explicitly set.
243  if (!OPG)
244  OPG = &getGlobalPassGate();
245  return *OPG;
246 }
247 
249  this->OPG = &OPG;
250 }
251 
253  if (LLVM_UNLIKELY(!OpaquePointers))
254  OpaquePointers = OpaquePointersCL;
255  return *OpaquePointers;
256 }
257 
259  assert((!OpaquePointers || *OpaquePointers == OP) &&
260  "Cannot change opaque pointers mode once set");
261  OpaquePointers = OP;
262 }
Int32Ty
IntegerType * Int32Ty
Definition: NVVMIntrRange.cpp:67
llvm::LLVMContext::pImpl
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:69
llvm::LLVMContextImpl::InlineAsms
ConstantUniqueMap< InlineAsm > InlineAsms
Definition: LLVMContextImpl.h:1508
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FloatTyID
const Type::TypeID FloatTyID
Definition: Mips16HardFloat.cpp:104
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:386
llvm::StringMapEntry< uint32_t >
llvm::LLVMContextImpl::FPConstants
FPMapTy FPConstants
Definition: LLVMContextImpl.h:1455
T
llvm::LLVMContextImpl::UVConstants
DenseMap< Type *, std::unique_ptr< UndefValue > > UVConstants
Definition: LLVMContextImpl.h:1493
TypeSize.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
ErrorHandling.h
llvm::LLVMContextImpl::ExprConstants
ConstantUniqueMap< ConstantExpr > ExprConstants
Definition: LLVMContextImpl.h:1506
llvm::LLVMContextImpl::setOptPassGate
void setOptPassGate(OptPassGate &)
Set the object which can disable optional passes and individual optimizations at compile time.
Definition: LLVMContextImpl.cpp:248
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
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::LLVMContextImpl::ValuesAsMetadata
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
Definition: LLVMContextImpl.h:1462
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:217
llvm::dwarf::Tag
Tag
Definition: Dwarf.h:103
llvm::LLVMContextImpl::~LLVMContextImpl
~LLVMContextImpl()
Definition: LLVMContextImpl.cpp:55
Use.h
llvm::LLVMContextImpl::getSyncScopeNames
void getSyncScopeNames(SmallVectorImpl< StringRef > &SSNs) const
getSyncScopeNames - Populates client supplied SmallVector with synchronization scope names registered...
Definition: LLVMContextImpl.cpp:233
llvm::LLVMContextImpl::ArrayConstants
ArrayConstantsTy ArrayConstants
Definition: LLVMContextImpl.h:1481
llvm::VFISAKind::SSE
@ SSE
CommandLine.h
llvm::LLVMContextImpl::dropTriviallyDeadConstantArrays
void dropTriviallyDeadConstantArrays()
Destroy the ConstantArrays if they are not used.
Definition: LLVMContextImpl.cpp:146
llvm::FoldingSetIterator
Definition: FoldingSet.h:394
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::LLVMContextImpl::getOperandBundleTagID
uint32_t getOperandBundleTagID(StringRef Tag) const
Definition: LLVMContextImpl.cpp:220
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:1478
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:209
llvm::ConstantArray
ConstantArray - Constant Array Declarations.
Definition: Constants.h:409
LLVMContextImpl.h
DiagnosticHandler.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:1406
llvm::getGlobalPassGate
OptPassGate & getGlobalPassGate()
Singleton instance of the OptBisect class, so multiple pass managers don't need to coordinate their u...
Definition: OptBisect.cpp:54
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:642
llvm::cl::Option::getNumOccurrences
int getNumOccurrences() const
Definition: CommandLine.h:401
llvm::LLVMContextImpl::CTNConstants
DenseMap< TargetExtType *, std::unique_ptr< ConstantTargetNone > > CTNConstants
Definition: LLVMContextImpl.h:1491
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:61
llvm::LLVMContextImpl::CDSConstants
StringMap< std::unique_ptr< ConstantDataSequential > > CDSConstants
Definition: LLVMContextImpl.h:1497
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")
OpaquePointersCL
static cl::opt< bool > OpaquePointersCL("opaque-pointers", cl::desc("Use opaque pointers"), cl::init(true))
DoubleTyID
const Type::TypeID DoubleTyID
Definition: Mips16HardFloat.cpp:105
llvm::SmallPtrSetImplBase::empty
bool empty() const
Definition: SmallPtrSet.h:92
llvm::LLVMContextImpl::BundleTagCache
StringMap< uint32_t > BundleTagCache
A set of interned tags for operand bundles.
Definition: LLVMContextImpl.h:1577
llvm::LLVMContextImpl::ValueMetadata
DenseMap< const Value *, MDAttachments > ValueMetadata
Collection of metadata used in this context.
Definition: LLVMContextImpl.h:1552
llvm::cl::opt< bool >
llvm::LLVMContextImpl::setOpaquePointers
void setOpaquePointers(bool OP)
Definition: LLVMContextImpl.cpp:258
llvm::LLVMContextImpl::CPNConstants
DenseMap< PointerType *, std::unique_ptr< ConstantPointerNull > > CPNConstants
Definition: LLVMContextImpl.h:1489
llvm::DenseMapBase::clear
void clear()
Definition: DenseMap.h:110
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::SyncScope::ID
uint8_t ID
Definition: LLVMContext.h:46
iterator.h
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::LLVMContextImpl::getOperandBundleTags
void getOperandBundleTags(SmallVectorImpl< StringRef > &Tags) const
Definition: LLVMContextImpl.cpp:214
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
llvm::SmallPtrSetImpl::begin
iterator begin() const
Definition: SmallPtrSet.h:403
llvm::LLVMContextImpl::OPG
OptPassGate * OPG
Definition: LLVMContextImpl.h:1614
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator_range.h
llvm::MDNode
Metadata node.
Definition: Metadata.h:943
OP
#define OP(n)
Definition: regex2.h:73
llvm::StringMapImpl::size
unsigned size() const
Definition: StringMap.h:95
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:1451
AttributeImpl.h
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:33
DiagHandler
static void DiagHandler(const SMDiagnostic &Diag, void *Context)
Definition: TextStub.cpp:1094
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::pop_back_val
T pop_back_val()
Definition: SetVector.h:225
llvm::LLVMContextImpl::getOptPassGate
OptPassGate & getOptPassGate() const
Access the object which can disable optional passes and individual optimizations at compile time.
Definition: LLVMContextImpl.cpp:242
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::LLVMContextImpl::getOpaquePointers
bool getOpaquePointers()
Definition: LLVMContextImpl.cpp:252
llvm::Offset
@ Offset
Definition: DWP.cpp:406
uint32_t
Compiler.h
llvm::LLVMContextImpl::StructConstants
StructConstantsTy StructConstants
Definition: LLVMContextImpl.h:1484
llvm::LLVMContextImpl::getOrInsertSyncScopeID
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID.
Definition: LLVMContextImpl.cpp:226
llvm::LLVMContextImpl::PVConstants
DenseMap< Type *, std::unique_ptr< PoisonValue > > PVConstants
Definition: LLVMContextImpl.h:1495
StringMapEntry.h
std
Definition: BitVector.h:851
LLVMRemarkStreamer.h
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:354
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:151
RemarkStreamer.h
llvm::LLVMContextImpl::AttrsSetNodes
FoldingSet< AttributeSetNode > AttrsSetNodes
Definition: LLVMContextImpl.h:1459
llvm::LLVMContextImpl::MetadataAsValues
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
Definition: LLVMContextImpl.h:1463
User.h
llvm::Module::dropTriviallyDeadConstantArrays
void dropTriviallyDeadConstantArrays()
Destroy ConstantArrays in LLVMContext if they are not used.
Definition: LLVMContextImpl.cpp:169
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:486
llvm::LLVMContextImpl::SSC
StringMap< SyncScope::ID > SSC
A set of interned synchronization scopes.
Definition: LLVMContextImpl.h:1585
llvm::SmallVectorImpl< StringRef >
llvm::LLVMContextImpl::VectorConstants
VectorConstantsTy VectorConstants
Definition: LLVMContextImpl.h:1487
llvm::SmallSetVector
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:300
llvm::MDNodeOpsKey::calculateHash
static unsigned calculateHash(MDNode *N, unsigned Offset=0)
Definition: LLVMContextImpl.cpp:192
llvm::LLVMContextImpl::DistinctMDNodes
std::vector< MDNode * > DistinctMDNodes
Definition: LLVMContextImpl.h:1476
llvm::cl::desc
Definition: CommandLine.h:411
LLVM_UNLIKELY
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:210
llvm::get_hashable_data
static const Metadata * get_hashable_data(const MDOperand &X)
Make MDOperand transparent for hashing.
Definition: LLVMContextImpl.cpp:188
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:667
llvm::LLVMContextImpl::LLVMContextImpl
LLVMContextImpl(LLVMContext &C)
Definition: LLVMContextImpl.cpp:40
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:152
llvm::ConstantUniqueMap::freeConstants
void freeConstants()
Definition: ConstantsContext.h:612
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:772
SetVector.h
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43