LLVMContextImpl.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/Module.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/Regex.h"
  19. #include <algorithm>
  20. using namespace llvm;
  21. LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
  22. : TheTrueVal(nullptr), TheFalseVal(nullptr),
  23. VoidTy(C, Type::VoidTyID),
  24. LabelTy(C, Type::LabelTyID),
  25. HalfTy(C, Type::HalfTyID),
  26. FloatTy(C, Type::FloatTyID),
  27. DoubleTy(C, Type::DoubleTyID),
  28. MetadataTy(C, Type::MetadataTyID),
  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. InlineAsmDiagHandler = nullptr;
  39. InlineAsmDiagContext = nullptr;
  40. DiagnosticHandler = nullptr;
  41. DiagnosticContext = nullptr;
  42. YieldCallback = nullptr;
  43. YieldOpaqueHandle = nullptr;
  44. NamedStructTypesUniqueID = 0;
  45. }
  46. namespace {
  47. /// \brief Regular expression corresponding to the value given in the
  48. /// command line flag -pass-remarks. Passes whose name matches this
  49. /// regexp will emit a diagnostic when calling
  50. /// LLVMContext::emitOptimizationRemark.
  51. static Regex *OptimizationRemarkPattern = nullptr;
  52. struct PassRemarksOpt {
  53. void operator=(const std::string &Val) const {
  54. // Create a regexp object to match pass names for emitOptimizationRemark.
  55. if (!Val.empty()) {
  56. delete OptimizationRemarkPattern;
  57. OptimizationRemarkPattern = new Regex(Val);
  58. std::string RegexError;
  59. if (!OptimizationRemarkPattern->isValid(RegexError))
  60. report_fatal_error("Invalid regular expression '" + Val +
  61. "' in -pass-remarks: " + RegexError,
  62. false);
  63. }
  64. };
  65. };
  66. static PassRemarksOpt PassRemarksOptLoc;
  67. // -pass-remarks
  68. // Command line flag to enable LLVMContext::emitOptimizationRemark()
  69. // and LLVMContext::emitOptimizationNote() calls.
  70. static cl::opt<PassRemarksOpt, true, cl::parser<std::string>>
  71. PassRemarks("pass-remarks", cl::value_desc("pattern"),
  72. cl::desc("Enable optimization remarks from passes whose name match "
  73. "the given regular expression"),
  74. cl::Hidden, cl::location(PassRemarksOptLoc), cl::ValueRequired,
  75. cl::ZeroOrMore);
  76. }
  77. bool
  78. LLVMContextImpl::optimizationRemarksEnabledFor(const char *PassName) const {
  79. return OptimizationRemarkPattern &&
  80. OptimizationRemarkPattern->match(PassName);
  81. }
  82. namespace {
  83. struct DropReferences {
  84. // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
  85. // is a Constant*.
  86. template<typename PairT>
  87. void operator()(const PairT &P) {
  88. P.second->dropAllReferences();
  89. }
  90. };
  91. // Temporary - drops pair.first instead of second.
  92. struct DropFirst {
  93. // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
  94. // is a Constant*.
  95. template<typename PairT>
  96. void operator()(const PairT &P) {
  97. P.first->dropAllReferences();
  98. }
  99. };
  100. }
  101. LLVMContextImpl::~LLVMContextImpl() {
  102. // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
  103. // will call LLVMContextImpl::removeModule, thus invalidating iterators into
  104. // the container. Avoid iterators during this operation:
  105. while (!OwnedModules.empty())
  106. delete *OwnedModules.begin();
  107. // Free the constants. This is important to do here to ensure that they are
  108. // freed before the LeakDetector is torn down.
  109. std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
  110. DropReferences());
  111. std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
  112. DropFirst());
  113. std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
  114. DropFirst());
  115. std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
  116. DropFirst());
  117. ExprConstants.freeConstants();
  118. ArrayConstants.freeConstants();
  119. StructConstants.freeConstants();
  120. VectorConstants.freeConstants();
  121. DeleteContainerSeconds(CAZConstants);
  122. DeleteContainerSeconds(CPNConstants);
  123. DeleteContainerSeconds(UVConstants);
  124. InlineAsms.freeConstants();
  125. DeleteContainerSeconds(IntConstants);
  126. DeleteContainerSeconds(FPConstants);
  127. for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
  128. E = CDSConstants.end(); I != E; ++I)
  129. delete I->second;
  130. CDSConstants.clear();
  131. // Destroy attributes.
  132. for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
  133. E = AttrsSet.end(); I != E; ) {
  134. FoldingSetIterator<AttributeImpl> Elem = I++;
  135. delete &*Elem;
  136. }
  137. // Destroy attribute lists.
  138. for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
  139. E = AttrsLists.end(); I != E; ) {
  140. FoldingSetIterator<AttributeSetImpl> Elem = I++;
  141. delete &*Elem;
  142. }
  143. // Destroy attribute node lists.
  144. for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
  145. E = AttrsSetNodes.end(); I != E; ) {
  146. FoldingSetIterator<AttributeSetNode> Elem = I++;
  147. delete &*Elem;
  148. }
  149. // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
  150. // and the NonUniquedMDNodes sets, so copy the values out first.
  151. SmallVector<MDNode*, 8> MDNodes;
  152. MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
  153. for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
  154. I != E; ++I)
  155. MDNodes.push_back(&*I);
  156. MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
  157. for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
  158. E = MDNodes.end(); I != E; ++I)
  159. (*I)->destroy();
  160. assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
  161. "Destroying all MDNodes didn't empty the Context's sets.");
  162. // Destroy MDStrings.
  163. DeleteContainerSeconds(MDStringCache);
  164. }
  165. // ConstantsContext anchors
  166. void UnaryConstantExpr::anchor() { }
  167. void BinaryConstantExpr::anchor() { }
  168. void SelectConstantExpr::anchor() { }
  169. void ExtractElementConstantExpr::anchor() { }
  170. void InsertElementConstantExpr::anchor() { }
  171. void ShuffleVectorConstantExpr::anchor() { }
  172. void ExtractValueConstantExpr::anchor() { }
  173. void InsertValueConstantExpr::anchor() { }
  174. void GetElementPtrConstantExpr::anchor() { }
  175. void CompareConstantExpr::anchor() { }