LLVMContextImpl.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include "LLVMContextImpl.h"
  13. #include "llvm/ADT/SetVector.h"
  14. #include "llvm/IR/Module.h"
  15. #include "llvm/IR/OptBisect.h"
  16. #include "llvm/IR/Type.h"
  17. #include "llvm/Support/ManagedStatic.h"
  18. #include <cassert>
  19. #include <utility>
  20. using namespace llvm;
  21. LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
  22. : DiagHandler(std::make_unique<DiagnosticHandler>()),
  23. VoidTy(C, Type::VoidTyID),
  24. LabelTy(C, Type::LabelTyID),
  25. HalfTy(C, Type::HalfTyID),
  26. BFloatTy(C, Type::BFloatTyID),
  27. FloatTy(C, Type::FloatTyID),
  28. DoubleTy(C, Type::DoubleTyID),
  29. MetadataTy(C, Type::MetadataTyID),
  30. TokenTy(C, Type::TokenTyID),
  31. X86_FP80Ty(C, Type::X86_FP80TyID),
  32. FP128Ty(C, Type::FP128TyID),
  33. PPC_FP128Ty(C, Type::PPC_FP128TyID),
  34. X86_MMXTy(C, Type::X86_MMXTyID),
  35. X86_AMXTy(C, Type::X86_AMXTyID),
  36. Int1Ty(C, 1),
  37. Int8Ty(C, 8),
  38. Int16Ty(C, 16),
  39. Int32Ty(C, 32),
  40. Int64Ty(C, 64),
  41. Int128Ty(C, 128) {}
  42. LLVMContextImpl::~LLVMContextImpl() {
  43. // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
  44. // will call LLVMContextImpl::removeModule, thus invalidating iterators into
  45. // the container. Avoid iterators during this operation:
  46. while (!OwnedModules.empty())
  47. delete *OwnedModules.begin();
  48. #ifndef NDEBUG
  49. // Check for metadata references from leaked Values.
  50. for (auto &Pair : ValueMetadata)
  51. Pair.first->dump();
  52. assert(ValueMetadata.empty() && "Values with metadata have been leaked");
  53. #endif
  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. I->dropAllReferences();
  58. #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
  59. for (auto *I : CLASS##s) \
  60. I->dropAllReferences();
  61. #include "llvm/IR/Metadata.def"
  62. // Also drop references that come from the Value bridges.
  63. for (auto &Pair : ValuesAsMetadata)
  64. Pair.second->dropUsers();
  65. for (auto &Pair : MetadataAsValues)
  66. Pair.second->dropUse();
  67. // Destroy MDNodes.
  68. for (MDNode *I : DistinctMDNodes)
  69. I->deleteAsSubclass();
  70. #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
  71. for (CLASS * I : CLASS##s) \
  72. delete I;
  73. #include "llvm/IR/Metadata.def"
  74. // Free the constants.
  75. for (auto *I : ExprConstants)
  76. I->dropAllReferences();
  77. for (auto *I : ArrayConstants)
  78. I->dropAllReferences();
  79. for (auto *I : StructConstants)
  80. I->dropAllReferences();
  81. for (auto *I : VectorConstants)
  82. I->dropAllReferences();
  83. ExprConstants.freeConstants();
  84. ArrayConstants.freeConstants();
  85. StructConstants.freeConstants();
  86. VectorConstants.freeConstants();
  87. InlineAsms.freeConstants();
  88. CAZConstants.clear();
  89. CPNConstants.clear();
  90. UVConstants.clear();
  91. PVConstants.clear();
  92. IntConstants.clear();
  93. FPConstants.clear();
  94. CDSConstants.clear();
  95. // Destroy attribute node lists.
  96. for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
  97. E = AttrsSetNodes.end(); I != E; ) {
  98. FoldingSetIterator<AttributeSetNode> Elem = I++;
  99. delete &*Elem;
  100. }
  101. // Destroy MetadataAsValues.
  102. {
  103. SmallVector<MetadataAsValue *, 8> MDVs;
  104. MDVs.reserve(MetadataAsValues.size());
  105. for (auto &Pair : MetadataAsValues)
  106. MDVs.push_back(Pair.second);
  107. MetadataAsValues.clear();
  108. for (auto *V : MDVs)
  109. delete V;
  110. }
  111. // Destroy ValuesAsMetadata.
  112. for (auto &Pair : ValuesAsMetadata)
  113. delete Pair.second;
  114. }
  115. void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
  116. SmallSetVector<ConstantArray *, 4> WorkList;
  117. // When ArrayConstants are of substantial size and only a few in them are
  118. // dead, starting WorkList with all elements of ArrayConstants can be
  119. // wasteful. Instead, starting WorkList with only elements that have empty
  120. // uses.
  121. for (ConstantArray *C : ArrayConstants)
  122. if (C->use_empty())
  123. WorkList.insert(C);
  124. while (!WorkList.empty()) {
  125. ConstantArray *C = WorkList.pop_back_val();
  126. if (C->use_empty()) {
  127. for (const Use &Op : C->operands()) {
  128. if (auto *COp = dyn_cast<ConstantArray>(Op))
  129. WorkList.insert(COp);
  130. }
  131. C->destroyConstant();
  132. }
  133. }
  134. }
  135. void Module::dropTriviallyDeadConstantArrays() {
  136. Context.pImpl->dropTriviallyDeadConstantArrays();
  137. }
  138. namespace llvm {
  139. /// Make MDOperand transparent for hashing.
  140. ///
  141. /// This overload of an implementation detail of the hashing library makes
  142. /// MDOperand hash to the same value as a \a Metadata pointer.
  143. ///
  144. /// Note that overloading \a hash_value() as follows:
  145. ///
  146. /// \code
  147. /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
  148. /// \endcode
  149. ///
  150. /// does not cause MDOperand to be transparent. In particular, a bare pointer
  151. /// doesn't get hashed before it's combined, whereas \a MDOperand would.
  152. static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
  153. } // end namespace llvm
  154. unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
  155. unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
  156. #ifndef NDEBUG
  157. {
  158. SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset));
  159. unsigned RawHash = calculateHash(MDs);
  160. assert(Hash == RawHash &&
  161. "Expected hash of MDOperand to equal hash of Metadata*");
  162. }
  163. #endif
  164. return Hash;
  165. }
  166. unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
  167. return hash_combine_range(Ops.begin(), Ops.end());
  168. }
  169. StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
  170. uint32_t NewIdx = BundleTagCache.size();
  171. return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
  172. }
  173. void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
  174. Tags.resize(BundleTagCache.size());
  175. for (const auto &T : BundleTagCache)
  176. Tags[T.second] = T.first();
  177. }
  178. uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
  179. auto I = BundleTagCache.find(Tag);
  180. assert(I != BundleTagCache.end() && "Unknown tag!");
  181. return I->second;
  182. }
  183. SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
  184. auto NewSSID = SSC.size();
  185. assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
  186. "Hit the maximum number of synchronization scopes allowed!");
  187. return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
  188. }
  189. void LLVMContextImpl::getSyncScopeNames(
  190. SmallVectorImpl<StringRef> &SSNs) const {
  191. SSNs.resize(SSC.size());
  192. for (const auto &SSE : SSC)
  193. SSNs[SSE.second] = SSE.first();
  194. }
  195. /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
  196. /// singleton OptBisect if not explicitly set.
  197. OptPassGate &LLVMContextImpl::getOptPassGate() const {
  198. if (!OPG)
  199. OPG = &(*OptBisector);
  200. return *OPG;
  201. }
  202. void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
  203. this->OPG = &OPG;
  204. }