LLVMContextImpl.cpp 8.3 KB

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