LLVM  mainline
LLVMContext.h
Go to the documentation of this file.
00001 //===-- llvm/LLVMContext.h - Class for managing "global" state --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares LLVMContext, a container of "global" state in LLVM, such
00011 // as the global type and constant uniquing tables.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_IR_LLVMCONTEXT_H
00016 #define LLVM_IR_LLVMCONTEXT_H
00017 
00018 #include "llvm-c/Core.h"
00019 #include "llvm/Support/CBindingWrapping.h"
00020 #include "llvm/Support/Compiler.h"
00021 #include "llvm/Support/Options.h"
00022 
00023 namespace llvm {
00024 
00025 class LLVMContextImpl;
00026 class StringRef;
00027 class Twine;
00028 class Instruction;
00029 class Module;
00030 class SMDiagnostic;
00031 class DiagnosticInfo;
00032 template <typename T> class SmallVectorImpl;
00033 class Function;
00034 class DebugLoc;
00035 
00036 /// This is an important class for using LLVM in a threaded context.  It
00037 /// (opaquely) owns and manages the core "global" data of LLVM's core
00038 /// infrastructure, including the type and constant uniquing tables.
00039 /// LLVMContext itself provides no locking guarantees, so you should be careful
00040 /// to have one context per thread.
00041 class LLVMContext {
00042 public:
00043   LLVMContextImpl *const pImpl;
00044   LLVMContext();
00045   ~LLVMContext();
00046 
00047   // Pinned metadata names, which always have the same value.  This is a
00048   // compile-time performance optimization, not a correctness optimization.
00049   enum {
00050     MD_dbg = 0,  // "dbg"
00051     MD_tbaa = 1, // "tbaa"
00052     MD_prof = 2,  // "prof"
00053     MD_fpmath = 3,  // "fpmath"
00054     MD_range = 4, // "range"
00055     MD_tbaa_struct = 5, // "tbaa.struct"
00056     MD_invariant_load = 6, // "invariant.load"
00057     MD_alias_scope = 7, // "alias.scope"
00058     MD_noalias = 8, // "noalias",
00059     MD_nontemporal = 9, // "nontemporal"
00060     MD_mem_parallel_loop_access = 10, // "llvm.mem.parallel_loop_access"
00061     MD_nonnull = 11, // "nonnull"
00062     MD_dereferenceable = 12, // "dereferenceable"
00063     MD_dereferenceable_or_null = 13 // "dereferenceable_or_null"
00064   };
00065 
00066   /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
00067   /// This ID is uniqued across modules in the current LLVMContext.
00068   unsigned getMDKindID(StringRef Name) const;
00069 
00070   /// getMDKindNames - Populate client supplied SmallVector with the name for
00071   /// custom metadata IDs registered in this LLVMContext.
00072   void getMDKindNames(SmallVectorImpl<StringRef> &Result) const;
00073 
00074 
00075   typedef void (*InlineAsmDiagHandlerTy)(const SMDiagnostic&, void *Context,
00076                                          unsigned LocCookie);
00077 
00078   /// Defines the type of a diagnostic handler.
00079   /// \see LLVMContext::setDiagnosticHandler.
00080   /// \see LLVMContext::diagnose.
00081   typedef void (*DiagnosticHandlerTy)(const DiagnosticInfo &DI, void *Context);
00082 
00083   /// Defines the type of a yield callback.
00084   /// \see LLVMContext::setYieldCallback.
00085   typedef void (*YieldCallbackTy)(LLVMContext *Context, void *OpaqueHandle);
00086 
00087   /// setInlineAsmDiagnosticHandler - This method sets a handler that is invoked
00088   /// when problems with inline asm are detected by the backend.  The first
00089   /// argument is a function pointer and the second is a context pointer that
00090   /// gets passed into the DiagHandler.
00091   ///
00092   /// LLVMContext doesn't take ownership or interpret either of these
00093   /// pointers.
00094   void setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
00095                                      void *DiagContext = nullptr);
00096 
00097   /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
00098   /// setInlineAsmDiagnosticHandler.
00099   InlineAsmDiagHandlerTy getInlineAsmDiagnosticHandler() const;
00100 
00101   /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
00102   /// setInlineAsmDiagnosticHandler.
00103   void *getInlineAsmDiagnosticContext() const;
00104 
00105   /// setDiagnosticHandler - This method sets a handler that is invoked
00106   /// when the backend needs to report anything to the user.  The first
00107   /// argument is a function pointer and the second is a context pointer that
00108   /// gets passed into the DiagHandler.  The third argument should be set to
00109   /// true if the handler only expects enabled diagnostics.
00110   ///
00111   /// LLVMContext doesn't take ownership or interpret either of these
00112   /// pointers.
00113   void setDiagnosticHandler(DiagnosticHandlerTy DiagHandler,
00114                             void *DiagContext = nullptr,
00115                             bool RespectFilters = false);
00116 
00117   /// getDiagnosticHandler - Return the diagnostic handler set by
00118   /// setDiagnosticHandler.
00119   DiagnosticHandlerTy getDiagnosticHandler() const;
00120 
00121   /// getDiagnosticContext - Return the diagnostic context set by
00122   /// setDiagnosticContext.
00123   void *getDiagnosticContext() const;
00124 
00125   /// \brief Report a message to the currently installed diagnostic handler.
00126   ///
00127   /// This function returns, in particular in the case of error reporting
00128   /// (DI.Severity == \a DS_Error), so the caller should leave the compilation
00129   /// process in a self-consistent state, even though the generated code
00130   /// need not be correct.
00131   ///
00132   /// The diagnostic message will be implicitly prefixed with a severity keyword
00133   /// according to \p DI.getSeverity(), i.e., "error: " for \a DS_Error,
00134   /// "warning: " for \a DS_Warning, and "note: " for \a DS_Note.
00135   void diagnose(const DiagnosticInfo &DI);
00136 
00137   /// \brief Registers a yield callback with the given context.
00138   ///
00139   /// The yield callback function may be called by LLVM to transfer control back
00140   /// to the client that invoked the LLVM compilation. This can be used to yield
00141   /// control of the thread, or perform periodic work needed by the client.
00142   /// There is no guaranteed frequency at which callbacks must occur; in fact,
00143   /// the client is not guaranteed to ever receive this callback. It is at the
00144   /// sole discretion of LLVM to do so and only if it can guarantee that
00145   /// suspending the thread won't block any forward progress in other LLVM
00146   /// contexts in the same process.
00147   ///
00148   /// At a suspend point, the state of the current LLVM context is intentionally
00149   /// undefined. No assumptions about it can or should be made. Only LLVM
00150   /// context API calls that explicitly state that they can be used during a
00151   /// yield callback are allowed to be used. Any other API calls into the
00152   /// context are not supported until the yield callback function returns
00153   /// control to LLVM. Other LLVM contexts are unaffected by this restriction.
00154   void setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle);
00155 
00156   /// \brief Calls the yield callback (if applicable).
00157   ///
00158   /// This transfers control of the current thread back to the client, which may
00159   /// suspend the current thread. Only call this method when LLVM doesn't hold
00160   /// any global mutex or cannot block the execution in another LLVM context.
00161   void yield();
00162 
00163   /// emitError - Emit an error message to the currently installed error handler
00164   /// with optional location information.  This function returns, so code should
00165   /// be prepared to drop the erroneous construct on the floor and "not crash".
00166   /// The generated code need not be correct.  The error message will be
00167   /// implicitly prefixed with "error: " and should not end with a ".".
00168   void emitError(unsigned LocCookie, const Twine &ErrorStr);
00169   void emitError(const Instruction *I, const Twine &ErrorStr);
00170   void emitError(const Twine &ErrorStr);
00171 
00172   /// \brief Query for a debug option's value.
00173   ///
00174   /// This function returns typed data populated from command line parsing.
00175   template <typename ValT, typename Base, ValT(Base::*Mem)>
00176   ValT getOption() const {
00177     return OptionRegistry::instance().template get<ValT, Base, Mem>();
00178   }
00179 
00180 private:
00181   LLVMContext(LLVMContext&) = delete;
00182   void operator=(LLVMContext&) = delete;
00183 
00184   /// addModule - Register a module as being instantiated in this context.  If
00185   /// the context is deleted, the module will be deleted as well.
00186   void addModule(Module*);
00187 
00188   /// removeModule - Unregister a module from this context.
00189   void removeModule(Module*);
00190 
00191   // Module needs access to the add/removeModule methods.
00192   friend class Module;
00193 };
00194 
00195 /// getGlobalContext - Returns a global context.  This is for LLVM clients that
00196 /// only care about operating on a single thread.
00197 extern LLVMContext &getGlobalContext();
00198 
00199 // Create wrappers for C Binding types (see CBindingWrapping.h).
00200 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLVMContext, LLVMContextRef)
00201 
00202 /* Specialized opaque context conversions.
00203  */
00204 inline LLVMContext **unwrap(LLVMContextRef* Tys) {
00205   return reinterpret_cast<LLVMContext**>(Tys);
00206 }
00207 
00208 inline LLVMContextRef *wrap(const LLVMContext **Tys) {
00209   return reinterpret_cast<LLVMContextRef*>(const_cast<LLVMContext**>(Tys));
00210 }
00211 
00212 }
00213 
00214 #endif