LLVM  16.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  UVConstants.clear();
117  PVConstants.clear();
119  FPConstants.clear();
120  CDSConstants.clear();
121 
122  // Destroy attribute node lists.
124  E = AttrsSetNodes.end(); I != E; ) {
126  delete &*Elem;
127  }
128 
129  // Destroy MetadataAsValues.
130  {
132  MDVs.reserve(MetadataAsValues.size());
133  for (auto &Pair : MetadataAsValues)
134  MDVs.push_back(Pair.second);
135  MetadataAsValues.clear();
136  for (auto *V : MDVs)
137  delete V;
138  }
139 
140  // Destroy ValuesAsMetadata.
141  for (auto &Pair : ValuesAsMetadata)
142  delete Pair.second;
143 }
144 
147 
148  // When ArrayConstants are of substantial size and only a few in them are
149  // dead, starting WorkList with all elements of ArrayConstants can be
150  // wasteful. Instead, starting WorkList with only elements that have empty
151  // uses.
153  if (C->use_empty())
154  WorkList.insert(C);
155 
156  while (!WorkList.empty()) {
157  ConstantArray *C = WorkList.pop_back_val();
158  if (C->use_empty()) {
159  for (const Use &Op : C->operands()) {
160  if (auto *COp = dyn_cast<ConstantArray>(Op))
161  WorkList.insert(COp);
162  }
163  C->destroyConstant();
164  }
165  }
166 }
167 
170 }
171 
172 namespace llvm {
173 
174 /// Make MDOperand transparent for hashing.
175 ///
176 /// This overload of an implementation detail of the hashing library makes
177 /// MDOperand hash to the same value as a \a Metadata pointer.
178 ///
179 /// Note that overloading \a hash_value() as follows:
180 ///
181 /// \code
182 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
183 /// \endcode
184 ///
185 /// does not cause MDOperand to be transparent. In particular, a bare pointer
186 /// doesn't get hashed before it's combined, whereas \a MDOperand would.
187 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
188 
189 } // end namespace llvm
190 
191 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
192  unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
193 #ifndef NDEBUG
194  {
195  SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset));
196  unsigned RawHash = calculateHash(MDs);
197  assert(Hash == RawHash &&
198  "Expected hash of MDOperand to equal hash of Metadata*");
199  }
200 #endif
201  return Hash;
202 }
203 
205  return hash_combine_range(Ops.begin(), Ops.end());
206 }
207 
209  uint32_t NewIdx = BundleTagCache.size();
210  return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
211 }
212 
214  Tags.resize(BundleTagCache.size());
215  for (const auto &T : BundleTagCache)
216  Tags[T.second] = T.first();
217 }
218 
220  auto I = BundleTagCache.find(Tag);
221  assert(I != BundleTagCache.end() && "Unknown tag!");
222  return I->second;
223 }
224 
226  auto NewSSID = SSC.size();
228  "Hit the maximum number of synchronization scopes allowed!");
229  return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
230 }
231 
233  SmallVectorImpl<StringRef> &SSNs) const {
234  SSNs.resize(SSC.size());
235  for (const auto &SSE : SSC)
236  SSNs[SSE.second] = SSE.first();
237 }
238 
239 /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
240 /// singleton OptBisect if not explicitly set.
242  if (!OPG)
243  OPG = &getGlobalPassGate();
244  return *OPG;
245 }
246 
248  this->OPG = &OPG;
249 }
250 
252  return OpaquePointers.has_value();
253 }
254 
256  if (LLVM_UNLIKELY(!OpaquePointers))
257  OpaquePointers = OpaquePointersCL;
258  return *OpaquePointers;
259 }
260 
262  assert((!OpaquePointers || OpaquePointers.value() == OP) &&
263  "Cannot change opaque pointers mode once set");
264  OpaquePointers = OP;
265 }
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:1459
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:387
llvm::StringMapEntry< uint32_t >
llvm::LLVMContextImpl::FPConstants
FPMapTy FPConstants
Definition: LLVMContextImpl.h:1408
llvm::LLVMContextImpl::UVConstants
DenseMap< Type *, std::unique_ptr< UndefValue > > UVConstants
Definition: LLVMContextImpl.h:1444
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:1457
llvm::LLVMContextImpl::setOptPassGate
void setOptPassGate(OptPassGate &)
Set the object which can disable optional passes and individual optimizations at compile time.
Definition: LLVMContextImpl.cpp:247
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
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:1415
llvm::StringMap::find
iterator find(StringRef Key)
Definition: StringMap.h:217
llvm::dwarf::Tag
Tag
Definition: Dwarf.h:105
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:232
llvm::LLVMContextImpl::ArrayConstants
ArrayConstantsTy ArrayConstants
Definition: LLVMContextImpl.h:1434
llvm::VFISAKind::SSE
@ SSE
CommandLine.h
llvm::LLVMContextImpl::dropTriviallyDeadConstantArrays
void dropTriviallyDeadConstantArrays()
Destroy the ConstantArrays if they are not used.
Definition: LLVMContextImpl.cpp:145
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:219
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:1431
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:208
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:1359
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:403
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::LLVMContextImpl::CDSConstants
StringMap< std::unique_ptr< ConstantDataSequential > > CDSConstants
Definition: LLVMContextImpl.h:1448
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:1525
llvm::LLVMContextImpl::ValueMetadata
DenseMap< const Value *, MDAttachments > ValueMetadata
Collection of metadata used in this context.
Definition: LLVMContextImpl.h:1500
llvm::cl::opt< bool >
llvm::LLVMContextImpl::setOpaquePointers
void setOpaquePointers(bool OP)
Definition: LLVMContextImpl.cpp:261
llvm::LLVMContextImpl::CPNConstants
DenseMap< PointerType *, std::unique_ptr< ConstantPointerNull > > CPNConstants
Definition: LLVMContextImpl.h:1442
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:213
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:447
llvm::SmallPtrSetImpl::begin
iterator begin() const
Definition: SmallPtrSet.h:403
llvm::LLVMContextImpl::OPG
OptPassGate * OPG
Definition: LLVMContextImpl.h:1562
llvm::LLVMContextImpl::hasOpaquePointersValue
bool hasOpaquePointersValue()
Definition: LLVMContextImpl.cpp:251
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
iterator_range.h
llvm::MDNode
Metadata node.
Definition: Metadata.h:944
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:1404
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::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::pop_back_val
T pop_back_val()
Definition: SetVector.h:232
llvm::LLVMContextImpl::getOptPassGate
OptPassGate & getOptPassGate() const
Access the object which can disable optional passes and individual optimizations at compile time.
Definition: LLVMContextImpl.cpp:241
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::LLVMContextImpl::getOpaquePointers
bool getOpaquePointers()
Definition: LLVMContextImpl.cpp:255
uint32_t
Compiler.h
llvm::LLVMContextImpl::StructConstants
StructConstantsTy StructConstants
Definition: LLVMContextImpl.h:1437
llvm::LLVMContextImpl::getOrInsertSyncScopeID
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID.
Definition: LLVMContextImpl.cpp:225
llvm::LLVMContextImpl::PVConstants
DenseMap< Type *, std::unique_ptr< PoisonValue > > PVConstants
Definition: LLVMContextImpl.h:1446
llvm::Optional::has_value
constexpr bool has_value() const
Definition: Optional.h:285
StringMapEntry.h
std
Definition: BitVector.h:851
LLVMRemarkStreamer.h
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:348
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:152
RemarkStreamer.h
llvm::LLVMContextImpl::AttrsSetNodes
FoldingSet< AttributeSetNode > AttrsSetNodes
Definition: LLVMContextImpl.h:1412
llvm::LLVMContextImpl::MetadataAsValues
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
Definition: LLVMContextImpl.h:1416
User.h
llvm::Module::dropTriviallyDeadConstantArrays
void dropTriviallyDeadConstantArrays()
Destroy ConstantArrays in LLVMContext if they are not used.
Definition: LLVMContextImpl.cpp:168
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::Optional::value
constexpr const T & value() const &
Definition: Optional.h:281
llvm::LLVMContextImpl::SSC
StringMap< SyncScope::ID > SSC
A set of interned synchronization scopes.
Definition: LLVMContextImpl.h:1533
llvm::SmallVectorImpl< StringRef >
llvm::LLVMContextImpl::VectorConstants
VectorConstantsTy VectorConstants
Definition: LLVMContextImpl.h:1440
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:191
llvm::LLVMContextImpl::DistinctMDNodes
std::vector< MDNode * > DistinctMDNodes
Definition: LLVMContextImpl.h:1429
llvm::cl::desc
Definition: CommandLine.h:413
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:187
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:153
llvm::ConstantUniqueMap::freeConstants
void freeConstants()
Definition: ConstantsContext.h:613
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