LLVM  15.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"
31 #include "llvm/Support/TypeSize.h"
32 #include <cassert>
33 #include <utility>
34 
35 using namespace llvm;
36 
37 static cl::opt<bool>
38  OpaquePointersCL("opaque-pointers", cl::desc("Use opaque pointers"),
39  cl::init(true));
40 
42  : DiagHandler(std::make_unique<DiagnosticHandler>()),
43  VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID),
44  HalfTy(C, Type::HalfTyID), BFloatTy(C, Type::BFloatTyID),
45  FloatTy(C, Type::FloatTyID), DoubleTy(C, Type::DoubleTyID),
46  MetadataTy(C, Type::MetadataTyID), TokenTy(C, Type::TokenTyID),
47  X86_FP80Ty(C, Type::X86_FP80TyID), FP128Ty(C, Type::FP128TyID),
48  PPC_FP128Ty(C, Type::PPC_FP128TyID), X86_MMXTy(C, Type::X86_MMXTyID),
49  X86_AMXTy(C, Type::X86_AMXTyID), Int1Ty(C, 1), Int8Ty(C, 8),
50  Int16Ty(C, 16), Int32Ty(C, 32), Int64Ty(C, 64), Int128Ty(C, 128) {
52  OpaquePointers = OpaquePointersCL;
53  }
54 }
55 
57  // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
58  // will call LLVMContextImpl::removeModule, thus invalidating iterators into
59  // the container. Avoid iterators during this operation:
60  while (!OwnedModules.empty())
61  delete *OwnedModules.begin();
62 
63 #ifndef NDEBUG
64  // Check for metadata references from leaked Values.
65  for (auto &Pair : ValueMetadata)
66  Pair.first->dump();
67  assert(ValueMetadata.empty() && "Values with metadata have been leaked");
68 #endif
69 
70  // Drop references for MDNodes. Do this before Values get deleted to avoid
71  // unnecessary RAUW when nodes are still unresolved.
72  for (auto *I : DistinctMDNodes) {
73  // We may have DIArgList that were uniqued, and as it has a custom
74  // implementation of dropAllReferences, it needs to be explicitly invoked.
75  if (auto *AL = dyn_cast<DIArgList>(I)) {
76  AL->dropAllReferences();
77  continue;
78  }
79  I->dropAllReferences();
80  }
81 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
82  for (auto *I : CLASS##s) \
83  I->dropAllReferences();
84 #include "llvm/IR/Metadata.def"
85 
86  // Also drop references that come from the Value bridges.
87  for (auto &Pair : ValuesAsMetadata)
88  Pair.second->dropUsers();
89  for (auto &Pair : MetadataAsValues)
90  Pair.second->dropUse();
91 
92  // Destroy MDNodes.
93  for (MDNode *I : DistinctMDNodes)
94  I->deleteAsSubclass();
95 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
96  for (CLASS * I : CLASS##s) \
97  delete I;
98 #include "llvm/IR/Metadata.def"
99 
100  // Free the constants.
101  for (auto *I : ExprConstants)
102  I->dropAllReferences();
103  for (auto *I : ArrayConstants)
104  I->dropAllReferences();
105  for (auto *I : StructConstants)
106  I->dropAllReferences();
107  for (auto *I : VectorConstants)
108  I->dropAllReferences();
109  ExprConstants.freeConstants();
113  InlineAsms.freeConstants();
114 
115  CAZConstants.clear();
116  CPNConstants.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 
192 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
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 = &(*OptBisector);
245  return *OPG;
246 }
247 
249  this->OPG = &OPG;
250 }
251 
253  return OpaquePointers.hasValue();
254 }
255 
257  if (LLVM_UNLIKELY(!(OpaquePointers.hasValue())))
258  OpaquePointers = OpaquePointersCL;
259  return *OpaquePointers;
260 }
261 
263  assert((!OpaquePointers.hasValue() || OpaquePointers.getValue() == OP) &&
264  "Cannot change opaque pointers mode once set");
265  OpaquePointers = OP;
266 }
StringMapEntry.h
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:1458
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
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:280
llvm::StringMapEntry< uint32_t >
llvm::LLVMContextImpl::FPConstants
FPMapTy FPConstants
Definition: LLVMContextImpl.h:1407
llvm::LLVMContextImpl::UVConstants
DenseMap< Type *, std::unique_ptr< UndefValue > > UVConstants
Definition: LLVMContextImpl.h:1443
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:1185
ErrorHandling.h
ManagedStatic.h
llvm::LLVMContextImpl::ExprConstants
ConstantUniqueMap< ConstantExpr > ExprConstants
Definition: LLVMContextImpl.h:1456
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:205
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
T
#define T
Definition: Mips16ISelLowering.cpp:341
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:1414
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:218
llvm::dwarf::Tag
Tag
Definition: Dwarf.h:105
llvm::LLVMContextImpl::~LLVMContextImpl
~LLVMContextImpl()
Definition: LLVMContextImpl.cpp:56
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:1433
llvm::Optional::hasValue
constexpr bool hasValue() const
Definition: Optional.h:312
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:276
llvm::LLVMContextImpl::CAZConstants
DenseMap< Type *, std::unique_ptr< ConstantAggregateZero > > CAZConstants
Definition: LLVMContextImpl.h:1430
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:209
llvm::ConstantArray
ConstantArray - Constant Array Declarations.
Definition: Constants.h:410
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:1358
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:619
llvm::cl::Option::getNumOccurrences
int getNumOccurrences() const
Definition: CommandLine.h:395
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::LLVMContextImpl::CDSConstants
StringMap< std::unique_ptr< ConstantDataSequential > > CDSConstants
Definition: LLVMContextImpl.h:1447
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:104
llvm::LLVMContextImpl::BundleTagCache
StringMap< uint32_t > BundleTagCache
A set of interned tags for operand bundles.
Definition: LLVMContextImpl.h:1518
llvm::LLVMContextImpl::ValueMetadata
DenseMap< const Value *, MDAttachments > ValueMetadata
Collection of metadata used in this context.
Definition: LLVMContextImpl.h:1498
llvm::cl::opt< bool >
llvm::LLVMContextImpl::setOpaquePointers
void setOpaquePointers(bool OP)
Definition: LLVMContextImpl.cpp:262
llvm::LLVMContextImpl::CPNConstants
DenseMap< PointerType *, std::unique_ptr< ConstantPointerNull > > CPNConstants
Definition: LLVMContextImpl.h:1441
llvm::DenseMapBase::clear
void clear()
Definition: DenseMap.h:112
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
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:432
llvm::SmallPtrSetImpl::begin
iterator begin() const
Definition: SmallPtrSet.h:403
llvm::LLVMContextImpl::OPG
OptPassGate * OPG
Definition: LLVMContextImpl.h:1555
llvm::LLVMContextImpl::hasOpaquePointersValue
bool hasOpaquePointersValue()
Definition: LLVMContextImpl.cpp:252
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Optional::getValue
constexpr const T & getValue() const &
Definition: Optional.h:306
iterator_range.h
llvm::MDNode
Metadata node.
Definition: Metadata.h:937
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:1403
AttributeImpl.h
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:242
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::LLVMContextImpl::getOpaquePointers
bool getOpaquePointers()
Definition: LLVMContextImpl.cpp:256
uint32_t
Compiler.h
llvm::LLVMContextImpl::StructConstants
StructConstantsTy StructConstants
Definition: LLVMContextImpl.h:1436
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:1445
std
Definition: BitVector.h:851
LLVMRemarkStreamer.h
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:345
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:152
RemarkStreamer.h
llvm::LLVMContextImpl::AttrsSetNodes
FoldingSet< AttributeSetNode > AttrsSetNodes
Definition: LLVMContextImpl.h:1411
llvm::LLVMContextImpl::MetadataAsValues
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
Definition: LLVMContextImpl.h:1415
User.h
llvm::Module::dropTriviallyDeadConstantArrays
void dropTriviallyDeadConstantArrays()
Destroy ConstantArrays in LLVMContext if they are not used.
Definition: LLVMContextImpl.cpp:169
llvm::SmallPtrSetImplBase::empty
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:92
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::LLVMContextImpl::SSC
StringMap< SyncScope::ID > SSC
A set of interned synchronization scopes.
Definition: LLVMContextImpl.h:1526
llvm::SmallVectorImpl< StringRef >
llvm::LLVMContextImpl::VectorConstants
VectorConstantsTy VectorConstants
Definition: LLVMContextImpl.h:1439
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:192
llvm::LLVMContextImpl::DistinctMDNodes
std::vector< MDNode * > DistinctMDNodes
Definition: LLVMContextImpl.h:1428
llvm::cl::desc
Definition: CommandLine.h:405
LLVM_UNLIKELY
#define LLVM_UNLIKELY(EXPR)
Definition: Compiler.h:220
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:644
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:41
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:153
llvm::ConstantUniqueMap::freeConstants
void freeConstants()
Definition: ConstantsContext.h:703
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:773
SetVector.h
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43