LLVMContextImpl.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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/IR/Module.h"
  14. #include "llvm/IR/OptBisect.h"
  15. #include "llvm/IR/Type.h"
  16. #include "llvm/Support/ManagedStatic.h"
  17. #include <cassert>
  18. #include <utility>
  19. using namespace llvm;
  20. LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
  21. : DiagHandler(std::make_unique<DiagnosticHandler>()),
  22. VoidTy(C, Type::VoidTyID),
  23. LabelTy(C, Type::LabelTyID),
  24. HalfTy(C, Type::HalfTyID),
  25. FloatTy(C, Type::FloatTyID),
  26. DoubleTy(C, Type::DoubleTyID),
  27. MetadataTy(C, Type::MetadataTyID),
  28. TokenTy(C, Type::TokenTyID),
  29. X86_FP80Ty(C, Type::X86_FP80TyID),
  30. FP128Ty(C, Type::FP128TyID),
  31. PPC_FP128Ty(C, Type::PPC_FP128TyID),
  32. X86_MMXTy(C, Type::X86_MMXTyID),
  33. Int1Ty(C, 1),
  34. Int8Ty(C, 8),
  35. Int16Ty(C, 16),
  36. Int32Ty(C, 32),
  37. Int64Ty(C, 64),
  38. Int128Ty(C, 128) {}
  39. LLVMContextImpl::~LLVMContextImpl() {
  40. // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
  41. // will call LLVMContextImpl::removeModule, thus invalidating iterators into
  42. // the container. Avoid iterators during this operation:
  43. while (!OwnedModules.empty())
  44. delete *OwnedModules.begin();
  45. #ifndef NDEBUG
  46. // Check for metadata references from leaked Instructions.
  47. for (auto &Pair : InstructionMetadata)
  48. Pair.first->dump();
  49. assert(InstructionMetadata.empty() &&
  50. "Instructions with metadata have been leaked");
  51. #endif
  52. // Drop references for MDNodes. Do this before Values get deleted to avoid
  53. // unnecessary RAUW when nodes are still unresolved.
  54. for (auto *I : DistinctMDNodes)
  55. I->dropAllReferences();
  56. #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
  57. for (auto *I : CLASS##s) \
  58. I->dropAllReferences();
  59. #include "llvm/IR/Metadata.def"
  60. // Also drop references that come from the Value bridges.
  61. for (auto &Pair : ValuesAsMetadata)
  62. Pair.second->dropUsers();
  63. for (auto &Pair : MetadataAsValues)
  64. Pair.second->dropUse();
  65. // Destroy MDNodes.
  66. for (MDNode *I : DistinctMDNodes)
  67. I->deleteAsSubclass();
  68. #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
  69. for (CLASS * I : CLASS##s) \
  70. delete I;
  71. #include "llvm/IR/Metadata.def"
  72. // Free the constants.
  73. for (auto *I : ExprConstants)
  74. I->dropAllReferences();
  75. for (auto *I : ArrayConstants)
  76. I->dropAllReferences();
  77. for (auto *I : StructConstants)
  78. I->dropAllReferences();
  79. for (auto *I : VectorConstants)
  80. I->dropAllReferences();
  81. ExprConstants.freeConstants();
  82. ArrayConstants.freeConstants();
  83. StructConstants.freeConstants();
  84. VectorConstants.freeConstants();
  85. InlineAsms.freeConstants();
  86. CAZConstants.clear();
  87. CPNConstants.clear();
  88. UVConstants.clear();
  89. IntConstants.clear();
  90. FPConstants.clear();
  91. for (auto &CDSConstant : CDSConstants)
  92. delete CDSConstant.second;
  93. CDSConstants.clear();
  94. // Destroy attributes.
  95. for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
  96. E = AttrsSet.end(); I != E; ) {
  97. FoldingSetIterator<AttributeImpl> Elem = I++;
  98. delete &*Elem;
  99. }
  100. // Destroy attribute lists.
  101. for (FoldingSetIterator<AttributeListImpl> I = AttrsLists.begin(),
  102. E = AttrsLists.end();
  103. I != E;) {
  104. FoldingSetIterator<AttributeListImpl> Elem = I++;
  105. delete &*Elem;
  106. }
  107. // Destroy attribute node lists.
  108. for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
  109. E = AttrsSetNodes.end(); I != E; ) {
  110. FoldingSetIterator<AttributeSetNode> Elem = I++;
  111. delete &*Elem;
  112. }
  113. // Destroy MetadataAsValues.
  114. {
  115. SmallVector<MetadataAsValue *, 8> MDVs;
  116. MDVs.reserve(MetadataAsValues.size());
  117. for (auto &Pair : MetadataAsValues)
  118. MDVs.push_back(Pair.second);
  119. MetadataAsValues.clear();
  120. for (auto *V : MDVs)
  121. delete V;
  122. }
  123. // Destroy ValuesAsMetadata.
  124. for (auto &Pair : ValuesAsMetadata)
  125. delete Pair.second;
  126. }
  127. void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
  128. bool Changed;
  129. do {
  130. Changed = false;
  131. for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
  132. auto *C = *I++;
  133. if (C->use_empty()) {
  134. Changed = true;
  135. C->destroyConstant();
  136. }
  137. }
  138. } while (Changed);
  139. }
  140. void Module::dropTriviallyDeadConstantArrays() {
  141. Context.pImpl->dropTriviallyDeadConstantArrays();
  142. }
  143. namespace llvm {
  144. /// Make MDOperand transparent for hashing.
  145. ///
  146. /// This overload of an implementation detail of the hashing library makes
  147. /// MDOperand hash to the same value as a \a Metadata pointer.
  148. ///
  149. /// Note that overloading \a hash_value() as follows:
  150. ///
  151. /// \code
  152. /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
  153. /// \endcode
  154. ///
  155. /// does not cause MDOperand to be transparent. In particular, a bare pointer
  156. /// doesn't get hashed before it's combined, whereas \a MDOperand would.
  157. static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
  158. } // end namespace llvm
  159. unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
  160. unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
  161. #ifndef NDEBUG
  162. {
  163. SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
  164. unsigned RawHash = calculateHash(MDs);
  165. assert(Hash == RawHash &&
  166. "Expected hash of MDOperand to equal hash of Metadata*");
  167. }
  168. #endif
  169. return Hash;
  170. }
  171. unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
  172. return hash_combine_range(Ops.begin(), Ops.end());
  173. }
  174. StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
  175. uint32_t NewIdx = BundleTagCache.size();
  176. return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
  177. }
  178. void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
  179. Tags.resize(BundleTagCache.size());
  180. for (const auto &T : BundleTagCache)
  181. Tags[T.second] = T.first();
  182. }
  183. uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
  184. auto I = BundleTagCache.find(Tag);
  185. assert(I != BundleTagCache.end() && "Unknown tag!");
  186. return I->second;
  187. }
  188. SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
  189. auto NewSSID = SSC.size();
  190. assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
  191. "Hit the maximum number of synchronization scopes allowed!");
  192. return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
  193. }
  194. void LLVMContextImpl::getSyncScopeNames(
  195. SmallVectorImpl<StringRef> &SSNs) const {
  196. SSNs.resize(SSC.size());
  197. for (const auto &SSE : SSC)
  198. SSNs[SSE.second] = SSE.first();
  199. }
  200. /// Singleton instance of the OptBisect class.
  201. ///
  202. /// This singleton is accessed via the LLVMContext::getOptPassGate() function.
  203. /// It provides a mechanism to disable passes and individual optimizations at
  204. /// compile time based on a command line option (-opt-bisect-limit) in order to
  205. /// perform a bisecting search for optimization-related problems.
  206. ///
  207. /// Even if multiple LLVMContext objects are created, they will all return the
  208. /// same instance of OptBisect in order to provide a single bisect count. Any
  209. /// code that uses the OptBisect object should be serialized when bisection is
  210. /// enabled in order to enable a consistent bisect count.
  211. static ManagedStatic<OptBisect> OptBisector;
  212. OptPassGate &LLVMContextImpl::getOptPassGate() const {
  213. if (!OPG)
  214. OPG = &(*OptBisector);
  215. return *OPG;
  216. }
  217. void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
  218. this->OPG = &OPG;
  219. }