LLVMContextImpl.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //===-- LLVMContextImpl.cpp - Implement LLVMContextImpl -------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the opaque LLVMContextImpl.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "LLVMContextImpl.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/IR/Attributes.h"
  16. #include "llvm/IR/DiagnosticInfo.h"
  17. #include "llvm/IR/Module.h"
  18. #include <algorithm>
  19. using namespace llvm;
  20. LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
  21. : TheTrueVal(nullptr), TheFalseVal(nullptr),
  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. X86_FP80Ty(C, Type::X86_FP80TyID),
  29. FP128Ty(C, Type::FP128TyID),
  30. PPC_FP128Ty(C, Type::PPC_FP128TyID),
  31. X86_MMXTy(C, Type::X86_MMXTyID),
  32. Int1Ty(C, 1),
  33. Int8Ty(C, 8),
  34. Int16Ty(C, 16),
  35. Int32Ty(C, 32),
  36. Int64Ty(C, 64),
  37. Int128Ty(C, 128) {
  38. InlineAsmDiagHandler = nullptr;
  39. InlineAsmDiagContext = nullptr;
  40. DiagnosticHandler = nullptr;
  41. DiagnosticContext = nullptr;
  42. RespectDiagnosticFilters = false;
  43. YieldCallback = nullptr;
  44. YieldOpaqueHandle = nullptr;
  45. NamedStructTypesUniqueID = 0;
  46. }
  47. namespace {
  48. struct DropReferences {
  49. // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
  50. // is a Constant*.
  51. template <typename PairT> void operator()(const PairT &P) {
  52. P.second->dropAllReferences();
  53. }
  54. };
  55. // Temporary - drops pair.first instead of second.
  56. struct DropFirst {
  57. // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
  58. // is a Constant*.
  59. template<typename PairT>
  60. void operator()(const PairT &P) {
  61. P.first->dropAllReferences();
  62. }
  63. };
  64. }
  65. LLVMContextImpl::~LLVMContextImpl() {
  66. // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
  67. // will call LLVMContextImpl::removeModule, thus invalidating iterators into
  68. // the container. Avoid iterators during this operation:
  69. while (!OwnedModules.empty())
  70. delete *OwnedModules.begin();
  71. // Drop references for MDNodes. Do this before Values get deleted to avoid
  72. // unnecessary RAUW when nodes are still unresolved.
  73. for (auto *I : DistinctMDNodes)
  74. I->dropAllReferences();
  75. #define HANDLE_MDNODE_LEAF(CLASS) \
  76. for (auto *I : CLASS##s) \
  77. I->dropAllReferences();
  78. #include "llvm/IR/Metadata.def"
  79. // Also drop references that come from the Value bridges.
  80. for (auto &Pair : ValuesAsMetadata)
  81. if (Pair.second) Pair.second->dropUsers(); // HLSL Change - if alloc failed, entry might not be populated
  82. for (auto &Pair : MetadataAsValues)
  83. if (Pair.second) Pair.second->dropUse(); // HLSL Change - if alloc failed, entry might not be populated
  84. // Destroy MDNodes.
  85. for (MDNode *I : DistinctMDNodes)
  86. I->deleteAsSubclass();
  87. #define HANDLE_MDNODE_LEAF(CLASS) \
  88. for (CLASS *I : CLASS##s) \
  89. delete I;
  90. #include "llvm/IR/Metadata.def"
  91. // Free the constants.
  92. std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
  93. DropFirst());
  94. std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
  95. DropFirst());
  96. std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
  97. DropFirst());
  98. std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
  99. DropFirst());
  100. ExprConstants.freeConstants();
  101. ArrayConstants.freeConstants();
  102. StructConstants.freeConstants();
  103. VectorConstants.freeConstants();
  104. DeleteContainerSeconds(CAZConstants);
  105. DeleteContainerSeconds(CPNConstants);
  106. DeleteContainerSeconds(UVConstants);
  107. InlineAsms.freeConstants();
  108. DeleteContainerSeconds(IntConstants);
  109. DeleteContainerSeconds(FPConstants);
  110. for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
  111. E = CDSConstants.end(); I != E; ++I)
  112. delete I->second;
  113. CDSConstants.clear();
  114. // Destroy attributes.
  115. for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
  116. E = AttrsSet.end(); I != E; ) {
  117. FoldingSetIterator<AttributeImpl> Elem = I++;
  118. delete &*Elem;
  119. }
  120. // Destroy attribute lists.
  121. for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
  122. E = AttrsLists.end(); I != E; ) {
  123. FoldingSetIterator<AttributeSetImpl> Elem = I++;
  124. delete &*Elem;
  125. }
  126. // Destroy attribute node lists.
  127. for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
  128. E = AttrsSetNodes.end(); I != E; ) {
  129. FoldingSetIterator<AttributeSetNode> Elem = I++;
  130. delete &*Elem;
  131. }
  132. // Destroy MetadataAsValues.
  133. {
  134. SmallVector<MetadataAsValue *, 8> MDVs;
  135. MDVs.reserve(MetadataAsValues.size());
  136. for (auto &Pair : MetadataAsValues)
  137. MDVs.push_back(Pair.second);
  138. MetadataAsValues.clear();
  139. for (auto *V : MDVs)
  140. delete V;
  141. }
  142. // Destroy ValuesAsMetadata.
  143. for (auto &Pair : ValuesAsMetadata)
  144. delete Pair.second;
  145. // Destroy MDStrings.
  146. MDStringCache.clear();
  147. }
  148. void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
  149. bool Changed;
  150. do {
  151. Changed = false;
  152. for (auto I = ArrayConstants.map_begin(), E = ArrayConstants.map_end();
  153. I != E; ) {
  154. auto *C = I->first;
  155. I++;
  156. if (C->use_empty()) {
  157. Changed = true;
  158. C->destroyConstant();
  159. }
  160. }
  161. } while (Changed);
  162. }
  163. void Module::dropTriviallyDeadConstantArrays() {
  164. Context.pImpl->dropTriviallyDeadConstantArrays();
  165. }
  166. namespace llvm {
  167. /// \brief Make MDOperand transparent for hashing.
  168. ///
  169. /// This overload of an implementation detail of the hashing library makes
  170. /// MDOperand hash to the same value as a \a Metadata pointer.
  171. ///
  172. /// Note that overloading \a hash_value() as follows:
  173. ///
  174. /// \code
  175. /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
  176. /// \endcode
  177. ///
  178. /// does not cause MDOperand to be transparent. In particular, a bare pointer
  179. /// doesn't get hashed before it's combined, whereas \a MDOperand would.
  180. static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
  181. }
  182. unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
  183. unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
  184. #ifndef NDEBUG
  185. {
  186. SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
  187. unsigned RawHash = calculateHash(MDs);
  188. assert(Hash == RawHash &&
  189. "Expected hash of MDOperand to equal hash of Metadata*");
  190. }
  191. #endif
  192. return Hash;
  193. }
  194. unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
  195. return hash_combine_range(Ops.begin(), Ops.end());
  196. }
  197. // ConstantsContext anchors
  198. void UnaryConstantExpr::anchor() { }
  199. void BinaryConstantExpr::anchor() { }
  200. void SelectConstantExpr::anchor() { }
  201. void ExtractElementConstantExpr::anchor() { }
  202. void InsertElementConstantExpr::anchor() { }
  203. void ShuffleVectorConstantExpr::anchor() { }
  204. void ExtractValueConstantExpr::anchor() { }
  205. void InsertValueConstantExpr::anchor() { }
  206. void GetElementPtrConstantExpr::anchor() { }
  207. void CompareConstantExpr::anchor() { }