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