LLVMContext.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
  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 LLVMContext, as a wrapper around the opaque
  11. // class LLVMContextImpl.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/LLVMContext.h"
  15. #include "LLVMContextImpl.h"
  16. #include "llvm/IR/Constants.h"
  17. #include "llvm/IR/DebugLoc.h"
  18. #include "llvm/IR/DiagnosticInfo.h"
  19. #include "llvm/IR/DiagnosticPrinter.h"
  20. #include "llvm/IR/Instruction.h"
  21. #include "llvm/IR/Metadata.h"
  22. #include "llvm/Support/ManagedStatic.h"
  23. #include "llvm/Support/SourceMgr.h"
  24. #include <cctype>
  25. using namespace llvm;
  26. static ManagedStatic<LLVMContext> GlobalContext;
  27. LLVMContext& llvm::getGlobalContext() {
  28. return *GlobalContext;
  29. }
  30. LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
  31. // Create the fixed metadata kinds. This is done in the same order as the
  32. // MD_* enum values so that they correspond.
  33. // Create the 'dbg' metadata kind.
  34. unsigned DbgID = getMDKindID("dbg");
  35. assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
  36. // Create the 'tbaa' metadata kind.
  37. unsigned TBAAID = getMDKindID("tbaa");
  38. assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
  39. // Create the 'prof' metadata kind.
  40. unsigned ProfID = getMDKindID("prof");
  41. assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
  42. // Create the 'fpmath' metadata kind.
  43. unsigned FPAccuracyID = getMDKindID("fpmath");
  44. assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
  45. (void)FPAccuracyID;
  46. // Create the 'range' metadata kind.
  47. unsigned RangeID = getMDKindID("range");
  48. assert(RangeID == MD_range && "range kind id drifted");
  49. (void)RangeID;
  50. // Create the 'tbaa.struct' metadata kind.
  51. unsigned TBAAStructID = getMDKindID("tbaa.struct");
  52. assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
  53. (void)TBAAStructID;
  54. // Create the 'invariant.load' metadata kind.
  55. unsigned InvariantLdId = getMDKindID("invariant.load");
  56. assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
  57. (void)InvariantLdId;
  58. }
  59. LLVMContext::~LLVMContext() { delete pImpl; }
  60. void LLVMContext::addModule(Module *M) {
  61. pImpl->OwnedModules.insert(M);
  62. }
  63. void LLVMContext::removeModule(Module *M) {
  64. pImpl->OwnedModules.erase(M);
  65. }
  66. //===----------------------------------------------------------------------===//
  67. // Recoverable Backend Errors
  68. //===----------------------------------------------------------------------===//
  69. void LLVMContext::
  70. setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
  71. void *DiagContext) {
  72. pImpl->InlineAsmDiagHandler = DiagHandler;
  73. pImpl->InlineAsmDiagContext = DiagContext;
  74. }
  75. /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
  76. /// setInlineAsmDiagnosticHandler.
  77. LLVMContext::InlineAsmDiagHandlerTy
  78. LLVMContext::getInlineAsmDiagnosticHandler() const {
  79. return pImpl->InlineAsmDiagHandler;
  80. }
  81. /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
  82. /// setInlineAsmDiagnosticHandler.
  83. void *LLVMContext::getInlineAsmDiagnosticContext() const {
  84. return pImpl->InlineAsmDiagContext;
  85. }
  86. void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
  87. void *DiagnosticContext) {
  88. pImpl->DiagnosticHandler = DiagnosticHandler;
  89. pImpl->DiagnosticContext = DiagnosticContext;
  90. }
  91. LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
  92. return pImpl->DiagnosticHandler;
  93. }
  94. void *LLVMContext::getDiagnosticContext() const {
  95. return pImpl->DiagnosticContext;
  96. }
  97. void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle)
  98. {
  99. pImpl->YieldCallback = Callback;
  100. pImpl->YieldOpaqueHandle = OpaqueHandle;
  101. }
  102. void LLVMContext::yield() {
  103. if (pImpl->YieldCallback)
  104. pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle);
  105. }
  106. void LLVMContext::emitError(const Twine &ErrorStr) {
  107. diagnose(DiagnosticInfoInlineAsm(ErrorStr));
  108. }
  109. void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
  110. assert (I && "Invalid instruction");
  111. diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
  112. }
  113. void LLVMContext::diagnose(const DiagnosticInfo &DI) {
  114. // If there is a report handler, use it.
  115. if (pImpl->DiagnosticHandler) {
  116. pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
  117. return;
  118. }
  119. // Optimization remarks are selective. They need to check whether
  120. // the regexp pattern, passed via -pass-remarks, matches the name
  121. // of the pass that is emitting the diagnostic. If there is no match,
  122. // ignore the diagnostic and return.
  123. if (DI.getKind() == llvm::DK_OptimizationRemark &&
  124. !pImpl->optimizationRemarksEnabledFor(
  125. cast<DiagnosticInfoOptimizationRemark>(DI).getPassName()))
  126. return;
  127. // Otherwise, print the message with a prefix based on the severity.
  128. std::string MsgStorage;
  129. raw_string_ostream Stream(MsgStorage);
  130. DiagnosticPrinterRawOStream DP(Stream);
  131. DI.print(DP);
  132. Stream.flush();
  133. switch (DI.getSeverity()) {
  134. case DS_Error:
  135. errs() << "error: " << MsgStorage << "\n";
  136. exit(1);
  137. case DS_Warning:
  138. errs() << "warning: " << MsgStorage << "\n";
  139. break;
  140. case DS_Remark:
  141. errs() << "remark: " << MsgStorage << "\n";
  142. break;
  143. case DS_Note:
  144. errs() << "note: " << MsgStorage << "\n";
  145. break;
  146. }
  147. }
  148. void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
  149. diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
  150. }
  151. void LLVMContext::emitOptimizationRemark(const char *PassName,
  152. const Function &Fn,
  153. const DebugLoc &DLoc,
  154. const Twine &Msg) {
  155. diagnose(DiagnosticInfoOptimizationRemark(PassName, Fn, DLoc, Msg));
  156. }
  157. //===----------------------------------------------------------------------===//
  158. // Metadata Kind Uniquing
  159. //===----------------------------------------------------------------------===//
  160. #ifndef NDEBUG
  161. /// isValidName - Return true if Name is a valid custom metadata handler name.
  162. static bool isValidName(StringRef MDName) {
  163. if (MDName.empty())
  164. return false;
  165. if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
  166. return false;
  167. for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
  168. ++I) {
  169. if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
  170. *I != '-' && *I != '.')
  171. return false;
  172. }
  173. return true;
  174. }
  175. #endif
  176. /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
  177. unsigned LLVMContext::getMDKindID(StringRef Name) const {
  178. assert(isValidName(Name) && "Invalid MDNode name");
  179. // If this is new, assign it its ID.
  180. return
  181. pImpl->CustomMDKindNames.GetOrCreateValue(
  182. Name, pImpl->CustomMDKindNames.size()).second;
  183. }
  184. /// getHandlerNames - Populate client supplied smallvector using custome
  185. /// metadata name and ID.
  186. void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
  187. Names.resize(pImpl->CustomMDKindNames.size());
  188. for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
  189. E = pImpl->CustomMDKindNames.end(); I != E; ++I)
  190. Names[I->second] = I->first();
  191. }